| 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>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <str_error.h>
|
|---|
| 41 | #include <bool.h>
|
|---|
| 42 |
|
|---|
| 43 | #include <usbhid_iface.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "hiddev.h"
|
|---|
| 46 | #include "usbhid.h"
|
|---|
| 47 |
|
|---|
| 48 | /*----------------------------------------------------------------------------*/
|
|---|
| 49 |
|
|---|
| 50 | usb_endpoint_description_t usb_hid_generic_poll_endpoint_description = {
|
|---|
| 51 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
|---|
| 52 | .direction = USB_DIRECTION_IN,
|
|---|
| 53 | .interface_class = USB_CLASS_HID,
|
|---|
| 54 | .flags = 0
|
|---|
| 55 | };
|
|---|
| 56 |
|
|---|
| 57 | const char *HID_GENERIC_FUN_NAME = "hid";
|
|---|
| 58 | const char *HID_GENERIC_CLASS_NAME = "hid";
|
|---|
| 59 |
|
|---|
| 60 | /*----------------------------------------------------------------------------*/
|
|---|
| 61 |
|
|---|
| 62 | static 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, int32_t *buffer,
|
|---|
| 65 | size_t size, size_t *act_size, unsigned int flags);
|
|---|
| 66 |
|
|---|
| 67 | static int usb_generic_hid_client_connected(ddf_fun_t *fun);
|
|---|
| 68 |
|
|---|
| 69 | /*----------------------------------------------------------------------------*/
|
|---|
| 70 |
|
|---|
| 71 | static usbhid_iface_t usb_generic_iface = {
|
|---|
| 72 | .get_event = usb_generic_hid_get_event,
|
|---|
| 73 | .get_event_length = usb_generic_hid_get_event_length
|
|---|
| 74 | };
|
|---|
| 75 |
|
|---|
| 76 | static ddf_dev_ops_t usb_generic_hid_ops = {
|
|---|
| 77 | .interfaces[USBHID_DEV_IFACE] = &usb_generic_iface,
|
|---|
| 78 | .open = usb_generic_hid_client_connected
|
|---|
| 79 | };
|
|---|
| 80 |
|
|---|
| 81 | /*----------------------------------------------------------------------------*/
|
|---|
| 82 |
|
|---|
| 83 | static size_t usb_generic_hid_get_event_length(ddf_fun_t *fun)
|
|---|
| 84 | {
|
|---|
| 85 | if (fun == NULL || fun->driver_data) {
|
|---|
| 86 | return 0;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
|
|---|
| 90 |
|
|---|
| 91 | return hid_dev->input_report_size;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /*----------------------------------------------------------------------------*/
|
|---|
| 95 |
|
|---|
| 96 | static int usb_generic_hid_get_event(ddf_fun_t *fun, int32_t *buffer,
|
|---|
| 97 | size_t size, size_t *act_size, unsigned int flags)
|
|---|
| 98 | {
|
|---|
| 99 | if (fun == NULL || fun->driver_data) {
|
|---|
| 100 | return EINVAL;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)fun->driver_data;
|
|---|
| 104 |
|
|---|
| 105 | if (hid_dev->input_report_size > size) {
|
|---|
| 106 | return EINVAL; // TODO: other error code
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /*! @todo This should probably be atomic. */
|
|---|
| 110 | if (usb_hid_report_ready()) {
|
|---|
| 111 | memcpy(buffer, hid_dev->input_report,
|
|---|
| 112 | hid_dev->input_report_size);
|
|---|
| 113 | *act_size = hid_dev->input_report_size;
|
|---|
| 114 | usb_hid_report_received();
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | // clear the buffer so that it will not be received twice
|
|---|
| 118 | //memset(hid_dev->input_report, 0, hid_dev->input_report_size);
|
|---|
| 119 |
|
|---|
| 120 | // note that we already received this report
|
|---|
| 121 | // report_received = true;
|
|---|
| 122 |
|
|---|
| 123 | return EOK;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /*----------------------------------------------------------------------------*/
|
|---|
| 127 |
|
|---|
| 128 | static int usb_generic_hid_client_connected(ddf_fun_t *fun)
|
|---|
| 129 | {
|
|---|
| 130 | usb_hid_report_received();
|
|---|
| 131 | return EOK;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /*----------------------------------------------------------------------------*/
|
|---|
| 135 |
|
|---|
| 136 | static int usb_generic_hid_create_function(usb_hid_dev_t *hid_dev)
|
|---|
| 137 | {
|
|---|
| 138 | /* Create the function exposed under /dev/devices. */
|
|---|
| 139 | /** @todo Generate numbers for the devices? */
|
|---|
| 140 | usb_log_debug("Creating DDF function %s...\n", HID_GENERIC_FUN_NAME);
|
|---|
| 141 | ddf_fun_t *fun = ddf_fun_create(hid_dev->usb_dev->ddf_dev, fun_exposed,
|
|---|
| 142 | HID_GENERIC_FUN_NAME);
|
|---|
| 143 | if (fun == NULL) {
|
|---|
| 144 | usb_log_error("Could not create DDF function node.\n");
|
|---|
| 145 | return ENOMEM;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | int rc = ddf_fun_bind(fun);
|
|---|
| 149 | if (rc != EOK) {
|
|---|
| 150 | usb_log_error("Could not bind DDF function: %s.\n",
|
|---|
| 151 | str_error(rc));
|
|---|
| 152 | ddf_fun_destroy(fun);
|
|---|
| 153 | return rc;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | fun->ops = &usb_generic_hid_ops;
|
|---|
| 157 | fun->driver_data = hid_dev;
|
|---|
| 158 |
|
|---|
| 159 | return EOK;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /*----------------------------------------------------------------------------*/
|
|---|
| 163 |
|
|---|
| 164 | int usb_generic_hid_init(usb_hid_dev_t *hid_dev)
|
|---|
| 165 | {
|
|---|
| 166 | if (hid_dev == NULL) {
|
|---|
| 167 | return EINVAL;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | return usb_generic_hid_create_function(hid_dev);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /*----------------------------------------------------------------------------*/
|
|---|
| 174 |
|
|---|
| 175 | bool usb_generic_hid_polling_callback(usb_hid_dev_t *hid_dev,
|
|---|
| 176 | uint8_t *buffer, size_t buffer_size)
|
|---|
| 177 | {
|
|---|
| 178 | usb_log_debug("usb_hid_polling_callback(%p, %p, %zu)\n",
|
|---|
| 179 | hid_dev, buffer, buffer_size);
|
|---|
| 180 | usb_debug_str_buffer(buffer, buffer_size, 0);
|
|---|
| 181 | return true;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | /**
|
|---|
| 185 | * @}
|
|---|
| 186 | */
|
|---|