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

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

usbhid: Remove redundant array size enums.

  • 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 if (dev->pipes[USB_HID_KBD_POLL_EP_NO].present) {
304 usb_log_debug("Found keyboard endpoint.\n");
305 // save the pipe index
306 hid_dev->poll_pipe_index = USB_HID_KBD_POLL_EP_NO;
307 } else if (dev->pipes[USB_HID_MOUSE_POLL_EP_NO].present) {
308 usb_log_debug("Found mouse endpoint.\n");
309 // save the pipe index
310 hid_dev->poll_pipe_index = USB_HID_MOUSE_POLL_EP_NO;
311 } else if (dev->pipes[USB_HID_GENERIC_POLL_EP_NO].present) {
312 usb_log_debug("Found generic HID endpoint.\n");
313 // save the pipe index
314 hid_dev->poll_pipe_index = USB_HID_GENERIC_POLL_EP_NO;
315 } else {
316 usb_log_error("None of supported endpoints found - probably"
317 " not a supported device.\n");
318 return ENOTSUP;
319 }
320
321 return EOK;
322}
323
324/*----------------------------------------------------------------------------*/
325
326static int usb_hid_init_report(usb_hid_dev_t *hid_dev)
327{
328 assert(hid_dev != NULL);
329
330 uint8_t report_id = 0;
331 size_t max_size = 0;
332
333 do {
334 usb_log_debug("Getting size of the report.\n");
335 const size_t size =
336 usb_hid_report_byte_size(&hid_dev->report, report_id,
337 USB_HID_REPORT_TYPE_INPUT);
338 usb_log_debug("Report ID: %u, size: %zu\n", report_id, size);
339 max_size = (size > max_size) ? size : max_size;
340 usb_log_debug("Getting next report ID\n");
341 report_id = usb_hid_get_next_report_id(&hid_dev->report,
342 report_id, USB_HID_REPORT_TYPE_INPUT);
343 } while (report_id != 0);
344
345 usb_log_debug("Max size of input report: %zu\n", max_size);
346
347 assert(hid_dev->input_report == NULL);
348
349 hid_dev->input_report = calloc(1, max_size);
350 if (hid_dev->input_report == NULL) {
351 return ENOMEM;
352 }
353 hid_dev->max_input_report_size = max_size;
354
355 return EOK;
356}
357
358/*----------------------------------------------------------------------------*/
359/*
360 * This functions initializes required structures from the device's descriptors
361 * and starts new fibril for polling the keyboard for events and another one for
362 * handling auto-repeat of keys.
363 *
364 * During initialization, the keyboard is switched into boot protocol, the idle
365 * rate is set to 0 (infinity), resulting in the keyboard only reporting event
366 * when a key is pressed or released. Finally, the LED lights are turned on
367 * according to the default setup of lock keys.
368 *
369 * @note By default, the keyboards is initialized with Num Lock turned on and
370 * other locks turned off.
371 *
372 * @param hid_dev Device to initialize, non-NULL.
373 * @param dev USB device, non-NULL.
374 * @return Error code.
375 */
376int usb_hid_init(usb_hid_dev_t *hid_dev, usb_device_t *dev)
377{
378 assert(hid_dev);
379 assert(dev);
380
381 usb_log_debug("Initializing HID structure...\n");
382
383 usb_hid_report_init(&hid_dev->report);
384
385 /* The USB device should already be initialized, save it in structure */
386 hid_dev->usb_dev = dev;
387 hid_dev->poll_pipe_index = -1;
388
389 int rc = usb_hid_check_pipes(hid_dev, dev);
390 if (rc != EOK) {
391 return rc;
392 }
393
394 /* Get the report descriptor and parse it. */
395 rc = usb_hid_process_report_descriptor(hid_dev->usb_dev,
396 &hid_dev->report, &hid_dev->report_desc, &hid_dev->report_desc_size);
397
398 bool fallback = false;
399
400 if (rc == EOK) {
401 // try to find subdrivers that may want to handle this device
402 rc = usb_hid_find_subdrivers(hid_dev);
403 if (rc != EOK || hid_dev->subdriver_count == 0) {
404 // try to fall back to the boot protocol if available
405 usb_log_info("No subdrivers found to handle this"
406 " device.\n");
407 fallback = true;
408 assert(hid_dev->subdrivers == NULL);
409 assert(hid_dev->subdriver_count == 0);
410 }
411 } else {
412 usb_log_error("Failed to parse Report descriptor.\n");
413 // try to fall back to the boot protocol if available
414 fallback = true;
415 }
416
417 if (fallback) {
418 // fall back to boot protocol
419 switch (hid_dev->poll_pipe_index) {
420 case USB_HID_KBD_POLL_EP_NO:
421 usb_log_info("Falling back to kbd boot protocol.\n");
422 rc = usb_kbd_set_boot_protocol(hid_dev);
423 if (rc == EOK) {
424 rc = usb_hid_set_boot_kbd_subdriver(hid_dev);
425 }
426 break;
427 case USB_HID_MOUSE_POLL_EP_NO:
428 usb_log_info("Falling back to mouse boot protocol.\n");
429 rc = usb_mouse_set_boot_protocol(hid_dev);
430 if (rc == EOK) {
431 rc = usb_hid_set_boot_mouse_subdriver(hid_dev);
432 }
433 break;
434 default:
435 assert(hid_dev->poll_pipe_index
436 == USB_HID_GENERIC_POLL_EP_NO);
437
438 usb_log_info("Falling back to generic HID driver.\n");
439 rc = usb_hid_set_generic_hid_subdriver(hid_dev);
440 }
441 }
442
443 if (rc != EOK) {
444 usb_log_error("No subdriver for handling this device could be"
445 " initialized: %s.\n", str_error(rc));
446 usb_log_debug("Subdriver count: %d\n",
447 hid_dev->subdriver_count);
448 } else {
449 bool ok = false;
450
451 usb_log_debug("Subdriver count: %d\n",
452 hid_dev->subdriver_count);
453
454 for (int i = 0; i < hid_dev->subdriver_count; ++i) {
455 if (hid_dev->subdrivers[i].init != NULL) {
456 usb_log_debug("Initializing subdriver %d.\n",i);
457 rc = hid_dev->subdrivers[i].init(hid_dev,
458 &hid_dev->subdrivers[i].data);
459 if (rc != EOK) {
460 usb_log_warning("Failed to initialize"
461 " HID subdriver structure.\n");
462 } else {
463 // at least one subdriver initialized
464 ok = true;
465 }
466 } else {
467 ok = true;
468 }
469 }
470
471 rc = (ok) ? EOK : -1; // what error to report
472 }
473
474
475 if (rc == EOK) {
476 // save max input report size and allocate space for the report
477 rc = usb_hid_init_report(hid_dev);
478 if (rc != EOK) {
479 usb_log_error("Failed to initialize input report buffer"
480 ".\n");
481 }
482 }
483
484 return rc;
485}
486
487/*----------------------------------------------------------------------------*/
488
489bool usb_hid_polling_callback(usb_device_t *dev, uint8_t *buffer,
490 size_t buffer_size, void *arg)
491{
492 if (dev == NULL || arg == NULL || buffer == NULL) {
493 usb_log_error("Missing arguments to polling callback.\n");
494 return false;
495 }
496 usb_hid_dev_t *hid_dev = arg;
497
498 assert(hid_dev->input_report != NULL);
499
500 usb_log_debug("New data [%zu/%zu]: %s\n", buffer_size,
501 hid_dev->max_input_report_size,
502 usb_debug_str_buffer(buffer, buffer_size, 0));
503
504 if (hid_dev->max_input_report_size >= buffer_size) {
505 /*! @todo This should probably be atomic. */
506 memcpy(hid_dev->input_report, buffer, buffer_size);
507 hid_dev->input_report_size = buffer_size;
508 usb_hid_new_report(hid_dev);
509 }
510
511 /* Parse the input report */
512 const int rc = usb_hid_parse_report(
513 &hid_dev->report, buffer, buffer_size, &hid_dev->report_id);
514 if (rc != EOK) {
515 usb_log_warning("Error in usb_hid_parse_report():"
516 "%s\n", str_error(rc));
517 }
518
519 bool cont = false;
520 /* Continue if at least one of the subdrivers want to continue */
521 for (int i = 0; i < hid_dev->subdriver_count; ++i) {
522 if (hid_dev->subdrivers[i].poll != NULL) {
523 cont = cont || hid_dev->subdrivers[i].poll(
524 hid_dev, hid_dev->subdrivers[i].data);
525 }
526 }
527
528 return cont;
529}
530
531/*----------------------------------------------------------------------------*/
532
533void usb_hid_polling_ended_callback(usb_device_t *dev, bool reason, void *arg)
534{
535 assert(dev);
536 assert(arg);
537
538 usb_hid_dev_t *hid_dev = arg;
539
540 for (int i = 0; i < hid_dev->subdriver_count; ++i) {
541 if (hid_dev->subdrivers[i].poll_end != NULL) {
542 hid_dev->subdrivers[i].poll_end(
543 hid_dev, hid_dev->subdrivers[i].data, reason);
544 }
545 }
546
547 hid_dev->running = false;
548}
549
550/*----------------------------------------------------------------------------*/
551
552void usb_hid_new_report(usb_hid_dev_t *hid_dev)
553{
554 ++hid_dev->report_nr;
555}
556
557/*----------------------------------------------------------------------------*/
558
559int usb_hid_report_number(const usb_hid_dev_t *hid_dev)
560{
561 return hid_dev->report_nr;
562}
563
564/*----------------------------------------------------------------------------*/
565void usb_hid_deinit(usb_hid_dev_t *hid_dev)
566{
567 assert(hid_dev);
568 assert(hid_dev->subdrivers != NULL || hid_dev->subdriver_count == 0);
569
570
571 usb_log_debug("Subdrivers: %p, subdriver count: %d\n",
572 hid_dev->subdrivers, hid_dev->subdriver_count);
573
574 for (int i = 0; i < hid_dev->subdriver_count; ++i) {
575 if (hid_dev->subdrivers[i].deinit != NULL) {
576 hid_dev->subdrivers[i].deinit(hid_dev,
577 hid_dev->subdrivers[i].data);
578 }
579 }
580
581 /* Free allocated structures */
582 free(hid_dev->subdrivers);
583 free(hid_dev->report_desc);
584
585 /* Destroy the parser */
586 usb_hid_report_deinit(&hid_dev->report);
587
588}
589
590/**
591 * @}
592 */
Note: See TracBrowser for help on using the repository browser.