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