[f0891ce] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
---|
| 3 | * All rights eps.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <bool.h>
|
---|
| 30 | #include <assert.h>
|
---|
| 31 | #include <errno.h>
|
---|
[90b9ab5] | 32 |
|
---|
[ba038f4] | 33 | #include <usb/debug.h>
|
---|
[f0891ce] | 34 | #include <usb/host/usb_endpoint_manager.h>
|
---|
| 35 |
|
---|
| 36 | #define BUCKET_COUNT 7
|
---|
| 37 |
|
---|
[92d6868] | 38 | #define MAX_KEYS (3)
|
---|
[f0891ce] | 39 | typedef struct {
|
---|
| 40 | link_t link;
|
---|
| 41 | size_t bw;
|
---|
[8dc762e0] | 42 | endpoint_t *ep;
|
---|
| 43 | } node_t;
|
---|
[f0891ce] | 44 | /*----------------------------------------------------------------------------*/
|
---|
[8dc762e0] | 45 | static hash_index_t node_hash(unsigned long key[])
|
---|
[f0891ce] | 46 | {
|
---|
| 47 | hash_index_t hash = 0;
|
---|
| 48 | unsigned i = 0;
|
---|
| 49 | for (;i < MAX_KEYS; ++i) {
|
---|
| 50 | hash ^= key[i];
|
---|
| 51 | }
|
---|
| 52 | hash %= BUCKET_COUNT;
|
---|
| 53 | return hash;
|
---|
| 54 | }
|
---|
| 55 | /*----------------------------------------------------------------------------*/
|
---|
[8dc762e0] | 56 | static int node_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
[f0891ce] | 57 | {
|
---|
| 58 | assert(item);
|
---|
[8dc762e0] | 59 | node_t *node = hash_table_get_instance(item, node_t, link);
|
---|
[92d6868] | 60 | assert(node);
|
---|
| 61 | assert(node->ep);
|
---|
| 62 | bool match = true;
|
---|
| 63 | switch (keys) {
|
---|
| 64 | case 3:
|
---|
| 65 | match = match && (key[2] == node->ep->direction);
|
---|
| 66 | case 2:
|
---|
| 67 | match = match && (key[1] == (unsigned long)node->ep->endpoint);
|
---|
| 68 | case 1:
|
---|
| 69 | match = match && (key[0] == (unsigned long)node->ep->address);
|
---|
| 70 | break;
|
---|
| 71 | default:
|
---|
| 72 | match = false;
|
---|
[f0891ce] | 73 | }
|
---|
[92d6868] | 74 | return match;
|
---|
[f0891ce] | 75 | }
|
---|
| 76 | /*----------------------------------------------------------------------------*/
|
---|
[8dc762e0] | 77 | static void node_remove(link_t *item)
|
---|
[f0891ce] | 78 | {
|
---|
| 79 | assert(item);
|
---|
[8dc762e0] | 80 | node_t *node = hash_table_get_instance(item, node_t, link);
|
---|
| 81 | endpoint_destroy(node->ep);
|
---|
| 82 | free(node);
|
---|
[f0891ce] | 83 | }
|
---|
| 84 | /*----------------------------------------------------------------------------*/
|
---|
[ba038f4] | 85 | static void node_toggle_reset_filtered(link_t *item, void *arg)
|
---|
| 86 | {
|
---|
| 87 | assert(item);
|
---|
| 88 | node_t *node = hash_table_get_instance(item, node_t, link);
|
---|
| 89 | usb_target_t *target = arg;
|
---|
| 90 | endpoint_toggle_reset_filtered(node->ep, *target);
|
---|
| 91 | }
|
---|
| 92 | /*----------------------------------------------------------------------------*/
|
---|
[9262571] | 93 | static hash_table_operations_t op = {
|
---|
[8dc762e0] | 94 | .hash = node_hash,
|
---|
| 95 | .compare = node_compare,
|
---|
| 96 | .remove_callback = node_remove,
|
---|
[f0891ce] | 97 | };
|
---|
| 98 | /*----------------------------------------------------------------------------*/
|
---|
| 99 | size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
|
---|
| 100 | size_t size, size_t max_packet_size)
|
---|
| 101 | {
|
---|
[d41f301] | 102 | /* We care about bandwidth only for interrupt and isochronous. */
|
---|
| 103 | if ((type != USB_TRANSFER_INTERRUPT)
|
---|
| 104 | && (type != USB_TRANSFER_ISOCHRONOUS)) {
|
---|
| 105 | return 0;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[f0891ce] | 108 | const unsigned packet_count =
|
---|
| 109 | (size + max_packet_size - 1) / max_packet_size;
|
---|
| 110 | /* TODO: It may be that ISO and INT transfers use only one data packet
|
---|
| 111 | * per transaction, but I did not find text in UB spec that confirms
|
---|
| 112 | * this */
|
---|
| 113 | /* NOTE: All data packets will be considered to be max_packet_size */
|
---|
| 114 | switch (speed)
|
---|
| 115 | {
|
---|
| 116 | case USB_SPEED_LOW:
|
---|
| 117 | assert(type == USB_TRANSFER_INTERRUPT);
|
---|
| 118 | /* Protocol overhead 13B
|
---|
| 119 | * (3 SYNC bytes, 3 PID bytes, 2 Endpoint + CRC bytes, 2
|
---|
| 120 | * CRC bytes, and a 3-byte interpacket delay)
|
---|
| 121 | * see USB spec page 45-46. */
|
---|
| 122 | /* Speed penalty 8: low speed is 8-times slower*/
|
---|
| 123 | return packet_count * (13 + max_packet_size) * 8;
|
---|
| 124 | case USB_SPEED_FULL:
|
---|
| 125 | /* Interrupt transfer overhead see above
|
---|
| 126 | * or page 45 of USB spec */
|
---|
| 127 | if (type == USB_TRANSFER_INTERRUPT)
|
---|
| 128 | return packet_count * (13 + max_packet_size);
|
---|
| 129 |
|
---|
| 130 | assert(type == USB_TRANSFER_ISOCHRONOUS);
|
---|
| 131 | /* Protocol overhead 9B
|
---|
| 132 | * (2 SYNC bytes, 2 PID bytes, 2 Endpoint + CRC bytes, 2 CRC
|
---|
| 133 | * bytes, and a 1-byte interpacket delay)
|
---|
| 134 | * see USB spec page 42 */
|
---|
| 135 | return packet_count * (9 + max_packet_size);
|
---|
| 136 | default:
|
---|
| 137 | return 0;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | /*----------------------------------------------------------------------------*/
|
---|
| 141 | int usb_endpoint_manager_init(usb_endpoint_manager_t *instance,
|
---|
| 142 | size_t available_bandwidth)
|
---|
| 143 | {
|
---|
| 144 | assert(instance);
|
---|
| 145 | fibril_mutex_initialize(&instance->guard);
|
---|
| 146 | fibril_condvar_initialize(&instance->change);
|
---|
| 147 | instance->free_bw = available_bandwidth;
|
---|
| 148 | bool ht =
|
---|
| 149 | hash_table_create(&instance->ep_table, BUCKET_COUNT, MAX_KEYS, &op);
|
---|
| 150 | return ht ? EOK : ENOMEM;
|
---|
| 151 | }
|
---|
| 152 | /*----------------------------------------------------------------------------*/
|
---|
| 153 | void usb_endpoint_manager_destroy(usb_endpoint_manager_t *instance)
|
---|
| 154 | {
|
---|
| 155 | hash_table_destroy(&instance->ep_table);
|
---|
| 156 | }
|
---|
| 157 | /*----------------------------------------------------------------------------*/
|
---|
| 158 | int usb_endpoint_manager_register_ep(usb_endpoint_manager_t *instance,
|
---|
[8dc762e0] | 159 | endpoint_t *ep, size_t data_size)
|
---|
[f0891ce] | 160 | {
|
---|
[8dc762e0] | 161 | assert(ep);
|
---|
| 162 | size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type,
|
---|
| 163 | data_size, ep->max_packet_size);
|
---|
[f0891ce] | 164 | assert(instance);
|
---|
| 165 |
|
---|
[92d6868] | 166 | unsigned long key[MAX_KEYS] =
|
---|
| 167 | {ep->address, ep->endpoint, ep->direction};
|
---|
[f0891ce] | 168 | fibril_mutex_lock(&instance->guard);
|
---|
| 169 |
|
---|
| 170 | link_t *item =
|
---|
[92d6868] | 171 | hash_table_find(&instance->ep_table, key);
|
---|
[f0891ce] | 172 | if (item != NULL) {
|
---|
| 173 | fibril_mutex_unlock(&instance->guard);
|
---|
| 174 | return EEXISTS;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | if (bw > instance->free_bw) {
|
---|
| 178 | fibril_mutex_unlock(&instance->guard);
|
---|
| 179 | return ENOSPC;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[8dc762e0] | 182 | node_t *node = malloc(sizeof(node_t));
|
---|
| 183 | if (node == NULL) {
|
---|
[f0891ce] | 184 | fibril_mutex_unlock(&instance->guard);
|
---|
| 185 | return ENOMEM;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[8dc762e0] | 188 | node->bw = bw;
|
---|
| 189 | node->ep = ep;
|
---|
| 190 | link_initialize(&node->link);
|
---|
[f0891ce] | 191 |
|
---|
[92d6868] | 192 | hash_table_insert(&instance->ep_table, key, &node->link);
|
---|
[f0891ce] | 193 | instance->free_bw -= bw;
|
---|
| 194 | fibril_mutex_unlock(&instance->guard);
|
---|
| 195 | fibril_condvar_broadcast(&instance->change);
|
---|
| 196 | return EOK;
|
---|
| 197 | }
|
---|
| 198 | /*----------------------------------------------------------------------------*/
|
---|
| 199 | int usb_endpoint_manager_unregister_ep(usb_endpoint_manager_t *instance,
|
---|
| 200 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
| 201 | {
|
---|
| 202 | assert(instance);
|
---|
[92d6868] | 203 | unsigned long key[MAX_KEYS] = {address, endpoint, direction};
|
---|
| 204 |
|
---|
[f0891ce] | 205 | fibril_mutex_lock(&instance->guard);
|
---|
[92d6868] | 206 | link_t *item = hash_table_find(&instance->ep_table, key);
|
---|
[f0891ce] | 207 | if (item == NULL) {
|
---|
| 208 | fibril_mutex_unlock(&instance->guard);
|
---|
| 209 | return EINVAL;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[8dc762e0] | 212 | node_t *node = hash_table_get_instance(item, node_t, link);
|
---|
[3eeb270f] | 213 | if (node->ep->active)
|
---|
| 214 | return EBUSY;
|
---|
| 215 |
|
---|
[8dc762e0] | 216 | instance->free_bw += node->bw;
|
---|
[92d6868] | 217 | hash_table_remove(&instance->ep_table, key, MAX_KEYS);
|
---|
[f0891ce] | 218 |
|
---|
| 219 | fibril_mutex_unlock(&instance->guard);
|
---|
| 220 | fibril_condvar_broadcast(&instance->change);
|
---|
| 221 | return EOK;
|
---|
| 222 | }
|
---|
| 223 | /*----------------------------------------------------------------------------*/
|
---|
[8dc762e0] | 224 | endpoint_t * usb_endpoint_manager_get_ep(usb_endpoint_manager_t *instance,
|
---|
[f0891ce] | 225 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
|
---|
| 226 | size_t *bw)
|
---|
| 227 | {
|
---|
| 228 | assert(instance);
|
---|
[92d6868] | 229 | unsigned long key[MAX_KEYS] = {address, endpoint, direction};
|
---|
| 230 |
|
---|
[f0891ce] | 231 | fibril_mutex_lock(&instance->guard);
|
---|
[92d6868] | 232 | link_t *item = hash_table_find(&instance->ep_table, key);
|
---|
[f0891ce] | 233 | if (item == NULL) {
|
---|
| 234 | fibril_mutex_unlock(&instance->guard);
|
---|
| 235 | return NULL;
|
---|
| 236 | }
|
---|
[8dc762e0] | 237 | node_t *node = hash_table_get_instance(item, node_t, link);
|
---|
[f0891ce] | 238 | if (bw)
|
---|
[8dc762e0] | 239 | *bw = node->bw;
|
---|
[f0891ce] | 240 |
|
---|
| 241 | fibril_mutex_unlock(&instance->guard);
|
---|
[8dc762e0] | 242 | return node->ep;
|
---|
[f0891ce] | 243 | }
|
---|
[ba038f4] | 244 | /*----------------------------------------------------------------------------*/
|
---|
| 245 | /** Check setup packet data for signs of toggle reset.
|
---|
| 246 | *
|
---|
| 247 | * @param[in] instance Device keeper structure to use.
|
---|
| 248 | * @param[in] target Device to receive setup packet.
|
---|
| 249 | * @param[in] data Setup packet data.
|
---|
| 250 | *
|
---|
| 251 | * Really ugly one.
|
---|
| 252 | */
|
---|
| 253 | void usb_endpoint_manager_reset_if_need(
|
---|
| 254 | usb_endpoint_manager_t *instance, usb_target_t target, const uint8_t *data)
|
---|
| 255 | {
|
---|
| 256 | assert(instance);
|
---|
| 257 | if (target.endpoint > 15 || target.endpoint < 0
|
---|
| 258 | || target.address >= USB11_ADDRESS_MAX || target.address < 0) {
|
---|
| 259 | usb_log_error("Invalid data when checking for toggle reset.\n");
|
---|
| 260 | return;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | switch (data[1])
|
---|
| 264 | {
|
---|
| 265 | case 0x01: /*clear feature*/
|
---|
| 266 | /* recipient is endpoint, value is zero (ENDPOINT_STALL) */
|
---|
| 267 | if (((data[0] & 0xf) == 1) && ((data[2] | data[3]) == 0)) {
|
---|
| 268 | /* endpoint number is < 16, thus first byte is enough */
|
---|
| 269 | usb_target_t reset_target =
|
---|
| 270 | { .address = target.address, data[4] };
|
---|
| 271 | fibril_mutex_lock(&instance->guard);
|
---|
| 272 | hash_table_apply(&instance->ep_table,
|
---|
| 273 | node_toggle_reset_filtered, &reset_target);
|
---|
| 274 | fibril_mutex_unlock(&instance->guard);
|
---|
| 275 | }
|
---|
| 276 | break;
|
---|
| 277 |
|
---|
| 278 | case 0x9: /* set configuration */
|
---|
| 279 | case 0x11: /* set interface */
|
---|
| 280 | /* target must be device */
|
---|
| 281 | if ((data[0] & 0xf) == 0) {
|
---|
| 282 | usb_target_t reset_target =
|
---|
| 283 | { .address = target.address, 0 };
|
---|
| 284 | fibril_mutex_lock(&instance->guard);
|
---|
| 285 | hash_table_apply(&instance->ep_table,
|
---|
| 286 | node_toggle_reset_filtered, &reset_target);
|
---|
| 287 | fibril_mutex_unlock(&instance->guard);
|
---|
| 288 | }
|
---|
| 289 | break;
|
---|
| 290 | }
|
---|
| 291 | }
|
---|