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