| 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 |
|
|---|
| 40 | #include <usb_iface.h>
|
|---|
| 41 | #include <usb/ddfiface.h>
|
|---|
| 42 | #include <usb/descriptor.h>
|
|---|
| 43 | #include <usb/recognise.h>
|
|---|
| 44 | #include <usb/request.h>
|
|---|
| 45 | #include <usb/classes/hub.h>
|
|---|
| 46 | #include <stdio.h>
|
|---|
| 47 |
|
|---|
| 48 | #include "usbhub.h"
|
|---|
| 49 | #include "usbhub_private.h"
|
|---|
| 50 | #include "port_status.h"
|
|---|
| 51 | #include "usb/usb.h"
|
|---|
| 52 | #include "usb/pipes.h"
|
|---|
| 53 | #include "usb/classes/classes.h"
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 | //static void usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port,
|
|---|
| 57 | // usb_speed_t speed);
|
|---|
| 58 |
|
|---|
| 59 | static int usb_hub_trigger_connecting_non_removable_devices(
|
|---|
| 60 | usb_hub_info_t * hub, usb_hub_descriptor_t * descriptor);
|
|---|
| 61 |
|
|---|
| 62 | #if 0
|
|---|
| 63 | /**
|
|---|
| 64 | * control loop running in hub`s fibril
|
|---|
| 65 | *
|
|---|
| 66 | * Hub`s fibril periodically asks for changes on hub and if needded calls
|
|---|
| 67 | * change handling routine.
|
|---|
| 68 | * @warning currently hub driver asks for changes once a second
|
|---|
| 69 | * @param hub_info_param hub representation pointer
|
|---|
| 70 | * @return zero
|
|---|
| 71 | */
|
|---|
| 72 | int usb_hub_control_loop(void * hub_info_param){
|
|---|
| 73 | usb_hub_info_t * hub_info = (usb_hub_info_t*)hub_info_param;
|
|---|
| 74 | int errorCode = EOK;
|
|---|
| 75 |
|
|---|
| 76 | while(errorCode == EOK){
|
|---|
| 77 | async_usleep(1000 * 1000 * 10 );/// \TODO proper number once
|
|---|
| 78 | errorCode = usb_hub_check_hub_changes(hub_info);
|
|---|
| 79 | }
|
|---|
| 80 | usb_log_error("something in ctrl loop went wrong, errno %d\n",errorCode);
|
|---|
| 81 |
|
|---|
| 82 | return 0;
|
|---|
| 83 | }
|
|---|
| 84 | #endif
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 | //*********************************************
|
|---|
| 88 | //
|
|---|
| 89 | // hub driver code, initialization
|
|---|
| 90 | //
|
|---|
| 91 | //*********************************************
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * create usb_hub_info_t structure
|
|---|
| 95 | *
|
|---|
| 96 | * Does only basic copying of known information into new structure.
|
|---|
| 97 | * @param usb_dev usb device structure
|
|---|
| 98 | * @return basic usb_hub_info_t structure
|
|---|
| 99 | */
|
|---|
| 100 | static usb_hub_info_t * usb_hub_info_create(usb_device_t * usb_dev) {
|
|---|
| 101 | usb_hub_info_t * result = usb_new(usb_hub_info_t);
|
|---|
| 102 | if(!result) return NULL;
|
|---|
| 103 | result->usb_device = usb_dev;
|
|---|
| 104 | result->status_change_pipe = usb_dev->pipes[0].pipe;
|
|---|
| 105 | result->control_pipe = &usb_dev->ctrl_pipe;
|
|---|
| 106 | result->is_default_address_used = false;
|
|---|
| 107 | return result;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /**
|
|---|
| 111 | * Load hub-specific information into hub_info structure and process if needed
|
|---|
| 112 | *
|
|---|
| 113 | * Particularly read port count and initialize structure holding port
|
|---|
| 114 | * information. If there are non-removable devices, start initializing them.
|
|---|
| 115 | * This function is hub-specific and should be run only after the hub is
|
|---|
| 116 | * configured using usb_hub_set_configuration function.
|
|---|
| 117 | * @param hub_info hub representation
|
|---|
| 118 | * @return error code
|
|---|
| 119 | */
|
|---|
| 120 | static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info){
|
|---|
| 121 | // get hub descriptor
|
|---|
| 122 | usb_log_debug("creating serialized descriptor\n");
|
|---|
| 123 | void * serialized_descriptor = malloc(USB_HUB_MAX_DESCRIPTOR_SIZE);
|
|---|
| 124 | usb_hub_descriptor_t * descriptor;
|
|---|
| 125 |
|
|---|
| 126 | /* this was one fix of some bug, should not be needed anymore
|
|---|
| 127 | * these lines allow to reset hub once more, it can be used as
|
|---|
| 128 | * brute-force initialization for non-removable devices
|
|---|
| 129 | int opResult = usb_request_set_configuration(&result->endpoints.control, 1);
|
|---|
| 130 | if(opResult!=EOK){
|
|---|
| 131 | usb_log_error("could not set default configuration, errno %d",opResult);
|
|---|
| 132 | return opResult;
|
|---|
| 133 | }
|
|---|
| 134 | */
|
|---|
| 135 | size_t received_size;
|
|---|
| 136 | int opResult = usb_request_get_descriptor(&hub_info->usb_device->ctrl_pipe,
|
|---|
| 137 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
|
|---|
| 138 | USB_DESCTYPE_HUB,
|
|---|
| 139 | 0, 0, serialized_descriptor,
|
|---|
| 140 | USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
|
|---|
| 141 |
|
|---|
| 142 | if (opResult != EOK) {
|
|---|
| 143 | usb_log_error("failed when receiving hub descriptor, badcode = %d\n",
|
|---|
| 144 | opResult);
|
|---|
| 145 | free(serialized_descriptor);
|
|---|
| 146 | return opResult;
|
|---|
| 147 | }
|
|---|
| 148 | usb_log_debug2("deserializing descriptor\n");
|
|---|
| 149 | descriptor = usb_deserialize_hub_desriptor(serialized_descriptor);
|
|---|
| 150 | if(descriptor==NULL){
|
|---|
| 151 | usb_log_warning("could not deserialize descriptor \n");
|
|---|
| 152 | return opResult;
|
|---|
| 153 | }
|
|---|
| 154 | usb_log_debug("setting port count to %d\n",descriptor->ports_count);
|
|---|
| 155 | hub_info->port_count = descriptor->ports_count;
|
|---|
| 156 | hub_info->ports = malloc(sizeof(usb_hub_port_t) * (hub_info->port_count+1));
|
|---|
| 157 | size_t port;
|
|---|
| 158 | for (port = 0; port < hub_info->port_count + 1; port++) {
|
|---|
| 159 | usb_hub_port_init(&hub_info->ports[port]);
|
|---|
| 160 | }
|
|---|
| 161 | //handle non-removable devices
|
|---|
| 162 | usb_hub_trigger_connecting_non_removable_devices(hub_info, descriptor);
|
|---|
| 163 | usb_log_debug2("freeing data\n");
|
|---|
| 164 | free(serialized_descriptor);
|
|---|
| 165 | free(descriptor->devices_removable);
|
|---|
| 166 | free(descriptor);
|
|---|
| 167 | return EOK;
|
|---|
| 168 | }
|
|---|
| 169 | /**
|
|---|
| 170 | * Set configuration of hub
|
|---|
| 171 | *
|
|---|
| 172 | * Check whether there is at least one configuration and sets the first one.
|
|---|
| 173 | * This function should be run prior to running any hub-specific action.
|
|---|
| 174 | * @param hub_info hub representation
|
|---|
| 175 | * @return error code
|
|---|
| 176 | */
|
|---|
| 177 | static int usb_hub_set_configuration(usb_hub_info_t * hub_info){
|
|---|
| 178 | //device descriptor
|
|---|
| 179 | usb_standard_device_descriptor_t *std_descriptor
|
|---|
| 180 | = &hub_info->usb_device->descriptors.device;
|
|---|
| 181 | usb_log_debug("hub has %d configurations\n",
|
|---|
| 182 | std_descriptor->configuration_count);
|
|---|
| 183 | if(std_descriptor->configuration_count<1){
|
|---|
| 184 | usb_log_error("there are no configurations available\n");
|
|---|
| 185 | return EINVAL;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | usb_standard_configuration_descriptor_t *config_descriptor
|
|---|
| 189 | = (usb_standard_configuration_descriptor_t *)
|
|---|
| 190 | hub_info->usb_device->descriptors.configuration;
|
|---|
| 191 |
|
|---|
| 192 | /* Set configuration. */
|
|---|
| 193 | int opResult = usb_request_set_configuration(
|
|---|
| 194 | &hub_info->usb_device->ctrl_pipe,
|
|---|
| 195 | config_descriptor->configuration_number);
|
|---|
| 196 |
|
|---|
| 197 | if (opResult != EOK) {
|
|---|
| 198 | usb_log_error("Failed to set hub configuration: %s.\n",
|
|---|
| 199 | str_error(opResult));
|
|---|
| 200 | return opResult;
|
|---|
| 201 | }
|
|---|
| 202 | usb_log_debug("\tused configuration %d\n",
|
|---|
| 203 | config_descriptor->configuration_number);
|
|---|
| 204 |
|
|---|
| 205 | return EOK;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | /**
|
|---|
| 209 | * Initialize hub device driver fibril
|
|---|
| 210 | *
|
|---|
| 211 | * Creates hub representation and fibril that periodically checks hub`s status.
|
|---|
| 212 | * Hub representation is passed to the fibril.
|
|---|
| 213 | * @param usb_dev generic usb device information
|
|---|
| 214 | * @return error code
|
|---|
| 215 | */
|
|---|
| 216 | int usb_hub_add_device(usb_device_t * usb_dev){
|
|---|
| 217 | if(!usb_dev) return EINVAL;
|
|---|
| 218 | usb_hub_info_t * hub_info = usb_hub_info_create(usb_dev);
|
|---|
| 219 | //create hc connection
|
|---|
| 220 | usb_log_debug("Initializing USB wire abstraction.\n");
|
|---|
| 221 | int opResult = usb_hc_connection_initialize_from_device(
|
|---|
| 222 | &hub_info->connection,
|
|---|
| 223 | hub_info->usb_device->ddf_dev);
|
|---|
| 224 | if(opResult != EOK){
|
|---|
| 225 | usb_log_error("could not initialize connection to device, errno %d\n",
|
|---|
| 226 | opResult);
|
|---|
| 227 | free(hub_info);
|
|---|
| 228 | return opResult;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | usb_pipe_start_session(hub_info->control_pipe);
|
|---|
| 232 | //set hub configuration
|
|---|
| 233 | opResult = usb_hub_set_configuration(hub_info);
|
|---|
| 234 | if(opResult!=EOK){
|
|---|
| 235 | usb_log_error("could not set hub configuration, errno %d\n",opResult);
|
|---|
| 236 | free(hub_info);
|
|---|
| 237 | return opResult;
|
|---|
| 238 | }
|
|---|
| 239 | //get port count and create attached_devs
|
|---|
| 240 | opResult = usb_hub_process_hub_specific_info(hub_info);
|
|---|
| 241 | if(opResult!=EOK){
|
|---|
| 242 | usb_log_error("could not set hub configuration, errno %d\n",opResult);
|
|---|
| 243 | free(hub_info);
|
|---|
| 244 | return opResult;
|
|---|
| 245 | }
|
|---|
| 246 | usb_pipe_end_session(hub_info->control_pipe);
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 | /// \TODO what is this?
|
|---|
| 250 | usb_log_debug("Creating `hub' function.\n");
|
|---|
| 251 | ddf_fun_t *hub_fun = ddf_fun_create(hub_info->usb_device->ddf_dev,
|
|---|
| 252 | fun_exposed, "hub");
|
|---|
| 253 | assert(hub_fun != NULL);
|
|---|
| 254 | hub_fun->ops = NULL;
|
|---|
| 255 |
|
|---|
| 256 | int rc = ddf_fun_bind(hub_fun);
|
|---|
| 257 | assert(rc == EOK);
|
|---|
| 258 | rc = ddf_fun_add_to_class(hub_fun, "hub");
|
|---|
| 259 | assert(rc == EOK);
|
|---|
| 260 |
|
|---|
| 261 | /*
|
|---|
| 262 | * The processing will require opened control pipe and connection
|
|---|
| 263 | * to the host controller.
|
|---|
| 264 | * It is waste of resources but let's hope there will be less
|
|---|
| 265 | * hubs than the phone limit.
|
|---|
| 266 | * FIXME: with some proper locking over pipes and session
|
|---|
| 267 | * auto destruction, this could work better.
|
|---|
| 268 | */
|
|---|
| 269 | rc = usb_pipe_start_session(&usb_dev->ctrl_pipe);
|
|---|
| 270 | if (rc != EOK) {
|
|---|
| 271 | usb_log_error("Failed to start session on control pipe: %s.\n",
|
|---|
| 272 | str_error(rc));
|
|---|
| 273 | goto leave;
|
|---|
| 274 | }
|
|---|
| 275 | rc = usb_hc_connection_open(&hub_info->connection);
|
|---|
| 276 | if (rc != EOK) {
|
|---|
| 277 | usb_pipe_end_session(&usb_dev->ctrl_pipe);
|
|---|
| 278 | usb_log_error("Failed to open connection to HC: %s.\n",
|
|---|
| 279 | str_error(rc));
|
|---|
| 280 | goto leave;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | rc = usb_device_auto_poll(hub_info->usb_device, 0,
|
|---|
| 284 | hub_port_changes_callback, ((hub_info->port_count+1) / 8) + 1,
|
|---|
| 285 | NULL, hub_info);
|
|---|
| 286 | if (rc != EOK) {
|
|---|
| 287 | usb_log_error("Failed to create polling fibril: %s.\n",
|
|---|
| 288 | str_error(rc));
|
|---|
| 289 | free(hub_info);
|
|---|
| 290 | return rc;
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | usb_log_info("Controlling hub `%s' (%d ports).\n",
|
|---|
| 294 | hub_info->usb_device->ddf_dev->name, hub_info->port_count);
|
|---|
| 295 | return EOK;
|
|---|
| 296 |
|
|---|
| 297 | leave:
|
|---|
| 298 | free(hub_info);
|
|---|
| 299 |
|
|---|
| 300 | return rc;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 | //*********************************************
|
|---|
| 305 | //
|
|---|
| 306 | // hub driver code, main loop and port handling
|
|---|
| 307 | //
|
|---|
| 308 | //*********************************************
|
|---|
| 309 |
|
|---|
| 310 | /**
|
|---|
| 311 | * triggers actions to connect non0removable devices
|
|---|
| 312 | *
|
|---|
| 313 | * This will trigger operations leading to activated non-removable device.
|
|---|
| 314 | * Control pipe of the hub must be open fo communication.
|
|---|
| 315 | * @param hub hub representation
|
|---|
| 316 | * @param descriptor usb hub descriptor
|
|---|
| 317 | * @return error code
|
|---|
| 318 | */
|
|---|
| 319 | static int usb_hub_trigger_connecting_non_removable_devices(usb_hub_info_t * hub,
|
|---|
| 320 | usb_hub_descriptor_t * descriptor)
|
|---|
| 321 | {
|
|---|
| 322 | usb_log_info("attaching non-removable devices(if any)\n");
|
|---|
| 323 | usb_device_request_setup_packet_t request;
|
|---|
| 324 | int opResult;
|
|---|
| 325 | size_t rcvd_size;
|
|---|
| 326 | usb_port_status_t status;
|
|---|
| 327 | uint8_t * non_removable_dev_bitmap = descriptor->devices_removable;
|
|---|
| 328 | int port;
|
|---|
| 329 | for(port=1;port<=descriptor->ports_count;++port){
|
|---|
| 330 | bool is_non_removable =
|
|---|
| 331 | ((non_removable_dev_bitmap[port/8]) >> (port%8)) %2;
|
|---|
| 332 | if(is_non_removable){
|
|---|
| 333 | usb_log_debug("non-removable device on port %d\n",port);
|
|---|
| 334 | usb_hub_set_port_status_request(&request, port);
|
|---|
| 335 | opResult = usb_pipe_control_read(
|
|---|
| 336 | hub->control_pipe,
|
|---|
| 337 | &request, sizeof(usb_device_request_setup_packet_t),
|
|---|
| 338 | &status, 4, &rcvd_size
|
|---|
| 339 | );
|
|---|
| 340 | if (opResult != EOK) {
|
|---|
| 341 | usb_log_error("could not get port status of port %d errno:%d\n",
|
|---|
| 342 | port, opResult);
|
|---|
| 343 | return opResult;
|
|---|
| 344 | }
|
|---|
| 345 | //set the status change bit, so it will be noticed in driver loop
|
|---|
| 346 | if(usb_port_dev_connected(&status)){
|
|---|
| 347 | usb_hub_set_disable_port_feature_request(&request, port,
|
|---|
| 348 | USB_HUB_FEATURE_PORT_CONNECTION);
|
|---|
| 349 | opResult = usb_pipe_control_read(
|
|---|
| 350 | hub->control_pipe,
|
|---|
| 351 | &request, sizeof(usb_device_request_setup_packet_t),
|
|---|
| 352 | &status, 4, &rcvd_size
|
|---|
| 353 | );
|
|---|
| 354 | if (opResult != EOK) {
|
|---|
| 355 | usb_log_warning(
|
|---|
| 356 | "could not clear port connection on port %d errno:%d\n",
|
|---|
| 357 | port, opResult);
|
|---|
| 358 | }
|
|---|
| 359 | usb_log_debug("cleared port connection\n");
|
|---|
| 360 | usb_hub_set_enable_port_feature_request(&request, port,
|
|---|
| 361 | USB_HUB_FEATURE_PORT_ENABLE);
|
|---|
| 362 | opResult = usb_pipe_control_read(
|
|---|
| 363 | hub->control_pipe,
|
|---|
| 364 | &request, sizeof(usb_device_request_setup_packet_t),
|
|---|
| 365 | &status, 4, &rcvd_size
|
|---|
| 366 | );
|
|---|
| 367 | if (opResult != EOK) {
|
|---|
| 368 | usb_log_warning(
|
|---|
| 369 | "could not set port enabled on port %d errno:%d\n",
|
|---|
| 370 | port, opResult);
|
|---|
| 371 | }
|
|---|
| 372 | usb_log_debug("port set to enabled - should lead to connection change\n");
|
|---|
| 373 | }
|
|---|
| 374 | }
|
|---|
| 375 | }
|
|---|
| 376 | /// \TODO this is just a debug code
|
|---|
| 377 | for(port=1;port<=descriptor->ports_count;++port){
|
|---|
| 378 | bool is_non_removable =
|
|---|
| 379 | ((non_removable_dev_bitmap[port/8]) >> (port%8)) %2;
|
|---|
| 380 | if(is_non_removable){
|
|---|
| 381 | usb_log_debug("port %d is non-removable\n",port);
|
|---|
| 382 | usb_port_status_t status;
|
|---|
| 383 | size_t rcvd_size;
|
|---|
| 384 | usb_device_request_setup_packet_t request;
|
|---|
| 385 | //int opResult;
|
|---|
| 386 | usb_hub_set_port_status_request(&request, port);
|
|---|
| 387 | //endpoint 0
|
|---|
| 388 | opResult = usb_pipe_control_read(
|
|---|
| 389 | hub->control_pipe,
|
|---|
| 390 | &request, sizeof(usb_device_request_setup_packet_t),
|
|---|
| 391 | &status, 4, &rcvd_size
|
|---|
| 392 | );
|
|---|
| 393 | if (opResult != EOK) {
|
|---|
| 394 | usb_log_error("could not get port status %d\n",opResult);
|
|---|
| 395 | }
|
|---|
| 396 | if (rcvd_size != sizeof (usb_port_status_t)) {
|
|---|
| 397 | usb_log_error("received status has incorrect size\n");
|
|---|
| 398 | }
|
|---|
| 399 | //something connected/disconnected
|
|---|
| 400 | if (usb_port_connect_change(&status)) {
|
|---|
| 401 | usb_log_debug("some connection changed\n");
|
|---|
| 402 | }
|
|---|
| 403 | usb_log_debug("status: %s\n",usb_debug_str_buffer(
|
|---|
| 404 | (uint8_t *)&status,4,4));
|
|---|
| 405 | }
|
|---|
| 406 | }
|
|---|
| 407 | return EOK;
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 |
|
|---|
| 411 | /**
|
|---|
| 412 | * release default address used by given hub
|
|---|
| 413 | *
|
|---|
| 414 | * Also unsets hub->is_default_address_used. Convenience wrapper function.
|
|---|
| 415 | * @note hub->connection MUST be open for communication
|
|---|
| 416 | * @param hub hub representation
|
|---|
| 417 | * @return error code
|
|---|
| 418 | */
|
|---|
| 419 | static int usb_hub_release_default_address(usb_hub_info_t * hub){
|
|---|
| 420 | int opResult = usb_hc_release_default_address(&hub->connection);
|
|---|
| 421 | if(opResult!=EOK){
|
|---|
| 422 | usb_log_error("could not release default address, errno %d\n",opResult);
|
|---|
| 423 | return opResult;
|
|---|
| 424 | }
|
|---|
| 425 | hub->is_default_address_used = false;
|
|---|
| 426 | return EOK;
|
|---|
| 427 | }
|
|---|
| 428 |
|
|---|
| 429 | #if 0
|
|---|
| 430 | /**
|
|---|
| 431 | * Reset the port with new device and reserve the default address.
|
|---|
| 432 | * @param hub hub representation
|
|---|
| 433 | * @param port port number, starting from 1
|
|---|
| 434 | * @param speed transfer speed of attached device, one of low, full or high
|
|---|
| 435 | */
|
|---|
| 436 | static void usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port,
|
|---|
| 437 | usb_speed_t speed) {
|
|---|
| 438 | //if this hub already uses default address, it cannot request it once more
|
|---|
| 439 | if(hub->is_default_address_used) return;
|
|---|
| 440 | usb_log_debug("some connection changed\n");
|
|---|
| 441 | assert(hub->control_pipe->hc_phone);
|
|---|
| 442 | int opResult = usb_hub_clear_port_feature(hub->control_pipe,
|
|---|
| 443 | port, USB_HUB_FEATURE_C_PORT_CONNECTION);
|
|---|
| 444 | if(opResult != EOK){
|
|---|
| 445 | usb_log_warning("could not clear port-change-connection flag\n");
|
|---|
| 446 | }
|
|---|
| 447 | usb_device_request_setup_packet_t request;
|
|---|
| 448 |
|
|---|
| 449 | //get default address
|
|---|
| 450 | opResult = usb_hc_reserve_default_address(&hub->connection, speed);
|
|---|
| 451 |
|
|---|
| 452 | if (opResult != EOK) {
|
|---|
| 453 | usb_log_warning("cannot assign default address, it is probably used %d\n",
|
|---|
| 454 | opResult);
|
|---|
| 455 | return;
|
|---|
| 456 | }
|
|---|
| 457 | hub->is_default_address_used = true;
|
|---|
| 458 | //reset port
|
|---|
| 459 | usb_hub_set_reset_port_request(&request, port);
|
|---|
| 460 | opResult = usb_pipe_control_write(
|
|---|
| 461 | hub->control_pipe,
|
|---|
| 462 | &request,sizeof(usb_device_request_setup_packet_t),
|
|---|
| 463 | NULL, 0
|
|---|
| 464 | );
|
|---|
| 465 | if (opResult != EOK) {
|
|---|
| 466 | usb_log_error("something went wrong when reseting a port %d\n",opResult);
|
|---|
| 467 | usb_hub_release_default_address(hub);
|
|---|
| 468 | }
|
|---|
| 469 | return;
|
|---|
| 470 | }
|
|---|
| 471 | #endif
|
|---|
| 472 |
|
|---|
| 473 | #if 0
|
|---|
| 474 | /**
|
|---|
| 475 | * Finalize adding new device after port reset
|
|---|
| 476 | *
|
|---|
| 477 | * Set device`s address and start it`s driver.
|
|---|
| 478 | * @param hub hub representation
|
|---|
| 479 | * @param port port number, starting from 1
|
|---|
| 480 | * @param speed transfer speed of attached device, one of low, full or high
|
|---|
| 481 | */
|
|---|
| 482 | static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
|
|---|
| 483 | uint16_t port, usb_speed_t speed) {
|
|---|
| 484 |
|
|---|
| 485 | int opResult;
|
|---|
| 486 | usb_log_debug("finalizing add device\n");
|
|---|
| 487 | opResult = usb_hub_clear_port_feature(hub->control_pipe,
|
|---|
| 488 | port, USB_HUB_FEATURE_C_PORT_RESET);
|
|---|
| 489 |
|
|---|
| 490 | if (opResult != EOK) {
|
|---|
| 491 | usb_log_error("failed to clear port reset feature\n");
|
|---|
| 492 | usb_hub_release_default_address(hub);
|
|---|
| 493 | return;
|
|---|
| 494 | }
|
|---|
| 495 | //create connection to device
|
|---|
| 496 | usb_pipe_t new_device_pipe;
|
|---|
| 497 | usb_device_connection_t new_device_connection;
|
|---|
| 498 | usb_device_connection_initialize_on_default_address(
|
|---|
| 499 | &new_device_connection,
|
|---|
| 500 | &hub->connection
|
|---|
| 501 | );
|
|---|
| 502 | usb_pipe_initialize_default_control(
|
|---|
| 503 | &new_device_pipe,
|
|---|
| 504 | &new_device_connection);
|
|---|
| 505 | usb_pipe_probe_default_control(&new_device_pipe);
|
|---|
| 506 |
|
|---|
| 507 | /* Request address from host controller. */
|
|---|
| 508 | usb_address_t new_device_address = usb_hc_request_address(
|
|---|
| 509 | &hub->connection,
|
|---|
| 510 | speed
|
|---|
| 511 | );
|
|---|
| 512 | if (new_device_address < 0) {
|
|---|
| 513 | usb_log_error("failed to get free USB address\n");
|
|---|
| 514 | opResult = new_device_address;
|
|---|
| 515 | usb_hub_release_default_address(hub);
|
|---|
| 516 | return;
|
|---|
| 517 | }
|
|---|
| 518 | usb_log_debug("setting new address %d\n",new_device_address);
|
|---|
| 519 | //opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
|
|---|
| 520 | // new_device_address);
|
|---|
| 521 | usb_pipe_start_session(&new_device_pipe);
|
|---|
| 522 | opResult = usb_request_set_address(&new_device_pipe,new_device_address);
|
|---|
| 523 | usb_pipe_end_session(&new_device_pipe);
|
|---|
| 524 | if (opResult != EOK) {
|
|---|
| 525 | usb_log_error("could not set address for new device %d\n",opResult);
|
|---|
| 526 | usb_hub_release_default_address(hub);
|
|---|
| 527 | return;
|
|---|
| 528 | }
|
|---|
| 529 |
|
|---|
| 530 | //opResult = usb_hub_release_default_address(hc);
|
|---|
| 531 | opResult = usb_hub_release_default_address(hub);
|
|---|
| 532 | if(opResult!=EOK){
|
|---|
| 533 | return;
|
|---|
| 534 | }
|
|---|
| 535 |
|
|---|
| 536 | devman_handle_t child_handle;
|
|---|
| 537 | //??
|
|---|
| 538 | opResult = usb_device_register_child_in_devman(new_device_address,
|
|---|
| 539 | hub->connection.hc_handle, hub->usb_device->ddf_dev, &child_handle,
|
|---|
| 540 | NULL, NULL, NULL);
|
|---|
| 541 |
|
|---|
| 542 | if (opResult != EOK) {
|
|---|
| 543 | usb_log_error("could not start driver for new device %d\n",opResult);
|
|---|
| 544 | return;
|
|---|
| 545 | }
|
|---|
| 546 | hub->attached_devs[port].handle = child_handle;
|
|---|
| 547 | hub->attached_devs[port].address = new_device_address;
|
|---|
| 548 |
|
|---|
| 549 | //opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
|
|---|
| 550 | opResult = usb_hc_register_device(
|
|---|
| 551 | &hub->connection,
|
|---|
| 552 | &hub->attached_devs[port]);
|
|---|
| 553 | if (opResult != EOK) {
|
|---|
| 554 | usb_log_error("could not assign address of device in hcd %d\n",opResult);
|
|---|
| 555 | return;
|
|---|
| 556 | }
|
|---|
| 557 | usb_log_info("Detected new device on `%s' (port %d), " \
|
|---|
| 558 | "address %d (handle %llu).\n",
|
|---|
| 559 | hub->usb_device->ddf_dev->name, (int) port,
|
|---|
| 560 | new_device_address, child_handle);
|
|---|
| 561 | }
|
|---|
| 562 | #endif
|
|---|
| 563 |
|
|---|
| 564 | /**
|
|---|
| 565 | * routine called when a device on port has been removed
|
|---|
| 566 | *
|
|---|
| 567 | * If the device on port had default address, it releases default address.
|
|---|
| 568 | * Otherwise does not do anything, because DDF does not allow to remove device
|
|---|
| 569 | * from it`s device tree.
|
|---|
| 570 | * @param hub hub representation
|
|---|
| 571 | * @param port port number, starting from 1
|
|---|
| 572 | */
|
|---|
| 573 | void usb_hub_removed_device(
|
|---|
| 574 | usb_hub_info_t * hub,uint16_t port) {
|
|---|
| 575 |
|
|---|
| 576 | int opResult = usb_hub_clear_port_feature(hub->control_pipe,
|
|---|
| 577 | port, USB_HUB_FEATURE_C_PORT_CONNECTION);
|
|---|
| 578 | if(opResult != EOK){
|
|---|
| 579 | usb_log_warning("could not clear port-change-connection flag\n");
|
|---|
| 580 | }
|
|---|
| 581 | /** \TODO remove device from device manager - not yet implemented in
|
|---|
| 582 | * devide manager
|
|---|
| 583 | */
|
|---|
| 584 |
|
|---|
| 585 | //close address
|
|---|
| 586 | if(hub->ports[port].attached_device.address >= 0){
|
|---|
| 587 | /*uncomment this code to use it when DDF allows device removal
|
|---|
| 588 | opResult = usb_hc_unregister_device(
|
|---|
| 589 | &hub->connection, hub->attached_devs[port].address);
|
|---|
| 590 | if(opResult != EOK) {
|
|---|
| 591 | dprintf(USB_LOG_LEVEL_WARNING, "could not release address of " \
|
|---|
| 592 | "removed device: %d", opResult);
|
|---|
| 593 | }
|
|---|
| 594 | hub->attached_devs[port].address = 0;
|
|---|
| 595 | hub->attached_devs[port].handle = 0;
|
|---|
| 596 | */
|
|---|
| 597 | }else{
|
|---|
| 598 | usb_log_warning("this is strange, disconnected device had no address\n");
|
|---|
| 599 | //device was disconnected before it`s port was reset - return default address
|
|---|
| 600 | usb_hub_release_default_address(hub);
|
|---|
| 601 | }
|
|---|
| 602 | }
|
|---|
| 603 |
|
|---|
| 604 |
|
|---|
| 605 | /**
|
|---|
| 606 | * Process over current condition on port.
|
|---|
| 607 | *
|
|---|
| 608 | * Turn off the power on the port.
|
|---|
| 609 | *
|
|---|
| 610 | * @param hub hub representation
|
|---|
| 611 | * @param port port number, starting from 1
|
|---|
| 612 | */
|
|---|
| 613 | void usb_hub_over_current( usb_hub_info_t * hub,
|
|---|
| 614 | uint16_t port){
|
|---|
| 615 | int opResult;
|
|---|
| 616 | opResult = usb_hub_clear_port_feature(hub->control_pipe,
|
|---|
| 617 | port, USB_HUB_FEATURE_PORT_POWER);
|
|---|
| 618 | if(opResult!=EOK){
|
|---|
| 619 | usb_log_error("cannot power off port %d; %d\n",
|
|---|
| 620 | port, opResult);
|
|---|
| 621 | }
|
|---|
| 622 | }
|
|---|
| 623 |
|
|---|
| 624 | #if 0
|
|---|
| 625 | /**
|
|---|
| 626 | * Process interrupts on given hub port
|
|---|
| 627 | *
|
|---|
| 628 | * Accepts connection, over current and port reset change.
|
|---|
| 629 | * @param hub hub representation
|
|---|
| 630 | * @param port port number, starting from 1
|
|---|
| 631 | */
|
|---|
| 632 | static void usb_hub_process_interrupt(usb_hub_info_t * hub,
|
|---|
| 633 | uint16_t port) {
|
|---|
| 634 | usb_log_debug("interrupt at port %d\n", port);
|
|---|
| 635 | //determine type of change
|
|---|
| 636 | usb_pipe_t *pipe = hub->control_pipe;
|
|---|
| 637 |
|
|---|
| 638 | int opResult;
|
|---|
| 639 |
|
|---|
| 640 | usb_port_status_t status;
|
|---|
| 641 | size_t rcvd_size;
|
|---|
| 642 | usb_device_request_setup_packet_t request;
|
|---|
| 643 | //int opResult;
|
|---|
| 644 | usb_hub_set_port_status_request(&request, port);
|
|---|
| 645 | //endpoint 0
|
|---|
| 646 |
|
|---|
| 647 | opResult = usb_pipe_control_read(
|
|---|
| 648 | pipe,
|
|---|
| 649 | &request, sizeof(usb_device_request_setup_packet_t),
|
|---|
| 650 | &status, 4, &rcvd_size
|
|---|
| 651 | );
|
|---|
| 652 | if (opResult != EOK) {
|
|---|
| 653 | usb_log_error("could not get port status\n");
|
|---|
| 654 | return;
|
|---|
| 655 | }
|
|---|
| 656 | if (rcvd_size != sizeof (usb_port_status_t)) {
|
|---|
| 657 | usb_log_error("received status has incorrect size\n");
|
|---|
| 658 | return;
|
|---|
| 659 | }
|
|---|
| 660 | //something connected/disconnected
|
|---|
| 661 | if (usb_port_connect_change(&status)) {
|
|---|
| 662 | usb_log_debug("connection change on port\n");
|
|---|
| 663 | if (usb_port_dev_connected(&status)) {
|
|---|
| 664 | usb_log_debug("some connection changed\n");
|
|---|
| 665 | usb_hub_init_add_device(hub, port, usb_port_speed(&status));
|
|---|
| 666 | } else {
|
|---|
| 667 | usb_hub_removed_device(hub, port);
|
|---|
| 668 | }
|
|---|
| 669 | }
|
|---|
| 670 | //over current
|
|---|
| 671 | if (usb_port_overcurrent_change(&status)) {
|
|---|
| 672 | //check if it was not auto-resolved
|
|---|
| 673 | usb_log_debug("overcurrent change on port\n");
|
|---|
| 674 | if(usb_port_over_current(&status)){
|
|---|
| 675 | usb_hub_over_current(hub,port);
|
|---|
| 676 | }else{
|
|---|
| 677 | usb_log_debug("over current condition was auto-resolved on port %d\n",
|
|---|
| 678 | port);
|
|---|
| 679 | }
|
|---|
| 680 | }
|
|---|
| 681 | //port reset
|
|---|
| 682 | if (usb_port_reset_completed(&status)) {
|
|---|
| 683 | usb_log_debug("port reset complete\n");
|
|---|
| 684 | if (usb_port_enabled(&status)) {
|
|---|
| 685 | usb_hub_finalize_add_device(hub, port, usb_port_speed(&status));
|
|---|
| 686 | } else {
|
|---|
| 687 | usb_log_warning("port reset, but port still not enabled\n");
|
|---|
| 688 | }
|
|---|
| 689 | }
|
|---|
| 690 | usb_log_debug("status %x\n ",status);
|
|---|
| 691 |
|
|---|
| 692 | usb_port_set_connect_change(&status, false);
|
|---|
| 693 | usb_port_set_reset(&status, false);
|
|---|
| 694 | usb_port_set_reset_completed(&status, false);
|
|---|
| 695 | usb_port_set_dev_connected(&status, false);
|
|---|
| 696 | if (status>>16) {
|
|---|
| 697 | usb_log_info("there was some unsupported change on port %d: %X\n",
|
|---|
| 698 | port,status);
|
|---|
| 699 |
|
|---|
| 700 | }
|
|---|
| 701 | }
|
|---|
| 702 |
|
|---|
| 703 | /**
|
|---|
| 704 | * check changes on hub
|
|---|
| 705 | *
|
|---|
| 706 | * Handles changes on each port with a status change.
|
|---|
| 707 | * @param hub_info hub representation
|
|---|
| 708 | * @return error code
|
|---|
| 709 | */
|
|---|
| 710 | int usb_hub_check_hub_changes(usb_hub_info_t * hub_info){
|
|---|
| 711 | int opResult;
|
|---|
| 712 | opResult = usb_pipe_start_session(
|
|---|
| 713 | hub_info->status_change_pipe);
|
|---|
| 714 | if(opResult != EOK){
|
|---|
| 715 | usb_log_error("could not initialize communication for hub; %d\n",
|
|---|
| 716 | opResult);
|
|---|
| 717 | return opResult;
|
|---|
| 718 | }
|
|---|
| 719 |
|
|---|
| 720 | size_t port_count = hub_info->port_count;
|
|---|
| 721 |
|
|---|
| 722 | /// FIXME: count properly
|
|---|
| 723 | size_t byte_length = ((port_count+1) / 8) + 1;
|
|---|
| 724 | void *change_bitmap = malloc(byte_length);
|
|---|
| 725 | size_t actual_size;
|
|---|
| 726 |
|
|---|
| 727 | /*
|
|---|
| 728 | * Send the request.
|
|---|
| 729 | */
|
|---|
| 730 | opResult = usb_pipe_read(
|
|---|
| 731 | hub_info->status_change_pipe,
|
|---|
| 732 | change_bitmap, byte_length, &actual_size
|
|---|
| 733 | );
|
|---|
| 734 |
|
|---|
| 735 | if (opResult != EOK) {
|
|---|
| 736 | free(change_bitmap);
|
|---|
| 737 | usb_log_warning("something went wrong while getting status of hub\n");
|
|---|
| 738 | usb_pipe_end_session(hub_info->status_change_pipe);
|
|---|
| 739 | return opResult;
|
|---|
| 740 | }
|
|---|
| 741 | unsigned int port;
|
|---|
| 742 | opResult = usb_pipe_start_session(hub_info->control_pipe);
|
|---|
| 743 | if(opResult!=EOK){
|
|---|
| 744 | usb_log_error("could not start control pipe session %d\n", opResult);
|
|---|
| 745 | usb_pipe_end_session(hub_info->status_change_pipe);
|
|---|
| 746 | return opResult;
|
|---|
| 747 | }
|
|---|
| 748 | opResult = usb_hc_connection_open(&hub_info->connection);
|
|---|
| 749 | if(opResult!=EOK){
|
|---|
| 750 | usb_log_error("could not start host controller session %d\n",
|
|---|
| 751 | opResult);
|
|---|
| 752 | usb_pipe_end_session(hub_info->control_pipe);
|
|---|
| 753 | usb_pipe_end_session(hub_info->status_change_pipe);
|
|---|
| 754 | return opResult;
|
|---|
| 755 | }
|
|---|
| 756 |
|
|---|
| 757 | ///todo, opresult check, pre obe konekce
|
|---|
| 758 | for (port = 1; port < port_count+1; ++port) {
|
|---|
| 759 | bool interrupt =
|
|---|
| 760 | (((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2;
|
|---|
| 761 | if (interrupt) {
|
|---|
| 762 | usb_hub_process_interrupt(
|
|---|
| 763 | hub_info, port);
|
|---|
| 764 | }
|
|---|
| 765 | }
|
|---|
| 766 | usb_hc_connection_close(&hub_info->connection);
|
|---|
| 767 | usb_pipe_end_session(hub_info->control_pipe);
|
|---|
| 768 | usb_pipe_end_session(hub_info->status_change_pipe);
|
|---|
| 769 | free(change_bitmap);
|
|---|
| 770 | return EOK;
|
|---|
| 771 | }
|
|---|
| 772 | #endif
|
|---|
| 773 |
|
|---|
| 774 |
|
|---|
| 775 | /**
|
|---|
| 776 | * @}
|
|---|
| 777 | */
|
|---|