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

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

Don't keep endpoints in two separate structures

Fixes crash "[HelenOS-USB-devel] uhci root hub" email
Move toggle resert testing ugliness to endpoint manager

  • Property mode set to 100644
File size: 9.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/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 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 size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type,
163 data_size, ep->max_packet_size);
164 assert(instance);
165
166 unsigned long key[MAX_KEYS] =
167 {ep->address, ep->endpoint, ep->direction};
168 fibril_mutex_lock(&instance->guard);
169
170 link_t *item =
171 hash_table_find(&instance->ep_table, key);
172 if (item != NULL) {
173 fibril_mutex_unlock(&instance->guard);
174 return EEXISTS;
175 }
176
177 if (bw > instance->free_bw) {
178 fibril_mutex_unlock(&instance->guard);
179 return ENOSPC;
180 }
181
182 node_t *node = malloc(sizeof(node_t));
183 if (node == NULL) {
184 fibril_mutex_unlock(&instance->guard);
185 return ENOMEM;
186 }
187
188 node->bw = bw;
189 node->ep = ep;
190 link_initialize(&node->link);
191
192 hash_table_insert(&instance->ep_table, key, &node->link);
193 instance->free_bw -= bw;
194 fibril_mutex_unlock(&instance->guard);
195 fibril_condvar_broadcast(&instance->change);
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 instance->free_bw += node->bw;
214 hash_table_remove(&instance->ep_table, key, MAX_KEYS);
215
216 fibril_mutex_unlock(&instance->guard);
217 fibril_condvar_broadcast(&instance->change);
218 return EOK;
219}
220/*----------------------------------------------------------------------------*/
221endpoint_t * usb_endpoint_manager_get_ep(usb_endpoint_manager_t *instance,
222 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
223 size_t *bw)
224{
225 assert(instance);
226 unsigned long key[MAX_KEYS] = {address, endpoint, direction};
227
228 fibril_mutex_lock(&instance->guard);
229 link_t *item = hash_table_find(&instance->ep_table, key);
230 if (item == NULL) {
231 fibril_mutex_unlock(&instance->guard);
232 return NULL;
233 }
234 node_t *node = hash_table_get_instance(item, node_t, link);
235 if (bw)
236 *bw = node->bw;
237
238 fibril_mutex_unlock(&instance->guard);
239 return node->ep;
240}
241/*----------------------------------------------------------------------------*/
242/** Check setup packet data for signs of toggle reset.
243 *
244 * @param[in] instance Device keeper structure to use.
245 * @param[in] target Device to receive setup packet.
246 * @param[in] data Setup packet data.
247 *
248 * Really ugly one.
249 */
250void usb_endpoint_manager_reset_if_need(
251 usb_endpoint_manager_t *instance, usb_target_t target, const uint8_t *data)
252{
253 assert(instance);
254 if (target.endpoint > 15 || target.endpoint < 0
255 || target.address >= USB11_ADDRESS_MAX || target.address < 0) {
256 usb_log_error("Invalid data when checking for toggle reset.\n");
257 return;
258 }
259
260 switch (data[1])
261 {
262 case 0x01: /*clear feature*/
263 /* recipient is endpoint, value is zero (ENDPOINT_STALL) */
264 if (((data[0] & 0xf) == 1) && ((data[2] | data[3]) == 0)) {
265 /* endpoint number is < 16, thus first byte is enough */
266 usb_target_t reset_target =
267 { .address = target.address, data[4] };
268 fibril_mutex_lock(&instance->guard);
269 hash_table_apply(&instance->ep_table,
270 node_toggle_reset_filtered, &reset_target);
271 fibril_mutex_unlock(&instance->guard);
272 }
273 break;
274
275 case 0x9: /* set configuration */
276 case 0x11: /* set interface */
277 /* target must be device */
278 if ((data[0] & 0xf) == 0) {
279 usb_target_t reset_target =
280 { .address = target.address, 0 };
281 fibril_mutex_lock(&instance->guard);
282 hash_table_apply(&instance->ep_table,
283 node_toggle_reset_filtered, &reset_target);
284 fibril_mutex_unlock(&instance->guard);
285 }
286 break;
287 }
288}
Note: See TracBrowser for help on using the repository browser.