[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>
|
---|
[4cf5b8e0] | 41 | #include <usb/host/usb_bus.h>
|
---|
[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] | 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.
|
---|
[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] | 70 | static 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] | 86 | static 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] | 105 | static 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] | 130 | size_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 |
|
---|
[5400606] | 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 | *
|
---|
[4cf5b8e0] | 175 | * @param instance usb_bus structure, non-null.
|
---|
[5400606] | 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 | */
|
---|
[4cf5b8e0] | 180 | int usb_bus_init(usb_bus_t *instance,
|
---|
[423c749] | 181 | size_t available_bandwidth, bw_count_func_t bw_count, usb_speed_t max_speed)
|
---|
[f0891ce] | 182 | {
|
---|
| 183 | assert(instance);
|
---|
| 184 | fibril_mutex_initialize(&instance->guard);
|
---|
| 185 | instance->free_bw = available_bandwidth;
|
---|
[933b0d7] | 186 | instance->bw_count = bw_count;
|
---|
[3aac088] | 187 | instance->last_address = 0;
|
---|
[423c749] | 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;
|
---|
[7265558] | 193 | }
|
---|
| 194 | return EOK;
|
---|
[f0891ce] | 195 | }
|
---|
[a76b01b4] | 196 |
|
---|
[5400606] | 197 | /** Register endpoint structure.
|
---|
| 198 | * Checks for duplicates.
|
---|
[4cf5b8e0] | 199 | * @param instance usb_bus, non-null.
|
---|
[5400606] | 200 | * @param ep endpoint_t to register.
|
---|
| 201 | * @param data_size Size of data to transfer.
|
---|
| 202 | * @return Error code.
|
---|
| 203 | */
|
---|
[4cf5b8e0] | 204 | int usb_bus_register_ep(usb_bus_t *instance, endpoint_t *ep, size_t data_size)
|
---|
[48ae3ef] | 205 | {
|
---|
| 206 | assert(instance);
|
---|
[5400606] | 207 | if (ep == NULL || ep->address < 0)
|
---|
| 208 | return EINVAL;
|
---|
[48ae3ef] | 209 |
|
---|
[5400606] | 210 | fibril_mutex_lock(&instance->guard);
|
---|
| 211 | /* Check for available bandwidth */
|
---|
[48ae3ef] | 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 | }
|
---|
[5400606] | 224 | list_append(&ep->link, get_list(instance, ep->address));
|
---|
[48ae3ef] | 225 |
|
---|
| 226 | instance->free_bw -= ep->bandwidth;
|
---|
| 227 | fibril_mutex_unlock(&instance->guard);
|
---|
| 228 | return EOK;
|
---|
| 229 | }
|
---|
[a76b01b4] | 230 |
|
---|
[5400606] | 231 | /** Unregister endpoint structure.
|
---|
| 232 | * Checks for duplicates.
|
---|
[4cf5b8e0] | 233 | * @param instance usb_bus, non-null.
|
---|
[5400606] | 234 | * @param ep endpoint_t to unregister.
|
---|
| 235 | * @return Error code.
|
---|
| 236 | */
|
---|
[4cf5b8e0] | 237 | int usb_bus_unregister_ep(usb_bus_t *instance, endpoint_t *ep)
|
---|
[48ae3ef] | 238 | {
|
---|
| 239 | assert(instance);
|
---|
[5400606] | 240 | if (ep == NULL || ep->address < 0)
|
---|
| 241 | return EINVAL;
|
---|
| 242 |
|
---|
[48ae3ef] | 243 | fibril_mutex_lock(&instance->guard);
|
---|
[5400606] | 244 | if (!list_member(&ep->link, get_list(instance, ep->address))) {
|
---|
| 245 | fibril_mutex_unlock(&instance->guard);
|
---|
| 246 | return ENOENT;
|
---|
| 247 | }
|
---|
[48ae3ef] | 248 | list_remove(&ep->link);
|
---|
[5400606] | 249 | instance->free_bw += ep->bandwidth;
|
---|
[48ae3ef] | 250 | fibril_mutex_unlock(&instance->guard);
|
---|
| 251 | return EOK;
|
---|
| 252 | }
|
---|
[a76b01b4] | 253 |
|
---|
[5400606] | 254 | /** Find endpoint_t representing the given communication route.
|
---|
[4cf5b8e0] | 255 | * @param instance usb_bus, non-null.
|
---|
[5400606] | 256 | * @param address
|
---|
| 257 | */
|
---|
[4cf5b8e0] | 258 | endpoint_t * usb_bus_find_ep(usb_bus_t *instance,
|
---|
[48ae3ef] | 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 | }
|
---|
[a76b01b4] | 268 |
|
---|
[5400606] | 269 | /** Create and register new endpoint_t structure.
|
---|
[4cf5b8e0] | 270 | * @param instance usb_bus structure, non-null.
|
---|
[5400606] | 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 | */
|
---|
[4cf5b8e0] | 282 | int usb_bus_add_ep(usb_bus_t *instance,
|
---|
[48ae3ef] | 283 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
|
---|
[ec6766a] | 284 | usb_transfer_type_t type, size_t max_packet_size, size_t data_size,
|
---|
[0ee999d] | 285 | ep_add_callback_t callback, void *arg, usb_address_t tt_address,
|
---|
| 286 | unsigned tt_port)
|
---|
[48ae3ef] | 287 | {
|
---|
| 288 | assert(instance);
|
---|
[5400606] | 289 | if (instance->bw_count == NULL)
|
---|
| 290 | return ENOTSUP;
|
---|
[ec6766a] | 291 | if (!usb_address_is_valid(address))
|
---|
[5400606] | 292 | return EINVAL;
|
---|
| 293 |
|
---|
[48ae3ef] | 294 |
|
---|
[5400606] | 295 | fibril_mutex_lock(&instance->guard);
|
---|
[ec6766a] | 296 | /* Check for speed and address */
|
---|
| 297 | if (!instance->devices[address].occupied) {
|
---|
[5400606] | 298 | fibril_mutex_unlock(&instance->guard);
|
---|
[ec6766a] | 299 | return ENOENT;
|
---|
[5400606] | 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 |
|
---|
[ec6766a] | 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 |
|
---|
[0ee999d] | 319 | ep = endpoint_create(address, endpoint, direction, type, speed,
|
---|
| 320 | max_packet_size, bw, tt_address, tt_port);
|
---|
[5400606] | 321 | if (!ep) {
|
---|
| 322 | fibril_mutex_unlock(&instance->guard);
|
---|
[48ae3ef] | 323 | return ENOMEM;
|
---|
[5400606] | 324 | }
|
---|
[48ae3ef] | 325 |
|
---|
| 326 | if (callback) {
|
---|
| 327 | const int ret = callback(ep, arg);
|
---|
| 328 | if (ret != EOK) {
|
---|
[5400606] | 329 | fibril_mutex_unlock(&instance->guard);
|
---|
[48ae3ef] | 330 | endpoint_destroy(ep);
|
---|
| 331 | return ret;
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
[5400606] | 334 | list_append(&ep->link, get_list(instance, ep->address));
|
---|
[48ae3ef] | 335 |
|
---|
[5400606] | 336 | instance->free_bw -= ep->bandwidth;
|
---|
| 337 | fibril_mutex_unlock(&instance->guard);
|
---|
| 338 | return EOK;
|
---|
[48ae3ef] | 339 | }
|
---|
[a76b01b4] | 340 |
|
---|
[5400606] | 341 | /** Unregister and destroy endpoint_t structure representing given route.
|
---|
[4cf5b8e0] | 342 | * @param instance usb_bus structure, non-null.
|
---|
[5400606] | 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 | */
|
---|
[4cf5b8e0] | 350 | int usb_bus_remove_ep(usb_bus_t *instance,
|
---|
[48ae3ef] | 351 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
|
---|
[17bbb28] | 352 | ep_remove_callback_t callback, void *arg)
|
---|
[48ae3ef] | 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 | }
|
---|
[a76b01b4] | 371 |
|
---|
[4cf5b8e0] | 372 | int usb_bus_reset_toggle(usb_bus_t *instance, usb_target_t target, bool all)
|
---|
[a6a9910] | 373 | {
|
---|
| 374 | assert(instance);
|
---|
[8a23fef] | 375 | if (!usb_target_is_valid(target))
|
---|
[a6a9910] | 376 | return EINVAL;
|
---|
| 377 |
|
---|
[8a23fef] | 378 | int ret = ENOENT;
|
---|
[a6a9910] | 379 |
|
---|
| 380 | fibril_mutex_lock(&instance->guard);
|
---|
[3f03199] | 381 | list_foreach(*get_list(instance, target.address), link, endpoint_t, ep) {
|
---|
[a6a9910] | 382 | if ((ep->address == target.address)
|
---|
| 383 | && (all || ep->endpoint == target.endpoint)) {
|
---|
| 384 | endpoint_toggle_set(ep, 0);
|
---|
[8a23fef] | 385 | ret = EOK;
|
---|
[a6a9910] | 386 | }
|
---|
| 387 | }
|
---|
| 388 | fibril_mutex_unlock(&instance->guard);
|
---|
[8a23fef] | 389 | return ret;
|
---|
[a6a9910] | 390 | }
|
---|
| 391 |
|
---|
[cbd568b] | 392 | /** Unregister and destroy all endpoints using given address.
|
---|
[4cf5b8e0] | 393 | * @param instance usb_bus structure, non-null.
|
---|
[cbd568b] | 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 | */
|
---|
[4cf5b8e0] | 401 | int usb_bus_remove_address(usb_bus_t *instance,
|
---|
[17bbb28] | 402 | usb_address_t address, ep_remove_callback_t callback, void *arg)
|
---|
[46f2808] | 403 | {
|
---|
| 404 | assert(instance);
|
---|
[8a23fef] | 405 | if (!usb_address_is_valid(address))
|
---|
| 406 | return EINVAL;
|
---|
| 407 |
|
---|
[46f2808] | 408 | fibril_mutex_lock(&instance->guard);
|
---|
[8a23fef] | 409 |
|
---|
| 410 | const int ret = instance->devices[address].occupied ? EOK : ENOENT;
|
---|
| 411 | instance->devices[address].occupied = false;
|
---|
| 412 |
|
---|
[3f03199] | 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);
|
---|
[46f2808] | 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);
|
---|
[8a23fef] | 425 | return ret;
|
---|
[46f2808] | 426 | }
|
---|
[423c749] | 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 | */
|
---|
[4cf5b8e0] | 435 | int usb_bus_request_address(usb_bus_t *instance,
|
---|
[423c749] | 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) {
|
---|
[4cf5b8e0] | 451 | addr = usb_bus_get_free_address(instance);
|
---|
[423c749] | 452 | }
|
---|
| 453 |
|
---|
| 454 | if (instance->devices[addr].occupied) {
|
---|
| 455 | if (strict) {
|
---|
| 456 | fibril_mutex_unlock(&instance->guard);
|
---|
| 457 | return ENOENT;
|
---|
| 458 | }
|
---|
[4cf5b8e0] | 459 | addr = usb_bus_get_free_address(instance);
|
---|
[423c749] | 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 | */
|
---|
[4cf5b8e0] | 482 | int usb_bus_get_speed(usb_bus_t *instance, usb_address_t address,
|
---|
| 483 | usb_speed_t *speed)
|
---|
[423c749] | 484 | {
|
---|
| 485 | assert(instance);
|
---|
| 486 | if (!usb_address_is_valid(address)) {
|
---|
| 487 | return EINVAL;
|
---|
| 488 | }
|
---|
| 489 |
|
---|
| 490 | fibril_mutex_lock(&instance->guard);
|
---|
| 491 |
|
---|
[8a23fef] | 492 | const int ret = instance->devices[address].occupied ? EOK : ENOENT;
|
---|
[423c749] | 493 | if (speed && instance->devices[address].occupied) {
|
---|
| 494 | *speed = instance->devices[address].speed;
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | fibril_mutex_unlock(&instance->guard);
|
---|
[8a23fef] | 498 | return ret;
|
---|
[423c749] | 499 | }
|
---|
[cbd568b] | 500 | /**
|
---|
| 501 | * @}
|
---|
| 502 | */
|
---|