Changeset 7363fc1 in mainline


Ignore:
Timestamp:
2013-01-27T11:52:37Z (12 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
7d20461
Parents:
87619045
Message:

libusbdev: Add usb_device constructor using devman handle.

Fix initialization error paths.

Location:
uspace/lib/usbdev
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/usbdev/include/usb/dev/device.h

    r87619045 r7363fc1  
    4646void usb_device_destroy_ddf(ddf_dev_t *);
    4747
    48 const char* usb_device_get_name(usb_device_t *);
     48usb_device_t * usb_device_create(devman_handle_t);
     49void usb_device_destroy(usb_device_t *);
     50
     51const char * usb_device_get_name(usb_device_t *);
    4952ddf_fun_t *usb_device_ddf_fun_create(usb_device_t *, fun_type_t, const char *);
    5053
  • uspace/lib/usbdev/src/devdrv.c

    r87619045 r7363fc1  
    389389}
    390390
     391/** Clean instance of a USB device.
     392 *
     393 * @param dev Device to be de-initialized.
     394 *
     395 * Does not free/destroy supplied pointer.
     396 */
     397static void usb_device_fini(usb_device_t *usb_dev)
     398{
     399        if (usb_dev) {
     400                usb_dev_disconnect(usb_dev->bus_session);
     401                /* Destroy existing pipes. */
     402                usb_device_destroy_pipes(usb_dev);
     403                /* Ignore errors and hope for the best. */
     404                usb_hc_connection_deinitialize(&usb_dev->hc_conn);
     405                usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
     406                usb_device_release_descriptors(usb_dev);
     407                free(usb_dev->driver_data);
     408                usb_dev->driver_data = NULL;
     409        }
     410}
     411
    391412/** Initialize new instance of USB device.
    392413 *
     
    399420 */
    400421static int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
    401     const usb_endpoint_description_t **endpoints, const char **errstr_ptr)
     422    const usb_endpoint_description_t **endpoints, const char **errstr_ptr,
     423    devman_handle_t handle)
    402424{
    403425        assert(usb_dev != NULL);
    404426        assert(ddf_dev != NULL);
     427        assert(errstr_ptr);
    405428
    406429        *errstr_ptr = NULL;
     
    412435        usb_dev->pipes = NULL;
    413436
    414         usb_dev->bus_session = usb_dev_connect_to_self(ddf_dev);
     437        if (ddf_dev)
     438                usb_dev->bus_session = usb_dev_connect_to_self(ddf_dev);
     439        else
     440                usb_dev->bus_session = usb_dev_connect(handle);
     441
    415442        if (!usb_dev->bus_session) {
    416443                *errstr_ptr = "device bus session create";
     
    425452            &hc_handle, &address, &usb_dev->interface_no);
    426453        if (rc != EOK) {
     454                usb_dev_disconnect(usb_dev->bus_session);
    427455                *errstr_ptr = "device parameters retrieval";
    428456                return rc;
     
    436464            &usb_dev->wire, &usb_dev->hc_conn, address);
    437465        if (rc != EOK) {
     466                usb_dev_disconnect(usb_dev->bus_session);
    438467                *errstr_ptr = "device connection initialization";
    439468                return rc;
     
    445474            &usb_dev->ctrl_pipe, &usb_dev->wire);
    446475        if (rc != EOK) {
     476                usb_dev_disconnect(usb_dev->bus_session);
    447477                *errstr_ptr = "default control pipe initialization";
    448478                return rc;
     
    452482        rc = usb_hc_connection_open(&usb_dev->hc_conn);
    453483        if (rc != EOK) {
     484                usb_dev_disconnect(usb_dev->bus_session);
    454485                *errstr_ptr = "hc connection open";
    455486                return rc;
     
    461492                *errstr_ptr = "descriptor retrieval";
    462493                usb_hc_connection_close(&usb_dev->hc_conn);
     494                usb_dev_disconnect(usb_dev->bus_session);
    463495                return rc;
    464496        }
     
    472504            usb_dev->descriptors.configuration_size, usb_dev->interface_no);
    473505
    474         /* Create and register other pipes than default control (EP 0) */
    475         rc = usb_device_create_pipes(usb_dev, endpoints);
    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);
    480                 /* Alternate interfaces may be allocated */
    481                 usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
    482                 *errstr_ptr = "pipes initialization";
    483                 return rc;
     506        if (endpoints) {
     507                /* Create and register other pipes than default control (EP 0)*/
     508                rc = usb_device_create_pipes(usb_dev, endpoints);
     509                if (rc != EOK) {
     510                        usb_hc_connection_close(&usb_dev->hc_conn);
     511                        usb_device_fini(usb_dev);
     512                        *errstr_ptr = "pipes initialization";
     513                        return rc;
     514                }
    484515        }
    485516
     
    488519}
    489520
    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  */
    496 static void usb_device_fini(usb_device_t *dev)
    497 {
    498         if (dev) {
    499                 usb_dev_disconnect(dev->bus_session);
    500                 /* Destroy existing pipes. */
    501                 usb_device_destroy_pipes(dev);
    502                 /* Ignore errors and hope for the best. */
    503                 usb_hc_connection_deinitialize(&dev->hc_conn);
    504                 usb_alternate_interfaces_deinit(&dev->alternate_interfaces);
    505                 usb_device_release_descriptors(dev);
    506                 free(dev->driver_data);
    507                 dev->driver_data = NULL;
    508         }
    509 }
    510 
    511521int usb_device_create_ddf(ddf_dev_t *ddf_dev,
    512522    const usb_endpoint_description_t **desc, const char **err)
     
    514524        assert(ddf_dev);
    515525        assert(err);
    516         usb_device_t *dev = ddf_dev_data_alloc(ddf_dev, sizeof(usb_device_t));
    517         if (dev == NULL) {
     526        usb_device_t *usb_dev =
     527            ddf_dev_data_alloc(ddf_dev, sizeof(usb_device_t));
     528        if (usb_dev == NULL) {
    518529                *err = "DDF data alloc";
    519530                return ENOMEM;
    520531        }
    521         return usb_device_init(dev, ddf_dev, desc, err);
     532        return usb_device_init(usb_dev, ddf_dev, desc, err, 0);
    522533}
    523534
     
    525536{
    526537        assert(ddf_dev);
    527         usb_device_t *dev = ddf_dev_data_get(ddf_dev);
    528         assert(dev);
    529         usb_device_fini(dev);
     538        usb_device_t *usb_dev = ddf_dev_data_get(ddf_dev);
     539        assert(usb_dev);
     540        usb_device_fini(usb_dev);
    530541        return;
     542}
     543
     544usb_device_t * usb_device_create(devman_handle_t handle)
     545{
     546        usb_device_t *usb_dev = malloc(sizeof(usb_device_t));
     547        if (!usb_dev)
     548                return NULL;
     549        const char* dummy = NULL;
     550        const int ret = usb_device_init(usb_dev, NULL, NULL, &dummy, handle);
     551        if (ret != EOK) {
     552                free(usb_dev);
     553                usb_dev = NULL;
     554        }
     555        return usb_dev;
     556
     557}
     558
     559void usb_device_destroy(usb_device_t *usb_dev)
     560{
     561        if (usb_dev) {
     562                usb_device_fini(usb_dev);
     563                free(usb_dev);
     564        }
    531565}
    532566
Note: See TracChangeset for help on using the changeset viewer.