Ignore:
File:
1 edited

Legend:

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

    r6ab7f3e9 r49ce810  
    5656
    5757
    58 static usb_hub_info_t * usb_hub_info_create(usb_device_t *usb_dev);
    59 
    60 static int usb_hub_process_hub_specific_info(usb_hub_info_t *hub_info);
    61 
    62 static int usb_hub_set_configuration(usb_hub_info_t *hub_info);
    63 
    64 static int usb_hub_start_hub_fibril(usb_hub_info_t *hub_info);
    65 
    66 static int usb_process_hub_over_current(usb_hub_info_t *hub_info,
     58static usb_hub_info_t * usb_hub_info_create(usb_device_t * usb_dev);
     59
     60static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info);
     61
     62static int usb_hub_set_configuration(usb_hub_info_t * hub_info);
     63
     64static int usb_hub_start_hub_fibril(usb_hub_info_t * hub_info);
     65
     66static int usb_process_hub_over_current(usb_hub_info_t * hub_info,
    6767    usb_hub_status_t status);
    6868
    69 static int usb_process_hub_local_power_change(usb_hub_info_t *hub_info,
     69static int usb_process_hub_local_power_change(usb_hub_info_t * hub_info,
    7070    usb_hub_status_t status);
    7171
    72 static void usb_hub_process_global_interrupt(usb_hub_info_t *hub_info);
    73 
    74 static void usb_hub_polling_terminated_callback(usb_device_t *device,
    75     bool was_error, void *data);
     72static void usb_hub_process_global_interrupt(usb_hub_info_t * hub_info);
     73
     74static void usb_hub_polling_terminted_callback(usb_device_t * device,
     75    bool was_error, void * data);
    7676
    7777
     
    9090 * @return error code
    9191 */
    92 int usb_hub_add_device(usb_device_t *usb_dev) {
     92int usb_hub_add_device(usb_device_t * usb_dev) {
    9393        if (!usb_dev) return EINVAL;
    94         usb_hub_info_t *hub_info = usb_hub_info_create(usb_dev);
     94        usb_hub_info_t * hub_info = usb_hub_info_create(usb_dev);
    9595        //create hc connection
    9696        usb_log_debug("Initializing USB wire abstraction.\n");
     
    9999            hub_info->usb_device->ddf_dev);
    100100        if (opResult != EOK) {
    101                 usb_log_error("Could not initialize connection to device, "
    102                     " %s\n",
    103                     str_error(opResult));
     101                usb_log_error("could not initialize connection to device, "
     102                    "errno %d\n",
     103                    opResult);
    104104                free(hub_info);
    105105                return opResult;
     
    109109        opResult = usb_hub_set_configuration(hub_info);
    110110        if (opResult != EOK) {
    111                 usb_log_error("Could not set hub configuration, %s\n",
    112                     str_error(opResult));
     111                usb_log_error("could not set hub configuration, errno %d\n",
     112                    opResult);
    113113                free(hub_info);
    114114                return opResult;
     
    117117        opResult = usb_hub_process_hub_specific_info(hub_info);
    118118        if (opResult != EOK) {
    119                 usb_log_error("Could process hub specific info, %s\n",
    120                     str_error(opResult));
     119                usb_log_error("could process hub specific info, errno %d\n",
     120                    opResult);
    121121                free(hub_info);
    122122                return opResult;
     
    135135
    136136        opResult = usb_hub_start_hub_fibril(hub_info);
    137         if (opResult != EOK)
     137        if(opResult!=EOK)
    138138                free(hub_info);
    139139        return opResult;
    140140}
     141
    141142
    142143/** Callback for polling hub for changes.
     
    192193 * @return basic usb_hub_info_t structure
    193194 */
    194 static usb_hub_info_t * usb_hub_info_create(usb_device_t *usb_dev) {
    195         usb_hub_info_t * result = malloc(sizeof (usb_hub_info_t));
     195static usb_hub_info_t * usb_hub_info_create(usb_device_t * usb_dev) {
     196        usb_hub_info_t * result = malloc(sizeof(usb_hub_info_t));
    196197        if (!result) return NULL;
    197198        result->usb_device = usb_dev;
     
    199200        result->control_pipe = &usb_dev->ctrl_pipe;
    200201        result->is_default_address_used = false;
    201 
    202         result->ports = NULL;
    203         result->port_count = (size_t) - 1;
    204         fibril_mutex_initialize(&result->port_mutex);
    205 
    206         fibril_mutex_initialize(&result->pending_ops_mutex);
    207         fibril_condvar_initialize(&result->pending_ops_cv);
    208         result->pending_ops_count = 0;
    209202        return result;
    210203}
     
    220213 * @return error code
    221214 */
    222 static int usb_hub_process_hub_specific_info(usb_hub_info_t *hub_info) {
     215static int usb_hub_process_hub_specific_info(usb_hub_info_t * hub_info) {
    223216        // get hub descriptor
    224         usb_log_debug("Creating serialized descriptor\n");
     217        usb_log_debug("creating serialized descriptor\n");
    225218        uint8_t serialized_descriptor[USB_HUB_MAX_DESCRIPTOR_SIZE];
    226219        usb_hub_descriptor_t * descriptor;
     
    234227
    235228        if (opResult != EOK) {
    236                 usb_log_error("Failed when receiving hub descriptor, "
    237                     "%s\n",
    238                     str_error(opResult));
     229                usb_log_error("failed when receiving hub descriptor, "
     230                    "badcode = %d\n",
     231                    opResult);
    239232                free(serialized_descriptor);
    240233                return opResult;
    241234        }
    242         usb_log_debug2("Deserializing descriptor\n");
     235        usb_log_debug2("deserializing descriptor\n");
    243236        descriptor = usb_create_deserialized_hub_desriptor(
    244237            serialized_descriptor);
     
    251244        /// \TODO this is not semantically correct
    252245        bool is_power_switched =
    253             ((descriptor->hub_characteristics & 1) == 0);
     246            ((descriptor->hub_characteristics & 1) ==0);
    254247        bool has_individual_port_powering =
    255             ((descriptor->hub_characteristics & 1) != 0);
     248            ((descriptor->hub_characteristics & 1) !=0);
    256249        hub_info->ports = malloc(
    257250            sizeof (usb_hub_port_t) * (hub_info->port_count + 1));
    258         if (!hub_info->ports) {
     251        if(!hub_info->ports){
    259252                return ENOMEM;
    260253        }
     
    263256                usb_hub_port_init(&hub_info->ports[port]);
    264257        }
    265         if (is_power_switched) {
    266                 usb_log_debug("Hub power switched\n");
    267 
    268                 if (!has_individual_port_powering) {
    269                         usb_log_debug("Has_global powering\n");
     258        if(is_power_switched){
     259                usb_log_debug("is_power_switched\n");
     260               
     261                if(!has_individual_port_powering){
     262                        usb_log_debug("!has_individual_port_powering\n");
    270263                        opResult = usb_hub_set_feature(hub_info->control_pipe,
    271264                            USB_HUB_FEATURE_C_HUB_LOCAL_POWER);
    272265                        if (opResult != EOK) {
    273                                 usb_log_error("Cannot power hub: %s\n",
     266                                usb_log_error("cannot power hub: %s\n",
    274267                                    str_error(opResult));
    275268                        }
     
    277270
    278271                for (port = 1; port <= hub_info->port_count; ++port) {
    279                         usb_log_debug("Powering port %zu.\n", port);
     272                        usb_log_debug("Powering port %zu.\n",port);
    280273                        opResult = usb_hub_set_port_feature(hub_info->control_pipe,
    281274                            port, USB_HUB_FEATURE_PORT_POWER);
     
    285278                        }
    286279                }
    287 
    288         } else {
    289                 usb_log_debug("Power not switched, not going to be powered\n");
    290         }
    291         usb_log_debug2("Freeing data\n");
     280               
     281        }else{
     282                usb_log_debug("!is_power_switched, not going to be powered\n");
     283        }
     284        usb_log_debug2("freeing data\n");
    292285        free(descriptor);
    293286        return EOK;
     
    302295 * @return error code
    303296 */
    304 static int usb_hub_set_configuration(usb_hub_info_t *hub_info) {
     297static int usb_hub_set_configuration(usb_hub_info_t * hub_info) {
    305298        //device descriptor
    306299        usb_standard_device_descriptor_t *std_descriptor
    307300            = &hub_info->usb_device->descriptors.device;
    308         usb_log_debug("Hub has %d configurations\n",
     301        usb_log_debug("hub has %d configurations\n",
    309302            std_descriptor->configuration_count);
    310303        if (std_descriptor->configuration_count < 1) {
    311                 usb_log_error("There are no configurations available\n");
     304                usb_log_error("there are no configurations available\n");
    312305                return EINVAL;
    313306        }
     
    327320                return opResult;
    328321        }
    329         usb_log_debug("\tUsed configuration %d\n",
     322        usb_log_debug("\tused configuration %d\n",
    330323            config_descriptor->configuration_number);
    331324
     
    342335 * @return error code
    343336 */
    344 static int usb_hub_start_hub_fibril(usb_hub_info_t *hub_info) {
     337static int usb_hub_start_hub_fibril(usb_hub_info_t * hub_info){
    345338        int rc;
    346339
    347340        rc = usb_device_auto_poll(hub_info->usb_device, 0,
    348341            hub_port_changes_callback, ((hub_info->port_count + 1) / 8) + 1,
    349             usb_hub_polling_terminated_callback, hub_info);
     342            usb_hub_polling_terminted_callback, hub_info);
    350343        if (rc != EOK) {
    351344                usb_log_error("Failed to create polling fibril: %s.\n",
     
    366359//*********************************************
    367360
     361
    368362/**
    369363 * process hub over current change
     
    374368 * @return error code
    375369 */
    376 static int usb_process_hub_over_current(usb_hub_info_t *hub_info,
     370static int usb_process_hub_over_current(usb_hub_info_t * hub_info,
    377371    usb_hub_status_t status) {
    378372        int opResult;
    379         if (usb_hub_is_status(status, USB_HUB_FEATURE_HUB_OVER_CURRENT)) {
     373        if (usb_hub_is_status(status,USB_HUB_FEATURE_HUB_OVER_CURRENT)){
    380374                //poweroff all ports
    381375                unsigned int port;
    382                 for (port = 1; port <= hub_info->port_count; ++port) {
     376                for(port = 1;port <= hub_info->port_count;++port){
    383377                        opResult = usb_hub_clear_port_feature(
    384                             hub_info->control_pipe, port,
     378                            hub_info->control_pipe,port,
    385379                            USB_HUB_FEATURE_PORT_POWER);
    386380                        if (opResult != EOK) {
    387381                                usb_log_warning(
    388                                     "Cannot power off port %d;  %s\n",
    389                                     port, str_error(opResult));
     382                                    "cannot power off port %d;  %d\n",
     383                                    port, opResult);
    390384                        }
    391385                }
     
    393387                //power all ports
    394388                unsigned int port;
    395                 for (port = 1; port <= hub_info->port_count; ++port) {
     389                for(port = 1;port <= hub_info->port_count;++port){
    396390                        opResult = usb_hub_set_port_feature(
    397                             hub_info->control_pipe, port,
     391                            hub_info->control_pipe,port,
    398392                            USB_HUB_FEATURE_PORT_POWER);
    399393                        if (opResult != EOK) {
    400394                                usb_log_warning(
    401                                     "Cannot power off port %d;  %s\n",
    402                                     port, str_error(opResult));
     395                                    "cannot power off port %d;  %d\n",
     396                                    port, opResult);
    403397                        }
    404398                }
     
    415409 * @return error code
    416410 */
    417 static int usb_process_hub_local_power_change(usb_hub_info_t *hub_info,
     411static int usb_process_hub_local_power_change(usb_hub_info_t * hub_info,
    418412    usb_hub_status_t status) {
    419413        int opResult = EOK;
    420414        opResult = usb_hub_clear_feature(hub_info->control_pipe,
    421415            USB_HUB_FEATURE_C_HUB_LOCAL_POWER);
    422         if (opResult != EOK) {
    423                 usb_log_error("Cannnot clear hub power change flag: "
    424                     "%s\n",
    425                     str_error(opResult));
     416                if (opResult != EOK) {
     417                usb_log_error("cannnot clear hub power change flag: "
     418                    "%d\n",
     419                    opResult);
    426420        }
    427421        return opResult;
     
    435429 * @param hub_info hub instance
    436430 */
    437 static void usb_hub_process_global_interrupt(usb_hub_info_t *hub_info) {
    438         usb_log_debug("Global interrupt on a hub\n");
     431static void usb_hub_process_global_interrupt(usb_hub_info_t * hub_info) {
     432        usb_log_debug("global interrupt on a hub\n");
    439433        usb_pipe_t *pipe = hub_info->control_pipe;
    440434        int opResult;
     
    453447            );
    454448        if (opResult != EOK) {
    455                 usb_log_error("Could not get hub status: %s\n",
    456                     str_error(opResult));
     449                usb_log_error("could not get hub status\n");
    457450                return;
    458451        }
    459452        if (rcvd_size != sizeof (usb_port_status_t)) {
    460                 usb_log_error("Received status has incorrect size\n");
     453                usb_log_error("received status has incorrect size\n");
    461454                return;
    462455        }
    463456        //port reset
    464457        if (
    465             usb_hub_is_status(status, 16 + USB_HUB_FEATURE_C_HUB_OVER_CURRENT)) {
     458            usb_hub_is_status(status,16+USB_HUB_FEATURE_C_HUB_OVER_CURRENT)) {
    466459                usb_process_hub_over_current(hub_info, status);
    467460        }
    468461        if (
    469             usb_hub_is_status(status, 16 + USB_HUB_FEATURE_C_HUB_LOCAL_POWER)) {
     462            usb_hub_is_status(status,16+USB_HUB_FEATURE_C_HUB_LOCAL_POWER)) {
    470463                usb_process_hub_local_power_change(hub_info, status);
    471464        }
     
    480473 * @param data pointer to usb_hub_info_t structure
    481474 */
    482 static void usb_hub_polling_terminated_callback(usb_device_t *device,
    483     bool was_error, void *data) {
    484         usb_hub_info_t * hub = data;
    485         assert(hub);
    486 
    487         fibril_mutex_lock(&hub->pending_ops_mutex);
    488 
    489         /* The device is dead. However there might be some pending operations
    490          * that we need to wait for.
    491          * One of them is device adding in progress.
    492          * The respective fibril is probably waiting for status change
    493          * in port reset (port enable) callback.
    494          * Such change would never come (otherwise we would not be here).
    495          * Thus, we would flush all pending port resets.
    496          */
    497         if (hub->pending_ops_count > 0) {
    498                 fibril_mutex_lock(&hub->port_mutex);
    499                 size_t port;
    500                 for (port = 0; port < hub->port_count; port++) {
    501                         usb_hub_port_t *the_port = hub->ports + port;
    502                         fibril_mutex_lock(&the_port->reset_mutex);
    503                         the_port->reset_completed = true;
    504                         the_port->reset_okay = false;
    505                         fibril_condvar_broadcast(&the_port->reset_cv);
    506                         fibril_mutex_unlock(&the_port->reset_mutex);
    507                 }
    508                 fibril_mutex_unlock(&hub->port_mutex);
    509         }
    510         /* And now wait for them. */
    511         while (hub->pending_ops_count > 0) {
    512                 fibril_condvar_wait(&hub->pending_ops_cv,
    513                     &hub->pending_ops_mutex);
    514         }
    515         fibril_mutex_unlock(&hub->pending_ops_mutex);
    516 
    517         free(hub->ports);
    518         free(hub);
     475static void usb_hub_polling_terminted_callback(usb_device_t * device,
     476    bool was_error, void * data){
     477        usb_hub_info_t * hub_info = data;
     478        if(!hub_info) return;
     479        free(hub_info->ports);
     480        free(hub_info);
    519481}
    520482
Note: See TracChangeset for help on using the changeset viewer.