Ignore:
File:
1 edited

Legend:

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

    rb803845 r882580a  
    6464 * @return Task exit status.
    6565 */
    66 int usb_driver_main(usb_driver_t *drv)
     66int usb_driver_main(const usb_driver_t *drv)
    6767{
    6868        assert(drv != NULL);
     
    140140        assert(driver->ops->device_add);
    141141
    142         int rc;
    143 
    144         usb_device_t *dev = NULL;
     142        usb_device_t *dev = ddf_dev_data_alloc(gen_dev, sizeof(usb_device_t));
     143        if (dev == NULL) {
     144                usb_log_error("USB device `%s' structure allocation failed.\n",
     145                    gen_dev->name);
     146                return ENOMEM;
     147        }
    145148        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",
     149        int rc = usb_device_init(dev, gen_dev, driver->endpoints, &err_msg);
     150        if (rc != EOK) {
     151                usb_log_error("USB device `%s' init failed (%s): %s.\n",
    149152                    gen_dev->name, err_msg, str_error(rc));
    150153                return rc;
    151154        }
    152         gen_dev->driver_data = dev;
    153155
    154156        rc = driver->ops->device_add(dev);
     
    516518
    517519
    518 /** Create new instance of USB device.
    519  *
     520/** Initialize new instance of USB device.
     521 *
     522 * @param[in] usb_dev Pointer to the new device.
    520523 * @param[in] ddf_dev Generic DDF device backing the USB one.
    521524 * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
    522  * @param[out] dev_ptr Where to store pointer to the new device.
    523525 * @param[out] errstr_ptr Where to store description of context
    524526 *      (in case error occurs).
    525527 * @return Error code.
    526528 */
    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);
     529int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
     530    const usb_endpoint_description_t **endpoints, const char **errstr_ptr)
     531{
     532        assert(usb_dev != NULL);
    532533        assert(ddf_dev != NULL);
    533534
    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;
     535        usb_dev->ddf_dev = ddf_dev;
     536        usb_dev->driver_data = NULL;
     537        usb_dev->descriptors.configuration = NULL;
     538        usb_dev->alternate_interfaces = NULL;
     539        usb_dev->pipes_count = 0;
     540        usb_dev->pipes = NULL;
    551541
    552542        /* Initialize backing wire and control pipe. */
    553         rc = init_wire_and_ctrl_pipe(dev, errstr_ptr);
     543        int rc = init_wire_and_ctrl_pipe(usb_dev, errstr_ptr);
    554544        if (rc != EOK) {
    555545                return rc;
     
    557547
    558548        /* Get our interface. */
    559         dev->interface_no = usb_device_get_assigned_interface(dev->ddf_dev);
     549        usb_dev->interface_no = usb_device_get_assigned_interface(ddf_dev);
    560550
    561551        /* Retrieve standard descriptors. */
    562         rc = usb_device_retrieve_descriptors(&dev->ctrl_pipe,
    563             &dev->descriptors);
    564         if (rc != EOK) {
     552        rc = usb_device_retrieve_descriptors(&usb_dev->ctrl_pipe,
     553            &usb_dev->descriptors);
     554        if (rc != EOK) {
     555                /* Nothing allocated, nothing to free. */
    565556                *errstr_ptr = "descriptor retrieval";
    566557                return rc;
    567558        }
    568559
    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) {
     560        /* Create alternate interfaces. We will silently ignore failure. */
     561        //TODO Why ignore?
     562        usb_alternate_interfaces_create(usb_dev->descriptors.configuration,
     563            usb_dev->descriptors.configuration_size, usb_dev->interface_no,
     564            &usb_dev->alternate_interfaces);
     565
     566        rc = initialize_other_pipes(endpoints, usb_dev, 0);
     567        if (rc != EOK) {
     568                /* Full configuration descriptor is allocated. */
     569                free(usb_dev->descriptors.configuration);
     570                /* Alternate interfaces may be allocated */
     571                usb_alternate_interfaces_destroy(usb_dev->alternate_interfaces);
    580572                *errstr_ptr = "pipes initialization";
    581573                return rc;
     
    583575
    584576        *errstr_ptr = NULL;
    585         *dev_ptr = dev;
    586577
    587578        return EOK;
    588579}
    589580
    590 /** Destroy instance of a USB device.
     581/** Clean instance of a USB device.
    591582 *
    592583 * @param dev Device to be de-initialized.
     
    596587void usb_device_deinit(usb_device_t *dev)
    597588{
    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);
     589        if (dev) {
     590                /* Ignore errors and hope for the best. */
     591                destroy_current_pipes(dev);
     592
     593                usb_alternate_interfaces_destroy(dev->alternate_interfaces);
     594                free(dev->descriptors.configuration);
     595                free(dev->driver_data);
     596        }
    611597}
    612598
Note: See TracChangeset for help on using the changeset viewer.