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. */
|
---|
56 | usb_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 |
|
---|
63 | static const int USB_HID_MAX_SUBDRIVERS = 10;
|
---|
64 |
|
---|
65 | /*----------------------------------------------------------------------------*/
|
---|
66 |
|
---|
67 | static 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 |
|
---|
97 | static 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 |
|
---|
127 | static 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 |
|
---|
157 | static 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 |
|
---|
165 | static bool usb_hid_path_matches(usb_hid_dev_t *hid_dev,
|
---|
166 | const usb_hid_subdriver_usage_t *path, int compare)
|
---|
167 | {
|
---|
168 | assert(hid_dev != NULL);
|
---|
169 | assert(path != 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 (path[i].usage != 0 || path[i].usage_page != 0) {
|
---|
178 | if (usb_hid_report_path_append_item(usage_path,
|
---|
179 | path[i].usage_page, path[i].usage) != EOK) {
|
---|
180 | usb_log_debug("Failed to append to usage path.\n");
|
---|
181 | usb_hid_report_path_free(usage_path);
|
---|
182 | return false;
|
---|
183 | }
|
---|
184 | ++i;
|
---|
185 | }
|
---|
186 |
|
---|
187 | assert(hid_dev->parser != NULL);
|
---|
188 |
|
---|
189 | usb_log_debug("Compare flags: %d\n", compare);
|
---|
190 | size_t size = usb_hid_report_input_length(hid_dev->parser, usage_path,
|
---|
191 | compare);
|
---|
192 | usb_log_debug("Size of the input report: %d\n", size);
|
---|
193 |
|
---|
194 | usb_hid_report_path_free(usage_path);
|
---|
195 |
|
---|
196 | return (size > 0);
|
---|
197 | }
|
---|
198 |
|
---|
199 | /*----------------------------------------------------------------------------*/
|
---|
200 |
|
---|
201 | static int usb_hid_save_subdrivers(usb_hid_dev_t *hid_dev,
|
---|
202 | const usb_hid_subdriver_t **subdrivers, int count)
|
---|
203 | {
|
---|
204 | int i;
|
---|
205 |
|
---|
206 | if (count <= 0) {
|
---|
207 | hid_dev->subdriver_count = 0;
|
---|
208 | hid_dev->subdrivers = NULL;
|
---|
209 | return EOK;
|
---|
210 | }
|
---|
211 |
|
---|
212 | hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(count *
|
---|
213 | sizeof(usb_hid_subdriver_t));
|
---|
214 | if (hid_dev->subdrivers == NULL) {
|
---|
215 | return ENOMEM;
|
---|
216 | }
|
---|
217 |
|
---|
218 | for (i = 0; i < count; ++i) {
|
---|
219 | hid_dev->subdrivers[i].init = subdrivers[i]->init;
|
---|
220 | hid_dev->subdrivers[i].deinit = subdrivers[i]->deinit;
|
---|
221 | hid_dev->subdrivers[i].poll = subdrivers[i]->poll;
|
---|
222 | hid_dev->subdrivers[i].poll_end = subdrivers[i]->poll_end;
|
---|
223 | }
|
---|
224 |
|
---|
225 | hid_dev->subdriver_count = count;
|
---|
226 |
|
---|
227 | return EOK;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /*----------------------------------------------------------------------------*/
|
---|
231 |
|
---|
232 | static 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 %u\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 %u\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,
|
---|
276 | mapping->usage_path, mapping->compare)) {
|
---|
277 | // does not matter if IDs were matched
|
---|
278 | matched = true;
|
---|
279 | }
|
---|
280 | } else {
|
---|
281 | // matched only if IDs were matched and there is no path
|
---|
282 | matched = ids_matched;
|
---|
283 | }
|
---|
284 |
|
---|
285 | if (matched) {
|
---|
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 | return usb_hid_save_subdrivers(hid_dev, subdrivers, count);
|
---|
294 | }
|
---|
295 |
|
---|
296 | /*----------------------------------------------------------------------------*/
|
---|
297 |
|
---|
298 | static int usb_hid_check_pipes(usb_hid_dev_t *hid_dev, usb_device_t *dev)
|
---|
299 | {
|
---|
300 | assert(hid_dev != NULL && dev != NULL);
|
---|
301 |
|
---|
302 | int rc = EOK;
|
---|
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 | rc = ENOTSUP;
|
---|
320 | }
|
---|
321 |
|
---|
322 | return rc;
|
---|
323 | }
|
---|
324 |
|
---|
325 | /*----------------------------------------------------------------------------*/
|
---|
326 |
|
---|
327 | usb_hid_dev_t *usb_hid_new(void)
|
---|
328 | {
|
---|
329 | usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)calloc(1,
|
---|
330 | sizeof(usb_hid_dev_t));
|
---|
331 |
|
---|
332 | if (hid_dev == NULL) {
|
---|
333 | usb_log_fatal("No memory!\n");
|
---|
334 | return NULL;
|
---|
335 | }
|
---|
336 |
|
---|
337 | hid_dev->parser = (usb_hid_report_parser_t *)(malloc(sizeof(
|
---|
338 | usb_hid_report_parser_t)));
|
---|
339 | if (hid_dev->parser == NULL) {
|
---|
340 | usb_log_fatal("No memory!\n");
|
---|
341 | free(hid_dev);
|
---|
342 | return NULL;
|
---|
343 | }
|
---|
344 |
|
---|
345 | hid_dev->poll_pipe_index = -1;
|
---|
346 |
|
---|
347 | return hid_dev;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /*----------------------------------------------------------------------------*/
|
---|
351 |
|
---|
352 | int usb_hid_init(usb_hid_dev_t *hid_dev, usb_device_t *dev)
|
---|
353 | {
|
---|
354 | int rc, i;
|
---|
355 |
|
---|
356 | usb_log_debug("Initializing HID structure...\n");
|
---|
357 |
|
---|
358 | if (hid_dev == NULL) {
|
---|
359 | usb_log_error("Failed to init HID structure: no structure given"
|
---|
360 | ".\n");
|
---|
361 | return EINVAL;
|
---|
362 | }
|
---|
363 |
|
---|
364 | if (dev == NULL) {
|
---|
365 | usb_log_error("Failed to init HID structure: no USB device"
|
---|
366 | " given.\n");
|
---|
367 | return EINVAL;
|
---|
368 | }
|
---|
369 |
|
---|
370 | /* The USB device should already be initialized, save it in structure */
|
---|
371 | hid_dev->usb_dev = dev;
|
---|
372 |
|
---|
373 | rc = usb_hid_check_pipes(hid_dev, dev);
|
---|
374 | if (rc != EOK) {
|
---|
375 | usb_hid_free(&hid_dev);
|
---|
376 | return rc;
|
---|
377 | }
|
---|
378 |
|
---|
379 | /* Initialize the report parser. */
|
---|
380 | rc = usb_hid_parser_init(hid_dev->parser);
|
---|
381 | if (rc != EOK) {
|
---|
382 | usb_log_error("Failed to initialize report parser.\n");
|
---|
383 | usb_hid_free(&hid_dev);
|
---|
384 | return rc;
|
---|
385 | }
|
---|
386 |
|
---|
387 | /* Get the report descriptor and parse it. */
|
---|
388 | rc = usb_hid_process_report_descriptor(hid_dev->usb_dev,
|
---|
389 | hid_dev->parser);
|
---|
390 |
|
---|
391 | bool fallback = false;
|
---|
392 |
|
---|
393 | if (rc == EOK) {
|
---|
394 | // try to find subdrivers that may want to handle this device
|
---|
395 | rc = usb_hid_find_subdrivers(hid_dev);
|
---|
396 | if (rc != EOK || hid_dev->subdriver_count == 0) {
|
---|
397 | // try to fall back to the boot protocol if available
|
---|
398 | usb_log_info("No subdrivers found to handle this"
|
---|
399 | " device.\n");
|
---|
400 | fallback = true;
|
---|
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_hid_free(&hid_dev);
|
---|
442 | } else {
|
---|
443 | bool ok = false;
|
---|
444 |
|
---|
445 | usb_log_debug("Subdriver count: %d\n",
|
---|
446 | hid_dev->subdriver_count);
|
---|
447 |
|
---|
448 | for (i = 0; i < hid_dev->subdriver_count; ++i) {
|
---|
449 | if (hid_dev->subdrivers[i].init != NULL) {
|
---|
450 | usb_log_debug("Initializing subdriver %d.\n",i);
|
---|
451 | rc = hid_dev->subdrivers[i].init(hid_dev);
|
---|
452 | if (rc != EOK) {
|
---|
453 | usb_log_warning("Failed to initialize"
|
---|
454 | " HID subdriver structure.\n");
|
---|
455 | } else {
|
---|
456 | // at least one subdriver initialized
|
---|
457 | ok = true;
|
---|
458 | }
|
---|
459 | } else {
|
---|
460 | ok = true;
|
---|
461 | }
|
---|
462 | }
|
---|
463 |
|
---|
464 | rc = (ok) ? EOK : -1; // what error to report
|
---|
465 | }
|
---|
466 |
|
---|
467 | return rc;
|
---|
468 | }
|
---|
469 |
|
---|
470 | /*----------------------------------------------------------------------------*/
|
---|
471 |
|
---|
472 | bool usb_hid_polling_callback(usb_device_t *dev, uint8_t *buffer,
|
---|
473 | size_t buffer_size, void *arg)
|
---|
474 | {
|
---|
475 | int i;
|
---|
476 |
|
---|
477 | if (dev == NULL || arg == NULL || buffer == NULL) {
|
---|
478 | usb_log_error("Missing arguments to polling callback.\n");
|
---|
479 | return false;
|
---|
480 | }
|
---|
481 |
|
---|
482 | usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
|
---|
483 |
|
---|
484 | bool cont = false;
|
---|
485 |
|
---|
486 | // continue if at least one of the subdrivers want to continue
|
---|
487 | for (i = 0; i < hid_dev->subdriver_count; ++i) {
|
---|
488 | if (hid_dev->subdrivers[i].poll != NULL
|
---|
489 | && hid_dev->subdrivers[i].poll(hid_dev, buffer,
|
---|
490 | buffer_size)) {
|
---|
491 | cont = true;
|
---|
492 | }
|
---|
493 | }
|
---|
494 |
|
---|
495 | return cont;
|
---|
496 | }
|
---|
497 |
|
---|
498 | /*----------------------------------------------------------------------------*/
|
---|
499 |
|
---|
500 | void usb_hid_polling_ended_callback(usb_device_t *dev, bool reason,
|
---|
501 | void *arg)
|
---|
502 | {
|
---|
503 | int i;
|
---|
504 |
|
---|
505 | if (dev == NULL || arg == NULL) {
|
---|
506 | return;
|
---|
507 | }
|
---|
508 |
|
---|
509 | usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
|
---|
510 |
|
---|
511 | for (i = 0; i < hid_dev->subdriver_count; ++i) {
|
---|
512 | if (hid_dev->subdrivers[i].poll_end != NULL) {
|
---|
513 | hid_dev->subdrivers[i].poll_end(hid_dev, reason);
|
---|
514 | }
|
---|
515 | }
|
---|
516 |
|
---|
517 | usb_hid_free(&hid_dev);
|
---|
518 | }
|
---|
519 |
|
---|
520 | /*----------------------------------------------------------------------------*/
|
---|
521 |
|
---|
522 | const char *usb_hid_get_function_name(const usb_hid_dev_t *hid_dev)
|
---|
523 | {
|
---|
524 | switch (hid_dev->poll_pipe_index) {
|
---|
525 | case USB_HID_KBD_POLL_EP_NO:
|
---|
526 | return HID_KBD_FUN_NAME;
|
---|
527 | break;
|
---|
528 | case USB_HID_MOUSE_POLL_EP_NO:
|
---|
529 | return HID_MOUSE_FUN_NAME;
|
---|
530 | break;
|
---|
531 | default:
|
---|
532 | return HID_GENERIC_FUN_NAME;
|
---|
533 | }
|
---|
534 | }
|
---|
535 |
|
---|
536 | /*----------------------------------------------------------------------------*/
|
---|
537 |
|
---|
538 | const char *usb_hid_get_class_name(const usb_hid_dev_t *hid_dev)
|
---|
539 | {
|
---|
540 | // this means that only boot protocol keyboards will be connected
|
---|
541 | // to the console; there is probably no better way to do this
|
---|
542 |
|
---|
543 | switch (hid_dev->poll_pipe_index) {
|
---|
544 | case USB_HID_KBD_POLL_EP_NO:
|
---|
545 | return HID_KBD_CLASS_NAME;
|
---|
546 | break;
|
---|
547 | case USB_HID_MOUSE_POLL_EP_NO:
|
---|
548 | return HID_MOUSE_CLASS_NAME;
|
---|
549 | break;
|
---|
550 | default:
|
---|
551 | return HID_GENERIC_CLASS_NAME;
|
---|
552 | }
|
---|
553 | }
|
---|
554 |
|
---|
555 | /*----------------------------------------------------------------------------*/
|
---|
556 |
|
---|
557 | void usb_hid_free(usb_hid_dev_t **hid_dev)
|
---|
558 | {
|
---|
559 | int i;
|
---|
560 |
|
---|
561 | if (hid_dev == NULL || *hid_dev == NULL) {
|
---|
562 | return;
|
---|
563 | }
|
---|
564 |
|
---|
565 | assert((*hid_dev)->subdrivers != NULL
|
---|
566 | || (*hid_dev)->subdriver_count == 0);
|
---|
567 |
|
---|
568 | for (i = 0; i < (*hid_dev)->subdriver_count; ++i) {
|
---|
569 | if ((*hid_dev)->subdrivers[i].deinit != NULL) {
|
---|
570 | (*hid_dev)->subdrivers[i].deinit(*hid_dev);
|
---|
571 | }
|
---|
572 | }
|
---|
573 |
|
---|
574 | // free the subdrivers info
|
---|
575 | if ((*hid_dev)->subdrivers != NULL) {
|
---|
576 | free((*hid_dev)->subdrivers);
|
---|
577 | }
|
---|
578 |
|
---|
579 | // destroy the parser
|
---|
580 | if ((*hid_dev)->parser != NULL) {
|
---|
581 | usb_hid_free_report_parser((*hid_dev)->parser);
|
---|
582 | }
|
---|
583 |
|
---|
584 | if ((*hid_dev)->report_desc != NULL) {
|
---|
585 | free((*hid_dev)->report_desc);
|
---|
586 | }
|
---|
587 |
|
---|
588 | free(*hid_dev);
|
---|
589 | *hid_dev = NULL;
|
---|
590 | }
|
---|
591 |
|
---|
592 | /**
|
---|
593 | * @}
|
---|
594 | */
|
---|