Changeset 6ee6e6f in mainline


Ignore:
Timestamp:
2011-04-10T13:20:06Z (13 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
4fa0a384
Parents:
c19329a
Message:

libusb refactoring, add usb_device_create()

The refactoring is mostly about moving code between functions.

Location:
uspace/lib/usb
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/usb/include/usb/devdrv.h

    rc19329a r6ee6e6f  
    174174    usb_endpoint_mapping_t **, size_t *);
    175175int usb_device_destroy_pipes(ddf_dev_t *, usb_endpoint_mapping_t *, size_t);
     176int usb_device_create(ddf_dev_t *, usb_endpoint_description_t **, usb_device_t **, const char **);
    176177
    177178size_t usb_interface_count_alternates(uint8_t *, size_t, uint8_t);
  • uspace/lib/usb/src/devdrv.c

    rc19329a r6ee6e6f  
    100100    usb_device_t *dev, int alternate_setting)
    101101{
     102        if (endpoints == NULL) {
     103                dev->pipes = NULL;
     104                dev->pipes_count = 0;
     105                return EOK;
     106        }
     107
    102108        usb_endpoint_mapping_t *pipes;
    103109        size_t pipes_count;
     
    109115
    110116        if (rc != EOK) {
    111                 usb_log_error(
    112                     "Failed to create endpoint pipes for `%s': %s.\n",
    113                     dev->ddf_dev->name, str_error(rc));
    114117                return rc;
    115118        }
     
    119122
    120123        return EOK;
    121 }
    122 
    123 /** Initialize all endpoint pipes.
    124  *
    125  * @param drv The driver.
    126  * @param dev The device to be initialized.
    127  * @return Error code.
    128  */
    129 static int initialize_pipes(usb_device_t *dev)
    130 {
    131         int rc;
    132 
    133         rc = usb_device_connection_initialize_from_device(&dev->wire,
    134             dev->ddf_dev);
    135         if (rc != EOK) {
    136                 usb_log_error(
    137                     "Failed initializing connection on device `%s'. %s.\n",
    138                     dev->ddf_dev->name, str_error(rc));
    139                 return rc;
    140         }
    141 
    142         rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe,
    143             &dev->wire);
    144         if (rc != EOK) {
    145                 usb_log_error("Failed to initialize default control pipe " \
    146                     "on device `%s': %s.\n",
    147                     dev->ddf_dev->name, str_error(rc));
    148                 return rc;
    149         }
    150 
    151         rc = usb_pipe_probe_default_control(&dev->ctrl_pipe);
    152         if (rc != EOK) {
    153                 usb_log_error(
    154                     "Probing default control pipe on device `%s' failed: %s.\n",
    155                     dev->ddf_dev->name, str_error(rc));
    156                 return rc;
    157         }
    158 
    159         /* Get our interface. */
    160         dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev);
    161 
    162         /*
    163          * We will do some querying of the device, it is worth to prepare
    164          * the long transfer.
    165          */
    166         rc = usb_pipe_start_long_transfer(&dev->ctrl_pipe);
    167         if (rc != EOK) {
    168                 usb_log_error("Failed to start transfer: %s.\n",
    169                     str_error(rc));
    170                 return rc;
    171         }
    172 
    173         /* Retrieve the descriptors. */
    174         rc = usb_device_retrieve_descriptors(&dev->ctrl_pipe,
    175             &dev->descriptors);
    176         if (rc != EOK) {
    177                 usb_log_error("Failed to retrieve standard device " \
    178                     "descriptors of %s: %s.\n",
    179                     dev->ddf_dev->name, str_error(rc));
    180                 return rc;
    181         }
    182 
    183 
    184         if (driver->endpoints != NULL) {
    185                 rc = initialize_other_pipes(driver->endpoints, dev, 0);
    186         }
    187 
    188         usb_pipe_end_long_transfer(&dev->ctrl_pipe);
    189 
    190         /* Rollback actions. */
    191         if (rc != EOK) {
    192                 if (dev->descriptors.configuration != NULL) {
    193                         free(dev->descriptors.configuration);
    194                 }
    195         }
    196 
    197         return rc;
    198124}
    199125
     
    339265        int rc;
    340266
    341         usb_device_t *dev = malloc(sizeof(usb_device_t));
    342         if (dev == NULL) {
    343                 usb_log_error("Out of memory when adding device `%s'.\n",
    344                     gen_dev->name);
    345                 return ENOMEM;
    346         }
    347 
    348 
    349         dev->ddf_dev = gen_dev;
    350         dev->ddf_dev->driver_data = dev;
    351         dev->driver_data = NULL;
    352         dev->descriptors.configuration = NULL;
    353 
    354         dev->pipes_count = 0;
    355         dev->pipes = NULL;
    356 
    357         rc = initialize_pipes(dev);
    358         if (rc != EOK) {
    359                 free(dev);
    360                 return rc;
    361         }
    362 
    363         (void) initialize_alternate_interfaces(dev);
     267        usb_device_t *dev = NULL;
     268        const char *err_msg = NULL;
     269        rc = usb_device_create(gen_dev, driver->endpoints, &dev, &err_msg);
     270        if (rc != EOK) {
     271                usb_log_error("USB device `%s' creation failed (%s): %s.\n",
     272                    gen_dev->name, err_msg, str_error(rc));
     273                return rc;
     274        }
    364275
    365276        return driver->ops->add_device(dev);
     
    641552}
    642553
     554/** Initialize control pipe and device descriptors. */
     555static int initialize_ctrl_pipe_and_descriptors(usb_device_t *dev,
     556     const char **errmsg)
     557{
     558        int rc;
     559
     560        rc = usb_device_connection_initialize_from_device(&dev->wire,
     561            dev->ddf_dev);
     562        if (rc != EOK) {
     563                *errmsg = "device connection initialization";
     564                return rc;
     565        }
     566
     567        rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe,
     568            &dev->wire);
     569        if (rc != EOK) {
     570                *errmsg = "default control pipe initialization";
     571                return rc;
     572        }
     573
     574        /* Get our interface. */
     575        dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev);
     576
     577        /*
     578         * We will do some querying of the device, it is worth to prepare
     579         * the long transfer.
     580         */
     581        rc = usb_pipe_start_long_transfer(&dev->ctrl_pipe);
     582        if (rc != EOK) {
     583                *errmsg = "transfer start";
     584                return rc;
     585        }
     586
     587        /* Retrieve the descriptors. */
     588        rc = usb_device_retrieve_descriptors(&dev->ctrl_pipe,
     589            &dev->descriptors);
     590        if (rc != EOK) {
     591                *errmsg = "descriptor retrieval";
     592        }
     593
     594        usb_pipe_end_long_transfer(&dev->ctrl_pipe);
     595
     596        return rc;
     597}
     598
     599
     600/** Create new instance of USB device.
     601 *
     602 * @param[in] ddf_dev Generic DDF device backing the USB one.
     603 * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
     604 * @param[out] dev_ptr Where to store pointer to the new device.
     605 * @param[out] errstr_ptr Where to store description of context
     606 *      (in case error occurs).
     607 * @return Error code.
     608 */
     609int usb_device_create(ddf_dev_t *ddf_dev,
     610    usb_endpoint_description_t **endpoints,
     611    usb_device_t **dev_ptr, const char **errstr_ptr)
     612{
     613        assert(dev_ptr != NULL);
     614        assert(ddf_dev != NULL);
     615
     616        int rc;
     617
     618        usb_device_t *dev = malloc(sizeof(usb_device_t));
     619        if (dev == NULL) {
     620                *errstr_ptr = "structure allocation";
     621                return ENOMEM;
     622        }
     623
     624        dev->ddf_dev = ddf_dev;
     625        dev->driver_data = NULL;
     626        dev->descriptors.configuration = NULL;
     627        dev->alternate_interfaces = NULL;
     628
     629        dev->pipes_count = 0;
     630        dev->pipes = NULL;
     631
     632        rc = initialize_ctrl_pipe_and_descriptors(dev, errstr_ptr);
     633        if (rc != EOK) {
     634                return rc;
     635        }
     636
     637        rc = initialize_alternate_interfaces(dev);
     638        if (rc != EOK) {
     639                /* We will try to silently ignore this. */
     640                dev->alternate_interfaces = NULL;
     641        }
     642
     643        rc = initialize_other_pipes(endpoints, dev, 0);
     644        if (rc != EOK) {
     645                *errstr_ptr = "pipes initialization";
     646                /* TODO: deallocate */
     647                return rc;
     648        }
     649
     650        *errstr_ptr = NULL;
     651        *dev_ptr = dev;
     652
     653        return EOK;
     654}
     655
    643656/**
    644657 * @}
Note: See TracChangeset for help on using the changeset viewer.