| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
|---|
| 3 | * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
|
|---|
| 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 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup libusbhost
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | * @brief UHCI host controller driver structure
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <assert.h>
|
|---|
| 38 | #include <atomic.h>
|
|---|
| 39 | #include <mem.h>
|
|---|
| 40 | #include <stdlib.h>
|
|---|
| 41 | #include <str_error.h>
|
|---|
| 42 | #include <usb/debug.h>
|
|---|
| 43 | #include <usb/descriptor.h>
|
|---|
| 44 | #include <usb/host/hcd.h>
|
|---|
| 45 | #include <usb/host/utility.h>
|
|---|
| 46 |
|
|---|
| 47 | #include "usb_transfer_batch.h"
|
|---|
| 48 | #include "bus.h"
|
|---|
| 49 |
|
|---|
| 50 | #include "endpoint.h"
|
|---|
| 51 |
|
|---|
| 52 | /**
|
|---|
| 53 | * Initialize provided endpoint structure.
|
|---|
| 54 | */
|
|---|
| 55 | void endpoint_init(endpoint_t *ep, device_t *dev, const usb_endpoint_descriptors_t *desc)
|
|---|
| 56 | {
|
|---|
| 57 | memset(ep, 0, sizeof(endpoint_t));
|
|---|
| 58 |
|
|---|
| 59 | assert(dev);
|
|---|
| 60 | ep->device = dev;
|
|---|
| 61 |
|
|---|
| 62 | atomic_set(&ep->refcnt, 0);
|
|---|
| 63 | fibril_condvar_initialize(&ep->avail);
|
|---|
| 64 |
|
|---|
| 65 | ep->endpoint = USB_ED_GET_EP(desc->endpoint);
|
|---|
| 66 | ep->direction = USB_ED_GET_DIR(desc->endpoint);
|
|---|
| 67 | ep->transfer_type = USB_ED_GET_TRANSFER_TYPE(desc->endpoint);
|
|---|
| 68 | ep->max_packet_size = USB_ED_GET_MPS(desc->endpoint);
|
|---|
| 69 | ep->packets_per_uframe = USB_ED_GET_ADD_OPPS(desc->endpoint) + 1;
|
|---|
| 70 |
|
|---|
| 71 | /** Direction both is our construct never present in descriptors */
|
|---|
| 72 | if (ep->transfer_type == USB_TRANSFER_CONTROL)
|
|---|
| 73 | ep->direction = USB_DIRECTION_BOTH;
|
|---|
| 74 |
|
|---|
| 75 | ep->max_transfer_size = ep->max_packet_size * ep->packets_per_uframe;
|
|---|
| 76 | ep->transfer_buffer_policy = DMA_POLICY_STRICT;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | * Get the bus endpoint belongs to.
|
|---|
| 81 | */
|
|---|
| 82 | static inline const bus_ops_t *get_bus_ops(endpoint_t *ep)
|
|---|
| 83 | {
|
|---|
| 84 | return ep->device->bus->ops;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /**
|
|---|
| 88 | * Increase the reference count on endpoint.
|
|---|
| 89 | */
|
|---|
| 90 | void endpoint_add_ref(endpoint_t *ep)
|
|---|
| 91 | {
|
|---|
| 92 | atomic_inc(&ep->refcnt);
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| 96 | * Call the desctruction callback. Default behavior is to free the memory directly.
|
|---|
| 97 | */
|
|---|
| 98 | static inline void endpoint_destroy(endpoint_t *ep)
|
|---|
| 99 | {
|
|---|
| 100 | const bus_ops_t *ops = get_bus_ops(ep);
|
|---|
| 101 | if (ops->endpoint_destroy) {
|
|---|
| 102 | ops->endpoint_destroy(ep);
|
|---|
| 103 | } else {
|
|---|
| 104 | assert(ep->active_batch == NULL);
|
|---|
| 105 |
|
|---|
| 106 | /* Assume mostly the eps will be allocated by malloc. */
|
|---|
| 107 | free(ep);
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | /**
|
|---|
| 112 | * Decrease the reference count.
|
|---|
| 113 | */
|
|---|
| 114 | void endpoint_del_ref(endpoint_t *ep)
|
|---|
| 115 | {
|
|---|
| 116 | if (atomic_predec(&ep->refcnt) == 0) {
|
|---|
| 117 | endpoint_destroy(ep);
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | /**
|
|---|
| 122 | * Mark the endpoint as online. Supply a guard to be used for this endpoint
|
|---|
| 123 | * synchronization.
|
|---|
| 124 | */
|
|---|
| 125 | void endpoint_set_online(endpoint_t *ep, fibril_mutex_t *guard)
|
|---|
| 126 | {
|
|---|
| 127 | ep->guard = guard;
|
|---|
| 128 | ep->online = true;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * Mark the endpoint as offline. All other fibrils waiting to activate this
|
|---|
| 133 | * endpoint will be interrupted.
|
|---|
| 134 | */
|
|---|
| 135 | void endpoint_set_offline_locked(endpoint_t *ep)
|
|---|
| 136 | {
|
|---|
| 137 | assert(ep);
|
|---|
| 138 | assert(fibril_mutex_is_locked(ep->guard));
|
|---|
| 139 |
|
|---|
| 140 | ep->online = false;
|
|---|
| 141 | fibril_condvar_broadcast(&ep->avail);
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /**
|
|---|
| 145 | * Wait until a transfer finishes. Can be used even when the endpoint is
|
|---|
| 146 | * offline (and is interrupted by the endpoint going offline).
|
|---|
| 147 | */
|
|---|
| 148 | void endpoint_wait_timeout_locked(endpoint_t *ep, suseconds_t timeout)
|
|---|
| 149 | {
|
|---|
| 150 | assert(ep);
|
|---|
| 151 | assert(fibril_mutex_is_locked(ep->guard));
|
|---|
| 152 |
|
|---|
| 153 | if (ep->active_batch == NULL)
|
|---|
| 154 | return;
|
|---|
| 155 |
|
|---|
| 156 | fibril_condvar_wait_timeout(&ep->avail, ep->guard, timeout);
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | /**
|
|---|
| 160 | * Mark the endpoint as active and block access for further fibrils. If the
|
|---|
| 161 | * endpoint is already active, it will block on ep->avail condvar.
|
|---|
| 162 | *
|
|---|
| 163 | * Call only under endpoint guard. After you activate the endpoint and release
|
|---|
| 164 | * the guard, you must assume that particular transfer is already
|
|---|
| 165 | * finished/aborted.
|
|---|
| 166 | *
|
|---|
| 167 | * Activation and deactivation is not done by the library to maximize
|
|---|
| 168 | * performance. The HC might want to prepare some memory buffers prior to
|
|---|
| 169 | * interfering with other world.
|
|---|
| 170 | *
|
|---|
| 171 | * @param batch Transfer batch this endpoint is blocked by.
|
|---|
| 172 | */
|
|---|
| 173 | int endpoint_activate_locked(endpoint_t *ep, usb_transfer_batch_t *batch)
|
|---|
| 174 | {
|
|---|
| 175 | assert(ep);
|
|---|
| 176 | assert(batch);
|
|---|
| 177 | assert(batch->ep == ep);
|
|---|
| 178 | assert(ep->guard);
|
|---|
| 179 | assert(fibril_mutex_is_locked(ep->guard));
|
|---|
| 180 |
|
|---|
| 181 | while (ep->online && ep->active_batch != NULL)
|
|---|
| 182 | fibril_condvar_wait(&ep->avail, ep->guard);
|
|---|
| 183 |
|
|---|
| 184 | if (!ep->online)
|
|---|
| 185 | return EINTR;
|
|---|
| 186 |
|
|---|
| 187 | assert(ep->active_batch == NULL);
|
|---|
| 188 | ep->active_batch = batch;
|
|---|
| 189 | return EOK;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | /**
|
|---|
| 193 | * Mark the endpoint as inactive and allow access for further fibrils.
|
|---|
| 194 | */
|
|---|
| 195 | void endpoint_deactivate_locked(endpoint_t *ep)
|
|---|
| 196 | {
|
|---|
| 197 | assert(ep);
|
|---|
| 198 | assert(fibril_mutex_is_locked(ep->guard));
|
|---|
| 199 |
|
|---|
| 200 | ep->active_batch = NULL;
|
|---|
| 201 | fibril_condvar_signal(&ep->avail);
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | /**
|
|---|
| 205 | * Initiate a transfer on an endpoint. Creates a transfer batch, checks the
|
|---|
| 206 | * bandwidth requirements and schedules the batch.
|
|---|
| 207 | *
|
|---|
| 208 | * @param endpoint Endpoint for which to send the batch
|
|---|
| 209 | * @param target The target of the transfer.
|
|---|
| 210 | * @param direction A direction of the transfer.
|
|---|
| 211 | * @param data A pointer to the data buffer.
|
|---|
| 212 | * @param size Size of the data buffer.
|
|---|
| 213 | * @param setup_data Data to use in the setup stage (Control communication type)
|
|---|
| 214 | * @param on_complete Callback which is called after the batch is complete
|
|---|
| 215 | * @param arg Callback parameter.
|
|---|
| 216 | * @param name Communication identifier (for nicer output).
|
|---|
| 217 | */
|
|---|
| 218 | errno_t endpoint_send_batch(endpoint_t *ep, usb_target_t target,
|
|---|
| 219 | usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
|
|---|
| 220 | usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
|
|---|
| 221 | {
|
|---|
| 222 | if (!ep)
|
|---|
| 223 | return EBADMEM;
|
|---|
| 224 |
|
|---|
| 225 | if (ep->transfer_type == USB_TRANSFER_CONTROL) {
|
|---|
| 226 | usb_log_debug("%s %d:%d %zu/%zuB, setup %#016" PRIx64, name,
|
|---|
| 227 | target.address, target.endpoint, size, ep->max_packet_size,
|
|---|
| 228 | setup_data);
|
|---|
| 229 | } else {
|
|---|
| 230 | usb_log_debug("%s %d:%d %zu/%zuB", name, target.address,
|
|---|
| 231 | target.endpoint, size, ep->max_packet_size);
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | device_t * const device = ep->device;
|
|---|
| 235 | if (!device) {
|
|---|
| 236 | usb_log_warning("Endpoint detached");
|
|---|
| 237 | return EAGAIN;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | const bus_ops_t *ops = device->bus->ops;
|
|---|
| 241 | if (!ops->batch_schedule) {
|
|---|
| 242 | usb_log_error("HCD does not implement scheduler.");
|
|---|
| 243 | return ENOTSUP;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | /*
|
|---|
| 247 | * Limit transfers with reserved bandwidth to the amount reserved.
|
|---|
| 248 | * OUT transfers are rejected, IN can be just trimmed in advance.
|
|---|
| 249 | */
|
|---|
| 250 | if ((ep->transfer_type == USB_TRANSFER_INTERRUPT || ep->transfer_type == USB_TRANSFER_ISOCHRONOUS) && size > ep->max_transfer_size) {
|
|---|
| 251 | if (direction == USB_DIRECTION_OUT)
|
|---|
| 252 | return ENOSPC;
|
|---|
| 253 | else
|
|---|
| 254 | size = ep->max_transfer_size;
|
|---|
| 255 |
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | /* Offline devices don't schedule transfers other than on EP0. */
|
|---|
| 259 | if (!device->online && ep->endpoint > 0)
|
|---|
| 260 | return EAGAIN;
|
|---|
| 261 |
|
|---|
| 262 | usb_transfer_batch_t *batch = usb_transfer_batch_create(ep);
|
|---|
| 263 | if (!batch) {
|
|---|
| 264 | usb_log_error("Failed to create transfer batch.");
|
|---|
| 265 | return ENOMEM;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | batch->target = target;
|
|---|
| 269 | batch->setup.packed = setup_data;
|
|---|
| 270 | batch->dir = direction;
|
|---|
| 271 | batch->buffer_size = size;
|
|---|
| 272 |
|
|---|
| 273 | errno_t err;
|
|---|
| 274 | if ((err = usb_transfer_batch_prepare_buffer(batch, data))) {
|
|---|
| 275 | usb_log_warning("Failed to prepare buffer for batch: %s", str_error(err));
|
|---|
| 276 | usb_transfer_batch_destroy(batch);
|
|---|
| 277 | return err;
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | batch->on_complete = on_complete;
|
|---|
| 281 | batch->on_complete_data = arg;
|
|---|
| 282 |
|
|---|
| 283 | const int ret = ops->batch_schedule(batch);
|
|---|
| 284 | if (ret != EOK) {
|
|---|
| 285 | usb_log_warning("Batch %p failed to schedule: %s", batch, str_error(ret));
|
|---|
| 286 | usb_transfer_batch_destroy(batch);
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | return ret;
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | /**
|
|---|
| 293 | * @}
|
|---|
| 294 | */
|
|---|