blob: 8554c1a7b9fc84138d452b005d8b61984d400563 [file] [log] [blame]
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -03001/*
2 * AIRcable USB Bluetooth Dongle Driver.
3 *
4 * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com)
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License version 2 as published by the
7 * Free Software Foundation.
8 *
9 * The device works as an standard CDC device, it has 2 interfaces, the first
10 * one is for firmware access and the second is the serial one.
11 * The protocol is very simply, there are two posibilities reading or writing.
12 * When writting the first urb must have a Header that starts with 0x20 0x29 the
13 * next two bytes must say how much data will be sended.
14 * When reading the process is almost equal except that the header starts with
15 * 0x00 0x20.
16 *
17 * The device simply need some stuff to understand data comming from the usb
18 * buffer: The First and Second byte is used for a Header, the Third and Fourth
19 * tells the device the amount of information the package holds.
20 * Packages are 60 bytes long Header Stuff.
21 * When writting to the device the first two bytes of the header are 0x20 0x29
22 * When reading the bytes are 0x00 0x20, or 0x00 0x10, there is an strange
23 * situation, when too much data arrives to the device because it sends the data
24 * but with out the header. I will use a simply hack to override this situation,
25 * if there is data coming that does not contain any header, then that is data
26 * that must go directly to the tty, as there is no documentation about if there
27 * is any other control code, I will simply check for the first
28 * one.
29 *
30 * The driver registers himself with the USB-serial core and the USB Core. I had
31 * to implement a probe function agains USB-serial, because other way, the
32 * driver was attaching himself to both interfaces. I have tryed with different
33 * configurations of usb_serial_driver with out exit, only the probe function
34 * could handle this correctly.
35 *
36 * I have taken some info from a Greg Kroah-Hartman article:
37 * http://www.linuxjournal.com/article/6573
38 * And from Linux Device Driver Kit CD, which is a great work, the authors taken
39 * the work to recompile lots of information an knowladge in drivers development
40 * and made it all avaible inside a cd.
41 * URL: http://kernel.org/pub/linux/kernel/people/gregkh/ddk/
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/* Vendor and Product ID */
54#define AIRCABLE_VID 0x16CA
55#define AIRCABLE_USB_PID 0x1502
56
57/* write buffer size defines */
58#define AIRCABLE_BUF_SIZE 2048
59
60/* Protocol Stuff */
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/* rx_flags */
70#define THROTTLED 0x01
71#define ACTUALLY_THROTTLED 0x02
72
73/*
74 * Version Information
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/* ID table that will be registered with USB core */
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/* Internal Structure */
89struct aircable_private {
90 spinlock_t rx_lock; /* spinlock for the receive lines */
91 struct circ_buf *tx_buf; /* write buffer */
92 struct circ_buf *rx_buf; /* read buffer */
93 int rx_flags; /* for throttilng */
94 struct work_struct rx_work; /* work cue for the receiving line */
95};
96
97/* Private methods */
98
99/* Circular Buffer Methods, code from ti_usb_3410_5052 used */
100/*
101 * serial_buf_clear
102 *
103 * Clear out all data in the circular buffer.
104 */
105static void serial_buf_clear(struct circ_buf *cb)
106{
107 cb->head = cb->tail = 0;
108}
109
110/*
111 * serial_buf_alloc
112 *
113 * Allocate a circular buffer and all associated memory.
114 */
115static struct circ_buf *serial_buf_alloc(void)
116{
117 struct circ_buf *cb;
118 cb = kmalloc(sizeof(struct circ_buf), GFP_KERNEL);
119 if (cb == NULL)
120 return NULL;
121 cb->buf = kmalloc(AIRCABLE_BUF_SIZE, GFP_KERNEL);
122 if (cb->buf == NULL) {
123 kfree(cb);
124 return NULL;
125 }
126 serial_buf_clear(cb);
127 return cb;
128}
129
130/*
131 * serial_buf_free
132 *
133 * Free the buffer and all associated memory.
134 */
135static void serial_buf_free(struct circ_buf *cb)
136{
137 kfree(cb->buf);
138 kfree(cb);
139}
140
141/*
142 * serial_buf_data_avail
143 *
144 * Return the number of bytes of data available in the circular
145 * buffer.
146 */
147static int serial_buf_data_avail(struct circ_buf *cb)
148{
149 return CIRC_CNT(cb->head,cb->tail,AIRCABLE_BUF_SIZE);
150}
151
152/*
153 * serial_buf_put
154 *
155 * Copy data data from a user buffer and put it into the circular buffer.
156 * Restrict to the amount of space available.
157 *
158 * Return the number of bytes copied.
159 */
160static int serial_buf_put(struct circ_buf *cb, const char *buf, int count)
161{
162 int c, ret = 0;
163 while (1) {
164 c = CIRC_SPACE_TO_END(cb->head, cb->tail, AIRCABLE_BUF_SIZE);
165 if (count < c)
166 c = count;
167 if (c <= 0)
168 break;
169 memcpy(cb->buf + cb->head, buf, c);
170 cb->head = (cb->head + c) & (AIRCABLE_BUF_SIZE-1);
171 buf += c;
172 count -= c;
173 ret= c;
174 }
175 return ret;
176}
177
178/*
179 * serial_buf_get
180 *
181 * Get data from the circular buffer and copy to the given buffer.
182 * Restrict to the amount of data available.
183 *
184 * Return the number of bytes copied.
185 */
186static int serial_buf_get(struct circ_buf *cb, char *buf, int count)
187{
188 int c, ret = 0;
189 while (1) {
190 c = CIRC_CNT_TO_END(cb->head, cb->tail, AIRCABLE_BUF_SIZE);
191 if (count < c)
192 c = count;
193 if (c <= 0)
194 break;
195 memcpy(buf, cb->buf + cb->tail, c);
196 cb->tail = (cb->tail + c) & (AIRCABLE_BUF_SIZE-1);
197 buf += c;
198 count -= c;
199 ret= c;
200 }
201 return ret;
202}
203
204/* End of circula buffer methods */
205
206static void aircable_send(struct usb_serial_port *port)
207{
208 int count, result;
209 struct aircable_private *priv = usb_get_serial_port_data(port);
210 unsigned char* buf;
211 dbg("%s - port %d", __FUNCTION__, port->number);
212 if (port->write_urb_busy)
213 return;
214
215 count = min(serial_buf_data_avail(priv->tx_buf), MAX_HCI_FRAMESIZE);
216 if (count == 0)
217 return;
218
219 buf = kzalloc(count + HCI_HEADER_LENGTH, GFP_ATOMIC);
220 if (!buf) {
Randy Dunlap8ac283a2006-08-26 10:56:10 -0700221 err("%s- kzalloc(%d) failed.", __FUNCTION__,
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300222 count + HCI_HEADER_LENGTH);
223 return;
224 }
225
226 buf[0] = TX_HEADER_0;
227 buf[1] = TX_HEADER_1;
228 buf[2] = (unsigned char)count;
229 buf[3] = (unsigned char)(count >> 8);
230 serial_buf_get(priv->tx_buf,buf + HCI_HEADER_LENGTH, MAX_HCI_FRAMESIZE);
231
232 memcpy(port->write_urb->transfer_buffer, buf,
233 count + HCI_HEADER_LENGTH);
234
235 kfree(buf);
236 port->write_urb_busy = 1;
237 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
238 count + HCI_HEADER_LENGTH,
239 port->write_urb->transfer_buffer);
240 port->write_urb->transfer_buffer_length = count + HCI_HEADER_LENGTH;
241 port->write_urb->dev = port->serial->dev;
242 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
243
244 if (result) {
245 dev_err(&port->dev,
246 "%s - failed submitting write urb, error %d\n",
247 __FUNCTION__, result);
248 port->write_urb_busy = 0;
249 }
250
251 schedule_work(&port->work);
252}
253
254static void aircable_read(void *params)
255{
256 struct usb_serial_port *port = params;
257 struct aircable_private *priv = usb_get_serial_port_data(port);
258 struct tty_struct *tty;
259 unsigned char *data;
260 int count;
261 if (priv->rx_flags & THROTTLED){
262 if (priv->rx_flags & ACTUALLY_THROTTLED)
263 schedule_work(&priv->rx_work);
264 return;
265 }
266
267 /* By now I will flush data to the tty in packages of no more than
268 * 64 bytes, to ensure I do not get throttled.
269 * Ask USB mailing list for better aproach.
270 */
271 tty = port->tty;
272
273 if (!tty)
274 schedule_work(&priv->rx_work);
275
276 count = min(64, serial_buf_data_avail(priv->rx_buf));
277
278 if (count <= 0)
279 return; //We have finished sending everything.
280
281 tty_prepare_flip_string(tty, &data, count);
282 if (!data){
Randy Dunlap8ac283a2006-08-26 10:56:10 -0700283 err("%s- kzalloc(%d) failed.", __FUNCTION__, count);
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300284 return;
285 }
286
287 serial_buf_get(priv->rx_buf, data, count);
288
289 tty_flip_buffer_push(tty);
290
291 if (serial_buf_data_avail(priv->rx_buf))
292 schedule_work(&priv->rx_work);
293
294 return;
295}
296/* End of private methods */
297
298static int aircable_probe(struct usb_serial *serial,
299 const struct usb_device_id *id)
300{
301 struct usb_host_interface *iface_desc = serial->interface->cur_altsetting;
302 struct usb_endpoint_descriptor *endpoint;
303 int num_bulk_out=0;
304 int i;
305
306 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
307 endpoint = &iface_desc->endpoint[i].desc;
Luiz Fernando N. Capitulino377f13b2006-10-26 13:02:45 -0300308 if (usb_endpoint_is_bulk_out(endpoint)) {
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300309 dbg("found bulk out on endpoint %d", i);
310 ++num_bulk_out;
311 }
312 }
313
314 if (num_bulk_out == 0) {
315 dbg("Invalid interface, discarding");
316 return -ENODEV;
317 }
318
319 return 0;
320}
321
322static int aircable_attach (struct usb_serial *serial)
323{
324 struct usb_serial_port *port = serial->port[0];
325 struct aircable_private *priv;
326
327 priv = kzalloc(sizeof(struct aircable_private), GFP_KERNEL);
328 if (!priv){
329 err("%s- kmalloc(%Zd) failed.", __FUNCTION__,
330 sizeof(struct aircable_private));
331 return -ENOMEM;
332 }
333
334 /* Allocation of Circular Buffers */
335 priv->tx_buf = serial_buf_alloc();
336 if (priv->tx_buf == NULL) {
337 kfree(priv);
338 return -ENOMEM;
339 }
340
341 priv->rx_buf = serial_buf_alloc();
342 if (priv->rx_buf == NULL) {
343 kfree(priv->tx_buf);
344 kfree(priv);
345 return -ENOMEM;
346 }
347
348 priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
349 INIT_WORK(&priv->rx_work, aircable_read, port);
350
351 usb_set_serial_port_data(serial->port[0], priv);
352
353 return 0;
354}
355
356static void aircable_shutdown(struct usb_serial *serial)
357{
358
359 struct usb_serial_port *port = serial->port[0];
360 struct aircable_private *priv = usb_get_serial_port_data(port);
361
362 dbg("%s", __FUNCTION__);
363
364 if (priv) {
365 serial_buf_free(priv->tx_buf);
366 serial_buf_free(priv->rx_buf);
367 usb_set_serial_port_data(port, NULL);
368 kfree(priv);
369 }
370}
371
372static int aircable_write_room(struct usb_serial_port *port)
373{
374 struct aircable_private *priv = usb_get_serial_port_data(port);
375 return serial_buf_data_avail(priv->tx_buf);
376}
377
378static int aircable_write(struct usb_serial_port *port,
379 const unsigned char *source, int count)
380{
381 struct aircable_private *priv = usb_get_serial_port_data(port);
382 int temp;
383
384 dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
385
386 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, source);
387
388 if (!count){
389 dbg("%s - write request of 0 bytes", __FUNCTION__);
390 return count;
391 }
392
393 temp = serial_buf_put(priv->tx_buf, source, count);
394
395 aircable_send(port);
396
397 if (count > AIRCABLE_BUF_SIZE)
398 count = AIRCABLE_BUF_SIZE;
399
400 return count;
401
402}
403
David Howells7d12e782006-10-05 14:55:46 +0100404static void aircable_write_bulk_callback(struct urb *urb)
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300405{
406 struct usb_serial_port *port = urb->context;
407 int result;
408
409 dbg("%s - urb->status: %d", __FUNCTION__ , urb->status);
410
411 /* This has been taken from cypress_m8.c cypress_write_int_callback */
412 switch (urb->status) {
413 case 0:
414 /* success */
415 break;
416 case -ECONNRESET:
417 case -ENOENT:
418 case -ESHUTDOWN:
419 /* this urb is terminated, clean up */
420 dbg("%s - urb shutting down with status: %d",
421 __FUNCTION__, urb->status);
422 port->write_urb_busy = 0;
423 return;
424 default:
425 /* error in the urb, so we have to resubmit it */
426 dbg("%s - Overflow in write", __FUNCTION__);
427 dbg("%s - nonzero write bulk status received: %d",
428 __FUNCTION__, urb->status);
429 port->write_urb->transfer_buffer_length = 1;
430 port->write_urb->dev = port->serial->dev;
431 result = usb_submit_urb(port->write_urb, GFP_KERNEL);
432 if (result)
433 dev_err(&urb->dev->dev,
434 "%s - failed resubmitting write urb, error %d\n",
435 __FUNCTION__, result);
436 else
437 return;
438 }
439
440 port->write_urb_busy = 0;
441
442 aircable_send(port);
443}
444
David Howells7d12e782006-10-05 14:55:46 +0100445static void aircable_read_bulk_callback(struct urb *urb)
Manuel Francisco Naranjo3fe70ba2006-08-09 16:35:12 -0300446{
447 struct usb_serial_port *port = urb->context;
448 struct aircable_private *priv = usb_get_serial_port_data(port);
449 struct tty_struct *tty;
450 unsigned long no_packages, remaining, package_length, i;
451 int result, shift = 0;
452 unsigned char *temp;
453
454 dbg("%s - port %d", __FUNCTION__, port->number);
455
456 if (urb->status) {
457 dbg("%s - urb->status = %d", __FUNCTION__, urb->status);
458 if (!port->open_count) {
459 dbg("%s - port is closed, exiting.", __FUNCTION__);
460 return;
461 }
462 if (urb->status == -EPROTO) {
463 dbg("%s - caught -EPROTO, resubmitting the urb",
464 __FUNCTION__);
465 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
466 usb_rcvbulkpipe(port->serial->dev,
467 port->bulk_in_endpointAddress),
468 port->read_urb->transfer_buffer,
469 port->read_urb->transfer_buffer_length,
470 aircable_read_bulk_callback, port);
471
472 result = usb_submit_urb(urb, GFP_ATOMIC);
473 if (result)
474 dev_err(&urb->dev->dev,
475 "%s - failed resubmitting read urb, error %d\n",
476 __FUNCTION__, result);
477 return;
478 }
479 dbg("%s - unable to handle the error, exiting.", __FUNCTION__);
480 return;
481 }
482
483 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
484 urb->actual_length,urb->transfer_buffer);
485
486 tty = port->tty;
487 if (tty && urb->actual_length) {
488 if (urb->actual_length <= 2) {
489 /* This is an incomplete package */
490 serial_buf_put(priv->rx_buf, urb->transfer_buffer,
491 urb->actual_length);
492 } else {
493 temp = urb->transfer_buffer;
494 if (temp[0] == RX_HEADER_0)
495 shift = HCI_HEADER_LENGTH;
496
497 remaining = urb->actual_length;
498 no_packages = urb->actual_length / (HCI_COMPLETE_FRAME);
499
500 if (urb->actual_length % HCI_COMPLETE_FRAME != 0)
501 no_packages+=1;
502
503 for (i = 0; i < no_packages ;i++) {
504 if (remaining > (HCI_COMPLETE_FRAME))
505 package_length = HCI_COMPLETE_FRAME;
506 else
507 package_length = remaining;
508 remaining -= package_length;
509
510 serial_buf_put(priv->rx_buf,
511 urb->transfer_buffer + shift +
512 (HCI_COMPLETE_FRAME) * (i),
513 package_length - shift);
514 }
515 }
516 aircable_read(port);
517 }
518
519 /* Schedule the next read _if_ we are still open */
520 if (port->open_count) {
521 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
522 usb_rcvbulkpipe(port->serial->dev,
523 port->bulk_in_endpointAddress),
524 port->read_urb->transfer_buffer,
525 port->read_urb->transfer_buffer_length,
526 aircable_read_bulk_callback, port);
527
528 result = usb_submit_urb(urb, GFP_ATOMIC);
529 if (result)
530 dev_err(&urb->dev->dev,
531 "%s - failed resubmitting read urb, error %d\n",
532 __FUNCTION__, result);
533 }
534
535 return;
536}
537
538/* Based on ftdi_sio.c throttle */
539static void aircable_throttle(struct usb_serial_port *port)
540{
541 struct aircable_private *priv = usb_get_serial_port_data(port);
542 unsigned long flags;
543
544 dbg("%s - port %d", __FUNCTION__, port->number);
545
546 spin_lock_irqsave(&priv->rx_lock, flags);
547 priv->rx_flags |= THROTTLED;
548 spin_unlock_irqrestore(&priv->rx_lock, flags);
549}
550
551/* Based on ftdi_sio.c unthrottle */
552static void aircable_unthrottle(struct usb_serial_port *port)
553{
554 struct aircable_private *priv = usb_get_serial_port_data(port);
555 int actually_throttled;
556 unsigned long flags;
557
558 dbg("%s - port %d", __FUNCTION__, port->number);
559
560 spin_lock_irqsave(&priv->rx_lock, flags);
561 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
562 priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
563 spin_unlock_irqrestore(&priv->rx_lock, flags);
564
565 if (actually_throttled)
566 schedule_work(&priv->rx_work);
567}
568
569static struct usb_serial_driver aircable_device = {
570 .description = "aircable",
571 .id_table = id_table,
572 .num_ports = 1,
573 .attach = aircable_attach,
574 .probe = aircable_probe,
575 .shutdown = aircable_shutdown,
576 .write = aircable_write,
577 .write_room = aircable_write_room,
578 .write_bulk_callback = aircable_write_bulk_callback,
579 .read_bulk_callback = aircable_read_bulk_callback,
580 .throttle = aircable_throttle,
581 .unthrottle = aircable_unthrottle,
582};
583
584static struct usb_driver aircable_driver = {
585 .name = "aircable",
586 .probe = usb_serial_probe,
587 .disconnect = usb_serial_disconnect,
588 .id_table = id_table,
589};
590
591static int __init aircable_init (void)
592{
593 int retval;
594 retval = usb_serial_register(&aircable_device);
595 if (retval)
596 goto failed_serial_register;
597 retval = usb_register(&aircable_driver);
598 if (retval)
599 goto failed_usb_register;
600 return 0;
601
602failed_serial_register:
603 usb_serial_deregister(&aircable_device);
604failed_usb_register:
605 return retval;
606}
607
608static void __exit aircable_exit (void)
609{
610 usb_deregister(&aircable_driver);
611 usb_serial_deregister(&aircable_device);
612}
613
614MODULE_AUTHOR(DRIVER_AUTHOR);
615MODULE_DESCRIPTION(DRIVER_DESC);
616MODULE_VERSION(DRIVER_VERSION);
617MODULE_LICENSE("GPL");
618
619module_init(aircable_init);
620module_exit(aircable_exit);
621
622module_param(debug, bool, S_IRUGO | S_IWUSR);
623MODULE_PARM_DESC(debug, "Debug enabled or not");