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