44 #include <linux/errno.h>
45 #include <linux/module.h>
46 #include <linux/slab.h>
47 #include <linux/cdrom.h>
49 #include <scsi/scsi.h>
62 #define USBAT_DEV_HP8200 0x01
63 #define USBAT_DEV_FLASH 0x02
65 #define USBAT_EPP_PORT 0x10
66 #define USBAT_EPP_REGISTER 0x30
67 #define USBAT_ATA 0x40
68 #define USBAT_ISA 0x50
71 #define USBAT_CMD_READ_REG 0x00
72 #define USBAT_CMD_WRITE_REG 0x01
73 #define USBAT_CMD_READ_BLOCK 0x02
74 #define USBAT_CMD_WRITE_BLOCK 0x03
75 #define USBAT_CMD_COND_READ_BLOCK 0x04
76 #define USBAT_CMD_COND_WRITE_BLOCK 0x05
77 #define USBAT_CMD_WRITE_REGS 0x07
80 #define USBAT_CMD_EXEC_CMD 0x80
81 #define USBAT_CMD_SET_FEAT 0x81
82 #define USBAT_CMD_UIO 0x82
85 #define USBAT_UIO_READ 1
86 #define USBAT_UIO_WRITE 0
89 #define USBAT_QUAL_FCQ 0x20
90 #define USBAT_QUAL_ALQ 0x10
93 #define USBAT_FLASH_MEDIA_NONE 0
94 #define USBAT_FLASH_MEDIA_CF 1
97 #define USBAT_FLASH_MEDIA_SAME 0
98 #define USBAT_FLASH_MEDIA_CHANGED 1
101 #define USBAT_ATA_DATA 0x10
102 #define USBAT_ATA_FEATURES 0x11
103 #define USBAT_ATA_ERROR 0x11
104 #define USBAT_ATA_SECCNT 0x12
105 #define USBAT_ATA_SECNUM 0x13
106 #define USBAT_ATA_LBA_ME 0x14
107 #define USBAT_ATA_LBA_HI 0x15
108 #define USBAT_ATA_DEVICE 0x16
109 #define USBAT_ATA_STATUS 0x17
110 #define USBAT_ATA_CMD 0x17
111 #define USBAT_ATA_ALTSTATUS 0x0E
114 #define USBAT_UIO_EPAD 0x80
115 #define USBAT_UIO_CDT 0x40
117 #define USBAT_UIO_1 0x20
118 #define USBAT_UIO_0 0x10
119 #define USBAT_UIO_EPP_ATA 0x08
120 #define USBAT_UIO_UI1 0x04
121 #define USBAT_UIO_UI0 0x02
122 #define USBAT_UIO_INTR_ACK 0x01
125 #define USBAT_UIO_DRVRST 0x80
126 #define USBAT_UIO_ACKD 0x40
127 #define USBAT_UIO_OE1 0x20
129 #define USBAT_UIO_OE0 0x10
130 #define USBAT_UIO_ADPRST 0x01
133 #define USBAT_FEAT_ETEN 0x80
134 #define USBAT_FEAT_U1 0x08
135 #define USBAT_FEAT_U0 0x04
136 #define USBAT_FEAT_ET1 0x02
137 #define USBAT_FEAT_ET2 0x01
151 #define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
152 #define LSB_of(s) ((s)&0xFF)
153 #define MSB_of(s) ((s)>>8)
155 static int transferred = 0;
160 static int init_usbat_cd(
struct us_data *us);
161 static int init_usbat_flash(
struct us_data *us);
167 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
168 vendorName, productName, useProtocol, useTransport, \
169 initFunction, flags) \
170 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
171 .driver_info = (flags) }
184 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
185 vendor_name, product_name, use_protocol, use_transport, \
186 init_function, Flags) \
188 .vendorName = vendor_name, \
189 .productName = product_name, \
190 .useProtocol = use_protocol, \
191 .useTransport = use_transport, \
192 .initFunction = init_function, \
206 static void usbat_pack_ata_sector_cmd(
unsigned char *
buf,
207 unsigned char thistime,
212 buf[2] = sector & 0xFF;
213 buf[3] = (sector >> 8) & 0xFF;
214 buf[4] = (sector >> 16) & 0xFF;
215 buf[5] = 0xE0 | ((sector >> 24) & 0x0F);
222 static int usbat_get_device_type(
struct us_data *us)
230 static int usbat_read(
struct us_data *us,
233 unsigned char *content)
248 static int usbat_write(
struct us_data *us,
251 unsigned char content)
266 static int usbat_bulk_read(
struct us_data *us,
274 US_DEBUGP(
"usbat_bulk_read: len = %d\n", len);
281 static int usbat_bulk_write(
struct us_data *us,
289 US_DEBUGP(
"usbat_bulk_write: len = %d\n", len);
298 static int usbat_execute_command(
struct us_data *us,
310 static int usbat_get_status(
struct us_data *us,
unsigned char *
status)
315 US_DEBUGP(
"usbat_get_status: 0x%02X\n", (
unsigned short) (*status));
322 static int usbat_check_status(
struct us_data *us)
324 unsigned char *reply = us->
iobuf;
327 rc = usbat_get_status(us, reply);
332 if (*reply & 0x01 && *reply != 0x51)
346 static int usbat_set_shuttle_features(
struct us_data *us,
347 unsigned char external_trigger,
348 unsigned char epp_control,
349 unsigned char mask_byte,
350 unsigned char test_pattern,
351 unsigned char subcountH,
352 unsigned char subcountL)
363 command[2] = epp_control;
371 command[3] = external_trigger;
378 command[4] = test_pattern;
384 command[5] = mask_byte;
392 command[6] = subcountL;
393 command[7] = subcountH;
395 return usbat_execute_command(us, command, 8);
402 static int usbat_wait_not_busy(
struct us_data *us,
int minutes)
406 unsigned char *status = us->
iobuf;
414 for (i=0; i<1200+minutes*60; i++) {
416 result = usbat_get_status(us, status);
420 if (*status & 0x01) {
421 result = usbat_read(us,
USBAT_ATA, 0x10, status);
427 if ((*status & 0x80)==0x00) {
428 US_DEBUGP(
"Waited not busy for %d steps\n", i);
442 US_DEBUGP(
"Waited not busy for %d minutes, timing out.\n",
450 static int usbat_read_block(
struct us_data *us,
470 result = usbat_execute_command(us, command, 8);
474 result = usbat_bulk_read(us, buf, len, use_sg);
482 static int usbat_write_block(
struct us_data *us,
483 unsigned char access,
490 unsigned char *command = us->
iobuf;
504 result = usbat_execute_command(us, command, 8);
509 result = usbat_bulk_write(us, buf, len, use_sg);
513 return usbat_wait_not_busy(us, minutes);
519 static int usbat_hp8200e_rw_block_test(
struct us_data *us,
520 unsigned char access,
522 unsigned char *data_out,
523 unsigned short num_registers,
524 unsigned char data_reg,
525 unsigned char status_reg,
526 unsigned char timeout,
527 unsigned char qualifier,
538 unsigned char *command = us->
iobuf;
542 unsigned char *status = us->
iobuf;
546 for (i=0; i<20; i++) {
573 command[6] =
LSB_of(num_registers*2);
574 command[7] =
MSB_of(num_registers*2);
579 command[cmdlen-8] = (direction==
DMA_TO_DEVICE ? 0x40 : 0xC0);
580 command[cmdlen-7] = access |
583 command[cmdlen-6] = data_reg;
584 command[cmdlen-5] = status_reg;
585 command[cmdlen-4] = timeout;
586 command[cmdlen-3] = qualifier;
587 command[cmdlen-2] =
LSB_of(len);
588 command[cmdlen-1] =
MSB_of(len);
590 result = usbat_execute_command(us, command, cmdlen);
597 for (
j=0;
j<num_registers;
j++) {
598 data[
j<<1] = registers[
j];
599 data[1+(
j<<1)] = data_out[
j];
602 result = usbat_bulk_write(us,
data, num_registers*2, 0);
609 pipe, buf, len, use_sg,
NULL);
666 return usbat_wait_not_busy(us, minutes);
670 US_DEBUGP(
"Bummer! %s bulk data 20 times failed.\n",
683 static int usbat_multiple_write(
struct us_data *us,
684 unsigned char *registers,
685 unsigned char *data_out,
686 unsigned short num_registers)
690 unsigned char *command = us->
iobuf;
705 command[6] =
LSB_of(num_registers*2);
706 command[7] =
MSB_of(num_registers*2);
709 result = usbat_execute_command(us, command, 8);
714 for (i=0; i<num_registers; i++) {
715 data[i<<1] = registers[
i];
716 data[1+(i<<1)] = data_out[i];
720 result = usbat_bulk_write(us, data, num_registers*2, 0);
725 return usbat_wait_not_busy(us, 0);
742 static int usbat_read_blocks(
struct us_data *us,
748 unsigned char *command = us->
iobuf;
760 result = usbat_execute_command(us, command, 8);
765 result = usbat_bulk_read(us, buffer, len, use_sg);
784 static int usbat_write_blocks(
struct us_data *us,
790 unsigned char *command = us->
iobuf;
802 result = usbat_execute_command(us, command, 8);
807 result = usbat_bulk_write(us, buffer, len, use_sg);
817 static int usbat_read_user_io(
struct us_data *us,
unsigned char *data_flags)
830 US_DEBUGP(
"usbat_read_user_io: UIO register reads %02X\n", (
unsigned short) (*data_flags));
838 static int usbat_write_user_io(
struct us_data *us,
839 unsigned char enable_flags,
840 unsigned char data_flags)
856 static int usbat_device_reset(
struct us_data *us)
864 rc = usbat_write_user_io(us,
874 rc = usbat_write_user_io(us,
886 static int usbat_device_enable_cdt(
struct us_data *us)
891 rc = usbat_write_user_io(us,
903 static int usbat_flash_check_media_present(
unsigned char *uio)
906 US_DEBUGP(
"usbat_flash_check_media_present: no media detected\n");
916 static int usbat_flash_check_media_changed(
unsigned char *uio)
919 US_DEBUGP(
"usbat_flash_check_media_changed: media change detected\n");
929 static int usbat_flash_check_media(
struct us_data *us,
933 unsigned char *uio = us->
iobuf;
935 rc = usbat_read_user_io(us, uio);
940 rc = usbat_flash_check_media_present(uio);
949 rc = usbat_flash_check_media_changed(uio);
953 rc = usbat_device_reset(us);
956 rc = usbat_device_enable_cdt(us);
962 rc = usbat_read_user_io(us, uio);
980 static int usbat_identify_device(
struct us_data *us,
989 rc = usbat_device_reset(us);
1004 rc = usbat_get_status(us, &status);
1009 if (status == 0xA1 || !(status & 0x01)) {
1011 US_DEBUGP(
"usbat_identify_device: Detected HP8200 CDRW\n");
1015 US_DEBUGP(
"usbat_identify_device: Detected Flash reader/writer\n");
1025 static int usbat_set_transport(
struct us_data *us,
1034 usbat_identify_device(us, info);
1041 us->
transport = usbat_hp8200e_transport;
1055 static int usbat_flash_get_sector_count(
struct us_data *us,
1058 unsigned char registers[3] = {
1063 unsigned char command[3] = { 0x01, 0xA0, 0xEC };
1064 unsigned char *reply;
1076 rc = usbat_multiple_write(us, registers, command, 3);
1078 US_DEBUGP(
"usbat_flash_get_sector_count: Gah! identify_device failed\n");
1092 rc = usbat_read_block(us, reply, 512, 0);
1097 ((
u32)(reply[116]) << 16) |
1098 ((
u32)(reply[115]) << 8) |
1099 ((
u32)(reply[114]) );
1111 static int usbat_flash_read_data(
struct us_data *us,
1116 unsigned char registers[7] = {
1125 unsigned char command[7];
1127 unsigned char thistime;
1128 unsigned int totallen, alloclen;
1130 unsigned int sg_offset = 0;
1133 result = usbat_flash_check_media(us, info);
1144 if (sector > 0x0FFFFFFF)
1147 totallen = sectors * info->
ssize;
1155 alloclen =
min(totallen, 65536
u);
1165 len =
min(totallen, alloclen);
1166 thistime = (len / info->
ssize) & 0xff;
1169 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20);
1172 result = usbat_multiple_write(us, registers, command, 7);
1177 result = usbat_read_blocks(us, buffer, len, 0);
1181 US_DEBUGP(
"usbat_flash_read_data: %d bytes\n", len);
1189 }
while (totallen > 0);
1202 static int usbat_flash_write_data(
struct us_data *us,
1207 unsigned char registers[7] = {
1216 unsigned char command[7];
1218 unsigned char thistime;
1219 unsigned int totallen, alloclen;
1221 unsigned int sg_offset = 0;
1224 result = usbat_flash_check_media(us, info);
1235 if (sector > 0x0FFFFFFF)
1238 totallen = sectors * info->
ssize;
1246 alloclen =
min(totallen, 65536
u);
1256 len =
min(totallen, alloclen);
1257 thistime = (len / info->
ssize) & 0xff;
1264 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
1267 result = usbat_multiple_write(us, registers, command, 7);
1272 result = usbat_write_blocks(us, buffer, len, 0);
1278 }
while (totallen > 0);
1292 static int usbat_hp8200e_handle_read10(
struct us_data *us,
1293 unsigned char *registers,
1294 unsigned char *data,
1301 unsigned int sg_offset = 0;
1304 US_DEBUGP(
"handle_read10: transfersize %d\n",
1307 if (scsi_bufflen(srb) < 0x10000) {
1309 result = usbat_hp8200e_rw_block_test(us,
USBAT_ATA,
1310 registers, data, 19,
1315 scsi_bufflen(srb), scsi_sg_count(srb), 1);
1332 US_DEBUGP(
"handle_read10: GPCMD_READ_CD: len %d\n", len);
1338 US_DEBUGP(
"handle_read10: transfersize 0, forcing %d\n",
1349 US_DEBUGP(
"Max read is %d bytes\n", len);
1350 len =
min(len, scsi_bufflen(srb));
1359 while (transferred != scsi_bufflen(srb)) {
1361 if (len > scsi_bufflen(srb) - transferred)
1362 len = scsi_bufflen(srb) - transferred;
1365 data[4] = (len>>8)&0xFF;
1369 data[7+2] =
MSB_of(sector>>16);
1370 data[7+3] =
LSB_of(sector>>16);
1371 data[7+4] =
MSB_of(sector&0xFFFF);
1372 data[7+5] =
LSB_of(sector&0xFFFF);
1378 result = usbat_hp8200e_rw_block_test(us,
USBAT_ATA,
1379 registers, data, 19,
1404 static int usbat_select_and_test_registers(
struct us_data *us)
1407 unsigned char *status = us->
iobuf;
1410 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
1454 static int init_usbat(
struct us_data *us,
int devicetype)
1460 unsigned char *status = us->
iobuf;
1464 US_DEBUGP(
"init_usbat: Gah! Can't allocate storage for usbat info struct!\n");
1470 rc = usbat_write_user_io(us,
1480 rc = usbat_read_user_io(us, status);
1486 rc = usbat_read_user_io(us, status);
1490 rc = usbat_read_user_io(us, status);
1496 rc = usbat_select_and_test_registers(us);
1502 rc = usbat_read_user_io(us, status);
1509 rc = usbat_device_enable_cdt(us);
1515 rc = usbat_read_user_io(us, status);
1523 rc = usbat_read_user_io(us, status);
1529 rc = usbat_select_and_test_registers(us);
1536 if (usbat_set_transport(us, info, devicetype))
1546 0x00, 0x88, 0x08, subcountH, subcountL);
1558 static int usbat_hp8200e_transport(
struct scsi_cmnd *srb,
struct us_data *us)
1561 unsigned char *status = us->
iobuf;
1562 unsigned char registers[32];
1563 unsigned char data[32];
1567 len = scsi_bufflen(srb);
1585 data[4] = (len>>8)&0xFF;
1589 for (i=7; i<19; i++) {
1590 registers[
i] = 0x10;
1594 result = usbat_get_status(us, status);
1603 result = usbat_hp8200e_rw_block_test(us,
USBAT_ATA,
1604 registers, data, 19,
1609 len, scsi_sg_count(srb), 10);
1613 US_DEBUGP(
"Wrote %08X bytes\n", transferred);
1621 return usbat_hp8200e_handle_read10(us, registers, data, srb);
1626 US_DEBUGP(
"Error: len = %08X... what do I do now?\n",
1631 result = usbat_multiple_write(us, registers, data, 7);
1669 len += ((
unsigned int) *status)<<8;
1675 result = usbat_read_block(us, scsi_sglist(srb), len,
1676 scsi_sg_count(srb));
1685 static int usbat_flash_transport(
struct scsi_cmnd * srb,
struct us_data *us)
1689 unsigned long block, blocks;
1691 static unsigned char inquiry_response[36] = {
1692 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1696 US_DEBUGP(
"usbat_flash_transport: INQUIRY. Returning bogus response.\n");
1697 memcpy(ptr, inquiry_response,
sizeof(inquiry_response));
1703 rc = usbat_flash_check_media(us, info);
1707 rc = usbat_flash_get_sector_count(us, info);
1712 info->
ssize = 0x200;
1713 US_DEBUGP(
"usbat_flash_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
1729 US_DEBUGP(
"usbat_flash_transport: Gah! MODE_SELECT_10.\n");
1734 block = ((
u32)(srb->
cmnd[2]) << 24) | ((
u32)(srb->
cmnd[3]) << 16) |
1739 US_DEBUGP(
"usbat_flash_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
1740 return usbat_flash_read_data(us, info, block, blocks);
1747 block = ((
u32)(srb->
cmnd[2]) << 24) | ((
u32)(srb->
cmnd[3]) << 16) |
1750 blocks = ((
u32)(srb->
cmnd[6]) << 24) | ((
u32)(srb->
cmnd[7]) << 16) |
1753 US_DEBUGP(
"usbat_flash_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
1754 return usbat_flash_read_data(us, info, block, blocks);
1758 block = ((
u32)(srb->
cmnd[2]) << 24) | ((
u32)(srb->
cmnd[3]) << 16) |
1763 US_DEBUGP(
"usbat_flash_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
1764 return usbat_flash_write_data(us, info, block, blocks);
1771 block = ((
u32)(srb->
cmnd[2]) << 24) | ((
u32)(srb->
cmnd[3]) << 16) |
1774 blocks = ((
u32)(srb->
cmnd[6]) << 24) | ((
u32)(srb->
cmnd[7]) << 16) |
1777 US_DEBUGP(
"usbat_flash_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
1778 return usbat_flash_write_data(us, info, block, blocks);
1783 US_DEBUGP(
"usbat_flash_transport: TEST_UNIT_READY.\n");
1785 rc = usbat_flash_check_media(us, info);
1789 return usbat_check_status(us);
1793 US_DEBUGP(
"usbat_flash_transport: REQUEST_SENSE.\n");
1814 US_DEBUGP(
"usbat_flash_transport: Gah! Unknown command: %d (0x%x)\n",
1822 static int init_usbat_cd(
struct us_data *us)
1827 static int init_usbat_flash(
struct us_data *us)
1839 (
id - usbat_usb_ids) + usbat_unusual_dev_list);
1855 static struct usb_driver usbat_driver = {
1856 .name =
"ums-usbat",
1857 .probe = usbat_probe,
1864 .id_table = usbat_usb_ids,