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