Changeset 00aece0 in mainline for uspace/lib/usbdev/src/devdrv.c


Ignore:
Timestamp:
2012-02-18T16:47:38Z (13 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
4449c6c
Parents:
bd5f3b7 (diff), f943dd3 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

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

    rbd5f3b7 r00aece0  
    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>
    4141#include <assert.h>
    4242
    43 static int generic_add_device(ddf_dev_t *);
     43static int generic_device_add(ddf_dev_t *);
     44static int generic_device_remove(ddf_dev_t *);
     45static int generic_device_gone(ddf_dev_t *);
    4446
    4547static driver_ops_t generic_driver_ops = {
    46         .add_device = generic_add_device
     48        .dev_add = generic_device_add,
     49        .dev_remove = generic_device_remove,
     50        .dev_gone = generic_device_gone,
    4751};
    4852static driver_t generic_driver = {
     
    5054};
    5155
    52 static usb_driver_t *driver = NULL;
    53 
     56static const usb_driver_t *driver = NULL;
    5457
    5558/** Main routine of USB device driver.
     
    6063 * @return Task exit status.
    6164 */
    62 int usb_driver_main(usb_driver_t *drv)
     65int usb_driver_main(const usb_driver_t *drv)
    6366{
    6467        assert(drv != NULL);
     
    7174        return ddf_driver_main(&generic_driver);
    7275}
    73 
     76/*----------------------------------------------------------------------------*/
    7477/** Count number of pipes the driver expects.
    7578 *
     
    7780 * @return Number of pipes (excluding default control pipe).
    7881 */
    79 static size_t count_other_pipes(usb_endpoint_description_t **endpoints)
    80 {
    81         size_t count = 0;
    82         if (endpoints == NULL) {
    83                 return 0;
    84         }
    85 
    86         while (endpoints[count] != NULL) {
    87                 count++;
    88         }
    89 
     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);
    9087        return count;
    9188}
    92 
    93 /** Initialize endpoint pipes, excluding default control one.
    94  *
    95  * @param drv The device driver.
    96  * @param dev Device to be initialized.
    97  * @return Error code.
    98  */
    99 static int initialize_other_pipes(usb_endpoint_description_t **endpoints,
    100     usb_device_t *dev, int alternate_setting)
    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,
    114             &pipes, &pipes_count);
    115 
    116         if (rc != EOK) {
    117                 return rc;
    118         }
    119 
    120         dev->pipes = pipes;
    121         dev->pipes_count = pipes_count;
    122 
    123         return EOK;
    124 }
    125 
    126 /** Callback when new device is supposed to be controlled by this driver.
    127  *
    128  * This callback is a wrapper for USB specific version of @c add_device.
     89/*----------------------------------------------------------------------------*/
     90/** Callback when a new device is supposed to be controlled by this driver.
     91 *
     92 * This callback is a wrapper for USB specific version of @c device_add.
    12993 *
    13094 * @param gen_dev Device structure as prepared by DDF.
    13195 * @return Error code.
    13296 */
    133 int generic_add_device(ddf_dev_t *gen_dev)
     97int generic_device_add(ddf_dev_t *gen_dev)
    13498{
    13599        assert(driver);
    136100        assert(driver->ops);
    137         assert(driver->ops->add_device);
    138 
    139         int rc;
    140 
    141         usb_device_t *dev = NULL;
     101        assert(driver->ops->device_add);
     102
     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. */
    142112        const char *err_msg = NULL;
    143         rc = usb_device_create(gen_dev, driver->endpoints, &dev, &err_msg);
    144         if (rc != EOK) {
    145                 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",
    146116                    gen_dev->name, err_msg, str_error(rc));
    147117                return rc;
    148118        }
    149119
    150         return driver->ops->add_device(dev);
    151 }
    152 
     120        /* Start USB driver specific initialization. */
     121        rc = driver->ops->device_add(dev);
     122        if (rc != EOK)
     123                usb_device_deinit(dev);
     124        return rc;
     125}
     126/*----------------------------------------------------------------------------*/
     127/** Callback when a device is supposed to be removed from the system.
     128 *
     129 * This callback is a wrapper for USB specific version of @c device_remove.
     130 *
     131 * @param gen_dev Device structure as prepared by DDF.
     132 * @return Error code.
     133 */
     134int generic_device_remove(ddf_dev_t *gen_dev)
     135{
     136        assert(driver);
     137        assert(driver->ops);
     138        if (driver->ops->device_rem == NULL)
     139                return ENOTSUP;
     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;
     147}
     148/*----------------------------------------------------------------------------*/
     149/** Callback when a device was removed from the system.
     150 *
     151 * This callback is a wrapper for USB specific version of @c device_gone.
     152 *
     153 * @param gen_dev Device structure as prepared by DDF.
     154 * @return Error code.
     155 */
     156int generic_device_gone(ddf_dev_t *gen_dev)
     157{
     158        assert(driver);
     159        assert(driver->ops);
     160        if (driver->ops->device_gone == NULL)
     161                return ENOTSUP;
     162        usb_device_t *usb_dev = gen_dev->driver_data;
     163        const int ret = driver->ops->device_gone(usb_dev);
     164        if (ret == EOK)
     165                usb_device_deinit(usb_dev);
     166
     167        return ret;
     168}
     169/*----------------------------------------------------------------------------*/
    153170/** Destroy existing pipes of a USB device.
    154171 *
    155172 * @param dev Device where to destroy the pipes.
    156  * @return Error code.
    157  */
    158 static int destroy_current_pipes(usb_device_t *dev)
    159 {
    160         int rc = usb_device_destroy_pipes(dev->ddf_dev,
    161             dev->pipes, dev->pipes_count);
    162         if (rc != EOK) {
    163                 return rc;
    164         }
    165 
     173 */
     174static void destroy_current_pipes(usb_device_t *dev)
     175{
     176        usb_device_destroy_pipes(dev->pipes, dev->pipes_count);
    166177        dev->pipes = NULL;
    167178        dev->pipes_count = 0;
    168 
    169         return EOK;
    170 }
    171 
     179}
     180/*----------------------------------------------------------------------------*/
    172181/** Change interface setting of a device.
    173182 * This function selects new alternate setting of an interface by issuing
     
    193202 */
    194203int usb_device_select_interface(usb_device_t *dev, uint8_t alternate_setting,
    195     usb_endpoint_description_t **endpoints)
     204    const usb_endpoint_description_t **endpoints)
    196205{
    197206        if (dev->interface_no < 0) {
     
    199208        }
    200209
    201         int rc;
    202 
    203210        /* Destroy existing pipes. */
    204         rc = destroy_current_pipes(dev);
    205         if (rc != EOK) {
    206                 return rc;
    207         }
     211        destroy_current_pipes(dev);
    208212
    209213        /* Change the interface itself. */
    210         rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,
     214        int rc = usb_request_set_interface(&dev->ctrl_pipe, dev->interface_no,
    211215            alternate_setting);
    212216        if (rc != EOK) {
     
    215219
    216220        /* Create new pipes. */
    217         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);
    218225
    219226        return rc;
     
    255262}
    256263
     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
    257275/** Create pipes for a device.
    258276 *
     
    262280 * - registers endpoints with the host controller
    263281 *
    264  * @param[in] dev Generic DDF device backing the USB one.
    265282 * @param[in] wire Initialized backing connection to the host controller.
    266283 * @param[in] endpoints Endpoints description, NULL terminated.
     
    272289 *      (not NULL terminated).
    273290 * @param[out] pipes_count_ptr Where to store number of pipes
    274  *      (set to if you wish to ignore the count).
    275  * @return Error code.
    276  */
    277 int usb_device_create_pipes(ddf_dev_t *dev, usb_device_connection_t *wire,
    278     usb_endpoint_description_t **endpoints,
    279     uint8_t *config_descr, size_t config_descr_size,
     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,
     295    const usb_endpoint_description_t **endpoints,
     296    const uint8_t *config_descr, size_t config_descr_size,
    280297    int interface_no, int interface_setting,
    281298    usb_endpoint_mapping_t **pipes_ptr, size_t *pipes_count_ptr)
    282299{
    283         assert(dev != NULL);
    284300        assert(wire != NULL);
    285         assert(endpoints != NULL);
    286301        assert(config_descr != NULL);
    287302        assert(config_descr_size > 0);
     
    291306        int rc;
    292307
    293         size_t pipe_count = count_other_pipes(endpoints);
     308        const size_t pipe_count = count_other_pipes(endpoints);
    294309        if (pipe_count == 0) {
     310                if (pipes_count_ptr)
     311                        *pipes_count_ptr = pipe_count;
    295312                *pipes_ptr = NULL;
    296313                return EOK;
     
    298315
    299316        usb_endpoint_mapping_t *pipes
    300             = malloc(sizeof(usb_endpoint_mapping_t) * pipe_count);
     317            = calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
    301318        if (pipes == NULL) {
    302319                return ENOMEM;
    303320        }
    304321
    305         /* Initialize to NULL to allow smooth rollback. */
     322        /* Now initialize. */
    306323        for (i = 0; i < pipe_count; i++) {
    307                 pipes[i].pipe = NULL;
    308         }
    309 
    310         /* Now allocate and fully initialize. */
    311         for (i = 0; i < pipe_count; i++) {
    312                 pipes[i].pipe = malloc(sizeof(usb_pipe_t));
    313                 if (pipes[i].pipe == NULL) {
    314                         rc = ENOMEM;
    315                         goto rollback_free_only;
    316                 }
    317324                pipes[i].description = endpoints[i];
    318325                pipes[i].interface_no = interface_no;
     
    324331            config_descr, config_descr_size, wire);
    325332        if (rc != EOK) {
    326                 goto rollback_free_only;
    327         }
    328 
    329         /* Register the endpoints with HC. */
    330         usb_hc_connection_t hc_conn;
    331         rc = usb_hc_connection_initialize_from_device(&hc_conn, dev);
    332         if (rc != EOK) {
    333                 goto rollback_free_only;
    334         }
    335 
    336         rc = usb_hc_connection_open(&hc_conn);
    337         if (rc != EOK) {
    338                 goto rollback_free_only;
    339         }
    340 
     333                free(pipes);
     334                return rc;
     335        }
     336
     337        /* Register created pipes. */
    341338        for (i = 0; i < pipe_count; i++) {
    342339                if (pipes[i].present) {
    343                         rc = usb_pipe_register(pipes[i].pipe,
    344                             pipes[i].descriptor->poll_interval, &hc_conn);
     340                        rc = usb_pipe_register(&pipes[i].pipe,
     341                            pipes[i].descriptor->poll_interval);
    345342                        if (rc != EOK) {
    346343                                goto rollback_unregister_endpoints;
     
    348345                }
    349346        }
    350 
    351         usb_hc_connection_close(&hc_conn);
    352347
    353348        *pipes_ptr = pipes;
     
    367362        for (i = 0; i < pipe_count; i++) {
    368363                if (pipes[i].present) {
    369                         usb_pipe_unregister(pipes[i].pipe, &hc_conn);
     364                        usb_pipe_unregister(&pipes[i].pipe);
    370365                }
    371366        }
    372367
    373         usb_hc_connection_close(&hc_conn);
    374 
    375         /*
    376          * Jump here if something went wrong before some actual communication
    377          * with HC. Then the only thing that needs to be done is to free
    378          * allocated memory.
    379          */
    380 rollback_free_only:
    381         for (i = 0; i < pipe_count; i++) {
    382                 if (pipes[i].pipe != NULL) {
    383                         free(pipes[i].pipe);
    384                 }
    385         }
    386368        free(pipes);
    387 
    388369        return rc;
    389370}
     
    391372/** Destroy pipes previously created by usb_device_create_pipes.
    392373 *
    393  * @param[in] dev Generic DDF device backing the USB one.
    394374 * @param[in] pipes Endpoint mapping to be destroyed.
    395375 * @param[in] pipes_count Number of endpoints.
    396376 */
    397 int usb_device_destroy_pipes(ddf_dev_t *dev,
    398     usb_endpoint_mapping_t *pipes, size_t pipes_count)
    399 {
    400         assert(dev != NULL);
    401         assert(((pipes != NULL) && (pipes_count > 0))
    402             || ((pipes == NULL) && (pipes_count == 0)));
    403 
    404         if (pipes_count == 0) {
    405                 return EOK;
    406         }
    407 
    408         int rc;
    409 
    410         /* Prepare connection to HC to allow endpoint unregistering. */
    411         usb_hc_connection_t hc_conn;
    412         rc = usb_hc_connection_initialize_from_device(&hc_conn, dev);
    413         if (rc != EOK) {
    414                 return rc;
    415         }
    416         rc = usb_hc_connection_open(&hc_conn);
    417         if (rc != EOK) {
    418                 return rc;
    419         }
    420 
     377void usb_device_destroy_pipes(usb_endpoint_mapping_t *pipes, size_t pipes_count)
     378{
    421379        /* Destroy the pipes. */
    422         size_t i;
    423         for (i = 0; i < pipes_count; i++) {
    424                 usb_pipe_unregister(pipes[i].pipe, &hc_conn);
    425                 free(pipes[i].pipe);
    426         }
    427 
    428         usb_hc_connection_close(&hc_conn);
    429 
     380        for (size_t i = 0; i < pipes_count; ++i) {
     381                assert(pipes);
     382                usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
     383                    i, pipes[i].present ? "" : "not ");
     384                if (pipes[i].present)
     385                        usb_pipe_unregister(&pipes[i].pipe);
     386        }
    430387        free(pipes);
    431 
    432         return EOK;
    433 }
    434 
    435 /** Initialize control pipe in a device.
    436  *
    437  * @param dev USB device in question.
    438  * @param errmsg Where to store error context.
    439  * @return
    440  */
    441 static int init_wire_and_ctrl_pipe(usb_device_t *dev, const char **errmsg)
    442 {
    443         int rc;
    444 
    445         rc = usb_device_connection_initialize_from_device(&dev->wire,
    446             dev->ddf_dev);
    447         if (rc != EOK) {
    448                 *errmsg = "device connection initialization";
    449                 return rc;
    450         }
    451 
    452         rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe,
    453             &dev->wire);
    454         if (rc != EOK) {
    455                 *errmsg = "default control pipe initialization";
    456                 return rc;
    457         }
    458 
    459         return EOK;
    460 }
    461 
    462 
    463 /** Create new instance of USB device.
    464  *
     388}
     389
     390/** Initialize new instance of USB device.
     391 *
     392 * @param[in] usb_dev Pointer to the new device.
    465393 * @param[in] ddf_dev Generic DDF device backing the USB one.
    466394 * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
    467  * @param[out] dev_ptr Where to store pointer to the new device.
    468395 * @param[out] errstr_ptr Where to store description of context
    469396 *      (in case error occurs).
    470397 * @return Error code.
    471398 */
    472 int usb_device_create(ddf_dev_t *ddf_dev,
    473     usb_endpoint_description_t **endpoints,
    474     usb_device_t **dev_ptr, const char **errstr_ptr)
    475 {
    476         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);
    477403        assert(ddf_dev != NULL);
    478404
    479         int rc;
    480 
    481         usb_device_t *dev = malloc(sizeof(usb_device_t));
    482         if (dev == NULL) {
    483                 *errstr_ptr = "structure allocation";
    484                 return ENOMEM;
    485         }
    486 
    487         // FIXME: proper deallocation in case of errors
    488 
    489         dev->ddf_dev = ddf_dev;
    490         dev->driver_data = NULL;
    491         dev->descriptors.configuration = NULL;
    492         dev->alternate_interfaces = NULL;
    493 
    494         dev->pipes_count = 0;
    495         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);
    496426
    497427        /* Initialize backing wire and control pipe. */
    498         rc = init_wire_and_ctrl_pipe(dev, errstr_ptr);
    499         if (rc != EOK) {
    500                 return rc;
    501         }
    502 
    503         /* Get our interface. */
    504         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        }
    505450
    506451        /* Retrieve standard descriptors. */
    507         rc = usb_device_retrieve_descriptors(&dev->ctrl_pipe,
    508             &dev->descriptors);
     452        rc = usb_device_retrieve_descriptors(
     453            &usb_dev->ctrl_pipe, &usb_dev->descriptors);
    509454        if (rc != EOK) {
    510455                *errstr_ptr = "descriptor retrieval";
    511                 return rc;
    512         }
    513 
    514         /* Create alternate interfaces. */
    515         rc = usb_alternate_interfaces_create(dev->descriptors.configuration,
    516             dev->descriptors.configuration_size, dev->interface_no,
    517             &dev->alternate_interfaces);
    518         if (rc != EOK) {
    519                 /* We will try to silently ignore this. */
    520                 dev->alternate_interfaces = NULL;
    521         }
    522 
    523         rc = initialize_other_pipes(endpoints, dev, 0);
    524         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);
    525482                *errstr_ptr = "pipes initialization";
    526483                return rc;
    527484        }
    528485
    529         *errstr_ptr = NULL;
    530         *dev_ptr = dev;
    531 
     486        usb_hc_connection_close(&usb_dev->hc_conn);
    532487        return EOK;
    533488}
    534489
    535 /** Destroy instance of a USB device.
    536  *
    537  * @param dev Device to be destroyed.
    538  */
    539 void usb_device_destroy(usb_device_t *dev)
    540 {
    541         if (dev == NULL) {
    542                 return;
    543         }
    544 
    545         /* Ignore errors and hope for the best. */
    546         usb_device_destroy_pipes(dev->ddf_dev, dev->pipes, dev->pipes_count);
    547         if (dev->descriptors.configuration != NULL) {
    548                 free(dev->descriptors.configuration);
    549         }
    550 
    551         if (dev->alternate_interfaces != NULL) {
    552                 if (dev->alternate_interfaces->alternatives != NULL) {
    553                         free(dev->alternate_interfaces->alternatives);
    554                 }
    555                 free(dev->alternate_interfaces);
    556         }
    557 
    558         free(dev);
     490/** Clean instance of a USB device.
     491 *
     492 * @param dev Device to be de-initialized.
     493 *
     494 * Does not free/destroy supplied pointer.
     495 */
     496void usb_device_deinit(usb_device_t *dev)
     497{
     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 */
     515void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
     516{
     517        assert(usb_dev);
     518        assert(usb_dev->driver_data == NULL);
     519        return usb_dev->driver_data = calloc(1, size);
     520
    559521}
    560522
Note: See TracChangeset for help on using the changeset viewer.