source: mainline/uspace/drv/usbhid/usbhid.c@ 3bcac68

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

Product and Vendor ID may be 0 - changed type to int.

Fixes #193

  • Property mode set to 100644
File size: 15.7 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 != 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 // 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 != NULL && 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 != NULL && 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_mapping_t *mapping)
167{
168 assert(hid_dev != NULL);
169 assert(mapping != NULL);
170
171 usb_hid_report_path_t *usage_path = usb_hid_report_path();
172 if (usage_path == NULL) {
173 usb_log_debug("Failed to create usage path.\n");
174 return false;
175 }
176 int i = 0;
177 while (mapping->usage_path[i].usage != 0
178 || mapping->usage_path[i].usage_page != 0) {
179 if (usb_hid_report_path_append_item(usage_path,
180 mapping->usage_path[i].usage_page,
181 mapping->usage_path[i].usage) != EOK) {
182 usb_log_debug("Failed to append to usage path.\n");
183 usb_hid_report_path_free(usage_path);
184 return false;
185 }
186 ++i;
187 }
188
189 if (mapping->report_id >= 0) {
190 usb_hid_report_path_set_report_id(usage_path,
191 mapping->report_id);
192 }
193
194 assert(hid_dev->parser != NULL);
195
196 usb_log_debug("Compare flags: %d\n", mapping->compare);
197 size_t size = usb_hid_report_input_length(hid_dev->parser, usage_path,
198 mapping->compare);
199 usb_log_debug("Size of the input report: %zuB\n", size);
200
201 usb_hid_report_path_free(usage_path);
202
203 return (size > 0);
204}
205
206/*----------------------------------------------------------------------------*/
207
208static int usb_hid_save_subdrivers(usb_hid_dev_t *hid_dev,
209 const usb_hid_subdriver_t **subdrivers, int count)
210{
211 int i;
212
213 if (count <= 0) {
214 hid_dev->subdriver_count = 0;
215 hid_dev->subdrivers = NULL;
216 return EOK;
217 }
218
219 hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(count *
220 sizeof(usb_hid_subdriver_t));
221 if (hid_dev->subdrivers == NULL) {
222 return ENOMEM;
223 }
224
225 for (i = 0; i < count; ++i) {
226 hid_dev->subdrivers[i].init = subdrivers[i]->init;
227 hid_dev->subdrivers[i].deinit = subdrivers[i]->deinit;
228 hid_dev->subdrivers[i].poll = subdrivers[i]->poll;
229 hid_dev->subdrivers[i].poll_end = subdrivers[i]->poll_end;
230 }
231
232 hid_dev->subdriver_count = count;
233
234 return EOK;
235}
236
237/*----------------------------------------------------------------------------*/
238
239static int usb_hid_find_subdrivers(usb_hid_dev_t *hid_dev)
240{
241 assert(hid_dev != NULL);
242
243 const usb_hid_subdriver_t *subdrivers[USB_HID_MAX_SUBDRIVERS];
244
245 int i = 0, count = 0;
246 const usb_hid_subdriver_mapping_t *mapping = &usb_hid_subdrivers[i];
247
248 bool ids_matched;
249 bool matched;
250
251 while (count < USB_HID_MAX_SUBDRIVERS &&
252 (mapping->usage_path != NULL
253 || mapping->vendor_id >= 0 || mapping->product_id >= 0)) {
254 // check the vendor & product ID
255 if (mapping->vendor_id >= 0 && mapping->product_id < 0) {
256 usb_log_warning("Missing Product ID for Vendor ID %d\n",
257 mapping->vendor_id);
258 return EINVAL;
259 }
260 if (mapping->product_id >= 0 && mapping->vendor_id < 0) {
261 usb_log_warning("Missing Vendor ID for Product ID %d\n",
262 mapping->product_id);
263 return EINVAL;
264 }
265
266 ids_matched = false;
267 matched = false;
268
269 if (mapping->vendor_id >= 0) {
270 assert(mapping->product_id >= 0);
271 usb_log_debug("Comparing device against vendor ID %u"
272 " and product ID %u.\n", mapping->vendor_id,
273 mapping->product_id);
274 if (usb_hid_ids_match(hid_dev, mapping)) {
275 usb_log_debug("IDs matched.\n");
276 ids_matched = true;
277 }
278 }
279
280 if (mapping->usage_path != NULL) {
281 usb_log_debug("Comparing device against usage path.\n");
282 if (usb_hid_path_matches(hid_dev, mapping)) {
283 // does not matter if IDs were matched
284 matched = true;
285 }
286 } else {
287 // matched only if IDs were matched and there is no path
288 matched = ids_matched;
289 }
290
291 if (matched) {
292 subdrivers[count++] = &mapping->subdriver;
293 }
294
295 mapping = &usb_hid_subdrivers[++i];
296 }
297
298 // we have all subdrivers determined, save them into the hid device
299 return usb_hid_save_subdrivers(hid_dev, subdrivers, count);
300}
301
302/*----------------------------------------------------------------------------*/
303
304static int usb_hid_check_pipes(usb_hid_dev_t *hid_dev, usb_device_t *dev)
305{
306 assert(hid_dev != NULL && dev != NULL);
307
308 int rc = EOK;
309
310 if (dev->pipes[USB_HID_KBD_POLL_EP_NO].present) {
311 usb_log_debug("Found keyboard endpoint.\n");
312 // save the pipe index
313 hid_dev->poll_pipe_index = USB_HID_KBD_POLL_EP_NO;
314 } else if (dev->pipes[USB_HID_MOUSE_POLL_EP_NO].present) {
315 usb_log_debug("Found mouse endpoint.\n");
316 // save the pipe index
317 hid_dev->poll_pipe_index = USB_HID_MOUSE_POLL_EP_NO;
318 } else if (dev->pipes[USB_HID_GENERIC_POLL_EP_NO].present) {
319 usb_log_debug("Found generic HID endpoint.\n");
320 // save the pipe index
321 hid_dev->poll_pipe_index = USB_HID_GENERIC_POLL_EP_NO;
322 } else {
323 usb_log_error("None of supported endpoints found - probably"
324 " not a supported device.\n");
325 rc = ENOTSUP;
326 }
327
328 return rc;
329}
330
331/*----------------------------------------------------------------------------*/
332
333usb_hid_dev_t *usb_hid_new(void)
334{
335 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)calloc(1,
336 sizeof(usb_hid_dev_t));
337
338 if (hid_dev == NULL) {
339 usb_log_fatal("No memory!\n");
340 return NULL;
341 }
342
343 hid_dev->parser = (usb_hid_report_t *)(malloc(sizeof(
344 usb_hid_report_t)));
345 if (hid_dev->parser == NULL) {
346 usb_log_fatal("No memory!\n");
347 free(hid_dev);
348 return NULL;
349 }
350
351 hid_dev->poll_pipe_index = -1;
352
353 return hid_dev;
354}
355
356/*----------------------------------------------------------------------------*/
357
358int usb_hid_init(usb_hid_dev_t *hid_dev, usb_device_t *dev)
359{
360 int rc, i;
361
362 usb_log_debug("Initializing HID structure...\n");
363
364 if (hid_dev == NULL) {
365 usb_log_error("Failed to init HID structure: no structure given"
366 ".\n");
367 return EINVAL;
368 }
369
370 if (dev == NULL) {
371 usb_log_error("Failed to init HID structure: no USB device"
372 " given.\n");
373 return EINVAL;
374 }
375
376 /* The USB device should already be initialized, save it in structure */
377 hid_dev->usb_dev = dev;
378
379 rc = usb_hid_check_pipes(hid_dev, dev);
380 if (rc != EOK) {
381 //usb_hid_free(&hid_dev);
382 return rc;
383 }
384
385 /* Get the report descriptor and parse it. */
386 rc = usb_hid_process_report_descriptor(hid_dev->usb_dev,
387 hid_dev->parser);
388
389 bool fallback = false;
390
391 if (rc == EOK) {
392 // try to find subdrivers that may want to handle this device
393 rc = usb_hid_find_subdrivers(hid_dev);
394 if (rc != EOK || hid_dev->subdriver_count == 0) {
395 // try to fall back to the boot protocol if available
396 usb_log_info("No subdrivers found to handle this"
397 " device.\n");
398 fallback = true;
399 assert(hid_dev->subdrivers == NULL);
400 assert(hid_dev->subdriver_count == 0);
401 }
402 } else {
403 usb_log_error("Failed to parse Report descriptor.\n");
404 // try to fall back to the boot protocol if available
405 fallback = true;
406 }
407
408 // TODO: remove the mouse hack
409 if (hid_dev->poll_pipe_index == USB_HID_MOUSE_POLL_EP_NO ||
410 fallback) {
411 // fall back to boot protocol
412 switch (hid_dev->poll_pipe_index) {
413 case USB_HID_KBD_POLL_EP_NO:
414 usb_log_info("Falling back to kbd boot protocol.\n");
415 rc = usb_kbd_set_boot_protocol(hid_dev);
416 if (rc == EOK) {
417 rc = usb_hid_set_boot_kbd_subdriver(hid_dev);
418 }
419 break;
420 case USB_HID_MOUSE_POLL_EP_NO:
421 usb_log_info("Falling back to mouse boot protocol.\n");
422 rc = usb_mouse_set_boot_protocol(hid_dev);
423 if (rc == EOK) {
424 rc = usb_hid_set_boot_mouse_subdriver(hid_dev);
425 }
426 break;
427 default:
428 assert(hid_dev->poll_pipe_index
429 == USB_HID_GENERIC_POLL_EP_NO);
430
431 /* TODO: this has no meaning if the report descriptor
432 is not parsed */
433 usb_log_info("Falling back to generic HID driver.\n");
434 rc = usb_hid_set_generic_hid_subdriver(hid_dev);
435 }
436 }
437
438 if (rc != EOK) {
439 usb_log_error("No subdriver for handling this device could be"
440 " initialized: %s.\n", str_error(rc));
441 usb_log_debug("Subdriver count: %d\n",
442 hid_dev->subdriver_count);
443 //usb_hid_free(&hid_dev);
444 } else {
445 bool ok = false;
446
447 usb_log_debug("Subdriver count: %d\n",
448 hid_dev->subdriver_count);
449
450 for (i = 0; i < hid_dev->subdriver_count; ++i) {
451 if (hid_dev->subdrivers[i].init != NULL) {
452 usb_log_debug("Initializing subdriver %d.\n",i);
453 rc = hid_dev->subdrivers[i].init(hid_dev);
454 if (rc != EOK) {
455 usb_log_warning("Failed to initialize"
456 " HID subdriver structure.\n");
457 } else {
458 // at least one subdriver initialized
459 ok = true;
460 }
461 } else {
462 ok = true;
463 }
464 }
465
466 rc = (ok) ? EOK : -1; // what error to report
467 }
468
469 return rc;
470}
471
472/*----------------------------------------------------------------------------*/
473
474bool usb_hid_polling_callback(usb_device_t *dev, uint8_t *buffer,
475 size_t buffer_size, void *arg)
476{
477 int i;
478
479 if (dev == NULL || arg == NULL || buffer == NULL) {
480 usb_log_error("Missing arguments to polling callback.\n");
481 return false;
482 }
483
484 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
485
486 bool cont = false;
487
488 // continue if at least one of the subdrivers want to continue
489 for (i = 0; i < hid_dev->subdriver_count; ++i) {
490 if (hid_dev->subdrivers[i].poll != NULL
491 && hid_dev->subdrivers[i].poll(hid_dev, buffer,
492 buffer_size)) {
493 cont = true;
494 }
495 }
496
497 return cont;
498}
499
500/*----------------------------------------------------------------------------*/
501
502void usb_hid_polling_ended_callback(usb_device_t *dev, bool reason,
503 void *arg)
504{
505 int i;
506
507 if (dev == NULL || arg == NULL) {
508 return;
509 }
510
511 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
512
513 for (i = 0; i < hid_dev->subdriver_count; ++i) {
514 if (hid_dev->subdrivers[i].poll_end != NULL) {
515 hid_dev->subdrivers[i].poll_end(hid_dev, reason);
516 }
517 }
518
519 usb_hid_free(&hid_dev);
520}
521
522/*----------------------------------------------------------------------------*/
523
524const char *usb_hid_get_function_name(const usb_hid_dev_t *hid_dev)
525{
526 switch (hid_dev->poll_pipe_index) {
527 case USB_HID_KBD_POLL_EP_NO:
528 return HID_KBD_FUN_NAME;
529 break;
530 case USB_HID_MOUSE_POLL_EP_NO:
531 return HID_MOUSE_FUN_NAME;
532 break;
533 default:
534 return HID_GENERIC_FUN_NAME;
535 }
536}
537
538/*----------------------------------------------------------------------------*/
539
540const char *usb_hid_get_class_name(const usb_hid_dev_t *hid_dev)
541{
542 // this means that only boot protocol keyboards will be connected
543 // to the console; there is probably no better way to do this
544
545 switch (hid_dev->poll_pipe_index) {
546 case USB_HID_KBD_POLL_EP_NO:
547 return HID_KBD_CLASS_NAME;
548 break;
549 case USB_HID_MOUSE_POLL_EP_NO:
550 return HID_MOUSE_CLASS_NAME;
551 break;
552 default:
553 return HID_GENERIC_CLASS_NAME;
554 }
555}
556
557/*----------------------------------------------------------------------------*/
558
559void usb_hid_free(usb_hid_dev_t **hid_dev)
560{
561 int i;
562
563 if (hid_dev == NULL || *hid_dev == NULL) {
564 return;
565 }
566
567 usb_log_debug("Subdrivers: %p, subdriver count: %d\n",
568 (*hid_dev)->subdrivers, (*hid_dev)->subdriver_count);
569
570 assert((*hid_dev)->subdrivers != NULL
571 || (*hid_dev)->subdriver_count == 0);
572
573 for (i = 0; i < (*hid_dev)->subdriver_count; ++i) {
574 if ((*hid_dev)->subdrivers[i].deinit != NULL) {
575 (*hid_dev)->subdrivers[i].deinit(*hid_dev);
576 }
577 }
578
579 // free the subdrivers info
580 if ((*hid_dev)->subdrivers != NULL) {
581 free((*hid_dev)->subdrivers);
582 }
583
584 // destroy the parser
585 if ((*hid_dev)->parser != NULL) {
586 usb_hid_free_report((*hid_dev)->parser);
587 }
588
589 if ((*hid_dev)->report_desc != NULL) {
590 free((*hid_dev)->report_desc);
591 }
592
593 free(*hid_dev);
594 *hid_dev = NULL;
595}
596
597/**
598 * @}
599 */
Note: See TracBrowser for help on using the repository browser.