| 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 <usb/hid/hid.h>
|
|---|
| 40 | #include <usb/hid/hidparser.h>
|
|---|
| 41 | #include <usb/hid/hidreport.h>
|
|---|
| 42 | #include <usb/hid/request.h>
|
|---|
| 43 | #include <errno.h>
|
|---|
| 44 | #include <str_error.h>
|
|---|
| 45 |
|
|---|
| 46 | #include "usbhid.h"
|
|---|
| 47 |
|
|---|
| 48 | #include "kbd/kbddev.h"
|
|---|
| 49 | #include "generic/hiddev.h"
|
|---|
| 50 | #include "mouse/mousedev.h"
|
|---|
| 51 | #include "subdrivers.h"
|
|---|
| 52 |
|
|---|
| 53 | /*----------------------------------------------------------------------------*/
|
|---|
| 54 |
|
|---|
| 55 | /* Array of endpoints expected on the device, NULL terminated. */
|
|---|
| 56 | const usb_endpoint_description_t *usb_hid_endpoints[] = {
|
|---|
| 57 | &usb_hid_kbd_poll_endpoint_description,
|
|---|
| 58 | &usb_hid_mouse_poll_endpoint_description,
|
|---|
| 59 | &usb_hid_generic_poll_endpoint_description,
|
|---|
| 60 | NULL
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | /*----------------------------------------------------------------------------*/
|
|---|
| 64 |
|
|---|
| 65 | static int usb_hid_set_boot_kbd_subdriver(usb_hid_dev_t *hid_dev)
|
|---|
| 66 | {
|
|---|
| 67 | assert(hid_dev != NULL);
|
|---|
| 68 | assert(hid_dev->subdriver_count == 0);
|
|---|
| 69 |
|
|---|
| 70 | hid_dev->subdrivers = malloc(sizeof(usb_hid_subdriver_t));
|
|---|
| 71 | if (hid_dev->subdrivers == NULL) {
|
|---|
| 72 | return ENOMEM;
|
|---|
| 73 | }
|
|---|
| 74 | hid_dev->subdriver_count = 1;
|
|---|
| 75 | // TODO 0 should be keyboard, but find a better way
|
|---|
| 76 | hid_dev->subdrivers[0] = usb_hid_subdrivers[0].subdriver;
|
|---|
| 77 |
|
|---|
| 78 | return EOK;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /*----------------------------------------------------------------------------*/
|
|---|
| 82 |
|
|---|
| 83 | static int usb_hid_set_boot_mouse_subdriver(usb_hid_dev_t *hid_dev)
|
|---|
| 84 | {
|
|---|
| 85 | assert(hid_dev != NULL);
|
|---|
| 86 | assert(hid_dev->subdriver_count == 0);
|
|---|
| 87 |
|
|---|
| 88 | hid_dev->subdrivers = malloc(sizeof(usb_hid_subdriver_t));
|
|---|
| 89 | if (hid_dev->subdrivers == NULL) {
|
|---|
| 90 | return ENOMEM;
|
|---|
| 91 | }
|
|---|
| 92 | hid_dev->subdriver_count = 1;
|
|---|
| 93 | // TODO 2 should be mouse, but find a better way
|
|---|
| 94 | hid_dev->subdrivers[0] = usb_hid_subdrivers[2].subdriver;
|
|---|
| 95 |
|
|---|
| 96 | return EOK;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /*----------------------------------------------------------------------------*/
|
|---|
| 100 |
|
|---|
| 101 | static int usb_hid_set_generic_hid_subdriver(usb_hid_dev_t *hid_dev)
|
|---|
| 102 | {
|
|---|
| 103 | assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
|
|---|
| 104 |
|
|---|
| 105 | hid_dev->subdrivers = malloc(sizeof(usb_hid_subdriver_t));
|
|---|
| 106 | if (hid_dev->subdrivers == NULL) {
|
|---|
| 107 | return ENOMEM;
|
|---|
| 108 | }
|
|---|
| 109 | hid_dev->subdriver_count = 1;
|
|---|
| 110 |
|
|---|
| 111 | /* Set generic hid subdriver routines */
|
|---|
| 112 | hid_dev->subdrivers[0].init = usb_generic_hid_init;
|
|---|
| 113 | hid_dev->subdrivers[0].poll = usb_generic_hid_polling_callback;
|
|---|
| 114 | hid_dev->subdrivers[0].poll_end = NULL;
|
|---|
| 115 | hid_dev->subdrivers[0].deinit = usb_generic_hid_deinit;
|
|---|
| 116 |
|
|---|
| 117 | return EOK;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /*----------------------------------------------------------------------------*/
|
|---|
| 121 |
|
|---|
| 122 | static bool usb_hid_ids_match(const usb_hid_dev_t *hid_dev,
|
|---|
| 123 | const usb_hid_subdriver_mapping_t *mapping)
|
|---|
| 124 | {
|
|---|
| 125 | assert(hid_dev != NULL);
|
|---|
| 126 | assert(hid_dev->usb_dev != NULL);
|
|---|
| 127 |
|
|---|
| 128 | return (hid_dev->usb_dev->descriptors.device.vendor_id
|
|---|
| 129 | == mapping->vendor_id
|
|---|
| 130 | && hid_dev->usb_dev->descriptors.device.product_id
|
|---|
| 131 | == mapping->product_id);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /*----------------------------------------------------------------------------*/
|
|---|
| 135 |
|
|---|
| 136 | static bool usb_hid_path_matches(usb_hid_dev_t *hid_dev,
|
|---|
| 137 | const usb_hid_subdriver_mapping_t *mapping)
|
|---|
| 138 | {
|
|---|
| 139 | assert(hid_dev != NULL);
|
|---|
| 140 | assert(mapping != NULL);
|
|---|
| 141 |
|
|---|
| 142 | usb_hid_report_path_t *usage_path = usb_hid_report_path();
|
|---|
| 143 | if (usage_path == NULL) {
|
|---|
| 144 | usb_log_debug("Failed to create usage path.\n");
|
|---|
| 145 | return false;
|
|---|
| 146 | }
|
|---|
| 147 | int i = 0;
|
|---|
| 148 | while (mapping->usage_path[i].usage != 0
|
|---|
| 149 | || mapping->usage_path[i].usage_page != 0) {
|
|---|
| 150 | if (usb_hid_report_path_append_item(usage_path,
|
|---|
| 151 | mapping->usage_path[i].usage_page,
|
|---|
| 152 | mapping->usage_path[i].usage) != EOK) {
|
|---|
| 153 | usb_log_debug("Failed to append to usage path.\n");
|
|---|
| 154 | usb_hid_report_path_free(usage_path);
|
|---|
| 155 | return false;
|
|---|
| 156 | }
|
|---|
| 157 | ++i;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | usb_log_debug("Compare flags: %d\n", mapping->compare);
|
|---|
| 161 |
|
|---|
| 162 | bool matches = false;
|
|---|
| 163 | uint8_t report_id = mapping->report_id;
|
|---|
| 164 |
|
|---|
| 165 | do {
|
|---|
| 166 | usb_log_debug("Trying report id %u\n", report_id);
|
|---|
| 167 |
|
|---|
| 168 | if (report_id != 0) {
|
|---|
| 169 | usb_hid_report_path_set_report_id(usage_path,
|
|---|
| 170 | report_id);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | usb_hid_report_field_t *field = usb_hid_report_get_sibling(
|
|---|
| 174 | &hid_dev->report, NULL, usage_path, mapping->compare,
|
|---|
| 175 | USB_HID_REPORT_TYPE_INPUT);
|
|---|
| 176 |
|
|---|
| 177 | usb_log_debug("Field: %p\n", field);
|
|---|
| 178 |
|
|---|
| 179 | if (field != NULL) {
|
|---|
| 180 | matches = true;
|
|---|
| 181 | break;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | report_id = usb_hid_get_next_report_id(
|
|---|
| 185 | &hid_dev->report, report_id, USB_HID_REPORT_TYPE_INPUT);
|
|---|
| 186 | } while (!matches && report_id != 0);
|
|---|
| 187 |
|
|---|
| 188 | usb_hid_report_path_free(usage_path);
|
|---|
| 189 |
|
|---|
| 190 | return matches;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | /*----------------------------------------------------------------------------*/
|
|---|
| 194 |
|
|---|
| 195 | static int usb_hid_save_subdrivers(usb_hid_dev_t *hid_dev,
|
|---|
| 196 | const usb_hid_subdriver_t **subdrivers, int count)
|
|---|
| 197 | {
|
|---|
| 198 | int i;
|
|---|
| 199 |
|
|---|
| 200 | if (count <= 0) {
|
|---|
| 201 | hid_dev->subdriver_count = 0;
|
|---|
| 202 | hid_dev->subdrivers = NULL;
|
|---|
| 203 | return EOK;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /* +1 for generic hid subdriver */
|
|---|
| 207 | hid_dev->subdrivers = calloc((count + 1), sizeof(usb_hid_subdriver_t));
|
|---|
| 208 | if (hid_dev->subdrivers == NULL) {
|
|---|
| 209 | return ENOMEM;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | for (i = 0; i < count; ++i) {
|
|---|
| 213 | hid_dev->subdrivers[i].init = subdrivers[i]->init;
|
|---|
| 214 | hid_dev->subdrivers[i].deinit = subdrivers[i]->deinit;
|
|---|
| 215 | hid_dev->subdrivers[i].poll = subdrivers[i]->poll;
|
|---|
| 216 | hid_dev->subdrivers[i].poll_end = subdrivers[i]->poll_end;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | /* Add one generic HID subdriver per device */
|
|---|
| 220 | hid_dev->subdrivers[count].init = usb_generic_hid_init;
|
|---|
| 221 | hid_dev->subdrivers[count].poll = usb_generic_hid_polling_callback;
|
|---|
| 222 | hid_dev->subdrivers[count].deinit = usb_generic_hid_deinit;
|
|---|
| 223 | hid_dev->subdrivers[count].poll_end = NULL;
|
|---|
| 224 |
|
|---|
| 225 | hid_dev->subdriver_count = count + 1;
|
|---|
| 226 |
|
|---|
| 227 | return EOK;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | /*----------------------------------------------------------------------------*/
|
|---|
| 231 |
|
|---|
| 232 | static int usb_hid_find_subdrivers(usb_hid_dev_t *hid_dev)
|
|---|
| 233 | {
|
|---|
| 234 | assert(hid_dev != NULL);
|
|---|
| 235 |
|
|---|
| 236 | const usb_hid_subdriver_t *subdrivers[USB_HID_MAX_SUBDRIVERS];
|
|---|
| 237 |
|
|---|
| 238 | int i = 0, count = 0;
|
|---|
| 239 | const usb_hid_subdriver_mapping_t *mapping = &usb_hid_subdrivers[i];
|
|---|
| 240 |
|
|---|
| 241 | bool ids_matched;
|
|---|
| 242 | bool matched;
|
|---|
| 243 |
|
|---|
| 244 | while (count < USB_HID_MAX_SUBDRIVERS &&
|
|---|
| 245 | (mapping->usage_path != NULL
|
|---|
| 246 | || mapping->vendor_id >= 0 || mapping->product_id >= 0)) {
|
|---|
| 247 | // check the vendor & product ID
|
|---|
| 248 | if (mapping->vendor_id >= 0 && mapping->product_id < 0) {
|
|---|
| 249 | usb_log_warning("Missing Product ID for Vendor ID %d\n",
|
|---|
| 250 | mapping->vendor_id);
|
|---|
| 251 | return EINVAL;
|
|---|
| 252 | }
|
|---|
| 253 | if (mapping->product_id >= 0 && mapping->vendor_id < 0) {
|
|---|
| 254 | usb_log_warning("Missing Vendor ID for Product ID %d\n",
|
|---|
| 255 | mapping->product_id);
|
|---|
| 256 | return EINVAL;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | ids_matched = false;
|
|---|
| 260 | matched = false;
|
|---|
| 261 |
|
|---|
| 262 | if (mapping->vendor_id >= 0) {
|
|---|
| 263 | assert(mapping->product_id >= 0);
|
|---|
| 264 | usb_log_debug("Comparing device against vendor ID %u"
|
|---|
| 265 | " and product ID %u.\n", mapping->vendor_id,
|
|---|
| 266 | mapping->product_id);
|
|---|
| 267 | if (usb_hid_ids_match(hid_dev, mapping)) {
|
|---|
| 268 | usb_log_debug("IDs matched.\n");
|
|---|
| 269 | ids_matched = true;
|
|---|
| 270 | }
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | if (mapping->usage_path != NULL) {
|
|---|
| 274 | usb_log_debug("Comparing device against usage path.\n");
|
|---|
| 275 | if (usb_hid_path_matches(hid_dev, mapping)) {
|
|---|
| 276 | // does not matter if IDs were matched
|
|---|
| 277 | matched = true;
|
|---|
| 278 | }
|
|---|
| 279 | } else {
|
|---|
| 280 | // matched only if IDs were matched and there is no path
|
|---|
| 281 | matched = ids_matched;
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | if (matched) {
|
|---|
| 285 | usb_log_debug("Subdriver matched.\n");
|
|---|
| 286 | subdrivers[count++] = &mapping->subdriver;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | mapping = &usb_hid_subdrivers[++i];
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | /* We have all subdrivers determined, save them into the hid device */
|
|---|
| 293 | // TODO Do we really need this complicated stuff if there is
|
|---|
| 294 | // max_subdrivers limitation?
|
|---|
| 295 | return usb_hid_save_subdrivers(hid_dev, subdrivers, count);
|
|---|
| 296 | }
|
|---|
| 297 | /*----------------------------------------------------------------------------*/
|
|---|
| 298 | static int usb_hid_check_pipes(usb_hid_dev_t *hid_dev, const usb_device_t *dev)
|
|---|
| 299 | {
|
|---|
| 300 | assert(hid_dev);
|
|---|
| 301 | assert(dev);
|
|---|
| 302 |
|
|---|
| 303 | static const struct {
|
|---|
| 304 | unsigned ep_number;
|
|---|
| 305 | const char* description;
|
|---|
| 306 | } endpoints[] = {
|
|---|
| 307 | {USB_HID_KBD_POLL_EP_NO, "Keyboard endpoint"},
|
|---|
| 308 | {USB_HID_MOUSE_POLL_EP_NO, "Mouse endpoint"},
|
|---|
| 309 | {USB_HID_GENERIC_POLL_EP_NO, "Generic HID endpoint"},
|
|---|
| 310 | };
|
|---|
| 311 |
|
|---|
| 312 | for (unsigned i = 0; i < sizeof(endpoints)/sizeof(endpoints[0]); ++i) {
|
|---|
| 313 | if (endpoints[i].ep_number >= dev->pipes_count) {
|
|---|
| 314 | return EINVAL;
|
|---|
| 315 | }
|
|---|
| 316 | if (dev->pipes[endpoints[i].ep_number].present) {
|
|---|
| 317 | usb_log_debug("Found: %s.\n", endpoints[i].description);
|
|---|
| 318 | hid_dev->poll_pipe_index = endpoints[i].ep_number;
|
|---|
| 319 | return EOK;
|
|---|
| 320 | }
|
|---|
| 321 | }
|
|---|
| 322 | return ENOTSUP;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | /*----------------------------------------------------------------------------*/
|
|---|
| 326 |
|
|---|
| 327 | static int usb_hid_init_report(usb_hid_dev_t *hid_dev)
|
|---|
| 328 | {
|
|---|
| 329 | assert(hid_dev != NULL);
|
|---|
| 330 |
|
|---|
| 331 | uint8_t report_id = 0;
|
|---|
| 332 | size_t max_size = 0;
|
|---|
| 333 |
|
|---|
| 334 | do {
|
|---|
| 335 | usb_log_debug("Getting size of the report.\n");
|
|---|
| 336 | const size_t size =
|
|---|
| 337 | usb_hid_report_byte_size(&hid_dev->report, report_id,
|
|---|
| 338 | USB_HID_REPORT_TYPE_INPUT);
|
|---|
| 339 | usb_log_debug("Report ID: %u, size: %zu\n", report_id, size);
|
|---|
| 340 | max_size = (size > max_size) ? size : max_size;
|
|---|
| 341 | usb_log_debug("Getting next report ID\n");
|
|---|
| 342 | report_id = usb_hid_get_next_report_id(&hid_dev->report,
|
|---|
| 343 | report_id, USB_HID_REPORT_TYPE_INPUT);
|
|---|
| 344 | } while (report_id != 0);
|
|---|
| 345 |
|
|---|
| 346 | usb_log_debug("Max size of input report: %zu\n", max_size);
|
|---|
| 347 |
|
|---|
| 348 | assert(hid_dev->input_report == NULL);
|
|---|
| 349 |
|
|---|
| 350 | hid_dev->input_report = calloc(1, max_size);
|
|---|
| 351 | if (hid_dev->input_report == NULL) {
|
|---|
| 352 | return ENOMEM;
|
|---|
| 353 | }
|
|---|
| 354 | hid_dev->max_input_report_size = max_size;
|
|---|
| 355 |
|
|---|
| 356 | return EOK;
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | /*----------------------------------------------------------------------------*/
|
|---|
| 360 | /*
|
|---|
| 361 | * This functions initializes required structures from the device's descriptors
|
|---|
| 362 | * and starts new fibril for polling the keyboard for events and another one for
|
|---|
| 363 | * handling auto-repeat of keys.
|
|---|
| 364 | *
|
|---|
| 365 | * During initialization, the keyboard is switched into boot protocol, the idle
|
|---|
| 366 | * rate is set to 0 (infinity), resulting in the keyboard only reporting event
|
|---|
| 367 | * when a key is pressed or released. Finally, the LED lights are turned on
|
|---|
| 368 | * according to the default setup of lock keys.
|
|---|
| 369 | *
|
|---|
| 370 | * @note By default, the keyboards is initialized with Num Lock turned on and
|
|---|
| 371 | * other locks turned off.
|
|---|
| 372 | *
|
|---|
| 373 | * @param hid_dev Device to initialize, non-NULL.
|
|---|
| 374 | * @param dev USB device, non-NULL.
|
|---|
| 375 | * @return Error code.
|
|---|
| 376 | */
|
|---|
| 377 | int usb_hid_init(usb_hid_dev_t *hid_dev, usb_device_t *dev)
|
|---|
| 378 | {
|
|---|
| 379 | assert(hid_dev);
|
|---|
| 380 | assert(dev);
|
|---|
| 381 |
|
|---|
| 382 | usb_log_debug("Initializing HID structure...\n");
|
|---|
| 383 |
|
|---|
| 384 | usb_hid_report_init(&hid_dev->report);
|
|---|
| 385 |
|
|---|
| 386 | /* The USB device should already be initialized, save it in structure */
|
|---|
| 387 | hid_dev->usb_dev = dev;
|
|---|
| 388 | hid_dev->poll_pipe_index = -1;
|
|---|
| 389 |
|
|---|
| 390 | int rc = usb_hid_check_pipes(hid_dev, dev);
|
|---|
| 391 | if (rc != EOK) {
|
|---|
| 392 | return rc;
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | /* Get the report descriptor and parse it. */
|
|---|
| 396 | rc = usb_hid_process_report_descriptor(hid_dev->usb_dev,
|
|---|
| 397 | &hid_dev->report, &hid_dev->report_desc, &hid_dev->report_desc_size);
|
|---|
| 398 |
|
|---|
| 399 | /*
|
|---|
| 400 | * 1) subdriver vytvori vlastnu ddf_fun, vlastne ddf_dev_ops, ktore da
|
|---|
| 401 | * do nej.
|
|---|
| 402 | * 2) do tych ops do .interfaces[DEV_IFACE_USBHID (asi)] priradi
|
|---|
| 403 | * vyplnenu strukturu usbhid_iface_t.
|
|---|
| 404 | * 3) klientska aplikacia - musi si rucne vytvorit telefon
|
|---|
| 405 | * (devman_device_connect() - cesta k zariadeniu (/hw/pci0/...) az
|
|---|
| 406 | * k tej fcii.
|
|---|
| 407 | * pouzit usb/classes/hid/iface.h - prvy int je telefon
|
|---|
| 408 | */
|
|---|
| 409 |
|
|---|
| 410 | bool fallback = false;
|
|---|
| 411 |
|
|---|
| 412 | if (rc == EOK) {
|
|---|
| 413 | // try to find subdrivers that may want to handle this device
|
|---|
| 414 | rc = usb_hid_find_subdrivers(hid_dev);
|
|---|
| 415 | if (rc != EOK || hid_dev->subdriver_count == 0) {
|
|---|
| 416 | // try to fall back to the boot protocol if available
|
|---|
| 417 | usb_log_info("No subdrivers found to handle this"
|
|---|
| 418 | " device.\n");
|
|---|
| 419 | fallback = true;
|
|---|
| 420 | assert(hid_dev->subdrivers == NULL);
|
|---|
| 421 | assert(hid_dev->subdriver_count == 0);
|
|---|
| 422 | }
|
|---|
| 423 | } else {
|
|---|
| 424 | usb_log_error("Failed to parse Report descriptor.\n");
|
|---|
| 425 | // try to fall back to the boot protocol if available
|
|---|
| 426 | fallback = true;
|
|---|
| 427 | }
|
|---|
| 428 |
|
|---|
| 429 | if (fallback) {
|
|---|
| 430 | // fall back to boot protocol
|
|---|
| 431 | switch (hid_dev->poll_pipe_index) {
|
|---|
| 432 | case USB_HID_KBD_POLL_EP_NO:
|
|---|
| 433 | usb_log_info("Falling back to kbd boot protocol.\n");
|
|---|
| 434 | rc = usb_kbd_set_boot_protocol(hid_dev);
|
|---|
| 435 | if (rc == EOK) {
|
|---|
| 436 | rc = usb_hid_set_boot_kbd_subdriver(hid_dev);
|
|---|
| 437 | }
|
|---|
| 438 | break;
|
|---|
| 439 | case USB_HID_MOUSE_POLL_EP_NO:
|
|---|
| 440 | usb_log_info("Falling back to mouse boot protocol.\n");
|
|---|
| 441 | rc = usb_mouse_set_boot_protocol(hid_dev);
|
|---|
| 442 | if (rc == EOK) {
|
|---|
| 443 | rc = usb_hid_set_boot_mouse_subdriver(hid_dev);
|
|---|
| 444 | }
|
|---|
| 445 | break;
|
|---|
| 446 | default:
|
|---|
| 447 | assert(hid_dev->poll_pipe_index
|
|---|
| 448 | == USB_HID_GENERIC_POLL_EP_NO);
|
|---|
| 449 |
|
|---|
| 450 | usb_log_info("Falling back to generic HID driver.\n");
|
|---|
| 451 | rc = usb_hid_set_generic_hid_subdriver(hid_dev);
|
|---|
| 452 | }
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | if (rc != EOK) {
|
|---|
| 456 | usb_log_error("No subdriver for handling this device could be"
|
|---|
| 457 | " initialized: %s.\n", str_error(rc));
|
|---|
| 458 | usb_log_debug("Subdriver count: %d\n",
|
|---|
| 459 | hid_dev->subdriver_count);
|
|---|
| 460 | } else {
|
|---|
| 461 | bool ok = false;
|
|---|
| 462 |
|
|---|
| 463 | usb_log_debug("Subdriver count: %d\n",
|
|---|
| 464 | hid_dev->subdriver_count);
|
|---|
| 465 |
|
|---|
| 466 | for (int i = 0; i < hid_dev->subdriver_count; ++i) {
|
|---|
| 467 | if (hid_dev->subdrivers[i].init != NULL) {
|
|---|
| 468 | usb_log_debug("Initializing subdriver %d.\n",i);
|
|---|
| 469 | rc = hid_dev->subdrivers[i].init(hid_dev,
|
|---|
| 470 | &hid_dev->subdrivers[i].data);
|
|---|
| 471 | if (rc != EOK) {
|
|---|
| 472 | usb_log_warning("Failed to initialize"
|
|---|
| 473 | " HID subdriver structure.\n");
|
|---|
| 474 | } else {
|
|---|
| 475 | // at least one subdriver initialized
|
|---|
| 476 | ok = true;
|
|---|
| 477 | }
|
|---|
| 478 | } else {
|
|---|
| 479 | ok = true;
|
|---|
| 480 | }
|
|---|
| 481 | }
|
|---|
| 482 |
|
|---|
| 483 | rc = (ok) ? EOK : -1; // what error to report
|
|---|
| 484 | }
|
|---|
| 485 |
|
|---|
| 486 |
|
|---|
| 487 | if (rc == EOK) {
|
|---|
| 488 | // save max input report size and allocate space for the report
|
|---|
| 489 | rc = usb_hid_init_report(hid_dev);
|
|---|
| 490 | if (rc != EOK) {
|
|---|
| 491 | usb_log_error("Failed to initialize input report buffer"
|
|---|
| 492 | ".\n");
|
|---|
| 493 | }
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | return rc;
|
|---|
| 497 | }
|
|---|
| 498 |
|
|---|
| 499 | /*----------------------------------------------------------------------------*/
|
|---|
| 500 |
|
|---|
| 501 | bool usb_hid_polling_callback(usb_device_t *dev, uint8_t *buffer,
|
|---|
| 502 | size_t buffer_size, void *arg)
|
|---|
| 503 | {
|
|---|
| 504 | if (dev == NULL || arg == NULL || buffer == NULL) {
|
|---|
| 505 | usb_log_error("Missing arguments to polling callback.\n");
|
|---|
| 506 | return false;
|
|---|
| 507 | }
|
|---|
| 508 | usb_hid_dev_t *hid_dev = arg;
|
|---|
| 509 |
|
|---|
| 510 | assert(hid_dev->input_report != NULL);
|
|---|
| 511 |
|
|---|
| 512 | usb_log_debug("New data [%zu/%zu]: %s\n", buffer_size,
|
|---|
| 513 | hid_dev->max_input_report_size,
|
|---|
| 514 | usb_debug_str_buffer(buffer, buffer_size, 0));
|
|---|
| 515 |
|
|---|
| 516 | if (hid_dev->max_input_report_size >= buffer_size) {
|
|---|
| 517 | /*! @todo This should probably be atomic. */
|
|---|
| 518 | memcpy(hid_dev->input_report, buffer, buffer_size);
|
|---|
| 519 | hid_dev->input_report_size = buffer_size;
|
|---|
| 520 | usb_hid_new_report(hid_dev);
|
|---|
| 521 | }
|
|---|
| 522 |
|
|---|
| 523 | /* Parse the input report */
|
|---|
| 524 | const int rc = usb_hid_parse_report(
|
|---|
| 525 | &hid_dev->report, buffer, buffer_size, &hid_dev->report_id);
|
|---|
| 526 | if (rc != EOK) {
|
|---|
| 527 | usb_log_warning("Error in usb_hid_parse_report():"
|
|---|
| 528 | "%s\n", str_error(rc));
|
|---|
| 529 | }
|
|---|
| 530 |
|
|---|
| 531 | bool cont = false;
|
|---|
| 532 | /* Continue if at least one of the subdrivers want to continue */
|
|---|
| 533 | for (int i = 0; i < hid_dev->subdriver_count; ++i) {
|
|---|
| 534 | if (hid_dev->subdrivers[i].poll != NULL) {
|
|---|
| 535 | cont = cont || hid_dev->subdrivers[i].poll(
|
|---|
| 536 | hid_dev, hid_dev->subdrivers[i].data);
|
|---|
| 537 | }
|
|---|
| 538 | }
|
|---|
| 539 |
|
|---|
| 540 | return cont;
|
|---|
| 541 | }
|
|---|
| 542 |
|
|---|
| 543 | /*----------------------------------------------------------------------------*/
|
|---|
| 544 |
|
|---|
| 545 | void usb_hid_polling_ended_callback(usb_device_t *dev, bool reason, void *arg)
|
|---|
| 546 | {
|
|---|
| 547 | assert(dev);
|
|---|
| 548 | assert(arg);
|
|---|
| 549 |
|
|---|
| 550 | usb_hid_dev_t *hid_dev = arg;
|
|---|
| 551 |
|
|---|
| 552 | for (int i = 0; i < hid_dev->subdriver_count; ++i) {
|
|---|
| 553 | if (hid_dev->subdrivers[i].poll_end != NULL) {
|
|---|
| 554 | hid_dev->subdrivers[i].poll_end(
|
|---|
| 555 | hid_dev, hid_dev->subdrivers[i].data, reason);
|
|---|
| 556 | }
|
|---|
| 557 | }
|
|---|
| 558 |
|
|---|
| 559 | hid_dev->running = false;
|
|---|
| 560 | }
|
|---|
| 561 |
|
|---|
| 562 | /*----------------------------------------------------------------------------*/
|
|---|
| 563 |
|
|---|
| 564 | void usb_hid_new_report(usb_hid_dev_t *hid_dev)
|
|---|
| 565 | {
|
|---|
| 566 | ++hid_dev->report_nr;
|
|---|
| 567 | }
|
|---|
| 568 |
|
|---|
| 569 | /*----------------------------------------------------------------------------*/
|
|---|
| 570 |
|
|---|
| 571 | int usb_hid_report_number(const usb_hid_dev_t *hid_dev)
|
|---|
| 572 | {
|
|---|
| 573 | return hid_dev->report_nr;
|
|---|
| 574 | }
|
|---|
| 575 |
|
|---|
| 576 | /*----------------------------------------------------------------------------*/
|
|---|
| 577 | void usb_hid_deinit(usb_hid_dev_t *hid_dev)
|
|---|
| 578 | {
|
|---|
| 579 | assert(hid_dev);
|
|---|
| 580 | assert(hid_dev->subdrivers != NULL || hid_dev->subdriver_count == 0);
|
|---|
| 581 |
|
|---|
| 582 |
|
|---|
| 583 | usb_log_debug("Subdrivers: %p, subdriver count: %d\n",
|
|---|
| 584 | hid_dev->subdrivers, hid_dev->subdriver_count);
|
|---|
| 585 |
|
|---|
| 586 | for (int i = 0; i < hid_dev->subdriver_count; ++i) {
|
|---|
| 587 | if (hid_dev->subdrivers[i].deinit != NULL) {
|
|---|
| 588 | hid_dev->subdrivers[i].deinit(hid_dev,
|
|---|
| 589 | hid_dev->subdrivers[i].data);
|
|---|
| 590 | }
|
|---|
| 591 | }
|
|---|
| 592 |
|
|---|
| 593 | /* Free allocated structures */
|
|---|
| 594 | free(hid_dev->subdrivers);
|
|---|
| 595 | free(hid_dev->report_desc);
|
|---|
| 596 |
|
|---|
| 597 | /* Destroy the parser */
|
|---|
| 598 | usb_hid_report_deinit(&hid_dev->report);
|
|---|
| 599 |
|
|---|
| 600 | }
|
|---|
| 601 |
|
|---|
| 602 | /**
|
|---|
| 603 | * @}
|
|---|
| 604 | */
|
|---|