| 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 | static ddf_dev_ops_t hub_device_ops = {
|
|---|
| 56 | .interfaces[USB_DEV_IFACE] = &usb_iface_hub_impl
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | /** Hub status-change endpoint description
|
|---|
| 60 | *
|
|---|
| 61 | * For more see usb hub specification in 11.15.1 of
|
|---|
| 62 | */
|
|---|
| 63 | static usb_endpoint_description_t status_change_endpoint_description = {
|
|---|
| 64 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
|---|
| 65 | .direction = USB_DIRECTION_IN,
|
|---|
| 66 | .interface_class = USB_CLASS_HUB,
|
|---|
| 67 | .interface_subclass = 0,
|
|---|
| 68 | .interface_protocol = 0,
|
|---|
| 69 | .flags = 0
|
|---|
| 70 | };
|
|---|
| 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 | while(true){
|
|---|
| 75 | usb_hub_check_hub_changes(hub_info);
|
|---|
| 76 | async_usleep(1000 * 1000 );/// \TODO proper number once
|
|---|
| 77 | }
|
|---|
| 78 | return 0;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 | //*********************************************
|
|---|
| 83 | //
|
|---|
| 84 | // hub driver code, initialization
|
|---|
| 85 | //
|
|---|
| 86 | //*********************************************
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Initialize connnections to host controller, device, and device
|
|---|
| 90 | * control endpoint
|
|---|
| 91 | * @param hub
|
|---|
| 92 | * @param device
|
|---|
| 93 | * @return
|
|---|
| 94 | */
|
|---|
| 95 | static int usb_hub_init_communication(usb_hub_info_t * hub){
|
|---|
| 96 | usb_log_debug("Initializing hub USB communication (hub->device->handle=%zu).\n", hub->device->handle);
|
|---|
| 97 | int opResult;
|
|---|
| 98 | opResult = usb_device_connection_initialize_from_device(
|
|---|
| 99 | &hub->device_connection,
|
|---|
| 100 | hub->device);
|
|---|
| 101 | if(opResult != EOK){
|
|---|
| 102 | dprintf(USB_LOG_LEVEL_ERROR,
|
|---|
| 103 | "could not initialize connection to hc, errno %d",opResult);
|
|---|
| 104 | return opResult;
|
|---|
| 105 | }
|
|---|
| 106 | usb_log_debug("Initializing USB wire abstraction.\n");
|
|---|
| 107 | opResult = usb_hc_connection_initialize_from_device(&hub->connection,
|
|---|
| 108 | hub->device);
|
|---|
| 109 | if(opResult != EOK){
|
|---|
| 110 | dprintf(USB_LOG_LEVEL_ERROR,
|
|---|
| 111 | "could not initialize connection to device, errno %d",opResult);
|
|---|
| 112 | return opResult;
|
|---|
| 113 | }
|
|---|
| 114 | usb_log_debug("Initializing default control pipe.\n");
|
|---|
| 115 | opResult = usb_endpoint_pipe_initialize_default_control(&hub->endpoints.control,
|
|---|
| 116 | &hub->device_connection);
|
|---|
| 117 | if(opResult != EOK){
|
|---|
| 118 | dprintf(USB_LOG_LEVEL_ERROR,
|
|---|
| 119 | "could not initialize connection to device endpoint, errno %d",opResult);
|
|---|
| 120 | }
|
|---|
| 121 | return opResult;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /**
|
|---|
| 125 | * When entering this function, hub->endpoints.control should be active.
|
|---|
| 126 | * @param hub
|
|---|
| 127 | * @return
|
|---|
| 128 | */
|
|---|
| 129 | static int usb_hub_process_configuration_descriptors(
|
|---|
| 130 | usb_hub_info_t * hub){
|
|---|
| 131 | if(hub==NULL) {
|
|---|
| 132 | return EINVAL;
|
|---|
| 133 | }
|
|---|
| 134 | int opResult;
|
|---|
| 135 |
|
|---|
| 136 | //device descriptor
|
|---|
| 137 | usb_standard_device_descriptor_t std_descriptor;
|
|---|
| 138 | opResult = usb_request_get_device_descriptor(&hub->endpoints.control,
|
|---|
| 139 | &std_descriptor);
|
|---|
| 140 | if(opResult!=EOK){
|
|---|
| 141 | dprintf(USB_LOG_LEVEL_ERROR, "could not get device descriptor, %d",opResult);
|
|---|
| 142 | return opResult;
|
|---|
| 143 | }
|
|---|
| 144 | dprintf(USB_LOG_LEVEL_INFO, "hub has %d configurations",
|
|---|
| 145 | std_descriptor.configuration_count);
|
|---|
| 146 | if(std_descriptor.configuration_count<1){
|
|---|
| 147 | dprintf(USB_LOG_LEVEL_ERROR, "THERE ARE NO CONFIGURATIONS AVAILABLE");
|
|---|
| 148 | //shouldn`t I return?
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /* Retrieve full configuration descriptor. */
|
|---|
| 152 | uint8_t *descriptors = NULL;
|
|---|
| 153 | size_t descriptors_size = 0;
|
|---|
| 154 | opResult = usb_request_get_full_configuration_descriptor_alloc(
|
|---|
| 155 | &hub->endpoints.control, 0,
|
|---|
| 156 | (void **) &descriptors, &descriptors_size);
|
|---|
| 157 | if (opResult != EOK) {
|
|---|
| 158 | usb_log_error("Could not get configuration descriptor: %s.\n",
|
|---|
| 159 | str_error(opResult));
|
|---|
| 160 | return opResult;
|
|---|
| 161 | }
|
|---|
| 162 | usb_standard_configuration_descriptor_t *config_descriptor
|
|---|
| 163 | = (usb_standard_configuration_descriptor_t *) descriptors;
|
|---|
| 164 |
|
|---|
| 165 | /* Set configuration. */
|
|---|
| 166 | opResult = usb_request_set_configuration(&hub->endpoints.control,
|
|---|
| 167 | config_descriptor->configuration_number);
|
|---|
| 168 |
|
|---|
| 169 | if (opResult != EOK) {
|
|---|
| 170 | usb_log_error("Failed to set hub configuration: %s.\n",
|
|---|
| 171 | str_error(opResult));
|
|---|
| 172 | return opResult;
|
|---|
| 173 | }
|
|---|
| 174 | dprintf(USB_LOG_LEVEL_DEBUG, "\tused configuration %d",
|
|---|
| 175 | config_descriptor->configuration_number);
|
|---|
| 176 |
|
|---|
| 177 | usb_endpoint_mapping_t endpoint_mapping[1] = {
|
|---|
| 178 | {
|
|---|
| 179 | .pipe = &hub->endpoints.status_change,
|
|---|
| 180 | .description = &status_change_endpoint_description,
|
|---|
| 181 | .interface_no =
|
|---|
| 182 | usb_device_get_assigned_interface(hub->device)
|
|---|
| 183 | }
|
|---|
| 184 | };
|
|---|
| 185 | opResult = usb_endpoint_pipe_initialize_from_configuration(
|
|---|
| 186 | endpoint_mapping, 1,
|
|---|
| 187 | descriptors, descriptors_size,
|
|---|
| 188 | &hub->device_connection);
|
|---|
| 189 | if (opResult != EOK) {
|
|---|
| 190 | dprintf(USB_LOG_LEVEL_ERROR,
|
|---|
| 191 | "Failed to initialize status change pipe: %s",
|
|---|
| 192 | str_error(opResult));
|
|---|
| 193 | return opResult;
|
|---|
| 194 | }
|
|---|
| 195 | if (!endpoint_mapping[0].present) {
|
|---|
| 196 | dprintf(USB_LOG_LEVEL_ERROR,"Not accepting device, " \
|
|---|
| 197 | "cannot understand what is happenning");
|
|---|
| 198 | return EREFUSED;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | free(descriptors);
|
|---|
| 202 | return EOK;
|
|---|
| 203 |
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 | /**
|
|---|
| 208 | * Create hub representation from device information.
|
|---|
| 209 | * @param device
|
|---|
| 210 | * @return pointer to created structure or NULL in case of error
|
|---|
| 211 | */
|
|---|
| 212 | usb_hub_info_t * usb_create_hub_info(ddf_dev_t * device) {
|
|---|
| 213 | usb_hub_info_t* result = usb_new(usb_hub_info_t);
|
|---|
| 214 | result->device = device;
|
|---|
| 215 | int opResult;
|
|---|
| 216 | opResult = usb_hub_init_communication(result);
|
|---|
| 217 | if(opResult != EOK){
|
|---|
| 218 | free(result);
|
|---|
| 219 | return NULL;
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | //result->device = device;
|
|---|
| 223 | result->port_count = -1;
|
|---|
| 224 | result->device = device;
|
|---|
| 225 |
|
|---|
| 226 | //result->usb_device = usb_new(usb_hcd_attached_device_info_t);
|
|---|
| 227 | size_t received_size;
|
|---|
| 228 |
|
|---|
| 229 | // get hub descriptor
|
|---|
| 230 | dprintf(USB_LOG_LEVEL_DEBUG, "creating serialized descripton");
|
|---|
| 231 | void * serialized_descriptor = malloc(USB_HUB_MAX_DESCRIPTOR_SIZE);
|
|---|
| 232 | usb_hub_descriptor_t * descriptor;
|
|---|
| 233 | dprintf(USB_LOG_LEVEL_DEBUG, "starting control transaction");
|
|---|
| 234 | usb_endpoint_pipe_start_session(&result->endpoints.control);
|
|---|
| 235 | opResult = usb_request_set_configuration(&result->endpoints.control, 1);
|
|---|
| 236 | assert(opResult == EOK);
|
|---|
| 237 |
|
|---|
| 238 | opResult = usb_request_get_descriptor(&result->endpoints.control,
|
|---|
| 239 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
|
|---|
| 240 | USB_DESCTYPE_HUB,
|
|---|
| 241 | 0, 0, serialized_descriptor,
|
|---|
| 242 | USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
|
|---|
| 243 | usb_endpoint_pipe_end_session(&result->endpoints.control);
|
|---|
| 244 |
|
|---|
| 245 | if (opResult != EOK) {
|
|---|
| 246 | dprintf(USB_LOG_LEVEL_ERROR, "failed when receiving hub descriptor, badcode = %d",opResult);
|
|---|
| 247 | free(serialized_descriptor);
|
|---|
| 248 | free(result);
|
|---|
| 249 | return NULL;
|
|---|
| 250 | }
|
|---|
| 251 | dprintf(USB_LOG_LEVEL_DEBUG2, "deserializing descriptor");
|
|---|
| 252 | descriptor = usb_deserialize_hub_desriptor(serialized_descriptor);
|
|---|
| 253 | if(descriptor==NULL){
|
|---|
| 254 | dprintf(USB_LOG_LEVEL_WARNING, "could not deserialize descriptor ");
|
|---|
| 255 | free(result);
|
|---|
| 256 | return NULL;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | dprintf(USB_LOG_LEVEL_INFO, "setting port count to %d",descriptor->ports_count);
|
|---|
| 260 | result->port_count = descriptor->ports_count;
|
|---|
| 261 | result->attached_devs = (usb_hc_attached_device_t*)
|
|---|
| 262 | malloc((result->port_count+1) * sizeof(usb_hc_attached_device_t));
|
|---|
| 263 | int i;
|
|---|
| 264 | for(i=0;i<result->port_count+1;++i){
|
|---|
| 265 | result->attached_devs[i].handle=0;
|
|---|
| 266 | result->attached_devs[i].address=0;
|
|---|
| 267 | }
|
|---|
| 268 | dprintf(USB_LOG_LEVEL_DEBUG2, "freeing data");
|
|---|
| 269 | free(serialized_descriptor);
|
|---|
| 270 | free(descriptor->devices_removable);
|
|---|
| 271 | free(descriptor);
|
|---|
| 272 |
|
|---|
| 273 | //finish
|
|---|
| 274 |
|
|---|
| 275 | dprintf(USB_LOG_LEVEL_INFO, "hub info created");
|
|---|
| 276 |
|
|---|
| 277 | return result;
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | /**
|
|---|
| 281 | * Create hub representation and add it into hub list
|
|---|
| 282 | * @param dev
|
|---|
| 283 | * @return
|
|---|
| 284 | */
|
|---|
| 285 | int usb_add_hub_device(ddf_dev_t *dev) {
|
|---|
| 286 | dprintf(USB_LOG_LEVEL_INFO, "add_hub_device(handle=%d)", (int) dev->handle);
|
|---|
| 287 |
|
|---|
| 288 | //dev->ops = &hub_device_ops;
|
|---|
| 289 | (void) hub_device_ops;
|
|---|
| 290 |
|
|---|
| 291 | usb_hub_info_t * hub_info = usb_create_hub_info(dev);
|
|---|
| 292 | if(!hub_info){
|
|---|
| 293 | return EINTR;
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | int opResult;
|
|---|
| 297 |
|
|---|
| 298 | //perform final configurations
|
|---|
| 299 | usb_endpoint_pipe_start_session(&hub_info->endpoints.control);
|
|---|
| 300 | // process descriptors
|
|---|
| 301 | opResult = usb_hub_process_configuration_descriptors(hub_info);
|
|---|
| 302 | if(opResult != EOK){
|
|---|
| 303 | dprintf(USB_LOG_LEVEL_ERROR,"could not get configuration descriptors, %d",
|
|---|
| 304 | opResult);
|
|---|
| 305 | return opResult;
|
|---|
| 306 | }
|
|---|
| 307 | //power ports
|
|---|
| 308 | usb_device_request_setup_packet_t request;
|
|---|
| 309 | int port;
|
|---|
| 310 | for (port = 1; port < hub_info->port_count+1; ++port) {
|
|---|
| 311 | usb_hub_set_power_port_request(&request, port);
|
|---|
| 312 | opResult = usb_endpoint_pipe_control_write(&hub_info->endpoints.control,
|
|---|
| 313 | &request,sizeof(usb_device_request_setup_packet_t), NULL, 0);
|
|---|
| 314 | dprintf(USB_LOG_LEVEL_INFO, "powering port %d",port);
|
|---|
| 315 | if (opResult != EOK) {
|
|---|
| 316 | dprintf(USB_LOG_LEVEL_WARNING, "something went wrong when setting hub`s %dth port", port);
|
|---|
| 317 | }
|
|---|
| 318 | }
|
|---|
| 319 | //ports powered, hub seems to be enabled
|
|---|
| 320 | usb_endpoint_pipe_end_session(&hub_info->endpoints.control);
|
|---|
| 321 |
|
|---|
| 322 | //add the hub to list
|
|---|
| 323 | //is this needed now?
|
|---|
| 324 | fibril_mutex_lock(&usb_hub_list_lock);
|
|---|
| 325 | usb_lst_append(&usb_hub_list, hub_info);
|
|---|
| 326 | fibril_mutex_unlock(&usb_hub_list_lock);
|
|---|
| 327 | dprintf(USB_LOG_LEVEL_DEBUG, "hub info added to list");
|
|---|
| 328 |
|
|---|
| 329 | dprintf(USB_LOG_LEVEL_DEBUG, "adding to ddf");
|
|---|
| 330 | ddf_fun_t *hub_fun = ddf_fun_create(dev, fun_exposed, "hub");
|
|---|
| 331 | assert(hub_fun != NULL);
|
|---|
| 332 | hub_fun->ops = NULL;
|
|---|
| 333 |
|
|---|
| 334 | int rc = ddf_fun_bind(hub_fun);
|
|---|
| 335 | assert(rc == EOK);
|
|---|
| 336 | rc = ddf_fun_add_to_class(hub_fun, "hub");
|
|---|
| 337 | assert(rc == EOK);
|
|---|
| 338 |
|
|---|
| 339 | fid_t fid = fibril_create(usb_hub_control_loop, hub_info);
|
|---|
| 340 | if (fid == 0) {
|
|---|
| 341 | dprintf(USB_LOG_LEVEL_ERROR,
|
|---|
| 342 | ": failed to start monitoring fibril for new hub");
|
|---|
| 343 | return ENOMEM;
|
|---|
| 344 | }
|
|---|
| 345 | fibril_add_ready(fid);
|
|---|
| 346 |
|
|---|
| 347 | dprintf(USB_LOG_LEVEL_DEBUG, "hub fibril created");
|
|---|
| 348 | //(void)hub_info;
|
|---|
| 349 | //usb_hub_check_hub_changes();
|
|---|
| 350 |
|
|---|
| 351 | dprintf(USB_LOG_LEVEL_INFO, "hub dev added");
|
|---|
| 352 | //address is lost...
|
|---|
| 353 | dprintf(USB_LOG_LEVEL_DEBUG, "\taddress %d, has %d ports ",
|
|---|
| 354 | //hub_info->endpoints.control.,
|
|---|
| 355 | hub_info->port_count);
|
|---|
| 356 |
|
|---|
| 357 | return EOK;
|
|---|
| 358 | //return ENOTSUP;
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 | //*********************************************
|
|---|
| 363 | //
|
|---|
| 364 | // hub driver code, main loop
|
|---|
| 365 | //
|
|---|
| 366 | //*********************************************
|
|---|
| 367 |
|
|---|
| 368 | /**
|
|---|
| 369 | * Reset the port with new device and reserve the default address.
|
|---|
| 370 | * @param hc
|
|---|
| 371 | * @param port
|
|---|
| 372 | * @param target
|
|---|
| 373 | */
|
|---|
| 374 | static void usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port) {
|
|---|
| 375 | usb_device_request_setup_packet_t request;
|
|---|
| 376 | int opResult;
|
|---|
| 377 | dprintf(USB_LOG_LEVEL_INFO, "some connection changed");
|
|---|
| 378 | assert(hub->endpoints.control.hc_phone);
|
|---|
| 379 | //get default address
|
|---|
| 380 | //opResult = usb_drv_reserve_default_address(hc);
|
|---|
| 381 | opResult = usb_hc_reserve_default_address(&hub->connection, USB_SPEED_LOW);
|
|---|
| 382 |
|
|---|
| 383 | if (opResult != EOK) {
|
|---|
| 384 | dprintf(USB_LOG_LEVEL_WARNING,
|
|---|
| 385 | "cannot assign default address, it is probably used %d",opResult);
|
|---|
| 386 | return;
|
|---|
| 387 | }
|
|---|
| 388 | //reset port
|
|---|
| 389 | usb_hub_set_reset_port_request(&request, port);
|
|---|
| 390 | opResult = usb_endpoint_pipe_control_write(
|
|---|
| 391 | &hub->endpoints.control,
|
|---|
| 392 | &request,sizeof(usb_device_request_setup_packet_t),
|
|---|
| 393 | NULL, 0
|
|---|
| 394 | );
|
|---|
| 395 | if (opResult != EOK) {
|
|---|
| 396 | dprintf(USB_LOG_LEVEL_ERROR,
|
|---|
| 397 | "something went wrong when reseting a port %d",opResult);
|
|---|
| 398 | //usb_hub_release_default_address(hc);
|
|---|
| 399 | usb_hc_release_default_address(&hub->connection);
|
|---|
| 400 | }
|
|---|
| 401 | }
|
|---|
| 402 |
|
|---|
| 403 | /**
|
|---|
| 404 | * Finalize adding new device after port reset
|
|---|
| 405 | * @param hc
|
|---|
| 406 | * @param port
|
|---|
| 407 | * @param target
|
|---|
| 408 | */
|
|---|
| 409 | static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
|
|---|
| 410 | uint16_t port, bool isLowSpeed) {
|
|---|
| 411 |
|
|---|
| 412 | int opResult;
|
|---|
| 413 | dprintf(USB_LOG_LEVEL_INFO, "finalizing add device");
|
|---|
| 414 | opResult = usb_hub_clear_port_feature(&hub->endpoints.control,
|
|---|
| 415 | port, USB_HUB_FEATURE_C_PORT_RESET);
|
|---|
| 416 |
|
|---|
| 417 | if (opResult != EOK) {
|
|---|
| 418 | dprintf(USB_LOG_LEVEL_ERROR, "failed to clear port reset feature");
|
|---|
| 419 | usb_hc_release_default_address(&hub->connection);
|
|---|
| 420 | return;
|
|---|
| 421 | }
|
|---|
| 422 | //create connection to device
|
|---|
| 423 | usb_endpoint_pipe_t new_device_pipe;
|
|---|
| 424 | usb_device_connection_t new_device_connection;
|
|---|
| 425 | usb_device_connection_initialize_on_default_address(
|
|---|
| 426 | &new_device_connection,
|
|---|
| 427 | &hub->connection
|
|---|
| 428 | );
|
|---|
| 429 | usb_endpoint_pipe_initialize_default_control(
|
|---|
| 430 | &new_device_pipe,
|
|---|
| 431 | &new_device_connection);
|
|---|
| 432 | /// \TODO get highspeed info
|
|---|
| 433 | usb_speed_t speed = isLowSpeed?USB_SPEED_LOW:USB_SPEED_FULL;
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 | /* Request address from host controller. */
|
|---|
| 437 | usb_address_t new_device_address = usb_hc_request_address(
|
|---|
| 438 | &hub->connection,
|
|---|
| 439 | speed/// \TODO fullspeed??
|
|---|
| 440 | );
|
|---|
| 441 | if (new_device_address < 0) {
|
|---|
| 442 | dprintf(USB_LOG_LEVEL_ERROR, "failed to get free USB address");
|
|---|
| 443 | opResult = new_device_address;
|
|---|
| 444 | usb_hc_release_default_address(&hub->connection);
|
|---|
| 445 | return;
|
|---|
| 446 | }
|
|---|
| 447 | dprintf(USB_LOG_LEVEL_INFO, "setting new address %d",new_device_address);
|
|---|
| 448 | //opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
|
|---|
| 449 | // new_device_address);
|
|---|
| 450 | usb_endpoint_pipe_start_session(&new_device_pipe);
|
|---|
| 451 | opResult = usb_request_set_address(&new_device_pipe,new_device_address);
|
|---|
| 452 | usb_endpoint_pipe_end_session(&new_device_pipe);
|
|---|
| 453 | if (opResult != EOK) {
|
|---|
| 454 | dprintf(USB_LOG_LEVEL_ERROR,
|
|---|
| 455 | "could not set address for new device %d",opResult);
|
|---|
| 456 | usb_hc_release_default_address(&hub->connection);
|
|---|
| 457 | return;
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 | //opResult = usb_hub_release_default_address(hc);
|
|---|
| 462 | opResult = usb_hc_release_default_address(&hub->connection);
|
|---|
| 463 | if(opResult!=EOK){
|
|---|
| 464 | return;
|
|---|
| 465 | }
|
|---|
| 466 |
|
|---|
| 467 | devman_handle_t child_handle;
|
|---|
| 468 | //??
|
|---|
| 469 | opResult = usb_device_register_child_in_devman(new_device_address,
|
|---|
| 470 | hub->connection.hc_handle, hub->device, &child_handle,
|
|---|
| 471 | NULL, NULL, NULL);
|
|---|
| 472 |
|
|---|
| 473 | if (opResult != EOK) {
|
|---|
| 474 | dprintf(USB_LOG_LEVEL_ERROR,
|
|---|
| 475 | "could not start driver for new device %d",opResult);
|
|---|
| 476 | return;
|
|---|
| 477 | }
|
|---|
| 478 | hub->attached_devs[port].handle = child_handle;
|
|---|
| 479 | hub->attached_devs[port].address = new_device_address;
|
|---|
| 480 |
|
|---|
| 481 | //opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
|
|---|
| 482 | opResult = usb_hc_register_device(
|
|---|
| 483 | &hub->connection,
|
|---|
| 484 | &hub->attached_devs[port]);
|
|---|
| 485 | if (opResult != EOK) {
|
|---|
| 486 | dprintf(USB_LOG_LEVEL_ERROR,
|
|---|
| 487 | "could not assign address of device in hcd %d",opResult);
|
|---|
| 488 | return;
|
|---|
| 489 | }
|
|---|
| 490 | dprintf(USB_LOG_LEVEL_INFO, "new device address %d, handle %zu",
|
|---|
| 491 | new_device_address, child_handle);
|
|---|
| 492 |
|
|---|
| 493 | }
|
|---|
| 494 |
|
|---|
| 495 | /**
|
|---|
| 496 | * Unregister device address in hc
|
|---|
| 497 | * @param hc
|
|---|
| 498 | * @param port
|
|---|
| 499 | * @param target
|
|---|
| 500 | */
|
|---|
| 501 | static void usb_hub_removed_device(
|
|---|
| 502 | usb_hub_info_t * hub,uint16_t port) {
|
|---|
| 503 | //usb_device_request_setup_packet_t request;
|
|---|
| 504 | int opResult;
|
|---|
| 505 |
|
|---|
| 506 | /** \TODO remove device from device manager - not yet implemented in
|
|---|
| 507 | * devide manager
|
|---|
| 508 | */
|
|---|
| 509 |
|
|---|
| 510 | //close address
|
|---|
| 511 | if(hub->attached_devs[port].address!=0){
|
|---|
| 512 | //opResult = usb_drv_release_address(hc,hub->attached_devs[port].address);
|
|---|
| 513 | opResult = usb_hc_unregister_device(
|
|---|
| 514 | &hub->connection, hub->attached_devs[port].address);
|
|---|
| 515 | if(opResult != EOK) {
|
|---|
| 516 | dprintf(USB_LOG_LEVEL_WARNING, "could not release address of " \
|
|---|
| 517 | "removed device: %d", opResult);
|
|---|
| 518 | }
|
|---|
| 519 | hub->attached_devs[port].address = 0;
|
|---|
| 520 | hub->attached_devs[port].handle = 0;
|
|---|
| 521 | }else{
|
|---|
| 522 | dprintf(USB_LOG_LEVEL_WARNING, "this is strange, disconnected device had no address");
|
|---|
| 523 | //device was disconnected before it`s port was reset - return default address
|
|---|
| 524 | //usb_drv_release_default_address(hc);
|
|---|
| 525 | usb_hc_release_default_address(&hub->connection);
|
|---|
| 526 | }
|
|---|
| 527 | }
|
|---|
| 528 |
|
|---|
| 529 |
|
|---|
| 530 | /**
|
|---|
| 531 | *Process over current condition on port.
|
|---|
| 532 | *
|
|---|
| 533 | * Turn off the power on the port.
|
|---|
| 534 | *
|
|---|
| 535 | * @param hub
|
|---|
| 536 | * @param port
|
|---|
| 537 | */
|
|---|
| 538 | static void usb_hub_over_current( usb_hub_info_t * hub,
|
|---|
| 539 | uint16_t port){
|
|---|
| 540 | int opResult;
|
|---|
| 541 | opResult = usb_hub_clear_port_feature(&hub->endpoints.control,
|
|---|
| 542 | port, USB_HUB_FEATURE_PORT_POWER);
|
|---|
| 543 | if(opResult!=EOK){
|
|---|
| 544 | dprintf(USB_LOG_LEVEL_ERROR, "cannot power off port %d; %d",
|
|---|
| 545 | port, opResult);
|
|---|
| 546 | }
|
|---|
| 547 | }
|
|---|
| 548 |
|
|---|
| 549 | /**
|
|---|
| 550 | * Process interrupts on given hub port
|
|---|
| 551 | * @param hc
|
|---|
| 552 | * @param port
|
|---|
| 553 | * @param target
|
|---|
| 554 | */
|
|---|
| 555 | static void usb_hub_process_interrupt(usb_hub_info_t * hub,
|
|---|
| 556 | uint16_t port) {
|
|---|
| 557 | dprintf(USB_LOG_LEVEL_DEBUG, "interrupt at port %d", port);
|
|---|
| 558 | //determine type of change
|
|---|
| 559 | usb_endpoint_pipe_t *pipe = &hub->endpoints.control;
|
|---|
| 560 |
|
|---|
| 561 | int opResult;
|
|---|
| 562 |
|
|---|
| 563 | usb_port_status_t status;
|
|---|
| 564 | size_t rcvd_size;
|
|---|
| 565 | usb_device_request_setup_packet_t request;
|
|---|
| 566 | //int opResult;
|
|---|
| 567 | usb_hub_set_port_status_request(&request, port);
|
|---|
| 568 | //endpoint 0
|
|---|
| 569 |
|
|---|
| 570 | opResult = usb_endpoint_pipe_control_read(
|
|---|
| 571 | pipe,
|
|---|
| 572 | &request, sizeof(usb_device_request_setup_packet_t),
|
|---|
| 573 | &status, 4, &rcvd_size
|
|---|
| 574 | );
|
|---|
| 575 | if (opResult != EOK) {
|
|---|
| 576 | dprintf(USB_LOG_LEVEL_ERROR, "could not get port status");
|
|---|
| 577 | return;
|
|---|
| 578 | }
|
|---|
| 579 | if (rcvd_size != sizeof (usb_port_status_t)) {
|
|---|
| 580 | dprintf(USB_LOG_LEVEL_ERROR, "received status has incorrect size");
|
|---|
| 581 | return;
|
|---|
| 582 | }
|
|---|
| 583 | //something connected/disconnected
|
|---|
| 584 | if (usb_port_connect_change(&status)) {
|
|---|
| 585 | opResult = usb_hub_clear_port_feature(pipe,
|
|---|
| 586 | port, USB_HUB_FEATURE_C_PORT_CONNECTION);
|
|---|
| 587 | // TODO: check opResult
|
|---|
| 588 | if (usb_port_dev_connected(&status)) {
|
|---|
| 589 | dprintf(USB_LOG_LEVEL_INFO, "some connection changed");
|
|---|
| 590 | usb_hub_init_add_device(hub, port);
|
|---|
| 591 | } else {
|
|---|
| 592 | usb_hub_removed_device(hub, port);
|
|---|
| 593 | }
|
|---|
| 594 | }
|
|---|
| 595 | //over current
|
|---|
| 596 | if (usb_port_overcurrent_change(&status)) {
|
|---|
| 597 | //check if it was not auto-resolved
|
|---|
| 598 | if(usb_port_over_current(&status)){
|
|---|
| 599 | usb_hub_over_current(hub,port);
|
|---|
| 600 | }else{
|
|---|
| 601 | dprintf(USB_LOG_LEVEL_INFO,
|
|---|
| 602 | "over current condition was auto-resolved on port %d",port);
|
|---|
| 603 | }
|
|---|
| 604 | }
|
|---|
| 605 | //port reset
|
|---|
| 606 | if (usb_port_reset_completed(&status)) {
|
|---|
| 607 | dprintf(USB_LOG_LEVEL_INFO, "port reset complete");
|
|---|
| 608 | if (usb_port_enabled(&status)) {
|
|---|
| 609 | usb_hub_finalize_add_device(hub, port, usb_port_low_speed(&status));
|
|---|
| 610 | } else {
|
|---|
| 611 | dprintf(USB_LOG_LEVEL_WARNING, "port reset, but port still not enabled");
|
|---|
| 612 | }
|
|---|
| 613 | }
|
|---|
| 614 |
|
|---|
| 615 | usb_port_set_connect_change(&status, false);
|
|---|
| 616 | usb_port_set_reset(&status, false);
|
|---|
| 617 | usb_port_set_reset_completed(&status, false);
|
|---|
| 618 | usb_port_set_dev_connected(&status, false);
|
|---|
| 619 | if (status>>16) {
|
|---|
| 620 | dprintf(USB_LOG_LEVEL_INFO, "there was some unsupported change on port %d: %X",port,status);
|
|---|
| 621 |
|
|---|
| 622 | }
|
|---|
| 623 | /// \TODO handle other changes
|
|---|
| 624 | }
|
|---|
| 625 |
|
|---|
| 626 | /**
|
|---|
| 627 | * Check changes on particular hub
|
|---|
| 628 | * @param hub_info_param
|
|---|
| 629 | */
|
|---|
| 630 | void usb_hub_check_hub_changes(usb_hub_info_t * hub_info){
|
|---|
| 631 | int opResult;
|
|---|
| 632 | opResult = usb_endpoint_pipe_start_session(&hub_info->endpoints.status_change);
|
|---|
| 633 | if(opResult != EOK){
|
|---|
| 634 | dprintf(USB_LOG_LEVEL_ERROR,
|
|---|
| 635 | "could not initialize communication for hub; %d", opResult);
|
|---|
| 636 | return;
|
|---|
| 637 | }
|
|---|
| 638 |
|
|---|
| 639 | size_t port_count = hub_info->port_count;
|
|---|
| 640 |
|
|---|
| 641 | /// FIXME: count properly
|
|---|
| 642 | size_t byte_length = ((port_count+1) / 8) + 1;
|
|---|
| 643 | void *change_bitmap = malloc(byte_length);
|
|---|
| 644 | size_t actual_size;
|
|---|
| 645 |
|
|---|
| 646 | /*
|
|---|
| 647 | * Send the request.
|
|---|
| 648 | */
|
|---|
| 649 | opResult = usb_endpoint_pipe_read(
|
|---|
| 650 | &hub_info->endpoints.status_change,
|
|---|
| 651 | change_bitmap, byte_length, &actual_size
|
|---|
| 652 | );
|
|---|
| 653 |
|
|---|
| 654 | if (opResult != EOK) {
|
|---|
| 655 | free(change_bitmap);
|
|---|
| 656 | dprintf(USB_LOG_LEVEL_WARNING, "something went wrong while getting status of hub");
|
|---|
| 657 | usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
|
|---|
| 658 | return;
|
|---|
| 659 | }
|
|---|
| 660 | unsigned int port;
|
|---|
| 661 | opResult = usb_endpoint_pipe_start_session(&hub_info->endpoints.control);
|
|---|
| 662 | if(opResult!=EOK){
|
|---|
| 663 | dprintf(USB_LOG_LEVEL_ERROR, "could not start control pipe session %d",
|
|---|
| 664 | opResult);
|
|---|
| 665 | usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
|
|---|
| 666 | return;
|
|---|
| 667 | }
|
|---|
| 668 | opResult = usb_hc_connection_open(&hub_info->connection);
|
|---|
| 669 | if(opResult!=EOK){
|
|---|
| 670 | dprintf(USB_LOG_LEVEL_ERROR, "could not start host controller session %d",
|
|---|
| 671 | opResult);
|
|---|
| 672 | usb_endpoint_pipe_end_session(&hub_info->endpoints.control);
|
|---|
| 673 | usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
|
|---|
| 674 | return;
|
|---|
| 675 | }
|
|---|
| 676 |
|
|---|
| 677 | ///todo, opresult check, pre obe konekce
|
|---|
| 678 | for (port = 1; port < port_count+1; ++port) {
|
|---|
| 679 | bool interrupt =
|
|---|
| 680 | (((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2;
|
|---|
| 681 | if (interrupt) {
|
|---|
| 682 | usb_hub_process_interrupt(
|
|---|
| 683 | hub_info, port);
|
|---|
| 684 | }
|
|---|
| 685 | }
|
|---|
| 686 | usb_hc_connection_close(&hub_info->connection);
|
|---|
| 687 | usb_endpoint_pipe_end_session(&hub_info->endpoints.control);
|
|---|
| 688 | usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
|
|---|
| 689 | free(change_bitmap);
|
|---|
| 690 | }
|
|---|
| 691 |
|
|---|
| 692 |
|
|---|
| 693 |
|
|---|
| 694 | /**
|
|---|
| 695 | * @}
|
|---|
| 696 | */
|
|---|