1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * AIRcable USB Bluetooth Dongle Driver.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2010 Johan Hovold <jhovold@gmail.com>
6*4882a593Smuzhiyun * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com)
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * The device works as an standard CDC device, it has 2 interfaces, the first
9*4882a593Smuzhiyun * one is for firmware access and the second is the serial one.
10*4882a593Smuzhiyun * The protocol is very simply, there are two possibilities reading or writing.
11*4882a593Smuzhiyun * When writing the first urb must have a Header that starts with 0x20 0x29 the
12*4882a593Smuzhiyun * next two bytes must say how much data will be sent.
13*4882a593Smuzhiyun * When reading the process is almost equal except that the header starts with
14*4882a593Smuzhiyun * 0x00 0x20.
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * The device simply need some stuff to understand data coming from the usb
17*4882a593Smuzhiyun * buffer: The First and Second byte is used for a Header, the Third and Fourth
18*4882a593Smuzhiyun * tells the device the amount of information the package holds.
19*4882a593Smuzhiyun * Packages are 60 bytes long Header Stuff.
20*4882a593Smuzhiyun * When writing to the device the first two bytes of the header are 0x20 0x29
21*4882a593Smuzhiyun * When reading the bytes are 0x00 0x20, or 0x00 0x10, there is an strange
22*4882a593Smuzhiyun * situation, when too much data arrives to the device because it sends the data
23*4882a593Smuzhiyun * but with out the header. I will use a simply hack to override this situation,
24*4882a593Smuzhiyun * if there is data coming that does not contain any header, then that is data
25*4882a593Smuzhiyun * that must go directly to the tty, as there is no documentation about if there
26*4882a593Smuzhiyun * is any other control code, I will simply check for the first
27*4882a593Smuzhiyun * one.
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * I have taken some info from a Greg Kroah-Hartman article:
30*4882a593Smuzhiyun * http://www.linuxjournal.com/article/6573
31*4882a593Smuzhiyun * And from Linux Device Driver Kit CD, which is a great work, the authors taken
32*4882a593Smuzhiyun * the work to recompile lots of information an knowledge in drivers development
33*4882a593Smuzhiyun * and made it all available inside a cd.
34*4882a593Smuzhiyun * URL: http://kernel.org/pub/linux/kernel/people/gregkh/ddk/
35*4882a593Smuzhiyun *
36*4882a593Smuzhiyun */
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #include <asm/unaligned.h>
39*4882a593Smuzhiyun #include <linux/tty.h>
40*4882a593Smuzhiyun #include <linux/slab.h>
41*4882a593Smuzhiyun #include <linux/module.h>
42*4882a593Smuzhiyun #include <linux/tty_flip.h>
43*4882a593Smuzhiyun #include <linux/usb.h>
44*4882a593Smuzhiyun #include <linux/usb/serial.h>
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /* Vendor and Product ID */
47*4882a593Smuzhiyun #define AIRCABLE_VID 0x16CA
48*4882a593Smuzhiyun #define AIRCABLE_USB_PID 0x1502
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /* Protocol Stuff */
51*4882a593Smuzhiyun #define HCI_HEADER_LENGTH 0x4
52*4882a593Smuzhiyun #define TX_HEADER_0 0x20
53*4882a593Smuzhiyun #define TX_HEADER_1 0x29
54*4882a593Smuzhiyun #define RX_HEADER_0 0x00
55*4882a593Smuzhiyun #define RX_HEADER_1 0x20
56*4882a593Smuzhiyun #define HCI_COMPLETE_FRAME 64
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun /* rx_flags */
59*4882a593Smuzhiyun #define THROTTLED 0x01
60*4882a593Smuzhiyun #define ACTUALLY_THROTTLED 0x02
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun #define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>"
63*4882a593Smuzhiyun #define DRIVER_DESC "AIRcable USB Driver"
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /* ID table that will be registered with USB core */
66*4882a593Smuzhiyun static const struct usb_device_id id_table[] = {
67*4882a593Smuzhiyun { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
68*4882a593Smuzhiyun { },
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun MODULE_DEVICE_TABLE(usb, id_table);
71*4882a593Smuzhiyun
aircable_prepare_write_buffer(struct usb_serial_port * port,void * dest,size_t size)72*4882a593Smuzhiyun static int aircable_prepare_write_buffer(struct usb_serial_port *port,
73*4882a593Smuzhiyun void *dest, size_t size)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun int count;
76*4882a593Smuzhiyun unsigned char *buf = dest;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH,
79*4882a593Smuzhiyun size - HCI_HEADER_LENGTH, &port->lock);
80*4882a593Smuzhiyun buf[0] = TX_HEADER_0;
81*4882a593Smuzhiyun buf[1] = TX_HEADER_1;
82*4882a593Smuzhiyun put_unaligned_le16(count, &buf[2]);
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun return count + HCI_HEADER_LENGTH;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
aircable_calc_num_ports(struct usb_serial * serial,struct usb_serial_endpoints * epds)87*4882a593Smuzhiyun static int aircable_calc_num_ports(struct usb_serial *serial,
88*4882a593Smuzhiyun struct usb_serial_endpoints *epds)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun /* Ignore the first interface, which has no bulk endpoints. */
91*4882a593Smuzhiyun if (epds->num_bulk_out == 0) {
92*4882a593Smuzhiyun dev_dbg(&serial->interface->dev,
93*4882a593Smuzhiyun "ignoring interface with no bulk-out endpoints\n");
94*4882a593Smuzhiyun return -ENODEV;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun return 1;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun
aircable_process_packet(struct usb_serial_port * port,int has_headers,char * packet,int len)100*4882a593Smuzhiyun static int aircable_process_packet(struct usb_serial_port *port,
101*4882a593Smuzhiyun int has_headers, char *packet, int len)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun if (has_headers) {
104*4882a593Smuzhiyun len -= HCI_HEADER_LENGTH;
105*4882a593Smuzhiyun packet += HCI_HEADER_LENGTH;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun if (len <= 0) {
108*4882a593Smuzhiyun dev_dbg(&port->dev, "%s - malformed packet\n", __func__);
109*4882a593Smuzhiyun return 0;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun tty_insert_flip_string(&port->port, packet, len);
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun return len;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
aircable_process_read_urb(struct urb * urb)117*4882a593Smuzhiyun static void aircable_process_read_urb(struct urb *urb)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun struct usb_serial_port *port = urb->context;
120*4882a593Smuzhiyun char *data = urb->transfer_buffer;
121*4882a593Smuzhiyun int has_headers;
122*4882a593Smuzhiyun int count;
123*4882a593Smuzhiyun int len;
124*4882a593Smuzhiyun int i;
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0);
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun count = 0;
129*4882a593Smuzhiyun for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) {
130*4882a593Smuzhiyun len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME);
131*4882a593Smuzhiyun count += aircable_process_packet(port, has_headers,
132*4882a593Smuzhiyun &data[i], len);
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun if (count)
136*4882a593Smuzhiyun tty_flip_buffer_push(&port->port);
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun static struct usb_serial_driver aircable_device = {
140*4882a593Smuzhiyun .driver = {
141*4882a593Smuzhiyun .owner = THIS_MODULE,
142*4882a593Smuzhiyun .name = "aircable",
143*4882a593Smuzhiyun },
144*4882a593Smuzhiyun .id_table = id_table,
145*4882a593Smuzhiyun .bulk_out_size = HCI_COMPLETE_FRAME,
146*4882a593Smuzhiyun .calc_num_ports = aircable_calc_num_ports,
147*4882a593Smuzhiyun .process_read_urb = aircable_process_read_urb,
148*4882a593Smuzhiyun .prepare_write_buffer = aircable_prepare_write_buffer,
149*4882a593Smuzhiyun .throttle = usb_serial_generic_throttle,
150*4882a593Smuzhiyun .unthrottle = usb_serial_generic_unthrottle,
151*4882a593Smuzhiyun };
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun static struct usb_serial_driver * const serial_drivers[] = {
154*4882a593Smuzhiyun &aircable_device, NULL
155*4882a593Smuzhiyun };
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun module_usb_serial_driver(serial_drivers, id_table);
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun MODULE_AUTHOR(DRIVER_AUTHOR);
160*4882a593Smuzhiyun MODULE_DESCRIPTION(DRIVER_DESC);
161*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
162