13fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo/* 23fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * AIRcable USB Bluetooth Dongle Driver. 33fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * 44272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold * Copyright (C) 2010 Johan Hovold <jhovold@gmail.com> 53fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com) 64272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold * 73fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * This program is free software; you can redistribute it and/or modify it under 83fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * the terms of the GNU General Public License version 2 as published by the 93fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * Free Software Foundation. 103fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * 113fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * The device works as an standard CDC device, it has 2 interfaces, the first 123fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * one is for firmware access and the second is the serial one. 13cd8c50532a42065339be1fe550e66b89d7ffd14fRahul Bedarkar * The protocol is very simply, there are two possibilities reading or writing. 14beb7dd86a101263bf63a78c7c6d4da3849b35bd6Robert P. J. Day * When writing the first urb must have a Header that starts with 0x20 0x29 the 15cd8c50532a42065339be1fe550e66b89d7ffd14fRahul Bedarkar * next two bytes must say how much data will be sent. 163fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * When reading the process is almost equal except that the header starts with 173fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * 0x00 0x20. 183fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * 1925985edcedea6396277003854657b5f3cb31a628Lucas De Marchi * The device simply need some stuff to understand data coming from the usb 203fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * buffer: The First and Second byte is used for a Header, the Third and Fourth 213fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * tells the device the amount of information the package holds. 223fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * Packages are 60 bytes long Header Stuff. 23beb7dd86a101263bf63a78c7c6d4da3849b35bd6Robert P. J. Day * When writing to the device the first two bytes of the header are 0x20 0x29 243fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * When reading the bytes are 0x00 0x20, or 0x00 0x10, there is an strange 253fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * situation, when too much data arrives to the device because it sends the data 263fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * but with out the header. I will use a simply hack to override this situation, 273fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * if there is data coming that does not contain any header, then that is data 283fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * that must go directly to the tty, as there is no documentation about if there 293fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * is any other control code, I will simply check for the first 303fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * one. 313fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * 323fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * The driver registers himself with the USB-serial core and the USB Core. I had 3325985edcedea6396277003854657b5f3cb31a628Lucas De Marchi * to implement a probe function against USB-serial, because other way, the 34cd8c50532a42065339be1fe550e66b89d7ffd14fRahul Bedarkar * driver was attaching himself to both interfaces. I have tried with different 353fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * configurations of usb_serial_driver with out exit, only the probe function 363fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * could handle this correctly. 373fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * 383fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * I have taken some info from a Greg Kroah-Hartman article: 393fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * http://www.linuxjournal.com/article/6573 403fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * And from Linux Device Driver Kit CD, which is a great work, the authors taken 41cd8c50532a42065339be1fe550e66b89d7ffd14fRahul Bedarkar * the work to recompile lots of information an knowledge in drivers development 42cd8c50532a42065339be1fe550e66b89d7ffd14fRahul Bedarkar * and made it all available inside a cd. 433fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * URL: http://kernel.org/pub/linux/kernel/people/gregkh/ddk/ 443fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo * 453fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo */ 463fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 474272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold#include <asm/unaligned.h> 483fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo#include <linux/tty.h> 495a0e3ad6af8660be21ca98a971cd00f331318c05Tejun Heo#include <linux/slab.h> 506eb0de827084060e6607c8f8542d9e9566214538Paul Gortmaker#include <linux/module.h> 513fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo#include <linux/tty_flip.h> 523fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo#include <linux/usb.h> 533fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo#include <linux/usb/serial.h> 543fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 553fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo/* Vendor and Product ID */ 563fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo#define AIRCABLE_VID 0x16CA 573fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo#define AIRCABLE_USB_PID 0x1502 583fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 593fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo/* Protocol Stuff */ 603fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo#define HCI_HEADER_LENGTH 0x4 613fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo#define TX_HEADER_0 0x20 623fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo#define TX_HEADER_1 0x29 633fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo#define RX_HEADER_0 0x00 643fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo#define RX_HEADER_1 0x20 653fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo#define HCI_COMPLETE_FRAME 64 663fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 673fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo/* rx_flags */ 683fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo#define THROTTLED 0x01 693fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo#define ACTUALLY_THROTTLED 0x02 703fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 714272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold#define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>" 723fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo#define DRIVER_DESC "AIRcable USB Driver" 733fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 743fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo/* ID table that will be registered with USB core */ 757d40d7e85a25e01948bcb4dc3eda1355af318337Németh Mártonstatic const struct usb_device_id id_table[] = { 763fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) }, 773fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo { }, 783fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo}; 793fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco NaranjoMODULE_DEVICE_TABLE(usb, id_table); 803fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 814272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovoldstatic int aircable_prepare_write_buffer(struct usb_serial_port *port, 82c23e5fc1f7dba228558b4a46e68f7af89515b13cJohan Hovold void *dest, size_t size) 833fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo{ 84c23e5fc1f7dba228558b4a46e68f7af89515b13cJohan Hovold int count; 85c23e5fc1f7dba228558b4a46e68f7af89515b13cJohan Hovold unsigned char *buf = dest; 863fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 874272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH, 884272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold size - HCI_HEADER_LENGTH, &port->lock); 893fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo buf[0] = TX_HEADER_0; 903fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo buf[1] = TX_HEADER_1; 914272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold put_unaligned_le16(count, &buf[2]); 923fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 93f26c2889a4d937371e2ec9b5690ac407f0496634Johan Hovold return count + HCI_HEADER_LENGTH; 943fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo} 953fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 963fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjostatic int aircable_probe(struct usb_serial *serial, 973fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo const struct usb_device_id *id) 983fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo{ 99c4d0f8cbca3a97900f85b082064a63c7a5928bd7Alan Cox struct usb_host_interface *iface_desc = serial->interface-> 100c4d0f8cbca3a97900f85b082064a63c7a5928bd7Alan Cox cur_altsetting; 1013fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo struct usb_endpoint_descriptor *endpoint; 102c4d0f8cbca3a97900f85b082064a63c7a5928bd7Alan Cox int num_bulk_out = 0; 1033fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo int i; 1043fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 1053fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) { 1063fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo endpoint = &iface_desc->endpoint[i].desc; 107377f13bf95b64cf5fb0fad0bf2b94106ad868562Luiz Fernando N. Capitulino if (usb_endpoint_is_bulk_out(endpoint)) { 10866afb5b535fe34cd6b33d2ef45aac24fac9370f8Greg Kroah-Hartman dev_dbg(&serial->dev->dev, 10966afb5b535fe34cd6b33d2ef45aac24fac9370f8Greg Kroah-Hartman "found bulk out on endpoint %d\n", i); 1103fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo ++num_bulk_out; 1113fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo } 1123fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo } 1133fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 1143fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo if (num_bulk_out == 0) { 11566afb5b535fe34cd6b33d2ef45aac24fac9370f8Greg Kroah-Hartman dev_dbg(&serial->dev->dev, "Invalid interface, discarding\n"); 1163fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo return -ENODEV; 1173fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo } 1183fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 1193fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo return 0; 1203fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo} 1213fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 12205c7cd39907184328f48d3e7899f9cdd653ad336Jiri Slabystatic int aircable_process_packet(struct usb_serial_port *port, 12305c7cd39907184328f48d3e7899f9cdd653ad336Jiri Slaby int has_headers, char *packet, int len) 1243fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo{ 1254272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold if (has_headers) { 1264272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold len -= HCI_HEADER_LENGTH; 1274272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold packet += HCI_HEADER_LENGTH; 1283fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo } 1294272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold if (len <= 0) { 13066afb5b535fe34cd6b33d2ef45aac24fac9370f8Greg Kroah-Hartman dev_dbg(&port->dev, "%s - malformed packet\n", __func__); 1314272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold return 0; 1323fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo } 1333fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 13405c7cd39907184328f48d3e7899f9cdd653ad336Jiri Slaby tty_insert_flip_string(&port->port, packet, len); 1353fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 1364272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold return len; 1373fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo} 1383fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 1394272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovoldstatic void aircable_process_read_urb(struct urb *urb) 1403fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo{ 1413fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo struct usb_serial_port *port = urb->context; 1424272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold char *data = (char *)urb->transfer_buffer; 1434272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold int has_headers; 1444272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold int count; 1454272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold int len; 1464272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold int i; 1473fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 1484272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0); 1493fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 1504272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold count = 0; 1514272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) { 1524272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME); 15305c7cd39907184328f48d3e7899f9cdd653ad336Jiri Slaby count += aircable_process_packet(port, has_headers, 1544272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold &data[i], len); 1553fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo } 1563fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 1574272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold if (count) 1582e124b4a390ca85325fae75764bef92f0547fa25Jiri Slaby tty_flip_buffer_push(&port->port); 1593fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo} 1603fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 1613fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjostatic struct usb_serial_driver aircable_device = { 16252d67f0b5c1b1827cd842020d40bdde4f7d04f59Johannes Hölzl .driver = { 16352d67f0b5c1b1827cd842020d40bdde4f7d04f59Johannes Hölzl .owner = THIS_MODULE, 16452d67f0b5c1b1827cd842020d40bdde4f7d04f59Johannes Hölzl .name = "aircable", 16552d67f0b5c1b1827cd842020d40bdde4f7d04f59Johannes Hölzl }, 1663fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo .id_table = id_table, 1673fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo .num_ports = 1, 1684272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold .bulk_out_size = HCI_COMPLETE_FRAME, 1693fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo .probe = aircable_probe, 1704272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold .process_read_urb = aircable_process_read_urb, 1714272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold .prepare_write_buffer = aircable_prepare_write_buffer, 1724272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold .throttle = usb_serial_generic_throttle, 1734272568b3dd8dbad36014a107c0fbbef6400c917Johan Hovold .unthrottle = usb_serial_generic_unthrottle, 1743fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo}; 1753fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 17608a4f6bc2e7046ce50849d7589b7d0763926d808Alan Sternstatic struct usb_serial_driver * const serial_drivers[] = { 17708a4f6bc2e7046ce50849d7589b7d0763926d808Alan Stern &aircable_device, NULL 17808a4f6bc2e7046ce50849d7589b7d0763926d808Alan Stern}; 17908a4f6bc2e7046ce50849d7589b7d0763926d808Alan Stern 18068e24113457e437b1576670f2419b77ed0531e9eGreg Kroah-Hartmanmodule_usb_serial_driver(serial_drivers, id_table); 1813fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco Naranjo 1823fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco NaranjoMODULE_AUTHOR(DRIVER_AUTHOR); 1833fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco NaranjoMODULE_DESCRIPTION(DRIVER_DESC); 1843fe70ba2272c123cf38e4c577bf220f8bcf25366Manuel Francisco NaranjoMODULE_LICENSE("GPL"); 185