blob: d634e6635632f8601fa943982a24f8c995ab320b [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 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030019 * The device simply need some stuff to understand data coming from the usb
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030020 * 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
Lucas De Marchi25985ed2011-03-30 22:57:33 -030033 * to implement a probe function against USB-serial, because other way, the
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030034 * 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>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040050#include <linux/module.h>
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030051#include <linux/tty_flip.h>
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030052#include <linux/usb.h>
53#include <linux/usb/serial.h>
54
Rusty Russell90ab5ee2012-01-13 09:32:20 +103055static bool debug;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030056
57/* Vendor and Product ID */
58#define AIRCABLE_VID 0x16CA
59#define AIRCABLE_USB_PID 0x1502
60
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030061/* 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
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030067#define HCI_COMPLETE_FRAME 64
68
69/* rx_flags */
70#define THROTTLED 0x01
71#define ACTUALLY_THROTTLED 0x02
72
73/*
74 * Version Information
75 */
Johan Hovold42725682010-05-05 23:45:24 +020076#define DRIVER_VERSION "v2.0"
77#define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>"
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030078#define DRIVER_DESC "AIRcable USB Driver"
79
80/* ID table that will be registered with USB core */
Németh Márton7d40d7e2010-01-10 15:34:24 +010081static const struct usb_device_id id_table[] = {
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030082 { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
83 { },
84};
85MODULE_DEVICE_TABLE(usb, id_table);
86
Johan Hovold42725682010-05-05 23:45:24 +020087static int aircable_prepare_write_buffer(struct usb_serial_port *port,
Johan Hovoldc23e5fc2010-05-05 23:58:13 +020088 void *dest, size_t size)
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030089{
Johan Hovoldc23e5fc2010-05-05 23:58:13 +020090 int count;
91 unsigned char *buf = dest;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030092
Johan Hovold42725682010-05-05 23:45:24 +020093 count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH,
94 size - HCI_HEADER_LENGTH, &port->lock);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030095 buf[0] = TX_HEADER_0;
96 buf[1] = TX_HEADER_1;
Johan Hovold42725682010-05-05 23:45:24 +020097 put_unaligned_le16(count, &buf[2]);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030098
Johan Hovoldf26c2882010-05-19 00:01:33 +020099 return count + HCI_HEADER_LENGTH;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300100}
101
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300102static int aircable_probe(struct usb_serial *serial,
103 const struct usb_device_id *id)
104{
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100105 struct usb_host_interface *iface_desc = serial->interface->
106 cur_altsetting;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300107 struct usb_endpoint_descriptor *endpoint;
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100108 int num_bulk_out = 0;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300109 int i;
110
111 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
112 endpoint = &iface_desc->endpoint[i].desc;
Luiz Fernando N. Capitulino377f13b2006-10-26 13:02:45 -0300113 if (usb_endpoint_is_bulk_out(endpoint)) {
Greg Kroah-Hartman66afb5b2012-05-15 16:27:08 -0700114 dev_dbg(&serial->dev->dev,
115 "found bulk out on endpoint %d\n", i);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300116 ++num_bulk_out;
117 }
118 }
119
120 if (num_bulk_out == 0) {
Greg Kroah-Hartman66afb5b2012-05-15 16:27:08 -0700121 dev_dbg(&serial->dev->dev, "Invalid interface, discarding\n");
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300122 return -ENODEV;
123 }
124
125 return 0;
126}
127
Johan Hovold42725682010-05-05 23:45:24 +0200128static int aircable_process_packet(struct tty_struct *tty,
129 struct usb_serial_port *port, int has_headers,
130 char *packet, int len)
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300131{
Johan Hovold42725682010-05-05 23:45:24 +0200132 if (has_headers) {
133 len -= HCI_HEADER_LENGTH;
134 packet += HCI_HEADER_LENGTH;
135 }
136 if (len <= 0) {
Greg Kroah-Hartman66afb5b2012-05-15 16:27:08 -0700137 dev_dbg(&port->dev, "%s - malformed packet\n", __func__);
Johan Hovold42725682010-05-05 23:45:24 +0200138 return 0;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300139 }
140
Johan Hovold42725682010-05-05 23:45:24 +0200141 tty_insert_flip_string(tty, packet, len);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300142
Johan Hovold42725682010-05-05 23:45:24 +0200143 return len;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300144}
145
Johan Hovold42725682010-05-05 23:45:24 +0200146static void aircable_process_read_urb(struct urb *urb)
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300147{
148 struct usb_serial_port *port = urb->context;
Johan Hovold42725682010-05-05 23:45:24 +0200149 char *data = (char *)urb->transfer_buffer;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300150 struct tty_struct *tty;
Johan Hovold42725682010-05-05 23:45:24 +0200151 int has_headers;
152 int count;
153 int len;
154 int i;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300155
Alan Cox4a90f092008-10-13 10:39:46 +0100156 tty = tty_port_tty_get(&port->port);
Johan Hovold42725682010-05-05 23:45:24 +0200157 if (!tty)
158 return;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300159
Johan Hovold42725682010-05-05 23:45:24 +0200160 has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300161
Johan Hovold42725682010-05-05 23:45:24 +0200162 count = 0;
163 for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) {
164 len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME);
165 count += aircable_process_packet(tty, port, has_headers,
166 &data[i], len);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300167 }
Johan Hovold42725682010-05-05 23:45:24 +0200168
169 if (count)
170 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100171 tty_kref_put(tty);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300172}
173
174static struct usb_serial_driver aircable_device = {
Johannes Hölzl52d67f02006-12-17 22:05:09 +0100175 .driver = {
176 .owner = THIS_MODULE,
177 .name = "aircable",
178 },
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300179 .id_table = id_table,
180 .num_ports = 1,
Johan Hovold42725682010-05-05 23:45:24 +0200181 .bulk_out_size = HCI_COMPLETE_FRAME,
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300182 .probe = aircable_probe,
Johan Hovold42725682010-05-05 23:45:24 +0200183 .process_read_urb = aircable_process_read_urb,
184 .prepare_write_buffer = aircable_prepare_write_buffer,
185 .throttle = usb_serial_generic_throttle,
186 .unthrottle = usb_serial_generic_unthrottle,
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300187};
188
Alan Stern08a4f6b2012-02-23 14:56:17 -0500189static struct usb_serial_driver * const serial_drivers[] = {
190 &aircable_device, NULL
191};
192
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700193module_usb_serial_driver(serial_drivers, id_table);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300194
195MODULE_AUTHOR(DRIVER_AUTHOR);
196MODULE_DESCRIPTION(DRIVER_DESC);
197MODULE_VERSION(DRIVER_VERSION);
198MODULE_LICENSE("GPL");
199
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300200module_param(debug, bool, S_IRUGO | S_IWUSR);
201MODULE_PARM_DESC(debug, "Debug enabled or not");