GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / usb / serial / aircable.c
1 /*
2  * AIRcable USB Bluetooth Dongle Driver.
3  *
4  * Copyright (C) 2010 Johan Hovold <jhovold@gmail.com>
5  * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com)
6  *
7  * 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 possibilities reading or writing.
14  * When writing the first urb must have a Header that starts with 0x20 0x29 the
15  * next two bytes must say how much data will be sent.
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 coming 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.
23  * When writing to the device the first two bytes of the header are 0x20 0x29
24  * 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  * I have taken some info from a Greg Kroah-Hartman article:
33  * http://www.linuxjournal.com/article/6573
34  * And from Linux Device Driver Kit CD, which is a great work, the authors taken
35  * the work to recompile lots of information an knowledge in drivers development
36  * and made it all available inside a cd.
37  * URL: http://kernel.org/pub/linux/kernel/people/gregkh/ddk/
38  *
39  */
40
41 #include <asm/unaligned.h>
42 #include <linux/tty.h>
43 #include <linux/slab.h>
44 #include <linux/module.h>
45 #include <linux/tty_flip.h>
46 #include <linux/usb.h>
47 #include <linux/usb/serial.h>
48
49 /* Vendor and Product ID */
50 #define AIRCABLE_VID            0x16CA
51 #define AIRCABLE_USB_PID        0x1502
52
53 /* Protocol Stuff */
54 #define HCI_HEADER_LENGTH       0x4
55 #define TX_HEADER_0             0x20
56 #define TX_HEADER_1             0x29
57 #define RX_HEADER_0             0x00
58 #define RX_HEADER_1             0x20
59 #define HCI_COMPLETE_FRAME      64
60
61 /* rx_flags */
62 #define THROTTLED               0x01
63 #define ACTUALLY_THROTTLED      0x02
64
65 #define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>"
66 #define DRIVER_DESC "AIRcable USB Driver"
67
68 /* ID table that will be registered with USB core */
69 static const struct usb_device_id id_table[] = {
70         { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
71         { },
72 };
73 MODULE_DEVICE_TABLE(usb, id_table);
74
75 static int aircable_prepare_write_buffer(struct usb_serial_port *port,
76                                                 void *dest, size_t size)
77 {
78         int count;
79         unsigned char *buf = dest;
80
81         count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH,
82                                         size - HCI_HEADER_LENGTH, &port->lock);
83         buf[0] = TX_HEADER_0;
84         buf[1] = TX_HEADER_1;
85         put_unaligned_le16(count, &buf[2]);
86
87         return count + HCI_HEADER_LENGTH;
88 }
89
90 static int aircable_calc_num_ports(struct usb_serial *serial,
91                                         struct usb_serial_endpoints *epds)
92 {
93         /* Ignore the first interface, which has no bulk endpoints. */
94         if (epds->num_bulk_out == 0) {
95                 dev_dbg(&serial->interface->dev,
96                         "ignoring interface with no bulk-out endpoints\n");
97                 return -ENODEV;
98         }
99
100         return 1;
101 }
102
103 static int aircable_process_packet(struct usb_serial_port *port,
104                 int has_headers, char *packet, int len)
105 {
106         if (has_headers) {
107                 len -= HCI_HEADER_LENGTH;
108                 packet += HCI_HEADER_LENGTH;
109         }
110         if (len <= 0) {
111                 dev_dbg(&port->dev, "%s - malformed packet\n", __func__);
112                 return 0;
113         }
114
115         tty_insert_flip_string(&port->port, packet, len);
116
117         return len;
118 }
119
120 static void aircable_process_read_urb(struct urb *urb)
121 {
122         struct usb_serial_port *port = urb->context;
123         char *data = (char *)urb->transfer_buffer;
124         int has_headers;
125         int count;
126         int len;
127         int i;
128
129         has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0);
130
131         count = 0;
132         for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) {
133                 len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME);
134                 count += aircable_process_packet(port, has_headers,
135                                                                 &data[i], len);
136         }
137
138         if (count)
139                 tty_flip_buffer_push(&port->port);
140 }
141
142 static struct usb_serial_driver aircable_device = {
143         .driver = {
144                 .owner =        THIS_MODULE,
145                 .name =         "aircable",
146         },
147         .id_table =             id_table,
148         .bulk_out_size =        HCI_COMPLETE_FRAME,
149         .calc_num_ports =       aircable_calc_num_ports,
150         .process_read_urb =     aircable_process_read_urb,
151         .prepare_write_buffer = aircable_prepare_write_buffer,
152         .throttle =             usb_serial_generic_throttle,
153         .unthrottle =           usb_serial_generic_unthrottle,
154 };
155
156 static struct usb_serial_driver * const serial_drivers[] = {
157         &aircable_device, NULL
158 };
159
160 module_usb_serial_driver(serial_drivers, id_table);
161
162 MODULE_AUTHOR(DRIVER_AUTHOR);
163 MODULE_DESCRIPTION(DRIVER_DESC);
164 MODULE_LICENSE("GPL");