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