Changeset 195890b in mainline for uspace/drv/usbhub/usbhub.c


Ignore:
Timestamp:
2011-04-03T21:39:28Z (13 years ago)
Author:
Matus Dekanek <smekideki@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
3bb6b35
Parents:
6c399765
Message:

codelifting

File:
1 edited

Legend:

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

    r6c399765 r195890b  
    5555
    5656static int usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port,
    57                 usb_speed_t speed);
     57        usb_speed_t speed);
    5858
    5959static int usb_hub_trigger_connecting_non_removable_devices(
    60                 usb_hub_info_t * hub, usb_hub_descriptor_t * descriptor);
     60        usb_hub_info_t * hub, usb_hub_descriptor_t * descriptor);
    6161
    6262/**
     
    6969 * @return zero
    7070 */
    71 int usb_hub_control_loop(void * hub_info_param){
    72         usb_hub_info_t * hub_info = (usb_hub_info_t*)hub_info_param;
     71int usb_hub_control_loop(void * hub_info_param) {
     72        usb_hub_info_t * hub_info = (usb_hub_info_t*) hub_info_param;
    7373        int errorCode = EOK;
    7474
    75         while(errorCode == EOK){
    76                 async_usleep(1000 * 1000 * 10 );/// \TODO proper number once
     75        while (errorCode == EOK) {
     76                async_usleep(1000 * 1000 * 10); /// \TODO proper number once
    7777                errorCode = usb_hub_check_hub_changes(hub_info);
    78                
    79         }
    80         usb_log_error("something in ctrl loop went wrong, errno %d\n",errorCode);
     78
     79        }
     80        usb_log_error("something in ctrl loop went wrong, errno %d\n",
     81                errorCode);
    8182
    8283        return 0;
     
    99100static usb_hub_info_t * usb_hub_info_create(usb_device_t * usb_dev) {
    100101        usb_hub_info_t * result = usb_new(usb_hub_info_t);
    101         if(!result) return NULL;
     102        if (!result) return NULL;
    102103        result->usb_device = usb_dev;
    103104        result->status_change_pipe = usb_dev->pipes[0].pipe;
     
    117118 * @return error code
    118119 */
    119 static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info){
     120static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info) {
    120121        // get hub descriptor
    121122        usb_log_debug("creating serialized descriptor\n");
     
    128129         */
    129130        int opResult = usb_request_set_configuration(hub_info->control_pipe,
    130                         1);
    131         if(opResult!=EOK){
    132                 usb_log_error("could not set default configuration, errno %d",opResult);
    133                 return opResult;
    134         }
    135          
    136          
     131                1);
     132        if (opResult != EOK) {
     133                usb_log_error("could not set default configuration, errno %d",
     134                        opResult);
     135                return opResult;
     136        }
     137
     138
    137139        size_t received_size;
    138140        opResult = usb_request_get_descriptor(&hub_info->usb_device->ctrl_pipe,
    139                         USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
    140                         USB_DESCTYPE_HUB,
    141                         0, 0, serialized_descriptor,
    142                         USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
    143 
    144         if (opResult != EOK) {
    145                 usb_log_error("failed when receiving hub descriptor, badcode = %d\n",
    146                                 opResult);
     141                USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
     142                USB_DESCTYPE_HUB,
     143                0, 0, serialized_descriptor,
     144                USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
     145
     146        if (opResult != EOK) {
     147                usb_log_error("failed when receiving hub descriptor, "
     148                        "badcode = %d\n",
     149                        opResult);
    147150                free(serialized_descriptor);
    148151                return opResult;
     
    150153        usb_log_debug2("deserializing descriptor\n");
    151154        descriptor = usb_deserialize_hub_desriptor(serialized_descriptor);
    152         if(descriptor==NULL){
     155        if (descriptor == NULL) {
    153156                usb_log_warning("could not deserialize descriptor \n");
    154157                return opResult;
    155158        }
    156         usb_log_debug("setting port count to %d\n",descriptor->ports_count);
     159        usb_log_debug("setting port count to %d\n", descriptor->ports_count);
    157160        hub_info->port_count = descriptor->ports_count;
    158161        hub_info->attached_devs = (usb_hc_attached_device_t*)
    159             malloc((hub_info->port_count+1) * sizeof(usb_hc_attached_device_t));
     162                malloc((hub_info->port_count + 1) *
     163                        sizeof (usb_hc_attached_device_t)
     164                );
    160165        int i;
    161         for(i=0;i<hub_info->port_count+1;++i){
    162                 hub_info->attached_devs[i].handle=0;
    163                 hub_info->attached_devs[i].address=0;
     166        for (i = 0; i < hub_info->port_count + 1; ++i) {
     167                hub_info->attached_devs[i].handle = 0;
     168                hub_info->attached_devs[i].address = 0;
    164169        }
    165170        //handle non-removable devices
     
    169174        hub_info->descriptor = descriptor;
    170175        hub_info->not_initialized_non_removables =
    171                 (uint8_t*)malloc((hub_info->port_count+8)/8);
     176                (uint8_t*) malloc((hub_info->port_count + 8) / 8);
    172177        memcpy(hub_info->not_initialized_non_removables,
    173178                descriptor->devices_removable,
    174                 (hub_info->port_count+8)/8
     179                (hub_info->port_count + 8) / 8
    175180                );
    176181
     
    179184        return EOK;
    180185}
     186
    181187/**
    182188 * Set configuration of hub
     
    187193 * @return error code
    188194 */
    189 static int usb_hub_set_configuration(usb_hub_info_t * hub_info){
     195static int usb_hub_set_configuration(usb_hub_info_t * hub_info) {
    190196        //device descriptor
    191197        usb_standard_device_descriptor_t *std_descriptor
    192             = &hub_info->usb_device->descriptors.device;
     198                = &hub_info->usb_device->descriptors.device;
    193199        usb_log_debug("hub has %d configurations\n",
    194             std_descriptor->configuration_count);
    195         if(std_descriptor->configuration_count<1){
     200                std_descriptor->configuration_count);
     201        if (std_descriptor->configuration_count < 1) {
    196202                usb_log_error("there are no configurations available\n");
    197203                return EINVAL;
     
    199205
    200206        usb_standard_configuration_descriptor_t *config_descriptor
    201             = (usb_standard_configuration_descriptor_t *)
    202             hub_info->usb_device->descriptors.configuration;
     207                = (usb_standard_configuration_descriptor_t *)
     208                hub_info->usb_device->descriptors.configuration;
    203209
    204210        /* Set configuration. */
    205211        int opResult = usb_request_set_configuration(
    206             &hub_info->usb_device->ctrl_pipe,
    207             config_descriptor->configuration_number);
     212                &hub_info->usb_device->ctrl_pipe,
     213                config_descriptor->configuration_number);
    208214
    209215        if (opResult != EOK) {
    210216                usb_log_error("Failed to set hub configuration: %s.\n",
    211                     str_error(opResult));
     217                        str_error(opResult));
    212218                return opResult;
    213219        }
    214220        usb_log_debug("\tused configuration %d\n",
    215                         config_descriptor->configuration_number);
     221                config_descriptor->configuration_number);
    216222
    217223        return EOK;
     
    226232 * @return error code
    227233 */
    228 int usb_hub_add_device(usb_device_t * usb_dev){
    229         if(!usb_dev) return EINVAL;
     234int usb_hub_add_device(usb_device_t * usb_dev) {
     235        if (!usb_dev) return EINVAL;
    230236        usb_hub_info_t * hub_info = usb_hub_info_create(usb_dev);
    231237        //create hc connection
    232238        usb_log_debug("Initializing USB wire abstraction.\n");
    233239        int opResult = usb_hc_connection_initialize_from_device(
    234                         &hub_info->connection,
    235                         hub_info->usb_device->ddf_dev);
    236         if(opResult != EOK){
    237                 usb_log_error("could not initialize connection to device, errno %d\n",
    238                                 opResult);
     240                &hub_info->connection,
     241                hub_info->usb_device->ddf_dev);
     242        if (opResult != EOK) {
     243                usb_log_error("could not initialize connection to device, "
     244                        "errno %d\n",
     245                        opResult);
    239246                free(hub_info);
    240247                return opResult;
    241248        }
    242        
     249
    243250        usb_pipe_start_session(hub_info->control_pipe);
    244251        //set hub configuration
    245252        opResult = usb_hub_set_configuration(hub_info);
    246         if(opResult!=EOK){
    247                 usb_log_error("could not set hub configuration, errno %d\n",opResult);
     253        if (opResult != EOK) {
     254                usb_log_error("could not set hub configuration, errno %d\n",
     255                        opResult);
    248256                free(hub_info);
    249257                return opResult;
     
    251259        //get port count and create attached_devs
    252260        opResult = usb_hub_process_hub_specific_info(hub_info);
    253         if(opResult!=EOK){
    254                 usb_log_error("could not set hub configuration, errno %d\n",opResult);
     261        if (opResult != EOK) {
     262                usb_log_error("could not set hub configuration, errno %d\n",
     263                        opResult);
    255264                free(hub_info);
    256265                return opResult;
     
    262271        usb_log_debug("Creating `hub' function.\n");
    263272        ddf_fun_t *hub_fun = ddf_fun_create(hub_info->usb_device->ddf_dev,
    264                         fun_exposed, "hub");
     273                fun_exposed, "hub");
    265274        assert(hub_fun != NULL);
    266275        hub_fun->ops = NULL;
     
    274283        fid_t fid = fibril_create(usb_hub_control_loop, hub_info);
    275284        if (fid == 0) {
    276                 usb_log_error("failed to start monitoring fibril for new hub.\n");
     285                usb_log_error("failed to start monitoring fibril for new"
     286                        " hub.\n");
    277287                return ENOMEM;
    278288        }
     
    281291
    282292        usb_log_info("Controlling hub `%s' (%d ports).\n",
    283             hub_info->usb_device->ddf_dev->name, hub_info->port_count);
     293                hub_info->usb_device->ddf_dev->name, hub_info->port_count);
    284294        return EOK;
    285295}
     
    302312 */
    303313static int usb_hub_trigger_connecting_non_removable_devices(
    304                 usb_hub_info_t * hub,
    305                 usb_hub_descriptor_t * descriptor)
    306 {
     314        usb_hub_info_t * hub,
     315        usb_hub_descriptor_t * descriptor) {
    307316        usb_log_info("attaching non-removable devices(if any)\n");
    308317        //usb_device_request_setup_packet_t request;
     
    314323
    315324        opResult = usb_request_set_configuration(hub->control_pipe,
    316                         1);
    317         if(opResult!=EOK){
    318                 usb_log_error("could not set default configuration, errno %d",opResult);
     325                1);
     326        if (opResult != EOK) {
     327                usb_log_error("could not set default configuration, errno %d",
     328                        opResult);
    319329                return opResult;
    320330        }
    321331#if 0
    322         for(port=1;port<=descriptor->ports_count;++port){
     332        for (port = 1; port <= descriptor->ports_count; ++port) {
    323333                bool is_non_removable =
    324                                 ((non_removable_dev_bitmap[port/8]) >> (port%8)) %2;
    325                 if(is_non_removable){
    326                         usb_log_debug("non-removable device on port %d\n",port);
     334                        ((non_removable_dev_bitmap[port / 8]) >> (port % 8)) % 2;
     335                if (is_non_removable) {
     336                        usb_log_debug("non-removable device on port %d\n", port);
    327337                        usb_hub_set_port_status_request(&request, port);
    328338                        opResult = usb_pipe_control_read(
    329                                         hub->control_pipe,
    330                                         &request, sizeof(usb_device_request_setup_packet_t),
    331                                         &status, 4, &rcvd_size
    332                                         );
     339                                hub->control_pipe,
     340                                &request,
     341                                sizeof (usb_device_request_setup_packet_t),
     342                                &status, 4, &rcvd_size
     343                                );
    333344                        if (opResult != EOK) {
    334                                 usb_log_error("could not get port status of port %d errno:%d\n",
    335                                                 port, opResult);
     345                                usb_log_error("could not get port status of "
     346                                        "port %d errno:%d\n",
     347                                        port, opResult);
    336348                                return opResult;
    337349                        }
    338350                        //try to reset port
    339                         if(usb_port_dev_connected(&status) || true){
    340                                 usb_hub_set_enable_port_feature_request(&request, port,
    341                                                 USB_HUB_FEATURE_PORT_RESET);
     351                        if (usb_port_dev_connected(&status) || true) {
     352                                usb_hub_set_enable_port_feature_request(
     353                                        &request, port,
     354                                        USB_HUB_FEATURE_PORT_RESET);
    342355                                opResult = usb_pipe_control_read(
    343                                                 hub->control_pipe,
    344                                                 &request, sizeof(usb_device_request_setup_packet_t),
    345                                                 &status, 4, &rcvd_size
    346                                                 );
     356                                        hub->control_pipe,
     357                                        &request,
     358                                        sizeof (usb_device_request_setup_packet_t),
     359                                        &status, 4, &rcvd_size
     360                                        );
    347361                                if (opResult != EOK) {
    348362                                        usb_log_warning(
    349                                                         "could not reset port %d errno:%d\n",
    350                                                         port, opResult);
     363                                                "could not reset port %d "
     364                                                "errno:%d\n",
     365                                                port, opResult);
    351366                                }
    352                                 usb_log_debug("port reset, should look like %d,x%x\n",
    353                                                 (1<<USB_HUB_FEATURE_PORT_RESET),
    354                                                 (1<<USB_HUB_FEATURE_PORT_RESET)
    355                                                 );
     367                                usb_log_debug("port reset, should look like "
     368                                        "%d,x%x\n",
     369                                        (1 << USB_HUB_FEATURE_PORT_RESET),
     370                                        (1 << USB_HUB_FEATURE_PORT_RESET)
     371                                        );
    356372                        }
    357                         //set the status change bit, so it will be noticed in driver loop
    358                         if(usb_port_dev_connected(&status) && false){
    359                                 usb_hub_set_disable_port_feature_request(&request, port,
    360                                                 USB_HUB_FEATURE_PORT_CONNECTION);
     373                        //set the status change bit, so it will be noticed
     374                        //in driver loop
     375                        if (usb_port_dev_connected(&status) && false) {
     376                                usb_hub_set_disable_port_feature_request(
     377                                        &request, port,
     378                                        USB_HUB_FEATURE_PORT_CONNECTION);
    361379                                opResult = usb_pipe_control_read(
    362                                                 hub->control_pipe,
    363                                                 &request, sizeof(usb_device_request_setup_packet_t),
    364                                                 &status, 4, &rcvd_size
    365                                                 );
     380                                        hub->control_pipe,
     381                                        &request,
     382                                        sizeof (usb_device_request_setup_packet_t),
     383                                        &status, 4, &rcvd_size
     384                                        );
    366385                                if (opResult != EOK) {
    367386                                        usb_log_warning(
    368                                                         "could not clear port connection on port %d errno:%d\n",
    369                                                         port, opResult);
     387                                                "could not clear port "
     388                                                "connection on port %d "
     389                                                "errno:%d\n",
     390                                                port, opResult);
    370391                                }
    371392                                usb_log_debug("cleared port connection\n");
    372                                 usb_hub_set_enable_port_feature_request(&request, port,
    373                                                 USB_HUB_FEATURE_PORT_ENABLE);
     393                                usb_hub_set_enable_port_feature_request(&request,
     394                                        port,
     395                                        USB_HUB_FEATURE_PORT_ENABLE);
    374396                                opResult = usb_pipe_control_read(
    375                                                 hub->control_pipe,
    376                                                 &request, sizeof(usb_device_request_setup_packet_t),
    377                                                 &status, 4, &rcvd_size
    378                                                 );
     397                                        hub->control_pipe,
     398                                        &request,
     399                                        sizeof (usb_device_request_setup_packet_t),
     400                                        &status, 4, &rcvd_size
     401                                        );
    379402                                if (opResult != EOK) {
    380403                                        usb_log_warning(
    381                                                         "could not set port enabled on port %d errno:%d\n",
    382                                                         port, opResult);
     404                                                "could not set port enabled "
     405                                                "on port %d errno:%d\n",
     406                                                port, opResult);
    383407                                }
    384                                 usb_log_debug("port set to enabled - should lead to connection change\n");
     408                                usb_log_debug("port set to enabled - "
     409                                        "should lead to connection change\n");
    385410                        }
    386411                }
     
    389414
    390415        /// \TODO this is just a debug code
    391         for(port=1;port<=descriptor->ports_count;++port){
     416        for (port = 1; port <= descriptor->ports_count; ++port) {
    392417                bool is_non_removable =
    393                                 ((non_removable_dev_bitmap[port/8]) >> (port%8)) %2;
    394                 if(is_non_removable){
    395                         usb_log_debug("CHECKING port %d is non-removable\n",port);
     418                        ((non_removable_dev_bitmap[port / 8]) >> (port % 8)) % 2;
     419                if (is_non_removable) {
     420                        usb_log_debug("CHECKING port %d is non-removable\n",
     421                                port);
    396422                        usb_port_status_t status;
    397423                        size_t rcvd_size;
     
    401427                        //endpoint 0
    402428                        opResult = usb_pipe_control_read(
    403                                         hub->control_pipe,
    404                                         &request, sizeof(usb_device_request_setup_packet_t),
    405                                         &status, 4, &rcvd_size
    406                                         );
     429                                hub->control_pipe,
     430                                &request,
     431                                sizeof (usb_device_request_setup_packet_t),
     432                                &status, 4, &rcvd_size
     433                                );
    407434                        if (opResult != EOK) {
    408                                 usb_log_error("could not get port status %d\n",opResult);
     435                                usb_log_error("could not get port status %d\n",
     436                                        opResult);
    409437                        }
    410438                        if (rcvd_size != sizeof (usb_port_status_t)) {
    411                                 usb_log_error("received status has incorrect size\n");
     439                                usb_log_error("received status has incorrect"
     440                                        " size\n");
    412441                        }
    413442                        //something connected/disconnected
     
    415444                                usb_log_debug("some connection changed\n");
    416445                        }
    417                         usb_log_debug("status: %s\n",usb_debug_str_buffer(
    418                                         (uint8_t *)&status,4,4));
     446                        usb_log_debug("status: %s\n", usb_debug_str_buffer(
     447                                (uint8_t *) & status, 4, 4));
    419448                }
    420449        }
     
    422451        return EOK;
    423452}
    424 
    425453
    426454/**
     
    432460 * @return error code
    433461 */
    434 static int usb_hub_release_default_address(usb_hub_info_t * hub){
     462static int usb_hub_release_default_address(usb_hub_info_t * hub) {
    435463        int opResult = usb_hc_release_default_address(&hub->connection);
    436         if(opResult!=EOK){
    437                 usb_log_error("could not release default address, errno %d\n",opResult);
     464        if (opResult != EOK) {
     465                usb_log_error("could not release default address, errno %d\n",
     466                        opResult);
    438467                return opResult;
    439468        }
     
    450479 */
    451480static int usb_hub_init_add_device(usb_hub_info_t * hub, uint16_t port,
    452                 usb_speed_t speed) {
     481        usb_speed_t speed) {
    453482        //if this hub already uses default address, it cannot request it once more
    454         if(hub->is_default_address_used) return EREFUSED;
     483        if (hub->is_default_address_used) return EREFUSED;
    455484        usb_log_debug("some connection changed\n");
    456485        assert(hub->control_pipe->hc_phone);
    457486        int opResult = usb_hub_clear_port_feature(hub->control_pipe,
    458                                 port, USB_HUB_FEATURE_C_PORT_CONNECTION);
    459         if(opResult != EOK){
     487                port, USB_HUB_FEATURE_C_PORT_CONNECTION);
     488        if (opResult != EOK) {
    460489                usb_log_warning("could not clear port-change-connection flag\n");
    461490        }
    462491        usb_device_request_setup_packet_t request;
    463        
     492
    464493        //get default address
    465494        opResult = usb_hc_reserve_default_address(&hub->connection, speed);
    466        
    467         if (opResult != EOK) {
    468                 usb_log_warning("cannot assign default address, it is probably used %d\n",
    469                                 opResult);
     495
     496        if (opResult != EOK) {
     497                usb_log_warning("cannot assign default address, it is probably "
     498                        "used %d\n",
     499                        opResult);
    470500                return opResult;
    471501        }
     
    474504        usb_hub_set_reset_port_request(&request, port);
    475505        opResult = usb_pipe_control_write(
    476                         hub->control_pipe,
    477                         &request,sizeof(usb_device_request_setup_packet_t),
    478                         NULL, 0
    479                         );
    480         if (opResult != EOK) {
    481                 usb_log_error("something went wrong when reseting a port %d\n",opResult);
     506                hub->control_pipe,
     507                &request, sizeof (usb_device_request_setup_packet_t),
     508                NULL, 0
     509                );
     510        if (opResult != EOK) {
     511                usb_log_error("something went wrong when reseting a port %d\n",
     512                        opResult);
    482513                usb_hub_release_default_address(hub);
    483514        }
     
    493524 * @param speed transfer speed of attached device, one of low, full or high
    494525 */
    495 static void usb_hub_finalize_add_device( usb_hub_info_t * hub,
    496                 uint16_t port, usb_speed_t speed) {
     526static void usb_hub_finalize_add_device(usb_hub_info_t * hub,
     527        uint16_t port, usb_speed_t speed) {
    497528
    498529        int opResult;
    499530        usb_log_debug("finalizing add device\n");
    500531        opResult = usb_hub_clear_port_feature(hub->control_pipe,
    501             port, USB_HUB_FEATURE_C_PORT_RESET);
     532                port, USB_HUB_FEATURE_C_PORT_RESET);
    502533
    503534        if (opResult != EOK) {
     
    510541        usb_device_connection_t new_device_connection;
    511542        usb_device_connection_initialize_on_default_address(
    512                         &new_device_connection,
    513                         &hub->connection
    514                         );
     543                &new_device_connection,
     544                &hub->connection
     545                );
    515546        usb_pipe_initialize_default_control(
    516                         &new_device_pipe,
    517                         &new_device_connection);
     547                &new_device_pipe,
     548                &new_device_connection);
    518549        usb_pipe_probe_default_control(&new_device_pipe);
    519550
    520551        /* Request address from host controller. */
    521552        usb_address_t new_device_address = usb_hc_request_address(
    522                         &hub->connection,
    523                         speed
    524                         );
     553                &hub->connection,
     554                speed
     555                );
    525556        if (new_device_address < 0) {
    526557                usb_log_error("failed to get free USB address\n");
     
    529560                return;
    530561        }
    531         usb_log_debug("setting new address %d\n",new_device_address);
     562        usb_log_debug("setting new address %d\n", new_device_address);
    532563        //opResult = usb_drv_req_set_address(hc, USB_ADDRESS_DEFAULT,
    533564        //    new_device_address);
    534565        usb_pipe_start_session(&new_device_pipe);
    535         opResult = usb_request_set_address(&new_device_pipe,new_device_address);
     566        opResult = usb_request_set_address(&new_device_pipe,
     567                new_device_address);
    536568        usb_pipe_end_session(&new_device_pipe);
    537569        if (opResult != EOK) {
    538                 usb_log_error("could not set address for new device %d\n",opResult);
     570                usb_log_error("could not set address for new device %d\n",
     571                        opResult);
    539572                usb_hub_release_default_address(hub);
    540573                return;
     
    543576        //opResult = usb_hub_release_default_address(hc);
    544577        opResult = usb_hub_release_default_address(hub);
    545         if(opResult!=EOK){
     578        if (opResult != EOK) {
    546579                return;
    547580        }
     
    549582        devman_handle_t child_handle;
    550583        //??
    551     opResult = usb_device_register_child_in_devman(new_device_address,
    552             hub->connection.hc_handle, hub->usb_device->ddf_dev, &child_handle,
    553             NULL, NULL, NULL);
    554 
    555         if (opResult != EOK) {
    556                 usb_log_error("could not start driver for new device %d\n",opResult);
     584        opResult = usb_device_register_child_in_devman(new_device_address,
     585                hub->connection.hc_handle, hub->usb_device->ddf_dev,
     586                &child_handle,
     587                NULL, NULL, NULL);
     588
     589        if (opResult != EOK) {
     590                usb_log_error("could not start driver for new device %d\n",
     591                        opResult);
    557592                return;
    558593        }
     
    562597        //opResult = usb_drv_bind_address(hc, new_device_address, child_handle);
    563598        opResult = usb_hc_register_device(
    564                         &hub->connection,
    565                         &hub->attached_devs[port]);
    566         if (opResult != EOK) {
    567                 usb_log_error("could not assign address of device in hcd %d\n",opResult);
     599                &hub->connection,
     600                &hub->attached_devs[port]);
     601        if (opResult != EOK) {
     602                usb_log_error("could not assign address of device in hcd %d\n",
     603                        opResult);
    568604                return;
    569605        }
    570606        usb_log_info("Detected new device on `%s' (port %d), " \
    571607            "address %d (handle %llu).\n",
    572             hub->usb_device->ddf_dev->name, (int) port,
    573             new_device_address, child_handle);
     608                hub->usb_device->ddf_dev->name, (int) port,
     609                new_device_address, child_handle);
    574610}
    575611
     
    584620 */
    585621static void usb_hub_removed_device(
    586     usb_hub_info_t * hub,uint16_t port) {
     622        usb_hub_info_t * hub, uint16_t port) {
    587623
    588624        int opResult = usb_hub_clear_port_feature(hub->control_pipe,
    589                                 port, USB_HUB_FEATURE_C_PORT_CONNECTION);
    590         if(opResult != EOK){
     625                port, USB_HUB_FEATURE_C_PORT_CONNECTION);
     626        if (opResult != EOK) {
    591627                usb_log_warning("could not clear port-change-connection flag\n");
    592628        }
     
    594630         * devide manager
    595631         */
    596        
     632
    597633        //close address
    598         if(hub->attached_devs[port].address!=0){
     634        if (hub->attached_devs[port].address != 0) {
    599635                /*uncomment this code to use it when DDF allows device removal
    600636                opResult = usb_hc_unregister_device(
    601                                 &hub->connection, hub->attached_devs[port].address);
     637                        &hub->connection,
     638                        hub->attached_devs[port].address);
    602639                if(opResult != EOK) {
    603                         dprintf(USB_LOG_LEVEL_WARNING, "could not release address of " \
     640                        dprintf(USB_LOG_LEVEL_WARNING, "could not release "
     641                                "address of "
    604642                            "removed device: %d", opResult);
    605643                }
     
    607645                hub->attached_devs[port].handle = 0;
    608646                 */
    609         }else{
    610                 usb_log_warning("this is strange, disconnected device had no address\n");
    611                 //device was disconnected before it`s port was reset - return default address
     647        } else {
     648                usb_log_warning("this is strange, disconnected device had "
     649                        "no address\n");
     650                //device was disconnected before it`s port was reset -
     651                //return default address
    612652                usb_hub_release_default_address(hub);
    613653        }
    614654}
    615655
    616 
    617656/**
    618657 * Process over current condition on port.
    619  * 
     658 *
    620659 * Turn off the power on the port.
    621660 *
     
    623662 * @param port port number, starting from 1
    624663 */
    625 static void usb_hub_over_current( usb_hub_info_t * hub,
    626                 uint16_t port){
     664static void usb_hub_over_current(usb_hub_info_t * hub,
     665        uint16_t port) {
    627666        int opResult;
    628667        opResult = usb_hub_clear_port_feature(hub->control_pipe,
    629             port, USB_HUB_FEATURE_PORT_POWER);
    630         if(opResult!=EOK){
     668                port, USB_HUB_FEATURE_PORT_POWER);
     669        if (opResult != EOK) {
    631670                usb_log_error("cannot power off port %d;  %d\n",
    632                                 port, opResult);
     671                        port, opResult);
    633672        }
    634673}
     
    641680 * @param port port number, starting from 1
    642681 */
    643 static void usb_hub_process_interrupt(usb_hub_info_t * hub, 
    644         uint16_t port) {
     682static void usb_hub_process_interrupt(usb_hub_info_t * hub,
     683        uint16_t port) {
    645684        usb_log_debug("interrupt at port %d\n", port);
    646685        //determine type of change
    647686        usb_pipe_t *pipe = hub->control_pipe;
    648        
     687
    649688        int opResult;
    650689
     
    657696
    658697        opResult = usb_pipe_control_read(
    659                         pipe,
    660                         &request, sizeof(usb_device_request_setup_packet_t),
    661                         &status, 4, &rcvd_size
    662                         );
     698                pipe,
     699                &request, sizeof (usb_device_request_setup_packet_t),
     700                &status, 4, &rcvd_size
     701                );
    663702        if (opResult != EOK) {
    664703                usb_log_error("could not get port status\n");
     
    674713                if (usb_port_dev_connected(&status)) {
    675714                        usb_log_debug("some connection changed\n");
    676                         usb_hub_init_add_device(hub, port, usb_port_speed(&status));
     715                        usb_hub_init_add_device(hub, port,
     716                                usb_port_speed(&status));
    677717                } else {
    678718                        usb_hub_removed_device(hub, port);
     
    683723                //check if it was not auto-resolved
    684724                usb_log_debug("overcurrent change on port\n");
    685                 if(usb_port_over_current(&status)){
    686                         usb_hub_over_current(hub,port);
    687                 }else{
    688                         usb_log_debug("over current condition was auto-resolved on port %d\n",
    689                                         port);
     725                if (usb_port_over_current(&status)) {
     726                        usb_hub_over_current(hub, port);
     727                } else {
     728                        usb_log_debug("over current condition was "
     729                                "auto-resolved on port %d\n",
     730                                port);
    690731                }
    691732        }
     
    694735                usb_log_debug("port reset complete\n");
    695736                if (usb_port_enabled(&status)) {
    696                         usb_hub_finalize_add_device(hub, port, usb_port_speed(&status));
     737                        usb_hub_finalize_add_device(hub, port,
     738                                usb_port_speed(&status));
    697739                } else {
    698                         usb_log_warning("port reset, but port still not enabled\n");
     740                        usb_log_warning("port reset, but port still not "
     741                                "enabled\n");
    699742                }
    700743        }
    701         usb_log_debug("status x%x : %d\n ",status,status);
     744        usb_log_debug("status x%x : %d\n ", status, status);
    702745
    703746        usb_port_set_connect_change(&status, false);
     
    705748        usb_port_set_reset_completed(&status, false);
    706749        usb_port_set_dev_connected(&status, false);
    707         if (status>>16) {
     750        if (status >> 16) {
    708751                usb_log_info("there was some unsupported change on port %d: %X\n",
    709                                 port,status);
    710 
    711         }
    712 }
    713 
     752                        port, status);
     753
     754        }
     755}
    714756
    715757static int initialize_non_removable(usb_hub_info_t * hub_info,
    716         unsigned int port){
     758        unsigned int port) {
    717759        int opResult;
    718760        usb_log_debug("there is not pluged in non-removable device on "
    719                 "port %d\n",port
     761                "port %d\n", port
    720762                );
    721763        //usb_hub_init_add_device(hub_info, port, usb_port_speed(&status));
     
    728770
    729771        opResult = usb_pipe_control_read(
    730                         hub_info->control_pipe,
    731                         &request, sizeof(usb_device_request_setup_packet_t),
    732                         &status, 4, &rcvd_size
    733                         );
    734         if (opResult != EOK) {
    735                 usb_log_error("could not get port status %d\n",opResult);
     772                hub_info->control_pipe,
     773                &request, sizeof (usb_device_request_setup_packet_t),
     774                &status, 4, &rcvd_size
     775                );
     776        if (opResult != EOK) {
     777                usb_log_error("could not get port status %d\n", opResult);
    736778                return opResult;
    737779        }
     
    740782                return opResult;
    741783        }
    742         usb_log_debug("port status %d, x%x\n",status,status);
    743         if(usb_port_dev_connected(&status)){
     784        usb_log_debug("port status %d, x%x\n", status, status);
     785        if (usb_port_dev_connected(&status)) {
    744786                usb_log_debug("there is connected device on this port\n");
    745787        }
    746         if(!hub_info->is_default_address_used)
    747                 usb_hub_init_add_device(hub_info, port, usb_port_speed(&status));
     788        if (!hub_info->is_default_address_used)
     789                usb_hub_init_add_device(hub_info, port,
     790                        usb_port_speed(&status));
    748791        return opResult;
    749792}
     
    756799 * @return error code
    757800 */
    758 int usb_hub_check_hub_changes(usb_hub_info_t * hub_info){
     801int usb_hub_check_hub_changes(usb_hub_info_t * hub_info) {
    759802        int opResult;
    760803        opResult = usb_pipe_start_session(
    761                         hub_info->status_change_pipe);
    762         //this might not be necessary - if all non-removables are ok, it is not needed here
     804                hub_info->status_change_pipe);
     805        //this might not be necessary - if all non-removables are ok, it is
     806        //not needed here
    763807        opResult = usb_pipe_start_session(hub_info->control_pipe);
    764         if(opResult != EOK){
     808        if (opResult != EOK) {
    765809                usb_log_error("could not initialize communication for hub; %d\n",
    766                                 opResult);
     810                        opResult);
    767811                return opResult;
    768812        }
     
    771815        //first check non-removable devices
    772816        {
    773         unsigned int port;
    774         for(port = 1; port<=port_count; ++port){
    775                 bool is_non_removable =
    776                         hub_info->not_initialized_non_removables[port/8]
    777                         & (1 << (port%8));
    778                 if(is_non_removable){
    779                         opResult = initialize_non_removable(hub_info,port);
     817                unsigned int port;
     818                for (port = 1; port <= port_count; ++port) {
     819                        bool is_non_removable =
     820                                hub_info->not_initialized_non_removables[port/8]
     821                                & (1 << (port % 8));
     822                        if (is_non_removable) {
     823                                opResult = initialize_non_removable(hub_info,
     824                                        port);
     825                        }
    780826                }
    781827        }
    782         }
    783828
    784829
    785830        /// FIXME: count properly
    786         size_t byte_length = ((port_count+1) / 8) + 1;
    787                 void *change_bitmap = malloc(byte_length);
     831        size_t byte_length = ((port_count + 1) / 8) + 1;
     832        void *change_bitmap = malloc(byte_length);
    788833        size_t actual_size;
    789834
     
    792837         */
    793838        opResult = usb_pipe_read(
    794                         hub_info->status_change_pipe,
    795                         change_bitmap, byte_length, &actual_size
    796                         );
     839                hub_info->status_change_pipe,
     840                change_bitmap, byte_length, &actual_size
     841                );
    797842
    798843        if (opResult != EOK) {
    799844                free(change_bitmap);
    800                 usb_log_warning("something went wrong while getting status of hub\n");
     845                usb_log_warning("something went wrong while getting the"
     846                        "status of hub\n");
    801847                usb_pipe_end_session(hub_info->status_change_pipe);
    802848                return opResult;
    803849        }
    804850        unsigned int port;
    805        
    806         if(opResult!=EOK){
    807                 usb_log_error("could not start control pipe session %d\n", opResult);
     851
     852        if (opResult != EOK) {
     853                usb_log_error("could not start control pipe session %d\n",
     854                        opResult);
    808855                usb_pipe_end_session(hub_info->status_change_pipe);
    809856                return opResult;
    810857        }
    811858        opResult = usb_hc_connection_open(&hub_info->connection);
    812         if(opResult!=EOK){
     859        if (opResult != EOK) {
    813860                usb_log_error("could not start host controller session %d\n",
    814                                 opResult);
     861                        opResult);
    815862                usb_pipe_end_session(hub_info->control_pipe);
    816863                usb_pipe_end_session(hub_info->status_change_pipe);
     
    819866
    820867        ///todo, opresult check, pre obe konekce
    821         for (port = 1; port < port_count+1; ++port) {
     868        for (port = 1; port < port_count + 1; ++port) {
    822869                bool interrupt =
    823                                 (((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2;
     870                        (((uint8_t*) change_bitmap)[port / 8] >> (port % 8)) % 2;
    824871                if (interrupt) {
    825872                        usb_hub_process_interrupt(
    826                                 hub_info, port);
     873                                hub_info, port);
    827874                }
    828875        }
Note: See TracChangeset for help on using the changeset viewer.