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