[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
|
---|
[1102eca] | 33 | *
|
---|
| 34 | * A bus_t implementation for USB 2 and lower. Implements USB 2 enumeration and
|
---|
| 35 | * configurable bandwidth counting.
|
---|
[41924f30] | 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <assert.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <macros.h>
|
---|
| 41 | #include <stdbool.h>
|
---|
[64fea02] | 42 | #include <stdlib.h>
|
---|
| 43 | #include <str_error.h>
|
---|
| 44 | #include <usb/debug.h>
|
---|
| 45 | #include <usb/descriptor.h>
|
---|
| 46 | #include <usb/request.h>
|
---|
| 47 | #include <usb/usb.h>
|
---|
| 48 |
|
---|
| 49 | #include "endpoint.h"
|
---|
| 50 | #include "ddf_helpers.h"
|
---|
| 51 |
|
---|
| 52 | #include "usb2_bus.h"
|
---|
[41924f30] | 53 |
|
---|
[1102eca] | 54 | /**
|
---|
| 55 | * Ops receive generic bus_t pointer.
|
---|
| 56 | */
|
---|
[41924f30] | 57 | static inline usb2_bus_t *bus_to_usb2_bus(bus_t *bus_base)
|
---|
| 58 | {
|
---|
| 59 | assert(bus_base);
|
---|
| 60 | return (usb2_bus_t *) bus_base;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[1102eca] | 63 | /**
|
---|
| 64 | * Request a new address. A free address is found and marked as occupied.
|
---|
| 65 | *
|
---|
| 66 | * There's no need to synchronize this method, because it is called only with
|
---|
| 67 | * default address reserved.
|
---|
| 68 | *
|
---|
[10cd715] | 69 | * @param bus usb_device_manager
|
---|
| 70 | * @param addr Pointer to requested address value, place to store new address
|
---|
| 71 | */
|
---|
[5e2b1ae6] | 72 | static int request_address(usb2_bus_t *bus, usb_address_t *addr)
|
---|
[10cd715] | 73 | {
|
---|
[3dc3f99] | 74 | // Find a free address
|
---|
| 75 | usb_address_t new_address = bus->last_address;
|
---|
| 76 | do {
|
---|
| 77 | new_address = (new_address + 1) % USB_ADDRESS_COUNT;
|
---|
| 78 | if (new_address == USB_ADDRESS_DEFAULT)
|
---|
| 79 | new_address = 1;
|
---|
| 80 | if (new_address == bus->last_address)
|
---|
| 81 | return ENOSPC;
|
---|
| 82 | } while (bus->address_occupied[new_address]);
|
---|
| 83 | bus->last_address = new_address;
|
---|
[10cd715] | 84 |
|
---|
[3dc3f99] | 85 | *addr = new_address;
|
---|
[56257ba] | 86 | bus->address_occupied[*addr] = true;
|
---|
[10cd715] | 87 |
|
---|
| 88 | return EOK;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[1102eca] | 91 | /**
|
---|
| 92 | * Mark address as free.
|
---|
| 93 | */
|
---|
| 94 | static void release_address(usb2_bus_t *bus, usb_address_t address)
|
---|
| 95 | {
|
---|
| 96 | bus->address_occupied[address] = false;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[56db65d] | 99 | static const usb_target_t usb2_default_target = {{
|
---|
| 100 | .address = USB_ADDRESS_DEFAULT,
|
---|
| 101 | .endpoint = 0,
|
---|
| 102 | }};
|
---|
| 103 |
|
---|
[1102eca] | 104 | /**
|
---|
| 105 | * Transition the device to the addressed state.
|
---|
| 106 | *
|
---|
| 107 | * Reserve address, configure the control EP, issue a SET_ADDRESS command.
|
---|
[53a9d02] | 108 | * Configure the device with the new address,
|
---|
[1102eca] | 109 | */
|
---|
[6832245] | 110 | static int address_device(device_t *dev)
|
---|
[20eaa82] | 111 | {
|
---|
| 112 | int err;
|
---|
| 113 |
|
---|
[6832245] | 114 | usb2_bus_t *bus = (usb2_bus_t *) dev->bus;
|
---|
| 115 |
|
---|
[58ac3ec] | 116 | /* The default address is currently reserved for this device */
|
---|
| 117 | dev->address = USB_ADDRESS_DEFAULT;
|
---|
| 118 |
|
---|
[20eaa82] | 119 | /** Reserve address early, we want pretty log messages */
|
---|
[837d53d] | 120 | usb_address_t address = USB_ADDRESS_DEFAULT;
|
---|
[5e2b1ae6] | 121 | if ((err = request_address(bus, &address))) {
|
---|
[20eaa82] | 122 | usb_log_error("Failed to reserve new address: %s.",
|
---|
[8b8c164] | 123 | str_error(err));
|
---|
| 124 | return err;
|
---|
[20eaa82] | 125 | }
|
---|
| 126 | usb_log_debug("Device(%d): Reserved new address.", address);
|
---|
| 127 |
|
---|
| 128 | /* Add default pipe on default address */
|
---|
| 129 | usb_log_debug("Device(%d): Adding default target (0:0)", address);
|
---|
[0206d35] | 130 |
|
---|
[9efad54] | 131 | usb_endpoint_descriptors_t ep0_desc = {
|
---|
| 132 | .endpoint.max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
|
---|
| 133 | };
|
---|
[0206d35] | 134 | endpoint_t *default_ep;
|
---|
[9efad54] | 135 | err = bus_endpoint_add(dev, &ep0_desc, &default_ep);
|
---|
[20eaa82] | 136 | if (err != EOK) {
|
---|
| 137 | usb_log_error("Device(%d): Failed to add default target: %s.",
|
---|
| 138 | address, str_error(err));
|
---|
| 139 | goto err_address;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[9efad54] | 142 | if ((err = hcd_get_ep0_max_packet_size(&ep0_desc.endpoint.max_packet_size, &bus->base, dev)))
|
---|
[306a36d] | 143 | goto err_address;
|
---|
[20eaa82] | 144 |
|
---|
| 145 | /* Set new address */
|
---|
| 146 | const usb_device_request_setup_packet_t set_address = SET_ADDRESS(address);
|
---|
| 147 |
|
---|
| 148 | usb_log_debug("Device(%d): Setting USB address.", address);
|
---|
[32fb6bce] | 149 | err = bus_device_send_batch_sync(dev, usb2_default_target, USB_DIRECTION_OUT,
|
---|
[20eaa82] | 150 | NULL, 0, *(uint64_t *)&set_address, "set address");
|
---|
| 151 | if (err != 0) {
|
---|
| 152 | usb_log_error("Device(%d): Failed to set new address: %s.",
|
---|
[306a36d] | 153 | address, str_error(err));
|
---|
[56db65d] | 154 | goto err_default_control_ep;
|
---|
[20eaa82] | 155 | }
|
---|
| 156 |
|
---|
[8b8c164] | 157 | /* We need to remove ep before we change the address */
|
---|
[6832245] | 158 | if ((err = bus_endpoint_remove(default_ep))) {
|
---|
[8b8c164] | 159 | usb_log_error("Device(%d): Failed to unregister default target: %s", address, str_error(err));
|
---|
| 160 | goto err_address;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[20eaa82] | 163 | dev->address = address;
|
---|
| 164 |
|
---|
| 165 | /* Register EP on the new address */
|
---|
| 166 | usb_log_debug("Device(%d): Registering control EP.", address);
|
---|
[9efad54] | 167 | err = bus_endpoint_add(dev, &ep0_desc, NULL);
|
---|
[20eaa82] | 168 | if (err != EOK) {
|
---|
| 169 | usb_log_error("Device(%d): Failed to register EP0: %s",
|
---|
| 170 | address, str_error(err));
|
---|
[8b8c164] | 171 | goto err_address;
|
---|
[20eaa82] | 172 | }
|
---|
| 173 |
|
---|
| 174 | return EOK;
|
---|
| 175 |
|
---|
[56db65d] | 176 | err_default_control_ep:
|
---|
[6832245] | 177 | bus_endpoint_remove(default_ep);
|
---|
[20eaa82] | 178 | err_address:
|
---|
[10cd715] | 179 | release_address(bus, address);
|
---|
[20eaa82] | 180 | return err;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[1102eca] | 183 | /**
|
---|
| 184 | * Enumerate a USB device. Move it to the addressed state, then explore it
|
---|
| 185 | * to create a DDF function node with proper characteristics.
|
---|
[20eaa82] | 186 | */
|
---|
[6832245] | 187 | static int usb2_bus_device_enumerate(device_t *dev)
|
---|
[20eaa82] | 188 | {
|
---|
| 189 | int err;
|
---|
[6832245] | 190 | usb2_bus_t *bus = bus_to_usb2_bus(dev->bus);
|
---|
[20eaa82] | 191 |
|
---|
| 192 | /* The speed of the new device was reported by the hub when reserving
|
---|
| 193 | * default address.
|
---|
| 194 | */
|
---|
[5e2b1ae6] | 195 | dev->speed = bus->base.default_address_speed;
|
---|
[20eaa82] | 196 | usb_log_debug("Found new %s speed USB device.", usb_str_speed(dev->speed));
|
---|
| 197 |
|
---|
| 198 | /* Assign an address to the device */
|
---|
[6832245] | 199 | if ((err = address_device(dev))) {
|
---|
[20eaa82] | 200 | usb_log_error("Failed to setup address of the new device: %s", str_error(err));
|
---|
| 201 | return err;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | /* Read the device descriptor, derive the match ids */
|
---|
[32fb6bce] | 205 | if ((err = hcd_device_explore(dev))) {
|
---|
[20eaa82] | 206 | usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
|
---|
[10cd715] | 207 | release_address(bus, dev->address);
|
---|
[20eaa82] | 208 | return err;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | return EOK;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[129e6f1] | 214 | static void usb2_bus_device_gone(device_t *dev)
|
---|
| 215 | {
|
---|
| 216 | // TODO: Implement me!
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[1102eca] | 219 | /**
|
---|
| 220 | * Register an endpoint to the bus. Reserves bandwidth.
|
---|
[41924f30] | 221 | */
|
---|
[6832245] | 222 | static int usb2_bus_register_ep(endpoint_t *ep)
|
---|
[41924f30] | 223 | {
|
---|
[6832245] | 224 | usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
|
---|
[53a9d02] | 225 | assert(fibril_mutex_is_locked(&ep->device->guard));
|
---|
[41924f30] | 226 | assert(ep);
|
---|
| 227 |
|
---|
| 228 | /* Check for available bandwidth */
|
---|
| 229 | if (ep->bandwidth > bus->free_bw)
|
---|
| 230 | return ENOSPC;
|
---|
| 231 |
|
---|
| 232 | bus->free_bw -= ep->bandwidth;
|
---|
| 233 |
|
---|
| 234 | return EOK;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[1102eca] | 237 | /**
|
---|
| 238 | * Release bandwidth reserved by the given endpoint.
|
---|
[41924f30] | 239 | */
|
---|
[bad4a05] | 240 | static void usb2_bus_unregister_ep(endpoint_t *ep)
|
---|
[41924f30] | 241 | {
|
---|
[6832245] | 242 | usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
|
---|
[41924f30] | 243 | assert(ep);
|
---|
| 244 |
|
---|
| 245 | bus->free_bw += ep->bandwidth;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[6832245] | 248 | const bus_ops_t usb2_bus_ops = {
|
---|
| 249 | .device_enumerate = usb2_bus_device_enumerate,
|
---|
[129e6f1] | 250 | .device_gone = usb2_bus_device_gone,
|
---|
[66c16b0] | 251 | .endpoint_register = usb2_bus_register_ep,
|
---|
| 252 | .endpoint_unregister = usb2_bus_unregister_ep,
|
---|
[41924f30] | 253 | };
|
---|
| 254 |
|
---|
| 255 | /** Initialize to default state.
|
---|
| 256 | *
|
---|
| 257 | * @param bus usb_bus structure, non-null.
|
---|
| 258 | * @param available_bandwidth Size of the bandwidth pool.
|
---|
| 259 | */
|
---|
[5e2b1ae6] | 260 | void usb2_bus_init(usb2_bus_t *bus, size_t available_bandwidth)
|
---|
[41924f30] | 261 | {
|
---|
| 262 | assert(bus);
|
---|
| 263 |
|
---|
[32fb6bce] | 264 | bus_init(&bus->base, sizeof(device_t));
|
---|
[6832245] | 265 | bus->base.ops = &usb2_bus_ops;
|
---|
[41924f30] | 266 |
|
---|
| 267 | bus->free_bw = available_bandwidth;
|
---|
[c1a966e] | 268 |
|
---|
| 269 | /*
|
---|
| 270 | * The first address allocated is for the roothub. This way, its
|
---|
| 271 | * address will be 127, and the first connected USB device will have
|
---|
| 272 | * address 1.
|
---|
| 273 | */
|
---|
| 274 | bus->last_address = USB_ADDRESS_COUNT - 2;
|
---|
[41924f30] | 275 | }
|
---|
[1102eca] | 276 |
|
---|
[41924f30] | 277 | /**
|
---|
| 278 | * @}
|
---|
| 279 | */
|
---|