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