| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
|---|
| 3 | * Copyright (c) 2011 Jan Vesely
|
|---|
| 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 |
|
|---|
| 30 | /** @addtogroup drvusbhub
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | * Hub ports functions.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <stdbool.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <str_error.h>
|
|---|
| 40 | #include <inttypes.h>
|
|---|
| 41 | #include <fibril_synch.h>
|
|---|
| 42 | #include <usbhc_iface.h>
|
|---|
| 43 |
|
|---|
| 44 | #include <usb/debug.h>
|
|---|
| 45 |
|
|---|
| 46 | #include "port.h"
|
|---|
| 47 | #include "usbhub.h"
|
|---|
| 48 | #include "status.h"
|
|---|
| 49 |
|
|---|
| 50 | #define port_log(lvl, port, fmt, ...) do { usb_log_##lvl("(%p-%u): " fmt, (port->hub), (port->port_number), ##__VA_ARGS__); } while (0)
|
|---|
| 51 |
|
|---|
| 52 | /** Initialize hub port information.
|
|---|
| 53 | *
|
|---|
| 54 | * @param port Port to be initialized.
|
|---|
| 55 | */
|
|---|
| 56 | void usb_hub_port_init(usb_hub_port_t *port, usb_hub_dev_t *hub, unsigned int port_number)
|
|---|
| 57 | {
|
|---|
| 58 | assert(port);
|
|---|
| 59 | memset(port, 0, sizeof(*port));
|
|---|
| 60 | port->hub = hub;
|
|---|
| 61 | port->port_number = port_number;
|
|---|
| 62 | usb_port_init(&port->base);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | static inline usb_hub_port_t *get_hub_port(usb_port_t *port)
|
|---|
| 66 | {
|
|---|
| 67 | assert(port);
|
|---|
| 68 | return (usb_hub_port_t *) port;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | /**
|
|---|
| 72 | * Inform the HC that the device on port is gone.
|
|---|
| 73 | */
|
|---|
| 74 | static void remove_device(usb_port_t *port_base)
|
|---|
| 75 | {
|
|---|
| 76 | usb_hub_port_t *port = get_hub_port(port_base);
|
|---|
| 77 |
|
|---|
| 78 | async_exch_t *exch = usb_device_bus_exchange_begin(port->hub->usb_device);
|
|---|
| 79 | if (!exch) {
|
|---|
| 80 | port_log(error, port, "Cannot remove the device, failed creating exchange.");
|
|---|
| 81 | return;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | const int err = usbhc_device_remove(exch, port->port_number);
|
|---|
| 85 | if (err)
|
|---|
| 86 | port_log(error, port, "Failed to remove device: %s", str_error(err));
|
|---|
| 87 |
|
|---|
| 88 | usb_device_bus_exchange_end(exch);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /**
|
|---|
| 92 | * Routine for adding a new device.
|
|---|
| 93 | *
|
|---|
| 94 | * Separate fibril is needed because the operation blocks on waiting for
|
|---|
| 95 | * requesting default address and resetting port, and we must not block the
|
|---|
| 96 | * control pipe.
|
|---|
| 97 | */
|
|---|
| 98 | static int enumerate_device(usb_port_t *port_base)
|
|---|
| 99 | {
|
|---|
| 100 | int err;
|
|---|
| 101 | usb_hub_port_t *port = get_hub_port(port_base);
|
|---|
| 102 |
|
|---|
| 103 | port_log(debug, port, "Setting up new device.");
|
|---|
| 104 |
|
|---|
| 105 | async_exch_t *exch = usb_device_bus_exchange_begin(port->hub->usb_device);
|
|---|
| 106 | if (!exch) {
|
|---|
| 107 | port_log(error, port, "Failed to create exchange.");
|
|---|
| 108 | return ENOMEM;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | /* Reserve default address */
|
|---|
| 112 | err = usb_hub_reserve_default_address(port->hub, exch, &port->base);
|
|---|
| 113 | if (err != EOK) {
|
|---|
| 114 | port_log(error, port, "Failed to reserve default address: %s", str_error(err));
|
|---|
| 115 | goto out_exch;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /* Reservation of default address could have blocked */
|
|---|
| 119 | if (port->base.state != PORT_CONNECTING)
|
|---|
| 120 | goto out_address;
|
|---|
| 121 |
|
|---|
| 122 | port_log(debug, port, "Got default address. Resetting port.");
|
|---|
| 123 | int rc = usb_hub_set_port_feature(port->hub, port->port_number, USB_HUB_FEATURE_PORT_RESET);
|
|---|
| 124 | if (rc != EOK) {
|
|---|
| 125 | port_log(warning, port, "Port reset request failed: %s", str_error(rc));
|
|---|
| 126 | goto out_address;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | if ((err = usb_port_wait_for_enabled(&port->base))) {
|
|---|
| 130 | port_log(error, port, "Failed to reset port: %s", str_error(err));
|
|---|
| 131 | goto out_address;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | port_log(debug, port, "Port reset, enumerating device.");
|
|---|
| 135 |
|
|---|
| 136 | if ((err = usbhc_device_enumerate(exch, port->port_number, port->speed))) {
|
|---|
| 137 | port_log(error, port, "Failed to enumerate device: %s", str_error(err));
|
|---|
| 138 | /* Disable the port */
|
|---|
| 139 | usb_hub_clear_port_feature(port->hub, port->port_number, USB_HUB_FEATURE_PORT_ENABLE);
|
|---|
| 140 | goto out_address;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | port_log(debug, port, "Device enumerated");
|
|---|
| 144 |
|
|---|
| 145 | out_address:
|
|---|
| 146 | usb_hub_release_default_address(port->hub, exch);
|
|---|
| 147 | out_exch:
|
|---|
| 148 | usb_device_bus_exchange_end(exch);
|
|---|
| 149 | return err;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | static void port_changed_connection(usb_hub_port_t *port, usb_port_status_t status)
|
|---|
| 153 | {
|
|---|
| 154 | const bool connected = !!(status & USB_HUB_PORT_STATUS_CONNECTION);
|
|---|
| 155 | port_log(debug, port, "Connection change: device %s.", connected ? "attached" : "removed");
|
|---|
| 156 |
|
|---|
| 157 | if (connected) {
|
|---|
| 158 | usb_port_connected(&port->base, &enumerate_device);
|
|---|
| 159 | } else {
|
|---|
| 160 | usb_port_disabled(&port->base, &remove_device);
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | static void port_changed_enabled(usb_hub_port_t *port, usb_port_status_t status)
|
|---|
| 165 | {
|
|---|
| 166 | const bool enabled = !!(status & USB_HUB_PORT_STATUS_ENABLED);
|
|---|
| 167 | if (enabled) {
|
|---|
| 168 | port_log(warning, port, "Port unexpectedly changed to enabled.");
|
|---|
| 169 | } else {
|
|---|
| 170 | usb_port_disabled(&port->base, &remove_device);
|
|---|
| 171 | }
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | static void port_changed_suspend(usb_hub_port_t *port, usb_port_status_t status)
|
|---|
| 175 | {
|
|---|
| 176 | port_log(error, port, "Port unexpectedly suspend. Weird, we do not support suspending!");
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | static void port_changed_overcurrent(usb_hub_port_t *port, usb_port_status_t status)
|
|---|
| 180 | {
|
|---|
| 181 | const bool overcurrent = !!(status & USB_HUB_PORT_STATUS_OC);
|
|---|
| 182 |
|
|---|
| 183 | /* According to the USB specs:
|
|---|
| 184 | * 11.13.5 Over-current Reporting and Recovery
|
|---|
| 185 | * Hub device is responsible for putting port in power off
|
|---|
| 186 | * mode. USB system software is responsible for powering port
|
|---|
| 187 | * back on when the over-current condition is gone */
|
|---|
| 188 |
|
|---|
| 189 | usb_port_disabled(&port->base, &remove_device);
|
|---|
| 190 |
|
|---|
| 191 | if (!overcurrent) {
|
|---|
| 192 | const int err = usb_hub_set_port_feature(port->hub, port->port_number, USB_HUB_FEATURE_PORT_POWER);
|
|---|
| 193 | if (err)
|
|---|
| 194 | port_log(error, port, "Failed to set port power after OC: %s.", str_error(err));
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | static void port_changed_reset(usb_hub_port_t *port, usb_port_status_t status)
|
|---|
| 199 | {
|
|---|
| 200 | const bool enabled = !!(status & USB_HUB_PORT_STATUS_ENABLED);
|
|---|
| 201 |
|
|---|
| 202 | if (enabled) {
|
|---|
| 203 | // The connecting fibril do not touch speed until the port is enabled,
|
|---|
| 204 | // so we do not have to lock
|
|---|
| 205 | port->speed = usb_port_speed(status);
|
|---|
| 206 | usb_port_enabled(&port->base);
|
|---|
| 207 | } else
|
|---|
| 208 | usb_port_disabled(&port->base, &remove_device);
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | typedef void (*change_handler_t)(usb_hub_port_t *, usb_port_status_t);
|
|---|
| 212 |
|
|---|
| 213 | static const change_handler_t port_change_handlers [] = {
|
|---|
| 214 | [USB_HUB_FEATURE_C_PORT_CONNECTION] = &port_changed_connection,
|
|---|
| 215 | [USB_HUB_FEATURE_C_PORT_ENABLE] = &port_changed_enabled,
|
|---|
| 216 | [USB_HUB_FEATURE_C_PORT_SUSPEND] = &port_changed_suspend,
|
|---|
| 217 | [USB_HUB_FEATURE_C_PORT_OVER_CURRENT] = &port_changed_overcurrent,
|
|---|
| 218 | [USB_HUB_FEATURE_C_PORT_RESET] = &port_changed_reset,
|
|---|
| 219 | [sizeof(usb_port_status_t) * 8] = NULL,
|
|---|
| 220 | };
|
|---|
| 221 |
|
|---|
| 222 | /**
|
|---|
| 223 | * Process interrupts on given port
|
|---|
| 224 | *
|
|---|
| 225 | * Accepts connection, over current and port reset change.
|
|---|
| 226 | * @param port port structure
|
|---|
| 227 | * @param hub hub representation
|
|---|
| 228 | */
|
|---|
| 229 | void usb_hub_port_process_interrupt(usb_hub_port_t *port)
|
|---|
| 230 | {
|
|---|
| 231 | assert(port);
|
|---|
| 232 | port_log(debug2, port, "Interrupt.");
|
|---|
| 233 |
|
|---|
| 234 | usb_port_status_t status = 0;
|
|---|
| 235 | const int err = usb_hub_get_port_status(port->hub, port->port_number, &status);
|
|---|
| 236 | if (err != EOK) {
|
|---|
| 237 | port_log(error, port, "Failed to get port status: %s.", str_error(err));
|
|---|
| 238 | return;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | for (uint32_t feature = 0; feature < sizeof(usb_port_status_t) * 8; ++feature) {
|
|---|
| 242 | uint32_t mask = 1 << feature;
|
|---|
| 243 |
|
|---|
| 244 | if ((status & mask) == 0)
|
|---|
| 245 | continue;
|
|---|
| 246 |
|
|---|
| 247 | if (!port_change_handlers[feature])
|
|---|
| 248 | continue;
|
|---|
| 249 |
|
|---|
| 250 | /* ACK this change */
|
|---|
| 251 | status &= ~mask;
|
|---|
| 252 | usb_hub_clear_port_feature(port->hub, port->port_number, feature);
|
|---|
| 253 |
|
|---|
| 254 | port_change_handlers[feature](port, status);
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | port_log(debug2, port, "Port status after handling: %#08" PRIx32, status);
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 | /**
|
|---|
| 262 | * @}
|
|---|
| 263 | */
|
|---|