]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/usb/serial/generic.c
Merge branch 'perf-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[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 EXPORT_SYMBOL_GPL(usb_serial_generic_chars_in_buffer);
266
267 static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
268                                                 int index, gfp_t mem_flags)
269 {
270         int res;
271
272         if (!test_and_clear_bit(index, &port->read_urbs_free))
273                 return 0;
274
275         dev_dbg(&port->dev, "%s - port %d, urb %d\n", __func__,
276                 port->number, index);
277
278         res = usb_submit_urb(port->read_urbs[index], mem_flags);
279         if (res) {
280                 if (res != -EPERM) {
281                         dev_err(&port->dev,
282                                         "%s - usb_submit_urb failed: %d\n",
283                                         __func__, res);
284                 }
285                 set_bit(index, &port->read_urbs_free);
286                 return res;
287         }
288
289         return 0;
290 }
291
292 int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
293                                         gfp_t mem_flags)
294 {
295         int res;
296         int i;
297
298         for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
299                 res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
300                 if (res)
301                         goto err;
302         }
303
304         return 0;
305 err:
306         for (; i >= 0; --i)
307                 usb_kill_urb(port->read_urbs[i]);
308
309         return res;
310 }
311 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
312
313 void usb_serial_generic_process_read_urb(struct urb *urb)
314 {
315         struct usb_serial_port *port = urb->context;
316         struct tty_struct *tty;
317         char *ch = (char *)urb->transfer_buffer;
318         int i;
319
320         if (!urb->actual_length)
321                 return;
322
323         tty = tty_port_tty_get(&port->port);
324         if (!tty)
325                 return;
326
327         /* The per character mucking around with sysrq path it too slow for
328            stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
329            where the USB serial is not a console anyway */
330         if (!port->port.console || !port->sysrq)
331                 tty_insert_flip_string(tty, ch, urb->actual_length);
332         else {
333                 for (i = 0; i < urb->actual_length; i++, ch++) {
334                         if (!usb_serial_handle_sysrq_char(port, *ch))
335                                 tty_insert_flip_char(tty, *ch, TTY_NORMAL);
336                 }
337         }
338         tty_flip_buffer_push(tty);
339         tty_kref_put(tty);
340 }
341 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
342
343 void usb_serial_generic_read_bulk_callback(struct urb *urb)
344 {
345         struct usb_serial_port *port = urb->context;
346         unsigned char *data = urb->transfer_buffer;
347         unsigned long flags;
348         int i;
349
350         for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
351                 if (urb == port->read_urbs[i])
352                         break;
353         }
354         set_bit(i, &port->read_urbs_free);
355
356         dev_dbg(&port->dev, "%s - port %d, urb %d, len %d\n",
357                 __func__, port->number, i, urb->actual_length);
358
359         if (urb->status) {
360                 dev_dbg(&port->dev, "%s - non-zero urb status: %d\n",
361                         __func__, urb->status);
362                 return;
363         }
364
365         usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
366         port->serial->type->process_read_urb(urb);
367
368         /* Throttle the device if requested by tty */
369         spin_lock_irqsave(&port->lock, flags);
370         port->throttled = port->throttle_req;
371         if (!port->throttled) {
372                 spin_unlock_irqrestore(&port->lock, flags);
373                 usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
374         } else
375                 spin_unlock_irqrestore(&port->lock, flags);
376 }
377 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
378
379 void usb_serial_generic_write_bulk_callback(struct urb *urb)
380 {
381         unsigned long flags;
382         struct usb_serial_port *port = urb->context;
383         int status = urb->status;
384         int i;
385
386         for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
387                 if (port->write_urbs[i] == urb)
388                         break;
389
390         spin_lock_irqsave(&port->lock, flags);
391         port->tx_bytes -= urb->transfer_buffer_length;
392         set_bit(i, &port->write_urbs_free);
393         spin_unlock_irqrestore(&port->lock, flags);
394
395         if (status) {
396                 dev_dbg(&port->dev, "%s - non-zero urb status: %d\n",
397                         __func__, status);
398
399                 spin_lock_irqsave(&port->lock, flags);
400                 kfifo_reset_out(&port->write_fifo);
401                 spin_unlock_irqrestore(&port->lock, flags);
402         } else {
403                 usb_serial_generic_write_start(port);
404         }
405
406         usb_serial_port_softint(port);
407 }
408 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
409
410 void usb_serial_generic_throttle(struct tty_struct *tty)
411 {
412         struct usb_serial_port *port = tty->driver_data;
413         unsigned long flags;
414
415         /* Set the throttle request flag. It will be picked up
416          * by usb_serial_generic_read_bulk_callback(). */
417         spin_lock_irqsave(&port->lock, flags);
418         port->throttle_req = 1;
419         spin_unlock_irqrestore(&port->lock, flags);
420 }
421 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
422
423 void usb_serial_generic_unthrottle(struct tty_struct *tty)
424 {
425         struct usb_serial_port *port = tty->driver_data;
426         int was_throttled;
427
428         /* Clear the throttle flags */
429         spin_lock_irq(&port->lock);
430         was_throttled = port->throttled;
431         port->throttled = port->throttle_req = 0;
432         spin_unlock_irq(&port->lock);
433
434         if (was_throttled)
435                 usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
436 }
437 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
438
439 #ifdef CONFIG_MAGIC_SYSRQ
440 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
441 {
442         if (port->sysrq && port->port.console) {
443                 if (ch && time_before(jiffies, port->sysrq)) {
444                         handle_sysrq(ch);
445                         port->sysrq = 0;
446                         return 1;
447                 }
448                 port->sysrq = 0;
449         }
450         return 0;
451 }
452 #else
453 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
454 {
455         return 0;
456 }
457 #endif
458 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
459
460 int usb_serial_handle_break(struct usb_serial_port *port)
461 {
462         if (!port->sysrq) {
463                 port->sysrq = jiffies + HZ*5;
464                 return 1;
465         }
466         port->sysrq = 0;
467         return 0;
468 }
469 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
470
471 /**
472  *      usb_serial_handle_dcd_change - handle a change of carrier detect state
473  *      @port: usb_serial_port structure for the open port
474  *      @tty: tty_struct structure for the port
475  *      @status: new carrier detect status, nonzero if active
476  */
477 void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
478                                 struct tty_struct *tty, unsigned int status)
479 {
480         struct tty_port *port = &usb_port->port;
481
482         dev_dbg(&usb_port->dev, "%s - port %d, status %d\n", __func__,
483                 usb_port->number, status);
484
485         if (status)
486                 wake_up_interruptible(&port->open_wait);
487         else if (tty && !C_CLOCAL(tty))
488                 tty_hangup(tty);
489 }
490 EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
491
492 int usb_serial_generic_resume(struct usb_serial *serial)
493 {
494         struct usb_serial_port *port;
495         int i, c = 0, r;
496
497         for (i = 0; i < serial->num_ports; i++) {
498                 port = serial->port[i];
499                 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
500                         continue;
501
502                 if (port->bulk_in_size) {
503                         r = usb_serial_generic_submit_read_urbs(port,
504                                                                 GFP_NOIO);
505                         if (r < 0)
506                                 c++;
507                 }
508
509                 if (port->bulk_out_size) {
510                         r = usb_serial_generic_write_start(port);
511                         if (r < 0)
512                                 c++;
513                 }
514         }
515
516         return c ? -EIO : 0;
517 }
518 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
519
520 void usb_serial_generic_disconnect(struct usb_serial *serial)
521 {
522         int i;
523
524         /* stop reads and writes on all ports */
525         for (i = 0; i < serial->num_ports; ++i)
526                 generic_cleanup(serial->port[i]);
527 }
528 EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect);
529
530 void usb_serial_generic_release(struct usb_serial *serial)
531 {
532 }