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

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

usb: Add support for multiple packets per microframe.

  • Property mode set to 100644
File size: 15.5 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
37#include <assert.h>
38#include <errno.h>
39#include <macros.h>
40#include <stdbool.h>
41
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/** Calculate bandwidth that needs to be reserved for communication with EP.
172 * Calculation follows USB 2.0 specification.
173 * @param speed Device's speed.
174 * @param type Type of the transfer.
175 * @param size Number of byte to transfer.
176 * @param max_packet_size Maximum bytes in one packet.
177 */
178size_t bandwidth_count_usb20(usb_speed_t speed, usb_transfer_type_t type,
179 size_t size, size_t max_packet_size)
180{
181 /* We care about bandwidth only for interrupt and isochronous. */
182 if ((type != USB_TRANSFER_INTERRUPT)
183 && (type != USB_TRANSFER_ISOCHRONOUS)) {
184 return 0;
185 }
186 //TODO Implement
187 return 0;
188}
189
190/** Initialize to default state.
191 * You need to provide valid bw_count function if you plan to use
192 * add_endpoint/remove_endpoint pair.
193 *
194 * @param instance usb_bus structure, non-null.
195 * @param available_bandwidth Size of the bandwidth pool.
196 * @param bw_count function to use to calculate endpoint bw requirements.
197 * @return Error code.
198 */
199int usb_bus_init(usb_bus_t *instance,
200 size_t available_bandwidth, bw_count_func_t bw_count, usb_speed_t max_speed)
201{
202 assert(instance);
203 fibril_mutex_initialize(&instance->guard);
204 instance->free_bw = available_bandwidth;
205 instance->bw_count = bw_count;
206 instance->last_address = 0;
207 instance->max_speed = max_speed;
208 for (unsigned i = 0; i < ARRAY_SIZE(instance->devices); ++i) {
209 list_initialize(&instance->devices[i].endpoint_list);
210 instance->devices[i].speed = USB_SPEED_MAX;
211 instance->devices[i].occupied = false;
212 }
213 return EOK;
214}
215
216/** Register endpoint structure.
217 * Checks for duplicates.
218 * @param instance usb_bus, non-null.
219 * @param ep endpoint_t to register.
220 * @param data_size Size of data to transfer.
221 * @return Error code.
222 */
223int usb_bus_register_ep(usb_bus_t *instance, endpoint_t *ep, size_t data_size)
224{
225 assert(instance);
226 if (ep == NULL || ep->address < 0)
227 return EINVAL;
228
229 fibril_mutex_lock(&instance->guard);
230 /* Check for available bandwidth */
231 if (ep->bandwidth > instance->free_bw) {
232 fibril_mutex_unlock(&instance->guard);
233 return ENOSPC;
234 }
235
236 /* Check for existence */
237 const endpoint_t *endpoint =
238 find_locked(instance, ep->address, ep->endpoint, ep->direction);
239 if (endpoint != NULL) {
240 fibril_mutex_unlock(&instance->guard);
241 return EEXISTS;
242 }
243 list_append(&ep->link, get_list(instance, ep->address));
244
245 instance->free_bw -= ep->bandwidth;
246 fibril_mutex_unlock(&instance->guard);
247 return EOK;
248}
249
250/** Unregister endpoint structure.
251 * Checks for duplicates.
252 * @param instance usb_bus, non-null.
253 * @param ep endpoint_t to unregister.
254 * @return Error code.
255 */
256int usb_bus_unregister_ep(usb_bus_t *instance, endpoint_t *ep)
257{
258 assert(instance);
259 if (ep == NULL || ep->address < 0)
260 return EINVAL;
261
262 fibril_mutex_lock(&instance->guard);
263 if (!list_member(&ep->link, get_list(instance, ep->address))) {
264 fibril_mutex_unlock(&instance->guard);
265 return ENOENT;
266 }
267 list_remove(&ep->link);
268 instance->free_bw += ep->bandwidth;
269 fibril_mutex_unlock(&instance->guard);
270 return EOK;
271}
272
273/** Find endpoint_t representing the given communication route.
274 * @param instance usb_bus, non-null.
275 * @param address
276 */
277endpoint_t * usb_bus_find_ep(usb_bus_t *instance,
278 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
279{
280 assert(instance);
281
282 fibril_mutex_lock(&instance->guard);
283 endpoint_t *ep = find_locked(instance, address, endpoint, direction);
284 fibril_mutex_unlock(&instance->guard);
285 return ep;
286}
287
288/** Create and register new endpoint_t structure.
289 * @param instance usb_bus structure, non-null.
290 * @param address USB address.
291 * @param endpoint USB endpoint number.
292 * @param direction Communication direction.
293 * @param type USB transfer type.
294 * @param speed USB Communication speed.
295 * @param max_packet_size Maximum size of data packets.
296 * @param data_size Expected communication size.
297 * @param callback function to call just after registering.
298 * @param arg Argument to pass to the callback function.
299 * @return Error code.
300 */
301int usb_bus_add_ep(usb_bus_t *instance,
302 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
303 usb_transfer_type_t type, size_t max_packet_size, unsigned packets,
304 size_t data_size, ep_add_callback_t callback, void *arg,
305 usb_address_t tt_address, unsigned tt_port)
306{
307 assert(instance);
308 if (instance->bw_count == NULL)
309 return ENOTSUP;
310 if (!usb_address_is_valid(address))
311 return EINVAL;
312
313
314 fibril_mutex_lock(&instance->guard);
315 /* Check for speed and address */
316 if (!instance->devices[address].occupied) {
317 fibril_mutex_unlock(&instance->guard);
318 return ENOENT;
319 }
320
321 /* Check for existence */
322 endpoint_t *ep = find_locked(instance, address, endpoint, direction);
323 if (ep != NULL) {
324 fibril_mutex_unlock(&instance->guard);
325 return EEXISTS;
326 }
327
328 const usb_speed_t speed = instance->devices[address].speed;
329 const size_t bw =
330 instance->bw_count(speed, type, data_size, max_packet_size);
331
332 /* Check for available bandwidth */
333 if (bw > instance->free_bw) {
334 fibril_mutex_unlock(&instance->guard);
335 return ENOSPC;
336 }
337
338 ep = endpoint_create(address, endpoint, direction, type, speed,
339 max_packet_size, packets, bw, tt_address, tt_port);
340 if (!ep) {
341 fibril_mutex_unlock(&instance->guard);
342 return ENOMEM;
343 }
344
345 if (callback) {
346 const int ret = callback(ep, arg);
347 if (ret != EOK) {
348 fibril_mutex_unlock(&instance->guard);
349 endpoint_destroy(ep);
350 return ret;
351 }
352 }
353 list_append(&ep->link, get_list(instance, ep->address));
354
355 instance->free_bw -= ep->bandwidth;
356 fibril_mutex_unlock(&instance->guard);
357 return EOK;
358}
359
360/** Unregister and destroy endpoint_t structure representing given route.
361 * @param instance usb_bus structure, non-null.
362 * @param address USB address.
363 * @param endpoint USB endpoint number.
364 * @param direction Communication direction.
365 * @param callback Function to call after unregister, before destruction.
366 * @arg Argument to pass to the callback function.
367 * @return Error code.
368 */
369int usb_bus_remove_ep(usb_bus_t *instance,
370 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
371 ep_remove_callback_t callback, void *arg)
372{
373 assert(instance);
374 fibril_mutex_lock(&instance->guard);
375 endpoint_t *ep = find_locked(instance, address, endpoint, direction);
376 if (ep != NULL) {
377 list_remove(&ep->link);
378 instance->free_bw += ep->bandwidth;
379 }
380 fibril_mutex_unlock(&instance->guard);
381 if (ep == NULL)
382 return ENOENT;
383
384 if (callback) {
385 callback(ep, arg);
386 }
387 endpoint_destroy(ep);
388 return EOK;
389}
390
391int usb_bus_reset_toggle(usb_bus_t *instance, usb_target_t target, bool all)
392{
393 assert(instance);
394 if (!usb_target_is_valid(target))
395 return EINVAL;
396
397 int ret = ENOENT;
398
399 fibril_mutex_lock(&instance->guard);
400 list_foreach(*get_list(instance, target.address), link, endpoint_t, ep) {
401 if ((ep->address == target.address)
402 && (all || ep->endpoint == target.endpoint)) {
403 endpoint_toggle_set(ep, 0);
404 ret = EOK;
405 }
406 }
407 fibril_mutex_unlock(&instance->guard);
408 return ret;
409}
410
411/** Unregister and destroy all endpoints using given address.
412 * @param instance usb_bus structure, non-null.
413 * @param address USB address.
414 * @param endpoint USB endpoint number.
415 * @param direction Communication direction.
416 * @param callback Function to call after unregister, before destruction.
417 * @arg Argument to pass to the callback function.
418 * @return Error code.
419 */
420int usb_bus_remove_address(usb_bus_t *instance,
421 usb_address_t address, ep_remove_callback_t callback, void *arg)
422{
423 assert(instance);
424 if (!usb_address_is_valid(address))
425 return EINVAL;
426
427 fibril_mutex_lock(&instance->guard);
428
429 const int ret = instance->devices[address].occupied ? EOK : ENOENT;
430 instance->devices[address].occupied = false;
431
432 list_t *list = get_list(instance, address);
433 for (link_t *link = list_first(list); link != NULL; ) {
434 endpoint_t *ep = list_get_instance(link, endpoint_t, link);
435 link = list_next(link, list);
436 if (ep->address == address) {
437 list_remove(&ep->link);
438 if (callback)
439 callback(ep, arg);
440 endpoint_destroy(ep);
441 }
442 }
443 fibril_mutex_unlock(&instance->guard);
444 return ret;
445}
446
447/** Request USB address.
448 * @param instance usb_device_manager
449 * @param address Pointer to requested address value, place to store new address
450 * @parma strict Fail if the requested address is not available.
451 * @return Error code.
452 * @note Default address is only available in strict mode.
453 */
454int usb_bus_request_address(usb_bus_t *instance,
455 usb_address_t *address, bool strict, usb_speed_t speed)
456{
457 assert(instance);
458 assert(address);
459 if (speed > instance->max_speed)
460 return ENOTSUP;
461
462 if (!usb_address_is_valid(*address))
463 return EINVAL;
464
465 usb_address_t addr = *address;
466
467 fibril_mutex_lock(&instance->guard);
468 /* Only grant default address to strict requests */
469 if ((addr == USB_ADDRESS_DEFAULT) && !strict) {
470 addr = usb_bus_get_free_address(instance);
471 }
472
473 if (instance->devices[addr].occupied) {
474 if (strict) {
475 fibril_mutex_unlock(&instance->guard);
476 return ENOENT;
477 }
478 addr = usb_bus_get_free_address(instance);
479 }
480 if (usb_address_is_valid(addr)) {
481 assert(instance->devices[addr].occupied == false);
482 assert(addr != USB_ADDRESS_DEFAULT || strict);
483
484 instance->devices[addr].occupied = true;
485 instance->devices[addr].speed = speed;
486 *address = addr;
487 addr = 0;
488 }
489
490 fibril_mutex_unlock(&instance->guard);
491 return addr;
492}
493
494/** Get speed assigned to USB address.
495 *
496 * @param[in] instance Device manager structure to use.
497 * @param[in] address Address the caller wants to find.
498 * @param[out] speed Assigned speed.
499 * @return Error code.
500 */
501int usb_bus_get_speed(usb_bus_t *instance, usb_address_t address,
502 usb_speed_t *speed)
503{
504 assert(instance);
505 if (!usb_address_is_valid(address)) {
506 return EINVAL;
507 }
508
509 fibril_mutex_lock(&instance->guard);
510
511 const int ret = instance->devices[address].occupied ? EOK : ENOENT;
512 if (speed && instance->devices[address].occupied) {
513 *speed = instance->devices[address].speed;
514 }
515
516 fibril_mutex_unlock(&instance->guard);
517 return ret;
518}
519/**
520 * @}
521 */
Note: See TracBrowser for help on using the repository browser.