[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 |
|
---|
| 35 | #include <driver.h>
|
---|
| 36 | #include <bool.h>
|
---|
| 37 | #include <errno.h>
|
---|
[4e8e1f5] | 38 | #include <str_error.h>
|
---|
[e080332] | 39 |
|
---|
[71ed4849] | 40 | #include <usb_iface.h>
|
---|
[e080332] | 41 | #include <usb/usbdrv.h>
|
---|
| 42 | #include <usb/descriptor.h>
|
---|
[4e8e1f5] | 43 | #include <usb/recognise.h>
|
---|
[e080332] | 44 | #include <usb/devreq.h>
|
---|
[d81ef61c] | 45 | #include <usb/request.h>
|
---|
[e080332] | 46 | #include <usb/classes/hub.h>
|
---|
| 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"
|
---|
[e080332] | 53 |
|
---|
[563fb40] | 54 | static int iface_get_hc_handle(device_t *device, devman_handle_t *handle)
|
---|
| 55 | {
|
---|
| 56 | return usb_hc_find(device->handle, handle);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[71ed4849] | 59 | static usb_iface_t hub_usb_iface = {
|
---|
[563fb40] | 60 | .get_hc_handle = iface_get_hc_handle
|
---|
[71ed4849] | 61 | };
|
---|
| 62 |
|
---|
| 63 | static device_ops_t hub_device_ops = {
|
---|
| 64 | .interfaces[USB_DEV_IFACE] = &hub_usb_iface
|
---|
| 65 | };
|
---|
| 66 |
|
---|
[e080332] | 67 | //*********************************************
|
---|
| 68 | //
|
---|
| 69 | // hub driver code, initialization
|
---|
| 70 | //
|
---|
| 71 | //*********************************************
|
---|
| 72 |
|
---|
[d81ef61c] | 73 | usb_hub_info_t * usb_create_hub_info(device_t * device) {
|
---|
[e080332] | 74 | usb_hub_info_t* result = usb_new(usb_hub_info_t);
|
---|
[6bb83c7] | 75 | usb_device_connection_initialize_from_device(&result->device_connection,
|
---|
| 76 | device);
|
---|
| 77 | usb_hc_connection_initialize_from_device(&result->connection,
|
---|
| 78 | device);
|
---|
[d81ef61c] | 79 | usb_endpoint_pipe_initialize_default_control(&result->endpoints.control,
|
---|
[6bb83c7] | 80 | &result->device_connection);
|
---|
[d81ef61c] | 81 |
|
---|
| 82 |
|
---|
[e080332] | 83 | //result->device = device;
|
---|
| 84 | result->port_count = -1;
|
---|
| 85 | /// \TODO is this correct? is the device stored?
|
---|
| 86 | result->device = device;
|
---|
| 87 |
|
---|
[6bb83c7] | 88 | //result->usb_device = usb_new(usb_hcd_attached_device_info_t);
|
---|
[d81ef61c] | 89 |
|
---|
[e080332] | 90 | // get hub descriptor
|
---|
| 91 |
|
---|
[103a3626] | 92 | dprintf(USB_LOG_LEVEL_DEBUG, "creating serialized descripton");
|
---|
[e080332] | 93 | void * serialized_descriptor = malloc(USB_HUB_MAX_DESCRIPTOR_SIZE);
|
---|
| 94 | usb_hub_descriptor_t * descriptor;
|
---|
| 95 | size_t received_size;
|
---|
| 96 | int opResult;
|
---|
[103a3626] | 97 | dprintf(USB_LOG_LEVEL_DEBUG, "starting control transaction");
|
---|
[d81ef61c] | 98 | usb_endpoint_pipe_start_session(&result->endpoints.control);
|
---|
| 99 | opResult = usb_request_get_descriptor(&result->endpoints.control,
|
---|
[f40a1e2] | 100 | USB_REQUEST_TYPE_CLASS,
|
---|
| 101 | USB_DESCTYPE_HUB, 0, 0, serialized_descriptor,
|
---|
[e080332] | 102 | USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
|
---|
[d81ef61c] | 103 | usb_endpoint_pipe_end_session(&result->endpoints.control);
|
---|
| 104 |
|
---|
| 105 | /* Initialize the interrupt endpoint.
|
---|
| 106 | usb_endpoint_pipe_initalize(
|
---|
| 107 | &hub_data->endpoints->status_change,
|
---|
| 108 | &endpiont_descriptor, &hub_data->connection);
|
---|
| 109 |
|
---|
| 110 | */ /// \TODO add this call
|
---|
[f40a1e2] | 111 |
|
---|
[e080332] | 112 | if (opResult != EOK) {
|
---|
[103a3626] | 113 | dprintf(USB_LOG_LEVEL_ERROR, "failed when receiving hub descriptor, badcode = %d",opResult);
|
---|
[e080332] | 114 | free(serialized_descriptor);
|
---|
| 115 | return result;
|
---|
| 116 | }
|
---|
[103a3626] | 117 | dprintf(USB_LOG_LEVEL_DEBUG2, "deserializing descriptor");
|
---|
[e080332] | 118 | descriptor = usb_deserialize_hub_desriptor(serialized_descriptor);
|
---|
| 119 | if(descriptor==NULL){
|
---|
[103a3626] | 120 | dprintf(USB_LOG_LEVEL_WARNING, "could not deserialize descriptor ");
|
---|
[e080332] | 121 | result->port_count = 1;///\TODO this code is only for debug!!!
|
---|
| 122 | return result;
|
---|
| 123 | }
|
---|
[103a3626] | 124 | dprintf(USB_LOG_LEVEL_INFO, "setting port count to %d",descriptor->ports_count);
|
---|
[e080332] | 125 | result->port_count = descriptor->ports_count;
|
---|
[6bb83c7] | 126 | result->attached_devs = (usb_hc_attached_device_t*)
|
---|
| 127 | malloc((result->port_count+1) * sizeof(usb_hc_attached_device_t));
|
---|
[e080332] | 128 | int i;
|
---|
| 129 | for(i=0;i<result->port_count+1;++i){
|
---|
[6bb83c7] | 130 | result->attached_devs[i].handle=0;
|
---|
[e080332] | 131 | result->attached_devs[i].address=0;
|
---|
| 132 | }
|
---|
[103a3626] | 133 | dprintf(USB_LOG_LEVEL_DEBUG2, "freeing data");
|
---|
[e080332] | 134 | free(serialized_descriptor);
|
---|
| 135 | free(descriptor->devices_removable);
|
---|
| 136 | free(descriptor);
|
---|
| 137 |
|
---|
| 138 | //finish
|
---|
| 139 |
|
---|
[103a3626] | 140 | dprintf(USB_LOG_LEVEL_INFO, "hub info created");
|
---|
[e080332] | 141 |
|
---|
| 142 | return result;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | int usb_add_hub_device(device_t *dev) {
|
---|
[103a3626] | 146 | dprintf(USB_LOG_LEVEL_INFO, "add_hub_device(handle=%d)", (int) dev->handle);
|
---|
[e080332] | 147 |
|
---|
| 148 | /*
|
---|
| 149 | * We are some (probably deeply nested) hub.
|
---|
| 150 | * Thus, assign our own operations and explore already
|
---|
| 151 | * connected devices.
|
---|
| 152 | */
|
---|
[71ed4849] | 153 | dev->ops = &hub_device_ops;
|
---|
[e080332] | 154 |
|
---|
| 155 |
|
---|
[d81ef61c] | 156 | usb_hub_info_t * hub_info = usb_create_hub_info(dev);
|
---|
| 157 | usb_endpoint_pipe_start_session(&hub_info->endpoints.control);
|
---|
[6bb83c7] | 158 |
|
---|
[e080332] | 159 | int port;
|
---|
| 160 | int opResult;
|
---|
[d81ef61c] | 161 | //usb_target_t target;
|
---|
| 162 | //target.address = hub_info->usb_device->address;
|
---|
| 163 | //target.endpoint = 0;
|
---|
[e080332] | 164 |
|
---|
| 165 | //get configuration descriptor
|
---|
| 166 | // this is not fully correct - there are more configurations
|
---|
| 167 | // and all should be checked
|
---|
| 168 | usb_standard_device_descriptor_t std_descriptor;
|
---|
[d81ef61c] | 169 | opResult = usb_request_get_device_descriptor(&hub_info->endpoints.control,
|
---|
[0cfc68f0] | 170 | &std_descriptor);
|
---|
[e080332] | 171 | if(opResult!=EOK){
|
---|
[103a3626] | 172 | dprintf(USB_LOG_LEVEL_ERROR, "could not get device descriptor, %d",opResult);
|
---|
[e080332] | 173 | return opResult;
|
---|
| 174 | }
|
---|
[103a3626] | 175 | dprintf(USB_LOG_LEVEL_INFO, "hub has %d configurations",std_descriptor.configuration_count);
|
---|
[e080332] | 176 | if(std_descriptor.configuration_count<1){
|
---|
[103a3626] | 177 | dprintf(USB_LOG_LEVEL_ERROR, "THERE ARE NO CONFIGURATIONS AVAILABLE");
|
---|
[f40a1e2] | 178 | //shouldn`t I return?
|
---|
[e080332] | 179 | }
|
---|
[5097bed4] | 180 | /// \TODO check other configurations
|
---|
[e080332] | 181 | usb_standard_configuration_descriptor_t config_descriptor;
|
---|
[d81ef61c] | 182 | opResult = usb_request_get_bare_configuration_descriptor(
|
---|
| 183 | &hub_info->endpoints.control, 0,
|
---|
[e080332] | 184 | &config_descriptor);
|
---|
| 185 | if(opResult!=EOK){
|
---|
[103a3626] | 186 | dprintf(USB_LOG_LEVEL_ERROR, "could not get configuration descriptor, %d",opResult);
|
---|
[e080332] | 187 | return opResult;
|
---|
| 188 | }
|
---|
| 189 | //set configuration
|
---|
[d81ef61c] | 190 | opResult = usb_request_set_configuration(&hub_info->endpoints.control,
|
---|
[f40a1e2] | 191 | config_descriptor.configuration_number);
|
---|
| 192 |
|
---|
[e080332] | 193 | if (opResult != EOK) {
|
---|
[103a3626] | 194 | dprintf(USB_LOG_LEVEL_ERROR, "something went wrong when setting hub`s configuration, %d", opResult);
|
---|
[e080332] | 195 | }
|
---|
| 196 |
|
---|
[f40a1e2] | 197 | usb_device_request_setup_packet_t request;
|
---|
[e080332] | 198 | for (port = 1; port < hub_info->port_count+1; ++port) {
|
---|
| 199 | usb_hub_set_power_port_request(&request, port);
|
---|
[d81ef61c] | 200 | opResult = usb_endpoint_pipe_control_write(&hub_info->endpoints.control,
|
---|
| 201 | &request,sizeof(usb_device_request_setup_packet_t), NULL, 0);
|
---|
[103a3626] | 202 | dprintf(USB_LOG_LEVEL_INFO, "powering port %d",port);
|
---|
[e080332] | 203 | if (opResult != EOK) {
|
---|
[103a3626] | 204 | dprintf(USB_LOG_LEVEL_WARNING, "something went wrong when setting hub`s %dth port", port);
|
---|
[e080332] | 205 | }
|
---|
| 206 | }
|
---|
| 207 | //ports powered, hub seems to be enabled
|
---|
| 208 |
|
---|
[d81ef61c] | 209 | usb_endpoint_pipe_end_session(&hub_info->endpoints.control);
|
---|
| 210 | //async_hangup(hc);
|
---|
[e080332] | 211 |
|
---|
| 212 | //add the hub to list
|
---|
[ba5ab09] | 213 | fibril_mutex_lock(&usb_hub_list_lock);
|
---|
[e080332] | 214 | usb_lst_append(&usb_hub_list, hub_info);
|
---|
[ba5ab09] | 215 | fibril_mutex_unlock(&usb_hub_list_lock);
|
---|
[5097bed4] | 216 |
|
---|
[103a3626] | 217 | dprintf(USB_LOG_LEVEL_DEBUG, "hub info added to list");
|
---|
[e080332] | 218 | //(void)hub_info;
|
---|
| 219 | usb_hub_check_hub_changes();
|
---|
| 220 |
|
---|
| 221 |
|
---|
| 222 |
|
---|
[103a3626] | 223 | dprintf(USB_LOG_LEVEL_INFO, "hub dev added");
|
---|
[6bb83c7] | 224 | //address is lost...
|
---|
[103a3626] | 225 | dprintf(USB_LOG_LEVEL_DEBUG, "\taddress %d, has %d ports ",
|
---|
[6bb83c7] | 226 | //hub_info->endpoints.control.,
|
---|
[e080332] | 227 | hub_info->port_count);
|
---|
[103a3626] | 228 | dprintf(USB_LOG_LEVEL_DEBUG, "\tused configuration %d",config_descriptor.configuration_number);
|
---|
[e080332] | 229 |
|
---|
| 230 | return EOK;
|
---|
| 231 | //return ENOTSUP;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 |
|
---|
| 235 | //*********************************************
|
---|
| 236 | //
|
---|
| 237 | // hub driver code, main loop
|
---|
| 238 | //
|
---|
| 239 | //*********************************************
|
---|
| 240 |
|
---|
[5097bed4] | 241 | /**
|
---|
[f40a1e2] | 242 | * Convenience function for releasing default address and writing debug info
|
---|
| 243 | * (these few lines are used too often to be written again and again).
|
---|
[5097bed4] | 244 | * @param hc
|
---|
| 245 | * @return
|
---|
| 246 | */
|
---|
| 247 | inline static int usb_hub_release_default_address(int hc){
|
---|
| 248 | int opResult;
|
---|
[103a3626] | 249 | dprintf(USB_LOG_LEVEL_INFO, "releasing default address");
|
---|
[5097bed4] | 250 | opResult = usb_drv_release_default_address(hc);
|
---|
| 251 | if (opResult != EOK) {
|
---|
[103a3626] | 252 | dprintf(USB_LOG_LEVEL_WARNING, "failed to release default address");
|
---|
[5097bed4] | 253 | }
|
---|
| 254 | return opResult;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[e080332] | 257 | /**
|
---|
[f40a1e2] | 258 | * Reset the port with new device and reserve the default address.
|
---|
[e080332] | 259 | * @param hc
|
---|
| 260 | * @param port
|
---|
| 261 | * @param target
|
---|
| 262 | */
|
---|
[d81ef61c] | 263 | static void usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port) {
|
---|
[e080332] | 264 | usb_device_request_setup_packet_t request;
|
---|
| 265 | int opResult;
|
---|
[103a3626] | 266 | dprintf(USB_LOG_LEVEL_INFO, "some connection changed");
|
---|
[6bb83c7] | 267 | assert(hub->endpoints.control.hc_phone);
|
---|
[e080332] | 268 | //get default address
|
---|
[6bb83c7] | 269 | //opResult = usb_drv_reserve_default_address(hc);
|
---|
[62066b4] | 270 | opResult = usb_hc_reserve_default_address(&hub->connection, USB_SPEED_LOW);
|
---|
[e080332] | 271 | if (opResult != EOK) {
|
---|
[103a3626] | 272 | dprintf(USB_LOG_LEVEL_WARNING, "cannot assign default address, it is probably used");
|
---|
[e080332] | 273 | return;
|
---|
| 274 | }
|
---|
| 275 | //reset port
|
---|
| 276 | usb_hub_set_reset_port_request(&request, port);
|
---|
[6bb83c7] | 277 | opResult = usb_endpoint_pipe_control_write(
|
---|
| 278 | &hub->endpoints.control,
|
---|
| 279 | &request,sizeof(usb_device_request_setup_packet_t),
|
---|
[e080332] | 280 | NULL, 0
|
---|
| 281 | );
|
---|
| 282 | if (opResult != EOK) {
|
---|
[103a3626] | 283 | dprintf(USB_LOG_LEVEL_ERROR, "something went wrong when reseting a port");
|
---|
[6bb83c7] | 284 | //usb_hub_release_default_address(hc);
|
---|
| 285 | usb_hc_release_default_address(&hub->connection);
|
---|
[e080332] | 286 | }
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | /**
|
---|
[f40a1e2] | 290 | * Finalize adding new device after port reset
|
---|
[e080332] | 291 | * @param hc
|
---|
| 292 | * @param port
|
---|
| 293 | * @param target
|
---|
| 294 | */
|
---|
| 295 | static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
|
---|
[6bb83c7] | 296 | uint16_t port) {
|
---|
[e080332] | 297 |
|
---|
| 298 | int opResult;
|
---|
[103a3626] | 299 | dprintf(USB_LOG_LEVEL_INFO, "finalizing add device");
|
---|
[6bb83c7] | 300 | opResult = usb_hub_clear_port_feature(&hub->endpoints.control,
|
---|
[e080332] | 301 | port, USB_HUB_FEATURE_C_PORT_RESET);
|
---|
[6bb83c7] | 302 |
|
---|
[e080332] | 303 | if (opResult != EOK) {
|
---|
[103a3626] | 304 | dprintf(USB_LOG_LEVEL_ERROR, "failed to clear port reset feature");
|
---|
[6bb83c7] | 305 | usb_hc_release_default_address(&hub->connection);
|
---|
[e080332] | 306 | return;
|
---|
| 307 | }
|
---|
[6bb83c7] | 308 | //create connection to device
|
---|
| 309 | usb_endpoint_pipe_t new_device_pipe;
|
---|
| 310 | usb_device_connection_t new_device_connection;
|
---|
| 311 | usb_device_connection_initialize_on_default_address(
|
---|
| 312 | &new_device_connection,
|
---|
| 313 | &hub->connection
|
---|
| 314 | );
|
---|
| 315 | usb_endpoint_pipe_initialize_default_control(
|
---|
| 316 | &new_device_pipe,
|
---|
| 317 | &new_device_connection);
|
---|
| 318 | /// \TODO get highspeed info
|
---|
| 319 |
|
---|
| 320 |
|
---|
[e080332] | 321 |
|
---|
[6bb83c7] | 322 |
|
---|
| 323 |
|
---|
| 324 | /* Request address from host controller. */
|
---|
| 325 | usb_address_t new_device_address = usb_hc_request_address(
|
---|
| 326 | &hub->connection,
|
---|
[62066b4] | 327 | USB_SPEED_LOW/// \TODO fullspeed??
|
---|
[6bb83c7] | 328 | );
|
---|
[e080332] | 329 | if (new_device_address < 0) {
|
---|
[103a3626] | 330 | dprintf(USB_LOG_LEVEL_ERROR, "failed to get free USB address");
|
---|
[e080332] | 331 | opResult = new_device_address;
|
---|
[6bb83c7] | 332 | usb_hc_release_default_address(&hub->connection);
|
---|
[e080332] | 333 | return;
|
---|
| 334 | }
|
---|
[103a3626] | 335 | dprintf(USB_LOG_LEVEL_INFO, "setting new address %d",new_device_address);
|
---|
[6bb83c7] | 336 | //opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
|
---|
| 337 | // new_device_address);
|
---|
| 338 | opResult = usb_request_set_address(&new_device_pipe,new_device_address);
|
---|
[e080332] | 339 |
|
---|
| 340 | if (opResult != EOK) {
|
---|
[103a3626] | 341 | dprintf(USB_LOG_LEVEL_ERROR, "could not set address for new device");
|
---|
[6bb83c7] | 342 | usb_hc_release_default_address(&hub->connection);
|
---|
[e080332] | 343 | return;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 |
|
---|
[6bb83c7] | 347 | //opResult = usb_hub_release_default_address(hc);
|
---|
| 348 | opResult = usb_hc_release_default_address(&hub->connection);
|
---|
[e080332] | 349 | if(opResult!=EOK){
|
---|
| 350 | return;
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | devman_handle_t child_handle;
|
---|
[6bb83c7] | 354 | //??
|
---|
| 355 | opResult = usb_device_register_child_in_devman(new_device_address,
|
---|
| 356 | hub->connection.hc_handle, hub->device, &child_handle);
|
---|
| 357 |
|
---|
[e080332] | 358 | if (opResult != EOK) {
|
---|
[103a3626] | 359 | dprintf(USB_LOG_LEVEL_ERROR, "could not start driver for new device");
|
---|
[e080332] | 360 | return;
|
---|
| 361 | }
|
---|
[6bb83c7] | 362 | hub->attached_devs[port].handle = child_handle;
|
---|
[e080332] | 363 | hub->attached_devs[port].address = new_device_address;
|
---|
| 364 |
|
---|
[6bb83c7] | 365 | //opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
|
---|
| 366 | opResult = usb_hc_register_device(
|
---|
| 367 | &hub->connection,
|
---|
| 368 | &hub->attached_devs[port]);
|
---|
[e080332] | 369 | if (opResult != EOK) {
|
---|
[103a3626] | 370 | dprintf(USB_LOG_LEVEL_ERROR, "could not assign address of device in hcd");
|
---|
[e080332] | 371 | return;
|
---|
| 372 | }
|
---|
[103a3626] | 373 | dprintf(USB_LOG_LEVEL_INFO, "new device address %d, handle %zu",
|
---|
[e080332] | 374 | new_device_address, child_handle);
|
---|
| 375 |
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | /**
|
---|
[f40a1e2] | 379 | * Unregister device address in hc
|
---|
[e080332] | 380 | * @param hc
|
---|
| 381 | * @param port
|
---|
| 382 | * @param target
|
---|
| 383 | */
|
---|
| 384 | static void usb_hub_removed_device(
|
---|
[6bb83c7] | 385 | usb_hub_info_t * hub,uint16_t port) {
|
---|
[e080332] | 386 | //usb_device_request_setup_packet_t request;
|
---|
| 387 | int opResult;
|
---|
[5097bed4] | 388 |
|
---|
[f40a1e2] | 389 | /** \TODO remove device from device manager - not yet implemented in
|
---|
| 390 | * devide manager
|
---|
| 391 | */
|
---|
[6bb83c7] | 392 |
|
---|
[e080332] | 393 | //close address
|
---|
| 394 | if(hub->attached_devs[port].address!=0){
|
---|
[6bb83c7] | 395 | //opResult = usb_drv_release_address(hc,hub->attached_devs[port].address);
|
---|
| 396 | opResult = usb_hc_unregister_device(
|
---|
| 397 | &hub->connection, hub->attached_devs[port].address);
|
---|
[e080332] | 398 | if(opResult != EOK) {
|
---|
[103a3626] | 399 | dprintf(USB_LOG_LEVEL_WARNING, "could not release address of " \
|
---|
[0cfc68f0] | 400 | "removed device: %d", opResult);
|
---|
[e080332] | 401 | }
|
---|
| 402 | hub->attached_devs[port].address = 0;
|
---|
[6bb83c7] | 403 | hub->attached_devs[port].handle = 0;
|
---|
[e080332] | 404 | }else{
|
---|
[103a3626] | 405 | dprintf(USB_LOG_LEVEL_WARNING, "this is strange, disconnected device had no address");
|
---|
[e080332] | 406 | //device was disconnected before it`s port was reset - return default address
|
---|
[6bb83c7] | 407 | //usb_drv_release_default_address(hc);
|
---|
| 408 | usb_hc_release_default_address(&hub->connection);
|
---|
[e080332] | 409 | }
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | /**
|
---|
[f40a1e2] | 413 | * Process interrupts on given hub port
|
---|
[e080332] | 414 | * @param hc
|
---|
| 415 | * @param port
|
---|
| 416 | * @param target
|
---|
| 417 | */
|
---|
[d81ef61c] | 418 | static void usb_hub_process_interrupt(usb_hub_info_t * hub,
|
---|
| 419 | uint16_t port) {
|
---|
[103a3626] | 420 | dprintf(USB_LOG_LEVEL_DEBUG, "interrupt at port %d", port);
|
---|
[e080332] | 421 | //determine type of change
|
---|
[d81ef61c] | 422 | usb_endpoint_pipe_t *pipe = &hub->endpoints.control;
|
---|
[6bb83c7] | 423 | int opResult = usb_endpoint_pipe_start_session(pipe);
|
---|
| 424 |
|
---|
[d81ef61c] | 425 | if(opResult != EOK){
|
---|
[6bb83c7] | 426 | dprintf(USB_LOG_LEVEL_ERROR, "cannot open pipe %d", opResult);
|
---|
[d81ef61c] | 427 | }
|
---|
| 428 |
|
---|
| 429 | /*
|
---|
[e080332] | 430 | usb_target_t target;
|
---|
| 431 | target.address=address;
|
---|
| 432 | target.endpoint=0;
|
---|
[d81ef61c] | 433 | */
|
---|
| 434 |
|
---|
[e080332] | 435 | usb_port_status_t status;
|
---|
| 436 | size_t rcvd_size;
|
---|
| 437 | usb_device_request_setup_packet_t request;
|
---|
[d81ef61c] | 438 | //int opResult;
|
---|
[e080332] | 439 | usb_hub_set_port_status_request(&request, port);
|
---|
| 440 | //endpoint 0
|
---|
| 441 |
|
---|
[d81ef61c] | 442 | opResult = usb_endpoint_pipe_control_read(
|
---|
| 443 | pipe,
|
---|
| 444 | &request, sizeof(usb_device_request_setup_packet_t),
|
---|
[e080332] | 445 | &status, 4, &rcvd_size
|
---|
| 446 | );
|
---|
| 447 | if (opResult != EOK) {
|
---|
[103a3626] | 448 | dprintf(USB_LOG_LEVEL_ERROR, "ERROR: could not get port status");
|
---|
[e080332] | 449 | return;
|
---|
| 450 | }
|
---|
| 451 | if (rcvd_size != sizeof (usb_port_status_t)) {
|
---|
[103a3626] | 452 | dprintf(USB_LOG_LEVEL_ERROR, "ERROR: received status has incorrect size");
|
---|
[e080332] | 453 | return;
|
---|
| 454 | }
|
---|
| 455 | //something connected/disconnected
|
---|
| 456 | if (usb_port_connect_change(&status)) {
|
---|
[6bb83c7] | 457 | opResult = usb_hub_clear_port_feature(pipe,
|
---|
[e080332] | 458 | port, USB_HUB_FEATURE_C_PORT_CONNECTION);
|
---|
| 459 | // TODO: check opResult
|
---|
| 460 | if (usb_port_dev_connected(&status)) {
|
---|
[103a3626] | 461 | dprintf(USB_LOG_LEVEL_INFO, "some connection changed");
|
---|
[d81ef61c] | 462 | usb_hub_init_add_device(hub, port);
|
---|
[e080332] | 463 | } else {
|
---|
[d81ef61c] | 464 | usb_hub_removed_device(hub, port);
|
---|
[e080332] | 465 | }
|
---|
| 466 | }
|
---|
| 467 | //port reset
|
---|
| 468 | if (usb_port_reset_completed(&status)) {
|
---|
[103a3626] | 469 | dprintf(USB_LOG_LEVEL_INFO, "port reset complete");
|
---|
[e080332] | 470 | if (usb_port_enabled(&status)) {
|
---|
[d81ef61c] | 471 | usb_hub_finalize_add_device(hub, port);
|
---|
[e080332] | 472 | } else {
|
---|
[103a3626] | 473 | dprintf(USB_LOG_LEVEL_WARNING, "ERROR: port reset, but port still not enabled");
|
---|
[e080332] | 474 | }
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 | usb_port_set_connect_change(&status, false);
|
---|
| 478 | usb_port_set_reset(&status, false);
|
---|
| 479 | usb_port_set_reset_completed(&status, false);
|
---|
| 480 | usb_port_set_dev_connected(&status, false);
|
---|
[f40a1e2] | 481 | if (status>>16) {
|
---|
[103a3626] | 482 | dprintf(USB_LOG_LEVEL_INFO, "there was some unsupported change on port %d: %X",port,status);
|
---|
[f40a1e2] | 483 |
|
---|
[e080332] | 484 | }
|
---|
| 485 | /// \TODO handle other changes
|
---|
| 486 | /// \TODO debug log for various situations
|
---|
[6bb83c7] | 487 | usb_endpoint_pipe_end_session(pipe);
|
---|
[d81ef61c] | 488 |
|
---|
[e080332] | 489 |
|
---|
| 490 | }
|
---|
| 491 |
|
---|
[f40a1e2] | 492 | /**
|
---|
| 493 | * Check changes on all known hubs.
|
---|
[e080332] | 494 | */
|
---|
| 495 | void usb_hub_check_hub_changes(void) {
|
---|
| 496 | /*
|
---|
| 497 | * Iterate through all hubs.
|
---|
| 498 | */
|
---|
| 499 | usb_general_list_t * lst_item;
|
---|
[ba5ab09] | 500 | fibril_mutex_lock(&usb_hub_list_lock);
|
---|
[e080332] | 501 | for (lst_item = usb_hub_list.next;
|
---|
| 502 | lst_item != &usb_hub_list;
|
---|
| 503 | lst_item = lst_item->next) {
|
---|
[ba5ab09] | 504 | fibril_mutex_unlock(&usb_hub_list_lock);
|
---|
[e080332] | 505 | usb_hub_info_t * hub_info = ((usb_hub_info_t*)lst_item->data);
|
---|
[d81ef61c] | 506 | int opResult;
|
---|
| 507 |
|
---|
| 508 | opResult = usb_endpoint_pipe_start_session(&hub_info->endpoints.status_change);
|
---|
| 509 | if(opResult != EOK){
|
---|
| 510 | continue;
|
---|
| 511 | }
|
---|
[e080332] | 512 | /*
|
---|
| 513 | * Check status change pipe of this hub.
|
---|
| 514 | */
|
---|
[d81ef61c] | 515 | /*
|
---|
[e080332] | 516 | usb_target_t target;
|
---|
[28cb8bf7] | 517 | target.address = hub_info->address;
|
---|
[e080332] | 518 | target.endpoint = 1;/// \TODO get from endpoint descriptor
|
---|
[103a3626] | 519 | dprintf(USB_LOG_LEVEL_INFO, "checking changes for hub at addr %d",
|
---|
[0cfc68f0] | 520 | target.address);
|
---|
[d81ef61c] | 521 | */
|
---|
[e080332] | 522 | size_t port_count = hub_info->port_count;
|
---|
| 523 |
|
---|
| 524 | /*
|
---|
| 525 | * Connect to respective HC.
|
---|
[d81ef61c] | 526 | *
|
---|
[71ed4849] | 527 | int hc = usb_drv_hc_connect_auto(hub_info->device, 0);
|
---|
[e080332] | 528 | if (hc < 0) {
|
---|
| 529 | continue;
|
---|
[d81ef61c] | 530 | }*/
|
---|
[e080332] | 531 |
|
---|
| 532 | /// FIXME: count properly
|
---|
| 533 | size_t byte_length = ((port_count+1) / 8) + 1;
|
---|
| 534 |
|
---|
| 535 | void *change_bitmap = malloc(byte_length);
|
---|
| 536 | size_t actual_size;
|
---|
[6bb83c7] | 537 | //usb_handle_t handle;
|
---|
[e080332] | 538 |
|
---|
| 539 | /*
|
---|
| 540 | * Send the request.
|
---|
| 541 | */
|
---|
[d81ef61c] | 542 | opResult = usb_endpoint_pipe_read(
|
---|
| 543 | &hub_info->endpoints.status_change,
|
---|
[6bb83c7] | 544 | change_bitmap, byte_length, &actual_size
|
---|
[d81ef61c] | 545 | );
|
---|
[e080332] | 546 |
|
---|
[6bb83c7] | 547 | //usb_drv_async_wait_for(handle);
|
---|
[e080332] | 548 |
|
---|
| 549 | if (opResult != EOK) {
|
---|
[ba5ab09] | 550 | free(change_bitmap);
|
---|
[103a3626] | 551 | dprintf(USB_LOG_LEVEL_WARNING, "something went wrong while getting status of hub");
|
---|
[e080332] | 552 | continue;
|
---|
| 553 | }
|
---|
| 554 | unsigned int port;
|
---|
| 555 | for (port = 1; port < port_count+1; ++port) {
|
---|
| 556 | bool interrupt =
|
---|
| 557 | (((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2;
|
---|
| 558 | if (interrupt) {
|
---|
| 559 | usb_hub_process_interrupt(
|
---|
[d81ef61c] | 560 | hub_info, port);
|
---|
[e080332] | 561 | }
|
---|
| 562 | }
|
---|
[d81ef61c] | 563 | usb_endpoint_pipe_end_session(&hub_info->endpoints.status_change);
|
---|
[5097bed4] | 564 | free(change_bitmap);
|
---|
[d81ef61c] | 565 |
|
---|
[e080332] | 566 |
|
---|
[d81ef61c] | 567 | //async_hangup(hc);
|
---|
[ba5ab09] | 568 | fibril_mutex_lock(&usb_hub_list_lock);
|
---|
[e080332] | 569 | }
|
---|
[ba5ab09] | 570 | fibril_mutex_unlock(&usb_hub_list_lock);
|
---|
[e080332] | 571 | }
|
---|
| 572 |
|
---|
| 573 |
|
---|
| 574 |
|
---|
| 575 |
|
---|
| 576 |
|
---|
| 577 | /**
|
---|
| 578 | * @}
|
---|
[71ed4849] | 579 | */
|
---|