[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
|
---|
[3538b0e] | 33 | * USB endpoint pipes 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 |
|
---|
[1561e8b] | 89 | return usb_device_control_read(pipe->wire,
|
---|
| 90 | pipe->endpoint_no, setup, buffer, size, size_transfered);
|
---|
[023a902] | 91 | }
|
---|
| 92 | /*----------------------------------------------------------------------------*/
|
---|
| 93 | /** Request an out transfer, no checking of input parameters.
|
---|
| 94 | *
|
---|
| 95 | * @param[in] pipe Pipe used for the transfer.
|
---|
| 96 | * @param[in] buffer Buffer with data to transfer.
|
---|
| 97 | * @param[in] size Size of the buffer (in bytes).
|
---|
| 98 | * @return Error code.
|
---|
| 99 | */
|
---|
| 100 | static int usb_pipe_write_no_check(usb_pipe_t *pipe, uint64_t setup,
|
---|
| 101 | const void *buffer, size_t size)
|
---|
| 102 | {
|
---|
| 103 | /* Only interrupt and bulk transfers are supported */
|
---|
| 104 | if (pipe->transfer_type != USB_TRANSFER_INTERRUPT &&
|
---|
| 105 | pipe->transfer_type != USB_TRANSFER_BULK &&
|
---|
| 106 | pipe->transfer_type != USB_TRANSFER_CONTROL)
|
---|
| 107 | return ENOTSUP;
|
---|
[e9ce696] | 108 |
|
---|
[1561e8b] | 109 | return usb_device_control_write(pipe->wire,
|
---|
| 110 | pipe->endpoint_no, setup, buffer, size);
|
---|
[023a902] | 111 | }
|
---|
| 112 | /*----------------------------------------------------------------------------*/
|
---|
| 113 | /** Try to clear endpoint halt of default control pipe.
|
---|
| 114 | *
|
---|
| 115 | * @param pipe Pipe for control endpoint zero.
|
---|
| 116 | */
|
---|
| 117 | static void clear_self_endpoint_halt(usb_pipe_t *pipe)
|
---|
| 118 | {
|
---|
| 119 | assert(pipe != NULL);
|
---|
| 120 |
|
---|
| 121 | if (!pipe->auto_reset_halt || (pipe->endpoint_no != 0)) {
|
---|
| 122 | return;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 | /* Prevent infinite recursion. */
|
---|
| 127 | pipe->auto_reset_halt = false;
|
---|
| 128 | usb_request_clear_endpoint_halt(pipe, 0);
|
---|
| 129 | pipe->auto_reset_halt = true;
|
---|
| 130 | }
|
---|
| 131 | /*----------------------------------------------------------------------------*/
|
---|
| 132 | /** Request a control read transfer on an endpoint pipe.
|
---|
| 133 | *
|
---|
| 134 | * This function encapsulates all three stages of a control transfer.
|
---|
| 135 | *
|
---|
| 136 | * @param[in] pipe Pipe used for the transfer.
|
---|
| 137 | * @param[in] setup_buffer Buffer with the setup packet.
|
---|
| 138 | * @param[in] setup_buffer_size Size of the setup packet (in bytes).
|
---|
| 139 | * @param[out] data_buffer Buffer for incoming data.
|
---|
| 140 | * @param[in] data_buffer_size Size of the buffer for incoming data (in bytes).
|
---|
| 141 | * @param[out] data_transfered_size Number of bytes that were actually
|
---|
| 142 | * transfered during the DATA stage.
|
---|
| 143 | * @return Error code.
|
---|
| 144 | */
|
---|
| 145 | int usb_pipe_control_read(usb_pipe_t *pipe,
|
---|
| 146 | const void *setup_buffer, size_t setup_buffer_size,
|
---|
| 147 | void *data_buffer, size_t data_buffer_size, size_t *data_transfered_size)
|
---|
| 148 | {
|
---|
| 149 | assert(pipe);
|
---|
| 150 |
|
---|
| 151 | if ((setup_buffer == NULL) || (setup_buffer_size != 8)) {
|
---|
| 152 | return EINVAL;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | if ((data_buffer == NULL) || (data_buffer_size == 0)) {
|
---|
| 156 | return EINVAL;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | if ((pipe->direction != USB_DIRECTION_BOTH)
|
---|
| 160 | || (pipe->transfer_type != USB_TRANSFER_CONTROL)) {
|
---|
| 161 | return EBADF;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | uint64_t setup_packet;
|
---|
| 165 | memcpy(&setup_packet, setup_buffer, 8);
|
---|
| 166 |
|
---|
| 167 | size_t act_size = 0;
|
---|
| 168 | const int rc = usb_pipe_read_no_check(pipe, setup_packet,
|
---|
| 169 | data_buffer, data_buffer_size, &act_size);
|
---|
| 170 |
|
---|
| 171 | if (rc == ESTALL) {
|
---|
| 172 | clear_self_endpoint_halt(pipe);
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | if (rc == EOK && data_transfered_size != NULL) {
|
---|
| 176 | *data_transfered_size = act_size;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | return rc;
|
---|
| 180 | }
|
---|
| 181 | /*----------------------------------------------------------------------------*/
|
---|
| 182 | /** Request a control write transfer on an endpoint pipe.
|
---|
| 183 | *
|
---|
| 184 | * This function encapsulates all three stages of a control transfer.
|
---|
| 185 | *
|
---|
| 186 | * @param[in] pipe Pipe used for the transfer.
|
---|
| 187 | * @param[in] setup_buffer Buffer with the setup packet.
|
---|
| 188 | * @param[in] setup_buffer_size Size of the setup packet (in bytes).
|
---|
| 189 | * @param[in] data_buffer Buffer with data to be sent.
|
---|
| 190 | * @param[in] data_buffer_size Size of the buffer with outgoing data (in bytes).
|
---|
| 191 | * @return Error code.
|
---|
| 192 | */
|
---|
| 193 | int usb_pipe_control_write(usb_pipe_t *pipe,
|
---|
| 194 | const void *setup_buffer, size_t setup_buffer_size,
|
---|
| 195 | const void *data_buffer, size_t data_buffer_size)
|
---|
| 196 | {
|
---|
| 197 | assert(pipe);
|
---|
| 198 |
|
---|
| 199 | if ((setup_buffer == NULL) || (setup_buffer_size != 8)) {
|
---|
| 200 | return EINVAL;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | if ((data_buffer == NULL) && (data_buffer_size > 0)) {
|
---|
| 204 | return EINVAL;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | if ((data_buffer != NULL) && (data_buffer_size == 0)) {
|
---|
| 208 | return EINVAL;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | if ((pipe->direction != USB_DIRECTION_BOTH)
|
---|
| 212 | || (pipe->transfer_type != USB_TRANSFER_CONTROL)) {
|
---|
| 213 | return EBADF;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | uint64_t setup_packet;
|
---|
| 217 | memcpy(&setup_packet, setup_buffer, 8);
|
---|
| 218 |
|
---|
| 219 | const int rc = usb_pipe_write_no_check(pipe, setup_packet,
|
---|
| 220 | data_buffer, data_buffer_size);
|
---|
| 221 |
|
---|
| 222 | if (rc == ESTALL) {
|
---|
| 223 | clear_self_endpoint_halt(pipe);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | return rc;
|
---|
| 227 | }
|
---|
| 228 | /*----------------------------------------------------------------------------*/
|
---|
| 229 | /** Request a read (in) transfer on an endpoint pipe.
|
---|
| 230 | *
|
---|
| 231 | * @param[in] pipe Pipe used for the transfer.
|
---|
| 232 | * @param[out] buffer Buffer where to store the data.
|
---|
| 233 | * @param[in] size Size of the buffer (in bytes).
|
---|
| 234 | * @param[out] size_transfered Number of bytes that were actually transfered.
|
---|
| 235 | * @return Error code.
|
---|
| 236 | */
|
---|
| 237 | int usb_pipe_read(usb_pipe_t *pipe,
|
---|
| 238 | void *buffer, size_t size, size_t *size_transfered)
|
---|
| 239 | {
|
---|
| 240 | assert(pipe);
|
---|
| 241 |
|
---|
| 242 | if (buffer == NULL) {
|
---|
| 243 | return EINVAL;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | if (size == 0) {
|
---|
| 247 | return EINVAL;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | if (pipe->direction != USB_DIRECTION_IN) {
|
---|
| 251 | return EBADF;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | if (pipe->transfer_type == USB_TRANSFER_CONTROL) {
|
---|
| 255 | return EBADF;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | size_t act_size = 0;
|
---|
| 259 | const int rc = usb_pipe_read_no_check(pipe, 0, buffer, size, &act_size);
|
---|
| 260 |
|
---|
| 261 |
|
---|
| 262 | if (rc == EOK && size_transfered != NULL) {
|
---|
| 263 | *size_transfered = act_size;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | return rc;
|
---|
| 267 | }
|
---|
| 268 | /*----------------------------------------------------------------------------*/
|
---|
| 269 | /** Request a write (out) transfer on an endpoint pipe.
|
---|
| 270 | *
|
---|
| 271 | * @param[in] pipe Pipe used for the transfer.
|
---|
| 272 | * @param[in] buffer Buffer with data to transfer.
|
---|
| 273 | * @param[in] size Size of the buffer (in bytes).
|
---|
| 274 | * @return Error code.
|
---|
| 275 | */
|
---|
| 276 | int usb_pipe_write(usb_pipe_t *pipe, const void *buffer, size_t size)
|
---|
| 277 | {
|
---|
| 278 | assert(pipe);
|
---|
| 279 |
|
---|
| 280 | if (buffer == NULL || size == 0) {
|
---|
| 281 | return EINVAL;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | if (pipe->direction != USB_DIRECTION_OUT) {
|
---|
| 285 | return EBADF;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | if (pipe->transfer_type == USB_TRANSFER_CONTROL) {
|
---|
| 289 | return EBADF;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | return usb_pipe_write_no_check(pipe, 0, buffer, size);
|
---|
| 293 | }
|
---|
[c804484] | 294 | /*----------------------------------------------------------------------------*/
|
---|
| 295 | /** Initialize USB endpoint pipe.
|
---|
| 296 | *
|
---|
| 297 | * @param pipe Endpoint pipe to be initialized.
|
---|
| 298 | * @param connection Connection to the USB device backing this pipe (the wire).
|
---|
| 299 | * @param endpoint_no Endpoint number (in USB 1.1 in range 0 to 15).
|
---|
| 300 | * @param transfer_type Transfer type (e.g. interrupt or bulk).
|
---|
| 301 | * @param max_packet_size Maximum packet size in bytes.
|
---|
| 302 | * @param direction Endpoint direction (in/out).
|
---|
| 303 | * @return Error code.
|
---|
| 304 | */
|
---|
| 305 | int usb_pipe_initialize(usb_pipe_t *pipe,
|
---|
| 306 | usb_device_connection_t *connection, usb_endpoint_t endpoint_no,
|
---|
| 307 | usb_transfer_type_t transfer_type, size_t max_packet_size,
|
---|
| 308 | usb_direction_t direction)
|
---|
| 309 | {
|
---|
| 310 | assert(pipe);
|
---|
| 311 | assert(connection);
|
---|
| 312 |
|
---|
| 313 | fibril_mutex_initialize(&pipe->guard);
|
---|
| 314 | pipe->wire = connection;
|
---|
| 315 | pipe->endpoint_no = endpoint_no;
|
---|
| 316 | pipe->transfer_type = transfer_type;
|
---|
| 317 | pipe->max_packet_size = max_packet_size;
|
---|
| 318 | pipe->direction = direction;
|
---|
| 319 | pipe->auto_reset_halt = false;
|
---|
| 320 |
|
---|
| 321 | return EOK;
|
---|
| 322 | }
|
---|
| 323 | /*----------------------------------------------------------------------------*/
|
---|
| 324 | /** Initialize USB endpoint pipe as the default zero control pipe.
|
---|
| 325 | *
|
---|
| 326 | * @param pipe Endpoint pipe to be initialized.
|
---|
| 327 | * @param connection Connection to the USB device backing this pipe (the wire).
|
---|
| 328 | * @return Error code.
|
---|
| 329 | */
|
---|
| 330 | int usb_pipe_initialize_default_control(usb_pipe_t *pipe,
|
---|
| 331 | usb_device_connection_t *connection)
|
---|
| 332 | {
|
---|
| 333 | assert(pipe);
|
---|
| 334 | assert(connection);
|
---|
| 335 |
|
---|
| 336 | int rc = usb_pipe_initialize(pipe, connection, 0, USB_TRANSFER_CONTROL,
|
---|
| 337 | CTRL_PIPE_MIN_PACKET_SIZE, USB_DIRECTION_BOTH);
|
---|
| 338 |
|
---|
| 339 | pipe->auto_reset_halt = true;
|
---|
| 340 |
|
---|
| 341 | return rc;
|
---|
| 342 | }
|
---|
| 343 | /*----------------------------------------------------------------------------*/
|
---|
| 344 | /** Register endpoint with the host controller.
|
---|
| 345 | *
|
---|
| 346 | * @param pipe Pipe to be registered.
|
---|
| 347 | * @param interval Polling interval.
|
---|
| 348 | * @return Error code.
|
---|
| 349 | */
|
---|
| 350 | int usb_pipe_register(usb_pipe_t *pipe, unsigned interval)
|
---|
| 351 | {
|
---|
| 352 | assert(pipe);
|
---|
| 353 | assert(pipe->wire);
|
---|
| 354 |
|
---|
| 355 | return usb_device_register_endpoint(pipe->wire,
|
---|
| 356 | pipe->endpoint_no, pipe->transfer_type,
|
---|
| 357 | pipe->direction, pipe->max_packet_size, interval);
|
---|
| 358 | }
|
---|
| 359 | /*----------------------------------------------------------------------------*/
|
---|
| 360 | /** Revert endpoint registration with the host controller.
|
---|
| 361 | *
|
---|
| 362 | * @param pipe Pipe to be unregistered.
|
---|
| 363 | * @return Error code.
|
---|
| 364 | */
|
---|
| 365 | int usb_pipe_unregister(usb_pipe_t *pipe)
|
---|
| 366 | {
|
---|
| 367 | assert(pipe);
|
---|
| 368 | assert(pipe->wire);
|
---|
| 369 |
|
---|
| 370 | return usb_device_unregister_endpoint(pipe->wire,
|
---|
| 371 | pipe->endpoint_no, pipe->direction);
|
---|
| 372 | }
|
---|
| 373 |
|
---|
[6865243c] | 374 | /**
|
---|
| 375 | * @}
|
---|
| 376 | */
|
---|