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