Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 1 | /* |
| 2 | * AIRcable USB Bluetooth Dongle Driver. |
| 3 | * |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 4 | * Copyright (C) 2010 Johan Hovold <jhovold@gmail.com> |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 5 | * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com) |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 6 | * |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 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. |
Robert P. J. Day | beb7dd8 | 2007-05-09 07:14:03 +0200 | [diff] [blame] | 14 | * When writing the first urb must have a Header that starts with 0x20 0x29 the |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 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 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 19 | * The device simply need some stuff to understand data coming from the usb |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 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. |
Robert P. J. Day | beb7dd8 | 2007-05-09 07:14:03 +0200 | [diff] [blame] | 23 | * When writing to the device the first two bytes of the header are 0x20 0x29 |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 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 |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 33 | * to implement a probe function against USB-serial, because other way, the |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 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 | |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 47 | #include <asm/unaligned.h> |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 48 | #include <linux/tty.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 49 | #include <linux/slab.h> |
Paul Gortmaker | 6eb0de8 | 2011-07-03 16:09:31 -0400 | [diff] [blame] | 50 | #include <linux/module.h> |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 51 | #include <linux/tty_flip.h> |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 52 | #include <linux/usb.h> |
| 53 | #include <linux/usb/serial.h> |
| 54 | |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 55 | /* Vendor and Product ID */ |
| 56 | #define AIRCABLE_VID 0x16CA |
| 57 | #define AIRCABLE_USB_PID 0x1502 |
| 58 | |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 59 | /* Protocol Stuff */ |
| 60 | #define HCI_HEADER_LENGTH 0x4 |
| 61 | #define TX_HEADER_0 0x20 |
| 62 | #define TX_HEADER_1 0x29 |
| 63 | #define RX_HEADER_0 0x00 |
| 64 | #define RX_HEADER_1 0x20 |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 65 | #define HCI_COMPLETE_FRAME 64 |
| 66 | |
| 67 | /* rx_flags */ |
| 68 | #define THROTTLED 0x01 |
| 69 | #define ACTUALLY_THROTTLED 0x02 |
| 70 | |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 71 | #define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>" |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 72 | #define DRIVER_DESC "AIRcable USB Driver" |
| 73 | |
| 74 | /* ID table that will be registered with USB core */ |
Németh Márton | 7d40d7e | 2010-01-10 15:34:24 +0100 | [diff] [blame] | 75 | static const struct usb_device_id id_table[] = { |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 76 | { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) }, |
| 77 | { }, |
| 78 | }; |
| 79 | MODULE_DEVICE_TABLE(usb, id_table); |
| 80 | |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 81 | static int aircable_prepare_write_buffer(struct usb_serial_port *port, |
Johan Hovold | c23e5fc | 2010-05-05 23:58:13 +0200 | [diff] [blame] | 82 | void *dest, size_t size) |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 83 | { |
Johan Hovold | c23e5fc | 2010-05-05 23:58:13 +0200 | [diff] [blame] | 84 | int count; |
| 85 | unsigned char *buf = dest; |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 86 | |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 87 | count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH, |
| 88 | size - HCI_HEADER_LENGTH, &port->lock); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 89 | buf[0] = TX_HEADER_0; |
| 90 | buf[1] = TX_HEADER_1; |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 91 | put_unaligned_le16(count, &buf[2]); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 92 | |
Johan Hovold | f26c288 | 2010-05-19 00:01:33 +0200 | [diff] [blame] | 93 | return count + HCI_HEADER_LENGTH; |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 94 | } |
| 95 | |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 96 | static int aircable_probe(struct usb_serial *serial, |
| 97 | const struct usb_device_id *id) |
| 98 | { |
Alan Cox | c4d0f8c | 2008-04-29 14:35:39 +0100 | [diff] [blame] | 99 | struct usb_host_interface *iface_desc = serial->interface-> |
| 100 | cur_altsetting; |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 101 | struct usb_endpoint_descriptor *endpoint; |
Alan Cox | c4d0f8c | 2008-04-29 14:35:39 +0100 | [diff] [blame] | 102 | int num_bulk_out = 0; |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 103 | int i; |
| 104 | |
| 105 | for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) { |
| 106 | endpoint = &iface_desc->endpoint[i].desc; |
Luiz Fernando N. Capitulino | 377f13b | 2006-10-26 13:02:45 -0300 | [diff] [blame] | 107 | if (usb_endpoint_is_bulk_out(endpoint)) { |
Greg Kroah-Hartman | 66afb5b | 2012-05-15 16:27:08 -0700 | [diff] [blame] | 108 | dev_dbg(&serial->dev->dev, |
| 109 | "found bulk out on endpoint %d\n", i); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 110 | ++num_bulk_out; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (num_bulk_out == 0) { |
Greg Kroah-Hartman | 66afb5b | 2012-05-15 16:27:08 -0700 | [diff] [blame] | 115 | dev_dbg(&serial->dev->dev, "Invalid interface, discarding\n"); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 116 | return -ENODEV; |
| 117 | } |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
Jiri Slaby | 05c7cd3 | 2013-01-03 15:53:04 +0100 | [diff] [blame] | 122 | static int aircable_process_packet(struct usb_serial_port *port, |
| 123 | int has_headers, char *packet, int len) |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 124 | { |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 125 | if (has_headers) { |
| 126 | len -= HCI_HEADER_LENGTH; |
| 127 | packet += HCI_HEADER_LENGTH; |
| 128 | } |
| 129 | if (len <= 0) { |
Greg Kroah-Hartman | 66afb5b | 2012-05-15 16:27:08 -0700 | [diff] [blame] | 130 | dev_dbg(&port->dev, "%s - malformed packet\n", __func__); |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 131 | return 0; |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 132 | } |
| 133 | |
Jiri Slaby | 05c7cd3 | 2013-01-03 15:53:04 +0100 | [diff] [blame] | 134 | tty_insert_flip_string(&port->port, packet, len); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 135 | |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 136 | return len; |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 137 | } |
| 138 | |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 139 | static void aircable_process_read_urb(struct urb *urb) |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 140 | { |
| 141 | struct usb_serial_port *port = urb->context; |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 142 | char *data = (char *)urb->transfer_buffer; |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 143 | struct tty_struct *tty; |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 144 | int has_headers; |
| 145 | int count; |
| 146 | int len; |
| 147 | int i; |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 148 | |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 149 | tty = tty_port_tty_get(&port->port); |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 150 | if (!tty) |
| 151 | return; |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 152 | |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 153 | has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 154 | |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 155 | count = 0; |
| 156 | for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) { |
| 157 | len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME); |
Jiri Slaby | 05c7cd3 | 2013-01-03 15:53:04 +0100 | [diff] [blame] | 158 | count += aircable_process_packet(port, has_headers, |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 159 | &data[i], len); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 160 | } |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 161 | |
| 162 | if (count) |
| 163 | tty_flip_buffer_push(tty); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 164 | tty_kref_put(tty); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static struct usb_serial_driver aircable_device = { |
Johannes Hölzl | 52d67f0 | 2006-12-17 22:05:09 +0100 | [diff] [blame] | 168 | .driver = { |
| 169 | .owner = THIS_MODULE, |
| 170 | .name = "aircable", |
| 171 | }, |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 172 | .id_table = id_table, |
| 173 | .num_ports = 1, |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 174 | .bulk_out_size = HCI_COMPLETE_FRAME, |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 175 | .probe = aircable_probe, |
Johan Hovold | 4272568 | 2010-05-05 23:45:24 +0200 | [diff] [blame] | 176 | .process_read_urb = aircable_process_read_urb, |
| 177 | .prepare_write_buffer = aircable_prepare_write_buffer, |
| 178 | .throttle = usb_serial_generic_throttle, |
| 179 | .unthrottle = usb_serial_generic_unthrottle, |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 180 | }; |
| 181 | |
Alan Stern | 08a4f6b | 2012-02-23 14:56:17 -0500 | [diff] [blame] | 182 | static struct usb_serial_driver * const serial_drivers[] = { |
| 183 | &aircable_device, NULL |
| 184 | }; |
| 185 | |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 186 | module_usb_serial_driver(serial_drivers, id_table); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 187 | |
| 188 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 189 | MODULE_DESCRIPTION(DRIVER_DESC); |
Manuel Francisco Naranjo | 3fe70ba | 2006-08-09 16:35:12 -0300 | [diff] [blame] | 190 | MODULE_LICENSE("GPL"); |