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
= {
45 .id_table
= generic_device_ids
,
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
58 int usb_serial_generic_register(void)
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
);
74 void usb_serial_generic_deregister(void)
76 #ifdef CONFIG_USB_SERIAL_GENERIC
77 usb_serial_deregister_drivers(serial_drivers
);
81 int usb_serial_generic_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
86 spin_lock_irqsave(&port
->lock
, flags
);
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
);
96 EXPORT_SYMBOL_GPL(usb_serial_generic_open
);
98 void usb_serial_generic_close(struct usb_serial_port
*port
)
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
,
141 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY
, &port
->flags
))
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
);
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
);
167 dev_err_console(port
, "%s - error submitting urb: %d\n",
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
);
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
)
197 if (!port
->bulk_out_size
)
203 count
= kfifo_in_locked(&port
->write_fifo
, buf
, count
, &port
->lock
);
204 result
= usb_serial_generic_write_start(port
, GFP_ATOMIC
);
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
;
218 if (!port
->bulk_out_size
)
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
);
229 int usb_serial_generic_chars_in_buffer(struct tty_struct
*tty
)
231 struct usb_serial_port
*port
= tty
->driver_data
;
235 if (!port
->bulk_out_size
)
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
);
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
;
251 unsigned long period
;
252 unsigned long expire
;
254 bps
= tty_get_baud_rate(tty
);
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);
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
))
273 if (timeout
&& time_after(jiffies
, expire
))
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
)
284 if (!test_and_clear_bit(index
, &port
->read_urbs_free
))
287 dev_dbg(&port
->dev
, "%s - urb %d\n", __func__
, index
);
289 res
= usb_submit_urb(port
->read_urbs
[index
], mem_flags
);
291 if (res
!= -EPERM
&& res
!= -ENODEV
) {
293 "%s - usb_submit_urb failed: %d\n",
296 set_bit(index
, &port
->read_urbs_free
);
303 int usb_serial_generic_submit_read_urbs(struct usb_serial_port
*port
,
309 for (i
= 0; i
< ARRAY_SIZE(port
->read_urbs
); ++i
) {
310 res
= usb_serial_generic_submit_read_urb(port
, i
, mem_flags
);
318 usb_kill_urb(port
->read_urbs
[i
]);
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
;
330 if (!urb
->actual_length
)
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
);
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
;
354 int status
= urb
->status
;
357 for (i
= 0; i
< ARRAY_SIZE(port
->read_urbs
); ++i
) {
358 if (urb
== port
->read_urbs
[i
])
361 set_bit(i
, &port
->read_urbs_free
);
363 dev_dbg(&port
->dev
, "%s - urb %d, len %d\n", __func__
, i
,
371 dev_dbg(&port
->dev
, "%s - urb stopped: %d\n",
375 dev_err(&port
->dev
, "%s - urb stopped: %d\n",
379 dev_dbg(&port
->dev
, "%s - nonzero urb status: %d\n",
384 usb_serial_debug_data(&port
->dev
, __func__
, urb
->actual_length
, data
);
385 port
->serial
->type
->process_read_urb(urb
);
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
);
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
)
403 struct usb_serial_port
*port
= urb
->context
;
404 int status
= urb
->status
;
407 for (i
= 0; i
< ARRAY_SIZE(port
->write_urbs
); ++i
) {
408 if (port
->write_urbs
[i
] == urb
)
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
);
422 dev_dbg(&port
->dev
, "%s - urb stopped: %d\n",
426 dev_err_console(port
, "%s - urb stopped: %d\n",
430 dev_err_console(port
, "%s - nonzero urb status: %d\n",
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
;
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
;
457 spin_lock_irq(&port
->lock
);
458 was_throttled
= port
->throttled
;
459 port
->throttled
= port
->throttle_req
= 0;
460 spin_unlock_irq(&port
->lock
);
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
;
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
))
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
));
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
;
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
))
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
;
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
;
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
)) {
557 int usb_serial_handle_sysrq_char(struct usb_serial_port
*port
, unsigned int ch
)
562 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char
);
564 int usb_serial_handle_break(struct usb_serial_port
*port
)
567 port
->sysrq
= jiffies
+ HZ
*5;
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
);
589 struct tty_ldisc
*ld
= tty_ldisc_ref(tty
);
592 if (ld
->ops
->dcd_change
)
593 ld
->ops
->dcd_change(tty
, status
);
599 wake_up_interruptible(&port
->open_wait
);
600 else if (tty
&& !C_CLOCAL(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
;
610 for (i
= 0; i
< serial
->num_ports
; i
++) {
611 port
= serial
->port
[i
];
612 if (!tty_port_initialized(&port
->port
))
615 if (port
->bulk_in_size
) {
616 r
= usb_serial_generic_submit_read_urbs(port
,
622 if (port
->bulk_out_size
) {
623 r
= usb_serial_generic_write_start(port
, GFP_NOIO
);
631 EXPORT_SYMBOL_GPL(usb_serial_generic_resume
);