[6865243c] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
[bd575647] | 3 | * Copyright (c) 2011 Jan Vesely
|
---|
[6865243c] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
[160b75e] | 29 | /** @addtogroup libusbdev
|
---|
[6865243c] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[dc04868] | 33 | * USB endpoint pipes miscellaneous functions.
|
---|
[6865243c] | 34 | */
|
---|
[bd575647] | 35 | #include <usb/dev/pipes.h>
|
---|
[023a902] | 36 | #include <usb/dev/request.h>
|
---|
[6865243c] | 37 | #include <errno.h>
|
---|
[43f698b] | 38 | #include <assert.h>
|
---|
[563fb40] | 39 |
|
---|
[e9ce696] | 40 | /** Prepare pipe for a long transfer.
|
---|
| 41 | *
|
---|
| 42 | * By a long transfer is mean transfer consisting of several
|
---|
| 43 | * requests to the HC.
|
---|
| 44 | * Calling such function is optional and it has positive effect of
|
---|
| 45 | * improved performance because IPC session is initiated only once.
|
---|
| 46 | *
|
---|
| 47 | * @param pipe Pipe over which the transfer will happen.
|
---|
| 48 | * @return Error code.
|
---|
| 49 | */
|
---|
[47dfb34] | 50 | int usb_pipe_start_long_transfer(usb_pipe_t *pipe)
|
---|
[e9ce696] | 51 | {
|
---|
[47dfb34] | 52 | assert(pipe);
|
---|
| 53 | assert(pipe->wire);
|
---|
| 54 | assert(pipe->wire->hc_connection);
|
---|
| 55 | return usb_hc_connection_open(pipe->wire->hc_connection);
|
---|
[e9ce696] | 56 | }
|
---|
[023a902] | 57 | /*----------------------------------------------------------------------------*/
|
---|
[e9ce696] | 58 | /** Terminate a long transfer on a pipe.
|
---|
| 59 | *
|
---|
| 60 | * @see usb_pipe_start_long_transfer
|
---|
| 61 | *
|
---|
| 62 | * @param pipe Pipe where to end the long transfer.
|
---|
| 63 | */
|
---|
[47dfb34] | 64 | int usb_pipe_end_long_transfer(usb_pipe_t *pipe)
|
---|
[e9ce696] | 65 | {
|
---|
[47dfb34] | 66 | assert(pipe);
|
---|
| 67 | assert(pipe->wire);
|
---|
| 68 | assert(pipe->wire->hc_connection);
|
---|
| 69 | return usb_hc_connection_close(pipe->wire->hc_connection);
|
---|
[e9ce696] | 70 | }
|
---|
[023a902] | 71 | /*----------------------------------------------------------------------------*/
|
---|
| 72 | /** Request an in transfer, no checking of input parameters.
|
---|
| 73 | *
|
---|
| 74 | * @param[in] pipe Pipe used for the transfer.
|
---|
| 75 | * @param[out] buffer Buffer where to store the data.
|
---|
| 76 | * @param[in] size Size of the buffer (in bytes).
|
---|
| 77 | * @param[out] size_transfered Number of bytes that were actually transfered.
|
---|
| 78 | * @return Error code.
|
---|
| 79 | */
|
---|
| 80 | static int usb_pipe_read_no_check(usb_pipe_t *pipe, uint64_t setup,
|
---|
| 81 | void *buffer, size_t size, size_t *size_transfered)
|
---|
| 82 | {
|
---|
| 83 | /* Isochronous transfer are not supported (yet) */
|
---|
| 84 | if (pipe->transfer_type != USB_TRANSFER_INTERRUPT &&
|
---|
| 85 | pipe->transfer_type != USB_TRANSFER_BULK &&
|
---|
| 86 | pipe->transfer_type != USB_TRANSFER_CONTROL)
|
---|
| 87 | return ENOTSUP;
|
---|
| 88 |
|
---|
| 89 | return usb_hc_control_read(pipe->wire->hc_connection,
|
---|
| 90 | pipe->wire->address, pipe->endpoint_no, setup, buffer, size,
|
---|
| 91 | size_transfered);
|
---|
| 92 | }
|
---|
| 93 | /*----------------------------------------------------------------------------*/
|
---|
| 94 | /** Request an out transfer, no checking of input parameters.
|
---|
| 95 | *
|
---|
| 96 | * @param[in] pipe Pipe used for the transfer.
|
---|
| 97 | * @param[in] buffer Buffer with data to transfer.
|
---|
| 98 | * @param[in] size Size of the buffer (in bytes).
|
---|
| 99 | * @return Error code.
|
---|
| 100 | */
|
---|
| 101 | static int usb_pipe_write_no_check(usb_pipe_t *pipe, uint64_t setup,
|
---|
| 102 | const void *buffer, size_t size)
|
---|
| 103 | {
|
---|
| 104 | /* Only interrupt and bulk transfers are supported */
|
---|
| 105 | if (pipe->transfer_type != USB_TRANSFER_INTERRUPT &&
|
---|
| 106 | pipe->transfer_type != USB_TRANSFER_BULK &&
|
---|
| 107 | pipe->transfer_type != USB_TRANSFER_CONTROL)
|
---|
| 108 | return ENOTSUP;
|
---|
[e9ce696] | 109 |
|
---|
[023a902] | 110 | return usb_hc_control_write(pipe->wire->hc_connection,
|
---|
| 111 | pipe->wire->address, pipe->endpoint_no, setup, buffer, size);
|
---|
| 112 | }
|
---|
| 113 | /*----------------------------------------------------------------------------*/
|
---|
| 114 | /** Try to clear endpoint halt of default control pipe.
|
---|
| 115 | *
|
---|
| 116 | * @param pipe Pipe for control endpoint zero.
|
---|
| 117 | */
|
---|
| 118 | static void clear_self_endpoint_halt(usb_pipe_t *pipe)
|
---|
| 119 | {
|
---|
| 120 | assert(pipe != NULL);
|
---|
| 121 |
|
---|
| 122 | if (!pipe->auto_reset_halt || (pipe->endpoint_no != 0)) {
|
---|
| 123 | return;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 | /* Prevent infinite recursion. */
|
---|
| 128 | pipe->auto_reset_halt = false;
|
---|
| 129 | usb_request_clear_endpoint_halt(pipe, 0);
|
---|
| 130 | pipe->auto_reset_halt = true;
|
---|
| 131 | }
|
---|
| 132 | /*----------------------------------------------------------------------------*/
|
---|
| 133 | /** Request a control read transfer on an endpoint pipe.
|
---|
| 134 | *
|
---|
| 135 | * This function encapsulates all three stages of a control transfer.
|
---|
| 136 | *
|
---|
| 137 | * @param[in] pipe Pipe used for the transfer.
|
---|
| 138 | * @param[in] setup_buffer Buffer with the setup packet.
|
---|
| 139 | * @param[in] setup_buffer_size Size of the setup packet (in bytes).
|
---|
| 140 | * @param[out] data_buffer Buffer for incoming data.
|
---|
| 141 | * @param[in] data_buffer_size Size of the buffer for incoming data (in bytes).
|
---|
| 142 | * @param[out] data_transfered_size Number of bytes that were actually
|
---|
| 143 | * transfered during the DATA stage.
|
---|
| 144 | * @return Error code.
|
---|
| 145 | */
|
---|
| 146 | int usb_pipe_control_read(usb_pipe_t *pipe,
|
---|
| 147 | const void *setup_buffer, size_t setup_buffer_size,
|
---|
| 148 | void *data_buffer, size_t data_buffer_size, size_t *data_transfered_size)
|
---|
| 149 | {
|
---|
| 150 | assert(pipe);
|
---|
| 151 |
|
---|
| 152 | if ((setup_buffer == NULL) || (setup_buffer_size != 8)) {
|
---|
| 153 | return EINVAL;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | if ((data_buffer == NULL) || (data_buffer_size == 0)) {
|
---|
| 157 | return EINVAL;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | if ((pipe->direction != USB_DIRECTION_BOTH)
|
---|
| 161 | || (pipe->transfer_type != USB_TRANSFER_CONTROL)) {
|
---|
| 162 | return EBADF;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | uint64_t setup_packet;
|
---|
| 166 | memcpy(&setup_packet, setup_buffer, 8);
|
---|
| 167 |
|
---|
| 168 | size_t act_size = 0;
|
---|
| 169 | const int rc = usb_pipe_read_no_check(pipe, setup_packet,
|
---|
| 170 | data_buffer, data_buffer_size, &act_size);
|
---|
| 171 |
|
---|
| 172 | if (rc == ESTALL) {
|
---|
| 173 | clear_self_endpoint_halt(pipe);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | if (rc == EOK && data_transfered_size != NULL) {
|
---|
| 177 | *data_transfered_size = act_size;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | return rc;
|
---|
| 181 | }
|
---|
| 182 | /*----------------------------------------------------------------------------*/
|
---|
| 183 | /** Request a control write transfer on an endpoint pipe.
|
---|
| 184 | *
|
---|
| 185 | * This function encapsulates all three stages of a control transfer.
|
---|
| 186 | *
|
---|
| 187 | * @param[in] pipe Pipe used for the transfer.
|
---|
| 188 | * @param[in] setup_buffer Buffer with the setup packet.
|
---|
| 189 | * @param[in] setup_buffer_size Size of the setup packet (in bytes).
|
---|
| 190 | * @param[in] data_buffer Buffer with data to be sent.
|
---|
| 191 | * @param[in] data_buffer_size Size of the buffer with outgoing data (in bytes).
|
---|
| 192 | * @return Error code.
|
---|
| 193 | */
|
---|
| 194 | int usb_pipe_control_write(usb_pipe_t *pipe,
|
---|
| 195 | const void *setup_buffer, size_t setup_buffer_size,
|
---|
| 196 | const void *data_buffer, size_t data_buffer_size)
|
---|
| 197 | {
|
---|
| 198 | assert(pipe);
|
---|
| 199 |
|
---|
| 200 | if ((setup_buffer == NULL) || (setup_buffer_size != 8)) {
|
---|
| 201 | return EINVAL;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | if ((data_buffer == NULL) && (data_buffer_size > 0)) {
|
---|
| 205 | return EINVAL;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | if ((data_buffer != NULL) && (data_buffer_size == 0)) {
|
---|
| 209 | return EINVAL;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | if ((pipe->direction != USB_DIRECTION_BOTH)
|
---|
| 213 | || (pipe->transfer_type != USB_TRANSFER_CONTROL)) {
|
---|
| 214 | return EBADF;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | uint64_t setup_packet;
|
---|
| 218 | memcpy(&setup_packet, setup_buffer, 8);
|
---|
| 219 |
|
---|
| 220 | const int rc = usb_pipe_write_no_check(pipe, setup_packet,
|
---|
| 221 | data_buffer, data_buffer_size);
|
---|
| 222 |
|
---|
| 223 | if (rc == ESTALL) {
|
---|
| 224 | clear_self_endpoint_halt(pipe);
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | return rc;
|
---|
| 228 | }
|
---|
| 229 | /*----------------------------------------------------------------------------*/
|
---|
| 230 | /** Request a read (in) transfer on an endpoint pipe.
|
---|
| 231 | *
|
---|
| 232 | * @param[in] pipe Pipe used for the transfer.
|
---|
| 233 | * @param[out] buffer Buffer where to store the data.
|
---|
| 234 | * @param[in] size Size of the buffer (in bytes).
|
---|
| 235 | * @param[out] size_transfered Number of bytes that were actually transfered.
|
---|
| 236 | * @return Error code.
|
---|
| 237 | */
|
---|
| 238 | int usb_pipe_read(usb_pipe_t *pipe,
|
---|
| 239 | void *buffer, size_t size, size_t *size_transfered)
|
---|
| 240 | {
|
---|
| 241 | assert(pipe);
|
---|
| 242 |
|
---|
| 243 | if (buffer == NULL) {
|
---|
| 244 | return EINVAL;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | if (size == 0) {
|
---|
| 248 | return EINVAL;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | if (pipe->direction != USB_DIRECTION_IN) {
|
---|
| 252 | return EBADF;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | if (pipe->transfer_type == USB_TRANSFER_CONTROL) {
|
---|
| 256 | return EBADF;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | size_t act_size = 0;
|
---|
| 260 | const int rc = usb_pipe_read_no_check(pipe, 0, buffer, size, &act_size);
|
---|
| 261 |
|
---|
| 262 |
|
---|
| 263 | if (rc == EOK && size_transfered != NULL) {
|
---|
| 264 | *size_transfered = act_size;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | return rc;
|
---|
| 268 | }
|
---|
| 269 | /*----------------------------------------------------------------------------*/
|
---|
| 270 | /** Request a write (out) transfer on an endpoint pipe.
|
---|
| 271 | *
|
---|
| 272 | * @param[in] pipe Pipe used for the transfer.
|
---|
| 273 | * @param[in] buffer Buffer with data to transfer.
|
---|
| 274 | * @param[in] size Size of the buffer (in bytes).
|
---|
| 275 | * @return Error code.
|
---|
| 276 | */
|
---|
| 277 | int usb_pipe_write(usb_pipe_t *pipe, const void *buffer, size_t size)
|
---|
| 278 | {
|
---|
| 279 | assert(pipe);
|
---|
| 280 |
|
---|
| 281 | if (buffer == NULL || size == 0) {
|
---|
| 282 | return EINVAL;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | if (pipe->direction != USB_DIRECTION_OUT) {
|
---|
| 286 | return EBADF;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | if (pipe->transfer_type == USB_TRANSFER_CONTROL) {
|
---|
| 290 | return EBADF;
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | return usb_pipe_write_no_check(pipe, 0, buffer, size);
|
---|
| 294 | }
|
---|
[6865243c] | 295 | /**
|
---|
| 296 | * @}
|
---|
| 297 | */
|
---|