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