source: mainline/uspace/lib/usbhid/src/hiddescriptor.c@ 1432fcf3

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

bit length of reports fixed

  • Property mode set to 100644
File size: 26.7 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 memset(field, 0, sizeof(usb_hid_report_field_t));
190 list_initialize(&field->link);
191
192 /* fill the attributes */
193 field->logical_minimum = report_item->logical_minimum;
194 field->logical_maximum = report_item->logical_maximum;
195 field->physical_minimum = report_item->physical_minimum;
196 field->physical_maximum = report_item->physical_maximum;
197
198 if(USB_HID_ITEM_FLAG_VARIABLE(report_item->item_flags) == 0){
199 /*
200 Store usage array. The Correct Usage Page and Usage is
201 depending on data in report and will be filled later
202 */
203 field->usage = 0;
204 field->usage_page = 0; //report_item->usage_page;
205
206 field->usages_count = report_item->usages_count;
207 field->usages = usages;
208 usages_used = 1;
209 }
210 else {
211
212 /* Fill the correct Usage and Usage Page */
213 int32_t usage;
214 if(i < report_item->usages_count) {
215 usage = report_item->usages[i];
216 }
217 else {
218 usage = report_item->usages[
219 report_item->usages_count- 1];
220 }
221
222 if(USB_HID_IS_EXTENDED_USAGE(usage)){
223 field->usage = USB_HID_EXTENDED_USAGE(usage);
224 field->usage_page =
225 USB_HID_EXTENDED_USAGE_PAGE(usage);
226 }
227 else {
228 // should not occur
229 field->usage = usage;
230 field->usage_page = report_item->usage_page;
231 }
232 }
233
234 usb_hid_report_set_last_item(path, USB_HID_TAG_CLASS_GLOBAL,
235 field->usage_page);
236 usb_hid_report_set_last_item(path, USB_HID_TAG_CLASS_LOCAL,
237 field->usage);
238
239 field->collection_path =
240 usb_hid_report_path_try_insert(report, path);
241
242 field->size = report_item->size;
243
244 size_t offset_byte = (report_item->offset + (i *
245 report_item->size)) / 8;
246
247 size_t offset_bit = 8 - ((report_item->offset + (i *
248 report_item->size)) % 8) - report_item->size;
249
250 field->offset = 8 * offset_byte + offset_bit;
251 if(report_item->id != 0) {
252 field->offset += 8;
253 report->use_report_ids = 1;
254 }
255 field->item_flags = report_item->item_flags;
256
257 /* find the right report list*/
258 usb_hid_report_description_t *report_des;
259 report_des = usb_hid_report_find_description(report,
260 report_item->id, report_item->type);
261
262 if(report_des == NULL){
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));
271
272 report_des->type = report_item->type;
273 report_des->report_id = report_item->id;
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
279 list_initialize (&report_des->link);
280 list_initialize (&report_des->report_items);
281
282 list_append(&report_des->link, &report->reports);
283 report->report_count++;
284 }
285
286 /* append this field to the end of founded report list */
287 list_append (&field->link, &report_des->report_items);
288
289 /* update the sizes */
290 report_des->bit_length += field->size;
291 report_des->item_length++;
292
293 }
294
295 // free only when not used!!!
296 if(usages && usages_used == 0) {
297 free(usages);
298 }
299
300 return EOK;
301}
302/*---------------------------------------------------------------------------*/
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) {
316
317 link_t *report_it = report->reports.next;
318 usb_hid_report_description_t *report_des = NULL;
319
320 while(report_it != &report->reports) {
321 report_des = list_get_instance(report_it,
322 usb_hid_report_description_t, link);
323
324 if((report_des->report_id == report_id) &&
325 (report_des->type == type)) {
326 return report_des;
327 }
328
329 report_it = report_it->next;
330 }
331
332 return NULL;
333}
334/*---------------------------------------------------------------------------*/
335
336/** Parse HID report descriptor.
337 *
338 * @param parser Opaque HID report parser structure.
339 * @param data Data describing the report.
340 * @return Error code.
341 * @retval ENOMEM If no more memmory is available
342 * @retval EINVAL If invalid data are founded
343 * @retval EOK If report descriptor is successfully parsed
344 */
345int usb_hid_parse_report_descriptor(usb_hid_report_t *report,
346 const uint8_t *data, size_t size)
347{
348 size_t i=0;
349 uint8_t tag=0;
350 uint8_t item_size=0;
351 int class=0;
352 int ret;
353 usb_hid_report_item_t *report_item=0;
354 usb_hid_report_item_t *new_report_item;
355 usb_hid_report_path_t *usage_path;
356
357 size_t offset_input=0;
358 size_t offset_output=0;
359 size_t offset_feature=0;
360
361 link_t stack;
362 list_initialize(&stack);
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));
374 list_initialize(&(report_item->link));
375
376 /* usage path context initialization */
377 if(!(usage_path=usb_hid_report_path())){
378 return ENOMEM;
379 }
380 usb_hid_report_path_append_item(usage_path, 0, 0);
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,
394 item_size,report_item, usage_path);
395
396 switch(ret){
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;
402
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 }
409
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;
423
424 report_item->offset = offset_output;
425 offset_output += report_item->count *
426 report_item->size;
427 break;
428
429 case USB_HID_REPORT_TAG_FEATURE:
430 report_item->type =
431 USB_HID_REPORT_TYPE_FEATURE;
432
433 report_item->offset = offset_feature;
434 offset_feature += report_item->count *
435 report_item->size;
436 break;
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);
469
470 new_report_item->usage_path = tmp_path;
471
472 list_prepend (&new_report_item->link, &stack);
473 break;
474 case USB_HID_REPORT_TAG_POP:
475 // restore current state from stack
476 if(list_empty (&stack)) {
477 return EINVAL;
478 }
479 free(report_item);
480
481 report_item = list_get_instance(stack.next,
482 usb_hid_report_item_t, link);
483
484 usb_hid_report_usage_path_t *tmp_usage_path;
485 tmp_usage_path = list_get_instance(
486 report_item->usage_path->link.prev,
487 usb_hid_report_usage_path_t, link);
488
489 usb_hid_report_set_last_item(usage_path,
490 USB_HID_TAG_CLASS_GLOBAL, tmp_usage_path->usage_page);
491
492 usb_hid_report_set_last_item(usage_path,
493 USB_HID_TAG_CLASS_LOCAL, tmp_usage_path->usage);
494
495 usb_hid_report_path_free(report_item->usage_path);
496 list_initialize(&report_item->usage_path->link);
497 list_remove (stack.next);
498
499 break;
500
501 default:
502 // nothing special to do
503 break;
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
520/*---------------------------------------------------------------------------*/
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 */
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
535 int ret;
536
537 switch(class){
538 case USB_HID_TAG_CLASS_MAIN:
539
540 if((ret=usb_hid_report_parse_main_tag(tag, data, item_size,
541 report_item, usage_path)) == EOK) {
542
543 return USB_HID_NEW_REPORT_ITEM;
544 }
545 else {
546 return ret;
547 }
548 break;
549
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;
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
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)
578{
579 usb_hid_report_usage_path_t *path_item;
580
581 switch(tag)
582 {
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;
589
590 case USB_HID_REPORT_TAG_COLLECTION:
591
592 /* store collection atributes */
593 path_item = list_get_instance(usage_path->head.prev,
594 usb_hid_report_usage_path_t, link);
595 path_item->flags = *data;
596
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]));
607
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;
617
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;
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 */
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
643 switch(tag)
644 {
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;
708
709 default:
710 return USB_HID_NO_ACTION;
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 */
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)
728{
729 int32_t extended_usage;
730
731 switch(tag) {
732 case USB_HID_REPORT_TAG_USAGE:
733 switch(report_item->in_delimiter) {
734 case INSIDE_DELIMITER_SET:
735 /* nothing to do
736 * we catch only the first one
737 */
738 break;
739
740 case START_DELIMITER_SET:
741 report_item->in_delimiter = INSIDE_DELIMITER_SET;
742 case OUTSIDE_DELIMITER_SET:
743 extended_usage = ((report_item->usage_page) << 16);
744 extended_usage += usb_hid_report_tag_data_uint32(
745 data,item_size);
746
747 report_item->usages[report_item->usages_count] =
748 extended_usage;
749
750 report_item->usages_count++;
751 break;
752 }
753 break;
754
755 case USB_HID_REPORT_TAG_USAGE_MINIMUM:
756 if (item_size == 3) {
757 // usage extended usages
758 report_item->extended_usage_page =
759 USB_HID_EXTENDED_USAGE_PAGE(
760 usb_hid_report_tag_data_uint32(data,item_size));
761
762
763 report_item->usage_minimum =
764 USB_HID_EXTENDED_USAGE(
765 usb_hid_report_tag_data_uint32(data,item_size));
766 }
767 else {
768 report_item->usage_minimum =
769 usb_hid_report_tag_data_uint32(data,item_size);
770 }
771 break;
772
773 case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
774 if (item_size == 3) {
775 if(report_item->extended_usage_page !=
776 USB_HID_EXTENDED_USAGE_PAGE(
777 usb_hid_report_tag_data_uint32(data,item_size))) {
778
779 return EINVAL;
780 }
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 }
791 else {
792 report_item->usage_maximum =
793 usb_hid_report_tag_data_uint32(data,item_size);
794 }
795
796 // vlozit zaznamy do pole usages
797 int32_t i;
798 for(i = report_item->usage_minimum;
799 i <= report_item->usage_maximum; i++) {
800
801 if(report_item->extended_usage_page) {
802 report_item->usages[report_item->usages_count++] =
803 (report_item->extended_usage_page << 16) + i;
804 }
805 else {
806 report_item->usages[report_item->usages_count++] =
807 (report_item->usage_page << 16) + i;
808 }
809 }
810 report_item->extended_usage_page = 0;
811
812 break;
813
814 case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
815 report_item->designator_index =
816 usb_hid_report_tag_data_uint32(data,item_size);
817 break;
818
819 case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
820 report_item->designator_minimum =
821 usb_hid_report_tag_data_uint32(data,item_size);
822 break;
823
824 case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
825 report_item->designator_maximum =
826 usb_hid_report_tag_data_uint32(data,item_size);
827 break;
828
829 case USB_HID_REPORT_TAG_STRING_INDEX:
830 report_item->string_index =
831 usb_hid_report_tag_data_uint32(data,item_size);
832 break;
833
834 case USB_HID_REPORT_TAG_STRING_MINIMUM:
835 report_item->string_minimum =
836 usb_hid_report_tag_data_uint32(data,item_size);
837 break;
838
839 case USB_HID_REPORT_TAG_STRING_MAXIMUM:
840 report_item->string_maximum =
841 usb_hid_report_tag_data_uint32(data,item_size);
842 break;
843
844 case USB_HID_REPORT_TAG_DELIMITER:
845 report_item->in_delimiter =
846 usb_hid_report_tag_data_uint32(data,item_size);
847 break;
848
849 default:
850 return USB_HID_NO_ACTION;
851 }
852
853 return EOK;
854}
855/*---------------------------------------------------------------------------*/
856
857/**
858 * Converts raw data to uint32 (thats the maximum length of short item data)
859 *
860 * @param Data buffer
861 * @param Size of buffer
862 * @return Converted int32 number
863 */
864uint32_t usb_hid_report_tag_data_uint32(const uint8_t *data, size_t size)
865{
866 unsigned int i;
867 uint32_t result;
868
869 result = 0;
870 for(i=0; i<size; i++) {
871 result = (result | (data[i]) << (i*8));
872 }
873
874 return result;
875}
876/*---------------------------------------------------------------------------*/
877
878/**
879 * Prints content of given list of report items.
880 *
881 * @param List of report items (usb_hid_report_item_t)
882 * @return void
883 */
884void usb_hid_descriptor_print_list(link_t *head)
885{
886 usb_hid_report_field_t *report_item;
887 link_t *item;
888
889
890 if(head == NULL || list_empty(head)) {
891 usb_log_debug("\tempty\n");
892 return;
893 }
894
895 for(item = head->next; item != head; item = item->next) {
896
897 report_item = list_get_instance(item, usb_hid_report_field_t,
898 link);
899
900 usb_log_debug("\t\tOFFSET: %X\n", report_item->offset);
901 usb_log_debug("\t\tSIZE: %zu\n", report_item->size);
902 usb_log_debug("\t\tLOGMIN: %d\n",
903 report_item->logical_minimum);
904 usb_log_debug("\t\tLOGMAX: %d\n",
905 report_item->logical_maximum);
906 usb_log_debug("\t\tPHYMIN: %d\n",
907 report_item->physical_minimum);
908 usb_log_debug("\t\tPHYMAX: %d\n",
909 report_item->physical_maximum);
910 usb_log_debug("\t\ttUSAGEMIN: %X\n",
911 report_item->usage_minimum);
912 usb_log_debug("\t\tUSAGEMAX: %X\n",
913 report_item->usage_maximum);
914 usb_log_debug("\t\tUSAGES COUNT: %zu\n",
915 report_item->usages_count);
916
917 usb_log_debug("\t\tVALUE: %X\n", report_item->value);
918 usb_log_debug("\t\ttUSAGE: %X\n", report_item->usage);
919 usb_log_debug("\t\tUSAGE PAGE: %X\n", report_item->usage_page);
920
921 usb_hid_print_usage_path(report_item->collection_path);
922
923 usb_log_debug("\n");
924
925 }
926
927}
928/*---------------------------------------------------------------------------*/
929
930/**
931 * Prints content of given report descriptor in human readable format.
932 *
933 * @param parser Parsed descriptor to print
934 * @return void
935 */
936void usb_hid_descriptor_print(usb_hid_report_t *report)
937{
938 if(report == NULL) {
939 return;
940 }
941
942 link_t *report_it = report->reports.next;
943 usb_hid_report_description_t *report_des;
944
945 while(report_it != &report->reports) {
946 report_des = list_get_instance(report_it,
947 usb_hid_report_description_t, link);
948 usb_log_debug("Report ID: %d\n", report_des->report_id);
949 usb_log_debug("\tType: %d\n", report_des->type);
950 usb_log_debug("\tLength: %zu\n", report_des->bit_length);
951 usb_log_debug("\tB Size: %zu\n",
952 usb_hid_report_byte_size(report,
953 report_des->report_id,
954 report_des->type));
955 usb_log_debug("\tItems: %zu\n", report_des->item_length);
956
957 usb_hid_descriptor_print_list(&report_des->report_items);
958
959 report_it = report_it->next;
960 }
961}
962/*---------------------------------------------------------------------------*/
963
964/**
965 * Releases whole linked list of report items
966 *
967 * @param head Head of list of report descriptor items (usb_hid_report_item_t)
968 * @return void
969 */
970void usb_hid_free_report_list(link_t *head)
971{
972 return;
973
974 usb_hid_report_item_t *report_item;
975 link_t *next;
976
977 if(head == NULL || list_empty(head)) {
978 return;
979 }
980
981 next = head->next;
982 while(next != head) {
983
984 report_item = list_get_instance(next, usb_hid_report_item_t, link);
985
986 while(!list_empty(&report_item->usage_path->link)) {
987 usb_hid_report_remove_last_item(report_item->usage_path);
988 }
989
990
991 next = next->next;
992
993 free(report_item);
994 }
995
996 return;
997
998}
999/*---------------------------------------------------------------------------*/
1000
1001/** Frees the HID report descriptor parser structure
1002 *
1003 * @param parser Opaque HID report parser structure
1004 * @return void
1005 */
1006void usb_hid_free_report(usb_hid_report_t *report)
1007{
1008 if(report == NULL){
1009 return;
1010 }
1011
1012 // free collection paths
1013 usb_hid_report_path_t *path;
1014 while(!list_empty(&report->collection_paths)) {
1015 path = list_get_instance(report->collection_paths.next,
1016 usb_hid_report_path_t, link);
1017
1018 usb_hid_report_path_free(path);
1019 }
1020
1021 // free report items
1022 usb_hid_report_description_t *report_des;
1023 usb_hid_report_field_t *field;
1024 while(!list_empty(&report->reports)) {
1025 report_des = list_get_instance(report->reports.next,
1026 usb_hid_report_description_t, link);
1027
1028 list_remove(&report_des->link);
1029
1030 while(!list_empty(&report_des->report_items)) {
1031 field = list_get_instance(
1032 report_des->report_items.next,
1033 usb_hid_report_field_t, link);
1034
1035 list_remove(&field->link);
1036
1037 free(field);
1038 }
1039
1040 free(report_des);
1041 }
1042
1043 return;
1044}
1045/*---------------------------------------------------------------------------*/
1046
1047/**
1048 * @}
1049 */
Note: See TracBrowser for help on using the repository browser.