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