Merge tag 'sound-3.8' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound
[linux-2.6-block.git] / drivers / staging / keucr / transport.c
CommitLineData
871f8477
CYC
1#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2
126bb03b
AC
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 ***********************************************************************/
3aa4fc58
CYC
18/*
19 * usb_stor_blocking_completion()
20 */
126bb03b
AC
21static void usb_stor_blocking_completion(struct urb *urb)
22{
23 struct completion *urb_done_ptr = urb->context;
24
871f8477 25 /* pr_info("transport --- usb_stor_blocking_completion\n"); */
126bb03b
AC
26 complete(urb_done_ptr);
27}
28
3aa4fc58
CYC
29/*
30 * usb_stor_msg_common()
31 */
126bb03b
AC
32static int usb_stor_msg_common(struct us_data *us, int timeout)
33{
34 struct completion urb_done;
35 long timeleft;
36 int status;
37
871f8477 38 /* pr_info("transport --- usb_stor_msg_common\n"); */
126bb03b
AC
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
307ae1d3 49 us->current_urb->transfer_flags = 0;
126bb03b
AC
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
3aa4fc58
CYC
61 if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
62 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
871f8477 63 /* pr_info("-- cancelling URB\n"); */
126bb03b
AC
64 usb_unlink_urb(us->current_urb);
65 }
66 }
67
3aa4fc58
CYC
68 timeleft = wait_for_completion_interruptible_timeout(&urb_done,
69 timeout ? : MAX_SCHEDULE_TIMEOUT);
126bb03b
AC
70 clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags);
71
3aa4fc58 72 if (timeleft <= 0) {
871f8477
CYC
73 /* pr_info("%s -- cancelling URB\n",
74 timeleft == 0 ? "Timeout" : "Signal"); */
126bb03b
AC
75 usb_kill_urb(us->current_urb);
76 }
77
78 return us->current_urb->status;
79}
80
3aa4fc58
CYC
81/*
82 * usb_stor_control_msg()
83 */
126bb03b
AC
84int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
85 u8 request, u8 requesttype, u16 value, u16 index,
86 void *data, u16 size, int timeout)
87{
88 int status;
89
871f8477 90 /* pr_info("transport --- usb_stor_control_msg\n"); */
126bb03b
AC
91
92 /* fill in the devrequest structure */
93 us->cr->bRequestType = requesttype;
94 us->cr->bRequest = request;
95 us->cr->wValue = cpu_to_le16(value);
96 us->cr->wIndex = cpu_to_le16(index);
97 us->cr->wLength = cpu_to_le16(size);
98
99 /* fill and submit the URB */
100 usb_fill_control_urb(us->current_urb, us->pusb_dev, pipe,
3aa4fc58 101 (unsigned char *) us->cr, data, size,
126bb03b
AC
102 usb_stor_blocking_completion, NULL);
103 status = usb_stor_msg_common(us, timeout);
104
105 /* return the actual length of the data transferred if no error */
106 if (status == 0)
107 status = us->current_urb->actual_length;
108 return status;
109}
110
3aa4fc58
CYC
111/*
112 * usb_stor_clear_halt()
113 */
126bb03b
AC
114int usb_stor_clear_halt(struct us_data *us, unsigned int pipe)
115{
116 int result;
117 int endp = usb_pipeendpoint(pipe);
118
871f8477 119 /* pr_info("transport --- usb_stor_clear_halt\n"); */
3aa4fc58 120 if (usb_pipein(pipe))
126bb03b
AC
121 endp |= USB_DIR_IN;
122
123 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
124 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
125 USB_ENDPOINT_HALT, endp,
126 NULL, 0, 3*HZ);
127
128 /* reset the endpoint toggle */
129 if (result >= 0)
3aa4fc58
CYC
130 /* usb_settoggle(us->pusb_dev, usb_pipeendpoint(pipe),
131 usb_pipeout(pipe), 0); */
132 usb_reset_endpoint(us->pusb_dev, endp);
126bb03b
AC
133
134 return result;
135}
136
3aa4fc58
CYC
137/*
138 * interpret_urb_result()
139 */
126bb03b
AC
140static int interpret_urb_result(struct us_data *us, unsigned int pipe,
141 unsigned int length, int result, unsigned int partial)
142{
871f8477 143 /* pr_info("transport --- interpret_urb_result\n"); */
126bb03b
AC
144 switch (result) {
145 /* no error code; did we send all the data? */
146 case 0:
3aa4fc58 147 if (partial != length) {
871f8477 148 /* pr_info("-- short transfer\n"); */
126bb03b
AC
149 return USB_STOR_XFER_SHORT;
150 }
871f8477 151 /* pr_info("-- transfer complete\n"); */
126bb03b
AC
152 return USB_STOR_XFER_GOOD;
153 case -EPIPE:
3aa4fc58 154 if (usb_pipecontrol(pipe)) {
871f8477 155 /* pr_info("-- stall on control pipe\n"); */
126bb03b
AC
156 return USB_STOR_XFER_STALLED;
157 }
871f8477 158 /* pr_info("clearing endpoint halt for pipe 0x%x\n", pipe); */
126bb03b
AC
159 if (usb_stor_clear_halt(us, pipe) < 0)
160 return USB_STOR_XFER_ERROR;
161 return USB_STOR_XFER_STALLED;
162 case -EOVERFLOW:
871f8477 163 /* pr_info("-- babble\n"); */
126bb03b
AC
164 return USB_STOR_XFER_LONG;
165 case -ECONNRESET:
871f8477 166 /* pr_info("-- transfer cancelled\n"); */
126bb03b
AC
167 return USB_STOR_XFER_ERROR;
168 case -EREMOTEIO:
871f8477 169 /* pr_info("-- short read transfer\n"); */
126bb03b
AC
170 return USB_STOR_XFER_SHORT;
171 case -EIO:
871f8477 172 /* pr_info("-- abort or disconnect in progress\n"); */
126bb03b
AC
173 return USB_STOR_XFER_ERROR;
174 default:
871f8477 175 /* pr_info("-- unknown error\n"); */
126bb03b
AC
176 return USB_STOR_XFER_ERROR;
177 }
178}
179
3aa4fc58
CYC
180/*
181 * usb_stor_bulk_transfer_buf()
182 */
126bb03b
AC
183int usb_stor_bulk_transfer_buf(struct us_data *us, unsigned int pipe,
184 void *buf, unsigned int length, unsigned int *act_len)
185{
186 int result;
187
871f8477 188 /* pr_info("transport --- usb_stor_bulk_transfer_buf\n"); */
126bb03b
AC
189
190 /* fill and submit the URB */
3aa4fc58
CYC
191 usb_fill_bulk_urb(us->current_urb, us->pusb_dev, pipe, buf,
192 length, usb_stor_blocking_completion, NULL);
126bb03b
AC
193 result = usb_stor_msg_common(us, 0);
194
195 /* store the actual length of the data transferred */
196 if (act_len)
197 *act_len = us->current_urb->actual_length;
198
3aa4fc58
CYC
199 return interpret_urb_result(us, pipe, length, result,
200 us->current_urb->actual_length);
126bb03b
AC
201}
202
3aa4fc58
CYC
203/*
204 * usb_stor_bulk_transfer_sglist()
205 */
126bb03b
AC
206static int usb_stor_bulk_transfer_sglist(struct us_data *us, unsigned int pipe,
207 struct scatterlist *sg, int num_sg, unsigned int length,
208 unsigned int *act_len)
209{
210 int result;
211
871f8477 212 /* pr_info("transport --- usb_stor_bulk_transfer_sglist\n"); */
126bb03b
AC
213 if (test_bit(US_FLIDX_ABORTING, &us->dflags))
214 return USB_STOR_XFER_ERROR;
215
216 /* initialize the scatter-gather request block */
3aa4fc58
CYC
217 result = usb_sg_init(&us->current_sg, us->pusb_dev, pipe, 0,
218 sg, num_sg, length, GFP_NOIO);
219 if (result) {
871f8477 220 /* pr_info("usb_sg_init returned %d\n", result); */
126bb03b
AC
221 return USB_STOR_XFER_ERROR;
222 }
223
3aa4fc58
CYC
224 /* since the block has been initialized successfully,
225 it's now okay to cancel it */
126bb03b
AC
226 set_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
227
228 /* did an abort/disconnect occur during the submission? */
3aa4fc58 229 if (test_bit(US_FLIDX_ABORTING, &us->dflags)) {
126bb03b 230 /* cancel the request, if it hasn't been cancelled already */
3aa4fc58 231 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
871f8477 232 /* pr_info("-- cancelling sg request\n"); */
126bb03b
AC
233 usb_sg_cancel(&us->current_sg);
234 }
235 }
236
237 /* wait for the completion of the transfer */
238 usb_sg_wait(&us->current_sg);
239 clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags);
240
241 result = us->current_sg.status;
242 if (act_len)
243 *act_len = us->current_sg.bytes;
244
3aa4fc58
CYC
245 return interpret_urb_result(us, pipe, length,
246 result, us->current_sg.bytes);
126bb03b
AC
247}
248
3aa4fc58
CYC
249/*
250 * usb_stor_bulk_srb()
251 */
252int usb_stor_bulk_srb(struct us_data *us, unsigned int pipe,
253 struct scsi_cmnd *srb)
126bb03b
AC
254{
255 unsigned int partial;
256 int result = usb_stor_bulk_transfer_sglist(us, pipe, scsi_sglist(srb),
257 scsi_sg_count(srb), scsi_bufflen(srb),
258 &partial);
259
260 scsi_set_resid(srb, scsi_bufflen(srb) - partial);
261 return result;
262}
263
3aa4fc58
CYC
264/*
265 * usb_stor_bulk_transfer_sg()
266 */
267int usb_stor_bulk_transfer_sg(struct us_data *us, unsigned int pipe,
126bb03b
AC
268 void *buf, unsigned int length_left, int use_sg, int *residual)
269{
270 int result;
271 unsigned int partial;
272
871f8477 273 /* pr_info("transport --- usb_stor_bulk_transfer_sg\n"); */
126bb03b 274 /* are we scatter-gathering? */
3aa4fc58 275 if (use_sg) {
126bb03b
AC
276 /* use the usb core scatter-gather primitives */
277 result = usb_stor_bulk_transfer_sglist(us, pipe,
278 (struct scatterlist *) buf, use_sg,
279 length_left, &partial);
280 length_left -= partial;
3aa4fc58 281 } else {
126bb03b 282 /* no scatter-gather, just make the request */
3aa4fc58
CYC
283 result = usb_stor_bulk_transfer_buf(us, pipe, buf,
284 length_left, &partial);
126bb03b
AC
285 length_left -= partial;
286 }
287
288 /* store the residual and return the error code */
289 if (residual)
290 *residual = length_left;
291 return result;
292}
293
294/***********************************************************************
295 * Transport routines
296 ***********************************************************************/
3aa4fc58
CYC
297/*
298 * usb_stor_invoke_transport()
299 */
126bb03b
AC
300void usb_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
301{
302 int need_auto_sense;
303 int result;
304
871f8477 305 /* pr_info("transport --- usb_stor_invoke_transport\n"); */
126bb03b
AC
306 usb_stor_print_cmd(srb);
307 /* send the command to the transport layer */
308 scsi_set_resid(srb, 0);
3aa4fc58
CYC
309 result = us->transport(srb, us); /* usb_stor_Bulk_transport; */
310
311 /* if the command gets aborted by the higher layers,
312 we need to short-circuit all other processing */
313 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
871f8477 314 /* pr_info("-- command was aborted\n"); */
126bb03b
AC
315 srb->result = DID_ABORT << 16;
316 goto Handle_Errors;
317 }
318
319 /* if there is a transport error, reset and don't auto-sense */
3aa4fc58 320 if (result == USB_STOR_TRANSPORT_ERROR) {
871f8477 321 /* pr_info("-- transport indicates error, resetting\n"); */
126bb03b
AC
322 srb->result = DID_ERROR << 16;
323 goto Handle_Errors;
324 }
325
326 /* if the transport provided its own sense data, don't auto-sense */
3aa4fc58 327 if (result == USB_STOR_TRANSPORT_NO_SENSE) {
126bb03b
AC
328 srb->result = SAM_STAT_CHECK_CONDITION;
329 return;
330 }
331
332 srb->result = SAM_STAT_GOOD;
333
334 /* Determine if we need to auto-sense */
335 need_auto_sense = 0;
336
3aa4fc58
CYC
337 if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_DPCM_USB) &&
338 srb->sc_data_direction != DMA_FROM_DEVICE) {
871f8477 339 /* pr_info("-- CB transport device requiring auto-sense\n"); */
126bb03b
AC
340 need_auto_sense = 1;
341 }
342
3aa4fc58 343 if (result == USB_STOR_TRANSPORT_FAILED) {
871f8477 344 /* pr_info("-- transport indicates command failure\n"); */
126bb03b
AC
345 need_auto_sense = 1;
346 }
347
348 /* Now, if we need to do the auto-sense, let's do it */
3aa4fc58 349 if (need_auto_sense) {
126bb03b
AC
350 int temp_result;
351 struct scsi_eh_save ses;
352
871f8477 353 pr_info("Issuing auto-REQUEST_SENSE\n");
126bb03b
AC
354
355 scsi_eh_prep_cmnd(srb, &ses, NULL, 0, US_SENSE_SIZE);
356
357 /* we must do the protocol translation here */
3aa4fc58
CYC
358 if (us->subclass == USB_SC_RBC ||
359 us->subclass == USB_SC_SCSI ||
360 us->subclass == USB_SC_CYP_ATACB) {
126bb03b 361 srb->cmd_len = 6;
3aa4fc58 362 } else {
126bb03b 363 srb->cmd_len = 12;
3aa4fc58 364 }
126bb03b
AC
365 /* issue the auto-sense command */
366 scsi_set_resid(srb, 0);
367 temp_result = us->transport(us->srb, us);
368
369 /* let's clean up right away */
370 scsi_eh_restore_cmnd(srb, &ses);
371
3aa4fc58 372 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
871f8477 373 /* pr_info("-- auto-sense aborted\n"); */
126bb03b
AC
374 srb->result = DID_ABORT << 16;
375 goto Handle_Errors;
376 }
3aa4fc58 377 if (temp_result != USB_STOR_TRANSPORT_GOOD) {
871f8477 378 /* pr_info("-- auto-sense failure\n"); */
126bb03b
AC
379 srb->result = DID_ERROR << 16;
380 if (!(us->fflags & US_FL_SCM_MULT_TARG))
381 goto Handle_Errors;
382 return;
383 }
384
385 /* set the result so the higher layers expect this data */
386 srb->result = SAM_STAT_CHECK_CONDITION;
387
388 if (result == USB_STOR_TRANSPORT_GOOD &&
389 (srb->sense_buffer[2] & 0xaf) == 0 &&
390 srb->sense_buffer[12] == 0 &&
3aa4fc58 391 srb->sense_buffer[13] == 0) {
126bb03b
AC
392 srb->result = SAM_STAT_GOOD;
393 srb->sense_buffer[0] = 0x0;
394 }
395 }
396
397 /* Did we transfer less than the minimum amount required? */
3aa4fc58
CYC
398 if (srb->result == SAM_STAT_GOOD && scsi_bufflen(srb) -
399 scsi_get_resid(srb) < srb->underflow)
400 srb->result = (DID_ERROR << 16);
401 /* v02 | (SUGGEST_RETRY << 24); */
126bb03b
AC
402
403 return;
404
405Handle_Errors:
406 scsi_lock(us_to_host(us));
407 set_bit(US_FLIDX_RESETTING, &us->dflags);
408 clear_bit(US_FLIDX_ABORTING, &us->dflags);
409 scsi_unlock(us_to_host(us));
410
411 mutex_unlock(&us->dev_mutex);
412 result = usb_stor_port_reset(us);
413 mutex_lock(&us->dev_mutex);
414
3aa4fc58 415 if (result < 0) {
126bb03b
AC
416 scsi_lock(us_to_host(us));
417 usb_stor_report_device_reset(us);
418 scsi_unlock(us_to_host(us));
419 us->transport_reset(us);
420 }
421 clear_bit(US_FLIDX_RESETTING, &us->dflags);
422}
423
3aa4fc58
CYC
424/*
425 * ENE_stor_invoke_transport()
426 */
126bb03b
AC
427void ENE_stor_invoke_transport(struct scsi_cmnd *srb, struct us_data *us)
428{
3aa4fc58 429 int result = 0;
126bb03b 430
871f8477 431 /* pr_info("transport --- ENE_stor_invoke_transport\n"); */
126bb03b
AC
432 usb_stor_print_cmd(srb);
433 /* send the command to the transport layer */
434 scsi_set_resid(srb, 0);
20c3d7f7 435 if (!(us->SM_Status.Ready))
126bb03b 436 result = ENE_InitMedia(us);
3aa4fc58 437
126bb03b
AC
438 if (us->Power_IsResum == true) {
439 result = ENE_InitMedia(us);
3aa4fc58
CYC
440 us->Power_IsResum = false;
441 }
442
3aa4fc58
CYC
443 if (us->SM_Status.Ready)
444 result = SM_SCSIIrp(us, srb);
445
446 /* if the command gets aborted by the higher layers,
447 we need to short-circuit all other processing */
448 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) {
871f8477 449 /* pr_info("-- command was aborted\n"); */
126bb03b
AC
450 srb->result = DID_ABORT << 16;
451 goto Handle_Errors;
452 }
453
454 /* if there is a transport error, reset and don't auto-sense */
3aa4fc58 455 if (result == USB_STOR_TRANSPORT_ERROR) {
871f8477 456 /* pr_info("-- transport indicates error, resetting\n"); */
126bb03b
AC
457 srb->result = DID_ERROR << 16;
458 goto Handle_Errors;
459 }
460
461 /* if the transport provided its own sense data, don't auto-sense */
3aa4fc58 462 if (result == USB_STOR_TRANSPORT_NO_SENSE) {
126bb03b
AC
463 srb->result = SAM_STAT_CHECK_CONDITION;
464 return;
465 }
466
467 srb->result = SAM_STAT_GOOD;
3aa4fc58 468 if (result == USB_STOR_TRANSPORT_FAILED) {
871f8477 469 /* pr_info("-- transport indicates command failure\n"); */
3aa4fc58 470 /* need_auto_sense = 1; */
126bb03b
AC
471 BuildSenseBuffer(srb, us->SrbStatus);
472 srb->result = SAM_STAT_CHECK_CONDITION;
473 }
474
475 /* Did we transfer less than the minimum amount required? */
3aa4fc58
CYC
476 if (srb->result == SAM_STAT_GOOD && scsi_bufflen(srb) -
477 scsi_get_resid(srb) < srb->underflow)
478 srb->result = (DID_ERROR << 16);
479 /* v02 | (SUGGEST_RETRY << 24); */
126bb03b
AC
480
481 return;
482
483Handle_Errors:
484 scsi_lock(us_to_host(us));
485 set_bit(US_FLIDX_RESETTING, &us->dflags);
486 clear_bit(US_FLIDX_ABORTING, &us->dflags);
487 scsi_unlock(us_to_host(us));
488
489 mutex_unlock(&us->dev_mutex);
490 result = usb_stor_port_reset(us);
491 mutex_lock(&us->dev_mutex);
492
3aa4fc58 493 if (result < 0) {
126bb03b
AC
494 scsi_lock(us_to_host(us));
495 usb_stor_report_device_reset(us);
496 scsi_unlock(us_to_host(us));
497 us->transport_reset(us);
498 }
499 clear_bit(US_FLIDX_RESETTING, &us->dflags);
500}
501
3aa4fc58
CYC
502/*
503 * BuildSenseBuffer()
504 */
126bb03b
AC
505void BuildSenseBuffer(struct scsi_cmnd *srb, int SrbStatus)
506{
3aa4fc58
CYC
507 BYTE *buf = srb->sense_buffer;
508 BYTE asc;
126bb03b 509
871f8477 510 pr_info("transport --- BuildSenseBuffer\n");
3aa4fc58
CYC
511 switch (SrbStatus) {
512 case SS_NOT_READY:
513 asc = 0x3a;
514 break; /* sense key = 0x02 */
515 case SS_MEDIUM_ERR:
516 asc = 0x0c;
517 break; /* sense key = 0x03 */
518 case SS_ILLEGAL_REQUEST:
519 asc = 0x20;
520 break; /* sense key = 0x05 */
521 default:
522 asc = 0x00;
523 break; /* ?? */
524 }
525
526 memset(buf, 0, 18);
527 buf[0x00] = 0xf0;
528 buf[0x02] = SrbStatus;
529 buf[0x07] = 0x0b;
530 buf[0x0c] = asc;
126bb03b
AC
531}
532
3aa4fc58
CYC
533/*
534 * usb_stor_stop_transport()
535 */
126bb03b
AC
536void usb_stor_stop_transport(struct us_data *us)
537{
871f8477 538 /* pr_info("transport --- usb_stor_stop_transport\n"); */
126bb03b 539
3aa4fc58 540 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) {
871f8477 541 /* pr_info("-- cancelling URB\n"); */
126bb03b
AC
542 usb_unlink_urb(us->current_urb);
543 }
544
3aa4fc58 545 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
871f8477 546 /* pr_info("-- cancelling sg request\n"); */
126bb03b
AC
547 usb_sg_cancel(&us->current_sg);
548 }
549}
550
3aa4fc58
CYC
551/*
552 * usb_stor_Bulk_max_lun()
553 */
126bb03b
AC
554int usb_stor_Bulk_max_lun(struct us_data *us)
555{
556 int result;
557
871f8477 558 /* pr_info("transport --- usb_stor_Bulk_max_lun\n"); */
126bb03b
AC
559 /* issue the command */
560 us->iobuf[0] = 0;
561 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
562 US_BULK_GET_MAX_LUN,
563 USB_DIR_IN | USB_TYPE_CLASS |
564 USB_RECIP_INTERFACE,
565 0, us->ifnum, us->iobuf, 1, HZ);
566
871f8477
CYC
567 /* pr_info("GetMaxLUN command result is %d, data is %d\n",
568 result, us->iobuf[0]); */
126bb03b
AC
569
570 /* if we have a successful request, return the result */
571 if (result > 0)
572 return us->iobuf[0];
573
574 return 0;
575}
576
3aa4fc58
CYC
577/*
578 * usb_stor_Bulk_transport()
579 */
126bb03b
AC
580int usb_stor_Bulk_transport(struct scsi_cmnd *srb, struct us_data *us)
581{
582 struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
583 struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
584 unsigned int transfer_length = scsi_bufflen(srb);
585 unsigned int residue;
586 int result;
587 int fake_sense = 0;
588 unsigned int cswlen;
589 unsigned int cbwlen = US_BULK_CB_WRAP_LEN;
590
871f8477 591 /* pr_info("transport --- usb_stor_Bulk_transport\n"); */
126bb03b 592 /* Take care of BULK32 devices; set extra byte to 0 */
3aa4fc58 593 if (unlikely(us->fflags & US_FL_BULK32)) {
126bb03b
AC
594 cbwlen = 32;
595 us->iobuf[31] = 0;
596 }
597
598 /* set up the command wrapper */
599 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
600 bcb->DataTransferLength = cpu_to_le32(transfer_length);
601 bcb->Flags = srb->sc_data_direction == DMA_FROM_DEVICE ? 1 << 7 : 0;
602 bcb->Tag = ++us->tag;
603 bcb->Lun = srb->device->lun;
604 if (us->fflags & US_FL_SCM_MULT_TARG)
605 bcb->Lun |= srb->device->id << 4;
606 bcb->Length = srb->cmd_len;
607
608 /* copy the command payload */
609 memset(bcb->CDB, 0, sizeof(bcb->CDB));
610 memcpy(bcb->CDB, srb->cmnd, bcb->Length);
611
3aa4fc58 612 /* send command */
126bb03b 613 /* send it to out endpoint */
871f8477 614 /* pr_info("Bulk Command S 0x%x T 0x%x L %d F %d Trg %d LUN %d CL %d\n",
126bb03b
AC
615 le32_to_cpu(bcb->Signature), bcb->Tag,
616 le32_to_cpu(bcb->DataTransferLength), bcb->Flags,
617 (bcb->Lun >> 4), (bcb->Lun & 0x0F),
871f8477 618 bcb->Length); */
3aa4fc58
CYC
619 result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
620 bcb, cbwlen, NULL);
871f8477 621 /* pr_info("Bulk command transfer result=%d\n", result); */
126bb03b
AC
622 if (result != USB_STOR_XFER_GOOD)
623 return USB_STOR_TRANSPORT_ERROR;
624
625 if (unlikely(us->fflags & US_FL_GO_SLOW))
626 udelay(125);
627
3aa4fc58
CYC
628 /* R/W data */
629 if (transfer_length) {
d48e5cff
CYC
630 unsigned int pipe;
631 if (srb->sc_data_direction == DMA_FROM_DEVICE)
632 pipe = us->recv_bulk_pipe;
633 else
634 pipe = us->send_bulk_pipe;
635
126bb03b 636 result = usb_stor_bulk_srb(us, pipe, srb);
871f8477 637 /* pr_info("Bulk data transfer result 0x%x\n", result); */
126bb03b
AC
638 if (result == USB_STOR_XFER_ERROR)
639 return USB_STOR_TRANSPORT_ERROR;
640
641 if (result == USB_STOR_XFER_LONG)
642 fake_sense = 1;
643 }
644
645 /* get CSW for device status */
871f8477 646 /* pr_info("Attempting to get CSW...\n"); */
3aa4fc58
CYC
647 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
648 US_BULK_CS_WRAP_LEN, &cswlen);
126bb03b 649
3aa4fc58 650 if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
871f8477 651 /* pr_info("Received 0-length CSW; retrying...\n"); */
3aa4fc58
CYC
652 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
653 US_BULK_CS_WRAP_LEN, &cswlen);
126bb03b
AC
654 }
655
656 /* did the attempt to read the CSW fail? */
3aa4fc58 657 if (result == USB_STOR_XFER_STALLED) {
126bb03b 658 /* get the status again */
871f8477 659 /* pr_info("Attempting to get CSW (2nd try)...\n"); */
3aa4fc58
CYC
660 result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
661 US_BULK_CS_WRAP_LEN, NULL);
126bb03b
AC
662 }
663
664 /* if we still have a failure at this point, we're in trouble */
871f8477 665 /* pr_info("Bulk status result = %d\n", result); */
126bb03b
AC
666 if (result != USB_STOR_XFER_GOOD)
667 return USB_STOR_TRANSPORT_ERROR;
668
669 /* check bulk status */
670 residue = le32_to_cpu(bcs->Residue);
871f8477
CYC
671 /* pr_info("Bulk Status S 0x%x T 0x%x R %u Stat 0x%x\n",
672 le32_to_cpu(bcs->Signature),
673 bcs->Tag, residue, bcs->Status); */
3aa4fc58
CYC
674 if (!(bcs->Tag == us->tag ||
675 (us->fflags & US_FL_BULK_IGNORE_TAG)) ||
676 bcs->Status > US_BULK_STAT_PHASE) {
871f8477 677 /* pr_info("Bulk logical error\n"); */
126bb03b
AC
678 return USB_STOR_TRANSPORT_ERROR;
679 }
680
3aa4fc58 681 if (!us->bcs_signature) {
126bb03b 682 us->bcs_signature = bcs->Signature;
3aa4fc58 683 /* if (us->bcs_signature != cpu_to_le32(US_BULK_CS_SIGN)) */
871f8477
CYC
684 /* pr_info("Learnt BCS signature 0x%08X\n",
685 le32_to_cpu(us->bcs_signature)); */
3aa4fc58 686 } else if (bcs->Signature != us->bcs_signature) {
871f8477 687 /* pr_info("Signature mismatch: got %08X, expecting %08X\n",
126bb03b 688 le32_to_cpu(bcs->Signature),
871f8477 689 le32_to_cpu(us->bcs_signature)); */
126bb03b
AC
690 return USB_STOR_TRANSPORT_ERROR;
691 }
692
693 /* try to compute the actual residue, based on how much data
694 * was really transferred and what the device tells us */
3aa4fc58 695 if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
126bb03b
AC
696
697 /* Heuristically detect devices that generate bogus residues
698 * by seeing what happens with INQUIRY and READ CAPACITY
699 * commands.
700 */
701 if (bcs->Status == US_BULK_STAT_OK &&
702 scsi_get_resid(srb) == 0 &&
703 ((srb->cmnd[0] == INQUIRY &&
704 transfer_length == 36) ||
705 (srb->cmnd[0] == READ_CAPACITY &&
3aa4fc58 706 transfer_length == 8))) {
126bb03b
AC
707 us->fflags |= US_FL_IGNORE_RESIDUE;
708
3aa4fc58 709 } else {
126bb03b 710 residue = min(residue, transfer_length);
3aa4fc58
CYC
711 scsi_set_resid(srb, max(scsi_get_resid(srb),
712 (int) residue));
126bb03b
AC
713 }
714 }
715
716 /* based on the status code, we report good or bad */
3aa4fc58
CYC
717 switch (bcs->Status) {
718 case US_BULK_STAT_OK:
719 if (fake_sense) {
720 memcpy(srb->sense_buffer, usb_stor_sense_invalidCDB,
721 sizeof(usb_stor_sense_invalidCDB));
722 return USB_STOR_TRANSPORT_NO_SENSE;
723 }
724 return USB_STOR_TRANSPORT_GOOD;
725
726 case US_BULK_STAT_FAIL:
727 return USB_STOR_TRANSPORT_FAILED;
728
729 case US_BULK_STAT_PHASE:
730 return USB_STOR_TRANSPORT_ERROR;
126bb03b
AC
731 }
732 return USB_STOR_TRANSPORT_ERROR;
733}
734
735/***********************************************************************
736 * Reset routines
737 ***********************************************************************/
3aa4fc58
CYC
738/*
739 * usb_stor_reset_common()
740 */
126bb03b
AC
741static int usb_stor_reset_common(struct us_data *us,
742 u8 request, u8 requesttype,
743 u16 value, u16 index, void *data, u16 size)
744{
745 int result;
746 int result2;
747
871f8477 748 /* pr_info("transport --- usb_stor_reset_common\n"); */
3aa4fc58 749 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
871f8477 750 /* pr_info("No reset during disconnect\n"); */
126bb03b
AC
751 return -EIO;
752 }
753
3aa4fc58
CYC
754 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
755 request, requesttype, value, index, data, size, 5*HZ);
756
757 if (result < 0) {
871f8477 758 /* pr_info("Soft reset failed: %d\n", result); */
126bb03b
AC
759 return result;
760 }
761
3aa4fc58
CYC
762 wait_event_interruptible_timeout(us->delay_wait,
763 test_bit(US_FLIDX_DISCONNECTING, &us->dflags), HZ*6);
764
765 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
871f8477 766 /* pr_info("Reset interrupted by disconnect\n"); */
126bb03b
AC
767 return -EIO;
768 }
769
871f8477 770 /* pr_info("Soft reset: clearing bulk-in endpoint halt\n"); */
126bb03b
AC
771 result = usb_stor_clear_halt(us, us->recv_bulk_pipe);
772
871f8477 773 /* pr_info("Soft reset: clearing bulk-out endpoint halt\n"); */
126bb03b
AC
774 result2 = usb_stor_clear_halt(us, us->send_bulk_pipe);
775
776 /* return a result code based on the result of the clear-halts */
777 if (result >= 0)
778 result = result2;
3aa4fc58 779 /* if (result < 0) */
871f8477 780 /* pr_info("Soft reset failed\n"); */
3aa4fc58 781 /* else */
871f8477 782 /* pr_info("Soft reset done\n"); */
126bb03b
AC
783 return result;
784}
785
3aa4fc58
CYC
786/*
787 * usb_stor_Bulk_reset()
788 */
126bb03b
AC
789int usb_stor_Bulk_reset(struct us_data *us)
790{
871f8477 791 /* pr_info("transport --- usb_stor_Bulk_reset\n"); */
126bb03b
AC
792 return usb_stor_reset_common(us, US_BULK_RESET_REQUEST,
793 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
794 0, us->ifnum, NULL, 0);
795}
796
3aa4fc58
CYC
797/*
798 * usb_stor_port_reset()
799 */
126bb03b
AC
800int usb_stor_port_reset(struct us_data *us)
801{
a200adb1 802 int result;
126bb03b 803
871f8477 804 /* pr_info("transport --- usb_stor_port_reset\n"); */
e1049604 805 result = usb_lock_device_for_reset(us->pusb_dev, us->pusb_intf);
126bb03b 806 if (result < 0)
871f8477 807 pr_info("unable to lock device for reset: %d\n", result);
e1049604 808 else {
126bb03b 809 /* Were we disconnected while waiting for the lock? */
e1049604 810 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
126bb03b 811 result = -EIO;
871f8477 812 /* pr_info("No reset during disconnect\n"); */
e1049604 813 } else {
126bb03b 814 result = usb_reset_device(us->pusb_dev);
871f8477
CYC
815 /* pr_info("usb_reset_composite_device returns %d\n",
816 result); */
126bb03b 817 }
e1049604 818 usb_unlock_device(us->pusb_dev);
126bb03b
AC
819 }
820 return result;
821}
822
823