source: mainline/uspace/drv/bus/usb/usbhid/usbhid.c@ 1c1b577

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1c1b577 was 93d2684, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

usbhid: Count max number of sub-drivers based on available subdrivers.

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