[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>
|
---|
[c01987c] | 37 | #include <usb/usb.h>
|
---|
| 38 |
|
---|
[43f698b] | 39 | #include <assert.h>
|
---|
[c01987c] | 40 | #include <async.h>
|
---|
| 41 | #include <errno.h>
|
---|
| 42 | #include <mem.h>
|
---|
[563fb40] | 43 |
|
---|
[023a902] | 44 | /** Try to clear endpoint halt of default control pipe.
|
---|
| 45 | *
|
---|
| 46 | * @param pipe Pipe for control endpoint zero.
|
---|
| 47 | */
|
---|
| 48 | static void clear_self_endpoint_halt(usb_pipe_t *pipe)
|
---|
| 49 | {
|
---|
| 50 | assert(pipe != NULL);
|
---|
| 51 |
|
---|
[816f5f4] | 52 | if (!pipe->auto_reset_halt || (pipe->desc.endpoint_no != 0)) {
|
---|
[023a902] | 53 | return;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | /* Prevent infinite recursion. */
|
---|
| 57 | pipe->auto_reset_halt = false;
|
---|
| 58 | usb_request_clear_endpoint_halt(pipe, 0);
|
---|
| 59 | pipe->auto_reset_halt = true;
|
---|
| 60 | }
|
---|
[a76b01b4] | 61 |
|
---|
[023a902] | 62 | /** Request a control read transfer on an endpoint pipe.
|
---|
| 63 | *
|
---|
| 64 | * This function encapsulates all three stages of a control transfer.
|
---|
| 65 | *
|
---|
| 66 | * @param[in] pipe Pipe used for the transfer.
|
---|
| 67 | * @param[in] setup_buffer Buffer with the setup packet.
|
---|
| 68 | * @param[in] setup_buffer_size Size of the setup packet (in bytes).
|
---|
| 69 | * @param[out] data_buffer Buffer for incoming data.
|
---|
| 70 | * @param[in] data_buffer_size Size of the buffer for incoming data (in bytes).
|
---|
[db51a6a6] | 71 | * @param[out] data_transferred_size Number of bytes that were actually
|
---|
| 72 | * transferred during the DATA stage.
|
---|
[023a902] | 73 | * @return Error code.
|
---|
| 74 | */
|
---|
| 75 | int usb_pipe_control_read(usb_pipe_t *pipe,
|
---|
| 76 | const void *setup_buffer, size_t setup_buffer_size,
|
---|
[db51a6a6] | 77 | void *buffer, size_t buffer_size, size_t *transferred_size)
|
---|
[023a902] | 78 | {
|
---|
| 79 | assert(pipe);
|
---|
| 80 |
|
---|
| 81 | if ((setup_buffer == NULL) || (setup_buffer_size != 8)) {
|
---|
| 82 | return EINVAL;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[22ecbde] | 85 | if ((buffer == NULL) || (buffer_size == 0)) {
|
---|
[023a902] | 86 | return EINVAL;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[816f5f4] | 89 | if ((pipe->desc.direction != USB_DIRECTION_BOTH)
|
---|
| 90 | || (pipe->desc.transfer_type != USB_TRANSFER_CONTROL)) {
|
---|
[023a902] | 91 | return EBADF;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | uint64_t setup_packet;
|
---|
| 95 | memcpy(&setup_packet, setup_buffer, 8);
|
---|
| 96 |
|
---|
[3969a42] | 97 | async_exch_t *exch = async_exchange_begin(pipe->bus_session);
|
---|
[023a902] | 98 | size_t act_size = 0;
|
---|
[41df71f9] | 99 | const int rc = usbhc_read(exch, pipe->desc.endpoint_no, setup_packet, buffer,
|
---|
[58563585] | 100 | buffer_size, &act_size);
|
---|
[3969a42] | 101 | async_exchange_end(exch);
|
---|
[023a902] | 102 |
|
---|
| 103 | if (rc == ESTALL) {
|
---|
| 104 | clear_self_endpoint_halt(pipe);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[db51a6a6] | 107 | if (rc == EOK && transferred_size != NULL) {
|
---|
| 108 | *transferred_size = act_size;
|
---|
[023a902] | 109 | }
|
---|
| 110 |
|
---|
| 111 | return rc;
|
---|
| 112 | }
|
---|
[a76b01b4] | 113 |
|
---|
[023a902] | 114 | /** Request a control write transfer on an endpoint pipe.
|
---|
| 115 | *
|
---|
| 116 | * This function encapsulates all three stages of a control transfer.
|
---|
| 117 | *
|
---|
| 118 | * @param[in] pipe Pipe used for the transfer.
|
---|
| 119 | * @param[in] setup_buffer Buffer with the setup packet.
|
---|
| 120 | * @param[in] setup_buffer_size Size of the setup packet (in bytes).
|
---|
| 121 | * @param[in] data_buffer Buffer with data to be sent.
|
---|
| 122 | * @param[in] data_buffer_size Size of the buffer with outgoing data (in bytes).
|
---|
| 123 | * @return Error code.
|
---|
| 124 | */
|
---|
| 125 | int usb_pipe_control_write(usb_pipe_t *pipe,
|
---|
| 126 | const void *setup_buffer, size_t setup_buffer_size,
|
---|
[22ecbde] | 127 | const void *buffer, size_t buffer_size)
|
---|
[023a902] | 128 | {
|
---|
| 129 | assert(pipe);
|
---|
| 130 |
|
---|
| 131 | if ((setup_buffer == NULL) || (setup_buffer_size != 8)) {
|
---|
| 132 | return EINVAL;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[22ecbde] | 135 | if ((buffer == NULL) && (buffer_size > 0)) {
|
---|
[023a902] | 136 | return EINVAL;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[22ecbde] | 139 | if ((buffer != NULL) && (buffer_size == 0)) {
|
---|
[023a902] | 140 | return EINVAL;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[816f5f4] | 143 | if ((pipe->desc.direction != USB_DIRECTION_BOTH)
|
---|
| 144 | || (pipe->desc.transfer_type != USB_TRANSFER_CONTROL)) {
|
---|
[023a902] | 145 | return EBADF;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | uint64_t setup_packet;
|
---|
| 149 | memcpy(&setup_packet, setup_buffer, 8);
|
---|
| 150 |
|
---|
[3969a42] | 151 | async_exch_t *exch = async_exchange_begin(pipe->bus_session);
|
---|
[41df71f9] | 152 | const int rc = usbhc_write(exch,
|
---|
[816f5f4] | 153 | pipe->desc.endpoint_no, setup_packet, buffer, buffer_size);
|
---|
[3969a42] | 154 | async_exchange_end(exch);
|
---|
[023a902] | 155 |
|
---|
| 156 | if (rc == ESTALL) {
|
---|
| 157 | clear_self_endpoint_halt(pipe);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | return rc;
|
---|
| 161 | }
|
---|
[a76b01b4] | 162 |
|
---|
[023a902] | 163 | /** Request a read (in) transfer on an endpoint pipe.
|
---|
| 164 | *
|
---|
| 165 | * @param[in] pipe Pipe used for the transfer.
|
---|
| 166 | * @param[out] buffer Buffer where to store the data.
|
---|
| 167 | * @param[in] size Size of the buffer (in bytes).
|
---|
[db51a6a6] | 168 | * @param[out] size_transferred Number of bytes that were actually transferred.
|
---|
[023a902] | 169 | * @return Error code.
|
---|
| 170 | */
|
---|
| 171 | int usb_pipe_read(usb_pipe_t *pipe,
|
---|
[db51a6a6] | 172 | void *buffer, size_t size, size_t *size_transferred)
|
---|
[023a902] | 173 | {
|
---|
| 174 | assert(pipe);
|
---|
| 175 |
|
---|
| 176 | if (buffer == NULL) {
|
---|
| 177 | return EINVAL;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | if (size == 0) {
|
---|
| 181 | return EINVAL;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[816f5f4] | 184 | if (pipe->desc.direction != USB_DIRECTION_IN) {
|
---|
[023a902] | 185 | return EBADF;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[816f5f4] | 188 | if (pipe->desc.transfer_type == USB_TRANSFER_CONTROL) {
|
---|
[023a902] | 189 | return EBADF;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[ba2fc2c] | 192 | async_exch_t *exch;
|
---|
| 193 | if (pipe->desc.transfer_type == USB_TRANSFER_ISOCHRONOUS)
|
---|
| 194 | exch = async_exchange_begin(pipe->isoch_session);
|
---|
| 195 | else
|
---|
| 196 | exch = async_exchange_begin(pipe->bus_session);
|
---|
[22ecbde] | 197 | size_t act_size = 0;
|
---|
[3969a42] | 198 | const int rc =
|
---|
[41df71f9] | 199 | usbhc_read(exch, pipe->desc.endpoint_no, 0, buffer, size, &act_size);
|
---|
[3969a42] | 200 | async_exchange_end(exch);
|
---|
[023a902] | 201 |
|
---|
[db51a6a6] | 202 | if (rc == EOK && size_transferred != NULL) {
|
---|
| 203 | *size_transferred = act_size;
|
---|
[023a902] | 204 | }
|
---|
| 205 |
|
---|
| 206 | return rc;
|
---|
| 207 | }
|
---|
[a76b01b4] | 208 |
|
---|
[023a902] | 209 | /** Request a write (out) transfer on an endpoint pipe.
|
---|
| 210 | *
|
---|
| 211 | * @param[in] pipe Pipe used for the transfer.
|
---|
| 212 | * @param[in] buffer Buffer with data to transfer.
|
---|
| 213 | * @param[in] size Size of the buffer (in bytes).
|
---|
| 214 | * @return Error code.
|
---|
| 215 | */
|
---|
| 216 | int usb_pipe_write(usb_pipe_t *pipe, const void *buffer, size_t size)
|
---|
| 217 | {
|
---|
| 218 | assert(pipe);
|
---|
| 219 |
|
---|
| 220 | if (buffer == NULL || size == 0) {
|
---|
| 221 | return EINVAL;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[816f5f4] | 224 | if (pipe->desc.direction != USB_DIRECTION_OUT) {
|
---|
[023a902] | 225 | return EBADF;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[816f5f4] | 228 | if (pipe->desc.transfer_type == USB_TRANSFER_CONTROL) {
|
---|
[023a902] | 229 | return EBADF;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[ba2fc2c] | 232 | async_exch_t *exch;
|
---|
| 233 | if (pipe->desc.transfer_type == USB_TRANSFER_ISOCHRONOUS)
|
---|
| 234 | exch = async_exchange_begin(pipe->isoch_session);
|
---|
| 235 | else
|
---|
| 236 | exch = async_exchange_begin(pipe->bus_session);
|
---|
[22ecbde] | 237 |
|
---|
[41df71f9] | 238 | const int rc = usbhc_write(exch, pipe->desc.endpoint_no, 0, buffer, size);
|
---|
[3969a42] | 239 | async_exchange_end(exch);
|
---|
| 240 | return rc;
|
---|
[023a902] | 241 | }
|
---|
[a76b01b4] | 242 |
|
---|
[ba2fc2c] | 243 | /** Setup isochronous session for isochronous communication.
|
---|
| 244 | * Isochronous endpoints need a different session as they might block while waiting for data.
|
---|
| 245 | *
|
---|
| 246 | * @param pipe Endpoint pipe being initialized.
|
---|
| 247 | * @return Error code.
|
---|
| 248 | */
|
---|
| 249 | static int usb_isoch_session_initialize(usb_pipe_t *pipe) {
|
---|
| 250 |
|
---|
[c280d7e] | 251 | /*
|
---|
| 252 | * XXX: As parallel exhanges are implemented by using parallel sessions,
|
---|
| 253 | * it is safe to just take the same session. Once this won't be true,
|
---|
| 254 | * just use session cloning to clone the bus session.
|
---|
| 255 | */
|
---|
| 256 | pipe->isoch_session = pipe->bus_session;
|
---|
[ba2fc2c] | 257 | return EOK;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
[c804484] | 260 | /** Initialize USB endpoint pipe.
|
---|
| 261 | *
|
---|
| 262 | * @param pipe Endpoint pipe to be initialized.
|
---|
[9e5b162] | 263 | * @param bus_session Endpoint pipe to be initialized.
|
---|
[c804484] | 264 | * @return Error code.
|
---|
| 265 | */
|
---|
[9efad54] | 266 | int usb_pipe_initialize(usb_pipe_t *pipe, usb_dev_session_t *bus_session, usb_transfer_type_t transfer_type)
|
---|
[c804484] | 267 | {
|
---|
| 268 | assert(pipe);
|
---|
| 269 |
|
---|
| 270 | pipe->auto_reset_halt = false;
|
---|
[8582076] | 271 | pipe->bus_session = bus_session;
|
---|
[c804484] | 272 |
|
---|
[9efad54] | 273 | if (transfer_type == USB_TRANSFER_ISOCHRONOUS)
|
---|
| 274 | return usb_isoch_session_initialize(pipe);
|
---|
[ba2fc2c] | 275 |
|
---|
[9efad54] | 276 | return EOK;
|
---|
[c804484] | 277 | }
|
---|
[a76b01b4] | 278 |
|
---|
[9efad54] | 279 | static const usb_pipe_desc_t default_control_pipe = {
|
---|
| 280 | .endpoint_no = 0,
|
---|
| 281 | .transfer_type = USB_TRANSFER_CONTROL,
|
---|
[9e5b162] | 282 | .direction = USB_DIRECTION_BOTH,
|
---|
[9efad54] | 283 | .max_transfer_size = CTRL_PIPE_MIN_PACKET_SIZE,
|
---|
[9e5b162] | 284 | };
|
---|
| 285 |
|
---|
[9efad54] | 286 | /** Initialize USB default control pipe.
|
---|
| 287 | *
|
---|
| 288 | * This one is special because it must not be registered, it is registered automatically.
|
---|
[c804484] | 289 | *
|
---|
| 290 | * @param pipe Endpoint pipe to be initialized.
|
---|
[9efad54] | 291 | * @param bus_session Endpoint pipe to be initialized.
|
---|
[c804484] | 292 | * @return Error code.
|
---|
| 293 | */
|
---|
[9e5b162] | 294 | int usb_pipe_initialize_default_control(usb_pipe_t *pipe, usb_dev_session_t *bus_session)
|
---|
[c804484] | 295 | {
|
---|
[9efad54] | 296 | const int ret = usb_pipe_initialize(pipe, bus_session, USB_TRANSFER_CONTROL);
|
---|
| 297 | if (ret)
|
---|
| 298 | return ret;
|
---|
| 299 |
|
---|
| 300 | pipe->desc = default_control_pipe;
|
---|
[ba654f2] | 301 | pipe->auto_reset_halt = true;
|
---|
[9efad54] | 302 |
|
---|
| 303 | return EOK;
|
---|
[c804484] | 304 | }
|
---|
[a76b01b4] | 305 |
|
---|
[c804484] | 306 | /** Register endpoint with the host controller.
|
---|
| 307 | *
|
---|
| 308 | * @param pipe Pipe to be registered.
|
---|
[9efad54] | 309 | * @param ep_desc Matched endpoint descriptor
|
---|
| 310 | * @param comp_desc Matched superspeed companion descriptro, if any
|
---|
[c804484] | 311 | * @return Error code.
|
---|
| 312 | */
|
---|
[9efad54] | 313 | int usb_pipe_register(usb_pipe_t *pipe, const usb_standard_endpoint_descriptor_t *ep_desc, const usb_superspeed_endpoint_companion_descriptor_t *comp_desc)
|
---|
[c804484] | 314 | {
|
---|
| 315 | assert(pipe);
|
---|
[11e9e613] | 316 | assert(pipe->bus_session);
|
---|
[9efad54] | 317 | assert(ep_desc);
|
---|
[816f5f4] | 318 |
|
---|
[11e9e613] | 319 | async_exch_t *exch = async_exchange_begin(pipe->bus_session);
|
---|
| 320 | if (!exch)
|
---|
| 321 | return ENOMEM;
|
---|
[816f5f4] | 322 |
|
---|
[9efad54] | 323 | usb_endpoint_descriptors_t descriptors;
|
---|
| 324 |
|
---|
| 325 | #define COPY(field) descriptors.endpoint.field = ep_desc->field
|
---|
| 326 | COPY(endpoint_address);
|
---|
| 327 | COPY(attributes);
|
---|
| 328 | COPY(max_packet_size);
|
---|
| 329 | COPY(poll_interval);
|
---|
| 330 | #undef COPY
|
---|
| 331 |
|
---|
| 332 | #define COPY(field) descriptors.companion.field = comp_desc->field
|
---|
| 333 | if (comp_desc) {
|
---|
| 334 | COPY(max_burst);
|
---|
| 335 | COPY(attributes);
|
---|
| 336 | COPY(bytes_per_interval);
|
---|
| 337 | }
|
---|
| 338 | #undef COPY
|
---|
[816f5f4] | 339 |
|
---|
[9efad54] | 340 | const int ret = usbhc_register_endpoint(exch, &pipe->desc, &descriptors);
|
---|
[11e9e613] | 341 | async_exchange_end(exch);
|
---|
| 342 | return ret;
|
---|
[c804484] | 343 | }
|
---|
[a76b01b4] | 344 |
|
---|
[c804484] | 345 | /** Revert endpoint registration with the host controller.
|
---|
| 346 | *
|
---|
| 347 | * @param pipe Pipe to be unregistered.
|
---|
| 348 | * @return Error code.
|
---|
| 349 | */
|
---|
| 350 | int usb_pipe_unregister(usb_pipe_t *pipe)
|
---|
| 351 | {
|
---|
| 352 | assert(pipe);
|
---|
[11e9e613] | 353 | assert(pipe->bus_session);
|
---|
| 354 | async_exch_t *exch = async_exchange_begin(pipe->bus_session);
|
---|
| 355 | if (!exch)
|
---|
| 356 | return ENOMEM;
|
---|
[816f5f4] | 357 |
|
---|
[41df71f9] | 358 | const int ret = usbhc_unregister_endpoint(exch, &pipe->desc);
|
---|
[816f5f4] | 359 |
|
---|
[11e9e613] | 360 | async_exchange_end(exch);
|
---|
| 361 | return ret;
|
---|
[c804484] | 362 | }
|
---|
| 363 |
|
---|
[6865243c] | 364 | /**
|
---|
| 365 | * @}
|
---|
| 366 | */
|
---|