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