linux/drivers/usb/storage/shuttle_usbat.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable
   4 *
   5 * Current development and maintenance by:
   6 *   (c) 2000, 2001 Robert Baruch (autophile@starband.net)
   7 *   (c) 2004, 2005 Daniel Drake <dsd@gentoo.org>
   8 *
   9 * Developed with the assistance of:
  10 *   (c) 2002 Alan Stern <stern@rowland.org>
  11 *
  12 * Flash support based on earlier work by:
  13 *   (c) 2002 Thomas Kreiling <usbdev@sm04.de>
  14 *
  15 * Many originally ATAPI devices were slightly modified to meet the USB
  16 * market by using some kind of translation from ATAPI to USB on the host,
  17 * and the peripheral would translate from USB back to ATAPI.
  18 *
  19 * SCM Microsystems (www.scmmicro.com) makes a device, sold to OEM's only, 
  20 * which does the USB-to-ATAPI conversion.  By obtaining the data sheet on
  21 * their device under nondisclosure agreement, I have been able to write
  22 * this driver for Linux.
  23 *
  24 * The chip used in the device can also be used for EPP and ISA translation
  25 * as well. This driver is only guaranteed to work with the ATAPI
  26 * translation.
  27 *
  28 * See the Kconfig help text for a list of devices known to be supported by
  29 * this driver.
  30 */
  31
  32#include <linux/errno.h>
  33#include <linux/module.h>
  34#include <linux/slab.h>
  35#include <linux/cdrom.h>
  36
  37#include <scsi/scsi.h>
  38#include <scsi/scsi_cmnd.h>
  39
  40#include "usb.h"
  41#include "transport.h"
  42#include "protocol.h"
  43#include "debug.h"
  44#include "scsiglue.h"
  45
  46#define DRV_NAME "ums-usbat"
  47
  48MODULE_DESCRIPTION("Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable");
  49MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>, Robert Baruch <autophile@starband.net>");
  50MODULE_LICENSE("GPL");
  51
  52/* Supported device types */
  53#define USBAT_DEV_HP8200        0x01
  54#define USBAT_DEV_FLASH         0x02
  55
  56#define USBAT_EPP_PORT          0x10
  57#define USBAT_EPP_REGISTER      0x30
  58#define USBAT_ATA               0x40
  59#define USBAT_ISA               0x50
  60
  61/* Commands (need to be logically OR'd with an access type */
  62#define USBAT_CMD_READ_REG              0x00
  63#define USBAT_CMD_WRITE_REG             0x01
  64#define USBAT_CMD_READ_BLOCK    0x02
  65#define USBAT_CMD_WRITE_BLOCK   0x03
  66#define USBAT_CMD_COND_READ_BLOCK       0x04
  67#define USBAT_CMD_COND_WRITE_BLOCK      0x05
  68#define USBAT_CMD_WRITE_REGS    0x07
  69
  70/* Commands (these don't need an access type) */
  71#define USBAT_CMD_EXEC_CMD      0x80
  72#define USBAT_CMD_SET_FEAT      0x81
  73#define USBAT_CMD_UIO           0x82
  74
  75/* Methods of accessing UIO register */
  76#define USBAT_UIO_READ  1
  77#define USBAT_UIO_WRITE 0
  78
  79/* Qualifier bits */
  80#define USBAT_QUAL_FCQ  0x20    /* full compare */
  81#define USBAT_QUAL_ALQ  0x10    /* auto load subcount */
  82
  83/* USBAT Flash Media status types */
  84#define USBAT_FLASH_MEDIA_NONE  0
  85#define USBAT_FLASH_MEDIA_CF    1
  86
  87/* USBAT Flash Media change types */
  88#define USBAT_FLASH_MEDIA_SAME  0
  89#define USBAT_FLASH_MEDIA_CHANGED       1
  90
  91/* USBAT ATA registers */
  92#define USBAT_ATA_DATA      0x10  /* read/write data (R/W) */
  93#define USBAT_ATA_FEATURES  0x11  /* set features (W) */
  94#define USBAT_ATA_ERROR     0x11  /* error (R) */
  95#define USBAT_ATA_SECCNT    0x12  /* sector count (R/W) */
  96#define USBAT_ATA_SECNUM    0x13  /* sector number (R/W) */
  97#define USBAT_ATA_LBA_ME    0x14  /* cylinder low (R/W) */
  98#define USBAT_ATA_LBA_HI    0x15  /* cylinder high (R/W) */
  99#define USBAT_ATA_DEVICE    0x16  /* head/device selection (R/W) */
 100#define USBAT_ATA_STATUS    0x17  /* device status (R) */
 101#define USBAT_ATA_CMD       0x17  /* device command (W) */
 102#define USBAT_ATA_ALTSTATUS 0x0E  /* status (no clear IRQ) (R) */
 103
 104/* USBAT User I/O Data registers */
 105#define USBAT_UIO_EPAD          0x80 /* Enable Peripheral Control Signals */
 106#define USBAT_UIO_CDT           0x40 /* Card Detect (Read Only) */
 107                                     /* CDT = ACKD & !UI1 & !UI0 */
 108#define USBAT_UIO_1             0x20 /* I/O 1 */
 109#define USBAT_UIO_0             0x10 /* I/O 0 */
 110#define USBAT_UIO_EPP_ATA       0x08 /* 1=EPP mode, 0=ATA mode */
 111#define USBAT_UIO_UI1           0x04 /* Input 1 */
 112#define USBAT_UIO_UI0           0x02 /* Input 0 */
 113#define USBAT_UIO_INTR_ACK      0x01 /* Interrupt (ATA/ISA)/Acknowledge (EPP) */
 114
 115/* USBAT User I/O Enable registers */
 116#define USBAT_UIO_DRVRST        0x80 /* Reset Peripheral */
 117#define USBAT_UIO_ACKD          0x40 /* Enable Card Detect */
 118#define USBAT_UIO_OE1           0x20 /* I/O 1 set=output/clr=input */
 119                                     /* If ACKD=1, set OE1 to 1 also. */
 120#define USBAT_UIO_OE0           0x10 /* I/O 0 set=output/clr=input */
 121#define USBAT_UIO_ADPRST        0x01 /* Reset SCM chip */
 122
 123/* USBAT Features */
 124#define USBAT_FEAT_ETEN 0x80    /* External trigger enable */
 125#define USBAT_FEAT_U1   0x08
 126#define USBAT_FEAT_U0   0x04
 127#define USBAT_FEAT_ET1  0x02
 128#define USBAT_FEAT_ET2  0x01
 129
 130struct usbat_info {
 131        int devicetype;
 132
 133        /* Used for Flash readers only */
 134        unsigned long sectors;     /* total sector count */
 135        unsigned long ssize;       /* sector size in bytes */
 136
 137        unsigned char sense_key;
 138        unsigned long sense_asc;   /* additional sense code */
 139        unsigned long sense_ascq;  /* additional sense code qualifier */
 140};
 141
 142#define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
 143#define LSB_of(s) ((s)&0xFF)
 144#define MSB_of(s) ((s)>>8)
 145
 146static int transferred = 0;
 147
 148static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us);
 149static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us);
 150
 151static int init_usbat_cd(struct us_data *us);
 152static int init_usbat_flash(struct us_data *us);
 153
 154
 155/*
 156 * The table of devices
 157 */
 158#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
 159                    vendorName, productName, useProtocol, useTransport, \
 160                    initFunction, flags) \
 161{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
 162  .driver_info = (flags) }
 163
 164static struct usb_device_id usbat_usb_ids[] = {
 165#       include "unusual_usbat.h"
 166        { }             /* Terminating entry */
 167};
 168MODULE_DEVICE_TABLE(usb, usbat_usb_ids);
 169
 170#undef UNUSUAL_DEV
 171
 172/*
 173 * The flags table
 174 */
 175#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
 176                    vendor_name, product_name, use_protocol, use_transport, \
 177                    init_function, Flags) \
 178{ \
 179        .vendorName = vendor_name,      \
 180        .productName = product_name,    \
 181        .useProtocol = use_protocol,    \
 182        .useTransport = use_transport,  \
 183        .initFunction = init_function,  \
 184}
 185
 186static struct us_unusual_dev usbat_unusual_dev_list[] = {
 187#       include "unusual_usbat.h"
 188        { }             /* Terminating entry */
 189};
 190
 191#undef UNUSUAL_DEV
 192
 193/*
 194 * Convenience function to produce an ATA read/write sectors command
 195 * Use cmd=0x20 for read, cmd=0x30 for write
 196 */
 197static void usbat_pack_ata_sector_cmd(unsigned char *buf,
 198                                        unsigned char thistime,
 199                                        u32 sector, unsigned char cmd)
 200{
 201        buf[0] = 0;
 202        buf[1] = thistime;
 203        buf[2] = sector & 0xFF;
 204        buf[3] = (sector >>  8) & 0xFF;
 205        buf[4] = (sector >> 16) & 0xFF;
 206        buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
 207        buf[6] = cmd;
 208}
 209
 210/*
 211 * Convenience function to get the device type (flash or hp8200)
 212 */
 213static int usbat_get_device_type(struct us_data *us)
 214{
 215        return ((struct usbat_info*)us->extra)->devicetype;
 216}
 217
 218/*
 219 * Read a register from the device
 220 */
 221static int usbat_read(struct us_data *us,
 222                      unsigned char access,
 223                      unsigned char reg,
 224                      unsigned char *content)
 225{
 226        return usb_stor_ctrl_transfer(us,
 227                us->recv_ctrl_pipe,
 228                access | USBAT_CMD_READ_REG,
 229                0xC0,
 230                (u16)reg,
 231                0,
 232                content,
 233                1);
 234}
 235
 236/*
 237 * Write to a register on the device
 238 */
 239static int usbat_write(struct us_data *us,
 240                       unsigned char access,
 241                       unsigned char reg,
 242                       unsigned char content)
 243{
 244        return usb_stor_ctrl_transfer(us,
 245                us->send_ctrl_pipe,
 246                access | USBAT_CMD_WRITE_REG,
 247                0x40,
 248                short_pack(reg, content),
 249                0,
 250                NULL,
 251                0);
 252}
 253
 254/*
 255 * Convenience function to perform a bulk read
 256 */
 257static int usbat_bulk_read(struct us_data *us,
 258                           void* buf,
 259                           unsigned int len,
 260                           int use_sg)
 261{
 262        if (len == 0)
 263                return USB_STOR_XFER_GOOD;
 264
 265        usb_stor_dbg(us, "len = %d\n", len);
 266        return usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe, buf, len, use_sg, NULL);
 267}
 268
 269/*
 270 * Convenience function to perform a bulk write
 271 */
 272static int usbat_bulk_write(struct us_data *us,
 273                            void* buf,
 274                            unsigned int len,
 275                            int use_sg)
 276{
 277        if (len == 0)
 278                return USB_STOR_XFER_GOOD;
 279
 280        usb_stor_dbg(us, "len = %d\n", len);
 281        return usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe, buf, len, use_sg, NULL);
 282}
 283
 284/*
 285 * Some USBAT-specific commands can only be executed over a command transport
 286 * This transport allows one (len=8) or two (len=16) vendor-specific commands
 287 * to be executed.
 288 */
 289static int usbat_execute_command(struct us_data *us,
 290                                                                 unsigned char *commands,
 291                                                                 unsigned int len)
 292{
 293        return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
 294                                                                  USBAT_CMD_EXEC_CMD, 0x40, 0, 0,
 295                                                                  commands, len);
 296}
 297
 298/*
 299 * Read the status register
 300 */
 301static int usbat_get_status(struct us_data *us, unsigned char *status)
 302{
 303        int rc;
 304        rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status);
 305
 306        usb_stor_dbg(us, "0x%02X\n", *status);
 307        return rc;
 308}
 309
 310/*
 311 * Check the device status
 312 */
 313static int usbat_check_status(struct us_data *us)
 314{
 315        unsigned char *reply = us->iobuf;
 316        int rc;
 317
 318        rc = usbat_get_status(us, reply);
 319        if (rc != USB_STOR_XFER_GOOD)
 320                return USB_STOR_TRANSPORT_FAILED;
 321
 322        /* error/check condition (0x51 is ok) */
 323        if (*reply & 0x01 && *reply != 0x51)
 324                return USB_STOR_TRANSPORT_FAILED;
 325
 326        /* device fault */
 327        if (*reply & 0x20)
 328                return USB_STOR_TRANSPORT_FAILED;
 329
 330        return USB_STOR_TRANSPORT_GOOD;
 331}
 332
 333/*
 334 * Stores critical information in internal registers in preparation for the execution
 335 * of a conditional usbat_read_blocks or usbat_write_blocks call.
 336 */
 337static int usbat_set_shuttle_features(struct us_data *us,
 338                                      unsigned char external_trigger,
 339                                      unsigned char epp_control,
 340                                      unsigned char mask_byte,
 341                                      unsigned char test_pattern,
 342                                      unsigned char subcountH,
 343                                      unsigned char subcountL)
 344{
 345        unsigned char *command = us->iobuf;
 346
 347        command[0] = 0x40;
 348        command[1] = USBAT_CMD_SET_FEAT;
 349
 350        /*
 351         * The only bit relevant to ATA access is bit 6
 352         * which defines 8 bit data access (set) or 16 bit (unset)
 353         */
 354        command[2] = epp_control;
 355
 356        /*
 357         * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1,
 358         * ET1 and ET2 define an external event to be checked for on event of a
 359         * _read_blocks or _write_blocks operation. The read/write will not take
 360         * place unless the defined trigger signal is active.
 361         */
 362        command[3] = external_trigger;
 363
 364        /*
 365         * The resultant byte of the mask operation (see mask_byte) is compared for
 366         * equivalence with this test pattern. If equal, the read/write will take
 367         * place.
 368         */
 369        command[4] = test_pattern;
 370
 371        /*
 372         * This value is logically ANDed with the status register field specified
 373         * in the read/write command.
 374         */
 375        command[5] = mask_byte;
 376
 377        /*
 378         * If ALQ is set in the qualifier, this field contains the address of the
 379         * registers where the byte count should be read for transferring the data.
 380         * If ALQ is not set, then this field contains the number of bytes to be
 381         * transferred.
 382         */
 383        command[6] = subcountL;
 384        command[7] = subcountH;
 385
 386        return usbat_execute_command(us, command, 8);
 387}
 388
 389/*
 390 * Block, waiting for an ATA device to become not busy or to report
 391 * an error condition.
 392 */
 393static int usbat_wait_not_busy(struct us_data *us, int minutes)
 394{
 395        int i;
 396        int result;
 397        unsigned char *status = us->iobuf;
 398
 399        /*
 400         * Synchronizing cache on a CDR could take a heck of a long time,
 401         * but probably not more than 10 minutes or so. On the other hand,
 402         * doing a full blank on a CDRW at speed 1 will take about 75
 403         * minutes!
 404         */
 405
 406        for (i=0; i<1200+minutes*60; i++) {
 407
 408                result = usbat_get_status(us, status);
 409
 410                if (result!=USB_STOR_XFER_GOOD)
 411                        return USB_STOR_TRANSPORT_ERROR;
 412                if (*status & 0x01) { /* check condition */
 413                        result = usbat_read(us, USBAT_ATA, 0x10, status);
 414                        return USB_STOR_TRANSPORT_FAILED;
 415                }
 416                if (*status & 0x20) /* device fault */
 417                        return USB_STOR_TRANSPORT_FAILED;
 418
 419                if ((*status & 0x80)==0x00) { /* not busy */
 420                        usb_stor_dbg(us, "Waited not busy for %d steps\n", i);
 421                        return USB_STOR_TRANSPORT_GOOD;
 422                }
 423
 424                if (i<500)
 425                        msleep(10); /* 5 seconds */
 426                else if (i<700)
 427                        msleep(50); /* 10 seconds */
 428                else if (i<1200)
 429                        msleep(100); /* 50 seconds */
 430                else
 431                        msleep(1000); /* X minutes */
 432        }
 433
 434        usb_stor_dbg(us, "Waited not busy for %d minutes, timing out\n",
 435                     minutes);
 436        return USB_STOR_TRANSPORT_FAILED;
 437}
 438
 439/*
 440 * Read block data from the data register
 441 */
 442static int usbat_read_block(struct us_data *us,
 443                            void* buf,
 444                            unsigned short len,
 445                            int use_sg)
 446{
 447        int result;
 448        unsigned char *command = us->iobuf;
 449
 450        if (!len)
 451                return USB_STOR_TRANSPORT_GOOD;
 452
 453        command[0] = 0xC0;
 454        command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK;
 455        command[2] = USBAT_ATA_DATA;
 456        command[3] = 0;
 457        command[4] = 0;
 458        command[5] = 0;
 459        command[6] = LSB_of(len);
 460        command[7] = MSB_of(len);
 461
 462        result = usbat_execute_command(us, command, 8);
 463        if (result != USB_STOR_XFER_GOOD)
 464                return USB_STOR_TRANSPORT_ERROR;
 465
 466        result = usbat_bulk_read(us, buf, len, use_sg);
 467        return (result == USB_STOR_XFER_GOOD ?
 468                        USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR);
 469}
 470
 471/*
 472 * Write block data via the data register
 473 */
 474static int usbat_write_block(struct us_data *us,
 475                             unsigned char access,
 476                             void* buf,
 477                             unsigned short len,
 478                             int minutes,
 479                             int use_sg)
 480{
 481        int result;
 482        unsigned char *command = us->iobuf;
 483
 484        if (!len)
 485                return USB_STOR_TRANSPORT_GOOD;
 486
 487        command[0] = 0x40;
 488        command[1] = access | USBAT_CMD_WRITE_BLOCK;
 489        command[2] = USBAT_ATA_DATA;
 490        command[3] = 0;
 491        command[4] = 0;
 492        command[5] = 0;
 493        command[6] = LSB_of(len);
 494        command[7] = MSB_of(len);
 495
 496        result = usbat_execute_command(us, command, 8);
 497
 498        if (result != USB_STOR_XFER_GOOD)
 499                return USB_STOR_TRANSPORT_ERROR;
 500
 501        result = usbat_bulk_write(us, buf, len, use_sg);
 502        if (result != USB_STOR_XFER_GOOD)
 503                return USB_STOR_TRANSPORT_ERROR;
 504
 505        return usbat_wait_not_busy(us, minutes);
 506}
 507
 508/*
 509 * Process read and write requests
 510 */
 511static int usbat_hp8200e_rw_block_test(struct us_data *us,
 512                                       unsigned char access,
 513                                       unsigned char *registers,
 514                                       unsigned char *data_out,
 515                                       unsigned short num_registers,
 516                                       unsigned char data_reg,
 517                                       unsigned char status_reg,
 518                                       unsigned char timeout,
 519                                       unsigned char qualifier,
 520                                       int direction,
 521                                       void *buf,
 522                                       unsigned short len,
 523                                       int use_sg,
 524                                       int minutes)
 525{
 526        int result;
 527        unsigned int pipe = (direction == DMA_FROM_DEVICE) ?
 528                        us->recv_bulk_pipe : us->send_bulk_pipe;
 529
 530        unsigned char *command = us->iobuf;
 531        int i, j;
 532        int cmdlen;
 533        unsigned char *data = us->iobuf;
 534        unsigned char *status = us->iobuf;
 535
 536        BUG_ON(num_registers > US_IOBUF_SIZE/2);
 537
 538        for (i=0; i<20; i++) {
 539
 540                /*
 541                 * The first time we send the full command, which consists
 542                 * of downloading the SCSI command followed by downloading
 543                 * the data via a write-and-test.  Any other time we only
 544                 * send the command to download the data -- the SCSI command
 545                 * is still 'active' in some sense in the device.
 546                 * 
 547                 * We're only going to try sending the data 10 times. After
 548                 * that, we just return a failure.
 549                 */
 550
 551                if (i==0) {
 552                        cmdlen = 16;
 553                        /*
 554                         * Write to multiple registers
 555                         * Not really sure the 0x07, 0x17, 0xfc, 0xe7 is
 556                         * necessary here, but that's what came out of the
 557                         * trace every single time.
 558                         */
 559                        command[0] = 0x40;
 560                        command[1] = access | USBAT_CMD_WRITE_REGS;
 561                        command[2] = 0x07;
 562                        command[3] = 0x17;
 563                        command[4] = 0xFC;
 564                        command[5] = 0xE7;
 565                        command[6] = LSB_of(num_registers*2);
 566                        command[7] = MSB_of(num_registers*2);
 567                } else
 568                        cmdlen = 8;
 569
 570                /* Conditionally read or write blocks */
 571                command[cmdlen-8] = (direction==DMA_TO_DEVICE ? 0x40 : 0xC0);
 572                command[cmdlen-7] = access |
 573                                (direction==DMA_TO_DEVICE ?
 574                                 USBAT_CMD_COND_WRITE_BLOCK : USBAT_CMD_COND_READ_BLOCK);
 575                command[cmdlen-6] = data_reg;
 576                command[cmdlen-5] = status_reg;
 577                command[cmdlen-4] = timeout;
 578                command[cmdlen-3] = qualifier;
 579                command[cmdlen-2] = LSB_of(len);
 580                command[cmdlen-1] = MSB_of(len);
 581
 582                result = usbat_execute_command(us, command, cmdlen);
 583
 584                if (result != USB_STOR_XFER_GOOD)
 585                        return USB_STOR_TRANSPORT_ERROR;
 586
 587                if (i==0) {
 588
 589                        for (j=0; j<num_registers; j++) {
 590                                data[j<<1] = registers[j];
 591                                data[1+(j<<1)] = data_out[j];
 592                        }
 593
 594                        result = usbat_bulk_write(us, data, num_registers*2, 0);
 595                        if (result != USB_STOR_XFER_GOOD)
 596                                return USB_STOR_TRANSPORT_ERROR;
 597
 598                }
 599
 600                result = usb_stor_bulk_transfer_sg(us,
 601                        pipe, buf, len, use_sg, NULL);
 602
 603                /*
 604                 * If we get a stall on the bulk download, we'll retry
 605                 * the bulk download -- but not the SCSI command because
 606                 * in some sense the SCSI command is still 'active' and
 607                 * waiting for the data. Don't ask me why this should be;
 608                 * I'm only following what the Windoze driver did.
 609                 *
 610                 * Note that a stall for the test-and-read/write command means
 611                 * that the test failed. In this case we're testing to make
 612                 * sure that the device is error-free
 613                 * (i.e. bit 0 -- CHK -- of status is 0). The most likely
 614                 * hypothesis is that the USBAT chip somehow knows what
 615                 * the device will accept, but doesn't give the device any
 616                 * data until all data is received. Thus, the device would
 617                 * still be waiting for the first byte of data if a stall
 618                 * occurs, even if the stall implies that some data was
 619                 * transferred.
 620                 */
 621
 622                if (result == USB_STOR_XFER_SHORT ||
 623                                result == USB_STOR_XFER_STALLED) {
 624
 625                        /*
 626                         * If we're reading and we stalled, then clear
 627                         * the bulk output pipe only the first time.
 628                         */
 629
 630                        if (direction==DMA_FROM_DEVICE && i==0) {
 631                                if (usb_stor_clear_halt(us,
 632                                                us->send_bulk_pipe) < 0)
 633                                        return USB_STOR_TRANSPORT_ERROR;
 634                        }
 635
 636                        /*
 637                         * Read status: is the device angry, or just busy?
 638                         */
 639
 640                        result = usbat_read(us, USBAT_ATA, 
 641                                direction==DMA_TO_DEVICE ?
 642                                        USBAT_ATA_STATUS : USBAT_ATA_ALTSTATUS,
 643                                status);
 644
 645                        if (result!=USB_STOR_XFER_GOOD)
 646                                return USB_STOR_TRANSPORT_ERROR;
 647                        if (*status & 0x01) /* check condition */
 648                                return USB_STOR_TRANSPORT_FAILED;
 649                        if (*status & 0x20) /* device fault */
 650                                return USB_STOR_TRANSPORT_FAILED;
 651
 652                        usb_stor_dbg(us, "Redoing %s\n",
 653                                     direction == DMA_TO_DEVICE
 654                                     ? "write" : "read");
 655
 656                } else if (result != USB_STOR_XFER_GOOD)
 657                        return USB_STOR_TRANSPORT_ERROR;
 658                else
 659                        return usbat_wait_not_busy(us, minutes);
 660
 661        }
 662
 663        usb_stor_dbg(us, "Bummer! %s bulk data 20 times failed\n",
 664                     direction == DMA_TO_DEVICE ? "Writing" : "Reading");
 665
 666        return USB_STOR_TRANSPORT_FAILED;
 667}
 668
 669/*
 670 * Write to multiple registers:
 671 * Allows us to write specific data to any registers. The data to be written
 672 * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN
 673 * which gets sent through bulk out.
 674 * Not designed for large transfers of data!
 675 */
 676static int usbat_multiple_write(struct us_data *us,
 677                                unsigned char *registers,
 678                                unsigned char *data_out,
 679                                unsigned short num_registers)
 680{
 681        int i, result;
 682        unsigned char *data = us->iobuf;
 683        unsigned char *command = us->iobuf;
 684
 685        BUG_ON(num_registers > US_IOBUF_SIZE/2);
 686
 687        /* Write to multiple registers, ATA access */
 688        command[0] = 0x40;
 689        command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS;
 690
 691        /* No relevance */
 692        command[2] = 0;
 693        command[3] = 0;
 694        command[4] = 0;
 695        command[5] = 0;
 696
 697        /* Number of bytes to be transferred (incl. addresses and data) */
 698        command[6] = LSB_of(num_registers*2);
 699        command[7] = MSB_of(num_registers*2);
 700
 701        /* The setup command */
 702        result = usbat_execute_command(us, command, 8);
 703        if (result != USB_STOR_XFER_GOOD)
 704                return USB_STOR_TRANSPORT_ERROR;
 705
 706        /* Create the reg/data, reg/data sequence */
 707        for (i=0; i<num_registers; i++) {
 708                data[i<<1] = registers[i];
 709                data[1+(i<<1)] = data_out[i];
 710        }
 711
 712        /* Send the data */
 713        result = usbat_bulk_write(us, data, num_registers*2, 0);
 714        if (result != USB_STOR_XFER_GOOD)
 715                return USB_STOR_TRANSPORT_ERROR;
 716
 717        if (usbat_get_device_type(us) == USBAT_DEV_HP8200)
 718                return usbat_wait_not_busy(us, 0);
 719        else
 720                return USB_STOR_TRANSPORT_GOOD;
 721}
 722
 723/*
 724 * Conditionally read blocks from device:
 725 * Allows us to read blocks from a specific data register, based upon the
 726 * condition that a status register can be successfully masked with a status
 727 * qualifier. If this condition is not initially met, the read will wait
 728 * up until a maximum amount of time has elapsed, as specified by timeout.
 729 * The read will start when the condition is met, otherwise the command aborts.
 730 *
 731 * The qualifier defined here is not the value that is masked, it defines
 732 * conditions for the write to take place. The actual masked qualifier (and
 733 * other related details) are defined beforehand with _set_shuttle_features().
 734 */
 735static int usbat_read_blocks(struct us_data *us,
 736                             void* buffer,
 737                             int len,
 738                             int use_sg)
 739{
 740        int result;
 741        unsigned char *command = us->iobuf;
 742
 743        command[0] = 0xC0;
 744        command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK;
 745        command[2] = USBAT_ATA_DATA;
 746        command[3] = USBAT_ATA_STATUS;
 747        command[4] = 0xFD; /* Timeout (ms); */
 748        command[5] = USBAT_QUAL_FCQ;
 749        command[6] = LSB_of(len);
 750        command[7] = MSB_of(len);
 751
 752        /* Multiple block read setup command */
 753        result = usbat_execute_command(us, command, 8);
 754        if (result != USB_STOR_XFER_GOOD)
 755                return USB_STOR_TRANSPORT_FAILED;
 756        
 757        /* Read the blocks we just asked for */
 758        result = usbat_bulk_read(us, buffer, len, use_sg);
 759        if (result != USB_STOR_XFER_GOOD)
 760                return USB_STOR_TRANSPORT_FAILED;
 761
 762        return USB_STOR_TRANSPORT_GOOD;
 763}
 764
 765/*
 766 * Conditionally write blocks to device:
 767 * Allows us to write blocks to a specific data register, based upon the
 768 * condition that a status register can be successfully masked with a status
 769 * qualifier. If this condition is not initially met, the write will wait
 770 * up until a maximum amount of time has elapsed, as specified by timeout.
 771 * The read will start when the condition is met, otherwise the command aborts.
 772 *
 773 * The qualifier defined here is not the value that is masked, it defines
 774 * conditions for the write to take place. The actual masked qualifier (and
 775 * other related details) are defined beforehand with _set_shuttle_features().
 776 */
 777static int usbat_write_blocks(struct us_data *us,
 778                              void* buffer,
 779                              int len,
 780                              int use_sg)
 781{
 782        int result;
 783        unsigned char *command = us->iobuf;
 784
 785        command[0] = 0x40;
 786        command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK;
 787        command[2] = USBAT_ATA_DATA;
 788        command[3] = USBAT_ATA_STATUS;
 789        command[4] = 0xFD; /* Timeout (ms) */
 790        command[5] = USBAT_QUAL_FCQ;
 791        command[6] = LSB_of(len);
 792        command[7] = MSB_of(len);
 793
 794        /* Multiple block write setup command */
 795        result = usbat_execute_command(us, command, 8);
 796        if (result != USB_STOR_XFER_GOOD)
 797                return USB_STOR_TRANSPORT_FAILED;
 798        
 799        /* Write the data */
 800        result = usbat_bulk_write(us, buffer, len, use_sg);
 801        if (result != USB_STOR_XFER_GOOD)
 802                return USB_STOR_TRANSPORT_FAILED;
 803
 804        return USB_STOR_TRANSPORT_GOOD;
 805}
 806
 807/*
 808 * Read the User IO register
 809 */
 810static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags)
 811{
 812        int result;
 813
 814        result = usb_stor_ctrl_transfer(us,
 815                us->recv_ctrl_pipe,
 816                USBAT_CMD_UIO,
 817                0xC0,
 818                0,
 819                0,
 820                data_flags,
 821                USBAT_UIO_READ);
 822
 823        usb_stor_dbg(us, "UIO register reads %02X\n", *data_flags);
 824
 825        return result;
 826}
 827
 828/*
 829 * Write to the User IO register
 830 */
 831static int usbat_write_user_io(struct us_data *us,
 832                               unsigned char enable_flags,
 833                               unsigned char data_flags)
 834{
 835        return usb_stor_ctrl_transfer(us,
 836                us->send_ctrl_pipe,
 837                USBAT_CMD_UIO,
 838                0x40,
 839                short_pack(enable_flags, data_flags),
 840                0,
 841                NULL,
 842                USBAT_UIO_WRITE);
 843}
 844
 845/*
 846 * Reset the device
 847 * Often needed on media change.
 848 */
 849static int usbat_device_reset(struct us_data *us)
 850{
 851        int rc;
 852
 853        /*
 854         * Reset peripheral, enable peripheral control signals
 855         * (bring reset signal up)
 856         */
 857        rc = usbat_write_user_io(us,
 858                                                         USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
 859                                                         USBAT_UIO_EPAD | USBAT_UIO_1);
 860        if (rc != USB_STOR_XFER_GOOD)
 861                return USB_STOR_TRANSPORT_ERROR;
 862                        
 863        /*
 864         * Enable peripheral control signals
 865         * (bring reset signal down)
 866         */
 867        rc = usbat_write_user_io(us,
 868                                                         USBAT_UIO_OE1  | USBAT_UIO_OE0,
 869                                                         USBAT_UIO_EPAD | USBAT_UIO_1);
 870        if (rc != USB_STOR_XFER_GOOD)
 871                return USB_STOR_TRANSPORT_ERROR;
 872
 873        return USB_STOR_TRANSPORT_GOOD;
 874}
 875
 876/*
 877 * Enable card detect
 878 */
 879static int usbat_device_enable_cdt(struct us_data *us)
 880{
 881        int rc;
 882
 883        /* Enable peripheral control signals and card detect */
 884        rc = usbat_write_user_io(us,
 885                                                         USBAT_UIO_ACKD | USBAT_UIO_OE1  | USBAT_UIO_OE0,
 886                                                         USBAT_UIO_EPAD | USBAT_UIO_1);
 887        if (rc != USB_STOR_XFER_GOOD)
 888                return USB_STOR_TRANSPORT_ERROR;
 889
 890        return USB_STOR_TRANSPORT_GOOD;
 891}
 892
 893/*
 894 * Determine if media is present.
 895 */
 896static int usbat_flash_check_media_present(struct us_data *us,
 897                                           unsigned char *uio)
 898{
 899        if (*uio & USBAT_UIO_UI0) {
 900                usb_stor_dbg(us, "no media detected\n");
 901                return USBAT_FLASH_MEDIA_NONE;
 902        }
 903
 904        return USBAT_FLASH_MEDIA_CF;
 905}
 906
 907/*
 908 * Determine if media has changed since last operation
 909 */
 910static int usbat_flash_check_media_changed(struct us_data *us,
 911                                           unsigned char *uio)
 912{
 913        if (*uio & USBAT_UIO_0) {
 914                usb_stor_dbg(us, "media change detected\n");
 915                return USBAT_FLASH_MEDIA_CHANGED;
 916        }
 917
 918        return USBAT_FLASH_MEDIA_SAME;
 919}
 920
 921/*
 922 * Check for media change / no media and handle the situation appropriately
 923 */
 924static int usbat_flash_check_media(struct us_data *us,
 925                                   struct usbat_info *info)
 926{
 927        int rc;
 928        unsigned char *uio = us->iobuf;
 929
 930        rc = usbat_read_user_io(us, uio);
 931        if (rc != USB_STOR_XFER_GOOD)
 932                return USB_STOR_TRANSPORT_ERROR;
 933
 934        /* Check for media existence */
 935        rc = usbat_flash_check_media_present(us, uio);
 936        if (rc == USBAT_FLASH_MEDIA_NONE) {
 937                info->sense_key = 0x02;
 938                info->sense_asc = 0x3A;
 939                info->sense_ascq = 0x00;
 940                return USB_STOR_TRANSPORT_FAILED;
 941        }
 942
 943        /* Check for media change */
 944        rc = usbat_flash_check_media_changed(us, uio);
 945        if (rc == USBAT_FLASH_MEDIA_CHANGED) {
 946
 947                /* Reset and re-enable card detect */
 948                rc = usbat_device_reset(us);
 949                if (rc != USB_STOR_TRANSPORT_GOOD)
 950                        return rc;
 951                rc = usbat_device_enable_cdt(us);
 952                if (rc != USB_STOR_TRANSPORT_GOOD)
 953                        return rc;
 954
 955                msleep(50);
 956
 957                rc = usbat_read_user_io(us, uio);
 958                if (rc != USB_STOR_XFER_GOOD)
 959                        return USB_STOR_TRANSPORT_ERROR;
 960                
 961                info->sense_key = UNIT_ATTENTION;
 962                info->sense_asc = 0x28;
 963                info->sense_ascq = 0x00;
 964                return USB_STOR_TRANSPORT_FAILED;
 965        }
 966
 967        return USB_STOR_TRANSPORT_GOOD;
 968}
 969
 970/*
 971 * Determine whether we are controlling a flash-based reader/writer,
 972 * or a HP8200-based CD drive.
 973 * Sets transport functions as appropriate.
 974 */
 975static int usbat_identify_device(struct us_data *us,
 976                                 struct usbat_info *info)
 977{
 978        int rc;
 979        unsigned char status;
 980
 981        if (!us || !info)
 982                return USB_STOR_TRANSPORT_ERROR;
 983
 984        rc = usbat_device_reset(us);
 985        if (rc != USB_STOR_TRANSPORT_GOOD)
 986                return rc;
 987        msleep(500);
 988
 989        /*
 990         * In attempt to distinguish between HP CDRW's and Flash readers, we now
 991         * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash
 992         * readers), this command should fail with error. On ATAPI devices (i.e.
 993         * CDROM drives), it should succeed.
 994         */
 995        rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1);
 996        if (rc != USB_STOR_XFER_GOOD)
 997                return USB_STOR_TRANSPORT_ERROR;
 998
 999        rc = usbat_get_status(us, &status);
1000        if (rc != USB_STOR_XFER_GOOD)
1001                return USB_STOR_TRANSPORT_ERROR;
1002
1003        /* Check for error bit, or if the command 'fell through' */
1004        if (status == 0xA1 || !(status & 0x01)) {
1005                /* Device is HP 8200 */
1006                usb_stor_dbg(us, "Detected HP8200 CDRW\n");
1007                info->devicetype = USBAT_DEV_HP8200;
1008        } else {
1009                /* Device is a CompactFlash reader/writer */
1010                usb_stor_dbg(us, "Detected Flash reader/writer\n");
1011                info->devicetype = USBAT_DEV_FLASH;
1012        }
1013
1014        return USB_STOR_TRANSPORT_GOOD;
1015}
1016
1017/*
1018 * Set the transport function based on the device type
1019 */
1020static int usbat_set_transport(struct us_data *us,
1021                               struct usbat_info *info,
1022                               int devicetype)
1023{
1024
1025        if (!info->devicetype)
1026                info->devicetype = devicetype;
1027
1028        if (!info->devicetype)
1029                usbat_identify_device(us, info);
1030
1031        switch (info->devicetype) {
1032        default:
1033                return USB_STOR_TRANSPORT_ERROR;
1034
1035        case  USBAT_DEV_HP8200:
1036                us->transport = usbat_hp8200e_transport;
1037                break;
1038
1039        case USBAT_DEV_FLASH:
1040                us->transport = usbat_flash_transport;
1041                break;
1042        }
1043
1044        return 0;
1045}
1046
1047/*
1048 * Read the media capacity
1049 */
1050static int usbat_flash_get_sector_count(struct us_data *us,
1051                                        struct usbat_info *info)
1052{
1053        unsigned char registers[3] = {
1054                USBAT_ATA_SECCNT,
1055                USBAT_ATA_DEVICE,
1056                USBAT_ATA_CMD,
1057        };
1058        unsigned char  command[3] = { 0x01, 0xA0, 0xEC };
1059        unsigned char *reply;
1060        unsigned char status;
1061        int rc;
1062
1063        if (!us || !info)
1064                return USB_STOR_TRANSPORT_ERROR;
1065
1066        reply = kmalloc(512, GFP_NOIO);
1067        if (!reply)
1068                return USB_STOR_TRANSPORT_ERROR;
1069
1070        /* ATA command : IDENTIFY DEVICE */
1071        rc = usbat_multiple_write(us, registers, command, 3);
1072        if (rc != USB_STOR_XFER_GOOD) {
1073                usb_stor_dbg(us, "Gah! identify_device failed\n");
1074                rc = USB_STOR_TRANSPORT_ERROR;
1075                goto leave;
1076        }
1077
1078        /* Read device status */
1079        if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) {
1080                rc = USB_STOR_TRANSPORT_ERROR;
1081                goto leave;
1082        }
1083
1084        msleep(100);
1085
1086        /* Read the device identification data */
1087        rc = usbat_read_block(us, reply, 512, 0);
1088        if (rc != USB_STOR_TRANSPORT_GOOD)
1089                goto leave;
1090
1091        info->sectors = ((u32)(reply[117]) << 24) |
1092                ((u32)(reply[116]) << 16) |
1093                ((u32)(reply[115]) <<  8) |
1094                ((u32)(reply[114])      );
1095
1096        rc = USB_STOR_TRANSPORT_GOOD;
1097
1098 leave:
1099        kfree(reply);
1100        return rc;
1101}
1102
1103/*
1104 * Read data from device
1105 */
1106static int usbat_flash_read_data(struct us_data *us,
1107                                                                 struct usbat_info *info,
1108                                                                 u32 sector,
1109                                                                 u32 sectors)
1110{
1111        unsigned char registers[7] = {
1112                USBAT_ATA_FEATURES,
1113                USBAT_ATA_SECCNT,
1114                USBAT_ATA_SECNUM,
1115                USBAT_ATA_LBA_ME,
1116                USBAT_ATA_LBA_HI,
1117                USBAT_ATA_DEVICE,
1118                USBAT_ATA_STATUS,
1119        };
1120        unsigned char command[7];
1121        unsigned char *buffer;
1122        unsigned char  thistime;
1123        unsigned int totallen, alloclen;
1124        int len, result;
1125        unsigned int sg_offset = 0;
1126        struct scatterlist *sg = NULL;
1127
1128        result = usbat_flash_check_media(us, info);
1129        if (result != USB_STOR_TRANSPORT_GOOD)
1130                return result;
1131
1132        /*
1133         * we're working in LBA mode.  according to the ATA spec,
1134         * we can support up to 28-bit addressing.  I don't know if Jumpshot
1135         * supports beyond 24-bit addressing.  It's kind of hard to test
1136         * since it requires > 8GB CF card.
1137         */
1138
1139        if (sector > 0x0FFFFFFF)
1140                return USB_STOR_TRANSPORT_ERROR;
1141
1142        totallen = sectors * info->ssize;
1143
1144        /*
1145         * Since we don't read more than 64 KB at a time, we have to create
1146         * a bounce buffer and move the data a piece at a time between the
1147         * bounce buffer and the actual transfer buffer.
1148         */
1149
1150        alloclen = min(totallen, 65536u);
1151        buffer = kmalloc(alloclen, GFP_NOIO);
1152        if (buffer == NULL)
1153                return USB_STOR_TRANSPORT_ERROR;
1154
1155        do {
1156                /*
1157                 * loop, never allocate or transfer more than 64k at once
1158                 * (min(128k, 255*info->ssize) is the real limit)
1159                 */
1160                len = min(totallen, alloclen);
1161                thistime = (len / info->ssize) & 0xff;
1162 
1163                /* ATA command 0x20 (READ SECTORS) */
1164                usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
1165
1166                /* Write/execute ATA read command */
1167                result = usbat_multiple_write(us, registers, command, 7);
1168                if (result != USB_STOR_TRANSPORT_GOOD)
1169                        goto leave;
1170
1171                /* Read the data we just requested */
1172                result = usbat_read_blocks(us, buffer, len, 0);
1173                if (result != USB_STOR_TRANSPORT_GOOD)
1174                        goto leave;
1175         
1176                usb_stor_dbg(us, "%d bytes\n", len);
1177        
1178                /* Store the data in the transfer buffer */
1179                usb_stor_access_xfer_buf(buffer, len, us->srb,
1180                                         &sg, &sg_offset, TO_XFER_BUF);
1181
1182                sector += thistime;
1183                totallen -= len;
1184        } while (totallen > 0);
1185
1186        kfree(buffer);
1187        return USB_STOR_TRANSPORT_GOOD;
1188
1189leave:
1190        kfree(buffer);
1191        return USB_STOR_TRANSPORT_ERROR;
1192}
1193
1194/*
1195 * Write data to device
1196 */
1197static int usbat_flash_write_data(struct us_data *us,
1198                                                                  struct usbat_info *info,
1199                                                                  u32 sector,
1200                                                                  u32 sectors)
1201{
1202        unsigned char registers[7] = {
1203                USBAT_ATA_FEATURES,
1204                USBAT_ATA_SECCNT,
1205                USBAT_ATA_SECNUM,
1206                USBAT_ATA_LBA_ME,
1207                USBAT_ATA_LBA_HI,
1208                USBAT_ATA_DEVICE,
1209                USBAT_ATA_STATUS,
1210        };
1211        unsigned char command[7];
1212        unsigned char *buffer;
1213        unsigned char  thistime;
1214        unsigned int totallen, alloclen;
1215        int len, result;
1216        unsigned int sg_offset = 0;
1217        struct scatterlist *sg = NULL;
1218
1219        result = usbat_flash_check_media(us, info);
1220        if (result != USB_STOR_TRANSPORT_GOOD)
1221                return result;
1222
1223        /*
1224         * we're working in LBA mode.  according to the ATA spec,
1225         * we can support up to 28-bit addressing.  I don't know if the device
1226         * supports beyond 24-bit addressing.  It's kind of hard to test
1227         * since it requires > 8GB media.
1228         */
1229
1230        if (sector > 0x0FFFFFFF)
1231                return USB_STOR_TRANSPORT_ERROR;
1232
1233        totallen = sectors * info->ssize;
1234
1235        /*
1236         * Since we don't write more than 64 KB at a time, we have to create
1237         * a bounce buffer and move the data a piece at a time between the
1238         * bounce buffer and the actual transfer buffer.
1239         */
1240
1241        alloclen = min(totallen, 65536u);
1242        buffer = kmalloc(alloclen, GFP_NOIO);
1243        if (buffer == NULL)
1244                return USB_STOR_TRANSPORT_ERROR;
1245
1246        do {
1247                /*
1248                 * loop, never allocate or transfer more than 64k at once
1249                 * (min(128k, 255*info->ssize) is the real limit)
1250                 */
1251                len = min(totallen, alloclen);
1252                thistime = (len / info->ssize) & 0xff;
1253
1254                /* Get the data from the transfer buffer */
1255                usb_stor_access_xfer_buf(buffer, len, us->srb,
1256                                         &sg, &sg_offset, FROM_XFER_BUF);
1257
1258                /* ATA command 0x30 (WRITE SECTORS) */
1259                usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
1260
1261                /* Write/execute ATA write command */
1262                result = usbat_multiple_write(us, registers, command, 7);
1263                if (result != USB_STOR_TRANSPORT_GOOD)
1264                        goto leave;
1265
1266                /* Write the data */
1267                result = usbat_write_blocks(us, buffer, len, 0);
1268                if (result != USB_STOR_TRANSPORT_GOOD)
1269                        goto leave;
1270
1271                sector += thistime;
1272                totallen -= len;
1273        } while (totallen > 0);
1274
1275        kfree(buffer);
1276        return result;
1277
1278leave:
1279        kfree(buffer);
1280        return USB_STOR_TRANSPORT_ERROR;
1281}
1282
1283/*
1284 * Squeeze a potentially huge (> 65535 byte) read10 command into
1285 * a little ( <= 65535 byte) ATAPI pipe
1286 */
1287static int usbat_hp8200e_handle_read10(struct us_data *us,
1288                                       unsigned char *registers,
1289                                       unsigned char *data,
1290                                       struct scsi_cmnd *srb)
1291{
1292        int result = USB_STOR_TRANSPORT_GOOD;
1293        unsigned char *buffer;
1294        unsigned int len;
1295        unsigned int sector;
1296        unsigned int sg_offset = 0;
1297        struct scatterlist *sg = NULL;
1298
1299        usb_stor_dbg(us, "transfersize %d\n", srb->transfersize);
1300
1301        if (scsi_bufflen(srb) < 0x10000) {
1302
1303                result = usbat_hp8200e_rw_block_test(us, USBAT_ATA, 
1304                        registers, data, 19,
1305                        USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1306                        (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1307                        DMA_FROM_DEVICE,
1308                        scsi_sglist(srb),
1309                        scsi_bufflen(srb), scsi_sg_count(srb), 1);
1310
1311                return result;
1312        }
1313
1314        /*
1315         * Since we're requesting more data than we can handle in
1316         * a single read command (max is 64k-1), we will perform
1317         * multiple reads, but each read must be in multiples of
1318         * a sector.  Luckily the sector size is in srb->transfersize
1319         * (see linux/drivers/scsi/sr.c).
1320         */
1321
1322        if (data[7+0] == GPCMD_READ_CD) {
1323                len = short_pack(data[7+9], data[7+8]);
1324                len <<= 16;
1325                len |= data[7+7];
1326                usb_stor_dbg(us, "GPCMD_READ_CD: len %d\n", len);
1327                srb->transfersize = scsi_bufflen(srb)/len;
1328        }
1329
1330        if (!srb->transfersize)  {
1331                srb->transfersize = 2048; /* A guess */
1332                usb_stor_dbg(us, "transfersize 0, forcing %d\n",
1333                             srb->transfersize);
1334        }
1335
1336        /*
1337         * Since we only read in one block at a time, we have to create
1338         * a bounce buffer and move the data a piece at a time between the
1339         * bounce buffer and the actual transfer buffer.
1340         */
1341
1342        len = (65535/srb->transfersize) * srb->transfersize;
1343        usb_stor_dbg(us, "Max read is %d bytes\n", len);
1344        len = min(len, scsi_bufflen(srb));
1345        buffer = kmalloc(len, GFP_NOIO);
1346        if (buffer == NULL) /* bloody hell! */
1347                return USB_STOR_TRANSPORT_FAILED;
1348        sector = short_pack(data[7+3], data[7+2]);
1349        sector <<= 16;
1350        sector |= short_pack(data[7+5], data[7+4]);
1351        transferred = 0;
1352
1353        while (transferred != scsi_bufflen(srb)) {
1354
1355                if (len > scsi_bufflen(srb) - transferred)
1356                        len = scsi_bufflen(srb) - transferred;
1357
1358                data[3] = len&0xFF;       /* (cylL) = expected length (L) */
1359                data[4] = (len>>8)&0xFF;  /* (cylH) = expected length (H) */
1360
1361                /* Fix up the SCSI command sector and num sectors */
1362
1363                data[7+2] = MSB_of(sector>>16); /* SCSI command sector */
1364                data[7+3] = LSB_of(sector>>16);
1365                data[7+4] = MSB_of(sector&0xFFFF);
1366                data[7+5] = LSB_of(sector&0xFFFF);
1367                if (data[7+0] == GPCMD_READ_CD)
1368                        data[7+6] = 0;
1369                data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */
1370                data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */
1371
1372                result = usbat_hp8200e_rw_block_test(us, USBAT_ATA, 
1373                        registers, data, 19,
1374                        USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD, 
1375                        (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1376                        DMA_FROM_DEVICE,
1377                        buffer,
1378                        len, 0, 1);
1379
1380                if (result != USB_STOR_TRANSPORT_GOOD)
1381                        break;
1382
1383                /* Store the data in the transfer buffer */
1384                usb_stor_access_xfer_buf(buffer, len, srb,
1385                                 &sg, &sg_offset, TO_XFER_BUF);
1386
1387                /* Update the amount transferred and the sector number */
1388
1389                transferred += len;
1390                sector += len / srb->transfersize;
1391
1392        } /* while transferred != scsi_bufflen(srb) */
1393
1394        kfree(buffer);
1395        return result;
1396}
1397
1398static int usbat_select_and_test_registers(struct us_data *us)
1399{
1400        int selector;
1401        unsigned char *status = us->iobuf;
1402
1403        /* try device = master, then device = slave. */
1404        for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
1405                if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) !=
1406                                USB_STOR_XFER_GOOD)
1407                        return USB_STOR_TRANSPORT_ERROR;
1408
1409                if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) != 
1410                                USB_STOR_XFER_GOOD)
1411                        return USB_STOR_TRANSPORT_ERROR;
1412
1413                if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) != 
1414                                USB_STOR_XFER_GOOD)
1415                        return USB_STOR_TRANSPORT_ERROR;
1416
1417                if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != 
1418                                USB_STOR_XFER_GOOD)
1419                        return USB_STOR_TRANSPORT_ERROR;
1420
1421                if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) != 
1422                                USB_STOR_XFER_GOOD)
1423                        return USB_STOR_TRANSPORT_ERROR;
1424
1425                if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) != 
1426                                USB_STOR_XFER_GOOD)
1427                        return USB_STOR_TRANSPORT_ERROR;
1428
1429                if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) != 
1430                                USB_STOR_XFER_GOOD)
1431                        return USB_STOR_TRANSPORT_ERROR;
1432
1433                if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != 
1434                                USB_STOR_XFER_GOOD)
1435                        return USB_STOR_TRANSPORT_ERROR;
1436
1437                if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != 
1438                                USB_STOR_XFER_GOOD)
1439                        return USB_STOR_TRANSPORT_ERROR;
1440        }
1441
1442        return USB_STOR_TRANSPORT_GOOD;
1443}
1444
1445/*
1446 * Initialize the USBAT processor and the storage device
1447 */
1448static int init_usbat(struct us_data *us, int devicetype)
1449{
1450        int rc;
1451        struct usbat_info *info;
1452        unsigned char subcountH = USBAT_ATA_LBA_HI;
1453        unsigned char subcountL = USBAT_ATA_LBA_ME;
1454        unsigned char *status = us->iobuf;
1455
1456        us->extra = kzalloc(sizeof(struct usbat_info), GFP_NOIO);
1457        if (!us->extra)
1458                return 1;
1459
1460        info = (struct usbat_info *) (us->extra);
1461
1462        /* Enable peripheral control signals */
1463        rc = usbat_write_user_io(us,
1464                                 USBAT_UIO_OE1 | USBAT_UIO_OE0,
1465                                 USBAT_UIO_EPAD | USBAT_UIO_1);
1466        if (rc != USB_STOR_XFER_GOOD)
1467                return USB_STOR_TRANSPORT_ERROR;
1468
1469        usb_stor_dbg(us, "INIT 1\n");
1470
1471        msleep(2000);
1472
1473        rc = usbat_read_user_io(us, status);
1474        if (rc != USB_STOR_TRANSPORT_GOOD)
1475                return rc;
1476
1477        usb_stor_dbg(us, "INIT 2\n");
1478
1479        rc = usbat_read_user_io(us, status);
1480        if (rc != USB_STOR_XFER_GOOD)
1481                return USB_STOR_TRANSPORT_ERROR;
1482
1483        rc = usbat_read_user_io(us, status);
1484        if (rc != USB_STOR_XFER_GOOD)
1485                return USB_STOR_TRANSPORT_ERROR;
1486
1487        usb_stor_dbg(us, "INIT 3\n");
1488
1489        rc = usbat_select_and_test_registers(us);
1490        if (rc != USB_STOR_TRANSPORT_GOOD)
1491                return rc;
1492
1493        usb_stor_dbg(us, "INIT 4\n");
1494
1495        rc = usbat_read_user_io(us, status);
1496        if (rc != USB_STOR_XFER_GOOD)
1497                return USB_STOR_TRANSPORT_ERROR;
1498
1499        usb_stor_dbg(us, "INIT 5\n");
1500
1501        /* Enable peripheral control signals and card detect */
1502        rc = usbat_device_enable_cdt(us);
1503        if (rc != USB_STOR_TRANSPORT_GOOD)
1504                return rc;
1505
1506        usb_stor_dbg(us, "INIT 6\n");
1507
1508        rc = usbat_read_user_io(us, status);
1509        if (rc != USB_STOR_XFER_GOOD)
1510                return USB_STOR_TRANSPORT_ERROR;
1511
1512        usb_stor_dbg(us, "INIT 7\n");
1513
1514        msleep(1400);
1515
1516        rc = usbat_read_user_io(us, status);
1517        if (rc != USB_STOR_XFER_GOOD)
1518                return USB_STOR_TRANSPORT_ERROR;
1519
1520        usb_stor_dbg(us, "INIT 8\n");
1521
1522        rc = usbat_select_and_test_registers(us);
1523        if (rc != USB_STOR_TRANSPORT_GOOD)
1524                return rc;
1525
1526        usb_stor_dbg(us, "INIT 9\n");
1527
1528        /* At this point, we need to detect which device we are using */
1529        if (usbat_set_transport(us, info, devicetype))
1530                return USB_STOR_TRANSPORT_ERROR;
1531
1532        usb_stor_dbg(us, "INIT 10\n");
1533
1534        if (usbat_get_device_type(us) == USBAT_DEV_FLASH) { 
1535                subcountH = 0x02;
1536                subcountL = 0x00;
1537        }
1538        rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1),
1539                                                                        0x00, 0x88, 0x08, subcountH, subcountL);
1540        if (rc != USB_STOR_XFER_GOOD)
1541                return USB_STOR_TRANSPORT_ERROR;
1542
1543        usb_stor_dbg(us, "INIT 11\n");
1544
1545        return USB_STOR_TRANSPORT_GOOD;
1546}
1547
1548/*
1549 * Transport for the HP 8200e
1550 */
1551static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us)
1552{
1553        int result;
1554        unsigned char *status = us->iobuf;
1555        unsigned char registers[32];
1556        unsigned char data[32];
1557        unsigned int len;
1558        int i;
1559
1560        len = scsi_bufflen(srb);
1561
1562        /*
1563         * Send A0 (ATA PACKET COMMAND).
1564         * Note: I guess we're never going to get any of the ATA
1565         * commands... just ATA Packet Commands.
1566         */
1567
1568        registers[0] = USBAT_ATA_FEATURES;
1569        registers[1] = USBAT_ATA_SECCNT;
1570        registers[2] = USBAT_ATA_SECNUM;
1571        registers[3] = USBAT_ATA_LBA_ME;
1572        registers[4] = USBAT_ATA_LBA_HI;
1573        registers[5] = USBAT_ATA_DEVICE;
1574        registers[6] = USBAT_ATA_CMD;
1575        data[0] = 0x00;
1576        data[1] = 0x00;
1577        data[2] = 0x00;
1578        data[3] = len&0xFF;             /* (cylL) = expected length (L) */
1579        data[4] = (len>>8)&0xFF;        /* (cylH) = expected length (H) */
1580        data[5] = 0xB0;                 /* (device sel) = slave */
1581        data[6] = 0xA0;                 /* (command) = ATA PACKET COMMAND */
1582
1583        for (i=7; i<19; i++) {
1584                registers[i] = 0x10;
1585                data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
1586        }
1587
1588        result = usbat_get_status(us, status);
1589        usb_stor_dbg(us, "Status = %02X\n", *status);
1590        if (result != USB_STOR_XFER_GOOD)
1591                return USB_STOR_TRANSPORT_ERROR;
1592        if (srb->cmnd[0] == TEST_UNIT_READY)
1593                transferred = 0;
1594
1595        if (srb->sc_data_direction == DMA_TO_DEVICE) {
1596
1597                result = usbat_hp8200e_rw_block_test(us, USBAT_ATA, 
1598                        registers, data, 19,
1599                        USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD,
1600                        (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ),
1601                        DMA_TO_DEVICE,
1602                        scsi_sglist(srb),
1603                        len, scsi_sg_count(srb), 10);
1604
1605                if (result == USB_STOR_TRANSPORT_GOOD) {
1606                        transferred += len;
1607                        usb_stor_dbg(us, "Wrote %08X bytes\n", transferred);
1608                }
1609
1610                return result;
1611
1612        } else if (srb->cmnd[0] == READ_10 ||
1613                   srb->cmnd[0] == GPCMD_READ_CD) {
1614
1615                return usbat_hp8200e_handle_read10(us, registers, data, srb);
1616
1617        }
1618
1619        if (len > 0xFFFF) {
1620                usb_stor_dbg(us, "Error: len = %08X... what do I do now?\n",
1621                             len);
1622                return USB_STOR_TRANSPORT_ERROR;
1623        }
1624
1625        result = usbat_multiple_write(us, registers, data, 7);
1626
1627        if (result != USB_STOR_TRANSPORT_GOOD)
1628                return result;
1629
1630        /*
1631         * Write the 12-byte command header.
1632         *
1633         * If the command is BLANK then set the timer for 75 minutes.
1634         * Otherwise set it for 10 minutes.
1635         *
1636         * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
1637         * AT SPEED 4 IS UNRELIABLE!!!
1638         */
1639
1640        result = usbat_write_block(us, USBAT_ATA, srb->cmnd, 12,
1641                                   srb->cmnd[0] == GPCMD_BLANK ? 75 : 10, 0);
1642
1643        if (result != USB_STOR_TRANSPORT_GOOD)
1644                return result;
1645
1646        /* If there is response data to be read in then do it here. */
1647
1648        if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) {
1649
1650                /* How many bytes to read in? Check cylL register */
1651
1652                if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != 
1653                        USB_STOR_XFER_GOOD) {
1654                        return USB_STOR_TRANSPORT_ERROR;
1655                }
1656
1657                if (len > 0xFF) { /* need to read cylH also */
1658                        len = *status;
1659                        if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) !=
1660                                    USB_STOR_XFER_GOOD) {
1661                                return USB_STOR_TRANSPORT_ERROR;
1662                        }
1663                        len += ((unsigned int) *status)<<8;
1664                }
1665                else
1666                        len = *status;
1667
1668
1669                result = usbat_read_block(us, scsi_sglist(srb), len,
1670                                                           scsi_sg_count(srb));
1671        }
1672
1673        return result;
1674}
1675
1676/*
1677 * Transport for USBAT02-based CompactFlash and similar storage devices
1678 */
1679static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us)
1680{
1681        int rc;
1682        struct usbat_info *info = (struct usbat_info *) (us->extra);
1683        unsigned long block, blocks;
1684        unsigned char *ptr = us->iobuf;
1685        static unsigned char inquiry_response[36] = {
1686                0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1687        };
1688
1689        if (srb->cmnd[0] == INQUIRY) {
1690                usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
1691                memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1692                fill_inquiry_response(us, ptr, 36);
1693                return USB_STOR_TRANSPORT_GOOD;
1694        }
1695
1696        if (srb->cmnd[0] == READ_CAPACITY) {
1697                rc = usbat_flash_check_media(us, info);
1698                if (rc != USB_STOR_TRANSPORT_GOOD)
1699                        return rc;
1700
1701                rc = usbat_flash_get_sector_count(us, info);
1702                if (rc != USB_STOR_TRANSPORT_GOOD)
1703                        return rc;
1704
1705                /* hard coded 512 byte sectors as per ATA spec */
1706                info->ssize = 0x200;
1707                usb_stor_dbg(us, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1708                             info->sectors, info->ssize);
1709
1710                /*
1711                 * build the reply
1712                 * note: must return the sector number of the last sector,
1713                 * *not* the total number of sectors
1714                 */
1715                ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
1716                ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
1717                usb_stor_set_xfer_buf(ptr, 8, srb);
1718
1719                return USB_STOR_TRANSPORT_GOOD;
1720        }
1721
1722        if (srb->cmnd[0] == MODE_SELECT_10) {
1723                usb_stor_dbg(us, "Gah! MODE_SELECT_10\n");
1724                return USB_STOR_TRANSPORT_ERROR;
1725        }
1726
1727        if (srb->cmnd[0] == READ_10) {
1728                block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1729                                ((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
1730
1731                blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1732
1733                usb_stor_dbg(us, "READ_10: read block 0x%04lx  count %ld\n",
1734                             block, blocks);
1735                return usbat_flash_read_data(us, info, block, blocks);
1736        }
1737
1738        if (srb->cmnd[0] == READ_12) {
1739                /*
1740                 * I don't think we'll ever see a READ_12 but support it anyway
1741                 */
1742                block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1743                        ((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
1744
1745                blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1746                         ((u32)(srb->cmnd[8]) <<  8) | ((u32)(srb->cmnd[9]));
1747
1748                usb_stor_dbg(us, "READ_12: read block 0x%04lx  count %ld\n",
1749                             block, blocks);
1750                return usbat_flash_read_data(us, info, block, blocks);
1751        }
1752
1753        if (srb->cmnd[0] == WRITE_10) {
1754                block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1755                        ((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
1756
1757                blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
1758
1759                usb_stor_dbg(us, "WRITE_10: write block 0x%04lx  count %ld\n",
1760                             block, blocks);
1761                return usbat_flash_write_data(us, info, block, blocks);
1762        }
1763
1764        if (srb->cmnd[0] == WRITE_12) {
1765                /*
1766                 * I don't think we'll ever see a WRITE_12 but support it anyway
1767                 */
1768                block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
1769                        ((u32)(srb->cmnd[4]) <<  8) | ((u32)(srb->cmnd[5]));
1770
1771                blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
1772                         ((u32)(srb->cmnd[8]) <<  8) | ((u32)(srb->cmnd[9]));
1773
1774                usb_stor_dbg(us, "WRITE_12: write block 0x%04lx  count %ld\n",
1775                             block, blocks);
1776                return usbat_flash_write_data(us, info, block, blocks);
1777        }
1778
1779
1780        if (srb->cmnd[0] == TEST_UNIT_READY) {
1781                usb_stor_dbg(us, "TEST_UNIT_READY\n");
1782
1783                rc = usbat_flash_check_media(us, info);
1784                if (rc != USB_STOR_TRANSPORT_GOOD)
1785                        return rc;
1786
1787                return usbat_check_status(us);
1788        }
1789
1790        if (srb->cmnd[0] == REQUEST_SENSE) {
1791                usb_stor_dbg(us, "REQUEST_SENSE\n");
1792
1793                memset(ptr, 0, 18);
1794                ptr[0] = 0xF0;
1795                ptr[2] = info->sense_key;
1796                ptr[7] = 11;
1797                ptr[12] = info->sense_asc;
1798                ptr[13] = info->sense_ascq;
1799                usb_stor_set_xfer_buf(ptr, 18, srb);
1800
1801                return USB_STOR_TRANSPORT_GOOD;
1802        }
1803
1804        if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1805                /*
1806                 * sure.  whatever.  not like we can stop the user from popping
1807                 * the media out of the device (no locking doors, etc)
1808                 */
1809                return USB_STOR_TRANSPORT_GOOD;
1810        }
1811
1812        usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
1813                     srb->cmnd[0], srb->cmnd[0]);
1814        info->sense_key = 0x05;
1815        info->sense_asc = 0x20;
1816        info->sense_ascq = 0x00;
1817        return USB_STOR_TRANSPORT_FAILED;
1818}
1819
1820static int init_usbat_cd(struct us_data *us)
1821{
1822        return init_usbat(us, USBAT_DEV_HP8200);
1823}
1824
1825static int init_usbat_flash(struct us_data *us)
1826{
1827        return init_usbat(us, USBAT_DEV_FLASH);
1828}
1829
1830static struct scsi_host_template usbat_host_template;
1831
1832static int usbat_probe(struct usb_interface *intf,
1833                         const struct usb_device_id *id)
1834{
1835        struct us_data *us;
1836        int result;
1837
1838        result = usb_stor_probe1(&us, intf, id,
1839                        (id - usbat_usb_ids) + usbat_unusual_dev_list,
1840                        &usbat_host_template);
1841        if (result)
1842                return result;
1843
1844        /*
1845         * The actual transport will be determined later by the
1846         * initialization routine; this is just a placeholder.
1847         */
1848        us->transport_name = "Shuttle USBAT";
1849        us->transport = usbat_flash_transport;
1850        us->transport_reset = usb_stor_CB_reset;
1851        us->max_lun = 0;
1852
1853        result = usb_stor_probe2(us);
1854        return result;
1855}
1856
1857static struct usb_driver usbat_driver = {
1858        .name =         DRV_NAME,
1859        .probe =        usbat_probe,
1860        .disconnect =   usb_stor_disconnect,
1861        .suspend =      usb_stor_suspend,
1862        .resume =       usb_stor_resume,
1863        .reset_resume = usb_stor_reset_resume,
1864        .pre_reset =    usb_stor_pre_reset,
1865        .post_reset =   usb_stor_post_reset,
1866        .id_table =     usbat_usb_ids,
1867        .soft_unbind =  1,
1868        .no_dynamic_id = 1,
1869};
1870
1871module_usb_stor_driver(usbat_driver, usbat_host_template, DRV_NAME);
1872