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