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