[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 |
|
---|
[ff14aede] | 198 | if (!dev->hub) {
|
---|
| 199 | /* The device is the roothub */
|
---|
[8b8c164] | 200 | dev->tt = (usb_tt_address_t) {
|
---|
| 201 | .address = -1,
|
---|
| 202 | .port = 0,
|
---|
| 203 | };
|
---|
[ff14aede] | 204 | } else {
|
---|
| 205 | hcd_setup_device_tt(dev);
|
---|
[20eaa82] | 206 | }
|
---|
| 207 |
|
---|
| 208 | /* Assign an address to the device */
|
---|
[6832245] | 209 | if ((err = address_device(dev))) {
|
---|
[20eaa82] | 210 | usb_log_error("Failed to setup address of the new device: %s", str_error(err));
|
---|
| 211 | return err;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | /* Read the device descriptor, derive the match ids */
|
---|
[32fb6bce] | 215 | if ((err = hcd_device_explore(dev))) {
|
---|
[20eaa82] | 216 | usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
|
---|
[10cd715] | 217 | release_address(bus, dev->address);
|
---|
[20eaa82] | 218 | return err;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | return EOK;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[129e6f1] | 224 | static void usb2_bus_device_gone(device_t *dev)
|
---|
| 225 | {
|
---|
| 226 | // TODO: Implement me!
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[1102eca] | 229 | /**
|
---|
| 230 | * Register an endpoint to the bus. Reserves bandwidth.
|
---|
[41924f30] | 231 | */
|
---|
[6832245] | 232 | static int usb2_bus_register_ep(endpoint_t *ep)
|
---|
[41924f30] | 233 | {
|
---|
[6832245] | 234 | usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
|
---|
[53a9d02] | 235 | assert(fibril_mutex_is_locked(&ep->device->guard));
|
---|
[41924f30] | 236 | assert(ep);
|
---|
| 237 |
|
---|
| 238 | /* Check for available bandwidth */
|
---|
| 239 | if (ep->bandwidth > bus->free_bw)
|
---|
| 240 | return ENOSPC;
|
---|
| 241 |
|
---|
| 242 | bus->free_bw -= ep->bandwidth;
|
---|
| 243 |
|
---|
| 244 | return EOK;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[1102eca] | 247 | /**
|
---|
| 248 | * Release bandwidth reserved by the given endpoint.
|
---|
[41924f30] | 249 | */
|
---|
[bad4a05] | 250 | static void usb2_bus_unregister_ep(endpoint_t *ep)
|
---|
[41924f30] | 251 | {
|
---|
[6832245] | 252 | usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
|
---|
[41924f30] | 253 | assert(ep);
|
---|
| 254 |
|
---|
| 255 | bus->free_bw += ep->bandwidth;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
[6832245] | 258 | const bus_ops_t usb2_bus_ops = {
|
---|
| 259 | .device_enumerate = usb2_bus_device_enumerate,
|
---|
[129e6f1] | 260 | .device_gone = usb2_bus_device_gone,
|
---|
[66c16b0] | 261 | .endpoint_register = usb2_bus_register_ep,
|
---|
| 262 | .endpoint_unregister = usb2_bus_unregister_ep,
|
---|
[41924f30] | 263 | };
|
---|
| 264 |
|
---|
| 265 | /** Initialize to default state.
|
---|
| 266 | *
|
---|
| 267 | * @param bus usb_bus structure, non-null.
|
---|
| 268 | * @param available_bandwidth Size of the bandwidth pool.
|
---|
| 269 | */
|
---|
[5e2b1ae6] | 270 | void usb2_bus_init(usb2_bus_t *bus, size_t available_bandwidth)
|
---|
[41924f30] | 271 | {
|
---|
| 272 | assert(bus);
|
---|
| 273 |
|
---|
[32fb6bce] | 274 | bus_init(&bus->base, sizeof(device_t));
|
---|
[6832245] | 275 | bus->base.ops = &usb2_bus_ops;
|
---|
[41924f30] | 276 |
|
---|
| 277 | bus->free_bw = available_bandwidth;
|
---|
| 278 | }
|
---|
[1102eca] | 279 |
|
---|
[41924f30] | 280 | /**
|
---|
| 281 | * @}
|
---|
| 282 | */
|
---|