00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <bool.h>
00030 #include <assert.h>
00031 #include <errno.h>
00032
00033 #include <usb/debug.h>
00034 #include <usb/host/usb_endpoint_manager.h>
00035
00036 #define BUCKET_COUNT 7
00037
00038 #define MAX_KEYS (3)
00039 typedef struct {
00040 link_t link;
00041 size_t bw;
00042 endpoint_t *ep;
00043 } node_t;
00044
00045 static hash_index_t node_hash(unsigned long key[])
00046 {
00047 hash_index_t hash = 0;
00048 unsigned i = 0;
00049 for (;i < MAX_KEYS; ++i) {
00050 hash ^= key[i];
00051 }
00052 hash %= BUCKET_COUNT;
00053 return hash;
00054 }
00055
00056 static int node_compare(unsigned long key[], hash_count_t keys, link_t *item)
00057 {
00058 assert(item);
00059 node_t *node = hash_table_get_instance(item, node_t, link);
00060 assert(node);
00061 assert(node->ep);
00062 bool match = true;
00063 switch (keys) {
00064 case 3:
00065 match = match && (key[2] == node->ep->direction);
00066 case 2:
00067 match = match && (key[1] == (unsigned long)node->ep->endpoint);
00068 case 1:
00069 match = match && (key[0] == (unsigned long)node->ep->address);
00070 break;
00071 default:
00072 match = false;
00073 }
00074 return match;
00075 }
00076
00077 static void node_remove(link_t *item)
00078 {
00079 assert(item);
00080 node_t *node = hash_table_get_instance(item, node_t, link);
00081 endpoint_destroy(node->ep);
00082 free(node);
00083 }
00084
00085 static void node_toggle_reset_filtered(link_t *item, void *arg)
00086 {
00087 assert(item);
00088 node_t *node = hash_table_get_instance(item, node_t, link);
00089 usb_target_t *target = arg;
00090 endpoint_toggle_reset_filtered(node->ep, *target);
00091 }
00092
00093 static hash_table_operations_t op = {
00094 .hash = node_hash,
00095 .compare = node_compare,
00096 .remove_callback = node_remove,
00097 };
00098
00099 size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
00100 size_t size, size_t max_packet_size)
00101 {
00102
00103 if ((type != USB_TRANSFER_INTERRUPT)
00104 && (type != USB_TRANSFER_ISOCHRONOUS)) {
00105 return 0;
00106 }
00107
00108 const unsigned packet_count =
00109 (size + max_packet_size - 1) / max_packet_size;
00110
00111
00112
00113
00114 switch (speed)
00115 {
00116 case USB_SPEED_LOW:
00117 assert(type == USB_TRANSFER_INTERRUPT);
00118
00119
00120
00121
00122
00123 return packet_count * (13 + max_packet_size) * 8;
00124 case USB_SPEED_FULL:
00125
00126
00127 if (type == USB_TRANSFER_INTERRUPT)
00128 return packet_count * (13 + max_packet_size);
00129
00130 assert(type == USB_TRANSFER_ISOCHRONOUS);
00131
00132
00133
00134
00135 return packet_count * (9 + max_packet_size);
00136 default:
00137 return 0;
00138 }
00139 }
00140
00141 int usb_endpoint_manager_init(usb_endpoint_manager_t *instance,
00142 size_t available_bandwidth)
00143 {
00144 assert(instance);
00145 fibril_mutex_initialize(&instance->guard);
00146 fibril_condvar_initialize(&instance->change);
00147 instance->free_bw = available_bandwidth;
00148 bool ht =
00149 hash_table_create(&instance->ep_table, BUCKET_COUNT, MAX_KEYS, &op);
00150 return ht ? EOK : ENOMEM;
00151 }
00152
00153 void usb_endpoint_manager_destroy(usb_endpoint_manager_t *instance)
00154 {
00155 hash_table_destroy(&instance->ep_table);
00156 }
00157
00158 int usb_endpoint_manager_register_ep(usb_endpoint_manager_t *instance,
00159 endpoint_t *ep, size_t data_size)
00160 {
00161 assert(ep);
00162 size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type,
00163 data_size, ep->max_packet_size);
00164 assert(instance);
00165
00166 unsigned long key[MAX_KEYS] =
00167 {ep->address, ep->endpoint, ep->direction};
00168 fibril_mutex_lock(&instance->guard);
00169
00170 link_t *item =
00171 hash_table_find(&instance->ep_table, key);
00172 if (item != NULL) {
00173 fibril_mutex_unlock(&instance->guard);
00174 return EEXISTS;
00175 }
00176
00177 if (bw > instance->free_bw) {
00178 fibril_mutex_unlock(&instance->guard);
00179 return ENOSPC;
00180 }
00181
00182 node_t *node = malloc(sizeof(node_t));
00183 if (node == NULL) {
00184 fibril_mutex_unlock(&instance->guard);
00185 return ENOMEM;
00186 }
00187
00188 node->bw = bw;
00189 node->ep = ep;
00190 link_initialize(&node->link);
00191
00192 hash_table_insert(&instance->ep_table, key, &node->link);
00193 instance->free_bw -= bw;
00194 fibril_mutex_unlock(&instance->guard);
00195 fibril_condvar_broadcast(&instance->change);
00196 return EOK;
00197 }
00198
00199 int usb_endpoint_manager_unregister_ep(usb_endpoint_manager_t *instance,
00200 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
00201 {
00202 assert(instance);
00203 unsigned long key[MAX_KEYS] = {address, endpoint, direction};
00204
00205 fibril_mutex_lock(&instance->guard);
00206 link_t *item = hash_table_find(&instance->ep_table, key);
00207 if (item == NULL) {
00208 fibril_mutex_unlock(&instance->guard);
00209 return EINVAL;
00210 }
00211
00212 node_t *node = hash_table_get_instance(item, node_t, link);
00213 if (node->ep->active)
00214 return EBUSY;
00215
00216 instance->free_bw += node->bw;
00217 hash_table_remove(&instance->ep_table, key, MAX_KEYS);
00218
00219 fibril_mutex_unlock(&instance->guard);
00220 fibril_condvar_broadcast(&instance->change);
00221 return EOK;
00222 }
00223
00224 endpoint_t * usb_endpoint_manager_get_ep(usb_endpoint_manager_t *instance,
00225 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
00226 size_t *bw)
00227 {
00228 assert(instance);
00229 unsigned long key[MAX_KEYS] = {address, endpoint, direction};
00230
00231 fibril_mutex_lock(&instance->guard);
00232 link_t *item = hash_table_find(&instance->ep_table, key);
00233 if (item == NULL) {
00234 fibril_mutex_unlock(&instance->guard);
00235 return NULL;
00236 }
00237 node_t *node = hash_table_get_instance(item, node_t, link);
00238 if (bw)
00239 *bw = node->bw;
00240
00241 fibril_mutex_unlock(&instance->guard);
00242 return node->ep;
00243 }
00244
00253 void usb_endpoint_manager_reset_if_need(
00254 usb_endpoint_manager_t *instance, usb_target_t target, const uint8_t *data)
00255 {
00256 assert(instance);
00257 if (target.endpoint > 15 || target.endpoint < 0
00258 || target.address >= USB11_ADDRESS_MAX || target.address < 0) {
00259 usb_log_error("Invalid data when checking for toggle reset.\n");
00260 return;
00261 }
00262
00263 switch (data[1])
00264 {
00265 case 0x01:
00266
00267 if (((data[0] & 0xf) == 1) && ((data[2] | data[3]) == 0)) {
00268
00269 usb_target_t reset_target =
00270 { .address = target.address, data[4] };
00271 fibril_mutex_lock(&instance->guard);
00272 hash_table_apply(&instance->ep_table,
00273 node_toggle_reset_filtered, &reset_target);
00274 fibril_mutex_unlock(&instance->guard);
00275 }
00276 break;
00277
00278 case 0x9:
00279 case 0x11:
00280
00281 if ((data[0] & 0xf) == 0) {
00282 usb_target_t reset_target =
00283 { .address = target.address, 0 };
00284 fibril_mutex_lock(&instance->guard);
00285 hash_table_apply(&instance->ep_table,
00286 node_toggle_reset_filtered, &reset_target);
00287 fibril_mutex_unlock(&instance->guard);
00288 }
00289 break;
00290 }
00291 }