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

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

libusbhost: Rename usb_endpoint_manager → usb_bus

It's shorter and more accurate.

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