aircable.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 comming 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 agains 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/tty_flip.h>
  50. #include <linux/usb.h>
  51. #include <linux/usb/serial.h>
  52. static int debug;
  53. /* Vendor and Product ID */
  54. #define AIRCABLE_VID 0x16CA
  55. #define AIRCABLE_USB_PID 0x1502
  56. /* Protocol Stuff */
  57. #define HCI_HEADER_LENGTH 0x4
  58. #define TX_HEADER_0 0x20
  59. #define TX_HEADER_1 0x29
  60. #define RX_HEADER_0 0x00
  61. #define RX_HEADER_1 0x20
  62. #define HCI_COMPLETE_FRAME 64
  63. /* rx_flags */
  64. #define THROTTLED 0x01
  65. #define ACTUALLY_THROTTLED 0x02
  66. /*
  67. * Version Information
  68. */
  69. #define DRIVER_VERSION "v2.0"
  70. #define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>"
  71. #define DRIVER_DESC "AIRcable USB Driver"
  72. /* ID table that will be registered with USB core */
  73. static const struct usb_device_id id_table[] = {
  74. { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
  75. { },
  76. };
  77. MODULE_DEVICE_TABLE(usb, id_table);
  78. static int aircable_prepare_write_buffer(struct usb_serial_port *port,
  79. void *dest, size_t size)
  80. {
  81. int count;
  82. unsigned char *buf = dest;
  83. count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH,
  84. size - HCI_HEADER_LENGTH, &port->lock);
  85. buf[0] = TX_HEADER_0;
  86. buf[1] = TX_HEADER_1;
  87. put_unaligned_le16(count, &buf[2]);
  88. return count + HCI_HEADER_LENGTH;
  89. }
  90. static int aircable_probe(struct usb_serial *serial,
  91. const struct usb_device_id *id)
  92. {
  93. struct usb_host_interface *iface_desc = serial->interface->
  94. cur_altsetting;
  95. struct usb_endpoint_descriptor *endpoint;
  96. int num_bulk_out = 0;
  97. int i;
  98. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  99. endpoint = &iface_desc->endpoint[i].desc;
  100. if (usb_endpoint_is_bulk_out(endpoint)) {
  101. dbg("found bulk out on endpoint %d", i);
  102. ++num_bulk_out;
  103. }
  104. }
  105. if (num_bulk_out == 0) {
  106. dbg("Invalid interface, discarding");
  107. return -ENODEV;
  108. }
  109. return 0;
  110. }
  111. static int aircable_process_packet(struct tty_struct *tty,
  112. struct usb_serial_port *port, int has_headers,
  113. char *packet, int len)
  114. {
  115. if (has_headers) {
  116. len -= HCI_HEADER_LENGTH;
  117. packet += HCI_HEADER_LENGTH;
  118. }
  119. if (len <= 0) {
  120. dbg("%s - malformed packet", __func__);
  121. return 0;
  122. }
  123. tty_insert_flip_string(tty, packet, len);
  124. return len;
  125. }
  126. static void aircable_process_read_urb(struct urb *urb)
  127. {
  128. struct usb_serial_port *port = urb->context;
  129. char *data = (char *)urb->transfer_buffer;
  130. struct tty_struct *tty;
  131. int has_headers;
  132. int count;
  133. int len;
  134. int i;
  135. tty = tty_port_tty_get(&port->port);
  136. if (!tty)
  137. return;
  138. has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0);
  139. count = 0;
  140. for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) {
  141. len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME);
  142. count += aircable_process_packet(tty, port, has_headers,
  143. &data[i], len);
  144. }
  145. if (count)
  146. tty_flip_buffer_push(tty);
  147. tty_kref_put(tty);
  148. }
  149. static struct usb_driver aircable_driver = {
  150. .name = "aircable",
  151. .probe = usb_serial_probe,
  152. .disconnect = usb_serial_disconnect,
  153. .id_table = id_table,
  154. .no_dynamic_id = 1,
  155. };
  156. static struct usb_serial_driver aircable_device = {
  157. .driver = {
  158. .owner = THIS_MODULE,
  159. .name = "aircable",
  160. },
  161. .usb_driver = &aircable_driver,
  162. .id_table = id_table,
  163. .num_ports = 1,
  164. .bulk_out_size = HCI_COMPLETE_FRAME,
  165. .probe = aircable_probe,
  166. .process_read_urb = aircable_process_read_urb,
  167. .prepare_write_buffer = aircable_prepare_write_buffer,
  168. .throttle = usb_serial_generic_throttle,
  169. .unthrottle = usb_serial_generic_unthrottle,
  170. };
  171. static int __init aircable_init(void)
  172. {
  173. int retval;
  174. retval = usb_serial_register(&aircable_device);
  175. if (retval)
  176. goto failed_serial_register;
  177. retval = usb_register(&aircable_driver);
  178. if (retval)
  179. goto failed_usb_register;
  180. return 0;
  181. failed_usb_register:
  182. usb_serial_deregister(&aircable_device);
  183. failed_serial_register:
  184. return retval;
  185. }
  186. static void __exit aircable_exit(void)
  187. {
  188. usb_deregister(&aircable_driver);
  189. usb_serial_deregister(&aircable_device);
  190. }
  191. MODULE_AUTHOR(DRIVER_AUTHOR);
  192. MODULE_DESCRIPTION(DRIVER_DESC);
  193. MODULE_VERSION(DRIVER_VERSION);
  194. MODULE_LICENSE("GPL");
  195. module_init(aircable_init);
  196. module_exit(aircable_exit);
  197. module_param(debug, bool, S_IRUGO | S_IWUSR);
  198. MODULE_PARM_DESC(debug, "Debug enabled or not");