Changes in uspace/drv/usbhub/utils.c [ea5dbaf:ee0d8a8] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/usbhub/utils.c
rea5dbaf ree0d8a8 34 34 */ 35 35 #include <driver.h> 36 #include <bool.h> 37 #include <errno.h> 38 36 #include <usb/devreq.h> 39 37 #include <usbhc_iface.h> 40 38 #include <usb/usbdrv.h> 41 39 #include <usb/descriptor.h> 42 #include <usb/devreq.h> 40 #include <driver.h> 41 #include <bool.h> 42 #include <errno.h> 43 43 #include <usb/classes/hub.h> 44 45 44 #include "usbhub.h" 46 45 #include "usbhub_private.h" 47 46 #include "port_status.h" 48 47 #include <usb/devreq.h> 48 49 static void check_hub_changes(void); 49 50 50 51 size_t USB_HUB_MAX_DESCRIPTOR_SIZE = 71; … … 301 302 //result->device = device; 302 303 result->port_count = -1; 303 /// \TODO is this correct? is the device stored?304 result->device = device;305 304 306 305 … … 317 316 }*/ 318 317 319 result-> usb_device = usb_new(usb_hcd_attached_device_info_t);320 result-> usb_device->address = addr;318 result->device = usb_new(usb_hcd_attached_device_info_t); 319 result->device->address = addr; 321 320 322 321 // get hub descriptor … … 351 350 //printf("[usb_hub] setting port count to %d\n",descriptor->ports_count); 352 351 result->port_count = descriptor->ports_count; 353 result->attached_devs = (usb_hub_attached_device_t*)354 malloc((result->port_count+1) * sizeof(usb_hub_attached_device_t));355 int i;356 for(i=0;i<result->port_count+1;++i){357 result->attached_devs[i].devman_handle=0;358 result->attached_devs[i].address=0;359 }360 352 //printf("[usb_hub] freeing data\n"); 361 353 free(serialized_descriptor); … … 370 362 } 371 363 364 /** Callback when new hub device is detected. 365 * 366 * @param dev New device. 367 * @return Error code. 368 */ 372 369 int usb_add_hub_device(device_t *dev) { 373 370 printf(NAME ": add_hub_device(handle=%d)\n", (int) dev->handle); … … 390 387 usb_device_request_setup_packet_t request; 391 388 usb_target_t target; 392 target.address = hub_info-> usb_device->address;389 target.address = hub_info->device->address; 393 390 target.endpoint = 0; 394 395 //get configuration descriptor 396 // this is not fully correct - there are more configurations 397 // and all should be checked 398 usb_standard_device_descriptor_t std_descriptor; 399 opResult = usb_drv_req_get_device_descriptor(hc, target.address, 400 &std_descriptor); 401 if(opResult!=EOK){ 402 printf("[usb_hub] could not get device descriptor, %d\n",opResult); 403 return 1;///\TODO some proper error code needed 404 } 405 printf("[usb_hub] hub has %d configurations\n",std_descriptor.configuration_count); 406 if(std_descriptor.configuration_count<1){ 407 printf("[usb_hub] THERE ARE NO CONFIGURATIONS AVAILABLE\n"); 408 } 409 usb_standard_configuration_descriptor_t config_descriptor; 410 opResult = usb_drv_req_get_bare_configuration_descriptor(hc, 411 target.address, 0, 412 &config_descriptor); 413 if(opResult!=EOK){ 414 printf("[usb_hub] could not get configuration descriptor, %d\n",opResult); 415 return 1;///\TODO some proper error code needed 416 } 417 //set configuration 418 request.request_type = 0; 419 request.request = USB_DEVREQ_SET_CONFIGURATION; 420 request.index=0; 421 request.length=0; 422 request.value_high=0; 423 request.value_low = config_descriptor.configuration_number; 424 opResult = usb_drv_sync_control_write(hc, target, &request, NULL, 0); 425 if (opResult != EOK) { 426 printf("[usb_hub]something went wrong when setting hub`s configuration, %d\n", opResult); 427 } 428 429 430 for (port = 1; port < hub_info->port_count+1; ++port) { 391 for (port = 0; port < hub_info->port_count; ++port) { 431 392 usb_hub_set_power_port_request(&request, port); 432 393 opResult = usb_drv_sync_control_write(hc, target, &request, NULL, 0); 433 printf("[usb_hub] powering port %d\n",port);434 394 if (opResult != EOK) { 435 395 printf("[usb_hub]something went wrong when setting hub`s %dth port\n", port); … … 437 397 } 438 398 //ports powered, hub seems to be enabled 439 440 399 441 400 ipc_hangup(hc); … … 445 404 printf("[usb_hub] hub info added to list\n"); 446 405 //(void)hub_info; 447 usb_hub_check_hub_changes();406 check_hub_changes(); 448 407 449 408 /// \TODO start the check loop, if not already started... … … 453 412 454 413 printf("[usb_hub] hub dev added\n"); 455 printf("\taddress %d, has %d ports \n",456 hub_info->usb_device->address,457 hub_info->port_count);458 printf("\tused configuration %d\n",config_descriptor.configuration_number);459 414 460 415 return EOK; … … 474 429 * @param target 475 430 */ 431 476 432 static void usb_hub_init_add_device(int hc, uint16_t port, usb_target_t target) { 477 433 usb_device_request_setup_packet_t request; 478 434 int opResult; 479 435 printf("[usb_hub] some connection changed\n"); 480 //get default address 436 481 437 opResult = usb_drv_reserve_default_address(hc); 482 438 if (opResult != EOK) { … … 503 459 * @param target 504 460 */ 505 static void usb_hub_finalize_add_device( usb_hub_info_t * hub,461 static void usb_hub_finalize_add_device( 506 462 int hc, uint16_t port, usb_target_t target) { 507 463 464 usb_device_request_setup_packet_t request; 508 465 int opResult; 509 466 printf("[usb_hub] finalizing add device\n"); 510 opResult = usb_hub_clear_port_feature(hc, target.address, 511 port, USB_HUB_FEATURE_C_PORT_RESET); 512 if (opResult != EOK) { 513 goto release; 514 } 515 516 /* Request address at from host controller. */ 517 usb_address_t new_device_address = usb_drv_request_address(hc); 518 if (new_device_address < 0) { 519 printf("[usb_hub] failed to get free USB address\n"); 520 opResult = new_device_address; 521 goto release; 522 } 523 printf("[usb_hub] setting new address\n"); 524 opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT, 525 new_device_address); 526 467 usb_address_t new_device_address = 468 usb_drv_request_address(hc); 469 usb_hub_set_set_address_request 470 (&request, new_device_address); 471 opResult = usb_drv_sync_control_write( 472 hc, target, 473 &request, 474 NULL, 0 475 ); 527 476 if (opResult != EOK) { 528 477 printf("[usb_hub] could not set address for new device\n"); 529 goto release; 530 } 531 532 release: 533 printf("[usb_hub] releasing default address\n"); 478 //will retry later... 479 return; 480 } 534 481 usb_drv_release_default_address(hc); 535 if (opResult != EOK) { 536 return; 537 } 538 539 devman_handle_t child_handle; 540 opResult = usb_drv_register_child_in_devman(hc, hub->device, 541 new_device_address, &child_handle); 542 if (opResult != EOK) { 543 printf("[usb_hub] could not start driver for new device \n"); 544 return; 545 } 546 hub->attached_devs[port].devman_handle = child_handle; 547 hub->attached_devs[port].address = new_device_address; 548 549 opResult = usb_drv_bind_address(hc, new_device_address, child_handle); 550 if (opResult != EOK) { 551 printf("[usb_hub] could not assign address of device in hcd \n"); 552 return; 553 } 554 printf("[usb_hub] new device address %d, handle %zu\n", 555 new_device_address, child_handle); 556 482 483 484 /// \TODO driver work 485 //add_child_device..... 557 486 } 558 487 … … 563 492 * @param target 564 493 */ 565 static void usb_hub_removed_device( 566 usb_hub_info_t * hub, int hc, uint16_t port, usb_target_t target) { 567 //usb_device_request_setup_packet_t request; 494 static void usb_hub_removed_device(int hc, uint16_t port, usb_target_t target) { 495 usb_device_request_setup_packet_t request; 568 496 int opResult; 569 497 //disable port 570 /*usb_hub_set_disable_port_request(&request, port);498 usb_hub_set_disable_port_request(&request, port); 571 499 opResult = usb_drv_sync_control_write( 572 500 hc, target, … … 577 505 //continue; 578 506 printf("[usb_hub] something went wrong when disabling a port\n"); 579 }*/ 580 /// \TODO remove device 581 582 hub->attached_devs[port].devman_handle=0; 507 } 508 //remove device 583 509 //close address 584 if(hub->attached_devs[port].address!=0){ 585 opResult = usb_drv_release_address(hc,hub->attached_devs[port].address); 586 if(opResult != EOK) { 587 printf("[usb_hub] could not release address of removed device: %d\n",opResult); 588 } 589 hub->attached_devs[port].address = 0; 590 }else{ 591 printf("[usb_hub] this is strange, disconnected device had no address\n"); 592 //device was disconnected before it`s port was reset - return default address 593 usb_drv_release_default_address(hc); 594 } 510 // 511 512 ///\TODO this code is not complete 595 513 } 596 514 … … 601 519 * @param target 602 520 */ 603 static void usb_hub_process_interrupt(usb_hub_info_t * hub, int hc, 604 uint16_t port, usb_address_t address) { 521 static void usb_hub_process_interrupt(int hc, uint16_t port, usb_target_t target) { 605 522 printf("[usb_hub] interrupt at port %d\n", port); 606 523 //determine type of change 607 usb_target_t target;608 target.address=address;609 target.endpoint=0;610 524 usb_port_status_t status; 611 525 size_t rcvd_size; … … 613 527 int opResult; 614 528 usb_hub_set_port_status_request(&request, port); 615 //endpoint 0616 529 617 530 opResult = usb_drv_sync_control_read( … … 630 543 //something connected/disconnected 631 544 if (usb_port_connect_change(&status)) { 632 opResult = usb_hub_clear_port_feature(hc, target.address,633 port, USB_HUB_FEATURE_C_PORT_CONNECTION);634 // TODO: check opResult635 545 if (usb_port_dev_connected(&status)) { 636 546 printf("[usb_hub] some connection changed\n"); 637 547 usb_hub_init_add_device(hc, port, target); 638 548 } else { 639 usb_hub_removed_device(h ub, hc, port, target);549 usb_hub_removed_device(hc, port, target); 640 550 } 641 551 } 642 552 //port reset 643 553 if (usb_port_reset_completed(&status)) { 644 printf("[usb_hub] port reset complete\n");554 printf("[usb_hub] finalizing add device\n"); 645 555 if (usb_port_enabled(&status)) { 646 usb_hub_finalize_add_device(h ub, hc, port, target);556 usb_hub_finalize_add_device(hc, port, target); 647 557 } else { 648 558 printf("[usb_hub] ERROR: port reset, but port still not enabled\n"); … … 655 565 usb_port_set_dev_connected(&status, false); 656 566 if (status) { 657 printf("[usb_hub]there was some unsupported change on port %d\n",port);567 printf("[usb_hub]there was some unsupported change on port\n"); 658 568 } 659 569 /// \TODO handle other changes … … 675 585 /** Check changes on all known hubs. 676 586 */ 677 void usb_hub_check_hub_changes(void) {587 static void check_hub_changes(void) { 678 588 /* 679 589 * Iterate through all hubs. … … 683 593 lst_item != &usb_hub_list; 684 594 lst_item = lst_item->next) { 685 usb_hub_info_t * hub_info = ((usb_hub_info_t*)lst_item->data);595 printf("[usb_hub] checking hub changes\n"); 686 596 /* 687 597 * Check status change pipe of this hub. 688 598 */ 689 599 690 usb_target_t target; 691 target.address = hub_info->usb_device->address; 692 target.endpoint = 1;/// \TODO get from endpoint descriptor 693 printf("[usb_hub] checking changes for hub at addr %d\n", 694 target.address); 695 696 size_t port_count = hub_info->port_count; 600 usb_target_t target = { 601 .address = 5, 602 .endpoint = 1 603 }; 604 /// \TODO uncomment once it works correctly 605 //target.address = usb_create_hub_info(lst_item)->device->address; 606 607 size_t port_count = 7; 697 608 698 609 /* 699 610 * Connect to respective HC. 700 611 */ 701 int hc = usb_drv_hc_connect(hub_info->device, 0); 612 /// \FIXME this is incorrect code: here 613 /// must be used particular device instead of NULL 614 //which one? 615 int hc = usb_drv_hc_connect(NULL, 0); 702 616 if (hc < 0) { 703 617 continue; … … 705 619 706 620 // FIXME: count properly 707 size_t byte_length = ( (port_count+1)/ 8) + 1;621 size_t byte_length = (port_count / 8) + 1; 708 622 709 623 void *change_bitmap = malloc(byte_length); … … 725 639 } 726 640 unsigned int port; 727 for (port = 1; port < port_count+1; ++port) {641 for (port = 0; port < port_count; ++port) { 728 642 bool interrupt = (((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2; 729 643 if (interrupt) { 730 usb_hub_process_interrupt( 731 hub_info, hc, port, hub_info->usb_device->address); 644 usb_hub_process_interrupt(hc, port, target); 732 645 } 733 646 }
Note:
See TracChangeset
for help on using the changeset viewer.