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 |
|
---|
43 | bool usb_mast_verbose = true;
|
---|
44 |
|
---|
45 | #define MASTLOG(format, ...) \
|
---|
46 | do { \
|
---|
47 | if (usb_mast_verbose) { \
|
---|
48 | usb_log_debug("USB cl08: " format, ##__VA_ARGS__); \
|
---|
49 | } \
|
---|
50 | } while (false)
|
---|
51 |
|
---|
52 | /** Request data from mass storage device.
|
---|
53 | *
|
---|
54 | * @param bulk_in_pipe Bulk in pipe to the device.
|
---|
55 | * @param bulk_out_pipe Bulk out pipe to the device.
|
---|
56 | * @param tag Command block wrapper tag (automatically compared with answer).
|
---|
57 | * @param lun LUN index.
|
---|
58 | * @param cmd SCSI command buffer (in SCSI endianness).
|
---|
59 | * @param cmd_size Length of SCSI command @p cmd in bytes.
|
---|
60 | * @param in_buffer Buffer where to store the answer (CSW is not returned).
|
---|
61 | * @param in_buffer_size Size of the buffer (size of the request to the device).
|
---|
62 | * @param received_size Number of actually received bytes.
|
---|
63 | * @return Error code.
|
---|
64 | */
|
---|
65 | int usb_massstor_data_in(usb_pipe_t *bulk_in_pipe, usb_pipe_t *bulk_out_pipe,
|
---|
66 | uint32_t tag, uint8_t lun, void *cmd, size_t cmd_size,
|
---|
67 | void *in_buffer, size_t in_buffer_size, size_t *received_size)
|
---|
68 | {
|
---|
69 | int rc;
|
---|
70 | size_t act_size;
|
---|
71 |
|
---|
72 | /* Prepare CBW - command block wrapper */
|
---|
73 | usb_massstor_cbw_t cbw;
|
---|
74 | usb_massstor_cbw_prepare(&cbw, tag, in_buffer_size,
|
---|
75 | USB_DIRECTION_IN, lun, cmd_size, cmd);
|
---|
76 |
|
---|
77 | /* First, send the CBW. */
|
---|
78 | rc = usb_pipe_write(bulk_out_pipe, &cbw, sizeof(cbw));
|
---|
79 | MASTLOG("CBW '%s' sent: %s.\n",
|
---|
80 | usb_debug_str_buffer((uint8_t *) &cbw, sizeof(cbw), 0),
|
---|
81 | str_error(rc));
|
---|
82 | if (rc != EOK) {
|
---|
83 | return rc;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /* Try to retrieve the data from the device. */
|
---|
87 | act_size = 0;
|
---|
88 | rc = usb_pipe_read(bulk_in_pipe, in_buffer, in_buffer_size, &act_size);
|
---|
89 | MASTLOG("Received %zuB (%s): %s.\n", act_size,
|
---|
90 | usb_debug_str_buffer((uint8_t *) in_buffer, act_size, 0),
|
---|
91 | str_error(rc));
|
---|
92 | if (rc != EOK) {
|
---|
93 | return rc;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /* Read CSW. */
|
---|
97 | usb_massstor_csw_t csw;
|
---|
98 | size_t csw_size;
|
---|
99 | rc = usb_pipe_read(bulk_in_pipe, &csw, sizeof(csw), &csw_size);
|
---|
100 | MASTLOG("CSW '%s' received (%zuB): %s.\n",
|
---|
101 | usb_debug_str_buffer((uint8_t *) &csw, csw_size, 0), csw_size,
|
---|
102 | str_error(rc));
|
---|
103 | if (rc != EOK) {
|
---|
104 | return rc;
|
---|
105 | }
|
---|
106 | if (csw_size != sizeof(csw)) {
|
---|
107 | return ERANGE;
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (csw.dCSWTag != tag) {
|
---|
111 | return EBADCHECKSUM;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /*
|
---|
115 | * Determine the actual return value from the CSW.
|
---|
116 | */
|
---|
117 | if (csw.dCSWStatus != 0) {
|
---|
118 | // FIXME: better error code
|
---|
119 | // FIXME: distinguish 0x01 and 0x02
|
---|
120 | return EXDEV;
|
---|
121 | }
|
---|
122 |
|
---|
123 | size_t residue = (size_t) uint32_usb2host(csw.dCSWDataResidue);
|
---|
124 | if (residue > in_buffer_size) {
|
---|
125 | return ERANGE;
|
---|
126 | }
|
---|
127 | if (act_size != in_buffer_size - residue) {
|
---|
128 | return ERANGE;
|
---|
129 | }
|
---|
130 | if (received_size != NULL) {
|
---|
131 | *received_size = in_buffer_size - residue;
|
---|
132 | }
|
---|
133 |
|
---|
134 | return EOK;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * @}
|
---|
139 | */
|
---|