1 /* Driver for USB Mass Storage compliant devices
3 * $Id: transport.c,v 1.44 2002/02/25 00:43:41 mdharm Exp $
5 * Current development and maintenance by:
6 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8 * Developed with the assistance of:
9 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
10 * (c) 2000 Stephen J. Gowdy (SGowdy@lbl.gov)
11 * (c) 2002 Alan Stern <stern@rowland.org>
14 * (c) 1999 Michael Gee (michael@linuxspecific.com)
16 * This driver is based on the 'USB Mass Storage Class' document. This
17 * describes in detail the protocol used to communicate with such
18 * devices. Clearly, the designers had SCSI and ATAPI commands in
19 * mind when they created this document. The commands are all very
20 * similar to commands in the SCSI-II and ATAPI specifications.
22 * It is important to note that in a number of cases this class
23 * exhibits class-specific exemptions from the USB specification.
24 * Notably the usage of NAK, STALL and ACK differs from the norm, in
25 * that they are used to communicate wait, failed and OK on commands.
27 * Also, for certain devices, the interrupt endpoint is used to convey
28 * status of a command.
30 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
31 * information about this driver.
33 * This program is free software; you can redistribute it and/or modify it
34 * under the terms of the GNU General Public License as published by the
35 * Free Software Foundation; either version 2, or (at your option) any
38 * This program is distributed in the hope that it will be useful, but
39 * WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41 * General Public License for more details.
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 675 Mass Ave, Cambridge, MA 02139, USA.
48 #include <linux/config.h>
49 #include "transport.h"
54 #include <linux/sched.h>
55 #include <linux/errno.h>
56 #include <linux/slab.h>
58 /***********************************************************************
60 ***********************************************************************/
62 /* Calculate the length of the data transfer (not the command) for any
65 unsigned int usb_stor_transfer_length(Scsi_Cmnd *srb)
70 unsigned int total = 0;
71 struct scatterlist *sg;
73 /* This table tells us:
74 X = command not supported
75 L = return length in cmnd[4] (8 bits).
76 M = return length in cmnd[8] (8 bits).
77 G = return length in cmnd[3] and cmnd[4] (16 bits)
78 H = return length in cmnd[7] and cmnd[8] (16 bits)
79 I = return length in cmnd[8] and cmnd[9] (16 bits)
80 C = return length in cmnd[2] to cmnd[5] (32 bits)
81 D = return length in cmnd[6] to cmnd[9] (32 bits)
82 B = return length in blocksize so we use buff_len
83 R = return length in cmnd[2] to cmnd[4] (24 bits)
84 S = return length in cmnd[3] to cmnd[5] (24 bits)
85 T = return length in cmnd[6] to cmnd[8] (24 bits)
86 U = return length in cmnd[7] to cmnd[9] (24 bits)
87 0-9 = fixed return length
90 Z = return length is mode dependant or not in command, use buff_len
93 static char *lengths =
95 /* 0123456789ABCDEF 0123456789ABCDEF */
97 "00XLZ6XZBXBBXXXB" "00LBBLG0R0L0GG0X" /* 00-1F */
98 "XXXXT8XXB4B0BBBB" "ZZZ0B00HCSSZTBHH" /* 20-3F */
99 "M0HHB0X000H0HH0X" "XHH0HHXX0TH0H0XX" /* 40-5F */
100 "XXXXXXXXXXXXXXXX" "XXXXXXXXXXXXXXXX" /* 60-7F */
101 "XXXXXXXXXXXXXXXX" "XXXXXXXXXXXXXXXX" /* 80-9F */
102 "X0XXX00XB0BXBXBB" "ZZZ0XUIDU000XHBX" /* A0-BF */
103 "XXXXXXXXXXXXXXXX" "XXXXXXXXXXXXXXXX" /* C0-DF */
104 "XDXXXXXXXXXXXXXX" "XXW00HXXXXXXXXXX"; /* E0-FF */
106 /* Commands checked in table:
117 GET_DATA_BUFFER_STATUS 34
120 GET_WINDOW 25 !!! Has more data than READ_CAPACITY, need to fix table
121 INITIALIZE_ELEMENT_STATUS 07 !!! REASSIGN_BLOCKS luckily uses buff_len
128 MEDIUM_SCAN 38 !!! This was M
134 OBJECT_POSITION 31 !!! Same as SEARCH_DATA_EQUAL
139 PLAY_AUDIO_TRACK_INDEX 48
140 PLAY_AUDIO_TRACK_RELATIVE_10 49
141 PLAY_AUDIO_TRACK_RELATIVE_12 a9
142 POSITION_TO_ELEMENT 2b
144 PREVENT_ALLOW_MEDIUM_REMOVAL 1e
145 PRINT 0a !!! Same as WRITE_6 but is always in bytes
152 READ_CDROM_CAPACITY 25
154 READ_DEFECT_DATA_12 b7
155 READ_ELEMENT_STATUS b8 !!! Think this is in bytes
156 READ_GENERATION 29 !!! Could also be M?
157 READ_HEADER 44 !!! This was L
159 READ_POSITION 34 !!! This should be V but conflicts with PRE-FETCH
161 READ_SUB-CHANNEL 42 !!! Is this in bytes?
162 READ_TOC 43 !!! Is this in bytes?
163 READ_UPDATED_BLOCK 2d
165 RECEIVE 08 !!! Same as READ_6 probably in bytes though
166 RECEIVE_DIAGNOSTIC_RESULTS 1c
167 RECOVER_BUFFERED_DATA 14 !!! For PRINTERs this is bytes
170 REQUEST_VOLUME_ELEMENT_ADDRESS b5 !!! Think this is in bytes
174 SCAN 1b !!! Conflicts with various commands, should be L
176 SEARCH_DATA_EQUAL_12 b1
178 SEARCH_DATA_LOW_12 b0
180 SEARCH_DATA_HIGH_12 b2
181 SEEK_6 0b !!! Conflicts with SLEW_AND_PRINT
183 SEND 0a !!! Same as WRITE_6, probably in bytes though
184 SEND 2a !!! Similar to WRITE_10 but for scanners
186 SEND_MESSAGE_6 0a !!! Same as WRITE_6 - is in bytes
187 SEND_MESSAGE_10 2a !!! Same as WRITE_10 - is in bytes
188 SEND_MESSAGE_12 aa !!! Same as WRITE_12 - is in bytes
190 SEND_VOLUME_TAG b6 !!! Think this is in bytes
194 SLEW_AND_PRINT 0b !!! Conflicts with SEEK_6
198 SYNCHRONIZE_BUFFER 10
209 WRITE_AND_VERIFY_12 ae
216 if (srb->sc_data_direction == SCSI_DATA_WRITE) {
220 switch (lengths[srb->cmnd[0]]) {
239 len = lengths[srb->cmnd[0]]-'0';
243 len = (((unsigned int)srb->cmnd[3])<<8) |
248 len = (((unsigned int)srb->cmnd[7])<<8) |
253 len = (((unsigned int)srb->cmnd[8])<<8) |
258 len = (((unsigned int)srb->cmnd[2])<<16) |
259 (((unsigned int)srb->cmnd[3])<<8) |
264 len = (((unsigned int)srb->cmnd[3])<<16) |
265 (((unsigned int)srb->cmnd[4])<<8) |
270 len = (((unsigned int)srb->cmnd[6])<<16) |
271 (((unsigned int)srb->cmnd[7])<<8) |
276 len = (((unsigned int)srb->cmnd[7])<<16) |
277 (((unsigned int)srb->cmnd[8])<<8) |
282 len = (((unsigned int)srb->cmnd[2])<<24) |
283 (((unsigned int)srb->cmnd[3])<<16) |
284 (((unsigned int)srb->cmnd[4])<<8) |
289 len = (((unsigned int)srb->cmnd[6])<<24) |
290 (((unsigned int)srb->cmnd[7])<<16) |
291 (((unsigned int)srb->cmnd[8])<<8) |
304 /* Use buffer size due to different block sizes */
309 US_DEBUGP("Error: UNSUPPORTED COMMAND %02X\n",
315 /* Use buffer size due to mode dependence */
320 US_DEBUGP("Error: COMMAND %02X out of range or table inconsistent (%c).\n",
321 srb->cmnd[0], lengths[srb->cmnd[0]] );
325 if ( doDefault == 1 ) {
326 /* Are we going to scatter gather? */
328 /* Add up the sizes of all the sg segments */
329 sg = (struct scatterlist *) srb->request_buffer;
330 for (i = 0; i < srb->use_sg; i++)
331 total += sg[i].length;
335 /* Just return the length of the buffer */
336 len = srb->request_bufflen;
342 /***********************************************************************
343 * Data transfer routines
344 ***********************************************************************/
346 /* This is the completion handler which will wake us up when an URB
349 static void usb_stor_blocking_completion(struct urb *urb)
351 struct completion *urb_done_ptr = (struct completion *)urb->context;
353 complete(urb_done_ptr);
356 /* This is our function to emulate usb_control_msg() but give us enough
357 * access to make aborts/resets work
359 int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
360 u8 request, u8 requesttype, u16 value, u16 index,
361 void *data, u16 size)
364 struct usb_ctrlrequest *dr;
366 /* allocate the device request structure */
367 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
371 /* fill in the structure */
372 dr->bRequestType = requesttype;
373 dr->bRequest = request;
374 dr->wValue = cpu_to_le16(value);
375 dr->wIndex = cpu_to_le16(index);
376 dr->wLength = cpu_to_le16(size);
378 /* set up data structures for the wakeup system */
379 init_completion(&us->current_done);
382 down(&(us->current_urb_sem));
385 FILL_CONTROL_URB(us->current_urb, us->pusb_dev, pipe,
386 (unsigned char*) dr, data, size,
387 usb_stor_blocking_completion, &us->current_done);
388 us->current_urb->actual_length = 0;
389 us->current_urb->error_count = 0;
390 us->current_urb->transfer_flags = USB_ASYNC_UNLINK;
391 us->current_urb->status = 0;
394 status = usb_submit_urb(us->current_urb);
396 /* something went wrong */
397 up(&(us->current_urb_sem));
402 /* wait for the completion of the URB */
403 up(&(us->current_urb_sem));
404 wait_for_completion(&us->current_done);
405 down(&(us->current_urb_sem));
407 /* return the actual length of the data transferred if no error*/
408 status = us->current_urb->status;
409 if (status == -ENOENT)
410 status = -ECONNRESET;
412 status = us->current_urb->actual_length;
414 /* release the lock and return status */
415 up(&(us->current_urb_sem));
420 /* This is our function to emulate usb_bulk_msg() but give us enough
421 * access to make aborts/resets work
423 int usb_stor_bulk_msg(struct us_data *us, void *data, int pipe,
424 unsigned int len, unsigned int *act_len)
428 /* set up data structures for the wakeup system */
429 init_completion(&us->current_done);
432 down(&(us->current_urb_sem));
435 FILL_BULK_URB(us->current_urb, us->pusb_dev, pipe, data, len,
436 usb_stor_blocking_completion, &us->current_done);
437 us->current_urb->actual_length = 0;
438 us->current_urb->error_count = 0;
439 us->current_urb->transfer_flags = USB_ASYNC_UNLINK;
440 us->current_urb->status = 0;
443 status = usb_submit_urb(us->current_urb);
445 /* something went wrong */
446 up(&(us->current_urb_sem));
450 /* wait for the completion of the URB */
451 up(&(us->current_urb_sem));
452 wait_for_completion(&us->current_done);
453 down(&(us->current_urb_sem));
454 if (us->current_urb->status == -ENOENT)
455 us->current_urb->status = -ECONNRESET;
457 /* return the actual length of the data transferred */
458 *act_len = us->current_urb->actual_length;
460 /* release the lock and return status */
461 up(&(us->current_urb_sem));
462 return us->current_urb->status;
465 /* This is a version of usb_clear_halt() that doesn't read the status from
466 * the device -- this is because some devices crash their internal firmware
467 * when the status is requested after a halt
469 int usb_stor_clear_halt(struct us_data *us, int pipe)
472 int endp = usb_pipeendpoint(pipe) | (usb_pipein(pipe) << 7);
474 result = usb_stor_control_msg(us,
475 usb_sndctrlpipe(us->pusb_dev, 0),
476 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0,
477 endp, NULL, 0); /* note: no 3*HZ timeout */
478 US_DEBUGP("usb_stor_clear_halt: result=%d\n", result);
480 /* this is a failure case */
484 /* reset the toggles and endpoint flags */
485 usb_endpoint_running(us->pusb_dev, usb_pipeendpoint(pipe),
487 usb_settoggle(us->pusb_dev, usb_pipeendpoint(pipe),
488 usb_pipeout(pipe), 0);
494 * Transfer one SCSI scatter-gather buffer via bulk transfer
496 * Note that this function is necessary because we want the ability to
497 * use scatter-gather memory. Good performance is achieved by a combination
498 * of scatter-gather and clustering (which makes each chunk bigger).
500 * Note that the lower layer will always retry when a NAK occurs, up to the
501 * timeout limit. Thus we don't have to worry about it for individual
504 int usb_stor_transfer_partial(struct us_data *us, char *buf, int length)
510 /* calculate the appropriate pipe information */
511 if (us->srb->sc_data_direction == SCSI_DATA_READ)
512 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
514 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
516 /* transfer the data */
517 US_DEBUGP("usb_stor_transfer_partial(): xfer %d bytes\n", length);
518 result = usb_stor_bulk_msg(us, buf, pipe, length, &partial);
519 US_DEBUGP("usb_stor_bulk_msg() returned %d xferred %d/%d\n",
520 result, partial, length);
522 /* if we stall, we need to clear it before we go on */
523 if (result == -EPIPE) {
524 US_DEBUGP("clearing endpoint halt for pipe 0x%x\n", pipe);
525 usb_stor_clear_halt(us, pipe);
528 /* did we abort this command? */
529 if (result == -ECONNRESET) {
530 US_DEBUGP("usb_stor_transfer_partial(): transfer aborted\n");
531 return US_BULK_TRANSFER_ABORTED;
534 /* did we send all the data? */
535 if (partial == length) {
536 US_DEBUGP("usb_stor_transfer_partial(): transfer complete\n");
537 return US_BULK_TRANSFER_GOOD;
540 /* NAK - that means we've retried a few times already */
541 if (result == -ETIMEDOUT) {
542 US_DEBUGP("usb_stor_transfer_partial(): device NAKed\n");
543 return US_BULK_TRANSFER_FAILED;
546 /* the catch-all error case */
548 US_DEBUGP("usb_stor_transfer_partial(): unknown error\n");
549 return US_BULK_TRANSFER_FAILED;
552 /* no error code, so we must have transferred some data,
553 * just not all of it */
554 return US_BULK_TRANSFER_SHORT;
558 * Transfer an entire SCSI command's worth of data payload over the bulk
561 * Note that this uses usb_stor_transfer_partial to achieve its goals -- this
562 * function simply determines if we're going to use scatter-gather or not,
563 * and acts appropriately. For now, it also re-interprets the error codes.
565 void usb_stor_transfer(Scsi_Cmnd *srb, struct us_data* us)
569 struct scatterlist *sg;
570 unsigned int total_transferred = 0;
571 unsigned int transfer_amount;
573 /* calculate how much we want to transfer */
574 transfer_amount = usb_stor_transfer_length(srb);
576 /* was someone foolish enough to request more data than available
578 if (transfer_amount > srb->request_bufflen)
579 transfer_amount = srb->request_bufflen;
581 /* are we scatter-gathering? */
584 /* loop over all the scatter gather structures and
585 * make the appropriate requests for each, until done
587 sg = (struct scatterlist *) srb->request_buffer;
588 for (i = 0; i < srb->use_sg; i++) {
590 /* transfer the lesser of the next buffer or the
592 if (transfer_amount - total_transferred >=
594 result = usb_stor_transfer_partial(us,
595 sg[i].address, sg[i].length);
596 total_transferred += sg[i].length;
598 result = usb_stor_transfer_partial(us,
600 transfer_amount - total_transferred);
602 /* if we get an error, end the loop here */
608 /* no scatter-gather, just make the request */
609 result = usb_stor_transfer_partial(us, srb->request_buffer,
612 /* return the result in the data structure itself */
613 srb->result = result;
616 /***********************************************************************
618 ***********************************************************************/
620 /* Invoke the transport and basic error-handling/recovery methods
622 * This is used by the protocol layers to actually send the message to
623 * the device and receive the response.
625 void usb_stor_invoke_transport(Scsi_Cmnd *srb, struct us_data *us)
631 * Grab device's exclusive access lock to prevent libusb/usbfs from
632 * sending out a command in the middle of ours (if libusb sends a
633 * get_descriptor or something on pipe 0 after our CBW and before
634 * our CSW, and then we get a stall, we have trouble).
636 usb_excl_lock(us->pusb_dev, 3, 0);
638 /* send the command to the transport layer */
639 result = us->transport(srb, us);
640 usb_excl_unlock(us->pusb_dev, 3);
642 /* if the command gets aborted by the higher layers, we need to
643 * short-circuit all other processing
645 if (result == USB_STOR_TRANSPORT_ABORTED) {
646 US_DEBUGP("-- transport indicates command was aborted\n");
647 srb->result = DID_ABORT << 16;
649 /* Bulk-only aborts require a device reset */
650 if (us->protocol == US_PR_BULK)
651 us->transport_reset(us);
655 /* if there is a transport error, reset and don't auto-sense */
656 if (result == USB_STOR_TRANSPORT_ERROR) {
657 US_DEBUGP("-- transport indicates error, resetting\n");
658 us->transport_reset(us);
659 srb->result = DID_ERROR << 16;
663 /* Determine if we need to auto-sense
665 * I normally don't use a flag like this, but it's almost impossible
666 * to understand what's going on here if I don't.
671 * If we're running the CB transport, which is incapable
672 * of determining status on it's own, we need to auto-sense almost
675 if (us->protocol == US_PR_CB || us->protocol == US_PR_DPCM_USB) {
676 US_DEBUGP("-- CB transport device requiring auto-sense\n");
679 /* There are some exceptions to this. Notably, if this is
680 * a UFI device and the command is REQUEST_SENSE or INQUIRY,
681 * then it is impossible to truly determine status.
683 if (us->subclass == US_SC_UFI &&
684 ((srb->cmnd[0] == REQUEST_SENSE) ||
685 (srb->cmnd[0] == INQUIRY))) {
686 US_DEBUGP("** no auto-sense for a special command\n");
692 * If we have a failure, we're going to do a REQUEST_SENSE
693 * automatically. Note that we differentiate between a command
694 * "failure" and an "error" in the transport mechanism.
696 if (result == USB_STOR_TRANSPORT_FAILED) {
697 US_DEBUGP("-- transport indicates command failure\n");
702 * Also, if we have a short transfer on a command that can't have
703 * a short transfer, we're going to do this.
705 if ((srb->result == US_BULK_TRANSFER_SHORT) &&
706 !((srb->cmnd[0] == REQUEST_SENSE) ||
707 (srb->cmnd[0] == INQUIRY) ||
708 (srb->cmnd[0] == MODE_SENSE) ||
709 (srb->cmnd[0] == LOG_SENSE) ||
710 (srb->cmnd[0] == MODE_SENSE_10))) {
711 US_DEBUGP("-- unexpectedly short transfer\n");
715 /* Now, if we need to do the auto-sense, let's do it */
716 if (need_auto_sense) {
718 void* old_request_buffer;
719 unsigned short old_sg;
720 unsigned old_request_bufflen;
721 unsigned char old_sc_data_direction;
722 unsigned char old_cmd_len;
723 unsigned char old_cmnd[MAX_COMMAND_SIZE];
725 US_DEBUGP("Issuing auto-REQUEST_SENSE\n");
727 /* save the old command */
728 memcpy(old_cmnd, srb->cmnd, MAX_COMMAND_SIZE);
729 old_cmd_len = srb->cmd_len;
731 /* set the command and the LUN */
732 memset(srb->cmnd, 0, MAX_COMMAND_SIZE);
733 srb->cmnd[0] = REQUEST_SENSE;
734 srb->cmnd[1] = old_cmnd[1] & 0xE0;
737 /* FIXME: we must do the protocol translation here */
738 if (us->subclass == US_SC_RBC || us->subclass == US_SC_SCSI)
743 /* set the transfer direction */
744 old_sc_data_direction = srb->sc_data_direction;
745 srb->sc_data_direction = SCSI_DATA_READ;
747 /* use the new buffer we have */
748 old_request_buffer = srb->request_buffer;
749 srb->request_buffer = srb->sense_buffer;
751 /* set the buffer length for transfer */
752 old_request_bufflen = srb->request_bufflen;
753 srb->request_bufflen = 18;
755 /* set up for no scatter-gather use */
756 old_sg = srb->use_sg;
759 /* issue the auto-sense command */
760 usb_excl_lock(us->pusb_dev, 3, 0);
761 temp_result = us->transport(us->srb, us);
762 usb_excl_unlock(us->pusb_dev, 3);
764 /* let's clean up right away */
765 srb->request_buffer = old_request_buffer;
766 srb->request_bufflen = old_request_bufflen;
767 srb->use_sg = old_sg;
768 srb->sc_data_direction = old_sc_data_direction;
769 srb->cmd_len = old_cmd_len;
770 memcpy(srb->cmnd, old_cmnd, MAX_COMMAND_SIZE);
772 if (temp_result == USB_STOR_TRANSPORT_ABORTED) {
773 US_DEBUGP("-- auto-sense aborted\n");
774 srb->result = DID_ABORT << 16;
777 if (temp_result != USB_STOR_TRANSPORT_GOOD) {
778 US_DEBUGP("-- auto-sense failure\n");
780 /* we skip the reset if this happens to be a
781 * multi-target device, since failure of an
782 * auto-sense is perfectly valid
784 if (!(us->flags & US_FL_SCM_MULT_TARG)) {
785 us->transport_reset(us);
787 srb->result = DID_ERROR << 16;
791 US_DEBUGP("-- Result from auto-sense is %d\n", temp_result);
792 US_DEBUGP("-- code: 0x%x, key: 0x%x, ASC: 0x%x, ASCQ: 0x%x\n",
793 srb->sense_buffer[0],
794 srb->sense_buffer[2] & 0xf,
795 srb->sense_buffer[12],
796 srb->sense_buffer[13]);
797 #ifdef CONFIG_USB_STORAGE_DEBUG
799 srb->sense_buffer[2] & 0xf,
800 srb->sense_buffer[12],
801 srb->sense_buffer[13]);
804 /* set the result so the higher layers expect this data */
805 srb->result = CHECK_CONDITION << 1;
807 /* If things are really okay, then let's show that */
808 if ((srb->sense_buffer[2] & 0xf) == 0x0)
809 srb->result = GOOD << 1;
810 } else /* if (need_auto_sense) */
811 srb->result = GOOD << 1;
813 /* Regardless of auto-sense, if we _know_ we have an error
814 * condition, show that in the result code
816 if (result == USB_STOR_TRANSPORT_FAILED)
817 srb->result = CHECK_CONDITION << 1;
819 /* If we think we're good, then make sure the sense data shows it.
820 * This is necessary because the auto-sense for some devices always
821 * sets byte 0 == 0x70, even if there is no error
823 if ((us->protocol == US_PR_CB || us->protocol == US_PR_DPCM_USB) &&
824 (result == USB_STOR_TRANSPORT_GOOD) &&
825 ((srb->sense_buffer[2] & 0xf) == 0x0))
826 srb->sense_buffer[0] = 0x0;
830 * Control/Bulk/Interrupt transport
833 /* The interrupt handler for CBI devices */
834 void usb_stor_CBI_irq(struct urb *urb)
836 struct us_data *us = (struct us_data *)urb->context;
838 US_DEBUGP("USB IRQ received for device on host %d\n", us->host_no);
839 US_DEBUGP("-- IRQ data length is %d\n", urb->actual_length);
840 US_DEBUGP("-- IRQ state is %d\n", urb->status);
841 US_DEBUGP("-- Interrupt Status (0x%x, 0x%x)\n",
842 us->irqbuf[0], us->irqbuf[1]);
844 /* reject improper IRQs */
845 if (urb->actual_length != 2) {
846 US_DEBUGP("-- IRQ too short\n");
850 /* is the device removed? */
851 if (urb->status == -ENODEV) {
852 US_DEBUGP("-- device has been removed\n");
856 /* was this a command-completion interrupt? */
857 if (us->irqbuf[0] && (us->subclass != US_SC_UFI)) {
858 US_DEBUGP("-- not a command-completion IRQ\n");
862 /* was this a wanted interrupt? */
863 if (!atomic_read(us->ip_wanted)) {
864 US_DEBUGP("ERROR: Unwanted interrupt received!\n");
868 /* adjust the flag */
869 atomic_set(us->ip_wanted, 0);
871 /* copy the valid data */
872 us->irqdata[0] = us->irqbuf[0];
873 us->irqdata[1] = us->irqbuf[1];
875 /* wake up the command thread */
876 US_DEBUGP("-- Current value of ip_waitq is: %d\n",
877 atomic_read(&us->ip_waitq.count));
881 int usb_stor_CBI_transport(Scsi_Cmnd *srb, struct us_data *us)
885 /* Set up for status notification */
886 atomic_set(us->ip_wanted, 1);
888 /* re-initialize the mutex so that we avoid any races with
889 * early/late IRQs from previous commands */
890 init_MUTEX_LOCKED(&(us->ip_waitq));
893 /* let's send the command via the control pipe */
894 result = usb_stor_control_msg(us, usb_sndctrlpipe(us->pusb_dev,0),
896 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
897 us->ifnum, srb->cmnd, srb->cmd_len);
899 /* check the return code for the command */
900 US_DEBUGP("Call to usb_stor_control_msg() returned %d\n", result);
902 /* Reset flag for status notification */
903 atomic_set(us->ip_wanted, 0);
906 /* if the command was aborted, indicate that */
907 if (result == -ECONNRESET)
908 return USB_STOR_TRANSPORT_ABORTED;
910 /* STALL must be cleared when it is detected */
911 if (result == -EPIPE) {
912 US_DEBUGP("-- Stall on control pipe. Clearing\n");
913 result = usb_stor_clear_halt(us,
914 usb_sndctrlpipe(us->pusb_dev, 0));
916 /* if the command was aborted, indicate that */
917 if (result == -ECONNRESET)
918 return USB_STOR_TRANSPORT_ABORTED;
919 return USB_STOR_TRANSPORT_FAILED;
923 /* Uh oh... serious problem here */
924 return USB_STOR_TRANSPORT_ERROR;
928 /* transfer the data payload for this command, if one exists*/
929 if (usb_stor_transfer_length(srb)) {
930 usb_stor_transfer(srb, us);
931 result = srb->result;
932 US_DEBUGP("CBI data stage result is 0x%x\n", result);
934 /* report any errors */
935 if (result == US_BULK_TRANSFER_ABORTED) {
936 atomic_set(us->ip_wanted, 0);
937 return USB_STOR_TRANSPORT_ABORTED;
939 if (result == US_BULK_TRANSFER_FAILED) {
940 atomic_set(us->ip_wanted, 0);
941 return USB_STOR_TRANSPORT_FAILED;
947 /* go to sleep until we get this interrupt */
948 US_DEBUGP("Current value of ip_waitq is: %d\n", atomic_read(&us->ip_waitq.count));
949 down(&(us->ip_waitq));
951 /* if we were woken up by an abort instead of the actual interrupt */
952 if (atomic_read(us->ip_wanted)) {
953 US_DEBUGP("Did not get interrupt on CBI\n");
954 atomic_set(us->ip_wanted, 0);
955 return USB_STOR_TRANSPORT_ABORTED;
958 US_DEBUGP("Got interrupt data (0x%x, 0x%x)\n",
959 us->irqdata[0], us->irqdata[1]);
961 /* UFI gives us ASC and ASCQ, like a request sense
963 * REQUEST_SENSE and INQUIRY don't affect the sense data on UFI
964 * devices, so we ignore the information for those commands. Note
965 * that this means we could be ignoring a real error on these
966 * commands, but that can't be helped.
968 if (us->subclass == US_SC_UFI) {
969 if (srb->cmnd[0] == REQUEST_SENSE ||
970 srb->cmnd[0] == INQUIRY)
971 return USB_STOR_TRANSPORT_GOOD;
973 if (((unsigned char*)us->irq_urb->transfer_buffer)[0])
974 return USB_STOR_TRANSPORT_FAILED;
976 return USB_STOR_TRANSPORT_GOOD;
979 /* If not UFI, we interpret the data as a result code
980 * The first byte should always be a 0x0
981 * The second byte & 0x0F should be 0x0 for good, otherwise error
983 if (us->irqdata[0]) {
984 US_DEBUGP("CBI IRQ data showed reserved bType %d\n",
986 return USB_STOR_TRANSPORT_ERROR;
989 switch (us->irqdata[1] & 0x0F) {
991 return USB_STOR_TRANSPORT_GOOD;
993 return USB_STOR_TRANSPORT_FAILED;
995 return USB_STOR_TRANSPORT_ERROR;
998 /* we should never get here, but if we do, we're in trouble */
999 return USB_STOR_TRANSPORT_ERROR;
1003 * Control/Bulk transport
1005 int usb_stor_CB_transport(Scsi_Cmnd *srb, struct us_data *us)
1010 /* let's send the command via the control pipe */
1011 result = usb_stor_control_msg(us, usb_sndctrlpipe(us->pusb_dev,0),
1013 USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
1014 us->ifnum, srb->cmnd, srb->cmd_len);
1016 /* check the return code for the command */
1017 US_DEBUGP("Call to usb_stor_control_msg() returned %d\n", result);
1019 /* if the command was aborted, indicate that */
1020 if (result == -ECONNRESET)
1021 return USB_STOR_TRANSPORT_ABORTED;
1023 /* a stall is a fatal condition from the device */
1024 if (result == -EPIPE) {
1025 US_DEBUGP("-- Stall on control pipe. Clearing\n");
1026 result = usb_stor_clear_halt(us,
1027 usb_sndctrlpipe(us->pusb_dev, 0));
1029 /* if the command was aborted, indicate that */
1030 if (result == -ECONNRESET)
1031 return USB_STOR_TRANSPORT_ABORTED;
1032 return USB_STOR_TRANSPORT_FAILED;
1035 /* Uh oh... serious problem here */
1036 return USB_STOR_TRANSPORT_ERROR;
1040 /* transfer the data payload for this command, if one exists*/
1041 if (usb_stor_transfer_length(srb)) {
1042 usb_stor_transfer(srb, us);
1043 result = srb->result;
1044 US_DEBUGP("CB data stage result is 0x%x\n", result);
1046 /* report any errors */
1047 if (result == US_BULK_TRANSFER_ABORTED) {
1048 return USB_STOR_TRANSPORT_ABORTED;
1050 if (result == US_BULK_TRANSFER_FAILED) {
1051 return USB_STOR_TRANSPORT_FAILED;
1056 /* NOTE: CB does not have a status stage. Silly, I know. So
1057 * we have to catch this at a higher level.
1059 return USB_STOR_TRANSPORT_GOOD;
1063 * Bulk only transport
1066 /* Determine what the maximum LUN supported is */
1067 int usb_stor_Bulk_max_lun(struct us_data *us)
1069 unsigned char *data;
1073 data = kmalloc(sizeof *data, GFP_KERNEL);
1078 /* issue the command -- use usb_control_msg() because
1079 * the state machine is not yet alive */
1080 pipe = usb_rcvctrlpipe(us->pusb_dev, 0);
1081 result = usb_control_msg(us->pusb_dev, pipe,
1082 US_BULK_GET_MAX_LUN,
1083 USB_DIR_IN | USB_TYPE_CLASS |
1084 USB_RECIP_INTERFACE,
1085 0, us->ifnum, data, sizeof(*data), HZ);
1087 US_DEBUGP("GetMaxLUN command result is %d, data is %d\n",
1090 /* if we have a successful request, return the result */
1099 /* if we get a STALL, clear the stall */
1100 if (result == -EPIPE) {
1101 US_DEBUGP("clearing endpoint halt for pipe 0x%x\n", pipe);
1103 /* Use usb_clear_halt() because the state machine
1104 * is not yet alive */
1105 usb_clear_halt(us->pusb_dev, pipe);
1108 /* return the default -- no LUNs */
1112 int usb_stor_Bulk_reset(struct us_data *us);
1114 int usb_stor_Bulk_transport(Scsi_Cmnd *srb, struct us_data *us)
1116 struct bulk_cb_wrap *bcb;
1117 struct bulk_cs_wrap *bcs;
1121 int ret = USB_STOR_TRANSPORT_ERROR;
1123 bcb = kmalloc(sizeof *bcb, in_interrupt() ? GFP_ATOMIC : GFP_NOIO);
1125 return USB_STOR_TRANSPORT_ERROR;
1127 bcs = kmalloc(sizeof *bcs, in_interrupt() ? GFP_ATOMIC : GFP_NOIO);
1130 return USB_STOR_TRANSPORT_ERROR;
1133 /* set up the command wrapper */
1134 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1135 bcb->DataTransferLength = cpu_to_le32(usb_stor_transfer_length(srb));
1136 bcb->Flags = srb->sc_data_direction == SCSI_DATA_READ ? 1 << 7 : 0;
1137 bcb->Tag = ++(us->tag);
1138 bcb->Lun = srb->cmnd[1] >> 5;
1139 if (us->flags & US_FL_SCM_MULT_TARG)
1140 bcb->Lun |= srb->target << 4;
1141 bcb->Length = srb->cmd_len;
1143 /* construct the pipe handle */
1144 pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
1146 /* copy the command payload */
1147 memset(bcb->CDB, 0, sizeof(bcb->CDB));
1148 memcpy(bcb->CDB, srb->cmnd, bcb->Length);
1150 /* send it to out endpoint */
1151 US_DEBUGP("Bulk command S 0x%x T 0x%x Trg %d LUN %d L %d F %d CL %d\n",
1152 le32_to_cpu(bcb->Signature), bcb->Tag,
1153 (bcb->Lun >> 4), (bcb->Lun & 0x0F),
1154 le32_to_cpu(bcb->DataTransferLength), bcb->Flags, bcb->Length);
1155 result = usb_stor_bulk_msg(us, bcb, pipe, US_BULK_CB_WRAP_LEN,
1157 US_DEBUGP("Bulk command transfer result=%d\n", result);
1159 /* if the command was aborted, indicate that */
1160 if (result == -ECONNRESET) {
1161 ret = USB_STOR_TRANSPORT_ABORTED;
1165 /* if we stall, we need to clear it before we go on */
1166 if (result == -EPIPE) {
1167 US_DEBUGP("clearing endpoint halt for pipe 0x%x\n", pipe);
1168 result = usb_stor_clear_halt(us, pipe);
1170 /* if the command was aborted, indicate that */
1171 if (result == -ECONNRESET) {
1172 ret = USB_STOR_TRANSPORT_ABORTED;
1176 } else if (result) {
1177 /* unknown error -- we've got a problem */
1178 ret = USB_STOR_TRANSPORT_ERROR;
1182 /* if the command transfered well, then we go to the data stage */
1185 /* Genesys Logic interface chips need a 100us delay between
1186 * the command phase and the data phase. Some systems need
1187 * even more, probably because of clock rate inaccuracies. */
1188 if (us->pusb_dev->descriptor.idVendor == USB_VENDOR_ID_GENESYS)
1191 /* send/receive data payload, if there is any */
1192 if (bcb->DataTransferLength) {
1193 usb_stor_transfer(srb, us);
1194 result = srb->result;
1195 US_DEBUGP("Bulk data transfer result 0x%x\n", result);
1197 /* if it was aborted, we need to indicate that */
1198 if (result == US_BULK_TRANSFER_ABORTED) {
1199 ret = USB_STOR_TRANSPORT_ABORTED;
1205 /* See flow chart on pg 15 of the Bulk Only Transport spec for
1206 * an explanation of how this code works.
1209 /* construct the pipe handle */
1210 pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
1212 /* get CSW for device status */
1213 US_DEBUGP("Attempting to get CSW...\n");
1214 result = usb_stor_bulk_msg(us, bcs, pipe, US_BULK_CS_WRAP_LEN,
1217 /* if the command was aborted, indicate that */
1218 if (result == -ECONNRESET) {
1219 ret = USB_STOR_TRANSPORT_ABORTED;
1223 /* did the attempt to read the CSW fail? */
1224 if (result == -EPIPE) {
1225 US_DEBUGP("clearing endpoint halt for pipe 0x%x\n", pipe);
1226 result = usb_stor_clear_halt(us, pipe);
1228 /* if the command was aborted, indicate that */
1229 if (result == -ECONNRESET) {
1230 ret = USB_STOR_TRANSPORT_ABORTED;
1234 /* get the status again */
1235 US_DEBUGP("Attempting to get CSW (2nd try)...\n");
1236 result = usb_stor_bulk_msg(us, bcs, pipe,
1237 US_BULK_CS_WRAP_LEN, &partial);
1239 /* if the command was aborted, indicate that */
1240 if (result == -ECONNRESET) {
1241 ret = USB_STOR_TRANSPORT_ABORTED;
1245 /* if it fails again, we need a reset and return an error*/
1246 if (result == -EPIPE) {
1247 US_DEBUGP("clearing halt for pipe 0x%x\n", pipe);
1248 result = usb_stor_clear_halt(us, pipe);
1250 /* if the command was aborted, indicate that */
1251 if (result == -ECONNRESET) {
1252 ret = USB_STOR_TRANSPORT_ABORTED;
1254 ret = USB_STOR_TRANSPORT_ERROR;
1260 /* if we still have a failure at this point, we're in trouble */
1261 US_DEBUGP("Bulk status result = %d\n", result);
1263 ret = USB_STOR_TRANSPORT_ERROR;
1267 /* check bulk status */
1268 US_DEBUGP("Bulk status Sig 0x%x T 0x%x R %d Stat 0x%x\n",
1269 le32_to_cpu(bcs->Signature), bcs->Tag,
1270 bcs->Residue, bcs->Status);
1271 if ((bcs->Signature != cpu_to_le32(US_BULK_CS_SIGN) && bcs->Signature != cpu_to_le32(US_BULK_CS_OLYMPUS_SIGN)) ||
1272 bcs->Tag != bcb->Tag ||
1273 bcs->Status > US_BULK_STAT_PHASE || partial != 13) {
1274 US_DEBUGP("Bulk logical error\n");
1275 ret = USB_STOR_TRANSPORT_ERROR;
1279 /* based on the status code, we report good or bad */
1280 switch (bcs->Status) {
1281 case US_BULK_STAT_OK:
1282 /* command good -- note that data could be short */
1283 ret = USB_STOR_TRANSPORT_GOOD;
1286 case US_BULK_STAT_FAIL:
1287 /* command failed */
1288 ret = USB_STOR_TRANSPORT_FAILED;
1291 case US_BULK_STAT_PHASE:
1292 /* phase error -- note that a transport reset will be
1293 * invoked by the invoke_transport() function
1295 ret = USB_STOR_TRANSPORT_ERROR;
1299 /* we should never get here, but if we do, we're in trouble */
1307 /***********************************************************************
1309 ***********************************************************************/
1311 /* This issues a CB[I] Reset to the device in question
1313 int usb_stor_CB_reset(struct us_data *us)
1315 unsigned char cmd[12];
1318 US_DEBUGP("CB_reset() called\n");
1320 /* if the device was removed, then we're already reset */
1324 memset(cmd, 0xFF, sizeof(cmd));
1325 cmd[0] = SEND_DIAGNOSTIC;
1327 result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
1329 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1330 0, us->ifnum, cmd, sizeof(cmd), HZ*5);
1333 US_DEBUGP("CB[I] soft reset failed %d\n", result);
1337 /* long wait for reset */
1338 set_current_state(TASK_UNINTERRUPTIBLE);
1339 schedule_timeout(HZ*6);
1340 set_current_state(TASK_RUNNING);
1342 US_DEBUGP("CB_reset: clearing endpoint halt\n");
1343 usb_stor_clear_halt(us,
1344 usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
1345 usb_stor_clear_halt(us,
1346 usb_sndbulkpipe(us->pusb_dev, us->ep_out));
1348 US_DEBUGP("CB_reset done\n");
1349 /* return a result code based on the result of the control message */
1353 /* This issues a Bulk-only Reset to the device in question, including
1354 * clearing the subsequent endpoint halts that may occur.
1356 int usb_stor_Bulk_reset(struct us_data *us)
1360 US_DEBUGP("Bulk reset requested\n");
1362 /* if the device was removed, then we're already reset */
1366 result = usb_control_msg(us->pusb_dev,
1367 usb_sndctrlpipe(us->pusb_dev,0),
1368 US_BULK_RESET_REQUEST,
1369 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1370 0, us->ifnum, NULL, 0, HZ*5);
1373 US_DEBUGP("Bulk soft reset failed %d\n", result);
1377 /* long wait for reset */
1378 set_current_state(TASK_UNINTERRUPTIBLE);
1379 schedule_timeout(HZ*6);
1380 set_current_state(TASK_RUNNING);
1382 usb_stor_clear_halt(us,
1383 usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
1384 usb_stor_clear_halt(us,
1385 usb_sndbulkpipe(us->pusb_dev, us->ep_out));
1386 US_DEBUGP("Bulk soft reset completed\n");