source: mainline/uspace/drv/bus/usb/usbmast/bo_trans.c@ a6abe20

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a6abe20 was a6abe20, checked in by Jan Vesely <jano.vesely@…>, 10 years ago

usbmast: Sanitize logging

Report the error condition.
Don't use custom verbosity control, that's what logctl_set_log_level is for.

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