1 /* Driver for USB Mass Storage compliant devices
3 * Current development and maintenance by:
4 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
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>
12 * (c) 1999 Michael Gee (michael@linuxspecific.com)
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.
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.
25 * Also, for certain devices, the interrupt endpoint is used to convey
26 * status of a command.
28 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
29 * information about this driver.
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
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.
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.
46 #include <linux/sched.h>
47 #include <linux/errno.h>
48 #include <linux/slab.h>
50 #include <scsi/scsi.h>
51 #include <scsi/scsi_eh.h>
52 #include <scsi/scsi_device.h>
55 #include "transport.h"
61 /***********************************************************************
62 * Data transfer routines
63 ***********************************************************************/
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.)
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.
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.
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.
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().
106 /* This is the completion handler which will wake us up when an URB
109 static void usb_stor_blocking_completion(struct urb
*urb
)
111 struct completion
*urb_done_ptr
= urb
->context
;
113 complete(urb_done_ptr
);
116 /* This is the common part of the URB message submission code
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.
122 static int usb_stor_msg_common(struct us_data
*us
, int timeout
)
124 struct completion urb_done
;
128 /* don't submit URBs during abort processing */
129 if (test_bit(US_FLIDX_ABORTING
, &us
->dflags
))
132 /* set up data structures for the wakeup system */
133 init_completion(&urb_done
);
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;
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
;
152 status
= usb_submit_urb(us
->current_urb
, GFP_NOIO
);
154 /* something went wrong */
158 /* since the URB has been submitted successfully, it's now okay
160 set_bit(US_FLIDX_URB_ACTIVE
, &us
->dflags
);
162 /* did an abort occur during the submission? */
163 if (test_bit(US_FLIDX_ABORTING
, &us
->dflags
)) {
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
);
172 /* wait for the completion of the URB */
173 timeleft
= wait_for_completion_interruptible_timeout(
174 &urb_done
, timeout
? : MAX_SCHEDULE_TIMEOUT
);
176 clear_bit(US_FLIDX_URB_ACTIVE
, &us
->dflags
);
179 US_DEBUGP("%s -- cancelling URB\n",
180 timeleft
== 0 ? "Timeout" : "Signal");
181 usb_kill_urb(us
->current_urb
);
184 /* return the URB status */
185 return us
->current_urb
->status
;
189 * Transfer one control message, with timeouts, and allowing early
190 * termination. Return codes are usual -Exxx, *not* USB_STOR_XFER_xxx.
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
)
198 US_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
199 __func__
, request
, requesttype
,
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
);
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
);
215 /* return the actual length of the data transferred if no error */
217 status
= us
->current_urb
->actual_length
;
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.
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.
230 * Since many vendors in this space limit their testing to interoperability
231 * with these two OSes, specification violations like this one are common.
233 int usb_stor_clear_halt(struct us_data
*us
, unsigned int pipe
)
236 int endp
= usb_pipeendpoint(pipe
);
238 if (usb_pipein (pipe
))
241 result
= usb_stor_control_msg(us
, us
->send_ctrl_pipe
,
242 USB_REQ_CLEAR_FEATURE
, USB_RECIP_ENDPOINT
,
243 USB_ENDPOINT_HALT
, endp
,
246 /* reset the endpoint toggle */
248 usb_settoggle(us
->pusb_dev
, usb_pipeendpoint(pipe
),
249 usb_pipeout(pipe
), 0);
251 US_DEBUGP("%s: result = %d\n", __func__
, result
);
257 * Interpret the results of a URB transfer
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.
263 static int interpret_urb_result(struct us_data
*us
, unsigned int pipe
,
264 unsigned int length
, int result
, unsigned int partial
)
266 US_DEBUGP("Status code %d; transferred %u/%u\n",
267 result
, partial
, length
);
270 /* no error code; did we send all the data? */
272 if (partial
!= length
) {
273 US_DEBUGP("-- short transfer\n");
274 return USB_STOR_XFER_SHORT
;
277 US_DEBUGP("-- transfer complete\n");
278 return USB_STOR_XFER_GOOD
;
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
;
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
;
295 /* babble - the device tried to send more than we wanted to read */
297 US_DEBUGP("-- babble\n");
298 return USB_STOR_XFER_LONG
;
300 /* the transfer was cancelled by abort, disconnect, or timeout */
302 US_DEBUGP("-- transfer cancelled\n");
303 return USB_STOR_XFER_ERROR
;
305 /* short scatter-gather read transfer */
307 US_DEBUGP("-- short read transfer\n");
308 return USB_STOR_XFER_SHORT
;
310 /* abort or disconnect in progress */
312 US_DEBUGP("-- abort or disconnect in progress\n");
313 return USB_STOR_XFER_ERROR
;
315 /* the catch-all error case */
317 US_DEBUGP("-- unknown error\n");
318 return USB_STOR_XFER_ERROR
;
323 * Transfer one control message, without timeouts, but allowing early
324 * termination. Return codes are USB_STOR_XFER_xxx.
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
)
332 US_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
333 __func__
, request
, requesttype
,
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
);
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);
349 return interpret_urb_result(us
, pipe
, size
, result
,
350 us
->current_urb
->actual_length
);
354 * Receive one interrupt buffer, without timeouts, but allowing early
355 * termination. Return codes are USB_STOR_XFER_xxx.
357 * This routine always uses us->recv_intr_pipe as the pipe and
358 * us->ep_bInterval as the interrupt interval.
360 static int usb_stor_intr_transfer(struct us_data
*us
, void *buf
,
364 unsigned int pipe
= us
->recv_intr_pipe
;
367 US_DEBUGP("%s: xfer %u bytes\n", __func__
, length
);
369 /* calculate the max packet size */
370 maxp
= usb_maxpacket(us
->pusb_dev
, pipe
, usb_pipeout(pipe
));
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
,
378 result
= usb_stor_msg_common(us
, 0);
380 return interpret_urb_result(us
, pipe
, length
, result
,
381 us
->current_urb
->actual_length
);
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.
389 int usb_stor_bulk_transfer_buf(struct us_data
*us
, unsigned int pipe
,
390 void *buf
, unsigned int length
, unsigned int *act_len
)
394 US_DEBUGP("%s: xfer %u bytes\n", __func__
, length
);
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);
401 /* store the actual length of the data transferred */
403 *act_len
= us
->current_urb
->actual_length
;
404 return interpret_urb_result(us
, pipe
, length
, result
,
405 us
->current_urb
->actual_length
);
409 * Transfer a scatter-gather list via bulk transfer
411 * This function does basically the same thing as usb_stor_bulk_transfer_buf()
412 * above, but it uses the usbcore scatter-gather library.
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
)
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
;
424 /* initialize the scatter-gather request block */
425 US_DEBUGP("%s: xfer %u bytes, %d entries\n", __func__
,
427 result
= usb_sg_init(&us
->current_sg
, us
->pusb_dev
, pipe
, 0,
428 sg
, num_sg
, length
, GFP_NOIO
);
430 US_DEBUGP("usb_sg_init returned %d\n", result
);
431 return USB_STOR_XFER_ERROR
;
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
);
438 /* did an abort occur during the submission? */
439 if (test_bit(US_FLIDX_ABORTING
, &us
->dflags
)) {
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
);
448 /* wait for the completion of the transfer */
449 usb_sg_wait(&us
->current_sg
);
450 clear_bit(US_FLIDX_SG_ACTIVE
, &us
->dflags
);
452 result
= us
->current_sg
.status
;
454 *act_len
= us
->current_sg
.bytes
;
455 return interpret_urb_result(us
, pipe
, length
, result
,
456 us
->current_sg
.bytes
);
460 * Common used function. Transfer a complete command
461 * via usb_stor_bulk_transfer_sglist() above. Set cmnd resid
463 int usb_stor_bulk_srb(struct us_data
* us
, unsigned int pipe
,
464 struct scsi_cmnd
* srb
)
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
),
471 scsi_set_resid(srb
, scsi_bufflen(srb
) - partial
);
476 * Transfer an entire SCSI command's worth of data payload over the bulk
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.
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
)
488 unsigned int partial
;
490 /* are we scatter-gathering? */
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
;
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
;
504 /* store the residual and return the error code */
506 *residual
= length_left
;
510 /***********************************************************************
512 ***********************************************************************/
514 /* Invoke the transport and basic error-handling/recovery methods
516 * This is used by the protocol layers to actually send the message to
517 * the device and receive the response.
519 void usb_stor_invoke_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
524 /* send the command to the transport layer */
525 scsi_set_resid(srb
, 0);
526 result
= us
->transport(srb
, us
);
528 /* if the command gets aborted by the higher layers, we need to
529 * short-circuit all other processing
531 if (test_bit(US_FLIDX_TIMED_OUT
, &us
->dflags
)) {
532 US_DEBUGP("-- command was aborted\n");
533 srb
->result
= DID_ABORT
<< 16;
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;
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
;
550 srb
->result
= SAM_STAT_GOOD
;
552 /* Determine if we need to auto-sense
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.
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.
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");
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.
576 if (result
== USB_STOR_TRANSPORT_FAILED
) {
577 US_DEBUGP("-- transport indicates command failure\n");
582 * Determine if this device is SAT by seeing if the
583 * command executed successfully. Otherwise we'll have
584 * to wait for at least one CHECK_CONDITION to determine
587 if ((srb
->cmnd
[0] == ATA_16
|| srb
->cmnd
[0] == ATA_12
) &&
588 result
== USB_STOR_TRANSPORT_GOOD
&&
589 !(us
->fflags
& US_FL_SANE_SENSE
) &&
590 !(srb
->cmnd
[2] & 0x20)) {
591 US_DEBUGP("-- SAT supported, increasing auto-sense\n");
592 us
->fflags
|= US_FL_SANE_SENSE
;
596 * A short transfer on a command where we don't expect it
597 * is unusual, but it doesn't mean we need to auto-sense.
599 if ((scsi_get_resid(srb
) > 0) &&
600 !((srb
->cmnd
[0] == REQUEST_SENSE
) ||
601 (srb
->cmnd
[0] == INQUIRY
) ||
602 (srb
->cmnd
[0] == MODE_SENSE
) ||
603 (srb
->cmnd
[0] == LOG_SENSE
) ||
604 (srb
->cmnd
[0] == MODE_SENSE_10
))) {
605 US_DEBUGP("-- unexpectedly short transfer\n");
608 /* Now, if we need to do the auto-sense, let's do it */
609 if (need_auto_sense
) {
611 struct scsi_eh_save ses
;
612 int sense_size
= US_SENSE_SIZE
;
614 /* device supports and needs bigger sense buffer */
615 if (us
->fflags
& US_FL_SANE_SENSE
)
618 US_DEBUGP("Issuing auto-REQUEST_SENSE\n");
620 scsi_eh_prep_cmnd(srb
, &ses
, NULL
, 0, sense_size
);
622 /* FIXME: we must do the protocol translation here */
623 if (us
->subclass
== US_SC_RBC
|| us
->subclass
== US_SC_SCSI
||
624 us
->subclass
== US_SC_CYP_ATACB
)
629 /* issue the auto-sense command */
630 scsi_set_resid(srb
, 0);
631 temp_result
= us
->transport(us
->srb
, us
);
633 /* let's clean up right away */
634 scsi_eh_restore_cmnd(srb
, &ses
);
636 if (test_bit(US_FLIDX_TIMED_OUT
, &us
->dflags
)) {
637 US_DEBUGP("-- auto-sense aborted\n");
638 srb
->result
= DID_ABORT
<< 16;
641 if (temp_result
!= USB_STOR_TRANSPORT_GOOD
) {
642 US_DEBUGP("-- auto-sense failure\n");
644 /* we skip the reset if this happens to be a
645 * multi-target device, since failure of an
646 * auto-sense is perfectly valid
648 srb
->result
= DID_ERROR
<< 16;
649 if (!(us
->fflags
& US_FL_SCM_MULT_TARG
))
654 /* If the sense data returned is larger than 18-bytes then we
655 * assume this device supports requesting more in the future.
656 * The response code must be 70h through 73h inclusive.
658 if (srb
->sense_buffer
[7] > (US_SENSE_SIZE
- 8) &&
659 !(us
->fflags
& US_FL_SANE_SENSE
) &&
660 (srb
->sense_buffer
[0] & 0x7C) == 0x70) {
661 US_DEBUGP("-- SANE_SENSE support enabled\n");
662 us
->fflags
|= US_FL_SANE_SENSE
;
664 /* Indicate to the user that we truncated their sense
665 * because we didn't know it supported larger sense.
667 US_DEBUGP("-- Sense data truncated to %i from %i\n",
669 srb
->sense_buffer
[7] + 8);
670 srb
->sense_buffer
[7] = (US_SENSE_SIZE
- 8);
673 US_DEBUGP("-- Result from auto-sense is %d\n", temp_result
);
674 US_DEBUGP("-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n",
675 srb
->sense_buffer
[0],
676 srb
->sense_buffer
[2] & 0xf,
677 srb
->sense_buffer
[12],
678 srb
->sense_buffer
[13]);
679 #ifdef CONFIG_USB_STORAGE_DEBUG
681 srb
->sense_buffer
[2] & 0xf,
682 srb
->sense_buffer
[12],
683 srb
->sense_buffer
[13]);
686 /* set the result so the higher layers expect this data */
687 srb
->result
= SAM_STAT_CHECK_CONDITION
;
689 /* If things are really okay, then let's show that. Zero
690 * out the sense buffer so the higher layers won't realize
691 * we did an unsolicited auto-sense. */
692 if (result
== USB_STOR_TRANSPORT_GOOD
&&
693 /* Filemark 0, ignore EOM, ILI 0, no sense */
694 (srb
->sense_buffer
[2] & 0xaf) == 0 &&
696 srb
->sense_buffer
[12] == 0 &&
697 srb
->sense_buffer
[13] == 0) {
698 srb
->result
= SAM_STAT_GOOD
;
699 srb
->sense_buffer
[0] = 0x0;
703 /* Did we transfer less than the minimum amount required? */
704 if ((srb
->result
== SAM_STAT_GOOD
|| srb
->sense_buffer
[2] == 0) &&
705 scsi_bufflen(srb
) - scsi_get_resid(srb
) < srb
->underflow
)
706 srb
->result
= (DID_ERROR
<< 16) | (SUGGEST_RETRY
<< 24);
710 /* Error and abort processing: try to resynchronize with the device
711 * by issuing a port reset. If that fails, try a class-specific
715 /* Set the RESETTING bit, and clear the ABORTING bit so that
716 * the reset may proceed. */
717 scsi_lock(us_to_host(us
));
718 set_bit(US_FLIDX_RESETTING
, &us
->dflags
);
719 clear_bit(US_FLIDX_ABORTING
, &us
->dflags
);
720 scsi_unlock(us_to_host(us
));
722 /* We must release the device lock because the pre_reset routine
723 * will want to acquire it. */
724 mutex_unlock(&us
->dev_mutex
);
725 result
= usb_stor_port_reset(us
);
726 mutex_lock(&us
->dev_mutex
);
729 scsi_lock(us_to_host(us
));
730 usb_stor_report_device_reset(us
);
731 scsi_unlock(us_to_host(us
));
732 us
->transport_reset(us
);
734 clear_bit(US_FLIDX_RESETTING
, &us
->dflags
);
737 /* Stop the current URB transfer */
738 void usb_stor_stop_transport(struct us_data
*us
)
740 US_DEBUGP("%s called\n", __func__
);
742 /* If the state machine is blocked waiting for an URB,
743 * let's wake it up. The test_and_clear_bit() call
744 * guarantees that if a URB has just been submitted,
745 * it won't be cancelled more than once. */
746 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE
, &us
->dflags
)) {
747 US_DEBUGP("-- cancelling URB\n");
748 usb_unlink_urb(us
->current_urb
);
751 /* If we are waiting for a scatter-gather operation, cancel it. */
752 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE
, &us
->dflags
)) {
753 US_DEBUGP("-- cancelling sg request\n");
754 usb_sg_cancel(&us
->current_sg
);
759 * Control/Bulk and Control/Bulk/Interrupt transport
762 int usb_stor_CB_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
764 unsigned int transfer_length
= scsi_bufflen(srb
);
765 unsigned int pipe
= 0;
769 /* let's send the command via the control pipe */
770 result
= usb_stor_ctrl_transfer(us
, us
->send_ctrl_pipe
,
772 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
, 0,
773 us
->ifnum
, srb
->cmnd
, srb
->cmd_len
);
775 /* check the return code for the command */
776 US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result
);
778 /* if we stalled the command, it means command failed */
779 if (result
== USB_STOR_XFER_STALLED
) {
780 return USB_STOR_TRANSPORT_FAILED
;
783 /* Uh oh... serious problem here */
784 if (result
!= USB_STOR_XFER_GOOD
) {
785 return USB_STOR_TRANSPORT_ERROR
;
789 /* transfer the data payload for this command, if one exists*/
790 if (transfer_length
) {
791 pipe
= srb
->sc_data_direction
== DMA_FROM_DEVICE
?
792 us
->recv_bulk_pipe
: us
->send_bulk_pipe
;
793 result
= usb_stor_bulk_srb(us
, pipe
, srb
);
794 US_DEBUGP("CBI data stage result is 0x%x\n", result
);
796 /* if we stalled the data transfer it means command failed */
797 if (result
== USB_STOR_XFER_STALLED
)
798 return USB_STOR_TRANSPORT_FAILED
;
799 if (result
> USB_STOR_XFER_STALLED
)
800 return USB_STOR_TRANSPORT_ERROR
;
805 /* NOTE: CB does not have a status stage. Silly, I know. So
806 * we have to catch this at a higher level.
808 if (us
->protocol
!= US_PR_CBI
)
809 return USB_STOR_TRANSPORT_GOOD
;
811 result
= usb_stor_intr_transfer(us
, us
->iobuf
, 2);
812 US_DEBUGP("Got interrupt data (0x%x, 0x%x)\n",
813 us
->iobuf
[0], us
->iobuf
[1]);
814 if (result
!= USB_STOR_XFER_GOOD
)
815 return USB_STOR_TRANSPORT_ERROR
;
817 /* UFI gives us ASC and ASCQ, like a request sense
819 * REQUEST_SENSE and INQUIRY don't affect the sense data on UFI
820 * devices, so we ignore the information for those commands. Note
821 * that this means we could be ignoring a real error on these
822 * commands, but that can't be helped.
824 if (us
->subclass
== US_SC_UFI
) {
825 if (srb
->cmnd
[0] == REQUEST_SENSE
||
826 srb
->cmnd
[0] == INQUIRY
)
827 return USB_STOR_TRANSPORT_GOOD
;
830 return USB_STOR_TRANSPORT_GOOD
;
833 /* If not UFI, we interpret the data as a result code
834 * The first byte should always be a 0x0.
836 * Some bogus devices don't follow that rule. They stuff the ASC
837 * into the first byte -- so if it's non-zero, call it a failure.
840 US_DEBUGP("CBI IRQ data showed reserved bType 0x%x\n",
846 /* The second byte & 0x0F should be 0x0 for good, otherwise error */
847 switch (us
->iobuf
[1] & 0x0F) {
849 return USB_STOR_TRANSPORT_GOOD
;
853 return USB_STOR_TRANSPORT_ERROR
;
855 /* the CBI spec requires that the bulk pipe must be cleared
856 * following any data-in/out command failure (section 2.4.3.1.3)
860 usb_stor_clear_halt(us
, pipe
);
861 return USB_STOR_TRANSPORT_FAILED
;
865 * Bulk only transport
868 /* Determine what the maximum LUN supported is */
869 int usb_stor_Bulk_max_lun(struct us_data
*us
)
873 /* issue the command */
875 result
= usb_stor_control_msg(us
, us
->recv_ctrl_pipe
,
877 USB_DIR_IN
| USB_TYPE_CLASS
|
879 0, us
->ifnum
, us
->iobuf
, 1, HZ
);
881 US_DEBUGP("GetMaxLUN command result is %d, data is %d\n",
882 result
, us
->iobuf
[0]);
884 /* if we have a successful request, return the result */
889 * Some devices don't like GetMaxLUN. They may STALL the control
890 * pipe, they may return a zero-length result, they may do nothing at
891 * all and timeout, or they may fail in even more bizarrely creative
892 * ways. In these cases the best approach is to use the default
893 * value: only one LUN.
898 int usb_stor_Bulk_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
900 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
901 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
902 unsigned int transfer_length
= scsi_bufflen(srb
);
903 unsigned int residue
;
907 unsigned int cbwlen
= US_BULK_CB_WRAP_LEN
;
909 /* Take care of BULK32 devices; set extra byte to 0 */
910 if (unlikely(us
->fflags
& US_FL_BULK32
)) {
915 /* set up the command wrapper */
916 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
917 bcb
->DataTransferLength
= cpu_to_le32(transfer_length
);
918 bcb
->Flags
= srb
->sc_data_direction
== DMA_FROM_DEVICE
? 1 << 7 : 0;
919 bcb
->Tag
= ++us
->tag
;
920 bcb
->Lun
= srb
->device
->lun
;
921 if (us
->fflags
& US_FL_SCM_MULT_TARG
)
922 bcb
->Lun
|= srb
->device
->id
<< 4;
923 bcb
->Length
= srb
->cmd_len
;
925 /* copy the command payload */
926 memset(bcb
->CDB
, 0, sizeof(bcb
->CDB
));
927 memcpy(bcb
->CDB
, srb
->cmnd
, bcb
->Length
);
929 /* send it to out endpoint */
930 US_DEBUGP("Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n",
931 le32_to_cpu(bcb
->Signature
), bcb
->Tag
,
932 le32_to_cpu(bcb
->DataTransferLength
), bcb
->Flags
,
933 (bcb
->Lun
>> 4), (bcb
->Lun
& 0x0F),
935 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
937 US_DEBUGP("Bulk command transfer result=%d\n", result
);
938 if (result
!= USB_STOR_XFER_GOOD
)
939 return USB_STOR_TRANSPORT_ERROR
;
942 /* send/receive data payload, if there is any */
944 /* Some USB-IDE converter chips need a 100us delay between the
945 * command phase and the data phase. Some devices need a little
946 * more than that, probably because of clock rate inaccuracies. */
947 if (unlikely(us
->fflags
& US_FL_GO_SLOW
))
950 if (transfer_length
) {
951 unsigned int pipe
= srb
->sc_data_direction
== DMA_FROM_DEVICE
?
952 us
->recv_bulk_pipe
: us
->send_bulk_pipe
;
953 result
= usb_stor_bulk_srb(us
, pipe
, srb
);
954 US_DEBUGP("Bulk data transfer result 0x%x\n", result
);
955 if (result
== USB_STOR_XFER_ERROR
)
956 return USB_STOR_TRANSPORT_ERROR
;
958 /* If the device tried to send back more data than the
959 * amount requested, the spec requires us to transfer
960 * the CSW anyway. Since there's no point retrying the
961 * the command, we'll return fake sense data indicating
962 * Illegal Request, Invalid Field in CDB.
964 if (result
== USB_STOR_XFER_LONG
)
968 /* See flow chart on pg 15 of the Bulk Only Transport spec for
969 * an explanation of how this code works.
972 /* get CSW for device status */
973 US_DEBUGP("Attempting to get CSW...\n");
974 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
975 bcs
, US_BULK_CS_WRAP_LEN
, &cswlen
);
977 /* Some broken devices add unnecessary zero-length packets to the
978 * end of their data transfers. Such packets show up as 0-length
979 * CSWs. If we encounter such a thing, try to read the CSW again.
981 if (result
== USB_STOR_XFER_SHORT
&& cswlen
== 0) {
982 US_DEBUGP("Received 0-length CSW; retrying...\n");
983 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
984 bcs
, US_BULK_CS_WRAP_LEN
, &cswlen
);
987 /* did the attempt to read the CSW fail? */
988 if (result
== USB_STOR_XFER_STALLED
) {
990 /* get the status again */
991 US_DEBUGP("Attempting to get CSW (2nd try)...\n");
992 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
,
993 bcs
, US_BULK_CS_WRAP_LEN
, NULL
);
996 /* if we still have a failure at this point, we're in trouble */
997 US_DEBUGP("Bulk status result = %d\n", result
);
998 if (result
!= USB_STOR_XFER_GOOD
)
999 return USB_STOR_TRANSPORT_ERROR
;
1001 /* check bulk status */
1002 residue
= le32_to_cpu(bcs
->Residue
);
1003 US_DEBUGP("Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
1004 le32_to_cpu(bcs
->Signature
), bcs
->Tag
,
1005 residue
, bcs
->Status
);
1006 if (!(bcs
->Tag
== us
->tag
|| (us
->fflags
& US_FL_BULK_IGNORE_TAG
)) ||
1007 bcs
->Status
> US_BULK_STAT_PHASE
) {
1008 US_DEBUGP("Bulk logical error\n");
1009 return USB_STOR_TRANSPORT_ERROR
;
1012 /* Some broken devices report odd signatures, so we do not check them
1013 * for validity against the spec. We store the first one we see,
1014 * and check subsequent transfers for validity against this signature.
1016 if (!us
->bcs_signature
) {
1017 us
->bcs_signature
= bcs
->Signature
;
1018 if (us
->bcs_signature
!= cpu_to_le32(US_BULK_CS_SIGN
))
1019 US_DEBUGP("Learnt BCS signature 0x%08X\n",
1020 le32_to_cpu(us
->bcs_signature
));
1021 } else if (bcs
->Signature
!= us
->bcs_signature
) {
1022 US_DEBUGP("Signature mismatch: got %08X, expecting %08X\n",
1023 le32_to_cpu(bcs
->Signature
),
1024 le32_to_cpu(us
->bcs_signature
));
1025 return USB_STOR_TRANSPORT_ERROR
;
1028 /* try to compute the actual residue, based on how much data
1029 * was really transferred and what the device tells us */
1030 if (residue
&& !(us
->fflags
& US_FL_IGNORE_RESIDUE
)) {
1032 /* Heuristically detect devices that generate bogus residues
1033 * by seeing what happens with INQUIRY and READ CAPACITY
1036 if (bcs
->Status
== US_BULK_STAT_OK
&&
1037 scsi_get_resid(srb
) == 0 &&
1038 ((srb
->cmnd
[0] == INQUIRY
&&
1039 transfer_length
== 36) ||
1040 (srb
->cmnd
[0] == READ_CAPACITY
&&
1041 transfer_length
== 8))) {
1042 us
->fflags
|= US_FL_IGNORE_RESIDUE
;
1045 residue
= min(residue
, transfer_length
);
1046 scsi_set_resid(srb
, max(scsi_get_resid(srb
),
1051 /* based on the status code, we report good or bad */
1052 switch (bcs
->Status
) {
1053 case US_BULK_STAT_OK
:
1054 /* device babbled -- return fake sense data */
1056 memcpy(srb
->sense_buffer
,
1057 usb_stor_sense_invalidCDB
,
1058 sizeof(usb_stor_sense_invalidCDB
));
1059 return USB_STOR_TRANSPORT_NO_SENSE
;
1062 /* command good -- note that data could be short */
1063 return USB_STOR_TRANSPORT_GOOD
;
1065 case US_BULK_STAT_FAIL
:
1066 /* command failed */
1067 return USB_STOR_TRANSPORT_FAILED
;
1069 case US_BULK_STAT_PHASE
:
1070 /* phase error -- note that a transport reset will be
1071 * invoked by the invoke_transport() function
1073 return USB_STOR_TRANSPORT_ERROR
;
1076 /* we should never get here, but if we do, we're in trouble */
1077 return USB_STOR_TRANSPORT_ERROR
;
1080 /***********************************************************************
1082 ***********************************************************************/
1084 /* This is the common part of the device reset code.
1086 * It's handy that every transport mechanism uses the control endpoint for
1089 * Basically, we send a reset with a 5-second timeout, so we don't get
1090 * jammed attempting to do the reset.
1092 static int usb_stor_reset_common(struct us_data
*us
,
1093 u8 request
, u8 requesttype
,
1094 u16 value
, u16 index
, void *data
, u16 size
)
1099 if (test_bit(US_FLIDX_DISCONNECTING
, &us
->dflags
)) {
1100 US_DEBUGP("No reset during disconnect\n");
1104 result
= usb_stor_control_msg(us
, us
->send_ctrl_pipe
,
1105 request
, requesttype
, value
, index
, data
, size
,
1108 US_DEBUGP("Soft reset failed: %d\n", result
);
1112 /* Give the device some time to recover from the reset,
1113 * but don't delay disconnect processing. */
1114 wait_event_interruptible_timeout(us
->delay_wait
,
1115 test_bit(US_FLIDX_DISCONNECTING
, &us
->dflags
),
1117 if (test_bit(US_FLIDX_DISCONNECTING
, &us
->dflags
)) {
1118 US_DEBUGP("Reset interrupted by disconnect\n");
1122 US_DEBUGP("Soft reset: clearing bulk-in endpoint halt\n");
1123 result
= usb_stor_clear_halt(us
, us
->recv_bulk_pipe
);
1125 US_DEBUGP("Soft reset: clearing bulk-out endpoint halt\n");
1126 result2
= usb_stor_clear_halt(us
, us
->send_bulk_pipe
);
1128 /* return a result code based on the result of the clear-halts */
1132 US_DEBUGP("Soft reset failed\n");
1134 US_DEBUGP("Soft reset done\n");
1138 /* This issues a CB[I] Reset to the device in question
1140 #define CB_RESET_CMD_SIZE 12
1142 int usb_stor_CB_reset(struct us_data
*us
)
1144 US_DEBUGP("%s called\n", __func__
);
1146 memset(us
->iobuf
, 0xFF, CB_RESET_CMD_SIZE
);
1147 us
->iobuf
[0] = SEND_DIAGNOSTIC
;
1149 return usb_stor_reset_common(us
, US_CBI_ADSC
,
1150 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1151 0, us
->ifnum
, us
->iobuf
, CB_RESET_CMD_SIZE
);
1154 /* This issues a Bulk-only Reset to the device in question, including
1155 * clearing the subsequent endpoint halts that may occur.
1157 int usb_stor_Bulk_reset(struct us_data
*us
)
1159 US_DEBUGP("%s called\n", __func__
);
1161 return usb_stor_reset_common(us
, US_BULK_RESET_REQUEST
,
1162 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1163 0, us
->ifnum
, NULL
, 0);
1166 /* Issue a USB port reset to the device. The caller must not hold
1169 int usb_stor_port_reset(struct us_data
*us
)
1173 result
= usb_lock_device_for_reset(us
->pusb_dev
, us
->pusb_intf
);
1175 US_DEBUGP("unable to lock device for reset: %d\n", result
);
1177 /* Were we disconnected while waiting for the lock? */
1178 if (test_bit(US_FLIDX_DISCONNECTING
, &us
->dflags
)) {
1180 US_DEBUGP("No reset during disconnect\n");
1182 result
= usb_reset_device(us
->pusb_dev
);
1183 US_DEBUGP("usb_reset_device returns %d\n",
1186 usb_unlock_device(us
->pusb_dev
);