1 /* 2 * Driver for Alauda-based card readers 3 * 4 * Current development and maintenance by: 5 * (c) 2005 Daniel Drake <dsd@gentoo.org> 6 * 7 * The 'Alauda' is a chip manufacturered by RATOC for OEM use. 8 * 9 * Alauda implements a vendor-specific command set to access two media reader 10 * ports (XD, SmartMedia). This driver converts SCSI commands to the commands 11 * which are accepted by these devices. 12 * 13 * The driver was developed through reverse-engineering, with the help of the 14 * sddr09 driver which has many similarities, and with some help from the 15 * (very old) vendor-supplied GPL sma03 driver. 16 * 17 * For protocol info, see http://alauda.sourceforge.net 18 * 19 * This program is free software; you can redistribute it and/or modify it 20 * under the terms of the GNU General Public License as published by the 21 * Free Software Foundation; either version 2, or (at your option) any 22 * later version. 23 * 24 * This program is distributed in the hope that it will be useful, but 25 * WITHOUT ANY WARRANTY; without even the implied warranty of 26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 27 * General Public License for more details. 28 * 29 * You should have received a copy of the GNU General Public License along 30 * with this program; if not, write to the Free Software Foundation, Inc., 31 * 675 Mass Ave, Cambridge, MA 02139, USA. 32 */ 33 34 #include <linux/module.h> 35 #include <linux/slab.h> 36 37 #include <scsi/scsi.h> 38 #include <scsi/scsi_cmnd.h> 39 #include <scsi/scsi_device.h> 40 41 #include "usb.h" 42 #include "transport.h" 43 #include "protocol.h" 44 #include "debug.h" 45 46 MODULE_DESCRIPTION("Driver for Alauda-based card readers"); 47 MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>"); 48 MODULE_LICENSE("GPL"); 49 50 /* 51 * Status bytes 52 */ 53 #define ALAUDA_STATUS_ERROR 0x01 54 #define ALAUDA_STATUS_READY 0x40 55 56 /* 57 * Control opcodes (for request field) 58 */ 59 #define ALAUDA_GET_XD_MEDIA_STATUS 0x08 60 #define ALAUDA_GET_SM_MEDIA_STATUS 0x98 61 #define ALAUDA_ACK_XD_MEDIA_CHANGE 0x0a 62 #define ALAUDA_ACK_SM_MEDIA_CHANGE 0x9a 63 #define ALAUDA_GET_XD_MEDIA_SIG 0x86 64 #define ALAUDA_GET_SM_MEDIA_SIG 0x96 65 66 /* 67 * Bulk command identity (byte 0) 68 */ 69 #define ALAUDA_BULK_CMD 0x40 70 71 /* 72 * Bulk opcodes (byte 1) 73 */ 74 #define ALAUDA_BULK_GET_REDU_DATA 0x85 75 #define ALAUDA_BULK_READ_BLOCK 0x94 76 #define ALAUDA_BULK_ERASE_BLOCK 0xa3 77 #define ALAUDA_BULK_WRITE_BLOCK 0xb4 78 #define ALAUDA_BULK_GET_STATUS2 0xb7 79 #define ALAUDA_BULK_RESET_MEDIA 0xe0 80 81 /* 82 * Port to operate on (byte 8) 83 */ 84 #define ALAUDA_PORT_XD 0x00 85 #define ALAUDA_PORT_SM 0x01 86 87 /* 88 * LBA and PBA are unsigned ints. Special values. 89 */ 90 #define UNDEF 0xffff 91 #define SPARE 0xfffe 92 #define UNUSABLE 0xfffd 93 94 struct alauda_media_info { 95 unsigned long capacity; /* total media size in bytes */ 96 unsigned int pagesize; /* page size in bytes */ 97 unsigned int blocksize; /* number of pages per block */ 98 unsigned int uzonesize; /* number of usable blocks per zone */ 99 unsigned int zonesize; /* number of blocks per zone */ 100 unsigned int blockmask; /* mask to get page from address */ 101 102 unsigned char pageshift; 103 unsigned char blockshift; 104 unsigned char zoneshift; 105 106 u16 **lba_to_pba; /* logical to physical block map */ 107 u16 **pba_to_lba; /* physical to logical block map */ 108 }; 109 110 struct alauda_info { 111 struct alauda_media_info port[2]; 112 int wr_ep; /* endpoint to write data out of */ 113 114 unsigned char sense_key; 115 unsigned long sense_asc; /* additional sense code */ 116 unsigned long sense_ascq; /* additional sense code qualifier */ 117 }; 118 119 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) ) 120 #define LSB_of(s) ((s)&0xFF) 121 #define MSB_of(s) ((s)>>8) 122 123 #define MEDIA_PORT(us) us->srb->device->lun 124 #define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)] 125 126 #define PBA_LO(pba) ((pba & 0xF) << 5) 127 #define PBA_HI(pba) (pba >> 3) 128 #define PBA_ZONE(pba) (pba >> 11) 129 130 static int init_alauda(struct us_data *us); 131 132 133 /* 134 * The table of devices 135 */ 136 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \ 137 vendorName, productName, useProtocol, useTransport, \ 138 initFunction, flags) \ 139 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \ 140 .driver_info = (flags) } 141 142 static struct usb_device_id alauda_usb_ids[] = { 143 # include "unusual_alauda.h" 144 { } /* Terminating entry */ 145 }; 146 MODULE_DEVICE_TABLE(usb, alauda_usb_ids); 147 148 #undef UNUSUAL_DEV 149 150 /* 151 * The flags table 152 */ 153 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \ 154 vendor_name, product_name, use_protocol, use_transport, \ 155 init_function, Flags) \ 156 { \ 157 .vendorName = vendor_name, \ 158 .productName = product_name, \ 159 .useProtocol = use_protocol, \ 160 .useTransport = use_transport, \ 161 .initFunction = init_function, \ 162 } 163 164 static struct us_unusual_dev alauda_unusual_dev_list[] = { 165 # include "unusual_alauda.h" 166 { } /* Terminating entry */ 167 }; 168 169 #undef UNUSUAL_DEV 170 171 172 /* 173 * Media handling 174 */ 175 176 struct alauda_card_info { 177 unsigned char id; /* id byte */ 178 unsigned char chipshift; /* 1<<cs bytes total capacity */ 179 unsigned char pageshift; /* 1<<ps bytes in a page */ 180 unsigned char blockshift; /* 1<<bs pages per block */ 181 unsigned char zoneshift; /* 1<<zs blocks per zone */ 182 }; 183 184 static struct alauda_card_info alauda_card_ids[] = { 185 /* NAND flash */ 186 { 0x6e, 20, 8, 4, 8}, /* 1 MB */ 187 { 0xe8, 20, 8, 4, 8}, /* 1 MB */ 188 { 0xec, 20, 8, 4, 8}, /* 1 MB */ 189 { 0x64, 21, 8, 4, 9}, /* 2 MB */ 190 { 0xea, 21, 8, 4, 9}, /* 2 MB */ 191 { 0x6b, 22, 9, 4, 9}, /* 4 MB */ 192 { 0xe3, 22, 9, 4, 9}, /* 4 MB */ 193 { 0xe5, 22, 9, 4, 9}, /* 4 MB */ 194 { 0xe6, 23, 9, 4, 10}, /* 8 MB */ 195 { 0x73, 24, 9, 5, 10}, /* 16 MB */ 196 { 0x75, 25, 9, 5, 10}, /* 32 MB */ 197 { 0x76, 26, 9, 5, 10}, /* 64 MB */ 198 { 0x79, 27, 9, 5, 10}, /* 128 MB */ 199 { 0x71, 28, 9, 5, 10}, /* 256 MB */ 200 201 /* MASK ROM */ 202 { 0x5d, 21, 9, 4, 8}, /* 2 MB */ 203 { 0xd5, 22, 9, 4, 9}, /* 4 MB */ 204 { 0xd6, 23, 9, 4, 10}, /* 8 MB */ 205 { 0x57, 24, 9, 4, 11}, /* 16 MB */ 206 { 0x58, 25, 9, 4, 12}, /* 32 MB */ 207 { 0,} 208 }; 209 210 static struct alauda_card_info *alauda_card_find_id(unsigned char id) { 211 int i; 212 213 for (i = 0; alauda_card_ids[i].id != 0; i++) 214 if (alauda_card_ids[i].id == id) 215 return &(alauda_card_ids[i]); 216 return NULL; 217 } 218 219 /* 220 * ECC computation. 221 */ 222 223 static unsigned char parity[256]; 224 static unsigned char ecc2[256]; 225 226 static void nand_init_ecc(void) { 227 int i, j, a; 228 229 parity[0] = 0; 230 for (i = 1; i < 256; i++) 231 parity[i] = (parity[i&(i-1)] ^ 1); 232 233 for (i = 0; i < 256; i++) { 234 a = 0; 235 for (j = 0; j < 8; j++) { 236 if (i & (1<<j)) { 237 if ((j & 1) == 0) 238 a ^= 0x04; 239 if ((j & 2) == 0) 240 a ^= 0x10; 241 if ((j & 4) == 0) 242 a ^= 0x40; 243 } 244 } 245 ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0)); 246 } 247 } 248 249 /* compute 3-byte ecc on 256 bytes */ 250 static void nand_compute_ecc(unsigned char *data, unsigned char *ecc) { 251 int i, j, a; 252 unsigned char par = 0, bit, bits[8] = {0}; 253 254 /* collect 16 checksum bits */ 255 for (i = 0; i < 256; i++) { 256 par ^= data[i]; 257 bit = parity[data[i]]; 258 for (j = 0; j < 8; j++) 259 if ((i & (1<<j)) == 0) 260 bits[j] ^= bit; 261 } 262 263 /* put 4+4+4 = 12 bits in the ecc */ 264 a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0]; 265 ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0)); 266 267 a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4]; 268 ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0)); 269 270 ecc[2] = ecc2[par]; 271 } 272 273 static int nand_compare_ecc(unsigned char *data, unsigned char *ecc) { 274 return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]); 275 } 276 277 static void nand_store_ecc(unsigned char *data, unsigned char *ecc) { 278 memcpy(data, ecc, 3); 279 } 280 281 /* 282 * Alauda driver 283 */ 284 285 /* 286 * Forget our PBA <---> LBA mappings for a particular port 287 */ 288 static void alauda_free_maps (struct alauda_media_info *media_info) 289 { 290 unsigned int shift = media_info->zoneshift 291 + media_info->blockshift + media_info->pageshift; 292 unsigned int num_zones = media_info->capacity >> shift; 293 unsigned int i; 294 295 if (media_info->lba_to_pba != NULL) 296 for (i = 0; i < num_zones; i++) { 297 kfree(media_info->lba_to_pba[i]); 298 media_info->lba_to_pba[i] = NULL; 299 } 300 301 if (media_info->pba_to_lba != NULL) 302 for (i = 0; i < num_zones; i++) { 303 kfree(media_info->pba_to_lba[i]); 304 media_info->pba_to_lba[i] = NULL; 305 } 306 } 307 308 /* 309 * Returns 2 bytes of status data 310 * The first byte describes media status, and second byte describes door status 311 */ 312 static int alauda_get_media_status(struct us_data *us, unsigned char *data) 313 { 314 int rc; 315 unsigned char command; 316 317 if (MEDIA_PORT(us) == ALAUDA_PORT_XD) 318 command = ALAUDA_GET_XD_MEDIA_STATUS; 319 else 320 command = ALAUDA_GET_SM_MEDIA_STATUS; 321 322 rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe, 323 command, 0xc0, 0, 1, data, 2); 324 325 usb_stor_dbg(us, "Media status %02X %02X\n", data[0], data[1]); 326 327 return rc; 328 } 329 330 /* 331 * Clears the "media was changed" bit so that we know when it changes again 332 * in the future. 333 */ 334 static int alauda_ack_media(struct us_data *us) 335 { 336 unsigned char command; 337 338 if (MEDIA_PORT(us) == ALAUDA_PORT_XD) 339 command = ALAUDA_ACK_XD_MEDIA_CHANGE; 340 else 341 command = ALAUDA_ACK_SM_MEDIA_CHANGE; 342 343 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe, 344 command, 0x40, 0, 1, NULL, 0); 345 } 346 347 /* 348 * Retrieves a 4-byte media signature, which indicates manufacturer, capacity, 349 * and some other details. 350 */ 351 static int alauda_get_media_signature(struct us_data *us, unsigned char *data) 352 { 353 unsigned char command; 354 355 if (MEDIA_PORT(us) == ALAUDA_PORT_XD) 356 command = ALAUDA_GET_XD_MEDIA_SIG; 357 else 358 command = ALAUDA_GET_SM_MEDIA_SIG; 359 360 return usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe, 361 command, 0xc0, 0, 0, data, 4); 362 } 363 364 /* 365 * Resets the media status (but not the whole device?) 366 */ 367 static int alauda_reset_media(struct us_data *us) 368 { 369 unsigned char *command = us->iobuf; 370 371 memset(command, 0, 9); 372 command[0] = ALAUDA_BULK_CMD; 373 command[1] = ALAUDA_BULK_RESET_MEDIA; 374 command[8] = MEDIA_PORT(us); 375 376 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, 377 command, 9, NULL); 378 } 379 380 /* 381 * Examines the media and deduces capacity, etc. 382 */ 383 static int alauda_init_media(struct us_data *us) 384 { 385 unsigned char *data = us->iobuf; 386 int ready = 0; 387 struct alauda_card_info *media_info; 388 unsigned int num_zones; 389 390 while (ready == 0) { 391 msleep(20); 392 393 if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD) 394 return USB_STOR_TRANSPORT_ERROR; 395 396 if (data[0] & 0x10) 397 ready = 1; 398 } 399 400 usb_stor_dbg(us, "We are ready for action!\n"); 401 402 if (alauda_ack_media(us) != USB_STOR_XFER_GOOD) 403 return USB_STOR_TRANSPORT_ERROR; 404 405 msleep(10); 406 407 if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD) 408 return USB_STOR_TRANSPORT_ERROR; 409 410 if (data[0] != 0x14) { 411 usb_stor_dbg(us, "Media not ready after ack\n"); 412 return USB_STOR_TRANSPORT_ERROR; 413 } 414 415 if (alauda_get_media_signature(us, data) != USB_STOR_XFER_GOOD) 416 return USB_STOR_TRANSPORT_ERROR; 417 418 usb_stor_dbg(us, "Media signature: %4ph\n", data); 419 media_info = alauda_card_find_id(data[1]); 420 if (media_info == NULL) { 421 pr_warn("alauda_init_media: Unrecognised media signature: %4ph\n", 422 data); 423 return USB_STOR_TRANSPORT_ERROR; 424 } 425 426 MEDIA_INFO(us).capacity = 1 << media_info->chipshift; 427 usb_stor_dbg(us, "Found media with capacity: %ldMB\n", 428 MEDIA_INFO(us).capacity >> 20); 429 430 MEDIA_INFO(us).pageshift = media_info->pageshift; 431 MEDIA_INFO(us).blockshift = media_info->blockshift; 432 MEDIA_INFO(us).zoneshift = media_info->zoneshift; 433 434 MEDIA_INFO(us).pagesize = 1 << media_info->pageshift; 435 MEDIA_INFO(us).blocksize = 1 << media_info->blockshift; 436 MEDIA_INFO(us).zonesize = 1 << media_info->zoneshift; 437 438 MEDIA_INFO(us).uzonesize = ((1 << media_info->zoneshift) / 128) * 125; 439 MEDIA_INFO(us).blockmask = MEDIA_INFO(us).blocksize - 1; 440 441 num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift 442 + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift); 443 MEDIA_INFO(us).pba_to_lba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO); 444 MEDIA_INFO(us).lba_to_pba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO); 445 446 if (alauda_reset_media(us) != USB_STOR_XFER_GOOD) 447 return USB_STOR_TRANSPORT_ERROR; 448 449 return USB_STOR_TRANSPORT_GOOD; 450 } 451 452 /* 453 * Examines the media status and does the right thing when the media has gone, 454 * appeared, or changed. 455 */ 456 static int alauda_check_media(struct us_data *us) 457 { 458 struct alauda_info *info = (struct alauda_info *) us->extra; 459 unsigned char status[2]; 460 int rc; 461 462 rc = alauda_get_media_status(us, status); 463 464 /* Check for no media or door open */ 465 if ((status[0] & 0x80) || ((status[0] & 0x1F) == 0x10) 466 || ((status[1] & 0x01) == 0)) { 467 usb_stor_dbg(us, "No media, or door open\n"); 468 alauda_free_maps(&MEDIA_INFO(us)); 469 info->sense_key = 0x02; 470 info->sense_asc = 0x3A; 471 info->sense_ascq = 0x00; 472 return USB_STOR_TRANSPORT_FAILED; 473 } 474 475 /* Check for media change */ 476 if (status[0] & 0x08) { 477 usb_stor_dbg(us, "Media change detected\n"); 478 alauda_free_maps(&MEDIA_INFO(us)); 479 alauda_init_media(us); 480 481 info->sense_key = UNIT_ATTENTION; 482 info->sense_asc = 0x28; 483 info->sense_ascq = 0x00; 484 return USB_STOR_TRANSPORT_FAILED; 485 } 486 487 return USB_STOR_TRANSPORT_GOOD; 488 } 489 490 /* 491 * Checks the status from the 2nd status register 492 * Returns 3 bytes of status data, only the first is known 493 */ 494 static int alauda_check_status2(struct us_data *us) 495 { 496 int rc; 497 unsigned char command[] = { 498 ALAUDA_BULK_CMD, ALAUDA_BULK_GET_STATUS2, 499 0, 0, 0, 0, 3, 0, MEDIA_PORT(us) 500 }; 501 unsigned char data[3]; 502 503 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, 504 command, 9, NULL); 505 if (rc != USB_STOR_XFER_GOOD) 506 return rc; 507 508 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, 509 data, 3, NULL); 510 if (rc != USB_STOR_XFER_GOOD) 511 return rc; 512 513 usb_stor_dbg(us, "%3ph\n", data); 514 if (data[0] & ALAUDA_STATUS_ERROR) 515 return USB_STOR_XFER_ERROR; 516 517 return USB_STOR_XFER_GOOD; 518 } 519 520 /* 521 * Gets the redundancy data for the first page of a PBA 522 * Returns 16 bytes. 523 */ 524 static int alauda_get_redu_data(struct us_data *us, u16 pba, unsigned char *data) 525 { 526 int rc; 527 unsigned char command[] = { 528 ALAUDA_BULK_CMD, ALAUDA_BULK_GET_REDU_DATA, 529 PBA_HI(pba), PBA_ZONE(pba), 0, PBA_LO(pba), 0, 0, MEDIA_PORT(us) 530 }; 531 532 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, 533 command, 9, NULL); 534 if (rc != USB_STOR_XFER_GOOD) 535 return rc; 536 537 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, 538 data, 16, NULL); 539 } 540 541 /* 542 * Finds the first unused PBA in a zone 543 * Returns the absolute PBA of an unused PBA, or 0 if none found. 544 */ 545 static u16 alauda_find_unused_pba(struct alauda_media_info *info, 546 unsigned int zone) 547 { 548 u16 *pba_to_lba = info->pba_to_lba[zone]; 549 unsigned int i; 550 551 for (i = 0; i < info->zonesize; i++) 552 if (pba_to_lba[i] == UNDEF) 553 return (zone << info->zoneshift) + i; 554 555 return 0; 556 } 557 558 /* 559 * Reads the redundancy data for all PBA's in a zone 560 * Produces lba <--> pba mappings 561 */ 562 static int alauda_read_map(struct us_data *us, unsigned int zone) 563 { 564 unsigned char *data = us->iobuf; 565 int result; 566 int i, j; 567 unsigned int zonesize = MEDIA_INFO(us).zonesize; 568 unsigned int uzonesize = MEDIA_INFO(us).uzonesize; 569 unsigned int lba_offset, lba_real, blocknum; 570 unsigned int zone_base_lba = zone * uzonesize; 571 unsigned int zone_base_pba = zone * zonesize; 572 u16 *lba_to_pba = kcalloc(zonesize, sizeof(u16), GFP_NOIO); 573 u16 *pba_to_lba = kcalloc(zonesize, sizeof(u16), GFP_NOIO); 574 if (lba_to_pba == NULL || pba_to_lba == NULL) { 575 result = USB_STOR_TRANSPORT_ERROR; 576 goto error; 577 } 578 579 usb_stor_dbg(us, "Mapping blocks for zone %d\n", zone); 580 581 /* 1024 PBA's per zone */ 582 for (i = 0; i < zonesize; i++) 583 lba_to_pba[i] = pba_to_lba[i] = UNDEF; 584 585 for (i = 0; i < zonesize; i++) { 586 blocknum = zone_base_pba + i; 587 588 result = alauda_get_redu_data(us, blocknum, data); 589 if (result != USB_STOR_XFER_GOOD) { 590 result = USB_STOR_TRANSPORT_ERROR; 591 goto error; 592 } 593 594 /* special PBAs have control field 0^16 */ 595 for (j = 0; j < 16; j++) 596 if (data[j] != 0) 597 goto nonz; 598 pba_to_lba[i] = UNUSABLE; 599 usb_stor_dbg(us, "PBA %d has no logical mapping\n", blocknum); 600 continue; 601 602 nonz: 603 /* unwritten PBAs have control field FF^16 */ 604 for (j = 0; j < 16; j++) 605 if (data[j] != 0xff) 606 goto nonff; 607 continue; 608 609 nonff: 610 /* normal PBAs start with six FFs */ 611 if (j < 6) { 612 usb_stor_dbg(us, "PBA %d has no logical mapping: reserved area = %02X%02X%02X%02X data status %02X block status %02X\n", 613 blocknum, 614 data[0], data[1], data[2], data[3], 615 data[4], data[5]); 616 pba_to_lba[i] = UNUSABLE; 617 continue; 618 } 619 620 if ((data[6] >> 4) != 0x01) { 621 usb_stor_dbg(us, "PBA %d has invalid address field %02X%02X/%02X%02X\n", 622 blocknum, data[6], data[7], 623 data[11], data[12]); 624 pba_to_lba[i] = UNUSABLE; 625 continue; 626 } 627 628 /* check even parity */ 629 if (parity[data[6] ^ data[7]]) { 630 printk(KERN_WARNING 631 "alauda_read_map: Bad parity in LBA for block %d" 632 " (%02X %02X)\n", i, data[6], data[7]); 633 pba_to_lba[i] = UNUSABLE; 634 continue; 635 } 636 637 lba_offset = short_pack(data[7], data[6]); 638 lba_offset = (lba_offset & 0x07FF) >> 1; 639 lba_real = lba_offset + zone_base_lba; 640 641 /* 642 * Every 1024 physical blocks ("zone"), the LBA numbers 643 * go back to zero, but are within a higher block of LBA's. 644 * Also, there is a maximum of 1000 LBA's per zone. 645 * In other words, in PBA 1024-2047 you will find LBA 0-999 646 * which are really LBA 1000-1999. This allows for 24 bad 647 * or special physical blocks per zone. 648 */ 649 650 if (lba_offset >= uzonesize) { 651 printk(KERN_WARNING 652 "alauda_read_map: Bad low LBA %d for block %d\n", 653 lba_real, blocknum); 654 continue; 655 } 656 657 if (lba_to_pba[lba_offset] != UNDEF) { 658 printk(KERN_WARNING 659 "alauda_read_map: " 660 "LBA %d seen for PBA %d and %d\n", 661 lba_real, lba_to_pba[lba_offset], blocknum); 662 continue; 663 } 664 665 pba_to_lba[i] = lba_real; 666 lba_to_pba[lba_offset] = blocknum; 667 continue; 668 } 669 670 MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba; 671 MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba; 672 result = 0; 673 goto out; 674 675 error: 676 kfree(lba_to_pba); 677 kfree(pba_to_lba); 678 out: 679 return result; 680 } 681 682 /* 683 * Checks to see whether we have already mapped a certain zone 684 * If we haven't, the map is generated 685 */ 686 static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone) 687 { 688 if (MEDIA_INFO(us).lba_to_pba[zone] == NULL 689 || MEDIA_INFO(us).pba_to_lba[zone] == NULL) 690 alauda_read_map(us, zone); 691 } 692 693 /* 694 * Erases an entire block 695 */ 696 static int alauda_erase_block(struct us_data *us, u16 pba) 697 { 698 int rc; 699 unsigned char command[] = { 700 ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba), 701 PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us) 702 }; 703 unsigned char buf[2]; 704 705 usb_stor_dbg(us, "Erasing PBA %d\n", pba); 706 707 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, 708 command, 9, NULL); 709 if (rc != USB_STOR_XFER_GOOD) 710 return rc; 711 712 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, 713 buf, 2, NULL); 714 if (rc != USB_STOR_XFER_GOOD) 715 return rc; 716 717 usb_stor_dbg(us, "Erase result: %02X %02X\n", buf[0], buf[1]); 718 return rc; 719 } 720 721 /* 722 * Reads data from a certain offset page inside a PBA, including interleaved 723 * redundancy data. Returns (pagesize+64)*pages bytes in data. 724 */ 725 static int alauda_read_block_raw(struct us_data *us, u16 pba, 726 unsigned int page, unsigned int pages, unsigned char *data) 727 { 728 int rc; 729 unsigned char command[] = { 730 ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba), 731 PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us) 732 }; 733 734 usb_stor_dbg(us, "pba %d page %d count %d\n", pba, page, pages); 735 736 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, 737 command, 9, NULL); 738 if (rc != USB_STOR_XFER_GOOD) 739 return rc; 740 741 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, 742 data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL); 743 } 744 745 /* 746 * Reads data from a certain offset page inside a PBA, excluding redundancy 747 * data. Returns pagesize*pages bytes in data. Note that data must be big enough 748 * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra' 749 * trailing bytes outside this function. 750 */ 751 static int alauda_read_block(struct us_data *us, u16 pba, 752 unsigned int page, unsigned int pages, unsigned char *data) 753 { 754 int i, rc; 755 unsigned int pagesize = MEDIA_INFO(us).pagesize; 756 757 rc = alauda_read_block_raw(us, pba, page, pages, data); 758 if (rc != USB_STOR_XFER_GOOD) 759 return rc; 760 761 /* Cut out the redundancy data */ 762 for (i = 0; i < pages; i++) { 763 int dest_offset = i * pagesize; 764 int src_offset = i * (pagesize + 64); 765 memmove(data + dest_offset, data + src_offset, pagesize); 766 } 767 768 return rc; 769 } 770 771 /* 772 * Writes an entire block of data and checks status after write. 773 * Redundancy data must be already included in data. Data should be 774 * (pagesize+64)*blocksize bytes in length. 775 */ 776 static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data) 777 { 778 int rc; 779 struct alauda_info *info = (struct alauda_info *) us->extra; 780 unsigned char command[] = { 781 ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba), 782 PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us) 783 }; 784 785 usb_stor_dbg(us, "pba %d\n", pba); 786 787 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, 788 command, 9, NULL); 789 if (rc != USB_STOR_XFER_GOOD) 790 return rc; 791 792 rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data, 793 (MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize, 794 NULL); 795 if (rc != USB_STOR_XFER_GOOD) 796 return rc; 797 798 return alauda_check_status2(us); 799 } 800 801 /* 802 * Write some data to a specific LBA. 803 */ 804 static int alauda_write_lba(struct us_data *us, u16 lba, 805 unsigned int page, unsigned int pages, 806 unsigned char *ptr, unsigned char *blockbuffer) 807 { 808 u16 pba, lbap, new_pba; 809 unsigned char *bptr, *cptr, *xptr; 810 unsigned char ecc[3]; 811 int i, result; 812 unsigned int uzonesize = MEDIA_INFO(us).uzonesize; 813 unsigned int zonesize = MEDIA_INFO(us).zonesize; 814 unsigned int pagesize = MEDIA_INFO(us).pagesize; 815 unsigned int blocksize = MEDIA_INFO(us).blocksize; 816 unsigned int lba_offset = lba % uzonesize; 817 unsigned int new_pba_offset; 818 unsigned int zone = lba / uzonesize; 819 820 alauda_ensure_map_for_zone(us, zone); 821 822 pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset]; 823 if (pba == 1) { 824 /* Maybe it is impossible to write to PBA 1. 825 Fake success, but don't do anything. */ 826 printk(KERN_WARNING 827 "alauda_write_lba: avoid writing to pba 1\n"); 828 return USB_STOR_TRANSPORT_GOOD; 829 } 830 831 new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone); 832 if (!new_pba) { 833 printk(KERN_WARNING 834 "alauda_write_lba: Out of unused blocks\n"); 835 return USB_STOR_TRANSPORT_ERROR; 836 } 837 838 /* read old contents */ 839 if (pba != UNDEF) { 840 result = alauda_read_block_raw(us, pba, 0, 841 blocksize, blockbuffer); 842 if (result != USB_STOR_XFER_GOOD) 843 return result; 844 } else { 845 memset(blockbuffer, 0, blocksize * (pagesize + 64)); 846 } 847 848 lbap = (lba_offset << 1) | 0x1000; 849 if (parity[MSB_of(lbap) ^ LSB_of(lbap)]) 850 lbap ^= 1; 851 852 /* check old contents and fill lba */ 853 for (i = 0; i < blocksize; i++) { 854 bptr = blockbuffer + (i * (pagesize + 64)); 855 cptr = bptr + pagesize; 856 nand_compute_ecc(bptr, ecc); 857 if (!nand_compare_ecc(cptr+13, ecc)) { 858 usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n", 859 i, pba); 860 nand_store_ecc(cptr+13, ecc); 861 } 862 nand_compute_ecc(bptr + (pagesize / 2), ecc); 863 if (!nand_compare_ecc(cptr+8, ecc)) { 864 usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n", 865 i, pba); 866 nand_store_ecc(cptr+8, ecc); 867 } 868 cptr[6] = cptr[11] = MSB_of(lbap); 869 cptr[7] = cptr[12] = LSB_of(lbap); 870 } 871 872 /* copy in new stuff and compute ECC */ 873 xptr = ptr; 874 for (i = page; i < page+pages; i++) { 875 bptr = blockbuffer + (i * (pagesize + 64)); 876 cptr = bptr + pagesize; 877 memcpy(bptr, xptr, pagesize); 878 xptr += pagesize; 879 nand_compute_ecc(bptr, ecc); 880 nand_store_ecc(cptr+13, ecc); 881 nand_compute_ecc(bptr + (pagesize / 2), ecc); 882 nand_store_ecc(cptr+8, ecc); 883 } 884 885 result = alauda_write_block(us, new_pba, blockbuffer); 886 if (result != USB_STOR_XFER_GOOD) 887 return result; 888 889 new_pba_offset = new_pba - (zone * zonesize); 890 MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba; 891 MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba; 892 usb_stor_dbg(us, "Remapped LBA %d to PBA %d\n", lba, new_pba); 893 894 if (pba != UNDEF) { 895 unsigned int pba_offset = pba - (zone * zonesize); 896 result = alauda_erase_block(us, pba); 897 if (result != USB_STOR_XFER_GOOD) 898 return result; 899 MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF; 900 } 901 902 return USB_STOR_TRANSPORT_GOOD; 903 } 904 905 /* 906 * Read data from a specific sector address 907 */ 908 static int alauda_read_data(struct us_data *us, unsigned long address, 909 unsigned int sectors) 910 { 911 unsigned char *buffer; 912 u16 lba, max_lba; 913 unsigned int page, len, offset; 914 unsigned int blockshift = MEDIA_INFO(us).blockshift; 915 unsigned int pageshift = MEDIA_INFO(us).pageshift; 916 unsigned int blocksize = MEDIA_INFO(us).blocksize; 917 unsigned int pagesize = MEDIA_INFO(us).pagesize; 918 unsigned int uzonesize = MEDIA_INFO(us).uzonesize; 919 struct scatterlist *sg; 920 int result; 921 922 /* 923 * Since we only read in one block at a time, we have to create 924 * a bounce buffer and move the data a piece at a time between the 925 * bounce buffer and the actual transfer buffer. 926 * We make this buffer big enough to hold temporary redundancy data, 927 * which we use when reading the data blocks. 928 */ 929 930 len = min(sectors, blocksize) * (pagesize + 64); 931 buffer = kmalloc(len, GFP_NOIO); 932 if (buffer == NULL) { 933 printk(KERN_WARNING "alauda_read_data: Out of memory\n"); 934 return USB_STOR_TRANSPORT_ERROR; 935 } 936 937 /* Figure out the initial LBA and page */ 938 lba = address >> blockshift; 939 page = (address & MEDIA_INFO(us).blockmask); 940 max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift); 941 942 result = USB_STOR_TRANSPORT_GOOD; 943 offset = 0; 944 sg = NULL; 945 946 while (sectors > 0) { 947 unsigned int zone = lba / uzonesize; /* integer division */ 948 unsigned int lba_offset = lba - (zone * uzonesize); 949 unsigned int pages; 950 u16 pba; 951 alauda_ensure_map_for_zone(us, zone); 952 953 /* Not overflowing capacity? */ 954 if (lba >= max_lba) { 955 usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n", 956 lba, max_lba); 957 result = USB_STOR_TRANSPORT_ERROR; 958 break; 959 } 960 961 /* Find number of pages we can read in this block */ 962 pages = min(sectors, blocksize - page); 963 len = pages << pageshift; 964 965 /* Find where this lba lives on disk */ 966 pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset]; 967 968 if (pba == UNDEF) { /* this lba was never written */ 969 usb_stor_dbg(us, "Read %d zero pages (LBA %d) page %d\n", 970 pages, lba, page); 971 972 /* This is not really an error. It just means 973 that the block has never been written. 974 Instead of returning USB_STOR_TRANSPORT_ERROR 975 it is better to return all zero data. */ 976 977 memset(buffer, 0, len); 978 } else { 979 usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n", 980 pages, pba, lba, page); 981 982 result = alauda_read_block(us, pba, page, pages, buffer); 983 if (result != USB_STOR_TRANSPORT_GOOD) 984 break; 985 } 986 987 /* Store the data in the transfer buffer */ 988 usb_stor_access_xfer_buf(buffer, len, us->srb, 989 &sg, &offset, TO_XFER_BUF); 990 991 page = 0; 992 lba++; 993 sectors -= pages; 994 } 995 996 kfree(buffer); 997 return result; 998 } 999 1000 /* 1001 * Write data to a specific sector address 1002 */ 1003 static int alauda_write_data(struct us_data *us, unsigned long address, 1004 unsigned int sectors) 1005 { 1006 unsigned char *buffer, *blockbuffer; 1007 unsigned int page, len, offset; 1008 unsigned int blockshift = MEDIA_INFO(us).blockshift; 1009 unsigned int pageshift = MEDIA_INFO(us).pageshift; 1010 unsigned int blocksize = MEDIA_INFO(us).blocksize; 1011 unsigned int pagesize = MEDIA_INFO(us).pagesize; 1012 struct scatterlist *sg; 1013 u16 lba, max_lba; 1014 int result; 1015 1016 /* 1017 * Since we don't write the user data directly to the device, 1018 * we have to create a bounce buffer and move the data a piece 1019 * at a time between the bounce buffer and the actual transfer buffer. 1020 */ 1021 1022 len = min(sectors, blocksize) * pagesize; 1023 buffer = kmalloc(len, GFP_NOIO); 1024 if (buffer == NULL) { 1025 printk(KERN_WARNING "alauda_write_data: Out of memory\n"); 1026 return USB_STOR_TRANSPORT_ERROR; 1027 } 1028 1029 /* 1030 * We also need a temporary block buffer, where we read in the old data, 1031 * overwrite parts with the new data, and manipulate the redundancy data 1032 */ 1033 blockbuffer = kmalloc((pagesize + 64) * blocksize, GFP_NOIO); 1034 if (blockbuffer == NULL) { 1035 printk(KERN_WARNING "alauda_write_data: Out of memory\n"); 1036 kfree(buffer); 1037 return USB_STOR_TRANSPORT_ERROR; 1038 } 1039 1040 /* Figure out the initial LBA and page */ 1041 lba = address >> blockshift; 1042 page = (address & MEDIA_INFO(us).blockmask); 1043 max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift); 1044 1045 result = USB_STOR_TRANSPORT_GOOD; 1046 offset = 0; 1047 sg = NULL; 1048 1049 while (sectors > 0) { 1050 /* Write as many sectors as possible in this block */ 1051 unsigned int pages = min(sectors, blocksize - page); 1052 len = pages << pageshift; 1053 1054 /* Not overflowing capacity? */ 1055 if (lba >= max_lba) { 1056 usb_stor_dbg(us, "Requested lba %u exceeds maximum %u\n", 1057 lba, max_lba); 1058 result = USB_STOR_TRANSPORT_ERROR; 1059 break; 1060 } 1061 1062 /* Get the data from the transfer buffer */ 1063 usb_stor_access_xfer_buf(buffer, len, us->srb, 1064 &sg, &offset, FROM_XFER_BUF); 1065 1066 result = alauda_write_lba(us, lba, page, pages, buffer, 1067 blockbuffer); 1068 if (result != USB_STOR_TRANSPORT_GOOD) 1069 break; 1070 1071 page = 0; 1072 lba++; 1073 sectors -= pages; 1074 } 1075 1076 kfree(buffer); 1077 kfree(blockbuffer); 1078 return result; 1079 } 1080 1081 /* 1082 * Our interface with the rest of the world 1083 */ 1084 1085 static void alauda_info_destructor(void *extra) 1086 { 1087 struct alauda_info *info = (struct alauda_info *) extra; 1088 int port; 1089 1090 if (!info) 1091 return; 1092 1093 for (port = 0; port < 2; port++) { 1094 struct alauda_media_info *media_info = &info->port[port]; 1095 1096 alauda_free_maps(media_info); 1097 kfree(media_info->lba_to_pba); 1098 kfree(media_info->pba_to_lba); 1099 } 1100 } 1101 1102 /* 1103 * Initialize alauda_info struct and find the data-write endpoint 1104 */ 1105 static int init_alauda(struct us_data *us) 1106 { 1107 struct alauda_info *info; 1108 struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting; 1109 nand_init_ecc(); 1110 1111 us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO); 1112 if (!us->extra) 1113 return USB_STOR_TRANSPORT_ERROR; 1114 1115 info = (struct alauda_info *) us->extra; 1116 us->extra_destructor = alauda_info_destructor; 1117 1118 info->wr_ep = usb_sndbulkpipe(us->pusb_dev, 1119 altsetting->endpoint[0].desc.bEndpointAddress 1120 & USB_ENDPOINT_NUMBER_MASK); 1121 1122 return USB_STOR_TRANSPORT_GOOD; 1123 } 1124 1125 static int alauda_transport(struct scsi_cmnd *srb, struct us_data *us) 1126 { 1127 int rc; 1128 struct alauda_info *info = (struct alauda_info *) us->extra; 1129 unsigned char *ptr = us->iobuf; 1130 static unsigned char inquiry_response[36] = { 1131 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00 1132 }; 1133 1134 if (srb->cmnd[0] == INQUIRY) { 1135 usb_stor_dbg(us, "INQUIRY - Returning bogus response\n"); 1136 memcpy(ptr, inquiry_response, sizeof(inquiry_response)); 1137 fill_inquiry_response(us, ptr, 36); 1138 return USB_STOR_TRANSPORT_GOOD; 1139 } 1140 1141 if (srb->cmnd[0] == TEST_UNIT_READY) { 1142 usb_stor_dbg(us, "TEST_UNIT_READY\n"); 1143 return alauda_check_media(us); 1144 } 1145 1146 if (srb->cmnd[0] == READ_CAPACITY) { 1147 unsigned int num_zones; 1148 unsigned long capacity; 1149 1150 rc = alauda_check_media(us); 1151 if (rc != USB_STOR_TRANSPORT_GOOD) 1152 return rc; 1153 1154 num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift 1155 + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift); 1156 1157 capacity = num_zones * MEDIA_INFO(us).uzonesize 1158 * MEDIA_INFO(us).blocksize; 1159 1160 /* Report capacity and page size */ 1161 ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1); 1162 ((__be32 *) ptr)[1] = cpu_to_be32(512); 1163 1164 usb_stor_set_xfer_buf(ptr, 8, srb); 1165 return USB_STOR_TRANSPORT_GOOD; 1166 } 1167 1168 if (srb->cmnd[0] == READ_10) { 1169 unsigned int page, pages; 1170 1171 rc = alauda_check_media(us); 1172 if (rc != USB_STOR_TRANSPORT_GOOD) 1173 return rc; 1174 1175 page = short_pack(srb->cmnd[3], srb->cmnd[2]); 1176 page <<= 16; 1177 page |= short_pack(srb->cmnd[5], srb->cmnd[4]); 1178 pages = short_pack(srb->cmnd[8], srb->cmnd[7]); 1179 1180 usb_stor_dbg(us, "READ_10: page %d pagect %d\n", page, pages); 1181 1182 return alauda_read_data(us, page, pages); 1183 } 1184 1185 if (srb->cmnd[0] == WRITE_10) { 1186 unsigned int page, pages; 1187 1188 rc = alauda_check_media(us); 1189 if (rc != USB_STOR_TRANSPORT_GOOD) 1190 return rc; 1191 1192 page = short_pack(srb->cmnd[3], srb->cmnd[2]); 1193 page <<= 16; 1194 page |= short_pack(srb->cmnd[5], srb->cmnd[4]); 1195 pages = short_pack(srb->cmnd[8], srb->cmnd[7]); 1196 1197 usb_stor_dbg(us, "WRITE_10: page %d pagect %d\n", page, pages); 1198 1199 return alauda_write_data(us, page, pages); 1200 } 1201 1202 if (srb->cmnd[0] == REQUEST_SENSE) { 1203 usb_stor_dbg(us, "REQUEST_SENSE\n"); 1204 1205 memset(ptr, 0, 18); 1206 ptr[0] = 0xF0; 1207 ptr[2] = info->sense_key; 1208 ptr[7] = 11; 1209 ptr[12] = info->sense_asc; 1210 ptr[13] = info->sense_ascq; 1211 usb_stor_set_xfer_buf(ptr, 18, srb); 1212 1213 return USB_STOR_TRANSPORT_GOOD; 1214 } 1215 1216 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) { 1217 /* sure. whatever. not like we can stop the user from popping 1218 the media out of the device (no locking doors, etc) */ 1219 return USB_STOR_TRANSPORT_GOOD; 1220 } 1221 1222 usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n", 1223 srb->cmnd[0], srb->cmnd[0]); 1224 info->sense_key = 0x05; 1225 info->sense_asc = 0x20; 1226 info->sense_ascq = 0x00; 1227 return USB_STOR_TRANSPORT_FAILED; 1228 } 1229 1230 static int alauda_probe(struct usb_interface *intf, 1231 const struct usb_device_id *id) 1232 { 1233 struct us_data *us; 1234 int result; 1235 1236 result = usb_stor_probe1(&us, intf, id, 1237 (id - alauda_usb_ids) + alauda_unusual_dev_list); 1238 if (result) 1239 return result; 1240 1241 us->transport_name = "Alauda Control/Bulk"; 1242 us->transport = alauda_transport; 1243 us->transport_reset = usb_stor_Bulk_reset; 1244 us->max_lun = 1; 1245 1246 result = usb_stor_probe2(us); 1247 return result; 1248 } 1249 1250 static struct usb_driver alauda_driver = { 1251 .name = "ums-alauda", 1252 .probe = alauda_probe, 1253 .disconnect = usb_stor_disconnect, 1254 .suspend = usb_stor_suspend, 1255 .resume = usb_stor_resume, 1256 .reset_resume = usb_stor_reset_resume, 1257 .pre_reset = usb_stor_pre_reset, 1258 .post_reset = usb_stor_post_reset, 1259 .id_table = alauda_usb_ids, 1260 .soft_unbind = 1, 1261 .no_dynamic_id = 1, 1262 }; 1263 1264 module_usb_driver(alauda_driver); 1265