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