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