linux-old/drivers/usb/serial/usbserial.c History
<<
>>
Prefs
   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. */
 321static int  generic_open                (struct usb_serial_port *port, struct file *filp);
 322static void generic_close               (struct usb_serial_port *port, struct file *filp);
 323static int  generic_write               (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
 324static int  generic_write_room          (struct usb_serial_port *port);
 325static int  generic_chars_in_buffer     (struct usb_serial_port *port);
 326static void generic_read_bulk_callback  (struct urb *urb);
 327static void generic_write_bulk_callback (struct urb *urb);
 328static void generic_shutdown            (struct usb_serial *serial);
 329
 330
 331#ifdef CONFIG_USB_SERIAL_GENERIC
 332static __u16    vendor  = 0x05f9;
 333static __u16    product = 0xffff;
 334
 335static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
 336
 337/* All of the device info needed for the Generic Serial Converter */
 338static 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 */
 354struct usb_serial_post_job {
 355        struct list_head link;
 356        struct usb_serial_port *port;
 357        int len;
 358        char buff[POST_BSIZE];
 359};
 360static spinlock_t post_lock = SPIN_LOCK_UNLOCKED;       /* Also covers ->ref */
 361static struct list_head post_list = LIST_HEAD_INIT(post_list);
 362static struct tq_struct post_task;
 363
 364/* local function prototypes */
 365static int  serial_open (struct tty_struct *tty, struct file * filp);
 366static void serial_close (struct tty_struct *tty, struct file * filp);
 367static int __serial_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
 368static int  serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count);
 369static int  serial_post_job(struct usb_serial_port *port, int from_user,
 370    int gfp, const unsigned char *buf, int count);
 371static int  serial_post_one(struct usb_serial_port *port, int from_user,
 372    int gfp, const unsigned char *buf, int count);
 373static int  serial_write_room (struct tty_struct *tty);
 374static int  serial_chars_in_buffer (struct tty_struct *tty);
 375static void serial_throttle (struct tty_struct * tty);
 376static void serial_unthrottle (struct tty_struct * tty);
 377static int  serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg);
 378static void serial_set_termios (struct tty_struct *tty, struct termios * old);
 379static void serial_shutdown (struct usb_serial *serial);
 380
 381static void * usb_serial_probe(struct usb_device *dev, unsigned int ifnum,
 382                               const struct usb_device_id *id);
 383static void usb_serial_disconnect(struct usb_device *dev, void *ptr);
 384
 385static 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
 400static int                      serial_refcount;
 401static struct tty_driver        serial_tty_driver;
 402static struct tty_struct *      serial_tty[SERIAL_TTY_MINORS];
 403static struct termios *         serial_termios[SERIAL_TTY_MINORS];
 404static struct termios *         serial_termios_locked[SERIAL_TTY_MINORS];
 405static struct usb_serial        *serial_table[SERIAL_TTY_MINORS];       /* initially all NULL */
 406
 407
 408static LIST_HEAD(usb_serial_driver_list);
 409
 410
 411static struct usb_serial *get_serial_by_minor (unsigned int minor)
 412{
 413        return serial_table[minor];
 414}
 415
 416
 417static struct usb_serial *get_free_serial (int num_ports, int *minor)
 418{
 419        struct usb_serial *serial = NULL;
 420        int i, j;
 421        int good_spot;
 422
 423        dbg("%s %d", __FUNCTION__, num_ports);
 424
 425        *minor = 0;
 426        for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
 427                if (serial_table[i])
 428                        continue;
 429
 430                good_spot = 1;
 431                for (j = 1; j <= num_ports-1; ++j)
 432                        if (serial_table[i+j])
 433                                good_spot = 0;
 434                if (good_spot == 0)
 435                        continue;
 436                        
 437                if (!(serial = kmalloc(sizeof(struct usb_serial), GFP_KERNEL))) {
 438                        err("%s - Out of memory", __FUNCTION__);
 439                        return NULL;
 440                }
 441                memset(serial, 0, sizeof(struct usb_serial));
 442                serial->magic = USB_SERIAL_MAGIC;
 443                serial_table[i] = serial;
 444                *minor = i;
 445                dbg("%s - minor base = %d", __FUNCTION__, *minor);
 446                for (i = *minor+1; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
 447                        serial_table[i] = serial;
 448                return serial;
 449        }
 450        return NULL;
 451}
 452
 453static void return_serial (struct usb_serial *serial)
 454{
 455        int i;
 456
 457        dbg("%s", __FUNCTION__);
 458
 459        if (serial == NULL)
 460                return;
 461
 462        for (i = 0; i < serial->num_ports; ++i) {
 463                serial_table[serial->minor + i] = NULL;
 464        }
 465
 466        return;
 467}
 468
 469/*
 470 * The post kludge.
 471 *
 472 * Our component drivers are hideously buggy and written by people
 473 * who have difficulty understanding the concept of spinlocks.
 474 * There were so many races and lockups that Greg K-H made a watershed
 475 * decision to provide what is essentially a single-threaded sandbox
 476 * for component drivers, protected by a semaphore. It helped a lot, but
 477 * for one little problem: when tty->low_latency is set, line disciplines
 478 * can call ->write from an interrupt, where the semaphore oopses.
 479 *
 480 * Rather than open the whole can of worms again, we just post writes
 481 * into a helper which can sleep.
 482 *
 483 * Kernel 2.6 has a proper fix. It replaces semaphores with proper locking.
 484 */
 485static void post_helper(void *arg)
 486{
 487        struct list_head *pos;
 488        struct usb_serial_post_job *job;
 489        struct usb_serial_port *port;
 490        struct usb_serial *serial;
 491        unsigned int flags;
 492
 493        spin_lock_irqsave(&post_lock, flags);
 494        pos = post_list.next;
 495        while (pos != &post_list) {
 496                job = list_entry(pos, struct usb_serial_post_job, link);
 497                port = job->port;
 498                /* get_usb_serial checks port->tty, so cannot be used */
 499                serial = port->serial;
 500                if (port->write_busy) {
 501                        dbg("%s - port %d busy", __FUNCTION__, port->number);
 502                        pos = pos->next;
 503                        continue;
 504                }
 505                list_del(&job->link);
 506                spin_unlock_irqrestore(&post_lock, flags);
 507
 508                down(&port->sem);
 509                dbg("%s - port %d len %d backlog %d", __FUNCTION__,
 510                    port->number, job->len, port->write_backlog);
 511                if (port->tty != NULL) {
 512                        int rc;
 513                        int sent = 0;
 514                        while (sent < job->len) {
 515                                rc = __serial_write(port, 0, job->buff + sent, job->len - sent);
 516                                if ((rc < 0) || signal_pending(current))
 517                                        break;
 518                                sent += rc;
 519                                if ((sent < job->len) && current->need_resched)
 520                                        schedule();
 521                        }
 522                }
 523                up(&port->sem);
 524
 525                spin_lock_irqsave(&post_lock, flags);
 526                port->write_backlog -= job->len;
 527                kfree(job);
 528                if (--serial->ref == 0)
 529                        kfree(serial);
 530                /* Have to reset because we dropped spinlock */
 531                pos = post_list.next;
 532        }
 533        spin_unlock_irqrestore(&post_lock, flags);
 534}
 535
 536#ifdef USES_EZUSB_FUNCTIONS
 537/* EZ-USB Control and Status Register.  Bit 0 controls 8051 reset */
 538#define CPUCS_REG    0x7F92
 539
 540int ezusb_writememory (struct usb_serial *serial, int address, unsigned char *data, int length, __u8 bRequest)
 541{
 542        int result;
 543        unsigned char *transfer_buffer;
 544
 545        /* dbg("ezusb_writememory %x, %d", address, length); */
 546        if (!serial->dev) {
 547                dbg("%s - no physical device present, failing.", __FUNCTION__);
 548                return -ENODEV;
 549        }
 550
 551        transfer_buffer =  kmalloc (length, GFP_KERNEL);
 552        if (!transfer_buffer) {
 553                err("%s - kmalloc(%d) failed.", __FUNCTION__, length);
 554                return -ENOMEM;
 555        }
 556        memcpy (transfer_buffer, data, length);
 557        result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), bRequest, 0x40, address, 0, transfer_buffer, length, 3*HZ);
 558        kfree (transfer_buffer);
 559        return result;
 560}
 561
 562int ezusb_set_reset (struct usb_serial *serial, unsigned char reset_bit)
 563{
 564        int     response;
 565        dbg("%s - %d", __FUNCTION__, reset_bit);
 566        response = ezusb_writememory (serial, CPUCS_REG, &reset_bit, 1, 0xa0);
 567        if (response < 0) {
 568                err("%s- %d failed", __FUNCTION__, reset_bit);
 569        }
 570        return response;
 571}
 572
 573#endif  /* USES_EZUSB_FUNCTIONS */
 574
 575/*****************************************************************************
 576 * Driver tty interface functions
 577 *****************************************************************************/
 578static int serial_open (struct tty_struct *tty, struct file * filp)
 579{
 580        struct usb_serial *serial;
 581        struct usb_serial_port *port;
 582        unsigned int portNumber;
 583        int retval = 0;
 584        
 585        dbg("%s", __FUNCTION__);
 586
 587        /* initialize the pointer incase something fails */
 588        tty->driver_data = NULL;
 589
 590        /* get the serial object associated with this tty pointer */
 591        serial = get_serial_by_minor (MINOR(tty->device));
 592
 593        if (serial_paranoia_check (serial, __FUNCTION__))
 594                return -ENODEV;
 595
 596        /* set up our port structure making the tty driver remember our port object, and us it */
 597        portNumber = MINOR(tty->device) - serial->minor;
 598        port = &serial->port[portNumber];
 599        tty->driver_data = port;
 600
 601        down (&port->sem);
 602        port->tty = tty;
 603         
 604        /* lock this module before we call it */
 605        if (serial->type->owner)
 606                __MOD_INC_USE_COUNT(serial->type->owner);
 607
 608        ++port->open_count;
 609        if (port->open_count == 1) {
 610                /* only call the device specific open if this 
 611                 * is the first time the port is opened */
 612                if (serial->type->open)
 613                        retval = serial->type->open(port, filp);
 614                else
 615                        retval = generic_open(port, filp);
 616        }
 617
 618        if (retval) {
 619                port->open_count = 0;
 620                if (serial->type->owner)
 621                        __MOD_DEC_USE_COUNT(serial->type->owner);
 622        }
 623
 624        up (&port->sem);
 625        return retval;
 626}
 627
 628static void __serial_close(struct usb_serial_port *port, struct file *filp)
 629{
 630        if (!port->open_count) {
 631                dbg ("%s - port not opened", __FUNCTION__);
 632                return;
 633        }
 634
 635        --port->open_count;
 636        if (port->open_count <= 0) {
 637                /* only call the device specific close if this 
 638                 * port is being closed by the last owner */
 639                if (port->serial->type->close)
 640                        port->serial->type->close(port, filp);
 641                else
 642                        generic_close(port, filp);
 643                port->open_count = 0;
 644                if (port->tty) {
 645                        port->tty->driver_data = NULL;
 646                        port->tty = NULL;
 647                }
 648        }
 649
 650        if (port->serial->type->owner)
 651                __MOD_DEC_USE_COUNT(port->serial->type->owner);
 652}
 653
 654static void serial_close(struct tty_struct *tty, struct file * filp)
 655{
 656        struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
 657        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 658
 659        if (!serial)
 660                return;
 661
 662        down (&port->sem);
 663
 664        dbg("%s - port %d", __FUNCTION__, port->number);
 665
 666        /* if disconnect beat us to the punch here, there's nothing to do */
 667        if (tty->driver_data) {
 668                /*
 669                 * XXX The right thing would be to wait for the output to drain.
 670                 * But we are not sufficiently daring to experiment in 2.4.
 671                 * N.B. If we do wait, no need to run post_helper here.
 672                 * Normall callback mechanism wakes it up just fine.
 673                 */
 674#if I_AM_A_DARING_HACKER
 675                tty->closing = 1;
 676                up (&port->sem);
 677                if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
 678                        tty_wait_until_sent(tty, info->closing_wait);
 679                down (&port->sem);
 680                if (!tty->driver_data) /* woopsie, disconnect, now what */ ;
 681#endif
 682                __serial_close(port, filp);
 683        }
 684
 685        up (&port->sem);
 686}
 687
 688static int __serial_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
 689{
 690        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 691        int retval = -EINVAL;
 692
 693        if (!serial)
 694                return -ENODEV;
 695
 696        dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
 697
 698        if (!port->open_count) {
 699                dbg("%s - port not opened", __FUNCTION__);
 700                goto exit;
 701        }
 702
 703        /* pass on to the driver specific version of this function if it is available */
 704        if (serial->type->write)
 705                retval = serial->type->write(port, from_user, buf, count);
 706        else
 707                retval = generic_write(port, from_user, buf, count);
 708
 709exit:
 710        return retval;
 711}
 712
 713static int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
 714{
 715        struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
 716        int rc;
 717
 718        if (!port)
 719                return -ENODEV;
 720
 721        if (!in_interrupt()) {
 722                /*
 723                 * Run post_list to reduce a possiblity of reordered writes.
 724                 * Tasks can make keventd to sleep, sometimes for a long time.
 725                 */
 726                post_helper(NULL);
 727
 728                down(&port->sem);
 729                /*
 730                 * This happens when a line discipline asks how much room
 731                 * we have, gets 64, then tries to perform two writes
 732                 * for a byte each. First write takes whole URB, second
 733                 * write hits this check.
 734                 */
 735                if (port->write_busy) {
 736                        up(&port->sem);
 737                        return serial_post_job(port, from_user, GFP_KERNEL,
 738                            buf, count);
 739                }
 740
 741                rc = __serial_write(port, from_user, buf, count);
 742                up(&port->sem);
 743                return rc;
 744        }
 745
 746        if (from_user) {
 747                /*
 748                 * This is a BUG-able offense because we cannot
 749                 * pagefault while in_interrupt, but we want to see
 750                 * something in dmesg rather than just blinking LEDs.
 751                 */
 752                err("user data in interrupt write");
 753                return -EINVAL;
 754        }
 755
 756        return serial_post_job(port, 0, GFP_ATOMIC, buf, count);
 757}
 758
 759static int serial_post_job(struct usb_serial_port *port, int from_user,
 760    int gfp, const unsigned char *buf, int count)
 761{
 762        int done = 0, length;
 763        int rc;
 764
 765        if (port == NULL)
 766                return -EPIPE;
 767
 768        if (count >= 512) {
 769                static int rate = 0;
 770                /*
 771                 * Data loss due to extreme circumstances.
 772                 * It's a ususal thing on serial to lose characters, isn't it?
 773                 * Neener, neener! Actually, it's probably an echo loop anyway.
 774                 * Only happens when getty starts talking to Visor.
 775                 */
 776                if (++rate % 1000 < 3) {
 777                        err("too much data (%d) from %s", count,
 778                            from_user? "user": "kernel");
 779                }
 780                count = 512;
 781        }
 782
 783        while (done < count) {
 784                length = count - done;
 785                if (length > POST_BSIZE)
 786                        length = POST_BSIZE;
 787                if (length > port->bulk_out_size)
 788                        length = port->bulk_out_size;
 789
 790                rc = serial_post_one(port, from_user, gfp, buf + done, length);
 791                if (rc <= 0) {
 792                        if (done != 0)
 793                                return done;
 794                        return rc;
 795                }
 796                done += rc;
 797        }
 798
 799        return done;
 800}
 801
 802static int serial_post_one(struct usb_serial_port *port, int from_user,
 803    int gfp, const unsigned char *buf, int count)
 804{
 805        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 806        struct usb_serial_post_job *job;
 807        unsigned long flags;
 808
 809        dbg("%s - port %d user %d count %d", __FUNCTION__, port->number, from_user, count);
 810
 811        job = kmalloc(sizeof(struct usb_serial_post_job), gfp);
 812        if (job == NULL)
 813                return -ENOMEM;
 814
 815        job->port = port;
 816        if (count >= POST_BSIZE)
 817                count = POST_BSIZE;
 818        job->len = count;
 819
 820        if (from_user) {
 821                if (copy_from_user(job->buff, buf, count)) {
 822                        kfree(job);
 823                        return -EFAULT;
 824                }
 825        } else {
 826                memcpy(job->buff, buf, count);
 827        }
 828
 829        spin_lock_irqsave(&post_lock, flags);
 830        port->write_backlog += count;
 831        list_add_tail(&job->link, &post_list);
 832        serial->ref++;          /* Protect the port->sem from kfree() */
 833        schedule_task(&post_task);
 834        spin_unlock_irqrestore(&post_lock, flags);
 835
 836        return count;
 837}
 838
 839static int serial_write_room (struct tty_struct *tty) 
 840{
 841        struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
 842        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 843        int retval = -EINVAL;
 844
 845        if (!serial)
 846                return -ENODEV;
 847
 848        if (in_interrupt()) {
 849                retval = 0;
 850                if (!port->write_busy && port->write_backlog == 0)
 851                        retval = port->bulk_out_size;
 852                dbg("%s - returns %d", __FUNCTION__, retval);
 853                return retval;
 854        }
 855
 856        down (&port->sem);
 857
 858        dbg("%s - port %d", __FUNCTION__, port->number);
 859
 860        if (!port->open_count) {
 861                dbg("%s - port not open", __FUNCTION__);
 862                goto exit;
 863        }
 864
 865        /* pass on to the driver specific version of this function if it is available */
 866        if (serial->type->write_room)
 867                retval = serial->type->write_room(port);
 868        else
 869                retval = generic_write_room(port);
 870
 871exit:
 872        up (&port->sem);
 873        return retval;
 874}
 875
 876static int serial_chars_in_buffer (struct tty_struct *tty) 
 877{
 878        struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
 879        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 880        int retval = -EINVAL;
 881
 882        if (!serial)
 883                return -ENODEV;
 884
 885        down (&port->sem);
 886
 887        if (!port->open_count) {
 888                dbg("%s - port %d: not open", __FUNCTION__, port->number);
 889                goto exit;
 890        }
 891
 892        /* pass on to the driver specific version of this function if it is available */
 893        if (serial->type->chars_in_buffer)
 894                retval = serial->type->chars_in_buffer(port);
 895        else
 896                retval = generic_chars_in_buffer(port);
 897
 898exit:
 899        up (&port->sem);
 900        return retval;
 901}
 902
 903static void serial_throttle (struct tty_struct * tty)
 904{
 905        struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
 906        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 907
 908        if (!serial)
 909                return;
 910
 911        down (&port->sem);
 912
 913        dbg("%s - port %d", __FUNCTION__, port->number);
 914
 915        if (!port->open_count) {
 916                dbg ("%s - port not open", __FUNCTION__);
 917                goto exit;
 918        }
 919
 920        /* pass on to the driver specific version of this function */
 921        if (serial->type->throttle)
 922                serial->type->throttle(port);
 923
 924exit:
 925        up (&port->sem);
 926}
 927
 928static void serial_unthrottle (struct tty_struct * tty)
 929{
 930        struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
 931        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 932
 933        if (!serial)
 934                return;
 935
 936        down (&port->sem);
 937
 938        dbg("%s - port %d", __FUNCTION__, port->number);
 939
 940        if (!port->open_count) {
 941                dbg("%s - port not open", __FUNCTION__);
 942                goto exit;
 943        }
 944
 945        /* pass on to the driver specific version of this function */
 946        if (serial->type->unthrottle)
 947                serial->type->unthrottle(port);
 948
 949exit:
 950        up (&port->sem);
 951}
 952
 953static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
 954{
 955        struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
 956        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 957        int retval = -ENODEV;
 958
 959        if (!serial)
 960                return -ENODEV;
 961
 962        down (&port->sem);
 963
 964        dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
 965
 966        if (!port->open_count) {
 967                dbg ("%s - port not open", __FUNCTION__);
 968                goto exit;
 969        }
 970
 971        /* pass on to the driver specific version of this function if it is available */
 972        if (serial->type->ioctl)
 973                retval = serial->type->ioctl(port, file, cmd, arg);
 974        else
 975                retval = -ENOIOCTLCMD;
 976
 977exit:
 978        up (&port->sem);
 979        return retval;
 980}
 981
 982static void serial_set_termios (struct tty_struct *tty, struct termios * old)
 983{
 984        struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
 985        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 986
 987        if (!serial)
 988                return;
 989
 990        down (&port->sem);
 991
 992        dbg("%s - port %d", __FUNCTION__, port->number);
 993
 994        if (!port->open_count) {
 995                dbg("%s - port not open", __FUNCTION__);
 996                goto exit;
 997        }
 998
 999        /* pass on to the driver specific version of this function if it is available */
1000        if (serial->type->set_termios)
1001                serial->type->set_termios(port, old);
1002
1003exit:
1004        up (&port->sem);
1005}
1006
1007static void serial_break (struct tty_struct *tty, int break_state)
1008{
1009        struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
1010        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
1011
1012        if (!serial)
1013                return;
1014
1015        down (&port->sem);
1016
1017        dbg("%s - port %d", __FUNCTION__, port->number);
1018
1019        if (!port->open_count) {
1020                dbg("%s - port not open", __FUNCTION__);
1021                goto exit;
1022        }
1023
1024        /* pass on to the driver specific version of this function if it is available */
1025        if (serial->type->break_ctl)
1026                serial->type->break_ctl(port, break_state);
1027
1028exit:
1029        up (&port->sem);
1030}
1031
1032static void serial_shutdown (struct usb_serial *serial)
1033{
1034        dbg ("%s", __FUNCTION__);
1035
1036        if (serial->type->shutdown)
1037                serial->type->shutdown(serial);
1038        else
1039                generic_shutdown(serial);
1040}
1041
1042static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
1043{
1044        struct usb_serial *serial;
1045        int length = 0;
1046        int i;
1047        off_t begin = 0;
1048        char tmp[40];
1049
1050        dbg("%s", __FUNCTION__);
1051        length += sprintf (page, "usbserinfo:1.0 driver:%s\n", DRIVER_VERSION);
1052        for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
1053                serial = get_serial_by_minor(i);
1054                if (serial == NULL)
1055                        continue;
1056
1057                length += sprintf (page+length, "%d:", i);
1058                if (serial->type->owner)
1059                        length += sprintf (page+length, " module:%s", serial->type->owner->name);
1060                length += sprintf (page+length, " name:\"%s\"", serial->type->name);
1061                length += sprintf (page+length, " vendor:%04x product:%04x", serial->vendor, serial->product);
1062                length += sprintf (page+length, " num_ports:%d", serial->num_ports);
1063                length += sprintf (page+length, " port:%d", i - serial->minor + 1);
1064
1065                usb_make_path(serial->dev, tmp, sizeof(tmp));
1066                length += sprintf (page+length, " path:%s", tmp);
1067                        
1068                length += sprintf (page+length, "\n");
1069                if ((length + begin) > (off + count))
1070                        goto done;
1071                if ((length + begin) < off) {
1072                        begin += length;
1073                        length = 0;
1074                }
1075        }
1076        *eof = 1;
1077done:
1078        if (off >= (length + begin))
1079                return 0;
1080        *start = page + (off-begin);
1081        return ((count < begin+length-off) ? count : begin+length-off);
1082}
1083
1084/*****************************************************************************
1085 * generic devices specific driver functions
1086 *****************************************************************************/
1087static int generic_open (struct usb_serial_port *port, struct file *filp)
1088{
1089        struct usb_serial *serial = port->serial;
1090        int result = 0;
1091
1092        if (port_paranoia_check (port, __FUNCTION__))
1093                return -ENODEV;
1094
1095        dbg("%s - port %d", __FUNCTION__, port->number);
1096
1097        /* force low_latency on so that our tty_push actually forces the data through, 
1098           otherwise it is scheduled, and with high data rates (like with OHCI) data
1099           can get lost. */
1100        if (port->tty)
1101                port->tty->low_latency = 1;
1102
1103        /* if we have a bulk interrupt, start reading from it */
1104        if (serial->num_bulk_in) {
1105                /* Start reading from the device */
1106                usb_fill_bulk_urb (port->read_urb, serial->dev,
1107                                   usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
1108                                   port->read_urb->transfer_buffer,
1109                                   port->read_urb->transfer_buffer_length,
1110                                   ((serial->type->read_bulk_callback) ?
1111                                     serial->type->read_bulk_callback :
1112                                     generic_read_bulk_callback),
1113                                   port);
1114                result = usb_submit_urb(port->read_urb);
1115                if (result)
1116                        err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
1117        }
1118
1119        return result;
1120}
1121
1122static void generic_cleanup (struct usb_serial_port *port)
1123{
1124        struct usb_serial *serial = port->serial;
1125
1126        dbg("%s - port %d", __FUNCTION__, port->number);
1127
1128        if (serial->dev) {
1129                /* shutdown any bulk reads that might be going on */
1130                if (serial->num_bulk_out)
1131                        usb_unlink_urb (port->write_urb);
1132                if (serial->num_bulk_in)
1133                        usb_unlink_urb (port->read_urb);
1134        }
1135}
1136
1137static void generic_close (struct usb_serial_port *port, struct file * filp)
1138{
1139        dbg("%s - port %d", __FUNCTION__, port->number);
1140        generic_cleanup (port);
1141}
1142
1143static int generic_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
1144{
1145        struct usb_serial *serial = port->serial;
1146        int result;
1147        unsigned long flags;
1148
1149        if (count == 0) {
1150                dbg("%s - write request of 0 bytes", __FUNCTION__);
1151                return (0);
1152        }
1153        if (count < 0) {
1154                err("%s - port %d: write request of %d bytes", __FUNCTION__,
1155                    port->number, count);
1156                return (0);
1157        }
1158
1159        /* only do something if we have a bulk out endpoint */
1160        if (serial->num_bulk_out) {
1161                if (port->write_busy) {
1162                        /* Happens when two threads run port_helper. Watch. */
1163                        info("%s - already writing", __FUNCTION__);
1164                        return (0);
1165                }
1166
1167                count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
1168
1169                if (from_user) {
1170                        if (copy_from_user(port->write_urb->transfer_buffer, buf, count))
1171                                return -EFAULT;
1172                } else {
1173                        memcpy (port->write_urb->transfer_buffer, buf, count);
1174                }
1175                dbg("%s - port %d [%d]", __FUNCTION__, port->number, count);
1176
1177                /* set up our urb */
1178                usb_fill_bulk_urb (port->write_urb, serial->dev,
1179                                   usb_sndbulkpipe (serial->dev,
1180                                                    port->bulk_out_endpointAddress),
1181                                   port->write_urb->transfer_buffer, count,
1182                                   ((serial->type->write_bulk_callback) ? 
1183                                     serial->type->write_bulk_callback :
1184                                     generic_write_bulk_callback), port);
1185
1186                /* send the data out the bulk port */
1187                port->write_busy = 1;
1188                result = usb_submit_urb(port->write_urb);
1189                if (result) {
1190                        err("%s - port %d: failed submitting write urb (%d)",
1191                             __FUNCTION__, port->number, result);
1192                        port->write_busy = 0;
1193                        spin_lock_irqsave(&post_lock, flags);
1194                        if (port->write_backlog != 0)
1195                                schedule_task(&post_task);
1196                        spin_unlock_irqrestore(&post_lock, flags);
1197
1198                } else
1199                        result = count;
1200
1201                return result;
1202        }
1203
1204        /* no bulk out, so return 0 bytes written */
1205        return (0);
1206}
1207
1208static int generic_write_room (struct usb_serial_port *port)
1209{
1210        struct usb_serial *serial = port->serial;
1211        int room = 0;
1212
1213        if (serial->num_bulk_out) {
1214                if (!port->write_busy && port->write_backlog == 0)
1215                        room = port->bulk_out_size;
1216        }
1217
1218        dbg("%s - port %d, returns %d", __FUNCTION__, port->number, room);
1219        return (room);
1220}
1221
1222static int generic_chars_in_buffer (struct usb_serial_port *port)
1223{
1224        struct usb_serial *serial = port->serial;
1225        int chars = 0;
1226
1227        dbg("%s - port %d", __FUNCTION__, port->number);
1228
1229        if (serial->num_bulk_out) {
1230                if (port->write_busy)
1231                        chars += port->write_urb->transfer_buffer_length;
1232                chars += port->write_backlog;   /* spin_lock... Baah */
1233        }
1234
1235        dbg("%s - returns %d", __FUNCTION__, chars);
1236        return (chars);
1237}
1238
1239static void generic_read_bulk_callback (struct urb *urb)
1240{
1241        struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1242        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
1243        struct tty_struct *tty;
1244        unsigned char *data = urb->transfer_buffer;
1245        int i;
1246        int result;
1247
1248        dbg("%s - port %d", __FUNCTION__, port->number);
1249
1250        if (!serial) {
1251                dbg("%s - bad serial pointer, exiting", __FUNCTION__);
1252                return;
1253        }
1254
1255        if (urb->status) {
1256                dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
1257                return;
1258        }
1259
1260        usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
1261
1262        tty = port->tty;
1263        if (tty && urb->actual_length) {
1264                for (i = 0; i < urb->actual_length ; ++i) {
1265                        /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
1266                        if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
1267                                tty_flip_buffer_push(tty);
1268                        }
1269                        /* this doesn't actually push the data through unless tty->low_latency is set */
1270                        tty_insert_flip_char(tty, data[i], 0);
1271                }
1272                tty_flip_buffer_push(tty);
1273        }
1274
1275        /* Continue trying to always read  */
1276        usb_fill_bulk_urb (port->read_urb, serial->dev,
1277                           usb_rcvbulkpipe (serial->dev,
1278                                            port->bulk_in_endpointAddress),
1279                           port->read_urb->transfer_buffer,
1280                           port->read_urb->transfer_buffer_length,
1281                           ((serial->type->read_bulk_callback) ? 
1282                             serial->type->read_bulk_callback : 
1283                             generic_read_bulk_callback), port);
1284        result = usb_submit_urb(port->read_urb);
1285        if (result)
1286                err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
1287}
1288
1289static void generic_write_bulk_callback (struct urb *urb)
1290{
1291        struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1292        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
1293
1294        dbg("%s - port %d", __FUNCTION__, port->number);
1295
1296        port->write_busy = 0;
1297        wmb();
1298
1299        if (!serial) {
1300                err("%s - null serial pointer, exiting", __FUNCTION__);
1301                return;
1302        }
1303
1304        if (urb->status) {
1305                dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
1306        }
1307
1308        queue_task(&port->tqueue, &tq_immediate);
1309        mark_bh(IMMEDIATE_BH);
1310
1311        return;
1312}
1313
1314static void generic_shutdown (struct usb_serial *serial)
1315{
1316        int i;
1317
1318        dbg("%s", __FUNCTION__);
1319
1320        /* stop reads and writes on all ports */
1321        for (i=0; i < serial->num_ports; ++i) {
1322                generic_cleanup (&serial->port[i]);
1323        }
1324}
1325
1326static void port_softint(void *private)
1327{
1328        struct usb_serial_port *port = (struct usb_serial_port *)private;
1329        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
1330        struct tty_struct *tty;
1331        unsigned long flags;
1332        struct tty_ldisc *ld;
1333
1334        dbg("%s - port %d", __FUNCTION__, port->number);
1335        
1336        if (!serial)
1337                return;
1338
1339        spin_lock_irqsave(&post_lock, flags);
1340        if (port->write_backlog != 0)
1341                schedule_task(&post_task);
1342        spin_unlock_irqrestore(&post_lock, flags);
1343
1344        tty = port->tty;
1345        if (!tty)
1346                return;
1347
1348        if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP))) {
1349                ld = tty_ldisc_ref(tty);
1350                if(ld) {
1351                        if(ld->write_wakeup) {
1352                                ld->write_wakeup(tty);
1353                                dbg("%s - write wakeup call.", __FUNCTION__);
1354                        }
1355                        tty_ldisc_deref(ld);
1356                }
1357        }
1358
1359        wake_up_interruptible(&tty->write_wait);
1360}
1361
1362
1363static void * usb_serial_probe(struct usb_device *dev, unsigned int ifnum,
1364                               const struct usb_device_id *id)
1365{
1366        struct usb_serial *serial = NULL;
1367        struct usb_serial_port *port;
1368        struct usb_interface *interface;
1369        struct usb_interface_descriptor *iface_desc;
1370        struct usb_endpoint_descriptor *endpoint;
1371        struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
1372        struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
1373        struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
1374        struct usb_serial_device_type *type = NULL;
1375        struct list_head *tmp;
1376        int found;
1377        int minor;
1378        int buffer_size;
1379        int i;
1380        int num_interrupt_in = 0;
1381        int num_bulk_in = 0;
1382        int num_bulk_out = 0;
1383        int num_ports;
1384        int max_endpoints;
1385        const struct usb_device_id *id_pattern = NULL;
1386        unsigned long flags;
1387
1388        /* loop through our list of known serial converters, and see if this
1389           device matches. */
1390        found = 0;
1391        interface = &dev->actconfig->interface[ifnum];
1392        list_for_each (tmp, &usb_serial_driver_list) {
1393                type = list_entry(tmp, struct usb_serial_device_type, driver_list);
1394                id_pattern = usb_match_id(dev, interface, type->id_table);
1395                if (id_pattern != NULL) {
1396                        dbg("descriptor matches");
1397                        found = 1;
1398                        break;
1399                }
1400        }
1401        if (!found) {
1402                /* no match */
1403                dbg("none matched");
1404                return(NULL);
1405        }
1406        
1407        /* descriptor matches, let's find the endpoints needed */
1408        /* check out the endpoints */
1409        iface_desc = &interface->altsetting[0];
1410        for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
1411                endpoint = &iface_desc->endpoint[i];
1412                
1413                if ((endpoint->bEndpointAddress & 0x80) &&
1414                    ((endpoint->bmAttributes & 3) == 0x02)) {
1415                        /* we found a bulk in endpoint */
1416                        dbg("found bulk in");
1417                        bulk_in_endpoint[num_bulk_in] = endpoint;
1418                        ++num_bulk_in;
1419                }
1420
1421                if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
1422                    ((endpoint->bmAttributes & 3) == 0x02)) {
1423                        /* we found a bulk out endpoint */
1424                        dbg("found bulk out");
1425                        bulk_out_endpoint[num_bulk_out] = endpoint;
1426                        ++num_bulk_out;
1427                }
1428                
1429                if ((endpoint->bEndpointAddress & 0x80) &&
1430                    ((endpoint->bmAttributes & 3) == 0x03)) {
1431                        /* we found a interrupt in endpoint */
1432                        dbg("found interrupt in");
1433                        interrupt_in_endpoint[num_interrupt_in] = endpoint;
1434                        ++num_interrupt_in;
1435                }
1436        }
1437
1438#if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
1439        /* BEGIN HORRIBLE HACK FOR PL2303 */ 
1440        /* this is needed due to the looney way its endpoints are set up */
1441        if (((dev->descriptor.idVendor == PL2303_VENDOR_ID) &&
1442             (dev->descriptor.idProduct == PL2303_PRODUCT_ID)) ||
1443            ((dev->descriptor.idVendor == ATEN_VENDOR_ID) &&
1444             (dev->descriptor.idProduct == ATEN_PRODUCT_ID))) {
1445                if (ifnum == 1) {
1446                        /* check out the endpoints of the other interface*/
1447                        struct usb_interface *other_iface;
1448
1449                        other_iface = &dev->actconfig->interface[ifnum ^ 1];
1450                        iface_desc = &other_iface->altsetting[0];
1451                        for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
1452                                endpoint = &iface_desc->endpoint[i];
1453                                if ((endpoint->bEndpointAddress & 0x80) &&
1454                                    ((endpoint->bmAttributes & 3) == 0x03)) {
1455                                        /* we found a interrupt in endpoint */
1456                                        dbg("found interrupt in for Prolific device on separate interface");
1457                                        interrupt_in_endpoint[num_interrupt_in] = endpoint;
1458                                        ++num_interrupt_in;
1459                                }
1460                        }
1461                }
1462
1463                /* Now make sure the PL-2303 is configured correctly.
1464                 * If not, give up now and hope this hack will work
1465                 * properly during a later invocation of usb_serial_probe
1466                 */
1467                if (num_bulk_in == 0 || num_bulk_out == 0) {
1468                        info("PL-2303 hack: descriptors matched but endpoints did not");
1469                        return NULL;
1470                }
1471        }
1472        /* END HORRIBLE HACK FOR PL2303 */
1473#endif
1474
1475        /* found all that we need */
1476        info("%s converter detected", type->name);
1477
1478#ifdef CONFIG_USB_SERIAL_GENERIC
1479        if (type == &generic_device) {
1480                num_ports = num_bulk_out;
1481                if (num_ports == 0) {
1482                        err("Generic device with no bulk out, not allowed.");
1483                        return NULL;
1484                }
1485        } else
1486#endif
1487                num_ports = type->num_ports;
1488
1489        serial = get_free_serial (num_ports, &minor);
1490        if (serial == NULL) {
1491                err("No more free serial devices");
1492                return NULL;
1493        }
1494
1495        serial->dev = dev;
1496        serial->type = type;
1497        serial->interface = interface;
1498        serial->minor = minor;
1499        serial->num_ports = num_ports;
1500        serial->num_bulk_in = num_bulk_in;
1501        serial->num_bulk_out = num_bulk_out;
1502        serial->num_interrupt_in = num_interrupt_in;
1503        serial->vendor = dev->descriptor.idVendor;
1504        serial->product = dev->descriptor.idProduct;
1505
1506        /* set up the endpoint information */
1507        for (i = 0; i < num_bulk_in; ++i) {
1508                endpoint = bulk_in_endpoint[i];
1509                port = &serial->port[i];
1510                port->read_urb = usb_alloc_urb (0);
1511                if (!port->read_urb) {
1512                        err("No free urbs available");
1513                        goto probe_error;
1514                }
1515                buffer_size = endpoint->wMaxPacketSize;
1516                port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
1517                port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1518                if (!port->bulk_in_buffer) {
1519                        err("Couldn't allocate bulk_in_buffer");
1520                        goto probe_error;
1521                }
1522                usb_fill_bulk_urb (port->read_urb, dev,
1523                                   usb_rcvbulkpipe (dev,
1524                                                    endpoint->bEndpointAddress),
1525                                   port->bulk_in_buffer, buffer_size,
1526                                   ((serial->type->read_bulk_callback) ? 
1527                                     serial->type->read_bulk_callback : 
1528                                     generic_read_bulk_callback),
1529                                   port);
1530        }
1531
1532        for (i = 0; i < num_bulk_out; ++i) {
1533                endpoint = bulk_out_endpoint[i];
1534                port = &serial->port[i];
1535                port->write_urb = usb_alloc_urb(0);
1536                if (!port->write_urb) {
1537                        err("No free urbs available");
1538                        goto probe_error;
1539                }
1540                buffer_size = endpoint->wMaxPacketSize;
1541                port->bulk_out_size = buffer_size;
1542                port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
1543                port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
1544                if (!port->bulk_out_buffer) {
1545                        err("Couldn't allocate bulk_out_buffer");
1546                        goto probe_error;
1547                }
1548                usb_fill_bulk_urb (port->write_urb, dev,
1549                                   usb_sndbulkpipe (dev,
1550                                                    endpoint->bEndpointAddress),
1551                                   port->bulk_out_buffer, buffer_size, 
1552                                   ((serial->type->write_bulk_callback) ? 
1553                                     serial->type->write_bulk_callback : 
1554                                     generic_write_bulk_callback),
1555                                   port);
1556        }
1557
1558        for (i = 0; i < num_interrupt_in; ++i) {
1559                endpoint = interrupt_in_endpoint[i];
1560                port = &serial->port[i];
1561                port->interrupt_in_urb = usb_alloc_urb(0);
1562                if (!port->interrupt_in_urb) {
1563                        err("No free urbs available");
1564                        goto probe_error;
1565                }
1566                buffer_size = endpoint->wMaxPacketSize;
1567                port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
1568                port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1569                if (!port->interrupt_in_buffer) {
1570                        err("Couldn't allocate interrupt_in_buffer");
1571                        goto probe_error;
1572                }
1573                usb_fill_int_urb (port->interrupt_in_urb, dev, 
1574                                  usb_rcvintpipe (dev,
1575                                                  endpoint->bEndpointAddress),
1576                                  port->interrupt_in_buffer, buffer_size, 
1577                                  serial->type->read_int_callback, port, 
1578                                  endpoint->bInterval);
1579        }
1580
1581        /* initialize some parts of the port structures */
1582        /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
1583        max_endpoints = max(num_bulk_in, num_bulk_out);
1584        max_endpoints = max(max_endpoints, num_interrupt_in);
1585        max_endpoints = max(max_endpoints, (int)serial->num_ports);
1586        dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
1587        for (i = 0; i < max_endpoints; ++i) {
1588                port = &serial->port[i];
1589                port->number = i + serial->minor;
1590                port->serial = serial;
1591                port->magic = USB_SERIAL_PORT_MAGIC;
1592                port->tqueue.routine = port_softint;
1593                port->tqueue.data = port;
1594                init_MUTEX (&port->sem);
1595        }
1596
1597        spin_lock_irqsave(&post_lock, flags);
1598        serial->ref = 1;
1599        spin_unlock_irqrestore(&post_lock, flags);
1600
1601        /* if this device type has a startup function, call it */
1602        if (type->startup) {
1603                i = type->startup (serial);
1604                if (i < 0)
1605                        goto startup_error;
1606                if (i > 0)
1607                        return serial;
1608        }
1609
1610        /* initialize the devfs nodes for this device and let the user know what ports we are bound to */
1611        for (i = 0; i < serial->num_ports; ++i) {
1612                tty_register_devfs (&serial_tty_driver, 0, serial->port[i].number);
1613                info("%s converter now attached to ttyUSB%d (or usb/tts/%d for devfs)", 
1614                     type->name, serial->port[i].number, serial->port[i].number);
1615        }
1616
1617        return serial; /* success */
1618
1619
1620startup_error:
1621        spin_lock_irqsave(&post_lock, flags);
1622        if (serial->ref != 1) {
1623                err("bug in component startup: ref %d\n", serial->ref);
1624        }
1625        spin_unlock_irqrestore(&post_lock, flags);
1626probe_error:
1627        for (i = 0; i < num_bulk_in; ++i) {
1628                port = &serial->port[i];
1629                if (port->read_urb)
1630                        usb_free_urb (port->read_urb);
1631                if (port->bulk_in_buffer)
1632                        kfree (port->bulk_in_buffer);
1633        }
1634        for (i = 0; i < num_bulk_out; ++i) {
1635                port = &serial->port[i];
1636                if (port->write_urb)
1637                        usb_free_urb (port->write_urb);
1638                if (port->bulk_out_buffer)
1639                        kfree (port->bulk_out_buffer);
1640        }
1641        for (i = 0; i < num_interrupt_in; ++i) {
1642                port = &serial->port[i];
1643                if (port->interrupt_in_urb)
1644                        usb_free_urb (port->interrupt_in_urb);
1645                if (port->interrupt_in_buffer)
1646                        kfree (port->interrupt_in_buffer);
1647        }
1648
1649        /* return the minor range that this device had */
1650        return_serial (serial);
1651
1652        /* free up any memory that we allocated */
1653        kfree (serial);
1654        return NULL;
1655}
1656
1657static void usb_serial_disconnect(struct usb_device *dev, void *ptr)
1658{
1659        struct usb_serial *serial = (struct usb_serial *) ptr;
1660        struct usb_serial_port *port;
1661        unsigned long flags;
1662        int i;
1663
1664        dbg ("%s", __FUNCTION__);
1665        if (serial) {
1666                /* fail all future close/read/write/ioctl/etc calls */
1667                for (i = 0; i < serial->num_ports; ++i) {
1668                        port = &serial->port[i];
1669                        down (&port->sem);
1670                        if (port->tty != NULL)
1671                                while (port->open_count > 0)
1672                                        __serial_close(port, NULL);
1673                        up (&port->sem);
1674                }
1675
1676                serial->dev = NULL;
1677                serial_shutdown (serial);
1678
1679                for (i = 0; i < serial->num_ports; ++i)
1680                        serial->port[i].open_count = 0;
1681
1682                for (i = 0; i < serial->num_bulk_in; ++i) {
1683                        port = &serial->port[i];
1684                        if (port->read_urb) {
1685                                usb_unlink_urb (port->read_urb);
1686                                usb_free_urb (port->read_urb);
1687                        }
1688                        if (port->bulk_in_buffer)
1689                                kfree (port->bulk_in_buffer);
1690                }
1691                for (i = 0; i < serial->num_bulk_out; ++i) {
1692                        port = &serial->port[i];
1693                        if (port->write_urb) {
1694                                usb_unlink_urb (port->write_urb);
1695                                usb_free_urb (port->write_urb);
1696                        }
1697                        if (port->bulk_out_buffer)
1698                                kfree (port->bulk_out_buffer);
1699                }
1700                for (i = 0; i < serial->num_interrupt_in; ++i) {
1701                        port = &serial->port[i];
1702                        if (port->interrupt_in_urb) {
1703                                usb_unlink_urb (port->interrupt_in_urb);
1704                                usb_free_urb (port->interrupt_in_urb);
1705                        }
1706                        if (port->interrupt_in_buffer)
1707                                kfree (port->interrupt_in_buffer);
1708                }
1709
1710                for (i = 0; i < serial->num_ports; ++i) {
1711                        tty_unregister_devfs (&serial_tty_driver, serial->port[i].number);
1712                        info("%s converter now disconnected from ttyUSB%d", serial->type->name, serial->port[i].number);
1713                }
1714
1715                /* return the minor range that this device had */
1716                return_serial (serial);
1717
1718                /* free up any memory that we allocated */
1719                spin_lock_irqsave(&post_lock, flags);
1720                if (--serial->ref == 0)
1721                        kfree(serial);
1722                spin_unlock_irqrestore(&post_lock, flags);
1723
1724        } else {
1725                info("device disconnected");
1726        }
1727
1728}
1729
1730
1731static struct tty_driver serial_tty_driver = {
1732        .magic =                TTY_DRIVER_MAGIC,
1733        .driver_name =          "usb-serial",
1734#ifndef CONFIG_DEVFS_FS
1735        .name =                 "ttyUSB",
1736#else
1737        .name =                 "usb/tts/%d",
1738#endif
1739        .major =                SERIAL_TTY_MAJOR,
1740        .minor_start =          0,
1741        .num =                  SERIAL_TTY_MINORS,
1742        .type =                 TTY_DRIVER_TYPE_SERIAL,
1743        .subtype =              SERIAL_TYPE_NORMAL,
1744        .flags =                TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
1745
1746        .refcount =             &serial_refcount,
1747        .table =                serial_tty,
1748        .termios =              serial_termios,
1749        .termios_locked =       serial_termios_locked,
1750
1751        .open =                 serial_open,
1752        .close =                serial_close,
1753        .write =                serial_write,
1754        .write_room =           serial_write_room,
1755        .ioctl =                serial_ioctl,
1756        .set_termios =          serial_set_termios,
1757        .throttle =             serial_throttle,
1758        .unthrottle =           serial_unthrottle,
1759        .break_ctl =            serial_break,
1760        .chars_in_buffer =      serial_chars_in_buffer,
1761        .read_proc =            serial_read_proc,
1762};
1763
1764
1765static int __init usb_serial_init(void)
1766{
1767        int i;
1768        int result;
1769
1770        /* Initalize our global data */
1771        for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1772                serial_table[i] = NULL;
1773        }
1774        post_task.routine = post_helper;
1775
1776        /* register the tty driver */
1777        serial_tty_driver.init_termios          = tty_std_termios;
1778        serial_tty_driver.init_termios.c_cflag  = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1779        if (tty_register_driver (&serial_tty_driver)) {
1780                err("%s - failed to register tty driver", __FUNCTION__);
1781                return -1;
1782        }
1783
1784        /* register the USB driver */
1785        result = usb_register(&usb_serial_driver);
1786        if (result < 0) {
1787                tty_unregister_driver(&serial_tty_driver);
1788                err("usb_register failed for the usb-serial driver. Error number %d", result);
1789                return -1;
1790        }
1791
1792#ifdef CONFIG_USB_SERIAL_GENERIC
1793        generic_device_ids[0].idVendor = vendor;
1794        generic_device_ids[0].idProduct = product;
1795        generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
1796        /* register our generic driver with ourselves */
1797        usb_serial_register (&generic_device);
1798#endif
1799
1800        info(DRIVER_DESC " " DRIVER_VERSION);
1801
1802        return 0;
1803}
1804
1805
1806static void __exit usb_serial_exit(void)
1807{
1808
1809#ifdef CONFIG_USB_SERIAL_GENERIC
1810        /* remove our generic driver */
1811        usb_serial_deregister (&generic_device);
1812#endif
1813        
1814        usb_deregister(&usb_serial_driver);
1815        tty_unregister_driver(&serial_tty_driver);
1816}
1817
1818
1819module_init(usb_serial_init);
1820module_exit(usb_serial_exit);
1821
1822
1823int usb_serial_register(struct usb_serial_device_type *new_device)
1824{
1825        /* Add this device to our list of devices */
1826        list_add(&new_device->driver_list, &usb_serial_driver_list);
1827
1828        info ("USB Serial support registered for %s", new_device->name);
1829
1830        usb_scan_devices();
1831
1832        return 0;
1833}
1834
1835
1836void usb_serial_deregister(struct usb_serial_device_type *device)
1837{
1838        struct usb_serial *serial;
1839        int i;
1840
1841        info("USB Serial deregistering driver %s", device->name);
1842
1843        /* clear out the serial_table if the device is attached to a port */
1844        for(i = 0; i < SERIAL_TTY_MINORS; ++i) {
1845                serial = serial_table[i];
1846                if ((serial != NULL) && (serial->type == device)) {
1847                        usb_driver_release_interface (&usb_serial_driver, serial->interface);
1848                        usb_serial_disconnect (NULL, serial);
1849                }
1850        }
1851
1852        list_del(&device->driver_list);
1853}
1854
1855
1856
1857/* If the usb-serial core is built into the core, the usb-serial drivers
1858   need these symbols to load properly as modules. */
1859EXPORT_SYMBOL(usb_serial_register);
1860EXPORT_SYMBOL(usb_serial_deregister);
1861#ifdef USES_EZUSB_FUNCTIONS
1862        EXPORT_SYMBOL(ezusb_writememory);
1863        EXPORT_SYMBOL(ezusb_set_reset);
1864#endif
1865
1866
1867/* Module information */
1868MODULE_AUTHOR( DRIVER_AUTHOR );
1869MODULE_DESCRIPTION( DRIVER_DESC );
1870MODULE_LICENSE("GPL");
1871
1872MODULE_PARM(debug, "i");
1873MODULE_PARM_DESC(debug, "Debug enabled or not");
1874
1875#ifdef CONFIG_USB_SERIAL_GENERIC
1876MODULE_PARM(vendor, "h");
1877MODULE_PARM_DESC(vendor, "User specified USB idVendor");
1878
1879MODULE_PARM(product, "h");
1880MODULE_PARM_DESC(product, "User specified USB idProduct");
1881#endif
1882
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.