[dac43be] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Matus Dekanek
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libusb usb
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief Hub driver.
|
---|
| 34 | */
|
---|
[4317827] | 35 | #include <driver.h>
|
---|
[94c19b8] | 36 | #include <bool.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 |
|
---|
[dac43be] | 39 | #include <usbhc_iface.h>
|
---|
[4317827] | 40 | #include <usb/usbdrv.h>
|
---|
[dac43be] | 41 | #include <usb/descriptor.h>
|
---|
[94c19b8] | 42 | #include <usb/devreq.h>
|
---|
[dac43be] | 43 | #include <usb/classes/hub.h>
|
---|
[94c19b8] | 44 |
|
---|
[4317827] | 45 | #include "usbhub.h"
|
---|
[b5ec347] | 46 | #include "usbhub_private.h"
|
---|
| 47 | #include "port_status.h"
|
---|
[94c19b8] | 48 |
|
---|
[dac43be] | 49 |
|
---|
| 50 | size_t USB_HUB_MAX_DESCRIPTOR_SIZE = 71;
|
---|
| 51 |
|
---|
| 52 | //*********************************************
|
---|
| 53 | //
|
---|
| 54 | // various utils
|
---|
| 55 | //
|
---|
| 56 | //*********************************************
|
---|
| 57 |
|
---|
[b5ec347] | 58 | //hub descriptor utils
|
---|
[98d06b8] | 59 |
|
---|
[dac43be] | 60 | void * usb_serialize_hub_descriptor(usb_hub_descriptor_t * descriptor) {
|
---|
| 61 | //base size
|
---|
| 62 | size_t size = 7;
|
---|
| 63 | //variable size according to port count
|
---|
| 64 | size_t var_size = descriptor->ports_count / 8 + ((descriptor->ports_count % 8 > 0) ? 1 : 0);
|
---|
| 65 | size += 2 * var_size;
|
---|
| 66 | uint8_t * result = (uint8_t*) malloc(size);
|
---|
| 67 | //size
|
---|
| 68 | result[0] = size;
|
---|
| 69 | //descriptor type
|
---|
| 70 | result[1] = USB_DESCTYPE_HUB;
|
---|
| 71 | result[2] = descriptor->ports_count;
|
---|
| 72 | /// @fixme handling of endianness??
|
---|
| 73 | result[3] = descriptor->hub_characteristics / 256;
|
---|
| 74 | result[4] = descriptor->hub_characteristics % 256;
|
---|
| 75 | result[5] = descriptor->pwr_on_2_good_time;
|
---|
| 76 | result[6] = descriptor->current_requirement;
|
---|
| 77 |
|
---|
| 78 | size_t i;
|
---|
| 79 | for (i = 0; i < var_size; ++i) {
|
---|
| 80 | result[7 + i] = descriptor->devices_removable[i];
|
---|
| 81 | }
|
---|
| 82 | for (i = 0; i < var_size; ++i) {
|
---|
| 83 | result[7 + var_size + i] = 255;
|
---|
| 84 | }
|
---|
| 85 | return result;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | usb_hub_descriptor_t * usb_deserialize_hub_desriptor(void * serialized_descriptor) {
|
---|
| 89 | uint8_t * sdescriptor = (uint8_t*) serialized_descriptor;
|
---|
[10096231] | 90 |
|
---|
| 91 | if (sdescriptor[1] != USB_DESCTYPE_HUB) {
|
---|
| 92 | printf("[usb_hub] wrong descriptor %x\n",sdescriptor[1]);
|
---|
| 93 | return NULL;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[b5ec347] | 96 | usb_hub_descriptor_t * result = usb_new(usb_hub_descriptor_t);
|
---|
[10096231] | 97 |
|
---|
| 98 |
|
---|
[dac43be] | 99 | result->ports_count = sdescriptor[2];
|
---|
| 100 | /// @fixme handling of endianness??
|
---|
| 101 | result->hub_characteristics = sdescriptor[4] + 256 * sdescriptor[3];
|
---|
| 102 | result->pwr_on_2_good_time = sdescriptor[5];
|
---|
| 103 | result->current_requirement = sdescriptor[6];
|
---|
| 104 | size_t var_size = result->ports_count / 8 + ((result->ports_count % 8 > 0) ? 1 : 0);
|
---|
| 105 | result->devices_removable = (uint8_t*) malloc(var_size);
|
---|
[10096231] | 106 | //printf("[usb_hub] getting removable devices data \n");
|
---|
[dac43be] | 107 | size_t i;
|
---|
| 108 | for (i = 0; i < var_size; ++i) {
|
---|
| 109 | result->devices_removable[i] = sdescriptor[7 + i];
|
---|
| 110 | }
|
---|
| 111 | return result;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[b5ec347] | 114 | //control transactions
|
---|
[98d06b8] | 115 |
|
---|
[b5ec347] | 116 | int usb_drv_sync_control_read(
|
---|
[98d06b8] | 117 | int phone, usb_target_t target,
|
---|
| 118 | usb_device_request_setup_packet_t * request,
|
---|
| 119 | void * rcvd_buffer, size_t rcvd_size, size_t * actual_size
|
---|
| 120 | ) {
|
---|
[b5ec347] | 121 | usb_handle_t handle;
|
---|
| 122 | int opResult;
|
---|
| 123 | //setup
|
---|
| 124 | opResult = usb_drv_async_control_read_setup(phone, target,
|
---|
[98d06b8] | 125 | request, sizeof (usb_device_request_setup_packet_t),
|
---|
| 126 | &handle);
|
---|
| 127 | if (opResult != EOK) {
|
---|
[b5ec347] | 128 | return opResult;
|
---|
| 129 | }
|
---|
| 130 | opResult = usb_drv_async_wait_for(handle);
|
---|
[98d06b8] | 131 | if (opResult != EOK) {
|
---|
[b5ec347] | 132 | return opResult;
|
---|
| 133 | }
|
---|
| 134 | //read
|
---|
| 135 | opResult = usb_drv_async_control_read_data(phone, target,
|
---|
[98d06b8] | 136 | rcvd_buffer, rcvd_size, actual_size,
|
---|
| 137 | &handle);
|
---|
| 138 | if (opResult != EOK) {
|
---|
[b5ec347] | 139 | return opResult;
|
---|
| 140 | }
|
---|
| 141 | opResult = usb_drv_async_wait_for(handle);
|
---|
[98d06b8] | 142 | if (opResult != EOK) {
|
---|
[b5ec347] | 143 | return opResult;
|
---|
| 144 | }
|
---|
| 145 | //finalize
|
---|
| 146 | opResult = usb_drv_async_control_read_status(phone, target,
|
---|
[98d06b8] | 147 | &handle);
|
---|
| 148 | if (opResult != EOK) {
|
---|
[b5ec347] | 149 | return opResult;
|
---|
| 150 | }
|
---|
| 151 | opResult = usb_drv_async_wait_for(handle);
|
---|
[98d06b8] | 152 | if (opResult != EOK) {
|
---|
[b5ec347] | 153 | return opResult;
|
---|
| 154 | }
|
---|
| 155 | return EOK;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | int usb_drv_sync_control_write(
|
---|
[98d06b8] | 159 | int phone, usb_target_t target,
|
---|
| 160 | usb_device_request_setup_packet_t * request,
|
---|
| 161 | void * sent_buffer, size_t sent_size
|
---|
| 162 | ) {
|
---|
[b5ec347] | 163 | usb_handle_t handle;
|
---|
| 164 | int opResult;
|
---|
| 165 | //setup
|
---|
| 166 | opResult = usb_drv_async_control_write_setup(phone, target,
|
---|
[98d06b8] | 167 | request, sizeof (usb_device_request_setup_packet_t),
|
---|
| 168 | &handle);
|
---|
| 169 | if (opResult != EOK) {
|
---|
[b5ec347] | 170 | return opResult;
|
---|
| 171 | }
|
---|
| 172 | opResult = usb_drv_async_wait_for(handle);
|
---|
[98d06b8] | 173 | if (opResult != EOK) {
|
---|
[b5ec347] | 174 | return opResult;
|
---|
| 175 | }
|
---|
| 176 | //write
|
---|
| 177 | opResult = usb_drv_async_control_write_data(phone, target,
|
---|
[98d06b8] | 178 | sent_buffer, sent_size,
|
---|
| 179 | &handle);
|
---|
| 180 | if (opResult != EOK) {
|
---|
[b5ec347] | 181 | return opResult;
|
---|
| 182 | }
|
---|
| 183 | opResult = usb_drv_async_wait_for(handle);
|
---|
[98d06b8] | 184 | if (opResult != EOK) {
|
---|
[b5ec347] | 185 | return opResult;
|
---|
| 186 | }
|
---|
| 187 | //finalize
|
---|
| 188 | opResult = usb_drv_async_control_write_status(phone, target,
|
---|
[98d06b8] | 189 | &handle);
|
---|
| 190 | if (opResult != EOK) {
|
---|
[b5ec347] | 191 | return opResult;
|
---|
| 192 | }
|
---|
| 193 | opResult = usb_drv_async_wait_for(handle);
|
---|
[98d06b8] | 194 | if (opResult != EOK) {
|
---|
[b5ec347] | 195 | return opResult;
|
---|
| 196 | }
|
---|
| 197 | return EOK;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | //list implementation
|
---|
| 201 |
|
---|
[98d06b8] | 202 | usb_general_list_t * usb_lst_create(void) {
|
---|
[b5ec347] | 203 | usb_general_list_t* result = usb_new(usb_general_list_t);
|
---|
| 204 | usb_lst_init(result);
|
---|
| 205 | return result;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[98d06b8] | 208 | void usb_lst_init(usb_general_list_t * lst) {
|
---|
[b5ec347] | 209 | lst->prev = lst;
|
---|
| 210 | lst->next = lst;
|
---|
| 211 | lst->data = NULL;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[98d06b8] | 214 | void usb_lst_prepend(usb_general_list_t* item, void* data) {
|
---|
[b5ec347] | 215 | usb_general_list_t* appended = usb_new(usb_general_list_t);
|
---|
[98d06b8] | 216 | appended->data = data;
|
---|
| 217 | appended->next = item;
|
---|
| 218 | appended->prev = item->prev;
|
---|
[b5ec347] | 219 | item->prev->next = appended;
|
---|
| 220 | item->prev = appended;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[98d06b8] | 223 | void usb_lst_append(usb_general_list_t* item, void* data) {
|
---|
| 224 | usb_general_list_t* appended = usb_new(usb_general_list_t);
|
---|
| 225 | appended->data = data;
|
---|
| 226 | appended->next = item->next;
|
---|
| 227 | appended->prev = item;
|
---|
[b5ec347] | 228 | item->next->prev = appended;
|
---|
| 229 | item->next = appended;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[98d06b8] | 232 | void usb_lst_remove(usb_general_list_t* item) {
|
---|
[b5ec347] | 233 | item->next->prev = item->prev;
|
---|
| 234 | item->prev->next = item->next;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[98d06b8] | 237 | static void usb_hub_test_port_status(void) {
|
---|
| 238 | printf("[usb_hub] -------------port status test---------\n");
|
---|
| 239 | usb_port_status_t status = 0;
|
---|
| 240 |
|
---|
| 241 | //printf("original status %d (should be 0)\n",(uint32_t)status);
|
---|
| 242 | usb_port_set_bit(&status, 1, 1);
|
---|
| 243 | //printf("%d =?= 2\n",(uint32_t)status);
|
---|
| 244 | if (status != 2) {
|
---|
| 245 | printf("[usb_port_status] test failed: wrong set of bit 1\n");
|
---|
| 246 | return;
|
---|
| 247 | }
|
---|
| 248 | usb_port_set_bit(&status, 3, 1);
|
---|
| 249 | if (status != 10) {
|
---|
| 250 | printf("[usb_port_status] test failed: wrong set of bit 3\n");
|
---|
| 251 | return;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | usb_port_set_bit(&status, 15, 1);
|
---|
| 255 | if (status != 10 + (1 << 15)) {
|
---|
| 256 | printf("[usb_port_status] test failed: wrong set of bit 15\n");
|
---|
| 257 | return;
|
---|
| 258 | }
|
---|
| 259 | usb_port_set_bit(&status, 1, 0);
|
---|
| 260 | if (status != 8 + (1 << 15)) {
|
---|
| 261 | printf("[usb_port_status] test failed: wrong unset of bit 1\n");
|
---|
| 262 | return;
|
---|
| 263 | }
|
---|
| 264 | int i;
|
---|
| 265 | for (i = 0; i < 32; ++i) {
|
---|
| 266 | if (i == 3 || i == 15) {
|
---|
| 267 | if (!usb_port_get_bit(&status, i)) {
|
---|
| 268 | printf("[usb_port_status] test failed: wrong bit at %d\n", i);
|
---|
| 269 | }
|
---|
| 270 | } else {
|
---|
| 271 | if (usb_port_get_bit(&status, i)) {
|
---|
| 272 | printf("[usb_port_status] test failed: wrong bit at %d\n", i);
|
---|
| 273 | }
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | printf("test ok\n");
|
---|
| 278 |
|
---|
| 279 |
|
---|
| 280 | //printf("%d =?= 10\n",(uint32_t)status);
|
---|
| 281 |
|
---|
| 282 | //printf("this should be 0: %d \n",usb_port_get_bit(&status,0));
|
---|
| 283 | //printf("this should be 1: %d \n",usb_port_get_bit(&status,1));
|
---|
| 284 | //printf("this should be 0: %d \n",usb_port_get_bit(&status,2));
|
---|
| 285 | //printf("this should be 1: %d \n",usb_port_get_bit(&status,3));
|
---|
| 286 | //printf("this should be 0: %d \n",usb_port_get_bit(&status,4));
|
---|
| 287 |
|
---|
| 288 |
|
---|
| 289 |
|
---|
[b5ec347] | 290 |
|
---|
[98d06b8] | 291 | }
|
---|
[dac43be] | 292 |
|
---|
| 293 | //*********************************************
|
---|
| 294 | //
|
---|
[98d06b8] | 295 | // hub driver code, initialization
|
---|
[dac43be] | 296 | //
|
---|
| 297 | //*********************************************
|
---|
| 298 |
|
---|
[98d06b8] | 299 | usb_hub_info_t * usb_create_hub_info(device_t * device, int hc) {
|
---|
[b5ec347] | 300 | usb_hub_info_t* result = usb_new(usb_hub_info_t);
|
---|
| 301 | //result->device = device;
|
---|
| 302 | result->port_count = -1;
|
---|
[94c19b8] | 303 | /// \TODO is this correct? is the device stored?
|
---|
| 304 | result->device = device;
|
---|
[b5ec347] | 305 |
|
---|
[98d06b8] | 306 |
|
---|
[10096231] | 307 | //printf("[usb_hub] phone to hc = %d\n", hc);
|
---|
[b5ec347] | 308 | if (hc < 0) {
|
---|
| 309 | return result;
|
---|
| 310 | }
|
---|
| 311 | //get some hub info
|
---|
[98d06b8] | 312 | usb_address_t addr = usb_drv_get_my_address(hc, device);
|
---|
| 313 | printf("[usb_hub] addres of newly created hub = %d\n", addr);
|
---|
[b5ec347] | 314 | /*if(addr<0){
|
---|
| 315 | //return result;
|
---|
| 316 |
|
---|
| 317 | }*/
|
---|
[98d06b8] | 318 |
|
---|
[94c19b8] | 319 | result->usb_device = usb_new(usb_hcd_attached_device_info_t);
|
---|
| 320 | result->usb_device->address = addr;
|
---|
[98d06b8] | 321 |
|
---|
| 322 | // get hub descriptor
|
---|
| 323 | usb_target_t target;
|
---|
| 324 | target.address = addr;
|
---|
| 325 | target.endpoint = 0;
|
---|
| 326 | usb_device_request_setup_packet_t request;
|
---|
[10096231] | 327 | //printf("[usb_hub] creating descriptor request\n");
|
---|
| 328 | usb_hub_set_descriptor_request(&request);
|
---|
| 329 |
|
---|
| 330 | //printf("[usb_hub] creating serialized descriptor\n");
|
---|
[98d06b8] | 331 | void * serialized_descriptor = malloc(USB_HUB_MAX_DESCRIPTOR_SIZE);
|
---|
| 332 | usb_hub_descriptor_t * descriptor;
|
---|
| 333 | size_t received_size;
|
---|
| 334 | int opResult;
|
---|
[10096231] | 335 | //printf("[usb_hub] starting control transaction\n");
|
---|
[98d06b8] | 336 | opResult = usb_drv_sync_control_read(
|
---|
| 337 | hc, target, &request, serialized_descriptor,
|
---|
| 338 | USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
|
---|
| 339 | if (opResult != EOK) {
|
---|
[10096231] | 340 | printf("[usb_hub] failed when receiving hub descriptor, badcode = %d\n",opResult);
|
---|
| 341 | ///\TODO memory leak will occur here!
|
---|
| 342 | return result;
|
---|
[98d06b8] | 343 | }
|
---|
[10096231] | 344 | //printf("[usb_hub] deserializing descriptor\n");
|
---|
[98d06b8] | 345 | descriptor = usb_deserialize_hub_desriptor(serialized_descriptor);
|
---|
[10096231] | 346 | if(descriptor==NULL){
|
---|
| 347 | printf("[usb_hub] could not deserialize descriptor \n");
|
---|
| 348 | result->port_count = 1;///\TODO this code is only for debug!!!
|
---|
| 349 | return result;
|
---|
| 350 | }
|
---|
| 351 | //printf("[usb_hub] setting port count to %d\n",descriptor->ports_count);
|
---|
[98d06b8] | 352 | result->port_count = descriptor->ports_count;
|
---|
[10096231] | 353 | //printf("[usb_hub] freeing data\n");
|
---|
[98d06b8] | 354 | free(serialized_descriptor);
|
---|
[10096231] | 355 | free(descriptor->devices_removable);
|
---|
[98d06b8] | 356 | free(descriptor);
|
---|
| 357 |
|
---|
| 358 | //finish
|
---|
| 359 |
|
---|
[b5ec347] | 360 | printf("[usb_hub] hub info created\n");
|
---|
[dac43be] | 361 |
|
---|
| 362 | return result;
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | int usb_add_hub_device(device_t *dev) {
|
---|
[4317827] | 366 | printf(NAME ": add_hub_device(handle=%d)\n", (int) dev->handle);
|
---|
[b5ec347] | 367 | printf("[usb_hub] hub device\n");
|
---|
[dac43be] | 368 |
|
---|
| 369 | /*
|
---|
| 370 | * We are some (probably deeply nested) hub.
|
---|
| 371 | * Thus, assign our own operations and explore already
|
---|
| 372 | * connected devices.
|
---|
| 373 | */
|
---|
| 374 |
|
---|
| 375 | //create the hub structure
|
---|
[98d06b8] | 376 | //get hc connection
|
---|
| 377 | /// \TODO correct params
|
---|
[10096231] | 378 | int hc = usb_drv_hc_connect(dev, 0);
|
---|
[98d06b8] | 379 |
|
---|
| 380 | usb_hub_info_t * hub_info = usb_create_hub_info(dev, hc);
|
---|
| 381 | int port;
|
---|
| 382 | int opResult;
|
---|
| 383 | usb_device_request_setup_packet_t request;
|
---|
| 384 | usb_target_t target;
|
---|
[94c19b8] | 385 | target.address = hub_info->usb_device->address;
|
---|
[98d06b8] | 386 | target.endpoint = 0;
|
---|
| 387 | for (port = 0; port < hub_info->port_count; ++port) {
|
---|
| 388 | usb_hub_set_power_port_request(&request, port);
|
---|
| 389 | opResult = usb_drv_sync_control_write(hc, target, &request, NULL, 0);
|
---|
| 390 | if (opResult != EOK) {
|
---|
| 391 | printf("[usb_hub]something went wrong when setting hub`s %dth port\n", port);
|
---|
| 392 | }
|
---|
| 393 | }
|
---|
| 394 | //ports powered, hub seems to be enabled
|
---|
| 395 |
|
---|
| 396 | ipc_hangup(hc);
|
---|
| 397 |
|
---|
| 398 | //add the hub to list
|
---|
[b5ec347] | 399 | usb_lst_append(&usb_hub_list, hub_info);
|
---|
| 400 | printf("[usb_hub] hub info added to list\n");
|
---|
| 401 | //(void)hub_info;
|
---|
[4c74ac3] | 402 | usb_hub_check_hub_changes();
|
---|
[98d06b8] | 403 |
|
---|
| 404 | /// \TODO start the check loop, if not already started...
|
---|
| 405 |
|
---|
| 406 | //this is just a test for port status bitmap type
|
---|
| 407 | usb_hub_test_port_status();
|
---|
| 408 |
|
---|
[b5ec347] | 409 | printf("[usb_hub] hub dev added\n");
|
---|
[4c74ac3] | 410 | printf("\taddress %d, has %d ports \n",
|
---|
| 411 | hub_info->usb_device->address,
|
---|
| 412 | hub_info->port_count);
|
---|
[dac43be] | 413 |
|
---|
| 414 | return EOK;
|
---|
| 415 | //return ENOTSUP;
|
---|
| 416 | }
|
---|
| 417 |
|
---|
[98d06b8] | 418 | //*********************************************
|
---|
| 419 | //
|
---|
| 420 | // hub driver code, main loop
|
---|
| 421 | //
|
---|
| 422 | //*********************************************
|
---|
| 423 |
|
---|
| 424 | /**
|
---|
| 425 | * reset the port with new device and reserve the default address
|
---|
| 426 | * @param hc
|
---|
| 427 | * @param port
|
---|
| 428 | * @param target
|
---|
| 429 | */
|
---|
| 430 | static void usb_hub_init_add_device(int hc, uint16_t port, usb_target_t target) {
|
---|
| 431 | usb_device_request_setup_packet_t request;
|
---|
| 432 | int opResult;
|
---|
| 433 | printf("[usb_hub] some connection changed\n");
|
---|
[10096231] | 434 |
|
---|
[98d06b8] | 435 | opResult = usb_drv_reserve_default_address(hc);
|
---|
| 436 | if (opResult != EOK) {
|
---|
| 437 | printf("[usb_hub] cannot assign default address, it is probably used\n");
|
---|
[10096231] | 438 | return;
|
---|
[98d06b8] | 439 | }
|
---|
| 440 | //reset port
|
---|
| 441 | usb_hub_set_reset_port_request(&request, port);
|
---|
| 442 | opResult = usb_drv_sync_control_write(
|
---|
| 443 | hc, target,
|
---|
| 444 | &request,
|
---|
| 445 | NULL, 0
|
---|
| 446 | );
|
---|
| 447 | if (opResult != EOK) {
|
---|
| 448 | //continue;
|
---|
| 449 | printf("[usb_hub] something went wrong when reseting a port\n");
|
---|
| 450 | }
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 | /**
|
---|
| 454 | * finalize adding new device after port reset
|
---|
| 455 | * @param hc
|
---|
| 456 | * @param port
|
---|
| 457 | * @param target
|
---|
| 458 | */
|
---|
[4c74ac3] | 459 | static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
|
---|
[98d06b8] | 460 | int hc, uint16_t port, usb_target_t target) {
|
---|
| 461 |
|
---|
| 462 | usb_device_request_setup_packet_t request;
|
---|
| 463 | int opResult;
|
---|
| 464 | printf("[usb_hub] finalizing add device\n");
|
---|
| 465 | usb_address_t new_device_address =
|
---|
| 466 | usb_drv_request_address(hc);
|
---|
| 467 | usb_hub_set_set_address_request
|
---|
| 468 | (&request, new_device_address);
|
---|
| 469 | opResult = usb_drv_sync_control_write(
|
---|
| 470 | hc, target,
|
---|
| 471 | &request,
|
---|
| 472 | NULL, 0
|
---|
| 473 | );
|
---|
| 474 | if (opResult != EOK) {
|
---|
| 475 | printf("[usb_hub] could not set address for new device\n");
|
---|
[10096231] | 476 | //will retry later...
|
---|
| 477 | return;
|
---|
[98d06b8] | 478 | }
|
---|
[94c19b8] | 479 |
|
---|
| 480 |
|
---|
[98d06b8] | 481 | usb_drv_release_default_address(hc);
|
---|
| 482 |
|
---|
[4c74ac3] | 483 | devman_handle_t child_handle;
|
---|
| 484 | opResult = usb_drv_register_child_in_devman(hc, hub->device,
|
---|
| 485 | new_device_address, &child_handle);
|
---|
| 486 | if (opResult != EOK) {
|
---|
| 487 | printf("[usb_hub] could not start driver for new device \n");
|
---|
| 488 | return;
|
---|
| 489 | }
|
---|
| 490 | usb_drv_bind_address(hc, new_device_address, child_handle);
|
---|
| 491 |
|
---|
[98d06b8] | 492 | }
|
---|
| 493 |
|
---|
| 494 | /**
|
---|
| 495 | * unregister device address in hc, close the port
|
---|
| 496 | * @param hc
|
---|
| 497 | * @param port
|
---|
| 498 | * @param target
|
---|
| 499 | */
|
---|
| 500 | static void usb_hub_removed_device(int hc, uint16_t port, usb_target_t target) {
|
---|
| 501 | usb_device_request_setup_packet_t request;
|
---|
| 502 | int opResult;
|
---|
| 503 | //disable port
|
---|
| 504 | usb_hub_set_disable_port_request(&request, port);
|
---|
| 505 | opResult = usb_drv_sync_control_write(
|
---|
| 506 | hc, target,
|
---|
| 507 | &request,
|
---|
| 508 | NULL, 0
|
---|
| 509 | );
|
---|
| 510 | if (opResult != EOK) {
|
---|
| 511 | //continue;
|
---|
| 512 | printf("[usb_hub] something went wrong when disabling a port\n");
|
---|
| 513 | }
|
---|
| 514 | //remove device
|
---|
| 515 | //close address
|
---|
| 516 | //
|
---|
| 517 |
|
---|
| 518 | ///\TODO this code is not complete
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | /**
|
---|
| 522 | * process interrupts on given hub port
|
---|
| 523 | * @param hc
|
---|
| 524 | * @param port
|
---|
| 525 | * @param target
|
---|
| 526 | */
|
---|
[4c74ac3] | 527 | static void usb_hub_process_interrupt(usb_hub_info_t * hub, int hc, uint16_t port, usb_target_t target) {
|
---|
[98d06b8] | 528 | printf("[usb_hub] interrupt at port %d\n", port);
|
---|
| 529 | //determine type of change
|
---|
| 530 | usb_port_status_t status;
|
---|
| 531 | size_t rcvd_size;
|
---|
| 532 | usb_device_request_setup_packet_t request;
|
---|
| 533 | int opResult;
|
---|
| 534 | usb_hub_set_port_status_request(&request, port);
|
---|
| 535 |
|
---|
| 536 | opResult = usb_drv_sync_control_read(
|
---|
| 537 | hc, target,
|
---|
| 538 | &request,
|
---|
| 539 | &status, 4, &rcvd_size
|
---|
| 540 | );
|
---|
| 541 | if (opResult != EOK) {
|
---|
| 542 | printf("[usb_hub] ERROR: could not get port status\n");
|
---|
| 543 | return;
|
---|
| 544 | }
|
---|
| 545 | if (rcvd_size != sizeof (usb_port_status_t)) {
|
---|
| 546 | printf("[usb_hub] ERROR: received status has incorrect size\n");
|
---|
| 547 | return;
|
---|
| 548 | }
|
---|
| 549 | //something connected/disconnected
|
---|
| 550 | if (usb_port_connect_change(&status)) {
|
---|
| 551 | if (usb_port_dev_connected(&status)) {
|
---|
| 552 | printf("[usb_hub] some connection changed\n");
|
---|
| 553 | usb_hub_init_add_device(hc, port, target);
|
---|
| 554 | } else {
|
---|
| 555 | usb_hub_removed_device(hc, port, target);
|
---|
| 556 | }
|
---|
| 557 | }
|
---|
| 558 | //port reset
|
---|
| 559 | if (usb_port_reset_completed(&status)) {
|
---|
| 560 | printf("[usb_hub] finalizing add device\n");
|
---|
| 561 | if (usb_port_enabled(&status)) {
|
---|
[4c74ac3] | 562 | usb_hub_finalize_add_device(hub, hc, port, target);
|
---|
[98d06b8] | 563 | } else {
|
---|
| 564 | printf("[usb_hub] ERROR: port reset, but port still not enabled\n");
|
---|
| 565 | }
|
---|
| 566 | }
|
---|
| 567 |
|
---|
| 568 | usb_port_set_connect_change(&status, false);
|
---|
| 569 | usb_port_set_reset(&status, false);
|
---|
| 570 | usb_port_set_reset_completed(&status, false);
|
---|
| 571 | usb_port_set_dev_connected(&status, false);
|
---|
| 572 | if (status) {
|
---|
| 573 | printf("[usb_hub]there was some unsupported change on port\n");
|
---|
| 574 | }
|
---|
| 575 | /// \TODO handle other changes
|
---|
| 576 | /// \TODO debug log for various situations
|
---|
[dac43be] | 577 |
|
---|
[b5ec347] | 578 |
|
---|
| 579 |
|
---|
[98d06b8] | 580 | /*
|
---|
| 581 | //configure device
|
---|
| 582 | usb_drv_reserve_default_address(hc);
|
---|
| 583 |
|
---|
| 584 | usb_address_t new_device_address = usb_drv_request_address(hc);
|
---|
| 585 |
|
---|
| 586 |
|
---|
| 587 | usb_drv_release_default_address(hc);
|
---|
| 588 | * */
|
---|
| 589 | }
|
---|
| 590 |
|
---|
[dac43be] | 591 | /** Check changes on all known hubs.
|
---|
| 592 | */
|
---|
[4c74ac3] | 593 | void usb_hub_check_hub_changes(void) {
|
---|
[dac43be] | 594 | /*
|
---|
[4317827] | 595 | * Iterate through all hubs.
|
---|
[dac43be] | 596 | */
|
---|
[b5ec347] | 597 | usb_general_list_t * lst_item;
|
---|
| 598 | for (lst_item = usb_hub_list.next;
|
---|
[98d06b8] | 599 | lst_item != &usb_hub_list;
|
---|
| 600 | lst_item = lst_item->next) {
|
---|
[b5ec347] | 601 | printf("[usb_hub] checking hub changes\n");
|
---|
[94c19b8] | 602 | usb_hub_info_t * hub_info = ((usb_hub_info_t*)lst_item->data);
|
---|
[4317827] | 603 | /*
|
---|
| 604 | * Check status change pipe of this hub.
|
---|
| 605 | */
|
---|
[98d06b8] | 606 |
|
---|
[94c19b8] | 607 | usb_target_t target;
|
---|
| 608 | target.address = hub_info->usb_device->address;
|
---|
| 609 | target.endpoint = 1;
|
---|
[4317827] | 610 |
|
---|
[94c19b8] | 611 | size_t port_count = hub_info->port_count;
|
---|
[4317827] | 612 |
|
---|
[dac43be] | 613 | /*
|
---|
[4317827] | 614 | * Connect to respective HC.
|
---|
[dac43be] | 615 | */
|
---|
[94c19b8] | 616 | int hc = usb_drv_hc_connect(hub_info->device, 0);
|
---|
[4317827] | 617 | if (hc < 0) {
|
---|
| 618 | continue;
|
---|
[dac43be] | 619 | }
|
---|
[4317827] | 620 |
|
---|
| 621 | // FIXME: count properly
|
---|
| 622 | size_t byte_length = (port_count / 8) + 1;
|
---|
| 623 |
|
---|
| 624 | void *change_bitmap = malloc(byte_length);
|
---|
| 625 | size_t actual_size;
|
---|
| 626 | usb_handle_t handle;
|
---|
| 627 |
|
---|
| 628 | /*
|
---|
| 629 | * Send the request.
|
---|
| 630 | */
|
---|
[b5ec347] | 631 | int opResult = usb_drv_async_interrupt_in(hc, target,
|
---|
[4317827] | 632 | change_bitmap, byte_length, &actual_size,
|
---|
| 633 | &handle);
|
---|
| 634 |
|
---|
| 635 | usb_drv_async_wait_for(handle);
|
---|
| 636 |
|
---|
[98d06b8] | 637 | if (opResult != EOK) {
|
---|
[b5ec347] | 638 | printf("[usb_hub] something went wrong while getting status of hub\n");
|
---|
| 639 | continue;
|
---|
| 640 | }
|
---|
| 641 | unsigned int port;
|
---|
[98d06b8] | 642 | for (port = 0; port < port_count; ++port) {
|
---|
| 643 | bool interrupt = (((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2;
|
---|
| 644 | if (interrupt) {
|
---|
[4c74ac3] | 645 | usb_hub_process_interrupt(hub_info, hc, port, target);
|
---|
[b5ec347] | 646 | }
|
---|
| 647 | }
|
---|
| 648 |
|
---|
| 649 |
|
---|
[4317827] | 650 | /*
|
---|
| 651 | * TODO: handle the changes.
|
---|
| 652 | */
|
---|
| 653 |
|
---|
[9ca0013] | 654 | /*
|
---|
| 655 | * WARNING: sample code, will not work out of the box.
|
---|
| 656 | * And does not contain code for checking for errors.
|
---|
| 657 | */
|
---|
| 658 | #if 0
|
---|
| 659 | /*
|
---|
| 660 | * Before opening the port, we must acquire the default
|
---|
| 661 | * address.
|
---|
| 662 | */
|
---|
| 663 | usb_drv_reserve_default_address(hc);
|
---|
| 664 |
|
---|
| 665 | usb_address_t new_device_address = usb_drv_request_address(hc);
|
---|
| 666 |
|
---|
| 667 | // TODO: open the port
|
---|
| 668 |
|
---|
| 669 | // TODO: send request for setting address to new_device_address
|
---|
| 670 |
|
---|
| 671 | /*
|
---|
| 672 | * Once new address is set, we can release the default
|
---|
| 673 | * address.
|
---|
| 674 | */
|
---|
| 675 | usb_drv_release_default_address(hc);
|
---|
| 676 |
|
---|
| 677 | /*
|
---|
| 678 | * Obtain descriptors and create match ids for devman.
|
---|
| 679 | */
|
---|
| 680 |
|
---|
| 681 | // TODO: get device descriptors
|
---|
| 682 |
|
---|
| 683 | // TODO: create match ids
|
---|
| 684 |
|
---|
| 685 | // TODO: add child device
|
---|
| 686 |
|
---|
| 687 | // child_device_register sets the device handle
|
---|
| 688 | // TODO: store it here
|
---|
| 689 | devman_handle_t new_device_handle = 0;
|
---|
| 690 |
|
---|
| 691 | /*
|
---|
| 692 | * Inform the HC that the new device has devman handle
|
---|
| 693 | * assigned.
|
---|
| 694 | */
|
---|
| 695 | usb_drv_bind_address(hc, new_device_address, new_device_handle);
|
---|
| 696 |
|
---|
| 697 | /*
|
---|
| 698 | * That's all.
|
---|
| 699 | */
|
---|
| 700 | #endif
|
---|
| 701 |
|
---|
[4317827] | 702 |
|
---|
| 703 | /*
|
---|
| 704 | * Hang-up the HC-connected phone.
|
---|
| 705 | */
|
---|
| 706 | ipc_hangup(hc);
|
---|
[dac43be] | 707 | }
|
---|
| 708 | }
|
---|
| 709 |
|
---|
| 710 | /**
|
---|
| 711 | * @}
|
---|
| 712 | */
|
---|