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