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