1 /* Transport & Protocol Driver for In-System Design, Inc. ISD200 ASIC
3 * Current development and maintenance:
4 * (C) 2001-2002 Björn Stenberg (bjorn@haxx.se)
6 * Developed with the assistance of:
7 * (C) 2002 Alan Stern <stern@rowland.org>
10 * (C) 2000 In-System Design, Inc. (support@in-system.com)
12 * The ISD200 ASIC does not natively support ATA devices. The chip
13 * does implement an interface, the ATA Command Block (ATACB) which provides
14 * a means of passing ATA commands and ATA register accesses to a device.
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 2, or (at your option) any
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 675 Mass Ave, Cambridge, MA 02139, USA.
32 * 2002-10-19: Removed the specialized transfer routines.
33 * (Alan Stern <stern@rowland.harvard.edu>)
34 * 2001-02-24: Removed lots of duplicate code and simplified the structure.
36 * 2002-01-16: Fixed endianness bug so it works on the ppc arch.
37 * (Luc Saillard <luc@saillard.org>)
38 * 2002-01-17: All bitfields removed.
45 #include <linux/jiffies.h>
46 #include <linux/errno.h>
47 #include <linux/module.h>
48 #include <linux/slab.h>
49 #include <linux/hdreg.h>
50 #include <linux/scatterlist.h>
52 #include <scsi/scsi.h>
53 #include <scsi/scsi_cmnd.h>
54 #include <scsi/scsi_device.h>
57 #include "transport.h"
62 MODULE_DESCRIPTION("Driver for In-System Design, Inc. ISD200 ASIC");
63 MODULE_AUTHOR("Björn Stenberg <bjorn@haxx.se>");
64 MODULE_LICENSE("GPL");
66 static int isd200_Initialization(struct us_data *us);
70 * The table of devices
72 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
73 vendorName, productName, useProtocol, useTransport, \
74 initFunction, flags) \
75 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
76 .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
78 struct usb_device_id isd200_usb_ids[] = {
79 # include "unusual_isd200.h"
80 { } /* Terminating entry */
82 MODULE_DEVICE_TABLE(usb, isd200_usb_ids);
90 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
91 vendor_name, product_name, use_protocol, use_transport, \
92 init_function, Flags) \
94 .vendorName = vendor_name, \
95 .productName = product_name, \
96 .useProtocol = use_protocol, \
97 .useTransport = use_transport, \
98 .initFunction = init_function, \
101 static struct us_unusual_dev isd200_unusual_dev_list[] = {
102 # include "unusual_isd200.h"
103 { } /* Terminating entry */
110 /* Timeout defines (in Seconds) */
112 #define ISD200_ENUM_BSY_TIMEOUT 35
113 #define ISD200_ENUM_DETECT_TIMEOUT 30
114 #define ISD200_DEFAULT_TIMEOUT 30
117 #define DF_ATA_DEVICE 0x0001
118 #define DF_MEDIA_STATUS_ENABLED 0x0002
119 #define DF_REMOVABLE_MEDIA 0x0004
121 /* capability bit definitions */
122 #define CAPABILITY_DMA 0x01
123 #define CAPABILITY_LBA 0x02
125 /* command_setX bit definitions */
126 #define COMMANDSET_REMOVABLE 0x02
127 #define COMMANDSET_MEDIA_STATUS 0x10
129 /* ATA Vendor Specific defines */
130 #define ATA_ADDRESS_DEVHEAD_STD 0xa0
131 #define ATA_ADDRESS_DEVHEAD_LBA_MODE 0x40
132 #define ATA_ADDRESS_DEVHEAD_SLAVE 0x10
134 /* Action Select bits */
135 #define ACTION_SELECT_0 0x01
136 #define ACTION_SELECT_1 0x02
137 #define ACTION_SELECT_2 0x04
138 #define ACTION_SELECT_3 0x08
139 #define ACTION_SELECT_4 0x10
140 #define ACTION_SELECT_5 0x20
141 #define ACTION_SELECT_6 0x40
142 #define ACTION_SELECT_7 0x80
144 /* Register Select bits */
145 #define REG_ALTERNATE_STATUS 0x01
146 #define REG_DEVICE_CONTROL 0x01
147 #define REG_ERROR 0x02
148 #define REG_FEATURES 0x02
149 #define REG_SECTOR_COUNT 0x04
150 #define REG_SECTOR_NUMBER 0x08
151 #define REG_CYLINDER_LOW 0x10
152 #define REG_CYLINDER_HIGH 0x20
153 #define REG_DEVICE_HEAD 0x40
154 #define REG_STATUS 0x80
155 #define REG_COMMAND 0x80
157 /* ATA registers offset definitions */
158 #define ATA_REG_ERROR_OFFSET 1
159 #define ATA_REG_LCYL_OFFSET 4
160 #define ATA_REG_HCYL_OFFSET 5
161 #define ATA_REG_STATUS_OFFSET 7
163 /* ATA error definitions not in <linux/hdreg.h> */
164 #define ATA_ERROR_MEDIA_CHANGE 0x20
166 /* ATA command definitions not in <linux/hdreg.h> */
167 #define ATA_COMMAND_GET_MEDIA_STATUS 0xDA
168 #define ATA_COMMAND_MEDIA_EJECT 0xED
170 /* ATA drive control definitions */
171 #define ATA_DC_DISABLE_INTERRUPTS 0x02
172 #define ATA_DC_RESET_CONTROLLER 0x04
173 #define ATA_DC_REENABLE_CONTROLLER 0x00
176 * General purpose return codes
179 #define ISD200_ERROR -1
180 #define ISD200_GOOD 0
183 * Transport return codes
186 #define ISD200_TRANSPORT_GOOD 0 /* Transport good, command good */
187 #define ISD200_TRANSPORT_FAILED 1 /* Transport good, command failed */
188 #define ISD200_TRANSPORT_ERROR 2 /* Transport bad (i.e. device dead) */
190 /* driver action codes */
191 #define ACTION_READ_STATUS 0
192 #define ACTION_RESET 1
193 #define ACTION_REENABLE 2
194 #define ACTION_SOFT_RESET 3
195 #define ACTION_ENUM 4
196 #define ACTION_IDENTIFY 5
206 unsigned char SignatureByte0;
207 unsigned char SignatureByte1;
208 unsigned char ActionSelect;
209 unsigned char RegisterSelect;
210 unsigned char TransferBlockSize;
211 unsigned char WriteData3F6;
212 unsigned char WriteData1F1;
213 unsigned char WriteData1F2;
214 unsigned char WriteData1F3;
215 unsigned char WriteData1F4;
216 unsigned char WriteData1F5;
217 unsigned char WriteData1F6;
218 unsigned char WriteData1F7;
219 unsigned char Reserved[3];
223 unsigned char SignatureByte0;
224 unsigned char SignatureByte1;
225 unsigned char ActionSelect;
226 unsigned char RegisterSelect;
227 unsigned char TransferBlockSize;
228 unsigned char AlternateStatusByte;
229 unsigned char ErrorByte;
230 unsigned char SectorCountByte;
231 unsigned char SectorNumberByte;
232 unsigned char CylinderLowByte;
233 unsigned char CylinderHighByte;
234 unsigned char DeviceHeadByte;
235 unsigned char StatusByte;
236 unsigned char Reserved[3];
240 unsigned char SignatureByte0;
241 unsigned char SignatureByte1;
242 unsigned char ActionSelect;
243 unsigned char RegisterSelect;
244 unsigned char TransferBlockSize;
245 unsigned char DeviceControlByte;
246 unsigned char FeaturesByte;
247 unsigned char SectorCountByte;
248 unsigned char SectorNumberByte;
249 unsigned char CylinderLowByte;
250 unsigned char CylinderHighByte;
251 unsigned char DeviceHeadByte;
252 unsigned char CommandByte;
253 unsigned char Reserved[3];
259 * Inquiry data structure. This is the data returned from the target
260 * after it receives an inquiry.
262 * This structure may be extended by the number of bytes specified
263 * in the field AdditionalLength. The defined size constant only
264 * includes fields through ProductRevisionLevel.
270 #define DIRECT_ACCESS_DEVICE 0x00 /* disks */
271 #define DEVICE_REMOVABLE 0x80
273 struct inquiry_data {
274 unsigned char DeviceType;
275 unsigned char DeviceTypeModifier;
276 unsigned char Versions;
277 unsigned char Format;
278 unsigned char AdditionalLength;
279 unsigned char Reserved[2];
280 unsigned char Capability;
281 unsigned char VendorId[8];
282 unsigned char ProductId[16];
283 unsigned char ProductRevisionLevel[4];
284 unsigned char VendorSpecific[20];
285 unsigned char Reserved3[40];
286 } __attribute__ ((packed));
289 * INQUIRY data buffer size
292 #define INQUIRYDATABUFFERSIZE 36
296 * ISD200 CONFIG data struct
299 #define ATACFG_TIMING 0x0f
300 #define ATACFG_ATAPI_RESET 0x10
301 #define ATACFG_MASTER 0x20
302 #define ATACFG_BLOCKSIZE 0xa0
304 #define ATACFGE_LAST_LUN 0x07
305 #define ATACFGE_DESC_OVERRIDE 0x08
306 #define ATACFGE_STATE_SUSPEND 0x10
307 #define ATACFGE_SKIP_BOOT 0x20
308 #define ATACFGE_CONF_DESC2 0x40
309 #define ATACFGE_INIT_STATUS 0x80
311 #define CFG_CAPABILITY_SRST 0x01
313 struct isd200_config {
314 unsigned char EventNotification;
315 unsigned char ExternalClock;
316 unsigned char ATAInitTimeout;
317 unsigned char ATAConfig;
318 unsigned char ATAMajorCommand;
319 unsigned char ATAMinorCommand;
320 unsigned char ATAExtraConfig;
321 unsigned char Capability;
322 }__attribute__ ((packed));
326 * ISD200 driver information struct
330 struct inquiry_data InquiryData;
331 struct hd_driveid *id;
332 struct isd200_config ConfigData;
333 unsigned char *RegsBuf;
334 unsigned char ATARegs[8];
335 unsigned char DeviceHead;
336 unsigned char DeviceFlags;
338 /* maximum number of LUNs supported */
339 unsigned char MaxLUNs;
340 unsigned char cmnd[BLK_MAX_CDB];
341 struct scsi_cmnd srb;
342 struct scatterlist sg;
347 * Read Capacity Data - returned in Big Endian format
350 struct read_capacity_data {
351 __be32 LogicalBlockAddress;
352 __be32 BytesPerBlock;
356 * Read Block Limits Data - returned in Big Endian format
357 * This structure returns the maximum and minimum block
358 * size for a TAPE device.
361 struct read_block_limits {
362 unsigned char Reserved;
363 unsigned char BlockMaximumSize[3];
364 unsigned char BlockMinimumSize[2];
372 #define SENSE_ERRCODE 0x7f
373 #define SENSE_ERRCODE_VALID 0x80
374 #define SENSE_FLAG_SENSE_KEY 0x0f
375 #define SENSE_FLAG_BAD_LENGTH 0x20
376 #define SENSE_FLAG_END_OF_MEDIA 0x40
377 #define SENSE_FLAG_FILE_MARK 0x80
379 unsigned char ErrorCode;
380 unsigned char SegmentNumber;
382 unsigned char Information[4];
383 unsigned char AdditionalSenseLength;
384 unsigned char CommandSpecificInformation[4];
385 unsigned char AdditionalSenseCode;
386 unsigned char AdditionalSenseCodeQualifier;
387 unsigned char FieldReplaceableUnitCode;
388 unsigned char SenseKeySpecific[3];
389 } __attribute__ ((packed));
392 * Default request sense buffer size
395 #define SENSE_BUFFER_SIZE 18
397 /***********************************************************************
399 ***********************************************************************/
401 /**************************************************************************
404 * Builds an artificial sense buffer to report the results of a
410 static void isd200_build_sense(struct us_data *us, struct scsi_cmnd *srb)
412 struct isd200_info *info = (struct isd200_info *)us->extra;
413 struct sense_data *buf = (struct sense_data *) &srb->sense_buffer[0];
414 unsigned char error = info->ATARegs[ATA_REG_ERROR_OFFSET];
416 if(error & ATA_ERROR_MEDIA_CHANGE) {
417 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
418 buf->AdditionalSenseLength = 0xb;
419 buf->Flags = UNIT_ATTENTION;
420 buf->AdditionalSenseCode = 0;
421 buf->AdditionalSenseCodeQualifier = 0;
422 } else if(error & MCR_ERR) {
423 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
424 buf->AdditionalSenseLength = 0xb;
425 buf->Flags = UNIT_ATTENTION;
426 buf->AdditionalSenseCode = 0;
427 buf->AdditionalSenseCodeQualifier = 0;
428 } else if(error & TRK0_ERR) {
429 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
430 buf->AdditionalSenseLength = 0xb;
431 buf->Flags = NOT_READY;
432 buf->AdditionalSenseCode = 0;
433 buf->AdditionalSenseCodeQualifier = 0;
434 } else if(error & ECC_ERR) {
435 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
436 buf->AdditionalSenseLength = 0xb;
437 buf->Flags = DATA_PROTECT;
438 buf->AdditionalSenseCode = 0;
439 buf->AdditionalSenseCodeQualifier = 0;
442 buf->AdditionalSenseLength = 0;
444 buf->AdditionalSenseCode = 0;
445 buf->AdditionalSenseCodeQualifier = 0;
450 /***********************************************************************
452 ***********************************************************************/
454 /**************************************************************************
455 * isd200_set_srb(), isd200_srb_set_bufflen()
457 * Two helpers to facilitate in initialization of scsi_cmnd structure
458 * Will need to change when struct scsi_cmnd changes
460 static void isd200_set_srb(struct isd200_info *info,
461 enum dma_data_direction dir, void* buff, unsigned bufflen)
463 struct scsi_cmnd *srb = &info->srb;
466 sg_init_one(&info->sg, buff, bufflen);
468 srb->sc_data_direction = dir;
469 srb->sdb.table.sgl = buff ? &info->sg : NULL;
470 srb->sdb.length = bufflen;
471 srb->sdb.table.nents = buff ? 1 : 0;
474 static void isd200_srb_set_bufflen(struct scsi_cmnd *srb, unsigned bufflen)
476 srb->sdb.length = bufflen;
480 /**************************************************************************
483 * Routine for sending commands to the isd200
488 static int isd200_action( struct us_data *us, int action,
489 void* pointer, int value )
492 struct scsi_device srb_dev;
493 struct isd200_info *info = (struct isd200_info *)us->extra;
494 struct scsi_cmnd *srb = &info->srb;
497 memset(&ata, 0, sizeof(ata));
498 memset(&srb_dev, 0, sizeof(srb_dev));
499 srb->cmnd = info->cmnd;
500 srb->device = &srb_dev;
501 ++srb->serial_number;
503 ata.generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
504 ata.generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
505 ata.generic.TransferBlockSize = 1;
508 case ACTION_READ_STATUS:
509 US_DEBUGP(" isd200_action(READ_STATUS)\n");
510 ata.generic.ActionSelect = ACTION_SELECT_0|ACTION_SELECT_2;
511 ata.generic.RegisterSelect =
512 REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
513 REG_STATUS | REG_ERROR;
514 isd200_set_srb(info, DMA_FROM_DEVICE, pointer, value);
518 US_DEBUGP(" isd200_action(ENUM,0x%02x)\n",value);
519 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
520 ACTION_SELECT_3|ACTION_SELECT_4|
522 ata.generic.RegisterSelect = REG_DEVICE_HEAD;
523 ata.write.DeviceHeadByte = value;
524 isd200_set_srb(info, DMA_NONE, NULL, 0);
528 US_DEBUGP(" isd200_action(RESET)\n");
529 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
530 ACTION_SELECT_3|ACTION_SELECT_4;
531 ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
532 ata.write.DeviceControlByte = ATA_DC_RESET_CONTROLLER;
533 isd200_set_srb(info, DMA_NONE, NULL, 0);
536 case ACTION_REENABLE:
537 US_DEBUGP(" isd200_action(REENABLE)\n");
538 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
539 ACTION_SELECT_3|ACTION_SELECT_4;
540 ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
541 ata.write.DeviceControlByte = ATA_DC_REENABLE_CONTROLLER;
542 isd200_set_srb(info, DMA_NONE, NULL, 0);
545 case ACTION_SOFT_RESET:
546 US_DEBUGP(" isd200_action(SOFT_RESET)\n");
547 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_5;
548 ata.generic.RegisterSelect = REG_DEVICE_HEAD | REG_COMMAND;
549 ata.write.DeviceHeadByte = info->DeviceHead;
550 ata.write.CommandByte = WIN_SRST;
551 isd200_set_srb(info, DMA_NONE, NULL, 0);
554 case ACTION_IDENTIFY:
555 US_DEBUGP(" isd200_action(IDENTIFY)\n");
556 ata.generic.RegisterSelect = REG_COMMAND;
557 ata.write.CommandByte = WIN_IDENTIFY;
558 isd200_set_srb(info, DMA_FROM_DEVICE, info->id,
559 sizeof(struct hd_driveid));
563 US_DEBUGP("Error: Undefined action %d\n",action);
567 memcpy(srb->cmnd, &ata, sizeof(ata.generic));
568 srb->cmd_len = sizeof(ata.generic);
569 status = usb_stor_Bulk_transport(srb, us);
570 if (status == USB_STOR_TRANSPORT_GOOD)
571 status = ISD200_GOOD;
573 US_DEBUGP(" isd200_action(0x%02x) error: %d\n",action,status);
574 status = ISD200_ERROR;
575 /* need to reset device here */
581 /**************************************************************************
589 static int isd200_read_regs( struct us_data *us )
591 struct isd200_info *info = (struct isd200_info *)us->extra;
592 int retStatus = ISD200_GOOD;
595 US_DEBUGP("Entering isd200_IssueATAReadRegs\n");
597 transferStatus = isd200_action( us, ACTION_READ_STATUS,
598 info->RegsBuf, sizeof(info->ATARegs) );
599 if (transferStatus != ISD200_TRANSPORT_GOOD) {
600 US_DEBUGP(" Error reading ATA registers\n");
601 retStatus = ISD200_ERROR;
603 memcpy(info->ATARegs, info->RegsBuf, sizeof(info->ATARegs));
604 US_DEBUGP(" Got ATA Register[ATA_REG_ERROR_OFFSET] = 0x%x\n",
605 info->ATARegs[ATA_REG_ERROR_OFFSET]);
612 /**************************************************************************
613 * Invoke the transport and basic error-handling/recovery methods
615 * This is used by the protocol layers to actually send the message to
616 * the device and receive the response.
618 static void isd200_invoke_transport( struct us_data *us,
619 struct scsi_cmnd *srb,
620 union ata_cdb *ataCdb )
622 int need_auto_sense = 0;
626 /* send the command to the transport layer */
627 memcpy(srb->cmnd, ataCdb, sizeof(ataCdb->generic));
628 srb->cmd_len = sizeof(ataCdb->generic);
629 transferStatus = usb_stor_Bulk_transport(srb, us);
631 /* if the command gets aborted by the higher layers, we need to
632 * short-circuit all other processing
634 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
635 US_DEBUGP("-- command was aborted\n");
639 switch (transferStatus) {
641 case USB_STOR_TRANSPORT_GOOD:
642 /* Indicate a good result */
643 srb->result = SAM_STAT_GOOD;
646 case USB_STOR_TRANSPORT_NO_SENSE:
647 US_DEBUGP("-- transport indicates protocol failure\n");
648 srb->result = SAM_STAT_CHECK_CONDITION;
651 case USB_STOR_TRANSPORT_FAILED:
652 US_DEBUGP("-- transport indicates command failure\n");
656 case USB_STOR_TRANSPORT_ERROR:
657 US_DEBUGP("-- transport indicates transport error\n");
658 srb->result = DID_ERROR << 16;
659 /* Need reset here */
663 US_DEBUGP("-- transport indicates unknown error\n");
664 srb->result = DID_ERROR << 16;
665 /* Need reset here */
669 if ((scsi_get_resid(srb) > 0) &&
670 !((srb->cmnd[0] == REQUEST_SENSE) ||
671 (srb->cmnd[0] == INQUIRY) ||
672 (srb->cmnd[0] == MODE_SENSE) ||
673 (srb->cmnd[0] == LOG_SENSE) ||
674 (srb->cmnd[0] == MODE_SENSE_10))) {
675 US_DEBUGP("-- unexpectedly short transfer\n");
679 if (need_auto_sense) {
680 result = isd200_read_regs(us);
681 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
682 US_DEBUGP("-- auto-sense aborted\n");
685 if (result == ISD200_GOOD) {
686 isd200_build_sense(us, srb);
687 srb->result = SAM_STAT_CHECK_CONDITION;
689 /* If things are really okay, then let's show that */
690 if ((srb->sense_buffer[2] & 0xf) == 0x0)
691 srb->result = SAM_STAT_GOOD;
693 srb->result = DID_ERROR << 16;
694 /* Need reset here */
698 /* Regardless of auto-sense, if we _know_ we have an error
699 * condition, show that in the result code
701 if (transferStatus == USB_STOR_TRANSPORT_FAILED)
702 srb->result = SAM_STAT_CHECK_CONDITION;
705 /* abort processing: the bulk-only transport requires a reset
706 * following an abort */
708 srb->result = DID_ABORT << 16;
710 /* permit the reset transfer to take place */
711 clear_bit(US_FLIDX_ABORTING, &us->dflags);
712 /* Need reset here */
715 #ifdef CONFIG_USB_STORAGE_DEBUG
716 static void isd200_log_config( struct isd200_info* info )
718 US_DEBUGP(" Event Notification: 0x%x\n",
719 info->ConfigData.EventNotification);
720 US_DEBUGP(" External Clock: 0x%x\n",
721 info->ConfigData.ExternalClock);
722 US_DEBUGP(" ATA Init Timeout: 0x%x\n",
723 info->ConfigData.ATAInitTimeout);
724 US_DEBUGP(" ATAPI Command Block Size: 0x%x\n",
725 (info->ConfigData.ATAConfig & ATACFG_BLOCKSIZE) >> 6);
726 US_DEBUGP(" Master/Slave Selection: 0x%x\n",
727 info->ConfigData.ATAConfig & ATACFG_MASTER);
728 US_DEBUGP(" ATAPI Reset: 0x%x\n",
729 info->ConfigData.ATAConfig & ATACFG_ATAPI_RESET);
730 US_DEBUGP(" ATA Timing: 0x%x\n",
731 info->ConfigData.ATAConfig & ATACFG_TIMING);
732 US_DEBUGP(" ATA Major Command: 0x%x\n",
733 info->ConfigData.ATAMajorCommand);
734 US_DEBUGP(" ATA Minor Command: 0x%x\n",
735 info->ConfigData.ATAMinorCommand);
736 US_DEBUGP(" Init Status: 0x%x\n",
737 info->ConfigData.ATAExtraConfig & ATACFGE_INIT_STATUS);
738 US_DEBUGP(" Config Descriptor 2: 0x%x\n",
739 info->ConfigData.ATAExtraConfig & ATACFGE_CONF_DESC2);
740 US_DEBUGP(" Skip Device Boot: 0x%x\n",
741 info->ConfigData.ATAExtraConfig & ATACFGE_SKIP_BOOT);
742 US_DEBUGP(" ATA 3 State Supsend: 0x%x\n",
743 info->ConfigData.ATAExtraConfig & ATACFGE_STATE_SUSPEND);
744 US_DEBUGP(" Descriptor Override: 0x%x\n",
745 info->ConfigData.ATAExtraConfig & ATACFGE_DESC_OVERRIDE);
746 US_DEBUGP(" Last LUN Identifier: 0x%x\n",
747 info->ConfigData.ATAExtraConfig & ATACFGE_LAST_LUN);
748 US_DEBUGP(" SRST Enable: 0x%x\n",
749 info->ConfigData.ATAExtraConfig & CFG_CAPABILITY_SRST);
753 /**************************************************************************
754 * isd200_write_config
756 * Write the ISD200 Configuration data
761 static int isd200_write_config( struct us_data *us )
763 struct isd200_info *info = (struct isd200_info *)us->extra;
764 int retStatus = ISD200_GOOD;
767 #ifdef CONFIG_USB_STORAGE_DEBUG
768 US_DEBUGP("Entering isd200_write_config\n");
769 US_DEBUGP(" Writing the following ISD200 Config Data:\n");
770 isd200_log_config(info);
773 /* let's send the command via the control pipe */
774 result = usb_stor_ctrl_transfer(
778 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
781 (void *) &info->ConfigData,
782 sizeof(info->ConfigData));
785 US_DEBUGP(" ISD200 Config Data was written successfully\n");
787 US_DEBUGP(" Request to write ISD200 Config Data failed!\n");
788 retStatus = ISD200_ERROR;
791 US_DEBUGP("Leaving isd200_write_config %08X\n", retStatus);
796 /**************************************************************************
799 * Reads the ISD200 Configuration data
804 static int isd200_read_config( struct us_data *us )
806 struct isd200_info *info = (struct isd200_info *)us->extra;
807 int retStatus = ISD200_GOOD;
810 US_DEBUGP("Entering isd200_read_config\n");
812 /* read the configuration information from ISD200. Use this to */
813 /* determine what the special ATA CDB bytes are. */
815 result = usb_stor_ctrl_transfer(
819 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
822 (void *) &info->ConfigData,
823 sizeof(info->ConfigData));
827 US_DEBUGP(" Retrieved the following ISD200 Config Data:\n");
828 #ifdef CONFIG_USB_STORAGE_DEBUG
829 isd200_log_config(info);
832 US_DEBUGP(" Request to get ISD200 Config Data failed!\n");
833 retStatus = ISD200_ERROR;
836 US_DEBUGP("Leaving isd200_read_config %08X\n", retStatus);
841 /**************************************************************************
842 * isd200_atapi_soft_reset
844 * Perform an Atapi Soft Reset on the device
849 static int isd200_atapi_soft_reset( struct us_data *us )
851 int retStatus = ISD200_GOOD;
854 US_DEBUGP("Entering isd200_atapi_soft_reset\n");
856 transferStatus = isd200_action( us, ACTION_SOFT_RESET, NULL, 0 );
857 if (transferStatus != ISD200_TRANSPORT_GOOD) {
858 US_DEBUGP(" Error issuing Atapi Soft Reset\n");
859 retStatus = ISD200_ERROR;
862 US_DEBUGP("Leaving isd200_atapi_soft_reset %08X\n", retStatus);
867 /**************************************************************************
870 * Perform an SRST on the device
875 static int isd200_srst( struct us_data *us )
877 int retStatus = ISD200_GOOD;
880 US_DEBUGP("Entering isd200_SRST\n");
882 transferStatus = isd200_action( us, ACTION_RESET, NULL, 0 );
884 /* check to see if this request failed */
885 if (transferStatus != ISD200_TRANSPORT_GOOD) {
886 US_DEBUGP(" Error issuing SRST\n");
887 retStatus = ISD200_ERROR;
889 /* delay 10ms to give the drive a chance to see it */
892 transferStatus = isd200_action( us, ACTION_REENABLE, NULL, 0 );
893 if (transferStatus != ISD200_TRANSPORT_GOOD) {
894 US_DEBUGP(" Error taking drive out of reset\n");
895 retStatus = ISD200_ERROR;
897 /* delay 50ms to give the drive a chance to recover after SRST */
902 US_DEBUGP("Leaving isd200_srst %08X\n", retStatus);
907 /**************************************************************************
910 * Helper function for isd200_manual_enum(). Does ENUM and READ_STATUS
911 * and tries to analyze the status registers
916 static int isd200_try_enum(struct us_data *us, unsigned char master_slave,
919 int status = ISD200_GOOD;
920 unsigned long endTime;
921 struct isd200_info *info = (struct isd200_info *)us->extra;
922 unsigned char *regs = info->RegsBuf;
923 int recheckAsMaster = 0;
926 endTime = jiffies + ISD200_ENUM_DETECT_TIMEOUT * HZ;
928 endTime = jiffies + ISD200_ENUM_BSY_TIMEOUT * HZ;
930 /* loop until we detect !BSY or timeout */
932 #ifdef CONFIG_USB_STORAGE_DEBUG
933 char* mstr = master_slave == ATA_ADDRESS_DEVHEAD_STD ?
937 status = isd200_action( us, ACTION_ENUM, NULL, master_slave );
938 if ( status != ISD200_GOOD )
941 status = isd200_action( us, ACTION_READ_STATUS,
943 if ( status != ISD200_GOOD )
947 if (regs[ATA_REG_STATUS_OFFSET] & BUSY_STAT) {
948 US_DEBUGP(" %s status is still BSY, try again...\n",mstr);
950 US_DEBUGP(" %s status !BSY, continue with next operation\n",mstr);
954 /* check for BUSY_STAT and */
955 /* WRERR_STAT (workaround ATA Zip drive) and */
956 /* ERR_STAT (workaround for Archos CD-ROM) */
957 else if (regs[ATA_REG_STATUS_OFFSET] &
958 (BUSY_STAT | WRERR_STAT | ERR_STAT )) {
959 US_DEBUGP(" Status indicates it is not ready, try again...\n");
961 /* check for DRDY, ATA devices set DRDY after SRST */
962 else if (regs[ATA_REG_STATUS_OFFSET] & READY_STAT) {
963 US_DEBUGP(" Identified ATA device\n");
964 info->DeviceFlags |= DF_ATA_DEVICE;
965 info->DeviceHead = master_slave;
968 /* check Cylinder High/Low to
969 determine if it is an ATAPI device
971 else if (regs[ATA_REG_HCYL_OFFSET] == 0xEB &&
972 regs[ATA_REG_LCYL_OFFSET] == 0x14) {
973 /* It seems that the RICOH
974 MP6200A CD/RW drive will
975 report itself okay as a
976 slave when it is really a
977 master. So this check again
978 as a master device just to
979 make sure it doesn't report
980 itself okay as a master also
982 if ((master_slave & ATA_ADDRESS_DEVHEAD_SLAVE) &&
984 US_DEBUGP(" Identified ATAPI device as slave. Rechecking again as master\n");
986 master_slave = ATA_ADDRESS_DEVHEAD_STD;
988 US_DEBUGP(" Identified ATAPI device\n");
989 info->DeviceHead = master_slave;
991 status = isd200_atapi_soft_reset(us);
995 US_DEBUGP(" Not ATA, not ATAPI. Weird.\n");
999 /* check for timeout on this request */
1000 if (time_after_eq(jiffies, endTime)) {
1002 US_DEBUGP(" BSY check timeout, just continue with next operation...\n");
1004 US_DEBUGP(" Device detect timeout!\n");
1012 /**************************************************************************
1013 * isd200_manual_enum
1015 * Determines if the drive attached is an ATA or ATAPI and if it is a
1021 static int isd200_manual_enum(struct us_data *us)
1023 struct isd200_info *info = (struct isd200_info *)us->extra;
1024 int retStatus = ISD200_GOOD;
1026 US_DEBUGP("Entering isd200_manual_enum\n");
1028 retStatus = isd200_read_config(us);
1029 if (retStatus == ISD200_GOOD) {
1031 /* master or slave? */
1032 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 0);
1033 if (retStatus == ISD200_GOOD)
1034 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_SLAVE, 0);
1036 if (retStatus == ISD200_GOOD) {
1037 retStatus = isd200_srst(us);
1038 if (retStatus == ISD200_GOOD)
1040 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 1);
1043 isslave = (info->DeviceHead & ATA_ADDRESS_DEVHEAD_SLAVE) ? 1 : 0;
1044 if (!(info->ConfigData.ATAConfig & ATACFG_MASTER)) {
1045 US_DEBUGP(" Setting Master/Slave selection to %d\n", isslave);
1046 info->ConfigData.ATAConfig &= 0x3f;
1047 info->ConfigData.ATAConfig |= (isslave<<6);
1048 retStatus = isd200_write_config(us);
1052 US_DEBUGP("Leaving isd200_manual_enum %08X\n", retStatus);
1056 static void isd200_fix_driveid (struct hd_driveid *id)
1058 #ifndef __LITTLE_ENDIAN
1059 # ifdef __BIG_ENDIAN
1063 id->config = __le16_to_cpu(id->config);
1064 id->cyls = __le16_to_cpu(id->cyls);
1065 id->reserved2 = __le16_to_cpu(id->reserved2);
1066 id->heads = __le16_to_cpu(id->heads);
1067 id->track_bytes = __le16_to_cpu(id->track_bytes);
1068 id->sector_bytes = __le16_to_cpu(id->sector_bytes);
1069 id->sectors = __le16_to_cpu(id->sectors);
1070 id->vendor0 = __le16_to_cpu(id->vendor0);
1071 id->vendor1 = __le16_to_cpu(id->vendor1);
1072 id->vendor2 = __le16_to_cpu(id->vendor2);
1073 stringcast = (u16 *)&id->serial_no[0];
1074 for (i = 0; i < (20/2); i++)
1075 stringcast[i] = __le16_to_cpu(stringcast[i]);
1076 id->buf_type = __le16_to_cpu(id->buf_type);
1077 id->buf_size = __le16_to_cpu(id->buf_size);
1078 id->ecc_bytes = __le16_to_cpu(id->ecc_bytes);
1079 stringcast = (u16 *)&id->fw_rev[0];
1080 for (i = 0; i < (8/2); i++)
1081 stringcast[i] = __le16_to_cpu(stringcast[i]);
1082 stringcast = (u16 *)&id->model[0];
1083 for (i = 0; i < (40/2); i++)
1084 stringcast[i] = __le16_to_cpu(stringcast[i]);
1085 id->dword_io = __le16_to_cpu(id->dword_io);
1086 id->reserved50 = __le16_to_cpu(id->reserved50);
1087 id->field_valid = __le16_to_cpu(id->field_valid);
1088 id->cur_cyls = __le16_to_cpu(id->cur_cyls);
1089 id->cur_heads = __le16_to_cpu(id->cur_heads);
1090 id->cur_sectors = __le16_to_cpu(id->cur_sectors);
1091 id->cur_capacity0 = __le16_to_cpu(id->cur_capacity0);
1092 id->cur_capacity1 = __le16_to_cpu(id->cur_capacity1);
1093 id->lba_capacity = __le32_to_cpu(id->lba_capacity);
1094 id->dma_1word = __le16_to_cpu(id->dma_1word);
1095 id->dma_mword = __le16_to_cpu(id->dma_mword);
1096 id->eide_pio_modes = __le16_to_cpu(id->eide_pio_modes);
1097 id->eide_dma_min = __le16_to_cpu(id->eide_dma_min);
1098 id->eide_dma_time = __le16_to_cpu(id->eide_dma_time);
1099 id->eide_pio = __le16_to_cpu(id->eide_pio);
1100 id->eide_pio_iordy = __le16_to_cpu(id->eide_pio_iordy);
1101 for (i = 0; i < 2; ++i)
1102 id->words69_70[i] = __le16_to_cpu(id->words69_70[i]);
1103 for (i = 0; i < 4; ++i)
1104 id->words71_74[i] = __le16_to_cpu(id->words71_74[i]);
1105 id->queue_depth = __le16_to_cpu(id->queue_depth);
1106 for (i = 0; i < 4; ++i)
1107 id->words76_79[i] = __le16_to_cpu(id->words76_79[i]);
1108 id->major_rev_num = __le16_to_cpu(id->major_rev_num);
1109 id->minor_rev_num = __le16_to_cpu(id->minor_rev_num);
1110 id->command_set_1 = __le16_to_cpu(id->command_set_1);
1111 id->command_set_2 = __le16_to_cpu(id->command_set_2);
1112 id->cfsse = __le16_to_cpu(id->cfsse);
1113 id->cfs_enable_1 = __le16_to_cpu(id->cfs_enable_1);
1114 id->cfs_enable_2 = __le16_to_cpu(id->cfs_enable_2);
1115 id->csf_default = __le16_to_cpu(id->csf_default);
1116 id->dma_ultra = __le16_to_cpu(id->dma_ultra);
1117 id->trseuc = __le16_to_cpu(id->trseuc);
1118 id->trsEuc = __le16_to_cpu(id->trsEuc);
1119 id->CurAPMvalues = __le16_to_cpu(id->CurAPMvalues);
1120 id->mprc = __le16_to_cpu(id->mprc);
1121 id->hw_config = __le16_to_cpu(id->hw_config);
1122 id->acoustic = __le16_to_cpu(id->acoustic);
1123 id->msrqs = __le16_to_cpu(id->msrqs);
1124 id->sxfert = __le16_to_cpu(id->sxfert);
1125 id->sal = __le16_to_cpu(id->sal);
1126 id->spg = __le32_to_cpu(id->spg);
1127 id->lba_capacity_2 = __le64_to_cpu(id->lba_capacity_2);
1128 for (i = 0; i < 22; i++)
1129 id->words104_125[i] = __le16_to_cpu(id->words104_125[i]);
1130 id->last_lun = __le16_to_cpu(id->last_lun);
1131 id->word127 = __le16_to_cpu(id->word127);
1132 id->dlf = __le16_to_cpu(id->dlf);
1133 id->csfo = __le16_to_cpu(id->csfo);
1134 for (i = 0; i < 26; i++)
1135 id->words130_155[i] = __le16_to_cpu(id->words130_155[i]);
1136 id->word156 = __le16_to_cpu(id->word156);
1137 for (i = 0; i < 3; i++)
1138 id->words157_159[i] = __le16_to_cpu(id->words157_159[i]);
1139 id->cfa_power = __le16_to_cpu(id->cfa_power);
1140 for (i = 0; i < 14; i++)
1141 id->words161_175[i] = __le16_to_cpu(id->words161_175[i]);
1142 for (i = 0; i < 31; i++)
1143 id->words176_205[i] = __le16_to_cpu(id->words176_205[i]);
1144 for (i = 0; i < 48; i++)
1145 id->words206_254[i] = __le16_to_cpu(id->words206_254[i]);
1146 id->integrity_word = __le16_to_cpu(id->integrity_word);
1148 # error "Please fix <asm/byteorder.h>"
1154 /**************************************************************************
1155 * isd200_get_inquiry_data
1162 static int isd200_get_inquiry_data( struct us_data *us )
1164 struct isd200_info *info = (struct isd200_info *)us->extra;
1165 int retStatus = ISD200_GOOD;
1166 struct hd_driveid *id = info->id;
1168 US_DEBUGP("Entering isd200_get_inquiry_data\n");
1170 /* set default to Master */
1171 info->DeviceHead = ATA_ADDRESS_DEVHEAD_STD;
1173 /* attempt to manually enumerate this device */
1174 retStatus = isd200_manual_enum(us);
1175 if (retStatus == ISD200_GOOD) {
1178 /* check for an ATA device */
1179 if (info->DeviceFlags & DF_ATA_DEVICE) {
1180 /* this must be an ATA device */
1181 /* perform an ATA Command Identify */
1182 transferStatus = isd200_action( us, ACTION_IDENTIFY,
1184 sizeof(struct hd_driveid) );
1185 if (transferStatus != ISD200_TRANSPORT_GOOD) {
1186 /* Error issuing ATA Command Identify */
1187 US_DEBUGP(" Error issuing ATA Command Identify\n");
1188 retStatus = ISD200_ERROR;
1190 /* ATA Command Identify successful */
1194 isd200_fix_driveid(id);
1196 US_DEBUGP(" Identify Data Structure:\n");
1197 US_DEBUGP(" config = 0x%x\n", id->config);
1198 US_DEBUGP(" cyls = 0x%x\n", id->cyls);
1199 US_DEBUGP(" heads = 0x%x\n", id->heads);
1200 US_DEBUGP(" track_bytes = 0x%x\n", id->track_bytes);
1201 US_DEBUGP(" sector_bytes = 0x%x\n", id->sector_bytes);
1202 US_DEBUGP(" sectors = 0x%x\n", id->sectors);
1203 US_DEBUGP(" serial_no[0] = 0x%x\n", id->serial_no[0]);
1204 US_DEBUGP(" buf_type = 0x%x\n", id->buf_type);
1205 US_DEBUGP(" buf_size = 0x%x\n", id->buf_size);
1206 US_DEBUGP(" ecc_bytes = 0x%x\n", id->ecc_bytes);
1207 US_DEBUGP(" fw_rev[0] = 0x%x\n", id->fw_rev[0]);
1208 US_DEBUGP(" model[0] = 0x%x\n", id->model[0]);
1209 US_DEBUGP(" max_multsect = 0x%x\n", id->max_multsect);
1210 US_DEBUGP(" dword_io = 0x%x\n", id->dword_io);
1211 US_DEBUGP(" capability = 0x%x\n", id->capability);
1212 US_DEBUGP(" tPIO = 0x%x\n", id->tPIO);
1213 US_DEBUGP(" tDMA = 0x%x\n", id->tDMA);
1214 US_DEBUGP(" field_valid = 0x%x\n", id->field_valid);
1215 US_DEBUGP(" cur_cyls = 0x%x\n", id->cur_cyls);
1216 US_DEBUGP(" cur_heads = 0x%x\n", id->cur_heads);
1217 US_DEBUGP(" cur_sectors = 0x%x\n", id->cur_sectors);
1218 US_DEBUGP(" cur_capacity = 0x%x\n", (id->cur_capacity1 << 16) + id->cur_capacity0 );
1219 US_DEBUGP(" multsect = 0x%x\n", id->multsect);
1220 US_DEBUGP(" lba_capacity = 0x%x\n", id->lba_capacity);
1221 US_DEBUGP(" command_set_1 = 0x%x\n", id->command_set_1);
1222 US_DEBUGP(" command_set_2 = 0x%x\n", id->command_set_2);
1224 memset(&info->InquiryData, 0, sizeof(info->InquiryData));
1226 /* Standard IDE interface only supports disks */
1227 info->InquiryData.DeviceType = DIRECT_ACCESS_DEVICE;
1229 /* The length must be at least 36 (5 + 31) */
1230 info->InquiryData.AdditionalLength = 0x1F;
1232 if (id->command_set_1 & COMMANDSET_MEDIA_STATUS) {
1233 /* set the removable bit */
1234 info->InquiryData.DeviceTypeModifier = DEVICE_REMOVABLE;
1235 info->DeviceFlags |= DF_REMOVABLE_MEDIA;
1238 /* Fill in vendor identification fields */
1239 src = (__be16*)id->model;
1240 dest = (__u16*)info->InquiryData.VendorId;
1242 dest[i] = be16_to_cpu(src[i]);
1244 src = (__be16*)(id->model+8);
1245 dest = (__u16*)info->InquiryData.ProductId;
1247 dest[i] = be16_to_cpu(src[i]);
1249 src = (__be16*)id->fw_rev;
1250 dest = (__u16*)info->InquiryData.ProductRevisionLevel;
1252 dest[i] = be16_to_cpu(src[i]);
1254 /* determine if it supports Media Status Notification */
1255 if (id->command_set_2 & COMMANDSET_MEDIA_STATUS) {
1256 US_DEBUGP(" Device supports Media Status Notification\n");
1258 /* Indicate that it is enabled, even though it is not
1259 * This allows the lock/unlock of the media to work
1262 info->DeviceFlags |= DF_MEDIA_STATUS_ENABLED;
1265 info->DeviceFlags &= ~DF_MEDIA_STATUS_ENABLED;
1270 * this must be an ATAPI device
1271 * use an ATAPI protocol (Transparent SCSI)
1273 us->protocol_name = "Transparent SCSI";
1274 us->proto_handler = usb_stor_transparent_scsi_command;
1276 US_DEBUGP("Protocol changed to: %s\n", us->protocol_name);
1278 /* Free driver structure */
1279 us->extra_destructor(info);
1282 us->extra_destructor = NULL;
1286 US_DEBUGP("Leaving isd200_get_inquiry_data %08X\n", retStatus);
1291 /**************************************************************************
1292 * isd200_scsi_to_ata
1294 * Translate SCSI commands to ATA commands.
1297 * 1 if the command needs to be sent to the transport layer
1300 static int isd200_scsi_to_ata(struct scsi_cmnd *srb, struct us_data *us,
1301 union ata_cdb * ataCdb)
1303 struct isd200_info *info = (struct isd200_info *)us->extra;
1304 struct hd_driveid *id = info->id;
1305 int sendToTransport = 1;
1306 unsigned char sectnum, head;
1307 unsigned short cylinder;
1309 unsigned long blockCount;
1310 unsigned char senseData[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1312 memset(ataCdb, 0, sizeof(union ata_cdb));
1315 switch (srb->cmnd[0]) {
1317 US_DEBUGP(" ATA OUT - INQUIRY\n");
1319 /* copy InquiryData */
1320 usb_stor_set_xfer_buf((unsigned char *) &info->InquiryData,
1321 sizeof(info->InquiryData), srb);
1322 srb->result = SAM_STAT_GOOD;
1323 sendToTransport = 0;
1327 US_DEBUGP(" ATA OUT - SCSIOP_MODE_SENSE\n");
1329 /* Initialize the return buffer */
1330 usb_stor_set_xfer_buf(senseData, sizeof(senseData), srb);
1332 if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
1334 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1335 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1336 ataCdb->generic.TransferBlockSize = 1;
1337 ataCdb->generic.RegisterSelect = REG_COMMAND;
1338 ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1339 isd200_srb_set_bufflen(srb, 0);
1341 US_DEBUGP(" Media Status not supported, just report okay\n");
1342 srb->result = SAM_STAT_GOOD;
1343 sendToTransport = 0;
1347 case TEST_UNIT_READY:
1348 US_DEBUGP(" ATA OUT - SCSIOP_TEST_UNIT_READY\n");
1350 if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
1352 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1353 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1354 ataCdb->generic.TransferBlockSize = 1;
1355 ataCdb->generic.RegisterSelect = REG_COMMAND;
1356 ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1357 isd200_srb_set_bufflen(srb, 0);
1359 US_DEBUGP(" Media Status not supported, just report okay\n");
1360 srb->result = SAM_STAT_GOOD;
1361 sendToTransport = 0;
1367 unsigned long capacity;
1368 struct read_capacity_data readCapacityData;
1370 US_DEBUGP(" ATA OUT - SCSIOP_READ_CAPACITY\n");
1372 if (id->capability & CAPABILITY_LBA ) {
1373 capacity = id->lba_capacity - 1;
1375 capacity = (id->heads *
1379 readCapacityData.LogicalBlockAddress = cpu_to_be32(capacity);
1380 readCapacityData.BytesPerBlock = cpu_to_be32(0x200);
1382 usb_stor_set_xfer_buf((unsigned char *) &readCapacityData,
1383 sizeof(readCapacityData), srb);
1384 srb->result = SAM_STAT_GOOD;
1385 sendToTransport = 0;
1390 US_DEBUGP(" ATA OUT - SCSIOP_READ\n");
1392 lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1393 blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1395 if (id->capability & CAPABILITY_LBA) {
1396 sectnum = (unsigned char)(lba);
1397 cylinder = (unsigned short)(lba>>8);
1398 head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1400 sectnum = (unsigned char)((lba % id->sectors) + 1);
1401 cylinder = (unsigned short)(lba / (id->sectors *
1403 head = (unsigned char)((lba / id->sectors) %
1406 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1407 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1408 ataCdb->generic.TransferBlockSize = 1;
1409 ataCdb->generic.RegisterSelect =
1410 REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
1411 REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
1412 REG_DEVICE_HEAD | REG_COMMAND;
1413 ataCdb->write.SectorCountByte = (unsigned char)blockCount;
1414 ataCdb->write.SectorNumberByte = sectnum;
1415 ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
1416 ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
1417 ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
1418 ataCdb->write.CommandByte = WIN_READ;
1422 US_DEBUGP(" ATA OUT - SCSIOP_WRITE\n");
1424 lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1425 blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1427 if (id->capability & CAPABILITY_LBA) {
1428 sectnum = (unsigned char)(lba);
1429 cylinder = (unsigned short)(lba>>8);
1430 head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1432 sectnum = (unsigned char)((lba % id->sectors) + 1);
1433 cylinder = (unsigned short)(lba / (id->sectors * id->heads));
1434 head = (unsigned char)((lba / id->sectors) % id->heads);
1436 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1437 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1438 ataCdb->generic.TransferBlockSize = 1;
1439 ataCdb->generic.RegisterSelect =
1440 REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
1441 REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
1442 REG_DEVICE_HEAD | REG_COMMAND;
1443 ataCdb->write.SectorCountByte = (unsigned char)blockCount;
1444 ataCdb->write.SectorNumberByte = sectnum;
1445 ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
1446 ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
1447 ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
1448 ataCdb->write.CommandByte = WIN_WRITE;
1451 case ALLOW_MEDIUM_REMOVAL:
1452 US_DEBUGP(" ATA OUT - SCSIOP_MEDIUM_REMOVAL\n");
1454 if (info->DeviceFlags & DF_REMOVABLE_MEDIA) {
1455 US_DEBUGP(" srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
1457 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1458 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1459 ataCdb->generic.TransferBlockSize = 1;
1460 ataCdb->generic.RegisterSelect = REG_COMMAND;
1461 ataCdb->write.CommandByte = (srb->cmnd[4] & 0x1) ?
1462 WIN_DOORLOCK : WIN_DOORUNLOCK;
1463 isd200_srb_set_bufflen(srb, 0);
1465 US_DEBUGP(" Not removeable media, just report okay\n");
1466 srb->result = SAM_STAT_GOOD;
1467 sendToTransport = 0;
1472 US_DEBUGP(" ATA OUT - SCSIOP_START_STOP_UNIT\n");
1473 US_DEBUGP(" srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
1475 if ((srb->cmnd[4] & 0x3) == 0x2) {
1476 US_DEBUGP(" Media Eject\n");
1477 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1478 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1479 ataCdb->generic.TransferBlockSize = 0;
1480 ataCdb->generic.RegisterSelect = REG_COMMAND;
1481 ataCdb->write.CommandByte = ATA_COMMAND_MEDIA_EJECT;
1482 } else if ((srb->cmnd[4] & 0x3) == 0x1) {
1483 US_DEBUGP(" Get Media Status\n");
1484 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1485 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1486 ataCdb->generic.TransferBlockSize = 1;
1487 ataCdb->generic.RegisterSelect = REG_COMMAND;
1488 ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1489 isd200_srb_set_bufflen(srb, 0);
1491 US_DEBUGP(" Nothing to do, just report okay\n");
1492 srb->result = SAM_STAT_GOOD;
1493 sendToTransport = 0;
1498 US_DEBUGP("Unsupported SCSI command - 0x%X\n", srb->cmnd[0]);
1499 srb->result = DID_ERROR << 16;
1500 sendToTransport = 0;
1504 return(sendToTransport);
1508 /**************************************************************************
1511 * Frees the driver structure.
1513 static void isd200_free_info_ptrs(void *info_)
1515 struct isd200_info *info = (struct isd200_info *) info_;
1519 kfree(info->RegsBuf);
1520 kfree(info->srb.sense_buffer);
1524 /**************************************************************************
1527 * Allocates (if necessary) and initializes the driver structure.
1532 static int isd200_init_info(struct us_data *us)
1534 int retStatus = ISD200_GOOD;
1535 struct isd200_info *info;
1537 info = (struct isd200_info *)
1538 kzalloc(sizeof(struct isd200_info), GFP_KERNEL);
1540 retStatus = ISD200_ERROR;
1542 info->id = (struct hd_driveid *)
1543 kzalloc(sizeof(struct hd_driveid), GFP_KERNEL);
1544 info->RegsBuf = (unsigned char *)
1545 kmalloc(sizeof(info->ATARegs), GFP_KERNEL);
1546 info->srb.sense_buffer =
1547 kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
1548 if (!info->id || !info->RegsBuf || !info->srb.sense_buffer) {
1549 isd200_free_info_ptrs(info);
1551 retStatus = ISD200_ERROR;
1555 if (retStatus == ISD200_GOOD) {
1557 us->extra_destructor = isd200_free_info_ptrs;
1559 US_DEBUGP("ERROR - kmalloc failure\n");
1564 /**************************************************************************
1565 * Initialization for the ISD200
1568 static int isd200_Initialization(struct us_data *us)
1570 US_DEBUGP("ISD200 Initialization...\n");
1572 /* Initialize ISD200 info struct */
1574 if (isd200_init_info(us) == ISD200_ERROR) {
1575 US_DEBUGP("ERROR Initializing ISD200 Info struct\n");
1577 /* Get device specific data */
1579 if (isd200_get_inquiry_data(us) != ISD200_GOOD)
1580 US_DEBUGP("ISD200 Initialization Failure\n");
1582 US_DEBUGP("ISD200 Initialization complete\n");
1589 /**************************************************************************
1590 * Protocol and Transport for the ISD200 ASIC
1592 * This protocol and transport are for ATA devices connected to an ISD200
1593 * ASIC. An ATAPI device that is conected as a slave device will be
1594 * detected in the driver initialization function and the protocol will
1595 * be changed to an ATAPI protocol (Transparent SCSI).
1599 static void isd200_ata_command(struct scsi_cmnd *srb, struct us_data *us)
1601 int sendToTransport = 1, orig_bufflen;
1602 union ata_cdb ataCdb;
1604 /* Make sure driver was initialized */
1606 if (us->extra == NULL)
1607 US_DEBUGP("ERROR Driver not initialized\n");
1609 scsi_set_resid(srb, 0);
1610 /* scsi_bufflen might change in protocol translation to ata */
1611 orig_bufflen = scsi_bufflen(srb);
1612 sendToTransport = isd200_scsi_to_ata(srb, us, &ataCdb);
1614 /* send the command to the transport layer */
1615 if (sendToTransport)
1616 isd200_invoke_transport(us, srb, &ataCdb);
1618 isd200_srb_set_bufflen(srb, orig_bufflen);
1621 static int isd200_probe(struct usb_interface *intf,
1622 const struct usb_device_id *id)
1627 result = usb_stor_probe1(&us, intf, id,
1628 (id - isd200_usb_ids) + isd200_unusual_dev_list);
1632 us->protocol_name = "ISD200 ATA/ATAPI";
1633 us->proto_handler = isd200_ata_command;
1635 result = usb_stor_probe2(us);
1639 static struct usb_driver isd200_driver = {
1640 .name = "ums-isd200",
1641 .probe = isd200_probe,
1642 .disconnect = usb_stor_disconnect,
1643 .suspend = usb_stor_suspend,
1644 .resume = usb_stor_resume,
1645 .reset_resume = usb_stor_reset_resume,
1646 .pre_reset = usb_stor_pre_reset,
1647 .post_reset = usb_stor_post_reset,
1648 .id_table = isd200_usb_ids,
1652 static int __init isd200_init(void)
1654 return usb_register(&isd200_driver);
1657 static void __exit isd200_exit(void)
1659 usb_deregister(&isd200_driver);
1662 module_init(isd200_init);
1663 module_exit(isd200_exit);