Merge branch 'linux_next' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[cascardo/linux.git] / drivers / usb / serial / generic.c
1 /*
2  * USB Serial Converter Generic functions
3  *
4  * Copyright (C) 2010 - 2011 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 #ifdef CONFIG_USB_SERIAL_GENERIC
30
31 static int generic_probe(struct usb_interface *interface,
32                          const struct usb_device_id *id);
33
34 static __u16 vendor  = 0x05f9;
35 static __u16 product = 0xffff;
36
37 module_param(vendor, ushort, 0);
38 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
39
40 module_param(product, ushort, 0);
41 MODULE_PARM_DESC(product, "User specified USB idProduct");
42
43 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
44
45 /* we want to look at all devices, as the vendor/product id can change
46  * depending on the command line argument */
47 static const struct usb_device_id generic_serial_ids[] = {
48         {.driver_info = 42},
49         {}
50 };
51
52 static struct usb_driver generic_driver = {
53         .name =         "usbserial_generic",
54         .probe =        generic_probe,
55         .disconnect =   usb_serial_disconnect,
56         .id_table =     generic_serial_ids,
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         .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 struct usb_serial_driver * const serial_drivers[] = {
75         &usb_serial_generic_device, NULL
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_drivers(&generic_driver, serial_drivers);
103 #endif
104         return retval;
105 }
106
107 void usb_serial_generic_deregister(void)
108 {
109 #ifdef CONFIG_USB_SERIAL_GENERIC
110         /* remove our generic driver */
111         usb_serial_deregister_drivers(&generic_driver, serial_drivers);
112 #endif
113 }
114
115 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
116 {
117         int result = 0;
118         unsigned long flags;
119
120         dbg("%s - port %d", __func__, port->number);
121
122         /* clear the throttle flags */
123         spin_lock_irqsave(&port->lock, flags);
124         port->throttled = 0;
125         port->throttle_req = 0;
126         spin_unlock_irqrestore(&port->lock, flags);
127
128         /* if we have a bulk endpoint, start reading from it */
129         if (port->bulk_in_size)
130                 result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
131
132         return result;
133 }
134 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
135
136 static void generic_cleanup(struct usb_serial_port *port)
137 {
138         struct usb_serial *serial = port->serial;
139         unsigned long flags;
140         int i;
141
142         dbg("%s - port %d", __func__, port->number);
143
144         if (serial->dev) {
145                 /* shutdown any bulk transfers that might be going on */
146                 if (port->bulk_out_size) {
147                         usb_kill_urb(port->write_urb);
148                         for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
149                                 usb_kill_urb(port->write_urbs[i]);
150
151                         spin_lock_irqsave(&port->lock, flags);
152                         kfifo_reset_out(&port->write_fifo);
153                         spin_unlock_irqrestore(&port->lock, flags);
154                 }
155                 if (port->bulk_in_size) {
156                         for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
157                                 usb_kill_urb(port->read_urbs[i]);
158                 }
159         }
160 }
161
162 void usb_serial_generic_close(struct usb_serial_port *port)
163 {
164         dbg("%s - port %d", __func__, port->number);
165         generic_cleanup(port);
166 }
167 EXPORT_SYMBOL_GPL(usb_serial_generic_close);
168
169 int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
170                                                 void *dest, size_t size)
171 {
172         return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
173 }
174
175 /**
176  * usb_serial_generic_write_start - kick off an URB write
177  * @port:       Pointer to the &struct usb_serial_port data
178  *
179  * Returns zero on success, or a negative errno value
180  */
181 static int usb_serial_generic_write_start(struct usb_serial_port *port)
182 {
183         struct urb *urb;
184         int count, result;
185         unsigned long flags;
186         int i;
187
188         if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
189                 return 0;
190 retry:
191         spin_lock_irqsave(&port->lock, flags);
192         if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
193                 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
194                 spin_unlock_irqrestore(&port->lock, flags);
195                 return 0;
196         }
197         i = (int)find_first_bit(&port->write_urbs_free,
198                                                 ARRAY_SIZE(port->write_urbs));
199         spin_unlock_irqrestore(&port->lock, flags);
200
201         urb = port->write_urbs[i];
202         count = port->serial->type->prepare_write_buffer(port,
203                                                 urb->transfer_buffer,
204                                                 port->bulk_out_size);
205         urb->transfer_buffer_length = count;
206         usb_serial_debug_data(debug, &port->dev, __func__, count,
207                                                 urb->transfer_buffer);
208         spin_lock_irqsave(&port->lock, flags);
209         port->tx_bytes += count;
210         spin_unlock_irqrestore(&port->lock, flags);
211
212         clear_bit(i, &port->write_urbs_free);
213         result = usb_submit_urb(urb, GFP_ATOMIC);
214         if (result) {
215                 dev_err_console(port, "%s - error submitting urb: %d\n",
216                                                 __func__, result);
217                 set_bit(i, &port->write_urbs_free);
218                 spin_lock_irqsave(&port->lock, flags);
219                 port->tx_bytes -= count;
220                 spin_unlock_irqrestore(&port->lock, flags);
221
222                 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
223                 return result;
224         }
225
226         /* Try sending off another urb, unless in irq context (in which case
227          * there will be no free urb). */
228         if (!in_irq())
229                 goto retry;
230
231         clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
232
233         return 0;
234 }
235
236 /**
237  * usb_serial_generic_write - generic write function for serial USB devices
238  * @tty:        Pointer to &struct tty_struct for the device
239  * @port:       Pointer to the &usb_serial_port structure for the device
240  * @buf:        Pointer to the data to write
241  * @count:      Number of bytes to write
242  *
243  * Returns the number of characters actually written, which may be anything
244  * from zero to @count. If an error occurs, it returns the negative errno
245  * value.
246  */
247 int usb_serial_generic_write(struct tty_struct *tty,
248         struct usb_serial_port *port, const unsigned char *buf, int count)
249 {
250         int result;
251
252         dbg("%s - port %d", __func__, port->number);
253
254         /* only do something if we have a bulk out endpoint */
255         if (!port->bulk_out_size)
256                 return -ENODEV;
257
258         if (!count)
259                 return 0;
260
261         count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
262         result = usb_serial_generic_write_start(port);
263         if (result)
264                 return result;
265
266         return count;
267 }
268 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
269
270 int usb_serial_generic_write_room(struct tty_struct *tty)
271 {
272         struct usb_serial_port *port = tty->driver_data;
273         unsigned long flags;
274         int room;
275
276         dbg("%s - port %d", __func__, port->number);
277
278         if (!port->bulk_out_size)
279                 return 0;
280
281         spin_lock_irqsave(&port->lock, flags);
282         room = kfifo_avail(&port->write_fifo);
283         spin_unlock_irqrestore(&port->lock, flags);
284
285         dbg("%s - returns %d", __func__, room);
286         return room;
287 }
288
289 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
290 {
291         struct usb_serial_port *port = tty->driver_data;
292         unsigned long flags;
293         int chars;
294
295         dbg("%s - port %d", __func__, port->number);
296
297         if (!port->bulk_out_size)
298                 return 0;
299
300         spin_lock_irqsave(&port->lock, flags);
301         chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
302         spin_unlock_irqrestore(&port->lock, flags);
303
304         dbg("%s - returns %d", __func__, chars);
305         return chars;
306 }
307
308 static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
309                                                 int index, gfp_t mem_flags)
310 {
311         int res;
312
313         if (!test_and_clear_bit(index, &port->read_urbs_free))
314                 return 0;
315
316         dbg("%s - port %d, urb %d\n", __func__, port->number, index);
317
318         res = usb_submit_urb(port->read_urbs[index], mem_flags);
319         if (res) {
320                 if (res != -EPERM) {
321                         dev_err(&port->dev,
322                                         "%s - usb_submit_urb failed: %d\n",
323                                         __func__, res);
324                 }
325                 set_bit(index, &port->read_urbs_free);
326                 return res;
327         }
328
329         return 0;
330 }
331
332 int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
333                                         gfp_t mem_flags)
334 {
335         int res;
336         int i;
337
338         dbg("%s - port %d", __func__, port->number);
339
340         for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
341                 res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
342                 if (res)
343                         goto err;
344         }
345
346         return 0;
347 err:
348         for (; i >= 0; --i)
349                 usb_kill_urb(port->read_urbs[i]);
350
351         return res;
352 }
353 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
354
355 void usb_serial_generic_process_read_urb(struct urb *urb)
356 {
357         struct usb_serial_port *port = urb->context;
358         struct tty_struct *tty;
359         char *ch = (char *)urb->transfer_buffer;
360         int i;
361
362         if (!urb->actual_length)
363                 return;
364
365         tty = tty_port_tty_get(&port->port);
366         if (!tty)
367                 return;
368
369         /* The per character mucking around with sysrq path it too slow for
370            stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
371            where the USB serial is not a console anyway */
372         if (!port->port.console || !port->sysrq)
373                 tty_insert_flip_string(tty, ch, urb->actual_length);
374         else {
375                 for (i = 0; i < urb->actual_length; i++, ch++) {
376                         if (!usb_serial_handle_sysrq_char(port, *ch))
377                                 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
378                 }
379         }
380         tty_flip_buffer_push(tty);
381         tty_kref_put(tty);
382 }
383 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
384
385 void usb_serial_generic_read_bulk_callback(struct urb *urb)
386 {
387         struct usb_serial_port *port = urb->context;
388         unsigned char *data = urb->transfer_buffer;
389         unsigned long flags;
390         int i;
391
392         for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
393                 if (urb == port->read_urbs[i])
394                         break;
395         }
396         set_bit(i, &port->read_urbs_free);
397
398         dbg("%s - port %d, urb %d, len %d\n", __func__, port->number, i,
399                                                         urb->actual_length);
400         if (urb->status) {
401                 dbg("%s - non-zero urb status: %d\n", __func__, urb->status);
402                 return;
403         }
404
405         usb_serial_debug_data(debug, &port->dev, __func__,
406                                                 urb->actual_length, data);
407         port->serial->type->process_read_urb(urb);
408
409         /* Throttle the device if requested by tty */
410         spin_lock_irqsave(&port->lock, flags);
411         port->throttled = port->throttle_req;
412         if (!port->throttled) {
413                 spin_unlock_irqrestore(&port->lock, flags);
414                 usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
415         } else
416                 spin_unlock_irqrestore(&port->lock, flags);
417 }
418 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
419
420 void usb_serial_generic_write_bulk_callback(struct urb *urb)
421 {
422         unsigned long flags;
423         struct usb_serial_port *port = urb->context;
424         int status = urb->status;
425         int i;
426
427         dbg("%s - port %d", __func__, port->number);
428
429         for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
430                 if (port->write_urbs[i] == urb)
431                         break;
432
433         spin_lock_irqsave(&port->lock, flags);
434         port->tx_bytes -= urb->transfer_buffer_length;
435         set_bit(i, &port->write_urbs_free);
436         spin_unlock_irqrestore(&port->lock, flags);
437
438         if (status) {
439                 dbg("%s - non-zero urb status: %d", __func__, status);
440
441                 spin_lock_irqsave(&port->lock, flags);
442                 kfifo_reset_out(&port->write_fifo);
443                 spin_unlock_irqrestore(&port->lock, flags);
444         } else {
445                 usb_serial_generic_write_start(port);
446         }
447
448         usb_serial_port_softint(port);
449 }
450 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
451
452 void usb_serial_generic_throttle(struct tty_struct *tty)
453 {
454         struct usb_serial_port *port = tty->driver_data;
455         unsigned long flags;
456
457         dbg("%s - port %d", __func__, port->number);
458
459         /* Set the throttle request flag. It will be picked up
460          * by usb_serial_generic_read_bulk_callback(). */
461         spin_lock_irqsave(&port->lock, flags);
462         port->throttle_req = 1;
463         spin_unlock_irqrestore(&port->lock, flags);
464 }
465 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
466
467 void usb_serial_generic_unthrottle(struct tty_struct *tty)
468 {
469         struct usb_serial_port *port = tty->driver_data;
470         int was_throttled;
471
472         dbg("%s - port %d", __func__, port->number);
473
474         /* Clear the throttle flags */
475         spin_lock_irq(&port->lock);
476         was_throttled = port->throttled;
477         port->throttled = port->throttle_req = 0;
478         spin_unlock_irq(&port->lock);
479
480         if (was_throttled)
481                 usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
482 }
483 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
484
485 #ifdef CONFIG_MAGIC_SYSRQ
486 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
487 {
488         if (port->sysrq && port->port.console) {
489                 if (ch && time_before(jiffies, port->sysrq)) {
490                         handle_sysrq(ch);
491                         port->sysrq = 0;
492                         return 1;
493                 }
494                 port->sysrq = 0;
495         }
496         return 0;
497 }
498 #else
499 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
500 {
501         return 0;
502 }
503 #endif
504 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
505
506 int usb_serial_handle_break(struct usb_serial_port *port)
507 {
508         if (!port->sysrq) {
509                 port->sysrq = jiffies + HZ*5;
510                 return 1;
511         }
512         port->sysrq = 0;
513         return 0;
514 }
515 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
516
517 /**
518  *      usb_serial_handle_dcd_change - handle a change of carrier detect state
519  *      @port: usb_serial_port structure for the open port
520  *      @tty: tty_struct structure for the port
521  *      @status: new carrier detect status, nonzero if active
522  */
523 void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
524                                 struct tty_struct *tty, unsigned int status)
525 {
526         struct tty_port *port = &usb_port->port;
527
528         dbg("%s - port %d, status %d", __func__, usb_port->number, status);
529
530         if (status)
531                 wake_up_interruptible(&port->open_wait);
532         else if (tty && !C_CLOCAL(tty))
533                 tty_hangup(tty);
534 }
535 EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
536
537 int usb_serial_generic_resume(struct usb_serial *serial)
538 {
539         struct usb_serial_port *port;
540         int i, c = 0, r;
541
542         for (i = 0; i < serial->num_ports; i++) {
543                 port = serial->port[i];
544                 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
545                         continue;
546
547                 if (port->bulk_in_size) {
548                         r = usb_serial_generic_submit_read_urbs(port,
549                                                                 GFP_NOIO);
550                         if (r < 0)
551                                 c++;
552                 }
553
554                 if (port->bulk_out_size) {
555                         r = usb_serial_generic_write_start(port);
556                         if (r < 0)
557                                 c++;
558                 }
559         }
560
561         return c ? -EIO : 0;
562 }
563 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
564
565 void usb_serial_generic_disconnect(struct usb_serial *serial)
566 {
567         int i;
568
569         dbg("%s", __func__);
570
571         /* stop reads and writes on all ports */
572         for (i = 0; i < serial->num_ports; ++i)
573                 generic_cleanup(serial->port[i]);
574 }
575 EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect);
576
577 void usb_serial_generic_release(struct usb_serial *serial)
578 {
579         dbg("%s", __func__);
580 }