Ignore:
File:
1 edited

Legend:

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

    ra0c05e7 r15f3c3f  
    4848/*----------------------------------------------------------------------------*/
    4949
    50 const usb_endpoint_description_t usb_hid_generic_poll_endpoint_description = {
     50usb_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,
    5654        .flags = 0
    5755};
     
    6159
    6260/*----------------------------------------------------------------------------*/
     61
    6362static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun);
    64 static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
     63
     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
    6667static int usb_generic_hid_client_connected(ddf_fun_t *fun);
     68
    6769static size_t usb_generic_get_report_descriptor_length(ddf_fun_t *fun);
    68 static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
     70
     71static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
    6972    size_t size, size_t *actual_size);
    70 /*----------------------------------------------------------------------------*/
     73
     74/*----------------------------------------------------------------------------*/
     75
    7176static usbhid_iface_t usb_generic_iface = {
    7277        .get_event = usb_generic_hid_get_event,
     
    7580        .get_report_descriptor = usb_generic_get_report_descriptor
    7681};
    77 /*----------------------------------------------------------------------------*/
     82
    7883static ddf_dev_ops_t usb_generic_hid_ops = {
    7984        .interfaces[USBHID_DEV_IFACE] = &usb_generic_iface,
    8085        .open = usb_generic_hid_client_connected
    8186};
    82 /*----------------------------------------------------------------------------*/
     87
     88/*----------------------------------------------------------------------------*/
     89
    8390static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun)
    8491{
    8592        usb_log_debug2("Generic HID: Get event length (fun: %p, "
    8693            "fun->driver_data: %p.\n", fun, fun->driver_data);
    87 
     94       
    8895        if (fun == NULL || fun->driver_data == NULL) {
    8996                return 0;
    9097        }
    9198
    92         const usb_hid_dev_t *hid_dev = fun->driver_data;
    93 
     99        usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
     100       
    94101        usb_log_debug2("hid_dev: %p, Max input report size (%zu).\n",
    95102            hid_dev, hid_dev->max_input_report_size);
    96 
     103       
    97104        return hid_dev->max_input_report_size;
    98105}
    99 /*----------------------------------------------------------------------------*/
    100 static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
     106
     107/*----------------------------------------------------------------------------*/
     108
     109static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
    101110    size_t size, size_t *act_size, int *event_nr, unsigned int flags)
    102111{
    103112        usb_log_debug2("Generic HID: Get event.\n");
    104 
     113       
    105114        if (fun == NULL || fun->driver_data == NULL || buffer == NULL
    106115            || act_size == NULL || event_nr == NULL) {
     
    109118        }
    110119
    111         const usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
    112 
     120        usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
     121       
    113122        if (hid_dev->input_report_size > size) {
    114                 usb_log_debug("input_report_size > size (%zu, %zu)\n",
     123                usb_log_debug("input_report_size > size (%zu, %zu)\n", 
    115124                    hid_dev->input_report_size, size);
    116125                return EINVAL;  // TODO: other error code
    117126        }
    118 
     127       
    119128        /*! @todo This should probably be somehow atomic. */
    120         memcpy(buffer, hid_dev->input_report,
     129        memcpy(buffer, hid_dev->input_report, 
    121130            hid_dev->input_report_size);
    122131        *act_size = hid_dev->input_report_size;
    123132        *event_nr = usb_hid_report_number(hid_dev);
    124 
     133       
    125134        usb_log_debug2("OK\n");
    126 
    127         return EOK;
    128 }
    129 /*----------------------------------------------------------------------------*/
     135       
     136        return EOK;
     137}
     138
     139/*----------------------------------------------------------------------------*/
     140
    130141static size_t usb_generic_get_report_descriptor_length(ddf_fun_t *fun)
    131142{
    132143        usb_log_debug("Generic HID: Get report descriptor length.\n");
    133 
     144       
    134145        if (fun == NULL || fun->driver_data == NULL) {
    135146                usb_log_debug("No function");
    136147                return EINVAL;
    137148        }
    138 
    139         const usb_hid_dev_t *hid_dev = fun->driver_data;
    140 
    141         usb_log_debug2("hid_dev->report_desc_size = %zu\n",
     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", 
    142153            hid_dev->report_desc_size);
    143 
     154       
    144155        return hid_dev->report_desc_size;
    145156}
    146 /*----------------------------------------------------------------------------*/
    147 static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
     157
     158/*----------------------------------------------------------------------------*/
     159
     160static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
    148161    size_t size, size_t *actual_size)
    149162{
    150163        usb_log_debug2("Generic HID: Get report descriptor.\n");
    151 
     164       
    152165        if (fun == NULL || fun->driver_data == NULL) {
    153166                usb_log_debug("No function");
    154167                return EINVAL;
    155168        }
    156 
    157         const usb_hid_dev_t *hid_dev = fun->driver_data;
    158 
     169       
     170        usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
     171       
    159172        if (hid_dev->report_desc_size > size) {
    160173                return EINVAL;
    161174        }
    162 
     175       
    163176        memcpy(desc, hid_dev->report_desc, hid_dev->report_desc_size);
    164177        *actual_size = hid_dev->report_desc_size;
    165 
    166         return EOK;
    167 }
    168 /*----------------------------------------------------------------------------*/
     178       
     179        return EOK;
     180}
     181
     182/*----------------------------------------------------------------------------*/
     183
    169184static int usb_generic_hid_client_connected(ddf_fun_t *fun)
    170185{
     
    172187        return EOK;
    173188}
    174 /*----------------------------------------------------------------------------*/
    175 void 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 /*----------------------------------------------------------------------------*/
    192 int usb_generic_hid_init(usb_hid_dev_t *hid_dev, void **data)
    193 {
    194         if (hid_dev == NULL) {
    195                 return EINVAL;
    196         }
    197 
     189
     190/*----------------------------------------------------------------------------*/
     191
     192static int usb_generic_hid_create_function(usb_hid_dev_t *hid_dev)
     193{       
    198194        /* Create the exposed function. */
     195        /** @todo Generate numbers for the devices? */
    199196        usb_log_debug("Creating DDF function %s...\n", HID_GENERIC_FUN_NAME);
    200197        ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
     
    204201                return ENOMEM;
    205202        }
    206 
    207         /* This is nasty, both device and this function have the same
    208          * driver data, thus destruction causes to double free */
     203       
     204        fun->ops = &usb_generic_hid_ops;
    209205        fun->driver_data = hid_dev;
    210         fun->ops = &usb_generic_hid_ops;
    211206
    212207        int rc = ddf_fun_bind(fun);
     
    214209                usb_log_error("Could not bind DDF function: %s.\n",
    215210                    str_error(rc));
    216                 fun->driver_data = NULL;
    217211                ddf_fun_destroy(fun);
    218212                return rc;
    219213        }
    220 
     214       
    221215        usb_log_debug("HID function created. Handle: %" PRIun "\n", fun->handle);
    222         *data = fun;
    223 
    224         return EOK;
    225 }
    226 /*----------------------------------------------------------------------------*/
     216       
     217        return EOK;
     218}
     219
     220/*----------------------------------------------------------------------------*/
     221
     222int 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
    227233bool usb_generic_hid_polling_callback(usb_hid_dev_t *hid_dev, void *data)
    228234{
    229235        return true;
    230236}
     237
    231238/**
    232239 * @}
Note: See TracChangeset for help on using the changeset viewer.