Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/usbhub/utils.c

    rea5dbaf ree0d8a8  
    3434 */
    3535#include <driver.h>
    36 #include <bool.h>
    37 #include <errno.h>
    38 
     36#include <usb/devreq.h>
    3937#include <usbhc_iface.h>
    4038#include <usb/usbdrv.h>
    4139#include <usb/descriptor.h>
    42 #include <usb/devreq.h>
     40#include <driver.h>
     41#include <bool.h>
     42#include <errno.h>
    4343#include <usb/classes/hub.h>
    44 
    4544#include "usbhub.h"
    4645#include "usbhub_private.h"
    4746#include "port_status.h"
    48 
     47#include <usb/devreq.h>
     48
     49static void check_hub_changes(void);
    4950
    5051size_t USB_HUB_MAX_DESCRIPTOR_SIZE = 71;
     
    301302        //result->device = device;
    302303        result->port_count = -1;
    303         /// \TODO is this correct? is the device stored?
    304         result->device = device;
    305304
    306305
     
    317316        }*/
    318317
    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;
    321320
    322321        // get hub descriptor
     
    351350        //printf("[usb_hub] setting port count to %d\n",descriptor->ports_count);
    352351        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         }
    360352        //printf("[usb_hub] freeing data\n");
    361353        free(serialized_descriptor);
     
    370362}
    371363
     364/** Callback when new hub device is detected.
     365 *
     366 * @param dev New device.
     367 * @return Error code.
     368 */
    372369int usb_add_hub_device(device_t *dev) {
    373370        printf(NAME ": add_hub_device(handle=%d)\n", (int) dev->handle);
     
    390387        usb_device_request_setup_packet_t request;
    391388        usb_target_t target;
    392         target.address = hub_info->usb_device->address;
     389        target.address = hub_info->device->address;
    393390        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) {
    431392                usb_hub_set_power_port_request(&request, port);
    432393                opResult = usb_drv_sync_control_write(hc, target, &request, NULL, 0);
    433                 printf("[usb_hub] powering port %d\n",port);
    434394                if (opResult != EOK) {
    435395                        printf("[usb_hub]something went wrong when setting hub`s %dth port\n", port);
     
    437397        }
    438398        //ports powered, hub seems to be enabled
    439        
    440399
    441400        ipc_hangup(hc);
     
    445404        printf("[usb_hub] hub info added to list\n");
    446405        //(void)hub_info;
    447         usb_hub_check_hub_changes();
     406        check_hub_changes();
    448407
    449408        /// \TODO start the check loop, if not already started...
     
    453412
    454413        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);
    459414
    460415        return EOK;
     
    474429 * @param target
    475430 */
     431
    476432static void usb_hub_init_add_device(int hc, uint16_t port, usb_target_t target) {
    477433        usb_device_request_setup_packet_t request;
    478434        int opResult;
    479435        printf("[usb_hub] some connection changed\n");
    480         //get default address
     436
    481437        opResult = usb_drv_reserve_default_address(hc);
    482438        if (opResult != EOK) {
     
    503459 * @param target
    504460 */
    505 static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
     461static void usb_hub_finalize_add_device(
    506462                int hc, uint16_t port, usb_target_t target) {
    507463
     464        usb_device_request_setup_packet_t request;
    508465        int opResult;
    509466        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                        );
    527476        if (opResult != EOK) {
    528477                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        }
    534481        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.....
    557486}
    558487
     
    563492 * @param target
    564493 */
    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;
     494static void usb_hub_removed_device(int hc, uint16_t port, usb_target_t target) {
     495        usb_device_request_setup_packet_t request;
    568496        int opResult;
    569497        //disable port
    570         /*usb_hub_set_disable_port_request(&request, port);
     498        usb_hub_set_disable_port_request(&request, port);
    571499        opResult = usb_drv_sync_control_write(
    572500                        hc, target,
     
    577505                //continue;
    578506                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
    583509        //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
    595513}
    596514
     
    601519 * @param target
    602520 */
    603 static void usb_hub_process_interrupt(usb_hub_info_t * hub, int hc,
    604         uint16_t port, usb_address_t address) {
     521static void usb_hub_process_interrupt(int hc, uint16_t port, usb_target_t target) {
    605522        printf("[usb_hub] interrupt at port %d\n", port);
    606523        //determine type of change
    607         usb_target_t target;
    608         target.address=address;
    609         target.endpoint=0;
    610524        usb_port_status_t status;
    611525        size_t rcvd_size;
     
    613527        int opResult;
    614528        usb_hub_set_port_status_request(&request, port);
    615         //endpoint 0
    616529
    617530        opResult = usb_drv_sync_control_read(
     
    630543        //something connected/disconnected
    631544        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 opResult
    635545                if (usb_port_dev_connected(&status)) {
    636546                        printf("[usb_hub] some connection changed\n");
    637547                        usb_hub_init_add_device(hc, port, target);
    638548                } else {
    639                         usb_hub_removed_device(hub, hc, port, target);
     549                        usb_hub_removed_device(hc, port, target);
    640550                }
    641551        }
    642552        //port reset
    643553        if (usb_port_reset_completed(&status)) {
    644                 printf("[usb_hub] port reset complete\n");
     554                printf("[usb_hub] finalizing add device\n");
    645555                if (usb_port_enabled(&status)) {
    646                         usb_hub_finalize_add_device(hub, hc, port, target);
     556                        usb_hub_finalize_add_device(hc, port, target);
    647557                } else {
    648558                        printf("[usb_hub] ERROR: port reset, but port still not enabled\n");
     
    655565        usb_port_set_dev_connected(&status, false);
    656566        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");
    658568        }
    659569        /// \TODO handle other changes
     
    675585/** Check changes on all known hubs.
    676586 */
    677 void usb_hub_check_hub_changes(void) {
     587static void check_hub_changes(void) {
    678588        /*
    679589         * Iterate through all hubs.
     
    683593                        lst_item != &usb_hub_list;
    684594                        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");
    686596                /*
    687597                 * Check status change pipe of this hub.
    688598                 */
    689599
    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;
    697608
    698609                /*
    699610                 * Connect to respective HC.
    700611                 */
    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);
    702616                if (hc < 0) {
    703617                        continue;
     
    705619
    706620                // FIXME: count properly
    707                 size_t byte_length = ((port_count+1) / 8) + 1;
     621                size_t byte_length = (port_count / 8) + 1;
    708622
    709623                void *change_bitmap = malloc(byte_length);
     
    725639                }
    726640                unsigned int port;
    727                 for (port = 1; port < port_count+1; ++port) {
     641                for (port = 0; port < port_count; ++port) {
    728642                        bool interrupt = (((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2;
    729643                        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);
    732645                        }
    733646                }
Note: See TracChangeset for help on using the changeset viewer.