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