source: mainline/uspace/lib/usbhid/src/hiddescriptor.c@ 8591b31

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8591b31 was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 24.9 KB
RevLine 
[9d05599]1/*
2 * Copyright (c) 2011 Matej Klonfar
[e0a5d4c]3 * Copyright (c) 2018 Ondrej Hlavaty
[9d05599]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
[160b75e]30/** @addtogroup libusbhid
[9d05599]31 * @{
32 */
33/** @file
34 * HID report descriptor and report data parser implementation.
35 */
[faa44e58]36#include <usb/hid/hidparser.h>
[9d05599]37#include <errno.h>
38#include <stdio.h>
39#include <mem.h>
40#include <usb/debug.h>
41#include <assert.h>
[38d150e]42#include <stdlib.h>
[9d05599]43
[f3b39b4]44/*
45 * Constants defining current parsing mode for correct parsing of the set of
46 * local tags (usage) enclosed in delimter tags.
47 */
48/**
49 * Second delimiter tag was read. The set of local items (usage) ended.
50 */
[22ded10]51#define OUTSIDE_DELIMITER_SET 0
[f3b39b4]52
53/**
54 * First delimiter tag was read. The set of local items (usage) started.
55 */
[22ded10]56#define START_DELIMITER_SET 1
[f3b39b4]57
58/**
59 * Parser is in the set of local items.
60 */
[22ded10]61#define INSIDE_DELIMITER_SET 2
[f3b39b4]62
[9d05599]63/** The new report item flag. Used to determine when the item is completly
64 * configured and should be added to the report structure
65 */
66#define USB_HID_NEW_REPORT_ITEM 1
67
68/** No special action after the report descriptor tag is processed should be
69 * done
70 */
71#define USB_HID_NO_ACTION 2
72
73#define USB_HID_RESET_OFFSET 3
74
[3ca4ae9]75#define USB_HID_INVALID -98
[9d05599]76/** Unknown tag was founded in report descriptor data*/
77#define USB_HID_UNKNOWN_TAG -99
78
[f3b39b4]79/**
80 * Checks if given collection path is already present in report structure and
81 * inserts it if not.
82 *
[1b20da0]83 * @param report Report structure
84 * @param cmp_path The collection path
[f3b39b4]85 * @return Pointer to the result collection path in report structure.
86 * @retval NULL If some error occurs
87 */
[b72efe8]88usb_hid_report_path_t *usb_hid_report_path_try_insert(usb_hid_report_t *report,
89 usb_hid_report_path_t *cmp_path)
90{
91 link_t *path_it = report->collection_paths.head.next;
[1519b91]92 usb_hid_report_path_t *path = NULL;
[a35b458]93
[3bacee1]94 if ((report == NULL) || (cmp_path == NULL)) {
[f3b39b4]95 return NULL;
96 }
[a35b458]97
[3bacee1]98 while (path_it != &report->collection_paths.head) {
[f3b39b4]99 path = list_get_instance(path_it, usb_hid_report_path_t,
[3bacee1]100 cpath_link);
[a35b458]101
[3bacee1]102 if (usb_hid_report_compare_usage_path(path, cmp_path,
103 USB_HID_PATH_COMPARE_STRICT) == 0) {
[1519b91]104 break;
[b72efe8]105 }
[1519b91]106 path_it = path_it->next;
107 }
[3bacee1]108 if (path_it == &report->collection_paths.head) {
[f3b39b4]109 path = usb_hid_report_path_clone(cmp_path);
[3bacee1]110 if (path == NULL) {
[f3b39b4]111 return NULL;
112 }
[b72efe8]113 list_append(&path->cpath_link, &report->collection_paths);
[1519b91]114 report->collection_paths_count++;
[b9d7965]115
116 return path;
[3bacee1]117 } else {
[f3b39b4]118 return list_get_instance(path_it, usb_hid_report_path_t,
[3bacee1]119 cpath_link);
[1519b91]120 }
121}
[9d05599]122
123/**
124 * Initialize the report descriptor parser structure
125 *
126 * @param parser Report descriptor parser structure
127 * @return Error code
[f3b39b4]128 * @retval EINVAL If no report structure was given
129 * @retval EOK If report structure was successfully initialized
[9d05599]130 */
[5a6cc679]131errno_t usb_hid_report_init(usb_hid_report_t *report)
[9d05599]132{
[8c62a71]133 if (report == NULL) {
[9d05599]134 return EINVAL;
135 }
136
137 memset(report, 0, sizeof(usb_hid_report_t));
138 list_initialize(&report->reports);
139 list_initialize(&report->collection_paths);
140
141 report->use_report_ids = 0;
[3bacee1]142 return EOK;
[9d05599]143}
144
[f3b39b4]145/**
[3b5d5b9d]146 *
147 *
[f3b39b4]148 * @param report Report structure in which the new report items should be
149 * stored
[1b20da0]150 * @param report_item Current report descriptor's parsing state table
[f3b39b4]151 * @return Error code
152 * @retval EOK If all fields were successfully append to report
153 * @retval EINVAL If invalid parameters (NULL) was given
154 * @retval ENOMEM If there is no memmory to store new report description
155 *
[3b5d5b9d]156 */
[5a6cc679]157errno_t usb_hid_report_append_fields(usb_hid_report_t *report,
[3bacee1]158 usb_hid_report_item_t *report_item)
159{
[f3b39b4]160
[9d05599]161 usb_hid_report_field_t *field;
162 int i;
163
[3a6e423]164 uint32_t *usages;
[3bacee1]165 int usages_used = 0;
[f3b39b4]166
[3bacee1]167 if ((report == NULL) || (report_item == NULL)) {
[f3b39b4]168 return EINVAL;
169 }
170
[3bacee1]171 if (report_item->usages_count > 0) {
[806a779]172 usages = malloc(sizeof(uint32_t) * report_item->usages_count);
[f3b39b4]173 memcpy(usages, report_item->usages, sizeof(int32_t) *
[3bacee1]174 report_item->usages_count);
175 } else {
[3a6e423]176 usages = NULL;
177 }
[a35b458]178
[1b20da0]179 usb_hid_report_path_t *path = report_item->usage_path;
[3bacee1]180 for (i = 0; i < report_item->count; i++) {
[9d05599]181
182 field = malloc(sizeof(usb_hid_report_field_t));
[3bacee1]183 if (field == NULL) {
[574f276]184 return ENOMEM;
185 }
186
[9d05599]187 memset(field, 0, sizeof(usb_hid_report_field_t));
[b72efe8]188 link_initialize(&field->ritems_link);
[9d05599]189
[1b20da0]190 /* fill the attributes */
[9d05599]191 field->logical_minimum = report_item->logical_minimum;
192 field->logical_maximum = report_item->logical_maximum;
193 field->physical_minimum = report_item->physical_minimum;
194 field->physical_maximum = report_item->physical_maximum;
195
[3bacee1]196 if (USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0) {
[1b20da0]197 /*
[47e00b83]198 * Store usage array. The Correct Usage Page and Usage is
199 * depending on data in report and will be filled later
200 */
[3a6e423]201 field->usage = 0;
202 field->usage_page = 0; //report_item->usage_page;
203
204 field->usages_count = report_item->usages_count;
205 field->usages = usages;
206 usages_used = 1;
[3bacee1]207 } else {
[9d05599]208
[3a6e423]209 /* Fill the correct Usage and Usage Page */
210 int32_t usage;
[3bacee1]211 if (i < report_item->usages_count) {
[3b5d5b9d]212 usage = report_item->usages[i];
[3bacee1]213 } else {
214 usage = report_item->usages[report_item->usages_count - 1];
[9d05599]215 }
216
[3bacee1]217 if (USB_HID_IS_EXTENDED_USAGE(usage)) {
[3a6e423]218 field->usage = USB_HID_EXTENDED_USAGE(usage);
[1b20da0]219 field->usage_page =
[3bacee1]220 USB_HID_EXTENDED_USAGE_PAGE(usage);
221 } else {
[3a6e423]222 // should not occur
[9d05599]223 field->usage = usage;
[3a6e423]224 field->usage_page = report_item->usage_page;
[9d05599]225 }
226 }
[a35b458]227
[f3b39b4]228 usb_hid_report_set_last_item(path, USB_HID_TAG_CLASS_GLOBAL,
[3bacee1]229 field->usage_page);
[f3b39b4]230 usb_hid_report_set_last_item(path, USB_HID_TAG_CLASS_LOCAL,
[3bacee1]231 field->usage);
[1519b91]232
[f3b39b4]233 field->collection_path =
[3bacee1]234 usb_hid_report_path_try_insert(report, path);
[1519b91]235
[9d05599]236 field->size = report_item->size;
[ee7e7c93]237
[6283cefb]238 field->offset = report_item->offset + (i * report_item->size);
[ee7e7c93]239
[3bacee1]240 if (report->use_report_ids != 0) {
[9d05599]241 field->offset += 8;
242 report->use_report_ids = 1;
243 }
[ee7e7c93]244
[9d05599]245 field->item_flags = report_item->item_flags;
246
247 /* find the right report list*/
248 usb_hid_report_description_t *report_des;
[f3b39b4]249 report_des = usb_hid_report_find_description(report,
[3bacee1]250 report_item->id, report_item->type);
[a35b458]251
[3bacee1]252 if (report_des == NULL) {
[f3b39b4]253 report_des = malloc(
[3bacee1]254 sizeof(usb_hid_report_description_t));
255 if (report_des == NULL) {
[f3b39b4]256 return ENOMEM;
257 }
258
259 memset(report_des, 0,
[3bacee1]260 sizeof(usb_hid_report_description_t));
[9d05599]261
262 report_des->type = report_item->type;
263 report_des->report_id = report_item->id;
[3bacee1]264 if (report_des->report_id != 0) {
[1432fcf3]265 /* set up the bit length by report_id field */
266 report_des->bit_length = 8;
267 }
268
[b72efe8]269 link_initialize (&report_des->reports_link);
[9d05599]270 list_initialize (&report_des->report_items);
271
[b72efe8]272 list_append(&report_des->reports_link, &report->reports);
[9d05599]273 report->report_count++;
274 }
275
276 /* append this field to the end of founded report list */
[b72efe8]277 list_append(&field->ritems_link, &report_des->report_items);
[a35b458]278
[9d05599]279 /* update the sizes */
280 report_des->bit_length += field->size;
281 report_des->item_length++;
282
283 }
284
[3a6e423]285 // free only when not used!!!
[3bacee1]286 if (usages && usages_used == 0) {
[3a6e423]287 free(usages);
288 }
[9d05599]289
290 return EOK;
291}
[a76b01b4]292
[f3b39b4]293/**
294 * Finds description of report with given report_id and of given type in
295 * opaque report structure.
296 *
297 * @param report Opaque structure containing the parsed report descriptor
298 * @param report_id ReportId of report we are searching
299 * @param type Type of report we are searching
300 * @return Pointer to the particular report description
301 * @retval NULL If no description is founded
302 */
[3bacee1]303usb_hid_report_description_t *usb_hid_report_find_description(
304 const usb_hid_report_t *report, uint8_t report_id,
305 usb_hid_report_type_t type)
306{
[9d05599]307
[3bacee1]308 if (report == NULL) {
[0dd3e49]309 return NULL;
310 }
311
[feeac0d]312 list_foreach(report->reports, reports_link,
313 usb_hid_report_description_t, report_des) {
[dcb7d7cd]314 // if report id not set, return the first of the type
[3bacee1]315 if (((report_des->report_id == report_id) || (report_id == 0)) &&
316 (report_des->type == type)) {
[9d05599]317 return report_des;
318 }
319 }
320
321 return NULL;
322}
[a76b01b4]323
[9d05599]324/** Parse HID report descriptor.
325 *
326 * @param parser Opaque HID report parser structure.
327 * @param data Data describing the report.
328 * @return Error code.
[f3b39b4]329 * @retval ENOMEM If no more memmory is available
330 * @retval EINVAL If invalid data are founded
331 * @retval EOK If report descriptor is successfully parsed
[9d05599]332 */
[1b20da0]333errno_t usb_hid_parse_report_descriptor(usb_hid_report_t *report,
[9d05599]334 const uint8_t *data, size_t size)
335{
[3bacee1]336 size_t i = 0;
337 uint8_t tag = 0;
338 uint8_t item_size = 0;
339 int class = 0;
[9d05599]340 int ret;
[3bacee1]341 usb_hid_report_item_t *report_item = 0;
[1b20da0]342 usb_hid_report_item_t *new_report_item;
[9d05599]343 usb_hid_report_path_t *usage_path;
344
[3bacee1]345 size_t offset_input = 0;
346 size_t offset_output = 0;
347 size_t offset_feature = 0;
[a35b458]348
[b72efe8]349 link_t *item_link;
[9d05599]350
[b72efe8]351 list_t stack;
352 list_initialize(&stack);
[9d05599]353
354 /* parser structure initialization*/
[3bacee1]355 if (usb_hid_report_init(report) != EOK) {
[9d05599]356 return EINVAL;
357 }
[a35b458]358
[9d05599]359 /*report item initialization*/
[3bacee1]360 if (!(report_item = malloc(sizeof(usb_hid_report_item_t)))) {
[9d05599]361 return ENOMEM;
362 }
363 memset(report_item, 0, sizeof(usb_hid_report_item_t));
[b72efe8]364 link_initialize(&(report_item->link));
[9d05599]365
366 /* usage path context initialization */
[3bacee1]367 if (!(usage_path = usb_hid_report_path())) {
[9d05599]368 return ENOMEM;
369 }
[1b20da0]370 usb_hid_report_path_append_item(usage_path, 0, 0);
[a35b458]371
[3bacee1]372 while (i < size) {
373 if (!USB_HID_ITEM_IS_LONG(data[i])) {
[9d05599]374
[3bacee1]375 if ((i + USB_HID_ITEM_SIZE(data[i])) >= size) {
[9d05599]376 return EINVAL;
377 }
[a35b458]378
[9d05599]379 tag = USB_HID_ITEM_TAG(data[i]);
380 item_size = USB_HID_ITEM_SIZE(data[i]);
381 class = USB_HID_ITEM_TAG_CLASS(data[i]);
[a35b458]382
[3bacee1]383 ret = usb_hid_report_parse_tag(tag, class, data + i + 1,
384 item_size, report_item, usage_path);
[f3b39b4]385
[3bacee1]386 switch (ret) {
[f3b39b4]387 case USB_HID_NEW_REPORT_ITEM:
[7c3fb9b]388 /*
389 * store report item to report and create the
[f3b39b4]390 * new one store current collection path
391 */
392 report_item->usage_path = usage_path;
[a35b458]393
[f3b39b4]394 usb_hid_report_path_set_report_id(
[3bacee1]395 report_item->usage_path, report_item->id);
[a35b458]396
[3bacee1]397 if (report_item->id != 0) {
[f3b39b4]398 report->use_report_ids = 1;
399 }
[a35b458]400
[3bacee1]401 switch (tag) {
[f3b39b4]402 case USB_HID_REPORT_TAG_INPUT:
[1b20da0]403 report_item->type =
[f3b39b4]404 USB_HID_REPORT_TYPE_INPUT;
405
406 report_item->offset = offset_input;
[1b20da0]407 offset_input += report_item->count *
[f3b39b4]408 report_item->size;
409 break;
[a35b458]410
[f3b39b4]411 case USB_HID_REPORT_TAG_OUTPUT:
[1b20da0]412 report_item->type =
[f3b39b4]413 USB_HID_REPORT_TYPE_OUTPUT;
[a35b458]414
[f3b39b4]415 report_item->offset = offset_output;
[1b20da0]416 offset_output += report_item->count *
[f3b39b4]417 report_item->size;
[9d05599]418 break;
[a35b458]419
[f3b39b4]420 case USB_HID_REPORT_TAG_FEATURE:
[1b20da0]421 report_item->type =
[f3b39b4]422 USB_HID_REPORT_TYPE_FEATURE;
[9d05599]423
[f3b39b4]424 report_item->offset = offset_feature;
[1b20da0]425 offset_feature += report_item->count *
[3bacee1]426 report_item->size;
[9d05599]427 break;
[a35b458]428
[f3b39b4]429 default:
430 usb_log_debug2(
431 "\tjump over - tag %X\n", tag);
[3bacee1]432 break;
[f3b39b4]433 }
[a35b458]434
[1b20da0]435 /*
436 * append new fields to the report structure
[f3b39b4]437 */
[1b20da0]438 usb_hid_report_append_fields(report,
[f3b39b4]439 report_item);
440
441 /* reset local items */
442 usb_hid_report_reset_local_items (report_item);
443 break;
444
445 case USB_HID_RESET_OFFSET:
446 offset_input = 0;
447 offset_output = 0;
448 offset_feature = 0;
[1b20da0]449 usb_hid_report_path_set_report_id (usage_path,
[f3b39b4]450 report_item->id);
451 break;
452
453 case USB_HID_REPORT_TAG_PUSH:
454 // push current state to stack
455 new_report_item = usb_hid_report_item_clone(
456 report_item);
[a35b458]457
[1b20da0]458 usb_hid_report_path_t *tmp_path =
[f3b39b4]459 usb_hid_report_path_clone(usage_path);
[9d05599]460
[1b20da0]461 new_report_item->usage_path = tmp_path;
[9d05599]462
[f3b39b4]463 list_prepend (&new_report_item->link, &stack);
464 break;
465 case USB_HID_REPORT_TAG_POP:
466 // restore current state from stack
[b72efe8]467 item_link = list_first(&stack);
468 if (item_link == NULL) {
[f3b39b4]469 return EINVAL;
470 }
471 free(report_item);
[a35b458]472
[b72efe8]473 report_item = list_get_instance(item_link,
[f3b39b4]474 usb_hid_report_item_t, link);
[a35b458]475
[f3b39b4]476 usb_hid_report_usage_path_t *tmp_usage_path;
477 tmp_usage_path = list_get_instance(
[b72efe8]478 report_item->usage_path->cpath_link.prev,
479 usb_hid_report_usage_path_t, rpath_items_link);
[a35b458]480
[1b20da0]481 usb_hid_report_set_last_item(usage_path,
[f3b39b4]482 USB_HID_TAG_CLASS_GLOBAL, tmp_usage_path->usage_page);
[a35b458]483
[1b20da0]484 usb_hid_report_set_last_item(usage_path,
[f3b39b4]485 USB_HID_TAG_CLASS_LOCAL, tmp_usage_path->usage);
[9d05599]486
[f3b39b4]487 usb_hid_report_path_free(report_item->usage_path);
[b72efe8]488 list_remove (item_link);
[a35b458]489
[f3b39b4]490 break;
[a35b458]491
[f3b39b4]492 default:
[1b20da0]493 // nothing special to do
[f3b39b4]494 break;
[9d05599]495 }
496
497 /* jump over the processed block */
498 i += 1 + USB_HID_ITEM_SIZE(data[i]);
[3bacee1]499 } else {
[9d05599]500 // TBD
[3bacee1]501 i += 3 + USB_HID_ITEM_SIZE(data[i + 1]);
[9d05599]502 }
[a35b458]503
[9d05599]504 }
[a35b458]505
[9d05599]506 return EOK;
507}
508
509/**
510 * Parse one tag of the report descriptor
511 *
512 * @param Tag to parse
513 * @param Report descriptor buffer
514 * @param Size of data belongs to this tag
515 * @param Current report item structe
516 * @return Code of action to be done next
517 */
[f3b39b4]518int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data,
[3bacee1]519 size_t item_size, usb_hid_report_item_t *report_item,
520 usb_hid_report_path_t *usage_path)
521{
[a35b458]522
[9d05599]523 int ret;
[a35b458]524
[3bacee1]525 switch (class) {
[f3b39b4]526 case USB_HID_TAG_CLASS_MAIN:
[9d05599]527
[3bacee1]528 if ((ret = usb_hid_report_parse_main_tag(tag, data, item_size,
529 report_item, usage_path)) == 0) {
[9d05599]530
[f3b39b4]531 return USB_HID_NEW_REPORT_ITEM;
[3bacee1]532 } else {
[f3b39b4]533 return ret;
534 }
535 break;
[9d05599]536
[1b20da0]537 case USB_HID_TAG_CLASS_GLOBAL:
[f3b39b4]538 return usb_hid_report_parse_global_tag(tag, data, item_size,
[3bacee1]539 report_item, usage_path);
[f3b39b4]540 break;
541
[1b20da0]542 case USB_HID_TAG_CLASS_LOCAL:
[f3b39b4]543 return usb_hid_report_parse_local_tag(tag, data, item_size,
[3bacee1]544 report_item, usage_path);
[f3b39b4]545 break;
[a35b458]546
[f3b39b4]547 default:
548 return USB_HID_NO_ACTION;
[9d05599]549 }
550}
551
552/**
553 * Parse main tags of report descriptor
554 *
555 * @param Tag identifier
556 * @param Data buffer
557 * @param Length of data buffer
558 * @param Current state table
[3ca4ae9]559 * @return 0 or USB_HID_ code
[9d05599]560 */
561
[1b20da0]562int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data,
[3bacee1]563 size_t item_size, usb_hid_report_item_t *report_item,
564 usb_hid_report_path_t *usage_path)
[674cf89]565{
566 usb_hid_report_usage_path_t *path_item;
[a35b458]567
[3bacee1]568 switch (tag) {
[f3b39b4]569 case USB_HID_REPORT_TAG_INPUT:
570 case USB_HID_REPORT_TAG_OUTPUT:
571 case USB_HID_REPORT_TAG_FEATURE:
[1b20da0]572 report_item->item_flags = *data;
573 return 0;
[f3b39b4]574 break;
[a35b458]575
[f3b39b4]576 case USB_HID_REPORT_TAG_COLLECTION:
[3a6e423]577
[f3b39b4]578 /* store collection atributes */
[b72efe8]579 path_item = list_get_instance(list_first(&usage_path->items),
[3bacee1]580 usb_hid_report_usage_path_t, rpath_items_link);
[b72efe8]581 path_item->flags = *data;
[a35b458]582
[f3b39b4]583 /* set last item */
[1b20da0]584 usb_hid_report_set_last_item(usage_path,
[3bacee1]585 USB_HID_TAG_CLASS_GLOBAL,
586 USB_HID_EXTENDED_USAGE_PAGE(report_item->usages[report_item->usages_count - 1]));
[f3b39b4]587
[1b20da0]588 usb_hid_report_set_last_item(usage_path,
[3bacee1]589 USB_HID_TAG_CLASS_LOCAL,
590 USB_HID_EXTENDED_USAGE(report_item->usages[report_item->usages_count - 1]));
[a35b458]591
[7c3fb9b]592 /*
593 * append the new one which will be set by common usage/usage
594 * page
595 */
[1b20da0]596 usb_hid_report_path_append_item(usage_path,
[3bacee1]597 report_item->usage_page,
598 report_item->usages[report_item->usages_count - 1]);
[f3b39b4]599
600 usb_hid_report_reset_local_items (report_item);
601 return USB_HID_NO_ACTION;
602 break;
[a35b458]603
[f3b39b4]604 case USB_HID_REPORT_TAG_END_COLLECTION:
605 usb_hid_report_remove_last_item(usage_path);
606 return USB_HID_NO_ACTION;
607 break;
608
609 default:
610 return USB_HID_NO_ACTION;
[9d05599]611 }
612
[3ca4ae9]613 return 0;
[9d05599]614}
615
616/**
617 * Parse global tags of report descriptor
618 *
619 * @param Tag identifier
620 * @param Data buffer
621 * @param Length of data buffer
622 * @param Current state table
[3ca4ae9]623 * @return 0 or USB_HID_ code
[9d05599]624 */
[1b20da0]625int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data,
[3bacee1]626 size_t item_size, usb_hid_report_item_t *report_item,
627 usb_hid_report_path_t *usage_path)
628{
[a35b458]629
[3bacee1]630 switch (tag) {
[f3b39b4]631 case USB_HID_REPORT_TAG_USAGE_PAGE:
[1b20da0]632 report_item->usage_page =
[3bacee1]633 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]634 break;
635
636 case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
637 report_item->logical_minimum = USB_HID_UINT32_TO_INT32(
[3bacee1]638 usb_hid_report_tag_data_uint32(data, item_size),
639 item_size * 8);
[f3b39b4]640 break;
641
642 case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
643 report_item->logical_maximum = USB_HID_UINT32_TO_INT32(
[3bacee1]644 usb_hid_report_tag_data_uint32(data, item_size),
645 item_size * 8);
[f3b39b4]646 break;
647
648 case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
649 report_item->physical_minimum = USB_HID_UINT32_TO_INT32(
[3bacee1]650 usb_hid_report_tag_data_uint32(data, item_size),
651 item_size * 8);
[1b20da0]652 break;
[f3b39b4]653
654 case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
655 report_item->physical_maximum = USB_HID_UINT32_TO_INT32(
[3bacee1]656 usb_hid_report_tag_data_uint32(data, item_size),
657 item_size * 8);
[f3b39b4]658 break;
659
660 case USB_HID_REPORT_TAG_UNIT_EXPONENT:
661 report_item->unit_exponent = usb_hid_report_tag_data_uint32(
[3bacee1]662 data, item_size);
[f3b39b4]663 break;
664
665 case USB_HID_REPORT_TAG_UNIT:
666 report_item->unit = usb_hid_report_tag_data_uint32(
[3bacee1]667 data, item_size);
[f3b39b4]668 break;
669
670 case USB_HID_REPORT_TAG_REPORT_SIZE:
671 report_item->size = usb_hid_report_tag_data_uint32(
[3bacee1]672 data, item_size);
[f3b39b4]673 break;
674
675 case USB_HID_REPORT_TAG_REPORT_COUNT:
676 report_item->count = usb_hid_report_tag_data_uint32(
[3bacee1]677 data, item_size);
[f3b39b4]678 break;
679
680 case USB_HID_REPORT_TAG_REPORT_ID:
[1b20da0]681 report_item->id = usb_hid_report_tag_data_uint32(data,
[3bacee1]682 item_size);
[f3b39b4]683 return USB_HID_RESET_OFFSET;
684 break;
[a35b458]685
[f3b39b4]686 case USB_HID_REPORT_TAG_PUSH:
687 case USB_HID_REPORT_TAG_POP:
[1b20da0]688 /*
[f3b39b4]689 * stack operations are done in top level parsing
690 * function
691 */
692 return tag;
693 break;
[a35b458]694
[f3b39b4]695 default:
696 return USB_HID_NO_ACTION;
[9d05599]697 }
698
[3ca4ae9]699 return 0;
[9d05599]700}
701
702/**
703 * Parse local tags of report descriptor
704 *
705 * @param Tag identifier
706 * @param Data buffer
707 * @param Length of data buffer
708 * @param Current state table
[3ca4ae9]709 * @return 0 or USB_HID_ code
[9d05599]710 */
[e141281]711int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data,
712 size_t item_size, usb_hid_report_item_t *report_item,
713 usb_hid_report_path_t *usage_path)
[9d05599]714{
[3a6e423]715 int32_t extended_usage;
[a35b458]716
[e141281]717 switch (tag) {
[f3b39b4]718 case USB_HID_REPORT_TAG_USAGE:
[e141281]719 switch (report_item->in_delimiter) {
[f3b39b4]720 case INSIDE_DELIMITER_SET:
[e141281]721 /*
722 * Nothing to do.
723 * We catch only the first one
[f3b39b4]724 */
[1be39e9]725 break;
[a35b458]726
[f3b39b4]727 case START_DELIMITER_SET:
728 report_item->in_delimiter = INSIDE_DELIMITER_SET;
[dc12262]729 /* Fallthrough */
[f3b39b4]730 case OUTSIDE_DELIMITER_SET:
731 extended_usage = ((report_item->usage_page) << 16);
[e141281]732 extended_usage +=
733 usb_hid_report_tag_data_uint32(data, item_size);
[a35b458]734
[e141281]735 report_item->usages[report_item->usages_count] =
736 extended_usage;
[a35b458]737
[f3b39b4]738 report_item->usages_count++;
739 break;
740 }
741 break;
[a35b458]742
[e141281]743 case USB_HID_REPORT_TAG_USAGE_MINIMUM:
[f3b39b4]744 if (item_size == 3) {
[e141281]745 /* Usage extended usages */
746 report_item->extended_usage_page =
[f3b39b4]747 USB_HID_EXTENDED_USAGE_PAGE(
[e141281]748 usb_hid_report_tag_data_uint32(data, item_size));
[a35b458]749
[e141281]750 report_item->usage_minimum =
[f3b39b4]751 USB_HID_EXTENDED_USAGE(
[e141281]752 usb_hid_report_tag_data_uint32(data, item_size));
753 } else {
754 report_item->usage_minimum =
755 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]756 }
757 break;
[a35b458]758
[f3b39b4]759 case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
760 if (item_size == 3) {
[e141281]761 if (report_item->extended_usage_page !=
762 USB_HID_EXTENDED_USAGE_PAGE(
763 usb_hid_report_tag_data_uint32(data, item_size))) {
[3ca4ae9]764 return USB_HID_INVALID;
[1be39e9]765 }
[a35b458]766
[e141281]767 /* Usage extended usages */
768 report_item->extended_usage_page =
769 USB_HID_EXTENDED_USAGE_PAGE(
[3bacee1]770 usb_hid_report_tag_data_uint32(data, item_size));
[a35b458]771
[e141281]772 report_item->usage_maximum =
773 USB_HID_EXTENDED_USAGE(
[3bacee1]774 usb_hid_report_tag_data_uint32(data, item_size));
[e141281]775 } else {
776 report_item->usage_maximum =
[3bacee1]777 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]778 }
[a35b458]779
[e141281]780 /* Put the records into the usages array */
781 for (int32_t i = report_item->usage_minimum;
[f3b39b4]782 i <= report_item->usage_maximum; i++) {
[a35b458]783
[e141281]784 if (report_item->extended_usage_page) {
785 report_item->usages[report_item->usages_count++] =
786 (report_item->extended_usage_page << 16) + i;
787 } else {
788 report_item->usages[report_item->usages_count++] =
789 (report_item->usage_page << 16) + i;
[f3b39b4]790 }
791 }
792 report_item->extended_usage_page = 0;
[a35b458]793
[f3b39b4]794 break;
[a35b458]795
[f3b39b4]796 case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
[e141281]797 report_item->designator_index =
798 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]799 break;
[a35b458]800
[f3b39b4]801 case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
[e141281]802 report_item->designator_minimum =
803 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]804 break;
[a35b458]805
[f3b39b4]806 case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
[e141281]807 report_item->designator_maximum =
808 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]809 break;
[a35b458]810
[f3b39b4]811 case USB_HID_REPORT_TAG_STRING_INDEX:
[e141281]812 report_item->string_index =
813 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]814 break;
[a35b458]815
[f3b39b4]816 case USB_HID_REPORT_TAG_STRING_MINIMUM:
[e141281]817 report_item->string_minimum =
818 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]819 break;
[a35b458]820
[f3b39b4]821 case USB_HID_REPORT_TAG_STRING_MAXIMUM:
[e141281]822 report_item->string_maximum =
823 usb_hid_report_tag_data_uint32(data, item_size);
824 break;
[a35b458]825
[f3b39b4]826 case USB_HID_REPORT_TAG_DELIMITER:
[e141281]827 report_item->in_delimiter =
828 usb_hid_report_tag_data_uint32(data, item_size);
[f3b39b4]829 break;
[a35b458]830
[f3b39b4]831 default:
832 return USB_HID_NO_ACTION;
[9d05599]833 }
[a35b458]834
[3ca4ae9]835 return 0;
[9d05599]836}
[a76b01b4]837
[9d05599]838/**
839 * Converts raw data to uint32 (thats the maximum length of short item data)
840 *
841 * @param Data buffer
842 * @param Size of buffer
843 * @return Converted int32 number
844 */
845uint32_t usb_hid_report_tag_data_uint32(const uint8_t *data, size_t size)
846{
847 unsigned int i;
848 uint32_t result;
849
850 result = 0;
[3bacee1]851 for (i = 0; i < size; i++) {
852 result = (result | (data[i]) << (i * 8));
[9d05599]853 }
854
855 return result;
856}
[a76b01b4]857
[9d05599]858/**
859 * Prints content of given list of report items.
860 *
861 * @param List of report items (usb_hid_report_item_t)
862 * @return void
863 */
[b72efe8]864void usb_hid_descriptor_print_list(list_t *list)
[9d05599]865{
[3bacee1]866 if (list == NULL || list_empty(list)) {
867 usb_log_debug("\tempty");
868 return;
[9d05599]869 }
[b72efe8]870
[3bacee1]871 list_foreach(*list, ritems_link, usb_hid_report_field_t,
872 report_item) {
[a1732929]873 usb_log_debug("\t\tOFFSET: %u", report_item->offset);
874 usb_log_debug("\t\tSIZE: %zu", report_item->size);
875 usb_log_debug("\t\tLOGMIN: %d",
[3bacee1]876 report_item->logical_minimum);
[a1732929]877 usb_log_debug("\t\tLOGMAX: %d",
[3bacee1]878 report_item->logical_maximum);
[a1732929]879 usb_log_debug("\t\tPHYMIN: %d",
[3bacee1]880 report_item->physical_minimum);
[a1732929]881 usb_log_debug("\t\tPHYMAX: %d",
[3bacee1]882 report_item->physical_maximum);
[a1732929]883 usb_log_debug("\t\ttUSAGEMIN: %X",
[3bacee1]884 report_item->usage_minimum);
[a1732929]885 usb_log_debug("\t\tUSAGEMAX: %X",
[3bacee1]886 report_item->usage_maximum);
[a1732929]887 usb_log_debug("\t\tUSAGES COUNT: %zu",
[3bacee1]888 report_item->usages_count);
[9d05599]889
[a1732929]890 usb_log_debug("\t\tVALUE: %X", report_item->value);
891 usb_log_debug("\t\ttUSAGE: %X", report_item->usage);
892 usb_log_debug("\t\tUSAGE PAGE: %X", report_item->usage_page);
[b72efe8]893
[3a6e423]894 usb_hid_print_usage_path(report_item->collection_path);
[9d05599]895 }
896}
[a76b01b4]897
[9d05599]898/**
899 * Prints content of given report descriptor in human readable format.
900 *
901 * @param parser Parsed descriptor to print
902 * @return void
903 */
904void usb_hid_descriptor_print(usb_hid_report_t *report)
905{
[feeac0d]906 if (report == NULL)
[9d05599]907 return;
908
[feeac0d]909 list_foreach(report->reports, reports_link,
910 usb_hid_report_description_t, report_des) {
[a1732929]911 usb_log_debug("Report ID: %d", report_des->report_id);
912 usb_log_debug("\tType: %d", report_des->type);
913 usb_log_debug("\tLength: %zu", report_des->bit_length);
914 usb_log_debug("\tB Size: %zu",
[3bacee1]915 usb_hid_report_byte_size(report,
916 report_des->report_id,
917 report_des->type));
[a1732929]918 usb_log_debug("\tItems: %zu", report_des->item_length);
[9d05599]919
920 usb_hid_descriptor_print_list(&report_des->report_items);
921 }
922}
[a76b01b4]923
[1b20da0]924/** Frees the HID report descriptor parser structure
[9d05599]925 *
926 * @param parser Opaque HID report parser structure
927 * @return void
928 */
[a8c4e871]929void usb_hid_report_deinit(usb_hid_report_t *report)
[9d05599]930{
[3bacee1]931 if (report == NULL) {
[9d05599]932 return;
933 }
934
935 // free collection paths
[b72efe8]936 link_t *path_link;
[9d05599]937 usb_hid_report_path_t *path;
[3bacee1]938 while (!list_empty(&report->collection_paths)) {
[b72efe8]939 path_link = list_first(&report->collection_paths);
940 path = list_get_instance(path_link,
941 usb_hid_report_path_t, cpath_link);
[f3b39b4]942
[b72efe8]943 list_remove(path_link);
944 usb_hid_report_path_free(path);
[9d05599]945 }
[a35b458]946
[9d05599]947 // free report items
948 usb_hid_report_description_t *report_des;
949 usb_hid_report_field_t *field;
[3bacee1]950 while (!list_empty(&report->reports)) {
[b72efe8]951 report_des = list_get_instance(list_first(&report->reports),
[3bacee1]952 usb_hid_report_description_t, reports_link);
[f3b39b4]953
[b72efe8]954 list_remove(&report_des->reports_link);
[a35b458]955
[3bacee1]956 while (!list_empty(&report_des->report_items)) {
[f3b39b4]957 field = list_get_instance(
[b72efe8]958 list_first(&report_des->report_items),
959 usb_hid_report_field_t, ritems_link);
[f3b39b4]960
[b72efe8]961 list_remove(&field->ritems_link);
[9d05599]962
963 free(field);
964 }
[a35b458]965
[9d05599]966 free(report_des);
967 }
[a35b458]968
[9d05599]969 return;
970}
[a76b01b4]971
[9d05599]972/**
973 * @}
974 */
Note: See TracBrowser for help on using the repository browser.