wishbone-serial.c 2.4 KB

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