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