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