source: mainline/uspace/lib/usbhost/src/usb_endpoint_manager.c@ 8b54fe6

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

libusbhost: Don't include direction in endpoint hash, make BIDIRECTIONAL match both directions.

This servers dual purpose:
1) Prevents registering IN and OUT endpoints if there already is a BIDIRECTIONAL endpoint with the same address and number
2) BIDIRECTIONAL endpoints are found when searching for any direction, thus control transfers don't need special handling in batch preparation.

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