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