WorkStruct: make allyesconfig
[safe/jmp/linux-2.6] / drivers / usb / serial / aircable.c
1 /*
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
51 static 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 */
81 static struct usb_device_id id_table [] = {
82         { USB_DEVICE(AIRCABLE_VID, AIRCABLE_USB_PID) },
83         { },
84 };
85 MODULE_DEVICE_TABLE(usb, id_table);
86
87
88 /* Internal Structure */
89 struct 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         struct usb_serial_port *port;   /* USB port with which associated */
96 };
97
98 /* Private methods */
99
100 /* Circular Buffer Methods, code from ti_usb_3410_5052 used */
101 /*
102  * serial_buf_clear
103  *
104  * Clear out all data in the circular buffer.
105  */
106 static void serial_buf_clear(struct circ_buf *cb)
107 {
108         cb->head = cb->tail = 0;
109 }
110
111 /*
112  * serial_buf_alloc
113  *
114  * Allocate a circular buffer and all associated memory.
115  */
116 static 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  * serial_buf_free
133  *
134  * Free the buffer and all associated memory.
135  */
136 static void serial_buf_free(struct circ_buf *cb)
137 {
138         kfree(cb->buf);
139         kfree(cb);
140 }
141
142 /*
143  * serial_buf_data_avail
144  *
145  * Return the number of bytes of data available in the circular
146  * buffer.
147  */
148 static 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  * serial_buf_put
155  *
156  * Copy data data from a user buffer and put it into the circular buffer.
157  * Restrict to the amount of space available.
158  *
159  * Return the number of bytes copied.
160  */
161 static 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  * serial_buf_get
181  *
182  * Get data from the circular buffer and copy to the given buffer.
183  * Restrict to the amount of data available.
184  *
185  * Return the number of bytes copied.
186  */
187 static 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 /* End of circula buffer methods */
206
207 static 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         dbg("%s - port %d", __FUNCTION__, port->number);
213         if (port->write_urb_busy)
214                 return;
215
216         count = min(serial_buf_data_avail(priv->tx_buf), MAX_HCI_FRAMESIZE);
217         if (count == 0)
218                 return;
219
220         buf = kzalloc(count + HCI_HEADER_LENGTH, GFP_ATOMIC);
221         if (!buf) {
222                 err("%s- kzalloc(%d) failed.", __FUNCTION__,
223                     count + HCI_HEADER_LENGTH);
224                 return;
225         }
226
227         buf[0] = TX_HEADER_0;
228         buf[1] = TX_HEADER_1;
229         buf[2] = (unsigned char)count;
230         buf[3] = (unsigned char)(count >> 8);
231         serial_buf_get(priv->tx_buf,buf + HCI_HEADER_LENGTH, MAX_HCI_FRAMESIZE);
232
233         memcpy(port->write_urb->transfer_buffer, buf,
234                count + HCI_HEADER_LENGTH);
235
236         kfree(buf);
237         port->write_urb_busy = 1;
238         usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
239                               count + HCI_HEADER_LENGTH,
240                               port->write_urb->transfer_buffer);
241         port->write_urb->transfer_buffer_length = count + HCI_HEADER_LENGTH;
242         port->write_urb->dev = port->serial->dev;
243         result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
244
245         if (result) {
246                 dev_err(&port->dev,
247                         "%s - failed submitting write urb, error %d\n",
248                         __FUNCTION__, result);
249                 port->write_urb_busy = 0;
250         }
251
252         schedule_work(&port->work);
253 }
254
255 static void aircable_read(struct work_struct *work)
256 {
257         struct aircable_private *priv =
258                 container_of(work, struct aircable_private, rx_work);
259         struct usb_serial_port *port = priv->port;
260         struct tty_struct *tty;
261         unsigned char *data;
262         int count;
263         if (priv->rx_flags & THROTTLED){
264                 if (priv->rx_flags & ACTUALLY_THROTTLED)
265                         schedule_work(&priv->rx_work);
266                 return;
267         }
268
269         /* By now I will flush data to the tty in packages of no more than
270          * 64 bytes, to ensure I do not get throttled.
271          * Ask USB mailing list for better aproach.
272          */
273         tty = port->tty;
274
275         if (!tty)
276                 schedule_work(&priv->rx_work);
277
278         count = min(64, serial_buf_data_avail(priv->rx_buf));
279
280         if (count <= 0)
281                 return; //We have finished sending everything.
282
283         tty_prepare_flip_string(tty, &data, count);
284         if (!data){
285                 err("%s- kzalloc(%d) failed.", __FUNCTION__, count);
286                 return;
287         }
288
289         serial_buf_get(priv->rx_buf, data, count);
290
291         tty_flip_buffer_push(tty);
292
293         if (serial_buf_data_avail(priv->rx_buf))
294                 schedule_work(&priv->rx_work);
295
296         return;
297 }
298 /* End of private methods */
299
300 static int aircable_probe(struct usb_serial *serial,
301                           const struct usb_device_id *id)
302 {
303         struct usb_host_interface *iface_desc = serial->interface->cur_altsetting;
304         struct usb_endpoint_descriptor *endpoint;
305         int num_bulk_out=0;
306         int i;
307
308         for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
309                 endpoint = &iface_desc->endpoint[i].desc;
310                 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
311                         ((endpoint->bmAttributes & 3) == 0x02)) {
312                         /* we found our bulk out endpoint */
313                         dbg("found bulk out on endpoint %d", i);
314                         ++num_bulk_out;
315                 }
316         }
317
318         if (num_bulk_out == 0) {
319                 dbg("Invalid interface, discarding");
320                 return -ENODEV;
321         }
322
323         return 0;
324 }
325
326 static int aircable_attach (struct usb_serial *serial)
327 {
328         struct usb_serial_port *port = serial->port[0];
329         struct aircable_private *priv;
330
331         priv = kzalloc(sizeof(struct aircable_private), GFP_KERNEL);
332         if (!priv){
333                 err("%s- kmalloc(%Zd) failed.", __FUNCTION__,
334                         sizeof(struct aircable_private));
335                 return -ENOMEM;
336         }
337
338         /* Allocation of Circular Buffers */
339         priv->tx_buf = serial_buf_alloc();
340         if (priv->tx_buf == NULL) {
341                 kfree(priv);
342                 return -ENOMEM;
343         }
344
345         priv->rx_buf = serial_buf_alloc();
346         if (priv->rx_buf == NULL) {
347                 kfree(priv->tx_buf);
348                 kfree(priv);
349                 return -ENOMEM;
350         }
351
352         priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
353         priv->port = port;
354         INIT_WORK(&priv->rx_work, aircable_read);
355
356         usb_set_serial_port_data(serial->port[0], priv);
357
358         return 0;
359 }
360
361 static void aircable_shutdown(struct usb_serial *serial)
362 {
363
364         struct usb_serial_port *port = serial->port[0];
365         struct aircable_private *priv = usb_get_serial_port_data(port);
366
367         dbg("%s", __FUNCTION__);
368
369         if (priv) {
370                 serial_buf_free(priv->tx_buf);
371                 serial_buf_free(priv->rx_buf);
372                 usb_set_serial_port_data(port, NULL);
373                 kfree(priv);
374         }
375 }
376
377 static int aircable_write_room(struct usb_serial_port *port)
378 {
379         struct aircable_private *priv = usb_get_serial_port_data(port);
380         return serial_buf_data_avail(priv->tx_buf);
381 }
382
383 static int aircable_write(struct usb_serial_port *port,
384                           const unsigned char *source, int count)
385 {
386         struct aircable_private *priv = usb_get_serial_port_data(port);
387         int temp;
388
389         dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
390
391         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, source);
392
393         if (!count){
394                 dbg("%s - write request of 0 bytes", __FUNCTION__);
395                 return count;
396         }
397
398         temp = serial_buf_put(priv->tx_buf, source, count);
399
400         aircable_send(port);
401
402         if (count > AIRCABLE_BUF_SIZE)
403                 count = AIRCABLE_BUF_SIZE;
404
405         return count;
406
407 }
408
409 static void aircable_write_bulk_callback(struct urb *urb)
410 {
411         struct usb_serial_port *port = urb->context;
412         int result;
413
414         dbg("%s - urb->status: %d", __FUNCTION__ , urb->status);
415
416         /* This has been taken from cypress_m8.c cypress_write_int_callback */
417         switch (urb->status) {
418                 case 0:
419                         /* success */
420                         break;
421                 case -ECONNRESET:
422                 case -ENOENT:
423                 case -ESHUTDOWN:
424                         /* this urb is terminated, clean up */
425                         dbg("%s - urb shutting down with status: %d",
426                             __FUNCTION__, urb->status);
427                         port->write_urb_busy = 0;
428                         return;
429                 default:
430                         /* error in the urb, so we have to resubmit it */
431                         dbg("%s - Overflow in write", __FUNCTION__);
432                         dbg("%s - nonzero write bulk status received: %d",
433                             __FUNCTION__, urb->status);
434                         port->write_urb->transfer_buffer_length = 1;
435                         port->write_urb->dev = port->serial->dev;
436                         result = usb_submit_urb(port->write_urb, GFP_KERNEL);
437                         if (result)
438                                 dev_err(&urb->dev->dev,
439                                         "%s - failed resubmitting write urb, error %d\n",
440                                         __FUNCTION__, result);
441                         else
442                                 return;
443         }
444
445         port->write_urb_busy = 0;
446
447         aircable_send(port);
448 }
449
450 static void aircable_read_bulk_callback(struct urb *urb)
451 {
452         struct usb_serial_port *port = urb->context;
453         struct aircable_private *priv = usb_get_serial_port_data(port);
454         struct tty_struct *tty;
455         unsigned long no_packages, remaining, package_length, i;
456         int result, shift = 0;
457         unsigned char *temp;
458
459         dbg("%s - port %d", __FUNCTION__, port->number);
460
461         if (urb->status) {
462                 dbg("%s - urb->status = %d", __FUNCTION__, urb->status);
463                 if (!port->open_count) {
464                         dbg("%s - port is closed, exiting.", __FUNCTION__);
465                         return;
466                 }
467                 if (urb->status == -EPROTO) {
468                         dbg("%s - caught -EPROTO, resubmitting the urb",
469                             __FUNCTION__);
470                         usb_fill_bulk_urb(port->read_urb, port->serial->dev,
471                                           usb_rcvbulkpipe(port->serial->dev,
472                                                           port->bulk_in_endpointAddress),
473                                           port->read_urb->transfer_buffer,
474                                           port->read_urb->transfer_buffer_length,
475                                           aircable_read_bulk_callback, port);
476
477                         result = usb_submit_urb(urb, GFP_ATOMIC);
478                         if (result)
479                                 dev_err(&urb->dev->dev,
480                                         "%s - failed resubmitting read urb, error %d\n",
481                                         __FUNCTION__, result);
482                         return;
483                 }
484                 dbg("%s - unable to handle the error, exiting.", __FUNCTION__);
485                 return;
486         }
487
488         usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
489                                 urb->actual_length,urb->transfer_buffer);
490
491         tty = port->tty;
492         if (tty && urb->actual_length) {
493                 if (urb->actual_length <= 2) {
494                         /* This is an incomplete package */
495                         serial_buf_put(priv->rx_buf, urb->transfer_buffer,
496                                        urb->actual_length);
497                 } else {
498                         temp = urb->transfer_buffer;
499                         if (temp[0] == RX_HEADER_0)
500                                 shift = HCI_HEADER_LENGTH;
501
502                         remaining = urb->actual_length;
503                         no_packages = urb->actual_length / (HCI_COMPLETE_FRAME);
504
505                         if (urb->actual_length % HCI_COMPLETE_FRAME != 0)
506                                 no_packages+=1;
507
508                         for (i = 0; i < no_packages ;i++) {
509                                 if (remaining > (HCI_COMPLETE_FRAME))
510                                         package_length = HCI_COMPLETE_FRAME;
511                                 else
512                                         package_length = remaining;
513                                 remaining -= package_length;
514
515                                 serial_buf_put(priv->rx_buf,
516                                         urb->transfer_buffer + shift +
517                                         (HCI_COMPLETE_FRAME) * (i),
518                                         package_length - shift);
519                         }
520                 }
521                 aircable_read(&priv->rx_work);
522         }
523
524         /* Schedule the next read _if_ we are still open */
525         if (port->open_count) {
526                 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
527                                   usb_rcvbulkpipe(port->serial->dev,
528                                                   port->bulk_in_endpointAddress),
529                                   port->read_urb->transfer_buffer,
530                                   port->read_urb->transfer_buffer_length,
531                                   aircable_read_bulk_callback, port);
532
533                 result = usb_submit_urb(urb, GFP_ATOMIC);
534                 if (result)
535                         dev_err(&urb->dev->dev,
536                                 "%s - failed resubmitting read urb, error %d\n",
537                                 __FUNCTION__, result);
538         }
539
540         return;
541 }
542
543 /* Based on ftdi_sio.c throttle */
544 static void aircable_throttle(struct usb_serial_port *port)
545 {
546         struct aircable_private *priv = usb_get_serial_port_data(port);
547         unsigned long flags;
548
549         dbg("%s - port %d", __FUNCTION__, port->number);
550
551         spin_lock_irqsave(&priv->rx_lock, flags);
552         priv->rx_flags |= THROTTLED;
553         spin_unlock_irqrestore(&priv->rx_lock, flags);
554 }
555
556 /* Based on ftdi_sio.c unthrottle */
557 static void aircable_unthrottle(struct usb_serial_port *port)
558 {
559         struct aircable_private *priv = usb_get_serial_port_data(port);
560         int actually_throttled;
561         unsigned long flags;
562
563         dbg("%s - port %d", __FUNCTION__, port->number);
564
565         spin_lock_irqsave(&priv->rx_lock, flags);
566         actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
567         priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
568         spin_unlock_irqrestore(&priv->rx_lock, flags);
569
570         if (actually_throttled)
571                 schedule_work(&priv->rx_work);
572 }
573
574 static struct usb_serial_driver aircable_device = {
575         .description =          "aircable",
576         .id_table =             id_table,
577         .num_ports =            1,
578         .attach =               aircable_attach,
579         .probe =                aircable_probe,
580         .shutdown =             aircable_shutdown,
581         .write =                aircable_write,
582         .write_room =           aircable_write_room,
583         .write_bulk_callback =  aircable_write_bulk_callback,
584         .read_bulk_callback =   aircable_read_bulk_callback,
585         .throttle =             aircable_throttle,
586         .unthrottle =           aircable_unthrottle,
587 };
588
589 static struct usb_driver aircable_driver = {
590         .name =         "aircable",
591         .probe =        usb_serial_probe,
592         .disconnect =   usb_serial_disconnect,
593         .id_table =     id_table,
594 };
595
596 static int __init aircable_init (void)
597 {
598         int retval;
599         retval = usb_serial_register(&aircable_device);
600         if (retval)
601                 goto failed_serial_register;
602         retval = usb_register(&aircable_driver);
603         if (retval)
604                 goto failed_usb_register;
605         return 0;
606
607 failed_serial_register:
608         usb_serial_deregister(&aircable_device);
609 failed_usb_register:
610         return retval;
611 }
612
613 static void __exit aircable_exit (void)
614 {
615         usb_deregister(&aircable_driver);
616         usb_serial_deregister(&aircable_device);
617 }
618
619 MODULE_AUTHOR(DRIVER_AUTHOR);
620 MODULE_DESCRIPTION(DRIVER_DESC);
621 MODULE_VERSION(DRIVER_VERSION);
622 MODULE_LICENSE("GPL");
623
624 module_init(aircable_init);
625 module_exit(aircable_exit);
626
627 module_param(debug, bool, S_IRUGO | S_IWUSR);
628 MODULE_PARM_DESC(debug, "Debug enabled or not");