| 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 |
|
|---|
| 42 | #include "usb_transfer_batch.h"
|
|---|
| 43 | #include "bus.h"
|
|---|
| 44 |
|
|---|
| 45 | #include "endpoint.h"
|
|---|
| 46 |
|
|---|
| 47 | /** Initialize provided endpoint structure.
|
|---|
| 48 | */
|
|---|
| 49 | void endpoint_init(endpoint_t *ep, device_t *dev, const usb_endpoint_desc_t *desc)
|
|---|
| 50 | {
|
|---|
| 51 | memset(ep, 0, sizeof(endpoint_t));
|
|---|
| 52 |
|
|---|
| 53 | assert(dev);
|
|---|
| 54 | ep->device = dev;
|
|---|
| 55 |
|
|---|
| 56 | atomic_set(&ep->refcnt, 0);
|
|---|
| 57 | link_initialize(&ep->link);
|
|---|
| 58 | fibril_mutex_initialize(&ep->guard);
|
|---|
| 59 | fibril_condvar_initialize(&ep->avail);
|
|---|
| 60 |
|
|---|
| 61 | ep->endpoint = desc->endpoint_no;
|
|---|
| 62 | ep->direction = desc->direction;
|
|---|
| 63 | ep->transfer_type = desc->transfer_type;
|
|---|
| 64 | ep->max_packet_size = desc->max_packet_size;
|
|---|
| 65 | ep->packets = desc->packets;
|
|---|
| 66 |
|
|---|
| 67 | ep->bandwidth = endpoint_count_bw(ep, desc->max_packet_size);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | static inline const bus_ops_t *get_bus_ops(endpoint_t *ep)
|
|---|
| 71 | {
|
|---|
| 72 | return ep->device->bus->ops;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | void endpoint_add_ref(endpoint_t *ep)
|
|---|
| 76 | {
|
|---|
| 77 | atomic_inc(&ep->refcnt);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | static inline void endpoint_destroy(endpoint_t *ep)
|
|---|
| 81 | {
|
|---|
| 82 | const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_destroy);
|
|---|
| 83 | if (ops) {
|
|---|
| 84 | ops->endpoint_destroy(ep);
|
|---|
| 85 | } else {
|
|---|
| 86 | assert(ep->active_batch == NULL);
|
|---|
| 87 |
|
|---|
| 88 | /* Assume mostly the eps will be allocated by malloc. */
|
|---|
| 89 | free(ep);
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | void endpoint_del_ref(endpoint_t *ep)
|
|---|
| 94 | {
|
|---|
| 95 | if (atomic_predec(&ep->refcnt) == 0) {
|
|---|
| 96 | endpoint_destroy(ep);
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /** Mark the endpoint as active and block access for further fibrils.
|
|---|
| 101 | * @param ep endpoint_t structure.
|
|---|
| 102 | */
|
|---|
| 103 | void endpoint_activate_locked(endpoint_t *ep, usb_transfer_batch_t *batch)
|
|---|
| 104 | {
|
|---|
| 105 | assert(ep);
|
|---|
| 106 | assert(batch);
|
|---|
| 107 | assert(batch->ep == ep);
|
|---|
| 108 | assert(fibril_mutex_is_locked(&ep->guard));
|
|---|
| 109 |
|
|---|
| 110 | while (ep->active_batch != NULL)
|
|---|
| 111 | fibril_condvar_wait(&ep->avail, &ep->guard);
|
|---|
| 112 | ep->active_batch = batch;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /** Mark the endpoint as inactive and allow access for further fibrils.
|
|---|
| 116 | * @param ep endpoint_t structure.
|
|---|
| 117 | */
|
|---|
| 118 | void endpoint_deactivate_locked(endpoint_t *ep)
|
|---|
| 119 | {
|
|---|
| 120 | assert(ep);
|
|---|
| 121 | assert(fibril_mutex_is_locked(&ep->guard));
|
|---|
| 122 |
|
|---|
| 123 | if (ep->active_batch && ep->active_batch->error == EOK)
|
|---|
| 124 | usb_transfer_batch_reset_toggle(ep->active_batch);
|
|---|
| 125 |
|
|---|
| 126 | ep->active_batch = NULL;
|
|---|
| 127 | fibril_condvar_signal(&ep->avail);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /** Abort an active batch on endpoint, if any.
|
|---|
| 131 | *
|
|---|
| 132 | * @param[in] ep endpoint_t structure.
|
|---|
| 133 | */
|
|---|
| 134 | void endpoint_abort(endpoint_t *ep)
|
|---|
| 135 | {
|
|---|
| 136 | assert(ep);
|
|---|
| 137 |
|
|---|
| 138 | fibril_mutex_lock(&ep->guard);
|
|---|
| 139 | usb_transfer_batch_t *batch = ep->active_batch;
|
|---|
| 140 | endpoint_deactivate_locked(ep);
|
|---|
| 141 | fibril_mutex_unlock(&ep->guard);
|
|---|
| 142 |
|
|---|
| 143 | if (batch)
|
|---|
| 144 | usb_transfer_batch_abort(batch);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /** Get the value of toggle bit. Either uses the toggle_get op, or just returns
|
|---|
| 148 | * the value of the toggle.
|
|---|
| 149 | * @param ep endpoint_t structure.
|
|---|
| 150 | */
|
|---|
| 151 | int endpoint_toggle_get(endpoint_t *ep)
|
|---|
| 152 | {
|
|---|
| 153 | assert(ep);
|
|---|
| 154 |
|
|---|
| 155 | const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_get_toggle);
|
|---|
| 156 | return ops
|
|---|
| 157 | ? ops->endpoint_get_toggle(ep)
|
|---|
| 158 | : ep->toggle;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /** Set the value of toggle bit. Either uses the toggle_set op, or just sets
|
|---|
| 162 | * the toggle inside.
|
|---|
| 163 | * @param ep endpoint_t structure.
|
|---|
| 164 | */
|
|---|
| 165 | void endpoint_toggle_set(endpoint_t *ep, bool toggle)
|
|---|
| 166 | {
|
|---|
| 167 | assert(ep);
|
|---|
| 168 |
|
|---|
| 169 | const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_set_toggle);
|
|---|
| 170 | if (ops) {
|
|---|
| 171 | ops->endpoint_set_toggle(ep, toggle);
|
|---|
| 172 | }
|
|---|
| 173 | else {
|
|---|
| 174 | ep->toggle = toggle;
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | ssize_t endpoint_count_bw(endpoint_t *ep, size_t packet_size)
|
|---|
| 179 | {
|
|---|
| 180 | assert(ep);
|
|---|
| 181 |
|
|---|
| 182 | const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_count_bw);
|
|---|
| 183 | if (!ops)
|
|---|
| 184 | return 0;
|
|---|
| 185 |
|
|---|
| 186 | return ops->endpoint_count_bw(ep, packet_size);
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | /**
|
|---|
| 190 | * @}
|
|---|
| 191 | */
|
|---|