]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/usb/storage/transport.c
[PATCH] pci: initialize struct pci_dev.error_state
[net-next-2.6.git] / drivers / usb / storage / transport.c
CommitLineData
1da177e4
LT
1/* 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
1da177e4
LT
48#include <linux/sched.h>
49#include <linux/errno.h>
50#include <linux/slab.h>
51
52#include <scsi/scsi.h>
53#include <scsi/scsi_cmnd.h>
54#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
b375a049
AS
98 * finish. Note that the URB can still be in progress even after a call to
99 * usb_unlink_urb() returns.
1da177e4
LT
100 *
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 */
111static void usb_stor_blocking_completion(struct urb *urb, struct pt_regs *regs)
112{
113 struct completion *urb_done_ptr = (struct completion *)urb->context;
114
115 complete(urb_done_ptr);
116}
1da177e4
LT
117
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;
3428cc43 127 long timeleft;
1da177e4
LT
128 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. */
b375a049 147 us->current_urb->transfer_flags = URB_NO_SETUP_DMA_MAP;
1da177e4
LT
148 if (us->current_urb->transfer_buffer == us->iobuf)
149 us->current_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
150 us->current_urb->transfer_dma = us->iobuf_dma;
151 us->current_urb->setup_dma = us->cr_dma;
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
1da177e4 174 /* wait for the completion of the URB */
3428cc43
FBH
175 timeleft = wait_for_completion_interruptible_timeout(
176 &urb_done, timeout ? : MAX_SCHEDULE_TIMEOUT);
1da177e4 177
3428cc43
FBH
178 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");
183 usb_unlink_urb(us->current_urb);
184 }
1da177e4
LT
185
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 */
5203ad44
MD
249 if (result >= 0)
250 usb_settoggle(us->pusb_dev, usb_pipeendpoint(pipe),
251 usb_pipeout(pipe), 0);
1da177e4
LT
252
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
297 /* timeout or excessively long NAK */
298 case -ETIMEDOUT:
299 US_DEBUGP("-- timeout or NAK\n");
300 return USB_STOR_XFER_ERROR;
301
302 /* babble - the device tried to send more than we wanted to read */
303 case -EOVERFLOW:
304 US_DEBUGP("-- babble\n");
305 return USB_STOR_XFER_LONG;
306
307 /* the transfer was cancelled by abort, disconnect, or timeout */
308 case -ECONNRESET:
309 US_DEBUGP("-- transfer cancelled\n");
310 return USB_STOR_XFER_ERROR;
311
312 /* short scatter-gather read transfer */
313 case -EREMOTEIO:
314 US_DEBUGP("-- short read transfer\n");
315 return USB_STOR_XFER_SHORT;
316
317 /* abort or disconnect in progress */
318 case -EIO:
319 US_DEBUGP("-- abort or disconnect in progress\n");
320 return USB_STOR_XFER_ERROR;
321
322 /* the catch-all error case */
323 default:
324 US_DEBUGP("-- unknown error\n");
325 return USB_STOR_XFER_ERROR;
326 }
327}
328
329/*
330 * Transfer one control message, without timeouts, but allowing early
331 * termination. Return codes are USB_STOR_XFER_xxx.
332 */
333int usb_stor_ctrl_transfer(struct us_data *us, unsigned int pipe,
334 u8 request, u8 requesttype, u16 value, u16 index,
335 void *data, u16 size)
336{
337 int result;
338
339 US_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
340 __FUNCTION__, request, requesttype,
341 value, index, size);
342
343 /* fill in the devrequest structure */
344 us->cr->bRequestType = requesttype;
345 us->cr->bRequest = request;
346 us->cr->wValue = cpu_to_le16(value);
347 us->cr->wIndex = cpu_to_le16(index);
348 us->cr->wLength = cpu_to_le16(size);
349
350 /* fill and submit the URB */
351 usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
352 (unsigned char*) us->cr, data, size,
353 usb_stor_blocking_completion, NULL);
354 result = usb_stor_msg_common(us, 0);
355
356 return interpret_urb_result(us, pipe, size, result,
357 us->current_urb->actual_length);
358}
359
360/*
361 * Receive one interrupt buffer, without timeouts, but allowing early
362 * termination. Return codes are USB_STOR_XFER_xxx.
363 *
364 * This routine always uses us->recv_intr_pipe as the pipe and
365 * us->ep_bInterval as the interrupt interval.
366 */
367static int usb_stor_intr_transfer(struct us_data *us, void *buf,
368 unsigned int length)
369{
370 int result;
371 unsigned int pipe = us->recv_intr_pipe;
372 unsigned int maxp;
373
374 US_DEBUGP("%s: xfer %u bytes\n", __FUNCTION__, length);
375
376 /* calculate the max packet size */
377 maxp = usb_maxpacket(us->pusb_dev, pipe, usb_pipeout(pipe));
378 if (maxp > length)
379 maxp = length;
380
381 /* fill and submit the URB */
382 usb_fill_int_urb(us->current_urb, us->pusb_dev, pipe, buf,
383 maxp, usb_stor_blocking_completion, NULL,
384 us->ep_bInterval);
385 result = usb_stor_msg_common(us, 0);
386
387 return interpret_urb_result(us, pipe, length, result,
388 us->current_urb->actual_length);
389}
390
391/*
392 * Transfer one buffer via bulk pipe, without timeouts, but allowing early
393 * termination. Return codes are USB_STOR_XFER_xxx. If the bulk pipe
394 * stalls during the transfer, the halt is automatically cleared.
395 */
396int usb_stor_bulk_transfer_buf(struct us_data *us, unsigned int pipe,
397 void *buf, unsigned int length, unsigned int *act_len)
398{
399 int result;
400
401 US_DEBUGP("%s: xfer %u bytes\n", __FUNCTION__, length);
402
403 /* fill and submit the URB */
404 usb_fill_bulk_urb(us->current_urb, us->pusb_dev, pipe, buf, length,
405 usb_stor_blocking_completion, NULL);
406 result = usb_stor_msg_common(us, 0);
407
408 /* store the actual length of the data transferred */
409 if (act_len)
410 *act_len = us->current_urb->actual_length;
411 return interpret_urb_result(us, pipe, length, result,
412 us->current_urb->actual_length);
413}
414
415/*
416 * Transfer a scatter-gather list via bulk transfer
417 *
418 * This function does basically the same thing as usb_stor_bulk_transfer_buf()
419 * above, but it uses the usbcore scatter-gather library.
420 */
421static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
422 struct scatterlist *sg, int num_sg, unsigned int length,
423 unsigned int *act_len)
424{
425 int result;
426
427 /* don't submit s-g requests during abort/disconnect processing */
428 if (us->flags & ABORTING_OR_DISCONNECTING)
429 return USB_STOR_XFER_ERROR;
430
431 /* initialize the scatter-gather request block */
432 US_DEBUGP("%s: xfer %u bytes, %d entries\n", __FUNCTION__,
433 length, num_sg);
434 result = usb_sg_init(&us->current_sg, us->pusb_dev, pipe, 0,
435 sg, num_sg, length, SLAB_NOIO);
436 if (result) {
437 US_DEBUGP("usb_sg_init returned %d\n", result);
438 return USB_STOR_XFER_ERROR;
439 }
440
441 /* since the block has been initialized successfully, it's now
442 * okay to cancel it */
443 set_bit(US_FLIDX_SG_ACTIVE, &us->flags);
444
445 /* did an abort/disconnect occur during the submission? */
446 if (us->flags & ABORTING_OR_DISCONNECTING) {
447
448 /* cancel the request, if it hasn't been cancelled already */
449 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->flags)) {
450 US_DEBUGP("-- cancelling sg request\n");
451 usb_sg_cancel(&us->current_sg);
452 }
453 }
454
455 /* wait for the completion of the transfer */
456 usb_sg_wait(&us->current_sg);
457 clear_bit(US_FLIDX_SG_ACTIVE, &us->flags);
458
459 result = us->current_sg.status;
460 if (act_len)
461 *act_len = us->current_sg.bytes;
462 return interpret_urb_result(us, pipe, length, result,
463 us->current_sg.bytes);
464}
465
466/*
467 * Transfer an entire SCSI command's worth of data payload over the bulk
468 * pipe.
469 *
470 * Note that this uses usb_stor_bulk_transfer_buf() and
471 * usb_stor_bulk_transfer_sglist() to achieve its goals --
472 * this function simply determines whether we're going to use
473 * scatter-gather or not, and acts appropriately.
474 */
475int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe,
476 void *buf, unsigned int length_left, int use_sg, int *residual)
477{
478 int result;
479 unsigned int partial;
480
481 /* are we scatter-gathering? */
482 if (use_sg) {
483 /* use the usb core scatter-gather primitives */
484 result = usb_stor_bulk_transfer_sglist(us, pipe,
485 (struct scatterlist *) buf, use_sg,
486 length_left, &partial);
487 length_left -= partial;
488 } else {
489 /* no scatter-gather, just make the request */
490 result = usb_stor_bulk_transfer_buf(us, pipe, buf,
491 length_left, &partial);
492 length_left -= partial;
493 }
494
495 /* store the residual and return the error code */
496 if (residual)
497 *residual = length_left;
498 return result;
499}
500
501/***********************************************************************
502 * Transport routines
503 ***********************************************************************/
504
505/* Invoke the transport and basic error-handling/recovery methods
506 *
507 * This is used by the protocol layers to actually send the message to
508 * the device and receive the response.
509 */
510void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
511{
512 int need_auto_sense;
513 int result;
514
515 /* send the command to the transport layer */
516 srb->resid = 0;
517 result = us->transport(srb, us);
518
519 /* if the command gets aborted by the higher layers, we need to
520 * short-circuit all other processing
521 */
522 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
523 US_DEBUGP("-- command was aborted\n");
4d07ef76
MD
524 srb->result = DID_ABORT << 16;
525 goto Handle_Errors;
1da177e4
LT
526 }
527
528 /* if there is a transport error, reset and don't auto-sense */
529 if (result == USB_STOR_TRANSPORT_ERROR) {
530 US_DEBUGP("-- transport indicates error, resetting\n");
1da177e4 531 srb->result = DID_ERROR << 16;
4d07ef76 532 goto Handle_Errors;
1da177e4
LT
533 }
534
535 /* if the transport provided its own sense data, don't auto-sense */
536 if (result == USB_STOR_TRANSPORT_NO_SENSE) {
537 srb->result = SAM_STAT_CHECK_CONDITION;
538 return;
539 }
540
541 srb->result = SAM_STAT_GOOD;
542
543 /* Determine if we need to auto-sense
544 *
545 * I normally don't use a flag like this, but it's almost impossible
546 * to understand what's going on here if I don't.
547 */
548 need_auto_sense = 0;
549
550 /*
551 * If we're running the CB transport, which is incapable
552 * of determining status on its own, we will auto-sense
553 * unless the operation involved a data-in transfer. Devices
554 * can signal most data-in errors by stalling the bulk-in pipe.
555 */
556 if ((us->protocol == US_PR_CB || us->protocol == US_PR_DPCM_USB) &&
557 srb->sc_data_direction != DMA_FROM_DEVICE) {
558 US_DEBUGP("-- CB transport device requiring auto-sense\n");
559 need_auto_sense = 1;
560 }
561
562 /*
563 * If we have a failure, we're going to do a REQUEST_SENSE
564 * automatically. Note that we differentiate between a command
565 * "failure" and an "error" in the transport mechanism.
566 */
567 if (result == USB_STOR_TRANSPORT_FAILED) {
568 US_DEBUGP("-- transport indicates command failure\n");
569 need_auto_sense = 1;
570 }
571
572 /*
573 * A short transfer on a command where we don't expect it
574 * is unusual, but it doesn't mean we need to auto-sense.
575 */
576 if ((srb->resid > 0) &&
577 !((srb->cmnd[0] == REQUEST_SENSE) ||
578 (srb->cmnd[0] == INQUIRY) ||
579 (srb->cmnd[0] == MODE_SENSE) ||
580 (srb->cmnd[0] == LOG_SENSE) ||
581 (srb->cmnd[0] == MODE_SENSE_10))) {
582 US_DEBUGP("-- unexpectedly short transfer\n");
583 }
584
585 /* Now, if we need to do the auto-sense, let's do it */
586 if (need_auto_sense) {
587 int temp_result;
588 void* old_request_buffer;
589 unsigned short old_sg;
590 unsigned old_request_bufflen;
591 unsigned char old_sc_data_direction;
592 unsigned char old_cmd_len;
593 unsigned char old_cmnd[MAX_COMMAND_SIZE];
1da177e4
LT
594 int old_resid;
595
596 US_DEBUGP("Issuing auto-REQUEST_SENSE\n");
597
598 /* save the old command */
599 memcpy(old_cmnd, srb->cmnd, MAX_COMMAND_SIZE);
600 old_cmd_len = srb->cmd_len;
601
602 /* set the command and the LUN */
603 memset(srb->cmnd, 0, MAX_COMMAND_SIZE);
604 srb->cmnd[0] = REQUEST_SENSE;
605 srb->cmnd[1] = old_cmnd[1] & 0xE0;
606 srb->cmnd[4] = 18;
607
608 /* FIXME: we must do the protocol translation here */
609 if (us->subclass == US_SC_RBC || us->subclass == US_SC_SCSI)
610 srb->cmd_len = 6;
611 else
612 srb->cmd_len = 12;
613
614 /* set the transfer direction */
615 old_sc_data_direction = srb->sc_data_direction;
616 srb->sc_data_direction = DMA_FROM_DEVICE;
617
618 /* use the new buffer we have */
619 old_request_buffer = srb->request_buffer;
bbafa466 620 srb->request_buffer = us->sensebuf;
1da177e4
LT
621
622 /* set the buffer length for transfer */
623 old_request_bufflen = srb->request_bufflen;
bbafa466 624 srb->request_bufflen = US_SENSE_SIZE;
1da177e4
LT
625
626 /* set up for no scatter-gather use */
627 old_sg = srb->use_sg;
628 srb->use_sg = 0;
629
1da177e4
LT
630 /* issue the auto-sense command */
631 old_resid = srb->resid;
632 srb->resid = 0;
633 temp_result = us->transport(us->srb, us);
634
635 /* let's clean up right away */
bbafa466 636 memcpy(srb->sense_buffer, us->sensebuf, US_SENSE_SIZE);
1da177e4
LT
637 srb->resid = old_resid;
638 srb->request_buffer = old_request_buffer;
639 srb->request_bufflen = old_request_bufflen;
640 srb->use_sg = old_sg;
1da177e4
LT
641 srb->sc_data_direction = old_sc_data_direction;
642 srb->cmd_len = old_cmd_len;
643 memcpy(srb->cmnd, old_cmnd, MAX_COMMAND_SIZE);
644
645 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
646 US_DEBUGP("-- auto-sense aborted\n");
4d07ef76
MD
647 srb->result = DID_ABORT << 16;
648 goto Handle_Errors;
1da177e4
LT
649 }
650 if (temp_result != USB_STOR_TRANSPORT_GOOD) {
651 US_DEBUGP("-- auto-sense failure\n");
652
653 /* we skip the reset if this happens to be a
654 * multi-target device, since failure of an
655 * auto-sense is perfectly valid
656 */
1da177e4 657 srb->result = DID_ERROR << 16;
4d07ef76
MD
658 if (!(us->flags & US_FL_SCM_MULT_TARG))
659 goto Handle_Errors;
1da177e4
LT
660 return;
661 }
662
663 US_DEBUGP("-- Result from auto-sense is %d\n", temp_result);
664 US_DEBUGP("-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n",
665 srb->sense_buffer[0],
666 srb->sense_buffer[2] & 0xf,
667 srb->sense_buffer[12],
668 srb->sense_buffer[13]);
669#ifdef CONFIG_USB_STORAGE_DEBUG
670 usb_stor_show_sense(
671 srb->sense_buffer[2] & 0xf,
672 srb->sense_buffer[12],
673 srb->sense_buffer[13]);
674#endif
675
676 /* set the result so the higher layers expect this data */
677 srb->result = SAM_STAT_CHECK_CONDITION;
678
679 /* If things are really okay, then let's show that. Zero
680 * out the sense buffer so the higher layers won't realize
681 * we did an unsolicited auto-sense. */
682 if (result == USB_STOR_TRANSPORT_GOOD &&
683 /* Filemark 0, ignore EOM, ILI 0, no sense */
684 (srb->sense_buffer[2] & 0xaf) == 0 &&
685 /* No ASC or ASCQ */
686 srb->sense_buffer[12] == 0 &&
687 srb->sense_buffer[13] == 0) {
688 srb->result = SAM_STAT_GOOD;
689 srb->sense_buffer[0] = 0x0;
690 }
691 }
692
693 /* Did we transfer less than the minimum amount required? */
694 if (srb->result == SAM_STAT_GOOD &&
695 srb->request_bufflen - srb->resid < srb->underflow)
696 srb->result = (DID_ERROR << 16) | (SUGGEST_RETRY << 24);
697
698 return;
699
4d07ef76
MD
700 /* Error and abort processing: try to resynchronize with the device
701 * by issuing a port reset. If that fails, try a class-specific
702 * device reset. */
703 Handle_Errors:
704
47104b0d
AS
705 /* Set the RESETTING bit, and clear the ABORTING bit so that
706 * the reset may proceed. */
4d07ef76 707 scsi_lock(us_to_host(us));
4d07ef76
MD
708 set_bit(US_FLIDX_RESETTING, &us->flags);
709 clear_bit(US_FLIDX_ABORTING, &us->flags);
710 scsi_unlock(us_to_host(us));
711
47104b0d
AS
712 /* We must release the device lock because the pre_reset routine
713 * will want to acquire it. */
714 mutex_unlock(&us->dev_mutex);
4d07ef76 715 result = usb_stor_port_reset(us);
47104b0d
AS
716 mutex_lock(&us->dev_mutex);
717
4d07ef76
MD
718 if (result < 0) {
719 scsi_lock(us_to_host(us));
720 usb_stor_report_device_reset(us);
721 scsi_unlock(us_to_host(us));
1da177e4 722 us->transport_reset(us);
4d07ef76
MD
723 }
724 clear_bit(US_FLIDX_RESETTING, &us->flags);
1da177e4
LT
725}
726
727/* Stop the current URB transfer */
728void usb_stor_stop_transport(struct us_data *us)
729{
730 US_DEBUGP("%s called\n", __FUNCTION__);
731
732 /* If the state machine is blocked waiting for an URB,
733 * let's wake it up. The test_and_clear_bit() call
734 * guarantees that if a URB has just been submitted,
735 * it won't be cancelled more than once. */
736 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->flags)) {
737 US_DEBUGP("-- cancelling URB\n");
738 usb_unlink_urb(us->current_urb);
739 }
740
741 /* If we are waiting for a scatter-gather operation, cancel it. */
742 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->flags)) {
743 US_DEBUGP("-- cancelling sg request\n");
744 usb_sg_cancel(&us->current_sg);
745 }
746}
747
748/*
749 * Control/Bulk/Interrupt transport
750 */
751
752int usb_stor_CBI_transport(struct scsi_cmnd *srb, struct us_data *us)
753{
754 unsigned int transfer_length = srb->request_bufflen;
755 unsigned int pipe = 0;
756 int result;
757
758 /* COMMAND STAGE */
759 /* let's send the command via the control pipe */
760 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
761 US_CBI_ADSC,
762 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
763 us->ifnum, srb->cmnd, srb->cmd_len);
764
765 /* check the return code for the command */
766 US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result);
767
768 /* if we stalled the command, it means command failed */
769 if (result == USB_STOR_XFER_STALLED) {
770 return USB_STOR_TRANSPORT_FAILED;
771 }
772
773 /* Uh oh... serious problem here */
774 if (result != USB_STOR_XFER_GOOD) {
775 return USB_STOR_TRANSPORT_ERROR;
776 }
777
778 /* DATA STAGE */
779 /* transfer the data payload for this command, if one exists*/
780 if (transfer_length) {
781 pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
782 us->recv_bulk_pipe : us->send_bulk_pipe;
783 result = usb_stor_bulk_transfer_sg(us, pipe,
784 srb->request_buffer, transfer_length,
785 srb->use_sg, &srb->resid);
786 US_DEBUGP("CBI data stage result is 0x%x\n", result);
787
788 /* if we stalled the data transfer it means command failed */
789 if (result == USB_STOR_XFER_STALLED)
790 return USB_STOR_TRANSPORT_FAILED;
791 if (result > USB_STOR_XFER_STALLED)
792 return USB_STOR_TRANSPORT_ERROR;
793 }
794
795 /* STATUS STAGE */
796 result = usb_stor_intr_transfer(us, us->iobuf, 2);
797 US_DEBUGP("Got interrupt data (0x%x, 0x%x)\n",
798 us->iobuf[0], us->iobuf[1]);
799 if (result != USB_STOR_XFER_GOOD)
800 return USB_STOR_TRANSPORT_ERROR;
801
802 /* UFI gives us ASC and ASCQ, like a request sense
803 *
804 * REQUEST_SENSE and INQUIRY don't affect the sense data on UFI
805 * devices, so we ignore the information for those commands. Note
806 * that this means we could be ignoring a real error on these
807 * commands, but that can't be helped.
808 */
809 if (us->subclass == US_SC_UFI) {
810 if (srb->cmnd[0] == REQUEST_SENSE ||
811 srb->cmnd[0] == INQUIRY)
812 return USB_STOR_TRANSPORT_GOOD;
813 if (us->iobuf[0])
814 goto Failed;
815 return USB_STOR_TRANSPORT_GOOD;
816 }
817
818 /* If not UFI, we interpret the data as a result code
819 * The first byte should always be a 0x0.
820 *
821 * Some bogus devices don't follow that rule. They stuff the ASC
822 * into the first byte -- so if it's non-zero, call it a failure.
823 */
824 if (us->iobuf[0]) {
825 US_DEBUGP("CBI IRQ data showed reserved bType 0x%x\n",
826 us->iobuf[0]);
827 goto Failed;
828
829 }
830
831 /* The second byte & 0x0F should be 0x0 for good, otherwise error */
832 switch (us->iobuf[1] & 0x0F) {
833 case 0x00:
834 return USB_STOR_TRANSPORT_GOOD;
835 case 0x01:
836 goto Failed;
837 }
838 return USB_STOR_TRANSPORT_ERROR;
839
840 /* the CBI spec requires that the bulk pipe must be cleared
841 * following any data-in/out command failure (section 2.4.3.1.3)
842 */
843 Failed:
844 if (pipe)
845 usb_stor_clear_halt(us, pipe);
846 return USB_STOR_TRANSPORT_FAILED;
847}
848
849/*
850 * Control/Bulk transport
851 */
852int usb_stor_CB_transport(struct scsi_cmnd *srb, struct us_data *us)
853{
854 unsigned int transfer_length = srb->request_bufflen;
855 int result;
856
857 /* COMMAND STAGE */
858 /* let's send the command via the control pipe */
859 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
860 US_CBI_ADSC,
861 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
862 us->ifnum, srb->cmnd, srb->cmd_len);
863
864 /* check the return code for the command */
865 US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result);
866
867 /* if we stalled the command, it means command failed */
868 if (result == USB_STOR_XFER_STALLED) {
869 return USB_STOR_TRANSPORT_FAILED;
870 }
871
872 /* Uh oh... serious problem here */
873 if (result != USB_STOR_XFER_GOOD) {
874 return USB_STOR_TRANSPORT_ERROR;
875 }
876
877 /* DATA STAGE */
878 /* transfer the data payload for this command, if one exists*/
879 if (transfer_length) {
880 unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
881 us->recv_bulk_pipe : us->send_bulk_pipe;
882 result = usb_stor_bulk_transfer_sg(us, pipe,
883 srb->request_buffer, transfer_length,
884 srb->use_sg, &srb->resid);
885 US_DEBUGP("CB data stage result is 0x%x\n", result);
886
887 /* if we stalled the data transfer it means command failed */
888 if (result == USB_STOR_XFER_STALLED)
889 return USB_STOR_TRANSPORT_FAILED;
890 if (result > USB_STOR_XFER_STALLED)
891 return USB_STOR_TRANSPORT_ERROR;
892 }
893
894 /* STATUS STAGE */
895 /* NOTE: CB does not have a status stage. Silly, I know. So
896 * we have to catch this at a higher level.
897 */
898 return USB_STOR_TRANSPORT_GOOD;
899}
900
901/*
902 * Bulk only transport
903 */
904
905/* Determine what the maximum LUN supported is */
906int usb_stor_Bulk_max_lun(struct us_data *us)
907{
908 int result;
909
910 /* issue the command */
b876aef7 911 us->iobuf[0] = 0;
1da177e4
LT
912 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
913 US_BULK_GET_MAX_LUN,
914 USB_DIR_IN | USB_TYPE_CLASS |
915 USB_RECIP_INTERFACE,
916 0, us->ifnum, us->iobuf, 1, HZ);
917
918 US_DEBUGP("GetMaxLUN command result is %d, data is %d\n",
919 result, us->iobuf[0]);
920
921 /* if we have a successful request, return the result */
922 if (result > 0)
923 return us->iobuf[0];
924
925 /*
926 * Some devices (i.e. Iomega Zip100) need this -- apparently
927 * the bulk pipes get STALLed when the GetMaxLUN request is
928 * processed. This is, in theory, harmless to all other devices
929 * (regardless of if they stall or not).
930 */
931 if (result == -EPIPE) {
932 usb_stor_clear_halt(us, us->recv_bulk_pipe);
933 usb_stor_clear_halt(us, us->send_bulk_pipe);
934 }
935
936 /*
937 * Some devices don't like GetMaxLUN. They may STALL the control
938 * pipe, they may return a zero-length result, they may do nothing at
939 * all and timeout, or they may fail in even more bizarrely creative
940 * ways. In these cases the best approach is to use the default
941 * value: only one LUN.
942 */
943 return 0;
944}
945
946int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
947{
948 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
949 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
950 unsigned int transfer_length = srb->request_bufflen;
951 unsigned int residue;
952 int result;
953 int fake_sense = 0;
954 unsigned int cswlen;
955 unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
956
957 /* Take care of BULK32 devices; set extra byte to 0 */
958 if ( unlikely(us->flags & US_FL_BULK32)) {
959 cbwlen = 32;
960 us->iobuf[31] = 0;
961 }
962
963 /* set up the command wrapper */
964 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
965 bcb->DataTransferLength = cpu_to_le32(transfer_length);
966 bcb->Flags = srb->sc_data_direction == DMA_FROM_DEVICE ? 1 << 7 : 0;
0f64e078 967 bcb->Tag = ++us->tag;
1da177e4
LT
968 bcb->Lun = srb->device->lun;
969 if (us->flags & US_FL_SCM_MULT_TARG)
970 bcb->Lun |= srb->device->id << 4;
971 bcb->Length = srb->cmd_len;
972
973 /* copy the command payload */
974 memset(bcb->CDB, 0, sizeof(bcb->CDB));
975 memcpy(bcb->CDB, srb->cmnd, bcb->Length);
976
977 /* send it to out endpoint */
978 US_DEBUGP("Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n",
979 le32_to_cpu(bcb->Signature), bcb->Tag,
980 le32_to_cpu(bcb->DataTransferLength), bcb->Flags,
981 (bcb->Lun >> 4), (bcb->Lun & 0x0F),
982 bcb->Length);
983 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
984 bcb, cbwlen, NULL);
985 US_DEBUGP("Bulk command transfer result=%d\n", result);
986 if (result != USB_STOR_XFER_GOOD)
987 return USB_STOR_TRANSPORT_ERROR;
988
989 /* DATA STAGE */
990 /* send/receive data payload, if there is any */
991
992 /* Some USB-IDE converter chips need a 100us delay between the
993 * command phase and the data phase. Some devices need a little
994 * more than that, probably because of clock rate inaccuracies. */
995 if (unlikely(us->flags & US_FL_GO_SLOW))
e4334fa4 996 udelay(125);
1da177e4
LT
997
998 if (transfer_length) {
999 unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
1000 us->recv_bulk_pipe : us->send_bulk_pipe;
1001 result = usb_stor_bulk_transfer_sg(us, pipe,
1002 srb->request_buffer, transfer_length,
1003 srb->use_sg, &srb->resid);
1004 US_DEBUGP("Bulk data transfer result 0x%x\n", result);
1005 if (result == USB_STOR_XFER_ERROR)
1006 return USB_STOR_TRANSPORT_ERROR;
1007
1008 /* If the device tried to send back more data than the
1009 * amount requested, the spec requires us to transfer
1010 * the CSW anyway. Since there's no point retrying the
1011 * the command, we'll return fake sense data indicating
1012 * Illegal Request, Invalid Field in CDB.
1013 */
1014 if (result == USB_STOR_XFER_LONG)
1015 fake_sense = 1;
1016 }
1017
1018 /* See flow chart on pg 15 of the Bulk Only Transport spec for
1019 * an explanation of how this code works.
1020 */
1021
1022 /* get CSW for device status */
1023 US_DEBUGP("Attempting to get CSW...\n");
1024 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1025 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
1026
1027 /* Some broken devices add unnecessary zero-length packets to the
1028 * end of their data transfers. Such packets show up as 0-length
1029 * CSWs. If we encounter such a thing, try to read the CSW again.
1030 */
1031 if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
1032 US_DEBUGP("Received 0-length CSW; retrying...\n");
1033 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1034 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
1035 }
1036
1037 /* did the attempt to read the CSW fail? */
1038 if (result == USB_STOR_XFER_STALLED) {
1039
1040 /* get the status again */
1041 US_DEBUGP("Attempting to get CSW (2nd try)...\n");
1042 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1043 bcs, US_BULK_CS_WRAP_LEN, NULL);
1044 }
1045
1046 /* if we still have a failure at this point, we're in trouble */
1047 US_DEBUGP("Bulk status result = %d\n", result);
1048 if (result != USB_STOR_XFER_GOOD)
1049 return USB_STOR_TRANSPORT_ERROR;
1050
1051 /* check bulk status */
1052 residue = le32_to_cpu(bcs->Residue);
1053 US_DEBUGP("Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
1054 le32_to_cpu(bcs->Signature), bcs->Tag,
1055 residue, bcs->Status);
0f64e078 1056 if (bcs->Tag != us->tag || bcs->Status > US_BULK_STAT_PHASE) {
1da177e4
LT
1057 US_DEBUGP("Bulk logical error\n");
1058 return USB_STOR_TRANSPORT_ERROR;
1059 }
1060
1061 /* Some broken devices report odd signatures, so we do not check them
1062 * for validity against the spec. We store the first one we see,
1063 * and check subsequent transfers for validity against this signature.
1064 */
1065 if (!us->bcs_signature) {
1066 us->bcs_signature = bcs->Signature;
1067 if (us->bcs_signature != cpu_to_le32(US_BULK_CS_SIGN))
1068 US_DEBUGP("Learnt BCS signature 0x%08X\n",
1069 le32_to_cpu(us->bcs_signature));
1070 } else if (bcs->Signature != us->bcs_signature) {
1071 US_DEBUGP("Signature mismatch: got %08X, expecting %08X\n",
1072 le32_to_cpu(bcs->Signature),
1073 le32_to_cpu(us->bcs_signature));
1074 return USB_STOR_TRANSPORT_ERROR;
1075 }
1076
1077 /* try to compute the actual residue, based on how much data
1078 * was really transferred and what the device tells us */
1079 if (residue) {
1080 if (!(us->flags & US_FL_IGNORE_RESIDUE)) {
1081 residue = min(residue, transfer_length);
1082 srb->resid = max(srb->resid, (int) residue);
1083 }
1084 }
1085
1086 /* based on the status code, we report good or bad */
1087 switch (bcs->Status) {
1088 case US_BULK_STAT_OK:
1089 /* device babbled -- return fake sense data */
1090 if (fake_sense) {
1091 memcpy(srb->sense_buffer,
1092 usb_stor_sense_invalidCDB,
1093 sizeof(usb_stor_sense_invalidCDB));
1094 return USB_STOR_TRANSPORT_NO_SENSE;
1095 }
1096
1097 /* command good -- note that data could be short */
1098 return USB_STOR_TRANSPORT_GOOD;
1099
1100 case US_BULK_STAT_FAIL:
1101 /* command failed */
1102 return USB_STOR_TRANSPORT_FAILED;
1103
1104 case US_BULK_STAT_PHASE:
1105 /* phase error -- note that a transport reset will be
1106 * invoked by the invoke_transport() function
1107 */
1108 return USB_STOR_TRANSPORT_ERROR;
1109 }
1110
1111 /* we should never get here, but if we do, we're in trouble */
1112 return USB_STOR_TRANSPORT_ERROR;
1113}
1114
1115/***********************************************************************
1116 * Reset routines
1117 ***********************************************************************/
1118
1119/* This is the common part of the device reset code.
1120 *
1121 * It's handy that every transport mechanism uses the control endpoint for
1122 * resets.
1123 *
5203ad44 1124 * Basically, we send a reset with a 5-second timeout, so we don't get
1da177e4
LT
1125 * jammed attempting to do the reset.
1126 */
1127static int usb_stor_reset_common(struct us_data *us,
1128 u8 request, u8 requesttype,
1129 u16 value, u16 index, void *data, u16 size)
1130{
1131 int result;
1132 int result2;
1da177e4 1133
4d07ef76
MD
1134 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
1135 US_DEBUGP("No reset during disconnect\n");
1136 return -EIO;
1137 }
1da177e4 1138
1da177e4
LT
1139 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
1140 request, requesttype, value, index, data, size,
5203ad44 1141 5*HZ);
1da177e4
LT
1142 if (result < 0) {
1143 US_DEBUGP("Soft reset failed: %d\n", result);
4d07ef76 1144 return result;
1da177e4
LT
1145 }
1146
1147 /* Give the device some time to recover from the reset,
1148 * but don't delay disconnect processing. */
1149 wait_event_interruptible_timeout(us->delay_wait,
1150 test_bit(US_FLIDX_DISCONNECTING, &us->flags),
1151 HZ*6);
1152 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
1153 US_DEBUGP("Reset interrupted by disconnect\n");
4d07ef76 1154 return -EIO;
1da177e4
LT
1155 }
1156
1157 US_DEBUGP("Soft reset: clearing bulk-in endpoint halt\n");
1158 result = usb_stor_clear_halt(us, us->recv_bulk_pipe);
1159
1160 US_DEBUGP("Soft reset: clearing bulk-out endpoint halt\n");
1161 result2 = usb_stor_clear_halt(us, us->send_bulk_pipe);
1162
5203ad44
MD
1163 /* return a result code based on the result of the clear-halts */
1164 if (result >= 0)
1165 result = result2;
4d07ef76 1166 if (result < 0)
1da177e4 1167 US_DEBUGP("Soft reset failed\n");
4d07ef76
MD
1168 else
1169 US_DEBUGP("Soft reset done\n");
1170 return result;
1da177e4
LT
1171}
1172
1173/* This issues a CB[I] Reset to the device in question
1174 */
1175#define CB_RESET_CMD_SIZE 12
1176
1177int usb_stor_CB_reset(struct us_data *us)
1178{
1179 US_DEBUGP("%s called\n", __FUNCTION__);
1180
1181 memset(us->iobuf, 0xFF, CB_RESET_CMD_SIZE);
1182 us->iobuf[0] = SEND_DIAGNOSTIC;
1183 us->iobuf[1] = 4;
1184 return usb_stor_reset_common(us, US_CBI_ADSC,
1185 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1186 0, us->ifnum, us->iobuf, CB_RESET_CMD_SIZE);
1187}
1188
1189/* This issues a Bulk-only Reset to the device in question, including
1190 * clearing the subsequent endpoint halts that may occur.
1191 */
1192int usb_stor_Bulk_reset(struct us_data *us)
1193{
1194 US_DEBUGP("%s called\n", __FUNCTION__);
1195
1196 return usb_stor_reset_common(us, US_BULK_RESET_REQUEST,
1197 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1198 0, us->ifnum, NULL, 0);
1199}
4d07ef76 1200
47104b0d
AS
1201/* Issue a USB port reset to the device. The caller must not hold
1202 * us->dev_mutex.
1203 */
4d07ef76
MD
1204int usb_stor_port_reset(struct us_data *us)
1205{
47104b0d 1206 int result, rc_lock;
4d07ef76 1207
47104b0d
AS
1208 result = rc_lock =
1209 usb_lock_device_for_reset(us->pusb_dev, us->pusb_intf);
1210 if (result < 0)
1211 US_DEBUGP("unable to lock device for reset: %d\n", result);
1212 else {
1213 /* Were we disconnected while waiting for the lock? */
1214 if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
1215 result = -EIO;
1216 US_DEBUGP("No reset during disconnect\n");
4d07ef76 1217 } else {
47104b0d
AS
1218 result = usb_reset_composite_device(
1219 us->pusb_dev, us->pusb_intf);
1220 US_DEBUGP("usb_reset_composite_device returns %d\n",
1221 result);
4d07ef76 1222 }
47104b0d
AS
1223 if (rc_lock)
1224 usb_unlock_device(us->pusb_dev);
4d07ef76
MD
1225 }
1226 return result;
1227}