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