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

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

Fixes in mkbd and libusbhid.

  • Fixed initialization of report parser in mkbd.
  • Fixed usb_hid_process_report_descriptor() - added some parameters.
  • Property mode set to 100644
File size: 19.1 KB
RevLine 
[966acede]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>
[faa44e58]39#include <usb/hid/hid.h>
40#include <usb/hid/hidparser.h>
41#include <usb/hid/hidreport.h>
42#include <usb/hid/request.h>
[61257f4]43#include <errno.h>
[f76153ce]44#include <str_error.h>
[966acede]45
46#include "usbhid.h"
47
[61257f4]48#include "kbd/kbddev.h"
[dd10e07]49#include "generic/hiddev.h"
[e9f0348]50#include "mouse/mousedev.h"
[62bd8d3]51#include "subdrivers.h"
[61257f4]52
[966acede]53/*----------------------------------------------------------------------------*/
54
55/* Array of endpoints expected on the device, NULL terminated. */
[fec47d4]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,
[61257f4]59 &usb_hid_generic_poll_endpoint_description,
[966acede]60 NULL
61};
62
[f76153ce]63static const int USB_HID_MAX_SUBDRIVERS = 10;
64
[dd3eda2]65static fibril_local bool report_received;
66
[966acede]67/*----------------------------------------------------------------------------*/
68
[60c0573]69static int usb_hid_set_boot_kbd_subdriver(usb_hid_dev_t *hid_dev)
[61257f4]70{
[e7df6cd]71 assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
[61257f4]72
[60c0573]73 hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(
74 sizeof(usb_hid_subdriver_t));
75 if (hid_dev->subdrivers == NULL) {
76 return ENOMEM;
[61257f4]77 }
78
[60c0573]79 // set the init callback
80 hid_dev->subdrivers[0].init = usb_kbd_init;
81
82 // set the polling callback
83 hid_dev->subdrivers[0].poll = usb_kbd_polling_callback;
84
85 // set the polling ended callback
86 hid_dev->subdrivers[0].poll_end = NULL;
87
88 // set the deinit callback
89 hid_dev->subdrivers[0].deinit = usb_kbd_deinit;
90
91 // set subdriver count
92 hid_dev->subdriver_count = 1;
93
94 return EOK;
95}
96
97/*----------------------------------------------------------------------------*/
98
99static int usb_hid_set_boot_mouse_subdriver(usb_hid_dev_t *hid_dev)
100{
[e7df6cd]101 assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
[60c0573]102
103 hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(
104 sizeof(usb_hid_subdriver_t));
105 if (hid_dev->subdrivers == NULL) {
106 return ENOMEM;
[61257f4]107 }
108
[60c0573]109 // set the init callback
110 hid_dev->subdrivers[0].init = usb_mouse_init;
111
112 // set the polling callback
113 hid_dev->subdrivers[0].poll = usb_mouse_polling_callback;
114
115 // set the polling ended callback
116 hid_dev->subdrivers[0].poll_end = NULL;
117
118 // set the deinit callback
119 hid_dev->subdrivers[0].deinit = usb_mouse_deinit;
120
121 // set subdriver count
122 hid_dev->subdriver_count = 1;
123
124 return EOK;
125}
126
127/*----------------------------------------------------------------------------*/
128
129static int usb_hid_set_generic_hid_subdriver(usb_hid_dev_t *hid_dev)
130{
[e7df6cd]131 assert(hid_dev != NULL && hid_dev->subdriver_count == 0);
[60c0573]132
133 hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(
134 sizeof(usb_hid_subdriver_t));
135 if (hid_dev->subdrivers == NULL) {
136 return ENOMEM;
137 }
138
139 // set the init callback
[3facf63a]140 hid_dev->subdrivers[0].init = usb_generic_hid_init;
[60c0573]141
142 // set the polling callback
143 hid_dev->subdrivers[0].poll = usb_generic_hid_polling_callback;
144
145 // set the polling ended callback
146 hid_dev->subdrivers[0].poll_end = NULL;
147
148 // set the deinit callback
149 hid_dev->subdrivers[0].deinit = NULL;
150
[f76153ce]151 // set subdriver count
152 hid_dev->subdriver_count = 1;
153
[60c0573]154 return EOK;
155}
156
157/*----------------------------------------------------------------------------*/
158
[f76153ce]159static bool usb_hid_ids_match(usb_hid_dev_t *hid_dev,
160 const usb_hid_subdriver_mapping_t *mapping)
161{
[36f737a]162 assert(hid_dev != NULL);
163 assert(hid_dev->usb_dev != NULL);
164
165 return (hid_dev->usb_dev->descriptors.device.vendor_id
166 == mapping->vendor_id
167 && hid_dev->usb_dev->descriptors.device.product_id
168 == mapping->product_id);
[f76153ce]169}
170
171/*----------------------------------------------------------------------------*/
172
173static bool usb_hid_path_matches(usb_hid_dev_t *hid_dev,
[e3b5129]174 const usb_hid_subdriver_mapping_t *mapping)
[f76153ce]175{
176 assert(hid_dev != NULL);
[e3b5129]177 assert(mapping != NULL);
[f76153ce]178
179 usb_hid_report_path_t *usage_path = usb_hid_report_path();
180 if (usage_path == NULL) {
[1cbb4b7]181 usb_log_debug("Failed to create usage path.\n");
[f76153ce]182 return false;
183 }
[777e336]184 int i = 0;
[e3b5129]185 while (mapping->usage_path[i].usage != 0
186 || mapping->usage_path[i].usage_page != 0) {
[f76153ce]187 if (usb_hid_report_path_append_item(usage_path,
[e3b5129]188 mapping->usage_path[i].usage_page,
189 mapping->usage_path[i].usage) != EOK) {
[1cbb4b7]190 usb_log_debug("Failed to append to usage path.\n");
[f76153ce]191 usb_hid_report_path_free(usage_path);
192 return false;
193 }
[777e336]194 ++i;
[f76153ce]195 }
196
[e3b5129]197 if (mapping->report_id >= 0) {
198 usb_hid_report_path_set_report_id(usage_path,
199 mapping->report_id);
[f76153ce]200 }
201
[e60436b]202 assert(hid_dev->report != NULL);
[1cbb4b7]203
[e3b5129]204 usb_log_debug("Compare flags: %d\n", mapping->compare);
[3a6e423]205// size_t size = usb_hid_report_size(hid_dev->report, 0,
206// USB_HID_REPORT_TYPE_INPUT);
207 size_t size = 0;
208 usb_hid_report_field_t *field = usb_hid_report_get_sibling (hid_dev->report,
209 NULL, usage_path, mapping->compare, USB_HID_REPORT_TYPE_INPUT);
210 while(field != NULL) {
211 size++;
212 field = usb_hid_report_get_sibling (hid_dev->report,
213 field, usage_path, mapping->compare,
214 USB_HID_REPORT_TYPE_INPUT);
215 }
[f76153ce]216
[3a6e423]217 usb_log_debug("Size of the input report: %zuB\n", size);
[f76153ce]218 usb_hid_report_path_free(usage_path);
219
[1cbb4b7]220 return (size > 0);
[f76153ce]221}
222
223/*----------------------------------------------------------------------------*/
224
225static int usb_hid_save_subdrivers(usb_hid_dev_t *hid_dev,
226 const usb_hid_subdriver_t **subdrivers, int count)
[60c0573]227{
[f76153ce]228 int i;
229
230 if (count <= 0) {
231 hid_dev->subdriver_count = 0;
232 hid_dev->subdrivers = NULL;
233 return EOK;
234 }
235
[78bfae9]236 // add one generic HID subdriver per device
237
238 hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc((count + 1) *
[f76153ce]239 sizeof(usb_hid_subdriver_t));
240 if (hid_dev->subdrivers == NULL) {
241 return ENOMEM;
242 }
243
244 for (i = 0; i < count; ++i) {
245 hid_dev->subdrivers[i].init = subdrivers[i]->init;
246 hid_dev->subdrivers[i].deinit = subdrivers[i]->deinit;
247 hid_dev->subdrivers[i].poll = subdrivers[i]->poll;
248 hid_dev->subdrivers[i].poll_end = subdrivers[i]->poll_end;
249 }
250
[78bfae9]251 hid_dev->subdrivers[count].init = usb_generic_hid_init;
252 hid_dev->subdrivers[count].poll = usb_generic_hid_polling_callback;
253 hid_dev->subdrivers[count].deinit = NULL;
254 hid_dev->subdrivers[count].poll_end = NULL;
255
256 hid_dev->subdriver_count = count + 1;
[f76153ce]257
[60c0573]258 return EOK;
[61257f4]259}
[966acede]260
261/*----------------------------------------------------------------------------*/
262
[f76153ce]263static int usb_hid_find_subdrivers(usb_hid_dev_t *hid_dev)
264{
[e7df6cd]265 assert(hid_dev != NULL);
266
[f76153ce]267 const usb_hid_subdriver_t *subdrivers[USB_HID_MAX_SUBDRIVERS];
268
269 int i = 0, count = 0;
270 const usb_hid_subdriver_mapping_t *mapping = &usb_hid_subdrivers[i];
[4bb9fd2]271
272 bool ids_matched;
273 bool matched;
[f76153ce]274
275 while (count < USB_HID_MAX_SUBDRIVERS &&
276 (mapping->usage_path != NULL
[d0a6e54]277 || mapping->vendor_id >= 0 || mapping->product_id >= 0)) {
[f76153ce]278 // check the vendor & product ID
[d0a6e54]279 if (mapping->vendor_id >= 0 && mapping->product_id < 0) {
280 usb_log_warning("Missing Product ID for Vendor ID %d\n",
[f76153ce]281 mapping->vendor_id);
282 return EINVAL;
283 }
[d0a6e54]284 if (mapping->product_id >= 0 && mapping->vendor_id < 0) {
285 usb_log_warning("Missing Vendor ID for Product ID %d\n",
[f76153ce]286 mapping->product_id);
287 return EINVAL;
288 }
289
[4bb9fd2]290 ids_matched = false;
291 matched = false;
292
[d0a6e54]293 if (mapping->vendor_id >= 0) {
294 assert(mapping->product_id >= 0);
[777e336]295 usb_log_debug("Comparing device against vendor ID %u"
296 " and product ID %u.\n", mapping->vendor_id,
[f76153ce]297 mapping->product_id);
298 if (usb_hid_ids_match(hid_dev, mapping)) {
[4bb9fd2]299 usb_log_debug("IDs matched.\n");
300 ids_matched = true;
[f76153ce]301 }
302 }
303
304 if (mapping->usage_path != NULL) {
[1cbb4b7]305 usb_log_debug("Comparing device against usage path.\n");
[e3b5129]306 if (usb_hid_path_matches(hid_dev, mapping)) {
[4bb9fd2]307 // does not matter if IDs were matched
308 matched = true;
[f76153ce]309 }
[4bb9fd2]310 } else {
311 // matched only if IDs were matched and there is no path
312 matched = ids_matched;
[f76153ce]313 }
[4bb9fd2]314
315 if (matched) {
[d1fb591]316 usb_log_debug("Subdriver matched.\n");
[4bb9fd2]317 subdrivers[count++] = &mapping->subdriver;
[f76153ce]318 }
[4bb9fd2]319
[f76153ce]320 mapping = &usb_hid_subdrivers[++i];
321 }
322
323 // we have all subdrivers determined, save them into the hid device
324 return usb_hid_save_subdrivers(hid_dev, subdrivers, count);
325}
326
327/*----------------------------------------------------------------------------*/
328
[61257f4]329static int usb_hid_check_pipes(usb_hid_dev_t *hid_dev, usb_device_t *dev)
[966acede]330{
[e7df6cd]331 assert(hid_dev != NULL && dev != NULL);
332
[f76153ce]333 int rc = EOK;
[60c0573]334
[61257f4]335 if (dev->pipes[USB_HID_KBD_POLL_EP_NO].present) {
336 usb_log_debug("Found keyboard endpoint.\n");
[60c0573]337 // save the pipe index
[61257f4]338 hid_dev->poll_pipe_index = USB_HID_KBD_POLL_EP_NO;
339 } else if (dev->pipes[USB_HID_MOUSE_POLL_EP_NO].present) {
340 usb_log_debug("Found mouse endpoint.\n");
[60c0573]341 // save the pipe index
[61257f4]342 hid_dev->poll_pipe_index = USB_HID_MOUSE_POLL_EP_NO;
343 } else if (dev->pipes[USB_HID_GENERIC_POLL_EP_NO].present) {
344 usb_log_debug("Found generic HID endpoint.\n");
[60c0573]345 // save the pipe index
[61257f4]346 hid_dev->poll_pipe_index = USB_HID_GENERIC_POLL_EP_NO;
347 } else {
[60c0573]348 usb_log_error("None of supported endpoints found - probably"
[61257f4]349 " not a supported device.\n");
[60c0573]350 rc = ENOTSUP;
[61257f4]351 }
352
[60c0573]353 return rc;
[61257f4]354}
355
356/*----------------------------------------------------------------------------*/
357
[d1fb591]358static int usb_hid_init_report(usb_hid_dev_t *hid_dev)
359{
360 assert(hid_dev != NULL && hid_dev->report != NULL);
361
362 uint8_t report_id = 0;
363 size_t size = usb_hid_report_size(hid_dev->report, report_id,
364 USB_HID_REPORT_TYPE_INPUT);
365
366 size_t max_size = 0;
367
368 while (size > 0) {
369 max_size = (size > max_size) ? size : max_size;
370 size = usb_hid_report_size(hid_dev->report, report_id,
371 USB_HID_REPORT_TYPE_INPUT);
372 ++report_id;
373 }
374
375 usb_log_debug("Max size of input report: %zu\n", max_size);
376
377 hid_dev->max_input_report_size = max_size;
378 assert(hid_dev->input_report == NULL);
379
380 hid_dev->input_report = malloc(max_size);
381 if (hid_dev->input_report == NULL) {
382 return ENOMEM;
383 }
384 memset(hid_dev->input_report, 0, max_size);
385
386 return EOK;
387}
388
389/*----------------------------------------------------------------------------*/
390
[60c0573]391usb_hid_dev_t *usb_hid_new(void)
392{
393 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)calloc(1,
394 sizeof(usb_hid_dev_t));
395
396 if (hid_dev == NULL) {
397 usb_log_fatal("No memory!\n");
398 return NULL;
399 }
400
[e60436b]401 hid_dev->report = (usb_hid_report_t *)(malloc(sizeof(
[e50cd7f]402 usb_hid_report_t)));
[e60436b]403 if (hid_dev->report == NULL) {
[60c0573]404 usb_log_fatal("No memory!\n");
405 free(hid_dev);
406 return NULL;
407 }
408
[f76153ce]409 hid_dev->poll_pipe_index = -1;
410
[60c0573]411 return hid_dev;
412}
413
414/*----------------------------------------------------------------------------*/
415
[61257f4]416int usb_hid_init(usb_hid_dev_t *hid_dev, usb_device_t *dev)
417{
[60c0573]418 int rc, i;
[61257f4]419
420 usb_log_debug("Initializing HID structure...\n");
421
422 if (hid_dev == NULL) {
423 usb_log_error("Failed to init HID structure: no structure given"
424 ".\n");
425 return EINVAL;
426 }
427
428 if (dev == NULL) {
429 usb_log_error("Failed to init HID structure: no USB device"
430 " given.\n");
431 return EINVAL;
432 }
433
434 /* The USB device should already be initialized, save it in structure */
435 hid_dev->usb_dev = dev;
436
437 rc = usb_hid_check_pipes(hid_dev, dev);
438 if (rc != EOK) {
[aaf6155]439 //usb_hid_free(&hid_dev);
[61257f4]440 return rc;
441 }
[e50cd7f]442
[f76153ce]443 /* Get the report descriptor and parse it. */
444 rc = usb_hid_process_report_descriptor(hid_dev->usb_dev,
[fa8d346]445 hid_dev->report, &hid_dev->report_desc, &hid_dev->report_desc_size);
[f76153ce]446
447 bool fallback = false;
448
449 if (rc == EOK) {
450 // try to find subdrivers that may want to handle this device
451 rc = usb_hid_find_subdrivers(hid_dev);
452 if (rc != EOK || hid_dev->subdriver_count == 0) {
453 // try to fall back to the boot protocol if available
454 usb_log_info("No subdrivers found to handle this"
455 " device.\n");
456 fallback = true;
[aaf6155]457 assert(hid_dev->subdrivers == NULL);
458 assert(hid_dev->subdriver_count == 0);
[f76153ce]459 }
460 } else {
461 usb_log_error("Failed to parse Report descriptor.\n");
462 // try to fall back to the boot protocol if available
463 fallback = true;
464 }
465
[7bb8036]466 if (fallback) {
[f76153ce]467 // fall back to boot protocol
468 switch (hid_dev->poll_pipe_index) {
469 case USB_HID_KBD_POLL_EP_NO:
470 usb_log_info("Falling back to kbd boot protocol.\n");
471 rc = usb_kbd_set_boot_protocol(hid_dev);
472 if (rc == EOK) {
473 rc = usb_hid_set_boot_kbd_subdriver(hid_dev);
[60c0573]474 }
[f76153ce]475 break;
476 case USB_HID_MOUSE_POLL_EP_NO:
477 usb_log_info("Falling back to mouse boot protocol.\n");
478 rc = usb_mouse_set_boot_protocol(hid_dev);
479 if (rc == EOK) {
480 rc = usb_hid_set_boot_mouse_subdriver(hid_dev);
481 }
482 break;
483 default:
484 assert(hid_dev->poll_pipe_index
485 == USB_HID_GENERIC_POLL_EP_NO);
486
487 /* TODO: this has no meaning if the report descriptor
488 is not parsed */
489 usb_log_info("Falling back to generic HID driver.\n");
490 rc = usb_hid_set_generic_hid_subdriver(hid_dev);
[e9f0348]491 }
[61257f4]492 }
493
[f76153ce]494 if (rc != EOK) {
495 usb_log_error("No subdriver for handling this device could be"
496 " initialized: %s.\n", str_error(rc));
[aaf6155]497 usb_log_debug("Subdriver count: %d\n",
498 hid_dev->subdriver_count);
499 //usb_hid_free(&hid_dev);
[f76153ce]500 } else {
501 bool ok = false;
502
503 usb_log_debug("Subdriver count: %d\n",
504 hid_dev->subdriver_count);
505
506 for (i = 0; i < hid_dev->subdriver_count; ++i) {
507 if (hid_dev->subdrivers[i].init != NULL) {
508 usb_log_debug("Initializing subdriver %d.\n",i);
[65b458c4]509 rc = hid_dev->subdrivers[i].init(hid_dev,
510 &hid_dev->subdrivers[i].data);
[f76153ce]511 if (rc != EOK) {
512 usb_log_warning("Failed to initialize"
513 " HID subdriver structure.\n");
514 } else {
515 // at least one subdriver initialized
516 ok = true;
517 }
518 } else {
519 ok = true;
520 }
521 }
522
523 rc = (ok) ? EOK : -1; // what error to report
524 }
525
[d1fb591]526 // save max input report size and allocate space for the report
527 rc = usb_hid_init_report(hid_dev);
528 if (rc != EOK) {
529 usb_log_error("Failed to initialize input report buffer.\n");
530 }
531
[61257f4]532 return rc;
533}
534
535/*----------------------------------------------------------------------------*/
536
[60c0573]537bool usb_hid_polling_callback(usb_device_t *dev, uint8_t *buffer,
538 size_t buffer_size, void *arg)
539{
540 int i;
541
542 if (dev == NULL || arg == NULL || buffer == NULL) {
543 usb_log_error("Missing arguments to polling callback.\n");
544 return false;
545 }
546
547 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
548
[d1fb591]549// int allocated = (hid_dev->input_report != NULL);
550 assert(hid_dev->input_report != NULL);
551 assert(hid_dev->max_input_report_size >= buffer_size);
[31cfee16]552
[d1fb591]553// if (/*!allocated*/
554// /*|| *//*hid_dev->input_report_size < buffer_size*/) {
555// uint8_t *input_old = hid_dev->input_report;
556// uint8_t *input_new = (uint8_t *)malloc(buffer_size);
[31cfee16]557
[d1fb591]558// if (input_new == NULL) {
559// usb_log_error("Failed to allocate space for input "
560// "buffer. This event may not be reported\n");
561// memset(hid_dev->input_report, 0,
562// hid_dev->input_report_size);
563// } else {
564// memcpy(input_new, input_old,
565// hid_dev->input_report_size);
566// hid_dev->input_report = input_new;
567// if (allocated) {
568// free(input_old);
569// }
570// usb_hid_new_report();
571// }
572// }
[31cfee16]573
574 /*! @todo This should probably be atomic. */
575 memcpy(hid_dev->input_report, buffer, buffer_size);
576 hid_dev->input_report_size = buffer_size;
[d1fb591]577 usb_hid_new_report();
[31cfee16]578
[60c0573]579 bool cont = false;
580
581 // continue if at least one of the subdrivers want to continue
582 for (i = 0; i < hid_dev->subdriver_count; ++i) {
583 if (hid_dev->subdrivers[i].poll != NULL
[65b458c4]584 && hid_dev->subdrivers[i].poll(hid_dev,
585 hid_dev->subdrivers[i].data, buffer, buffer_size)) {
[60c0573]586 cont = true;
587 }
588 }
589
590 return cont;
591}
592
593/*----------------------------------------------------------------------------*/
594
[61257f4]595void usb_hid_polling_ended_callback(usb_device_t *dev, bool reason,
596 void *arg)
597{
[60c0573]598 int i;
599
[61257f4]600 if (dev == NULL || arg == NULL) {
601 return;
602 }
603
604 usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
605
[60c0573]606 for (i = 0; i < hid_dev->subdriver_count; ++i) {
607 if (hid_dev->subdrivers[i].poll_end != NULL) {
[65b458c4]608 hid_dev->subdrivers[i].poll_end(hid_dev,
609 hid_dev->subdrivers[i].data, reason);
[60c0573]610 }
611 }
612
[61257f4]613 usb_hid_free(&hid_dev);
614}
615
616/*----------------------------------------------------------------------------*/
617
[31cfee16]618//const char *usb_hid_get_function_name(const usb_hid_dev_t *hid_dev)
619//{
620// switch (hid_dev->poll_pipe_index) {
621// case USB_HID_KBD_POLL_EP_NO:
622// return HID_KBD_FUN_NAME;
623// break;
624// case USB_HID_MOUSE_POLL_EP_NO:
625// return HID_MOUSE_FUN_NAME;
626// break;
627// default:
628// return HID_GENERIC_FUN_NAME;
629// }
630//}
[61257f4]631
632/*----------------------------------------------------------------------------*/
633
[31cfee16]634//const char *usb_hid_get_class_name(const usb_hid_dev_t *hid_dev)
635//{
636// // this means that only boot protocol keyboards will be connected
637// // to the console; there is probably no better way to do this
638
639// switch (hid_dev->poll_pipe_index) {
640// case USB_HID_KBD_POLL_EP_NO:
641// return HID_KBD_CLASS_NAME;
642// break;
643// case USB_HID_MOUSE_POLL_EP_NO:
644// return HID_MOUSE_CLASS_NAME;
645// break;
646// default:
647// return HID_GENERIC_CLASS_NAME;
648// }
649//}
[61257f4]650
651/*----------------------------------------------------------------------------*/
652
[dd3eda2]653void usb_hid_new_report(void)
654{
655 report_received = false;
656}
657
658/*----------------------------------------------------------------------------*/
659
660void usb_hid_report_received(void)
661{
662 report_received = true;
663}
664
665/*----------------------------------------------------------------------------*/
666
667bool usb_hid_report_ready(void)
668{
669 return !report_received;
670}
671
672/*----------------------------------------------------------------------------*/
673
[61257f4]674void usb_hid_free(usb_hid_dev_t **hid_dev)
675{
[60c0573]676 int i;
677
[61257f4]678 if (hid_dev == NULL || *hid_dev == NULL) {
679 return;
680 }
681
[aaf6155]682 usb_log_debug("Subdrivers: %p, subdriver count: %d\n",
683 (*hid_dev)->subdrivers, (*hid_dev)->subdriver_count);
684
[f76153ce]685 assert((*hid_dev)->subdrivers != NULL
686 || (*hid_dev)->subdriver_count == 0);
687
[60c0573]688 for (i = 0; i < (*hid_dev)->subdriver_count; ++i) {
689 if ((*hid_dev)->subdrivers[i].deinit != NULL) {
[65b458c4]690 (*hid_dev)->subdrivers[i].deinit(*hid_dev,
691 (*hid_dev)->subdrivers[i].data);
[60c0573]692 }
[61257f4]693 }
[f76153ce]694
695 // free the subdrivers info
696 if ((*hid_dev)->subdrivers != NULL) {
[1cbb4b7]697 free((*hid_dev)->subdrivers);
[f76153ce]698 }
[61257f4]699
700 // destroy the parser
[e60436b]701 if ((*hid_dev)->report != NULL) {
702 usb_hid_free_report((*hid_dev)->report);
[61257f4]703 }
704
705 if ((*hid_dev)->report_desc != NULL) {
706 free((*hid_dev)->report_desc);
707 }
708
709 free(*hid_dev);
710 *hid_dev = NULL;
[966acede]711}
712
713/**
714 * @}
715 */
Note: See TracBrowser for help on using the repository browser.