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

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

usb: Make endpoint descriptions const.

  • Property mode set to 100644
File size: 17.5 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
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 usb_log_debug("Compare flags: %d\n", mapping->compare);
207
208 bool matches = false;
209 uint8_t report_id = mapping->report_id;
210
211 do {
212 usb_log_debug("Trying report id %u\n", report_id);
213
214 if (report_id != 0) {
215 usb_hid_report_path_set_report_id(usage_path,
216 report_id);
217 }
218
219 usb_hid_report_field_t *field = usb_hid_report_get_sibling(
220 &hid_dev->report, NULL, usage_path, mapping->compare,
221 USB_HID_REPORT_TYPE_INPUT);
222
223 usb_log_debug("Field: %p\n", field);
224
225 if (field != NULL) {
226 matches = true;
227 break;
228 }
229
230 report_id = usb_hid_get_next_report_id(
231 &hid_dev->report, report_id, USB_HID_REPORT_TYPE_INPUT);
232 } while (!matches && report_id != 0);
233
234 usb_hid_report_path_free(usage_path);
235
236 return matches;
237}
238
239/*----------------------------------------------------------------------------*/
240
241static int usb_hid_save_subdrivers(usb_hid_dev_t *hid_dev,
242 const usb_hid_subdriver_t **subdrivers, int count)
243{
244 int i;
245
246 if (count <= 0) {
247 hid_dev->subdriver_count = 0;
248 hid_dev->subdrivers = NULL;
249 return EOK;
250 }
251
252 // add one generic HID subdriver per device
253
254 hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc((count + 1) *
255 sizeof(usb_hid_subdriver_t));
256 if (hid_dev->subdrivers == NULL) {
257 return ENOMEM;
258 }
259
260 for (i = 0; i < count; ++i) {
261 hid_dev->subdrivers[i].init = subdrivers[i]->init;
262 hid_dev->subdrivers[i].deinit = subdrivers[i]->deinit;
263 hid_dev->subdrivers[i].poll = subdrivers[i]->poll;
264 hid_dev->subdrivers[i].poll_end = subdrivers[i]->poll_end;
265 }
266
267 hid_dev->subdrivers[count].init = usb_generic_hid_init;
268 hid_dev->subdrivers[count].poll = usb_generic_hid_polling_callback;
269 hid_dev->subdrivers[count].deinit = usb_generic_hid_deinit;
270 hid_dev->subdrivers[count].poll_end = NULL;
271
272 hid_dev->subdriver_count = count + 1;
273
274 return EOK;
275}
276
277/*----------------------------------------------------------------------------*/
278
279static int usb_hid_find_subdrivers(usb_hid_dev_t *hid_dev)
280{
281 assert(hid_dev != NULL);
282
283 const usb_hid_subdriver_t *subdrivers[USB_HID_MAX_SUBDRIVERS];
284
285 int i = 0, count = 0;
286 const usb_hid_subdriver_mapping_t *mapping = &usb_hid_subdrivers[i];
287
288 bool ids_matched;
289 bool matched;
290
291 while (count < USB_HID_MAX_SUBDRIVERS &&
292 (mapping->usage_path != NULL
293 || mapping->vendor_id >= 0 || mapping->product_id >= 0)) {
294 // check the vendor & product ID
295 if (mapping->vendor_id >= 0 && mapping->product_id < 0) {
296 usb_log_warning("Missing Product ID for Vendor ID %d\n",
297 mapping->vendor_id);
298 return EINVAL;
299 }
300 if (mapping->product_id >= 0 && mapping->vendor_id < 0) {
301 usb_log_warning("Missing Vendor ID for Product ID %d\n",
302 mapping->product_id);
303 return EINVAL;
304 }
305
306 ids_matched = false;
307 matched = false;
308
309 if (mapping->vendor_id >= 0) {
310 assert(mapping->product_id >= 0);
311 usb_log_debug("Comparing device against vendor ID %u"
312 " and product ID %u.\n", mapping->vendor_id,
313 mapping->product_id);
314 if (usb_hid_ids_match(hid_dev, mapping)) {
315 usb_log_debug("IDs matched.\n");
316 ids_matched = true;
317 }
318 }
319
320 if (mapping->usage_path != NULL) {
321 usb_log_debug("Comparing device against usage path.\n");
322 if (usb_hid_path_matches(hid_dev, mapping)) {
323 // does not matter if IDs were matched
324 matched = true;
325 }
326 } else {
327 // matched only if IDs were matched and there is no path
328 matched = ids_matched;
329 }
330
331 if (matched) {
332 usb_log_debug("Subdriver matched.\n");
333 subdrivers[count++] = &mapping->subdriver;
334 }
335
336 mapping = &usb_hid_subdrivers[++i];
337 }
338
339 // we have all subdrivers determined, save them into the hid device
340 return usb_hid_save_subdrivers(hid_dev, subdrivers, count);
341}
342
343/*----------------------------------------------------------------------------*/
344
345static int usb_hid_check_pipes(usb_hid_dev_t *hid_dev, usb_device_t *dev)
346{
347 assert(hid_dev != NULL && dev != NULL);
348
349 int rc = EOK;
350
351 if (dev->pipes[USB_HID_KBD_POLL_EP_NO].present) {
352 usb_log_debug("Found keyboard endpoint.\n");
353 // save the pipe index
354 hid_dev->poll_pipe_index = USB_HID_KBD_POLL_EP_NO;
355 } else if (dev->pipes[USB_HID_MOUSE_POLL_EP_NO].present) {
356 usb_log_debug("Found mouse endpoint.\n");
357 // save the pipe index
358 hid_dev->poll_pipe_index = USB_HID_MOUSE_POLL_EP_NO;
359 } else if (dev->pipes[USB_HID_GENERIC_POLL_EP_NO].present) {
360 usb_log_debug("Found generic HID endpoint.\n");
361 // save the pipe index
362 hid_dev->poll_pipe_index = USB_HID_GENERIC_POLL_EP_NO;
363 } else {
364 usb_log_error("None of supported endpoints found - probably"
365 " not a supported device.\n");
366 rc = ENOTSUP;
367 }
368
369 return rc;
370}
371
372/*----------------------------------------------------------------------------*/
373
374static int usb_hid_init_report(usb_hid_dev_t *hid_dev)
375{
376 assert(hid_dev != NULL);
377
378 uint8_t report_id = 0;
379 size_t max_size = 0;
380
381 do {
382 usb_log_debug("Getting size of the report.\n");
383 const size_t size =
384 usb_hid_report_byte_size(&hid_dev->report, report_id,
385 USB_HID_REPORT_TYPE_INPUT);
386 usb_log_debug("Report ID: %u, size: %zu\n", report_id, size);
387 max_size = (size > max_size) ? size : max_size;
388 usb_log_debug("Getting next report ID\n");
389 report_id = usb_hid_get_next_report_id(&hid_dev->report,
390 report_id, USB_HID_REPORT_TYPE_INPUT);
391 } while (report_id != 0);
392
393 usb_log_debug("Max size of input report: %zu\n", max_size);
394
395 hid_dev->max_input_report_size = max_size;
396 assert(hid_dev->input_report == NULL);
397
398 hid_dev->input_report = malloc(max_size);
399 if (hid_dev->input_report == NULL) {
400 return ENOMEM;
401 }
402 memset(hid_dev->input_report, 0, max_size);
403
404 return EOK;
405}
406
407/*----------------------------------------------------------------------------*/
408
409int usb_hid_init(usb_hid_dev_t *hid_dev, usb_device_t *dev)
410{
411 int rc, i;
412
413 usb_log_debug("Initializing HID structure...\n");
414
415 if (hid_dev == NULL) {
416 usb_log_error("Failed to init HID structure: no structure given"
417 ".\n");
418 return EINVAL;
419 }
420
421 if (dev == NULL) {
422 usb_log_error("Failed to init HID structure: no USB device"
423 " given.\n");
424 return EINVAL;
425 }
426
427 usb_hid_report_init(&hid_dev->report);
428
429 /* The USB device should already be initialized, save it in structure */
430 hid_dev->usb_dev = dev;
431 hid_dev->poll_pipe_index = -1;
432
433 rc = usb_hid_check_pipes(hid_dev, dev);
434 if (rc != EOK) {
435 return rc;
436 }
437
438 /* Get the report descriptor and parse it. */
439 rc = usb_hid_process_report_descriptor(hid_dev->usb_dev,
440 &hid_dev->report, &hid_dev->report_desc, &hid_dev->report_desc_size);
441
442 bool fallback = false;
443
444 if (rc == EOK) {
445 // try to find subdrivers that may want to handle this device
446 rc = usb_hid_find_subdrivers(hid_dev);
447 if (rc != EOK || hid_dev->subdriver_count == 0) {
448 // try to fall back to the boot protocol if available
449 usb_log_info("No subdrivers found to handle this"
450 " device.\n");
451 fallback = true;
452 assert(hid_dev->subdrivers == NULL);
453 assert(hid_dev->subdriver_count == 0);
454 }
455 } else {
456 usb_log_error("Failed to parse Report descriptor.\n");
457 // try to fall back to the boot protocol if available
458 fallback = true;
459 }
460
461 if (fallback) {
462 // fall back to boot protocol
463 switch (hid_dev->poll_pipe_index) {
464 case USB_HID_KBD_POLL_EP_NO:
465 usb_log_info("Falling back to kbd boot protocol.\n");
466 rc = usb_kbd_set_boot_protocol(hid_dev);
467 if (rc == EOK) {
468 rc = usb_hid_set_boot_kbd_subdriver(hid_dev);
469 }
470 break;
471 case USB_HID_MOUSE_POLL_EP_NO:
472 usb_log_info("Falling back to mouse boot protocol.\n");
473 rc = usb_mouse_set_boot_protocol(hid_dev);
474 if (rc == EOK) {
475 rc = usb_hid_set_boot_mouse_subdriver(hid_dev);
476 }
477 break;
478 default:
479 assert(hid_dev->poll_pipe_index
480 == USB_HID_GENERIC_POLL_EP_NO);
481
482 usb_log_info("Falling back to generic HID driver.\n");
483 rc = usb_hid_set_generic_hid_subdriver(hid_dev);
484 }
485 }
486
487 if (rc != EOK) {
488 usb_log_error("No subdriver for handling this device could be"
489 " initialized: %s.\n", str_error(rc));
490 usb_log_debug("Subdriver count: %d\n",
491 hid_dev->subdriver_count);
492
493 } else {
494 bool ok = false;
495
496 usb_log_debug("Subdriver count: %d\n",
497 hid_dev->subdriver_count);
498
499 for (i = 0; i < hid_dev->subdriver_count; ++i) {
500 if (hid_dev->subdrivers[i].init != NULL) {
501 usb_log_debug("Initializing subdriver %d.\n",i);
502 rc = hid_dev->subdrivers[i].init(hid_dev,
503 &hid_dev->subdrivers[i].data);
504 if (rc != EOK) {
505 usb_log_warning("Failed to initialize"
506 " HID subdriver structure.\n");
507 } else {
508 // at least one subdriver initialized
509 ok = true;
510 }
511 } else {
512 ok = true;
513 }
514 }
515
516 rc = (ok) ? EOK : -1; // what error to report
517 }
518
519
520 if (rc == EOK) {
521 // save max input report size and allocate space for the report
522 rc = usb_hid_init_report(hid_dev);
523 if (rc != EOK) {
524 usb_log_error("Failed to initialize input report buffer"
525 ".\n");
526 }
527 }
528
529
530 return rc;
531}
532
533/*----------------------------------------------------------------------------*/
534
535bool usb_hid_polling_callback(usb_device_t *dev, uint8_t *buffer,
536 size_t buffer_size, void *arg)
537{
538 int i;
539
540 if (dev == NULL || arg == NULL || buffer == NULL) {
541 usb_log_error("Missing arguments to polling callback.\n");
542 return false;
543 }
544
545 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
546
547 assert(hid_dev->input_report != NULL);
548 usb_log_debug("New data [%zu/%zu]: %s\n", buffer_size,
549 hid_dev->max_input_report_size,
550 usb_debug_str_buffer(buffer, buffer_size, 0));
551
552 if (hid_dev->max_input_report_size >= buffer_size) {
553 /*! @todo This should probably be atomic. */
554 memcpy(hid_dev->input_report, buffer, buffer_size);
555 hid_dev->input_report_size = buffer_size;
556 usb_hid_new_report(hid_dev);
557 }
558
559 // parse the input report
560
561 int rc = usb_hid_parse_report(&hid_dev->report, buffer, buffer_size,
562 &hid_dev->report_id);
563
564 if (rc != EOK) {
565 usb_log_warning("Error in usb_hid_parse_report():"
566 "%s\n", str_error(rc));
567 }
568
569 bool cont = false;
570
571 // continue if at least one of the subdrivers want to continue
572 for (i = 0; i < hid_dev->subdriver_count; ++i) {
573 if (hid_dev->subdrivers[i].poll != NULL
574 && hid_dev->subdrivers[i].poll(hid_dev,
575 hid_dev->subdrivers[i].data)) {
576 cont = true;
577 }
578 }
579
580 return cont;
581}
582
583/*----------------------------------------------------------------------------*/
584
585void usb_hid_polling_ended_callback(usb_device_t *dev, bool reason,
586 void *arg)
587{
588 int i;
589
590 if (dev == NULL || arg == NULL) {
591 return;
592 }
593
594 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
595
596 for (i = 0; i < hid_dev->subdriver_count; ++i) {
597 if (hid_dev->subdrivers[i].poll_end != NULL) {
598 hid_dev->subdrivers[i].poll_end(hid_dev,
599 hid_dev->subdrivers[i].data, reason);
600 }
601 }
602
603 hid_dev->running = false;
604}
605
606/*----------------------------------------------------------------------------*/
607
608void usb_hid_new_report(usb_hid_dev_t *hid_dev)
609{
610 ++hid_dev->report_nr;
611}
612
613/*----------------------------------------------------------------------------*/
614
615int usb_hid_report_number(usb_hid_dev_t *hid_dev)
616{
617 return hid_dev->report_nr;
618}
619
620/*----------------------------------------------------------------------------*/
621
622void usb_hid_deinit(usb_hid_dev_t *hid_dev)
623{
624 int i;
625
626 if (hid_dev == NULL) {
627 return;
628 }
629
630 usb_log_debug("Subdrivers: %p, subdriver count: %d\n",
631 hid_dev->subdrivers, hid_dev->subdriver_count);
632
633 assert(hid_dev->subdrivers != NULL
634 || hid_dev->subdriver_count == 0);
635
636 for (i = 0; i < hid_dev->subdriver_count; ++i) {
637 if (hid_dev->subdrivers[i].deinit != NULL) {
638 hid_dev->subdrivers[i].deinit(hid_dev,
639 hid_dev->subdrivers[i].data);
640 }
641 }
642
643 /* Free allocated structures */
644 free(hid_dev->subdrivers);
645 free(hid_dev->report_desc);
646
647 /* Destroy the parser */
648 usb_hid_report_deinit(&hid_dev->report);
649
650}
651
652/**
653 * @}
654 */
Note: See TracBrowser for help on using the repository browser.