Ignore:
File:
1 edited

Legend:

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

    r70452dd4 r065064e6  
    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        .add_device = 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;
     56static const usb_driver_t *driver = NULL;
    5357
    5458
     
    111115        int rc = usb_device_create_pipes(dev->ddf_dev, &dev->wire, endpoints,
    112116            dev->descriptors.configuration, dev->descriptors.configuration_size,
    113             dev->interface_no, alternate_setting,
    114             &pipes, &pipes_count);
     117            dev->interface_no, alternate_setting, &pipes, &pipes_count);
    115118
    116119        if (rc != EOK) {
     
    123126        return EOK;
    124127}
    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.
     128/*----------------------------------------------------------------------------*/
     129/** Callback when a new device is supposed to be controlled by this driver.
     130 *
     131 * This callback is a wrapper for USB specific version of @c device_add.
    129132 *
    130133 * @param gen_dev Device structure as prepared by DDF.
    131134 * @return Error code.
    132135 */
    133 int generic_add_device(ddf_dev_t *gen_dev)
     136int generic_device_add(ddf_dev_t *gen_dev)
    134137{
    135138        assert(driver);
    136139        assert(driver->ops);
    137         assert(driver->ops->add_device);
     140        assert(driver->ops->device_add);
    138141
    139142        int rc;
     
    147150                return rc;
    148151        }
    149 
    150         return driver->ops->add_device(dev);
    151 }
    152 
     152        gen_dev->driver_data = dev;
     153
     154        rc = driver->ops->device_add(dev);
     155        if (rc != EOK)
     156                usb_device_destroy(dev);
     157        return rc;
     158}
     159/*----------------------------------------------------------------------------*/
     160/** Callback when a device is supposed to be removed from the system.
     161 *
     162 * This callback is a wrapper for USB specific version of @c device_remove.
     163 *
     164 * @param gen_dev Device structure as prepared by DDF.
     165 * @return Error code.
     166 */
     167int generic_device_remove(ddf_dev_t *gen_dev)
     168{
     169        assert(driver);
     170        assert(driver->ops);
     171        if (driver->ops->device_rem == NULL)
     172                return ENOTSUP;
     173        /* Just tell the driver to stop whatever it is doing, keep structures */
     174        return driver->ops->device_rem(gen_dev->driver_data);
     175}
     176/*----------------------------------------------------------------------------*/
     177/** Callback when a device was removed from the system.
     178 *
     179 * This callback is a wrapper for USB specific version of @c device_gone.
     180 *
     181 * @param gen_dev Device structure as prepared by DDF.
     182 * @return Error code.
     183 */
     184int generic_device_gone(ddf_dev_t *gen_dev)
     185{
     186        assert(driver);
     187        assert(driver->ops);
     188        if (driver->ops->device_gone == NULL)
     189                return ENOTSUP;
     190        usb_device_t *usb_dev = gen_dev->driver_data;
     191        const int ret = driver->ops->device_gone(usb_dev);
     192        if (ret == EOK)
     193                usb_device_destroy(usb_dev);
     194
     195        return ret;
     196}
     197/*----------------------------------------------------------------------------*/
    153198/** Destroy existing pipes of a USB device.
    154199 *
     
    275320 * @return Error code.
    276321 */
    277 int usb_device_create_pipes(ddf_dev_t *dev, usb_device_connection_t *wire,
     322int usb_device_create_pipes(const ddf_dev_t *dev, usb_device_connection_t *wire,
    278323    usb_endpoint_description_t **endpoints,
    279     uint8_t *config_descr, size_t config_descr_size,
     324    const uint8_t *config_descr, size_t config_descr_size,
    280325    int interface_no, int interface_setting,
    281326    usb_endpoint_mapping_t **pipes_ptr, size_t *pipes_count_ptr)
     
    291336        int rc;
    292337
    293         size_t pipe_count = count_other_pipes(endpoints);
     338        const size_t pipe_count = count_other_pipes(endpoints);
    294339        if (pipe_count == 0) {
     340                *pipes_count_ptr = pipe_count;
    295341                *pipes_ptr = NULL;
    296342                return EOK;
     
    349395        }
    350396
    351         usb_hc_connection_close(&hc_conn);
     397        if (usb_hc_connection_close(&hc_conn) != EOK)
     398                usb_log_warning("usb_device_create_pipes(): "
     399                    "Failed to close connection.\n");
    352400
    353401        *pipes_ptr = pipes;
     
    371419        }
    372420
    373         usb_hc_connection_close(&hc_conn);
     421        if (usb_hc_connection_close(&hc_conn) != EOK)
     422                usb_log_warning("usb_device_create_pipes(): "
     423                    "Failed to close connection.\n");
    374424
    375425        /*
     
    395445 * @param[in] pipes_count Number of endpoints.
    396446 */
    397 int usb_device_destroy_pipes(ddf_dev_t *dev,
     447int usb_device_destroy_pipes(const ddf_dev_t *dev,
    398448    usb_endpoint_mapping_t *pipes, size_t pipes_count)
    399449{
    400450        assert(dev != NULL);
    401         assert(((pipes != NULL) && (pipes_count > 0))
    402             || ((pipes == NULL) && (pipes_count == 0)));
    403451
    404452        if (pipes_count == 0) {
     453                assert(pipes == NULL);
    405454                return EOK;
    406455        }
     456        assert(pipes != NULL);
    407457
    408458        int rc;
     
    422472        size_t i;
    423473        for (i = 0; i < pipes_count; i++) {
    424                 usb_pipe_unregister(pipes[i].pipe, &hc_conn);
     474                usb_log_debug2("Unregistering pipe %zu (%spresent).\n",
     475                    i, pipes[i].present ? "" : "not ");
     476                if (pipes[i].present)
     477                        usb_pipe_unregister(pipes[i].pipe, &hc_conn);
    425478                free(pipes[i].pipe);
    426479        }
    427480
    428         usb_hc_connection_close(&hc_conn);
     481        if (usb_hc_connection_close(&hc_conn) != EOK)
     482                usb_log_warning("usb_device_destroy_pipes(): "
     483                    "Failed to close connection.\n");
    429484
    430485        free(pipes);
     
    544599
    545600        /* 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         }
     601        destroy_current_pipes(dev);
    550602
    551603        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);
     604                free(dev->alternate_interfaces->alternatives);
     605        }
     606        free(dev->alternate_interfaces);
     607        free(dev->descriptors.configuration);
     608        free(dev->driver_data);
     609}
     610
     611void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
     612{
     613        assert(usb_dev);
     614        assert(usb_dev->driver_data == NULL);
     615        return usb_dev->driver_data = calloc(1, size);
     616
    559617}
    560618
Note: See TracChangeset for help on using the changeset viewer.