[61257f4] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Lubos Slovak
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup drvusbhid
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * USB HID driver API.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <usb/debug.h>
|
---|
| 38 | #include <usb/classes/classes.h>
|
---|
[3facf63a] | 39 | #include <errno.h>
|
---|
| 40 | #include <str_error.h>
|
---|
[dd3eda2] | 41 | #include <bool.h>
|
---|
[3facf63a] | 42 |
|
---|
| 43 | #include <usbhid_iface.h>
|
---|
[61257f4] | 44 |
|
---|
[dd10e07] | 45 | #include "hiddev.h"
|
---|
[60c0573] | 46 | #include "usbhid.h"
|
---|
[61257f4] | 47 |
|
---|
| 48 | /*----------------------------------------------------------------------------*/
|
---|
| 49 |
|
---|
[b803845] | 50 | const usb_endpoint_description_t usb_hid_generic_poll_endpoint_description = {
|
---|
[61257f4] | 51 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
---|
| 52 | .direction = USB_DIRECTION_IN,
|
---|
| 53 | .interface_class = USB_CLASS_HID,
|
---|
[aaf835d] | 54 | .interface_subclass = -1,
|
---|
| 55 | .interface_protocol = -1,
|
---|
[61257f4] | 56 | .flags = 0
|
---|
| 57 | };
|
---|
| 58 |
|
---|
| 59 | const char *HID_GENERIC_FUN_NAME = "hid";
|
---|
| 60 | const char *HID_GENERIC_CLASS_NAME = "hid";
|
---|
| 61 |
|
---|
| 62 | /*----------------------------------------------------------------------------*/
|
---|
[3facf63a] | 63 | static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun);
|
---|
[5f047d0] | 64 | static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
|
---|
[266fcd8] | 65 | size_t size, size_t *act_size, int *event_nr, unsigned int flags);
|
---|
[dd3eda2] | 66 | static int usb_generic_hid_client_connected(ddf_fun_t *fun);
|
---|
[d7c72db] | 67 | static size_t usb_generic_get_report_descriptor_length(ddf_fun_t *fun);
|
---|
[5f047d0] | 68 | static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
|
---|
[d7c72db] | 69 | size_t size, size_t *actual_size);
|
---|
[3facf63a] | 70 | /*----------------------------------------------------------------------------*/
|
---|
| 71 | static usbhid_iface_t usb_generic_iface = {
|
---|
| 72 | .get_event = usb_generic_hid_get_event,
|
---|
[d7c72db] | 73 | .get_event_length = usb_generic_hid_get_event_length,
|
---|
| 74 | .get_report_descriptor_length = usb_generic_get_report_descriptor_length,
|
---|
| 75 | .get_report_descriptor = usb_generic_get_report_descriptor
|
---|
[3facf63a] | 76 | };
|
---|
[5f047d0] | 77 | /*----------------------------------------------------------------------------*/
|
---|
[3facf63a] | 78 | static ddf_dev_ops_t usb_generic_hid_ops = {
|
---|
[dd3eda2] | 79 | .interfaces[USBHID_DEV_IFACE] = &usb_generic_iface,
|
---|
| 80 | .open = usb_generic_hid_client_connected
|
---|
[3facf63a] | 81 | };
|
---|
| 82 | /*----------------------------------------------------------------------------*/
|
---|
| 83 | static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun)
|
---|
| 84 | {
|
---|
[ff41576d] | 85 | usb_log_debug2("Generic HID: Get event length (fun: %p, "
|
---|
[4e78236] | 86 | "fun->driver_data: %p.\n", fun, fun->driver_data);
|
---|
[cc29622] | 87 |
|
---|
[4e78236] | 88 | if (fun == NULL || fun->driver_data == NULL) {
|
---|
[3facf63a] | 89 | return 0;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[5f047d0] | 92 | const usb_hid_dev_t *hid_dev = fun->driver_data;
|
---|
[cc29622] | 93 |
|
---|
[0d59d0e9] | 94 | usb_log_debug2("hid_dev: %p, Max input report size (%zu).\n",
|
---|
[4e78236] | 95 | hid_dev, hid_dev->max_input_report_size);
|
---|
[cc29622] | 96 |
|
---|
[4e78236] | 97 | return hid_dev->max_input_report_size;
|
---|
[3facf63a] | 98 | }
|
---|
| 99 | /*----------------------------------------------------------------------------*/
|
---|
[5f047d0] | 100 | static int usb_generic_hid_get_event(ddf_fun_t *fun, uint8_t *buffer,
|
---|
[266fcd8] | 101 | size_t size, size_t *act_size, int *event_nr, unsigned int flags)
|
---|
[3facf63a] | 102 | {
|
---|
[ff41576d] | 103 | usb_log_debug2("Generic HID: Get event.\n");
|
---|
[cc29622] | 104 |
|
---|
[266fcd8] | 105 | if (fun == NULL || fun->driver_data == NULL || buffer == NULL
|
---|
| 106 | || act_size == NULL || event_nr == NULL) {
|
---|
[4e78236] | 107 | usb_log_debug("No function");
|
---|
[3facf63a] | 108 | return EINVAL;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[5f047d0] | 111 | const usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
|
---|
[cc29622] | 112 |
|
---|
[3facf63a] | 113 | if (hid_dev->input_report_size > size) {
|
---|
[5f047d0] | 114 | usb_log_debug("input_report_size > size (%zu, %zu)\n",
|
---|
[da56be2] | 115 | hid_dev->input_report_size, size);
|
---|
[3facf63a] | 116 | return EINVAL; // TODO: other error code
|
---|
| 117 | }
|
---|
[cc29622] | 118 |
|
---|
[19e0560e] | 119 | /*! @todo This should probably be somehow atomic. */
|
---|
[5f047d0] | 120 | memcpy(buffer, hid_dev->input_report,
|
---|
[ff41576d] | 121 | hid_dev->input_report_size);
|
---|
| 122 | *act_size = hid_dev->input_report_size;
|
---|
[266fcd8] | 123 | *event_nr = usb_hid_report_number(hid_dev);
|
---|
[cc29622] | 124 |
|
---|
[ff41576d] | 125 | usb_log_debug2("OK\n");
|
---|
[cc29622] | 126 |
|
---|
[3facf63a] | 127 | return EOK;
|
---|
| 128 | }
|
---|
| 129 | /*----------------------------------------------------------------------------*/
|
---|
[d7c72db] | 130 | static size_t usb_generic_get_report_descriptor_length(ddf_fun_t *fun)
|
---|
| 131 | {
|
---|
| 132 | usb_log_debug("Generic HID: Get report descriptor length.\n");
|
---|
[cc29622] | 133 |
|
---|
[d7c72db] | 134 | if (fun == NULL || fun->driver_data == NULL) {
|
---|
| 135 | usb_log_debug("No function");
|
---|
| 136 | return EINVAL;
|
---|
| 137 | }
|
---|
[cc29622] | 138 |
|
---|
[5f047d0] | 139 | const usb_hid_dev_t *hid_dev = fun->driver_data;
|
---|
[cc29622] | 140 |
|
---|
[5f047d0] | 141 | usb_log_debug2("hid_dev->report_desc_size = %zu\n",
|
---|
[ff41576d] | 142 | hid_dev->report_desc_size);
|
---|
[cc29622] | 143 |
|
---|
[d7c72db] | 144 | return hid_dev->report_desc_size;
|
---|
| 145 | }
|
---|
| 146 | /*----------------------------------------------------------------------------*/
|
---|
[5f047d0] | 147 | static int usb_generic_get_report_descriptor(ddf_fun_t *fun, uint8_t *desc,
|
---|
[d7c72db] | 148 | size_t size, size_t *actual_size)
|
---|
| 149 | {
|
---|
[ff41576d] | 150 | usb_log_debug2("Generic HID: Get report descriptor.\n");
|
---|
[cc29622] | 151 |
|
---|
[d7c72db] | 152 | if (fun == NULL || fun->driver_data == NULL) {
|
---|
| 153 | usb_log_debug("No function");
|
---|
| 154 | return EINVAL;
|
---|
| 155 | }
|
---|
[cc29622] | 156 |
|
---|
[5f047d0] | 157 | const usb_hid_dev_t *hid_dev = fun->driver_data;
|
---|
[cc29622] | 158 |
|
---|
[d7c72db] | 159 | if (hid_dev->report_desc_size > size) {
|
---|
[19e0560e] | 160 | return EINVAL;
|
---|
[d7c72db] | 161 | }
|
---|
[cc29622] | 162 |
|
---|
[d7c72db] | 163 | memcpy(desc, hid_dev->report_desc, hid_dev->report_desc_size);
|
---|
| 164 | *actual_size = hid_dev->report_desc_size;
|
---|
[cc29622] | 165 |
|
---|
[d7c72db] | 166 | return EOK;
|
---|
| 167 | }
|
---|
| 168 | /*----------------------------------------------------------------------------*/
|
---|
[dd3eda2] | 169 | static int usb_generic_hid_client_connected(ddf_fun_t *fun)
|
---|
| 170 | {
|
---|
[4939490] | 171 | usb_log_debug("Generic HID: Client connected.\n");
|
---|
[dd3eda2] | 172 | return EOK;
|
---|
| 173 | }
|
---|
| 174 | /*----------------------------------------------------------------------------*/
|
---|
[c5b6db53] | 175 | void usb_generic_hid_deinit(usb_hid_dev_t *hid_dev, void *data)
|
---|
| 176 | {
|
---|
| 177 | ddf_fun_t *fun = data;
|
---|
[5f047d0] | 178 | if (fun == NULL)
|
---|
| 179 | return;
|
---|
| 180 |
|
---|
[c5b6db53] | 181 | const int ret = ddf_fun_unbind(fun);
|
---|
| 182 | if (ret != EOK) {
|
---|
[2a5b62b] | 183 | usb_log_error("Failed to unbind generic hid fun.\n");
|
---|
[c5b6db53] | 184 | return;
|
---|
| 185 | }
|
---|
[2a5b62b] | 186 | usb_log_debug2("%s unbound.\n", fun->name);
|
---|
[065064e6] | 187 | /* We did not allocate this, so leave this alone
|
---|
| 188 | * the device would take care of it */
|
---|
| 189 | fun->driver_data = NULL;
|
---|
[c5b6db53] | 190 | ddf_fun_destroy(fun);
|
---|
| 191 | }
|
---|
| 192 | /*----------------------------------------------------------------------------*/
|
---|
| 193 | int usb_generic_hid_init(usb_hid_dev_t *hid_dev, void **data)
|
---|
| 194 | {
|
---|
| 195 | if (hid_dev == NULL) {
|
---|
| 196 | return EINVAL;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[15f3c3f] | 199 | /* Create the exposed function. */
|
---|
[3facf63a] | 200 | usb_log_debug("Creating DDF function %s...\n", HID_GENERIC_FUN_NAME);
|
---|
| 201 | ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
|
---|
| 202 | HID_GENERIC_FUN_NAME);
|
---|
| 203 | if (fun == NULL) {
|
---|
| 204 | usb_log_error("Could not create DDF function node.\n");
|
---|
| 205 | return ENOMEM;
|
---|
| 206 | }
|
---|
[cc29622] | 207 |
|
---|
[5f047d0] | 208 | /* This is nasty, both device and this function have the same
|
---|
| 209 | * driver data, thus destruction causes to double free */
|
---|
| 210 | fun->driver_data = hid_dev;
|
---|
[4939490] | 211 | fun->ops = &usb_generic_hid_ops;
|
---|
[3facf63a] | 212 |
|
---|
| 213 | int rc = ddf_fun_bind(fun);
|
---|
| 214 | if (rc != EOK) {
|
---|
| 215 | usb_log_error("Could not bind DDF function: %s.\n",
|
---|
| 216 | str_error(rc));
|
---|
[5f047d0] | 217 | fun->driver_data = NULL;
|
---|
[3facf63a] | 218 | ddf_fun_destroy(fun);
|
---|
| 219 | return rc;
|
---|
| 220 | }
|
---|
[cc29622] | 221 |
|
---|
[0d59d0e9] | 222 | usb_log_debug("HID function created. Handle: %" PRIun "\n", fun->handle);
|
---|
[c5b6db53] | 223 | *data = fun;
|
---|
[cc29622] | 224 |
|
---|
[3facf63a] | 225 | return EOK;
|
---|
| 226 | }
|
---|
| 227 | /*----------------------------------------------------------------------------*/
|
---|
[4d3c13e] | 228 | bool usb_generic_hid_polling_callback(usb_hid_dev_t *hid_dev, void *data)
|
---|
[61257f4] | 229 | {
|
---|
| 230 | return true;
|
---|
| 231 | }
|
---|
| 232 | /**
|
---|
| 233 | * @}
|
---|
| 234 | */
|
---|