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