source: mainline/uspace/drv/usbhid/usbhid.c@ f76153ce

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

Subdriver initialization.

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