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