source: mainline/uspace/lib/usbhid/src/hiddescriptor.c@ 806a779

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 806a779 was 806a779, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

usb: little changes to make compiler happy

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