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