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#include <linux/tty.h>
46#include <linux/tty_flip.h>
47#include <linux/circ_buf.h>
48#include <linux/usb.h>
49#include <linux/usb/serial.h>
50
51static int debug;
52
53
54#define AIRCABLE_VID 0x16CA
55#define AIRCABLE_USB_PID 0x1502
56
57
58#define AIRCABLE_BUF_SIZE 2048
59
60
61#define HCI_HEADER_LENGTH 0x4
62#define TX_HEADER_0 0x20
63#define TX_HEADER_1 0x29
64#define RX_HEADER_0 0x00
65#define RX_HEADER_1 0x20
66#define MAX_HCI_FRAMESIZE 60
67#define HCI_COMPLETE_FRAME 64
68
69
70#define THROTTLED 0x01
71#define ACTUALLY_THROTTLED 0x02
72
73
74
75
76#define DRIVER_VERSION "v1.0b2"
77#define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>"
78#define DRIVER_DESC "AIRcable USB Driver"
79
80
81static struct usb_device_id id_table [] = {
82 { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
83 { },
84};
85MODULE_DEVICE_TABLE(usb, id_table);
86
87
88
89struct aircable_private {
90 spinlock_t rx_lock;
91 struct circ_buf *tx_buf;
92 struct circ_buf *rx_buf;
93 int rx_flags;
94 struct work_struct rx_work;
95 struct usb_serial_port *port;
96};
97
98
99
100
101
102
103
104
105
106static void serial_buf_clear(struct circ_buf *cb)
107{
108 cb->head = cb->tail = 0;
109}
110
111
112
113
114
115
116static struct circ_buf *serial_buf_alloc(void)
117{
118 struct circ_buf *cb;
119 cb = kmalloc(sizeof(struct circ_buf), GFP_KERNEL);
120 if (cb == NULL)
121 return NULL;
122 cb->buf = kmalloc(AIRCABLE_BUF_SIZE, GFP_KERNEL);
123 if (cb->buf == NULL) {
124 kfree(cb);
125 return NULL;
126 }
127 serial_buf_clear(cb);
128 return cb;
129}
130
131
132
133
134
135
136static void serial_buf_free(struct circ_buf *cb)
137{
138 kfree(cb->buf);
139 kfree(cb);
140}
141
142
143
144
145
146
147
148static int serial_buf_data_avail(struct circ_buf *cb)
149{
150 return CIRC_CNT(cb->head,cb->tail,AIRCABLE_BUF_SIZE);
151}
152
153
154
155
156
157
158
159
160
161static int serial_buf_put(struct circ_buf *cb, const char *buf, int count)
162{
163 int c, ret = 0;
164 while (1) {
165 c = CIRC_SPACE_TO_END(cb->head, cb->tail, AIRCABLE_BUF_SIZE);
166 if (count < c)
167 c = count;
168 if (c <= 0)
169 break;
170 memcpy(cb->buf + cb->head, buf, c);
171 cb->head = (cb->head + c) & (AIRCABLE_BUF_SIZE-1);
172 buf += c;
173 count -= c;
174 ret= c;
175 }
176 return ret;
177}
178
179
180
181
182
183
184
185
186
187static int serial_buf_get(struct circ_buf *cb, char *buf, int count)
188{
189 int c, ret = 0;
190 while (1) {
191 c = CIRC_CNT_TO_END(cb->head, cb->tail, AIRCABLE_BUF_SIZE);
192 if (count < c)
193 c = count;
194 if (c <= 0)
195 break;
196 memcpy(buf, cb->buf + cb->tail, c);
197 cb->tail = (cb->tail + c) & (AIRCABLE_BUF_SIZE-1);
198 buf += c;
199 count -= c;
200 ret= c;
201 }
202 return ret;
203}
204
205
206
207static void aircable_send(struct usb_serial_port *port)
208{
209 int count, result;
210 struct aircable_private *priv = usb_get_serial_port_data(port);
211 unsigned char* buf;
212 u16 *dbuf;
213 dbg("%s - port %d", __FUNCTION__, port->number);
214 if (port->write_urb_busy)
215 return;
216
217 count = min(serial_buf_data_avail(priv->tx_buf), MAX_HCI_FRAMESIZE);
218 if (count == 0)
219 return;
220
221 buf = kzalloc(count + HCI_HEADER_LENGTH, GFP_ATOMIC);
222 if (!buf) {
223 err("%s- kzalloc(%d) failed.", __FUNCTION__,
224 count + HCI_HEADER_LENGTH);
225 return;
226 }
227
228 buf[0] = TX_HEADER_0;
229 buf[1] = TX_HEADER_1;
230 dbuf = (u16 *)&buf[2];
231 *dbuf = cpu_to_le16((u16)count);
232 serial_buf_get(priv->tx_buf,buf + HCI_HEADER_LENGTH, MAX_HCI_FRAMESIZE);
233
234 memcpy(port->write_urb->transfer_buffer, buf,
235 count + HCI_HEADER_LENGTH);
236
237 kfree(buf);
238 port->write_urb_busy = 1;
239 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
240 count + HCI_HEADER_LENGTH,
241 port->write_urb->transfer_buffer);
242 port->write_urb->transfer_buffer_length = count + HCI_HEADER_LENGTH;
243 port->write_urb->dev = port->serial->dev;
244 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
245
246 if (result) {
247 dev_err(&port->dev,
248 "%s - failed submitting write urb, error %d\n",
249 __FUNCTION__, result);
250 port->write_urb_busy = 0;
251 }
252
253 schedule_work(&port->work);
254}
255
256static void aircable_read(struct work_struct *work)
257{
258 struct aircable_private *priv =
259 container_of(work, struct aircable_private, rx_work);
260 struct usb_serial_port *port = priv->port;
261 struct tty_struct *tty;
262 unsigned char *data;
263 int count;
264 if (priv->rx_flags & THROTTLED){
265 if (priv->rx_flags & ACTUALLY_THROTTLED)
266 schedule_work(&priv->rx_work);
267 return;
268 }
269
270
271
272
273
274 tty = port->tty;
275
276 if (!tty) {
277 schedule_work(&priv->rx_work);
278 err("%s - No tty available", __FUNCTION__);
279 return ;
280 }
281
282 count = min(64, serial_buf_data_avail(priv->rx_buf));
283
284 if (count <= 0)
285 return;
286
287 tty_prepare_flip_string(tty, &data, count);
288 if (!data){
289 err("%s- kzalloc(%d) failed.", __FUNCTION__, count);
290 return;
291 }
292
293 serial_buf_get(priv->rx_buf, data, count);
294
295 tty_flip_buffer_push(tty);
296
297 if (serial_buf_data_avail(priv->rx_buf))
298 schedule_work(&priv->rx_work);
299
300 return;
301}
302
303
304static int aircable_probe(struct usb_serial *serial,
305 const struct usb_device_id *id)
306{
307 struct usb_host_interface *iface_desc = serial->interface->cur_altsetting;
308 struct usb_endpoint_descriptor *endpoint;
309 int num_bulk_out=0;
310 int i;
311
312 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
313 endpoint = &iface_desc->endpoint[i].desc;
314 if (usb_endpoint_is_bulk_out(endpoint)) {
315 dbg("found bulk out on endpoint %d", i);
316 ++num_bulk_out;
317 }
318 }
319
320 if (num_bulk_out == 0) {
321 dbg("Invalid interface, discarding");
322 return -ENODEV;
323 }
324
325 return 0;
326}
327
328static int aircable_attach (struct usb_serial *serial)
329{
330 struct usb_serial_port *port = serial->port[0];
331 struct aircable_private *priv;
332
333 priv = kzalloc(sizeof(struct aircable_private), GFP_KERNEL);
334 if (!priv){
335 err("%s- kmalloc(%Zd) failed.", __FUNCTION__,
336 sizeof(struct aircable_private));
337 return -ENOMEM;
338 }
339
340
341 priv->tx_buf = serial_buf_alloc();
342 if (priv->tx_buf == NULL) {
343 kfree(priv);
344 return -ENOMEM;
345 }
346
347 priv->rx_buf = serial_buf_alloc();
348 if (priv->rx_buf == NULL) {
349 kfree(priv->tx_buf);
350 kfree(priv);
351 return -ENOMEM;
352 }
353
354 priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
355 priv->port = port;
356 INIT_WORK(&priv->rx_work, aircable_read);
357
358 usb_set_serial_port_data(serial->port[0], priv);
359
360 return 0;
361}
362
363static void aircable_shutdown(struct usb_serial *serial)
364{
365
366 struct usb_serial_port *port = serial->port[0];
367 struct aircable_private *priv = usb_get_serial_port_data(port);
368
369 dbg("%s", __FUNCTION__);
370
371 if (priv) {
372 serial_buf_free(priv->tx_buf);
373 serial_buf_free(priv->rx_buf);
374 usb_set_serial_port_data(port, NULL);
375 kfree(priv);
376 }
377}
378
379static int aircable_write_room(struct usb_serial_port *port)
380{
381 struct aircable_private *priv = usb_get_serial_port_data(port);
382 return serial_buf_data_avail(priv->tx_buf);
383}
384
385static int aircable_write(struct usb_serial_port *port,
386 const unsigned char *source, int count)
387{
388 struct aircable_private *priv = usb_get_serial_port_data(port);
389 int temp;
390
391 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
392
393 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, source);
394
395 if (!count){
396 dbg("%s - write request of 0 bytes", __FUNCTION__);
397 return count;
398 }
399
400 temp = serial_buf_put(priv->tx_buf, source, count);
401
402 aircable_send(port);
403
404 if (count > AIRCABLE_BUF_SIZE)
405 count = AIRCABLE_BUF_SIZE;
406
407 return count;
408
409}
410
411static void aircable_write_bulk_callback(struct urb *urb)
412{
413 struct usb_serial_port *port = urb->context;
414 int status = urb->status;
415 int result;
416
417 dbg("%s - urb status: %d", __FUNCTION__ , status);
418
419
420 switch (status) {
421 case 0:
422
423 break;
424 case -ECONNRESET:
425 case -ENOENT:
426 case -ESHUTDOWN:
427
428 dbg("%s - urb shutting down with status: %d",
429 __FUNCTION__, status);
430 port->write_urb_busy = 0;
431 return;
432 default:
433
434 dbg("%s - Overflow in write", __FUNCTION__);
435 dbg("%s - nonzero write bulk status received: %d",
436 __FUNCTION__, status);
437 port->write_urb->transfer_buffer_length = 1;
438 port->write_urb->dev = port->serial->dev;
439 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
440 if (result)
441 dev_err(&urb->dev->dev,
442 "%s - failed resubmitting write urb, error %d\n",
443 __FUNCTION__, result);
444 else
445 return;
446 }
447
448 port->write_urb_busy = 0;
449
450 aircable_send(port);
451}
452
453static void aircable_read_bulk_callback(struct urb *urb)
454{
455 struct usb_serial_port *port = urb->context;
456 struct aircable_private *priv = usb_get_serial_port_data(port);
457 struct tty_struct *tty;
458 unsigned long no_packages, remaining, package_length, i;
459 int result, shift = 0;
460 unsigned char *temp;
461 int status = urb->status;
462
463 dbg("%s - port %d", __FUNCTION__, port->number);
464
465 if (status) {
466 dbg("%s - urb status = %d", __FUNCTION__, status);
467 if (!port->open_count) {
468 dbg("%s - port is closed, exiting.", __FUNCTION__);
469 return;
470 }
471 if (status == -EPROTO) {
472 dbg("%s - caught -EPROTO, resubmitting the urb",
473 __FUNCTION__);
474 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
475 usb_rcvbulkpipe(port->serial->dev,
476 port->bulk_in_endpointAddress),
477 port->read_urb->transfer_buffer,
478 port->read_urb->transfer_buffer_length,
479 aircable_read_bulk_callback, port);
480
481 result = usb_submit_urb(urb, GFP_ATOMIC);
482 if (result)
483 dev_err(&urb->dev->dev,
484 "%s - failed resubmitting read urb, error %d\n",
485 __FUNCTION__, result);
486 return;
487 }
488 dbg("%s - unable to handle the error, exiting.", __FUNCTION__);
489 return;
490 }
491
492 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
493 urb->actual_length,urb->transfer_buffer);
494
495 tty = port->tty;
496 if (tty && urb->actual_length) {
497 if (urb->actual_length <= 2) {
498
499 serial_buf_put(priv->rx_buf, urb->transfer_buffer,
500 urb->actual_length);
501 } else {
502 temp = urb->transfer_buffer;
503 if (temp[0] == RX_HEADER_0)
504 shift = HCI_HEADER_LENGTH;
505
506 remaining = urb->actual_length;
507 no_packages = urb->actual_length / (HCI_COMPLETE_FRAME);
508
509 if (urb->actual_length % HCI_COMPLETE_FRAME != 0)
510 no_packages+=1;
511
512 for (i = 0; i < no_packages ;i++) {
513 if (remaining > (HCI_COMPLETE_FRAME))
514 package_length = HCI_COMPLETE_FRAME;
515 else
516 package_length = remaining;
517 remaining -= package_length;
518
519 serial_buf_put(priv->rx_buf,
520 urb->transfer_buffer + shift +
521 (HCI_COMPLETE_FRAME) * (i),
522 package_length - shift);
523 }
524 }
525 aircable_read(&priv->rx_work);
526 }
527
528
529 if (port->open_count) {
530 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
531 usb_rcvbulkpipe(port->serial->dev,
532 port->bulk_in_endpointAddress),
533 port->read_urb->transfer_buffer,
534 port->read_urb->transfer_buffer_length,
535 aircable_read_bulk_callback, port);
536
537 result = usb_submit_urb(urb, GFP_ATOMIC);
538 if (result)
539 dev_err(&urb->dev->dev,
540 "%s - failed resubmitting read urb, error %d\n",
541 __FUNCTION__, result);
542 }
543
544 return;
545}
546
547
548static void aircable_throttle(struct usb_serial_port *port)
549{
550 struct aircable_private *priv = usb_get_serial_port_data(port);
551 unsigned long flags;
552
553 dbg("%s - port %d", __FUNCTION__, port->number);
554
555 spin_lock_irqsave(&priv->rx_lock, flags);
556 priv->rx_flags |= THROTTLED;
557 spin_unlock_irqrestore(&priv->rx_lock, flags);
558}
559
560
561static void aircable_unthrottle(struct usb_serial_port *port)
562{
563 struct aircable_private *priv = usb_get_serial_port_data(port);
564 int actually_throttled;
565 unsigned long flags;
566
567 dbg("%s - port %d", __FUNCTION__, port->number);
568
569 spin_lock_irqsave(&priv->rx_lock, flags);
570 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
571 priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
572 spin_unlock_irqrestore(&priv->rx_lock, flags);
573
574 if (actually_throttled)
575 schedule_work(&priv->rx_work);
576}
577
578static struct usb_driver aircable_driver = {
579 .name = "aircable",
580 .probe = usb_serial_probe,
581 .disconnect = usb_serial_disconnect,
582 .id_table = id_table,
583 .no_dynamic_id = 1,
584};
585
586static struct usb_serial_driver aircable_device = {
587 .driver = {
588 .owner = THIS_MODULE,
589 .name = "aircable",
590 },
591 .usb_driver = &aircable_driver,
592 .id_table = id_table,
593 .num_ports = 1,
594 .attach = aircable_attach,
595 .probe = aircable_probe,
596 .shutdown = aircable_shutdown,
597 .write = aircable_write,
598 .write_room = aircable_write_room,
599 .write_bulk_callback = aircable_write_bulk_callback,
600 .read_bulk_callback = aircable_read_bulk_callback,
601 .throttle = aircable_throttle,
602 .unthrottle = aircable_unthrottle,
603};
604
605static int __init aircable_init (void)
606{
607 int retval;
608 retval = usb_serial_register(&aircable_device);
609 if (retval)
610 goto failed_serial_register;
611 retval = usb_register(&aircable_driver);
612 if (retval)
613 goto failed_usb_register;
614 return 0;
615
616failed_serial_register:
617 usb_serial_deregister(&aircable_device);
618failed_usb_register:
619 return retval;
620}
621
622static void __exit aircable_exit (void)
623{
624 usb_deregister(&aircable_driver);
625 usb_serial_deregister(&aircable_device);
626}
627
628MODULE_AUTHOR(DRIVER_AUTHOR);
629MODULE_DESCRIPTION(DRIVER_DESC);
630MODULE_VERSION(DRIVER_VERSION);
631MODULE_LICENSE("GPL");
632
633module_init(aircable_init);
634module_exit(aircable_exit);
635
636module_param(debug, bool, S_IRUGO | S_IWUSR);
637MODULE_PARM_DESC(debug, "Debug enabled or not");
638