| [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>
|
|---|
| [e080332] | 39 |
|
|---|
| [71ed4849] | 40 | #include <usb_iface.h>
|
|---|
| [357a302] | 41 | #include <usb/ddfiface.h>
|
|---|
| [e080332] | 42 | #include <usb/descriptor.h>
|
|---|
| [4e8e1f5] | 43 | #include <usb/recognise.h>
|
|---|
| [d81ef61c] | 44 | #include <usb/request.h>
|
|---|
| [e080332] | 45 | #include <usb/classes/hub.h>
|
|---|
| [cd4b184] | 46 | #include <stdio.h>
|
|---|
| [e080332] | 47 |
|
|---|
| 48 | #include "usbhub.h"
|
|---|
| 49 | #include "usbhub_private.h"
|
|---|
| 50 | #include "port_status.h"
|
|---|
| [f40a1e2] | 51 | #include "usb/usb.h"
|
|---|
| [d81ef61c] | 52 | #include "usb/pipes.h"
|
|---|
| [15b0432] | 53 | #include "usb/classes/classes.h"
|
|---|
| [e080332] | 54 |
|
|---|
| [eb1a2f4] | 55 | static ddf_dev_ops_t hub_device_ops = {
|
|---|
| [357a302] | 56 | .interfaces[USB_DEV_IFACE] = &usb_iface_hub_impl
|
|---|
| [71ed4849] | 57 | };
|
|---|
| 58 |
|
|---|
| [cd4b184] | 59 | /** Hub status-change endpoint description
|
|---|
| 60 | *
|
|---|
| 61 | * For more see usb hub specification in 11.15.1 of
|
|---|
| 62 | */
|
|---|
| [15b0432] | 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,
|
|---|
| [cd4b184] | 67 | .interface_subclass = 0,
|
|---|
| 68 | .interface_protocol = 0,
|
|---|
| [15b0432] | 69 | .flags = 0
|
|---|
| 70 | };
|
|---|
| 71 |
|
|---|
| [cd4b184] | 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 |
|
|---|
| [15b0432] | 81 |
|
|---|
| [e080332] | 82 | //*********************************************
|
|---|
| 83 | //
|
|---|
| 84 | // hub driver code, initialization
|
|---|
| 85 | //
|
|---|
| 86 | //*********************************************
|
|---|
| 87 |
|
|---|
| [15b0432] | 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){
|
|---|
| [eb1a2f4] | 96 | usb_log_debug("Initializing hub USB communication (hub->device->handle=%zu).\n", hub->device->handle);
|
|---|
| [15b0432] | 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 | }
|
|---|
| [eb1a2f4] | 106 | usb_log_debug("Initializing USB wire abstraction.\n");
|
|---|
| [15b0432] | 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 | }
|
|---|
| [eb1a2f4] | 114 | usb_log_debug("Initializing default control pipe.\n");
|
|---|
| [15b0432] | 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 |
|
|---|
| [d70e0a3c] | 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(
|
|---|
| [15b0432] | 155 | &hub->endpoints.control, 0,
|
|---|
| [d70e0a3c] | 156 | (void **) &descriptors, &descriptors_size);
|
|---|
| 157 | if (opResult != EOK) {
|
|---|
| 158 | usb_log_error("Could not get configuration descriptor: %s.\n",
|
|---|
| 159 | str_error(opResult));
|
|---|
| [15b0432] | 160 | return opResult;
|
|---|
| 161 | }
|
|---|
| [d70e0a3c] | 162 | usb_standard_configuration_descriptor_t *config_descriptor
|
|---|
| 163 | = (usb_standard_configuration_descriptor_t *) descriptors;
|
|---|
| 164 |
|
|---|
| 165 | /* Set configuration. */
|
|---|
| [15b0432] | 166 | opResult = usb_request_set_configuration(&hub->endpoints.control,
|
|---|
| [d70e0a3c] | 167 | config_descriptor->configuration_number);
|
|---|
| [15b0432] | 168 |
|
|---|
| 169 | if (opResult != EOK) {
|
|---|
| [d70e0a3c] | 170 | usb_log_error("Failed to set hub configuration: %s.\n",
|
|---|
| 171 | str_error(opResult));
|
|---|
| [15b0432] | 172 | return opResult;
|
|---|
| 173 | }
|
|---|
| 174 | dprintf(USB_LOG_LEVEL_DEBUG, "\tused configuration %d",
|
|---|
| [d70e0a3c] | 175 | config_descriptor->configuration_number);
|
|---|
| [15b0432] | 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,
|
|---|
| [d70e0a3c] | 187 | descriptors, descriptors_size,
|
|---|
| [15b0432] | 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 | */
|
|---|
| [eb1a2f4] | 212 | usb_hub_info_t * usb_create_hub_info(ddf_dev_t * device) {
|
|---|
| [e080332] | 213 | usb_hub_info_t* result = usb_new(usb_hub_info_t);
|
|---|
| [15b0432] | 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 | }
|
|---|
| [d81ef61c] | 221 |
|
|---|
| [e080332] | 222 | //result->device = device;
|
|---|
| 223 | result->port_count = -1;
|
|---|
| 224 | result->device = device;
|
|---|
| 225 |
|
|---|
| [6bb83c7] | 226 | //result->usb_device = usb_new(usb_hcd_attached_device_info_t);
|
|---|
| [15b0432] | 227 | size_t received_size;
|
|---|
| [e080332] | 228 |
|
|---|
| [15b0432] | 229 | // get hub descriptor
|
|---|
| [103a3626] | 230 | dprintf(USB_LOG_LEVEL_DEBUG, "creating serialized descripton");
|
|---|
| [e080332] | 231 | void * serialized_descriptor = malloc(USB_HUB_MAX_DESCRIPTOR_SIZE);
|
|---|
| 232 | usb_hub_descriptor_t * descriptor;
|
|---|
| [103a3626] | 233 | dprintf(USB_LOG_LEVEL_DEBUG, "starting control transaction");
|
|---|
| [d81ef61c] | 234 | usb_endpoint_pipe_start_session(&result->endpoints.control);
|
|---|
| [4e900c1] | 235 | opResult = usb_request_set_configuration(&result->endpoints.control, 1);
|
|---|
| 236 | assert(opResult == EOK);
|
|---|
| 237 |
|
|---|
| [d81ef61c] | 238 | opResult = usb_request_get_descriptor(&result->endpoints.control,
|
|---|
| [140c033] | 239 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
|
|---|
| [ad4562c2] | 240 | USB_DESCTYPE_HUB,
|
|---|
| 241 | 0, 0, serialized_descriptor,
|
|---|
| [e080332] | 242 | USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
|
|---|
| [d81ef61c] | 243 | usb_endpoint_pipe_end_session(&result->endpoints.control);
|
|---|
| 244 |
|
|---|
| [e080332] | 245 | if (opResult != EOK) {
|
|---|
| [103a3626] | 246 | dprintf(USB_LOG_LEVEL_ERROR, "failed when receiving hub descriptor, badcode = %d",opResult);
|
|---|
| [e080332] | 247 | free(serialized_descriptor);
|
|---|
| [332f860] | 248 | free(result);
|
|---|
| 249 | return NULL;
|
|---|
| [e080332] | 250 | }
|
|---|
| [103a3626] | 251 | dprintf(USB_LOG_LEVEL_DEBUG2, "deserializing descriptor");
|
|---|
| [e080332] | 252 | descriptor = usb_deserialize_hub_desriptor(serialized_descriptor);
|
|---|
| 253 | if(descriptor==NULL){
|
|---|
| [103a3626] | 254 | dprintf(USB_LOG_LEVEL_WARNING, "could not deserialize descriptor ");
|
|---|
| [332f860] | 255 | free(result);
|
|---|
| 256 | return NULL;
|
|---|
| [e080332] | 257 | }
|
|---|
| [15b0432] | 258 |
|
|---|
| [103a3626] | 259 | dprintf(USB_LOG_LEVEL_INFO, "setting port count to %d",descriptor->ports_count);
|
|---|
| [e080332] | 260 | result->port_count = descriptor->ports_count;
|
|---|
| [6bb83c7] | 261 | result->attached_devs = (usb_hc_attached_device_t*)
|
|---|
| 262 | malloc((result->port_count+1) * sizeof(usb_hc_attached_device_t));
|
|---|
| [e080332] | 263 | int i;
|
|---|
| 264 | for(i=0;i<result->port_count+1;++i){
|
|---|
| [6bb83c7] | 265 | result->attached_devs[i].handle=0;
|
|---|
| [e080332] | 266 | result->attached_devs[i].address=0;
|
|---|
| 267 | }
|
|---|
| [103a3626] | 268 | dprintf(USB_LOG_LEVEL_DEBUG2, "freeing data");
|
|---|
| [e080332] | 269 | free(serialized_descriptor);
|
|---|
| 270 | free(descriptor->devices_removable);
|
|---|
| 271 | free(descriptor);
|
|---|
| 272 |
|
|---|
| 273 | //finish
|
|---|
| 274 |
|
|---|
| [103a3626] | 275 | dprintf(USB_LOG_LEVEL_INFO, "hub info created");
|
|---|
| [e080332] | 276 |
|
|---|
| 277 | return result;
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| [15b0432] | 280 | /**
|
|---|
| 281 | * Create hub representation and add it into hub list
|
|---|
| 282 | * @param dev
|
|---|
| 283 | * @return
|
|---|
| 284 | */
|
|---|
| [eb1a2f4] | 285 | int usb_add_hub_device(ddf_dev_t *dev) {
|
|---|
| [103a3626] | 286 | dprintf(USB_LOG_LEVEL_INFO, "add_hub_device(handle=%d)", (int) dev->handle);
|
|---|
| [e080332] | 287 |
|
|---|
| [eb1a2f4] | 288 | //dev->ops = &hub_device_ops;
|
|---|
| 289 | (void) hub_device_ops;
|
|---|
| [e080332] | 290 |
|
|---|
| [d81ef61c] | 291 | usb_hub_info_t * hub_info = usb_create_hub_info(dev);
|
|---|
| [332f860] | 292 | if(!hub_info){
|
|---|
| 293 | return EINTR;
|
|---|
| 294 | }
|
|---|
| [6bb83c7] | 295 |
|
|---|
| [e080332] | 296 | int opResult;
|
|---|
| 297 |
|
|---|
| [15b0432] | 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){
|
|---|
| [332f860] | 303 | dprintf(USB_LOG_LEVEL_ERROR,"could not get configuration descriptors, %d",
|
|---|
| [15b0432] | 304 | opResult);
|
|---|
| [e080332] | 305 | return opResult;
|
|---|
| 306 | }
|
|---|
| [15b0432] | 307 | //power ports
|
|---|
| [f40a1e2] | 308 | usb_device_request_setup_packet_t request;
|
|---|
| [15b0432] | 309 | int port;
|
|---|
| [e080332] | 310 | for (port = 1; port < hub_info->port_count+1; ++port) {
|
|---|
| 311 | usb_hub_set_power_port_request(&request, port);
|
|---|
| [d81ef61c] | 312 | opResult = usb_endpoint_pipe_control_write(&hub_info->endpoints.control,
|
|---|
| 313 | &request,sizeof(usb_device_request_setup_packet_t), NULL, 0);
|
|---|
| [103a3626] | 314 | dprintf(USB_LOG_LEVEL_INFO, "powering port %d",port);
|
|---|
| [e080332] | 315 | if (opResult != EOK) {
|
|---|
| [103a3626] | 316 | dprintf(USB_LOG_LEVEL_WARNING, "something went wrong when setting hub`s %dth port", port);
|
|---|
| [e080332] | 317 | }
|
|---|
| 318 | }
|
|---|
| 319 | //ports powered, hub seems to be enabled
|
|---|
| [d81ef61c] | 320 | usb_endpoint_pipe_end_session(&hub_info->endpoints.control);
|
|---|
| [e080332] | 321 |
|
|---|
| 322 | //add the hub to list
|
|---|
| [cd4b184] | 323 | //is this needed now?
|
|---|
| [ba5ab09] | 324 | fibril_mutex_lock(&usb_hub_list_lock);
|
|---|
| [e080332] | 325 | usb_lst_append(&usb_hub_list, hub_info);
|
|---|
| [ba5ab09] | 326 | fibril_mutex_unlock(&usb_hub_list_lock);
|
|---|
| [103a3626] | 327 | dprintf(USB_LOG_LEVEL_DEBUG, "hub info added to list");
|
|---|
| [cd4b184] | 328 |
|
|---|
| [cc34f32f] | 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 |
|
|---|
| [cd4b184] | 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");
|
|---|
| [e080332] | 348 | //(void)hub_info;
|
|---|
| [cd4b184] | 349 | //usb_hub_check_hub_changes();
|
|---|
| [e080332] | 350 |
|
|---|
| [103a3626] | 351 | dprintf(USB_LOG_LEVEL_INFO, "hub dev added");
|
|---|
| [6bb83c7] | 352 | //address is lost...
|
|---|
| [103a3626] | 353 | dprintf(USB_LOG_LEVEL_DEBUG, "\taddress %d, has %d ports ",
|
|---|
| [6bb83c7] | 354 | //hub_info->endpoints.control.,
|
|---|
| [e080332] | 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 | /**
|
|---|
| [f40a1e2] | 369 | * Reset the port with new device and reserve the default address.
|
|---|
| [e080332] | 370 | * @param hc
|
|---|
| 371 | * @param port
|
|---|
| 372 | * @param target
|
|---|
| 373 | */
|
|---|
| [d81ef61c] | 374 | static void usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port) {
|
|---|
| [e080332] | 375 | usb_device_request_setup_packet_t request;
|
|---|
| 376 | int opResult;
|
|---|
| [103a3626] | 377 | dprintf(USB_LOG_LEVEL_INFO, "some connection changed");
|
|---|
| [6bb83c7] | 378 | assert(hub->endpoints.control.hc_phone);
|
|---|
| [e080332] | 379 | //get default address
|
|---|
| [6bb83c7] | 380 | //opResult = usb_drv_reserve_default_address(hc);
|
|---|
| [62066b4] | 381 | opResult = usb_hc_reserve_default_address(&hub->connection, USB_SPEED_LOW);
|
|---|
| [cd4b184] | 382 |
|
|---|
| [e080332] | 383 | if (opResult != EOK) {
|
|---|
| [cd4b184] | 384 | dprintf(USB_LOG_LEVEL_WARNING,
|
|---|
| 385 | "cannot assign default address, it is probably used %d",opResult);
|
|---|
| [e080332] | 386 | return;
|
|---|
| 387 | }
|
|---|
| 388 | //reset port
|
|---|
| 389 | usb_hub_set_reset_port_request(&request, port);
|
|---|
| [6bb83c7] | 390 | opResult = usb_endpoint_pipe_control_write(
|
|---|
| 391 | &hub->endpoints.control,
|
|---|
| 392 | &request,sizeof(usb_device_request_setup_packet_t),
|
|---|
| [e080332] | 393 | NULL, 0
|
|---|
| 394 | );
|
|---|
| 395 | if (opResult != EOK) {
|
|---|
| [cd4b184] | 396 | dprintf(USB_LOG_LEVEL_ERROR,
|
|---|
| 397 | "something went wrong when reseting a port %d",opResult);
|
|---|
| [6bb83c7] | 398 | //usb_hub_release_default_address(hc);
|
|---|
| 399 | usb_hc_release_default_address(&hub->connection);
|
|---|
| [e080332] | 400 | }
|
|---|
| 401 | }
|
|---|
| 402 |
|
|---|
| 403 | /**
|
|---|
| [f40a1e2] | 404 | * Finalize adding new device after port reset
|
|---|
| [e080332] | 405 | * @param hc
|
|---|
| 406 | * @param port
|
|---|
| 407 | * @param target
|
|---|
| 408 | */
|
|---|
| 409 | static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
|
|---|
| [cc34f32f] | 410 | uint16_t port, bool isLowSpeed) {
|
|---|
| [e080332] | 411 |
|
|---|
| 412 | int opResult;
|
|---|
| [103a3626] | 413 | dprintf(USB_LOG_LEVEL_INFO, "finalizing add device");
|
|---|
| [6bb83c7] | 414 | opResult = usb_hub_clear_port_feature(&hub->endpoints.control,
|
|---|
| [e080332] | 415 | port, USB_HUB_FEATURE_C_PORT_RESET);
|
|---|
| [6bb83c7] | 416 |
|
|---|
| [e080332] | 417 | if (opResult != EOK) {
|
|---|
| [103a3626] | 418 | dprintf(USB_LOG_LEVEL_ERROR, "failed to clear port reset feature");
|
|---|
| [6bb83c7] | 419 | usb_hc_release_default_address(&hub->connection);
|
|---|
| [e080332] | 420 | return;
|
|---|
| 421 | }
|
|---|
| [6bb83c7] | 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
|
|---|
| [cc34f32f] | 433 | usb_speed_t speed = isLowSpeed?USB_SPEED_LOW:USB_SPEED_FULL;
|
|---|
| [e080332] | 434 |
|
|---|
| [6bb83c7] | 435 |
|
|---|
| 436 | /* Request address from host controller. */
|
|---|
| 437 | usb_address_t new_device_address = usb_hc_request_address(
|
|---|
| 438 | &hub->connection,
|
|---|
| [cc34f32f] | 439 | speed/// \TODO fullspeed??
|
|---|
| [6bb83c7] | 440 | );
|
|---|
| [e080332] | 441 | if (new_device_address < 0) {
|
|---|
| [103a3626] | 442 | dprintf(USB_LOG_LEVEL_ERROR, "failed to get free USB address");
|
|---|
| [e080332] | 443 | opResult = new_device_address;
|
|---|
| [6bb83c7] | 444 | usb_hc_release_default_address(&hub->connection);
|
|---|
| [e080332] | 445 | return;
|
|---|
| 446 | }
|
|---|
| [103a3626] | 447 | dprintf(USB_LOG_LEVEL_INFO, "setting new address %d",new_device_address);
|
|---|
| [6bb83c7] | 448 | //opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
|
|---|
| 449 | // new_device_address);
|
|---|
| [cd4b184] | 450 | usb_endpoint_pipe_start_session(&new_device_pipe);
|
|---|
| [6bb83c7] | 451 | opResult = usb_request_set_address(&new_device_pipe,new_device_address);
|
|---|
| [cd4b184] | 452 | usb_endpoint_pipe_end_session(&new_device_pipe);
|
|---|
| [e080332] | 453 | if (opResult != EOK) {
|
|---|
| [cd4b184] | 454 | dprintf(USB_LOG_LEVEL_ERROR,
|
|---|
| 455 | "could not set address for new device %d",opResult);
|
|---|
| [6bb83c7] | 456 | usb_hc_release_default_address(&hub->connection);
|
|---|
| [e080332] | 457 | return;
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| [6bb83c7] | 461 | //opResult = usb_hub_release_default_address(hc);
|
|---|
| 462 | opResult = usb_hc_release_default_address(&hub->connection);
|
|---|
| [e080332] | 463 | if(opResult!=EOK){
|
|---|
| 464 | return;
|
|---|
| 465 | }
|
|---|
| 466 |
|
|---|
| 467 | devman_handle_t child_handle;
|
|---|
| [6bb83c7] | 468 | //??
|
|---|
| 469 | opResult = usb_device_register_child_in_devman(new_device_address,
|
|---|
| [eb1a2f4] | 470 | hub->connection.hc_handle, hub->device, &child_handle,
|
|---|
| 471 | NULL, NULL, NULL);
|
|---|
| [6bb83c7] | 472 |
|
|---|
| [e080332] | 473 | if (opResult != EOK) {
|
|---|
| [cd4b184] | 474 | dprintf(USB_LOG_LEVEL_ERROR,
|
|---|
| 475 | "could not start driver for new device %d",opResult);
|
|---|
| [e080332] | 476 | return;
|
|---|
| 477 | }
|
|---|
| [6bb83c7] | 478 | hub->attached_devs[port].handle = child_handle;
|
|---|
| [e080332] | 479 | hub->attached_devs[port].address = new_device_address;
|
|---|
| 480 |
|
|---|
| [6bb83c7] | 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]);
|
|---|
| [e080332] | 485 | if (opResult != EOK) {
|
|---|
| [cd4b184] | 486 | dprintf(USB_LOG_LEVEL_ERROR,
|
|---|
| 487 | "could not assign address of device in hcd %d",opResult);
|
|---|
| [e080332] | 488 | return;
|
|---|
| 489 | }
|
|---|
| [103a3626] | 490 | dprintf(USB_LOG_LEVEL_INFO, "new device address %d, handle %zu",
|
|---|
| [e080332] | 491 | new_device_address, child_handle);
|
|---|
| 492 |
|
|---|
| 493 | }
|
|---|
| 494 |
|
|---|
| 495 | /**
|
|---|
| [f40a1e2] | 496 | * Unregister device address in hc
|
|---|
| [e080332] | 497 | * @param hc
|
|---|
| 498 | * @param port
|
|---|
| 499 | * @param target
|
|---|
| 500 | */
|
|---|
| 501 | static void usb_hub_removed_device(
|
|---|
| [6bb83c7] | 502 | usb_hub_info_t * hub,uint16_t port) {
|
|---|
| [e080332] | 503 | //usb_device_request_setup_packet_t request;
|
|---|
| 504 | int opResult;
|
|---|
| [5097bed4] | 505 |
|
|---|
| [f40a1e2] | 506 | /** \TODO remove device from device manager - not yet implemented in
|
|---|
| 507 | * devide manager
|
|---|
| 508 | */
|
|---|
| [6bb83c7] | 509 |
|
|---|
| [e080332] | 510 | //close address
|
|---|
| 511 | if(hub->attached_devs[port].address!=0){
|
|---|
| [6bb83c7] | 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);
|
|---|
| [e080332] | 515 | if(opResult != EOK) {
|
|---|
| [103a3626] | 516 | dprintf(USB_LOG_LEVEL_WARNING, "could not release address of " \
|
|---|
| [0cfc68f0] | 517 | "removed device: %d", opResult);
|
|---|
| [e080332] | 518 | }
|
|---|
| 519 | hub->attached_devs[port].address = 0;
|
|---|
| [6bb83c7] | 520 | hub->attached_devs[port].handle = 0;
|
|---|
| [e080332] | 521 | }else{
|
|---|
| [103a3626] | 522 | dprintf(USB_LOG_LEVEL_WARNING, "this is strange, disconnected device had no address");
|
|---|
| [e080332] | 523 | //device was disconnected before it`s port was reset - return default address
|
|---|
| [6bb83c7] | 524 | //usb_drv_release_default_address(hc);
|
|---|
| 525 | usb_hc_release_default_address(&hub->connection);
|
|---|
| [e080332] | 526 | }
|
|---|
| 527 | }
|
|---|
| 528 |
|
|---|
| [cd4b184] | 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 |
|
|---|
| [e080332] | 549 | /**
|
|---|
| [f40a1e2] | 550 | * Process interrupts on given hub port
|
|---|
| [e080332] | 551 | * @param hc
|
|---|
| 552 | * @param port
|
|---|
| 553 | * @param target
|
|---|
| 554 | */
|
|---|
| [d81ef61c] | 555 | static void usb_hub_process_interrupt(usb_hub_info_t * hub,
|
|---|
| 556 | uint16_t port) {
|
|---|
| [103a3626] | 557 | dprintf(USB_LOG_LEVEL_DEBUG, "interrupt at port %d", port);
|
|---|
| [e080332] | 558 | //determine type of change
|
|---|
| [d81ef61c] | 559 | usb_endpoint_pipe_t *pipe = &hub->endpoints.control;
|
|---|
| [6bb83c7] | 560 |
|
|---|
| [cd4b184] | 561 | int opResult;
|
|---|
| [d81ef61c] | 562 |
|
|---|
| [e080332] | 563 | usb_port_status_t status;
|
|---|
| 564 | size_t rcvd_size;
|
|---|
| 565 | usb_device_request_setup_packet_t request;
|
|---|
| [d81ef61c] | 566 | //int opResult;
|
|---|
| [e080332] | 567 | usb_hub_set_port_status_request(&request, port);
|
|---|
| 568 | //endpoint 0
|
|---|
| 569 |
|
|---|
| [d81ef61c] | 570 | opResult = usb_endpoint_pipe_control_read(
|
|---|
| 571 | pipe,
|
|---|
| 572 | &request, sizeof(usb_device_request_setup_packet_t),
|
|---|
| [e080332] | 573 | &status, 4, &rcvd_size
|
|---|
| 574 | );
|
|---|
| 575 | if (opResult != EOK) {
|
|---|
| [cd4b184] | 576 | dprintf(USB_LOG_LEVEL_ERROR, "could not get port status");
|
|---|
| [e080332] | 577 | return;
|
|---|
| 578 | }
|
|---|
| 579 | if (rcvd_size != sizeof (usb_port_status_t)) {
|
|---|
| [cd4b184] | 580 | dprintf(USB_LOG_LEVEL_ERROR, "received status has incorrect size");
|
|---|
| [e080332] | 581 | return;
|
|---|
| 582 | }
|
|---|
| 583 | //something connected/disconnected
|
|---|
| 584 | if (usb_port_connect_change(&status)) {
|
|---|
| [6bb83c7] | 585 | opResult = usb_hub_clear_port_feature(pipe,
|
|---|
| [e080332] | 586 | port, USB_HUB_FEATURE_C_PORT_CONNECTION);
|
|---|
| 587 | // TODO: check opResult
|
|---|
| 588 | if (usb_port_dev_connected(&status)) {
|
|---|
| [103a3626] | 589 | dprintf(USB_LOG_LEVEL_INFO, "some connection changed");
|
|---|
| [d81ef61c] | 590 | usb_hub_init_add_device(hub, port);
|
|---|
| [e080332] | 591 | } else {
|
|---|
| [d81ef61c] | 592 | usb_hub_removed_device(hub, port);
|
|---|
| [e080332] | 593 | }
|
|---|
| 594 | }
|
|---|
| [cd4b184] | 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 | }
|
|---|
| [e080332] | 605 | //port reset
|
|---|
| 606 | if (usb_port_reset_completed(&status)) {
|
|---|
| [103a3626] | 607 | dprintf(USB_LOG_LEVEL_INFO, "port reset complete");
|
|---|
| [e080332] | 608 | if (usb_port_enabled(&status)) {
|
|---|
| [cc34f32f] | 609 | usb_hub_finalize_add_device(hub, port, usb_port_low_speed(&status));
|
|---|
| [e080332] | 610 | } else {
|
|---|
| [cd4b184] | 611 | dprintf(USB_LOG_LEVEL_WARNING, "port reset, but port still not enabled");
|
|---|
| [e080332] | 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);
|
|---|
| [f40a1e2] | 619 | if (status>>16) {
|
|---|
| [103a3626] | 620 | dprintf(USB_LOG_LEVEL_INFO, "there was some unsupported change on port %d: %X",port,status);
|
|---|
| [f40a1e2] | 621 |
|
|---|
| [e080332] | 622 | }
|
|---|
| 623 | /// \TODO handle other changes
|
|---|
| 624 | }
|
|---|
| 625 |
|
|---|
| [f40a1e2] | 626 | /**
|
|---|
| [cd4b184] | 627 | * Check changes on particular hub
|
|---|
| 628 | * @param hub_info_param
|
|---|
| [e080332] | 629 | */
|
|---|
| [cd4b184] | 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;
|
|---|
| [e080332] | 640 |
|
|---|
| [cd4b184] | 641 | /// FIXME: count properly
|
|---|
| 642 | size_t byte_length = ((port_count+1) / 8) + 1;
|
|---|
| [e080332] | 643 | void *change_bitmap = malloc(byte_length);
|
|---|
| [cd4b184] | 644 | size_t actual_size;
|
|---|
| [e080332] | 645 |
|
|---|
| [cd4b184] | 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 | );
|
|---|
| [e080332] | 653 |
|
|---|
| [cd4b184] | 654 | if (opResult != EOK) {
|
|---|
| [5097bed4] | 655 | free(change_bitmap);
|
|---|
| [cd4b184] | 656 | dprintf(USB_LOG_LEVEL_WARNING, "something went wrong while getting status of hub");
|
|---|
| [dff940f8] | 657 | usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
|
|---|
| [cd4b184] | 658 | return;
|
|---|
| 659 | }
|
|---|
| 660 | unsigned int port;
|
|---|
| 661 | opResult = usb_endpoint_pipe_start_session(&hub_info->endpoints.control);
|
|---|
| [dff940f8] | 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 | }
|
|---|
| [cd4b184] | 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 | }
|
|---|
| [e080332] | 691 |
|
|---|
| 692 |
|
|---|
| 693 |
|
|---|
| 694 | /**
|
|---|
| 695 | * @}
|
|---|
| [71ed4849] | 696 | */
|
|---|