xref: /linux/drivers/usb/storage/transport.c (revision 2fc2991175bf77395e6b15fe6b2304d3bf72da40)
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 
48 #include <linux/config.h>
49 #include <linux/sched.h>
50 #include <linux/errno.h>
51 #include <linux/slab.h>
52 
53 #include <scsi/scsi.h>
54 #include <scsi/scsi_cmnd.h>
55 #include <scsi/scsi_device.h>
56 
57 #include "usb.h"
58 #include "transport.h"
59 #include "protocol.h"
60 #include "scsiglue.h"
61 #include "debug.h"
62 
63 
64 /***********************************************************************
65  * Data transfer routines
66  ***********************************************************************/
67 
68 /*
69  * This is subtle, so pay attention:
70  * ---------------------------------
71  * We're very concerned about races with a command abort.  Hanging this code
72  * is a sure fire way to hang the kernel.  (Note that this discussion applies
73  * only to transactions resulting from a scsi queued-command, since only
74  * these transactions are subject to a scsi abort.  Other transactions, such
75  * as those occurring during device-specific initialization, must be handled
76  * by a separate code path.)
77  *
78  * The abort function (usb_storage_command_abort() in scsiglue.c) first
79  * sets the machine state and the ABORTING bit in us->flags to prevent
80  * new URBs from being submitted.  It then calls usb_stor_stop_transport()
81  * below, which atomically tests-and-clears the URB_ACTIVE bit in us->flags
82  * to see if the current_urb needs to be stopped.  Likewise, the SG_ACTIVE
83  * bit is tested to see if the current_sg scatter-gather request needs to be
84  * stopped.  The timeout callback routine does much the same thing.
85  *
86  * When a disconnect occurs, the DISCONNECTING bit in us->flags is set to
87  * prevent new URBs from being submitted, and usb_stor_stop_transport() is
88  * called to stop any ongoing requests.
89  *
90  * The submit function first verifies that the submitting is allowed
91  * (neither ABORTING nor DISCONNECTING bits are set) and that the submit
92  * completes without errors, and only then sets the URB_ACTIVE bit.  This
93  * prevents the stop_transport() function from trying to cancel the URB
94  * while the submit call is underway.  Next, the submit function must test
95  * the flags to see if an abort or disconnect occurred during the submission
96  * or before the URB_ACTIVE bit was set.  If so, it's essential to cancel
97  * the URB if it hasn't been cancelled already (i.e., if the URB_ACTIVE bit
98  * is still set).  Either way, the function must then wait for the URB to
99  * finish.  Note that the URB can still be in progress even after a call to
100  * usb_unlink_urb() returns.
101  *
102  * The idea is that (1) once the ABORTING or DISCONNECTING bit is set,
103  * either the stop_transport() function or the submitting function
104  * is guaranteed to call usb_unlink_urb() for an active URB,
105  * and (2) test_and_clear_bit() prevents usb_unlink_urb() from being
106  * called more than once or from being called during usb_submit_urb().
107  */
108 
109 /* This is the completion handler which will wake us up when an URB
110  * completes.
111  */
112 static void usb_stor_blocking_completion(struct urb *urb, struct pt_regs *regs)
113 {
114 	struct completion *urb_done_ptr = (struct completion *)urb->context;
115 
116 	complete(urb_done_ptr);
117 }
118 
119 /* This is the timeout handler which will cancel an URB when its timeout
120  * expires.
121  */
122 static void timeout_handler(unsigned long us_)
123 {
124 	struct us_data *us = (struct us_data *) us_;
125 
126 	if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->flags)) {
127 		US_DEBUGP("Timeout -- cancelling URB\n");
128 		usb_unlink_urb(us->current_urb);
129 	}
130 }
131 
132 /* This is the common part of the URB message submission code
133  *
134  * All URBs from the usb-storage driver involved in handling a queued scsi
135  * command _must_ pass through this function (or something like it) for the
136  * abort mechanisms to work properly.
137  */
138 static int usb_stor_msg_common(struct us_data *us, int timeout)
139 {
140 	struct completion urb_done;
141 	struct timer_list to_timer;
142 	int status;
143 
144 	/* don't submit URBs during abort/disconnect processing */
145 	if (us->flags & ABORTING_OR_DISCONNECTING)
146 		return -EIO;
147 
148 	/* set up data structures for the wakeup system */
149 	init_completion(&urb_done);
150 
151 	/* fill the common fields in the URB */
152 	us->current_urb->context = &urb_done;
153 	us->current_urb->actual_length = 0;
154 	us->current_urb->error_count = 0;
155 	us->current_urb->status = 0;
156 
157 	/* we assume that if transfer_buffer isn't us->iobuf then it
158 	 * hasn't been mapped for DMA.  Yes, this is clunky, but it's
159 	 * easier than always having the caller tell us whether the
160 	 * transfer buffer has already been mapped. */
161 	us->current_urb->transfer_flags = URB_NO_SETUP_DMA_MAP;
162 	if (us->current_urb->transfer_buffer == us->iobuf)
163 		us->current_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
164 	us->current_urb->transfer_dma = us->iobuf_dma;
165 	us->current_urb->setup_dma = us->cr_dma;
166 
167 	/* submit the URB */
168 	status = usb_submit_urb(us->current_urb, GFP_NOIO);
169 	if (status) {
170 		/* something went wrong */
171 		return status;
172 	}
173 
174 	/* since the URB has been submitted successfully, it's now okay
175 	 * to cancel it */
176 	set_bit(US_FLIDX_URB_ACTIVE, &us->flags);
177 
178 	/* did an abort/disconnect occur during the submission? */
179 	if (us->flags & ABORTING_OR_DISCONNECTING) {
180 
181 		/* cancel the URB, if it hasn't been cancelled already */
182 		if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->flags)) {
183 			US_DEBUGP("-- cancelling URB\n");
184 			usb_unlink_urb(us->current_urb);
185 		}
186 	}
187 
188 	/* submit the timeout timer, if a timeout was requested */
189 	if (timeout > 0) {
190 		init_timer(&to_timer);
191 		to_timer.expires = jiffies + timeout;
192 		to_timer.function = timeout_handler;
193 		to_timer.data = (unsigned long) us;
194 		add_timer(&to_timer);
195 	}
196 
197 	/* wait for the completion of the URB */
198 	wait_for_completion(&urb_done);
199 	clear_bit(US_FLIDX_URB_ACTIVE, &us->flags);
200 
201 	/* clean up the timeout timer */
202 	if (timeout > 0)
203 		del_timer_sync(&to_timer);
204 
205 	/* return the URB status */
206 	return us->current_urb->status;
207 }
208 
209 /*
210  * Transfer one control message, with timeouts, and allowing early
211  * termination.  Return codes are usual -Exxx, *not* USB_STOR_XFER_xxx.
212  */
213 int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
214 		 u8 request, u8 requesttype, u16 value, u16 index,
215 		 void *data, u16 size, int timeout)
216 {
217 	int status;
218 
219 	US_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
220 			__FUNCTION__, request, requesttype,
221 			value, index, size);
222 
223 	/* fill in the devrequest structure */
224 	us->cr->bRequestType = requesttype;
225 	us->cr->bRequest = request;
226 	us->cr->wValue = cpu_to_le16(value);
227 	us->cr->wIndex = cpu_to_le16(index);
228 	us->cr->wLength = cpu_to_le16(size);
229 
230 	/* fill and submit the URB */
231 	usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
232 			 (unsigned char*) us->cr, data, size,
233 			 usb_stor_blocking_completion, NULL);
234 	status = usb_stor_msg_common(us, timeout);
235 
236 	/* return the actual length of the data transferred if no error */
237 	if (status == 0)
238 		status = us->current_urb->actual_length;
239 	return status;
240 }
241 
242 /* This is a version of usb_clear_halt() that allows early termination and
243  * doesn't read the status from the device -- this is because some devices
244  * crash their internal firmware when the status is requested after a halt.
245  *
246  * A definitive list of these 'bad' devices is too difficult to maintain or
247  * make complete enough to be useful.  This problem was first observed on the
248  * Hagiwara FlashGate DUAL unit.  However, bus traces reveal that neither
249  * MacOS nor Windows checks the status after clearing a halt.
250  *
251  * Since many vendors in this space limit their testing to interoperability
252  * with these two OSes, specification violations like this one are common.
253  */
254 int usb_stor_clear_halt(struct us_data *us, unsigned int pipe)
255 {
256 	int result;
257 	int endp = usb_pipeendpoint(pipe);
258 
259 	if (usb_pipein (pipe))
260 		endp |= USB_DIR_IN;
261 
262 	result = usb_stor_control_msg(us, us->send_ctrl_pipe,
263 		USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
264 		USB_ENDPOINT_HALT, endp,
265 		NULL, 0, 3*HZ);
266 
267 	/* reset the endpoint toggle */
268 	if (result >= 0)
269 		usb_settoggle(us->pusb_dev, usb_pipeendpoint(pipe),
270 				usb_pipeout(pipe), 0);
271 
272 	US_DEBUGP("%s: result = %d\n", __FUNCTION__, result);
273 	return result;
274 }
275 
276 
277 /*
278  * Interpret the results of a URB transfer
279  *
280  * This function prints appropriate debugging messages, clears halts on
281  * non-control endpoints, and translates the status to the corresponding
282  * USB_STOR_XFER_xxx return code.
283  */
284 static int interpret_urb_result(struct us_data *us, unsigned int pipe,
285 		unsigned int length, int result, unsigned int partial)
286 {
287 	US_DEBUGP("Status code %d; transferred %u/%u\n",
288 			result, partial, length);
289 	switch (result) {
290 
291 	/* no error code; did we send all the data? */
292 	case 0:
293 		if (partial != length) {
294 			US_DEBUGP("-- short transfer\n");
295 			return USB_STOR_XFER_SHORT;
296 		}
297 
298 		US_DEBUGP("-- transfer complete\n");
299 		return USB_STOR_XFER_GOOD;
300 
301 	/* stalled */
302 	case -EPIPE:
303 		/* for control endpoints, (used by CB[I]) a stall indicates
304 		 * a failed command */
305 		if (usb_pipecontrol(pipe)) {
306 			US_DEBUGP("-- stall on control pipe\n");
307 			return USB_STOR_XFER_STALLED;
308 		}
309 
310 		/* for other sorts of endpoint, clear the stall */
311 		US_DEBUGP("clearing endpoint halt for pipe 0x%x\n", pipe);
312 		if (usb_stor_clear_halt(us, pipe) < 0)
313 			return USB_STOR_XFER_ERROR;
314 		return USB_STOR_XFER_STALLED;
315 
316 	/* timeout or excessively long NAK */
317 	case -ETIMEDOUT:
318 		US_DEBUGP("-- timeout or NAK\n");
319 		return USB_STOR_XFER_ERROR;
320 
321 	/* babble - the device tried to send more than we wanted to read */
322 	case -EOVERFLOW:
323 		US_DEBUGP("-- babble\n");
324 		return USB_STOR_XFER_LONG;
325 
326 	/* the transfer was cancelled by abort, disconnect, or timeout */
327 	case -ECONNRESET:
328 		US_DEBUGP("-- transfer cancelled\n");
329 		return USB_STOR_XFER_ERROR;
330 
331 	/* short scatter-gather read transfer */
332 	case -EREMOTEIO:
333 		US_DEBUGP("-- short read transfer\n");
334 		return USB_STOR_XFER_SHORT;
335 
336 	/* abort or disconnect in progress */
337 	case -EIO:
338 		US_DEBUGP("-- abort or disconnect in progress\n");
339 		return USB_STOR_XFER_ERROR;
340 
341 	/* the catch-all error case */
342 	default:
343 		US_DEBUGP("-- unknown error\n");
344 		return USB_STOR_XFER_ERROR;
345 	}
346 }
347 
348 /*
349  * Transfer one control message, without timeouts, but allowing early
350  * termination.  Return codes are USB_STOR_XFER_xxx.
351  */
352 int usb_stor_ctrl_transfer(struct us_data *us, unsigned int pipe,
353 		u8 request, u8 requesttype, u16 value, u16 index,
354 		void *data, u16 size)
355 {
356 	int result;
357 
358 	US_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
359 			__FUNCTION__, request, requesttype,
360 			value, index, size);
361 
362 	/* fill in the devrequest structure */
363 	us->cr->bRequestType = requesttype;
364 	us->cr->bRequest = request;
365 	us->cr->wValue = cpu_to_le16(value);
366 	us->cr->wIndex = cpu_to_le16(index);
367 	us->cr->wLength = cpu_to_le16(size);
368 
369 	/* fill and submit the URB */
370 	usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
371 			 (unsigned char*) us->cr, data, size,
372 			 usb_stor_blocking_completion, NULL);
373 	result = usb_stor_msg_common(us, 0);
374 
375 	return interpret_urb_result(us, pipe, size, result,
376 			us->current_urb->actual_length);
377 }
378 
379 /*
380  * Receive one interrupt buffer, without timeouts, but allowing early
381  * termination.  Return codes are USB_STOR_XFER_xxx.
382  *
383  * This routine always uses us->recv_intr_pipe as the pipe and
384  * us->ep_bInterval as the interrupt interval.
385  */
386 static int usb_stor_intr_transfer(struct us_data *us, void *buf,
387 				  unsigned int length)
388 {
389 	int result;
390 	unsigned int pipe = us->recv_intr_pipe;
391 	unsigned int maxp;
392 
393 	US_DEBUGP("%s: xfer %u bytes\n", __FUNCTION__, length);
394 
395 	/* calculate the max packet size */
396 	maxp = usb_maxpacket(us->pusb_dev, pipe, usb_pipeout(pipe));
397 	if (maxp > length)
398 		maxp = length;
399 
400 	/* fill and submit the URB */
401 	usb_fill_int_urb(us->current_urb, us->pusb_dev, pipe, buf,
402 			maxp, usb_stor_blocking_completion, NULL,
403 			us->ep_bInterval);
404 	result = usb_stor_msg_common(us, 0);
405 
406 	return interpret_urb_result(us, pipe, length, result,
407 			us->current_urb->actual_length);
408 }
409 
410 /*
411  * Transfer one buffer via bulk pipe, without timeouts, but allowing early
412  * termination.  Return codes are USB_STOR_XFER_xxx.  If the bulk pipe
413  * stalls during the transfer, the halt is automatically cleared.
414  */
415 int usb_stor_bulk_transfer_buf(struct us_data *us, unsigned int pipe,
416 	void *buf, unsigned int length, unsigned int *act_len)
417 {
418 	int result;
419 
420 	US_DEBUGP("%s: xfer %u bytes\n", __FUNCTION__, length);
421 
422 	/* fill and submit the URB */
423 	usb_fill_bulk_urb(us->current_urb, us->pusb_dev, pipe, buf, length,
424 		      usb_stor_blocking_completion, NULL);
425 	result = usb_stor_msg_common(us, 0);
426 
427 	/* store the actual length of the data transferred */
428 	if (act_len)
429 		*act_len = us->current_urb->actual_length;
430 	return interpret_urb_result(us, pipe, length, result,
431 			us->current_urb->actual_length);
432 }
433 
434 /*
435  * Transfer a scatter-gather list via bulk transfer
436  *
437  * This function does basically the same thing as usb_stor_bulk_transfer_buf()
438  * above, but it uses the usbcore scatter-gather library.
439  */
440 static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
441 		struct scatterlist *sg, int num_sg, unsigned int length,
442 		unsigned int *act_len)
443 {
444 	int result;
445 
446 	/* don't submit s-g requests during abort/disconnect processing */
447 	if (us->flags & ABORTING_OR_DISCONNECTING)
448 		return USB_STOR_XFER_ERROR;
449 
450 	/* initialize the scatter-gather request block */
451 	US_DEBUGP("%s: xfer %u bytes, %d entries\n", __FUNCTION__,
452 			length, num_sg);
453 	result = usb_sg_init(&us->current_sg, us->pusb_dev, pipe, 0,
454 			sg, num_sg, length, SLAB_NOIO);
455 	if (result) {
456 		US_DEBUGP("usb_sg_init returned %d\n", result);
457 		return USB_STOR_XFER_ERROR;
458 	}
459 
460 	/* since the block has been initialized successfully, it's now
461 	 * okay to cancel it */
462 	set_bit(US_FLIDX_SG_ACTIVE, &us->flags);
463 
464 	/* did an abort/disconnect occur during the submission? */
465 	if (us->flags & ABORTING_OR_DISCONNECTING) {
466 
467 		/* cancel the request, if it hasn't been cancelled already */
468 		if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->flags)) {
469 			US_DEBUGP("-- cancelling sg request\n");
470 			usb_sg_cancel(&us->current_sg);
471 		}
472 	}
473 
474 	/* wait for the completion of the transfer */
475 	usb_sg_wait(&us->current_sg);
476 	clear_bit(US_FLIDX_SG_ACTIVE, &us->flags);
477 
478 	result = us->current_sg.status;
479 	if (act_len)
480 		*act_len = us->current_sg.bytes;
481 	return interpret_urb_result(us, pipe, length, result,
482 			us->current_sg.bytes);
483 }
484 
485 /*
486  * Transfer an entire SCSI command's worth of data payload over the bulk
487  * pipe.
488  *
489  * Note that this uses usb_stor_bulk_transfer_buf() and
490  * usb_stor_bulk_transfer_sglist() to achieve its goals --
491  * this function simply determines whether we're going to use
492  * scatter-gather or not, and acts appropriately.
493  */
494 int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe,
495 		void *buf, unsigned int length_left, int use_sg, int *residual)
496 {
497 	int result;
498 	unsigned int partial;
499 
500 	/* are we scatter-gathering? */
501 	if (use_sg) {
502 		/* use the usb core scatter-gather primitives */
503 		result = usb_stor_bulk_transfer_sglist(us, pipe,
504 				(struct scatterlist *) buf, use_sg,
505 				length_left, &partial);
506 		length_left -= partial;
507 	} else {
508 		/* no scatter-gather, just make the request */
509 		result = usb_stor_bulk_transfer_buf(us, pipe, buf,
510 				length_left, &partial);
511 		length_left -= partial;
512 	}
513 
514 	/* store the residual and return the error code */
515 	if (residual)
516 		*residual = length_left;
517 	return result;
518 }
519 
520 /***********************************************************************
521  * Transport routines
522  ***********************************************************************/
523 
524 /* Invoke the transport and basic error-handling/recovery methods
525  *
526  * This is used by the protocol layers to actually send the message to
527  * the device and receive the response.
528  */
529 void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
530 {
531 	int need_auto_sense;
532 	int result;
533 
534 	/* send the command to the transport layer */
535 	srb->resid = 0;
536 	result = us->transport(srb, us);
537 
538 	/* if the command gets aborted by the higher layers, we need to
539 	 * short-circuit all other processing
540 	 */
541 	if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
542 		US_DEBUGP("-- command was aborted\n");
543 		srb->result = DID_ABORT << 16;
544 		goto Handle_Errors;
545 	}
546 
547 	/* if there is a transport error, reset and don't auto-sense */
548 	if (result == USB_STOR_TRANSPORT_ERROR) {
549 		US_DEBUGP("-- transport indicates error, resetting\n");
550 		srb->result = DID_ERROR << 16;
551 		goto Handle_Errors;
552 	}
553 
554 	/* if the transport provided its own sense data, don't auto-sense */
555 	if (result == USB_STOR_TRANSPORT_NO_SENSE) {
556 		srb->result = SAM_STAT_CHECK_CONDITION;
557 		return;
558 	}
559 
560 	srb->result = SAM_STAT_GOOD;
561 
562 	/* Determine if we need to auto-sense
563 	 *
564 	 * I normally don't use a flag like this, but it's almost impossible
565 	 * to understand what's going on here if I don't.
566 	 */
567 	need_auto_sense = 0;
568 
569 	/*
570 	 * If we're running the CB transport, which is incapable
571 	 * of determining status on its own, we will auto-sense
572 	 * unless the operation involved a data-in transfer.  Devices
573 	 * can signal most data-in errors by stalling the bulk-in pipe.
574 	 */
575 	if ((us->protocol == US_PR_CB || us->protocol == US_PR_DPCM_USB) &&
576 			srb->sc_data_direction != DMA_FROM_DEVICE) {
577 		US_DEBUGP("-- CB transport device requiring auto-sense\n");
578 		need_auto_sense = 1;
579 	}
580 
581 	/*
582 	 * If we have a failure, we're going to do a REQUEST_SENSE
583 	 * automatically.  Note that we differentiate between a command
584 	 * "failure" and an "error" in the transport mechanism.
585 	 */
586 	if (result == USB_STOR_TRANSPORT_FAILED) {
587 		US_DEBUGP("-- transport indicates command failure\n");
588 		need_auto_sense = 1;
589 	}
590 
591 	/*
592 	 * A short transfer on a command where we don't expect it
593 	 * is unusual, but it doesn't mean we need to auto-sense.
594 	 */
595 	if ((srb->resid > 0) &&
596 	    !((srb->cmnd[0] == REQUEST_SENSE) ||
597 	      (srb->cmnd[0] == INQUIRY) ||
598 	      (srb->cmnd[0] == MODE_SENSE) ||
599 	      (srb->cmnd[0] == LOG_SENSE) ||
600 	      (srb->cmnd[0] == MODE_SENSE_10))) {
601 		US_DEBUGP("-- unexpectedly short transfer\n");
602 	}
603 
604 	/* Now, if we need to do the auto-sense, let's do it */
605 	if (need_auto_sense) {
606 		int temp_result;
607 		void* old_request_buffer;
608 		unsigned short old_sg;
609 		unsigned old_request_bufflen;
610 		unsigned char old_sc_data_direction;
611 		unsigned char old_cmd_len;
612 		unsigned char old_cmnd[MAX_COMMAND_SIZE];
613 		int old_resid;
614 
615 		US_DEBUGP("Issuing auto-REQUEST_SENSE\n");
616 
617 		/* save the old command */
618 		memcpy(old_cmnd, srb->cmnd, MAX_COMMAND_SIZE);
619 		old_cmd_len = srb->cmd_len;
620 
621 		/* set the command and the LUN */
622 		memset(srb->cmnd, 0, MAX_COMMAND_SIZE);
623 		srb->cmnd[0] = REQUEST_SENSE;
624 		srb->cmnd[1] = old_cmnd[1] & 0xE0;
625 		srb->cmnd[4] = 18;
626 
627 		/* FIXME: we must do the protocol translation here */
628 		if (us->subclass == US_SC_RBC || us->subclass == US_SC_SCSI)
629 			srb->cmd_len = 6;
630 		else
631 			srb->cmd_len = 12;
632 
633 		/* set the transfer direction */
634 		old_sc_data_direction = srb->sc_data_direction;
635 		srb->sc_data_direction = DMA_FROM_DEVICE;
636 
637 		/* use the new buffer we have */
638 		old_request_buffer = srb->request_buffer;
639 		srb->request_buffer = us->sensebuf;
640 
641 		/* set the buffer length for transfer */
642 		old_request_bufflen = srb->request_bufflen;
643 		srb->request_bufflen = US_SENSE_SIZE;
644 
645 		/* set up for no scatter-gather use */
646 		old_sg = srb->use_sg;
647 		srb->use_sg = 0;
648 
649 		/* issue the auto-sense command */
650 		old_resid = srb->resid;
651 		srb->resid = 0;
652 		temp_result = us->transport(us->srb, us);
653 
654 		/* let's clean up right away */
655 		memcpy(srb->sense_buffer, us->sensebuf, US_SENSE_SIZE);
656 		srb->resid = old_resid;
657 		srb->request_buffer = old_request_buffer;
658 		srb->request_bufflen = old_request_bufflen;
659 		srb->use_sg = old_sg;
660 		srb->sc_data_direction = old_sc_data_direction;
661 		srb->cmd_len = old_cmd_len;
662 		memcpy(srb->cmnd, old_cmnd, MAX_COMMAND_SIZE);
663 
664 		if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
665 			US_DEBUGP("-- auto-sense aborted\n");
666 			srb->result = DID_ABORT << 16;
667 			goto Handle_Errors;
668 		}
669 		if (temp_result != USB_STOR_TRANSPORT_GOOD) {
670 			US_DEBUGP("-- auto-sense failure\n");
671 
672 			/* we skip the reset if this happens to be a
673 			 * multi-target device, since failure of an
674 			 * auto-sense is perfectly valid
675 			 */
676 			srb->result = DID_ERROR << 16;
677 			if (!(us->flags & US_FL_SCM_MULT_TARG))
678 				goto Handle_Errors;
679 			return;
680 		}
681 
682 		US_DEBUGP("-- Result from auto-sense is %d\n", temp_result);
683 		US_DEBUGP("-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n",
684 			  srb->sense_buffer[0],
685 			  srb->sense_buffer[2] & 0xf,
686 			  srb->sense_buffer[12],
687 			  srb->sense_buffer[13]);
688 #ifdef CONFIG_USB_STORAGE_DEBUG
689 		usb_stor_show_sense(
690 			  srb->sense_buffer[2] & 0xf,
691 			  srb->sense_buffer[12],
692 			  srb->sense_buffer[13]);
693 #endif
694 
695 		/* set the result so the higher layers expect this data */
696 		srb->result = SAM_STAT_CHECK_CONDITION;
697 
698 		/* If things are really okay, then let's show that.  Zero
699 		 * out the sense buffer so the higher layers won't realize
700 		 * we did an unsolicited auto-sense. */
701 		if (result == USB_STOR_TRANSPORT_GOOD &&
702 			/* Filemark 0, ignore EOM, ILI 0, no sense */
703 				(srb->sense_buffer[2] & 0xaf) == 0 &&
704 			/* No ASC or ASCQ */
705 				srb->sense_buffer[12] == 0 &&
706 				srb->sense_buffer[13] == 0) {
707 			srb->result = SAM_STAT_GOOD;
708 			srb->sense_buffer[0] = 0x0;
709 		}
710 	}
711 
712 	/* Did we transfer less than the minimum amount required? */
713 	if (srb->result == SAM_STAT_GOOD &&
714 			srb->request_bufflen - srb->resid < srb->underflow)
715 		srb->result = (DID_ERROR << 16) | (SUGGEST_RETRY << 24);
716 
717 	return;
718 
719 	/* Error and abort processing: try to resynchronize with the device
720 	 * by issuing a port reset.  If that fails, try a class-specific
721 	 * device reset. */
722   Handle_Errors:
723 
724 	/* Let the SCSI layer know we are doing a reset, set the
725 	 * RESETTING bit, and clear the ABORTING bit so that the reset
726 	 * may proceed. */
727 	scsi_lock(us_to_host(us));
728 	usb_stor_report_bus_reset(us);
729 	set_bit(US_FLIDX_RESETTING, &us->flags);
730 	clear_bit(US_FLIDX_ABORTING, &us->flags);
731 	scsi_unlock(us_to_host(us));
732 
733 	result = usb_stor_port_reset(us);
734 	if (result < 0) {
735 		scsi_lock(us_to_host(us));
736 		usb_stor_report_device_reset(us);
737 		scsi_unlock(us_to_host(us));
738 		us->transport_reset(us);
739 	}
740 	clear_bit(US_FLIDX_RESETTING, &us->flags);
741 }
742 
743 /* Stop the current URB transfer */
744 void usb_stor_stop_transport(struct us_data *us)
745 {
746 	US_DEBUGP("%s called\n", __FUNCTION__);
747 
748 	/* If the state machine is blocked waiting for an URB,
749 	 * let's wake it up.  The test_and_clear_bit() call
750 	 * guarantees that if a URB has just been submitted,
751 	 * it won't be cancelled more than once. */
752 	if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->flags)) {
753 		US_DEBUGP("-- cancelling URB\n");
754 		usb_unlink_urb(us->current_urb);
755 	}
756 
757 	/* If we are waiting for a scatter-gather operation, cancel it. */
758 	if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->flags)) {
759 		US_DEBUGP("-- cancelling sg request\n");
760 		usb_sg_cancel(&us->current_sg);
761 	}
762 }
763 
764 /*
765  * Control/Bulk/Interrupt transport
766  */
767 
768 int usb_stor_CBI_transport(struct scsi_cmnd *srb, struct us_data *us)
769 {
770 	unsigned int transfer_length = srb->request_bufflen;
771 	unsigned int pipe = 0;
772 	int result;
773 
774 	/* COMMAND STAGE */
775 	/* let's send the command via the control pipe */
776 	result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
777 				      US_CBI_ADSC,
778 				      USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
779 				      us->ifnum, srb->cmnd, srb->cmd_len);
780 
781 	/* check the return code for the command */
782 	US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result);
783 
784 	/* if we stalled the command, it means command failed */
785 	if (result == USB_STOR_XFER_STALLED) {
786 		return USB_STOR_TRANSPORT_FAILED;
787 	}
788 
789 	/* Uh oh... serious problem here */
790 	if (result != USB_STOR_XFER_GOOD) {
791 		return USB_STOR_TRANSPORT_ERROR;
792 	}
793 
794 	/* DATA STAGE */
795 	/* transfer the data payload for this command, if one exists*/
796 	if (transfer_length) {
797 		pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
798 				us->recv_bulk_pipe : us->send_bulk_pipe;
799 		result = usb_stor_bulk_transfer_sg(us, pipe,
800 					srb->request_buffer, transfer_length,
801 					srb->use_sg, &srb->resid);
802 		US_DEBUGP("CBI data stage result is 0x%x\n", result);
803 
804 		/* if we stalled the data transfer it means command failed */
805 		if (result == USB_STOR_XFER_STALLED)
806 			return USB_STOR_TRANSPORT_FAILED;
807 		if (result > USB_STOR_XFER_STALLED)
808 			return USB_STOR_TRANSPORT_ERROR;
809 	}
810 
811 	/* STATUS STAGE */
812 	result = usb_stor_intr_transfer(us, us->iobuf, 2);
813 	US_DEBUGP("Got interrupt data (0x%x, 0x%x)\n",
814 			us->iobuf[0], us->iobuf[1]);
815 	if (result != USB_STOR_XFER_GOOD)
816 		return USB_STOR_TRANSPORT_ERROR;
817 
818 	/* UFI gives us ASC and ASCQ, like a request sense
819 	 *
820 	 * REQUEST_SENSE and INQUIRY don't affect the sense data on UFI
821 	 * devices, so we ignore the information for those commands.  Note
822 	 * that this means we could be ignoring a real error on these
823 	 * commands, but that can't be helped.
824 	 */
825 	if (us->subclass == US_SC_UFI) {
826 		if (srb->cmnd[0] == REQUEST_SENSE ||
827 		    srb->cmnd[0] == INQUIRY)
828 			return USB_STOR_TRANSPORT_GOOD;
829 		if (us->iobuf[0])
830 			goto Failed;
831 		return USB_STOR_TRANSPORT_GOOD;
832 	}
833 
834 	/* If not UFI, we interpret the data as a result code
835 	 * The first byte should always be a 0x0.
836 	 *
837 	 * Some bogus devices don't follow that rule.  They stuff the ASC
838 	 * into the first byte -- so if it's non-zero, call it a failure.
839 	 */
840 	if (us->iobuf[0]) {
841 		US_DEBUGP("CBI IRQ data showed reserved bType 0x%x\n",
842 				us->iobuf[0]);
843 		goto Failed;
844 
845 	}
846 
847 	/* The second byte & 0x0F should be 0x0 for good, otherwise error */
848 	switch (us->iobuf[1] & 0x0F) {
849 		case 0x00:
850 			return USB_STOR_TRANSPORT_GOOD;
851 		case 0x01:
852 			goto Failed;
853 	}
854 	return USB_STOR_TRANSPORT_ERROR;
855 
856 	/* the CBI spec requires that the bulk pipe must be cleared
857 	 * following any data-in/out command failure (section 2.4.3.1.3)
858 	 */
859   Failed:
860 	if (pipe)
861 		usb_stor_clear_halt(us, pipe);
862 	return USB_STOR_TRANSPORT_FAILED;
863 }
864 
865 /*
866  * Control/Bulk transport
867  */
868 int usb_stor_CB_transport(struct scsi_cmnd *srb, struct us_data *us)
869 {
870 	unsigned int transfer_length = srb->request_bufflen;
871 	int result;
872 
873 	/* COMMAND STAGE */
874 	/* let's send the command via the control pipe */
875 	result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
876 				      US_CBI_ADSC,
877 				      USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
878 				      us->ifnum, srb->cmnd, srb->cmd_len);
879 
880 	/* check the return code for the command */
881 	US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result);
882 
883 	/* if we stalled the command, it means command failed */
884 	if (result == USB_STOR_XFER_STALLED) {
885 		return USB_STOR_TRANSPORT_FAILED;
886 	}
887 
888 	/* Uh oh... serious problem here */
889 	if (result != USB_STOR_XFER_GOOD) {
890 		return USB_STOR_TRANSPORT_ERROR;
891 	}
892 
893 	/* DATA STAGE */
894 	/* transfer the data payload for this command, if one exists*/
895 	if (transfer_length) {
896 		unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
897 				us->recv_bulk_pipe : us->send_bulk_pipe;
898 		result = usb_stor_bulk_transfer_sg(us, pipe,
899 					srb->request_buffer, transfer_length,
900 					srb->use_sg, &srb->resid);
901 		US_DEBUGP("CB data stage result is 0x%x\n", result);
902 
903 		/* if we stalled the data transfer it means command failed */
904 		if (result == USB_STOR_XFER_STALLED)
905 			return USB_STOR_TRANSPORT_FAILED;
906 		if (result > USB_STOR_XFER_STALLED)
907 			return USB_STOR_TRANSPORT_ERROR;
908 	}
909 
910 	/* STATUS STAGE */
911 	/* NOTE: CB does not have a status stage.  Silly, I know.  So
912 	 * we have to catch this at a higher level.
913 	 */
914 	return USB_STOR_TRANSPORT_GOOD;
915 }
916 
917 /*
918  * Bulk only transport
919  */
920 
921 /* Determine what the maximum LUN supported is */
922 int usb_stor_Bulk_max_lun(struct us_data *us)
923 {
924 	int result;
925 
926 	/* issue the command */
927 	us->iobuf[0] = 0;
928 	result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
929 				 US_BULK_GET_MAX_LUN,
930 				 USB_DIR_IN | USB_TYPE_CLASS |
931 				 USB_RECIP_INTERFACE,
932 				 0, us->ifnum, us->iobuf, 1, HZ);
933 
934 	US_DEBUGP("GetMaxLUN command result is %d, data is %d\n",
935 		  result, us->iobuf[0]);
936 
937 	/* if we have a successful request, return the result */
938 	if (result > 0)
939 		return us->iobuf[0];
940 
941 	/*
942 	 * Some devices (i.e. Iomega Zip100) need this -- apparently
943 	 * the bulk pipes get STALLed when the GetMaxLUN request is
944 	 * processed.   This is, in theory, harmless to all other devices
945 	 * (regardless of if they stall or not).
946 	 */
947 	if (result == -EPIPE) {
948 		usb_stor_clear_halt(us, us->recv_bulk_pipe);
949 		usb_stor_clear_halt(us, us->send_bulk_pipe);
950 	}
951 
952 	/*
953 	 * Some devices don't like GetMaxLUN.  They may STALL the control
954 	 * pipe, they may return a zero-length result, they may do nothing at
955 	 * all and timeout, or they may fail in even more bizarrely creative
956 	 * ways.  In these cases the best approach is to use the default
957 	 * value: only one LUN.
958 	 */
959 	return 0;
960 }
961 
962 int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
963 {
964 	struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
965 	struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
966 	unsigned int transfer_length = srb->request_bufflen;
967 	unsigned int residue;
968 	int result;
969 	int fake_sense = 0;
970 	unsigned int cswlen;
971 	unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
972 
973 	/* Take care of BULK32 devices; set extra byte to 0 */
974 	if ( unlikely(us->flags & US_FL_BULK32)) {
975 		cbwlen = 32;
976 		us->iobuf[31] = 0;
977 	}
978 
979 	/* set up the command wrapper */
980 	bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
981 	bcb->DataTransferLength = cpu_to_le32(transfer_length);
982 	bcb->Flags = srb->sc_data_direction == DMA_FROM_DEVICE ? 1 << 7 : 0;
983 	bcb->Tag = ++us->tag;
984 	bcb->Lun = srb->device->lun;
985 	if (us->flags & US_FL_SCM_MULT_TARG)
986 		bcb->Lun |= srb->device->id << 4;
987 	bcb->Length = srb->cmd_len;
988 
989 	/* copy the command payload */
990 	memset(bcb->CDB, 0, sizeof(bcb->CDB));
991 	memcpy(bcb->CDB, srb->cmnd, bcb->Length);
992 
993 	/* send it to out endpoint */
994 	US_DEBUGP("Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n",
995 			le32_to_cpu(bcb->Signature), bcb->Tag,
996 			le32_to_cpu(bcb->DataTransferLength), bcb->Flags,
997 			(bcb->Lun >> 4), (bcb->Lun & 0x0F),
998 			bcb->Length);
999 	result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
1000 				bcb, cbwlen, NULL);
1001 	US_DEBUGP("Bulk command transfer result=%d\n", result);
1002 	if (result != USB_STOR_XFER_GOOD)
1003 		return USB_STOR_TRANSPORT_ERROR;
1004 
1005 	/* DATA STAGE */
1006 	/* send/receive data payload, if there is any */
1007 
1008 	/* Some USB-IDE converter chips need a 100us delay between the
1009 	 * command phase and the data phase.  Some devices need a little
1010 	 * more than that, probably because of clock rate inaccuracies. */
1011 	if (unlikely(us->flags & US_FL_GO_SLOW))
1012 		udelay(125);
1013 
1014 	if (transfer_length) {
1015 		unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
1016 				us->recv_bulk_pipe : us->send_bulk_pipe;
1017 		result = usb_stor_bulk_transfer_sg(us, pipe,
1018 					srb->request_buffer, transfer_length,
1019 					srb->use_sg, &srb->resid);
1020 		US_DEBUGP("Bulk data transfer result 0x%x\n", result);
1021 		if (result == USB_STOR_XFER_ERROR)
1022 			return USB_STOR_TRANSPORT_ERROR;
1023 
1024 		/* If the device tried to send back more data than the
1025 		 * amount requested, the spec requires us to transfer
1026 		 * the CSW anyway.  Since there's no point retrying the
1027 		 * the command, we'll return fake sense data indicating
1028 		 * Illegal Request, Invalid Field in CDB.
1029 		 */
1030 		if (result == USB_STOR_XFER_LONG)
1031 			fake_sense = 1;
1032 	}
1033 
1034 	/* See flow chart on pg 15 of the Bulk Only Transport spec for
1035 	 * an explanation of how this code works.
1036 	 */
1037 
1038 	/* get CSW for device status */
1039 	US_DEBUGP("Attempting to get CSW...\n");
1040 	result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1041 				bcs, US_BULK_CS_WRAP_LEN, &cswlen);
1042 
1043 	/* Some broken devices add unnecessary zero-length packets to the
1044 	 * end of their data transfers.  Such packets show up as 0-length
1045 	 * CSWs.  If we encounter such a thing, try to read the CSW again.
1046 	 */
1047 	if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
1048 		US_DEBUGP("Received 0-length CSW; retrying...\n");
1049 		result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1050 				bcs, US_BULK_CS_WRAP_LEN, &cswlen);
1051 	}
1052 
1053 	/* did the attempt to read the CSW fail? */
1054 	if (result == USB_STOR_XFER_STALLED) {
1055 
1056 		/* get the status again */
1057 		US_DEBUGP("Attempting to get CSW (2nd try)...\n");
1058 		result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1059 				bcs, US_BULK_CS_WRAP_LEN, NULL);
1060 	}
1061 
1062 	/* if we still have a failure at this point, we're in trouble */
1063 	US_DEBUGP("Bulk status result = %d\n", result);
1064 	if (result != USB_STOR_XFER_GOOD)
1065 		return USB_STOR_TRANSPORT_ERROR;
1066 
1067 	/* check bulk status */
1068 	residue = le32_to_cpu(bcs->Residue);
1069 	US_DEBUGP("Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
1070 			le32_to_cpu(bcs->Signature), bcs->Tag,
1071 			residue, bcs->Status);
1072 	if (bcs->Tag != us->tag || bcs->Status > US_BULK_STAT_PHASE) {
1073 		US_DEBUGP("Bulk logical error\n");
1074 		return USB_STOR_TRANSPORT_ERROR;
1075 	}
1076 
1077 	/* Some broken devices report odd signatures, so we do not check them
1078 	 * for validity against the spec. We store the first one we see,
1079 	 * and check subsequent transfers for validity against this signature.
1080 	 */
1081 	if (!us->bcs_signature) {
1082 		us->bcs_signature = bcs->Signature;
1083 		if (us->bcs_signature != cpu_to_le32(US_BULK_CS_SIGN))
1084 			US_DEBUGP("Learnt BCS signature 0x%08X\n",
1085 					le32_to_cpu(us->bcs_signature));
1086 	} else if (bcs->Signature != us->bcs_signature) {
1087 		US_DEBUGP("Signature mismatch: got %08X, expecting %08X\n",
1088 			  le32_to_cpu(bcs->Signature),
1089 			  le32_to_cpu(us->bcs_signature));
1090 		return USB_STOR_TRANSPORT_ERROR;
1091 	}
1092 
1093 	/* try to compute the actual residue, based on how much data
1094 	 * was really transferred and what the device tells us */
1095 	if (residue) {
1096 		if (!(us->flags & US_FL_IGNORE_RESIDUE)) {
1097 			residue = min(residue, transfer_length);
1098 			srb->resid = max(srb->resid, (int) residue);
1099 		}
1100 	}
1101 
1102 	/* based on the status code, we report good or bad */
1103 	switch (bcs->Status) {
1104 		case US_BULK_STAT_OK:
1105 			/* device babbled -- return fake sense data */
1106 			if (fake_sense) {
1107 				memcpy(srb->sense_buffer,
1108 				       usb_stor_sense_invalidCDB,
1109 				       sizeof(usb_stor_sense_invalidCDB));
1110 				return USB_STOR_TRANSPORT_NO_SENSE;
1111 			}
1112 
1113 			/* command good -- note that data could be short */
1114 			return USB_STOR_TRANSPORT_GOOD;
1115 
1116 		case US_BULK_STAT_FAIL:
1117 			/* command failed */
1118 			return USB_STOR_TRANSPORT_FAILED;
1119 
1120 		case US_BULK_STAT_PHASE:
1121 			/* phase error -- note that a transport reset will be
1122 			 * invoked by the invoke_transport() function
1123 			 */
1124 			return USB_STOR_TRANSPORT_ERROR;
1125 	}
1126 
1127 	/* we should never get here, but if we do, we're in trouble */
1128 	return USB_STOR_TRANSPORT_ERROR;
1129 }
1130 
1131 /***********************************************************************
1132  * Reset routines
1133  ***********************************************************************/
1134 
1135 /* This is the common part of the device reset code.
1136  *
1137  * It's handy that every transport mechanism uses the control endpoint for
1138  * resets.
1139  *
1140  * Basically, we send a reset with a 5-second timeout, so we don't get
1141  * jammed attempting to do the reset.
1142  */
1143 static int usb_stor_reset_common(struct us_data *us,
1144 		u8 request, u8 requesttype,
1145 		u16 value, u16 index, void *data, u16 size)
1146 {
1147 	int result;
1148 	int result2;
1149 
1150 	if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
1151 		US_DEBUGP("No reset during disconnect\n");
1152 		return -EIO;
1153 	}
1154 
1155 	result = usb_stor_control_msg(us, us->send_ctrl_pipe,
1156 			request, requesttype, value, index, data, size,
1157 			5*HZ);
1158 	if (result < 0) {
1159 		US_DEBUGP("Soft reset failed: %d\n", result);
1160 		return result;
1161 	}
1162 
1163  	/* Give the device some time to recover from the reset,
1164  	 * but don't delay disconnect processing. */
1165  	wait_event_interruptible_timeout(us->delay_wait,
1166  			test_bit(US_FLIDX_DISCONNECTING, &us->flags),
1167  			HZ*6);
1168 	if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
1169 		US_DEBUGP("Reset interrupted by disconnect\n");
1170 		return -EIO;
1171 	}
1172 
1173 	US_DEBUGP("Soft reset: clearing bulk-in endpoint halt\n");
1174 	result = usb_stor_clear_halt(us, us->recv_bulk_pipe);
1175 
1176 	US_DEBUGP("Soft reset: clearing bulk-out endpoint halt\n");
1177 	result2 = usb_stor_clear_halt(us, us->send_bulk_pipe);
1178 
1179 	/* return a result code based on the result of the clear-halts */
1180 	if (result >= 0)
1181 		result = result2;
1182 	if (result < 0)
1183 		US_DEBUGP("Soft reset failed\n");
1184 	else
1185 		US_DEBUGP("Soft reset done\n");
1186 	return result;
1187 }
1188 
1189 /* This issues a CB[I] Reset to the device in question
1190  */
1191 #define CB_RESET_CMD_SIZE	12
1192 
1193 int usb_stor_CB_reset(struct us_data *us)
1194 {
1195 	US_DEBUGP("%s called\n", __FUNCTION__);
1196 
1197 	memset(us->iobuf, 0xFF, CB_RESET_CMD_SIZE);
1198 	us->iobuf[0] = SEND_DIAGNOSTIC;
1199 	us->iobuf[1] = 4;
1200 	return usb_stor_reset_common(us, US_CBI_ADSC,
1201 				 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1202 				 0, us->ifnum, us->iobuf, CB_RESET_CMD_SIZE);
1203 }
1204 
1205 /* This issues a Bulk-only Reset to the device in question, including
1206  * clearing the subsequent endpoint halts that may occur.
1207  */
1208 int usb_stor_Bulk_reset(struct us_data *us)
1209 {
1210 	US_DEBUGP("%s called\n", __FUNCTION__);
1211 
1212 	return usb_stor_reset_common(us, US_BULK_RESET_REQUEST,
1213 				 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1214 				 0, us->ifnum, NULL, 0);
1215 }
1216 
1217 /* Issue a USB port reset to the device.  But don't do anything if
1218  * there's more than one interface in the device, so that other users
1219  * are not affected. */
1220 int usb_stor_port_reset(struct us_data *us)
1221 {
1222 	int result, rc;
1223 
1224 	if (test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {
1225 		result = -EIO;
1226 		US_DEBUGP("No reset during disconnect\n");
1227 	} else if (us->pusb_dev->actconfig->desc.bNumInterfaces != 1) {
1228 		result = -EBUSY;
1229 		US_DEBUGP("Refusing to reset a multi-interface device\n");
1230 	} else {
1231 		result = rc =
1232 			usb_lock_device_for_reset(us->pusb_dev, us->pusb_intf);
1233 		if (result < 0) {
1234 			US_DEBUGP("unable to lock device for reset: %d\n",
1235 					result);
1236 		} else {
1237 			result = usb_reset_device(us->pusb_dev);
1238 			if (rc)
1239 				usb_unlock_device(us->pusb_dev);
1240 			US_DEBUGP("usb_reset_device returns %d\n", result);
1241 		}
1242 	}
1243 	return result;
1244 }
1245