source: mainline/uspace/lib/usbhost/src/usb_bus.c@ de0213c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since de0213c was 516d361, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Return address from usb_bus_get_free_address() separately from error code.

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