[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
|
---|
[92d6868] | 37 | #define MAX_KEYS (3)
|
---|
[83c3123] | 38 |
|
---|
| 39 | static hash_index_t usb_hash(unsigned long key[])
|
---|
[f0891ce] | 40 | {
|
---|
[4cbb6e4] | 41 | /* USB endpoints use 4 bits, thus ((key[0] << 4) | key[1])
|
---|
| 42 | * produces unique value for every address.endpoint pair */
|
---|
| 43 | return ((key[0] << 4) | key[1]) % BUCKET_COUNT;
|
---|
[f0891ce] | 44 | }
|
---|
| 45 | /*----------------------------------------------------------------------------*/
|
---|
[83c3123] | 46 | static int ep_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
[f0891ce] | 47 | {
|
---|
| 48 | assert(item);
|
---|
[83c3123] | 49 | endpoint_t *ep = hash_table_get_instance(item, endpoint_t, link);
|
---|
| 50 | assert(ep);
|
---|
[92d6868] | 51 | bool match = true;
|
---|
| 52 | switch (keys) {
|
---|
| 53 | case 3:
|
---|
[4cbb6e4] | 54 | match = match &&
|
---|
[83c3123] | 55 | ((key[2] == ep->direction)
|
---|
| 56 | || (ep->direction == USB_DIRECTION_BOTH)
|
---|
| 57 | || (key[2] == USB_DIRECTION_BOTH));
|
---|
[92d6868] | 58 | case 2:
|
---|
[83c3123] | 59 | match = match && (key[1] == (unsigned long)ep->endpoint);
|
---|
[92d6868] | 60 | case 1:
|
---|
[83c3123] | 61 | match = match && (key[0] == (unsigned long)ep->address);
|
---|
[92d6868] | 62 | break;
|
---|
| 63 | default:
|
---|
| 64 | match = false;
|
---|
[f0891ce] | 65 | }
|
---|
[92d6868] | 66 | return match;
|
---|
[f0891ce] | 67 | }
|
---|
| 68 | /*----------------------------------------------------------------------------*/
|
---|
[83c3123] | 69 | static void ep_remove(link_t *item)
|
---|
[f0891ce] | 70 | {
|
---|
| 71 | assert(item);
|
---|
[83c3123] | 72 | endpoint_t *ep = hash_table_get_instance(item, endpoint_t, link);
|
---|
| 73 | endpoint_destroy(ep);
|
---|
[f0891ce] | 74 | }
|
---|
| 75 | /*----------------------------------------------------------------------------*/
|
---|
[83c3123] | 76 | static void toggle_reset_filtered(link_t *item, void *arg)
|
---|
[ba038f4] | 77 | {
|
---|
| 78 | assert(item);
|
---|
[83c3123] | 79 | endpoint_t *ep = hash_table_get_instance(item, endpoint_t, link);
|
---|
| 80 | const usb_target_t *target = arg;
|
---|
| 81 | endpoint_toggle_reset_filtered(ep, *target);
|
---|
[ba038f4] | 82 | }
|
---|
| 83 | /*----------------------------------------------------------------------------*/
|
---|
[9262571] | 84 | static hash_table_operations_t op = {
|
---|
[83c3123] | 85 | .hash = usb_hash,
|
---|
| 86 | .compare = ep_compare,
|
---|
| 87 | .remove_callback = ep_remove,
|
---|
[f0891ce] | 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 | {
|
---|
[d41f301] | 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 |
|
---|
[f0891ce] | 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,
|
---|
[933b0d7] | 133 | size_t available_bandwidth,
|
---|
| 134 | size_t (*bw_count)(usb_speed_t, usb_transfer_type_t, size_t, size_t))
|
---|
[f0891ce] | 135 | {
|
---|
| 136 | assert(instance);
|
---|
| 137 | fibril_mutex_initialize(&instance->guard);
|
---|
| 138 | instance->free_bw = available_bandwidth;
|
---|
[933b0d7] | 139 | instance->bw_count = bw_count;
|
---|
[563d9d0a] | 140 | const bool ht =
|
---|
[f0891ce] | 141 | hash_table_create(&instance->ep_table, BUCKET_COUNT, MAX_KEYS, &op);
|
---|
| 142 | return ht ? EOK : ENOMEM;
|
---|
| 143 | }
|
---|
| 144 | /*----------------------------------------------------------------------------*/
|
---|
| 145 | void usb_endpoint_manager_destroy(usb_endpoint_manager_t *instance)
|
---|
| 146 | {
|
---|
| 147 | hash_table_destroy(&instance->ep_table);
|
---|
| 148 | }
|
---|
| 149 | /*----------------------------------------------------------------------------*/
|
---|
| 150 | int usb_endpoint_manager_register_ep(usb_endpoint_manager_t *instance,
|
---|
[8dc762e0] | 151 | endpoint_t *ep, size_t data_size)
|
---|
[f0891ce] | 152 | {
|
---|
[933b0d7] | 153 | assert(instance);
|
---|
| 154 | assert(instance->bw_count);
|
---|
[8dc762e0] | 155 | assert(ep);
|
---|
[83c3123] | 156 | ep->bandwidth = instance->bw_count(ep->speed, ep->transfer_type,
|
---|
[8dc762e0] | 157 | data_size, ep->max_packet_size);
|
---|
[f0891ce] | 158 |
|
---|
[563d9d0a] | 159 | fibril_mutex_lock(&instance->guard);
|
---|
| 160 |
|
---|
[83c3123] | 161 | if (ep->bandwidth > instance->free_bw) {
|
---|
[563d9d0a] | 162 | fibril_mutex_unlock(&instance->guard);
|
---|
| 163 | return ENOSPC;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[92d6868] | 166 | unsigned long key[MAX_KEYS] =
|
---|
| 167 | {ep->address, ep->endpoint, ep->direction};
|
---|
[f0891ce] | 168 |
|
---|
[563d9d0a] | 169 | const link_t *item =
|
---|
[92d6868] | 170 | hash_table_find(&instance->ep_table, key);
|
---|
[f0891ce] | 171 | if (item != NULL) {
|
---|
| 172 | fibril_mutex_unlock(&instance->guard);
|
---|
| 173 | return EEXISTS;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[83c3123] | 176 | hash_table_insert(&instance->ep_table, key, &ep->link);
|
---|
| 177 | instance->free_bw -= ep->bandwidth;
|
---|
[f0891ce] | 178 | fibril_mutex_unlock(&instance->guard);
|
---|
| 179 | return EOK;
|
---|
| 180 | }
|
---|
| 181 | /*----------------------------------------------------------------------------*/
|
---|
| 182 | int usb_endpoint_manager_unregister_ep(usb_endpoint_manager_t *instance,
|
---|
| 183 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
| 184 | {
|
---|
| 185 | assert(instance);
|
---|
[92d6868] | 186 | unsigned long key[MAX_KEYS] = {address, endpoint, direction};
|
---|
| 187 |
|
---|
[f0891ce] | 188 | fibril_mutex_lock(&instance->guard);
|
---|
[92d6868] | 189 | link_t *item = hash_table_find(&instance->ep_table, key);
|
---|
[f0891ce] | 190 | if (item == NULL) {
|
---|
| 191 | fibril_mutex_unlock(&instance->guard);
|
---|
| 192 | return EINVAL;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[83c3123] | 195 | endpoint_t *ep = hash_table_get_instance(item, endpoint_t, link);
|
---|
| 196 | if (ep->active) {
|
---|
[563d9d0a] | 197 | fibril_mutex_unlock(&instance->guard);
|
---|
[3eeb270f] | 198 | return EBUSY;
|
---|
[563d9d0a] | 199 | }
|
---|
[3eeb270f] | 200 |
|
---|
[83c3123] | 201 | instance->free_bw += ep->bandwidth;
|
---|
[92d6868] | 202 | hash_table_remove(&instance->ep_table, key, MAX_KEYS);
|
---|
[f0891ce] | 203 |
|
---|
| 204 | fibril_mutex_unlock(&instance->guard);
|
---|
| 205 | return EOK;
|
---|
| 206 | }
|
---|
| 207 | /*----------------------------------------------------------------------------*/
|
---|
[8dc762e0] | 208 | endpoint_t * usb_endpoint_manager_get_ep(usb_endpoint_manager_t *instance,
|
---|
[83c3123] | 209 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
[f0891ce] | 210 | {
|
---|
| 211 | assert(instance);
|
---|
[92d6868] | 212 | unsigned long key[MAX_KEYS] = {address, endpoint, direction};
|
---|
| 213 |
|
---|
[f0891ce] | 214 | fibril_mutex_lock(&instance->guard);
|
---|
[563d9d0a] | 215 | const link_t *item = hash_table_find(&instance->ep_table, key);
|
---|
[f0891ce] | 216 | if (item == NULL) {
|
---|
| 217 | fibril_mutex_unlock(&instance->guard);
|
---|
| 218 | return NULL;
|
---|
| 219 | }
|
---|
[83c3123] | 220 | endpoint_t *ep = hash_table_get_instance(item, endpoint_t, link);
|
---|
[f0891ce] | 221 |
|
---|
| 222 | fibril_mutex_unlock(&instance->guard);
|
---|
[83c3123] | 223 | return ep;
|
---|
[f0891ce] | 224 | }
|
---|
[ba038f4] | 225 | /*----------------------------------------------------------------------------*/
|
---|
| 226 | /** Check setup packet data for signs of toggle reset.
|
---|
| 227 | *
|
---|
| 228 | * @param[in] instance Device keeper structure to use.
|
---|
| 229 | * @param[in] target Device to receive setup packet.
|
---|
| 230 | * @param[in] data Setup packet data.
|
---|
| 231 | *
|
---|
| 232 | * Really ugly one.
|
---|
| 233 | */
|
---|
| 234 | void usb_endpoint_manager_reset_if_need(
|
---|
| 235 | usb_endpoint_manager_t *instance, usb_target_t target, const uint8_t *data)
|
---|
| 236 | {
|
---|
| 237 | assert(instance);
|
---|
[563d9d0a] | 238 | if (!usb_target_is_valid(target)) {
|
---|
[ba038f4] | 239 | usb_log_error("Invalid data when checking for toggle reset.\n");
|
---|
| 240 | return;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[563d9d0a] | 243 | assert(data);
|
---|
[ba038f4] | 244 | switch (data[1])
|
---|
| 245 | {
|
---|
[563d9d0a] | 246 | case 0x01: /* Clear Feature -- resets only cleared ep */
|
---|
| 247 | /* Recipient is endpoint, value is zero (ENDPOINT_STALL) */
|
---|
[ba038f4] | 248 | if (((data[0] & 0xf) == 1) && ((data[2] | data[3]) == 0)) {
|
---|
| 249 | /* endpoint number is < 16, thus first byte is enough */
|
---|
| 250 | usb_target_t reset_target =
|
---|
| 251 | { .address = target.address, data[4] };
|
---|
| 252 | fibril_mutex_lock(&instance->guard);
|
---|
| 253 | hash_table_apply(&instance->ep_table,
|
---|
[83c3123] | 254 | toggle_reset_filtered, &reset_target);
|
---|
[ba038f4] | 255 | fibril_mutex_unlock(&instance->guard);
|
---|
| 256 | }
|
---|
| 257 | break;
|
---|
| 258 |
|
---|
[563d9d0a] | 259 | case 0x9: /* Set Configuration */
|
---|
| 260 | case 0x11: /* Set Interface */
|
---|
| 261 | /* Recipient must be device */
|
---|
[ba038f4] | 262 | if ((data[0] & 0xf) == 0) {
|
---|
| 263 | usb_target_t reset_target =
|
---|
| 264 | { .address = target.address, 0 };
|
---|
| 265 | fibril_mutex_lock(&instance->guard);
|
---|
| 266 | hash_table_apply(&instance->ep_table,
|
---|
[83c3123] | 267 | toggle_reset_filtered, &reset_target);
|
---|
[ba038f4] | 268 | fibril_mutex_unlock(&instance->guard);
|
---|
| 269 | }
|
---|
| 270 | break;
|
---|
| 271 | }
|
---|
| 272 | }
|
---|