| 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 | * Generic functions for USB mass storage (implementation).
|
|---|
| 35 | */
|
|---|
| 36 | #include "mast.h"
|
|---|
| 37 | #include "cmds.h"
|
|---|
| 38 | #include <bool.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <str_error.h>
|
|---|
| 41 | #include <usb/debug.h>
|
|---|
| 42 | #include <usb/dev/request.h>
|
|---|
| 43 |
|
|---|
| 44 | bool usb_mast_verbose = true;
|
|---|
| 45 |
|
|---|
| 46 | #define MASTLOG(format, ...) \
|
|---|
| 47 | do { \
|
|---|
| 48 | if (usb_mast_verbose) { \
|
|---|
| 49 | usb_log_debug("USB cl08: " format, ##__VA_ARGS__); \
|
|---|
| 50 | } \
|
|---|
| 51 | } while (false)
|
|---|
| 52 |
|
|---|
| 53 | /** Request data from mass storage device.
|
|---|
| 54 | *
|
|---|
| 55 | * @param tag Command block wrapper tag (automatically compared with answer).
|
|---|
| 56 | * @param lun LUN index.
|
|---|
| 57 | * @param cmd SCSI command buffer (in SCSI endianness).
|
|---|
| 58 | * @param cmd_size Length of SCSI command @p cmd in bytes.
|
|---|
| 59 | * @param in_buffer Buffer where to store the answer (CSW is not returned).
|
|---|
| 60 | * @param in_buffer_size Size of the buffer (size of the request to the device).
|
|---|
| 61 | * @param received_size Number of actually received bytes.
|
|---|
| 62 | * @return Error code.
|
|---|
| 63 | */
|
|---|
| 64 | int usb_massstor_data_in(usb_device_t *dev,
|
|---|
| 65 | uint32_t tag, uint8_t lun, void *cmd, size_t cmd_size,
|
|---|
| 66 | void *in_buffer, size_t in_buffer_size, size_t *received_size)
|
|---|
| 67 | {
|
|---|
| 68 | int rc;
|
|---|
| 69 | size_t act_size;
|
|---|
| 70 | usb_pipe_t *bulk_in_pipe = dev->pipes[BULK_IN_EP].pipe;
|
|---|
| 71 | usb_pipe_t *bulk_out_pipe = dev->pipes[BULK_OUT_EP].pipe;
|
|---|
| 72 |
|
|---|
| 73 | /* Prepare CBW - command block wrapper */
|
|---|
| 74 | usb_massstor_cbw_t cbw;
|
|---|
| 75 | usb_massstor_cbw_prepare(&cbw, tag, in_buffer_size,
|
|---|
| 76 | USB_DIRECTION_IN, lun, cmd_size, cmd);
|
|---|
| 77 |
|
|---|
| 78 | /* First, send the CBW. */
|
|---|
| 79 | rc = usb_pipe_write(bulk_out_pipe, &cbw, sizeof(cbw));
|
|---|
| 80 | MASTLOG("CBW '%s' sent: %s.\n",
|
|---|
| 81 | usb_debug_str_buffer((uint8_t *) &cbw, sizeof(cbw), 0),
|
|---|
| 82 | str_error(rc));
|
|---|
| 83 | if (rc != EOK) {
|
|---|
| 84 | return rc;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /* Try to retrieve the data from the device. */
|
|---|
| 88 | act_size = 0;
|
|---|
| 89 | rc = usb_pipe_read(bulk_in_pipe, in_buffer, in_buffer_size, &act_size);
|
|---|
| 90 | MASTLOG("Received %zuB (%s): %s.\n", act_size,
|
|---|
| 91 | usb_debug_str_buffer((uint8_t *) in_buffer, act_size, 0),
|
|---|
| 92 | str_error(rc));
|
|---|
| 93 | if (rc != EOK) {
|
|---|
| 94 | /*
|
|---|
| 95 | * XXX If the pipe is stalled, we should clear it
|
|---|
| 96 | * and read CSW.
|
|---|
| 97 | */
|
|---|
| 98 | return rc;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /* Read CSW. */
|
|---|
| 102 | usb_massstor_csw_t csw;
|
|---|
| 103 | size_t csw_size;
|
|---|
| 104 | rc = usb_pipe_read(bulk_in_pipe, &csw, sizeof(csw), &csw_size);
|
|---|
| 105 | MASTLOG("CSW '%s' received (%zuB): %s.\n",
|
|---|
| 106 | usb_debug_str_buffer((uint8_t *) &csw, csw_size, 0), csw_size,
|
|---|
| 107 | str_error(rc));
|
|---|
| 108 | if (rc != EOK) {
|
|---|
| 109 | MASTLOG("rc != EOK\n");
|
|---|
| 110 | return rc;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | if (csw_size != sizeof(csw)) {
|
|---|
| 114 | MASTLOG("csw_size != sizeof(csw)\n");
|
|---|
| 115 | return ERANGE;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | if (csw.dCSWTag != tag) {
|
|---|
| 119 | MASTLOG("csw.dCSWTag != tag\n");
|
|---|
| 120 | return EBADCHECKSUM;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /*
|
|---|
| 124 | * Determine the actual return value from the CSW.
|
|---|
| 125 | */
|
|---|
| 126 | if (csw.dCSWStatus != 0) {
|
|---|
| 127 | MASTLOG("csw.dCSWStatus != 0\n");
|
|---|
| 128 | // FIXME: better error code
|
|---|
| 129 | // FIXME: distinguish 0x01 and 0x02
|
|---|
| 130 | return EXDEV;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | size_t residue = (size_t) uint32_usb2host(csw.dCSWDataResidue);
|
|---|
| 134 | if (residue > in_buffer_size) {
|
|---|
| 135 | MASTLOG("residue > in_buffer_size\n");
|
|---|
| 136 | return ERANGE;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /*
|
|---|
| 140 | * When the device has less data to send than requested, it can
|
|---|
| 141 | * either stall the pipe or send garbage and indicate that via
|
|---|
| 142 | * the residue field in CSW. That means in_buffer_size - residue
|
|---|
| 143 | * is the authoritative size of data received.
|
|---|
| 144 | */
|
|---|
| 145 |
|
|---|
| 146 | if (received_size != NULL) {
|
|---|
| 147 | *received_size = in_buffer_size - residue;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | return EOK;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /** Perform bulk-only mass storage reset.
|
|---|
| 154 | *
|
|---|
| 155 | * @param dev Device to be reseted.
|
|---|
| 156 | * @return Error code.
|
|---|
| 157 | */
|
|---|
| 158 | int usb_massstor_reset(usb_device_t *dev)
|
|---|
| 159 | {
|
|---|
| 160 | return usb_control_request_set(&dev->ctrl_pipe,
|
|---|
| 161 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
|
|---|
| 162 | 0xFF, 0, dev->interface_no, NULL, 0);
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /** Perform complete reset recovery of bulk-only mass storage.
|
|---|
| 166 | *
|
|---|
| 167 | * Notice that no error is reported because if this fails, the error
|
|---|
| 168 | * would reappear on next transaction somehow.
|
|---|
| 169 | *
|
|---|
| 170 | * @param dev Device to be reseted.
|
|---|
| 171 | */
|
|---|
| 172 | void usb_massstor_reset_recovery(usb_device_t *dev)
|
|---|
| 173 | {
|
|---|
| 174 | /* We would ignore errors here because if this fails
|
|---|
| 175 | * we are doomed anyway and any following transaction would fail.
|
|---|
| 176 | */
|
|---|
| 177 | usb_massstor_reset(dev);
|
|---|
| 178 | usb_pipe_clear_halt(&dev->ctrl_pipe, dev->pipes[BULK_IN_EP].pipe);
|
|---|
| 179 | usb_pipe_clear_halt(&dev->ctrl_pipe, dev->pipes[BULK_OUT_EP].pipe);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /** Get max LUN of a mass storage device.
|
|---|
| 183 | *
|
|---|
| 184 | * @see usb_masstor_get_lun_count
|
|---|
| 185 | *
|
|---|
| 186 | * @warning Error from this command does not necessarily indicate malfunction
|
|---|
| 187 | * of the device. Device does not need to support this request.
|
|---|
| 188 | * You shall rather use usb_masstor_get_lun_count.
|
|---|
| 189 | *
|
|---|
| 190 | * @param dev Mass storage device.
|
|---|
| 191 | * @return Error code of maximum LUN (index, not count).
|
|---|
| 192 | */
|
|---|
| 193 | int usb_massstor_get_max_lun(usb_device_t *dev)
|
|---|
| 194 | {
|
|---|
| 195 | uint8_t max_lun;
|
|---|
| 196 | size_t data_recv_len;
|
|---|
| 197 | int rc = usb_control_request_get(&dev->ctrl_pipe,
|
|---|
| 198 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
|
|---|
| 199 | 0xFE, 0, dev->interface_no, &max_lun, 1, &data_recv_len);
|
|---|
| 200 | if (rc != EOK) {
|
|---|
| 201 | return rc;
|
|---|
| 202 | }
|
|---|
| 203 | if (data_recv_len != 1) {
|
|---|
| 204 | return EEMPTY;
|
|---|
| 205 | }
|
|---|
| 206 | return (int) max_lun;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /** Get number of LUNs supported by mass storage device.
|
|---|
| 210 | *
|
|---|
| 211 | * @warning This function hides any error during the request
|
|---|
| 212 | * (typically that shall not be a problem).
|
|---|
| 213 | *
|
|---|
| 214 | * @param dev Mass storage device.
|
|---|
| 215 | * @return Number of LUNs.
|
|---|
| 216 | */
|
|---|
| 217 | size_t usb_masstor_get_lun_count(usb_device_t *dev)
|
|---|
| 218 | {
|
|---|
| 219 | int max_lun = usb_massstor_get_max_lun(dev);
|
|---|
| 220 | if (max_lun < 0) {
|
|---|
| 221 | max_lun = 1;
|
|---|
| 222 | } else {
|
|---|
| 223 | max_lun++;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | return (size_t) max_lun;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | /**
|
|---|
| 230 | * @}
|
|---|
| 231 | */
|
|---|