blob: a1df686c306648ec68471ead3d6dca0ee8a62771 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -03002/*
3 * AIRcable USB Bluetooth Dongle Driver.
4 *
Johan Hovold42725682010-05-05 23:45:24 +02005 * Copyright (C) 2010 Johan Hovold <jhovold@gmail.com>
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -03006 * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com)
Johan Hovold42725682010-05-05 23:45:24 +02007 *
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -03008 * The device works as an standard CDC device, it has 2 interfaces, the first
9 * one is for firmware access and the second is the serial one.
Rahul Bedarkarcd8c5052014-01-02 19:29:24 +053010 * The protocol is very simply, there are two possibilities reading or writing.
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +020011 * When writing the first urb must have a Header that starts with 0x20 0x29 the
Rahul Bedarkarcd8c5052014-01-02 19:29:24 +053012 * next two bytes must say how much data will be sent.
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030013 * When reading the process is almost equal except that the header starts with
14 * 0x00 0x20.
15 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016 * The device simply need some stuff to understand data coming from the usb
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030017 * buffer: The First and Second byte is used for a Header, the Third and Fourth
18 * tells the device the amount of information the package holds.
19 * Packages are 60 bytes long Header Stuff.
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +020020 * When writing to the device the first two bytes of the header are 0x20 0x29
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030021 * When reading the bytes are 0x00 0x20, or 0x00 0x10, there is an strange
22 * situation, when too much data arrives to the device because it sends the data
23 * but with out the header. I will use a simply hack to override this situation,
24 * if there is data coming that does not contain any header, then that is data
25 * that must go directly to the tty, as there is no documentation about if there
26 * is any other control code, I will simply check for the first
27 * one.
28 *
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030029 * I have taken some info from a Greg Kroah-Hartman article:
30 * http://www.linuxjournal.com/article/6573
31 * And from Linux Device Driver Kit CD, which is a great work, the authors taken
Rahul Bedarkarcd8c5052014-01-02 19:29:24 +053032 * the work to recompile lots of information an knowledge in drivers development
33 * and made it all available inside a cd.
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030034 * URL: http://kernel.org/pub/linux/kernel/people/gregkh/ddk/
35 *
36 */
37
Johan Hovold42725682010-05-05 23:45:24 +020038#include <asm/unaligned.h>
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030039#include <linux/tty.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040041#include <linux/module.h>
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030042#include <linux/tty_flip.h>
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030043#include <linux/usb.h>
44#include <linux/usb/serial.h>
45
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030046/* Vendor and Product ID */
47#define AIRCABLE_VID 0x16CA
48#define AIRCABLE_USB_PID 0x1502
49
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030050/* Protocol Stuff */
51#define HCI_HEADER_LENGTH 0x4
52#define TX_HEADER_0 0x20
53#define TX_HEADER_1 0x29
54#define RX_HEADER_0 0x00
55#define RX_HEADER_1 0x20
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030056#define HCI_COMPLETE_FRAME 64
57
58/* rx_flags */
59#define THROTTLED 0x01
60#define ACTUALLY_THROTTLED 0x02
61
Johan Hovold42725682010-05-05 23:45:24 +020062#define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>"
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030063#define DRIVER_DESC "AIRcable USB Driver"
64
65/* ID table that will be registered with USB core */
Németh Márton7d40d7e2010-01-10 15:34:24 +010066static const struct usb_device_id id_table[] = {
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030067 { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
68 { },
69};
70MODULE_DEVICE_TABLE(usb, id_table);
71
Johan Hovold42725682010-05-05 23:45:24 +020072static int aircable_prepare_write_buffer(struct usb_serial_port *port,
Johan Hovoldc23e5fc2010-05-05 23:58:13 +020073 void *dest, size_t size)
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030074{
Johan Hovoldc23e5fc2010-05-05 23:58:13 +020075 int count;
76 unsigned char *buf = dest;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030077
Johan Hovold42725682010-05-05 23:45:24 +020078 count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH,
79 size - HCI_HEADER_LENGTH, &port->lock);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030080 buf[0] = TX_HEADER_0;
81 buf[1] = TX_HEADER_1;
Johan Hovold42725682010-05-05 23:45:24 +020082 put_unaligned_le16(count, &buf[2]);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030083
Johan Hovoldf26c2882010-05-19 00:01:33 +020084 return count + HCI_HEADER_LENGTH;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030085}
86
Johan Hovold5f391972017-03-16 17:13:36 +010087static int aircable_calc_num_ports(struct usb_serial *serial,
88 struct usb_serial_endpoints *epds)
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030089{
Johan Hovold5f391972017-03-16 17:13:36 +010090 /* Ignore the first interface, which has no bulk endpoints. */
91 if (epds->num_bulk_out == 0) {
92 dev_dbg(&serial->interface->dev,
93 "ignoring interface with no bulk-out endpoints\n");
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030094 return -ENODEV;
95 }
96
Johan Hovold5f391972017-03-16 17:13:36 +010097 return 1;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -030098}
99
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100100static int aircable_process_packet(struct usb_serial_port *port,
101 int has_headers, char *packet, int len)
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300102{
Johan Hovold42725682010-05-05 23:45:24 +0200103 if (has_headers) {
104 len -= HCI_HEADER_LENGTH;
105 packet += HCI_HEADER_LENGTH;
106 }
107 if (len <= 0) {
Greg Kroah-Hartman66afb5b2012-05-15 16:27:08 -0700108 dev_dbg(&port->dev, "%s - malformed packet\n", __func__);
Johan Hovold42725682010-05-05 23:45:24 +0200109 return 0;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300110 }
111
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100112 tty_insert_flip_string(&port->port, packet, len);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300113
Johan Hovold42725682010-05-05 23:45:24 +0200114 return len;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300115}
116
Johan Hovold42725682010-05-05 23:45:24 +0200117static void aircable_process_read_urb(struct urb *urb)
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300118{
119 struct usb_serial_port *port = urb->context;
Johan Hovoldeb0c68e2020-07-08 14:50:00 +0200120 char *data = urb->transfer_buffer;
Johan Hovold42725682010-05-05 23:45:24 +0200121 int has_headers;
122 int count;
123 int len;
124 int i;
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300125
Johan Hovold42725682010-05-05 23:45:24 +0200126 has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300127
Johan Hovold42725682010-05-05 23:45:24 +0200128 count = 0;
129 for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) {
130 len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME);
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100131 count += aircable_process_packet(port, has_headers,
Johan Hovold42725682010-05-05 23:45:24 +0200132 &data[i], len);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300133 }
Johan Hovold42725682010-05-05 23:45:24 +0200134
135 if (count)
Jiri Slaby2e124b42013-01-03 15:53:06 +0100136 tty_flip_buffer_push(&port->port);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300137}
138
139static struct usb_serial_driver aircable_device = {
Johannes Hölzl52d67f02006-12-17 22:05:09 +0100140 .driver = {
141 .owner = THIS_MODULE,
142 .name = "aircable",
143 },
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300144 .id_table = id_table,
Johan Hovold42725682010-05-05 23:45:24 +0200145 .bulk_out_size = HCI_COMPLETE_FRAME,
Johan Hovold5f391972017-03-16 17:13:36 +0100146 .calc_num_ports = aircable_calc_num_ports,
Johan Hovold42725682010-05-05 23:45:24 +0200147 .process_read_urb = aircable_process_read_urb,
148 .prepare_write_buffer = aircable_prepare_write_buffer,
149 .throttle = usb_serial_generic_throttle,
150 .unthrottle = usb_serial_generic_unthrottle,
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300151};
152
Alan Stern08a4f6b2012-02-23 14:56:17 -0500153static struct usb_serial_driver * const serial_drivers[] = {
154 &aircable_device, NULL
155};
156
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700157module_usb_serial_driver(serial_drivers, id_table);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300158
159MODULE_AUTHOR(DRIVER_AUTHOR);
160MODULE_DESCRIPTION(DRIVER_DESC);
Johan Hovold627cfa82017-11-03 18:12:08 +0100161MODULE_LICENSE("GPL v2");