| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Matus Dekanek
|
|---|
| 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 | /** @addtogroup drvusbhub
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief usb hub main functionality
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <ddf/driver.h>
|
|---|
| 37 | #include <bool.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <str_error.h>
|
|---|
| 40 | #include <inttypes.h>
|
|---|
| 41 | #include <stdio.h>
|
|---|
| 42 |
|
|---|
| 43 | #include <usb/usb.h>
|
|---|
| 44 | #include <usb/debug.h>
|
|---|
| 45 | #include <usb/dev/pipes.h>
|
|---|
| 46 | #include <usb/classes/classes.h>
|
|---|
| 47 | #include <usb/ddfiface.h>
|
|---|
| 48 | #include <usb/descriptor.h>
|
|---|
| 49 | #include <usb/dev/recognise.h>
|
|---|
| 50 | #include <usb/dev/request.h>
|
|---|
| 51 | #include <usb/classes/hub.h>
|
|---|
| 52 | #include <usb/dev/poll.h>
|
|---|
| 53 | #include <usb_iface.h>
|
|---|
| 54 |
|
|---|
| 55 | #include "usbhub.h"
|
|---|
| 56 | #include "status.h"
|
|---|
| 57 |
|
|---|
| 58 | #define HUB_FNC_NAME "hub"
|
|---|
| 59 |
|
|---|
| 60 | /** Standard get hub global status request */
|
|---|
| 61 | static const usb_device_request_setup_packet_t get_hub_status_request = {
|
|---|
| 62 | .request_type = USB_HUB_REQ_TYPE_GET_HUB_STATUS,
|
|---|
| 63 | .request = USB_HUB_REQUEST_GET_STATUS,
|
|---|
| 64 | .index = 0,
|
|---|
| 65 | .value = 0,
|
|---|
| 66 | .length = sizeof(usb_hub_status_t),
|
|---|
| 67 | };
|
|---|
| 68 |
|
|---|
| 69 | static int usb_set_first_configuration(usb_device_t *usb_device);
|
|---|
| 70 | static int usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev);
|
|---|
| 71 | static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
|
|---|
| 72 | usb_hub_status_t status);
|
|---|
| 73 | static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev);
|
|---|
| 74 | static void usb_hub_polling_terminated_callback(usb_device_t *device,
|
|---|
| 75 | bool was_error, void *data);
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * Initialize hub device driver structure.
|
|---|
| 79 | *
|
|---|
| 80 | * Creates hub representation and fibril that periodically checks hub's status.
|
|---|
| 81 | * Hub representation is passed to the fibril.
|
|---|
| 82 | * @param usb_dev generic usb device information
|
|---|
| 83 | * @return error code
|
|---|
| 84 | */
|
|---|
| 85 | int usb_hub_device_add(usb_device_t *usb_dev)
|
|---|
| 86 | {
|
|---|
| 87 | assert(usb_dev);
|
|---|
| 88 | /* Create driver soft-state structure */
|
|---|
| 89 | usb_hub_dev_t *hub_dev =
|
|---|
| 90 | usb_device_data_alloc(usb_dev, sizeof(usb_hub_dev_t));
|
|---|
| 91 | if (hub_dev == NULL) {
|
|---|
| 92 | usb_log_error("Failed to create hub driver structure.\n");
|
|---|
| 93 | return ENOMEM;
|
|---|
| 94 | }
|
|---|
| 95 | hub_dev->usb_device = usb_dev;
|
|---|
| 96 | hub_dev->pending_ops_count = 0;
|
|---|
| 97 | hub_dev->running = false;
|
|---|
| 98 | fibril_mutex_initialize(&hub_dev->pending_ops_mutex);
|
|---|
| 99 | fibril_condvar_initialize(&hub_dev->pending_ops_cv);
|
|---|
| 100 |
|
|---|
| 101 | /* Create hc connection */
|
|---|
| 102 | usb_log_debug("Initializing USB wire abstraction.\n");
|
|---|
| 103 | int opResult = usb_hc_connection_initialize_from_device(
|
|---|
| 104 | &hub_dev->connection, hub_dev->usb_device->ddf_dev);
|
|---|
| 105 | if (opResult != EOK) {
|
|---|
| 106 | usb_log_error("Could not initialize connection to device: %s\n",
|
|---|
| 107 | str_error(opResult));
|
|---|
| 108 | return opResult;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | /* Set hub's first configuration. (There should be only one) */
|
|---|
| 112 | opResult = usb_set_first_configuration(usb_dev);
|
|---|
| 113 | if (opResult != EOK) {
|
|---|
| 114 | usb_log_error("Could not set hub configuration: %s\n",
|
|---|
| 115 | str_error(opResult));
|
|---|
| 116 | return opResult;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | /* Get port count and create attached_devices. */
|
|---|
| 120 | opResult = usb_hub_process_hub_specific_info(hub_dev);
|
|---|
| 121 | if (opResult != EOK) {
|
|---|
| 122 | usb_log_error("Could process hub specific info, %s\n",
|
|---|
| 123 | str_error(opResult));
|
|---|
| 124 | return opResult;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /* Create hub control function. */
|
|---|
| 128 | usb_log_debug("Creating DDF function '" HUB_FNC_NAME "'.\n");
|
|---|
| 129 | hub_dev->hub_fun = ddf_fun_create(hub_dev->usb_device->ddf_dev,
|
|---|
| 130 | fun_exposed, HUB_FNC_NAME);
|
|---|
| 131 | if (hub_dev->hub_fun == NULL) {
|
|---|
| 132 | usb_log_error("Failed to create hub function.\n");
|
|---|
| 133 | return ENOMEM;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | /* Bind hub control function. */
|
|---|
| 137 | opResult = ddf_fun_bind(hub_dev->hub_fun);
|
|---|
| 138 | if (opResult != EOK) {
|
|---|
| 139 | usb_log_error("Failed to bind hub function: %s.\n",
|
|---|
| 140 | str_error(opResult));
|
|---|
| 141 | ddf_fun_destroy(hub_dev->hub_fun);
|
|---|
| 142 | return opResult;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /* Start hub operation. */
|
|---|
| 146 | opResult = usb_device_auto_poll(hub_dev->usb_device, 0,
|
|---|
| 147 | hub_port_changes_callback, ((hub_dev->port_count + 1 + 8) / 8),
|
|---|
| 148 | usb_hub_polling_terminated_callback, hub_dev);
|
|---|
| 149 | if (opResult != EOK) {
|
|---|
| 150 | /* Function is already bound */
|
|---|
| 151 | ddf_fun_unbind(hub_dev->hub_fun);
|
|---|
| 152 | ddf_fun_destroy(hub_dev->hub_fun);
|
|---|
| 153 | usb_log_error("Failed to create polling fibril: %s.\n",
|
|---|
| 154 | str_error(opResult));
|
|---|
| 155 | return opResult;
|
|---|
| 156 | }
|
|---|
| 157 | hub_dev->running = true;
|
|---|
| 158 | usb_log_info("Controlling hub '%s' (%zu ports).\n",
|
|---|
| 159 | hub_dev->usb_device->ddf_dev->name, hub_dev->port_count);
|
|---|
| 160 |
|
|---|
| 161 | return EOK;
|
|---|
| 162 | }
|
|---|
| 163 | /*----------------------------------------------------------------------------*/
|
|---|
| 164 | /**
|
|---|
| 165 | * Turn off power to all ports.
|
|---|
| 166 | *
|
|---|
| 167 | * @param usb_dev generic usb device information
|
|---|
| 168 | * @return error code
|
|---|
| 169 | */
|
|---|
| 170 | int usb_hub_device_remove(usb_device_t *usb_dev)
|
|---|
| 171 | {
|
|---|
| 172 | return ENOTSUP;
|
|---|
| 173 | }
|
|---|
| 174 | /*----------------------------------------------------------------------------*/
|
|---|
| 175 | /**
|
|---|
| 176 | * Remove all attached devices
|
|---|
| 177 | * @param usb_dev generic usb device information
|
|---|
| 178 | * @return error code
|
|---|
| 179 | */
|
|---|
| 180 | int usb_hub_device_gone(usb_device_t *usb_dev)
|
|---|
| 181 | {
|
|---|
| 182 | assert(usb_dev);
|
|---|
| 183 | usb_hub_dev_t *hub = usb_dev->driver_data;
|
|---|
| 184 | assert(hub);
|
|---|
| 185 | unsigned tries = 10;
|
|---|
| 186 | while (hub->running) {
|
|---|
| 187 | async_usleep(100000);
|
|---|
| 188 | if (!tries--) {
|
|---|
| 189 | usb_log_error("Can't remove hub, still running.\n");
|
|---|
| 190 | return EINPROGRESS;
|
|---|
| 191 | }
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | assert(!hub->running);
|
|---|
| 195 |
|
|---|
| 196 | for (size_t port = 0; port < hub->port_count; ++port) {
|
|---|
| 197 | if (hub->ports[port].attached_device.fun) {
|
|---|
| 198 | const int ret =
|
|---|
| 199 | usb_hub_port_fini(&hub->ports[port], hub);
|
|---|
| 200 | if (ret != EOK)
|
|---|
| 201 | return ret;
|
|---|
| 202 | }
|
|---|
| 203 | }
|
|---|
| 204 | free(hub->ports);
|
|---|
| 205 |
|
|---|
| 206 | const int ret = ddf_fun_unbind(hub->hub_fun);
|
|---|
| 207 | if (ret != EOK) {
|
|---|
| 208 | usb_log_error("Failed to unbind '%s' function: %s.\n",
|
|---|
| 209 | HUB_FNC_NAME, str_error(ret));
|
|---|
| 210 | return ret;
|
|---|
| 211 | }
|
|---|
| 212 | ddf_fun_destroy(hub->hub_fun);
|
|---|
| 213 |
|
|---|
| 214 | usb_log_info("USB hub driver, stopped and cleaned.\n");
|
|---|
| 215 | return EOK;
|
|---|
| 216 | }
|
|---|
| 217 | /*----------------------------------------------------------------------------*/
|
|---|
| 218 | /** Callback for polling hub for changes.
|
|---|
| 219 | *
|
|---|
| 220 | * @param dev Device where the change occured.
|
|---|
| 221 | * @param change_bitmap Bitmap of changed ports.
|
|---|
| 222 | * @param change_bitmap_size Size of the bitmap in bytes.
|
|---|
| 223 | * @param arg Custom argument, points to @c usb_hub_dev_t.
|
|---|
| 224 | * @return Whether to continue polling.
|
|---|
| 225 | */
|
|---|
| 226 | bool hub_port_changes_callback(usb_device_t *dev,
|
|---|
| 227 | uint8_t *change_bitmap, size_t change_bitmap_size, void *arg)
|
|---|
| 228 | {
|
|---|
| 229 | usb_log_debug("hub_port_changes_callback\n");
|
|---|
| 230 | usb_hub_dev_t *hub = arg;
|
|---|
| 231 | assert(hub);
|
|---|
| 232 |
|
|---|
| 233 | /* It is an error condition if we didn't receive enough data */
|
|---|
| 234 | if (change_bitmap_size == 0) {
|
|---|
| 235 | return false;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | /* Lowest bit indicates global change */
|
|---|
| 239 | const bool change = change_bitmap[0] & 1;
|
|---|
| 240 | if (change) {
|
|---|
| 241 | usb_hub_global_interrupt(hub);
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | /* N + 1 bit indicates change on port N */
|
|---|
| 245 | for (size_t port = 0; port < hub->port_count + 1; port++) {
|
|---|
| 246 | const size_t bit = port + 1;
|
|---|
| 247 | const bool change = (change_bitmap[bit / 8] >> (bit % 8)) & 1;
|
|---|
| 248 | if (change) {
|
|---|
| 249 | usb_hub_port_process_interrupt(&hub->ports[port], hub);
|
|---|
| 250 | }
|
|---|
| 251 | }
|
|---|
| 252 | return true;
|
|---|
| 253 | }
|
|---|
| 254 | /*----------------------------------------------------------------------------*/
|
|---|
| 255 | /**
|
|---|
| 256 | * Load hub-specific information into hub_dev structure and process if needed
|
|---|
| 257 | *
|
|---|
| 258 | * Read port count and initialize structures holding per port information.
|
|---|
| 259 | * If there are any non-removable devices, start initializing them.
|
|---|
| 260 | * This function is hub-specific and should be run only after the hub is
|
|---|
| 261 | * configured using usb_set_first_configuration function.
|
|---|
| 262 | * @param hub_dev hub representation
|
|---|
| 263 | * @return error code
|
|---|
| 264 | */
|
|---|
| 265 | static int usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev)
|
|---|
| 266 | {
|
|---|
| 267 | assert(hub_dev);
|
|---|
| 268 |
|
|---|
| 269 | /* Get hub descriptor. */
|
|---|
| 270 | usb_log_debug("Retrieving descriptor\n");
|
|---|
| 271 | usb_pipe_t *control_pipe = &hub_dev->usb_device->ctrl_pipe;
|
|---|
| 272 |
|
|---|
| 273 | usb_hub_descriptor_header_t descriptor;
|
|---|
| 274 | size_t received_size;
|
|---|
| 275 | int opResult = usb_request_get_descriptor(control_pipe,
|
|---|
| 276 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
|
|---|
| 277 | USB_DESCTYPE_HUB, 0, 0, &descriptor,
|
|---|
| 278 | sizeof(usb_hub_descriptor_header_t), &received_size);
|
|---|
| 279 | if (opResult != EOK) {
|
|---|
| 280 | usb_log_error("Failed to receive hub descriptor: %s.\n",
|
|---|
| 281 | str_error(opResult));
|
|---|
| 282 | return opResult;
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | usb_log_debug("Setting port count to %d.\n", descriptor.port_count);
|
|---|
| 286 | hub_dev->port_count = descriptor.port_count;
|
|---|
| 287 |
|
|---|
| 288 | hub_dev->ports = calloc(hub_dev->port_count, sizeof(usb_hub_port_t));
|
|---|
| 289 | if (!hub_dev->ports) {
|
|---|
| 290 | return ENOMEM;
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | for (size_t port = 0; port < hub_dev->port_count; ++port) {
|
|---|
| 294 | usb_hub_port_init(
|
|---|
| 295 | &hub_dev->ports[port], port + 1, control_pipe);
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | hub_dev->power_switched =
|
|---|
| 299 | !(descriptor.characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
|
|---|
| 300 | hub_dev->per_port_power =
|
|---|
| 301 | descriptor.characteristics & HUB_CHAR_POWER_PER_PORT_FLAG;
|
|---|
| 302 |
|
|---|
| 303 | if (!hub_dev->power_switched) {
|
|---|
| 304 | usb_log_info(
|
|---|
| 305 | "Power switching not supported, ports always powered.\n");
|
|---|
| 306 | return EOK;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | usb_log_info("Hub port power switching enabled.\n");
|
|---|
| 310 |
|
|---|
| 311 | for (size_t port = 0; port < hub_dev->port_count; ++port) {
|
|---|
| 312 | usb_log_debug("Powering port %zu.\n", port);
|
|---|
| 313 | const int ret = usb_hub_port_set_feature(
|
|---|
| 314 | &hub_dev->ports[port], USB_HUB_FEATURE_PORT_POWER);
|
|---|
| 315 |
|
|---|
| 316 | if (ret != EOK) {
|
|---|
| 317 | usb_log_error("Cannot power on port %zu: %s.\n",
|
|---|
| 318 | hub_dev->ports[port].port_number, str_error(ret));
|
|---|
| 319 | } else {
|
|---|
| 320 | if (!hub_dev->per_port_power) {
|
|---|
| 321 | usb_log_debug("Ganged power switching, "
|
|---|
| 322 | "one port is enough.\n");
|
|---|
| 323 | break;
|
|---|
| 324 | }
|
|---|
| 325 | }
|
|---|
| 326 | }
|
|---|
| 327 | return EOK;
|
|---|
| 328 | }
|
|---|
| 329 | /*----------------------------------------------------------------------------*/
|
|---|
| 330 | /**
|
|---|
| 331 | * Set configuration of and USB device
|
|---|
| 332 | *
|
|---|
| 333 | * Check whether there is at least one configuration and sets the first one.
|
|---|
| 334 | * This function should be run prior to running any hub-specific action.
|
|---|
| 335 | * @param usb_device usb device representation
|
|---|
| 336 | * @return error code
|
|---|
| 337 | */
|
|---|
| 338 | static int usb_set_first_configuration(usb_device_t *usb_device)
|
|---|
| 339 | {
|
|---|
| 340 | assert(usb_device);
|
|---|
| 341 | /* Get number of possible configurations from device descriptor */
|
|---|
| 342 | const size_t configuration_count =
|
|---|
| 343 | usb_device->descriptors.device.configuration_count;
|
|---|
| 344 | usb_log_debug("Hub has %zu configurations.\n", configuration_count);
|
|---|
| 345 |
|
|---|
| 346 | if (configuration_count < 1) {
|
|---|
| 347 | usb_log_error("There are no configurations available\n");
|
|---|
| 348 | return EINVAL;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | if (usb_device->descriptors.configuration_size
|
|---|
| 352 | < sizeof(usb_standard_configuration_descriptor_t)) {
|
|---|
| 353 | usb_log_error("Configuration descriptor is not big enough"
|
|---|
| 354 | " to fit standard configuration descriptor.\n");
|
|---|
| 355 | return EOVERFLOW;
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | // TODO: Make sure that the cast is correct
|
|---|
| 359 | usb_standard_configuration_descriptor_t *config_descriptor
|
|---|
| 360 | = (usb_standard_configuration_descriptor_t *)
|
|---|
| 361 | usb_device->descriptors.configuration;
|
|---|
| 362 |
|
|---|
| 363 | /* Set configuration. Use the configuration that was in
|
|---|
| 364 | * usb_device->descriptors.configuration i.e. The first one. */
|
|---|
| 365 | const int opResult = usb_request_set_configuration(
|
|---|
| 366 | &usb_device->ctrl_pipe, config_descriptor->configuration_number);
|
|---|
| 367 | if (opResult != EOK) {
|
|---|
| 368 | usb_log_error("Failed to set hub configuration: %s.\n",
|
|---|
| 369 | str_error(opResult));
|
|---|
| 370 | } else {
|
|---|
| 371 | usb_log_debug("\tUsed configuration %d\n",
|
|---|
| 372 | config_descriptor->configuration_number);
|
|---|
| 373 | }
|
|---|
| 374 | return opResult;
|
|---|
| 375 | }
|
|---|
| 376 | /*----------------------------------------------------------------------------*/
|
|---|
| 377 | /**
|
|---|
| 378 | * Process hub over current change
|
|---|
| 379 | *
|
|---|
| 380 | * This means either to power off the hub or power it on.
|
|---|
| 381 | * @param hub_dev hub instance
|
|---|
| 382 | * @param status hub status bitmask
|
|---|
| 383 | * @return error code
|
|---|
| 384 | */
|
|---|
| 385 | static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
|
|---|
| 386 | usb_hub_status_t status)
|
|---|
| 387 | {
|
|---|
| 388 | if (status & USB_HUB_STATUS_OVER_CURRENT) {
|
|---|
| 389 | /* Hub should remove power from all ports if it detects OC */
|
|---|
| 390 | usb_log_warning("Detected hub over-current condition, "
|
|---|
| 391 | "all ports should be powered off.");
|
|---|
| 392 | return;
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | /* Ports are always powered. */
|
|---|
| 396 | if (!hub_dev->power_switched)
|
|---|
| 397 | return;
|
|---|
| 398 |
|
|---|
| 399 | /* Over-current condition is gone, it is safe to turn the ports on. */
|
|---|
| 400 | for (size_t port = 0; port < hub_dev->port_count; ++port) {
|
|---|
| 401 | const int ret = usb_hub_port_set_feature(
|
|---|
| 402 | &hub_dev->ports[port], USB_HUB_FEATURE_PORT_POWER);
|
|---|
| 403 | if (ret != EOK) {
|
|---|
| 404 | usb_log_warning("HUB OVER-CURRENT GONE: Cannot power on"
|
|---|
| 405 | " port %zu: %s\n", hub_dev->ports[port].port_number,
|
|---|
| 406 | str_error(ret));
|
|---|
| 407 | } else {
|
|---|
| 408 | if (!hub_dev->per_port_power)
|
|---|
| 409 | return;
|
|---|
| 410 | }
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | }
|
|---|
| 414 | /*----------------------------------------------------------------------------*/
|
|---|
| 415 | /**
|
|---|
| 416 | * Process hub interrupts.
|
|---|
| 417 | *
|
|---|
| 418 | * The change can be either in the over-current condition or local-power change.
|
|---|
| 419 | * @param hub_dev hub instance
|
|---|
| 420 | */
|
|---|
| 421 | static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev)
|
|---|
| 422 | {
|
|---|
| 423 | assert(hub_dev);
|
|---|
| 424 | assert(hub_dev->usb_device);
|
|---|
| 425 | usb_log_debug("Global interrupt on a hub\n");
|
|---|
| 426 | usb_pipe_t *control_pipe = &hub_dev->usb_device->ctrl_pipe;
|
|---|
| 427 |
|
|---|
| 428 | usb_hub_status_t status;
|
|---|
| 429 | size_t rcvd_size;
|
|---|
| 430 | /* NOTE: We can't use standard USB GET_STATUS request, because
|
|---|
| 431 | * hubs reply is 4byte instead of 2 */
|
|---|
| 432 | const int opResult = usb_pipe_control_read(control_pipe,
|
|---|
| 433 | &get_hub_status_request, sizeof(get_hub_status_request),
|
|---|
| 434 | &status, sizeof(usb_hub_status_t), &rcvd_size);
|
|---|
| 435 | if (opResult != EOK) {
|
|---|
| 436 | usb_log_error("Could not get hub status: %s\n",
|
|---|
| 437 | str_error(opResult));
|
|---|
| 438 | return;
|
|---|
| 439 | }
|
|---|
| 440 | if (rcvd_size != sizeof(usb_hub_status_t)) {
|
|---|
| 441 | usb_log_error("Received status has incorrect size\n");
|
|---|
| 442 | return;
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 | /* Handle status changes */
|
|---|
| 446 | if (status & USB_HUB_STATUS_C_OVER_CURRENT) {
|
|---|
| 447 | usb_hub_over_current(hub_dev, status);
|
|---|
| 448 | /* Ack change in hub OC flag */
|
|---|
| 449 | const int ret = usb_request_clear_feature(
|
|---|
| 450 | &hub_dev->usb_device->ctrl_pipe, USB_REQUEST_TYPE_CLASS,
|
|---|
| 451 | USB_REQUEST_RECIPIENT_DEVICE,
|
|---|
| 452 | USB_HUB_FEATURE_C_HUB_OVER_CURRENT, 0);
|
|---|
| 453 | if (ret != EOK) {
|
|---|
| 454 | usb_log_error("Failed to clear hub over-current "
|
|---|
| 455 | "change flag: %s.\n", str_error(opResult));
|
|---|
| 456 | }
|
|---|
| 457 | }
|
|---|
| 458 |
|
|---|
| 459 | if (status & USB_HUB_STATUS_C_LOCAL_POWER) {
|
|---|
| 460 | /* NOTE: Handling this is more complicated.
|
|---|
| 461 | * If the transition is from bus power to local power, all
|
|---|
| 462 | * is good and we may signal the parent hub that we don't
|
|---|
| 463 | * need the power.
|
|---|
| 464 | * If the transition is from local power to bus power
|
|---|
| 465 | * the hub should turn off all the ports and devices need
|
|---|
| 466 | * to be reinitialized taking into account the limited power
|
|---|
| 467 | * that is now available.
|
|---|
| 468 | * There is no support for power distribution in HelenOS,
|
|---|
| 469 | * (or other OSes/hub devices that I've seen) so this is not
|
|---|
| 470 | * implemented.
|
|---|
| 471 | * Just ACK the change.
|
|---|
| 472 | */
|
|---|
| 473 | const int ret = usb_request_clear_feature(
|
|---|
| 474 | control_pipe, USB_REQUEST_TYPE_CLASS,
|
|---|
| 475 | USB_REQUEST_RECIPIENT_DEVICE,
|
|---|
| 476 | USB_HUB_FEATURE_C_HUB_LOCAL_POWER, 0);
|
|---|
| 477 | if (opResult != EOK) {
|
|---|
| 478 | usb_log_error("Failed to clear hub power change "
|
|---|
| 479 | "flag: %s.\n", str_error(ret));
|
|---|
| 480 | }
|
|---|
| 481 | }
|
|---|
| 482 | }
|
|---|
| 483 | /*----------------------------------------------------------------------------*/
|
|---|
| 484 | /**
|
|---|
| 485 | * callback called from hub polling fibril when the fibril terminates
|
|---|
| 486 | *
|
|---|
| 487 | * Does not perform cleanup, just marks the hub as not running.
|
|---|
| 488 | * @param device usb device afected
|
|---|
| 489 | * @param was_error indicates that the fibril is stoped due to an error
|
|---|
| 490 | * @param data pointer to usb_hub_dev_t structure
|
|---|
| 491 | */
|
|---|
| 492 | static void usb_hub_polling_terminated_callback(usb_device_t *device,
|
|---|
| 493 | bool was_error, void *data)
|
|---|
| 494 | {
|
|---|
| 495 | usb_hub_dev_t *hub = data;
|
|---|
| 496 | assert(hub);
|
|---|
| 497 |
|
|---|
| 498 | fibril_mutex_lock(&hub->pending_ops_mutex);
|
|---|
| 499 |
|
|---|
| 500 | /* The device is dead. However there might be some pending operations
|
|---|
| 501 | * that we need to wait for.
|
|---|
| 502 | * One of them is device adding in progress.
|
|---|
| 503 | * The respective fibril is probably waiting for status change
|
|---|
| 504 | * in port reset (port enable) callback.
|
|---|
| 505 | * Such change would never come (otherwise we would not be here).
|
|---|
| 506 | * Thus, we would flush all pending port resets.
|
|---|
| 507 | */
|
|---|
| 508 | if (hub->pending_ops_count > 0) {
|
|---|
| 509 | for (size_t port = 0; port < hub->port_count; ++port) {
|
|---|
| 510 | usb_hub_port_reset_fail(&hub->ports[port]);
|
|---|
| 511 | }
|
|---|
| 512 | }
|
|---|
| 513 | /* And now wait for them. */
|
|---|
| 514 | while (hub->pending_ops_count > 0) {
|
|---|
| 515 | fibril_condvar_wait(&hub->pending_ops_cv,
|
|---|
| 516 | &hub->pending_ops_mutex);
|
|---|
| 517 | }
|
|---|
| 518 | fibril_mutex_unlock(&hub->pending_ops_mutex);
|
|---|
| 519 | hub->running = false;
|
|---|
| 520 | }
|
|---|
| 521 | /**
|
|---|
| 522 | * @}
|
|---|
| 523 | */
|
|---|