aircable.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 possibilities 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 sent.
  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. * I have taken some info from a Greg Kroah-Hartman article:
  33. * http://www.linuxjournal.com/article/6573
  34. * And from Linux Device Driver Kit CD, which is a great work, the authors taken
  35. * the work to recompile lots of information an knowledge in drivers development
  36. * and made it all available inside a cd.
  37. * URL: http://kernel.org/pub/linux/kernel/people/gregkh/ddk/
  38. *
  39. */
  40. #include <asm/unaligned.h>
  41. #include <linux/tty.h>
  42. #include <linux/slab.h>
  43. #include <linux/module.h>
  44. #include <linux/tty_flip.h>
  45. #include <linux/usb.h>
  46. #include <linux/usb/serial.h>
  47. /* Vendor and Product ID */
  48. #define AIRCABLE_VID 0x16CA
  49. #define AIRCABLE_USB_PID 0x1502
  50. /* Protocol Stuff */
  51. #define HCI_HEADER_LENGTH 0x4
  52. #define TX_HEADER_0 0x20
  53. #define TX_HEADER_1 0x29
  54. #define RX_HEADER_0 0x00
  55. #define RX_HEADER_1 0x20
  56. #define HCI_COMPLETE_FRAME 64
  57. /* rx_flags */
  58. #define THROTTLED 0x01
  59. #define ACTUALLY_THROTTLED 0x02
  60. #define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>"
  61. #define DRIVER_DESC "AIRcable USB Driver"
  62. /* ID table that will be registered with USB core */
  63. static const struct usb_device_id id_table[] = {
  64. { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
  65. { },
  66. };
  67. MODULE_DEVICE_TABLE(usb, id_table);
  68. static int aircable_prepare_write_buffer(struct usb_serial_port *port,
  69. void *dest, size_t size)
  70. {
  71. int count;
  72. unsigned char *buf = dest;
  73. count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH,
  74. size - HCI_HEADER_LENGTH, &port->lock);
  75. buf[0] = TX_HEADER_0;
  76. buf[1] = TX_HEADER_1;
  77. put_unaligned_le16(count, &buf[2]);
  78. return count + HCI_HEADER_LENGTH;
  79. }
  80. static int aircable_calc_num_ports(struct usb_serial *serial,
  81. struct usb_serial_endpoints *epds)
  82. {
  83. /* Ignore the first interface, which has no bulk endpoints. */
  84. if (epds->num_bulk_out == 0) {
  85. dev_dbg(&serial->interface->dev,
  86. "ignoring interface with no bulk-out endpoints\n");
  87. return -ENODEV;
  88. }
  89. return 1;
  90. }
  91. static int aircable_process_packet(struct usb_serial_port *port,
  92. int has_headers, char *packet, int len)
  93. {
  94. if (has_headers) {
  95. len -= HCI_HEADER_LENGTH;
  96. packet += HCI_HEADER_LENGTH;
  97. }
  98. if (len <= 0) {
  99. dev_dbg(&port->dev, "%s - malformed packet\n", __func__);
  100. return 0;
  101. }
  102. tty_insert_flip_string(&port->port, packet, len);
  103. return len;
  104. }
  105. static void aircable_process_read_urb(struct urb *urb)
  106. {
  107. struct usb_serial_port *port = urb->context;
  108. char *data = (char *)urb->transfer_buffer;
  109. int has_headers;
  110. int count;
  111. int len;
  112. int i;
  113. has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0);
  114. count = 0;
  115. for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) {
  116. len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME);
  117. count += aircable_process_packet(port, has_headers,
  118. &data[i], len);
  119. }
  120. if (count)
  121. tty_flip_buffer_push(&port->port);
  122. }
  123. static struct usb_serial_driver aircable_device = {
  124. .driver = {
  125. .owner = THIS_MODULE,
  126. .name = "aircable",
  127. },
  128. .id_table = id_table,
  129. .bulk_out_size = HCI_COMPLETE_FRAME,
  130. .calc_num_ports = aircable_calc_num_ports,
  131. .process_read_urb = aircable_process_read_urb,
  132. .prepare_write_buffer = aircable_prepare_write_buffer,
  133. .throttle = usb_serial_generic_throttle,
  134. .unthrottle = usb_serial_generic_unthrottle,
  135. };
  136. static struct usb_serial_driver * const serial_drivers[] = {
  137. &aircable_device, NULL
  138. };
  139. module_usb_serial_driver(serial_drivers, id_table);
  140. MODULE_AUTHOR(DRIVER_AUTHOR);
  141. MODULE_DESCRIPTION(DRIVER_DESC);
  142. MODULE_LICENSE("GPL");