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