Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/bus/usb/usbhid/generic/hiddev.c

    r15f3c3f ra0c05e7  
    4848/*----------------------------------------------------------------------------*/
    4949
    50 usb_endpoint_description_t usb_hid_generic_poll_endpoint_description = {
     50const usb_endpoint_description_t usb_hid_generic_poll_endpoint_description = {
    5151        .transfer_type = USB_TRANSFER_INTERRUPT,
    5252        .direction = USB_DIRECTION_IN,
    5353        .interface_class = USB_CLASS_HID,
     54        .interface_subclass = -1,
     55        .interface_protocol = -1,
    5456        .flags = 0
    5557};
     
    5961
    6062/*----------------------------------------------------------------------------*/
    61 
    6263static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun);
    63 
    64 static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
     64static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
    6565    size_t size, size_t *act_size, int *event_nr, unsigned int flags);
    66 
    6766static int usb_generic_hid_client_connected(ddf_fun_t *fun);
    68 
    6967static size_t usb_generic_get_report_descriptor_length(ddf_fun_t *fun);
    70 
    71 static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
     68static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
    7269    size_t size, size_t *actual_size);
    73 
    74 /*----------------------------------------------------------------------------*/
    75 
     70/*----------------------------------------------------------------------------*/
    7671static usbhid_iface_t usb_generic_iface = {
    7772        .get_event = usb_generic_hid_get_event,
     
    8075        .get_report_descriptor = usb_generic_get_report_descriptor
    8176};
    82 
     77/*----------------------------------------------------------------------------*/
    8378static ddf_dev_ops_t usb_generic_hid_ops = {
    8479        .interfaces[USBHID_DEV_IFACE] = &usb_generic_iface,
    8580        .open = usb_generic_hid_client_connected
    8681};
    87 
    88 /*----------------------------------------------------------------------------*/
    89 
     82/*----------------------------------------------------------------------------*/
    9083static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun)
    9184{
    9285        usb_log_debug2("Generic HID: Get event length (fun: %p, "
    9386            "fun->driver_data: %p.\n", fun, fun->driver_data);
    94        
     87
    9588        if (fun == NULL || fun->driver_data == NULL) {
    9689                return 0;
    9790        }
    9891
    99         usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
    100        
     92        const usb_hid_dev_t *hid_dev = fun->driver_data;
     93
    10194        usb_log_debug2("hid_dev: %p, Max input report size (%zu).\n",
    10295            hid_dev, hid_dev->max_input_report_size);
    103        
     96
    10497        return hid_dev->max_input_report_size;
    10598}
    106 
    107 /*----------------------------------------------------------------------------*/
    108 
    109 static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
     99/*----------------------------------------------------------------------------*/
     100static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
    110101    size_t size, size_t *act_size, int *event_nr, unsigned int flags)
    111102{
    112103        usb_log_debug2("Generic HID: Get event.\n");
    113        
     104
    114105        if (fun == NULL || fun->driver_data == NULL || buffer == NULL
    115106            || act_size == NULL || event_nr == NULL) {
     
    118109        }
    119110
    120         usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
    121        
     111        const usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
     112
    122113        if (hid_dev->input_report_size > size) {
    123                 usb_log_debug("input_report_size > size (%zu, %zu)\n", 
     114                usb_log_debug("input_report_size > size (%zu, %zu)\n",
    124115                    hid_dev->input_report_size, size);
    125116                return EINVAL;  // TODO: other error code
    126117        }
    127        
     118
    128119        /*! @todo This should probably be somehow atomic. */
    129         memcpy(buffer, hid_dev->input_report, 
     120        memcpy(buffer, hid_dev->input_report,
    130121            hid_dev->input_report_size);
    131122        *act_size = hid_dev->input_report_size;
    132123        *event_nr = usb_hid_report_number(hid_dev);
    133        
     124
    134125        usb_log_debug2("OK\n");
    135        
    136         return EOK;
    137 }
    138 
    139 /*----------------------------------------------------------------------------*/
    140 
     126
     127        return EOK;
     128}
     129/*----------------------------------------------------------------------------*/
    141130static size_t usb_generic_get_report_descriptor_length(ddf_fun_t *fun)
    142131{
    143132        usb_log_debug("Generic HID: Get report descriptor length.\n");
    144        
     133
    145134        if (fun == NULL || fun->driver_data == NULL) {
    146135                usb_log_debug("No function");
    147136                return EINVAL;
    148137        }
    149        
    150         usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
    151        
    152         usb_log_debug2("hid_dev->report_desc_size = %zu\n", 
     138
     139        const usb_hid_dev_t *hid_dev = fun->driver_data;
     140
     141        usb_log_debug2("hid_dev->report_desc_size = %zu\n",
    153142            hid_dev->report_desc_size);
    154        
     143
    155144        return hid_dev->report_desc_size;
    156145}
    157 
    158 /*----------------------------------------------------------------------------*/
    159 
    160 static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
     146/*----------------------------------------------------------------------------*/
     147static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
    161148    size_t size, size_t *actual_size)
    162149{
    163150        usb_log_debug2("Generic HID: Get report descriptor.\n");
    164        
     151
    165152        if (fun == NULL || fun->driver_data == NULL) {
    166153                usb_log_debug("No function");
    167154                return EINVAL;
    168155        }
    169        
    170         usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
    171        
     156
     157        const usb_hid_dev_t *hid_dev = fun->driver_data;
     158
    172159        if (hid_dev->report_desc_size > size) {
    173160                return EINVAL;
    174161        }
    175        
     162
    176163        memcpy(desc, hid_dev->report_desc, hid_dev->report_desc_size);
    177164        *actual_size = hid_dev->report_desc_size;
    178        
    179         return EOK;
    180 }
    181 
    182 /*----------------------------------------------------------------------------*/
    183 
     165
     166        return EOK;
     167}
     168/*----------------------------------------------------------------------------*/
    184169static int usb_generic_hid_client_connected(ddf_fun_t *fun)
    185170{
     
    187172        return EOK;
    188173}
    189 
    190 /*----------------------------------------------------------------------------*/
    191 
    192 static int usb_generic_hid_create_function(usb_hid_dev_t *hid_dev)
    193 {       
     174/*----------------------------------------------------------------------------*/
     175void usb_generic_hid_deinit(usb_hid_dev_t *hid_dev, void *data)
     176{
     177        ddf_fun_t *fun = data;
     178        if (fun == NULL)
     179                return;
     180
     181        if (ddf_fun_unbind(fun) != EOK) {
     182                usb_log_error("Failed to unbind generic hid fun.\n");
     183                return;
     184        }
     185        usb_log_debug2("%s unbound.\n", fun->name);
     186        /* We did not allocate this, so leave this alone
     187         * the device would take care of it */
     188        fun->driver_data = NULL;
     189        ddf_fun_destroy(fun);
     190}
     191/*----------------------------------------------------------------------------*/
     192int usb_generic_hid_init(usb_hid_dev_t *hid_dev, void **data)
     193{
     194        if (hid_dev == NULL) {
     195                return EINVAL;
     196        }
     197
    194198        /* Create the exposed function. */
    195         /** @todo Generate numbers for the devices? */
    196199        usb_log_debug("Creating DDF function %s...\n", HID_GENERIC_FUN_NAME);
    197200        ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
     
    201204                return ENOMEM;
    202205        }
    203        
     206
     207        /* This is nasty, both device and this function have the same
     208         * driver data, thus destruction causes to double free */
     209        fun->driver_data = hid_dev;
    204210        fun->ops = &usb_generic_hid_ops;
    205         fun->driver_data = hid_dev;
    206211
    207212        int rc = ddf_fun_bind(fun);
     
    209214                usb_log_error("Could not bind DDF function: %s.\n",
    210215                    str_error(rc));
     216                fun->driver_data = NULL;
    211217                ddf_fun_destroy(fun);
    212218                return rc;
    213219        }
    214        
     220
    215221        usb_log_debug("HID function created. Handle: %" PRIun "\n", fun->handle);
    216        
    217         return EOK;
    218 }
    219 
    220 /*----------------------------------------------------------------------------*/
    221 
    222 int usb_generic_hid_init(usb_hid_dev_t *hid_dev, void **data)
    223 {
    224         if (hid_dev == NULL) {
    225                 return EINVAL;
    226         }
    227        
    228         return usb_generic_hid_create_function(hid_dev);
    229 }
    230 
    231 /*----------------------------------------------------------------------------*/
    232 
     222        *data = fun;
     223
     224        return EOK;
     225}
     226/*----------------------------------------------------------------------------*/
    233227bool usb_generic_hid_polling_callback(usb_hid_dev_t *hid_dev, void *data)
    234228{
    235229        return true;
    236230}
    237 
    238231/**
    239232 * @}
Note: See TracChangeset for help on using the changeset viewer.