| 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 |
|
|---|
| 30 | /** @addtogroup drvusbhub
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | * @brief usb hub main functionality
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <ddf/driver.h>
|
|---|
| 38 | #include <stdbool.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <str_error.h>
|
|---|
| 41 | #include <inttypes.h>
|
|---|
| 42 | #include <stdio.h>
|
|---|
| 43 |
|
|---|
| 44 | #include <usb/usb.h>
|
|---|
| 45 | #include <usb/debug.h>
|
|---|
| 46 | #include <usb/dev/pipes.h>
|
|---|
| 47 | #include <usb/classes/classes.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 | /** Hub status-change endpoint description.
|
|---|
| 61 | *
|
|---|
| 62 | * For more information see section 11.15.1 of USB 1.1 specification.
|
|---|
| 63 | */
|
|---|
| 64 | const usb_endpoint_description_t hub_status_change_endpoint_description =
|
|---|
| 65 | {
|
|---|
| 66 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
|---|
| 67 | .direction = USB_DIRECTION_IN,
|
|---|
| 68 | .interface_class = USB_CLASS_HUB,
|
|---|
| 69 | .interface_subclass = 0,
|
|---|
| 70 | .interface_protocol = 0,
|
|---|
| 71 | .flags = 0
|
|---|
| 72 | };
|
|---|
| 73 |
|
|---|
| 74 | /** Standard get hub global status request */
|
|---|
| 75 | static const usb_device_request_setup_packet_t get_hub_status_request = {
|
|---|
| 76 | .request_type = USB_HUB_REQ_TYPE_GET_HUB_STATUS,
|
|---|
| 77 | .request = USB_HUB_REQUEST_GET_STATUS,
|
|---|
| 78 | .index = 0,
|
|---|
| 79 | .value = 0,
|
|---|
| 80 | .length = sizeof(usb_hub_status_t),
|
|---|
| 81 | };
|
|---|
| 82 |
|
|---|
| 83 | static int usb_set_first_configuration(usb_device_t *usb_device);
|
|---|
| 84 | static int usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev);
|
|---|
| 85 | static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
|
|---|
| 86 | usb_hub_status_t status);
|
|---|
| 87 | static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev);
|
|---|
| 88 |
|
|---|
| 89 | static bool usb_hub_polling_error_callback(usb_device_t *dev, int err_code, void *arg)
|
|---|
| 90 | {
|
|---|
| 91 | assert(dev);
|
|---|
| 92 | assert(arg);
|
|---|
| 93 |
|
|---|
| 94 | usb_log_error("Device %s polling error: %s", usb_device_get_name(dev), str_error(err_code));
|
|---|
| 95 |
|
|---|
| 96 | return true;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /**
|
|---|
| 100 | * Initialize hub device driver structure.
|
|---|
| 101 | *
|
|---|
| 102 | * Creates hub representation and fibril that periodically checks hub's status.
|
|---|
| 103 | * Hub representation is passed to the fibril.
|
|---|
| 104 | * @param usb_dev generic usb device information
|
|---|
| 105 | * @return error code
|
|---|
| 106 | */
|
|---|
| 107 | int usb_hub_device_add(usb_device_t *usb_dev)
|
|---|
| 108 | {
|
|---|
| 109 | assert(usb_dev);
|
|---|
| 110 | /* Create driver soft-state structure */
|
|---|
| 111 | usb_hub_dev_t *hub_dev =
|
|---|
| 112 | usb_device_data_alloc(usb_dev, sizeof(usb_hub_dev_t));
|
|---|
| 113 | if (hub_dev == NULL) {
|
|---|
| 114 | usb_log_error("Failed to create hub driver structure.");
|
|---|
| 115 | return ENOMEM;
|
|---|
| 116 | }
|
|---|
| 117 | hub_dev->usb_device = usb_dev;
|
|---|
| 118 |
|
|---|
| 119 | fibril_mutex_initialize(&hub_dev->default_address_guard);
|
|---|
| 120 | fibril_condvar_initialize(&hub_dev->default_address_cv);
|
|---|
| 121 |
|
|---|
| 122 | /* Set hub's first configuration. (There should be only one) */
|
|---|
| 123 | int opResult = usb_set_first_configuration(usb_dev);
|
|---|
| 124 | if (opResult != EOK) {
|
|---|
| 125 | usb_log_error("Could not set hub configuration: %s",
|
|---|
| 126 | str_error(opResult));
|
|---|
| 127 | return opResult;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /* Get port count and create attached_devices. */
|
|---|
| 131 | opResult = usb_hub_process_hub_specific_info(hub_dev);
|
|---|
| 132 | if (opResult != EOK) {
|
|---|
| 133 | usb_log_error("Could process hub specific info, %s",
|
|---|
| 134 | str_error(opResult));
|
|---|
| 135 | return opResult;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /* Create hub control function. */
|
|---|
| 139 | usb_log_debug("Creating DDF function '" HUB_FNC_NAME "'.");
|
|---|
| 140 | hub_dev->hub_fun = usb_device_ddf_fun_create(hub_dev->usb_device,
|
|---|
| 141 | fun_exposed, HUB_FNC_NAME);
|
|---|
| 142 | if (hub_dev->hub_fun == NULL) {
|
|---|
| 143 | usb_log_error("Failed to create hub function.");
|
|---|
| 144 | return ENOMEM;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /* Bind hub control function. */
|
|---|
| 148 | opResult = ddf_fun_bind(hub_dev->hub_fun);
|
|---|
| 149 | if (opResult != EOK) {
|
|---|
| 150 | usb_log_error("Failed to bind hub function: %s.",
|
|---|
| 151 | str_error(opResult));
|
|---|
| 152 | ddf_fun_destroy(hub_dev->hub_fun);
|
|---|
| 153 | return opResult;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /* Start hub operation. */
|
|---|
| 157 | usb_polling_t *polling = &hub_dev->polling;
|
|---|
| 158 | opResult = usb_polling_init(polling);
|
|---|
| 159 | if (opResult != EOK) {
|
|---|
| 160 | /* Function is already bound */
|
|---|
| 161 | ddf_fun_unbind(hub_dev->hub_fun);
|
|---|
| 162 | ddf_fun_destroy(hub_dev->hub_fun);
|
|---|
| 163 | usb_log_error("Failed to initialize polling fibril: %s.",
|
|---|
| 164 | str_error(opResult));
|
|---|
| 165 | return opResult;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | polling->device = hub_dev->usb_device;
|
|---|
| 169 | polling->ep_mapping = usb_device_get_mapped_ep_desc(hub_dev->usb_device,
|
|---|
| 170 | &hub_status_change_endpoint_description);
|
|---|
| 171 | polling->request_size = ((hub_dev->port_count + 1 + 7) / 8);
|
|---|
| 172 | polling->buffer = malloc(polling->request_size);
|
|---|
| 173 | polling->on_data = hub_port_changes_callback;
|
|---|
| 174 | polling->on_error = usb_hub_polling_error_callback;
|
|---|
| 175 | polling->arg = hub_dev;
|
|---|
| 176 |
|
|---|
| 177 | opResult = usb_polling_start(polling);
|
|---|
| 178 | if (opResult != EOK) {
|
|---|
| 179 | /* Polling is already initialized. */
|
|---|
| 180 | free(polling->buffer);
|
|---|
| 181 | usb_polling_fini(polling);
|
|---|
| 182 | ddf_fun_unbind(hub_dev->hub_fun);
|
|---|
| 183 | ddf_fun_destroy(hub_dev->hub_fun);
|
|---|
| 184 | usb_log_error("Failed to create polling fibril: %s.",
|
|---|
| 185 | str_error(opResult));
|
|---|
| 186 | return opResult;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | usb_log_info("Controlling hub '%s' (%p: %zu ports).",
|
|---|
| 190 | usb_device_get_name(hub_dev->usb_device), hub_dev,
|
|---|
| 191 | hub_dev->port_count);
|
|---|
| 192 |
|
|---|
| 193 | return EOK;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | static int usb_hub_cleanup(usb_hub_dev_t *hub)
|
|---|
| 197 | {
|
|---|
| 198 | free(hub->polling.buffer);
|
|---|
| 199 | usb_polling_fini(&hub->polling);
|
|---|
| 200 |
|
|---|
| 201 | for (size_t port = 0; port < hub->port_count; ++port) {
|
|---|
| 202 | usb_port_fini(&hub->ports[port].base);
|
|---|
| 203 | }
|
|---|
| 204 | free(hub->ports);
|
|---|
| 205 |
|
|---|
| 206 | const int ret = ddf_fun_unbind(hub->hub_fun);
|
|---|
| 207 | if (ret != EOK) {
|
|---|
| 208 | usb_log_error("(%p) Failed to unbind '%s' function: %s.",
|
|---|
| 209 | hub, HUB_FNC_NAME, str_error(ret));
|
|---|
| 210 | return ret;
|
|---|
| 211 | }
|
|---|
| 212 | ddf_fun_destroy(hub->hub_fun);
|
|---|
| 213 |
|
|---|
| 214 | usb_log_info("(%p) USB hub driver stopped and cleaned.", hub);
|
|---|
| 215 |
|
|---|
| 216 | /* Device data (usb_hub_dev_t) will be freed by usbdev. */
|
|---|
| 217 | return EOK;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | /**
|
|---|
| 221 | * Turn off power to all ports.
|
|---|
| 222 | *
|
|---|
| 223 | * @param usb_dev generic usb device information
|
|---|
| 224 | * @return error code
|
|---|
| 225 | */
|
|---|
| 226 | int usb_hub_device_remove(usb_device_t *usb_dev)
|
|---|
| 227 | {
|
|---|
| 228 | assert(usb_dev);
|
|---|
| 229 | usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
|
|---|
| 230 | assert(hub);
|
|---|
| 231 |
|
|---|
| 232 | usb_log_info("(%p) USB hub removed, joining polling fibril.", hub);
|
|---|
| 233 |
|
|---|
| 234 | /* Join polling fibril (ignoring error code). */
|
|---|
| 235 | usb_polling_join(&hub->polling);
|
|---|
| 236 | usb_log_info("(%p) USB hub polling stopped, freeing memory.", hub);
|
|---|
| 237 |
|
|---|
| 238 | /* Destroy hub. */
|
|---|
| 239 | return usb_hub_cleanup(hub);
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | /**
|
|---|
| 243 | * Remove all attached devices
|
|---|
| 244 | * @param usb_dev generic usb device information
|
|---|
| 245 | * @return error code
|
|---|
| 246 | */
|
|---|
| 247 | int usb_hub_device_gone(usb_device_t *usb_dev)
|
|---|
| 248 | {
|
|---|
| 249 | assert(usb_dev);
|
|---|
| 250 | usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
|
|---|
| 251 | assert(hub);
|
|---|
| 252 |
|
|---|
| 253 | usb_log_info("(%p) USB hub gone, joining polling fibril.", hub);
|
|---|
| 254 |
|
|---|
| 255 | /* Join polling fibril (ignoring error code). */
|
|---|
| 256 | usb_polling_join(&hub->polling);
|
|---|
| 257 | usb_log_info("(%p) USB hub polling stopped, freeing memory.", hub);
|
|---|
| 258 |
|
|---|
| 259 | /* Destroy hub. */
|
|---|
| 260 | return usb_hub_cleanup(hub);
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | /** Callback for polling hub for changes.
|
|---|
| 264 | *
|
|---|
| 265 | * @param dev Device where the change occured.
|
|---|
| 266 | * @param change_bitmap Bitmap of changed ports.
|
|---|
| 267 | * @param change_bitmap_size Size of the bitmap in bytes.
|
|---|
| 268 | * @param arg Custom argument, points to @c usb_hub_dev_t.
|
|---|
| 269 | * @return Whether to continue polling.
|
|---|
| 270 | */
|
|---|
| 271 | bool hub_port_changes_callback(usb_device_t *dev,
|
|---|
| 272 | uint8_t *change_bitmap, size_t change_bitmap_size, void *arg)
|
|---|
| 273 | {
|
|---|
| 274 | usb_hub_dev_t *hub = arg;
|
|---|
| 275 | assert(hub);
|
|---|
| 276 |
|
|---|
| 277 | /* It is an error condition if we didn't receive enough data */
|
|---|
| 278 | if (change_bitmap_size == 0) {
|
|---|
| 279 | return false;
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | /* Lowest bit indicates global change */
|
|---|
| 283 | const bool change = change_bitmap[0] & 1;
|
|---|
| 284 | if (change) {
|
|---|
| 285 | usb_hub_global_interrupt(hub);
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | /* N + 1 bit indicates change on port N */
|
|---|
| 289 | for (size_t port = 0; port < hub->port_count; ++port) {
|
|---|
| 290 | const size_t bit = port + 1;
|
|---|
| 291 | const bool change = (change_bitmap[bit / 8] >> (bit % 8)) & 1;
|
|---|
| 292 | if (change) {
|
|---|
| 293 | usb_hub_port_process_interrupt(&hub->ports[port]);
|
|---|
| 294 | }
|
|---|
| 295 | }
|
|---|
| 296 | return true;
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | /**
|
|---|
| 300 | * Load hub-specific information into hub_dev structure and process if needed
|
|---|
| 301 | *
|
|---|
| 302 | * Read port count and initialize structures holding per port information.
|
|---|
| 303 | * If there are any non-removable devices, start initializing them.
|
|---|
| 304 | * This function is hub-specific and should be run only after the hub is
|
|---|
| 305 | * configured using usb_set_first_configuration function.
|
|---|
| 306 | * @param hub_dev hub representation
|
|---|
| 307 | * @return error code
|
|---|
| 308 | */
|
|---|
| 309 | static int usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev)
|
|---|
| 310 | {
|
|---|
| 311 | assert(hub_dev);
|
|---|
| 312 |
|
|---|
| 313 | /* Get hub descriptor. */
|
|---|
| 314 | usb_log_debug("(%p): Retrieving descriptor.", hub_dev);
|
|---|
| 315 | usb_pipe_t *control_pipe =
|
|---|
| 316 | usb_device_get_default_pipe(hub_dev->usb_device);
|
|---|
| 317 |
|
|---|
| 318 | usb_hub_descriptor_header_t descriptor;
|
|---|
| 319 | size_t received_size;
|
|---|
| 320 | int opResult = usb_request_get_descriptor(control_pipe,
|
|---|
| 321 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
|
|---|
| 322 | USB_DESCTYPE_HUB, 0, 0, &descriptor,
|
|---|
| 323 | sizeof(usb_hub_descriptor_header_t), &received_size);
|
|---|
| 324 | if (opResult != EOK) {
|
|---|
| 325 | usb_log_error("(%p): Failed to receive hub descriptor: %s.",
|
|---|
| 326 | hub_dev, str_error(opResult));
|
|---|
| 327 | return opResult;
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | usb_log_debug("(%p): Setting port count to %d.", hub_dev,
|
|---|
| 331 | descriptor.port_count);
|
|---|
| 332 | hub_dev->port_count = descriptor.port_count;
|
|---|
| 333 | hub_dev->control_pipe = control_pipe;
|
|---|
| 334 |
|
|---|
| 335 | hub_dev->ports = calloc(hub_dev->port_count, sizeof(usb_hub_port_t));
|
|---|
| 336 | if (!hub_dev->ports) {
|
|---|
| 337 | return ENOMEM;
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | for (size_t port = 0; port < hub_dev->port_count; ++port) {
|
|---|
| 341 | usb_hub_port_init(&hub_dev->ports[port], hub_dev, port + 1);
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | hub_dev->power_switched =
|
|---|
| 345 | !(descriptor.characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
|
|---|
| 346 | hub_dev->per_port_power =
|
|---|
| 347 | descriptor.characteristics & HUB_CHAR_POWER_PER_PORT_FLAG;
|
|---|
| 348 |
|
|---|
| 349 | if (!hub_dev->power_switched) {
|
|---|
| 350 | usb_log_info("(%p): Power switching not supported, "
|
|---|
| 351 | "ports always powered.", hub_dev);
|
|---|
| 352 | return EOK;
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | usb_log_info("(%p): Hub port power switching enabled (%s).", hub_dev,
|
|---|
| 356 | hub_dev->per_port_power ? "per port" : "ganged");
|
|---|
| 357 |
|
|---|
| 358 | for (unsigned int port = 0; port < hub_dev->port_count; ++port) {
|
|---|
| 359 | usb_log_debug("(%p): Powering port %u.", hub_dev, port);
|
|---|
| 360 | const int ret = usb_hub_set_port_feature(hub_dev, port, USB_HUB_FEATURE_PORT_POWER);
|
|---|
| 361 |
|
|---|
| 362 | if (ret != EOK) {
|
|---|
| 363 | usb_log_error("(%p-%u): Cannot power on port: %s.",
|
|---|
| 364 | hub_dev, hub_dev->ports[port].port_number,
|
|---|
| 365 | str_error(ret));
|
|---|
| 366 | } else {
|
|---|
| 367 | if (!hub_dev->per_port_power) {
|
|---|
| 368 | usb_log_debug("(%p) Ganged power switching, "
|
|---|
| 369 | "one port is enough.", hub_dev);
|
|---|
| 370 | break;
|
|---|
| 371 | }
|
|---|
| 372 | }
|
|---|
| 373 | }
|
|---|
| 374 | return EOK;
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | /**
|
|---|
| 378 | * Set configuration of and USB device
|
|---|
| 379 | *
|
|---|
| 380 | * Check whether there is at least one configuration and sets the first one.
|
|---|
| 381 | * This function should be run prior to running any hub-specific action.
|
|---|
| 382 | * @param usb_device usb device representation
|
|---|
| 383 | * @return error code
|
|---|
| 384 | */
|
|---|
| 385 | static int usb_set_first_configuration(usb_device_t *usb_device)
|
|---|
| 386 | {
|
|---|
| 387 | assert(usb_device);
|
|---|
| 388 | /* Get number of possible configurations from device descriptor */
|
|---|
| 389 | const size_t configuration_count =
|
|---|
| 390 | usb_device_descriptors(usb_device)->device.configuration_count;
|
|---|
| 391 | usb_log_debug("Hub has %zu configurations.", configuration_count);
|
|---|
| 392 |
|
|---|
| 393 | if (configuration_count < 1) {
|
|---|
| 394 | usb_log_error("There are no configurations available");
|
|---|
| 395 | return EINVAL;
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | const size_t config_size =
|
|---|
| 399 | usb_device_descriptors(usb_device)->full_config_size;
|
|---|
| 400 | const usb_standard_configuration_descriptor_t *config_descriptor =
|
|---|
| 401 | usb_device_descriptors(usb_device)->full_config;
|
|---|
| 402 |
|
|---|
| 403 | if (config_size < sizeof(usb_standard_configuration_descriptor_t)) {
|
|---|
| 404 | usb_log_error("Configuration descriptor is not big enough"
|
|---|
| 405 | " to fit standard configuration descriptor.\n");
|
|---|
| 406 | return EOVERFLOW;
|
|---|
| 407 | }
|
|---|
| 408 |
|
|---|
| 409 | /* Set configuration. Use the configuration that was in
|
|---|
| 410 | * usb_device->descriptors.configuration i.e. The first one. */
|
|---|
| 411 | const int opResult = usb_request_set_configuration(
|
|---|
| 412 | usb_device_get_default_pipe(usb_device),
|
|---|
| 413 | config_descriptor->configuration_number);
|
|---|
| 414 | if (opResult != EOK) {
|
|---|
| 415 | usb_log_error("Failed to set hub configuration: %s.",
|
|---|
| 416 | str_error(opResult));
|
|---|
| 417 | } else {
|
|---|
| 418 | usb_log_debug("\tUsed configuration %d",
|
|---|
| 419 | config_descriptor->configuration_number);
|
|---|
| 420 | }
|
|---|
| 421 | return opResult;
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | /**
|
|---|
| 425 | * Process hub over current change
|
|---|
| 426 | *
|
|---|
| 427 | * This means either to power off the hub or power it on.
|
|---|
| 428 | * @param hub_dev hub instance
|
|---|
| 429 | * @param status hub status bitmask
|
|---|
| 430 | * @return error code
|
|---|
| 431 | */
|
|---|
| 432 | static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
|
|---|
| 433 | usb_hub_status_t status)
|
|---|
| 434 | {
|
|---|
| 435 | if (status & USB_HUB_STATUS_OVER_CURRENT) {
|
|---|
| 436 | /* Hub should remove power from all ports if it detects OC */
|
|---|
| 437 | usb_log_warning("(%p) Detected hub over-current condition, "
|
|---|
| 438 | "all ports should be powered off.", hub_dev);
|
|---|
| 439 | return;
|
|---|
| 440 | }
|
|---|
| 441 |
|
|---|
| 442 | /* Ports are always powered. */
|
|---|
| 443 | if (!hub_dev->power_switched)
|
|---|
| 444 | return;
|
|---|
| 445 |
|
|---|
| 446 | /* Over-current condition is gone, it is safe to turn the ports on. */
|
|---|
| 447 | for (size_t port = 0; port < hub_dev->port_count; ++port) {
|
|---|
| 448 | const int ret = usb_hub_set_port_feature(hub_dev, port, USB_HUB_FEATURE_PORT_POWER);
|
|---|
| 449 | if (ret != EOK) {
|
|---|
| 450 | usb_log_warning("(%p-%u): HUB OVER-CURRENT GONE: Cannot"
|
|---|
| 451 | " power on port: %s\n", hub_dev,
|
|---|
| 452 | hub_dev->ports[port].port_number, str_error(ret));
|
|---|
| 453 | } else {
|
|---|
| 454 | if (!hub_dev->per_port_power)
|
|---|
| 455 | return;
|
|---|
| 456 | }
|
|---|
| 457 | }
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 | /**
|
|---|
| 461 | * Set feature on the real hub port.
|
|---|
| 462 | *
|
|---|
| 463 | * @param port Port structure.
|
|---|
| 464 | * @param feature Feature selector.
|
|---|
| 465 | */
|
|---|
| 466 | int usb_hub_set_port_feature(const usb_hub_dev_t *hub, size_t port_number, usb_hub_class_feature_t feature)
|
|---|
| 467 | {
|
|---|
| 468 | assert(hub);
|
|---|
| 469 | const usb_device_request_setup_packet_t clear_request = {
|
|---|
| 470 | .request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE,
|
|---|
| 471 | .request = USB_DEVREQ_SET_FEATURE,
|
|---|
| 472 | .index = uint16_host2usb(port_number),
|
|---|
| 473 | .value = feature,
|
|---|
| 474 | .length = 0,
|
|---|
| 475 | };
|
|---|
| 476 | return usb_pipe_control_write(hub->control_pipe, &clear_request,
|
|---|
| 477 | sizeof(clear_request), NULL, 0);
|
|---|
| 478 | }
|
|---|
| 479 |
|
|---|
| 480 | /**
|
|---|
| 481 | * Clear feature on the real hub port.
|
|---|
| 482 | *
|
|---|
| 483 | * @param port Port structure.
|
|---|
| 484 | * @param feature Feature selector.
|
|---|
| 485 | */
|
|---|
| 486 | int usb_hub_clear_port_feature(const usb_hub_dev_t *hub, size_t port_number, usb_hub_class_feature_t feature)
|
|---|
| 487 | {
|
|---|
| 488 | assert(hub);
|
|---|
| 489 | const usb_device_request_setup_packet_t clear_request = {
|
|---|
| 490 | .request_type = USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE,
|
|---|
| 491 | .request = USB_DEVREQ_CLEAR_FEATURE,
|
|---|
| 492 | .value = feature,
|
|---|
| 493 | .index = uint16_host2usb(port_number),
|
|---|
| 494 | .length = 0,
|
|---|
| 495 | };
|
|---|
| 496 | return usb_pipe_control_write(hub->control_pipe,
|
|---|
| 497 | &clear_request, sizeof(clear_request), NULL, 0);
|
|---|
| 498 | }
|
|---|
| 499 |
|
|---|
| 500 | /**
|
|---|
| 501 | * Retrieve port status.
|
|---|
| 502 | *
|
|---|
| 503 | * @param[in] port Port structure
|
|---|
| 504 | * @param[out] status Where to store the port status.
|
|---|
| 505 | * @return Error code.
|
|---|
| 506 | */
|
|---|
| 507 | int usb_hub_get_port_status(const usb_hub_dev_t *hub, size_t port_number, usb_port_status_t *status)
|
|---|
| 508 | {
|
|---|
| 509 | assert(hub);
|
|---|
| 510 | assert(status);
|
|---|
| 511 |
|
|---|
| 512 | /* USB hub specific GET_PORT_STATUS request. See USB Spec 11.16.2.6
|
|---|
| 513 | * Generic GET_STATUS request cannot be used because of the difference
|
|---|
| 514 | * in status data size (2B vs. 4B)*/
|
|---|
| 515 | const usb_device_request_setup_packet_t request = {
|
|---|
| 516 | .request_type = USB_HUB_REQ_TYPE_GET_PORT_STATUS,
|
|---|
| 517 | .request = USB_HUB_REQUEST_GET_STATUS,
|
|---|
| 518 | .value = 0,
|
|---|
| 519 | .index = uint16_host2usb(port_number),
|
|---|
| 520 | .length = sizeof(usb_port_status_t),
|
|---|
| 521 | };
|
|---|
| 522 | size_t recv_size;
|
|---|
| 523 |
|
|---|
| 524 | const int rc = usb_pipe_control_read(hub->control_pipe,
|
|---|
| 525 | &request, sizeof(usb_device_request_setup_packet_t),
|
|---|
| 526 | status, sizeof(*status), &recv_size);
|
|---|
| 527 | if (rc != EOK)
|
|---|
| 528 | return rc;
|
|---|
| 529 |
|
|---|
| 530 | if (recv_size != sizeof(*status))
|
|---|
| 531 | return ELIMIT;
|
|---|
| 532 |
|
|---|
| 533 | return EOK;
|
|---|
| 534 | }
|
|---|
| 535 |
|
|---|
| 536 | /**
|
|---|
| 537 | * Process hub interrupts.
|
|---|
| 538 | *
|
|---|
| 539 | * The change can be either in the over-current condition or local-power change.
|
|---|
| 540 | * @param hub_dev hub instance
|
|---|
| 541 | */
|
|---|
| 542 | static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev)
|
|---|
| 543 | {
|
|---|
| 544 | assert(hub_dev);
|
|---|
| 545 | assert(hub_dev->usb_device);
|
|---|
| 546 | usb_log_debug("(%p): Global interrupt on th hub.", hub_dev);
|
|---|
| 547 | usb_pipe_t *control_pipe =
|
|---|
| 548 | usb_device_get_default_pipe(hub_dev->usb_device);
|
|---|
| 549 |
|
|---|
| 550 | usb_hub_status_t status;
|
|---|
| 551 | size_t rcvd_size;
|
|---|
| 552 | /* NOTE: We can't use standard USB GET_STATUS request, because
|
|---|
| 553 | * hubs reply is 4byte instead of 2 */
|
|---|
| 554 | const int opResult = usb_pipe_control_read(control_pipe,
|
|---|
| 555 | &get_hub_status_request, sizeof(get_hub_status_request),
|
|---|
| 556 | &status, sizeof(usb_hub_status_t), &rcvd_size);
|
|---|
| 557 | if (opResult != EOK) {
|
|---|
| 558 | usb_log_error("(%p): Could not get hub status: %s.", hub_dev,
|
|---|
| 559 | str_error(opResult));
|
|---|
| 560 | return;
|
|---|
| 561 | }
|
|---|
| 562 | if (rcvd_size != sizeof(usb_hub_status_t)) {
|
|---|
| 563 | usb_log_error("(%p): Received status has incorrect size: "
|
|---|
| 564 | "%zu != %zu", hub_dev, rcvd_size, sizeof(usb_hub_status_t));
|
|---|
| 565 | return;
|
|---|
| 566 | }
|
|---|
| 567 |
|
|---|
| 568 | /* Handle status changes */
|
|---|
| 569 | if (status & USB_HUB_STATUS_C_OVER_CURRENT) {
|
|---|
| 570 | usb_hub_over_current(hub_dev, status);
|
|---|
| 571 | /* Ack change in hub OC flag */
|
|---|
| 572 | const int ret = usb_request_clear_feature(
|
|---|
| 573 | control_pipe, USB_REQUEST_TYPE_CLASS,
|
|---|
| 574 | USB_REQUEST_RECIPIENT_DEVICE,
|
|---|
| 575 | USB_HUB_FEATURE_C_HUB_OVER_CURRENT, 0);
|
|---|
| 576 | if (ret != EOK) {
|
|---|
| 577 | usb_log_error("(%p): Failed to clear hub over-current "
|
|---|
| 578 | "change flag: %s.\n", hub_dev, str_error(opResult));
|
|---|
| 579 | }
|
|---|
| 580 | }
|
|---|
| 581 |
|
|---|
| 582 | if (status & USB_HUB_STATUS_C_LOCAL_POWER) {
|
|---|
| 583 | /* NOTE: Handling this is more complicated.
|
|---|
| 584 | * If the transition is from bus power to local power, all
|
|---|
| 585 | * is good and we may signal the parent hub that we don't
|
|---|
| 586 | * need the power.
|
|---|
| 587 | * If the transition is from local power to bus power
|
|---|
| 588 | * the hub should turn off all the ports and devices need
|
|---|
| 589 | * to be reinitialized taking into account the limited power
|
|---|
| 590 | * that is now available.
|
|---|
| 591 | * There is no support for power distribution in HelenOS,
|
|---|
| 592 | * (or other OSes/hub devices that I've seen) so this is not
|
|---|
| 593 | * implemented.
|
|---|
| 594 | * Just ACK the change.
|
|---|
| 595 | */
|
|---|
| 596 | const int ret = usb_request_clear_feature(
|
|---|
| 597 | control_pipe, USB_REQUEST_TYPE_CLASS,
|
|---|
| 598 | USB_REQUEST_RECIPIENT_DEVICE,
|
|---|
| 599 | USB_HUB_FEATURE_C_HUB_LOCAL_POWER, 0);
|
|---|
| 600 | if (opResult != EOK) {
|
|---|
| 601 | usb_log_error("(%p): Failed to clear hub power change "
|
|---|
| 602 | "flag: %s.\n", hub_dev, str_error(ret));
|
|---|
| 603 | }
|
|---|
| 604 | }
|
|---|
| 605 | }
|
|---|
| 606 |
|
|---|
| 607 | static FIBRIL_CONDVAR_INITIALIZE(global_hub_default_address_cv);
|
|---|
| 608 |
|
|---|
| 609 | /**
|
|---|
| 610 | * Reserve a default address for a port across all other devices connected to
|
|---|
| 611 | * the bus. We aggregate requests for ports to minimize delays between
|
|---|
| 612 | * connecting multiple devices from one hub - which happens e.g. when the hub
|
|---|
| 613 | * is connected with already attached devices.
|
|---|
| 614 | */
|
|---|
| 615 | int usb_hub_reserve_default_address(usb_hub_dev_t *hub, async_exch_t *exch, usb_port_t *port)
|
|---|
| 616 | {
|
|---|
| 617 | assert(hub);
|
|---|
| 618 | assert(exch);
|
|---|
| 619 | assert(port);
|
|---|
| 620 | assert(fibril_mutex_is_locked(&port->guard));
|
|---|
| 621 |
|
|---|
| 622 | fibril_mutex_lock(&hub->default_address_guard);
|
|---|
| 623 | if (hub->default_address_requests++ == 0) {
|
|---|
| 624 | /* We're the first to request the address, we can just do it */
|
|---|
| 625 | fibril_mutex_unlock(&hub->default_address_guard);
|
|---|
| 626 | int err;
|
|---|
| 627 | while ((err = usbhc_reserve_default_address(exch)) == EAGAIN) {
|
|---|
| 628 | // We ignore the return value here, as we cannot give up now.
|
|---|
| 629 | usb_port_condvar_wait_timeout(port, &global_hub_default_address_cv, 500000);
|
|---|
| 630 | }
|
|---|
| 631 | return err;
|
|---|
| 632 | } else {
|
|---|
| 633 | /* Drop the port guard, we're going to wait */
|
|---|
| 634 | fibril_mutex_unlock(&port->guard);
|
|---|
| 635 |
|
|---|
| 636 | /* Wait for a signal */
|
|---|
| 637 | fibril_condvar_wait(&hub->default_address_cv, &hub->default_address_guard);
|
|---|
| 638 |
|
|---|
| 639 | /* Remember ABBA, first drop the hub guard */
|
|---|
| 640 | fibril_mutex_unlock(&hub->default_address_guard);
|
|---|
| 641 | fibril_mutex_lock(&port->guard);
|
|---|
| 642 | return EOK;
|
|---|
| 643 | }
|
|---|
| 644 | }
|
|---|
| 645 |
|
|---|
| 646 | /**
|
|---|
| 647 | * Release the default address from a port.
|
|---|
| 648 | */
|
|---|
| 649 | int usb_hub_release_default_address(usb_hub_dev_t *hub, async_exch_t *exch)
|
|---|
| 650 | {
|
|---|
| 651 | int ret = EOK;
|
|---|
| 652 |
|
|---|
| 653 | fibril_mutex_lock(&hub->default_address_guard);
|
|---|
| 654 | if (--hub->default_address_requests == 0) {
|
|---|
| 655 | // We must do it in critical section to prevent other fibril
|
|---|
| 656 | // from requesting the address before we release
|
|---|
| 657 | ret = usbhc_release_default_address(exch);
|
|---|
| 658 | // This is optimistic optimization - it may wake one hub from polling sleep
|
|---|
| 659 | fibril_condvar_signal(&global_hub_default_address_cv);
|
|---|
| 660 | } else {
|
|---|
| 661 | fibril_condvar_signal(&hub->default_address_cv);
|
|---|
| 662 | }
|
|---|
| 663 | fibril_mutex_unlock(&hub->default_address_guard);
|
|---|
| 664 |
|
|---|
| 665 | return ret;
|
|---|
| 666 | }
|
|---|
| 667 |
|
|---|
| 668 | /**
|
|---|
| 669 | * @}
|
|---|
| 670 | */
|
|---|