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

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

libusbhost: Remove speed argument from add_ep.

It can be queried internally.

  • Property mode set to 100644
File size: 15.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/** @addtogroup libusbhost
29 * @{
30 */
31/** @file
32 * HC Endpoint management.
33 */
34
35#include <stdbool.h>
36#include <assert.h>
37#include <errno.h>
38#include <macros.h>
39
40#include <usb/debug.h>
41#include <usb/host/usb_endpoint_manager.h>
42
43/** Endpoint compare helper function.
44 *
45 * USB_DIRECTION_BOTH matches both IN and OUT.
46 * @param ep Endpoint to compare, non-null.
47 * @param address Tested address.
48 * @param endpoint Tested endpoint number.
49 * @param direction Tested direction.
50 * @return True if ep can be used to communicate with given device,
51 * false otherwise.
52 */
53static inline bool ep_match(const endpoint_t *ep,
54 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
55{
56 assert(ep);
57 return
58 ((direction == ep->direction)
59 || (ep->direction == USB_DIRECTION_BOTH)
60 || (direction == USB_DIRECTION_BOTH))
61 && (endpoint == ep->endpoint)
62 && (address == ep->address);
63}
64
65/** Get list that holds endpoints for given address.
66 * @param instance usb_endpoint_manager structure, non-null.
67 * @param addr USB address, must be >= 0.
68 * @return Pointer to the appropriate list.
69 */
70static list_t * get_list(usb_endpoint_manager_t *instance, usb_address_t addr)
71{
72 assert(instance);
73 assert(addr >= 0);
74 return &instance->devices[addr % ARRAY_SIZE(instance->devices)].endpoint_list;
75}
76
77/** Internal search function, works on locked structure.
78 * @param instance usb_endpoint_manager structure, non-null.
79 * @param address USB address, must be valid.
80 * @param endpoint USB endpoint number.
81 * @param direction Communication direction.
82 * @return Pointer to endpoint_t structure representing given communication
83 * target, NULL if there is no such endpoint registered.
84 * @note Assumes that the internal mutex is locked.
85 */
86static endpoint_t * find_locked(usb_endpoint_manager_t *instance,
87 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
88{
89 assert(instance);
90 assert(fibril_mutex_is_locked(&instance->guard));
91 if (address < 0)
92 return NULL;
93 list_foreach(*get_list(instance, address), iterator) {
94 endpoint_t *ep = endpoint_get_instance(iterator);
95 if (ep_match(ep, address, endpoint, direction))
96 return ep;
97 }
98 return NULL;
99}
100
101/** Get a free USB address
102 *
103 * @param[in] instance Device manager structure to use.
104 * @return Free address, or error code.
105 */
106static usb_address_t usb_endpoint_manager_get_free_address(
107 usb_endpoint_manager_t *instance)
108{
109
110 usb_address_t new_address = instance->last_address;
111 do {
112 new_address = (new_address + 1) % USB_ADDRESS_COUNT;
113 if (new_address == USB_ADDRESS_DEFAULT)
114 new_address = 1;
115 if (new_address == instance->last_address)
116 return ENOSPC;
117 } while (instance->devices[new_address].occupied);
118
119 assert(new_address != USB_ADDRESS_DEFAULT);
120 instance->last_address = new_address;
121
122 return new_address;
123}
124
125/** Calculate bandwidth that needs to be reserved for communication with EP.
126 * Calculation follows USB 1.1 specification.
127 * @param speed Device's speed.
128 * @param type Type of the transfer.
129 * @param size Number of byte to transfer.
130 * @param max_packet_size Maximum bytes in one packet.
131 */
132size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
133 size_t size, size_t max_packet_size)
134{
135 /* We care about bandwidth only for interrupt and isochronous. */
136 if ((type != USB_TRANSFER_INTERRUPT)
137 && (type != USB_TRANSFER_ISOCHRONOUS)) {
138 return 0;
139 }
140
141 const unsigned packet_count =
142 (size + max_packet_size - 1) / max_packet_size;
143 /* TODO: It may be that ISO and INT transfers use only one packet per
144 * transaction, but I did not find text in USB spec to confirm this */
145 /* NOTE: All data packets will be considered to be max_packet_size */
146 switch (speed)
147 {
148 case USB_SPEED_LOW:
149 assert(type == USB_TRANSFER_INTERRUPT);
150 /* Protocol overhead 13B
151 * (3 SYNC bytes, 3 PID bytes, 2 Endpoint + CRC bytes, 2
152 * CRC bytes, and a 3-byte interpacket delay)
153 * see USB spec page 45-46. */
154 /* Speed penalty 8: low speed is 8-times slower*/
155 return packet_count * (13 + max_packet_size) * 8;
156 case USB_SPEED_FULL:
157 /* Interrupt transfer overhead see above
158 * or page 45 of USB spec */
159 if (type == USB_TRANSFER_INTERRUPT)
160 return packet_count * (13 + max_packet_size);
161
162 assert(type == USB_TRANSFER_ISOCHRONOUS);
163 /* Protocol overhead 9B
164 * (2 SYNC bytes, 2 PID bytes, 2 Endpoint + CRC bytes, 2 CRC
165 * bytes, and a 1-byte interpacket delay)
166 * see USB spec page 42 */
167 return packet_count * (9 + max_packet_size);
168 default:
169 return 0;
170 }
171}
172
173/** Initialize to default state.
174 * You need to provide valid bw_count function if you plan to use
175 * add_endpoint/remove_endpoint pair.
176 *
177 * @param instance usb_endpoint_manager structure, non-null.
178 * @param available_bandwidth Size of the bandwidth pool.
179 * @param bw_count function to use to calculate endpoint bw requirements.
180 * @return Error code.
181 */
182int usb_endpoint_manager_init(usb_endpoint_manager_t *instance,
183 size_t available_bandwidth, bw_count_func_t bw_count, usb_speed_t max_speed)
184{
185 assert(instance);
186 fibril_mutex_initialize(&instance->guard);
187 instance->free_bw = available_bandwidth;
188 instance->bw_count = bw_count;
189 instance->last_address = 0;
190 instance->max_speed = max_speed;
191 for (unsigned i = 0; i < ARRAY_SIZE(instance->devices); ++i) {
192 list_initialize(&instance->devices[i].endpoint_list);
193 instance->devices[i].speed = USB_SPEED_MAX;
194 instance->devices[i].occupied = false;
195 }
196 return EOK;
197}
198
199/** Register endpoint structure.
200 * Checks for duplicates.
201 * @param instance usb_endpoint_manager, non-null.
202 * @param ep endpoint_t to register.
203 * @param data_size Size of data to transfer.
204 * @return Error code.
205 */
206int usb_endpoint_manager_register_ep(usb_endpoint_manager_t *instance,
207 endpoint_t *ep, size_t data_size)
208{
209 assert(instance);
210 if (ep == NULL || ep->address < 0)
211 return EINVAL;
212
213 fibril_mutex_lock(&instance->guard);
214 /* Check for available bandwidth */
215 if (ep->bandwidth > instance->free_bw) {
216 fibril_mutex_unlock(&instance->guard);
217 return ENOSPC;
218 }
219
220 /* Check for existence */
221 const endpoint_t *endpoint =
222 find_locked(instance, ep->address, ep->endpoint, ep->direction);
223 if (endpoint != NULL) {
224 fibril_mutex_unlock(&instance->guard);
225 return EEXISTS;
226 }
227 list_append(&ep->link, get_list(instance, ep->address));
228
229 instance->free_bw -= ep->bandwidth;
230 fibril_mutex_unlock(&instance->guard);
231 return EOK;
232}
233
234/** Unregister endpoint structure.
235 * Checks for duplicates.
236 * @param instance usb_endpoint_manager, non-null.
237 * @param ep endpoint_t to unregister.
238 * @return Error code.
239 */
240int usb_endpoint_manager_unregister_ep(
241 usb_endpoint_manager_t *instance, endpoint_t *ep)
242{
243 assert(instance);
244 if (ep == NULL || ep->address < 0)
245 return EINVAL;
246
247 fibril_mutex_lock(&instance->guard);
248 if (!list_member(&ep->link, get_list(instance, ep->address))) {
249 fibril_mutex_unlock(&instance->guard);
250 return ENOENT;
251 }
252 list_remove(&ep->link);
253 instance->free_bw += ep->bandwidth;
254 fibril_mutex_unlock(&instance->guard);
255 return EOK;
256}
257
258/** Find endpoint_t representing the given communication route.
259 * @param instance usb_endpoint_manager, non-null.
260 * @param address
261 */
262endpoint_t * usb_endpoint_manager_find_ep(usb_endpoint_manager_t *instance,
263 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
264{
265 assert(instance);
266
267 fibril_mutex_lock(&instance->guard);
268 endpoint_t *ep = find_locked(instance, address, endpoint, direction);
269 fibril_mutex_unlock(&instance->guard);
270 return ep;
271}
272
273/** Create and register new endpoint_t structure.
274 * @param instance usb_endpoint_manager structure, non-null.
275 * @param address USB address.
276 * @param endpoint USB endpoint number.
277 * @param direction Communication direction.
278 * @param type USB transfer type.
279 * @param speed USB Communication speed.
280 * @param max_packet_size Maximum size of data packets.
281 * @param data_size Expected communication size.
282 * @param callback function to call just after registering.
283 * @param arg Argument to pass to the callback function.
284 * @return Error code.
285 */
286int usb_endpoint_manager_add_ep(usb_endpoint_manager_t *instance,
287 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
288 usb_transfer_type_t type, size_t max_packet_size, size_t data_size,
289 ep_add_callback_t callback, void *arg)
290{
291 assert(instance);
292 if (instance->bw_count == NULL)
293 return ENOTSUP;
294 if (!usb_address_is_valid(address))
295 return EINVAL;
296
297
298 fibril_mutex_lock(&instance->guard);
299 /* Check for speed and address */
300 if (!instance->devices[address].occupied) {
301 fibril_mutex_unlock(&instance->guard);
302 return ENOENT;
303 }
304
305 /* Check for existence */
306 endpoint_t *ep = find_locked(instance, address, endpoint, direction);
307 if (ep != NULL) {
308 fibril_mutex_unlock(&instance->guard);
309 return EEXISTS;
310 }
311
312 const usb_speed_t speed = instance->devices[address].speed;
313 const size_t bw =
314 instance->bw_count(speed, type, data_size, max_packet_size);
315
316 /* Check for available bandwidth */
317 if (bw > instance->free_bw) {
318 fibril_mutex_unlock(&instance->guard);
319 return ENOSPC;
320 }
321
322 ep = endpoint_create(
323 address, endpoint, direction, type, speed, max_packet_size, bw);
324 if (!ep) {
325 fibril_mutex_unlock(&instance->guard);
326 return ENOMEM;
327 }
328
329 if (callback) {
330 const int ret = callback(ep, arg);
331 if (ret != EOK) {
332 fibril_mutex_unlock(&instance->guard);
333 endpoint_destroy(ep);
334 return ret;
335 }
336 }
337 list_append(&ep->link, get_list(instance, ep->address));
338
339 instance->free_bw -= ep->bandwidth;
340 fibril_mutex_unlock(&instance->guard);
341 return EOK;
342}
343
344/** Unregister and destroy endpoint_t structure representing given route.
345 * @param instance usb_endpoint_manager structure, non-null.
346 * @param address USB address.
347 * @param endpoint USB endpoint number.
348 * @param direction Communication direction.
349 * @param callback Function to call after unregister, before destruction.
350 * @arg Argument to pass to the callback function.
351 * @return Error code.
352 */
353int usb_endpoint_manager_remove_ep(usb_endpoint_manager_t *instance,
354 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
355 ep_remove_callback_t callback, void *arg)
356{
357 assert(instance);
358 fibril_mutex_lock(&instance->guard);
359 endpoint_t *ep = find_locked(instance, address, endpoint, direction);
360 if (ep != NULL) {
361 list_remove(&ep->link);
362 instance->free_bw += ep->bandwidth;
363 }
364 fibril_mutex_unlock(&instance->guard);
365 if (ep == NULL)
366 return ENOENT;
367
368 if (callback) {
369 callback(ep, arg);
370 }
371 endpoint_destroy(ep);
372 return EOK;
373}
374
375int usb_endpoint_manager_reset_toggle(usb_endpoint_manager_t *instance,
376 usb_target_t target, bool all)
377{
378 assert(instance);
379 if (!usb_target_is_valid(target))
380 return EINVAL;
381
382 int ret = ENOENT;
383
384 fibril_mutex_lock(&instance->guard);
385 list_foreach(*get_list(instance, target.address), it) {
386 endpoint_t *ep = endpoint_get_instance(it);
387 if ((ep->address == target.address)
388 && (all || ep->endpoint == target.endpoint)) {
389 endpoint_toggle_set(ep, 0);
390 ret = EOK;
391 }
392 }
393 fibril_mutex_unlock(&instance->guard);
394 return ret;
395}
396
397/** Unregister and destroy all endpoints using given address.
398 * @param instance usb_endpoint_manager structure, non-null.
399 * @param address USB address.
400 * @param endpoint USB endpoint number.
401 * @param direction Communication direction.
402 * @param callback Function to call after unregister, before destruction.
403 * @arg Argument to pass to the callback function.
404 * @return Error code.
405 */
406int usb_endpoint_manager_remove_address(usb_endpoint_manager_t *instance,
407 usb_address_t address, ep_remove_callback_t callback, void *arg)
408{
409 assert(instance);
410 if (!usb_address_is_valid(address))
411 return EINVAL;
412
413 fibril_mutex_lock(&instance->guard);
414
415 const int ret = instance->devices[address].occupied ? EOK : ENOENT;
416 instance->devices[address].occupied = false;
417
418 list_foreach(*get_list(instance, address), iterator) {
419 endpoint_t *ep = endpoint_get_instance(iterator);
420 if (ep->address == address) {
421 iterator = iterator->next;
422 list_remove(&ep->link);
423 if (callback)
424 callback(ep, arg);
425 endpoint_destroy(ep);
426 }
427 }
428 fibril_mutex_unlock(&instance->guard);
429 return ret;
430}
431
432/** Request USB address.
433 * @param instance usb_device_manager
434 * @param address Pointer to requested address value, place to store new address
435 * @parma strict Fail if the requested address is not available.
436 * @return Error code.
437 * @note Default address is only available in strict mode.
438 */
439int usb_endpoint_manager_request_address(usb_endpoint_manager_t *instance,
440 usb_address_t *address, bool strict, usb_speed_t speed)
441{
442 assert(instance);
443 assert(address);
444 if (speed > instance->max_speed)
445 return ENOTSUP;
446
447 if (!usb_address_is_valid(*address))
448 return EINVAL;
449
450 usb_address_t addr = *address;
451
452 fibril_mutex_lock(&instance->guard);
453 /* Only grant default address to strict requests */
454 if ((addr == USB_ADDRESS_DEFAULT) && !strict) {
455 addr = usb_endpoint_manager_get_free_address(instance);
456 }
457
458 if (instance->devices[addr].occupied) {
459 if (strict) {
460 fibril_mutex_unlock(&instance->guard);
461 return ENOENT;
462 }
463 addr = usb_endpoint_manager_get_free_address(instance);
464 }
465 if (usb_address_is_valid(addr)) {
466 assert(instance->devices[addr].occupied == false);
467 assert(addr != USB_ADDRESS_DEFAULT || strict);
468
469 instance->devices[addr].occupied = true;
470 instance->devices[addr].speed = speed;
471 *address = addr;
472 addr = 0;
473 }
474
475 fibril_mutex_unlock(&instance->guard);
476 return addr;
477}
478
479/** Get speed assigned to USB address.
480 *
481 * @param[in] instance Device manager structure to use.
482 * @param[in] address Address the caller wants to find.
483 * @param[out] speed Assigned speed.
484 * @return Error code.
485 */
486int usb_endpoint_manager_get_info_by_address(usb_endpoint_manager_t *instance,
487 usb_address_t address, usb_speed_t *speed)
488{
489 assert(instance);
490 if (!usb_address_is_valid(address)) {
491 return EINVAL;
492 }
493
494 fibril_mutex_lock(&instance->guard);
495
496 const int ret = instance->devices[address].occupied ? EOK : ENOENT;
497 if (speed && instance->devices[address].occupied) {
498 *speed = instance->devices[address].speed;
499 }
500
501 fibril_mutex_unlock(&instance->guard);
502 return ret;
503}
504/**
505 * @}
506 */
Note: See TracBrowser for help on using the repository browser.