linux-old/drivers/usb/serial/visor.c
<<
>>
Prefs
   1/*
   2 * USB HandSpring Visor, Palm m50x, and Sony Clie driver
   3 * (supports all of the Palm OS USB devices)
   4 *
   5 *      Copyright (C) 1999 - 2003
   6 *          Greg Kroah-Hartman (greg@kroah.com)
   7 *
   8 *      This program is free software; you can redistribute it and/or modify
   9 *      it under the terms of the GNU General Public License as published by
  10 *      the Free Software Foundation; either version 2 of the License, or
  11 *      (at your option) any later version.
  12 *
  13 * See Documentation/usb/usb-serial.txt for more information on using this driver
  14 *
  15 * (06/03/2003) Judd Montgomery <judd at jpilot.org>
  16 *      Added support for module parameter options for untested/unknown
  17 *      devices.
  18 *
  19 * (03/09/2003) gkh
  20 *      Added support for the Sony Clie NZ90V device.  Thanks to Martin Brachtl
  21 *      <brachtl@redgrep.cz> for the information.
  22 *
  23 * (3/07/2003) Adam Pennington <adamp@coed.org>
  24 *      Backported version 2.1 of the driver from the 2.5 bitkeeper tree
  25 *      making Treo actually work.
  26 *
  27 * (2/18/2003) Adam Powell <hazelsct at debian.org>
  28 *      Backported 2.5 driver mods to support Handspring Treo.
  29 *
  30 * (2/11/2003) Adam Powell <hazelsct at debian.org>
  31 *      Added device and vendor ids for the Samsung I330 phone.
  32 *
  33 * (04/03/2002) gkh
  34 *      Added support for the Sony OS 4.1 devices.  Thanks to Hiroyuki ARAKI
  35 *      <hiro@zob.ne.jp> for the information.
  36 *
  37 * (03/23/2002) gkh
  38 *      Added support for the Palm i705 device, thanks to Thomas Riemer
  39 *      <tom@netmech.com> for the information.
  40 *
  41 * (03/21/2002) gkh
  42 *      Added support for the Palm m130 device, thanks to Udo Eisenbarth
  43 *      <udo.eisenbarth@web.de> for the information.
  44 *
  45 * (02/21/2002) SilaS
  46 *      Added support for the Palm m515 devices.
  47 *
  48 * (02/15/2002) gkh
  49 *      Added support for the Clie S-360 device.
  50 *
  51 * (12/18/2001) gkh
  52 *      Added better Clie support for 3.5 devices.  Thanks to Geoffrey Levand
  53 *      for the patch.
  54 *
  55 * (11/11/2001) gkh
  56 *      Added support for the m125 devices, and added check to prevent oopses
  57 *      for CliƩ devices that lie about the number of ports they have.
  58 *
  59 * (08/30/2001) gkh
  60 *      Added support for the Clie devices, both the 3.5 and 4.0 os versions.
  61 *      Many thanks to Daniel Burke, and Bryan Payne for helping with this.
  62 *
  63 * (08/23/2001) gkh
  64 *      fixed a few potential bugs pointed out by Oliver Neukum.
  65 *
  66 * (05/30/2001) gkh
  67 *      switched from using spinlock to a semaphore, which fixes lots of problems.
  68 *
  69 * (05/28/2000) gkh
  70 *      Added initial support for the Palm m500 and Palm m505 devices.
  71 *
  72 * (04/08/2001) gb
  73 *      Identify version on module load.
  74 *
  75 * (01/21/2000) gkh
  76 *      Added write_room and chars_in_buffer, as they were previously using the
  77 *      generic driver versions which is all wrong now that we are using an urb
  78 *      pool.  Thanks to Wolfgang Grandegger for pointing this out to me.
  79 *      Removed count assignment in the write function, which was not needed anymore
  80 *      either.  Thanks to Al Borchers for pointing this out.
  81 *
  82 * (12/12/2000) gkh
  83 *      Moved MOD_DEC to end of visor_close to be nicer, as the final write 
  84 *      message can sleep.
  85 * 
  86 * (11/12/2000) gkh
  87 *      Fixed bug with data being dropped on the floor by forcing tty->low_latency
  88 *      to be on.  Hopefully this fixes the OHCI issue!
  89 *
  90 * (11/01/2000) Adam J. Richter
  91 *      usb_device_id table support
  92 * 
  93 * (10/05/2000) gkh
  94 *      Fixed bug with urb->dev not being set properly, now that the usb
  95 *      core needs it.
  96 * 
  97 * (09/11/2000) gkh
  98 *      Got rid of always calling kmalloc for every urb we wrote out to the
  99 *      device.
 100 *      Added visor_read_callback so we can keep track of bytes in and out for
 101 *      those people who like to know the speed of their device.
 102 *      Removed DEBUG #ifdefs with call to usb_serial_debug_data
 103 *
 104 * (09/06/2000) gkh
 105 *      Fixed oops in visor_exit.  Need to uncomment usb_unlink_urb call _after_
 106 *      the host controller drivers set urb->dev = NULL when the urb is finished.
 107 *
 108 * (08/28/2000) gkh
 109 *      Added locks for SMP safeness.
 110 *
 111 * (08/08/2000) gkh
 112 *      Fixed endian problem in visor_startup.
 113 *      Fixed MOD_INC and MOD_DEC logic and the ability to open a port more 
 114 *      than once.
 115 * 
 116 * (07/23/2000) gkh
 117 *      Added pool of write urbs to speed up transfers to the visor.
 118 * 
 119 * (07/19/2000) gkh
 120 *      Added module_init and module_exit functions to handle the fact that this
 121 *      driver is a loadable module now.
 122 *
 123 * (07/03/2000) gkh
 124 *      Added visor_set_ioctl and visor_set_termios functions (they don't do much
 125 *      of anything, but are good for debugging.)
 126 * 
 127 * (06/25/2000) gkh
 128 *      Fixed bug in visor_unthrottle that should help with the disconnect in PPP
 129 *      bug that people have been reporting.
 130 *
 131 * (06/23/2000) gkh
 132 *      Cleaned up debugging statements in a quest to find UHCI timeout bug.
 133 *
 134 * (04/27/2000) Ryan VanderBijl
 135 *      Fixed memory leak in visor_close
 136 *
 137 * (03/26/2000) gkh
 138 *      Split driver up into device specific pieces.
 139 * 
 140 */
 141
 142#include <linux/config.h>
 143#include <linux/kernel.h>
 144#include <linux/errno.h>
 145#include <linux/init.h>
 146#include <linux/slab.h>
 147#include <linux/tty.h>
 148#include <linux/tty_driver.h>
 149#include <linux/tty_flip.h>
 150#include <linux/module.h>
 151#include <linux/spinlock.h>
 152#include <asm/uaccess.h>
 153#include <linux/usb.h>
 154
 155#ifdef CONFIG_USB_SERIAL_DEBUG
 156        static int debug = 1;
 157#else
 158        static int debug;
 159#endif
 160
 161#include "usb-serial.h"
 162#include "visor.h"
 163
 164/*
 165 * Version Information
 166 */
 167#define DRIVER_VERSION "v1.7"
 168#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
 169#define DRIVER_DESC "USB HandSpring Visor, Palm m50x, Treo, Sony CliƩ driver"
 170
 171/* function prototypes for a handspring visor */
 172static int  visor_open          (struct usb_serial_port *port, struct file *filp);
 173static void visor_close         (struct usb_serial_port *port, struct file *filp);
 174static int  visor_write         (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
 175static int  visor_write_room            (struct usb_serial_port *port);
 176static int  visor_chars_in_buffer       (struct usb_serial_port *port);
 177static void visor_throttle      (struct usb_serial_port *port);
 178static void visor_unthrottle    (struct usb_serial_port *port);
 179static int  visor_startup       (struct usb_serial *serial);
 180static void visor_shutdown      (struct usb_serial *serial);
 181static int  visor_ioctl         (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
 182static void visor_set_termios   (struct usb_serial_port *port, struct termios *old_termios);
 183static void visor_write_bulk_callback   (struct urb *urb);
 184static void visor_read_bulk_callback    (struct urb *urb);
 185static void visor_read_int_callback     (struct urb *urb);
 186static int  clie_3_5_startup    (struct usb_serial *serial);
 187static void treo_attach         (struct usb_serial *serial);
 188
 189/* Parameters that may be passed into the module. */
 190static int vendor = -1;
 191static int product = -1;
 192static int param_register;
 193
 194
 195static struct usb_device_id id_table [] = {
 196        { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID) },
 197        { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID) },
 198        { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID) },
 199        { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID) },
 200        { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID) },
 201        { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID) },
 202        { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID) },
 203        { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID) },
 204        { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID) },
 205        { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID) },
 206        { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID) },
 207        { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID) },
 208        { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID) },
 209        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID) },
 210        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID) },
 211        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID) },
 212        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID) },
 213        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID) },
 214        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID) },
 215        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID) },
 216        { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID) },
 217        { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID) },
 218        { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID) },
 219        { }                                     /* Terminating entry */
 220};
 221
 222static struct usb_device_id clie_id_3_5_table [] = {
 223        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
 224        { }                                     /* Terminating entry */
 225};
 226
 227static __devinitdata struct usb_device_id id_table_combined [] = {
 228        { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID) },
 229        { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID) },
 230        { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID) },
 231        { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID) },
 232        { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID) },
 233        { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID) },
 234        { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID) },
 235        { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID) },
 236        { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID) },
 237        { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID) },
 238        { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID) },
 239        { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID) },
 240        { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID) },
 241        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
 242        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID) },
 243        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID) },
 244        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID) },
 245        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID) },
 246        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID) },
 247        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID) },
 248        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID) },
 249        { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID) },
 250        { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID) },
 251        { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID) },
 252        { }                                     /* Terminating entry */
 253};
 254
 255/* For passed in parameters */
 256static struct usb_device_id id_param_table [] = {
 257        { },
 258        { }                                     /* Terminating entry */
 259};
 260
 261MODULE_DEVICE_TABLE (usb, id_table_combined);
 262
 263
 264
 265/* All of the device info needed for the Handspring Visor, and Palm 4.0 devices */
 266static struct usb_serial_device_type handspring_device = {
 267        .owner =                THIS_MODULE,
 268        .name =                 "Handspring Visor / Treo / Palm 4.0 / CliƩ 4.x",
 269        .id_table =             id_table,
 270        .num_interrupt_in =     NUM_DONT_CARE,
 271        .num_bulk_in =          2,
 272        .num_bulk_out =         2,
 273        .num_ports =            2,
 274        .open =                 visor_open,
 275        .close =                visor_close,
 276        .throttle =             visor_throttle,
 277        .unthrottle =           visor_unthrottle,
 278        .startup =              visor_startup,
 279        .shutdown =             visor_shutdown,
 280        .ioctl =                visor_ioctl,
 281        .set_termios =          visor_set_termios,
 282        .write =                visor_write,
 283        .write_room =           visor_write_room,
 284        .chars_in_buffer =      visor_chars_in_buffer,
 285        .write_bulk_callback =  visor_write_bulk_callback,
 286        .read_bulk_callback =   visor_read_bulk_callback,
 287        .read_int_callback =    visor_read_int_callback,
 288};
 289
 290/* device info for the Sony Clie OS version 3.5 */
 291static struct usb_serial_device_type clie_3_5_device = {
 292        .owner =                THIS_MODULE,
 293        .name =                 "Sony CliƩ 3.5",
 294        .id_table =             clie_id_3_5_table,
 295        .num_interrupt_in =     0,
 296        .num_bulk_in =          1,
 297        .num_bulk_out =         1,
 298        .num_ports =            1,
 299        .open =                 visor_open,
 300        .close =                visor_close,
 301        .throttle =             visor_throttle,
 302        .unthrottle =           visor_unthrottle,
 303        .startup =              clie_3_5_startup,
 304        .ioctl =                visor_ioctl,
 305        .set_termios =          visor_set_termios,
 306        .write =                visor_write,
 307        .write_room =           visor_write_room,
 308        .chars_in_buffer =      visor_chars_in_buffer,
 309        .write_bulk_callback =  visor_write_bulk_callback,
 310        .read_bulk_callback =   visor_read_bulk_callback,
 311};
 312
 313/* This structure is for Handspring Visor, and Palm 4.0 devices that are not
 314 * compiled into the kernel, but can be passed in when the module is loaded.
 315 * This will allow the visor driver to work with new Vendor and Device IDs
 316 * without recompiling the driver.
 317 */
 318static struct usb_serial_device_type param_device = {
 319        .owner =                THIS_MODULE,
 320        .name =                 "user specified device with Palm 4.x protocols",
 321        .id_table =             id_param_table,
 322        .num_interrupt_in =     NUM_DONT_CARE,
 323        .num_bulk_in =          2,
 324        .num_bulk_out =         2,
 325        .num_ports =            2,
 326        .open =                 visor_open,
 327        .close =                visor_close,
 328        .throttle =             visor_throttle,
 329        .unthrottle =           visor_unthrottle,
 330        .startup =              visor_startup,
 331        .shutdown =             visor_shutdown,
 332        .ioctl =                visor_ioctl,
 333        .set_termios =          visor_set_termios,
 334        .write =                visor_write,
 335        .write_room =           visor_write_room,
 336        .chars_in_buffer =      visor_chars_in_buffer,
 337        .write_bulk_callback =  visor_write_bulk_callback,
 338        .read_bulk_callback =   visor_read_bulk_callback,
 339        .read_int_callback =    visor_read_int_callback,
 340};
 341
 342#define NUM_URBS                        24
 343#define URB_TRANSFER_BUFFER_SIZE        768
 344static struct urb       *write_urb_pool[NUM_URBS];
 345static spinlock_t       write_urb_pool_lock;
 346static int              bytes_in;
 347static int              bytes_out;
 348
 349
 350/******************************************************************************
 351 * Handspring Visor specific driver functions
 352 ******************************************************************************/
 353static int visor_open (struct usb_serial_port *port, struct file *filp)
 354{
 355        struct usb_serial *serial = port->serial;
 356        int result = 0;
 357
 358        if (port_paranoia_check (port, __FUNCTION__))
 359                return -ENODEV;
 360        
 361        dbg("%s - port %d", __FUNCTION__, port->number);
 362
 363        if (!port->read_urb) {
 364                /* this is needed for some brain dead Sony devices */
 365                err ("Device lied about number of ports, please use a lower one.");
 366                return -ENODEV;
 367        }
 368
 369        bytes_in = 0;
 370        bytes_out = 0;
 371
 372        /*
 373         * Force low_latency on so that our tty_push actually forces the data
 374         * through, otherwise it is scheduled, and with high data rates (like
 375         * with OHCI) data can get lost.
 376         */
 377        if (port->tty)
 378                port->tty->low_latency = 1;
 379
 380        /* Start reading from the device */
 381        usb_fill_bulk_urb (port->read_urb, serial->dev,
 382                           usb_rcvbulkpipe (serial->dev, 
 383                                            port->bulk_in_endpointAddress),
 384                           port->read_urb->transfer_buffer,
 385                           port->read_urb->transfer_buffer_length,
 386                           visor_read_bulk_callback, port);
 387        result = usb_submit_urb(port->read_urb);
 388        if (result) {
 389                err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
 390                goto exit;
 391        }
 392        
 393        if (port->interrupt_in_urb) {
 394                dbg("%s - adding interrupt input for treo", __FUNCTION__);
 395                result = usb_submit_urb(port->interrupt_in_urb);
 396                if (result)
 397                        err("%s - failed submitting interrupt urb, error %d\n",
 398                            __FUNCTION__, result);
 399        }
 400exit:
 401        return result;
 402}
 403
 404
 405static void visor_close (struct usb_serial_port *port, struct file * filp)
 406{
 407        struct usb_serial *serial;
 408        unsigned char *transfer_buffer;
 409
 410        if (port_paranoia_check (port, __FUNCTION__))
 411                return;
 412        
 413        dbg("%s - port %d", __FUNCTION__, port->number);
 414                         
 415        serial = get_usb_serial (port, __FUNCTION__);
 416        if (!serial)
 417                return;
 418        
 419
 420
 421
 422        if (serial->dev) {
 423                /* only send a shutdown message if the 
 424                 * device is still here */
 425                transfer_buffer =  kmalloc (0x12, GFP_KERNEL);
 426                if (!transfer_buffer) {
 427                        err("%s - kmalloc(%d) failed.", __FUNCTION__, 0x12);
 428                } else {
 429                        /* send a shutdown message to the device */
 430                        usb_control_msg (serial->dev,
 431                                         usb_rcvctrlpipe(serial->dev, 0),
 432                                         VISOR_CLOSE_NOTIFICATION, 0xc2,
 433                                         0x0000, 0x0000, 
 434                                         transfer_buffer, 0x12, 300);
 435                        kfree (transfer_buffer);
 436                }
 437
 438                /* shutdown our urbs */
 439                usb_unlink_urb (port->read_urb);
 440                if (port->interrupt_in_urb)
 441                  usb_unlink_urb (port->interrupt_in_urb);
 442                /* Try to send shutdown message, if the device is gone, this will just fail. */
 443                transfer_buffer =  kmalloc (0x12, GFP_KERNEL);
 444                if (transfer_buffer) {
 445                  usb_control_msg (serial->dev,
 446                                 usb_rcvctrlpipe(serial->dev, 0),
 447                                 VISOR_CLOSE_NOTIFICATION, 0xc2,
 448                                 0x0000, 0x0000, 
 449                                 transfer_buffer, 0x12, 300);
 450                kfree (transfer_buffer);                        
 451                
 452                }
 453        }
 454        /* Uncomment the following line if you want to see some statistics in your syslog */
 455        info ("Bytes In = %d  Bytes Out = %d", bytes_in, bytes_out);
 456}
 457
 458
 459static int visor_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
 460{
 461        struct usb_serial *serial = port->serial;
 462        struct urb *urb;
 463        const unsigned char *current_position = buf;
 464        unsigned long flags;
 465        int status;
 466        int i;
 467        int bytes_sent = 0;
 468        int transfer_size;
 469
 470        dbg("%s - port %d", __FUNCTION__, port->number);
 471
 472        while (count > 0) {
 473                /* try to find a free urb in our list of them */
 474                urb = NULL;
 475                spin_lock_irqsave (&write_urb_pool_lock, flags);
 476                for (i = 0; i < NUM_URBS; ++i) {
 477                        if (write_urb_pool[i]->status != -EINPROGRESS) {
 478                                urb = write_urb_pool[i];
 479                                break;
 480                        }
 481                }
 482                spin_unlock_irqrestore (&write_urb_pool_lock, flags);
 483                if (urb == NULL) {
 484                        dbg("%s - no more free urbs", __FUNCTION__);
 485                        goto exit;
 486                }
 487                if (urb->transfer_buffer == NULL) {
 488                        urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
 489                        if (urb->transfer_buffer == NULL) {
 490                                err("%s no more kernel memory...", __FUNCTION__);
 491                                goto exit;
 492                        }
 493                }
 494                
 495                transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
 496                if (from_user) {
 497                        if (copy_from_user (urb->transfer_buffer, current_position, transfer_size)) {
 498                                bytes_sent = -EFAULT;
 499                                break;
 500                        }
 501                } else {
 502                        memcpy (urb->transfer_buffer, current_position, transfer_size);
 503                }
 504
 505                usb_serial_debug_data (__FILE__, __FUNCTION__, transfer_size, urb->transfer_buffer);
 506
 507                /* build up our urb */
 508                FILL_BULK_URB (urb, serial->dev, usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress), 
 509                                urb->transfer_buffer, transfer_size, visor_write_bulk_callback, port);
 510                urb->transfer_flags |= USB_QUEUE_BULK;
 511
 512                /* send it down the pipe */
 513                status = usb_submit_urb(urb);
 514                if (status) {
 515                        err("%s - usb_submit_urb(write bulk) failed with status = %d", __FUNCTION__, status);
 516                        bytes_sent = status;
 517                        break;
 518                }
 519
 520                current_position += transfer_size;
 521                bytes_sent += transfer_size;
 522                count -= transfer_size;
 523                bytes_out += transfer_size;
 524        }
 525
 526exit:
 527        return bytes_sent;
 528} 
 529
 530
 531static int visor_write_room (struct usb_serial_port *port)
 532{
 533        unsigned long flags;
 534        int i;
 535        int room = 0;
 536
 537        dbg("%s - port %d", __FUNCTION__, port->number);
 538        
 539        spin_lock_irqsave (&write_urb_pool_lock, flags);
 540
 541        for (i = 0; i < NUM_URBS; ++i) {
 542                if (write_urb_pool[i]->status != -EINPROGRESS) {
 543                        room += URB_TRANSFER_BUFFER_SIZE;
 544                }
 545        }
 546        
 547        spin_unlock_irqrestore (&write_urb_pool_lock, flags);
 548        
 549        dbg("%s - returns %d", __FUNCTION__, room);
 550        return (room);
 551}
 552
 553
 554static int visor_chars_in_buffer (struct usb_serial_port *port)
 555{
 556        unsigned long flags;
 557        int i;
 558        int chars = 0;
 559
 560        dbg("%s - port %d", __FUNCTION__, port->number);
 561        
 562        spin_lock_irqsave (&write_urb_pool_lock, flags);
 563
 564        for (i = 0; i < NUM_URBS; ++i) {
 565                if (write_urb_pool[i]->status == -EINPROGRESS) {
 566                        chars += URB_TRANSFER_BUFFER_SIZE;
 567                }
 568        }
 569        
 570        spin_unlock_irqrestore (&write_urb_pool_lock, flags);
 571
 572        dbg("%s - returns %d", __FUNCTION__, chars);
 573        return (chars);
 574}
 575
 576
 577static void visor_write_bulk_callback (struct urb *urb)
 578{
 579        struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
 580
 581        if (port_paranoia_check (port, __FUNCTION__))
 582                return;
 583        
 584        dbg("%s - port %d", __FUNCTION__, port->number);
 585        
 586        if (urb->status) {
 587                dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
 588                return;
 589        }
 590
 591        queue_task(&port->tqueue, &tq_immediate);
 592        mark_bh(IMMEDIATE_BH);
 593
 594        return;
 595}
 596
 597
 598static void visor_read_bulk_callback (struct urb *urb)
 599{
 600        struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
 601        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 602        struct tty_struct *tty;
 603        unsigned char *data = urb->transfer_buffer;
 604        int i;
 605        int result;
 606
 607        if (port_paranoia_check (port, __FUNCTION__))
 608                return;
 609
 610        dbg("%s - port %d", __FUNCTION__, port->number);
 611
 612        if (!serial) {
 613                dbg("%s - bad serial pointer, exiting", __FUNCTION__);
 614                return;
 615        }
 616
 617        if (urb->status) {
 618                dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
 619                return;
 620        }
 621
 622        usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
 623
 624        tty = port->tty;
 625        if (tty && urb->actual_length) {
 626                for (i = 0; i < urb->actual_length ; ++i) {
 627                        /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
 628                        if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
 629                                tty_flip_buffer_push(tty);
 630                        }
 631                        /* this doesn't actually push the data through unless tty->low_latency is set */
 632                        tty_insert_flip_char(tty, data[i], 0);
 633                }
 634                tty_flip_buffer_push(tty);
 635                bytes_in += urb->actual_length;
 636        }
 637
 638        /* Continue trying to always read  */
 639        usb_fill_bulk_urb (port->read_urb, serial->dev,
 640                           usb_rcvbulkpipe (serial->dev,
 641                                            port->bulk_in_endpointAddress),
 642                           port->read_urb->transfer_buffer,
 643                           port->read_urb->transfer_buffer_length,
 644                           visor_read_bulk_callback, port);
 645        result = usb_submit_urb(port->read_urb);
 646        if (result)
 647                err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
 648        return;
 649}
 650
 651
 652static void visor_read_int_callback (struct urb *urb)
 653{
 654        switch (urb->status) {
 655        case 0:
 656                /* success */
 657                break;
 658        case -ECONNRESET:
 659        case -ENOENT:
 660        case -ESHUTDOWN:
 661                /* this urb is terminated, clean up */
 662                dbg("%s - urb shutting down with status: %d",
 663                    __FUNCTION__, urb->status);
 664                return;
 665        default:
 666                dbg("%s - nonzero urb status received: %d",
 667                    __FUNCTION__, urb->status);
 668                goto exit;
 669        }
 670
 671        /*
 672         * This information is still unknown what it can be used for.
 673         * If anyone has an idea, please let the author know...
 674         *
 675         * Rumor has it this endpoint is used to notify when data
 676         * is ready to be read from the bulk ones.
 677         */
 678        usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length,
 679                               urb->transfer_buffer);
 680
 681exit:
 682        return;
 683}
 684
 685
 686static void visor_throttle (struct usb_serial_port *port)
 687{
 688        dbg("%s - port %d", __FUNCTION__, port->number);
 689        usb_unlink_urb (port->read_urb);
 690}
 691
 692
 693static void visor_unthrottle (struct usb_serial_port *port)
 694{
 695        int result;
 696
 697        dbg("%s - port %d", __FUNCTION__, port->number);
 698
 699        port->read_urb->dev = port->serial->dev;
 700        result = usb_submit_urb(port->read_urb);
 701        if (result)
 702                err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
 703}
 704
 705static int visor_startup (struct usb_serial *serial)
 706{
 707        int response;
 708        int i;
 709        unsigned char *transfer_buffer;
 710
 711        dbg("%s", __FUNCTION__);
 712
 713        dbg("%s - Set config to 1", __FUNCTION__);
 714        usb_set_configuration (serial->dev, 1);
 715
 716        if ((serial->dev->descriptor.idVendor == HANDSPRING_VENDOR_ID) &&
 717            (serial->dev->descriptor.idProduct == HANDSPRING_VISOR_ID)) {
 718                struct visor_connection_info *connection_info;
 719                char *string;
 720                int num_ports;
 721
 722                transfer_buffer = kmalloc (sizeof (*connection_info),
 723                                           GFP_KERNEL);
 724                if (!transfer_buffer) {
 725                        err("%s - kmalloc(%d) failed.", __FUNCTION__,
 726                            sizeof (*connection_info));
 727                        return -ENOMEM;
 728                }
 729
 730                /* send a get connection info request */
 731                response = usb_control_msg (serial->dev,
 732                                            usb_rcvctrlpipe(serial->dev, 0),
 733                                            VISOR_GET_CONNECTION_INFORMATION,
 734                                            0xc2, 0x0000, 0x0000,
 735                                            transfer_buffer,
 736                                            sizeof (*connection_info), 300);
 737                if (response < 0) {
 738                        err("%s - error getting connection information",
 739                            __FUNCTION__);
 740                        goto exit;
 741                }
 742
 743                connection_info = (struct visor_connection_info *)transfer_buffer;
 744                le16_to_cpus(&connection_info->num_ports);
 745                num_ports = connection_info->num_ports;
 746
 747                /* handle devices that report invalid stuff here */
 748                if (num_ports > 2)
 749                        num_ports = 2;
 750                info("%s: Number of ports: %d", serial->type->name, connection_info->num_ports);
 751                for (i = 0; i < num_ports; ++i) {
 752                        switch (connection_info->connections[i].port_function_id) {
 753                                case VISOR_FUNCTION_GENERIC:
 754                                        string = "Generic";
 755                                        break;
 756                                case VISOR_FUNCTION_DEBUGGER:
 757                                        string = "Debugger";
 758                                        break;
 759                                case VISOR_FUNCTION_HOTSYNC:
 760                                        string = "HotSync";
 761                                        break;
 762                                case VISOR_FUNCTION_CONSOLE:
 763                                        string = "Console";
 764                                        break;
 765                                case VISOR_FUNCTION_REMOTE_FILE_SYS:
 766                                        string = "Remote File System";
 767                                        break;
 768                                default:
 769                                        string = "unknown";
 770                                        break;  
 771                        }
 772                        info("%s: port %d, is for %s use and is bound to ttyUSB%d", serial->type->name,
 773                             connection_info->connections[i].port, string, serial->minor + i);
 774                }
 775        } else {
 776                struct palm_ext_connection_info *connection_info;
 777
 778                transfer_buffer = kmalloc (sizeof (*connection_info),
 779                                           GFP_KERNEL);
 780                if (!transfer_buffer) {
 781                        err("%s - kmalloc(%d) failed.", __FUNCTION__,
 782                            sizeof (*connection_info));
 783                        return -ENOMEM;
 784                }
 785
 786                response = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), 
 787                                            PALM_GET_EXT_CONNECTION_INFORMATION,
 788                                            0xc2, 0x0000, 0x0000, transfer_buffer, 
 789                                            sizeof (*connection_info), 300);
 790                if (response < 0) {
 791                        err("%s - error %d getting connection info",
 792                            __FUNCTION__, response);
 793                } else {
 794                        usb_serial_debug_data (__FILE__, __FUNCTION__, 0x14, transfer_buffer);
 795                }
 796        }
 797
 798        /* Do our horrible Treo hack, if we should */
 799        treo_attach(serial);
 800
 801        /* ask for the number of bytes available, but ignore the response as it is broken */
 802        response = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), VISOR_REQUEST_BYTES_AVAILABLE,
 803                                        0xc2, 0x0000, 0x0005, transfer_buffer, 0x02, 300);
 804        if (response < 0) {
 805                err("%s - error getting bytes available request", __FUNCTION__);
 806        }
 807
 808exit:
 809        kfree (transfer_buffer);
 810
 811        /* continue on with initialization */
 812        return 0;
 813}
 814
 815static int clie_3_5_startup (struct usb_serial *serial)
 816{
 817        int result;
 818        u8 data;
 819
 820        dbg("%s", __FUNCTION__);
 821
 822        /*
 823         * Note that PEG-300 series devices expect the following two calls.
 824         */
 825
 826        /* get the config number */
 827        result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0),
 828                                  USB_REQ_GET_CONFIGURATION, USB_DIR_IN,
 829                                  0, 0, &data, 1, HZ * 3);
 830        if (result < 0) {
 831                err("%s: get config number failed: %d", __FUNCTION__, result);
 832                return result;
 833        }
 834        if (result != 1) {
 835                err("%s: get config number bad return length: %d", __FUNCTION__, result);
 836                return -EIO;
 837        }
 838
 839        /* get the interface number */
 840        result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0),
 841                                  USB_REQ_GET_INTERFACE, 
 842                                  USB_DIR_IN | USB_DT_DEVICE,
 843                                  0, 0, &data, 1, HZ * 3);
 844        if (result < 0) {
 845                err("%s: get interface number failed: %d", __FUNCTION__, result);
 846                return result;
 847        }
 848        if (result != 1) {
 849                err("%s: get interface number bad return length: %d", __FUNCTION__, result);
 850                return -EIO;
 851        }
 852
 853        return 0;
 854}
 855
 856static void treo_attach (struct usb_serial *serial)
 857{
 858        struct usb_serial_port *port;
 859        int i;
 860
 861        /* Only do this endpoint hack for the Handspring devices with
 862         * interrupt in endpoints, which for now are the Treo devices. */
 863        if ((serial->dev->descriptor.idVendor != HANDSPRING_VENDOR_ID) ||
 864            (serial->num_interrupt_in == 0))
 865                return;
 866
 867        dbg("%s", __FUNCTION__);
 868
 869        /* Ok, this is pretty ugly, but these devices want to use the
 870         * interrupt endpoint as paired up with a bulk endpoint for a
 871         * "virtual serial port".  So let's force the endpoints to be
 872         * where we want them to be. */
 873        for (i = serial->num_bulk_in; i < serial->num_ports; ++i) {
 874                port = &serial->port[i];
 875                port->read_urb = serial->port[0].read_urb;
 876                port->bulk_in_endpointAddress = serial->port[0].bulk_in_endpointAddress;
 877                port->bulk_in_buffer = serial->port[0].bulk_in_buffer;
 878        }
 879
 880        for (i = serial->num_bulk_out; i < serial->num_ports; ++i) {
 881                port = &serial->port[i];
 882                port->write_urb = serial->port[0].write_urb;
 883                port->bulk_out_size = serial->port[0].bulk_out_size;
 884                port->bulk_out_endpointAddress = serial->port[0].bulk_out_endpointAddress;
 885                port->bulk_out_buffer = serial->port[0].bulk_out_buffer;
 886        }
 887
 888        for (i = serial->num_interrupt_in; i < serial->num_ports; ++i) {
 889                port = &serial->port[i];
 890                port->interrupt_in_urb = serial->port[0].interrupt_in_urb;
 891                port->interrupt_in_endpointAddress = serial->port[0].interrupt_in_endpointAddress;
 892                port->interrupt_in_buffer = serial->port[0].interrupt_in_buffer;
 893        }
 894}
 895
 896static void visor_shutdown (struct usb_serial *serial)
 897{
 898        dbg("%s", __FUNCTION__);
 899}
 900
 901static int visor_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
 902{
 903        dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
 904
 905        return -ENOIOCTLCMD;
 906}
 907
 908
 909/* This function is all nice and good, but we don't change anything based on it :) */
 910static void visor_set_termios (struct usb_serial_port *port, struct termios *old_termios)
 911{
 912        unsigned int cflag;
 913
 914        dbg("%s - port %d", __FUNCTION__, port->number);
 915
 916        if ((!port->tty) || (!port->tty->termios)) {
 917                dbg("%s - no tty structures", __FUNCTION__);
 918                return;
 919        }
 920
 921        cflag = port->tty->termios->c_cflag;
 922        /* check that they really want us to change something */
 923        if (old_termios) {
 924                if ((cflag == old_termios->c_cflag) &&
 925                    (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
 926                        dbg("%s - nothing to change...", __FUNCTION__);
 927                        return;
 928                }
 929        }
 930
 931        /* get the byte size */
 932        switch (cflag & CSIZE) {
 933                case CS5:       dbg("%s - data bits = 5", __FUNCTION__);   break;
 934                case CS6:       dbg("%s - data bits = 6", __FUNCTION__);   break;
 935                case CS7:       dbg("%s - data bits = 7", __FUNCTION__);   break;
 936                default:
 937                case CS8:       dbg("%s - data bits = 8", __FUNCTION__);   break;
 938        }
 939        
 940        /* determine the parity */
 941        if (cflag & PARENB)
 942                if (cflag & PARODD)
 943                        dbg("%s - parity = odd", __FUNCTION__);
 944                else
 945                        dbg("%s - parity = even", __FUNCTION__);
 946        else
 947                dbg("%s - parity = none", __FUNCTION__);
 948
 949        /* figure out the stop bits requested */
 950        if (cflag & CSTOPB)
 951                dbg("%s - stop bits = 2", __FUNCTION__);
 952        else
 953                dbg("%s - stop bits = 1", __FUNCTION__);
 954
 955        
 956        /* figure out the flow control settings */
 957        if (cflag & CRTSCTS)
 958                dbg("%s - RTS/CTS is enabled", __FUNCTION__);
 959        else
 960                dbg("%s - RTS/CTS is disabled", __FUNCTION__);
 961        
 962        /* determine software flow control */
 963        if (I_IXOFF(port->tty))
 964                dbg("%s - XON/XOFF is enabled, XON = %2x, XOFF = %2x",
 965                    __FUNCTION__, START_CHAR(port->tty), STOP_CHAR(port->tty));
 966        else
 967                dbg("%s - XON/XOFF is disabled", __FUNCTION__);
 968
 969        /* get the baud rate wanted */
 970        dbg("%s - baud rate = %d", __FUNCTION__, tty_get_baud_rate(port->tty));
 971
 972        return;
 973}
 974
 975
 976static int __init visor_init (void)
 977{
 978        struct urb *urb;
 979        int i;
 980
 981        /* Only if parameters were passed to us */
 982        if ((vendor > 0) && (product > 0)) {
 983                struct usb_device_id usb_dev_temp[]=
 984                        {{USB_DEVICE(vendor, product)}};
 985                id_param_table[0] = usb_dev_temp[0];
 986                info("Untested USB device specified at time of module insertion");
 987                info("Warning: This is not guaranteed to work");
 988                info("Using a newer kernel is preferred to this method");
 989                info("Adding Palm OS protocol 4.x support for unknown device: 0x%x/0x%x",
 990                        param_device.id_table[0].idVendor, param_device.id_table[0].idProduct);
 991                param_register = 1;
 992                usb_serial_register (&param_device);
 993        }
 994        usb_serial_register (&handspring_device);
 995        usb_serial_register (&clie_3_5_device);
 996        
 997        /* create our write urb pool and transfer buffers */ 
 998        spin_lock_init (&write_urb_pool_lock);
 999        for (i = 0; i < NUM_URBS; ++i) {
1000                urb = usb_alloc_urb(0);
1001                write_urb_pool[i] = urb;
1002                if (urb == NULL) {
1003                        err("No more urbs???");
1004                        continue;
1005                }
1006
1007                urb->transfer_buffer = NULL;
1008                urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1009                if (!urb->transfer_buffer) {
1010                        err("%s - out of memory for urb buffers.", __FUNCTION__);
1011                        continue;
1012                }
1013        }
1014
1015        info(DRIVER_DESC " " DRIVER_VERSION);
1016
1017        return 0;
1018}
1019
1020
1021static void __exit visor_exit (void)
1022{
1023        int i;
1024        unsigned long flags;
1025
1026        if (param_register) {
1027                param_register = 0;
1028                usb_serial_deregister (&param_device);
1029        }
1030        usb_serial_deregister (&handspring_device);
1031        usb_serial_deregister (&clie_3_5_device);
1032
1033        spin_lock_irqsave (&write_urb_pool_lock, flags);
1034
1035        for (i = 0; i < NUM_URBS; ++i) {
1036                if (write_urb_pool[i]) {
1037                        /* FIXME - uncomment the following usb_unlink_urb call when
1038                         * the host controllers get fixed to set urb->dev = NULL after
1039                         * the urb is finished.  Otherwise this call oopses. */
1040                        /* usb_unlink_urb(write_urb_pool[i]); */
1041                        if (write_urb_pool[i]->transfer_buffer)
1042                                kfree(write_urb_pool[i]->transfer_buffer);
1043                        usb_free_urb (write_urb_pool[i]);
1044                }
1045        }
1046
1047        spin_unlock_irqrestore (&write_urb_pool_lock, flags);
1048}
1049
1050
1051module_init(visor_init);
1052module_exit(visor_exit);
1053
1054MODULE_AUTHOR( DRIVER_AUTHOR );
1055MODULE_DESCRIPTION( DRIVER_DESC );
1056MODULE_LICENSE("GPL");
1057
1058MODULE_PARM(debug, "i");
1059MODULE_PARM_DESC(debug, "Debug enabled or not");
1060MODULE_PARM(vendor, "i");
1061MODULE_PARM_DESC(vendor, "User specified vendor ID");
1062MODULE_PARM(product, "i");
1063MODULE_PARM_DESC(product, "User specified product ID");
1064
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.