aircable.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * AIRcable USB Bluetooth Dongle Driver.
  3. *
  4. * Copyright (C) 2010 Johan Hovold <jhovold@gmail.com>
  5. * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU General Public License version 2 as published by the
  9. * Free Software Foundation.
  10. *
  11. * The device works as an standard CDC device, it has 2 interfaces, the first
  12. * one is for firmware access and the second is the serial one.
  13. * The protocol is very simply, there are two posibilities reading or writing.
  14. * When writing the first urb must have a Header that starts with 0x20 0x29 the
  15. * next two bytes must say how much data will be sended.
  16. * When reading the process is almost equal except that the header starts with
  17. * 0x00 0x20.
  18. *
  19. * The device simply need some stuff to understand data coming from the usb
  20. * buffer: The First and Second byte is used for a Header, the Third and Fourth
  21. * tells the device the amount of information the package holds.
  22. * Packages are 60 bytes long Header Stuff.
  23. * When writing to the device the first two bytes of the header are 0x20 0x29
  24. * When reading the bytes are 0x00 0x20, or 0x00 0x10, there is an strange
  25. * situation, when too much data arrives to the device because it sends the data
  26. * but with out the header. I will use a simply hack to override this situation,
  27. * if there is data coming that does not contain any header, then that is data
  28. * that must go directly to the tty, as there is no documentation about if there
  29. * is any other control code, I will simply check for the first
  30. * one.
  31. *
  32. * The driver registers himself with the USB-serial core and the USB Core. I had
  33. * to implement a probe function against USB-serial, because other way, the
  34. * driver was attaching himself to both interfaces. I have tryed with different
  35. * configurations of usb_serial_driver with out exit, only the probe function
  36. * could handle this correctly.
  37. *
  38. * I have taken some info from a Greg Kroah-Hartman article:
  39. * http://www.linuxjournal.com/article/6573
  40. * And from Linux Device Driver Kit CD, which is a great work, the authors taken
  41. * the work to recompile lots of information an knowladge in drivers development
  42. * and made it all avaible inside a cd.
  43. * URL: http://kernel.org/pub/linux/kernel/people/gregkh/ddk/
  44. *
  45. */
  46. #include <asm/unaligned.h>
  47. #include <linux/tty.h>
  48. #include <linux/slab.h>
  49. #include <linux/module.h>
  50. #include <linux/tty_flip.h>
  51. #include <linux/usb.h>
  52. #include <linux/usb/serial.h>
  53. static bool debug;
  54. /* Vendor and Product ID */
  55. #define AIRCABLE_VID 0x16CA
  56. #define AIRCABLE_USB_PID 0x1502
  57. /* Protocol Stuff */
  58. #define HCI_HEADER_LENGTH 0x4
  59. #define TX_HEADER_0 0x20
  60. #define TX_HEADER_1 0x29
  61. #define RX_HEADER_0 0x00
  62. #define RX_HEADER_1 0x20
  63. #define HCI_COMPLETE_FRAME 64
  64. /* rx_flags */
  65. #define THROTTLED 0x01
  66. #define ACTUALLY_THROTTLED 0x02
  67. /*
  68. * Version Information
  69. */
  70. #define DRIVER_VERSION "v2.0"
  71. #define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>"
  72. #define DRIVER_DESC "AIRcable USB Driver"
  73. /* ID table that will be registered with USB core */
  74. static const struct usb_device_id id_table[] = {
  75. { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
  76. { },
  77. };
  78. MODULE_DEVICE_TABLE(usb, id_table);
  79. static int aircable_prepare_write_buffer(struct usb_serial_port *port,
  80. void *dest, size_t size)
  81. {
  82. int count;
  83. unsigned char *buf = dest;
  84. count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH,
  85. size - HCI_HEADER_LENGTH, &port->lock);
  86. buf[0] = TX_HEADER_0;
  87. buf[1] = TX_HEADER_1;
  88. put_unaligned_le16(count, &buf[2]);
  89. return count + HCI_HEADER_LENGTH;
  90. }
  91. static int aircable_probe(struct usb_serial *serial,
  92. const struct usb_device_id *id)
  93. {
  94. struct usb_host_interface *iface_desc = serial->interface->
  95. cur_altsetting;
  96. struct usb_endpoint_descriptor *endpoint;
  97. int num_bulk_out = 0;
  98. int i;
  99. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  100. endpoint = &iface_desc->endpoint[i].desc;
  101. if (usb_endpoint_is_bulk_out(endpoint)) {
  102. dbg("found bulk out on endpoint %d", i);
  103. ++num_bulk_out;
  104. }
  105. }
  106. if (num_bulk_out == 0) {
  107. dbg("Invalid interface, discarding");
  108. return -ENODEV;
  109. }
  110. return 0;
  111. }
  112. static int aircable_process_packet(struct tty_struct *tty,
  113. struct usb_serial_port *port, int has_headers,
  114. char *packet, int len)
  115. {
  116. if (has_headers) {
  117. len -= HCI_HEADER_LENGTH;
  118. packet += HCI_HEADER_LENGTH;
  119. }
  120. if (len <= 0) {
  121. dbg("%s - malformed packet", __func__);
  122. return 0;
  123. }
  124. tty_insert_flip_string(tty, packet, len);
  125. return len;
  126. }
  127. static void aircable_process_read_urb(struct urb *urb)
  128. {
  129. struct usb_serial_port *port = urb->context;
  130. char *data = (char *)urb->transfer_buffer;
  131. struct tty_struct *tty;
  132. int has_headers;
  133. int count;
  134. int len;
  135. int i;
  136. tty = tty_port_tty_get(&port->port);
  137. if (!tty)
  138. return;
  139. has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0);
  140. count = 0;
  141. for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) {
  142. len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME);
  143. count += aircable_process_packet(tty, port, has_headers,
  144. &data[i], len);
  145. }
  146. if (count)
  147. tty_flip_buffer_push(tty);
  148. tty_kref_put(tty);
  149. }
  150. static struct usb_driver aircable_driver = {
  151. .name = "aircable",
  152. .probe = usb_serial_probe,
  153. .disconnect = usb_serial_disconnect,
  154. .id_table = id_table,
  155. };
  156. static struct usb_serial_driver aircable_device = {
  157. .driver = {
  158. .owner = THIS_MODULE,
  159. .name = "aircable",
  160. },
  161. .id_table = id_table,
  162. .num_ports = 1,
  163. .bulk_out_size = HCI_COMPLETE_FRAME,
  164. .probe = aircable_probe,
  165. .process_read_urb = aircable_process_read_urb,
  166. .prepare_write_buffer = aircable_prepare_write_buffer,
  167. .throttle = usb_serial_generic_throttle,
  168. .unthrottle = usb_serial_generic_unthrottle,
  169. };
  170. static struct usb_serial_driver * const serial_drivers[] = {
  171. &aircable_device, NULL
  172. };
  173. module_usb_serial_driver(aircable_driver, serial_drivers);
  174. MODULE_AUTHOR(DRIVER_AUTHOR);
  175. MODULE_DESCRIPTION(DRIVER_DESC);
  176. MODULE_VERSION(DRIVER_VERSION);
  177. MODULE_LICENSE("GPL");
  178. module_param(debug, bool, S_IRUGO | S_IWUSR);
  179. MODULE_PARM_DESC(debug, "Debug enabled or not");