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