blob: cac5162937b3dece0891f6209424d245e5b14e00 [file] [log] [blame]
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -03001/*
2 * AIRcable USB Bluetooth Dongle Driver.
3 *
Johan Hovold42725682010-05-05 23:45:24 +02004 * Copyright (C) 2010 Johan Hovold <jhovold@gmail.com>
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -03005 * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com)
Johan Hovold42725682010-05-05 23:45:24 +02006 *
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -03007 * 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. Daybeb7dd82007-05-09 07:14:03 +020014 * When writing the first urb must have a Header that starts with 0x20 0x29 the
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030015 * 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.
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +020023 * When writing to the device the first two bytes of the header are 0x20 0x29
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030024 * 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
Johan Hovold42725682010-05-05 23:45:24 +020047#include <asm/unaligned.h>
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030048#include <linux/tty.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030050#include <linux/tty_flip.h>
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030051#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
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030060/* 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
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030066#define HCI_COMPLETE_FRAME 64
67
68/* rx_flags */
69#define THROTTLED 0x01
70#define ACTUALLY_THROTTLED 0x02
71
72/*
73 * Version Information
74 */
Johan Hovold42725682010-05-05 23:45:24 +020075#define DRIVER_VERSION "v2.0"
76#define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>"
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030077#define DRIVER_DESC "AIRcable USB Driver"
78
79/* ID table that will be registered with USB core */
Németh Márton7d40d7e2010-01-10 15:34:24 +010080static const struct usb_device_id id_table[] = {
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030081 { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
82 { },
83};
84MODULE_DEVICE_TABLE(usb, id_table);
85
Johan Hovold42725682010-05-05 23:45:24 +020086static int aircable_prepare_write_buffer(struct usb_serial_port *port,
87 void **dest, size_t size, const void *src, size_t count)
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030088{
Johan Hovold42725682010-05-05 23:45:24 +020089 unsigned char *buf = *dest;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030090
Johan Hovold42725682010-05-05 23:45:24 +020091 count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH,
92 size - HCI_HEADER_LENGTH, &port->lock);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030093 buf[0] = TX_HEADER_0;
94 buf[1] = TX_HEADER_1;
Johan Hovold42725682010-05-05 23:45:24 +020095 put_unaligned_le16(count, &buf[2]);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030096
Johan Hovold42725682010-05-05 23:45:24 +020097 return count;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030098}
99
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300100static int aircable_probe(struct usb_serial *serial,
101 const struct usb_device_id *id)
102{
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100103 struct usb_host_interface *iface_desc = serial->interface->
104 cur_altsetting;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300105 struct usb_endpoint_descriptor *endpoint;
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100106 int num_bulk_out = 0;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300107 int i;
108
109 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
110 endpoint = &iface_desc->endpoint[i].desc;
Luiz Fernando N. Capitulino377f13b2006-10-26 13:02:45 -0300111 if (usb_endpoint_is_bulk_out(endpoint)) {
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300112 dbg("found bulk out on endpoint %d", i);
113 ++num_bulk_out;
114 }
115 }
116
117 if (num_bulk_out == 0) {
118 dbg("Invalid interface, discarding");
119 return -ENODEV;
120 }
121
122 return 0;
123}
124
Johan Hovold42725682010-05-05 23:45:24 +0200125static int aircable_process_packet(struct tty_struct *tty,
126 struct usb_serial_port *port, int has_headers,
127 char *packet, int len)
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300128{
Johan Hovold42725682010-05-05 23:45:24 +0200129 if (has_headers) {
130 len -= HCI_HEADER_LENGTH;
131 packet += HCI_HEADER_LENGTH;
132 }
133 if (len <= 0) {
134 dbg("%s - malformed packet", __func__);
135 return 0;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300136 }
137
Johan Hovold42725682010-05-05 23:45:24 +0200138 tty_insert_flip_string(tty, packet, len);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300139
Johan Hovold42725682010-05-05 23:45:24 +0200140 return len;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300141}
142
Johan Hovold42725682010-05-05 23:45:24 +0200143static void aircable_process_read_urb(struct urb *urb)
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300144{
145 struct usb_serial_port *port = urb->context;
Johan Hovold42725682010-05-05 23:45:24 +0200146 char *data = (char *)urb->transfer_buffer;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300147 struct tty_struct *tty;
Johan Hovold42725682010-05-05 23:45:24 +0200148 int has_headers;
149 int count;
150 int len;
151 int i;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300152
Alan Cox4a90f092008-10-13 10:39:46 +0100153 tty = tty_port_tty_get(&port->port);
Johan Hovold42725682010-05-05 23:45:24 +0200154 if (!tty)
155 return;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300156
Johan Hovold42725682010-05-05 23:45:24 +0200157 has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300158
Johan Hovold42725682010-05-05 23:45:24 +0200159 count = 0;
160 for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) {
161 len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME);
162 count += aircable_process_packet(tty, port, has_headers,
163 &data[i], len);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300164 }
Johan Hovold42725682010-05-05 23:45:24 +0200165
166 if (count)
167 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100168 tty_kref_put(tty);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300169}
170
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100171static struct usb_driver aircable_driver = {
172 .name = "aircable",
173 .probe = usb_serial_probe,
174 .disconnect = usb_serial_disconnect,
175 .id_table = id_table,
176 .no_dynamic_id = 1,
177};
178
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300179static struct usb_serial_driver aircable_device = {
Johannes Hölzl52d67f02006-12-17 22:05:09 +0100180 .driver = {
181 .owner = THIS_MODULE,
182 .name = "aircable",
183 },
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100184 .usb_driver = &aircable_driver,
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300185 .id_table = id_table,
186 .num_ports = 1,
Johan Hovold42725682010-05-05 23:45:24 +0200187 .bulk_out_size = HCI_COMPLETE_FRAME,
188 /* Must modify prepare_write_buffer if multi_urb_write is changed. */
189 .multi_urb_write = 0,
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300190 .probe = aircable_probe,
Johan Hovold42725682010-05-05 23:45:24 +0200191 .process_read_urb = aircable_process_read_urb,
192 .prepare_write_buffer = aircable_prepare_write_buffer,
193 .throttle = usb_serial_generic_throttle,
194 .unthrottle = usb_serial_generic_unthrottle,
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300195};
196
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100197static int __init aircable_init(void)
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300198{
199 int retval;
200 retval = usb_serial_register(&aircable_device);
201 if (retval)
202 goto failed_serial_register;
203 retval = usb_register(&aircable_driver);
204 if (retval)
205 goto failed_usb_register;
206 return 0;
207
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300208failed_usb_register:
Dave Young78c8fb32009-02-01 18:54:54 +0800209 usb_serial_deregister(&aircable_device);
210failed_serial_register:
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300211 return retval;
212}
213
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100214static void __exit aircable_exit(void)
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300215{
216 usb_deregister(&aircable_driver);
217 usb_serial_deregister(&aircable_device);
218}
219
220MODULE_AUTHOR(DRIVER_AUTHOR);
221MODULE_DESCRIPTION(DRIVER_DESC);
222MODULE_VERSION(DRIVER_VERSION);
223MODULE_LICENSE("GPL");
224
225module_init(aircable_init);
226module_exit(aircable_exit);
227
228module_param(debug, bool, S_IRUGO | S_IWUSR);
229MODULE_PARM_DESC(debug, "Debug enabled or not");