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