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