1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #include <linux/sched.h>
4 #include <linux/errno.h>
5 #include <linux/slab.h>
8 #include <scsi/scsi_eh.h>
9 #include <scsi/scsi_device.h>
13 #include "transport.h"
15 /***********************************************************************
16 * Data transfer routines
17 ***********************************************************************/
19 * usb_stor_blocking_completion()
21 static void usb_stor_blocking_completion(struct urb
*urb
)
23 struct completion
*urb_done_ptr
= urb
->context
;
25 /* pr_info("transport --- usb_stor_blocking_completion\n"); */
26 complete(urb_done_ptr
);
30 * usb_stor_msg_common()
32 static int usb_stor_msg_common(struct us_data
*us
, int timeout
)
34 struct completion urb_done
;
38 /* pr_info("transport --- usb_stor_msg_common\n"); */
39 if (test_bit(US_FLIDX_ABORTING
, &us
->dflags
))
42 init_completion(&urb_done
);
44 us
->current_urb
->context
= &urb_done
;
45 us
->current_urb
->actual_length
= 0;
46 us
->current_urb
->error_count
= 0;
47 us
->current_urb
->status
= 0;
49 us
->current_urb
->transfer_flags
= 0;
50 if (us
->current_urb
->transfer_buffer
== us
->iobuf
)
51 us
->current_urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
52 us
->current_urb
->transfer_dma
= us
->iobuf_dma
;
53 us
->current_urb
->setup_dma
= us
->cr_dma
;
55 status
= usb_submit_urb(us
->current_urb
, GFP_NOIO
);
59 set_bit(US_FLIDX_URB_ACTIVE
, &us
->dflags
);
61 if (test_bit(US_FLIDX_ABORTING
, &us
->dflags
)) {
62 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE
, &us
->dflags
)) {
63 /* pr_info("-- cancelling URB\n"); */
64 usb_unlink_urb(us
->current_urb
);
68 timeleft
= wait_for_completion_interruptible_timeout(&urb_done
,
69 timeout
? : MAX_SCHEDULE_TIMEOUT
);
70 clear_bit(US_FLIDX_URB_ACTIVE
, &us
->dflags
);
73 /* pr_info("%s -- cancelling URB\n",
74 timeleft == 0 ? "Timeout" : "Signal"); */
75 usb_kill_urb(us
->current_urb
);
78 return us
->current_urb
->status
;
82 * usb_stor_print_cmd():
84 static void usb_stor_print_cmd(struct us_data
*us
, struct scsi_cmnd
*srb
)
93 dev_dbg(&us
->pusb_dev
->dev
,
94 "scsi cmd %X --- SCSIOP_INQUIRY\n", cmd
);
97 dev_dbg(&us
->pusb_dev
->dev
,
98 "scsi cmd %X --- SCSIOP_MODE_SENSE\n", cmd
);
101 dev_dbg(&us
->pusb_dev
->dev
,
102 "scsi cmd %X --- SCSIOP_START_STOP\n", cmd
);
105 dev_dbg(&us
->pusb_dev
->dev
,
106 "scsi cmd %X --- SCSIOP_READ_CAPACITY\n", cmd
);
112 case ALLOW_MEDIUM_REMOVAL
:
113 dev_dbg(&us
->pusb_dev
->dev
,
114 "scsi cmd %X --- SCSIOP_ALLOW_MEDIUM_REMOVAL\n", cmd
);
117 dev_dbg(&us
->pusb_dev
->dev
, "scsi cmd %X --- Other cmd\n", cmd
);
123 * usb_stor_control_msg()
125 int usb_stor_control_msg(struct us_data
*us
, unsigned int pipe
,
126 u8 request
, u8 requesttype
, u16 value
, u16 index
,
127 void *data
, u16 size
, int timeout
)
131 /* pr_info("transport --- usb_stor_control_msg\n"); */
133 /* fill in the devrequest structure */
134 us
->cr
->bRequestType
= requesttype
;
135 us
->cr
->bRequest
= request
;
136 us
->cr
->wValue
= cpu_to_le16(value
);
137 us
->cr
->wIndex
= cpu_to_le16(index
);
138 us
->cr
->wLength
= cpu_to_le16(size
);
140 /* fill and submit the URB */
141 usb_fill_control_urb(us
->current_urb
, us
->pusb_dev
, pipe
,
142 (unsigned char *) us
->cr
, data
, size
,
143 usb_stor_blocking_completion
, NULL
);
144 status
= usb_stor_msg_common(us
, timeout
);
146 /* return the actual length of the data transferred if no error */
148 status
= us
->current_urb
->actual_length
;
153 * usb_stor_clear_halt()
155 int usb_stor_clear_halt(struct us_data
*us
, unsigned int pipe
)
158 int endp
= usb_pipeendpoint(pipe
);
160 /* pr_info("transport --- usb_stor_clear_halt\n"); */
161 if (usb_pipein(pipe
))
164 result
= usb_stor_control_msg(us
, us
->send_ctrl_pipe
,
165 USB_REQ_CLEAR_FEATURE
, USB_RECIP_ENDPOINT
,
166 USB_ENDPOINT_HALT
, endp
,
169 /* reset the endpoint toggle */
171 /* usb_settoggle(us->pusb_dev, usb_pipeendpoint(pipe),
172 usb_pipeout(pipe), 0); */
173 usb_reset_endpoint(us
->pusb_dev
, endp
);
179 * interpret_urb_result()
181 static int interpret_urb_result(struct us_data
*us
, unsigned int pipe
,
182 unsigned int length
, int result
, unsigned int partial
)
184 /* pr_info("transport --- interpret_urb_result\n"); */
186 /* no error code; did we send all the data? */
188 if (partial
!= length
) {
189 /* pr_info("-- short transfer\n"); */
190 return USB_STOR_XFER_SHORT
;
192 /* pr_info("-- transfer complete\n"); */
193 return USB_STOR_XFER_GOOD
;
195 if (usb_pipecontrol(pipe
)) {
196 /* pr_info("-- stall on control pipe\n"); */
197 return USB_STOR_XFER_STALLED
;
199 /* pr_info("clearing endpoint halt for pipe 0x%x\n", pipe); */
200 if (usb_stor_clear_halt(us
, pipe
) < 0)
201 return USB_STOR_XFER_ERROR
;
202 return USB_STOR_XFER_STALLED
;
204 /* pr_info("-- babble\n"); */
205 return USB_STOR_XFER_LONG
;
207 /* pr_info("-- transfer cancelled\n"); */
208 return USB_STOR_XFER_ERROR
;
210 /* pr_info("-- short read transfer\n"); */
211 return USB_STOR_XFER_SHORT
;
213 /* pr_info("-- abort or disconnect in progress\n"); */
214 return USB_STOR_XFER_ERROR
;
216 /* pr_info("-- unknown error\n"); */
217 return USB_STOR_XFER_ERROR
;
222 * usb_stor_bulk_transfer_buf()
224 int usb_stor_bulk_transfer_buf(struct us_data
*us
, unsigned int pipe
,
225 void *buf
, unsigned int length
, unsigned int *act_len
)
229 /* pr_info("transport --- usb_stor_bulk_transfer_buf\n"); */
231 /* fill and submit the URB */
232 usb_fill_bulk_urb(us
->current_urb
, us
->pusb_dev
, pipe
, buf
,
233 length
, usb_stor_blocking_completion
, NULL
);
234 result
= usb_stor_msg_common(us
, 0);
236 /* store the actual length of the data transferred */
238 *act_len
= us
->current_urb
->actual_length
;
240 return interpret_urb_result(us
, pipe
, length
, result
,
241 us
->current_urb
->actual_length
);
245 * usb_stor_bulk_transfer_sglist()
247 static int usb_stor_bulk_transfer_sglist(struct us_data
*us
, unsigned int pipe
,
248 struct scatterlist
*sg
, int num_sg
, unsigned int length
,
249 unsigned int *act_len
)
253 /* pr_info("transport --- usb_stor_bulk_transfer_sglist\n"); */
254 if (test_bit(US_FLIDX_ABORTING
, &us
->dflags
))
255 return USB_STOR_XFER_ERROR
;
257 /* initialize the scatter-gather request block */
258 result
= usb_sg_init(&us
->current_sg
, us
->pusb_dev
, pipe
, 0,
259 sg
, num_sg
, length
, GFP_NOIO
);
261 /* pr_info("usb_sg_init returned %d\n", result); */
262 return USB_STOR_XFER_ERROR
;
265 /* since the block has been initialized successfully,
266 it's now okay to cancel it */
267 set_bit(US_FLIDX_SG_ACTIVE
, &us
->dflags
);
269 /* did an abort/disconnect occur during the submission? */
270 if (test_bit(US_FLIDX_ABORTING
, &us
->dflags
)) {
271 /* cancel the request, if it hasn't been cancelled already */
272 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE
, &us
->dflags
)) {
273 /* pr_info("-- cancelling sg request\n"); */
274 usb_sg_cancel(&us
->current_sg
);
278 /* wait for the completion of the transfer */
279 usb_sg_wait(&us
->current_sg
);
280 clear_bit(US_FLIDX_SG_ACTIVE
, &us
->dflags
);
282 result
= us
->current_sg
.status
;
284 *act_len
= us
->current_sg
.bytes
;
286 return interpret_urb_result(us
, pipe
, length
,
287 result
, us
->current_sg
.bytes
);
291 * usb_stor_bulk_srb()
293 int usb_stor_bulk_srb(struct us_data
*us
, unsigned int pipe
,
294 struct scsi_cmnd
*srb
)
296 unsigned int partial
;
297 int result
= usb_stor_bulk_transfer_sglist(us
, pipe
, scsi_sglist(srb
),
298 scsi_sg_count(srb
), scsi_bufflen(srb
),
301 scsi_set_resid(srb
, scsi_bufflen(srb
) - partial
);
306 * usb_stor_bulk_transfer_sg()
308 int usb_stor_bulk_transfer_sg(struct us_data
*us
, unsigned int pipe
,
309 void *buf
, unsigned int length_left
, int use_sg
, int *residual
)
312 unsigned int partial
;
314 /* pr_info("transport --- usb_stor_bulk_transfer_sg\n"); */
315 /* are we scatter-gathering? */
317 /* use the usb core scatter-gather primitives */
318 result
= usb_stor_bulk_transfer_sglist(us
, pipe
,
319 (struct scatterlist
*) buf
, use_sg
,
320 length_left
, &partial
);
321 length_left
-= partial
;
323 /* no scatter-gather, just make the request */
324 result
= usb_stor_bulk_transfer_buf(us
, pipe
, buf
,
325 length_left
, &partial
);
326 length_left
-= partial
;
329 /* store the residual and return the error code */
331 *residual
= length_left
;
335 /***********************************************************************
337 ***********************************************************************/
339 * usb_stor_invoke_transport()
341 void usb_stor_invoke_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
346 /* pr_info("transport --- usb_stor_invoke_transport\n"); */
347 usb_stor_print_cmd(us
, srb
);
348 /* send the command to the transport layer */
349 scsi_set_resid(srb
, 0);
350 result
= us
->transport(srb
, us
); /* usb_stor_Bulk_transport; */
352 /* if the command gets aborted by the higher layers,
353 we need to short-circuit all other processing */
354 if (test_bit(US_FLIDX_TIMED_OUT
, &us
->dflags
)) {
355 /* pr_info("-- command was aborted\n"); */
356 srb
->result
= DID_ABORT
<< 16;
360 /* if there is a transport error, reset and don't auto-sense */
361 if (result
== USB_STOR_TRANSPORT_ERROR
) {
362 /* pr_info("-- transport indicates error, resetting\n"); */
363 srb
->result
= DID_ERROR
<< 16;
367 /* if the transport provided its own sense data, don't auto-sense */
368 if (result
== USB_STOR_TRANSPORT_NO_SENSE
) {
369 srb
->result
= SAM_STAT_CHECK_CONDITION
;
373 srb
->result
= SAM_STAT_GOOD
;
375 /* Determine if we need to auto-sense */
378 if ((us
->protocol
== USB_PR_CB
|| us
->protocol
== USB_PR_DPCM_USB
) &&
379 srb
->sc_data_direction
!= DMA_FROM_DEVICE
) {
380 /* pr_info("-- CB transport device requiring auto-sense\n"); */
384 if (result
== USB_STOR_TRANSPORT_FAILED
) {
385 /* pr_info("-- transport indicates command failure\n"); */
389 /* Now, if we need to do the auto-sense, let's do it */
390 if (need_auto_sense
) {
392 struct scsi_eh_save ses
;
394 pr_info("Issuing auto-REQUEST_SENSE\n");
396 scsi_eh_prep_cmnd(srb
, &ses
, NULL
, 0, US_SENSE_SIZE
);
398 /* we must do the protocol translation here */
399 if (us
->subclass
== USB_SC_RBC
||
400 us
->subclass
== USB_SC_SCSI
||
401 us
->subclass
== USB_SC_CYP_ATACB
) {
406 /* issue the auto-sense command */
407 scsi_set_resid(srb
, 0);
408 temp_result
= us
->transport(us
->srb
, us
);
410 /* let's clean up right away */
411 scsi_eh_restore_cmnd(srb
, &ses
);
413 if (test_bit(US_FLIDX_TIMED_OUT
, &us
->dflags
)) {
414 /* pr_info("-- auto-sense aborted\n"); */
415 srb
->result
= DID_ABORT
<< 16;
418 if (temp_result
!= USB_STOR_TRANSPORT_GOOD
) {
419 /* pr_info("-- auto-sense failure\n"); */
420 srb
->result
= DID_ERROR
<< 16;
421 if (!(us
->fflags
& US_FL_SCM_MULT_TARG
))
426 /* set the result so the higher layers expect this data */
427 srb
->result
= SAM_STAT_CHECK_CONDITION
;
429 if (result
== USB_STOR_TRANSPORT_GOOD
&&
430 (srb
->sense_buffer
[2] & 0xaf) == 0 &&
431 srb
->sense_buffer
[12] == 0 &&
432 srb
->sense_buffer
[13] == 0) {
433 srb
->result
= SAM_STAT_GOOD
;
434 srb
->sense_buffer
[0] = 0x0;
438 /* Did we transfer less than the minimum amount required? */
439 if (srb
->result
== SAM_STAT_GOOD
&& scsi_bufflen(srb
) -
440 scsi_get_resid(srb
) < srb
->underflow
)
441 srb
->result
= (DID_ERROR
<< 16);
442 /* v02 | (SUGGEST_RETRY << 24); */
447 scsi_lock(us_to_host(us
));
448 set_bit(US_FLIDX_RESETTING
, &us
->dflags
);
449 clear_bit(US_FLIDX_ABORTING
, &us
->dflags
);
450 scsi_unlock(us_to_host(us
));
452 mutex_unlock(&us
->dev_mutex
);
453 result
= usb_stor_port_reset(us
);
454 mutex_lock(&us
->dev_mutex
);
457 scsi_lock(us_to_host(us
));
458 usb_stor_report_device_reset(us
);
459 scsi_unlock(us_to_host(us
));
460 us
->transport_reset(us
);
462 clear_bit(US_FLIDX_RESETTING
, &us
->dflags
);
466 * ENE_stor_invoke_transport()
468 void ENE_stor_invoke_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
472 /* pr_info("transport --- ENE_stor_invoke_transport\n"); */
473 usb_stor_print_cmd(us
, srb
);
474 /* send the command to the transport layer */
475 scsi_set_resid(srb
, 0);
476 if (!(us
->SM_Status
.Ready
))
477 result
= ENE_InitMedia(us
);
479 if (us
->Power_IsResum
== true) {
480 result
= ENE_InitMedia(us
);
481 us
->Power_IsResum
= false;
484 if (us
->SM_Status
.Ready
)
485 result
= SM_SCSIIrp(us
, srb
);
487 /* if the command gets aborted by the higher layers,
488 we need to short-circuit all other processing */
489 if (test_bit(US_FLIDX_TIMED_OUT
, &us
->dflags
)) {
490 /* pr_info("-- command was aborted\n"); */
491 srb
->result
= DID_ABORT
<< 16;
495 /* if there is a transport error, reset and don't auto-sense */
496 if (result
== USB_STOR_TRANSPORT_ERROR
) {
497 /* pr_info("-- transport indicates error, resetting\n"); */
498 srb
->result
= DID_ERROR
<< 16;
502 /* if the transport provided its own sense data, don't auto-sense */
503 if (result
== USB_STOR_TRANSPORT_NO_SENSE
) {
504 srb
->result
= SAM_STAT_CHECK_CONDITION
;
508 srb
->result
= SAM_STAT_GOOD
;
509 if (result
== USB_STOR_TRANSPORT_FAILED
) {
510 /* pr_info("-- transport indicates command failure\n"); */
511 /* need_auto_sense = 1; */
512 BuildSenseBuffer(srb
, us
->SrbStatus
);
513 srb
->result
= SAM_STAT_CHECK_CONDITION
;
516 /* Did we transfer less than the minimum amount required? */
517 if (srb
->result
== SAM_STAT_GOOD
&& scsi_bufflen(srb
) -
518 scsi_get_resid(srb
) < srb
->underflow
)
519 srb
->result
= (DID_ERROR
<< 16);
520 /* v02 | (SUGGEST_RETRY << 24); */
525 scsi_lock(us_to_host(us
));
526 set_bit(US_FLIDX_RESETTING
, &us
->dflags
);
527 clear_bit(US_FLIDX_ABORTING
, &us
->dflags
);
528 scsi_unlock(us_to_host(us
));
530 mutex_unlock(&us
->dev_mutex
);
531 result
= usb_stor_port_reset(us
);
532 mutex_lock(&us
->dev_mutex
);
535 scsi_lock(us_to_host(us
));
536 usb_stor_report_device_reset(us
);
537 scsi_unlock(us_to_host(us
));
538 us
->transport_reset(us
);
540 clear_bit(US_FLIDX_RESETTING
, &us
->dflags
);
546 void BuildSenseBuffer(struct scsi_cmnd
*srb
, int SrbStatus
)
548 u8
*buf
= srb
->sense_buffer
;
551 pr_info("transport --- BuildSenseBuffer\n");
555 break; /* sense key = 0x02 */
558 break; /* sense key = 0x03 */
559 case SS_ILLEGAL_REQUEST
:
561 break; /* sense key = 0x05 */
569 buf
[0x02] = SrbStatus
;
575 * usb_stor_stop_transport()
577 void usb_stor_stop_transport(struct us_data
*us
)
579 /* pr_info("transport --- usb_stor_stop_transport\n"); */
581 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE
, &us
->dflags
)) {
582 /* pr_info("-- cancelling URB\n"); */
583 usb_unlink_urb(us
->current_urb
);
586 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE
, &us
->dflags
)) {
587 /* pr_info("-- cancelling sg request\n"); */
588 usb_sg_cancel(&us
->current_sg
);
593 * usb_stor_Bulk_max_lun()
595 int usb_stor_Bulk_max_lun(struct us_data
*us
)
599 /* pr_info("transport --- usb_stor_Bulk_max_lun\n"); */
600 /* issue the command */
602 result
= usb_stor_control_msg(us
, us
->recv_ctrl_pipe
,
604 USB_DIR_IN
| USB_TYPE_CLASS
|
606 0, us
->ifnum
, us
->iobuf
, 1, HZ
);
608 /* pr_info("GetMaxLUN command result is %d, data is %d\n",
609 result, us->iobuf[0]); */
611 /* if we have a successful request, return the result */
619 * usb_stor_Bulk_transport()
621 int usb_stor_Bulk_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
623 struct bulk_cb_wrap
*bcb
= (struct bulk_cb_wrap
*) us
->iobuf
;
624 struct bulk_cs_wrap
*bcs
= (struct bulk_cs_wrap
*) us
->iobuf
;
625 unsigned int transfer_length
= scsi_bufflen(srb
);
626 unsigned int residue
;
630 unsigned int cbwlen
= US_BULK_CB_WRAP_LEN
;
632 /* pr_info("transport --- usb_stor_Bulk_transport\n"); */
633 /* Take care of BULK32 devices; set extra byte to 0 */
634 if (unlikely(us
->fflags
& US_FL_BULK32
)) {
639 /* set up the command wrapper */
640 bcb
->Signature
= cpu_to_le32(US_BULK_CB_SIGN
);
641 bcb
->DataTransferLength
= cpu_to_le32(transfer_length
);
642 bcb
->Flags
= srb
->sc_data_direction
== DMA_FROM_DEVICE
? 1 << 7 : 0;
643 bcb
->Tag
= ++us
->tag
;
644 bcb
->Lun
= srb
->device
->lun
;
645 if (us
->fflags
& US_FL_SCM_MULT_TARG
)
646 bcb
->Lun
|= srb
->device
->id
<< 4;
647 bcb
->Length
= srb
->cmd_len
;
649 /* copy the command payload */
650 memset(bcb
->CDB
, 0, sizeof(bcb
->CDB
));
651 memcpy(bcb
->CDB
, srb
->cmnd
, bcb
->Length
);
654 /* send it to out endpoint */
655 /* pr_info("Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n",
656 le32_to_cpu(bcb->Signature), bcb->Tag,
657 le32_to_cpu(bcb->DataTransferLength), bcb->Flags,
658 (bcb->Lun >> 4), (bcb->Lun & 0x0F),
660 result
= usb_stor_bulk_transfer_buf(us
, us
->send_bulk_pipe
,
662 /* pr_info("Bulk command transfer result=%d\n", result); */
663 if (result
!= USB_STOR_XFER_GOOD
)
664 return USB_STOR_TRANSPORT_ERROR
;
666 if (unlikely(us
->fflags
& US_FL_GO_SLOW
))
670 if (transfer_length
) {
672 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
)
673 pipe
= us
->recv_bulk_pipe
;
675 pipe
= us
->send_bulk_pipe
;
677 result
= usb_stor_bulk_srb(us
, pipe
, srb
);
678 /* pr_info("Bulk data transfer result 0x%x\n", result); */
679 if (result
== USB_STOR_XFER_ERROR
)
680 return USB_STOR_TRANSPORT_ERROR
;
682 if (result
== USB_STOR_XFER_LONG
)
686 /* get CSW for device status */
687 /* pr_info("Attempting to get CSW...\n"); */
688 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
,
689 US_BULK_CS_WRAP_LEN
, &cswlen
);
691 if (result
== USB_STOR_XFER_SHORT
&& cswlen
== 0) {
692 /* pr_info("Received 0-length CSW; retrying...\n"); */
693 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
,
694 US_BULK_CS_WRAP_LEN
, &cswlen
);
697 /* did the attempt to read the CSW fail? */
698 if (result
== USB_STOR_XFER_STALLED
) {
699 /* get the status again */
700 /* pr_info("Attempting to get CSW (2nd try)...\n"); */
701 result
= usb_stor_bulk_transfer_buf(us
, us
->recv_bulk_pipe
, bcs
,
702 US_BULK_CS_WRAP_LEN
, NULL
);
705 /* if we still have a failure at this point, we're in trouble */
706 /* pr_info("Bulk status result = %d\n", result); */
707 if (result
!= USB_STOR_XFER_GOOD
)
708 return USB_STOR_TRANSPORT_ERROR
;
710 /* check bulk status */
711 residue
= le32_to_cpu(bcs
->Residue
);
712 /* pr_info("Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
713 le32_to_cpu(bcs->Signature),
714 bcs->Tag, residue, bcs->Status); */
715 if (!(bcs
->Tag
== us
->tag
||
716 (us
->fflags
& US_FL_BULK_IGNORE_TAG
)) ||
717 bcs
->Status
> US_BULK_STAT_PHASE
) {
718 /* pr_info("Bulk logical error\n"); */
719 return USB_STOR_TRANSPORT_ERROR
;
722 if (!us
->bcs_signature
) {
723 us
->bcs_signature
= bcs
->Signature
;
724 /* if (us->bcs_signature != cpu_to_le32(US_BULK_CS_SIGN)) */
725 /* pr_info("Learnt BCS signature 0x%08X\n",
726 le32_to_cpu(us->bcs_signature)); */
727 } else if (bcs
->Signature
!= us
->bcs_signature
) {
728 /* pr_info("Signature mismatch: got %08X, expecting %08X\n",
729 le32_to_cpu(bcs->Signature),
730 le32_to_cpu(us->bcs_signature)); */
731 return USB_STOR_TRANSPORT_ERROR
;
734 /* try to compute the actual residue, based on how much data
735 * was really transferred and what the device tells us */
736 if (residue
&& !(us
->fflags
& US_FL_IGNORE_RESIDUE
)) {
738 /* Heuristically detect devices that generate bogus residues
739 * by seeing what happens with INQUIRY and READ CAPACITY
742 if (bcs
->Status
== US_BULK_STAT_OK
&&
743 scsi_get_resid(srb
) == 0 &&
744 ((srb
->cmnd
[0] == INQUIRY
&&
745 transfer_length
== 36) ||
746 (srb
->cmnd
[0] == READ_CAPACITY
&&
747 transfer_length
== 8))) {
748 us
->fflags
|= US_FL_IGNORE_RESIDUE
;
751 residue
= min(residue
, transfer_length
);
752 scsi_set_resid(srb
, max_t(int, scsi_get_resid(srb
),
757 /* based on the status code, we report good or bad */
758 switch (bcs
->Status
) {
759 case US_BULK_STAT_OK
:
761 memcpy(srb
->sense_buffer
, usb_stor_sense_invalidCDB
,
762 sizeof(usb_stor_sense_invalidCDB
));
763 return USB_STOR_TRANSPORT_NO_SENSE
;
765 return USB_STOR_TRANSPORT_GOOD
;
767 case US_BULK_STAT_FAIL
:
768 return USB_STOR_TRANSPORT_FAILED
;
770 case US_BULK_STAT_PHASE
:
771 return USB_STOR_TRANSPORT_ERROR
;
773 return USB_STOR_TRANSPORT_ERROR
;
776 /***********************************************************************
778 ***********************************************************************/
780 * usb_stor_reset_common()
782 static int usb_stor_reset_common(struct us_data
*us
,
783 u8 request
, u8 requesttype
,
784 u16 value
, u16 index
, void *data
, u16 size
)
789 /* pr_info("transport --- usb_stor_reset_common\n"); */
790 if (test_bit(US_FLIDX_DISCONNECTING
, &us
->dflags
)) {
791 /* pr_info("No reset during disconnect\n"); */
795 result
= usb_stor_control_msg(us
, us
->send_ctrl_pipe
,
796 request
, requesttype
, value
, index
, data
, size
, 5*HZ
);
799 /* pr_info("Soft reset failed: %d\n", result); */
803 wait_event_interruptible_timeout(us
->delay_wait
,
804 test_bit(US_FLIDX_DISCONNECTING
, &us
->dflags
), HZ
*6);
806 if (test_bit(US_FLIDX_DISCONNECTING
, &us
->dflags
)) {
807 /* pr_info("Reset interrupted by disconnect\n"); */
811 /* pr_info("Soft reset: clearing bulk-in endpoint halt\n"); */
812 result
= usb_stor_clear_halt(us
, us
->recv_bulk_pipe
);
814 /* pr_info("Soft reset: clearing bulk-out endpoint halt\n"); */
815 result2
= usb_stor_clear_halt(us
, us
->send_bulk_pipe
);
817 /* return a result code based on the result of the clear-halts */
820 /* if (result < 0) */
821 /* pr_info("Soft reset failed\n"); */
823 /* pr_info("Soft reset done\n"); */
828 * usb_stor_Bulk_reset()
830 int usb_stor_Bulk_reset(struct us_data
*us
)
832 /* pr_info("transport --- usb_stor_Bulk_reset\n"); */
833 return usb_stor_reset_common(us
, US_BULK_RESET_REQUEST
,
834 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
835 0, us
->ifnum
, NULL
, 0);
839 * usb_stor_port_reset()
841 int usb_stor_port_reset(struct us_data
*us
)
845 /* pr_info("transport --- usb_stor_port_reset\n"); */
846 result
= usb_lock_device_for_reset(us
->pusb_dev
, us
->pusb_intf
);
848 pr_info("unable to lock device for reset: %d\n", result
);
850 /* Were we disconnected while waiting for the lock? */
851 if (test_bit(US_FLIDX_DISCONNECTING
, &us
->dflags
)) {
853 /* pr_info("No reset during disconnect\n"); */
855 result
= usb_reset_device(us
->pusb_dev
);
856 /* pr_info("usb_reset_composite_device returns %d\n",
859 usb_unlock_device(us
->pusb_dev
);