46 #include <linux/errno.h>
47 #include <linux/module.h>
48 #include <linux/slab.h>
53 #include <scsi/scsi.h>
67 static int isd200_Initialization(
struct us_data *us);
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) }
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, \
110 #define ISD200_ENUM_BSY_TIMEOUT 35
111 #define ISD200_ENUM_DETECT_TIMEOUT 30
112 #define ISD200_DEFAULT_TIMEOUT 30
115 #define DF_ATA_DEVICE 0x0001
116 #define DF_MEDIA_STATUS_ENABLED 0x0002
117 #define DF_REMOVABLE_MEDIA 0x0004
120 #define CAPABILITY_DMA 0x01
121 #define CAPABILITY_LBA 0x02
124 #define COMMANDSET_REMOVABLE 0x02
125 #define COMMANDSET_MEDIA_STATUS 0x10
128 #define ATA_ADDRESS_DEVHEAD_STD 0xa0
129 #define ATA_ADDRESS_DEVHEAD_LBA_MODE 0x40
130 #define ATA_ADDRESS_DEVHEAD_SLAVE 0x10
133 #define ACTION_SELECT_0 0x01
134 #define ACTION_SELECT_1 0x02
135 #define ACTION_SELECT_2 0x04
136 #define ACTION_SELECT_3 0x08
137 #define ACTION_SELECT_4 0x10
138 #define ACTION_SELECT_5 0x20
139 #define ACTION_SELECT_6 0x40
140 #define ACTION_SELECT_7 0x80
143 #define REG_ALTERNATE_STATUS 0x01
144 #define REG_DEVICE_CONTROL 0x01
145 #define REG_ERROR 0x02
146 #define REG_FEATURES 0x02
147 #define REG_SECTOR_COUNT 0x04
148 #define REG_SECTOR_NUMBER 0x08
149 #define REG_CYLINDER_LOW 0x10
150 #define REG_CYLINDER_HIGH 0x20
151 #define REG_DEVICE_HEAD 0x40
152 #define REG_STATUS 0x80
153 #define REG_COMMAND 0x80
156 #define ATA_REG_ERROR_OFFSET 1
157 #define ATA_REG_LCYL_OFFSET 4
158 #define ATA_REG_HCYL_OFFSET 5
159 #define ATA_REG_STATUS_OFFSET 7
162 #define ATA_ERROR_MEDIA_CHANGE 0x20
165 #define ATA_COMMAND_GET_MEDIA_STATUS 0xDA
166 #define ATA_COMMAND_MEDIA_EJECT 0xED
169 #define ATA_DC_DISABLE_INTERRUPTS 0x02
170 #define ATA_DC_RESET_CONTROLLER 0x04
171 #define ATA_DC_REENABLE_CONTROLLER 0x00
177 #define ISD200_ERROR -1
178 #define ISD200_GOOD 0
184 #define ISD200_TRANSPORT_GOOD 0
185 #define ISD200_TRANSPORT_FAILED 1
186 #define ISD200_TRANSPORT_ERROR 2
189 #define ACTION_READ_STATUS 0
190 #define ACTION_RESET 1
191 #define ACTION_REENABLE 2
192 #define ACTION_SOFT_RESET 3
193 #define ACTION_ENUM 4
194 #define ACTION_IDENTIFY 5
268 #define DIRECT_ACCESS_DEVICE 0x00
269 #define DEVICE_REMOVABLE 0x80
290 #define INQUIRYDATABUFFERSIZE 36
297 #define ATACFG_TIMING 0x0f
298 #define ATACFG_ATAPI_RESET 0x10
299 #define ATACFG_MASTER 0x20
300 #define ATACFG_BLOCKSIZE 0xa0
302 #define ATACFGE_LAST_LUN 0x07
303 #define ATACFGE_DESC_OVERRIDE 0x08
304 #define ATACFGE_STATE_SUSPEND 0x10
305 #define ATACFGE_SKIP_BOOT 0x20
306 #define ATACFGE_CONF_DESC2 0x40
307 #define ATACFGE_INIT_STATUS 0x80
309 #define CFG_CAPABILITY_SRST 0x01
332 unsigned char ATARegs[8];
338 unsigned char cmnd[BLK_MAX_CDB];
361 unsigned char BlockMaximumSize[3];
362 unsigned char BlockMinimumSize[2];
370 #define SENSE_ERRCODE 0x7f
371 #define SENSE_ERRCODE_VALID 0x80
372 #define SENSE_FLAG_SENSE_KEY 0x0f
373 #define SENSE_FLAG_BAD_LENGTH 0x20
374 #define SENSE_FLAG_END_OF_MEDIA 0x40
375 #define SENSE_FLAG_FILE_MARK 0x80
393 #define SENSE_BUFFER_SIZE 18
416 buf->AdditionalSenseLength = 0xb;
418 buf->AdditionalSenseCode = 0;
419 buf->AdditionalSenseCodeQualifier = 0;
422 buf->AdditionalSenseLength = 0xb;
424 buf->AdditionalSenseCode = 0;
425 buf->AdditionalSenseCodeQualifier = 0;
428 buf->AdditionalSenseLength = 0xb;
430 buf->AdditionalSenseCode = 0;
431 buf->AdditionalSenseCodeQualifier = 0;
434 buf->AdditionalSenseLength = 0xb;
436 buf->AdditionalSenseCode = 0;
437 buf->AdditionalSenseCodeQualifier = 0;
440 buf->AdditionalSenseLength = 0;
442 buf->AdditionalSenseCode = 0;
443 buf->AdditionalSenseCodeQualifier = 0;
467 srb->
sdb.table.sgl = buff ? &info->
sg :
NULL;
469 srb->
sdb.table.nents = buff ? 1 : 0;
472 static void isd200_srb_set_bufflen(
struct scsi_cmnd *srb,
unsigned bufflen)
496 memset(&ata, 0,
sizeof(ata));
502 ata.
generic.TransferBlockSize = 1;
506 US_DEBUGP(
" isd200_action(READ_STATUS)\n");
515 US_DEBUGP(
" isd200_action(ENUM,0x%02x)\n",value);
543 US_DEBUGP(
" isd200_action(SOFT_RESET)\n");
560 US_DEBUGP(
"Error: Undefined action %d\n",action);
586 static int isd200_read_regs(
struct us_data *us )
592 US_DEBUGP(
"Entering isd200_IssueATAReadRegs\n");
597 US_DEBUGP(
" Error reading ATA registers\n");
601 US_DEBUGP(
" Got ATA Register[ATA_REG_ERROR_OFFSET] = 0x%x\n",
615 static void isd200_invoke_transport(
struct us_data *us,
619 int need_auto_sense = 0;
636 switch (transferStatus) {
644 US_DEBUGP(
"-- transport indicates protocol failure\n");
649 US_DEBUGP(
"-- transport indicates command failure\n");
654 US_DEBUGP(
"-- transport indicates transport error\n");
660 US_DEBUGP(
"-- transport indicates unknown error\n");
666 if ((scsi_get_resid(srb) > 0) &&
672 US_DEBUGP(
"-- unexpectedly short transfer\n");
676 if (need_auto_sense) {
677 result = isd200_read_regs(us);
683 isd200_build_sense(us, srb);
712 #ifdef CONFIG_USB_STORAGE_DEBUG
713 static void isd200_log_config(
struct isd200_info* info )
721 US_DEBUGP(
" ATAPI Command Block Size: 0x%x\n",
723 US_DEBUGP(
" Master/Slave Selection: 0x%x\n",
735 US_DEBUGP(
" Config Descriptor 2: 0x%x\n",
739 US_DEBUGP(
" ATA 3 State Supsend: 0x%x\n",
741 US_DEBUGP(
" Descriptor Override: 0x%x\n",
743 US_DEBUGP(
" Last LUN Identifier: 0x%x\n",
758 static int isd200_write_config(
struct us_data *us )
764 #ifdef CONFIG_USB_STORAGE_DEBUG
765 US_DEBUGP(
"Entering isd200_write_config\n");
766 US_DEBUGP(
" Writing the following ISD200 Config Data:\n");
767 isd200_log_config(info);
782 US_DEBUGP(
" ISD200 Config Data was written successfully\n");
784 US_DEBUGP(
" Request to write ISD200 Config Data failed!\n");
788 US_DEBUGP(
"Leaving isd200_write_config %08X\n", retStatus);
801 static int isd200_read_config(
struct us_data *us )
807 US_DEBUGP(
"Entering isd200_read_config\n");
824 US_DEBUGP(
" Retrieved the following ISD200 Config Data:\n");
825 #ifdef CONFIG_USB_STORAGE_DEBUG
826 isd200_log_config(info);
829 US_DEBUGP(
" Request to get ISD200 Config Data failed!\n");
833 US_DEBUGP(
"Leaving isd200_read_config %08X\n", retStatus);
846 static int isd200_atapi_soft_reset(
struct us_data *us )
851 US_DEBUGP(
"Entering isd200_atapi_soft_reset\n");
855 US_DEBUGP(
" Error issuing Atapi Soft Reset\n");
859 US_DEBUGP(
"Leaving isd200_atapi_soft_reset %08X\n", retStatus);
872 static int isd200_srst(
struct us_data *us )
891 US_DEBUGP(
" Error taking drive out of reset\n");
899 US_DEBUGP(
"Leaving isd200_srst %08X\n", retStatus);
913 static int isd200_try_enum(
struct us_data *us,
unsigned char master_slave,
917 unsigned long endTime;
920 int recheckAsMaster = 0;
929 #ifdef CONFIG_USB_STORAGE_DEBUG
945 US_DEBUGP(
" %s status is still BSY, try again...\n",mstr);
947 US_DEBUGP(
" %s status !BSY, continue with next operation\n",mstr);
956 US_DEBUGP(
" Status indicates it is not ready, try again...\n");
981 US_DEBUGP(
" Identified ATAPI device as slave. Rechecking again as master\n");
988 status = isd200_atapi_soft_reset(us);
992 US_DEBUGP(
" Not ATA, not ATAPI. Weird.\n");
999 US_DEBUGP(
" BSY check timeout, just continue with next operation...\n");
1018 static int isd200_manual_enum(
struct us_data *us)
1023 US_DEBUGP(
"Entering isd200_manual_enum\n");
1025 retStatus = isd200_read_config(us);
1034 retStatus = isd200_srst(us);
1042 US_DEBUGP(
" Setting Master/Slave selection to %d\n", isslave);
1045 retStatus = isd200_write_config(us);
1049 US_DEBUGP(
"Leaving isd200_manual_enum %08X\n", retStatus);
1053 static void isd200_fix_driveid(
u16 *
id)
1055 #ifndef __LITTLE_ENDIAN
1056 # ifdef __BIG_ENDIAN
1062 # error "Please fix <asm/byteorder.h>"
1067 static void isd200_dump_driveid(
u16 *
id)
1069 US_DEBUGP(
" Identify Data Structure:\n");
1073 US_DEBUGP(
" track_bytes = 0x%x\n",
id[4]);
1074 US_DEBUGP(
" sector_bytes = 0x%x\n",
id[5]);
1077 US_DEBUGP(
" buf_type = 0x%x\n",
id[20]);
1079 US_DEBUGP(
" ecc_bytes = 0x%x\n",
id[22]);
1106 static int isd200_get_inquiry_data(
struct us_data *us )
1112 US_DEBUGP(
"Entering isd200_get_inquiry_data\n");
1118 retStatus = isd200_manual_enum(us);
1127 id, ATA_ID_WORDS * 2);
1130 US_DEBUGP(
" Error issuing ATA Command Identify\n");
1138 isd200_fix_driveid(
id);
1139 isd200_dump_driveid(
id);
1173 US_DEBUGP(
" Device supports Media Status Notification\n");
1203 US_DEBUGP(
"Leaving isd200_get_inquiry_data %08X\n", retStatus);
1222 int sendToTransport = 1;
1223 unsigned char sectnum,
head;
1224 unsigned short cylinder;
1226 unsigned long blockCount;
1227 unsigned char senseData[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1232 switch (srb->
cmnd[0]) {
1240 sendToTransport = 0;
1244 US_DEBUGP(
" ATA OUT - SCSIOP_MODE_SENSE\n");
1253 ataCdb->
generic.TransferBlockSize = 1;
1256 isd200_srb_set_bufflen(srb, 0);
1258 US_DEBUGP(
" Media Status not supported, just report okay\n");
1260 sendToTransport = 0;
1265 US_DEBUGP(
" ATA OUT - SCSIOP_TEST_UNIT_READY\n");
1271 ataCdb->
generic.TransferBlockSize = 1;
1274 isd200_srb_set_bufflen(srb, 0);
1276 US_DEBUGP(
" Media Status not supported, just report okay\n");
1278 sendToTransport = 0;
1287 US_DEBUGP(
" ATA OUT - SCSIOP_READ_CAPACITY\n");
1299 sizeof(readCapacityData), srb);
1301 sendToTransport = 0;
1309 blockCount = (
unsigned long)srb->
cmnd[7]<<8 | (
unsigned long)srb->
cmnd[8];
1312 sectnum = (
unsigned char)(
lba);
1313 cylinder = (
unsigned short)(
lba>>8);
1317 cylinder = (
u16)(
lba / (
id[ATA_ID_SECTORS] *
1319 head = (
u8)((
lba /
id[ATA_ID_SECTORS]) %
1324 ataCdb->
generic.TransferBlockSize = 1;
1325 ataCdb->
generic.RegisterSelect =
1329 ataCdb->
write.SectorCountByte = (
unsigned char)blockCount;
1330 ataCdb->
write.SectorNumberByte = sectnum;
1331 ataCdb->
write.CylinderHighByte = (
unsigned char)(cylinder>>8);
1332 ataCdb->
write.CylinderLowByte = (
unsigned char)cylinder;
1341 blockCount = (
unsigned long)srb->
cmnd[7]<<8 | (
unsigned long)srb->
cmnd[8];
1344 sectnum = (
unsigned char)(
lba);
1345 cylinder = (
unsigned short)(
lba>>8);
1348 sectnum = (
u8)((
lba %
id[ATA_ID_SECTORS]) + 1);
1349 cylinder = (
u16)(
lba / (
id[ATA_ID_SECTORS] *
1351 head = (
u8)((
lba /
id[ATA_ID_SECTORS]) %
1356 ataCdb->
generic.TransferBlockSize = 1;
1357 ataCdb->
generic.RegisterSelect =
1361 ataCdb->
write.SectorCountByte = (
unsigned char)blockCount;
1362 ataCdb->
write.SectorNumberByte = sectnum;
1363 ataCdb->
write.CylinderHighByte = (
unsigned char)(cylinder>>8);
1364 ataCdb->
write.CylinderLowByte = (
unsigned char)cylinder;
1370 US_DEBUGP(
" ATA OUT - SCSIOP_MEDIUM_REMOVAL\n");
1377 ataCdb->
generic.TransferBlockSize = 1;
1379 ataCdb->
write.CommandByte = (srb->
cmnd[4] & 0x1) ?
1381 isd200_srb_set_bufflen(srb, 0);
1383 US_DEBUGP(
" Not removeable media, just report okay\n");
1385 sendToTransport = 0;
1390 US_DEBUGP(
" ATA OUT - SCSIOP_START_STOP_UNIT\n");
1393 if ((srb->
cmnd[4] & 0x3) == 0x2) {
1397 ataCdb->
generic.TransferBlockSize = 0;
1400 }
else if ((srb->
cmnd[4] & 0x3) == 0x1) {
1404 ataCdb->
generic.TransferBlockSize = 1;
1407 isd200_srb_set_bufflen(srb, 0);
1409 US_DEBUGP(
" Nothing to do, just report okay\n");
1411 sendToTransport = 0;
1416 US_DEBUGP(
"Unsupported SCSI command - 0x%X\n", srb->
cmnd[0]);
1418 sendToTransport = 0;
1422 return(sendToTransport);
1431 static void isd200_free_info_ptrs(
void *info_)
1450 static int isd200_init_info(
struct us_data *us)
1460 info->
RegsBuf = (
unsigned char *)
1462 info->
srb.sense_buffer =
1464 if (!info->
id || !info->
RegsBuf || !info->
srb.sense_buffer) {
1465 isd200_free_info_ptrs(info);
1484 static int isd200_Initialization(
struct us_data *us)
1486 US_DEBUGP(
"ISD200 Initialization...\n");
1491 US_DEBUGP(
"ERROR Initializing ISD200 Info struct\n");
1496 US_DEBUGP(
"ISD200 Initialization Failure\n");
1498 US_DEBUGP(
"ISD200 Initialization complete\n");
1517 int sendToTransport = 1, orig_bufflen;
1523 US_DEBUGP(
"ERROR Driver not initialized\n");
1525 scsi_set_resid(srb, 0);
1527 orig_bufflen = scsi_bufflen(srb);
1528 sendToTransport = isd200_scsi_to_ata(srb, us, &ataCdb);
1531 if (sendToTransport)
1532 isd200_invoke_transport(us, srb, &ataCdb);
1534 isd200_srb_set_bufflen(srb, orig_bufflen);
1544 (
id - isd200_usb_ids) + isd200_unusual_dev_list);
1555 static struct usb_driver isd200_driver = {
1556 .name =
"ums-isd200",
1557 .probe = isd200_probe,
1564 .id_table = isd200_usb_ids,