43 #include <linux/errno.h>
44 #include <linux/module.h>
45 #include <linux/slab.h>
47 #include <scsi/scsi.h>
60 static int usb_stor_sddr09_dpcm_init(
struct us_data *us);
62 static int usb_stor_sddr09_init(
struct us_data *us);
68 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
69 vendorName, productName, useProtocol, useTransport, \
70 initFunction, flags) \
71 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
72 .driver_info = (flags) }
85 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
86 vendor_name, product_name, use_protocol, use_transport, \
87 init_function, Flags) \
89 .vendorName = vendor_name, \
90 .productName = product_name, \
91 .useProtocol = use_protocol, \
92 .useTransport = use_transport, \
93 .initFunction = init_function, \
104 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
105 #define LSB_of(s) ((s)&0xFF)
106 #define MSB_of(s) ((s)>>8)
130 #define NAND_MFR_AMD 0x01
131 #define NAND_MFR_NATSEMI 0x8f
132 #define NAND_MFR_TOSHIBA 0x98
133 #define NAND_MFR_SAMSUNG 0xec
135 static inline char *nand_flash_manufacturer(
int manuf_id) {
159 { 0x6e, 20, 8, 4, 8, 2},
160 { 0xe8, 20, 8, 4, 8, 2},
161 { 0xec, 20, 8, 4, 8, 2},
162 { 0x64, 21, 8, 4, 9, 2},
163 { 0xea, 21, 8, 4, 9, 2},
164 { 0x6b, 22, 9, 4, 9, 2},
165 { 0xe3, 22, 9, 4, 9, 2},
166 { 0xe5, 22, 9, 4, 9, 2},
167 { 0xe6, 23, 9, 4, 10, 2},
168 { 0x73, 24, 9, 5, 10, 2},
169 { 0x75, 25, 9, 5, 10, 2},
170 { 0x76, 26, 9, 5, 10, 3},
171 { 0x79, 27, 9, 5, 10, 3},
174 { 0x5d, 21, 9, 4, 8, 2},
175 { 0xd5, 22, 9, 4, 9, 2},
176 { 0xd6, 23, 9, 4, 10, 2},
177 { 0x57, 24, 9, 4, 11, 2},
178 { 0x58, 25, 9, 4, 12, 2},
183 nand_find_id(
unsigned char id) {
186 for (i = 0; i <
ARRAY_SIZE(nand_flash_ids); i++)
187 if (nand_flash_ids[i].
model_id ==
id)
188 return &(nand_flash_ids[
i]);
195 static unsigned char parity[256];
196 static unsigned char ecc2[256];
198 static void nand_init_ecc(
void) {
202 for (i = 1; i < 256; i++)
203 parity[i] = (parity[i&(i-1)] ^ 1);
205 for (i = 0; i < 256; i++) {
207 for (j = 0; j < 8; j++) {
217 ecc2[
i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
222 static void nand_compute_ecc(
unsigned char *
data,
unsigned char *
ecc) {
224 unsigned char par,
bit,
bits[8];
227 for (j = 0; j < 8; j++)
231 for (i = 0; i < 256; i++) {
233 bit = parity[data[
i]];
234 for (j = 0; j < 8; j++)
235 if ((i & (1<<j)) == 0)
240 a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
241 ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
243 a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
244 ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
249 static int nand_compare_ecc(
unsigned char *data,
unsigned char *ecc) {
250 return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
253 static void nand_store_ecc(
unsigned char *data,
unsigned char *ecc) {
281 #define CONTROL_SHIFT 6
289 #define LUNBITS (LUN << 5)
294 #define UNDEF 0xffffffff
295 #define SPARE 0xfffffffe
296 #define UNUSABLE 0xfffffffd
298 static const int erase_bad_lba_entries = 0;
303 sddr09_send_command(
struct us_data *us,
306 unsigned char *xfer_data,
309 unsigned char requesttype = (0x41 |
direction);
320 0, 0, xfer_data, xfer_len);
324 default:
return -
EIO;
329 sddr09_send_scsi_command(
struct us_data *us,
331 unsigned int command_len) {
332 return sddr09_send_command(us, 0,
USB_DIR_OUT, command, command_len);
341 sddr09_test_unit_ready(
struct us_data *us) {
348 result = sddr09_send_scsi_command(us, command, 6);
350 US_DEBUGP(
"sddr09_test_unit_ready returns %d\n", result);
362 sddr09_request_sense(
struct us_data *us,
unsigned char *sensebuf,
int buflen) {
371 result = sddr09_send_scsi_command(us, command, 12);
376 sensebuf, buflen,
NULL);
403 sddr09_readX(
struct us_data *us,
int x,
unsigned long fromaddress,
404 int nr_of_pages,
int bulklen,
unsigned char *
buf,
407 unsigned char *command = us->
iobuf;
412 command[2] =
MSB_of(fromaddress>>16);
413 command[3] =
LSB_of(fromaddress>>16);
414 command[4] =
MSB_of(fromaddress & 0xFFFF);
415 command[5] =
LSB_of(fromaddress & 0xFFFF);
420 command[10] =
MSB_of(nr_of_pages);
421 command[11] =
LSB_of(nr_of_pages);
423 result = sddr09_send_scsi_command(us, command, 12);
426 US_DEBUGP(
"Result for send_control in sddr09_read2%d %d\n",
432 buf, bulklen, use_sg,
NULL);
435 US_DEBUGP(
"Result for bulk_transfer in sddr09_read2%d %d\n",
452 sddr09_read20(
struct us_data *us,
unsigned long fromaddress,
453 int nr_of_pages,
int pageshift,
unsigned char *buf,
int use_sg) {
454 int bulklen = nr_of_pages << pageshift;
457 return sddr09_readX(us, 0, fromaddress, nr_of_pages, bulklen,
475 sddr09_read21(
struct us_data *us,
unsigned long fromaddress,
476 int count,
int controlshift,
unsigned char *buf,
int use_sg) {
478 int bulklen = (count << controlshift);
479 return sddr09_readX(us, 1, fromaddress, count, bulklen,
493 sddr09_read22(
struct us_data *us,
unsigned long fromaddress,
494 int nr_of_pages,
int pageshift,
unsigned char *buf,
int use_sg) {
496 int bulklen = (nr_of_pages << pageshift) + (nr_of_pages <<
CONTROL_SHIFT);
497 US_DEBUGP(
"sddr09_read22: reading %d pages, %d bytes\n",
498 nr_of_pages, bulklen);
499 return sddr09_readX(us, 2, fromaddress, nr_of_pages, bulklen,
518 sddr09_read23(
struct us_data *us,
unsigned long fromaddress,
519 int count,
int controlshift,
unsigned char *buf,
int use_sg) {
521 int bulklen = (count << controlshift);
522 return sddr09_readX(us, 3, fromaddress, count, bulklen,
537 sddr09_erase(
struct us_data *us,
unsigned long Eaddress) {
538 unsigned char *command = us->
iobuf;
541 US_DEBUGP(
"sddr09_erase: erase address %lu\n", Eaddress);
546 command[6] =
MSB_of(Eaddress>>16);
547 command[7] =
LSB_of(Eaddress>>16);
548 command[8] =
MSB_of(Eaddress & 0xFFFF);
549 command[9] =
LSB_of(Eaddress & 0xFFFF);
551 result = sddr09_send_scsi_command(us, command, 12);
554 US_DEBUGP(
"Result for send_control in sddr09_erase %d\n",
586 sddr09_writeX(
struct us_data *us,
587 unsigned long Waddress,
unsigned long Eaddress,
588 int nr_of_pages,
int bulklen,
unsigned char *buf,
int use_sg) {
590 unsigned char *command = us->
iobuf;
596 command[2] =
MSB_of(Waddress>>16);
597 command[3] =
LSB_of(Waddress>>16);
598 command[4] =
MSB_of(Waddress & 0xFFFF);
599 command[5] =
LSB_of(Waddress & 0xFFFF);
601 command[6] =
MSB_of(Eaddress>>16);
602 command[7] =
LSB_of(Eaddress>>16);
603 command[8] =
MSB_of(Eaddress & 0xFFFF);
604 command[9] =
LSB_of(Eaddress & 0xFFFF);
606 command[10] =
MSB_of(nr_of_pages);
607 command[11] =
LSB_of(nr_of_pages);
609 result = sddr09_send_scsi_command(us, command, 12);
612 US_DEBUGP(
"Result for send_control in sddr09_writeX %d\n",
618 buf, bulklen, use_sg,
NULL);
621 US_DEBUGP(
"Result for bulk_transfer in sddr09_writeX %d\n",
631 int nr_of_pages,
int pageshift,
unsigned char *buf,
633 int bulklen = (nr_of_pages << pageshift) + (nr_of_pages <<
CONTROL_SHIFT);
634 return sddr09_writeX(us, address, address, nr_of_pages, bulklen,
651 sddr09_read_sg_test_only(
struct us_data *us) {
652 unsigned char *command = us->
iobuf;
661 address = 040000; ct = 1;
663 bulklen += (ct << 9);
664 command[4*nsg+2] =
ct;
665 command[4*nsg+1] = ((address >> 9) & 0xFF);
666 command[4*nsg+0] = ((address >> 17) & 0xFF);
667 command[4*nsg-1] = ((address >> 25) & 0xFF);
669 address = 0340000; ct = 1;
671 bulklen += (ct << 9);
672 command[4*nsg+2] =
ct;
673 command[4*nsg+1] = ((address >> 9) & 0xFF);
674 command[4*nsg+0] = ((address >> 17) & 0xFF);
675 command[4*nsg-1] = ((address >> 25) & 0xFF);
677 address = 01000000; ct = 2;
679 bulklen += (ct << 9);
680 command[4*nsg+2] =
ct;
681 command[4*nsg+1] = ((address >> 9) & 0xFF);
682 command[4*nsg+0] = ((address >> 17) & 0xFF);
683 command[4*nsg-1] = ((address >> 25) & 0xFF);
687 result = sddr09_send_scsi_command(us, command, 4*nsg+3);
690 US_DEBUGP(
"Result for send_control in sddr09_read_sg %d\n",
703 US_DEBUGP(
"Result for bulk_transfer in sddr09_read_sg %d\n",
724 sddr09_read_status(
struct us_data *us,
unsigned char *
status) {
726 unsigned char *command = us->
iobuf;
727 unsigned char *data = us->
iobuf;
736 result = sddr09_send_scsi_command(us, command, 12);
747 sddr09_read_data(
struct us_data *us,
748 unsigned long address,
753 unsigned int lba, maxlba, pba;
784 while (sectors > 0) {
792 US_DEBUGP(
"Error: Requested lba %u exceeds "
793 "maximum %u\n", lba, maxlba);
803 US_DEBUGP(
"Read %d zero pages (LBA %d) page %d\n",
815 " (LBA %d) page %d\n",
816 pages, pba, lba, page);
818 address = ((pba << info->
blockshift) + page) <<
821 result = sddr09_read20(us, address>>1,
842 static unsigned int lastpba = 1;
843 int zonestart,
end,
i;
845 zonestart = (lba/1000) << 10;
851 for (i = lastpba+1; i <
end; i++) {
857 for (i = 0; i <= lastpba; i++) {
867 sddr09_write_lba(
struct us_data *us,
unsigned int lba,
868 unsigned int page,
unsigned int pages,
869 unsigned char *
ptr,
unsigned char *blockbuffer) {
872 unsigned long address;
873 unsigned int pba, lbap;
874 unsigned int pagelen;
875 unsigned char *bptr, *cptr, *xptr;
876 unsigned char ecc[3];
877 int i, result, isnew;
879 lbap = ((lba % 1000) << 1) | 0x1000;
886 pba = sddr09_find_unused_pba(info, lba);
889 "sddr09_write_lba: Out of unused blocks\n");
908 result = sddr09_read22(us, address>>1, info->
blocksize,
915 bptr = blockbuffer + i*pagelen;
917 nand_compute_ecc(bptr, ecc);
918 if (!nand_compare_ecc(cptr+13, ecc)) {
919 US_DEBUGP(
"Warning: bad ecc in page %d- of pba %d\n",
921 nand_store_ecc(cptr+13, ecc);
923 nand_compute_ecc(bptr+(info->
pagesize / 2), ecc);
924 if (!nand_compare_ecc(cptr+8, ecc)) {
925 US_DEBUGP(
"Warning: bad ecc in page %d+ of pba %d\n",
927 nand_store_ecc(cptr+8, ecc);
929 cptr[6] = cptr[11] =
MSB_of(lbap);
930 cptr[7] = cptr[12] =
LSB_of(lbap);
935 for (i = page; i < page+
pages; i++) {
936 bptr = blockbuffer + i*pagelen;
940 nand_compute_ecc(bptr, ecc);
941 nand_store_ecc(cptr+13, ecc);
942 nand_compute_ecc(bptr+(info->
pagesize / 2), ecc);
943 nand_store_ecc(cptr+8, ecc);
946 US_DEBUGP(
"Rewrite PBA %d (LBA %d)\n", pba, lba);
948 result = sddr09_write_inplace(us, address>>1, info->
blocksize,
951 US_DEBUGP(
"sddr09_write_inplace returns %d\n", result);
955 unsigned char status = 0;
956 int result2 = sddr09_read_status(us, &status);
958 US_DEBUGP(
"sddr09_write_inplace: cannot read status\n");
959 else if (status != 0xc0)
960 US_DEBUGP(
"sddr09_write_inplace: status after write: 0x%x\n",
967 int result2 = sddr09_test_unit_ready(us);
975 sddr09_write_data(
struct us_data *us,
976 unsigned long address,
977 unsigned int sectors) {
980 unsigned int lba, maxlba, page, pages;
981 unsigned int pagelen, blocklen;
982 unsigned char *blockbuffer;
1025 while (sectors > 0) {
1033 if (lba >= maxlba) {
1034 US_DEBUGP(
"Error: Requested lba %u exceeds "
1035 "maximum %u\n", lba, maxlba);
1044 result = sddr09_write_lba(us, lba, page, pages,
1061 sddr09_read_control(
struct us_data *us,
1062 unsigned long address,
1063 unsigned int blocks,
1064 unsigned char *content,
1067 US_DEBUGP(
"Read control address %lu, blocks %d\n",
1070 return sddr09_read21(us, address, blocks,
1085 sddr09_read_deviceID(
struct us_data *us,
unsigned char *deviceID) {
1086 unsigned char *command = us->
iobuf;
1087 unsigned char *content = us->
iobuf;
1094 result = sddr09_send_scsi_command(us, command, 12);
1101 for (i = 0; i < 4; i++)
1102 deviceID[i] = content[i];
1112 result = sddr09_read_status(us, &status);
1114 US_DEBUGP(
"sddr09_get_wp: read_status fails\n");
1117 US_DEBUGP(
"sddr09_get_wp: status 0x%02X", status);
1118 if ((status & 0x80) == 0) {
1138 sddr09_reset(
struct us_data *us) {
1140 unsigned char *command = us->
iobuf;
1146 return sddr09_send_scsi_command(us, command, 12);
1151 sddr09_get_cardinfo(
struct us_data *us,
unsigned char flags) {
1153 unsigned char deviceID[4];
1159 result = sddr09_read_deviceID(us, deviceID);
1162 US_DEBUGP(
"Result of read_deviceID is %d\n", result);
1167 sprintf(blurbtxt,
"sddr09: Found Flash card, ID = %02X %02X %02X %02X",
1168 deviceID[0], deviceID[1], deviceID[2], deviceID[3]);
1173 nand_flash_manufacturer(deviceID[0]));
1176 cardinfo = nand_find_id(deviceID[1]);
1182 ", %d MB", 1<<(cardinfo->
chipshift - 20));
1185 ", type unrecognized");
1189 if (deviceID[2] == 0xa5) {
1195 if (deviceID[3] == 0xc0) {
1210 sddr09_read_map(
struct us_data *us) {
1213 int numblocks, alloc_len, alloc_blocks;
1215 unsigned char *
buffer, *buffer_end, *ptr;
1216 unsigned int lba,
lbact;
1229 #define SDDR09_READ_MAP_BUFSZ 65536
1234 if (buffer ==
NULL) {
1239 buffer_end = buffer + alloc_len;
1241 #undef SDDR09_READ_MAP_BUFSZ
1254 for (i = 0; i < numblocks; i++)
1262 for (i = 0; i < numblocks; i++) {
1264 if (ptr >= buffer_end) {
1268 result = sddr09_read_control(
1270 min(alloc_blocks, numblocks - i),
1279 if (i == 0 || i == 1) {
1285 for (j = 0; j < 16; j++)
1295 for (j = 0; j < 16; j++)
1304 "sddr09: PBA %d has no logical mapping: "
1305 "reserved area = %02X%02X%02X%02X "
1306 "data status %02X block status %02X\n",
1307 i, ptr[0], ptr[1], ptr[2], ptr[3],
1313 if ((ptr[6] >> 4) != 0x01) {
1315 "sddr09: PBA %d has invalid address field "
1316 "%02X%02X/%02X%02X\n",
1317 i, ptr[6], ptr[7], ptr[11], ptr[12]);
1323 if (parity[ptr[6] ^ ptr[7]]) {
1325 "sddr09: Bad parity in LBA for block %d"
1326 " (%02X %02X)\n", i, ptr[6], ptr[7]);
1332 lba = (lba & 0x07FF) >> 1;
1345 "sddr09: Bad low LBA %d for block %d\n",
1347 goto possibly_erase;
1350 lba += 1000*(i/0x400);
1354 "sddr09: LBA %d seen for PBA %d and %d\n",
1356 goto possibly_erase;
1364 if (erase_bad_lba_entries) {
1368 sddr09_erase(us, address>>1);
1381 for (i = 0; i < numblocks; i += 1024) {
1384 for (j = 0; j < 1024 && i+j < numblocks; j++) {
1410 sddr09_card_info_destructor(
void *
extra) {
1421 sddr09_common_init(
struct us_data *us) {
1425 if (us->
pusb_dev->actconfig->desc.bConfigurationValue != 1) {
1427 ->actconfig->desc.bConfigurationValue);
1432 US_DEBUGP(
"Result of usb_reset_configuration is %d\n", result);
1433 if (result == -
EPIPE) {
1434 US_DEBUGP(
"-- stall on control interface\n");
1435 }
else if (result != 0) {
1437 US_DEBUGP(
"-- Unknown error. Rejecting device\n");
1457 usb_stor_sddr09_dpcm_init(
struct us_data *us) {
1459 unsigned char *data = us->
iobuf;
1461 result = sddr09_common_init(us);
1465 result = sddr09_send_command(us, 0x01,
USB_DIR_IN, data, 2);
1467 US_DEBUGP(
"sddr09_init: send_command fails\n");
1471 US_DEBUGP(
"SDDR09init: %02X %02X\n", data[0], data[1]);
1474 result = sddr09_send_command(us, 0x08,
USB_DIR_IN, data, 2);
1476 US_DEBUGP(
"sddr09_init: 2nd send_command fails\n");
1480 US_DEBUGP(
"SDDR09init: %02X %02X\n", data[0], data[1]);
1483 result = sddr09_request_sense(us, data, 18);
1484 if (result == 0 && data[2] != 0) {
1486 for (j=0; j<18; j++)
1487 printk(
" %02X", data[j]);
1512 switch (srb->
device->lun) {
1531 ret = sddr09_transport(srb, us);
1536 US_DEBUGP(
"dpcm_transport: Invalid LUN %d\n",
1550 static unsigned char sensekey = 0, sensecode = 0;
1551 static unsigned char havefakesense = 0;
1553 unsigned char *ptr = us->
iobuf;
1559 static unsigned char inquiry_response[8] = {
1560 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
1564 static unsigned char mode_page_01[19] = {
1565 0x00, 0x0F, 0x00, 0x0, 0x0, 0x0, 0x00,
1567 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1578 ptr[12] = sensecode;
1580 sensekey = sensecode = havefakesense = 0;
1590 memcpy(ptr, inquiry_response, 8);
1598 sddr09_get_wp(us, info);
1600 cardinfo = sddr09_get_cardinfo(us, info->
flags);
1617 if (sddr09_read_map(us)) {
1637 int modepage = (srb->
cmnd[2] & 0x3F);
1642 if (modepage == 0x01 || modepage == 0x3F) {
1643 US_DEBUGP(
"SDDR09: Dummy up request for "
1644 "mode page 0x%x\n", modepage);
1646 memcpy(ptr, mode_page_01,
sizeof(mode_page_01));
1670 US_DEBUGP(
"READ_10: read page %d pagect %d\n",
1673 result = sddr09_read_data(us, page, pages);
1685 US_DEBUGP(
"WRITE_10: write page %d pagect %d\n",
1688 result = sddr09_write_data(us, page, pages);
1707 srb->
cmnd[1] = LUNBITS;
1710 for (i=0; i<12; i++)
1713 US_DEBUGP(
"SDDR09: Send control for command %s\n", ptr);
1715 result = sddr09_send_scsi_command(us, srb->
cmnd, 12);
1717 US_DEBUGP(
"sddr09_transport: sddr09_send_scsi_command "
1718 "returns %d\n", result);
1722 if (scsi_bufflen(srb) == 0)
1732 "sending" :
"receiving",
1748 usb_stor_sddr09_init(
struct us_data *us) {
1749 return sddr09_common_init(us);
1759 (
id - sddr09_usb_ids) + sddr09_unusual_dev_list);
1779 static struct usb_driver sddr09_driver = {
1780 .name =
"ums-sddr09",
1781 .probe = sddr09_probe,
1788 .id_table = sddr09_usb_ids,