1/* Driver for SanDisk SDDR-09 SmartMedia reader 2 * 3 * (c) 2000, 2001 Robert Baruch (autophile@starband.net) 4 * (c) 2002 Andries Brouwer (aeb@cwi.nl) 5 * Developed with the assistance of: 6 * (c) 2002 Alan Stern <stern@rowland.org> 7 * 8 * The SanDisk SDDR-09 SmartMedia reader uses the Shuttle EUSB-01 chip. 9 * This chip is a programmable USB controller. In the SDDR-09, it has 10 * been programmed to obey a certain limited set of SCSI commands. 11 * This driver translates the "real" SCSI commands to the SDDR-09 SCSI 12 * commands. 13 * 14 * This program is free software; you can redistribute it and/or modify it 15 * under the terms of the GNU General Public License as published by the 16 * Free Software Foundation; either version 2, or (at your option) any 17 * later version. 18 * 19 * This program is distributed in the hope that it will be useful, but 20 * WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 22 * General Public License for more details. 23 * 24 * You should have received a copy of the GNU General Public License along 25 * with this program; if not, write to the Free Software Foundation, Inc., 26 * 675 Mass Ave, Cambridge, MA 02139, USA. 27 */ 28 29/* 30 * Known vendor commands: 12 bytes, first byte is opcode 31 * 32 * E7: read scatter gather 33 * E8: read 34 * E9: write 35 * EA: erase 36 * EB: reset 37 * EC: read status 38 * ED: read ID 39 * EE: write CIS (?) 40 * EF: compute checksum (?) 41 */ 42 43#include <linux/errno.h> 44#include <linux/module.h> 45#include <linux/slab.h> 46 47#include <scsi/scsi.h> 48#include <scsi/scsi_cmnd.h> 49#include <scsi/scsi_device.h> 50 51#include "usb.h" 52#include "transport.h" 53#include "protocol.h" 54#include "debug.h" 55 56MODULE_DESCRIPTION("Driver for SanDisk SDDR-09 SmartMedia reader"); 57MODULE_AUTHOR("Andries Brouwer <aeb@cwi.nl>, Robert Baruch <autophile@starband.net>"); 58MODULE_LICENSE("GPL"); 59 60static int usb_stor_sddr09_dpcm_init(struct us_data *us); 61static int sddr09_transport(struct scsi_cmnd *srb, struct us_data *us); 62static int usb_stor_sddr09_init(struct us_data *us); 63 64 65/* 66 * The table of devices 67 */ 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)|(USB_US_TYPE_STOR<<24) } 73 74struct usb_device_id sddr09_usb_ids[] = { 75# include "unusual_sddr09.h" 76 { } /* Terminating entry */ 77}; 78MODULE_DEVICE_TABLE(usb, sddr09_usb_ids); 79 80#undef UNUSUAL_DEV 81 82/* 83 * The flags table 84 */ 85#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \ 86 vendor_name, product_name, use_protocol, use_transport, \ 87 init_function, Flags) \ 88{ \ 89 .vendorName = vendor_name, \ 90 .productName = product_name, \ 91 .useProtocol = use_protocol, \ 92 .useTransport = use_transport, \ 93 .initFunction = init_function, \ 94} 95 96static struct us_unusual_dev sddr09_unusual_dev_list[] = { 97# include "unusual_sddr09.h" 98 { } /* Terminating entry */ 99}; 100 101#undef UNUSUAL_DEV 102 103 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) 107 108/* #define US_DEBUGP printk */ 109 110/* 111 * First some stuff that does not belong here: 112 * data on SmartMedia and other cards, completely 113 * unrelated to this driver. 114 * Similar stuff occurs in <linux/mtd/nand_ids.h>. 115 */ 116 117struct nand_flash_dev { 118 int model_id; 119 int chipshift; /* 1<<cs bytes total capacity */ 120 char pageshift; /* 1<<ps bytes in a page */ 121 char blockshift; /* 1<<bs pages in an erase block */ 122 char zoneshift; /* 1<<zs blocks in a zone */ 123 /* # of logical blocks is 125/128 of this */ 124 char pageadrlen; /* length of an address in bytes - 1 */ 125}; 126 127/* 128 * NAND Flash Manufacturer ID Codes 129 */ 130#define NAND_MFR_AMD 0x01 131#define NAND_MFR_NATSEMI 0x8f 132#define NAND_MFR_TOSHIBA 0x98 133#define NAND_MFR_SAMSUNG 0xec 134 135static inline char *nand_flash_manufacturer(int manuf_id) { 136 switch(manuf_id) { 137 case NAND_MFR_AMD: 138 return "AMD"; 139 case NAND_MFR_NATSEMI: 140 return "NATSEMI"; 141 case NAND_MFR_TOSHIBA: 142 return "Toshiba"; 143 case NAND_MFR_SAMSUNG: 144 return "Samsung"; 145 default: 146 return "unknown"; 147 } 148} 149 150/* 151 * It looks like it is unnecessary to attach manufacturer to the 152 * remaining data: SSFDC prescribes manufacturer-independent id codes. 153 * 154 * 256 MB NAND flash has a 5-byte ID with 2nd byte 0xaa, 0xba, 0xca or 0xda. 155 */ 156 157static struct nand_flash_dev nand_flash_ids[] = { 158 /* NAND flash */ 159 { 0x6e, 20, 8, 4, 8, 2}, /* 1 MB */ 160 { 0xe8, 20, 8, 4, 8, 2}, /* 1 MB */ 161 { 0xec, 20, 8, 4, 8, 2}, /* 1 MB */ 162 { 0x64, 21, 8, 4, 9, 2}, /* 2 MB */ 163 { 0xea, 21, 8, 4, 9, 2}, /* 2 MB */ 164 { 0x6b, 22, 9, 4, 9, 2}, /* 4 MB */ 165 { 0xe3, 22, 9, 4, 9, 2}, /* 4 MB */ 166 { 0xe5, 22, 9, 4, 9, 2}, /* 4 MB */ 167 { 0xe6, 23, 9, 4, 10, 2}, /* 8 MB */ 168 { 0x73, 24, 9, 5, 10, 2}, /* 16 MB */ 169 { 0x75, 25, 9, 5, 10, 2}, /* 32 MB */ 170 { 0x76, 26, 9, 5, 10, 3}, /* 64 MB */ 171 { 0x79, 27, 9, 5, 10, 3}, /* 128 MB */ 172 173 /* MASK ROM */ 174 { 0x5d, 21, 9, 4, 8, 2}, /* 2 MB */ 175 { 0xd5, 22, 9, 4, 9, 2}, /* 4 MB */ 176 { 0xd6, 23, 9, 4, 10, 2}, /* 8 MB */ 177 { 0x57, 24, 9, 4, 11, 2}, /* 16 MB */ 178 { 0x58, 25, 9, 4, 12, 2}, /* 32 MB */ 179 { 0,} 180}; 181 182static struct nand_flash_dev * 183nand_find_id(unsigned char id) { 184 int i; 185 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]); 189 return NULL; 190} 191 192/* 193 * ECC computation. 194 */ 195static unsigned char parity[256]; 196static unsigned char ecc2[256]; 197 198static void nand_init_ecc(void) { 199 int i, j, a; 200 201 parity[0] = 0; 202 for (i = 1; i < 256; i++) 203 parity[i] = (parity[i&(i-1)] ^ 1); 204 205 for (i = 0; i < 256; i++) { 206 a = 0; 207 for (j = 0; j < 8; j++) { 208 if (i & (1<<j)) { 209 if ((j & 1) == 0) 210 a ^= 0x04; 211 if ((j & 2) == 0) 212 a ^= 0x10; 213 if ((j & 4) == 0) 214 a ^= 0x40; 215 } 216 } 217 ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0)); 218 } 219} 220 221/* compute 3-byte ecc on 256 bytes */ 222static void nand_compute_ecc(unsigned char *data, unsigned char *ecc) { 223 int i, j, a; 224 unsigned char par, bit, bits[8]; 225 226 par = 0; 227 for (j = 0; j < 8; j++) 228 bits[j] = 0; 229 230 /* collect 16 checksum bits */ 231 for (i = 0; i < 256; i++) { 232 par ^= data[i]; 233 bit = parity[data[i]]; 234 for (j = 0; j < 8; j++) 235 if ((i & (1<<j)) == 0) 236 bits[j] ^= bit; 237 } 238 239 /* put 4+4+4 = 12 bits in the ecc */ 240 a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0]; 241 ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0)); 242 243 a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4]; 244 ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0)); 245 246 ecc[2] = ecc2[par]; 247} 248 249static int nand_compare_ecc(unsigned char *data, unsigned char *ecc) { 250 return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]); 251} 252 253static void nand_store_ecc(unsigned char *data, unsigned char *ecc) { 254 memcpy(data, ecc, 3); 255} 256 257/* 258 * The actual driver starts here. 259 */ 260 261struct sddr09_card_info { 262 unsigned long capacity; /* Size of card in bytes */ 263 int pagesize; /* Size of page in bytes */ 264 int pageshift; /* log2 of pagesize */ 265 int blocksize; /* Size of block in pages */ 266 int blockshift; /* log2 of blocksize */ 267 int blockmask; /* 2^blockshift - 1 */ 268 int *lba_to_pba; /* logical to physical map */ 269 int *pba_to_lba; /* physical to logical map */ 270 int lbact; /* number of available pages */ 271 int flags; 272#define SDDR09_WP 1 /* write protected */ 273}; 274 275/* 276 * On my 16MB card, control blocks have size 64 (16 real control bytes, 277 * and 48 junk bytes). In reality of course the card uses 16 control bytes, 278 * so the reader makes up the remaining 48. Don't know whether these numbers 279 * depend on the card. For now a constant. 280 */ 281#define CONTROL_SHIFT 6 282 283/* 284 * On my Combo CF/SM reader, the SM reader has LUN 1. 285 * (and things fail with LUN 0). 286 * It seems LUN is irrelevant for others. 287 */ 288#define LUN 1 289#define LUNBITS (LUN << 5) 290 291/* 292 * LBA and PBA are unsigned ints. Special values. 293 */ 294#define UNDEF 0xffffffff 295#define SPARE 0xfffffffe 296#define UNUSABLE 0xfffffffd 297 298static const int erase_bad_lba_entries = 0; 299 300/* send vendor interface command (0x41) */ 301/* called for requests 0, 1, 8 */ 302static int 303sddr09_send_command(struct us_data *us, 304 unsigned char request, 305 unsigned char direction, 306 unsigned char *xfer_data, 307 unsigned int xfer_len) { 308 unsigned int pipe; 309 unsigned char requesttype = (0x41 | direction); 310 int rc; 311 312 // Get the receive or send control pipe number 313 314 if (direction == USB_DIR_IN) 315 pipe = us->recv_ctrl_pipe; 316 else 317 pipe = us->send_ctrl_pipe; 318 319 rc = usb_stor_ctrl_transfer(us, pipe, request, requesttype, 320 0, 0, xfer_data, xfer_len); 321 switch (rc) { 322 case USB_STOR_XFER_GOOD: return 0; 323 case USB_STOR_XFER_STALLED: return -EPIPE; 324 default: return -EIO; 325 } 326} 327 328static int 329sddr09_send_scsi_command(struct us_data *us, 330 unsigned char *command, 331 unsigned int command_len) { 332 return sddr09_send_command(us, 0, USB_DIR_OUT, command, command_len); 333} 334 335 336/* 337 * Request Sense Command: 12 bytes. 338 * byte 0: opcode: 03 339 * byte 4: data length 340 */ 341static int 342sddr09_request_sense(struct us_data *us, unsigned char *sensebuf, int buflen) { 343 unsigned char *command = us->iobuf; 344 int result; 345 346 memset(command, 0, 12); 347 command[0] = 0x03; 348 command[1] = LUNBITS; 349 command[4] = buflen; 350 351 result = sddr09_send_scsi_command(us, command, 12); 352 if (result) 353 return result; 354 355 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, 356 sensebuf, buflen, NULL); 357 return (result == USB_STOR_XFER_GOOD ? 0 : -EIO); 358} 359 360/* 361 * Read Command: 12 bytes. 362 * byte 0: opcode: E8 363 * byte 1: last two bits: 00: read data, 01: read blockwise control, 364 * 10: read both, 11: read pagewise control. 365 * It turns out we need values 20, 21, 22, 23 here (LUN 1). 366 * bytes 2-5: address (interpretation depends on byte 1, see below) 367 * bytes 10-11: count (idem) 368 * 369 * A page has 512 data bytes and 64 control bytes (16 control and 48 junk). 370 * A read data command gets data in 512-byte pages. 371 * A read control command gets control in 64-byte chunks. 372 * A read both command gets data+control in 576-byte chunks. 373 * 374 * Blocks are groups of 32 pages, and read blockwise control jumps to the 375 * next block, while read pagewise control jumps to the next page after 376 * reading a group of 64 control bytes. 377 * [Here 512 = 1<<pageshift, 32 = 1<<blockshift, 64 is constant?] 378 * 379 * (1 MB and 2 MB cards are a bit different, but I have only a 16 MB card.) 380 */ 381 382static int 383sddr09_readX(struct us_data *us, int x, unsigned long fromaddress, 384 int nr_of_pages, int bulklen, unsigned char *buf, 385 int use_sg) { 386 387 unsigned char *command = us->iobuf; 388 int result; 389 390 command[0] = 0xE8; 391 command[1] = LUNBITS | x; 392 command[2] = MSB_of(fromaddress>>16); 393 command[3] = LSB_of(fromaddress>>16); 394 command[4] = MSB_of(fromaddress & 0xFFFF); 395 command[5] = LSB_of(fromaddress & 0xFFFF); 396 command[6] = 0; 397 command[7] = 0; 398 command[8] = 0; 399 command[9] = 0; 400 command[10] = MSB_of(nr_of_pages); 401 command[11] = LSB_of(nr_of_pages); 402 403 result = sddr09_send_scsi_command(us, command, 12); 404 405 if (result) { 406 US_DEBUGP("Result for send_control in sddr09_read2%d %d\n", 407 x, result); 408 return result; 409 } 410 411 result = usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe, 412 buf, bulklen, use_sg, NULL); 413 414 if (result != USB_STOR_XFER_GOOD) { 415 US_DEBUGP("Result for bulk_transfer in sddr09_read2%d %d\n", 416 x, result); 417 return -EIO; 418 } 419 return 0; 420} 421 422/* 423 * Read Data 424 * 425 * fromaddress counts data shorts: 426 * increasing it by 256 shifts the bytestream by 512 bytes; 427 * the last 8 bits are ignored. 428 * 429 * nr_of_pages counts pages of size (1 << pageshift). 430 */ 431static int 432sddr09_read20(struct us_data *us, unsigned long fromaddress, 433 int nr_of_pages, int pageshift, unsigned char *buf, int use_sg) { 434 int bulklen = nr_of_pages << pageshift; 435 436 /* The last 8 bits of fromaddress are ignored. */ 437 return sddr09_readX(us, 0, fromaddress, nr_of_pages, bulklen, 438 buf, use_sg); 439} 440 441/* 442 * Read Blockwise Control 443 * 444 * fromaddress gives the starting position (as in read data; 445 * the last 8 bits are ignored); increasing it by 32*256 shifts 446 * the output stream by 64 bytes. 447 * 448 * count counts control groups of size (1 << controlshift). 449 * For me, controlshift = 6. Is this constant? 450 * 451 * After getting one control group, jump to the next block 452 * (fromaddress += 8192). 453 */ 454static int 455sddr09_read21(struct us_data *us, unsigned long fromaddress, 456 int count, int controlshift, unsigned char *buf, int use_sg) { 457 458 int bulklen = (count << controlshift); 459 return sddr09_readX(us, 1, fromaddress, count, bulklen, 460 buf, use_sg); 461} 462 463/* 464 * Read both Data and Control 465 * 466 * fromaddress counts data shorts, ignoring control: 467 * increasing it by 256 shifts the bytestream by 576 = 512+64 bytes; 468 * the last 8 bits are ignored. 469 * 470 * nr_of_pages counts pages of size (1 << pageshift) + (1 << controlshift). 471 */ 472static int 473sddr09_read22(struct us_data *us, unsigned long fromaddress, 474 int nr_of_pages, int pageshift, unsigned char *buf, int use_sg) { 475 476 int bulklen = (nr_of_pages << pageshift) + (nr_of_pages << CONTROL_SHIFT); 477 US_DEBUGP("sddr09_read22: reading %d pages, %d bytes\n", 478 nr_of_pages, bulklen); 479 return sddr09_readX(us, 2, fromaddress, nr_of_pages, bulklen, 480 buf, use_sg); 481} 482 483 484/* 485 * Erase Command: 12 bytes. 486 * byte 0: opcode: EA 487 * bytes 6-9: erase address (big-endian, counting shorts, sector aligned). 488 * 489 * Always precisely one block is erased; bytes 2-5 and 10-11 are ignored. 490 * The byte address being erased is 2*Eaddress. 491 * The CIS cannot be erased. 492 */ 493static int 494sddr09_erase(struct us_data *us, unsigned long Eaddress) { 495 unsigned char *command = us->iobuf; 496 int result; 497 498 US_DEBUGP("sddr09_erase: erase address %lu\n", Eaddress); 499 500 memset(command, 0, 12); 501 command[0] = 0xEA; 502 command[1] = LUNBITS; 503 command[6] = MSB_of(Eaddress>>16); 504 command[7] = LSB_of(Eaddress>>16); 505 command[8] = MSB_of(Eaddress & 0xFFFF); 506 command[9] = LSB_of(Eaddress & 0xFFFF); 507 508 result = sddr09_send_scsi_command(us, command, 12); 509 510 if (result) 511 US_DEBUGP("Result for send_control in sddr09_erase %d\n", 512 result); 513 514 return result; 515} 516 517/* 518 * Write CIS Command: 12 bytes. 519 * byte 0: opcode: EE 520 * bytes 2-5: write address in shorts 521 * bytes 10-11: sector count 522 * 523 * This writes at the indicated address. Don't know how it differs 524 * from E9. Maybe it does not erase? However, it will also write to 525 * the CIS. 526 * 527 * When two such commands on the same page follow each other directly, 528 * the second one is not done. 529 */ 530 531/* 532 * Write Command: 12 bytes. 533 * byte 0: opcode: E9 534 * bytes 2-5: write address (big-endian, counting shorts, sector aligned). 535 * bytes 6-9: erase address (big-endian, counting shorts, sector aligned). 536 * bytes 10-11: sector count (big-endian, in 512-byte sectors). 537 * 538 * If write address equals erase address, the erase is done first, 539 * otherwise the write is done first. When erase address equals zero 540 * no erase is done? 541 */ 542static int 543sddr09_writeX(struct us_data *us, 544 unsigned long Waddress, unsigned long Eaddress, 545 int nr_of_pages, int bulklen, unsigned char *buf, int use_sg) { 546 547 unsigned char *command = us->iobuf; 548 int result; 549 550 command[0] = 0xE9; 551 command[1] = LUNBITS; 552 553 command[2] = MSB_of(Waddress>>16); 554 command[3] = LSB_of(Waddress>>16); 555 command[4] = MSB_of(Waddress & 0xFFFF); 556 command[5] = LSB_of(Waddress & 0xFFFF); 557 558 command[6] = MSB_of(Eaddress>>16); 559 command[7] = LSB_of(Eaddress>>16); 560 command[8] = MSB_of(Eaddress & 0xFFFF); 561 command[9] = LSB_of(Eaddress & 0xFFFF); 562 563 command[10] = MSB_of(nr_of_pages); 564 command[11] = LSB_of(nr_of_pages); 565 566 result = sddr09_send_scsi_command(us, command, 12); 567 568 if (result) { 569 US_DEBUGP("Result for send_control in sddr09_writeX %d\n", 570 result); 571 return result; 572 } 573 574 result = usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe, 575 buf, bulklen, use_sg, NULL); 576 577 if (result != USB_STOR_XFER_GOOD) { 578 US_DEBUGP("Result for bulk_transfer in sddr09_writeX %d\n", 579 result); 580 return -EIO; 581 } 582 return 0; 583} 584 585/* erase address, write same address */ 586static int 587sddr09_write_inplace(struct us_data *us, unsigned long address, 588 int nr_of_pages, int pageshift, unsigned char *buf, 589 int use_sg) { 590 int bulklen = (nr_of_pages << pageshift) + (nr_of_pages << CONTROL_SHIFT); 591 return sddr09_writeX(us, address, address, nr_of_pages, bulklen, 592 buf, use_sg); 593} 594 595 596/* 597 * Read Status Command: 12 bytes. 598 * byte 0: opcode: EC 599 * 600 * Returns 64 bytes, all zero except for the first. 601 * bit 0: 1: Error 602 * bit 5: 1: Suspended 603 * bit 6: 1: Ready 604 * bit 7: 1: Not write-protected 605 */ 606 607static int 608sddr09_read_status(struct us_data *us, unsigned char *status) { 609 610 unsigned char *command = us->iobuf; 611 unsigned char *data = us->iobuf; 612 int result; 613 614 US_DEBUGP("Reading status...\n"); 615 616 memset(command, 0, 12); 617 command[0] = 0xEC; 618 command[1] = LUNBITS; 619 620 result = sddr09_send_scsi_command(us, command, 12); 621 if (result) 622 return result; 623 624 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, 625 data, 64, NULL); 626 *status = data[0]; 627 return (result == USB_STOR_XFER_GOOD ? 0 : -EIO); 628} 629 630static int 631sddr09_read_data(struct us_data *us, 632 unsigned long address, 633 unsigned int sectors) { 634 635 struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra; 636 unsigned char *buffer; 637 unsigned int lba, maxlba, pba; 638 unsigned int page, pages; 639 unsigned int len, offset; 640 struct scatterlist *sg; 641 int result; 642 643 // Figure out the initial LBA and page 644 lba = address >> info->blockshift; 645 page = (address & info->blockmask); 646 maxlba = info->capacity >> (info->pageshift + info->blockshift); 647 if (lba >= maxlba) 648 return -EIO; 649 650 // Since we only read in one block at a time, we have to create 651 // a bounce buffer and move the data a piece at a time between the 652 // bounce buffer and the actual transfer buffer. 653 654 len = min(sectors, (unsigned int) info->blocksize) * info->pagesize; 655 buffer = kmalloc(len, GFP_NOIO); 656 if (buffer == NULL) { 657 printk(KERN_WARNING "sddr09_read_data: Out of memory\n"); 658 return -ENOMEM; 659 } 660 661 // This could be made much more efficient by checking for 662 // contiguous LBA's. Another exercise left to the student. 663 664 result = 0; 665 offset = 0; 666 sg = NULL; 667 668 while (sectors > 0) { 669 670 /* Find number of pages we can read in this block */ 671 pages = min(sectors, info->blocksize - page); 672 len = pages << info->pageshift; 673 674 /* Not overflowing capacity? */ 675 if (lba >= maxlba) { 676 US_DEBUGP("Error: Requested lba %u exceeds " 677 "maximum %u\n", lba, maxlba); 678 result = -EIO; 679 break; 680 } 681 682 /* Find where this lba lives on disk */ 683 pba = info->lba_to_pba[lba]; 684 685 if (pba == UNDEF) { /* this lba was never written */ 686 687 US_DEBUGP("Read %d zero pages (LBA %d) page %d\n", 688 pages, lba, page); 689 690 /* This is not really an error. It just means 691 that the block has never been written. 692 Instead of returning an error 693 it is better to return all zero data. */ 694 695 memset(buffer, 0, len); 696 697 } else { 698 US_DEBUGP("Read %d pages, from PBA %d" 699 " (LBA %d) page %d\n", 700 pages, pba, lba, page); 701 702 address = ((pba << info->blockshift) + page) << 703 info->pageshift; 704 705 result = sddr09_read20(us, address>>1, 706 pages, info->pageshift, buffer, 0); 707 if (result) 708 break; 709 } 710 711 // Store the data in the transfer buffer 712 usb_stor_access_xfer_buf(buffer, len, us->srb, 713 &sg, &offset, TO_XFER_BUF); 714 715 page = 0; 716 lba++; 717 sectors -= pages; 718 } 719 720 kfree(buffer); 721 return result; 722} 723 724static unsigned int 725sddr09_find_unused_pba(struct sddr09_card_info *info, unsigned int lba) { 726 static unsigned int lastpba = 1; 727 int zonestart, end, i; 728 729 zonestart = (lba/1000) << 10; 730 end = info->capacity >> (info->blockshift + info->pageshift); 731 end -= zonestart; 732 if (end > 1024) 733 end = 1024; 734 735 for (i = lastpba+1; i < end; i++) { 736 if (info->pba_to_lba[zonestart+i] == UNDEF) { 737 lastpba = i; 738 return zonestart+i; 739 } 740 } 741 for (i = 0; i <= lastpba; i++) { 742 if (info->pba_to_lba[zonestart+i] == UNDEF) { 743 lastpba = i; 744 return zonestart+i; 745 } 746 } 747 return 0; 748} 749 750static int 751sddr09_write_lba(struct us_data *us, unsigned int lba, 752 unsigned int page, unsigned int pages, 753 unsigned char *ptr, unsigned char *blockbuffer) { 754 755 struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra; 756 unsigned long address; 757 unsigned int pba, lbap; 758 unsigned int pagelen; 759 unsigned char *bptr, *cptr, *xptr; 760 unsigned char ecc[3]; 761 int i, result, isnew; 762 763 lbap = ((lba % 1000) << 1) | 0x1000; 764 if (parity[MSB_of(lbap) ^ LSB_of(lbap)]) 765 lbap ^= 1; 766 pba = info->lba_to_pba[lba]; 767 isnew = 0; 768 769 if (pba == UNDEF) { 770 pba = sddr09_find_unused_pba(info, lba); 771 if (!pba) { 772 printk(KERN_WARNING 773 "sddr09_write_lba: Out of unused blocks\n"); 774 return -ENOSPC; 775 } 776 info->pba_to_lba[pba] = lba; 777 info->lba_to_pba[lba] = pba; 778 isnew = 1; 779 } 780 781 if (pba == 1) { 782 /* Maybe it is impossible to write to PBA 1. 783 Fake success, but don't do anything. */ 784 printk(KERN_WARNING "sddr09: avoid writing to pba 1\n"); 785 return 0; 786 } 787 788 pagelen = (1 << info->pageshift) + (1 << CONTROL_SHIFT); 789 790 /* read old contents */ 791 address = (pba << (info->pageshift + info->blockshift)); 792 result = sddr09_read22(us, address>>1, info->blocksize, 793 info->pageshift, blockbuffer, 0); 794 if (result) 795 return result; 796 797 /* check old contents and fill lba */ 798 for (i = 0; i < info->blocksize; i++) { 799 bptr = blockbuffer + i*pagelen; 800 cptr = bptr + info->pagesize; 801 nand_compute_ecc(bptr, ecc); 802 if (!nand_compare_ecc(cptr+13, ecc)) { 803 US_DEBUGP("Warning: bad ecc in page %d- of pba %d\n", 804 i, pba); 805 nand_store_ecc(cptr+13, ecc); 806 } 807 nand_compute_ecc(bptr+(info->pagesize / 2), ecc); 808 if (!nand_compare_ecc(cptr+8, ecc)) { 809 US_DEBUGP("Warning: bad ecc in page %d+ of pba %d\n", 810 i, pba); 811 nand_store_ecc(cptr+8, ecc); 812 } 813 cptr[6] = cptr[11] = MSB_of(lbap); 814 cptr[7] = cptr[12] = LSB_of(lbap); 815 } 816 817 /* copy in new stuff and compute ECC */ 818 xptr = ptr; 819 for (i = page; i < page+pages; i++) { 820 bptr = blockbuffer + i*pagelen; 821 cptr = bptr + info->pagesize; 822 memcpy(bptr, xptr, info->pagesize); 823 xptr += info->pagesize; 824 nand_compute_ecc(bptr, ecc); 825 nand_store_ecc(cptr+13, ecc); 826 nand_compute_ecc(bptr+(info->pagesize / 2), ecc); 827 nand_store_ecc(cptr+8, ecc); 828 } 829 830 US_DEBUGP("Rewrite PBA %d (LBA %d)\n", pba, lba); 831 832 result = sddr09_write_inplace(us, address>>1, info->blocksize, 833 info->pageshift, blockbuffer, 0); 834 835 US_DEBUGP("sddr09_write_inplace returns %d\n", result); 836 837 838 839 return result; 840} 841 842static int 843sddr09_write_data(struct us_data *us, 844 unsigned long address, 845 unsigned int sectors) { 846 847 struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra; 848 unsigned int lba, maxlba, page, pages; 849 unsigned int pagelen, blocklen; 850 unsigned char *blockbuffer; 851 unsigned char *buffer; 852 unsigned int len, offset; 853 struct scatterlist *sg; 854 int result; 855 856 // Figure out the initial LBA and page 857 lba = address >> info->blockshift; 858 page = (address & info->blockmask); 859 maxlba = info->capacity >> (info->pageshift + info->blockshift); 860 if (lba >= maxlba) 861 return -EIO; 862 863 // blockbuffer is used for reading in the old data, overwriting 864 // with the new data, and performing ECC calculations 865 866 /* TODO: instead of doing kmalloc/kfree for each write, 867 add a bufferpointer to the info structure */ 868 869 pagelen = (1 << info->pageshift) + (1 << CONTROL_SHIFT); 870 blocklen = (pagelen << info->blockshift); 871 blockbuffer = kmalloc(blocklen, GFP_NOIO); 872 if (!blockbuffer) { 873 printk(KERN_WARNING "sddr09_write_data: Out of memory\n"); 874 return -ENOMEM; 875 } 876 877 // Since we don't write the user data directly to the device, 878 // we have to create a bounce buffer and move the data a piece 879 // at a time between the bounce buffer and the actual transfer buffer. 880 881 len = min(sectors, (unsigned int) info->blocksize) * info->pagesize; 882 buffer = kmalloc(len, GFP_NOIO); 883 if (buffer == NULL) { 884 printk(KERN_WARNING "sddr09_write_data: Out of memory\n"); 885 kfree(blockbuffer); 886 return -ENOMEM; 887 } 888 889 result = 0; 890 offset = 0; 891 sg = NULL; 892 893 while (sectors > 0) { 894 895 // Write as many sectors as possible in this block 896 897 pages = min(sectors, info->blocksize - page); 898 len = (pages << info->pageshift); 899 900 /* Not overflowing capacity? */ 901 if (lba >= maxlba) { 902 US_DEBUGP("Error: Requested lba %u exceeds " 903 "maximum %u\n", lba, maxlba); 904 result = -EIO; 905 break; 906 } 907 908 // Get the data from the transfer buffer 909 usb_stor_access_xfer_buf(buffer, len, us->srb, 910 &sg, &offset, FROM_XFER_BUF); 911 912 result = sddr09_write_lba(us, lba, page, pages, 913 buffer, blockbuffer); 914 if (result) 915 break; 916 917 page = 0; 918 lba++; 919 sectors -= pages; 920 } 921 922 kfree(buffer); 923 kfree(blockbuffer); 924 925 return result; 926} 927 928static int 929sddr09_read_control(struct us_data *us, 930 unsigned long address, 931 unsigned int blocks, 932 unsigned char *content, 933 int use_sg) { 934 935 US_DEBUGP("Read control address %lu, blocks %d\n", 936 address, blocks); 937 938 return sddr09_read21(us, address, blocks, 939 CONTROL_SHIFT, content, use_sg); 940} 941 942/* 943 * Read Device ID Command: 12 bytes. 944 * byte 0: opcode: ED 945 * 946 * Returns 2 bytes: Manufacturer ID and Device ID. 947 * On more recent cards 3 bytes: the third byte is an option code A5 948 * signifying that the secret command to read an 128-bit ID is available. 949 * On still more recent cards 4 bytes: the fourth byte C0 means that 950 * a second read ID cmd is available. 951 */ 952static int 953sddr09_read_deviceID(struct us_data *us, unsigned char *deviceID) { 954 unsigned char *command = us->iobuf; 955 unsigned char *content = us->iobuf; 956 int result, i; 957 958 memset(command, 0, 12); 959 command[0] = 0xED; 960 command[1] = LUNBITS; 961 962 result = sddr09_send_scsi_command(us, command, 12); 963 if (result) 964 return result; 965 966 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, 967 content, 64, NULL); 968 969 for (i = 0; i < 4; i++) 970 deviceID[i] = content[i]; 971 972 return (result == USB_STOR_XFER_GOOD ? 0 : -EIO); 973} 974 975static int 976sddr09_get_wp(struct us_data *us, struct sddr09_card_info *info) { 977 int result; 978 unsigned char status; 979 980 result = sddr09_read_status(us, &status); 981 if (result) { 982 US_DEBUGP("sddr09_get_wp: read_status fails\n"); 983 return result; 984 } 985 US_DEBUGP("sddr09_get_wp: status 0x%02X", status); 986 if ((status & 0x80) == 0) { 987 info->flags |= SDDR09_WP; /* write protected */ 988 US_DEBUGP(" WP"); 989 } 990 if (status & 0x40) 991 US_DEBUGP(" Ready"); 992 if (status & LUNBITS) 993 US_DEBUGP(" Suspended"); 994 if (status & 0x1) 995 US_DEBUGP(" Error"); 996 US_DEBUGP("\n"); 997 return 0; 998} 999 1000 1001static struct nand_flash_dev * 1002sddr09_get_cardinfo(struct us_data *us, unsigned char flags) { 1003 struct nand_flash_dev *cardinfo; 1004 unsigned char deviceID[4]; 1005 char blurbtxt[256]; 1006 int result; 1007 1008 US_DEBUGP("Reading capacity...\n"); 1009 1010 result = sddr09_read_deviceID(us, deviceID); 1011 1012 if (result) { 1013 US_DEBUGP("Result of read_deviceID is %d\n", result); 1014 printk(KERN_WARNING "sddr09: could not read card info\n"); 1015 return NULL; 1016 } 1017 1018 sprintf(blurbtxt, "sddr09: Found Flash card, ID = %02X %02X %02X %02X", 1019 deviceID[0], deviceID[1], deviceID[2], deviceID[3]); 1020 1021 /* Byte 0 is the manufacturer */ 1022 sprintf(blurbtxt + strlen(blurbtxt), 1023 ": Manuf. %s", 1024 nand_flash_manufacturer(deviceID[0])); 1025 1026 /* Byte 1 is the device type */ 1027 cardinfo = nand_find_id(deviceID[1]); 1028 if (cardinfo) { 1029 /* MB or MiB? It is neither. A 16 MB card has 1030 17301504 raw bytes, of which 16384000 are 1031 usable for user data. */ 1032 sprintf(blurbtxt + strlen(blurbtxt), 1033 ", %d MB", 1<<(cardinfo->chipshift - 20)); 1034 } else { 1035 sprintf(blurbtxt + strlen(blurbtxt), 1036 ", type unrecognized"); 1037 } 1038 1039 /* Byte 2 is code to signal availability of 128-bit ID */ 1040 if (deviceID[2] == 0xa5) { 1041 sprintf(blurbtxt + strlen(blurbtxt), 1042 ", 128-bit ID"); 1043 } 1044 1045 /* Byte 3 announces the availability of another read ID command */ 1046 if (deviceID[3] == 0xc0) { 1047 sprintf(blurbtxt + strlen(blurbtxt), 1048 ", extra cmd"); 1049 } 1050 1051 if (flags & SDDR09_WP) 1052 sprintf(blurbtxt + strlen(blurbtxt), 1053 ", WP"); 1054 1055 printk(KERN_WARNING "%s\n", blurbtxt); 1056 1057 return cardinfo; 1058} 1059 1060static int 1061sddr09_read_map(struct us_data *us) { 1062 1063 struct sddr09_card_info *info = (struct sddr09_card_info *) us->extra; 1064 int numblocks, alloc_len, alloc_blocks; 1065 int i, j, result; 1066 unsigned char *buffer, *buffer_end, *ptr; 1067 unsigned int lba, lbact; 1068 1069 if (!info->capacity) 1070 return -1; 1071 1072 // size of a block is 1 << (blockshift + pageshift) bytes 1073 // divide into the total capacity to get the number of blocks 1074 1075 numblocks = info->capacity >> (info->blockshift + info->pageshift); 1076 1077 // read 64 bytes for every block (actually 1 << CONTROL_SHIFT) 1078 // but only use a 64 KB buffer 1079 // buffer size used must be a multiple of (1 << CONTROL_SHIFT) 1080#define SDDR09_READ_MAP_BUFSZ 65536 1081 1082 alloc_blocks = min(numblocks, SDDR09_READ_MAP_BUFSZ >> CONTROL_SHIFT); 1083 alloc_len = (alloc_blocks << CONTROL_SHIFT); 1084 buffer = kmalloc(alloc_len, GFP_NOIO); 1085 if (buffer == NULL) { 1086 printk(KERN_WARNING "sddr09_read_map: out of memory\n"); 1087 result = -1; 1088 goto done; 1089 } 1090 buffer_end = buffer + alloc_len; 1091 1092#undef SDDR09_READ_MAP_BUFSZ 1093 1094 kfree(info->lba_to_pba); 1095 kfree(info->pba_to_lba); 1096 info->lba_to_pba = kmalloc(numblocks*sizeof(int), GFP_NOIO); 1097 info->pba_to_lba = kmalloc(numblocks*sizeof(int), GFP_NOIO); 1098 1099 if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) { 1100 printk(KERN_WARNING "sddr09_read_map: out of memory\n"); 1101 result = -1; 1102 goto done; 1103 } 1104 1105 for (i = 0; i < numblocks; i++) 1106 info->lba_to_pba[i] = info->pba_to_lba[i] = UNDEF; 1107 1108 /* 1109 * Define lba-pba translation table 1110 */ 1111 1112 ptr = buffer_end; 1113 for (i = 0; i < numblocks; i++) { 1114 ptr += (1 << CONTROL_SHIFT); 1115 if (ptr >= buffer_end) { 1116 unsigned long address; 1117 1118 address = i << (info->pageshift + info->blockshift); 1119 result = sddr09_read_control( 1120 us, address>>1, 1121 min(alloc_blocks, numblocks - i), 1122 buffer, 0); 1123 if (result) { 1124 result = -1; 1125 goto done; 1126 } 1127 ptr = buffer; 1128 } 1129 1130 if (i == 0 || i == 1) { 1131 info->pba_to_lba[i] = UNUSABLE; 1132 continue; 1133 } 1134 1135 /* special PBAs have control field 0^16 */ 1136 for (j = 0; j < 16; j++) 1137 if (ptr[j] != 0) 1138 goto nonz; 1139 info->pba_to_lba[i] = UNUSABLE; 1140 printk(KERN_WARNING "sddr09: PBA %d has no logical mapping\n", 1141 i); 1142 continue; 1143 1144 nonz: 1145 /* unwritten PBAs have control field FF^16 */ 1146 for (j = 0; j < 16; j++) 1147 if (ptr[j] != 0xff) 1148 goto nonff; 1149 continue; 1150 1151 nonff: 1152 /* normal PBAs start with six FFs */ 1153 if (j < 6) { 1154 printk(KERN_WARNING 1155 "sddr09: PBA %d has no logical mapping: " 1156 "reserved area = %02X%02X%02X%02X " 1157 "data status %02X block status %02X\n", 1158 i, ptr[0], ptr[1], ptr[2], ptr[3], 1159 ptr[4], ptr[5]); 1160 info->pba_to_lba[i] = UNUSABLE; 1161 continue; 1162 } 1163 1164 if ((ptr[6] >> 4) != 0x01) { 1165 printk(KERN_WARNING 1166 "sddr09: PBA %d has invalid address field " 1167 "%02X%02X/%02X%02X\n", 1168 i, ptr[6], ptr[7], ptr[11], ptr[12]); 1169 info->pba_to_lba[i] = UNUSABLE; 1170 continue; 1171 } 1172 1173 /* check even parity */ 1174 if (parity[ptr[6] ^ ptr[7]]) { 1175 printk(KERN_WARNING 1176 "sddr09: Bad parity in LBA for block %d" 1177 " (%02X %02X)\n", i, ptr[6], ptr[7]); 1178 info->pba_to_lba[i] = UNUSABLE; 1179 continue; 1180 } 1181 1182 lba = short_pack(ptr[7], ptr[6]); 1183 lba = (lba & 0x07FF) >> 1; 1184 1185 /* 1186 * Every 1024 physical blocks ("zone"), the LBA numbers 1187 * go back to zero, but are within a higher block of LBA's. 1188 * Also, there is a maximum of 1000 LBA's per zone. 1189 * In other words, in PBA 1024-2047 you will find LBA 0-999 1190 * which are really LBA 1000-1999. This allows for 24 bad 1191 * or special physical blocks per zone. 1192 */ 1193 1194 if (lba >= 1000) { 1195 printk(KERN_WARNING 1196 "sddr09: Bad low LBA %d for block %d\n", 1197 lba, i); 1198 goto possibly_erase; 1199 } 1200 1201 lba += 1000*(i/0x400); 1202 1203 if (info->lba_to_pba[lba] != UNDEF) { 1204 printk(KERN_WARNING 1205 "sddr09: LBA %d seen for PBA %d and %d\n", 1206 lba, info->lba_to_pba[lba], i); 1207 goto possibly_erase; 1208 } 1209 1210 info->pba_to_lba[i] = lba; 1211 info->lba_to_pba[lba] = i; 1212 continue; 1213 1214 possibly_erase: 1215 if (erase_bad_lba_entries) { 1216 unsigned long address; 1217 1218 address = (i << (info->pageshift + info->blockshift)); 1219 sddr09_erase(us, address>>1); 1220 info->pba_to_lba[i] = UNDEF; 1221 } else 1222 info->pba_to_lba[i] = UNUSABLE; 1223 } 1224 1225 /* 1226 * Approximate capacity. This is not entirely correct yet, 1227 * since a zone with less than 1000 usable pages leads to 1228 * missing LBAs. Especially if it is the last zone, some 1229 * LBAs can be past capacity. 1230 */ 1231 lbact = 0; 1232 for (i = 0; i < numblocks; i += 1024) { 1233 int ct = 0; 1234 1235 for (j = 0; j < 1024 && i+j < numblocks; j++) { 1236 if (info->pba_to_lba[i+j] != UNUSABLE) { 1237 if (ct >= 1000) 1238 info->pba_to_lba[i+j] = SPARE; 1239 else 1240 ct++; 1241 } 1242 } 1243 lbact += ct; 1244 } 1245 info->lbact = lbact; 1246 US_DEBUGP("Found %d LBA's\n", lbact); 1247 result = 0; 1248 1249 done: 1250 if (result != 0) { 1251 kfree(info->lba_to_pba); 1252 kfree(info->pba_to_lba); 1253 info->lba_to_pba = NULL; 1254 info->pba_to_lba = NULL; 1255 } 1256 kfree(buffer); 1257 return result; 1258} 1259 1260static void 1261sddr09_card_info_destructor(void *extra) { 1262 struct sddr09_card_info *info = (struct sddr09_card_info *)extra; 1263 1264 if (!info) 1265 return; 1266 1267 kfree(info->lba_to_pba); 1268 kfree(info->pba_to_lba); 1269} 1270 1271static int 1272sddr09_common_init(struct us_data *us) { 1273 int result; 1274 1275 /* set the configuration -- STALL is an acceptable response here */ 1276 if (us->pusb_dev->actconfig->desc.bConfigurationValue != 1) { 1277 US_DEBUGP("active config #%d != 1 ??\n", us->pusb_dev 1278 ->actconfig->desc.bConfigurationValue); 1279 return -EINVAL; 1280 } 1281 1282 result = usb_reset_configuration(us->pusb_dev); 1283 US_DEBUGP("Result of usb_reset_configuration is %d\n", result); 1284 if (result == -EPIPE) { 1285 US_DEBUGP("-- stall on control interface\n"); 1286 } else if (result != 0) { 1287 /* it's not a stall, but another error -- time to bail */ 1288 US_DEBUGP("-- Unknown error. Rejecting device\n"); 1289 return -EINVAL; 1290 } 1291 1292 us->extra = kzalloc(sizeof(struct sddr09_card_info), GFP_NOIO); 1293 if (!us->extra) 1294 return -ENOMEM; 1295 us->extra_destructor = sddr09_card_info_destructor; 1296 1297 nand_init_ecc(); 1298 return 0; 1299} 1300 1301 1302/* 1303 * This is needed at a very early stage. If this is not listed in the 1304 * unusual devices list but called from here then LUN 0 of the combo reader 1305 * is not recognized. But I do not know what precisely these calls do. 1306 */ 1307static int 1308usb_stor_sddr09_dpcm_init(struct us_data *us) { 1309 int result; 1310 unsigned char *data = us->iobuf; 1311 1312 result = sddr09_common_init(us); 1313 if (result) 1314 return result; 1315 1316 result = sddr09_send_command(us, 0x01, USB_DIR_IN, data, 2); 1317 if (result) { 1318 US_DEBUGP("sddr09_init: send_command fails\n"); 1319 return result; 1320 } 1321 1322 US_DEBUGP("SDDR09init: %02X %02X\n", data[0], data[1]); 1323 // get 07 02 1324 1325 result = sddr09_send_command(us, 0x08, USB_DIR_IN, data, 2); 1326 if (result) { 1327 US_DEBUGP("sddr09_init: 2nd send_command fails\n"); 1328 return result; 1329 } 1330 1331 US_DEBUGP("SDDR09init: %02X %02X\n", data[0], data[1]); 1332 // get 07 00 1333 1334 result = sddr09_request_sense(us, data, 18); 1335 if (result == 0 && data[2] != 0) { 1336 int j; 1337 for (j=0; j<18; j++) 1338 printk(" %02X", data[j]); 1339 printk("\n"); 1340 // get 70 00 00 00 00 00 00 * 00 00 00 00 00 00 1341 // 70: current command 1342 // sense key 0, sense code 0, extd sense code 0 1343 // additional transfer length * = sizeof(data) - 7 1344 // Or: 70 00 06 00 00 00 00 0b 00 00 00 00 28 00 00 00 00 00 1345 // sense key 06, sense code 28: unit attention, 1346 // not ready to ready transition 1347 } 1348 1349 // test unit ready 1350 1351 return 0; /* not result */ 1352} 1353 1354/* 1355 * Transport for the Microtech DPCM-USB 1356 */ 1357static int dpcm_transport(struct scsi_cmnd *srb, struct us_data *us) 1358{ 1359 int ret; 1360 1361 US_DEBUGP("dpcm_transport: LUN=%d\n", srb->device->lun); 1362 1363 switch (srb->device->lun) { 1364 case 0: 1365 1366 /* 1367 * LUN 0 corresponds to the CompactFlash card reader. 1368 */ 1369 ret = usb_stor_CB_transport(srb, us); 1370 break; 1371 1372 case 1: 1373 1374 /* 1375 * LUN 1 corresponds to the SmartMedia card reader. 1376 */ 1377 1378 /* 1379 * Set the LUN to 0 (just in case). 1380 */ 1381 srb->device->lun = 0; 1382 ret = sddr09_transport(srb, us); 1383 srb->device->lun = 1; 1384 break; 1385 1386 default: 1387 US_DEBUGP("dpcm_transport: Invalid LUN %d\n", 1388 srb->device->lun); 1389 ret = USB_STOR_TRANSPORT_ERROR; 1390 break; 1391 } 1392 return ret; 1393} 1394 1395 1396/* 1397 * Transport for the Sandisk SDDR-09 1398 */ 1399static int sddr09_transport(struct scsi_cmnd *srb, struct us_data *us) 1400{ 1401 static unsigned char sensekey = 0, sensecode = 0; 1402 static unsigned char havefakesense = 0; 1403 int result, i; 1404 unsigned char *ptr = us->iobuf; 1405 unsigned long capacity; 1406 unsigned int page, pages; 1407 1408 struct sddr09_card_info *info; 1409 1410 static unsigned char inquiry_response[8] = { 1411 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00 1412 }; 1413 1414 /* note: no block descriptor support */ 1415 static unsigned char mode_page_01[19] = { 1416 0x00, 0x0F, 0x00, 0x0, 0x0, 0x0, 0x00, 1417 0x01, 0x0A, 1418 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 1419 }; 1420 1421 info = (struct sddr09_card_info *)us->extra; 1422 1423 if (srb->cmnd[0] == REQUEST_SENSE && havefakesense) { 1424 /* for a faked command, we have to follow with a faked sense */ 1425 memset(ptr, 0, 18); 1426 ptr[0] = 0x70; 1427 ptr[2] = sensekey; 1428 ptr[7] = 11; 1429 ptr[12] = sensecode; 1430 usb_stor_set_xfer_buf(ptr, 18, srb); 1431 sensekey = sensecode = havefakesense = 0; 1432 return USB_STOR_TRANSPORT_GOOD; 1433 } 1434 1435 havefakesense = 1; 1436 1437 /* Dummy up a response for INQUIRY since SDDR09 doesn't 1438 respond to INQUIRY commands */ 1439 1440 if (srb->cmnd[0] == INQUIRY) { 1441 memcpy(ptr, inquiry_response, 8); 1442 fill_inquiry_response(us, ptr, 36); 1443 return USB_STOR_TRANSPORT_GOOD; 1444 } 1445 1446 if (srb->cmnd[0] == READ_CAPACITY) { 1447 struct nand_flash_dev *cardinfo; 1448 1449 sddr09_get_wp(us, info); /* read WP bit */ 1450 1451 cardinfo = sddr09_get_cardinfo(us, info->flags); 1452 if (!cardinfo) { 1453 /* probably no media */ 1454 init_error: 1455 sensekey = 0x02; /* not ready */ 1456 sensecode = 0x3a; /* medium not present */ 1457 return USB_STOR_TRANSPORT_FAILED; 1458 } 1459 1460 info->capacity = (1 << cardinfo->chipshift); 1461 info->pageshift = cardinfo->pageshift; 1462 info->pagesize = (1 << info->pageshift); 1463 info->blockshift = cardinfo->blockshift; 1464 info->blocksize = (1 << info->blockshift); 1465 info->blockmask = info->blocksize - 1; 1466 1467 // map initialization, must follow get_cardinfo() 1468 if (sddr09_read_map(us)) { 1469 /* probably out of memory */ 1470 goto init_error; 1471 } 1472 1473 // Report capacity 1474 1475 capacity = (info->lbact << info->blockshift) - 1; 1476 1477 ((__be32 *) ptr)[0] = cpu_to_be32(capacity); 1478 1479 // Report page size 1480 1481 ((__be32 *) ptr)[1] = cpu_to_be32(info->pagesize); 1482 usb_stor_set_xfer_buf(ptr, 8, srb); 1483 1484 return USB_STOR_TRANSPORT_GOOD; 1485 } 1486 1487 if (srb->cmnd[0] == MODE_SENSE_10) { 1488 int modepage = (srb->cmnd[2] & 0x3F); 1489 1490 /* They ask for the Read/Write error recovery page, 1491 or for all pages. */ 1492 /* %% We should check DBD %% */ 1493 if (modepage == 0x01 || modepage == 0x3F) { 1494 US_DEBUGP("SDDR09: Dummy up request for " 1495 "mode page 0x%x\n", modepage); 1496 1497 memcpy(ptr, mode_page_01, sizeof(mode_page_01)); 1498 ((__be16*)ptr)[0] = cpu_to_be16(sizeof(mode_page_01) - 2); 1499 ptr[3] = (info->flags & SDDR09_WP) ? 0x80 : 0; 1500 usb_stor_set_xfer_buf(ptr, sizeof(mode_page_01), srb); 1501 return USB_STOR_TRANSPORT_GOOD; 1502 } 1503 1504 sensekey = 0x05; /* illegal request */ 1505 sensecode = 0x24; /* invalid field in CDB */ 1506 return USB_STOR_TRANSPORT_FAILED; 1507 } 1508 1509 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) 1510 return USB_STOR_TRANSPORT_GOOD; 1511 1512 havefakesense = 0; 1513 1514 if (srb->cmnd[0] == READ_10) { 1515 1516 page = short_pack(srb->cmnd[3], srb->cmnd[2]); 1517 page <<= 16; 1518 page |= short_pack(srb->cmnd[5], srb->cmnd[4]); 1519 pages = short_pack(srb->cmnd[8], srb->cmnd[7]); 1520 1521 US_DEBUGP("READ_10: read page %d pagect %d\n", 1522 page, pages); 1523 1524 result = sddr09_read_data(us, page, pages); 1525 return (result == 0 ? USB_STOR_TRANSPORT_GOOD : 1526 USB_STOR_TRANSPORT_ERROR); 1527 } 1528 1529 if (srb->cmnd[0] == WRITE_10) { 1530 1531 page = short_pack(srb->cmnd[3], srb->cmnd[2]); 1532 page <<= 16; 1533 page |= short_pack(srb->cmnd[5], srb->cmnd[4]); 1534 pages = short_pack(srb->cmnd[8], srb->cmnd[7]); 1535 1536 US_DEBUGP("WRITE_10: write page %d pagect %d\n", 1537 page, pages); 1538 1539 result = sddr09_write_data(us, page, pages); 1540 return (result == 0 ? USB_STOR_TRANSPORT_GOOD : 1541 USB_STOR_TRANSPORT_ERROR); 1542 } 1543 1544 /* catch-all for all other commands, except 1545 * pass TEST_UNIT_READY and REQUEST_SENSE through 1546 */ 1547 if (srb->cmnd[0] != TEST_UNIT_READY && 1548 srb->cmnd[0] != REQUEST_SENSE) { 1549 sensekey = 0x05; /* illegal request */ 1550 sensecode = 0x20; /* invalid command */ 1551 havefakesense = 1; 1552 return USB_STOR_TRANSPORT_FAILED; 1553 } 1554 1555 for (; srb->cmd_len<12; srb->cmd_len++) 1556 srb->cmnd[srb->cmd_len] = 0; 1557 1558 srb->cmnd[1] = LUNBITS; 1559 1560 ptr[0] = 0; 1561 for (i=0; i<12; i++) 1562 sprintf(ptr+strlen(ptr), "%02X ", srb->cmnd[i]); 1563 1564 US_DEBUGP("SDDR09: Send control for command %s\n", ptr); 1565 1566 result = sddr09_send_scsi_command(us, srb->cmnd, 12); 1567 if (result) { 1568 US_DEBUGP("sddr09_transport: sddr09_send_scsi_command " 1569 "returns %d\n", result); 1570 return USB_STOR_TRANSPORT_ERROR; 1571 } 1572 1573 if (scsi_bufflen(srb) == 0) 1574 return USB_STOR_TRANSPORT_GOOD; 1575 1576 if (srb->sc_data_direction == DMA_TO_DEVICE || 1577 srb->sc_data_direction == DMA_FROM_DEVICE) { 1578 unsigned int pipe = (srb->sc_data_direction == DMA_TO_DEVICE) 1579 ? us->send_bulk_pipe : us->recv_bulk_pipe; 1580 1581 US_DEBUGP("SDDR09: %s %d bytes\n", 1582 (srb->sc_data_direction == DMA_TO_DEVICE) ? 1583 "sending" : "receiving", 1584 scsi_bufflen(srb)); 1585 1586 result = usb_stor_bulk_srb(us, pipe, srb); 1587 1588 return (result == USB_STOR_XFER_GOOD ? 1589 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR); 1590 } 1591 1592 return USB_STOR_TRANSPORT_GOOD; 1593} 1594 1595/* 1596 * Initialization routine for the sddr09 subdriver 1597 */ 1598static int 1599usb_stor_sddr09_init(struct us_data *us) { 1600 return sddr09_common_init(us); 1601} 1602 1603static int sddr09_probe(struct usb_interface *intf, 1604 const struct usb_device_id *id) 1605{ 1606 struct us_data *us; 1607 int result; 1608 1609 result = usb_stor_probe1(&us, intf, id, 1610 (id - sddr09_usb_ids) + sddr09_unusual_dev_list); 1611 if (result) 1612 return result; 1613 1614 if (us->protocol == US_PR_DPCM_USB) { 1615 us->transport_name = "Control/Bulk-EUSB/SDDR09"; 1616 us->transport = dpcm_transport; 1617 us->transport_reset = usb_stor_CB_reset; 1618 us->max_lun = 1; 1619 } else { 1620 us->transport_name = "EUSB/SDDR09"; 1621 us->transport = sddr09_transport; 1622 us->transport_reset = usb_stor_CB_reset; 1623 us->max_lun = 0; 1624 } 1625 1626 result = usb_stor_probe2(us); 1627 return result; 1628} 1629 1630static struct usb_driver sddr09_driver = { 1631 .name = "ums-sddr09", 1632 .probe = sddr09_probe, 1633 .disconnect = usb_stor_disconnect, 1634 .suspend = usb_stor_suspend, 1635 .resume = usb_stor_resume, 1636 .reset_resume = usb_stor_reset_resume, 1637 .pre_reset = usb_stor_pre_reset, 1638 .post_reset = usb_stor_post_reset, 1639 .id_table = sddr09_usb_ids, 1640 .soft_unbind = 1, 1641}; 1642 1643static int __init sddr09_init(void) 1644{ 1645 return usb_register(&sddr09_driver); 1646} 1647 1648static void __exit sddr09_exit(void) 1649{ 1650 usb_deregister(&sddr09_driver); 1651} 1652 1653module_init(sddr09_init); 1654module_exit(sddr09_exit); 1655