source: mainline/uspace/drv/block/usbmast/bo_trans.c@ 9905da7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9905da7 was 7c3fb9b, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix block comment formatting (ccheck).

  • Property mode set to 100644
File size: 7.6 KB
Line 
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 * USB mass storage bulk-only transport.
35 */
36
37#include <stdbool.h>
38#include <errno.h>
39#include <str_error.h>
40#include <usb/debug.h>
41#include <usb/dev/request.h>
42
43#include "bo_trans.h"
44#include "cmdw.h"
45#include "usbmast.h"
46
47
48#define MASTLOG(format, ...) \
49 usb_log_debug2("USB cl08: " format, ##__VA_ARGS__)
50
51/** Send command via bulk-only transport.
52 *
53 * @param mfun Mass storage function
54 * @param tag Command block wrapper tag (automatically compared
55 * with answer)
56 * @param cmd SCSI command
57 *
58 * @return Error code
59 */
60errno_t usb_massstor_cmd(usbmast_fun_t *mfun, uint32_t tag, scsi_cmd_t *cmd)
61{
62 errno_t rc;
63
64 if (cmd->data_in && cmd->data_out)
65 return EINVAL;
66
67 usb_pipe_t *bulk_in_pipe = mfun->mdev->bulk_in_pipe;
68 usb_pipe_t *bulk_out_pipe = mfun->mdev->bulk_out_pipe;
69
70 usb_pipe_t *dpipe = bulk_out_pipe;
71 usb_direction_t ddir = USB_DIRECTION_OUT;
72 size_t dbuf_size = cmd->data_out_size;
73
74 if (cmd->data_in) {
75 ddir = USB_DIRECTION_IN;
76 dbuf_size = cmd->data_in_size;
77 dpipe = bulk_in_pipe;
78 }
79
80 /* Prepare CBW - command block wrapper */
81 usb_massstor_cbw_t cbw;
82 usb_massstor_cbw_prepare(&cbw, tag, dbuf_size, ddir, mfun->lun,
83 cmd->cdb_size, cmd->cdb);
84
85 /* Send the CBW. */
86 MASTLOG("Sending CBW.\n");
87 rc = usb_pipe_write(bulk_out_pipe, &cbw, sizeof(cbw));
88 MASTLOG("CBW '%s' sent: %s.\n",
89 usb_debug_str_buffer((uint8_t *) &cbw, sizeof(cbw), 0),
90 str_error(rc));
91 if (rc != EOK) {
92 usb_log_error("Bulk out write failed: %s", str_error(rc));
93 return EIO;
94 }
95
96 MASTLOG("Transferring data.\n");
97 if (cmd->data_in) {
98 size_t act_size;
99 /* Recieve data from the device. */
100 rc = usb_pipe_read(dpipe, cmd->data_in, cmd->data_in_size,
101 &act_size);
102 MASTLOG("Received %zu bytes (%s): %s.\n", act_size,
103 usb_debug_str_buffer(cmd->data_in, act_size, 0),
104 str_error(rc));
105 }
106 if (cmd->data_out) {
107 /* Send data to the device. */
108 rc = usb_pipe_write(dpipe, cmd->data_out, cmd->data_out_size);
109 MASTLOG("Sent %zu bytes (%s): %s.\n", cmd->data_out_size,
110 usb_debug_str_buffer(cmd->data_out, cmd->data_out_size, 0),
111 str_error(rc));
112 }
113
114 if (rc == ESTALL) {
115 /* Clear stall condition and continue below to read CSW. */
116 usb_pipe_clear_halt(
117 usb_device_get_default_pipe(mfun->mdev->usb_dev), dpipe);
118 } else if (rc != EOK) {
119 usb_log_error("Failed to transfer data: %s", str_error(rc));
120 return EIO;
121 }
122
123 /* Read CSW. */
124 usb_massstor_csw_t csw;
125 size_t csw_size;
126 MASTLOG("Reading CSW.\n");
127 rc = usb_pipe_read(bulk_in_pipe, &csw, sizeof(csw), &csw_size);
128 MASTLOG("CSW '%s' received (%zu bytes): %s.\n",
129 usb_debug_str_buffer((uint8_t *) &csw, csw_size, 0), csw_size,
130 str_error(rc));
131 if (rc != EOK) {
132 usb_log_error("Failed to read CSW: %s", str_error(rc));
133 return EIO;
134 }
135
136 if (csw_size != sizeof(csw)) {
137 usb_log_error("Received CSW of incorrect size.");
138 return EIO;
139 }
140
141 if (csw.dCSWTag != tag) {
142 usb_log_error("Received CSW with incorrect tag. (expected: %"
143 PRIX32 " received: %" PRIx32, tag, csw.dCSWTag);
144 return EIO;
145 }
146
147 /*
148 * Determine the actual return value from the CSW.
149 */
150 switch (csw.dCSWStatus) {
151 case cbs_passed:
152 cmd->status = CMDS_GOOD;
153 break;
154 case cbs_failed:
155 cmd->status = CMDS_FAILED;
156 usb_log_error("CBS Failed.");
157 break;
158 case cbs_phase_error:
159 usb_log_error("CBS phase error.");
160 rc = EIO;
161 break;
162 default:
163 usb_log_error("CBS other error.");
164 rc = EIO;
165 break;
166 }
167
168 const size_t residue = uint32_usb2host(csw.dCSWDataResidue);
169 if (residue > dbuf_size) {
170 usb_log_error("Residue > buffer size (%zu > %zu).",
171 residue, dbuf_size);
172 return EIO;
173 }
174
175 /*
176 * When the device has less data to send than requested (or cannot
177 * receive moredata), it can either stall the pipe or send garbage
178 * (ignore data) and indicate that via the residue field in CSW.
179 * That means dbuf_size - residue is the authoritative size of data
180 * received (sent).
181 */
182
183 if (cmd->data_in)
184 cmd->rcvd_size = dbuf_size - residue;
185
186 return rc;
187}
188
189/** Perform bulk-only mass storage reset.
190 *
191 * @param mfun Mass storage function
192 * @return Error code
193 */
194errno_t usb_massstor_reset(usbmast_dev_t *mdev)
195{
196 return usb_control_request_set(
197 usb_device_get_default_pipe(mdev->usb_dev),
198 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
199 0xFF, 0, usb_device_get_iface_number(mdev->usb_dev), NULL, 0);
200}
201
202/** Perform complete reset recovery of bulk-only mass storage.
203 *
204 * Notice that no error is reported because if this fails, the error
205 * would reappear on next transaction somehow.
206 *
207 * @param mfun Mass storage function
208 */
209void usb_massstor_reset_recovery(usbmast_dev_t *mdev)
210{
211 /*
212 * We would ignore errors here because if this fails
213 * we are doomed anyway and any following transaction would fail.
214 */
215 usb_massstor_reset(mdev);
216 usb_pipe_clear_halt(usb_device_get_default_pipe(mdev->usb_dev),
217 mdev->bulk_in_pipe);
218 usb_pipe_clear_halt(usb_device_get_default_pipe(mdev->usb_dev),
219 mdev->bulk_out_pipe);
220}
221
222/** Get max LUN of a mass storage device.
223 *
224 * @see usb_masstor_get_lun_count
225 *
226 * @warning Error from this command does not necessarily indicate malfunction
227 * of the device. Device does not need to support this request.
228 * You shall rather use usb_masstor_get_lun_count.
229 *
230 * @param mfun Mass storage function
231 * @return Maximum LUN (index, not count), or -1
232 */
233int usb_massstor_get_max_lun(usbmast_dev_t *mdev)
234{
235 uint8_t max_lun;
236 size_t data_recv_len;
237 errno_t rc = usb_control_request_get(
238 usb_device_get_default_pipe(mdev->usb_dev),
239 USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE,
240 0xFE, 0, usb_device_get_iface_number(mdev->usb_dev), &max_lun, 1,
241 &data_recv_len);
242 if (rc != EOK) {
243 return -1;
244 }
245 if (data_recv_len != 1) {
246 return -1;
247 }
248 return max_lun;
249}
250
251/** Get number of LUNs supported by mass storage device.
252 *
253 * @warning This function hides any error during the request
254 * (typically that shall not be a problem).
255 *
256 * @param mfun Mass storage function
257 * @return Number of LUNs
258 */
259size_t usb_masstor_get_lun_count(usbmast_dev_t *mdev)
260{
261 int max_lun = usb_massstor_get_max_lun(mdev);
262 if (max_lun < 0) {
263 max_lun = 1;
264 } else {
265 max_lun++;
266 }
267
268 return (size_t) max_lun;
269}
270
271/**
272 * @}
273 */
Note: See TracBrowser for help on using the repository browser.