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