| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
|---|
| 3 | * All rights reserved.
|
|---|
| 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 <assert.h>
|
|---|
| 30 | #include <errno.h>
|
|---|
| 31 | #include <usb/host/bandwidth.h>
|
|---|
| 32 |
|
|---|
| 33 | typedef struct {
|
|---|
| 34 | usb_address_t address;
|
|---|
| 35 | usb_endpoint_t endpoint;
|
|---|
| 36 | usb_direction_t direction;
|
|---|
| 37 | } __attribute__((aligned (sizeof(unsigned long)))) transfer_t;
|
|---|
| 38 | /*----------------------------------------------------------------------------*/
|
|---|
| 39 | typedef struct {
|
|---|
| 40 | transfer_t transfer;
|
|---|
| 41 | link_t link;
|
|---|
| 42 | bool used;
|
|---|
| 43 | size_t required;
|
|---|
| 44 | } transfer_status_t;
|
|---|
| 45 | /*----------------------------------------------------------------------------*/
|
|---|
| 46 | #define BUCKET_COUNT 7
|
|---|
| 47 | #define MAX_KEYS (sizeof(transfer_t) / sizeof(unsigned long))
|
|---|
| 48 | /*----------------------------------------------------------------------------*/
|
|---|
| 49 | static hash_index_t transfer_hash(unsigned long key[])
|
|---|
| 50 | {
|
|---|
| 51 | hash_index_t hash = 0;
|
|---|
| 52 | unsigned i = 0;
|
|---|
| 53 | for (;i < MAX_KEYS; ++i) {
|
|---|
| 54 | hash ^= key[i];
|
|---|
| 55 | }
|
|---|
| 56 | hash %= BUCKET_COUNT;
|
|---|
| 57 | return hash;
|
|---|
| 58 | }
|
|---|
| 59 | /*----------------------------------------------------------------------------*/
|
|---|
| 60 | static int trans_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
|---|
| 61 | {
|
|---|
| 62 | assert(item);
|
|---|
| 63 | transfer_status_t *status =
|
|---|
| 64 | hash_table_get_instance(item, transfer_status_t, link);
|
|---|
| 65 | const size_t bytes =
|
|---|
| 66 | keys < MAX_KEYS ? keys * sizeof(unsigned long) : sizeof(transfer_t);
|
|---|
| 67 | return bcmp(key, &status->transfer, bytes);
|
|---|
| 68 | }
|
|---|
| 69 | /*----------------------------------------------------------------------------*/
|
|---|
| 70 | static void dummy(link_t *item) {}
|
|---|
| 71 | /*----------------------------------------------------------------------------*/
|
|---|
| 72 | hash_table_operations_t op = {
|
|---|
| 73 | .hash = transfer_hash,
|
|---|
| 74 | .compare = trans_compare,
|
|---|
| 75 | .remove_callback = dummy,
|
|---|
| 76 | };
|
|---|
| 77 | /*----------------------------------------------------------------------------*/
|
|---|
| 78 | int bandwidth_init(bandwidth_t *instance)
|
|---|
| 79 | {
|
|---|
| 80 | assert(instance);
|
|---|
| 81 | fibril_mutex_initialize(&instance->guard);
|
|---|
| 82 | return
|
|---|
| 83 | hash_table_create(&instance->reserved, BUCKET_COUNT, MAX_KEYS, &op);
|
|---|
| 84 | }
|
|---|
| 85 | /*----------------------------------------------------------------------------*/
|
|---|
| 86 | void bandwidth_destroy(bandwidth_t *instance)
|
|---|
| 87 | {
|
|---|
| 88 | hash_table_destroy(&instance->reserved);
|
|---|
| 89 | }
|
|---|
| 90 | /*----------------------------------------------------------------------------*/
|
|---|
| 91 | int bandwidth_reserve(bandwidth_t *instance, usb_address_t address,
|
|---|
| 92 | usb_endpoint_t endpoint, usb_direction_t direction, usb_speed_t speed,
|
|---|
| 93 | usb_transfer_type_t transfer_type, size_t max_packet_size, size_t size,
|
|---|
| 94 | unsigned interval)
|
|---|
| 95 | {
|
|---|
| 96 | assert(instance);
|
|---|
| 97 | transfer_t trans = {
|
|---|
| 98 | .address = address,
|
|---|
| 99 | .endpoint = endpoint,
|
|---|
| 100 | .direction = direction,
|
|---|
| 101 | };
|
|---|
| 102 | fibril_mutex_lock(&instance->guard);
|
|---|
| 103 | link_t *item =
|
|---|
| 104 | hash_table_find(&instance->reserved, (unsigned long*)&trans);
|
|---|
| 105 | if (item != NULL) {
|
|---|
| 106 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 107 | return EEXISTS;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | transfer_status_t *status = malloc(sizeof(transfer_status_t));
|
|---|
| 111 | if (status == NULL) {
|
|---|
| 112 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 113 | return ENOMEM;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | status->transfer = trans;
|
|---|
| 117 | status->required = 0;
|
|---|
| 118 | status->used = false;
|
|---|
| 119 | link_initialize(&status->link);
|
|---|
| 120 |
|
|---|
| 121 | hash_table_insert(&instance->reserved,
|
|---|
| 122 | (unsigned long*)&status->transfer, &status->link);
|
|---|
| 123 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 124 | return EOK;
|
|---|
| 125 | /* TODO: compute bandwidth used */
|
|---|
| 126 | }
|
|---|
| 127 | /*----------------------------------------------------------------------------*/
|
|---|
| 128 | int bandwidth_release(bandwidth_t *instance, usb_address_t address,
|
|---|
| 129 | usb_endpoint_t endpoint, usb_direction_t direction)
|
|---|
| 130 | {
|
|---|
| 131 | assert(instance);
|
|---|
| 132 | transfer_t trans = {
|
|---|
| 133 | .address = address,
|
|---|
| 134 | .endpoint = endpoint,
|
|---|
| 135 | .direction = direction,
|
|---|
| 136 | };
|
|---|
| 137 | fibril_mutex_lock(&instance->guard);
|
|---|
| 138 | link_t *item =
|
|---|
| 139 | hash_table_find(&instance->reserved, (unsigned long*)&trans);
|
|---|
| 140 | if (item == NULL) {
|
|---|
| 141 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 142 | return EINVAL;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | hash_table_remove(&instance->reserved,
|
|---|
| 146 | (unsigned long*)&trans, MAX_KEYS);
|
|---|
| 147 |
|
|---|
| 148 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 149 | return EOK;
|
|---|
| 150 | /* TODO: compute bandwidth freed */
|
|---|
| 151 | }
|
|---|
| 152 | /*----------------------------------------------------------------------------*/
|
|---|
| 153 | int bandwidth_use(bandwidth_t *instance, usb_address_t address,
|
|---|
| 154 | usb_endpoint_t endpoint, usb_direction_t direction)
|
|---|
| 155 | {
|
|---|
| 156 | assert(instance);
|
|---|
| 157 | transfer_t trans = {
|
|---|
| 158 | .address = address,
|
|---|
| 159 | .endpoint = endpoint,
|
|---|
| 160 | .direction = direction,
|
|---|
| 161 | };
|
|---|
| 162 | fibril_mutex_lock(&instance->guard);
|
|---|
| 163 | link_t *item =
|
|---|
| 164 | hash_table_find(&instance->reserved, (unsigned long*)&trans);
|
|---|
| 165 | int ret = EOK;
|
|---|
| 166 | if (item != NULL) {
|
|---|
| 167 | transfer_status_t *status =
|
|---|
| 168 | hash_table_get_instance(item, transfer_status_t, link);
|
|---|
| 169 | assert(status);
|
|---|
| 170 | if (status->used) {
|
|---|
| 171 | ret = EINPROGRESS;
|
|---|
| 172 | }
|
|---|
| 173 | status->used = true;
|
|---|
| 174 | } else {
|
|---|
| 175 | ret = EINVAL;
|
|---|
| 176 | }
|
|---|
| 177 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 178 | return ret;
|
|---|
| 179 | }
|
|---|
| 180 | /*----------------------------------------------------------------------------*/
|
|---|
| 181 | int bandwidth_free(bandwidth_t *instance, usb_address_t address,
|
|---|
| 182 | usb_endpoint_t endpoint, usb_direction_t direction)
|
|---|
| 183 | {
|
|---|
| 184 | assert(instance);
|
|---|
| 185 | transfer_t trans = {
|
|---|
| 186 | .address = address,
|
|---|
| 187 | .endpoint = endpoint,
|
|---|
| 188 | .direction = direction,
|
|---|
| 189 | };
|
|---|
| 190 | fibril_mutex_lock(&instance->guard);
|
|---|
| 191 | link_t *item =
|
|---|
| 192 | hash_table_find(&instance->reserved, (unsigned long*)&trans);
|
|---|
| 193 | int ret = EOK;
|
|---|
| 194 | if (item != NULL) {
|
|---|
| 195 | transfer_status_t *status =
|
|---|
| 196 | hash_table_get_instance(item, transfer_status_t, link);
|
|---|
| 197 | assert(status);
|
|---|
| 198 | if (!status->used) {
|
|---|
| 199 | ret = ENOENT;
|
|---|
| 200 | }
|
|---|
| 201 | status->used = false;
|
|---|
| 202 | } else {
|
|---|
| 203 | ret = EINVAL;
|
|---|
| 204 | }
|
|---|
| 205 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 206 | return ret;
|
|---|
| 207 | }
|
|---|