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
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>
[c86ca9a]36#include <usb/debug.h>
[8d2e251]37
[f0891ce]38#include <assert.h>
39#include <errno.h>
[423c749]40#include <macros.h>
[8d2e251]41#include <stdbool.h>
[90b9ab5]42
[f0891ce]43
[5400606]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 */
[7265558]54static inline bool ep_match(const endpoint_t *ep,
55 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
[f0891ce]56{
[83c3123]57 assert(ep);
[7265558]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);
[f0891ce]64}
[a76b01b4]65
[cbd568b]66/** Get list that holds endpoints for given address.
[4cf5b8e0]67 * @param instance usb_bus structure, non-null.
[5400606]68 * @param addr USB address, must be >= 0.
69 * @return Pointer to the appropriate list.
70 */
[4cf5b8e0]71static list_t * get_list(usb_bus_t *instance, usb_address_t addr)
[f0891ce]72{
[7265558]73 assert(instance);
[5400606]74 assert(addr >= 0);
[423c749]75 return &instance->devices[addr % ARRAY_SIZE(instance->devices)].endpoint_list;
[f0891ce]76}
[a76b01b4]77
[5400606]78/** Internal search function, works on locked structure.
[4cf5b8e0]79 * @param instance usb_bus structure, non-null.
[5400606]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.
[cbd568b]85 * @note Assumes that the internal mutex is locked.
[5400606]86 */
[4cf5b8e0]87static endpoint_t * find_locked(usb_bus_t *instance,
[7265558]88 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
[ba038f4]89{
[7265558]90 assert(instance);
91 assert(fibril_mutex_is_locked(&instance->guard));
[5400606]92 if (address < 0)
93 return NULL;
[feeac0d]94 list_foreach(*get_list(instance, address), link, endpoint_t, ep) {
[7265558]95 if (ep_match(ep, address, endpoint, direction))
96 return ep;
97 }
98 return NULL;
[ba038f4]99}
[a76b01b4]100
[423c749]101/** Get a free USB address
102 *
103 * @param[in] instance Device manager structure to use.
[516d361]104 * @param[out] address Free address.
105 * @return Error code.
[423c749]106 */
[516d361]107static int usb_bus_get_free_address(usb_bus_t *instance, usb_address_t *address)
[423c749]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
[516d361]122 *address = new_address;
123 return EOK;
[423c749]124}
125
[5400606]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 */
[f0891ce]133size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
134 size_t size, size_t max_packet_size)
135{
[d41f301]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
[f0891ce]142 const unsigned packet_count =
143 (size + max_packet_size - 1) / max_packet_size;
[7265558]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 */
[f0891ce]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}
[a76b01b4]173
[75d8821]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
[5400606]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 *
[4cf5b8e0]197 * @param instance usb_bus structure, non-null.
[5400606]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 */
[4cf5b8e0]202int usb_bus_init(usb_bus_t *instance,
[423c749]203 size_t available_bandwidth, bw_count_func_t bw_count, usb_speed_t max_speed)
[f0891ce]204{
205 assert(instance);
206 fibril_mutex_initialize(&instance->guard);
207 instance->free_bw = available_bandwidth;
[933b0d7]208 instance->bw_count = bw_count;
[3aac088]209 instance->last_address = 0;
[423c749]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;
[7265558]215 }
216 return EOK;
[f0891ce]217}
[a76b01b4]218
[5400606]219/** Register endpoint structure.
220 * Checks for duplicates.
[4cf5b8e0]221 * @param instance usb_bus, non-null.
[5400606]222 * @param ep endpoint_t to register.
223 * @param data_size Size of data to transfer.
224 * @return Error code.
225 */
[4cf5b8e0]226int usb_bus_register_ep(usb_bus_t *instance, endpoint_t *ep, size_t data_size)
[48ae3ef]227{
228 assert(instance);
[5400606]229 if (ep == NULL || ep->address < 0)
230 return EINVAL;
[48ae3ef]231
[5400606]232 fibril_mutex_lock(&instance->guard);
233 /* Check for available bandwidth */
[48ae3ef]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);
[8a637a4]244 return EEXIST;
[48ae3ef]245 }
[f527f58]246 /* Add endpoint list's reference to ep. */
247 endpoint_add_ref(ep);
[5400606]248 list_append(&ep->link, get_list(instance, ep->address));
[48ae3ef]249
250 instance->free_bw -= ep->bandwidth;
[c86ca9a]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));
[48ae3ef]254 fibril_mutex_unlock(&instance->guard);
255 return EOK;
256}
[a76b01b4]257
[5400606]258/** Unregister endpoint structure.
259 * Checks for duplicates.
[4cf5b8e0]260 * @param instance usb_bus, non-null.
[5400606]261 * @param ep endpoint_t to unregister.
262 * @return Error code.
263 */
[4cf5b8e0]264int usb_bus_unregister_ep(usb_bus_t *instance, endpoint_t *ep)
[48ae3ef]265{
266 assert(instance);
[5400606]267 if (ep == NULL || ep->address < 0)
268 return EINVAL;
269
[48ae3ef]270 fibril_mutex_lock(&instance->guard);
[5400606]271 if (!list_member(&ep->link, get_list(instance, ep->address))) {
272 fibril_mutex_unlock(&instance->guard);
273 return ENOENT;
274 }
[48ae3ef]275 list_remove(&ep->link);
[5400606]276 instance->free_bw += ep->bandwidth;
[c86ca9a]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));
[f527f58]280 /* Drop endpoint list's reference to ep. */
281 endpoint_del_ref(ep);
[48ae3ef]282 fibril_mutex_unlock(&instance->guard);
283 return EOK;
284}
[a76b01b4]285
[5400606]286/** Find endpoint_t representing the given communication route.
[4cf5b8e0]287 * @param instance usb_bus, non-null.
[5400606]288 * @param address
289 */
[4cf5b8e0]290endpoint_t * usb_bus_find_ep(usb_bus_t *instance,
[48ae3ef]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);
[f527f58]297 if (ep) {
298 /* We are exporting ep to the outside world, add reference. */
299 endpoint_add_ref(ep);
300 }
[48ae3ef]301 fibril_mutex_unlock(&instance->guard);
302 return ep;
303}
[a76b01b4]304
[5400606]305/** Create and register new endpoint_t structure.
[4cf5b8e0]306 * @param instance usb_bus structure, non-null.
[5400606]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 */
[4cf5b8e0]318int usb_bus_add_ep(usb_bus_t *instance,
[48ae3ef]319 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
[4e732f1a]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)
[48ae3ef]323{
324 assert(instance);
[5400606]325 if (instance->bw_count == NULL)
326 return ENOTSUP;
[ec6766a]327 if (!usb_address_is_valid(address))
[5400606]328 return EINVAL;
329
[48ae3ef]330
[5400606]331 fibril_mutex_lock(&instance->guard);
[ec6766a]332 /* Check for speed and address */
333 if (!instance->devices[address].occupied) {
[5400606]334 fibril_mutex_unlock(&instance->guard);
[ec6766a]335 return ENOENT;
[5400606]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);
[8a637a4]342 return EEXIST;
[5400606]343 }
344
[ec6766a]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
[0ee999d]355 ep = endpoint_create(address, endpoint, direction, type, speed,
[4e732f1a]356 max_packet_size, packets, bw, tt_address, tt_port);
[5400606]357 if (!ep) {
358 fibril_mutex_unlock(&instance->guard);
[48ae3ef]359 return ENOMEM;
[5400606]360 }
[48ae3ef]361
[f527f58]362 /* Add our reference to ep. */
363 endpoint_add_ref(ep);
364
[48ae3ef]365 if (callback) {
366 const int ret = callback(ep, arg);
367 if (ret != EOK) {
[5400606]368 fibril_mutex_unlock(&instance->guard);
[f527f58]369 endpoint_del_ref(ep);
[48ae3ef]370 return ret;
371 }
372 }
[f527f58]373
374 /* Add endpoint list's reference to ep. */
375 endpoint_add_ref(ep);
[5400606]376 list_append(&ep->link, get_list(instance, ep->address));
[48ae3ef]377
[5400606]378 instance->free_bw -= ep->bandwidth;
379 fibril_mutex_unlock(&instance->guard);
[f527f58]380
381 /* Drop our reference to ep. */
382 endpoint_del_ref(ep);
383
[5400606]384 return EOK;
[48ae3ef]385}
[a76b01b4]386
[5400606]387/** Unregister and destroy endpoint_t structure representing given route.
[4cf5b8e0]388 * @param instance usb_bus structure, non-null.
[5400606]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 */
[4cf5b8e0]396int usb_bus_remove_ep(usb_bus_t *instance,
[48ae3ef]397 usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
[17bbb28]398 ep_remove_callback_t callback, void *arg)
[48ae3ef]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 }
[f527f58]414 /* Drop endpoint list's reference to ep. */
415 endpoint_del_ref(ep);
[48ae3ef]416 return EOK;
417}
[a76b01b4]418
[4cf5b8e0]419int usb_bus_reset_toggle(usb_bus_t *instance, usb_target_t target, bool all)
[a6a9910]420{
421 assert(instance);
[8a23fef]422 if (!usb_target_is_valid(target))
[a6a9910]423 return EINVAL;
424
[8a23fef]425 int ret = ENOENT;
[a6a9910]426
427 fibril_mutex_lock(&instance->guard);
[3f03199]428 list_foreach(*get_list(instance, target.address), link, endpoint_t, ep) {
[a6a9910]429 if ((ep->address == target.address)
430 && (all || ep->endpoint == target.endpoint)) {
431 endpoint_toggle_set(ep, 0);
[8a23fef]432 ret = EOK;
[a6a9910]433 }
434 }
435 fibril_mutex_unlock(&instance->guard);
[8a23fef]436 return ret;
[a6a9910]437}
438
[cbd568b]439/** Unregister and destroy all endpoints using given address.
[4cf5b8e0]440 * @param instance usb_bus structure, non-null.
[cbd568b]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 */
[4cf5b8e0]448int usb_bus_remove_address(usb_bus_t *instance,
[17bbb28]449 usb_address_t address, ep_remove_callback_t callback, void *arg)
[46f2808]450{
451 assert(instance);
[8a23fef]452 if (!usb_address_is_valid(address))
453 return EINVAL;
454
[46f2808]455 fibril_mutex_lock(&instance->guard);
[feeac0d]456
[8a23fef]457 const int ret = instance->devices[address].occupied ? EOK : ENOENT;
458 instance->devices[address].occupied = false;
[feeac0d]459
[3f03199]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);
[46f2808]464 if (ep->address == address) {
465 list_remove(&ep->link);
466 if (callback)
467 callback(ep, arg);
[f527f58]468 /* Drop endpoint list's reference to ep. */
469 endpoint_del_ref(ep);
[46f2808]470 }
471 }
472 fibril_mutex_unlock(&instance->guard);
[8a23fef]473 return ret;
[46f2808]474}
[423c749]475
476/** Request USB address.
477 * @param instance usb_device_manager
[516d361]478 * @param[inout] address Pointer to requested address value, place to store new address
[423c749]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 */
[4cf5b8e0]483int usb_bus_request_address(usb_bus_t *instance,
[423c749]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;
[516d361]495 int rc;
[423c749]496
497 fibril_mutex_lock(&instance->guard);
498 /* Only grant default address to strict requests */
499 if ((addr == USB_ADDRESS_DEFAULT) && !strict) {
[516d361]500 rc = usb_bus_get_free_address(instance, &addr);
501 if (rc != EOK) {
502 fibril_mutex_unlock(&instance->guard);
503 return rc;
504 }
[423c749]505 }
506
507 if (instance->devices[addr].occupied) {
508 if (strict) {
509 fibril_mutex_unlock(&instance->guard);
510 return ENOENT;
511 }
[516d361]512 rc = usb_bus_get_free_address(instance, &addr);
513 if (rc != EOK) {
514 fibril_mutex_unlock(&instance->guard);
515 return rc;
516 }
[423c749]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);
[516d361]529
530 *address = addr;
531 return EOK;
[423c749]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 */
[4cf5b8e0]541int usb_bus_get_speed(usb_bus_t *instance, usb_address_t address,
542 usb_speed_t *speed)
[423c749]543{
544 assert(instance);
545 if (!usb_address_is_valid(address)) {
546 return EINVAL;
547 }
548
549 fibril_mutex_lock(&instance->guard);
550
[8a23fef]551 const int ret = instance->devices[address].occupied ? EOK : ENOENT;
[423c749]552 if (speed && instance->devices[address].occupied) {
553 *speed = instance->devices[address].speed;
554 }
555
556 fibril_mutex_unlock(&instance->guard);
[8a23fef]557 return ret;
[46f2808]558}
[cbd568b]559/**
560 * @}
561 */
Note: See TracBrowser for help on using the repository browser.