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 <macros.h>
|
---|
41 | #include <usb/dev/driver.h>
|
---|
42 | #include <usb/debug.h>
|
---|
43 | #include <errno.h>
|
---|
44 | #include <str_error.h>
|
---|
45 | #include <str.h>
|
---|
46 | #include <scsi/sbc.h>
|
---|
47 | #include <scsi/spc.h>
|
---|
48 | #include "cmds.h"
|
---|
49 | #include "mast.h"
|
---|
50 | #include "scsi_ms.h"
|
---|
51 |
|
---|
52 | /** Get string representation for SCSI peripheral device type.
|
---|
53 | *
|
---|
54 | * @param type SCSI peripheral device type code.
|
---|
55 | * @return String representation.
|
---|
56 | */
|
---|
57 | const char *usbmast_scsi_dev_type_str(unsigned type)
|
---|
58 | {
|
---|
59 | return scsi_get_dev_type_str(type);
|
---|
60 | }
|
---|
61 |
|
---|
62 | /** Perform SCSI Inquiry command on USB mass storage device.
|
---|
63 | *
|
---|
64 | * @param dev USB device.
|
---|
65 | * @param inquiry_result Where to store parsed inquiry result.
|
---|
66 | * @return Error code.
|
---|
67 | */
|
---|
68 | int usbmast_inquiry(usb_device_t *dev, usbmast_inquiry_data_t *inq_res)
|
---|
69 | {
|
---|
70 | scsi_std_inquiry_data_t inq_data;
|
---|
71 | size_t response_len;
|
---|
72 | scsi_cdb_inquiry_t cdb;
|
---|
73 | int rc;
|
---|
74 |
|
---|
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);
|
---|
81 |
|
---|
82 | if (rc != EOK) {
|
---|
83 | usb_log_error("Inquiry failed, device %s: %s.\n",
|
---|
84 | dev->ddf_dev->name, str_error(rc));
|
---|
85 | return rc;
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (response_len < SCSI_STD_INQUIRY_DATA_MIN_SIZE) {
|
---|
89 | usb_log_error("SCSI Inquiry response too short (%zu).\n",
|
---|
90 | response_len);
|
---|
91 | return EIO;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /*
|
---|
95 | * Parse inquiry data and fill in the result structure.
|
---|
96 | */
|
---|
97 |
|
---|
98 | bzero(inq_res, sizeof(*inq_res));
|
---|
99 |
|
---|
100 | inq_res->device_type = BIT_RANGE_EXTRACT(uint8_t,
|
---|
101 | inq_data.pqual_devtype, SCSI_PQDT_DEV_TYPE_h, SCSI_PQDT_DEV_TYPE_l);
|
---|
102 |
|
---|
103 | inq_res->removable = BIT_RANGE_EXTRACT(uint8_t,
|
---|
104 | inq_data.rmb, SCSI_RMB_RMB, SCSI_RMB_RMB);
|
---|
105 |
|
---|
106 | spascii_to_str(inq_res->vendor, SCSI_INQ_VENDOR_STR_BUFSIZE,
|
---|
107 | inq_data.vendor, sizeof(inq_data.vendor));
|
---|
108 |
|
---|
109 | spascii_to_str(inq_res->product, SCSI_INQ_PRODUCT_STR_BUFSIZE,
|
---|
110 | inq_data.product, sizeof(inq_data.product));
|
---|
111 |
|
---|
112 | spascii_to_str(inq_res->revision, SCSI_INQ_REVISION_STR_BUFSIZE,
|
---|
113 | inq_data.revision, sizeof(inq_data.revision));
|
---|
114 |
|
---|
115 | return EOK;
|
---|
116 | }
|
---|
117 |
|
---|
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 |
|
---|
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 |
|
---|
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 |
|
---|
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 |
|
---|
289 | /**
|
---|
290 | * @}
|
---|
291 | */
|
---|