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 | return rc;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /* Read CSW. */
|
---|
98 | usb_massstor_csw_t csw;
|
---|
99 | size_t csw_size;
|
---|
100 | rc = usb_pipe_read(bulk_in_pipe, &csw, sizeof(csw), &csw_size);
|
---|
101 | MASTLOG("CSW '%s' received (%zuB): %s.\n",
|
---|
102 | usb_debug_str_buffer((uint8_t *) &csw, csw_size, 0), csw_size,
|
---|
103 | str_error(rc));
|
---|
104 | if (rc != EOK) {
|
---|
105 | return rc;
|
---|
106 | }
|
---|
107 | if (csw_size != sizeof(csw)) {
|
---|
108 | return ERANGE;
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (csw.dCSWTag != tag) {
|
---|
112 | return EBADCHECKSUM;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /*
|
---|
116 | * Determine the actual return value from the CSW.
|
---|
117 | */
|
---|
118 | if (csw.dCSWStatus != 0) {
|
---|
119 | // FIXME: better error code
|
---|
120 | // FIXME: distinguish 0x01 and 0x02
|
---|
121 | return EXDEV;
|
---|
122 | }
|
---|
123 |
|
---|
124 | size_t residue = (size_t) uint32_usb2host(csw.dCSWDataResidue);
|
---|
125 | if (residue > in_buffer_size) {
|
---|
126 | return ERANGE;
|
---|
127 | }
|
---|
128 | if (act_size != in_buffer_size - residue) {
|
---|
129 | return ERANGE;
|
---|
130 | }
|
---|
131 | if (received_size != NULL) {
|
---|
132 | *received_size = in_buffer_size - residue;
|
---|
133 | }
|
---|
134 |
|
---|
135 | return EOK;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /** Perform bulk-only mass storage reset.
|
---|
139 | *
|
---|
140 | * @param dev Device to be reseted.
|
---|
141 | * @return Error code.
|
---|
142 | */
|
---|
143 | int usb_massstor_reset(usb_device_t *dev)
|
---|
144 | {
|
---|
145 | return usb_control_request_set(&dev->ctrl_pipe,
|
---|
146 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
|
---|
147 | 0xFF, 0, dev->interface_no, NULL, 0);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /** Perform complete reset recovery of bulk-only mass storage.
|
---|
151 | *
|
---|
152 | * Notice that no error is reported because if this fails, the error
|
---|
153 | * would reappear on next transaction somehow.
|
---|
154 | *
|
---|
155 | * @param dev Device to be reseted.
|
---|
156 | */
|
---|
157 | void usb_massstor_reset_recovery(usb_device_t *dev)
|
---|
158 | {
|
---|
159 | /* We would ignore errors here because if this fails
|
---|
160 | * we are doomed anyway and any following transaction would fail.
|
---|
161 | */
|
---|
162 | usb_massstor_reset(dev);
|
---|
163 | usb_pipe_clear_halt(&dev->ctrl_pipe, dev->pipes[BULK_IN_EP].pipe);
|
---|
164 | usb_pipe_clear_halt(&dev->ctrl_pipe, dev->pipes[BULK_OUT_EP].pipe);
|
---|
165 | }
|
---|
166 |
|
---|
167 | /** Get max LUN of a mass storage device.
|
---|
168 | *
|
---|
169 | * @see usb_masstor_get_lun_count
|
---|
170 | *
|
---|
171 | * @warning Error from this command does not necessarily indicate malfunction
|
---|
172 | * of the device. Device does not need to support this request.
|
---|
173 | * You shall rather use usb_masstor_get_lun_count.
|
---|
174 | *
|
---|
175 | * @param dev Mass storage device.
|
---|
176 | * @return Error code of maximum LUN (index, not count).
|
---|
177 | */
|
---|
178 | int usb_massstor_get_max_lun(usb_device_t *dev)
|
---|
179 | {
|
---|
180 | uint8_t max_lun;
|
---|
181 | size_t data_recv_len;
|
---|
182 | int rc = usb_control_request_get(&dev->ctrl_pipe,
|
---|
183 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
|
---|
184 | 0xFE, 0, dev->interface_no, &max_lun, 1, &data_recv_len);
|
---|
185 | if (rc != EOK) {
|
---|
186 | return rc;
|
---|
187 | }
|
---|
188 | if (data_recv_len != 1) {
|
---|
189 | return EEMPTY;
|
---|
190 | }
|
---|
191 | return (int) max_lun;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /** Get number of LUNs supported by mass storage device.
|
---|
195 | *
|
---|
196 | * @warning This function hides any error during the request
|
---|
197 | * (typically that shall not be a problem).
|
---|
198 | *
|
---|
199 | * @param dev Mass storage device.
|
---|
200 | * @return Number of LUNs.
|
---|
201 | */
|
---|
202 | size_t usb_masstor_get_lun_count(usb_device_t *dev)
|
---|
203 | {
|
---|
204 | int max_lun = usb_massstor_get_max_lun(dev);
|
---|
205 | if (max_lun < 0) {
|
---|
206 | max_lun = 1;
|
---|
207 | } else {
|
---|
208 | max_lun++;
|
---|
209 | }
|
---|
210 |
|
---|
211 | return (size_t) max_lun;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * @}
|
---|
216 | */
|
---|