| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
|---|
| 3 | * Copyright (c) 2011 Jan Vesely
|
|---|
| 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 | */
|
|---|
| 29 | /** @addtogroup drvusbhub
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * Hub ports functions.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <bool.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <str_error.h>
|
|---|
| 39 | #include <inttypes.h>
|
|---|
| 40 | #include <fibril_synch.h>
|
|---|
| 41 |
|
|---|
| 42 | #include <usb/debug.h>
|
|---|
| 43 | #include <usb/dev/hub.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "port.h"
|
|---|
| 46 | #include "usbhub.h"
|
|---|
| 47 | #include "status.h"
|
|---|
| 48 |
|
|---|
| 49 | /** Information for fibril for device discovery. */
|
|---|
| 50 | struct add_device_phase1 {
|
|---|
| 51 | usb_hub_dev_t *hub;
|
|---|
| 52 | usb_hub_port_t *port;
|
|---|
| 53 | usb_speed_t speed;
|
|---|
| 54 | };
|
|---|
| 55 |
|
|---|
| 56 | static int usb_hub_port_device_gone(usb_hub_port_t *port, usb_hub_dev_t *hub);
|
|---|
| 57 | static void usb_hub_port_reset_completed(usb_hub_port_t *port,
|
|---|
| 58 | usb_port_status_t status);
|
|---|
| 59 | static int get_port_status(usb_hub_port_t *port, usb_port_status_t *status);
|
|---|
| 60 | static int enable_port_callback(void *arg);
|
|---|
| 61 | static int add_device_phase1_worker_fibril(void *arg);
|
|---|
| 62 | static int create_add_device_fibril(usb_hub_port_t *port, usb_hub_dev_t *hub,
|
|---|
| 63 | usb_speed_t speed);
|
|---|
| 64 |
|
|---|
| 65 | int usb_hub_port_fini(usb_hub_port_t *port, usb_hub_dev_t *hub)
|
|---|
| 66 | {
|
|---|
| 67 | assert(port);
|
|---|
| 68 | if (port->attached_device.fun)
|
|---|
| 69 | return usb_hub_port_device_gone(port, hub);
|
|---|
| 70 | return EOK;
|
|---|
| 71 | }
|
|---|
| 72 | /*----------------------------------------------------------------------------*/
|
|---|
| 73 | /**
|
|---|
| 74 | * Clear feature on hub port.
|
|---|
| 75 | *
|
|---|
| 76 | * @param port Port structure.
|
|---|
| 77 | * @param feature Feature selector.
|
|---|
| 78 | * @return Operation result
|
|---|
| 79 | */
|
|---|
| 80 | int usb_hub_port_clear_feature(
|
|---|
| 81 | usb_hub_port_t *port, usb_hub_class_feature_t feature)
|
|---|
| 82 | {
|
|---|
| 83 | assert(port);
|
|---|
| 84 | const usb_device_request_setup_packet_t clear_request = {
|
|---|
| 85 | .request_type = USB_HUB_REQ_TYPE_CLEAR_PORT_FEATURE,
|
|---|
| 86 | .request = USB_DEVREQ_CLEAR_FEATURE,
|
|---|
| 87 | .value = feature,
|
|---|
| 88 | .index = port->port_number,
|
|---|
| 89 | .length = 0,
|
|---|
| 90 | };
|
|---|
| 91 | return usb_pipe_control_write(port->control_pipe, &clear_request,
|
|---|
| 92 | sizeof(clear_request), NULL, 0);
|
|---|
| 93 | }
|
|---|
| 94 | /*----------------------------------------------------------------------------*/
|
|---|
| 95 | /**
|
|---|
| 96 | * Set feature on hub port.
|
|---|
| 97 | *
|
|---|
| 98 | * @param port Port structure.
|
|---|
| 99 | * @param feature Feature selector.
|
|---|
| 100 | * @return Operation result
|
|---|
| 101 | */
|
|---|
| 102 | int usb_hub_port_set_feature(
|
|---|
| 103 | usb_hub_port_t *port, usb_hub_class_feature_t feature)
|
|---|
| 104 | {
|
|---|
| 105 | assert(port);
|
|---|
| 106 | const usb_device_request_setup_packet_t clear_request = {
|
|---|
| 107 | .request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE,
|
|---|
| 108 | .request = USB_DEVREQ_SET_FEATURE,
|
|---|
| 109 | .index = port->port_number,
|
|---|
| 110 | .value = feature,
|
|---|
| 111 | .length = 0,
|
|---|
| 112 | };
|
|---|
| 113 | return usb_pipe_control_write(port->control_pipe, &clear_request,
|
|---|
| 114 | sizeof(clear_request), NULL, 0);
|
|---|
| 115 | }
|
|---|
| 116 | /*----------------------------------------------------------------------------*/
|
|---|
| 117 | /**
|
|---|
| 118 | * Mark reset process as failed due to external reasons
|
|---|
| 119 | *
|
|---|
| 120 | * @param port Port structure
|
|---|
| 121 | */
|
|---|
| 122 | void usb_hub_port_reset_fail(usb_hub_port_t *port)
|
|---|
| 123 | {
|
|---|
| 124 | assert(port);
|
|---|
| 125 | fibril_mutex_lock(&port->mutex);
|
|---|
| 126 | port->reset_completed = true;
|
|---|
| 127 | port->reset_okay = false;
|
|---|
| 128 | fibril_condvar_broadcast(&port->reset_cv);
|
|---|
| 129 | fibril_mutex_unlock(&port->mutex);
|
|---|
| 130 | }
|
|---|
| 131 | /*----------------------------------------------------------------------------*/
|
|---|
| 132 | /**
|
|---|
| 133 | * Process interrupts on given port
|
|---|
| 134 | *
|
|---|
| 135 | * Accepts connection, over current and port reset change.
|
|---|
| 136 | * @param port port structure
|
|---|
| 137 | * @param hub hub representation
|
|---|
| 138 | */
|
|---|
| 139 | void usb_hub_port_process_interrupt(usb_hub_port_t *port, usb_hub_dev_t *hub)
|
|---|
| 140 | {
|
|---|
| 141 | assert(port);
|
|---|
| 142 | assert(hub);
|
|---|
| 143 | usb_log_debug("Interrupt at port %zu\n", port->port_number);
|
|---|
| 144 |
|
|---|
| 145 | usb_port_status_t status;
|
|---|
| 146 | const int opResult = get_port_status(port, &status);
|
|---|
| 147 | if (opResult != EOK) {
|
|---|
| 148 | usb_log_error("Failed to get port %zu status: %s.\n",
|
|---|
| 149 | port->port_number, str_error(opResult));
|
|---|
| 150 | return;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /* Connection change */
|
|---|
| 154 | if (status & USB_HUB_PORT_C_STATUS_CONNECTION) {
|
|---|
| 155 | const bool connected =
|
|---|
| 156 | (status & USB_HUB_PORT_STATUS_CONNECTION) != 0;
|
|---|
| 157 | usb_log_debug("Connection change on port %zu: device %s.\n",
|
|---|
| 158 | port->port_number, connected ? "attached" : "removed");
|
|---|
| 159 |
|
|---|
| 160 | /* ACK the change */
|
|---|
| 161 | const int opResult = usb_hub_port_clear_feature(port,
|
|---|
| 162 | USB_HUB_FEATURE_C_PORT_CONNECTION);
|
|---|
| 163 | if (opResult != EOK) {
|
|---|
| 164 | usb_log_warning("Failed to clear port-change-connection"
|
|---|
| 165 | " flag: %s.\n", str_error(opResult));
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | if (connected) {
|
|---|
| 169 | const int opResult = create_add_device_fibril(port, hub,
|
|---|
| 170 | usb_port_speed(status));
|
|---|
| 171 | if (opResult != EOK) {
|
|---|
| 172 | usb_log_error(
|
|---|
| 173 | "Cannot handle change on port %zu: %s.\n",
|
|---|
| 174 | port->port_number, str_error(opResult));
|
|---|
| 175 | }
|
|---|
| 176 | } else {
|
|---|
| 177 | /* If enabled change was reported leave the removal
|
|---|
| 178 | * to that handler, it shall ACK the change too. */
|
|---|
| 179 | if (!(status & USB_HUB_PORT_C_STATUS_ENABLED)) {
|
|---|
| 180 | usb_hub_port_device_gone(port, hub);
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | /* Enable change, ports are automatically disabled on errors. */
|
|---|
| 186 | if (status & USB_HUB_PORT_C_STATUS_ENABLED) {
|
|---|
| 187 | usb_log_info("Port %zu, disabled because of errors.\n",
|
|---|
| 188 | port->port_number);
|
|---|
| 189 | usb_hub_port_device_gone(port, hub);
|
|---|
| 190 | const int rc = usb_hub_port_clear_feature(port,
|
|---|
| 191 | USB_HUB_FEATURE_C_PORT_ENABLE);
|
|---|
| 192 | if (rc != EOK) {
|
|---|
| 193 | usb_log_error(
|
|---|
| 194 | "Failed to clear port %zu enable change feature: "
|
|---|
| 195 | "%s.\n", port->port_number, str_error(rc));
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | /* Suspend change */
|
|---|
| 201 | if (status & USB_HUB_PORT_C_STATUS_SUSPEND) {
|
|---|
| 202 | usb_log_error("Port %zu went to suspend state, this should"
|
|---|
| 203 | "NOT happen as we do not support suspend state!",
|
|---|
| 204 | port->port_number);
|
|---|
| 205 | const int rc = usb_hub_port_clear_feature(port,
|
|---|
| 206 | USB_HUB_FEATURE_C_PORT_SUSPEND);
|
|---|
| 207 | if (rc != EOK) {
|
|---|
| 208 | usb_log_error(
|
|---|
| 209 | "Failed to clear port %zu suspend change feature: "
|
|---|
| 210 | "%s.\n", port->port_number, str_error(rc));
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | /* Over current */
|
|---|
| 215 | if (status & USB_HUB_PORT_C_STATUS_OC) {
|
|---|
| 216 | /* According to the USB specs:
|
|---|
| 217 | * 11.13.5 Over-current Reporting and Recovery
|
|---|
| 218 | * Hub device is responsible for putting port in power off
|
|---|
| 219 | * mode. USB system software is responsible for powering port
|
|---|
| 220 | * back on when the over-current condition is gone */
|
|---|
| 221 | const int rc = usb_hub_port_clear_feature(port,
|
|---|
| 222 | USB_HUB_FEATURE_C_PORT_OVER_CURRENT);
|
|---|
| 223 | if (rc != EOK) {
|
|---|
| 224 | usb_log_error(
|
|---|
| 225 | "Failed to clear port %zu OC change feature: %s.\n",
|
|---|
| 226 | port->port_number, str_error(rc));
|
|---|
| 227 | }
|
|---|
| 228 | if (!(status & ~USB_HUB_PORT_STATUS_OC)) {
|
|---|
| 229 | const int rc = usb_hub_port_set_feature(
|
|---|
| 230 | port, USB_HUB_FEATURE_PORT_POWER);
|
|---|
| 231 | if (rc != EOK) {
|
|---|
| 232 | usb_log_error(
|
|---|
| 233 | "Failed to set port %zu power after OC:"
|
|---|
| 234 | " %s.\n", port->port_number, str_error(rc));
|
|---|
| 235 | }
|
|---|
| 236 | }
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | /* Port reset, set on port reset complete. */
|
|---|
| 240 | if (status & USB_HUB_PORT_C_STATUS_RESET) {
|
|---|
| 241 | usb_hub_port_reset_completed(port, status);
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | usb_log_debug("Port %zu status 0x%08" PRIx32 "\n",
|
|---|
| 245 | port->port_number, status);
|
|---|
| 246 | }
|
|---|
| 247 | /*----------------------------------------------------------------------------*/
|
|---|
| 248 | /**
|
|---|
| 249 | * routine called when a device on port has been removed
|
|---|
| 250 | *
|
|---|
| 251 | * If the device on port had default address, it releases default address.
|
|---|
| 252 | * Otherwise does not do anything, because DDF does not allow to remove device
|
|---|
| 253 | * from it`s device tree.
|
|---|
| 254 | * @param port port structure
|
|---|
| 255 | * @param hub hub representation
|
|---|
| 256 | */
|
|---|
| 257 | int usb_hub_port_device_gone(usb_hub_port_t *port, usb_hub_dev_t *hub)
|
|---|
| 258 | {
|
|---|
| 259 | assert(port);
|
|---|
| 260 | assert(hub);
|
|---|
| 261 | if (port->attached_device.address < 0) {
|
|---|
| 262 | usb_log_warning(
|
|---|
| 263 | "Device on port %zu removed before being registered.\n",
|
|---|
| 264 | port->port_number);
|
|---|
| 265 |
|
|---|
| 266 | /*
|
|---|
| 267 | * Device was removed before port reset completed.
|
|---|
| 268 | * We will announce a failed port reset to unblock the
|
|---|
| 269 | * port reset callback from new device wrapper.
|
|---|
| 270 | */
|
|---|
| 271 | usb_hub_port_reset_fail(port);
|
|---|
| 272 | return EOK;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | fibril_mutex_lock(&port->mutex);
|
|---|
| 276 | assert(port->attached_device.fun);
|
|---|
| 277 | usb_log_debug("Removing device on port %zu.\n", port->port_number);
|
|---|
| 278 | int ret = ddf_fun_unbind(port->attached_device.fun);
|
|---|
| 279 | if (ret != EOK) {
|
|---|
| 280 | usb_log_error("Failed to unbind child function on port"
|
|---|
| 281 | " %zu: %s.\n", port->port_number, str_error(ret));
|
|---|
| 282 | fibril_mutex_unlock(&port->mutex);
|
|---|
| 283 | return ret;
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | ddf_fun_destroy(port->attached_device.fun);
|
|---|
| 287 | port->attached_device.fun = NULL;
|
|---|
| 288 |
|
|---|
| 289 | ret = usb_hub_unregister_device(&hub->usb_device->hc_conn,
|
|---|
| 290 | &port->attached_device);
|
|---|
| 291 | if (ret != EOK) {
|
|---|
| 292 | usb_log_warning("Failed to unregister address of the "
|
|---|
| 293 | "removed device: %s.\n", str_error(ret));
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | port->attached_device.address = -1;
|
|---|
| 297 | fibril_mutex_unlock(&port->mutex);
|
|---|
| 298 | usb_log_info("Removed device on port %zu.\n", port->port_number);
|
|---|
| 299 | return EOK;
|
|---|
| 300 | }
|
|---|
| 301 | /*----------------------------------------------------------------------------*/
|
|---|
| 302 | /**
|
|---|
| 303 | * Process port reset change
|
|---|
| 304 | *
|
|---|
| 305 | * After this change port should be enabled, unless some problem occurred.
|
|---|
| 306 | * This functions triggers second phase of enabling new device.
|
|---|
| 307 | * @param port Port structure
|
|---|
| 308 | * @param status Port status mask
|
|---|
| 309 | */
|
|---|
| 310 | void usb_hub_port_reset_completed(usb_hub_port_t *port,
|
|---|
| 311 | usb_port_status_t status)
|
|---|
| 312 | {
|
|---|
| 313 | assert(port);
|
|---|
| 314 | fibril_mutex_lock(&port->mutex);
|
|---|
| 315 | /* Finalize device adding. */
|
|---|
| 316 | port->reset_completed = true;
|
|---|
| 317 | port->reset_okay = (status & USB_HUB_PORT_STATUS_ENABLED) != 0;
|
|---|
| 318 |
|
|---|
| 319 | if (port->reset_okay) {
|
|---|
| 320 | usb_log_debug("Port %zu reset complete.\n", port->port_number);
|
|---|
| 321 | } else {
|
|---|
| 322 | usb_log_warning(
|
|---|
| 323 | "Port %zu reset complete but port not enabled.\n",
|
|---|
| 324 | port->port_number);
|
|---|
| 325 | }
|
|---|
| 326 | fibril_condvar_broadcast(&port->reset_cv);
|
|---|
| 327 | fibril_mutex_unlock(&port->mutex);
|
|---|
| 328 |
|
|---|
| 329 | /* Clear the port reset change. */
|
|---|
| 330 | int rc = usb_hub_port_clear_feature(port, USB_HUB_FEATURE_C_PORT_RESET);
|
|---|
| 331 | if (rc != EOK) {
|
|---|
| 332 | usb_log_error(
|
|---|
| 333 | "Failed to clear port %zu reset change feature: %s.\n",
|
|---|
| 334 | port->port_number, str_error(rc));
|
|---|
| 335 | }
|
|---|
| 336 | }
|
|---|
| 337 | /*----------------------------------------------------------------------------*/
|
|---|
| 338 | /** Retrieve port status.
|
|---|
| 339 | *
|
|---|
| 340 | * @param[in] port Port structure
|
|---|
| 341 | * @param[out] status Where to store the port status.
|
|---|
| 342 | * @return Error code.
|
|---|
| 343 | */
|
|---|
| 344 | static int get_port_status(usb_hub_port_t *port, usb_port_status_t *status)
|
|---|
| 345 | {
|
|---|
| 346 | assert(port);
|
|---|
| 347 | /* USB hub specific GET_PORT_STATUS request. See USB Spec 11.16.2.6
|
|---|
| 348 | * Generic GET_STATUS request cannot be used because of the difference
|
|---|
| 349 | * in status data size (2B vs. 4B)*/
|
|---|
| 350 | const usb_device_request_setup_packet_t request = {
|
|---|
| 351 | .request_type = USB_HUB_REQ_TYPE_GET_PORT_STATUS,
|
|---|
| 352 | .request = USB_HUB_REQUEST_GET_STATUS,
|
|---|
| 353 | .value = 0,
|
|---|
| 354 | .index = port->port_number,
|
|---|
| 355 | .length = sizeof(usb_port_status_t),
|
|---|
| 356 | };
|
|---|
| 357 | size_t recv_size;
|
|---|
| 358 | usb_port_status_t status_tmp;
|
|---|
| 359 |
|
|---|
| 360 | const int rc = usb_pipe_control_read(port->control_pipe,
|
|---|
| 361 | &request, sizeof(usb_device_request_setup_packet_t),
|
|---|
| 362 | &status_tmp, sizeof(status_tmp), &recv_size);
|
|---|
| 363 | if (rc != EOK) {
|
|---|
| 364 | return rc;
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | if (recv_size != sizeof (status_tmp)) {
|
|---|
| 368 | return ELIMIT;
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | if (status != NULL) {
|
|---|
| 372 | *status = status_tmp;
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 | return EOK;
|
|---|
| 376 | }
|
|---|
| 377 | /*----------------------------------------------------------------------------*/
|
|---|
| 378 | /** Callback for enabling a specific port.
|
|---|
| 379 | *
|
|---|
| 380 | * We wait on a CV until port is reseted.
|
|---|
| 381 | * That is announced via change on interrupt pipe.
|
|---|
| 382 | *
|
|---|
| 383 | * @param port_no Port number (starting at 1).
|
|---|
| 384 | * @param arg Custom argument, points to @c usb_hub_dev_t.
|
|---|
| 385 | * @return Error code.
|
|---|
| 386 | */
|
|---|
| 387 | static int enable_port_callback(void *arg)
|
|---|
| 388 | {
|
|---|
| 389 | usb_hub_port_t *port = arg;
|
|---|
| 390 | assert(port);
|
|---|
| 391 | const int rc =
|
|---|
| 392 | usb_hub_port_set_feature(port, USB_HUB_FEATURE_PORT_RESET);
|
|---|
| 393 | if (rc != EOK) {
|
|---|
| 394 | usb_log_warning("Port reset failed: %s.\n", str_error(rc));
|
|---|
| 395 | return rc;
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | /*
|
|---|
| 399 | * Wait until reset completes.
|
|---|
| 400 | */
|
|---|
| 401 | fibril_mutex_lock(&port->mutex);
|
|---|
| 402 | while (!port->reset_completed) {
|
|---|
| 403 | fibril_condvar_wait(&port->reset_cv, &port->mutex);
|
|---|
| 404 | }
|
|---|
| 405 | fibril_mutex_unlock(&port->mutex);
|
|---|
| 406 |
|
|---|
| 407 | return port->reset_okay ? EOK : ESTALL;
|
|---|
| 408 | }
|
|---|
| 409 | /*----------------------------------------------------------------------------*/
|
|---|
| 410 | /** Fibril for adding a new device.
|
|---|
| 411 | *
|
|---|
| 412 | * Separate fibril is needed because the port reset completion is announced
|
|---|
| 413 | * via interrupt pipe and thus we cannot block here.
|
|---|
| 414 | *
|
|---|
| 415 | * @param arg Pointer to struct add_device_phase1.
|
|---|
| 416 | * @return 0 Always.
|
|---|
| 417 | */
|
|---|
| 418 | int add_device_phase1_worker_fibril(void *arg)
|
|---|
| 419 | {
|
|---|
| 420 | struct add_device_phase1 *data = arg;
|
|---|
| 421 | assert(data);
|
|---|
| 422 |
|
|---|
| 423 | usb_address_t new_address;
|
|---|
| 424 | ddf_fun_t *child_fun;
|
|---|
| 425 |
|
|---|
| 426 | const int rc = usb_hc_new_device_wrapper(data->hub->usb_device->ddf_dev,
|
|---|
| 427 | &data->hub->usb_device->hc_conn, data->speed, enable_port_callback,
|
|---|
| 428 | data->port, &new_address, NULL, NULL, &child_fun);
|
|---|
| 429 |
|
|---|
| 430 | if (rc == EOK) {
|
|---|
| 431 | fibril_mutex_lock(&data->port->mutex);
|
|---|
| 432 | data->port->attached_device.fun = child_fun;
|
|---|
| 433 | data->port->attached_device.address = new_address;
|
|---|
| 434 | fibril_mutex_unlock(&data->port->mutex);
|
|---|
| 435 |
|
|---|
| 436 | usb_log_info("Detected new device on `%s' (port %zu), "
|
|---|
| 437 | "address %d (handle %" PRIun ").\n",
|
|---|
| 438 | data->hub->usb_device->ddf_dev->name,
|
|---|
| 439 | data->port->port_number, new_address, child_fun->handle);
|
|---|
| 440 | } else {
|
|---|
| 441 | usb_log_error("Failed registering device on port %zu: %s.\n",
|
|---|
| 442 | data->port->port_number, str_error(rc));
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 |
|
|---|
| 446 | fibril_mutex_lock(&data->hub->pending_ops_mutex);
|
|---|
| 447 | assert(data->hub->pending_ops_count > 0);
|
|---|
| 448 | --data->hub->pending_ops_count;
|
|---|
| 449 | fibril_condvar_signal(&data->hub->pending_ops_cv);
|
|---|
| 450 | fibril_mutex_unlock(&data->hub->pending_ops_mutex);
|
|---|
| 451 |
|
|---|
| 452 | free(arg);
|
|---|
| 453 |
|
|---|
| 454 | return rc;
|
|---|
| 455 | }
|
|---|
| 456 | /*----------------------------------------------------------------------------*/
|
|---|
| 457 | /** Start device adding when connection change is detected.
|
|---|
| 458 | *
|
|---|
| 459 | * This fires a new fibril to complete the device addition.
|
|---|
| 460 | *
|
|---|
| 461 | * @param hub Hub where the change occured.
|
|---|
| 462 | * @param port Port index (starting at 1).
|
|---|
| 463 | * @param speed Speed of the device.
|
|---|
| 464 | * @return Error code.
|
|---|
| 465 | */
|
|---|
| 466 | static int create_add_device_fibril(usb_hub_port_t *port, usb_hub_dev_t *hub,
|
|---|
| 467 | usb_speed_t speed)
|
|---|
| 468 | {
|
|---|
| 469 | assert(hub);
|
|---|
| 470 | assert(port);
|
|---|
| 471 | struct add_device_phase1 *data
|
|---|
| 472 | = malloc(sizeof(struct add_device_phase1));
|
|---|
| 473 | if (data == NULL) {
|
|---|
| 474 | return ENOMEM;
|
|---|
| 475 | }
|
|---|
| 476 | data->hub = hub;
|
|---|
| 477 | data->port = port;
|
|---|
| 478 | data->speed = speed;
|
|---|
| 479 |
|
|---|
| 480 | fibril_mutex_lock(&port->mutex);
|
|---|
| 481 | port->reset_completed = false;
|
|---|
| 482 | fibril_mutex_unlock(&port->mutex);
|
|---|
| 483 |
|
|---|
| 484 | fid_t fibril = fibril_create(add_device_phase1_worker_fibril, data);
|
|---|
| 485 | if (fibril == 0) {
|
|---|
| 486 | free(data);
|
|---|
| 487 | return ENOMEM;
|
|---|
| 488 | }
|
|---|
| 489 | fibril_mutex_lock(&hub->pending_ops_mutex);
|
|---|
| 490 | ++hub->pending_ops_count;
|
|---|
| 491 | fibril_mutex_unlock(&hub->pending_ops_mutex);
|
|---|
| 492 | fibril_add_ready(fibril);
|
|---|
| 493 |
|
|---|
| 494 | return EOK;
|
|---|
| 495 | }
|
|---|
| 496 |
|
|---|
| 497 | /**
|
|---|
| 498 | * @}
|
|---|
| 499 | */
|
|---|