source: mainline/uspace/drv/bus/usb/usbhid/usbhid.c@ 98604cc

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

usbhid: Implement device_gone.

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