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