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