wishbone-serial.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * USB Wishbone-Serial adapter driver
  3. *
  4. * Copyright (C) 2013 Wesley W. Terpstra <w.terpstra@gsi.de>
  5. * Copyright (C) 2013 GSI Helmholtz Centre for Heavy Ion Research GmbH
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/tty.h>
  14. #include <linux/module.h>
  15. #include <linux/usb.h>
  16. #include <linux/usb/serial.h>
  17. #include <linux/uaccess.h>
  18. #define GSI_VENDOR_OPENCLOSE 0xB0
  19. static const struct usb_device_id id_table[] = {
  20. { USB_DEVICE_AND_INTERFACE_INFO(0x1D50, 0x6062, 0xFF, 0xFF, 0xFF) },
  21. { },
  22. };
  23. MODULE_DEVICE_TABLE(usb, id_table);
  24. /*
  25. * Etherbone must be told that a new stream has begun before data arrives.
  26. * This is necessary to restart the negotiation of Wishbone bus parameters.
  27. * Similarly, when the stream ends, Etherbone must be told so that the cycle
  28. * line can be driven low in the case that userspace failed to do so.
  29. */
  30. static int usb_gsi_openclose(struct usb_serial_port *port, int value)
  31. {
  32. struct usb_device *dev = port->serial->dev;
  33. return usb_control_msg(
  34. dev,
  35. usb_sndctrlpipe(dev, 0), /* Send to EP0OUT */
  36. GSI_VENDOR_OPENCLOSE,
  37. USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
  38. value, /* wValue = device is open(1) or closed(0) */
  39. port->serial->interface->cur_altsetting->desc.bInterfaceNumber,
  40. NULL, 0, /* There is no data stage */
  41. 5000); /* Timeout till operation fails */
  42. }
  43. static int wishbone_serial_open(struct tty_struct *tty,
  44. struct usb_serial_port *port)
  45. {
  46. int retval;
  47. retval = usb_gsi_openclose(port, 1);
  48. if (retval) {
  49. dev_err(&port->serial->dev->dev,
  50. "Could not mark device as open (%d)\n",
  51. retval);
  52. return retval;
  53. }
  54. retval = usb_serial_generic_open(tty, port);
  55. if (retval)
  56. usb_gsi_openclose(port, 0);
  57. return retval;
  58. }
  59. static void wishbone_serial_close(struct usb_serial_port *port)
  60. {
  61. usb_serial_generic_close(port);
  62. usb_gsi_openclose(port, 0);
  63. }
  64. static struct usb_serial_driver wishbone_serial_device = {
  65. .driver = {
  66. .owner = THIS_MODULE,
  67. .name = "wishbone_serial",
  68. },
  69. .id_table = id_table,
  70. .num_ports = 1,
  71. .open = &wishbone_serial_open,
  72. .close = &wishbone_serial_close,
  73. };
  74. static struct usb_serial_driver * const serial_drivers[] = {
  75. &wishbone_serial_device, NULL
  76. };
  77. module_usb_serial_driver(serial_drivers, id_table);
  78. MODULE_AUTHOR("Wesley W. Terpstra <w.terpstra@gsi.de>");
  79. MODULE_DESCRIPTION("USB Wishbone-Serial adapter");
  80. MODULE_LICENSE("GPL");