| 1 | /* | 
|---|
| 2 | * Copyright (c) 2010 Matus Dekanek | 
|---|
| 3 | * All rights reserved. | 
|---|
| 4 | * | 
|---|
| 5 | * Redistribution and use in source and binary forms, with or without | 
|---|
| 6 | * modification, are permitted provided that the following conditions | 
|---|
| 7 | * are met: | 
|---|
| 8 | * | 
|---|
| 9 | * - Redistributions of source code must retain the above copyright | 
|---|
| 10 | *   notice, this list of conditions and the following disclaimer. | 
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright | 
|---|
| 12 | *   notice, this list of conditions and the following disclaimer in the | 
|---|
| 13 | *   documentation and/or other materials provided with the distribution. | 
|---|
| 14 | * - The name of the author may not be used to endorse or promote products | 
|---|
| 15 | *   derived from this software without specific prior written permission. | 
|---|
| 16 | * | 
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | 
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 27 | */ | 
|---|
| 28 | /** @addtogroup drvusbhub | 
|---|
| 29 | * @{ | 
|---|
| 30 | */ | 
|---|
| 31 | /** @file | 
|---|
| 32 | * @brief usb hub main functionality | 
|---|
| 33 | */ | 
|---|
| 34 |  | 
|---|
| 35 | #include <ddf/driver.h> | 
|---|
| 36 | #include <bool.h> | 
|---|
| 37 | #include <errno.h> | 
|---|
| 38 | #include <str_error.h> | 
|---|
| 39 | #include <inttypes.h> | 
|---|
| 40 |  | 
|---|
| 41 | #include <usb_iface.h> | 
|---|
| 42 | #include <usb/ddfiface.h> | 
|---|
| 43 | #include <usb/descriptor.h> | 
|---|
| 44 | #include <usb/dev/recognise.h> | 
|---|
| 45 | #include <usb/dev/request.h> | 
|---|
| 46 | #include <usb/classes/hub.h> | 
|---|
| 47 | #include <usb/dev/poll.h> | 
|---|
| 48 | #include <stdio.h> | 
|---|
| 49 |  | 
|---|
| 50 | #include "usbhub.h" | 
|---|
| 51 | #include "usbhub_private.h" | 
|---|
| 52 | #include "port_status.h" | 
|---|
| 53 | #include <usb/usb.h> | 
|---|
| 54 | #include <usb/dev/pipes.h> | 
|---|
| 55 | #include <usb/classes/classes.h> | 
|---|
| 56 |  | 
|---|
| 57 |  | 
|---|
| 58 | static usb_hub_info_t * usb_hub_info_create(usb_device_t *usb_dev); | 
|---|
| 59 |  | 
|---|
| 60 | static int usb_hub_process_hub_specific_info(usb_hub_info_t *hub_info); | 
|---|
| 61 |  | 
|---|
| 62 | static int usb_hub_set_configuration(usb_hub_info_t *hub_info); | 
|---|
| 63 |  | 
|---|
| 64 | static int usb_hub_start_hub_fibril(usb_hub_info_t *hub_info); | 
|---|
| 65 |  | 
|---|
| 66 | static int usb_process_hub_over_current(usb_hub_info_t *hub_info, | 
|---|
| 67 | usb_hub_status_t status); | 
|---|
| 68 |  | 
|---|
| 69 | static int usb_process_hub_local_power_change(usb_hub_info_t *hub_info, | 
|---|
| 70 | usb_hub_status_t status); | 
|---|
| 71 |  | 
|---|
| 72 | static void usb_hub_process_global_interrupt(usb_hub_info_t *hub_info); | 
|---|
| 73 |  | 
|---|
| 74 | static void usb_hub_polling_terminated_callback(usb_device_t *device, | 
|---|
| 75 | bool was_error, void *data); | 
|---|
| 76 |  | 
|---|
| 77 |  | 
|---|
| 78 | //********************************************* | 
|---|
| 79 | // | 
|---|
| 80 | //  hub driver code, initialization | 
|---|
| 81 | // | 
|---|
| 82 | //********************************************* | 
|---|
| 83 |  | 
|---|
| 84 | /** | 
|---|
| 85 | * Initialize hub device driver fibril | 
|---|
| 86 | * | 
|---|
| 87 | * Creates hub representation and fibril that periodically checks hub`s status. | 
|---|
| 88 | * Hub representation is passed to the fibril. | 
|---|
| 89 | * @param usb_dev generic usb device information | 
|---|
| 90 | * @return error code | 
|---|
| 91 | */ | 
|---|
| 92 | int usb_hub_add_device(usb_device_t *usb_dev) { | 
|---|
| 93 | if (!usb_dev) return EINVAL; | 
|---|
| 94 | usb_hub_info_t *hub_info = usb_hub_info_create(usb_dev); | 
|---|
| 95 | //create hc connection | 
|---|
| 96 | usb_log_debug("Initializing USB wire abstraction.\n"); | 
|---|
| 97 | int opResult = usb_hc_connection_initialize_from_device( | 
|---|
| 98 | &hub_info->connection, | 
|---|
| 99 | hub_info->usb_device->ddf_dev); | 
|---|
| 100 | if (opResult != EOK) { | 
|---|
| 101 | usb_log_error("Could not initialize connection to device, " | 
|---|
| 102 | " %s\n", | 
|---|
| 103 | str_error(opResult)); | 
|---|
| 104 | free(hub_info); | 
|---|
| 105 | return opResult; | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | //set hub configuration | 
|---|
| 109 | opResult = usb_hub_set_configuration(hub_info); | 
|---|
| 110 | if (opResult != EOK) { | 
|---|
| 111 | usb_log_error("Could not set hub configuration, %s\n", | 
|---|
| 112 | str_error(opResult)); | 
|---|
| 113 | free(hub_info); | 
|---|
| 114 | return opResult; | 
|---|
| 115 | } | 
|---|
| 116 | //get port count and create attached_devs | 
|---|
| 117 | opResult = usb_hub_process_hub_specific_info(hub_info); | 
|---|
| 118 | if (opResult != EOK) { | 
|---|
| 119 | usb_log_error("Could process hub specific info, %s\n", | 
|---|
| 120 | str_error(opResult)); | 
|---|
| 121 | free(hub_info); | 
|---|
| 122 | return opResult; | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | usb_log_debug("Creating 'hub' function in DDF.\n"); | 
|---|
| 126 | ddf_fun_t *hub_fun = ddf_fun_create(hub_info->usb_device->ddf_dev, | 
|---|
| 127 | fun_exposed, "hub"); | 
|---|
| 128 | assert(hub_fun != NULL); | 
|---|
| 129 | hub_fun->ops = NULL; | 
|---|
| 130 |  | 
|---|
| 131 | opResult = ddf_fun_bind(hub_fun); | 
|---|
| 132 | assert(opResult == EOK); | 
|---|
| 133 | opResult = ddf_fun_add_to_class(hub_fun, "hub"); | 
|---|
| 134 | assert(opResult == EOK); | 
|---|
| 135 |  | 
|---|
| 136 | opResult = usb_hub_start_hub_fibril(hub_info); | 
|---|
| 137 | if (opResult != EOK) | 
|---|
| 138 | free(hub_info); | 
|---|
| 139 | return opResult; | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | /** Callback for polling hub for changes. | 
|---|
| 143 | * | 
|---|
| 144 | * @param dev Device where the change occured. | 
|---|
| 145 | * @param change_bitmap Bitmap of changed ports. | 
|---|
| 146 | * @param change_bitmap_size Size of the bitmap in bytes. | 
|---|
| 147 | * @param arg Custom argument, points to @c usb_hub_info_t. | 
|---|
| 148 | * @return Whether to continue polling. | 
|---|
| 149 | */ | 
|---|
| 150 | bool hub_port_changes_callback(usb_device_t *dev, | 
|---|
| 151 | uint8_t *change_bitmap, size_t change_bitmap_size, void *arg) { | 
|---|
| 152 | usb_log_debug("hub_port_changes_callback\n"); | 
|---|
| 153 | usb_hub_info_t *hub = (usb_hub_info_t *) arg; | 
|---|
| 154 |  | 
|---|
| 155 | /* FIXME: check that we received enough bytes. */ | 
|---|
| 156 | if (change_bitmap_size == 0) { | 
|---|
| 157 | goto leave; | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | bool change; | 
|---|
| 161 | change = ((uint8_t*) change_bitmap)[0] & 1; | 
|---|
| 162 | if (change) { | 
|---|
| 163 | usb_hub_process_global_interrupt(hub); | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | size_t port; | 
|---|
| 167 | for (port = 1; port < hub->port_count + 1; port++) { | 
|---|
| 168 | bool change = (change_bitmap[port / 8] >> (port % 8)) % 2; | 
|---|
| 169 | if (change) { | 
|---|
| 170 | usb_hub_process_port_interrupt(hub, port); | 
|---|
| 171 | } | 
|---|
| 172 | } | 
|---|
| 173 | leave: | 
|---|
| 174 | /* FIXME: proper interval. */ | 
|---|
| 175 | async_usleep(1000 * 250); | 
|---|
| 176 |  | 
|---|
| 177 | return true; | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 |  | 
|---|
| 181 | //********************************************* | 
|---|
| 182 | // | 
|---|
| 183 | //  support functions | 
|---|
| 184 | // | 
|---|
| 185 | //********************************************* | 
|---|
| 186 |  | 
|---|
| 187 | /** | 
|---|
| 188 | * create usb_hub_info_t structure | 
|---|
| 189 | * | 
|---|
| 190 | * Does only basic copying of known information into new structure. | 
|---|
| 191 | * @param usb_dev usb device structure | 
|---|
| 192 | * @return basic usb_hub_info_t structure | 
|---|
| 193 | */ | 
|---|
| 194 | static usb_hub_info_t * usb_hub_info_create(usb_device_t *usb_dev) { | 
|---|
| 195 | usb_hub_info_t * result = malloc(sizeof (usb_hub_info_t)); | 
|---|
| 196 | if (!result) return NULL; | 
|---|
| 197 | result->usb_device = usb_dev; | 
|---|
| 198 | result->status_change_pipe = usb_dev->pipes[0].pipe; | 
|---|
| 199 | result->control_pipe = &usb_dev->ctrl_pipe; | 
|---|
| 200 | result->is_default_address_used = false; | 
|---|
| 201 |  | 
|---|
| 202 | result->ports = NULL; | 
|---|
| 203 | result->port_count = (size_t) - 1; | 
|---|
| 204 | fibril_mutex_initialize(&result->port_mutex); | 
|---|
| 205 |  | 
|---|
| 206 | fibril_mutex_initialize(&result->pending_ops_mutex); | 
|---|
| 207 | fibril_condvar_initialize(&result->pending_ops_cv); | 
|---|
| 208 | result->pending_ops_count = 0; | 
|---|
| 209 | return result; | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | /** | 
|---|
| 213 | * Load hub-specific information into hub_info structure and process if needed | 
|---|
| 214 | * | 
|---|
| 215 | * Particularly read port count and initialize structure holding port | 
|---|
| 216 | * information. If there are non-removable devices, start initializing them. | 
|---|
| 217 | * This function is hub-specific and should be run only after the hub is | 
|---|
| 218 | * configured using usb_hub_set_configuration function. | 
|---|
| 219 | * @param hub_info hub representation | 
|---|
| 220 | * @return error code | 
|---|
| 221 | */ | 
|---|
| 222 | static int usb_hub_process_hub_specific_info(usb_hub_info_t *hub_info) { | 
|---|
| 223 | // get hub descriptor | 
|---|
| 224 | usb_log_debug("Creating serialized descriptor\n"); | 
|---|
| 225 | uint8_t serialized_descriptor[USB_HUB_MAX_DESCRIPTOR_SIZE]; | 
|---|
| 226 | usb_hub_descriptor_t * descriptor; | 
|---|
| 227 | int opResult; | 
|---|
| 228 |  | 
|---|
| 229 | size_t received_size; | 
|---|
| 230 | opResult = usb_request_get_descriptor(hub_info->control_pipe, | 
|---|
| 231 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE, | 
|---|
| 232 | USB_DESCTYPE_HUB, 0, 0, serialized_descriptor, | 
|---|
| 233 | USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size); | 
|---|
| 234 |  | 
|---|
| 235 | if (opResult != EOK) { | 
|---|
| 236 | usb_log_error("Failed when receiving hub descriptor, " | 
|---|
| 237 | "%s\n", | 
|---|
| 238 | str_error(opResult)); | 
|---|
| 239 | free(serialized_descriptor); | 
|---|
| 240 | return opResult; | 
|---|
| 241 | } | 
|---|
| 242 | usb_log_debug2("Deserializing descriptor\n"); | 
|---|
| 243 | descriptor = usb_create_deserialized_hub_desriptor( | 
|---|
| 244 | serialized_descriptor); | 
|---|
| 245 | if (descriptor == NULL) { | 
|---|
| 246 | usb_log_warning("could not deserialize descriptor \n"); | 
|---|
| 247 | return ENOMEM; | 
|---|
| 248 | } | 
|---|
| 249 | usb_log_debug("setting port count to %d\n", descriptor->ports_count); | 
|---|
| 250 | hub_info->port_count = descriptor->ports_count; | 
|---|
| 251 | bool is_power_switched = | 
|---|
| 252 | ((descriptor->hub_characteristics & 1) == 0); | 
|---|
| 253 | bool has_individual_port_powering = | 
|---|
| 254 | ((descriptor->hub_characteristics & 1) != 0); | 
|---|
| 255 | hub_info->ports = malloc( | 
|---|
| 256 | sizeof (usb_hub_port_t) * (hub_info->port_count + 1)); | 
|---|
| 257 | if (!hub_info->ports) { | 
|---|
| 258 | return ENOMEM; | 
|---|
| 259 | } | 
|---|
| 260 | size_t port; | 
|---|
| 261 | for (port = 0; port < hub_info->port_count + 1; ++port) { | 
|---|
| 262 | usb_hub_port_init(&hub_info->ports[port]); | 
|---|
| 263 | } | 
|---|
| 264 | if (is_power_switched) { | 
|---|
| 265 | usb_log_debug("Hub power switched\n"); | 
|---|
| 266 |  | 
|---|
| 267 | if (!has_individual_port_powering) { | 
|---|
| 268 | //this setting actually makes no difference | 
|---|
| 269 | usb_log_debug("Hub has global powering\n"); | 
|---|
| 270 | } | 
|---|
| 271 |  | 
|---|
| 272 | for (port = 1; port <= hub_info->port_count; ++port) { | 
|---|
| 273 | usb_log_debug("Powering port %zu.\n", port); | 
|---|
| 274 | opResult = usb_hub_set_port_feature(hub_info->control_pipe, | 
|---|
| 275 | port, USB_HUB_FEATURE_PORT_POWER); | 
|---|
| 276 | if (opResult != EOK) { | 
|---|
| 277 | usb_log_error("Cannot power on port %zu: %s.\n", | 
|---|
| 278 | port, str_error(opResult)); | 
|---|
| 279 | } | 
|---|
| 280 | } | 
|---|
| 281 |  | 
|---|
| 282 | } else { | 
|---|
| 283 | usb_log_debug("Power not switched, not going to be powered\n"); | 
|---|
| 284 | } | 
|---|
| 285 | usb_log_debug2("Freeing data\n"); | 
|---|
| 286 | free(descriptor); | 
|---|
| 287 | return EOK; | 
|---|
| 288 | } | 
|---|
| 289 |  | 
|---|
| 290 | /** | 
|---|
| 291 | * Set configuration of hub | 
|---|
| 292 | * | 
|---|
| 293 | * Check whether there is at least one configuration and sets the first one. | 
|---|
| 294 | * This function should be run prior to running any hub-specific action. | 
|---|
| 295 | * @param hub_info hub representation | 
|---|
| 296 | * @return error code | 
|---|
| 297 | */ | 
|---|
| 298 | static int usb_hub_set_configuration(usb_hub_info_t *hub_info) { | 
|---|
| 299 | //device descriptor | 
|---|
| 300 | usb_standard_device_descriptor_t *std_descriptor | 
|---|
| 301 | = &hub_info->usb_device->descriptors.device; | 
|---|
| 302 | usb_log_debug("Hub has %d configurations\n", | 
|---|
| 303 | std_descriptor->configuration_count); | 
|---|
| 304 | if (std_descriptor->configuration_count < 1) { | 
|---|
| 305 | usb_log_error("There are no configurations available\n"); | 
|---|
| 306 | return EINVAL; | 
|---|
| 307 | } | 
|---|
| 308 |  | 
|---|
| 309 | usb_standard_configuration_descriptor_t *config_descriptor | 
|---|
| 310 | = (usb_standard_configuration_descriptor_t *) | 
|---|
| 311 | hub_info->usb_device->descriptors.configuration; | 
|---|
| 312 |  | 
|---|
| 313 | /* Set configuration. */ | 
|---|
| 314 | int opResult = usb_request_set_configuration( | 
|---|
| 315 | &hub_info->usb_device->ctrl_pipe, | 
|---|
| 316 | config_descriptor->configuration_number); | 
|---|
| 317 |  | 
|---|
| 318 | if (opResult != EOK) { | 
|---|
| 319 | usb_log_error("Failed to set hub configuration: %s.\n", | 
|---|
| 320 | str_error(opResult)); | 
|---|
| 321 | return opResult; | 
|---|
| 322 | } | 
|---|
| 323 | usb_log_debug("\tUsed configuration %d\n", | 
|---|
| 324 | config_descriptor->configuration_number); | 
|---|
| 325 |  | 
|---|
| 326 | return EOK; | 
|---|
| 327 | } | 
|---|
| 328 |  | 
|---|
| 329 | /** | 
|---|
| 330 | * create and start fibril with hub control loop | 
|---|
| 331 | * | 
|---|
| 332 | * Before the fibril is started, the control pipe and host controller | 
|---|
| 333 | * connection of the hub is open. | 
|---|
| 334 | * | 
|---|
| 335 | * @param hub_info hub representing structure | 
|---|
| 336 | * @return error code | 
|---|
| 337 | */ | 
|---|
| 338 | static int usb_hub_start_hub_fibril(usb_hub_info_t *hub_info) { | 
|---|
| 339 | int rc; | 
|---|
| 340 |  | 
|---|
| 341 | rc = usb_device_auto_poll(hub_info->usb_device, 0, | 
|---|
| 342 | hub_port_changes_callback, ((hub_info->port_count + 1) / 8) + 1, | 
|---|
| 343 | usb_hub_polling_terminated_callback, hub_info); | 
|---|
| 344 | if (rc != EOK) { | 
|---|
| 345 | usb_log_error("Failed to create polling fibril: %s.\n", | 
|---|
| 346 | str_error(rc)); | 
|---|
| 347 | free(hub_info); | 
|---|
| 348 | return rc; | 
|---|
| 349 | } | 
|---|
| 350 |  | 
|---|
| 351 | usb_log_info("Controlling hub `%s' (%zu ports).\n", | 
|---|
| 352 | hub_info->usb_device->ddf_dev->name, hub_info->port_count); | 
|---|
| 353 | return EOK; | 
|---|
| 354 | } | 
|---|
| 355 |  | 
|---|
| 356 | //********************************************* | 
|---|
| 357 | // | 
|---|
| 358 | //  change handling functions | 
|---|
| 359 | // | 
|---|
| 360 | //********************************************* | 
|---|
| 361 |  | 
|---|
| 362 | /** | 
|---|
| 363 | * process hub over current change | 
|---|
| 364 | * | 
|---|
| 365 | * This means either to power off the hub or power it on. | 
|---|
| 366 | * @param hub_info hub instance | 
|---|
| 367 | * @param status hub status bitmask | 
|---|
| 368 | * @return error code | 
|---|
| 369 | */ | 
|---|
| 370 | static int usb_process_hub_over_current(usb_hub_info_t *hub_info, | 
|---|
| 371 | usb_hub_status_t status) { | 
|---|
| 372 | int opResult; | 
|---|
| 373 | if (usb_hub_is_status(status, USB_HUB_FEATURE_HUB_OVER_CURRENT)) { | 
|---|
| 374 | //poweroff all ports | 
|---|
| 375 | unsigned int port; | 
|---|
| 376 | for (port = 1; port <= hub_info->port_count; ++port) { | 
|---|
| 377 | opResult = usb_hub_clear_port_feature( | 
|---|
| 378 | hub_info->control_pipe, port, | 
|---|
| 379 | USB_HUB_FEATURE_PORT_POWER); | 
|---|
| 380 | if (opResult != EOK) { | 
|---|
| 381 | usb_log_warning( | 
|---|
| 382 | "Cannot power off port %d;  %s\n", | 
|---|
| 383 | port, str_error(opResult)); | 
|---|
| 384 | } | 
|---|
| 385 | } | 
|---|
| 386 | } else { | 
|---|
| 387 | //power all ports | 
|---|
| 388 | unsigned int port; | 
|---|
| 389 | for (port = 1; port <= hub_info->port_count; ++port) { | 
|---|
| 390 | opResult = usb_hub_set_port_feature( | 
|---|
| 391 | hub_info->control_pipe, port, | 
|---|
| 392 | USB_HUB_FEATURE_PORT_POWER); | 
|---|
| 393 | if (opResult != EOK) { | 
|---|
| 394 | usb_log_warning( | 
|---|
| 395 | "Cannot power off port %d;  %s\n", | 
|---|
| 396 | port, str_error(opResult)); | 
|---|
| 397 | } | 
|---|
| 398 | } | 
|---|
| 399 | } | 
|---|
| 400 | return opResult; | 
|---|
| 401 | } | 
|---|
| 402 |  | 
|---|
| 403 | /** | 
|---|
| 404 | * process hub local power change | 
|---|
| 405 | * | 
|---|
| 406 | * This change is ignored. | 
|---|
| 407 | * @param hub_info hub instance | 
|---|
| 408 | * @param status hub status bitmask | 
|---|
| 409 | * @return error code | 
|---|
| 410 | */ | 
|---|
| 411 | static int usb_process_hub_local_power_change(usb_hub_info_t *hub_info, | 
|---|
| 412 | usb_hub_status_t status) { | 
|---|
| 413 | int opResult = EOK; | 
|---|
| 414 | opResult = usb_hub_clear_feature(hub_info->control_pipe, | 
|---|
| 415 | USB_HUB_FEATURE_C_HUB_LOCAL_POWER); | 
|---|
| 416 | if (opResult != EOK) { | 
|---|
| 417 | usb_log_error("Cannnot clear hub power change flag: " | 
|---|
| 418 | "%s\n", | 
|---|
| 419 | str_error(opResult)); | 
|---|
| 420 | } | 
|---|
| 421 | return opResult; | 
|---|
| 422 | } | 
|---|
| 423 |  | 
|---|
| 424 | /** | 
|---|
| 425 | * process hub interrupts | 
|---|
| 426 | * | 
|---|
| 427 | * The change can be either in the over-current condition or | 
|---|
| 428 | * local-power change. | 
|---|
| 429 | * @param hub_info hub instance | 
|---|
| 430 | */ | 
|---|
| 431 | static void usb_hub_process_global_interrupt(usb_hub_info_t *hub_info) { | 
|---|
| 432 | usb_log_debug("Global interrupt on a hub\n"); | 
|---|
| 433 | usb_pipe_t *pipe = hub_info->control_pipe; | 
|---|
| 434 | int opResult; | 
|---|
| 435 |  | 
|---|
| 436 | usb_port_status_t status; | 
|---|
| 437 | size_t rcvd_size; | 
|---|
| 438 | usb_device_request_setup_packet_t request; | 
|---|
| 439 | //int opResult; | 
|---|
| 440 | usb_hub_set_hub_status_request(&request); | 
|---|
| 441 | //endpoint 0 | 
|---|
| 442 |  | 
|---|
| 443 | opResult = usb_pipe_control_read( | 
|---|
| 444 | pipe, | 
|---|
| 445 | &request, sizeof (usb_device_request_setup_packet_t), | 
|---|
| 446 | &status, 4, &rcvd_size | 
|---|
| 447 | ); | 
|---|
| 448 | if (opResult != EOK) { | 
|---|
| 449 | usb_log_error("Could not get hub status: %s\n", | 
|---|
| 450 | str_error(opResult)); | 
|---|
| 451 | return; | 
|---|
| 452 | } | 
|---|
| 453 | if (rcvd_size != sizeof (usb_port_status_t)) { | 
|---|
| 454 | usb_log_error("Received status has incorrect size\n"); | 
|---|
| 455 | return; | 
|---|
| 456 | } | 
|---|
| 457 | //port reset | 
|---|
| 458 | if ( | 
|---|
| 459 | usb_hub_is_status(status, 16 + USB_HUB_FEATURE_C_HUB_OVER_CURRENT)) { | 
|---|
| 460 | usb_process_hub_over_current(hub_info, status); | 
|---|
| 461 | } | 
|---|
| 462 | if ( | 
|---|
| 463 | usb_hub_is_status(status, 16 + USB_HUB_FEATURE_C_HUB_LOCAL_POWER)) { | 
|---|
| 464 | usb_process_hub_local_power_change(hub_info, status); | 
|---|
| 465 | } | 
|---|
| 466 | } | 
|---|
| 467 |  | 
|---|
| 468 | /** | 
|---|
| 469 | * callback called from hub polling fibril when the fibril terminates | 
|---|
| 470 | * | 
|---|
| 471 | * Should perform a cleanup - deletes hub_info. | 
|---|
| 472 | * @param device usb device afected | 
|---|
| 473 | * @param was_error indicates that the fibril is stoped due to an error | 
|---|
| 474 | * @param data pointer to usb_hub_info_t structure | 
|---|
| 475 | */ | 
|---|
| 476 | static void usb_hub_polling_terminated_callback(usb_device_t *device, | 
|---|
| 477 | bool was_error, void *data) { | 
|---|
| 478 | usb_hub_info_t * hub = data; | 
|---|
| 479 | assert(hub); | 
|---|
| 480 |  | 
|---|
| 481 | fibril_mutex_lock(&hub->pending_ops_mutex); | 
|---|
| 482 |  | 
|---|
| 483 | /* The device is dead. However there might be some pending operations | 
|---|
| 484 | * that we need to wait for. | 
|---|
| 485 | * One of them is device adding in progress. | 
|---|
| 486 | * The respective fibril is probably waiting for status change | 
|---|
| 487 | * in port reset (port enable) callback. | 
|---|
| 488 | * Such change would never come (otherwise we would not be here). | 
|---|
| 489 | * Thus, we would flush all pending port resets. | 
|---|
| 490 | */ | 
|---|
| 491 | if (hub->pending_ops_count > 0) { | 
|---|
| 492 | fibril_mutex_lock(&hub->port_mutex); | 
|---|
| 493 | size_t port; | 
|---|
| 494 | for (port = 0; port < hub->port_count; port++) { | 
|---|
| 495 | usb_hub_port_t *the_port = hub->ports + port; | 
|---|
| 496 | fibril_mutex_lock(&the_port->reset_mutex); | 
|---|
| 497 | the_port->reset_completed = true; | 
|---|
| 498 | the_port->reset_okay = false; | 
|---|
| 499 | fibril_condvar_broadcast(&the_port->reset_cv); | 
|---|
| 500 | fibril_mutex_unlock(&the_port->reset_mutex); | 
|---|
| 501 | } | 
|---|
| 502 | fibril_mutex_unlock(&hub->port_mutex); | 
|---|
| 503 | } | 
|---|
| 504 | /* And now wait for them. */ | 
|---|
| 505 | while (hub->pending_ops_count > 0) { | 
|---|
| 506 | fibril_condvar_wait(&hub->pending_ops_cv, | 
|---|
| 507 | &hub->pending_ops_mutex); | 
|---|
| 508 | } | 
|---|
| 509 | fibril_mutex_unlock(&hub->pending_ops_mutex); | 
|---|
| 510 |  | 
|---|
| 511 | usb_device_destroy(hub->usb_device); | 
|---|
| 512 |  | 
|---|
| 513 | free(hub->ports); | 
|---|
| 514 | free(hub); | 
|---|
| 515 | } | 
|---|
| 516 |  | 
|---|
| 517 |  | 
|---|
| 518 |  | 
|---|
| 519 |  | 
|---|
| 520 | /** | 
|---|
| 521 | * @} | 
|---|
| 522 | */ | 
|---|