Ignore:
File:
1 edited

Legend:

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

    rb803845 r9d58539  
    11/*
    22 * Copyright (c) 2011 Vojtech Horky
     3 * Copyright (c) 2011 Jan Vesely
    34 * All rights reserved.
    45 *
     
    2627 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2728 */
    28 
    2929/** @addtogroup libusbdev
    3030 * @{
     
    3636#include <usb/dev/request.h>
    3737#include <usb/debug.h>
    38 #include <usb/dev/dp.h>
     38#include <usb/dev.h>
    3939#include <errno.h>
    4040#include <str_error.h>
     
    4646
    4747static driver_ops_t generic_driver_ops = {
    48         .add_device = generic_device_add,
     48        .dev_add = generic_device_add,
    4949        .dev_remove = generic_device_remove,
    5050        .dev_gone = generic_device_gone,
     
    5656static const usb_driver_t *driver = NULL;
    5757
    58 
    5958/** Main routine of USB device driver.
    6059 *
     
    6463 * @return Task exit status.
    6564 */
    66 int usb_driver_main(usb_driver_t *drv)
     65int usb_driver_main(const usb_driver_t *drv)
    6766{
    6867        assert(drv != NULL);
     
    7574        return ddf_driver_main(&generic_driver);
    7675}
    77 
     76/*----------------------------------------------------------------------------*/
    7877/** Count number of pipes the driver expects.
    7978 *
     
    8180 * @return Number of pipes (excluding default control pipe).
    8281 */
    83 static 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 
     82static inline size_t count_other_pipes(
     83    const usb_endpoint_description_t **endpoints)
     84{
     85        size_t count;
     86        for (count = 0; endpoints != NULL && endpoints[count] != NULL; ++count);
    9487        return count;
    95 }
    96 
    97 /** Initialize endpoint pipes, excluding default control one.
    98  *
    99  * @param drv The device driver.
    100  * @param dev Device to be initialized.
    101  * @return Error code.
    102  */
    103 static int initialize_other_pipes(const usb_endpoint_description_t **endpoints,
    104     usb_device_t *dev, int alternate_setting)
    105 {
    106         if (endpoints == NULL) {
    107                 dev->pipes = NULL;
    108                 dev->pipes_count = 0;
    109                 return EOK;
    110         }
    111 
    112         usb_endpoint_mapping_t *pipes;
    113         size_t pipes_count;
    114 
    115         int rc = usb_device_create_pipes(dev->ddf_dev, &dev->wire, endpoints,
    116             dev->descriptors.configuration, dev->descriptors.configuration_size,
    117             dev->interface_no, alternate_setting, &pipes, &pipes_count);
    118 
    119         if (rc != EOK) {
    120                 return rc;
    121         }
    122 
    123         dev->pipes = pipes;
    124         dev->pipes_count = pipes_count;
    125 
    126         return EOK;
    12788}
    12889/*----------------------------------------------------------------------------*/
     
    140101        assert(driver->ops->device_add);
    141102
    142         int rc;
    143 
    144         usb_device_t *dev = NULL;
     103        /* Get place for driver data. */
     104        usb_device_t *dev = ddf_dev_data_alloc(gen_dev, sizeof(usb_device_t));
     105        if (dev == NULL) {
     106                usb_log_error("USB device `%s' structure allocation failed.\n",
     107                    gen_dev->name);
     108                return ENOMEM;
     109        }
     110
     111        /* Initialize generic USB driver data. */
    145112        const char *err_msg = NULL;
    146         rc = usb_device_create(gen_dev, driver->endpoints, &dev, &err_msg);
    147         if (rc != EOK) {
    148                 usb_log_error("USB device `%s' creation failed (%s): %s.\n",
     113        int rc = usb_device_init(dev, gen_dev, driver->endpoints, &err_msg);
     114        if (rc != EOK) {
     115                usb_log_error("USB device `%s' init failed (%s): %s.\n",
    149116                    gen_dev->name, err_msg, str_error(rc));
    150117                return rc;
    151118        }
    152         gen_dev->driver_data = dev;
    153 
     119
     120        /* Start USB driver specific initialization. */
    154121        rc = driver->ops->device_add(dev);
    155122        if (rc != EOK)
     
    171138        if (driver->ops->device_rem == NULL)
    172139                return ENOTSUP;
    173         /* Just tell the driver to stop whatever it is doing, keep structures */
    174         return driver->ops->device_rem(gen_dev->driver_data);
     140        /* Just tell the driver to stop whatever it is doing */
     141        usb_device_t *usb_dev = gen_dev->driver_data;
     142        const int ret = driver->ops->device_rem(usb_dev);
     143        if (ret != EOK)
     144                return ret;
     145        usb_device_deinit(usb_dev);
     146        return EOK;
    175147}
    176148/*----------------------------------------------------------------------------*/
     
    199171 *
    200172 * @param dev Device where to destroy the pipes.
    201  * @return Error code.
    202  */
    203 static int destroy_current_pipes(usb_device_t *dev)
    204 {
    205         int rc = usb_device_destroy_pipes(dev->ddf_dev,
    206             dev->pipes, dev->pipes_count);
    207         if (rc != EOK) {
    208                 return rc;
    209         }
    210 
     173 */
     174static void destroy_current_pipes(usb_device_t *dev)
     175{
     176        usb_device_destroy_pipes(dev->pipes, dev->pipes_count);
    211177        dev->pipes = NULL;
    212178        dev->pipes_count = 0;
    213 
    214         return EOK;
    215 }
    216 
     179}
     180/*----------------------------------------------------------------------------*/
    217181/** Change interface setting of a device.
    218182 * This function selects new alternate setting of an interface by issuing
     
    244208        }
    245209
    246         int rc;
    247 
    248210        /* Destroy existing pipes. */
    249         rc = destroy_current_pipes(dev);
    250         if (rc != EOK) {
    251                 return rc;
    252         }
     211        destroy_current_pipes(dev);
    253212
    254213        /* Change the interface itself. */
    255         rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,
     214        int rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,
    256215            alternate_setting);
    257216        if (rc != EOK) {
     
    260219
    261220        /* Create new pipes. */
    262         rc = initialize_other_pipes(endpoints, dev, (int) alternate_setting);
     221        rc = usb_device_create_pipes(&dev->wire, endpoints,
     222            dev->descriptors.configuration, dev->descriptors.configuration_size,
     223            dev->interface_no, (int)alternate_setting,
     224            &dev->pipes, &dev->pipes_count);
    263225
    264226        return rc;
     
    300262}
    301263
     264/** Cleanup structure initialized via usb_device_retrieve_descriptors.
     265 *
     266 * @param[in] descriptors Where to store the descriptors.
     267 */
     268void usb_device_release_descriptors(usb_device_descriptors_t *descriptors)
     269{
     270        assert(descriptors);
     271        free(descriptors->configuration);
     272        descriptors->configuration = NULL;
     273}
     274
    302275/** Create pipes for a device.
    303276 *
     
    307280 * - registers endpoints with the host controller
    308281 *
    309  * @param[in] dev Generic DDF device backing the USB one.
    310282 * @param[in] wire Initialized backing connection to the host controller.
    311283 * @param[in] endpoints Endpoints description, NULL terminated.
     
    317289 *      (not NULL terminated).
    318290 * @param[out] pipes_count_ptr Where to store number of pipes
    319  *      (set to if you wish to ignore the count).
    320  * @return Error code.
    321  */
    322 int usb_device_create_pipes(const ddf_dev_t *dev, usb_device_connection_t *wire,
     291 *      (set to NULL if you wish to ignore the count).
     292 * @return Error code.
     293 */
     294int usb_device_create_pipes(usb_device_connection_t *wire,
    323295    const usb_endpoint_description_t **endpoints,
    324296    const uint8_t *config_descr, size_t config_descr_size,
     
    326298    usb_endpoint_mapping_t **pipes_ptr, size_t *pipes_count_ptr)
    327299{
    328         assert(dev != NULL);
    329300        assert(wire != NULL);
    330         assert(endpoints != NULL);
    331301        assert(config_descr != NULL);
    332302        assert(config_descr_size > 0);
     
    338308        const size_t pipe_count = count_other_pipes(endpoints);
    339309        if (pipe_count == 0) {
    340                 *pipes_count_ptr = pipe_count;
     310                if (pipes_count_ptr)
     311                        *pipes_count_ptr = pipe_count;
    341312                *pipes_ptr = NULL;
    342313                return EOK;
     
    344315
    345316        usb_endpoint_mapping_t *pipes
    346             = malloc(sizeof(usb_endpoint_mapping_t) * pipe_count);
     317            = calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
    347318        if (pipes == NULL) {
    348319                return ENOMEM;
    349320        }
    350321
    351         /* Initialize to NULL to allow smooth rollback. */
     322        /* Now initialize. */
    352323        for (i = 0; i < pipe_count; i++) {
    353                 pipes[i].pipe = NULL;
    354         }
    355 
    356         /* Now allocate and fully initialize. */
    357         for (i = 0; i < pipe_count; i++) {
    358                 pipes[i].pipe = malloc(sizeof(usb_pipe_t));
    359                 if (pipes[i].pipe == NULL) {
    360                         rc = ENOMEM;
    361                         goto rollback_free_only;
    362                 }
    363324                pipes[i].description = endpoints[i];
    364325                pipes[i].interface_no = interface_no;
     
    370331            config_descr, config_descr_size, wire);
    371332        if (rc != EOK) {
    372                 goto rollback_free_only;
    373         }
    374 
    375         /* Register the endpoints with HC. */
    376         usb_hc_connection_t hc_conn;
    377         rc = usb_hc_connection_initialize_from_device(&hc_conn, dev);
    378         if (rc != EOK) {
    379                 goto rollback_free_only;
    380         }
    381 
    382         rc = usb_hc_connection_open(&hc_conn);
    383         if (rc != EOK) {
    384                 goto rollback_free_only;
    385         }
    386 
     333                free(pipes);
     334                return rc;
     335        }
     336
     337        /* Register created pipes. */
    387338        for (i = 0; i < pipe_count; i++) {
    388339                if (pipes[i].present) {
    389                         rc = usb_pipe_register(pipes[i].pipe,
    390                             pipes[i].descriptor->poll_interval, &hc_conn);
     340                        rc = usb_pipe_register(&pipes[i].pipe,
     341                            pipes[i].descriptor->poll_interval);
    391342                        if (rc != EOK) {
    392343                                goto rollback_unregister_endpoints;
     
    394345                }
    395346        }
    396 
    397         if (usb_hc_connection_close(&hc_conn) != EOK)
    398                 usb_log_warning("usb_device_create_pipes(): "
    399                     "Failed to close connection.\n");
    400347
    401348        *pipes_ptr = pipes;
     
    415362        for (i = 0; i < pipe_count; i++) {
    416363                if (pipes[i].present) {
    417                         usb_pipe_unregister(pipes[i].pipe, &hc_conn);
     364                        usb_pipe_unregister(&pipes[i].pipe);
    418365                }
    419366        }
    420367
    421         if (usb_hc_connection_close(&hc_conn) != EOK)
    422                 usb_log_warning("usb_device_create_pipes(): "
    423                     "Failed to close connection.\n");
    424 
    425         /*
    426          * Jump here if something went wrong before some actual communication
    427          * with HC. Then the only thing that needs to be done is to free
    428          * allocated memory.
    429          */
    430 rollback_free_only:
    431         for (i = 0; i < pipe_count; i++) {
    432                 if (pipes[i].pipe != NULL) {
    433                         free(pipes[i].pipe);
    434                 }
    435         }
    436368        free(pipes);
    437 
    438369        return rc;
    439370}
     
    441372/** Destroy pipes previously created by usb_device_create_pipes.
    442373 *
    443  * @param[in] dev Generic DDF device backing the USB one.
    444374 * @param[in] pipes Endpoint mapping to be destroyed.
    445375 * @param[in] pipes_count Number of endpoints.
    446376 */
    447 int usb_device_destroy_pipes(const ddf_dev_t *dev,
    448     usb_endpoint_mapping_t *pipes, size_t pipes_count)
    449 {
    450         assert(dev != NULL);
    451 
    452         if (pipes_count == 0) {
    453                 assert(pipes == NULL);
    454                 return EOK;
    455         }
    456         assert(pipes != NULL);
    457 
    458         int rc;
    459 
    460         /* Prepare connection to HC to allow endpoint unregistering. */
    461         usb_hc_connection_t hc_conn;
    462         rc = usb_hc_connection_initialize_from_device(&hc_conn, dev);
    463         if (rc != EOK) {
    464                 return rc;
    465         }
    466         rc = usb_hc_connection_open(&hc_conn);
    467         if (rc != EOK) {
    468                 return rc;
    469         }
    470 
     377void usb_device_destroy_pipes(usb_endpoint_mapping_t *pipes, size_t pipes_count)
     378{
    471379        /* Destroy the pipes. */
    472         size_t i;
    473         for (i = 0; i < pipes_count; i++) {
    474                 usb_log_debug2("Unregistering pipe %zu (%spresent).\n",
     380        for (size_t i = 0; i < pipes_count; ++i) {
     381                assert(pipes);
     382                usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
    475383                    i, pipes[i].present ? "" : "not ");
    476384                if (pipes[i].present)
    477                         usb_pipe_unregister(pipes[i].pipe, &hc_conn);
    478                 free(pipes[i].pipe);
    479         }
    480 
    481         if (usb_hc_connection_close(&hc_conn) != EOK)
    482                 usb_log_warning("usb_device_destroy_pipes(): "
    483                     "Failed to close connection.\n");
    484 
     385                        usb_pipe_unregister(&pipes[i].pipe);
     386        }
    485387        free(pipes);
    486 
    487         return EOK;
    488 }
    489 
    490 /** Initialize control pipe in a device.
    491  *
    492  * @param dev USB device in question.
    493  * @param errmsg Where to store error context.
    494  * @return
    495  */
    496 static int init_wire_and_ctrl_pipe(usb_device_t *dev, const char **errmsg)
    497 {
    498         int rc;
    499 
    500         rc = usb_device_connection_initialize_from_device(&dev->wire,
    501             dev->ddf_dev);
    502         if (rc != EOK) {
    503                 *errmsg = "device connection initialization";
    504                 return rc;
    505         }
    506 
    507         rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe,
    508             &dev->wire);
    509         if (rc != EOK) {
    510                 *errmsg = "default control pipe initialization";
    511                 return rc;
    512         }
    513 
    514         return EOK;
    515 }
    516 
    517 
    518 /** Create new instance of USB device.
    519  *
     388}
     389
     390/** Initialize new instance of USB device.
     391 *
     392 * @param[in] usb_dev Pointer to the new device.
    520393 * @param[in] ddf_dev Generic DDF device backing the USB one.
    521394 * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
    522  * @param[out] dev_ptr Where to store pointer to the new device.
    523395 * @param[out] errstr_ptr Where to store description of context
    524396 *      (in case error occurs).
    525397 * @return Error code.
    526398 */
    527 int usb_device_create(ddf_dev_t *ddf_dev,
    528     const usb_endpoint_description_t **endpoints,
    529     usb_device_t **dev_ptr, const char **errstr_ptr)
    530 {
    531         assert(dev_ptr != NULL);
     399int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
     400    const usb_endpoint_description_t **endpoints, const char **errstr_ptr)
     401{
     402        assert(usb_dev != NULL);
    532403        assert(ddf_dev != NULL);
    533404
    534         int rc;
    535 
    536         usb_device_t *dev = malloc(sizeof(usb_device_t));
    537         if (dev == NULL) {
    538                 *errstr_ptr = "structure allocation";
    539                 return ENOMEM;
    540         }
    541 
    542         // FIXME: proper deallocation in case of errors
    543 
    544         dev->ddf_dev = ddf_dev;
    545         dev->driver_data = NULL;
    546         dev->descriptors.configuration = NULL;
    547         dev->alternate_interfaces = NULL;
    548 
    549         dev->pipes_count = 0;
    550         dev->pipes = NULL;
     405        *errstr_ptr = NULL;
     406
     407        usb_dev->ddf_dev = ddf_dev;
     408        usb_dev->driver_data = NULL;
     409        usb_dev->descriptors.configuration = NULL;
     410        usb_dev->pipes_count = 0;
     411        usb_dev->pipes = NULL;
     412
     413        /* Get assigned params */
     414        devman_handle_t hc_handle;
     415        usb_address_t address;
     416
     417        int rc = usb_get_info_by_handle(ddf_dev->handle,
     418            &hc_handle, &address, &usb_dev->interface_no);
     419        if (rc != EOK) {
     420                *errstr_ptr = "device parameters retrieval";
     421                return rc;
     422        }
     423
     424        /* Initialize hc connection. */
     425        usb_hc_connection_initialize(&usb_dev->hc_conn, hc_handle);
    551426
    552427        /* Initialize backing wire and control pipe. */
    553         rc = init_wire_and_ctrl_pipe(dev, errstr_ptr);
    554         if (rc != EOK) {
    555                 return rc;
    556         }
    557 
    558         /* Get our interface. */
    559         dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev);
     428        rc = usb_device_connection_initialize(
     429            &usb_dev->wire, &usb_dev->hc_conn, address);
     430        if (rc != EOK) {
     431                *errstr_ptr = "device connection initialization";
     432                return rc;
     433        }
     434
     435        /* This pipe was registered by the hub driver,
     436         * during device initialization. */
     437        rc = usb_pipe_initialize_default_control(
     438            &usb_dev->ctrl_pipe, &usb_dev->wire);
     439        if (rc != EOK) {
     440                *errstr_ptr = "default control pipe initialization";
     441                return rc;
     442        }
     443
     444        /* Open hc connection for pipe registration. */
     445        rc = usb_hc_connection_open(&usb_dev->hc_conn);
     446        if (rc != EOK) {
     447                *errstr_ptr = "hc connection open";
     448                return rc;
     449        }
    560450
    561451        /* Retrieve standard descriptors. */
    562         rc = usb_device_retrieve_descriptors(&dev->ctrl_pipe,
    563             &dev->descriptors);
     452        rc = usb_device_retrieve_descriptors(
     453            &usb_dev->ctrl_pipe, &usb_dev->descriptors);
    564454        if (rc != EOK) {
    565455                *errstr_ptr = "descriptor retrieval";
    566                 return rc;
    567         }
    568 
    569         /* Create alternate interfaces. */
    570         rc = usb_alternate_interfaces_create(dev->descriptors.configuration,
    571             dev->descriptors.configuration_size, dev->interface_no,
    572             &dev->alternate_interfaces);
    573         if (rc != EOK) {
    574                 /* We will try to silently ignore this. */
    575                 dev->alternate_interfaces = NULL;
    576         }
    577 
    578         rc = initialize_other_pipes(endpoints, dev, 0);
    579         if (rc != EOK) {
     456                usb_hc_connection_close(&usb_dev->hc_conn);
     457                return rc;
     458        }
     459
     460        /* Create alternate interfaces. We will silently ignore failure.
     461         * We might either control one interface or an entire device,
     462         * it makes no sense to speak about alternate interfaces when
     463         * controlling a device. */
     464        rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
     465            usb_dev->descriptors.configuration,
     466            usb_dev->descriptors.configuration_size, usb_dev->interface_no);
     467        const int alternate_iface =
     468            (rc == EOK) ? usb_dev->alternate_interfaces.current : 0;
     469
     470        /* Create and register other pipes than default control (EP 0) */
     471        rc = usb_device_create_pipes(&usb_dev->wire, endpoints,
     472            usb_dev->descriptors.configuration,
     473            usb_dev->descriptors.configuration_size,
     474            usb_dev->interface_no, (int)alternate_iface,
     475            &usb_dev->pipes, &usb_dev->pipes_count);
     476        if (rc != EOK) {
     477                usb_hc_connection_close(&usb_dev->hc_conn);
     478                /* Full configuration descriptor is allocated. */
     479                usb_device_release_descriptors(&usb_dev->descriptors);
     480                /* Alternate interfaces may be allocated */
     481                usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
    580482                *errstr_ptr = "pipes initialization";
    581483                return rc;
    582484        }
    583485
    584         *errstr_ptr = NULL;
    585         *dev_ptr = dev;
    586 
     486        usb_hc_connection_close(&usb_dev->hc_conn);
    587487        return EOK;
    588488}
    589489
    590 /** Destroy instance of a USB device.
     490/** Clean instance of a USB device.
    591491 *
    592492 * @param dev Device to be de-initialized.
     
    596496void usb_device_deinit(usb_device_t *dev)
    597497{
    598         if (dev == NULL) {
    599                 return;
    600         }
    601 
    602         /* Ignore errors and hope for the best. */
    603         destroy_current_pipes(dev);
    604 
    605         if (dev->alternate_interfaces != NULL) {
    606                 free(dev->alternate_interfaces->alternatives);
    607         }
    608         free(dev->alternate_interfaces);
    609         free(dev->descriptors.configuration);
    610         free(dev->driver_data);
    611 }
    612 
     498        if (dev) {
     499                /* Destroy existing pipes. */
     500                destroy_current_pipes(dev);
     501                /* Ignore errors and hope for the best. */
     502                usb_hc_connection_deinitialize(&dev->hc_conn);
     503                usb_alternate_interfaces_deinit(&dev->alternate_interfaces);
     504                usb_device_release_descriptors(&dev->descriptors);
     505                free(dev->driver_data);
     506                dev->driver_data = NULL;
     507        }
     508}
     509
     510/** Allocate driver specific data.
     511 * @param usb_dev usb_device structure.
     512 * @param size requested data size.
     513 * @return Pointer to the newly allocated space, NULL on failure.
     514 */
    613515void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
    614516{
Note: See TracChangeset for help on using the changeset viewer.