[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 | */
|
---|
[58563585] | 29 |
|
---|
[281ebae] | 30 | /** @addtogroup drvusbhub
|
---|
[e080332] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | * @brief usb hub main functionality
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[eb1a2f4] | 37 | #include <ddf/driver.h>
|
---|
[3e6a98c5] | 38 | #include <stdbool.h>
|
---|
[e080332] | 39 | #include <errno.h>
|
---|
[4e8e1f5] | 40 | #include <str_error.h>
|
---|
[3e490eb] | 41 | #include <inttypes.h>
|
---|
[193da9d6] | 42 | #include <stdio.h>
|
---|
[e080332] | 43 |
|
---|
[193da9d6] | 44 | #include <usb/usb.h>
|
---|
[6c5abf9] | 45 | #include <usb/debug.h>
|
---|
[193da9d6] | 46 | #include <usb/dev/pipes.h>
|
---|
| 47 | #include <usb/classes/classes.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>
|
---|
[3cdaa7f] | 53 | #include <usbhc_iface.h>
|
---|
[e080332] | 54 |
|
---|
| 55 | #include "usbhub.h"
|
---|
[400f363] | 56 | #include "status.h"
|
---|
[e080332] | 57 |
|
---|
[d46b13d] | 58 | #define HUB_FNC_NAME "hub"
|
---|
[58563585] | 59 |
|
---|
[bb70637] | 60 | /** Hub status-change endpoint description.
|
---|
| 61 | *
|
---|
| 62 | * For more information see section 11.15.1 of USB 1.1 specification.
|
---|
| 63 | */
|
---|
| 64 | const usb_endpoint_description_t hub_status_change_endpoint_description =
|
---|
| 65 | {
|
---|
| 66 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
---|
| 67 | .direction = USB_DIRECTION_IN,
|
---|
| 68 | .interface_class = USB_CLASS_HUB,
|
---|
| 69 | .interface_subclass = 0,
|
---|
| 70 | .interface_protocol = 0,
|
---|
| 71 | .flags = 0
|
---|
| 72 | };
|
---|
[df3ad97] | 73 |
|
---|
[bf73a02] | 74 | /** Standard get hub global status request */
|
---|
| 75 | static const usb_device_request_setup_packet_t get_hub_status_request = {
|
---|
| 76 | .request_type = USB_HUB_REQ_TYPE_GET_HUB_STATUS,
|
---|
| 77 | .request = USB_HUB_REQUEST_GET_STATUS,
|
---|
[bba0f1fc] | 78 | .index = 0,
|
---|
[bf73a02] | 79 | .value = 0,
|
---|
| 80 | .length = sizeof(usb_hub_status_t),
|
---|
| 81 | };
|
---|
| 82 |
|
---|
[193da9d6] | 83 | static int usb_set_first_configuration(usb_device_t *usb_device);
|
---|
[eda7a4e0] | 84 | static int usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev);
|
---|
| 85 | static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
|
---|
[f35b294] | 86 | usb_hub_status_t status);
|
---|
[eda7a4e0] | 87 | static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev);
|
---|
[205f0766] | 88 |
|
---|
[0b90f49] | 89 | static bool usb_hub_polling_error_callback(usb_device_t *dev, int err_code, void *arg)
|
---|
| 90 | {
|
---|
| 91 | assert(dev);
|
---|
| 92 | assert(arg);
|
---|
| 93 |
|
---|
[c4e84ed6] | 94 | usb_log_error("Device %s polling error: %s", usb_device_get_name(dev), str_error(err_code));
|
---|
[0b90f49] | 95 |
|
---|
[c4e84ed6] | 96 | return true;
|
---|
[0b90f49] | 97 | }
|
---|
| 98 |
|
---|
[a209648] | 99 | /**
|
---|
[54d1ad9] | 100 | * Initialize hub device driver structure.
|
---|
[a209648] | 101 | *
|
---|
[ea6de35] | 102 | * Creates hub representation and fibril that periodically checks hub's status.
|
---|
[a209648] | 103 | * Hub representation is passed to the fibril.
|
---|
| 104 | * @param usb_dev generic usb device information
|
---|
| 105 | * @return error code
|
---|
| 106 | */
|
---|
[1a4ea01d] | 107 | int usb_hub_device_add(usb_device_t *usb_dev)
|
---|
[d46b13d] | 108 | {
|
---|
[193da9d6] | 109 | assert(usb_dev);
|
---|
| 110 | /* Create driver soft-state structure */
|
---|
[e27c2476] | 111 | usb_hub_dev_t *hub_dev =
|
---|
| 112 | usb_device_data_alloc(usb_dev, sizeof(usb_hub_dev_t));
|
---|
[eda7a4e0] | 113 | if (hub_dev == NULL) {
|
---|
[a1732929] | 114 | usb_log_error("Failed to create hub driver structure.");
|
---|
[193da9d6] | 115 | return ENOMEM;
|
---|
| 116 | }
|
---|
[e27c2476] | 117 | hub_dev->usb_device = usb_dev;
|
---|
[129b821f] | 118 | hub_dev->speed = usb_device_get_speed(usb_dev);
|
---|
[ee9ea16] | 119 |
|
---|
[51a51be] | 120 | fibril_mutex_initialize(&hub_dev->default_address_guard);
|
---|
| 121 | fibril_condvar_initialize(&hub_dev->default_address_cv);
|
---|
| 122 |
|
---|
[193da9d6] | 123 | /* Set hub's first configuration. (There should be only one) */
|
---|
[d93f5afb] | 124 | int opResult = usb_set_first_configuration(usb_dev);
|
---|
[a209648] | 125 | if (opResult != EOK) {
|
---|
[a1732929] | 126 | usb_log_error("Could not set hub configuration: %s",
|
---|
[5c1a65e] | 127 | str_error(opResult));
|
---|
[a209648] | 128 | return opResult;
|
---|
| 129 | }
|
---|
[d46b13d] | 130 |
|
---|
[205f0766] | 131 | /* Get port count and create attached_devices. */
|
---|
[eda7a4e0] | 132 | opResult = usb_hub_process_hub_specific_info(hub_dev);
|
---|
[a209648] | 133 | if (opResult != EOK) {
|
---|
[a1732929] | 134 | usb_log_error("Could process hub specific info, %s",
|
---|
[5c1a65e] | 135 | str_error(opResult));
|
---|
[a209648] | 136 | return opResult;
|
---|
| 137 | }
|
---|
[9063484] | 138 |
|
---|
[54d1ad9] | 139 | /* Create hub control function. */
|
---|
[a1732929] | 140 | usb_log_debug("Creating DDF function '" HUB_FNC_NAME "'.");
|
---|
[6785b538] | 141 | hub_dev->hub_fun = usb_device_ddf_fun_create(hub_dev->usb_device,
|
---|
[d46b13d] | 142 | fun_exposed, HUB_FNC_NAME);
|
---|
[eda7a4e0] | 143 | if (hub_dev->hub_fun == NULL) {
|
---|
[a1732929] | 144 | usb_log_error("Failed to create hub function.");
|
---|
[d46b13d] | 145 | return ENOMEM;
|
---|
| 146 | }
|
---|
[a209648] | 147 |
|
---|
[54d1ad9] | 148 | /* Bind hub control function. */
|
---|
[eda7a4e0] | 149 | opResult = ddf_fun_bind(hub_dev->hub_fun);
|
---|
[d46b13d] | 150 | if (opResult != EOK) {
|
---|
[a1732929] | 151 | usb_log_error("Failed to bind hub function: %s.",
|
---|
[d46b13d] | 152 | str_error(opResult));
|
---|
[eda7a4e0] | 153 | ddf_fun_destroy(hub_dev->hub_fun);
|
---|
[d46b13d] | 154 | return opResult;
|
---|
| 155 | }
|
---|
[3e490eb] | 156 |
|
---|
[54d1ad9] | 157 | /* Start hub operation. */
|
---|
[8b71f3e] | 158 | usb_polling_t *polling = &hub_dev->polling;
|
---|
| 159 | opResult = usb_polling_init(polling);
|
---|
| 160 | if (opResult != EOK) {
|
---|
[01d9707] | 161 | /* Function is already bound */
|
---|
[8b71f3e] | 162 | ddf_fun_unbind(hub_dev->hub_fun);
|
---|
| 163 | ddf_fun_destroy(hub_dev->hub_fun);
|
---|
[a1732929] | 164 | usb_log_error("Failed to initialize polling fibril: %s.",
|
---|
[8b71f3e] | 165 | str_error(opResult));
|
---|
| 166 | return opResult;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | polling->device = hub_dev->usb_device;
|
---|
| 170 | polling->ep_mapping = usb_device_get_mapped_ep_desc(hub_dev->usb_device,
|
---|
[7dddd7b] | 171 | &hub_status_change_endpoint_description);
|
---|
[8b71f3e] | 172 | polling->request_size = ((hub_dev->port_count + 1 + 7) / 8);
|
---|
| 173 | polling->buffer = malloc(polling->request_size);
|
---|
| 174 | polling->on_data = hub_port_changes_callback;
|
---|
| 175 | polling->on_error = usb_hub_polling_error_callback;
|
---|
| 176 | polling->arg = hub_dev;
|
---|
| 177 |
|
---|
| 178 | opResult = usb_polling_start(polling);
|
---|
[d46b13d] | 179 | if (opResult != EOK) {
|
---|
[01d9707] | 180 | /* Polling is already initialized. */
|
---|
[8b71f3e] | 181 | free(polling->buffer);
|
---|
[01d9707] | 182 | usb_polling_fini(polling);
|
---|
[eda7a4e0] | 183 | ddf_fun_unbind(hub_dev->hub_fun);
|
---|
| 184 | ddf_fun_destroy(hub_dev->hub_fun);
|
---|
[a1732929] | 185 | usb_log_error("Failed to create polling fibril: %s.",
|
---|
[d46b13d] | 186 | str_error(opResult));
|
---|
| 187 | return opResult;
|
---|
| 188 | }
|
---|
[8b71f3e] | 189 |
|
---|
[129b821f] | 190 | usb_log_info("Controlling %s-speed hub '%s' (%p: %zu ports).",
|
---|
| 191 | usb_str_speed(hub_dev->speed),
|
---|
[e98e5fc] | 192 | usb_device_get_name(hub_dev->usb_device), hub_dev,
|
---|
| 193 | hub_dev->port_count);
|
---|
[d46b13d] | 194 |
|
---|
| 195 | return EOK;
|
---|
[a209648] | 196 | }
|
---|
[76fbd9a] | 197 |
|
---|
[0b90f49] | 198 | static int usb_hub_cleanup(usb_hub_dev_t *hub)
|
---|
[5a73a7e] | 199 | {
|
---|
[8b71f3e] | 200 | free(hub->polling.buffer);
|
---|
| 201 | usb_polling_fini(&hub->polling);
|
---|
| 202 |
|
---|
[0b90f49] | 203 | for (size_t port = 0; port < hub->port_count; ++port) {
|
---|
[94f8c363] | 204 | usb_port_fini(&hub->ports[port].base);
|
---|
[0b90f49] | 205 | }
|
---|
| 206 | free(hub->ports);
|
---|
| 207 |
|
---|
| 208 | const int ret = ddf_fun_unbind(hub->hub_fun);
|
---|
| 209 | if (ret != EOK) {
|
---|
| 210 | usb_log_error("(%p) Failed to unbind '%s' function: %s.",
|
---|
| 211 | hub, HUB_FNC_NAME, str_error(ret));
|
---|
| 212 | return ret;
|
---|
| 213 | }
|
---|
| 214 | ddf_fun_destroy(hub->hub_fun);
|
---|
| 215 |
|
---|
| 216 | usb_log_info("(%p) USB hub driver stopped and cleaned.", hub);
|
---|
| 217 |
|
---|
| 218 | /* Device data (usb_hub_dev_t) will be freed by usbdev. */
|
---|
[5a73a7e] | 219 | return EOK;
|
---|
[54d1ad9] | 220 | }
|
---|
[76fbd9a] | 221 |
|
---|
[91173333] | 222 | /**
|
---|
| 223 | * Turn off power to all ports.
|
---|
| 224 | *
|
---|
| 225 | * @param usb_dev generic usb device information
|
---|
| 226 | * @return error code
|
---|
| 227 | */
|
---|
| 228 | int usb_hub_device_remove(usb_device_t *usb_dev)
|
---|
[0b90f49] | 229 | {
|
---|
| 230 | assert(usb_dev);
|
---|
| 231 | usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
|
---|
| 232 | assert(hub);
|
---|
| 233 |
|
---|
[91173333] | 234 | usb_log_info("(%p) USB hub removed, joining polling fibril.", hub);
|
---|
[0b90f49] | 235 |
|
---|
[8b71f3e] | 236 | /* Join polling fibril (ignoring error code). */
|
---|
| 237 | usb_polling_join(&hub->polling);
|
---|
[0b90f49] | 238 | usb_log_info("(%p) USB hub polling stopped, freeing memory.", hub);
|
---|
| 239 |
|
---|
| 240 | /* Destroy hub. */
|
---|
| 241 | return usb_hub_cleanup(hub);
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[54d1ad9] | 244 | /**
|
---|
| 245 | * Remove all attached devices
|
---|
| 246 | * @param usb_dev generic usb device information
|
---|
| 247 | * @return error code
|
---|
| 248 | */
|
---|
| 249 | int usb_hub_device_gone(usb_device_t *usb_dev)
|
---|
| 250 | {
|
---|
| 251 | assert(usb_dev);
|
---|
[0f4bff8] | 252 | usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
|
---|
[54d1ad9] | 253 | assert(hub);
|
---|
[0b90f49] | 254 |
|
---|
| 255 | usb_log_info("(%p) USB hub gone, joining polling fibril.", hub);
|
---|
| 256 |
|
---|
[8b71f3e] | 257 | /* Join polling fibril (ignoring error code). */
|
---|
| 258 | usb_polling_join(&hub->polling);
|
---|
[91173333] | 259 | usb_log_info("(%p) USB hub polling stopped, freeing memory.", hub);
|
---|
[54d1ad9] | 260 |
|
---|
[b233821] | 261 | /* Destroy hub. */
|
---|
[0b90f49] | 262 | return usb_hub_cleanup(hub);
|
---|
[54d1ad9] | 263 | }
|
---|
[76fbd9a] | 264 |
|
---|
[1e1b1a9] | 265 | /** Callback for polling hub for changes.
|
---|
[3e490eb] | 266 | *
|
---|
| 267 | * @param dev Device where the change occured.
|
---|
| 268 | * @param change_bitmap Bitmap of changed ports.
|
---|
| 269 | * @param change_bitmap_size Size of the bitmap in bytes.
|
---|
[6626bba9] | 270 | * @param arg Custom argument, points to @c usb_hub_dev_t.
|
---|
[3e490eb] | 271 | * @return Whether to continue polling.
|
---|
| 272 | */
|
---|
| 273 | bool hub_port_changes_callback(usb_device_t *dev,
|
---|
[983e135] | 274 | uint8_t *change_bitmap, size_t change_bitmap_size, void *arg)
|
---|
| 275 | {
|
---|
[6626bba9] | 276 | usb_hub_dev_t *hub = arg;
|
---|
[0212751] | 277 | assert(hub);
|
---|
[3e490eb] | 278 |
|
---|
[0212751] | 279 | /* It is an error condition if we didn't receive enough data */
|
---|
[3e490eb] | 280 | if (change_bitmap_size == 0) {
|
---|
[0212751] | 281 | return false;
|
---|
[3e490eb] | 282 | }
|
---|
| 283 |
|
---|
[983e135] | 284 | /* Lowest bit indicates global change */
|
---|
| 285 | const bool change = change_bitmap[0] & 1;
|
---|
[f35b294] | 286 | if (change) {
|
---|
[ea6de35] | 287 | usb_hub_global_interrupt(hub);
|
---|
[1e1b1a9] | 288 | }
|
---|
| 289 |
|
---|
[983e135] | 290 | /* N + 1 bit indicates change on port N */
|
---|
[ea30cc1] | 291 | for (size_t port = 0; port < hub->port_count; ++port) {
|
---|
[a14d6a7] | 292 | const size_t bit = port + 1;
|
---|
| 293 | const bool change = (change_bitmap[bit / 8] >> (bit % 8)) & 1;
|
---|
[3e490eb] | 294 | if (change) {
|
---|
[94f8c363] | 295 | usb_hub_port_process_interrupt(&hub->ports[port]);
|
---|
[3e490eb] | 296 | }
|
---|
| 297 | }
|
---|
| 298 | return true;
|
---|
| 299 | }
|
---|
[76fbd9a] | 300 |
|
---|
[09daa8b] | 301 | /**
|
---|
[eda7a4e0] | 302 | * Load hub-specific information into hub_dev structure and process if needed
|
---|
[09daa8b] | 303 | *
|
---|
[ea6de35] | 304 | * Read port count and initialize structures holding per port information.
|
---|
| 305 | * If there are any non-removable devices, start initializing them.
|
---|
[09daa8b] | 306 | * This function is hub-specific and should be run only after the hub is
|
---|
[193da9d6] | 307 | * configured using usb_set_first_configuration function.
|
---|
[eda7a4e0] | 308 | * @param hub_dev hub representation
|
---|
[09daa8b] | 309 | * @return error code
|
---|
| 310 | */
|
---|
[eda7a4e0] | 311 | static int usb_hub_process_hub_specific_info(usb_hub_dev_t *hub_dev)
|
---|
[5fd0dc23] | 312 | {
|
---|
[eda7a4e0] | 313 | assert(hub_dev);
|
---|
[ea6de35] | 314 |
|
---|
| 315 | /* Get hub descriptor. */
|
---|
[e98e5fc] | 316 | usb_log_debug("(%p): Retrieving descriptor.", hub_dev);
|
---|
[0f4bff8] | 317 | usb_pipe_t *control_pipe =
|
---|
| 318 | usb_device_get_default_pipe(hub_dev->usb_device);
|
---|
[09daa8b] | 319 |
|
---|
[621ba8c] | 320 | usb_hub_descriptor_header_t descriptor;
|
---|
[09daa8b] | 321 | size_t received_size;
|
---|
[621ba8c] | 322 | int opResult = usb_request_get_descriptor(control_pipe,
|
---|
[f35b294] | 323 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
[621ba8c] | 324 | USB_DESCTYPE_HUB, 0, 0, &descriptor,
|
---|
[32cd37f] | 325 | sizeof(usb_hub_descriptor_header_t), &received_size);
|
---|
[09daa8b] | 326 | if (opResult != EOK) {
|
---|
[a1732929] | 327 | usb_log_error("(%p): Failed to receive hub descriptor: %s.",
|
---|
[e98e5fc] | 328 | hub_dev, str_error(opResult));
|
---|
[15b0432] | 329 | return opResult;
|
---|
| 330 | }
|
---|
[621ba8c] | 331 |
|
---|
[a1732929] | 332 | usb_log_debug("(%p): Setting port count to %d.", hub_dev,
|
---|
[e98e5fc] | 333 | descriptor.port_count);
|
---|
[eda7a4e0] | 334 | hub_dev->port_count = descriptor.port_count;
|
---|
[c4e84ed6] | 335 | hub_dev->control_pipe = control_pipe;
|
---|
[5fd0dc23] | 336 |
|
---|
[a14d6a7] | 337 | hub_dev->ports = calloc(hub_dev->port_count, sizeof(usb_hub_port_t));
|
---|
[eda7a4e0] | 338 | if (!hub_dev->ports) {
|
---|
[361fcec] | 339 | return ENOMEM;
|
---|
| 340 | }
|
---|
[5fd0dc23] | 341 |
|
---|
[a14d6a7] | 342 | for (size_t port = 0; port < hub_dev->port_count; ++port) {
|
---|
[c4e84ed6] | 343 | usb_hub_port_init(&hub_dev->ports[port], hub_dev, port + 1);
|
---|
[15b0432] | 344 | }
|
---|
[5fd0dc23] | 345 |
|
---|
[54d1ad9] | 346 | hub_dev->power_switched =
|
---|
[621ba8c] | 347 | !(descriptor.characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
|
---|
[54d1ad9] | 348 | hub_dev->per_port_power =
|
---|
| 349 | descriptor.characteristics & HUB_CHAR_POWER_PER_PORT_FLAG;
|
---|
| 350 |
|
---|
| 351 | if (!hub_dev->power_switched) {
|
---|
[e98e5fc] | 352 | usb_log_info("(%p): Power switching not supported, "
|
---|
| 353 | "ports always powered.", hub_dev);
|
---|
[54d1ad9] | 354 | return EOK;
|
---|
| 355 | }
|
---|
| 356 |
|
---|
[a1732929] | 357 | usb_log_info("(%p): Hub port power switching enabled (%s).", hub_dev,
|
---|
[dc1d499] | 358 | hub_dev->per_port_power ? "per port" : "ganged");
|
---|
[54d1ad9] | 359 |
|
---|
[58563585] | 360 | for (unsigned int port = 0; port < hub_dev->port_count; ++port) {
|
---|
[34c9cfc] | 361 | usb_log_debug("(%p): Powering port %u.", hub_dev, port);
|
---|
[c4e84ed6] | 362 | const int ret = usb_hub_set_port_feature(hub_dev, port, USB_HUB_FEATURE_PORT_POWER);
|
---|
[54d1ad9] | 363 |
|
---|
| 364 | if (ret != EOK) {
|
---|
[a1732929] | 365 | usb_log_error("(%p-%u): Cannot power on port: %s.",
|
---|
[e98e5fc] | 366 | hub_dev, hub_dev->ports[port].port_number,
|
---|
| 367 | str_error(ret));
|
---|
[54d1ad9] | 368 | } else {
|
---|
| 369 | if (!hub_dev->per_port_power) {
|
---|
[e98e5fc] | 370 | usb_log_debug("(%p) Ganged power switching, "
|
---|
| 371 | "one port is enough.", hub_dev);
|
---|
[54d1ad9] | 372 | break;
|
---|
[361fcec] | 373 | }
|
---|
| 374 | }
|
---|
[15b0432] | 375 | }
|
---|
[206f71a] | 376 | return EOK;
|
---|
[15b0432] | 377 | }
|
---|
[76fbd9a] | 378 |
|
---|
[15b0432] | 379 | /**
|
---|
[193da9d6] | 380 | * Set configuration of and USB device
|
---|
[09daa8b] | 381 | *
|
---|
| 382 | * Check whether there is at least one configuration and sets the first one.
|
---|
| 383 | * This function should be run prior to running any hub-specific action.
|
---|
[193da9d6] | 384 | * @param usb_device usb device representation
|
---|
[df3ad97] | 385 | * @return error code
|
---|
[15b0432] | 386 | */
|
---|
[193da9d6] | 387 | static int usb_set_first_configuration(usb_device_t *usb_device)
|
---|
[983e135] | 388 | {
|
---|
[193da9d6] | 389 | assert(usb_device);
|
---|
| 390 | /* Get number of possible configurations from device descriptor */
|
---|
| 391 | const size_t configuration_count =
|
---|
[e2dfa86] | 392 | usb_device_descriptors(usb_device)->device.configuration_count;
|
---|
[a1732929] | 393 | usb_log_debug("Hub has %zu configurations.", configuration_count);
|
---|
[983e135] | 394 |
|
---|
[193da9d6] | 395 | if (configuration_count < 1) {
|
---|
[a1732929] | 396 | usb_log_error("There are no configurations available");
|
---|
[625f1ba] | 397 | return EINVAL;
|
---|
[15b0432] | 398 | }
|
---|
| 399 |
|
---|
[e2dfa86] | 400 | const size_t config_size =
|
---|
| 401 | usb_device_descriptors(usb_device)->full_config_size;
|
---|
[8b68bdf] | 402 | const usb_standard_configuration_descriptor_t *config_descriptor =
|
---|
[e2dfa86] | 403 | usb_device_descriptors(usb_device)->full_config;
|
---|
[8b68bdf] | 404 |
|
---|
| 405 | if (config_size < sizeof(usb_standard_configuration_descriptor_t)) {
|
---|
[fec6bf2] | 406 | usb_log_error("Configuration descriptor is not big enough"
|
---|
| 407 | " to fit standard configuration descriptor.\n");
|
---|
| 408 | return EOVERFLOW;
|
---|
| 409 | }
|
---|
| 410 |
|
---|
[193da9d6] | 411 | /* Set configuration. Use the configuration that was in
|
---|
[ea6de35] | 412 | * usb_device->descriptors.configuration i.e. The first one. */
|
---|
[193da9d6] | 413 | const int opResult = usb_request_set_configuration(
|
---|
[0f4bff8] | 414 | usb_device_get_default_pipe(usb_device),
|
---|
| 415 | config_descriptor->configuration_number);
|
---|
[15b0432] | 416 | if (opResult != EOK) {
|
---|
[a1732929] | 417 | usb_log_error("Failed to set hub configuration: %s.",
|
---|
[f35b294] | 418 | str_error(opResult));
|
---|
[ea6de35] | 419 | } else {
|
---|
[a1732929] | 420 | usb_log_debug("\tUsed configuration %d",
|
---|
[ea6de35] | 421 | config_descriptor->configuration_number);
|
---|
[15b0432] | 422 | }
|
---|
[ea6de35] | 423 | return opResult;
|
---|
[15b0432] | 424 | }
|
---|
[76fbd9a] | 425 |
|
---|
[3dba1ca] | 426 | /**
|
---|
[ea6de35] | 427 | * Process hub over current change
|
---|
[3dba1ca] | 428 | *
|
---|
| 429 | * This means either to power off the hub or power it on.
|
---|
[eda7a4e0] | 430 | * @param hub_dev hub instance
|
---|
[3dba1ca] | 431 | * @param status hub status bitmask
|
---|
| 432 | * @return error code
|
---|
| 433 | */
|
---|
[eda7a4e0] | 434 | static void usb_hub_over_current(const usb_hub_dev_t *hub_dev,
|
---|
[bf73a02] | 435 | usb_hub_status_t status)
|
---|
| 436 | {
|
---|
| 437 | if (status & USB_HUB_STATUS_OVER_CURRENT) {
|
---|
[0212751] | 438 | /* Hub should remove power from all ports if it detects OC */
|
---|
[e98e5fc] | 439 | usb_log_warning("(%p) Detected hub over-current condition, "
|
---|
| 440 | "all ports should be powered off.", hub_dev);
|
---|
[54d1ad9] | 441 | return;
|
---|
[040ab02] | 442 | }
|
---|
[54d1ad9] | 443 |
|
---|
| 444 | /* Ports are always powered. */
|
---|
| 445 | if (!hub_dev->power_switched)
|
---|
| 446 | return;
|
---|
| 447 |
|
---|
| 448 | /* Over-current condition is gone, it is safe to turn the ports on. */
|
---|
| 449 | for (size_t port = 0; port < hub_dev->port_count; ++port) {
|
---|
[c4e84ed6] | 450 | const int ret = usb_hub_set_port_feature(hub_dev, port, USB_HUB_FEATURE_PORT_POWER);
|
---|
[54d1ad9] | 451 | if (ret != EOK) {
|
---|
[e98e5fc] | 452 | usb_log_warning("(%p-%u): HUB OVER-CURRENT GONE: Cannot"
|
---|
| 453 | " power on port: %s\n", hub_dev,
|
---|
| 454 | hub_dev->ports[port].port_number, str_error(ret));
|
---|
[54d1ad9] | 455 | } else {
|
---|
| 456 | if (!hub_dev->per_port_power)
|
---|
| 457 | return;
|
---|
| 458 | }
|
---|
[0212751] | 459 | }
|
---|
[c4e84ed6] | 460 | }
|
---|
| 461 |
|
---|
| 462 | /**
|
---|
| 463 | * Set feature on the real hub port.
|
---|
| 464 | *
|
---|
| 465 | * @param port Port structure.
|
---|
| 466 | * @param feature Feature selector.
|
---|
| 467 | */
|
---|
| 468 | int usb_hub_set_port_feature(const usb_hub_dev_t *hub, size_t port_number, usb_hub_class_feature_t feature)
|
---|
| 469 | {
|
---|
| 470 | assert(hub);
|
---|
| 471 | const usb_device_request_setup_packet_t clear_request = {
|
---|
| 472 | .request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE,
|
---|
| 473 | .request = USB_DEVREQ_SET_FEATURE,
|
---|
| 474 | .index = uint16_host2usb(port_number),
|
---|
| 475 | .value = feature,
|
---|
| 476 | .length = 0,
|
---|
| 477 | };
|
---|
| 478 | return usb_pipe_control_write(hub->control_pipe, &clear_request,
|
---|
| 479 | sizeof(clear_request), NULL, 0);
|
---|
| 480 | }
|
---|
| 481 |
|
---|
| 482 | /**
|
---|
| 483 | * Clear feature on the real hub port.
|
---|
| 484 | *
|
---|
| 485 | * @param port Port structure.
|
---|
| 486 | * @param feature Feature selector.
|
---|
| 487 | */
|
---|
| 488 | int usb_hub_clear_port_feature(const usb_hub_dev_t *hub, size_t port_number, usb_hub_class_feature_t feature)
|
---|
| 489 | {
|
---|
| 490 | assert(hub);
|
---|
| 491 | const usb_device_request_setup_packet_t clear_request = {
|
---|
| 492 | .request_type = USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE,
|
---|
| 493 | .request = USB_DEVREQ_CLEAR_FEATURE,
|
---|
| 494 | .value = feature,
|
---|
| 495 | .index = uint16_host2usb(port_number),
|
---|
| 496 | .length = 0,
|
---|
| 497 | };
|
---|
| 498 | return usb_pipe_control_write(hub->control_pipe,
|
---|
| 499 | &clear_request, sizeof(clear_request), NULL, 0);
|
---|
| 500 | }
|
---|
[54d1ad9] | 501 |
|
---|
[c4e84ed6] | 502 | /**
|
---|
| 503 | * Retrieve port status.
|
---|
| 504 | *
|
---|
| 505 | * @param[in] port Port structure
|
---|
| 506 | * @param[out] status Where to store the port status.
|
---|
| 507 | * @return Error code.
|
---|
| 508 | */
|
---|
| 509 | int usb_hub_get_port_status(const usb_hub_dev_t *hub, size_t port_number, usb_port_status_t *status)
|
---|
| 510 | {
|
---|
| 511 | assert(hub);
|
---|
| 512 | assert(status);
|
---|
| 513 |
|
---|
| 514 | /* USB hub specific GET_PORT_STATUS request. See USB Spec 11.16.2.6
|
---|
| 515 | * Generic GET_STATUS request cannot be used because of the difference
|
---|
| 516 | * in status data size (2B vs. 4B)*/
|
---|
| 517 | const usb_device_request_setup_packet_t request = {
|
---|
| 518 | .request_type = USB_HUB_REQ_TYPE_GET_PORT_STATUS,
|
---|
| 519 | .request = USB_HUB_REQUEST_GET_STATUS,
|
---|
| 520 | .value = 0,
|
---|
| 521 | .index = uint16_host2usb(port_number),
|
---|
| 522 | .length = sizeof(usb_port_status_t),
|
---|
| 523 | };
|
---|
| 524 | size_t recv_size;
|
---|
| 525 |
|
---|
| 526 | const int rc = usb_pipe_control_read(hub->control_pipe,
|
---|
| 527 | &request, sizeof(usb_device_request_setup_packet_t),
|
---|
| 528 | status, sizeof(*status), &recv_size);
|
---|
| 529 | if (rc != EOK)
|
---|
| 530 | return rc;
|
---|
| 531 |
|
---|
| 532 | if (recv_size != sizeof(*status))
|
---|
| 533 | return ELIMIT;
|
---|
| 534 |
|
---|
| 535 | return EOK;
|
---|
[040ab02] | 536 | }
|
---|
[76fbd9a] | 537 |
|
---|
[3dba1ca] | 538 | /**
|
---|
[ea6de35] | 539 | * Process hub interrupts.
|
---|
[3dba1ca] | 540 | *
|
---|
[ea6de35] | 541 | * The change can be either in the over-current condition or local-power change.
|
---|
[eda7a4e0] | 542 | * @param hub_dev hub instance
|
---|
[3dba1ca] | 543 | */
|
---|
[eda7a4e0] | 544 | static void usb_hub_global_interrupt(const usb_hub_dev_t *hub_dev)
|
---|
[bf73a02] | 545 | {
|
---|
[eda7a4e0] | 546 | assert(hub_dev);
|
---|
| 547 | assert(hub_dev->usb_device);
|
---|
[e98e5fc] | 548 | usb_log_debug("(%p): Global interrupt on th hub.", hub_dev);
|
---|
[0f4bff8] | 549 | usb_pipe_t *control_pipe =
|
---|
| 550 | usb_device_get_default_pipe(hub_dev->usb_device);
|
---|
[040ab02] | 551 |
|
---|
[bf73a02] | 552 | usb_hub_status_t status;
|
---|
[040ab02] | 553 | size_t rcvd_size;
|
---|
[ea6de35] | 554 | /* NOTE: We can't use standard USB GET_STATUS request, because
|
---|
| 555 | * hubs reply is 4byte instead of 2 */
|
---|
[75eb6735] | 556 | const int opResult = usb_pipe_control_read(control_pipe,
|
---|
| 557 | &get_hub_status_request, sizeof(get_hub_status_request),
|
---|
[bf73a02] | 558 | &status, sizeof(usb_hub_status_t), &rcvd_size);
|
---|
[040ab02] | 559 | if (opResult != EOK) {
|
---|
[e98e5fc] | 560 | usb_log_error("(%p): Could not get hub status: %s.", hub_dev,
|
---|
[5c1a65e] | 561 | str_error(opResult));
|
---|
[040ab02] | 562 | return;
|
---|
| 563 | }
|
---|
[bf73a02] | 564 | if (rcvd_size != sizeof(usb_hub_status_t)) {
|
---|
[e98e5fc] | 565 | usb_log_error("(%p): Received status has incorrect size: "
|
---|
| 566 | "%zu != %zu", hub_dev, rcvd_size, sizeof(usb_hub_status_t));
|
---|
[040ab02] | 567 | return;
|
---|
| 568 | }
|
---|
[bf73a02] | 569 |
|
---|
| 570 | /* Handle status changes */
|
---|
[a61c683] | 571 | if (status & USB_HUB_STATUS_C_OVER_CURRENT) {
|
---|
[eda7a4e0] | 572 | usb_hub_over_current(hub_dev, status);
|
---|
[54d1ad9] | 573 | /* Ack change in hub OC flag */
|
---|
| 574 | const int ret = usb_request_clear_feature(
|
---|
[0f4bff8] | 575 | control_pipe, USB_REQUEST_TYPE_CLASS,
|
---|
[54d1ad9] | 576 | USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 577 | USB_HUB_FEATURE_C_HUB_OVER_CURRENT, 0);
|
---|
| 578 | if (ret != EOK) {
|
---|
[e98e5fc] | 579 | usb_log_error("(%p): Failed to clear hub over-current "
|
---|
| 580 | "change flag: %s.\n", hub_dev, str_error(opResult));
|
---|
[54d1ad9] | 581 | }
|
---|
[a61c683] | 582 | }
|
---|
[bf73a02] | 583 |
|
---|
| 584 | if (status & USB_HUB_STATUS_C_LOCAL_POWER) {
|
---|
| 585 | /* NOTE: Handling this is more complicated.
|
---|
| 586 | * If the transition is from bus power to local power, all
|
---|
| 587 | * is good and we may signal the parent hub that we don't
|
---|
| 588 | * need the power.
|
---|
| 589 | * If the transition is from local power to bus power
|
---|
| 590 | * the hub should turn off all the ports and devices need
|
---|
| 591 | * to be reinitialized taking into account the limited power
|
---|
| 592 | * that is now available.
|
---|
| 593 | * There is no support for power distribution in HelenOS,
|
---|
| 594 | * (or other OSes/hub devices that I've seen) so this is not
|
---|
| 595 | * implemented.
|
---|
| 596 | * Just ACK the change.
|
---|
| 597 | */
|
---|
[54d1ad9] | 598 | const int ret = usb_request_clear_feature(
|
---|
[bba0f1fc] | 599 | control_pipe, USB_REQUEST_TYPE_CLASS,
|
---|
| 600 | USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 601 | USB_HUB_FEATURE_C_HUB_LOCAL_POWER, 0);
|
---|
[bf73a02] | 602 | if (opResult != EOK) {
|
---|
[e98e5fc] | 603 | usb_log_error("(%p): Failed to clear hub power change "
|
---|
| 604 | "flag: %s.\n", hub_dev, str_error(ret));
|
---|
[bf73a02] | 605 | }
|
---|
[040ab02] | 606 | }
|
---|
| 607 | }
|
---|
[76fbd9a] | 608 |
|
---|
[94f8c363] | 609 | static FIBRIL_CONDVAR_INITIALIZE(global_hub_default_address_cv);
|
---|
| 610 |
|
---|
[51a51be] | 611 | /**
|
---|
| 612 | * Reserve a default address for a port across all other devices connected to
|
---|
| 613 | * the bus. We aggregate requests for ports to minimize delays between
|
---|
| 614 | * connecting multiple devices from one hub - which happens e.g. when the hub
|
---|
| 615 | * is connected with already attached devices.
|
---|
| 616 | */
|
---|
[94f8c363] | 617 | int usb_hub_reserve_default_address(usb_hub_dev_t *hub, async_exch_t *exch, usb_port_t *port)
|
---|
[51a51be] | 618 | {
|
---|
| 619 | assert(hub);
|
---|
| 620 | assert(exch);
|
---|
[94f8c363] | 621 | assert(port);
|
---|
| 622 | assert(fibril_mutex_is_locked(&port->guard));
|
---|
[51a51be] | 623 |
|
---|
| 624 | fibril_mutex_lock(&hub->default_address_guard);
|
---|
| 625 | if (hub->default_address_requests++ == 0) {
|
---|
| 626 | /* We're the first to request the address, we can just do it */
|
---|
| 627 | fibril_mutex_unlock(&hub->default_address_guard);
|
---|
| 628 | int err;
|
---|
| 629 | while ((err = usbhc_reserve_default_address(exch)) == EAGAIN) {
|
---|
[94f8c363] | 630 | // We ignore the return value here, as we cannot give up now.
|
---|
| 631 | usb_port_condvar_wait_timeout(port, &global_hub_default_address_cv, 500000);
|
---|
[51a51be] | 632 | }
|
---|
| 633 | return err;
|
---|
| 634 | } else {
|
---|
| 635 | /* Drop the port guard, we're going to wait */
|
---|
[94f8c363] | 636 | fibril_mutex_unlock(&port->guard);
|
---|
[51a51be] | 637 |
|
---|
| 638 | /* Wait for a signal */
|
---|
| 639 | fibril_condvar_wait(&hub->default_address_cv, &hub->default_address_guard);
|
---|
| 640 |
|
---|
| 641 | /* Remember ABBA, first drop the hub guard */
|
---|
| 642 | fibril_mutex_unlock(&hub->default_address_guard);
|
---|
[94f8c363] | 643 | fibril_mutex_lock(&port->guard);
|
---|
[51a51be] | 644 | return EOK;
|
---|
| 645 | }
|
---|
| 646 | }
|
---|
| 647 |
|
---|
| 648 | /**
|
---|
| 649 | * Release the default address from a port.
|
---|
| 650 | */
|
---|
| 651 | int usb_hub_release_default_address(usb_hub_dev_t *hub, async_exch_t *exch)
|
---|
| 652 | {
|
---|
[740dafc] | 653 | int ret = EOK;
|
---|
| 654 |
|
---|
[51a51be] | 655 | fibril_mutex_lock(&hub->default_address_guard);
|
---|
| 656 | if (--hub->default_address_requests == 0) {
|
---|
[740dafc] | 657 | // We must do it in critical section to prevent other fibril
|
---|
| 658 | // from requesting the address before we release
|
---|
| 659 | ret = usbhc_release_default_address(exch);
|
---|
[94f8c363] | 660 | // This is optimistic optimization - it may wake one hub from polling sleep
|
---|
| 661 | fibril_condvar_signal(&global_hub_default_address_cv);
|
---|
[51a51be] | 662 | } else {
|
---|
| 663 | fibril_condvar_signal(&hub->default_address_cv);
|
---|
| 664 | }
|
---|
[740dafc] | 665 | fibril_mutex_unlock(&hub->default_address_guard);
|
---|
| 666 |
|
---|
| 667 | return ret;
|
---|
[51a51be] | 668 | }
|
---|
| 669 |
|
---|
[e080332] | 670 | /**
|
---|
| 671 | * @}
|
---|
[71ed4849] | 672 | */
|
---|