source: mainline/uspace/drv/usbhid/usbhid.c@ 65b458c4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 65b458c4 was 65b458c4, checked in by Lubos Slovak <lubos.slovak@…>, 14 years ago

Subdriver-specific data moved to the subdriver structure.

+ DDF ops removed from hid_dev_t.

Should fix #225

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