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
72
73
74#define DRIVER_VERSION "v2.0"
75#define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>"
76#define DRIVER_DESC "AIRcable USB Driver"
77
78
79static const struct usb_device_id id_table[] = {
80 { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
81 { },
82};
83MODULE_DEVICE_TABLE(usb, id_table);
84
85static int aircable_prepare_write_buffer(struct usb_serial_port *port,
86 void *dest, size_t size)
87{
88 int count;
89 unsigned char *buf = dest;
90
91 count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH,
92 size - HCI_HEADER_LENGTH, &port->lock);
93 buf[0] = TX_HEADER_0;
94 buf[1] = TX_HEADER_1;
95 put_unaligned_le16(count, &buf[2]);
96
97 return count + HCI_HEADER_LENGTH;
98}
99
100static int aircable_probe(struct usb_serial *serial,
101 const struct usb_device_id *id)
102{
103 struct usb_host_interface *iface_desc = serial->interface->
104 cur_altsetting;
105 struct usb_endpoint_descriptor *endpoint;
106 int num_bulk_out = 0;
107 int i;
108
109 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
110 endpoint = &iface_desc->endpoint[i].desc;
111 if (usb_endpoint_is_bulk_out(endpoint)) {
112 dev_dbg(&serial->dev->dev,
113 "found bulk out on endpoint %d\n", i);
114 ++num_bulk_out;
115 }
116 }
117
118 if (num_bulk_out == 0) {
119 dev_dbg(&serial->dev->dev, "Invalid interface, discarding\n");
120 return -ENODEV;
121 }
122
123 return 0;
124}
125
126static int aircable_process_packet(struct tty_struct *tty,
127 struct usb_serial_port *port, int has_headers,
128 char *packet, int len)
129{
130 if (has_headers) {
131 len -= HCI_HEADER_LENGTH;
132 packet += HCI_HEADER_LENGTH;
133 }
134 if (len <= 0) {
135 dev_dbg(&port->dev, "%s - malformed packet\n", __func__);
136 return 0;
137 }
138
139 tty_insert_flip_string(tty, packet, len);
140
141 return len;
142}
143
144static void aircable_process_read_urb(struct urb *urb)
145{
146 struct usb_serial_port *port = urb->context;
147 char *data = (char *)urb->transfer_buffer;
148 struct tty_struct *tty;
149 int has_headers;
150 int count;
151 int len;
152 int i;
153
154 tty = tty_port_tty_get(&port->port);
155 if (!tty)
156 return;
157
158 has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0);
159
160 count = 0;
161 for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) {
162 len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME);
163 count += aircable_process_packet(tty, port, has_headers,
164 &data[i], len);
165 }
166
167 if (count)
168 tty_flip_buffer_push(tty);
169 tty_kref_put(tty);
170}
171
172static struct usb_serial_driver aircable_device = {
173 .driver = {
174 .owner = THIS_MODULE,
175 .name = "aircable",
176 },
177 .id_table = id_table,
178 .num_ports = 1,
179 .bulk_out_size = HCI_COMPLETE_FRAME,
180 .probe = aircable_probe,
181 .process_read_urb = aircable_process_read_urb,
182 .prepare_write_buffer = aircable_prepare_write_buffer,
183 .throttle = usb_serial_generic_throttle,
184 .unthrottle = usb_serial_generic_unthrottle,
185};
186
187static struct usb_serial_driver * const serial_drivers[] = {
188 &aircable_device, NULL
189};
190
191module_usb_serial_driver(serial_drivers, id_table);
192
193MODULE_AUTHOR(DRIVER_AUTHOR);
194MODULE_DESCRIPTION(DRIVER_DESC);
195MODULE_VERSION(DRIVER_VERSION);
196MODULE_LICENSE("GPL");
197