Ignore:
File:
1 edited

Legend:

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

    r56fd7cf r9d58539  
    3535 */
    3636
    37 /* XXX Fix this */
    38 #define _DDF_DATA_IMPLANT
    39 
    4037#include <usb/debug.h>
    4138#include <usb/classes/classes.h>
     
    4946#include "usbhid.h"
    5047
    51 
     48/*----------------------------------------------------------------------------*/
    5249
    5350const usb_endpoint_description_t usb_hid_generic_poll_endpoint_description = {
     
    6360const char *HID_GENERIC_CLASS_NAME = "hid";
    6461
    65 
     62/*----------------------------------------------------------------------------*/
    6663static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun);
    6764static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
     
    7168static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
    7269    size_t size, size_t *actual_size);
    73 
     70/*----------------------------------------------------------------------------*/
    7471static usbhid_iface_t usb_generic_iface = {
    7572        .get_event = usb_generic_hid_get_event,
     
    7875        .get_report_descriptor = usb_generic_get_report_descriptor
    7976};
    80 
     77/*----------------------------------------------------------------------------*/
    8178static ddf_dev_ops_t usb_generic_hid_ops = {
    8279        .interfaces[USBHID_DEV_IFACE] = &usb_generic_iface,
    8380        .open = usb_generic_hid_client_connected
    8481};
    85 
    86 /** Return hid_dev_t * for generic HID function node.
    87  *
    88  * For the generic HID subdriver the 'hid' function has usb_hid_gen_fun_t
    89  * as soft state. Through that we can get to the usb_hid_dev_t.
    90  */
    91 static usb_hid_dev_t *fun_hid_dev(ddf_fun_t *fun)
    92 {
    93         return ((usb_hid_gen_fun_t *)ddf_fun_data_get(fun))->hid_dev;
    94 }
    95 
     82/*----------------------------------------------------------------------------*/
    9683static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun)
    9784{
    9885        usb_log_debug2("Generic HID: Get event length (fun: %p, "
    99             "fun->driver_data: %p.\n", fun, ddf_fun_data_get(fun));
    100 
    101         const usb_hid_dev_t *hid_dev = fun_hid_dev(fun);
     86            "fun->driver_data: %p.\n", fun, fun->driver_data);
     87
     88        if (fun == NULL || fun->driver_data == NULL) {
     89                return 0;
     90        }
     91
     92        const usb_hid_dev_t *hid_dev = fun->driver_data;
    10293
    10394        usb_log_debug2("hid_dev: %p, Max input report size (%zu).\n",
     
    10697        return hid_dev->max_input_report_size;
    10798}
    108 
     99/*----------------------------------------------------------------------------*/
    109100static 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)
     
    112103        usb_log_debug2("Generic HID: Get event.\n");
    113104
    114         if (buffer == NULL || act_size == NULL || event_nr == NULL) {
     105        if (fun == NULL || fun->driver_data == NULL || buffer == NULL
     106            || act_size == NULL || event_nr == NULL) {
    115107                usb_log_debug("No function");
    116108                return EINVAL;
    117109        }
    118110
    119         const usb_hid_dev_t *hid_dev = fun_hid_dev(fun);
     111        const usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
    120112
    121113        if (hid_dev->input_report_size > size) {
     
    135127        return EOK;
    136128}
    137 
     129/*----------------------------------------------------------------------------*/
    138130static size_t usb_generic_get_report_descriptor_length(ddf_fun_t *fun)
    139131{
    140132        usb_log_debug("Generic HID: Get report descriptor length.\n");
    141133
    142         const usb_hid_dev_t *hid_dev = fun_hid_dev(fun);
     134        if (fun == NULL || fun->driver_data == NULL) {
     135                usb_log_debug("No function");
     136                return EINVAL;
     137        }
     138
     139        const usb_hid_dev_t *hid_dev = fun->driver_data;
    143140
    144141        usb_log_debug2("hid_dev->report_desc_size = %zu\n",
     
    147144        return hid_dev->report_desc_size;
    148145}
    149 
     146/*----------------------------------------------------------------------------*/
    150147static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
    151148    size_t size, size_t *actual_size)
     
    153150        usb_log_debug2("Generic HID: Get report descriptor.\n");
    154151
    155         const usb_hid_dev_t *hid_dev = fun_hid_dev(fun);
     152        if (fun == NULL || fun->driver_data == NULL) {
     153                usb_log_debug("No function");
     154                return EINVAL;
     155        }
     156
     157        const usb_hid_dev_t *hid_dev = fun->driver_data;
    156158
    157159        if (hid_dev->report_desc_size > size) {
     
    164166        return EOK;
    165167}
    166 
     168/*----------------------------------------------------------------------------*/
    167169static int usb_generic_hid_client_connected(ddf_fun_t *fun)
    168170{
     
    170172        return EOK;
    171173}
    172 
     174/*----------------------------------------------------------------------------*/
    173175void usb_generic_hid_deinit(usb_hid_dev_t *hid_dev, void *data)
    174176{
     
    181183                return;
    182184        }
    183         usb_log_debug2("%s unbound.\n", ddf_fun_get_name(fun));
     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;
    184189        ddf_fun_destroy(fun);
    185190}
    186 
     191/*----------------------------------------------------------------------------*/
    187192int usb_generic_hid_init(usb_hid_dev_t *hid_dev, void **data)
    188193{
    189         usb_hid_gen_fun_t *hid_fun;
    190 
    191194        if (hid_dev == NULL) {
    192195                return EINVAL;
     
    202205        }
    203206
    204         /* Create softstate */
    205         hid_fun = ddf_fun_data_alloc(fun, sizeof(usb_hid_gen_fun_t));
    206         hid_fun->hid_dev = hid_dev;
    207         ddf_fun_set_ops(fun, &usb_generic_hid_ops);
     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;
     210        fun->ops = &usb_generic_hid_ops;
    208211
    209212        int rc = ddf_fun_bind(fun);
     
    211214                usb_log_error("Could not bind DDF function: %s.\n",
    212215                    str_error(rc));
     216                fun->driver_data = NULL;
    213217                ddf_fun_destroy(fun);
    214218                return rc;
    215219        }
    216220
    217         usb_log_debug("HID function created. Handle: %" PRIun "\n",
    218             ddf_fun_get_handle(fun));
     221        usb_log_debug("HID function created. Handle: %" PRIun "\n", fun->handle);
    219222        *data = fun;
    220223
    221224        return EOK;
    222225}
    223 
     226/*----------------------------------------------------------------------------*/
    224227bool usb_generic_hid_polling_callback(usb_hid_dev_t *hid_dev, void *data)
    225228{
Note: See TracChangeset for help on using the changeset viewer.