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
RevLine 
[f0891ce]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 */
[cbd568b]28/** @addtogroup libusbhost
29 * @{
30 */
31/** @file
32 * HC Endpoint management.
33 */
[f0891ce]34
[8d2e251]35#include <usb/host/usb_bus.h>
36
[f0891ce]37#include <assert.h>
38#include <errno.h>
[423c749]39#include <macros.h>
[8d2e251]40#include <stdbool.h>
[90b9ab5]41
[f0891ce]42
[5400606]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 */
[7265558]53static inline bool ep_match(const endpoint_t *ep,
54 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
[f0891ce]55{
[83c3123]56 assert(ep);
[7265558]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);
[f0891ce]63}
[a76b01b4]64
[cbd568b]65/** Get list that holds endpoints for given address.
[4cf5b8e0]66 * @param instance usb_bus structure, non-null.
[5400606]67 * @param addr USB address, must be >= 0.
68 * @return Pointer to the appropriate list.
69 */
[4cf5b8e0]70static list_t * get_list(usb_bus_t *instance, usb_address_t addr)
[f0891ce]71{
[7265558]72 assert(instance);
[5400606]73 assert(addr >= 0);
[423c749]74 return &instance->devices[addr % ARRAY_SIZE(instance->devices)].endpoint_list;
[f0891ce]75}
[a76b01b4]76
[5400606]77/** Internal search function, works on locked structure.
[4cf5b8e0]78 * @param instance usb_bus structure, non-null.
[5400606]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.
[cbd568b]84 * @note Assumes that the internal mutex is locked.
[5400606]85 */
[4cf5b8e0]86static endpoint_t * find_locked(usb_bus_t *instance,
[7265558]87 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
[ba038f4]88{
[7265558]89 assert(instance);
90 assert(fibril_mutex_is_locked(&instance->guard));
[5400606]91 if (address < 0)
92 return NULL;
[feeac0d]93 list_foreach(*get_list(instance, address), link, endpoint_t, ep) {
[7265558]94 if (ep_match(ep, address, endpoint, direction))
95 return ep;
96 }
97 return NULL;
[ba038f4]98}
[a76b01b4]99
[423c749]100/** Get a free USB address
101 *
102 * @param[in] instance Device manager structure to use.
103 * @return Free address, or error code.
104 */
[4cf5b8e0]105static usb_address_t usb_bus_get_free_address(usb_bus_t *instance)
[423c749]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
[5400606]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 */
[f0891ce]130size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
131 size_t size, size_t max_packet_size)
132{
[d41f301]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
[f0891ce]139 const unsigned packet_count =
140 (size + max_packet_size - 1) / max_packet_size;
[7265558]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 */
[f0891ce]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}
[a76b01b4]170
[75d8821]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
[5400606]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 *
[4cf5b8e0]194 * @param instance usb_bus structure, non-null.
[5400606]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 */
[4cf5b8e0]199int usb_bus_init(usb_bus_t *instance,
[423c749]200 size_t available_bandwidth, bw_count_func_t bw_count, usb_speed_t max_speed)
[f0891ce]201{
202 assert(instance);
203 fibril_mutex_initialize(&instance->guard);
204 instance->free_bw = available_bandwidth;
[933b0d7]205 instance->bw_count = bw_count;
[3aac088]206 instance->last_address = 0;
[423c749]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;
[7265558]212 }
213 return EOK;
[f0891ce]214}
[a76b01b4]215
[5400606]216/** Register endpoint structure.
217 * Checks for duplicates.
[4cf5b8e0]218 * @param instance usb_bus, non-null.
[5400606]219 * @param ep endpoint_t to register.
220 * @param data_size Size of data to transfer.
221 * @return Error code.
222 */
[4cf5b8e0]223int usb_bus_register_ep(usb_bus_t *instance, endpoint_t *ep, size_t data_size)
[48ae3ef]224{
225 assert(instance);
[5400606]226 if (ep == NULL || ep->address < 0)
227 return EINVAL;
[48ae3ef]228
[5400606]229 fibril_mutex_lock(&instance->guard);
230 /* Check for available bandwidth */
[48ae3ef]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 }
[5400606]243 list_append(&ep->link, get_list(instance, ep->address));
[48ae3ef]244
245 instance->free_bw -= ep->bandwidth;
246 fibril_mutex_unlock(&instance->guard);
247 return EOK;
248}
[a76b01b4]249
[5400606]250/** Unregister endpoint structure.
251 * Checks for duplicates.
[4cf5b8e0]252 * @param instance usb_bus, non-null.
[5400606]253 * @param ep endpoint_t to unregister.
254 * @return Error code.
255 */
[4cf5b8e0]256int usb_bus_unregister_ep(usb_bus_t *instance, endpoint_t *ep)
[48ae3ef]257{
258 assert(instance);
[5400606]259 if (ep == NULL || ep->address < 0)
260 return EINVAL;
261
[48ae3ef]262 fibril_mutex_lock(&instance->guard);
[5400606]263 if (!list_member(&ep->link, get_list(instance, ep->address))) {
264 fibril_mutex_unlock(&instance->guard);
265 return ENOENT;
266 }
[48ae3ef]267 list_remove(&ep->link);
[5400606]268 instance->free_bw += ep->bandwidth;
[48ae3ef]269 fibril_mutex_unlock(&instance->guard);
270 return EOK;
271}
[a76b01b4]272
[5400606]273/** Find endpoint_t representing the given communication route.
[4cf5b8e0]274 * @param instance usb_bus, non-null.
[5400606]275 * @param address
276 */
[4cf5b8e0]277endpoint_t * usb_bus_find_ep(usb_bus_t *instance,
[48ae3ef]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}
[a76b01b4]287
[5400606]288/** Create and register new endpoint_t structure.
[4cf5b8e0]289 * @param instance usb_bus structure, non-null.
[5400606]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 */
[4cf5b8e0]301int usb_bus_add_ep(usb_bus_t *instance,
[48ae3ef]302 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
[4e732f1a]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)
[48ae3ef]306{
307 assert(instance);
[5400606]308 if (instance->bw_count == NULL)
309 return ENOTSUP;
[ec6766a]310 if (!usb_address_is_valid(address))
[5400606]311 return EINVAL;
312
[48ae3ef]313
[5400606]314 fibril_mutex_lock(&instance->guard);
[ec6766a]315 /* Check for speed and address */
316 if (!instance->devices[address].occupied) {
[5400606]317 fibril_mutex_unlock(&instance->guard);
[ec6766a]318 return ENOENT;
[5400606]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
[ec6766a]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
[0ee999d]338 ep = endpoint_create(address, endpoint, direction, type, speed,
[4e732f1a]339 max_packet_size, packets, bw, tt_address, tt_port);
[5400606]340 if (!ep) {
341 fibril_mutex_unlock(&instance->guard);
[48ae3ef]342 return ENOMEM;
[5400606]343 }
[48ae3ef]344
345 if (callback) {
346 const int ret = callback(ep, arg);
347 if (ret != EOK) {
[5400606]348 fibril_mutex_unlock(&instance->guard);
[48ae3ef]349 endpoint_destroy(ep);
350 return ret;
351 }
352 }
[5400606]353 list_append(&ep->link, get_list(instance, ep->address));
[48ae3ef]354
[5400606]355 instance->free_bw -= ep->bandwidth;
356 fibril_mutex_unlock(&instance->guard);
357 return EOK;
[48ae3ef]358}
[a76b01b4]359
[5400606]360/** Unregister and destroy endpoint_t structure representing given route.
[4cf5b8e0]361 * @param instance usb_bus structure, non-null.
[5400606]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 */
[4cf5b8e0]369int usb_bus_remove_ep(usb_bus_t *instance,
[48ae3ef]370 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
[17bbb28]371 ep_remove_callback_t callback, void *arg)
[48ae3ef]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}
[a76b01b4]390
[4cf5b8e0]391int usb_bus_reset_toggle(usb_bus_t *instance, usb_target_t target, bool all)
[a6a9910]392{
393 assert(instance);
[8a23fef]394 if (!usb_target_is_valid(target))
[a6a9910]395 return EINVAL;
396
[8a23fef]397 int ret = ENOENT;
[a6a9910]398
399 fibril_mutex_lock(&instance->guard);
[3f03199]400 list_foreach(*get_list(instance, target.address), link, endpoint_t, ep) {
[a6a9910]401 if ((ep->address == target.address)
402 && (all || ep->endpoint == target.endpoint)) {
403 endpoint_toggle_set(ep, 0);
[8a23fef]404 ret = EOK;
[a6a9910]405 }
406 }
407 fibril_mutex_unlock(&instance->guard);
[8a23fef]408 return ret;
[a6a9910]409}
410
[cbd568b]411/** Unregister and destroy all endpoints using given address.
[4cf5b8e0]412 * @param instance usb_bus structure, non-null.
[cbd568b]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 */
[4cf5b8e0]420int usb_bus_remove_address(usb_bus_t *instance,
[17bbb28]421 usb_address_t address, ep_remove_callback_t callback, void *arg)
[46f2808]422{
423 assert(instance);
[8a23fef]424 if (!usb_address_is_valid(address))
425 return EINVAL;
426
[46f2808]427 fibril_mutex_lock(&instance->guard);
[8a23fef]428
429 const int ret = instance->devices[address].occupied ? EOK : ENOENT;
430 instance->devices[address].occupied = false;
431
[3f03199]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);
[46f2808]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);
[8a23fef]444 return ret;
[46f2808]445}
[423c749]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 */
[4cf5b8e0]454int usb_bus_request_address(usb_bus_t *instance,
[423c749]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) {
[4cf5b8e0]470 addr = usb_bus_get_free_address(instance);
[423c749]471 }
472
473 if (instance->devices[addr].occupied) {
474 if (strict) {
475 fibril_mutex_unlock(&instance->guard);
476 return ENOENT;
477 }
[4cf5b8e0]478 addr = usb_bus_get_free_address(instance);
[423c749]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 */
[4cf5b8e0]501int usb_bus_get_speed(usb_bus_t *instance, usb_address_t address,
502 usb_speed_t *speed)
[423c749]503{
504 assert(instance);
505 if (!usb_address_is_valid(address)) {
506 return EINVAL;
507 }
508
509 fibril_mutex_lock(&instance->guard);
510
[8a23fef]511 const int ret = instance->devices[address].occupied ? EOK : ENOENT;
[423c749]512 if (speed && instance->devices[address].occupied) {
513 *speed = instance->devices[address].speed;
514 }
515
516 fibril_mutex_unlock(&instance->guard);
[8a23fef]517 return ret;
[423c749]518}
[cbd568b]519/**
520 * @}
521 */
Note: See TracBrowser for help on using the repository browser.