]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/keucr/transport.c
Merge 3.10-rc6 into staging-next
[karo-tx-linux.git] / 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         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);
93
94         switch (cmd) {
95         case TEST_UNIT_READY:
96                 /* dev_dbg(&us->pusb_dev->dev,
97                         "scsi cmd %X --- SCSIOP_TEST_UNIT_READY\n", cmd); */
98                 break;
99         case INQUIRY:
100                 dev_dbg(&us->pusb_dev->dev,
101                                 "scsi cmd %X --- SCSIOP_INQUIRY\n", cmd);
102                 break;
103         case MODE_SENSE:
104                 dev_dbg(&us->pusb_dev->dev,
105                                 "scsi cmd %X --- SCSIOP_MODE_SENSE\n", cmd);
106                 break;
107         case START_STOP:
108                 dev_dbg(&us->pusb_dev->dev,
109                                 "scsi cmd %X --- SCSIOP_START_STOP\n", cmd);
110                 break;
111         case READ_CAPACITY:
112                 dev_dbg(&us->pusb_dev->dev,
113                                 "scsi cmd %X --- SCSIOP_READ_CAPACITY\n", cmd);
114                 break;
115         case READ_10:
116                 /*  dev_dbg(&us->pusb_dev->dev,
117                         "scsi cmd %X --- SCSIOP_READ,bn = %X, blen = %X\n"
118                         cmd, bn, blen); */
119                 break;
120         case WRITE_10:
121                 /* dev_dbg(&us->pusb_dev->dev,
122                         "scsi cmd %X --- SCSIOP_WRITE, bn = %X, blen = %X\n",
123                         cmd, bn, blen); */
124                 break;
125         case ALLOW_MEDIUM_REMOVAL:
126                 dev_dbg(&us->pusb_dev->dev,
127                         "scsi cmd %X --- SCSIOP_ALLOW_MEDIUM_REMOVAL\n", cmd);
128                 break;
129         default:
130                 dev_dbg(&us->pusb_dev->dev, "scsi cmd %X --- Other cmd\n", cmd);
131                 break;
132         }
133         bn = 0;
134 }
135
136 /*
137  * usb_stor_control_msg()
138  */
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)
142 {
143         int status;
144
145         /* pr_info("transport --- usb_stor_control_msg\n"); */
146
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);
153
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);
159
160         /* return the actual length of the data transferred if no error */
161         if (status == 0)
162                 status = us->current_urb->actual_length;
163         return status;
164 }
165
166 /*
167  * usb_stor_clear_halt()
168  */
169 int usb_stor_clear_halt(struct us_data *us, unsigned int pipe)
170 {
171         int result;
172         int endp = usb_pipeendpoint(pipe);
173
174         /* pr_info("transport --- usb_stor_clear_halt\n"); */
175         if (usb_pipein(pipe))
176                 endp |= USB_DIR_IN;
177
178         result = usb_stor_control_msg(us, us->send_ctrl_pipe,
179                 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
180                 USB_ENDPOINT_HALT, endp,
181                 NULL, 0, 3*HZ);
182
183         /* reset the endpoint toggle */
184         if (result >= 0)
185                 /* usb_settoggle(us->pusb_dev, usb_pipeendpoint(pipe),
186                                                 usb_pipeout(pipe), 0); */
187                 usb_reset_endpoint(us->pusb_dev, endp);
188
189         return result;
190 }
191
192 /*
193  * interpret_urb_result()
194  */
195 static int interpret_urb_result(struct us_data *us, unsigned int pipe,
196                 unsigned int length, int result, unsigned int partial)
197 {
198         /* pr_info("transport --- interpret_urb_result\n"); */
199         switch (result) {
200         /* no error code; did we send all the data? */
201         case 0:
202                 if (partial != length) {
203                         /* pr_info("-- short transfer\n"); */
204                         return USB_STOR_XFER_SHORT;
205                 }
206                 /* pr_info("-- transfer complete\n"); */
207                 return USB_STOR_XFER_GOOD;
208         case -EPIPE:
209                 if (usb_pipecontrol(pipe)) {
210                         /* pr_info("-- stall on control pipe\n"); */
211                         return USB_STOR_XFER_STALLED;
212                 }
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;
217         case -EOVERFLOW:
218                 /* pr_info("-- babble\n"); */
219                 return USB_STOR_XFER_LONG;
220         case -ECONNRESET:
221                 /* pr_info("-- transfer cancelled\n"); */
222                 return USB_STOR_XFER_ERROR;
223         case -EREMOTEIO:
224                 /* pr_info("-- short read transfer\n"); */
225                 return USB_STOR_XFER_SHORT;
226         case -EIO:
227                 /* pr_info("-- abort or disconnect in progress\n"); */
228                 return USB_STOR_XFER_ERROR;
229         default:
230                 /* pr_info("-- unknown error\n"); */
231                 return USB_STOR_XFER_ERROR;
232         }
233 }
234
235 /*
236  * usb_stor_bulk_transfer_buf()
237  */
238 int usb_stor_bulk_transfer_buf(struct us_data *us, unsigned int pipe,
239         void *buf, unsigned int length, unsigned int *act_len)
240 {
241         int result;
242
243         /* pr_info("transport --- usb_stor_bulk_transfer_buf\n"); */
244
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);
249
250         /* store the actual length of the data transferred */
251         if (act_len)
252                 *act_len = us->current_urb->actual_length;
253
254         return interpret_urb_result(us, pipe, length, result,
255                                         us->current_urb->actual_length);
256 }
257
258 /*
259  * usb_stor_bulk_transfer_sglist()
260  */
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)
264 {
265         int result;
266
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;
270
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);
274         if (result) {
275                 /* pr_info("usb_sg_init returned %d\n", result); */
276                 return USB_STOR_XFER_ERROR;
277         }
278
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);
282
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);
289                 }
290         }
291
292         /* wait for the completion of the transfer */
293         usb_sg_wait(&us->current_sg);
294         clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
295
296         result = us->current_sg.status;
297         if (act_len)
298                 *act_len = us->current_sg.bytes;
299
300         return interpret_urb_result(us, pipe, length,
301                                         result, us->current_sg.bytes);
302 }
303
304 /*
305  * usb_stor_bulk_srb()
306  */
307 int usb_stor_bulk_srb(struct us_data *us, unsigned int pipe,
308                                         struct scsi_cmnd *srb)
309 {
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),
313                                       &partial);
314
315         scsi_set_resid(srb, scsi_bufflen(srb) - partial);
316         return result;
317 }
318
319 /*
320  * usb_stor_bulk_transfer_sg()
321  */
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)
324 {
325         int result;
326         unsigned int partial;
327
328         /* pr_info("transport --- usb_stor_bulk_transfer_sg\n"); */
329         /* are we scatter-gathering? */
330         if (use_sg) {
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;
336         } else {
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;
341         }
342
343         /* store the residual and return the error code */
344         if (residual)
345                 *residual = length_left;
346         return result;
347 }
348
349 /***********************************************************************
350  * Transport routines
351  ***********************************************************************/
352 /*
353  * usb_stor_invoke_transport()
354  */
355 void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
356 {
357         int need_auto_sense;
358         int result;
359
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; */
365
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;
371                 goto Handle_Errors;
372         }
373
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;
378                 goto Handle_Errors;
379         }
380
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;
384                 return;
385         }
386
387         srb->result = SAM_STAT_GOOD;
388
389         /* Determine if we need to auto-sense */
390         need_auto_sense = 0;
391
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"); */
395                 need_auto_sense = 1;
396         }
397
398         if (result == USB_STOR_TRANSPORT_FAILED) {
399                 /* pr_info("-- transport indicates command failure\n"); */
400                 need_auto_sense = 1;
401         }
402
403         /* Now, if we need to do the auto-sense, let's do it */
404         if (need_auto_sense) {
405                 int temp_result;
406                 struct scsi_eh_save ses;
407
408                 pr_info("Issuing auto-REQUEST_SENSE\n");
409
410                 scsi_eh_prep_cmnd(srb, &ses, NULL, 0, US_SENSE_SIZE);
411
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) {
416                         srb->cmd_len = 6;
417                 } else {
418                         srb->cmd_len = 12;
419                 }
420                 /* issue the auto-sense command */
421                 scsi_set_resid(srb, 0);
422                 temp_result = us->transport(us->srb, us);
423
424                 /* let's clean up right away */
425                 scsi_eh_restore_cmnd(srb, &ses);
426
427                 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
428                         /* pr_info("-- auto-sense aborted\n"); */
429                         srb->result = DID_ABORT << 16;
430                         goto Handle_Errors;
431                 }
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))
436                                 goto Handle_Errors;
437                         return;
438                 }
439
440                 /* set the result so the higher layers expect this data */
441                 srb->result = SAM_STAT_CHECK_CONDITION;
442
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;
449                 }
450         }
451
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); */
457
458         return;
459
460 Handle_Errors:
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));
465
466         mutex_unlock(&us->dev_mutex);
467         result = usb_stor_port_reset(us);
468         mutex_lock(&us->dev_mutex);
469
470         if (result < 0) {
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);
475         }
476         clear_bit(US_FLIDX_RESETTING, &us->dflags);
477 }
478
479 /*
480  * ENE_stor_invoke_transport()
481  */
482 void ENE_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
483 {
484         int result = 0;
485
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);
492
493         if (us->Power_IsResum == true) {
494                 result = ENE_InitMedia(us);
495                 us->Power_IsResum = false;
496         }
497
498         if (us->SM_Status.Ready)
499                 result = SM_SCSIIrp(us, srb);
500
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;
506                 goto Handle_Errors;
507         }
508
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;
513                 goto Handle_Errors;
514         }
515
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;
519                 return;
520         }
521
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;
528         }
529
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); */
535
536         return;
537
538 Handle_Errors:
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));
543
544         mutex_unlock(&us->dev_mutex);
545         result = usb_stor_port_reset(us);
546         mutex_lock(&us->dev_mutex);
547
548         if (result < 0) {
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);
553         }
554         clear_bit(US_FLIDX_RESETTING, &us->dflags);
555 }
556
557 /*
558  * BuildSenseBuffer()
559  */
560 void BuildSenseBuffer(struct scsi_cmnd *srb, int SrbStatus)
561 {
562         BYTE    *buf = srb->sense_buffer;
563         BYTE    asc;
564
565         pr_info("transport --- BuildSenseBuffer\n");
566         switch (SrbStatus) {
567         case SS_NOT_READY:
568                 asc = 0x3a;
569                 break;  /*  sense key = 0x02 */
570         case SS_MEDIUM_ERR:
571                 asc = 0x0c;
572                 break;  /*  sense key = 0x03 */
573         case SS_ILLEGAL_REQUEST:
574                 asc = 0x20;
575                 break;  /*  sense key = 0x05 */
576         default:
577                 asc = 0x00;
578                 break;  /*  ?? */
579         }
580
581         memset(buf, 0, 18);
582         buf[0x00] = 0xf0;
583         buf[0x02] = SrbStatus;
584         buf[0x07] = 0x0b;
585         buf[0x0c] = asc;
586 }
587
588 /*
589  * usb_stor_stop_transport()
590  */
591 void usb_stor_stop_transport(struct us_data *us)
592 {
593         /* pr_info("transport --- usb_stor_stop_transport\n"); */
594
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);
598         }
599
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);
603         }
604 }
605
606 /*
607  * usb_stor_Bulk_max_lun()
608  */
609 int usb_stor_Bulk_max_lun(struct us_data *us)
610 {
611         int result;
612
613         /* pr_info("transport --- usb_stor_Bulk_max_lun\n"); */
614         /* issue the command */
615         us->iobuf[0] = 0;
616         result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
617                                  US_BULK_GET_MAX_LUN,
618                                  USB_DIR_IN | USB_TYPE_CLASS |
619                                  USB_RECIP_INTERFACE,
620                                  0, us->ifnum, us->iobuf, 1, HZ);
621
622         /* pr_info("GetMaxLUN command result is %d, data is %d\n",
623                                                 result, us->iobuf[0]); */
624
625         /* if we have a successful request, return the result */
626         if (result > 0)
627                 return us->iobuf[0];
628
629         return 0;
630 }
631
632 /*
633  * usb_stor_Bulk_transport()
634  */
635 int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
636 {
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;
641         int result;
642         int fake_sense = 0;
643         unsigned int cswlen;
644         unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
645
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)) {
649                 cbwlen = 32;
650                 us->iobuf[31] = 0;
651         }
652
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;
662
663         /* copy the command payload */
664         memset(bcb->CDB, 0, sizeof(bcb->CDB));
665         memcpy(bcb->CDB, srb->cmnd, bcb->Length);
666
667         /*  send command */
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),
673                         bcb->Length); */
674         result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
675                                                 bcb, cbwlen, NULL);
676         /* pr_info("Bulk command transfer result=%d\n", result); */
677         if (result != USB_STOR_XFER_GOOD)
678                 return USB_STOR_TRANSPORT_ERROR;
679
680         if (unlikely(us->fflags & US_FL_GO_SLOW))
681                 udelay(125);
682
683         /*  R/W data */
684         if (transfer_length) {
685                 unsigned int pipe;
686                 if (srb->sc_data_direction == DMA_FROM_DEVICE)
687                         pipe = us->recv_bulk_pipe;
688                 else
689                         pipe = us->send_bulk_pipe;
690
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;
695
696                 if (result == USB_STOR_XFER_LONG)
697                         fake_sense = 1;
698         }
699
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);
704
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);
709         }
710
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);
717         }
718
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;
723
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;
734         }
735
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;
746         }
747
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)) {
751
752                 /* Heuristically detect devices that generate bogus residues
753                  * by seeing what happens with INQUIRY and READ CAPACITY
754                  * commands.
755                  */
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;
763
764                 } else {
765                         residue = min(residue, transfer_length);
766                         scsi_set_resid(srb, max_t(int, scsi_get_resid(srb),
767                                                         residue));
768                 }
769         }
770
771         /* based on the status code, we report good or bad */
772         switch (bcs->Status) {
773         case US_BULK_STAT_OK:
774                 if (fake_sense) {
775                         memcpy(srb->sense_buffer, usb_stor_sense_invalidCDB,
776                                         sizeof(usb_stor_sense_invalidCDB));
777                         return USB_STOR_TRANSPORT_NO_SENSE;
778                 }
779                 return USB_STOR_TRANSPORT_GOOD;
780
781         case US_BULK_STAT_FAIL:
782                 return USB_STOR_TRANSPORT_FAILED;
783
784         case US_BULK_STAT_PHASE:
785                 return USB_STOR_TRANSPORT_ERROR;
786         }
787         return USB_STOR_TRANSPORT_ERROR;
788 }
789
790 /***********************************************************************
791  * Reset routines
792  ***********************************************************************/
793 /*
794  * usb_stor_reset_common()
795  */
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)
799 {
800         int result;
801         int result2;
802
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"); */
806                 return -EIO;
807         }
808
809         result = usb_stor_control_msg(us, us->send_ctrl_pipe,
810                         request, requesttype, value, index, data, size, 5*HZ);
811
812         if (result < 0) {
813                 /* pr_info("Soft reset failed: %d\n", result); */
814                 return result;
815         }
816
817         wait_event_interruptible_timeout(us->delay_wait,
818                         test_bit(US_FLIDX_DISCONNECTING, &us->dflags),  HZ*6);
819
820         if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
821                 /* pr_info("Reset interrupted by disconnect\n"); */
822                 return -EIO;
823         }
824
825         /* pr_info("Soft reset: clearing bulk-in endpoint halt\n"); */
826         result = usb_stor_clear_halt(us, us->recv_bulk_pipe);
827
828         /* pr_info("Soft reset: clearing bulk-out endpoint halt\n"); */
829         result2 = usb_stor_clear_halt(us, us->send_bulk_pipe);
830
831         /* return a result code based on the result of the clear-halts */
832         if (result >= 0)
833                 result = result2;
834         /* if (result < 0) */
835                 /* pr_info("Soft reset failed\n"); */
836         /* else */
837                 /* pr_info("Soft reset done\n"); */
838         return result;
839 }
840
841 /*
842  * usb_stor_Bulk_reset()
843  */
844 int usb_stor_Bulk_reset(struct us_data *us)
845 {
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);
850 }
851
852 /*
853  * usb_stor_port_reset()
854  */
855 int usb_stor_port_reset(struct us_data *us)
856 {
857         int result;
858
859         /* pr_info("transport --- usb_stor_port_reset\n"); */
860         result = usb_lock_device_for_reset(us->pusb_dev, us->pusb_intf);
861         if (result < 0)
862                 pr_info("unable to lock device for reset: %d\n", result);
863         else {
864                 /* Were we disconnected while waiting for the lock? */
865                 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
866                         result = -EIO;
867                         /* pr_info("No reset during disconnect\n"); */
868                 } else {
869                         result = usb_reset_device(us->pusb_dev);
870                         /* pr_info("usb_reset_composite_device returns %d\n",
871                                                                 result); */
872                 }
873                 usb_unlock_device(us->pusb_dev);
874         }
875         return result;
876 }
877
878