[70c12d6] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
[71fa44c] | 3 | * Copyright (c) 2011 Jiri Svoboda
|
---|
[70c12d6] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup drvusbmast
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
[6430ac6] | 35 | * SCSI functions for USB mass storage driver.
|
---|
[70c12d6] | 36 | */
|
---|
[64e6945d] | 37 | #include <bitops.h>
|
---|
[71fa44c] | 38 | #include <byteorder.h>
|
---|
| 39 | #include <inttypes.h>
|
---|
[f7a55f9] | 40 | #include <macros.h>
|
---|
[7d521e24] | 41 | #include <usb/dev/driver.h>
|
---|
[70c12d6] | 42 | #include <usb/debug.h>
|
---|
| 43 | #include <errno.h>
|
---|
| 44 | #include <str_error.h>
|
---|
| 45 | #include <str.h>
|
---|
[71fa44c] | 46 | #include <scsi/sbc.h>
|
---|
[0feaae4] | 47 | #include <scsi/spc.h>
|
---|
[70c12d6] | 48 | #include "cmds.h"
|
---|
| 49 | #include "mast.h"
|
---|
[6430ac6] | 50 | #include "scsi_ms.h"
|
---|
[70c12d6] | 51 |
|
---|
[cf002dbf] | 52 | /** Get string representation for SCSI peripheral device type.
|
---|
| 53 | *
|
---|
[6430ac6] | 54 | * @param type SCSI peripheral device type code.
|
---|
| 55 | * @return String representation.
|
---|
[cf002dbf] | 56 | */
|
---|
[6430ac6] | 57 | const char *usbmast_scsi_dev_type_str(unsigned type)
|
---|
[70c12d6] | 58 | {
|
---|
[0feaae4] | 59 | return scsi_get_dev_type_str(type);
|
---|
[70c12d6] | 60 | }
|
---|
| 61 |
|
---|
[71fa44c] | 62 | /** Perform SCSI Inquiry command on USB mass storage device.
|
---|
[cf002dbf] | 63 | *
|
---|
[6430ac6] | 64 | * @param dev USB device.
|
---|
[cf002dbf] | 65 | * @param inquiry_result Where to store parsed inquiry result.
|
---|
[6430ac6] | 66 | * @return Error code.
|
---|
[cf002dbf] | 67 | */
|
---|
[6430ac6] | 68 | int usbmast_inquiry(usb_device_t *dev, usbmast_inquiry_data_t *inq_res)
|
---|
[70c12d6] | 69 | {
|
---|
[7b2c17c] | 70 | scsi_std_inquiry_data_t inq_data;
|
---|
| 71 | size_t response_len;
|
---|
[71fa44c] | 72 | scsi_cdb_inquiry_t cdb;
|
---|
[70c12d6] | 73 | int rc;
|
---|
| 74 |
|
---|
[71fa44c] | 75 | memset(&cdb, 0, sizeof(cdb));
|
---|
| 76 | cdb.op_code = SCSI_CMD_INQUIRY;
|
---|
| 77 | cdb.alloc_len = host2uint16_t_be(sizeof(inq_data));
|
---|
| 78 |
|
---|
| 79 | rc = usb_massstor_data_in(dev, 0xDEADBEEF, 0, (uint8_t *) &cdb,
|
---|
| 80 | sizeof(cdb), &inq_data, sizeof(inq_data), &response_len);
|
---|
[70c12d6] | 81 |
|
---|
| 82 | if (rc != EOK) {
|
---|
[71fa44c] | 83 | usb_log_error("Inquiry failed, device %s: %s.\n",
|
---|
| 84 | dev->ddf_dev->name, str_error(rc));
|
---|
[70c12d6] | 85 | return rc;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[7b2c17c] | 88 | if (response_len < SCSI_STD_INQUIRY_DATA_MIN_SIZE) {
|
---|
[71fa44c] | 89 | usb_log_error("SCSI Inquiry response too short (%zu).\n",
|
---|
| 90 | response_len);
|
---|
[7b2c17c] | 91 | return EIO;
|
---|
[70c12d6] | 92 | }
|
---|
| 93 |
|
---|
| 94 | /*
|
---|
[7b2c17c] | 95 | * Parse inquiry data and fill in the result structure.
|
---|
[70c12d6] | 96 | */
|
---|
[7b2c17c] | 97 |
|
---|
[6430ac6] | 98 | bzero(inq_res, sizeof(*inq_res));
|
---|
[70c12d6] | 99 |
|
---|
[6430ac6] | 100 | inq_res->device_type = BIT_RANGE_EXTRACT(uint8_t,
|
---|
[64e6945d] | 101 | inq_data.pqual_devtype, SCSI_PQDT_DEV_TYPE_h, SCSI_PQDT_DEV_TYPE_l);
|
---|
[70c12d6] | 102 |
|
---|
[6430ac6] | 103 | inq_res->removable = BIT_RANGE_EXTRACT(uint8_t,
|
---|
[64e6945d] | 104 | inq_data.rmb, SCSI_RMB_RMB, SCSI_RMB_RMB);
|
---|
[d2fac08c] | 105 |
|
---|
[6430ac6] | 106 | spascii_to_str(inq_res->vendor, SCSI_INQ_VENDOR_STR_BUFSIZE,
|
---|
[dcb74c0a] | 107 | inq_data.vendor, sizeof(inq_data.vendor));
|
---|
[70c12d6] | 108 |
|
---|
[6430ac6] | 109 | spascii_to_str(inq_res->product, SCSI_INQ_PRODUCT_STR_BUFSIZE,
|
---|
[dcb74c0a] | 110 | inq_data.product, sizeof(inq_data.product));
|
---|
[70c12d6] | 111 |
|
---|
[6430ac6] | 112 | spascii_to_str(inq_res->revision, SCSI_INQ_REVISION_STR_BUFSIZE,
|
---|
[dcb74c0a] | 113 | inq_data.revision, sizeof(inq_data.revision));
|
---|
[70c12d6] | 114 |
|
---|
| 115 | return EOK;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[f7a55f9] | 118 | /** Perform SCSI Request Sense command on USB mass storage device.
|
---|
| 119 | *
|
---|
| 120 | * @param dev USB device
|
---|
| 121 | * @param buf Destination buffer
|
---|
| 122 | * @param size Size of @a buf
|
---|
| 123 | *
|
---|
| 124 | * @return Error code.
|
---|
| 125 | */
|
---|
| 126 | int usbmast_request_sense(usb_device_t *dev, void *buf, size_t size)
|
---|
| 127 | {
|
---|
| 128 | scsi_cdb_request_sense_t cdb;
|
---|
| 129 | size_t data_len;
|
---|
| 130 | int rc;
|
---|
| 131 |
|
---|
| 132 | memset(&cdb, 0, sizeof(cdb));
|
---|
| 133 | cdb.op_code = SCSI_CMD_REQUEST_SENSE;
|
---|
| 134 | cdb.alloc_len = min(size, SCSI_SENSE_DATA_MAX_SIZE);
|
---|
| 135 |
|
---|
| 136 | rc = usb_massstor_data_in(dev, 0xDEADBEEF, 0, (uint8_t *) &cdb,
|
---|
| 137 | sizeof(cdb), buf, size, &data_len);
|
---|
| 138 |
|
---|
| 139 | if (rc != EOK) {
|
---|
| 140 | usb_log_error("Request Sense failed, device %s: %s.\n",
|
---|
| 141 | dev->ddf_dev->name, str_error(rc));
|
---|
| 142 | return rc;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | if (data_len < SCSI_SENSE_DATA_MIN_SIZE) {
|
---|
| 146 | /* The missing bytes should be considered to be zeroes. */
|
---|
| 147 | memset((uint8_t *)buf + data_len, 0,
|
---|
| 148 | SCSI_SENSE_DATA_MIN_SIZE - data_len);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | return EOK;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[71fa44c] | 154 | /** Perform SCSI Read Capacity command on USB mass storage device.
|
---|
| 155 | *
|
---|
| 156 | * @param dev USB device.
|
---|
| 157 | * @param nblocks Output, number of blocks.
|
---|
| 158 | * @param block_size Output, block size in bytes.
|
---|
| 159 | *
|
---|
| 160 | * @return Error code.
|
---|
| 161 | */
|
---|
| 162 | int usbmast_read_capacity(usb_device_t *dev, uint32_t *nblocks,
|
---|
| 163 | uint32_t *block_size)
|
---|
| 164 | {
|
---|
| 165 | scsi_cdb_read_capacity_10_t cdb;
|
---|
| 166 | scsi_read_capacity_10_data_t data;
|
---|
| 167 | size_t data_len;
|
---|
| 168 | int rc;
|
---|
| 169 |
|
---|
| 170 | memset(&cdb, 0, sizeof(cdb));
|
---|
| 171 | cdb.op_code = SCSI_CMD_READ_CAPACITY_10;
|
---|
| 172 |
|
---|
| 173 | rc = usb_massstor_data_in(dev, 0xDEADBEEF, 0, (uint8_t *) &cdb,
|
---|
| 174 | sizeof(cdb), &data, sizeof(data), &data_len);
|
---|
| 175 |
|
---|
| 176 | if (rc != EOK) {
|
---|
| 177 | usb_log_error("Read Capacity (10) failed, device %s: %s.\n",
|
---|
| 178 | dev->ddf_dev->name, str_error(rc));
|
---|
| 179 | return rc;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | if (data_len < sizeof(data)) {
|
---|
| 183 | usb_log_error("SCSI Read Capacity response too short (%zu).\n",
|
---|
| 184 | data_len);
|
---|
| 185 | return EIO;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | *nblocks = uint32_t_be2host(data.last_lba) + 1;
|
---|
| 189 | *block_size = uint32_t_be2host(data.block_size);
|
---|
| 190 |
|
---|
| 191 | return EOK;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[f7a55f9] | 194 | /** Perform SCSI Read command on USB mass storage device.
|
---|
| 195 | *
|
---|
| 196 | * @param dev USB device.
|
---|
| 197 | * @param ba Address of first block.
|
---|
| 198 | * @param nblocks Number of blocks to read.
|
---|
| 199 | * @param bsize Block size.
|
---|
| 200 | *
|
---|
| 201 | * @return Error code.
|
---|
| 202 | */
|
---|
| 203 | int usbmast_read(usb_device_t *dev, uint64_t ba, size_t nblocks, size_t bsize,
|
---|
| 204 | void *buf)
|
---|
| 205 | {
|
---|
| 206 | scsi_cdb_read_12_t cdb;
|
---|
| 207 | size_t data_len;
|
---|
| 208 | int rc;
|
---|
| 209 |
|
---|
| 210 | /* XXX Need softstate to store block size. */
|
---|
| 211 |
|
---|
| 212 | if (ba > UINT32_MAX)
|
---|
| 213 | return ELIMIT;
|
---|
| 214 |
|
---|
| 215 | if ((uint64_t)nblocks * bsize > UINT32_MAX)
|
---|
| 216 | return ELIMIT;
|
---|
| 217 |
|
---|
| 218 | memset(&cdb, 0, sizeof(cdb));
|
---|
| 219 | cdb.op_code = SCSI_CMD_READ_12;
|
---|
| 220 | cdb.lba = host2uint32_t_be(ba);
|
---|
| 221 | cdb.xfer_len = host2uint32_t_be(nblocks);
|
---|
| 222 |
|
---|
| 223 | rc = usb_massstor_data_in(dev, 0xDEADBEEF, 0, (uint8_t *) &cdb,
|
---|
| 224 | sizeof(cdb), buf, nblocks * bsize, &data_len);
|
---|
| 225 |
|
---|
| 226 | if (rc != EOK) {
|
---|
| 227 | usb_log_error("Read (12) failed, device %s: %s.\n",
|
---|
| 228 | dev->ddf_dev->name, str_error(rc));
|
---|
| 229 | return rc;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | if (data_len < nblocks * bsize) {
|
---|
| 233 | usb_log_error("SCSI Read response too short (%zu).\n",
|
---|
| 234 | data_len);
|
---|
| 235 | return EIO;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | return EOK;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[38c9505] | 241 | /** Perform SCSI Write command on USB mass storage device.
|
---|
| 242 | *
|
---|
| 243 | * @param dev USB device
|
---|
| 244 | * @param ba Address of first block
|
---|
| 245 | * @param nblocks Number of blocks to read
|
---|
| 246 | * @param bsize Block size
|
---|
| 247 | * @param data Data to write
|
---|
| 248 | *
|
---|
| 249 | * @return Error code
|
---|
| 250 | */
|
---|
| 251 | int usbmast_write(usb_device_t *dev, uint64_t ba, size_t nblocks, size_t bsize,
|
---|
| 252 | const void *data)
|
---|
| 253 | {
|
---|
| 254 | scsi_cdb_write_12_t cdb;
|
---|
| 255 | size_t sent_len;
|
---|
| 256 | int rc;
|
---|
| 257 |
|
---|
| 258 | /* XXX Need softstate to store block size. */
|
---|
| 259 |
|
---|
| 260 | if (ba > UINT32_MAX)
|
---|
| 261 | return ELIMIT;
|
---|
| 262 |
|
---|
| 263 | if ((uint64_t)nblocks * bsize > UINT32_MAX)
|
---|
| 264 | return ELIMIT;
|
---|
| 265 |
|
---|
| 266 | memset(&cdb, 0, sizeof(cdb));
|
---|
| 267 | cdb.op_code = SCSI_CMD_WRITE_12;
|
---|
| 268 | cdb.lba = host2uint32_t_be(ba);
|
---|
| 269 | cdb.xfer_len = host2uint32_t_be(nblocks);
|
---|
| 270 |
|
---|
| 271 | rc = usb_massstor_data_out(dev, 0xDEADBEEF, 0, (uint8_t *) &cdb,
|
---|
| 272 | sizeof(cdb), data, nblocks * bsize, &sent_len);
|
---|
| 273 |
|
---|
| 274 | if (rc != EOK) {
|
---|
| 275 | usb_log_error("Write (12) failed, device %s: %s.\n",
|
---|
| 276 | dev->ddf_dev->name, str_error(rc));
|
---|
| 277 | return rc;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | if (sent_len < nblocks * bsize) {
|
---|
| 281 | usb_log_error("SCSI Write not all bytes transferred (%zu).\n",
|
---|
| 282 | sent_len);
|
---|
| 283 | return EIO;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | return EOK;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[70c12d6] | 289 | /**
|
---|
| 290 | * @}
|
---|
| 291 | */
|
---|