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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e9e24f2 was f527f58, checked in by Jakub Jermar <jakub@…>, 9 years ago

Reference-count endpoint_t structures

Track explicit references to USB endpoints. Call endpoint_destroy()
only after the last reference is destroyed. This prevents the scenario
in which removal was attempted on an endpoint while there was a transfer
in progress.

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