blob: bdd4334bed5a71c7615455cedbb755925cec1436 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Driver for USB Mass Storage compliant devices
2 *
3 * $Id: transport.c,v 1.47 2002/04/22 03:39:43 mdharm Exp $
4 *
5 * Current development and maintenance by:
6 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
7 *
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>
12 *
13 * Initial work by:
14 * (c) 1999 Michael Gee (michael@linuxspecific.com)
15 *
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.
21 *
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.
26 *
27 * Also, for certain devices, the interrupt endpoint is used to convey
28 * status of a command.
29 *
30 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
31 * information about this driver.
32 *
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
36 * later version.
37 *
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.
42 *
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.
46 */
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/sched.h>
49#include <linux/errno.h>
50#include <linux/slab.h>
51
52#include <scsi/scsi.h>
Boaz Harroshdff6de72007-09-10 22:36:31 +030053#include <scsi/scsi_eh.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <scsi/scsi_device.h>
55
56#include "usb.h"
57#include "transport.h"
58#include "protocol.h"
59#include "scsiglue.h"
60#include "debug.h"
61
62
63/***********************************************************************
64 * Data transfer routines
65 ***********************************************************************/
66
67/*
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.)
76 *
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.
84 *
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.
88 *
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
Alan Sternb375a042005-07-29 16:11:07 -040098 * finish. Note that the URB can still be in progress even after a call to
99 * usb_unlink_urb() returns.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
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().
106 */
107
108/* This is the completion handler which will wake us up when an URB
109 * completes.
110 */
David Howells7d12e782006-10-05 14:55:46 +0100111static void usb_stor_blocking_completion(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 struct completion *urb_done_ptr = (struct completion *)urb->context;
114
115 complete(urb_done_ptr);
116}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/* This is the common part of the URB message submission code
119 *
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.
123 */
124static int usb_stor_msg_common(struct us_data *us, int timeout)
125{
126 struct completion urb_done;
Franck Bui-Huu3428cc42006-05-24 16:57:28 +0200127 long timeleft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int status;
129
130 /* don't submit URBs during abort/disconnect processing */
131 if (us->flags & ABORTING_OR_DISCONNECTING)
132 return -EIO;
133
134 /* set up data structures for the wakeup system */
135 init_completion(&urb_done);
136
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;
142
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. */
Alan Sternb375a042005-07-29 16:11:07 -0400147 us->current_urb->transfer_flags = URB_NO_SETUP_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 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;
152
153 /* submit the URB */
154 status = usb_submit_urb(us->current_urb, GFP_NOIO);
155 if (status) {
156 /* something went wrong */
157 return status;
158 }
159
160 /* since the URB has been submitted successfully, it's now okay
161 * to cancel it */
162 set_bit(US_FLIDX_URB_ACTIVE, &us->flags);
163
164 /* did an abort/disconnect occur during the submission? */
165 if (us->flags & ABORTING_OR_DISCONNECTING) {
166
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);
171 }
172 }
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 /* wait for the completion of the URB */
Franck Bui-Huu3428cc42006-05-24 16:57:28 +0200175 timeleft = wait_for_completion_interruptible_timeout(
176 &urb_done, timeout ? : MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Franck Bui-Huu3428cc42006-05-24 16:57:28 +0200178 clear_bit(US_FLIDX_URB_ACTIVE, &us->flags);
179
180 if (timeleft <= 0) {
181 US_DEBUGP("%s -- cancelling URB\n",
182 timeleft == 0 ? "Timeout" : "Signal");
Alan Sternd6b7d3b2006-07-10 04:44:47 -0700183 usb_kill_urb(us->current_urb);
Franck Bui-Huu3428cc42006-05-24 16:57:28 +0200184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 /* return the URB status */
187 return us->current_urb->status;
188}
189
190/*
191 * Transfer one control message, with timeouts, and allowing early
192 * termination. Return codes are usual -Exxx, *not* USB_STOR_XFER_xxx.
193 */
194int 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)
197{
198 int status;
199
200 US_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
201 __FUNCTION__, request, requesttype,
202 value, index, size);
203
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);
210
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);
216
217 /* return the actual length of the data transferred if no error */
218 if (status == 0)
219 status = us->current_urb->actual_length;
220 return status;
221}
222
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.
226 *
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.
231 *
232 * Since many vendors in this space limit their testing to interoperability
233 * with these two OSes, specification violations like this one are common.
234 */
235int usb_stor_clear_halt(struct us_data *us, unsigned int pipe)
236{
237 int result;
238 int endp = usb_pipeendpoint(pipe);
239
240 if (usb_pipein (pipe))
241 endp |= USB_DIR_IN;
242
243 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
244 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
245 USB_ENDPOINT_HALT, endp,
246 NULL, 0, 3*HZ);
247
248 /* reset the endpoint toggle */
Matthew Dharm5203ad42005-06-06 17:19:29 -0700249 if (result >= 0)
250 usb_settoggle(us->pusb_dev, usb_pipeendpoint(pipe),
251 usb_pipeout(pipe), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 US_DEBUGP("%s: result = %d\n", __FUNCTION__, result);
254 return result;
255}
256
257
258/*
259 * Interpret the results of a URB transfer
260 *
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.
264 */
265static int interpret_urb_result(struct us_data *us, unsigned int pipe,
266 unsigned int length, int result, unsigned int partial)
267{
268 US_DEBUGP("Status code %d; transferred %u/%u\n",
269 result, partial, length);
270 switch (result) {
271
272 /* no error code; did we send all the data? */
273 case 0:
274 if (partial != length) {
275 US_DEBUGP("-- short transfer\n");
276 return USB_STOR_XFER_SHORT;
277 }
278
279 US_DEBUGP("-- transfer complete\n");
280 return USB_STOR_XFER_GOOD;
281
282 /* stalled */
283 case -EPIPE:
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;
289 }
290
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;
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /* babble - the device tried to send more than we wanted to read */
298 case -EOVERFLOW:
299 US_DEBUGP("-- babble\n");
300 return USB_STOR_XFER_LONG;
301
302 /* the transfer was cancelled by abort, disconnect, or timeout */
303 case -ECONNRESET:
304 US_DEBUGP("-- transfer cancelled\n");
305 return USB_STOR_XFER_ERROR;
306
307 /* short scatter-gather read transfer */
308 case -EREMOTEIO:
309 US_DEBUGP("-- short read transfer\n");
310 return USB_STOR_XFER_SHORT;
311
312 /* abort or disconnect in progress */
313 case -EIO:
314 US_DEBUGP("-- abort or disconnect in progress\n");
315 return USB_STOR_XFER_ERROR;
316
317 /* the catch-all error case */
318 default:
319 US_DEBUGP("-- unknown error\n");
320 return USB_STOR_XFER_ERROR;
321 }
322}
323
324/*
325 * Transfer one control message, without timeouts, but allowing early
326 * termination. Return codes are USB_STOR_XFER_xxx.
327 */
328int 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)
331{
332 int result;
333
334 US_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
335 __FUNCTION__, request, requesttype,
336 value, index, size);
337
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);
344
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);
350
351 return interpret_urb_result(us, pipe, size, result,
352 us->current_urb->actual_length);
353}
354
355/*
356 * Receive one interrupt buffer, without timeouts, but allowing early
357 * termination. Return codes are USB_STOR_XFER_xxx.
358 *
359 * This routine always uses us->recv_intr_pipe as the pipe and
360 * us->ep_bInterval as the interrupt interval.
361 */
362static int usb_stor_intr_transfer(struct us_data *us, void *buf,
363 unsigned int length)
364{
365 int result;
366 unsigned int pipe = us->recv_intr_pipe;
367 unsigned int maxp;
368
369 US_DEBUGP("%s: xfer %u bytes\n", __FUNCTION__, length);
370
371 /* calculate the max packet size */
372 maxp = usb_maxpacket(us->pusb_dev, pipe, usb_pipeout(pipe));
373 if (maxp > length)
374 maxp = length;
375
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,
379 us->ep_bInterval);
380 result = usb_stor_msg_common(us, 0);
381
382 return interpret_urb_result(us, pipe, length, result,
383 us->current_urb->actual_length);
384}
385
386/*
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.
390 */
391int usb_stor_bulk_transfer_buf(struct us_data *us, unsigned int pipe,
392 void *buf, unsigned int length, unsigned int *act_len)
393{
394 int result;
395
396 US_DEBUGP("%s: xfer %u bytes\n", __FUNCTION__, length);
397
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);
402
403 /* store the actual length of the data transferred */
404 if (act_len)
405 *act_len = us->current_urb->actual_length;
406 return interpret_urb_result(us, pipe, length, result,
407 us->current_urb->actual_length);
408}
409
410/*
411 * Transfer a scatter-gather list via bulk transfer
412 *
413 * This function does basically the same thing as usb_stor_bulk_transfer_buf()
414 * above, but it uses the usbcore scatter-gather library.
415 */
416static 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)
419{
420 int result;
421
422 /* don't submit s-g requests during abort/disconnect processing */
423 if (us->flags & ABORTING_OR_DISCONNECTING)
424 return USB_STOR_XFER_ERROR;
425
426 /* initialize the scatter-gather request block */
427 US_DEBUGP("%s: xfer %u bytes, %d entries\n", __FUNCTION__,
428 length, num_sg);
429 result = usb_sg_init(&us->current_sg, us->pusb_dev, pipe, 0,
Christoph Lameter55acbda2006-12-06 20:33:13 -0800430 sg, num_sg, length, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (result) {
432 US_DEBUGP("usb_sg_init returned %d\n", result);
433 return USB_STOR_XFER_ERROR;
434 }
435
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);
439
440 /* did an abort/disconnect occur during the submission? */
441 if (us->flags & ABORTING_OR_DISCONNECTING) {
442
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);
447 }
448 }
449
450 /* wait for the completion of the transfer */
451 usb_sg_wait(&us->current_sg);
452 clear_bit(US_FLIDX_SG_ACTIVE, &us->flags);
453
454 result = us->current_sg.status;
455 if (act_len)
456 *act_len = us->current_sg.bytes;
457 return interpret_urb_result(us, pipe, length, result,
458 us->current_sg.bytes);
459}
460
461/*
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300462 * Common used function. Transfer a complete command
463 * via usb_stor_bulk_transfer_sglist() above. Set cmnd resid
464 */
465int usb_stor_bulk_srb(struct us_data* us, unsigned int pipe,
466 struct scsi_cmnd* srb)
467{
468 unsigned int partial;
469 int result = usb_stor_bulk_transfer_sglist(us, pipe, scsi_sglist(srb),
470 scsi_sg_count(srb), scsi_bufflen(srb),
471 &partial);
472
473 scsi_set_resid(srb, scsi_bufflen(srb) - partial);
474 return result;
475}
476
477/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * Transfer an entire SCSI command's worth of data payload over the bulk
479 * pipe.
480 *
481 * Note that this uses usb_stor_bulk_transfer_buf() and
482 * usb_stor_bulk_transfer_sglist() to achieve its goals --
483 * this function simply determines whether we're going to use
484 * scatter-gather or not, and acts appropriately.
485 */
486int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe,
487 void *buf, unsigned int length_left, int use_sg, int *residual)
488{
489 int result;
490 unsigned int partial;
491
492 /* are we scatter-gathering? */
493 if (use_sg) {
494 /* use the usb core scatter-gather primitives */
495 result = usb_stor_bulk_transfer_sglist(us, pipe,
496 (struct scatterlist *) buf, use_sg,
497 length_left, &partial);
498 length_left -= partial;
499 } else {
500 /* no scatter-gather, just make the request */
501 result = usb_stor_bulk_transfer_buf(us, pipe, buf,
502 length_left, &partial);
503 length_left -= partial;
504 }
505
506 /* store the residual and return the error code */
507 if (residual)
508 *residual = length_left;
509 return result;
510}
511
512/***********************************************************************
513 * Transport routines
514 ***********************************************************************/
515
516/* Invoke the transport and basic error-handling/recovery methods
517 *
518 * This is used by the protocol layers to actually send the message to
519 * the device and receive the response.
520 */
521void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
522{
523 int need_auto_sense;
524 int result;
525
526 /* send the command to the transport layer */
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300527 scsi_set_resid(srb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 result = us->transport(srb, us);
529
530 /* if the command gets aborted by the higher layers, we need to
531 * short-circuit all other processing
532 */
533 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
534 US_DEBUGP("-- command was aborted\n");
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700535 srb->result = DID_ABORT << 16;
536 goto Handle_Errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538
539 /* if there is a transport error, reset and don't auto-sense */
540 if (result == USB_STOR_TRANSPORT_ERROR) {
541 US_DEBUGP("-- transport indicates error, resetting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 srb->result = DID_ERROR << 16;
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700543 goto Handle_Errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545
546 /* if the transport provided its own sense data, don't auto-sense */
547 if (result == USB_STOR_TRANSPORT_NO_SENSE) {
548 srb->result = SAM_STAT_CHECK_CONDITION;
549 return;
550 }
551
552 srb->result = SAM_STAT_GOOD;
553
554 /* Determine if we need to auto-sense
555 *
556 * I normally don't use a flag like this, but it's almost impossible
557 * to understand what's going on here if I don't.
558 */
559 need_auto_sense = 0;
560
561 /*
562 * If we're running the CB transport, which is incapable
563 * of determining status on its own, we will auto-sense
564 * unless the operation involved a data-in transfer. Devices
565 * can signal most data-in errors by stalling the bulk-in pipe.
566 */
567 if ((us->protocol == US_PR_CB || us->protocol == US_PR_DPCM_USB) &&
568 srb->sc_data_direction != DMA_FROM_DEVICE) {
569 US_DEBUGP("-- CB transport device requiring auto-sense\n");
570 need_auto_sense = 1;
571 }
572
573 /*
574 * If we have a failure, we're going to do a REQUEST_SENSE
575 * automatically. Note that we differentiate between a command
576 * "failure" and an "error" in the transport mechanism.
577 */
578 if (result == USB_STOR_TRANSPORT_FAILED) {
579 US_DEBUGP("-- transport indicates command failure\n");
580 need_auto_sense = 1;
581 }
582
583 /*
584 * A short transfer on a command where we don't expect it
585 * is unusual, but it doesn't mean we need to auto-sense.
586 */
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300587 if ((scsi_get_resid(srb) > 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 !((srb->cmnd[0] == REQUEST_SENSE) ||
589 (srb->cmnd[0] == INQUIRY) ||
590 (srb->cmnd[0] == MODE_SENSE) ||
591 (srb->cmnd[0] == LOG_SENSE) ||
592 (srb->cmnd[0] == MODE_SENSE_10))) {
593 US_DEBUGP("-- unexpectedly short transfer\n");
594 }
595
596 /* Now, if we need to do the auto-sense, let's do it */
597 if (need_auto_sense) {
598 int temp_result;
Boaz Harroshdff6de72007-09-10 22:36:31 +0300599 struct scsi_eh_save ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 US_DEBUGP("Issuing auto-REQUEST_SENSE\n");
602
Boaz Harroshdff6de72007-09-10 22:36:31 +0300603 scsi_eh_prep_cmnd(srb, &ses, NULL, 0, US_SENSE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /* FIXME: we must do the protocol translation here */
606 if (us->subclass == US_SC_RBC || us->subclass == US_SC_SCSI)
607 srb->cmd_len = 6;
608 else
609 srb->cmd_len = 12;
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /* issue the auto-sense command */
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300612 scsi_set_resid(srb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 temp_result = us->transport(us->srb, us);
614
615 /* let's clean up right away */
Boaz Harroshdff6de72007-09-10 22:36:31 +0300616 scsi_eh_restore_cmnd(srb, &ses);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
619 US_DEBUGP("-- auto-sense aborted\n");
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700620 srb->result = DID_ABORT << 16;
621 goto Handle_Errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623 if (temp_result != USB_STOR_TRANSPORT_GOOD) {
624 US_DEBUGP("-- auto-sense failure\n");
625
626 /* we skip the reset if this happens to be a
627 * multi-target device, since failure of an
628 * auto-sense is perfectly valid
629 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 srb->result = DID_ERROR << 16;
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700631 if (!(us->flags & US_FL_SCM_MULT_TARG))
632 goto Handle_Errors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return;
634 }
635
636 US_DEBUGP("-- Result from auto-sense is %d\n", temp_result);
637 US_DEBUGP("-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n",
638 srb->sense_buffer[0],
639 srb->sense_buffer[2] & 0xf,
640 srb->sense_buffer[12],
641 srb->sense_buffer[13]);
642#ifdef CONFIG_USB_STORAGE_DEBUG
643 usb_stor_show_sense(
644 srb->sense_buffer[2] & 0xf,
645 srb->sense_buffer[12],
646 srb->sense_buffer[13]);
647#endif
648
649 /* set the result so the higher layers expect this data */
650 srb->result = SAM_STAT_CHECK_CONDITION;
651
652 /* If things are really okay, then let's show that. Zero
653 * out the sense buffer so the higher layers won't realize
654 * we did an unsolicited auto-sense. */
655 if (result == USB_STOR_TRANSPORT_GOOD &&
656 /* Filemark 0, ignore EOM, ILI 0, no sense */
657 (srb->sense_buffer[2] & 0xaf) == 0 &&
658 /* No ASC or ASCQ */
659 srb->sense_buffer[12] == 0 &&
660 srb->sense_buffer[13] == 0) {
661 srb->result = SAM_STAT_GOOD;
662 srb->sense_buffer[0] = 0x0;
663 }
664 }
665
666 /* Did we transfer less than the minimum amount required? */
667 if (srb->result == SAM_STAT_GOOD &&
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300668 scsi_bufflen(srb) - scsi_get_resid(srb) < srb->underflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 srb->result = (DID_ERROR << 16) | (SUGGEST_RETRY << 24);
670
671 return;
672
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700673 /* Error and abort processing: try to resynchronize with the device
674 * by issuing a port reset. If that fails, try a class-specific
675 * device reset. */
676 Handle_Errors:
677
Alan Stern47104b02006-06-01 13:52:56 -0400678 /* Set the RESETTING bit, and clear the ABORTING bit so that
679 * the reset may proceed. */
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700680 scsi_lock(us_to_host(us));
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700681 set_bit(US_FLIDX_RESETTING, &us->flags);
682 clear_bit(US_FLIDX_ABORTING, &us->flags);
683 scsi_unlock(us_to_host(us));
684
Alan Stern47104b02006-06-01 13:52:56 -0400685 /* We must release the device lock because the pre_reset routine
686 * will want to acquire it. */
687 mutex_unlock(&us->dev_mutex);
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700688 result = usb_stor_port_reset(us);
Alan Stern47104b02006-06-01 13:52:56 -0400689 mutex_lock(&us->dev_mutex);
690
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700691 if (result < 0) {
692 scsi_lock(us_to_host(us));
693 usb_stor_report_device_reset(us);
694 scsi_unlock(us_to_host(us));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 us->transport_reset(us);
Matthew Dharm4d07ef72005-06-06 17:21:41 -0700696 }
697 clear_bit(US_FLIDX_RESETTING, &us->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
700/* Stop the current URB transfer */
701void usb_stor_stop_transport(struct us_data *us)
702{
703 US_DEBUGP("%s called\n", __FUNCTION__);
704
705 /* If the state machine is blocked waiting for an URB,
706 * let's wake it up. The test_and_clear_bit() call
707 * guarantees that if a URB has just been submitted,
708 * it won't be cancelled more than once. */
709 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->flags)) {
710 US_DEBUGP("-- cancelling URB\n");
711 usb_unlink_urb(us->current_urb);
712 }
713
714 /* If we are waiting for a scatter-gather operation, cancel it. */
715 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->flags)) {
716 US_DEBUGP("-- cancelling sg request\n");
717 usb_sg_cancel(&us->current_sg);
718 }
719}
720
721/*
722 * Control/Bulk/Interrupt transport
723 */
724
725int usb_stor_CBI_transport(struct scsi_cmnd *srb, struct us_data *us)
726{
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300727 unsigned int transfer_length = scsi_bufflen(srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 unsigned int pipe = 0;
729 int result;
730
731 /* COMMAND STAGE */
732 /* let's send the command via the control pipe */
733 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
734 US_CBI_ADSC,
735 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
736 us->ifnum, srb->cmnd, srb->cmd_len);
737
738 /* check the return code for the command */
739 US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result);
740
741 /* if we stalled the command, it means command failed */
742 if (result == USB_STOR_XFER_STALLED) {
743 return USB_STOR_TRANSPORT_FAILED;
744 }
745
746 /* Uh oh... serious problem here */
747 if (result != USB_STOR_XFER_GOOD) {
748 return USB_STOR_TRANSPORT_ERROR;
749 }
750
751 /* DATA STAGE */
752 /* transfer the data payload for this command, if one exists*/
753 if (transfer_length) {
754 pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
755 us->recv_bulk_pipe : us->send_bulk_pipe;
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300756 result = usb_stor_bulk_srb(us, pipe, srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 US_DEBUGP("CBI data stage result is 0x%x\n", result);
758
759 /* if we stalled the data transfer it means command failed */
760 if (result == USB_STOR_XFER_STALLED)
761 return USB_STOR_TRANSPORT_FAILED;
762 if (result > USB_STOR_XFER_STALLED)
763 return USB_STOR_TRANSPORT_ERROR;
764 }
765
766 /* STATUS STAGE */
767 result = usb_stor_intr_transfer(us, us->iobuf, 2);
768 US_DEBUGP("Got interrupt data (0x%x, 0x%x)\n",
769 us->iobuf[0], us->iobuf[1]);
770 if (result != USB_STOR_XFER_GOOD)
771 return USB_STOR_TRANSPORT_ERROR;
772
773 /* UFI gives us ASC and ASCQ, like a request sense
774 *
775 * REQUEST_SENSE and INQUIRY don't affect the sense data on UFI
776 * devices, so we ignore the information for those commands. Note
777 * that this means we could be ignoring a real error on these
778 * commands, but that can't be helped.
779 */
780 if (us->subclass == US_SC_UFI) {
781 if (srb->cmnd[0] == REQUEST_SENSE ||
782 srb->cmnd[0] == INQUIRY)
783 return USB_STOR_TRANSPORT_GOOD;
784 if (us->iobuf[0])
785 goto Failed;
786 return USB_STOR_TRANSPORT_GOOD;
787 }
788
789 /* If not UFI, we interpret the data as a result code
790 * The first byte should always be a 0x0.
791 *
792 * Some bogus devices don't follow that rule. They stuff the ASC
793 * into the first byte -- so if it's non-zero, call it a failure.
794 */
795 if (us->iobuf[0]) {
796 US_DEBUGP("CBI IRQ data showed reserved bType 0x%x\n",
797 us->iobuf[0]);
798 goto Failed;
799
800 }
801
802 /* The second byte & 0x0F should be 0x0 for good, otherwise error */
803 switch (us->iobuf[1] & 0x0F) {
804 case 0x00:
805 return USB_STOR_TRANSPORT_GOOD;
806 case 0x01:
807 goto Failed;
808 }
809 return USB_STOR_TRANSPORT_ERROR;
810
811 /* the CBI spec requires that the bulk pipe must be cleared
812 * following any data-in/out command failure (section 2.4.3.1.3)
813 */
814 Failed:
815 if (pipe)
816 usb_stor_clear_halt(us, pipe);
817 return USB_STOR_TRANSPORT_FAILED;
818}
819
820/*
821 * Control/Bulk transport
822 */
823int usb_stor_CB_transport(struct scsi_cmnd *srb, struct us_data *us)
824{
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300825 unsigned int transfer_length = scsi_bufflen(srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 int result;
827
828 /* COMMAND STAGE */
829 /* let's send the command via the control pipe */
830 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
831 US_CBI_ADSC,
832 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
833 us->ifnum, srb->cmnd, srb->cmd_len);
834
835 /* check the return code for the command */
836 US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result);
837
838 /* if we stalled the command, it means command failed */
839 if (result == USB_STOR_XFER_STALLED) {
840 return USB_STOR_TRANSPORT_FAILED;
841 }
842
843 /* Uh oh... serious problem here */
844 if (result != USB_STOR_XFER_GOOD) {
845 return USB_STOR_TRANSPORT_ERROR;
846 }
847
848 /* DATA STAGE */
849 /* transfer the data payload for this command, if one exists*/
850 if (transfer_length) {
851 unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
852 us->recv_bulk_pipe : us->send_bulk_pipe;
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300853 result = usb_stor_bulk_srb(us, pipe, srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 US_DEBUGP("CB data stage result is 0x%x\n", result);
855
856 /* if we stalled the data transfer it means command failed */
857 if (result == USB_STOR_XFER_STALLED)
858 return USB_STOR_TRANSPORT_FAILED;
859 if (result > USB_STOR_XFER_STALLED)
860 return USB_STOR_TRANSPORT_ERROR;
861 }
862
863 /* STATUS STAGE */
864 /* NOTE: CB does not have a status stage. Silly, I know. So
865 * we have to catch this at a higher level.
866 */
867 return USB_STOR_TRANSPORT_GOOD;
868}
869
870/*
871 * Bulk only transport
872 */
873
874/* Determine what the maximum LUN supported is */
875int usb_stor_Bulk_max_lun(struct us_data *us)
876{
877 int result;
878
879 /* issue the command */
Alan Sternb876aef2005-10-23 19:38:56 -0700880 us->iobuf[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
882 US_BULK_GET_MAX_LUN,
883 USB_DIR_IN | USB_TYPE_CLASS |
884 USB_RECIP_INTERFACE,
885 0, us->ifnum, us->iobuf, 1, HZ);
886
887 US_DEBUGP("GetMaxLUN command result is %d, data is %d\n",
888 result, us->iobuf[0]);
889
890 /* if we have a successful request, return the result */
891 if (result > 0)
892 return us->iobuf[0];
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /*
895 * Some devices don't like GetMaxLUN. They may STALL the control
896 * pipe, they may return a zero-length result, they may do nothing at
897 * all and timeout, or they may fail in even more bizarrely creative
898 * ways. In these cases the best approach is to use the default
899 * value: only one LUN.
900 */
901 return 0;
902}
903
904int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
905{
906 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
907 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300908 unsigned int transfer_length = scsi_bufflen(srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 unsigned int residue;
910 int result;
911 int fake_sense = 0;
912 unsigned int cswlen;
913 unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
914
915 /* Take care of BULK32 devices; set extra byte to 0 */
916 if ( unlikely(us->flags & US_FL_BULK32)) {
917 cbwlen = 32;
918 us->iobuf[31] = 0;
919 }
920
921 /* set up the command wrapper */
922 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
923 bcb->DataTransferLength = cpu_to_le32(transfer_length);
924 bcb->Flags = srb->sc_data_direction == DMA_FROM_DEVICE ? 1 << 7 : 0;
Matthew Dharm0f64e072005-07-28 14:43:08 -0700925 bcb->Tag = ++us->tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 bcb->Lun = srb->device->lun;
927 if (us->flags & US_FL_SCM_MULT_TARG)
928 bcb->Lun |= srb->device->id << 4;
929 bcb->Length = srb->cmd_len;
930
931 /* copy the command payload */
932 memset(bcb->CDB, 0, sizeof(bcb->CDB));
933 memcpy(bcb->CDB, srb->cmnd, bcb->Length);
934
935 /* send it to out endpoint */
936 US_DEBUGP("Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n",
937 le32_to_cpu(bcb->Signature), bcb->Tag,
938 le32_to_cpu(bcb->DataTransferLength), bcb->Flags,
939 (bcb->Lun >> 4), (bcb->Lun & 0x0F),
940 bcb->Length);
941 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
942 bcb, cbwlen, NULL);
943 US_DEBUGP("Bulk command transfer result=%d\n", result);
944 if (result != USB_STOR_XFER_GOOD)
945 return USB_STOR_TRANSPORT_ERROR;
946
947 /* DATA STAGE */
948 /* send/receive data payload, if there is any */
949
950 /* Some USB-IDE converter chips need a 100us delay between the
951 * command phase and the data phase. Some devices need a little
952 * more than that, probably because of clock rate inaccuracies. */
953 if (unlikely(us->flags & US_FL_GO_SLOW))
Phil Dibowitze4334fa2005-04-18 17:39:27 -0700954 udelay(125);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 if (transfer_length) {
957 unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
958 us->recv_bulk_pipe : us->send_bulk_pipe;
Boaz Harrosh6d416e62007-09-10 18:01:08 +0300959 result = usb_stor_bulk_srb(us, pipe, srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 US_DEBUGP("Bulk data transfer result 0x%x\n", result);
961 if (result == USB_STOR_XFER_ERROR)
962 return USB_STOR_TRANSPORT_ERROR;
963
964 /* If the device tried to send back more data than the
965 * amount requested, the spec requires us to transfer
966 * the CSW anyway. Since there's no point retrying the
967 * the command, we'll return fake sense data indicating
968 * Illegal Request, Invalid Field in CDB.
969 */
970 if (result == USB_STOR_XFER_LONG)
971 fake_sense = 1;
972 }
973
974 /* See flow chart on pg 15 of the Bulk Only Transport spec for
975 * an explanation of how this code works.
976 */
977
978 /* get CSW for device status */
979 US_DEBUGP("Attempting to get CSW...\n");
980 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
981 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
982
983 /* Some broken devices add unnecessary zero-length packets to the
984 * end of their data transfers. Such packets show up as 0-length
985 * CSWs. If we encounter such a thing, try to read the CSW again.
986 */
987 if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
988 US_DEBUGP("Received 0-length CSW; retrying...\n");
989 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
990 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
991 }
992
993 /* did the attempt to read the CSW fail? */
994 if (result == USB_STOR_XFER_STALLED) {
995
996 /* get the status again */
997 US_DEBUGP("Attempting to get CSW (2nd try)...\n");
998 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
999 bcs, US_BULK_CS_WRAP_LEN, NULL);
1000 }
1001
1002 /* if we still have a failure at this point, we're in trouble */
1003 US_DEBUGP("Bulk status result = %d\n", result);
1004 if (result != USB_STOR_XFER_GOOD)
1005 return USB_STOR_TRANSPORT_ERROR;
1006
1007 /* check bulk status */
1008 residue = le32_to_cpu(bcs->Residue);
1009 US_DEBUGP("Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
1010 le32_to_cpu(bcs->Signature), bcs->Tag,
1011 residue, bcs->Status);
Constantin Baranovcc36bdd2008-03-16 20:04:23 +00001012 if (!(bcs->Tag == us->tag || (us->flags & US_FL_BULK_IGNORE_TAG)) ||
1013 bcs->Status > US_BULK_STAT_PHASE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 US_DEBUGP("Bulk logical error\n");
1015 return USB_STOR_TRANSPORT_ERROR;
1016 }
1017
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.
1021 */
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;
1032 }
1033
1034 /* try to compute the actual residue, based on how much data
1035 * was really transferred and what the device tells us */
1036 if (residue) {
1037 if (!(us->flags & US_FL_IGNORE_RESIDUE)) {
1038 residue = min(residue, transfer_length);
Boaz Harrosh6d416e62007-09-10 18:01:08 +03001039 scsi_set_resid(srb, max(scsi_get_resid(srb),
1040 (int) residue));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042 }
1043
1044 /* based on the status code, we report good or bad */
1045 switch (bcs->Status) {
1046 case US_BULK_STAT_OK:
1047 /* device babbled -- return fake sense data */
1048 if (fake_sense) {
1049 memcpy(srb->sense_buffer,
1050 usb_stor_sense_invalidCDB,
1051 sizeof(usb_stor_sense_invalidCDB));
1052 return USB_STOR_TRANSPORT_NO_SENSE;
1053 }
1054
1055 /* command good -- note that data could be short */
1056 return USB_STOR_TRANSPORT_GOOD;
1057
1058 case US_BULK_STAT_FAIL:
1059 /* command failed */
1060 return USB_STOR_TRANSPORT_FAILED;
1061
1062 case US_BULK_STAT_PHASE:
1063 /* phase error -- note that a transport reset will be
1064 * invoked by the invoke_transport() function
1065 */
1066 return USB_STOR_TRANSPORT_ERROR;
1067 }
1068
1069 /* we should never get here, but if we do, we're in trouble */
1070 return USB_STOR_TRANSPORT_ERROR;
1071}
1072
1073/***********************************************************************
1074 * Reset routines
1075 ***********************************************************************/
1076
1077/* This is the common part of the device reset code.
1078 *
1079 * It's handy that every transport mechanism uses the control endpoint for
1080 * resets.
1081 *
Matthew Dharm5203ad42005-06-06 17:19:29 -07001082 * Basically, we send a reset with a 5-second timeout, so we don't get
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 * jammed attempting to do the reset.
1084 */
1085static int usb_stor_reset_common(struct us_data *us,
1086 u8 request, u8 requesttype,
1087 u16 value, u16 index, void *data, u16 size)
1088{
1089 int result;
1090 int result2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001092 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
1093 US_DEBUGP("No reset during disconnect\n");
1094 return -EIO;
1095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
1098 request, requesttype, value, index, data, size,
Matthew Dharm5203ad42005-06-06 17:19:29 -07001099 5*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (result < 0) {
1101 US_DEBUGP("Soft reset failed: %d\n", result);
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001102 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104
1105 /* Give the device some time to recover from the reset,
1106 * but don't delay disconnect processing. */
1107 wait_event_interruptible_timeout(us->delay_wait,
1108 test_bit(US_FLIDX_DISCONNECTING, &us->flags),
1109 HZ*6);
1110 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
1111 US_DEBUGP("Reset interrupted by disconnect\n");
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001112 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
1114
1115 US_DEBUGP("Soft reset: clearing bulk-in endpoint halt\n");
1116 result = usb_stor_clear_halt(us, us->recv_bulk_pipe);
1117
1118 US_DEBUGP("Soft reset: clearing bulk-out endpoint halt\n");
1119 result2 = usb_stor_clear_halt(us, us->send_bulk_pipe);
1120
Matthew Dharm5203ad42005-06-06 17:19:29 -07001121 /* return a result code based on the result of the clear-halts */
1122 if (result >= 0)
1123 result = result2;
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001124 if (result < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 US_DEBUGP("Soft reset failed\n");
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001126 else
1127 US_DEBUGP("Soft reset done\n");
1128 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131/* This issues a CB[I] Reset to the device in question
1132 */
1133#define CB_RESET_CMD_SIZE 12
1134
1135int usb_stor_CB_reset(struct us_data *us)
1136{
1137 US_DEBUGP("%s called\n", __FUNCTION__);
1138
1139 memset(us->iobuf, 0xFF, CB_RESET_CMD_SIZE);
1140 us->iobuf[0] = SEND_DIAGNOSTIC;
1141 us->iobuf[1] = 4;
1142 return usb_stor_reset_common(us, US_CBI_ADSC,
1143 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1144 0, us->ifnum, us->iobuf, CB_RESET_CMD_SIZE);
1145}
1146
1147/* This issues a Bulk-only Reset to the device in question, including
1148 * clearing the subsequent endpoint halts that may occur.
1149 */
1150int usb_stor_Bulk_reset(struct us_data *us)
1151{
1152 US_DEBUGP("%s called\n", __FUNCTION__);
1153
1154 return usb_stor_reset_common(us, US_BULK_RESET_REQUEST,
1155 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1156 0, us->ifnum, NULL, 0);
1157}
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001158
Alan Stern47104b02006-06-01 13:52:56 -04001159/* Issue a USB port reset to the device. The caller must not hold
1160 * us->dev_mutex.
1161 */
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001162int usb_stor_port_reset(struct us_data *us)
1163{
Alan Stern47104b02006-06-01 13:52:56 -04001164 int result, rc_lock;
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001165
Alan Stern47104b02006-06-01 13:52:56 -04001166 result = rc_lock =
1167 usb_lock_device_for_reset(us->pusb_dev, us->pusb_intf);
1168 if (result < 0)
1169 US_DEBUGP("unable to lock device for reset: %d\n", result);
1170 else {
1171 /* Were we disconnected while waiting for the lock? */
1172 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
1173 result = -EIO;
1174 US_DEBUGP("No reset during disconnect\n");
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001175 } else {
Alan Stern47104b02006-06-01 13:52:56 -04001176 result = usb_reset_composite_device(
1177 us->pusb_dev, us->pusb_intf);
1178 US_DEBUGP("usb_reset_composite_device returns %d\n",
1179 result);
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001180 }
Alan Stern47104b02006-06-01 13:52:56 -04001181 if (rc_lock)
1182 usb_unlock_device(us->pusb_dev);
Matthew Dharm4d07ef72005-06-06 17:21:41 -07001183 }
1184 return result;
1185}