aircable.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * AIRcable USB Bluetooth Dongle Driver.
  4. *
  5. * Copyright (C) 2010 Johan Hovold <jhovold@gmail.com>
  6. * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com)
  7. *
  8. * The device works as an standard CDC device, it has 2 interfaces, the first
  9. * one is for firmware access and the second is the serial one.
  10. * The protocol is very simply, there are two possibilities reading or writing.
  11. * When writing the first urb must have a Header that starts with 0x20 0x29 the
  12. * next two bytes must say how much data will be sent.
  13. * When reading the process is almost equal except that the header starts with
  14. * 0x00 0x20.
  15. *
  16. * The device simply need some stuff to understand data coming from the usb
  17. * buffer: The First and Second byte is used for a Header, the Third and Fourth
  18. * tells the device the amount of information the package holds.
  19. * Packages are 60 bytes long Header Stuff.
  20. * When writing to the device the first two bytes of the header are 0x20 0x29
  21. * When reading the bytes are 0x00 0x20, or 0x00 0x10, there is an strange
  22. * situation, when too much data arrives to the device because it sends the data
  23. * but with out the header. I will use a simply hack to override this situation,
  24. * if there is data coming that does not contain any header, then that is data
  25. * that must go directly to the tty, as there is no documentation about if there
  26. * is any other control code, I will simply check for the first
  27. * one.
  28. *
  29. * I have taken some info from a Greg Kroah-Hartman article:
  30. * http://www.linuxjournal.com/article/6573
  31. * And from Linux Device Driver Kit CD, which is a great work, the authors taken
  32. * the work to recompile lots of information an knowledge in drivers development
  33. * and made it all available inside a cd.
  34. * URL: http://kernel.org/pub/linux/kernel/people/gregkh/ddk/
  35. *
  36. */
  37. #include <asm/unaligned.h>
  38. #include <linux/tty.h>
  39. #include <linux/slab.h>
  40. #include <linux/module.h>
  41. #include <linux/tty_flip.h>
  42. #include <linux/usb.h>
  43. #include <linux/usb/serial.h>
  44. /* Vendor and Product ID */
  45. #define AIRCABLE_VID 0x16CA
  46. #define AIRCABLE_USB_PID 0x1502
  47. /* Protocol Stuff */
  48. #define HCI_HEADER_LENGTH 0x4
  49. #define TX_HEADER_0 0x20
  50. #define TX_HEADER_1 0x29
  51. #define RX_HEADER_0 0x00
  52. #define RX_HEADER_1 0x20
  53. #define HCI_COMPLETE_FRAME 64
  54. /* rx_flags */
  55. #define THROTTLED 0x01
  56. #define ACTUALLY_THROTTLED 0x02
  57. #define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>"
  58. #define DRIVER_DESC "AIRcable USB Driver"
  59. /* ID table that will be registered with USB core */
  60. static const struct usb_device_id id_table[] = {
  61. { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
  62. { },
  63. };
  64. MODULE_DEVICE_TABLE(usb, id_table);
  65. static int aircable_prepare_write_buffer(struct usb_serial_port *port,
  66. void *dest, size_t size)
  67. {
  68. int count;
  69. unsigned char *buf = dest;
  70. count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH,
  71. size - HCI_HEADER_LENGTH, &port->lock);
  72. buf[0] = TX_HEADER_0;
  73. buf[1] = TX_HEADER_1;
  74. put_unaligned_le16(count, &buf[2]);
  75. return count + HCI_HEADER_LENGTH;
  76. }
  77. static int aircable_calc_num_ports(struct usb_serial *serial,
  78. struct usb_serial_endpoints *epds)
  79. {
  80. /* Ignore the first interface, which has no bulk endpoints. */
  81. if (epds->num_bulk_out == 0) {
  82. dev_dbg(&serial->interface->dev,
  83. "ignoring interface with no bulk-out endpoints\n");
  84. return -ENODEV;
  85. }
  86. return 1;
  87. }
  88. static int aircable_process_packet(struct usb_serial_port *port,
  89. int has_headers, char *packet, int len)
  90. {
  91. if (has_headers) {
  92. len -= HCI_HEADER_LENGTH;
  93. packet += HCI_HEADER_LENGTH;
  94. }
  95. if (len <= 0) {
  96. dev_dbg(&port->dev, "%s - malformed packet\n", __func__);
  97. return 0;
  98. }
  99. tty_insert_flip_string(&port->port, packet, len);
  100. return len;
  101. }
  102. static void aircable_process_read_urb(struct urb *urb)
  103. {
  104. struct usb_serial_port *port = urb->context;
  105. char *data = (char *)urb->transfer_buffer;
  106. int has_headers;
  107. int count;
  108. int len;
  109. int i;
  110. has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0);
  111. count = 0;
  112. for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) {
  113. len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME);
  114. count += aircable_process_packet(port, has_headers,
  115. &data[i], len);
  116. }
  117. if (count)
  118. tty_flip_buffer_push(&port->port);
  119. }
  120. static struct usb_serial_driver aircable_device = {
  121. .driver = {
  122. .owner = THIS_MODULE,
  123. .name = "aircable",
  124. },
  125. .id_table = id_table,
  126. .bulk_out_size = HCI_COMPLETE_FRAME,
  127. .calc_num_ports = aircable_calc_num_ports,
  128. .process_read_urb = aircable_process_read_urb,
  129. .prepare_write_buffer = aircable_prepare_write_buffer,
  130. .throttle = usb_serial_generic_throttle,
  131. .unthrottle = usb_serial_generic_unthrottle,
  132. };
  133. static struct usb_serial_driver * const serial_drivers[] = {
  134. &aircable_device, NULL
  135. };
  136. module_usb_serial_driver(serial_drivers, id_table);
  137. MODULE_AUTHOR(DRIVER_AUTHOR);
  138. MODULE_DESCRIPTION(DRIVER_DESC);
  139. MODULE_LICENSE("GPL v2");