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