[41924f30] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
---|
| 3 | * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 | /** @addtogroup libusbhost
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * HC Endpoint management.
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <usb/host/usb2_bus.h>
|
---|
| 37 | #include <usb/host/endpoint.h>
|
---|
[20eaa82] | 38 | #include <usb/host/ddf_helpers.h>
|
---|
[41924f30] | 39 | #include <usb/debug.h>
|
---|
[20eaa82] | 40 | #include <usb/request.h>
|
---|
| 41 | #include <usb/descriptor.h>
|
---|
| 42 | #include <usb/usb.h>
|
---|
[41924f30] | 43 |
|
---|
| 44 | #include <assert.h>
|
---|
| 45 | #include <errno.h>
|
---|
| 46 | #include <macros.h>
|
---|
[20eaa82] | 47 | #include <str_error.h>
|
---|
[e6b9182] | 48 | #include <stdlib.h>
|
---|
[41924f30] | 49 | #include <stdbool.h>
|
---|
| 50 |
|
---|
| 51 | /** Ops receive generic bus_t pointer. */
|
---|
| 52 | static inline usb2_bus_t *bus_to_usb2_bus(bus_t *bus_base)
|
---|
| 53 | {
|
---|
| 54 | assert(bus_base);
|
---|
| 55 | return (usb2_bus_t *) bus_base;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | /** Get list that holds endpoints for given address.
|
---|
| 59 | * @param bus usb2_bus structure, non-null.
|
---|
| 60 | * @param addr USB address, must be >= 0.
|
---|
| 61 | * @return Pointer to the appropriate list.
|
---|
| 62 | */
|
---|
| 63 | static list_t * get_list(usb2_bus_t *bus, usb_address_t addr)
|
---|
| 64 | {
|
---|
| 65 | assert(bus);
|
---|
| 66 | assert(addr >= 0);
|
---|
| 67 | return &bus->devices[addr % ARRAY_SIZE(bus->devices)].endpoint_list;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[20eaa82] | 70 | /** Get speed assigned to USB address.
|
---|
| 71 | *
|
---|
| 72 | * @param[in] bus Device manager structure to use.
|
---|
| 73 | * @param[in] address Address the caller wants to find.
|
---|
| 74 | * @param[out] speed Assigned speed.
|
---|
| 75 | * @return Error code.
|
---|
| 76 | */
|
---|
| 77 | static int usb2_bus_get_speed(bus_t *bus_base, usb_address_t address, usb_speed_t *speed)
|
---|
| 78 | {
|
---|
| 79 | usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
|
---|
| 80 |
|
---|
| 81 | if (!usb_address_is_valid(address)) {
|
---|
| 82 | return EINVAL;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | const int ret = bus->devices[address].occupied ? EOK : ENOENT;
|
---|
| 86 | if (speed && bus->devices[address].occupied) {
|
---|
| 87 | *speed = bus->devices[address].speed;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | return ret;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[56db65d] | 93 | static const usb_endpoint_desc_t usb2_default_control_ep = {
|
---|
| 94 | .endpoint_no = 0,
|
---|
| 95 | .transfer_type = USB_TRANSFER_CONTROL,
|
---|
| 96 | .direction = USB_DIRECTION_BOTH,
|
---|
| 97 | .max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
|
---|
| 98 | .packets = 1,
|
---|
| 99 | };
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | static const usb_target_t usb2_default_target = {{
|
---|
| 103 | .address = USB_ADDRESS_DEFAULT,
|
---|
| 104 | .endpoint = 0,
|
---|
| 105 | }};
|
---|
| 106 |
|
---|
[20eaa82] | 107 | static int usb2_bus_address_device(bus_t *bus, hcd_t *hcd, device_t *dev)
|
---|
| 108 | {
|
---|
| 109 | int err;
|
---|
| 110 |
|
---|
| 111 | /** Reserve address early, we want pretty log messages */
|
---|
| 112 | const usb_address_t address = bus_reserve_default_address(bus, dev->speed);
|
---|
| 113 | if (address < 0) {
|
---|
| 114 | usb_log_error("Failed to reserve new address: %s.",
|
---|
| 115 | str_error(address));
|
---|
| 116 | return address;
|
---|
| 117 | }
|
---|
| 118 | usb_log_debug("Device(%d): Reserved new address.", address);
|
---|
| 119 |
|
---|
| 120 | /* Add default pipe on default address */
|
---|
| 121 | usb_log_debug("Device(%d): Adding default target (0:0)", address);
|
---|
[56db65d] | 122 | err = bus_add_ep(bus, dev, &usb2_default_control_ep);
|
---|
[20eaa82] | 123 | if (err != EOK) {
|
---|
| 124 | usb_log_error("Device(%d): Failed to add default target: %s.",
|
---|
| 125 | address, str_error(err));
|
---|
| 126 | goto err_address;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | /* Get max packet size for default pipe */
|
---|
| 130 | usb_standard_device_descriptor_t desc = { 0 };
|
---|
| 131 | const usb_device_request_setup_packet_t get_device_desc_8 =
|
---|
| 132 | GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE);
|
---|
| 133 |
|
---|
| 134 | usb_log_debug("Device(%d): Requesting first 8B of device descriptor.",
|
---|
| 135 | address);
|
---|
[56db65d] | 136 | ssize_t got = hcd_send_batch_sync(hcd, dev, usb2_default_target, USB_DIRECTION_IN,
|
---|
[327f147] | 137 | (char *) &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
|
---|
[20eaa82] | 138 | "read first 8 bytes of dev descriptor");
|
---|
| 139 |
|
---|
| 140 | if (got != CTRL_PIPE_MIN_PACKET_SIZE) {
|
---|
| 141 | err = got < 0 ? got : EOVERFLOW;
|
---|
| 142 | usb_log_error("Device(%d): Failed to get 8B of dev descr: %s.",
|
---|
| 143 | address, str_error(err));
|
---|
[56db65d] | 144 | goto err_default_control_ep;
|
---|
[20eaa82] | 145 | }
|
---|
| 146 |
|
---|
| 147 | /* Set new address */
|
---|
| 148 | const usb_device_request_setup_packet_t set_address = SET_ADDRESS(address);
|
---|
| 149 |
|
---|
| 150 | usb_log_debug("Device(%d): Setting USB address.", address);
|
---|
[56db65d] | 151 | err = hcd_send_batch_sync(hcd, dev, usb2_default_target, USB_DIRECTION_OUT,
|
---|
[20eaa82] | 152 | NULL, 0, *(uint64_t *)&set_address, "set address");
|
---|
| 153 | if (err != 0) {
|
---|
| 154 | usb_log_error("Device(%d): Failed to set new address: %s.",
|
---|
| 155 | address, str_error(got));
|
---|
[56db65d] | 156 | goto err_default_control_ep;
|
---|
[20eaa82] | 157 | }
|
---|
| 158 |
|
---|
| 159 | dev->address = address;
|
---|
| 160 |
|
---|
[56db65d] | 161 | const usb_endpoint_desc_t control_ep = {
|
---|
| 162 | .endpoint_no = 0,
|
---|
| 163 | .transfer_type = USB_TRANSFER_CONTROL,
|
---|
| 164 | .direction = USB_DIRECTION_BOTH,
|
---|
| 165 | .max_packet_size = ED_MPS_PACKET_SIZE_GET(uint16_usb2host(desc.max_packet_size)),
|
---|
| 166 | .packets = ED_MPS_TRANS_OPPORTUNITIES_GET(uint16_usb2host(desc.max_packet_size)),
|
---|
| 167 | };
|
---|
| 168 |
|
---|
[20eaa82] | 169 | /* Register EP on the new address */
|
---|
| 170 | usb_log_debug("Device(%d): Registering control EP.", address);
|
---|
[56db65d] | 171 | err = bus_add_ep(bus, dev, &control_ep);
|
---|
[20eaa82] | 172 | if (err != EOK) {
|
---|
| 173 | usb_log_error("Device(%d): Failed to register EP0: %s",
|
---|
| 174 | address, str_error(err));
|
---|
[56db65d] | 175 | goto err_default_control_ep;
|
---|
[20eaa82] | 176 | }
|
---|
| 177 |
|
---|
[56db65d] | 178 | bus_remove_ep(bus, dev, usb2_default_target, USB_DIRECTION_BOTH);
|
---|
[20eaa82] | 179 | return EOK;
|
---|
| 180 |
|
---|
[56db65d] | 181 | err_default_control_ep:
|
---|
| 182 | bus_remove_ep(bus, dev, usb2_default_target, USB_DIRECTION_BOTH);
|
---|
[20eaa82] | 183 | err_address:
|
---|
| 184 | bus_release_address(bus, address);
|
---|
| 185 | return err;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | /** Enumerate a new USB device
|
---|
| 189 | */
|
---|
| 190 | static int usb2_bus_enumerate_device(bus_t *bus, hcd_t *hcd, device_t *dev)
|
---|
| 191 | {
|
---|
| 192 | int err;
|
---|
| 193 |
|
---|
| 194 | /* The speed of the new device was reported by the hub when reserving
|
---|
| 195 | * default address.
|
---|
| 196 | */
|
---|
| 197 | if ((err = usb2_bus_get_speed(bus, USB_ADDRESS_DEFAULT, &dev->speed))) {
|
---|
| 198 | usb_log_error("Failed to verify speed: %s.", str_error(err));
|
---|
| 199 | return err;
|
---|
| 200 | }
|
---|
| 201 | usb_log_debug("Found new %s speed USB device.", usb_str_speed(dev->speed));
|
---|
| 202 |
|
---|
| 203 | /* Manage TT */
|
---|
| 204 | if (dev->hub->speed == USB_SPEED_HIGH && usb_speed_is_11(dev->speed)) {
|
---|
| 205 | /* For LS devices under HS hub */
|
---|
| 206 | /* TODO: How about SS hubs? */
|
---|
| 207 | dev->tt.address = dev->hub->address;
|
---|
| 208 | dev->tt.port = dev->port;
|
---|
| 209 | }
|
---|
| 210 | else {
|
---|
| 211 | /* Inherit hub's TT */
|
---|
| 212 | dev->tt = dev->hub->tt;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | /* Assign an address to the device */
|
---|
| 216 | if ((err = usb2_bus_address_device(bus, hcd, dev))) {
|
---|
| 217 | usb_log_error("Failed to setup address of the new device: %s", str_error(err));
|
---|
| 218 | return err;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | /* Read the device descriptor, derive the match ids */
|
---|
| 222 | if ((err = hcd_ddf_device_explore(hcd, dev))) {
|
---|
| 223 | usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
|
---|
| 224 | bus_release_address(bus, dev->address);
|
---|
| 225 | return err;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | return EOK;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[41924f30] | 231 | /** Get a free USB address
|
---|
| 232 | *
|
---|
| 233 | * @param[in] bus Device manager structure to use.
|
---|
| 234 | * @return Free address, or error code.
|
---|
| 235 | */
|
---|
| 236 | static int usb_bus_get_free_address(usb2_bus_t *bus, usb_address_t *addr)
|
---|
| 237 | {
|
---|
| 238 | usb_address_t new_address = bus->last_address;
|
---|
| 239 | do {
|
---|
| 240 | new_address = (new_address + 1) % USB_ADDRESS_COUNT;
|
---|
| 241 | if (new_address == USB_ADDRESS_DEFAULT)
|
---|
| 242 | new_address = 1;
|
---|
| 243 | if (new_address == bus->last_address)
|
---|
| 244 | return ENOSPC;
|
---|
| 245 | } while (bus->devices[new_address].occupied);
|
---|
| 246 |
|
---|
| 247 | assert(new_address != USB_ADDRESS_DEFAULT);
|
---|
| 248 | bus->last_address = new_address;
|
---|
| 249 |
|
---|
| 250 | *addr = new_address;
|
---|
| 251 | return EOK;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | /** Find endpoint.
|
---|
| 255 | * @param bus usb_bus structure, non-null.
|
---|
| 256 | * @param target Endpoint address.
|
---|
| 257 | * @param direction Communication direction.
|
---|
| 258 | * @return Pointer to endpoint_t structure representing given communication
|
---|
| 259 | * target, NULL if there is no such endpoint registered.
|
---|
| 260 | * @note Assumes that the internal mutex is locked.
|
---|
| 261 | */
|
---|
[327f147] | 262 | static endpoint_t *usb2_bus_find_ep(bus_t *bus_base, device_t *device, usb_target_t target, usb_direction_t direction)
|
---|
[41924f30] | 263 | {
|
---|
| 264 | usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
|
---|
| 265 |
|
---|
| 266 | if (!usb_address_is_valid(target.address))
|
---|
| 267 | return NULL;
|
---|
| 268 |
|
---|
| 269 | list_foreach(*get_list(bus, target.address), link, endpoint_t, ep) {
|
---|
| 270 | if (((direction == ep->direction)
|
---|
| 271 | || (ep->direction == USB_DIRECTION_BOTH)
|
---|
| 272 | || (direction == USB_DIRECTION_BOTH))
|
---|
| 273 | && (target.packed == ep->target.packed))
|
---|
| 274 | return ep;
|
---|
| 275 | }
|
---|
| 276 | return NULL;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[e6b9182] | 279 | static endpoint_t *usb2_bus_create_ep(bus_t *bus)
|
---|
| 280 | {
|
---|
| 281 | endpoint_t *ep = malloc(sizeof(endpoint_t));
|
---|
| 282 | if (!ep)
|
---|
| 283 | return NULL;
|
---|
| 284 |
|
---|
| 285 | endpoint_init(ep, bus);
|
---|
| 286 | return ep;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[41924f30] | 289 | /** Register an endpoint to the bus. Reserves bandwidth.
|
---|
| 290 | * @param bus usb_bus structure, non-null.
|
---|
| 291 | * @param endpoint USB endpoint number.
|
---|
| 292 | */
|
---|
[56db65d] | 293 | static int usb2_bus_register_ep(bus_t *bus_base, endpoint_t *ep, const usb_endpoint_desc_t *desc)
|
---|
[41924f30] | 294 | {
|
---|
| 295 | usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
|
---|
| 296 | assert(ep);
|
---|
| 297 |
|
---|
[56db65d] | 298 | assert(ep->device);
|
---|
[41924f30] | 299 |
|
---|
[56db65d] | 300 | /* Extract USB2-related information from endpoint_desc */
|
---|
| 301 | ep->target = (usb_target_t) {{
|
---|
| 302 | .address = ep->device->address,
|
---|
| 303 | .endpoint = desc->endpoint_no,
|
---|
| 304 | }};
|
---|
| 305 | ep->direction = desc->direction;
|
---|
| 306 | ep->transfer_type = desc->transfer_type;
|
---|
| 307 | ep->max_packet_size = desc->max_packet_size;
|
---|
| 308 | ep->packets = desc->packets;
|
---|
[41924f30] | 309 |
|
---|
[56db65d] | 310 | ep->bandwidth = bus_base->ops.count_bw(ep, desc->max_packet_size);
|
---|
[41924f30] | 311 |
|
---|
| 312 | /* Check for existence */
|
---|
[327f147] | 313 | if (usb2_bus_find_ep(bus_base, ep->device, ep->target, ep->direction))
|
---|
[41924f30] | 314 | return EEXIST;
|
---|
| 315 |
|
---|
| 316 | /* Check for available bandwidth */
|
---|
| 317 | if (ep->bandwidth > bus->free_bw)
|
---|
| 318 | return ENOSPC;
|
---|
| 319 |
|
---|
[56db65d] | 320 | list_append(&ep->link, get_list(bus, ep->device->address));
|
---|
[41924f30] | 321 | bus->free_bw -= ep->bandwidth;
|
---|
| 322 |
|
---|
| 323 | return EOK;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 |
|
---|
| 327 | /** Release bandwidth reserved by the given endpoint.
|
---|
| 328 | */
|
---|
[ee794529] | 329 | static int usb2_bus_unregister_ep(bus_t *bus_base, endpoint_t *ep)
|
---|
[41924f30] | 330 | {
|
---|
| 331 | usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
|
---|
| 332 | assert(ep);
|
---|
| 333 |
|
---|
| 334 | bus->free_bw += ep->bandwidth;
|
---|
| 335 |
|
---|
| 336 | return EOK;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | static int usb2_bus_reset_toggle(bus_t *bus_base, usb_target_t target, bool all)
|
---|
| 340 | {
|
---|
| 341 | usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
|
---|
| 342 |
|
---|
| 343 | if (!usb_target_is_valid(target))
|
---|
| 344 | return EINVAL;
|
---|
| 345 |
|
---|
| 346 | int ret = ENOENT;
|
---|
| 347 |
|
---|
| 348 | list_foreach(*get_list(bus, target.address), link, endpoint_t, ep) {
|
---|
| 349 | if ((ep->target.address == target.address)
|
---|
| 350 | && (all || ep->target.endpoint == target.endpoint)) {
|
---|
| 351 | endpoint_toggle_set(ep, 0);
|
---|
| 352 | ret = EOK;
|
---|
| 353 | }
|
---|
| 354 | }
|
---|
| 355 | return ret;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | /** Unregister and destroy all endpoints using given address.
|
---|
| 359 | * @param bus usb_bus structure, non-null.
|
---|
| 360 | * @param address USB address.
|
---|
| 361 | * @param endpoint USB endpoint number.
|
---|
| 362 | * @param direction Communication direction.
|
---|
| 363 | * @return Error code.
|
---|
| 364 | */
|
---|
| 365 | static int usb2_bus_release_address(bus_t *bus_base, usb_address_t address)
|
---|
| 366 | {
|
---|
| 367 | usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
|
---|
| 368 |
|
---|
| 369 | if (!usb_address_is_valid(address))
|
---|
| 370 | return EINVAL;
|
---|
| 371 |
|
---|
| 372 | const int ret = bus->devices[address].occupied ? EOK : ENOENT;
|
---|
| 373 | bus->devices[address].occupied = false;
|
---|
| 374 |
|
---|
| 375 | list_t *list = get_list(bus, address);
|
---|
| 376 | for (link_t *link = list_first(list); link != NULL; ) {
|
---|
| 377 | endpoint_t *ep = list_get_instance(link, endpoint_t, link);
|
---|
| 378 | link = list_next(link, list);
|
---|
| 379 | assert(ep->target.address == address);
|
---|
| 380 | list_remove(&ep->link);
|
---|
| 381 |
|
---|
[20eaa82] | 382 | usb_log_warning("Endpoint %d:%d %s was left behind, removing.\n",
|
---|
| 383 | ep->target.address, ep->target.endpoint, usb_str_direction(ep->direction));
|
---|
| 384 |
|
---|
[41924f30] | 385 | /* Drop bus reference */
|
---|
| 386 | endpoint_del_ref(ep);
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | return ret;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | /** Request USB address.
|
---|
| 393 | * @param bus usb_device_manager
|
---|
| 394 | * @param addr Pointer to requested address value, place to store new address
|
---|
| 395 | * @parma strict Fail if the requested address is not available.
|
---|
| 396 | * @return Error code.
|
---|
| 397 | * @note Default address is only available in strict mode.
|
---|
| 398 | */
|
---|
| 399 | static int usb2_bus_request_address(bus_t *bus_base, usb_address_t *addr, bool strict, usb_speed_t speed)
|
---|
| 400 | {
|
---|
| 401 | int err;
|
---|
| 402 |
|
---|
| 403 | usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
|
---|
| 404 | assert(addr);
|
---|
| 405 |
|
---|
| 406 | if (!usb_address_is_valid(*addr))
|
---|
| 407 | return EINVAL;
|
---|
| 408 |
|
---|
| 409 | /* Only grant default address to strict requests */
|
---|
| 410 | if ((*addr == USB_ADDRESS_DEFAULT) && !strict) {
|
---|
| 411 | if ((err = usb_bus_get_free_address(bus, addr)))
|
---|
| 412 | return err;
|
---|
| 413 | }
|
---|
| 414 | else if (bus->devices[*addr].occupied) {
|
---|
| 415 | if (strict) {
|
---|
| 416 | return ENOENT;
|
---|
| 417 | }
|
---|
| 418 | if ((err = usb_bus_get_free_address(bus, addr)))
|
---|
| 419 | return err;
|
---|
| 420 | }
|
---|
| 421 |
|
---|
| 422 | assert(usb_address_is_valid(*addr));
|
---|
| 423 | assert(bus->devices[*addr].occupied == false);
|
---|
| 424 | assert(*addr != USB_ADDRESS_DEFAULT || strict);
|
---|
| 425 |
|
---|
| 426 | bus->devices[*addr].occupied = true;
|
---|
| 427 | bus->devices[*addr].speed = speed;
|
---|
| 428 |
|
---|
| 429 | return EOK;
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 | static const bus_ops_t usb2_bus_ops = {
|
---|
[20eaa82] | 433 | .enumerate_device = usb2_bus_enumerate_device,
|
---|
[e6b9182] | 434 | .create_endpoint = usb2_bus_create_ep,
|
---|
[41924f30] | 435 | .find_endpoint = usb2_bus_find_ep,
|
---|
[ee794529] | 436 | .unregister_endpoint = usb2_bus_unregister_ep,
|
---|
[41924f30] | 437 | .register_endpoint = usb2_bus_register_ep,
|
---|
| 438 | .request_address = usb2_bus_request_address,
|
---|
| 439 | .release_address = usb2_bus_release_address,
|
---|
| 440 | .reset_toggle = usb2_bus_reset_toggle,
|
---|
| 441 | };
|
---|
| 442 |
|
---|
| 443 | /** Initialize to default state.
|
---|
| 444 | *
|
---|
| 445 | * @param bus usb_bus structure, non-null.
|
---|
| 446 | * @param available_bandwidth Size of the bandwidth pool.
|
---|
| 447 | * @param bw_count function to use to calculate endpoint bw requirements.
|
---|
| 448 | * @return Error code.
|
---|
| 449 | */
|
---|
[e6b9182] | 450 | int usb2_bus_init(usb2_bus_t *bus, size_t available_bandwidth, count_bw_func_t count_bw)
|
---|
[41924f30] | 451 | {
|
---|
| 452 | assert(bus);
|
---|
| 453 |
|
---|
[20eaa82] | 454 | bus_init(&bus->base, sizeof(device_t));
|
---|
[41924f30] | 455 |
|
---|
[fc0271a5] | 456 | bus->base.ops = usb2_bus_ops;
|
---|
| 457 | bus->base.ops.count_bw = count_bw;
|
---|
[41924f30] | 458 |
|
---|
| 459 | bus->free_bw = available_bandwidth;
|
---|
| 460 | bus->last_address = 0;
|
---|
| 461 | for (unsigned i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
|
---|
| 462 | list_initialize(&bus->devices[i].endpoint_list);
|
---|
| 463 | bus->devices[i].speed = USB_SPEED_MAX;
|
---|
| 464 | bus->devices[i].occupied = false;
|
---|
| 465 | }
|
---|
| 466 | return EOK;
|
---|
| 467 | }
|
---|
| 468 | /**
|
---|
| 469 | * @}
|
---|
| 470 | */
|
---|