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