phonepad:add sew868
[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 #ifdef CONFIG_MW100
61 static int MW100_USB = 0;
62 #define MW100_USB_PORT     (SERIAL_TTY_MINORS - 10)
63 #endif
64 #ifdef CONFIG_MT6229
65 static int MT6229_USB = 0;
66 #define MT6229_USB_PORT     (SERIAL_TTY_MINORS - 10)
67 #endif
68 #ifdef CONFIG_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 #ifdef CONFIG_MU509
123         if (MU509_USB)
124                 a= MU509_USB_PORT;
125 #endif
126 #ifdef CONFIG_MW100     
127         if (MW100_USB)          
128                 a= MW100_USB_PORT;
129 #endif
130 #ifdef CONFIG_MT6229    
131         if (MT6229_USB)         
132                 a= MT6229_USB_PORT;
133 #endif
134 #ifdef CONFIG_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;
706         struct usb_serial_driver *drv;
707
708         /* Check if the usb id matches a known device */
709         list_for_each_entry(drv, &usb_serial_driver_list, driver_list) {
710                 id = get_iface_id(drv, iface);
711                 if (id)
712                         return drv;
713         }
714
715         return NULL;
716 }
717
718 static int serial_carrier_raised(struct tty_port *port)
719 {
720         struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
721         struct usb_serial_driver *drv = p->serial->type;
722         if (drv->carrier_raised)
723                 return drv->carrier_raised(p);
724         /* No carrier control - don't block */
725         return 1;       
726 }
727
728 static void serial_dtr_rts(struct tty_port *port, int on)
729 {
730         struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
731         struct usb_serial_driver *drv = p->serial->type;
732         if (drv->dtr_rts)
733                 drv->dtr_rts(p, on);
734 }
735
736 static const struct tty_port_operations serial_port_ops = {
737         .carrier_raised = serial_carrier_raised,
738         .dtr_rts = serial_dtr_rts,
739         .activate = serial_activate,
740         .shutdown = serial_down,
741 };
742
743 int usb_serial_probe(struct usb_interface *interface,
744                                const struct usb_device_id *id)
745 {
746         struct usb_device *dev = interface_to_usbdev(interface);
747         struct usb_serial *serial = NULL;
748         struct usb_serial_port *port;
749         struct usb_host_interface *iface_desc;
750         struct usb_endpoint_descriptor *endpoint;
751         struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
752         struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
753         struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
754         struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
755         struct usb_serial_driver *type = NULL;
756         int retval;
757         unsigned int minor;
758         int buffer_size;
759         int i;
760         int num_interrupt_in = 0;
761         int num_interrupt_out = 0;
762         int num_bulk_in = 0;
763         int num_bulk_out = 0;
764         int num_ports = 0;
765         int max_endpoints;
766
767         mutex_lock(&table_lock);
768         type = search_serial_device(interface);
769         if (!type) {
770                 mutex_unlock(&table_lock);
771                 dbg("none matched");
772                 return -ENODEV;
773         }
774
775         if (!try_module_get(type->driver.owner)) {
776                 mutex_unlock(&table_lock);
777                 dev_err(&interface->dev, "module get failed, exiting\n");
778                 return -EIO;
779         }
780         mutex_unlock(&table_lock);
781
782         serial = create_serial(dev, interface, type);
783         if (!serial) {
784                 module_put(type->driver.owner);
785                 dev_err(&interface->dev, "%s - out of memory\n", __func__);
786                 return -ENOMEM;
787         }
788
789         /* if this device type has a probe function, call it */
790         if (type->probe) {
791                 const struct usb_device_id *id;
792
793                 id = get_iface_id(type, interface);
794                 retval = type->probe(serial, id);
795
796                 if (retval) {
797                         dbg("sub driver rejected device");
798                         kfree(serial);
799                         module_put(type->driver.owner);
800                         return retval;
801                 }
802         }
803
804         /* descriptor matches, let's find the endpoints needed */
805         /* check out the endpoints */
806         iface_desc = interface->cur_altsetting;
807         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
808                 endpoint = &iface_desc->endpoint[i].desc;
809
810                 if (usb_endpoint_is_bulk_in(endpoint)) {
811                         /* we found a bulk in endpoint */
812                         dbg("found bulk in on endpoint %d", i);
813                         bulk_in_endpoint[num_bulk_in] = endpoint;
814                         ++num_bulk_in;
815                 }
816
817                 if (usb_endpoint_is_bulk_out(endpoint)) {
818                         /* we found a bulk out endpoint */
819                         dbg("found bulk out on endpoint %d", i);
820                         bulk_out_endpoint[num_bulk_out] = endpoint;
821                         ++num_bulk_out;
822                 }
823
824                 if (usb_endpoint_is_int_in(endpoint)) {
825                         /* we found a interrupt in endpoint */
826                         dbg("found interrupt in on endpoint %d", i);
827                         interrupt_in_endpoint[num_interrupt_in] = endpoint;
828                         ++num_interrupt_in;
829                 }
830
831                 if (usb_endpoint_is_int_out(endpoint)) {
832                         /* we found an interrupt out endpoint */
833                         dbg("found interrupt out on endpoint %d", i);
834                         interrupt_out_endpoint[num_interrupt_out] = endpoint;
835                         ++num_interrupt_out;
836                 }
837         }
838
839 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
840         /* BEGIN HORRIBLE HACK FOR PL2303 */
841         /* this is needed due to the looney way its endpoints are set up */
842         if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
843              (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
844             ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
845              (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID)) ||
846             ((le16_to_cpu(dev->descriptor.idVendor) == ALCOR_VENDOR_ID) &&
847              (le16_to_cpu(dev->descriptor.idProduct) == ALCOR_PRODUCT_ID)) ||
848             ((le16_to_cpu(dev->descriptor.idVendor) == SIEMENS_VENDOR_ID) &&
849              (le16_to_cpu(dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_EF81))) {
850                 if (interface != dev->actconfig->interface[0]) {
851                         /* check out the endpoints of the other interface*/
852                         iface_desc = dev->actconfig->interface[0]->cur_altsetting;
853                         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
854                                 endpoint = &iface_desc->endpoint[i].desc;
855                                 if (usb_endpoint_is_int_in(endpoint)) {
856                                         /* we found a interrupt in endpoint */
857                                         dbg("found interrupt in for Prolific device on separate interface");
858                                         interrupt_in_endpoint[num_interrupt_in] = endpoint;
859                                         ++num_interrupt_in;
860                                 }
861                         }
862                 }
863
864                 /* Now make sure the PL-2303 is configured correctly.
865                  * If not, give up now and hope this hack will work
866                  * properly during a later invocation of usb_serial_probe
867                  */
868                 if (num_bulk_in == 0 || num_bulk_out == 0) {
869                         dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
870                         kfree(serial);
871                         module_put(type->driver.owner);
872                         return -ENODEV;
873                 }
874         }
875         /* END HORRIBLE HACK FOR PL2303 */
876 #endif
877
878 #ifdef CONFIG_USB_SERIAL_GENERIC
879         if (type == &usb_serial_generic_device) {
880                 num_ports = num_bulk_out;
881                 if (num_ports == 0) {
882                         dev_err(&interface->dev,
883                             "Generic device with no bulk out, not allowed.\n");
884                         kfree(serial);
885                         module_put(type->driver.owner);
886                         return -EIO;
887                 }
888         }
889 #endif
890         if (!num_ports) {
891                 /* if this device type has a calc_num_ports function, call it */
892                 if (type->calc_num_ports)
893                         num_ports = type->calc_num_ports(serial);
894                 if (!num_ports)
895                         num_ports = type->num_ports;
896         }
897
898         serial->num_ports = num_ports;
899         serial->num_bulk_in = num_bulk_in;
900         serial->num_bulk_out = num_bulk_out;
901         serial->num_interrupt_in = num_interrupt_in;
902         serial->num_interrupt_out = num_interrupt_out;
903
904         /* found all that we need */
905         dev_info(&interface->dev, "%s converter detected\n",
906                         type->description);
907
908         /* create our ports, we need as many as the max endpoints */
909         /* we don't use num_ports here because some devices have more
910            endpoint pairs than ports */
911         max_endpoints = max(num_bulk_in, num_bulk_out);
912         max_endpoints = max(max_endpoints, num_interrupt_in);
913         max_endpoints = max(max_endpoints, num_interrupt_out);
914         max_endpoints = max(max_endpoints, (int)serial->num_ports);
915         serial->num_port_pointers = max_endpoints;
916
917         dbg("%s - setting up %d port structures for this device",
918                                                 __func__, max_endpoints);
919         for (i = 0; i < max_endpoints; ++i) {
920                 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
921                 if (!port)
922                         goto probe_error;
923                 tty_port_init(&port->port);
924                 port->port.ops = &serial_port_ops;
925                 port->serial = serial;
926                 spin_lock_init(&port->lock);
927                 /* Keep this for private driver use for the moment but
928                    should probably go away */
929                 INIT_WORK(&port->work, usb_serial_port_work);
930                 serial->port[i] = port;
931                 port->dev.parent = &interface->dev;
932                 port->dev.driver = NULL;
933                 port->dev.bus = &usb_serial_bus_type;
934                 port->dev.release = &port_release;
935                 device_initialize(&port->dev);
936         }
937
938         /* set up the endpoint information */
939         for (i = 0; i < num_bulk_in; ++i) {
940                 endpoint = bulk_in_endpoint[i];
941                 port = serial->port[i];
942                 port->read_urb = usb_alloc_urb(0, GFP_KERNEL);
943                 if (!port->read_urb) {
944                         dev_err(&interface->dev, "No free urbs available\n");
945                         goto probe_error;
946                 }
947                 buffer_size = max_t(int, serial->type->bulk_in_size,
948                                 le16_to_cpu(endpoint->wMaxPacketSize));
949                 port->bulk_in_size = buffer_size;
950                 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
951                 port->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
952                 if (!port->bulk_in_buffer) {
953                         dev_err(&interface->dev,
954                                         "Couldn't allocate bulk_in_buffer\n");
955                         goto probe_error;
956                 }
957                 usb_fill_bulk_urb(port->read_urb, dev,
958                                 usb_rcvbulkpipe(dev,
959                                                 endpoint->bEndpointAddress),
960                                 port->bulk_in_buffer, buffer_size,
961                                 serial->type->read_bulk_callback, port);
962         }
963
964         for (i = 0; i < num_bulk_out; ++i) {
965                 int j;
966
967                 endpoint = bulk_out_endpoint[i];
968                 port = serial->port[i];
969                 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
970                 if (!port->write_urb) {
971                         dev_err(&interface->dev, "No free urbs available\n");
972                         goto probe_error;
973                 }
974                 if (kfifo_alloc(&port->write_fifo, PAGE_SIZE, GFP_KERNEL))
975                         goto probe_error;
976                 buffer_size = serial->type->bulk_out_size;
977                 if (!buffer_size)
978                         buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
979                 port->bulk_out_size = buffer_size;
980                 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
981                 port->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
982                 if (!port->bulk_out_buffer) {
983                         dev_err(&interface->dev,
984                                         "Couldn't allocate bulk_out_buffer\n");
985                         goto probe_error;
986                 }
987                 usb_fill_bulk_urb(port->write_urb, dev,
988                                 usb_sndbulkpipe(dev,
989                                         endpoint->bEndpointAddress),
990                                 port->bulk_out_buffer, buffer_size,
991                                 serial->type->write_bulk_callback, port);
992                 for (j = 0; j < ARRAY_SIZE(port->write_urbs); ++j) {
993                         set_bit(j, &port->write_urbs_free);
994                         port->write_urbs[j] = usb_alloc_urb(0, GFP_KERNEL);
995                         if (!port->write_urbs[j]) {
996                                 dev_err(&interface->dev,
997                                                 "No free urbs available\n");
998                                 goto probe_error;
999                         }
1000                         port->bulk_out_buffers[j] = kmalloc(buffer_size,
1001                                                                 GFP_KERNEL);
1002                         if (!port->bulk_out_buffers[j]) {
1003                                 dev_err(&interface->dev,
1004                                         "Couldn't allocate bulk_out_buffer\n");
1005                                 goto probe_error;
1006                         }
1007                         usb_fill_bulk_urb(port->write_urbs[j], dev,
1008                                         usb_sndbulkpipe(dev,
1009                                                 endpoint->bEndpointAddress),
1010                                         port->bulk_out_buffers[j], buffer_size,
1011                                         serial->type->write_bulk_callback,
1012                                         port);
1013                 }
1014         }
1015
1016         if (serial->type->read_int_callback) {
1017                 for (i = 0; i < num_interrupt_in; ++i) {
1018                         endpoint = interrupt_in_endpoint[i];
1019                         port = serial->port[i];
1020                         port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
1021                         if (!port->interrupt_in_urb) {
1022                                 dev_err(&interface->dev,
1023                                                 "No free urbs available\n");
1024                                 goto probe_error;
1025                         }
1026                         buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1027                         port->interrupt_in_endpointAddress =
1028                                                 endpoint->bEndpointAddress;
1029                         port->interrupt_in_buffer = kmalloc(buffer_size,
1030                                                                 GFP_KERNEL);
1031                         if (!port->interrupt_in_buffer) {
1032                                 dev_err(&interface->dev,
1033                                     "Couldn't allocate interrupt_in_buffer\n");
1034                                 goto probe_error;
1035                         }
1036                         usb_fill_int_urb(port->interrupt_in_urb, dev,
1037                                 usb_rcvintpipe(dev,
1038                                                 endpoint->bEndpointAddress),
1039                                 port->interrupt_in_buffer, buffer_size,
1040                                 serial->type->read_int_callback, port,
1041                                 endpoint->bInterval);
1042                 }
1043         } else if (num_interrupt_in) {
1044                 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
1045         }
1046
1047         if (serial->type->write_int_callback) {
1048                 for (i = 0; i < num_interrupt_out; ++i) {
1049                         endpoint = interrupt_out_endpoint[i];
1050                         port = serial->port[i];
1051                         port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
1052                         if (!port->interrupt_out_urb) {
1053                                 dev_err(&interface->dev,
1054                                                 "No free urbs available\n");
1055                                 goto probe_error;
1056                         }
1057                         buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1058                         port->interrupt_out_size = buffer_size;
1059                         port->interrupt_out_endpointAddress =
1060                                                 endpoint->bEndpointAddress;
1061                         port->interrupt_out_buffer = kmalloc(buffer_size,
1062                                                                 GFP_KERNEL);
1063                         if (!port->interrupt_out_buffer) {
1064                                 dev_err(&interface->dev,
1065                                   "Couldn't allocate interrupt_out_buffer\n");
1066                                 goto probe_error;
1067                         }
1068                         usb_fill_int_urb(port->interrupt_out_urb, dev,
1069                                 usb_sndintpipe(dev,
1070                                                   endpoint->bEndpointAddress),
1071                                 port->interrupt_out_buffer, buffer_size,
1072                                 serial->type->write_int_callback, port,
1073                                 endpoint->bInterval);
1074                 }
1075         } else if (num_interrupt_out) {
1076                 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
1077         }
1078
1079         /* if this device type has an attach function, call it */
1080         if (type->attach) {
1081                 retval = type->attach(serial);
1082                 if (retval < 0)
1083                         goto probe_error;
1084                 serial->attached = 1;
1085                 if (retval > 0) {
1086                         /* quietly accept this device, but don't bind to a
1087                            serial port as it's about to disappear */
1088                         serial->num_ports = 0;
1089                         goto exit;
1090                 }
1091         } else {
1092                 serial->attached = 1;
1093         }
1094 #ifdef CONFIG_MU509
1095                 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x12D1 ) && (le16_to_cpu(dev->descriptor.idProduct) == 0x1001))
1096                         MU509_USB =1;
1097                 else
1098                         MU509_USB = 0;
1099 #endif
1100 #ifdef CONFIG_MW100             
1101         if ((le16_to_cpu(dev->descriptor.idVendor) == 0x19f5) && (le16_to_cpu(dev->descriptor.idProduct) == 0x9013))                    
1102                 MW100_USB =1;           
1103         else                    
1104                 MW100_USB = 0;
1105 #endif
1106 #ifdef CONFIG_MT6229            
1107         if ((le16_to_cpu(dev->descriptor.idVendor) == 0x0E8D) && (le16_to_cpu(dev->descriptor.idProduct) == 0x00A2))                    
1108                 MT6229_USB =1;          
1109         else                    
1110                 MT6229_USB = 0;
1111 #endif
1112 #ifdef CONFIG_SEW868            
1113         if ((le16_to_cpu(dev->descriptor.idVendor) == 0x19d2) && (le16_to_cpu(dev->descriptor.idProduct) == 0xffeb))                    
1114                 SEW868_USB =1;          
1115         else                    
1116                 SEW868_USB = 0;
1117 #endif
1118
1119         if (get_free_serial(serial, num_ports, &minor) == NULL) {
1120                 dev_err(&interface->dev, "No more free serial devices\n");
1121                 goto probe_error;
1122         }
1123         serial->minor = minor;
1124
1125         /* register all of the individual ports with the driver core */
1126         for (i = 0; i < num_ports; ++i) {
1127                 port = serial->port[i];
1128                 dev_set_name(&port->dev, "ttyUSB%d", port->number);
1129                 dbg ("%s - registering %s", __func__, dev_name(&port->dev));
1130                 port->dev_state = PORT_REGISTERING;
1131                 device_enable_async_suspend(&port->dev);
1132
1133                 retval = device_add(&port->dev);
1134                 if (retval) {
1135                         dev_err(&port->dev, "Error registering port device, "
1136                                 "continuing\n");
1137                         port->dev_state = PORT_UNREGISTERED;
1138                 } else {
1139                         port->dev_state = PORT_REGISTERED;
1140                 }
1141         }
1142
1143         usb_serial_console_init(debug, minor);
1144
1145 exit:
1146         /* success */
1147         usb_set_intfdata(interface, serial);
1148         module_put(type->driver.owner);
1149         return 0;
1150
1151 probe_error:
1152         usb_serial_put(serial);
1153         module_put(type->driver.owner);
1154         return -EIO;
1155 }
1156 EXPORT_SYMBOL_GPL(usb_serial_probe);
1157
1158 void usb_serial_disconnect(struct usb_interface *interface)
1159 {
1160         int i;
1161         struct usb_serial *serial = usb_get_intfdata(interface);
1162         struct device *dev = &interface->dev;
1163         struct usb_serial_port *port;
1164
1165         usb_serial_console_disconnect(serial);
1166         dbg("%s", __func__);
1167
1168         mutex_lock(&serial->disc_mutex);
1169         usb_set_intfdata(interface, NULL);
1170         /* must set a flag, to signal subdrivers */
1171         serial->disconnected = 1;
1172         mutex_unlock(&serial->disc_mutex);
1173
1174         for (i = 0; i < serial->num_ports; ++i) {
1175                 port = serial->port[i];
1176                 if (port) {
1177                         struct tty_struct *tty = tty_port_tty_get(&port->port);
1178                         if (tty) {
1179                                 tty_vhangup(tty);
1180                                 tty_kref_put(tty);
1181                         }
1182                         kill_traffic(port);
1183                         cancel_work_sync(&port->work);
1184                         if (port->dev_state == PORT_REGISTERED) {
1185
1186                                 /* Make sure the port is bound so that the
1187                                  * driver's port_remove method is called.
1188                                  */
1189                                 if (!port->dev.driver) {
1190                                         int rc;
1191
1192                                         port->dev.driver =
1193                                                         &serial->type->driver;
1194                                         rc = device_bind_driver(&port->dev);
1195                                 }
1196                                 port->dev_state = PORT_UNREGISTERING;
1197                                 device_del(&port->dev);
1198                                 port->dev_state = PORT_UNREGISTERED;
1199                         }
1200                 }
1201         }
1202         serial->type->disconnect(serial);
1203
1204         /* let the last holder of this object cause it to be cleaned up */
1205         usb_serial_put(serial);
1206         dev_info(dev, "device disconnected\n");
1207 }
1208 EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1209
1210 int usb_serial_suspend(struct usb_interface *intf, pm_message_t message)
1211 {
1212         struct usb_serial *serial = usb_get_intfdata(intf);
1213         struct usb_serial_port *port;
1214         int i, r = 0;
1215
1216         serial->suspending = 1;
1217
1218         if (serial->type->suspend) {
1219                 r = serial->type->suspend(serial, message);
1220                 if (r < 0) {
1221                         serial->suspending = 0;
1222                         goto err_out;
1223                 }
1224         }
1225
1226         for (i = 0; i < serial->num_ports; ++i) {
1227                 port = serial->port[i];
1228                 if (port)
1229                         kill_traffic(port);
1230         }
1231
1232 err_out:
1233         return r;
1234 }
1235 EXPORT_SYMBOL(usb_serial_suspend);
1236
1237 int usb_serial_resume(struct usb_interface *intf)
1238 {
1239         struct usb_serial *serial = usb_get_intfdata(intf);
1240         int rv;
1241
1242         serial->suspending = 0;
1243         if (serial->type->resume)
1244                 rv = serial->type->resume(serial);
1245         else
1246                 rv = usb_serial_generic_resume(serial);
1247
1248         return rv;
1249 }
1250 EXPORT_SYMBOL(usb_serial_resume);
1251
1252 static const struct tty_operations serial_ops = {
1253         .open =                 serial_open,
1254         .close =                serial_close,
1255         .write =                serial_write,
1256         .hangup =               serial_hangup,
1257         .write_room =           serial_write_room,
1258         .ioctl =                serial_ioctl,
1259         .set_termios =          serial_set_termios,
1260         .throttle =             serial_throttle,
1261         .unthrottle =           serial_unthrottle,
1262         .break_ctl =            serial_break,
1263         .chars_in_buffer =      serial_chars_in_buffer,
1264         .tiocmget =             serial_tiocmget,
1265         .tiocmset =             serial_tiocmset,
1266         .get_icount =           serial_get_icount,
1267         .cleanup =              serial_cleanup,
1268         .install =              serial_install,
1269         .proc_fops =            &serial_proc_fops,
1270 };
1271
1272
1273 struct tty_driver *usb_serial_tty_driver;
1274
1275 static int __init usb_serial_init(void)
1276 {
1277         int i;
1278         int result;
1279
1280         usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1281         if (!usb_serial_tty_driver)
1282                 return -ENOMEM;
1283
1284         /* Initialize our global data */
1285         for (i = 0; i < SERIAL_TTY_MINORS; ++i)
1286                 serial_table[i] = NULL;
1287
1288         result = bus_register(&usb_serial_bus_type);
1289         if (result) {
1290                 printk(KERN_ERR "usb-serial: %s - registering bus driver "
1291                        "failed\n", __func__);
1292                 goto exit_bus;
1293         }
1294
1295         usb_serial_tty_driver->owner = THIS_MODULE;
1296         usb_serial_tty_driver->driver_name = "usbserial";
1297         usb_serial_tty_driver->name =   "ttyUSB";
1298         usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1299         usb_serial_tty_driver->minor_start = 0;
1300         usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1301         usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1302         usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW |
1303                                                 TTY_DRIVER_DYNAMIC_DEV;
1304         usb_serial_tty_driver->init_termios = tty_std_termios;
1305         usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD
1306                                                         | HUPCL | CLOCAL;
1307         usb_serial_tty_driver->init_termios.c_ispeed = 9600;
1308         usb_serial_tty_driver->init_termios.c_ospeed = 9600;
1309         tty_set_operations(usb_serial_tty_driver, &serial_ops);
1310         result = tty_register_driver(usb_serial_tty_driver);
1311         if (result) {
1312                 printk(KERN_ERR "usb-serial: %s - tty_register_driver failed\n",
1313                        __func__);
1314                 goto exit_reg_driver;
1315         }
1316
1317         /* register the USB driver */
1318         result = usb_register(&usb_serial_driver);
1319         if (result < 0) {
1320                 printk(KERN_ERR "usb-serial: %s - usb_register failed\n",
1321                        __func__);
1322                 goto exit_tty;
1323         }
1324
1325         /* register the generic driver, if we should */
1326         result = usb_serial_generic_register(debug);
1327         if (result < 0) {
1328                 printk(KERN_ERR "usb-serial: %s - registering generic "
1329                        "driver failed\n", __func__);
1330                 goto exit_generic;
1331         }
1332
1333         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1334
1335         return result;
1336
1337 exit_generic:
1338         usb_deregister(&usb_serial_driver);
1339
1340 exit_tty:
1341         tty_unregister_driver(usb_serial_tty_driver);
1342
1343 exit_reg_driver:
1344         bus_unregister(&usb_serial_bus_type);
1345
1346 exit_bus:
1347         printk(KERN_ERR "usb-serial: %s - returning with error %d\n",
1348                __func__, result);
1349         put_tty_driver(usb_serial_tty_driver);
1350         return result;
1351 }
1352
1353
1354 static void __exit usb_serial_exit(void)
1355 {
1356         usb_serial_console_exit();
1357
1358         usb_serial_generic_deregister();
1359
1360         usb_deregister(&usb_serial_driver);
1361         tty_unregister_driver(usb_serial_tty_driver);
1362         put_tty_driver(usb_serial_tty_driver);
1363         bus_unregister(&usb_serial_bus_type);
1364 }
1365
1366
1367 module_init(usb_serial_init);
1368 module_exit(usb_serial_exit);
1369
1370 #define set_to_generic_if_null(type, function)                          \
1371         do {                                                            \
1372                 if (!type->function) {                                  \
1373                         type->function = usb_serial_generic_##function; \
1374                         dbg("Had to override the " #function            \
1375                                 " usb serial operation with the generic one.");\
1376                         }                                               \
1377         } while (0)
1378
1379 static void fixup_generic(struct usb_serial_driver *device)
1380 {
1381         set_to_generic_if_null(device, open);
1382         set_to_generic_if_null(device, write);
1383         set_to_generic_if_null(device, close);
1384         set_to_generic_if_null(device, write_room);
1385         set_to_generic_if_null(device, chars_in_buffer);
1386         set_to_generic_if_null(device, read_bulk_callback);
1387         set_to_generic_if_null(device, write_bulk_callback);
1388         set_to_generic_if_null(device, disconnect);
1389         set_to_generic_if_null(device, release);
1390         set_to_generic_if_null(device, process_read_urb);
1391         set_to_generic_if_null(device, prepare_write_buffer);
1392 }
1393
1394 int usb_serial_register(struct usb_serial_driver *driver)
1395 {
1396         /* must be called with BKL held */
1397         int retval;
1398
1399         if (usb_disabled())
1400                 return -ENODEV;
1401
1402         fixup_generic(driver);
1403
1404         if (!driver->description)
1405                 driver->description = driver->driver.name;
1406         if (!driver->usb_driver) {
1407                 WARN(1, "Serial driver %s has no usb_driver\n",
1408                                 driver->description);
1409                 return -EINVAL;
1410         }
1411         driver->usb_driver->supports_autosuspend = 1;
1412
1413         /* Add this device to our list of devices */
1414         mutex_lock(&table_lock);
1415         list_add(&driver->driver_list, &usb_serial_driver_list);
1416
1417         retval = usb_serial_bus_register(driver);
1418         if (retval) {
1419                 printk(KERN_ERR "usb-serial: problem %d when registering "
1420                        "driver %s\n", retval, driver->description);
1421                 list_del(&driver->driver_list);
1422         } else
1423                 printk(KERN_INFO "USB Serial support registered for %s\n",
1424                                                 driver->description);
1425
1426         mutex_unlock(&table_lock);
1427         return retval;
1428 }
1429 EXPORT_SYMBOL_GPL(usb_serial_register);
1430
1431
1432 void usb_serial_deregister(struct usb_serial_driver *device)
1433 {
1434         /* must be called with BKL held */
1435         printk(KERN_INFO "USB Serial deregistering driver %s\n",
1436                device->description);
1437         mutex_lock(&table_lock);
1438         list_del(&device->driver_list);
1439         usb_serial_bus_deregister(device);
1440         mutex_unlock(&table_lock);
1441 }
1442 EXPORT_SYMBOL_GPL(usb_serial_deregister);
1443
1444 /* Module information */
1445 MODULE_AUTHOR(DRIVER_AUTHOR);
1446 MODULE_DESCRIPTION(DRIVER_DESC);
1447 MODULE_LICENSE("GPL");
1448
1449 module_param(debug, bool, S_IRUGO | S_IWUSR);
1450 MODULE_PARM_DESC(debug, "Debug enabled or not");