source: mainline/uspace/lib/usbhost/src/usb_endpoint_manager.c@ 563d9d0a

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

libusbhost: minor cleanup and fixes

  • Property mode set to 100644
File size: 9.3 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/debug.h>
34#include <usb/host/usb_endpoint_manager.h>
35
36#define BUCKET_COUNT 7
37
38#define MAX_KEYS (3)
39typedef struct {
40 link_t link;
41 size_t bw;
42 endpoint_t *ep;
43} node_t;
44/*----------------------------------------------------------------------------*/
45static hash_index_t node_hash(unsigned long key[])
46{
47 hash_index_t hash = 0;
48 unsigned i = 0;
49 for (;i < MAX_KEYS; ++i) {
50 hash ^= key[i];
51 }
52 hash %= BUCKET_COUNT;
53 return hash;
54}
55/*----------------------------------------------------------------------------*/
56static int node_compare(unsigned long key[], hash_count_t keys, link_t *item)
57{
58 assert(item);
59 node_t *node = hash_table_get_instance(item, node_t, link);
60 assert(node);
61 assert(node->ep);
62 bool match = true;
63 switch (keys) {
64 case 3:
65 match = match && (key[2] == node->ep->direction);
66 case 2:
67 match = match && (key[1] == (unsigned long)node->ep->endpoint);
68 case 1:
69 match = match && (key[0] == (unsigned long)node->ep->address);
70 break;
71 default:
72 match = false;
73 }
74 return match;
75}
76/*----------------------------------------------------------------------------*/
77static void node_remove(link_t *item)
78{
79 assert(item);
80 node_t *node = hash_table_get_instance(item, node_t, link);
81 endpoint_destroy(node->ep);
82 free(node);
83}
84/*----------------------------------------------------------------------------*/
85static void node_toggle_reset_filtered(link_t *item, void *arg)
86{
87 assert(item);
88 node_t *node = hash_table_get_instance(item, node_t, link);
89 usb_target_t *target = arg;
90 endpoint_toggle_reset_filtered(node->ep, *target);
91}
92/*----------------------------------------------------------------------------*/
93static hash_table_operations_t op = {
94 .hash = node_hash,
95 .compare = node_compare,
96 .remove_callback = node_remove,
97};
98/*----------------------------------------------------------------------------*/
99size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
100 size_t size, size_t max_packet_size)
101{
102 /* We care about bandwidth only for interrupt and isochronous. */
103 if ((type != USB_TRANSFER_INTERRUPT)
104 && (type != USB_TRANSFER_ISOCHRONOUS)) {
105 return 0;
106 }
107
108 const unsigned packet_count =
109 (size + max_packet_size - 1) / max_packet_size;
110 /* TODO: It may be that ISO and INT transfers use only one data packet
111 * per transaction, but I did not find text in UB spec that confirms
112 * this */
113 /* NOTE: All data packets will be considered to be max_packet_size */
114 switch (speed)
115 {
116 case USB_SPEED_LOW:
117 assert(type == USB_TRANSFER_INTERRUPT);
118 /* Protocol overhead 13B
119 * (3 SYNC bytes, 3 PID bytes, 2 Endpoint + CRC bytes, 2
120 * CRC bytes, and a 3-byte interpacket delay)
121 * see USB spec page 45-46. */
122 /* Speed penalty 8: low speed is 8-times slower*/
123 return packet_count * (13 + max_packet_size) * 8;
124 case USB_SPEED_FULL:
125 /* Interrupt transfer overhead see above
126 * or page 45 of USB spec */
127 if (type == USB_TRANSFER_INTERRUPT)
128 return packet_count * (13 + max_packet_size);
129
130 assert(type == USB_TRANSFER_ISOCHRONOUS);
131 /* Protocol overhead 9B
132 * (2 SYNC bytes, 2 PID bytes, 2 Endpoint + CRC bytes, 2 CRC
133 * bytes, and a 1-byte interpacket delay)
134 * see USB spec page 42 */
135 return packet_count * (9 + max_packet_size);
136 default:
137 return 0;
138 }
139}
140/*----------------------------------------------------------------------------*/
141int usb_endpoint_manager_init(usb_endpoint_manager_t *instance,
142 size_t available_bandwidth)
143{
144 assert(instance);
145 fibril_mutex_initialize(&instance->guard);
146 fibril_condvar_initialize(&instance->change);
147 instance->free_bw = available_bandwidth;
148 const bool ht =
149 hash_table_create(&instance->ep_table, BUCKET_COUNT, MAX_KEYS, &op);
150 return ht ? EOK : ENOMEM;
151}
152/*----------------------------------------------------------------------------*/
153void usb_endpoint_manager_destroy(usb_endpoint_manager_t *instance)
154{
155 hash_table_destroy(&instance->ep_table);
156}
157/*----------------------------------------------------------------------------*/
158int usb_endpoint_manager_register_ep(usb_endpoint_manager_t *instance,
159 endpoint_t *ep, size_t data_size)
160{
161 assert(ep);
162 const size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type,
163 data_size, ep->max_packet_size);
164 assert(instance);
165
166 fibril_mutex_lock(&instance->guard);
167
168 if (bw > instance->free_bw) {
169 fibril_mutex_unlock(&instance->guard);
170 return ENOSPC;
171 }
172
173 unsigned long key[MAX_KEYS] =
174 {ep->address, ep->endpoint, ep->direction};
175
176 const link_t *item =
177 hash_table_find(&instance->ep_table, key);
178 if (item != NULL) {
179 fibril_mutex_unlock(&instance->guard);
180 return EEXISTS;
181 }
182
183 node_t *node = malloc(sizeof(node_t));
184 if (node == NULL) {
185 fibril_mutex_unlock(&instance->guard);
186 return ENOMEM;
187 }
188
189 node->bw = bw;
190 node->ep = ep;
191 link_initialize(&node->link);
192
193 hash_table_insert(&instance->ep_table, key, &node->link);
194 instance->free_bw -= bw;
195 fibril_mutex_unlock(&instance->guard);
196 fibril_condvar_broadcast(&instance->change);
197 return EOK;
198}
199/*----------------------------------------------------------------------------*/
200int usb_endpoint_manager_unregister_ep(usb_endpoint_manager_t *instance,
201 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
202{
203 assert(instance);
204 unsigned long key[MAX_KEYS] = {address, endpoint, direction};
205
206 fibril_mutex_lock(&instance->guard);
207 link_t *item = hash_table_find(&instance->ep_table, key);
208 if (item == NULL) {
209 fibril_mutex_unlock(&instance->guard);
210 return EINVAL;
211 }
212
213 node_t *node = hash_table_get_instance(item, node_t, link);
214 if (node->ep->active) {
215 fibril_mutex_unlock(&instance->guard);
216 return EBUSY;
217 }
218
219 instance->free_bw += node->bw;
220 hash_table_remove(&instance->ep_table, key, MAX_KEYS);
221
222 fibril_mutex_unlock(&instance->guard);
223 fibril_condvar_broadcast(&instance->change);
224 return EOK;
225}
226/*----------------------------------------------------------------------------*/
227endpoint_t * usb_endpoint_manager_get_ep(usb_endpoint_manager_t *instance,
228 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
229 size_t *bw)
230{
231 assert(instance);
232 unsigned long key[MAX_KEYS] = {address, endpoint, direction};
233
234 fibril_mutex_lock(&instance->guard);
235 const link_t *item = hash_table_find(&instance->ep_table, key);
236 if (item == NULL) {
237 fibril_mutex_unlock(&instance->guard);
238 return NULL;
239 }
240 const node_t *node = hash_table_get_instance(item, node_t, link);
241 if (bw)
242 *bw = node->bw;
243
244 fibril_mutex_unlock(&instance->guard);
245 return node->ep;
246}
247/*----------------------------------------------------------------------------*/
248/** Check setup packet data for signs of toggle reset.
249 *
250 * @param[in] instance Device keeper structure to use.
251 * @param[in] target Device to receive setup packet.
252 * @param[in] data Setup packet data.
253 *
254 * Really ugly one.
255 */
256void usb_endpoint_manager_reset_if_need(
257 usb_endpoint_manager_t *instance, usb_target_t target, const uint8_t *data)
258{
259 assert(instance);
260 if (!usb_target_is_valid(target)) {
261 usb_log_error("Invalid data when checking for toggle reset.\n");
262 return;
263 }
264
265 assert(data);
266 switch (data[1])
267 {
268 case 0x01: /* Clear Feature -- resets only cleared ep */
269 /* Recipient is endpoint, value is zero (ENDPOINT_STALL) */
270 if (((data[0] & 0xf) == 1) && ((data[2] | data[3]) == 0)) {
271 /* endpoint number is < 16, thus first byte is enough */
272 usb_target_t reset_target =
273 { .address = target.address, data[4] };
274 fibril_mutex_lock(&instance->guard);
275 hash_table_apply(&instance->ep_table,
276 node_toggle_reset_filtered, &reset_target);
277 fibril_mutex_unlock(&instance->guard);
278 }
279 break;
280
281 case 0x9: /* Set Configuration */
282 case 0x11: /* Set Interface */
283 /* Recipient must be device */
284 if ((data[0] & 0xf) == 0) {
285 usb_target_t reset_target =
286 { .address = target.address, 0 };
287 fibril_mutex_lock(&instance->guard);
288 hash_table_apply(&instance->ep_table,
289 node_toggle_reset_filtered, &reset_target);
290 fibril_mutex_unlock(&instance->guard);
291 }
292 break;
293 }
294}
Note: See TracBrowser for help on using the repository browser.