x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / drivers / usb / serial / generic.c
blob49ce2be90fa00e5b4305784f2532e34069752072
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.
12 #include <linux/kernel.h>
13 #include <linux/sched/signal.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 struct usb_serial_driver usb_serial_generic_device = {
41 .driver = {
42 .owner = THIS_MODULE,
43 .name = "generic",
45 .id_table = generic_device_ids,
46 .num_ports = 1,
47 .throttle = usb_serial_generic_throttle,
48 .unthrottle = usb_serial_generic_unthrottle,
49 .resume = usb_serial_generic_resume,
52 static struct usb_serial_driver * const serial_drivers[] = {
53 &usb_serial_generic_device, NULL
56 #endif
58 int usb_serial_generic_register(void)
60 int retval = 0;
62 #ifdef CONFIG_USB_SERIAL_GENERIC
63 generic_device_ids[0].idVendor = vendor;
64 generic_device_ids[0].idProduct = product;
65 generic_device_ids[0].match_flags =
66 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
68 retval = usb_serial_register_drivers(serial_drivers,
69 "usbserial_generic", generic_device_ids);
70 #endif
71 return retval;
74 void usb_serial_generic_deregister(void)
76 #ifdef CONFIG_USB_SERIAL_GENERIC
77 usb_serial_deregister_drivers(serial_drivers);
78 #endif
81 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
83 int result = 0;
84 unsigned long flags;
86 spin_lock_irqsave(&port->lock, flags);
87 port->throttled = 0;
88 port->throttle_req = 0;
89 spin_unlock_irqrestore(&port->lock, flags);
91 if (port->bulk_in_size)
92 result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
94 return result;
96 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
98 void usb_serial_generic_close(struct usb_serial_port *port)
100 unsigned long flags;
101 int i;
103 if (port->bulk_out_size) {
104 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
105 usb_kill_urb(port->write_urbs[i]);
107 spin_lock_irqsave(&port->lock, flags);
108 kfifo_reset_out(&port->write_fifo);
109 spin_unlock_irqrestore(&port->lock, flags);
111 if (port->bulk_in_size) {
112 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
113 usb_kill_urb(port->read_urbs[i]);
116 EXPORT_SYMBOL_GPL(usb_serial_generic_close);
118 int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
119 void *dest, size_t size)
121 return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
125 * usb_serial_generic_write_start - start writing buffered data
126 * @port: usb-serial port
127 * @mem_flags: flags to use for memory allocations
129 * Serialised using USB_SERIAL_WRITE_BUSY flag.
131 * Return: Zero on success or if busy, otherwise a negative errno value.
133 int usb_serial_generic_write_start(struct usb_serial_port *port,
134 gfp_t mem_flags)
136 struct urb *urb;
137 int count, result;
138 unsigned long flags;
139 int i;
141 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
142 return 0;
143 retry:
144 spin_lock_irqsave(&port->lock, flags);
145 if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
146 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
147 spin_unlock_irqrestore(&port->lock, flags);
148 return 0;
150 i = (int)find_first_bit(&port->write_urbs_free,
151 ARRAY_SIZE(port->write_urbs));
152 spin_unlock_irqrestore(&port->lock, flags);
154 urb = port->write_urbs[i];
155 count = port->serial->type->prepare_write_buffer(port,
156 urb->transfer_buffer,
157 port->bulk_out_size);
158 urb->transfer_buffer_length = count;
159 usb_serial_debug_data(&port->dev, __func__, count, urb->transfer_buffer);
160 spin_lock_irqsave(&port->lock, flags);
161 port->tx_bytes += count;
162 spin_unlock_irqrestore(&port->lock, flags);
164 clear_bit(i, &port->write_urbs_free);
165 result = usb_submit_urb(urb, mem_flags);
166 if (result) {
167 dev_err_console(port, "%s - error submitting urb: %d\n",
168 __func__, result);
169 set_bit(i, &port->write_urbs_free);
170 spin_lock_irqsave(&port->lock, flags);
171 port->tx_bytes -= count;
172 spin_unlock_irqrestore(&port->lock, flags);
174 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
175 return result;
178 goto retry; /* try sending off another urb */
180 EXPORT_SYMBOL_GPL(usb_serial_generic_write_start);
183 * usb_serial_generic_write - generic write function
184 * @tty: tty for the port
185 * @port: usb-serial port
186 * @buf: data to write
187 * @count: number of bytes to write
189 * Return: The number of characters buffered, which may be anything from
190 * zero to @count, or a negative errno value.
192 int usb_serial_generic_write(struct tty_struct *tty,
193 struct usb_serial_port *port, const unsigned char *buf, int count)
195 int result;
197 if (!port->bulk_out_size)
198 return -ENODEV;
200 if (!count)
201 return 0;
203 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
204 result = usb_serial_generic_write_start(port, GFP_ATOMIC);
205 if (result)
206 return result;
208 return count;
210 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
212 int usb_serial_generic_write_room(struct tty_struct *tty)
214 struct usb_serial_port *port = tty->driver_data;
215 unsigned long flags;
216 int room;
218 if (!port->bulk_out_size)
219 return 0;
221 spin_lock_irqsave(&port->lock, flags);
222 room = kfifo_avail(&port->write_fifo);
223 spin_unlock_irqrestore(&port->lock, flags);
225 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
226 return room;
229 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
231 struct usb_serial_port *port = tty->driver_data;
232 unsigned long flags;
233 int chars;
235 if (!port->bulk_out_size)
236 return 0;
238 spin_lock_irqsave(&port->lock, flags);
239 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
240 spin_unlock_irqrestore(&port->lock, flags);
242 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
243 return chars;
245 EXPORT_SYMBOL_GPL(usb_serial_generic_chars_in_buffer);
247 void usb_serial_generic_wait_until_sent(struct tty_struct *tty, long timeout)
249 struct usb_serial_port *port = tty->driver_data;
250 unsigned int bps;
251 unsigned long period;
252 unsigned long expire;
254 bps = tty_get_baud_rate(tty);
255 if (!bps)
256 bps = 9600; /* B0 */
258 * Use a poll-period of roughly the time it takes to send one
259 * character or at least one jiffy.
261 period = max_t(unsigned long, (10 * HZ / bps), 1);
262 if (timeout)
263 period = min_t(unsigned long, period, timeout);
265 dev_dbg(&port->dev, "%s - timeout = %u ms, period = %u ms\n",
266 __func__, jiffies_to_msecs(timeout),
267 jiffies_to_msecs(period));
268 expire = jiffies + timeout;
269 while (!port->serial->type->tx_empty(port)) {
270 schedule_timeout_interruptible(period);
271 if (signal_pending(current))
272 break;
273 if (timeout && time_after(jiffies, expire))
274 break;
277 EXPORT_SYMBOL_GPL(usb_serial_generic_wait_until_sent);
279 static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
280 int index, gfp_t mem_flags)
282 int res;
284 if (!test_and_clear_bit(index, &port->read_urbs_free))
285 return 0;
287 dev_dbg(&port->dev, "%s - urb %d\n", __func__, index);
289 res = usb_submit_urb(port->read_urbs[index], mem_flags);
290 if (res) {
291 if (res != -EPERM && res != -ENODEV) {
292 dev_err(&port->dev,
293 "%s - usb_submit_urb failed: %d\n",
294 __func__, res);
296 set_bit(index, &port->read_urbs_free);
297 return res;
300 return 0;
303 int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
304 gfp_t mem_flags)
306 int res;
307 int i;
309 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
310 res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
311 if (res)
312 goto err;
315 return 0;
316 err:
317 for (; i >= 0; --i)
318 usb_kill_urb(port->read_urbs[i]);
320 return res;
322 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
324 void usb_serial_generic_process_read_urb(struct urb *urb)
326 struct usb_serial_port *port = urb->context;
327 char *ch = (char *)urb->transfer_buffer;
328 int i;
330 if (!urb->actual_length)
331 return;
333 * The per character mucking around with sysrq path it too slow for
334 * stuff like 3G modems, so shortcircuit it in the 99.9999999% of
335 * cases where the USB serial is not a console anyway.
337 if (!port->port.console || !port->sysrq) {
338 tty_insert_flip_string(&port->port, ch, urb->actual_length);
339 } else {
340 for (i = 0; i < urb->actual_length; i++, ch++) {
341 if (!usb_serial_handle_sysrq_char(port, *ch))
342 tty_insert_flip_char(&port->port, *ch, TTY_NORMAL);
345 tty_flip_buffer_push(&port->port);
347 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
349 void usb_serial_generic_read_bulk_callback(struct urb *urb)
351 struct usb_serial_port *port = urb->context;
352 unsigned char *data = urb->transfer_buffer;
353 unsigned long flags;
354 int status = urb->status;
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);
365 switch (status) {
366 case 0:
367 break;
368 case -ENOENT:
369 case -ECONNRESET:
370 case -ESHUTDOWN:
371 dev_dbg(&port->dev, "%s - urb stopped: %d\n",
372 __func__, status);
373 return;
374 case -EPIPE:
375 dev_err(&port->dev, "%s - urb stopped: %d\n",
376 __func__, status);
377 return;
378 default:
379 dev_dbg(&port->dev, "%s - nonzero urb status: %d\n",
380 __func__, status);
381 goto resubmit;
384 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
385 port->serial->type->process_read_urb(urb);
387 resubmit:
388 /* Throttle the device if requested by tty */
389 spin_lock_irqsave(&port->lock, flags);
390 port->throttled = port->throttle_req;
391 if (!port->throttled) {
392 spin_unlock_irqrestore(&port->lock, flags);
393 usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
394 } else {
395 spin_unlock_irqrestore(&port->lock, flags);
398 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
400 void usb_serial_generic_write_bulk_callback(struct urb *urb)
402 unsigned long flags;
403 struct usb_serial_port *port = urb->context;
404 int status = urb->status;
405 int i;
407 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) {
408 if (port->write_urbs[i] == urb)
409 break;
411 spin_lock_irqsave(&port->lock, flags);
412 port->tx_bytes -= urb->transfer_buffer_length;
413 set_bit(i, &port->write_urbs_free);
414 spin_unlock_irqrestore(&port->lock, flags);
416 switch (status) {
417 case 0:
418 break;
419 case -ENOENT:
420 case -ECONNRESET:
421 case -ESHUTDOWN:
422 dev_dbg(&port->dev, "%s - urb stopped: %d\n",
423 __func__, status);
424 return;
425 case -EPIPE:
426 dev_err_console(port, "%s - urb stopped: %d\n",
427 __func__, status);
428 return;
429 default:
430 dev_err_console(port, "%s - nonzero urb status: %d\n",
431 __func__, status);
432 goto resubmit;
435 resubmit:
436 usb_serial_generic_write_start(port, GFP_ATOMIC);
437 usb_serial_port_softint(port);
439 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
441 void usb_serial_generic_throttle(struct tty_struct *tty)
443 struct usb_serial_port *port = tty->driver_data;
444 unsigned long flags;
446 spin_lock_irqsave(&port->lock, flags);
447 port->throttle_req = 1;
448 spin_unlock_irqrestore(&port->lock, flags);
450 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
452 void usb_serial_generic_unthrottle(struct tty_struct *tty)
454 struct usb_serial_port *port = tty->driver_data;
455 int was_throttled;
457 spin_lock_irq(&port->lock);
458 was_throttled = port->throttled;
459 port->throttled = port->throttle_req = 0;
460 spin_unlock_irq(&port->lock);
462 if (was_throttled)
463 usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
465 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
467 static bool usb_serial_generic_msr_changed(struct tty_struct *tty,
468 unsigned long arg, struct async_icount *cprev)
470 struct usb_serial_port *port = tty->driver_data;
471 struct async_icount cnow;
472 unsigned long flags;
473 bool ret;
476 * Use tty-port initialised flag to detect all hangups including the
477 * one generated at USB-device disconnect.
479 if (!tty_port_initialized(&port->port))
480 return true;
482 spin_lock_irqsave(&port->lock, flags);
483 cnow = port->icount; /* atomic copy*/
484 spin_unlock_irqrestore(&port->lock, flags);
486 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
487 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
488 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
489 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
491 *cprev = cnow;
493 return ret;
496 int usb_serial_generic_tiocmiwait(struct tty_struct *tty, unsigned long arg)
498 struct usb_serial_port *port = tty->driver_data;
499 struct async_icount cnow;
500 unsigned long flags;
501 int ret;
503 spin_lock_irqsave(&port->lock, flags);
504 cnow = port->icount; /* atomic copy */
505 spin_unlock_irqrestore(&port->lock, flags);
507 ret = wait_event_interruptible(port->port.delta_msr_wait,
508 usb_serial_generic_msr_changed(tty, arg, &cnow));
509 if (!ret && !tty_port_initialized(&port->port))
510 ret = -EIO;
512 return ret;
514 EXPORT_SYMBOL_GPL(usb_serial_generic_tiocmiwait);
516 int usb_serial_generic_get_icount(struct tty_struct *tty,
517 struct serial_icounter_struct *icount)
519 struct usb_serial_port *port = tty->driver_data;
520 struct async_icount cnow;
521 unsigned long flags;
523 spin_lock_irqsave(&port->lock, flags);
524 cnow = port->icount; /* atomic copy */
525 spin_unlock_irqrestore(&port->lock, flags);
527 icount->cts = cnow.cts;
528 icount->dsr = cnow.dsr;
529 icount->rng = cnow.rng;
530 icount->dcd = cnow.dcd;
531 icount->tx = cnow.tx;
532 icount->rx = cnow.rx;
533 icount->frame = cnow.frame;
534 icount->parity = cnow.parity;
535 icount->overrun = cnow.overrun;
536 icount->brk = cnow.brk;
537 icount->buf_overrun = cnow.buf_overrun;
539 return 0;
541 EXPORT_SYMBOL_GPL(usb_serial_generic_get_icount);
543 #ifdef CONFIG_MAGIC_SYSRQ
544 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
546 if (port->sysrq && port->port.console) {
547 if (ch && time_before(jiffies, port->sysrq)) {
548 handle_sysrq(ch);
549 port->sysrq = 0;
550 return 1;
552 port->sysrq = 0;
554 return 0;
556 #else
557 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
559 return 0;
561 #endif
562 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
564 int usb_serial_handle_break(struct usb_serial_port *port)
566 if (!port->sysrq) {
567 port->sysrq = jiffies + HZ*5;
568 return 1;
570 port->sysrq = 0;
571 return 0;
573 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
576 * usb_serial_handle_dcd_change - handle a change of carrier detect state
577 * @port: usb-serial port
578 * @tty: tty for the port
579 * @status: new carrier detect status, nonzero if active
581 void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
582 struct tty_struct *tty, unsigned int status)
584 struct tty_port *port = &usb_port->port;
586 dev_dbg(&usb_port->dev, "%s - status %d\n", __func__, status);
588 if (tty) {
589 struct tty_ldisc *ld = tty_ldisc_ref(tty);
591 if (ld) {
592 if (ld->ops->dcd_change)
593 ld->ops->dcd_change(tty, status);
594 tty_ldisc_deref(ld);
598 if (status)
599 wake_up_interruptible(&port->open_wait);
600 else if (tty && !C_CLOCAL(tty))
601 tty_hangup(tty);
603 EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
605 int usb_serial_generic_resume(struct usb_serial *serial)
607 struct usb_serial_port *port;
608 int i, c = 0, r;
610 for (i = 0; i < serial->num_ports; i++) {
611 port = serial->port[i];
612 if (!tty_port_initialized(&port->port))
613 continue;
615 if (port->bulk_in_size) {
616 r = usb_serial_generic_submit_read_urbs(port,
617 GFP_NOIO);
618 if (r < 0)
619 c++;
622 if (port->bulk_out_size) {
623 r = usb_serial_generic_write_start(port, GFP_NOIO);
624 if (r < 0)
625 c++;
629 return c ? -EIO : 0;
631 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);