source: mainline/uspace/lib/usb/src/host/usb_endpoint_manager.c@ 92d6868

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 92d6868 was 92d6868, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

Use sane key management in usb_endpoint_manager

  • Property mode set to 100644
File size: 7.2 KB
Line 
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)
38typedef struct {
39 link_t link;
40 size_t bw;
41 endpoint_t *ep;
42} node_t;
43/*----------------------------------------------------------------------------*/
44static 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/*----------------------------------------------------------------------------*/
55static 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/*----------------------------------------------------------------------------*/
76static 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/*----------------------------------------------------------------------------*/
84static hash_table_operations_t op = {
85 .hash = node_hash,
86 .compare = node_compare,
87 .remove_callback = node_remove,
88};
89/*----------------------------------------------------------------------------*/
90size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
91 size_t size, size_t max_packet_size)
92{
93 const unsigned packet_count =
94 (size + max_packet_size - 1) / max_packet_size;
95 /* TODO: It may be that ISO and INT transfers use only one data packet
96 * per transaction, but I did not find text in UB spec that confirms
97 * this */
98 /* NOTE: All data packets will be considered to be max_packet_size */
99 switch (speed)
100 {
101 case USB_SPEED_LOW:
102 assert(type == USB_TRANSFER_INTERRUPT);
103 /* Protocol overhead 13B
104 * (3 SYNC bytes, 3 PID bytes, 2 Endpoint + CRC bytes, 2
105 * CRC bytes, and a 3-byte interpacket delay)
106 * see USB spec page 45-46. */
107 /* Speed penalty 8: low speed is 8-times slower*/
108 return packet_count * (13 + max_packet_size) * 8;
109 case USB_SPEED_FULL:
110 /* Interrupt transfer overhead see above
111 * or page 45 of USB spec */
112 if (type == USB_TRANSFER_INTERRUPT)
113 return packet_count * (13 + max_packet_size);
114
115 assert(type == USB_TRANSFER_ISOCHRONOUS);
116 /* Protocol overhead 9B
117 * (2 SYNC bytes, 2 PID bytes, 2 Endpoint + CRC bytes, 2 CRC
118 * bytes, and a 1-byte interpacket delay)
119 * see USB spec page 42 */
120 return packet_count * (9 + max_packet_size);
121 default:
122 return 0;
123 }
124}
125/*----------------------------------------------------------------------------*/
126int usb_endpoint_manager_init(usb_endpoint_manager_t *instance,
127 size_t available_bandwidth)
128{
129 assert(instance);
130 fibril_mutex_initialize(&instance->guard);
131 fibril_condvar_initialize(&instance->change);
132 instance->free_bw = available_bandwidth;
133 bool ht =
134 hash_table_create(&instance->ep_table, BUCKET_COUNT, MAX_KEYS, &op);
135 return ht ? EOK : ENOMEM;
136}
137/*----------------------------------------------------------------------------*/
138void usb_endpoint_manager_destroy(usb_endpoint_manager_t *instance)
139{
140 hash_table_destroy(&instance->ep_table);
141}
142/*----------------------------------------------------------------------------*/
143int usb_endpoint_manager_register_ep(usb_endpoint_manager_t *instance,
144 endpoint_t *ep, size_t data_size)
145{
146 assert(ep);
147 size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type,
148 data_size, ep->max_packet_size);
149 assert(instance);
150
151 unsigned long key[MAX_KEYS] =
152 {ep->address, ep->endpoint, ep->direction};
153 fibril_mutex_lock(&instance->guard);
154
155 link_t *item =
156 hash_table_find(&instance->ep_table, key);
157 if (item != NULL) {
158 fibril_mutex_unlock(&instance->guard);
159 return EEXISTS;
160 }
161
162 if (bw > instance->free_bw) {
163 fibril_mutex_unlock(&instance->guard);
164 return ENOSPC;
165 }
166
167 node_t *node = malloc(sizeof(node_t));
168 if (node == NULL) {
169 fibril_mutex_unlock(&instance->guard);
170 return ENOMEM;
171 }
172
173 node->bw = bw;
174 node->ep = ep;
175 link_initialize(&node->link);
176
177 hash_table_insert(&instance->ep_table, key, &node->link);
178 instance->free_bw -= bw;
179 fibril_mutex_unlock(&instance->guard);
180 fibril_condvar_broadcast(&instance->change);
181 return EOK;
182}
183/*----------------------------------------------------------------------------*/
184int usb_endpoint_manager_unregister_ep(usb_endpoint_manager_t *instance,
185 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
186{
187 assert(instance);
188 unsigned long key[MAX_KEYS] = {address, endpoint, direction};
189
190 fibril_mutex_lock(&instance->guard);
191 link_t *item = hash_table_find(&instance->ep_table, key);
192 if (item == NULL) {
193 fibril_mutex_unlock(&instance->guard);
194 return EINVAL;
195 }
196
197 node_t *node = hash_table_get_instance(item, node_t, link);
198 instance->free_bw += node->bw;
199 hash_table_remove(&instance->ep_table, key, MAX_KEYS);
200
201 fibril_mutex_unlock(&instance->guard);
202 fibril_condvar_broadcast(&instance->change);
203 return EOK;
204}
205/*----------------------------------------------------------------------------*/
206endpoint_t * usb_endpoint_manager_get_ep(usb_endpoint_manager_t *instance,
207 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
208 size_t *bw)
209{
210 assert(instance);
211 unsigned long key[MAX_KEYS] = {address, endpoint, direction};
212
213 fibril_mutex_lock(&instance->guard);
214 link_t *item = hash_table_find(&instance->ep_table, key);
215 if (item == NULL) {
216 fibril_mutex_unlock(&instance->guard);
217 return NULL;
218 }
219 node_t *node = hash_table_get_instance(item, node_t, link);
220 if (bw)
221 *bw = node->bw;
222
223 fibril_mutex_unlock(&instance->guard);
224 return node->ep;
225}
Note: See TracBrowser for help on using the repository browser.