source: mainline/uspace/lib/usb/src/hidparser.c@ 3de529c

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

delimiter descriptor field is supported now

  • Property mode set to 100644
File size: 18.1 KB
Line 
1/*
2 * Copyright (c) 2010 Vojtech Horky
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 libusb
30 * @{
31 */
32/** @file
33 * @brief HID parser implementation.
34 */
35#include <usb/classes/hidparser.h>
36#include <errno.h>
37#include <stdio.h>
38#include <malloc.h>
39#include <mem.h>
40#include <usb/debug.h>
41
42#define USB_HID_NEW_REPORT_ITEM 1
43#define USB_HID_NO_ACTION 2
44#define USB_HID_UNKNOWN_TAG -99
45
46#define BAD_HACK_USAGE_PAGE 0x07
47
48int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
49 usb_hid_report_item_t *report_item);
50int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
51 usb_hid_report_item_t *report_item);
52int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
53 usb_hid_report_item_t *report_item);
54int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
55 usb_hid_report_item_t *report_item);
56
57void usb_hid_descriptor_print_list(link_t *head);
58int usb_hid_report_reset_local_items();
59void usb_hid_free_report_list(link_t *head);
60int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size);
61inline size_t usb_hid_count_item_offset(usb_hid_report_item_t * report_item, size_t offset);
62int usb_hid_translate_data(usb_hid_report_item_t *item, const uint8_t *data, size_t j);
63int usb_pow(int a, int b);
64
65int usb_pow(int a, int b)
66{
67 switch(b) {
68 case 0:
69 return 1;
70 break;
71 case 1:
72 return a;
73 break;
74 default:
75 return a * usb_pow(a, b-1);
76 break;
77 }
78}
79
80/**
81 *
82 */
83int usb_hid_parser_init(usb_hid_report_parser_t *parser)
84{
85 if(parser == NULL) {
86 return -1;
87 }
88
89 list_initialize(&(parser->input));
90 list_initialize(&(parser->output));
91 list_initialize(&(parser->feature));
92
93 return EOK;
94}
95
96
97/** Parse HID report descriptor.
98 *
99 * @param parser Opaque HID report parser structure.
100 * @param data Data describing the report.
101 * @return Error code.
102 */
103int usb_hid_parse_report_descriptor(usb_hid_report_parser_t *parser,
104 const uint8_t *data, size_t size)
105{
106 size_t i=0;
107 uint8_t tag=0;
108 uint8_t item_size=0;
109 int class=0;
110 int ret;
111 usb_hid_report_item_t *report_item=0;
112 usb_hid_report_item_t *new_report_item;
113
114 size_t offset=0;
115
116
117 if(!(report_item=malloc(sizeof(usb_hid_report_item_t)))){
118 return ENOMEM;
119 }
120 link_initialize(&(report_item->link));
121
122 while(i<size){
123 if(!USB_HID_ITEM_IS_LONG(data[i])){
124
125 if((i+1) >= size){
126 return -1; // TODO ERROR CODE
127 }
128
129 tag = USB_HID_ITEM_TAG(data[i]);
130 item_size = USB_HID_ITEM_SIZE(data[i]);
131 class = USB_HID_ITEM_TAG_CLASS(data[i]);
132
133 usb_log_debug2(
134 "i(%u) data(%X) value(%X): TAG %u, class %u, size %u - ", i,
135 data[i], usb_hid_report_tag_data_int32(data+i+1,item_size),
136 tag, class, item_size);
137
138 ret = usb_hid_report_parse_tag(tag,class,data+i+1,
139 item_size,report_item);
140 usb_log_debug2("ret: %u\n", ret);
141 switch(ret){
142 case USB_HID_NEW_REPORT_ITEM:
143 // store report item to report and create the new one
144 usb_log_debug("\nNEW REPORT ITEM: %X",tag);
145
146 report_item->offset = offset;
147 offset += report_item->count * report_item->size;
148
149 switch(tag) {
150 case USB_HID_REPORT_TAG_INPUT:
151 usb_log_debug(" - INPUT\n");
152 list_append(&(report_item->link), &(parser->input));
153 break;
154 case USB_HID_REPORT_TAG_OUTPUT:
155 usb_log_debug(" - OUTPUT\n");
156 list_append(&(report_item->link), &(parser->output));
157
158 break;
159 case USB_HID_REPORT_TAG_FEATURE:
160 usb_log_debug(" - FEATURE\n");
161 list_append(&(report_item->link), &(parser->feature));
162 break;
163 default:
164 usb_log_debug("\tjump over - tag %X\n", tag);
165 break;
166 }
167
168 /* clone current state table to the new item */
169 if(!(new_report_item = malloc(sizeof(usb_hid_report_item_t)))) {
170 return ENOMEM;
171 }
172 memcpy(new_report_item,report_item, sizeof(usb_hid_report_item_t));
173 link_initialize(&(new_report_item->link));
174 report_item = new_report_item;
175
176 break;
177 case USB_HID_REPORT_TAG_PUSH:
178 // push current state to stack
179 // not yet implemented
180 break;
181 case USB_HID_REPORT_TAG_POP:
182 // restore current state from stack
183 // not yet implemented
184 break;
185
186 default:
187 // nothing special to do
188 break;
189 }
190
191 /* jump over the processed block */
192 i += 1 + USB_HID_ITEM_SIZE(data[i]);
193 }
194 else{
195 // TBD
196 i += 3 + USB_HID_ITEM_SIZE(data[i+1]);
197 }
198
199
200 }
201
202
203 return EOK;
204}
205
206
207/**
208 * Parse input report.
209 *
210 * @param data Data for report
211 * @param size Size of report
212 * @param callbacks Callbacks for report actions
213 * @param arg Custom arguments
214 *
215 * @return Error code
216 */
217int usb_hid_boot_keyboard_input_report(const uint8_t *data, size_t size,
218 const usb_hid_report_in_callbacks_t *callbacks, void *arg)
219{
220 int i;
221 usb_hid_report_item_t item;
222
223 /* fill item due to the boot protocol report descriptor */
224 // modifier keys are in the first byte
225 uint8_t modifiers = data[0];
226
227 item.offset = 2; /* second byte is reserved */
228 item.size = 8;
229 item.count = 6;
230 item.usage_minimum = 0;
231 item.usage_maximum = 255;
232 item.logical_minimum = 0;
233 item.logical_maximum = 255;
234
235 if (size != 8) {
236 return -1; //ERANGE;
237 }
238
239 uint8_t keys[6];
240 for (i = 0; i < item.count; i++) {
241 keys[i] = data[i + item.offset];
242 }
243
244 callbacks->keyboard(keys, 6, modifiers, arg);
245 return EOK;
246}
247
248/**
249 * Makes output report for keyboard boot protocol
250 *
251 * @param leds
252 * @param output Output report data buffer
253 * @param size Size of the output buffer
254 * @return Error code
255 */
256int usb_hid_boot_keyboard_output_report(uint8_t leds, uint8_t *data, size_t size)
257{
258 if(size != 1){
259 return -1;
260 }
261
262 /* used only first five bits, others are only padding*/
263 *data = leds;
264 return EOK;
265}
266
267/**
268 *
269 * @param Tag to parse
270 * @param Report descriptor buffer
271 * @param Size of data belongs to this tag
272 * @param Current report item structe
273 * @return Code of action to be done next
274 */
275int usb_hid_report_parse_tag(uint8_t tag, uint8_t class, const uint8_t *data, size_t item_size,
276 usb_hid_report_item_t *report_item)
277{
278 int ret;
279
280 switch(class){
281 case USB_HID_TAG_CLASS_MAIN:
282
283 if((ret=usb_hid_report_parse_main_tag(tag,data,item_size,report_item)) == EOK) {
284 return USB_HID_NEW_REPORT_ITEM;
285 }
286 else {
287 /*TODO process the error */
288 return ret;
289 }
290 break;
291
292 case USB_HID_TAG_CLASS_GLOBAL:
293 return usb_hid_report_parse_global_tag(tag,data,item_size,report_item);
294 break;
295
296 case USB_HID_TAG_CLASS_LOCAL:
297 return usb_hid_report_parse_local_tag(tag,data,item_size,report_item);
298 break;
299 default:
300 return USB_HID_NO_ACTION;
301 }
302}
303
304/**
305 * Parse main tags of report descriptor
306 *
307 * @param Tag identifier
308 * @param Data buffer
309 * @param Length of data buffer
310 * @param Current state table
311 * @return Error code
312 */
313
314int usb_hid_report_parse_main_tag(uint8_t tag, const uint8_t *data, size_t item_size,
315 usb_hid_report_item_t *report_item)
316{
317 switch(tag)
318 {
319 case USB_HID_REPORT_TAG_INPUT:
320 case USB_HID_REPORT_TAG_OUTPUT:
321 case USB_HID_REPORT_TAG_FEATURE:
322 report_item->item_flags = *data;
323 return EOK;
324 break;
325
326 case USB_HID_REPORT_TAG_COLLECTION:
327 // TODO
328 break;
329
330 case USB_HID_REPORT_TAG_END_COLLECTION:
331 /* should be ignored */
332 break;
333 default:
334 return USB_HID_NO_ACTION;
335 }
336
337 return USB_HID_NO_ACTION;
338}
339
340/**
341 * Parse global tags of report descriptor
342 *
343 * @param Tag identifier
344 * @param Data buffer
345 * @param Length of data buffer
346 * @param Current state table
347 * @return Error code
348 */
349
350int usb_hid_report_parse_global_tag(uint8_t tag, const uint8_t *data, size_t item_size,
351 usb_hid_report_item_t *report_item)
352{
353 // TODO take care about the bit length of data
354 switch(tag)
355 {
356 case USB_HID_REPORT_TAG_USAGE_PAGE:
357 report_item->usage_page = usb_hid_report_tag_data_int32(data,item_size);
358 break;
359 case USB_HID_REPORT_TAG_LOGICAL_MINIMUM:
360 report_item->logical_minimum = usb_hid_report_tag_data_int32(data,item_size);
361 break;
362 case USB_HID_REPORT_TAG_LOGICAL_MAXIMUM:
363 report_item->logical_maximum = usb_hid_report_tag_data_int32(data,item_size);
364 break;
365 case USB_HID_REPORT_TAG_PHYSICAL_MINIMUM:
366 report_item->physical_minimum = usb_hid_report_tag_data_int32(data,item_size);
367 break;
368 case USB_HID_REPORT_TAG_PHYSICAL_MAXIMUM:
369 report_item->physical_maximum = usb_hid_report_tag_data_int32(data,item_size);
370 break;
371 case USB_HID_REPORT_TAG_UNIT_EXPONENT:
372 report_item->unit_exponent = usb_hid_report_tag_data_int32(data,item_size);
373 break;
374 case USB_HID_REPORT_TAG_UNIT:
375 report_item->unit = usb_hid_report_tag_data_int32(data,item_size);
376 break;
377 case USB_HID_REPORT_TAG_REPORT_SIZE:
378 report_item->size = usb_hid_report_tag_data_int32(data,item_size);
379 break;
380 case USB_HID_REPORT_TAG_REPORT_COUNT:
381 report_item->count = usb_hid_report_tag_data_int32(data,item_size);
382 break;
383 case USB_HID_REPORT_TAG_REPORT_ID:
384 report_item->id = usb_hid_report_tag_data_int32(data,item_size);
385 break;
386 case USB_HID_REPORT_TAG_PUSH:
387 case USB_HID_REPORT_TAG_POP:
388 return tag;
389 break;
390
391 default:
392 return USB_HID_NO_ACTION;
393 }
394
395 return EOK;
396}
397
398/**
399 * Parse local tags of report descriptor
400 *
401 * @param Tag identifier
402 * @param Data buffer
403 * @param Length of data buffer
404 * @param Current state table
405 * @return Error code
406 */
407int usb_hid_report_parse_local_tag(uint8_t tag, const uint8_t *data, size_t item_size,
408 usb_hid_report_item_t *report_item)
409{
410 switch(tag)
411 {
412 case USB_HID_REPORT_TAG_USAGE:
413 report_item->usage = usb_hid_report_tag_data_int32(data,item_size);
414 break;
415 case USB_HID_REPORT_TAG_USAGE_MINIMUM:
416 report_item->usage_minimum = usb_hid_report_tag_data_int32(data,item_size);
417 break;
418 case USB_HID_REPORT_TAG_USAGE_MAXIMUM:
419 report_item->usage_maximum = usb_hid_report_tag_data_int32(data,item_size);
420 break;
421 case USB_HID_REPORT_TAG_DESIGNATOR_INDEX:
422 report_item->designator_index = usb_hid_report_tag_data_int32(data,item_size);
423 break;
424 case USB_HID_REPORT_TAG_DESIGNATOR_MINIMUM:
425 report_item->designator_minimum = usb_hid_report_tag_data_int32(data,item_size);
426 break;
427 case USB_HID_REPORT_TAG_DESIGNATOR_MAXIMUM:
428 report_item->designator_maximum = usb_hid_report_tag_data_int32(data,item_size);
429 break;
430 case USB_HID_REPORT_TAG_STRING_INDEX:
431 report_item->string_index = usb_hid_report_tag_data_int32(data,item_size);
432 break;
433 case USB_HID_REPORT_TAG_STRING_MINIMUM:
434 report_item->string_minimum = usb_hid_report_tag_data_int32(data,item_size);
435 break;
436 case USB_HID_REPORT_TAG_STRING_MAXIMUM:
437 report_item->string_maximum = usb_hid_report_tag_data_int32(data,item_size);
438 break;
439 case USB_HID_REPORT_TAG_DELIMITER:
440 report_item->delimiter = usb_hid_report_tag_data_int32(data,item_size);
441 break;
442
443 default:
444 return USB_HID_NO_ACTION;
445 }
446
447 return EOK;
448}
449
450/**
451 * Converts raw data to int32 (thats the maximum length of short item data)
452 *
453 * @param Data buffer
454 * @param Size of buffer
455 * @return Converted int32 number
456 */
457int32_t usb_hid_report_tag_data_int32(const uint8_t *data, size_t size)
458{
459 unsigned int i;
460 int32_t result;
461
462 result = 0;
463 for(i=0; i<size; i++) {
464 result = (result | (data[i]) << (i*8));
465 }
466
467 return result;
468}
469
470
471
472/**
473 * Prints content of given list of report items.
474 *
475 * @param List of report items
476 * @return void
477 */
478void usb_hid_descriptor_print_list(link_t *head)
479{
480 usb_hid_report_item_t *report_item;
481 link_t *item;
482
483 if(head == NULL || list_empty(head)) {
484 printf("\tempty\n");
485 return;
486 }
487
488 for(item = head->next; item != head; item = item->next) {
489
490 report_item = list_get_instance(item, usb_hid_report_item_t, link);
491
492 printf("\tOFFSET: %X\n", report_item->offset);
493 printf("\tCOUNT: %X\n", report_item->count);
494 printf("\tSIZE: %X\n", report_item->size);
495 printf("\tCONSTANT: %X\n", USB_HID_ITEM_FLAG_CONSTANT(report_item->item_flags));
496 printf("\tUSAGE: %X\n", report_item->usage);
497 printf("\tUSAGE PAGE: %X\n", report_item->usage_page);
498 printf("\tLOGMIN: %X\n", report_item->logical_minimum);
499 printf("\tLOGMAX: %X\n", report_item->logical_maximum);
500 printf("\tPHYMIN: %X\n", report_item->physical_minimum);
501 printf("\tPHYMAX: %X\n", report_item->physical_maximum);
502 printf("\n");
503
504 }
505
506
507}
508/**
509 * Prints content of given descriptor in human readable format.
510 *
511 * @param Parsed descriptor to print
512 * @return void
513 */
514void usb_hid_descriptor_print(usb_hid_report_parser_t *parser)
515{
516 printf("INPUT:\n");
517 usb_hid_descriptor_print_list(&parser->input);
518
519 printf("OUTPUT: \n");
520 usb_hid_descriptor_print_list(&parser->output);
521
522 printf("FEATURE:\n");
523 usb_hid_descriptor_print_list(&parser->feature);
524
525}
526
527/**
528 * Releases whole linked list of report items
529 *
530 *
531 */
532void usb_hid_free_report_list(link_t *head)
533{
534 return;
535
536 usb_hid_report_item_t *report_item;
537 link_t *next;
538
539 if(head == NULL || list_empty(head)) {
540 return;
541 }
542
543 next = head->next;
544 while(next != head) {
545
546 report_item = list_get_instance(next, usb_hid_report_item_t, link);
547 next = next->next;
548
549 free(report_item);
550 }
551
552 return;
553
554}
555
556/** Free the HID report parser structure
557 *
558 * @param parser Opaque HID report parser structure
559 * @return Error code
560 */
561void usb_hid_free_report_parser(usb_hid_report_parser_t *parser)
562{
563 if(parser == NULL){
564 return;
565 }
566
567 usb_hid_free_report_list(&parser->input);
568 usb_hid_free_report_list(&parser->output);
569 usb_hid_free_report_list(&parser->feature);
570
571 return;
572}
573
574/** Parse and act upon a HID report.
575 *
576 * @see usb_hid_parse_report_descriptor
577 *
578 * @param parser Opaque HID report parser structure.
579 * @param data Data for the report.
580 * @param callbacks Callbacks for report actions.
581 * @param arg Custom argument (passed through to the callbacks).
582 * @return Error code.
583 */
584int usb_hid_parse_report(const usb_hid_report_parser_t *parser,
585 const uint8_t *data, size_t size,
586 const usb_hid_report_in_callbacks_t *callbacks, void *arg)
587{
588 /*
589 *
590 * only key codes (usage page 0x07) will be processed
591 * other usages will be ignored
592 */
593 link_t *list_item;
594 usb_hid_report_item_t *item;
595 uint8_t *keys;
596 size_t key_count=0;
597 size_t i=0;
598 size_t j=0;
599
600 // get the size of result keycodes array
601 list_item = parser->input.next;
602 while(list_item != &(parser->input)) {
603
604 item = list_get_instance(list_item, usb_hid_report_item_t, link);
605 if(item->usage_page == BAD_HACK_USAGE_PAGE) {
606 key_count += item->count;
607 }
608
609 list_item = list_item->next;
610 }
611
612
613 if(!(keys = malloc(sizeof(uint8_t) * key_count))){
614 return ENOMEM;
615 }
616
617 // read data
618 list_item = parser->input.next;
619 while(list_item != &(parser->input)) {
620
621 item = list_get_instance(list_item, usb_hid_report_item_t, link);
622 if(item->usage_page == BAD_HACK_USAGE_PAGE) {
623 for(j=0; j<(size_t)(item->count); j++) {
624 keys[i++] = usb_hid_translate_data(item, data,j);
625 }
626 }
627 list_item = list_item->next;
628 }
629
630 callbacks->keyboard(keys, key_count, 0, arg);
631
632 free(keys);
633 return EOK;
634
635}
636
637
638int usb_hid_translate_data(usb_hid_report_item_t *item, const uint8_t *data, size_t j)
639{
640 int resolution;
641 int offset;
642 int part_size;
643
644 int32_t value;
645 int32_t mask;
646 const uint8_t *foo;
647
648 // now only common numbers llowed
649 if(item->size > 32) {
650 return 0;
651 }
652
653 if((item->physical_minimum == 0) && (item->physical_maximum ==0)) {
654 item->physical_minimum = item->logical_minimum;
655 item->physical_maximum = item->logical_maximum;
656 }
657
658 resolution = (item->logical_maximum - item->logical_minimum) / ((item->physical_maximum - item->physical_minimum) * (usb_pow(10,(item->unit_exponent))));
659 offset = item->offset + (j * item->size);
660
661 // FIXME
662 if((offset/8) != ((offset+item->size)/8)) {
663 usb_log_debug2("offset %d\n", offset);
664
665 part_size = ((offset+item->size)%8);
666 usb_log_debug2("part size %d\n",part_size);
667
668 // the higher one
669 foo = data+(offset/8);
670 mask = ((1 << (item->size-part_size))-1);
671 value = (*foo & mask) << part_size;
672
673 usb_log_debug2("hfoo %x\n", *foo);
674 usb_log_debug2("hmaska %x\n", mask);
675 usb_log_debug2("hval %d\n", value);
676
677 // the lower one
678 foo = data+((offset+item->size)/8);
679 mask = ((1 << part_size)-1) << (8-part_size);
680 value += ((*foo & mask) >> (8-part_size));
681
682 usb_log_debug2("lfoo %x\n", *foo);
683 usb_log_debug2("lmaska %x\n", mask);
684 usb_log_debug2("lval %d\n", ((*foo & mask) >> (8-(item->size-part_size))));
685 usb_log_debug2("val %d\n", value);
686
687
688 }
689 else {
690 foo = data+(offset/8);
691 mask = ((1 << item->size)-1) << (8-((offset%8)+item->size));
692 value = (*foo & mask) >> (8-((offset%8)+item->size));
693
694 usb_log_debug2("offset %d\n", offset);
695 usb_log_debug2("foo %x\n", *foo);
696 usb_log_debug2("maska %x\n", mask);
697 usb_log_debug2("val %d\n", value);
698 }
699
700 usb_log_debug2("---\n\n");
701
702 return (int)(((value - item->logical_minimum) / resolution) + item->physical_minimum);
703
704}
705/**
706 * @}
707 */
Note: See TracBrowser for help on using the repository browser.