usb_endpoint_manager.c

00001 /*
00002  * Copyright (c) 2011 Jan Vesely
00003  * All rights eps.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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         /* We care about bandwidth only for interrupt and isochronous. */
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         /* TODO: It may be that ISO and INT transfers use only one data packet
00111          * per transaction, but I did not find text in UB spec that confirms
00112          * this */
00113         /* NOTE: All data packets will be considered to be max_packet_size */
00114         switch (speed)
00115         {
00116         case USB_SPEED_LOW:
00117                 assert(type == USB_TRANSFER_INTERRUPT);
00118                 /* Protocol overhead 13B
00119                  * (3 SYNC bytes, 3 PID bytes, 2 Endpoint + CRC bytes, 2
00120                  * CRC bytes, and a 3-byte interpacket delay)
00121                  * see USB spec page 45-46. */
00122                 /* Speed penalty 8: low speed is 8-times slower*/
00123                 return packet_count * (13 + max_packet_size) * 8;
00124         case USB_SPEED_FULL:
00125                 /* Interrupt transfer overhead see above
00126                  * or page 45 of USB spec */
00127                 if (type == USB_TRANSFER_INTERRUPT)
00128                         return packet_count * (13 + max_packet_size);
00129 
00130                 assert(type == USB_TRANSFER_ISOCHRONOUS);
00131                 /* Protocol overhead 9B
00132                  * (2 SYNC bytes, 2 PID bytes, 2 Endpoint + CRC bytes, 2 CRC
00133                  * bytes, and a 1-byte interpacket delay)
00134                  * see USB spec page 42 */
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: /*clear feature*/
00266                 /* recipient is endpoint, value is zero (ENDPOINT_STALL) */
00267                 if (((data[0] & 0xf) == 1) && ((data[2] | data[3]) == 0)) {
00268                         /* endpoint number is < 16, thus first byte is enough */
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: /* set configuration */
00279         case 0x11: /* set interface */
00280                 /* target must be device */
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 }

Generated on Thu Jun 2 07:45:49 2011 for HelenOS/USB by  doxygen 1.4.7