[2ad98fd] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
| 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 |
|
---|
| 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 |
|
---|
| 44 | #include "ports.h"
|
---|
| 45 | #include "usbhub.h"
|
---|
| 46 | #include "usbhub_private.h"
|
---|
| 47 | #include "port_status.h"
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 | /** Information for fibril for device discovery. */
|
---|
| 51 | struct add_device_phase1 {
|
---|
| 52 | usb_hub_info_t *hub;
|
---|
| 53 | size_t port;
|
---|
| 54 | usb_speed_t speed;
|
---|
| 55 | };
|
---|
| 56 |
|
---|
| 57 | static void usb_hub_removed_device(
|
---|
| 58 | usb_hub_info_t * hub, uint16_t port);
|
---|
| 59 |
|
---|
| 60 | static void usb_hub_port_reset_completed(usb_hub_info_t * hub,
|
---|
| 61 | uint16_t port, uint32_t status);
|
---|
| 62 |
|
---|
| 63 | static void usb_hub_port_over_current(usb_hub_info_t * hub,
|
---|
| 64 | uint16_t port, uint32_t status);
|
---|
| 65 |
|
---|
| 66 | static int get_port_status(usb_pipe_t *ctrl_pipe, size_t port,
|
---|
| 67 | usb_port_status_t *status);
|
---|
| 68 |
|
---|
| 69 | static int enable_port_callback(int port_no, void *arg);
|
---|
| 70 |
|
---|
| 71 | static int add_device_phase1_worker_fibril(void *arg);
|
---|
| 72 |
|
---|
| 73 | static int create_add_device_fibril(usb_hub_info_t *hub, size_t port,
|
---|
| 74 | usb_speed_t speed);
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | * Process interrupts on given hub port
|
---|
| 78 | *
|
---|
| 79 | * Accepts connection, over current and port reset change.
|
---|
| 80 | * @param hub hub representation
|
---|
| 81 | * @param port port number, starting from 1
|
---|
| 82 | */
|
---|
| 83 | void usb_hub_process_interrupt(usb_hub_info_t * hub,
|
---|
| 84 | uint16_t port) {
|
---|
| 85 | usb_log_debug("interrupt at port %d\n", port);
|
---|
| 86 | //determine type of change
|
---|
| 87 | //usb_pipe_t *pipe = hub->control_pipe;
|
---|
| 88 |
|
---|
| 89 | int opResult;
|
---|
| 90 |
|
---|
| 91 | usb_port_status_t status;
|
---|
| 92 | opResult = get_port_status(&hub->usb_device->ctrl_pipe, port, &status);
|
---|
| 93 | if (opResult != EOK) {
|
---|
| 94 | usb_log_error("Failed to get port %zu status: %s.\n",
|
---|
| 95 | port, str_error(opResult));
|
---|
| 96 | return;
|
---|
| 97 | }
|
---|
| 98 | //connection change
|
---|
| 99 | if (usb_port_is_status(status, USB_HUB_FEATURE_C_PORT_CONNECTION)) {
|
---|
| 100 | bool device_connected = usb_port_is_status(status,
|
---|
| 101 | USB_HUB_FEATURE_PORT_CONNECTION);
|
---|
| 102 | usb_log_debug("Connection change on port %zu: %s.\n", port,
|
---|
| 103 | device_connected ? "device attached" : "device removed");
|
---|
| 104 |
|
---|
| 105 | if (device_connected) {
|
---|
| 106 | opResult = create_add_device_fibril(hub, port,
|
---|
| 107 | usb_port_speed(status));
|
---|
| 108 | if (opResult != EOK) {
|
---|
| 109 | usb_log_error(
|
---|
| 110 | "Cannot handle change on port %zu: %s.\n",
|
---|
| 111 | str_error(opResult));
|
---|
| 112 | }
|
---|
| 113 | } else {
|
---|
| 114 | usb_hub_removed_device(hub, port);
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | //over current
|
---|
| 118 | if (usb_port_is_status(status, USB_HUB_FEATURE_C_PORT_OVER_CURRENT)) {
|
---|
| 119 | //check if it was not auto-resolved
|
---|
| 120 | usb_log_debug("overcurrent change on port\n");
|
---|
| 121 | usb_hub_port_over_current(hub, port, status);
|
---|
| 122 | }
|
---|
| 123 | //port reset
|
---|
| 124 | if (usb_port_is_status(status, USB_HUB_FEATURE_C_PORT_RESET)) {
|
---|
| 125 | usb_hub_port_reset_completed(hub, port, status);
|
---|
| 126 | }
|
---|
| 127 | usb_log_debug("status x%x : %d\n ", status, status);
|
---|
| 128 |
|
---|
| 129 | usb_port_status_set_bit(
|
---|
| 130 | &status, USB_HUB_FEATURE_C_PORT_CONNECTION,false);
|
---|
| 131 | usb_port_status_set_bit(
|
---|
| 132 | &status, USB_HUB_FEATURE_PORT_RESET,false);
|
---|
| 133 | usb_port_status_set_bit(
|
---|
| 134 | &status, USB_HUB_FEATURE_C_PORT_RESET,false);
|
---|
| 135 | usb_port_status_set_bit(
|
---|
| 136 | &status, USB_HUB_FEATURE_C_PORT_OVER_CURRENT,false);
|
---|
| 137 | /// \TODO what about port power change?
|
---|
| 138 | if (status >> 16) {
|
---|
| 139 | usb_log_info("there was unsupported change on port %d: %X\n",
|
---|
| 140 | port, status);
|
---|
| 141 |
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 | /**
|
---|
| 147 | * routine called when a device on port has been removed
|
---|
| 148 | *
|
---|
| 149 | * If the device on port had default address, it releases default address.
|
---|
| 150 | * Otherwise does not do anything, because DDF does not allow to remove device
|
---|
| 151 | * from it`s device tree.
|
---|
| 152 | * @param hub hub representation
|
---|
| 153 | * @param port port number, starting from 1
|
---|
| 154 | */
|
---|
| 155 | static void usb_hub_removed_device(
|
---|
| 156 | usb_hub_info_t * hub, uint16_t port) {
|
---|
| 157 |
|
---|
| 158 | int opResult = usb_hub_clear_port_feature(hub->control_pipe,
|
---|
| 159 | port, USB_HUB_FEATURE_C_PORT_CONNECTION);
|
---|
| 160 | if (opResult != EOK) {
|
---|
| 161 | usb_log_warning("could not clear port-change-connection flag\n");
|
---|
| 162 | }
|
---|
| 163 | /** \TODO remove device from device manager - not yet implemented in
|
---|
| 164 | * devide manager
|
---|
| 165 | */
|
---|
| 166 |
|
---|
| 167 | //close address
|
---|
| 168 | //if (hub->attached_devs[port].address != 0) {
|
---|
| 169 | if(hub->ports[port].attached_device.address >= 0){
|
---|
| 170 | /*uncomment this code to use it when DDF allows device removal
|
---|
| 171 | opResult = usb_hc_unregister_device(
|
---|
| 172 | &hub->connection,
|
---|
| 173 | hub->attached_devs[port].address);
|
---|
| 174 | if(opResult != EOK) {
|
---|
| 175 | dprintf(USB_LOG_LEVEL_WARNING, "could not release "
|
---|
| 176 | "address of "
|
---|
| 177 | "removed device: %d", opResult);
|
---|
| 178 | }
|
---|
| 179 | hub->attached_devs[port].address = 0;
|
---|
| 180 | hub->attached_devs[port].handle = 0;
|
---|
| 181 | */
|
---|
| 182 | } else {
|
---|
[46e078a] | 183 | // TODO: is this really reason to print a warning?
|
---|
| 184 | usb_log_warning("Device removed before being registered.\n");
|
---|
| 185 |
|
---|
| 186 | /*
|
---|
| 187 | * Device was removed before port reset completed.
|
---|
| 188 | * We will announce a failed port reset to unblock the
|
---|
| 189 | * port reset callback from new device wrapper.
|
---|
| 190 | */
|
---|
| 191 | usb_hub_port_t *the_port = hub->ports + port;
|
---|
| 192 | fibril_mutex_lock(&the_port->reset_mutex);
|
---|
| 193 | the_port->reset_completed = true;
|
---|
| 194 | the_port->reset_okay = false;
|
---|
| 195 | fibril_condvar_broadcast(&the_port->reset_cv);
|
---|
| 196 | fibril_mutex_unlock(&the_port->reset_mutex);
|
---|
[2ad98fd] | 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 | /**
|
---|
| 202 | * Process port reset change
|
---|
| 203 | *
|
---|
| 204 | * After this change port should be enabled, unless some problem occured.
|
---|
| 205 | * This functions triggers second phase of enabling new device.
|
---|
| 206 | * @param hub
|
---|
| 207 | * @param port
|
---|
| 208 | * @param status
|
---|
| 209 | */
|
---|
| 210 | static void usb_hub_port_reset_completed(usb_hub_info_t * hub,
|
---|
| 211 | uint16_t port, uint32_t status){
|
---|
| 212 | usb_log_debug("Port %zu reset complete.\n", port);
|
---|
| 213 | if (usb_port_is_status(status, USB_HUB_FEATURE_PORT_ENABLE)) {
|
---|
| 214 | /* Finalize device adding. */
|
---|
| 215 | usb_hub_port_t *the_port = hub->ports + port;
|
---|
| 216 | fibril_mutex_lock(&the_port->reset_mutex);
|
---|
| 217 | the_port->reset_completed = true;
|
---|
[46e078a] | 218 | the_port->reset_okay = true;
|
---|
[2ad98fd] | 219 | fibril_condvar_broadcast(&the_port->reset_cv);
|
---|
| 220 | fibril_mutex_unlock(&the_port->reset_mutex);
|
---|
| 221 | } else {
|
---|
| 222 | usb_log_warning(
|
---|
| 223 | "Port %zu reset complete but port not enabled.\n",
|
---|
| 224 | port);
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | /**
|
---|
| 229 | * Process over current condition on port.
|
---|
| 230 | *
|
---|
| 231 | * Turn off the power on the port.
|
---|
| 232 | *
|
---|
| 233 | * @param hub hub representation
|
---|
| 234 | * @param port port number, starting from 1
|
---|
| 235 | */
|
---|
| 236 | static void usb_hub_port_over_current(usb_hub_info_t * hub,
|
---|
| 237 | uint16_t port, uint32_t status) {
|
---|
| 238 | int opResult;
|
---|
| 239 | if(usb_port_is_status(status, USB_HUB_FEATURE_PORT_OVER_CURRENT)){
|
---|
| 240 | opResult = usb_hub_clear_port_feature(hub->control_pipe,
|
---|
| 241 | port, USB_HUB_FEATURE_PORT_POWER);
|
---|
| 242 | if (opResult != EOK) {
|
---|
| 243 | usb_log_error("cannot power off port %d; %d\n",
|
---|
| 244 | port, opResult);
|
---|
| 245 | }
|
---|
| 246 | }else{
|
---|
| 247 | opResult = usb_hub_set_port_feature(hub->control_pipe,
|
---|
| 248 | port, USB_HUB_FEATURE_PORT_POWER);
|
---|
| 249 | if (opResult != EOK) {
|
---|
| 250 | usb_log_error("cannot power on port %d; %d\n",
|
---|
| 251 | port, opResult);
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | /** Retrieve port status.
|
---|
| 257 | *
|
---|
| 258 | * @param[in] ctrl_pipe Control pipe to use.
|
---|
| 259 | * @param[in] port Port number (starting at 1).
|
---|
| 260 | * @param[out] status Where to store the port status.
|
---|
| 261 | * @return Error code.
|
---|
| 262 | */
|
---|
| 263 | static int get_port_status(usb_pipe_t *ctrl_pipe, size_t port,
|
---|
| 264 | usb_port_status_t *status)
|
---|
| 265 | {
|
---|
| 266 | size_t recv_size;
|
---|
| 267 | usb_device_request_setup_packet_t request;
|
---|
| 268 | usb_port_status_t status_tmp;
|
---|
| 269 |
|
---|
| 270 | usb_hub_set_port_status_request(&request, port);
|
---|
| 271 | int rc = usb_pipe_control_read(ctrl_pipe,
|
---|
| 272 | &request, sizeof(usb_device_request_setup_packet_t),
|
---|
| 273 | &status_tmp, sizeof(status_tmp), &recv_size);
|
---|
| 274 | if (rc != EOK) {
|
---|
| 275 | return rc;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | if (recv_size != sizeof (status_tmp)) {
|
---|
| 279 | return ELIMIT;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | if (status != NULL) {
|
---|
| 283 | *status = status_tmp;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | return EOK;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | /** Callback for enabling a specific port.
|
---|
| 290 | *
|
---|
| 291 | * We wait on a CV until port is reseted.
|
---|
| 292 | * That is announced via change on interrupt pipe.
|
---|
| 293 | *
|
---|
| 294 | * @param port_no Port number (starting at 1).
|
---|
| 295 | * @param arg Custom argument, points to @c usb_hub_info_t.
|
---|
| 296 | * @return Error code.
|
---|
| 297 | */
|
---|
| 298 | static int enable_port_callback(int port_no, void *arg)
|
---|
| 299 | {
|
---|
| 300 | usb_hub_info_t *hub = arg;
|
---|
| 301 | int rc;
|
---|
| 302 | usb_device_request_setup_packet_t request;
|
---|
| 303 | usb_hub_port_t *my_port = hub->ports + port_no;
|
---|
| 304 |
|
---|
| 305 | usb_hub_set_reset_port_request(&request, port_no);
|
---|
| 306 | rc = usb_pipe_control_write(hub->control_pipe,
|
---|
| 307 | &request, sizeof(request), NULL, 0);
|
---|
| 308 | if (rc != EOK) {
|
---|
| 309 | usb_log_warning("Port reset failed: %s.\n", str_error(rc));
|
---|
| 310 | return rc;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | /*
|
---|
| 314 | * Wait until reset completes.
|
---|
| 315 | */
|
---|
| 316 | fibril_mutex_lock(&my_port->reset_mutex);
|
---|
| 317 | while (!my_port->reset_completed) {
|
---|
| 318 | fibril_condvar_wait(&my_port->reset_cv, &my_port->reset_mutex);
|
---|
| 319 | }
|
---|
| 320 | fibril_mutex_unlock(&my_port->reset_mutex);
|
---|
| 321 |
|
---|
| 322 | /* Clear the port reset change. */
|
---|
| 323 | rc = usb_hub_clear_port_feature(hub->control_pipe,
|
---|
| 324 | port_no, USB_HUB_FEATURE_C_PORT_RESET);
|
---|
| 325 | if (rc != EOK) {
|
---|
| 326 | usb_log_error("Failed to clear port %d reset feature: %s.\n",
|
---|
| 327 | port_no, str_error(rc));
|
---|
| 328 | return rc;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
[46e078a] | 331 | if (my_port->reset_okay) {
|
---|
| 332 | return EOK;
|
---|
| 333 | } else {
|
---|
| 334 | return ESTALL;
|
---|
| 335 | }
|
---|
[2ad98fd] | 336 | }
|
---|
| 337 |
|
---|
| 338 | /** Fibril for adding a new device.
|
---|
| 339 | *
|
---|
| 340 | * Separate fibril is needed because the port reset completion is announced
|
---|
| 341 | * via interrupt pipe and thus we cannot block here.
|
---|
| 342 | *
|
---|
| 343 | * @param arg Pointer to struct add_device_phase1.
|
---|
| 344 | * @return 0 Always.
|
---|
| 345 | */
|
---|
| 346 | static int add_device_phase1_worker_fibril(void *arg)
|
---|
| 347 | {
|
---|
| 348 | struct add_device_phase1 *data
|
---|
| 349 | = (struct add_device_phase1 *) arg;
|
---|
| 350 |
|
---|
| 351 | usb_address_t new_address;
|
---|
| 352 | devman_handle_t child_handle;
|
---|
| 353 |
|
---|
| 354 | int rc = usb_hc_new_device_wrapper(data->hub->usb_device->ddf_dev,
|
---|
| 355 | &data->hub->connection, data->speed,
|
---|
| 356 | enable_port_callback, (int) data->port, data->hub,
|
---|
| 357 | &new_address, &child_handle,
|
---|
| 358 | NULL, NULL, NULL);
|
---|
| 359 |
|
---|
| 360 | if (rc != EOK) {
|
---|
| 361 | usb_log_error("Failed registering device on port %zu: %s.\n",
|
---|
| 362 | data->port, str_error(rc));
|
---|
| 363 | goto leave;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | data->hub->ports[data->port].attached_device.handle = child_handle;
|
---|
| 367 | data->hub->ports[data->port].attached_device.address = new_address;
|
---|
| 368 |
|
---|
| 369 | usb_log_info("Detected new device on `%s' (port %zu), "
|
---|
| 370 | "address %d (handle %" PRIun ").\n",
|
---|
| 371 | data->hub->usb_device->ddf_dev->name, data->port,
|
---|
| 372 | new_address, child_handle);
|
---|
| 373 |
|
---|
| 374 | leave:
|
---|
| 375 | free(arg);
|
---|
| 376 |
|
---|
| 377 | return EOK;
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 |
|
---|
| 381 | /** Start device adding when connection change is detected.
|
---|
| 382 | *
|
---|
| 383 | * This fires a new fibril to complete the device addition.
|
---|
| 384 | *
|
---|
| 385 | * @param hub Hub where the change occured.
|
---|
| 386 | * @param port Port index (starting at 1).
|
---|
| 387 | * @param speed Speed of the device.
|
---|
| 388 | * @return Error code.
|
---|
| 389 | */
|
---|
| 390 | static int create_add_device_fibril(usb_hub_info_t *hub, size_t port,
|
---|
| 391 | usb_speed_t speed)
|
---|
| 392 | {
|
---|
| 393 | struct add_device_phase1 *data
|
---|
| 394 | = malloc(sizeof(struct add_device_phase1));
|
---|
| 395 | if (data == NULL) {
|
---|
| 396 | return ENOMEM;
|
---|
| 397 | }
|
---|
| 398 | data->hub = hub;
|
---|
| 399 | data->port = port;
|
---|
| 400 | data->speed = speed;
|
---|
| 401 |
|
---|
| 402 | usb_hub_port_t *the_port = hub->ports + port;
|
---|
| 403 |
|
---|
| 404 | fibril_mutex_lock(&the_port->reset_mutex);
|
---|
| 405 | the_port->reset_completed = false;
|
---|
| 406 | fibril_mutex_unlock(&the_port->reset_mutex);
|
---|
| 407 |
|
---|
| 408 | int rc = usb_hub_clear_port_feature(hub->control_pipe, port,
|
---|
| 409 | USB_HUB_FEATURE_C_PORT_CONNECTION);
|
---|
| 410 | if (rc != EOK) {
|
---|
| 411 | free(data);
|
---|
| 412 | usb_log_warning("Failed to clear port change flag: %s.\n",
|
---|
| 413 | str_error(rc));
|
---|
| 414 | return rc;
|
---|
| 415 | }
|
---|
| 416 |
|
---|
| 417 | fid_t fibril = fibril_create(add_device_phase1_worker_fibril, data);
|
---|
| 418 | if (fibril == 0) {
|
---|
| 419 | free(data);
|
---|
| 420 | return ENOMEM;
|
---|
| 421 | }
|
---|
| 422 | fibril_add_ready(fibril);
|
---|
| 423 |
|
---|
| 424 | return EOK;
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | /**
|
---|
| 428 | * @}
|
---|
| 429 | */
|
---|