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 "transport.h"
58 #include "protocol.h"
59 #include "scsiglue.h"
60 #include "usb.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 because the URB_ASYNC_UNLINK flag is set, the URB can
100 * still be in progress even after a call to 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 =
162 URB_ASYNC_UNLINK | URB_NO_SETUP_DMA_MAP;
163 if (us->current_urb->transfer_buffer == us->iobuf)
164 us->current_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
165 us->current_urb->transfer_dma = us->iobuf_dma;
166 us->current_urb->setup_dma = us->cr_dma;
167
168 /* submit the URB */
169 status = usb_submit_urb(us->current_urb, GFP_NOIO);
170 if (status) {
171 /* something went wrong */
172 return status;
173 }
174
175 /* since the URB has been submitted successfully, it's now okay
176 * to cancel it */
177 set_bit(US_FLIDX_URB_ACTIVE, &us->flags);
178
179 /* did an abort/disconnect occur during the submission? */
180 if (us->flags & ABORTING_OR_DISCONNECTING) {
181
182 /* cancel the URB, if it hasn't been cancelled already */
183 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->flags)) {
184 US_DEBUGP("-- cancelling URB\n");
185 usb_unlink_urb(us->current_urb);
186 }
187 }
188
189 /* submit the timeout timer, if a timeout was requested */
190 if (timeout > 0) {
191 init_timer(&to_timer);
192 to_timer.expires = jiffies + timeout;
193 to_timer.function = timeout_handler;
194 to_timer.data = (unsigned long) us;
195 add_timer(&to_timer);
196 }
197
198 /* wait for the completion of the URB */
199 wait_for_completion(&urb_done);
200 clear_bit(US_FLIDX_URB_ACTIVE, &us->flags);
201
202 /* clean up the timeout timer */
203 if (timeout > 0)
204 del_timer_sync(&to_timer);
205
206 /* return the URB status */
207 return us->current_urb->status;
208 }
209
210 /*
211 * Transfer one control message, with timeouts, and allowing early
212 * termination. Return codes are usual -Exxx, *not* USB_STOR_XFER_xxx.
213 */
214 int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
215 u8 request, u8 requesttype, u16 value, u16 index,
216 void *data, u16 size, int timeout)
217 {
218 int status;
219
220 US_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
221 __FUNCTION__, request, requesttype,
222 value, index, size);
223
224 /* fill in the devrequest structure */
225 us->cr->bRequestType = requesttype;
226 us->cr->bRequest = request;
227 us->cr->wValue = cpu_to_le16(value);
228 us->cr->wIndex = cpu_to_le16(index);
229 us->cr->wLength = cpu_to_le16(size);
230
231 /* fill and submit the URB */
232 usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
233 (unsigned char*) us->cr, data, size,
234 usb_stor_blocking_completion, NULL);
235 status = usb_stor_msg_common(us, timeout);
236
237 /* return the actual length of the data transferred if no error */
238 if (status == 0)
239 status = us->current_urb->actual_length;
240 return status;
241 }
242
243 /* This is a version of usb_clear_halt() that allows early termination and
244 * doesn't read the status from the device -- this is because some devices
245 * crash their internal firmware when the status is requested after a halt.
246 *
247 * A definitive list of these 'bad' devices is too difficult to maintain or
248 * make complete enough to be useful. This problem was first observed on the
249 * Hagiwara FlashGate DUAL unit. However, bus traces reveal that neither
250 * MacOS nor Windows checks the status after clearing a halt.
251 *
252 * Since many vendors in this space limit their testing to interoperability
253 * with these two OSes, specification violations like this one are common.
254 */
255 int usb_stor_clear_halt(struct us_data *us, unsigned int pipe)
256 {
257 int result;
258 int endp = usb_pipeendpoint(pipe);
259
260 if (usb_pipein (pipe))
261 endp |= USB_DIR_IN;
262
263 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
264 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
265 USB_ENDPOINT_HALT, endp,
266 NULL, 0, 3*HZ);
267
268 /* reset the endpoint toggle */
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 int usb_stor_intr_transfer(struct us_data *us, void *buf, unsigned int length)
387 {
388 int result;
389 unsigned int pipe = us->recv_intr_pipe;
390 unsigned int maxp;
391
392 US_DEBUGP("%s: xfer %u bytes\n", __FUNCTION__, length);
393
394 /* calculate the max packet size */
395 maxp = usb_maxpacket(us->pusb_dev, pipe, usb_pipeout(pipe));
396 if (maxp > length)
397 maxp = length;
398
399 /* fill and submit the URB */
400 usb_fill_int_urb(us->current_urb, us->pusb_dev, pipe, buf,
401 maxp, usb_stor_blocking_completion, NULL,
402 us->ep_bInterval);
403 result = usb_stor_msg_common(us, 0);
404
405 return interpret_urb_result(us, pipe, length, result,
406 us->current_urb->actual_length);
407 }
408
409 /*
410 * Transfer one buffer via bulk pipe, without timeouts, but allowing early
411 * termination. Return codes are USB_STOR_XFER_xxx. If the bulk pipe
412 * stalls during the transfer, the halt is automatically cleared.
413 */
414 int usb_stor_bulk_transfer_buf(struct us_data *us, unsigned int pipe,
415 void *buf, unsigned int length, unsigned int *act_len)
416 {
417 int result;
418
419 US_DEBUGP("%s: xfer %u bytes\n", __FUNCTION__, length);
420
421 /* fill and submit the URB */
422 usb_fill_bulk_urb(us->current_urb, us->pusb_dev, pipe, buf, length,
423 usb_stor_blocking_completion, NULL);
424 result = usb_stor_msg_common(us, 0);
425
426 /* store the actual length of the data transferred */
427 if (act_len)
428 *act_len = us->current_urb->actual_length;
429 return interpret_urb_result(us, pipe, length, result,
430 us->current_urb->actual_length);
431 }
432
433 /*
434 * Transfer a scatter-gather list via bulk transfer
435 *
436 * This function does basically the same thing as usb_stor_bulk_transfer_buf()
437 * above, but it uses the usbcore scatter-gather library.
438 */
439 int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
440 struct scatterlist *sg, int num_sg, unsigned int length,
441 unsigned int *act_len)
442 {
443 int result;
444
445 /* don't submit s-g requests during abort/disconnect processing */
446 if (us->flags & ABORTING_OR_DISCONNECTING)
447 return USB_STOR_XFER_ERROR;
448
449 /* initialize the scatter-gather request block */
450 US_DEBUGP("%s: xfer %u bytes, %d entries\n", __FUNCTION__,
451 length, num_sg);
452 result = usb_sg_init(&us->current_sg, us->pusb_dev, pipe, 0,
453 sg, num_sg, length, SLAB_NOIO);
454 if (result) {
455 US_DEBUGP("usb_sg_init returned %d\n", result);
456 return USB_STOR_XFER_ERROR;
457 }
458
459 /* since the block has been initialized successfully, it's now
460 * okay to cancel it */
461 set_bit(US_FLIDX_SG_ACTIVE, &us->flags);
462
463 /* did an abort/disconnect occur during the submission? */
464 if (us->flags & ABORTING_OR_DISCONNECTING) {
465
466 /* cancel the request, if it hasn't been cancelled already */
467 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->flags)) {
468 US_DEBUGP("-- cancelling sg request\n");
469 usb_sg_cancel(&us->current_sg);
470 }
471 }
472
473 /* wait for the completion of the transfer */
474 usb_sg_wait(&us->current_sg);
475 clear_bit(US_FLIDX_SG_ACTIVE, &us->flags);
476
477 result = us->current_sg.status;
478 if (act_len)
479 *act_len = us->current_sg.bytes;
480 return interpret_urb_result(us, pipe, length, result,
481 us->current_sg.bytes);
482 }
483
484 /*
485 * Transfer an entire SCSI command's worth of data payload over the bulk
486 * pipe.
487 *
488 * Note that this uses usb_stor_bulk_transfer_buf() and
489 * usb_stor_bulk_transfer_sglist() to achieve its goals --
490 * this function simply determines whether we're going to use
491 * scatter-gather or not, and acts appropriately.
492 */
493 int usb_stor_bulk_transfer_sg(struct us_data* us, unsigned int pipe,
494 void *buf, unsigned int length_left, int use_sg, int *residual)
495 {
496 int result;
497 unsigned int partial;
498
499 /* are we scatter-gathering? */
500 if (use_sg) {
501 /* use the usb core scatter-gather primitives */
502 result = usb_stor_bulk_transfer_sglist(us, pipe,
503 (struct scatterlist *) buf, use_sg,
504 length_left, &partial);
505 length_left -= partial;
506 } else {
507 /* no scatter-gather, just make the request */
508 result = usb_stor_bulk_transfer_buf(us, pipe, buf,
509 length_left, &partial);
510 length_left -= partial;
511 }
512
513 /* store the residual and return the error code */
514 if (residual)
515 *residual = length_left;
516 return result;
517 }
518
519 /***********************************************************************
520 * Transport routines
521 ***********************************************************************/
522
523 /* Invoke the transport and basic error-handling/recovery methods
524 *
525 * This is used by the protocol layers to actually send the message to
526 * the device and receive the response.
527 */
528 void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
529 {
530 int need_auto_sense;
531 int result;
532
533 /* send the command to the transport layer */
534 srb->resid = 0;
535 result = us->transport(srb, us);
536
537 /* if the command gets aborted by the higher layers, we need to
538 * short-circuit all other processing
539 */
540 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
541 US_DEBUGP("-- command was aborted\n");
542 goto Handle_Abort;
543 }
544
545 /* if there is a transport error, reset and don't auto-sense */
546 if (result == USB_STOR_TRANSPORT_ERROR) {
547 US_DEBUGP("-- transport indicates error, resetting\n");
548 us->transport_reset(us);
549 srb->result = DID_ERROR << 16;
550 return;
551 }
552
553 /* if the transport provided its own sense data, don't auto-sense */
554 if (result == USB_STOR_TRANSPORT_NO_SENSE) {
555 srb->result = SAM_STAT_CHECK_CONDITION;
556 return;
557 }
558
559 srb->result = SAM_STAT_GOOD;
560
561 /* Determine if we need to auto-sense
562 *
563 * I normally don't use a flag like this, but it's almost impossible
564 * to understand what's going on here if I don't.
565 */
566 need_auto_sense = 0;
567
568 /*
569 * If we're running the CB transport, which is incapable
570 * of determining status on its own, we will auto-sense
571 * unless the operation involved a data-in transfer. Devices
572 * can signal most data-in errors by stalling the bulk-in pipe.
573 */
574 if ((us->protocol == US_PR_CB || us->protocol == US_PR_DPCM_USB) &&
575 srb->sc_data_direction != DMA_FROM_DEVICE) {
576 US_DEBUGP("-- CB transport device requiring auto-sense\n");
577 need_auto_sense = 1;
578 }
579
580 /*
581 * If we have a failure, we're going to do a REQUEST_SENSE
582 * automatically. Note that we differentiate between a command
583 * "failure" and an "error" in the transport mechanism.
584 */
585 if (result == USB_STOR_TRANSPORT_FAILED) {
586 US_DEBUGP("-- transport indicates command failure\n");
587 need_auto_sense = 1;
588 }
589
590 /*
591 * A short transfer on a command where we don't expect it
592 * is unusual, but it doesn't mean we need to auto-sense.
593 */
594 if ((srb->resid > 0) &&
595 !((srb->cmnd[0] == REQUEST_SENSE) ||
596 (srb->cmnd[0] == INQUIRY) ||
597 (srb->cmnd[0] == MODE_SENSE) ||
598 (srb->cmnd[0] == LOG_SENSE) ||
599 (srb->cmnd[0] == MODE_SENSE_10))) {
600 US_DEBUGP("-- unexpectedly short transfer\n");
601 }
602
603 /* Now, if we need to do the auto-sense, let's do it */
604 if (need_auto_sense) {
605 int temp_result;
606 void* old_request_buffer;
607 unsigned short old_sg;
608 unsigned old_request_bufflen;
609 unsigned char old_sc_data_direction;
610 unsigned char old_cmd_len;
611 unsigned char old_cmnd[MAX_COMMAND_SIZE];
612 unsigned long old_serial_number;
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 = srb->sense_buffer;
640
641 /* set the buffer length for transfer */
642 old_request_bufflen = srb->request_bufflen;
643 srb->request_bufflen = 18;
644
645 /* set up for no scatter-gather use */
646 old_sg = srb->use_sg;
647 srb->use_sg = 0;
648
649 /* change the serial number -- toggle the high bit*/
650 old_serial_number = srb->serial_number;
651 srb->serial_number ^= 0x80000000;
652
653 /* issue the auto-sense command */
654 old_resid = srb->resid;
655 srb->resid = 0;
656 temp_result = us->transport(us->srb, us);
657
658 /* let's clean up right away */
659 srb->resid = old_resid;
660 srb->request_buffer = old_request_buffer;
661 srb->request_bufflen = old_request_bufflen;
662 srb->use_sg = old_sg;
663 srb->serial_number = old_serial_number;
664 srb->sc_data_direction = old_sc_data_direction;
665 srb->cmd_len = old_cmd_len;
666 memcpy(srb->cmnd, old_cmnd, MAX_COMMAND_SIZE);
667
668 if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
669 US_DEBUGP("-- auto-sense aborted\n");
670 goto Handle_Abort;
671 }
672 if (temp_result != USB_STOR_TRANSPORT_GOOD) {
673 US_DEBUGP("-- auto-sense failure\n");
674
675 /* we skip the reset if this happens to be a
676 * multi-target device, since failure of an
677 * auto-sense is perfectly valid
678 */
679 if (!(us->flags & US_FL_SCM_MULT_TARG))
680 us->transport_reset(us);
681 srb->result = DID_ERROR << 16;
682 return;
683 }
684
685 US_DEBUGP("-- Result from auto-sense is %d\n", temp_result);
686 US_DEBUGP("-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n",
687 srb->sense_buffer[0],
688 srb->sense_buffer[2] & 0xf,
689 srb->sense_buffer[12],
690 srb->sense_buffer[13]);
691 #ifdef CONFIG_USB_STORAGE_DEBUG
692 usb_stor_show_sense(
693 srb->sense_buffer[2] & 0xf,
694 srb->sense_buffer[12],
695 srb->sense_buffer[13]);
696 #endif
697
698 /* set the result so the higher layers expect this data */
699 srb->result = SAM_STAT_CHECK_CONDITION;
700
701 /* If things are really okay, then let's show that. Zero
702 * out the sense buffer so the higher layers won't realize
703 * we did an unsolicited auto-sense. */
704 if (result == USB_STOR_TRANSPORT_GOOD &&
705 /* Filemark 0, ignore EOM, ILI 0, no sense */
706 (srb->sense_buffer[2] & 0xaf) == 0 &&
707 /* No ASC or ASCQ */
708 srb->sense_buffer[12] == 0 &&
709 srb->sense_buffer[13] == 0) {
710 srb->result = SAM_STAT_GOOD;
711 srb->sense_buffer[0] = 0x0;
712 }
713 }
714
715 /* Did we transfer less than the minimum amount required? */
716 if (srb->result == SAM_STAT_GOOD &&
717 srb->request_bufflen - srb->resid < srb->underflow)
718 srb->result = (DID_ERROR << 16) | (SUGGEST_RETRY << 24);
719
720 return;
721
722 /* abort processing: the bulk-only transport requires a reset
723 * following an abort */
724 Handle_Abort:
725 srb->result = DID_ABORT << 16;
726 if (us->protocol == US_PR_BULK)
727 us->transport_reset(us);
728 }
729
730 /* Stop the current URB transfer */
731 void usb_stor_stop_transport(struct us_data *us)
732 {
733 US_DEBUGP("%s called\n", __FUNCTION__);
734
735 /* If the state machine is blocked waiting for an URB,
736 * let's wake it up. The test_and_clear_bit() call
737 * guarantees that if a URB has just been submitted,
738 * it won't be cancelled more than once. */
739 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->flags)) {
740 US_DEBUGP("-- cancelling URB\n");
741 usb_unlink_urb(us->current_urb);
742 }
743
744 /* If we are waiting for a scatter-gather operation, cancel it. */
745 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->flags)) {
746 US_DEBUGP("-- cancelling sg request\n");
747 usb_sg_cancel(&us->current_sg);
748 }
749 }
750
751 /*
752 * Control/Bulk/Interrupt transport
753 */
754
755 int usb_stor_CBI_transport(struct scsi_cmnd *srb, struct us_data *us)
756 {
757 unsigned int transfer_length = srb->request_bufflen;
758 unsigned int pipe = 0;
759 int result;
760
761 /* COMMAND STAGE */
762 /* let's send the command via the control pipe */
763 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
764 US_CBI_ADSC,
765 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
766 us->ifnum, srb->cmnd, srb->cmd_len);
767
768 /* check the return code for the command */
769 US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result);
770
771 /* if we stalled the command, it means command failed */
772 if (result == USB_STOR_XFER_STALLED) {
773 return USB_STOR_TRANSPORT_FAILED;
774 }
775
776 /* Uh oh... serious problem here */
777 if (result != USB_STOR_XFER_GOOD) {
778 return USB_STOR_TRANSPORT_ERROR;
779 }
780
781 /* DATA STAGE */
782 /* transfer the data payload for this command, if one exists*/
783 if (transfer_length) {
784 pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
785 us->recv_bulk_pipe : us->send_bulk_pipe;
786 result = usb_stor_bulk_transfer_sg(us, pipe,
787 srb->request_buffer, transfer_length,
788 srb->use_sg, &srb->resid);
789 US_DEBUGP("CBI data stage result is 0x%x\n", result);
790
791 /* if we stalled the data transfer it means command failed */
792 if (result == USB_STOR_XFER_STALLED)
793 return USB_STOR_TRANSPORT_FAILED;
794 if (result > USB_STOR_XFER_STALLED)
795 return USB_STOR_TRANSPORT_ERROR;
796 }
797
798 /* STATUS STAGE */
799 result = usb_stor_intr_transfer(us, us->iobuf, 2);
800 US_DEBUGP("Got interrupt data (0x%x, 0x%x)\n",
801 us->iobuf[0], us->iobuf[1]);
802 if (result != USB_STOR_XFER_GOOD)
803 return USB_STOR_TRANSPORT_ERROR;
804
805 /* UFI gives us ASC and ASCQ, like a request sense
806 *
807 * REQUEST_SENSE and INQUIRY don't affect the sense data on UFI
808 * devices, so we ignore the information for those commands. Note
809 * that this means we could be ignoring a real error on these
810 * commands, but that can't be helped.
811 */
812 if (us->subclass == US_SC_UFI) {
813 if (srb->cmnd[0] == REQUEST_SENSE ||
814 srb->cmnd[0] == INQUIRY)
815 return USB_STOR_TRANSPORT_GOOD;
816 if (us->iobuf[0])
817 goto Failed;
818 return USB_STOR_TRANSPORT_GOOD;
819 }
820
821 /* If not UFI, we interpret the data as a result code
822 * The first byte should always be a 0x0.
823 *
824 * Some bogus devices don't follow that rule. They stuff the ASC
825 * into the first byte -- so if it's non-zero, call it a failure.
826 */
827 if (us->iobuf[0]) {
828 US_DEBUGP("CBI IRQ data showed reserved bType 0x%x\n",
829 us->iobuf[0]);
830 goto Failed;
831
832 }
833
834 /* The second byte & 0x0F should be 0x0 for good, otherwise error */
835 switch (us->iobuf[1] & 0x0F) {
836 case 0x00:
837 return USB_STOR_TRANSPORT_GOOD;
838 case 0x01:
839 goto Failed;
840 }
841 return USB_STOR_TRANSPORT_ERROR;
842
843 /* the CBI spec requires that the bulk pipe must be cleared
844 * following any data-in/out command failure (section 2.4.3.1.3)
845 */
846 Failed:
847 if (pipe)
848 usb_stor_clear_halt(us, pipe);
849 return USB_STOR_TRANSPORT_FAILED;
850 }
851
852 /*
853 * Control/Bulk transport
854 */
855 int usb_stor_CB_transport(struct scsi_cmnd *srb, struct us_data *us)
856 {
857 unsigned int transfer_length = srb->request_bufflen;
858 int result;
859
860 /* COMMAND STAGE */
861 /* let's send the command via the control pipe */
862 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
863 US_CBI_ADSC,
864 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
865 us->ifnum, srb->cmnd, srb->cmd_len);
866
867 /* check the return code for the command */
868 US_DEBUGP("Call to usb_stor_ctrl_transfer() returned %d\n", result);
869
870 /* if we stalled the command, it means command failed */
871 if (result == USB_STOR_XFER_STALLED) {
872 return USB_STOR_TRANSPORT_FAILED;
873 }
874
875 /* Uh oh... serious problem here */
876 if (result != USB_STOR_XFER_GOOD) {
877 return USB_STOR_TRANSPORT_ERROR;
878 }
879
880 /* DATA STAGE */
881 /* transfer the data payload for this command, if one exists*/
882 if (transfer_length) {
883 unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
884 us->recv_bulk_pipe : us->send_bulk_pipe;
885 result = usb_stor_bulk_transfer_sg(us, pipe,
886 srb->request_buffer, transfer_length,
887 srb->use_sg, &srb->resid);
888 US_DEBUGP("CB data stage result is 0x%x\n", result);
889
890 /* if we stalled the data transfer it means command failed */
891 if (result == USB_STOR_XFER_STALLED)
892 return USB_STOR_TRANSPORT_FAILED;
893 if (result > USB_STOR_XFER_STALLED)
894 return USB_STOR_TRANSPORT_ERROR;
895 }
896
897 /* STATUS STAGE */
898 /* NOTE: CB does not have a status stage. Silly, I know. So
899 * we have to catch this at a higher level.
900 */
901 return USB_STOR_TRANSPORT_GOOD;
902 }
903
904 /*
905 * Bulk only transport
906 */
907
908 /* Determine what the maximum LUN supported is */
909 int usb_stor_Bulk_max_lun(struct us_data *us)
910 {
911 int result;
912
913 /* issue the command */
914 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
915 US_BULK_GET_MAX_LUN,
916 USB_DIR_IN | USB_TYPE_CLASS |
917 USB_RECIP_INTERFACE,
918 0, us->ifnum, us->iobuf, 1, HZ);
919
920 US_DEBUGP("GetMaxLUN command result is %d, data is %d\n",
921 result, us->iobuf[0]);
922
923 /* if we have a successful request, return the result */
924 if (result > 0)
925 return us->iobuf[0];
926
927 /*
928 * Some devices (i.e. Iomega Zip100) need this -- apparently
929 * the bulk pipes get STALLed when the GetMaxLUN request is
930 * processed. This is, in theory, harmless to all other devices
931 * (regardless of if they stall or not).
932 */
933 if (result == -EPIPE) {
934 usb_stor_clear_halt(us, us->recv_bulk_pipe);
935 usb_stor_clear_halt(us, us->send_bulk_pipe);
936 }
937
938 /*
939 * Some devices don't like GetMaxLUN. They may STALL the control
940 * pipe, they may return a zero-length result, they may do nothing at
941 * all and timeout, or they may fail in even more bizarrely creative
942 * ways. In these cases the best approach is to use the default
943 * value: only one LUN.
944 */
945 return 0;
946 }
947
948 int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
949 {
950 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
951 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
952 unsigned int transfer_length = srb->request_bufflen;
953 unsigned int residue;
954 int result;
955 int fake_sense = 0;
956 unsigned int cswlen;
957 unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
958
959 /* Take care of BULK32 devices; set extra byte to 0 */
960 if ( unlikely(us->flags & US_FL_BULK32)) {
961 cbwlen = 32;
962 us->iobuf[31] = 0;
963 }
964
965 /* set up the command wrapper */
966 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
967 bcb->DataTransferLength = cpu_to_le32(transfer_length);
968 bcb->Flags = srb->sc_data_direction == DMA_FROM_DEVICE ? 1 << 7 : 0;
969 bcb->Tag = srb->serial_number;
970 bcb->Lun = srb->device->lun;
971 if (us->flags & US_FL_SCM_MULT_TARG)
972 bcb->Lun |= srb->device->id << 4;
973 bcb->Length = srb->cmd_len;
974
975 /* copy the command payload */
976 memset(bcb->CDB, 0, sizeof(bcb->CDB));
977 memcpy(bcb->CDB, srb->cmnd, bcb->Length);
978
979 /* send it to out endpoint */
980 US_DEBUGP("Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n",
981 le32_to_cpu(bcb->Signature), bcb->Tag,
982 le32_to_cpu(bcb->DataTransferLength), bcb->Flags,
983 (bcb->Lun >> 4), (bcb->Lun & 0x0F),
984 bcb->Length);
985 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
986 bcb, cbwlen, NULL);
987 US_DEBUGP("Bulk command transfer result=%d\n", result);
988 if (result != USB_STOR_XFER_GOOD)
989 return USB_STOR_TRANSPORT_ERROR;
990
991 /* DATA STAGE */
992 /* send/receive data payload, if there is any */
993
994 /* Genesys Logic interface chips need a 100us delay between the
995 * command phase and the data phase. Some devices need a little
996 * more than that, probably because of clock rate inaccuracies. */
997 if (le16_to_cpu(us->pusb_dev->descriptor.idVendor) == USB_VENDOR_ID_GENESYS)
998 udelay(110);
999
1000 if (transfer_length) {
1001 unsigned int pipe = srb->sc_data_direction == DMA_FROM_DEVICE ?
1002 us->recv_bulk_pipe : us->send_bulk_pipe;
1003 result = usb_stor_bulk_transfer_sg(us, pipe,
1004 srb->request_buffer, transfer_length,
1005 srb->use_sg, &srb->resid);
1006 US_DEBUGP("Bulk data transfer result 0x%x\n", result);
1007 if (result == USB_STOR_XFER_ERROR)
1008 return USB_STOR_TRANSPORT_ERROR;
1009
1010 /* If the device tried to send back more data than the
1011 * amount requested, the spec requires us to transfer
1012 * the CSW anyway. Since there's no point retrying the
1013 * the command, we'll return fake sense data indicating
1014 * Illegal Request, Invalid Field in CDB.
1015 */
1016 if (result == USB_STOR_XFER_LONG)
1017 fake_sense = 1;
1018 }
1019
1020 /* See flow chart on pg 15 of the Bulk Only Transport spec for
1021 * an explanation of how this code works.
1022 */
1023
1024 /* get CSW for device status */
1025 US_DEBUGP("Attempting to get CSW...\n");
1026 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1027 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
1028
1029 /* Some broken devices add unnecessary zero-length packets to the
1030 * end of their data transfers. Such packets show up as 0-length
1031 * CSWs. If we encounter such a thing, try to read the CSW again.
1032 */
1033 if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
1034 US_DEBUGP("Received 0-length CSW; retrying...\n");
1035 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1036 bcs, US_BULK_CS_WRAP_LEN, &cswlen);
1037 }
1038
1039 /* did the attempt to read the CSW fail? */
1040 if (result == USB_STOR_XFER_STALLED) {
1041
1042 /* get the status again */
1043 US_DEBUGP("Attempting to get CSW (2nd try)...\n");
1044 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
1045 bcs, US_BULK_CS_WRAP_LEN, NULL);
1046 }
1047
1048 /* if we still have a failure at this point, we're in trouble */
1049 US_DEBUGP("Bulk status result = %d\n", result);
1050 if (result != USB_STOR_XFER_GOOD)
1051 return USB_STOR_TRANSPORT_ERROR;
1052
1053 /* check bulk status */
1054 residue = le32_to_cpu(bcs->Residue);
1055 US_DEBUGP("Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
1056 le32_to_cpu(bcs->Signature), bcs->Tag,
1057 residue, bcs->Status);
1058 if ((bcs->Signature != cpu_to_le32(US_BULK_CS_SIGN) &&
1059 bcs->Signature != cpu_to_le32(US_BULK_CS_OLYMPUS_SIGN)) ||
1060 bcs->Tag != srb->serial_number ||
1061 bcs->Status > US_BULK_STAT_PHASE) {
1062 US_DEBUGP("Bulk logical error\n");
1063 return USB_STOR_TRANSPORT_ERROR;
1064 }
1065
1066 /* try to compute the actual residue, based on how much data
1067 * was really transferred and what the device tells us */
1068 if (residue) {
1069 if (!(us->flags & US_FL_IGNORE_RESIDUE) ||
1070 srb->sc_data_direction == DMA_TO_DEVICE) {
1071 residue = min(residue, transfer_length);
1072 srb->resid = max(srb->resid, (int) residue);
1073 }
1074 }
1075
1076 /* based on the status code, we report good or bad */
1077 switch (bcs->Status) {
1078 case US_BULK_STAT_OK:
1079 /* device babbled -- return fake sense data */
1080 if (fake_sense) {
1081 memcpy(srb->sense_buffer,
1082 usb_stor_sense_invalidCDB,
1083 sizeof(usb_stor_sense_invalidCDB));
1084 return USB_STOR_TRANSPORT_NO_SENSE;
1085 }
1086
1087 /* command good -- note that data could be short */
1088 return USB_STOR_TRANSPORT_GOOD;
1089
1090 case US_BULK_STAT_FAIL:
1091 /* command failed */
1092 return USB_STOR_TRANSPORT_FAILED;
1093
1094 case US_BULK_STAT_PHASE:
1095 /* phase error -- note that a transport reset will be
1096 * invoked by the invoke_transport() function
1097 */
1098 return USB_STOR_TRANSPORT_ERROR;
1099 }
1100
1101 /* we should never get here, but if we do, we're in trouble */
1102 return USB_STOR_TRANSPORT_ERROR;
1103 }
1104
1105 /***********************************************************************
1106 * Reset routines
1107 ***********************************************************************/
1108
1109 /* This is the common part of the device reset code.
1110 *
1111 * It's handy that every transport mechanism uses the control endpoint for
1112 * resets.
1113 *
1114 * Basically, we send a reset with a 20-second timeout, so we don't get
1115 * jammed attempting to do the reset.
1116 */
1117 static int usb_stor_reset_common(struct us_data *us,
1118 u8 request, u8 requesttype,
1119 u16 value, u16 index, void *data, u16 size)
1120 {
1121 int result;
1122 int result2;
1123 int rc = FAILED;
1124
1125 /* Let the SCSI layer know we are doing a reset, set the
1126 * RESETTING bit, and clear the ABORTING bit so that the reset
1127 * may proceed.
1128 */
1129 scsi_lock(us->host);
1130 usb_stor_report_device_reset(us);
1131 set_bit(US_FLIDX_RESETTING, &us->flags);
1132 clear_bit(US_FLIDX_ABORTING, &us->flags);
1133 scsi_unlock(us->host);
1134
1135 /* A 20-second timeout may seem rather long, but a LaCie
1136 * StudioDrive USB2 device takes 16+ seconds to get going
1137 * following a powerup or USB attach event.
1138 */
1139 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
1140 request, requesttype, value, index, data, size,
1141 20*HZ);
1142 if (result < 0) {
1143 US_DEBUGP("Soft reset failed: %d\n", result);
1144 goto Done;
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->dev_reset_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");
1154 goto Done;
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
1163 /* return a result code based on the result of the control message */
1164 if (result < 0 || result2 < 0) {
1165 US_DEBUGP("Soft reset failed\n");
1166 goto Done;
1167 }
1168 US_DEBUGP("Soft reset done\n");
1169 rc = SUCCESS;
1170
1171 Done:
1172 clear_bit(US_FLIDX_RESETTING, &us->flags);
1173 return rc;
1174 }
1175
1176 /* This issues a CB[I] Reset to the device in question
1177 */
1178 #define CB_RESET_CMD_SIZE 12
1179
1180 int usb_stor_CB_reset(struct us_data *us)
1181 {
1182 US_DEBUGP("%s called\n", __FUNCTION__);
1183
1184 memset(us->iobuf, 0xFF, CB_RESET_CMD_SIZE);
1185 us->iobuf[0] = SEND_DIAGNOSTIC;
1186 us->iobuf[1] = 4;
1187 return usb_stor_reset_common(us, US_CBI_ADSC,
1188 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1189 0, us->ifnum, us->iobuf, CB_RESET_CMD_SIZE);
1190 }
1191
1192 /* This issues a Bulk-only Reset to the device in question, including
1193 * clearing the subsequent endpoint halts that may occur.
1194 */
1195 int usb_stor_Bulk_reset(struct us_data *us)
1196 {
1197 US_DEBUGP("%s called\n", __FUNCTION__);
1198
1199 return usb_stor_reset_common(us, US_BULK_RESET_REQUEST,
1200 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1201 0, us->ifnum, NULL, 0);
1202 }
1203
|
This page was automatically generated by the
LXR engine.
|