1 /* Driver for USB Mass Storage compliant devices
3 * $Id: transport.c,v 1.47 2002/04/22 03:39:43 mdharm Exp $
5 * Current development and maintenance by:
6 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 * Developed with the assistance of:
9 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
10 * (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov)
11 * (c) 2002 Alan Stern <stern@rowland.org>
14 * (c) 1999 Michael Gee (michael@linuxspecific.com)
16 * This driver is based on the 'USB Mass Storage Class' document. This
17 * describes in detail the protocol used to communicate with such
18 * devices. Clearly, the designers had SCSI and ATAPI commands in
19 * mind when they created this document. The commands are all very
20 * similar to commands in the SCSI-II and ATAPI specifications.
22 * It is important to note that in a number of cases this class
23 * exhibits class-specific exemptions from the USB specification.
24 * Notably the usage of NAK, STALL and ACK differs from the norm, in
25 * that they are used to communicate wait, failed and OK on commands.
27 * Also, for certain devices, the interrupt endpoint is used to convey
28 * status of a command.
30 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
31 * information about this driver.
33 * This program is free software; you can redistribute it and/or modify it
34 * under the terms of the GNU General Public License as published by the
35 * Free Software Foundation; either version 2, or (at your option) any
38 * This program is distributed in the hope that it will be useful, but
39 * WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41 * General Public License for more details.
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 675 Mass Ave, Cambridge, MA 02139, USA.
48 #include <linux/sched.h>
49 #include <linux/errno.h>
50 #include <linux/slab.h>
52 #include <scsi/scsi.h>
53 #include <scsi/scsi_eh.h>
54 #include <scsi/scsi_device.h>
57 #include "transport.h"
63 /***********************************************************************
64 * Data transfer routines
65 ***********************************************************************/
68 * This is subtle, so pay attention:
69 * ---------------------------------
70 * We're very concerned about races with a command abort. Hanging this code
71 * is a sure fire way to hang the kernel. (Note that this discussion applies
72 * only to transactions resulting from a scsi queued-command, since only
73 * these transactions are subject to a scsi abort. Other transactions, such
74 * as those occurring during device-specific initialization, must be handled
75 * by a separate code path.)
77 * The abort function (usb_storage_command_abort() in scsiglue.c) first
78 * sets the machine state and the ABORTING bit in us->flags to prevent
79 * new URBs from being submitted. It then calls usb_stor_stop_transport()
80 * below, which atomically tests-and-clears the URB_ACTIVE bit in us->flags
81 * to see if the current_urb needs to be stopped. Likewise, the SG_ACTIVE
82 * bit is tested to see if the current_sg scatter-gather request needs to be
83 * stopped. The timeout callback routine does much the same thing.
85 * When a disconnect occurs, the DISCONNECTING bit in us->flags is set to
86 * prevent new URBs from being submitted, and usb_stor_stop_transport() is
87 * called to stop any ongoing requests.
89 * The submit function first verifies that the submitting is allowed
90 * (neither ABORTING nor DISCONNECTING bits are set) and that the submit
91 * completes without errors, and only then sets the URB_ACTIVE bit. This
92 * prevents the stop_transport() function from trying to cancel the URB
93 * while the submit call is underway. Next, the submit function must test
94 * the flags to see if an abort or disconnect occurred during the submission
95 * or before the URB_ACTIVE bit was set. If so, it's essential to cancel
96 * the URB if it hasn't been cancelled already (i.e., if the URB_ACTIVE bit
97 * is still set). Either way, the function must then wait for the URB to
98 * finish. Note that the URB can still be in progress even after a call to
99 * usb_unlink_urb() returns.
101 * The idea is that (1) once the ABORTING or DISCONNECTING bit is set,
102 * either the stop_transport() function or the submitting function
103 * is guaranteed to call usb_unlink_urb() for an active URB,
104 * and (2) test_and_clear_bit() prevents usb_unlink_urb() from being
105 * called more than once or from being called during usb_submit_urb().
108 /* This is the completion handler which will wake us up when an URB
111 static void usb_stor_blocking_completion(struct urb *urb)
113 struct completion *urb_done_ptr = (struct completion *)urb->context;
115 complete(urb_done_ptr);
118 /* This is the common part of the URB message submission code
120 * All URBs from the usb-storage driver involved in handling a queued scsi
121 * command _must_ pass through this function (or something like it) for the
122 * abort mechanisms to work properly.
124 static int usb_stor_msg_common(struct us_data *us, int timeout)
126 struct completion urb_done;
130 /* don't submit URBs during abort/disconnect processing */
131 if (us->flags & ABORTING_OR_DISCONNECTING)
134 /* set up data structures for the wakeup system */
135 init_completion(&urb_done);
137 /* fill the common fields in the URB */
138 us->current_urb->context = &urb_done;
139 us->current_urb->actual_length = 0;
140 us->current_urb->error_count = 0;
141 us->current_urb->status = 0;
143 /* we assume that if transfer_buffer isn't us->iobuf then it
144 * hasn't been mapped for DMA. Yes, this is clunky, but it's
145 * easier than always having the caller tell us whether the
146 * transfer buffer has already been mapped. */
147 us->current_urb->transfer_flags = URB_NO_SETUP_DMA_MAP;
148 if (us->current_urb->transfer_buffer == us->iobuf)
149 us->current_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
150 us->current_urb->transfer_dma = us->iobuf_dma;
151 us->current_urb->setup_dma = us->cr_dma;
154 status = usb_submit_urb(us->current_urb, GFP_NOIO);
156 /* something went wrong */
160 /* since the URB has been submitted successfully, it's now okay
162 set_bit(US_FLIDX_URB_ACTIVE, &us->flags);
164 /* did an abort/disconnect occur during the submission? */
165 if (us->flags & ABORTING_OR_DISCONNECTING) {
167 /* cancel the URB, if it hasn't been cancelled already */
168 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->flags)) {
169 US_DEBUGP("-- cancelling URB\n");
170 usb_unlink_urb(us->current_urb);
174 /* wait for the completion of the URB */
175 timeleft = wait_for_completion_interruptible_timeout(
176 &urb_done, timeout ? : MAX_SCHEDULE_TIMEOUT);
178 clear_bit(US_FLIDX_URB_ACTIVE, &us->flags);
181 US_DEBUGP("%s -- cancelling URB\n",
182 timeleft == 0 ? "Timeout" : "Signal");
183 usb_kill_urb(us->current_urb);
186 /* return the URB status */
187 return us->current_urb->status;
191 * Transfer one control message, with timeouts, and allowing early
192 * termination. Return codes are usual -Exxx, *not* USB_STOR_XFER_xxx.
194 int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
195 u8 request, u8 requesttype, u16 value, u16 index,
196 void *data, u16 size, int timeout)
200 US_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
201 __FUNCTION__, request, requesttype,
204 /* fill in the devrequest structure */
205 us->cr->bRequestType = requesttype;
206 us->cr->bRequest = request;
207 us->cr->wValue = cpu_to_le16(value);
208 us->cr->wIndex = cpu_to_le16(index);
209 us->cr->wLength = cpu_to_le16(size);
211 /* fill and submit the URB */
212 usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
213 (unsigned char*) us->cr, data, size,
214 usb_stor_blocking_completion, NULL);
215 status = usb_stor_msg_common(us, timeout);
217 /* return the actual length of the data transferred if no error */
219 status = us->current_urb->actual_length;
223 /* This is a version of usb_clear_halt() that allows early termination and
224 * doesn't read the status from the device -- this is because some devices
225 * crash their internal firmware when the status is requested after a halt.
227 * A definitive list of these 'bad' devices is too difficult to maintain or
228 * make complete enough to be useful. This problem was first observed on the
229 * Hagiwara FlashGate DUAL unit. However, bus traces reveal that neither
230 * MacOS nor Windows checks the status after clearing a halt.
232 * Since many vendors in this space limit their testing to interoperability
233 * with these two OSes, specification violations like this one are common.
235 int usb_stor_clear_halt(struct us_data *us, unsigned int pipe)
238 int endp = usb_pipeendpoint(pipe);
240 if (usb_pipein (pipe))
243 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
244 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
245 USB_ENDPOINT_HALT, endp,
248 /* reset the endpoint toggle */
250 usb_settoggle(us->pusb_dev, usb_pipeendpoint(pipe),
251 usb_pipeout(pipe), 0);
253 US_DEBUGP("%s: result = %d\n", __FUNCTION__, result);
259 * Interpret the results of a URB transfer
261 * This function prints appropriate debugging messages, clears halts on
262 * non-control endpoints, and translates the status to the corresponding
263 * USB_STOR_XFER_xxx return code.
265 static int interpret_urb_result(struct us_data *us, unsigned int pipe,
266 unsigned int length, int result, unsigned int partial)
268 US_DEBUGP("Status code %d; transferred %u/%u\n",
269 result, partial, length);
272 /* no error code; did we send all the data? */
274 if (partial != length) {
275 US_DEBUGP("-- short transfer\n");
276 return USB_STOR_XFER_SHORT;
279 US_DEBUGP("-- transfer complete\n");
280 return USB_STOR_XFER_GOOD;
284 /* for control endpoints, (used by CB[I]) a stall indicates
285 * a failed command */
286 if (usb_pipecontrol(pipe)) {
287 US_DEBUGP("-- stall on control pipe\n");
288 return USB_STOR_XFER_STALLED;
291 /* for other sorts of endpoint, clear the stall */
292 US_DEBUGP("clearing endpoint halt for pipe 0x%x\n", pipe);
293 if (usb_stor_clear_halt(us, pipe) < 0)
294 return USB_STOR_XFER_ERROR;
295 return USB_STOR_XFER_STALLED;
297 /* babble - the device tried to send more than we wanted to read */
299 US_DEBUGP("-- babble\n");
300 return USB_STOR_XFER_LONG;
302 /* the transfer was cancelled by abort, disconnect, or timeout */
304 US_DEBUGP("-- transfer cancelled\n");
305 return USB_STOR_XFER_ERROR;
307 /* short scatter-gather read transfer */
309 US_DEBUGP("-- short read transfer\n");
310 return USB_STOR_XFER_SHORT;
312 /* abort or disconnect in progress */
314 US_DEBUGP("-- abort or disconnect in progress\n");
315 return USB_STOR_XFER_ERROR;
317 /* the catch-all error case */
319 US_DEBUGP("-- unknown error\n");
320 return USB_STOR_XFER_ERROR;
325 * Transfer one control message, without timeouts, but allowing early
326 * termination. Return codes are USB_STOR_XFER_xxx.
328 int usb_stor_ctrl_transfer(struct us_data *us, unsigned int pipe,
329 u8 request, u8 requesttype, u16 value, u16 index,
330 void *data, u16 size)
334 US_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
335 __FUNCTION__, request, requesttype,
338 /* fill in the devrequest structure */
339 us->cr->bRequestType = requesttype;
340 us->cr->bRequest = request;
341 us->cr->wValue = cpu_to_le16(value);
342 us->cr->wIndex = cpu_to_le16(index);
343 us->cr->wLength = cpu_to_le16(size);
345 /* fill and submit the URB */
346 usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
347 (unsigned char*) us->cr, data, size,
348 usb_stor_blocking_completion, NULL);
349 result = usb_stor_msg_common(us, 0);
351 return interpret_urb_result(us, pipe, size, result,
352 us->current_urb->actual_length);
356 * Receive one interrupt buffer, without timeouts, but allowing early
357 * termination. Return codes are USB_STOR_XFER_xxx.
359 * This routine always uses us->recv_intr_pipe as the pipe and
360 * us->ep_bInterval as the interrupt interval.
362 static int usb_stor_intr_transfer(struct us_data *us, void *buf,
366 unsigned int pipe = us->recv_intr_pipe;
369 US_DEBUGP("%s: xfer %u bytes\n", __FUNCTION__, length);
371 /* calculate the max packet size */
372 maxp = usb_maxpacket(us->pusb_dev, pipe, usb_pipeout(pipe));
376 /* fill and submit the URB */
377 usb_fill_int_urb(us->current_urb, us->pusb_dev, pipe, buf,
378 maxp, usb_stor_blocking_completion, NULL,
380 result = usb_stor_msg_common(us, 0);
382 return interpret_urb_result(us, pipe, length, result,
383 us->current_urb->actual_length);
387 * Transfer one buffer via bulk pipe, without timeouts, but allowing early
388 * termination. Return codes are USB_STOR_XFER_xxx. If the bulk pipe
389 * stalls during the transfer, the halt is automatically cleared.
391 int usb_stor_bulk_transfer_buf(struct us_data *us, unsigned int pipe,
392 void *buf, unsigned int length, unsigned int *act_len)
396 US_DEBUGP("%s: xfer %u bytes\n", __FUNCTION__, length);
398 /* fill and submit the URB */
399 usb_fill_bulk_urb(us->current_urb, us->pusb_dev, pipe, buf, length,
400 usb_stor_blocking_completion, NULL);
401 result = usb_stor_msg_common(us, 0);
403 /* store the actual length of the data transferred */
405 *act_len = us->current_urb->actual_length;
406 return interpret_urb_result(us, pipe, length, result,
407 us->current_urb->actual_length);
411 * Transfer a scatter-gather list via bulk transfer
413 * This function does basically the same thing as usb_stor_bulk_transfer_buf()
414 * above, but it uses the usbcore scatter-gather library.
416 static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
417 struct scatterlist *sg, int num_sg, unsigned int length,
418 unsigned int *act_len)
422 /* don't submit s-g requests during abort/disconnect processing */
423 if (us->flags & ABORTING_OR_DISCONNECTING)
424 return USB_STOR_XFER_ERROR;
426 /* initialize the scatter-gather request block */
427 US_DEBUGP("%s: xfer %u bytes, %d entries\n", __FUNCTION__,
429 result = usb_sg_init(&us->current_sg, us->pusb_dev, pipe, 0,
430 sg, num_sg, length, GFP_NOIO);
432 US_DEBUGP("usb_sg_init returned %d\n", result);
433 return USB_STOR_XFER_ERROR;
436 /* since the block has been initialized successfully, it's now
437 * okay to cancel it */
438 set_bit(US_FLIDX_SG_ACTIVE, &us->flags);
440 /* did an abort/disconnect occur during the submission? */
441 if (us->flags & ABORTING_OR_DISCONNECTING) {
443 /* cancel the request, if it hasn't been cancelled already */
444 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->flags)) {
445 US_DEBUGP("-- cancelling sg request\n");
446 usb_sg_cancel(&us->current_sg);
450 /* wait for the completion of the transfer */
451 usb_sg_wait(&us->current_sg);
452 clear_bit(US_FLIDX_SG_ACTIVE, &us->flags);
454 result = us->current_sg.status;
456 *act_len = us->current_sg.bytes;
457 return interpret_urb_result(us, pipe, length, result,
458 us->current_sg.bytes);
462 * Transfer an entire SCSI command's worth of data payload over the bulk
465 * Note that this uses usb_stor_bulk_transfer_buf() and
466 * usb_stor_bulk_transfer_sglist() to achieve its goals --
467 * this function simply determines whether we're going to use
468 * scatter-gather or not, and acts appropriately.
470 int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe,
471 void *buf, unsigned int length_left, int use_sg, int *residual)
474 unsigned int partial;
476 /* are we scatter-gathering? */
478 /* use the usb core scatter-gather primitives */
479 result = usb_stor_bulk_transfer_sglist(us, pipe,
480 (struct scatterlist *) buf, use_sg,
481 length_left, &partial);
482 length_left -= partial;
484 /* no scatter-gather, just make the request */
485 result = usb_stor_bulk_transfer_buf(us, pipe, buf,
486 length_left, &partial);
487 length_left -= partial;
490 /* store the residual and return the error code */
492 *residual = length_left;
496 /***********************************************************************
498 ***********************************************************************/
500 /* Invoke the transport and basic error-handling/recovery methods
502 * This is used by the protocol layers to actually send the message to
503 * the device and receive the response.
505 void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
510 /* send the command to the transport layer */
512 result = us->transport(srb, us);
514 /* if the command gets aborted by the higher layers, we need to
515 * short-circuit all other processing
517 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
518 US_DEBUGP("-- command was aborted\n");
519 srb->result = DID_ABORT << 16;
523 /* if there is a transport error, reset and don't auto-sense */
524 if (result == USB_STOR_TRANSPORT_ERROR) {
525 US_DEBUGP("-- transport indicates error, resetting\n");
526 srb->result = DID_ERROR << 16;
530 /* if the transport provided its own sense data, don't auto-sense */
531 if (result == USB_STOR_TRANSPORT_NO_SENSE) {
532 srb->result = SAM_STAT_CHECK_CONDITION;
536 srb->result = SAM_STAT_GOOD;
538 /* Determine if we need to auto-sense
540 * I normally don't use a flag like this, but it's almost impossible
541 * to understand what's going on here if I don't.
546 * If we're running the CB transport, which is incapable
547 * of determining status on its own, we will auto-sense
548 * unless the operation involved a data-in transfer. Devices
549 * can signal most data-in errors by stalling the bulk-in pipe.
551 if ((us->protocol == US_PR_CB || us->protocol == US_PR_DPCM_USB) &&
552 srb->sc_data_direction != DMA_FROM_DEVICE) {
553 US_DEBUGP("-- CB transport device requiring auto-sense\n");
558 * If we have a failure, we're going to do a REQUEST_SENSE
559 * automatically. Note that we differentiate between a command
560 * "failure" and an "error" in the transport mechanism.
562 if (result == USB_STOR_TRANSPORT_FAILED) {
563 US_DEBUGP("-- transport indicates command failure\n");
568 * A short transfer on a command where we don't expect it
569 * is unusual, but it doesn't mean we need to auto-sense.
571 if ((srb->resid > 0) &&
572 !((srb->cmnd[0] == REQUEST_SENSE) ||
573 (srb->cmnd[0] == INQUIRY) ||
574 (srb->cmnd[0] == MODE_SENSE) ||
575 (srb->cmnd[0] == LOG_SENSE) ||
576 (srb->cmnd[0] == MODE_SENSE_10))) {
577 US_DEBUGP("-- unexpectedly short transfer\n");
580 /* Now, if we need to do the auto-sense, let's do it */
581 if (need_auto_sense) {
583 struct scsi_eh_save ses;
585 US_DEBUGP("Issuing auto-REQUEST_SENSE\n");
587 scsi_eh_prep_cmnd(srb, &ses, NULL, 0, US_SENSE_SIZE);
589 /* FIXME: we must do the protocol translation here */
590 if (us->subclass == US_SC_RBC || us->subclass == US_SC_SCSI)
595 /* issue the auto-sense command */
597 temp_result = us->transport(us->srb, us);
599 /* let's clean up right away */
600 scsi_eh_restore_cmnd(srb, &ses);
602 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
603 US_DEBUGP("-- auto-sense aborted\n");
604 srb->result = DID_ABORT << 16;
607 if (temp_result != USB_STOR_TRANSPORT_GOOD) {
608 US_DEBUGP("-- auto-sense failure\n");
610 /* we skip the reset if this happens to be a
611 * multi-target device, since failure of an
612 * auto-sense is perfectly valid
614 srb->result = DID_ERROR << 16;
615 if (!(us->flags & US_FL_SCM_MULT_TARG))
620 US_DEBUGP("-- Result from auto-sense is %d\n", temp_result);
621 US_DEBUGP("-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n",
622 srb->sense_buffer[0],
623 srb->sense_buffer[2] & 0xf,
624 srb->sense_buffer[12],
625 srb->sense_buffer[13]);
626 #ifdef CONFIG_USB_STORAGE_DEBUG
628 srb->sense_buffer[2] & 0xf,
629 srb->sense_buffer[12],
630 srb->sense_buffer[13]);
633 /* set the result so the higher layers expect this data */
634 srb->result = SAM_STAT_CHECK_CONDITION;
636 /* If things are really okay, then let's show that. Zero
637 * out the sense buffer so the higher layers won't realize
638 * we did an unsolicited auto-sense. */
639 if (result == USB_STOR_TRANSPORT_GOOD &&
640 /* Filemark 0, ignore EOM, ILI 0, no sense */
641 (srb->sense_buffer[2] & 0xaf) == 0 &&
643 srb->sense_buffer[12] == 0 &&
644 srb->sense_buffer[13] == 0) {
645 srb->result = SAM_STAT_GOOD;
646 srb->sense_buffer[0] = 0x0;
650 /* Did we transfer less than the minimum amount required? */
651 if (srb->result == SAM_STAT_GOOD &&
652 srb->request_bufflen - srb->resid < srb->underflow)
653 srb->result = (DID_ERROR << 16) | (SUGGEST_RETRY << 24);
657 /* Error and abort processing: try to resynchronize with the device
658 * by issuing a port reset. If that fails, try a class-specific
662 /* Set the RESETTING bit, and clear the ABORTING bit so that
663 * the reset may proceed. */
664 scsi_lock(us_to_host(us));
665 set_bit(US_FLIDX_RESETTING, &us->flags);
666 clear_bit(US_FLIDX_ABORTING, &us->flags);
667 scsi_unlock(us_to_host(us));
669 /* We must release the device lock because the pre_reset routine
670 * will want to acquire it. */
671 mutex_unlock(&us->dev_mutex);
672 result = usb_stor_port_reset(us);
673 mutex_lock(&us->dev_mutex);
676 scsi_lock(us_to_host(us));
677 usb_stor_report_device_reset(us);
678 scsi_unlock(us_to_host(us));
679 us->transport_reset(us);
681 clear_bit(US_FLIDX_RESETTING, &us->flags);
684 /* Stop the current URB transfer */
685 void usb_stor_stop_transport(struct us_data *us)
687 US_DEBUGP("%s called\n", __FUNCTION__);
689 /* If the state machine is blocked waiting for an URB,
690 * let's wake it up. The test_and_clear_bit() call
691 * guarantees that if a URB has just been submitted,
692 * it won't be cancelled more than once. */
693 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->flags)) {
694 US_DEBUGP("-- cancelling URB\n");
695 usb_unlink_urb(us->current_urb);
698 /* If we are waiting for a scatter-gather operation, cancel it. */
699 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->flags)) {
700 US_DEBUGP("-- cancelling sg request\n");
701 usb_sg_cancel(&us->current_sg);
706 * Control/Bulk/Interrupt transport
709 int usb_stor_CBI_transport(struct scsi_cmnd *srb, struct us_data *us)
711 unsigned int transfer_length = srb->request_bufflen;
712 unsigned int pipe = 0;
716 /* let's send the command via the control pipe */
717 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
719 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
720 us->ifnum, srb->cmnd, srb->cmd_len);
722 /* check the return code for the command */
723 US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result);
725 /* if we stalled the command, it means command failed */
726 if (result == USB_STOR_XFER_STALLED) {
727 return USB_STOR_TRANSPORT_FAILED;
730 /* Uh oh... serious problem here */
731 if (result != USB_STOR_XFER_GOOD) {
732 return USB_STOR_TRANSPORT_ERROR;
736 /* transfer the data payload for this command, if one exists*/
737 if (transfer_length) {
738 pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
739 us->recv_bulk_pipe : us->send_bulk_pipe;
740 result = usb_stor_bulk_transfer_sg(us, pipe,
741 srb->request_buffer, transfer_length,
742 srb->use_sg, &srb->resid);
743 US_DEBUGP("CBI data stage result is 0x%x\n", result);
745 /* if we stalled the data transfer it means command failed */
746 if (result == USB_STOR_XFER_STALLED)
747 return USB_STOR_TRANSPORT_FAILED;
748 if (result > USB_STOR_XFER_STALLED)
749 return USB_STOR_TRANSPORT_ERROR;
753 result = usb_stor_intr_transfer(us, us->iobuf, 2);
754 US_DEBUGP("Got interrupt data (0x%x, 0x%x)\n",
755 us->iobuf[0], us->iobuf[1]);
756 if (result != USB_STOR_XFER_GOOD)
757 return USB_STOR_TRANSPORT_ERROR;
759 /* UFI gives us ASC and ASCQ, like a request sense
761 * REQUEST_SENSE and INQUIRY don't affect the sense data on UFI
762 * devices, so we ignore the information for those commands. Note
763 * that this means we could be ignoring a real error on these
764 * commands, but that can't be helped.
766 if (us->subclass == US_SC_UFI) {
767 if (srb->cmnd[0] == REQUEST_SENSE ||
768 srb->cmnd[0] == INQUIRY)
769 return USB_STOR_TRANSPORT_GOOD;
772 return USB_STOR_TRANSPORT_GOOD;
775 /* If not UFI, we interpret the data as a result code
776 * The first byte should always be a 0x0.
778 * Some bogus devices don't follow that rule. They stuff the ASC
779 * into the first byte -- so if it's non-zero, call it a failure.
782 US_DEBUGP("CBI IRQ data showed reserved bType 0x%x\n",
788 /* The second byte & 0x0F should be 0x0 for good, otherwise error */
789 switch (us->iobuf[1] & 0x0F) {
791 return USB_STOR_TRANSPORT_GOOD;
795 return USB_STOR_TRANSPORT_ERROR;
797 /* the CBI spec requires that the bulk pipe must be cleared
798 * following any data-in/out command failure (section 2.4.3.1.3)
802 usb_stor_clear_halt(us, pipe);
803 return USB_STOR_TRANSPORT_FAILED;
807 * Control/Bulk transport
809 int usb_stor_CB_transport(struct scsi_cmnd *srb, struct us_data *us)
811 unsigned int transfer_length = srb->request_bufflen;
815 /* let's send the command via the control pipe */
816 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
818 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
819 us->ifnum, srb->cmnd, srb->cmd_len);
821 /* check the return code for the command */
822 US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result);
824 /* if we stalled the command, it means command failed */
825 if (result == USB_STOR_XFER_STALLED) {
826 return USB_STOR_TRANSPORT_FAILED;
829 /* Uh oh... serious problem here */
830 if (result != USB_STOR_XFER_GOOD) {
831 return USB_STOR_TRANSPORT_ERROR;
835 /* transfer the data payload for this command, if one exists*/
836 if (transfer_length) {
837 unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
838 us->recv_bulk_pipe : us->send_bulk_pipe;
839 result = usb_stor_bulk_transfer_sg(us, pipe,
840 srb->request_buffer, transfer_length,
841 srb->use_sg, &srb->resid);
842 US_DEBUGP("CB data stage result is 0x%x\n", result);
844 /* if we stalled the data transfer it means command failed */
845 if (result == USB_STOR_XFER_STALLED)
846 return USB_STOR_TRANSPORT_FAILED;
847 if (result > USB_STOR_XFER_STALLED)
848 return USB_STOR_TRANSPORT_ERROR;
852 /* NOTE: CB does not have a status stage. Silly, I know. So
853 * we have to catch this at a higher level.
855 return USB_STOR_TRANSPORT_GOOD;
859 * Bulk only transport
862 /* Determine what the maximum LUN supported is */
863 int usb_stor_Bulk_max_lun(struct us_data *us)
867 /* issue the command */
869 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
871 USB_DIR_IN | USB_TYPE_CLASS |
873 0, us->ifnum, us->iobuf, 1, HZ);
875 US_DEBUGP("GetMaxLUN command result is %d, data is %d\n",
876 result, us->iobuf[0]);
878 /* if we have a successful request, return the result */
883 * Some devices (i.e. Iomega Zip100) need this -- apparently
884 * the bulk pipes get STALLed when the GetMaxLUN request is
885 * processed. This is, in theory, harmless to all other devices
886 * (regardless of if they stall or not).
888 if (result == -EPIPE) {
889 usb_stor_clear_halt(us, us->recv_bulk_pipe);
890 usb_stor_clear_halt(us, us->send_bulk_pipe);
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.
903 int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
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 = srb->request_bufflen;
908 unsigned int residue;
912 unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
914 /* Take care of BULK32 devices; set extra byte to 0 */
915 if ( unlikely(us->flags & US_FL_BULK32)) {
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->flags & US_FL_SCM_MULT_TARG)
927 bcb->Lun |= srb->device->id << 4;
928 bcb->Length = srb->cmd_len;
930 /* copy the command payload */
931 memset(bcb->CDB, 0, sizeof(bcb->CDB));
932 memcpy(bcb->CDB, srb->cmnd, bcb->Length);
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),
940 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
942 US_DEBUGP("Bulk command transfer result=%d\n", result);
943 if (result != USB_STOR_XFER_GOOD)
944 return USB_STOR_TRANSPORT_ERROR;
947 /* send/receive data payload, if there is any */
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->flags & US_FL_GO_SLOW))
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_transfer_sg(us, pipe,
959 srb->request_buffer, transfer_length,
960 srb->use_sg, &srb->resid);
961 US_DEBUGP("Bulk data transfer result 0x%x\n", result);
962 if (result == USB_STOR_XFER_ERROR)
963 return USB_STOR_TRANSPORT_ERROR;
965 /* If the device tried to send back more data than the
966 * amount requested, the spec requires us to transfer
967 * the CSW anyway. Since there's no point retrying the
968 * the command, we'll return fake sense data indicating
969 * Illegal Request, Invalid Field in CDB.
971 if (result == USB_STOR_XFER_LONG)
975 /* See flow chart on pg 15 of the Bulk Only Transport spec for
976 * an explanation of how this code works.
979 /* get CSW for device status */
980 US_DEBUGP("Attempting to get CSW...\n");
981 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
982 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
984 /* Some broken devices add unnecessary zero-length packets to the
985 * end of their data transfers. Such packets show up as 0-length
986 * CSWs. If we encounter such a thing, try to read the CSW again.
988 if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
989 US_DEBUGP("Received 0-length CSW; retrying...\n");
990 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
991 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
994 /* did the attempt to read the CSW fail? */
995 if (result == USB_STOR_XFER_STALLED) {
997 /* get the status again */
998 US_DEBUGP("Attempting to get CSW (2nd try)...\n");
999 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1000 bcs, US_BULK_CS_WRAP_LEN, NULL);
1003 /* if we still have a failure at this point, we're in trouble */
1004 US_DEBUGP("Bulk status result = %d\n", result);
1005 if (result != USB_STOR_XFER_GOOD)
1006 return USB_STOR_TRANSPORT_ERROR;
1008 /* check bulk status */
1009 residue = le32_to_cpu(bcs->Residue);
1010 US_DEBUGP("Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
1011 le32_to_cpu(bcs->Signature), bcs->Tag,
1012 residue, bcs->Status);
1013 if (bcs->Tag != us->tag || bcs->Status > US_BULK_STAT_PHASE) {
1014 US_DEBUGP("Bulk logical error\n");
1015 return USB_STOR_TRANSPORT_ERROR;
1018 /* Some broken devices report odd signatures, so we do not check them
1019 * for validity against the spec. We store the first one we see,
1020 * and check subsequent transfers for validity against this signature.
1022 if (!us->bcs_signature) {
1023 us->bcs_signature = bcs->Signature;
1024 if (us->bcs_signature != cpu_to_le32(US_BULK_CS_SIGN))
1025 US_DEBUGP("Learnt BCS signature 0x%08X\n",
1026 le32_to_cpu(us->bcs_signature));
1027 } else if (bcs->Signature != us->bcs_signature) {
1028 US_DEBUGP("Signature mismatch: got %08X, expecting %08X\n",
1029 le32_to_cpu(bcs->Signature),
1030 le32_to_cpu(us->bcs_signature));
1031 return USB_STOR_TRANSPORT_ERROR;
1034 /* try to compute the actual residue, based on how much data
1035 * was really transferred and what the device tells us */
1037 if (!(us->flags & US_FL_IGNORE_RESIDUE)) {
1038 residue = min(residue, transfer_length);
1039 srb->resid = max(srb->resid, (int) residue);
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 */
1048 memcpy(srb->sense_buffer,
1049 usb_stor_sense_invalidCDB,
1050 sizeof(usb_stor_sense_invalidCDB));
1051 return USB_STOR_TRANSPORT_NO_SENSE;
1054 /* command good -- note that data could be short */
1055 return USB_STOR_TRANSPORT_GOOD;
1057 case US_BULK_STAT_FAIL:
1058 /* command failed */
1059 return USB_STOR_TRANSPORT_FAILED;
1061 case US_BULK_STAT_PHASE:
1062 /* phase error -- note that a transport reset will be
1063 * invoked by the invoke_transport() function
1065 return USB_STOR_TRANSPORT_ERROR;
1068 /* we should never get here, but if we do, we're in trouble */
1069 return USB_STOR_TRANSPORT_ERROR;
1072 /***********************************************************************
1074 ***********************************************************************/
1076 /* This is the common part of the device reset code.
1078 * It's handy that every transport mechanism uses the control endpoint for
1081 * Basically, we send a reset with a 5-second timeout, so we don't get
1082 * jammed attempting to do the reset.
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)
1091 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
1092 US_DEBUGP("No reset during disconnect\n");
1096 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
1097 request, requesttype, value, index, data, size,
1100 US_DEBUGP("Soft reset failed: %d\n", result);
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->flags),
1109 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
1110 US_DEBUGP("Reset interrupted by disconnect\n");
1114 US_DEBUGP("Soft reset: clearing bulk-in endpoint halt\n");
1115 result = usb_stor_clear_halt(us, us->recv_bulk_pipe);
1117 US_DEBUGP("Soft reset: clearing bulk-out endpoint halt\n");
1118 result2 = usb_stor_clear_halt(us, us->send_bulk_pipe);
1120 /* return a result code based on the result of the clear-halts */
1124 US_DEBUGP("Soft reset failed\n");
1126 US_DEBUGP("Soft reset done\n");
1130 /* This issues a CB[I] Reset to the device in question
1132 #define CB_RESET_CMD_SIZE 12
1134 int usb_stor_CB_reset(struct us_data *us)
1136 US_DEBUGP("%s called\n", __FUNCTION__);
1138 memset(us->iobuf, 0xFF, CB_RESET_CMD_SIZE);
1139 us->iobuf[0] = SEND_DIAGNOSTIC;
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);
1146 /* This issues a Bulk-only Reset to the device in question, including
1147 * clearing the subsequent endpoint halts that may occur.
1149 int usb_stor_Bulk_reset(struct us_data *us)
1151 US_DEBUGP("%s called\n", __FUNCTION__);
1153 return usb_stor_reset_common(us, US_BULK_RESET_REQUEST,
1154 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1155 0, us->ifnum, NULL, 0);
1158 /* Issue a USB port reset to the device. The caller must not hold
1161 int usb_stor_port_reset(struct us_data *us)
1163 int result, rc_lock;
1166 usb_lock_device_for_reset(us->pusb_dev, us->pusb_intf);
1168 US_DEBUGP("unable to lock device for reset: %d\n", result);
1170 /* Were we disconnected while waiting for the lock? */
1171 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
1173 US_DEBUGP("No reset during disconnect\n");
1175 result = usb_reset_composite_device(
1176 us->pusb_dev, us->pusb_intf);
1177 US_DEBUGP("usb_reset_composite_device returns %d\n",
1181 usb_unlock_device(us->pusb_dev);