Linux 3.12.39
[linux/fpc-iii.git] / drivers / usb / serial / generic.c
blob6e66b5f84f78b393983b3b03097b7072501d2731
1 /*
2 * USB Serial Converter Generic functions
4 * Copyright (C) 2010 - 2013 Johan Hovold (jhovold@gmail.com)
5 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
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.
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>
27 #ifdef CONFIG_USB_SERIAL_GENERIC
29 static __u16 vendor = 0x05f9;
30 static __u16 product = 0xffff;
32 module_param(vendor, ushort, 0);
33 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
35 module_param(product, ushort, 0);
36 MODULE_PARM_DESC(product, "User specified USB idProduct");
38 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
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",
46 .id_table = generic_device_ids,
47 .num_ports = 1,
48 .throttle = usb_serial_generic_throttle,
49 .unthrottle = usb_serial_generic_unthrottle,
50 .resume = usb_serial_generic_resume,
53 static struct usb_serial_driver * const serial_drivers[] = {
54 &usb_serial_generic_device, NULL
57 #endif
59 int usb_serial_generic_register(void)
61 int retval = 0;
63 #ifdef CONFIG_USB_SERIAL_GENERIC
64 generic_device_ids[0].idVendor = vendor;
65 generic_device_ids[0].idProduct = product;
66 generic_device_ids[0].match_flags =
67 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
69 /* register our generic driver with ourselves */
70 retval = usb_serial_register_drivers(serial_drivers,
71 "usbserial_generic", generic_device_ids);
72 #endif
73 return retval;
76 void usb_serial_generic_deregister(void)
78 #ifdef CONFIG_USB_SERIAL_GENERIC
79 /* remove our generic driver */
80 usb_serial_deregister_drivers(serial_drivers);
81 #endif
84 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
86 int result = 0;
87 unsigned long flags;
89 /* clear the throttle flags */
90 spin_lock_irqsave(&port->lock, flags);
91 port->throttled = 0;
92 port->throttle_req = 0;
93 spin_unlock_irqrestore(&port->lock, flags);
95 /* if we have a bulk endpoint, start reading from it */
96 if (port->bulk_in_size)
97 result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
99 return result;
101 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
103 void usb_serial_generic_close(struct usb_serial_port *port)
105 unsigned long flags;
106 int i;
108 if (port->bulk_out_size) {
109 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
110 usb_kill_urb(port->write_urbs[i]);
112 spin_lock_irqsave(&port->lock, flags);
113 kfifo_reset_out(&port->write_fifo);
114 spin_unlock_irqrestore(&port->lock, flags);
116 if (port->bulk_in_size) {
117 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
118 usb_kill_urb(port->read_urbs[i]);
121 EXPORT_SYMBOL_GPL(usb_serial_generic_close);
123 int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
124 void *dest, size_t size)
126 return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
130 * usb_serial_generic_write_start - kick off an URB write
131 * @port: Pointer to the &struct usb_serial_port data
133 * Returns zero on success, or a negative errno value
135 static int usb_serial_generic_write_start(struct usb_serial_port *port)
137 struct urb *urb;
138 int count, result;
139 unsigned long flags;
140 int i;
142 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
143 return 0;
144 retry:
145 spin_lock_irqsave(&port->lock, flags);
146 if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
147 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
148 spin_unlock_irqrestore(&port->lock, flags);
149 return 0;
151 i = (int)find_first_bit(&port->write_urbs_free,
152 ARRAY_SIZE(port->write_urbs));
153 spin_unlock_irqrestore(&port->lock, flags);
155 urb = port->write_urbs[i];
156 count = port->serial->type->prepare_write_buffer(port,
157 urb->transfer_buffer,
158 port->bulk_out_size);
159 urb->transfer_buffer_length = count;
160 usb_serial_debug_data(&port->dev, __func__, count, urb->transfer_buffer);
161 spin_lock_irqsave(&port->lock, flags);
162 port->tx_bytes += count;
163 spin_unlock_irqrestore(&port->lock, flags);
165 clear_bit(i, &port->write_urbs_free);
166 result = usb_submit_urb(urb, GFP_ATOMIC);
167 if (result) {
168 dev_err_console(port, "%s - error submitting urb: %d\n",
169 __func__, result);
170 set_bit(i, &port->write_urbs_free);
171 spin_lock_irqsave(&port->lock, flags);
172 port->tx_bytes -= count;
173 spin_unlock_irqrestore(&port->lock, flags);
175 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
176 return result;
179 goto retry; /* try sending off another urb */
183 * usb_serial_generic_write - generic write function for serial USB devices
184 * @tty: Pointer to &struct tty_struct for the device
185 * @port: Pointer to the &usb_serial_port structure for the device
186 * @buf: Pointer to the data to write
187 * @count: Number of bytes to write
189 * Returns the number of characters actually written, which may be anything
190 * from zero to @count. If an error occurs, it returns the negative errno
191 * value.
193 int usb_serial_generic_write(struct tty_struct *tty,
194 struct usb_serial_port *port, const unsigned char *buf, int count)
196 int result;
198 /* only do something if we have a bulk out endpoint */
199 if (!port->bulk_out_size)
200 return -ENODEV;
202 if (!count)
203 return 0;
205 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
206 result = usb_serial_generic_write_start(port);
207 if (result)
208 return result;
210 return count;
212 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
214 int usb_serial_generic_write_room(struct tty_struct *tty)
216 struct usb_serial_port *port = tty->driver_data;
217 unsigned long flags;
218 int room;
220 if (!port->bulk_out_size)
221 return 0;
223 spin_lock_irqsave(&port->lock, flags);
224 room = kfifo_avail(&port->write_fifo);
225 spin_unlock_irqrestore(&port->lock, flags);
227 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
228 return room;
231 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
233 struct usb_serial_port *port = tty->driver_data;
234 unsigned long flags;
235 int chars;
237 if (!port->bulk_out_size)
238 return 0;
240 spin_lock_irqsave(&port->lock, flags);
241 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
242 spin_unlock_irqrestore(&port->lock, flags);
244 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
245 return chars;
247 EXPORT_SYMBOL_GPL(usb_serial_generic_chars_in_buffer);
249 void usb_serial_generic_wait_until_sent(struct tty_struct *tty, long timeout)
251 struct usb_serial_port *port = tty->driver_data;
252 unsigned int bps;
253 unsigned long period;
254 unsigned long expire;
256 bps = tty_get_baud_rate(tty);
257 if (!bps)
258 bps = 9600; /* B0 */
260 * Use a poll-period of roughly the time it takes to send one
261 * character or at least one jiffy.
263 period = max_t(unsigned long, (10 * HZ / bps), 1);
264 if (timeout)
265 period = min_t(unsigned long, period, timeout);
267 dev_dbg(&port->dev, "%s - timeout = %u ms, period = %u ms\n",
268 __func__, jiffies_to_msecs(timeout),
269 jiffies_to_msecs(period));
270 expire = jiffies + timeout;
271 while (!port->serial->type->tx_empty(port)) {
272 schedule_timeout_interruptible(period);
273 if (signal_pending(current))
274 break;
275 if (timeout && time_after(jiffies, expire))
276 break;
279 EXPORT_SYMBOL_GPL(usb_serial_generic_wait_until_sent);
281 static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
282 int index, gfp_t mem_flags)
284 int res;
286 if (!test_and_clear_bit(index, &port->read_urbs_free))
287 return 0;
289 dev_dbg(&port->dev, "%s - urb %d\n", __func__, index);
291 res = usb_submit_urb(port->read_urbs[index], mem_flags);
292 if (res) {
293 if (res != -EPERM) {
294 dev_err(&port->dev,
295 "%s - usb_submit_urb failed: %d\n",
296 __func__, res);
298 set_bit(index, &port->read_urbs_free);
299 return res;
302 return 0;
305 int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
306 gfp_t mem_flags)
308 int res;
309 int i;
311 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
312 res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
313 if (res)
314 goto err;
317 return 0;
318 err:
319 for (; i >= 0; --i)
320 usb_kill_urb(port->read_urbs[i]);
322 return res;
324 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
326 void usb_serial_generic_process_read_urb(struct urb *urb)
328 struct usb_serial_port *port = urb->context;
329 char *ch = (char *)urb->transfer_buffer;
330 int i;
332 if (!urb->actual_length)
333 return;
335 /* The per character mucking around with sysrq path it too slow for
336 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
337 where the USB serial is not a console anyway */
338 if (!port->port.console || !port->sysrq)
339 tty_insert_flip_string(&port->port, ch, urb->actual_length);
340 else {
341 for (i = 0; i < urb->actual_length; i++, ch++) {
342 if (!usb_serial_handle_sysrq_char(port, *ch))
343 tty_insert_flip_char(&port->port, *ch, TTY_NORMAL);
346 tty_flip_buffer_push(&port->port);
348 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
350 void usb_serial_generic_read_bulk_callback(struct urb *urb)
352 struct usb_serial_port *port = urb->context;
353 unsigned char *data = urb->transfer_buffer;
354 unsigned long flags;
355 int i;
357 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
358 if (urb == port->read_urbs[i])
359 break;
361 set_bit(i, &port->read_urbs_free);
363 dev_dbg(&port->dev, "%s - urb %d, len %d\n", __func__, i,
364 urb->actual_length);
366 if (urb->status) {
367 dev_dbg(&port->dev, "%s - non-zero urb status: %d\n",
368 __func__, urb->status);
369 return;
372 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
373 port->serial->type->process_read_urb(urb);
375 /* Throttle the device if requested by tty */
376 spin_lock_irqsave(&port->lock, flags);
377 port->throttled = port->throttle_req;
378 if (!port->throttled) {
379 spin_unlock_irqrestore(&port->lock, flags);
380 usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
381 } else
382 spin_unlock_irqrestore(&port->lock, flags);
384 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
386 void usb_serial_generic_write_bulk_callback(struct urb *urb)
388 unsigned long flags;
389 struct usb_serial_port *port = urb->context;
390 int status = urb->status;
391 int i;
393 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
394 if (port->write_urbs[i] == urb)
395 break;
397 spin_lock_irqsave(&port->lock, flags);
398 port->tx_bytes -= urb->transfer_buffer_length;
399 set_bit(i, &port->write_urbs_free);
400 spin_unlock_irqrestore(&port->lock, flags);
402 if (status) {
403 dev_dbg(&port->dev, "%s - non-zero urb status: %d\n",
404 __func__, status);
406 spin_lock_irqsave(&port->lock, flags);
407 kfifo_reset_out(&port->write_fifo);
408 spin_unlock_irqrestore(&port->lock, flags);
409 } else {
410 usb_serial_generic_write_start(port);
413 usb_serial_port_softint(port);
415 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
417 void usb_serial_generic_throttle(struct tty_struct *tty)
419 struct usb_serial_port *port = tty->driver_data;
420 unsigned long flags;
422 /* Set the throttle request flag. It will be picked up
423 * by usb_serial_generic_read_bulk_callback(). */
424 spin_lock_irqsave(&port->lock, flags);
425 port->throttle_req = 1;
426 spin_unlock_irqrestore(&port->lock, flags);
428 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
430 void usb_serial_generic_unthrottle(struct tty_struct *tty)
432 struct usb_serial_port *port = tty->driver_data;
433 int was_throttled;
435 /* Clear the throttle flags */
436 spin_lock_irq(&port->lock);
437 was_throttled = port->throttled;
438 port->throttled = port->throttle_req = 0;
439 spin_unlock_irq(&port->lock);
441 if (was_throttled)
442 usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
444 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
446 static bool usb_serial_generic_msr_changed(struct tty_struct *tty,
447 unsigned long arg, struct async_icount *cprev)
449 struct usb_serial_port *port = tty->driver_data;
450 struct async_icount cnow;
451 unsigned long flags;
452 bool ret;
455 * Use tty-port initialised flag to detect all hangups including the
456 * one generated at USB-device disconnect.
458 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
459 return true;
461 spin_lock_irqsave(&port->lock, flags);
462 cnow = port->icount; /* atomic copy*/
463 spin_unlock_irqrestore(&port->lock, flags);
465 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
466 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
467 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
468 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
470 *cprev = cnow;
472 return ret;
475 int usb_serial_generic_tiocmiwait(struct tty_struct *tty, unsigned long arg)
477 struct usb_serial_port *port = tty->driver_data;
478 struct async_icount cnow;
479 unsigned long flags;
480 int ret;
482 spin_lock_irqsave(&port->lock, flags);
483 cnow = port->icount; /* atomic copy */
484 spin_unlock_irqrestore(&port->lock, flags);
486 ret = wait_event_interruptible(port->port.delta_msr_wait,
487 usb_serial_generic_msr_changed(tty, arg, &cnow));
488 if (!ret && !test_bit(ASYNCB_INITIALIZED, &port->port.flags))
489 ret = -EIO;
491 return ret;
493 EXPORT_SYMBOL_GPL(usb_serial_generic_tiocmiwait);
495 int usb_serial_generic_get_icount(struct tty_struct *tty,
496 struct serial_icounter_struct *icount)
498 struct usb_serial_port *port = tty->driver_data;
499 struct async_icount cnow;
500 unsigned long flags;
502 spin_lock_irqsave(&port->lock, flags);
503 cnow = port->icount; /* atomic copy */
504 spin_unlock_irqrestore(&port->lock, flags);
506 icount->cts = cnow.cts;
507 icount->dsr = cnow.dsr;
508 icount->rng = cnow.rng;
509 icount->dcd = cnow.dcd;
510 icount->tx = cnow.tx;
511 icount->rx = cnow.rx;
512 icount->frame = cnow.frame;
513 icount->parity = cnow.parity;
514 icount->overrun = cnow.overrun;
515 icount->brk = cnow.brk;
516 icount->buf_overrun = cnow.buf_overrun;
518 return 0;
520 EXPORT_SYMBOL_GPL(usb_serial_generic_get_icount);
522 #ifdef CONFIG_MAGIC_SYSRQ
523 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
525 if (port->sysrq && port->port.console) {
526 if (ch && time_before(jiffies, port->sysrq)) {
527 handle_sysrq(ch);
528 port->sysrq = 0;
529 return 1;
531 port->sysrq = 0;
533 return 0;
535 #else
536 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
538 return 0;
540 #endif
541 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
543 int usb_serial_handle_break(struct usb_serial_port *port)
545 if (!port->sysrq) {
546 port->sysrq = jiffies + HZ*5;
547 return 1;
549 port->sysrq = 0;
550 return 0;
552 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
555 * usb_serial_handle_dcd_change - handle a change of carrier detect state
556 * @port: usb_serial_port structure for the open port
557 * @tty: tty_struct structure for the port
558 * @status: new carrier detect status, nonzero if active
560 void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
561 struct tty_struct *tty, unsigned int status)
563 struct tty_port *port = &usb_port->port;
565 dev_dbg(&usb_port->dev, "%s - status %d\n", __func__, status);
567 if (status)
568 wake_up_interruptible(&port->open_wait);
569 else if (tty && !C_CLOCAL(tty))
570 tty_hangup(tty);
572 EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
574 int usb_serial_generic_resume(struct usb_serial *serial)
576 struct usb_serial_port *port;
577 int i, c = 0, r;
579 for (i = 0; i < serial->num_ports; i++) {
580 port = serial->port[i];
581 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
582 continue;
584 if (port->bulk_in_size) {
585 r = usb_serial_generic_submit_read_urbs(port,
586 GFP_NOIO);
587 if (r < 0)
588 c++;
591 if (port->bulk_out_size) {
592 r = usb_serial_generic_write_start(port);
593 if (r < 0)
594 c++;
598 return c ? -EIO : 0;
600 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);