[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);
|
---|
| 235 | opResult = usb_request_get_descriptor(&result->endpoints.control,
|
---|
[ad4562c2] | 236 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 237 | USB_DESCTYPE_HUB,
|
---|
| 238 | 0, 0, serialized_descriptor,
|
---|
[e080332] | 239 | USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
|
---|
[d81ef61c] | 240 | usb_endpoint_pipe_end_session(&result->endpoints.control);
|
---|
| 241 |
|
---|
[e080332] | 242 | if (opResult != EOK) {
|
---|
[103a3626] | 243 | dprintf(USB_LOG_LEVEL_ERROR, "failed when receiving hub descriptor, badcode = %d",opResult);
|
---|
[e080332] | 244 | free(serialized_descriptor);
|
---|
| 245 | return result;
|
---|
| 246 | }
|
---|
[103a3626] | 247 | dprintf(USB_LOG_LEVEL_DEBUG2, "deserializing descriptor");
|
---|
[e080332] | 248 | descriptor = usb_deserialize_hub_desriptor(serialized_descriptor);
|
---|
| 249 | if(descriptor==NULL){
|
---|
[103a3626] | 250 | dprintf(USB_LOG_LEVEL_WARNING, "could not deserialize descriptor ");
|
---|
[e080332] | 251 | result->port_count = 1;///\TODO this code is only for debug!!!
|
---|
| 252 | return result;
|
---|
| 253 | }
|
---|
[15b0432] | 254 |
|
---|
[103a3626] | 255 | dprintf(USB_LOG_LEVEL_INFO, "setting port count to %d",descriptor->ports_count);
|
---|
[e080332] | 256 | result->port_count = descriptor->ports_count;
|
---|
[6bb83c7] | 257 | result->attached_devs = (usb_hc_attached_device_t*)
|
---|
| 258 | malloc((result->port_count+1) * sizeof(usb_hc_attached_device_t));
|
---|
[e080332] | 259 | int i;
|
---|
| 260 | for(i=0;i<result->port_count+1;++i){
|
---|
[6bb83c7] | 261 | result->attached_devs[i].handle=0;
|
---|
[e080332] | 262 | result->attached_devs[i].address=0;
|
---|
| 263 | }
|
---|
[103a3626] | 264 | dprintf(USB_LOG_LEVEL_DEBUG2, "freeing data");
|
---|
[e080332] | 265 | free(serialized_descriptor);
|
---|
| 266 | free(descriptor->devices_removable);
|
---|
| 267 | free(descriptor);
|
---|
| 268 |
|
---|
| 269 | //finish
|
---|
| 270 |
|
---|
[103a3626] | 271 | dprintf(USB_LOG_LEVEL_INFO, "hub info created");
|
---|
[e080332] | 272 |
|
---|
| 273 | return result;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[15b0432] | 276 | /**
|
---|
| 277 | * Create hub representation and add it into hub list
|
---|
| 278 | * @param dev
|
---|
| 279 | * @return
|
---|
| 280 | */
|
---|
[eb1a2f4] | 281 | int usb_add_hub_device(ddf_dev_t *dev) {
|
---|
[103a3626] | 282 | dprintf(USB_LOG_LEVEL_INFO, "add_hub_device(handle=%d)", (int) dev->handle);
|
---|
[e080332] | 283 |
|
---|
[eb1a2f4] | 284 | //dev->ops = &hub_device_ops;
|
---|
| 285 | (void) hub_device_ops;
|
---|
[e080332] | 286 |
|
---|
[d81ef61c] | 287 | usb_hub_info_t * hub_info = usb_create_hub_info(dev);
|
---|
[6bb83c7] | 288 |
|
---|
[e080332] | 289 | int opResult;
|
---|
| 290 |
|
---|
[15b0432] | 291 | //perform final configurations
|
---|
| 292 | usb_endpoint_pipe_start_session(&hub_info->endpoints.control);
|
---|
| 293 | // process descriptors
|
---|
| 294 | opResult = usb_hub_process_configuration_descriptors(hub_info);
|
---|
| 295 | if(opResult != EOK){
|
---|
| 296 | dprintf(USB_LOG_LEVEL_ERROR,"could not get condiguration descriptors, %d",
|
---|
| 297 | opResult);
|
---|
[e080332] | 298 | return opResult;
|
---|
| 299 | }
|
---|
[15b0432] | 300 | //power ports
|
---|
[f40a1e2] | 301 | usb_device_request_setup_packet_t request;
|
---|
[15b0432] | 302 | int port;
|
---|
[e080332] | 303 | for (port = 1; port < hub_info->port_count+1; ++port) {
|
---|
| 304 | usb_hub_set_power_port_request(&request, port);
|
---|
[d81ef61c] | 305 | opResult = usb_endpoint_pipe_control_write(&hub_info->endpoints.control,
|
---|
| 306 | &request,sizeof(usb_device_request_setup_packet_t), NULL, 0);
|
---|
[103a3626] | 307 | dprintf(USB_LOG_LEVEL_INFO, "powering port %d",port);
|
---|
[e080332] | 308 | if (opResult != EOK) {
|
---|
[103a3626] | 309 | dprintf(USB_LOG_LEVEL_WARNING, "something went wrong when setting hub`s %dth port", port);
|
---|
[e080332] | 310 | }
|
---|
| 311 | }
|
---|
| 312 | //ports powered, hub seems to be enabled
|
---|
[d81ef61c] | 313 | usb_endpoint_pipe_end_session(&hub_info->endpoints.control);
|
---|
[e080332] | 314 |
|
---|
| 315 | //add the hub to list
|
---|
[cd4b184] | 316 | //is this needed now?
|
---|
[ba5ab09] | 317 | fibril_mutex_lock(&usb_hub_list_lock);
|
---|
[e080332] | 318 | usb_lst_append(&usb_hub_list, hub_info);
|
---|
[ba5ab09] | 319 | fibril_mutex_unlock(&usb_hub_list_lock);
|
---|
[103a3626] | 320 | dprintf(USB_LOG_LEVEL_DEBUG, "hub info added to list");
|
---|
[cd4b184] | 321 |
|
---|
[cc34f32f] | 322 | dprintf(USB_LOG_LEVEL_DEBUG, "adding to ddf");
|
---|
| 323 | ddf_fun_t *hub_fun = ddf_fun_create(dev, fun_exposed, "hub");
|
---|
| 324 | assert(hub_fun != NULL);
|
---|
| 325 | hub_fun->ops = NULL;
|
---|
| 326 |
|
---|
| 327 | int rc = ddf_fun_bind(hub_fun);
|
---|
| 328 | assert(rc == EOK);
|
---|
| 329 | rc = ddf_fun_add_to_class(hub_fun, "hub");
|
---|
| 330 | assert(rc == EOK);
|
---|
| 331 |
|
---|
[cd4b184] | 332 | fid_t fid = fibril_create(usb_hub_control_loop, hub_info);
|
---|
| 333 | if (fid == 0) {
|
---|
| 334 | dprintf(USB_LOG_LEVEL_ERROR,
|
---|
| 335 | ": failed to start monitoring fibril for new hub");
|
---|
| 336 | return ENOMEM;
|
---|
| 337 | }
|
---|
| 338 | fibril_add_ready(fid);
|
---|
| 339 |
|
---|
| 340 | dprintf(USB_LOG_LEVEL_DEBUG, "hub fibril created");
|
---|
[e080332] | 341 | //(void)hub_info;
|
---|
[cd4b184] | 342 | //usb_hub_check_hub_changes();
|
---|
[e080332] | 343 |
|
---|
[103a3626] | 344 | dprintf(USB_LOG_LEVEL_INFO, "hub dev added");
|
---|
[6bb83c7] | 345 | //address is lost...
|
---|
[103a3626] | 346 | dprintf(USB_LOG_LEVEL_DEBUG, "\taddress %d, has %d ports ",
|
---|
[6bb83c7] | 347 | //hub_info->endpoints.control.,
|
---|
[e080332] | 348 | hub_info->port_count);
|
---|
| 349 |
|
---|
| 350 | return EOK;
|
---|
| 351 | //return ENOTSUP;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 |
|
---|
| 355 | //*********************************************
|
---|
| 356 | //
|
---|
| 357 | // hub driver code, main loop
|
---|
| 358 | //
|
---|
| 359 | //*********************************************
|
---|
| 360 |
|
---|
| 361 | /**
|
---|
[f40a1e2] | 362 | * Reset the port with new device and reserve the default address.
|
---|
[e080332] | 363 | * @param hc
|
---|
| 364 | * @param port
|
---|
| 365 | * @param target
|
---|
| 366 | */
|
---|
[d81ef61c] | 367 | static void usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port) {
|
---|
[e080332] | 368 | usb_device_request_setup_packet_t request;
|
---|
| 369 | int opResult;
|
---|
[103a3626] | 370 | dprintf(USB_LOG_LEVEL_INFO, "some connection changed");
|
---|
[6bb83c7] | 371 | assert(hub->endpoints.control.hc_phone);
|
---|
[e080332] | 372 | //get default address
|
---|
[6bb83c7] | 373 | //opResult = usb_drv_reserve_default_address(hc);
|
---|
[62066b4] | 374 | opResult = usb_hc_reserve_default_address(&hub->connection, USB_SPEED_LOW);
|
---|
[cd4b184] | 375 |
|
---|
[e080332] | 376 | if (opResult != EOK) {
|
---|
[cd4b184] | 377 | dprintf(USB_LOG_LEVEL_WARNING,
|
---|
| 378 | "cannot assign default address, it is probably used %d",opResult);
|
---|
[e080332] | 379 | return;
|
---|
| 380 | }
|
---|
| 381 | //reset port
|
---|
| 382 | usb_hub_set_reset_port_request(&request, port);
|
---|
[6bb83c7] | 383 | opResult = usb_endpoint_pipe_control_write(
|
---|
| 384 | &hub->endpoints.control,
|
---|
| 385 | &request,sizeof(usb_device_request_setup_packet_t),
|
---|
[e080332] | 386 | NULL, 0
|
---|
| 387 | );
|
---|
| 388 | if (opResult != EOK) {
|
---|
[cd4b184] | 389 | dprintf(USB_LOG_LEVEL_ERROR,
|
---|
| 390 | "something went wrong when reseting a port %d",opResult);
|
---|
[6bb83c7] | 391 | //usb_hub_release_default_address(hc);
|
---|
| 392 | usb_hc_release_default_address(&hub->connection);
|
---|
[e080332] | 393 | }
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | /**
|
---|
[f40a1e2] | 397 | * Finalize adding new device after port reset
|
---|
[e080332] | 398 | * @param hc
|
---|
| 399 | * @param port
|
---|
| 400 | * @param target
|
---|
| 401 | */
|
---|
| 402 | static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
|
---|
[cc34f32f] | 403 | uint16_t port, bool isLowSpeed) {
|
---|
[e080332] | 404 |
|
---|
| 405 | int opResult;
|
---|
[103a3626] | 406 | dprintf(USB_LOG_LEVEL_INFO, "finalizing add device");
|
---|
[6bb83c7] | 407 | opResult = usb_hub_clear_port_feature(&hub->endpoints.control,
|
---|
[e080332] | 408 | port, USB_HUB_FEATURE_C_PORT_RESET);
|
---|
[6bb83c7] | 409 |
|
---|
[e080332] | 410 | if (opResult != EOK) {
|
---|
[103a3626] | 411 | dprintf(USB_LOG_LEVEL_ERROR, "failed to clear port reset feature");
|
---|
[6bb83c7] | 412 | usb_hc_release_default_address(&hub->connection);
|
---|
[e080332] | 413 | return;
|
---|
| 414 | }
|
---|
[6bb83c7] | 415 | //create connection to device
|
---|
| 416 | usb_endpoint_pipe_t new_device_pipe;
|
---|
| 417 | usb_device_connection_t new_device_connection;
|
---|
| 418 | usb_device_connection_initialize_on_default_address(
|
---|
| 419 | &new_device_connection,
|
---|
| 420 | &hub->connection
|
---|
| 421 | );
|
---|
| 422 | usb_endpoint_pipe_initialize_default_control(
|
---|
| 423 | &new_device_pipe,
|
---|
| 424 | &new_device_connection);
|
---|
| 425 | /// \TODO get highspeed info
|
---|
[cc34f32f] | 426 | usb_speed_t speed = isLowSpeed?USB_SPEED_LOW:USB_SPEED_FULL;
|
---|
[e080332] | 427 |
|
---|
[6bb83c7] | 428 |
|
---|
| 429 | /* Request address from host controller. */
|
---|
| 430 | usb_address_t new_device_address = usb_hc_request_address(
|
---|
| 431 | &hub->connection,
|
---|
[cc34f32f] | 432 | speed/// \TODO fullspeed??
|
---|
[6bb83c7] | 433 | );
|
---|
[e080332] | 434 | if (new_device_address < 0) {
|
---|
[103a3626] | 435 | dprintf(USB_LOG_LEVEL_ERROR, "failed to get free USB address");
|
---|
[e080332] | 436 | opResult = new_device_address;
|
---|
[6bb83c7] | 437 | usb_hc_release_default_address(&hub->connection);
|
---|
[e080332] | 438 | return;
|
---|
| 439 | }
|
---|
[103a3626] | 440 | dprintf(USB_LOG_LEVEL_INFO, "setting new address %d",new_device_address);
|
---|
[6bb83c7] | 441 | //opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
|
---|
| 442 | // new_device_address);
|
---|
[cd4b184] | 443 | usb_endpoint_pipe_start_session(&new_device_pipe);
|
---|
[6bb83c7] | 444 | opResult = usb_request_set_address(&new_device_pipe,new_device_address);
|
---|
[cd4b184] | 445 | usb_endpoint_pipe_end_session(&new_device_pipe);
|
---|
[e080332] | 446 | if (opResult != EOK) {
|
---|
[cd4b184] | 447 | dprintf(USB_LOG_LEVEL_ERROR,
|
---|
| 448 | "could not set address for new device %d",opResult);
|
---|
[6bb83c7] | 449 | usb_hc_release_default_address(&hub->connection);
|
---|
[e080332] | 450 | return;
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 |
|
---|
[6bb83c7] | 454 | //opResult = usb_hub_release_default_address(hc);
|
---|
| 455 | opResult = usb_hc_release_default_address(&hub->connection);
|
---|
[e080332] | 456 | if(opResult!=EOK){
|
---|
| 457 | return;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | devman_handle_t child_handle;
|
---|
[6bb83c7] | 461 | //??
|
---|
| 462 | opResult = usb_device_register_child_in_devman(new_device_address,
|
---|
[eb1a2f4] | 463 | hub->connection.hc_handle, hub->device, &child_handle,
|
---|
| 464 | NULL, NULL, NULL);
|
---|
[6bb83c7] | 465 |
|
---|
[e080332] | 466 | if (opResult != EOK) {
|
---|
[cd4b184] | 467 | dprintf(USB_LOG_LEVEL_ERROR,
|
---|
| 468 | "could not start driver for new device %d",opResult);
|
---|
[e080332] | 469 | return;
|
---|
| 470 | }
|
---|
[6bb83c7] | 471 | hub->attached_devs[port].handle = child_handle;
|
---|
[e080332] | 472 | hub->attached_devs[port].address = new_device_address;
|
---|
| 473 |
|
---|
[6bb83c7] | 474 | //opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
|
---|
| 475 | opResult = usb_hc_register_device(
|
---|
| 476 | &hub->connection,
|
---|
| 477 | &hub->attached_devs[port]);
|
---|
[e080332] | 478 | if (opResult != EOK) {
|
---|
[cd4b184] | 479 | dprintf(USB_LOG_LEVEL_ERROR,
|
---|
| 480 | "could not assign address of device in hcd %d",opResult);
|
---|
[e080332] | 481 | return;
|
---|
| 482 | }
|
---|
[103a3626] | 483 | dprintf(USB_LOG_LEVEL_INFO, "new device address %d, handle %zu",
|
---|
[e080332] | 484 | new_device_address, child_handle);
|
---|
| 485 |
|
---|
| 486 | }
|
---|
| 487 |
|
---|
| 488 | /**
|
---|
[f40a1e2] | 489 | * Unregister device address in hc
|
---|
[e080332] | 490 | * @param hc
|
---|
| 491 | * @param port
|
---|
| 492 | * @param target
|
---|
| 493 | */
|
---|
| 494 | static void usb_hub_removed_device(
|
---|
[6bb83c7] | 495 | usb_hub_info_t * hub,uint16_t port) {
|
---|
[e080332] | 496 | //usb_device_request_setup_packet_t request;
|
---|
| 497 | int opResult;
|
---|
[5097bed4] | 498 |
|
---|
[f40a1e2] | 499 | /** \TODO remove device from device manager - not yet implemented in
|
---|
| 500 | * devide manager
|
---|
| 501 | */
|
---|
[6bb83c7] | 502 |
|
---|
[e080332] | 503 | //close address
|
---|
| 504 | if(hub->attached_devs[port].address!=0){
|
---|
[6bb83c7] | 505 | //opResult = usb_drv_release_address(hc,hub->attached_devs[port].address);
|
---|
| 506 | opResult = usb_hc_unregister_device(
|
---|
| 507 | &hub->connection, hub->attached_devs[port].address);
|
---|
[e080332] | 508 | if(opResult != EOK) {
|
---|
[103a3626] | 509 | dprintf(USB_LOG_LEVEL_WARNING, "could not release address of " \
|
---|
[0cfc68f0] | 510 | "removed device: %d", opResult);
|
---|
[e080332] | 511 | }
|
---|
| 512 | hub->attached_devs[port].address = 0;
|
---|
[6bb83c7] | 513 | hub->attached_devs[port].handle = 0;
|
---|
[e080332] | 514 | }else{
|
---|
[103a3626] | 515 | dprintf(USB_LOG_LEVEL_WARNING, "this is strange, disconnected device had no address");
|
---|
[e080332] | 516 | //device was disconnected before it`s port was reset - return default address
|
---|
[6bb83c7] | 517 | //usb_drv_release_default_address(hc);
|
---|
| 518 | usb_hc_release_default_address(&hub->connection);
|
---|
[e080332] | 519 | }
|
---|
| 520 | }
|
---|
| 521 |
|
---|
[cd4b184] | 522 |
|
---|
| 523 | /**
|
---|
| 524 | *Process over current condition on port.
|
---|
| 525 | *
|
---|
| 526 | * Turn off the power on the port.
|
---|
| 527 | *
|
---|
| 528 | * @param hub
|
---|
| 529 | * @param port
|
---|
| 530 | */
|
---|
| 531 | static void usb_hub_over_current( usb_hub_info_t * hub,
|
---|
| 532 | uint16_t port){
|
---|
| 533 | int opResult;
|
---|
| 534 | opResult = usb_hub_clear_port_feature(&hub->endpoints.control,
|
---|
| 535 | port, USB_HUB_FEATURE_PORT_POWER);
|
---|
| 536 | if(opResult!=EOK){
|
---|
| 537 | dprintf(USB_LOG_LEVEL_ERROR, "cannot power off port %d; %d",
|
---|
| 538 | port, opResult);
|
---|
| 539 | }
|
---|
| 540 | }
|
---|
| 541 |
|
---|
[e080332] | 542 | /**
|
---|
[f40a1e2] | 543 | * Process interrupts on given hub port
|
---|
[e080332] | 544 | * @param hc
|
---|
| 545 | * @param port
|
---|
| 546 | * @param target
|
---|
| 547 | */
|
---|
[d81ef61c] | 548 | static void usb_hub_process_interrupt(usb_hub_info_t * hub,
|
---|
| 549 | uint16_t port) {
|
---|
[103a3626] | 550 | dprintf(USB_LOG_LEVEL_DEBUG, "interrupt at port %d", port);
|
---|
[e080332] | 551 | //determine type of change
|
---|
[d81ef61c] | 552 | usb_endpoint_pipe_t *pipe = &hub->endpoints.control;
|
---|
[6bb83c7] | 553 |
|
---|
[cd4b184] | 554 | int opResult;
|
---|
[d81ef61c] | 555 |
|
---|
[e080332] | 556 | usb_port_status_t status;
|
---|
| 557 | size_t rcvd_size;
|
---|
| 558 | usb_device_request_setup_packet_t request;
|
---|
[d81ef61c] | 559 | //int opResult;
|
---|
[e080332] | 560 | usb_hub_set_port_status_request(&request, port);
|
---|
| 561 | //endpoint 0
|
---|
| 562 |
|
---|
[d81ef61c] | 563 | opResult = usb_endpoint_pipe_control_read(
|
---|
| 564 | pipe,
|
---|
| 565 | &request, sizeof(usb_device_request_setup_packet_t),
|
---|
[e080332] | 566 | &status, 4, &rcvd_size
|
---|
| 567 | );
|
---|
| 568 | if (opResult != EOK) {
|
---|
[cd4b184] | 569 | dprintf(USB_LOG_LEVEL_ERROR, "could not get port status");
|
---|
[e080332] | 570 | return;
|
---|
| 571 | }
|
---|
| 572 | if (rcvd_size != sizeof (usb_port_status_t)) {
|
---|
[cd4b184] | 573 | dprintf(USB_LOG_LEVEL_ERROR, "received status has incorrect size");
|
---|
[e080332] | 574 | return;
|
---|
| 575 | }
|
---|
| 576 | //something connected/disconnected
|
---|
| 577 | if (usb_port_connect_change(&status)) {
|
---|
[6bb83c7] | 578 | opResult = usb_hub_clear_port_feature(pipe,
|
---|
[e080332] | 579 | port, USB_HUB_FEATURE_C_PORT_CONNECTION);
|
---|
| 580 | // TODO: check opResult
|
---|
| 581 | if (usb_port_dev_connected(&status)) {
|
---|
[103a3626] | 582 | dprintf(USB_LOG_LEVEL_INFO, "some connection changed");
|
---|
[d81ef61c] | 583 | usb_hub_init_add_device(hub, port);
|
---|
[e080332] | 584 | } else {
|
---|
[d81ef61c] | 585 | usb_hub_removed_device(hub, port);
|
---|
[e080332] | 586 | }
|
---|
| 587 | }
|
---|
[cd4b184] | 588 | //over current
|
---|
| 589 | if (usb_port_overcurrent_change(&status)) {
|
---|
| 590 | //check if it was not auto-resolved
|
---|
| 591 | if(usb_port_over_current(&status)){
|
---|
| 592 | usb_hub_over_current(hub,port);
|
---|
| 593 | }else{
|
---|
| 594 | dprintf(USB_LOG_LEVEL_INFO,
|
---|
| 595 | "over current condition was auto-resolved on port %d",port);
|
---|
| 596 | }
|
---|
| 597 | }
|
---|
[e080332] | 598 | //port reset
|
---|
| 599 | if (usb_port_reset_completed(&status)) {
|
---|
[103a3626] | 600 | dprintf(USB_LOG_LEVEL_INFO, "port reset complete");
|
---|
[e080332] | 601 | if (usb_port_enabled(&status)) {
|
---|
[cc34f32f] | 602 | usb_hub_finalize_add_device(hub, port, usb_port_low_speed(&status));
|
---|
[e080332] | 603 | } else {
|
---|
[cd4b184] | 604 | dprintf(USB_LOG_LEVEL_WARNING, "port reset, but port still not enabled");
|
---|
[e080332] | 605 | }
|
---|
| 606 | }
|
---|
| 607 |
|
---|
| 608 | usb_port_set_connect_change(&status, false);
|
---|
| 609 | usb_port_set_reset(&status, false);
|
---|
| 610 | usb_port_set_reset_completed(&status, false);
|
---|
| 611 | usb_port_set_dev_connected(&status, false);
|
---|
[f40a1e2] | 612 | if (status>>16) {
|
---|
[103a3626] | 613 | dprintf(USB_LOG_LEVEL_INFO, "there was some unsupported change on port %d: %X",port,status);
|
---|
[f40a1e2] | 614 |
|
---|
[e080332] | 615 | }
|
---|
| 616 | /// \TODO handle other changes
|
---|
| 617 | }
|
---|
| 618 |
|
---|
[f40a1e2] | 619 | /**
|
---|
[cd4b184] | 620 | * Check changes on particular hub
|
---|
| 621 | * @param hub_info_param
|
---|
[e080332] | 622 | */
|
---|
[cd4b184] | 623 | void usb_hub_check_hub_changes(usb_hub_info_t * hub_info){
|
---|
| 624 | int opResult;
|
---|
| 625 | opResult = usb_endpoint_pipe_start_session(&hub_info->endpoints.status_change);
|
---|
| 626 | if(opResult != EOK){
|
---|
| 627 | dprintf(USB_LOG_LEVEL_ERROR,
|
---|
| 628 | "could not initialize communication for hub; %d", opResult);
|
---|
| 629 | return;
|
---|
| 630 | }
|
---|
| 631 |
|
---|
| 632 | size_t port_count = hub_info->port_count;
|
---|
[e080332] | 633 |
|
---|
[cd4b184] | 634 | /// FIXME: count properly
|
---|
| 635 | size_t byte_length = ((port_count+1) / 8) + 1;
|
---|
[e080332] | 636 | void *change_bitmap = malloc(byte_length);
|
---|
[cd4b184] | 637 | size_t actual_size;
|
---|
[e080332] | 638 |
|
---|
[cd4b184] | 639 | /*
|
---|
| 640 | * Send the request.
|
---|
| 641 | */
|
---|
| 642 | opResult = usb_endpoint_pipe_read(
|
---|
| 643 | &hub_info->endpoints.status_change,
|
---|
| 644 | change_bitmap, byte_length, &actual_size
|
---|
| 645 | );
|
---|
[e080332] | 646 |
|
---|
[cd4b184] | 647 | if (opResult != EOK) {
|
---|
[5097bed4] | 648 | free(change_bitmap);
|
---|
[cd4b184] | 649 | dprintf(USB_LOG_LEVEL_WARNING, "something went wrong while getting status of hub");
|
---|
[dff940f8] | 650 | usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
|
---|
[cd4b184] | 651 | return;
|
---|
| 652 | }
|
---|
| 653 | unsigned int port;
|
---|
| 654 | opResult = usb_endpoint_pipe_start_session(&hub_info->endpoints.control);
|
---|
[dff940f8] | 655 | if(opResult!=EOK){
|
---|
| 656 | dprintf(USB_LOG_LEVEL_ERROR, "could not start control pipe session %d",
|
---|
| 657 | opResult);
|
---|
| 658 | usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
|
---|
| 659 | return;
|
---|
| 660 | }
|
---|
| 661 | opResult = usb_hc_connection_open(&hub_info->connection);
|
---|
| 662 | if(opResult!=EOK){
|
---|
| 663 | dprintf(USB_LOG_LEVEL_ERROR, "could not start host controller session %d",
|
---|
| 664 | opResult);
|
---|
| 665 | usb_endpoint_pipe_end_session(&hub_info->endpoints.control);
|
---|
| 666 | usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
|
---|
| 667 | return;
|
---|
| 668 | }
|
---|
[cd4b184] | 669 |
|
---|
| 670 | ///todo, opresult check, pre obe konekce
|
---|
| 671 | for (port = 1; port < port_count+1; ++port) {
|
---|
| 672 | bool interrupt =
|
---|
| 673 | (((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2;
|
---|
| 674 | if (interrupt) {
|
---|
| 675 | usb_hub_process_interrupt(
|
---|
| 676 | hub_info, port);
|
---|
| 677 | }
|
---|
| 678 | }
|
---|
| 679 | usb_hc_connection_close(&hub_info->connection);
|
---|
| 680 | usb_endpoint_pipe_end_session(&hub_info->endpoints.control);
|
---|
| 681 | usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
|
---|
| 682 | free(change_bitmap);
|
---|
| 683 | }
|
---|
[e080332] | 684 |
|
---|
| 685 |
|
---|
| 686 |
|
---|
| 687 | /**
|
---|
| 688 | * @}
|
---|
[71ed4849] | 689 | */
|
---|