| 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 | /* Set hub's first configuration. (There should be only one) */
|
|---|
| 120 | int opResult = usb_set_first_configuration(usb_dev);
|
|---|
| 121 | if (opResult != EOK) {
|
|---|
| 122 | usb_log_error("Could not set hub configuration: %s",
|
|---|
| 123 | str_error(opResult));
|
|---|
| 124 | return opResult;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /* Get port count and create attached_devices. */
|
|---|
| 128 | opResult = usb_hub_process_hub_specific_info(hub_dev);
|
|---|
| 129 | if (opResult != EOK) {
|
|---|
| 130 | usb_log_error("Could process hub specific info, %s",
|
|---|
| 131 | str_error(opResult));
|
|---|
| 132 | return opResult;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /* Create hub control function. */
|
|---|
| 136 | usb_log_debug("Creating DDF function '" HUB_FNC_NAME "'.");
|
|---|
| 137 | hub_dev->hub_fun = usb_device_ddf_fun_create(hub_dev->usb_device,
|
|---|
| 138 | fun_exposed, HUB_FNC_NAME);
|
|---|
| 139 | if (hub_dev->hub_fun == NULL) {
|
|---|
| 140 | usb_log_error("Failed to create hub function.");
|
|---|
| 141 | return ENOMEM;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /* Bind hub control function. */
|
|---|
| 145 | opResult = ddf_fun_bind(hub_dev->hub_fun);
|
|---|
| 146 | if (opResult != EOK) {
|
|---|
| 147 | usb_log_error("Failed to bind hub function: %s.",
|
|---|
| 148 | str_error(opResult));
|
|---|
| 149 | ddf_fun_destroy(hub_dev->hub_fun);
|
|---|
| 150 | return opResult;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /* Start hub operation. */
|
|---|
| 154 | usb_polling_t *polling = &hub_dev->polling;
|
|---|
| 155 | opResult = usb_polling_init(polling);
|
|---|
| 156 | if (opResult != EOK) {
|
|---|
| 157 | /* Function is already bound */
|
|---|
| 158 | ddf_fun_unbind(hub_dev->hub_fun);
|
|---|
| 159 | ddf_fun_destroy(hub_dev->hub_fun);
|
|---|
| 160 | usb_log_error("Failed to initialize polling fibril: %s.",
|
|---|
| 161 | str_error(opResult));
|
|---|
| 162 | return opResult;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | polling->device = hub_dev->usb_device;
|
|---|
| 166 | polling->ep_mapping = usb_device_get_mapped_ep_desc(hub_dev->usb_device,
|
|---|
| 167 | &hub_status_change_endpoint_description);
|
|---|
| 168 | polling->request_size = ((hub_dev->port_count + 1 + 7) / 8);
|
|---|
| 169 | polling->buffer = malloc(polling->request_size);
|
|---|
| 170 | polling->on_data = hub_port_changes_callback;
|
|---|
| 171 | polling->on_error = usb_hub_polling_error_callback;
|
|---|
| 172 | polling->arg = hub_dev;
|
|---|
| 173 |
|
|---|
| 174 | opResult = usb_polling_start(polling);
|
|---|
| 175 | if (opResult != EOK) {
|
|---|
| 176 | /* Polling is already initialized. */
|
|---|
| 177 | free(polling->buffer);
|
|---|
| 178 | usb_polling_fini(polling);
|
|---|
| 179 | ddf_fun_unbind(hub_dev->hub_fun);
|
|---|
| 180 | ddf_fun_destroy(hub_dev->hub_fun);
|
|---|
| 181 | usb_log_error("Failed to create polling fibril: %s.",
|
|---|
| 182 | str_error(opResult));
|
|---|
| 183 | return opResult;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | usb_log_info("Controlling hub '%s' (%p: %zu ports).",
|
|---|
| 187 | usb_device_get_name(hub_dev->usb_device), hub_dev,
|
|---|
| 188 | hub_dev->port_count);
|
|---|
| 189 |
|
|---|
| 190 | return EOK;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | static int usb_hub_cleanup(usb_hub_dev_t *hub)
|
|---|
| 194 | {
|
|---|
| 195 | free(hub->polling.buffer);
|
|---|
| 196 | usb_polling_fini(&hub->polling);
|
|---|
| 197 |
|
|---|
| 198 | for (size_t port = 0; port < hub->port_count; ++port) {
|
|---|
| 199 | usb_hub_port_fini(&hub->ports[port]);
|
|---|
| 200 | }
|
|---|
| 201 | free(hub->ports);
|
|---|
| 202 |
|
|---|
| 203 | const int ret = ddf_fun_unbind(hub->hub_fun);
|
|---|
| 204 | if (ret != EOK) {
|
|---|
| 205 | usb_log_error("(%p) Failed to unbind '%s' function: %s.",
|
|---|
| 206 | hub, HUB_FNC_NAME, str_error(ret));
|
|---|
| 207 | return ret;
|
|---|
| 208 | }
|
|---|
| 209 | ddf_fun_destroy(hub->hub_fun);
|
|---|
| 210 |
|
|---|
| 211 | usb_log_info("(%p) USB hub driver stopped and cleaned.", hub);
|
|---|
| 212 |
|
|---|
| 213 | /* Device data (usb_hub_dev_t) will be freed by usbdev. */
|
|---|
| 214 | return EOK;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | /**
|
|---|
| 218 | * Turn off power to all ports.
|
|---|
| 219 | *
|
|---|
| 220 | * @param usb_dev generic usb device information
|
|---|
| 221 | * @return error code
|
|---|
| 222 | */
|
|---|
| 223 | int usb_hub_device_remove(usb_device_t *usb_dev)
|
|---|
| 224 | {
|
|---|
| 225 | assert(usb_dev);
|
|---|
| 226 | usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
|
|---|
| 227 | assert(hub);
|
|---|
| 228 |
|
|---|
| 229 | usb_log_info("(%p) USB hub removed, joining polling fibril.", hub);
|
|---|
| 230 |
|
|---|
| 231 | /* Join polling fibril (ignoring error code). */
|
|---|
| 232 | usb_polling_join(&hub->polling);
|
|---|
| 233 | usb_log_info("(%p) USB hub polling stopped, freeing memory.", hub);
|
|---|
| 234 |
|
|---|
| 235 | /* Destroy hub. */
|
|---|
| 236 | return usb_hub_cleanup(hub);
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | /**
|
|---|
| 240 | * Remove all attached devices
|
|---|
| 241 | * @param usb_dev generic usb device information
|
|---|
| 242 | * @return error code
|
|---|
| 243 | */
|
|---|
| 244 | int usb_hub_device_gone(usb_device_t *usb_dev)
|
|---|
| 245 | {
|
|---|
| 246 | assert(usb_dev);
|
|---|
| 247 | usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
|
|---|
| 248 | assert(hub);
|
|---|
| 249 |
|
|---|
| 250 | usb_log_info("(%p) USB hub gone, joining polling fibril.", hub);
|
|---|
| 251 |
|
|---|
| 252 | /* Join polling fibril (ignoring error code). */
|
|---|
| 253 | usb_polling_join(&hub->polling);
|
|---|
| 254 | usb_log_info("(%p) USB hub polling stopped, freeing memory.", hub);
|
|---|
| 255 |
|
|---|
| 256 | /* Destroy hub. */
|
|---|
| 257 | return usb_hub_cleanup(hub);
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | /** Callback for polling hub for changes.
|
|---|
| 261 | *
|
|---|
| 262 | * @param dev Device where the change occured.
|
|---|
| 263 | * @param change_bitmap Bitmap of changed ports.
|
|---|
| 264 | * @param change_bitmap_size Size of the bitmap in bytes.
|
|---|
| 265 | * @param arg Custom argument, points to @c usb_hub_dev_t.
|
|---|
| 266 | * @return Whether to continue polling.
|
|---|
| 267 | */
|
|---|
| 268 | bool hub_port_changes_callback(usb_device_t *dev,
|
|---|
| 269 | uint8_t *change_bitmap, size_t change_bitmap_size, void *arg)
|
|---|
| 270 | {
|
|---|
| 271 | usb_hub_dev_t *hub = arg;
|
|---|
| 272 | assert(hub);
|
|---|
| 273 |
|
|---|
| 274 | /* It is an error condition if we didn't receive enough data */
|
|---|
| 275 | if (change_bitmap_size == 0) {
|
|---|
| 276 | return false;
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | /* Lowest bit indicates global change */
|
|---|
| 280 | const bool change = change_bitmap[0] & 1;
|
|---|
| 281 | if (change) {
|
|---|
| 282 | usb_hub_global_interrupt(hub);
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | /* N + 1 bit indicates change on port N */
|
|---|
| 286 | for (size_t port = 0; port < hub->port_count; ++port) {
|
|---|
| 287 | const size_t bit = port + 1;
|
|---|
| 288 | const bool change = (change_bitmap[bit / 8] >> (bit % 8)) & 1;
|
|---|
| 289 | if (change) {
|
|---|
| 290 | usb_hub_port_process_interrupt(&hub->ports[port], hub);
|
|---|
| 291 | }
|
|---|
| 292 | }
|
|---|
| 293 | return true;
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | /**
|
|---|
| 297 | * Load hub-specific information into hub_dev structure and process if needed
|
|---|
| 298 | *
|
|---|
| 299 | * Read port count and initialize structures holding per port information.
|
|---|
| 300 | * If there are any non-removable devices, start initializing them.
|
|---|
| 301 | * This function is hub-specific and should be run only after the hub is
|
|---|
| 302 | * configured using usb_set_first_configuration function.
|
|---|
| 303 | * @param hub_dev hub representation
|
|---|
| 304 | * @return error code
|
|---|
| 305 | */
|
|---|
| 306 | static int usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev)
|
|---|
| 307 | {
|
|---|
| 308 | assert(hub_dev);
|
|---|
| 309 |
|
|---|
| 310 | /* Get hub descriptor. */
|
|---|
| 311 | usb_log_debug("(%p): Retrieving descriptor.", hub_dev);
|
|---|
| 312 | usb_pipe_t *control_pipe =
|
|---|
| 313 | usb_device_get_default_pipe(hub_dev->usb_device);
|
|---|
| 314 |
|
|---|
| 315 | usb_hub_descriptor_header_t descriptor;
|
|---|
| 316 | size_t received_size;
|
|---|
| 317 | int opResult = usb_request_get_descriptor(control_pipe,
|
|---|
| 318 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
|
|---|
| 319 | USB_DESCTYPE_HUB, 0, 0, &descriptor,
|
|---|
| 320 | sizeof(usb_hub_descriptor_header_t), &received_size);
|
|---|
| 321 | if (opResult != EOK) {
|
|---|
| 322 | usb_log_error("(%p): Failed to receive hub descriptor: %s.",
|
|---|
| 323 | hub_dev, str_error(opResult));
|
|---|
| 324 | return opResult;
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | usb_log_debug("(%p): Setting port count to %d.", hub_dev,
|
|---|
| 328 | descriptor.port_count);
|
|---|
| 329 | hub_dev->port_count = descriptor.port_count;
|
|---|
| 330 | hub_dev->control_pipe = control_pipe;
|
|---|
| 331 |
|
|---|
| 332 | hub_dev->ports = calloc(hub_dev->port_count, sizeof(usb_hub_port_t));
|
|---|
| 333 | if (!hub_dev->ports) {
|
|---|
| 334 | return ENOMEM;
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | for (size_t port = 0; port < hub_dev->port_count; ++port) {
|
|---|
| 338 | usb_hub_port_init(&hub_dev->ports[port], hub_dev, port + 1);
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | hub_dev->power_switched =
|
|---|
| 342 | !(descriptor.characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
|
|---|
| 343 | hub_dev->per_port_power =
|
|---|
| 344 | descriptor.characteristics & HUB_CHAR_POWER_PER_PORT_FLAG;
|
|---|
| 345 |
|
|---|
| 346 | if (!hub_dev->power_switched) {
|
|---|
| 347 | usb_log_info("(%p): Power switching not supported, "
|
|---|
| 348 | "ports always powered.", hub_dev);
|
|---|
| 349 | return EOK;
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | usb_log_info("(%p): Hub port power switching enabled (%s).", hub_dev,
|
|---|
| 353 | hub_dev->per_port_power ? "per port" : "ganged");
|
|---|
| 354 |
|
|---|
| 355 | for (unsigned int port = 0; port < hub_dev->port_count; ++port) {
|
|---|
| 356 | usb_log_debug("(%p): Powering port %u.", hub_dev, port);
|
|---|
| 357 | const int ret = usb_hub_set_port_feature(hub_dev, port, USB_HUB_FEATURE_PORT_POWER);
|
|---|
| 358 |
|
|---|
| 359 | if (ret != EOK) {
|
|---|
| 360 | usb_log_error("(%p-%u): Cannot power on port: %s.",
|
|---|
| 361 | hub_dev, hub_dev->ports[port].port_number,
|
|---|
| 362 | str_error(ret));
|
|---|
| 363 | } else {
|
|---|
| 364 | if (!hub_dev->per_port_power) {
|
|---|
| 365 | usb_log_debug("(%p) Ganged power switching, "
|
|---|
| 366 | "one port is enough.", hub_dev);
|
|---|
| 367 | break;
|
|---|
| 368 | }
|
|---|
| 369 | }
|
|---|
| 370 | }
|
|---|
| 371 | return EOK;
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | /**
|
|---|
| 375 | * Set configuration of and USB device
|
|---|
| 376 | *
|
|---|
| 377 | * Check whether there is at least one configuration and sets the first one.
|
|---|
| 378 | * This function should be run prior to running any hub-specific action.
|
|---|
| 379 | * @param usb_device usb device representation
|
|---|
| 380 | * @return error code
|
|---|
| 381 | */
|
|---|
| 382 | static int usb_set_first_configuration(usb_device_t *usb_device)
|
|---|
| 383 | {
|
|---|
| 384 | assert(usb_device);
|
|---|
| 385 | /* Get number of possible configurations from device descriptor */
|
|---|
| 386 | const size_t configuration_count =
|
|---|
| 387 | usb_device_descriptors(usb_device)->device.configuration_count;
|
|---|
| 388 | usb_log_debug("Hub has %zu configurations.", configuration_count);
|
|---|
| 389 |
|
|---|
| 390 | if (configuration_count < 1) {
|
|---|
| 391 | usb_log_error("There are no configurations available");
|
|---|
| 392 | return EINVAL;
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | const size_t config_size =
|
|---|
| 396 | usb_device_descriptors(usb_device)->full_config_size;
|
|---|
| 397 | const usb_standard_configuration_descriptor_t *config_descriptor =
|
|---|
| 398 | usb_device_descriptors(usb_device)->full_config;
|
|---|
| 399 |
|
|---|
| 400 | if (config_size < sizeof(usb_standard_configuration_descriptor_t)) {
|
|---|
| 401 | usb_log_error("Configuration descriptor is not big enough"
|
|---|
| 402 | " to fit standard configuration descriptor.\n");
|
|---|
| 403 | return EOVERFLOW;
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | /* Set configuration. Use the configuration that was in
|
|---|
| 407 | * usb_device->descriptors.configuration i.e. The first one. */
|
|---|
| 408 | const int opResult = usb_request_set_configuration(
|
|---|
| 409 | usb_device_get_default_pipe(usb_device),
|
|---|
| 410 | config_descriptor->configuration_number);
|
|---|
| 411 | if (opResult != EOK) {
|
|---|
| 412 | usb_log_error("Failed to set hub configuration: %s.",
|
|---|
| 413 | str_error(opResult));
|
|---|
| 414 | } else {
|
|---|
| 415 | usb_log_debug("\tUsed configuration %d",
|
|---|
| 416 | config_descriptor->configuration_number);
|
|---|
| 417 | }
|
|---|
| 418 | return opResult;
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | /**
|
|---|
| 422 | * Process hub over current change
|
|---|
| 423 | *
|
|---|
| 424 | * This means either to power off the hub or power it on.
|
|---|
| 425 | * @param hub_dev hub instance
|
|---|
| 426 | * @param status hub status bitmask
|
|---|
| 427 | * @return error code
|
|---|
| 428 | */
|
|---|
| 429 | static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
|
|---|
| 430 | usb_hub_status_t status)
|
|---|
| 431 | {
|
|---|
| 432 | if (status & USB_HUB_STATUS_OVER_CURRENT) {
|
|---|
| 433 | /* Hub should remove power from all ports if it detects OC */
|
|---|
| 434 | usb_log_warning("(%p) Detected hub over-current condition, "
|
|---|
| 435 | "all ports should be powered off.", hub_dev);
|
|---|
| 436 | return;
|
|---|
| 437 | }
|
|---|
| 438 |
|
|---|
| 439 | /* Ports are always powered. */
|
|---|
| 440 | if (!hub_dev->power_switched)
|
|---|
| 441 | return;
|
|---|
| 442 |
|
|---|
| 443 | /* Over-current condition is gone, it is safe to turn the ports on. */
|
|---|
| 444 | for (size_t port = 0; port < hub_dev->port_count; ++port) {
|
|---|
| 445 | const int ret = usb_hub_set_port_feature(hub_dev, port, USB_HUB_FEATURE_PORT_POWER);
|
|---|
| 446 | if (ret != EOK) {
|
|---|
| 447 | usb_log_warning("(%p-%u): HUB OVER-CURRENT GONE: Cannot"
|
|---|
| 448 | " power on port: %s\n", hub_dev,
|
|---|
| 449 | hub_dev->ports[port].port_number, str_error(ret));
|
|---|
| 450 | } else {
|
|---|
| 451 | if (!hub_dev->per_port_power)
|
|---|
| 452 | return;
|
|---|
| 453 | }
|
|---|
| 454 | }
|
|---|
| 455 | }
|
|---|
| 456 |
|
|---|
| 457 | /**
|
|---|
| 458 | * Set feature on the real hub port.
|
|---|
| 459 | *
|
|---|
| 460 | * @param port Port structure.
|
|---|
| 461 | * @param feature Feature selector.
|
|---|
| 462 | */
|
|---|
| 463 | int usb_hub_set_port_feature(const usb_hub_dev_t *hub, size_t port_number, usb_hub_class_feature_t feature)
|
|---|
| 464 | {
|
|---|
| 465 | assert(hub);
|
|---|
| 466 | const usb_device_request_setup_packet_t clear_request = {
|
|---|
| 467 | .request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE,
|
|---|
| 468 | .request = USB_DEVREQ_SET_FEATURE,
|
|---|
| 469 | .index = uint16_host2usb(port_number),
|
|---|
| 470 | .value = feature,
|
|---|
| 471 | .length = 0,
|
|---|
| 472 | };
|
|---|
| 473 | return usb_pipe_control_write(hub->control_pipe, &clear_request,
|
|---|
| 474 | sizeof(clear_request), NULL, 0);
|
|---|
| 475 | }
|
|---|
| 476 |
|
|---|
| 477 | /**
|
|---|
| 478 | * Clear feature on the real hub port.
|
|---|
| 479 | *
|
|---|
| 480 | * @param port Port structure.
|
|---|
| 481 | * @param feature Feature selector.
|
|---|
| 482 | */
|
|---|
| 483 | int usb_hub_clear_port_feature(const usb_hub_dev_t *hub, size_t port_number, usb_hub_class_feature_t feature)
|
|---|
| 484 | {
|
|---|
| 485 | assert(hub);
|
|---|
| 486 | const usb_device_request_setup_packet_t clear_request = {
|
|---|
| 487 | .request_type = USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE,
|
|---|
| 488 | .request = USB_DEVREQ_CLEAR_FEATURE,
|
|---|
| 489 | .value = feature,
|
|---|
| 490 | .index = uint16_host2usb(port_number),
|
|---|
| 491 | .length = 0,
|
|---|
| 492 | };
|
|---|
| 493 | return usb_pipe_control_write(hub->control_pipe,
|
|---|
| 494 | &clear_request, sizeof(clear_request), NULL, 0);
|
|---|
| 495 | }
|
|---|
| 496 |
|
|---|
| 497 | /**
|
|---|
| 498 | * Retrieve port status.
|
|---|
| 499 | *
|
|---|
| 500 | * @param[in] port Port structure
|
|---|
| 501 | * @param[out] status Where to store the port status.
|
|---|
| 502 | * @return Error code.
|
|---|
| 503 | */
|
|---|
| 504 | int usb_hub_get_port_status(const usb_hub_dev_t *hub, size_t port_number, usb_port_status_t *status)
|
|---|
| 505 | {
|
|---|
| 506 | assert(hub);
|
|---|
| 507 | assert(status);
|
|---|
| 508 |
|
|---|
| 509 | /* USB hub specific GET_PORT_STATUS request. See USB Spec 11.16.2.6
|
|---|
| 510 | * Generic GET_STATUS request cannot be used because of the difference
|
|---|
| 511 | * in status data size (2B vs. 4B)*/
|
|---|
| 512 | const usb_device_request_setup_packet_t request = {
|
|---|
| 513 | .request_type = USB_HUB_REQ_TYPE_GET_PORT_STATUS,
|
|---|
| 514 | .request = USB_HUB_REQUEST_GET_STATUS,
|
|---|
| 515 | .value = 0,
|
|---|
| 516 | .index = uint16_host2usb(port_number),
|
|---|
| 517 | .length = sizeof(usb_port_status_t),
|
|---|
| 518 | };
|
|---|
| 519 | size_t recv_size;
|
|---|
| 520 |
|
|---|
| 521 | const int rc = usb_pipe_control_read(hub->control_pipe,
|
|---|
| 522 | &request, sizeof(usb_device_request_setup_packet_t),
|
|---|
| 523 | status, sizeof(*status), &recv_size);
|
|---|
| 524 | if (rc != EOK)
|
|---|
| 525 | return rc;
|
|---|
| 526 |
|
|---|
| 527 | if (recv_size != sizeof(*status))
|
|---|
| 528 | return ELIMIT;
|
|---|
| 529 |
|
|---|
| 530 | return EOK;
|
|---|
| 531 | }
|
|---|
| 532 |
|
|---|
| 533 | /**
|
|---|
| 534 | * Process hub interrupts.
|
|---|
| 535 | *
|
|---|
| 536 | * The change can be either in the over-current condition or local-power change.
|
|---|
| 537 | * @param hub_dev hub instance
|
|---|
| 538 | */
|
|---|
| 539 | static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev)
|
|---|
| 540 | {
|
|---|
| 541 | assert(hub_dev);
|
|---|
| 542 | assert(hub_dev->usb_device);
|
|---|
| 543 | usb_log_debug("(%p): Global interrupt on th hub.", hub_dev);
|
|---|
| 544 | usb_pipe_t *control_pipe =
|
|---|
| 545 | usb_device_get_default_pipe(hub_dev->usb_device);
|
|---|
| 546 |
|
|---|
| 547 | usb_hub_status_t status;
|
|---|
| 548 | size_t rcvd_size;
|
|---|
| 549 | /* NOTE: We can't use standard USB GET_STATUS request, because
|
|---|
| 550 | * hubs reply is 4byte instead of 2 */
|
|---|
| 551 | const int opResult = usb_pipe_control_read(control_pipe,
|
|---|
| 552 | &get_hub_status_request, sizeof(get_hub_status_request),
|
|---|
| 553 | &status, sizeof(usb_hub_status_t), &rcvd_size);
|
|---|
| 554 | if (opResult != EOK) {
|
|---|
| 555 | usb_log_error("(%p): Could not get hub status: %s.", hub_dev,
|
|---|
| 556 | str_error(opResult));
|
|---|
| 557 | return;
|
|---|
| 558 | }
|
|---|
| 559 | if (rcvd_size != sizeof(usb_hub_status_t)) {
|
|---|
| 560 | usb_log_error("(%p): Received status has incorrect size: "
|
|---|
| 561 | "%zu != %zu", hub_dev, rcvd_size, sizeof(usb_hub_status_t));
|
|---|
| 562 | return;
|
|---|
| 563 | }
|
|---|
| 564 |
|
|---|
| 565 | /* Handle status changes */
|
|---|
| 566 | if (status & USB_HUB_STATUS_C_OVER_CURRENT) {
|
|---|
| 567 | usb_hub_over_current(hub_dev, status);
|
|---|
| 568 | /* Ack change in hub OC flag */
|
|---|
| 569 | const int ret = usb_request_clear_feature(
|
|---|
| 570 | control_pipe, USB_REQUEST_TYPE_CLASS,
|
|---|
| 571 | USB_REQUEST_RECIPIENT_DEVICE,
|
|---|
| 572 | USB_HUB_FEATURE_C_HUB_OVER_CURRENT, 0);
|
|---|
| 573 | if (ret != EOK) {
|
|---|
| 574 | usb_log_error("(%p): Failed to clear hub over-current "
|
|---|
| 575 | "change flag: %s.\n", hub_dev, str_error(opResult));
|
|---|
| 576 | }
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| 579 | if (status & USB_HUB_STATUS_C_LOCAL_POWER) {
|
|---|
| 580 | /* NOTE: Handling this is more complicated.
|
|---|
| 581 | * If the transition is from bus power to local power, all
|
|---|
| 582 | * is good and we may signal the parent hub that we don't
|
|---|
| 583 | * need the power.
|
|---|
| 584 | * If the transition is from local power to bus power
|
|---|
| 585 | * the hub should turn off all the ports and devices need
|
|---|
| 586 | * to be reinitialized taking into account the limited power
|
|---|
| 587 | * that is now available.
|
|---|
| 588 | * There is no support for power distribution in HelenOS,
|
|---|
| 589 | * (or other OSes/hub devices that I've seen) so this is not
|
|---|
| 590 | * implemented.
|
|---|
| 591 | * Just ACK the change.
|
|---|
| 592 | */
|
|---|
| 593 | const int ret = usb_request_clear_feature(
|
|---|
| 594 | control_pipe, USB_REQUEST_TYPE_CLASS,
|
|---|
| 595 | USB_REQUEST_RECIPIENT_DEVICE,
|
|---|
| 596 | USB_HUB_FEATURE_C_HUB_LOCAL_POWER, 0);
|
|---|
| 597 | if (opResult != EOK) {
|
|---|
| 598 | usb_log_error("(%p): Failed to clear hub power change "
|
|---|
| 599 | "flag: %s.\n", hub_dev, str_error(ret));
|
|---|
| 600 | }
|
|---|
| 601 | }
|
|---|
| 602 | }
|
|---|
| 603 |
|
|---|
| 604 | /**
|
|---|
| 605 | * @}
|
|---|
| 606 | */
|
|---|