// SPDX-License-Identifier: GPL-2.0+ /* * Driver for SanDisk SDDR-09 SmartMedia reader * * (c) 2000, 2001 Robert Baruch (autophile@starband.net) * (c) 2002 Andries Brouwer (aeb@cwi.nl) * Developed with the assistance of: * (c) 2002 Alan Stern <stern@rowland.org> * * The SanDisk SDDR-09 SmartMedia reader uses the Shuttle EUSB-01 chip. * This chip is a programmable USB controller. In the SDDR-09, it has * been programmed to obey a certain limited set of SCSI commands. * This driver translates the "real" SCSI commands to the SDDR-09 SCSI * commands.
*/
/* * Known vendor commands: 12 bytes, first byte is opcode * * E7: read scatter gather * E8: read * E9: write * EA: erase * EB: reset * EC: read status * ED: read ID * EE: write CIS (?) * EF: compute checksum (?)
*/
/* * First some stuff that does not belong here: * data on SmartMedia and other cards, completely * unrelated to this driver. * Similar stuff occurs in <linux/mtd/nand_ids.h>.
*/
struct nand_flash_dev { int model_id; int chipshift; /* 1<<cs bytes total capacity */ char pageshift; /* 1<<ps bytes in a page */ char blockshift; /* 1<<bs pages in an erase block */ char zoneshift; /* 1<<zs blocks in a zone */ /* # of logical blocks is 125/128 of this */ char pageadrlen; /* length of an address in bytes - 1 */
};
staticinlinechar *nand_flash_manufacturer(int manuf_id) { switch(manuf_id) { case NAND_MFR_AMD: return"AMD"; case NAND_MFR_NATSEMI: return"NATSEMI"; case NAND_MFR_TOSHIBA: return"Toshiba"; case NAND_MFR_SAMSUNG: return"Samsung"; default: return"unknown";
}
}
/* * It looks like it is unnecessary to attach manufacturer to the * remaining data: SSFDC prescribes manufacturer-independent id codes. * * 256 MB NAND flash has a 5-byte ID with 2nd byte 0xaa, 0xba, 0xca or 0xda.
*/
struct sddr09_card_info { unsignedlong capacity; /* Size of card in bytes */ int pagesize; /* Size of page in bytes */ int pageshift; /* log2 of pagesize */ int blocksize; /* Size of block in pages */ int blockshift; /* log2 of blocksize */ int blockmask; /* 2^blockshift - 1 */ int *lba_to_pba; /* logical to physical map */ int *pba_to_lba; /* physical to logical map */ int lbact; /* number of available pages */ int flags; #define SDDR09_WP 1 /* write protected */
};
/* * On my 16MB card, control blocks have size 64 (16 real control bytes, * and 48 junk bytes). In reality of course the card uses 16 control bytes, * so the reader makes up the remaining 48. Don't know whether these numbers * depend on the card. For now a constant.
*/ #define CONTROL_SHIFT 6
/* * On my Combo CF/SM reader, the SM reader has LUN 1. * (and things fail with LUN 0). * It seems LUN is irrelevant for others.
*/ #define LUN 1 #define LUNBITS (LUN << 5)
/* * LBA and PBA are unsigned ints. Special values.
*/ #define UNDEF 0xffffffff #define SPARE 0xfffffffe #define UNUSABLE 0xfffffffd
/* * Read Command: 12 bytes. * byte 0: opcode: E8 * byte 1: last two bits: 00: read data, 01: read blockwise control, * 10: read both, 11: read pagewise control. * It turns out we need values 20, 21, 22, 23 here (LUN 1). * bytes 2-5: address (interpretation depends on byte 1, see below) * bytes 10-11: count (idem) * * A page has 512 data bytes and 64 control bytes (16 control and 48 junk). * A read data command gets data in 512-byte pages. * A read control command gets control in 64-byte chunks. * A read both command gets data+control in 576-byte chunks. * * Blocks are groups of 32 pages, and read blockwise control jumps to the * next block, while read pagewise control jumps to the next page after * reading a group of 64 control bytes. * [Here 512 = 1<<pageshift, 32 = 1<<blockshift, 64 is constant?] * * (1 MB and 2 MB cards are a bit different, but I have only a 16 MB card.)
*/
staticint
sddr09_readX(struct us_data *us, int x, unsignedlong fromaddress, int nr_of_pages, int bulklen, unsignedchar *buf, int use_sg) {
result = sddr09_send_scsi_command(us, command, 12);
if (result) {
usb_stor_dbg(us, "Result for send_control in sddr09_read2%d %d\n",
x, result); return result;
}
result = usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe,
buf, bulklen, use_sg, NULL);
if (result != USB_STOR_XFER_GOOD) {
usb_stor_dbg(us, "Result for bulk_transfer in sddr09_read2%d %d\n",
x, result); return -EIO;
} return 0;
}
/* * Read Data * * fromaddress counts data shorts: * increasing it by 256 shifts the bytestream by 512 bytes; * the last 8 bits are ignored. * * nr_of_pages counts pages of size (1 << pageshift).
*/ staticint
sddr09_read20(struct us_data *us, unsignedlong fromaddress, int nr_of_pages, int pageshift, unsignedchar *buf, int use_sg) { int bulklen = nr_of_pages << pageshift;
/* The last 8 bits of fromaddress are ignored. */ return sddr09_readX(us, 0, fromaddress, nr_of_pages, bulklen,
buf, use_sg);
}
/* * Read Blockwise Control * * fromaddress gives the starting position (as in read data; * the last 8 bits are ignored); increasing it by 32*256 shifts * the output stream by 64 bytes. * * count counts control groups of size (1 << controlshift). * For me, controlshift = 6. Is this constant? * * After getting one control group, jump to the next block * (fromaddress += 8192).
*/ staticint
sddr09_read21(struct us_data *us, unsignedlong fromaddress, int count, int controlshift, unsignedchar *buf, int use_sg) {
/* * Read both Data and Control * * fromaddress counts data shorts, ignoring control: * increasing it by 256 shifts the bytestream by 576 = 512+64 bytes; * the last 8 bits are ignored. * * nr_of_pages counts pages of size (1 << pageshift) + (1 << controlshift).
*/ staticint
sddr09_read22(struct us_data *us, unsignedlong fromaddress, int nr_of_pages, int pageshift, unsignedchar *buf, int use_sg) {
#if 0 /* * Read Pagewise Control * * fromaddress gives the starting position (as in read data; * the last 8 bits are ignored); increasing it by 256 shifts * the output stream by 64 bytes. * * count counts control groups of size (1 << controlshift). * For me, controlshift = 6. Is this constant? * * After getting one control group, jump to the next page * (fromaddress += 256).
*/ staticint
sddr09_read23(struct us_data *us, unsignedlong fromaddress, int count, int controlshift, unsignedchar *buf, int use_sg) {
result = sddr09_send_scsi_command(us, command, 12);
if (result)
usb_stor_dbg(us, "Result for send_control in sddr09_erase %d\n",
result);
return result;
}
/* * Write CIS Command: 12 bytes. * byte 0: opcode: EE * bytes 2-5: write address in shorts * bytes 10-11: sector count * * This writes at the indicated address. Don't know how it differs * from E9. Maybe it does not erase? However, it will also write to * the CIS. * * When two such commands on the same page follow each other directly, * the second one is not done.
*/
/* * Write Command: 12 bytes. * byte 0: opcode: E9 * bytes 2-5: write address (big-endian, counting shorts, sector aligned). * bytes 6-9: erase address (big-endian, counting shorts, sector aligned). * bytes 10-11: sector count (big-endian, in 512-byte sectors). * * If write address equals erase address, the erase is done first, * otherwise the write is done first. When erase address equals zero * no erase is done?
*/ staticint
sddr09_writeX(struct us_data *us, unsignedlong Waddress, unsignedlong Eaddress, int nr_of_pages, int bulklen, unsignedchar *buf, int use_sg) {
result = sddr09_send_scsi_command(us, command, 12);
if (result) {
usb_stor_dbg(us, "Result for send_control in sddr09_writeX %d\n",
result); return result;
}
result = usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe,
buf, bulklen, use_sg, NULL);
if (result != USB_STOR_XFER_GOOD) {
usb_stor_dbg(us, "Result for bulk_transfer in sddr09_writeX %d\n",
result); return -EIO;
} return 0;
}
/* erase address, write same address */ staticint
sddr09_write_inplace(struct us_data *us, unsignedlong address, int nr_of_pages, int pageshift, unsignedchar *buf, int use_sg) { int bulklen = (nr_of_pages << pageshift) + (nr_of_pages << CONTROL_SHIFT); return sddr09_writeX(us, address, address, nr_of_pages, bulklen,
buf, use_sg);
}
#if 0 /* * Read Scatter Gather Command: 3+4n bytes. * byte 0: opcode E7 * byte 2: n * bytes 4i-1,4i,4i+1: page address * byte 4i+2: page count * (i=1..n) * * This reads several pages from the card to a single memory buffer. * The last two bits of byte 1 have the same meaning as for E8.
*/ staticint
sddr09_read_sg_test_only(struct us_data *us) { unsignedchar *command = us->iobuf; int result, bulklen, nsg, ct; unsignedchar *buf; unsignedlong address;
result = sddr09_send_scsi_command(us, command, 4*nsg+3);
if (result) {
usb_stor_dbg(us, "Result for send_control in sddr09_read_sg %d\n",
result); return result;
}
buf = kmalloc(bulklen, GFP_NOIO); if (!buf) return -ENOMEM;
result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
buf, bulklen, NULL);
kfree(buf); if (result != USB_STOR_XFER_GOOD) {
usb_stor_dbg(us, "Result for bulk_transfer in sddr09_read_sg %d\n",
result); return -EIO;
}
return 0;
} #endif
/* * Read Status Command: 12 bytes. * byte 0: opcode: EC * * Returns 64 bytes, all zero except for the first. * bit 0: 1: Error * bit 5: 1: Suspended * bit 6: 1: Ready * bit 7: 1: Not write-protected
*/
// Figure out the initial LBA and page
lba = address >> info->blockshift;
page = (address & info->blockmask);
maxlba = info->capacity >> (info->pageshift + info->blockshift); if (lba >= maxlba) return -EIO;
// Since we only read in one block at a time, we have to create // a bounce buffer and move the data a piece at a time between the // bounce buffer and the actual transfer buffer.
len = min_t(unsignedint, sectors, info->blocksize) * info->pagesize;
buffer = kmalloc(len, GFP_NOIO); if (!buffer) return -ENOMEM;
// This could be made much more efficient by checking for // contiguous LBA's. Another exercise left to the student.
result = 0;
offset = 0;
sg = NULL;
while (sectors > 0) {
/* Find number of pages we can read in this block */
pages = min(sectors, info->blocksize - page);
len = pages << info->pageshift;
/* Not overflowing capacity? */ if (lba >= maxlba) {
usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n",
lba, maxlba);
result = -EIO; break;
}
/* Find where this lba lives on disk */
pba = info->lba_to_pba[lba];
if (pba == UNDEF) { /* this lba was never written */
/* * This is not really an error. It just means * that the block has never been written. * Instead of returning an error * it is better to return all zero data.
*/
if (pba == UNDEF) {
pba = sddr09_find_unused_pba(info, lba); if (!pba) {
printk(KERN_WARNING "sddr09_write_lba: Out of unused blocks\n"); return -ENOSPC;
}
info->pba_to_lba[pba] = lba;
info->lba_to_pba[lba] = pba;
}
if (pba == 1) { /* * Maybe it is impossible to write to PBA 1. * Fake success, but don't do anything.
*/
printk(KERN_WARNING "sddr09: avoid writing to pba 1\n"); return 0;
}
/* * Since we don't write the user data directly to the device, * we have to create a bounce buffer and move the data a piece * at a time between the bounce buffer and the actual transfer buffer.
*/
len = min_t(unsignedint, sectors, info->blocksize) * info->pagesize;
buffer = kmalloc(len, GFP_NOIO); if (!buffer) {
kfree(blockbuffer); return -ENOMEM;
}
result = 0;
offset = 0;
sg = NULL;
while (sectors > 0) {
/* Write as many sectors as possible in this block */
/* * Read Device ID Command: 12 bytes. * byte 0: opcode: ED * * Returns 2 bytes: Manufacturer ID and Device ID. * On more recent cards 3 bytes: the third byte is an option code A5 * signifying that the secret command to read an 128-bit ID is available. * On still more recent cards 4 bytes: the fourth byte C0 means that * a second read ID cmd is available.
*/ staticint
sddr09_read_deviceID(struct us_data *us, unsignedchar *deviceID) { unsignedchar *command = us->iobuf; unsignedchar *content = us->iobuf; int result, i;
if (result) {
usb_stor_dbg(us, "Result of read_deviceID is %d\n", result);
printk(KERN_WARNING "sddr09: could not read card info\n"); return NULL;
}
sprintf(blurbtxt, "sddr09: Found Flash card, ID = %4ph", deviceID);
/* Byte 0 is the manufacturer */
sprintf(blurbtxt + strlen(blurbtxt), ": Manuf. %s",
nand_flash_manufacturer(deviceID[0]));
/* Byte 1 is the device type */
cardinfo = nand_find_id(deviceID[1]); if (cardinfo) { /* * MB or MiB? It is neither. A 16 MB card has * 17301504 raw bytes, of which 16384000 are * usable for user data.
*/
sprintf(blurbtxt + strlen(blurbtxt), ", %d MB", 1<<(cardinfo->chipshift - 20));
} else {
sprintf(blurbtxt + strlen(blurbtxt), ", type unrecognized");
}
/* Byte 2 is code to signal availability of 128-bit ID */ if (deviceID[2] == 0xa5) {
sprintf(blurbtxt + strlen(blurbtxt), ", 128-bit ID");
}
/* Byte 3 announces the availability of another read ID command */ if (deviceID[3] == 0xc0) {
sprintf(blurbtxt + strlen(blurbtxt), ", extra cmd");
}
if (flags & SDDR09_WP)
sprintf(blurbtxt + strlen(blurbtxt), ", WP");
printk(KERN_WARNING "%s\n", blurbtxt);
return cardinfo;
}
staticint
sddr09_read_map(struct us_data *us) {
struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra; int numblocks, alloc_len, alloc_blocks; int i, j, result; unsignedchar *buffer, *buffer_end, *ptr; unsignedint lba, lbact;
if (!info->capacity) return -1;
/* * size of a block is 1 << (blockshift + pageshift) bytes * divide into the total capacity to get the number of blocks
*/
/* * read 64 bytes for every block (actually 1 << CONTROL_SHIFT) * but only use a 64 KB buffer * buffer size used must be a multiple of (1 << CONTROL_SHIFT)
*/ #define SDDR09_READ_MAP_BUFSZ 65536
if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) {
printk(KERN_WARNING "sddr09_read_map: out of memory\n");
result = -1; goto done;
}
for (i = 0; i < numblocks; i++)
info->lba_to_pba[i] = info->pba_to_lba[i] = UNDEF;
/* * Define lba-pba translation table
*/
ptr = buffer_end; for (i = 0; i < numblocks; i++) {
ptr += (1 << CONTROL_SHIFT); if (ptr >= buffer_end) { unsignedlong address;
address = i << (info->pageshift + info->blockshift);
result = sddr09_read_control(
us, address>>1,
min(alloc_blocks, numblocks - i),
buffer, 0); if (result) {
result = -1; goto done;
}
ptr = buffer;
}
if (i == 0 || i == 1) {
info->pba_to_lba[i] = UNUSABLE; continue;
}
/* special PBAs have control field 0^16 */ for (j = 0; j < 16; j++) if (ptr[j] != 0) goto nonz;
info->pba_to_lba[i] = UNUSABLE;
printk(KERN_WARNING "sddr09: PBA %d has no logical mapping\n",
i); continue;
nonz: /* unwritten PBAs have control field FF^16 */ for (j = 0; j < 16; j++) if (ptr[j] != 0xff) goto nonff; continue;
nonff: /* normal PBAs start with six FFs */ if (j < 6) {
printk(KERN_WARNING "sddr09: PBA %d has no logical mapping: " "reserved area = %02X%02X%02X%02X " "data status %02X block status %02X\n",
i, ptr[0], ptr[1], ptr[2], ptr[3],
ptr[4], ptr[5]);
info->pba_to_lba[i] = UNUSABLE; continue;
}
if ((ptr[6] >> 4) != 0x01) {
printk(KERN_WARNING "sddr09: PBA %d has invalid address field " "%02X%02X/%02X%02X\n",
i, ptr[6], ptr[7], ptr[11], ptr[12]);
info->pba_to_lba[i] = UNUSABLE; continue;
}
/* check even parity */ if (parity[ptr[6] ^ ptr[7]]) {
printk(KERN_WARNING "sddr09: Bad parity in LBA for block %d" " (%02X %02X)\n", i, ptr[6], ptr[7]);
info->pba_to_lba[i] = UNUSABLE; continue;
}
/* * Every 1024 physical blocks ("zone"), the LBA numbers * go back to zero, but are within a higher block of LBA's. * Also, there is a maximum of 1000 LBA's per zone. * In other words, in PBA 1024-2047 you will find LBA 0-999 * which are really LBA 1000-1999. This allows for 24 bad * or special physical blocks per zone.
*/
if (lba >= 1000) {
printk(KERN_WARNING "sddr09: Bad low LBA %d for block %d\n",
lba, i); goto possibly_erase;
}
lba += 1000*(i/0x400);
if (info->lba_to_pba[lba] != UNDEF) {
printk(KERN_WARNING "sddr09: LBA %d seen for PBA %d and %d\n",
lba, info->lba_to_pba[lba], i); goto possibly_erase;
}
/* * Approximate capacity. This is not entirely correct yet, * since a zone with less than 1000 usable pages leads to * missing LBAs. Especially if it is the last zone, some * LBAs can be past capacity.
*/
lbact = 0; for (i = 0; i < numblocks; i += 1024) { int ct = 0;
staticint
sddr09_common_init(struct us_data *us) { int result;
/* set the configuration -- STALL is an acceptable response here */ if (us->pusb_dev->actconfig->desc.bConfigurationValue != 1) {
usb_stor_dbg(us, "active config #%d != 1 ??\n",
us->pusb_dev->actconfig->desc.bConfigurationValue); return -EINVAL;
}
result = usb_reset_configuration(us->pusb_dev);
usb_stor_dbg(us, "Result of usb_reset_configuration is %d\n", result); if (result == -EPIPE) {
usb_stor_dbg(us, "-- stall on control interface\n");
} elseif (result != 0) { /* it's not a stall, but another error -- time to bail */
usb_stor_dbg(us, "-- Unknown error. Rejecting device\n"); return -EINVAL;
}
/* * This is needed at a very early stage. If this is not listed in the * unusual devices list but called from here then LUN 0 of the combo reader * is not recognized. But I do not know what precisely these calls do.
*/ staticint
usb_stor_sddr09_dpcm_init(struct us_data *us) { int result; unsignedchar *data = us->iobuf;
result = sddr09_common_init(us); if (result) return result;
result = sddr09_send_command(us, 0x01, USB_DIR_IN, data, 2); if (result) {
usb_stor_dbg(us, "send_command fails\n"); return result;
}
usb_stor_dbg(us, "%02X %02X\n", data[0], data[1]); // get 07 02
result = sddr09_send_command(us, 0x08, USB_DIR_IN, data, 2); if (result) {
usb_stor_dbg(us, "2nd send_command fails\n"); return result;
}
usb_stor_dbg(us, "%02X %02X\n", data[0], data[1]); // get 07 00
result = sddr09_request_sense(us, data, 18); if (result == 0 && data[2] != 0) { int j; for (j=0; j<18; j++)
printk(" %02X", data[j]);
printk("\n"); // get 70 00 00 00 00 00 00 * 00 00 00 00 00 00 // 70: current command // sense key 0, sense code 0, extd sense code 0 // additional transfer length * = sizeof(data) - 7 // Or: 70 00 06 00 00 00 00 0b 00 00 00 00 28 00 00 00 00 00 // sense key 06, sense code 28: unit attention, // not ready to ready transition
}
// test unit ready
return 0; /* not result */
}
/* * Transport for the Microtech DPCM-USB
*/ staticint dpcm_transport(struct scsi_cmnd *srb, struct us_data *us)
{ int ret;
if (srb->cmnd[0] == MODE_SENSE_10) { int modepage = (srb->cmnd[2] & 0x3F);
/* * They ask for the Read/Write error recovery page, * or for all pages.
*/ /* %% We should check DBD %% */ if (modepage == 0x01 || modepage == 0x3F) {
usb_stor_dbg(us, "Dummy up request for mode page 0x%x\n",
modepage);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.