2
* Copyright (c) 2011 Lubos Slovak
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
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.
29
/** @addtogroup drvusbhid
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>
44
#include <str_error.h>
48
#include "kbd/kbddev.h"
49
#include "generic/hiddev.h"
50
#include "mouse/mousedev.h"
51
#include "subdrivers.h"
53
/*----------------------------------------------------------------------------*/
55
/* Array of endpoints expected on the device, NULL terminated. */
56
usb_endpoint_description_t *usb_hid_endpoints[USB_HID_POLL_EP_COUNT + 1] = {
57
&usb_hid_kbd_poll_endpoint_description,
58
&usb_hid_mouse_poll_endpoint_description,
59
&usb_hid_generic_poll_endpoint_description,
63
static const int USB_HID_MAX_SUBDRIVERS = 10;
65
/*----------------------------------------------------------------------------*/
67
static int usb_hid_set_boot_kbd_subdriver(usb_hid_dev_t *hid_dev)
69
assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
71
hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(
72
sizeof(usb_hid_subdriver_t));
73
if (hid_dev->subdrivers == NULL) {
77
assert(hid_dev->subdriver_count >= 0);
79
// set the init callback
80
hid_dev->subdrivers[hid_dev->subdriver_count].init = usb_kbd_init;
82
// set the polling callback
83
hid_dev->subdrivers[hid_dev->subdriver_count].poll =
84
usb_kbd_polling_callback;
86
// set the polling ended callback
87
hid_dev->subdrivers[hid_dev->subdriver_count].poll_end = NULL;
89
// set the deinit callback
90
hid_dev->subdrivers[hid_dev->subdriver_count].deinit = usb_kbd_deinit;
92
// set subdriver count
93
++hid_dev->subdriver_count;
98
/*----------------------------------------------------------------------------*/
100
static int usb_hid_set_boot_mouse_subdriver(usb_hid_dev_t *hid_dev)
102
assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
104
hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(
105
sizeof(usb_hid_subdriver_t));
106
if (hid_dev->subdrivers == NULL) {
110
assert(hid_dev->subdriver_count >= 0);
112
// set the init callback
113
hid_dev->subdrivers[hid_dev->subdriver_count].init = usb_mouse_init;
115
// set the polling callback
116
hid_dev->subdrivers[hid_dev->subdriver_count].poll =
117
usb_mouse_polling_callback;
119
// set the polling ended callback
120
hid_dev->subdrivers[hid_dev->subdriver_count].poll_end = NULL;
122
// set the deinit callback
123
hid_dev->subdrivers[hid_dev->subdriver_count].deinit = usb_mouse_deinit;
125
// set subdriver count
126
++hid_dev->subdriver_count;
131
/*----------------------------------------------------------------------------*/
133
static int usb_hid_set_generic_hid_subdriver(usb_hid_dev_t *hid_dev)
135
assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
137
hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(
138
sizeof(usb_hid_subdriver_t));
139
if (hid_dev->subdrivers == NULL) {
143
assert(hid_dev->subdriver_count >= 0);
145
// set the init callback
146
hid_dev->subdrivers[hid_dev->subdriver_count].init =
147
usb_generic_hid_init;
149
// set the polling callback
150
hid_dev->subdrivers[hid_dev->subdriver_count].poll =
151
usb_generic_hid_polling_callback;
153
// set the polling ended callback
154
hid_dev->subdrivers[hid_dev->subdriver_count].poll_end = NULL;
156
// set the deinit callback
157
hid_dev->subdrivers[hid_dev->subdriver_count].deinit = NULL;
159
// set subdriver count
160
++hid_dev->subdriver_count;
165
/*----------------------------------------------------------------------------*/
167
static bool usb_hid_ids_match(usb_hid_dev_t *hid_dev,
168
const usb_hid_subdriver_mapping_t *mapping)
170
assert(hid_dev != NULL);
171
assert(hid_dev->usb_dev != NULL);
173
return (hid_dev->usb_dev->descriptors.device.vendor_id
174
== mapping->vendor_id
175
&& hid_dev->usb_dev->descriptors.device.product_id
176
== mapping->product_id);
179
/*----------------------------------------------------------------------------*/
181
static bool usb_hid_path_matches(usb_hid_dev_t *hid_dev,
182
const usb_hid_subdriver_mapping_t *mapping)
184
assert(hid_dev != NULL);
185
assert(mapping != NULL);
187
usb_hid_report_path_t *usage_path = usb_hid_report_path();
188
if (usage_path == NULL) {
189
usb_log_debug("Failed to create usage path.\n");
193
while (mapping->usage_path[i].usage != 0
194
|| mapping->usage_path[i].usage_page != 0) {
195
if (usb_hid_report_path_append_item(usage_path,
196
mapping->usage_path[i].usage_page,
197
mapping->usage_path[i].usage) != EOK) {
198
usb_log_debug("Failed to append to usage path.\n");
199
usb_hid_report_path_free(usage_path);
205
assert(hid_dev->report != NULL);
207
usb_log_debug("Compare flags: %d\n", mapping->compare);
209
bool matches = false;
210
uint8_t report_id = mapping->report_id;
213
usb_log_debug("Trying report id %u\n", report_id);
215
if (report_id != 0) {
216
usb_hid_report_path_set_report_id(usage_path,
220
usb_hid_report_field_t *field = usb_hid_report_get_sibling(
222
NULL, usage_path, mapping->compare,
223
USB_HID_REPORT_TYPE_INPUT);
225
usb_log_debug("Field: %p\n", field);
232
report_id = usb_hid_get_next_report_id(
233
hid_dev->report, report_id,
234
USB_HID_REPORT_TYPE_INPUT);
235
} while (!matches && report_id != 0);
237
usb_hid_report_path_free(usage_path);
242
/*----------------------------------------------------------------------------*/
244
static int usb_hid_save_subdrivers(usb_hid_dev_t *hid_dev,
245
const usb_hid_subdriver_t **subdrivers, int count)
250
hid_dev->subdriver_count = 0;
251
hid_dev->subdrivers = NULL;
255
// add one generic HID subdriver per device
257
hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc((count + 1) *
258
sizeof(usb_hid_subdriver_t));
259
if (hid_dev->subdrivers == NULL) {
263
for (i = 0; i < count; ++i) {
264
hid_dev->subdrivers[i].init = subdrivers[i]->init;
265
hid_dev->subdrivers[i].deinit = subdrivers[i]->deinit;
266
hid_dev->subdrivers[i].poll = subdrivers[i]->poll;
267
hid_dev->subdrivers[i].poll_end = subdrivers[i]->poll_end;
270
hid_dev->subdrivers[count].init = usb_generic_hid_init;
271
hid_dev->subdrivers[count].poll = usb_generic_hid_polling_callback;
272
hid_dev->subdrivers[count].deinit = NULL;
273
hid_dev->subdrivers[count].poll_end = NULL;
275
hid_dev->subdriver_count = count + 1;
280
/*----------------------------------------------------------------------------*/
282
static int usb_hid_find_subdrivers(usb_hid_dev_t *hid_dev)
284
assert(hid_dev != NULL);
286
const usb_hid_subdriver_t *subdrivers[USB_HID_MAX_SUBDRIVERS];
288
int i = 0, count = 0;
289
const usb_hid_subdriver_mapping_t *mapping = &usb_hid_subdrivers[i];
294
while (count < USB_HID_MAX_SUBDRIVERS &&
295
(mapping->usage_path != NULL
296
|| mapping->vendor_id >= 0 || mapping->product_id >= 0)) {
297
// check the vendor & product ID
298
if (mapping->vendor_id >= 0 && mapping->product_id < 0) {
299
usb_log_warning("Missing Product ID for Vendor ID %d\n",
303
if (mapping->product_id >= 0 && mapping->vendor_id < 0) {
304
usb_log_warning("Missing Vendor ID for Product ID %d\n",
305
mapping->product_id);
312
if (mapping->vendor_id >= 0) {
313
assert(mapping->product_id >= 0);
314
usb_log_debug("Comparing device against vendor ID %u"
315
" and product ID %u.\n", mapping->vendor_id,
316
mapping->product_id);
317
if (usb_hid_ids_match(hid_dev, mapping)) {
318
usb_log_debug("IDs matched.\n");
323
if (mapping->usage_path != NULL) {
324
usb_log_debug("Comparing device against usage path.\n");
325
if (usb_hid_path_matches(hid_dev, mapping)) {
326
// does not matter if IDs were matched
330
// matched only if IDs were matched and there is no path
331
matched = ids_matched;
335
usb_log_debug("Subdriver matched.\n");
336
subdrivers[count++] = &mapping->subdriver;
339
mapping = &usb_hid_subdrivers[++i];
342
// we have all subdrivers determined, save them into the hid device
343
return usb_hid_save_subdrivers(hid_dev, subdrivers, count);
346
/*----------------------------------------------------------------------------*/
348
static int usb_hid_check_pipes(usb_hid_dev_t *hid_dev, usb_device_t *dev)
350
assert(hid_dev != NULL && dev != NULL);
354
if (dev->pipes[USB_HID_KBD_POLL_EP_NO].present) {
355
usb_log_debug("Found keyboard endpoint.\n");
356
// save the pipe index
357
hid_dev->poll_pipe_index = USB_HID_KBD_POLL_EP_NO;
358
} else if (dev->pipes[USB_HID_MOUSE_POLL_EP_NO].present) {
359
usb_log_debug("Found mouse endpoint.\n");
360
// save the pipe index
361
hid_dev->poll_pipe_index = USB_HID_MOUSE_POLL_EP_NO;
362
} else if (dev->pipes[USB_HID_GENERIC_POLL_EP_NO].present) {
363
usb_log_debug("Found generic HID endpoint.\n");
364
// save the pipe index
365
hid_dev->poll_pipe_index = USB_HID_GENERIC_POLL_EP_NO;
367
usb_log_error("None of supported endpoints found - probably"
368
" not a supported device.\n");
375
/*----------------------------------------------------------------------------*/
377
static int usb_hid_init_report(usb_hid_dev_t *hid_dev)
379
assert(hid_dev != NULL && hid_dev->report != NULL);
381
uint8_t report_id = 0;
387
usb_log_debug("Getting size of the report.\n");
388
size = usb_hid_report_byte_size(hid_dev->report, report_id,
389
USB_HID_REPORT_TYPE_INPUT);
390
usb_log_debug("Report ID: %u, size: %zu\n", report_id, size);
391
max_size = (size > max_size) ? size : max_size;
392
usb_log_debug("Getting next report ID\n");
393
report_id = usb_hid_get_next_report_id(hid_dev->report,
394
report_id, USB_HID_REPORT_TYPE_INPUT);
395
} while (report_id != 0);
397
usb_log_debug("Max size of input report: %zu\n", max_size);
399
hid_dev->max_input_report_size = max_size;
400
assert(hid_dev->input_report == NULL);
402
hid_dev->input_report = malloc(max_size);
403
if (hid_dev->input_report == NULL) {
406
memset(hid_dev->input_report, 0, max_size);
411
/*----------------------------------------------------------------------------*/
413
usb_hid_dev_t *usb_hid_new(void)
415
usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)calloc(1,
416
sizeof(usb_hid_dev_t));
418
if (hid_dev == NULL) {
419
usb_log_fatal("No memory!\n");
423
hid_dev->report = (usb_hid_report_t *)(malloc(sizeof(
425
if (hid_dev->report == NULL) {
426
usb_log_fatal("No memory!\n");
431
hid_dev->poll_pipe_index = -1;
436
/*----------------------------------------------------------------------------*/
438
int usb_hid_init(usb_hid_dev_t *hid_dev, usb_device_t *dev)
442
usb_log_debug("Initializing HID structure...\n");
444
if (hid_dev == NULL) {
445
usb_log_error("Failed to init HID structure: no structure given"
451
usb_log_error("Failed to init HID structure: no USB device"
456
/* The USB device should already be initialized, save it in structure */
457
hid_dev->usb_dev = dev;
459
rc = usb_hid_check_pipes(hid_dev, dev);
464
/* Get the report descriptor and parse it. */
465
rc = usb_hid_process_report_descriptor(hid_dev->usb_dev,
466
hid_dev->report, &hid_dev->report_desc, &hid_dev->report_desc_size);
468
bool fallback = false;
471
// try to find subdrivers that may want to handle this device
472
rc = usb_hid_find_subdrivers(hid_dev);
473
if (rc != EOK || hid_dev->subdriver_count == 0) {
474
// try to fall back to the boot protocol if available
475
usb_log_info("No subdrivers found to handle this"
478
assert(hid_dev->subdrivers == NULL);
479
assert(hid_dev->subdriver_count == 0);
482
usb_log_error("Failed to parse Report descriptor.\n");
483
// try to fall back to the boot protocol if available
488
// fall back to boot protocol
489
switch (hid_dev->poll_pipe_index) {
490
case USB_HID_KBD_POLL_EP_NO:
491
usb_log_info("Falling back to kbd boot protocol.\n");
492
rc = usb_kbd_set_boot_protocol(hid_dev);
494
rc = usb_hid_set_boot_kbd_subdriver(hid_dev);
497
case USB_HID_MOUSE_POLL_EP_NO:
498
usb_log_info("Falling back to mouse boot protocol.\n");
499
rc = usb_mouse_set_boot_protocol(hid_dev);
501
rc = usb_hid_set_boot_mouse_subdriver(hid_dev);
505
assert(hid_dev->poll_pipe_index
506
== USB_HID_GENERIC_POLL_EP_NO);
508
usb_log_info("Falling back to generic HID driver.\n");
509
rc = usb_hid_set_generic_hid_subdriver(hid_dev);
514
usb_log_error("No subdriver for handling this device could be"
515
" initialized: %s.\n", str_error(rc));
516
usb_log_debug("Subdriver count: %d\n",
517
hid_dev->subdriver_count);
522
usb_log_debug("Subdriver count: %d\n",
523
hid_dev->subdriver_count);
525
for (i = 0; i < hid_dev->subdriver_count; ++i) {
526
if (hid_dev->subdrivers[i].init != NULL) {
527
usb_log_debug("Initializing subdriver %d.\n",i);
528
rc = hid_dev->subdrivers[i].init(hid_dev,
529
&hid_dev->subdrivers[i].data);
531
usb_log_warning("Failed to initialize"
532
" HID subdriver structure.\n");
534
// at least one subdriver initialized
542
rc = (ok) ? EOK : -1; // what error to report
547
// save max input report size and allocate space for the report
548
rc = usb_hid_init_report(hid_dev);
550
usb_log_error("Failed to initialize input report buffer"
559
/*----------------------------------------------------------------------------*/
561
bool usb_hid_polling_callback(usb_device_t *dev, uint8_t *buffer,
562
size_t buffer_size, void *arg)
566
if (dev == NULL || arg == NULL || buffer == NULL) {
567
usb_log_error("Missing arguments to polling callback.\n");
571
usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
573
assert(hid_dev->input_report != NULL);
574
usb_log_debug("Max input report size: %zu, buffer size: %zu\n",
575
hid_dev->max_input_report_size, buffer_size);
577
if (hid_dev->max_input_report_size >= buffer_size) {
578
/*! @todo This should probably be atomic. */
579
memcpy(hid_dev->input_report, buffer, buffer_size);
580
hid_dev->input_report_size = buffer_size;
581
usb_hid_new_report(hid_dev);
586
// continue if at least one of the subdrivers want to continue
587
for (i = 0; i < hid_dev->subdriver_count; ++i) {
588
if (hid_dev->subdrivers[i].poll != NULL
589
&& hid_dev->subdrivers[i].poll(hid_dev,
590
hid_dev->subdrivers[i].data, buffer, buffer_size)) {
598
/*----------------------------------------------------------------------------*/
600
void usb_hid_polling_ended_callback(usb_device_t *dev, bool reason,
605
if (dev == NULL || arg == NULL) {
609
usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
611
for (i = 0; i < hid_dev->subdriver_count; ++i) {
612
if (hid_dev->subdrivers[i].poll_end != NULL) {
613
hid_dev->subdrivers[i].poll_end(hid_dev,
614
hid_dev->subdrivers[i].data, reason);
618
usb_hid_free(&hid_dev);
621
/*----------------------------------------------------------------------------*/
623
void usb_hid_new_report(usb_hid_dev_t *hid_dev)
625
++hid_dev->report_nr;
628
/*----------------------------------------------------------------------------*/
630
int usb_hid_report_number(usb_hid_dev_t *hid_dev)
632
return hid_dev->report_nr;
635
/*----------------------------------------------------------------------------*/
637
void usb_hid_free(usb_hid_dev_t **hid_dev)
641
if (hid_dev == NULL || *hid_dev == NULL) {
645
usb_log_debug("Subdrivers: %p, subdriver count: %d\n",
646
(*hid_dev)->subdrivers, (*hid_dev)->subdriver_count);
648
assert((*hid_dev)->subdrivers != NULL
649
|| (*hid_dev)->subdriver_count == 0);
651
for (i = 0; i < (*hid_dev)->subdriver_count; ++i) {
652
if ((*hid_dev)->subdrivers[i].deinit != NULL) {
653
(*hid_dev)->subdrivers[i].deinit(*hid_dev,
654
(*hid_dev)->subdrivers[i].data);
658
// free the subdrivers info
659
if ((*hid_dev)->subdrivers != NULL) {
660
free((*hid_dev)->subdrivers);
663
// destroy the parser
664
if ((*hid_dev)->report != NULL) {
665
usb_hid_free_report((*hid_dev)->report);
668
if ((*hid_dev)->report_desc != NULL) {
669
free((*hid_dev)->report_desc);