[e080332] | 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 | */
|
---|
[281ebae] | 28 | /** @addtogroup drvusbhub
|
---|
[e080332] | 29 | * @{
|
---|
| 30 | */
|
---|
| 31 | /** @file
|
---|
| 32 | * @brief usb hub main functionality
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[eb1a2f4] | 35 | #include <ddf/driver.h>
|
---|
[e080332] | 36 | #include <bool.h>
|
---|
| 37 | #include <errno.h>
|
---|
[4e8e1f5] | 38 | #include <str_error.h>
|
---|
[3e490eb] | 39 | #include <inttypes.h>
|
---|
[e080332] | 40 |
|
---|
[71ed4849] | 41 | #include <usb_iface.h>
|
---|
[357a302] | 42 | #include <usb/ddfiface.h>
|
---|
[e080332] | 43 | #include <usb/descriptor.h>
|
---|
[7d521e24] | 44 | #include <usb/dev/recognise.h>
|
---|
| 45 | #include <usb/dev/request.h>
|
---|
[e080332] | 46 | #include <usb/classes/hub.h>
|
---|
[7d521e24] | 47 | #include <usb/dev/poll.h>
|
---|
[cd4b184] | 48 | #include <stdio.h>
|
---|
[e080332] | 49 |
|
---|
| 50 | #include "usbhub.h"
|
---|
| 51 | #include "usbhub_private.h"
|
---|
| 52 | #include "port_status.h"
|
---|
[7d521e24] | 53 | #include <usb/usb.h>
|
---|
| 54 | #include <usb/dev/pipes.h>
|
---|
| 55 | #include <usb/classes/classes.h>
|
---|
[e080332] | 56 |
|
---|
[df3ad97] | 57 |
|
---|
[a209648] | 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 |
|
---|
[f35b294] | 64 | static int usb_hub_start_hub_fibril(usb_hub_info_t * hub_info);
|
---|
[a209648] | 65 |
|
---|
| 66 | static int usb_process_hub_over_current(usb_hub_info_t * hub_info,
|
---|
[f35b294] | 67 | usb_hub_status_t status);
|
---|
[a209648] | 68 |
|
---|
[ffca03c] | 69 | static int usb_process_hub_local_power_change(usb_hub_info_t * hub_info,
|
---|
[f35b294] | 70 | usb_hub_status_t status);
|
---|
[a209648] | 71 |
|
---|
| 72 | static void usb_hub_process_global_interrupt(usb_hub_info_t * hub_info);
|
---|
| 73 |
|
---|
[aca3489] | 74 | static void usb_hub_polling_terminted_callback(usb_device_t * device,
|
---|
| 75 | bool was_error, void * data);
|
---|
| 76 |
|
---|
[b495a93] | 77 |
|
---|
[e080332] | 78 | //*********************************************
|
---|
| 79 | //
|
---|
| 80 | // hub driver code, initialization
|
---|
| 81 | //
|
---|
| 82 | //*********************************************
|
---|
| 83 |
|
---|
[a209648] | 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(
|
---|
[f35b294] | 98 | &hub_info->connection,
|
---|
| 99 | hub_info->usb_device->ddf_dev);
|
---|
[a209648] | 100 | if (opResult != EOK) {
|
---|
| 101 | usb_log_error("could not initialize connection to device, "
|
---|
[f35b294] | 102 | "errno %d\n",
|
---|
| 103 | opResult);
|
---|
[a209648] | 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, errno %d\n",
|
---|
[f35b294] | 112 | opResult);
|
---|
[a209648] | 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) {
|
---|
[f35b294] | 119 | usb_log_error("could process hub specific info, errno %d\n",
|
---|
| 120 | opResult);
|
---|
[a209648] | 121 | free(hub_info);
|
---|
| 122 | return opResult;
|
---|
| 123 | }
|
---|
[9063484] | 124 |
|
---|
| 125 | usb_log_debug("Creating 'hub' function in DDF.\n");
|
---|
[a209648] | 126 | ddf_fun_t *hub_fun = ddf_fun_create(hub_info->usb_device->ddf_dev,
|
---|
[f35b294] | 127 | fun_exposed, "hub");
|
---|
[a209648] | 128 | assert(hub_fun != NULL);
|
---|
| 129 | hub_fun->ops = NULL;
|
---|
| 130 |
|
---|
[f35b294] | 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);
|
---|
[3e490eb] | 135 |
|
---|
[f35b294] | 136 | opResult = usb_hub_start_hub_fibril(hub_info);
|
---|
| 137 | if(opResult!=EOK)
|
---|
[3e490eb] | 138 | free(hub_info);
|
---|
[f35b294] | 139 | return opResult;
|
---|
[a209648] | 140 | }
|
---|
| 141 |
|
---|
| 142 |
|
---|
[1e1b1a9] | 143 | /** Callback for polling hub for changes.
|
---|
[3e490eb] | 144 | *
|
---|
| 145 | * @param dev Device where the change occured.
|
---|
| 146 | * @param change_bitmap Bitmap of changed ports.
|
---|
| 147 | * @param change_bitmap_size Size of the bitmap in bytes.
|
---|
| 148 | * @param arg Custom argument, points to @c usb_hub_info_t.
|
---|
| 149 | * @return Whether to continue polling.
|
---|
| 150 | */
|
---|
| 151 | bool hub_port_changes_callback(usb_device_t *dev,
|
---|
[f35b294] | 152 | uint8_t *change_bitmap, size_t change_bitmap_size, void *arg) {
|
---|
[c1693dae] | 153 | usb_log_debug("hub_port_changes_callback\n");
|
---|
[3e490eb] | 154 | usb_hub_info_t *hub = (usb_hub_info_t *) arg;
|
---|
| 155 |
|
---|
| 156 | /* FIXME: check that we received enough bytes. */
|
---|
| 157 | if (change_bitmap_size == 0) {
|
---|
| 158 | goto leave;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[1e1b1a9] | 161 | bool change;
|
---|
[f35b294] | 162 | change = ((uint8_t*) change_bitmap)[0] & 1;
|
---|
| 163 | if (change) {
|
---|
[1e1b1a9] | 164 | usb_hub_process_global_interrupt(hub);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[3e490eb] | 167 | size_t port;
|
---|
| 168 | for (port = 1; port < hub->port_count + 1; port++) {
|
---|
| 169 | bool change = (change_bitmap[port / 8] >> (port % 8)) % 2;
|
---|
| 170 | if (change) {
|
---|
| 171 | usb_hub_process_interrupt(hub, port);
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | leave:
|
---|
| 175 | /* FIXME: proper interval. */
|
---|
[9014dcd] | 176 | async_usleep(1000 * 250);
|
---|
[3e490eb] | 177 |
|
---|
| 178 | return true;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[1e1b1a9] | 181 |
|
---|
[a209648] | 182 | //*********************************************
|
---|
| 183 | //
|
---|
| 184 | // support functions
|
---|
| 185 | //
|
---|
| 186 | //*********************************************
|
---|
| 187 |
|
---|
[15b0432] | 188 | /**
|
---|
[09daa8b] | 189 | * create usb_hub_info_t structure
|
---|
| 190 | *
|
---|
| 191 | * Does only basic copying of known information into new structure.
|
---|
| 192 | * @param usb_dev usb device structure
|
---|
| 193 | * @return basic usb_hub_info_t structure
|
---|
[15b0432] | 194 | */
|
---|
[09daa8b] | 195 | static usb_hub_info_t * usb_hub_info_create(usb_device_t * usb_dev) {
|
---|
[f35b294] | 196 | usb_hub_info_t * result = malloc(sizeof(usb_hub_info_t));
|
---|
[195890b] | 197 | if (!result) return NULL;
|
---|
[09daa8b] | 198 | result->usb_device = usb_dev;
|
---|
| 199 | result->status_change_pipe = usb_dev->pipes[0].pipe;
|
---|
| 200 | result->control_pipe = &usb_dev->ctrl_pipe;
|
---|
| 201 | result->is_default_address_used = false;
|
---|
| 202 | return result;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | /**
|
---|
[e6223239] | 206 | * Load hub-specific information into hub_info structure and process if needed
|
---|
[09daa8b] | 207 | *
|
---|
| 208 | * Particularly read port count and initialize structure holding port
|
---|
[e6223239] | 209 | * information. If there are non-removable devices, start initializing them.
|
---|
[09daa8b] | 210 | * This function is hub-specific and should be run only after the hub is
|
---|
| 211 | * configured using usb_hub_set_configuration function.
|
---|
[df3ad97] | 212 | * @param hub_info hub representation
|
---|
[09daa8b] | 213 | * @return error code
|
---|
| 214 | */
|
---|
[195890b] | 215 | static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info) {
|
---|
[09daa8b] | 216 | // get hub descriptor
|
---|
| 217 | usb_log_debug("creating serialized descriptor\n");
|
---|
[c1693dae] | 218 | uint8_t serialized_descriptor[USB_HUB_MAX_DESCRIPTOR_SIZE];
|
---|
[09daa8b] | 219 | usb_hub_descriptor_t * descriptor;
|
---|
[040ab02] | 220 | int opResult;
|
---|
[09daa8b] | 221 |
|
---|
| 222 | size_t received_size;
|
---|
[1c89f74] | 223 | opResult = usb_request_get_descriptor(hub_info->control_pipe,
|
---|
[f35b294] | 224 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 225 | USB_DESCTYPE_HUB, 0, 0, serialized_descriptor,
|
---|
| 226 | USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
|
---|
[09daa8b] | 227 |
|
---|
| 228 | if (opResult != EOK) {
|
---|
[195890b] | 229 | usb_log_error("failed when receiving hub descriptor, "
|
---|
[f35b294] | 230 | "badcode = %d\n",
|
---|
| 231 | opResult);
|
---|
[09daa8b] | 232 | free(serialized_descriptor);
|
---|
[15b0432] | 233 | return opResult;
|
---|
| 234 | }
|
---|
[09daa8b] | 235 | usb_log_debug2("deserializing descriptor\n");
|
---|
[cd5b878] | 236 | descriptor = usb_create_deserialized_hub_desriptor(
|
---|
| 237 | serialized_descriptor);
|
---|
[195890b] | 238 | if (descriptor == NULL) {
|
---|
[09daa8b] | 239 | usb_log_warning("could not deserialize descriptor \n");
|
---|
[c1693dae] | 240 | return ENOMEM;
|
---|
[15b0432] | 241 | }
|
---|
[195890b] | 242 | usb_log_debug("setting port count to %d\n", descriptor->ports_count);
|
---|
[09daa8b] | 243 | hub_info->port_count = descriptor->ports_count;
|
---|
[1e1b1a9] | 244 | /// \TODO this is not semantically correct
|
---|
[c1693dae] | 245 | bool is_power_switched =
|
---|
| 246 | ((descriptor->hub_characteristics & 1) ==0);
|
---|
| 247 | bool has_individual_port_powering =
|
---|
| 248 | ((descriptor->hub_characteristics & 1) !=0);
|
---|
[f35b294] | 249 | hub_info->ports = malloc(
|
---|
| 250 | sizeof (usb_hub_port_t) * (hub_info->port_count + 1));
|
---|
[361fcec] | 251 | if(!hub_info->ports){
|
---|
| 252 | return ENOMEM;
|
---|
| 253 | }
|
---|
[3e490eb] | 254 | size_t port;
|
---|
[9014dcd] | 255 | for (port = 0; port < hub_info->port_count + 1; ++port) {
|
---|
[3e490eb] | 256 | usb_hub_port_init(&hub_info->ports[port]);
|
---|
[15b0432] | 257 | }
|
---|
[c1693dae] | 258 | if(is_power_switched){
|
---|
| 259 | usb_log_debug("is_power_switched\n");
|
---|
[9014dcd] | 260 |
|
---|
| 261 | if(!has_individual_port_powering){
|
---|
[c1693dae] | 262 | usb_log_debug("!has_individual_port_powering\n");
|
---|
| 263 | opResult = usb_hub_set_feature(hub_info->control_pipe,
|
---|
| 264 | USB_HUB_FEATURE_C_HUB_LOCAL_POWER);
|
---|
| 265 | if (opResult != EOK) {
|
---|
[4125b7d] | 266 | usb_log_error("cannot power hub: %s\n",
|
---|
| 267 | str_error(opResult));
|
---|
[c1693dae] | 268 | }
|
---|
[58226b4] | 269 | }
|
---|
[361fcec] | 270 |
|
---|
| 271 | for (port = 1; port <= hub_info->port_count; ++port) {
|
---|
| 272 | usb_log_debug("Powering port %zu.\n",port);
|
---|
| 273 | opResult = usb_hub_set_port_feature(hub_info->control_pipe,
|
---|
| 274 | port, USB_HUB_FEATURE_PORT_POWER);
|
---|
| 275 | if (opResult != EOK) {
|
---|
| 276 | usb_log_error("cannot power on port %zu: %s.\n",
|
---|
| 277 | port, str_error(opResult));
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[c1693dae] | 281 | }else{
|
---|
[361fcec] | 282 | usb_log_debug("!is_power_switched, not going to be powered\n");
|
---|
[15b0432] | 283 | }
|
---|
[09daa8b] | 284 | usb_log_debug2("freeing data\n");
|
---|
[36cd378] | 285 | free(descriptor);
|
---|
[206f71a] | 286 | return EOK;
|
---|
[15b0432] | 287 | }
|
---|
[195890b] | 288 |
|
---|
[15b0432] | 289 | /**
|
---|
[09daa8b] | 290 | * Set configuration of hub
|
---|
| 291 | *
|
---|
| 292 | * Check whether there is at least one configuration and sets the first one.
|
---|
| 293 | * This function should be run prior to running any hub-specific action.
|
---|
[df3ad97] | 294 | * @param hub_info hub representation
|
---|
| 295 | * @return error code
|
---|
[15b0432] | 296 | */
|
---|
[195890b] | 297 | static int usb_hub_set_configuration(usb_hub_info_t * hub_info) {
|
---|
[15b0432] | 298 | //device descriptor
|
---|
[625f1ba] | 299 | usb_standard_device_descriptor_t *std_descriptor
|
---|
[f35b294] | 300 | = &hub_info->usb_device->descriptors.device;
|
---|
[fbefd0e] | 301 | usb_log_debug("hub has %d configurations\n",
|
---|
[f35b294] | 302 | std_descriptor->configuration_count);
|
---|
[195890b] | 303 | if (std_descriptor->configuration_count < 1) {
|
---|
[4d0c40b] | 304 | usb_log_error("there are no configurations available\n");
|
---|
[625f1ba] | 305 | return EINVAL;
|
---|
[15b0432] | 306 | }
|
---|
| 307 |
|
---|
[d70e0a3c] | 308 | usb_standard_configuration_descriptor_t *config_descriptor
|
---|
[f35b294] | 309 | = (usb_standard_configuration_descriptor_t *)
|
---|
| 310 | hub_info->usb_device->descriptors.configuration;
|
---|
[d70e0a3c] | 311 |
|
---|
| 312 | /* Set configuration. */
|
---|
[625f1ba] | 313 | int opResult = usb_request_set_configuration(
|
---|
[f35b294] | 314 | &hub_info->usb_device->ctrl_pipe,
|
---|
| 315 | config_descriptor->configuration_number);
|
---|
[15b0432] | 316 |
|
---|
| 317 | if (opResult != EOK) {
|
---|
[d70e0a3c] | 318 | usb_log_error("Failed to set hub configuration: %s.\n",
|
---|
[f35b294] | 319 | str_error(opResult));
|
---|
[15b0432] | 320 | return opResult;
|
---|
| 321 | }
|
---|
[09daa8b] | 322 | usb_log_debug("\tused configuration %d\n",
|
---|
[f35b294] | 323 | config_descriptor->configuration_number);
|
---|
[625f1ba] | 324 |
|
---|
[15b0432] | 325 | return EOK;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
[e080332] | 328 | /**
|
---|
[f35b294] | 329 | * create and start fibril with hub control loop
|
---|
[1e1b1a9] | 330 | *
|
---|
[f35b294] | 331 | * Before the fibril is started, the control pipe and host controller
|
---|
| 332 | * connection of the hub is open.
|
---|
| 333 | *
|
---|
| 334 | * @param hub_info hub representing structure
|
---|
[6c399765] | 335 | * @return error code
|
---|
[e080332] | 336 | */
|
---|
[f35b294] | 337 | static int usb_hub_start_hub_fibril(usb_hub_info_t * hub_info){
|
---|
[1e1b1a9] | 338 | /*
|
---|
[f35b294] | 339 | * The processing will require opened control pipe and connection
|
---|
| 340 | * to the host controller.
|
---|
| 341 | * It is waste of resources but let's hope there will be less
|
---|
| 342 | * hubs than the phone limit.
|
---|
| 343 | * FIXME: with some proper locking over pipes and session
|
---|
| 344 | * auto destruction, this could work better.
|
---|
[1e1b1a9] | 345 | */
|
---|
[cd5b878] | 346 | int rc = usb_hc_connection_open(&hub_info->connection);
|
---|
[f35b294] | 347 | if (rc != EOK) {
|
---|
[cd5b878] | 348 | //usb_pipe_end_session(hub_info->control_pipe);
|
---|
[f35b294] | 349 | usb_log_error("Failed to open connection to HC: %s.\n",
|
---|
| 350 | str_error(rc));
|
---|
| 351 | return rc;
|
---|
[e93e319] | 352 | }
|
---|
[195890b] | 353 |
|
---|
[f35b294] | 354 | rc = usb_device_auto_poll(hub_info->usb_device, 0,
|
---|
| 355 | hub_port_changes_callback, ((hub_info->port_count + 1) / 8) + 1,
|
---|
[aca3489] | 356 | usb_hub_polling_terminted_callback, hub_info);
|
---|
[f35b294] | 357 | if (rc != EOK) {
|
---|
| 358 | usb_log_error("Failed to create polling fibril: %s.\n",
|
---|
| 359 | str_error(rc));
|
---|
| 360 | free(hub_info);
|
---|
| 361 | return rc;
|
---|
[cd4b184] | 362 | }
|
---|
[f35b294] | 363 |
|
---|
[4125b7d] | 364 | usb_log_info("Controlling hub `%s' (%zu ports).\n",
|
---|
[f35b294] | 365 | hub_info->usb_device->ddf_dev->name, hub_info->port_count);
|
---|
[3e490eb] | 366 | return EOK;
|
---|
| 367 | }
|
---|
| 368 |
|
---|
[f35b294] | 369 | //*********************************************
|
---|
| 370 | //
|
---|
| 371 | // change handling functions
|
---|
| 372 | //
|
---|
| 373 | //*********************************************
|
---|
| 374 |
|
---|
[e080332] | 375 |
|
---|
[3dba1ca] | 376 | /**
|
---|
| 377 | * process hub over current change
|
---|
| 378 | *
|
---|
| 379 | * This means either to power off the hub or power it on.
|
---|
| 380 | * @param hub_info hub instance
|
---|
| 381 | * @param status hub status bitmask
|
---|
| 382 | * @return error code
|
---|
| 383 | */
|
---|
[040ab02] | 384 | static int usb_process_hub_over_current(usb_hub_info_t * hub_info,
|
---|
[f35b294] | 385 | usb_hub_status_t status) {
|
---|
[040ab02] | 386 | int opResult;
|
---|
[f35b294] | 387 | if (usb_hub_is_status(status,USB_HUB_FEATURE_HUB_OVER_CURRENT)){
|
---|
[ffca03c] | 388 | //poweroff all ports
|
---|
| 389 | unsigned int port;
|
---|
| 390 | for(port = 1;port <= hub_info->port_count;++port){
|
---|
| 391 | opResult = usb_hub_clear_port_feature(
|
---|
| 392 | hub_info->control_pipe,port,
|
---|
| 393 | USB_HUB_FEATURE_PORT_POWER);
|
---|
| 394 | if (opResult != EOK) {
|
---|
| 395 | usb_log_warning(
|
---|
| 396 | "cannot power off port %d; %d\n",
|
---|
| 397 | port, opResult);
|
---|
| 398 | }
|
---|
[040ab02] | 399 | }
|
---|
[f35b294] | 400 | } else {
|
---|
[ffca03c] | 401 | //power all ports
|
---|
| 402 | unsigned int port;
|
---|
| 403 | for(port = 1;port <= hub_info->port_count;++port){
|
---|
| 404 | opResult = usb_hub_set_port_feature(
|
---|
| 405 | hub_info->control_pipe,port,
|
---|
| 406 | USB_HUB_FEATURE_PORT_POWER);
|
---|
| 407 | if (opResult != EOK) {
|
---|
| 408 | usb_log_warning(
|
---|
| 409 | "cannot power off port %d; %d\n",
|
---|
| 410 | port, opResult);
|
---|
| 411 | }
|
---|
[040ab02] | 412 | }
|
---|
| 413 | }
|
---|
| 414 | return opResult;
|
---|
| 415 | }
|
---|
| 416 |
|
---|
[3dba1ca] | 417 | /**
|
---|
[ffca03c] | 418 | * process hub local power change
|
---|
[3dba1ca] | 419 | *
|
---|
[ffca03c] | 420 | * This change is ignored.
|
---|
[3dba1ca] | 421 | * @param hub_info hub instance
|
---|
| 422 | * @param status hub status bitmask
|
---|
| 423 | * @return error code
|
---|
| 424 | */
|
---|
[ffca03c] | 425 | static int usb_process_hub_local_power_change(usb_hub_info_t * hub_info,
|
---|
[f35b294] | 426 | usb_hub_status_t status) {
|
---|
[152ec79] | 427 | int opResult = EOK;
|
---|
| 428 | opResult = usb_hub_clear_feature(hub_info->control_pipe,
|
---|
| 429 | USB_HUB_FEATURE_C_HUB_LOCAL_POWER);
|
---|
[9014dcd] | 430 | if (opResult != EOK) {
|
---|
[152ec79] | 431 | usb_log_error("cannnot clear hub power change flag: "
|
---|
| 432 | "%d\n",
|
---|
| 433 | opResult);
|
---|
[040ab02] | 434 | }
|
---|
| 435 | return opResult;
|
---|
| 436 | }
|
---|
| 437 |
|
---|
[3dba1ca] | 438 | /**
|
---|
| 439 | * process hub interrupts
|
---|
| 440 | *
|
---|
| 441 | * The change can be either in the over-current condition or
|
---|
[63d4d4fd] | 442 | * local-power change.
|
---|
[3dba1ca] | 443 | * @param hub_info hub instance
|
---|
| 444 | */
|
---|
[f35b294] | 445 | static void usb_hub_process_global_interrupt(usb_hub_info_t * hub_info) {
|
---|
[040ab02] | 446 | usb_log_debug("global interrupt on a hub\n");
|
---|
| 447 | usb_pipe_t *pipe = hub_info->control_pipe;
|
---|
| 448 | int opResult;
|
---|
| 449 |
|
---|
| 450 | usb_port_status_t status;
|
---|
| 451 | size_t rcvd_size;
|
---|
| 452 | usb_device_request_setup_packet_t request;
|
---|
| 453 | //int opResult;
|
---|
| 454 | usb_hub_set_hub_status_request(&request);
|
---|
| 455 | //endpoint 0
|
---|
| 456 |
|
---|
| 457 | opResult = usb_pipe_control_read(
|
---|
[f35b294] | 458 | pipe,
|
---|
| 459 | &request, sizeof (usb_device_request_setup_packet_t),
|
---|
| 460 | &status, 4, &rcvd_size
|
---|
| 461 | );
|
---|
[040ab02] | 462 | if (opResult != EOK) {
|
---|
| 463 | usb_log_error("could not get hub status\n");
|
---|
| 464 | return;
|
---|
| 465 | }
|
---|
| 466 | if (rcvd_size != sizeof (usb_port_status_t)) {
|
---|
| 467 | usb_log_error("received status has incorrect size\n");
|
---|
| 468 | return;
|
---|
| 469 | }
|
---|
| 470 | //port reset
|
---|
[f35b294] | 471 | if (
|
---|
| 472 | usb_hub_is_status(status,16+USB_HUB_FEATURE_C_HUB_OVER_CURRENT)) {
|
---|
| 473 | usb_process_hub_over_current(hub_info, status);
|
---|
[040ab02] | 474 | }
|
---|
[f35b294] | 475 | if (
|
---|
| 476 | usb_hub_is_status(status,16+USB_HUB_FEATURE_C_HUB_LOCAL_POWER)) {
|
---|
[ffca03c] | 477 | usb_process_hub_local_power_change(hub_info, status);
|
---|
[040ab02] | 478 | }
|
---|
| 479 | }
|
---|
| 480 |
|
---|
[aca3489] | 481 | /**
|
---|
| 482 | * callback called from hub polling fibril when the fibril terminates
|
---|
| 483 | *
|
---|
| 484 | * Should perform a cleanup - deletes hub_info.
|
---|
| 485 | * @param device usb device afected
|
---|
| 486 | * @param was_error indicates that the fibril is stoped due to an error
|
---|
| 487 | * @param data pointer to usb_hub_info_t structure
|
---|
| 488 | */
|
---|
| 489 | static void usb_hub_polling_terminted_callback(usb_device_t * device,
|
---|
| 490 | bool was_error, void * data){
|
---|
| 491 | usb_hub_info_t * hub_info = data;
|
---|
| 492 | if(!hub_info) return;
|
---|
| 493 | free(hub_info->ports);
|
---|
| 494 | free(hub_info);
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 |
|
---|
| 498 |
|
---|
| 499 |
|
---|
[e080332] | 500 | /**
|
---|
| 501 | * @}
|
---|
[71ed4849] | 502 | */
|
---|