Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/usbdev/src/devdrv.c

    r0c0f823b r882580a  
    4646
    4747static driver_ops_t generic_driver_ops = {
    48         .dev_add = generic_device_add,
     48        .add_device = generic_device_add,
    4949        .dev_remove = generic_device_remove,
    5050        .dev_gone = generic_device_gone,
     
    8181 * @return Number of pipes (excluding default control pipe).
    8282 */
    83 static inline size_t count_other_pipes(
    84     const usb_endpoint_description_t **endpoints)
    85 {
    86         size_t count;
    87         for (count = 0; endpoints && endpoints[count] != NULL; ++count);
     83static size_t count_other_pipes(const usb_endpoint_description_t **endpoints)
     84{
     85        size_t count = 0;
     86        if (endpoints == NULL) {
     87                return 0;
     88        }
     89
     90        while (endpoints[count] != NULL) {
     91                count++;
     92        }
     93
    8894        return count;
    8995}
     
    98104    usb_device_t *dev, int alternate_setting)
    99105{
    100         assert(dev);
    101 
    102106        if (endpoints == NULL) {
    103107                dev->pipes = NULL;
     
    296300
    297301        return rc;
    298 }
    299 
    300 /** Cleanup structure initialized via usb_device_retrieve_descriptors.
    301  *
    302  * @param[in] descriptors Where to store the descriptors.
    303  */
    304 void usb_device_release_descriptors(usb_device_descriptors_t *descriptors)
    305 {
    306         assert(descriptors);
    307         free(descriptors->configuration);
    308         descriptors->configuration = NULL;
    309302}
    310303
     
    326319 *      (not NULL terminated).
    327320 * @param[out] pipes_count_ptr Where to store number of pipes
    328  *      (set to NULL if you wish to ignore the count).
     321 *      (set to if you wish to ignore the count).
    329322 * @return Error code.
    330323 */
     
    347340        const size_t pipe_count = count_other_pipes(endpoints);
    348341        if (pipe_count == 0) {
    349                 if (pipes_count_ptr)
    350                         *pipes_count_ptr = pipe_count;
     342                *pipes_count_ptr = pipe_count;
    351343                *pipes_ptr = NULL;
    352344                return EOK;
     
    354346
    355347        usb_endpoint_mapping_t *pipes
    356             = calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
     348            = malloc(sizeof(usb_endpoint_mapping_t) * pipe_count);
    357349        if (pipes == NULL) {
    358350                return ENOMEM;
    359351        }
    360352
     353        /* Initialize to NULL to allow smooth rollback. */
     354        for (i = 0; i < pipe_count; i++) {
     355                pipes[i].pipe = NULL;
     356        }
     357
    361358        /* Now allocate and fully initialize. */
    362359        for (i = 0; i < pipe_count; i++) {
     360                pipes[i].pipe = malloc(sizeof(usb_pipe_t));
     361                if (pipes[i].pipe == NULL) {
     362                        rc = ENOMEM;
     363                        goto rollback_free_only;
     364                }
    363365                pipes[i].description = endpoints[i];
    364366                pipes[i].interface_no = interface_no;
     
    387389        for (i = 0; i < pipe_count; i++) {
    388390                if (pipes[i].present) {
    389                         rc = usb_pipe_register(&pipes[i].pipe,
     391                        rc = usb_pipe_register(pipes[i].pipe,
    390392                            pipes[i].descriptor->poll_interval, &hc_conn);
    391393                        if (rc != EOK) {
     
    396398
    397399        if (usb_hc_connection_close(&hc_conn) != EOK)
    398                 usb_log_warning("%s: Failed to close connection.\n",
    399                     __FUNCTION__);
     400                usb_log_warning("usb_device_create_pipes(): "
     401                    "Failed to close connection.\n");
    400402
    401403        *pipes_ptr = pipes;
     
    415417        for (i = 0; i < pipe_count; i++) {
    416418                if (pipes[i].present) {
    417                         usb_pipe_unregister(&pipes[i].pipe, &hc_conn);
     419                        usb_pipe_unregister(pipes[i].pipe, &hc_conn);
    418420                }
    419421        }
     
    429431         */
    430432rollback_free_only:
     433        for (i = 0; i < pipe_count; i++) {
     434                if (pipes[i].pipe != NULL) {
     435                        free(pipes[i].pipe);
     436                }
     437        }
    431438        free(pipes);
    432439
     
    470477                    i, pipes[i].present ? "" : "not ");
    471478                if (pipes[i].present)
    472                         usb_pipe_unregister(&pipes[i].pipe, &hc_conn);
     479                        usb_pipe_unregister(pipes[i].pipe, &hc_conn);
     480                free(pipes[i].pipe);
    473481        }
    474482
     
    481489        return EOK;
    482490}
     491
     492/** Initialize control pipe in a device.
     493 *
     494 * @param dev USB device in question.
     495 * @param errmsg Where to store error context.
     496 * @return
     497 */
     498static int init_wire_and_ctrl_pipe(usb_device_t *dev, const char **errmsg)
     499{
     500        int rc;
     501
     502        rc = usb_device_connection_initialize_from_device(&dev->wire,
     503            dev->ddf_dev);
     504        if (rc != EOK) {
     505                *errmsg = "device connection initialization";
     506                return rc;
     507        }
     508
     509        rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe,
     510            &dev->wire);
     511        if (rc != EOK) {
     512                *errmsg = "default control pipe initialization";
     513                return rc;
     514        }
     515
     516        return EOK;
     517}
     518
    483519
    484520/** Initialize new instance of USB device.
     
    497533        assert(ddf_dev != NULL);
    498534
    499         *errstr_ptr = NULL;
    500 
    501535        usb_dev->ddf_dev = ddf_dev;
    502536        usb_dev->driver_data = NULL;
    503537        usb_dev->descriptors.configuration = NULL;
     538        usb_dev->alternate_interfaces = NULL;
    504539        usb_dev->pipes_count = 0;
    505540        usb_dev->pipes = NULL;
    506541
    507542        /* Initialize backing wire and control pipe. */
    508         int rc = usb_device_connection_initialize_from_device(
    509             &usb_dev->wire, ddf_dev);
    510         if (rc != EOK) {
    511                 *errstr_ptr = "device connection initialization";
    512                 return rc;
    513         }
    514 
    515         /* This pipe was registered by the hub driver,
    516          * during device initialization. */
    517         rc = usb_pipe_initialize_default_control(&usb_dev->ctrl_pipe,
    518             &usb_dev->wire);
    519         if (rc != EOK) {
    520                 *errstr_ptr = "default control pipe initialization";
     543        int rc = init_wire_and_ctrl_pipe(usb_dev, errstr_ptr);
     544        if (rc != EOK) {
    521545                return rc;
    522546        }
     
    529553            &usb_dev->descriptors);
    530554        if (rc != EOK) {
     555                /* Nothing allocated, nothing to free. */
    531556                *errstr_ptr = "descriptor retrieval";
    532557                return rc;
    533558        }
    534559
    535         /* Create alternate interfaces. We will silently ignore failure.
    536          * We might either control one interface or an entire device,
    537          * it makes no sense to speak about alternate interfaces when
    538          * controlling a device. */
    539         rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
    540             usb_dev->descriptors.configuration,
    541             usb_dev->descriptors.configuration_size, usb_dev->interface_no);
    542         const int alternate_iface =
    543             (rc == EOK) ? usb_dev->alternate_interfaces.current : 0;
    544 
    545         /* TODO Add comment here. */
    546         rc = initialize_other_pipes(endpoints, usb_dev, alternate_iface);
     560        /* Create alternate interfaces. We will silently ignore failure. */
     561        //TODO Why ignore?
     562        usb_alternate_interfaces_create(usb_dev->descriptors.configuration,
     563            usb_dev->descriptors.configuration_size, usb_dev->interface_no,
     564            &usb_dev->alternate_interfaces);
     565
     566        rc = initialize_other_pipes(endpoints, usb_dev, 0);
    547567        if (rc != EOK) {
    548568                /* Full configuration descriptor is allocated. */
    549                 usb_device_release_descriptors(&usb_dev->descriptors);
     569                free(usb_dev->descriptors.configuration);
    550570                /* Alternate interfaces may be allocated */
    551                 usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
     571                usb_alternate_interfaces_destroy(usb_dev->alternate_interfaces);
    552572                *errstr_ptr = "pipes initialization";
    553573                return rc;
    554574        }
     575
     576        *errstr_ptr = NULL;
    555577
    556578        return EOK;
     
    569591                destroy_current_pipes(dev);
    570592
    571                 usb_alternate_interfaces_deinit(&dev->alternate_interfaces);
    572                 usb_device_release_descriptors(&dev->descriptors);
     593                usb_alternate_interfaces_destroy(dev->alternate_interfaces);
     594                free(dev->descriptors.configuration);
    573595                free(dev->driver_data);
    574596        }
Note: See TracChangeset for help on using the changeset viewer.