1 // SPDX-License-Identifier: GPL-2.0
3 * USB Serial Converter Generic functions
5 * Copyright (C) 2010 - 2013 Johan Hovold (jhovold@gmail.com)
6 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/sched/signal.h>
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/sysrq.h>
18 #include <linux/tty.h>
19 #include <linux/tty_flip.h>
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/usb.h>
23 #include <linux/usb/serial.h>
24 #include <linux/uaccess.h>
25 #include <linux/kfifo.h>
26 #include <linux/serial.h>
28 #ifdef CONFIG_USB_SERIAL_GENERIC
30 static __u16 vendor
= 0x05f9;
31 static __u16 product
= 0xffff;
33 module_param(vendor
, ushort
, 0);
34 MODULE_PARM_DESC(vendor
, "User specified USB idVendor");
36 module_param(product
, ushort
, 0);
37 MODULE_PARM_DESC(product
, "User specified USB idProduct");
39 static struct usb_device_id generic_device_ids
[2]; /* Initially all zeroes. */
41 static int usb_serial_generic_probe(struct usb_serial
*serial
,
42 const struct usb_device_id
*id
)
44 struct device
*dev
= &serial
->interface
->dev
;
46 dev_info(dev
, "The \"generic\" usb-serial driver is only for testing and one-off prototypes.\n");
47 dev_info(dev
, "Tell linux-usb@vger.kernel.org to add your device to a proper driver.\n");
52 static int usb_serial_generic_calc_num_ports(struct usb_serial
*serial
,
53 struct usb_serial_endpoints
*epds
)
55 struct device
*dev
= &serial
->interface
->dev
;
58 num_ports
= max(epds
->num_bulk_in
, epds
->num_bulk_out
);
61 dev_err(dev
, "device has no bulk endpoints\n");
68 static struct usb_serial_driver usb_serial_generic_device
= {
73 .id_table
= generic_device_ids
,
74 .probe
= usb_serial_generic_probe
,
75 .calc_num_ports
= usb_serial_generic_calc_num_ports
,
76 .throttle
= usb_serial_generic_throttle
,
77 .unthrottle
= usb_serial_generic_unthrottle
,
78 .resume
= usb_serial_generic_resume
,
81 static struct usb_serial_driver
* const serial_drivers
[] = {
82 &usb_serial_generic_device
, NULL
87 int usb_serial_generic_register(void)
91 #ifdef CONFIG_USB_SERIAL_GENERIC
92 generic_device_ids
[0].idVendor
= vendor
;
93 generic_device_ids
[0].idProduct
= product
;
94 generic_device_ids
[0].match_flags
=
95 USB_DEVICE_ID_MATCH_VENDOR
| USB_DEVICE_ID_MATCH_PRODUCT
;
97 retval
= usb_serial_register_drivers(serial_drivers
,
98 "usbserial_generic", generic_device_ids
);
103 void usb_serial_generic_deregister(void)
105 #ifdef CONFIG_USB_SERIAL_GENERIC
106 usb_serial_deregister_drivers(serial_drivers
);
110 int usb_serial_generic_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
115 spin_lock_irqsave(&port
->lock
, flags
);
117 port
->throttle_req
= 0;
118 spin_unlock_irqrestore(&port
->lock
, flags
);
120 if (port
->bulk_in_size
)
121 result
= usb_serial_generic_submit_read_urbs(port
, GFP_KERNEL
);
125 EXPORT_SYMBOL_GPL(usb_serial_generic_open
);
127 void usb_serial_generic_close(struct usb_serial_port
*port
)
132 if (port
->bulk_out_size
) {
133 for (i
= 0; i
< ARRAY_SIZE(port
->write_urbs
); ++i
)
134 usb_kill_urb(port
->write_urbs
[i
]);
136 spin_lock_irqsave(&port
->lock
, flags
);
137 kfifo_reset_out(&port
->write_fifo
);
138 spin_unlock_irqrestore(&port
->lock
, flags
);
140 if (port
->bulk_in_size
) {
141 for (i
= 0; i
< ARRAY_SIZE(port
->read_urbs
); ++i
)
142 usb_kill_urb(port
->read_urbs
[i
]);
145 EXPORT_SYMBOL_GPL(usb_serial_generic_close
);
147 int usb_serial_generic_prepare_write_buffer(struct usb_serial_port
*port
,
148 void *dest
, size_t size
)
150 return kfifo_out_locked(&port
->write_fifo
, dest
, size
, &port
->lock
);
154 * usb_serial_generic_write_start - start writing buffered data
155 * @port: usb-serial port
156 * @mem_flags: flags to use for memory allocations
158 * Serialised using USB_SERIAL_WRITE_BUSY flag.
160 * Return: Zero on success or if busy, otherwise a negative errno value.
162 int usb_serial_generic_write_start(struct usb_serial_port
*port
,
170 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY
, &port
->flags
))
173 spin_lock_irqsave(&port
->lock
, flags
);
174 if (!port
->write_urbs_free
|| !kfifo_len(&port
->write_fifo
)) {
175 clear_bit_unlock(USB_SERIAL_WRITE_BUSY
, &port
->flags
);
176 spin_unlock_irqrestore(&port
->lock
, flags
);
179 i
= (int)find_first_bit(&port
->write_urbs_free
,
180 ARRAY_SIZE(port
->write_urbs
));
181 spin_unlock_irqrestore(&port
->lock
, flags
);
183 urb
= port
->write_urbs
[i
];
184 count
= port
->serial
->type
->prepare_write_buffer(port
,
185 urb
->transfer_buffer
,
186 port
->bulk_out_size
);
187 urb
->transfer_buffer_length
= count
;
188 usb_serial_debug_data(&port
->dev
, __func__
, count
, urb
->transfer_buffer
);
189 spin_lock_irqsave(&port
->lock
, flags
);
190 port
->tx_bytes
+= count
;
191 spin_unlock_irqrestore(&port
->lock
, flags
);
193 clear_bit(i
, &port
->write_urbs_free
);
194 result
= usb_submit_urb(urb
, mem_flags
);
196 dev_err_console(port
, "%s - error submitting urb: %d\n",
198 set_bit(i
, &port
->write_urbs_free
);
199 spin_lock_irqsave(&port
->lock
, flags
);
200 port
->tx_bytes
-= count
;
201 spin_unlock_irqrestore(&port
->lock
, flags
);
203 clear_bit_unlock(USB_SERIAL_WRITE_BUSY
, &port
->flags
);
207 goto retry
; /* try sending off another urb */
209 EXPORT_SYMBOL_GPL(usb_serial_generic_write_start
);
212 * usb_serial_generic_write - generic write function
213 * @tty: tty for the port
214 * @port: usb-serial port
215 * @buf: data to write
216 * @count: number of bytes to write
218 * Return: The number of characters buffered, which may be anything from
219 * zero to @count, or a negative errno value.
221 int usb_serial_generic_write(struct tty_struct
*tty
,
222 struct usb_serial_port
*port
, const unsigned char *buf
, int count
)
226 if (!port
->bulk_out_size
)
232 count
= kfifo_in_locked(&port
->write_fifo
, buf
, count
, &port
->lock
);
233 result
= usb_serial_generic_write_start(port
, GFP_ATOMIC
);
239 EXPORT_SYMBOL_GPL(usb_serial_generic_write
);
241 int usb_serial_generic_write_room(struct tty_struct
*tty
)
243 struct usb_serial_port
*port
= tty
->driver_data
;
247 if (!port
->bulk_out_size
)
250 spin_lock_irqsave(&port
->lock
, flags
);
251 room
= kfifo_avail(&port
->write_fifo
);
252 spin_unlock_irqrestore(&port
->lock
, flags
);
254 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, room
);
258 int usb_serial_generic_chars_in_buffer(struct tty_struct
*tty
)
260 struct usb_serial_port
*port
= tty
->driver_data
;
264 if (!port
->bulk_out_size
)
267 spin_lock_irqsave(&port
->lock
, flags
);
268 chars
= kfifo_len(&port
->write_fifo
) + port
->tx_bytes
;
269 spin_unlock_irqrestore(&port
->lock
, flags
);
271 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, chars
);
274 EXPORT_SYMBOL_GPL(usb_serial_generic_chars_in_buffer
);
276 void usb_serial_generic_wait_until_sent(struct tty_struct
*tty
, long timeout
)
278 struct usb_serial_port
*port
= tty
->driver_data
;
280 unsigned long period
;
281 unsigned long expire
;
283 bps
= tty_get_baud_rate(tty
);
287 * Use a poll-period of roughly the time it takes to send one
288 * character or at least one jiffy.
290 period
= max_t(unsigned long, (10 * HZ
/ bps
), 1);
292 period
= min_t(unsigned long, period
, timeout
);
294 dev_dbg(&port
->dev
, "%s - timeout = %u ms, period = %u ms\n",
295 __func__
, jiffies_to_msecs(timeout
),
296 jiffies_to_msecs(period
));
297 expire
= jiffies
+ timeout
;
298 while (!port
->serial
->type
->tx_empty(port
)) {
299 schedule_timeout_interruptible(period
);
300 if (signal_pending(current
))
302 if (timeout
&& time_after(jiffies
, expire
))
306 EXPORT_SYMBOL_GPL(usb_serial_generic_wait_until_sent
);
308 static int usb_serial_generic_submit_read_urb(struct usb_serial_port
*port
,
309 int index
, gfp_t mem_flags
)
313 if (!test_and_clear_bit(index
, &port
->read_urbs_free
))
316 dev_dbg(&port
->dev
, "%s - urb %d\n", __func__
, index
);
318 res
= usb_submit_urb(port
->read_urbs
[index
], mem_flags
);
320 if (res
!= -EPERM
&& res
!= -ENODEV
) {
322 "%s - usb_submit_urb failed: %d\n",
325 set_bit(index
, &port
->read_urbs_free
);
332 int usb_serial_generic_submit_read_urbs(struct usb_serial_port
*port
,
338 for (i
= 0; i
< ARRAY_SIZE(port
->read_urbs
); ++i
) {
339 res
= usb_serial_generic_submit_read_urb(port
, i
, mem_flags
);
347 usb_kill_urb(port
->read_urbs
[i
]);
351 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs
);
353 void usb_serial_generic_process_read_urb(struct urb
*urb
)
355 struct usb_serial_port
*port
= urb
->context
;
356 char *ch
= (char *)urb
->transfer_buffer
;
359 if (!urb
->actual_length
)
362 * The per character mucking around with sysrq path it too slow for
363 * stuff like 3G modems, so shortcircuit it in the 99.9999999% of
364 * cases where the USB serial is not a console anyway.
366 if (!port
->port
.console
|| !port
->sysrq
) {
367 tty_insert_flip_string(&port
->port
, ch
, urb
->actual_length
);
369 for (i
= 0; i
< urb
->actual_length
; i
++, ch
++) {
370 if (!usb_serial_handle_sysrq_char(port
, *ch
))
371 tty_insert_flip_char(&port
->port
, *ch
, TTY_NORMAL
);
374 tty_flip_buffer_push(&port
->port
);
376 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb
);
378 void usb_serial_generic_read_bulk_callback(struct urb
*urb
)
380 struct usb_serial_port
*port
= urb
->context
;
381 unsigned char *data
= urb
->transfer_buffer
;
383 int status
= urb
->status
;
386 for (i
= 0; i
< ARRAY_SIZE(port
->read_urbs
); ++i
) {
387 if (urb
== port
->read_urbs
[i
])
390 set_bit(i
, &port
->read_urbs_free
);
392 dev_dbg(&port
->dev
, "%s - urb %d, len %d\n", __func__
, i
,
400 dev_dbg(&port
->dev
, "%s - urb stopped: %d\n",
404 dev_err(&port
->dev
, "%s - urb stopped: %d\n",
408 dev_dbg(&port
->dev
, "%s - nonzero urb status: %d\n",
413 usb_serial_debug_data(&port
->dev
, __func__
, urb
->actual_length
, data
);
414 port
->serial
->type
->process_read_urb(urb
);
417 /* Throttle the device if requested by tty */
418 spin_lock_irqsave(&port
->lock
, flags
);
419 port
->throttled
= port
->throttle_req
;
420 if (!port
->throttled
) {
421 spin_unlock_irqrestore(&port
->lock
, flags
);
422 usb_serial_generic_submit_read_urb(port
, i
, GFP_ATOMIC
);
424 spin_unlock_irqrestore(&port
->lock
, flags
);
427 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback
);
429 void usb_serial_generic_write_bulk_callback(struct urb
*urb
)
432 struct usb_serial_port
*port
= urb
->context
;
433 int status
= urb
->status
;
436 for (i
= 0; i
< ARRAY_SIZE(port
->write_urbs
); ++i
) {
437 if (port
->write_urbs
[i
] == urb
)
440 spin_lock_irqsave(&port
->lock
, flags
);
441 port
->tx_bytes
-= urb
->transfer_buffer_length
;
442 set_bit(i
, &port
->write_urbs_free
);
443 spin_unlock_irqrestore(&port
->lock
, flags
);
451 dev_dbg(&port
->dev
, "%s - urb stopped: %d\n",
455 dev_err_console(port
, "%s - urb stopped: %d\n",
459 dev_err_console(port
, "%s - nonzero urb status: %d\n",
465 usb_serial_generic_write_start(port
, GFP_ATOMIC
);
466 usb_serial_port_softint(port
);
468 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback
);
470 void usb_serial_generic_throttle(struct tty_struct
*tty
)
472 struct usb_serial_port
*port
= tty
->driver_data
;
475 spin_lock_irqsave(&port
->lock
, flags
);
476 port
->throttle_req
= 1;
477 spin_unlock_irqrestore(&port
->lock
, flags
);
479 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle
);
481 void usb_serial_generic_unthrottle(struct tty_struct
*tty
)
483 struct usb_serial_port
*port
= tty
->driver_data
;
486 spin_lock_irq(&port
->lock
);
487 was_throttled
= port
->throttled
;
488 port
->throttled
= port
->throttle_req
= 0;
489 spin_unlock_irq(&port
->lock
);
492 usb_serial_generic_submit_read_urbs(port
, GFP_KERNEL
);
494 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle
);
496 static bool usb_serial_generic_msr_changed(struct tty_struct
*tty
,
497 unsigned long arg
, struct async_icount
*cprev
)
499 struct usb_serial_port
*port
= tty
->driver_data
;
500 struct async_icount cnow
;
505 * Use tty-port initialised flag to detect all hangups including the
506 * one generated at USB-device disconnect.
508 if (!tty_port_initialized(&port
->port
))
511 spin_lock_irqsave(&port
->lock
, flags
);
512 cnow
= port
->icount
; /* atomic copy*/
513 spin_unlock_irqrestore(&port
->lock
, flags
);
515 ret
= ((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
->rng
)) ||
516 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
->dsr
)) ||
517 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
->dcd
)) ||
518 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
->cts
));
525 int usb_serial_generic_tiocmiwait(struct tty_struct
*tty
, unsigned long arg
)
527 struct usb_serial_port
*port
= tty
->driver_data
;
528 struct async_icount cnow
;
532 spin_lock_irqsave(&port
->lock
, flags
);
533 cnow
= port
->icount
; /* atomic copy */
534 spin_unlock_irqrestore(&port
->lock
, flags
);
536 ret
= wait_event_interruptible(port
->port
.delta_msr_wait
,
537 usb_serial_generic_msr_changed(tty
, arg
, &cnow
));
538 if (!ret
&& !tty_port_initialized(&port
->port
))
543 EXPORT_SYMBOL_GPL(usb_serial_generic_tiocmiwait
);
545 int usb_serial_generic_get_icount(struct tty_struct
*tty
,
546 struct serial_icounter_struct
*icount
)
548 struct usb_serial_port
*port
= tty
->driver_data
;
549 struct async_icount cnow
;
552 spin_lock_irqsave(&port
->lock
, flags
);
553 cnow
= port
->icount
; /* atomic copy */
554 spin_unlock_irqrestore(&port
->lock
, flags
);
556 icount
->cts
= cnow
.cts
;
557 icount
->dsr
= cnow
.dsr
;
558 icount
->rng
= cnow
.rng
;
559 icount
->dcd
= cnow
.dcd
;
560 icount
->tx
= cnow
.tx
;
561 icount
->rx
= cnow
.rx
;
562 icount
->frame
= cnow
.frame
;
563 icount
->parity
= cnow
.parity
;
564 icount
->overrun
= cnow
.overrun
;
565 icount
->brk
= cnow
.brk
;
566 icount
->buf_overrun
= cnow
.buf_overrun
;
570 EXPORT_SYMBOL_GPL(usb_serial_generic_get_icount
);
572 #ifdef CONFIG_MAGIC_SYSRQ
573 int usb_serial_handle_sysrq_char(struct usb_serial_port
*port
, unsigned int ch
)
575 if (port
->sysrq
&& port
->port
.console
) {
576 if (ch
&& time_before(jiffies
, port
->sysrq
)) {
586 int usb_serial_handle_sysrq_char(struct usb_serial_port
*port
, unsigned int ch
)
591 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char
);
593 int usb_serial_handle_break(struct usb_serial_port
*port
)
596 port
->sysrq
= jiffies
+ HZ
*5;
602 EXPORT_SYMBOL_GPL(usb_serial_handle_break
);
605 * usb_serial_handle_dcd_change - handle a change of carrier detect state
606 * @port: usb-serial port
607 * @tty: tty for the port
608 * @status: new carrier detect status, nonzero if active
610 void usb_serial_handle_dcd_change(struct usb_serial_port
*usb_port
,
611 struct tty_struct
*tty
, unsigned int status
)
613 struct tty_port
*port
= &usb_port
->port
;
615 dev_dbg(&usb_port
->dev
, "%s - status %d\n", __func__
, status
);
618 struct tty_ldisc
*ld
= tty_ldisc_ref(tty
);
621 if (ld
->ops
->dcd_change
)
622 ld
->ops
->dcd_change(tty
, status
);
628 wake_up_interruptible(&port
->open_wait
);
629 else if (tty
&& !C_CLOCAL(tty
))
632 EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change
);
634 int usb_serial_generic_resume(struct usb_serial
*serial
)
636 struct usb_serial_port
*port
;
639 for (i
= 0; i
< serial
->num_ports
; i
++) {
640 port
= serial
->port
[i
];
641 if (!tty_port_initialized(&port
->port
))
644 if (port
->bulk_in_size
) {
645 r
= usb_serial_generic_submit_read_urbs(port
,
651 if (port
->bulk_out_size
) {
652 r
= usb_serial_generic_write_start(port
, GFP_NOIO
);
660 EXPORT_SYMBOL_GPL(usb_serial_generic_resume
);