blob: 9cc30afd6d3123119c4ac77107e3b1b57344a7b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Driver for USB Mass Storage compliant devices
2 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sched.h>
47#include <linux/errno.h>
48#include <linux/slab.h>
49
50#include <scsi/scsi.h>
Boaz Harroshdff6de72007-09-10 22:36:31 +030051#include <scsi/scsi_eh.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#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
Alan Stern7e4d6c32008-05-01 15:35:18 -040076 * sets the machine state and the ABORTING bit in us->dflags to prevent
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * new URBs from being submitted. It then calls usb_stor_stop_transport()
Alan Stern7e4d6c32008-05-01 15:35:18 -040078 * below, which atomically tests-and-clears the URB_ACTIVE bit in us->dflags
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * 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 *
Alan Stern7e4d6c32008-05-01 15:35:18 -040083 * When a disconnect occurs, the DISCONNECTING bit in us->dflags is set to
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * 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
Alan Sternb375a042005-07-29 16:11:07 -040096 * finish. Note that the URB can still be in progress even after a call to
97 * usb_unlink_urb() returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *
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 */
David Howells7d12e782006-10-05 14:55:46 +0100109static void usb_stor_blocking_completion(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Ming Leicdc97792008-02-24 18:41:47 +0800111 struct completion *urb_done_ptr = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 complete(urb_done_ptr);
114}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
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 */
122static int usb_stor_msg_common(struct us_data *us, int timeout)
123{
124 struct completion urb_done;
Franck Bui-Huu3428cc42006-05-24 16:57:28 +0200125 long timeleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int status;
127
Alan Stern543f7812008-05-08 11:55:59 -0400128 /* don't submit URBs during abort processing */
129 if (test_bit(US_FLIDX_ABORTING, &us->dflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 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. */
Alan Sternb375a042005-07-29 16:11:07 -0400145 us->current_urb->transfer_flags = URB_NO_SETUP_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 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 */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400160 set_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Alan Stern543f7812008-05-08 11:55:59 -0400162 /* did an abort occur during the submission? */
163 if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 /* cancel the URB, if it hasn't been cancelled already */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400166 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 US_DEBUGP("-- cancelling URB\n");
168 usb_unlink_urb(us->current_urb);
169 }
170 }
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 /* wait for the completion of the URB */
Franck Bui-Huu3428cc42006-05-24 16:57:28 +0200173 timeleft = wait_for_completion_interruptible_timeout(
174 &urb_done, timeout ? : MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Alan Stern7e4d6c32008-05-01 15:35:18 -0400176 clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
Franck Bui-Huu3428cc42006-05-24 16:57:28 +0200177
178 if (timeleft <= 0) {
179 US_DEBUGP("%s -- cancelling URB\n",
180 timeleft == 0 ? "Timeout" : "Signal");
Alan Sternd6b7d3b2006-07-10 04:44:47 -0700181 usb_kill_urb(us->current_urb);
Franck Bui-Huu3428cc42006-05-24 16:57:28 +0200182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
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 */
192int 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",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800199 __func__, request, requesttype,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 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 */
233int 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 */
Matthew Dharm5203ad42005-06-06 17:19:29 -0700247 if (result >= 0)
248 usb_settoggle(us->pusb_dev, usb_pipeendpoint(pipe),
249 usb_pipeout(pipe), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Harvey Harrison441b62c2008-03-03 16:08:34 -0800251 US_DEBUGP("%s: result = %d\n", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 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 */
263static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* 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 */
326int 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",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800333 __func__, request, requesttype,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 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 */
360static 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
Harvey Harrison441b62c2008-03-03 16:08:34 -0800367 US_DEBUGP("%s: xfer %u bytes\n", __func__, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
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 */
389int 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
Harvey Harrison441b62c2008-03-03 16:08:34 -0800394 US_DEBUGP("%s: xfer %u bytes\n", __func__, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
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 */
414static 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
Alan Stern543f7812008-05-08 11:55:59 -0400420 /* don't submit s-g requests during abort processing */
421 if (test_bit(US_FLIDX_ABORTING, &us->dflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return USB_STOR_XFER_ERROR;
423
424 /* initialize the scatter-gather request block */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800425 US_DEBUGP("%s: xfer %u bytes, %d entries\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 length, num_sg);
427 result = usb_sg_init(&us->current_sg, us->pusb_dev, pipe, 0,
Christoph Lameter55acbda2006-12-06 20:33:13 -0800428 sg, num_sg, length, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 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 */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400436 set_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Alan Stern543f7812008-05-08 11:55:59 -0400438 /* did an abort occur during the submission? */
439 if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 /* cancel the request, if it hasn't been cancelled already */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400442 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 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);
Alan Stern7e4d6c32008-05-01 15:35:18 -0400450 clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
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/*
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300460 * Common used function. Transfer a complete command
461 * via usb_stor_bulk_transfer_sglist() above. Set cmnd resid
462 */
463int 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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 * 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 */
484int 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 */
519void 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 */
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300525 scsi_set_resid(srb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 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 */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400531 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 US_DEBUGP("-- command was aborted\n");
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700533 srb->result = DID_ABORT << 16;
534 goto Handle_Errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 srb->result = DID_ERROR << 16;
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700541 goto Handle_Errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
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 /*
Ben Efros1537e0a2008-11-18 13:31:13 -0800582 * 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
585 * SANE_SENSE support
586 */
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;
593 }
594
595 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * 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.
598 */
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300599 if ((scsi_get_resid(srb) > 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 !((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");
606 }
607
608 /* Now, if we need to do the auto-sense, let's do it */
609 if (need_auto_sense) {
610 int temp_result;
Boaz Harroshdff6de72007-09-10 22:36:31 +0300611 struct scsi_eh_save ses;
Ben Efros1537e0a2008-11-18 13:31:13 -0800612 int sense_size = US_SENSE_SIZE;
613
614 /* device supports and needs bigger sense buffer */
615 if (us->fflags & US_FL_SANE_SENSE)
616 sense_size = ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 US_DEBUGP("Issuing auto-REQUEST_SENSE\n");
619
Ben Efros1537e0a2008-11-18 13:31:13 -0800620 scsi_eh_prep_cmnd(srb, &ses, NULL, 0, sense_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 /* FIXME: we must do the protocol translation here */
matthieu castetd2770642008-03-19 19:40:52 +0100623 if (us->subclass == US_SC_RBC || us->subclass == US_SC_SCSI ||
624 us->subclass == US_SC_CYP_ATACB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 srb->cmd_len = 6;
626 else
627 srb->cmd_len = 12;
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* issue the auto-sense command */
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300630 scsi_set_resid(srb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 temp_result = us->transport(us->srb, us);
632
633 /* let's clean up right away */
Boaz Harroshdff6de72007-09-10 22:36:31 +0300634 scsi_eh_restore_cmnd(srb, &ses);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Alan Stern7e4d6c32008-05-01 15:35:18 -0400636 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 US_DEBUGP("-- auto-sense aborted\n");
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700638 srb->result = DID_ABORT << 16;
639 goto Handle_Errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641 if (temp_result != USB_STOR_TRANSPORT_GOOD) {
642 US_DEBUGP("-- auto-sense failure\n");
643
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
647 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 srb->result = DID_ERROR << 16;
Alan Stern7e4d6c32008-05-01 15:35:18 -0400649 if (!(us->fflags & US_FL_SCM_MULT_TARG))
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700650 goto Handle_Errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return;
652 }
653
Ben Efros1537e0a2008-11-18 13:31:13 -0800654 /* 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.
657 */
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;
663
664 /* Indicate to the user that we truncated their sense
665 * because we didn't know it supported larger sense.
666 */
667 US_DEBUGP("-- Sense data truncated to %i from %i\n",
668 US_SENSE_SIZE,
669 srb->sense_buffer[7] + 8);
670 srb->sense_buffer[7] = (US_SENSE_SIZE - 8);
671 }
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 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
680 usb_stor_show_sense(
681 srb->sense_buffer[2] & 0xf,
682 srb->sense_buffer[12],
683 srb->sense_buffer[13]);
684#endif
685
686 /* set the result so the higher layers expect this data */
687 srb->result = SAM_STAT_CHECK_CONDITION;
688
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 &&
695 /* No ASC or ASCQ */
696 srb->sense_buffer[12] == 0 &&
697 srb->sense_buffer[13] == 0) {
698 srb->result = SAM_STAT_GOOD;
699 srb->sense_buffer[0] = 0x0;
700 }
701 }
702
703 /* Did we transfer less than the minimum amount required? */
Alan Stern8bfa2472008-09-02 10:12:11 -0400704 if ((srb->result == SAM_STAT_GOOD || srb->sense_buffer[2] == 0) &&
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300705 scsi_bufflen(srb) - scsi_get_resid(srb) < srb->underflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 srb->result = (DID_ERROR << 16) | (SUGGEST_RETRY << 24);
707
708 return;
709
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700710 /* Error and abort processing: try to resynchronize with the device
711 * by issuing a port reset. If that fails, try a class-specific
712 * device reset. */
713 Handle_Errors:
714
Alan Stern47104b02006-06-01 13:52:56 -0400715 /* Set the RESETTING bit, and clear the ABORTING bit so that
716 * the reset may proceed. */
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700717 scsi_lock(us_to_host(us));
Alan Stern7e4d6c32008-05-01 15:35:18 -0400718 set_bit(US_FLIDX_RESETTING, &us->dflags);
719 clear_bit(US_FLIDX_ABORTING, &us->dflags);
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700720 scsi_unlock(us_to_host(us));
721
Alan Stern47104b02006-06-01 13:52:56 -0400722 /* We must release the device lock because the pre_reset routine
723 * will want to acquire it. */
724 mutex_unlock(&us->dev_mutex);
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700725 result = usb_stor_port_reset(us);
Alan Stern47104b02006-06-01 13:52:56 -0400726 mutex_lock(&us->dev_mutex);
727
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700728 if (result < 0) {
729 scsi_lock(us_to_host(us));
730 usb_stor_report_device_reset(us);
731 scsi_unlock(us_to_host(us));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 us->transport_reset(us);
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700733 }
Alan Stern7e4d6c32008-05-01 15:35:18 -0400734 clear_bit(US_FLIDX_RESETTING, &us->dflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
737/* Stop the current URB transfer */
738void usb_stor_stop_transport(struct us_data *us)
739{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800740 US_DEBUGP("%s called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
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. */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400746 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 US_DEBUGP("-- cancelling URB\n");
748 usb_unlink_urb(us->current_urb);
749 }
750
751 /* If we are waiting for a scatter-gather operation, cancel it. */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400752 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 US_DEBUGP("-- cancelling sg request\n");
754 usb_sg_cancel(&us->current_sg);
755 }
756}
757
758/*
Alan Stern64648a92008-11-20 14:20:03 -0500759 * Control/Bulk and Control/Bulk/Interrupt transport
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 */
761
Alan Stern64648a92008-11-20 14:20:03 -0500762int usb_stor_CB_transport(struct scsi_cmnd *srb, struct us_data *us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300764 unsigned int transfer_length = scsi_bufflen(srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 unsigned int pipe = 0;
766 int result;
767
768 /* COMMAND STAGE */
769 /* let's send the command via the control pipe */
770 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
771 US_CBI_ADSC,
772 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
773 us->ifnum, srb->cmnd, srb->cmd_len);
774
775 /* check the return code for the command */
776 US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result);
777
778 /* if we stalled the command, it means command failed */
779 if (result == USB_STOR_XFER_STALLED) {
780 return USB_STOR_TRANSPORT_FAILED;
781 }
782
783 /* Uh oh... serious problem here */
784 if (result != USB_STOR_XFER_GOOD) {
785 return USB_STOR_TRANSPORT_ERROR;
786 }
787
788 /* DATA STAGE */
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;
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300793 result = usb_stor_bulk_srb(us, pipe, srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 US_DEBUGP("CBI data stage result is 0x%x\n", result);
795
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;
801 }
802
803 /* STATUS STAGE */
Alan Stern64648a92008-11-20 14:20:03 -0500804
805 /* NOTE: CB does not have a status stage. Silly, I know. So
806 * we have to catch this at a higher level.
807 */
808 if (us->protocol != US_PR_CBI)
809 return USB_STOR_TRANSPORT_GOOD;
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 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;
816
817 /* UFI gives us ASC and ASCQ, like a request sense
818 *
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.
823 */
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;
828 if (us->iobuf[0])
829 goto Failed;
830 return USB_STOR_TRANSPORT_GOOD;
831 }
832
833 /* If not UFI, we interpret the data as a result code
834 * The first byte should always be a 0x0.
835 *
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.
838 */
839 if (us->iobuf[0]) {
840 US_DEBUGP("CBI IRQ data showed reserved bType 0x%x\n",
841 us->iobuf[0]);
842 goto Failed;
843
844 }
845
846 /* The second byte & 0x0F should be 0x0 for good, otherwise error */
847 switch (us->iobuf[1] & 0x0F) {
848 case 0x00:
849 return USB_STOR_TRANSPORT_GOOD;
850 case 0x01:
851 goto Failed;
852 }
853 return USB_STOR_TRANSPORT_ERROR;
854
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)
857 */
858 Failed:
859 if (pipe)
860 usb_stor_clear_halt(us, pipe);
861 return USB_STOR_TRANSPORT_FAILED;
862}
863
864/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 * Bulk only transport
866 */
867
868/* Determine what the maximum LUN supported is */
869int usb_stor_Bulk_max_lun(struct us_data *us)
870{
871 int result;
872
873 /* issue the command */
Alan Sternb876aef2005-10-23 19:38:56 -0700874 us->iobuf[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
876 US_BULK_GET_MAX_LUN,
877 USB_DIR_IN | USB_TYPE_CLASS |
878 USB_RECIP_INTERFACE,
879 0, us->ifnum, us->iobuf, 1, HZ);
880
881 US_DEBUGP("GetMaxLUN command result is %d, data is %d\n",
882 result, us->iobuf[0]);
883
884 /* if we have a successful request, return the result */
885 if (result > 0)
886 return us->iobuf[0];
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 /*
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.
894 */
895 return 0;
896}
897
898int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
899{
900 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
901 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300902 unsigned int transfer_length = scsi_bufflen(srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 unsigned int residue;
904 int result;
905 int fake_sense = 0;
906 unsigned int cswlen;
907 unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
908
909 /* Take care of BULK32 devices; set extra byte to 0 */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400910 if (unlikely(us->fflags & US_FL_BULK32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 cbwlen = 32;
912 us->iobuf[31] = 0;
913 }
914
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;
Matthew Dharm0f64e072005-07-28 14:43:08 -0700919 bcb->Tag = ++us->tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 bcb->Lun = srb->device->lun;
Alan Stern7e4d6c32008-05-01 15:35:18 -0400921 if (us->fflags & US_FL_SCM_MULT_TARG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 bcb->Lun |= srb->device->id << 4;
923 bcb->Length = srb->cmd_len;
924
925 /* copy the command payload */
926 memset(bcb->CDB, 0, sizeof(bcb->CDB));
927 memcpy(bcb->CDB, srb->cmnd, bcb->Length);
928
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),
934 bcb->Length);
935 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
936 bcb, cbwlen, NULL);
937 US_DEBUGP("Bulk command transfer result=%d\n", result);
938 if (result != USB_STOR_XFER_GOOD)
939 return USB_STOR_TRANSPORT_ERROR;
940
941 /* DATA STAGE */
942 /* send/receive data payload, if there is any */
943
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. */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400947 if (unlikely(us->fflags & US_FL_GO_SLOW))
Phil Dibowitze4334fa2005-04-18 17:39:27 -0700948 udelay(125);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 if (transfer_length) {
951 unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
952 us->recv_bulk_pipe : us->send_bulk_pipe;
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300953 result = usb_stor_bulk_srb(us, pipe, srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 US_DEBUGP("Bulk data transfer result 0x%x\n", result);
955 if (result == USB_STOR_XFER_ERROR)
956 return USB_STOR_TRANSPORT_ERROR;
957
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.
963 */
964 if (result == USB_STOR_XFER_LONG)
965 fake_sense = 1;
966 }
967
968 /* See flow chart on pg 15 of the Bulk Only Transport spec for
969 * an explanation of how this code works.
970 */
971
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);
976
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.
980 */
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);
985 }
986
987 /* did the attempt to read the CSW fail? */
988 if (result == USB_STOR_XFER_STALLED) {
989
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);
994 }
995
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;
1000
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);
Alan Stern7e4d6c32008-05-01 15:35:18 -04001006 if (!(bcs->Tag == us->tag || (us->fflags & US_FL_BULK_IGNORE_TAG)) ||
Constantin Baranovcc36bdd2008-03-16 20:04:23 +00001007 bcs->Status > US_BULK_STAT_PHASE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 US_DEBUGP("Bulk logical error\n");
1009 return USB_STOR_TRANSPORT_ERROR;
1010 }
1011
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.
1015 */
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;
1026 }
1027
1028 /* try to compute the actual residue, based on how much data
1029 * was really transferred and what the device tells us */
Alan Stern59f4ff22008-07-29 11:58:06 -04001030 if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
1031
1032 /* Heuristically detect devices that generate bogus residues
1033 * by seeing what happens with INQUIRY and READ CAPACITY
1034 * commands.
1035 */
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;
1043
1044 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 residue = min(residue, transfer_length);
Boaz Harrosh6d416e62007-09-10 18:01:08 +03001046 scsi_set_resid(srb, max(scsi_get_resid(srb),
1047 (int) residue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049 }
1050
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 */
1055 if (fake_sense) {
1056 memcpy(srb->sense_buffer,
1057 usb_stor_sense_invalidCDB,
1058 sizeof(usb_stor_sense_invalidCDB));
1059 return USB_STOR_TRANSPORT_NO_SENSE;
1060 }
1061
1062 /* command good -- note that data could be short */
1063 return USB_STOR_TRANSPORT_GOOD;
1064
1065 case US_BULK_STAT_FAIL:
1066 /* command failed */
1067 return USB_STOR_TRANSPORT_FAILED;
1068
1069 case US_BULK_STAT_PHASE:
1070 /* phase error -- note that a transport reset will be
1071 * invoked by the invoke_transport() function
1072 */
1073 return USB_STOR_TRANSPORT_ERROR;
1074 }
1075
1076 /* we should never get here, but if we do, we're in trouble */
1077 return USB_STOR_TRANSPORT_ERROR;
1078}
1079
1080/***********************************************************************
1081 * Reset routines
1082 ***********************************************************************/
1083
1084/* This is the common part of the device reset code.
1085 *
1086 * It's handy that every transport mechanism uses the control endpoint for
1087 * resets.
1088 *
Matthew Dharm5203ad42005-06-06 17:19:29 -07001089 * Basically, we send a reset with a 5-second timeout, so we don't get
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 * jammed attempting to do the reset.
1091 */
1092static int usb_stor_reset_common(struct us_data *us,
1093 u8 request, u8 requesttype,
1094 u16 value, u16 index, void *data, u16 size)
1095{
1096 int result;
1097 int result2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Alan Stern7e4d6c32008-05-01 15:35:18 -04001099 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001100 US_DEBUGP("No reset during disconnect\n");
1101 return -EIO;
1102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
1105 request, requesttype, value, index, data, size,
Matthew Dharm5203ad42005-06-06 17:19:29 -07001106 5*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (result < 0) {
1108 US_DEBUGP("Soft reset failed: %d\n", result);
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001109 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
1111
Alan Stern7e4d6c32008-05-01 15:35:18 -04001112 /* 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),
1116 HZ*6);
1117 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 US_DEBUGP("Reset interrupted by disconnect\n");
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001119 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121
1122 US_DEBUGP("Soft reset: clearing bulk-in endpoint halt\n");
1123 result = usb_stor_clear_halt(us, us->recv_bulk_pipe);
1124
1125 US_DEBUGP("Soft reset: clearing bulk-out endpoint halt\n");
1126 result2 = usb_stor_clear_halt(us, us->send_bulk_pipe);
1127
Matthew Dharm5203ad42005-06-06 17:19:29 -07001128 /* return a result code based on the result of the clear-halts */
1129 if (result >= 0)
1130 result = result2;
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001131 if (result < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 US_DEBUGP("Soft reset failed\n");
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001133 else
1134 US_DEBUGP("Soft reset done\n");
1135 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
1138/* This issues a CB[I] Reset to the device in question
1139 */
1140#define CB_RESET_CMD_SIZE 12
1141
1142int usb_stor_CB_reset(struct us_data *us)
1143{
Harvey Harrison441b62c2008-03-03 16:08:34 -08001144 US_DEBUGP("%s called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 memset(us->iobuf, 0xFF, CB_RESET_CMD_SIZE);
1147 us->iobuf[0] = SEND_DIAGNOSTIC;
1148 us->iobuf[1] = 4;
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);
1152}
1153
1154/* This issues a Bulk-only Reset to the device in question, including
1155 * clearing the subsequent endpoint halts that may occur.
1156 */
1157int usb_stor_Bulk_reset(struct us_data *us)
1158{
Harvey Harrison441b62c2008-03-03 16:08:34 -08001159 US_DEBUGP("%s called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 return usb_stor_reset_common(us, US_BULK_RESET_REQUEST,
1162 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1163 0, us->ifnum, NULL, 0);
1164}
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001165
Alan Stern47104b02006-06-01 13:52:56 -04001166/* Issue a USB port reset to the device. The caller must not hold
1167 * us->dev_mutex.
1168 */
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001169int usb_stor_port_reset(struct us_data *us)
1170{
Alan Stern011b15d2008-11-04 11:29:27 -05001171 int result;
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001172
Alan Stern011b15d2008-11-04 11:29:27 -05001173 result = usb_lock_device_for_reset(us->pusb_dev, us->pusb_intf);
Alan Stern47104b02006-06-01 13:52:56 -04001174 if (result < 0)
1175 US_DEBUGP("unable to lock device for reset: %d\n", result);
1176 else {
1177 /* Were we disconnected while waiting for the lock? */
Alan Stern7e4d6c32008-05-01 15:35:18 -04001178 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
Alan Stern47104b02006-06-01 13:52:56 -04001179 result = -EIO;
1180 US_DEBUGP("No reset during disconnect\n");
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001181 } else {
Ming Lei742120c2008-06-18 22:00:29 +08001182 result = usb_reset_device(us->pusb_dev);
1183 US_DEBUGP("usb_reset_device returns %d\n",
Alan Stern47104b02006-06-01 13:52:56 -04001184 result);
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001185 }
Alan Stern011b15d2008-11-04 11:29:27 -05001186 usb_unlock_device(us->pusb_dev);
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001187 }
1188 return result;
1189}