source: mainline/uspace/lib/usbhid/src/hiddescriptor.c@ 79ae36dd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 79ae36dd was 0dd3e49, checked in by Matej Klonfar <maklf@…>, 14 years ago

find report description bug fix

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