Commit | Line | Data |
---|---|---|
3fe70ba2 MFN |
1 | /* |
2 | * AIRcable USB Bluetooth Dongle Driver. | |
3 | * | |
4272568b | 4 | * Copyright (C) 2010 Johan Hovold <jhovold@gmail.com> |
3fe70ba2 | 5 | * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com) |
4272568b | 6 | * |
3fe70ba2 MFN |
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. | |
beb7dd86 | 14 | * When writing the first urb must have a Header that starts with 0x20 0x29 the |
3fe70ba2 MFN |
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 | * | |
25985edc | 19 | * The device simply need some stuff to understand data coming from the usb |
3fe70ba2 MFN |
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. | |
beb7dd86 | 23 | * When writing to the device the first two bytes of the header are 0x20 0x29 |
3fe70ba2 MFN |
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 | |
25985edc | 33 | * to implement a probe function against USB-serial, because other way, the |
3fe70ba2 MFN |
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 | ||
4272568b | 47 | #include <asm/unaligned.h> |
3fe70ba2 | 48 | #include <linux/tty.h> |
5a0e3ad6 | 49 | #include <linux/slab.h> |
6eb0de82 | 50 | #include <linux/module.h> |
3fe70ba2 | 51 | #include <linux/tty_flip.h> |
3fe70ba2 MFN |
52 | #include <linux/usb.h> |
53 | #include <linux/usb/serial.h> | |
54 | ||
90ab5ee9 | 55 | static bool debug; |
3fe70ba2 MFN |
56 | |
57 | /* Vendor and Product ID */ | |
58 | #define AIRCABLE_VID 0x16CA | |
59 | #define AIRCABLE_USB_PID 0x1502 | |
60 | ||
3fe70ba2 MFN |
61 | /* Protocol Stuff */ |
62 | #define HCI_HEADER_LENGTH 0x4 | |
63 | #define TX_HEADER_0 0x20 | |
64 | #define TX_HEADER_1 0x29 | |
65 | #define RX_HEADER_0 0x00 | |
66 | #define RX_HEADER_1 0x20 | |
3fe70ba2 MFN |
67 | #define HCI_COMPLETE_FRAME 64 |
68 | ||
69 | /* rx_flags */ | |
70 | #define THROTTLED 0x01 | |
71 | #define ACTUALLY_THROTTLED 0x02 | |
72 | ||
73 | /* | |
74 | * Version Information | |
75 | */ | |
4272568b JH |
76 | #define DRIVER_VERSION "v2.0" |
77 | #define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>" | |
3fe70ba2 MFN |
78 | #define DRIVER_DESC "AIRcable USB Driver" |
79 | ||
80 | /* ID table that will be registered with USB core */ | |
7d40d7e8 | 81 | static const struct usb_device_id id_table[] = { |
3fe70ba2 MFN |
82 | { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) }, |
83 | { }, | |
84 | }; | |
85 | MODULE_DEVICE_TABLE(usb, id_table); | |
86 | ||
4272568b | 87 | static int aircable_prepare_write_buffer(struct usb_serial_port *port, |
c23e5fc1 | 88 | void *dest, size_t size) |
3fe70ba2 | 89 | { |
c23e5fc1 JH |
90 | int count; |
91 | unsigned char *buf = dest; | |
3fe70ba2 | 92 | |
4272568b JH |
93 | count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH, |
94 | size - HCI_HEADER_LENGTH, &port->lock); | |
3fe70ba2 MFN |
95 | buf[0] = TX_HEADER_0; |
96 | buf[1] = TX_HEADER_1; | |
4272568b | 97 | put_unaligned_le16(count, &buf[2]); |
3fe70ba2 | 98 | |
f26c2889 | 99 | return count + HCI_HEADER_LENGTH; |
3fe70ba2 | 100 | } |
3fe70ba2 MFN |
101 | |
102 | static int aircable_probe(struct usb_serial *serial, | |
103 | const struct usb_device_id *id) | |
104 | { | |
c4d0f8cb AC |
105 | struct usb_host_interface *iface_desc = serial->interface-> |
106 | cur_altsetting; | |
3fe70ba2 | 107 | struct usb_endpoint_descriptor *endpoint; |
c4d0f8cb | 108 | int num_bulk_out = 0; |
3fe70ba2 MFN |
109 | int i; |
110 | ||
111 | for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) { | |
112 | endpoint = &iface_desc->endpoint[i].desc; | |
377f13bf | 113 | if (usb_endpoint_is_bulk_out(endpoint)) { |
3fe70ba2 MFN |
114 | dbg("found bulk out on endpoint %d", i); |
115 | ++num_bulk_out; | |
116 | } | |
117 | } | |
118 | ||
119 | if (num_bulk_out == 0) { | |
120 | dbg("Invalid interface, discarding"); | |
121 | return -ENODEV; | |
122 | } | |
123 | ||
124 | return 0; | |
125 | } | |
126 | ||
4272568b JH |
127 | static int aircable_process_packet(struct tty_struct *tty, |
128 | struct usb_serial_port *port, int has_headers, | |
129 | char *packet, int len) | |
3fe70ba2 | 130 | { |
4272568b JH |
131 | if (has_headers) { |
132 | len -= HCI_HEADER_LENGTH; | |
133 | packet += HCI_HEADER_LENGTH; | |
3fe70ba2 | 134 | } |
4272568b JH |
135 | if (len <= 0) { |
136 | dbg("%s - malformed packet", __func__); | |
137 | return 0; | |
3fe70ba2 MFN |
138 | } |
139 | ||
4272568b | 140 | tty_insert_flip_string(tty, packet, len); |
3fe70ba2 | 141 | |
4272568b | 142 | return len; |
3fe70ba2 MFN |
143 | } |
144 | ||
4272568b | 145 | static void aircable_process_read_urb(struct urb *urb) |
3fe70ba2 MFN |
146 | { |
147 | struct usb_serial_port *port = urb->context; | |
4272568b | 148 | char *data = (char *)urb->transfer_buffer; |
3fe70ba2 | 149 | struct tty_struct *tty; |
4272568b JH |
150 | int has_headers; |
151 | int count; | |
152 | int len; | |
153 | int i; | |
3fe70ba2 | 154 | |
4a90f09b | 155 | tty = tty_port_tty_get(&port->port); |
4272568b JH |
156 | if (!tty) |
157 | return; | |
3fe70ba2 | 158 | |
4272568b | 159 | has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0); |
3fe70ba2 | 160 | |
4272568b JH |
161 | count = 0; |
162 | for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) { | |
163 | len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME); | |
164 | count += aircable_process_packet(tty, port, has_headers, | |
165 | &data[i], len); | |
3fe70ba2 | 166 | } |
3fe70ba2 | 167 | |
4272568b JH |
168 | if (count) |
169 | tty_flip_buffer_push(tty); | |
170 | tty_kref_put(tty); | |
3fe70ba2 MFN |
171 | } |
172 | ||
173 | static struct usb_serial_driver aircable_device = { | |
52d67f0b JH |
174 | .driver = { |
175 | .owner = THIS_MODULE, | |
176 | .name = "aircable", | |
177 | }, | |
3fe70ba2 MFN |
178 | .id_table = id_table, |
179 | .num_ports = 1, | |
4272568b | 180 | .bulk_out_size = HCI_COMPLETE_FRAME, |
3fe70ba2 | 181 | .probe = aircable_probe, |
4272568b JH |
182 | .process_read_urb = aircable_process_read_urb, |
183 | .prepare_write_buffer = aircable_prepare_write_buffer, | |
184 | .throttle = usb_serial_generic_throttle, | |
185 | .unthrottle = usb_serial_generic_unthrottle, | |
3fe70ba2 MFN |
186 | }; |
187 | ||
08a4f6bc AS |
188 | static struct usb_serial_driver * const serial_drivers[] = { |
189 | &aircable_device, NULL | |
190 | }; | |
191 | ||
68e24113 | 192 | module_usb_serial_driver(serial_drivers, id_table); |
3fe70ba2 MFN |
193 | |
194 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
195 | MODULE_DESCRIPTION(DRIVER_DESC); | |
196 | MODULE_VERSION(DRIVER_VERSION); | |
197 | MODULE_LICENSE("GPL"); | |
198 | ||
3fe70ba2 MFN |
199 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
200 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |