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