]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/usb/serial/generic.c
USB: add SPDX identifiers to all remaining files in drivers/usb/
[mirror_ubuntu-jammy-kernel.git] / drivers / usb / serial / generic.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * USB Serial Converter Generic functions
4 *
5 * Copyright (C) 2010 - 2013 Johan Hovold (jhovold@gmail.com)
6 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
7 *
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.
11 */
12
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>
27
28 #ifdef CONFIG_USB_SERIAL_GENERIC
29
30 static __u16 vendor = 0x05f9;
31 static __u16 product = 0xffff;
32
33 module_param(vendor, ushort, 0);
34 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
35
36 module_param(product, ushort, 0);
37 MODULE_PARM_DESC(product, "User specified USB idProduct");
38
39 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
40
41 static int usb_serial_generic_probe(struct usb_serial *serial,
42 const struct usb_device_id *id)
43 {
44 struct device *dev = &serial->interface->dev;
45
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");
48
49 return 0;
50 }
51
52 static int usb_serial_generic_calc_num_ports(struct usb_serial *serial,
53 struct usb_serial_endpoints *epds)
54 {
55 struct device *dev = &serial->interface->dev;
56 int num_ports;
57
58 num_ports = max(epds->num_bulk_in, epds->num_bulk_out);
59
60 if (num_ports == 0) {
61 dev_err(dev, "device has no bulk endpoints\n");
62 return -ENODEV;
63 }
64
65 return num_ports;
66 }
67
68 static struct usb_serial_driver usb_serial_generic_device = {
69 .driver = {
70 .owner = THIS_MODULE,
71 .name = "generic",
72 },
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,
79 };
80
81 static struct usb_serial_driver * const serial_drivers[] = {
82 &usb_serial_generic_device, NULL
83 };
84
85 #endif
86
87 int usb_serial_generic_register(void)
88 {
89 int retval = 0;
90
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;
96
97 retval = usb_serial_register_drivers(serial_drivers,
98 "usbserial_generic", generic_device_ids);
99 #endif
100 return retval;
101 }
102
103 void usb_serial_generic_deregister(void)
104 {
105 #ifdef CONFIG_USB_SERIAL_GENERIC
106 usb_serial_deregister_drivers(serial_drivers);
107 #endif
108 }
109
110 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
111 {
112 int result = 0;
113 unsigned long flags;
114
115 spin_lock_irqsave(&port->lock, flags);
116 port->throttled = 0;
117 port->throttle_req = 0;
118 spin_unlock_irqrestore(&port->lock, flags);
119
120 if (port->bulk_in_size)
121 result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
122
123 return result;
124 }
125 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
126
127 void usb_serial_generic_close(struct usb_serial_port *port)
128 {
129 unsigned long flags;
130 int i;
131
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]);
135
136 spin_lock_irqsave(&port->lock, flags);
137 kfifo_reset_out(&port->write_fifo);
138 spin_unlock_irqrestore(&port->lock, flags);
139 }
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]);
143 }
144 }
145 EXPORT_SYMBOL_GPL(usb_serial_generic_close);
146
147 int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
148 void *dest, size_t size)
149 {
150 return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
151 }
152
153 /**
154 * usb_serial_generic_write_start - start writing buffered data
155 * @port: usb-serial port
156 * @mem_flags: flags to use for memory allocations
157 *
158 * Serialised using USB_SERIAL_WRITE_BUSY flag.
159 *
160 * Return: Zero on success or if busy, otherwise a negative errno value.
161 */
162 int usb_serial_generic_write_start(struct usb_serial_port *port,
163 gfp_t mem_flags)
164 {
165 struct urb *urb;
166 int count, result;
167 unsigned long flags;
168 int i;
169
170 if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
171 return 0;
172 retry:
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);
177 return 0;
178 }
179 i = (int)find_first_bit(&port->write_urbs_free,
180 ARRAY_SIZE(port->write_urbs));
181 spin_unlock_irqrestore(&port->lock, flags);
182
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);
192
193 clear_bit(i, &port->write_urbs_free);
194 result = usb_submit_urb(urb, mem_flags);
195 if (result) {
196 dev_err_console(port, "%s - error submitting urb: %d\n",
197 __func__, result);
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);
202
203 clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
204 return result;
205 }
206
207 goto retry; /* try sending off another urb */
208 }
209 EXPORT_SYMBOL_GPL(usb_serial_generic_write_start);
210
211 /**
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
217 *
218 * Return: The number of characters buffered, which may be anything from
219 * zero to @count, or a negative errno value.
220 */
221 int usb_serial_generic_write(struct tty_struct *tty,
222 struct usb_serial_port *port, const unsigned char *buf, int count)
223 {
224 int result;
225
226 if (!port->bulk_out_size)
227 return -ENODEV;
228
229 if (!count)
230 return 0;
231
232 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
233 result = usb_serial_generic_write_start(port, GFP_ATOMIC);
234 if (result)
235 return result;
236
237 return count;
238 }
239 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
240
241 int usb_serial_generic_write_room(struct tty_struct *tty)
242 {
243 struct usb_serial_port *port = tty->driver_data;
244 unsigned long flags;
245 int room;
246
247 if (!port->bulk_out_size)
248 return 0;
249
250 spin_lock_irqsave(&port->lock, flags);
251 room = kfifo_avail(&port->write_fifo);
252 spin_unlock_irqrestore(&port->lock, flags);
253
254 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
255 return room;
256 }
257
258 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
259 {
260 struct usb_serial_port *port = tty->driver_data;
261 unsigned long flags;
262 int chars;
263
264 if (!port->bulk_out_size)
265 return 0;
266
267 spin_lock_irqsave(&port->lock, flags);
268 chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
269 spin_unlock_irqrestore(&port->lock, flags);
270
271 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
272 return chars;
273 }
274 EXPORT_SYMBOL_GPL(usb_serial_generic_chars_in_buffer);
275
276 void usb_serial_generic_wait_until_sent(struct tty_struct *tty, long timeout)
277 {
278 struct usb_serial_port *port = tty->driver_data;
279 unsigned int bps;
280 unsigned long period;
281 unsigned long expire;
282
283 bps = tty_get_baud_rate(tty);
284 if (!bps)
285 bps = 9600; /* B0 */
286 /*
287 * Use a poll-period of roughly the time it takes to send one
288 * character or at least one jiffy.
289 */
290 period = max_t(unsigned long, (10 * HZ / bps), 1);
291 if (timeout)
292 period = min_t(unsigned long, period, timeout);
293
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))
301 break;
302 if (timeout && time_after(jiffies, expire))
303 break;
304 }
305 }
306 EXPORT_SYMBOL_GPL(usb_serial_generic_wait_until_sent);
307
308 static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
309 int index, gfp_t mem_flags)
310 {
311 int res;
312
313 if (!test_and_clear_bit(index, &port->read_urbs_free))
314 return 0;
315
316 dev_dbg(&port->dev, "%s - urb %d\n", __func__, index);
317
318 res = usb_submit_urb(port->read_urbs[index], mem_flags);
319 if (res) {
320 if (res != -EPERM && res != -ENODEV) {
321 dev_err(&port->dev,
322 "%s - usb_submit_urb failed: %d\n",
323 __func__, res);
324 }
325 set_bit(index, &port->read_urbs_free);
326 return res;
327 }
328
329 return 0;
330 }
331
332 int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
333 gfp_t mem_flags)
334 {
335 int res;
336 int i;
337
338 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
339 res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
340 if (res)
341 goto err;
342 }
343
344 return 0;
345 err:
346 for (; i >= 0; --i)
347 usb_kill_urb(port->read_urbs[i]);
348
349 return res;
350 }
351 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
352
353 void usb_serial_generic_process_read_urb(struct urb *urb)
354 {
355 struct usb_serial_port *port = urb->context;
356 char *ch = (char *)urb->transfer_buffer;
357 int i;
358
359 if (!urb->actual_length)
360 return;
361 /*
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.
365 */
366 if (!port->port.console || !port->sysrq) {
367 tty_insert_flip_string(&port->port, ch, urb->actual_length);
368 } else {
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);
372 }
373 }
374 tty_flip_buffer_push(&port->port);
375 }
376 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
377
378 void usb_serial_generic_read_bulk_callback(struct urb *urb)
379 {
380 struct usb_serial_port *port = urb->context;
381 unsigned char *data = urb->transfer_buffer;
382 unsigned long flags;
383 int status = urb->status;
384 int i;
385
386 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
387 if (urb == port->read_urbs[i])
388 break;
389 }
390 set_bit(i, &port->read_urbs_free);
391
392 dev_dbg(&port->dev, "%s - urb %d, len %d\n", __func__, i,
393 urb->actual_length);
394 switch (status) {
395 case 0:
396 break;
397 case -ENOENT:
398 case -ECONNRESET:
399 case -ESHUTDOWN:
400 dev_dbg(&port->dev, "%s - urb stopped: %d\n",
401 __func__, status);
402 return;
403 case -EPIPE:
404 dev_err(&port->dev, "%s - urb stopped: %d\n",
405 __func__, status);
406 return;
407 default:
408 dev_dbg(&port->dev, "%s - nonzero urb status: %d\n",
409 __func__, status);
410 goto resubmit;
411 }
412
413 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
414 port->serial->type->process_read_urb(urb);
415
416 resubmit:
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);
423 } else {
424 spin_unlock_irqrestore(&port->lock, flags);
425 }
426 }
427 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
428
429 void usb_serial_generic_write_bulk_callback(struct urb *urb)
430 {
431 unsigned long flags;
432 struct usb_serial_port *port = urb->context;
433 int status = urb->status;
434 int i;
435
436 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) {
437 if (port->write_urbs[i] == urb)
438 break;
439 }
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);
444
445 switch (status) {
446 case 0:
447 break;
448 case -ENOENT:
449 case -ECONNRESET:
450 case -ESHUTDOWN:
451 dev_dbg(&port->dev, "%s - urb stopped: %d\n",
452 __func__, status);
453 return;
454 case -EPIPE:
455 dev_err_console(port, "%s - urb stopped: %d\n",
456 __func__, status);
457 return;
458 default:
459 dev_err_console(port, "%s - nonzero urb status: %d\n",
460 __func__, status);
461 goto resubmit;
462 }
463
464 resubmit:
465 usb_serial_generic_write_start(port, GFP_ATOMIC);
466 usb_serial_port_softint(port);
467 }
468 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
469
470 void usb_serial_generic_throttle(struct tty_struct *tty)
471 {
472 struct usb_serial_port *port = tty->driver_data;
473 unsigned long flags;
474
475 spin_lock_irqsave(&port->lock, flags);
476 port->throttle_req = 1;
477 spin_unlock_irqrestore(&port->lock, flags);
478 }
479 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
480
481 void usb_serial_generic_unthrottle(struct tty_struct *tty)
482 {
483 struct usb_serial_port *port = tty->driver_data;
484 int was_throttled;
485
486 spin_lock_irq(&port->lock);
487 was_throttled = port->throttled;
488 port->throttled = port->throttle_req = 0;
489 spin_unlock_irq(&port->lock);
490
491 if (was_throttled)
492 usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
493 }
494 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
495
496 static bool usb_serial_generic_msr_changed(struct tty_struct *tty,
497 unsigned long arg, struct async_icount *cprev)
498 {
499 struct usb_serial_port *port = tty->driver_data;
500 struct async_icount cnow;
501 unsigned long flags;
502 bool ret;
503
504 /*
505 * Use tty-port initialised flag to detect all hangups including the
506 * one generated at USB-device disconnect.
507 */
508 if (!tty_port_initialized(&port->port))
509 return true;
510
511 spin_lock_irqsave(&port->lock, flags);
512 cnow = port->icount; /* atomic copy*/
513 spin_unlock_irqrestore(&port->lock, flags);
514
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));
519
520 *cprev = cnow;
521
522 return ret;
523 }
524
525 int usb_serial_generic_tiocmiwait(struct tty_struct *tty, unsigned long arg)
526 {
527 struct usb_serial_port *port = tty->driver_data;
528 struct async_icount cnow;
529 unsigned long flags;
530 int ret;
531
532 spin_lock_irqsave(&port->lock, flags);
533 cnow = port->icount; /* atomic copy */
534 spin_unlock_irqrestore(&port->lock, flags);
535
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))
539 ret = -EIO;
540
541 return ret;
542 }
543 EXPORT_SYMBOL_GPL(usb_serial_generic_tiocmiwait);
544
545 int usb_serial_generic_get_icount(struct tty_struct *tty,
546 struct serial_icounter_struct *icount)
547 {
548 struct usb_serial_port *port = tty->driver_data;
549 struct async_icount cnow;
550 unsigned long flags;
551
552 spin_lock_irqsave(&port->lock, flags);
553 cnow = port->icount; /* atomic copy */
554 spin_unlock_irqrestore(&port->lock, flags);
555
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;
567
568 return 0;
569 }
570 EXPORT_SYMBOL_GPL(usb_serial_generic_get_icount);
571
572 #ifdef CONFIG_MAGIC_SYSRQ
573 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
574 {
575 if (port->sysrq && port->port.console) {
576 if (ch && time_before(jiffies, port->sysrq)) {
577 handle_sysrq(ch);
578 port->sysrq = 0;
579 return 1;
580 }
581 port->sysrq = 0;
582 }
583 return 0;
584 }
585 #else
586 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
587 {
588 return 0;
589 }
590 #endif
591 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
592
593 int usb_serial_handle_break(struct usb_serial_port *port)
594 {
595 if (!port->sysrq) {
596 port->sysrq = jiffies + HZ*5;
597 return 1;
598 }
599 port->sysrq = 0;
600 return 0;
601 }
602 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
603
604 /**
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
609 */
610 void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
611 struct tty_struct *tty, unsigned int status)
612 {
613 struct tty_port *port = &usb_port->port;
614
615 dev_dbg(&usb_port->dev, "%s - status %d\n", __func__, status);
616
617 if (tty) {
618 struct tty_ldisc *ld = tty_ldisc_ref(tty);
619
620 if (ld) {
621 if (ld->ops->dcd_change)
622 ld->ops->dcd_change(tty, status);
623 tty_ldisc_deref(ld);
624 }
625 }
626
627 if (status)
628 wake_up_interruptible(&port->open_wait);
629 else if (tty && !C_CLOCAL(tty))
630 tty_hangup(tty);
631 }
632 EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
633
634 int usb_serial_generic_resume(struct usb_serial *serial)
635 {
636 struct usb_serial_port *port;
637 int i, c = 0, r;
638
639 for (i = 0; i < serial->num_ports; i++) {
640 port = serial->port[i];
641 if (!tty_port_initialized(&port->port))
642 continue;
643
644 if (port->bulk_in_size) {
645 r = usb_serial_generic_submit_read_urbs(port,
646 GFP_NOIO);
647 if (r < 0)
648 c++;
649 }
650
651 if (port->bulk_out_size) {
652 r = usb_serial_generic_write_start(port, GFP_NOIO);
653 if (r < 0)
654 c++;
655 }
656 }
657
658 return c ? -EIO : 0;
659 }
660 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);