xref: /openbmc/linux/drivers/usb/serial/generic.c (revision 4f6cce39)
1 /*
2  * USB Serial Converter Generic functions
3  *
4  * Copyright (C) 2010 - 2013 Johan Hovold (jhovold@gmail.com)
5  * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
6  *
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.
10  */
11 
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>
26 
27 #ifdef CONFIG_USB_SERIAL_GENERIC
28 
29 static __u16 vendor  = 0x05f9;
30 static __u16 product = 0xffff;
31 
32 module_param(vendor, ushort, 0);
33 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
34 
35 module_param(product, ushort, 0);
36 MODULE_PARM_DESC(product, "User specified USB idProduct");
37 
38 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
39 
40 struct usb_serial_driver usb_serial_generic_device = {
41 	.driver = {
42 		.owner =	THIS_MODULE,
43 		.name =		"generic",
44 	},
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,
50 };
51 
52 static struct usb_serial_driver * const serial_drivers[] = {
53 	&usb_serial_generic_device, NULL
54 };
55 
56 #endif
57 
58 int usb_serial_generic_register(void)
59 {
60 	int retval = 0;
61 
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;
67 
68 	retval = usb_serial_register_drivers(serial_drivers,
69 			"usbserial_generic", generic_device_ids);
70 #endif
71 	return retval;
72 }
73 
74 void usb_serial_generic_deregister(void)
75 {
76 #ifdef CONFIG_USB_SERIAL_GENERIC
77 	usb_serial_deregister_drivers(serial_drivers);
78 #endif
79 }
80 
81 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
82 {
83 	int result = 0;
84 	unsigned long flags;
85 
86 	spin_lock_irqsave(&port->lock, flags);
87 	port->throttled = 0;
88 	port->throttle_req = 0;
89 	spin_unlock_irqrestore(&port->lock, flags);
90 
91 	if (port->bulk_in_size)
92 		result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
93 
94 	return result;
95 }
96 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
97 
98 void usb_serial_generic_close(struct usb_serial_port *port)
99 {
100 	unsigned long flags;
101 	int i;
102 
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]);
106 
107 		spin_lock_irqsave(&port->lock, flags);
108 		kfifo_reset_out(&port->write_fifo);
109 		spin_unlock_irqrestore(&port->lock, flags);
110 	}
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]);
114 	}
115 }
116 EXPORT_SYMBOL_GPL(usb_serial_generic_close);
117 
118 int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
119 						void *dest, size_t size)
120 {
121 	return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
122 }
123 
124 /**
125  * usb_serial_generic_write_start - start writing buffered data
126  * @port: usb-serial port
127  * @mem_flags: flags to use for memory allocations
128  *
129  * Serialised using USB_SERIAL_WRITE_BUSY flag.
130  *
131  * Return: Zero on success or if busy, otherwise a negative errno value.
132  */
133 int usb_serial_generic_write_start(struct usb_serial_port *port,
134 							gfp_t mem_flags)
135 {
136 	struct urb *urb;
137 	int count, result;
138 	unsigned long flags;
139 	int i;
140 
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;
149 	}
150 	i = (int)find_first_bit(&port->write_urbs_free,
151 						ARRAY_SIZE(port->write_urbs));
152 	spin_unlock_irqrestore(&port->lock, flags);
153 
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);
163 
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);
173 
174 		clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
175 		return result;
176 	}
177 
178 	goto retry;	/* try sending off another urb */
179 }
180 EXPORT_SYMBOL_GPL(usb_serial_generic_write_start);
181 
182 /**
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
188  *
189  * Return: The number of characters buffered, which may be anything from
190  * zero to @count, or a negative errno value.
191  */
192 int usb_serial_generic_write(struct tty_struct *tty,
193 	struct usb_serial_port *port, const unsigned char *buf, int count)
194 {
195 	int result;
196 
197 	if (!port->bulk_out_size)
198 		return -ENODEV;
199 
200 	if (!count)
201 		return 0;
202 
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;
207 
208 	return count;
209 }
210 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
211 
212 int usb_serial_generic_write_room(struct tty_struct *tty)
213 {
214 	struct usb_serial_port *port = tty->driver_data;
215 	unsigned long flags;
216 	int room;
217 
218 	if (!port->bulk_out_size)
219 		return 0;
220 
221 	spin_lock_irqsave(&port->lock, flags);
222 	room = kfifo_avail(&port->write_fifo);
223 	spin_unlock_irqrestore(&port->lock, flags);
224 
225 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
226 	return room;
227 }
228 
229 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
230 {
231 	struct usb_serial_port *port = tty->driver_data;
232 	unsigned long flags;
233 	int chars;
234 
235 	if (!port->bulk_out_size)
236 		return 0;
237 
238 	spin_lock_irqsave(&port->lock, flags);
239 	chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
240 	spin_unlock_irqrestore(&port->lock, flags);
241 
242 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
243 	return chars;
244 }
245 EXPORT_SYMBOL_GPL(usb_serial_generic_chars_in_buffer);
246 
247 void usb_serial_generic_wait_until_sent(struct tty_struct *tty, long timeout)
248 {
249 	struct usb_serial_port *port = tty->driver_data;
250 	unsigned int bps;
251 	unsigned long period;
252 	unsigned long expire;
253 
254 	bps = tty_get_baud_rate(tty);
255 	if (!bps)
256 		bps = 9600;	/* B0 */
257 	/*
258 	 * Use a poll-period of roughly the time it takes to send one
259 	 * character or at least one jiffy.
260 	 */
261 	period = max_t(unsigned long, (10 * HZ / bps), 1);
262 	if (timeout)
263 		period = min_t(unsigned long, period, timeout);
264 
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;
275 	}
276 }
277 EXPORT_SYMBOL_GPL(usb_serial_generic_wait_until_sent);
278 
279 static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
280 						int index, gfp_t mem_flags)
281 {
282 	int res;
283 
284 	if (!test_and_clear_bit(index, &port->read_urbs_free))
285 		return 0;
286 
287 	dev_dbg(&port->dev, "%s - urb %d\n", __func__, index);
288 
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);
295 		}
296 		set_bit(index, &port->read_urbs_free);
297 		return res;
298 	}
299 
300 	return 0;
301 }
302 
303 int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
304 					gfp_t mem_flags)
305 {
306 	int res;
307 	int i;
308 
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;
313 	}
314 
315 	return 0;
316 err:
317 	for (; i >= 0; --i)
318 		usb_kill_urb(port->read_urbs[i]);
319 
320 	return res;
321 }
322 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
323 
324 void usb_serial_generic_process_read_urb(struct urb *urb)
325 {
326 	struct usb_serial_port *port = urb->context;
327 	char *ch = (char *)urb->transfer_buffer;
328 	int i;
329 
330 	if (!urb->actual_length)
331 		return;
332 	/*
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.
336 	 */
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);
343 		}
344 	}
345 	tty_flip_buffer_push(&port->port);
346 }
347 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
348 
349 void usb_serial_generic_read_bulk_callback(struct urb *urb)
350 {
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;
356 
357 	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
358 		if (urb == port->read_urbs[i])
359 			break;
360 	}
361 	set_bit(i, &port->read_urbs_free);
362 
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;
382 	}
383 
384 	usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
385 	port->serial->type->process_read_urb(urb);
386 
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);
396 	}
397 }
398 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
399 
400 void usb_serial_generic_write_bulk_callback(struct urb *urb)
401 {
402 	unsigned long flags;
403 	struct usb_serial_port *port = urb->context;
404 	int status = urb->status;
405 	int i;
406 
407 	for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) {
408 		if (port->write_urbs[i] == urb)
409 			break;
410 	}
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);
415 
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;
433 	}
434 
435 resubmit:
436 	usb_serial_generic_write_start(port, GFP_ATOMIC);
437 	usb_serial_port_softint(port);
438 }
439 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
440 
441 void usb_serial_generic_throttle(struct tty_struct *tty)
442 {
443 	struct usb_serial_port *port = tty->driver_data;
444 	unsigned long flags;
445 
446 	spin_lock_irqsave(&port->lock, flags);
447 	port->throttle_req = 1;
448 	spin_unlock_irqrestore(&port->lock, flags);
449 }
450 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
451 
452 void usb_serial_generic_unthrottle(struct tty_struct *tty)
453 {
454 	struct usb_serial_port *port = tty->driver_data;
455 	int was_throttled;
456 
457 	spin_lock_irq(&port->lock);
458 	was_throttled = port->throttled;
459 	port->throttled = port->throttle_req = 0;
460 	spin_unlock_irq(&port->lock);
461 
462 	if (was_throttled)
463 		usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
464 }
465 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
466 
467 static bool usb_serial_generic_msr_changed(struct tty_struct *tty,
468 				unsigned long arg, struct async_icount *cprev)
469 {
470 	struct usb_serial_port *port = tty->driver_data;
471 	struct async_icount cnow;
472 	unsigned long flags;
473 	bool ret;
474 
475 	/*
476 	 * Use tty-port initialised flag to detect all hangups including the
477 	 * one generated at USB-device disconnect.
478 	 */
479 	if (!tty_port_initialized(&port->port))
480 		return true;
481 
482 	spin_lock_irqsave(&port->lock, flags);
483 	cnow = port->icount;				/* atomic copy*/
484 	spin_unlock_irqrestore(&port->lock, flags);
485 
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));
490 
491 	*cprev = cnow;
492 
493 	return ret;
494 }
495 
496 int usb_serial_generic_tiocmiwait(struct tty_struct *tty, unsigned long arg)
497 {
498 	struct usb_serial_port *port = tty->driver_data;
499 	struct async_icount cnow;
500 	unsigned long flags;
501 	int ret;
502 
503 	spin_lock_irqsave(&port->lock, flags);
504 	cnow = port->icount;				/* atomic copy */
505 	spin_unlock_irqrestore(&port->lock, flags);
506 
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;
511 
512 	return ret;
513 }
514 EXPORT_SYMBOL_GPL(usb_serial_generic_tiocmiwait);
515 
516 int usb_serial_generic_get_icount(struct tty_struct *tty,
517 					struct serial_icounter_struct *icount)
518 {
519 	struct usb_serial_port *port = tty->driver_data;
520 	struct async_icount cnow;
521 	unsigned long flags;
522 
523 	spin_lock_irqsave(&port->lock, flags);
524 	cnow = port->icount;				/* atomic copy */
525 	spin_unlock_irqrestore(&port->lock, flags);
526 
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;
538 
539 	return 0;
540 }
541 EXPORT_SYMBOL_GPL(usb_serial_generic_get_icount);
542 
543 #ifdef CONFIG_MAGIC_SYSRQ
544 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
545 {
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;
551 		}
552 		port->sysrq = 0;
553 	}
554 	return 0;
555 }
556 #else
557 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
558 {
559 	return 0;
560 }
561 #endif
562 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
563 
564 int usb_serial_handle_break(struct usb_serial_port *port)
565 {
566 	if (!port->sysrq) {
567 		port->sysrq = jiffies + HZ*5;
568 		return 1;
569 	}
570 	port->sysrq = 0;
571 	return 0;
572 }
573 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
574 
575 /**
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
580  */
581 void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
582 				struct tty_struct *tty, unsigned int status)
583 {
584 	struct tty_port *port = &usb_port->port;
585 
586 	dev_dbg(&usb_port->dev, "%s - status %d\n", __func__, status);
587 
588 	if (tty) {
589 		struct tty_ldisc *ld = tty_ldisc_ref(tty);
590 
591 		if (ld) {
592 			if (ld->ops->dcd_change)
593 				ld->ops->dcd_change(tty, status);
594 			tty_ldisc_deref(ld);
595 		}
596 	}
597 
598 	if (status)
599 		wake_up_interruptible(&port->open_wait);
600 	else if (tty && !C_CLOCAL(tty))
601 		tty_hangup(tty);
602 }
603 EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
604 
605 int usb_serial_generic_resume(struct usb_serial *serial)
606 {
607 	struct usb_serial_port *port;
608 	int i, c = 0, r;
609 
610 	for (i = 0; i < serial->num_ports; i++) {
611 		port = serial->port[i];
612 		if (!tty_port_initialized(&port->port))
613 			continue;
614 
615 		if (port->bulk_in_size) {
616 			r = usb_serial_generic_submit_read_urbs(port,
617 								GFP_NOIO);
618 			if (r < 0)
619 				c++;
620 		}
621 
622 		if (port->bulk_out_size) {
623 			r = usb_serial_generic_write_start(port, GFP_NOIO);
624 			if (r < 0)
625 				c++;
626 		}
627 	}
628 
629 	return c ? -EIO : 0;
630 }
631 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
632