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