| 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>
|
|---|
| 32 |
|
|---|
| 33 | #include <usb/host/usb_endpoint_manager.h>
|
|---|
| 34 |
|
|---|
| 35 | #define BUCKET_COUNT 7
|
|---|
| 36 |
|
|---|
| 37 | #define MAX_KEYS (3)
|
|---|
| 38 | typedef struct {
|
|---|
| 39 | link_t link;
|
|---|
| 40 | size_t bw;
|
|---|
| 41 | endpoint_t *ep;
|
|---|
| 42 | } node_t;
|
|---|
| 43 | /*----------------------------------------------------------------------------*/
|
|---|
| 44 | static hash_index_t node_hash(unsigned long key[])
|
|---|
| 45 | {
|
|---|
| 46 | hash_index_t hash = 0;
|
|---|
| 47 | unsigned i = 0;
|
|---|
| 48 | for (;i < MAX_KEYS; ++i) {
|
|---|
| 49 | hash ^= key[i];
|
|---|
| 50 | }
|
|---|
| 51 | hash %= BUCKET_COUNT;
|
|---|
| 52 | return hash;
|
|---|
| 53 | }
|
|---|
| 54 | /*----------------------------------------------------------------------------*/
|
|---|
| 55 | static int node_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
|---|
| 56 | {
|
|---|
| 57 | assert(item);
|
|---|
| 58 | node_t *node = hash_table_get_instance(item, node_t, link);
|
|---|
| 59 | assert(node);
|
|---|
| 60 | assert(node->ep);
|
|---|
| 61 | bool match = true;
|
|---|
| 62 | switch (keys) {
|
|---|
| 63 | case 3:
|
|---|
| 64 | match = match && (key[2] == node->ep->direction);
|
|---|
| 65 | case 2:
|
|---|
| 66 | match = match && (key[1] == (unsigned long)node->ep->endpoint);
|
|---|
| 67 | case 1:
|
|---|
| 68 | match = match && (key[0] == (unsigned long)node->ep->address);
|
|---|
| 69 | break;
|
|---|
| 70 | default:
|
|---|
| 71 | match = false;
|
|---|
| 72 | }
|
|---|
| 73 | return match;
|
|---|
| 74 | }
|
|---|
| 75 | /*----------------------------------------------------------------------------*/
|
|---|
| 76 | static void node_remove(link_t *item)
|
|---|
| 77 | {
|
|---|
| 78 | assert(item);
|
|---|
| 79 | node_t *node = hash_table_get_instance(item, node_t, link);
|
|---|
| 80 | endpoint_destroy(node->ep);
|
|---|
| 81 | free(node);
|
|---|
| 82 | }
|
|---|
| 83 | /*----------------------------------------------------------------------------*/
|
|---|
| 84 | static hash_table_operations_t op = {
|
|---|
| 85 | .hash = node_hash,
|
|---|
| 86 | .compare = node_compare,
|
|---|
| 87 | .remove_callback = node_remove,
|
|---|
| 88 | };
|
|---|
| 89 | /*----------------------------------------------------------------------------*/
|
|---|
| 90 | size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
|
|---|
| 91 | size_t size, size_t max_packet_size)
|
|---|
| 92 | {
|
|---|
| 93 | /* We care about bandwidth only for interrupt and isochronous. */
|
|---|
| 94 | if ((type != USB_TRANSFER_INTERRUPT)
|
|---|
| 95 | && (type != USB_TRANSFER_ISOCHRONOUS)) {
|
|---|
| 96 | return 0;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | const unsigned packet_count =
|
|---|
| 100 | (size + max_packet_size - 1) / max_packet_size;
|
|---|
| 101 | /* TODO: It may be that ISO and INT transfers use only one data packet
|
|---|
| 102 | * per transaction, but I did not find text in UB spec that confirms
|
|---|
| 103 | * this */
|
|---|
| 104 | /* NOTE: All data packets will be considered to be max_packet_size */
|
|---|
| 105 | switch (speed)
|
|---|
| 106 | {
|
|---|
| 107 | case USB_SPEED_LOW:
|
|---|
| 108 | assert(type == USB_TRANSFER_INTERRUPT);
|
|---|
| 109 | /* Protocol overhead 13B
|
|---|
| 110 | * (3 SYNC bytes, 3 PID bytes, 2 Endpoint + CRC bytes, 2
|
|---|
| 111 | * CRC bytes, and a 3-byte interpacket delay)
|
|---|
| 112 | * see USB spec page 45-46. */
|
|---|
| 113 | /* Speed penalty 8: low speed is 8-times slower*/
|
|---|
| 114 | return packet_count * (13 + max_packet_size) * 8;
|
|---|
| 115 | case USB_SPEED_FULL:
|
|---|
| 116 | /* Interrupt transfer overhead see above
|
|---|
| 117 | * or page 45 of USB spec */
|
|---|
| 118 | if (type == USB_TRANSFER_INTERRUPT)
|
|---|
| 119 | return packet_count * (13 + max_packet_size);
|
|---|
| 120 |
|
|---|
| 121 | assert(type == USB_TRANSFER_ISOCHRONOUS);
|
|---|
| 122 | /* Protocol overhead 9B
|
|---|
| 123 | * (2 SYNC bytes, 2 PID bytes, 2 Endpoint + CRC bytes, 2 CRC
|
|---|
| 124 | * bytes, and a 1-byte interpacket delay)
|
|---|
| 125 | * see USB spec page 42 */
|
|---|
| 126 | return packet_count * (9 + max_packet_size);
|
|---|
| 127 | default:
|
|---|
| 128 | return 0;
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 | /*----------------------------------------------------------------------------*/
|
|---|
| 132 | int usb_endpoint_manager_init(usb_endpoint_manager_t *instance,
|
|---|
| 133 | size_t available_bandwidth)
|
|---|
| 134 | {
|
|---|
| 135 | assert(instance);
|
|---|
| 136 | fibril_mutex_initialize(&instance->guard);
|
|---|
| 137 | fibril_condvar_initialize(&instance->change);
|
|---|
| 138 | instance->free_bw = available_bandwidth;
|
|---|
| 139 | bool ht =
|
|---|
| 140 | hash_table_create(&instance->ep_table, BUCKET_COUNT, MAX_KEYS, &op);
|
|---|
| 141 | return ht ? EOK : ENOMEM;
|
|---|
| 142 | }
|
|---|
| 143 | /*----------------------------------------------------------------------------*/
|
|---|
| 144 | void usb_endpoint_manager_destroy(usb_endpoint_manager_t *instance)
|
|---|
| 145 | {
|
|---|
| 146 | hash_table_destroy(&instance->ep_table);
|
|---|
| 147 | }
|
|---|
| 148 | /*----------------------------------------------------------------------------*/
|
|---|
| 149 | int usb_endpoint_manager_register_ep(usb_endpoint_manager_t *instance,
|
|---|
| 150 | endpoint_t *ep, size_t data_size)
|
|---|
| 151 | {
|
|---|
| 152 | assert(ep);
|
|---|
| 153 | size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type,
|
|---|
| 154 | data_size, ep->max_packet_size);
|
|---|
| 155 | assert(instance);
|
|---|
| 156 |
|
|---|
| 157 | unsigned long key[MAX_KEYS] =
|
|---|
| 158 | {ep->address, ep->endpoint, ep->direction};
|
|---|
| 159 | fibril_mutex_lock(&instance->guard);
|
|---|
| 160 |
|
|---|
| 161 | link_t *item =
|
|---|
| 162 | hash_table_find(&instance->ep_table, key);
|
|---|
| 163 | if (item != NULL) {
|
|---|
| 164 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 165 | return EEXISTS;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | if (bw > instance->free_bw) {
|
|---|
| 169 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 170 | return ENOSPC;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | node_t *node = malloc(sizeof(node_t));
|
|---|
| 174 | if (node == NULL) {
|
|---|
| 175 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 176 | return ENOMEM;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | node->bw = bw;
|
|---|
| 180 | node->ep = ep;
|
|---|
| 181 | link_initialize(&node->link);
|
|---|
| 182 |
|
|---|
| 183 | hash_table_insert(&instance->ep_table, key, &node->link);
|
|---|
| 184 | instance->free_bw -= bw;
|
|---|
| 185 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 186 | fibril_condvar_broadcast(&instance->change);
|
|---|
| 187 | return EOK;
|
|---|
| 188 | }
|
|---|
| 189 | /*----------------------------------------------------------------------------*/
|
|---|
| 190 | int usb_endpoint_manager_unregister_ep(usb_endpoint_manager_t *instance,
|
|---|
| 191 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
|
|---|
| 192 | {
|
|---|
| 193 | assert(instance);
|
|---|
| 194 | unsigned long key[MAX_KEYS] = {address, endpoint, direction};
|
|---|
| 195 |
|
|---|
| 196 | fibril_mutex_lock(&instance->guard);
|
|---|
| 197 | link_t *item = hash_table_find(&instance->ep_table, key);
|
|---|
| 198 | if (item == NULL) {
|
|---|
| 199 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 200 | return EINVAL;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | node_t *node = hash_table_get_instance(item, node_t, link);
|
|---|
| 204 | instance->free_bw += node->bw;
|
|---|
| 205 | hash_table_remove(&instance->ep_table, key, MAX_KEYS);
|
|---|
| 206 |
|
|---|
| 207 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 208 | fibril_condvar_broadcast(&instance->change);
|
|---|
| 209 | return EOK;
|
|---|
| 210 | }
|
|---|
| 211 | /*----------------------------------------------------------------------------*/
|
|---|
| 212 | endpoint_t * usb_endpoint_manager_get_ep(usb_endpoint_manager_t *instance,
|
|---|
| 213 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
|
|---|
| 214 | size_t *bw)
|
|---|
| 215 | {
|
|---|
| 216 | assert(instance);
|
|---|
| 217 | unsigned long key[MAX_KEYS] = {address, endpoint, direction};
|
|---|
| 218 |
|
|---|
| 219 | fibril_mutex_lock(&instance->guard);
|
|---|
| 220 | link_t *item = hash_table_find(&instance->ep_table, key);
|
|---|
| 221 | if (item == NULL) {
|
|---|
| 222 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 223 | return NULL;
|
|---|
| 224 | }
|
|---|
| 225 | node_t *node = hash_table_get_instance(item, node_t, link);
|
|---|
| 226 | if (bw)
|
|---|
| 227 | *bw = node->bw;
|
|---|
| 228 |
|
|---|
| 229 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 230 | return node->ep;
|
|---|
| 231 | }
|
|---|