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)
86 PBYTE Cdb = srb->cmnd;
88 DWORD bn = ((Cdb[2] << 24) & 0xff000000) |
89 ((Cdb[3] << 16) & 0x00ff0000) |
90 ((Cdb[4] << 8) & 0x0000ff00) |
91 ((Cdb[5] << 0) & 0x000000ff);
92 WORD blen = ((Cdb[7] << 8) & 0xff00) | ((Cdb[8] << 0) & 0x00ff);
96 /* dev_dbg(&us->pusb_dev->dev,
97 "scsi cmd %X --- SCSIOP_TEST_UNIT_READY\n", cmd); */
100 dev_dbg(&us->pusb_dev->dev,
101 "scsi cmd %X --- SCSIOP_INQUIRY\n", cmd);
104 dev_dbg(&us->pusb_dev->dev,
105 "scsi cmd %X --- SCSIOP_MODE_SENSE\n", cmd);
108 dev_dbg(&us->pusb_dev->dev,
109 "scsi cmd %X --- SCSIOP_START_STOP\n", cmd);
112 dev_dbg(&us->pusb_dev->dev,
113 "scsi cmd %X --- SCSIOP_READ_CAPACITY\n", cmd);
116 /* dev_dbg(&us->pusb_dev->dev,
117 "scsi cmd %X --- SCSIOP_READ,bn = %X, blen = %X\n"
121 /* dev_dbg(&us->pusb_dev->dev,
122 "scsi cmd %X --- SCSIOP_WRITE, bn = %X, blen = %X\n",
125 case ALLOW_MEDIUM_REMOVAL:
126 dev_dbg(&us->pusb_dev->dev,
127 "scsi cmd %X --- SCSIOP_ALLOW_MEDIUM_REMOVAL\n", cmd);
130 dev_dbg(&us->pusb_dev->dev, "scsi cmd %X --- Other cmd\n", cmd);
137 * usb_stor_control_msg()
139 int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
140 u8 request, u8 requesttype, u16 value, u16 index,
141 void *data, u16 size, int timeout)
145 /* pr_info("transport --- usb_stor_control_msg\n"); */
147 /* fill in the devrequest structure */
148 us->cr->bRequestType = requesttype;
149 us->cr->bRequest = request;
150 us->cr->wValue = cpu_to_le16(value);
151 us->cr->wIndex = cpu_to_le16(index);
152 us->cr->wLength = cpu_to_le16(size);
154 /* fill and submit the URB */
155 usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
156 (unsigned char *) us->cr, data, size,
157 usb_stor_blocking_completion, NULL);
158 status = usb_stor_msg_common(us, timeout);
160 /* return the actual length of the data transferred if no error */
162 status = us->current_urb->actual_length;
167 * usb_stor_clear_halt()
169 int usb_stor_clear_halt(struct us_data *us, unsigned int pipe)
172 int endp = usb_pipeendpoint(pipe);
174 /* pr_info("transport --- usb_stor_clear_halt\n"); */
175 if (usb_pipein(pipe))
178 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
179 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
180 USB_ENDPOINT_HALT, endp,
183 /* reset the endpoint toggle */
185 /* usb_settoggle(us->pusb_dev, usb_pipeendpoint(pipe),
186 usb_pipeout(pipe), 0); */
187 usb_reset_endpoint(us->pusb_dev, endp);
193 * interpret_urb_result()
195 static int interpret_urb_result(struct us_data *us, unsigned int pipe,
196 unsigned int length, int result, unsigned int partial)
198 /* pr_info("transport --- interpret_urb_result\n"); */
200 /* no error code; did we send all the data? */
202 if (partial != length) {
203 /* pr_info("-- short transfer\n"); */
204 return USB_STOR_XFER_SHORT;
206 /* pr_info("-- transfer complete\n"); */
207 return USB_STOR_XFER_GOOD;
209 if (usb_pipecontrol(pipe)) {
210 /* pr_info("-- stall on control pipe\n"); */
211 return USB_STOR_XFER_STALLED;
213 /* pr_info("clearing endpoint halt for pipe 0x%x\n", pipe); */
214 if (usb_stor_clear_halt(us, pipe) < 0)
215 return USB_STOR_XFER_ERROR;
216 return USB_STOR_XFER_STALLED;
218 /* pr_info("-- babble\n"); */
219 return USB_STOR_XFER_LONG;
221 /* pr_info("-- transfer cancelled\n"); */
222 return USB_STOR_XFER_ERROR;
224 /* pr_info("-- short read transfer\n"); */
225 return USB_STOR_XFER_SHORT;
227 /* pr_info("-- abort or disconnect in progress\n"); */
228 return USB_STOR_XFER_ERROR;
230 /* pr_info("-- unknown error\n"); */
231 return USB_STOR_XFER_ERROR;
236 * usb_stor_bulk_transfer_buf()
238 int usb_stor_bulk_transfer_buf(struct us_data *us, unsigned int pipe,
239 void *buf, unsigned int length, unsigned int *act_len)
243 /* pr_info("transport --- usb_stor_bulk_transfer_buf\n"); */
245 /* fill and submit the URB */
246 usb_fill_bulk_urb(us->current_urb, us->pusb_dev, pipe, buf,
247 length, usb_stor_blocking_completion, NULL);
248 result = usb_stor_msg_common(us, 0);
250 /* store the actual length of the data transferred */
252 *act_len = us->current_urb->actual_length;
254 return interpret_urb_result(us, pipe, length, result,
255 us->current_urb->actual_length);
259 * usb_stor_bulk_transfer_sglist()
261 static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
262 struct scatterlist *sg, int num_sg, unsigned int length,
263 unsigned int *act_len)
267 /* pr_info("transport --- usb_stor_bulk_transfer_sglist\n"); */
268 if (test_bit(US_FLIDX_ABORTING, &us->dflags))
269 return USB_STOR_XFER_ERROR;
271 /* initialize the scatter-gather request block */
272 result = usb_sg_init(&us->current_sg, us->pusb_dev, pipe, 0,
273 sg, num_sg, length, GFP_NOIO);
275 /* pr_info("usb_sg_init returned %d\n", result); */
276 return USB_STOR_XFER_ERROR;
279 /* since the block has been initialized successfully,
280 it's now okay to cancel it */
281 set_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
283 /* did an abort/disconnect occur during the submission? */
284 if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
285 /* cancel the request, if it hasn't been cancelled already */
286 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
287 /* pr_info("-- cancelling sg request\n"); */
288 usb_sg_cancel(&us->current_sg);
292 /* wait for the completion of the transfer */
293 usb_sg_wait(&us->current_sg);
294 clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
296 result = us->current_sg.status;
298 *act_len = us->current_sg.bytes;
300 return interpret_urb_result(us, pipe, length,
301 result, us->current_sg.bytes);
305 * usb_stor_bulk_srb()
307 int usb_stor_bulk_srb(struct us_data *us, unsigned int pipe,
308 struct scsi_cmnd *srb)
310 unsigned int partial;
311 int result = usb_stor_bulk_transfer_sglist(us, pipe, scsi_sglist(srb),
312 scsi_sg_count(srb), scsi_bufflen(srb),
315 scsi_set_resid(srb, scsi_bufflen(srb) - partial);
320 * usb_stor_bulk_transfer_sg()
322 int usb_stor_bulk_transfer_sg(struct us_data *us, unsigned int pipe,
323 void *buf, unsigned int length_left, int use_sg, int *residual)
326 unsigned int partial;
328 /* pr_info("transport --- usb_stor_bulk_transfer_sg\n"); */
329 /* are we scatter-gathering? */
331 /* use the usb core scatter-gather primitives */
332 result = usb_stor_bulk_transfer_sglist(us, pipe,
333 (struct scatterlist *) buf, use_sg,
334 length_left, &partial);
335 length_left -= partial;
337 /* no scatter-gather, just make the request */
338 result = usb_stor_bulk_transfer_buf(us, pipe, buf,
339 length_left, &partial);
340 length_left -= partial;
343 /* store the residual and return the error code */
345 *residual = length_left;
349 /***********************************************************************
351 ***********************************************************************/
353 * usb_stor_invoke_transport()
355 void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
360 /* pr_info("transport --- usb_stor_invoke_transport\n"); */
361 usb_stor_print_cmd(us, srb);
362 /* send the command to the transport layer */
363 scsi_set_resid(srb, 0);
364 result = us->transport(srb, us); /* usb_stor_Bulk_transport; */
366 /* if the command gets aborted by the higher layers,
367 we need to short-circuit all other processing */
368 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
369 /* pr_info("-- command was aborted\n"); */
370 srb->result = DID_ABORT << 16;
374 /* if there is a transport error, reset and don't auto-sense */
375 if (result == USB_STOR_TRANSPORT_ERROR) {
376 /* pr_info("-- transport indicates error, resetting\n"); */
377 srb->result = DID_ERROR << 16;
381 /* if the transport provided its own sense data, don't auto-sense */
382 if (result == USB_STOR_TRANSPORT_NO_SENSE) {
383 srb->result = SAM_STAT_CHECK_CONDITION;
387 srb->result = SAM_STAT_GOOD;
389 /* Determine if we need to auto-sense */
392 if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_DPCM_USB) &&
393 srb->sc_data_direction != DMA_FROM_DEVICE) {
394 /* pr_info("-- CB transport device requiring auto-sense\n"); */
398 if (result == USB_STOR_TRANSPORT_FAILED) {
399 /* pr_info("-- transport indicates command failure\n"); */
403 /* Now, if we need to do the auto-sense, let's do it */
404 if (need_auto_sense) {
406 struct scsi_eh_save ses;
408 pr_info("Issuing auto-REQUEST_SENSE\n");
410 scsi_eh_prep_cmnd(srb, &ses, NULL, 0, US_SENSE_SIZE);
412 /* we must do the protocol translation here */
413 if (us->subclass == USB_SC_RBC ||
414 us->subclass == USB_SC_SCSI ||
415 us->subclass == USB_SC_CYP_ATACB) {
420 /* issue the auto-sense command */
421 scsi_set_resid(srb, 0);
422 temp_result = us->transport(us->srb, us);
424 /* let's clean up right away */
425 scsi_eh_restore_cmnd(srb, &ses);
427 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
428 /* pr_info("-- auto-sense aborted\n"); */
429 srb->result = DID_ABORT << 16;
432 if (temp_result != USB_STOR_TRANSPORT_GOOD) {
433 /* pr_info("-- auto-sense failure\n"); */
434 srb->result = DID_ERROR << 16;
435 if (!(us->fflags & US_FL_SCM_MULT_TARG))
440 /* set the result so the higher layers expect this data */
441 srb->result = SAM_STAT_CHECK_CONDITION;
443 if (result == USB_STOR_TRANSPORT_GOOD &&
444 (srb->sense_buffer[2] & 0xaf) == 0 &&
445 srb->sense_buffer[12] == 0 &&
446 srb->sense_buffer[13] == 0) {
447 srb->result = SAM_STAT_GOOD;
448 srb->sense_buffer[0] = 0x0;
452 /* Did we transfer less than the minimum amount required? */
453 if (srb->result == SAM_STAT_GOOD && scsi_bufflen(srb) -
454 scsi_get_resid(srb) < srb->underflow)
455 srb->result = (DID_ERROR << 16);
456 /* v02 | (SUGGEST_RETRY << 24); */
461 scsi_lock(us_to_host(us));
462 set_bit(US_FLIDX_RESETTING, &us->dflags);
463 clear_bit(US_FLIDX_ABORTING, &us->dflags);
464 scsi_unlock(us_to_host(us));
466 mutex_unlock(&us->dev_mutex);
467 result = usb_stor_port_reset(us);
468 mutex_lock(&us->dev_mutex);
471 scsi_lock(us_to_host(us));
472 usb_stor_report_device_reset(us);
473 scsi_unlock(us_to_host(us));
474 us->transport_reset(us);
476 clear_bit(US_FLIDX_RESETTING, &us->dflags);
480 * ENE_stor_invoke_transport()
482 void ENE_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
486 /* pr_info("transport --- ENE_stor_invoke_transport\n"); */
487 usb_stor_print_cmd(us, srb);
488 /* send the command to the transport layer */
489 scsi_set_resid(srb, 0);
490 if (!(us->SM_Status.Ready))
491 result = ENE_InitMedia(us);
493 if (us->Power_IsResum == true) {
494 result = ENE_InitMedia(us);
495 us->Power_IsResum = false;
498 if (us->SM_Status.Ready)
499 result = SM_SCSIIrp(us, srb);
501 /* if the command gets aborted by the higher layers,
502 we need to short-circuit all other processing */
503 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
504 /* pr_info("-- command was aborted\n"); */
505 srb->result = DID_ABORT << 16;
509 /* if there is a transport error, reset and don't auto-sense */
510 if (result == USB_STOR_TRANSPORT_ERROR) {
511 /* pr_info("-- transport indicates error, resetting\n"); */
512 srb->result = DID_ERROR << 16;
516 /* if the transport provided its own sense data, don't auto-sense */
517 if (result == USB_STOR_TRANSPORT_NO_SENSE) {
518 srb->result = SAM_STAT_CHECK_CONDITION;
522 srb->result = SAM_STAT_GOOD;
523 if (result == USB_STOR_TRANSPORT_FAILED) {
524 /* pr_info("-- transport indicates command failure\n"); */
525 /* need_auto_sense = 1; */
526 BuildSenseBuffer(srb, us->SrbStatus);
527 srb->result = SAM_STAT_CHECK_CONDITION;
530 /* Did we transfer less than the minimum amount required? */
531 if (srb->result == SAM_STAT_GOOD && scsi_bufflen(srb) -
532 scsi_get_resid(srb) < srb->underflow)
533 srb->result = (DID_ERROR << 16);
534 /* v02 | (SUGGEST_RETRY << 24); */
539 scsi_lock(us_to_host(us));
540 set_bit(US_FLIDX_RESETTING, &us->dflags);
541 clear_bit(US_FLIDX_ABORTING, &us->dflags);
542 scsi_unlock(us_to_host(us));
544 mutex_unlock(&us->dev_mutex);
545 result = usb_stor_port_reset(us);
546 mutex_lock(&us->dev_mutex);
549 scsi_lock(us_to_host(us));
550 usb_stor_report_device_reset(us);
551 scsi_unlock(us_to_host(us));
552 us->transport_reset(us);
554 clear_bit(US_FLIDX_RESETTING, &us->dflags);
560 void BuildSenseBuffer(struct scsi_cmnd *srb, int SrbStatus)
562 BYTE *buf = srb->sense_buffer;
565 pr_info("transport --- BuildSenseBuffer\n");
569 break; /* sense key = 0x02 */
572 break; /* sense key = 0x03 */
573 case SS_ILLEGAL_REQUEST:
575 break; /* sense key = 0x05 */
583 buf[0x02] = SrbStatus;
589 * usb_stor_stop_transport()
591 void usb_stor_stop_transport(struct us_data *us)
593 /* pr_info("transport --- usb_stor_stop_transport\n"); */
595 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
596 /* pr_info("-- cancelling URB\n"); */
597 usb_unlink_urb(us->current_urb);
600 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
601 /* pr_info("-- cancelling sg request\n"); */
602 usb_sg_cancel(&us->current_sg);
607 * usb_stor_Bulk_max_lun()
609 int usb_stor_Bulk_max_lun(struct us_data *us)
613 /* pr_info("transport --- usb_stor_Bulk_max_lun\n"); */
614 /* issue the command */
616 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
618 USB_DIR_IN | USB_TYPE_CLASS |
620 0, us->ifnum, us->iobuf, 1, HZ);
622 /* pr_info("GetMaxLUN command result is %d, data is %d\n",
623 result, us->iobuf[0]); */
625 /* if we have a successful request, return the result */
633 * usb_stor_Bulk_transport()
635 int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
637 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
638 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
639 unsigned int transfer_length = scsi_bufflen(srb);
640 unsigned int residue;
644 unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
646 /* pr_info("transport --- usb_stor_Bulk_transport\n"); */
647 /* Take care of BULK32 devices; set extra byte to 0 */
648 if (unlikely(us->fflags & US_FL_BULK32)) {
653 /* set up the command wrapper */
654 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
655 bcb->DataTransferLength = cpu_to_le32(transfer_length);
656 bcb->Flags = srb->sc_data_direction == DMA_FROM_DEVICE ? 1 << 7 : 0;
657 bcb->Tag = ++us->tag;
658 bcb->Lun = srb->device->lun;
659 if (us->fflags & US_FL_SCM_MULT_TARG)
660 bcb->Lun |= srb->device->id << 4;
661 bcb->Length = srb->cmd_len;
663 /* copy the command payload */
664 memset(bcb->CDB, 0, sizeof(bcb->CDB));
665 memcpy(bcb->CDB, srb->cmnd, bcb->Length);
668 /* send it to out endpoint */
669 /* pr_info("Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n",
670 le32_to_cpu(bcb->Signature), bcb->Tag,
671 le32_to_cpu(bcb->DataTransferLength), bcb->Flags,
672 (bcb->Lun >> 4), (bcb->Lun & 0x0F),
674 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
676 /* pr_info("Bulk command transfer result=%d\n", result); */
677 if (result != USB_STOR_XFER_GOOD)
678 return USB_STOR_TRANSPORT_ERROR;
680 if (unlikely(us->fflags & US_FL_GO_SLOW))
684 if (transfer_length) {
686 if (srb->sc_data_direction == DMA_FROM_DEVICE)
687 pipe = us->recv_bulk_pipe;
689 pipe = us->send_bulk_pipe;
691 result = usb_stor_bulk_srb(us, pipe, srb);
692 /* pr_info("Bulk data transfer result 0x%x\n", result); */
693 if (result == USB_STOR_XFER_ERROR)
694 return USB_STOR_TRANSPORT_ERROR;
696 if (result == USB_STOR_XFER_LONG)
700 /* get CSW for device status */
701 /* pr_info("Attempting to get CSW...\n"); */
702 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
703 US_BULK_CS_WRAP_LEN, &cswlen);
705 if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
706 /* pr_info("Received 0-length CSW; retrying...\n"); */
707 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
708 US_BULK_CS_WRAP_LEN, &cswlen);
711 /* did the attempt to read the CSW fail? */
712 if (result == USB_STOR_XFER_STALLED) {
713 /* get the status again */
714 /* pr_info("Attempting to get CSW (2nd try)...\n"); */
715 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
716 US_BULK_CS_WRAP_LEN, NULL);
719 /* if we still have a failure at this point, we're in trouble */
720 /* pr_info("Bulk status result = %d\n", result); */
721 if (result != USB_STOR_XFER_GOOD)
722 return USB_STOR_TRANSPORT_ERROR;
724 /* check bulk status */
725 residue = le32_to_cpu(bcs->Residue);
726 /* pr_info("Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
727 le32_to_cpu(bcs->Signature),
728 bcs->Tag, residue, bcs->Status); */
729 if (!(bcs->Tag == us->tag ||
730 (us->fflags & US_FL_BULK_IGNORE_TAG)) ||
731 bcs->Status > US_BULK_STAT_PHASE) {
732 /* pr_info("Bulk logical error\n"); */
733 return USB_STOR_TRANSPORT_ERROR;
736 if (!us->bcs_signature) {
737 us->bcs_signature = bcs->Signature;
738 /* if (us->bcs_signature != cpu_to_le32(US_BULK_CS_SIGN)) */
739 /* pr_info("Learnt BCS signature 0x%08X\n",
740 le32_to_cpu(us->bcs_signature)); */
741 } else if (bcs->Signature != us->bcs_signature) {
742 /* pr_info("Signature mismatch: got %08X, expecting %08X\n",
743 le32_to_cpu(bcs->Signature),
744 le32_to_cpu(us->bcs_signature)); */
745 return USB_STOR_TRANSPORT_ERROR;
748 /* try to compute the actual residue, based on how much data
749 * was really transferred and what the device tells us */
750 if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
752 /* Heuristically detect devices that generate bogus residues
753 * by seeing what happens with INQUIRY and READ CAPACITY
756 if (bcs->Status == US_BULK_STAT_OK &&
757 scsi_get_resid(srb) == 0 &&
758 ((srb->cmnd[0] == INQUIRY &&
759 transfer_length == 36) ||
760 (srb->cmnd[0] == READ_CAPACITY &&
761 transfer_length == 8))) {
762 us->fflags |= US_FL_IGNORE_RESIDUE;
765 residue = min(residue, transfer_length);
766 scsi_set_resid(srb, max_t(int, scsi_get_resid(srb),
771 /* based on the status code, we report good or bad */
772 switch (bcs->Status) {
773 case US_BULK_STAT_OK:
775 memcpy(srb->sense_buffer, usb_stor_sense_invalidCDB,
776 sizeof(usb_stor_sense_invalidCDB));
777 return USB_STOR_TRANSPORT_NO_SENSE;
779 return USB_STOR_TRANSPORT_GOOD;
781 case US_BULK_STAT_FAIL:
782 return USB_STOR_TRANSPORT_FAILED;
784 case US_BULK_STAT_PHASE:
785 return USB_STOR_TRANSPORT_ERROR;
787 return USB_STOR_TRANSPORT_ERROR;
790 /***********************************************************************
792 ***********************************************************************/
794 * usb_stor_reset_common()
796 static int usb_stor_reset_common(struct us_data *us,
797 u8 request, u8 requesttype,
798 u16 value, u16 index, void *data, u16 size)
803 /* pr_info("transport --- usb_stor_reset_common\n"); */
804 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
805 /* pr_info("No reset during disconnect\n"); */
809 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
810 request, requesttype, value, index, data, size, 5*HZ);
813 /* pr_info("Soft reset failed: %d\n", result); */
817 wait_event_interruptible_timeout(us->delay_wait,
818 test_bit(US_FLIDX_DISCONNECTING, &us->dflags), HZ*6);
820 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
821 /* pr_info("Reset interrupted by disconnect\n"); */
825 /* pr_info("Soft reset: clearing bulk-in endpoint halt\n"); */
826 result = usb_stor_clear_halt(us, us->recv_bulk_pipe);
828 /* pr_info("Soft reset: clearing bulk-out endpoint halt\n"); */
829 result2 = usb_stor_clear_halt(us, us->send_bulk_pipe);
831 /* return a result code based on the result of the clear-halts */
834 /* if (result < 0) */
835 /* pr_info("Soft reset failed\n"); */
837 /* pr_info("Soft reset done\n"); */
842 * usb_stor_Bulk_reset()
844 int usb_stor_Bulk_reset(struct us_data *us)
846 /* pr_info("transport --- usb_stor_Bulk_reset\n"); */
847 return usb_stor_reset_common(us, US_BULK_RESET_REQUEST,
848 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
849 0, us->ifnum, NULL, 0);
853 * usb_stor_port_reset()
855 int usb_stor_port_reset(struct us_data *us)
859 /* pr_info("transport --- usb_stor_port_reset\n"); */
860 result = usb_lock_device_for_reset(us->pusb_dev, us->pusb_intf);
862 pr_info("unable to lock device for reset: %d\n", result);
864 /* Were we disconnected while waiting for the lock? */
865 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
867 /* pr_info("No reset during disconnect\n"); */
869 result = usb_reset_device(us->pusb_dev);
870 /* pr_info("usb_reset_composite_device returns %d\n",
873 usb_unlock_device(us->pusb_dev);