Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Transport & Protocol Driver for In-System Design, Inc. ISD200 ASIC
0004  *
0005  * Current development and maintenance:
0006  *   (C) 2001-2002 Björn Stenberg (bjorn@haxx.se)
0007  *
0008  * Developed with the assistance of:
0009  *   (C) 2002 Alan Stern <stern@rowland.org>
0010  *
0011  * Initial work:
0012  *   (C) 2000 In-System Design, Inc. (support@in-system.com)
0013  *
0014  * The ISD200 ASIC does not natively support ATA devices.  The chip
0015  * does implement an interface, the ATA Command Block (ATACB) which provides
0016  * a means of passing ATA commands and ATA register accesses to a device.
0017  *
0018  * History:
0019  *
0020  *  2002-10-19: Removed the specialized transfer routines.
0021  *      (Alan Stern <stern@rowland.harvard.edu>)
0022  *  2001-02-24: Removed lots of duplicate code and simplified the structure.
0023  *        (bjorn@haxx.se)
0024  *  2002-01-16: Fixed endianness bug so it works on the ppc arch.
0025  *        (Luc Saillard <luc@saillard.org>)
0026  *  2002-01-17: All bitfields removed.
0027  *        (bjorn@haxx.se)
0028  */
0029 
0030 
0031 /* Include files */
0032 
0033 #include <linux/jiffies.h>
0034 #include <linux/errno.h>
0035 #include <linux/module.h>
0036 #include <linux/slab.h>
0037 #include <linux/ata.h>
0038 #include <linux/hdreg.h>
0039 #include <linux/scatterlist.h>
0040 
0041 #include <scsi/scsi.h>
0042 #include <scsi/scsi_cmnd.h>
0043 #include <scsi/scsi_device.h>
0044 
0045 #include "usb.h"
0046 #include "transport.h"
0047 #include "protocol.h"
0048 #include "debug.h"
0049 #include "scsiglue.h"
0050 
0051 #define DRV_NAME "ums-isd200"
0052 
0053 MODULE_DESCRIPTION("Driver for In-System Design, Inc. ISD200 ASIC");
0054 MODULE_AUTHOR("Björn Stenberg <bjorn@haxx.se>");
0055 MODULE_LICENSE("GPL");
0056 MODULE_IMPORT_NS(USB_STORAGE);
0057 
0058 static int isd200_Initialization(struct us_data *us);
0059 
0060 
0061 /*
0062  * The table of devices
0063  */
0064 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
0065             vendorName, productName, useProtocol, useTransport, \
0066             initFunction, flags) \
0067 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
0068   .driver_info = (flags) }
0069 
0070 static struct usb_device_id isd200_usb_ids[] = {
0071 #   include "unusual_isd200.h"
0072     { }     /* Terminating entry */
0073 };
0074 MODULE_DEVICE_TABLE(usb, isd200_usb_ids);
0075 
0076 #undef UNUSUAL_DEV
0077 
0078 /*
0079  * The flags table
0080  */
0081 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
0082             vendor_name, product_name, use_protocol, use_transport, \
0083             init_function, Flags) \
0084 { \
0085     .vendorName = vendor_name,  \
0086     .productName = product_name,    \
0087     .useProtocol = use_protocol,    \
0088     .useTransport = use_transport,  \
0089     .initFunction = init_function,  \
0090 }
0091 
0092 static struct us_unusual_dev isd200_unusual_dev_list[] = {
0093 #   include "unusual_isd200.h"
0094     { }     /* Terminating entry */
0095 };
0096 
0097 #undef UNUSUAL_DEV
0098 
0099 /* Timeout defines (in Seconds) */
0100 
0101 #define ISD200_ENUM_BSY_TIMEOUT     35
0102 #define ISD200_ENUM_DETECT_TIMEOUT      30
0103 #define ISD200_DEFAULT_TIMEOUT      30
0104 
0105 /* device flags */
0106 #define DF_ATA_DEVICE       0x0001
0107 #define DF_MEDIA_STATUS_ENABLED 0x0002
0108 #define DF_REMOVABLE_MEDIA  0x0004
0109 
0110 /* capability bit definitions */
0111 #define CAPABILITY_DMA      0x01
0112 #define CAPABILITY_LBA      0x02
0113 
0114 /* command_setX bit definitions */
0115 #define COMMANDSET_REMOVABLE    0x02
0116 #define COMMANDSET_MEDIA_STATUS 0x10
0117 
0118 /* ATA Vendor Specific defines */
0119 #define ATA_ADDRESS_DEVHEAD_STD      0xa0
0120 #define ATA_ADDRESS_DEVHEAD_LBA_MODE 0x40    
0121 #define ATA_ADDRESS_DEVHEAD_SLAVE    0x10
0122 
0123 /* Action Select bits */
0124 #define ACTION_SELECT_0      0x01
0125 #define ACTION_SELECT_1      0x02
0126 #define ACTION_SELECT_2      0x04
0127 #define ACTION_SELECT_3      0x08
0128 #define ACTION_SELECT_4      0x10
0129 #define ACTION_SELECT_5      0x20
0130 #define ACTION_SELECT_6      0x40
0131 #define ACTION_SELECT_7      0x80
0132 
0133 /* Register Select bits */
0134 #define REG_ALTERNATE_STATUS    0x01
0135 #define REG_DEVICE_CONTROL  0x01
0136 #define REG_ERROR       0x02
0137 #define REG_FEATURES        0x02
0138 #define REG_SECTOR_COUNT    0x04
0139 #define REG_SECTOR_NUMBER   0x08
0140 #define REG_CYLINDER_LOW    0x10
0141 #define REG_CYLINDER_HIGH   0x20
0142 #define REG_DEVICE_HEAD     0x40
0143 #define REG_STATUS      0x80
0144 #define REG_COMMAND     0x80
0145 
0146 /* ATA registers offset definitions */
0147 #define ATA_REG_ERROR_OFFSET        1
0148 #define ATA_REG_LCYL_OFFSET     4
0149 #define ATA_REG_HCYL_OFFSET     5
0150 #define ATA_REG_STATUS_OFFSET       7
0151 
0152 /* ATA error definitions not in <linux/hdreg.h> */
0153 #define ATA_ERROR_MEDIA_CHANGE      0x20
0154 
0155 /* ATA command definitions not in <linux/hdreg.h> */
0156 #define ATA_COMMAND_GET_MEDIA_STATUS    0xDA
0157 #define ATA_COMMAND_MEDIA_EJECT     0xED
0158 
0159 /* ATA drive control definitions */
0160 #define ATA_DC_DISABLE_INTERRUPTS   0x02
0161 #define ATA_DC_RESET_CONTROLLER     0x04
0162 #define ATA_DC_REENABLE_CONTROLLER  0x00
0163 
0164 /*
0165  *  General purpose return codes
0166  */ 
0167 
0168 #define ISD200_ERROR        -1
0169 #define ISD200_GOOD      0
0170 
0171 /*
0172  * Transport return codes
0173  */
0174 
0175 #define ISD200_TRANSPORT_GOOD       0   /* Transport good, command good     */
0176 #define ISD200_TRANSPORT_FAILED     1   /* Transport good, command failed   */
0177 #define ISD200_TRANSPORT_ERROR      2   /* Transport bad (i.e. device dead) */
0178 
0179 /* driver action codes */
0180 #define ACTION_READ_STATUS  0
0181 #define ACTION_RESET        1
0182 #define ACTION_REENABLE     2
0183 #define ACTION_SOFT_RESET   3
0184 #define ACTION_ENUM     4
0185 #define ACTION_IDENTIFY     5
0186 
0187 
0188 /*
0189  * ata_cdb struct
0190  */
0191 
0192 
0193 union ata_cdb {
0194     struct {
0195         unsigned char SignatureByte0;
0196         unsigned char SignatureByte1;
0197         unsigned char ActionSelect;
0198         unsigned char RegisterSelect;
0199         unsigned char TransferBlockSize;
0200         unsigned char WriteData3F6;
0201         unsigned char WriteData1F1;
0202         unsigned char WriteData1F2;
0203         unsigned char WriteData1F3;
0204         unsigned char WriteData1F4;
0205         unsigned char WriteData1F5;
0206         unsigned char WriteData1F6;
0207         unsigned char WriteData1F7;
0208         unsigned char Reserved[3];
0209     } generic;
0210 
0211     struct {
0212         unsigned char SignatureByte0;
0213         unsigned char SignatureByte1;
0214         unsigned char ActionSelect;
0215         unsigned char RegisterSelect;
0216         unsigned char TransferBlockSize;
0217         unsigned char AlternateStatusByte;
0218         unsigned char ErrorByte;
0219         unsigned char SectorCountByte;
0220         unsigned char SectorNumberByte;
0221         unsigned char CylinderLowByte;
0222         unsigned char CylinderHighByte;
0223         unsigned char DeviceHeadByte;
0224         unsigned char StatusByte;
0225         unsigned char Reserved[3];
0226     } read;
0227 
0228     struct {
0229         unsigned char SignatureByte0;
0230         unsigned char SignatureByte1;
0231         unsigned char ActionSelect;
0232         unsigned char RegisterSelect;
0233         unsigned char TransferBlockSize;
0234         unsigned char DeviceControlByte;
0235         unsigned char FeaturesByte;
0236         unsigned char SectorCountByte;
0237         unsigned char SectorNumberByte;
0238         unsigned char CylinderLowByte;
0239         unsigned char CylinderHighByte;
0240         unsigned char DeviceHeadByte;
0241         unsigned char CommandByte;
0242         unsigned char Reserved[3];
0243     } write;
0244 };
0245 
0246 
0247 /*
0248  * Inquiry data structure. This is the data returned from the target
0249  * after it receives an inquiry.
0250  *
0251  * This structure may be extended by the number of bytes specified
0252  * in the field AdditionalLength. The defined size constant only
0253  * includes fields through ProductRevisionLevel.
0254  */
0255 
0256 /*
0257  * DeviceType field
0258  */
0259 #define DIRECT_ACCESS_DEVICE        0x00    /* disks */
0260 #define DEVICE_REMOVABLE        0x80
0261 
0262 struct inquiry_data {
0263     unsigned char DeviceType;
0264     unsigned char DeviceTypeModifier;
0265     unsigned char Versions;
0266     unsigned char Format; 
0267     unsigned char AdditionalLength;
0268     unsigned char Reserved[2];
0269     unsigned char Capability;
0270     unsigned char VendorId[8];
0271     unsigned char ProductId[16];
0272     unsigned char ProductRevisionLevel[4];
0273     unsigned char VendorSpecific[20];
0274     unsigned char Reserved3[40];
0275 } __attribute__ ((packed));
0276 
0277 /*
0278  * INQUIRY data buffer size
0279  */
0280 
0281 #define INQUIRYDATABUFFERSIZE 36
0282 
0283 
0284 /*
0285  * ISD200 CONFIG data struct
0286  */
0287 
0288 #define ATACFG_TIMING     0x0f
0289 #define ATACFG_ATAPI_RESET     0x10
0290 #define ATACFG_MASTER     0x20
0291 #define ATACFG_BLOCKSIZE       0xa0
0292 
0293 #define ATACFGE_LAST_LUN       0x07
0294 #define ATACFGE_DESC_OVERRIDE  0x08
0295 #define ATACFGE_STATE_SUSPEND  0x10
0296 #define ATACFGE_SKIP_BOOT      0x20
0297 #define ATACFGE_CONF_DESC2     0x40
0298 #define ATACFGE_INIT_STATUS    0x80
0299 
0300 #define CFG_CAPABILITY_SRST    0x01
0301 
0302 struct isd200_config {
0303     unsigned char EventNotification;
0304     unsigned char ExternalClock;
0305     unsigned char ATAInitTimeout;
0306     unsigned char ATAConfig;
0307     unsigned char ATAMajorCommand;
0308     unsigned char ATAMinorCommand;
0309     unsigned char ATAExtraConfig;
0310     unsigned char Capability;
0311 }__attribute__ ((packed));
0312 
0313 
0314 /*
0315  * ISD200 driver information struct
0316  */
0317 
0318 struct isd200_info {
0319     struct inquiry_data InquiryData;
0320     u16 *id;
0321     struct isd200_config ConfigData;
0322     unsigned char *RegsBuf;
0323     unsigned char ATARegs[8];
0324     unsigned char DeviceHead;
0325     unsigned char DeviceFlags;
0326 
0327     /* maximum number of LUNs supported */
0328     unsigned char MaxLUNs;
0329     unsigned char cmnd[MAX_COMMAND_SIZE];
0330     struct scsi_cmnd srb;
0331     struct scatterlist sg;
0332 };
0333 
0334 
0335 /*
0336  * Read Capacity Data - returned in Big Endian format
0337  */
0338 
0339 struct read_capacity_data {
0340     __be32 LogicalBlockAddress;
0341     __be32 BytesPerBlock;
0342 };
0343 
0344 /*
0345  * Read Block Limits Data - returned in Big Endian format
0346  * This structure returns the maximum and minimum block
0347  * size for a TAPE device.
0348  */
0349 
0350 struct read_block_limits {
0351     unsigned char Reserved;
0352     unsigned char BlockMaximumSize[3];
0353     unsigned char BlockMinimumSize[2];
0354 };
0355 
0356 
0357 /*
0358  * Sense Data Format
0359  */
0360 
0361 #define SENSE_ERRCODE      0x7f
0362 #define SENSE_ERRCODE_VALID     0x80
0363 #define SENSE_FLAG_SENSE_KEY    0x0f
0364 #define SENSE_FLAG_BAD_LENGTH   0x20
0365 #define SENSE_FLAG_END_OF_MEDIA 0x40
0366 #define SENSE_FLAG_FILE_MARK    0x80
0367 struct sense_data {
0368     unsigned char ErrorCode;
0369     unsigned char SegmentNumber;
0370     unsigned char Flags;
0371     unsigned char Information[4];
0372     unsigned char AdditionalSenseLength;
0373     unsigned char CommandSpecificInformation[4];
0374     unsigned char AdditionalSenseCode;
0375     unsigned char AdditionalSenseCodeQualifier;
0376     unsigned char FieldReplaceableUnitCode;
0377     unsigned char SenseKeySpecific[3];
0378 } __attribute__ ((packed));
0379 
0380 /*
0381  * Default request sense buffer size
0382  */
0383 
0384 #define SENSE_BUFFER_SIZE 18
0385 
0386 /***********************************************************************
0387  * Helper routines
0388  ***********************************************************************/
0389 
0390 /**************************************************************************
0391  * isd200_build_sense
0392  *                                   
0393  *  Builds an artificial sense buffer to report the results of a 
0394  *  failed command.
0395  *                                     
0396  * RETURNS:
0397  *    void
0398  */
0399 static void isd200_build_sense(struct us_data *us, struct scsi_cmnd *srb)
0400 {
0401     struct isd200_info *info = (struct isd200_info *)us->extra;
0402     struct sense_data *buf = (struct sense_data *) &srb->sense_buffer[0];
0403     unsigned char error = info->ATARegs[ATA_REG_ERROR_OFFSET];
0404 
0405     if(error & ATA_ERROR_MEDIA_CHANGE) {
0406         buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
0407         buf->AdditionalSenseLength = 0xb;
0408         buf->Flags = UNIT_ATTENTION;
0409         buf->AdditionalSenseCode = 0;
0410         buf->AdditionalSenseCodeQualifier = 0;
0411     } else if (error & ATA_MCR) {
0412         buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
0413         buf->AdditionalSenseLength = 0xb;
0414         buf->Flags =  UNIT_ATTENTION;
0415         buf->AdditionalSenseCode = 0;
0416         buf->AdditionalSenseCodeQualifier = 0;
0417     } else if (error & ATA_TRK0NF) {
0418         buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
0419         buf->AdditionalSenseLength = 0xb;
0420         buf->Flags =  NOT_READY;
0421         buf->AdditionalSenseCode = 0;
0422         buf->AdditionalSenseCodeQualifier = 0;
0423     } else if (error & ATA_UNC) {
0424         buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
0425         buf->AdditionalSenseLength = 0xb;
0426         buf->Flags =  DATA_PROTECT;
0427         buf->AdditionalSenseCode = 0;
0428         buf->AdditionalSenseCodeQualifier = 0;
0429     } else {
0430         buf->ErrorCode = 0;
0431         buf->AdditionalSenseLength = 0;
0432         buf->Flags =  0;
0433         buf->AdditionalSenseCode = 0;
0434         buf->AdditionalSenseCodeQualifier = 0;
0435     }
0436 }
0437 
0438 
0439 /***********************************************************************
0440  * Transport routines
0441  ***********************************************************************/
0442 
0443 /**************************************************************************
0444  *  isd200_set_srb(), isd200_srb_set_bufflen()
0445  *
0446  * Two helpers to facilitate in initialization of scsi_cmnd structure
0447  * Will need to change when struct scsi_cmnd changes
0448  */
0449 static void isd200_set_srb(struct isd200_info *info,
0450     enum dma_data_direction dir, void* buff, unsigned bufflen)
0451 {
0452     struct scsi_cmnd *srb = &info->srb;
0453 
0454     if (buff)
0455         sg_init_one(&info->sg, buff, bufflen);
0456 
0457     srb->sc_data_direction = dir;
0458     srb->sdb.table.sgl = buff ? &info->sg : NULL;
0459     srb->sdb.length = bufflen;
0460     srb->sdb.table.nents = buff ? 1 : 0;
0461 }
0462 
0463 static void isd200_srb_set_bufflen(struct scsi_cmnd *srb, unsigned bufflen)
0464 {
0465     srb->sdb.length = bufflen;
0466 }
0467 
0468 
0469 /**************************************************************************
0470  *  isd200_action
0471  *
0472  * Routine for sending commands to the isd200
0473  *
0474  * RETURNS:
0475  *    ISD status code
0476  */
0477 static int isd200_action( struct us_data *us, int action, 
0478               void* pointer, int value )
0479 {
0480     union ata_cdb ata;
0481     /* static to prevent this large struct being placed on the valuable stack */
0482     static struct scsi_device srb_dev;
0483     struct isd200_info *info = (struct isd200_info *)us->extra;
0484     struct scsi_cmnd *srb = &info->srb;
0485     int status;
0486 
0487     memset(&ata, 0, sizeof(ata));
0488     memcpy(srb->cmnd, info->cmnd, MAX_COMMAND_SIZE);
0489     srb->device = &srb_dev;
0490 
0491     ata.generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
0492     ata.generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
0493     ata.generic.TransferBlockSize = 1;
0494 
0495     switch ( action ) {
0496     case ACTION_READ_STATUS:
0497         usb_stor_dbg(us, "   isd200_action(READ_STATUS)\n");
0498         ata.generic.ActionSelect = ACTION_SELECT_0|ACTION_SELECT_2;
0499         ata.generic.RegisterSelect =
0500           REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
0501           REG_STATUS | REG_ERROR;
0502         isd200_set_srb(info, DMA_FROM_DEVICE, pointer, value);
0503         break;
0504 
0505     case ACTION_ENUM:
0506         usb_stor_dbg(us, "   isd200_action(ENUM,0x%02x)\n", value);
0507         ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
0508                        ACTION_SELECT_3|ACTION_SELECT_4|
0509                        ACTION_SELECT_5;
0510         ata.generic.RegisterSelect = REG_DEVICE_HEAD;
0511         ata.write.DeviceHeadByte = value;
0512         isd200_set_srb(info, DMA_NONE, NULL, 0);
0513         break;
0514 
0515     case ACTION_RESET:
0516         usb_stor_dbg(us, "   isd200_action(RESET)\n");
0517         ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
0518                        ACTION_SELECT_3|ACTION_SELECT_4;
0519         ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
0520         ata.write.DeviceControlByte = ATA_DC_RESET_CONTROLLER;
0521         isd200_set_srb(info, DMA_NONE, NULL, 0);
0522         break;
0523 
0524     case ACTION_REENABLE:
0525         usb_stor_dbg(us, "   isd200_action(REENABLE)\n");
0526         ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
0527                        ACTION_SELECT_3|ACTION_SELECT_4;
0528         ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
0529         ata.write.DeviceControlByte = ATA_DC_REENABLE_CONTROLLER;
0530         isd200_set_srb(info, DMA_NONE, NULL, 0);
0531         break;
0532 
0533     case ACTION_SOFT_RESET:
0534         usb_stor_dbg(us, "   isd200_action(SOFT_RESET)\n");
0535         ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_5;
0536         ata.generic.RegisterSelect = REG_DEVICE_HEAD | REG_COMMAND;
0537         ata.write.DeviceHeadByte = info->DeviceHead;
0538         ata.write.CommandByte = ATA_CMD_DEV_RESET;
0539         isd200_set_srb(info, DMA_NONE, NULL, 0);
0540         break;
0541 
0542     case ACTION_IDENTIFY:
0543         usb_stor_dbg(us, "   isd200_action(IDENTIFY)\n");
0544         ata.generic.RegisterSelect = REG_COMMAND;
0545         ata.write.CommandByte = ATA_CMD_ID_ATA;
0546         isd200_set_srb(info, DMA_FROM_DEVICE, info->id,
0547                 ATA_ID_WORDS * 2);
0548         break;
0549 
0550     default:
0551         usb_stor_dbg(us, "Error: Undefined action %d\n", action);
0552         return ISD200_ERROR;
0553     }
0554 
0555     memcpy(srb->cmnd, &ata, sizeof(ata.generic));
0556     srb->cmd_len = sizeof(ata.generic);
0557     status = usb_stor_Bulk_transport(srb, us);
0558     if (status == USB_STOR_TRANSPORT_GOOD)
0559         status = ISD200_GOOD;
0560     else {
0561         usb_stor_dbg(us, "   isd200_action(0x%02x) error: %d\n",
0562                  action, status);
0563         status = ISD200_ERROR;
0564         /* need to reset device here */
0565     }
0566 
0567     return status;
0568 }
0569 
0570 /**************************************************************************
0571  * isd200_read_regs
0572  *                                   
0573  * Read ATA Registers
0574  *
0575  * RETURNS:
0576  *    ISD status code
0577  */
0578 static int isd200_read_regs( struct us_data *us )
0579 {
0580     struct isd200_info *info = (struct isd200_info *)us->extra;
0581     int retStatus = ISD200_GOOD;
0582     int transferStatus;
0583 
0584     usb_stor_dbg(us, "Entering isd200_IssueATAReadRegs\n");
0585 
0586     transferStatus = isd200_action( us, ACTION_READ_STATUS,
0587                     info->RegsBuf, sizeof(info->ATARegs) );
0588     if (transferStatus != ISD200_TRANSPORT_GOOD) {
0589         usb_stor_dbg(us, "   Error reading ATA registers\n");
0590         retStatus = ISD200_ERROR;
0591     } else {
0592         memcpy(info->ATARegs, info->RegsBuf, sizeof(info->ATARegs));
0593         usb_stor_dbg(us, "   Got ATA Register[ATA_REG_ERROR_OFFSET] = 0x%x\n",
0594                  info->ATARegs[ATA_REG_ERROR_OFFSET]);
0595     }
0596 
0597     return retStatus;
0598 }
0599 
0600 
0601 /**************************************************************************
0602  * Invoke the transport and basic error-handling/recovery methods
0603  *
0604  * This is used by the protocol layers to actually send the message to
0605  * the device and receive the response.
0606  */
0607 static void isd200_invoke_transport( struct us_data *us, 
0608                   struct scsi_cmnd *srb, 
0609                   union ata_cdb *ataCdb )
0610 {
0611     int need_auto_sense = 0;
0612     int transferStatus;
0613     int result;
0614 
0615     /* send the command to the transport layer */
0616     memcpy(srb->cmnd, ataCdb, sizeof(ataCdb->generic));
0617     srb->cmd_len = sizeof(ataCdb->generic);
0618     transferStatus = usb_stor_Bulk_transport(srb, us);
0619 
0620     /*
0621      * if the command gets aborted by the higher layers, we need to
0622      * short-circuit all other processing
0623      */
0624     if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
0625         usb_stor_dbg(us, "-- command was aborted\n");
0626         goto Handle_Abort;
0627     }
0628 
0629     switch (transferStatus) {
0630 
0631     case USB_STOR_TRANSPORT_GOOD:
0632         /* Indicate a good result */
0633         srb->result = SAM_STAT_GOOD;
0634         break;
0635 
0636     case USB_STOR_TRANSPORT_NO_SENSE:
0637         usb_stor_dbg(us, "-- transport indicates protocol failure\n");
0638         srb->result = SAM_STAT_CHECK_CONDITION;
0639         return;
0640 
0641     case USB_STOR_TRANSPORT_FAILED:
0642         usb_stor_dbg(us, "-- transport indicates command failure\n");
0643         need_auto_sense = 1;
0644         break;
0645 
0646     case USB_STOR_TRANSPORT_ERROR:
0647         usb_stor_dbg(us, "-- transport indicates transport error\n");
0648         srb->result = DID_ERROR << 16;
0649         /* Need reset here */
0650         return;
0651     
0652     default:
0653         usb_stor_dbg(us, "-- transport indicates unknown error\n");
0654         srb->result = DID_ERROR << 16;
0655         /* Need reset here */
0656         return;
0657     }
0658 
0659     if ((scsi_get_resid(srb) > 0) &&
0660         !((srb->cmnd[0] == REQUEST_SENSE) ||
0661           (srb->cmnd[0] == INQUIRY) ||
0662           (srb->cmnd[0] == MODE_SENSE) ||
0663           (srb->cmnd[0] == LOG_SENSE) ||
0664           (srb->cmnd[0] == MODE_SENSE_10))) {
0665         usb_stor_dbg(us, "-- unexpectedly short transfer\n");
0666         need_auto_sense = 1;
0667     }
0668 
0669     if (need_auto_sense) {
0670         result = isd200_read_regs(us);
0671         if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
0672             usb_stor_dbg(us, "-- auto-sense aborted\n");
0673             goto Handle_Abort;
0674         }
0675         if (result == ISD200_GOOD) {
0676             isd200_build_sense(us, srb);
0677             srb->result = SAM_STAT_CHECK_CONDITION;
0678 
0679             /* If things are really okay, then let's show that */
0680             if ((srb->sense_buffer[2] & 0xf) == 0x0)
0681                 srb->result = SAM_STAT_GOOD;
0682         } else {
0683             srb->result = DID_ERROR << 16;
0684             /* Need reset here */
0685         }
0686     }
0687 
0688     /*
0689      * Regardless of auto-sense, if we _know_ we have an error
0690      * condition, show that in the result code
0691      */
0692     if (transferStatus == USB_STOR_TRANSPORT_FAILED)
0693         srb->result = SAM_STAT_CHECK_CONDITION;
0694     return;
0695 
0696     /*
0697      * abort processing: the bulk-only transport requires a reset
0698      * following an abort
0699      */
0700     Handle_Abort:
0701     srb->result = DID_ABORT << 16;
0702 
0703     /* permit the reset transfer to take place */
0704     clear_bit(US_FLIDX_ABORTING, &us->dflags);
0705     /* Need reset here */
0706 }
0707 
0708 #ifdef CONFIG_USB_STORAGE_DEBUG
0709 static void isd200_log_config(struct us_data *us, struct isd200_info *info)
0710 {
0711     usb_stor_dbg(us, "      Event Notification: 0x%x\n",
0712              info->ConfigData.EventNotification);
0713     usb_stor_dbg(us, "      External Clock: 0x%x\n",
0714              info->ConfigData.ExternalClock);
0715     usb_stor_dbg(us, "      ATA Init Timeout: 0x%x\n",
0716              info->ConfigData.ATAInitTimeout);
0717     usb_stor_dbg(us, "      ATAPI Command Block Size: 0x%x\n",
0718              (info->ConfigData.ATAConfig & ATACFG_BLOCKSIZE) >> 6);
0719     usb_stor_dbg(us, "      Master/Slave Selection: 0x%x\n",
0720              info->ConfigData.ATAConfig & ATACFG_MASTER);
0721     usb_stor_dbg(us, "      ATAPI Reset: 0x%x\n",
0722              info->ConfigData.ATAConfig & ATACFG_ATAPI_RESET);
0723     usb_stor_dbg(us, "      ATA Timing: 0x%x\n",
0724              info->ConfigData.ATAConfig & ATACFG_TIMING);
0725     usb_stor_dbg(us, "      ATA Major Command: 0x%x\n",
0726              info->ConfigData.ATAMajorCommand);
0727     usb_stor_dbg(us, "      ATA Minor Command: 0x%x\n",
0728              info->ConfigData.ATAMinorCommand);
0729     usb_stor_dbg(us, "      Init Status: 0x%x\n",
0730              info->ConfigData.ATAExtraConfig & ATACFGE_INIT_STATUS);
0731     usb_stor_dbg(us, "      Config Descriptor 2: 0x%x\n",
0732              info->ConfigData.ATAExtraConfig & ATACFGE_CONF_DESC2);
0733     usb_stor_dbg(us, "      Skip Device Boot: 0x%x\n",
0734              info->ConfigData.ATAExtraConfig & ATACFGE_SKIP_BOOT);
0735     usb_stor_dbg(us, "      ATA 3 State Suspend: 0x%x\n",
0736              info->ConfigData.ATAExtraConfig & ATACFGE_STATE_SUSPEND);
0737     usb_stor_dbg(us, "      Descriptor Override: 0x%x\n",
0738              info->ConfigData.ATAExtraConfig & ATACFGE_DESC_OVERRIDE);
0739     usb_stor_dbg(us, "      Last LUN Identifier: 0x%x\n",
0740              info->ConfigData.ATAExtraConfig & ATACFGE_LAST_LUN);
0741     usb_stor_dbg(us, "      SRST Enable: 0x%x\n",
0742              info->ConfigData.ATAExtraConfig & CFG_CAPABILITY_SRST);
0743 }
0744 #endif
0745 
0746 /**************************************************************************
0747  * isd200_write_config
0748  *                                   
0749  * Write the ISD200 Configuration data
0750  *
0751  * RETURNS:
0752  *    ISD status code
0753  */
0754 static int isd200_write_config( struct us_data *us ) 
0755 {
0756     struct isd200_info *info = (struct isd200_info *)us->extra;
0757     int retStatus = ISD200_GOOD;
0758     int result;
0759 
0760 #ifdef CONFIG_USB_STORAGE_DEBUG
0761     usb_stor_dbg(us, "Entering isd200_write_config\n");
0762     usb_stor_dbg(us, "   Writing the following ISD200 Config Data:\n");
0763     isd200_log_config(us, info);
0764 #endif
0765 
0766     /* let's send the command via the control pipe */
0767     result = usb_stor_ctrl_transfer(
0768         us, 
0769         us->send_ctrl_pipe,
0770         0x01, 
0771         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0772         0x0000, 
0773         0x0002, 
0774         (void *) &info->ConfigData, 
0775         sizeof(info->ConfigData));
0776 
0777     if (result >= 0) {
0778         usb_stor_dbg(us, "   ISD200 Config Data was written successfully\n");
0779     } else {
0780         usb_stor_dbg(us, "   Request to write ISD200 Config Data failed!\n");
0781         retStatus = ISD200_ERROR;
0782     }
0783 
0784     usb_stor_dbg(us, "Leaving isd200_write_config %08X\n", retStatus);
0785     return retStatus;
0786 }
0787 
0788 
0789 /**************************************************************************
0790  * isd200_read_config
0791  *                                   
0792  * Reads the ISD200 Configuration data
0793  *
0794  * RETURNS:
0795  *    ISD status code
0796  */
0797 static int isd200_read_config( struct us_data *us ) 
0798 {
0799     struct isd200_info *info = (struct isd200_info *)us->extra;
0800     int retStatus = ISD200_GOOD;
0801     int result;
0802 
0803     usb_stor_dbg(us, "Entering isd200_read_config\n");
0804 
0805     /* read the configuration information from ISD200.  Use this to */
0806     /* determine what the special ATA CDB bytes are.        */
0807 
0808     result = usb_stor_ctrl_transfer(
0809         us, 
0810         us->recv_ctrl_pipe,
0811         0x02, 
0812         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0813         0x0000, 
0814         0x0002, 
0815         (void *) &info->ConfigData, 
0816         sizeof(info->ConfigData));
0817 
0818 
0819     if (result >= 0) {
0820         usb_stor_dbg(us, "   Retrieved the following ISD200 Config Data:\n");
0821 #ifdef CONFIG_USB_STORAGE_DEBUG
0822         isd200_log_config(us, info);
0823 #endif
0824     } else {
0825         usb_stor_dbg(us, "   Request to get ISD200 Config Data failed!\n");
0826         retStatus = ISD200_ERROR;
0827     }
0828 
0829     usb_stor_dbg(us, "Leaving isd200_read_config %08X\n", retStatus);
0830     return retStatus;
0831 }
0832 
0833 
0834 /**************************************************************************
0835  * isd200_atapi_soft_reset
0836  *                                   
0837  * Perform an Atapi Soft Reset on the device
0838  *
0839  * RETURNS:
0840  *    NT status code
0841  */
0842 static int isd200_atapi_soft_reset( struct us_data *us ) 
0843 {
0844     int retStatus = ISD200_GOOD;
0845     int transferStatus;
0846 
0847     usb_stor_dbg(us, "Entering isd200_atapi_soft_reset\n");
0848 
0849     transferStatus = isd200_action( us, ACTION_SOFT_RESET, NULL, 0 );
0850     if (transferStatus != ISD200_TRANSPORT_GOOD) {
0851         usb_stor_dbg(us, "   Error issuing Atapi Soft Reset\n");
0852         retStatus = ISD200_ERROR;
0853     }
0854 
0855     usb_stor_dbg(us, "Leaving isd200_atapi_soft_reset %08X\n", retStatus);
0856     return retStatus;
0857 }
0858 
0859 
0860 /**************************************************************************
0861  * isd200_srst
0862  *                                   
0863  * Perform an SRST on the device
0864  *
0865  * RETURNS:
0866  *    ISD status code
0867  */
0868 static int isd200_srst( struct us_data *us ) 
0869 {
0870     int retStatus = ISD200_GOOD;
0871     int transferStatus;
0872 
0873     usb_stor_dbg(us, "Entering isd200_SRST\n");
0874 
0875     transferStatus = isd200_action( us, ACTION_RESET, NULL, 0 );
0876 
0877     /* check to see if this request failed */
0878     if (transferStatus != ISD200_TRANSPORT_GOOD) {
0879         usb_stor_dbg(us, "   Error issuing SRST\n");
0880         retStatus = ISD200_ERROR;
0881     } else {
0882         /* delay 10ms to give the drive a chance to see it */
0883         msleep(10);
0884 
0885         transferStatus = isd200_action( us, ACTION_REENABLE, NULL, 0 );
0886         if (transferStatus != ISD200_TRANSPORT_GOOD) {
0887             usb_stor_dbg(us, "   Error taking drive out of reset\n");
0888             retStatus = ISD200_ERROR;
0889         } else {
0890             /* delay 50ms to give the drive a chance to recover after SRST */
0891             msleep(50);
0892         }
0893     }
0894 
0895     usb_stor_dbg(us, "Leaving isd200_srst %08X\n", retStatus);
0896     return retStatus;
0897 }
0898 
0899 
0900 /**************************************************************************
0901  * isd200_try_enum
0902  *                                   
0903  * Helper function for isd200_manual_enum(). Does ENUM and READ_STATUS
0904  * and tries to analyze the status registers
0905  *
0906  * RETURNS:
0907  *    ISD status code
0908  */
0909 static int isd200_try_enum(struct us_data *us, unsigned char master_slave,
0910                int detect )
0911 {
0912     int status = ISD200_GOOD;
0913     unsigned long endTime;
0914     struct isd200_info *info = (struct isd200_info *)us->extra;
0915     unsigned char *regs = info->RegsBuf;
0916     int recheckAsMaster = 0;
0917 
0918     if ( detect )
0919         endTime = jiffies + ISD200_ENUM_DETECT_TIMEOUT * HZ;
0920     else
0921         endTime = jiffies + ISD200_ENUM_BSY_TIMEOUT * HZ;
0922 
0923     /* loop until we detect !BSY or timeout */
0924     while(1) {
0925 
0926         status = isd200_action( us, ACTION_ENUM, NULL, master_slave );
0927         if ( status != ISD200_GOOD )
0928             break;
0929 
0930         status = isd200_action( us, ACTION_READ_STATUS, 
0931                     regs, 8 );
0932         if ( status != ISD200_GOOD )
0933             break;
0934 
0935         if (!detect) {
0936             if (regs[ATA_REG_STATUS_OFFSET] & ATA_BUSY) {
0937                 usb_stor_dbg(us, "   %s status is still BSY, try again...\n",
0938                          master_slave == ATA_ADDRESS_DEVHEAD_STD ?
0939                          "Master" : "Slave");
0940             } else {
0941                 usb_stor_dbg(us, "   %s status !BSY, continue with next operation\n",
0942                          master_slave == ATA_ADDRESS_DEVHEAD_STD ?
0943                          "Master" : "Slave");
0944                 break;
0945             }
0946         }
0947         /* check for ATA_BUSY and */
0948         /* ATA_DF (workaround ATA Zip drive) and */
0949         /* ATA_ERR (workaround for Archos CD-ROM) */
0950         else if (regs[ATA_REG_STATUS_OFFSET] &
0951              (ATA_BUSY | ATA_DF | ATA_ERR)) {
0952             usb_stor_dbg(us, "   Status indicates it is not ready, try again...\n");
0953         }
0954         /* check for DRDY, ATA devices set DRDY after SRST */
0955         else if (regs[ATA_REG_STATUS_OFFSET] & ATA_DRDY) {
0956             usb_stor_dbg(us, "   Identified ATA device\n");
0957             info->DeviceFlags |= DF_ATA_DEVICE;
0958             info->DeviceHead = master_slave;
0959             break;
0960         } 
0961         /*
0962          * check Cylinder High/Low to
0963          * determine if it is an ATAPI device
0964          */
0965         else if (regs[ATA_REG_HCYL_OFFSET] == 0xEB &&
0966              regs[ATA_REG_LCYL_OFFSET] == 0x14) {
0967             /*
0968              * It seems that the RICOH
0969              * MP6200A CD/RW drive will
0970              * report itself okay as a
0971              * slave when it is really a
0972              * master. So this check again
0973              * as a master device just to
0974              * make sure it doesn't report
0975              * itself okay as a master also
0976              */
0977             if ((master_slave & ATA_ADDRESS_DEVHEAD_SLAVE) &&
0978                 !recheckAsMaster) {
0979                 usb_stor_dbg(us, "   Identified ATAPI device as slave.  Rechecking again as master\n");
0980                 recheckAsMaster = 1;
0981                 master_slave = ATA_ADDRESS_DEVHEAD_STD;
0982             } else {
0983                 usb_stor_dbg(us, "   Identified ATAPI device\n");
0984                 info->DeviceHead = master_slave;
0985                   
0986                 status = isd200_atapi_soft_reset(us);
0987                 break;
0988             }
0989         } else {
0990             usb_stor_dbg(us, "   Not ATA, not ATAPI - Weird\n");
0991             break;
0992         }
0993 
0994         /* check for timeout on this request */
0995         if (time_after_eq(jiffies, endTime)) {
0996             if (!detect)
0997                 usb_stor_dbg(us, "   BSY check timeout, just continue with next operation...\n");
0998             else
0999                 usb_stor_dbg(us, "   Device detect timeout!\n");
1000             break;
1001         }
1002     }
1003 
1004     return status;
1005 }
1006 
1007 /**************************************************************************
1008  * isd200_manual_enum
1009  *                                   
1010  * Determines if the drive attached is an ATA or ATAPI and if it is a
1011  * master or slave.
1012  *
1013  * RETURNS:
1014  *    ISD status code
1015  */
1016 static int isd200_manual_enum(struct us_data *us)
1017 {
1018     struct isd200_info *info = (struct isd200_info *)us->extra;
1019     int retStatus = ISD200_GOOD;
1020 
1021     usb_stor_dbg(us, "Entering isd200_manual_enum\n");
1022 
1023     retStatus = isd200_read_config(us);
1024     if (retStatus == ISD200_GOOD) {
1025         int isslave;
1026         /* master or slave? */
1027         retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 0);
1028         if (retStatus == ISD200_GOOD)
1029             retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_SLAVE, 0);
1030 
1031         if (retStatus == ISD200_GOOD) {
1032             retStatus = isd200_srst(us);
1033             if (retStatus == ISD200_GOOD)
1034                 /* ata or atapi? */
1035                 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 1);
1036         }
1037 
1038         isslave = (info->DeviceHead & ATA_ADDRESS_DEVHEAD_SLAVE) ? 1 : 0;
1039         if (!(info->ConfigData.ATAConfig & ATACFG_MASTER)) {
1040             usb_stor_dbg(us, "   Setting Master/Slave selection to %d\n",
1041                      isslave);
1042             info->ConfigData.ATAConfig &= 0x3f;
1043             info->ConfigData.ATAConfig |= (isslave<<6);
1044             retStatus = isd200_write_config(us);
1045         }
1046     }
1047 
1048     usb_stor_dbg(us, "Leaving isd200_manual_enum %08X\n", retStatus);
1049     return(retStatus);
1050 }
1051 
1052 static void isd200_fix_driveid(u16 *id)
1053 {
1054 #ifndef __LITTLE_ENDIAN
1055 # ifdef __BIG_ENDIAN
1056     int i;
1057 
1058     for (i = 0; i < ATA_ID_WORDS; i++)
1059         id[i] = __le16_to_cpu(id[i]);
1060 # else
1061 #  error "Please fix <asm/byteorder.h>"
1062 # endif
1063 #endif
1064 }
1065 
1066 static void isd200_dump_driveid(struct us_data *us, u16 *id)
1067 {
1068     usb_stor_dbg(us, "   Identify Data Structure:\n");
1069     usb_stor_dbg(us, "      config = 0x%x\n",   id[ATA_ID_CONFIG]);
1070     usb_stor_dbg(us, "      cyls = 0x%x\n",     id[ATA_ID_CYLS]);
1071     usb_stor_dbg(us, "      heads = 0x%x\n",    id[ATA_ID_HEADS]);
1072     usb_stor_dbg(us, "      track_bytes = 0x%x\n",  id[4]);
1073     usb_stor_dbg(us, "      sector_bytes = 0x%x\n", id[5]);
1074     usb_stor_dbg(us, "      sectors = 0x%x\n",  id[ATA_ID_SECTORS]);
1075     usb_stor_dbg(us, "      serial_no[0] = 0x%x\n", *(char *)&id[ATA_ID_SERNO]);
1076     usb_stor_dbg(us, "      buf_type = 0x%x\n", id[20]);
1077     usb_stor_dbg(us, "      buf_size = 0x%x\n", id[ATA_ID_BUF_SIZE]);
1078     usb_stor_dbg(us, "      ecc_bytes = 0x%x\n",    id[22]);
1079     usb_stor_dbg(us, "      fw_rev[0] = 0x%x\n",    *(char *)&id[ATA_ID_FW_REV]);
1080     usb_stor_dbg(us, "      model[0] = 0x%x\n", *(char *)&id[ATA_ID_PROD]);
1081     usb_stor_dbg(us, "      max_multsect = 0x%x\n", id[ATA_ID_MAX_MULTSECT] & 0xff);
1082     usb_stor_dbg(us, "      dword_io = 0x%x\n", id[ATA_ID_DWORD_IO]);
1083     usb_stor_dbg(us, "      capability = 0x%x\n",   id[ATA_ID_CAPABILITY] >> 8);
1084     usb_stor_dbg(us, "      tPIO = 0x%x\n",   id[ATA_ID_OLD_PIO_MODES] >> 8);
1085     usb_stor_dbg(us, "      tDMA = 0x%x\n",   id[ATA_ID_OLD_DMA_MODES] >> 8);
1086     usb_stor_dbg(us, "      field_valid = 0x%x\n",  id[ATA_ID_FIELD_VALID]);
1087     usb_stor_dbg(us, "      cur_cyls = 0x%x\n", id[ATA_ID_CUR_CYLS]);
1088     usb_stor_dbg(us, "      cur_heads = 0x%x\n",    id[ATA_ID_CUR_HEADS]);
1089     usb_stor_dbg(us, "      cur_sectors = 0x%x\n",  id[ATA_ID_CUR_SECTORS]);
1090     usb_stor_dbg(us, "      cur_capacity = 0x%x\n", ata_id_u32(id, 57));
1091     usb_stor_dbg(us, "      multsect = 0x%x\n", id[ATA_ID_MULTSECT] & 0xff);
1092     usb_stor_dbg(us, "      lba_capacity = 0x%x\n", ata_id_u32(id, ATA_ID_LBA_CAPACITY));
1093     usb_stor_dbg(us, "      command_set_1 = 0x%x\n", id[ATA_ID_COMMAND_SET_1]);
1094     usb_stor_dbg(us, "      command_set_2 = 0x%x\n", id[ATA_ID_COMMAND_SET_2]);
1095 }
1096 
1097 /**************************************************************************
1098  * isd200_get_inquiry_data
1099  *
1100  * Get inquiry data
1101  *
1102  * RETURNS:
1103  *    ISD status code
1104  */
1105 static int isd200_get_inquiry_data( struct us_data *us )
1106 {
1107     struct isd200_info *info = (struct isd200_info *)us->extra;
1108     int retStatus = ISD200_GOOD;
1109     u16 *id = info->id;
1110 
1111     usb_stor_dbg(us, "Entering isd200_get_inquiry_data\n");
1112 
1113     /* set default to Master */
1114     info->DeviceHead = ATA_ADDRESS_DEVHEAD_STD;
1115 
1116     /* attempt to manually enumerate this device */
1117     retStatus = isd200_manual_enum(us);
1118     if (retStatus == ISD200_GOOD) {
1119         int transferStatus;
1120 
1121         /* check for an ATA device */
1122         if (info->DeviceFlags & DF_ATA_DEVICE) {
1123             /* this must be an ATA device */
1124             /* perform an ATA Command Identify */
1125             transferStatus = isd200_action( us, ACTION_IDENTIFY,
1126                             id, ATA_ID_WORDS * 2);
1127             if (transferStatus != ISD200_TRANSPORT_GOOD) {
1128                 /* Error issuing ATA Command Identify */
1129                 usb_stor_dbg(us, "   Error issuing ATA Command Identify\n");
1130                 retStatus = ISD200_ERROR;
1131             } else {
1132                 /* ATA Command Identify successful */
1133                 int i;
1134                 __be16 *src;
1135                 __u16 *dest;
1136 
1137                 isd200_fix_driveid(id);
1138                 isd200_dump_driveid(us, id);
1139 
1140                 memset(&info->InquiryData, 0, sizeof(info->InquiryData));
1141 
1142                 /* Standard IDE interface only supports disks */
1143                 info->InquiryData.DeviceType = DIRECT_ACCESS_DEVICE;
1144 
1145                 /* The length must be at least 36 (5 + 31) */
1146                 info->InquiryData.AdditionalLength = 0x1F;
1147 
1148                 if (id[ATA_ID_COMMAND_SET_1] & COMMANDSET_MEDIA_STATUS) {
1149                     /* set the removable bit */
1150                     info->InquiryData.DeviceTypeModifier = DEVICE_REMOVABLE;
1151                     info->DeviceFlags |= DF_REMOVABLE_MEDIA;
1152                 }
1153 
1154                 /* Fill in vendor identification fields */
1155                 src = (__be16 *)&id[ATA_ID_PROD];
1156                 dest = (__u16*)info->InquiryData.VendorId;
1157                 for (i = 0; i < 4; i++)
1158                     dest[i] = be16_to_cpu(src[i]);
1159 
1160                 src = (__be16 *)&id[ATA_ID_PROD + 8/2];
1161                 dest = (__u16*)info->InquiryData.ProductId;
1162                 for (i=0;i<8;i++)
1163                     dest[i] = be16_to_cpu(src[i]);
1164 
1165                 src = (__be16 *)&id[ATA_ID_FW_REV];
1166                 dest = (__u16*)info->InquiryData.ProductRevisionLevel;
1167                 for (i=0;i<2;i++)
1168                     dest[i] = be16_to_cpu(src[i]);
1169 
1170                 /* determine if it supports Media Status Notification */
1171                 if (id[ATA_ID_COMMAND_SET_2] & COMMANDSET_MEDIA_STATUS) {
1172                     usb_stor_dbg(us, "   Device supports Media Status Notification\n");
1173 
1174                     /*
1175                      * Indicate that it is enabled, even
1176                      * though it is not.
1177                      * This allows the lock/unlock of the
1178                      * media to work correctly.
1179                      */
1180                     info->DeviceFlags |= DF_MEDIA_STATUS_ENABLED;
1181                 }
1182                 else
1183                     info->DeviceFlags &= ~DF_MEDIA_STATUS_ENABLED;
1184 
1185             }
1186         } else {
1187             /* 
1188              * this must be an ATAPI device 
1189              * use an ATAPI protocol (Transparent SCSI)
1190              */
1191             us->protocol_name = "Transparent SCSI";
1192             us->proto_handler = usb_stor_transparent_scsi_command;
1193 
1194             usb_stor_dbg(us, "Protocol changed to: %s\n",
1195                      us->protocol_name);
1196         
1197             /* Free driver structure */
1198             us->extra_destructor(info);
1199             kfree(info);
1200             us->extra = NULL;
1201             us->extra_destructor = NULL;
1202         }
1203     }
1204 
1205     usb_stor_dbg(us, "Leaving isd200_get_inquiry_data %08X\n", retStatus);
1206 
1207     return(retStatus);
1208 }
1209 
1210 /**************************************************************************
1211  * isd200_scsi_to_ata
1212  *                                   
1213  * Translate SCSI commands to ATA commands.
1214  *
1215  * RETURNS:
1216  *    1 if the command needs to be sent to the transport layer
1217  *    0 otherwise
1218  */
1219 static int isd200_scsi_to_ata(struct scsi_cmnd *srb, struct us_data *us,
1220                   union ata_cdb * ataCdb)
1221 {
1222     struct isd200_info *info = (struct isd200_info *)us->extra;
1223     u16 *id = info->id;
1224     int sendToTransport = 1;
1225     unsigned char sectnum, head;
1226     unsigned short cylinder;
1227     unsigned long lba;
1228     unsigned long blockCount;
1229     unsigned char senseData[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1230 
1231     memset(ataCdb, 0, sizeof(union ata_cdb));
1232 
1233     /* SCSI Command */
1234     switch (srb->cmnd[0]) {
1235     case INQUIRY:
1236         usb_stor_dbg(us, "   ATA OUT - INQUIRY\n");
1237 
1238         /* copy InquiryData */
1239         usb_stor_set_xfer_buf((unsigned char *) &info->InquiryData,
1240                 sizeof(info->InquiryData), srb);
1241         srb->result = SAM_STAT_GOOD;
1242         sendToTransport = 0;
1243         break;
1244 
1245     case MODE_SENSE:
1246         usb_stor_dbg(us, "   ATA OUT - SCSIOP_MODE_SENSE\n");
1247 
1248         /* Initialize the return buffer */
1249         usb_stor_set_xfer_buf(senseData, sizeof(senseData), srb);
1250 
1251         if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
1252         {
1253             ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1254             ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1255             ataCdb->generic.TransferBlockSize = 1;
1256             ataCdb->generic.RegisterSelect = REG_COMMAND;
1257             ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1258             isd200_srb_set_bufflen(srb, 0);
1259         } else {
1260             usb_stor_dbg(us, "   Media Status not supported, just report okay\n");
1261             srb->result = SAM_STAT_GOOD;
1262             sendToTransport = 0;
1263         }
1264         break;
1265 
1266     case TEST_UNIT_READY:
1267         usb_stor_dbg(us, "   ATA OUT - SCSIOP_TEST_UNIT_READY\n");
1268 
1269         if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
1270         {
1271             ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1272             ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1273             ataCdb->generic.TransferBlockSize = 1;
1274             ataCdb->generic.RegisterSelect = REG_COMMAND;
1275             ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1276             isd200_srb_set_bufflen(srb, 0);
1277         } else {
1278             usb_stor_dbg(us, "   Media Status not supported, just report okay\n");
1279             srb->result = SAM_STAT_GOOD;
1280             sendToTransport = 0;
1281         }
1282         break;
1283 
1284     case READ_CAPACITY:
1285     {
1286         unsigned long capacity;
1287         struct read_capacity_data readCapacityData;
1288 
1289         usb_stor_dbg(us, "   ATA OUT - SCSIOP_READ_CAPACITY\n");
1290 
1291         if (ata_id_has_lba(id))
1292             capacity = ata_id_u32(id, ATA_ID_LBA_CAPACITY) - 1;
1293         else
1294             capacity = (id[ATA_ID_HEADS] * id[ATA_ID_CYLS] *
1295                     id[ATA_ID_SECTORS]) - 1;
1296 
1297         readCapacityData.LogicalBlockAddress = cpu_to_be32(capacity);
1298         readCapacityData.BytesPerBlock = cpu_to_be32(0x200);
1299 
1300         usb_stor_set_xfer_buf((unsigned char *) &readCapacityData,
1301                 sizeof(readCapacityData), srb);
1302         srb->result = SAM_STAT_GOOD;
1303         sendToTransport = 0;
1304     }
1305     break;
1306 
1307     case READ_10:
1308         usb_stor_dbg(us, "   ATA OUT - SCSIOP_READ\n");
1309 
1310         lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1311         blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1312 
1313         if (ata_id_has_lba(id)) {
1314             sectnum = (unsigned char)(lba);
1315             cylinder = (unsigned short)(lba>>8);
1316             head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1317         } else {
1318             sectnum = (u8)((lba % id[ATA_ID_SECTORS]) + 1);
1319             cylinder = (u16)(lba / (id[ATA_ID_SECTORS] *
1320                     id[ATA_ID_HEADS]));
1321             head = (u8)((lba / id[ATA_ID_SECTORS]) %
1322                     id[ATA_ID_HEADS]);
1323         }
1324         ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1325         ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1326         ataCdb->generic.TransferBlockSize = 1;
1327         ataCdb->generic.RegisterSelect =
1328           REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
1329           REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
1330           REG_DEVICE_HEAD  | REG_COMMAND;
1331         ataCdb->write.SectorCountByte = (unsigned char)blockCount;
1332         ataCdb->write.SectorNumberByte = sectnum;
1333         ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
1334         ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
1335         ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
1336         ataCdb->write.CommandByte = ATA_CMD_PIO_READ;
1337         break;
1338 
1339     case WRITE_10:
1340         usb_stor_dbg(us, "   ATA OUT - SCSIOP_WRITE\n");
1341 
1342         lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1343         blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1344 
1345         if (ata_id_has_lba(id)) {
1346             sectnum = (unsigned char)(lba);
1347             cylinder = (unsigned short)(lba>>8);
1348             head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1349         } else {
1350             sectnum = (u8)((lba % id[ATA_ID_SECTORS]) + 1);
1351             cylinder = (u16)(lba / (id[ATA_ID_SECTORS] *
1352                     id[ATA_ID_HEADS]));
1353             head = (u8)((lba / id[ATA_ID_SECTORS]) %
1354                     id[ATA_ID_HEADS]);
1355         }
1356         ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1357         ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1358         ataCdb->generic.TransferBlockSize = 1;
1359         ataCdb->generic.RegisterSelect =
1360           REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
1361           REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
1362           REG_DEVICE_HEAD  | REG_COMMAND;
1363         ataCdb->write.SectorCountByte = (unsigned char)blockCount;
1364         ataCdb->write.SectorNumberByte = sectnum;
1365         ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
1366         ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
1367         ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
1368         ataCdb->write.CommandByte = ATA_CMD_PIO_WRITE;
1369         break;
1370 
1371     case ALLOW_MEDIUM_REMOVAL:
1372         usb_stor_dbg(us, "   ATA OUT - SCSIOP_MEDIUM_REMOVAL\n");
1373 
1374         if (info->DeviceFlags & DF_REMOVABLE_MEDIA) {
1375             usb_stor_dbg(us, "   srb->cmnd[4] = 0x%X\n",
1376                      srb->cmnd[4]);
1377         
1378             ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1379             ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1380             ataCdb->generic.TransferBlockSize = 1;
1381             ataCdb->generic.RegisterSelect = REG_COMMAND;
1382             ataCdb->write.CommandByte = (srb->cmnd[4] & 0x1) ?
1383                 ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK;
1384             isd200_srb_set_bufflen(srb, 0);
1385         } else {
1386             usb_stor_dbg(us, "   Not removable media, just report okay\n");
1387             srb->result = SAM_STAT_GOOD;
1388             sendToTransport = 0;
1389         }
1390         break;
1391 
1392     case START_STOP:    
1393         usb_stor_dbg(us, "   ATA OUT - SCSIOP_START_STOP_UNIT\n");
1394         usb_stor_dbg(us, "   srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
1395 
1396         if ((srb->cmnd[4] & 0x3) == 0x2) {
1397             usb_stor_dbg(us, "   Media Eject\n");
1398             ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1399             ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1400             ataCdb->generic.TransferBlockSize = 0;
1401             ataCdb->generic.RegisterSelect = REG_COMMAND;
1402             ataCdb->write.CommandByte = ATA_COMMAND_MEDIA_EJECT;
1403         } else if ((srb->cmnd[4] & 0x3) == 0x1) {
1404             usb_stor_dbg(us, "   Get Media Status\n");
1405             ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1406             ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1407             ataCdb->generic.TransferBlockSize = 1;
1408             ataCdb->generic.RegisterSelect = REG_COMMAND;
1409             ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1410             isd200_srb_set_bufflen(srb, 0);
1411         } else {
1412             usb_stor_dbg(us, "   Nothing to do, just report okay\n");
1413             srb->result = SAM_STAT_GOOD;
1414             sendToTransport = 0;
1415         }
1416         break;
1417 
1418     default:
1419         usb_stor_dbg(us, "Unsupported SCSI command - 0x%X\n",
1420                  srb->cmnd[0]);
1421         srb->result = DID_ERROR << 16;
1422         sendToTransport = 0;
1423         break;
1424     }
1425 
1426     return(sendToTransport);
1427 }
1428 
1429 
1430 /**************************************************************************
1431  * isd200_free_info
1432  *
1433  * Frees the driver structure.
1434  */
1435 static void isd200_free_info_ptrs(void *info_)
1436 {
1437     struct isd200_info *info = (struct isd200_info *) info_;
1438 
1439     if (info) {
1440         kfree(info->id);
1441         kfree(info->RegsBuf);
1442         kfree(info->srb.sense_buffer);
1443     }
1444 }
1445 
1446 /**************************************************************************
1447  * isd200_init_info
1448  *                                   
1449  * Allocates (if necessary) and initializes the driver structure.
1450  *
1451  * RETURNS:
1452  *    error status code
1453  */
1454 static int isd200_init_info(struct us_data *us)
1455 {
1456     struct isd200_info *info;
1457 
1458     info = kzalloc(sizeof(struct isd200_info), GFP_KERNEL);
1459     if (!info)
1460         return -ENOMEM;
1461 
1462     info->id = kzalloc(ATA_ID_WORDS * 2, GFP_KERNEL);
1463     info->RegsBuf = kmalloc(sizeof(info->ATARegs), GFP_KERNEL);
1464     info->srb.sense_buffer = kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
1465 
1466     if (!info->id || !info->RegsBuf || !info->srb.sense_buffer) {
1467         isd200_free_info_ptrs(info);
1468         kfree(info);
1469         return -ENOMEM;
1470     }
1471 
1472     us->extra = info;
1473     us->extra_destructor = isd200_free_info_ptrs;
1474 
1475     return 0;
1476 }
1477 
1478 /**************************************************************************
1479  * Initialization for the ISD200 
1480  */
1481 
1482 static int isd200_Initialization(struct us_data *us)
1483 {
1484     usb_stor_dbg(us, "ISD200 Initialization...\n");
1485 
1486     /* Initialize ISD200 info struct */
1487 
1488     if (isd200_init_info(us) == ISD200_ERROR) {
1489         usb_stor_dbg(us, "ERROR Initializing ISD200 Info struct\n");
1490     } else {
1491         /* Get device specific data */
1492 
1493         if (isd200_get_inquiry_data(us) != ISD200_GOOD)
1494             usb_stor_dbg(us, "ISD200 Initialization Failure\n");
1495         else
1496             usb_stor_dbg(us, "ISD200 Initialization complete\n");
1497     }
1498 
1499     return 0;
1500 }
1501 
1502 
1503 /**************************************************************************
1504  * Protocol and Transport for the ISD200 ASIC
1505  *
1506  * This protocol and transport are for ATA devices connected to an ISD200
1507  * ASIC.  An ATAPI device that is connected as a slave device will be
1508  * detected in the driver initialization function and the protocol will
1509  * be changed to an ATAPI protocol (Transparent SCSI).
1510  *
1511  */
1512 
1513 static void isd200_ata_command(struct scsi_cmnd *srb, struct us_data *us)
1514 {
1515     int sendToTransport, orig_bufflen;
1516     union ata_cdb ataCdb;
1517 
1518     /* Make sure driver was initialized */
1519 
1520     if (us->extra == NULL) {
1521         usb_stor_dbg(us, "ERROR Driver not initialized\n");
1522         srb->result = DID_ERROR << 16;
1523         return;
1524     }
1525 
1526     scsi_set_resid(srb, 0);
1527     /* scsi_bufflen might change in protocol translation to ata */
1528     orig_bufflen = scsi_bufflen(srb);
1529     sendToTransport = isd200_scsi_to_ata(srb, us, &ataCdb);
1530 
1531     /* send the command to the transport layer */
1532     if (sendToTransport)
1533         isd200_invoke_transport(us, srb, &ataCdb);
1534 
1535     isd200_srb_set_bufflen(srb, orig_bufflen);
1536 }
1537 
1538 static struct scsi_host_template isd200_host_template;
1539 
1540 static int isd200_probe(struct usb_interface *intf,
1541              const struct usb_device_id *id)
1542 {
1543     struct us_data *us;
1544     int result;
1545 
1546     result = usb_stor_probe1(&us, intf, id,
1547             (id - isd200_usb_ids) + isd200_unusual_dev_list,
1548             &isd200_host_template);
1549     if (result)
1550         return result;
1551 
1552     us->protocol_name = "ISD200 ATA/ATAPI";
1553     us->proto_handler = isd200_ata_command;
1554 
1555     result = usb_stor_probe2(us);
1556     return result;
1557 }
1558 
1559 static struct usb_driver isd200_driver = {
1560     .name =     DRV_NAME,
1561     .probe =    isd200_probe,
1562     .disconnect =   usb_stor_disconnect,
1563     .suspend =  usb_stor_suspend,
1564     .resume =   usb_stor_resume,
1565     .reset_resume = usb_stor_reset_resume,
1566     .pre_reset =    usb_stor_pre_reset,
1567     .post_reset =   usb_stor_post_reset,
1568     .id_table = isd200_usb_ids,
1569     .soft_unbind =  1,
1570     .no_dynamic_id = 1,
1571 };
1572 
1573 module_usb_stor_driver(isd200_driver, isd200_host_template, DRV_NAME);