1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable 4 * 5 * Current development and maintenance by: 6 * (c) 2000, 2001 Robert Baruch (autophile@starband.net) 7 * (c) 2004, 2005 Daniel Drake <dsd@gentoo.org> 8 * 9 * Developed with the assistance of: 10 * (c) 2002 Alan Stern <stern@rowland.org> 11 * 12 * Flash support based on earlier work by: 13 * (c) 2002 Thomas Kreiling <usbdev@sm04.de> 14 * 15 * Many originally ATAPI devices were slightly modified to meet the USB 16 * market by using some kind of translation from ATAPI to USB on the host, 17 * and the peripheral would translate from USB back to ATAPI. 18 * 19 * SCM Microsystems (www.scmmicro.com) makes a device, sold to OEM's only, 20 * which does the USB-to-ATAPI conversion. By obtaining the data sheet on 21 * their device under nondisclosure agreement, I have been able to write 22 * this driver for Linux. 23 * 24 * The chip used in the device can also be used for EPP and ISA translation 25 * as well. This driver is only guaranteed to work with the ATAPI 26 * translation. 27 * 28 * See the Kconfig help text for a list of devices known to be supported by 29 * this driver. 30 */ 31 32 #include <linux/errno.h> 33 #include <linux/module.h> 34 #include <linux/slab.h> 35 #include <linux/string_choices.h> 36 #include <linux/cdrom.h> 37 38 #include <scsi/scsi.h> 39 #include <scsi/scsi_cmnd.h> 40 41 #include "usb.h" 42 #include "transport.h" 43 #include "protocol.h" 44 #include "debug.h" 45 #include "scsiglue.h" 46 47 #define DRV_NAME "ums-usbat" 48 49 MODULE_DESCRIPTION("Driver for SCM Microsystems (a.k.a. Shuttle) USB-ATAPI cable"); 50 MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>, Robert Baruch <autophile@starband.net>"); 51 MODULE_LICENSE("GPL"); 52 MODULE_IMPORT_NS("USB_STORAGE"); 53 54 /* Supported device types */ 55 #define USBAT_DEV_HP8200 0x01 56 #define USBAT_DEV_FLASH 0x02 57 58 #define USBAT_EPP_PORT 0x10 59 #define USBAT_EPP_REGISTER 0x30 60 #define USBAT_ATA 0x40 61 #define USBAT_ISA 0x50 62 63 /* Commands (need to be logically OR'd with an access type */ 64 #define USBAT_CMD_READ_REG 0x00 65 #define USBAT_CMD_WRITE_REG 0x01 66 #define USBAT_CMD_READ_BLOCK 0x02 67 #define USBAT_CMD_WRITE_BLOCK 0x03 68 #define USBAT_CMD_COND_READ_BLOCK 0x04 69 #define USBAT_CMD_COND_WRITE_BLOCK 0x05 70 #define USBAT_CMD_WRITE_REGS 0x07 71 72 /* Commands (these don't need an access type) */ 73 #define USBAT_CMD_EXEC_CMD 0x80 74 #define USBAT_CMD_SET_FEAT 0x81 75 #define USBAT_CMD_UIO 0x82 76 77 /* Methods of accessing UIO register */ 78 #define USBAT_UIO_READ 1 79 #define USBAT_UIO_WRITE 0 80 81 /* Qualifier bits */ 82 #define USBAT_QUAL_FCQ 0x20 /* full compare */ 83 #define USBAT_QUAL_ALQ 0x10 /* auto load subcount */ 84 85 /* USBAT Flash Media status types */ 86 #define USBAT_FLASH_MEDIA_NONE 0 87 #define USBAT_FLASH_MEDIA_CF 1 88 89 /* USBAT Flash Media change types */ 90 #define USBAT_FLASH_MEDIA_SAME 0 91 #define USBAT_FLASH_MEDIA_CHANGED 1 92 93 /* USBAT ATA registers */ 94 #define USBAT_ATA_DATA 0x10 /* read/write data (R/W) */ 95 #define USBAT_ATA_FEATURES 0x11 /* set features (W) */ 96 #define USBAT_ATA_ERROR 0x11 /* error (R) */ 97 #define USBAT_ATA_SECCNT 0x12 /* sector count (R/W) */ 98 #define USBAT_ATA_SECNUM 0x13 /* sector number (R/W) */ 99 #define USBAT_ATA_LBA_ME 0x14 /* cylinder low (R/W) */ 100 #define USBAT_ATA_LBA_HI 0x15 /* cylinder high (R/W) */ 101 #define USBAT_ATA_DEVICE 0x16 /* head/device selection (R/W) */ 102 #define USBAT_ATA_STATUS 0x17 /* device status (R) */ 103 #define USBAT_ATA_CMD 0x17 /* device command (W) */ 104 #define USBAT_ATA_ALTSTATUS 0x0E /* status (no clear IRQ) (R) */ 105 106 /* USBAT User I/O Data registers */ 107 #define USBAT_UIO_EPAD 0x80 /* Enable Peripheral Control Signals */ 108 #define USBAT_UIO_CDT 0x40 /* Card Detect (Read Only) */ 109 /* CDT = ACKD & !UI1 & !UI0 */ 110 #define USBAT_UIO_1 0x20 /* I/O 1 */ 111 #define USBAT_UIO_0 0x10 /* I/O 0 */ 112 #define USBAT_UIO_EPP_ATA 0x08 /* 1=EPP mode, 0=ATA mode */ 113 #define USBAT_UIO_UI1 0x04 /* Input 1 */ 114 #define USBAT_UIO_UI0 0x02 /* Input 0 */ 115 #define USBAT_UIO_INTR_ACK 0x01 /* Interrupt (ATA/ISA)/Acknowledge (EPP) */ 116 117 /* USBAT User I/O Enable registers */ 118 #define USBAT_UIO_DRVRST 0x80 /* Reset Peripheral */ 119 #define USBAT_UIO_ACKD 0x40 /* Enable Card Detect */ 120 #define USBAT_UIO_OE1 0x20 /* I/O 1 set=output/clr=input */ 121 /* If ACKD=1, set OE1 to 1 also. */ 122 #define USBAT_UIO_OE0 0x10 /* I/O 0 set=output/clr=input */ 123 #define USBAT_UIO_ADPRST 0x01 /* Reset SCM chip */ 124 125 /* USBAT Features */ 126 #define USBAT_FEAT_ETEN 0x80 /* External trigger enable */ 127 #define USBAT_FEAT_U1 0x08 128 #define USBAT_FEAT_U0 0x04 129 #define USBAT_FEAT_ET1 0x02 130 #define USBAT_FEAT_ET2 0x01 131 132 struct usbat_info { 133 int devicetype; 134 135 /* Used for Flash readers only */ 136 unsigned long sectors; /* total sector count */ 137 unsigned long ssize; /* sector size in bytes */ 138 139 unsigned char sense_key; 140 unsigned long sense_asc; /* additional sense code */ 141 unsigned long sense_ascq; /* additional sense code qualifier */ 142 }; 143 144 #define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) ) 145 #define LSB_of(s) ((s)&0xFF) 146 #define MSB_of(s) ((s)>>8) 147 148 static int transferred = 0; 149 150 static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us); 151 static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us); 152 153 static int init_usbat_cd(struct us_data *us); 154 static int init_usbat_flash(struct us_data *us); 155 156 157 /* 158 * The table of devices 159 */ 160 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \ 161 vendorName, productName, useProtocol, useTransport, \ 162 initFunction, flags) \ 163 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \ 164 .driver_info = (flags) } 165 166 static const struct usb_device_id usbat_usb_ids[] = { 167 # include "unusual_usbat.h" 168 { } /* Terminating entry */ 169 }; 170 MODULE_DEVICE_TABLE(usb, usbat_usb_ids); 171 172 #undef UNUSUAL_DEV 173 174 /* 175 * The flags table 176 */ 177 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \ 178 vendor_name, product_name, use_protocol, use_transport, \ 179 init_function, Flags) \ 180 { \ 181 .vendorName = vendor_name, \ 182 .productName = product_name, \ 183 .useProtocol = use_protocol, \ 184 .useTransport = use_transport, \ 185 .initFunction = init_function, \ 186 } 187 188 static const struct us_unusual_dev usbat_unusual_dev_list[] = { 189 # include "unusual_usbat.h" 190 { } /* Terminating entry */ 191 }; 192 193 #undef UNUSUAL_DEV 194 195 /* 196 * Convenience function to produce an ATA read/write sectors command 197 * Use cmd=0x20 for read, cmd=0x30 for write 198 */ 199 static void usbat_pack_ata_sector_cmd(unsigned char *buf, 200 unsigned char thistime, 201 u32 sector, unsigned char cmd) 202 { 203 buf[0] = 0; 204 buf[1] = thistime; 205 buf[2] = sector & 0xFF; 206 buf[3] = (sector >> 8) & 0xFF; 207 buf[4] = (sector >> 16) & 0xFF; 208 buf[5] = 0xE0 | ((sector >> 24) & 0x0F); 209 buf[6] = cmd; 210 } 211 212 /* 213 * Convenience function to get the device type (flash or hp8200) 214 */ 215 static int usbat_get_device_type(struct us_data *us) 216 { 217 return ((struct usbat_info*)us->extra)->devicetype; 218 } 219 220 /* 221 * Read a register from the device 222 */ 223 static int usbat_read(struct us_data *us, 224 unsigned char access, 225 unsigned char reg, 226 unsigned char *content) 227 { 228 return usb_stor_ctrl_transfer(us, 229 us->recv_ctrl_pipe, 230 access | USBAT_CMD_READ_REG, 231 0xC0, 232 (u16)reg, 233 0, 234 content, 235 1); 236 } 237 238 /* 239 * Write to a register on the device 240 */ 241 static int usbat_write(struct us_data *us, 242 unsigned char access, 243 unsigned char reg, 244 unsigned char content) 245 { 246 return usb_stor_ctrl_transfer(us, 247 us->send_ctrl_pipe, 248 access | USBAT_CMD_WRITE_REG, 249 0x40, 250 short_pack(reg, content), 251 0, 252 NULL, 253 0); 254 } 255 256 /* 257 * Convenience function to perform a bulk read 258 */ 259 static int usbat_bulk_read(struct us_data *us, 260 void* buf, 261 unsigned int len, 262 int use_sg) 263 { 264 if (len == 0) 265 return USB_STOR_XFER_GOOD; 266 267 usb_stor_dbg(us, "len = %d\n", len); 268 return usb_stor_bulk_transfer_sg(us, us->recv_bulk_pipe, buf, len, use_sg, NULL); 269 } 270 271 /* 272 * Convenience function to perform a bulk write 273 */ 274 static int usbat_bulk_write(struct us_data *us, 275 void* buf, 276 unsigned int len, 277 int use_sg) 278 { 279 if (len == 0) 280 return USB_STOR_XFER_GOOD; 281 282 usb_stor_dbg(us, "len = %d\n", len); 283 return usb_stor_bulk_transfer_sg(us, us->send_bulk_pipe, buf, len, use_sg, NULL); 284 } 285 286 /* 287 * Some USBAT-specific commands can only be executed over a command transport 288 * This transport allows one (len=8) or two (len=16) vendor-specific commands 289 * to be executed. 290 */ 291 static int usbat_execute_command(struct us_data *us, 292 unsigned char *commands, 293 unsigned int len) 294 { 295 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe, 296 USBAT_CMD_EXEC_CMD, 0x40, 0, 0, 297 commands, len); 298 } 299 300 /* 301 * Read the status register 302 */ 303 static int usbat_get_status(struct us_data *us, unsigned char *status) 304 { 305 int rc; 306 rc = usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status); 307 308 usb_stor_dbg(us, "0x%02X\n", *status); 309 return rc; 310 } 311 312 /* 313 * Check the device status 314 */ 315 static int usbat_check_status(struct us_data *us) 316 { 317 unsigned char *reply = us->iobuf; 318 int rc; 319 320 rc = usbat_get_status(us, reply); 321 if (rc != USB_STOR_XFER_GOOD) 322 return USB_STOR_TRANSPORT_FAILED; 323 324 /* error/check condition (0x51 is ok) */ 325 if (*reply & 0x01 && *reply != 0x51) 326 return USB_STOR_TRANSPORT_FAILED; 327 328 /* device fault */ 329 if (*reply & 0x20) 330 return USB_STOR_TRANSPORT_FAILED; 331 332 return USB_STOR_TRANSPORT_GOOD; 333 } 334 335 /* 336 * Stores critical information in internal registers in preparation for the execution 337 * of a conditional usbat_read_blocks or usbat_write_blocks call. 338 */ 339 static int usbat_set_shuttle_features(struct us_data *us, 340 unsigned char external_trigger, 341 unsigned char epp_control, 342 unsigned char mask_byte, 343 unsigned char test_pattern, 344 unsigned char subcountH, 345 unsigned char subcountL) 346 { 347 unsigned char *command = us->iobuf; 348 349 command[0] = 0x40; 350 command[1] = USBAT_CMD_SET_FEAT; 351 352 /* 353 * The only bit relevant to ATA access is bit 6 354 * which defines 8 bit data access (set) or 16 bit (unset) 355 */ 356 command[2] = epp_control; 357 358 /* 359 * If FCQ is set in the qualifier (defined in R/W cmd), then bits U0, U1, 360 * ET1 and ET2 define an external event to be checked for on event of a 361 * _read_blocks or _write_blocks operation. The read/write will not take 362 * place unless the defined trigger signal is active. 363 */ 364 command[3] = external_trigger; 365 366 /* 367 * The resultant byte of the mask operation (see mask_byte) is compared for 368 * equivalence with this test pattern. If equal, the read/write will take 369 * place. 370 */ 371 command[4] = test_pattern; 372 373 /* 374 * This value is logically ANDed with the status register field specified 375 * in the read/write command. 376 */ 377 command[5] = mask_byte; 378 379 /* 380 * If ALQ is set in the qualifier, this field contains the address of the 381 * registers where the byte count should be read for transferring the data. 382 * If ALQ is not set, then this field contains the number of bytes to be 383 * transferred. 384 */ 385 command[6] = subcountL; 386 command[7] = subcountH; 387 388 return usbat_execute_command(us, command, 8); 389 } 390 391 /* 392 * Block, waiting for an ATA device to become not busy or to report 393 * an error condition. 394 */ 395 static int usbat_wait_not_busy(struct us_data *us, int minutes) 396 { 397 int i; 398 int result; 399 unsigned char *status = us->iobuf; 400 401 /* 402 * Synchronizing cache on a CDR could take a heck of a long time, 403 * but probably not more than 10 minutes or so. On the other hand, 404 * doing a full blank on a CDRW at speed 1 will take about 75 405 * minutes! 406 */ 407 408 for (i=0; i<1200+minutes*60; i++) { 409 410 result = usbat_get_status(us, status); 411 412 if (result!=USB_STOR_XFER_GOOD) 413 return USB_STOR_TRANSPORT_ERROR; 414 if (*status & 0x01) { /* check condition */ 415 result = usbat_read(us, USBAT_ATA, 0x10, status); 416 return USB_STOR_TRANSPORT_FAILED; 417 } 418 if (*status & 0x20) /* device fault */ 419 return USB_STOR_TRANSPORT_FAILED; 420 421 if ((*status & 0x80)==0x00) { /* not busy */ 422 usb_stor_dbg(us, "Waited not busy for %d steps\n", i); 423 return USB_STOR_TRANSPORT_GOOD; 424 } 425 426 if (i<500) 427 msleep(10); /* 5 seconds */ 428 else if (i<700) 429 msleep(50); /* 10 seconds */ 430 else if (i<1200) 431 msleep(100); /* 50 seconds */ 432 else 433 msleep(1000); /* X minutes */ 434 } 435 436 usb_stor_dbg(us, "Waited not busy for %d minutes, timing out\n", 437 minutes); 438 return USB_STOR_TRANSPORT_FAILED; 439 } 440 441 /* 442 * Read block data from the data register 443 */ 444 static int usbat_read_block(struct us_data *us, 445 void* buf, 446 unsigned short len, 447 int use_sg) 448 { 449 int result; 450 unsigned char *command = us->iobuf; 451 452 if (!len) 453 return USB_STOR_TRANSPORT_GOOD; 454 455 command[0] = 0xC0; 456 command[1] = USBAT_ATA | USBAT_CMD_READ_BLOCK; 457 command[2] = USBAT_ATA_DATA; 458 command[3] = 0; 459 command[4] = 0; 460 command[5] = 0; 461 command[6] = LSB_of(len); 462 command[7] = MSB_of(len); 463 464 result = usbat_execute_command(us, command, 8); 465 if (result != USB_STOR_XFER_GOOD) 466 return USB_STOR_TRANSPORT_ERROR; 467 468 result = usbat_bulk_read(us, buf, len, use_sg); 469 return (result == USB_STOR_XFER_GOOD ? 470 USB_STOR_TRANSPORT_GOOD : USB_STOR_TRANSPORT_ERROR); 471 } 472 473 /* 474 * Write block data via the data register 475 */ 476 static int usbat_write_block(struct us_data *us, 477 unsigned char access, 478 void* buf, 479 unsigned short len, 480 int minutes, 481 int use_sg) 482 { 483 int result; 484 unsigned char *command = us->iobuf; 485 486 if (!len) 487 return USB_STOR_TRANSPORT_GOOD; 488 489 command[0] = 0x40; 490 command[1] = access | USBAT_CMD_WRITE_BLOCK; 491 command[2] = USBAT_ATA_DATA; 492 command[3] = 0; 493 command[4] = 0; 494 command[5] = 0; 495 command[6] = LSB_of(len); 496 command[7] = MSB_of(len); 497 498 result = usbat_execute_command(us, command, 8); 499 500 if (result != USB_STOR_XFER_GOOD) 501 return USB_STOR_TRANSPORT_ERROR; 502 503 result = usbat_bulk_write(us, buf, len, use_sg); 504 if (result != USB_STOR_XFER_GOOD) 505 return USB_STOR_TRANSPORT_ERROR; 506 507 return usbat_wait_not_busy(us, minutes); 508 } 509 510 /* 511 * Process read and write requests 512 */ 513 static int usbat_hp8200e_rw_block_test(struct us_data *us, 514 unsigned char access, 515 unsigned char *registers, 516 unsigned char *data_out, 517 unsigned short num_registers, 518 unsigned char data_reg, 519 unsigned char status_reg, 520 unsigned char timeout, 521 unsigned char qualifier, 522 int direction, 523 void *buf, 524 unsigned short len, 525 int use_sg, 526 int minutes) 527 { 528 int result; 529 unsigned int pipe = (direction == DMA_FROM_DEVICE) ? 530 us->recv_bulk_pipe : us->send_bulk_pipe; 531 532 unsigned char *command = us->iobuf; 533 int i, j; 534 int cmdlen; 535 unsigned char *data = us->iobuf; 536 unsigned char *status = us->iobuf; 537 538 BUG_ON(num_registers > US_IOBUF_SIZE/2); 539 540 for (i=0; i<20; i++) { 541 542 /* 543 * The first time we send the full command, which consists 544 * of downloading the SCSI command followed by downloading 545 * the data via a write-and-test. Any other time we only 546 * send the command to download the data -- the SCSI command 547 * is still 'active' in some sense in the device. 548 * 549 * We're only going to try sending the data 10 times. After 550 * that, we just return a failure. 551 */ 552 553 if (i==0) { 554 cmdlen = 16; 555 /* 556 * Write to multiple registers 557 * Not really sure the 0x07, 0x17, 0xfc, 0xe7 is 558 * necessary here, but that's what came out of the 559 * trace every single time. 560 */ 561 command[0] = 0x40; 562 command[1] = access | USBAT_CMD_WRITE_REGS; 563 command[2] = 0x07; 564 command[3] = 0x17; 565 command[4] = 0xFC; 566 command[5] = 0xE7; 567 command[6] = LSB_of(num_registers*2); 568 command[7] = MSB_of(num_registers*2); 569 } else 570 cmdlen = 8; 571 572 /* Conditionally read or write blocks */ 573 command[cmdlen-8] = (direction==DMA_TO_DEVICE ? 0x40 : 0xC0); 574 command[cmdlen-7] = access | 575 (direction==DMA_TO_DEVICE ? 576 USBAT_CMD_COND_WRITE_BLOCK : USBAT_CMD_COND_READ_BLOCK); 577 command[cmdlen-6] = data_reg; 578 command[cmdlen-5] = status_reg; 579 command[cmdlen-4] = timeout; 580 command[cmdlen-3] = qualifier; 581 command[cmdlen-2] = LSB_of(len); 582 command[cmdlen-1] = MSB_of(len); 583 584 result = usbat_execute_command(us, command, cmdlen); 585 586 if (result != USB_STOR_XFER_GOOD) 587 return USB_STOR_TRANSPORT_ERROR; 588 589 if (i==0) { 590 591 for (j=0; j<num_registers; j++) { 592 data[j<<1] = registers[j]; 593 data[1+(j<<1)] = data_out[j]; 594 } 595 596 result = usbat_bulk_write(us, data, num_registers*2, 0); 597 if (result != USB_STOR_XFER_GOOD) 598 return USB_STOR_TRANSPORT_ERROR; 599 600 } 601 602 result = usb_stor_bulk_transfer_sg(us, 603 pipe, buf, len, use_sg, NULL); 604 605 /* 606 * If we get a stall on the bulk download, we'll retry 607 * the bulk download -- but not the SCSI command because 608 * in some sense the SCSI command is still 'active' and 609 * waiting for the data. Don't ask me why this should be; 610 * I'm only following what the Windoze driver did. 611 * 612 * Note that a stall for the test-and-read/write command means 613 * that the test failed. In this case we're testing to make 614 * sure that the device is error-free 615 * (i.e. bit 0 -- CHK -- of status is 0). The most likely 616 * hypothesis is that the USBAT chip somehow knows what 617 * the device will accept, but doesn't give the device any 618 * data until all data is received. Thus, the device would 619 * still be waiting for the first byte of data if a stall 620 * occurs, even if the stall implies that some data was 621 * transferred. 622 */ 623 624 if (result == USB_STOR_XFER_SHORT || 625 result == USB_STOR_XFER_STALLED) { 626 627 /* 628 * If we're reading and we stalled, then clear 629 * the bulk output pipe only the first time. 630 */ 631 632 if (direction==DMA_FROM_DEVICE && i==0) { 633 if (usb_stor_clear_halt(us, 634 us->send_bulk_pipe) < 0) 635 return USB_STOR_TRANSPORT_ERROR; 636 } 637 638 /* 639 * Read status: is the device angry, or just busy? 640 */ 641 642 result = usbat_read(us, USBAT_ATA, 643 direction==DMA_TO_DEVICE ? 644 USBAT_ATA_STATUS : USBAT_ATA_ALTSTATUS, 645 status); 646 647 if (result!=USB_STOR_XFER_GOOD) 648 return USB_STOR_TRANSPORT_ERROR; 649 if (*status & 0x01) /* check condition */ 650 return USB_STOR_TRANSPORT_FAILED; 651 if (*status & 0x20) /* device fault */ 652 return USB_STOR_TRANSPORT_FAILED; 653 654 usb_stor_dbg(us, "Redoing %s\n", 655 str_write_read(direction == DMA_TO_DEVICE)); 656 657 } else if (result != USB_STOR_XFER_GOOD) 658 return USB_STOR_TRANSPORT_ERROR; 659 else 660 return usbat_wait_not_busy(us, minutes); 661 662 } 663 664 usb_stor_dbg(us, "Bummer! %s bulk data 20 times failed\n", 665 direction == DMA_TO_DEVICE ? "Writing" : "Reading"); 666 667 return USB_STOR_TRANSPORT_FAILED; 668 } 669 670 /* 671 * Write to multiple registers: 672 * Allows us to write specific data to any registers. The data to be written 673 * gets packed in this sequence: reg0, data0, reg1, data1, ..., regN, dataN 674 * which gets sent through bulk out. 675 * Not designed for large transfers of data! 676 */ 677 static int usbat_multiple_write(struct us_data *us, 678 unsigned char *registers, 679 unsigned char *data_out, 680 unsigned short num_registers) 681 { 682 int i, result; 683 unsigned char *data = us->iobuf; 684 unsigned char *command = us->iobuf; 685 686 BUG_ON(num_registers > US_IOBUF_SIZE/2); 687 688 /* Write to multiple registers, ATA access */ 689 command[0] = 0x40; 690 command[1] = USBAT_ATA | USBAT_CMD_WRITE_REGS; 691 692 /* No relevance */ 693 command[2] = 0; 694 command[3] = 0; 695 command[4] = 0; 696 command[5] = 0; 697 698 /* Number of bytes to be transferred (incl. addresses and data) */ 699 command[6] = LSB_of(num_registers*2); 700 command[7] = MSB_of(num_registers*2); 701 702 /* The setup command */ 703 result = usbat_execute_command(us, command, 8); 704 if (result != USB_STOR_XFER_GOOD) 705 return USB_STOR_TRANSPORT_ERROR; 706 707 /* Create the reg/data, reg/data sequence */ 708 for (i=0; i<num_registers; i++) { 709 data[i<<1] = registers[i]; 710 data[1+(i<<1)] = data_out[i]; 711 } 712 713 /* Send the data */ 714 result = usbat_bulk_write(us, data, num_registers*2, 0); 715 if (result != USB_STOR_XFER_GOOD) 716 return USB_STOR_TRANSPORT_ERROR; 717 718 if (usbat_get_device_type(us) == USBAT_DEV_HP8200) 719 return usbat_wait_not_busy(us, 0); 720 else 721 return USB_STOR_TRANSPORT_GOOD; 722 } 723 724 /* 725 * Conditionally read blocks from device: 726 * Allows us to read blocks from a specific data register, based upon the 727 * condition that a status register can be successfully masked with a status 728 * qualifier. If this condition is not initially met, the read will wait 729 * up until a maximum amount of time has elapsed, as specified by timeout. 730 * The read will start when the condition is met, otherwise the command aborts. 731 * 732 * The qualifier defined here is not the value that is masked, it defines 733 * conditions for the write to take place. The actual masked qualifier (and 734 * other related details) are defined beforehand with _set_shuttle_features(). 735 */ 736 static int usbat_read_blocks(struct us_data *us, 737 void* buffer, 738 int len, 739 int use_sg) 740 { 741 int result; 742 unsigned char *command = us->iobuf; 743 744 command[0] = 0xC0; 745 command[1] = USBAT_ATA | USBAT_CMD_COND_READ_BLOCK; 746 command[2] = USBAT_ATA_DATA; 747 command[3] = USBAT_ATA_STATUS; 748 command[4] = 0xFD; /* Timeout (ms); */ 749 command[5] = USBAT_QUAL_FCQ; 750 command[6] = LSB_of(len); 751 command[7] = MSB_of(len); 752 753 /* Multiple block read setup command */ 754 result = usbat_execute_command(us, command, 8); 755 if (result != USB_STOR_XFER_GOOD) 756 return USB_STOR_TRANSPORT_FAILED; 757 758 /* Read the blocks we just asked for */ 759 result = usbat_bulk_read(us, buffer, len, use_sg); 760 if (result != USB_STOR_XFER_GOOD) 761 return USB_STOR_TRANSPORT_FAILED; 762 763 return USB_STOR_TRANSPORT_GOOD; 764 } 765 766 /* 767 * Conditionally write blocks to device: 768 * Allows us to write blocks to a specific data register, based upon the 769 * condition that a status register can be successfully masked with a status 770 * qualifier. If this condition is not initially met, the write will wait 771 * up until a maximum amount of time has elapsed, as specified by timeout. 772 * The read will start when the condition is met, otherwise the command aborts. 773 * 774 * The qualifier defined here is not the value that is masked, it defines 775 * conditions for the write to take place. The actual masked qualifier (and 776 * other related details) are defined beforehand with _set_shuttle_features(). 777 */ 778 static int usbat_write_blocks(struct us_data *us, 779 void* buffer, 780 int len, 781 int use_sg) 782 { 783 int result; 784 unsigned char *command = us->iobuf; 785 786 command[0] = 0x40; 787 command[1] = USBAT_ATA | USBAT_CMD_COND_WRITE_BLOCK; 788 command[2] = USBAT_ATA_DATA; 789 command[3] = USBAT_ATA_STATUS; 790 command[4] = 0xFD; /* Timeout (ms) */ 791 command[5] = USBAT_QUAL_FCQ; 792 command[6] = LSB_of(len); 793 command[7] = MSB_of(len); 794 795 /* Multiple block write setup command */ 796 result = usbat_execute_command(us, command, 8); 797 if (result != USB_STOR_XFER_GOOD) 798 return USB_STOR_TRANSPORT_FAILED; 799 800 /* Write the data */ 801 result = usbat_bulk_write(us, buffer, len, use_sg); 802 if (result != USB_STOR_XFER_GOOD) 803 return USB_STOR_TRANSPORT_FAILED; 804 805 return USB_STOR_TRANSPORT_GOOD; 806 } 807 808 /* 809 * Read the User IO register 810 */ 811 static int usbat_read_user_io(struct us_data *us, unsigned char *data_flags) 812 { 813 int result; 814 815 result = usb_stor_ctrl_transfer(us, 816 us->recv_ctrl_pipe, 817 USBAT_CMD_UIO, 818 0xC0, 819 0, 820 0, 821 data_flags, 822 USBAT_UIO_READ); 823 824 usb_stor_dbg(us, "UIO register reads %02X\n", *data_flags); 825 826 return result; 827 } 828 829 /* 830 * Write to the User IO register 831 */ 832 static int usbat_write_user_io(struct us_data *us, 833 unsigned char enable_flags, 834 unsigned char data_flags) 835 { 836 return usb_stor_ctrl_transfer(us, 837 us->send_ctrl_pipe, 838 USBAT_CMD_UIO, 839 0x40, 840 short_pack(enable_flags, data_flags), 841 0, 842 NULL, 843 USBAT_UIO_WRITE); 844 } 845 846 /* 847 * Reset the device 848 * Often needed on media change. 849 */ 850 static int usbat_device_reset(struct us_data *us) 851 { 852 int rc; 853 854 /* 855 * Reset peripheral, enable peripheral control signals 856 * (bring reset signal up) 857 */ 858 rc = usbat_write_user_io(us, 859 USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0, 860 USBAT_UIO_EPAD | USBAT_UIO_1); 861 if (rc != USB_STOR_XFER_GOOD) 862 return USB_STOR_TRANSPORT_ERROR; 863 864 /* 865 * Enable peripheral control signals 866 * (bring reset signal down) 867 */ 868 rc = usbat_write_user_io(us, 869 USBAT_UIO_OE1 | USBAT_UIO_OE0, 870 USBAT_UIO_EPAD | USBAT_UIO_1); 871 if (rc != USB_STOR_XFER_GOOD) 872 return USB_STOR_TRANSPORT_ERROR; 873 874 return USB_STOR_TRANSPORT_GOOD; 875 } 876 877 /* 878 * Enable card detect 879 */ 880 static int usbat_device_enable_cdt(struct us_data *us) 881 { 882 int rc; 883 884 /* Enable peripheral control signals and card detect */ 885 rc = usbat_write_user_io(us, 886 USBAT_UIO_ACKD | USBAT_UIO_OE1 | USBAT_UIO_OE0, 887 USBAT_UIO_EPAD | USBAT_UIO_1); 888 if (rc != USB_STOR_XFER_GOOD) 889 return USB_STOR_TRANSPORT_ERROR; 890 891 return USB_STOR_TRANSPORT_GOOD; 892 } 893 894 /* 895 * Determine if media is present. 896 */ 897 static int usbat_flash_check_media_present(struct us_data *us, 898 unsigned char *uio) 899 { 900 if (*uio & USBAT_UIO_UI0) { 901 usb_stor_dbg(us, "no media detected\n"); 902 return USBAT_FLASH_MEDIA_NONE; 903 } 904 905 return USBAT_FLASH_MEDIA_CF; 906 } 907 908 /* 909 * Determine if media has changed since last operation 910 */ 911 static int usbat_flash_check_media_changed(struct us_data *us, 912 unsigned char *uio) 913 { 914 if (*uio & USBAT_UIO_0) { 915 usb_stor_dbg(us, "media change detected\n"); 916 return USBAT_FLASH_MEDIA_CHANGED; 917 } 918 919 return USBAT_FLASH_MEDIA_SAME; 920 } 921 922 /* 923 * Check for media change / no media and handle the situation appropriately 924 */ 925 static int usbat_flash_check_media(struct us_data *us, 926 struct usbat_info *info) 927 { 928 int rc; 929 unsigned char *uio = us->iobuf; 930 931 rc = usbat_read_user_io(us, uio); 932 if (rc != USB_STOR_XFER_GOOD) 933 return USB_STOR_TRANSPORT_ERROR; 934 935 /* Check for media existence */ 936 rc = usbat_flash_check_media_present(us, uio); 937 if (rc == USBAT_FLASH_MEDIA_NONE) { 938 info->sense_key = 0x02; 939 info->sense_asc = 0x3A; 940 info->sense_ascq = 0x00; 941 return USB_STOR_TRANSPORT_FAILED; 942 } 943 944 /* Check for media change */ 945 rc = usbat_flash_check_media_changed(us, uio); 946 if (rc == USBAT_FLASH_MEDIA_CHANGED) { 947 948 /* Reset and re-enable card detect */ 949 rc = usbat_device_reset(us); 950 if (rc != USB_STOR_TRANSPORT_GOOD) 951 return rc; 952 rc = usbat_device_enable_cdt(us); 953 if (rc != USB_STOR_TRANSPORT_GOOD) 954 return rc; 955 956 msleep(50); 957 958 rc = usbat_read_user_io(us, uio); 959 if (rc != USB_STOR_XFER_GOOD) 960 return USB_STOR_TRANSPORT_ERROR; 961 962 info->sense_key = UNIT_ATTENTION; 963 info->sense_asc = 0x28; 964 info->sense_ascq = 0x00; 965 return USB_STOR_TRANSPORT_FAILED; 966 } 967 968 return USB_STOR_TRANSPORT_GOOD; 969 } 970 971 /* 972 * Determine whether we are controlling a flash-based reader/writer, 973 * or a HP8200-based CD drive. 974 * Sets transport functions as appropriate. 975 */ 976 static int usbat_identify_device(struct us_data *us, 977 struct usbat_info *info) 978 { 979 int rc; 980 unsigned char status; 981 982 if (!us || !info) 983 return USB_STOR_TRANSPORT_ERROR; 984 985 rc = usbat_device_reset(us); 986 if (rc != USB_STOR_TRANSPORT_GOOD) 987 return rc; 988 msleep(500); 989 990 /* 991 * In attempt to distinguish between HP CDRW's and Flash readers, we now 992 * execute the IDENTIFY PACKET DEVICE command. On ATA devices (i.e. flash 993 * readers), this command should fail with error. On ATAPI devices (i.e. 994 * CDROM drives), it should succeed. 995 */ 996 rc = usbat_write(us, USBAT_ATA, USBAT_ATA_CMD, 0xA1); 997 if (rc != USB_STOR_XFER_GOOD) 998 return USB_STOR_TRANSPORT_ERROR; 999 1000 rc = usbat_get_status(us, &status); 1001 if (rc != USB_STOR_XFER_GOOD) 1002 return USB_STOR_TRANSPORT_ERROR; 1003 1004 /* Check for error bit, or if the command 'fell through' */ 1005 if (status == 0xA1 || !(status & 0x01)) { 1006 /* Device is HP 8200 */ 1007 usb_stor_dbg(us, "Detected HP8200 CDRW\n"); 1008 info->devicetype = USBAT_DEV_HP8200; 1009 } else { 1010 /* Device is a CompactFlash reader/writer */ 1011 usb_stor_dbg(us, "Detected Flash reader/writer\n"); 1012 info->devicetype = USBAT_DEV_FLASH; 1013 } 1014 1015 return USB_STOR_TRANSPORT_GOOD; 1016 } 1017 1018 /* 1019 * Set the transport function based on the device type 1020 */ 1021 static int usbat_set_transport(struct us_data *us, 1022 struct usbat_info *info, 1023 int devicetype) 1024 { 1025 1026 if (!info->devicetype) 1027 info->devicetype = devicetype; 1028 1029 if (!info->devicetype) 1030 usbat_identify_device(us, info); 1031 1032 switch (info->devicetype) { 1033 default: 1034 return USB_STOR_TRANSPORT_ERROR; 1035 1036 case USBAT_DEV_HP8200: 1037 us->transport = usbat_hp8200e_transport; 1038 break; 1039 1040 case USBAT_DEV_FLASH: 1041 /* The ATA sector size of these readers is fixed at 512 bytes */ 1042 info->ssize = 0x200; 1043 us->transport = usbat_flash_transport; 1044 break; 1045 } 1046 1047 return 0; 1048 } 1049 1050 /* 1051 * Read the media capacity 1052 */ 1053 static int usbat_flash_get_sector_count(struct us_data *us, 1054 struct usbat_info *info) 1055 { 1056 unsigned char registers[3] = { 1057 USBAT_ATA_SECCNT, 1058 USBAT_ATA_DEVICE, 1059 USBAT_ATA_CMD, 1060 }; 1061 unsigned char command[3] = { 0x01, 0xA0, 0xEC }; 1062 unsigned char *reply; 1063 unsigned char status; 1064 int rc; 1065 1066 if (!us || !info) 1067 return USB_STOR_TRANSPORT_ERROR; 1068 1069 reply = kmalloc(512, GFP_NOIO); 1070 if (!reply) 1071 return USB_STOR_TRANSPORT_ERROR; 1072 1073 /* ATA command : IDENTIFY DEVICE */ 1074 rc = usbat_multiple_write(us, registers, command, 3); 1075 if (rc != USB_STOR_XFER_GOOD) { 1076 usb_stor_dbg(us, "Gah! identify_device failed\n"); 1077 rc = USB_STOR_TRANSPORT_ERROR; 1078 goto leave; 1079 } 1080 1081 /* Read device status */ 1082 if (usbat_get_status(us, &status) != USB_STOR_XFER_GOOD) { 1083 rc = USB_STOR_TRANSPORT_ERROR; 1084 goto leave; 1085 } 1086 1087 msleep(100); 1088 1089 /* Read the device identification data */ 1090 rc = usbat_read_block(us, reply, 512, 0); 1091 if (rc != USB_STOR_TRANSPORT_GOOD) 1092 goto leave; 1093 1094 info->sectors = ((u32)(reply[117]) << 24) | 1095 ((u32)(reply[116]) << 16) | 1096 ((u32)(reply[115]) << 8) | 1097 ((u32)(reply[114]) ); 1098 1099 rc = USB_STOR_TRANSPORT_GOOD; 1100 1101 leave: 1102 kfree(reply); 1103 return rc; 1104 } 1105 1106 /* 1107 * Read data from device 1108 */ 1109 static int usbat_flash_read_data(struct us_data *us, 1110 struct usbat_info *info, 1111 u32 sector, 1112 u32 sectors) 1113 { 1114 unsigned char registers[7] = { 1115 USBAT_ATA_FEATURES, 1116 USBAT_ATA_SECCNT, 1117 USBAT_ATA_SECNUM, 1118 USBAT_ATA_LBA_ME, 1119 USBAT_ATA_LBA_HI, 1120 USBAT_ATA_DEVICE, 1121 USBAT_ATA_STATUS, 1122 }; 1123 unsigned char command[7]; 1124 unsigned char *buffer; 1125 unsigned char thistime; 1126 unsigned int totallen, alloclen; 1127 int len, result; 1128 unsigned int sg_offset = 0; 1129 struct scatterlist *sg = NULL; 1130 1131 result = usbat_flash_check_media(us, info); 1132 if (result != USB_STOR_TRANSPORT_GOOD) 1133 return result; 1134 1135 /* 1136 * we're working in LBA mode. according to the ATA spec, 1137 * we can support up to 28-bit addressing. I don't know if Jumpshot 1138 * supports beyond 24-bit addressing. It's kind of hard to test 1139 * since it requires > 8GB CF card. 1140 */ 1141 1142 if (sector > 0x0FFFFFFF) 1143 return USB_STOR_TRANSPORT_ERROR; 1144 1145 totallen = sectors * info->ssize; 1146 1147 /* 1148 * Since we don't read more than 64 KB at a time, we have to create 1149 * a bounce buffer and move the data a piece at a time between the 1150 * bounce buffer and the actual transfer buffer. 1151 */ 1152 1153 alloclen = min(totallen, 65536u); 1154 buffer = kmalloc(alloclen, GFP_NOIO); 1155 if (buffer == NULL) 1156 return USB_STOR_TRANSPORT_ERROR; 1157 1158 do { 1159 /* 1160 * loop, never allocate or transfer more than 64k at once 1161 * (min(128k, 255*info->ssize) is the real limit) 1162 */ 1163 len = min(totallen, alloclen); 1164 thistime = (len / info->ssize) & 0xff; 1165 1166 /* ATA command 0x20 (READ SECTORS) */ 1167 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x20); 1168 1169 /* Write/execute ATA read command */ 1170 result = usbat_multiple_write(us, registers, command, 7); 1171 if (result != USB_STOR_TRANSPORT_GOOD) 1172 goto leave; 1173 1174 /* Read the data we just requested */ 1175 result = usbat_read_blocks(us, buffer, len, 0); 1176 if (result != USB_STOR_TRANSPORT_GOOD) 1177 goto leave; 1178 1179 usb_stor_dbg(us, "%d bytes\n", len); 1180 1181 /* Store the data in the transfer buffer */ 1182 usb_stor_access_xfer_buf(buffer, len, us->srb, 1183 &sg, &sg_offset, TO_XFER_BUF); 1184 1185 sector += thistime; 1186 totallen -= len; 1187 } while (totallen > 0); 1188 1189 kfree(buffer); 1190 return USB_STOR_TRANSPORT_GOOD; 1191 1192 leave: 1193 kfree(buffer); 1194 return USB_STOR_TRANSPORT_ERROR; 1195 } 1196 1197 /* 1198 * Write data to device 1199 */ 1200 static int usbat_flash_write_data(struct us_data *us, 1201 struct usbat_info *info, 1202 u32 sector, 1203 u32 sectors) 1204 { 1205 unsigned char registers[7] = { 1206 USBAT_ATA_FEATURES, 1207 USBAT_ATA_SECCNT, 1208 USBAT_ATA_SECNUM, 1209 USBAT_ATA_LBA_ME, 1210 USBAT_ATA_LBA_HI, 1211 USBAT_ATA_DEVICE, 1212 USBAT_ATA_STATUS, 1213 }; 1214 unsigned char command[7]; 1215 unsigned char *buffer; 1216 unsigned char thistime; 1217 unsigned int totallen, alloclen; 1218 int len, result; 1219 unsigned int sg_offset = 0; 1220 struct scatterlist *sg = NULL; 1221 1222 result = usbat_flash_check_media(us, info); 1223 if (result != USB_STOR_TRANSPORT_GOOD) 1224 return result; 1225 1226 /* 1227 * we're working in LBA mode. according to the ATA spec, 1228 * we can support up to 28-bit addressing. I don't know if the device 1229 * supports beyond 24-bit addressing. It's kind of hard to test 1230 * since it requires > 8GB media. 1231 */ 1232 1233 if (sector > 0x0FFFFFFF) 1234 return USB_STOR_TRANSPORT_ERROR; 1235 1236 totallen = sectors * info->ssize; 1237 1238 /* 1239 * Since we don't write more than 64 KB at a time, we have to create 1240 * a bounce buffer and move the data a piece at a time between the 1241 * bounce buffer and the actual transfer buffer. 1242 */ 1243 1244 alloclen = min(totallen, 65536u); 1245 buffer = kmalloc(alloclen, GFP_NOIO); 1246 if (buffer == NULL) 1247 return USB_STOR_TRANSPORT_ERROR; 1248 1249 do { 1250 /* 1251 * loop, never allocate or transfer more than 64k at once 1252 * (min(128k, 255*info->ssize) is the real limit) 1253 */ 1254 len = min(totallen, alloclen); 1255 thistime = (len / info->ssize) & 0xff; 1256 1257 /* Get the data from the transfer buffer */ 1258 usb_stor_access_xfer_buf(buffer, len, us->srb, 1259 &sg, &sg_offset, FROM_XFER_BUF); 1260 1261 /* ATA command 0x30 (WRITE SECTORS) */ 1262 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30); 1263 1264 /* Write/execute ATA write command */ 1265 result = usbat_multiple_write(us, registers, command, 7); 1266 if (result != USB_STOR_TRANSPORT_GOOD) 1267 goto leave; 1268 1269 /* Write the data */ 1270 result = usbat_write_blocks(us, buffer, len, 0); 1271 if (result != USB_STOR_TRANSPORT_GOOD) 1272 goto leave; 1273 1274 sector += thistime; 1275 totallen -= len; 1276 } while (totallen > 0); 1277 1278 kfree(buffer); 1279 return result; 1280 1281 leave: 1282 kfree(buffer); 1283 return USB_STOR_TRANSPORT_ERROR; 1284 } 1285 1286 /* 1287 * Squeeze a potentially huge (> 65535 byte) read10 command into 1288 * a little ( <= 65535 byte) ATAPI pipe 1289 */ 1290 static int usbat_hp8200e_handle_read10(struct us_data *us, 1291 unsigned char *registers, 1292 unsigned char *data, 1293 struct scsi_cmnd *srb) 1294 { 1295 int result = USB_STOR_TRANSPORT_GOOD; 1296 unsigned char *buffer; 1297 unsigned int len; 1298 unsigned int sector; 1299 unsigned int sg_offset = 0; 1300 struct scatterlist *sg = NULL; 1301 1302 usb_stor_dbg(us, "transfersize %d\n", srb->transfersize); 1303 1304 if (scsi_bufflen(srb) < 0x10000) { 1305 1306 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA, 1307 registers, data, 19, 1308 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD, 1309 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ), 1310 DMA_FROM_DEVICE, 1311 scsi_sglist(srb), 1312 scsi_bufflen(srb), scsi_sg_count(srb), 1); 1313 1314 return result; 1315 } 1316 1317 /* 1318 * Since we're requesting more data than we can handle in 1319 * a single read command (max is 64k-1), we will perform 1320 * multiple reads, but each read must be in multiples of 1321 * a sector. Luckily the sector size is in srb->transfersize 1322 * (see linux/drivers/scsi/sr.c). 1323 */ 1324 1325 if (data[7+0] == GPCMD_READ_CD) { 1326 len = short_pack(data[7+9], data[7+8]); 1327 len <<= 16; 1328 len |= data[7+7]; 1329 usb_stor_dbg(us, "GPCMD_READ_CD: len %d\n", len); 1330 srb->transfersize = scsi_bufflen(srb)/len; 1331 } 1332 1333 if (!srb->transfersize) { 1334 srb->transfersize = 2048; /* A guess */ 1335 usb_stor_dbg(us, "transfersize 0, forcing %d\n", 1336 srb->transfersize); 1337 } 1338 1339 /* 1340 * Since we only read in one block at a time, we have to create 1341 * a bounce buffer and move the data a piece at a time between the 1342 * bounce buffer and the actual transfer buffer. 1343 */ 1344 1345 len = (65535/srb->transfersize) * srb->transfersize; 1346 usb_stor_dbg(us, "Max read is %d bytes\n", len); 1347 len = min(len, scsi_bufflen(srb)); 1348 buffer = kmalloc(len, GFP_NOIO); 1349 if (buffer == NULL) /* bloody hell! */ 1350 return USB_STOR_TRANSPORT_FAILED; 1351 sector = short_pack(data[7+3], data[7+2]); 1352 sector <<= 16; 1353 sector |= short_pack(data[7+5], data[7+4]); 1354 transferred = 0; 1355 1356 while (transferred != scsi_bufflen(srb)) { 1357 1358 if (len > scsi_bufflen(srb) - transferred) 1359 len = scsi_bufflen(srb) - transferred; 1360 1361 data[3] = len&0xFF; /* (cylL) = expected length (L) */ 1362 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */ 1363 1364 /* Fix up the SCSI command sector and num sectors */ 1365 1366 data[7+2] = MSB_of(sector>>16); /* SCSI command sector */ 1367 data[7+3] = LSB_of(sector>>16); 1368 data[7+4] = MSB_of(sector&0xFFFF); 1369 data[7+5] = LSB_of(sector&0xFFFF); 1370 if (data[7+0] == GPCMD_READ_CD) 1371 data[7+6] = 0; 1372 data[7+7] = MSB_of(len / srb->transfersize); /* SCSI command */ 1373 data[7+8] = LSB_of(len / srb->transfersize); /* num sectors */ 1374 1375 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA, 1376 registers, data, 19, 1377 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD, 1378 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ), 1379 DMA_FROM_DEVICE, 1380 buffer, 1381 len, 0, 1); 1382 1383 if (result != USB_STOR_TRANSPORT_GOOD) 1384 break; 1385 1386 /* Store the data in the transfer buffer */ 1387 usb_stor_access_xfer_buf(buffer, len, srb, 1388 &sg, &sg_offset, TO_XFER_BUF); 1389 1390 /* Update the amount transferred and the sector number */ 1391 1392 transferred += len; 1393 sector += len / srb->transfersize; 1394 1395 } /* while transferred != scsi_bufflen(srb) */ 1396 1397 kfree(buffer); 1398 return result; 1399 } 1400 1401 static int usbat_select_and_test_registers(struct us_data *us) 1402 { 1403 int selector; 1404 unsigned char *status = us->iobuf; 1405 1406 /* try device = master, then device = slave. */ 1407 for (selector = 0xA0; selector <= 0xB0; selector += 0x10) { 1408 if (usbat_write(us, USBAT_ATA, USBAT_ATA_DEVICE, selector) != 1409 USB_STOR_XFER_GOOD) 1410 return USB_STOR_TRANSPORT_ERROR; 1411 1412 if (usbat_read(us, USBAT_ATA, USBAT_ATA_STATUS, status) != 1413 USB_STOR_XFER_GOOD) 1414 return USB_STOR_TRANSPORT_ERROR; 1415 1416 if (usbat_read(us, USBAT_ATA, USBAT_ATA_DEVICE, status) != 1417 USB_STOR_XFER_GOOD) 1418 return USB_STOR_TRANSPORT_ERROR; 1419 1420 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != 1421 USB_STOR_XFER_GOOD) 1422 return USB_STOR_TRANSPORT_ERROR; 1423 1424 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) != 1425 USB_STOR_XFER_GOOD) 1426 return USB_STOR_TRANSPORT_ERROR; 1427 1428 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_ME, 0x55) != 1429 USB_STOR_XFER_GOOD) 1430 return USB_STOR_TRANSPORT_ERROR; 1431 1432 if (usbat_write(us, USBAT_ATA, USBAT_ATA_LBA_HI, 0xAA) != 1433 USB_STOR_XFER_GOOD) 1434 return USB_STOR_TRANSPORT_ERROR; 1435 1436 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != 1437 USB_STOR_XFER_GOOD) 1438 return USB_STOR_TRANSPORT_ERROR; 1439 1440 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != 1441 USB_STOR_XFER_GOOD) 1442 return USB_STOR_TRANSPORT_ERROR; 1443 } 1444 1445 return USB_STOR_TRANSPORT_GOOD; 1446 } 1447 1448 /* 1449 * Initialize the USBAT processor and the storage device 1450 */ 1451 static int init_usbat(struct us_data *us, int devicetype) 1452 { 1453 int rc; 1454 struct usbat_info *info; 1455 unsigned char subcountH = USBAT_ATA_LBA_HI; 1456 unsigned char subcountL = USBAT_ATA_LBA_ME; 1457 unsigned char *status = us->iobuf; 1458 1459 us->extra = kzalloc_obj(struct usbat_info, GFP_NOIO); 1460 if (!us->extra) 1461 return -ENOMEM; 1462 1463 info = (struct usbat_info *) (us->extra); 1464 1465 /* Enable peripheral control signals */ 1466 rc = usbat_write_user_io(us, 1467 USBAT_UIO_OE1 | USBAT_UIO_OE0, 1468 USBAT_UIO_EPAD | USBAT_UIO_1); 1469 if (rc != USB_STOR_XFER_GOOD) 1470 return -EIO; 1471 1472 usb_stor_dbg(us, "INIT 1\n"); 1473 1474 msleep(2000); 1475 1476 rc = usbat_read_user_io(us, status); 1477 if (rc != USB_STOR_TRANSPORT_GOOD) 1478 return -EIO; 1479 1480 usb_stor_dbg(us, "INIT 2\n"); 1481 1482 rc = usbat_read_user_io(us, status); 1483 if (rc != USB_STOR_XFER_GOOD) 1484 return -EIO; 1485 1486 rc = usbat_read_user_io(us, status); 1487 if (rc != USB_STOR_XFER_GOOD) 1488 return -EIO; 1489 1490 usb_stor_dbg(us, "INIT 3\n"); 1491 1492 rc = usbat_select_and_test_registers(us); 1493 if (rc != USB_STOR_TRANSPORT_GOOD) 1494 return -EIO; 1495 1496 usb_stor_dbg(us, "INIT 4\n"); 1497 1498 rc = usbat_read_user_io(us, status); 1499 if (rc != USB_STOR_XFER_GOOD) 1500 return -EIO; 1501 1502 usb_stor_dbg(us, "INIT 5\n"); 1503 1504 /* Enable peripheral control signals and card detect */ 1505 rc = usbat_device_enable_cdt(us); 1506 if (rc != USB_STOR_TRANSPORT_GOOD) 1507 return -EIO; 1508 1509 usb_stor_dbg(us, "INIT 6\n"); 1510 1511 rc = usbat_read_user_io(us, status); 1512 if (rc != USB_STOR_XFER_GOOD) 1513 return -EIO; 1514 1515 usb_stor_dbg(us, "INIT 7\n"); 1516 1517 msleep(1400); 1518 1519 rc = usbat_read_user_io(us, status); 1520 if (rc != USB_STOR_XFER_GOOD) 1521 return -EIO; 1522 1523 usb_stor_dbg(us, "INIT 8\n"); 1524 1525 rc = usbat_select_and_test_registers(us); 1526 if (rc != USB_STOR_TRANSPORT_GOOD) 1527 return -EIO; 1528 1529 usb_stor_dbg(us, "INIT 9\n"); 1530 1531 /* At this point, we need to detect which device we are using */ 1532 if (usbat_set_transport(us, info, devicetype)) 1533 return -EIO; 1534 1535 usb_stor_dbg(us, "INIT 10\n"); 1536 1537 if (usbat_get_device_type(us) == USBAT_DEV_FLASH) { 1538 subcountH = 0x02; 1539 subcountL = 0x00; 1540 } 1541 rc = usbat_set_shuttle_features(us, (USBAT_FEAT_ETEN | USBAT_FEAT_ET2 | USBAT_FEAT_ET1), 1542 0x00, 0x88, 0x08, subcountH, subcountL); 1543 if (rc != USB_STOR_XFER_GOOD) 1544 return -EIO; 1545 1546 usb_stor_dbg(us, "INIT 11\n"); 1547 1548 return 0; 1549 } 1550 1551 /* 1552 * Transport for the HP 8200e 1553 */ 1554 static int usbat_hp8200e_transport(struct scsi_cmnd *srb, struct us_data *us) 1555 { 1556 int result; 1557 unsigned char *status = us->iobuf; 1558 unsigned char registers[32]; 1559 unsigned char data[32]; 1560 unsigned int len; 1561 int i; 1562 1563 len = scsi_bufflen(srb); 1564 1565 /* 1566 * Send A0 (ATA PACKET COMMAND). 1567 * Note: I guess we're never going to get any of the ATA 1568 * commands... just ATA Packet Commands. 1569 */ 1570 1571 registers[0] = USBAT_ATA_FEATURES; 1572 registers[1] = USBAT_ATA_SECCNT; 1573 registers[2] = USBAT_ATA_SECNUM; 1574 registers[3] = USBAT_ATA_LBA_ME; 1575 registers[4] = USBAT_ATA_LBA_HI; 1576 registers[5] = USBAT_ATA_DEVICE; 1577 registers[6] = USBAT_ATA_CMD; 1578 data[0] = 0x00; 1579 data[1] = 0x00; 1580 data[2] = 0x00; 1581 data[3] = len&0xFF; /* (cylL) = expected length (L) */ 1582 data[4] = (len>>8)&0xFF; /* (cylH) = expected length (H) */ 1583 data[5] = 0xB0; /* (device sel) = slave */ 1584 data[6] = 0xA0; /* (command) = ATA PACKET COMMAND */ 1585 1586 for (i=7; i<19; i++) { 1587 registers[i] = 0x10; 1588 data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7]; 1589 } 1590 1591 result = usbat_get_status(us, status); 1592 usb_stor_dbg(us, "Status = %02X\n", *status); 1593 if (result != USB_STOR_XFER_GOOD) 1594 return USB_STOR_TRANSPORT_ERROR; 1595 if (srb->cmnd[0] == TEST_UNIT_READY) 1596 transferred = 0; 1597 1598 if (srb->sc_data_direction == DMA_TO_DEVICE) { 1599 1600 result = usbat_hp8200e_rw_block_test(us, USBAT_ATA, 1601 registers, data, 19, 1602 USBAT_ATA_DATA, USBAT_ATA_STATUS, 0xFD, 1603 (USBAT_QUAL_FCQ | USBAT_QUAL_ALQ), 1604 DMA_TO_DEVICE, 1605 scsi_sglist(srb), 1606 len, scsi_sg_count(srb), 10); 1607 1608 if (result == USB_STOR_TRANSPORT_GOOD) { 1609 transferred += len; 1610 usb_stor_dbg(us, "Wrote %08X bytes\n", transferred); 1611 } 1612 1613 return result; 1614 1615 } else if (srb->cmnd[0] == READ_10 || 1616 srb->cmnd[0] == GPCMD_READ_CD) { 1617 1618 return usbat_hp8200e_handle_read10(us, registers, data, srb); 1619 1620 } 1621 1622 if (len > 0xFFFF) { 1623 usb_stor_dbg(us, "Error: len = %08X... what do I do now?\n", 1624 len); 1625 return USB_STOR_TRANSPORT_ERROR; 1626 } 1627 1628 result = usbat_multiple_write(us, registers, data, 7); 1629 1630 if (result != USB_STOR_TRANSPORT_GOOD) 1631 return result; 1632 1633 /* 1634 * Write the 12-byte command header. 1635 * 1636 * If the command is BLANK then set the timer for 75 minutes. 1637 * Otherwise set it for 10 minutes. 1638 * 1639 * NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW 1640 * AT SPEED 4 IS UNRELIABLE!!! 1641 */ 1642 1643 result = usbat_write_block(us, USBAT_ATA, srb->cmnd, 12, 1644 srb->cmnd[0] == GPCMD_BLANK ? 75 : 10, 0); 1645 1646 if (result != USB_STOR_TRANSPORT_GOOD) 1647 return result; 1648 1649 /* If there is response data to be read in then do it here. */ 1650 1651 if (len != 0 && (srb->sc_data_direction == DMA_FROM_DEVICE)) { 1652 1653 /* How many bytes to read in? Check cylL register */ 1654 1655 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_ME, status) != 1656 USB_STOR_XFER_GOOD) { 1657 return USB_STOR_TRANSPORT_ERROR; 1658 } 1659 1660 if (len > 0xFF) { /* need to read cylH also */ 1661 len = *status; 1662 if (usbat_read(us, USBAT_ATA, USBAT_ATA_LBA_HI, status) != 1663 USB_STOR_XFER_GOOD) { 1664 return USB_STOR_TRANSPORT_ERROR; 1665 } 1666 len += ((unsigned int) *status)<<8; 1667 } 1668 else 1669 len = *status; 1670 1671 1672 result = usbat_read_block(us, scsi_sglist(srb), len, 1673 scsi_sg_count(srb)); 1674 } 1675 1676 return result; 1677 } 1678 1679 /* 1680 * Transport for USBAT02-based CompactFlash and similar storage devices 1681 */ 1682 static int usbat_flash_transport(struct scsi_cmnd * srb, struct us_data *us) 1683 { 1684 int rc; 1685 struct usbat_info *info = (struct usbat_info *) (us->extra); 1686 unsigned long block, blocks; 1687 unsigned char *ptr = us->iobuf; 1688 static const unsigned char inquiry_response[36] = { 1689 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00 1690 }; 1691 1692 if (srb->cmnd[0] == INQUIRY) { 1693 usb_stor_dbg(us, "INQUIRY - Returning bogus response\n"); 1694 memcpy(ptr, inquiry_response, sizeof(inquiry_response)); 1695 fill_inquiry_response(us, ptr, 36); 1696 return USB_STOR_TRANSPORT_GOOD; 1697 } 1698 1699 if (srb->cmnd[0] == READ_CAPACITY) { 1700 rc = usbat_flash_check_media(us, info); 1701 if (rc != USB_STOR_TRANSPORT_GOOD) 1702 return rc; 1703 1704 rc = usbat_flash_get_sector_count(us, info); 1705 if (rc != USB_STOR_TRANSPORT_GOOD) 1706 return rc; 1707 1708 usb_stor_dbg(us, "READ_CAPACITY: %ld sectors, %ld bytes per sector\n", 1709 info->sectors, info->ssize); 1710 1711 /* 1712 * build the reply 1713 * note: must return the sector number of the last sector, 1714 * *not* the total number of sectors 1715 */ 1716 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1); 1717 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize); 1718 usb_stor_set_xfer_buf(ptr, 8, srb); 1719 1720 return USB_STOR_TRANSPORT_GOOD; 1721 } 1722 1723 if (srb->cmnd[0] == MODE_SELECT_10) { 1724 usb_stor_dbg(us, "Gah! MODE_SELECT_10\n"); 1725 return USB_STOR_TRANSPORT_ERROR; 1726 } 1727 1728 if (srb->cmnd[0] == READ_10) { 1729 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | 1730 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); 1731 1732 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8])); 1733 1734 usb_stor_dbg(us, "READ_10: read block 0x%04lx count %ld\n", 1735 block, blocks); 1736 return usbat_flash_read_data(us, info, block, blocks); 1737 } 1738 1739 if (srb->cmnd[0] == READ_12) { 1740 /* 1741 * I don't think we'll ever see a READ_12 but support it anyway 1742 */ 1743 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | 1744 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); 1745 1746 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) | 1747 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9])); 1748 1749 usb_stor_dbg(us, "READ_12: read block 0x%04lx count %ld\n", 1750 block, blocks); 1751 return usbat_flash_read_data(us, info, block, blocks); 1752 } 1753 1754 if (srb->cmnd[0] == WRITE_10) { 1755 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | 1756 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); 1757 1758 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8])); 1759 1760 usb_stor_dbg(us, "WRITE_10: write block 0x%04lx count %ld\n", 1761 block, blocks); 1762 return usbat_flash_write_data(us, info, block, blocks); 1763 } 1764 1765 if (srb->cmnd[0] == WRITE_12) { 1766 /* 1767 * I don't think we'll ever see a WRITE_12 but support it anyway 1768 */ 1769 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) | 1770 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5])); 1771 1772 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) | 1773 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9])); 1774 1775 usb_stor_dbg(us, "WRITE_12: write block 0x%04lx count %ld\n", 1776 block, blocks); 1777 return usbat_flash_write_data(us, info, block, blocks); 1778 } 1779 1780 1781 if (srb->cmnd[0] == TEST_UNIT_READY) { 1782 usb_stor_dbg(us, "TEST_UNIT_READY\n"); 1783 1784 rc = usbat_flash_check_media(us, info); 1785 if (rc != USB_STOR_TRANSPORT_GOOD) 1786 return rc; 1787 1788 return usbat_check_status(us); 1789 } 1790 1791 if (srb->cmnd[0] == REQUEST_SENSE) { 1792 usb_stor_dbg(us, "REQUEST_SENSE\n"); 1793 1794 memset(ptr, 0, 18); 1795 ptr[0] = 0xF0; 1796 ptr[2] = info->sense_key; 1797 ptr[7] = 11; 1798 ptr[12] = info->sense_asc; 1799 ptr[13] = info->sense_ascq; 1800 usb_stor_set_xfer_buf(ptr, 18, srb); 1801 1802 return USB_STOR_TRANSPORT_GOOD; 1803 } 1804 1805 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) { 1806 /* 1807 * sure. whatever. not like we can stop the user from popping 1808 * the media out of the device (no locking doors, etc) 1809 */ 1810 return USB_STOR_TRANSPORT_GOOD; 1811 } 1812 1813 usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n", 1814 srb->cmnd[0], srb->cmnd[0]); 1815 info->sense_key = 0x05; 1816 info->sense_asc = 0x20; 1817 info->sense_ascq = 0x00; 1818 return USB_STOR_TRANSPORT_FAILED; 1819 } 1820 1821 static int init_usbat_cd(struct us_data *us) 1822 { 1823 return init_usbat(us, USBAT_DEV_HP8200); 1824 } 1825 1826 static int init_usbat_flash(struct us_data *us) 1827 { 1828 return init_usbat(us, USBAT_DEV_FLASH); 1829 } 1830 1831 static struct scsi_host_template usbat_host_template; 1832 1833 static int usbat_probe(struct usb_interface *intf, 1834 const struct usb_device_id *id) 1835 { 1836 struct us_data *us; 1837 int result; 1838 1839 result = usb_stor_probe1(&us, intf, id, 1840 (id - usbat_usb_ids) + usbat_unusual_dev_list, 1841 &usbat_host_template); 1842 if (result) 1843 return result; 1844 1845 /* 1846 * The actual transport will be determined later by the 1847 * initialization routine; this is just a placeholder. 1848 */ 1849 us->transport_name = "Shuttle USBAT"; 1850 us->transport = usbat_flash_transport; 1851 us->transport_reset = usb_stor_CB_reset; 1852 us->max_lun = 0; 1853 1854 result = usb_stor_probe2(us); 1855 return result; 1856 } 1857 1858 static struct usb_driver usbat_driver = { 1859 .name = DRV_NAME, 1860 .probe = usbat_probe, 1861 .disconnect = usb_stor_disconnect, 1862 .suspend = usb_stor_suspend, 1863 .resume = usb_stor_resume, 1864 .reset_resume = usb_stor_reset_resume, 1865 .pre_reset = usb_stor_pre_reset, 1866 .post_reset = usb_stor_post_reset, 1867 .id_table = usbat_usb_ids, 1868 .soft_unbind = 1, 1869 .no_dynamic_id = 1, 1870 }; 1871 1872 module_usb_stor_driver(usbat_driver, usbat_host_template, DRV_NAME); 1873