1 /* Driver for Datafab USB Compact Flash reader 2 * 3 * datafab driver v0.1: 4 * 5 * First release 6 * 7 * Current development and maintenance by: 8 * (c) 2000 Jimmie Mayfield (mayfield+datafab@sackheads.org) 9 * 10 * Many thanks to Robert Baruch for the SanDisk SmartMedia reader driver 11 * which I used as a template for this driver. 12 * 13 * Some bugfixes and scatter-gather code by Gregory P. Smith 14 * (greg-usb@electricrain.com) 15 * 16 * Fix for media change by Joerg Schneider (js@joergschneider.com) 17 * 18 * Other contributors: 19 * (c) 2002 Alan Stern <stern@rowland.org> 20 * 21 * This program is free software; you can redistribute it and/or modify it 22 * under the terms of the GNU General Public License as published by the 23 * Free Software Foundation; either version 2, or (at your option) any 24 * later version. 25 * 26 * This program is distributed in the hope that it will be useful, but 27 * WITHOUT ANY WARRANTY; without even the implied warranty of 28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 29 * General Public License for more details. 30 * 31 * You should have received a copy of the GNU General Public License along 32 * with this program; if not, write to the Free Software Foundation, Inc., 33 * 675 Mass Ave, Cambridge, MA 02139, USA. 34 */ 35 36 /* 37 * This driver attempts to support USB CompactFlash reader/writer devices 38 * based on Datafab USB-to-ATA chips. It was specifically developed for the 39 * Datafab MDCFE-B USB CompactFlash reader but has since been found to work 40 * with a variety of Datafab-based devices from a number of manufacturers. 41 * I've received a report of this driver working with a Datafab-based 42 * SmartMedia device though please be aware that I'm personally unable to 43 * test SmartMedia support. 44 * 45 * This driver supports reading and writing. If you're truly paranoid, 46 * however, you can force the driver into a write-protected state by setting 47 * the WP enable bits in datafab_handle_mode_sense(). See the comments 48 * in that routine. 49 */ 50 51 #include <linux/errno.h> 52 #include <linux/slab.h> 53 54 #include <scsi/scsi.h> 55 #include <scsi/scsi_cmnd.h> 56 57 #include "usb.h" 58 #include "transport.h" 59 #include "protocol.h" 60 #include "debug.h" 61 #include "datafab.h" 62 63 static int datafab_determine_lun(struct us_data *us, 64 struct datafab_info *info); 65 66 67 static inline int 68 datafab_bulk_read(struct us_data *us, unsigned char *data, unsigned int len) { 69 if (len == 0) 70 return USB_STOR_XFER_GOOD; 71 72 US_DEBUGP("datafab_bulk_read: len = %d\n", len); 73 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, 74 data, len, NULL); 75 } 76 77 78 static inline int 79 datafab_bulk_write(struct us_data *us, unsigned char *data, unsigned int len) { 80 if (len == 0) 81 return USB_STOR_XFER_GOOD; 82 83 US_DEBUGP("datafab_bulk_write: len = %d\n", len); 84 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, 85 data, len, NULL); 86 } 87 88 89 static int datafab_read_data(struct us_data *us, 90 struct datafab_info *info, 91 u32 sector, 92 u32 sectors) 93 { 94 unsigned char *command = us->iobuf; 95 unsigned char *buffer; 96 unsigned char thistime; 97 unsigned int totallen, alloclen; 98 int len, result; 99 unsigned int sg_offset = 0; 100 struct scatterlist *sg = NULL; 101 102 // we're working in LBA mode. according to the ATA spec, 103 // we can support up to 28-bit addressing. I don't know if Datafab 104 // supports beyond 24-bit addressing. It's kind of hard to test 105 // since it requires > 8GB CF card. 106 // 107 if (sectors > 0x0FFFFFFF) 108 return USB_STOR_TRANSPORT_ERROR; 109 110 if (info->lun == -1) { 111 result = datafab_determine_lun(us, info); 112 if (result != USB_STOR_TRANSPORT_GOOD) 113 return result; 114 } 115 116 totallen = sectors * info->ssize; 117 118 // Since we don't read more than 64 KB at a time, we have to create 119 // a bounce buffer and move the data a piece at a time between the 120 // bounce buffer and the actual transfer buffer. 121 122 alloclen = min(totallen, 65536u); 123 buffer = kmalloc(alloclen, GFP_NOIO); 124 if (buffer == NULL) 125 return USB_STOR_TRANSPORT_ERROR; 126 127 do { 128 // loop, never allocate or transfer more than 64k at once 129 // (min(128k, 255*info->ssize) is the real limit) 130 131 len = min(totallen, alloclen); 132 thistime = (len / info->ssize) & 0xff; 133 134 command[0] = 0; 135 command[1] = thistime; 136 command[2] = sector & 0xFF; 137 command[3] = (sector >> 8) & 0xFF; 138 command[4] = (sector >> 16) & 0xFF; 139 140 command[5] = 0xE0 + (info->lun << 4); 141 command[5] |= (sector >> 24) & 0x0F; 142 command[6] = 0x20; 143 command[7] = 0x01; 144 145 // send the read command 146 result = datafab_bulk_write(us, command, 8); 147 if (result != USB_STOR_XFER_GOOD) 148 goto leave; 149 150 // read the result 151 result = datafab_bulk_read(us, buffer, len); 152 if (result != USB_STOR_XFER_GOOD) 153 goto leave; 154 155 // Store the data in the transfer buffer 156 usb_stor_access_xfer_buf(buffer, len, us->srb, 157 &sg, &sg_offset, TO_XFER_BUF); 158 159 sector += thistime; 160 totallen -= len; 161 } while (totallen > 0); 162 163 kfree(buffer); 164 return USB_STOR_TRANSPORT_GOOD; 165 166 leave: 167 kfree(buffer); 168 return USB_STOR_TRANSPORT_ERROR; 169 } 170 171 172 static int datafab_write_data(struct us_data *us, 173 struct datafab_info *info, 174 u32 sector, 175 u32 sectors) 176 { 177 unsigned char *command = us->iobuf; 178 unsigned char *reply = us->iobuf; 179 unsigned char *buffer; 180 unsigned char thistime; 181 unsigned int totallen, alloclen; 182 int len, result; 183 unsigned int sg_offset = 0; 184 struct scatterlist *sg = NULL; 185 186 // we're working in LBA mode. according to the ATA spec, 187 // we can support up to 28-bit addressing. I don't know if Datafab 188 // supports beyond 24-bit addressing. It's kind of hard to test 189 // since it requires > 8GB CF card. 190 // 191 if (sectors > 0x0FFFFFFF) 192 return USB_STOR_TRANSPORT_ERROR; 193 194 if (info->lun == -1) { 195 result = datafab_determine_lun(us, info); 196 if (result != USB_STOR_TRANSPORT_GOOD) 197 return result; 198 } 199 200 totallen = sectors * info->ssize; 201 202 // Since we don't write more than 64 KB at a time, we have to create 203 // a bounce buffer and move the data a piece at a time between the 204 // bounce buffer and the actual transfer buffer. 205 206 alloclen = min(totallen, 65536u); 207 buffer = kmalloc(alloclen, GFP_NOIO); 208 if (buffer == NULL) 209 return USB_STOR_TRANSPORT_ERROR; 210 211 do { 212 // loop, never allocate or transfer more than 64k at once 213 // (min(128k, 255*info->ssize) is the real limit) 214 215 len = min(totallen, alloclen); 216 thistime = (len / info->ssize) & 0xff; 217 218 // Get the data from the transfer buffer 219 usb_stor_access_xfer_buf(buffer, len, us->srb, 220 &sg, &sg_offset, FROM_XFER_BUF); 221 222 command[0] = 0; 223 command[1] = thistime; 224 command[2] = sector & 0xFF; 225 command[3] = (sector >> 8) & 0xFF; 226 command[4] = (sector >> 16) & 0xFF; 227 228 command[5] = 0xE0 + (info->lun << 4); 229 command[5] |= (sector >> 24) & 0x0F; 230 command[6] = 0x30; 231 command[7] = 0x02; 232 233 // send the command 234 result = datafab_bulk_write(us, command, 8); 235 if (result != USB_STOR_XFER_GOOD) 236 goto leave; 237 238 // send the data 239 result = datafab_bulk_write(us, buffer, len); 240 if (result != USB_STOR_XFER_GOOD) 241 goto leave; 242 243 // read the result 244 result = datafab_bulk_read(us, reply, 2); 245 if (result != USB_STOR_XFER_GOOD) 246 goto leave; 247 248 if (reply[0] != 0x50 && reply[1] != 0) { 249 US_DEBUGP("datafab_write_data: Gah! " 250 "write return code: %02x %02x\n", 251 reply[0], reply[1]); 252 result = USB_STOR_TRANSPORT_ERROR; 253 goto leave; 254 } 255 256 sector += thistime; 257 totallen -= len; 258 } while (totallen > 0); 259 260 kfree(buffer); 261 return USB_STOR_TRANSPORT_GOOD; 262 263 leave: 264 kfree(buffer); 265 return USB_STOR_TRANSPORT_ERROR; 266 } 267 268 269 static int datafab_determine_lun(struct us_data *us, 270 struct datafab_info *info) 271 { 272 // Dual-slot readers can be thought of as dual-LUN devices. 273 // We need to determine which card slot is being used. 274 // We'll send an IDENTIFY DEVICE command and see which LUN responds... 275 // 276 // There might be a better way of doing this? 277 278 static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 }; 279 unsigned char *command = us->iobuf; 280 unsigned char *buf; 281 int count = 0, rc; 282 283 if (!us || !info) 284 return USB_STOR_TRANSPORT_ERROR; 285 286 memcpy(command, scommand, 8); 287 buf = kmalloc(512, GFP_NOIO); 288 if (!buf) 289 return USB_STOR_TRANSPORT_ERROR; 290 291 US_DEBUGP("datafab_determine_lun: locating...\n"); 292 293 // we'll try 3 times before giving up... 294 // 295 while (count++ < 3) { 296 command[5] = 0xa0; 297 298 rc = datafab_bulk_write(us, command, 8); 299 if (rc != USB_STOR_XFER_GOOD) { 300 rc = USB_STOR_TRANSPORT_ERROR; 301 goto leave; 302 } 303 304 rc = datafab_bulk_read(us, buf, 512); 305 if (rc == USB_STOR_XFER_GOOD) { 306 info->lun = 0; 307 rc = USB_STOR_TRANSPORT_GOOD; 308 goto leave; 309 } 310 311 command[5] = 0xb0; 312 313 rc = datafab_bulk_write(us, command, 8); 314 if (rc != USB_STOR_XFER_GOOD) { 315 rc = USB_STOR_TRANSPORT_ERROR; 316 goto leave; 317 } 318 319 rc = datafab_bulk_read(us, buf, 512); 320 if (rc == USB_STOR_XFER_GOOD) { 321 info->lun = 1; 322 rc = USB_STOR_TRANSPORT_GOOD; 323 goto leave; 324 } 325 326 msleep(20); 327 } 328 329 rc = USB_STOR_TRANSPORT_ERROR; 330 331 leave: 332 kfree(buf); 333 return rc; 334 } 335 336 static int datafab_id_device(struct us_data *us, 337 struct datafab_info *info) 338 { 339 // this is a variation of the ATA "IDENTIFY DEVICE" command...according 340 // to the ATA spec, 'Sector Count' isn't used but the Windows driver 341 // sets this bit so we do too... 342 // 343 static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 }; 344 unsigned char *command = us->iobuf; 345 unsigned char *reply; 346 int rc; 347 348 if (!us || !info) 349 return USB_STOR_TRANSPORT_ERROR; 350 351 if (info->lun == -1) { 352 rc = datafab_determine_lun(us, info); 353 if (rc != USB_STOR_TRANSPORT_GOOD) 354 return rc; 355 } 356 357 memcpy(command, scommand, 8); 358 reply = kmalloc(512, GFP_NOIO); 359 if (!reply) 360 return USB_STOR_TRANSPORT_ERROR; 361 362 command[5] += (info->lun << 4); 363 364 rc = datafab_bulk_write(us, command, 8); 365 if (rc != USB_STOR_XFER_GOOD) { 366 rc = USB_STOR_TRANSPORT_ERROR; 367 goto leave; 368 } 369 370 // we'll go ahead and extract the media capacity while we're here... 371 // 372 rc = datafab_bulk_read(us, reply, 512); 373 if (rc == USB_STOR_XFER_GOOD) { 374 // capacity is at word offset 57-58 375 // 376 info->sectors = ((u32)(reply[117]) << 24) | 377 ((u32)(reply[116]) << 16) | 378 ((u32)(reply[115]) << 8) | 379 ((u32)(reply[114]) ); 380 rc = USB_STOR_TRANSPORT_GOOD; 381 goto leave; 382 } 383 384 rc = USB_STOR_TRANSPORT_ERROR; 385 386 leave: 387 kfree(reply); 388 return rc; 389 } 390 391 392 static int datafab_handle_mode_sense(struct us_data *us, 393 struct scsi_cmnd * srb, 394 int sense_6) 395 { 396 static unsigned char rw_err_page[12] = { 397 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0 398 }; 399 static unsigned char cache_page[12] = { 400 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0 401 }; 402 static unsigned char rbac_page[12] = { 403 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0 404 }; 405 static unsigned char timer_page[8] = { 406 0x1C, 0x6, 0, 0, 0, 0 407 }; 408 unsigned char pc, page_code; 409 unsigned int i = 0; 410 struct datafab_info *info = (struct datafab_info *) (us->extra); 411 unsigned char *ptr = us->iobuf; 412 413 // most of this stuff is just a hack to get things working. the 414 // datafab reader doesn't present a SCSI interface so we 415 // fudge the SCSI commands... 416 // 417 418 pc = srb->cmnd[2] >> 6; 419 page_code = srb->cmnd[2] & 0x3F; 420 421 switch (pc) { 422 case 0x0: 423 US_DEBUGP("datafab_handle_mode_sense: Current values\n"); 424 break; 425 case 0x1: 426 US_DEBUGP("datafab_handle_mode_sense: Changeable values\n"); 427 break; 428 case 0x2: 429 US_DEBUGP("datafab_handle_mode_sense: Default values\n"); 430 break; 431 case 0x3: 432 US_DEBUGP("datafab_handle_mode_sense: Saves values\n"); 433 break; 434 } 435 436 memset(ptr, 0, 8); 437 if (sense_6) { 438 ptr[2] = 0x00; // WP enable: 0x80 439 i = 4; 440 } else { 441 ptr[3] = 0x00; // WP enable: 0x80 442 i = 8; 443 } 444 445 switch (page_code) { 446 default: 447 // vendor-specific mode 448 info->sense_key = 0x05; 449 info->sense_asc = 0x24; 450 info->sense_ascq = 0x00; 451 return USB_STOR_TRANSPORT_FAILED; 452 453 case 0x1: 454 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page)); 455 i += sizeof(rw_err_page); 456 break; 457 458 case 0x8: 459 memcpy(ptr + i, cache_page, sizeof(cache_page)); 460 i += sizeof(cache_page); 461 break; 462 463 case 0x1B: 464 memcpy(ptr + i, rbac_page, sizeof(rbac_page)); 465 i += sizeof(rbac_page); 466 break; 467 468 case 0x1C: 469 memcpy(ptr + i, timer_page, sizeof(timer_page)); 470 i += sizeof(timer_page); 471 break; 472 473 case 0x3F: // retrieve all pages 474 memcpy(ptr + i, timer_page, sizeof(timer_page)); 475 i += sizeof(timer_page); 476 memcpy(ptr + i, rbac_page, sizeof(rbac_page)); 477 i += sizeof(rbac_page); 478 memcpy(ptr + i, cache_page, sizeof(cache_page)); 479 i += sizeof(cache_page); 480 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page)); 481 i += sizeof(rw_err_page); 482 break; 483 } 484 485 if (sense_6) 486 ptr[0] = i - 1; 487 else 488 ((__be16 *) ptr)[0] = cpu_to_be16(i - 2); 489 usb_stor_set_xfer_buf(ptr, i, srb); 490 491 return USB_STOR_TRANSPORT_GOOD; 492 } 493 494 static void datafab_info_destructor(void *extra) 495 { 496 // this routine is a placeholder... 497 // currently, we don't allocate any extra memory so we're okay 498 } 499 500 501 // Transport for the Datafab MDCFE-B 502 // 503 int datafab_transport(struct scsi_cmnd * srb, struct us_data *us) 504 { 505 struct datafab_info *info; 506 int rc; 507 unsigned long block, blocks; 508 unsigned char *ptr = us->iobuf; 509 static unsigned char inquiry_reply[8] = { 510 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00 511 }; 512 513 if (!us->extra) { 514 us->extra = kzalloc(sizeof(struct datafab_info), GFP_NOIO); 515 if (!us->extra) { 516 US_DEBUGP("datafab_transport: Gah! " 517 "Can't allocate storage for Datafab info struct!\n"); 518 return USB_STOR_TRANSPORT_ERROR; 519 } 520 us->extra_destructor = datafab_info_destructor; 521 ((struct datafab_info *)us->extra)->lun = -1; 522 } 523 524 info = (struct datafab_info *) (us->extra); 525 526 if (srb->cmnd[0] == INQUIRY) { 527 US_DEBUGP("datafab_transport: INQUIRY. Returning bogus response"); 528 memcpy(ptr, inquiry_reply, sizeof(inquiry_reply)); 529 fill_inquiry_response(us, ptr, 36); 530 return USB_STOR_TRANSPORT_GOOD; 531 } 532 533 if (srb->cmnd[0] == READ_CAPACITY) { 534 info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec 535 rc = datafab_id_device(us, info); 536 if (rc != USB_STOR_TRANSPORT_GOOD) 537 return rc; 538 539 US_DEBUGP("datafab_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n", 540 info->sectors, info->ssize); 541 542 // build the reply 543 // we need the last sector, not the number of sectors 544 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1); 545 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize); 546 usb_stor_set_xfer_buf(ptr, 8, srb); 547 548 return USB_STOR_TRANSPORT_GOOD; 549 } 550 551 if (srb->cmnd[0] == MODE_SELECT_10) { 552 US_DEBUGP("datafab_transport: Gah! MODE_SELECT_10.\n"); 553 return USB_STOR_TRANSPORT_ERROR; 554 } 555 556 // don't bother implementing READ_6 or WRITE_6. 557 // 558 if (srb->cmnd[0] == READ_10) { 559 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | 560 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); 561 562 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8])); 563 564 US_DEBUGP("datafab_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks); 565 return datafab_read_data(us, info, block, blocks); 566 } 567 568 if (srb->cmnd[0] == READ_12) { 569 // we'll probably never see a READ_12 but we'll do it anyway... 570 // 571 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | 572 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); 573 574 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) | 575 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9])); 576 577 US_DEBUGP("datafab_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks); 578 return datafab_read_data(us, info, block, blocks); 579 } 580 581 if (srb->cmnd[0] == WRITE_10) { 582 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | 583 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); 584 585 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8])); 586 587 US_DEBUGP("datafab_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks); 588 return datafab_write_data(us, info, block, blocks); 589 } 590 591 if (srb->cmnd[0] == WRITE_12) { 592 // we'll probably never see a WRITE_12 but we'll do it anyway... 593 // 594 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | 595 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); 596 597 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) | 598 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9])); 599 600 US_DEBUGP("datafab_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks); 601 return datafab_write_data(us, info, block, blocks); 602 } 603 604 if (srb->cmnd[0] == TEST_UNIT_READY) { 605 US_DEBUGP("datafab_transport: TEST_UNIT_READY.\n"); 606 return datafab_id_device(us, info); 607 } 608 609 if (srb->cmnd[0] == REQUEST_SENSE) { 610 US_DEBUGP("datafab_transport: REQUEST_SENSE. Returning faked response\n"); 611 612 // this response is pretty bogus right now. eventually if necessary 613 // we can set the correct sense data. so far though it hasn't been 614 // necessary 615 // 616 memset(ptr, 0, 18); 617 ptr[0] = 0xF0; 618 ptr[2] = info->sense_key; 619 ptr[7] = 11; 620 ptr[12] = info->sense_asc; 621 ptr[13] = info->sense_ascq; 622 usb_stor_set_xfer_buf(ptr, 18, srb); 623 624 return USB_STOR_TRANSPORT_GOOD; 625 } 626 627 if (srb->cmnd[0] == MODE_SENSE) { 628 US_DEBUGP("datafab_transport: MODE_SENSE_6 detected\n"); 629 return datafab_handle_mode_sense(us, srb, 1); 630 } 631 632 if (srb->cmnd[0] == MODE_SENSE_10) { 633 US_DEBUGP("datafab_transport: MODE_SENSE_10 detected\n"); 634 return datafab_handle_mode_sense(us, srb, 0); 635 } 636 637 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) { 638 // sure. whatever. not like we can stop the user from 639 // popping the media out of the device (no locking doors, etc) 640 // 641 return USB_STOR_TRANSPORT_GOOD; 642 } 643 644 if (srb->cmnd[0] == START_STOP) { 645 /* this is used by sd.c'check_scsidisk_media_change to detect 646 media change */ 647 US_DEBUGP("datafab_transport: START_STOP.\n"); 648 /* the first datafab_id_device after a media change returns 649 an error (determined experimentally) */ 650 rc = datafab_id_device(us, info); 651 if (rc == USB_STOR_TRANSPORT_GOOD) { 652 info->sense_key = NO_SENSE; 653 srb->result = SUCCESS; 654 } else { 655 info->sense_key = UNIT_ATTENTION; 656 srb->result = SAM_STAT_CHECK_CONDITION; 657 } 658 return rc; 659 } 660 661 US_DEBUGP("datafab_transport: Gah! Unknown command: %d (0x%x)\n", 662 srb->cmnd[0], srb->cmnd[0]); 663 info->sense_key = 0x05; 664 info->sense_asc = 0x20; 665 info->sense_ascq = 0x00; 666 return USB_STOR_TRANSPORT_FAILED; 667 } 668