| 1 | /* | 
|---|
| 2 | * Copyright (c) 2011 Vojtech Horky | 
|---|
| 3 | * All rights reserved. | 
|---|
| 4 | * | 
|---|
| 5 | * Redistribution and use in source and binary forms, with or without | 
|---|
| 6 | * modification, are permitted provided that the following conditions | 
|---|
| 7 | * are met: | 
|---|
| 8 | * | 
|---|
| 9 | * - Redistributions of source code must retain the above copyright | 
|---|
| 10 | *   notice, this list of conditions and the following disclaimer. | 
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright | 
|---|
| 12 | *   notice, this list of conditions and the following disclaimer in the | 
|---|
| 13 | *   documentation and/or other materials provided with the distribution. | 
|---|
| 14 | * - The name of the author may not be used to endorse or promote products | 
|---|
| 15 | *   derived from this software without specific prior written permission. | 
|---|
| 16 | * | 
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | 
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** @addtogroup drvusbmast | 
|---|
| 30 | * @{ | 
|---|
| 31 | */ | 
|---|
| 32 | /** | 
|---|
| 33 | * @file | 
|---|
| 34 | * USB mass storage bulk-only transport. | 
|---|
| 35 | */ | 
|---|
| 36 |  | 
|---|
| 37 | #include <stdbool.h> | 
|---|
| 38 | #include <errno.h> | 
|---|
| 39 | #include <str_error.h> | 
|---|
| 40 | #include <usb/debug.h> | 
|---|
| 41 | #include <usb/dev/request.h> | 
|---|
| 42 |  | 
|---|
| 43 | #include "bo_trans.h" | 
|---|
| 44 | #include "cmdw.h" | 
|---|
| 45 | #include "usbmast.h" | 
|---|
| 46 |  | 
|---|
| 47 | #define MASTLOG(format, ...) \ | 
|---|
| 48 | usb_log_debug2("USB cl08: " format, ##__VA_ARGS__) | 
|---|
| 49 |  | 
|---|
| 50 | /** Send command via bulk-only transport. | 
|---|
| 51 | * | 
|---|
| 52 | * @param mfun          Mass storage function | 
|---|
| 53 | * @param tag           Command block wrapper tag (automatically compared | 
|---|
| 54 | *                      with answer) | 
|---|
| 55 | * @param cmd           SCSI command | 
|---|
| 56 | * | 
|---|
| 57 | * @return              Error code | 
|---|
| 58 | */ | 
|---|
| 59 | errno_t usb_massstor_cmd(usbmast_fun_t *mfun, uint32_t tag, scsi_cmd_t *cmd) | 
|---|
| 60 | { | 
|---|
| 61 | errno_t rc; | 
|---|
| 62 |  | 
|---|
| 63 | if (cmd->data_in && cmd->data_out) | 
|---|
| 64 | return EINVAL; | 
|---|
| 65 |  | 
|---|
| 66 | usb_pipe_t *bulk_in_pipe = mfun->mdev->bulk_in_pipe; | 
|---|
| 67 | usb_pipe_t *bulk_out_pipe = mfun->mdev->bulk_out_pipe; | 
|---|
| 68 |  | 
|---|
| 69 | usb_pipe_t *dpipe = bulk_out_pipe; | 
|---|
| 70 | usb_direction_t ddir = USB_DIRECTION_OUT; | 
|---|
| 71 | size_t dbuf_size = cmd->data_out_size; | 
|---|
| 72 |  | 
|---|
| 73 | if (cmd->data_in) { | 
|---|
| 74 | ddir = USB_DIRECTION_IN; | 
|---|
| 75 | dbuf_size = cmd->data_in_size; | 
|---|
| 76 | dpipe = bulk_in_pipe; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | /* Prepare CBW - command block wrapper */ | 
|---|
| 80 | usb_massstor_cbw_t cbw; | 
|---|
| 81 | usb_massstor_cbw_prepare(&cbw, tag, dbuf_size, ddir, mfun->lun, | 
|---|
| 82 | cmd->cdb_size, cmd->cdb); | 
|---|
| 83 |  | 
|---|
| 84 | /* Send the CBW. */ | 
|---|
| 85 | MASTLOG("Sending CBW.\n"); | 
|---|
| 86 | rc = usb_pipe_write(bulk_out_pipe, &cbw, sizeof(cbw)); | 
|---|
| 87 | MASTLOG("CBW '%s' sent: %s.\n", | 
|---|
| 88 | usb_debug_str_buffer((uint8_t *) &cbw, sizeof(cbw), 0), | 
|---|
| 89 | str_error(rc)); | 
|---|
| 90 | if (rc != EOK) { | 
|---|
| 91 | usb_log_error("Bulk out write failed: %s", str_error(rc)); | 
|---|
| 92 | return EIO; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | MASTLOG("Transferring data.\n"); | 
|---|
| 96 | if (cmd->data_in) { | 
|---|
| 97 | size_t act_size; | 
|---|
| 98 | /* Recieve data from the device. */ | 
|---|
| 99 | rc = usb_pipe_read(dpipe, cmd->data_in, cmd->data_in_size, | 
|---|
| 100 | &act_size); | 
|---|
| 101 | MASTLOG("Received %zu bytes (%s): %s.\n", act_size, | 
|---|
| 102 | usb_debug_str_buffer(cmd->data_in, act_size, 0), | 
|---|
| 103 | str_error(rc)); | 
|---|
| 104 | } | 
|---|
| 105 | if (cmd->data_out) { | 
|---|
| 106 | /* Send data to the device. */ | 
|---|
| 107 | rc = usb_pipe_write(dpipe, cmd->data_out, cmd->data_out_size); | 
|---|
| 108 | MASTLOG("Sent %zu bytes (%s): %s.\n", cmd->data_out_size, | 
|---|
| 109 | usb_debug_str_buffer(cmd->data_out, cmd->data_out_size, 0), | 
|---|
| 110 | str_error(rc)); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | if (rc == ESTALL) { | 
|---|
| 114 | /* Clear stall condition and continue below to read CSW. */ | 
|---|
| 115 | usb_pipe_clear_halt( | 
|---|
| 116 | usb_device_get_default_pipe(mfun->mdev->usb_dev), dpipe); | 
|---|
| 117 | } else if (rc != EOK) { | 
|---|
| 118 | usb_log_error("Failed to transfer data: %s", str_error(rc)); | 
|---|
| 119 | return EIO; | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | /* Read CSW. */ | 
|---|
| 123 | usb_massstor_csw_t csw; | 
|---|
| 124 | size_t csw_size; | 
|---|
| 125 | MASTLOG("Reading CSW.\n"); | 
|---|
| 126 | rc = usb_pipe_read(bulk_in_pipe, &csw, sizeof(csw), &csw_size); | 
|---|
| 127 | MASTLOG("CSW '%s' received (%zu bytes): %s.\n", | 
|---|
| 128 | usb_debug_str_buffer((uint8_t *) &csw, csw_size, 0), csw_size, | 
|---|
| 129 | str_error(rc)); | 
|---|
| 130 | if (rc != EOK) { | 
|---|
| 131 | usb_log_error("Failed to read CSW: %s", str_error(rc)); | 
|---|
| 132 | return EIO; | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | if (csw_size != sizeof(csw)) { | 
|---|
| 136 | usb_log_error("Received CSW of incorrect size."); | 
|---|
| 137 | return EIO; | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | if (csw.dCSWTag != tag) { | 
|---|
| 141 | usb_log_error("Received CSW with incorrect tag. (expected: %" | 
|---|
| 142 | PRIX32 " received: %" PRIx32, tag, csw.dCSWTag); | 
|---|
| 143 | return EIO; | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | /* | 
|---|
| 147 | * Determine the actual return value from the CSW. | 
|---|
| 148 | */ | 
|---|
| 149 | switch (csw.dCSWStatus) { | 
|---|
| 150 | case cbs_passed: | 
|---|
| 151 | cmd->status = CMDS_GOOD; | 
|---|
| 152 | break; | 
|---|
| 153 | case cbs_failed: | 
|---|
| 154 | cmd->status = CMDS_FAILED; | 
|---|
| 155 | usb_log_error("CBS Failed."); | 
|---|
| 156 | break; | 
|---|
| 157 | case cbs_phase_error: | 
|---|
| 158 | usb_log_error("CBS phase error."); | 
|---|
| 159 | rc = EIO; | 
|---|
| 160 | break; | 
|---|
| 161 | default: | 
|---|
| 162 | usb_log_error("CBS other error."); | 
|---|
| 163 | rc = EIO; | 
|---|
| 164 | break; | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | const size_t residue = uint32_usb2host(csw.dCSWDataResidue); | 
|---|
| 168 | if (residue > dbuf_size) { | 
|---|
| 169 | usb_log_error("Residue > buffer size (%zu > %zu).", | 
|---|
| 170 | residue, dbuf_size); | 
|---|
| 171 | return EIO; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | /* | 
|---|
| 175 | * When the device has less data to send than requested (or cannot | 
|---|
| 176 | * receive moredata), it can either stall the pipe or send garbage | 
|---|
| 177 | * (ignore data) and indicate that via the residue field in CSW. | 
|---|
| 178 | * That means dbuf_size - residue is the authoritative size of data | 
|---|
| 179 | * received (sent). | 
|---|
| 180 | */ | 
|---|
| 181 |  | 
|---|
| 182 | if (cmd->data_in) | 
|---|
| 183 | cmd->rcvd_size = dbuf_size - residue; | 
|---|
| 184 |  | 
|---|
| 185 | return rc; | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | /** Perform bulk-only mass storage reset. | 
|---|
| 189 | * | 
|---|
| 190 | * @param mfun          Mass storage function | 
|---|
| 191 | * @return              Error code | 
|---|
| 192 | */ | 
|---|
| 193 | errno_t usb_massstor_reset(usbmast_dev_t *mdev) | 
|---|
| 194 | { | 
|---|
| 195 | return usb_control_request_set( | 
|---|
| 196 | usb_device_get_default_pipe(mdev->usb_dev), | 
|---|
| 197 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE, | 
|---|
| 198 | 0xFF, 0, usb_device_get_iface_number(mdev->usb_dev), NULL, 0); | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | /** Perform complete reset recovery of bulk-only mass storage. | 
|---|
| 202 | * | 
|---|
| 203 | * Notice that no error is reported because if this fails, the error | 
|---|
| 204 | * would reappear on next transaction somehow. | 
|---|
| 205 | * | 
|---|
| 206 | * @param mfun          Mass storage function | 
|---|
| 207 | */ | 
|---|
| 208 | void usb_massstor_reset_recovery(usbmast_dev_t *mdev) | 
|---|
| 209 | { | 
|---|
| 210 | /* | 
|---|
| 211 | * We would ignore errors here because if this fails | 
|---|
| 212 | * we are doomed anyway and any following transaction would fail. | 
|---|
| 213 | */ | 
|---|
| 214 | usb_massstor_reset(mdev); | 
|---|
| 215 | usb_pipe_clear_halt(usb_device_get_default_pipe(mdev->usb_dev), | 
|---|
| 216 | mdev->bulk_in_pipe); | 
|---|
| 217 | usb_pipe_clear_halt(usb_device_get_default_pipe(mdev->usb_dev), | 
|---|
| 218 | mdev->bulk_out_pipe); | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | /** Get max LUN of a mass storage device. | 
|---|
| 222 | * | 
|---|
| 223 | * @see usb_masstor_get_lun_count | 
|---|
| 224 | * | 
|---|
| 225 | * @warning Error from this command does not necessarily indicate malfunction | 
|---|
| 226 | * of the device. Device does not need to support this request. | 
|---|
| 227 | * You shall rather use usb_masstor_get_lun_count. | 
|---|
| 228 | * | 
|---|
| 229 | * @param mfun          Mass storage function | 
|---|
| 230 | * @return              Maximum LUN (index, not count), or -1 | 
|---|
| 231 | */ | 
|---|
| 232 | int usb_massstor_get_max_lun(usbmast_dev_t *mdev) | 
|---|
| 233 | { | 
|---|
| 234 | uint8_t max_lun; | 
|---|
| 235 | size_t data_recv_len; | 
|---|
| 236 | errno_t rc = usb_control_request_get( | 
|---|
| 237 | usb_device_get_default_pipe(mdev->usb_dev), | 
|---|
| 238 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE, | 
|---|
| 239 | 0xFE, 0, usb_device_get_iface_number(mdev->usb_dev), &max_lun, 1, | 
|---|
| 240 | &data_recv_len); | 
|---|
| 241 | if (rc != EOK) { | 
|---|
| 242 | return -1; | 
|---|
| 243 | } | 
|---|
| 244 | if (data_recv_len != 1) { | 
|---|
| 245 | return -1; | 
|---|
| 246 | } | 
|---|
| 247 | return max_lun; | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | /** Get number of LUNs supported by mass storage device. | 
|---|
| 251 | * | 
|---|
| 252 | * @warning This function hides any error during the request | 
|---|
| 253 | * (typically that shall not be a problem). | 
|---|
| 254 | * | 
|---|
| 255 | * @param mfun          Mass storage function | 
|---|
| 256 | * @return              Number of LUNs | 
|---|
| 257 | */ | 
|---|
| 258 | size_t usb_masstor_get_lun_count(usbmast_dev_t *mdev) | 
|---|
| 259 | { | 
|---|
| 260 | int max_lun = usb_massstor_get_max_lun(mdev); | 
|---|
| 261 | if (max_lun < 0) { | 
|---|
| 262 | max_lun = 1; | 
|---|
| 263 | } else { | 
|---|
| 264 | max_lun++; | 
|---|
| 265 | } | 
|---|
| 266 |  | 
|---|
| 267 | return (size_t) max_lun; | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | /** | 
|---|
| 271 | * @} | 
|---|
| 272 | */ | 
|---|