source: mainline/uspace/drv/block/usbmast/bo_trans.c@ 74c0de0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 74c0de0 was 53b9f2c, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Move usbhid and usbmast alongide hid and block drivers, respectively.

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