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

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

libusbhost: Add TT information to usb_enpoint_t structure.

  • 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, usb_address_t tt_address,
290 unsigned tt_port)
291{
292 assert(instance);
293 if (instance->bw_count == NULL)
294 return ENOTSUP;
295 if (!usb_address_is_valid(address))
296 return EINVAL;
297
298
299 fibril_mutex_lock(&instance->guard);
300 /* Check for speed and address */
301 if (!instance->devices[address].occupied) {
302 fibril_mutex_unlock(&instance->guard);
303 return ENOENT;
304 }
305
306 /* Check for existence */
307 endpoint_t *ep = find_locked(instance, address, endpoint, direction);
308 if (ep != NULL) {
309 fibril_mutex_unlock(&instance->guard);
310 return EEXISTS;
311 }
312
313 const usb_speed_t speed = instance->devices[address].speed;
314 const size_t bw =
315 instance->bw_count(speed, type, data_size, max_packet_size);
316
317 /* Check for available bandwidth */
318 if (bw > instance->free_bw) {
319 fibril_mutex_unlock(&instance->guard);
320 return ENOSPC;
321 }
322
323 ep = endpoint_create(address, endpoint, direction, type, speed,
324 max_packet_size, bw, tt_address, tt_port);
325 if (!ep) {
326 fibril_mutex_unlock(&instance->guard);
327 return ENOMEM;
328 }
329
330 if (callback) {
331 const int ret = callback(ep, arg);
332 if (ret != EOK) {
333 fibril_mutex_unlock(&instance->guard);
334 endpoint_destroy(ep);
335 return ret;
336 }
337 }
338 list_append(&ep->link, get_list(instance, ep->address));
339
340 instance->free_bw -= ep->bandwidth;
341 fibril_mutex_unlock(&instance->guard);
342 return EOK;
343}
344
345/** Unregister and destroy endpoint_t structure representing given route.
346 * @param instance usb_endpoint_manager structure, non-null.
347 * @param address USB address.
348 * @param endpoint USB endpoint number.
349 * @param direction Communication direction.
350 * @param callback Function to call after unregister, before destruction.
351 * @arg Argument to pass to the callback function.
352 * @return Error code.
353 */
354int usb_endpoint_manager_remove_ep(usb_endpoint_manager_t *instance,
355 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
356 ep_remove_callback_t callback, void *arg)
357{
358 assert(instance);
359 fibril_mutex_lock(&instance->guard);
360 endpoint_t *ep = find_locked(instance, address, endpoint, direction);
361 if (ep != NULL) {
362 list_remove(&ep->link);
363 instance->free_bw += ep->bandwidth;
364 }
365 fibril_mutex_unlock(&instance->guard);
366 if (ep == NULL)
367 return ENOENT;
368
369 if (callback) {
370 callback(ep, arg);
371 }
372 endpoint_destroy(ep);
373 return EOK;
374}
375
376int usb_endpoint_manager_reset_toggle(usb_endpoint_manager_t *instance,
377 usb_target_t target, bool all)
378{
379 assert(instance);
380 if (!usb_target_is_valid(target))
381 return EINVAL;
382
383 int ret = ENOENT;
384
385 fibril_mutex_lock(&instance->guard);
386 list_foreach(*get_list(instance, target.address), it) {
387 endpoint_t *ep = endpoint_get_instance(it);
388 if ((ep->address == target.address)
389 && (all || ep->endpoint == target.endpoint)) {
390 endpoint_toggle_set(ep, 0);
391 ret = EOK;
392 }
393 }
394 fibril_mutex_unlock(&instance->guard);
395 return ret;
396}
397
398/** Unregister and destroy all endpoints using given address.
399 * @param instance usb_endpoint_manager structure, non-null.
400 * @param address USB address.
401 * @param endpoint USB endpoint number.
402 * @param direction Communication direction.
403 * @param callback Function to call after unregister, before destruction.
404 * @arg Argument to pass to the callback function.
405 * @return Error code.
406 */
407int usb_endpoint_manager_remove_address(usb_endpoint_manager_t *instance,
408 usb_address_t address, ep_remove_callback_t callback, void *arg)
409{
410 assert(instance);
411 if (!usb_address_is_valid(address))
412 return EINVAL;
413
414 fibril_mutex_lock(&instance->guard);
415
416 const int ret = instance->devices[address].occupied ? EOK : ENOENT;
417 instance->devices[address].occupied = false;
418
419 list_foreach(*get_list(instance, address), iterator) {
420 endpoint_t *ep = endpoint_get_instance(iterator);
421 if (ep->address == address) {
422 iterator = iterator->next;
423 list_remove(&ep->link);
424 if (callback)
425 callback(ep, arg);
426 endpoint_destroy(ep);
427 }
428 }
429 fibril_mutex_unlock(&instance->guard);
430 return ret;
431}
432
433/** Request USB address.
434 * @param instance usb_device_manager
435 * @param address Pointer to requested address value, place to store new address
436 * @parma strict Fail if the requested address is not available.
437 * @return Error code.
438 * @note Default address is only available in strict mode.
439 */
440int usb_endpoint_manager_request_address(usb_endpoint_manager_t *instance,
441 usb_address_t *address, bool strict, usb_speed_t speed)
442{
443 assert(instance);
444 assert(address);
445 if (speed > instance->max_speed)
446 return ENOTSUP;
447
448 if (!usb_address_is_valid(*address))
449 return EINVAL;
450
451 usb_address_t addr = *address;
452
453 fibril_mutex_lock(&instance->guard);
454 /* Only grant default address to strict requests */
455 if ((addr == USB_ADDRESS_DEFAULT) && !strict) {
456 addr = usb_endpoint_manager_get_free_address(instance);
457 }
458
459 if (instance->devices[addr].occupied) {
460 if (strict) {
461 fibril_mutex_unlock(&instance->guard);
462 return ENOENT;
463 }
464 addr = usb_endpoint_manager_get_free_address(instance);
465 }
466 if (usb_address_is_valid(addr)) {
467 assert(instance->devices[addr].occupied == false);
468 assert(addr != USB_ADDRESS_DEFAULT || strict);
469
470 instance->devices[addr].occupied = true;
471 instance->devices[addr].speed = speed;
472 *address = addr;
473 addr = 0;
474 }
475
476 fibril_mutex_unlock(&instance->guard);
477 return addr;
478}
479
480/** Get speed assigned to USB address.
481 *
482 * @param[in] instance Device manager structure to use.
483 * @param[in] address Address the caller wants to find.
484 * @param[out] speed Assigned speed.
485 * @return Error code.
486 */
487int usb_endpoint_manager_get_speed(usb_endpoint_manager_t *instance,
488 usb_address_t address, usb_speed_t *speed)
489{
490 assert(instance);
491 if (!usb_address_is_valid(address)) {
492 return EINVAL;
493 }
494
495 fibril_mutex_lock(&instance->guard);
496
497 const int ret = instance->devices[address].occupied ? EOK : ENOENT;
498 if (speed && instance->devices[address].occupied) {
499 *speed = instance->devices[address].speed;
500 }
501
502 fibril_mutex_unlock(&instance->guard);
503 return ret;
504}
505/**
506 * @}
507 */
Note: See TracBrowser for help on using the repository browser.