source: mainline/uspace/drv/hid/usbhid/usbhid.c@ 17c1d9db

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 17c1d9db was 17c1d9db, checked in by Petr Manek <petr.manek@…>, 8 years ago

usbhid: no need to expose polling callbacks

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