| [966acede] | 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>
|
|---|
| [61257f4] | 39 | #include <usb/classes/hid.h>
|
|---|
| 40 | #include <usb/classes/hidparser.h>
|
|---|
| 41 | #include <usb/classes/hidreport.h>
|
|---|
| [7f2e33a] | 42 | #include <usb/classes/hidreq.h>
|
|---|
| [61257f4] | 43 | #include <errno.h>
|
|---|
| [f76153ce] | 44 | #include <str_error.h>
|
|---|
| [966acede] | 45 |
|
|---|
| 46 | #include "usbhid.h"
|
|---|
| 47 |
|
|---|
| [61257f4] | 48 | #include "kbd/kbddev.h"
|
|---|
| [dd10e07] | 49 | #include "generic/hiddev.h"
|
|---|
| [e9f0348] | 50 | #include "mouse/mousedev.h"
|
|---|
| [62bd8d3] | 51 | #include "subdrivers.h"
|
|---|
| [61257f4] | 52 |
|
|---|
| [966acede] | 53 | /*----------------------------------------------------------------------------*/
|
|---|
| 54 |
|
|---|
| 55 | /* Array of endpoints expected on the device, NULL terminated. */
|
|---|
| [fec47d4] | 56 | usb_endpoint_description_t *usb_hid_endpoints[USB_HID_POLL_EP_COUNT + 1] = {
|
|---|
| 57 | &usb_hid_kbd_poll_endpoint_description,
|
|---|
| 58 | &usb_hid_mouse_poll_endpoint_description,
|
|---|
| [61257f4] | 59 | &usb_hid_generic_poll_endpoint_description,
|
|---|
| [966acede] | 60 | NULL
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| [f76153ce] | 63 | static const int USB_HID_MAX_SUBDRIVERS = 10;
|
|---|
| 64 |
|
|---|
| [dd3eda2] | 65 | static fibril_local bool report_received;
|
|---|
| 66 |
|
|---|
| [966acede] | 67 | /*----------------------------------------------------------------------------*/
|
|---|
| 68 |
|
|---|
| [60c0573] | 69 | static int usb_hid_set_boot_kbd_subdriver(usb_hid_dev_t *hid_dev)
|
|---|
| [61257f4] | 70 | {
|
|---|
| [e7df6cd] | 71 | assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
|
|---|
| [61257f4] | 72 |
|
|---|
| [60c0573] | 73 | hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(
|
|---|
| 74 | sizeof(usb_hid_subdriver_t));
|
|---|
| 75 | if (hid_dev->subdrivers == NULL) {
|
|---|
| 76 | return ENOMEM;
|
|---|
| [61257f4] | 77 | }
|
|---|
| 78 |
|
|---|
| [60c0573] | 79 | // set the init callback
|
|---|
| 80 | hid_dev->subdrivers[0].init = usb_kbd_init;
|
|---|
| 81 |
|
|---|
| 82 | // set the polling callback
|
|---|
| 83 | hid_dev->subdrivers[0].poll = usb_kbd_polling_callback;
|
|---|
| 84 |
|
|---|
| 85 | // set the polling ended callback
|
|---|
| 86 | hid_dev->subdrivers[0].poll_end = NULL;
|
|---|
| 87 |
|
|---|
| 88 | // set the deinit callback
|
|---|
| 89 | hid_dev->subdrivers[0].deinit = usb_kbd_deinit;
|
|---|
| 90 |
|
|---|
| 91 | // set subdriver count
|
|---|
| 92 | hid_dev->subdriver_count = 1;
|
|---|
| 93 |
|
|---|
| 94 | return EOK;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /*----------------------------------------------------------------------------*/
|
|---|
| 98 |
|
|---|
| 99 | static int usb_hid_set_boot_mouse_subdriver(usb_hid_dev_t *hid_dev)
|
|---|
| 100 | {
|
|---|
| [e7df6cd] | 101 | assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
|
|---|
| [60c0573] | 102 |
|
|---|
| 103 | hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(
|
|---|
| 104 | sizeof(usb_hid_subdriver_t));
|
|---|
| 105 | if (hid_dev->subdrivers == NULL) {
|
|---|
| 106 | return ENOMEM;
|
|---|
| [61257f4] | 107 | }
|
|---|
| 108 |
|
|---|
| [60c0573] | 109 | // set the init callback
|
|---|
| 110 | hid_dev->subdrivers[0].init = usb_mouse_init;
|
|---|
| 111 |
|
|---|
| 112 | // set the polling callback
|
|---|
| 113 | hid_dev->subdrivers[0].poll = usb_mouse_polling_callback;
|
|---|
| 114 |
|
|---|
| 115 | // set the polling ended callback
|
|---|
| 116 | hid_dev->subdrivers[0].poll_end = NULL;
|
|---|
| 117 |
|
|---|
| 118 | // set the deinit callback
|
|---|
| 119 | hid_dev->subdrivers[0].deinit = usb_mouse_deinit;
|
|---|
| 120 |
|
|---|
| 121 | // set subdriver count
|
|---|
| 122 | hid_dev->subdriver_count = 1;
|
|---|
| 123 |
|
|---|
| 124 | return EOK;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /*----------------------------------------------------------------------------*/
|
|---|
| 128 |
|
|---|
| 129 | static int usb_hid_set_generic_hid_subdriver(usb_hid_dev_t *hid_dev)
|
|---|
| 130 | {
|
|---|
| [e7df6cd] | 131 | assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
|
|---|
| [60c0573] | 132 |
|
|---|
| 133 | hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(
|
|---|
| 134 | sizeof(usb_hid_subdriver_t));
|
|---|
| 135 | if (hid_dev->subdrivers == NULL) {
|
|---|
| 136 | return ENOMEM;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | // set the init callback
|
|---|
| [3facf63a] | 140 | hid_dev->subdrivers[0].init = usb_generic_hid_init;
|
|---|
| [60c0573] | 141 |
|
|---|
| 142 | // set the polling callback
|
|---|
| 143 | hid_dev->subdrivers[0].poll = usb_generic_hid_polling_callback;
|
|---|
| 144 |
|
|---|
| 145 | // set the polling ended callback
|
|---|
| 146 | hid_dev->subdrivers[0].poll_end = NULL;
|
|---|
| 147 |
|
|---|
| 148 | // set the deinit callback
|
|---|
| 149 | hid_dev->subdrivers[0].deinit = NULL;
|
|---|
| 150 |
|
|---|
| [f76153ce] | 151 | // set subdriver count
|
|---|
| 152 | hid_dev->subdriver_count = 1;
|
|---|
| 153 |
|
|---|
| [60c0573] | 154 | return EOK;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /*----------------------------------------------------------------------------*/
|
|---|
| 158 |
|
|---|
| [f76153ce] | 159 | static bool usb_hid_ids_match(usb_hid_dev_t *hid_dev,
|
|---|
| 160 | const usb_hid_subdriver_mapping_t *mapping)
|
|---|
| 161 | {
|
|---|
| [36f737a] | 162 | assert(hid_dev != NULL);
|
|---|
| 163 | assert(hid_dev->usb_dev != NULL);
|
|---|
| 164 |
|
|---|
| 165 | return (hid_dev->usb_dev->descriptors.device.vendor_id
|
|---|
| 166 | == mapping->vendor_id
|
|---|
| 167 | && hid_dev->usb_dev->descriptors.device.product_id
|
|---|
| 168 | == mapping->product_id);
|
|---|
| [f76153ce] | 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /*----------------------------------------------------------------------------*/
|
|---|
| 172 |
|
|---|
| 173 | static bool usb_hid_path_matches(usb_hid_dev_t *hid_dev,
|
|---|
| [e3b5129] | 174 | const usb_hid_subdriver_mapping_t *mapping)
|
|---|
| [f76153ce] | 175 | {
|
|---|
| 176 | assert(hid_dev != NULL);
|
|---|
| [e3b5129] | 177 | assert(mapping != NULL);
|
|---|
| [f76153ce] | 178 |
|
|---|
| 179 | usb_hid_report_path_t *usage_path = usb_hid_report_path();
|
|---|
| 180 | if (usage_path == NULL) {
|
|---|
| [1cbb4b7] | 181 | usb_log_debug("Failed to create usage path.\n");
|
|---|
| [f76153ce] | 182 | return false;
|
|---|
| 183 | }
|
|---|
| [777e336] | 184 | int i = 0;
|
|---|
| [e3b5129] | 185 | while (mapping->usage_path[i].usage != 0
|
|---|
| 186 | || mapping->usage_path[i].usage_page != 0) {
|
|---|
| [f76153ce] | 187 | if (usb_hid_report_path_append_item(usage_path,
|
|---|
| [e3b5129] | 188 | mapping->usage_path[i].usage_page,
|
|---|
| 189 | mapping->usage_path[i].usage) != EOK) {
|
|---|
| [1cbb4b7] | 190 | usb_log_debug("Failed to append to usage path.\n");
|
|---|
| [f76153ce] | 191 | usb_hid_report_path_free(usage_path);
|
|---|
| 192 | return false;
|
|---|
| 193 | }
|
|---|
| [777e336] | 194 | ++i;
|
|---|
| [f76153ce] | 195 | }
|
|---|
| 196 |
|
|---|
| [e3b5129] | 197 | if (mapping->report_id >= 0) {
|
|---|
| 198 | usb_hid_report_path_set_report_id(usage_path,
|
|---|
| 199 | mapping->report_id);
|
|---|
| [f76153ce] | 200 | }
|
|---|
| 201 |
|
|---|
| [e60436b] | 202 | assert(hid_dev->report != NULL);
|
|---|
| [1cbb4b7] | 203 |
|
|---|
| [e3b5129] | 204 | usb_log_debug("Compare flags: %d\n", mapping->compare);
|
|---|
| [e60436b] | 205 | size_t size = usb_hid_report_input_length(hid_dev->report, usage_path,
|
|---|
| [e3b5129] | 206 | mapping->compare);
|
|---|
| [4125b7d] | 207 | usb_log_debug("Size of the input report: %zuB\n", size);
|
|---|
| [f76153ce] | 208 |
|
|---|
| 209 | usb_hid_report_path_free(usage_path);
|
|---|
| 210 |
|
|---|
| [1cbb4b7] | 211 | return (size > 0);
|
|---|
| [f76153ce] | 212 | }
|
|---|
| 213 |
|
|---|
| 214 | /*----------------------------------------------------------------------------*/
|
|---|
| 215 |
|
|---|
| 216 | static int usb_hid_save_subdrivers(usb_hid_dev_t *hid_dev,
|
|---|
| 217 | const usb_hid_subdriver_t **subdrivers, int count)
|
|---|
| [60c0573] | 218 | {
|
|---|
| [f76153ce] | 219 | int i;
|
|---|
| 220 |
|
|---|
| 221 | if (count <= 0) {
|
|---|
| 222 | hid_dev->subdriver_count = 0;
|
|---|
| 223 | hid_dev->subdrivers = NULL;
|
|---|
| 224 | return EOK;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(count *
|
|---|
| 228 | sizeof(usb_hid_subdriver_t));
|
|---|
| 229 | if (hid_dev->subdrivers == NULL) {
|
|---|
| 230 | return ENOMEM;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | for (i = 0; i < count; ++i) {
|
|---|
| 234 | hid_dev->subdrivers[i].init = subdrivers[i]->init;
|
|---|
| 235 | hid_dev->subdrivers[i].deinit = subdrivers[i]->deinit;
|
|---|
| 236 | hid_dev->subdrivers[i].poll = subdrivers[i]->poll;
|
|---|
| 237 | hid_dev->subdrivers[i].poll_end = subdrivers[i]->poll_end;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | hid_dev->subdriver_count = count;
|
|---|
| 241 |
|
|---|
| [60c0573] | 242 | return EOK;
|
|---|
| [61257f4] | 243 | }
|
|---|
| [966acede] | 244 |
|
|---|
| 245 | /*----------------------------------------------------------------------------*/
|
|---|
| 246 |
|
|---|
| [f76153ce] | 247 | static int usb_hid_find_subdrivers(usb_hid_dev_t *hid_dev)
|
|---|
| 248 | {
|
|---|
| [e7df6cd] | 249 | assert(hid_dev != NULL);
|
|---|
| 250 |
|
|---|
| [f76153ce] | 251 | const usb_hid_subdriver_t *subdrivers[USB_HID_MAX_SUBDRIVERS];
|
|---|
| 252 |
|
|---|
| 253 | int i = 0, count = 0;
|
|---|
| 254 | const usb_hid_subdriver_mapping_t *mapping = &usb_hid_subdrivers[i];
|
|---|
| [4bb9fd2] | 255 |
|
|---|
| 256 | bool ids_matched;
|
|---|
| 257 | bool matched;
|
|---|
| [f76153ce] | 258 |
|
|---|
| 259 | while (count < USB_HID_MAX_SUBDRIVERS &&
|
|---|
| 260 | (mapping->usage_path != NULL
|
|---|
| [d0a6e54] | 261 | || mapping->vendor_id >= 0 || mapping->product_id >= 0)) {
|
|---|
| [f76153ce] | 262 | // check the vendor & product ID
|
|---|
| [d0a6e54] | 263 | if (mapping->vendor_id >= 0 && mapping->product_id < 0) {
|
|---|
| 264 | usb_log_warning("Missing Product ID for Vendor ID %d\n",
|
|---|
| [f76153ce] | 265 | mapping->vendor_id);
|
|---|
| 266 | return EINVAL;
|
|---|
| 267 | }
|
|---|
| [d0a6e54] | 268 | if (mapping->product_id >= 0 && mapping->vendor_id < 0) {
|
|---|
| 269 | usb_log_warning("Missing Vendor ID for Product ID %d\n",
|
|---|
| [f76153ce] | 270 | mapping->product_id);
|
|---|
| 271 | return EINVAL;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| [4bb9fd2] | 274 | ids_matched = false;
|
|---|
| 275 | matched = false;
|
|---|
| 276 |
|
|---|
| [d0a6e54] | 277 | if (mapping->vendor_id >= 0) {
|
|---|
| 278 | assert(mapping->product_id >= 0);
|
|---|
| [777e336] | 279 | usb_log_debug("Comparing device against vendor ID %u"
|
|---|
| 280 | " and product ID %u.\n", mapping->vendor_id,
|
|---|
| [f76153ce] | 281 | mapping->product_id);
|
|---|
| 282 | if (usb_hid_ids_match(hid_dev, mapping)) {
|
|---|
| [4bb9fd2] | 283 | usb_log_debug("IDs matched.\n");
|
|---|
| 284 | ids_matched = true;
|
|---|
| [f76153ce] | 285 | }
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | if (mapping->usage_path != NULL) {
|
|---|
| [1cbb4b7] | 289 | usb_log_debug("Comparing device against usage path.\n");
|
|---|
| [e3b5129] | 290 | if (usb_hid_path_matches(hid_dev, mapping)) {
|
|---|
| [4bb9fd2] | 291 | // does not matter if IDs were matched
|
|---|
| 292 | matched = true;
|
|---|
| [f76153ce] | 293 | }
|
|---|
| [4bb9fd2] | 294 | } else {
|
|---|
| 295 | // matched only if IDs were matched and there is no path
|
|---|
| 296 | matched = ids_matched;
|
|---|
| [f76153ce] | 297 | }
|
|---|
| [4bb9fd2] | 298 |
|
|---|
| 299 | if (matched) {
|
|---|
| 300 | subdrivers[count++] = &mapping->subdriver;
|
|---|
| [f76153ce] | 301 | }
|
|---|
| [4bb9fd2] | 302 |
|
|---|
| [f76153ce] | 303 | mapping = &usb_hid_subdrivers[++i];
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | // we have all subdrivers determined, save them into the hid device
|
|---|
| 307 | return usb_hid_save_subdrivers(hid_dev, subdrivers, count);
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | /*----------------------------------------------------------------------------*/
|
|---|
| 311 |
|
|---|
| [61257f4] | 312 | static int usb_hid_check_pipes(usb_hid_dev_t *hid_dev, usb_device_t *dev)
|
|---|
| [966acede] | 313 | {
|
|---|
| [e7df6cd] | 314 | assert(hid_dev != NULL && dev != NULL);
|
|---|
| 315 |
|
|---|
| [f76153ce] | 316 | int rc = EOK;
|
|---|
| [60c0573] | 317 |
|
|---|
| [61257f4] | 318 | if (dev->pipes[USB_HID_KBD_POLL_EP_NO].present) {
|
|---|
| 319 | usb_log_debug("Found keyboard endpoint.\n");
|
|---|
| [60c0573] | 320 | // save the pipe index
|
|---|
| [61257f4] | 321 | hid_dev->poll_pipe_index = USB_HID_KBD_POLL_EP_NO;
|
|---|
| 322 | } else if (dev->pipes[USB_HID_MOUSE_POLL_EP_NO].present) {
|
|---|
| 323 | usb_log_debug("Found mouse endpoint.\n");
|
|---|
| [60c0573] | 324 | // save the pipe index
|
|---|
| [61257f4] | 325 | hid_dev->poll_pipe_index = USB_HID_MOUSE_POLL_EP_NO;
|
|---|
| 326 | } else if (dev->pipes[USB_HID_GENERIC_POLL_EP_NO].present) {
|
|---|
| 327 | usb_log_debug("Found generic HID endpoint.\n");
|
|---|
| [60c0573] | 328 | // save the pipe index
|
|---|
| [61257f4] | 329 | hid_dev->poll_pipe_index = USB_HID_GENERIC_POLL_EP_NO;
|
|---|
| 330 | } else {
|
|---|
| [60c0573] | 331 | usb_log_error("None of supported endpoints found - probably"
|
|---|
| [61257f4] | 332 | " not a supported device.\n");
|
|---|
| [60c0573] | 333 | rc = ENOTSUP;
|
|---|
| [61257f4] | 334 | }
|
|---|
| 335 |
|
|---|
| [60c0573] | 336 | return rc;
|
|---|
| [61257f4] | 337 | }
|
|---|
| 338 |
|
|---|
| 339 | /*----------------------------------------------------------------------------*/
|
|---|
| 340 |
|
|---|
| [60c0573] | 341 | usb_hid_dev_t *usb_hid_new(void)
|
|---|
| 342 | {
|
|---|
| 343 | usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)calloc(1,
|
|---|
| 344 | sizeof(usb_hid_dev_t));
|
|---|
| 345 |
|
|---|
| 346 | if (hid_dev == NULL) {
|
|---|
| 347 | usb_log_fatal("No memory!\n");
|
|---|
| 348 | return NULL;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| [e60436b] | 351 | hid_dev->report = (usb_hid_report_t *)(malloc(sizeof(
|
|---|
| [e50cd7f] | 352 | usb_hid_report_t)));
|
|---|
| [e60436b] | 353 | if (hid_dev->report == NULL) {
|
|---|
| [60c0573] | 354 | usb_log_fatal("No memory!\n");
|
|---|
| 355 | free(hid_dev);
|
|---|
| 356 | return NULL;
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| [f76153ce] | 359 | hid_dev->poll_pipe_index = -1;
|
|---|
| 360 |
|
|---|
| [60c0573] | 361 | return hid_dev;
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | /*----------------------------------------------------------------------------*/
|
|---|
| 365 |
|
|---|
| [61257f4] | 366 | int usb_hid_init(usb_hid_dev_t *hid_dev, usb_device_t *dev)
|
|---|
| 367 | {
|
|---|
| [60c0573] | 368 | int rc, i;
|
|---|
| [61257f4] | 369 |
|
|---|
| 370 | usb_log_debug("Initializing HID structure...\n");
|
|---|
| 371 |
|
|---|
| 372 | if (hid_dev == NULL) {
|
|---|
| 373 | usb_log_error("Failed to init HID structure: no structure given"
|
|---|
| 374 | ".\n");
|
|---|
| 375 | return EINVAL;
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | if (dev == NULL) {
|
|---|
| 379 | usb_log_error("Failed to init HID structure: no USB device"
|
|---|
| 380 | " given.\n");
|
|---|
| 381 | return EINVAL;
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | /* The USB device should already be initialized, save it in structure */
|
|---|
| 385 | hid_dev->usb_dev = dev;
|
|---|
| 386 |
|
|---|
| 387 | rc = usb_hid_check_pipes(hid_dev, dev);
|
|---|
| 388 | if (rc != EOK) {
|
|---|
| [aaf6155] | 389 | //usb_hid_free(&hid_dev);
|
|---|
| [61257f4] | 390 | return rc;
|
|---|
| 391 | }
|
|---|
| [e50cd7f] | 392 |
|
|---|
| [f76153ce] | 393 | /* Get the report descriptor and parse it. */
|
|---|
| 394 | rc = usb_hid_process_report_descriptor(hid_dev->usb_dev,
|
|---|
| [e60436b] | 395 | hid_dev->report);
|
|---|
| [f76153ce] | 396 |
|
|---|
| 397 | bool fallback = false;
|
|---|
| 398 |
|
|---|
| 399 | if (rc == EOK) {
|
|---|
| 400 | // try to find subdrivers that may want to handle this device
|
|---|
| 401 | rc = usb_hid_find_subdrivers(hid_dev);
|
|---|
| 402 | if (rc != EOK || hid_dev->subdriver_count == 0) {
|
|---|
| 403 | // try to fall back to the boot protocol if available
|
|---|
| 404 | usb_log_info("No subdrivers found to handle this"
|
|---|
| 405 | " device.\n");
|
|---|
| 406 | fallback = true;
|
|---|
| [aaf6155] | 407 | assert(hid_dev->subdrivers == NULL);
|
|---|
| 408 | assert(hid_dev->subdriver_count == 0);
|
|---|
| [f76153ce] | 409 | }
|
|---|
| 410 | } else {
|
|---|
| 411 | usb_log_error("Failed to parse Report descriptor.\n");
|
|---|
| 412 | // try to fall back to the boot protocol if available
|
|---|
| 413 | fallback = true;
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| [7bb8036] | 416 | if (fallback) {
|
|---|
| [f76153ce] | 417 | // fall back to boot protocol
|
|---|
| 418 | switch (hid_dev->poll_pipe_index) {
|
|---|
| 419 | case USB_HID_KBD_POLL_EP_NO:
|
|---|
| 420 | usb_log_info("Falling back to kbd boot protocol.\n");
|
|---|
| 421 | rc = usb_kbd_set_boot_protocol(hid_dev);
|
|---|
| 422 | if (rc == EOK) {
|
|---|
| 423 | rc = usb_hid_set_boot_kbd_subdriver(hid_dev);
|
|---|
| [60c0573] | 424 | }
|
|---|
| [f76153ce] | 425 | break;
|
|---|
| 426 | case USB_HID_MOUSE_POLL_EP_NO:
|
|---|
| 427 | usb_log_info("Falling back to mouse boot protocol.\n");
|
|---|
| 428 | rc = usb_mouse_set_boot_protocol(hid_dev);
|
|---|
| 429 | if (rc == EOK) {
|
|---|
| 430 | rc = usb_hid_set_boot_mouse_subdriver(hid_dev);
|
|---|
| 431 | }
|
|---|
| 432 | break;
|
|---|
| 433 | default:
|
|---|
| 434 | assert(hid_dev->poll_pipe_index
|
|---|
| 435 | == USB_HID_GENERIC_POLL_EP_NO);
|
|---|
| 436 |
|
|---|
| 437 | /* TODO: this has no meaning if the report descriptor
|
|---|
| 438 | is not parsed */
|
|---|
| 439 | usb_log_info("Falling back to generic HID driver.\n");
|
|---|
| 440 | rc = usb_hid_set_generic_hid_subdriver(hid_dev);
|
|---|
| [e9f0348] | 441 | }
|
|---|
| [61257f4] | 442 | }
|
|---|
| 443 |
|
|---|
| [f76153ce] | 444 | if (rc != EOK) {
|
|---|
| 445 | usb_log_error("No subdriver for handling this device could be"
|
|---|
| 446 | " initialized: %s.\n", str_error(rc));
|
|---|
| [aaf6155] | 447 | usb_log_debug("Subdriver count: %d\n",
|
|---|
| 448 | hid_dev->subdriver_count);
|
|---|
| 449 | //usb_hid_free(&hid_dev);
|
|---|
| [f76153ce] | 450 | } else {
|
|---|
| 451 | bool ok = false;
|
|---|
| 452 |
|
|---|
| 453 | usb_log_debug("Subdriver count: %d\n",
|
|---|
| 454 | hid_dev->subdriver_count);
|
|---|
| 455 |
|
|---|
| 456 | for (i = 0; i < hid_dev->subdriver_count; ++i) {
|
|---|
| 457 | if (hid_dev->subdrivers[i].init != NULL) {
|
|---|
| 458 | usb_log_debug("Initializing subdriver %d.\n",i);
|
|---|
| 459 | rc = hid_dev->subdrivers[i].init(hid_dev);
|
|---|
| 460 | if (rc != EOK) {
|
|---|
| 461 | usb_log_warning("Failed to initialize"
|
|---|
| 462 | " HID subdriver structure.\n");
|
|---|
| 463 | } else {
|
|---|
| 464 | // at least one subdriver initialized
|
|---|
| 465 | ok = true;
|
|---|
| 466 | }
|
|---|
| 467 | } else {
|
|---|
| 468 | ok = true;
|
|---|
| 469 | }
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|
| 472 | rc = (ok) ? EOK : -1; // what error to report
|
|---|
| 473 | }
|
|---|
| 474 |
|
|---|
| [61257f4] | 475 | return rc;
|
|---|
| 476 | }
|
|---|
| 477 |
|
|---|
| 478 | /*----------------------------------------------------------------------------*/
|
|---|
| 479 |
|
|---|
| [60c0573] | 480 | bool usb_hid_polling_callback(usb_device_t *dev, uint8_t *buffer,
|
|---|
| 481 | size_t buffer_size, void *arg)
|
|---|
| 482 | {
|
|---|
| 483 | int i;
|
|---|
| 484 |
|
|---|
| 485 | if (dev == NULL || arg == NULL || buffer == NULL) {
|
|---|
| 486 | usb_log_error("Missing arguments to polling callback.\n");
|
|---|
| 487 | return false;
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
|
|---|
| 491 |
|
|---|
| [054537b] | 492 | int allocated = (hid_dev->input_report != NULL);
|
|---|
| [31cfee16] | 493 |
|
|---|
| 494 | if (!allocated
|
|---|
| 495 | || hid_dev->input_report_size < buffer_size) {
|
|---|
| 496 | uint8_t *input_old = hid_dev->input_report;
|
|---|
| 497 | uint8_t *input_new = (uint8_t *)malloc(buffer_size);
|
|---|
| 498 |
|
|---|
| 499 | if (input_new == NULL) {
|
|---|
| 500 | usb_log_error("Failed to allocate space for input "
|
|---|
| 501 | "buffer. This event may not be reported\n");
|
|---|
| 502 | memset(hid_dev->input_report, 0,
|
|---|
| 503 | hid_dev->input_report_size);
|
|---|
| 504 | } else {
|
|---|
| 505 | memcpy(input_new, input_old,
|
|---|
| 506 | hid_dev->input_report_size);
|
|---|
| 507 | hid_dev->input_report = input_new;
|
|---|
| 508 | if (allocated) {
|
|---|
| 509 | free(input_old);
|
|---|
| 510 | }
|
|---|
| [dd3eda2] | 511 | usb_hid_new_report();
|
|---|
| [31cfee16] | 512 | }
|
|---|
| 513 | }
|
|---|
| 514 |
|
|---|
| 515 | /*! @todo This should probably be atomic. */
|
|---|
| 516 | memcpy(hid_dev->input_report, buffer, buffer_size);
|
|---|
| 517 | hid_dev->input_report_size = buffer_size;
|
|---|
| 518 |
|
|---|
| [60c0573] | 519 | bool cont = false;
|
|---|
| 520 |
|
|---|
| 521 | // continue if at least one of the subdrivers want to continue
|
|---|
| 522 | for (i = 0; i < hid_dev->subdriver_count; ++i) {
|
|---|
| 523 | if (hid_dev->subdrivers[i].poll != NULL
|
|---|
| 524 | && hid_dev->subdrivers[i].poll(hid_dev, buffer,
|
|---|
| 525 | buffer_size)) {
|
|---|
| 526 | cont = true;
|
|---|
| 527 | }
|
|---|
| 528 | }
|
|---|
| 529 |
|
|---|
| 530 | return cont;
|
|---|
| 531 | }
|
|---|
| 532 |
|
|---|
| 533 | /*----------------------------------------------------------------------------*/
|
|---|
| 534 |
|
|---|
| [61257f4] | 535 | void usb_hid_polling_ended_callback(usb_device_t *dev, bool reason,
|
|---|
| 536 | void *arg)
|
|---|
| 537 | {
|
|---|
| [60c0573] | 538 | int i;
|
|---|
| 539 |
|
|---|
| [61257f4] | 540 | if (dev == NULL || arg == NULL) {
|
|---|
| 541 | return;
|
|---|
| 542 | }
|
|---|
| 543 |
|
|---|
| 544 | usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
|
|---|
| 545 |
|
|---|
| [60c0573] | 546 | for (i = 0; i < hid_dev->subdriver_count; ++i) {
|
|---|
| 547 | if (hid_dev->subdrivers[i].poll_end != NULL) {
|
|---|
| 548 | hid_dev->subdrivers[i].poll_end(hid_dev, reason);
|
|---|
| 549 | }
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| [61257f4] | 552 | usb_hid_free(&hid_dev);
|
|---|
| 553 | }
|
|---|
| 554 |
|
|---|
| 555 | /*----------------------------------------------------------------------------*/
|
|---|
| 556 |
|
|---|
| [31cfee16] | 557 | //const char *usb_hid_get_function_name(const usb_hid_dev_t *hid_dev)
|
|---|
| 558 | //{
|
|---|
| 559 | // switch (hid_dev->poll_pipe_index) {
|
|---|
| 560 | // case USB_HID_KBD_POLL_EP_NO:
|
|---|
| 561 | // return HID_KBD_FUN_NAME;
|
|---|
| 562 | // break;
|
|---|
| 563 | // case USB_HID_MOUSE_POLL_EP_NO:
|
|---|
| 564 | // return HID_MOUSE_FUN_NAME;
|
|---|
| 565 | // break;
|
|---|
| 566 | // default:
|
|---|
| 567 | // return HID_GENERIC_FUN_NAME;
|
|---|
| 568 | // }
|
|---|
| 569 | //}
|
|---|
| [61257f4] | 570 |
|
|---|
| 571 | /*----------------------------------------------------------------------------*/
|
|---|
| 572 |
|
|---|
| [31cfee16] | 573 | //const char *usb_hid_get_class_name(const usb_hid_dev_t *hid_dev)
|
|---|
| 574 | //{
|
|---|
| 575 | // // this means that only boot protocol keyboards will be connected
|
|---|
| 576 | // // to the console; there is probably no better way to do this
|
|---|
| 577 |
|
|---|
| 578 | // switch (hid_dev->poll_pipe_index) {
|
|---|
| 579 | // case USB_HID_KBD_POLL_EP_NO:
|
|---|
| 580 | // return HID_KBD_CLASS_NAME;
|
|---|
| 581 | // break;
|
|---|
| 582 | // case USB_HID_MOUSE_POLL_EP_NO:
|
|---|
| 583 | // return HID_MOUSE_CLASS_NAME;
|
|---|
| 584 | // break;
|
|---|
| 585 | // default:
|
|---|
| 586 | // return HID_GENERIC_CLASS_NAME;
|
|---|
| 587 | // }
|
|---|
| 588 | //}
|
|---|
| [61257f4] | 589 |
|
|---|
| 590 | /*----------------------------------------------------------------------------*/
|
|---|
| 591 |
|
|---|
| [dd3eda2] | 592 | void usb_hid_new_report(void)
|
|---|
| 593 | {
|
|---|
| 594 | report_received = false;
|
|---|
| 595 | }
|
|---|
| 596 |
|
|---|
| 597 | /*----------------------------------------------------------------------------*/
|
|---|
| 598 |
|
|---|
| 599 | void usb_hid_report_received(void)
|
|---|
| 600 | {
|
|---|
| 601 | report_received = true;
|
|---|
| 602 | }
|
|---|
| 603 |
|
|---|
| 604 | /*----------------------------------------------------------------------------*/
|
|---|
| 605 |
|
|---|
| 606 | bool usb_hid_report_ready(void)
|
|---|
| 607 | {
|
|---|
| 608 | return !report_received;
|
|---|
| 609 | }
|
|---|
| 610 |
|
|---|
| 611 | /*----------------------------------------------------------------------------*/
|
|---|
| 612 |
|
|---|
| [61257f4] | 613 | void usb_hid_free(usb_hid_dev_t **hid_dev)
|
|---|
| 614 | {
|
|---|
| [60c0573] | 615 | int i;
|
|---|
| 616 |
|
|---|
| [61257f4] | 617 | if (hid_dev == NULL || *hid_dev == NULL) {
|
|---|
| 618 | return;
|
|---|
| 619 | }
|
|---|
| 620 |
|
|---|
| [aaf6155] | 621 | usb_log_debug("Subdrivers: %p, subdriver count: %d\n",
|
|---|
| 622 | (*hid_dev)->subdrivers, (*hid_dev)->subdriver_count);
|
|---|
| 623 |
|
|---|
| [f76153ce] | 624 | assert((*hid_dev)->subdrivers != NULL
|
|---|
| 625 | || (*hid_dev)->subdriver_count == 0);
|
|---|
| 626 |
|
|---|
| [60c0573] | 627 | for (i = 0; i < (*hid_dev)->subdriver_count; ++i) {
|
|---|
| 628 | if ((*hid_dev)->subdrivers[i].deinit != NULL) {
|
|---|
| 629 | (*hid_dev)->subdrivers[i].deinit(*hid_dev);
|
|---|
| 630 | }
|
|---|
| [61257f4] | 631 | }
|
|---|
| [f76153ce] | 632 |
|
|---|
| 633 | // free the subdrivers info
|
|---|
| 634 | if ((*hid_dev)->subdrivers != NULL) {
|
|---|
| [1cbb4b7] | 635 | free((*hid_dev)->subdrivers);
|
|---|
| [f76153ce] | 636 | }
|
|---|
| [61257f4] | 637 |
|
|---|
| 638 | // destroy the parser
|
|---|
| [e60436b] | 639 | if ((*hid_dev)->report != NULL) {
|
|---|
| 640 | usb_hid_free_report((*hid_dev)->report);
|
|---|
| [61257f4] | 641 | }
|
|---|
| 642 |
|
|---|
| 643 | if ((*hid_dev)->report_desc != NULL) {
|
|---|
| 644 | free((*hid_dev)->report_desc);
|
|---|
| 645 | }
|
|---|
| 646 |
|
|---|
| 647 | free(*hid_dev);
|
|---|
| 648 | *hid_dev = NULL;
|
|---|
| [966acede] | 649 | }
|
|---|
| 650 |
|
|---|
| 651 | /**
|
|---|
| 652 | * @}
|
|---|
| 653 | */
|
|---|