source: mainline/uspace/lib/usbhost/src/usb_endpoint_manager.c@ 83c3123

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

libusbhost: Store bandwidth in endpoint structure.

Remove redundant node_t tructure and use endpoint_t directly.

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