[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 <assert.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <macros.h>
|
---|
| 39 | #include <stdbool.h>
|
---|
[64fea02] | 40 | #include <stdlib.h>
|
---|
| 41 | #include <str_error.h>
|
---|
| 42 | #include <usb/debug.h>
|
---|
| 43 | #include <usb/descriptor.h>
|
---|
| 44 | #include <usb/request.h>
|
---|
| 45 | #include <usb/usb.h>
|
---|
| 46 |
|
---|
| 47 | #include "endpoint.h"
|
---|
| 48 | #include "ddf_helpers.h"
|
---|
| 49 |
|
---|
| 50 | #include "usb2_bus.h"
|
---|
[41924f30] | 51 |
|
---|
| 52 | /** Ops receive generic bus_t pointer. */
|
---|
| 53 | static inline usb2_bus_t *bus_to_usb2_bus(bus_t *bus_base)
|
---|
| 54 | {
|
---|
| 55 | assert(bus_base);
|
---|
| 56 | return (usb2_bus_t *) bus_base;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[10cd715] | 59 | /** Get a free USB address
|
---|
| 60 | *
|
---|
| 61 | * @param[in] bus Device manager structure to use.
|
---|
| 62 | * @return Free address, or error code.
|
---|
| 63 | */
|
---|
| 64 | static int get_free_address(usb2_bus_t *bus, usb_address_t *addr)
|
---|
| 65 | {
|
---|
| 66 | usb_address_t new_address = bus->last_address;
|
---|
| 67 | do {
|
---|
| 68 | new_address = (new_address + 1) % USB_ADDRESS_COUNT;
|
---|
| 69 | if (new_address == USB_ADDRESS_DEFAULT)
|
---|
| 70 | new_address = 1;
|
---|
| 71 | if (new_address == bus->last_address)
|
---|
| 72 | return ENOSPC;
|
---|
[56257ba] | 73 | } while (bus->address_occupied[new_address]);
|
---|
[10cd715] | 74 |
|
---|
| 75 | assert(new_address != USB_ADDRESS_DEFAULT);
|
---|
| 76 | bus->last_address = new_address;
|
---|
| 77 |
|
---|
| 78 | *addr = new_address;
|
---|
| 79 | return EOK;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /** Unregister and destroy all endpoints using given address.
|
---|
| 83 | * @param bus usb_bus structure, non-null.
|
---|
| 84 | * @param address USB address.
|
---|
| 85 | * @param endpoint USB endpoint number.
|
---|
| 86 | * @param direction Communication direction.
|
---|
| 87 | * @return Error code.
|
---|
| 88 | */
|
---|
| 89 | static int release_address(usb2_bus_t *bus, usb_address_t address)
|
---|
| 90 | {
|
---|
| 91 | if (!usb_address_is_valid(address))
|
---|
[20eaa82] | 92 | return EINVAL;
|
---|
| 93 |
|
---|
[56257ba] | 94 | const int ret = bus->address_occupied[address] ? EOK : ENOENT;
|
---|
| 95 | bus->address_occupied[address] = false;
|
---|
[20eaa82] | 96 | return ret;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[10cd715] | 99 | /** Request USB address.
|
---|
| 100 | * @param bus usb_device_manager
|
---|
| 101 | * @param addr Pointer to requested address value, place to store new address
|
---|
| 102 | * @return Error code.
|
---|
| 103 | * @note Default address is only available in strict mode.
|
---|
| 104 | */
|
---|
[5e2b1ae6] | 105 | static int request_address(usb2_bus_t *bus, usb_address_t *addr)
|
---|
[10cd715] | 106 | {
|
---|
| 107 | int err;
|
---|
| 108 |
|
---|
| 109 | assert(bus);
|
---|
| 110 | assert(addr);
|
---|
| 111 |
|
---|
| 112 | if (!usb_address_is_valid(*addr))
|
---|
| 113 | return EINVAL;
|
---|
| 114 |
|
---|
[5e2b1ae6] | 115 | if ((err = get_free_address(bus, addr)))
|
---|
| 116 | return err;
|
---|
[10cd715] | 117 |
|
---|
| 118 | assert(usb_address_is_valid(*addr));
|
---|
[56257ba] | 119 | assert(bus->address_occupied[*addr] == false);
|
---|
[5e2b1ae6] | 120 | assert(*addr != USB_ADDRESS_DEFAULT);
|
---|
[10cd715] | 121 |
|
---|
[56257ba] | 122 | bus->address_occupied[*addr] = true;
|
---|
[10cd715] | 123 |
|
---|
| 124 | return EOK;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[56db65d] | 127 | static const usb_target_t usb2_default_target = {{
|
---|
| 128 | .address = USB_ADDRESS_DEFAULT,
|
---|
| 129 | .endpoint = 0,
|
---|
| 130 | }};
|
---|
| 131 |
|
---|
[6832245] | 132 | static int address_device(device_t *dev)
|
---|
[20eaa82] | 133 | {
|
---|
| 134 | int err;
|
---|
| 135 |
|
---|
[6832245] | 136 | usb2_bus_t *bus = (usb2_bus_t *) dev->bus;
|
---|
| 137 |
|
---|
[58ac3ec] | 138 | /* The default address is currently reserved for this device */
|
---|
| 139 | dev->address = USB_ADDRESS_DEFAULT;
|
---|
| 140 |
|
---|
[20eaa82] | 141 | /** Reserve address early, we want pretty log messages */
|
---|
[837d53d] | 142 | usb_address_t address = USB_ADDRESS_DEFAULT;
|
---|
[5e2b1ae6] | 143 | if ((err = request_address(bus, &address))) {
|
---|
[20eaa82] | 144 | usb_log_error("Failed to reserve new address: %s.",
|
---|
[8b8c164] | 145 | str_error(err));
|
---|
| 146 | return err;
|
---|
[20eaa82] | 147 | }
|
---|
| 148 | usb_log_debug("Device(%d): Reserved new address.", address);
|
---|
| 149 |
|
---|
| 150 | /* Add default pipe on default address */
|
---|
| 151 | usb_log_debug("Device(%d): Adding default target (0:0)", address);
|
---|
[0206d35] | 152 |
|
---|
[9efad54] | 153 | usb_endpoint_descriptors_t ep0_desc = {
|
---|
| 154 | .endpoint.max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
|
---|
| 155 | };
|
---|
[0206d35] | 156 | endpoint_t *default_ep;
|
---|
[9efad54] | 157 | err = bus_endpoint_add(dev, &ep0_desc, &default_ep);
|
---|
[20eaa82] | 158 | if (err != EOK) {
|
---|
| 159 | usb_log_error("Device(%d): Failed to add default target: %s.",
|
---|
| 160 | address, str_error(err));
|
---|
| 161 | goto err_address;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[9efad54] | 164 | if ((err = hcd_get_ep0_max_packet_size(&ep0_desc.endpoint.max_packet_size, &bus->base, dev)))
|
---|
[306a36d] | 165 | goto err_address;
|
---|
[20eaa82] | 166 |
|
---|
| 167 | /* Set new address */
|
---|
| 168 | const usb_device_request_setup_packet_t set_address = SET_ADDRESS(address);
|
---|
| 169 |
|
---|
| 170 | usb_log_debug("Device(%d): Setting USB address.", address);
|
---|
[32fb6bce] | 171 | err = bus_device_send_batch_sync(dev, usb2_default_target, USB_DIRECTION_OUT,
|
---|
[20eaa82] | 172 | NULL, 0, *(uint64_t *)&set_address, "set address");
|
---|
| 173 | if (err != 0) {
|
---|
| 174 | usb_log_error("Device(%d): Failed to set new address: %s.",
|
---|
[306a36d] | 175 | address, str_error(err));
|
---|
[56db65d] | 176 | goto err_default_control_ep;
|
---|
[20eaa82] | 177 | }
|
---|
| 178 |
|
---|
[8b8c164] | 179 | /* We need to remove ep before we change the address */
|
---|
[6832245] | 180 | if ((err = bus_endpoint_remove(default_ep))) {
|
---|
[8b8c164] | 181 | usb_log_error("Device(%d): Failed to unregister default target: %s", address, str_error(err));
|
---|
| 182 | goto err_address;
|
---|
| 183 | }
|
---|
| 184 | endpoint_del_ref(default_ep);
|
---|
| 185 |
|
---|
[20eaa82] | 186 | dev->address = address;
|
---|
| 187 |
|
---|
| 188 | /* Register EP on the new address */
|
---|
| 189 | usb_log_debug("Device(%d): Registering control EP.", address);
|
---|
[9efad54] | 190 | err = bus_endpoint_add(dev, &ep0_desc, NULL);
|
---|
[20eaa82] | 191 | if (err != EOK) {
|
---|
| 192 | usb_log_error("Device(%d): Failed to register EP0: %s",
|
---|
| 193 | address, str_error(err));
|
---|
[8b8c164] | 194 | goto err_address;
|
---|
[20eaa82] | 195 | }
|
---|
| 196 |
|
---|
[deb2e55] | 197 | /* From now on, the device is officially online, yay! */
|
---|
| 198 | fibril_mutex_lock(&dev->guard);
|
---|
| 199 | dev->online = true;
|
---|
| 200 | fibril_mutex_unlock(&dev->guard);
|
---|
| 201 |
|
---|
[20eaa82] | 202 | return EOK;
|
---|
| 203 |
|
---|
[56db65d] | 204 | err_default_control_ep:
|
---|
[6832245] | 205 | bus_endpoint_remove(default_ep);
|
---|
[0206d35] | 206 | endpoint_del_ref(default_ep);
|
---|
[20eaa82] | 207 | err_address:
|
---|
[10cd715] | 208 | release_address(bus, address);
|
---|
[20eaa82] | 209 | return err;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | /** Enumerate a new USB device
|
---|
| 213 | */
|
---|
[6832245] | 214 | static int usb2_bus_device_enumerate(device_t *dev)
|
---|
[20eaa82] | 215 | {
|
---|
| 216 | int err;
|
---|
[6832245] | 217 | usb2_bus_t *bus = bus_to_usb2_bus(dev->bus);
|
---|
[20eaa82] | 218 |
|
---|
| 219 | /* The speed of the new device was reported by the hub when reserving
|
---|
| 220 | * default address.
|
---|
| 221 | */
|
---|
[5e2b1ae6] | 222 | dev->speed = bus->base.default_address_speed;
|
---|
[20eaa82] | 223 | usb_log_debug("Found new %s speed USB device.", usb_str_speed(dev->speed));
|
---|
| 224 |
|
---|
[ff14aede] | 225 | if (!dev->hub) {
|
---|
| 226 | /* The device is the roothub */
|
---|
[8b8c164] | 227 | dev->tt = (usb_tt_address_t) {
|
---|
| 228 | .address = -1,
|
---|
| 229 | .port = 0,
|
---|
| 230 | };
|
---|
[ff14aede] | 231 | } else {
|
---|
| 232 | hcd_setup_device_tt(dev);
|
---|
[20eaa82] | 233 | }
|
---|
| 234 |
|
---|
| 235 | /* Assign an address to the device */
|
---|
[6832245] | 236 | if ((err = address_device(dev))) {
|
---|
[20eaa82] | 237 | usb_log_error("Failed to setup address of the new device: %s", str_error(err));
|
---|
| 238 | return err;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | /* Read the device descriptor, derive the match ids */
|
---|
[32fb6bce] | 242 | if ((err = hcd_device_explore(dev))) {
|
---|
[20eaa82] | 243 | usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
|
---|
[10cd715] | 244 | release_address(bus, dev->address);
|
---|
[20eaa82] | 245 | return err;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | return EOK;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[9efad54] | 251 | static endpoint_t *usb2_bus_create_ep(device_t *dev, const usb_endpoint_descriptors_t *desc)
|
---|
[e6b9182] | 252 | {
|
---|
| 253 | endpoint_t *ep = malloc(sizeof(endpoint_t));
|
---|
| 254 | if (!ep)
|
---|
| 255 | return NULL;
|
---|
| 256 |
|
---|
[6832245] | 257 | endpoint_init(ep, dev, desc);
|
---|
[e6b9182] | 258 | return ep;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
[41924f30] | 261 | /** Register an endpoint to the bus. Reserves bandwidth.
|
---|
| 262 | * @param bus usb_bus structure, non-null.
|
---|
| 263 | * @param endpoint USB endpoint number.
|
---|
| 264 | */
|
---|
[6832245] | 265 | static int usb2_bus_register_ep(endpoint_t *ep)
|
---|
[41924f30] | 266 | {
|
---|
[6832245] | 267 | usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
|
---|
| 268 | assert(fibril_mutex_is_locked(&bus->base.guard));
|
---|
[41924f30] | 269 | assert(ep);
|
---|
| 270 |
|
---|
| 271 | /* Check for available bandwidth */
|
---|
| 272 | if (ep->bandwidth > bus->free_bw)
|
---|
| 273 | return ENOSPC;
|
---|
| 274 |
|
---|
| 275 | bus->free_bw -= ep->bandwidth;
|
---|
| 276 |
|
---|
| 277 | return EOK;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | /** Release bandwidth reserved by the given endpoint.
|
---|
| 281 | */
|
---|
[6832245] | 282 | static int usb2_bus_unregister_ep(endpoint_t *ep)
|
---|
[41924f30] | 283 | {
|
---|
[6832245] | 284 | usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
|
---|
[41924f30] | 285 | assert(ep);
|
---|
| 286 |
|
---|
| 287 | bus->free_bw += ep->bandwidth;
|
---|
| 288 |
|
---|
| 289 | return EOK;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
[6832245] | 292 | const bus_ops_t usb2_bus_ops = {
|
---|
| 293 | .device_enumerate = usb2_bus_device_enumerate,
|
---|
[66c16b0] | 294 | .endpoint_create = usb2_bus_create_ep,
|
---|
| 295 | .endpoint_register = usb2_bus_register_ep,
|
---|
| 296 | .endpoint_unregister = usb2_bus_unregister_ep,
|
---|
[41924f30] | 297 | };
|
---|
| 298 |
|
---|
| 299 | /** Initialize to default state.
|
---|
| 300 | *
|
---|
| 301 | * @param bus usb_bus structure, non-null.
|
---|
| 302 | * @param available_bandwidth Size of the bandwidth pool.
|
---|
| 303 | * @param bw_count function to use to calculate endpoint bw requirements.
|
---|
| 304 | * @return Error code.
|
---|
| 305 | */
|
---|
[5e2b1ae6] | 306 | void usb2_bus_init(usb2_bus_t *bus, size_t available_bandwidth)
|
---|
[41924f30] | 307 | {
|
---|
| 308 | assert(bus);
|
---|
| 309 |
|
---|
[32fb6bce] | 310 | bus_init(&bus->base, sizeof(device_t));
|
---|
[6832245] | 311 | bus->base.ops = &usb2_bus_ops;
|
---|
[41924f30] | 312 |
|
---|
| 313 | bus->free_bw = available_bandwidth;
|
---|
| 314 | }
|
---|
| 315 | /**
|
---|
| 316 | * @}
|
---|
| 317 | */
|
---|