| [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] | 54 | static 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] | 71 | static 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] | 87 | static 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.
|
|---|
| 104 | * @return Free address, or error code.
|
|---|
| 105 | */
|
|---|
| [4cf5b8e0] | 106 | static usb_address_t usb_bus_get_free_address(usb_bus_t *instance)
|
|---|
| [423c749] | 107 | {
|
|---|
| 108 |
|
|---|
| 109 | usb_address_t new_address = instance->last_address;
|
|---|
| 110 | do {
|
|---|
| 111 | new_address = (new_address + 1) % USB_ADDRESS_COUNT;
|
|---|
| 112 | if (new_address == USB_ADDRESS_DEFAULT)
|
|---|
| 113 | new_address = 1;
|
|---|
| 114 | if (new_address == instance->last_address)
|
|---|
| 115 | return ENOSPC;
|
|---|
| 116 | } while (instance->devices[new_address].occupied);
|
|---|
| 117 |
|
|---|
| 118 | assert(new_address != USB_ADDRESS_DEFAULT);
|
|---|
| 119 | instance->last_address = new_address;
|
|---|
| 120 |
|
|---|
| 121 | return new_address;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| [5400606] | 124 | /** Calculate bandwidth that needs to be reserved for communication with EP.
|
|---|
| 125 | * Calculation follows USB 1.1 specification.
|
|---|
| 126 | * @param speed Device's speed.
|
|---|
| 127 | * @param type Type of the transfer.
|
|---|
| 128 | * @param size Number of byte to transfer.
|
|---|
| 129 | * @param max_packet_size Maximum bytes in one packet.
|
|---|
| 130 | */
|
|---|
| [f0891ce] | 131 | size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
|
|---|
| 132 | size_t size, size_t max_packet_size)
|
|---|
| 133 | {
|
|---|
| [d41f301] | 134 | /* We care about bandwidth only for interrupt and isochronous. */
|
|---|
| 135 | if ((type != USB_TRANSFER_INTERRUPT)
|
|---|
| 136 | && (type != USB_TRANSFER_ISOCHRONOUS)) {
|
|---|
| 137 | return 0;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| [f0891ce] | 140 | const unsigned packet_count =
|
|---|
| 141 | (size + max_packet_size - 1) / max_packet_size;
|
|---|
| [7265558] | 142 | /* TODO: It may be that ISO and INT transfers use only one packet per
|
|---|
| 143 | * transaction, but I did not find text in USB spec to confirm this */
|
|---|
| [f0891ce] | 144 | /* NOTE: All data packets will be considered to be max_packet_size */
|
|---|
| 145 | switch (speed)
|
|---|
| 146 | {
|
|---|
| 147 | case USB_SPEED_LOW:
|
|---|
| 148 | assert(type == USB_TRANSFER_INTERRUPT);
|
|---|
| 149 | /* Protocol overhead 13B
|
|---|
| 150 | * (3 SYNC bytes, 3 PID bytes, 2 Endpoint + CRC bytes, 2
|
|---|
| 151 | * CRC bytes, and a 3-byte interpacket delay)
|
|---|
| 152 | * see USB spec page 45-46. */
|
|---|
| 153 | /* Speed penalty 8: low speed is 8-times slower*/
|
|---|
| 154 | return packet_count * (13 + max_packet_size) * 8;
|
|---|
| 155 | case USB_SPEED_FULL:
|
|---|
| 156 | /* Interrupt transfer overhead see above
|
|---|
| 157 | * or page 45 of USB spec */
|
|---|
| 158 | if (type == USB_TRANSFER_INTERRUPT)
|
|---|
| 159 | return packet_count * (13 + max_packet_size);
|
|---|
| 160 |
|
|---|
| 161 | assert(type == USB_TRANSFER_ISOCHRONOUS);
|
|---|
| 162 | /* Protocol overhead 9B
|
|---|
| 163 | * (2 SYNC bytes, 2 PID bytes, 2 Endpoint + CRC bytes, 2 CRC
|
|---|
| 164 | * bytes, and a 1-byte interpacket delay)
|
|---|
| 165 | * see USB spec page 42 */
|
|---|
| 166 | return packet_count * (9 + max_packet_size);
|
|---|
| 167 | default:
|
|---|
| 168 | return 0;
|
|---|
| 169 | }
|
|---|
| 170 | }
|
|---|
| [a76b01b4] | 171 |
|
|---|
| [75d8821] | 172 | /** Calculate bandwidth that needs to be reserved for communication with EP.
|
|---|
| 173 | * Calculation follows USB 2.0 specification.
|
|---|
| 174 | * @param speed Device's speed.
|
|---|
| 175 | * @param type Type of the transfer.
|
|---|
| 176 | * @param size Number of byte to transfer.
|
|---|
| 177 | * @param max_packet_size Maximum bytes in one packet.
|
|---|
| 178 | */
|
|---|
| 179 | size_t bandwidth_count_usb20(usb_speed_t speed, usb_transfer_type_t type,
|
|---|
| 180 | size_t size, size_t max_packet_size)
|
|---|
| 181 | {
|
|---|
| 182 | /* We care about bandwidth only for interrupt and isochronous. */
|
|---|
| 183 | if ((type != USB_TRANSFER_INTERRUPT)
|
|---|
| 184 | && (type != USB_TRANSFER_ISOCHRONOUS)) {
|
|---|
| 185 | return 0;
|
|---|
| 186 | }
|
|---|
| 187 | //TODO Implement
|
|---|
| 188 | return 0;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| [5400606] | 191 | /** Initialize to default state.
|
|---|
| 192 | * You need to provide valid bw_count function if you plan to use
|
|---|
| 193 | * add_endpoint/remove_endpoint pair.
|
|---|
| 194 | *
|
|---|
| [4cf5b8e0] | 195 | * @param instance usb_bus structure, non-null.
|
|---|
| [5400606] | 196 | * @param available_bandwidth Size of the bandwidth pool.
|
|---|
| 197 | * @param bw_count function to use to calculate endpoint bw requirements.
|
|---|
| 198 | * @return Error code.
|
|---|
| 199 | */
|
|---|
| [4cf5b8e0] | 200 | int usb_bus_init(usb_bus_t *instance,
|
|---|
| [423c749] | 201 | size_t available_bandwidth, bw_count_func_t bw_count, usb_speed_t max_speed)
|
|---|
| [f0891ce] | 202 | {
|
|---|
| 203 | assert(instance);
|
|---|
| 204 | fibril_mutex_initialize(&instance->guard);
|
|---|
| 205 | instance->free_bw = available_bandwidth;
|
|---|
| [933b0d7] | 206 | instance->bw_count = bw_count;
|
|---|
| [3aac088] | 207 | instance->last_address = 0;
|
|---|
| [423c749] | 208 | instance->max_speed = max_speed;
|
|---|
| 209 | for (unsigned i = 0; i < ARRAY_SIZE(instance->devices); ++i) {
|
|---|
| 210 | list_initialize(&instance->devices[i].endpoint_list);
|
|---|
| 211 | instance->devices[i].speed = USB_SPEED_MAX;
|
|---|
| 212 | instance->devices[i].occupied = false;
|
|---|
| [7265558] | 213 | }
|
|---|
| 214 | return EOK;
|
|---|
| [f0891ce] | 215 | }
|
|---|
| [a76b01b4] | 216 |
|
|---|
| [5400606] | 217 | /** Register endpoint structure.
|
|---|
| 218 | * Checks for duplicates.
|
|---|
| [4cf5b8e0] | 219 | * @param instance usb_bus, non-null.
|
|---|
| [5400606] | 220 | * @param ep endpoint_t to register.
|
|---|
| 221 | * @param data_size Size of data to transfer.
|
|---|
| 222 | * @return Error code.
|
|---|
| 223 | */
|
|---|
| [4cf5b8e0] | 224 | int usb_bus_register_ep(usb_bus_t *instance, endpoint_t *ep, size_t data_size)
|
|---|
| [48ae3ef] | 225 | {
|
|---|
| 226 | assert(instance);
|
|---|
| [5400606] | 227 | if (ep == NULL || ep->address < 0)
|
|---|
| 228 | return EINVAL;
|
|---|
| [48ae3ef] | 229 |
|
|---|
| [5400606] | 230 | fibril_mutex_lock(&instance->guard);
|
|---|
| 231 | /* Check for available bandwidth */
|
|---|
| [48ae3ef] | 232 | if (ep->bandwidth > instance->free_bw) {
|
|---|
| 233 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 234 | return ENOSPC;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | /* Check for existence */
|
|---|
| 238 | const endpoint_t *endpoint =
|
|---|
| 239 | find_locked(instance, ep->address, ep->endpoint, ep->direction);
|
|---|
| 240 | if (endpoint != NULL) {
|
|---|
| 241 | fibril_mutex_unlock(&instance->guard);
|
|---|
| [8a637a4] | 242 | return EEXIST;
|
|---|
| [48ae3ef] | 243 | }
|
|---|
| [f527f58] | 244 | /* Add endpoint list's reference to ep. */
|
|---|
| 245 | endpoint_add_ref(ep);
|
|---|
| [5400606] | 246 | list_append(&ep->link, get_list(instance, ep->address));
|
|---|
| [48ae3ef] | 247 |
|
|---|
| 248 | instance->free_bw -= ep->bandwidth;
|
|---|
| [c86ca9a] | 249 | usb_log_debug("Registered EP(%d:%d:%s:%s)\n", ep->address, ep->endpoint,
|
|---|
| 250 | usb_str_transfer_type_short(ep->transfer_type),
|
|---|
| 251 | usb_str_direction(ep->direction));
|
|---|
| [48ae3ef] | 252 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 253 | return EOK;
|
|---|
| 254 | }
|
|---|
| [a76b01b4] | 255 |
|
|---|
| [5400606] | 256 | /** Unregister endpoint structure.
|
|---|
| 257 | * Checks for duplicates.
|
|---|
| [4cf5b8e0] | 258 | * @param instance usb_bus, non-null.
|
|---|
| [5400606] | 259 | * @param ep endpoint_t to unregister.
|
|---|
| 260 | * @return Error code.
|
|---|
| 261 | */
|
|---|
| [4cf5b8e0] | 262 | int usb_bus_unregister_ep(usb_bus_t *instance, endpoint_t *ep)
|
|---|
| [48ae3ef] | 263 | {
|
|---|
| 264 | assert(instance);
|
|---|
| [5400606] | 265 | if (ep == NULL || ep->address < 0)
|
|---|
| 266 | return EINVAL;
|
|---|
| 267 |
|
|---|
| [48ae3ef] | 268 | fibril_mutex_lock(&instance->guard);
|
|---|
| [5400606] | 269 | if (!list_member(&ep->link, get_list(instance, ep->address))) {
|
|---|
| 270 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 271 | return ENOENT;
|
|---|
| 272 | }
|
|---|
| [48ae3ef] | 273 | list_remove(&ep->link);
|
|---|
| [5400606] | 274 | instance->free_bw += ep->bandwidth;
|
|---|
| [c86ca9a] | 275 | usb_log_debug("Unregistered EP(%d:%d:%s:%s)\n", ep->address,
|
|---|
| 276 | ep->endpoint, usb_str_transfer_type_short(ep->transfer_type),
|
|---|
| 277 | usb_str_direction(ep->direction));
|
|---|
| [f527f58] | 278 | /* Drop endpoint list's reference to ep. */
|
|---|
| 279 | endpoint_del_ref(ep);
|
|---|
| [48ae3ef] | 280 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 281 | return EOK;
|
|---|
| 282 | }
|
|---|
| [a76b01b4] | 283 |
|
|---|
| [5400606] | 284 | /** Find endpoint_t representing the given communication route.
|
|---|
| [4cf5b8e0] | 285 | * @param instance usb_bus, non-null.
|
|---|
| [5400606] | 286 | * @param address
|
|---|
| 287 | */
|
|---|
| [4cf5b8e0] | 288 | endpoint_t * usb_bus_find_ep(usb_bus_t *instance,
|
|---|
| [48ae3ef] | 289 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
|
|---|
| 290 | {
|
|---|
| 291 | assert(instance);
|
|---|
| 292 |
|
|---|
| 293 | fibril_mutex_lock(&instance->guard);
|
|---|
| 294 | endpoint_t *ep = find_locked(instance, address, endpoint, direction);
|
|---|
| [f527f58] | 295 | if (ep) {
|
|---|
| 296 | /* We are exporting ep to the outside world, add reference. */
|
|---|
| 297 | endpoint_add_ref(ep);
|
|---|
| 298 | }
|
|---|
| [48ae3ef] | 299 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 300 | return ep;
|
|---|
| 301 | }
|
|---|
| [a76b01b4] | 302 |
|
|---|
| [5400606] | 303 | /** Create and register new endpoint_t structure.
|
|---|
| [4cf5b8e0] | 304 | * @param instance usb_bus structure, non-null.
|
|---|
| [5400606] | 305 | * @param address USB address.
|
|---|
| 306 | * @param endpoint USB endpoint number.
|
|---|
| 307 | * @param direction Communication direction.
|
|---|
| 308 | * @param type USB transfer type.
|
|---|
| 309 | * @param speed USB Communication speed.
|
|---|
| 310 | * @param max_packet_size Maximum size of data packets.
|
|---|
| 311 | * @param data_size Expected communication size.
|
|---|
| 312 | * @param callback function to call just after registering.
|
|---|
| 313 | * @param arg Argument to pass to the callback function.
|
|---|
| 314 | * @return Error code.
|
|---|
| 315 | */
|
|---|
| [4cf5b8e0] | 316 | int usb_bus_add_ep(usb_bus_t *instance,
|
|---|
| [48ae3ef] | 317 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
|
|---|
| [4e732f1a] | 318 | usb_transfer_type_t type, size_t max_packet_size, unsigned packets,
|
|---|
| 319 | size_t data_size, ep_add_callback_t callback, void *arg,
|
|---|
| 320 | usb_address_t tt_address, unsigned tt_port)
|
|---|
| [48ae3ef] | 321 | {
|
|---|
| 322 | assert(instance);
|
|---|
| [5400606] | 323 | if (instance->bw_count == NULL)
|
|---|
| 324 | return ENOTSUP;
|
|---|
| [ec6766a] | 325 | if (!usb_address_is_valid(address))
|
|---|
| [5400606] | 326 | return EINVAL;
|
|---|
| 327 |
|
|---|
| [48ae3ef] | 328 |
|
|---|
| [5400606] | 329 | fibril_mutex_lock(&instance->guard);
|
|---|
| [ec6766a] | 330 | /* Check for speed and address */
|
|---|
| 331 | if (!instance->devices[address].occupied) {
|
|---|
| [5400606] | 332 | fibril_mutex_unlock(&instance->guard);
|
|---|
| [ec6766a] | 333 | return ENOENT;
|
|---|
| [5400606] | 334 | }
|
|---|
| 335 |
|
|---|
| 336 | /* Check for existence */
|
|---|
| 337 | endpoint_t *ep = find_locked(instance, address, endpoint, direction);
|
|---|
| 338 | if (ep != NULL) {
|
|---|
| 339 | fibril_mutex_unlock(&instance->guard);
|
|---|
| [8a637a4] | 340 | return EEXIST;
|
|---|
| [5400606] | 341 | }
|
|---|
| 342 |
|
|---|
| [ec6766a] | 343 | const usb_speed_t speed = instance->devices[address].speed;
|
|---|
| 344 | const size_t bw =
|
|---|
| 345 | instance->bw_count(speed, type, data_size, max_packet_size);
|
|---|
| 346 |
|
|---|
| 347 | /* Check for available bandwidth */
|
|---|
| 348 | if (bw > instance->free_bw) {
|
|---|
| 349 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 350 | return ENOSPC;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| [0ee999d] | 353 | ep = endpoint_create(address, endpoint, direction, type, speed,
|
|---|
| [4e732f1a] | 354 | max_packet_size, packets, bw, tt_address, tt_port);
|
|---|
| [5400606] | 355 | if (!ep) {
|
|---|
| 356 | fibril_mutex_unlock(&instance->guard);
|
|---|
| [48ae3ef] | 357 | return ENOMEM;
|
|---|
| [5400606] | 358 | }
|
|---|
| [48ae3ef] | 359 |
|
|---|
| [f527f58] | 360 | /* Add our reference to ep. */
|
|---|
| 361 | endpoint_add_ref(ep);
|
|---|
| 362 |
|
|---|
| [48ae3ef] | 363 | if (callback) {
|
|---|
| 364 | const int ret = callback(ep, arg);
|
|---|
| 365 | if (ret != EOK) {
|
|---|
| [5400606] | 366 | fibril_mutex_unlock(&instance->guard);
|
|---|
| [f527f58] | 367 | endpoint_del_ref(ep);
|
|---|
| [48ae3ef] | 368 | return ret;
|
|---|
| 369 | }
|
|---|
| 370 | }
|
|---|
| [f527f58] | 371 |
|
|---|
| 372 | /* Add endpoint list's reference to ep. */
|
|---|
| 373 | endpoint_add_ref(ep);
|
|---|
| [5400606] | 374 | list_append(&ep->link, get_list(instance, ep->address));
|
|---|
| [48ae3ef] | 375 |
|
|---|
| [5400606] | 376 | instance->free_bw -= ep->bandwidth;
|
|---|
| 377 | fibril_mutex_unlock(&instance->guard);
|
|---|
| [f527f58] | 378 |
|
|---|
| 379 | /* Drop our reference to ep. */
|
|---|
| 380 | endpoint_del_ref(ep);
|
|---|
| 381 |
|
|---|
| [5400606] | 382 | return EOK;
|
|---|
| [48ae3ef] | 383 | }
|
|---|
| [a76b01b4] | 384 |
|
|---|
| [5400606] | 385 | /** Unregister and destroy endpoint_t structure representing given route.
|
|---|
| [4cf5b8e0] | 386 | * @param instance usb_bus structure, non-null.
|
|---|
| [5400606] | 387 | * @param address USB address.
|
|---|
| 388 | * @param endpoint USB endpoint number.
|
|---|
| 389 | * @param direction Communication direction.
|
|---|
| 390 | * @param callback Function to call after unregister, before destruction.
|
|---|
| 391 | * @arg Argument to pass to the callback function.
|
|---|
| 392 | * @return Error code.
|
|---|
| 393 | */
|
|---|
| [4cf5b8e0] | 394 | int usb_bus_remove_ep(usb_bus_t *instance,
|
|---|
| [48ae3ef] | 395 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
|
|---|
| [17bbb28] | 396 | ep_remove_callback_t callback, void *arg)
|
|---|
| [48ae3ef] | 397 | {
|
|---|
| 398 | assert(instance);
|
|---|
| 399 | fibril_mutex_lock(&instance->guard);
|
|---|
| 400 | endpoint_t *ep = find_locked(instance, address, endpoint, direction);
|
|---|
| 401 | if (ep != NULL) {
|
|---|
| 402 | list_remove(&ep->link);
|
|---|
| 403 | instance->free_bw += ep->bandwidth;
|
|---|
| 404 | }
|
|---|
| 405 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 406 | if (ep == NULL)
|
|---|
| 407 | return ENOENT;
|
|---|
| 408 |
|
|---|
| 409 | if (callback) {
|
|---|
| 410 | callback(ep, arg);
|
|---|
| 411 | }
|
|---|
| [f527f58] | 412 | /* Drop endpoint list's reference to ep. */
|
|---|
| 413 | endpoint_del_ref(ep);
|
|---|
| [48ae3ef] | 414 | return EOK;
|
|---|
| 415 | }
|
|---|
| [a76b01b4] | 416 |
|
|---|
| [4cf5b8e0] | 417 | int usb_bus_reset_toggle(usb_bus_t *instance, usb_target_t target, bool all)
|
|---|
| [a6a9910] | 418 | {
|
|---|
| 419 | assert(instance);
|
|---|
| [8a23fef] | 420 | if (!usb_target_is_valid(target))
|
|---|
| [a6a9910] | 421 | return EINVAL;
|
|---|
| 422 |
|
|---|
| [8a23fef] | 423 | int ret = ENOENT;
|
|---|
| [a6a9910] | 424 |
|
|---|
| 425 | fibril_mutex_lock(&instance->guard);
|
|---|
| [3f03199] | 426 | list_foreach(*get_list(instance, target.address), link, endpoint_t, ep) {
|
|---|
| [a6a9910] | 427 | if ((ep->address == target.address)
|
|---|
| 428 | && (all || ep->endpoint == target.endpoint)) {
|
|---|
| 429 | endpoint_toggle_set(ep, 0);
|
|---|
| [8a23fef] | 430 | ret = EOK;
|
|---|
| [a6a9910] | 431 | }
|
|---|
| 432 | }
|
|---|
| 433 | fibril_mutex_unlock(&instance->guard);
|
|---|
| [8a23fef] | 434 | return ret;
|
|---|
| [a6a9910] | 435 | }
|
|---|
| 436 |
|
|---|
| [cbd568b] | 437 | /** Unregister and destroy all endpoints using given address.
|
|---|
| [4cf5b8e0] | 438 | * @param instance usb_bus structure, non-null.
|
|---|
| [cbd568b] | 439 | * @param address USB address.
|
|---|
| 440 | * @param endpoint USB endpoint number.
|
|---|
| 441 | * @param direction Communication direction.
|
|---|
| 442 | * @param callback Function to call after unregister, before destruction.
|
|---|
| 443 | * @arg Argument to pass to the callback function.
|
|---|
| 444 | * @return Error code.
|
|---|
| 445 | */
|
|---|
| [4cf5b8e0] | 446 | int usb_bus_remove_address(usb_bus_t *instance,
|
|---|
| [17bbb28] | 447 | usb_address_t address, ep_remove_callback_t callback, void *arg)
|
|---|
| [46f2808] | 448 | {
|
|---|
| 449 | assert(instance);
|
|---|
| [8a23fef] | 450 | if (!usb_address_is_valid(address))
|
|---|
| 451 | return EINVAL;
|
|---|
| 452 |
|
|---|
| [46f2808] | 453 | fibril_mutex_lock(&instance->guard);
|
|---|
| [feeac0d] | 454 |
|
|---|
| [8a23fef] | 455 | const int ret = instance->devices[address].occupied ? EOK : ENOENT;
|
|---|
| 456 | instance->devices[address].occupied = false;
|
|---|
| [feeac0d] | 457 |
|
|---|
| [3f03199] | 458 | list_t *list = get_list(instance, address);
|
|---|
| 459 | for (link_t *link = list_first(list); link != NULL; ) {
|
|---|
| 460 | endpoint_t *ep = list_get_instance(link, endpoint_t, link);
|
|---|
| 461 | link = list_next(link, list);
|
|---|
| [46f2808] | 462 | if (ep->address == address) {
|
|---|
| 463 | list_remove(&ep->link);
|
|---|
| 464 | if (callback)
|
|---|
| 465 | callback(ep, arg);
|
|---|
| [f527f58] | 466 | /* Drop endpoint list's reference to ep. */
|
|---|
| 467 | endpoint_del_ref(ep);
|
|---|
| [46f2808] | 468 | }
|
|---|
| 469 | }
|
|---|
| 470 | fibril_mutex_unlock(&instance->guard);
|
|---|
| [8a23fef] | 471 | return ret;
|
|---|
| [46f2808] | 472 | }
|
|---|
| [423c749] | 473 |
|
|---|
| 474 | /** Request USB address.
|
|---|
| 475 | * @param instance usb_device_manager
|
|---|
| 476 | * @param address Pointer to requested address value, place to store new address
|
|---|
| 477 | * @parma strict Fail if the requested address is not available.
|
|---|
| 478 | * @return Error code.
|
|---|
| 479 | * @note Default address is only available in strict mode.
|
|---|
| 480 | */
|
|---|
| [4cf5b8e0] | 481 | int usb_bus_request_address(usb_bus_t *instance,
|
|---|
| [423c749] | 482 | usb_address_t *address, bool strict, usb_speed_t speed)
|
|---|
| 483 | {
|
|---|
| 484 | assert(instance);
|
|---|
| 485 | assert(address);
|
|---|
| 486 | if (speed > instance->max_speed)
|
|---|
| 487 | return ENOTSUP;
|
|---|
| 488 |
|
|---|
| 489 | if (!usb_address_is_valid(*address))
|
|---|
| 490 | return EINVAL;
|
|---|
| 491 |
|
|---|
| 492 | usb_address_t addr = *address;
|
|---|
| 493 |
|
|---|
| 494 | fibril_mutex_lock(&instance->guard);
|
|---|
| 495 | /* Only grant default address to strict requests */
|
|---|
| 496 | if ((addr == USB_ADDRESS_DEFAULT) && !strict) {
|
|---|
| [4cf5b8e0] | 497 | addr = usb_bus_get_free_address(instance);
|
|---|
| [423c749] | 498 | }
|
|---|
| 499 |
|
|---|
| 500 | if (instance->devices[addr].occupied) {
|
|---|
| 501 | if (strict) {
|
|---|
| 502 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 503 | return ENOENT;
|
|---|
| 504 | }
|
|---|
| [4cf5b8e0] | 505 | addr = usb_bus_get_free_address(instance);
|
|---|
| [423c749] | 506 | }
|
|---|
| 507 | if (usb_address_is_valid(addr)) {
|
|---|
| 508 | assert(instance->devices[addr].occupied == false);
|
|---|
| 509 | assert(addr != USB_ADDRESS_DEFAULT || strict);
|
|---|
| 510 |
|
|---|
| 511 | instance->devices[addr].occupied = true;
|
|---|
| 512 | instance->devices[addr].speed = speed;
|
|---|
| 513 | *address = addr;
|
|---|
| 514 | addr = 0;
|
|---|
| 515 | }
|
|---|
| 516 |
|
|---|
| 517 | fibril_mutex_unlock(&instance->guard);
|
|---|
| 518 | return addr;
|
|---|
| 519 | }
|
|---|
| 520 |
|
|---|
| 521 | /** Get speed assigned to USB address.
|
|---|
| 522 | *
|
|---|
| 523 | * @param[in] instance Device manager structure to use.
|
|---|
| 524 | * @param[in] address Address the caller wants to find.
|
|---|
| 525 | * @param[out] speed Assigned speed.
|
|---|
| 526 | * @return Error code.
|
|---|
| 527 | */
|
|---|
| [4cf5b8e0] | 528 | int usb_bus_get_speed(usb_bus_t *instance, usb_address_t address,
|
|---|
| 529 | usb_speed_t *speed)
|
|---|
| [423c749] | 530 | {
|
|---|
| 531 | assert(instance);
|
|---|
| 532 | if (!usb_address_is_valid(address)) {
|
|---|
| 533 | return EINVAL;
|
|---|
| 534 | }
|
|---|
| 535 |
|
|---|
| 536 | fibril_mutex_lock(&instance->guard);
|
|---|
| 537 |
|
|---|
| [8a23fef] | 538 | const int ret = instance->devices[address].occupied ? EOK : ENOENT;
|
|---|
| [423c749] | 539 | if (speed && instance->devices[address].occupied) {
|
|---|
| 540 | *speed = instance->devices[address].speed;
|
|---|
| 541 | }
|
|---|
| 542 |
|
|---|
| 543 | fibril_mutex_unlock(&instance->guard);
|
|---|
| [8a23fef] | 544 | return ret;
|
|---|
| [46f2808] | 545 | }
|
|---|
| [cbd568b] | 546 | /**
|
|---|
| 547 | * @}
|
|---|
| 548 | */
|
|---|