USB: serial: reimplement generic fifo-based writes
[pandora-kernel.git] / drivers / usb / serial / generic.c
1 /*
2  * USB Serial Converter Generic functions
3  *
4  * Copyright (C) 2010 Johan Hovold (jhovold@gmail.com)
5  * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License version
9  *      2 as published by the Free Software Foundation.
10  *
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/sysrq.h>
17 #include <linux/tty.h>
18 #include <linux/tty_flip.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/usb.h>
22 #include <linux/usb/serial.h>
23 #include <linux/uaccess.h>
24 #include <linux/kfifo.h>
25 #include <linux/serial.h>
26
27 static int debug;
28
29 #define MAX_TX_URBS             40
30
31 #ifdef CONFIG_USB_SERIAL_GENERIC
32
33 static int generic_probe(struct usb_interface *interface,
34                          const struct usb_device_id *id);
35
36 static __u16 vendor  = 0x05f9;
37 static __u16 product = 0xffff;
38
39 module_param(vendor, ushort, 0);
40 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
41
42 module_param(product, ushort, 0);
43 MODULE_PARM_DESC(product, "User specified USB idProduct");
44
45 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
46
47 /* we want to look at all devices, as the vendor/product id can change
48  * depending on the command line argument */
49 static const struct usb_device_id generic_serial_ids[] = {
50         {.driver_info = 42},
51         {}
52 };
53
54 static struct usb_driver generic_driver = {
55         .name =         "usbserial_generic",
56         .probe =        generic_probe,
57         .disconnect =   usb_serial_disconnect,
58         .id_table =     generic_serial_ids,
59         .no_dynamic_id =        1,
60 };
61
62 /* All of the device info needed for the Generic Serial Converter */
63 struct usb_serial_driver usb_serial_generic_device = {
64         .driver = {
65                 .owner =        THIS_MODULE,
66                 .name =         "generic",
67         },
68         .id_table =             generic_device_ids,
69         .usb_driver =           &generic_driver,
70         .num_ports =            1,
71         .disconnect =           usb_serial_generic_disconnect,
72         .release =              usb_serial_generic_release,
73         .throttle =             usb_serial_generic_throttle,
74         .unthrottle =           usb_serial_generic_unthrottle,
75         .resume =               usb_serial_generic_resume,
76 };
77
78 static int generic_probe(struct usb_interface *interface,
79                                const struct usb_device_id *id)
80 {
81         const struct usb_device_id *id_pattern;
82
83         id_pattern = usb_match_id(interface, generic_device_ids);
84         if (id_pattern != NULL)
85                 return usb_serial_probe(interface, id);
86         return -ENODEV;
87 }
88 #endif
89
90 int usb_serial_generic_register(int _debug)
91 {
92         int retval = 0;
93
94         debug = _debug;
95 #ifdef CONFIG_USB_SERIAL_GENERIC
96         generic_device_ids[0].idVendor = vendor;
97         generic_device_ids[0].idProduct = product;
98         generic_device_ids[0].match_flags =
99                 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
100
101         /* register our generic driver with ourselves */
102         retval = usb_serial_register(&usb_serial_generic_device);
103         if (retval)
104                 goto exit;
105         retval = usb_register(&generic_driver);
106         if (retval)
107                 usb_serial_deregister(&usb_serial_generic_device);
108 exit:
109 #endif
110         return retval;
111 }
112
113 void usb_serial_generic_deregister(void)
114 {
115 #ifdef CONFIG_USB_SERIAL_GENERIC
116         /* remove our generic driver */
117         usb_deregister(&generic_driver);
118         usb_serial_deregister(&usb_serial_generic_device);
119 #endif
120 }
121
122 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
123 {
124         int result = 0;
125         unsigned long flags;
126
127         dbg("%s - port %d", __func__, port->number);
128
129         /* clear the throttle flags */
130         spin_lock_irqsave(&port->lock, flags);
131         port->throttled = 0;
132         port->throttle_req = 0;
133         spin_unlock_irqrestore(&port->lock, flags);
134
135         /* if we have a bulk endpoint, start reading from it */
136         if (port->bulk_in_size)
137                 result = usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
138
139         return result;
140 }
141 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
142
143 static void generic_cleanup(struct usb_serial_port *port)
144 {
145         struct usb_serial *serial = port->serial;
146         unsigned long flags;
147         int i;
148
149         dbg("%s - port %d", __func__, port->number);
150
151         if (serial->dev) {
152                 /* shutdown any bulk transfers that might be going on */
153                 if (port->bulk_out_size) {
154                         usb_kill_urb(port->write_urb);
155                         for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
156                                 usb_kill_urb(port->write_urbs[i]);
157
158                         spin_lock_irqsave(&port->lock, flags);
159                         kfifo_reset_out(&port->write_fifo);
160                         spin_unlock_irqrestore(&port->lock, flags);
161                 }
162                 if (port->bulk_in_size)
163                         usb_kill_urb(port->read_urb);
164         }
165 }
166
167 void usb_serial_generic_close(struct usb_serial_port *port)
168 {
169         dbg("%s - port %d", __func__, port->number);
170         generic_cleanup(port);
171 }
172 EXPORT_SYMBOL_GPL(usb_serial_generic_close);
173
174 int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
175                 void **dest, size_t size, const void *src, size_t count)
176 {
177         if (!*dest) {
178                 size = count;
179                 *dest = kmalloc(count, GFP_ATOMIC);
180                 if (!*dest) {
181                         dev_err(&port->dev, "%s - could not allocate buffer\n",
182                                         __func__);
183                         return -ENOMEM;
184                 }
185         }
186         if (src) {
187                 count = size;
188                 memcpy(*dest, src, size);
189         } else {
190                 count = kfifo_out_locked(&port->write_fifo, *dest, size,
191                                                                 &port->lock);
192         }
193         return count;
194 }
195 EXPORT_SYMBOL_GPL(usb_serial_generic_prepare_write_buffer);
196
197 static int usb_serial_multi_urb_write(struct tty_struct *tty,
198         struct usb_serial_port *port, const unsigned char *buf, int count)
199 {
200         unsigned long flags;
201         struct urb *urb;
202         void *buffer;
203         int status;
204
205         spin_lock_irqsave(&port->lock, flags);
206         if (port->tx_urbs == MAX_TX_URBS) {
207                 spin_unlock_irqrestore(&port->lock, flags);
208                 dbg("%s - write limit hit", __func__);
209                 return 0;
210         }
211         port->tx_urbs++;
212         spin_unlock_irqrestore(&port->lock, flags);
213
214         urb = usb_alloc_urb(0, GFP_ATOMIC);
215         if (!urb) {
216                 dev_err(&port->dev, "%s - no free urbs available\n", __func__);
217                 status = -ENOMEM;
218                 goto err_urb;
219         }
220
221         buffer = NULL;
222         count = min_t(int, count, PAGE_SIZE);
223         count = port->serial->type->prepare_write_buffer(port, &buffer, 0,
224                                                                 buf, count);
225         if (count < 0) {
226                 status = count;
227                 goto err_buf;
228         }
229         usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
230         usb_fill_bulk_urb(urb, port->serial->dev,
231                         usb_sndbulkpipe(port->serial->dev,
232                                         port->bulk_out_endpointAddress),
233                         buffer, count,
234                         port->serial->type->write_bulk_callback, port);
235
236         status = usb_submit_urb(urb, GFP_ATOMIC);
237         if (status) {
238                 dev_err(&port->dev, "%s - error submitting urb: %d\n",
239                                 __func__, status);
240                 goto err;
241         }
242         spin_lock_irqsave(&port->lock, flags);
243         port->tx_bytes += urb->transfer_buffer_length;
244         spin_unlock_irqrestore(&port->lock, flags);
245
246         usb_free_urb(urb);
247
248         return count;
249 err:
250         kfree(buffer);
251 err_buf:
252         usb_free_urb(urb);
253 err_urb:
254         spin_lock_irqsave(&port->lock, flags);
255         port->tx_urbs--;
256         spin_unlock_irqrestore(&port->lock, flags);
257
258         return status;
259 }
260
261 /**
262  * usb_serial_generic_write_start - kick off an URB write
263  * @port:       Pointer to the &struct usb_serial_port data
264  *
265  * Returns zero on success, or a negative errno value
266  */
267 static int usb_serial_generic_write_start(struct usb_serial_port *port)
268 {
269         struct urb *urb;
270         int count, result;
271         unsigned long flags;
272         int i;
273
274         if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
275                 return 0;
276 retry:
277         spin_lock_irqsave(&port->lock, flags);
278         if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
279                 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
280                 spin_unlock_irqrestore(&port->lock, flags);
281                 return 0;
282         }
283         i = (int)find_first_bit(&port->write_urbs_free,
284                                                 ARRAY_SIZE(port->write_urbs));
285         spin_unlock_irqrestore(&port->lock, flags);
286
287         urb = port->write_urbs[i];
288         count = port->serial->type->prepare_write_buffer(port,
289                                                 &urb->transfer_buffer,
290                                                 port->bulk_out_size, NULL, 0);
291         urb->transfer_buffer_length = count;
292         usb_serial_debug_data(debug, &port->dev, __func__, count,
293                                                 urb->transfer_buffer);
294         result = usb_submit_urb(urb, GFP_ATOMIC);
295         if (result) {
296                 dev_err(&port->dev, "%s - error submitting urb: %d\n",
297                                                 __func__, result);
298                 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
299                 return result;
300         }
301         clear_bit(i, &port->write_urbs_free);
302
303         spin_lock_irqsave(&port->lock, flags);
304         port->tx_bytes += count;
305         spin_unlock_irqrestore(&port->lock, flags);
306
307         /* Try sending off another urb, unless in irq context (in which case
308          * there will be no free urb). */
309         if (!in_irq())
310                 goto retry;
311
312         clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
313
314         return 0;
315 }
316
317 /**
318  * usb_serial_generic_write - generic write function for serial USB devices
319  * @tty:        Pointer to &struct tty_struct for the device
320  * @port:       Pointer to the &usb_serial_port structure for the device
321  * @buf:        Pointer to the data to write
322  * @count:      Number of bytes to write
323  *
324  * Returns the number of characters actually written, which may be anything
325  * from zero to @count. If an error occurs, it returns the negative errno
326  * value.
327  */
328 int usb_serial_generic_write(struct tty_struct *tty,
329         struct usb_serial_port *port, const unsigned char *buf, int count)
330 {
331         struct usb_serial *serial = port->serial;
332         int result;
333
334         dbg("%s - port %d", __func__, port->number);
335
336         /* only do something if we have a bulk out endpoint */
337         if (!port->bulk_out_size)
338                 return -ENODEV;
339
340         if (!count)
341                 return 0;
342
343         if (serial->type->multi_urb_write)
344                 return usb_serial_multi_urb_write(tty, port, buf, count);
345
346         count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
347         result = usb_serial_generic_write_start(port);
348
349         if (result >= 0)
350                 result = count;
351
352         return result;
353 }
354 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
355
356 int usb_serial_generic_write_room(struct tty_struct *tty)
357 {
358         struct usb_serial_port *port = tty->driver_data;
359         struct usb_serial *serial = port->serial;
360         unsigned long flags;
361         int room;
362
363         dbg("%s - port %d", __func__, port->number);
364
365         if (!port->bulk_out_size)
366                 return 0;
367
368         spin_lock_irqsave(&port->lock, flags);
369         if (serial->type->multi_urb_write)
370                 room = (MAX_TX_URBS - port->tx_urbs) * PAGE_SIZE;
371         else
372                 room = kfifo_avail(&port->write_fifo);
373         spin_unlock_irqrestore(&port->lock, flags);
374
375         dbg("%s - returns %d", __func__, room);
376         return room;
377 }
378
379 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
380 {
381         struct usb_serial_port *port = tty->driver_data;
382         struct usb_serial *serial = port->serial;
383         unsigned long flags;
384         int chars;
385
386         dbg("%s - port %d", __func__, port->number);
387
388         if (!port->bulk_out_size)
389                 return 0;
390
391         spin_lock_irqsave(&port->lock, flags);
392         if (serial->type->multi_urb_write)
393                 chars = port->tx_bytes;
394         else
395                 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
396         spin_unlock_irqrestore(&port->lock, flags);
397
398         dbg("%s - returns %d", __func__, chars);
399         return chars;
400 }
401
402 int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
403                                         gfp_t mem_flags)
404 {
405         int result;
406
407         result = usb_submit_urb(port->read_urb, mem_flags);
408         if (result && result != -EPERM) {
409                 dev_err(&port->dev, "%s - error submitting urb: %d\n",
410                                                         __func__, result);
411         }
412         return result;
413 }
414 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urb);
415
416 void usb_serial_generic_process_read_urb(struct urb *urb)
417 {
418         struct usb_serial_port *port = urb->context;
419         struct tty_struct *tty;
420         char *ch = (char *)urb->transfer_buffer;
421         int i;
422
423         tty = tty_port_tty_get(&port->port);
424         if (!tty)
425                 return;
426
427         /* The per character mucking around with sysrq path it too slow for
428            stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
429            where the USB serial is not a console anyway */
430         if (!port->port.console || !port->sysrq)
431                 tty_insert_flip_string(tty, ch, urb->actual_length);
432         else {
433                 for (i = 0; i < urb->actual_length; i++, ch++) {
434                         if (!usb_serial_handle_sysrq_char(tty, port, *ch))
435                                 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
436                 }
437         }
438         tty_flip_buffer_push(tty);
439         tty_kref_put(tty);
440 }
441 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
442
443 void usb_serial_generic_read_bulk_callback(struct urb *urb)
444 {
445         struct usb_serial_port *port = urb->context;
446         unsigned char *data = urb->transfer_buffer;
447         int status = urb->status;
448         unsigned long flags;
449
450         dbg("%s - port %d", __func__, port->number);
451
452         if (unlikely(status != 0)) {
453                 dbg("%s - nonzero read bulk status received: %d",
454                     __func__, status);
455                 return;
456         }
457
458         usb_serial_debug_data(debug, &port->dev, __func__,
459                                                 urb->actual_length, data);
460         port->serial->type->process_read_urb(urb);
461
462         /* Throttle the device if requested by tty */
463         spin_lock_irqsave(&port->lock, flags);
464         port->throttled = port->throttle_req;
465         if (!port->throttled) {
466                 spin_unlock_irqrestore(&port->lock, flags);
467                 usb_serial_generic_submit_read_urb(port, GFP_ATOMIC);
468         } else
469                 spin_unlock_irqrestore(&port->lock, flags);
470 }
471 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
472
473 void usb_serial_generic_write_bulk_callback(struct urb *urb)
474 {
475         unsigned long flags;
476         struct usb_serial_port *port = urb->context;
477         int status = urb->status;
478         int i;
479
480         dbg("%s - port %d", __func__, port->number);
481
482         if (port->serial->type->multi_urb_write) {
483                 kfree(urb->transfer_buffer);
484
485                 spin_lock_irqsave(&port->lock, flags);
486                 port->tx_bytes -= urb->transfer_buffer_length;
487                 port->tx_urbs--;
488                 spin_unlock_irqrestore(&port->lock, flags);
489         } else {
490                 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
491                         if (port->write_urbs[i] == urb)
492                                 break;
493
494                 spin_lock_irqsave(&port->lock, flags);
495                 port->tx_bytes -= urb->transfer_buffer_length;
496                 set_bit(i, &port->write_urbs_free);
497                 spin_unlock_irqrestore(&port->lock, flags);
498
499                 if (status) {
500                         spin_lock_irqsave(&port->lock, flags);
501                         kfifo_reset_out(&port->write_fifo);
502                         spin_unlock_irqrestore(&port->lock, flags);
503                 } else {
504                         usb_serial_generic_write_start(port);
505                 }
506         }
507
508         if (status)
509                 dbg("%s - non-zero urb status: %d", __func__, status);
510
511         usb_serial_port_softint(port);
512 }
513 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
514
515 void usb_serial_generic_throttle(struct tty_struct *tty)
516 {
517         struct usb_serial_port *port = tty->driver_data;
518         unsigned long flags;
519
520         dbg("%s - port %d", __func__, port->number);
521
522         /* Set the throttle request flag. It will be picked up
523          * by usb_serial_generic_read_bulk_callback(). */
524         spin_lock_irqsave(&port->lock, flags);
525         port->throttle_req = 1;
526         spin_unlock_irqrestore(&port->lock, flags);
527 }
528 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
529
530 void usb_serial_generic_unthrottle(struct tty_struct *tty)
531 {
532         struct usb_serial_port *port = tty->driver_data;
533         int was_throttled;
534
535         dbg("%s - port %d", __func__, port->number);
536
537         /* Clear the throttle flags */
538         spin_lock_irq(&port->lock);
539         was_throttled = port->throttled;
540         port->throttled = port->throttle_req = 0;
541         spin_unlock_irq(&port->lock);
542
543         if (was_throttled)
544                 usb_serial_generic_submit_read_urb(port, GFP_KERNEL);
545 }
546 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
547
548 #ifdef CONFIG_MAGIC_SYSRQ
549 int usb_serial_handle_sysrq_char(struct tty_struct *tty,
550                         struct usb_serial_port *port, unsigned int ch)
551 {
552         if (port->sysrq && port->port.console) {
553                 if (ch && time_before(jiffies, port->sysrq)) {
554                         handle_sysrq(ch, tty);
555                         port->sysrq = 0;
556                         return 1;
557                 }
558                 port->sysrq = 0;
559         }
560         return 0;
561 }
562 #else
563 int usb_serial_handle_sysrq_char(struct tty_struct *tty,
564                         struct usb_serial_port *port, unsigned int ch)
565 {
566         return 0;
567 }
568 #endif
569 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
570
571 int usb_serial_handle_break(struct usb_serial_port *port)
572 {
573         if (!port->sysrq) {
574                 port->sysrq = jiffies + HZ*5;
575                 return 1;
576         }
577         port->sysrq = 0;
578         return 0;
579 }
580 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
581
582 int usb_serial_generic_resume(struct usb_serial *serial)
583 {
584         struct usb_serial_port *port;
585         int i, c = 0, r;
586
587         for (i = 0; i < serial->num_ports; i++) {
588                 port = serial->port[i];
589                 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
590                         continue;
591
592                 if (port->read_urb) {
593                         r = usb_submit_urb(port->read_urb, GFP_NOIO);
594                         if (r < 0)
595                                 c++;
596                 }
597
598                 if (port->bulk_out_size) {
599                         r = usb_serial_generic_write_start(port);
600                         if (r < 0)
601                                 c++;
602                 }
603         }
604
605         return c ? -EIO : 0;
606 }
607 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
608
609 void usb_serial_generic_disconnect(struct usb_serial *serial)
610 {
611         int i;
612
613         dbg("%s", __func__);
614
615         /* stop reads and writes on all ports */
616         for (i = 0; i < serial->num_ports; ++i)
617                 generic_cleanup(serial->port[i]);
618 }
619
620 void usb_serial_generic_release(struct usb_serial *serial)
621 {
622         dbg("%s", __func__);
623 }