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