RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / usb / storage / isd200.c
blob800b3d8f8d1c6c3c658e958d3b2c6090ff27ecb0
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>
9 * Initial work:
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
19 * later version.
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.
30 * History:
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.
35 * (bjorn@haxx.se)
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.
39 * (bjorn@haxx.se)
43 /* Include files */
45 #include <linux/jiffies.h>
46 #include <linux/errno.h>
47 #include <linux/module.h>
48 #include <linux/slab.h>
49 #include <linux/ata.h>
50 #include <linux/hdreg.h>
51 #include <linux/scatterlist.h>
53 #include <scsi/scsi.h>
54 #include <scsi/scsi_cmnd.h>
55 #include <scsi/scsi_device.h>
57 #include "usb.h"
58 #include "transport.h"
59 #include "protocol.h"
60 #include "debug.h"
61 #include "scsiglue.h"
63 MODULE_DESCRIPTION("Driver for In-System Design, Inc. ISD200 ASIC");
64 MODULE_AUTHOR("Björn Stenberg <bjorn@haxx.se>");
65 MODULE_LICENSE("GPL");
67 static int isd200_Initialization(struct us_data *us);
71 * The table of devices
73 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
74 vendorName, productName, useProtocol, useTransport, \
75 initFunction, flags) \
76 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
77 .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
79 struct usb_device_id isd200_usb_ids[] = {
80 # include "unusual_isd200.h"
81 { } /* Terminating entry */
83 MODULE_DEVICE_TABLE(usb, isd200_usb_ids);
85 #undef UNUSUAL_DEV
86 #undef USUAL_DEV
89 * The flags table
91 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
92 vendor_name, product_name, use_protocol, use_transport, \
93 init_function, Flags) \
94 { \
95 .vendorName = vendor_name, \
96 .productName = product_name, \
97 .useProtocol = use_protocol, \
98 .useTransport = use_transport, \
99 .initFunction = init_function, \
102 static struct us_unusual_dev isd200_unusual_dev_list[] = {
103 # include "unusual_isd200.h"
104 { } /* Terminating entry */
107 #undef UNUSUAL_DEV
108 #undef USUAL_DEV
111 /* Timeout defines (in Seconds) */
113 #define ISD200_ENUM_BSY_TIMEOUT 35
114 #define ISD200_ENUM_DETECT_TIMEOUT 30
115 #define ISD200_DEFAULT_TIMEOUT 30
117 /* device flags */
118 #define DF_ATA_DEVICE 0x0001
119 #define DF_MEDIA_STATUS_ENABLED 0x0002
120 #define DF_REMOVABLE_MEDIA 0x0004
122 /* capability bit definitions */
123 #define CAPABILITY_DMA 0x01
124 #define CAPABILITY_LBA 0x02
126 /* command_setX bit definitions */
127 #define COMMANDSET_REMOVABLE 0x02
128 #define COMMANDSET_MEDIA_STATUS 0x10
130 /* ATA Vendor Specific defines */
131 #define ATA_ADDRESS_DEVHEAD_STD 0xa0
132 #define ATA_ADDRESS_DEVHEAD_LBA_MODE 0x40
133 #define ATA_ADDRESS_DEVHEAD_SLAVE 0x10
135 /* Action Select bits */
136 #define ACTION_SELECT_0 0x01
137 #define ACTION_SELECT_1 0x02
138 #define ACTION_SELECT_2 0x04
139 #define ACTION_SELECT_3 0x08
140 #define ACTION_SELECT_4 0x10
141 #define ACTION_SELECT_5 0x20
142 #define ACTION_SELECT_6 0x40
143 #define ACTION_SELECT_7 0x80
145 /* Register Select bits */
146 #define REG_ALTERNATE_STATUS 0x01
147 #define REG_DEVICE_CONTROL 0x01
148 #define REG_ERROR 0x02
149 #define REG_FEATURES 0x02
150 #define REG_SECTOR_COUNT 0x04
151 #define REG_SECTOR_NUMBER 0x08
152 #define REG_CYLINDER_LOW 0x10
153 #define REG_CYLINDER_HIGH 0x20
154 #define REG_DEVICE_HEAD 0x40
155 #define REG_STATUS 0x80
156 #define REG_COMMAND 0x80
158 /* ATA registers offset definitions */
159 #define ATA_REG_ERROR_OFFSET 1
160 #define ATA_REG_LCYL_OFFSET 4
161 #define ATA_REG_HCYL_OFFSET 5
162 #define ATA_REG_STATUS_OFFSET 7
164 /* ATA error definitions not in <linux/hdreg.h> */
165 #define ATA_ERROR_MEDIA_CHANGE 0x20
167 /* ATA command definitions not in <linux/hdreg.h> */
168 #define ATA_COMMAND_GET_MEDIA_STATUS 0xDA
169 #define ATA_COMMAND_MEDIA_EJECT 0xED
171 /* ATA drive control definitions */
172 #define ATA_DC_DISABLE_INTERRUPTS 0x02
173 #define ATA_DC_RESET_CONTROLLER 0x04
174 #define ATA_DC_REENABLE_CONTROLLER 0x00
177 * General purpose return codes
180 #define ISD200_ERROR -1
181 #define ISD200_GOOD 0
184 * Transport return codes
187 #define ISD200_TRANSPORT_GOOD 0 /* Transport good, command good */
188 #define ISD200_TRANSPORT_FAILED 1 /* Transport good, command failed */
189 #define ISD200_TRANSPORT_ERROR 2 /* Transport bad (i.e. device dead) */
191 /* driver action codes */
192 #define ACTION_READ_STATUS 0
193 #define ACTION_RESET 1
194 #define ACTION_REENABLE 2
195 #define ACTION_SOFT_RESET 3
196 #define ACTION_ENUM 4
197 #define ACTION_IDENTIFY 5
201 * ata_cdb struct
205 union ata_cdb {
206 struct {
207 unsigned char SignatureByte0;
208 unsigned char SignatureByte1;
209 unsigned char ActionSelect;
210 unsigned char RegisterSelect;
211 unsigned char TransferBlockSize;
212 unsigned char WriteData3F6;
213 unsigned char WriteData1F1;
214 unsigned char WriteData1F2;
215 unsigned char WriteData1F3;
216 unsigned char WriteData1F4;
217 unsigned char WriteData1F5;
218 unsigned char WriteData1F6;
219 unsigned char WriteData1F7;
220 unsigned char Reserved[3];
221 } generic;
223 struct {
224 unsigned char SignatureByte0;
225 unsigned char SignatureByte1;
226 unsigned char ActionSelect;
227 unsigned char RegisterSelect;
228 unsigned char TransferBlockSize;
229 unsigned char AlternateStatusByte;
230 unsigned char ErrorByte;
231 unsigned char SectorCountByte;
232 unsigned char SectorNumberByte;
233 unsigned char CylinderLowByte;
234 unsigned char CylinderHighByte;
235 unsigned char DeviceHeadByte;
236 unsigned char StatusByte;
237 unsigned char Reserved[3];
238 } read;
240 struct {
241 unsigned char SignatureByte0;
242 unsigned char SignatureByte1;
243 unsigned char ActionSelect;
244 unsigned char RegisterSelect;
245 unsigned char TransferBlockSize;
246 unsigned char DeviceControlByte;
247 unsigned char FeaturesByte;
248 unsigned char SectorCountByte;
249 unsigned char SectorNumberByte;
250 unsigned char CylinderLowByte;
251 unsigned char CylinderHighByte;
252 unsigned char DeviceHeadByte;
253 unsigned char CommandByte;
254 unsigned char Reserved[3];
255 } write;
260 * Inquiry data structure. This is the data returned from the target
261 * after it receives an inquiry.
263 * This structure may be extended by the number of bytes specified
264 * in the field AdditionalLength. The defined size constant only
265 * includes fields through ProductRevisionLevel.
269 * DeviceType field
271 #define DIRECT_ACCESS_DEVICE 0x00 /* disks */
272 #define DEVICE_REMOVABLE 0x80
274 struct inquiry_data {
275 unsigned char DeviceType;
276 unsigned char DeviceTypeModifier;
277 unsigned char Versions;
278 unsigned char Format;
279 unsigned char AdditionalLength;
280 unsigned char Reserved[2];
281 unsigned char Capability;
282 unsigned char VendorId[8];
283 unsigned char ProductId[16];
284 unsigned char ProductRevisionLevel[4];
285 unsigned char VendorSpecific[20];
286 unsigned char Reserved3[40];
287 } __attribute__ ((packed));
290 * INQUIRY data buffer size
293 #define INQUIRYDATABUFFERSIZE 36
297 * ISD200 CONFIG data struct
300 #define ATACFG_TIMING 0x0f
301 #define ATACFG_ATAPI_RESET 0x10
302 #define ATACFG_MASTER 0x20
303 #define ATACFG_BLOCKSIZE 0xa0
305 #define ATACFGE_LAST_LUN 0x07
306 #define ATACFGE_DESC_OVERRIDE 0x08
307 #define ATACFGE_STATE_SUSPEND 0x10
308 #define ATACFGE_SKIP_BOOT 0x20
309 #define ATACFGE_CONF_DESC2 0x40
310 #define ATACFGE_INIT_STATUS 0x80
312 #define CFG_CAPABILITY_SRST 0x01
314 struct isd200_config {
315 unsigned char EventNotification;
316 unsigned char ExternalClock;
317 unsigned char ATAInitTimeout;
318 unsigned char ATAConfig;
319 unsigned char ATAMajorCommand;
320 unsigned char ATAMinorCommand;
321 unsigned char ATAExtraConfig;
322 unsigned char Capability;
323 }__attribute__ ((packed));
327 * ISD200 driver information struct
330 struct isd200_info {
331 struct inquiry_data InquiryData;
332 u16 *id;
333 struct isd200_config ConfigData;
334 unsigned char *RegsBuf;
335 unsigned char ATARegs[8];
336 unsigned char DeviceHead;
337 unsigned char DeviceFlags;
339 /* maximum number of LUNs supported */
340 unsigned char MaxLUNs;
341 unsigned char cmnd[BLK_MAX_CDB];
342 struct scsi_cmnd srb;
343 struct scatterlist sg;
348 * Read Capacity Data - returned in Big Endian format
351 struct read_capacity_data {
352 __be32 LogicalBlockAddress;
353 __be32 BytesPerBlock;
357 * Read Block Limits Data - returned in Big Endian format
358 * This structure returns the maximum and minimum block
359 * size for a TAPE device.
362 struct read_block_limits {
363 unsigned char Reserved;
364 unsigned char BlockMaximumSize[3];
365 unsigned char BlockMinimumSize[2];
370 * Sense Data Format
373 #define SENSE_ERRCODE 0x7f
374 #define SENSE_ERRCODE_VALID 0x80
375 #define SENSE_FLAG_SENSE_KEY 0x0f
376 #define SENSE_FLAG_BAD_LENGTH 0x20
377 #define SENSE_FLAG_END_OF_MEDIA 0x40
378 #define SENSE_FLAG_FILE_MARK 0x80
379 struct sense_data {
380 unsigned char ErrorCode;
381 unsigned char SegmentNumber;
382 unsigned char Flags;
383 unsigned char Information[4];
384 unsigned char AdditionalSenseLength;
385 unsigned char CommandSpecificInformation[4];
386 unsigned char AdditionalSenseCode;
387 unsigned char AdditionalSenseCodeQualifier;
388 unsigned char FieldReplaceableUnitCode;
389 unsigned char SenseKeySpecific[3];
390 } __attribute__ ((packed));
393 * Default request sense buffer size
396 #define SENSE_BUFFER_SIZE 18
398 /***********************************************************************
399 * Helper routines
400 ***********************************************************************/
402 /**************************************************************************
403 * isd200_build_sense
405 * Builds an artificial sense buffer to report the results of a
406 * failed command.
408 * RETURNS:
409 * void
411 static void isd200_build_sense(struct us_data *us, struct scsi_cmnd *srb)
413 struct isd200_info *info = (struct isd200_info *)us->extra;
414 struct sense_data *buf = (struct sense_data *) &srb->sense_buffer[0];
415 unsigned char error = info->ATARegs[ATA_REG_ERROR_OFFSET];
417 if(error & ATA_ERROR_MEDIA_CHANGE) {
418 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
419 buf->AdditionalSenseLength = 0xb;
420 buf->Flags = UNIT_ATTENTION;
421 buf->AdditionalSenseCode = 0;
422 buf->AdditionalSenseCodeQualifier = 0;
423 } else if (error & ATA_MCR) {
424 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
425 buf->AdditionalSenseLength = 0xb;
426 buf->Flags = UNIT_ATTENTION;
427 buf->AdditionalSenseCode = 0;
428 buf->AdditionalSenseCodeQualifier = 0;
429 } else if (error & ATA_TRK0NF) {
430 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
431 buf->AdditionalSenseLength = 0xb;
432 buf->Flags = NOT_READY;
433 buf->AdditionalSenseCode = 0;
434 buf->AdditionalSenseCodeQualifier = 0;
435 } else if (error & ATA_UNC) {
436 buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
437 buf->AdditionalSenseLength = 0xb;
438 buf->Flags = DATA_PROTECT;
439 buf->AdditionalSenseCode = 0;
440 buf->AdditionalSenseCodeQualifier = 0;
441 } else {
442 buf->ErrorCode = 0;
443 buf->AdditionalSenseLength = 0;
444 buf->Flags = 0;
445 buf->AdditionalSenseCode = 0;
446 buf->AdditionalSenseCodeQualifier = 0;
451 /***********************************************************************
452 * Transport routines
453 ***********************************************************************/
455 /**************************************************************************
456 * isd200_set_srb(), isd200_srb_set_bufflen()
458 * Two helpers to facilitate in initialization of scsi_cmnd structure
459 * Will need to change when struct scsi_cmnd changes
461 static void isd200_set_srb(struct isd200_info *info,
462 enum dma_data_direction dir, void* buff, unsigned bufflen)
464 struct scsi_cmnd *srb = &info->srb;
466 if (buff)
467 sg_init_one(&info->sg, buff, bufflen);
469 srb->sc_data_direction = dir;
470 srb->sdb.table.sgl = buff ? &info->sg : NULL;
471 srb->sdb.length = bufflen;
472 srb->sdb.table.nents = buff ? 1 : 0;
475 static void isd200_srb_set_bufflen(struct scsi_cmnd *srb, unsigned bufflen)
477 srb->sdb.length = bufflen;
481 /**************************************************************************
482 * isd200_action
484 * Routine for sending commands to the isd200
486 * RETURNS:
487 * ISD status code
489 static int isd200_action( struct us_data *us, int action,
490 void* pointer, int value )
492 union ata_cdb ata;
493 /* static to prevent this large struct being placed on the valuable stack */
494 static struct scsi_device srb_dev;
495 struct isd200_info *info = (struct isd200_info *)us->extra;
496 struct scsi_cmnd *srb = &info->srb;
497 int status;
499 memset(&ata, 0, sizeof(ata));
500 srb->cmnd = info->cmnd;
501 srb->device = &srb_dev;
502 ++srb->serial_number;
504 ata.generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
505 ata.generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
506 ata.generic.TransferBlockSize = 1;
508 switch ( action ) {
509 case ACTION_READ_STATUS:
510 US_DEBUGP(" isd200_action(READ_STATUS)\n");
511 ata.generic.ActionSelect = ACTION_SELECT_0|ACTION_SELECT_2;
512 ata.generic.RegisterSelect =
513 REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
514 REG_STATUS | REG_ERROR;
515 isd200_set_srb(info, DMA_FROM_DEVICE, pointer, value);
516 break;
518 case ACTION_ENUM:
519 US_DEBUGP(" isd200_action(ENUM,0x%02x)\n",value);
520 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
521 ACTION_SELECT_3|ACTION_SELECT_4|
522 ACTION_SELECT_5;
523 ata.generic.RegisterSelect = REG_DEVICE_HEAD;
524 ata.write.DeviceHeadByte = value;
525 isd200_set_srb(info, DMA_NONE, NULL, 0);
526 break;
528 case ACTION_RESET:
529 US_DEBUGP(" isd200_action(RESET)\n");
530 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
531 ACTION_SELECT_3|ACTION_SELECT_4;
532 ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
533 ata.write.DeviceControlByte = ATA_DC_RESET_CONTROLLER;
534 isd200_set_srb(info, DMA_NONE, NULL, 0);
535 break;
537 case ACTION_REENABLE:
538 US_DEBUGP(" isd200_action(REENABLE)\n");
539 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
540 ACTION_SELECT_3|ACTION_SELECT_4;
541 ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
542 ata.write.DeviceControlByte = ATA_DC_REENABLE_CONTROLLER;
543 isd200_set_srb(info, DMA_NONE, NULL, 0);
544 break;
546 case ACTION_SOFT_RESET:
547 US_DEBUGP(" isd200_action(SOFT_RESET)\n");
548 ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_5;
549 ata.generic.RegisterSelect = REG_DEVICE_HEAD | REG_COMMAND;
550 ata.write.DeviceHeadByte = info->DeviceHead;
551 ata.write.CommandByte = ATA_CMD_DEV_RESET;
552 isd200_set_srb(info, DMA_NONE, NULL, 0);
553 break;
555 case ACTION_IDENTIFY:
556 US_DEBUGP(" isd200_action(IDENTIFY)\n");
557 ata.generic.RegisterSelect = REG_COMMAND;
558 ata.write.CommandByte = ATA_CMD_ID_ATA;
559 isd200_set_srb(info, DMA_FROM_DEVICE, info->id,
560 ATA_ID_WORDS * 2);
561 break;
563 default:
564 US_DEBUGP("Error: Undefined action %d\n",action);
565 return ISD200_ERROR;
568 memcpy(srb->cmnd, &ata, sizeof(ata.generic));
569 srb->cmd_len = sizeof(ata.generic);
570 status = usb_stor_Bulk_transport(srb, us);
571 if (status == USB_STOR_TRANSPORT_GOOD)
572 status = ISD200_GOOD;
573 else {
574 US_DEBUGP(" isd200_action(0x%02x) error: %d\n",action,status);
575 status = ISD200_ERROR;
576 /* need to reset device here */
579 return status;
582 /**************************************************************************
583 * isd200_read_regs
585 * Read ATA Registers
587 * RETURNS:
588 * ISD status code
590 static int isd200_read_regs( struct us_data *us )
592 struct isd200_info *info = (struct isd200_info *)us->extra;
593 int retStatus = ISD200_GOOD;
594 int transferStatus;
596 US_DEBUGP("Entering isd200_IssueATAReadRegs\n");
598 transferStatus = isd200_action( us, ACTION_READ_STATUS,
599 info->RegsBuf, sizeof(info->ATARegs) );
600 if (transferStatus != ISD200_TRANSPORT_GOOD) {
601 US_DEBUGP(" Error reading ATA registers\n");
602 retStatus = ISD200_ERROR;
603 } else {
604 memcpy(info->ATARegs, info->RegsBuf, sizeof(info->ATARegs));
605 US_DEBUGP(" Got ATA Register[ATA_REG_ERROR_OFFSET] = 0x%x\n",
606 info->ATARegs[ATA_REG_ERROR_OFFSET]);
609 return retStatus;
613 /**************************************************************************
614 * Invoke the transport and basic error-handling/recovery methods
616 * This is used by the protocol layers to actually send the message to
617 * the device and receive the response.
619 static void isd200_invoke_transport( struct us_data *us,
620 struct scsi_cmnd *srb,
621 union ata_cdb *ataCdb )
623 int need_auto_sense = 0;
624 int transferStatus;
625 int result;
627 /* send the command to the transport layer */
628 memcpy(srb->cmnd, ataCdb, sizeof(ataCdb->generic));
629 srb->cmd_len = sizeof(ataCdb->generic);
630 transferStatus = usb_stor_Bulk_transport(srb, us);
632 /* if the command gets aborted by the higher layers, we need to
633 * short-circuit all other processing
635 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
636 US_DEBUGP("-- command was aborted\n");
637 goto Handle_Abort;
640 switch (transferStatus) {
642 case USB_STOR_TRANSPORT_GOOD:
643 /* Indicate a good result */
644 srb->result = SAM_STAT_GOOD;
645 break;
647 case USB_STOR_TRANSPORT_NO_SENSE:
648 US_DEBUGP("-- transport indicates protocol failure\n");
649 srb->result = SAM_STAT_CHECK_CONDITION;
650 return;
652 case USB_STOR_TRANSPORT_FAILED:
653 US_DEBUGP("-- transport indicates command failure\n");
654 need_auto_sense = 1;
655 break;
657 case USB_STOR_TRANSPORT_ERROR:
658 US_DEBUGP("-- transport indicates transport error\n");
659 srb->result = DID_ERROR << 16;
660 /* Need reset here */
661 return;
663 default:
664 US_DEBUGP("-- transport indicates unknown error\n");
665 srb->result = DID_ERROR << 16;
666 /* Need reset here */
667 return;
670 if ((scsi_get_resid(srb) > 0) &&
671 !((srb->cmnd[0] == REQUEST_SENSE) ||
672 (srb->cmnd[0] == INQUIRY) ||
673 (srb->cmnd[0] == MODE_SENSE) ||
674 (srb->cmnd[0] == LOG_SENSE) ||
675 (srb->cmnd[0] == MODE_SENSE_10))) {
676 US_DEBUGP("-- unexpectedly short transfer\n");
677 need_auto_sense = 1;
680 if (need_auto_sense) {
681 result = isd200_read_regs(us);
682 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
683 US_DEBUGP("-- auto-sense aborted\n");
684 goto Handle_Abort;
686 if (result == ISD200_GOOD) {
687 isd200_build_sense(us, srb);
688 srb->result = SAM_STAT_CHECK_CONDITION;
690 /* If things are really okay, then let's show that */
691 if ((srb->sense_buffer[2] & 0xf) == 0x0)
692 srb->result = SAM_STAT_GOOD;
693 } else {
694 srb->result = DID_ERROR << 16;
695 /* Need reset here */
699 /* Regardless of auto-sense, if we _know_ we have an error
700 * condition, show that in the result code
702 if (transferStatus == USB_STOR_TRANSPORT_FAILED)
703 srb->result = SAM_STAT_CHECK_CONDITION;
704 return;
706 /* abort processing: the bulk-only transport requires a reset
707 * following an abort */
708 Handle_Abort:
709 srb->result = DID_ABORT << 16;
711 /* permit the reset transfer to take place */
712 clear_bit(US_FLIDX_ABORTING, &us->dflags);
713 /* Need reset here */
716 #ifdef CONFIG_USB_STORAGE_DEBUG
717 static void isd200_log_config( struct isd200_info* info )
719 US_DEBUGP(" Event Notification: 0x%x\n",
720 info->ConfigData.EventNotification);
721 US_DEBUGP(" External Clock: 0x%x\n",
722 info->ConfigData.ExternalClock);
723 US_DEBUGP(" ATA Init Timeout: 0x%x\n",
724 info->ConfigData.ATAInitTimeout);
725 US_DEBUGP(" ATAPI Command Block Size: 0x%x\n",
726 (info->ConfigData.ATAConfig & ATACFG_BLOCKSIZE) >> 6);
727 US_DEBUGP(" Master/Slave Selection: 0x%x\n",
728 info->ConfigData.ATAConfig & ATACFG_MASTER);
729 US_DEBUGP(" ATAPI Reset: 0x%x\n",
730 info->ConfigData.ATAConfig & ATACFG_ATAPI_RESET);
731 US_DEBUGP(" ATA Timing: 0x%x\n",
732 info->ConfigData.ATAConfig & ATACFG_TIMING);
733 US_DEBUGP(" ATA Major Command: 0x%x\n",
734 info->ConfigData.ATAMajorCommand);
735 US_DEBUGP(" ATA Minor Command: 0x%x\n",
736 info->ConfigData.ATAMinorCommand);
737 US_DEBUGP(" Init Status: 0x%x\n",
738 info->ConfigData.ATAExtraConfig & ATACFGE_INIT_STATUS);
739 US_DEBUGP(" Config Descriptor 2: 0x%x\n",
740 info->ConfigData.ATAExtraConfig & ATACFGE_CONF_DESC2);
741 US_DEBUGP(" Skip Device Boot: 0x%x\n",
742 info->ConfigData.ATAExtraConfig & ATACFGE_SKIP_BOOT);
743 US_DEBUGP(" ATA 3 State Supsend: 0x%x\n",
744 info->ConfigData.ATAExtraConfig & ATACFGE_STATE_SUSPEND);
745 US_DEBUGP(" Descriptor Override: 0x%x\n",
746 info->ConfigData.ATAExtraConfig & ATACFGE_DESC_OVERRIDE);
747 US_DEBUGP(" Last LUN Identifier: 0x%x\n",
748 info->ConfigData.ATAExtraConfig & ATACFGE_LAST_LUN);
749 US_DEBUGP(" SRST Enable: 0x%x\n",
750 info->ConfigData.ATAExtraConfig & CFG_CAPABILITY_SRST);
752 #endif
754 /**************************************************************************
755 * isd200_write_config
757 * Write the ISD200 Configuration data
759 * RETURNS:
760 * ISD status code
762 static int isd200_write_config( struct us_data *us )
764 struct isd200_info *info = (struct isd200_info *)us->extra;
765 int retStatus = ISD200_GOOD;
766 int result;
768 #ifdef CONFIG_USB_STORAGE_DEBUG
769 US_DEBUGP("Entering isd200_write_config\n");
770 US_DEBUGP(" Writing the following ISD200 Config Data:\n");
771 isd200_log_config(info);
772 #endif
774 /* let's send the command via the control pipe */
775 result = usb_stor_ctrl_transfer(
776 us,
777 us->send_ctrl_pipe,
778 0x01,
779 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
780 0x0000,
781 0x0002,
782 (void *) &info->ConfigData,
783 sizeof(info->ConfigData));
785 if (result >= 0) {
786 US_DEBUGP(" ISD200 Config Data was written successfully\n");
787 } else {
788 US_DEBUGP(" Request to write ISD200 Config Data failed!\n");
789 retStatus = ISD200_ERROR;
792 US_DEBUGP("Leaving isd200_write_config %08X\n", retStatus);
793 return retStatus;
797 /**************************************************************************
798 * isd200_read_config
800 * Reads the ISD200 Configuration data
802 * RETURNS:
803 * ISD status code
805 static int isd200_read_config( struct us_data *us )
807 struct isd200_info *info = (struct isd200_info *)us->extra;
808 int retStatus = ISD200_GOOD;
809 int result;
811 US_DEBUGP("Entering isd200_read_config\n");
813 /* read the configuration information from ISD200. Use this to */
814 /* determine what the special ATA CDB bytes are. */
816 result = usb_stor_ctrl_transfer(
817 us,
818 us->recv_ctrl_pipe,
819 0x02,
820 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
821 0x0000,
822 0x0002,
823 (void *) &info->ConfigData,
824 sizeof(info->ConfigData));
827 if (result >= 0) {
828 US_DEBUGP(" Retrieved the following ISD200 Config Data:\n");
829 #ifdef CONFIG_USB_STORAGE_DEBUG
830 isd200_log_config(info);
831 #endif
832 } else {
833 US_DEBUGP(" Request to get ISD200 Config Data failed!\n");
834 retStatus = ISD200_ERROR;
837 US_DEBUGP("Leaving isd200_read_config %08X\n", retStatus);
838 return retStatus;
842 /**************************************************************************
843 * isd200_atapi_soft_reset
845 * Perform an Atapi Soft Reset on the device
847 * RETURNS:
848 * NT status code
850 static int isd200_atapi_soft_reset( struct us_data *us )
852 int retStatus = ISD200_GOOD;
853 int transferStatus;
855 US_DEBUGP("Entering isd200_atapi_soft_reset\n");
857 transferStatus = isd200_action( us, ACTION_SOFT_RESET, NULL, 0 );
858 if (transferStatus != ISD200_TRANSPORT_GOOD) {
859 US_DEBUGP(" Error issuing Atapi Soft Reset\n");
860 retStatus = ISD200_ERROR;
863 US_DEBUGP("Leaving isd200_atapi_soft_reset %08X\n", retStatus);
864 return retStatus;
868 /**************************************************************************
869 * isd200_srst
871 * Perform an SRST on the device
873 * RETURNS:
874 * ISD status code
876 static int isd200_srst( struct us_data *us )
878 int retStatus = ISD200_GOOD;
879 int transferStatus;
881 US_DEBUGP("Entering isd200_SRST\n");
883 transferStatus = isd200_action( us, ACTION_RESET, NULL, 0 );
885 /* check to see if this request failed */
886 if (transferStatus != ISD200_TRANSPORT_GOOD) {
887 US_DEBUGP(" Error issuing SRST\n");
888 retStatus = ISD200_ERROR;
889 } else {
890 /* delay 10ms to give the drive a chance to see it */
891 msleep(10);
893 transferStatus = isd200_action( us, ACTION_REENABLE, NULL, 0 );
894 if (transferStatus != ISD200_TRANSPORT_GOOD) {
895 US_DEBUGP(" Error taking drive out of reset\n");
896 retStatus = ISD200_ERROR;
897 } else {
898 /* delay 50ms to give the drive a chance to recover after SRST */
899 msleep(50);
903 US_DEBUGP("Leaving isd200_srst %08X\n", retStatus);
904 return retStatus;
908 /**************************************************************************
909 * isd200_try_enum
911 * Helper function for isd200_manual_enum(). Does ENUM and READ_STATUS
912 * and tries to analyze the status registers
914 * RETURNS:
915 * ISD status code
917 static int isd200_try_enum(struct us_data *us, unsigned char master_slave,
918 int detect )
920 int status = ISD200_GOOD;
921 unsigned long endTime;
922 struct isd200_info *info = (struct isd200_info *)us->extra;
923 unsigned char *regs = info->RegsBuf;
924 int recheckAsMaster = 0;
926 if ( detect )
927 endTime = jiffies + ISD200_ENUM_DETECT_TIMEOUT * HZ;
928 else
929 endTime = jiffies + ISD200_ENUM_BSY_TIMEOUT * HZ;
931 /* loop until we detect !BSY or timeout */
932 while(1) {
933 #ifdef CONFIG_USB_STORAGE_DEBUG
934 char* mstr = master_slave == ATA_ADDRESS_DEVHEAD_STD ?
935 "Master" : "Slave";
936 #endif
938 status = isd200_action( us, ACTION_ENUM, NULL, master_slave );
939 if ( status != ISD200_GOOD )
940 break;
942 status = isd200_action( us, ACTION_READ_STATUS,
943 regs, 8 );
944 if ( status != ISD200_GOOD )
945 break;
947 if (!detect) {
948 if (regs[ATA_REG_STATUS_OFFSET] & ATA_BUSY) {
949 US_DEBUGP(" %s status is still BSY, try again...\n",mstr);
950 } else {
951 US_DEBUGP(" %s status !BSY, continue with next operation\n",mstr);
952 break;
955 /* check for ATA_BUSY and */
956 else if (regs[ATA_REG_STATUS_OFFSET] &
957 (ATA_BUSY | ATA_DF | ATA_ERR)) {
958 US_DEBUGP(" Status indicates it is not ready, try again...\n");
960 /* check for DRDY, ATA devices set DRDY after SRST */
961 else if (regs[ATA_REG_STATUS_OFFSET] & ATA_DRDY) {
962 US_DEBUGP(" Identified ATA device\n");
963 info->DeviceFlags |= DF_ATA_DEVICE;
964 info->DeviceHead = master_slave;
965 break;
967 /* check Cylinder High/Low to
968 determine if it is an ATAPI device
970 else if (regs[ATA_REG_HCYL_OFFSET] == 0xEB &&
971 regs[ATA_REG_LCYL_OFFSET] == 0x14) {
972 /* It seems that the RICOH
973 MP6200A CD/RW drive will
974 report itself okay as a
975 slave when it is really a
976 master. So this check again
977 as a master device just to
978 make sure it doesn't report
979 itself okay as a master also
981 if ((master_slave & ATA_ADDRESS_DEVHEAD_SLAVE) &&
982 !recheckAsMaster) {
983 US_DEBUGP(" Identified ATAPI device as slave. Rechecking again as master\n");
984 recheckAsMaster = 1;
985 master_slave = ATA_ADDRESS_DEVHEAD_STD;
986 } else {
987 US_DEBUGP(" Identified ATAPI device\n");
988 info->DeviceHead = master_slave;
990 status = isd200_atapi_soft_reset(us);
991 break;
993 } else {
994 US_DEBUGP(" Not ATA, not ATAPI. Weird.\n");
995 break;
998 /* check for timeout on this request */
999 if (time_after_eq(jiffies, endTime)) {
1000 if (!detect)
1001 US_DEBUGP(" BSY check timeout, just continue with next operation...\n");
1002 else
1003 US_DEBUGP(" Device detect timeout!\n");
1004 break;
1008 return status;
1011 /**************************************************************************
1012 * isd200_manual_enum
1014 * Determines if the drive attached is an ATA or ATAPI and if it is a
1015 * master or slave.
1017 * RETURNS:
1018 * ISD status code
1020 static int isd200_manual_enum(struct us_data *us)
1022 struct isd200_info *info = (struct isd200_info *)us->extra;
1023 int retStatus = ISD200_GOOD;
1025 US_DEBUGP("Entering isd200_manual_enum\n");
1027 retStatus = isd200_read_config(us);
1028 if (retStatus == ISD200_GOOD) {
1029 int isslave;
1030 /* master or slave? */
1031 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 0);
1032 if (retStatus == ISD200_GOOD)
1033 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_SLAVE, 0);
1035 if (retStatus == ISD200_GOOD) {
1036 retStatus = isd200_srst(us);
1037 if (retStatus == ISD200_GOOD)
1038 /* ata or atapi? */
1039 retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 1);
1042 isslave = (info->DeviceHead & ATA_ADDRESS_DEVHEAD_SLAVE) ? 1 : 0;
1043 if (!(info->ConfigData.ATAConfig & ATACFG_MASTER)) {
1044 US_DEBUGP(" Setting Master/Slave selection to %d\n", isslave);
1045 info->ConfigData.ATAConfig &= 0x3f;
1046 info->ConfigData.ATAConfig |= (isslave<<6);
1047 retStatus = isd200_write_config(us);
1051 US_DEBUGP("Leaving isd200_manual_enum %08X\n", retStatus);
1052 return(retStatus);
1055 static void isd200_fix_driveid(u16 *id)
1057 #ifndef __LITTLE_ENDIAN
1058 # ifdef __BIG_ENDIAN
1059 int i;
1061 for (i = 0; i < ATA_ID_WORDS; i++)
1062 id[i] = __le16_to_cpu(id[i]);
1063 # else
1064 # error "Please fix <asm/byteorder.h>"
1065 # endif
1066 #endif
1069 static void isd200_dump_driveid(u16 *id)
1071 US_DEBUGP(" Identify Data Structure:\n");
1072 US_DEBUGP(" config = 0x%x\n", id[ATA_ID_CONFIG]);
1073 US_DEBUGP(" cyls = 0x%x\n", id[ATA_ID_CYLS]);
1074 US_DEBUGP(" heads = 0x%x\n", id[ATA_ID_HEADS]);
1075 US_DEBUGP(" track_bytes = 0x%x\n", id[4]);
1076 US_DEBUGP(" sector_bytes = 0x%x\n", id[5]);
1077 US_DEBUGP(" sectors = 0x%x\n", id[ATA_ID_SECTORS]);
1078 US_DEBUGP(" serial_no[0] = 0x%x\n", *(char *)&id[ATA_ID_SERNO]);
1079 US_DEBUGP(" buf_type = 0x%x\n", id[20]);
1080 US_DEBUGP(" buf_size = 0x%x\n", id[ATA_ID_BUF_SIZE]);
1081 US_DEBUGP(" ecc_bytes = 0x%x\n", id[22]);
1082 US_DEBUGP(" fw_rev[0] = 0x%x\n", *(char *)&id[ATA_ID_FW_REV]);
1083 US_DEBUGP(" model[0] = 0x%x\n", *(char *)&id[ATA_ID_PROD]);
1084 US_DEBUGP(" max_multsect = 0x%x\n", id[ATA_ID_MAX_MULTSECT] & 0xff);
1085 US_DEBUGP(" dword_io = 0x%x\n", id[ATA_ID_DWORD_IO]);
1086 US_DEBUGP(" capability = 0x%x\n", id[ATA_ID_CAPABILITY] >> 8);
1087 US_DEBUGP(" tPIO = 0x%x\n", id[ATA_ID_OLD_PIO_MODES] >> 8);
1088 US_DEBUGP(" tDMA = 0x%x\n", id[ATA_ID_OLD_DMA_MODES] >> 8);
1089 US_DEBUGP(" field_valid = 0x%x\n", id[ATA_ID_FIELD_VALID]);
1090 US_DEBUGP(" cur_cyls = 0x%x\n", id[ATA_ID_CUR_CYLS]);
1091 US_DEBUGP(" cur_heads = 0x%x\n", id[ATA_ID_CUR_HEADS]);
1092 US_DEBUGP(" cur_sectors = 0x%x\n", id[ATA_ID_CUR_SECTORS]);
1093 US_DEBUGP(" cur_capacity = 0x%x\n", ata_id_u32(id, 57));
1094 US_DEBUGP(" multsect = 0x%x\n", id[ATA_ID_MULTSECT] & 0xff);
1095 US_DEBUGP(" lba_capacity = 0x%x\n", ata_id_u32(id, ATA_ID_LBA_CAPACITY));
1096 US_DEBUGP(" command_set_1 = 0x%x\n", id[ATA_ID_COMMAND_SET_1]);
1097 US_DEBUGP(" command_set_2 = 0x%x\n", id[ATA_ID_COMMAND_SET_2]);
1100 /**************************************************************************
1101 * isd200_get_inquiry_data
1103 * Get inquiry data
1105 * RETURNS:
1106 * ISD status code
1108 static int isd200_get_inquiry_data( struct us_data *us )
1110 struct isd200_info *info = (struct isd200_info *)us->extra;
1111 int retStatus = ISD200_GOOD;
1112 u16 *id = info->id;
1114 US_DEBUGP("Entering isd200_get_inquiry_data\n");
1116 /* set default to Master */
1117 info->DeviceHead = ATA_ADDRESS_DEVHEAD_STD;
1119 /* attempt to manually enumerate this device */
1120 retStatus = isd200_manual_enum(us);
1121 if (retStatus == ISD200_GOOD) {
1122 int transferStatus;
1124 /* check for an ATA device */
1125 if (info->DeviceFlags & DF_ATA_DEVICE) {
1126 /* this must be an ATA device */
1127 /* perform an ATA Command Identify */
1128 transferStatus = isd200_action( us, ACTION_IDENTIFY,
1129 id, ATA_ID_WORDS * 2);
1130 if (transferStatus != ISD200_TRANSPORT_GOOD) {
1131 /* Error issuing ATA Command Identify */
1132 US_DEBUGP(" Error issuing ATA Command Identify\n");
1133 retStatus = ISD200_ERROR;
1134 } else {
1135 /* ATA Command Identify successful */
1136 int i;
1137 __be16 *src;
1138 __u16 *dest;
1140 isd200_fix_driveid(id);
1141 isd200_dump_driveid(id);
1143 memset(&info->InquiryData, 0, sizeof(info->InquiryData));
1145 /* Standard IDE interface only supports disks */
1146 info->InquiryData.DeviceType = DIRECT_ACCESS_DEVICE;
1148 /* The length must be at least 36 (5 + 31) */
1149 info->InquiryData.AdditionalLength = 0x1F;
1151 if (id[ATA_ID_COMMAND_SET_1] & COMMANDSET_MEDIA_STATUS) {
1152 /* set the removable bit */
1153 info->InquiryData.DeviceTypeModifier = DEVICE_REMOVABLE;
1154 info->DeviceFlags |= DF_REMOVABLE_MEDIA;
1157 /* Fill in vendor identification fields */
1158 src = (__be16 *)&id[ATA_ID_PROD];
1159 dest = (__u16*)info->InquiryData.VendorId;
1160 for (i=0;i<4;i++)
1161 dest[i] = be16_to_cpu(src[i]);
1163 src = (__be16 *)&id[ATA_ID_PROD + 8/2];
1164 dest = (__u16*)info->InquiryData.ProductId;
1165 for (i=0;i<8;i++)
1166 dest[i] = be16_to_cpu(src[i]);
1168 src = (__be16 *)&id[ATA_ID_FW_REV];
1169 dest = (__u16*)info->InquiryData.ProductRevisionLevel;
1170 for (i=0;i<2;i++)
1171 dest[i] = be16_to_cpu(src[i]);
1173 /* determine if it supports Media Status Notification */
1174 if (id[ATA_ID_COMMAND_SET_2] & COMMANDSET_MEDIA_STATUS) {
1175 US_DEBUGP(" Device supports Media Status Notification\n");
1177 /* Indicate that it is enabled, even though it is not
1178 * This allows the lock/unlock of the media to work
1179 * correctly.
1181 info->DeviceFlags |= DF_MEDIA_STATUS_ENABLED;
1183 else
1184 info->DeviceFlags &= ~DF_MEDIA_STATUS_ENABLED;
1187 } else {
1189 * this must be an ATAPI device
1190 * use an ATAPI protocol (Transparent SCSI)
1192 us->protocol_name = "Transparent SCSI";
1193 us->proto_handler = usb_stor_transparent_scsi_command;
1195 US_DEBUGP("Protocol changed to: %s\n", us->protocol_name);
1197 /* Free driver structure */
1198 us->extra_destructor(info);
1199 kfree(info);
1200 us->extra = NULL;
1201 us->extra_destructor = NULL;
1205 US_DEBUGP("Leaving isd200_get_inquiry_data %08X\n", retStatus);
1207 return(retStatus);
1210 /**************************************************************************
1211 * isd200_scsi_to_ata
1213 * Translate SCSI commands to ATA commands.
1215 * RETURNS:
1216 * 1 if the command needs to be sent to the transport layer
1217 * 0 otherwise
1219 static int isd200_scsi_to_ata(struct scsi_cmnd *srb, struct us_data *us,
1220 union ata_cdb * ataCdb)
1222 struct isd200_info *info = (struct isd200_info *)us->extra;
1223 u16 *id = info->id;
1224 int sendToTransport = 1;
1225 unsigned char sectnum, head;
1226 unsigned short cylinder;
1227 unsigned long lba;
1228 unsigned long blockCount;
1229 unsigned char senseData[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1231 memset(ataCdb, 0, sizeof(union ata_cdb));
1233 /* SCSI Command */
1234 switch (srb->cmnd[0]) {
1235 case INQUIRY:
1236 US_DEBUGP(" ATA OUT - INQUIRY\n");
1238 /* copy InquiryData */
1239 usb_stor_set_xfer_buf((unsigned char *) &info->InquiryData,
1240 sizeof(info->InquiryData), srb);
1241 srb->result = SAM_STAT_GOOD;
1242 sendToTransport = 0;
1243 break;
1245 case MODE_SENSE:
1246 US_DEBUGP(" ATA OUT - SCSIOP_MODE_SENSE\n");
1248 /* Initialize the return buffer */
1249 usb_stor_set_xfer_buf(senseData, sizeof(senseData), srb);
1251 if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
1253 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1254 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1255 ataCdb->generic.TransferBlockSize = 1;
1256 ataCdb->generic.RegisterSelect = REG_COMMAND;
1257 ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1258 isd200_srb_set_bufflen(srb, 0);
1259 } else {
1260 US_DEBUGP(" Media Status not supported, just report okay\n");
1261 srb->result = SAM_STAT_GOOD;
1262 sendToTransport = 0;
1264 break;
1266 case TEST_UNIT_READY:
1267 US_DEBUGP(" ATA OUT - SCSIOP_TEST_UNIT_READY\n");
1269 if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
1271 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1272 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1273 ataCdb->generic.TransferBlockSize = 1;
1274 ataCdb->generic.RegisterSelect = REG_COMMAND;
1275 ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1276 isd200_srb_set_bufflen(srb, 0);
1277 } else {
1278 US_DEBUGP(" Media Status not supported, just report okay\n");
1279 srb->result = SAM_STAT_GOOD;
1280 sendToTransport = 0;
1282 break;
1284 case READ_CAPACITY:
1286 unsigned long capacity;
1287 struct read_capacity_data readCapacityData;
1289 US_DEBUGP(" ATA OUT - SCSIOP_READ_CAPACITY\n");
1291 if (ata_id_has_lba(id))
1292 capacity = ata_id_u32(id, ATA_ID_LBA_CAPACITY) - 1;
1293 else
1294 capacity = (id[ATA_ID_HEADS] * id[ATA_ID_CYLS] *
1295 id[ATA_ID_SECTORS]) - 1;
1297 readCapacityData.LogicalBlockAddress = cpu_to_be32(capacity);
1298 readCapacityData.BytesPerBlock = cpu_to_be32(0x200);
1300 usb_stor_set_xfer_buf((unsigned char *) &readCapacityData,
1301 sizeof(readCapacityData), srb);
1302 srb->result = SAM_STAT_GOOD;
1303 sendToTransport = 0;
1305 break;
1307 case READ_10:
1308 US_DEBUGP(" ATA OUT - SCSIOP_READ\n");
1310 lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1311 blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1313 if (ata_id_has_lba(id)) {
1314 sectnum = (unsigned char)(lba);
1315 cylinder = (unsigned short)(lba>>8);
1316 head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1317 } else {
1318 sectnum = (u8)((lba % id[ATA_ID_SECTORS]) + 1);
1319 cylinder = (u16)(lba / (id[ATA_ID_SECTORS] *
1320 id[ATA_ID_HEADS]));
1321 head = (u8)((lba / id[ATA_ID_SECTORS]) %
1322 id[ATA_ID_HEADS]);
1324 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1325 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1326 ataCdb->generic.TransferBlockSize = 1;
1327 ataCdb->generic.RegisterSelect =
1328 REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
1329 REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
1330 REG_DEVICE_HEAD | REG_COMMAND;
1331 ataCdb->write.SectorCountByte = (unsigned char)blockCount;
1332 ataCdb->write.SectorNumberByte = sectnum;
1333 ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
1334 ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
1335 ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
1336 ataCdb->write.CommandByte = ATA_CMD_PIO_READ;
1337 break;
1339 case WRITE_10:
1340 US_DEBUGP(" ATA OUT - SCSIOP_WRITE\n");
1342 lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
1343 blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
1345 if (ata_id_has_lba(id)) {
1346 sectnum = (unsigned char)(lba);
1347 cylinder = (unsigned short)(lba>>8);
1348 head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
1349 } else {
1350 sectnum = (u8)((lba % id[ATA_ID_SECTORS]) + 1);
1351 cylinder = (u16)(lba / (id[ATA_ID_SECTORS] *
1352 id[ATA_ID_HEADS]));
1353 head = (u8)((lba / id[ATA_ID_SECTORS]) %
1354 id[ATA_ID_HEADS]);
1356 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1357 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1358 ataCdb->generic.TransferBlockSize = 1;
1359 ataCdb->generic.RegisterSelect =
1360 REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
1361 REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
1362 REG_DEVICE_HEAD | REG_COMMAND;
1363 ataCdb->write.SectorCountByte = (unsigned char)blockCount;
1364 ataCdb->write.SectorNumberByte = sectnum;
1365 ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
1366 ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
1367 ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
1368 ataCdb->write.CommandByte = ATA_CMD_PIO_WRITE;
1369 break;
1371 case ALLOW_MEDIUM_REMOVAL:
1372 US_DEBUGP(" ATA OUT - SCSIOP_MEDIUM_REMOVAL\n");
1374 if (info->DeviceFlags & DF_REMOVABLE_MEDIA) {
1375 US_DEBUGP(" srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
1377 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1378 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1379 ataCdb->generic.TransferBlockSize = 1;
1380 ataCdb->generic.RegisterSelect = REG_COMMAND;
1381 ataCdb->write.CommandByte = (srb->cmnd[4] & 0x1) ?
1382 ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK;
1383 isd200_srb_set_bufflen(srb, 0);
1384 } else {
1385 US_DEBUGP(" Not removeable media, just report okay\n");
1386 srb->result = SAM_STAT_GOOD;
1387 sendToTransport = 0;
1389 break;
1391 case START_STOP:
1392 US_DEBUGP(" ATA OUT - SCSIOP_START_STOP_UNIT\n");
1393 US_DEBUGP(" srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
1395 if ((srb->cmnd[4] & 0x3) == 0x2) {
1396 US_DEBUGP(" Media Eject\n");
1397 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1398 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1399 ataCdb->generic.TransferBlockSize = 0;
1400 ataCdb->generic.RegisterSelect = REG_COMMAND;
1401 ataCdb->write.CommandByte = ATA_COMMAND_MEDIA_EJECT;
1402 } else if ((srb->cmnd[4] & 0x3) == 0x1) {
1403 US_DEBUGP(" Get Media Status\n");
1404 ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
1405 ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
1406 ataCdb->generic.TransferBlockSize = 1;
1407 ataCdb->generic.RegisterSelect = REG_COMMAND;
1408 ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
1409 isd200_srb_set_bufflen(srb, 0);
1410 } else {
1411 US_DEBUGP(" Nothing to do, just report okay\n");
1412 srb->result = SAM_STAT_GOOD;
1413 sendToTransport = 0;
1415 break;
1417 default:
1418 US_DEBUGP("Unsupported SCSI command - 0x%X\n", srb->cmnd[0]);
1419 srb->result = DID_ERROR << 16;
1420 sendToTransport = 0;
1421 break;
1424 return(sendToTransport);
1428 /**************************************************************************
1429 * isd200_free_info
1431 * Frees the driver structure.
1433 static void isd200_free_info_ptrs(void *info_)
1435 struct isd200_info *info = (struct isd200_info *) info_;
1437 if (info) {
1438 kfree(info->id);
1439 kfree(info->RegsBuf);
1440 kfree(info->srb.sense_buffer);
1444 /**************************************************************************
1445 * isd200_init_info
1447 * Allocates (if necessary) and initializes the driver structure.
1449 * RETURNS:
1450 * ISD status code
1452 static int isd200_init_info(struct us_data *us)
1454 int retStatus = ISD200_GOOD;
1455 struct isd200_info *info;
1457 info = kzalloc(sizeof(struct isd200_info), GFP_KERNEL);
1458 if (!info)
1459 retStatus = ISD200_ERROR;
1460 else {
1461 info->id = kzalloc(ATA_ID_WORDS * 2, GFP_KERNEL);
1462 info->RegsBuf = (unsigned char *)
1463 kmalloc(sizeof(info->ATARegs), GFP_KERNEL);
1464 info->srb.sense_buffer =
1465 kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
1466 if (!info->id || !info->RegsBuf || !info->srb.sense_buffer) {
1467 isd200_free_info_ptrs(info);
1468 kfree(info);
1469 retStatus = ISD200_ERROR;
1473 if (retStatus == ISD200_GOOD) {
1474 us->extra = info;
1475 us->extra_destructor = isd200_free_info_ptrs;
1476 } else
1477 US_DEBUGP("ERROR - kmalloc failure\n");
1479 return retStatus;
1482 /**************************************************************************
1483 * Initialization for the ISD200
1486 static int isd200_Initialization(struct us_data *us)
1488 US_DEBUGP("ISD200 Initialization...\n");
1490 /* Initialize ISD200 info struct */
1492 if (isd200_init_info(us) == ISD200_ERROR) {
1493 US_DEBUGP("ERROR Initializing ISD200 Info struct\n");
1494 } else {
1495 /* Get device specific data */
1497 if (isd200_get_inquiry_data(us) != ISD200_GOOD)
1498 US_DEBUGP("ISD200 Initialization Failure\n");
1499 else
1500 US_DEBUGP("ISD200 Initialization complete\n");
1503 return 0;
1507 /**************************************************************************
1508 * Protocol and Transport for the ISD200 ASIC
1510 * This protocol and transport are for ATA devices connected to an ISD200
1511 * ASIC. An ATAPI device that is conected as a slave device will be
1512 * detected in the driver initialization function and the protocol will
1513 * be changed to an ATAPI protocol (Transparent SCSI).
1517 static void isd200_ata_command(struct scsi_cmnd *srb, struct us_data *us)
1519 int sendToTransport = 1, orig_bufflen;
1520 union ata_cdb ataCdb;
1522 /* Make sure driver was initialized */
1524 if (us->extra == NULL)
1525 US_DEBUGP("ERROR Driver not initialized\n");
1527 scsi_set_resid(srb, 0);
1528 /* scsi_bufflen might change in protocol translation to ata */
1529 orig_bufflen = scsi_bufflen(srb);
1530 sendToTransport = isd200_scsi_to_ata(srb, us, &ataCdb);
1532 /* send the command to the transport layer */
1533 if (sendToTransport)
1534 isd200_invoke_transport(us, srb, &ataCdb);
1536 isd200_srb_set_bufflen(srb, orig_bufflen);
1539 static int isd200_probe(struct usb_interface *intf,
1540 const struct usb_device_id *id)
1542 struct us_data *us;
1543 int result;
1545 result = usb_stor_probe1(&us, intf, id,
1546 (id - isd200_usb_ids) + isd200_unusual_dev_list);
1547 if (result)
1548 return result;
1550 us->protocol_name = "ISD200 ATA/ATAPI";
1551 us->proto_handler = isd200_ata_command;
1553 result = usb_stor_probe2(us);
1554 return result;
1557 static struct usb_driver isd200_driver = {
1558 .name = "ums-isd200",
1559 .probe = isd200_probe,
1560 .disconnect = usb_stor_disconnect,
1561 .suspend = usb_stor_suspend,
1562 .resume = usb_stor_resume,
1563 .reset_resume = usb_stor_reset_resume,
1564 .pre_reset = usb_stor_pre_reset,
1565 .post_reset = usb_stor_post_reset,
1566 .id_table = isd200_usb_ids,
1567 .soft_unbind = 1,
1570 static int __init isd200_init(void)
1572 return usb_register(&isd200_driver);
1575 static void __exit isd200_exit(void)
1577 usb_deregister(&isd200_driver);
1580 module_init(isd200_init);
1581 module_exit(isd200_exit);