1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47#include <asm/unaligned.h>
48#include <linux/tty.h>
49#include <linux/slab.h>
50#include <linux/module.h>
51#include <linux/tty_flip.h>
52#include <linux/usb.h>
53#include <linux/usb/serial.h>
54
55
56#define AIRCABLE_VID 0x16CA
57#define AIRCABLE_USB_PID 0x1502
58
59
60#define HCI_HEADER_LENGTH 0x4
61#define TX_HEADER_0 0x20
62#define TX_HEADER_1 0x29
63#define RX_HEADER_0 0x00
64#define RX_HEADER_1 0x20
65#define HCI_COMPLETE_FRAME 64
66
67
68#define THROTTLED 0x01
69#define ACTUALLY_THROTTLED 0x02
70
71#define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>"
72#define DRIVER_DESC "AIRcable USB Driver"
73
74
75static const struct usb_device_id id_table[] = {
76 { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
77 { },
78};
79MODULE_DEVICE_TABLE(usb, id_table);
80
81static int aircable_prepare_write_buffer(struct usb_serial_port *port,
82 void *dest, size_t size)
83{
84 int count;
85 unsigned char *buf = dest;
86
87 count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH,
88 size - HCI_HEADER_LENGTH, &port->lock);
89 buf[0] = TX_HEADER_0;
90 buf[1] = TX_HEADER_1;
91 put_unaligned_le16(count, &buf[2]);
92
93 return count + HCI_HEADER_LENGTH;
94}
95
96static int aircable_probe(struct usb_serial *serial,
97 const struct usb_device_id *id)
98{
99 struct usb_host_interface *iface_desc = serial->interface->
100 cur_altsetting;
101 struct usb_endpoint_descriptor *endpoint;
102 int num_bulk_out = 0;
103 int i;
104
105 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
106 endpoint = &iface_desc->endpoint[i].desc;
107 if (usb_endpoint_is_bulk_out(endpoint)) {
108 dev_dbg(&serial->dev->dev,
109 "found bulk out on endpoint %d\n", i);
110 ++num_bulk_out;
111 }
112 }
113
114 if (num_bulk_out == 0) {
115 dev_dbg(&serial->dev->dev, "Invalid interface, discarding\n");
116 return -ENODEV;
117 }
118
119 return 0;
120}
121
122static int aircable_process_packet(struct usb_serial_port *port,
123 int has_headers, char *packet, int len)
124{
125 if (has_headers) {
126 len -= HCI_HEADER_LENGTH;
127 packet += HCI_HEADER_LENGTH;
128 }
129 if (len <= 0) {
130 dev_dbg(&port->dev, "%s - malformed packet\n", __func__);
131 return 0;
132 }
133
134 tty_insert_flip_string(&port->port, packet, len);
135
136 return len;
137}
138
139static void aircable_process_read_urb(struct urb *urb)
140{
141 struct usb_serial_port *port = urb->context;
142 char *data = (char *)urb->transfer_buffer;
143 int has_headers;
144 int count;
145 int len;
146 int i;
147
148 has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0);
149
150 count = 0;
151 for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) {
152 len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME);
153 count += aircable_process_packet(port, has_headers,
154 &data[i], len);
155 }
156
157 if (count)
158 tty_flip_buffer_push(&port->port);
159}
160
161static struct usb_serial_driver aircable_device = {
162 .driver = {
163 .owner = THIS_MODULE,
164 .name = "aircable",
165 },
166 .id_table = id_table,
167 .num_ports = 1,
168 .bulk_out_size = HCI_COMPLETE_FRAME,
169 .probe = aircable_probe,
170 .process_read_urb = aircable_process_read_urb,
171 .prepare_write_buffer = aircable_prepare_write_buffer,
172 .throttle = usb_serial_generic_throttle,
173 .unthrottle = usb_serial_generic_unthrottle,
174};
175
176static struct usb_serial_driver * const serial_drivers[] = {
177 &aircable_device, NULL
178};
179
180module_usb_serial_driver(serial_drivers, id_table);
181
182MODULE_AUTHOR(DRIVER_AUTHOR);
183MODULE_DESCRIPTION(DRIVER_DESC);
184MODULE_LICENSE("GPL");
185