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 int  clie_5_startup      (struct usb_serial *serial);
 188static void treo_attach         (struct usb_serial *serial);
 189
 190/* Parameters that may be passed into the module. */
 191static int vendor = -1;
 192static int product = -1;
 193static int param_register;
 194
 195
 196static struct usb_device_id id_table [] = {
 197        { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID) },
 198        { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID) },
 199        { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID) },
 200        { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID) },
 201        { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID) },
 202        { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID) },
 203        { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID) },
 204        { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID) },
 205        { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID) },
 206        { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID) },
 207        { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID) },
 208        { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID) },
 209        { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE31_ID) },
 210        { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID) },
 211        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID) },
 212        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID) },
 213        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID) },
 214        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID) },
 215        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID) },
 216        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID) },
 217        { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID) },
 218        { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID) },
 219        { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID) },
 220        { }                                     /* Terminating entry */
 221};
 222
 223static struct usb_device_id clie_id_3_5_table [] = {
 224        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
 225        { }                                     /* Terminating entry */
 226};
 227
 228
 229static struct usb_device_id clie_id_5_table [] = {
 230        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID) },
 231        { }                                     /* Terminating entry */
 232};
 233
 234static __devinitdata struct usb_device_id id_table_combined [] = {
 235        { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID) },
 236        { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID) },
 237        { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID) },
 238        { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID) },
 239        { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID) },
 240        { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID) },
 241        { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID) },
 242        { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID) },
 243        { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID) },
 244        { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID) },
 245        { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID) },
 246        { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID) },
 247        { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE31_ID) },
 248        { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID) },
 249        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
 250        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID) },
 251        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID) },
 252        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID) },
 253        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID) },
 254        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID) },
 255        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID) },
 256        { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID) },
 257        { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID) },
 258        { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID) },
 259        { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID) },
 260        { }                                     /* Terminating entry */
 261};
 262
 263/* For passed in parameters */
 264static struct usb_device_id id_param_table [] = {
 265        { },
 266        { }                                     /* Terminating entry */
 267};
 268
 269MODULE_DEVICE_TABLE (usb, id_table_combined);
 270
 271
 272
 273/* All of the device info needed for the Handspring Visor, and Palm 4.0 devices */
 274static struct usb_serial_device_type handspring_device = {
 275        .owner =                THIS_MODULE,
 276        .name =                 "Handspring Visor / Treo / Palm 4.0 / CliƩ 4.x",
 277        .id_table =             id_table,
 278        .num_interrupt_in =     NUM_DONT_CARE,
 279        .num_bulk_in =          2,
 280        .num_bulk_out =         2,
 281        .num_ports =            2,
 282        .open =                 visor_open,
 283        .close =                visor_close,
 284        .throttle =             visor_throttle,
 285        .unthrottle =           visor_unthrottle,
 286        .startup =              visor_startup,
 287        .shutdown =             visor_shutdown,
 288        .ioctl =                visor_ioctl,
 289        .set_termios =          visor_set_termios,
 290        .write =                visor_write,
 291        .write_room =           visor_write_room,
 292        .chars_in_buffer =      visor_chars_in_buffer,
 293        .write_bulk_callback =  visor_write_bulk_callback,
 294        .read_bulk_callback =   visor_read_bulk_callback,
 295        .read_int_callback =    visor_read_int_callback,
 296};
 297
 298/* device info for the Sony Clie OS version 3.5 */
 299static struct usb_serial_device_type clie_3_5_device = {
 300        .owner =                THIS_MODULE,
 301        .name =                 "Sony CliƩ 3.5",
 302        .id_table =             clie_id_3_5_table,
 303        .num_interrupt_in =     0,
 304        .num_bulk_in =          1,
 305        .num_bulk_out =         1,
 306        .num_ports =            1,
 307        .open =                 visor_open,
 308        .close =                visor_close,
 309        .throttle =             visor_throttle,
 310        .unthrottle =           visor_unthrottle,
 311        .startup =              clie_3_5_startup,
 312        .ioctl =                visor_ioctl,
 313        .set_termios =          visor_set_termios,
 314        .write =                visor_write,
 315        .write_room =           visor_write_room,
 316        .chars_in_buffer =      visor_chars_in_buffer,
 317        .write_bulk_callback =  visor_write_bulk_callback,
 318        .read_bulk_callback =   visor_read_bulk_callback,
 319};
 320
 321
 322/* device info for the Sony Clie OS version 5.0 */
 323static struct usb_serial_device_type clie_5_device = {
 324        .owner =                THIS_MODULE,
 325        .name =                 "Sony CliƩ 5.0",
 326        .id_table =             clie_id_5_table,
 327        .num_interrupt_in =     NUM_DONT_CARE,
 328        .num_bulk_in =          2,
 329        .num_bulk_out =         2,
 330        .num_ports =            2,
 331        .open =                 visor_open,
 332        .close =                visor_close,
 333        .throttle =             visor_throttle,
 334        .unthrottle =           visor_unthrottle,
 335        .startup =              clie_5_startup,
 336        .shutdown =             visor_shutdown,
 337        .ioctl =                visor_ioctl,
 338        .set_termios =          visor_set_termios,
 339        .write =                visor_write,
 340        .write_room =           visor_write_room,
 341        .chars_in_buffer =      visor_chars_in_buffer,
 342        .write_bulk_callback =  visor_write_bulk_callback,
 343        .read_bulk_callback =   visor_read_bulk_callback,
 344        .read_int_callback =    visor_read_int_callback,
 345};
 346
 347/* This structure is for Handspring Visor, and Palm 4.0 devices that are not
 348 * compiled into the kernel, but can be passed in when the module is loaded.
 349 * This will allow the visor driver to work with new Vendor and Device IDs
 350 * without recompiling the driver.
 351 */
 352static struct usb_serial_device_type param_device = {
 353        .owner =                THIS_MODULE,
 354        .name =                 "user specified device with Palm 4.x protocols",
 355        .id_table =             id_param_table,
 356        .num_interrupt_in =     NUM_DONT_CARE,
 357        .num_bulk_in =          2,
 358        .num_bulk_out =         2,
 359        .num_ports =            2,
 360        .open =                 visor_open,
 361        .close =                visor_close,
 362        .throttle =             visor_throttle,
 363        .unthrottle =           visor_unthrottle,
 364        .startup =              visor_startup,
 365        .shutdown =             visor_shutdown,
 366        .ioctl =                visor_ioctl,
 367        .set_termios =          visor_set_termios,
 368        .write =                visor_write,
 369        .write_room =           visor_write_room,
 370        .chars_in_buffer =      visor_chars_in_buffer,
 371        .write_bulk_callback =  visor_write_bulk_callback,
 372        .read_bulk_callback =   visor_read_bulk_callback,
 373        .read_int_callback =    visor_read_int_callback,
 374};
 375
 376#define NUM_URBS                        24
 377#define URB_TRANSFER_BUFFER_SIZE        768
 378static struct urb       *write_urb_pool[NUM_URBS];
 379static spinlock_t       write_urb_pool_lock;
 380static int              bytes_in;
 381static int              bytes_out;
 382
 383
 384/******************************************************************************
 385 * Handspring Visor specific driver functions
 386 ******************************************************************************/
 387static int visor_open (struct usb_serial_port *port, struct file *filp)
 388{
 389        struct usb_serial *serial = port->serial;
 390        int result = 0;
 391
 392        if (port_paranoia_check (port, __FUNCTION__))
 393                return -ENODEV;
 394        
 395        dbg("%s - port %d", __FUNCTION__, port->number);
 396
 397        if (!port->read_urb) {
 398                /* this is needed for some brain dead Sony devices */
 399                err ("Device lied about number of ports, please use a lower one.");
 400                return -ENODEV;
 401        }
 402
 403        bytes_in = 0;
 404        bytes_out = 0;
 405
 406        /*
 407         * Force low_latency on so that our tty_push actually forces the data
 408         * through, otherwise it is scheduled, and with high data rates (like
 409         * with OHCI) data can get lost.
 410         */
 411        if (port->tty)
 412                port->tty->low_latency = 1;
 413
 414        /* Start reading from the device */
 415        usb_fill_bulk_urb (port->read_urb, serial->dev,
 416                           usb_rcvbulkpipe (serial->dev, 
 417                                            port->bulk_in_endpointAddress),
 418                           port->read_urb->transfer_buffer,
 419                           port->read_urb->transfer_buffer_length,
 420                           visor_read_bulk_callback, port);
 421        result = usb_submit_urb(port->read_urb);
 422        if (result) {
 423                err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
 424                goto exit;
 425        }
 426        
 427        if (port->interrupt_in_urb) {
 428                dbg("%s - adding interrupt input for treo", __FUNCTION__);
 429                result = usb_submit_urb(port->interrupt_in_urb);
 430                if (result)
 431                        err("%s - failed submitting interrupt urb, error %d\n",
 432                            __FUNCTION__, result);
 433        }
 434exit:
 435        return result;
 436}
 437
 438
 439static void visor_close (struct usb_serial_port *port, struct file * filp)
 440{
 441        struct usb_serial *serial;
 442        unsigned char *transfer_buffer;
 443
 444        if (port_paranoia_check (port, __FUNCTION__))
 445                return;
 446        
 447        dbg("%s - port %d", __FUNCTION__, port->number);
 448                         
 449        serial = get_usb_serial (port, __FUNCTION__);
 450        if (!serial)
 451                return;
 452        
 453
 454
 455
 456        if (serial->dev) {
 457                /* only send a shutdown message if the 
 458                 * device is still here */
 459                transfer_buffer =  kmalloc (0x12, GFP_KERNEL);
 460                if (!transfer_buffer) {
 461                        err("%s - kmalloc(%d) failed.", __FUNCTION__, 0x12);
 462                } else {
 463                        /* send a shutdown message to the device */
 464                        usb_control_msg (serial->dev,
 465                                         usb_rcvctrlpipe(serial->dev, 0),
 466                                         VISOR_CLOSE_NOTIFICATION, 0xc2,
 467                                         0x0000, 0x0000, 
 468                                         transfer_buffer, 0x12, 300);
 469                        kfree (transfer_buffer);
 470                }
 471
 472                /* shutdown our urbs */
 473                usb_unlink_urb (port->read_urb);
 474                if (port->interrupt_in_urb)
 475                  usb_unlink_urb (port->interrupt_in_urb);
 476                /* Try to send shutdown message, if the device is gone, this will just fail. */
 477                transfer_buffer =  kmalloc (0x12, GFP_KERNEL);
 478                if (transfer_buffer) {
 479                  usb_control_msg (serial->dev,
 480                                 usb_rcvctrlpipe(serial->dev, 0),
 481                                 VISOR_CLOSE_NOTIFICATION, 0xc2,
 482                                 0x0000, 0x0000, 
 483                                 transfer_buffer, 0x12, 300);
 484                kfree (transfer_buffer);                        
 485                
 486                }
 487        }
 488        /* Uncomment the following line if you want to see some statistics in your syslog */
 489        info ("Bytes In = %d  Bytes Out = %d", bytes_in, bytes_out);
 490}
 491
 492
 493static int visor_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
 494{
 495        struct usb_serial *serial = port->serial;
 496        struct urb *urb;
 497        const unsigned char *current_position = buf;
 498        unsigned long flags;
 499        int status;
 500        int i;
 501        int bytes_sent = 0;
 502        int transfer_size;
 503
 504        dbg("%s - port %d", __FUNCTION__, port->number);
 505
 506        while (count > 0) {
 507                /* try to find a free urb in our list of them */
 508                urb = NULL;
 509                spin_lock_irqsave (&write_urb_pool_lock, flags);
 510                for (i = 0; i < NUM_URBS; ++i) {
 511                        if (write_urb_pool[i]->status != -EINPROGRESS) {
 512                                urb = write_urb_pool[i];
 513                                break;
 514                        }
 515                }
 516                spin_unlock_irqrestore (&write_urb_pool_lock, flags);
 517                if (urb == NULL) {
 518                        dbg("%s - no more free urbs", __FUNCTION__);
 519                        goto exit;
 520                }
 521                if (urb->transfer_buffer == NULL) {
 522                        urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
 523                        if (urb->transfer_buffer == NULL) {
 524                                err("%s no more kernel memory...", __FUNCTION__);
 525                                goto exit;
 526                        }
 527                }
 528                
 529                transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
 530                if (from_user) {
 531                        if (copy_from_user (urb->transfer_buffer, current_position, transfer_size)) {
 532                                bytes_sent = -EFAULT;
 533                                break;
 534                        }
 535                } else {
 536                        memcpy (urb->transfer_buffer, current_position, transfer_size);
 537                }
 538
 539                usb_serial_debug_data (__FILE__, __FUNCTION__, transfer_size, urb->transfer_buffer);
 540
 541                /* build up our urb */
 542                FILL_BULK_URB (urb, serial->dev, usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress), 
 543                                urb->transfer_buffer, transfer_size, visor_write_bulk_callback, port);
 544                urb->transfer_flags |= USB_QUEUE_BULK;
 545
 546                /* send it down the pipe */
 547                status = usb_submit_urb(urb);
 548                if (status) {
 549                        err("%s - usb_submit_urb(write bulk) failed with status = %d", __FUNCTION__, status);
 550                        bytes_sent = status;
 551                        break;
 552                }
 553
 554                current_position += transfer_size;
 555                bytes_sent += transfer_size;
 556                count -= transfer_size;
 557                bytes_out += transfer_size;
 558        }
 559
 560exit:
 561        return bytes_sent;
 562} 
 563
 564
 565static int visor_write_room (struct usb_serial_port *port)
 566{
 567        unsigned long flags;
 568        int i;
 569        int room = 0;
 570
 571        dbg("%s - port %d", __FUNCTION__, port->number);
 572        
 573        spin_lock_irqsave (&write_urb_pool_lock, flags);
 574
 575        for (i = 0; i < NUM_URBS; ++i) {
 576                if (write_urb_pool[i]->status != -EINPROGRESS) {
 577                        room += URB_TRANSFER_BUFFER_SIZE;
 578                }
 579        }
 580        
 581        spin_unlock_irqrestore (&write_urb_pool_lock, flags);
 582        
 583        dbg("%s - returns %d", __FUNCTION__, room);
 584        return (room);
 585}
 586
 587
 588static int visor_chars_in_buffer (struct usb_serial_port *port)
 589{
 590        unsigned long flags;
 591        int i;
 592        int chars = 0;
 593
 594        dbg("%s - port %d", __FUNCTION__, port->number);
 595        
 596        spin_lock_irqsave (&write_urb_pool_lock, flags);
 597
 598        for (i = 0; i < NUM_URBS; ++i) {
 599                if (write_urb_pool[i]->status == -EINPROGRESS) {
 600                        chars += URB_TRANSFER_BUFFER_SIZE;
 601                }
 602        }
 603        
 604        spin_unlock_irqrestore (&write_urb_pool_lock, flags);
 605
 606        dbg("%s - returns %d", __FUNCTION__, chars);
 607        return (chars);
 608}
 609
 610
 611static void visor_write_bulk_callback (struct urb *urb)
 612{
 613        struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
 614
 615        if (port_paranoia_check (port, __FUNCTION__))
 616                return;
 617        
 618        dbg("%s - port %d", __FUNCTION__, port->number);
 619        
 620        if (urb->status) {
 621                dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
 622                return;
 623        }
 624
 625        queue_task(&port->tqueue, &tq_immediate);
 626        mark_bh(IMMEDIATE_BH);
 627
 628        return;
 629}
 630
 631
 632static void visor_read_bulk_callback (struct urb *urb)
 633{
 634        struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
 635        struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
 636        struct tty_struct *tty;
 637        unsigned char *data = urb->transfer_buffer;
 638        int i;
 639        int result;
 640
 641        if (port_paranoia_check (port, __FUNCTION__))
 642                return;
 643
 644        dbg("%s - port %d", __FUNCTION__, port->number);
 645
 646        if (!serial) {
 647                dbg("%s - bad serial pointer, exiting", __FUNCTION__);
 648                return;
 649        }
 650
 651        if (urb->status) {
 652                dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
 653                return;
 654        }
 655
 656        usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
 657
 658        tty = port->tty;
 659        if (tty && urb->actual_length) {
 660                for (i = 0; i < urb->actual_length ; ++i) {
 661                        /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
 662                        if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
 663                                tty_flip_buffer_push(tty);
 664                        }
 665                        /* this doesn't actually push the data through unless tty->low_latency is set */
 666                        tty_insert_flip_char(tty, data[i], 0);
 667                }
 668                tty_flip_buffer_push(tty);
 669                bytes_in += urb->actual_length;
 670        }
 671
 672        /* Continue trying to always read  */
 673        usb_fill_bulk_urb (port->read_urb, serial->dev,
 674                           usb_rcvbulkpipe (serial->dev,
 675                                            port->bulk_in_endpointAddress),
 676                           port->read_urb->transfer_buffer,
 677                           port->read_urb->transfer_buffer_length,
 678                           visor_read_bulk_callback, port);
 679        result = usb_submit_urb(port->read_urb);
 680        if (result)
 681                err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
 682        return;
 683}
 684
 685
 686static void visor_read_int_callback (struct urb *urb)
 687{
 688        switch (urb->status) {
 689        case 0:
 690                /* success */
 691                break;
 692        case -ECONNRESET:
 693        case -ENOENT:
 694        case -ESHUTDOWN:
 695                /* this urb is terminated, clean up */
 696                dbg("%s - urb shutting down with status: %d",
 697                    __FUNCTION__, urb->status);
 698                return;
 699        default:
 700                dbg("%s - nonzero urb status received: %d",
 701                    __FUNCTION__, urb->status);
 702                goto exit;
 703        }
 704
 705        /*
 706         * This information is still unknown what it can be used for.
 707         * If anyone has an idea, please let the author know...
 708         *
 709         * Rumor has it this endpoint is used to notify when data
 710         * is ready to be read from the bulk ones.
 711         */
 712        usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length,
 713                               urb->transfer_buffer);
 714
 715exit:
 716        return;
 717}
 718
 719
 720static void visor_throttle (struct usb_serial_port *port)
 721{
 722        dbg("%s - port %d", __FUNCTION__, port->number);
 723        usb_unlink_urb (port->read_urb);
 724}
 725
 726
 727static void visor_unthrottle (struct usb_serial_port *port)
 728{
 729        int result;
 730
 731        dbg("%s - port %d", __FUNCTION__, port->number);
 732
 733        port->read_urb->dev = port->serial->dev;
 734        result = usb_submit_urb(port->read_urb);
 735        if (result)
 736                err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
 737}
 738
 739static int visor_startup (struct usb_serial *serial)
 740{
 741        int response;
 742        int i;
 743        unsigned char *transfer_buffer;
 744
 745        dbg("%s", __FUNCTION__);
 746
 747        dbg("%s - Set config to 1", __FUNCTION__);
 748        usb_set_configuration (serial->dev, 1);
 749
 750        if ((serial->dev->descriptor.idVendor == HANDSPRING_VENDOR_ID) &&
 751            (serial->dev->descriptor.idProduct == HANDSPRING_VISOR_ID)) {
 752                struct visor_connection_info *connection_info;
 753                char *string;
 754                int num_ports;
 755
 756                transfer_buffer = kmalloc (sizeof (*connection_info),
 757                                           GFP_KERNEL);
 758                if (!transfer_buffer) {
 759                        err("%s - kmalloc(%d) failed.", __FUNCTION__,
 760                            sizeof (*connection_info));
 761                        return -ENOMEM;
 762                }
 763
 764                /* send a get connection info request */
 765                response = usb_control_msg (serial->dev,
 766                                            usb_rcvctrlpipe(serial->dev, 0),
 767                                            VISOR_GET_CONNECTION_INFORMATION,
 768                                            0xc2, 0x0000, 0x0000,
 769                                            transfer_buffer,
 770                                            sizeof (*connection_info), 300);
 771                if (response < 0) {
 772                        err("%s - error getting connection information",
 773                            __FUNCTION__);
 774                        goto exit;
 775                }
 776
 777                connection_info = (struct visor_connection_info *)transfer_buffer;
 778                le16_to_cpus(&connection_info->num_ports);
 779                num_ports = connection_info->num_ports;
 780
 781                /* handle devices that report invalid stuff here */
 782                if (num_ports > 2)
 783                        num_ports = 2;
 784                info("%s: Number of ports: %d", serial->type->name, connection_info->num_ports);
 785                for (i = 0; i < num_ports; ++i) {
 786                        switch (connection_info->connections[i].port_function_id) {
 787                                case VISOR_FUNCTION_GENERIC:
 788                                        string = "Generic";
 789                                        break;
 790                                case VISOR_FUNCTION_DEBUGGER:
 791                                        string = "Debugger";
 792                                        break;
 793                                case VISOR_FUNCTION_HOTSYNC:
 794                                        string = "HotSync";
 795                                        break;
 796                                case VISOR_FUNCTION_CONSOLE:
 797                                        string = "Console";
 798                                        break;
 799                                case VISOR_FUNCTION_REMOTE_FILE_SYS:
 800                                        string = "Remote File System";
 801                                        break;
 802                                default:
 803                                        string = "unknown";
 804                                        break;  
 805                        }
 806                        info("%s: port %d, is for %s use and is bound to ttyUSB%d", serial->type->name,
 807                             connection_info->connections[i].port, string, serial->minor + i);
 808                }
 809        } else {
 810                struct palm_ext_connection_info *connection_info;
 811
 812                transfer_buffer = kmalloc (sizeof (*connection_info),
 813                                           GFP_KERNEL);
 814                if (!transfer_buffer) {
 815                        err("%s - kmalloc(%d) failed.", __FUNCTION__,
 816                            sizeof (*connection_info));
 817                        return -ENOMEM;
 818                }
 819
 820                response = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), 
 821                                            PALM_GET_EXT_CONNECTION_INFORMATION,
 822                                            0xc2, 0x0000, 0x0000, transfer_buffer, 
 823                                            sizeof (*connection_info), 300);
 824                if (response < 0) {
 825                        err("%s - error %d getting connection info",
 826                            __FUNCTION__, response);
 827                } else {
 828                        usb_serial_debug_data (__FILE__, __FUNCTION__, 0x14, transfer_buffer);
 829                }
 830        }
 831
 832        /* Do our horrible Treo hack, if we should */
 833        treo_attach(serial);
 834
 835        /* ask for the number of bytes available, but ignore the response as it is broken */
 836        response = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), VISOR_REQUEST_BYTES_AVAILABLE,
 837                                        0xc2, 0x0000, 0x0005, transfer_buffer, 0x02, 300);
 838        if (response < 0) {
 839                err("%s - error getting bytes available request", __FUNCTION__);
 840        }
 841
 842exit:
 843        kfree (transfer_buffer);
 844
 845        /* continue on with initialization */
 846        return 0;
 847}
 848
 849static int clie_3_5_startup (struct usb_serial *serial)
 850{
 851        int result;
 852        u8 data;
 853
 854        dbg("%s", __FUNCTION__);
 855
 856        /*
 857         * Note that PEG-300 series devices expect the following two calls.
 858         */
 859
 860        /* get the config number */
 861        result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0),
 862                                  USB_REQ_GET_CONFIGURATION, USB_DIR_IN,
 863                                  0, 0, &data, 1, HZ * 3);
 864        if (result < 0) {
 865                err("%s: get config number failed: %d", __FUNCTION__, result);
 866                return result;
 867        }
 868        if (result != 1) {
 869                err("%s: get config number bad return length: %d", __FUNCTION__, result);
 870                return -EIO;
 871        }
 872
 873        /* get the interface number */
 874        result = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0),
 875                                  USB_REQ_GET_INTERFACE, 
 876                                  USB_DIR_IN | USB_DT_DEVICE,
 877                                  0, 0, &data, 1, HZ * 3);
 878        if (result < 0) {
 879                err("%s: get interface number failed: %d", __FUNCTION__, result);
 880                return result;
 881        }
 882        if (result != 1) {
 883                err("%s: get interface number bad return length: %d", __FUNCTION__, result);
 884                return -EIO;
 885        }
 886
 887        return 0;
 888}
 889
 890
 891static int clie_5_startup (struct usb_serial *serial)
 892{
 893        int response;
 894        unsigned char *transfer_buffer;
 895        struct palm_ext_connection_info *connection_info;
 896
 897        dbg("%s", __FUNCTION__);
 898
 899        dbg("%s - Set config to 1", __FUNCTION__);
 900        usb_set_configuration(serial->dev, 1);
 901
 902        transfer_buffer = kmalloc(sizeof (*connection_info),
 903                                        GFP_KERNEL);
 904        if (!transfer_buffer) {
 905                err("%s - kmalloc(%d) failed.", __FUNCTION__,
 906                        sizeof (*connection_info));
 907                return -ENOMEM;
 908        }
 909
 910        response = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 
 911                                        PALM_GET_EXT_CONNECTION_INFORMATION,
 912                                        0xc2, 0x0000, 0x0000, transfer_buffer, 
 913                                        sizeof(*connection_info), 300);
 914        if (response < 0) {
 915                err("%s - error %d getting connection info",
 916                         __FUNCTION__, response);
 917        } else {
 918                usb_serial_debug_data (__FILE__, __FUNCTION__, 0x14, transfer_buffer);
 919        }
 920
 921        /* ask for the number of bytes available, but ignore the response as it is broken */
 922        response = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), VISOR_REQUEST_BYTES_AVAILABLE,
 923                                        0xc2, 0x0000, 0x0005, transfer_buffer, 0x02, 300);
 924        if (response < 0) {
 925                err("%s - error getting bytes available request", __FUNCTION__);
 926        }
 927
 928        kfree (transfer_buffer);
 929
 930        /* UX50/TH55 registers 2 ports. 
 931           Communication in from the TH55 uses bulk_in_endpointAddress from port 0 
 932           Communication out to the TH55 uses bulk_out_endpointAddress from port 1 
 933
 934           Lets do a quick and dirty mapping
 935        */
 936
 937        /* some sanity check */
 938        if (serial->num_ports < 2)
 939                return -ENODEV;
 940
 941        /* port 0 now uses the modified endpoint Address */
 942        serial->port[0].bulk_out_endpointAddress = serial->port[1].bulk_out_endpointAddress;
 943
 944        /* continue on with initialization */
 945        return 0;
 946}
 947
 948
 949
 950
 951static void treo_attach (struct usb_serial *serial)
 952{
 953        struct usb_serial_port *port;
 954        int i;
 955
 956        /* Only do this endpoint hack for the Handspring devices with
 957         * interrupt in endpoints, which for now are the Treo devices. */
 958        if ((serial->dev->descriptor.idVendor != HANDSPRING_VENDOR_ID) ||
 959            (serial->num_interrupt_in == 0))
 960                return;
 961
 962        dbg("%s", __FUNCTION__);
 963
 964        /* Ok, this is pretty ugly, but these devices want to use the
 965         * interrupt endpoint as paired up with a bulk endpoint for a
 966         * "virtual serial port".  So let's force the endpoints to be
 967         * where we want them to be. */
 968        for (i = serial->num_bulk_in; i < serial->num_ports; ++i) {
 969                port = &serial->port[i];
 970                port->read_urb = serial->port[0].read_urb;
 971                port->bulk_in_endpointAddress = serial->port[0].bulk_in_endpointAddress;
 972                port->bulk_in_buffer = serial->port[0].bulk_in_buffer;
 973        }
 974
 975        for (i = serial->num_bulk_out; i < serial->num_ports; ++i) {
 976                port = &serial->port[i];
 977                port->write_urb = serial->port[0].write_urb;
 978                port->bulk_out_size = serial->port[0].bulk_out_size;
 979                port->bulk_out_endpointAddress = serial->port[0].bulk_out_endpointAddress;
 980                port->bulk_out_buffer = serial->port[0].bulk_out_buffer;
 981        }
 982
 983        for (i = serial->num_interrupt_in; i < serial->num_ports; ++i) {
 984                port = &serial->port[i];
 985                port->interrupt_in_urb = serial->port[0].interrupt_in_urb;
 986                port->interrupt_in_endpointAddress = serial->port[0].interrupt_in_endpointAddress;
 987                port->interrupt_in_buffer = serial->port[0].interrupt_in_buffer;
 988        }
 989}
 990
 991static void visor_shutdown (struct usb_serial *serial)
 992{
 993        dbg("%s", __FUNCTION__);
 994}
 995
 996static int visor_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
 997{
 998        dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
 999
1000        return -ENOIOCTLCMD;
1001}
1002
1003
1004/* This function is all nice and good, but we don't change anything based on it :) */
1005static void visor_set_termios (struct usb_serial_port *port, struct termios *old_termios)
1006{
1007        unsigned int cflag;
1008
1009        dbg("%s - port %d", __FUNCTION__, port->number);
1010
1011        if ((!port->tty) || (!port->tty->termios)) {
1012                dbg("%s - no tty structures", __FUNCTION__);
1013                return;
1014        }
1015
1016        cflag = port->tty->termios->c_cflag;
1017        /* check that they really want us to change something */
1018        if (old_termios) {
1019                if ((cflag == old_termios->c_cflag) &&
1020                    (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
1021                        dbg("%s - nothing to change...", __FUNCTION__);
1022                        return;
1023                }
1024        }
1025
1026        /* get the byte size */
1027        switch (cflag & CSIZE) {
1028                case CS5:       dbg("%s - data bits = 5", __FUNCTION__);   break;
1029                case CS6:       dbg("%s - data bits = 6", __FUNCTION__);   break;
1030                case CS7:       dbg("%s - data bits = 7", __FUNCTION__);   break;
1031                default:
1032                case CS8:       dbg("%s - data bits = 8", __FUNCTION__);   break;
1033        }
1034        
1035        /* determine the parity */
1036        if (cflag & PARENB)
1037                if (cflag & PARODD)
1038                        dbg("%s - parity = odd", __FUNCTION__);
1039                else
1040                        dbg("%s - parity = even", __FUNCTION__);
1041        else
1042                dbg("%s - parity = none", __FUNCTION__);
1043
1044        /* figure out the stop bits requested */
1045        if (cflag & CSTOPB)
1046                dbg("%s - stop bits = 2", __FUNCTION__);
1047        else
1048                dbg("%s - stop bits = 1", __FUNCTION__);
1049
1050        
1051        /* figure out the flow control settings */
1052        if (cflag & CRTSCTS)
1053                dbg("%s - RTS/CTS is enabled", __FUNCTION__);
1054        else
1055                dbg("%s - RTS/CTS is disabled", __FUNCTION__);
1056        
1057        /* determine software flow control */
1058        if (I_IXOFF(port->tty))
1059                dbg("%s - XON/XOFF is enabled, XON = %2x, XOFF = %2x",
1060                    __FUNCTION__, START_CHAR(port->tty), STOP_CHAR(port->tty));
1061        else
1062                dbg("%s - XON/XOFF is disabled", __FUNCTION__);
1063
1064        /* get the baud rate wanted */
1065        dbg("%s - baud rate = %d", __FUNCTION__, tty_get_baud_rate(port->tty));
1066
1067        return;
1068}
1069
1070
1071static int __init visor_init (void)
1072{
1073        struct urb *urb;
1074        int i;
1075
1076        /* Only if parameters were passed to us */
1077        if ((vendor > 0) && (product > 0)) {
1078                struct usb_device_id usb_dev_temp[]=
1079                        {{USB_DEVICE(vendor, product)}};
1080                id_param_table[0] = usb_dev_temp[0];
1081                info("Untested USB device specified at time of module insertion");
1082                info("Warning: This is not guaranteed to work");
1083                info("Using a newer kernel is preferred to this method");
1084                info("Adding Palm OS protocol 4.x support for unknown device: 0x%x/0x%x",
1085                        param_device.id_table[0].idVendor, param_device.id_table[0].idProduct);
1086                param_register = 1;
1087                usb_serial_register (&param_device);
1088        }
1089        usb_serial_register (&handspring_device);
1090        usb_serial_register (&clie_3_5_device);
1091        usb_serial_register (&clie_5_device);
1092        
1093        /* create our write urb pool and transfer buffers */ 
1094        spin_lock_init (&write_urb_pool_lock);
1095        for (i = 0; i < NUM_URBS; ++i) {
1096                urb = usb_alloc_urb(0);
1097                write_urb_pool[i] = urb;
1098                if (urb == NULL) {
1099                        err("No more urbs???");
1100                        continue;
1101                }
1102
1103                urb->transfer_buffer = NULL;
1104                urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1105                if (!urb->transfer_buffer) {
1106                        err("%s - out of memory for urb buffers.", __FUNCTION__);
1107                        continue;
1108                }
1109        }
1110
1111        info(DRIVER_DESC " " DRIVER_VERSION);
1112
1113        return 0;
1114}
1115
1116
1117static void __exit visor_exit (void)
1118{
1119        int i;
1120        unsigned long flags;
1121
1122        if (param_register) {
1123                param_register = 0;
1124                usb_serial_deregister (&param_device);
1125        }
1126        usb_serial_deregister (&handspring_device);
1127        usb_serial_deregister (&clie_3_5_device);
1128        usb_serial_deregister (&clie_5_device);
1129
1130        spin_lock_irqsave (&write_urb_pool_lock, flags);
1131
1132        for (i = 0; i < NUM_URBS; ++i) {
1133                if (write_urb_pool[i]) {
1134                        /* FIXME - uncomment the following usb_unlink_urb call when
1135                         * the host controllers get fixed to set urb->dev = NULL after
1136                         * the urb is finished.  Otherwise this call oopses. */
1137                        /* usb_unlink_urb(write_urb_pool[i]); */
1138                        if (write_urb_pool[i]->transfer_buffer)
1139                                kfree(write_urb_pool[i]->transfer_buffer);
1140                        usb_free_urb (write_urb_pool[i]);
1141                }
1142        }
1143
1144        spin_unlock_irqrestore (&write_urb_pool_lock, flags);
1145}
1146
1147
1148module_init(visor_init);
1149module_exit(visor_exit);
1150
1151MODULE_AUTHOR( DRIVER_AUTHOR );
1152MODULE_DESCRIPTION( DRIVER_DESC );
1153MODULE_LICENSE("GPL");
1154
1155MODULE_PARM(debug, "i");
1156MODULE_PARM_DESC(debug, "Debug enabled or not");
1157MODULE_PARM(vendor, "i");
1158MODULE_PARM_DESC(vendor, "User specified vendor ID");
1159MODULE_PARM(product, "i");
1160MODULE_PARM_DESC(product, "User specified product ID");
1161
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.