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