| 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 | static void usb_hub_polling_terminated_callback(usb_device_t *device,
|
|---|
| 89 | bool was_error, void *data);
|
|---|
| 90 |
|
|---|
| 91 | static bool usb_hub_polling_error_callback(usb_device_t *dev, int err_code, void *arg)
|
|---|
| 92 | {
|
|---|
| 93 | assert(dev);
|
|---|
| 94 | assert(arg);
|
|---|
| 95 | usb_hub_dev_t *hub = arg;
|
|---|
| 96 |
|
|---|
| 97 | usb_log_error("Device %s polling error: %s", usb_device_get_name(dev),
|
|---|
| 98 | str_error(err_code));
|
|---|
| 99 |
|
|---|
| 100 | /* Continue polling until the device is about to be removed. */
|
|---|
| 101 | return hub->running;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | /**
|
|---|
| 105 | * Initialize hub device driver structure.
|
|---|
| 106 | *
|
|---|
| 107 | * Creates hub representation and fibril that periodically checks hub's status.
|
|---|
| 108 | * Hub representation is passed to the fibril.
|
|---|
| 109 | * @param usb_dev generic usb device information
|
|---|
| 110 | * @return error code
|
|---|
| 111 | */
|
|---|
| 112 | int usb_hub_device_add(usb_device_t *usb_dev)
|
|---|
| 113 | {
|
|---|
| 114 | assert(usb_dev);
|
|---|
| 115 | /* Create driver soft-state structure */
|
|---|
| 116 | usb_hub_dev_t *hub_dev =
|
|---|
| 117 | usb_device_data_alloc(usb_dev, sizeof(usb_hub_dev_t));
|
|---|
| 118 | if (hub_dev == NULL) {
|
|---|
| 119 | usb_log_error("Failed to create hub driver structure.\n");
|
|---|
| 120 | return ENOMEM;
|
|---|
| 121 | }
|
|---|
| 122 | hub_dev->usb_device = usb_dev;
|
|---|
| 123 | hub_dev->pending_ops_count = 0;
|
|---|
| 124 | hub_dev->running = false;
|
|---|
| 125 | fibril_mutex_initialize(&hub_dev->pending_ops_mutex);
|
|---|
| 126 | fibril_condvar_initialize(&hub_dev->pending_ops_cv);
|
|---|
| 127 |
|
|---|
| 128 | /* Set hub's first configuration. (There should be only one) */
|
|---|
| 129 | int opResult = usb_set_first_configuration(usb_dev);
|
|---|
| 130 | if (opResult != EOK) {
|
|---|
| 131 | usb_log_error("Could not set hub configuration: %s\n",
|
|---|
| 132 | str_error(opResult));
|
|---|
| 133 | return opResult;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | /* Get port count and create attached_devices. */
|
|---|
| 137 | opResult = usb_hub_process_hub_specific_info(hub_dev);
|
|---|
| 138 | if (opResult != EOK) {
|
|---|
| 139 | usb_log_error("Could process hub specific info, %s\n",
|
|---|
| 140 | str_error(opResult));
|
|---|
| 141 | return opResult;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /* Create hub control function. */
|
|---|
| 145 | usb_log_debug("Creating DDF function '" HUB_FNC_NAME "'.\n");
|
|---|
| 146 | hub_dev->hub_fun = usb_device_ddf_fun_create(hub_dev->usb_device,
|
|---|
| 147 | fun_exposed, HUB_FNC_NAME);
|
|---|
| 148 | if (hub_dev->hub_fun == NULL) {
|
|---|
| 149 | usb_log_error("Failed to create hub function.\n");
|
|---|
| 150 | return ENOMEM;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /* Bind hub control function. */
|
|---|
| 154 | opResult = ddf_fun_bind(hub_dev->hub_fun);
|
|---|
| 155 | if (opResult != EOK) {
|
|---|
| 156 | usb_log_error("Failed to bind hub function: %s.\n",
|
|---|
| 157 | str_error(opResult));
|
|---|
| 158 | ddf_fun_destroy(hub_dev->hub_fun);
|
|---|
| 159 | return opResult;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /* Start hub operation. */
|
|---|
| 163 | const usb_device_polling_config_t config = {
|
|---|
| 164 | .debug = 1,
|
|---|
| 165 | .auto_clear_halt = true,
|
|---|
| 166 | .delay = -1,
|
|---|
| 167 | .max_failures = 3,
|
|---|
| 168 | .on_data = hub_port_changes_callback,
|
|---|
| 169 | .on_polling_end = usb_hub_polling_terminated_callback,
|
|---|
| 170 | .on_error = usb_hub_polling_error_callback,
|
|---|
| 171 | .arg = hub_dev,
|
|---|
| 172 | };
|
|---|
| 173 |
|
|---|
| 174 | usb_endpoint_mapping_t *epm =
|
|---|
| 175 | usb_device_get_mapped_ep_desc(hub_dev->usb_device,
|
|---|
| 176 | &hub_status_change_endpoint_description);
|
|---|
| 177 | opResult = usb_device_poll(hub_dev->usb_device, epm, &config,
|
|---|
| 178 | ((hub_dev->port_count + 1 + 7) / 8), &hub_dev->polling);
|
|---|
| 179 |
|
|---|
| 180 | if (opResult != EOK) {
|
|---|
| 181 | /* Function is already bound */
|
|---|
| 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.\n",
|
|---|
| 185 | str_error(opResult));
|
|---|
| 186 | return opResult;
|
|---|
| 187 | }
|
|---|
| 188 | hub_dev->running = true;
|
|---|
| 189 | usb_log_info("Controlling hub '%s' (%p: %zu ports).\n",
|
|---|
| 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 | assert(!hub->running);
|
|---|
| 199 |
|
|---|
| 200 | for (size_t port = 0; port < hub->port_count; ++port) {
|
|---|
| 201 | const int ret = usb_hub_port_fini(&hub->ports[port], hub);
|
|---|
| 202 | if (ret != EOK)
|
|---|
| 203 | return ret;
|
|---|
| 204 | }
|
|---|
| 205 | free(hub->ports);
|
|---|
| 206 |
|
|---|
| 207 | const int ret = ddf_fun_unbind(hub->hub_fun);
|
|---|
| 208 | if (ret != EOK) {
|
|---|
| 209 | usb_log_error("(%p) Failed to unbind '%s' function: %s.",
|
|---|
| 210 | hub, HUB_FNC_NAME, str_error(ret));
|
|---|
| 211 | return ret;
|
|---|
| 212 | }
|
|---|
| 213 | ddf_fun_destroy(hub->hub_fun);
|
|---|
| 214 |
|
|---|
| 215 | usb_log_info("(%p) USB hub driver stopped and cleaned.", hub);
|
|---|
| 216 |
|
|---|
| 217 | /* Device data (usb_hub_dev_t) will be freed by usbdev. */
|
|---|
| 218 | return EOK;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | /**
|
|---|
| 222 | * Turn off power to all ports.
|
|---|
| 223 | *
|
|---|
| 224 | * @param usb_dev generic usb device information
|
|---|
| 225 | * @return error code
|
|---|
| 226 | */
|
|---|
| 227 | int usb_hub_device_remove(usb_device_t *usb_dev)
|
|---|
| 228 | {
|
|---|
| 229 | assert(usb_dev);
|
|---|
| 230 | usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
|
|---|
| 231 | assert(hub);
|
|---|
| 232 |
|
|---|
| 233 | usb_log_info("(%p) USB hub removed, joining polling fibril.", hub);
|
|---|
| 234 |
|
|---|
| 235 | /* Join polling fibril. */
|
|---|
| 236 | usb_device_poll_join(hub->polling);
|
|---|
| 237 | usb_log_info("(%p) USB hub polling stopped, freeing memory.", hub);
|
|---|
| 238 |
|
|---|
| 239 | /* Destroy hub. */
|
|---|
| 240 | return usb_hub_cleanup(hub);
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | /**
|
|---|
| 244 | * Remove all attached devices
|
|---|
| 245 | * @param usb_dev generic usb device information
|
|---|
| 246 | * @return error code
|
|---|
| 247 | */
|
|---|
| 248 | int usb_hub_device_gone(usb_device_t *usb_dev)
|
|---|
| 249 | {
|
|---|
| 250 | assert(usb_dev);
|
|---|
| 251 | usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
|
|---|
| 252 | assert(hub);
|
|---|
| 253 |
|
|---|
| 254 | usb_log_info("(%p) USB hub gone, joining polling fibril.", hub);
|
|---|
| 255 |
|
|---|
| 256 | /* Join polling fibril. */
|
|---|
| 257 | usb_device_poll_join(hub->polling);
|
|---|
| 258 | usb_log_info("(%p) USB hub polling stopped, freeing memory.", hub);
|
|---|
| 259 |
|
|---|
| 260 | /* Destroy hub. */
|
|---|
| 261 | return usb_hub_cleanup(hub);
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | /** Callback for polling hub for changes.
|
|---|
| 265 | *
|
|---|
| 266 | * @param dev Device where the change occured.
|
|---|
| 267 | * @param change_bitmap Bitmap of changed ports.
|
|---|
| 268 | * @param change_bitmap_size Size of the bitmap in bytes.
|
|---|
| 269 | * @param arg Custom argument, points to @c usb_hub_dev_t.
|
|---|
| 270 | * @return Whether to continue polling.
|
|---|
| 271 | */
|
|---|
| 272 | bool hub_port_changes_callback(usb_device_t *dev,
|
|---|
| 273 | uint8_t *change_bitmap, size_t change_bitmap_size, void *arg)
|
|---|
| 274 | {
|
|---|
| 275 | usb_hub_dev_t *hub = arg;
|
|---|
| 276 | assert(hub);
|
|---|
| 277 |
|
|---|
| 278 | /* It is an error condition if we didn't receive enough data */
|
|---|
| 279 | if (change_bitmap_size == 0) {
|
|---|
| 280 | return false;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | /* Lowest bit indicates global change */
|
|---|
| 284 | const bool change = change_bitmap[0] & 1;
|
|---|
| 285 | if (change) {
|
|---|
| 286 | usb_hub_global_interrupt(hub);
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | /* N + 1 bit indicates change on port N */
|
|---|
| 290 | for (size_t port = 0; port < hub->port_count; ++port) {
|
|---|
| 291 | const size_t bit = port + 1;
|
|---|
| 292 | const bool change = (change_bitmap[bit / 8] >> (bit % 8)) & 1;
|
|---|
| 293 | if (change) {
|
|---|
| 294 | usb_hub_port_process_interrupt(&hub->ports[port], hub);
|
|---|
| 295 | }
|
|---|
| 296 | }
|
|---|
| 297 | return true;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | /**
|
|---|
| 301 | * Load hub-specific information into hub_dev structure and process if needed
|
|---|
| 302 | *
|
|---|
| 303 | * Read port count and initialize structures holding per port information.
|
|---|
| 304 | * If there are any non-removable devices, start initializing them.
|
|---|
| 305 | * This function is hub-specific and should be run only after the hub is
|
|---|
| 306 | * configured using usb_set_first_configuration function.
|
|---|
| 307 | * @param hub_dev hub representation
|
|---|
| 308 | * @return error code
|
|---|
| 309 | */
|
|---|
| 310 | static int usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev)
|
|---|
| 311 | {
|
|---|
| 312 | assert(hub_dev);
|
|---|
| 313 |
|
|---|
| 314 | /* Get hub descriptor. */
|
|---|
| 315 | usb_log_debug("(%p): Retrieving descriptor.", hub_dev);
|
|---|
| 316 | usb_pipe_t *control_pipe =
|
|---|
| 317 | usb_device_get_default_pipe(hub_dev->usb_device);
|
|---|
| 318 |
|
|---|
| 319 | usb_hub_descriptor_header_t descriptor;
|
|---|
| 320 | size_t received_size;
|
|---|
| 321 | int opResult = usb_request_get_descriptor(control_pipe,
|
|---|
| 322 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
|
|---|
| 323 | USB_DESCTYPE_HUB, 0, 0, &descriptor,
|
|---|
| 324 | sizeof(usb_hub_descriptor_header_t), &received_size);
|
|---|
| 325 | if (opResult != EOK) {
|
|---|
| 326 | usb_log_error("(%p): Failed to receive hub descriptor: %s.\n",
|
|---|
| 327 | hub_dev, str_error(opResult));
|
|---|
| 328 | return opResult;
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | usb_log_debug("(%p): Setting port count to %d.\n", hub_dev,
|
|---|
| 332 | descriptor.port_count);
|
|---|
| 333 | hub_dev->port_count = descriptor.port_count;
|
|---|
| 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(
|
|---|
| 342 | &hub_dev->ports[port], port + 1, control_pipe);
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | hub_dev->power_switched =
|
|---|
| 346 | !(descriptor.characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
|
|---|
| 347 | hub_dev->per_port_power =
|
|---|
| 348 | descriptor.characteristics & HUB_CHAR_POWER_PER_PORT_FLAG;
|
|---|
| 349 |
|
|---|
| 350 | if (!hub_dev->power_switched) {
|
|---|
| 351 | usb_log_info("(%p): Power switching not supported, "
|
|---|
| 352 | "ports always powered.", hub_dev);
|
|---|
| 353 | return EOK;
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | usb_log_info("(%p): Hub port power switching enabled (%s).\n", hub_dev,
|
|---|
| 357 | hub_dev->per_port_power ? "per port" : "ganged");
|
|---|
| 358 |
|
|---|
| 359 | for (unsigned int port = 0; port < hub_dev->port_count; ++port) {
|
|---|
| 360 | usb_log_debug("(%p): Powering port %u.", hub_dev, port);
|
|---|
| 361 | const int ret = usb_hub_port_set_feature(
|
|---|
| 362 | &hub_dev->ports[port], USB_HUB_FEATURE_PORT_POWER);
|
|---|
| 363 |
|
|---|
| 364 | if (ret != EOK) {
|
|---|
| 365 | usb_log_error("(%p-%u): Cannot power on port: %s.\n",
|
|---|
| 366 | hub_dev, hub_dev->ports[port].port_number,
|
|---|
| 367 | str_error(ret));
|
|---|
| 368 | } else {
|
|---|
| 369 | if (!hub_dev->per_port_power) {
|
|---|
| 370 | usb_log_debug("(%p) Ganged power switching, "
|
|---|
| 371 | "one port is enough.", hub_dev);
|
|---|
| 372 | break;
|
|---|
| 373 | }
|
|---|
| 374 | }
|
|---|
| 375 | }
|
|---|
| 376 | return EOK;
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | /**
|
|---|
| 380 | * Set configuration of and USB device
|
|---|
| 381 | *
|
|---|
| 382 | * Check whether there is at least one configuration and sets the first one.
|
|---|
| 383 | * This function should be run prior to running any hub-specific action.
|
|---|
| 384 | * @param usb_device usb device representation
|
|---|
| 385 | * @return error code
|
|---|
| 386 | */
|
|---|
| 387 | static int usb_set_first_configuration(usb_device_t *usb_device)
|
|---|
| 388 | {
|
|---|
| 389 | assert(usb_device);
|
|---|
| 390 | /* Get number of possible configurations from device descriptor */
|
|---|
| 391 | const size_t configuration_count =
|
|---|
| 392 | usb_device_descriptors(usb_device)->device.configuration_count;
|
|---|
| 393 | usb_log_debug("Hub has %zu configurations.\n", configuration_count);
|
|---|
| 394 |
|
|---|
| 395 | if (configuration_count < 1) {
|
|---|
| 396 | usb_log_error("There are no configurations available\n");
|
|---|
| 397 | return EINVAL;
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | const size_t config_size =
|
|---|
| 401 | usb_device_descriptors(usb_device)->full_config_size;
|
|---|
| 402 | const usb_standard_configuration_descriptor_t *config_descriptor =
|
|---|
| 403 | usb_device_descriptors(usb_device)->full_config;
|
|---|
| 404 |
|
|---|
| 405 | if (config_size < sizeof(usb_standard_configuration_descriptor_t)) {
|
|---|
| 406 | usb_log_error("Configuration descriptor is not big enough"
|
|---|
| 407 | " to fit standard configuration descriptor.\n");
|
|---|
| 408 | return EOVERFLOW;
|
|---|
| 409 | }
|
|---|
| 410 |
|
|---|
| 411 | /* Set configuration. Use the configuration that was in
|
|---|
| 412 | * usb_device->descriptors.configuration i.e. The first one. */
|
|---|
| 413 | const int opResult = usb_request_set_configuration(
|
|---|
| 414 | usb_device_get_default_pipe(usb_device),
|
|---|
| 415 | config_descriptor->configuration_number);
|
|---|
| 416 | if (opResult != EOK) {
|
|---|
| 417 | usb_log_error("Failed to set hub configuration: %s.\n",
|
|---|
| 418 | str_error(opResult));
|
|---|
| 419 | } else {
|
|---|
| 420 | usb_log_debug("\tUsed configuration %d\n",
|
|---|
| 421 | config_descriptor->configuration_number);
|
|---|
| 422 | }
|
|---|
| 423 | return opResult;
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | /**
|
|---|
| 427 | * Process hub over current change
|
|---|
| 428 | *
|
|---|
| 429 | * This means either to power off the hub or power it on.
|
|---|
| 430 | * @param hub_dev hub instance
|
|---|
| 431 | * @param status hub status bitmask
|
|---|
| 432 | * @return error code
|
|---|
| 433 | */
|
|---|
| 434 | static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
|
|---|
| 435 | usb_hub_status_t status)
|
|---|
| 436 | {
|
|---|
| 437 | if (status & USB_HUB_STATUS_OVER_CURRENT) {
|
|---|
| 438 | /* Hub should remove power from all ports if it detects OC */
|
|---|
| 439 | usb_log_warning("(%p) Detected hub over-current condition, "
|
|---|
| 440 | "all ports should be powered off.", hub_dev);
|
|---|
| 441 | return;
|
|---|
| 442 | }
|
|---|
| 443 |
|
|---|
| 444 | /* Ports are always powered. */
|
|---|
| 445 | if (!hub_dev->power_switched)
|
|---|
| 446 | return;
|
|---|
| 447 |
|
|---|
| 448 | /* Over-current condition is gone, it is safe to turn the ports on. */
|
|---|
| 449 | for (size_t port = 0; port < hub_dev->port_count; ++port) {
|
|---|
| 450 | const int ret = usb_hub_port_set_feature(
|
|---|
| 451 | &hub_dev->ports[port], USB_HUB_FEATURE_PORT_POWER);
|
|---|
| 452 | if (ret != EOK) {
|
|---|
| 453 | usb_log_warning("(%p-%u): HUB OVER-CURRENT GONE: Cannot"
|
|---|
| 454 | " power on port: %s\n", hub_dev,
|
|---|
| 455 | hub_dev->ports[port].port_number, str_error(ret));
|
|---|
| 456 | } else {
|
|---|
| 457 | if (!hub_dev->per_port_power)
|
|---|
| 458 | return;
|
|---|
| 459 | }
|
|---|
| 460 | }
|
|---|
| 461 |
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | /**
|
|---|
| 465 | * Process hub interrupts.
|
|---|
| 466 | *
|
|---|
| 467 | * The change can be either in the over-current condition or local-power change.
|
|---|
| 468 | * @param hub_dev hub instance
|
|---|
| 469 | */
|
|---|
| 470 | static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev)
|
|---|
| 471 | {
|
|---|
| 472 | assert(hub_dev);
|
|---|
| 473 | assert(hub_dev->usb_device);
|
|---|
| 474 | usb_log_debug("(%p): Global interrupt on th hub.", hub_dev);
|
|---|
| 475 | usb_pipe_t *control_pipe =
|
|---|
| 476 | usb_device_get_default_pipe(hub_dev->usb_device);
|
|---|
| 477 |
|
|---|
| 478 | usb_hub_status_t status;
|
|---|
| 479 | size_t rcvd_size;
|
|---|
| 480 | /* NOTE: We can't use standard USB GET_STATUS request, because
|
|---|
| 481 | * hubs reply is 4byte instead of 2 */
|
|---|
| 482 | const int opResult = usb_pipe_control_read(control_pipe,
|
|---|
| 483 | &get_hub_status_request, sizeof(get_hub_status_request),
|
|---|
| 484 | &status, sizeof(usb_hub_status_t), &rcvd_size);
|
|---|
| 485 | if (opResult != EOK) {
|
|---|
| 486 | usb_log_error("(%p): Could not get hub status: %s.", hub_dev,
|
|---|
| 487 | str_error(opResult));
|
|---|
| 488 | return;
|
|---|
| 489 | }
|
|---|
| 490 | if (rcvd_size != sizeof(usb_hub_status_t)) {
|
|---|
| 491 | usb_log_error("(%p): Received status has incorrect size: "
|
|---|
| 492 | "%zu != %zu", hub_dev, rcvd_size, sizeof(usb_hub_status_t));
|
|---|
| 493 | return;
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | /* Handle status changes */
|
|---|
| 497 | if (status & USB_HUB_STATUS_C_OVER_CURRENT) {
|
|---|
| 498 | usb_hub_over_current(hub_dev, status);
|
|---|
| 499 | /* Ack change in hub OC flag */
|
|---|
| 500 | const int ret = usb_request_clear_feature(
|
|---|
| 501 | control_pipe, USB_REQUEST_TYPE_CLASS,
|
|---|
| 502 | USB_REQUEST_RECIPIENT_DEVICE,
|
|---|
| 503 | USB_HUB_FEATURE_C_HUB_OVER_CURRENT, 0);
|
|---|
| 504 | if (ret != EOK) {
|
|---|
| 505 | usb_log_error("(%p): Failed to clear hub over-current "
|
|---|
| 506 | "change flag: %s.\n", hub_dev, str_error(opResult));
|
|---|
| 507 | }
|
|---|
| 508 | }
|
|---|
| 509 |
|
|---|
| 510 | if (status & USB_HUB_STATUS_C_LOCAL_POWER) {
|
|---|
| 511 | /* NOTE: Handling this is more complicated.
|
|---|
| 512 | * If the transition is from bus power to local power, all
|
|---|
| 513 | * is good and we may signal the parent hub that we don't
|
|---|
| 514 | * need the power.
|
|---|
| 515 | * If the transition is from local power to bus power
|
|---|
| 516 | * the hub should turn off all the ports and devices need
|
|---|
| 517 | * to be reinitialized taking into account the limited power
|
|---|
| 518 | * that is now available.
|
|---|
| 519 | * There is no support for power distribution in HelenOS,
|
|---|
| 520 | * (or other OSes/hub devices that I've seen) so this is not
|
|---|
| 521 | * implemented.
|
|---|
| 522 | * Just ACK the change.
|
|---|
| 523 | */
|
|---|
| 524 | const int ret = usb_request_clear_feature(
|
|---|
| 525 | control_pipe, USB_REQUEST_TYPE_CLASS,
|
|---|
| 526 | USB_REQUEST_RECIPIENT_DEVICE,
|
|---|
| 527 | USB_HUB_FEATURE_C_HUB_LOCAL_POWER, 0);
|
|---|
| 528 | if (opResult != EOK) {
|
|---|
| 529 | usb_log_error("(%p): Failed to clear hub power change "
|
|---|
| 530 | "flag: %s.\n", hub_dev, str_error(ret));
|
|---|
| 531 | }
|
|---|
| 532 | }
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| 535 | /**
|
|---|
| 536 | * callback called from hub polling fibril when the fibril terminates
|
|---|
| 537 | *
|
|---|
| 538 | * Does not perform cleanup, just marks the hub as not running.
|
|---|
| 539 | * @param device usb device afected
|
|---|
| 540 | * @param was_error indicates that the fibril is stoped due to an error
|
|---|
| 541 | * @param data pointer to usb_hub_dev_t structure
|
|---|
| 542 | */
|
|---|
| 543 | static void usb_hub_polling_terminated_callback(usb_device_t *device,
|
|---|
| 544 | bool was_error, void *data)
|
|---|
| 545 | {
|
|---|
| 546 | usb_hub_dev_t *hub = data;
|
|---|
| 547 | assert(hub);
|
|---|
| 548 |
|
|---|
| 549 | fibril_mutex_lock(&hub->pending_ops_mutex);
|
|---|
| 550 |
|
|---|
| 551 | /* The device is dead. However there might be some pending operations
|
|---|
| 552 | * that we need to wait for.
|
|---|
| 553 | * One of them is device adding in progress.
|
|---|
| 554 | * The respective fibril is probably waiting for status change
|
|---|
| 555 | * in port reset (port enable) callback.
|
|---|
| 556 | * Such change would never come (otherwise we would not be here).
|
|---|
| 557 | * Thus, we would flush all pending port resets.
|
|---|
| 558 | */
|
|---|
| 559 | if (hub->pending_ops_count > 0) {
|
|---|
| 560 | for (size_t port = 0; port < hub->port_count; ++port) {
|
|---|
| 561 | usb_hub_port_reset_fail(&hub->ports[port]);
|
|---|
| 562 | }
|
|---|
| 563 | }
|
|---|
| 564 | /* And now wait for them. */
|
|---|
| 565 | while (hub->pending_ops_count > 0) {
|
|---|
| 566 | fibril_condvar_wait(&hub->pending_ops_cv,
|
|---|
| 567 | &hub->pending_ops_mutex);
|
|---|
| 568 | }
|
|---|
| 569 | fibril_mutex_unlock(&hub->pending_ops_mutex);
|
|---|
| 570 | hub->running = false;
|
|---|
| 571 | }
|
|---|
| 572 |
|
|---|
| 573 | /**
|
|---|
| 574 | * @}
|
|---|
| 575 | */
|
|---|