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