xref: /linux/drivers/usb/serial/usb-serial.c (revision 2a31b5378c6db9c4a8d399e5d10d32cd4b732bef)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * USB Serial Converter driver
4  *
5  * Copyright (C) 2009 - 2013 Johan Hovold (jhovold@gmail.com)
6  * Copyright (C) 1999 - 2012 Greg Kroah-Hartman (greg@kroah.com)
7  * Copyright (C) 2000 Peter Berger (pberger@brimson.com)
8  * Copyright (C) 2000 Al Borchers (borchers@steinerpoint.com)
9  *
10  * This driver was originally based on the ACM driver by Armin Fuerst (which was
11  * based on a driver by Brad Keryan)
12  *
13  * See Documentation/usb/usb-serial.rst for more information on using this
14  * driver
15  */
16 
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/tty.h>
24 #include <linux/tty_driver.h>
25 #include <linux/module.h>
26 #include <linux/seq_file.h>
27 #include <linux/spinlock.h>
28 #include <linux/mutex.h>
29 #include <linux/list.h>
30 #include <linux/serial.h>
31 #include <linux/usb.h>
32 #include <linux/usb/serial.h>
33 #include <linux/kfifo.h>
34 #include <linux/idr.h>
35 
36 #define DRIVER_AUTHOR "Greg Kroah-Hartman <gregkh@linuxfoundation.org>"
37 #define DRIVER_DESC "USB Serial Driver core"
38 
39 #define USB_SERIAL_TTY_MAJOR	188
40 #define USB_SERIAL_TTY_MINORS	512	/* should be enough for a while */
41 
42 /* There is no MODULE_DEVICE_TABLE for usbserial.c.  Instead
43    the MODULE_DEVICE_TABLE declarations in each serial driver
44    cause the "hotplug" program to pull in whatever module is necessary
45    via modprobe, and modprobe will load usbserial because the serial
46    drivers depend on it.
47 */
48 
49 static DEFINE_IDR(serial_minors);
50 static DEFINE_MUTEX(table_lock);
51 static LIST_HEAD(usb_serial_driver_list);
52 
53 /*
54  * Look up the serial port structure.  If it is found and it hasn't been
55  * disconnected, return with the parent usb_serial structure's disc_mutex held
56  * and its refcount incremented.  Otherwise return NULL.
57  */
58 struct usb_serial_port *usb_serial_port_get_by_minor(unsigned minor)
59 {
60 	struct usb_serial *serial;
61 	struct usb_serial_port *port;
62 
63 	mutex_lock(&table_lock);
64 	port = idr_find(&serial_minors, minor);
65 	if (!port)
66 		goto exit;
67 
68 	serial = port->serial;
69 	mutex_lock(&serial->disc_mutex);
70 	if (serial->disconnected) {
71 		mutex_unlock(&serial->disc_mutex);
72 		port = NULL;
73 	} else {
74 		kref_get(&serial->kref);
75 	}
76 exit:
77 	mutex_unlock(&table_lock);
78 	return port;
79 }
80 
81 static int allocate_minors(struct usb_serial *serial, int num_ports)
82 {
83 	struct usb_serial_port *port;
84 	unsigned int i, j;
85 	int minor;
86 
87 	dev_dbg(&serial->interface->dev, "%s %d\n", __func__, num_ports);
88 
89 	mutex_lock(&table_lock);
90 	for (i = 0; i < num_ports; ++i) {
91 		port = serial->port[i];
92 		minor = idr_alloc(&serial_minors, port, 0,
93 					USB_SERIAL_TTY_MINORS, GFP_KERNEL);
94 		if (minor < 0)
95 			goto error;
96 		port->minor = minor;
97 		port->port_number = i;
98 	}
99 	serial->minors_reserved = 1;
100 	mutex_unlock(&table_lock);
101 	return 0;
102 error:
103 	/* unwind the already allocated minors */
104 	for (j = 0; j < i; ++j)
105 		idr_remove(&serial_minors, serial->port[j]->minor);
106 	mutex_unlock(&table_lock);
107 	return minor;
108 }
109 
110 static void release_minors(struct usb_serial *serial)
111 {
112 	int i;
113 
114 	mutex_lock(&table_lock);
115 	for (i = 0; i < serial->num_ports; ++i)
116 		idr_remove(&serial_minors, serial->port[i]->minor);
117 	mutex_unlock(&table_lock);
118 	serial->minors_reserved = 0;
119 }
120 
121 int usb_serial_claim_interface(struct usb_serial *serial, struct usb_interface *intf)
122 {
123 	struct usb_driver *driver = serial->type->usb_driver;
124 	int ret;
125 
126 	if (serial->sibling)
127 		return -EBUSY;
128 
129 	ret = usb_driver_claim_interface(driver, intf, serial);
130 	if (ret) {
131 		dev_err(&serial->interface->dev,
132 				"failed to claim sibling interface: %d\n", ret);
133 		return ret;
134 	}
135 
136 	serial->sibling = intf;
137 
138 	return 0;
139 }
140 EXPORT_SYMBOL_GPL(usb_serial_claim_interface);
141 
142 static void release_sibling(struct usb_serial *serial, struct usb_interface *intf)
143 {
144 	struct usb_driver *driver = serial->type->usb_driver;
145 	struct usb_interface *sibling;
146 
147 	if (!serial->sibling)
148 		return;
149 
150 	if (intf == serial->sibling)
151 		sibling = serial->interface;
152 	else
153 		sibling = serial->sibling;
154 
155 	usb_set_intfdata(sibling, NULL);
156 	usb_driver_release_interface(driver, sibling);
157 }
158 
159 static void destroy_serial(struct kref *kref)
160 {
161 	struct usb_serial *serial;
162 	struct usb_serial_port *port;
163 	int i;
164 
165 	serial = to_usb_serial(kref);
166 
167 	/* return the minor range that this device had */
168 	if (serial->minors_reserved)
169 		release_minors(serial);
170 
171 	if (serial->attached && serial->type->release)
172 		serial->type->release(serial);
173 
174 	/* Now that nothing is using the ports, they can be freed */
175 	for (i = 0; i < serial->num_port_pointers; ++i) {
176 		port = serial->port[i];
177 		if (port) {
178 			port->serial = NULL;
179 			put_device(&port->dev);
180 		}
181 	}
182 
183 	usb_put_intf(serial->interface);
184 	usb_put_dev(serial->dev);
185 	kfree(serial);
186 }
187 
188 void usb_serial_put(struct usb_serial *serial)
189 {
190 	kref_put(&serial->kref, destroy_serial);
191 }
192 
193 /*****************************************************************************
194  * Driver tty interface functions
195  *****************************************************************************/
196 
197 /**
198  * serial_install - install tty
199  * @driver: the driver (USB in our case)
200  * @tty: the tty being created
201  *
202  * Initialise the termios structure for this tty.  We use the default
203  * USB serial settings but permit them to be overridden by
204  * serial->type->init_termios on first open.
205  *
206  * This is the first place a new tty gets used.  Hence this is where we
207  * acquire references to the usb_serial structure and the driver module,
208  * where we store a pointer to the port.  All these actions are reversed
209  * in serial_cleanup().
210  */
211 static int serial_install(struct tty_driver *driver, struct tty_struct *tty)
212 {
213 	int idx = tty->index;
214 	struct usb_serial *serial;
215 	struct usb_serial_port *port;
216 	bool init_termios;
217 	int retval = -ENODEV;
218 
219 	port = usb_serial_port_get_by_minor(idx);
220 	if (!port)
221 		return retval;
222 
223 	serial = port->serial;
224 	if (!try_module_get(serial->type->driver.owner))
225 		goto err_put_serial;
226 
227 	init_termios = (driver->termios[idx] == NULL);
228 
229 	retval = tty_standard_install(driver, tty);
230 	if (retval)
231 		goto err_put_module;
232 
233 	mutex_unlock(&serial->disc_mutex);
234 
235 	/* allow the driver to update the initial settings */
236 	if (init_termios && serial->type->init_termios)
237 		serial->type->init_termios(tty);
238 
239 	tty->driver_data = port;
240 
241 	return retval;
242 
243 err_put_module:
244 	module_put(serial->type->driver.owner);
245 err_put_serial:
246 	usb_serial_put(serial);
247 	mutex_unlock(&serial->disc_mutex);
248 	return retval;
249 }
250 
251 static int serial_port_activate(struct tty_port *tport, struct tty_struct *tty)
252 {
253 	struct usb_serial_port *port =
254 		container_of(tport, struct usb_serial_port, port);
255 	struct usb_serial *serial = port->serial;
256 	int retval;
257 
258 	mutex_lock(&serial->disc_mutex);
259 	if (serial->disconnected) {
260 		retval = -ENODEV;
261 		goto out_unlock;
262 	}
263 
264 	retval = usb_autopm_get_interface(serial->interface);
265 	if (retval)
266 		goto out_unlock;
267 
268 	retval = port->serial->type->open(tty, port);
269 	if (retval)
270 		usb_autopm_put_interface(serial->interface);
271 out_unlock:
272 	mutex_unlock(&serial->disc_mutex);
273 
274 	if (retval < 0)
275 		retval = usb_translate_errors(retval);
276 
277 	return retval;
278 }
279 
280 static int serial_open(struct tty_struct *tty, struct file *filp)
281 {
282 	struct usb_serial_port *port = tty->driver_data;
283 
284 	dev_dbg(&port->dev, "%s\n", __func__);
285 
286 	return tty_port_open(&port->port, tty, filp);
287 }
288 
289 /**
290  * serial_port_shutdown - shut down hardware
291  * @tport: tty port to shut down
292  *
293  * Shut down a USB serial port. Serialized against activate by the
294  * tport mutex and kept to matching open/close pairs
295  * of calls by the tty-port initialized flag.
296  *
297  * Not called if tty is console.
298  */
299 static void serial_port_shutdown(struct tty_port *tport)
300 {
301 	struct usb_serial_port *port =
302 		container_of(tport, struct usb_serial_port, port);
303 	struct usb_serial_driver *drv = port->serial->type;
304 
305 	if (drv->close)
306 		drv->close(port);
307 
308 	usb_autopm_put_interface(port->serial->interface);
309 }
310 
311 static void serial_hangup(struct tty_struct *tty)
312 {
313 	struct usb_serial_port *port = tty->driver_data;
314 
315 	dev_dbg(&port->dev, "%s\n", __func__);
316 
317 	tty_port_hangup(&port->port);
318 }
319 
320 static void serial_close(struct tty_struct *tty, struct file *filp)
321 {
322 	struct usb_serial_port *port = tty->driver_data;
323 
324 	dev_dbg(&port->dev, "%s\n", __func__);
325 
326 	tty_port_close(&port->port, tty, filp);
327 }
328 
329 /**
330  * serial_cleanup - free resources post close/hangup
331  * @tty: tty to clean up
332  *
333  * Do the resource freeing and refcount dropping for the port.
334  * Avoid freeing the console.
335  *
336  * Called asynchronously after the last tty kref is dropped.
337  */
338 static void serial_cleanup(struct tty_struct *tty)
339 {
340 	struct usb_serial_port *port = tty->driver_data;
341 	struct usb_serial *serial;
342 	struct module *owner;
343 
344 	dev_dbg(&port->dev, "%s\n", __func__);
345 
346 	/* The console is magical.  Do not hang up the console hardware
347 	 * or there will be tears.
348 	 */
349 	if (port->port.console)
350 		return;
351 
352 	tty->driver_data = NULL;
353 
354 	serial = port->serial;
355 	owner = serial->type->driver.owner;
356 
357 	usb_serial_put(serial);
358 	module_put(owner);
359 }
360 
361 static ssize_t serial_write(struct tty_struct *tty, const u8 *buf, size_t count)
362 {
363 	struct usb_serial_port *port = tty->driver_data;
364 	int retval = -ENODEV;
365 
366 	if (port->serial->dev->state == USB_STATE_NOTATTACHED)
367 		goto exit;
368 
369 	dev_dbg(&port->dev, "%s - %zu byte(s)\n", __func__, count);
370 
371 	retval = port->serial->type->write(tty, port, buf, count);
372 	if (retval < 0)
373 		retval = usb_translate_errors(retval);
374 exit:
375 	return retval;
376 }
377 
378 static unsigned int serial_write_room(struct tty_struct *tty)
379 {
380 	struct usb_serial_port *port = tty->driver_data;
381 
382 	dev_dbg(&port->dev, "%s\n", __func__);
383 
384 	return port->serial->type->write_room(tty);
385 }
386 
387 static unsigned int serial_chars_in_buffer(struct tty_struct *tty)
388 {
389 	struct usb_serial_port *port = tty->driver_data;
390 	struct usb_serial *serial = port->serial;
391 
392 	dev_dbg(&port->dev, "%s\n", __func__);
393 
394 	if (serial->disconnected)
395 		return 0;
396 
397 	return serial->type->chars_in_buffer(tty);
398 }
399 
400 static void serial_wait_until_sent(struct tty_struct *tty, int timeout)
401 {
402 	struct usb_serial_port *port = tty->driver_data;
403 
404 	dev_dbg(&port->dev, "%s\n", __func__);
405 
406 	if (!port->serial->type->wait_until_sent)
407 		return;
408 
409 	port->serial->type->wait_until_sent(tty, timeout);
410 }
411 
412 static void serial_throttle(struct tty_struct *tty)
413 {
414 	struct usb_serial_port *port = tty->driver_data;
415 
416 	dev_dbg(&port->dev, "%s\n", __func__);
417 
418 	if (port->serial->type->throttle)
419 		port->serial->type->throttle(tty);
420 }
421 
422 static void serial_unthrottle(struct tty_struct *tty)
423 {
424 	struct usb_serial_port *port = tty->driver_data;
425 
426 	dev_dbg(&port->dev, "%s\n", __func__);
427 
428 	if (port->serial->type->unthrottle)
429 		port->serial->type->unthrottle(tty);
430 }
431 
432 static int serial_get_serial(struct tty_struct *tty, struct serial_struct *ss)
433 {
434 	struct usb_serial_port *port = tty->driver_data;
435 	struct tty_port *tport = &port->port;
436 	unsigned int close_delay, closing_wait;
437 	int ret = 0;
438 
439 	mutex_lock(&tport->mutex);
440 	if (tty_io_error(tty)) {
441 		ret = -EIO;
442 		goto out_unlock;
443 	}
444 
445 	close_delay = jiffies_to_msecs(tport->close_delay) / 10;
446 	closing_wait = tport->closing_wait;
447 	if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
448 		closing_wait = jiffies_to_msecs(closing_wait) / 10;
449 
450 	ss->line = port->minor;
451 	ss->close_delay = close_delay;
452 	ss->closing_wait = closing_wait;
453 
454 	if (port->serial->type->get_serial)
455 		port->serial->type->get_serial(tty, ss);
456 out_unlock:
457 	mutex_unlock(&tport->mutex);
458 
459 	return ret;
460 }
461 
462 static int serial_set_serial(struct tty_struct *tty, struct serial_struct *ss)
463 {
464 	struct usb_serial_port *port = tty->driver_data;
465 	struct tty_port *tport = &port->port;
466 	unsigned int close_delay, closing_wait;
467 	int ret = 0;
468 
469 	close_delay = msecs_to_jiffies(ss->close_delay * 10);
470 	closing_wait = ss->closing_wait;
471 	if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
472 		closing_wait = msecs_to_jiffies(closing_wait * 10);
473 
474 	mutex_lock(&tport->mutex);
475 	if (tty_io_error(tty)) {
476 		ret = -EIO;
477 		goto out_unlock;
478 	}
479 
480 	if (!capable(CAP_SYS_ADMIN)) {
481 		if (close_delay != tport->close_delay ||
482 				closing_wait != tport->closing_wait) {
483 			ret = -EPERM;
484 			goto out_unlock;
485 		}
486 	}
487 
488 	if (port->serial->type->set_serial) {
489 		ret = port->serial->type->set_serial(tty, ss);
490 		if (ret)
491 			goto out_unlock;
492 	}
493 
494 	tport->close_delay = close_delay;
495 	tport->closing_wait = closing_wait;
496 out_unlock:
497 	mutex_unlock(&tport->mutex);
498 
499 	return ret;
500 }
501 
502 static int serial_ioctl(struct tty_struct *tty,
503 					unsigned int cmd, unsigned long arg)
504 {
505 	struct usb_serial_port *port = tty->driver_data;
506 	struct tty_port *tport = &port->port;
507 	int retval = -ENOIOCTLCMD;
508 
509 	dev_dbg(&port->dev, "%s - cmd 0x%04x\n", __func__, cmd);
510 
511 	switch (cmd) {
512 	case TIOCMIWAIT:
513 		if (port->serial->type->tiocmiwait)
514 			retval = port->serial->type->tiocmiwait(tty, arg);
515 		break;
516 	default:
517 		if (!port->serial->type->ioctl)
518 			break;
519 
520 		if (cmd == TIOCSRS485)
521 			down_write(&tty->termios_rwsem);
522 
523 		mutex_lock(&tport->mutex);
524 		if (tty_io_error(tty))
525 			retval = -EIO;
526 		else
527 			retval = port->serial->type->ioctl(tty, cmd, arg);
528 		mutex_unlock(&tport->mutex);
529 
530 		if (cmd == TIOCSRS485)
531 			up_write(&tty->termios_rwsem);
532 	}
533 
534 	return retval;
535 }
536 
537 static void serial_set_termios(struct tty_struct *tty,
538 		               const struct ktermios *old)
539 {
540 	struct usb_serial_port *port = tty->driver_data;
541 	struct tty_port *tport = &port->port;
542 
543 	dev_dbg(&port->dev, "%s\n", __func__);
544 
545 	if (!port->serial->type->set_termios) {
546 		tty_termios_copy_hw(&tty->termios, old);
547 		return;
548 	}
549 
550 	mutex_lock(&tport->mutex);
551 	if (!tty_io_error(tty))
552 		port->serial->type->set_termios(tty, port, old);
553 	mutex_unlock(&tport->mutex);
554 }
555 
556 static int serial_break(struct tty_struct *tty, int break_state)
557 {
558 	struct usb_serial_port *port = tty->driver_data;
559 	struct tty_port *tport = &port->port;
560 	int ret;
561 
562 	dev_dbg(&port->dev, "%s\n", __func__);
563 
564 	if (!port->serial->type->break_ctl)
565 		return -ENOTTY;
566 
567 	mutex_lock(&tport->mutex);
568 	if (tty_io_error(tty))
569 		ret = -EIO;
570 	else
571 		ret = port->serial->type->break_ctl(tty, break_state);
572 	mutex_unlock(&tport->mutex);
573 
574 	return ret;
575 }
576 
577 static int serial_proc_show(struct seq_file *m, void *v)
578 {
579 	struct usb_serial *serial;
580 	struct usb_serial_port *port;
581 	int i;
582 	char tmp[40];
583 
584 	seq_puts(m, "usbserinfo:1.0 driver:2.0\n");
585 	for (i = 0; i < USB_SERIAL_TTY_MINORS; ++i) {
586 		port = usb_serial_port_get_by_minor(i);
587 		if (port == NULL)
588 			continue;
589 		serial = port->serial;
590 
591 		seq_printf(m, "%d:", i);
592 		if (serial->type->driver.owner)
593 			seq_printf(m, " module:%s",
594 				module_name(serial->type->driver.owner));
595 		seq_printf(m, " name:\"%s\"",
596 				serial->type->description);
597 		seq_printf(m, " vendor:%04x product:%04x",
598 			le16_to_cpu(serial->dev->descriptor.idVendor),
599 			le16_to_cpu(serial->dev->descriptor.idProduct));
600 		seq_printf(m, " num_ports:%d", serial->num_ports);
601 		seq_printf(m, " port:%d", port->port_number);
602 		usb_make_path(serial->dev, tmp, sizeof(tmp));
603 		seq_printf(m, " path:%s", tmp);
604 
605 		seq_putc(m, '\n');
606 		usb_serial_put(serial);
607 		mutex_unlock(&serial->disc_mutex);
608 	}
609 	return 0;
610 }
611 
612 static int serial_tiocmget(struct tty_struct *tty)
613 {
614 	struct usb_serial_port *port = tty->driver_data;
615 	struct tty_port *tport = &port->port;
616 	int ret;
617 
618 	dev_dbg(&port->dev, "%s\n", __func__);
619 
620 	if (!port->serial->type->tiocmget)
621 		return -ENOTTY;
622 
623 	mutex_lock(&tport->mutex);
624 	if (tty_io_error(tty))
625 		ret = -EIO;
626 	else
627 		ret = port->serial->type->tiocmget(tty);
628 	mutex_unlock(&tport->mutex);
629 
630 	return ret;
631 }
632 
633 static int serial_tiocmset(struct tty_struct *tty,
634 			    unsigned int set, unsigned int clear)
635 {
636 	struct usb_serial_port *port = tty->driver_data;
637 	struct tty_port *tport = &port->port;
638 	int ret;
639 
640 	dev_dbg(&port->dev, "%s\n", __func__);
641 
642 	if (!port->serial->type->tiocmset)
643 		return -ENOTTY;
644 
645 	mutex_lock(&tport->mutex);
646 	if (tty_io_error(tty))
647 		ret = -EIO;
648 	else
649 		ret = port->serial->type->tiocmset(tty, set, clear);
650 	mutex_unlock(&tport->mutex);
651 
652 	return ret;
653 }
654 
655 static int serial_get_icount(struct tty_struct *tty,
656 				struct serial_icounter_struct *icount)
657 {
658 	struct usb_serial_port *port = tty->driver_data;
659 
660 	dev_dbg(&port->dev, "%s\n", __func__);
661 
662 	if (port->serial->type->get_icount)
663 		return port->serial->type->get_icount(tty, icount);
664 	return -ENOTTY;
665 }
666 
667 /*
668  * We would be calling tty_wakeup here, but unfortunately some line
669  * disciplines have an annoying habit of calling tty->write from
670  * the write wakeup callback (e.g. n_hdlc.c).
671  */
672 void usb_serial_port_softint(struct usb_serial_port *port)
673 {
674 	schedule_work(&port->work);
675 }
676 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
677 
678 static void usb_serial_port_work(struct work_struct *work)
679 {
680 	struct usb_serial_port *port =
681 		container_of(work, struct usb_serial_port, work);
682 
683 	tty_port_tty_wakeup(&port->port);
684 }
685 
686 static void usb_serial_port_poison_urbs(struct usb_serial_port *port)
687 {
688 	int i;
689 
690 	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
691 		usb_poison_urb(port->read_urbs[i]);
692 	for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
693 		usb_poison_urb(port->write_urbs[i]);
694 
695 	usb_poison_urb(port->interrupt_in_urb);
696 	usb_poison_urb(port->interrupt_out_urb);
697 }
698 
699 static void usb_serial_port_unpoison_urbs(struct usb_serial_port *port)
700 {
701 	int i;
702 
703 	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
704 		usb_unpoison_urb(port->read_urbs[i]);
705 	for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
706 		usb_unpoison_urb(port->write_urbs[i]);
707 
708 	usb_unpoison_urb(port->interrupt_in_urb);
709 	usb_unpoison_urb(port->interrupt_out_urb);
710 }
711 
712 static void usb_serial_port_release(struct device *dev)
713 {
714 	struct usb_serial_port *port = to_usb_serial_port(dev);
715 	int i;
716 
717 	dev_dbg(dev, "%s\n", __func__);
718 
719 	usb_free_urb(port->interrupt_in_urb);
720 	usb_free_urb(port->interrupt_out_urb);
721 	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
722 		usb_free_urb(port->read_urbs[i]);
723 		kfree(port->bulk_in_buffers[i]);
724 	}
725 	for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) {
726 		usb_free_urb(port->write_urbs[i]);
727 		kfree(port->bulk_out_buffers[i]);
728 	}
729 	kfifo_free(&port->write_fifo);
730 	kfree(port->interrupt_in_buffer);
731 	kfree(port->interrupt_out_buffer);
732 	tty_port_destroy(&port->port);
733 	kfree(port);
734 }
735 
736 static struct usb_serial *create_serial(struct usb_device *dev,
737 					struct usb_interface *interface,
738 					struct usb_serial_driver *driver)
739 {
740 	struct usb_serial *serial;
741 
742 	serial = kzalloc_obj(*serial);
743 	if (!serial)
744 		return NULL;
745 	serial->dev = usb_get_dev(dev);
746 	serial->type = driver;
747 	serial->interface = usb_get_intf(interface);
748 	kref_init(&serial->kref);
749 	mutex_init(&serial->disc_mutex);
750 	serial->minors_reserved = 0;
751 
752 	return serial;
753 }
754 
755 static const struct usb_device_id *match_dynamic_id(struct usb_interface *intf,
756 					    struct usb_serial_driver *drv)
757 {
758 	struct usb_dynid *dynid;
759 
760 	guard(mutex)(&usb_dynids_lock);
761 	list_for_each_entry(dynid, &drv->dynids.list, node) {
762 		if (usb_match_one_id(intf, &dynid->id)) {
763 			return &dynid->id;
764 		}
765 	}
766 	return NULL;
767 }
768 
769 static const struct usb_device_id *get_iface_id(struct usb_serial_driver *drv,
770 						struct usb_interface *intf)
771 {
772 	const struct usb_device_id *id;
773 
774 	id = usb_match_id(intf, drv->id_table);
775 	if (id) {
776 		dev_dbg(&intf->dev, "static descriptor matches\n");
777 		goto exit;
778 	}
779 	id = match_dynamic_id(intf, drv);
780 	if (id)
781 		dev_dbg(&intf->dev, "dynamic descriptor matches\n");
782 exit:
783 	return id;
784 }
785 
786 /* Caller must hold table_lock */
787 static struct usb_serial_driver *search_serial_device(
788 					struct usb_interface *iface)
789 {
790 	const struct usb_device_id *id = NULL;
791 	struct usb_serial_driver *drv;
792 	struct usb_driver *driver = to_usb_driver(iface->dev.driver);
793 
794 	/* Check if the usb id matches a known device */
795 	list_for_each_entry(drv, &usb_serial_driver_list, driver_list) {
796 		if (drv->usb_driver == driver)
797 			id = get_iface_id(drv, iface);
798 		if (id)
799 			return drv;
800 	}
801 
802 	return NULL;
803 }
804 
805 static bool serial_port_carrier_raised(struct tty_port *port)
806 {
807 	struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
808 	struct usb_serial_driver *drv = p->serial->type;
809 
810 	if (drv->carrier_raised)
811 		return drv->carrier_raised(p);
812 	/* No carrier control - don't block */
813 	return true;
814 }
815 
816 static void serial_port_dtr_rts(struct tty_port *port, bool on)
817 {
818 	struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
819 	struct usb_serial_driver *drv = p->serial->type;
820 
821 	if (drv->dtr_rts)
822 		drv->dtr_rts(p, on);
823 }
824 
825 static ssize_t port_number_show(struct device *dev,
826 				struct device_attribute *attr, char *buf)
827 {
828 	struct usb_serial_port *port = to_usb_serial_port(dev);
829 
830 	return sprintf(buf, "%u\n", port->port_number);
831 }
832 static DEVICE_ATTR_RO(port_number);
833 
834 static struct attribute *usb_serial_port_attrs[] = {
835 	&dev_attr_port_number.attr,
836 	NULL
837 };
838 ATTRIBUTE_GROUPS(usb_serial_port);
839 
840 static const struct tty_port_operations serial_port_ops = {
841 	.carrier_raised		= serial_port_carrier_raised,
842 	.dtr_rts		= serial_port_dtr_rts,
843 	.activate		= serial_port_activate,
844 	.shutdown		= serial_port_shutdown,
845 };
846 
847 static void store_endpoint(struct usb_serial *serial,
848 					struct usb_serial_endpoints *epds,
849 					struct usb_endpoint_descriptor *epd)
850 {
851 	struct device *dev = &serial->interface->dev;
852 	u8 addr = epd->bEndpointAddress;
853 
854 	if (usb_endpoint_is_bulk_in(epd)) {
855 		if (epds->num_bulk_in == ARRAY_SIZE(epds->bulk_in))
856 			return;
857 		dev_dbg(dev, "found bulk in endpoint %02x\n", addr);
858 		epds->bulk_in[epds->num_bulk_in++] = epd;
859 	} else if (usb_endpoint_is_bulk_out(epd)) {
860 		if (epds->num_bulk_out == ARRAY_SIZE(epds->bulk_out))
861 			return;
862 		dev_dbg(dev, "found bulk out endpoint %02x\n", addr);
863 		epds->bulk_out[epds->num_bulk_out++] = epd;
864 	} else if (usb_endpoint_is_int_in(epd)) {
865 		if (epds->num_interrupt_in == ARRAY_SIZE(epds->interrupt_in))
866 			return;
867 		dev_dbg(dev, "found interrupt in endpoint %02x\n", addr);
868 		epds->interrupt_in[epds->num_interrupt_in++] = epd;
869 	} else if (usb_endpoint_is_int_out(epd)) {
870 		if (epds->num_interrupt_out == ARRAY_SIZE(epds->interrupt_out))
871 			return;
872 		dev_dbg(dev, "found interrupt out endpoint %02x\n", addr);
873 		epds->interrupt_out[epds->num_interrupt_out++] = epd;
874 	}
875 }
876 
877 static void find_endpoints(struct usb_serial *serial,
878 					struct usb_serial_endpoints *epds,
879 					struct usb_interface *intf)
880 {
881 	struct usb_host_interface *iface_desc;
882 	struct usb_endpoint_descriptor *epd;
883 	unsigned int i;
884 
885 	iface_desc = intf->cur_altsetting;
886 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
887 		epd = &iface_desc->endpoint[i].desc;
888 		store_endpoint(serial, epds, epd);
889 	}
890 }
891 
892 static int setup_port_bulk_in(struct usb_serial_port *port,
893 					struct usb_endpoint_descriptor *epd)
894 {
895 	struct usb_serial_driver *type = port->serial->type;
896 	struct usb_device *udev = port->serial->dev;
897 	int buffer_size;
898 	int i;
899 
900 	buffer_size = max_t(int, type->bulk_in_size, usb_endpoint_maxp(epd));
901 	port->bulk_in_size = buffer_size;
902 	port->bulk_in_endpointAddress = epd->bEndpointAddress;
903 
904 	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
905 		set_bit(i, &port->read_urbs_free);
906 		port->read_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
907 		if (!port->read_urbs[i])
908 			return -ENOMEM;
909 		port->bulk_in_buffers[i] = kmalloc(buffer_size, GFP_KERNEL);
910 		if (!port->bulk_in_buffers[i])
911 			return -ENOMEM;
912 		usb_fill_bulk_urb(port->read_urbs[i], udev,
913 				usb_rcvbulkpipe(udev, epd->bEndpointAddress),
914 				port->bulk_in_buffers[i], buffer_size,
915 				type->read_bulk_callback, port);
916 	}
917 
918 	port->read_urb = port->read_urbs[0];
919 	port->bulk_in_buffer = port->bulk_in_buffers[0];
920 
921 	return 0;
922 }
923 
924 static int setup_port_bulk_out(struct usb_serial_port *port,
925 					struct usb_endpoint_descriptor *epd)
926 {
927 	struct usb_serial_driver *type = port->serial->type;
928 	struct usb_device *udev = port->serial->dev;
929 	int buffer_size;
930 	int i;
931 
932 	if (kfifo_alloc(&port->write_fifo, PAGE_SIZE, GFP_KERNEL))
933 		return -ENOMEM;
934 	if (type->bulk_out_size)
935 		buffer_size = type->bulk_out_size;
936 	else
937 		buffer_size = usb_endpoint_maxp(epd);
938 	port->bulk_out_size = buffer_size;
939 	port->bulk_out_endpointAddress = epd->bEndpointAddress;
940 
941 	for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) {
942 		set_bit(i, &port->write_urbs_free);
943 		port->write_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
944 		if (!port->write_urbs[i])
945 			return -ENOMEM;
946 		port->bulk_out_buffers[i] = kmalloc(buffer_size, GFP_KERNEL);
947 		if (!port->bulk_out_buffers[i])
948 			return -ENOMEM;
949 		usb_fill_bulk_urb(port->write_urbs[i], udev,
950 				usb_sndbulkpipe(udev, epd->bEndpointAddress),
951 				port->bulk_out_buffers[i], buffer_size,
952 				type->write_bulk_callback, port);
953 	}
954 
955 	port->write_urb = port->write_urbs[0];
956 	port->bulk_out_buffer = port->bulk_out_buffers[0];
957 
958 	return 0;
959 }
960 
961 static int setup_port_interrupt_in(struct usb_serial_port *port,
962 					struct usb_endpoint_descriptor *epd)
963 {
964 	struct usb_serial_driver *type = port->serial->type;
965 	struct usb_device *udev = port->serial->dev;
966 	int buffer_size;
967 
968 	port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
969 	if (!port->interrupt_in_urb)
970 		return -ENOMEM;
971 	buffer_size = usb_endpoint_maxp(epd);
972 	port->interrupt_in_endpointAddress = epd->bEndpointAddress;
973 	port->interrupt_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
974 	if (!port->interrupt_in_buffer)
975 		return -ENOMEM;
976 	usb_fill_int_urb(port->interrupt_in_urb, udev,
977 			usb_rcvintpipe(udev, epd->bEndpointAddress),
978 			port->interrupt_in_buffer, buffer_size,
979 			type->read_int_callback, port,
980 			epd->bInterval);
981 
982 	return 0;
983 }
984 
985 static int setup_port_interrupt_out(struct usb_serial_port *port,
986 					struct usb_endpoint_descriptor *epd)
987 {
988 	struct usb_serial_driver *type = port->serial->type;
989 	struct usb_device *udev = port->serial->dev;
990 	int buffer_size;
991 
992 	port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
993 	if (!port->interrupt_out_urb)
994 		return -ENOMEM;
995 	buffer_size = usb_endpoint_maxp(epd);
996 	port->interrupt_out_size = buffer_size;
997 	port->interrupt_out_endpointAddress = epd->bEndpointAddress;
998 	port->interrupt_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
999 	if (!port->interrupt_out_buffer)
1000 		return -ENOMEM;
1001 	usb_fill_int_urb(port->interrupt_out_urb, udev,
1002 			usb_sndintpipe(udev, epd->bEndpointAddress),
1003 			port->interrupt_out_buffer, buffer_size,
1004 			type->write_int_callback, port,
1005 			epd->bInterval);
1006 
1007 	return 0;
1008 }
1009 
1010 static int usb_serial_probe(struct usb_interface *interface,
1011 			       const struct usb_device_id *id)
1012 {
1013 	struct device *ddev = &interface->dev;
1014 	struct usb_device *dev = interface_to_usbdev(interface);
1015 	struct usb_serial *serial = NULL;
1016 	struct usb_serial_port *port;
1017 	struct usb_serial_endpoints *epds;
1018 	struct usb_serial_driver *type = NULL;
1019 	int retval;
1020 	int i;
1021 	int num_ports = 0;
1022 	unsigned char max_endpoints;
1023 
1024 	mutex_lock(&table_lock);
1025 	type = search_serial_device(interface);
1026 	if (!type) {
1027 		mutex_unlock(&table_lock);
1028 		dev_dbg(ddev, "none matched\n");
1029 		return -ENODEV;
1030 	}
1031 
1032 	if (!try_module_get(type->driver.owner)) {
1033 		mutex_unlock(&table_lock);
1034 		dev_err(ddev, "module get failed, exiting\n");
1035 		return -EIO;
1036 	}
1037 	mutex_unlock(&table_lock);
1038 
1039 	serial = create_serial(dev, interface, type);
1040 	if (!serial) {
1041 		retval = -ENOMEM;
1042 		goto err_put_module;
1043 	}
1044 
1045 	/* if this device type has a probe function, call it */
1046 	if (type->probe) {
1047 		const struct usb_device_id *id;
1048 
1049 		id = get_iface_id(type, interface);
1050 		retval = type->probe(serial, id);
1051 
1052 		if (retval) {
1053 			dev_dbg(ddev, "sub driver rejected device\n");
1054 			goto err_release_sibling;
1055 		}
1056 	}
1057 
1058 	/* descriptor matches, let's find the endpoints needed */
1059 	epds = kzalloc_obj(*epds);
1060 	if (!epds) {
1061 		retval = -ENOMEM;
1062 		goto err_release_sibling;
1063 	}
1064 
1065 	find_endpoints(serial, epds, interface);
1066 	if (serial->sibling)
1067 		find_endpoints(serial, epds, serial->sibling);
1068 
1069 	if (epds->num_bulk_in < type->num_bulk_in ||
1070 			epds->num_bulk_out < type->num_bulk_out ||
1071 			epds->num_interrupt_in < type->num_interrupt_in ||
1072 			epds->num_interrupt_out < type->num_interrupt_out) {
1073 		dev_err(ddev, "required endpoints missing\n");
1074 		retval = -ENODEV;
1075 		goto err_free_epds;
1076 	}
1077 
1078 	if (type->calc_num_ports) {
1079 		retval = type->calc_num_ports(serial, epds);
1080 		if (retval < 0)
1081 			goto err_free_epds;
1082 		num_ports = retval;
1083 	}
1084 
1085 	if (!num_ports)
1086 		num_ports = type->num_ports;
1087 
1088 	if (num_ports > MAX_NUM_PORTS) {
1089 		dev_warn(ddev, "too many ports requested: %d\n", num_ports);
1090 		num_ports = MAX_NUM_PORTS;
1091 	}
1092 
1093 	serial->num_ports = (unsigned char)num_ports;
1094 	serial->num_bulk_in = epds->num_bulk_in;
1095 	serial->num_bulk_out = epds->num_bulk_out;
1096 	serial->num_interrupt_in = epds->num_interrupt_in;
1097 	serial->num_interrupt_out = epds->num_interrupt_out;
1098 
1099 	/* found all that we need */
1100 	dev_info(ddev, "%s converter detected\n", type->description);
1101 
1102 	/* create our ports, we need as many as the max endpoints */
1103 	/* we don't use num_ports here because some devices have more
1104 	   endpoint pairs than ports */
1105 	max_endpoints = max(epds->num_bulk_in, epds->num_bulk_out);
1106 	max_endpoints = max(max_endpoints, epds->num_interrupt_in);
1107 	max_endpoints = max(max_endpoints, epds->num_interrupt_out);
1108 	max_endpoints = max(max_endpoints, serial->num_ports);
1109 	serial->num_port_pointers = max_endpoints;
1110 
1111 	dev_dbg(ddev, "setting up %d port structure(s)\n", max_endpoints);
1112 	for (i = 0; i < max_endpoints; ++i) {
1113 		port = kzalloc_obj(struct usb_serial_port);
1114 		if (!port) {
1115 			retval = -ENOMEM;
1116 			goto err_free_epds;
1117 		}
1118 		tty_port_init(&port->port);
1119 		port->port.ops = &serial_port_ops;
1120 		port->serial = serial;
1121 		spin_lock_init(&port->lock);
1122 		/* Keep this for private driver use for the moment but
1123 		   should probably go away */
1124 		INIT_WORK(&port->work, usb_serial_port_work);
1125 		serial->port[i] = port;
1126 		port->dev.parent = &interface->dev;
1127 		port->dev.driver = NULL;
1128 		port->dev.bus = &usb_serial_bus_type;
1129 		port->dev.release = &usb_serial_port_release;
1130 		port->dev.groups = usb_serial_port_groups;
1131 		device_initialize(&port->dev);
1132 	}
1133 
1134 	/* set up the endpoint information */
1135 	for (i = 0; i < epds->num_bulk_in; ++i) {
1136 		retval = setup_port_bulk_in(serial->port[i], epds->bulk_in[i]);
1137 		if (retval)
1138 			goto err_free_epds;
1139 	}
1140 
1141 	for (i = 0; i < epds->num_bulk_out; ++i) {
1142 		retval = setup_port_bulk_out(serial->port[i],
1143 				epds->bulk_out[i]);
1144 		if (retval)
1145 			goto err_free_epds;
1146 	}
1147 
1148 	if (serial->type->read_int_callback) {
1149 		for (i = 0; i < epds->num_interrupt_in; ++i) {
1150 			retval = setup_port_interrupt_in(serial->port[i],
1151 					epds->interrupt_in[i]);
1152 			if (retval)
1153 				goto err_free_epds;
1154 		}
1155 	} else if (epds->num_interrupt_in) {
1156 		dev_dbg(ddev, "The device claims to support interrupt in transfers, but read_int_callback is not defined\n");
1157 	}
1158 
1159 	if (serial->type->write_int_callback) {
1160 		for (i = 0; i < epds->num_interrupt_out; ++i) {
1161 			retval = setup_port_interrupt_out(serial->port[i],
1162 					epds->interrupt_out[i]);
1163 			if (retval)
1164 				goto err_free_epds;
1165 		}
1166 	} else if (epds->num_interrupt_out) {
1167 		dev_dbg(ddev, "The device claims to support interrupt out transfers, but write_int_callback is not defined\n");
1168 	}
1169 
1170 	usb_set_intfdata(interface, serial);
1171 
1172 	/* if this device type has an attach function, call it */
1173 	if (type->attach) {
1174 		retval = type->attach(serial);
1175 		if (retval < 0)
1176 			goto err_free_epds;
1177 		serial->attached = 1;
1178 		if (retval > 0) {
1179 			/* quietly accept this device, but don't bind to a
1180 			   serial port as it's about to disappear */
1181 			serial->num_ports = 0;
1182 			goto exit;
1183 		}
1184 	} else {
1185 		serial->attached = 1;
1186 	}
1187 
1188 	retval = allocate_minors(serial, num_ports);
1189 	if (retval) {
1190 		dev_err(ddev, "No more free serial minor numbers\n");
1191 		goto err_free_epds;
1192 	}
1193 
1194 	/* register all of the individual ports with the driver core */
1195 	for (i = 0; i < num_ports; ++i) {
1196 		port = serial->port[i];
1197 		dev_set_name(&port->dev, "ttyUSB%d", port->minor);
1198 		dev_dbg(ddev, "registering %s\n", dev_name(&port->dev));
1199 		device_enable_async_suspend(&port->dev);
1200 
1201 		retval = device_add(&port->dev);
1202 		if (retval)
1203 			dev_err(ddev, "Error registering port device, continuing\n");
1204 	}
1205 
1206 	if (num_ports > 0)
1207 		usb_serial_console_init(serial->port[0]->minor);
1208 exit:
1209 	kfree(epds);
1210 	module_put(type->driver.owner);
1211 	return 0;
1212 
1213 err_free_epds:
1214 	kfree(epds);
1215 err_release_sibling:
1216 	release_sibling(serial, interface);
1217 	usb_serial_put(serial);
1218 err_put_module:
1219 	module_put(type->driver.owner);
1220 
1221 	return retval;
1222 }
1223 
1224 static void usb_serial_disconnect(struct usb_interface *interface)
1225 {
1226 	int i;
1227 	struct usb_serial *serial = usb_get_intfdata(interface);
1228 	struct device *dev = &interface->dev;
1229 	struct usb_serial_port *port;
1230 
1231 	/* sibling interface is cleaning up */
1232 	if (!serial)
1233 		return;
1234 
1235 	usb_serial_console_disconnect(serial);
1236 
1237 	mutex_lock(&serial->disc_mutex);
1238 	/* must set a flag, to signal subdrivers */
1239 	serial->disconnected = 1;
1240 	mutex_unlock(&serial->disc_mutex);
1241 
1242 	for (i = 0; i < serial->num_ports; ++i) {
1243 		port = serial->port[i];
1244 		tty_port_tty_vhangup(&port->port);
1245 		usb_serial_port_poison_urbs(port);
1246 		wake_up_interruptible(&port->port.delta_msr_wait);
1247 		cancel_work_sync(&port->work);
1248 	}
1249 
1250 	if (serial->type->disconnect)
1251 		serial->type->disconnect(serial);
1252 
1253 	for (i = 0; i < serial->num_ports; ++i) {
1254 		port = serial->port[i];
1255 		if (device_is_registered(&port->dev))
1256 			device_del(&port->dev);
1257 	}
1258 
1259 	release_sibling(serial, interface);
1260 
1261 	/* let the last holder of this object cause it to be cleaned up */
1262 	usb_serial_put(serial);
1263 	dev_info(dev, "device disconnected\n");
1264 }
1265 
1266 int usb_serial_suspend(struct usb_interface *intf, pm_message_t message)
1267 {
1268 	struct usb_serial *serial = usb_get_intfdata(intf);
1269 	int i, r;
1270 
1271 	/* suspend when called for first sibling interface */
1272 	if (serial->suspend_count++)
1273 		return 0;
1274 
1275 	/*
1276 	 * serial->type->suspend() MUST return 0 in system sleep context,
1277 	 * otherwise, the resume callback has to recover device from
1278 	 * previous suspend failure.
1279 	 */
1280 	if (serial->type->suspend) {
1281 		r = serial->type->suspend(serial, message);
1282 		if (r < 0) {
1283 			serial->suspend_count--;
1284 			return r;
1285 		}
1286 	}
1287 
1288 	for (i = 0; i < serial->num_ports; ++i)
1289 		usb_serial_port_poison_urbs(serial->port[i]);
1290 
1291 	return 0;
1292 }
1293 EXPORT_SYMBOL(usb_serial_suspend);
1294 
1295 static void usb_serial_unpoison_port_urbs(struct usb_serial *serial)
1296 {
1297 	int i;
1298 
1299 	for (i = 0; i < serial->num_ports; ++i)
1300 		usb_serial_port_unpoison_urbs(serial->port[i]);
1301 }
1302 
1303 int usb_serial_resume(struct usb_interface *intf)
1304 {
1305 	struct usb_serial *serial = usb_get_intfdata(intf);
1306 	int rv;
1307 
1308 	/* resume when called for last sibling interface */
1309 	if (--serial->suspend_count)
1310 		return 0;
1311 
1312 	usb_serial_unpoison_port_urbs(serial);
1313 
1314 	if (serial->type->resume)
1315 		rv = serial->type->resume(serial);
1316 	else
1317 		rv = usb_serial_generic_resume(serial);
1318 
1319 	return rv;
1320 }
1321 EXPORT_SYMBOL(usb_serial_resume);
1322 
1323 static int usb_serial_reset_resume(struct usb_interface *intf)
1324 {
1325 	struct usb_serial *serial = usb_get_intfdata(intf);
1326 	int rv;
1327 
1328 	/* resume when called for last sibling interface */
1329 	if (--serial->suspend_count)
1330 		return 0;
1331 
1332 	usb_serial_unpoison_port_urbs(serial);
1333 
1334 	if (serial->type->reset_resume) {
1335 		rv = serial->type->reset_resume(serial);
1336 	} else {
1337 		rv = -EOPNOTSUPP;
1338 		intf->needs_binding = 1;
1339 	}
1340 
1341 	return rv;
1342 }
1343 
1344 static const struct tty_operations serial_ops = {
1345 	.open =			serial_open,
1346 	.close =		serial_close,
1347 	.write =		serial_write,
1348 	.hangup =		serial_hangup,
1349 	.write_room =		serial_write_room,
1350 	.ioctl =		serial_ioctl,
1351 	.set_termios =		serial_set_termios,
1352 	.throttle =		serial_throttle,
1353 	.unthrottle =		serial_unthrottle,
1354 	.break_ctl =		serial_break,
1355 	.chars_in_buffer =	serial_chars_in_buffer,
1356 	.wait_until_sent =	serial_wait_until_sent,
1357 	.tiocmget =		serial_tiocmget,
1358 	.tiocmset =		serial_tiocmset,
1359 	.get_icount =		serial_get_icount,
1360 	.set_serial =		serial_set_serial,
1361 	.get_serial =		serial_get_serial,
1362 	.cleanup =		serial_cleanup,
1363 	.install =		serial_install,
1364 	.proc_show =		serial_proc_show,
1365 };
1366 
1367 
1368 struct tty_driver *usb_serial_tty_driver;
1369 
1370 static int __init usb_serial_init(void)
1371 {
1372 	int result;
1373 
1374 	usb_serial_tty_driver = tty_alloc_driver(USB_SERIAL_TTY_MINORS,
1375 			TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV |
1376 			TTY_DRIVER_RESET_SAVED_TERMIOS);
1377 	if (IS_ERR(usb_serial_tty_driver))
1378 		return PTR_ERR(usb_serial_tty_driver);
1379 
1380 	/* Initialize our global data */
1381 	result = bus_register(&usb_serial_bus_type);
1382 	if (result) {
1383 		pr_err("%s - registering bus driver failed\n", __func__);
1384 		goto err_put_driver;
1385 	}
1386 
1387 	usb_serial_tty_driver->driver_name = "usbserial";
1388 	usb_serial_tty_driver->name = "ttyUSB";
1389 	usb_serial_tty_driver->major = USB_SERIAL_TTY_MAJOR;
1390 	usb_serial_tty_driver->minor_start = 0;
1391 	usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1392 	usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1393 	usb_serial_tty_driver->init_termios = tty_std_termios;
1394 	usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD
1395 							| HUPCL | CLOCAL;
1396 	usb_serial_tty_driver->init_termios.c_ispeed = 9600;
1397 	usb_serial_tty_driver->init_termios.c_ospeed = 9600;
1398 	tty_set_operations(usb_serial_tty_driver, &serial_ops);
1399 	result = tty_register_driver(usb_serial_tty_driver);
1400 	if (result) {
1401 		pr_err("%s - tty_register_driver failed\n", __func__);
1402 		goto err_unregister_bus;
1403 	}
1404 
1405 	/* register the generic driver, if we should */
1406 	result = usb_serial_generic_register();
1407 	if (result < 0) {
1408 		pr_err("%s - registering generic driver failed\n", __func__);
1409 		goto err_unregister_driver;
1410 	}
1411 
1412 	return result;
1413 
1414 err_unregister_driver:
1415 	tty_unregister_driver(usb_serial_tty_driver);
1416 err_unregister_bus:
1417 	bus_unregister(&usb_serial_bus_type);
1418 err_put_driver:
1419 	pr_err("%s - returning with error %d\n", __func__, result);
1420 	tty_driver_kref_put(usb_serial_tty_driver);
1421 	return result;
1422 }
1423 
1424 
1425 static void __exit usb_serial_exit(void)
1426 {
1427 	usb_serial_console_exit();
1428 
1429 	usb_serial_generic_deregister();
1430 
1431 	tty_unregister_driver(usb_serial_tty_driver);
1432 	tty_driver_kref_put(usb_serial_tty_driver);
1433 	bus_unregister(&usb_serial_bus_type);
1434 	idr_destroy(&serial_minors);
1435 }
1436 
1437 
1438 module_init(usb_serial_init);
1439 module_exit(usb_serial_exit);
1440 
1441 #define set_to_generic_if_null(type, function)				\
1442 	do {								\
1443 		if (!type->function) {					\
1444 			type->function = usb_serial_generic_##function;	\
1445 			pr_debug("%s: using generic " #function	"\n",	\
1446 						type->driver.name);	\
1447 		}							\
1448 	} while (0)
1449 
1450 static void usb_serial_operations_init(struct usb_serial_driver *device)
1451 {
1452 	set_to_generic_if_null(device, open);
1453 	set_to_generic_if_null(device, write);
1454 	set_to_generic_if_null(device, close);
1455 	set_to_generic_if_null(device, write_room);
1456 	set_to_generic_if_null(device, chars_in_buffer);
1457 	if (device->tx_empty)
1458 		set_to_generic_if_null(device, wait_until_sent);
1459 	set_to_generic_if_null(device, read_bulk_callback);
1460 	set_to_generic_if_null(device, write_bulk_callback);
1461 	set_to_generic_if_null(device, process_read_urb);
1462 	set_to_generic_if_null(device, prepare_write_buffer);
1463 }
1464 
1465 static int usb_serial_register(struct usb_serial_driver *driver)
1466 {
1467 	int retval;
1468 
1469 	if (usb_disabled())
1470 		return -ENODEV;
1471 
1472 	if (!driver->description)
1473 		driver->description = driver->driver.name;
1474 	if (!driver->usb_driver) {
1475 		WARN(1, "Serial driver %s has no usb_driver\n",
1476 				driver->description);
1477 		return -EINVAL;
1478 	}
1479 
1480 	/* Prevent individual ports from being unbound. */
1481 	driver->driver.suppress_bind_attrs = true;
1482 
1483 	usb_serial_operations_init(driver);
1484 
1485 	/* Add this device to our list of devices */
1486 	mutex_lock(&table_lock);
1487 	list_add(&driver->driver_list, &usb_serial_driver_list);
1488 
1489 	retval = usb_serial_bus_register(driver);
1490 	if (retval) {
1491 		pr_err("problem %d when registering driver %s\n", retval, driver->description);
1492 		list_del(&driver->driver_list);
1493 	} else {
1494 		pr_info("USB Serial support registered for %s\n", driver->description);
1495 	}
1496 	mutex_unlock(&table_lock);
1497 	return retval;
1498 }
1499 
1500 static void usb_serial_deregister(struct usb_serial_driver *device)
1501 {
1502 	pr_info("USB Serial deregistering driver %s\n", device->description);
1503 
1504 	mutex_lock(&table_lock);
1505 	list_del(&device->driver_list);
1506 	mutex_unlock(&table_lock);
1507 
1508 	usb_serial_bus_deregister(device);
1509 }
1510 
1511 /**
1512  * __usb_serial_register_drivers - register drivers for a usb-serial module
1513  * @serial_drivers: NULL-terminated array of pointers to drivers to be registered
1514  * @owner: owning module
1515  * @name: name of the usb_driver for this set of @serial_drivers
1516  * @id_table: list of all devices this @serial_drivers set binds to
1517  *
1518  * Registers all the drivers in the @serial_drivers array, and dynamically
1519  * creates a struct usb_driver with the name @name and id_table of @id_table.
1520  */
1521 int __usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers[],
1522 				  struct module *owner, const char *name,
1523 				  const struct usb_device_id *id_table)
1524 {
1525 	int rc;
1526 	struct usb_driver *udriver;
1527 	struct usb_serial_driver * const *sd, * const *s;
1528 
1529 	/*
1530 	 * udriver must be registered before any of the serial drivers,
1531 	 * because the store_new_id() routine for the serial drivers (in
1532 	 * bus.c) probes udriver.
1533 	 *
1534 	 * Performance hack: We don't want udriver to be probed until
1535 	 * the serial drivers are registered, because the probe would
1536 	 * simply fail for lack of a matching serial driver.
1537 	 * So we leave udriver's id_table set to NULL until we are all set.
1538 	 *
1539 	 * Suspend/resume support is implemented in the usb-serial core,
1540 	 * so fill in the PM-related fields in udriver.
1541 	 */
1542 	udriver = kzalloc_obj(*udriver);
1543 	if (!udriver)
1544 		return -ENOMEM;
1545 
1546 	udriver->name = name;
1547 	udriver->no_dynamic_id = 1;
1548 	udriver->supports_autosuspend = 1;
1549 	udriver->suspend = usb_serial_suspend;
1550 	udriver->resume = usb_serial_resume;
1551 	udriver->probe = usb_serial_probe;
1552 	udriver->disconnect = usb_serial_disconnect;
1553 
1554 	/* we only set the reset_resume field if the serial_driver has one */
1555 	for (sd = serial_drivers; *sd; ++sd) {
1556 		if ((*sd)->reset_resume) {
1557 			udriver->reset_resume = usb_serial_reset_resume;
1558 			break;
1559 		}
1560 	}
1561 
1562 	rc = usb_register(udriver);
1563 	if (rc)
1564 		goto err_free_driver;
1565 
1566 	for (sd = serial_drivers; *sd; ++sd) {
1567 		(*sd)->usb_driver = udriver;
1568 		(*sd)->driver.owner = owner;
1569 		rc = usb_serial_register(*sd);
1570 		if (rc)
1571 			goto err_deregister_drivers;
1572 	}
1573 
1574 	/* Now set udriver's id_table and look for matches */
1575 	udriver->id_table = id_table;
1576 	rc = driver_attach(&udriver->driver);
1577 	return 0;
1578 
1579 err_deregister_drivers:
1580 	for (s = serial_drivers; s < sd; ++s)
1581 		usb_serial_bus_remove_new_id(*s);
1582 	usb_deregister(udriver);
1583 	while (sd-- > serial_drivers)
1584 		usb_serial_deregister(*sd);
1585 err_free_driver:
1586 	kfree(udriver);
1587 	return rc;
1588 }
1589 EXPORT_SYMBOL_GPL(__usb_serial_register_drivers);
1590 
1591 /**
1592  * usb_serial_deregister_drivers - deregister drivers for a usb-serial module
1593  * @serial_drivers: NULL-terminated array of pointers to drivers to be deregistered
1594  *
1595  * Deregisters all the drivers in the @serial_drivers array and deregisters and
1596  * frees the struct usb_driver that was created by the call to
1597  * usb_serial_register_drivers().
1598  */
1599 void usb_serial_deregister_drivers(struct usb_serial_driver *const serial_drivers[])
1600 {
1601 	struct usb_driver *udriver = (*serial_drivers)->usb_driver;
1602 	struct usb_serial_driver * const *sd;
1603 
1604 	/*
1605 	 * udriver must be deregistered before the serial drivers so that
1606 	 * I/O is stopped before unbinding the ports.
1607 	 *
1608 	 * Remove the new_id attributes to prevent ids from being added and
1609 	 * triggering a probe of udriver after it has been deregistered.
1610 	 */
1611 	for (sd = serial_drivers; *sd; ++sd)
1612 		usb_serial_bus_remove_new_id(*sd);
1613 
1614 	usb_deregister(udriver);
1615 
1616 	for (sd = serial_drivers; *sd; ++sd)
1617 		usb_serial_deregister(*sd);
1618 
1619 	kfree(udriver);
1620 }
1621 EXPORT_SYMBOL_GPL(usb_serial_deregister_drivers);
1622 
1623 MODULE_AUTHOR(DRIVER_AUTHOR);
1624 MODULE_DESCRIPTION(DRIVER_DESC);
1625 MODULE_LICENSE("GPL v2");
1626