added mtd driver
[linux-2.4.git] / drivers / usb / serial / usbserial.c
1 /*
2  * USB Serial Converter driver
3  *
4  * Copyright (C) 1999 - 2002 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  * (10/10/2001) gkh
18  *      usb_serial_disconnect() now sets the serial->dev pointer is to NULL to
19  *      help prevent child drivers from accessing the device since it is now
20  *      gone.
21  *
22  * (09/13/2001) gkh
23  *      Moved generic driver initialize after we have registered with the USB
24  *      core.  Thanks to Randy Dunlap for pointing this problem out.
25  *
26  * (07/03/2001) gkh
27  *      Fixed module paramater size.  Thanks to John Brockmeyer for the pointer.
28  *      Fixed vendor and product getting defined through the MODULE_PARM macro
29  *      if the Generic driver wasn't compiled in.
30  *      Fixed problem with generic_shutdown() not being called for drivers that
31  *      don't have a shutdown() function.
32  *
33  * (06/06/2001) gkh
34  *      added evil hack that is needed for the prolific pl2303 device due to the
35  *      crazy way its endpoints are set up.
36  *
37  * (05/30/2001) gkh
38  *      switched from using spinlock to a semaphore, which fixes lots of problems.
39  *
40  * (04/08/2001) gb
41  *      Identify version on module load.
42  *
43  * 2001_02_05 gkh
44  *      Fixed buffer overflows bug with the generic serial driver.  Thanks to
45  *      Todd Squires <squirest@ct0.com> for fixing this.
46  *
47  * (01/10/2001) gkh
48  *      Fixed bug where the generic serial adaptor grabbed _any_ device that was
49  *      offered to it.
50  *
51  * (12/12/2000) gkh
52  *      Removed MOD_INC and MOD_DEC from poll and disconnect functions, and
53  *      moved them to the serial_open and serial_close functions.
54  *      Also fixed bug with there not being a MOD_DEC for the generic driver
55  *      (thanks to Gary Brubaker for finding this.)
56  *
57  * (11/29/2000) gkh
58  *      Small NULL pointer initialization cleanup which saves a bit of disk image
59  *
60  * (11/01/2000) Adam J. Richter
61  *      instead of using idVendor/idProduct pairs, usb serial drivers
62  *      now identify their hardware interest with usb_device_id tables,
63  *      which they usually have anyhow for use with MODULE_DEVICE_TABLE.
64  *
65  * (10/05/2000) gkh
66  *      Fixed bug with urb->dev not being set properly, now that the usb
67  *      core needs it.
68  * 
69  * (09/11/2000) gkh
70  *      Removed DEBUG #ifdefs with call to usb_serial_debug_data
71  *
72  * (08/28/2000) gkh
73  *      Added port_lock to port structure.
74  *      Added locks for SMP safeness to generic driver
75  *      Fixed the ability to open a generic device's port more than once.
76  *
77  * (07/23/2000) gkh
78  *      Added bulk_out_endpointAddress to port structure.
79  *
80  * (07/19/2000) gkh, pberger, and borchers
81  *      Modifications to allow usb-serial drivers to be modules.
82  *
83  * (07/03/2000) gkh
84  *      Added more debugging to serial_ioctl call
85  * 
86  * (06/25/2000) gkh
87  *      Changed generic_write_bulk_callback to not call wake_up_interruptible
88  *      directly, but to have port_softint do it at a safer time.
89  *
90  * (06/23/2000) gkh
91  *      Cleaned up debugging statements in a quest to find UHCI timeout bug.
92  *
93  * (05/22/2000) gkh
94  *      Changed the makefile, enabling the big CONFIG_USB_SERIAL_SOMTHING to be 
95  *      removed from the individual device source files.
96  *
97  * (05/03/2000) gkh
98  *      Added the Digi Acceleport driver from Al Borchers and Peter Berger.
99  * 
100  * (05/02/2000) gkh
101  *      Changed devfs and tty register code to work properly now. This was based on
102  *      the ACM driver changes by Vojtech Pavlik.
103  *
104  * (04/27/2000) Ryan VanderBijl
105  *      Put calls to *_paranoia_checks into one function.
106  * 
107  * (04/23/2000) gkh
108  *      Fixed bug that Randy Dunlap found for Generic devices with no bulk out ports.
109  *      Moved when the startup code printed out the devices that are supported.
110  *
111  * (04/19/2000) gkh
112  *      Added driver for ZyXEL omni.net lcd plus ISDN TA
113  *      Made startup info message specify which drivers were compiled in.
114  *
115  * (04/03/2000) gkh
116  *      Changed the probe process to remove the module unload races.
117  *      Changed where the tty layer gets initialized to have devfs work nicer.
118  *      Added initial devfs support.
119  *
120  * (03/26/2000) gkh
121  *      Split driver up into device specific pieces.
122  * 
123  * (03/19/2000) gkh
124  *      Fixed oops that could happen when device was removed while a program
125  *      was talking to the device.
126  *      Removed the static urbs and now all urbs are created and destroyed
127  *      dynamically.
128  *      Reworked the internal interface. Now everything is based on the 
129  *      usb_serial_port structure instead of the larger usb_serial structure.
130  *      This fixes the bug that a multiport device could not have more than
131  *      one port open at one time.
132  *
133  * (03/17/2000) gkh
134  *      Added config option for debugging messages.
135  *      Added patch for keyspan pda from Brian Warner.
136  *
137  * (03/06/2000) gkh
138  *      Added the keyspan pda code from Brian Warner <warner@lothar.com>
139  *      Moved a bunch of the port specific stuff into its own structure. This
140  *      is in anticipation of the true multiport devices (there's a bug if you
141  *      try to access more than one port of any multiport device right now)
142  *
143  * (02/21/2000) gkh
144  *      Made it so that any serial devices only have to specify which functions
145  *      they want to overload from the generic function calls (great, 
146  *      inheritance in C, in a driver, just what I wanted...)
147  *      Added support for set_termios and ioctl function calls. No drivers take
148  *      advantage of this yet.
149  *      Removed the #ifdef MODULE, now there is no module specific code.
150  *      Cleaned up a few comments in usb-serial.h that were wrong (thanks again
151  *      to Miles Lott).
152  *      Small fix to get_free_serial.
153  *
154  * (02/14/2000) gkh
155  *      Removed the Belkin and Peracom functionality from the driver due to
156  *      the lack of support from the vendor, and me not wanting people to 
157  *      accidenatly buy the device, expecting it to work with Linux.
158  *      Added read_bulk_callback and write_bulk_callback to the type structure
159  *      for the needs of the FTDI and WhiteHEAT driver.
160  *      Changed all reverences to FTDI to FTDI_SIO at the request of Bill
161  *      Ryder.
162  *      Changed the output urb size back to the max endpoint size to make
163  *      the ftdi_sio driver have it easier, and due to the fact that it didn't
164  *      really increase the speed any.
165  *
166  * (02/11/2000) gkh
167  *      Added VISOR_FUNCTION_CONSOLE to the visor startup function. This was a
168  *      patch from Miles Lott (milos@insync.net).
169  *      Fixed bug with not restoring the minor range that a device grabs, if
170  *      the startup function fails (thanks Miles for finding this).
171  *
172  * (02/05/2000) gkh
173  *      Added initial framework for the Keyspan PDA serial converter so that
174  *      Brian Warner has a place to put his code.
175  *      Made the ezusb specific functions generic enough that different
176  *      devices can use them (whiteheat and keyspan_pda both need them).
177  *      Split out a whole bunch of structure and other stuff to a seperate
178  *      usb-serial.h file.
179  *      Made the Visor connection messages a little more understandable, now
180  *      that Miles Lott (milos@insync.net) has gotten the Generic channel to
181  *      work. Also made them always show up in the log file.
182  * 
183  * (01/25/2000) gkh
184  *      Added initial framework for FTDI serial converter so that Bill Ryder
185  *      has a place to put his code.
186  *      Added the vendor specific info from Handspring. Now we can print out
187  *      informational debug messages as well as understand what is happening.
188  *
189  * (01/23/2000) gkh
190  *      Fixed problem of crash when trying to open a port that didn't have a
191  *      device assigned to it. Made the minor node finding a little smarter,
192  *      now it looks to find a continous space for the new device.
193  *
194  * (01/21/2000) gkh
195  *      Fixed bug in visor_startup with patch from Miles Lott (milos@insync.net)
196  *      Fixed get_serial_by_minor which was all messed up for multi port 
197  *      devices. Fixed multi port problem for generic devices. Now the number
198  *      of ports is determined by the number of bulk out endpoints for the
199  *      generic device.
200  *
201  * (01/19/2000) gkh
202  *      Removed lots of cruft that was around from the old (pre urb) driver 
203  *      interface.
204  *      Made the serial_table dynamic. This should save lots of memory when
205  *      the number of minor nodes goes up to 256.
206  *      Added initial support for devices that have more than one port. 
207  *      Added more debugging comments for the Visor, and added a needed 
208  *      set_configuration call.
209  *
210  * (01/17/2000) gkh
211  *      Fixed the WhiteHEAT firmware (my processing tool had a bug)
212  *      and added new debug loader firmware for it.
213  *      Removed the put_char function as it isn't really needed.
214  *      Added visor startup commands as found by the Win98 dump.
215  * 
216  * (01/13/2000) gkh
217  *      Fixed the vendor id for the generic driver to the one I meant it to be.
218  *
219  * (01/12/2000) gkh
220  *      Forget the version numbering...that's pretty useless...
221  *      Made the driver able to be compiled so that the user can select which
222  *      converter they want to use. This allows people who only want the Visor
223  *      support to not pay the memory size price of the WhiteHEAT.
224  *      Fixed bug where the generic driver (idVendor=0000 and idProduct=0000)
225  *      grabbed the root hub. Not good.
226  * 
227  * version 0.4.0 (01/10/2000) gkh
228  *      Added whiteheat.h containing the firmware for the ConnectTech WhiteHEAT
229  *      device. Added startup function to allow firmware to be downloaded to
230  *      a device if it needs to be.
231  *      Added firmware download logic to the WhiteHEAT device.
232  *      Started to add #defines to split up the different drivers for potential
233  *      configuration option.
234  *      
235  * version 0.3.1 (12/30/99) gkh
236  *      Fixed problems with urb for bulk out.
237  *      Added initial support for multiple sets of endpoints. This enables
238  *      the Handspring Visor to be attached successfully. Only the first
239  *      bulk in / bulk out endpoint pair is being used right now.
240  *
241  * version 0.3.0 (12/27/99) gkh
242  *      Added initial support for the Handspring Visor based on a patch from
243  *      Miles Lott (milos@sneety.insync.net)
244  *      Cleaned up the code a bunch and converted over to using urbs only.
245  *
246  * version 0.2.3 (12/21/99) gkh
247  *      Added initial support for the Connect Tech WhiteHEAT converter.
248  *      Incremented the number of ports in expectation of getting the
249  *      WhiteHEAT to work properly (4 ports per connection).
250  *      Added notification on insertion and removal of what port the
251  *      device is/was connected to (and what kind of device it was).
252  *
253  * version 0.2.2 (12/16/99) gkh
254  *      Changed major number to the new allocated number. We're legal now!
255  *
256  * version 0.2.1 (12/14/99) gkh
257  *      Fixed bug that happens when device node is opened when there isn't a
258  *      device attached to it. Thanks to marek@webdesign.no for noticing this.
259  *
260  * version 0.2.0 (11/10/99) gkh
261  *      Split up internals to make it easier to add different types of serial 
262  *      converters to the code.
263  *      Added a "generic" driver that gets it's vendor and product id
264  *      from when the module is loaded. Thanks to David E. Nelson (dnelson@jump.net)
265  *      for the idea and sample code (from the usb scanner driver.)
266  *      Cleared up any licensing questions by releasing it under the GNU GPL.
267  *
268  * version 0.1.2 (10/25/99) gkh
269  *      Fixed bug in detecting device.
270  *
271  * version 0.1.1 (10/05/99) gkh
272  *      Changed the major number to not conflict with anything else.
273  *
274  * version 0.1 (09/28/99) gkh
275  *      Can recognize the two different devices and start up a read from
276  *      device when asked to. Writes also work. No control signals yet, this
277  *      all is vendor specific data (i.e. no spec), also no control for
278  *      different baud rates or other bit settings.
279  *      Currently we are using the same devid as the acm driver. This needs
280  *      to change.
281  * 
282  */
283
284 #include <linux/config.h>
285 #include <linux/kernel.h>
286 #include <linux/sched.h>
287 #include <linux/signal.h>
288 #include <linux/errno.h>
289 #include <linux/poll.h>
290 #include <linux/init.h>
291 #include <linux/slab.h>
292 #include <linux/fcntl.h>
293 #include <linux/tty.h>
294 #include <linux/tty_driver.h>
295 #include <linux/tty_flip.h>
296 #include <linux/module.h>
297 #include <linux/spinlock.h>
298 #include <linux/list.h>
299 #include <linux/smp_lock.h>
300 #include <linux/usb.h>
301
302 #ifdef CONFIG_USB_SERIAL_DEBUG
303         static int debug = 1;
304 #else
305         static int debug;
306 #endif
307
308 #include "usb-serial.h"
309 #include "pl2303.h"
310
311 /*
312  * Version Information
313  */
314 #define DRIVER_VERSION "v1.4"
315 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux-usb/"
316 #define DRIVER_DESC "USB Serial Driver core"
317
318 /* function prototypes for a "generic" type serial converter (no flow control, not all endpoints needed) */
319 /* need to always compile these in, as some of the other devices use these functions as their own. */
320 /* if a driver does not provide a function pointer, the generic function will be called. */
321 static int  generic_open                (struct usb_serial_port *port, struct file *filp);
322 static void generic_close               (struct usb_serial_port *port, struct file *filp);
323 static int  generic_write               (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
324 static int  generic_write_room          (struct usb_serial_port *port);
325 static int  generic_chars_in_buffer     (struct usb_serial_port *port);
326 static void generic_read_bulk_callback  (struct urb *urb);
327 static void generic_write_bulk_callback (struct urb *urb);
328 static void generic_shutdown            (struct usb_serial *serial);
329
330
331 #ifdef CONFIG_USB_SERIAL_GENERIC
332 static __u16    vendor  = 0x05f9;
333 static __u16    product = 0xffff;
334
335 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
336
337 /* All of the device info needed for the Generic Serial Converter */
338 static struct usb_serial_device_type generic_device = {
339         .owner =                THIS_MODULE,
340         .name =                 "Generic",
341         .id_table =             generic_device_ids,
342         .num_interrupt_in =     NUM_DONT_CARE,
343         .num_bulk_in =          NUM_DONT_CARE,
344         .num_bulk_out =         NUM_DONT_CARE,
345         .num_ports =            1,
346         .shutdown =             generic_shutdown,
347 };
348 #endif
349
350 /*
351  * The post kludge structures and variables.
352  */
353 #define POST_BSIZE      100     /* little below 128 in total */
354 struct usb_serial_post_job {
355         struct list_head link;
356         struct usb_serial_port *port;
357         int len;
358         char buff[POST_BSIZE];
359 };
360 static spinlock_t post_lock = SPIN_LOCK_UNLOCKED;       /* Also covers ->ref */
361 static struct list_head post_list = LIST_HEAD_INIT(post_list);
362 static struct tq_struct post_task;
363
364 /* local function prototypes */
365 static int  serial_open (struct tty_struct *tty, struct file * filp);
366 static void serial_close (struct tty_struct *tty, struct file * filp);
367 static int __serial_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
368 static int  serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count);
369 static int  serial_post_job(struct usb_serial_port *port, int from_user,
370     int gfp, const unsigned char *buf, int count);
371 static int  serial_post_one(struct usb_serial_port *port, int from_user,
372     int gfp, const unsigned char *buf, int count);
373 static int  serial_write_room (struct tty_struct *tty);
374 static int  serial_chars_in_buffer (struct tty_struct *tty);
375 static void serial_throttle (struct tty_struct * tty);
376 static void serial_unthrottle (struct tty_struct * tty);
377 static int  serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg);
378 static void serial_set_termios (struct tty_struct *tty, struct termios * old);
379 static void serial_shutdown (struct usb_serial *serial);
380
381 static void * usb_serial_probe(struct usb_device *dev, unsigned int ifnum,
382                                const struct usb_device_id *id);
383 static void usb_serial_disconnect(struct usb_device *dev, void *ptr);
384
385 static struct usb_driver usb_serial_driver = {
386         .name =         "serial",
387         .probe =        usb_serial_probe,
388         .disconnect =   usb_serial_disconnect,
389         .id_table =     NULL,                   /* check all devices */
390 };
391
392 /* There is no MODULE_DEVICE_TABLE for usbserial.c.  Instead
393    the MODULE_DEVICE_TABLE declarations in each serial driver
394    cause the "hotplug" program to pull in whatever module is necessary
395    via modprobe, and modprobe will load usbserial because the serial
396    drivers depend on it.
397 */
398
399
400 static int                      serial_refcount;
401 static struct tty_driver        serial_tty_driver;
402 static struct tty_struct *      serial_tty[SERIAL_TTY_MINORS];
403 static struct termios *         serial_termios[SERIAL_TTY_MINORS];
404 static struct termios *         serial_termios_locked[SERIAL_TTY_MINORS];
405 static struct usb_serial        *serial_table[SERIAL_TTY_MINORS];       /* initially all NULL */
406
407
408 static LIST_HEAD(usb_serial_driver_list);
409
410
411 struct usb_serial *usb_serial_get_serial(struct usb_serial_port *port,
412     const char *function)
413 {
414
415         /* if no port was specified, or it fails a paranoia check */
416         if (!port ||
417             port_paranoia_check (port, function) ||
418             serial_paranoia_check (port->serial, function)) {
419                 return NULL;
420         }
421
422         /* disconnected, cut off all operations */
423         if (port->serial->dev == NULL)
424                 return NULL;
425
426         return port->serial;
427 }
428
429
430 static struct usb_serial *get_serial_by_minor (unsigned int minor)
431 {
432         return serial_table[minor];
433 }
434
435
436 static struct usb_serial *get_free_serial (int num_ports, int *minor)
437 {
438         struct usb_serial *serial = NULL;
439         int i, j;
440         int good_spot;
441
442         dbg("%s %d", __FUNCTION__, num_ports);
443
444         *minor = 0;
445         for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
446                 if (serial_table[i])
447                         continue;
448
449                 good_spot = 1;
450                 for (j = 1; j <= num_ports-1; ++j)
451                         if (serial_table[i+j])
452                                 good_spot = 0;
453                 if (good_spot == 0)
454                         continue;
455                         
456                 if (!(serial = kmalloc(sizeof(struct usb_serial), GFP_KERNEL))) {
457                         err("%s - Out of memory", __FUNCTION__);
458                         return NULL;
459                 }
460                 memset(serial, 0, sizeof(struct usb_serial));
461                 serial->magic = USB_SERIAL_MAGIC;
462                 serial_table[i] = serial;
463                 *minor = i;
464                 dbg("%s - minor base = %d", __FUNCTION__, *minor);
465                 for (i = *minor+1; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
466                         serial_table[i] = serial;
467                 return serial;
468         }
469         return NULL;
470 }
471
472 static void return_serial (struct usb_serial *serial)
473 {
474         int i;
475
476         dbg("%s", __FUNCTION__);
477
478         if (serial == NULL)
479                 return;
480
481         for (i = 0; i < serial->num_ports; ++i) {
482                 serial_table[serial->minor + i] = NULL;
483         }
484
485         return;
486 }
487
488 /*
489  * A regular foo_put(), except a) it's open-coded without kref, and
490  * b) it's not the only place which does --serial->ref (due to locking).
491  *
492  * This does not do an equivalent of return_serial() because serial_table[]
493  * has a lifetime from probe to disconnect.
494  */
495 static void serial_put(struct usb_serial *serial)
496 {
497         unsigned long flags;
498
499         spin_lock_irqsave(&post_lock, flags);
500         if (--serial->ref == 0)
501                 kfree(serial);
502         spin_unlock_irqrestore(&post_lock, flags);
503 }
504
505 /*
506  * The post kludge.
507  *
508  * Our component drivers are hideously buggy and written by people
509  * who have difficulty understanding the concept of spinlocks.
510  * There were so many races and lockups that Greg K-H made a watershed
511  * decision to provide what is essentially a single-threaded sandbox
512  * for component drivers, protected by a semaphore. It helped a lot, but
513  * for one little problem: when tty->low_latency is set, line disciplines
514  * can call ->write from an interrupt, where the semaphore oopses.
515  *
516  * Rather than open the whole can of worms again, we just post writes
517  * into a helper which can sleep.
518  *
519  * Kernel 2.6 has a proper fix. It replaces semaphores with proper locking.
520  */
521 static void post_helper(void *arg)
522 {
523         struct list_head *pos;
524         struct usb_serial_post_job *job;
525         struct usb_serial_port *port;
526         struct usb_serial *serial;
527         unsigned long flags;
528
529         spin_lock_irqsave(&post_lock, flags);
530         pos = post_list.next;
531         while (pos != &post_list) {
532                 job = list_entry(pos, struct usb_serial_post_job, link);
533                 port = job->port;
534                 /* get_usb_serial checks serial->dev, so cannot be used */
535                 serial = port->serial;
536                 if (port->write_busy) {
537                         dbg("%s - port %d busy", __FUNCTION__, port->number);
538                         pos = pos->next;
539                         continue;
540                 }
541                 list_del(&job->link);
542                 spin_unlock_irqrestore(&post_lock, flags);
543
544                 down(&port->sem);
545                 dbg("%s - port %d len %d backlog %d", __FUNCTION__,
546                     port->number, job->len, port->write_backlog);
547                 if (serial->dev != NULL) {
548                         int rc;
549                         int sent = 0;
550                         while (sent < job->len) {
551                                 rc = __serial_write(port, 0, job->buff + sent, job->len - sent);
552                                 if ((rc < 0) || signal_pending(current))
553                                         break;
554                                 sent += rc;
555                                 if ((sent < job->len) && current->need_resched)
556                                         schedule();
557                         }
558                 }
559                 up(&port->sem);
560
561                 spin_lock_irqsave(&post_lock, flags);
562                 port->write_backlog -= job->len;
563                 kfree(job);
564                 if (--serial->ref == 0)
565                         kfree(serial);
566                 /* Have to reset because we dropped spinlock */
567                 pos = post_list.next;
568         }
569         spin_unlock_irqrestore(&post_lock, flags);
570 }
571
572 #ifdef USES_EZUSB_FUNCTIONS
573 /* EZ-USB Control and Status Register.  Bit 0 controls 8051 reset */
574 #define CPUCS_REG    0x7F92
575
576 int ezusb_writememory (struct usb_serial *serial, int address, unsigned char *data, int length, __u8 bRequest)
577 {
578         int result;
579         unsigned char *transfer_buffer;
580
581         /* dbg("ezusb_writememory %x, %d", address, length); */
582         if (!serial->dev) {
583                 dbg("%s - no physical device present, failing.", __FUNCTION__);
584                 return -ENODEV;
585         }
586
587         transfer_buffer =  kmalloc (length, GFP_KERNEL);
588         if (!transfer_buffer) {
589                 err("%s - kmalloc(%d) failed.", __FUNCTION__, length);
590                 return -ENOMEM;
591         }
592         memcpy (transfer_buffer, data, length);
593         result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), bRequest, 0x40, address, 0, transfer_buffer, length, 3*HZ);
594         kfree (transfer_buffer);
595         return result;
596 }
597
598 int ezusb_set_reset (struct usb_serial *serial, unsigned char reset_bit)
599 {
600         int     response;
601         dbg("%s - %d", __FUNCTION__, reset_bit);
602         response = ezusb_writememory (serial, CPUCS_REG, &reset_bit, 1, 0xa0);
603         if (response < 0) {
604                 err("%s- %d failed", __FUNCTION__, reset_bit);
605         }
606         return response;
607 }
608
609 #endif  /* USES_EZUSB_FUNCTIONS */
610
611 /*****************************************************************************
612  * Driver tty interface functions
613  *****************************************************************************/
614 static int serial_open (struct tty_struct *tty, struct file * filp)
615 {
616         struct usb_serial *serial;
617         struct usb_serial_port *port;
618         unsigned int portNumber;
619         int retval = 0;
620         unsigned long flags;
621
622         dbg("%s", __FUNCTION__);
623
624         /* initialize the pointer incase something fails */
625         tty->driver_data = NULL;
626
627         /*
628          * In a sane refcounting system, this would've been called serial_get().
629          */
630         spin_lock_irqsave(&post_lock, flags);
631         /* get the serial object associated with this tty pointer */
632         serial = get_serial_by_minor (MINOR(tty->device));
633         if (serial_paranoia_check(serial, __FUNCTION__) || serial->dev == NULL) {
634                 spin_unlock_irqrestore(&post_lock, flags);
635                 return -ENODEV;
636         }
637         serial->ref++;          /* Protect the port->sem from kfree() */
638         spin_unlock_irqrestore(&post_lock, flags);
639
640         /* set up our port structure making the tty driver remember our port object, and us it */
641         portNumber = MINOR(tty->device) - serial->minor;
642         port = &serial->port[portNumber];
643         tty->driver_data = port;
644
645         down (&port->sem);
646         port->tty = tty;
647
648         /* lock this module before we call it */
649         if (serial->type->owner)
650                 __MOD_INC_USE_COUNT(serial->type->owner);
651
652         ++port->open_count;
653         if (port->open_count == 1) {
654                 /* only call the device specific open if this 
655                  * is the first time the port is opened */
656                 if (serial->type->open)
657                         retval = serial->type->open(port, filp);
658                 else
659                         retval = generic_open(port, filp);
660         }
661
662         if (retval) {
663                 port->open_count = 0;
664                 if (serial->type->owner)
665                         __MOD_DEC_USE_COUNT(serial->type->owner);
666         }
667
668         up (&port->sem);
669         if (retval)
670                 serial_put(serial);
671         return retval;
672 }
673
674 static void __serial_close(struct usb_serial_port *port, struct file *filp)
675 {
676
677         if (!port->open_count) {
678                 err("%s - port %d: not open", __FUNCTION__, port->number);
679                 return;
680         }
681
682         --port->open_count;
683         if (port->open_count <= 0) {
684                 /* only call the device specific close if this 
685                  * port is being closed by the last owner */
686                 if (port->serial->type->close)
687                         port->serial->type->close(port, filp);
688                 else
689                         generic_close(port, filp);
690                 port->open_count = 0;
691                 if (port->tty) {
692                         port->tty->driver_data = NULL;
693                         port->tty = NULL;
694                 }
695         }
696
697         if (port->serial->type->owner)
698                 __MOD_DEC_USE_COUNT(port->serial->type->owner);
699 }
700
701 static void serial_close(struct tty_struct *tty, struct file * filp)
702 {
703         struct usb_serial_port *port;
704         struct usb_serial *serial;
705
706         if ((port = tty->driver_data) == NULL) {
707                 /* This happens if someone opened us with O_NDELAY */
708                 return;
709         }
710         if ((serial = port->serial) == NULL) {
711                 err("%s - port %d: not open (count %d)", __FUNCTION__, port->number, port->open_count);
712                 return;
713         }
714
715         dbg("%s - port %d", __FUNCTION__, port->number);
716
717         tty->closing = 1;
718         if (serial->dev != NULL) {
719                 /* In most drivers, this is set with setserial */
720                 /** if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) **/
721                 tty_wait_until_sent(tty, /** info->closing_wait **/ 30*HZ);
722         }
723
724         down (&port->sem);
725         __serial_close(port, filp);
726         up (&port->sem);
727
728         serial_put(serial);
729         tty->closing = 0;
730 }
731
732 static int __serial_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
733 {
734         struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
735         int retval = -EINVAL;
736
737         if (!serial)
738                 return -ENODEV;
739
740         dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
741
742         if (!port->open_count) {
743                 dbg("%s - port not open", __FUNCTION__);
744                 goto exit;
745         }
746
747         /* pass on to the driver specific version of this function if it is available */
748         if (serial->type->write)
749                 retval = serial->type->write(port, from_user, buf, count);
750         else
751                 retval = generic_write(port, from_user, buf, count);
752
753 exit:
754         return retval;
755 }
756
757 static int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
758 {
759         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
760         int rc;
761
762         if (!port)
763                 return -ENODEV;
764
765         if (!in_interrupt()) {
766                 /*
767                  * Run post_list to reduce a possiblity of reordered writes.
768                  * Tasks can make keventd to sleep, sometimes for a long time.
769                  */
770                 post_helper(NULL);
771
772                 down(&port->sem);
773                 /*
774                  * This happens when a line discipline asks how much room
775                  * we have, gets 64, then tries to perform two writes
776                  * for a byte each. First write takes whole URB, second
777                  * write hits this check.
778                  */
779                 if (port->write_busy) {
780                         up(&port->sem);
781                         return serial_post_job(port, from_user, GFP_KERNEL,
782                             buf, count);
783                 }
784
785                 rc = __serial_write(port, from_user, buf, count);
786                 up(&port->sem);
787                 return rc;
788         }
789
790         if (from_user) {
791                 /*
792                  * This is a BUG-able offense because we cannot
793                  * pagefault while in_interrupt, but we want to see
794                  * something in dmesg rather than just blinking LEDs.
795                  */
796                 err("user data in interrupt write");
797                 return -EINVAL;
798         }
799
800         return serial_post_job(port, 0, GFP_ATOMIC, buf, count);
801 }
802
803 static int serial_post_job(struct usb_serial_port *port, int from_user,
804     int gfp, const unsigned char *buf, int count)
805 {
806         int done = 0, length;
807         int rc;
808
809         if (port == NULL)
810                 return -EPIPE;
811
812         if (count >= 512) {
813                 static int rate = 0;
814                 /*
815                  * Data loss due to extreme circumstances.
816                  * It's a ususal thing on serial to lose characters, isn't it?
817                  * Neener, neener! Actually, it's probably an echo loop anyway.
818                  * Only happens when getty starts talking to Visor.
819                  */
820                 if (++rate % 1000 < 3) {
821                         err("too much data (%d) from %s", count,
822                             from_user? "user": "kernel");
823                 }
824                 count = 512;
825         }
826
827         while (done < count) {
828                 length = count - done;
829                 if (length > POST_BSIZE)
830                         length = POST_BSIZE;
831                 if (length > port->bulk_out_size)
832                         length = port->bulk_out_size;
833
834                 rc = serial_post_one(port, from_user, gfp, buf + done, length);
835                 if (rc <= 0) {
836                         if (done != 0)
837                                 return done;
838                         return rc;
839                 }
840                 done += rc;
841         }
842
843         return done;
844 }
845
846 static int serial_post_one(struct usb_serial_port *port, int from_user,
847     int gfp, const unsigned char *buf, int count)
848 {
849         struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
850         struct usb_serial_post_job *job;
851         unsigned long flags;
852
853         if (!serial)
854                 return -ENODEV;
855
856         dbg("%s - port %d user %d count %d", __FUNCTION__, port->number, from_user, count);
857
858         job = kmalloc(sizeof(struct usb_serial_post_job), gfp);
859         if (job == NULL)
860                 return -ENOMEM;
861
862         job->port = port;
863         if (count >= POST_BSIZE)
864                 count = POST_BSIZE;
865         job->len = count;
866
867         if (from_user) {
868                 if (copy_from_user(job->buff, buf, count)) {
869                         kfree(job);
870                         return -EFAULT;
871                 }
872         } else {
873                 memcpy(job->buff, buf, count);
874         }
875
876         spin_lock_irqsave(&post_lock, flags);
877         port->write_backlog += count;
878         list_add_tail(&job->link, &post_list);
879         serial->ref++;          /* Protect the port->sem from kfree() */
880         schedule_task(&post_task);
881         spin_unlock_irqrestore(&post_lock, flags);
882
883         return count;
884 }
885
886 static int serial_write_room (struct tty_struct *tty) 
887 {
888         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
889         struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
890         int retval = -EINVAL;
891
892         if (!serial)
893                 return -ENODEV;
894
895         if (in_interrupt()) {
896                 retval = 0;
897                 if (!port->write_busy && port->write_backlog == 0)
898                         retval = port->bulk_out_size;
899                 dbg("%s - returns %d", __FUNCTION__, retval);
900                 return retval;
901         }
902
903         down (&port->sem);
904
905         dbg("%s - port %d", __FUNCTION__, port->number);
906
907         if (!port->open_count) {
908                 dbg("%s - port not open", __FUNCTION__);
909                 goto exit;
910         }
911
912         /* pass on to the driver specific version of this function if it is available */
913         if (serial->type->write_room)
914                 retval = serial->type->write_room(port);
915         else
916                 retval = generic_write_room(port);
917
918 exit:
919         up (&port->sem);
920         return retval;
921 }
922
923 static int serial_chars_in_buffer (struct tty_struct *tty) 
924 {
925         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
926         struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
927         int retval = -EINVAL;
928
929         if (!serial)
930                 return -ENODEV;
931
932         down (&port->sem);
933
934         if (!port->open_count) {
935                 dbg("%s - port %d: not open", __FUNCTION__, port->number);
936                 goto exit;
937         }
938
939         /* pass on to the driver specific version of this function if it is available */
940         if (serial->type->chars_in_buffer)
941                 retval = serial->type->chars_in_buffer(port);
942         else
943                 retval = generic_chars_in_buffer(port);
944
945 exit:
946         up (&port->sem);
947         return retval;
948 }
949
950 static void serial_throttle (struct tty_struct * tty)
951 {
952         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
953         struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
954
955         if (!serial)
956                 return;
957
958         down (&port->sem);
959
960         dbg("%s - port %d", __FUNCTION__, port->number);
961
962         if (!port->open_count) {
963                 dbg ("%s - port not open", __FUNCTION__);
964                 goto exit;
965         }
966
967         /* pass on to the driver specific version of this function */
968         if (serial->type->throttle)
969                 serial->type->throttle(port);
970
971 exit:
972         up (&port->sem);
973 }
974
975 static void serial_unthrottle (struct tty_struct * tty)
976 {
977         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
978         struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
979
980         if (!serial)
981                 return;
982
983         down (&port->sem);
984
985         dbg("%s - port %d", __FUNCTION__, port->number);
986
987         if (!port->open_count) {
988                 dbg("%s - port not open", __FUNCTION__);
989                 goto exit;
990         }
991
992         /* pass on to the driver specific version of this function */
993         if (serial->type->unthrottle)
994                 serial->type->unthrottle(port);
995
996 exit:
997         up (&port->sem);
998 }
999
1000 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
1001 {
1002         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
1003         struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
1004         int retval = -ENODEV;
1005
1006         if (!serial)
1007                 return -ENODEV;
1008
1009         down (&port->sem);
1010
1011         dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
1012
1013         if (!port->open_count) {
1014                 dbg ("%s - port not open", __FUNCTION__);
1015                 goto exit;
1016         }
1017
1018         /* pass on to the driver specific version of this function if it is available */
1019         if (serial->type->ioctl)
1020                 retval = serial->type->ioctl(port, file, cmd, arg);
1021         else
1022                 retval = -ENOIOCTLCMD;
1023
1024 exit:
1025         up (&port->sem);
1026         return retval;
1027 }
1028
1029 static void serial_set_termios (struct tty_struct *tty, struct termios * old)
1030 {
1031         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
1032         struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
1033
1034         if (!serial)
1035                 return;
1036
1037         down (&port->sem);
1038
1039         dbg("%s - port %d", __FUNCTION__, port->number);
1040
1041         if (!port->open_count) {
1042                 dbg("%s - port not open", __FUNCTION__);
1043                 goto exit;
1044         }
1045
1046         /* pass on to the driver specific version of this function if it is available */
1047         if (serial->type->set_termios)
1048                 serial->type->set_termios(port, old);
1049
1050 exit:
1051         up (&port->sem);
1052 }
1053
1054 static void serial_break (struct tty_struct *tty, int break_state)
1055 {
1056         struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
1057         struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
1058
1059         if (!serial)
1060                 return;
1061
1062         down (&port->sem);
1063
1064         dbg("%s - port %d", __FUNCTION__, port->number);
1065
1066         if (!port->open_count) {
1067                 dbg("%s - port not open", __FUNCTION__);
1068                 goto exit;
1069         }
1070
1071         /* pass on to the driver specific version of this function if it is available */
1072         if (serial->type->break_ctl)
1073                 serial->type->break_ctl(port, break_state);
1074
1075 exit:
1076         up (&port->sem);
1077 }
1078
1079 static void serial_shutdown (struct usb_serial *serial)
1080 {
1081         dbg ("%s", __FUNCTION__);
1082
1083         if (serial->type->shutdown)
1084                 serial->type->shutdown(serial);
1085         else
1086                 generic_shutdown(serial);
1087 }
1088
1089 static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
1090 {
1091         struct usb_serial *serial;
1092         int length = 0;
1093         int i;
1094         off_t begin = 0;
1095         char tmp[40];
1096
1097         dbg("%s", __FUNCTION__);
1098         length += sprintf (page, "usbserinfo:1.0 driver:%s\n", DRIVER_VERSION);
1099         for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
1100                 serial = get_serial_by_minor(i);
1101                 if (serial == NULL)
1102                         continue;
1103
1104                 length += sprintf (page+length, "%d:", i);
1105                 if (serial->type->owner)
1106                         length += sprintf (page+length, " module:%s", serial->type->owner->name);
1107                 length += sprintf (page+length, " name:\"%s\"", serial->type->name);
1108                 length += sprintf (page+length, " vendor:%04x product:%04x", serial->vendor, serial->product);
1109                 length += sprintf (page+length, " num_ports:%d", serial->num_ports);
1110                 length += sprintf (page+length, " port:%d", i - serial->minor + 1);
1111
1112                 usb_make_path(serial->dev, tmp, sizeof(tmp));
1113                 length += sprintf (page+length, " path:%s", tmp);
1114                         
1115                 length += sprintf (page+length, "\n");
1116                 if ((length + begin) > (off + count))
1117                         goto done;
1118                 if ((length + begin) < off) {
1119                         begin += length;
1120                         length = 0;
1121                 }
1122         }
1123         *eof = 1;
1124 done:
1125         if (off >= (length + begin))
1126                 return 0;
1127         *start = page + (off-begin);
1128         return ((count < begin+length-off) ? count : begin+length-off);
1129 }
1130
1131 /*****************************************************************************
1132  * generic devices specific driver functions
1133  *****************************************************************************/
1134 static int generic_open (struct usb_serial_port *port, struct file *filp)
1135 {
1136         struct usb_serial *serial = port->serial;
1137         int result = 0;
1138
1139         if (port_paranoia_check (port, __FUNCTION__))
1140                 return -ENODEV;
1141
1142         dbg("%s - port %d", __FUNCTION__, port->number);
1143
1144         /* force low_latency on so that our tty_push actually forces the data through, 
1145            otherwise it is scheduled, and with high data rates (like with OHCI) data
1146            can get lost. */
1147         if (port->tty)
1148                 port->tty->low_latency = 1;
1149
1150         /* if we have a bulk interrupt, start reading from it */
1151         if (serial->num_bulk_in) {
1152                 /* Start reading from the device */
1153                 usb_fill_bulk_urb (port->read_urb, serial->dev,
1154                                    usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
1155                                    port->read_urb->transfer_buffer,
1156                                    port->read_urb->transfer_buffer_length,
1157                                    ((serial->type->read_bulk_callback) ?
1158                                      serial->type->read_bulk_callback :
1159                                      generic_read_bulk_callback),
1160                                    port);
1161                 result = usb_submit_urb(port->read_urb);
1162                 if (result)
1163                         err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
1164         }
1165
1166         return result;
1167 }
1168
1169 static void generic_cleanup (struct usb_serial_port *port)
1170 {
1171         struct usb_serial *serial = port->serial;
1172
1173         dbg("%s - port %d", __FUNCTION__, port->number);
1174
1175         if (serial->dev) {
1176                 /* shutdown any bulk reads that might be going on */
1177                 if (serial->num_bulk_out)
1178                         usb_unlink_urb (port->write_urb);
1179                 if (serial->num_bulk_in)
1180                         usb_unlink_urb (port->read_urb);
1181         }
1182 }
1183
1184 static void generic_close (struct usb_serial_port *port, struct file * filp)
1185 {
1186         dbg("%s - port %d", __FUNCTION__, port->number);
1187         generic_cleanup (port);
1188 }
1189
1190 static int generic_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
1191 {
1192         struct usb_serial *serial = port->serial;
1193         int result;
1194         unsigned long flags;
1195
1196         if (count == 0) {
1197                 dbg("%s - write request of 0 bytes", __FUNCTION__);
1198                 return (0);
1199         }
1200         if (count < 0) {
1201                 err("%s - port %d: write request of %d bytes", __FUNCTION__,
1202                     port->number, count);
1203                 return (0);
1204         }
1205
1206         /* only do something if we have a bulk out endpoint */
1207         if (serial->num_bulk_out) {
1208                 if (port->write_busy) {
1209                         /* Happens when two threads run port_helper. Watch. */
1210                         info("%s - already writing", __FUNCTION__);
1211                         return (0);
1212                 }
1213
1214                 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
1215
1216                 if (from_user) {
1217                         if (copy_from_user(port->write_urb->transfer_buffer, buf, count))
1218                                 return -EFAULT;
1219                 } else {
1220                         memcpy (port->write_urb->transfer_buffer, buf, count);
1221                 }
1222                 dbg("%s - port %d [%d]", __FUNCTION__, port->number, count);
1223
1224                 /* set up our urb */
1225                 usb_fill_bulk_urb (port->write_urb, serial->dev,
1226                                    usb_sndbulkpipe (serial->dev,
1227                                                     port->bulk_out_endpointAddress),
1228                                    port->write_urb->transfer_buffer, count,
1229                                    ((serial->type->write_bulk_callback) ? 
1230                                      serial->type->write_bulk_callback :
1231                                      generic_write_bulk_callback), port);
1232
1233                 /* send the data out the bulk port */
1234                 port->write_busy = 1;
1235                 result = usb_submit_urb(port->write_urb);
1236                 if (result) {
1237                         err("%s - port %d: failed submitting write urb (%d)",
1238                              __FUNCTION__, port->number, result);
1239                         port->write_busy = 0;
1240                         spin_lock_irqsave(&post_lock, flags);
1241                         if (port->write_backlog != 0)
1242                                 schedule_task(&post_task);
1243                         spin_unlock_irqrestore(&post_lock, flags);
1244
1245                 } else
1246                         result = count;
1247
1248                 return result;
1249         }
1250
1251         /* no bulk out, so return 0 bytes written */
1252         return (0);
1253 }
1254
1255 static int generic_write_room (struct usb_serial_port *port)
1256 {
1257         struct usb_serial *serial = port->serial;
1258         int room = 0;
1259
1260         if (serial->num_bulk_out) {
1261                 if (!port->write_busy && port->write_backlog == 0)
1262                         room = port->bulk_out_size;
1263         }
1264
1265         dbg("%s - port %d, returns %d", __FUNCTION__, port->number, room);
1266         return (room);
1267 }
1268
1269 static int generic_chars_in_buffer (struct usb_serial_port *port)
1270 {
1271         struct usb_serial *serial = port->serial;
1272         int chars = 0;
1273
1274         dbg("%s - port %d", __FUNCTION__, port->number);
1275
1276         if (serial->num_bulk_out) {
1277                 if (port->write_busy)
1278                         chars += port->write_urb->transfer_buffer_length;
1279                 chars += port->write_backlog;   /* spin_lock... Baah */
1280         }
1281
1282         dbg("%s - returns %d", __FUNCTION__, chars);
1283         return (chars);
1284 }
1285
1286 static void generic_read_bulk_callback (struct urb *urb)
1287 {
1288         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1289         struct usb_serial *serial = port->serial;
1290         struct tty_struct *tty;
1291         unsigned char *data = urb->transfer_buffer;
1292         int i;
1293         int result;
1294
1295         if (!serial) {
1296                 err("%s - null serial pointer, exiting", __FUNCTION__);
1297                 return;
1298         }
1299
1300         if (urb->status) {
1301                 dbg("%s - nonzero read bulk status received: %d, pipe 0x%x",
1302                     __FUNCTION__, urb->status, urb->pipe);
1303                 return;
1304         }
1305
1306         dbg("%s - port %d", __FUNCTION__, port->number);
1307
1308         usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
1309
1310         tty = port->tty;
1311         if (tty && urb->actual_length) {
1312                 for (i = 0; i < urb->actual_length ; ++i) {
1313                         /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
1314                         if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
1315                                 tty_flip_buffer_push(tty);
1316                         }
1317                         /* this doesn't actually push the data through unless tty->low_latency is set */
1318                         tty_insert_flip_char(tty, data[i], 0);
1319                 }
1320                 tty_flip_buffer_push(tty);
1321         }
1322
1323         if (serial->dev == NULL)
1324                 return;
1325
1326         /* Continue trying to always read  */
1327         usb_fill_bulk_urb (port->read_urb, serial->dev,
1328                            usb_rcvbulkpipe (serial->dev,
1329                                             port->bulk_in_endpointAddress),
1330                            port->read_urb->transfer_buffer,
1331                            port->read_urb->transfer_buffer_length,
1332                            ((serial->type->read_bulk_callback) ? 
1333                              serial->type->read_bulk_callback : 
1334                              generic_read_bulk_callback), port);
1335         result = usb_submit_urb(port->read_urb);
1336         if (result)
1337                 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
1338 }
1339
1340 static void generic_write_bulk_callback (struct urb *urb)
1341 {
1342         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1343
1344         dbg("%s - port %d", __FUNCTION__, port->number);
1345
1346         port->write_busy = 0;
1347         wmb();
1348
1349         if (urb->status) {
1350                 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
1351         }
1352
1353         queue_task(&port->tqueue, &tq_immediate);
1354         mark_bh(IMMEDIATE_BH);
1355
1356         return;
1357 }
1358
1359 static void generic_shutdown (struct usb_serial *serial)
1360 {
1361         int i;
1362
1363         dbg("%s", __FUNCTION__);
1364
1365         /* stop reads and writes on all ports */
1366         for (i=0; i < serial->num_ports; ++i) {
1367                 generic_cleanup (&serial->port[i]);
1368         }
1369 }
1370
1371 static void port_softint(void *private)
1372 {
1373         struct usb_serial_port *port = (struct usb_serial_port *)private;
1374         struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
1375         struct tty_struct *tty;
1376         unsigned long flags;
1377         struct tty_ldisc *ld;
1378
1379         dbg("%s - port %d", __FUNCTION__, port->number);
1380         
1381         if (!serial)
1382                 return;
1383
1384         spin_lock_irqsave(&post_lock, flags);
1385         if (port->write_backlog != 0)
1386                 schedule_task(&post_task);
1387         spin_unlock_irqrestore(&post_lock, flags);
1388
1389         tty = port->tty;
1390         if (!tty)
1391                 return;
1392
1393         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP))) {
1394                 ld = tty_ldisc_ref(tty);
1395                 if(ld) {
1396                         if(ld->write_wakeup) {
1397                                 ld->write_wakeup(tty);
1398                                 dbg("%s - write wakeup call.", __FUNCTION__);
1399                         }
1400                         tty_ldisc_deref(ld);
1401                 }
1402         }
1403
1404         wake_up_interruptible(&tty->write_wait);
1405 }
1406
1407
1408 static void * usb_serial_probe(struct usb_device *dev, unsigned int ifnum,
1409                                const struct usb_device_id *id)
1410 {
1411         struct usb_serial *serial = NULL;
1412         struct usb_serial_port *port;
1413         struct usb_interface *interface;
1414         struct usb_interface_descriptor *iface_desc;
1415         struct usb_endpoint_descriptor *endpoint;
1416         struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
1417         struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
1418         struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
1419         struct usb_serial_device_type *type = NULL;
1420         struct list_head *tmp;
1421         int found;
1422         int minor;
1423         int buffer_size;
1424         int i;
1425         int num_interrupt_in = 0;
1426         int num_bulk_in = 0;
1427         int num_bulk_out = 0;
1428         int num_ports;
1429         int max_endpoints;
1430         const struct usb_device_id *id_pattern = NULL;
1431         unsigned long flags;
1432
1433         /* loop through our list of known serial converters, and see if this
1434            device matches. */
1435         found = 0;
1436         interface = &dev->actconfig->interface[ifnum];
1437         list_for_each (tmp, &usb_serial_driver_list) {
1438                 type = list_entry(tmp, struct usb_serial_device_type, driver_list);
1439                 id_pattern = usb_match_id(dev, interface, type->id_table);
1440                 if (id_pattern != NULL) {
1441                         dbg("descriptor matches");
1442                         found = 1;
1443                         break;
1444                 }
1445         }
1446         if (!found) {
1447                 /* no match */
1448                 dbg("none matched");
1449                 return(NULL);
1450         }
1451         
1452         /* descriptor matches, let's find the endpoints needed */
1453         /* check out the endpoints */
1454         iface_desc = &interface->altsetting[0];
1455         for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
1456                 endpoint = &iface_desc->endpoint[i];
1457                 
1458                 if ((endpoint->bEndpointAddress & 0x80) &&
1459                     ((endpoint->bmAttributes & 3) == 0x02)) {
1460                         /* we found a bulk in endpoint */
1461                         dbg("found bulk in");
1462                         bulk_in_endpoint[num_bulk_in] = endpoint;
1463                         ++num_bulk_in;
1464                 }
1465
1466                 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
1467                     ((endpoint->bmAttributes & 3) == 0x02)) {
1468                         /* we found a bulk out endpoint */
1469                         dbg("found bulk out");
1470                         bulk_out_endpoint[num_bulk_out] = endpoint;
1471                         ++num_bulk_out;
1472                 }
1473                 
1474                 if ((endpoint->bEndpointAddress & 0x80) &&
1475                     ((endpoint->bmAttributes & 3) == 0x03)) {
1476                         /* we found a interrupt in endpoint */
1477                         dbg("found interrupt in");
1478                         interrupt_in_endpoint[num_interrupt_in] = endpoint;
1479                         ++num_interrupt_in;
1480                 }
1481         }
1482
1483 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
1484         /* BEGIN HORRIBLE HACK FOR PL2303 */ 
1485         /* this is needed due to the looney way its endpoints are set up */
1486         if (((dev->descriptor.idVendor == PL2303_VENDOR_ID) &&
1487              (dev->descriptor.idProduct == PL2303_PRODUCT_ID)) ||
1488             ((dev->descriptor.idVendor == ATEN_VENDOR_ID) &&
1489              (dev->descriptor.idProduct == ATEN_PRODUCT_ID))) {
1490                 if (ifnum == 1) {
1491                         /* check out the endpoints of the other interface*/
1492                         struct usb_interface *other_iface;
1493
1494                         other_iface = &dev->actconfig->interface[ifnum ^ 1];
1495                         iface_desc = &other_iface->altsetting[0];
1496                         for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
1497                                 endpoint = &iface_desc->endpoint[i];
1498                                 if ((endpoint->bEndpointAddress & 0x80) &&
1499                                     ((endpoint->bmAttributes & 3) == 0x03)) {
1500                                         /* we found a interrupt in endpoint */
1501                                         dbg("found interrupt in for Prolific device on separate interface");
1502                                         interrupt_in_endpoint[num_interrupt_in] = endpoint;
1503                                         ++num_interrupt_in;
1504                                 }
1505                         }
1506                 }
1507
1508                 /* Now make sure the PL-2303 is configured correctly.
1509                  * If not, give up now and hope this hack will work
1510                  * properly during a later invocation of usb_serial_probe
1511                  */
1512                 if (num_bulk_in == 0 || num_bulk_out == 0) {
1513                         info("PL-2303 hack: descriptors matched but endpoints did not");
1514                         return NULL;
1515                 }
1516         }
1517         /* END HORRIBLE HACK FOR PL2303 */
1518 #endif
1519
1520         /* found all that we need */
1521         info("%s converter detected", type->name);
1522
1523 #ifdef CONFIG_USB_SERIAL_GENERIC
1524         if (type == &generic_device) {
1525                 num_ports = num_bulk_out;
1526                 if (num_ports == 0) {
1527                         err("Generic device with no bulk out, not allowed.");
1528                         return NULL;
1529                 }
1530         } else
1531 #endif
1532                 num_ports = type->num_ports;
1533
1534         serial = get_free_serial (num_ports, &minor);
1535         if (serial == NULL) {
1536                 err("No more free serial devices");
1537                 return NULL;
1538         }
1539
1540         serial->dev = dev;
1541         serial->type = type;
1542         serial->interface = interface;
1543         serial->minor = minor;
1544         serial->num_ports = num_ports;
1545         serial->num_bulk_in = num_bulk_in;
1546         serial->num_bulk_out = num_bulk_out;
1547         serial->num_interrupt_in = num_interrupt_in;
1548         serial->vendor = dev->descriptor.idVendor;
1549         serial->product = dev->descriptor.idProduct;
1550
1551         /* set up the endpoint information */
1552         for (i = 0; i < num_bulk_in; ++i) {
1553                 endpoint = bulk_in_endpoint[i];
1554                 port = &serial->port[i];
1555                 port->read_urb = usb_alloc_urb (0);
1556                 if (!port->read_urb) {
1557                         err("No free urbs available");
1558                         goto probe_error;
1559                 }
1560                 buffer_size = endpoint->wMaxPacketSize;
1561                 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
1562                 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1563                 if (!port->bulk_in_buffer) {
1564                         err("Couldn't allocate bulk_in_buffer");
1565                         goto probe_error;
1566                 }
1567                 usb_fill_bulk_urb (port->read_urb, dev,
1568                                    usb_rcvbulkpipe (dev,
1569                                                     endpoint->bEndpointAddress),
1570                                    port->bulk_in_buffer, buffer_size,
1571                                    ((serial->type->read_bulk_callback) ? 
1572                                      serial->type->read_bulk_callback : 
1573                                      generic_read_bulk_callback),
1574                                    port);
1575         }
1576
1577         for (i = 0; i < num_bulk_out; ++i) {
1578                 endpoint = bulk_out_endpoint[i];
1579                 port = &serial->port[i];
1580                 port->write_urb = usb_alloc_urb(0);
1581                 if (!port->write_urb) {
1582                         err("No free urbs available");
1583                         goto probe_error;
1584                 }
1585                 buffer_size = endpoint->wMaxPacketSize;
1586                 port->bulk_out_size = buffer_size;
1587                 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
1588                 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
1589                 if (!port->bulk_out_buffer) {
1590                         err("Couldn't allocate bulk_out_buffer");
1591                         goto probe_error;
1592                 }
1593                 usb_fill_bulk_urb (port->write_urb, dev,
1594                                    usb_sndbulkpipe (dev,
1595                                                     endpoint->bEndpointAddress),
1596                                    port->bulk_out_buffer, buffer_size, 
1597                                    ((serial->type->write_bulk_callback) ? 
1598                                      serial->type->write_bulk_callback : 
1599                                      generic_write_bulk_callback),
1600                                    port);
1601         }
1602
1603         for (i = 0; i < num_interrupt_in; ++i) {
1604                 endpoint = interrupt_in_endpoint[i];
1605                 port = &serial->port[i];
1606                 port->interrupt_in_urb = usb_alloc_urb(0);
1607                 if (!port->interrupt_in_urb) {
1608                         err("No free urbs available");
1609                         goto probe_error;
1610                 }
1611                 buffer_size = endpoint->wMaxPacketSize;
1612                 port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
1613                 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1614                 if (!port->interrupt_in_buffer) {
1615                         err("Couldn't allocate interrupt_in_buffer");
1616                         goto probe_error;
1617                 }
1618                 usb_fill_int_urb (port->interrupt_in_urb, dev, 
1619                                   usb_rcvintpipe (dev,
1620                                                   endpoint->bEndpointAddress),
1621                                   port->interrupt_in_buffer, buffer_size, 
1622                                   serial->type->read_int_callback, port, 
1623                                   endpoint->bInterval);
1624         }
1625
1626         /* initialize some parts of the port structures */
1627         /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
1628         max_endpoints = max(num_bulk_in, num_bulk_out);
1629         max_endpoints = max(max_endpoints, num_interrupt_in);
1630         max_endpoints = max(max_endpoints, (int)serial->num_ports);
1631         dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
1632         for (i = 0; i < max_endpoints; ++i) {
1633                 port = &serial->port[i];
1634                 port->number = i + serial->minor;
1635                 port->serial = serial;
1636                 port->magic = USB_SERIAL_PORT_MAGIC;
1637                 port->tqueue.routine = port_softint;
1638                 port->tqueue.data = port;
1639                 init_MUTEX (&port->sem);
1640         }
1641
1642         spin_lock_irqsave(&post_lock, flags);
1643         serial->ref = 1;
1644         spin_unlock_irqrestore(&post_lock, flags);
1645
1646         /* if this device type has a startup function, call it */
1647         if (type->startup) {
1648                 i = type->startup (serial);
1649                 if (i < 0)
1650                         goto startup_error;
1651                 if (i > 0)
1652                         return serial;
1653         }
1654
1655         /* initialize the devfs nodes for this device and let the user know what ports we are bound to */
1656         for (i = 0; i < serial->num_ports; ++i) {
1657                 tty_register_devfs (&serial_tty_driver, 0, serial->port[i].number);
1658                 info("%s converter now attached to ttyUSB%d (or usb/tts/%d for devfs)", 
1659                      type->name, serial->port[i].number, serial->port[i].number);
1660         }
1661
1662         return serial; /* success */
1663
1664
1665 startup_error:
1666         spin_lock_irqsave(&post_lock, flags);
1667         if (serial->ref != 1) {
1668                 err("bug in component startup: ref %d\n", serial->ref);
1669         }
1670         spin_unlock_irqrestore(&post_lock, flags);
1671 probe_error:
1672         for (i = 0; i < num_bulk_in; ++i) {
1673                 port = &serial->port[i];
1674                 if (port->read_urb)
1675                         usb_free_urb (port->read_urb);
1676                 if (port->bulk_in_buffer)
1677                         kfree (port->bulk_in_buffer);
1678         }
1679         for (i = 0; i < num_bulk_out; ++i) {
1680                 port = &serial->port[i];
1681                 if (port->write_urb)
1682                         usb_free_urb (port->write_urb);
1683                 if (port->bulk_out_buffer)
1684                         kfree (port->bulk_out_buffer);
1685         }
1686         for (i = 0; i < num_interrupt_in; ++i) {
1687                 port = &serial->port[i];
1688                 if (port->interrupt_in_urb)
1689                         usb_free_urb (port->interrupt_in_urb);
1690                 if (port->interrupt_in_buffer)
1691                         kfree (port->interrupt_in_buffer);
1692         }
1693
1694         /* return the minor range that this device had */
1695         return_serial (serial);
1696
1697         /* free up any memory that we allocated */
1698         kfree (serial);
1699         return NULL;
1700 }
1701
1702 static void usb_serial_disconnect(struct usb_device *dev, void *ptr)
1703 {
1704         struct usb_serial *serial = (struct usb_serial *) ptr;
1705         struct usb_serial_port *port;
1706         int i;
1707
1708         dbg ("%s", __FUNCTION__);
1709         if (serial) {
1710                 for (i = 0; i < serial->num_ports; ++i) {
1711                         port = &serial->port[i];
1712                         down (&port->sem);
1713                         if (port->tty != NULL)
1714                                 tty_hangup(port->tty);
1715                         up (&port->sem);
1716                 }
1717                 serial_shutdown (serial);
1718
1719                 /* fail all future close/read/write/ioctl/etc calls */
1720                 serial->dev = NULL;
1721                 wmb();
1722
1723                 for (i = 0; i < serial->num_bulk_in; ++i) {
1724                         port = &serial->port[i];
1725                         if (port->read_urb) {
1726                                 usb_unlink_urb (port->read_urb);
1727                                 usb_free_urb (port->read_urb);
1728                         }
1729                         if (port->bulk_in_buffer)
1730                                 kfree (port->bulk_in_buffer);
1731                 }
1732                 for (i = 0; i < serial->num_bulk_out; ++i) {
1733                         port = &serial->port[i];
1734                         if (port->write_urb) {
1735                                 usb_unlink_urb (port->write_urb);
1736                                 usb_free_urb (port->write_urb);
1737                         }
1738                         if (port->bulk_out_buffer)
1739                                 kfree (port->bulk_out_buffer);
1740                 }
1741                 for (i = 0; i < serial->num_interrupt_in; ++i) {
1742                         port = &serial->port[i];
1743                         if (port->interrupt_in_urb) {
1744                                 usb_unlink_urb (port->interrupt_in_urb);
1745                                 usb_free_urb (port->interrupt_in_urb);
1746                         }
1747                         if (port->interrupt_in_buffer)
1748                                 kfree (port->interrupt_in_buffer);
1749                 }
1750
1751                 for (i = 0; i < serial->num_ports; ++i) {
1752                         tty_unregister_devfs (&serial_tty_driver, serial->port[i].number);
1753                         info("%s converter now disconnected from ttyUSB%d", serial->type->name, serial->port[i].number);
1754                 }
1755
1756                 /* return the minor range that this device had */
1757                 return_serial (serial);
1758
1759                 /* free up any memory that we allocated */
1760                 serial_put (serial);
1761
1762         } else {
1763                 info("device disconnected");
1764         }
1765
1766 }
1767
1768
1769 static struct tty_driver serial_tty_driver = {
1770         .magic =                TTY_DRIVER_MAGIC,
1771         .driver_name =          "usb-serial",
1772 #ifndef CONFIG_DEVFS_FS
1773         .name =                 "ttyUSB",
1774 #else
1775         .name =                 "usb/tts/%d",
1776 #endif
1777         .major =                SERIAL_TTY_MAJOR,
1778         .minor_start =          0,
1779         .num =                  SERIAL_TTY_MINORS,
1780         .type =                 TTY_DRIVER_TYPE_SERIAL,
1781         .subtype =              SERIAL_TYPE_NORMAL,
1782         .flags =                TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
1783
1784         .refcount =             &serial_refcount,
1785         .table =                serial_tty,
1786         .termios =              serial_termios,
1787         .termios_locked =       serial_termios_locked,
1788
1789         .open =                 serial_open,
1790         .close =                serial_close,
1791         .write =                serial_write,
1792         .write_room =           serial_write_room,
1793         .ioctl =                serial_ioctl,
1794         .set_termios =          serial_set_termios,
1795         .throttle =             serial_throttle,
1796         .unthrottle =           serial_unthrottle,
1797         .break_ctl =            serial_break,
1798         .chars_in_buffer =      serial_chars_in_buffer,
1799         .read_proc =            serial_read_proc,
1800 };
1801
1802
1803 static int __init usb_serial_init(void)
1804 {
1805         int i;
1806         int result;
1807
1808         /* Initalize our global data */
1809         for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1810                 serial_table[i] = NULL;
1811         }
1812         post_task.routine = post_helper;
1813
1814         /* register the tty driver */
1815         serial_tty_driver.init_termios          = tty_std_termios;
1816         serial_tty_driver.init_termios.c_cflag  = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1817         if (tty_register_driver (&serial_tty_driver)) {
1818                 err("%s - failed to register tty driver", __FUNCTION__);
1819                 return -1;
1820         }
1821
1822         /* register the USB driver */
1823         result = usb_register(&usb_serial_driver);
1824         if (result < 0) {
1825                 tty_unregister_driver(&serial_tty_driver);
1826                 err("usb_register failed for the usb-serial driver. Error number %d", result);
1827                 return -1;
1828         }
1829
1830 #ifdef CONFIG_USB_SERIAL_GENERIC
1831         generic_device_ids[0].idVendor = vendor;
1832         generic_device_ids[0].idProduct = product;
1833         generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
1834         /* register our generic driver with ourselves */
1835         usb_serial_register (&generic_device);
1836 #endif
1837
1838         info(DRIVER_DESC " " DRIVER_VERSION);
1839
1840         return 0;
1841 }
1842
1843
1844 static void __exit usb_serial_exit(void)
1845 {
1846
1847 #ifdef CONFIG_USB_SERIAL_GENERIC
1848         /* remove our generic driver */
1849         usb_serial_deregister (&generic_device);
1850 #endif
1851         
1852         usb_deregister(&usb_serial_driver);
1853         tty_unregister_driver(&serial_tty_driver);
1854
1855         while (!list_empty(&usb_serial_driver_list)) {
1856                 err("%s - module is in use, hanging...", __FUNCTION__);
1857                 msleep(5000);
1858         }
1859 }
1860
1861
1862 module_init(usb_serial_init);
1863 module_exit(usb_serial_exit);
1864
1865
1866 int usb_serial_register(struct usb_serial_device_type *new_device)
1867 {
1868         /* Add this device to our list of devices */
1869         list_add(&new_device->driver_list, &usb_serial_driver_list);
1870
1871         info ("USB Serial support registered for %s", new_device->name);
1872
1873         usb_scan_devices();
1874
1875         return 0;
1876 }
1877
1878
1879 void usb_serial_deregister(struct usb_serial_device_type *device)
1880 {
1881         struct usb_serial *serial;
1882         int i;
1883
1884         info("USB Serial deregistering driver %s", device->name);
1885
1886         /* clear out the serial_table if the device is attached to a port */
1887         for(i = 0; i < SERIAL_TTY_MINORS; ++i) {
1888                 serial = serial_table[i];
1889                 if ((serial != NULL) && (serial->type == device)) {
1890                         usb_driver_release_interface (&usb_serial_driver, serial->interface);
1891                         usb_serial_disconnect (NULL, serial);
1892                 }
1893         }
1894
1895         list_del(&device->driver_list);
1896 }
1897
1898
1899
1900 /* If the usb-serial core is built into the core, the usb-serial drivers
1901    need these symbols to load properly as modules. */
1902 EXPORT_SYMBOL(usb_serial_register);
1903 EXPORT_SYMBOL(usb_serial_deregister);
1904 #ifdef USES_EZUSB_FUNCTIONS
1905         EXPORT_SYMBOL(ezusb_writememory);
1906         EXPORT_SYMBOL(ezusb_set_reset);
1907 #endif
1908 EXPORT_SYMBOL(usb_serial_get_serial);
1909
1910 /* Module information */
1911 MODULE_AUTHOR( DRIVER_AUTHOR );
1912 MODULE_DESCRIPTION( DRIVER_DESC );
1913 MODULE_LICENSE("GPL");
1914
1915 MODULE_PARM(debug, "i");
1916 MODULE_PARM_DESC(debug, "Debug enabled or not");
1917
1918 #ifdef CONFIG_USB_SERIAL_GENERIC
1919 MODULE_PARM(vendor, "h");
1920 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
1921
1922 MODULE_PARM(product, "h");
1923 MODULE_PARM_DESC(product, "User specified USB idProduct");
1924 #endif