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