]> Pileus Git - ~andy/linux/blob - drivers/staging/keucr/transport.c
Merge tag 'pm-3.13-rc3-fixup' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[~andy/linux] / drivers / staging / keucr / transport.c
1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2
3 #include <linux/sched.h>
4 #include <linux/errno.h>
5 #include <linux/slab.h>
6
7 #include <scsi/scsi.h>
8 #include <scsi/scsi_eh.h>
9 #include <scsi/scsi_device.h>
10
11 #include "usb.h"
12 #include "scsiglue.h"
13 #include "transport.h"
14
15 /***********************************************************************
16  * Data transfer routines
17  ***********************************************************************/
18 /*
19  * usb_stor_blocking_completion()
20  */
21 static void usb_stor_blocking_completion(struct urb *urb)
22 {
23         struct completion *urb_done_ptr = urb->context;
24
25         /* pr_info("transport --- usb_stor_blocking_completion\n"); */
26         complete(urb_done_ptr);
27 }
28
29 /*
30  * usb_stor_msg_common()
31  */
32 static int usb_stor_msg_common(struct us_data *us, int timeout)
33 {
34         struct completion urb_done;
35         long timeleft;
36         int status;
37
38         /* pr_info("transport --- usb_stor_msg_common\n"); */
39         if (test_bit(US_FLIDX_ABORTING, &us->dflags))
40                 return -EIO;
41
42         init_completion(&urb_done);
43
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;
48
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;
54
55         status = usb_submit_urb(us->current_urb, GFP_NOIO);
56         if (status)
57                 return status;
58
59         set_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
60
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);
65                 }
66         }
67
68         timeleft = wait_for_completion_interruptible_timeout(&urb_done,
69                                         timeout ? : MAX_SCHEDULE_TIMEOUT);
70         clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
71
72         if (timeleft <= 0) {
73                 /* pr_info("%s -- cancelling URB\n",
74                         timeleft == 0 ? "Timeout" : "Signal"); */
75                 usb_kill_urb(us->current_urb);
76         }
77
78         return us->current_urb->status;
79 }
80
81 /*
82  * usb_stor_print_cmd():
83  */
84 static void usb_stor_print_cmd(struct us_data *us, struct scsi_cmnd *srb)
85 {
86         PBYTE   Cdb = srb->cmnd;
87         DWORD   cmd = Cdb[0];
88
89         switch (cmd) {
90         case TEST_UNIT_READY:
91                 break;
92         case INQUIRY:
93                 dev_dbg(&us->pusb_dev->dev,
94                                 "scsi cmd %X --- SCSIOP_INQUIRY\n", cmd);
95                 break;
96         case MODE_SENSE:
97                 dev_dbg(&us->pusb_dev->dev,
98                                 "scsi cmd %X --- SCSIOP_MODE_SENSE\n", cmd);
99                 break;
100         case START_STOP:
101                 dev_dbg(&us->pusb_dev->dev,
102                                 "scsi cmd %X --- SCSIOP_START_STOP\n", cmd);
103                 break;
104         case READ_CAPACITY:
105                 dev_dbg(&us->pusb_dev->dev,
106                                 "scsi cmd %X --- SCSIOP_READ_CAPACITY\n", cmd);
107                 break;
108         case READ_10:
109                 break;
110         case WRITE_10:
111                 break;
112         case ALLOW_MEDIUM_REMOVAL:
113                 dev_dbg(&us->pusb_dev->dev,
114                         "scsi cmd %X --- SCSIOP_ALLOW_MEDIUM_REMOVAL\n", cmd);
115                 break;
116         default:
117                 dev_dbg(&us->pusb_dev->dev, "scsi cmd %X --- Other cmd\n", cmd);
118                 break;
119         }
120 }
121
122 /*
123  * usb_stor_control_msg()
124  */
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)
128 {
129         int status;
130
131         /* pr_info("transport --- usb_stor_control_msg\n"); */
132
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);
139
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);
145
146         /* return the actual length of the data transferred if no error */
147         if (status == 0)
148                 status = us->current_urb->actual_length;
149         return status;
150 }
151
152 /*
153  * usb_stor_clear_halt()
154  */
155 int usb_stor_clear_halt(struct us_data *us, unsigned int pipe)
156 {
157         int result;
158         int endp = usb_pipeendpoint(pipe);
159
160         /* pr_info("transport --- usb_stor_clear_halt\n"); */
161         if (usb_pipein(pipe))
162                 endp |= USB_DIR_IN;
163
164         result = usb_stor_control_msg(us, us->send_ctrl_pipe,
165                 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
166                 USB_ENDPOINT_HALT, endp,
167                 NULL, 0, 3*HZ);
168
169         /* reset the endpoint toggle */
170         if (result >= 0)
171                 /* usb_settoggle(us->pusb_dev, usb_pipeendpoint(pipe),
172                                                 usb_pipeout(pipe), 0); */
173                 usb_reset_endpoint(us->pusb_dev, endp);
174
175         return result;
176 }
177
178 /*
179  * interpret_urb_result()
180  */
181 static int interpret_urb_result(struct us_data *us, unsigned int pipe,
182                 unsigned int length, int result, unsigned int partial)
183 {
184         /* pr_info("transport --- interpret_urb_result\n"); */
185         switch (result) {
186         /* no error code; did we send all the data? */
187         case 0:
188                 if (partial != length) {
189                         /* pr_info("-- short transfer\n"); */
190                         return USB_STOR_XFER_SHORT;
191                 }
192                 /* pr_info("-- transfer complete\n"); */
193                 return USB_STOR_XFER_GOOD;
194         case -EPIPE:
195                 if (usb_pipecontrol(pipe)) {
196                         /* pr_info("-- stall on control pipe\n"); */
197                         return USB_STOR_XFER_STALLED;
198                 }
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;
203         case -EOVERFLOW:
204                 /* pr_info("-- babble\n"); */
205                 return USB_STOR_XFER_LONG;
206         case -ECONNRESET:
207                 /* pr_info("-- transfer cancelled\n"); */
208                 return USB_STOR_XFER_ERROR;
209         case -EREMOTEIO:
210                 /* pr_info("-- short read transfer\n"); */
211                 return USB_STOR_XFER_SHORT;
212         case -EIO:
213                 /* pr_info("-- abort or disconnect in progress\n"); */
214                 return USB_STOR_XFER_ERROR;
215         default:
216                 /* pr_info("-- unknown error\n"); */
217                 return USB_STOR_XFER_ERROR;
218         }
219 }
220
221 /*
222  * usb_stor_bulk_transfer_buf()
223  */
224 int usb_stor_bulk_transfer_buf(struct us_data *us, unsigned int pipe,
225         void *buf, unsigned int length, unsigned int *act_len)
226 {
227         int result;
228
229         /* pr_info("transport --- usb_stor_bulk_transfer_buf\n"); */
230
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);
235
236         /* store the actual length of the data transferred */
237         if (act_len)
238                 *act_len = us->current_urb->actual_length;
239
240         return interpret_urb_result(us, pipe, length, result,
241                                         us->current_urb->actual_length);
242 }
243
244 /*
245  * usb_stor_bulk_transfer_sglist()
246  */
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)
250 {
251         int result;
252
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;
256
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);
260         if (result) {
261                 /* pr_info("usb_sg_init returned %d\n", result); */
262                 return USB_STOR_XFER_ERROR;
263         }
264
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);
268
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);
275                 }
276         }
277
278         /* wait for the completion of the transfer */
279         usb_sg_wait(&us->current_sg);
280         clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
281
282         result = us->current_sg.status;
283         if (act_len)
284                 *act_len = us->current_sg.bytes;
285
286         return interpret_urb_result(us, pipe, length,
287                                         result, us->current_sg.bytes);
288 }
289
290 /*
291  * usb_stor_bulk_srb()
292  */
293 int usb_stor_bulk_srb(struct us_data *us, unsigned int pipe,
294                                         struct scsi_cmnd *srb)
295 {
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),
299                                       &partial);
300
301         scsi_set_resid(srb, scsi_bufflen(srb) - partial);
302         return result;
303 }
304
305 /*
306  * usb_stor_bulk_transfer_sg()
307  */
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)
310 {
311         int result;
312         unsigned int partial;
313
314         /* pr_info("transport --- usb_stor_bulk_transfer_sg\n"); */
315         /* are we scatter-gathering? */
316         if (use_sg) {
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;
322         } else {
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;
327         }
328
329         /* store the residual and return the error code */
330         if (residual)
331                 *residual = length_left;
332         return result;
333 }
334
335 /***********************************************************************
336  * Transport routines
337  ***********************************************************************/
338 /*
339  * usb_stor_invoke_transport()
340  */
341 void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
342 {
343         int need_auto_sense;
344         int result;
345
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; */
351
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;
357                 goto Handle_Errors;
358         }
359
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;
364                 goto Handle_Errors;
365         }
366
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;
370                 return;
371         }
372
373         srb->result = SAM_STAT_GOOD;
374
375         /* Determine if we need to auto-sense */
376         need_auto_sense = 0;
377
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"); */
381                 need_auto_sense = 1;
382         }
383
384         if (result == USB_STOR_TRANSPORT_FAILED) {
385                 /* pr_info("-- transport indicates command failure\n"); */
386                 need_auto_sense = 1;
387         }
388
389         /* Now, if we need to do the auto-sense, let's do it */
390         if (need_auto_sense) {
391                 int temp_result;
392                 struct scsi_eh_save ses;
393
394                 pr_info("Issuing auto-REQUEST_SENSE\n");
395
396                 scsi_eh_prep_cmnd(srb, &ses, NULL, 0, US_SENSE_SIZE);
397
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) {
402                         srb->cmd_len = 6;
403                 } else {
404                         srb->cmd_len = 12;
405                 }
406                 /* issue the auto-sense command */
407                 scsi_set_resid(srb, 0);
408                 temp_result = us->transport(us->srb, us);
409
410                 /* let's clean up right away */
411                 scsi_eh_restore_cmnd(srb, &ses);
412
413                 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
414                         /* pr_info("-- auto-sense aborted\n"); */
415                         srb->result = DID_ABORT << 16;
416                         goto Handle_Errors;
417                 }
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))
422                                 goto Handle_Errors;
423                         return;
424                 }
425
426                 /* set the result so the higher layers expect this data */
427                 srb->result = SAM_STAT_CHECK_CONDITION;
428
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;
435                 }
436         }
437
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); */
443
444         return;
445
446 Handle_Errors:
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));
451
452         mutex_unlock(&us->dev_mutex);
453         result = usb_stor_port_reset(us);
454         mutex_lock(&us->dev_mutex);
455
456         if (result < 0) {
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);
461         }
462         clear_bit(US_FLIDX_RESETTING, &us->dflags);
463 }
464
465 /*
466  * ENE_stor_invoke_transport()
467  */
468 void ENE_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
469 {
470         int result = 0;
471
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);
478
479         if (us->Power_IsResum == true) {
480                 result = ENE_InitMedia(us);
481                 us->Power_IsResum = false;
482         }
483
484         if (us->SM_Status.Ready)
485                 result = SM_SCSIIrp(us, srb);
486
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;
492                 goto Handle_Errors;
493         }
494
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;
499                 goto Handle_Errors;
500         }
501
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;
505                 return;
506         }
507
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;
514         }
515
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); */
521
522         return;
523
524 Handle_Errors:
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));
529
530         mutex_unlock(&us->dev_mutex);
531         result = usb_stor_port_reset(us);
532         mutex_lock(&us->dev_mutex);
533
534         if (result < 0) {
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);
539         }
540         clear_bit(US_FLIDX_RESETTING, &us->dflags);
541 }
542
543 /*
544  * BuildSenseBuffer()
545  */
546 void BuildSenseBuffer(struct scsi_cmnd *srb, int SrbStatus)
547 {
548         BYTE    *buf = srb->sense_buffer;
549         BYTE    asc;
550
551         pr_info("transport --- BuildSenseBuffer\n");
552         switch (SrbStatus) {
553         case SS_NOT_READY:
554                 asc = 0x3a;
555                 break;  /*  sense key = 0x02 */
556         case SS_MEDIUM_ERR:
557                 asc = 0x0c;
558                 break;  /*  sense key = 0x03 */
559         case SS_ILLEGAL_REQUEST:
560                 asc = 0x20;
561                 break;  /*  sense key = 0x05 */
562         default:
563                 asc = 0x00;
564                 break;  /*  ?? */
565         }
566
567         memset(buf, 0, 18);
568         buf[0x00] = 0xf0;
569         buf[0x02] = SrbStatus;
570         buf[0x07] = 0x0b;
571         buf[0x0c] = asc;
572 }
573
574 /*
575  * usb_stor_stop_transport()
576  */
577 void usb_stor_stop_transport(struct us_data *us)
578 {
579         /* pr_info("transport --- usb_stor_stop_transport\n"); */
580
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);
584         }
585
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);
589         }
590 }
591
592 /*
593  * usb_stor_Bulk_max_lun()
594  */
595 int usb_stor_Bulk_max_lun(struct us_data *us)
596 {
597         int result;
598
599         /* pr_info("transport --- usb_stor_Bulk_max_lun\n"); */
600         /* issue the command */
601         us->iobuf[0] = 0;
602         result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
603                                  US_BULK_GET_MAX_LUN,
604                                  USB_DIR_IN | USB_TYPE_CLASS |
605                                  USB_RECIP_INTERFACE,
606                                  0, us->ifnum, us->iobuf, 1, HZ);
607
608         /* pr_info("GetMaxLUN command result is %d, data is %d\n",
609                                                 result, us->iobuf[0]); */
610
611         /* if we have a successful request, return the result */
612         if (result > 0)
613                 return us->iobuf[0];
614
615         return 0;
616 }
617
618 /*
619  * usb_stor_Bulk_transport()
620  */
621 int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
622 {
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;
627         int result;
628         int fake_sense = 0;
629         unsigned int cswlen;
630         unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
631
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)) {
635                 cbwlen = 32;
636                 us->iobuf[31] = 0;
637         }
638
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;
648
649         /* copy the command payload */
650         memset(bcb->CDB, 0, sizeof(bcb->CDB));
651         memcpy(bcb->CDB, srb->cmnd, bcb->Length);
652
653         /*  send command */
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),
659                         bcb->Length); */
660         result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
661                                                 bcb, cbwlen, NULL);
662         /* pr_info("Bulk command transfer result=%d\n", result); */
663         if (result != USB_STOR_XFER_GOOD)
664                 return USB_STOR_TRANSPORT_ERROR;
665
666         if (unlikely(us->fflags & US_FL_GO_SLOW))
667                 udelay(125);
668
669         /*  R/W data */
670         if (transfer_length) {
671                 unsigned int pipe;
672                 if (srb->sc_data_direction == DMA_FROM_DEVICE)
673                         pipe = us->recv_bulk_pipe;
674                 else
675                         pipe = us->send_bulk_pipe;
676
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;
681
682                 if (result == USB_STOR_XFER_LONG)
683                         fake_sense = 1;
684         }
685
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);
690
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);
695         }
696
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);
703         }
704
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;
709
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;
720         }
721
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;
732         }
733
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)) {
737
738                 /* Heuristically detect devices that generate bogus residues
739                  * by seeing what happens with INQUIRY and READ CAPACITY
740                  * commands.
741                  */
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;
749
750                 } else {
751                         residue = min(residue, transfer_length);
752                         scsi_set_resid(srb, max_t(int, scsi_get_resid(srb),
753                                                         residue));
754                 }
755         }
756
757         /* based on the status code, we report good or bad */
758         switch (bcs->Status) {
759         case US_BULK_STAT_OK:
760                 if (fake_sense) {
761                         memcpy(srb->sense_buffer, usb_stor_sense_invalidCDB,
762                                         sizeof(usb_stor_sense_invalidCDB));
763                         return USB_STOR_TRANSPORT_NO_SENSE;
764                 }
765                 return USB_STOR_TRANSPORT_GOOD;
766
767         case US_BULK_STAT_FAIL:
768                 return USB_STOR_TRANSPORT_FAILED;
769
770         case US_BULK_STAT_PHASE:
771                 return USB_STOR_TRANSPORT_ERROR;
772         }
773         return USB_STOR_TRANSPORT_ERROR;
774 }
775
776 /***********************************************************************
777  * Reset routines
778  ***********************************************************************/
779 /*
780  * usb_stor_reset_common()
781  */
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)
785 {
786         int result;
787         int result2;
788
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"); */
792                 return -EIO;
793         }
794
795         result = usb_stor_control_msg(us, us->send_ctrl_pipe,
796                         request, requesttype, value, index, data, size, 5*HZ);
797
798         if (result < 0) {
799                 /* pr_info("Soft reset failed: %d\n", result); */
800                 return result;
801         }
802
803         wait_event_interruptible_timeout(us->delay_wait,
804                         test_bit(US_FLIDX_DISCONNECTING, &us->dflags),  HZ*6);
805
806         if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
807                 /* pr_info("Reset interrupted by disconnect\n"); */
808                 return -EIO;
809         }
810
811         /* pr_info("Soft reset: clearing bulk-in endpoint halt\n"); */
812         result = usb_stor_clear_halt(us, us->recv_bulk_pipe);
813
814         /* pr_info("Soft reset: clearing bulk-out endpoint halt\n"); */
815         result2 = usb_stor_clear_halt(us, us->send_bulk_pipe);
816
817         /* return a result code based on the result of the clear-halts */
818         if (result >= 0)
819                 result = result2;
820         /* if (result < 0) */
821                 /* pr_info("Soft reset failed\n"); */
822         /* else */
823                 /* pr_info("Soft reset done\n"); */
824         return result;
825 }
826
827 /*
828  * usb_stor_Bulk_reset()
829  */
830 int usb_stor_Bulk_reset(struct us_data *us)
831 {
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);
836 }
837
838 /*
839  * usb_stor_port_reset()
840  */
841 int usb_stor_port_reset(struct us_data *us)
842 {
843         int result;
844
845         /* pr_info("transport --- usb_stor_port_reset\n"); */
846         result = usb_lock_device_for_reset(us->pusb_dev, us->pusb_intf);
847         if (result < 0)
848                 pr_info("unable to lock device for reset: %d\n", result);
849         else {
850                 /* Were we disconnected while waiting for the lock? */
851                 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
852                         result = -EIO;
853                         /* pr_info("No reset during disconnect\n"); */
854                 } else {
855                         result = usb_reset_device(us->pusb_dev);
856                         /* pr_info("usb_reset_composite_device returns %d\n",
857                                                                 result); */
858                 }
859                 usb_unlock_device(us->pusb_dev);
860         }
861         return result;
862 }
863
864