Changeset 20a3465 in mainline for uspace/drv/bus/usb/usbhid/main.c


Ignore:
Timestamp:
2011-10-30T19:50:54Z (14 years ago)
Author:
Maurizio Lombardi <m.lombardi85@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
3ce78580, 48902fa
Parents:
4c3ad56 (diff), 45bf63c (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/drv/bus/usb/usbhid/main.c

    r4c3ad56 r20a3465  
    4646#include "usbhid.h"
    4747
    48 /*----------------------------------------------------------------------------*/
    49 
    5048#define NAME "usbhid"
    5149
     
    6765 *
    6866 * @param dev Device to add.
    69  *
    70  * @retval EOK if successful.
    71  * @retval ENOMEM if there
    72  * @return Other error code inherited from one of functions usb_kbd_init(),
    73  *         ddf_fun_bind() and ddf_fun_add_to_class().
     67 * @return Error code.
    7468 */
    7569static int usb_hid_try_add_device(usb_device_t *dev)
    7670{
    7771        assert(dev != NULL);
    78        
    79         /*
    80          * Initialize device (get and process descriptors, get address, etc.)
    81          */
     72
     73        /* Initialize device (get and process descriptors, get address, etc.) */
    8274        usb_log_debug("Initializing USB/HID device...\n");
    83        
    84         usb_hid_dev_t *hid_dev = usb_hid_new();
     75
     76        usb_hid_dev_t *hid_dev =
     77            usb_device_data_alloc(dev, sizeof(usb_hid_dev_t));
    8578        if (hid_dev == NULL) {
    8679                usb_log_error("Error while creating USB/HID device "
     
    8881                return ENOMEM;
    8982        }
    90        
     83
    9184        int rc = usb_hid_init(hid_dev, dev);
    92        
     85
    9386        if (rc != EOK) {
    9487                usb_log_error("Failed to initialize USB/HID device.\n");
    95                 usb_hid_destroy(hid_dev);
     88                usb_hid_deinit(hid_dev);
    9689                return rc;
    97         }       
    98        
     90        }
     91
    9992        usb_log_debug("USB/HID device structure initialized.\n");
    100        
     93
    10194        /*
    10295         * 1) subdriver vytvori vlastnu ddf_fun, vlastne ddf_dev_ops, ktore da
     
    109102         *    pouzit usb/classes/hid/iface.h - prvy int je telefon
    110103         */
    111        
     104
    112105        /* Start automated polling function.
    113106         * This will create a separate fibril that will query the device
     
    125118           /* Custom argument. */
    126119           hid_dev);
    127        
    128        
     120
    129121        if (rc != EOK) {
    130122                usb_log_error("Failed to start polling fibril for `%s'.\n",
    131123                    dev->ddf_dev->name);
     124                usb_hid_deinit(hid_dev);
    132125                return rc;
    133126        }
     127        hid_dev->running = true;
    134128
    135129        /*
     
    138132        return EOK;
    139133}
    140 
    141134/*----------------------------------------------------------------------------*/
    142135/**
     
    146139 *
    147140 * @param dev Structure representing the new device.
    148  *
    149  * @retval EOK if successful.
    150  * @retval EREFUSED if the device is not supported.
     141 * @return Error code.
    151142 */
    152143static int usb_hid_device_add(usb_device_t *dev)
    153144{
    154145        usb_log_debug("usb_hid_device_add()\n");
    155        
     146
    156147        if (dev == NULL) {
    157148                usb_log_warning("Wrong parameter given for add_device().\n");
    158149                return EINVAL;
    159150        }
    160        
     151
    161152        if (dev->interface_no < 0) {
    162153                usb_log_warning("Device is not a supported HID device.\n");
     
    165156                return ENOTSUP;
    166157        }
    167        
     158
    168159        int rc = usb_hid_try_add_device(dev);
    169        
     160
    170161        if (rc != EOK) {
    171162                usb_log_warning("Device is not a supported HID device.\n");
     
    174165                return rc;
    175166        }
    176        
     167
    177168        usb_log_info("HID device `%s' ready to use.\n", dev->ddf_dev->name);
    178169
    179170        return EOK;
    180171}
    181 
    182 /*----------------------------------------------------------------------------*/
    183 
    184 /* Currently, the framework supports only device adding. Once the framework
    185  * supports unplug, more callbacks will be added. */
    186 static usb_driver_ops_t usb_hid_driver_ops = {
    187         .device_add = usb_hid_device_add,
     172/*----------------------------------------------------------------------------*/
     173/**
     174 * Callback for a device about to be removed from the driver.
     175 *
     176 * @param dev Structure representing the device.
     177 * @return Error code.
     178 */
     179static int usb_hid_device_rem(usb_device_t *dev)
     180{
     181        return EOK;
     182}
     183/*----------------------------------------------------------------------------*/
     184/**
     185 * Callback for removing a device from the driver.
     186 *
     187 * @param dev Structure representing the device.
     188 * @return Error code.
     189 */
     190static int usb_hid_device_gone(usb_device_t *dev)
     191{
     192        usb_hid_dev_t *hid_dev = dev->driver_data;
     193        unsigned tries = 10;
     194        while (hid_dev->running) {
     195                async_usleep(100000);
     196                if (!tries--) {
     197                        usb_log_error("Can't remove hub, still running.\n");
     198                        return EBUSY;
     199                }
     200        }
     201
     202        assert(!hid_dev->running);
     203        usb_hid_deinit(hid_dev);
     204        usb_log_debug2("%s destruction complete.\n", dev->ddf_dev->name);
     205        return EOK;
     206}
     207/*----------------------------------------------------------------------------*/
     208/** USB generic driver callbacks */
     209static const usb_driver_ops_t usb_hid_driver_ops = {
     210        .device_add = usb_hid_device_add,
     211        .device_rem = usb_hid_device_rem,
     212        .device_gone = usb_hid_device_gone,
    188213};
    189 
    190 
    191 /* The driver itself. */
    192 static usb_driver_t usb_hid_driver = {
     214/*----------------------------------------------------------------------------*/
     215/** The driver itself. */
     216static const usb_driver_t usb_hid_driver = {
    193217        .name = NAME,
    194218        .ops = &usb_hid_driver_ops,
    195219        .endpoints = usb_hid_endpoints
    196220};
    197 
    198 /*----------------------------------------------------------------------------*/
    199 
     221/*----------------------------------------------------------------------------*/
    200222int main(int argc, char *argv[])
    201223{
     
    206228        return usb_driver_main(&usb_hid_driver);
    207229}
    208 
    209230/**
    210231 * @}
Note: See TracChangeset for help on using the changeset viewer.