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 |
|
---|
44 | #include <errno.h>
|
---|
45 | #include <macros.h>
|
---|
46 | #include <str_error.h>
|
---|
47 |
|
---|
48 | #include "usbhid.h"
|
---|
49 |
|
---|
50 | #include "kbd/kbddev.h"
|
---|
51 | #include "generic/hiddev.h"
|
---|
52 | #include "mouse/mousedev.h"
|
---|
53 | #include "subdrivers.h"
|
---|
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 | static int usb_hid_set_boot_kbd_subdriver(usb_hid_dev_t *hid_dev)
|
---|
64 | {
|
---|
65 | assert(hid_dev != NULL);
|
---|
66 | assert(hid_dev->subdriver_count == 0);
|
---|
67 |
|
---|
68 | hid_dev->subdrivers = malloc(sizeof(usb_hid_subdriver_t));
|
---|
69 | if (hid_dev->subdrivers == NULL) {
|
---|
70 | return ENOMEM;
|
---|
71 | }
|
---|
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;
|
---|
75 |
|
---|
76 | return EOK;
|
---|
77 | }
|
---|
78 |
|
---|
79 | static int usb_hid_set_boot_mouse_subdriver(usb_hid_dev_t *hid_dev)
|
---|
80 | {
|
---|
81 | assert(hid_dev != NULL);
|
---|
82 | assert(hid_dev->subdriver_count == 0);
|
---|
83 |
|
---|
84 | hid_dev->subdrivers = malloc(sizeof(usb_hid_subdriver_t));
|
---|
85 | if (hid_dev->subdrivers == NULL) {
|
---|
86 | return ENOMEM;
|
---|
87 | }
|
---|
88 | hid_dev->subdriver_count = 1;
|
---|
89 | // TODO 2 should be mouse, but find a better way
|
---|
90 | hid_dev->subdrivers[0] = usb_hid_subdrivers[2].subdriver;
|
---|
91 |
|
---|
92 | return EOK;
|
---|
93 | }
|
---|
94 |
|
---|
95 | static int usb_hid_set_generic_hid_subdriver(usb_hid_dev_t *hid_dev)
|
---|
96 | {
|
---|
97 | assert(hid_dev != NULL);
|
---|
98 | assert(hid_dev->subdriver_count == 0);
|
---|
99 |
|
---|
100 | hid_dev->subdrivers = malloc(sizeof(usb_hid_subdriver_t));
|
---|
101 | if (hid_dev->subdrivers == NULL) {
|
---|
102 | return ENOMEM;
|
---|
103 | }
|
---|
104 | hid_dev->subdriver_count = 1;
|
---|
105 |
|
---|
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;
|
---|
111 |
|
---|
112 | return EOK;
|
---|
113 | }
|
---|
114 |
|
---|
115 | static bool usb_hid_ids_match(const usb_hid_dev_t *hid_dev,
|
---|
116 | const usb_hid_subdriver_mapping_t *mapping)
|
---|
117 | {
|
---|
118 | assert(hid_dev);
|
---|
119 | assert(hid_dev->usb_dev);
|
---|
120 | assert(mapping);
|
---|
121 | const usb_standard_device_descriptor_t *d =
|
---|
122 | &usb_device_descriptors(hid_dev->usb_dev)->device;
|
---|
123 |
|
---|
124 | return (d->vendor_id == mapping->vendor_id)
|
---|
125 | && (d->product_id == mapping->product_id);
|
---|
126 | }
|
---|
127 |
|
---|
128 | static bool usb_hid_path_matches(usb_hid_dev_t *hid_dev,
|
---|
129 | const usb_hid_subdriver_mapping_t *mapping)
|
---|
130 | {
|
---|
131 | assert(hid_dev != NULL);
|
---|
132 | assert(mapping != NULL);
|
---|
133 |
|
---|
134 | usb_hid_report_path_t *usage_path = usb_hid_report_path();
|
---|
135 | if (usage_path == NULL) {
|
---|
136 | usb_log_debug("Failed to create usage path.\n");
|
---|
137 | return false;
|
---|
138 | }
|
---|
139 |
|
---|
140 | for (int i = 0; mapping->usage_path[i].usage != 0
|
---|
141 | || mapping->usage_path[i].usage_page != 0; ++i) {
|
---|
142 | if (usb_hid_report_path_append_item(usage_path,
|
---|
143 | mapping->usage_path[i].usage_page,
|
---|
144 | mapping->usage_path[i].usage) != EOK) {
|
---|
145 | usb_log_debug("Failed to append to usage path.\n");
|
---|
146 | usb_hid_report_path_free(usage_path);
|
---|
147 | return false;
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | usb_log_debug("Compare flags: %d\n", mapping->compare);
|
---|
152 |
|
---|
153 | bool matches = false;
|
---|
154 | uint8_t report_id = mapping->report_id;
|
---|
155 |
|
---|
156 | do {
|
---|
157 | usb_log_debug("Trying report id %u\n", report_id);
|
---|
158 | if (report_id != 0) {
|
---|
159 | usb_hid_report_path_set_report_id(usage_path,
|
---|
160 | report_id);
|
---|
161 | }
|
---|
162 |
|
---|
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);
|
---|
167 |
|
---|
168 | usb_log_debug("Field: %p\n", field);
|
---|
169 |
|
---|
170 | if (field != NULL) {
|
---|
171 | matches = true;
|
---|
172 | break;
|
---|
173 | }
|
---|
174 |
|
---|
175 | report_id = usb_hid_get_next_report_id(
|
---|
176 | &hid_dev->report, report_id, USB_HID_REPORT_TYPE_INPUT);
|
---|
177 | } while (!matches && report_id != 0);
|
---|
178 |
|
---|
179 | usb_hid_report_path_free(usage_path);
|
---|
180 |
|
---|
181 | return matches;
|
---|
182 | }
|
---|
183 |
|
---|
184 | static int usb_hid_save_subdrivers(usb_hid_dev_t *hid_dev,
|
---|
185 | const usb_hid_subdriver_t **subdrivers, unsigned count)
|
---|
186 | {
|
---|
187 | assert(hid_dev);
|
---|
188 | assert(subdrivers);
|
---|
189 |
|
---|
190 | if (count == 0) {
|
---|
191 | hid_dev->subdriver_count = 0;
|
---|
192 | hid_dev->subdrivers = NULL;
|
---|
193 | return EOK;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /* +1 for generic hid subdriver */
|
---|
197 | hid_dev->subdrivers = calloc((count + 1), sizeof(usb_hid_subdriver_t));
|
---|
198 | if (hid_dev->subdrivers == NULL) {
|
---|
199 | return ENOMEM;
|
---|
200 | }
|
---|
201 |
|
---|
202 | for (unsigned i = 0; i < count; ++i) {
|
---|
203 | hid_dev->subdrivers[i] = *subdrivers[i];
|
---|
204 | }
|
---|
205 |
|
---|
206 | /* Add one generic HID subdriver per device */
|
---|
207 | hid_dev->subdrivers[count].init = usb_generic_hid_init;
|
---|
208 | hid_dev->subdrivers[count].poll = usb_generic_hid_polling_callback;
|
---|
209 | hid_dev->subdrivers[count].deinit = usb_generic_hid_deinit;
|
---|
210 | hid_dev->subdrivers[count].poll_end = NULL;
|
---|
211 |
|
---|
212 | hid_dev->subdriver_count = count + 1;
|
---|
213 |
|
---|
214 | return EOK;
|
---|
215 | }
|
---|
216 |
|
---|
217 | static int usb_hid_find_subdrivers(usb_hid_dev_t *hid_dev)
|
---|
218 | {
|
---|
219 | assert(hid_dev != NULL);
|
---|
220 |
|
---|
221 | const usb_hid_subdriver_t *subdrivers[USB_HID_MAX_SUBDRIVERS];
|
---|
222 | unsigned count = 0;
|
---|
223 |
|
---|
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. */
|
---|
228 | if (mapping->vendor_id >= 0 && mapping->product_id < 0) {
|
---|
229 | usb_log_warning("Mapping[%d]: Missing Product ID for "
|
---|
230 | "Vendor ID %d\n", i, mapping->vendor_id);
|
---|
231 | }
|
---|
232 | if (mapping->product_id >= 0 && mapping->vendor_id < 0) {
|
---|
233 | usb_log_warning("Mapping[%d]: Missing Vendor ID for "
|
---|
234 | "Product ID %d\n", i, mapping->product_id);
|
---|
235 | }
|
---|
236 |
|
---|
237 | bool matched = false;
|
---|
238 |
|
---|
239 | /* Check ID match. */
|
---|
240 | if (mapping->vendor_id >= 0 && mapping->product_id >= 0) {
|
---|
241 | usb_log_debug("Comparing device against vendor ID %u"
|
---|
242 | " and product ID %u.\n", mapping->vendor_id,
|
---|
243 | mapping->product_id);
|
---|
244 | if (usb_hid_ids_match(hid_dev, mapping)) {
|
---|
245 | usb_log_debug("IDs matched.\n");
|
---|
246 | matched = true;
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | /* Check usage match. */
|
---|
251 | if (mapping->usage_path != NULL) {
|
---|
252 | usb_log_debug("Comparing device against usage path.\n");
|
---|
253 | if (usb_hid_path_matches(hid_dev, mapping)) {
|
---|
254 | /* Does not matter if IDs were matched. */
|
---|
255 | matched = true;
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | if (matched) {
|
---|
260 | usb_log_debug("Subdriver matched.\n");
|
---|
261 | subdrivers[count++] = &mapping->subdriver;
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | /* We have all subdrivers determined, save them into the hid device */
|
---|
266 | return usb_hid_save_subdrivers(hid_dev, subdrivers, count);
|
---|
267 | }
|
---|
268 |
|
---|
269 | static int usb_hid_check_pipes(usb_hid_dev_t *hid_dev, usb_device_t *dev)
|
---|
270 | {
|
---|
271 | assert(hid_dev);
|
---|
272 | assert(dev);
|
---|
273 |
|
---|
274 | static const struct {
|
---|
275 | const usb_endpoint_description_t *desc;
|
---|
276 | const char* description;
|
---|
277 | } endpoints[] = {
|
---|
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"},
|
---|
281 | };
|
---|
282 |
|
---|
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) {
|
---|
287 | usb_log_debug("Found: %s.\n", endpoints[i].description);
|
---|
288 | hid_dev->poll_pipe_mapping = epm;
|
---|
289 | return EOK;
|
---|
290 | }
|
---|
291 | }
|
---|
292 | return ENOTSUP;
|
---|
293 | }
|
---|
294 |
|
---|
295 | static int usb_hid_init_report(usb_hid_dev_t *hid_dev)
|
---|
296 | {
|
---|
297 | assert(hid_dev != NULL);
|
---|
298 |
|
---|
299 | uint8_t report_id = 0;
|
---|
300 | size_t max_size = 0;
|
---|
301 |
|
---|
302 | do {
|
---|
303 | usb_log_debug("Getting size of the report.\n");
|
---|
304 | const size_t size =
|
---|
305 | usb_hid_report_byte_size(&hid_dev->report, report_id,
|
---|
306 | USB_HID_REPORT_TYPE_INPUT);
|
---|
307 | usb_log_debug("Report ID: %u, size: %zu\n", report_id, size);
|
---|
308 | max_size = (size > max_size) ? size : max_size;
|
---|
309 | usb_log_debug("Getting next report ID\n");
|
---|
310 | report_id = usb_hid_get_next_report_id(&hid_dev->report,
|
---|
311 | report_id, USB_HID_REPORT_TYPE_INPUT);
|
---|
312 | } while (report_id != 0);
|
---|
313 |
|
---|
314 | usb_log_debug("Max size of input report: %zu\n", max_size);
|
---|
315 |
|
---|
316 | assert(hid_dev->input_report == NULL);
|
---|
317 |
|
---|
318 | hid_dev->input_report = calloc(1, max_size);
|
---|
319 | if (hid_dev->input_report == NULL) {
|
---|
320 | return ENOMEM;
|
---|
321 | }
|
---|
322 | hid_dev->max_input_report_size = max_size;
|
---|
323 |
|
---|
324 | return EOK;
|
---|
325 | }
|
---|
326 |
|
---|
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
|
---|
334 | * when a key is pressed or released. Finally, the LED lights are turned on
|
---|
335 | * according to the default setup of lock keys.
|
---|
336 | *
|
---|
337 | * @note By default, the keyboards is initialized with Num Lock turned on and
|
---|
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 | */
|
---|
344 | int usb_hid_init(usb_hid_dev_t *hid_dev, usb_device_t *dev)
|
---|
345 | {
|
---|
346 | assert(hid_dev);
|
---|
347 | assert(dev);
|
---|
348 |
|
---|
349 | usb_log_debug("Initializing HID structure...\n");
|
---|
350 |
|
---|
351 | usb_hid_report_init(&hid_dev->report);
|
---|
352 |
|
---|
353 | /* The USB device should already be initialized, save it in structure */
|
---|
354 | hid_dev->usb_dev = dev;
|
---|
355 | hid_dev->poll_pipe_mapping = NULL;
|
---|
356 |
|
---|
357 | int rc = usb_hid_check_pipes(hid_dev, dev);
|
---|
358 | if (rc != EOK) {
|
---|
359 | return rc;
|
---|
360 | }
|
---|
361 |
|
---|
362 | /* Get the report descriptor and parse it. */
|
---|
363 | rc = usb_hid_process_report_descriptor(
|
---|
364 | hid_dev->usb_dev, &hid_dev->report, &hid_dev->report_desc,
|
---|
365 | &hid_dev->report_desc_size);
|
---|
366 |
|
---|
367 | /* If report parsing went well, find subdrivers. */
|
---|
368 | if (rc == EOK) {
|
---|
369 | usb_hid_find_subdrivers(hid_dev);
|
---|
370 | } else {
|
---|
371 | usb_log_error("Failed to parse report descriptor: fallback.\n");
|
---|
372 | hid_dev->subdrivers = NULL;
|
---|
373 | hid_dev->subdriver_count = 0;
|
---|
374 | }
|
---|
375 |
|
---|
376 | usb_log_debug("Subdriver count(before trying boot protocol): %d\n",
|
---|
377 | hid_dev->subdriver_count);
|
---|
378 |
|
---|
379 | /* No subdrivers, fall back to the boot protocol if available. */
|
---|
380 | if (hid_dev->subdriver_count == 0) {
|
---|
381 | assert(hid_dev->subdrivers == NULL);
|
---|
382 | usb_log_info("No subdrivers found to handle device, trying "
|
---|
383 | "boot protocol.\n");
|
---|
384 |
|
---|
385 | switch (hid_dev->poll_pipe_mapping->interface->interface_protocol) {
|
---|
386 | case USB_HID_PROTOCOL_KEYBOARD:
|
---|
387 | usb_log_info("Falling back to kbd boot protocol.\n");
|
---|
388 | rc = usb_kbd_set_boot_protocol(hid_dev);
|
---|
389 | if (rc == EOK) {
|
---|
390 | usb_hid_set_boot_kbd_subdriver(hid_dev);
|
---|
391 | }
|
---|
392 | break;
|
---|
393 | case USB_HID_PROTOCOL_MOUSE:
|
---|
394 | usb_log_info("Falling back to mouse boot protocol.\n");
|
---|
395 | rc = usb_mouse_set_boot_protocol(hid_dev);
|
---|
396 | if (rc == EOK) {
|
---|
397 | usb_hid_set_boot_mouse_subdriver(hid_dev);
|
---|
398 | }
|
---|
399 | break;
|
---|
400 | default:
|
---|
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 | /* Initialize subdrivers */
|
---|
418 | bool ok = false;
|
---|
419 | for (unsigned i = 0; i < hid_dev->subdriver_count; ++i) {
|
---|
420 | if (hid_dev->subdrivers[i].init != NULL) {
|
---|
421 | usb_log_debug("Initializing subdriver %d.\n",i);
|
---|
422 | const int pret = hid_dev->subdrivers[i].init(hid_dev,
|
---|
423 | &hid_dev->subdrivers[i].data);
|
---|
424 | if (pret != EOK) {
|
---|
425 | usb_log_warning("Failed to initialize"
|
---|
426 | " HID subdriver structure: %s.\n",
|
---|
427 | str_error(pret));
|
---|
428 | rc = pret;
|
---|
429 | } else {
|
---|
430 | /* At least one subdriver initialized. */
|
---|
431 | ok = true;
|
---|
432 | }
|
---|
433 | } else {
|
---|
434 | /* Does not need initialization. */
|
---|
435 | ok = true;
|
---|
436 | }
|
---|
437 | }
|
---|
438 |
|
---|
439 | if (ok) {
|
---|
440 | /* Save max input report size and
|
---|
441 | * allocate space for the report */
|
---|
442 | rc = usb_hid_init_report(hid_dev);
|
---|
443 | if (rc != EOK) {
|
---|
444 | usb_log_error("Failed to initialize input report buffer"
|
---|
445 | ".\n");
|
---|
446 | }
|
---|
447 | }
|
---|
448 |
|
---|
449 | return rc;
|
---|
450 | }
|
---|
451 |
|
---|
452 | bool usb_hid_polling_callback(usb_device_t *dev, uint8_t *buffer,
|
---|
453 | size_t buffer_size, void *arg)
|
---|
454 | {
|
---|
455 | if (dev == NULL || arg == NULL || buffer == NULL) {
|
---|
456 | usb_log_error("Missing arguments to polling callback.\n");
|
---|
457 | return false;
|
---|
458 | }
|
---|
459 | usb_hid_dev_t *hid_dev = arg;
|
---|
460 |
|
---|
461 | assert(hid_dev->input_report != NULL);
|
---|
462 |
|
---|
463 | usb_log_debug("New data [%zu/%zu]: %s\n", buffer_size,
|
---|
464 | hid_dev->max_input_report_size,
|
---|
465 | usb_debug_str_buffer(buffer, buffer_size, 0));
|
---|
466 |
|
---|
467 | if (hid_dev->max_input_report_size >= buffer_size) {
|
---|
468 | /*! @todo This should probably be atomic. */
|
---|
469 | memcpy(hid_dev->input_report, buffer, buffer_size);
|
---|
470 | hid_dev->input_report_size = buffer_size;
|
---|
471 | usb_hid_new_report(hid_dev);
|
---|
472 | }
|
---|
473 |
|
---|
474 | /* Parse the input report */
|
---|
475 | const int rc = usb_hid_parse_report(
|
---|
476 | &hid_dev->report, buffer, buffer_size, &hid_dev->report_id);
|
---|
477 | if (rc != EOK) {
|
---|
478 | usb_log_warning("Failure in usb_hid_parse_report():"
|
---|
479 | "%s\n", str_error(rc));
|
---|
480 | }
|
---|
481 |
|
---|
482 | bool cont = false;
|
---|
483 | /* Continue if at least one of the subdrivers want to continue */
|
---|
484 | for (unsigned i = 0; i < hid_dev->subdriver_count; ++i) {
|
---|
485 | if (hid_dev->subdrivers[i].poll != NULL) {
|
---|
486 | cont = cont || hid_dev->subdrivers[i].poll(
|
---|
487 | hid_dev, hid_dev->subdrivers[i].data);
|
---|
488 | }
|
---|
489 | }
|
---|
490 |
|
---|
491 | return cont;
|
---|
492 | }
|
---|
493 |
|
---|
494 | void usb_hid_polling_ended_callback(usb_device_t *dev, bool reason, void *arg)
|
---|
495 | {
|
---|
496 | assert(dev);
|
---|
497 | assert(arg);
|
---|
498 |
|
---|
499 | usb_hid_dev_t *hid_dev = arg;
|
---|
500 |
|
---|
501 | for (unsigned i = 0; i < hid_dev->subdriver_count; ++i) {
|
---|
502 | if (hid_dev->subdrivers[i].poll_end != NULL) {
|
---|
503 | hid_dev->subdrivers[i].poll_end(
|
---|
504 | hid_dev, hid_dev->subdrivers[i].data, reason);
|
---|
505 | }
|
---|
506 | }
|
---|
507 |
|
---|
508 | hid_dev->running = false;
|
---|
509 | }
|
---|
510 |
|
---|
511 | void usb_hid_new_report(usb_hid_dev_t *hid_dev)
|
---|
512 | {
|
---|
513 | ++hid_dev->report_nr;
|
---|
514 | }
|
---|
515 |
|
---|
516 | int usb_hid_report_number(const usb_hid_dev_t *hid_dev)
|
---|
517 | {
|
---|
518 | return hid_dev->report_nr;
|
---|
519 | }
|
---|
520 |
|
---|
521 | void usb_hid_deinit(usb_hid_dev_t *hid_dev)
|
---|
522 | {
|
---|
523 | assert(hid_dev);
|
---|
524 | assert(hid_dev->subdrivers != NULL || hid_dev->subdriver_count == 0);
|
---|
525 |
|
---|
526 |
|
---|
527 | usb_log_debug("Subdrivers: %p, subdriver count: %d\n",
|
---|
528 | hid_dev->subdrivers, hid_dev->subdriver_count);
|
---|
529 |
|
---|
530 | for (unsigned i = 0; i < hid_dev->subdriver_count; ++i) {
|
---|
531 | if (hid_dev->subdrivers[i].deinit != NULL) {
|
---|
532 | hid_dev->subdrivers[i].deinit(hid_dev,
|
---|
533 | hid_dev->subdrivers[i].data);
|
---|
534 | }
|
---|
535 | }
|
---|
536 |
|
---|
537 | /* Free allocated structures */
|
---|
538 | free(hid_dev->subdrivers);
|
---|
539 | free(hid_dev->report_desc);
|
---|
540 |
|
---|
541 | /* Destroy the parser */
|
---|
542 | usb_hid_report_deinit(&hid_dev->report);
|
---|
543 |
|
---|
544 | }
|
---|
545 |
|
---|
546 | /**
|
---|
547 | * @}
|
---|
548 | */
|
---|