linux/drivers/usb/storage/isd200.c
<<
>>
Prefs
   1/* Transport & Protocol Driver for In-System Design, Inc. ISD200 ASIC
   2 *
   3 * $Id: isd200.c,v 1.16 2002/04/22 03:39:43 mdharm Exp $
   4 *
   5 * Current development and maintenance:
   6 *   (C) 2001-2002 Björn Stenberg (bjorn@haxx.se)
   7 *
   8 * Developed with the assistance of:
   9 *   (C) 2002 Alan Stern <stern@rowland.org>
  10 *
  11 * Initial work:
  12 *   (C) 2000 In-System Design, Inc. (support@in-system.com)
  13 *
  14 * The ISD200 ASIC does not natively support ATA devices.  The chip
  15 * does implement an interface, the ATA Command Block (ATACB) which provides
  16 * a means of passing ATA commands and ATA register accesses to a device.
  17 *
  18 * This program is free software; you can redistribute it and/or modify it
  19 * under the terms of the GNU General Public License as published by the
  20 * Free Software Foundation; either version 2, or (at your option) any
  21 * later version.
  22 *
  23 * This program is distributed in the hope that it will be useful, but
  24 * WITHOUT ANY WARRANTY; without even the implied warranty of
  25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  26 * General Public License for more details.
  27 *
  28 * You should have received a copy of the GNU General Public License along
  29 * with this program; if not, write to the Free Software Foundation, Inc.,
  30 * 675 Mass Ave, Cambridge, MA 02139, USA.
  31 *
  32 * History:
  33 *
  34 *  2002-10-19: Removed the specialized transfer routines.
  35 *              (Alan Stern <stern@rowland.harvard.edu>)
  36 *  2001-02-24: Removed lots of duplicate code and simplified the structure.
  37 *            (bjorn@haxx.se)
  38 *  2002-01-16: Fixed endianness bug so it works on the ppc arch.
  39 *            (Luc Saillard <luc@saillard.org>)
  40 *  2002-01-17: All bitfields removed.
  41 *            (bjorn@haxx.se)
  42 */
  43
  44
  45/* Include files */
  46
  47#include <linux/jiffies.h>
  48#include <linux/errno.h>
  49#include <linux/slab.h>
  50#include <linux/hdreg.h>
  51#include <linux/ide.h>
  52
  53#include <scsi/scsi.h>
  54#include <scsi/scsi_cmnd.h>
  55#include <scsi/scsi_device.h>
  56
  57#include "usb.h"
  58#include "transport.h"
  59#include "protocol.h"
  60#include "debug.h"
  61#include "scsiglue.h"
  62#include "isd200.h"
  63
  64
  65/* Timeout defines (in Seconds) */
  66
  67#define ISD200_ENUM_BSY_TIMEOUT         35
  68#define ISD200_ENUM_DETECT_TIMEOUT      30
  69#define ISD200_DEFAULT_TIMEOUT          30
  70
  71/* device flags */
  72#define DF_ATA_DEVICE           0x0001
  73#define DF_MEDIA_STATUS_ENABLED 0x0002
  74#define DF_REMOVABLE_MEDIA      0x0004
  75
  76/* capability bit definitions */
  77#define CAPABILITY_DMA          0x01
  78#define CAPABILITY_LBA          0x02
  79
  80/* command_setX bit definitions */
  81#define COMMANDSET_REMOVABLE    0x02
  82#define COMMANDSET_MEDIA_STATUS 0x10
  83
  84/* ATA Vendor Specific defines */
  85#define ATA_ADDRESS_DEVHEAD_STD      0xa0
  86#define ATA_ADDRESS_DEVHEAD_LBA_MODE 0x40    
  87#define ATA_ADDRESS_DEVHEAD_SLAVE    0x10
  88
  89/* Action Select bits */
  90#define ACTION_SELECT_0      0x01
  91#define ACTION_SELECT_1      0x02
  92#define ACTION_SELECT_2      0x04
  93#define ACTION_SELECT_3      0x08
  94#define ACTION_SELECT_4      0x10
  95#define ACTION_SELECT_5      0x20
  96#define ACTION_SELECT_6      0x40
  97#define ACTION_SELECT_7      0x80
  98
  99/* Register Select bits */
 100#define REG_ALTERNATE_STATUS    0x01
 101#define REG_DEVICE_CONTROL      0x01
 102#define REG_ERROR               0x02
 103#define REG_FEATURES            0x02
 104#define REG_SECTOR_COUNT        0x04
 105#define REG_SECTOR_NUMBER       0x08
 106#define REG_CYLINDER_LOW        0x10
 107#define REG_CYLINDER_HIGH       0x20
 108#define REG_DEVICE_HEAD         0x40
 109#define REG_STATUS              0x80
 110#define REG_COMMAND             0x80
 111
 112/* ATA error definitions not in <linux/hdreg.h> */
 113#define ATA_ERROR_MEDIA_CHANGE          0x20
 114
 115/* ATA command definitions not in <linux/hdreg.h> */
 116#define ATA_COMMAND_GET_MEDIA_STATUS    0xDA
 117#define ATA_COMMAND_MEDIA_EJECT         0xED
 118
 119/* ATA drive control definitions */
 120#define ATA_DC_DISABLE_INTERRUPTS       0x02
 121#define ATA_DC_RESET_CONTROLLER         0x04
 122#define ATA_DC_REENABLE_CONTROLLER      0x00
 123
 124/*
 125 *  General purpose return codes
 126 */ 
 127
 128#define ISD200_ERROR            -1
 129#define ISD200_GOOD              0
 130
 131/*
 132 * Transport return codes
 133 */
 134
 135#define ISD200_TRANSPORT_GOOD       0   /* Transport good, command good     */
 136#define ISD200_TRANSPORT_FAILED     1   /* Transport good, command failed   */
 137#define ISD200_TRANSPORT_ERROR      2   /* Transport bad (i.e. device dead) */
 138
 139/* driver action codes */
 140#define ACTION_READ_STATUS      0
 141#define ACTION_RESET            1
 142#define ACTION_REENABLE         2
 143#define ACTION_SOFT_RESET       3
 144#define ACTION_ENUM             4
 145#define ACTION_IDENTIFY         5
 146
 147
 148/*
 149 * ata_cdb struct
 150 */
 151
 152
 153union ata_cdb {
 154        struct {
 155                unsigned char SignatureByte0;
 156                unsigned char SignatureByte1;
 157                unsigned char ActionSelect;
 158                unsigned char RegisterSelect;
 159                unsigned char TransferBlockSize;
 160                unsigned char WriteData3F6;
 161                unsigned char WriteData1F1;
 162                unsigned char WriteData1F2;
 163                unsigned char WriteData1F3;
 164                unsigned char WriteData1F4;
 165                unsigned char WriteData1F5;
 166                unsigned char WriteData1F6;
 167                unsigned char WriteData1F7;
 168                unsigned char Reserved[3];
 169        } generic;
 170
 171        struct {
 172                unsigned char SignatureByte0;
 173                unsigned char SignatureByte1;
 174                unsigned char ActionSelect;
 175                unsigned char RegisterSelect;
 176                unsigned char TransferBlockSize;
 177                unsigned char AlternateStatusByte;
 178                unsigned char ErrorByte;
 179                unsigned char SectorCountByte;
 180                unsigned char SectorNumberByte;
 181                unsigned char CylinderLowByte;
 182                unsigned char CylinderHighByte;
 183                unsigned char DeviceHeadByte;
 184                unsigned char StatusByte;
 185                unsigned char Reserved[3];
 186        } read;
 187
 188        struct {
 189                unsigned char SignatureByte0;
 190                unsigned char SignatureByte1;
 191                unsigned char ActionSelect;
 192                unsigned char RegisterSelect;
 193                unsigned char TransferBlockSize;
 194                unsigned char DeviceControlByte;
 195                unsigned char FeaturesByte;
 196                unsigned char SectorCountByte;
 197                unsigned char SectorNumberByte;
 198                unsigned char CylinderLowByte;
 199                unsigned char CylinderHighByte;
 200                unsigned char DeviceHeadByte;
 201                unsigned char CommandByte;
 202                unsigned char Reserved[3];
 203        } write;
 204};
 205
 206
 207/*
 208 * Inquiry data structure. This is the data returned from the target
 209 * after it receives an inquiry.
 210 *
 211 * This structure may be extended by the number of bytes specified
 212 * in the field AdditionalLength. The defined size constant only
 213 * includes fields through ProductRevisionLevel.
 214 */
 215
 216/*
 217 * DeviceType field
 218 */
 219#define DIRECT_ACCESS_DEVICE        0x00    /* disks */
 220#define DEVICE_REMOVABLE                0x80
 221
 222struct inquiry_data {
 223        unsigned char DeviceType;
 224        unsigned char DeviceTypeModifier;
 225        unsigned char Versions;
 226        unsigned char Format; 
 227        unsigned char AdditionalLength;
 228        unsigned char Reserved[2];
 229        unsigned char Capability;
 230        unsigned char VendorId[8];
 231        unsigned char ProductId[16];
 232        unsigned char ProductRevisionLevel[4];
 233        unsigned char VendorSpecific[20];
 234        unsigned char Reserved3[40];
 235} __attribute__ ((packed));
 236
 237/*
 238 * INQUIRY data buffer size
 239 */
 240
 241#define INQUIRYDATABUFFERSIZE 36
 242
 243
 244/*
 245 * ISD200 CONFIG data struct
 246 */
 247
 248#define ATACFG_TIMING     0x0f
 249#define ATACFG_ATAPI_RESET     0x10
 250#define ATACFG_MASTER     0x20
 251#define ATACFG_BLOCKSIZE       0xa0
 252
 253#define ATACFGE_LAST_LUN       0x07
 254#define ATACFGE_DESC_OVERRIDE  0x08
 255#define ATACFGE_STATE_SUSPEND  0x10
 256#define ATACFGE_SKIP_BOOT      0x20
 257#define ATACFGE_CONF_DESC2     0x40
 258#define ATACFGE_INIT_STATUS    0x80
 259
 260#define CFG_CAPABILITY_SRST    0x01
 261
 262struct isd200_config {
 263        unsigned char EventNotification;
 264        unsigned char ExternalClock;
 265        unsigned char ATAInitTimeout;
 266        unsigned char ATAConfig;
 267        unsigned char ATAMajorCommand;
 268        unsigned char ATAMinorCommand;
 269        unsigned char ATAExtraConfig;
 270        unsigned char Capability;
 271}__attribute__ ((packed));
 272
 273
 274/*
 275 * ISD200 driver information struct
 276 */
 277
 278struct isd200_info {
 279        struct inquiry_data InquiryData;
 280        struct hd_driveid *id;
 281        struct isd200_config ConfigData;
 282        unsigned char *RegsBuf;
 283        unsigned char ATARegs[8];
 284        unsigned char DeviceHead;
 285        unsigned char DeviceFlags;
 286
 287        /* maximum number of LUNs supported */
 288        unsigned char MaxLUNs;
 289        struct scsi_cmnd srb;
 290};
 291
 292
 293/*
 294 * Read Capacity Data - returned in Big Endian format
 295 */
 296
 297struct read_capacity_data {
 298        __be32 LogicalBlockAddress;
 299        __be32 BytesPerBlock;
 300};
 301
 302/*
 303 * Read Block Limits Data - returned in Big Endian format
 304 * This structure returns the maximum and minimum block
 305 * size for a TAPE device.
 306 */
 307
 308struct read_block_limits {
 309        unsigned char Reserved;
 310        unsigned char BlockMaximumSize[3];
 311        unsigned char BlockMinimumSize[2];
 312};
 313
 314
 315/*
 316 * Sense Data Format
 317 */
 318
 319#define SENSE_ERRCODE      0x7f
 320#define SENSE_ERRCODE_VALID     0x80
 321#define SENSE_FLAG_SENSE_KEY    0x0f
 322#define SENSE_FLAG_BAD_LENGTH   0x20
 323#define SENSE_FLAG_END_OF_MEDIA 0x40
 324#define SENSE_FLAG_FILE_MARK    0x80
 325struct sense_data {
 326        unsigned char ErrorCode;
 327        unsigned char SegmentNumber;
 328        unsigned char Flags;
 329        unsigned char Information[4];
 330        unsigned char AdditionalSenseLength;
 331        unsigned char CommandSpecificInformation[4];
 332        unsigned char AdditionalSenseCode;
 333        unsigned char AdditionalSenseCodeQualifier;
 334        unsigned char FieldReplaceableUnitCode;
 335        unsigned char SenseKeySpecific[3];
 336} __attribute__ ((packed));
 337
 338/*
 339 * Default request sense buffer size
 340 */
 341
 342#define SENSE_BUFFER_SIZE 18
 343
 344/***********************************************************************
 345 * Helper routines
 346 ***********************************************************************/
 347
 348/**************************************************************************
 349 * isd200_build_sense
 350 *                                                                       
 351 *  Builds an artificial sense buffer to report the results of a 
 352 *  failed command.
 353 *                                                                     
 354 * RETURNS:
 355 *    void
 356 */
 357static void isd200_build_sense(struct us_data *us, struct scsi_cmnd *srb)
 358{
 359        struct isd200_info *info = (struct isd200_info *)us->extra;
 360        struct sense_data *buf = (struct sense_data *) &srb->sense_buffer[0];
 361        unsigned char error = info->ATARegs[IDE_ERROR_OFFSET];
 362
 363        if(error & ATA_ERROR_MEDIA_CHANGE) {
 364                buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
 365                buf->AdditionalSenseLength = 0xb;
 366                buf->Flags = UNIT_ATTENTION;
 367                buf->AdditionalSenseCode = 0;
 368                buf->AdditionalSenseCodeQualifier = 0;
 369        } else if(error & MCR_ERR) {
 370                buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
 371                buf->AdditionalSenseLength = 0xb;
 372                buf->Flags =  UNIT_ATTENTION;
 373                buf->AdditionalSenseCode = 0;
 374                buf->AdditionalSenseCodeQualifier = 0;
 375        } else if(error & TRK0_ERR) {
 376                buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
 377                buf->AdditionalSenseLength = 0xb;
 378                buf->Flags =  NOT_READY;
 379                buf->AdditionalSenseCode = 0;
 380                buf->AdditionalSenseCodeQualifier = 0;
 381        } else if(error & ECC_ERR) {
 382                buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
 383                buf->AdditionalSenseLength = 0xb;
 384                buf->Flags =  DATA_PROTECT;
 385                buf->AdditionalSenseCode = 0;
 386                buf->AdditionalSenseCodeQualifier = 0;
 387        } else {
 388                buf->ErrorCode = 0;
 389                buf->AdditionalSenseLength = 0;
 390                buf->Flags =  0;
 391                buf->AdditionalSenseCode = 0;
 392                buf->AdditionalSenseCodeQualifier = 0;
 393        }
 394}
 395
 396
 397/***********************************************************************
 398 * Transport routines
 399 ***********************************************************************/
 400
 401
 402/**************************************************************************
 403 *  isd200_action
 404 *
 405 * Routine for sending commands to the isd200
 406 *
 407 * RETURNS:
 408 *    ISD status code
 409 */
 410static int isd200_action( struct us_data *us, int action, 
 411                          void* pointer, int value )
 412{
 413        union ata_cdb ata;
 414        struct scsi_device srb_dev;
 415        struct isd200_info *info = (struct isd200_info *)us->extra;
 416        struct scsi_cmnd *srb = &info->srb;
 417        int status;
 418
 419        memset(&ata, 0, sizeof(ata));
 420        memset(&srb_dev, 0, sizeof(srb_dev));
 421        srb->device = &srb_dev;
 422        ++srb->serial_number;
 423
 424        ata.generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
 425        ata.generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
 426        ata.generic.TransferBlockSize = 1;
 427
 428        switch ( action ) {
 429        case ACTION_READ_STATUS:
 430                US_DEBUGP("   isd200_action(READ_STATUS)\n");
 431                ata.generic.ActionSelect = ACTION_SELECT_0|ACTION_SELECT_2;
 432                ata.generic.RegisterSelect =
 433                  REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
 434                  REG_STATUS | REG_ERROR;
 435                srb->sc_data_direction = DMA_FROM_DEVICE;
 436                srb->request_buffer = pointer;
 437                srb->request_bufflen = value;
 438                break;
 439
 440        case ACTION_ENUM:
 441                US_DEBUGP("   isd200_action(ENUM,0x%02x)\n",value);
 442                ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
 443                                           ACTION_SELECT_3|ACTION_SELECT_4|
 444                                           ACTION_SELECT_5;
 445                ata.generic.RegisterSelect = REG_DEVICE_HEAD;
 446                ata.write.DeviceHeadByte = value;
 447                srb->sc_data_direction = DMA_NONE;
 448                break;
 449
 450        case ACTION_RESET:
 451                US_DEBUGP("   isd200_action(RESET)\n");
 452                ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
 453                                           ACTION_SELECT_3|ACTION_SELECT_4;
 454                ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
 455                ata.write.DeviceControlByte = ATA_DC_RESET_CONTROLLER;
 456                srb->sc_data_direction = DMA_NONE;
 457                break;
 458
 459        case ACTION_REENABLE:
 460                US_DEBUGP("   isd200_action(REENABLE)\n");
 461                ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
 462                                           ACTION_SELECT_3|ACTION_SELECT_4;
 463                ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
 464                ata.write.DeviceControlByte = ATA_DC_REENABLE_CONTROLLER;
 465                srb->sc_data_direction = DMA_NONE;
 466                break;
 467
 468        case ACTION_SOFT_RESET:
 469                US_DEBUGP("   isd200_action(SOFT_RESET)\n");
 470                ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_5;
 471                ata.generic.RegisterSelect = REG_DEVICE_HEAD | REG_COMMAND;
 472                ata.write.DeviceHeadByte = info->DeviceHead;
 473                ata.write.CommandByte = WIN_SRST;
 474                srb->sc_data_direction = DMA_NONE;
 475                break;
 476
 477        case ACTION_IDENTIFY:
 478                US_DEBUGP("   isd200_action(IDENTIFY)\n");
 479                ata.generic.RegisterSelect = REG_COMMAND;
 480                ata.write.CommandByte = WIN_IDENTIFY;
 481                srb->sc_data_direction = DMA_FROM_DEVICE;
 482                srb->request_buffer = (void *) info->id;
 483                srb->request_bufflen = sizeof(struct hd_driveid);
 484                break;
 485
 486        default:
 487                US_DEBUGP("Error: Undefined action %d\n",action);
 488                break;
 489        }
 490
 491        memcpy(srb->cmnd, &ata, sizeof(ata.generic));
 492        srb->cmd_len = sizeof(ata.generic);
 493        status = usb_stor_Bulk_transport(srb, us);
 494        if (status == USB_STOR_TRANSPORT_GOOD)
 495                status = ISD200_GOOD;
 496        else {
 497                US_DEBUGP("   isd200_action(0x%02x) error: %d\n",action,status);
 498                status = ISD200_ERROR;
 499                /* need to reset device here */
 500        }
 501
 502        return status;
 503}
 504
 505/**************************************************************************
 506 * isd200_read_regs
 507 *                                                                       
 508 * Read ATA Registers
 509 *
 510 * RETURNS:
 511 *    ISD status code
 512 */
 513static int isd200_read_regs( struct us_data *us )
 514{
 515        struct isd200_info *info = (struct isd200_info *)us->extra;
 516        int retStatus = ISD200_GOOD;
 517        int transferStatus;
 518
 519        US_DEBUGP("Entering isd200_IssueATAReadRegs\n");
 520
 521        transferStatus = isd200_action( us, ACTION_READ_STATUS,
 522                                    info->RegsBuf, sizeof(info->ATARegs) );
 523        if (transferStatus != ISD200_TRANSPORT_GOOD) {
 524                US_DEBUGP("   Error reading ATA registers\n");
 525                retStatus = ISD200_ERROR;
 526        } else {
 527                memcpy(info->ATARegs, info->RegsBuf, sizeof(info->ATARegs));
 528                US_DEBUGP("   Got ATA Register[IDE_ERROR_OFFSET] = 0x%x\n", 
 529                          info->ATARegs[IDE_ERROR_OFFSET]);
 530        }
 531
 532        return retStatus;
 533}
 534
 535
 536/**************************************************************************
 537 * Invoke the transport and basic error-handling/recovery methods
 538 *
 539 * This is used by the protocol layers to actually send the message to
 540 * the device and receive the response.
 541 */
 542static void isd200_invoke_transport( struct us_data *us, 
 543                              struct scsi_cmnd *srb, 
 544                              union ata_cdb *ataCdb )
 545{
 546        int need_auto_sense = 0;
 547        int transferStatus;
 548        int result;
 549
 550        /* send the command to the transport layer */
 551        memcpy(srb->cmnd, ataCdb, sizeof(ataCdb->generic));
 552        srb->cmd_len = sizeof(ataCdb->generic);
 553        transferStatus = usb_stor_Bulk_transport(srb, us);
 554
 555        /* if the command gets aborted by the higher layers, we need to
 556         * short-circuit all other processing
 557         */
 558        if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
 559                US_DEBUGP("-- command was aborted\n");
 560                goto Handle_Abort;
 561        }
 562
 563        switch (transferStatus) {
 564
 565        case USB_STOR_TRANSPORT_GOOD:
 566                /* Indicate a good result */
 567                srb->result = SAM_STAT_GOOD;
 568                break;
 569
 570        case USB_STOR_TRANSPORT_NO_SENSE:
 571                US_DEBUGP("-- transport indicates protocol failure\n");
 572                srb->result = SAM_STAT_CHECK_CONDITION;
 573                return;
 574
 575        case USB_STOR_TRANSPORT_FAILED:
 576                US_DEBUGP("-- transport indicates command failure\n");
 577                need_auto_sense = 1;
 578                break;
 579
 580        case USB_STOR_TRANSPORT_ERROR:
 581                US_DEBUGP("-- transport indicates transport error\n");
 582                srb->result = DID_ERROR << 16;
 583                /* Need reset here */
 584                return;
 585    
 586        default:
 587                US_DEBUGP("-- transport indicates unknown error\n");   
 588                srb->result = DID_ERROR << 16;
 589                /* Need reset here */
 590                return;
 591        }
 592
 593        if ((srb->resid > 0) &&
 594            !((srb->cmnd[0] == REQUEST_SENSE) ||
 595              (srb->cmnd[0] == INQUIRY) ||
 596              (srb->cmnd[0] == MODE_SENSE) ||
 597              (srb->cmnd[0] == LOG_SENSE) ||
 598              (srb->cmnd[0] == MODE_SENSE_10))) {
 599                US_DEBUGP("-- unexpectedly short transfer\n");
 600                need_auto_sense = 1;
 601        }
 602
 603        if (need_auto_sense) {
 604                result = isd200_read_regs(us);
 605                if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
 606                        US_DEBUGP("-- auto-sense aborted\n");
 607                        goto Handle_Abort;
 608                }
 609                if (result == ISD200_GOOD) {
 610                        isd200_build_sense(us, srb);
 611                        srb->result = SAM_STAT_CHECK_CONDITION;
 612
 613                        /* If things are really okay, then let's show that */
 614                        if ((srb->sense_buffer[2] & 0xf) == 0x0)
 615                                srb->result = SAM_STAT_GOOD;
 616                } else {
 617                        srb->result = DID_ERROR << 16;
 618                        /* Need reset here */
 619                }
 620        }
 621
 622        /* Regardless of auto-sense, if we _know_ we have an error
 623         * condition, show that in the result code
 624         */
 625        if (transferStatus == USB_STOR_TRANSPORT_FAILED)
 626                srb->result = SAM_STAT_CHECK_CONDITION;
 627        return;
 628
 629        /* abort processing: the bulk-only transport requires a reset
 630         * following an abort */
 631        Handle_Abort:
 632        srb->result = DID_ABORT << 16;
 633
 634        /* permit the reset transfer to take place */
 635        clear_bit(US_FLIDX_ABORTING, &us->flags);
 636        /* Need reset here */
 637}
 638
 639#ifdef CONFIG_USB_STORAGE_DEBUG
 640static void isd200_log_config( struct isd200_info* info )
 641{
 642        US_DEBUGP("      Event Notification: 0x%x\n", 
 643                  info->ConfigData.EventNotification);
 644        US_DEBUGP("      External Clock: 0x%x\n", 
 645                  info->ConfigData.ExternalClock);
 646        US_DEBUGP("      ATA Init Timeout: 0x%x\n", 
 647                  info->ConfigData.ATAInitTimeout);
 648        US_DEBUGP("      ATAPI Command Block Size: 0x%x\n", 
 649                  (info->ConfigData.ATAConfig & ATACFG_BLOCKSIZE) >> 6);
 650        US_DEBUGP("      Master/Slave Selection: 0x%x\n", 
 651                  info->ConfigData.ATAConfig & ATACFG_MASTER);
 652        US_DEBUGP("      ATAPI Reset: 0x%x\n",
 653                  info->ConfigData.ATAConfig & ATACFG_ATAPI_RESET);
 654        US_DEBUGP("      ATA Timing: 0x%x\n",
 655                  info->ConfigData.ATAConfig & ATACFG_TIMING);
 656        US_DEBUGP("      ATA Major Command: 0x%x\n", 
 657                  info->ConfigData.ATAMajorCommand);
 658        US_DEBUGP("      ATA Minor Command: 0x%x\n", 
 659                  info->ConfigData.ATAMinorCommand);
 660        US_DEBUGP("      Init Status: 0x%x\n", 
 661                  info->ConfigData.ATAExtraConfig & ATACFGE_INIT_STATUS);
 662        US_DEBUGP("      Config Descriptor 2: 0x%x\n", 
 663                  info->ConfigData.ATAExtraConfig & ATACFGE_CONF_DESC2);
 664        US_DEBUGP("      Skip Device Boot: 0x%x\n",
 665                  info->ConfigData.ATAExtraConfig & ATACFGE_SKIP_BOOT);
 666        US_DEBUGP("      ATA 3 State Supsend: 0x%x\n",
 667                  info->ConfigData.ATAExtraConfig & ATACFGE_STATE_SUSPEND);
 668        US_DEBUGP("      Descriptor Override: 0x%x\n", 
 669                  info->ConfigData.ATAExtraConfig & ATACFGE_DESC_OVERRIDE);
 670        US_DEBUGP("      Last LUN Identifier: 0x%x\n",
 671                  info->ConfigData.ATAExtraConfig & ATACFGE_LAST_LUN);
 672        US_DEBUGP("      SRST Enable: 0x%x\n", 
 673                  info->ConfigData.ATAExtraConfig & CFG_CAPABILITY_SRST);
 674}
 675#endif
 676
 677/**************************************************************************
 678 * isd200_write_config
 679 *                                                                       
 680 * Write the ISD200 Configuration data
 681 *
 682 * RETURNS:
 683 *    ISD status code
 684 */
 685static int isd200_write_config( struct us_data *us ) 
 686{
 687        struct isd200_info *info = (struct isd200_info *)us->extra;
 688        int retStatus = ISD200_GOOD;
 689        int result;
 690
 691#ifdef CONFIG_USB_STORAGE_DEBUG
 692        US_DEBUGP("Entering isd200_write_config\n");
 693        US_DEBUGP("   Writing the following ISD200 Config Data:\n");
 694        isd200_log_config(info);
 695#endif
 696
 697        /* let's send the command via the control pipe */
 698        result = usb_stor_ctrl_transfer(
 699                us, 
 700                us->send_ctrl_pipe,
 701                0x01, 
 702                USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
 703                0x0000, 
 704                0x0002, 
 705                (void *) &info->ConfigData, 
 706                sizeof(info->ConfigData));
 707
 708        if (result >= 0) {
 709                US_DEBUGP("   ISD200 Config Data was written successfully\n");
 710        } else {
 711                US_DEBUGP("   Request to write ISD200 Config Data failed!\n");
 712                retStatus = ISD200_ERROR;
 713        }
 714
 715        US_DEBUGP("Leaving isd200_write_config %08X\n", retStatus);
 716        return retStatus;
 717}
 718
 719
 720/**************************************************************************
 721 * isd200_read_config
 722 *                                                                       
 723 * Reads the ISD200 Configuration data
 724 *
 725 * RETURNS:
 726 *    ISD status code
 727 */
 728static int isd200_read_config( struct us_data *us ) 
 729{
 730        struct isd200_info *info = (struct isd200_info *)us->extra;
 731        int retStatus = ISD200_GOOD;
 732        int result;
 733
 734        US_DEBUGP("Entering isd200_read_config\n");
 735
 736        /* read the configuration information from ISD200.  Use this to */
 737        /* determine what the special ATA CDB bytes are.                */
 738
 739        result = usb_stor_ctrl_transfer(
 740                us, 
 741                us->recv_ctrl_pipe,
 742                0x02, 
 743                USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
 744                0x0000, 
 745                0x0002, 
 746                (void *) &info->ConfigData, 
 747                sizeof(info->ConfigData));
 748
 749
 750        if (result >= 0) {
 751                US_DEBUGP("   Retrieved the following ISD200 Config Data:\n");
 752#ifdef CONFIG_USB_STORAGE_DEBUG
 753                isd200_log_config(info);
 754#endif
 755        } else {
 756                US_DEBUGP("   Request to get ISD200 Config Data failed!\n");
 757                retStatus = ISD200_ERROR;
 758        }
 759
 760        US_DEBUGP("Leaving isd200_read_config %08X\n", retStatus);
 761        return retStatus;
 762}
 763
 764
 765/**************************************************************************
 766 * isd200_atapi_soft_reset
 767 *                                                                       
 768 * Perform an Atapi Soft Reset on the device
 769 *
 770 * RETURNS:
 771 *    NT status code
 772 */
 773static int isd200_atapi_soft_reset( struct us_data *us ) 
 774{
 775        int retStatus = ISD200_GOOD;
 776        int transferStatus;
 777
 778        US_DEBUGP("Entering isd200_atapi_soft_reset\n");
 779
 780        transferStatus = isd200_action( us, ACTION_SOFT_RESET, NULL, 0 );
 781        if (transferStatus != ISD200_TRANSPORT_GOOD) {
 782                US_DEBUGP("   Error issuing Atapi Soft Reset\n");
 783                retStatus = ISD200_ERROR;
 784        }
 785
 786        US_DEBUGP("Leaving isd200_atapi_soft_reset %08X\n", retStatus);
 787        return retStatus;
 788}
 789
 790
 791/**************************************************************************
 792 * isd200_srst
 793 *                                                                       
 794 * Perform an SRST on the device
 795 *
 796 * RETURNS:
 797 *    ISD status code
 798 */
 799static int isd200_srst( struct us_data *us ) 
 800{
 801        int retStatus = ISD200_GOOD;
 802        int transferStatus;
 803
 804        US_DEBUGP("Entering isd200_SRST\n");
 805
 806        transferStatus = isd200_action( us, ACTION_RESET, NULL, 0 );
 807
 808        /* check to see if this request failed */
 809        if (transferStatus != ISD200_TRANSPORT_GOOD) {
 810                US_DEBUGP("   Error issuing SRST\n");
 811                retStatus = ISD200_ERROR;
 812        } else {
 813                /* delay 10ms to give the drive a chance to see it */
 814                msleep(10);
 815
 816                transferStatus = isd200_action( us, ACTION_REENABLE, NULL, 0 );
 817                if (transferStatus != ISD200_TRANSPORT_GOOD) {
 818                        US_DEBUGP("   Error taking drive out of reset\n");
 819                        retStatus = ISD200_ERROR;
 820                } else {
 821                        /* delay 50ms to give the drive a chance to recover after SRST */
 822                        msleep(50);
 823                }
 824        }
 825
 826        US_DEBUGP("Leaving isd200_srst %08X\n", retStatus);
 827        return retStatus;
 828}
 829
 830
 831/**************************************************************************
 832 * isd200_try_enum
 833 *                                                                       
 834 * Helper function for isd200_manual_enum(). Does ENUM and READ_STATUS
 835 * and tries to analyze the status registers
 836 *
 837 * RETURNS:
 838 *    ISD status code
 839 */
 840static int isd200_try_enum(struct us_data *us, unsigned char master_slave,
 841                           int detect )
 842{
 843        int status = ISD200_GOOD;
 844        unsigned long endTime;
 845        struct isd200_info *info = (struct isd200_info *)us->extra;
 846        unsigned char *regs = info->RegsBuf;
 847        int recheckAsMaster = 0;
 848
 849        if ( detect )
 850                endTime = jiffies + ISD200_ENUM_DETECT_TIMEOUT * HZ;
 851        else
 852                endTime = jiffies + ISD200_ENUM_BSY_TIMEOUT * HZ;
 853
 854        /* loop until we detect !BSY or timeout */
 855        while(1) {
 856#ifdef CONFIG_USB_STORAGE_DEBUG
 857                char* mstr = master_slave == ATA_ADDRESS_DEVHEAD_STD ?
 858                        "Master" : "Slave";
 859#endif
 860
 861                status = isd200_action( us, ACTION_ENUM, NULL, master_slave );
 862                if ( status != ISD200_GOOD )
 863                        break;
 864
 865                status = isd200_action( us, ACTION_READ_STATUS, 
 866                                        regs, 8 );
 867                if ( status != ISD200_GOOD )
 868                        break;
 869
 870                if (!detect) {
 871                        if (regs[IDE_STATUS_OFFSET] & BUSY_STAT ) {
 872                                US_DEBUGP("   %s status is still BSY, try again...\n",mstr);
 873                        } else {
 874                                US_DEBUGP("   %s status !BSY, continue with next operation\n",mstr);
 875                                break;
 876                        }
 877                }
 878                /* check for BUSY_STAT and */
 879                /* WRERR_STAT (workaround ATA Zip drive) and */ 
 880                /* ERR_STAT (workaround for Archos CD-ROM) */
 881                else if (regs[IDE_STATUS_OFFSET] & 
 882                         (BUSY_STAT | WRERR_STAT | ERR_STAT )) {
 883                        US_DEBUGP("   Status indicates it is not ready, try again...\n");
 884                }
 885                /* check for DRDY, ATA devices set DRDY after SRST */
 886                else if (regs[IDE_STATUS_OFFSET] & READY_STAT) {
 887                        US_DEBUGP("   Identified ATA device\n");
 888                        info->DeviceFlags |= DF_ATA_DEVICE;
 889                        info->DeviceHead = master_slave;
 890                        break;
 891                } 
 892                /* check Cylinder High/Low to
 893                   determine if it is an ATAPI device
 894                */
 895                else if ((regs[IDE_HCYL_OFFSET] == 0xEB) &&
 896                         (regs[IDE_LCYL_OFFSET] == 0x14)) {
 897                        /* It seems that the RICOH 
 898                           MP6200A CD/RW drive will 
 899                           report itself okay as a
 900                           slave when it is really a
 901                           master. So this check again
 902                           as a master device just to
 903                           make sure it doesn't report
 904                           itself okay as a master also
 905                        */
 906                        if ((master_slave & ATA_ADDRESS_DEVHEAD_SLAVE) &&
 907                            !recheckAsMaster) {
 908                                US_DEBUGP("   Identified ATAPI device as slave.  Rechecking again as master\n");
 909                                recheckAsMaster = 1;
 910                                master_slave = ATA_ADDRESS_DEVHEAD_STD;
 911                        } else {
 912                                US_DEBUGP("   Identified ATAPI device\n");
 913                                info->DeviceHead = master_slave;
 914                              
 915                                status = isd200_atapi_soft_reset(us);
 916                                break;
 917                        }
 918                } else {
 919                        US_DEBUGP("   Not ATA, not ATAPI. Weird.\n");
 920                        break;
 921                }
 922
 923                /* check for timeout on this request */
 924                if (time_after_eq(jiffies, endTime)) {
 925                        if (!detect)
 926                                US_DEBUGP("   BSY check timeout, just continue with next operation...\n");
 927                        else
 928                                US_DEBUGP("   Device detect timeout!\n");
 929                        break;
 930                }
 931        }
 932
 933        return status;
 934}
 935
 936/**************************************************************************
 937 * isd200_manual_enum
 938 *                                                                       
 939 * Determines if the drive attached is an ATA or ATAPI and if it is a
 940 * master or slave.
 941 *
 942 * RETURNS:
 943 *    ISD status code
 944 */
 945static int isd200_manual_enum(struct us_data *us)
 946{
 947        struct isd200_info *info = (struct isd200_info *)us->extra;
 948        int retStatus = ISD200_GOOD;
 949
 950        US_DEBUGP("Entering isd200_manual_enum\n");
 951
 952        retStatus = isd200_read_config(us);
 953        if (retStatus == ISD200_GOOD) {
 954                int isslave;
 955                /* master or slave? */
 956                retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 0);
 957                if (retStatus == ISD200_GOOD)
 958                        retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_SLAVE, 0);
 959
 960                if (retStatus == ISD200_GOOD) {
 961                        retStatus = isd200_srst(us);
 962                        if (retStatus == ISD200_GOOD)
 963                                /* ata or atapi? */
 964                                retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 1);
 965                }
 966
 967                isslave = (info->DeviceHead & ATA_ADDRESS_DEVHEAD_SLAVE) ? 1 : 0;
 968                if (!(info->ConfigData.ATAConfig & ATACFG_MASTER)) {
 969                        US_DEBUGP("   Setting Master/Slave selection to %d\n", isslave);
 970                        info->ConfigData.ATAConfig &= 0x3f;
 971                        info->ConfigData.ATAConfig |= (isslave<<6);
 972                        retStatus = isd200_write_config(us);
 973                }
 974        }
 975
 976        US_DEBUGP("Leaving isd200_manual_enum %08X\n", retStatus);
 977        return(retStatus);
 978}
 979
 980/*
 981 *      We are the last non IDE user of the legacy IDE ident structures
 982 *      and we thus want to keep a private copy of this function so the
 983 *      driver can be used without the obsolete drivers/ide layer
 984 */
 985
 986static void isd200_fix_driveid (struct hd_driveid *id)
 987{
 988#ifndef __LITTLE_ENDIAN
 989# ifdef __BIG_ENDIAN
 990        int i;
 991        u16 *stringcast;
 992
 993        id->config         = __le16_to_cpu(id->config);
 994        id->cyls           = __le16_to_cpu(id->cyls);
 995        id->reserved2      = __le16_to_cpu(id->reserved2);
 996        id->heads          = __le16_to_cpu(id->heads);
 997        id->track_bytes    = __le16_to_cpu(id->track_bytes);
 998        id->sector_bytes   = __le16_to_cpu(id->sector_bytes);
 999        id->sectors        = __le16_to_cpu(id->sectors);
1000        id->vendor0        = __le16_to_cpu(id->vendor0);
1001        id->vendor1        = __le16_to_cpu(id->vendor1);
1002        id->vendor2        = __le16_to_cpu(id->vendor2);
1003        stringcast = (u16 *)&id->serial_no[0];
1004        for (i = 0; i < (20/2); i++)
1005                stringcast[i] = __le16_to_cpu(stringcast[i]);
1006        id->buf_type       = __le16_to_cpu(id->buf_type);
1007        id->buf_size       = __le16_to_cpu(id->buf_size);
1008        id->ecc_bytes      = __le16_to_cpu(id->ecc_bytes);
1009        stringcast = (u16 *)&id->fw_rev[0];
1010        for (i = 0; i < (8/2); i++)
1011                stringcast[i] = __le16_to_cpu(stringcast[i]);
1012        stringcast = (u16 *)&id->model[0];
1013        for (i = 0; i < (40/2); i++)
1014                stringcast[i] = __le16_to_cpu(stringcast[i]);
1015        id->dword_io       = __le16_to_cpu(id->dword_io);
1016        id->reserved50     = __le16_to_cpu(id->reserved50);
1017        id->field_valid    = __le16_to_cpu(id->field_valid);
1018        id->cur_cyls       = __le16_to_cpu(id->cur_cyls);
1019        id->cur_heads      = __le16_to_cpu(id->cur_heads);
1020        id->cur_sectors    = __le16_to_cpu(id->cur_sectors);
1021        id->cur_capacity0  = __le16_to_cpu(id->cur_capacity0);
1022        id->cur_capacity1  = __le16_to_cpu(id->cur_capacity1);
1023        id->lba_capacity   = __le32_to_cpu(id->lba_capacity);
1024        id->dma_1word      = __le16_to_cpu(id->dma_1word);
1025        id->dma_mword      = __le16_to_cpu(id->dma_mword);
1026        id->eide_pio_modes = __le16_to_cpu(id->eide_pio_modes);
1027        id->eide_dma_min   = __le16_to_cpu(id->eide_dma_min);
1028        id->eide_dma_time  = __le16_to_cpu(id->eide_dma_time);
1029        id->eide_pio       = __le16_to_cpu(id->eide_pio);
1030        id->eide_pio_iordy = __le16_to_cpu(id->eide_pio_iordy);
1031        for (i = 0; i < 2; ++i)
1032                id->words69_70[i] = __le16_to_cpu(id->words69_70[i]);
1033        for (i = 0; i < 4; ++i)
1034                id->words71_74[i] = __le16_to_cpu(id->words71_74[i]);
1035        id->queue_depth    = __le16_to_cpu(id->queue_depth);
1036        for (i = 0; i < 4; ++i)
1037                id->words76_79[i] = __le16_to_cpu(id->words76_79[i]);
1038        id->major_rev_num  = __le16_to_cpu(id->major_rev_num);
1039        id->minor_rev_num  = __le16_to_cpu(id->minor_rev_num);
1040        id->command_set_1  = __le16_to_cpu(id->command_set_1);
1041        id->command_set_2  = __le16_to_cpu(id->command_set_2);
1042        id->cfsse          = __le16_to_cpu(id->cfsse);
1043        id->cfs_enable_1   = __le16_to_cpu(id->cfs_enable_1);
1044        id->cfs_enable_2   = __le16_to_cpu(id->cfs_enable_2);
1045        id->csf_default    = __le16_to_cpu(id->csf_default);
1046        id->dma_ultra      = __le16_to_cpu(id->dma_ultra);
1047        id->trseuc         = __le16_to_cpu(id->trseuc);
1048        id->trsEuc         = __le16_to_cpu(id->trsEuc);
1049        id->CurAPMvalues   = __le16_to_cpu(id->CurAPMvalues);
1050        id->mprc           = __le16_to_cpu(id->mprc);
1051        id->hw_config      = __le16_to_cpu(id->hw_config);
1052        id->acoustic       = __le16_to_cpu(id->acoustic);
1053        id->msrqs          = __le16_to_cpu(id->msrqs);
1054        id->sxfert         = __le16_to_cpu(id->sxfert);
1055        id->sal            = __le16_to_cpu(id->sal);
1056        id->spg            = __le32_to_cpu(id->spg);
1057        id->lba_capacity_2 = __le64_to_cpu(id->lba_capacity_2);
1058        for (i = 0; i < 22; i++)
1059                id->words104_125[i]   = __le16_to_cpu(id->words104_125[i]);
1060        id->last_lun       = __le16_to_cpu(id->last_lun);
1061        id->word127        = __le16_to_cpu(id->word127);
1062        id->dlf            = __le16_to_cpu(id->dlf);
1063        id->csfo           = __le16_to_cpu(id->csfo);
1064        for (i = 0; i < 26; i++)
1065                id->words130_155[i] = __le16_to_cpu(id->words130_155[i]);
1066        id->word156        = __le16_to_cpu(id->word156);
1067        for (i = 0; i < 3; i++)
1068                id->words157_159[i] = __le16_to_cpu(id->words157_159[i]);
1069        id->cfa_power      = __le16_to_cpu(id->cfa_power);
1070        for (i = 0; i < 14; i++)
1071                id->words161_175[i] = __le16_to_cpu(id->words161_175[i]);
1072        for (i = 0; i < 31; i++)
1073                id->words176_205[i] = __le16_to_cpu(id->words176_205[i]);
1074        for (i = 0; i < 48; i++)
1075                id->words206_254[i] = __le16_to_cpu(id->words206_254[i]);
1076        id->integrity_word  = __le16_to_cpu(id->integrity_word);
1077# else
1078#  error "Please fix <asm/byteorder.h>"
1079# endif
1080#endif
1081}
1082
1083
1084/**************************************************************************
1085 * isd200_get_inquiry_data
1086 *
1087 * Get inquiry data
1088 *
1089 * RETURNS:
1090 *    ISD status code
1091 */
1092static int isd200_get_inquiry_data( struct us_data *us )
1093{
1094        struct isd200_info *info = (struct isd200_info *)us->extra;
1095        int retStatus = ISD200_GOOD;
1096        struct hd_driveid *id = info->id;
1097
1098        US_DEBUGP("Entering isd200_get_inquiry_data\n");
1099
1100        /* set default to Master */
1101        info->DeviceHead = ATA_ADDRESS_DEVHEAD_STD;
1102
1103        /* attempt to manually enumerate this device */
1104        retStatus = isd200_manual_enum(us);
1105        if (retStatus == ISD200_GOOD) {
1106                int transferStatus;
1107
1108                /* check for an ATA device */
1109                if (info->DeviceFlags & DF_ATA_DEVICE) {
1110                        /* this must be an ATA device */
1111                        /* perform an ATA Command Identify */
1112                        transferStatus = isd200_action( us, ACTION_IDENTIFY,
1113                                                        id, 
1114                                                        sizeof(struct hd_driveid) );
1115                        if (transferStatus != ISD200_TRANSPORT_GOOD) {
1116                                /* Error issuing ATA Command Identify */
1117                                US_DEBUGP("   Error issuing ATA Command Identify\n");
1118                                retStatus = ISD200_ERROR;
1119                        } else {
1120                                /* ATA Command Identify successful */
1121                                int i;
1122                                __be16 *src;
1123                                __u16 *dest;
1124                                isd200_fix_driveid(id);
1125
1126                                US_DEBUGP("   Identify Data Structure:\n");
1127                                US_DEBUGP("      config = 0x%x\n", id->config);
1128                                US_DEBUGP("      cyls = 0x%x\n", id->cyls);
1129                                US_DEBUGP("      heads = 0x%x\n", id->heads);
1130                                US_DEBUGP("      track_bytes = 0x%x\n", id->track_bytes);
1131                                US_DEBUGP("      sector_bytes = 0x%x\n", id->sector_bytes);
1132                                US_DEBUGP("      sectors = 0x%x\n", id->sectors);
1133                                US_DEBUGP("      serial_no[0] = 0x%x\n", id->serial_no[0]);
1134                                US_DEBUGP("      buf_type = 0x%x\n", id->buf_type);
1135                                US_DEBUGP("      buf_size = 0x%x\n", id->buf_size);
1136                                US_DEBUGP("      ecc_bytes = 0x%x\n", id->ecc_bytes);
1137                                US_DEBUGP("      fw_rev[0] = 0x%x\n", id->fw_rev[0]);
1138                                US_DEBUGP("      model[0] = 0x%x\n", id->model[0]);
1139                                US_DEBUGP("      max_multsect = 0x%x\n", id->max_multsect);
1140                                US_DEBUGP("      dword_io = 0x%x\n", id->dword_io);
1141                                US_DEBUGP("      capability = 0x%x\n", id->capability);
1142                                US_DEBUGP("      tPIO = 0x%x\n", id->tPIO);
1143                                US_DEBUGP("      tDMA = 0x%x\n", id->tDMA);
1144                                US_DEBUGP("      field_valid = 0x%x\n", id->field_valid);
1145                                US_DEBUGP("      cur_cyls = 0x%x\n", id->cur_cyls);
1146                                US_DEBUGP("      cur_heads = 0x%x\n", id->cur_heads);
1147                                US_DEBUGP("      cur_sectors = 0x%x\n", id->cur_sectors);
1148                                US_DEBUGP("      cur_capacity = 0x%x\n", (id->cur_capacity1 << 16) + id->cur_capacity0 );
1149                                US_DEBUGP("      multsect = 0x%x\n", id->multsect);
1150                                US_DEBUGP("      lba_capacity = 0x%x\n", id->lba_capacity);
1151                                US_DEBUGP("      command_set_1 = 0x%x\n", id->command_set_1);
1152                                US_DEBUGP("      command_set_2 = 0x%x\n", id->command_set_2);
1153
1154                                memset(&info->InquiryData, 0, sizeof(info->InquiryData));
1155
1156                                /* Standard IDE interface only supports disks */
1157                                info->InquiryData.DeviceType = DIRECT_ACCESS_DEVICE;
1158
1159                                /* The length must be at least 36 (5 + 31) */
1160                                info->InquiryData.AdditionalLength = 0x1F;
1161
1162                                if (id->command_set_1 & COMMANDSET_MEDIA_STATUS) {
1163                                        /* set the removable bit */
1164                                        info->InquiryData.DeviceTypeModifier = DEVICE_REMOVABLE;
1165                                        info->DeviceFlags |= DF_REMOVABLE_MEDIA;
1166                                }
1167
1168                                /* Fill in vendor identification fields */
1169                                src = (__be16*)id->model;
1170                                dest = (__u16*)info->InquiryData.VendorId;
1171                                for (i=0;i<4;i++)
1172                                        dest[i] = be16_to_cpu(src[i]);
1173
1174                                src = (__be16*)(id->model+8);
1175                                dest = (__u16*)info->InquiryData.ProductId;
1176                                for (i=0;i<8;i++)
1177                                        dest[i] = be16_to_cpu(src[i]);
1178
1179                                src = (__be16*)id->fw_rev;
1180                                dest = (__u16*)info->InquiryData.ProductRevisionLevel;
1181                                for (i=0;i<2;i++)
1182                                        dest[i] = be16_to_cpu(src[i]);
1183
1184                                /* determine if it supports Media Status Notification */
1185                                if (id->command_set_2 & COMMANDSET_MEDIA_STATUS) {
1186                                        US_DEBUGP("   Device supports Media Status Notification\n");
1187
1188                                        /* Indicate that it is enabled, even though it is not
1189                                         * This allows the lock/unlock of the media to work
1190                                         * correctly.
1191                                         */
1192                                        info->DeviceFlags |= DF_MEDIA_STATUS_ENABLED;
1193                                }
1194                                else
1195                                        info->DeviceFlags &= ~DF_MEDIA_STATUS_ENABLED;
1196
1197                        }
1198                } else {
1199                        /* 
1200                         * this must be an ATAPI device 
1201                         * use an ATAPI protocol (Transparent SCSI)
1202                         */
1203                        us->protocol_name = "Transparent SCSI";
1204                        us->proto_handler = usb_stor_transparent_scsi_command;
1205
1206                        US_DEBUGP("Protocol changed to: %s\n", us->protocol_name);
1207            
1208                        /* Free driver structure */         
1209                        us->extra_destructor(info);
1210                        us->extra = NULL;
1211                        us->extra_destructor = NULL;
1212                }
1213        }
1214
1215        US_DEBUGP("Leaving isd200_get_inquiry_data %08X\n", retStatus);
1216
1217        return(retStatus);
1218}
1219
1220
1221/**************************************************************************
1222 * isd200_scsi_to_ata
1223 *                                                                       
1224 * Translate SCSI commands to ATA commands.
1225 *
1226 * RETURNS:
1227 *    1 if the command needs to be sent to the transport layer
1228 *    0 otherwise
1229 */
1230static int isd200_scsi_to_ata(struct scsi_cmnd *srb, struct us_data *us,
1231                              union ata_cdb * ataCdb)
1232{
1233        struct isd200_info *info = (struct isd200_info *)us->extra;
1234        struct hd_driveid *id = info->id;
1235        int sendToTransport = 1;
1236        unsigned char sectnum, head;
1237        unsigned short cylinder;
1238        unsigned long lba;
1239        unsigned long blockCount;
1240        unsigned char senseData[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1241
1242        memset(ataCdb, 0, sizeof(union ata_cdb));
1243
1244        /* SCSI Command */
1245        switch (srb->cmnd[0]) {
1246        case INQUIRY:
1247                US_DEBUGP("   ATA OUT - INQUIRY\n");
1248
1249                /* copy InquiryData */
1250                usb_stor_set_xfer_buf((unsigned char *) &info->InquiryData,
1251                                sizeof(info->InquiryData), srb);
1252                srb->result = SAM_STAT_GOOD;
1253                sendToTransport = 0;
1254                break;
1255
1256        case MODE_SENSE:
1257                US_DEBUGP("   ATA OUT - SCSIOP_MODE_SENSE\n");
1258
1259                /* Initialize the return buffer */
1260                usb_stor_set_xfer_buf(senseData, sizeof(senseData), srb);
1261
1262                if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
1263                {
1264                        ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1265                        ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1266                        ataCdb->generic.TransferBlockSize = 1;
1267                        ataCdb->generic.RegisterSelect = REG_COMMAND;
1268                        ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1269                        srb->request_bufflen = 0;
1270                } else {
1271                        US_DEBUGP("   Media Status not supported, just report okay\n");
1272                        srb->result = SAM_STAT_GOOD;
1273                        sendToTransport = 0;
1274                }
1275                break;
1276
1277        case TEST_UNIT_READY:
1278                US_DEBUGP("   ATA OUT - SCSIOP_TEST_UNIT_READY\n");
1279
1280                if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
1281                {
1282                        ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1283                        ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1284                        ataCdb->generic.TransferBlockSize = 1;
1285                        ataCdb->generic.RegisterSelect = REG_COMMAND;
1286                        ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1287                        srb->request_bufflen = 0;
1288                } else {
1289                        US_DEBUGP("   Media Status not supported, just report okay\n");
1290                        srb->result = SAM_STAT_GOOD;
1291                        sendToTransport = 0;
1292                }
1293                break;
1294
1295        case READ_CAPACITY:
1296        {
1297                unsigned long capacity;
1298                struct read_capacity_data readCapacityData;
1299
1300                US_DEBUGP("   ATA OUT - SCSIOP_READ_CAPACITY\n");
1301
1302                if (id->capability & CAPABILITY_LBA ) {
1303                        capacity = id->lba_capacity - 1;
1304                } else {
1305                        capacity = (id->heads *
1306                                    id->cyls *
1307                                    id->sectors) - 1;
1308                }
1309                readCapacityData.LogicalBlockAddress = cpu_to_be32(capacity);
1310                readCapacityData.BytesPerBlock = cpu_to_be32(0x200);
1311
1312                usb_stor_set_xfer_buf((unsigned char *) &readCapacityData,
1313                                sizeof(readCapacityData), srb);
1314                srb->result = SAM_STAT_GOOD;
1315                sendToTransport = 0;
1316        }
1317        break;
1318
1319        case READ_10:
1320                US_DEBUGP("   ATA OUT - SCSIOP_READ\n");
1321
1322                lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1323                blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1324
1325                if (id->capability & CAPABILITY_LBA) {
1326                        sectnum = (unsigned char)(lba);
1327                        cylinder = (unsigned short)(lba>>8);
1328                        head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1329                } else {
1330                        sectnum = (unsigned char)((lba % id->sectors) + 1);
1331                        cylinder = (unsigned short)(lba / (id->sectors *
1332                                                           id->heads));
1333                        head = (unsigned char)((lba / id->sectors) %
1334                                               id->heads);
1335                }
1336                ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1337                ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1338                ataCdb->generic.TransferBlockSize = 1;
1339                ataCdb->generic.RegisterSelect =
1340                  REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
1341                  REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
1342                  REG_DEVICE_HEAD  | REG_COMMAND;
1343                ataCdb->write.SectorCountByte = (unsigned char)blockCount;
1344                ataCdb->write.SectorNumberByte = sectnum;
1345                ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
1346                ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
1347                ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
1348                ataCdb->write.CommandByte = WIN_READ;
1349                break;
1350
1351        case WRITE_10:
1352                US_DEBUGP("   ATA OUT - SCSIOP_WRITE\n");
1353
1354                lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1355                blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1356
1357                if (id->capability & CAPABILITY_LBA) {
1358                        sectnum = (unsigned char)(lba);
1359                        cylinder = (unsigned short)(lba>>8);
1360                        head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1361                } else {
1362                        sectnum = (unsigned char)((lba % id->sectors) + 1);
1363                        cylinder = (unsigned short)(lba / (id->sectors * id->heads));
1364                        head = (unsigned char)((lba / id->sectors) % id->heads);
1365                }
1366                ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1367                ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1368                ataCdb->generic.TransferBlockSize = 1;
1369                ataCdb->generic.RegisterSelect =
1370                  REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
1371                  REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
1372                  REG_DEVICE_HEAD  | REG_COMMAND;
1373                ataCdb->write.SectorCountByte = (unsigned char)blockCount;
1374                ataCdb->write.SectorNumberByte = sectnum;
1375                ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
1376                ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
1377                ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
1378                ataCdb->write.CommandByte = WIN_WRITE;
1379                break;
1380
1381        case ALLOW_MEDIUM_REMOVAL:
1382                US_DEBUGP("   ATA OUT - SCSIOP_MEDIUM_REMOVAL\n");
1383
1384                if (info->DeviceFlags & DF_REMOVABLE_MEDIA) {
1385                        US_DEBUGP("   srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
1386            
1387                        ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1388                        ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1389                        ataCdb->generic.TransferBlockSize = 1;
1390                        ataCdb->generic.RegisterSelect = REG_COMMAND;
1391                        ataCdb->write.CommandByte = (srb->cmnd[4] & 0x1) ?
1392                                WIN_DOORLOCK : WIN_DOORUNLOCK;
1393                        srb->request_bufflen = 0;
1394                } else {
1395                        US_DEBUGP("   Not removeable media, just report okay\n");
1396                        srb->result = SAM_STAT_GOOD;
1397                        sendToTransport = 0;
1398                }
1399                break;
1400
1401        case START_STOP:    
1402                US_DEBUGP("   ATA OUT - SCSIOP_START_STOP_UNIT\n");
1403                US_DEBUGP("   srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
1404
1405                if ((srb->cmnd[4] & 0x3) == 0x2) {
1406                        US_DEBUGP("   Media Eject\n");
1407                        ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1408                        ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1409                        ataCdb->generic.TransferBlockSize = 0;
1410                        ataCdb->generic.RegisterSelect = REG_COMMAND;
1411                        ataCdb->write.CommandByte = ATA_COMMAND_MEDIA_EJECT;
1412                } else if ((srb->cmnd[4] & 0x3) == 0x1) {
1413                        US_DEBUGP("   Get Media Status\n");
1414                        ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1415                        ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1416                        ataCdb->generic.TransferBlockSize = 1;
1417                        ataCdb->generic.RegisterSelect = REG_COMMAND;
1418                        ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1419                        srb->request_bufflen = 0;
1420                } else {
1421                        US_DEBUGP("   Nothing to do, just report okay\n");
1422                        srb->result = SAM_STAT_GOOD;
1423                        sendToTransport = 0;
1424                }
1425                break;
1426
1427        default:
1428                US_DEBUGP("Unsupported SCSI command - 0x%X\n", srb->cmnd[0]);
1429                srb->result = DID_ERROR << 16;
1430                sendToTransport = 0;
1431                break;
1432        }
1433
1434        return(sendToTransport);
1435}
1436
1437
1438/**************************************************************************
1439 * isd200_free_info
1440 *
1441 * Frees the driver structure.
1442 */
1443static void isd200_free_info_ptrs(void *info_)
1444{
1445        struct isd200_info *info = (struct isd200_info *) info_;
1446
1447        if (info) {
1448                kfree(info->id);
1449                kfree(info->RegsBuf);
1450        }
1451}
1452
1453/**************************************************************************
1454 * isd200_init_info
1455 *                                                                       
1456 * Allocates (if necessary) and initializes the driver structure.
1457 *
1458 * RETURNS:
1459 *    ISD status code
1460 */
1461static int isd200_init_info(struct us_data *us)
1462{
1463        int retStatus = ISD200_GOOD;
1464        struct isd200_info *info;
1465
1466        info = (struct isd200_info *)
1467                        kzalloc(sizeof(struct isd200_info), GFP_KERNEL);
1468        if (!info)
1469                retStatus = ISD200_ERROR;
1470        else {
1471                info->id = (struct hd_driveid *)
1472                                kzalloc(sizeof(struct hd_driveid), GFP_KERNEL);
1473                info->RegsBuf = (unsigned char *)
1474                                kmalloc(sizeof(info->ATARegs), GFP_KERNEL);
1475                if (!info->id || !info->RegsBuf) {
1476                        isd200_free_info_ptrs(info);
1477                        kfree(info);
1478                        retStatus = ISD200_ERROR;
1479                }
1480        }
1481
1482        if (retStatus == ISD200_GOOD) {
1483                us->extra = info;
1484                us->extra_destructor = isd200_free_info_ptrs;
1485        } else
1486                US_DEBUGP("ERROR - kmalloc failure\n");
1487
1488        return retStatus;
1489}
1490
1491/**************************************************************************
1492 * Initialization for the ISD200 
1493 */
1494
1495int isd200_Initialization(struct us_data *us)
1496{
1497        US_DEBUGP("ISD200 Initialization...\n");
1498
1499        /* Initialize ISD200 info struct */
1500
1501        if (isd200_init_info(us) == ISD200_ERROR) {
1502                US_DEBUGP("ERROR Initializing ISD200 Info struct\n");
1503        } else {
1504                /* Get device specific data */
1505
1506                if (isd200_get_inquiry_data(us) != ISD200_GOOD)
1507                        US_DEBUGP("ISD200 Initialization Failure\n");
1508                else
1509                        US_DEBUGP("ISD200 Initialization complete\n");
1510        }
1511
1512        return 0;
1513}
1514
1515
1516/**************************************************************************
1517 * Protocol and Transport for the ISD200 ASIC
1518 *
1519 * This protocol and transport are for ATA devices connected to an ISD200
1520 * ASIC.  An ATAPI device that is conected as a slave device will be
1521 * detected in the driver initialization function and the protocol will
1522 * be changed to an ATAPI protocol (Transparent SCSI).
1523 *
1524 */
1525
1526void isd200_ata_command(struct scsi_cmnd *srb, struct us_data *us)
1527{
1528        int sendToTransport = 1;
1529        union ata_cdb ataCdb;
1530
1531        /* Make sure driver was initialized */
1532
1533        if (us->extra == NULL)
1534                US_DEBUGP("ERROR Driver not initialized\n");
1535
1536        /* Convert command */
1537        srb->resid = 0;
1538        sendToTransport = isd200_scsi_to_ata(srb, us, &ataCdb);
1539
1540        /* send the command to the transport layer */
1541        if (sendToTransport)
1542                isd200_invoke_transport(us, srb, &ataCdb);
1543}
1544