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

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

usbhid: Merge usb_hid_device_add and usb_hid_try_add_device.

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