| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
|---|
| 3 | * Copyright (c) 2011 Jiri Svoboda
|
|---|
| 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
|
|---|
| 35 | * SCSI functions for USB mass storage driver.
|
|---|
| 36 | */
|
|---|
| 37 | #include <bitops.h>
|
|---|
| 38 | #include <byteorder.h>
|
|---|
| 39 | #include <inttypes.h>
|
|---|
| 40 | #include <usb/dev/driver.h>
|
|---|
| 41 | #include <usb/debug.h>
|
|---|
| 42 | #include <errno.h>
|
|---|
| 43 | #include <str_error.h>
|
|---|
| 44 | #include <str.h>
|
|---|
| 45 | #include <scsi/sbc.h>
|
|---|
| 46 | #include <scsi/spc.h>
|
|---|
| 47 | #include "cmds.h"
|
|---|
| 48 | #include "mast.h"
|
|---|
| 49 | #include "scsi_ms.h"
|
|---|
| 50 |
|
|---|
| 51 | /** Get string representation for SCSI peripheral device type.
|
|---|
| 52 | *
|
|---|
| 53 | * @param type SCSI peripheral device type code.
|
|---|
| 54 | * @return String representation.
|
|---|
| 55 | */
|
|---|
| 56 | const char *usbmast_scsi_dev_type_str(unsigned type)
|
|---|
| 57 | {
|
|---|
| 58 | return scsi_get_dev_type_str(type);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /** Perform SCSI Inquiry command on USB mass storage device.
|
|---|
| 62 | *
|
|---|
| 63 | * @param dev USB device.
|
|---|
| 64 | * @param inquiry_result Where to store parsed inquiry result.
|
|---|
| 65 | * @return Error code.
|
|---|
| 66 | */
|
|---|
| 67 | int usbmast_inquiry(usb_device_t *dev, usbmast_inquiry_data_t *inq_res)
|
|---|
| 68 | {
|
|---|
| 69 | scsi_std_inquiry_data_t inq_data;
|
|---|
| 70 | size_t response_len;
|
|---|
| 71 | scsi_cdb_inquiry_t cdb;
|
|---|
| 72 | int rc;
|
|---|
| 73 |
|
|---|
| 74 | memset(&cdb, 0, sizeof(cdb));
|
|---|
| 75 | cdb.op_code = SCSI_CMD_INQUIRY;
|
|---|
| 76 | cdb.alloc_len = host2uint16_t_be(sizeof(inq_data));
|
|---|
| 77 |
|
|---|
| 78 | rc = usb_massstor_data_in(dev, 0xDEADBEEF, 0, (uint8_t *) &cdb,
|
|---|
| 79 | sizeof(cdb), &inq_data, sizeof(inq_data), &response_len);
|
|---|
| 80 |
|
|---|
| 81 | if (rc != EOK) {
|
|---|
| 82 | usb_log_error("Inquiry failed, device %s: %s.\n",
|
|---|
| 83 | dev->ddf_dev->name, str_error(rc));
|
|---|
| 84 | return rc;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | if (response_len < SCSI_STD_INQUIRY_DATA_MIN_SIZE) {
|
|---|
| 88 | usb_log_error("SCSI Inquiry response too short (%zu).\n",
|
|---|
| 89 | response_len);
|
|---|
| 90 | return EIO;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /*
|
|---|
| 94 | * Parse inquiry data and fill in the result structure.
|
|---|
| 95 | */
|
|---|
| 96 |
|
|---|
| 97 | bzero(inq_res, sizeof(*inq_res));
|
|---|
| 98 |
|
|---|
| 99 | inq_res->device_type = BIT_RANGE_EXTRACT(uint8_t,
|
|---|
| 100 | inq_data.pqual_devtype, SCSI_PQDT_DEV_TYPE_h, SCSI_PQDT_DEV_TYPE_l);
|
|---|
| 101 |
|
|---|
| 102 | inq_res->removable = BIT_RANGE_EXTRACT(uint8_t,
|
|---|
| 103 | inq_data.rmb, SCSI_RMB_RMB, SCSI_RMB_RMB);
|
|---|
| 104 |
|
|---|
| 105 | spascii_to_str(inq_res->vendor, SCSI_INQ_VENDOR_STR_BUFSIZE,
|
|---|
| 106 | inq_data.vendor, sizeof(inq_data.vendor));
|
|---|
| 107 |
|
|---|
| 108 | spascii_to_str(inq_res->product, SCSI_INQ_PRODUCT_STR_BUFSIZE,
|
|---|
| 109 | inq_data.product, sizeof(inq_data.product));
|
|---|
| 110 |
|
|---|
| 111 | spascii_to_str(inq_res->revision, SCSI_INQ_REVISION_STR_BUFSIZE,
|
|---|
| 112 | inq_data.revision, sizeof(inq_data.revision));
|
|---|
| 113 |
|
|---|
| 114 | return EOK;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | /** Perform SCSI Read Capacity command on USB mass storage device.
|
|---|
| 118 | *
|
|---|
| 119 | * @param dev USB device.
|
|---|
| 120 | * @param nblocks Output, number of blocks.
|
|---|
| 121 | * @param block_size Output, block size in bytes.
|
|---|
| 122 | *
|
|---|
| 123 | * @return Error code.
|
|---|
| 124 | */
|
|---|
| 125 | int usbmast_read_capacity(usb_device_t *dev, uint32_t *nblocks,
|
|---|
| 126 | uint32_t *block_size)
|
|---|
| 127 | {
|
|---|
| 128 | scsi_cdb_read_capacity_10_t cdb;
|
|---|
| 129 | scsi_read_capacity_10_data_t data;
|
|---|
| 130 | size_t data_len;
|
|---|
| 131 | int rc;
|
|---|
| 132 |
|
|---|
| 133 | memset(&cdb, 0, sizeof(cdb));
|
|---|
| 134 | cdb.op_code = SCSI_CMD_READ_CAPACITY_10;
|
|---|
| 135 |
|
|---|
| 136 | rc = usb_massstor_data_in(dev, 0xDEADBEEF, 0, (uint8_t *) &cdb,
|
|---|
| 137 | sizeof(cdb), &data, sizeof(data), &data_len);
|
|---|
| 138 |
|
|---|
| 139 | if (rc != EOK) {
|
|---|
| 140 | usb_log_error("Read Capacity (10) failed, device %s: %s.\n",
|
|---|
| 141 | dev->ddf_dev->name, str_error(rc));
|
|---|
| 142 | return rc;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | if (data_len < sizeof(data)) {
|
|---|
| 146 | usb_log_error("SCSI Read Capacity response too short (%zu).\n",
|
|---|
| 147 | data_len);
|
|---|
| 148 | return EIO;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | *nblocks = uint32_t_be2host(data.last_lba) + 1;
|
|---|
| 152 | *block_size = uint32_t_be2host(data.block_size);
|
|---|
| 153 |
|
|---|
| 154 | return EOK;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /**
|
|---|
| 158 | * @}
|
|---|
| 159 | */
|
|---|