VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / usb / serial / usb-serial.c
1 /*
2  * USB Serial Converter driver
3  *
4  * Copyright (C) 1999 - 2004 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  * (12/10/2002) gkh
18  *      Split the ports off into their own struct device, and added a
19  *      usb-serial bus driver.
20  *
21  * (11/19/2002) gkh
22  *      removed a few #ifdefs for the generic code and cleaned up the failure
23  *      logic in initialization.
24  *
25  * (10/02/2002) gkh
26  *      moved the console code to console.c and out of this file.
27  *
28  * (06/05/2002) gkh
29  *      moved location of startup() call in serial_probe() until after all
30  *      of the port information and endpoints are initialized.  This makes
31  *      things easier for some drivers.
32  *
33  * (04/10/2002) gkh
34  *      added serial_read_proc function which creates a
35  *      /proc/tty/driver/usb-serial file.
36  *
37  * (03/27/2002) gkh
38  *      Got USB serial console code working properly and merged into the main
39  *      version of the tree.  Thanks to Randy Dunlap for the initial version
40  *      of this code, and for pushing me to finish it up.
41  *      The USB serial console works with any usb serial driver device.
42  *
43  * (03/21/2002) gkh
44  *      Moved all manipulation of port->open_count into the core.  Now the
45  *      individual driver's open and close functions are called only when the
46  *      first open() and last close() is called.  Making the drivers a bit
47  *      smaller and simpler.
48  *      Fixed a bug if a driver didn't have the owner field set.
49  *
50  * (02/26/2002) gkh
51  *      Moved all locking into the main serial_* functions, instead of having 
52  *      the individual drivers have to grab the port semaphore.  This should
53  *      reduce races.
54  *      Reworked the MOD_INC logic a bit to always increment and decrement, even
55  *      if the generic driver is being used.
56  *
57  * (10/10/2001) gkh
58  *      usb_serial_disconnect() now sets the serial->dev pointer is to NULL to
59  *      help prevent child drivers from accessing the device since it is now
60  *      gone.
61  *
62  * (09/13/2001) gkh
63  *      Moved generic driver initialize after we have registered with the USB
64  *      core.  Thanks to Randy Dunlap for pointing this problem out.
65  *
66  * (07/03/2001) gkh
67  *      Fixed module paramater size.  Thanks to John Brockmeyer for the pointer.
68  *      Fixed vendor and product getting defined through the MODULE_PARM macro
69  *      if the Generic driver wasn't compiled in.
70  *      Fixed problem with generic_shutdown() not being called for drivers that
71  *      don't have a shutdown() function.
72  *
73  * (06/06/2001) gkh
74  *      added evil hack that is needed for the prolific pl2303 device due to the
75  *      crazy way its endpoints are set up.
76  *
77  * (05/30/2001) gkh
78  *      switched from using spinlock to a semaphore, which fixes lots of problems.
79  *
80  * (04/08/2001) gb
81  *      Identify version on module load.
82  *
83  * 2001_02_05 gkh
84  *      Fixed buffer overflows bug with the generic serial driver.  Thanks to
85  *      Todd Squires <squirest@ct0.com> for fixing this.
86  *
87  * (01/10/2001) gkh
88  *      Fixed bug where the generic serial adaptor grabbed _any_ device that was
89  *      offered to it.
90  *
91  * (12/12/2000) gkh
92  *      Removed MOD_INC and MOD_DEC from poll and disconnect functions, and
93  *      moved them to the serial_open and serial_close functions.
94  *      Also fixed bug with there not being a MOD_DEC for the generic driver
95  *      (thanks to Gary Brubaker for finding this.)
96  *
97  * (11/29/2000) gkh
98  *      Small NULL pointer initialization cleanup which saves a bit of disk image
99  *
100  * (11/01/2000) Adam J. Richter
101  *      instead of using idVendor/idProduct pairs, usb serial drivers
102  *      now identify their hardware interest with usb_device_id tables,
103  *      which they usually have anyhow for use with MODULE_DEVICE_TABLE.
104  *
105  * (10/05/2000) gkh
106  *      Fixed bug with urb->dev not being set properly, now that the usb
107  *      core needs it.
108  * 
109  * (09/11/2000) gkh
110  *      Removed DEBUG #ifdefs with call to usb_serial_debug_data
111  *
112  * (08/28/2000) gkh
113  *      Added port_lock to port structure.
114  *      Added locks for SMP safeness to generic driver
115  *      Fixed the ability to open a generic device's port more than once.
116  *
117  * (07/23/2000) gkh
118  *      Added bulk_out_endpointAddress to port structure.
119  *
120  * (07/19/2000) gkh, pberger, and borchers
121  *      Modifications to allow usb-serial drivers to be modules.
122  *
123  * (07/03/2000) gkh
124  *      Added more debugging to serial_ioctl call
125  * 
126  * (06/25/2000) gkh
127  *      Changed generic_write_bulk_callback to not call wake_up_interruptible
128  *      directly, but to have port_softint do it at a safer time.
129  *
130  * (06/23/2000) gkh
131  *      Cleaned up debugging statements in a quest to find UHCI timeout bug.
132  *
133  * (05/22/2000) gkh
134  *      Changed the makefile, enabling the big CONFIG_USB_SERIAL_SOMTHING to be 
135  *      removed from the individual device source files.
136  *
137  * (05/03/2000) gkh
138  *      Added the Digi Acceleport driver from Al Borchers and Peter Berger.
139  * 
140  * (05/02/2000) gkh
141  *      Changed devfs and tty register code to work properly now. This was based on
142  *      the ACM driver changes by Vojtech Pavlik.
143  *
144  * (04/27/2000) Ryan VanderBijl
145  *      Put calls to *_paranoia_checks into one function.
146  * 
147  * (04/23/2000) gkh
148  *      Fixed bug that Randy Dunlap found for Generic devices with no bulk out ports.
149  *      Moved when the startup code printed out the devices that are supported.
150  *
151  * (04/19/2000) gkh
152  *      Added driver for ZyXEL omni.net lcd plus ISDN TA
153  *      Made startup info message specify which drivers were compiled in.
154  *
155  * (04/03/2000) gkh
156  *      Changed the probe process to remove the module unload races.
157  *      Changed where the tty layer gets initialized to have devfs work nicer.
158  *      Added initial devfs support.
159  *
160  * (03/26/2000) gkh
161  *      Split driver up into device specific pieces.
162  * 
163  * (03/19/2000) gkh
164  *      Fixed oops that could happen when device was removed while a program
165  *      was talking to the device.
166  *      Removed the static urbs and now all urbs are created and destroyed
167  *      dynamically.
168  *      Reworked the internal interface. Now everything is based on the 
169  *      usb_serial_port structure instead of the larger usb_serial structure.
170  *      This fixes the bug that a multiport device could not have more than
171  *      one port open at one time.
172  *
173  * (03/17/2000) gkh
174  *      Added config option for debugging messages.
175  *      Added patch for keyspan pda from Brian Warner.
176  *
177  * (03/06/2000) gkh
178  *      Added the keyspan pda code from Brian Warner <warner@lothar.com>
179  *      Moved a bunch of the port specific stuff into its own structure. This
180  *      is in anticipation of the true multiport devices (there's a bug if you
181  *      try to access more than one port of any multiport device right now)
182  *
183  * (02/21/2000) gkh
184  *      Made it so that any serial devices only have to specify which functions
185  *      they want to overload from the generic function calls (great, 
186  *      inheritance in C, in a driver, just what I wanted...)
187  *      Added support for set_termios and ioctl function calls. No drivers take
188  *      advantage of this yet.
189  *      Removed the #ifdef MODULE, now there is no module specific code.
190  *      Cleaned up a few comments in usb-serial.h that were wrong (thanks again
191  *      to Miles Lott).
192  *      Small fix to get_free_serial.
193  *
194  * (02/14/2000) gkh
195  *      Removed the Belkin and Peracom functionality from the driver due to
196  *      the lack of support from the vendor, and me not wanting people to 
197  *      accidenatly buy the device, expecting it to work with Linux.
198  *      Added read_bulk_callback and write_bulk_callback to the type structure
199  *      for the needs of the FTDI and WhiteHEAT driver.
200  *      Changed all reverences to FTDI to FTDI_SIO at the request of Bill
201  *      Ryder.
202  *      Changed the output urb size back to the max endpoint size to make
203  *      the ftdi_sio driver have it easier, and due to the fact that it didn't
204  *      really increase the speed any.
205  *
206  * (02/11/2000) gkh
207  *      Added VISOR_FUNCTION_CONSOLE to the visor startup function. This was a
208  *      patch from Miles Lott (milos@insync.net).
209  *      Fixed bug with not restoring the minor range that a device grabs, if
210  *      the startup function fails (thanks Miles for finding this).
211  *
212  * (02/05/2000) gkh
213  *      Added initial framework for the Keyspan PDA serial converter so that
214  *      Brian Warner has a place to put his code.
215  *      Made the ezusb specific functions generic enough that different
216  *      devices can use them (whiteheat and keyspan_pda both need them).
217  *      Split out a whole bunch of structure and other stuff to a separate
218  *      usb-serial.h file.
219  *      Made the Visor connection messages a little more understandable, now
220  *      that Miles Lott (milos@insync.net) has gotten the Generic channel to
221  *      work. Also made them always show up in the log file.
222  * 
223  * (01/25/2000) gkh
224  *      Added initial framework for FTDI serial converter so that Bill Ryder
225  *      has a place to put his code.
226  *      Added the vendor specific info from Handspring. Now we can print out
227  *      informational debug messages as well as understand what is happening.
228  *
229  * (01/23/2000) gkh
230  *      Fixed problem of crash when trying to open a port that didn't have a
231  *      device assigned to it. Made the minor node finding a little smarter,
232  *      now it looks to find a continuous space for the new device.
233  *
234  * (01/21/2000) gkh
235  *      Fixed bug in visor_startup with patch from Miles Lott (milos@insync.net)
236  *      Fixed get_serial_by_minor which was all messed up for multi port 
237  *      devices. Fixed multi port problem for generic devices. Now the number
238  *      of ports is determined by the number of bulk out endpoints for the
239  *      generic device.
240  *
241  * (01/19/2000) gkh
242  *      Removed lots of cruft that was around from the old (pre urb) driver 
243  *      interface.
244  *      Made the serial_table dynamic. This should save lots of memory when
245  *      the number of minor nodes goes up to 256.
246  *      Added initial support for devices that have more than one port. 
247  *      Added more debugging comments for the Visor, and added a needed 
248  *      set_configuration call.
249  *
250  * (01/17/2000) gkh
251  *      Fixed the WhiteHEAT firmware (my processing tool had a bug)
252  *      and added new debug loader firmware for it.
253  *      Removed the put_char function as it isn't really needed.
254  *      Added visor startup commands as found by the Win98 dump.
255  * 
256  * (01/13/2000) gkh
257  *      Fixed the vendor id for the generic driver to the one I meant it to be.
258  *
259  * (01/12/2000) gkh
260  *      Forget the version numbering...that's pretty useless...
261  *      Made the driver able to be compiled so that the user can select which
262  *      converter they want to use. This allows people who only want the Visor
263  *      support to not pay the memory size price of the WhiteHEAT.
264  *      Fixed bug where the generic driver (idVendor=0000 and idProduct=0000)
265  *      grabbed the root hub. Not good.
266  * 
267  * version 0.4.0 (01/10/2000) gkh
268  *      Added whiteheat.h containing the firmware for the ConnectTech WhiteHEAT
269  *      device. Added startup function to allow firmware to be downloaded to
270  *      a device if it needs to be.
271  *      Added firmware download logic to the WhiteHEAT device.
272  *      Started to add #defines to split up the different drivers for potential
273  *      configuration option.
274  *      
275  * version 0.3.1 (12/30/99) gkh
276  *      Fixed problems with urb for bulk out.
277  *      Added initial support for multiple sets of endpoints. This enables
278  *      the Handspring Visor to be attached successfully. Only the first
279  *      bulk in / bulk out endpoint pair is being used right now.
280  *
281  * version 0.3.0 (12/27/99) gkh
282  *      Added initial support for the Handspring Visor based on a patch from
283  *      Miles Lott (milos@sneety.insync.net)
284  *      Cleaned up the code a bunch and converted over to using urbs only.
285  *
286  * version 0.2.3 (12/21/99) gkh
287  *      Added initial support for the Connect Tech WhiteHEAT converter.
288  *      Incremented the number of ports in expectation of getting the
289  *      WhiteHEAT to work properly (4 ports per connection).
290  *      Added notification on insertion and removal of what port the
291  *      device is/was connected to (and what kind of device it was).
292  *
293  * version 0.2.2 (12/16/99) gkh
294  *      Changed major number to the new allocated number. We're legal now!
295  *
296  * version 0.2.1 (12/14/99) gkh
297  *      Fixed bug that happens when device node is opened when there isn't a
298  *      device attached to it. Thanks to marek@webdesign.no for noticing this.
299  *
300  * version 0.2.0 (11/10/99) gkh
301  *      Split up internals to make it easier to add different types of serial 
302  *      converters to the code.
303  *      Added a "generic" driver that gets it's vendor and product id
304  *      from when the module is loaded. Thanks to David E. Nelson (dnelson@jump.net)
305  *      for the idea and sample code (from the usb scanner driver.)
306  *      Cleared up any licensing questions by releasing it under the GNU GPL.
307  *
308  * version 0.1.2 (10/25/99) gkh
309  *      Fixed bug in detecting device.
310  *
311  * version 0.1.1 (10/05/99) gkh
312  *      Changed the major number to not conflict with anything else.
313  *
314  * version 0.1 (09/28/99) gkh
315  *      Can recognize the two different devices and start up a read from
316  *      device when asked to. Writes also work. No control signals yet, this
317  *      all is vendor specific data (i.e. no spec), also no control for
318  *      different baud rates or other bit settings.
319  *      Currently we are using the same devid as the acm driver. This needs
320  *      to change.
321  * 
322  */
323
324 #include <linux/config.h>
325 #include <linux/kernel.h>
326 #include <linux/errno.h>
327 #include <linux/init.h>
328 #include <linux/slab.h>
329 #include <linux/tty.h>
330 #include <linux/tty_driver.h>
331 #include <linux/tty_flip.h>
332 #include <linux/module.h>
333 #include <linux/moduleparam.h>
334 #include <linux/spinlock.h>
335 #include <linux/list.h>
336 #include <linux/smp_lock.h>
337 #include <asm/uaccess.h>
338 #include <linux/usb.h>
339 #include "usb-serial.h"
340 #include "pl2303.h"
341
342 /*
343  * Version Information
344  */
345 #define DRIVER_VERSION "v2.0"
346 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
347 #define DRIVER_DESC "USB Serial Driver core"
348
349 /* Driver structure we register with the USB core */
350 static struct usb_driver usb_serial_driver = {
351         .owner =        THIS_MODULE,
352         .name =         "usbserial",
353         .probe =        usb_serial_probe,
354         .disconnect =   usb_serial_disconnect,
355 };
356
357 /* There is no MODULE_DEVICE_TABLE for usbserial.c.  Instead
358    the MODULE_DEVICE_TABLE declarations in each serial driver
359    cause the "hotplug" program to pull in whatever module is necessary
360    via modprobe, and modprobe will load usbserial because the serial
361    drivers depend on it.
362 */
363
364 static int debug;
365 static struct usb_serial *serial_table[SERIAL_TTY_MINORS];      /* initially all NULL */
366 static LIST_HEAD(usb_serial_driver_list);
367
368 struct usb_serial *usb_serial_get_by_index(unsigned index)
369 {
370         struct usb_serial *serial = serial_table[index];
371
372         if (serial)
373                 kref_get(&serial->kref);
374         return serial;
375 }
376
377 static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_ports, unsigned int *minor)
378 {
379         unsigned int i, j;
380         int good_spot;
381
382         dbg("%s %d", __FUNCTION__, num_ports);
383
384         *minor = 0;
385         for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
386                 if (serial_table[i])
387                         continue;
388
389                 good_spot = 1;
390                 for (j = 1; j <= num_ports-1; ++j)
391                         if ((serial_table[i+j]) || (i+j >= SERIAL_TTY_MINORS)) {
392                                 good_spot = 0;
393                                 i += j;
394                                 break;
395                         }
396                 if (good_spot == 0)
397                         continue;
398
399                 *minor = i;
400                 dbg("%s - minor base = %d", __FUNCTION__, *minor);
401                 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
402                         serial_table[i] = serial;
403                 return serial;
404         }
405         return NULL;
406 }
407
408 static void return_serial (struct usb_serial *serial)
409 {
410         int i;
411
412         dbg("%s", __FUNCTION__);
413
414         if (serial == NULL)
415                 return;
416
417         for (i = 0; i < serial->num_ports; ++i) {
418                 serial_table[serial->minor + i] = NULL;
419         }
420
421         return;
422 }
423
424 /*****************************************************************************
425  * Driver tty interface functions
426  *****************************************************************************/
427 static int serial_open (struct tty_struct *tty, struct file * filp)
428 {
429         struct usb_serial *serial;
430         struct usb_serial_port *port;
431         unsigned int portNumber;
432         int retval = 0;
433         
434         dbg("%s", __FUNCTION__);
435
436         /* initialize the pointer incase something fails */
437         tty->driver_data = NULL;
438
439         /* get the serial object associated with this tty pointer */
440         serial = usb_serial_get_by_index(tty->index);
441         if (!serial) {
442                 retval = -ENODEV;
443                 goto bailout;
444         }
445
446         /* set up our port structure making the tty driver remember our port object, and us it */
447         portNumber = tty->index - serial->minor;
448         port = serial->port[portNumber];
449         tty->driver_data = port;
450
451         port->tty = tty;
452          
453         /* lock this module before we call it,
454            this may, which means we must bail out, safe because we are called with BKL held */
455         if (!try_module_get(serial->type->owner)) {
456                 retval = -ENODEV;
457                 goto bailout;
458         }
459
460         ++port->open_count;
461         if (port->open_count == 1) {
462                 /* only call the device specific open if this 
463                  * is the first time the port is opened */
464                 retval = serial->type->open(port, filp);
465                 if (retval) {
466                         port->open_count = 0;
467                         module_put(serial->type->owner);
468                         kref_put(&serial->kref);
469                 }
470         }
471 bailout:
472         return retval;
473 }
474
475 static void serial_close(struct tty_struct *tty, struct file * filp)
476 {
477         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
478
479         if (!port)
480                 return;
481
482         dbg("%s - port %d", __FUNCTION__, port->number);
483
484         --port->open_count;
485         if (port->open_count <= 0) {
486                 /* only call the device specific close if this 
487                  * port is being closed by the last owner */
488                 port->serial->type->close(port, filp);
489                 port->open_count = 0;
490
491                 if (port->tty) {
492                         if (port->tty->driver_data)
493                                 port->tty->driver_data = NULL;
494                         port->tty = NULL;
495                 }
496         }
497
498         module_put(port->serial->type->owner);
499         kref_put(&port->serial->kref);
500 }
501
502 static int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
503 {
504         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
505         int retval = -EINVAL;
506
507         dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
508
509         if (!port->open_count) {
510                 dbg("%s - port not opened", __FUNCTION__);
511                 goto exit;
512         }
513
514         /* pass on to the driver specific version of this function */
515         retval = port->serial->type->write(port, from_user, buf, count);
516
517 exit:
518         return retval;
519 }
520
521 static int serial_write_room (struct tty_struct *tty) 
522 {
523         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
524         int retval = -EINVAL;
525
526         dbg("%s - port %d", __FUNCTION__, port->number);
527
528         if (!port->open_count) {
529                 dbg("%s - port not open", __FUNCTION__);
530                 goto exit;
531         }
532
533         /* pass on to the driver specific version of this function */
534         retval = port->serial->type->write_room(port);
535
536 exit:
537         return retval;
538 }
539
540 static int serial_chars_in_buffer (struct tty_struct *tty) 
541 {
542         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
543         int retval = -EINVAL;
544
545         dbg("%s = port %d", __FUNCTION__, port->number);
546
547         if (!port->open_count) {
548                 dbg("%s - port not open", __FUNCTION__);
549                 goto exit;
550         }
551
552         /* pass on to the driver specific version of this function */
553         retval = port->serial->type->chars_in_buffer(port);
554
555 exit:
556         return retval;
557 }
558
559 static void serial_throttle (struct tty_struct * tty)
560 {
561         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
562
563         dbg("%s - port %d", __FUNCTION__, port->number);
564
565         if (!port->open_count) {
566                 dbg ("%s - port not open", __FUNCTION__);
567                 goto exit;
568         }
569
570         /* pass on to the driver specific version of this function */
571         if (port->serial->type->throttle)
572                 port->serial->type->throttle(port);
573
574 exit:
575         ;
576 }
577
578 static void serial_unthrottle (struct tty_struct * tty)
579 {
580         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
581
582         dbg("%s - port %d", __FUNCTION__, port->number);
583
584         if (!port->open_count) {
585                 dbg("%s - port not open", __FUNCTION__);
586                 goto exit;
587         }
588
589         /* pass on to the driver specific version of this function */
590         if (port->serial->type->unthrottle)
591                 port->serial->type->unthrottle(port);
592
593 exit:
594         ;
595 }
596
597 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
598 {
599         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
600         int retval = -ENODEV;
601
602         dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
603
604         if (!port->open_count) {
605                 dbg ("%s - port not open", __FUNCTION__);
606                 goto exit;
607         }
608
609         /* pass on to the driver specific version of this function if it is available */
610         if (port->serial->type->ioctl)
611                 retval = port->serial->type->ioctl(port, file, cmd, arg);
612         else
613                 retval = -ENOIOCTLCMD;
614
615 exit:
616         return retval;
617 }
618
619 static void serial_set_termios (struct tty_struct *tty, struct termios * old)
620 {
621         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
622
623         dbg("%s - port %d", __FUNCTION__, port->number);
624
625         if (!port->open_count) {
626                 dbg("%s - port not open", __FUNCTION__);
627                 goto exit;
628         }
629
630         /* pass on to the driver specific version of this function if it is available */
631         if (port->serial->type->set_termios)
632                 port->serial->type->set_termios(port, old);
633
634 exit:
635         ;
636 }
637
638 static void serial_break (struct tty_struct *tty, int break_state)
639 {
640         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
641
642         dbg("%s - port %d", __FUNCTION__, port->number);
643
644         if (!port->open_count) {
645                 dbg("%s - port not open", __FUNCTION__);
646                 goto exit;
647         }
648
649         /* pass on to the driver specific version of this function if it is available */
650         if (port->serial->type->break_ctl)
651                 port->serial->type->break_ctl(port, break_state);
652
653 exit:
654         ;
655 }
656
657 static void serial_shutdown (struct usb_serial *serial)
658 {
659         dbg ("%s", __FUNCTION__);
660
661         serial->type->shutdown(serial);
662 }
663
664 static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
665 {
666         struct usb_serial *serial;
667         int length = 0;
668         int i;
669         off_t begin = 0;
670         char tmp[40];
671
672         dbg("%s", __FUNCTION__);
673         length += sprintf (page, "usbserinfo:1.0 driver:%s\n", DRIVER_VERSION);
674         for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
675                 serial = usb_serial_get_by_index(i);
676                 if (serial == NULL)
677                         continue;
678
679                 length += sprintf (page+length, "%d:", i);
680                 if (serial->type->owner)
681                         length += sprintf (page+length, " module:%s", module_name(serial->type->owner));
682                 length += sprintf (page+length, " name:\"%s\"", serial->type->name);
683                 length += sprintf (page+length, " vendor:%04x product:%04x", serial->vendor, serial->product);
684                 length += sprintf (page+length, " num_ports:%d", serial->num_ports);
685                 length += sprintf (page+length, " port:%d", i - serial->minor + 1);
686
687                 usb_make_path(serial->dev, tmp, sizeof(tmp));
688                 length += sprintf (page+length, " path:%s", tmp);
689                         
690                 length += sprintf (page+length, "\n");
691                 if ((length + begin) > (off + count))
692                         goto done;
693                 if ((length + begin) < off) {
694                         begin += length;
695                         length = 0;
696                 }
697                 kref_put(&serial->kref);
698         }
699         *eof = 1;
700 done:
701         if (off >= (length + begin))
702                 return 0;
703         *start = page + (off-begin);
704         return ((count < begin+length-off) ? count : begin+length-off);
705 }
706
707 static int serial_tiocmget (struct tty_struct *tty, struct file *file)
708 {
709         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
710
711         dbg("%s - port %d", __FUNCTION__, port->number);
712
713         if (!port->open_count) {
714                 dbg("%s - port not open", __FUNCTION__);
715                 goto exit;
716         }
717
718         if (port->serial->type->tiocmget)
719                 return port->serial->type->tiocmget(port, file);
720
721 exit:
722         return -EINVAL;
723 }
724
725 static int serial_tiocmset (struct tty_struct *tty, struct file *file,
726                             unsigned int set, unsigned int clear)
727 {
728         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
729
730         dbg("%s - port %d", __FUNCTION__, port->number);
731
732         if (!port->open_count) {
733                 dbg("%s - port not open", __FUNCTION__);
734                 goto exit;
735         }
736
737         if (port->serial->type->tiocmset)
738                 return port->serial->type->tiocmset(port, file, set, clear);
739
740 exit:
741         return -EINVAL;
742 }
743
744 void usb_serial_port_softint(void *private)
745 {
746         struct usb_serial_port *port = (struct usb_serial_port *)private;
747         struct tty_struct *tty;
748
749         dbg("%s - port %d", __FUNCTION__, port->number);
750         
751         if (!port)
752                 return;
753
754         tty = port->tty;
755         if (!tty)
756                 return;
757
758         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) {
759                 dbg("%s - write wakeup call.", __FUNCTION__);
760                 (tty->ldisc.write_wakeup)(tty);
761         }
762
763         wake_up_interruptible(&tty->write_wait);
764 }
765
766 static void destroy_serial(struct kref *kref)
767 {
768         struct usb_serial *serial;
769         struct usb_serial_port *port;
770         int i;
771
772         serial = to_usb_serial(kref);
773
774         dbg ("%s - %s", __FUNCTION__, serial->type->name);
775         serial_shutdown (serial);
776
777         /* return the minor range that this device had */
778         return_serial(serial);
779
780         for (i = 0; i < serial->num_ports; ++i)
781                 serial->port[i]->open_count = 0;
782
783         /* the ports are cleaned up and released in port_release() */
784         for (i = 0; i < serial->num_ports; ++i)
785                 if (serial->port[i]->dev.parent != NULL) {
786                         device_unregister(&serial->port[i]->dev);
787                         serial->port[i] = NULL;
788                 }
789
790         /* If this is a "fake" port, we have to clean it up here, as it will
791          * not get cleaned up in port_release() as it was never registered with
792          * the driver core */
793         if (serial->num_ports < serial->num_port_pointers) {
794                 for (i = serial->num_ports; i < serial->num_port_pointers; ++i) {
795                         port = serial->port[i];
796                         if (!port)
797                                 continue;
798                         if (port->read_urb) {
799                                 usb_unlink_urb(port->read_urb);
800                                 usb_free_urb(port->read_urb);
801                         }
802                         if (port->write_urb) {
803                                 usb_unlink_urb(port->write_urb);
804                                 usb_free_urb(port->write_urb);
805                         }
806                         if (port->interrupt_in_urb) {
807                                 usb_unlink_urb(port->interrupt_in_urb);
808                                 usb_free_urb(port->interrupt_in_urb);
809                         }
810                         kfree(port->bulk_in_buffer);
811                         kfree(port->bulk_out_buffer);
812                         kfree(port->interrupt_in_buffer);
813                 }
814         }
815
816         usb_put_dev(serial->dev);
817
818         /* free up any memory that we allocated */
819         kfree (serial);
820 }
821
822 static void port_release(struct device *dev)
823 {
824         struct usb_serial_port *port = to_usb_serial_port(dev);
825
826         dbg ("%s - %s", __FUNCTION__, dev->bus_id);
827         if (port->read_urb) {
828                 usb_unlink_urb(port->read_urb);
829                 usb_free_urb(port->read_urb);
830         }
831         if (port->write_urb) {
832                 usb_unlink_urb(port->write_urb);
833                 usb_free_urb(port->write_urb);
834         }
835         if (port->interrupt_in_urb) {
836                 usb_unlink_urb(port->interrupt_in_urb);
837                 usb_free_urb(port->interrupt_in_urb);
838         }
839         kfree(port->bulk_in_buffer);
840         kfree(port->bulk_out_buffer);
841         kfree(port->interrupt_in_buffer);
842         kfree(port);
843 }
844
845 static struct usb_serial * create_serial (struct usb_device *dev, 
846                                           struct usb_interface *interface,
847                                           struct usb_serial_device_type *type)
848 {
849         struct usb_serial *serial;
850
851         serial = kmalloc (sizeof (*serial), GFP_KERNEL);
852         if (!serial) {
853                 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
854                 return NULL;
855         }
856         memset (serial, 0, sizeof(*serial));
857         serial->dev = usb_get_dev(dev);
858         serial->type = type;
859         serial->interface = interface;
860         serial->vendor = dev->descriptor.idVendor;
861         serial->product = dev->descriptor.idProduct;
862         kref_init(&serial->kref, destroy_serial);
863
864         return serial;
865 }
866
867 int usb_serial_probe(struct usb_interface *interface,
868                                const struct usb_device_id *id)
869 {
870         struct usb_device *dev = interface_to_usbdev (interface);
871         struct usb_serial *serial = NULL;
872         struct usb_serial_port *port;
873         struct usb_host_interface *iface_desc;
874         struct usb_endpoint_descriptor *endpoint;
875         struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
876         struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
877         struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
878         struct usb_serial_device_type *type = NULL;
879         struct list_head *tmp;
880         int retval;
881         int found;
882         int minor;
883         int buffer_size;
884         int i;
885         int num_interrupt_in = 0;
886         int num_bulk_in = 0;
887         int num_bulk_out = 0;
888         int num_ports = 0;
889         int max_endpoints;
890         const struct usb_device_id *id_pattern = NULL;
891
892         /* loop through our list of known serial converters, and see if this
893            device matches. */
894         found = 0;
895         list_for_each (tmp, &usb_serial_driver_list) {
896                 type = list_entry(tmp, struct usb_serial_device_type, driver_list);
897                 id_pattern = usb_match_id(interface, type->id_table);
898                 if (id_pattern != NULL) {
899                         dbg("descriptor matches");
900                         found = 1;
901                         break;
902                 }
903         }
904         if (!found) {
905                 /* no match */
906                 dbg("none matched");
907                 return -ENODEV;
908         }
909
910         serial = create_serial (dev, interface, type);
911         if (!serial) {
912                 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
913                 return -ENODEV;
914         }
915
916         /* if this device type has a probe function, call it */
917         if (type->probe) {
918                 if (!try_module_get(type->owner)) {
919                         dev_err(&interface->dev, "module get failed, exiting\n");
920                         kfree (serial);
921                         return -EIO;
922                 }
923                 retval = type->probe (serial, id_pattern);
924                 module_put(type->owner);
925
926                 if (retval) {
927                         dbg ("sub driver rejected device");
928                         kfree (serial);
929                         return retval;
930                 }
931         }
932
933         /* descriptor matches, let's find the endpoints needed */
934         /* check out the endpoints */
935         iface_desc = interface->cur_altsetting;
936         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
937                 endpoint = &iface_desc->endpoint[i].desc;
938                 
939                 if ((endpoint->bEndpointAddress & 0x80) &&
940                     ((endpoint->bmAttributes & 3) == 0x02)) {
941                         /* we found a bulk in endpoint */
942                         dbg("found bulk in");
943                         bulk_in_endpoint[num_bulk_in] = endpoint;
944                         ++num_bulk_in;
945                 }
946
947                 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
948                     ((endpoint->bmAttributes & 3) == 0x02)) {
949                         /* we found a bulk out endpoint */
950                         dbg("found bulk out");
951                         bulk_out_endpoint[num_bulk_out] = endpoint;
952                         ++num_bulk_out;
953                 }
954                 
955                 if ((endpoint->bEndpointAddress & 0x80) &&
956                     ((endpoint->bmAttributes & 3) == 0x03)) {
957                         /* we found a interrupt in endpoint */
958                         dbg("found interrupt in");
959                         interrupt_in_endpoint[num_interrupt_in] = endpoint;
960                         ++num_interrupt_in;
961                 }
962         }
963
964 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
965         /* BEGIN HORRIBLE HACK FOR PL2303 */ 
966         /* this is needed due to the looney way its endpoints are set up */
967         if (((dev->descriptor.idVendor == PL2303_VENDOR_ID) &&
968              (dev->descriptor.idProduct == PL2303_PRODUCT_ID)) ||
969             ((dev->descriptor.idVendor == ATEN_VENDOR_ID) &&
970              (dev->descriptor.idProduct == ATEN_PRODUCT_ID))) {
971                 if (interface != dev->actconfig->interface[0]) {
972                         /* check out the endpoints of the other interface*/
973                         iface_desc = dev->actconfig->interface[0]->cur_altsetting;
974                         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
975                                 endpoint = &iface_desc->endpoint[i].desc;
976                                 if ((endpoint->bEndpointAddress & 0x80) &&
977                                     ((endpoint->bmAttributes & 3) == 0x03)) {
978                                         /* we found a interrupt in endpoint */
979                                         dbg("found interrupt in for Prolific device on separate interface");
980                                         interrupt_in_endpoint[num_interrupt_in] = endpoint;
981                                         ++num_interrupt_in;
982                                 }
983                         }
984                 }
985
986                 /* Now make sure the PL-2303 is configured correctly.
987                  * If not, give up now and hope this hack will work
988                  * properly during a later invocation of usb_serial_probe
989                  */
990                 if (num_bulk_in == 0 || num_bulk_out == 0) {
991                         dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
992                         kfree (serial);
993                         return -ENODEV;
994                 }
995         }
996         /* END HORRIBLE HACK FOR PL2303 */
997 #endif
998
999         /* found all that we need */
1000         dev_info(&interface->dev, "%s converter detected\n", type->name);
1001
1002 #ifdef CONFIG_USB_SERIAL_GENERIC
1003         if (type == &usb_serial_generic_device) {
1004                 num_ports = num_bulk_out;
1005                 if (num_ports == 0) {
1006                         dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
1007                         kfree (serial);
1008                         return -EIO;
1009                 }
1010         }
1011 #endif
1012         if (!num_ports) {
1013                 /* if this device type has a calc_num_ports function, call it */
1014                 if (type->calc_num_ports) {
1015                         if (!try_module_get(type->owner)) {
1016                                 dev_err(&interface->dev, "module get failed, exiting\n");
1017                                 kfree (serial);
1018                                 return -EIO;
1019                         }
1020                         num_ports = type->calc_num_ports (serial);
1021                         module_put(type->owner);
1022                 }
1023                 if (!num_ports)
1024                         num_ports = type->num_ports;
1025         }
1026
1027         if (get_free_serial (serial, num_ports, &minor) == NULL) {
1028                 dev_err(&interface->dev, "No more free serial devices\n");
1029                 kfree (serial);
1030                 return -ENOMEM;
1031         }
1032
1033         serial->minor = minor;
1034         serial->num_ports = num_ports;
1035         serial->num_bulk_in = num_bulk_in;
1036         serial->num_bulk_out = num_bulk_out;
1037         serial->num_interrupt_in = num_interrupt_in;
1038
1039         /* create our ports, we need as many as the max endpoints */
1040         /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
1041         max_endpoints = max(num_bulk_in, num_bulk_out);
1042         max_endpoints = max(max_endpoints, num_interrupt_in);
1043         max_endpoints = max(max_endpoints, (int)serial->num_ports);
1044         serial->num_port_pointers = max_endpoints;
1045         dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
1046         for (i = 0; i < max_endpoints; ++i) {
1047                 port = kmalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
1048                 if (!port)
1049                         goto probe_error;
1050                 memset(port, 0x00, sizeof(struct usb_serial_port));
1051                 port->number = i + serial->minor;
1052                 port->serial = serial;
1053                 INIT_WORK(&port->work, usb_serial_port_softint, port);
1054                 serial->port[i] = port;
1055         }
1056
1057         /* set up the endpoint information */
1058         for (i = 0; i < num_bulk_in; ++i) {
1059                 endpoint = bulk_in_endpoint[i];
1060                 port = serial->port[i];
1061                 port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
1062                 if (!port->read_urb) {
1063                         dev_err(&interface->dev, "No free urbs available\n");
1064                         goto probe_error;
1065                 }
1066                 buffer_size = endpoint->wMaxPacketSize;
1067                 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
1068                 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1069                 if (!port->bulk_in_buffer) {
1070                         dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
1071                         goto probe_error;
1072                 }
1073                 usb_fill_bulk_urb (port->read_urb, dev,
1074                                    usb_rcvbulkpipe (dev,
1075                                                     endpoint->bEndpointAddress),
1076                                    port->bulk_in_buffer, buffer_size,
1077                                    serial->type->read_bulk_callback,
1078                                    port);
1079         }
1080
1081         for (i = 0; i < num_bulk_out; ++i) {
1082                 endpoint = bulk_out_endpoint[i];
1083                 port = serial->port[i];
1084                 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
1085                 if (!port->write_urb) {
1086                         dev_err(&interface->dev, "No free urbs available\n");
1087                         goto probe_error;
1088                 }
1089                 buffer_size = endpoint->wMaxPacketSize;
1090                 port->bulk_out_size = buffer_size;
1091                 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
1092                 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
1093                 if (!port->bulk_out_buffer) {
1094                         dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
1095                         goto probe_error;
1096                 }
1097                 usb_fill_bulk_urb (port->write_urb, dev,
1098                                    usb_sndbulkpipe (dev,
1099                                                     endpoint->bEndpointAddress),
1100                                    port->bulk_out_buffer, buffer_size, 
1101                                    serial->type->write_bulk_callback,
1102                                    port);
1103         }
1104
1105         for (i = 0; i < num_interrupt_in; ++i) {
1106                 endpoint = interrupt_in_endpoint[i];
1107                 port = serial->port[i];
1108                 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
1109                 if (!port->interrupt_in_urb) {
1110                         dev_err(&interface->dev, "No free urbs available\n");
1111                         goto probe_error;
1112                 }
1113                 buffer_size = endpoint->wMaxPacketSize;
1114                 port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
1115                 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1116                 if (!port->interrupt_in_buffer) {
1117                         dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
1118                         goto probe_error;
1119                 }
1120                 usb_fill_int_urb (port->interrupt_in_urb, dev, 
1121                                   usb_rcvintpipe (dev,
1122                                                   endpoint->bEndpointAddress),
1123                                   port->interrupt_in_buffer, buffer_size, 
1124                                   serial->type->read_int_callback, port, 
1125                                   endpoint->bInterval);
1126         }
1127
1128         /* if this device type has an attach function, call it */
1129         if (type->attach) {
1130                 if (!try_module_get(type->owner)) {
1131                         dev_err(&interface->dev, "module get failed, exiting\n");
1132                         goto probe_error;
1133                 }
1134                 retval = type->attach (serial);
1135                 module_put(type->owner);
1136                 if (retval < 0)
1137                         goto probe_error;
1138                 if (retval > 0) {
1139                         /* quietly accept this device, but don't bind to a serial port
1140                          * as it's about to disappear */
1141                         goto exit;
1142                 }
1143         }
1144
1145         /* register all of the individual ports with the driver core */
1146         for (i = 0; i < num_ports; ++i) {
1147                 port = serial->port[i];
1148                 port->dev.parent = &interface->dev;
1149                 port->dev.driver = NULL;
1150                 port->dev.bus = &usb_serial_bus_type;
1151                 port->dev.release = &port_release;
1152
1153                 snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
1154                 dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
1155                 device_register (&port->dev);
1156         }
1157
1158         usb_serial_console_init (debug, minor);
1159
1160 exit:
1161         /* success */
1162         usb_set_intfdata (interface, serial);
1163         return 0;
1164
1165 probe_error:
1166         for (i = 0; i < num_bulk_in; ++i) {
1167                 port = serial->port[i];
1168                 if (!port)
1169                         continue;
1170                 if (port->read_urb)
1171                         usb_free_urb (port->read_urb);
1172                 kfree(port->bulk_in_buffer);
1173         }
1174         for (i = 0; i < num_bulk_out; ++i) {
1175                 port = serial->port[i];
1176                 if (!port)
1177                         continue;
1178                 if (port->write_urb)
1179                         usb_free_urb (port->write_urb);
1180                 kfree(port->bulk_out_buffer);
1181         }
1182         for (i = 0; i < num_interrupt_in; ++i) {
1183                 port = serial->port[i];
1184                 if (!port)
1185                         continue;
1186                 if (port->interrupt_in_urb)
1187                         usb_free_urb (port->interrupt_in_urb);
1188                 kfree(port->interrupt_in_buffer);
1189         }
1190
1191         /* return the minor range that this device had */
1192         return_serial (serial);
1193
1194         /* free up any memory that we allocated */
1195         for (i = 0; i < serial->num_port_pointers; ++i)
1196                 kfree(serial->port[i]);
1197         kfree (serial);
1198         return -EIO;
1199 }
1200
1201 void usb_serial_disconnect(struct usb_interface *interface)
1202 {
1203         struct usb_serial *serial = usb_get_intfdata (interface);
1204         struct device *dev = &interface->dev;
1205
1206         dbg ("%s", __FUNCTION__);
1207
1208         usb_set_intfdata (interface, NULL);
1209         if (serial) {
1210                 /* let the last holder of this object 
1211                  * cause it to be cleaned up */
1212                 kref_put(&serial->kref);
1213         }
1214         dev_info(dev, "device disconnected\n");
1215 }
1216
1217 static struct tty_operations serial_ops = {
1218         .open =                 serial_open,
1219         .close =                serial_close,
1220         .write =                serial_write,
1221         .write_room =           serial_write_room,
1222         .ioctl =                serial_ioctl,
1223         .set_termios =          serial_set_termios,
1224         .throttle =             serial_throttle,
1225         .unthrottle =           serial_unthrottle,
1226         .break_ctl =            serial_break,
1227         .chars_in_buffer =      serial_chars_in_buffer,
1228         .read_proc =            serial_read_proc,
1229         .tiocmget =             serial_tiocmget,
1230         .tiocmset =             serial_tiocmset,
1231 };
1232
1233 struct tty_driver *usb_serial_tty_driver;
1234
1235 static int __init usb_serial_init(void)
1236 {
1237         int i;
1238         int result = 0;
1239
1240         usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1241         if (!usb_serial_tty_driver)
1242                 return -ENOMEM;
1243
1244         /* Initialize our global data */
1245         for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1246                 serial_table[i] = NULL;
1247         }
1248
1249         bus_register(&usb_serial_bus_type);
1250
1251         /* register the generic driver, if we should */
1252         result = usb_serial_generic_register(debug);
1253         if (result < 0) {
1254                 err("%s - registering generic driver failed", __FUNCTION__);
1255                 goto exit;
1256         }
1257
1258         usb_serial_tty_driver->owner = THIS_MODULE;
1259         usb_serial_tty_driver->driver_name = "usbserial";
1260         usb_serial_tty_driver->devfs_name = "usb/tts/";
1261         usb_serial_tty_driver->name =   "ttyUSB";
1262         usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1263         usb_serial_tty_driver->minor_start = 0;
1264         usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1265         usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1266         usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1267         usb_serial_tty_driver->init_termios = tty_std_termios;
1268         usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1269         tty_set_operations(usb_serial_tty_driver, &serial_ops);
1270         result = tty_register_driver(usb_serial_tty_driver);
1271         if (result) {
1272                 err("%s - tty_register_driver failed", __FUNCTION__);
1273                 goto exit_generic;
1274         }
1275
1276         /* register the USB driver */
1277         result = usb_register(&usb_serial_driver);
1278         if (result < 0) {
1279                 err("%s - usb_register failed", __FUNCTION__);
1280                 goto exit_tty;
1281         }
1282
1283         info(DRIVER_DESC " " DRIVER_VERSION);
1284
1285         return result;
1286
1287 exit_tty:
1288         tty_unregister_driver(usb_serial_tty_driver);
1289
1290 exit_generic:
1291         usb_serial_generic_deregister();
1292
1293 exit:
1294         err ("%s - returning with error %d", __FUNCTION__, result);
1295         put_tty_driver(usb_serial_tty_driver);
1296         return result;
1297 }
1298
1299
1300 static void __exit usb_serial_exit(void)
1301 {
1302         usb_serial_console_exit();
1303
1304         usb_serial_generic_deregister();
1305
1306         usb_deregister(&usb_serial_driver);
1307         tty_unregister_driver(usb_serial_tty_driver);
1308         put_tty_driver(usb_serial_tty_driver);
1309         bus_unregister(&usb_serial_bus_type);
1310 }
1311
1312
1313 module_init(usb_serial_init);
1314 module_exit(usb_serial_exit);
1315
1316 #define set_to_generic_if_null(type, function)                          \
1317         do {                                                            \
1318                 if (!type->function) {                                  \
1319                         type->function = usb_serial_generic_##function; \
1320                         dbg("Had to override the " #function            \
1321                                  " usb serial operation with the generic one.");\
1322                         }                                               \
1323         } while (0)
1324
1325 static void fixup_generic(struct usb_serial_device_type *device)
1326 {
1327         set_to_generic_if_null(device, open);
1328         set_to_generic_if_null(device, write);
1329         set_to_generic_if_null(device, close);
1330         set_to_generic_if_null(device, write_room);
1331         set_to_generic_if_null(device, chars_in_buffer);
1332         set_to_generic_if_null(device, read_bulk_callback);
1333         set_to_generic_if_null(device, write_bulk_callback);
1334         set_to_generic_if_null(device, shutdown);
1335 }
1336
1337 int usb_serial_register(struct usb_serial_device_type *new_device)
1338 {
1339         int retval;
1340
1341         fixup_generic(new_device);
1342
1343         /* Add this device to our list of devices */
1344         list_add(&new_device->driver_list, &usb_serial_driver_list);
1345
1346         retval =  usb_serial_bus_register (new_device);
1347
1348         if (retval)
1349                 goto error;
1350
1351         info("USB Serial support registered for %s", new_device->name);
1352
1353         return retval;
1354 error:
1355         err("problem %d when registering driver %s", retval, new_device->name);
1356         list_del(&new_device->driver_list);
1357
1358         return retval;
1359 }
1360
1361
1362 void usb_serial_deregister(struct usb_serial_device_type *device)
1363 {
1364         info("USB Serial deregistering driver %s", device->name);
1365         list_del(&device->driver_list);
1366         usb_serial_bus_deregister(device);
1367 }
1368
1369
1370
1371 /* If the usb-serial core is built into the core, the usb-serial drivers
1372    need these symbols to load properly as modules. */
1373 EXPORT_SYMBOL(usb_serial_register);
1374 EXPORT_SYMBOL(usb_serial_deregister);
1375 EXPORT_SYMBOL(usb_serial_probe);
1376 EXPORT_SYMBOL(usb_serial_disconnect);
1377 EXPORT_SYMBOL(usb_serial_port_softint);
1378
1379
1380 /* Module information */
1381 MODULE_AUTHOR( DRIVER_AUTHOR );
1382 MODULE_DESCRIPTION( DRIVER_DESC );
1383 MODULE_LICENSE("GPL");
1384
1385 module_param(debug, bool, S_IRUGO | S_IWUSR);
1386 MODULE_PARM_DESC(debug, "Debug enabled or not");