46 #include <linux/sched.h>
48 #include <linux/errno.h>
49 #include <linux/export.h>
53 #include <scsi/scsi.h>
64 #include "../../scsi/sd.h"
115 static void usb_stor_blocking_completion(
struct urb *
urb)
117 struct completion *urb_done_ptr = urb->context;
128 static int usb_stor_msg_common(
struct us_data *us,
int timeout)
139 init_completion(&urb_done);
150 us->
current_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
182 timeleft == 0 ?
"Timeout" :
"Signal");
200 US_DEBUGP(
"%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
201 __func__, request, requesttype,
205 us->
cr->bRequestType = requesttype;
213 (
unsigned char*) us->
cr, data, size,
214 usb_stor_blocking_completion,
NULL);
215 status = usb_stor_msg_common(us, timeout);
239 int endp = usb_pipeendpoint(pipe);
241 if (usb_pipein (pipe))
252 US_DEBUGP(
"%s: result = %d\n", __func__, result);
265 static int interpret_urb_result(
struct us_data *us,
unsigned int pipe,
268 US_DEBUGP(
"Status code %d; transferred %u/%u\n",
269 result, partial, length);
274 if (partial != length) {
286 if (usb_pipecontrol(pipe)) {
292 US_DEBUGP(
"clearing endpoint halt for pipe 0x%x\n", pipe);
314 US_DEBUGP(
"-- abort or disconnect in progress\n");
334 US_DEBUGP(
"%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
335 __func__, request, requesttype,
339 us->
cr->bRequestType = requesttype;
347 (
unsigned char*) us->
cr, data, size,
348 usb_stor_blocking_completion,
NULL);
349 result = usb_stor_msg_common(us, 0);
351 return interpret_urb_result(us, pipe, size, result,
363 static int usb_stor_intr_transfer(
struct us_data *us,
void *
buf,
370 US_DEBUGP(
"%s: xfer %u bytes\n", __func__, length);
373 maxp = usb_maxpacket(us->
pusb_dev, pipe, usb_pipeout(pipe));
379 maxp, usb_stor_blocking_completion,
NULL,
381 result = usb_stor_msg_common(us, 0);
383 return interpret_urb_result(us, pipe, length, result,
393 void *buf,
unsigned int length,
unsigned int *act_len)
397 US_DEBUGP(
"%s: xfer %u bytes\n", __func__, length);
401 usb_stor_blocking_completion,
NULL);
402 result = usb_stor_msg_common(us, 0);
407 return interpret_urb_result(us, pipe, length, result,
418 static int usb_stor_bulk_transfer_sglist(
struct us_data *us,
unsigned int pipe,
420 unsigned int *act_len)
429 US_DEBUGP(
"%s: xfer %u bytes, %d entries\n", __func__,
434 US_DEBUGP(
"usb_sg_init returned %d\n", result);
459 return interpret_urb_result(us, pipe, length, result,
470 unsigned int partial;
471 int result = usb_stor_bulk_transfer_sglist(us, pipe, scsi_sglist(srb),
472 scsi_sg_count(srb), scsi_bufflen(srb),
475 scsi_set_resid(srb, scsi_bufflen(srb) - partial);
490 void *buf,
unsigned int length_left,
int use_sg,
int *
residual)
493 unsigned int partial;
498 result = usb_stor_bulk_transfer_sglist(us, pipe,
500 length_left, &partial);
501 length_left -= partial;
505 length_left, &partial);
506 length_left -= partial;
511 *residual = length_left;
526 struct gendisk *disk;
531 static unsigned char record_not_found[18] = {
551 sector = (srb->
cmnd[2] << 24) | (srb->
cmnd[3] << 16) |
552 (srb->
cmnd[4] << 8) | (srb->
cmnd[5]);
582 sizeof(record_not_found));
605 scsi_set_resid(srb, 0);
619 US_DEBUGP(
"-- transport indicates error, resetting\n");
627 last_sector_hacks(us, srb);
648 US_DEBUGP(
"-- CB transport device requiring auto-sense\n");
658 US_DEBUGP(
"-- transport indicates command failure\n");
670 !(us->
fflags & US_FL_SANE_SENSE) &&
671 !(us->
fflags & US_FL_BAD_SENSE) &&
672 !(srb->
cmnd[2] & 0x20))) {
673 US_DEBUGP(
"-- SAT supported, increasing auto-sense\n");
674 us->
fflags |= US_FL_SANE_SENSE;
681 if ((scsi_get_resid(srb) > 0) &&
687 US_DEBUGP(
"-- unexpectedly short transfer\n");
691 if (need_auto_sense) {
700 if (us->
fflags & US_FL_SANE_SENSE)
703 US_DEBUGP(
"Issuing auto-REQUEST_SENSE\n");
715 scsi_set_resid(srb, 0);
727 us->
fflags &= ~US_FL_SANE_SENSE;
728 us->
fflags |= US_FL_BAD_SENSE;
740 US_DEBUGP(
"-- auto-sense failure, retry small sense\n");
742 us->
fflags &= ~US_FL_SANE_SENSE;
743 us->
fflags |= US_FL_BAD_SENSE;
756 if (!(us->
fflags & US_FL_SCM_MULT_TARG))
766 !(us->
fflags & US_FL_SANE_SENSE) &&
767 !(us->
fflags & US_FL_BAD_SENSE) &&
769 US_DEBUGP(
"-- SANE_SENSE support enabled\n");
770 us->
fflags |= US_FL_SANE_SENSE;
775 US_DEBUGP(
"-- Sense data truncated to %i from %i\n",
784 US_DEBUGP(
"-- Result from auto-sense is %d\n", temp_result);
785 US_DEBUGP(
"-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n",
788 #ifdef CONFIG_USB_STORAGE_DEBUG
797 fm_ili = (scdd ? scdd[3] : srb->
sense_buffer[2]) & 0xA0;
858 scsi_bufflen(srb) - scsi_get_resid(srb) < srb->
underflow)
861 last_sector_hacks(us, srb);
889 last_sector_hacks(us, srb);
919 unsigned int transfer_length = scsi_bufflen(srb);
920 unsigned int pipe = 0;
931 US_DEBUGP(
"Call to usb_stor_ctrl_transfer() returned %d\n", result);
945 if (transfer_length) {
949 US_DEBUGP(
"CBI data stage result is 0x%x\n", result);
966 result = usb_stor_intr_transfer(us, us->
iobuf, 2);
967 US_DEBUGP(
"Got interrupt data (0x%x, 0x%x)\n",
995 US_DEBUGP(
"CBI IRQ data showed reserved bType 0x%x\n",
1002 switch (us->
iobuf[1] & 0x0F) {
1037 US_DEBUGP(
"GetMaxLUN command result is %d, data is %d\n",
1038 result, us->
iobuf[0]);
1042 return us->
iobuf[0];
1058 unsigned int transfer_length = scsi_bufflen(srb);
1059 unsigned int residue;
1062 unsigned int cswlen;
1078 if (us->
fflags & US_FL_SCM_MULT_TARG)
1087 US_DEBUGP(
"Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n",
1090 (bcb->
Lun >> 4), (bcb->
Lun & 0x0F),
1094 US_DEBUGP(
"Bulk command transfer result=%d\n", result);
1107 if (transfer_length) {
1111 US_DEBUGP(
"Bulk data transfer result 0x%x\n", result);
1130 US_DEBUGP(
"Attempting to get CSW...\n");
1139 US_DEBUGP(
"Received 0-length CSW; retrying...\n");
1148 US_DEBUGP(
"Attempting to get CSW (2nd try)...\n");
1154 US_DEBUGP(
"Bulk status result = %d\n", result);
1160 US_DEBUGP(
"Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
1162 residue, bcs->Status);
1163 if (!(bcs->Tag == us->
tag || (us->
fflags & US_FL_BULK_IGNORE_TAG)) ||
1176 US_DEBUGP(
"Learnt BCS signature 0x%08X\n",
1179 US_DEBUGP(
"Signature mismatch: got %08X, expecting %08X\n",
1187 if (residue && !(us->
fflags & US_FL_IGNORE_RESIDUE)) {
1194 scsi_get_resid(srb) == 0 &&
1196 transfer_length == 36) ||
1198 transfer_length == 8))) {
1199 us->
fflags |= US_FL_IGNORE_RESIDUE;
1202 residue =
min(residue, transfer_length);
1203 scsi_set_resid(srb,
max(scsi_get_resid(srb),
1209 switch (bcs->Status) {
1250 static int usb_stor_reset_common(
struct us_data *us,
1258 US_DEBUGP(
"No reset during disconnect\n");
1263 request, requesttype, value, index, data, size,
1266 US_DEBUGP(
"Soft reset failed: %d\n", result);
1276 US_DEBUGP(
"Reset interrupted by disconnect\n");
1280 US_DEBUGP(
"Soft reset: clearing bulk-in endpoint halt\n");
1283 US_DEBUGP(
"Soft reset: clearing bulk-out endpoint halt\n");
1298 #define CB_RESET_CMD_SIZE 12
1339 US_DEBUGP(
"unable to lock device for reset: %d\n", result);
1344 US_DEBUGP(
"No reset during disconnect\n");
1347 US_DEBUGP(
"usb_reset_device returns %d\n",