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