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