1 /* Driver for USB Mass Storage compliant devices 2 * 3 * Current development and maintenance by: 4 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 5 * 6 * Developed with the assistance of: 7 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org) 8 * (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov) 9 * (c) 2002 Alan Stern <stern@rowland.org> 10 * 11 * Initial work by: 12 * (c) 1999 Michael Gee (michael@linuxspecific.com) 13 * 14 * This driver is based on the 'USB Mass Storage Class' document. This 15 * describes in detail the protocol used to communicate with such 16 * devices. Clearly, the designers had SCSI and ATAPI commands in 17 * mind when they created this document. The commands are all very 18 * similar to commands in the SCSI-II and ATAPI specifications. 19 * 20 * It is important to note that in a number of cases this class 21 * exhibits class-specific exemptions from the USB specification. 22 * Notably the usage of NAK, STALL and ACK differs from the norm, in 23 * that they are used to communicate wait, failed and OK on commands. 24 * 25 * Also, for certain devices, the interrupt endpoint is used to convey 26 * status of a command. 27 * 28 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more 29 * information about 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 50 #include <scsi/scsi.h> 51 #include <scsi/scsi_eh.h> 52 #include <scsi/scsi_device.h> 53 54 #include "usb.h" 55 #include "transport.h" 56 #include "protocol.h" 57 #include "scsiglue.h" 58 #include "debug.h" 59 60 61 /*********************************************************************** 62 * Data transfer routines 63 ***********************************************************************/ 64 65 /* 66 * This is subtle, so pay attention: 67 * --------------------------------- 68 * We're very concerned about races with a command abort. Hanging this code 69 * is a sure fire way to hang the kernel. (Note that this discussion applies 70 * only to transactions resulting from a scsi queued-command, since only 71 * these transactions are subject to a scsi abort. Other transactions, such 72 * as those occurring during device-specific initialization, must be handled 73 * by a separate code path.) 74 * 75 * The abort function (usb_storage_command_abort() in scsiglue.c) first 76 * sets the machine state and the ABORTING bit in us->dflags to prevent 77 * new URBs from being submitted. It then calls usb_stor_stop_transport() 78 * below, which atomically tests-and-clears the URB_ACTIVE bit in us->dflags 79 * to see if the current_urb needs to be stopped. Likewise, the SG_ACTIVE 80 * bit is tested to see if the current_sg scatter-gather request needs to be 81 * stopped. The timeout callback routine does much the same thing. 82 * 83 * When a disconnect occurs, the DISCONNECTING bit in us->dflags is set to 84 * prevent new URBs from being submitted, and usb_stor_stop_transport() is 85 * called to stop any ongoing requests. 86 * 87 * The submit function first verifies that the submitting is allowed 88 * (neither ABORTING nor DISCONNECTING bits are set) and that the submit 89 * completes without errors, and only then sets the URB_ACTIVE bit. This 90 * prevents the stop_transport() function from trying to cancel the URB 91 * while the submit call is underway. Next, the submit function must test 92 * the flags to see if an abort or disconnect occurred during the submission 93 * or before the URB_ACTIVE bit was set. If so, it's essential to cancel 94 * the URB if it hasn't been cancelled already (i.e., if the URB_ACTIVE bit 95 * is still set). Either way, the function must then wait for the URB to 96 * finish. Note that the URB can still be in progress even after a call to 97 * usb_unlink_urb() returns. 98 * 99 * The idea is that (1) once the ABORTING or DISCONNECTING bit is set, 100 * either the stop_transport() function or the submitting function 101 * is guaranteed to call usb_unlink_urb() for an active URB, 102 * and (2) test_and_clear_bit() prevents usb_unlink_urb() from being 103 * called more than once or from being called during usb_submit_urb(). 104 */ 105 106 /* This is the completion handler which will wake us up when an URB 107 * completes. 108 */ 109 static void usb_stor_blocking_completion(struct urb *urb) 110 { 111 struct completion *urb_done_ptr = urb->context; 112 113 complete(urb_done_ptr); 114 } 115 116 /* This is the common part of the URB message submission code 117 * 118 * All URBs from the usb-storage driver involved in handling a queued scsi 119 * command _must_ pass through this function (or something like it) for the 120 * abort mechanisms to work properly. 121 */ 122 static int usb_stor_msg_common(struct us_data *us, int timeout) 123 { 124 struct completion urb_done; 125 long timeleft; 126 int status; 127 128 /* don't submit URBs during abort processing */ 129 if (test_bit(US_FLIDX_ABORTING, &us->dflags)) 130 return -EIO; 131 132 /* set up data structures for the wakeup system */ 133 init_completion(&urb_done); 134 135 /* fill the common fields in the URB */ 136 us->current_urb->context = &urb_done; 137 us->current_urb->actual_length = 0; 138 us->current_urb->error_count = 0; 139 us->current_urb->status = 0; 140 141 /* we assume that if transfer_buffer isn't us->iobuf then it 142 * hasn't been mapped for DMA. Yes, this is clunky, but it's 143 * easier than always having the caller tell us whether the 144 * transfer buffer has already been mapped. */ 145 us->current_urb->transfer_flags = URB_NO_SETUP_DMA_MAP; 146 if (us->current_urb->transfer_buffer == us->iobuf) 147 us->current_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 148 us->current_urb->transfer_dma = us->iobuf_dma; 149 us->current_urb->setup_dma = us->cr_dma; 150 151 /* submit the URB */ 152 status = usb_submit_urb(us->current_urb, GFP_NOIO); 153 if (status) { 154 /* something went wrong */ 155 return status; 156 } 157 158 /* since the URB has been submitted successfully, it's now okay 159 * to cancel it */ 160 set_bit(US_FLIDX_URB_ACTIVE, &us->dflags); 161 162 /* did an abort occur during the submission? */ 163 if (test_bit(US_FLIDX_ABORTING, &us->dflags)) { 164 165 /* cancel the URB, if it hasn't been cancelled already */ 166 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) { 167 US_DEBUGP("-- cancelling URB\n"); 168 usb_unlink_urb(us->current_urb); 169 } 170 } 171 172 /* wait for the completion of the URB */ 173 timeleft = wait_for_completion_interruptible_timeout( 174 &urb_done, timeout ? : MAX_SCHEDULE_TIMEOUT); 175 176 clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags); 177 178 if (timeleft <= 0) { 179 US_DEBUGP("%s -- cancelling URB\n", 180 timeleft == 0 ? "Timeout" : "Signal"); 181 usb_kill_urb(us->current_urb); 182 } 183 184 /* return the URB status */ 185 return us->current_urb->status; 186 } 187 188 /* 189 * Transfer one control message, with timeouts, and allowing early 190 * termination. Return codes are usual -Exxx, *not* USB_STOR_XFER_xxx. 191 */ 192 int usb_stor_control_msg(struct us_data *us, unsigned int pipe, 193 u8 request, u8 requesttype, u16 value, u16 index, 194 void *data, u16 size, int timeout) 195 { 196 int status; 197 198 US_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n", 199 __func__, request, requesttype, 200 value, index, size); 201 202 /* fill in the devrequest structure */ 203 us->cr->bRequestType = requesttype; 204 us->cr->bRequest = request; 205 us->cr->wValue = cpu_to_le16(value); 206 us->cr->wIndex = cpu_to_le16(index); 207 us->cr->wLength = cpu_to_le16(size); 208 209 /* fill and submit the URB */ 210 usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe, 211 (unsigned char*) us->cr, data, size, 212 usb_stor_blocking_completion, NULL); 213 status = usb_stor_msg_common(us, timeout); 214 215 /* return the actual length of the data transferred if no error */ 216 if (status == 0) 217 status = us->current_urb->actual_length; 218 return status; 219 } 220 221 /* This is a version of usb_clear_halt() that allows early termination and 222 * doesn't read the status from the device -- this is because some devices 223 * crash their internal firmware when the status is requested after a halt. 224 * 225 * A definitive list of these 'bad' devices is too difficult to maintain or 226 * make complete enough to be useful. This problem was first observed on the 227 * Hagiwara FlashGate DUAL unit. However, bus traces reveal that neither 228 * MacOS nor Windows checks the status after clearing a halt. 229 * 230 * Since many vendors in this space limit their testing to interoperability 231 * with these two OSes, specification violations like this one are common. 232 */ 233 int usb_stor_clear_halt(struct us_data *us, unsigned int pipe) 234 { 235 int result; 236 int endp = usb_pipeendpoint(pipe); 237 238 if (usb_pipein (pipe)) 239 endp |= USB_DIR_IN; 240 241 result = usb_stor_control_msg(us, us->send_ctrl_pipe, 242 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 243 USB_ENDPOINT_HALT, endp, 244 NULL, 0, 3*HZ); 245 246 /* reset the endpoint toggle */ 247 if (result >= 0) 248 usb_settoggle(us->pusb_dev, usb_pipeendpoint(pipe), 249 usb_pipeout(pipe), 0); 250 251 US_DEBUGP("%s: result = %d\n", __func__, result); 252 return result; 253 } 254 255 256 /* 257 * Interpret the results of a URB transfer 258 * 259 * This function prints appropriate debugging messages, clears halts on 260 * non-control endpoints, and translates the status to the corresponding 261 * USB_STOR_XFER_xxx return code. 262 */ 263 static int interpret_urb_result(struct us_data *us, unsigned int pipe, 264 unsigned int length, int result, unsigned int partial) 265 { 266 US_DEBUGP("Status code %d; transferred %u/%u\n", 267 result, partial, length); 268 switch (result) { 269 270 /* no error code; did we send all the data? */ 271 case 0: 272 if (partial != length) { 273 US_DEBUGP("-- short transfer\n"); 274 return USB_STOR_XFER_SHORT; 275 } 276 277 US_DEBUGP("-- transfer complete\n"); 278 return USB_STOR_XFER_GOOD; 279 280 /* stalled */ 281 case -EPIPE: 282 /* for control endpoints, (used by CB[I]) a stall indicates 283 * a failed command */ 284 if (usb_pipecontrol(pipe)) { 285 US_DEBUGP("-- stall on control pipe\n"); 286 return USB_STOR_XFER_STALLED; 287 } 288 289 /* for other sorts of endpoint, clear the stall */ 290 US_DEBUGP("clearing endpoint halt for pipe 0x%x\n", pipe); 291 if (usb_stor_clear_halt(us, pipe) < 0) 292 return USB_STOR_XFER_ERROR; 293 return USB_STOR_XFER_STALLED; 294 295 /* babble - the device tried to send more than we wanted to read */ 296 case -EOVERFLOW: 297 US_DEBUGP("-- babble\n"); 298 return USB_STOR_XFER_LONG; 299 300 /* the transfer was cancelled by abort, disconnect, or timeout */ 301 case -ECONNRESET: 302 US_DEBUGP("-- transfer cancelled\n"); 303 return USB_STOR_XFER_ERROR; 304 305 /* short scatter-gather read transfer */ 306 case -EREMOTEIO: 307 US_DEBUGP("-- short read transfer\n"); 308 return USB_STOR_XFER_SHORT; 309 310 /* abort or disconnect in progress */ 311 case -EIO: 312 US_DEBUGP("-- abort or disconnect in progress\n"); 313 return USB_STOR_XFER_ERROR; 314 315 /* the catch-all error case */ 316 default: 317 US_DEBUGP("-- unknown error\n"); 318 return USB_STOR_XFER_ERROR; 319 } 320 } 321 322 /* 323 * Transfer one control message, without timeouts, but allowing early 324 * termination. Return codes are USB_STOR_XFER_xxx. 325 */ 326 int usb_stor_ctrl_transfer(struct us_data *us, unsigned int pipe, 327 u8 request, u8 requesttype, u16 value, u16 index, 328 void *data, u16 size) 329 { 330 int result; 331 332 US_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n", 333 __func__, request, requesttype, 334 value, index, size); 335 336 /* fill in the devrequest structure */ 337 us->cr->bRequestType = requesttype; 338 us->cr->bRequest = request; 339 us->cr->wValue = cpu_to_le16(value); 340 us->cr->wIndex = cpu_to_le16(index); 341 us->cr->wLength = cpu_to_le16(size); 342 343 /* fill and submit the URB */ 344 usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe, 345 (unsigned char*) us->cr, data, size, 346 usb_stor_blocking_completion, NULL); 347 result = usb_stor_msg_common(us, 0); 348 349 return interpret_urb_result(us, pipe, size, result, 350 us->current_urb->actual_length); 351 } 352 353 /* 354 * Receive one interrupt buffer, without timeouts, but allowing early 355 * termination. Return codes are USB_STOR_XFER_xxx. 356 * 357 * This routine always uses us->recv_intr_pipe as the pipe and 358 * us->ep_bInterval as the interrupt interval. 359 */ 360 static int usb_stor_intr_transfer(struct us_data *us, void *buf, 361 unsigned int length) 362 { 363 int result; 364 unsigned int pipe = us->recv_intr_pipe; 365 unsigned int maxp; 366 367 US_DEBUGP("%s: xfer %u bytes\n", __func__, length); 368 369 /* calculate the max packet size */ 370 maxp = usb_maxpacket(us->pusb_dev, pipe, usb_pipeout(pipe)); 371 if (maxp > length) 372 maxp = length; 373 374 /* fill and submit the URB */ 375 usb_fill_int_urb(us->current_urb, us->pusb_dev, pipe, buf, 376 maxp, usb_stor_blocking_completion, NULL, 377 us->ep_bInterval); 378 result = usb_stor_msg_common(us, 0); 379 380 return interpret_urb_result(us, pipe, length, result, 381 us->current_urb->actual_length); 382 } 383 384 /* 385 * Transfer one buffer via bulk pipe, without timeouts, but allowing early 386 * termination. Return codes are USB_STOR_XFER_xxx. If the bulk pipe 387 * stalls during the transfer, the halt is automatically cleared. 388 */ 389 int usb_stor_bulk_transfer_buf(struct us_data *us, unsigned int pipe, 390 void *buf, unsigned int length, unsigned int *act_len) 391 { 392 int result; 393 394 US_DEBUGP("%s: xfer %u bytes\n", __func__, length); 395 396 /* fill and submit the URB */ 397 usb_fill_bulk_urb(us->current_urb, us->pusb_dev, pipe, buf, length, 398 usb_stor_blocking_completion, NULL); 399 result = usb_stor_msg_common(us, 0); 400 401 /* store the actual length of the data transferred */ 402 if (act_len) 403 *act_len = us->current_urb->actual_length; 404 return interpret_urb_result(us, pipe, length, result, 405 us->current_urb->actual_length); 406 } 407 408 /* 409 * Transfer a scatter-gather list via bulk transfer 410 * 411 * This function does basically the same thing as usb_stor_bulk_transfer_buf() 412 * above, but it uses the usbcore scatter-gather library. 413 */ 414 static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe, 415 struct scatterlist *sg, int num_sg, unsigned int length, 416 unsigned int *act_len) 417 { 418 int result; 419 420 /* don't submit s-g requests during abort processing */ 421 if (test_bit(US_FLIDX_ABORTING, &us->dflags)) 422 return USB_STOR_XFER_ERROR; 423 424 /* initialize the scatter-gather request block */ 425 US_DEBUGP("%s: xfer %u bytes, %d entries\n", __func__, 426 length, num_sg); 427 result = usb_sg_init(&us->current_sg, us->pusb_dev, pipe, 0, 428 sg, num_sg, length, GFP_NOIO); 429 if (result) { 430 US_DEBUGP("usb_sg_init returned %d\n", result); 431 return USB_STOR_XFER_ERROR; 432 } 433 434 /* since the block has been initialized successfully, it's now 435 * okay to cancel it */ 436 set_bit(US_FLIDX_SG_ACTIVE, &us->dflags); 437 438 /* did an abort occur during the submission? */ 439 if (test_bit(US_FLIDX_ABORTING, &us->dflags)) { 440 441 /* cancel the request, if it hasn't been cancelled already */ 442 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) { 443 US_DEBUGP("-- cancelling sg request\n"); 444 usb_sg_cancel(&us->current_sg); 445 } 446 } 447 448 /* wait for the completion of the transfer */ 449 usb_sg_wait(&us->current_sg); 450 clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags); 451 452 result = us->current_sg.status; 453 if (act_len) 454 *act_len = us->current_sg.bytes; 455 return interpret_urb_result(us, pipe, length, result, 456 us->current_sg.bytes); 457 } 458 459 /* 460 * Common used function. Transfer a complete command 461 * via usb_stor_bulk_transfer_sglist() above. Set cmnd resid 462 */ 463 int usb_stor_bulk_srb(struct us_data* us, unsigned int pipe, 464 struct scsi_cmnd* srb) 465 { 466 unsigned int partial; 467 int result = usb_stor_bulk_transfer_sglist(us, pipe, scsi_sglist(srb), 468 scsi_sg_count(srb), scsi_bufflen(srb), 469 &partial); 470 471 scsi_set_resid(srb, scsi_bufflen(srb) - partial); 472 return result; 473 } 474 475 /* 476 * Transfer an entire SCSI command's worth of data payload over the bulk 477 * pipe. 478 * 479 * Note that this uses usb_stor_bulk_transfer_buf() and 480 * usb_stor_bulk_transfer_sglist() to achieve its goals -- 481 * this function simply determines whether we're going to use 482 * scatter-gather or not, and acts appropriately. 483 */ 484 int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe, 485 void *buf, unsigned int length_left, int use_sg, int *residual) 486 { 487 int result; 488 unsigned int partial; 489 490 /* are we scatter-gathering? */ 491 if (use_sg) { 492 /* use the usb core scatter-gather primitives */ 493 result = usb_stor_bulk_transfer_sglist(us, pipe, 494 (struct scatterlist *) buf, use_sg, 495 length_left, &partial); 496 length_left -= partial; 497 } else { 498 /* no scatter-gather, just make the request */ 499 result = usb_stor_bulk_transfer_buf(us, pipe, buf, 500 length_left, &partial); 501 length_left -= partial; 502 } 503 504 /* store the residual and return the error code */ 505 if (residual) 506 *residual = length_left; 507 return result; 508 } 509 510 /*********************************************************************** 511 * Transport routines 512 ***********************************************************************/ 513 514 /* Invoke the transport and basic error-handling/recovery methods 515 * 516 * This is used by the protocol layers to actually send the message to 517 * the device and receive the response. 518 */ 519 void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us) 520 { 521 int need_auto_sense; 522 int result; 523 524 /* send the command to the transport layer */ 525 scsi_set_resid(srb, 0); 526 result = us->transport(srb, us); 527 528 /* if the command gets aborted by the higher layers, we need to 529 * short-circuit all other processing 530 */ 531 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) { 532 US_DEBUGP("-- command was aborted\n"); 533 srb->result = DID_ABORT << 16; 534 goto Handle_Errors; 535 } 536 537 /* if there is a transport error, reset and don't auto-sense */ 538 if (result == USB_STOR_TRANSPORT_ERROR) { 539 US_DEBUGP("-- transport indicates error, resetting\n"); 540 srb->result = DID_ERROR << 16; 541 goto Handle_Errors; 542 } 543 544 /* if the transport provided its own sense data, don't auto-sense */ 545 if (result == USB_STOR_TRANSPORT_NO_SENSE) { 546 srb->result = SAM_STAT_CHECK_CONDITION; 547 return; 548 } 549 550 srb->result = SAM_STAT_GOOD; 551 552 /* Determine if we need to auto-sense 553 * 554 * I normally don't use a flag like this, but it's almost impossible 555 * to understand what's going on here if I don't. 556 */ 557 need_auto_sense = 0; 558 559 /* 560 * If we're running the CB transport, which is incapable 561 * of determining status on its own, we will auto-sense 562 * unless the operation involved a data-in transfer. Devices 563 * can signal most data-in errors by stalling the bulk-in pipe. 564 */ 565 if ((us->protocol == US_PR_CB || us->protocol == US_PR_DPCM_USB) && 566 srb->sc_data_direction != DMA_FROM_DEVICE) { 567 US_DEBUGP("-- CB transport device requiring auto-sense\n"); 568 need_auto_sense = 1; 569 } 570 571 /* 572 * If we have a failure, we're going to do a REQUEST_SENSE 573 * automatically. Note that we differentiate between a command 574 * "failure" and an "error" in the transport mechanism. 575 */ 576 if (result == USB_STOR_TRANSPORT_FAILED) { 577 US_DEBUGP("-- transport indicates command failure\n"); 578 need_auto_sense = 1; 579 } 580 581 /* 582 * A short transfer on a command where we don't expect it 583 * is unusual, but it doesn't mean we need to auto-sense. 584 */ 585 if ((scsi_get_resid(srb) > 0) && 586 !((srb->cmnd[0] == REQUEST_SENSE) || 587 (srb->cmnd[0] == INQUIRY) || 588 (srb->cmnd[0] == MODE_SENSE) || 589 (srb->cmnd[0] == LOG_SENSE) || 590 (srb->cmnd[0] == MODE_SENSE_10))) { 591 US_DEBUGP("-- unexpectedly short transfer\n"); 592 } 593 594 /* Now, if we need to do the auto-sense, let's do it */ 595 if (need_auto_sense) { 596 int temp_result; 597 struct scsi_eh_save ses; 598 599 US_DEBUGP("Issuing auto-REQUEST_SENSE\n"); 600 601 scsi_eh_prep_cmnd(srb, &ses, NULL, 0, US_SENSE_SIZE); 602 603 /* FIXME: we must do the protocol translation here */ 604 if (us->subclass == US_SC_RBC || us->subclass == US_SC_SCSI || 605 us->subclass == US_SC_CYP_ATACB) 606 srb->cmd_len = 6; 607 else 608 srb->cmd_len = 12; 609 610 /* issue the auto-sense command */ 611 scsi_set_resid(srb, 0); 612 temp_result = us->transport(us->srb, us); 613 614 /* let's clean up right away */ 615 scsi_eh_restore_cmnd(srb, &ses); 616 617 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) { 618 US_DEBUGP("-- auto-sense aborted\n"); 619 srb->result = DID_ABORT << 16; 620 goto Handle_Errors; 621 } 622 if (temp_result != USB_STOR_TRANSPORT_GOOD) { 623 US_DEBUGP("-- auto-sense failure\n"); 624 625 /* we skip the reset if this happens to be a 626 * multi-target device, since failure of an 627 * auto-sense is perfectly valid 628 */ 629 srb->result = DID_ERROR << 16; 630 if (!(us->fflags & US_FL_SCM_MULT_TARG)) 631 goto Handle_Errors; 632 return; 633 } 634 635 US_DEBUGP("-- Result from auto-sense is %d\n", temp_result); 636 US_DEBUGP("-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n", 637 srb->sense_buffer[0], 638 srb->sense_buffer[2] & 0xf, 639 srb->sense_buffer[12], 640 srb->sense_buffer[13]); 641 #ifdef CONFIG_USB_STORAGE_DEBUG 642 usb_stor_show_sense( 643 srb->sense_buffer[2] & 0xf, 644 srb->sense_buffer[12], 645 srb->sense_buffer[13]); 646 #endif 647 648 /* set the result so the higher layers expect this data */ 649 srb->result = SAM_STAT_CHECK_CONDITION; 650 651 /* If things are really okay, then let's show that. Zero 652 * out the sense buffer so the higher layers won't realize 653 * we did an unsolicited auto-sense. */ 654 if (result == USB_STOR_TRANSPORT_GOOD && 655 /* Filemark 0, ignore EOM, ILI 0, no sense */ 656 (srb->sense_buffer[2] & 0xaf) == 0 && 657 /* No ASC or ASCQ */ 658 srb->sense_buffer[12] == 0 && 659 srb->sense_buffer[13] == 0) { 660 srb->result = SAM_STAT_GOOD; 661 srb->sense_buffer[0] = 0x0; 662 } 663 } 664 665 /* Did we transfer less than the minimum amount required? */ 666 if (srb->result == SAM_STAT_GOOD && 667 scsi_bufflen(srb) - scsi_get_resid(srb) < srb->underflow) 668 srb->result = (DID_ERROR << 16) | (SUGGEST_RETRY << 24); 669 670 return; 671 672 /* Error and abort processing: try to resynchronize with the device 673 * by issuing a port reset. If that fails, try a class-specific 674 * device reset. */ 675 Handle_Errors: 676 677 /* Set the RESETTING bit, and clear the ABORTING bit so that 678 * the reset may proceed. */ 679 scsi_lock(us_to_host(us)); 680 set_bit(US_FLIDX_RESETTING, &us->dflags); 681 clear_bit(US_FLIDX_ABORTING, &us->dflags); 682 scsi_unlock(us_to_host(us)); 683 684 /* We must release the device lock because the pre_reset routine 685 * will want to acquire it. */ 686 mutex_unlock(&us->dev_mutex); 687 result = usb_stor_port_reset(us); 688 mutex_lock(&us->dev_mutex); 689 690 if (result < 0) { 691 scsi_lock(us_to_host(us)); 692 usb_stor_report_device_reset(us); 693 scsi_unlock(us_to_host(us)); 694 us->transport_reset(us); 695 } 696 clear_bit(US_FLIDX_RESETTING, &us->dflags); 697 } 698 699 /* Stop the current URB transfer */ 700 void usb_stor_stop_transport(struct us_data *us) 701 { 702 US_DEBUGP("%s called\n", __func__); 703 704 /* If the state machine is blocked waiting for an URB, 705 * let's wake it up. The test_and_clear_bit() call 706 * guarantees that if a URB has just been submitted, 707 * it won't be cancelled more than once. */ 708 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) { 709 US_DEBUGP("-- cancelling URB\n"); 710 usb_unlink_urb(us->current_urb); 711 } 712 713 /* If we are waiting for a scatter-gather operation, cancel it. */ 714 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) { 715 US_DEBUGP("-- cancelling sg request\n"); 716 usb_sg_cancel(&us->current_sg); 717 } 718 } 719 720 /* 721 * Control/Bulk/Interrupt transport 722 */ 723 724 int usb_stor_CBI_transport(struct scsi_cmnd *srb, struct us_data *us) 725 { 726 unsigned int transfer_length = scsi_bufflen(srb); 727 unsigned int pipe = 0; 728 int result; 729 730 /* COMMAND STAGE */ 731 /* let's send the command via the control pipe */ 732 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe, 733 US_CBI_ADSC, 734 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0, 735 us->ifnum, srb->cmnd, srb->cmd_len); 736 737 /* check the return code for the command */ 738 US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result); 739 740 /* if we stalled the command, it means command failed */ 741 if (result == USB_STOR_XFER_STALLED) { 742 return USB_STOR_TRANSPORT_FAILED; 743 } 744 745 /* Uh oh... serious problem here */ 746 if (result != USB_STOR_XFER_GOOD) { 747 return USB_STOR_TRANSPORT_ERROR; 748 } 749 750 /* DATA STAGE */ 751 /* transfer the data payload for this command, if one exists*/ 752 if (transfer_length) { 753 pipe = srb->sc_data_direction == DMA_FROM_DEVICE ? 754 us->recv_bulk_pipe : us->send_bulk_pipe; 755 result = usb_stor_bulk_srb(us, pipe, srb); 756 US_DEBUGP("CBI data stage result is 0x%x\n", result); 757 758 /* if we stalled the data transfer it means command failed */ 759 if (result == USB_STOR_XFER_STALLED) 760 return USB_STOR_TRANSPORT_FAILED; 761 if (result > USB_STOR_XFER_STALLED) 762 return USB_STOR_TRANSPORT_ERROR; 763 } 764 765 /* STATUS STAGE */ 766 result = usb_stor_intr_transfer(us, us->iobuf, 2); 767 US_DEBUGP("Got interrupt data (0x%x, 0x%x)\n", 768 us->iobuf[0], us->iobuf[1]); 769 if (result != USB_STOR_XFER_GOOD) 770 return USB_STOR_TRANSPORT_ERROR; 771 772 /* UFI gives us ASC and ASCQ, like a request sense 773 * 774 * REQUEST_SENSE and INQUIRY don't affect the sense data on UFI 775 * devices, so we ignore the information for those commands. Note 776 * that this means we could be ignoring a real error on these 777 * commands, but that can't be helped. 778 */ 779 if (us->subclass == US_SC_UFI) { 780 if (srb->cmnd[0] == REQUEST_SENSE || 781 srb->cmnd[0] == INQUIRY) 782 return USB_STOR_TRANSPORT_GOOD; 783 if (us->iobuf[0]) 784 goto Failed; 785 return USB_STOR_TRANSPORT_GOOD; 786 } 787 788 /* If not UFI, we interpret the data as a result code 789 * The first byte should always be a 0x0. 790 * 791 * Some bogus devices don't follow that rule. They stuff the ASC 792 * into the first byte -- so if it's non-zero, call it a failure. 793 */ 794 if (us->iobuf[0]) { 795 US_DEBUGP("CBI IRQ data showed reserved bType 0x%x\n", 796 us->iobuf[0]); 797 goto Failed; 798 799 } 800 801 /* The second byte & 0x0F should be 0x0 for good, otherwise error */ 802 switch (us->iobuf[1] & 0x0F) { 803 case 0x00: 804 return USB_STOR_TRANSPORT_GOOD; 805 case 0x01: 806 goto Failed; 807 } 808 return USB_STOR_TRANSPORT_ERROR; 809 810 /* the CBI spec requires that the bulk pipe must be cleared 811 * following any data-in/out command failure (section 2.4.3.1.3) 812 */ 813 Failed: 814 if (pipe) 815 usb_stor_clear_halt(us, pipe); 816 return USB_STOR_TRANSPORT_FAILED; 817 } 818 819 /* 820 * Control/Bulk transport 821 */ 822 int usb_stor_CB_transport(struct scsi_cmnd *srb, struct us_data *us) 823 { 824 unsigned int transfer_length = scsi_bufflen(srb); 825 int result; 826 827 /* COMMAND STAGE */ 828 /* let's send the command via the control pipe */ 829 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe, 830 US_CBI_ADSC, 831 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0, 832 us->ifnum, srb->cmnd, srb->cmd_len); 833 834 /* check the return code for the command */ 835 US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result); 836 837 /* if we stalled the command, it means command failed */ 838 if (result == USB_STOR_XFER_STALLED) { 839 return USB_STOR_TRANSPORT_FAILED; 840 } 841 842 /* Uh oh... serious problem here */ 843 if (result != USB_STOR_XFER_GOOD) { 844 return USB_STOR_TRANSPORT_ERROR; 845 } 846 847 /* DATA STAGE */ 848 /* transfer the data payload for this command, if one exists*/ 849 if (transfer_length) { 850 unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ? 851 us->recv_bulk_pipe : us->send_bulk_pipe; 852 result = usb_stor_bulk_srb(us, pipe, srb); 853 US_DEBUGP("CB data stage result is 0x%x\n", result); 854 855 /* if we stalled the data transfer it means command failed */ 856 if (result == USB_STOR_XFER_STALLED) 857 return USB_STOR_TRANSPORT_FAILED; 858 if (result > USB_STOR_XFER_STALLED) 859 return USB_STOR_TRANSPORT_ERROR; 860 } 861 862 /* STATUS STAGE */ 863 /* NOTE: CB does not have a status stage. Silly, I know. So 864 * we have to catch this at a higher level. 865 */ 866 return USB_STOR_TRANSPORT_GOOD; 867 } 868 869 /* 870 * Bulk only transport 871 */ 872 873 /* Determine what the maximum LUN supported is */ 874 int usb_stor_Bulk_max_lun(struct us_data *us) 875 { 876 int result; 877 878 /* issue the command */ 879 us->iobuf[0] = 0; 880 result = usb_stor_control_msg(us, us->recv_ctrl_pipe, 881 US_BULK_GET_MAX_LUN, 882 USB_DIR_IN | USB_TYPE_CLASS | 883 USB_RECIP_INTERFACE, 884 0, us->ifnum, us->iobuf, 1, HZ); 885 886 US_DEBUGP("GetMaxLUN command result is %d, data is %d\n", 887 result, us->iobuf[0]); 888 889 /* if we have a successful request, return the result */ 890 if (result > 0) 891 return us->iobuf[0]; 892 893 /* 894 * Some devices don't like GetMaxLUN. They may STALL the control 895 * pipe, they may return a zero-length result, they may do nothing at 896 * all and timeout, or they may fail in even more bizarrely creative 897 * ways. In these cases the best approach is to use the default 898 * value: only one LUN. 899 */ 900 return 0; 901 } 902 903 int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us) 904 { 905 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf; 906 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf; 907 unsigned int transfer_length = scsi_bufflen(srb); 908 unsigned int residue; 909 int result; 910 int fake_sense = 0; 911 unsigned int cswlen; 912 unsigned int cbwlen = US_BULK_CB_WRAP_LEN; 913 914 /* Take care of BULK32 devices; set extra byte to 0 */ 915 if (unlikely(us->fflags & US_FL_BULK32)) { 916 cbwlen = 32; 917 us->iobuf[31] = 0; 918 } 919 920 /* set up the command wrapper */ 921 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); 922 bcb->DataTransferLength = cpu_to_le32(transfer_length); 923 bcb->Flags = srb->sc_data_direction == DMA_FROM_DEVICE ? 1 << 7 : 0; 924 bcb->Tag = ++us->tag; 925 bcb->Lun = srb->device->lun; 926 if (us->fflags & US_FL_SCM_MULT_TARG) 927 bcb->Lun |= srb->device->id << 4; 928 bcb->Length = srb->cmd_len; 929 930 /* copy the command payload */ 931 memset(bcb->CDB, 0, sizeof(bcb->CDB)); 932 memcpy(bcb->CDB, srb->cmnd, bcb->Length); 933 934 /* send it to out endpoint */ 935 US_DEBUGP("Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n", 936 le32_to_cpu(bcb->Signature), bcb->Tag, 937 le32_to_cpu(bcb->DataTransferLength), bcb->Flags, 938 (bcb->Lun >> 4), (bcb->Lun & 0x0F), 939 bcb->Length); 940 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, 941 bcb, cbwlen, NULL); 942 US_DEBUGP("Bulk command transfer result=%d\n", result); 943 if (result != USB_STOR_XFER_GOOD) 944 return USB_STOR_TRANSPORT_ERROR; 945 946 /* DATA STAGE */ 947 /* send/receive data payload, if there is any */ 948 949 /* Some USB-IDE converter chips need a 100us delay between the 950 * command phase and the data phase. Some devices need a little 951 * more than that, probably because of clock rate inaccuracies. */ 952 if (unlikely(us->fflags & US_FL_GO_SLOW)) 953 udelay(125); 954 955 if (transfer_length) { 956 unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ? 957 us->recv_bulk_pipe : us->send_bulk_pipe; 958 result = usb_stor_bulk_srb(us, pipe, srb); 959 US_DEBUGP("Bulk data transfer result 0x%x\n", result); 960 if (result == USB_STOR_XFER_ERROR) 961 return USB_STOR_TRANSPORT_ERROR; 962 963 /* If the device tried to send back more data than the 964 * amount requested, the spec requires us to transfer 965 * the CSW anyway. Since there's no point retrying the 966 * the command, we'll return fake sense data indicating 967 * Illegal Request, Invalid Field in CDB. 968 */ 969 if (result == USB_STOR_XFER_LONG) 970 fake_sense = 1; 971 } 972 973 /* See flow chart on pg 15 of the Bulk Only Transport spec for 974 * an explanation of how this code works. 975 */ 976 977 /* get CSW for device status */ 978 US_DEBUGP("Attempting to get CSW...\n"); 979 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, 980 bcs, US_BULK_CS_WRAP_LEN, &cswlen); 981 982 /* Some broken devices add unnecessary zero-length packets to the 983 * end of their data transfers. Such packets show up as 0-length 984 * CSWs. If we encounter such a thing, try to read the CSW again. 985 */ 986 if (result == USB_STOR_XFER_SHORT && cswlen == 0) { 987 US_DEBUGP("Received 0-length CSW; retrying...\n"); 988 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, 989 bcs, US_BULK_CS_WRAP_LEN, &cswlen); 990 } 991 992 /* did the attempt to read the CSW fail? */ 993 if (result == USB_STOR_XFER_STALLED) { 994 995 /* get the status again */ 996 US_DEBUGP("Attempting to get CSW (2nd try)...\n"); 997 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, 998 bcs, US_BULK_CS_WRAP_LEN, NULL); 999 } 1000 1001 /* if we still have a failure at this point, we're in trouble */ 1002 US_DEBUGP("Bulk status result = %d\n", result); 1003 if (result != USB_STOR_XFER_GOOD) 1004 return USB_STOR_TRANSPORT_ERROR; 1005 1006 /* check bulk status */ 1007 residue = le32_to_cpu(bcs->Residue); 1008 US_DEBUGP("Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n", 1009 le32_to_cpu(bcs->Signature), bcs->Tag, 1010 residue, bcs->Status); 1011 if (!(bcs->Tag == us->tag || (us->fflags & US_FL_BULK_IGNORE_TAG)) || 1012 bcs->Status > US_BULK_STAT_PHASE) { 1013 US_DEBUGP("Bulk logical error\n"); 1014 return USB_STOR_TRANSPORT_ERROR; 1015 } 1016 1017 /* Some broken devices report odd signatures, so we do not check them 1018 * for validity against the spec. We store the first one we see, 1019 * and check subsequent transfers for validity against this signature. 1020 */ 1021 if (!us->bcs_signature) { 1022 us->bcs_signature = bcs->Signature; 1023 if (us->bcs_signature != cpu_to_le32(US_BULK_CS_SIGN)) 1024 US_DEBUGP("Learnt BCS signature 0x%08X\n", 1025 le32_to_cpu(us->bcs_signature)); 1026 } else if (bcs->Signature != us->bcs_signature) { 1027 US_DEBUGP("Signature mismatch: got %08X, expecting %08X\n", 1028 le32_to_cpu(bcs->Signature), 1029 le32_to_cpu(us->bcs_signature)); 1030 return USB_STOR_TRANSPORT_ERROR; 1031 } 1032 1033 /* try to compute the actual residue, based on how much data 1034 * was really transferred and what the device tells us */ 1035 if (residue) { 1036 if (!(us->fflags & US_FL_IGNORE_RESIDUE)) { 1037 residue = min(residue, transfer_length); 1038 scsi_set_resid(srb, max(scsi_get_resid(srb), 1039 (int) residue)); 1040 } 1041 } 1042 1043 /* based on the status code, we report good or bad */ 1044 switch (bcs->Status) { 1045 case US_BULK_STAT_OK: 1046 /* device babbled -- return fake sense data */ 1047 if (fake_sense) { 1048 memcpy(srb->sense_buffer, 1049 usb_stor_sense_invalidCDB, 1050 sizeof(usb_stor_sense_invalidCDB)); 1051 return USB_STOR_TRANSPORT_NO_SENSE; 1052 } 1053 1054 /* command good -- note that data could be short */ 1055 return USB_STOR_TRANSPORT_GOOD; 1056 1057 case US_BULK_STAT_FAIL: 1058 /* command failed */ 1059 return USB_STOR_TRANSPORT_FAILED; 1060 1061 case US_BULK_STAT_PHASE: 1062 /* phase error -- note that a transport reset will be 1063 * invoked by the invoke_transport() function 1064 */ 1065 return USB_STOR_TRANSPORT_ERROR; 1066 } 1067 1068 /* we should never get here, but if we do, we're in trouble */ 1069 return USB_STOR_TRANSPORT_ERROR; 1070 } 1071 1072 /*********************************************************************** 1073 * Reset routines 1074 ***********************************************************************/ 1075 1076 /* This is the common part of the device reset code. 1077 * 1078 * It's handy that every transport mechanism uses the control endpoint for 1079 * resets. 1080 * 1081 * Basically, we send a reset with a 5-second timeout, so we don't get 1082 * jammed attempting to do the reset. 1083 */ 1084 static int usb_stor_reset_common(struct us_data *us, 1085 u8 request, u8 requesttype, 1086 u16 value, u16 index, void *data, u16 size) 1087 { 1088 int result; 1089 int result2; 1090 1091 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) { 1092 US_DEBUGP("No reset during disconnect\n"); 1093 return -EIO; 1094 } 1095 1096 result = usb_stor_control_msg(us, us->send_ctrl_pipe, 1097 request, requesttype, value, index, data, size, 1098 5*HZ); 1099 if (result < 0) { 1100 US_DEBUGP("Soft reset failed: %d\n", result); 1101 return result; 1102 } 1103 1104 /* Give the device some time to recover from the reset, 1105 * but don't delay disconnect processing. */ 1106 wait_event_interruptible_timeout(us->delay_wait, 1107 test_bit(US_FLIDX_DISCONNECTING, &us->dflags), 1108 HZ*6); 1109 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) { 1110 US_DEBUGP("Reset interrupted by disconnect\n"); 1111 return -EIO; 1112 } 1113 1114 US_DEBUGP("Soft reset: clearing bulk-in endpoint halt\n"); 1115 result = usb_stor_clear_halt(us, us->recv_bulk_pipe); 1116 1117 US_DEBUGP("Soft reset: clearing bulk-out endpoint halt\n"); 1118 result2 = usb_stor_clear_halt(us, us->send_bulk_pipe); 1119 1120 /* return a result code based on the result of the clear-halts */ 1121 if (result >= 0) 1122 result = result2; 1123 if (result < 0) 1124 US_DEBUGP("Soft reset failed\n"); 1125 else 1126 US_DEBUGP("Soft reset done\n"); 1127 return result; 1128 } 1129 1130 /* This issues a CB[I] Reset to the device in question 1131 */ 1132 #define CB_RESET_CMD_SIZE 12 1133 1134 int usb_stor_CB_reset(struct us_data *us) 1135 { 1136 US_DEBUGP("%s called\n", __func__); 1137 1138 memset(us->iobuf, 0xFF, CB_RESET_CMD_SIZE); 1139 us->iobuf[0] = SEND_DIAGNOSTIC; 1140 us->iobuf[1] = 4; 1141 return usb_stor_reset_common(us, US_CBI_ADSC, 1142 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 1143 0, us->ifnum, us->iobuf, CB_RESET_CMD_SIZE); 1144 } 1145 1146 /* This issues a Bulk-only Reset to the device in question, including 1147 * clearing the subsequent endpoint halts that may occur. 1148 */ 1149 int usb_stor_Bulk_reset(struct us_data *us) 1150 { 1151 US_DEBUGP("%s called\n", __func__); 1152 1153 return usb_stor_reset_common(us, US_BULK_RESET_REQUEST, 1154 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 1155 0, us->ifnum, NULL, 0); 1156 } 1157 1158 /* Issue a USB port reset to the device. The caller must not hold 1159 * us->dev_mutex. 1160 */ 1161 int usb_stor_port_reset(struct us_data *us) 1162 { 1163 int result, rc_lock; 1164 1165 result = rc_lock = 1166 usb_lock_device_for_reset(us->pusb_dev, us->pusb_intf); 1167 if (result < 0) 1168 US_DEBUGP("unable to lock device for reset: %d\n", result); 1169 else { 1170 /* Were we disconnected while waiting for the lock? */ 1171 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) { 1172 result = -EIO; 1173 US_DEBUGP("No reset during disconnect\n"); 1174 } else { 1175 result = usb_reset_device(us->pusb_dev); 1176 US_DEBUGP("usb_reset_device returns %d\n", 1177 result); 1178 } 1179 if (rc_lock) 1180 usb_unlock_device(us->pusb_dev); 1181 } 1182 return result; 1183 } 1184