[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>
|
---|
[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 | /* Array of endpoints expected on the device, NULL terminated. */
|
---|
[b803845] | 54 | const usb_endpoint_description_t *usb_hid_endpoints[] = {
|
---|
[fec47d4] | 55 | &usb_hid_kbd_poll_endpoint_description,
|
---|
| 56 | &usb_hid_mouse_poll_endpoint_description,
|
---|
[61257f4] | 57 | &usb_hid_generic_poll_endpoint_description,
|
---|
[966acede] | 58 | NULL
|
---|
| 59 | };
|
---|
[76fbd9a] | 60 |
|
---|
[60c0573] | 61 | static int usb_hid_set_boot_kbd_subdriver(usb_hid_dev_t *hid_dev)
|
---|
[61257f4] | 62 | {
|
---|
[3f8f09f] | 63 | assert(hid_dev != NULL);
|
---|
| 64 | assert(hid_dev->subdriver_count == 0);
|
---|
[cc29622] | 65 |
|
---|
[3f8f09f] | 66 | hid_dev->subdrivers = malloc(sizeof(usb_hid_subdriver_t));
|
---|
[60c0573] | 67 | if (hid_dev->subdrivers == NULL) {
|
---|
| 68 | return ENOMEM;
|
---|
[61257f4] | 69 | }
|
---|
[3f8f09f] | 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;
|
---|
[cc29622] | 73 |
|
---|
[60c0573] | 74 | return EOK;
|
---|
| 75 | }
|
---|
[76fbd9a] | 76 |
|
---|
[60c0573] | 77 | static int usb_hid_set_boot_mouse_subdriver(usb_hid_dev_t *hid_dev)
|
---|
| 78 | {
|
---|
[3f8f09f] | 79 | assert(hid_dev != NULL);
|
---|
| 80 | assert(hid_dev->subdriver_count == 0);
|
---|
[cc29622] | 81 |
|
---|
[3f8f09f] | 82 | hid_dev->subdrivers = malloc(sizeof(usb_hid_subdriver_t));
|
---|
[60c0573] | 83 | if (hid_dev->subdrivers == NULL) {
|
---|
| 84 | return ENOMEM;
|
---|
[61257f4] | 85 | }
|
---|
[3f8f09f] | 86 | hid_dev->subdriver_count = 1;
|
---|
| 87 | // TODO 2 should be mouse, but find a better way
|
---|
[6f730278] | 88 | hid_dev->subdrivers[0] = usb_hid_subdrivers[2].subdriver;
|
---|
[cc29622] | 89 |
|
---|
[60c0573] | 90 | return EOK;
|
---|
| 91 | }
|
---|
[76fbd9a] | 92 |
|
---|
[60c0573] | 93 | static int usb_hid_set_generic_hid_subdriver(usb_hid_dev_t *hid_dev)
|
---|
| 94 | {
|
---|
[07b9cbae] | 95 | assert(hid_dev != NULL);
|
---|
| 96 | assert(hid_dev->subdriver_count == 0);
|
---|
[cc29622] | 97 |
|
---|
[3f8f09f] | 98 | hid_dev->subdrivers = malloc(sizeof(usb_hid_subdriver_t));
|
---|
[60c0573] | 99 | if (hid_dev->subdrivers == NULL) {
|
---|
| 100 | return ENOMEM;
|
---|
| 101 | }
|
---|
[3f8f09f] | 102 | hid_dev->subdriver_count = 1;
|
---|
[cc29622] | 103 |
|
---|
[3f8f09f] | 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;
|
---|
[cc29622] | 109 |
|
---|
[60c0573] | 110 | return EOK;
|
---|
| 111 | }
|
---|
[76fbd9a] | 112 |
|
---|
[3f8f09f] | 113 | static bool usb_hid_ids_match(const usb_hid_dev_t *hid_dev,
|
---|
[f76153ce] | 114 | const usb_hid_subdriver_mapping_t *mapping)
|
---|
| 115 | {
|
---|
[36f737a] | 116 | assert(hid_dev != NULL);
|
---|
| 117 | assert(hid_dev->usb_dev != NULL);
|
---|
[cc29622] | 118 |
|
---|
[3f8f09f] | 119 | return (hid_dev->usb_dev->descriptors.device.vendor_id
|
---|
[36f737a] | 120 | == mapping->vendor_id
|
---|
[90df90c] | 121 | && hid_dev->usb_dev->descriptors.device.product_id
|
---|
[36f737a] | 122 | == mapping->product_id);
|
---|
[f76153ce] | 123 | }
|
---|
[76fbd9a] | 124 |
|
---|
[3f8f09f] | 125 | static bool usb_hid_path_matches(usb_hid_dev_t *hid_dev,
|
---|
[e3b5129] | 126 | const usb_hid_subdriver_mapping_t *mapping)
|
---|
[f76153ce] | 127 | {
|
---|
| 128 | assert(hid_dev != NULL);
|
---|
[e3b5129] | 129 | assert(mapping != NULL);
|
---|
[cc29622] | 130 |
|
---|
[f76153ce] | 131 | usb_hid_report_path_t *usage_path = usb_hid_report_path();
|
---|
| 132 | if (usage_path == NULL) {
|
---|
[1cbb4b7] | 133 | usb_log_debug("Failed to create usage path.\n");
|
---|
[f76153ce] | 134 | return false;
|
---|
| 135 | }
|
---|
[07b9cbae] | 136 |
|
---|
| 137 | for (int i = 0; mapping->usage_path[i].usage != 0
|
---|
| 138 | || mapping->usage_path[i].usage_page != 0; ++i) {
|
---|
[3f8f09f] | 139 | if (usb_hid_report_path_append_item(usage_path,
|
---|
| 140 | mapping->usage_path[i].usage_page,
|
---|
[e3b5129] | 141 | mapping->usage_path[i].usage) != EOK) {
|
---|
[1cbb4b7] | 142 | usb_log_debug("Failed to append to usage path.\n");
|
---|
[f76153ce] | 143 | usb_hid_report_path_free(usage_path);
|
---|
| 144 | return false;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
[cc29622] | 147 |
|
---|
[e3b5129] | 148 | usb_log_debug("Compare flags: %d\n", mapping->compare);
|
---|
[cc29622] | 149 |
|
---|
[f3f9733] | 150 | bool matches = false;
|
---|
| 151 | uint8_t report_id = mapping->report_id;
|
---|
[dcb7d7cd] | 152 |
|
---|
[19e0560e] | 153 | do {
|
---|
[f3f9733] | 154 | usb_log_debug("Trying report id %u\n", report_id);
|
---|
| 155 | if (report_id != 0) {
|
---|
[dcb7d7cd] | 156 | usb_hid_report_path_set_report_id(usage_path,
|
---|
[f3f9733] | 157 | report_id);
|
---|
[dcb7d7cd] | 158 | }
|
---|
| 159 |
|
---|
[07b9cbae] | 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);
|
---|
[a8c4e871] | 164 |
|
---|
[f3f9733] | 165 | usb_log_debug("Field: %p\n", field);
|
---|
[dcb7d7cd] | 166 |
|
---|
[f3f9733] | 167 | if (field != NULL) {
|
---|
| 168 | matches = true;
|
---|
| 169 | break;
|
---|
[dcb7d7cd] | 170 | }
|
---|
[a8c4e871] | 171 |
|
---|
[f3f9733] | 172 | report_id = usb_hid_get_next_report_id(
|
---|
[a8c4e871] | 173 | &hid_dev->report, report_id, USB_HID_REPORT_TYPE_INPUT);
|
---|
[f3f9733] | 174 | } while (!matches && report_id != 0);
|
---|
[cc29622] | 175 |
|
---|
[f76153ce] | 176 | usb_hid_report_path_free(usage_path);
|
---|
[cc29622] | 177 |
|
---|
[f3f9733] | 178 | return matches;
|
---|
[f76153ce] | 179 | }
|
---|
[76fbd9a] | 180 |
|
---|
[3f8f09f] | 181 | static int usb_hid_save_subdrivers(usb_hid_dev_t *hid_dev,
|
---|
[07b9cbae] | 182 | const usb_hid_subdriver_t **subdrivers, unsigned count)
|
---|
[60c0573] | 183 | {
|
---|
[07b9cbae] | 184 | assert(hid_dev);
|
---|
| 185 | assert(subdrivers);
|
---|
[cc29622] | 186 |
|
---|
[07b9cbae] | 187 | if (count == 0) {
|
---|
[f76153ce] | 188 | hid_dev->subdriver_count = 0;
|
---|
| 189 | hid_dev->subdrivers = NULL;
|
---|
| 190 | return EOK;
|
---|
| 191 | }
|
---|
[cc29622] | 192 |
|
---|
[3f8f09f] | 193 | /* +1 for generic hid subdriver */
|
---|
| 194 | hid_dev->subdrivers = calloc((count + 1), sizeof(usb_hid_subdriver_t));
|
---|
[f76153ce] | 195 | if (hid_dev->subdrivers == NULL) {
|
---|
| 196 | return ENOMEM;
|
---|
| 197 | }
|
---|
[cc29622] | 198 |
|
---|
[07b9cbae] | 199 | for (unsigned i = 0; i < count; ++i) {
|
---|
| 200 | hid_dev->subdrivers[i] = *subdrivers[i];
|
---|
[f76153ce] | 201 | }
|
---|
[cc29622] | 202 |
|
---|
[3f8f09f] | 203 | /* Add one generic HID subdriver per device */
|
---|
[78bfae9] | 204 | hid_dev->subdrivers[count].init = usb_generic_hid_init;
|
---|
| 205 | hid_dev->subdrivers[count].poll = usb_generic_hid_polling_callback;
|
---|
[c5b6db53] | 206 | hid_dev->subdrivers[count].deinit = usb_generic_hid_deinit;
|
---|
[78bfae9] | 207 | hid_dev->subdrivers[count].poll_end = NULL;
|
---|
[cc29622] | 208 |
|
---|
[78bfae9] | 209 | hid_dev->subdriver_count = count + 1;
|
---|
[cc29622] | 210 |
|
---|
[60c0573] | 211 | return EOK;
|
---|
[61257f4] | 212 | }
|
---|
[76fbd9a] | 213 |
|
---|
[f76153ce] | 214 | static int usb_hid_find_subdrivers(usb_hid_dev_t *hid_dev)
|
---|
| 215 | {
|
---|
[e7df6cd] | 216 | assert(hid_dev != NULL);
|
---|
[cc29622] | 217 |
|
---|
[f76153ce] | 218 | const usb_hid_subdriver_t *subdrivers[USB_HID_MAX_SUBDRIVERS];
|
---|
[07b9cbae] | 219 | unsigned count = 0;
|
---|
[cc29622] | 220 |
|
---|
[07b9cbae] | 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. */
|
---|
[d0a6e54] | 225 | if (mapping->vendor_id >= 0 && mapping->product_id < 0) {
|
---|
[07b9cbae] | 226 | usb_log_warning("Mapping[%d]: Missing Product ID for "
|
---|
| 227 | "Vendor ID %d\n", i, mapping->vendor_id);
|
---|
[f76153ce] | 228 | }
|
---|
[d0a6e54] | 229 | if (mapping->product_id >= 0 && mapping->vendor_id < 0) {
|
---|
[07b9cbae] | 230 | usb_log_warning("Mapping[%d]: Missing Vendor ID for "
|
---|
| 231 | "Product ID %d\n", i, mapping->product_id);
|
---|
[f76153ce] | 232 | }
|
---|
[3f8f09f] | 233 |
|
---|
[07b9cbae] | 234 | bool matched = false;
|
---|
[3f8f09f] | 235 |
|
---|
[07b9cbae] | 236 | /* Check ID match. */
|
---|
| 237 | if (mapping->vendor_id >= 0 && mapping->product_id >= 0) {
|
---|
[777e336] | 238 | usb_log_debug("Comparing device against vendor ID %u"
|
---|
| 239 | " and product ID %u.\n", mapping->vendor_id,
|
---|
[f76153ce] | 240 | mapping->product_id);
|
---|
| 241 | if (usb_hid_ids_match(hid_dev, mapping)) {
|
---|
[4bb9fd2] | 242 | usb_log_debug("IDs matched.\n");
|
---|
[07b9cbae] | 243 | matched = true;
|
---|
[f76153ce] | 244 | }
|
---|
| 245 | }
|
---|
[3f8f09f] | 246 |
|
---|
[07b9cbae] | 247 | /* Check usage match. */
|
---|
[f76153ce] | 248 | if (mapping->usage_path != NULL) {
|
---|
[1cbb4b7] | 249 | usb_log_debug("Comparing device against usage path.\n");
|
---|
[e3b5129] | 250 | if (usb_hid_path_matches(hid_dev, mapping)) {
|
---|
[2d1ba51] | 251 | /* Does not matter if IDs were matched. */
|
---|
[4bb9fd2] | 252 | matched = true;
|
---|
[f76153ce] | 253 | }
|
---|
| 254 | }
|
---|
[3f8f09f] | 255 |
|
---|
[4bb9fd2] | 256 | if (matched) {
|
---|
[d1fb591] | 257 | usb_log_debug("Subdriver matched.\n");
|
---|
[4bb9fd2] | 258 | subdrivers[count++] = &mapping->subdriver;
|
---|
[f76153ce] | 259 | }
|
---|
| 260 | }
|
---|
[cc29622] | 261 |
|
---|
[3f8f09f] | 262 | /* We have all subdrivers determined, save them into the hid device */
|
---|
[f76153ce] | 263 | return usb_hid_save_subdrivers(hid_dev, subdrivers, count);
|
---|
| 264 | }
|
---|
[76fbd9a] | 265 |
|
---|
[3f8f09f] | 266 | static int usb_hid_check_pipes(usb_hid_dev_t *hid_dev, const usb_device_t *dev)
|
---|
[966acede] | 267 | {
|
---|
[3f8f09f] | 268 | assert(hid_dev);
|
---|
| 269 | assert(dev);
|
---|
[cc29622] | 270 |
|
---|
[90df90c] | 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 | }
|
---|
[61257f4] | 289 | }
|
---|
[90df90c] | 290 | return ENOTSUP;
|
---|
[61257f4] | 291 | }
|
---|
[76fbd9a] | 292 |
|
---|
[d1fb591] | 293 | static int usb_hid_init_report(usb_hid_dev_t *hid_dev)
|
---|
| 294 | {
|
---|
[a8c4e871] | 295 | assert(hid_dev != NULL);
|
---|
[cc29622] | 296 |
|
---|
[d1fb591] | 297 | uint8_t report_id = 0;
|
---|
| 298 | size_t max_size = 0;
|
---|
[cc29622] | 299 |
|
---|
[a9cdca0] | 300 | do {
|
---|
[2002595] | 301 | usb_log_debug("Getting size of the report.\n");
|
---|
[a8c4e871] | 302 | const size_t size =
|
---|
| 303 | usb_hid_report_byte_size(&hid_dev->report, report_id,
|
---|
| 304 | USB_HID_REPORT_TYPE_INPUT);
|
---|
[8fb45e08] | 305 | usb_log_debug("Report ID: %u, size: %zu\n", report_id, size);
|
---|
| 306 | max_size = (size > max_size) ? size : max_size;
|
---|
[2002595] | 307 | usb_log_debug("Getting next report ID\n");
|
---|
[a8c4e871] | 308 | report_id = usb_hid_get_next_report_id(&hid_dev->report,
|
---|
[a9cdca0] | 309 | report_id, USB_HID_REPORT_TYPE_INPUT);
|
---|
| 310 | } while (report_id != 0);
|
---|
[cc29622] | 311 |
|
---|
[d1fb591] | 312 | usb_log_debug("Max size of input report: %zu\n", max_size);
|
---|
[cc29622] | 313 |
|
---|
[d1fb591] | 314 | assert(hid_dev->input_report == NULL);
|
---|
[cc29622] | 315 |
|
---|
[3f8f09f] | 316 | hid_dev->input_report = calloc(1, max_size);
|
---|
[d1fb591] | 317 | if (hid_dev->input_report == NULL) {
|
---|
| 318 | return ENOMEM;
|
---|
| 319 | }
|
---|
[3f8f09f] | 320 | hid_dev->max_input_report_size = max_size;
|
---|
[cc29622] | 321 |
|
---|
[d1fb591] | 322 | return EOK;
|
---|
| 323 | }
|
---|
[76fbd9a] | 324 |
|
---|
[555499da] | 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 | */
|
---|
[61257f4] | 342 | int usb_hid_init(usb_hid_dev_t *hid_dev, usb_device_t *dev)
|
---|
| 343 | {
|
---|
[555499da] | 344 | assert(hid_dev);
|
---|
| 345 | assert(dev);
|
---|
[cc29622] | 346 |
|
---|
[61257f4] | 347 | usb_log_debug("Initializing HID structure...\n");
|
---|
[cc29622] | 348 |
|
---|
[a8c4e871] | 349 | usb_hid_report_init(&hid_dev->report);
|
---|
[e5024111] | 350 |
|
---|
[61257f4] | 351 | /* The USB device should already be initialized, save it in structure */
|
---|
| 352 | hid_dev->usb_dev = dev;
|
---|
[065064e6] | 353 | hid_dev->poll_pipe_index = -1;
|
---|
[cc29622] | 354 |
|
---|
[555499da] | 355 | int rc = usb_hid_check_pipes(hid_dev, dev);
|
---|
[61257f4] | 356 | if (rc != EOK) {
|
---|
| 357 | return rc;
|
---|
| 358 | }
|
---|
[a8c4e871] | 359 |
|
---|
[f76153ce] | 360 | /* Get the report descriptor and parse it. */
|
---|
[07b9cbae] | 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);
|
---|
[cc29622] | 364 |
|
---|
[07b9cbae] | 365 | /* If report parsing went well, find subdrivers. */
|
---|
[f76153ce] | 366 | if (rc == EOK) {
|
---|
[07b9cbae] | 367 | usb_hid_find_subdrivers(hid_dev);
|
---|
[f76153ce] | 368 | } else {
|
---|
[07b9cbae] | 369 | usb_log_error("Failed to parse report descriptor: fallback.\n");
|
---|
| 370 | hid_dev->subdrivers = NULL;
|
---|
| 371 | hid_dev->subdriver_count = 0;
|
---|
[f76153ce] | 372 | }
|
---|
[cc29622] | 373 |
|
---|
[07b9cbae] | 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 |
|
---|
[f76153ce] | 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) {
|
---|
[07b9cbae] | 388 | usb_hid_set_boot_kbd_subdriver(hid_dev);
|
---|
[60c0573] | 389 | }
|
---|
[f76153ce] | 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) {
|
---|
[07b9cbae] | 395 | usb_hid_set_boot_mouse_subdriver(hid_dev);
|
---|
[f76153ce] | 396 | }
|
---|
| 397 | break;
|
---|
| 398 | default:
|
---|
[3f8f09f] | 399 | assert(hid_dev->poll_pipe_index
|
---|
[f76153ce] | 400 | == USB_HID_GENERIC_POLL_EP_NO);
|
---|
| 401 | usb_log_info("Falling back to generic HID driver.\n");
|
---|
[07b9cbae] | 402 | usb_hid_set_generic_hid_subdriver(hid_dev);
|
---|
[e9f0348] | 403 | }
|
---|
[61257f4] | 404 | }
|
---|
[cc29622] | 405 |
|
---|
[07b9cbae] | 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;
|
---|
[f317490] | 428 | for (unsigned i = 0; i < hid_dev->subdriver_count; ++i) {
|
---|
[07b9cbae] | 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;
|
---|
[f76153ce] | 438 | } else {
|
---|
[07b9cbae] | 439 | /* At least one subdriver initialized. */
|
---|
[f76153ce] | 440 | ok = true;
|
---|
| 441 | }
|
---|
[07b9cbae] | 442 | } else {
|
---|
| 443 | /* Does not need initialization. */
|
---|
| 444 | ok = true;
|
---|
[f76153ce] | 445 | }
|
---|
| 446 | }
|
---|
[cc29622] | 447 |
|
---|
[07b9cbae] | 448 | if (ok) {
|
---|
| 449 | /* Save max input report size and
|
---|
| 450 | * allocate space for the report */
|
---|
[2002595] | 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 | }
|
---|
[d1fb591] | 456 | }
|
---|
[cc29622] | 457 |
|
---|
[61257f4] | 458 | return rc;
|
---|
| 459 | }
|
---|
[76fbd9a] | 460 |
|
---|
[3f8f09f] | 461 | bool usb_hid_polling_callback(usb_device_t *dev, uint8_t *buffer,
|
---|
[60c0573] | 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 | }
|
---|
[3f8f09f] | 468 | usb_hid_dev_t *hid_dev = arg;
|
---|
[cc29622] | 469 |
|
---|
[d1fb591] | 470 | assert(hid_dev->input_report != NULL);
|
---|
[3f8f09f] | 471 |
|
---|
[ae5f77d5] | 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));
|
---|
[19e0560e] | 475 |
|
---|
[2002595] | 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 | }
|
---|
[cc29622] | 482 |
|
---|
[3f8f09f] | 483 | /* Parse the input report */
|
---|
| 484 | const int rc = usb_hid_parse_report(
|
---|
| 485 | &hid_dev->report, buffer, buffer_size, &hid_dev->report_id);
|
---|
[65c3794] | 486 | if (rc != EOK) {
|
---|
| 487 | usb_log_warning("Error in usb_hid_parse_report():"
|
---|
| 488 | "%s\n", str_error(rc));
|
---|
[a8c4e871] | 489 | }
|
---|
[cc29622] | 490 |
|
---|
[60c0573] | 491 | bool cont = false;
|
---|
[3f8f09f] | 492 | /* Continue if at least one of the subdrivers want to continue */
|
---|
[f317490] | 493 | for (unsigned i = 0; i < hid_dev->subdriver_count; ++i) {
|
---|
[3f8f09f] | 494 | if (hid_dev->subdrivers[i].poll != NULL) {
|
---|
| 495 | cont = cont || hid_dev->subdrivers[i].poll(
|
---|
| 496 | hid_dev, hid_dev->subdrivers[i].data);
|
---|
[60c0573] | 497 | }
|
---|
| 498 | }
|
---|
[cc29622] | 499 |
|
---|
[60c0573] | 500 | return cont;
|
---|
| 501 | }
|
---|
[76fbd9a] | 502 |
|
---|
[3f8f09f] | 503 | void usb_hid_polling_ended_callback(usb_device_t *dev, bool reason, void *arg)
|
---|
[61257f4] | 504 | {
|
---|
[3f8f09f] | 505 | assert(dev);
|
---|
| 506 | assert(arg);
|
---|
[cc29622] | 507 |
|
---|
[3f8f09f] | 508 | usb_hid_dev_t *hid_dev = arg;
|
---|
[cc29622] | 509 |
|
---|
[f317490] | 510 | for (unsigned i = 0; i < hid_dev->subdriver_count; ++i) {
|
---|
[60c0573] | 511 | if (hid_dev->subdrivers[i].poll_end != NULL) {
|
---|
[3f8f09f] | 512 | hid_dev->subdrivers[i].poll_end(
|
---|
| 513 | hid_dev, hid_dev->subdrivers[i].data, reason);
|
---|
[60c0573] | 514 | }
|
---|
| 515 | }
|
---|
[cc29622] | 516 |
|
---|
[68dbe3e] | 517 | hid_dev->running = false;
|
---|
[61257f4] | 518 | }
|
---|
[76fbd9a] | 519 |
|
---|
[8fb45e08] | 520 | void usb_hid_new_report(usb_hid_dev_t *hid_dev)
|
---|
[dd3eda2] | 521 | {
|
---|
[8fb45e08] | 522 | ++hid_dev->report_nr;
|
---|
[dd3eda2] | 523 | }
|
---|
[76fbd9a] | 524 |
|
---|
[3f8f09f] | 525 | int usb_hid_report_number(const usb_hid_dev_t *hid_dev)
|
---|
[dd3eda2] | 526 | {
|
---|
[8fb45e08] | 527 | return hid_dev->report_nr;
|
---|
[dd3eda2] | 528 | }
|
---|
[76fbd9a] | 529 |
|
---|
[a8c4e871] | 530 | void usb_hid_deinit(usb_hid_dev_t *hid_dev)
|
---|
[61257f4] | 531 | {
|
---|
[3f8f09f] | 532 | assert(hid_dev);
|
---|
| 533 | assert(hid_dev->subdrivers != NULL || hid_dev->subdriver_count == 0);
|
---|
[cc29622] | 534 |
|
---|
| 535 |
|
---|
[aaf6155] | 536 | usb_log_debug("Subdrivers: %p, subdriver count: %d\n",
|
---|
[5f6e25e] | 537 | hid_dev->subdrivers, hid_dev->subdriver_count);
|
---|
[cc29622] | 538 |
|
---|
[f317490] | 539 | for (unsigned i = 0; i < hid_dev->subdriver_count; ++i) {
|
---|
[5f6e25e] | 540 | if (hid_dev->subdrivers[i].deinit != NULL) {
|
---|
| 541 | hid_dev->subdrivers[i].deinit(hid_dev,
|
---|
| 542 | hid_dev->subdrivers[i].data);
|
---|
[60c0573] | 543 | }
|
---|
[61257f4] | 544 | }
|
---|
[cc29622] | 545 |
|
---|
[cddd151] | 546 | /* Free allocated structures */
|
---|
| 547 | free(hid_dev->subdrivers);
|
---|
| 548 | free(hid_dev->report_desc);
|
---|
[61257f4] | 549 |
|
---|
[cddd151] | 550 | /* Destroy the parser */
|
---|
[a8c4e871] | 551 | usb_hid_report_deinit(&hid_dev->report);
|
---|
[61257f4] | 552 |
|
---|
[966acede] | 553 | }
|
---|
| 554 |
|
---|
| 555 | /**
|
---|
| 556 | * @}
|
---|
| 557 | */
|
---|