wishbone-serial.c 2.6 KB

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