xref: /linux/drivers/usb/serial/usb-serial.c (revision 63c3a1a9be06f8f8c75d54ef9331e35adb2e7919)
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 	struct usb_serial *serial = port->serial;
404 
405 	dev_dbg(&port->dev, "%s\n", __func__);
406 
407 	if (!port->serial->type->wait_until_sent)
408 		return;
409 
410 	mutex_lock(&serial->disc_mutex);
411 	if (!serial->disconnected)
412 		port->serial->type->wait_until_sent(tty, timeout);
413 	mutex_unlock(&serial->disc_mutex);
414 }
415 
416 static void serial_throttle(struct tty_struct *tty)
417 {
418 	struct usb_serial_port *port = tty->driver_data;
419 
420 	dev_dbg(&port->dev, "%s\n", __func__);
421 
422 	if (port->serial->type->throttle)
423 		port->serial->type->throttle(tty);
424 }
425 
426 static void serial_unthrottle(struct tty_struct *tty)
427 {
428 	struct usb_serial_port *port = tty->driver_data;
429 
430 	dev_dbg(&port->dev, "%s\n", __func__);
431 
432 	if (port->serial->type->unthrottle)
433 		port->serial->type->unthrottle(tty);
434 }
435 
436 static int serial_get_serial(struct tty_struct *tty, struct serial_struct *ss)
437 {
438 	struct usb_serial_port *port = tty->driver_data;
439 	struct tty_port *tport = &port->port;
440 	unsigned int close_delay, closing_wait;
441 
442 	mutex_lock(&tport->mutex);
443 
444 	close_delay = jiffies_to_msecs(tport->close_delay) / 10;
445 	closing_wait = tport->closing_wait;
446 	if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
447 		closing_wait = jiffies_to_msecs(closing_wait) / 10;
448 
449 	ss->line = port->minor;
450 	ss->close_delay = close_delay;
451 	ss->closing_wait = closing_wait;
452 
453 	if (port->serial->type->get_serial)
454 		port->serial->type->get_serial(tty, ss);
455 
456 	mutex_unlock(&tport->mutex);
457 
458 	return 0;
459 }
460 
461 static int serial_set_serial(struct tty_struct *tty, struct serial_struct *ss)
462 {
463 	struct usb_serial_port *port = tty->driver_data;
464 	struct tty_port *tport = &port->port;
465 	unsigned int close_delay, closing_wait;
466 	int ret = 0;
467 
468 	close_delay = msecs_to_jiffies(ss->close_delay * 10);
469 	closing_wait = ss->closing_wait;
470 	if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
471 		closing_wait = msecs_to_jiffies(closing_wait * 10);
472 
473 	mutex_lock(&tport->mutex);
474 
475 	if (!capable(CAP_SYS_ADMIN)) {
476 		if (close_delay != tport->close_delay ||
477 				closing_wait != tport->closing_wait) {
478 			ret = -EPERM;
479 			goto out_unlock;
480 		}
481 	}
482 
483 	if (port->serial->type->set_serial) {
484 		ret = port->serial->type->set_serial(tty, ss);
485 		if (ret)
486 			goto out_unlock;
487 	}
488 
489 	tport->close_delay = close_delay;
490 	tport->closing_wait = closing_wait;
491 out_unlock:
492 	mutex_unlock(&tport->mutex);
493 
494 	return ret;
495 }
496 
497 static int serial_ioctl(struct tty_struct *tty,
498 					unsigned int cmd, unsigned long arg)
499 {
500 	struct usb_serial_port *port = tty->driver_data;
501 	int retval = -ENOIOCTLCMD;
502 
503 	dev_dbg(&port->dev, "%s - cmd 0x%04x\n", __func__, cmd);
504 
505 	switch (cmd) {
506 	case TIOCMIWAIT:
507 		if (port->serial->type->tiocmiwait)
508 			retval = port->serial->type->tiocmiwait(tty, arg);
509 		break;
510 	default:
511 		if (port->serial->type->ioctl)
512 			retval = port->serial->type->ioctl(tty, cmd, arg);
513 	}
514 
515 	return retval;
516 }
517 
518 static void serial_set_termios(struct tty_struct *tty,
519 		               const struct ktermios *old)
520 {
521 	struct usb_serial_port *port = tty->driver_data;
522 
523 	dev_dbg(&port->dev, "%s\n", __func__);
524 
525 	if (port->serial->type->set_termios)
526 		port->serial->type->set_termios(tty, port, old);
527 	else
528 		tty_termios_copy_hw(&tty->termios, old);
529 }
530 
531 static int serial_break(struct tty_struct *tty, int break_state)
532 {
533 	struct usb_serial_port *port = tty->driver_data;
534 
535 	dev_dbg(&port->dev, "%s\n", __func__);
536 
537 	if (port->serial->type->break_ctl)
538 		return port->serial->type->break_ctl(tty, break_state);
539 
540 	return -ENOTTY;
541 }
542 
543 static int serial_proc_show(struct seq_file *m, void *v)
544 {
545 	struct usb_serial *serial;
546 	struct usb_serial_port *port;
547 	int i;
548 	char tmp[40];
549 
550 	seq_puts(m, "usbserinfo:1.0 driver:2.0\n");
551 	for (i = 0; i < USB_SERIAL_TTY_MINORS; ++i) {
552 		port = usb_serial_port_get_by_minor(i);
553 		if (port == NULL)
554 			continue;
555 		serial = port->serial;
556 
557 		seq_printf(m, "%d:", i);
558 		if (serial->type->driver.owner)
559 			seq_printf(m, " module:%s",
560 				module_name(serial->type->driver.owner));
561 		seq_printf(m, " name:\"%s\"",
562 				serial->type->description);
563 		seq_printf(m, " vendor:%04x product:%04x",
564 			le16_to_cpu(serial->dev->descriptor.idVendor),
565 			le16_to_cpu(serial->dev->descriptor.idProduct));
566 		seq_printf(m, " num_ports:%d", serial->num_ports);
567 		seq_printf(m, " port:%d", port->port_number);
568 		usb_make_path(serial->dev, tmp, sizeof(tmp));
569 		seq_printf(m, " path:%s", tmp);
570 
571 		seq_putc(m, '\n');
572 		usb_serial_put(serial);
573 		mutex_unlock(&serial->disc_mutex);
574 	}
575 	return 0;
576 }
577 
578 static int serial_tiocmget(struct tty_struct *tty)
579 {
580 	struct usb_serial_port *port = tty->driver_data;
581 
582 	dev_dbg(&port->dev, "%s\n", __func__);
583 
584 	if (port->serial->type->tiocmget)
585 		return port->serial->type->tiocmget(tty);
586 	return -ENOTTY;
587 }
588 
589 static int serial_tiocmset(struct tty_struct *tty,
590 			    unsigned int set, unsigned int clear)
591 {
592 	struct usb_serial_port *port = tty->driver_data;
593 
594 	dev_dbg(&port->dev, "%s\n", __func__);
595 
596 	if (port->serial->type->tiocmset)
597 		return port->serial->type->tiocmset(tty, set, clear);
598 	return -ENOTTY;
599 }
600 
601 static int serial_get_icount(struct tty_struct *tty,
602 				struct serial_icounter_struct *icount)
603 {
604 	struct usb_serial_port *port = tty->driver_data;
605 
606 	dev_dbg(&port->dev, "%s\n", __func__);
607 
608 	if (port->serial->type->get_icount)
609 		return port->serial->type->get_icount(tty, icount);
610 	return -ENOTTY;
611 }
612 
613 /*
614  * We would be calling tty_wakeup here, but unfortunately some line
615  * disciplines have an annoying habit of calling tty->write from
616  * the write wakeup callback (e.g. n_hdlc.c).
617  */
618 void usb_serial_port_softint(struct usb_serial_port *port)
619 {
620 	schedule_work(&port->work);
621 }
622 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
623 
624 static void usb_serial_port_work(struct work_struct *work)
625 {
626 	struct usb_serial_port *port =
627 		container_of(work, struct usb_serial_port, work);
628 
629 	tty_port_tty_wakeup(&port->port);
630 }
631 
632 static void usb_serial_port_poison_urbs(struct usb_serial_port *port)
633 {
634 	int i;
635 
636 	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
637 		usb_poison_urb(port->read_urbs[i]);
638 	for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
639 		usb_poison_urb(port->write_urbs[i]);
640 
641 	usb_poison_urb(port->interrupt_in_urb);
642 	usb_poison_urb(port->interrupt_out_urb);
643 }
644 
645 static void usb_serial_port_unpoison_urbs(struct usb_serial_port *port)
646 {
647 	int i;
648 
649 	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
650 		usb_unpoison_urb(port->read_urbs[i]);
651 	for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
652 		usb_unpoison_urb(port->write_urbs[i]);
653 
654 	usb_unpoison_urb(port->interrupt_in_urb);
655 	usb_unpoison_urb(port->interrupt_out_urb);
656 }
657 
658 static void usb_serial_port_release(struct device *dev)
659 {
660 	struct usb_serial_port *port = to_usb_serial_port(dev);
661 	int i;
662 
663 	dev_dbg(dev, "%s\n", __func__);
664 
665 	usb_free_urb(port->interrupt_in_urb);
666 	usb_free_urb(port->interrupt_out_urb);
667 	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
668 		usb_free_urb(port->read_urbs[i]);
669 		kfree(port->bulk_in_buffers[i]);
670 	}
671 	for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) {
672 		usb_free_urb(port->write_urbs[i]);
673 		kfree(port->bulk_out_buffers[i]);
674 	}
675 	kfifo_free(&port->write_fifo);
676 	kfree(port->interrupt_in_buffer);
677 	kfree(port->interrupt_out_buffer);
678 	tty_port_destroy(&port->port);
679 	kfree(port);
680 }
681 
682 static struct usb_serial *create_serial(struct usb_device *dev,
683 					struct usb_interface *interface,
684 					struct usb_serial_driver *driver)
685 {
686 	struct usb_serial *serial;
687 
688 	serial = kzalloc_obj(*serial);
689 	if (!serial)
690 		return NULL;
691 	serial->dev = usb_get_dev(dev);
692 	serial->type = driver;
693 	serial->interface = usb_get_intf(interface);
694 	kref_init(&serial->kref);
695 	mutex_init(&serial->disc_mutex);
696 	serial->minors_reserved = 0;
697 
698 	return serial;
699 }
700 
701 static const struct usb_device_id *match_dynamic_id(struct usb_interface *intf,
702 					    struct usb_serial_driver *drv)
703 {
704 	struct usb_dynid *dynid;
705 
706 	guard(mutex)(&usb_dynids_lock);
707 	list_for_each_entry(dynid, &drv->dynids.list, node) {
708 		if (usb_match_one_id(intf, &dynid->id)) {
709 			return &dynid->id;
710 		}
711 	}
712 	return NULL;
713 }
714 
715 static const struct usb_device_id *get_iface_id(struct usb_serial_driver *drv,
716 						struct usb_interface *intf)
717 {
718 	const struct usb_device_id *id;
719 
720 	id = usb_match_id(intf, drv->id_table);
721 	if (id) {
722 		dev_dbg(&intf->dev, "static descriptor matches\n");
723 		goto exit;
724 	}
725 	id = match_dynamic_id(intf, drv);
726 	if (id)
727 		dev_dbg(&intf->dev, "dynamic descriptor matches\n");
728 exit:
729 	return id;
730 }
731 
732 /* Caller must hold table_lock */
733 static struct usb_serial_driver *search_serial_device(
734 					struct usb_interface *iface)
735 {
736 	const struct usb_device_id *id = NULL;
737 	struct usb_serial_driver *drv;
738 	struct usb_driver *driver = to_usb_driver(iface->dev.driver);
739 
740 	/* Check if the usb id matches a known device */
741 	list_for_each_entry(drv, &usb_serial_driver_list, driver_list) {
742 		if (drv->usb_driver == driver)
743 			id = get_iface_id(drv, iface);
744 		if (id)
745 			return drv;
746 	}
747 
748 	return NULL;
749 }
750 
751 static bool serial_port_carrier_raised(struct tty_port *port)
752 {
753 	struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
754 	struct usb_serial_driver *drv = p->serial->type;
755 
756 	if (drv->carrier_raised)
757 		return drv->carrier_raised(p);
758 	/* No carrier control - don't block */
759 	return true;
760 }
761 
762 static void serial_port_dtr_rts(struct tty_port *port, bool on)
763 {
764 	struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
765 	struct usb_serial_driver *drv = p->serial->type;
766 
767 	if (drv->dtr_rts)
768 		drv->dtr_rts(p, on);
769 }
770 
771 static ssize_t port_number_show(struct device *dev,
772 				struct device_attribute *attr, char *buf)
773 {
774 	struct usb_serial_port *port = to_usb_serial_port(dev);
775 
776 	return sprintf(buf, "%u\n", port->port_number);
777 }
778 static DEVICE_ATTR_RO(port_number);
779 
780 static struct attribute *usb_serial_port_attrs[] = {
781 	&dev_attr_port_number.attr,
782 	NULL
783 };
784 ATTRIBUTE_GROUPS(usb_serial_port);
785 
786 static const struct tty_port_operations serial_port_ops = {
787 	.carrier_raised		= serial_port_carrier_raised,
788 	.dtr_rts		= serial_port_dtr_rts,
789 	.activate		= serial_port_activate,
790 	.shutdown		= serial_port_shutdown,
791 };
792 
793 static void store_endpoint(struct usb_serial *serial,
794 					struct usb_serial_endpoints *epds,
795 					struct usb_endpoint_descriptor *epd)
796 {
797 	struct device *dev = &serial->interface->dev;
798 	u8 addr = epd->bEndpointAddress;
799 
800 	if (usb_endpoint_is_bulk_in(epd)) {
801 		if (epds->num_bulk_in == ARRAY_SIZE(epds->bulk_in))
802 			return;
803 		dev_dbg(dev, "found bulk in endpoint %02x\n", addr);
804 		epds->bulk_in[epds->num_bulk_in++] = epd;
805 	} else if (usb_endpoint_is_bulk_out(epd)) {
806 		if (epds->num_bulk_out == ARRAY_SIZE(epds->bulk_out))
807 			return;
808 		dev_dbg(dev, "found bulk out endpoint %02x\n", addr);
809 		epds->bulk_out[epds->num_bulk_out++] = epd;
810 	} else if (usb_endpoint_is_int_in(epd)) {
811 		if (epds->num_interrupt_in == ARRAY_SIZE(epds->interrupt_in))
812 			return;
813 		dev_dbg(dev, "found interrupt in endpoint %02x\n", addr);
814 		epds->interrupt_in[epds->num_interrupt_in++] = epd;
815 	} else if (usb_endpoint_is_int_out(epd)) {
816 		if (epds->num_interrupt_out == ARRAY_SIZE(epds->interrupt_out))
817 			return;
818 		dev_dbg(dev, "found interrupt out endpoint %02x\n", addr);
819 		epds->interrupt_out[epds->num_interrupt_out++] = epd;
820 	}
821 }
822 
823 static void find_endpoints(struct usb_serial *serial,
824 					struct usb_serial_endpoints *epds,
825 					struct usb_interface *intf)
826 {
827 	struct usb_host_interface *iface_desc;
828 	struct usb_endpoint_descriptor *epd;
829 	unsigned int i;
830 
831 	iface_desc = intf->cur_altsetting;
832 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
833 		epd = &iface_desc->endpoint[i].desc;
834 		store_endpoint(serial, epds, epd);
835 	}
836 }
837 
838 static int setup_port_bulk_in(struct usb_serial_port *port,
839 					struct usb_endpoint_descriptor *epd)
840 {
841 	struct usb_serial_driver *type = port->serial->type;
842 	struct usb_device *udev = port->serial->dev;
843 	int buffer_size;
844 	int i;
845 
846 	buffer_size = max_t(int, type->bulk_in_size, usb_endpoint_maxp(epd));
847 	port->bulk_in_size = buffer_size;
848 	port->bulk_in_endpointAddress = epd->bEndpointAddress;
849 
850 	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
851 		set_bit(i, &port->read_urbs_free);
852 		port->read_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
853 		if (!port->read_urbs[i])
854 			return -ENOMEM;
855 		port->bulk_in_buffers[i] = kmalloc(buffer_size, GFP_KERNEL);
856 		if (!port->bulk_in_buffers[i])
857 			return -ENOMEM;
858 		usb_fill_bulk_urb(port->read_urbs[i], udev,
859 				usb_rcvbulkpipe(udev, epd->bEndpointAddress),
860 				port->bulk_in_buffers[i], buffer_size,
861 				type->read_bulk_callback, port);
862 	}
863 
864 	port->read_urb = port->read_urbs[0];
865 	port->bulk_in_buffer = port->bulk_in_buffers[0];
866 
867 	return 0;
868 }
869 
870 static int setup_port_bulk_out(struct usb_serial_port *port,
871 					struct usb_endpoint_descriptor *epd)
872 {
873 	struct usb_serial_driver *type = port->serial->type;
874 	struct usb_device *udev = port->serial->dev;
875 	int buffer_size;
876 	int i;
877 
878 	if (kfifo_alloc(&port->write_fifo, PAGE_SIZE, GFP_KERNEL))
879 		return -ENOMEM;
880 	if (type->bulk_out_size)
881 		buffer_size = type->bulk_out_size;
882 	else
883 		buffer_size = usb_endpoint_maxp(epd);
884 	port->bulk_out_size = buffer_size;
885 	port->bulk_out_endpointAddress = epd->bEndpointAddress;
886 
887 	for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) {
888 		set_bit(i, &port->write_urbs_free);
889 		port->write_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
890 		if (!port->write_urbs[i])
891 			return -ENOMEM;
892 		port->bulk_out_buffers[i] = kmalloc(buffer_size, GFP_KERNEL);
893 		if (!port->bulk_out_buffers[i])
894 			return -ENOMEM;
895 		usb_fill_bulk_urb(port->write_urbs[i], udev,
896 				usb_sndbulkpipe(udev, epd->bEndpointAddress),
897 				port->bulk_out_buffers[i], buffer_size,
898 				type->write_bulk_callback, port);
899 	}
900 
901 	port->write_urb = port->write_urbs[0];
902 	port->bulk_out_buffer = port->bulk_out_buffers[0];
903 
904 	return 0;
905 }
906 
907 static int setup_port_interrupt_in(struct usb_serial_port *port,
908 					struct usb_endpoint_descriptor *epd)
909 {
910 	struct usb_serial_driver *type = port->serial->type;
911 	struct usb_device *udev = port->serial->dev;
912 	int buffer_size;
913 
914 	port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
915 	if (!port->interrupt_in_urb)
916 		return -ENOMEM;
917 	buffer_size = usb_endpoint_maxp(epd);
918 	port->interrupt_in_endpointAddress = epd->bEndpointAddress;
919 	port->interrupt_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
920 	if (!port->interrupt_in_buffer)
921 		return -ENOMEM;
922 	usb_fill_int_urb(port->interrupt_in_urb, udev,
923 			usb_rcvintpipe(udev, epd->bEndpointAddress),
924 			port->interrupt_in_buffer, buffer_size,
925 			type->read_int_callback, port,
926 			epd->bInterval);
927 
928 	return 0;
929 }
930 
931 static int setup_port_interrupt_out(struct usb_serial_port *port,
932 					struct usb_endpoint_descriptor *epd)
933 {
934 	struct usb_serial_driver *type = port->serial->type;
935 	struct usb_device *udev = port->serial->dev;
936 	int buffer_size;
937 
938 	port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
939 	if (!port->interrupt_out_urb)
940 		return -ENOMEM;
941 	buffer_size = usb_endpoint_maxp(epd);
942 	port->interrupt_out_size = buffer_size;
943 	port->interrupt_out_endpointAddress = epd->bEndpointAddress;
944 	port->interrupt_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
945 	if (!port->interrupt_out_buffer)
946 		return -ENOMEM;
947 	usb_fill_int_urb(port->interrupt_out_urb, udev,
948 			usb_sndintpipe(udev, epd->bEndpointAddress),
949 			port->interrupt_out_buffer, buffer_size,
950 			type->write_int_callback, port,
951 			epd->bInterval);
952 
953 	return 0;
954 }
955 
956 static int usb_serial_probe(struct usb_interface *interface,
957 			       const struct usb_device_id *id)
958 {
959 	struct device *ddev = &interface->dev;
960 	struct usb_device *dev = interface_to_usbdev(interface);
961 	struct usb_serial *serial = NULL;
962 	struct usb_serial_port *port;
963 	struct usb_serial_endpoints *epds;
964 	struct usb_serial_driver *type = NULL;
965 	int retval;
966 	int i;
967 	int num_ports = 0;
968 	unsigned char max_endpoints;
969 
970 	mutex_lock(&table_lock);
971 	type = search_serial_device(interface);
972 	if (!type) {
973 		mutex_unlock(&table_lock);
974 		dev_dbg(ddev, "none matched\n");
975 		return -ENODEV;
976 	}
977 
978 	if (!try_module_get(type->driver.owner)) {
979 		mutex_unlock(&table_lock);
980 		dev_err(ddev, "module get failed, exiting\n");
981 		return -EIO;
982 	}
983 	mutex_unlock(&table_lock);
984 
985 	serial = create_serial(dev, interface, type);
986 	if (!serial) {
987 		retval = -ENOMEM;
988 		goto err_put_module;
989 	}
990 
991 	/* if this device type has a probe function, call it */
992 	if (type->probe) {
993 		const struct usb_device_id *id;
994 
995 		id = get_iface_id(type, interface);
996 		retval = type->probe(serial, id);
997 
998 		if (retval) {
999 			dev_dbg(ddev, "sub driver rejected device\n");
1000 			goto err_release_sibling;
1001 		}
1002 	}
1003 
1004 	/* descriptor matches, let's find the endpoints needed */
1005 	epds = kzalloc_obj(*epds);
1006 	if (!epds) {
1007 		retval = -ENOMEM;
1008 		goto err_release_sibling;
1009 	}
1010 
1011 	find_endpoints(serial, epds, interface);
1012 	if (serial->sibling)
1013 		find_endpoints(serial, epds, serial->sibling);
1014 
1015 	if (epds->num_bulk_in < type->num_bulk_in ||
1016 			epds->num_bulk_out < type->num_bulk_out ||
1017 			epds->num_interrupt_in < type->num_interrupt_in ||
1018 			epds->num_interrupt_out < type->num_interrupt_out) {
1019 		dev_err(ddev, "required endpoints missing\n");
1020 		retval = -ENODEV;
1021 		goto err_free_epds;
1022 	}
1023 
1024 	if (type->calc_num_ports) {
1025 		retval = type->calc_num_ports(serial, epds);
1026 		if (retval < 0)
1027 			goto err_free_epds;
1028 		num_ports = retval;
1029 	}
1030 
1031 	if (!num_ports)
1032 		num_ports = type->num_ports;
1033 
1034 	if (num_ports > MAX_NUM_PORTS) {
1035 		dev_warn(ddev, "too many ports requested: %d\n", num_ports);
1036 		num_ports = MAX_NUM_PORTS;
1037 	}
1038 
1039 	serial->num_ports = (unsigned char)num_ports;
1040 	serial->num_bulk_in = epds->num_bulk_in;
1041 	serial->num_bulk_out = epds->num_bulk_out;
1042 	serial->num_interrupt_in = epds->num_interrupt_in;
1043 	serial->num_interrupt_out = epds->num_interrupt_out;
1044 
1045 	/* found all that we need */
1046 	dev_info(ddev, "%s converter detected\n", type->description);
1047 
1048 	/* create our ports, we need as many as the max endpoints */
1049 	/* we don't use num_ports here because some devices have more
1050 	   endpoint pairs than ports */
1051 	max_endpoints = max(epds->num_bulk_in, epds->num_bulk_out);
1052 	max_endpoints = max(max_endpoints, epds->num_interrupt_in);
1053 	max_endpoints = max(max_endpoints, epds->num_interrupt_out);
1054 	max_endpoints = max(max_endpoints, serial->num_ports);
1055 	serial->num_port_pointers = max_endpoints;
1056 
1057 	dev_dbg(ddev, "setting up %d port structure(s)\n", max_endpoints);
1058 	for (i = 0; i < max_endpoints; ++i) {
1059 		port = kzalloc_obj(struct usb_serial_port);
1060 		if (!port) {
1061 			retval = -ENOMEM;
1062 			goto err_free_epds;
1063 		}
1064 		tty_port_init(&port->port);
1065 		port->port.ops = &serial_port_ops;
1066 		port->serial = serial;
1067 		spin_lock_init(&port->lock);
1068 		/* Keep this for private driver use for the moment but
1069 		   should probably go away */
1070 		INIT_WORK(&port->work, usb_serial_port_work);
1071 		serial->port[i] = port;
1072 		port->dev.parent = &interface->dev;
1073 		port->dev.driver = NULL;
1074 		port->dev.bus = &usb_serial_bus_type;
1075 		port->dev.release = &usb_serial_port_release;
1076 		port->dev.groups = usb_serial_port_groups;
1077 		device_initialize(&port->dev);
1078 	}
1079 
1080 	/* set up the endpoint information */
1081 	for (i = 0; i < epds->num_bulk_in; ++i) {
1082 		retval = setup_port_bulk_in(serial->port[i], epds->bulk_in[i]);
1083 		if (retval)
1084 			goto err_free_epds;
1085 	}
1086 
1087 	for (i = 0; i < epds->num_bulk_out; ++i) {
1088 		retval = setup_port_bulk_out(serial->port[i],
1089 				epds->bulk_out[i]);
1090 		if (retval)
1091 			goto err_free_epds;
1092 	}
1093 
1094 	if (serial->type->read_int_callback) {
1095 		for (i = 0; i < epds->num_interrupt_in; ++i) {
1096 			retval = setup_port_interrupt_in(serial->port[i],
1097 					epds->interrupt_in[i]);
1098 			if (retval)
1099 				goto err_free_epds;
1100 		}
1101 	} else if (epds->num_interrupt_in) {
1102 		dev_dbg(ddev, "The device claims to support interrupt in transfers, but read_int_callback is not defined\n");
1103 	}
1104 
1105 	if (serial->type->write_int_callback) {
1106 		for (i = 0; i < epds->num_interrupt_out; ++i) {
1107 			retval = setup_port_interrupt_out(serial->port[i],
1108 					epds->interrupt_out[i]);
1109 			if (retval)
1110 				goto err_free_epds;
1111 		}
1112 	} else if (epds->num_interrupt_out) {
1113 		dev_dbg(ddev, "The device claims to support interrupt out transfers, but write_int_callback is not defined\n");
1114 	}
1115 
1116 	usb_set_intfdata(interface, serial);
1117 
1118 	/* if this device type has an attach function, call it */
1119 	if (type->attach) {
1120 		retval = type->attach(serial);
1121 		if (retval < 0)
1122 			goto err_free_epds;
1123 		serial->attached = 1;
1124 		if (retval > 0) {
1125 			/* quietly accept this device, but don't bind to a
1126 			   serial port as it's about to disappear */
1127 			serial->num_ports = 0;
1128 			goto exit;
1129 		}
1130 	} else {
1131 		serial->attached = 1;
1132 	}
1133 
1134 	retval = allocate_minors(serial, num_ports);
1135 	if (retval) {
1136 		dev_err(ddev, "No more free serial minor numbers\n");
1137 		goto err_free_epds;
1138 	}
1139 
1140 	/* register all of the individual ports with the driver core */
1141 	for (i = 0; i < num_ports; ++i) {
1142 		port = serial->port[i];
1143 		dev_set_name(&port->dev, "ttyUSB%d", port->minor);
1144 		dev_dbg(ddev, "registering %s\n", dev_name(&port->dev));
1145 		device_enable_async_suspend(&port->dev);
1146 
1147 		retval = device_add(&port->dev);
1148 		if (retval)
1149 			dev_err(ddev, "Error registering port device, continuing\n");
1150 	}
1151 
1152 	if (num_ports > 0)
1153 		usb_serial_console_init(serial->port[0]->minor);
1154 exit:
1155 	kfree(epds);
1156 	module_put(type->driver.owner);
1157 	return 0;
1158 
1159 err_free_epds:
1160 	kfree(epds);
1161 err_release_sibling:
1162 	release_sibling(serial, interface);
1163 	usb_serial_put(serial);
1164 err_put_module:
1165 	module_put(type->driver.owner);
1166 
1167 	return retval;
1168 }
1169 
1170 static void usb_serial_disconnect(struct usb_interface *interface)
1171 {
1172 	int i;
1173 	struct usb_serial *serial = usb_get_intfdata(interface);
1174 	struct device *dev = &interface->dev;
1175 	struct usb_serial_port *port;
1176 
1177 	/* sibling interface is cleaning up */
1178 	if (!serial)
1179 		return;
1180 
1181 	usb_serial_console_disconnect(serial);
1182 
1183 	mutex_lock(&serial->disc_mutex);
1184 	/* must set a flag, to signal subdrivers */
1185 	serial->disconnected = 1;
1186 	mutex_unlock(&serial->disc_mutex);
1187 
1188 	for (i = 0; i < serial->num_ports; ++i) {
1189 		port = serial->port[i];
1190 		tty_port_tty_vhangup(&port->port);
1191 		usb_serial_port_poison_urbs(port);
1192 		wake_up_interruptible(&port->port.delta_msr_wait);
1193 		cancel_work_sync(&port->work);
1194 		if (device_is_registered(&port->dev))
1195 			device_del(&port->dev);
1196 	}
1197 	if (serial->type->disconnect)
1198 		serial->type->disconnect(serial);
1199 
1200 	release_sibling(serial, interface);
1201 
1202 	/* let the last holder of this object cause it to be cleaned up */
1203 	usb_serial_put(serial);
1204 	dev_info(dev, "device disconnected\n");
1205 }
1206 
1207 int usb_serial_suspend(struct usb_interface *intf, pm_message_t message)
1208 {
1209 	struct usb_serial *serial = usb_get_intfdata(intf);
1210 	int i, r;
1211 
1212 	/* suspend when called for first sibling interface */
1213 	if (serial->suspend_count++)
1214 		return 0;
1215 
1216 	/*
1217 	 * serial->type->suspend() MUST return 0 in system sleep context,
1218 	 * otherwise, the resume callback has to recover device from
1219 	 * previous suspend failure.
1220 	 */
1221 	if (serial->type->suspend) {
1222 		r = serial->type->suspend(serial, message);
1223 		if (r < 0) {
1224 			serial->suspend_count--;
1225 			return r;
1226 		}
1227 	}
1228 
1229 	for (i = 0; i < serial->num_ports; ++i)
1230 		usb_serial_port_poison_urbs(serial->port[i]);
1231 
1232 	return 0;
1233 }
1234 EXPORT_SYMBOL(usb_serial_suspend);
1235 
1236 static void usb_serial_unpoison_port_urbs(struct usb_serial *serial)
1237 {
1238 	int i;
1239 
1240 	for (i = 0; i < serial->num_ports; ++i)
1241 		usb_serial_port_unpoison_urbs(serial->port[i]);
1242 }
1243 
1244 int usb_serial_resume(struct usb_interface *intf)
1245 {
1246 	struct usb_serial *serial = usb_get_intfdata(intf);
1247 	int rv;
1248 
1249 	/* resume when called for last sibling interface */
1250 	if (--serial->suspend_count)
1251 		return 0;
1252 
1253 	usb_serial_unpoison_port_urbs(serial);
1254 
1255 	if (serial->type->resume)
1256 		rv = serial->type->resume(serial);
1257 	else
1258 		rv = usb_serial_generic_resume(serial);
1259 
1260 	return rv;
1261 }
1262 EXPORT_SYMBOL(usb_serial_resume);
1263 
1264 static int usb_serial_reset_resume(struct usb_interface *intf)
1265 {
1266 	struct usb_serial *serial = usb_get_intfdata(intf);
1267 	int rv;
1268 
1269 	/* resume when called for last sibling interface */
1270 	if (--serial->suspend_count)
1271 		return 0;
1272 
1273 	usb_serial_unpoison_port_urbs(serial);
1274 
1275 	if (serial->type->reset_resume) {
1276 		rv = serial->type->reset_resume(serial);
1277 	} else {
1278 		rv = -EOPNOTSUPP;
1279 		intf->needs_binding = 1;
1280 	}
1281 
1282 	return rv;
1283 }
1284 
1285 static const struct tty_operations serial_ops = {
1286 	.open =			serial_open,
1287 	.close =		serial_close,
1288 	.write =		serial_write,
1289 	.hangup =		serial_hangup,
1290 	.write_room =		serial_write_room,
1291 	.ioctl =		serial_ioctl,
1292 	.set_termios =		serial_set_termios,
1293 	.throttle =		serial_throttle,
1294 	.unthrottle =		serial_unthrottle,
1295 	.break_ctl =		serial_break,
1296 	.chars_in_buffer =	serial_chars_in_buffer,
1297 	.wait_until_sent =	serial_wait_until_sent,
1298 	.tiocmget =		serial_tiocmget,
1299 	.tiocmset =		serial_tiocmset,
1300 	.get_icount =		serial_get_icount,
1301 	.set_serial =		serial_set_serial,
1302 	.get_serial =		serial_get_serial,
1303 	.cleanup =		serial_cleanup,
1304 	.install =		serial_install,
1305 	.proc_show =		serial_proc_show,
1306 };
1307 
1308 
1309 struct tty_driver *usb_serial_tty_driver;
1310 
1311 static int __init usb_serial_init(void)
1312 {
1313 	int result;
1314 
1315 	usb_serial_tty_driver = tty_alloc_driver(USB_SERIAL_TTY_MINORS,
1316 			TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV);
1317 	if (IS_ERR(usb_serial_tty_driver))
1318 		return PTR_ERR(usb_serial_tty_driver);
1319 
1320 	/* Initialize our global data */
1321 	result = bus_register(&usb_serial_bus_type);
1322 	if (result) {
1323 		pr_err("%s - registering bus driver failed\n", __func__);
1324 		goto err_put_driver;
1325 	}
1326 
1327 	usb_serial_tty_driver->driver_name = "usbserial";
1328 	usb_serial_tty_driver->name = "ttyUSB";
1329 	usb_serial_tty_driver->major = USB_SERIAL_TTY_MAJOR;
1330 	usb_serial_tty_driver->minor_start = 0;
1331 	usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1332 	usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1333 	usb_serial_tty_driver->init_termios = tty_std_termios;
1334 	usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD
1335 							| HUPCL | CLOCAL;
1336 	usb_serial_tty_driver->init_termios.c_ispeed = 9600;
1337 	usb_serial_tty_driver->init_termios.c_ospeed = 9600;
1338 	tty_set_operations(usb_serial_tty_driver, &serial_ops);
1339 	result = tty_register_driver(usb_serial_tty_driver);
1340 	if (result) {
1341 		pr_err("%s - tty_register_driver failed\n", __func__);
1342 		goto err_unregister_bus;
1343 	}
1344 
1345 	/* register the generic driver, if we should */
1346 	result = usb_serial_generic_register();
1347 	if (result < 0) {
1348 		pr_err("%s - registering generic driver failed\n", __func__);
1349 		goto err_unregister_driver;
1350 	}
1351 
1352 	return result;
1353 
1354 err_unregister_driver:
1355 	tty_unregister_driver(usb_serial_tty_driver);
1356 err_unregister_bus:
1357 	bus_unregister(&usb_serial_bus_type);
1358 err_put_driver:
1359 	pr_err("%s - returning with error %d\n", __func__, result);
1360 	tty_driver_kref_put(usb_serial_tty_driver);
1361 	return result;
1362 }
1363 
1364 
1365 static void __exit usb_serial_exit(void)
1366 {
1367 	usb_serial_console_exit();
1368 
1369 	usb_serial_generic_deregister();
1370 
1371 	tty_unregister_driver(usb_serial_tty_driver);
1372 	tty_driver_kref_put(usb_serial_tty_driver);
1373 	bus_unregister(&usb_serial_bus_type);
1374 	idr_destroy(&serial_minors);
1375 }
1376 
1377 
1378 module_init(usb_serial_init);
1379 module_exit(usb_serial_exit);
1380 
1381 #define set_to_generic_if_null(type, function)				\
1382 	do {								\
1383 		if (!type->function) {					\
1384 			type->function = usb_serial_generic_##function;	\
1385 			pr_debug("%s: using generic " #function	"\n",	\
1386 						type->driver.name);	\
1387 		}							\
1388 	} while (0)
1389 
1390 static void usb_serial_operations_init(struct usb_serial_driver *device)
1391 {
1392 	set_to_generic_if_null(device, open);
1393 	set_to_generic_if_null(device, write);
1394 	set_to_generic_if_null(device, close);
1395 	set_to_generic_if_null(device, write_room);
1396 	set_to_generic_if_null(device, chars_in_buffer);
1397 	if (device->tx_empty)
1398 		set_to_generic_if_null(device, wait_until_sent);
1399 	set_to_generic_if_null(device, read_bulk_callback);
1400 	set_to_generic_if_null(device, write_bulk_callback);
1401 	set_to_generic_if_null(device, process_read_urb);
1402 	set_to_generic_if_null(device, prepare_write_buffer);
1403 }
1404 
1405 static int usb_serial_register(struct usb_serial_driver *driver)
1406 {
1407 	int retval;
1408 
1409 	if (usb_disabled())
1410 		return -ENODEV;
1411 
1412 	if (!driver->description)
1413 		driver->description = driver->driver.name;
1414 	if (!driver->usb_driver) {
1415 		WARN(1, "Serial driver %s has no usb_driver\n",
1416 				driver->description);
1417 		return -EINVAL;
1418 	}
1419 
1420 	/* Prevent individual ports from being unbound. */
1421 	driver->driver.suppress_bind_attrs = true;
1422 
1423 	usb_serial_operations_init(driver);
1424 
1425 	/* Add this device to our list of devices */
1426 	mutex_lock(&table_lock);
1427 	list_add(&driver->driver_list, &usb_serial_driver_list);
1428 
1429 	retval = usb_serial_bus_register(driver);
1430 	if (retval) {
1431 		pr_err("problem %d when registering driver %s\n", retval, driver->description);
1432 		list_del(&driver->driver_list);
1433 	} else {
1434 		pr_info("USB Serial support registered for %s\n", driver->description);
1435 	}
1436 	mutex_unlock(&table_lock);
1437 	return retval;
1438 }
1439 
1440 static void usb_serial_deregister(struct usb_serial_driver *device)
1441 {
1442 	pr_info("USB Serial deregistering driver %s\n", device->description);
1443 
1444 	mutex_lock(&table_lock);
1445 	list_del(&device->driver_list);
1446 	mutex_unlock(&table_lock);
1447 
1448 	usb_serial_bus_deregister(device);
1449 }
1450 
1451 /**
1452  * __usb_serial_register_drivers - register drivers for a usb-serial module
1453  * @serial_drivers: NULL-terminated array of pointers to drivers to be registered
1454  * @owner: owning module
1455  * @name: name of the usb_driver for this set of @serial_drivers
1456  * @id_table: list of all devices this @serial_drivers set binds to
1457  *
1458  * Registers all the drivers in the @serial_drivers array, and dynamically
1459  * creates a struct usb_driver with the name @name and id_table of @id_table.
1460  */
1461 int __usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers[],
1462 				  struct module *owner, const char *name,
1463 				  const struct usb_device_id *id_table)
1464 {
1465 	int rc;
1466 	struct usb_driver *udriver;
1467 	struct usb_serial_driver * const *sd;
1468 
1469 	/*
1470 	 * udriver must be registered before any of the serial drivers,
1471 	 * because the store_new_id() routine for the serial drivers (in
1472 	 * bus.c) probes udriver.
1473 	 *
1474 	 * Performance hack: We don't want udriver to be probed until
1475 	 * the serial drivers are registered, because the probe would
1476 	 * simply fail for lack of a matching serial driver.
1477 	 * So we leave udriver's id_table set to NULL until we are all set.
1478 	 *
1479 	 * Suspend/resume support is implemented in the usb-serial core,
1480 	 * so fill in the PM-related fields in udriver.
1481 	 */
1482 	udriver = kzalloc_obj(*udriver);
1483 	if (!udriver)
1484 		return -ENOMEM;
1485 
1486 	udriver->name = name;
1487 	udriver->no_dynamic_id = 1;
1488 	udriver->supports_autosuspend = 1;
1489 	udriver->suspend = usb_serial_suspend;
1490 	udriver->resume = usb_serial_resume;
1491 	udriver->probe = usb_serial_probe;
1492 	udriver->disconnect = usb_serial_disconnect;
1493 
1494 	/* we only set the reset_resume field if the serial_driver has one */
1495 	for (sd = serial_drivers; *sd; ++sd) {
1496 		if ((*sd)->reset_resume) {
1497 			udriver->reset_resume = usb_serial_reset_resume;
1498 			break;
1499 		}
1500 	}
1501 
1502 	rc = usb_register(udriver);
1503 	if (rc)
1504 		goto err_free_driver;
1505 
1506 	for (sd = serial_drivers; *sd; ++sd) {
1507 		(*sd)->usb_driver = udriver;
1508 		(*sd)->driver.owner = owner;
1509 		rc = usb_serial_register(*sd);
1510 		if (rc)
1511 			goto err_deregister_drivers;
1512 	}
1513 
1514 	/* Now set udriver's id_table and look for matches */
1515 	udriver->id_table = id_table;
1516 	rc = driver_attach(&udriver->driver);
1517 	return 0;
1518 
1519 err_deregister_drivers:
1520 	while (sd-- > serial_drivers)
1521 		usb_serial_deregister(*sd);
1522 	usb_deregister(udriver);
1523 err_free_driver:
1524 	kfree(udriver);
1525 	return rc;
1526 }
1527 EXPORT_SYMBOL_GPL(__usb_serial_register_drivers);
1528 
1529 /**
1530  * usb_serial_deregister_drivers - deregister drivers for a usb-serial module
1531  * @serial_drivers: NULL-terminated array of pointers to drivers to be deregistered
1532  *
1533  * Deregisters all the drivers in the @serial_drivers array and deregisters and
1534  * frees the struct usb_driver that was created by the call to
1535  * usb_serial_register_drivers().
1536  */
1537 void usb_serial_deregister_drivers(struct usb_serial_driver *const serial_drivers[])
1538 {
1539 	struct usb_driver *udriver = (*serial_drivers)->usb_driver;
1540 
1541 	for (; *serial_drivers; ++serial_drivers)
1542 		usb_serial_deregister(*serial_drivers);
1543 	usb_deregister(udriver);
1544 	kfree(udriver);
1545 }
1546 EXPORT_SYMBOL_GPL(usb_serial_deregister_drivers);
1547 
1548 MODULE_AUTHOR(DRIVER_AUTHOR);
1549 MODULE_DESCRIPTION(DRIVER_DESC);
1550 MODULE_LICENSE("GPL v2");
1551