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