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