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