Ignore:
File:
1 edited

Legend:

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

    rb77931d r4291215  
    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 *
     
    7574        return ddf_driver_main(&generic_driver);
    7675}
    77 
     76/*----------------------------------------------------------------------------*/
    7877/** Count number of pipes the driver expects.
    7978 *
     
    8584{
    8685        size_t count;
    87         for (count = 0; endpoints && endpoints[count] != NULL; ++count);
     86        for (count = 0; endpoints != NULL && endpoints[count] != NULL; ++count);
    8887        return count;
    89 }
    90 
    91 /** Initialize endpoint pipes, excluding default control one.
    92  *
    93  * @param drv The device driver.
    94  * @param dev Device to be initialized.
    95  * @return Error code.
    96  */
    97 static int initialize_other_pipes(const usb_endpoint_description_t **endpoints,
    98     usb_device_t *dev, int alternate_setting)
    99 {
    100         assert(dev);
    101 
    102         if (endpoints == NULL) {
    103                 dev->pipes = NULL;
    104                 dev->pipes_count = 0;
    105                 return EOK;
    106         }
    107 
    108         usb_endpoint_mapping_t *pipes;
    109         size_t pipes_count;
    110 
    111         int rc = usb_device_create_pipes(dev->ddf_dev, &dev->wire, endpoints,
    112             dev->descriptors.configuration, dev->descriptors.configuration_size,
    113             dev->interface_no, alternate_setting, &pipes, &pipes_count);
    114 
    115         if (rc != EOK) {
    116                 return rc;
    117         }
    118 
    119         dev->pipes = pipes;
    120         dev->pipes_count = pipes_count;
    121 
    122         return EOK;
    12388}
    12489/*----------------------------------------------------------------------------*/
     
    136101        assert(driver->ops->device_add);
    137102
     103        /* Get place for driver data. */
    138104        usb_device_t *dev = ddf_dev_data_alloc(gen_dev, sizeof(usb_device_t));
    139105        if (dev == NULL) {
     
    142108                return ENOMEM;
    143109        }
     110
     111        /* Initialize generic USB driver data. */
    144112        const char *err_msg = NULL;
    145113        int rc = usb_device_init(dev, gen_dev, driver->endpoints, &err_msg);
     
    150118        }
    151119
     120        /* Start USB driver specific initialization. */
    152121        rc = driver->ops->device_add(dev);
    153122        if (rc != EOK)
     
    169138        if (driver->ops->device_rem == NULL)
    170139                return ENOTSUP;
    171         /* Just tell the driver to stop whatever it is doing, keep structures */
    172         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;
    173147}
    174148/*----------------------------------------------------------------------------*/
     
    197171 *
    198172 * @param dev Device where to destroy the pipes.
    199  * @return Error code.
    200  */
    201 static int destroy_current_pipes(usb_device_t *dev)
    202 {
    203         int rc = usb_device_destroy_pipes(dev->ddf_dev,
    204             dev->pipes, dev->pipes_count);
    205         if (rc != EOK) {
    206                 return rc;
    207         }
    208 
     173 */
     174static void destroy_current_pipes(usb_device_t *dev)
     175{
     176        usb_device_destroy_pipes(dev->pipes, dev->pipes_count);
    209177        dev->pipes = NULL;
    210178        dev->pipes_count = 0;
    211 
    212         return EOK;
    213 }
    214 
     179}
     180/*----------------------------------------------------------------------------*/
    215181/** Change interface setting of a device.
    216182 * This function selects new alternate setting of an interface by issuing
     
    242208        }
    243209
    244         int rc;
    245 
    246210        /* Destroy existing pipes. */
    247         rc = destroy_current_pipes(dev);
    248         if (rc != EOK) {
    249                 return rc;
    250         }
     211        destroy_current_pipes(dev);
    251212
    252213        /* Change the interface itself. */
    253         rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,
     214        int rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,
    254215            alternate_setting);
    255216        if (rc != EOK) {
     
    258219
    259220        /* Create new pipes. */
    260         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);
    261225
    262226        return rc;
     
    316280 * - registers endpoints with the host controller
    317281 *
    318  * @param[in] dev Generic DDF device backing the USB one.
    319282 * @param[in] wire Initialized backing connection to the host controller.
    320283 * @param[in] endpoints Endpoints description, NULL terminated.
     
    329292 * @return Error code.
    330293 */
    331 int usb_device_create_pipes(const ddf_dev_t *dev, usb_device_connection_t *wire,
     294int usb_device_create_pipes(usb_device_connection_t *wire,
    332295    const usb_endpoint_description_t **endpoints,
    333296    const uint8_t *config_descr, size_t config_descr_size,
     
    335298    usb_endpoint_mapping_t **pipes_ptr, size_t *pipes_count_ptr)
    336299{
    337         assert(dev != NULL);
    338300        assert(wire != NULL);
    339         assert(endpoints != NULL);
    340301        assert(config_descr != NULL);
    341302        assert(config_descr_size > 0);
     
    359320        }
    360321
    361         /* Now allocate and fully initialize. */
     322        /* Now initialize. */
    362323        for (i = 0; i < pipe_count; i++) {
    363324                pipes[i].description = endpoints[i];
     
    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) {
    389340                        rc = usb_pipe_register(&pipes[i].pipe,
    390                             pipes[i].descriptor->poll_interval, &hc_conn);
     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("%s: Failed to close connection.\n",
    399                     __FUNCTION__);
    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:
    431368        free(pipes);
    432 
    433369        return rc;
    434370}
     
    436372/** Destroy pipes previously created by usb_device_create_pipes.
    437373 *
    438  * @param[in] dev Generic DDF device backing the USB one.
    439374 * @param[in] pipes Endpoint mapping to be destroyed.
    440375 * @param[in] pipes_count Number of endpoints.
    441376 */
    442 int usb_device_destroy_pipes(const ddf_dev_t *dev,
    443     usb_endpoint_mapping_t *pipes, size_t pipes_count)
    444 {
    445         assert(dev != NULL);
    446 
    447         if (pipes_count == 0) {
    448                 assert(pipes == NULL);
    449                 return EOK;
    450         }
    451         assert(pipes != NULL);
    452 
    453         int rc;
    454 
    455         /* Prepare connection to HC to allow endpoint unregistering. */
    456         usb_hc_connection_t hc_conn;
    457         rc = usb_hc_connection_initialize_from_device(&hc_conn, dev);
    458         if (rc != EOK) {
    459                 return rc;
    460         }
    461         rc = usb_hc_connection_open(&hc_conn);
    462         if (rc != EOK) {
    463                 return rc;
    464         }
    465 
     377void usb_device_destroy_pipes(usb_endpoint_mapping_t *pipes, size_t pipes_count)
     378{
    466379        /* Destroy the pipes. */
    467         size_t i;
    468         for (i = 0; i < pipes_count; i++) {
    469                 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",
    470383                    i, pipes[i].present ? "" : "not ");
    471384                if (pipes[i].present)
    472                         usb_pipe_unregister(&pipes[i].pipe, &hc_conn);
    473         }
    474 
    475         if (usb_hc_connection_close(&hc_conn) != EOK)
    476                 usb_log_warning("usb_device_destroy_pipes(): "
    477                     "Failed to close connection.\n");
    478 
     385                        usb_pipe_unregister(&pipes[i].pipe);
     386        }
    479387        free(pipes);
    480 
    481         return EOK;
    482388}
    483389
     
    505411        usb_dev->pipes = NULL;
    506412
     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);
     426
    507427        /* Initialize backing wire and control pipe. */
    508         int rc = usb_device_connection_initialize_from_device(
    509             &usb_dev->wire, ddf_dev);
     428        rc = usb_device_connection_initialize(
     429            &usb_dev->wire, &usb_dev->hc_conn, address);
    510430        if (rc != EOK) {
    511431                *errstr_ptr = "device connection initialization";
     
    515435        /* This pipe was registered by the hub driver,
    516436         * during device initialization. */
    517         rc = usb_pipe_initialize_default_control(&usb_dev->ctrl_pipe,
    518             &usb_dev->wire);
     437        rc = usb_pipe_initialize_default_control(
     438            &usb_dev->ctrl_pipe, &usb_dev->wire);
    519439        if (rc != EOK) {
    520440                *errstr_ptr = "default control pipe initialization";
     
    522442        }
    523443
    524         /* Get our interface. */
    525         usb_dev->interface_no = usb_device_get_assigned_interface(ddf_dev);
     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        }
    526450
    527451        /* Retrieve standard descriptors. */
    528         rc = usb_device_retrieve_descriptors(&usb_dev->ctrl_pipe,
    529             &usb_dev->descriptors);
     452        rc = usb_device_retrieve_descriptors(
     453            &usb_dev->ctrl_pipe, &usb_dev->descriptors);
    530454        if (rc != EOK) {
    531455                *errstr_ptr = "descriptor retrieval";
     456                usb_hc_connection_close(&usb_dev->hc_conn);
    532457                return rc;
    533458        }
     
    543468            (rc == EOK) ? usb_dev->alternate_interfaces.current : 0;
    544469
    545         /* TODO Add comment here. */
    546         rc = initialize_other_pipes(endpoints, usb_dev, alternate_iface);
    547         if (rc != EOK) {
     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);
    548478                /* Full configuration descriptor is allocated. */
    549479                usb_device_release_descriptors(&usb_dev->descriptors);
     
    554484        }
    555485
     486        usb_hc_connection_close(&usb_dev->hc_conn);
    556487        return EOK;
    557488}
     
    566497{
    567498        if (dev) {
     499                /* Destroy existing pipes. */
     500                destroy_current_pipes(dev);
    568501                /* Ignore errors and hope for the best. */
    569                 destroy_current_pipes(dev);
    570 
     502                usb_hc_connection_deinitialize(&dev->hc_conn);
    571503                usb_alternate_interfaces_deinit(&dev->alternate_interfaces);
    572504                usb_device_release_descriptors(&dev->descriptors);
    573505                free(dev->driver_data);
    574         }
    575 }
    576 
     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 */
    577515void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
    578516{
Note: See TracChangeset for help on using the changeset viewer.