1 /*
2 ********************************************************************************
3 * USB Hid Driver
4 *
5 * (c) Copyright 2006-2010, All winners Co,Ld.
6 * All Right Reserved
7 *
8 * FileName : HidProtocol.c
9 *
10 * Author : Javen
11 *
12 * Date : 2010/06/02
13 *
14 * Description : Hid 协议
15 *
16 * Others : NULL
17 *
18 * History:
19 * <time> <author> <version > <desc>
20 * 2010.06.02 Javen 1.0 build this file
21 *
22 ********************************************************************************
23 */
24
25 //#include "usb_host_config.h"
26 //#include "usb_host_base_types.h"
27 #include "usb_os_platform.h"
28 #include "error.h"
29
30 #include "usb_host_common.h"
31 #include "usb_msg.h"
32 #include "urb.h"
33
34 #include "HidSpec.h"
35 #include "Hid_i.h"
36 #include "HidProtocol.h"
37 #include "HidTransport.h"
38 #include "sunxi_hal_common.h"
39 //#define USBH_HID_PRINT_REPORT
40
41 #ifdef USBH_HID_PRINT_REPORT
42
print_field(usbHidField_t * Field)43 static void print_field(usbHidField_t *Field)
44 {
45 unsigned int i = 0;
46
47 hal_log_info("\n------------------field---------------\n");
48 hal_log_info("Index = %d\n", Field->Index);
49 hal_log_info("HidReport = %x\n", Field->HidReport);
50 hal_log_info("physical = %x\n", Field->physical);
51 hal_log_info("logical = %x\n", Field->logical);
52 hal_log_info("application = %x\n", Field->application);
53
54 for (i = 0; i < Field->maxusage; i++) {
55 hal_log_info("\n");
56 hal_log_info("usage[%d].hid = %x\n", i, Field->usage[i].hid);
57 hal_log_info("usage[%d].collection_index = %x\n", i, Field->usage[i].collection_index);
58 hal_log_info("usage[%d].code = %x\n", i, Field->usage[i].code);
59 hal_log_info("usage[%d].type = %x\n", i, Field->usage[i].type);
60 hal_log_info("usage[%d].hat_min = %x\n", i, Field->usage[i].hat_min);
61 hal_log_info("usage[%d].hat_max = %x\n", i, Field->usage[i].hat_max);
62 hal_log_info("usage[%d].hat_dir = %x\n", i, Field->usage[i].hat_dir);
63 }
64
65 hal_log_info("usage = %x\n", Field->usage);
66 hal_log_info("maxusage = %x\n", Field->maxusage);
67 hal_log_info("flags = %x\n", Field->flags);
68 hal_log_info("report_offset = %x\n", Field->report_offset);
69 hal_log_info("report_size = %x\n", Field->report_size);
70 hal_log_info("report_count = %x\n", Field->report_count);
71 hal_log_info("report_type = %x\n", Field->report_type);
72 hal_log_info("value = %x\n", Field->value);
73 hal_log_info("logical_maximum = %x\n", Field->logical_maximum);
74 hal_log_info("logical_minimum = %x\n", Field->logical_minimum);
75 hal_log_info("physical_maximum = %x\n", Field->physical_maximum);
76 hal_log_info("physical_minimum = %x\n", Field->physical_minimum);
77 hal_log_info("unit_exponent = %x\n", Field->unit_exponent);
78 hal_log_info("unit = %x\n", Field->unit);
79 hal_log_info("--------------------------------------\n");
80 }
81
print_item(usbHidItem_t * item)82 static void print_item(usbHidItem_t *item)
83 {
84 hal_log_info("\n------------------item-----------------\n");
85 hal_log_info("format = %d\n", item->format);
86 hal_log_info("size = %d\n", item->size);
87 hal_log_info("type = %d\n", item->type);
88 hal_log_info("tag = %d\n", item->tag);
89 hal_log_info("data = %x\n", item->data);
90 hal_log_info("--------------------------------------\n");
91
92 }
93
print_Report(usbHidReport_t * Report)94 static void print_Report(usbHidReport_t *Report)
95 {
96 unsigned int i = 0;
97 unsigned int j = 0;
98
99 if(Report == NULL){
100 hal_log_err("Report is NULL\n");
101 return;
102 }
103
104 hal_log_info("\n");
105
106 hal_log_info("\n Report num = %d\n", i);
107 hal_log_info("Report Id = %d\n", Report->Id);
108 hal_log_info("Report Type = %d\n", Report->Type);
109 hal_log_info("Report Maxfield = %d\n", Report->Maxfield);
110
111 for(j = 0; j < Report->Maxfield; j++){
112 hal_log_info("\n field num = %d\n", j);
113 print_field(Report->Field[j]);
114 }
115
116 hal_log_info("Report Size = %d\n", Report->Size);
117 }
118
print_ReportEnum(HidDev_t * HidDev)119 static void print_ReportEnum(HidDev_t *HidDev)
120 {
121 unsigned int i = 0;
122 unsigned int j = 0;
123 usbHidReportEnum_t *ReportEnum = NULL;
124
125 hal_log_info("\n\n-----------print_ReportEnum--------\n");
126 for(i = 0; i < USB_HID_REPORT_TYPES; i++){
127 ReportEnum = &HidDev->HidReportEnum[i];
128 if(ReportEnum == NULL){
129 continue;
130 }
131
132 hal_log_info("\n ReportEnum num = %d\n", i);
133 hal_log_info("ReportEnum numbered = %d\n", ReportEnum->numbered);
134 hal_log_info("ReportEnum ReportNum = %d\n", ReportEnum->ReportNum);
135
136 for(j = 0; j < USB_HID_REPORT_MAX_NUM; j++){
137 print_Report(ReportEnum->Report[j]);
138 }
139 }
140 hal_log_info("-----------print_ReportEnum--------\n\n");
141 }
142
print_global(usbHidGlobalItems_t * Global)143 static void print_global(usbHidGlobalItems_t *Global)
144 {
145 hal_log_info("\n");
146 hal_log_info("UsagePage = %x\n", Global->UsagePage);
147 hal_log_info("LogicalMaximum = %d\n", Global->LogicalMaximum);
148 hal_log_info("LogicalMinimum = %d\n", Global->LogicalMinimum);
149 hal_log_info("PhysicalMinimum = %d\n", Global->PhysicalMinimum);
150 hal_log_info("PhysicalMaximum = %d\n", Global->PhysicalMaximum);
151 hal_log_info("UnitExponent = %d\n", Global->UnitExponent);
152 hal_log_info("Unit = %d\n", Global->Unit);
153 hal_log_info("ReportSize = %d\n", Global->ReportSize);
154 hal_log_info("ReportId = %d\n", Global->ReportId);
155 hal_log_info("ReportCount = %d\n", Global->ReportCount);
156 hal_log_info("Push = %x\n", Global->Push);
157 hal_log_info("Pop = %x\n", Global->Pop);
158 }
159
print_local(usbHidLocalItems_t * Local)160 static void print_local(usbHidLocalItems_t *Local)
161 {
162 unsigned int i = 0;
163
164 if(Local == NULL){
165 hal_log_err("Local is NULL\n");
166 return;
167 }
168
169 for(i = 0; i < Local->usage_index; i++){
170 hal_log_info("Local->usage[%d] = %x\n", i, Local->usage[i]);
171 }
172 /*
173 for(i = 0; i < USB_HID_MAX_USAGES; i++){
174 hal_log_info("Local->collection_index[%d] = %x\n", i, Local->collection_index[i]);
175 }
176 */
177 hal_log_info("Local->usage_index = %d\n", Local->usage_index);
178 hal_log_info("Local->usage_minimum = %d\n", Local->usage_minimum);
179 hal_log_info("Local->delimiter_depth = %d\n", Local->delimiter_depth);
180 hal_log_info("Local->delimiter_branch = %d\n", Local->delimiter_branch);
181 }
182
print_parser(usbHidParser_t * Parser)183 static void print_parser(usbHidParser_t *Parser)
184 {
185 unsigned int i = 0;
186
187 hal_log_info("\n");
188 hal_log_info("\n");
189 hal_log_info("--------------print_parser-------------\n");
190 print_global(&Parser->global);
191 for(i = 0; i < USB_HID_GLOBAL_STACK_SIZE; i++){
192 print_global(&Parser->global_stack[i]);
193 }
194
195 hal_log_info("global_stack_ptr = %d\n", Parser->global_stack_ptr);
196 print_local(&Parser->local);
197
198 for(i = 0; i < USB_HID_COLLECTION_STACK_SIZE; i++){
199 hal_log_info("collection_stack[%d] = %x\n", i, Parser->collection_stack[i]);
200 }
201
202 hal_log_info("global_stack_ptr = %d\n", Parser->collection_stack_ptr);
203 hal_log_info("--------------------------------------\n");
204 hal_log_info("\n");
205 }
206
207 #else
208
print_field(usbHidField_t * Field)209 static void print_field(usbHidField_t *Field)
210 {
211
212 }
213
print_item(usbHidItem_t * item)214 static void print_item(usbHidItem_t *item)
215 {
216
217 }
218
print_ReportEnum(HidDev_t * HidDev)219 static void print_ReportEnum(HidDev_t *HidDev)
220 {
221
222 }
223
print_local(usbHidLocalItems_t * Local)224 static void print_local(usbHidLocalItems_t *Local)
225 {
226
227 }
228
print_parser(usbHidParser_t * Parser)229 static void print_parser(usbHidParser_t *Parser)
230 {
231
232 }
233
234 #endif
235
236 /*
237 *******************************************************************************
238 * HidGetIdle
239 *
240 * Description:
241 *
242 *
243 * Parameters:
244 *
245 *
246 * Return value:
247 *
248 *
249 * note:
250 * 无
251 *
252 *******************************************************************************
253 */
HidGetClassDescriptor(HidDev_t * HidDev,unsigned int InterfaceNo,unsigned int DescriptorType,void * Buffer,unsigned int TransferLength)254 int HidGetClassDescriptor(HidDev_t *HidDev,
255 unsigned int InterfaceNo,
256 unsigned int DescriptorType,
257 void *Buffer,
258 unsigned int TransferLength)
259 {
260 int ret = 0;
261 unsigned int retries = 4; /* retry */
262
263 do {
264 ret = usb_control_msg(HidDev->pusb_dev,
265 usb_rcvctrlpipe(HidDev->pusb_dev, 0),
266 USB_REQ_GET_DESCRIPTOR,
267 USB_RECIP_INTERFACE | USB_TYPE_STANDARD | USB_DIR_IN,
268 (DescriptorType << 8),
269 InterfaceNo,
270 Buffer,
271 TransferLength,
272 USB_CTRL_GET_TIMEOUT);
273 retries--;
274 } while (ret < TransferLength && retries);
275
276 if(ret == TransferLength){
277 return USB_ERR_SUCCESS;
278 }else{
279 return USB_ERR_COMMAND_EXECUTE_FAILED;
280 }
281 }
282
283 /*
284 *******************************************************************************
285 * HidGetIdle
286 *
287 * Description:
288 *
289 *
290 * Parameters:
291 *
292 *
293 * Return value:
294 *
295 *
296 * note:
297 * 无
298 *
299 *******************************************************************************
300 */
HidGetIdle(HidDev_t * HidDev,unsigned int InterfaceNo,unsigned int ReportId,void * IdleRate)301 int HidGetIdle(HidDev_t *HidDev,
302 unsigned int InterfaceNo,
303 unsigned int ReportId,
304 void *IdleRate)
305 {
306 hal_log_err("ERR: HidGetIdle not support\n");
307
308 return USB_ERR_SUCCESS;
309 }
310
311 /*
312 *******************************************************************************
313 * HidSetIlde
314 *
315 * Description:
316 *
317 *
318 * Parameters:
319 *
320 *
321 * Return value:
322 *
323 *
324 * note:
325 * 无
326 *
327 *******************************************************************************
328 */
HidSetIlde(HidDev_t * HidDev,unsigned int InterfaceNo,unsigned int Duration,unsigned int ReportId)329 int HidSetIlde(HidDev_t *HidDev,
330 unsigned int InterfaceNo,
331 unsigned int Duration,
332 unsigned int ReportId)
333 {
334 int ret = 0;
335
336 ret = usb_control_msg(HidDev->pusb_dev,
337 usb_sndctrlpipe(HidDev->pusb_dev, 0),
338 USB_HID_REQ_SET_IDLE,
339 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
340 (Duration << 8) | ReportId,
341 InterfaceNo,
342 NULL,
343 0,
344 USB_CTRL_SET_TIMEOUT);
345 if(ret < 0){
346 hal_log_err("ERR: HidSetIlde: usb_control_msg failed\n");
347 return USB_ERR_COMMAND_EXECUTE_FAILED;
348 }
349
350 return USB_ERR_SUCCESS;
351 }
352
353 /*
354 *******************************************************************************
355 * HidGetReport
356 *
357 * Description:
358 *
359 *
360 * Parameters:
361 *
362 *
363 * Return value:
364 *
365 *
366 * note:
367 * 无
368 *
369 *******************************************************************************
370 */
HidGetReport(HidDev_t * HidDev,unsigned int InterfaceNo,unsigned int ReportType,unsigned int ReportId,unsigned int ReportLength,void * ReportData)371 int HidGetReport(HidDev_t *HidDev,
372 unsigned int InterfaceNo,
373 unsigned int ReportType,
374 unsigned int ReportId,
375 unsigned int ReportLength,
376 void *ReportData)
377 {
378 int ret = 0;
379
380 ret = usb_control_msg(HidDev->pusb_dev,
381 usb_rcvctrlpipe(HidDev->pusb_dev, 0),
382 USB_HID_HID_REQ_GET_REPORT,
383 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
384 (ReportType << 8) | ReportId,
385 InterfaceNo,
386 ReportData,
387 ReportLength,
388 USB_CTRL_GET_TIMEOUT);
389
390 if(ret == ReportLength){
391 return USB_ERR_SUCCESS;
392 }else{
393 return USB_ERR_COMMAND_EXECUTE_FAILED;
394 }
395 }
396
397 /*
398 *******************************************************************************
399 * HidSetReport
400 *
401 * Description:
402 *
403 *
404 * Parameters:
405 *
406 *
407 * Return value:
408 *
409 *
410 * note:
411 * 无
412 *
413 *******************************************************************************
414 */
HidSetReport(HidDev_t * HidDev,unsigned int InterfaceNo,unsigned int ReportType,unsigned int ReportId,unsigned int ReportLength,void * ReportData)415 int HidSetReport(HidDev_t *HidDev,
416 unsigned int InterfaceNo,
417 unsigned int ReportType,
418 unsigned int ReportId,
419 unsigned int ReportLength,
420 void *ReportData)
421 {
422 hal_log_err("ERR: HidGetIdle not support\n");
423
424 return USB_ERR_SUCCESS;
425 }
426
427 /*
428 *******************************************************************************
429 * HidGetProtocol
430 *
431 * Description:
432 *
433 *
434 * Parameters:
435 *
436 *
437 * Return value:
438 *
439 *
440 * note:
441 * 无
442 *
443 *******************************************************************************
444 */
HidGetProtocol(HidDev_t * HidDev,unsigned int InterfaceNo,void * ProtocolData)445 int HidGetProtocol(HidDev_t *HidDev, unsigned int InterfaceNo, void *ProtocolData)
446 {
447 hal_log_err("ERR: HidGetIdle not support\n");
448
449 return USB_ERR_SUCCESS;
450 }
451
452 /*
453 *******************************************************************************
454 * HidSetProtocol
455 *
456 * Description:
457 *
458 *
459 * Parameters:
460 *
461 *
462 * Return value:
463 *
464 *
465 * note:
466 * 无
467 *
468 *******************************************************************************
469 */
HidSetProtocol(HidDev_t * HidDev,unsigned int InterfaceNo,unsigned int Protocoltype)470 int HidSetProtocol(HidDev_t *HidDev, unsigned int InterfaceNo, unsigned int Protocoltype)
471 {
472 hal_log_err("ERR: HidGetIdle not support\n");
473
474 return USB_ERR_SUCCESS;
475 }
476
477 /*
478 *******************************************************************************
479 * HidRegisterReport
480 *
481 * Description:
482 * Register a new report for a device.
483 *
484 * Parameters:
485 *
486 *
487 * Return value:
488 *
489 *
490 * note:
491 * 无
492 *
493 *******************************************************************************
494 */
HidRegisterReport(HidDev_t * HidDev,unsigned int type,unsigned int id)495 static usbHidReport_t *HidRegisterReport(HidDev_t *HidDev, unsigned int type, unsigned int id)
496 {
497 usbHidReportEnum_t *ReportEnum = HidDev->HidReportEnum + type;
498 usbHidReport_t *Report = NULL;
499
500 if (ReportEnum->Report[id]){
501 return ReportEnum->Report[id];
502 }
503
504 Report = hal_malloc(sizeof(usbHidReport_t));
505 if (Report == NULL){
506 hal_log_err("ERR: malloc failed\n");
507 return NULL;
508 }
509
510 ReportEnum->ReportNum += 1;
511
512 memset(Report, 0, sizeof(usbHidReport_t));
513
514 if (id != 0){
515 ReportEnum->numbered = 1;
516 }
517
518 Report->Id = id;
519 Report->Type = type;
520 Report->Size = 0;
521 ReportEnum->Report[id] = Report;
522
523 return Report;
524 }
525
526 /*
527 *******************************************************************************
528 * HidRegisterField
529 *
530 * Description:
531 * Register a new field for this report.
532 *
533 * Parameters:
534 *
535 *
536 * Return value:
537 *
538 *
539 * note:
540 * 无
541 *
542 *******************************************************************************
543 */
HidRegisterField(usbHidReport_t * Report,unsigned int Usages,unsigned int Values)544 static usbHidField_t *HidRegisterField(usbHidReport_t *Report, unsigned int Usages, unsigned int Values)
545 {
546 usbHidField_t *Field = NULL;
547
548 if (Report->Maxfield >= USB_HID_MAX_FIELDS) {
549 hal_log_err("ERR: too many fields in report\n");
550 return NULL;
551 }
552
553 Field = (usbHidField_t *)hal_malloc(sizeof(usbHidField_t));
554 if (Field == NULL){
555 hal_log_err("ERR: malloc failed\n");
556 return NULL;
557 }
558
559 memset(Field, 0, sizeof(usbHidField_t));
560
561 Field->usage = (usbHidUsage_t *)hal_malloc(Usages * sizeof(usbHidUsage_t));
562 if (Field->usage == NULL){
563 hal_log_err("ERR: malloc failed\n");
564 goto ERR;
565 }
566 memset(Field->usage, 0, Usages * sizeof(usbHidUsage_t));
567
568 Field->value = (unsigned int *)hal_malloc(Values * sizeof(unsigned int));
569 if (Field->value == NULL){
570 hal_log_err("ERR: malloc failed\n");
571 goto ERR;
572 }
573 memset(Field->value, 0, Values * sizeof(unsigned int));
574
575 Field->Index = Report->Maxfield++;
576 Report->Field[Field->Index] = Field;
577 Field->HidReport = Report;
578
579 return Field;
580
581 ERR:
582 if(Field->usage){
583 hal_free(Field->usage);
584 Field->usage = NULL;
585 }
586
587 hal_free(Field);
588 Field = NULL;
589
590 return NULL;
591 }
592
593 /*
594 *******************************************************************************
595 * open_collection
596 *
597 * Description:
598 *
599 *
600 * Parameters:
601 *
602 *
603 * Return value:
604 *
605 *
606 * note:
607 * 无
608 *
609 *******************************************************************************
610 */
open_collection(usbHidParser_t * parser,unsigned type)611 static int open_collection(usbHidParser_t *parser, unsigned type)
612 {
613 usbHidCollectionItems_t *collection = NULL;
614 unsigned int usage = 0;
615
616 // 从取出最先进入local的usage
617 // 在处理local usage的函数hid_parser_local==>HID_LOCAL_ITEM_TAG_USAGE中
618 // data = (parser->global.usage_page local.usage[0];
619 // 高16位对应global.usage_page,低16位才对应local的usage
620 // 比如报告描述符的前6个字节数据如下:
621 // parser->global.usage_page = 0x01; 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
622 // parser->local.usage[0] = (0x01 local.usage[0] = (0x01 local.usage[0];
623 usage = parser->local.usage[0];
624
625 if (parser->collection_stack_ptr == USB_HID_COLLECTION_STACK_SIZE) {
626 hal_log_err("ERR: collection stack overflow\n");
627 return -1;
628 }
629
630 if (parser->HidDev->maxcollection == parser->HidDev->collection_size) {
631 collection = hal_malloc(sizeof(usbHidCollectionItems_t) * parser->HidDev->collection_size * 2);
632 if (collection == NULL) {
633 hal_log_err("ERR: failed to reallocate collection array\n");
634 return -1;
635 }
636
637 memset(collection, 0, sizeof(usbHidCollectionItems_t) * parser->HidDev->collection_size * 2);
638
639 memcpy(collection,
640 parser->HidDev->collection,
641 sizeof(usbHidCollectionItems_t) * parser->HidDev->collection_size);
642 memset(collection + parser->HidDev->collection_size,
643 0,
644 sizeof(usbHidCollectionItems_t) * parser->HidDev->collection_size);
645
646 hal_free(parser->HidDev->collection);
647 parser->HidDev->collection = collection;
648 parser->HidDev->collection_size *= 2;
649 }
650
651 parser->collection_stack[parser->collection_stack_ptr++] = parser->HidDev->maxcollection;
652 collection = parser->HidDev->collection + parser->HidDev->maxcollection++;
653 collection->Type = type;
654 collection->Usage = usage;
655 collection->Level = parser->collection_stack_ptr - 1;
656
657 if (type == USB_HID_COLLECTION_APPLICATION){
658 parser->HidDev->maxapplication++;
659 }
660
661 return 0;
662 }
663
664 /*
665 *******************************************************************************
666 * close_collection
667 *
668 * Description:
669 * Close a collection.
670 *
671 * Parameters:
672 *
673 *
674 * Return value:
675 *
676 *
677 * note:
678 * 无
679 *
680 *******************************************************************************
681 */
close_collection(usbHidParser_t * parser)682 static int close_collection(usbHidParser_t *parser)
683 {
684 if (!parser->collection_stack_ptr) {
685 hal_log_err("ERR: collection stack underflow\n");
686 return -1;
687 }
688
689 parser->collection_stack_ptr--;
690
691 return 0;
692 }
693
694 /*
695 *******************************************************************************
696 * hid_lookup_collection
697 *
698 * Description:
699 * Climb up the stack, search for the specified collection type
700 * and return the usage.
701 *
702 * Parameters:
703 *
704 *
705 * Return value:
706 *
707 *
708 * note:
709 * 无
710 *
711 *******************************************************************************
712 */
hid_lookup_collection(usbHidParser_t * parser,unsigned int type)713 static unsigned hid_lookup_collection(usbHidParser_t *parser, unsigned int type)
714 {
715 int n = 0;
716
717 for (n = parser->collection_stack_ptr - 1; n >= 0; n--){
718 if (parser->HidDev->collection[parser->collection_stack[n]].Type == type){
719 return parser->HidDev->collection[parser->collection_stack[n]].Usage;
720 }
721 }
722
723 return 0; /* we know nothing about this usage type */
724 }
725
726
727 /*
728 *******************************************************************************
729 * HidAddUsage
730 *
731 * Description:
732 * Add a usage to the temporary parser table.
733 *
734 * Parameters:
735 * Parser : output.
736 * Usage : input.
737 *
738 * Return value:
739 *
740 *
741 * note:
742 * 无
743 *
744 *******************************************************************************
745 */
HidAddUsage(usbHidParser_t * Parser,unsigned int Usage)746 static int HidAddUsage(usbHidParser_t *Parser, unsigned int Usage)
747 {
748 if (Parser->local.usage_index >= USB_HID_MAX_USAGES) {
749 hal_log_err("ERR: usage index exceeded\n");
750 return USB_ERR_DATA_OVERFLOW;
751 }
752
753 Parser->local.usage[Parser->local.usage_index] = Usage;
754 Parser->local.collection_index[Parser->local.usage_index] =
755 Parser->collection_stack_ptr ?
756 Parser->collection_stack[Parser->collection_stack_ptr - 1] : 0;
757 Parser->local.usage_index++;
758
759 return 0;
760 }
761
762 /*
763 *******************************************************************************
764 * HidAddField
765 *
766 * Description:
767 *
768 *
769 * Parameters:
770 *
771 *
772 * Return value:
773 *
774 *
775 * note:
776 * 无
777 *
778 *******************************************************************************
779 */
HidAddField(usbHidParser_t * Parser,unsigned int ReportType,unsigned int Flags)780 static int HidAddField(usbHidParser_t *Parser, unsigned int ReportType, unsigned int Flags)
781 {
782 usbHidReport_t *Report = NULL;
783 usbHidField_t *Field = NULL;
784 int usages = 0;
785 unsigned int offset = 0;
786 int i = 0;
787
788 Report = HidRegisterReport(Parser->HidDev, ReportType, Parser->global.ReportId);
789 if (Report == NULL) {
790 hal_log_err("ERR: hid_register_report failed\n");
791 return -1;
792 }
793
794 if (Parser->global.LogicalMaximum < Parser->global.LogicalMinimum) {
795 hal_log_err("ERR: logical range invalid (%d, %d)\n", Parser->global.LogicalMinimum,
796 Parser->global.LogicalMaximum);
797 return USB_ERR_BAD_ARGUMENTS;
798 }
799
800 offset = Report->Size;
801 Report->Size += Parser->global.ReportSize * Parser->global.ReportCount;
802
803 if (!Parser->local.usage_index){ /* Ignore padding fields */
804 return 0;
805 }
806
807 usages = max(Parser->local.usage_index, Parser->global.ReportCount);
808
809 Field = HidRegisterField(Report, usages, Parser->global.ReportCount);
810 if (Field == NULL){
811 hal_log_err("ERR: HidRegisterField failed\n");
812 return 0;
813 }
814
815 Field->physical = hid_lookup_collection(Parser, USB_HID_COLLECTION_PHYSICAL);
816 Field->logical = hid_lookup_collection(Parser, USB_HID_COLLECTION_LOGICAL);
817 Field->application = hid_lookup_collection(Parser, USB_HID_COLLECTION_APPLICATION);
818
819 for (i = 0; i < usages; i++) {
820 int j = i;
821
822 /* Duplicate the last usage we parsed if we have excess values */
823 if (i >= Parser->local.usage_index){
824 j = Parser->local.usage_index - 1;
825 }
826
827 Field->usage[i].hid = Parser->local.usage[j];
828 Field->usage[i].collection_index = Parser->local.collection_index[j];
829 }
830
831 Field->maxusage = usages;
832 Field->flags = Flags;
833 Field->report_offset = offset;
834 Field->report_type = ReportType;
835 Field->report_size = Parser->global.ReportSize;
836 Field->report_count = Parser->global.ReportCount;
837 Field->logical_minimum = Parser->global.LogicalMinimum;
838 Field->logical_maximum = Parser->global.LogicalMaximum;
839 Field->physical_minimum = Parser->global.PhysicalMinimum;
840 Field->physical_maximum = Parser->global.PhysicalMaximum;
841 Field->unit_exponent = Parser->global.UnitExponent;
842 Field->unit = Parser->global.Unit;
843
844 print_field(Field);
845
846 return 0;
847 }
848
849 /*
850 *******************************************************************************
851 * item_udata
852 *
853 * Description:
854 * Read data value from item.
855 *
856 * Parameters:
857 *
858 *
859 * Return value:
860 *
861 *
862 * note:
863 * 无
864 *
865 *******************************************************************************
866 */
item_udata(usbHidItem_t * Item)867 static unsigned int item_udata(usbHidItem_t *Item)
868 {
869 switch (Item->size) {
870 case 1:
871 return Item->data.Data_u8;
872 //break;
873
874 case 2:
875 return Item->data.Data_u16;
876 //break;
877
878 case 4:
879 return Item->data.Data_u32;
880 //break;
881
882 default:
883 hal_log_err("ERR: unkown item size(%d)\n", Item->size);
884 }
885
886 return 0;
887 }
888
889 /*
890 *******************************************************************************
891 * item_sdata
892 *
893 * Description:
894 * Read data value from item.
895 *
896 * Parameters:
897 *
898 *
899 * Return value:
900 *
901 *
902 * note:
903 * 无
904 *
905 *******************************************************************************
906 */
item_sdata(usbHidItem_t * Item)907 static int item_sdata(usbHidItem_t *Item)
908 {
909 switch (Item->size) {
910 case 1:
911 return Item->data.Data_s8;
912 //break;
913
914 case 2:
915 return Item->data.Data_s16;
916 //break;
917
918 case 4:
919 return Item->data.Data_s32;
920 //break;
921
922 default:
923 hal_log_err("ERR: unkown item size(%d)\n", Item->size);
924 }
925
926 return 0;
927 }
928
929 /*
930 *******************************************************************************
931 * HidParserGlobal
932 *
933 * Description:
934 * 解析全局目录项
935 *
936 * Parameters:
937 * Parser : output. 解析的结果
938 * Item : input. 待解析的目录项
939 *
940 * Return value:
941 * 成功或者失败
942 *
943 * note:
944 * 无
945 *
946 *******************************************************************************
947 */
HidParserGlobal(usbHidParser_t * Parser,usbHidItem_t * Item)948 static int HidParserGlobal(usbHidParser_t *Parser, usbHidItem_t *Item)
949 {
950 switch(Item->tag){
951 case USB_HID_GLOBAL_ITEM_TAG_PUSH:
952
953 if (Parser->global_stack_ptr == USB_HID_GLOBAL_STACK_SIZE) {
954 hal_log_err("ERR: global enviroment stack overflow\n");
955 return USB_ERR_DATA_OVERFLOW;
956 }
957
958 /* 将parser->global压入堆栈parser->global_stack[global_stack_ptr] */
959 memcpy(Parser->global_stack + Parser->global_stack_ptr++,
960 &Parser->global,
961 sizeof(usbHidGlobalItems_t));
962 return USB_ERR_SUCCESS;
963 //break;
964
965 case USB_HID_GLOBAL_ITEM_TAG_POP:
966
967 if (!Parser->global_stack_ptr) {
968 hal_log_err("ERR: global enviroment stack underflow\n");
969 return -1;
970 }
971
972 /* 将parser->global_stack[--global_stack_ptr]出栈[luther.gliethttp] */
973 memcpy(&Parser->global,
974 Parser->global_stack + --Parser->global_stack_ptr,
975 sizeof(usbHidGlobalItems_t));
976 return USB_ERR_SUCCESS;
977 //break;
978
979 case USB_HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
980 /* 全局量global.usage_page, 比如:
981 0x05, 0x01, --- USAGE_PAGE (Generic Desktop)
982 0x05, 0x09, --- USAGE_PAGE (Button)
983 */
984 Parser->global.UsagePage = item_udata(Item);
985 return USB_ERR_SUCCESS;
986 //break;
987
988 case USB_HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
989 Parser->global.LogicalMinimum = item_sdata(Item);
990 return USB_ERR_SUCCESS;
991 //break;
992
993 case USB_HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
994 if (Parser->global.LogicalMinimum< 0){
995 Parser->global.LogicalMaximum = item_sdata(Item);
996 }else{
997 Parser->global.LogicalMaximum = item_udata(Item);
998 }
999
1000 return USB_ERR_SUCCESS;
1001 //break;
1002
1003 case USB_HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
1004 Parser->global.PhysicalMinimum = item_sdata(Item);
1005 return USB_ERR_SUCCESS;
1006 //break;
1007
1008 case USB_HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
1009 if (Parser->global.PhysicalMinimum< 0){
1010 Parser->global.PhysicalMaximum = item_sdata(Item);
1011 }else{
1012 Parser->global.PhysicalMaximum = item_udata(Item);
1013 }
1014
1015 return USB_ERR_SUCCESS;
1016 //break;
1017
1018 case USB_HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
1019 Parser->global.UnitExponent= item_sdata(Item);
1020 return USB_ERR_SUCCESS;
1021 //break;
1022
1023 case USB_HID_GLOBAL_ITEM_TAG_UNIT:
1024 Parser->global.Unit = item_udata(Item);
1025 return USB_ERR_SUCCESS;
1026 //break;
1027
1028 case USB_HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
1029 /* 比如:0x75, 0x01, REPORT_SIZE (1), 表示1个bit */
1030 if ((Parser->global.ReportSize = item_udata(Item)) > 32) {
1031 hal_log_err("ERR: invalid report_size %d\n", Parser->global.ReportSize);
1032 return USB_ERR_UNKOWN_ERROR;
1033 }
1034 return USB_ERR_SUCCESS;
1035 //break;
1036
1037 case USB_HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
1038 if ((Parser->global.ReportCount = item_udata(Item)) > USB_HID_MAX_USAGES) {
1039 hal_log_err("ERR: invalid report_count %d\n", Parser->global.ReportSize);
1040 return USB_ERR_UNKOWN_ERROR;
1041 }
1042 return USB_ERR_SUCCESS;
1043 //break;
1044
1045 case USB_HID_GLOBAL_ITEM_TAG_REPORT_ID:
1046 if ((Parser->global.ReportId = item_udata(Item)) == 0) {
1047 hal_log_err("ERR: report_id 0 is invalid\n");
1048 return USB_ERR_UNKOWN_ERROR;
1049 }
1050 return USB_ERR_SUCCESS;
1051 //break;
1052
1053 default:
1054 hal_log_err("ERR: unknown global tag 0x%x\n", Item->tag);
1055 //return USB_ERR_UNKOWN_ERROR;
1056 }
1057
1058 return USB_ERR_UNKOWN_ERROR;
1059 }
1060
1061 /*
1062 *******************************************************************************
1063 * HidParserGlobal
1064 *
1065 * Description:
1066 * 解析全局目录项
1067 *
1068 * Parameters:
1069 * Parser : output. 解析的结果
1070 * Item : input. 待解析的目录项
1071 *
1072 * Return value:
1073 * 成功或者失败
1074 *
1075 * note:
1076 * 无
1077 *
1078 *******************************************************************************
1079 */
HidParserLocal(usbHidParser_t * Parser,usbHidItem_t * Item)1080 static int HidParserLocal(usbHidParser_t *Parser, usbHidItem_t *Item)
1081 {
1082 unsigned int data = 0;
1083 unsigned int n = 0;
1084
1085 if (Item->size == 0) {
1086 hal_log_err("ERR: item data expected for local item\n");
1087 return -1;
1088 }
1089
1090 data = item_udata(Item);
1091
1092 switch (Item->tag) {
1093 case USB_HID_LOCAL_ITEM_TAG_DELIMITER:
1094 if (data) {
1095 /*
1096 * We treat items before the first delimiter
1097 * as global to all usage sets (branch 0).
1098 * In the moment we process only these global
1099 * items and the first delimiter set.
1100 */
1101 if (Parser->local.delimiter_depth != 0) {
1102 hal_log_err("ERR: nested delimiters\n");
1103 return -1;
1104 }
1105
1106 Parser->local.delimiter_depth++;
1107 Parser->local.delimiter_branch++;
1108 } else {
1109 if (Parser->local.delimiter_depth < 1) {
1110 hal_log_err("ERR: bogus close delimiter\n");
1111 return -1;
1112 }
1113
1114 Parser->local.delimiter_depth--;
1115 }
1116 return 1;
1117 //break;
1118
1119 case USB_HID_LOCAL_ITEM_TAG_USAGE:
1120 if (Parser->local.delimiter_branch > 1) {
1121 hal_log_err("ERR: alternative usage ignored\n");
1122 return 0;
1123 }
1124
1125 if (Item->size <= 2){
1126 data = (Parser->global.UsagePage << 16) + data;
1127 }
1128
1129 return HidAddUsage(Parser, data);
1130 //break;
1131
1132 case USB_HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
1133 if (Parser->local.delimiter_branch > 1) {
1134 hal_log_err("ERR: alternative usage ignored\n");
1135 return 0;
1136 }
1137
1138 if (Item->size <= 2){
1139 data = (Parser->global.UsagePage << 16) + data;
1140 }
1141
1142 Parser->local.usage_minimum = data;
1143
1144 return 0;
1145 //break;
1146
1147 case USB_HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
1148 if (Parser->local.delimiter_branch > 1) {
1149 hal_log_err("ERR: alternative usage ignored\n");
1150 return 0;
1151 }
1152
1153 if (Item->size <= 2){
1154 data = (Parser->global.UsagePage << 16) + data;
1155 }
1156
1157 for (n = Parser->local.usage_minimum; n <= data; n++){
1158 if (HidAddUsage(Parser, n)) {
1159 hal_log_err("ERR: hid_add_usage failed\n");
1160 return -1;
1161 }
1162 }
1163
1164 return 0;
1165 //break;
1166
1167 default:
1168 hal_log_err("ERR: unknown local item tag 0x%x\n", Item->tag);
1169 //return 0;
1170 }
1171
1172 return 0;
1173 }
1174
1175 /*
1176 *******************************************************************************
1177 * hid_parser_main
1178 *
1179 * Description:
1180 * Process a main item.
1181 *
1182 * Parameters:
1183 *
1184 *
1185 * Return value:
1186 * 成功或者失败
1187 *
1188 * note:
1189 * 无
1190 *
1191 *******************************************************************************
1192 */
HidParserMain(usbHidParser_t * parser,usbHidItem_t * item)1193 static int HidParserMain(usbHidParser_t *parser, usbHidItem_t *item)
1194 {
1195 unsigned int data = 0;
1196 int ret = 0;
1197
1198 data = item_udata(item); //返回该Short Item数据,数据有4种,0,1,2,4字节
1199
1200 switch (item->tag) {
1201 case USB_HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
1202 //Open a collection. 入栈push操作The type/usage is pushed on the stack
1203 ret = open_collection(parser, data & 0xff);
1204 break;
1205
1206 case USB_HID_MAIN_ITEM_TAG_END_COLLECTION:
1207 // Close a collection.出栈pop操作parser->collection_stack_ptr
1208 ret = close_collection(parser);
1209 break;
1210
1211 case USB_HID_MAIN_ITEM_TAG_INPUT:
1212 ret = HidAddField(parser, USB_HID_REPORT_INPUT, data);
1213 break;
1214
1215 case USB_HID_MAIN_ITEM_TAG_OUTPUT:
1216 ret = HidAddField(parser, USB_HID_REPORT_OUTPUT, data);
1217 break;
1218
1219 case USB_HID_MAIN_ITEM_TAG_FEATURE:
1220 ret = HidAddField(parser, USB_HID_REPORT_FEATURE, data);
1221 break;
1222
1223 default:
1224 hal_log_err("ERR: unknown main item tag 0x%x\n", item->tag);
1225 ret = 0;
1226 }
1227
1228 // 清0所有parser->local内存数据,local包括:
1229 // struct hid_local {
1230 // unsigned usage[HID_MAX_USAGES]; /* usage array */
1231 // unsigned collection_index[HID_MAX_USAGES]; /* collection index array */
1232 // unsigned usage_index;
1233 // unsigned usage_minimum;
1234 // unsigned delimiter_depth;
1235 // unsigned delimiter_branch;
1236 // };
1237 // 所以parser->local.usage_index在遇到main item时全部清0.
1238 print_local(&(parser->local));
1239 memset(&(parser->local), 0, sizeof(parser->local)); /* Reset the local parser environment */
1240
1241 return ret;
1242 }
1243
1244 /*
1245 *******************************************************************************
1246 * hid_parser_main
1247 *
1248 * Description:
1249 * Process a reserved item..
1250 *
1251 * Parameters:
1252 *
1253 *
1254 * Return value:
1255 * 成功或者失败
1256 *
1257 * note:
1258 * 无
1259 *
1260 *******************************************************************************
1261 */
HidParserReserved(usbHidParser_t * parser,usbHidItem_t * item)1262 static int HidParserReserved(usbHidParser_t *parser, usbHidItem_t *item)
1263 {
1264 hal_log_err("ERR: reserved item type, tag 0x%x\n", item->tag);
1265
1266 return 0;
1267 }
1268
1269 /*
1270 * Free a report and all registered fields. The field->usage and
1271 * field->value table's are allocated behind the field, so we need
1272 * only to free(field) itself.
1273 */
1274
hid_free_report(usbHidReport_t * report)1275 static void hid_free_report(usbHidReport_t *report)
1276 {
1277 unsigned int n;
1278
1279 for (n = 0; n < report->Maxfield; n++){
1280 if(report->Field[n]){
1281 if(report->Field[n]->usage){
1282 hal_free(report->Field[n]->usage);
1283 report->Field[n]->usage = NULL;
1284 }
1285
1286 if(report->Field[n]->value){
1287 hal_free(report->Field[n]->value);
1288 report->Field[n]->value = NULL;
1289 }
1290
1291 hal_free(report->Field[n]);
1292 report->Field[n] = NULL;
1293 }
1294 }
1295
1296 hal_free(report);
1297
1298 return;
1299 }
1300
1301 /*
1302 * Free a device structure, all reports, and all fields.
1303 */
1304
hid_free_device(HidDev_t * HidDev)1305 void hid_free_device(HidDev_t *HidDev)
1306 {
1307 unsigned int i = 0;
1308 unsigned int j = 0;
1309
1310 for (i = 0; i < USB_HID_REPORT_TYPES; i++) {
1311 usbHidReportEnum_t *report_enum = HidDev->HidReportEnum + i;
1312
1313 for (j = 0; j < 256; j++) {
1314 usbHidReport_t *report = report_enum->Report[j];
1315
1316 if (report){
1317 report_enum->ReportNum -= 1;
1318 hid_free_report(report);
1319 report_enum->Report[j] = NULL;
1320 }
1321 }
1322 }
1323
1324 if(HidDev->collection){
1325 hal_free(HidDev->collection);
1326 HidDev->collection = NULL;
1327 }
1328
1329 return ;
1330 }
1331
1332 /*
1333 *******************************************************************************
1334 * HidFetchItem
1335 *
1336 * Description:
1337 * 取出Item,并且解析
1338 *
1339 * Parameters:
1340 * Start : input. 本次带解析的report起始地址
1341 * End : input. report的结束地址
1342 * Item : output. 解析后的Item
1343 *
1344 * Return value:
1345 * 返回解析后的Item
1346 *
1347 * note:
1348 * 无
1349 *
1350 *******************************************************************************
1351 */
HidFetchItem(__u8 * Start,__u8 * End,usbHidItem_t * Item)1352 static __u8 *HidFetchItem(__u8 *Start, __u8 *End, usbHidItem_t *Item)
1353 {
1354 __u8 b = 0;
1355
1356 if(Start == NULL || End == NULL || Item == NULL){
1357 hal_log_err("ERR: input error\n");
1358 return NULL;
1359 }
1360
1361 memset(Item, 0, sizeof(usbHidItem_t));
1362
1363 if ((End - Start) <= 0){
1364 hal_log_err("ERR: the item is invalid. Start = %x, End = %x\n", Start, End);
1365 return NULL;
1366 }
1367
1368 b = *Start++;
1369
1370 Item->type = (b >> 2) & 3; // 取出类型
1371 Item->tag = (b >> 4) & 15; // 取出tag信息
1372
1373 if (Item->tag == USB_HID_ITEM_TAG_LONG) {
1374 Item->format = USB_HID_ITEM_FORMAT_LONG;
1375
1376 if ((End - Start) < 2){
1377 hal_log_err("ERR: the item is invalid. Start = %x, End = %x\n", Start, End);
1378 return NULL;
1379 }
1380
1381 Item->size = *Start++;
1382 Item->tag = *Start++;
1383
1384 // 保证该Long item拥有所需的足够数据
1385 if ((End - Start) < Item->size){
1386 hal_log_err("ERR: the item is invalid. Start = %x, End = %x, Item->size = %x\n",
1387 Start, End, Item->size);
1388 return NULL;
1389 }
1390
1391 Item->data.longdata = Start; // 从第4个字节开始就是数据区
1392 Start += Item->size; // start指向下一个item开始处
1393
1394 return Start;
1395 }
1396
1397 Item->format = USB_HID_ITEM_FORMAT_SHORT;
1398 Item->size = b & 3; //取出数据大小
1399
1400 switch(Item->size){
1401 case 0: // 没有数据区,start指向下一个item开始处
1402 return Start;
1403
1404 case 1:
1405 if ((End - Start) < 1)
1406 return NULL;
1407 Item->data.Data_u8 = *Start++; // 取出1字节数据
1408 return Start;
1409
1410 case 2:
1411 if ((End - Start) < 2)
1412 return NULL;
1413 Item->data.Data_u16 = le16_to_cpu(*((__u16 *)Start));
1414 Start = (__u8 *)((__u16 *)Start + 1); // start加2
1415 return Start;
1416
1417 case 3:
1418 Item->size++; // 强制调整到4字节
1419 if ((End - Start) < 4)
1420 return NULL;
1421 Item->data.Data_u32 = le32_to_cpu(*((unsigned int*)Start));
1422 Start = (__u8 *)((unsigned int *)Start + 1); // start加4
1423 return Start;
1424 }
1425
1426 return NULL;
1427 }
1428
1429 /*
1430 *******************************************************************************
1431 * HidParseReport
1432 *
1433 * Description:
1434 *
1435 *
1436 * Parameters:
1437 *
1438 *
1439 * Return value:
1440 *
1441 *
1442 * note:
1443 * 无
1444 *
1445 *******************************************************************************
1446 */
HidParseReport(__u8 * ReportData,unsigned int ReportSize,HidDev_t * HidDev)1447 int HidParseReport(__u8 *ReportData, unsigned int ReportSize, HidDev_t *HidDev)
1448 {
1449 usbHidParser_t *parser = NULL; /* 因为usbHidParser_t结构大于16k, 因此使用malloc */
1450 usbHidItem_t item;
1451 __u8 *end = NULL;
1452 int ret = 0;
1453
1454 parser = (usbHidParser_t *)hal_malloc(sizeof(usbHidParser_t));
1455 if(parser == NULL){
1456 hal_log_err("ERR: malloc failed\n");
1457 ret = USB_ERR_MALLOC_FAILED;
1458
1459 goto ERR0;
1460 }
1461 memset(parser, 0, sizeof(usbHidParser_t));
1462
1463 HidDev->collection = hal_malloc(sizeof(usbHidCollectionItems_t) *
1464 USB_HID_DEFAULT_NUM_COLLECTIONS);
1465 if (HidDev->collection == NULL) {
1466 hal_log_err("ERR: malloc failed\n");
1467 ret = USB_ERR_MALLOC_FAILED;
1468
1469 goto ERR1;
1470 }
1471 memset(HidDev->collection,
1472 0,
1473 sizeof(usbHidCollectionItems_t) * USB_HID_DEFAULT_NUM_COLLECTIONS);
1474
1475 HidDev->collection_size = USB_HID_DEFAULT_NUM_COLLECTIONS;
1476 parser->HidDev = HidDev;
1477
1478 // 从设备报告描述符中读取一个item项
1479 end = ReportData + ReportSize;
1480 while ((ReportData = HidFetchItem(ReportData, end, &item)) != NULL) {
1481 print_item(&item);
1482
1483 /* 现在Long item项还没有使用,所以这里不支持 */
1484 if (item.format != USB_HID_ITEM_FORMAT_SHORT) {
1485 hal_log_err("ERR: unexpected long global item\n");
1486 ret = USB_ERR_UNKOWN_ERROR;
1487 goto ERR2;
1488 }
1489
1490 switch(item.type){
1491 case USB_HID_ITEM_TYPE_MAIN:
1492 if (HidParserMain(parser, &item)) {
1493 hal_log_err("ERR: item %u %u %u %u parsing failed\n",
1494 item.format,
1495 (unsigned)item.size,
1496 (unsigned)item.type,
1497 (unsigned)item.tag);
1498 ret = USB_ERR_UNKOWN_ERROR;
1499 goto ERR2;
1500 }
1501 break;
1502
1503 case USB_HID_ITEM_TYPE_GLOBAL:
1504 if (HidParserGlobal(parser, &item)) {
1505 hal_log_err("ERR: item %u %u %u %u parsing failed\n",
1506 item.format,
1507 (unsigned)item.size,
1508 (unsigned)item.type,
1509 (unsigned)item.tag);
1510 ret = USB_ERR_UNKOWN_ERROR;
1511 goto ERR2;
1512 }
1513 break;
1514
1515 case USB_HID_ITEM_TYPE_LOCAL:
1516 if (HidParserLocal(parser, &item)) {
1517 hal_log_err("ERR: item %u %u %u %u parsing failed\n",
1518 item.format,
1519 (unsigned)item.size,
1520 (unsigned)item.type,
1521 (unsigned)item.tag);
1522 ret = USB_ERR_UNKOWN_ERROR;
1523 goto ERR2;
1524 }
1525 break;
1526
1527 default:
1528 if (HidParserReserved(parser, &item)) {
1529 hal_log_err("ERR: item %u %u %u %u parsing failed\n",
1530 item.format,
1531 (unsigned)item.size,
1532 (unsigned)item.type,
1533 (unsigned)item.tag);
1534 ret = USB_ERR_UNKOWN_ERROR;
1535 goto ERR2;
1536 }
1537 }
1538
1539 // 解析完了
1540 if (ReportData == end) {
1541 // 入栈操作多于出栈操作,比如COLLECTION (Application)就是入栈, END_COLLECTION对应出栈
1542 // 目前定义堆栈大小为4个, #define HID_COLLECTION_STACK_SIZE 4
1543 // 所以报告描述符脚本书写有误,返回NULL,失败
1544 if (parser->collection_stack_ptr) {
1545 hal_log_err("ERR: unbalanced collection at end of report description\n");
1546 ret = USB_ERR_UNKOWN_ERROR;
1547 goto ERR2;
1548 }
1549
1550 // 该变量也是通过入栈,出栈收集的,所以也必须配对
1551 if (parser->local.delimiter_depth) {
1552 hal_log_err("ERR: unbalanced delimiter at end of report description\n");
1553 ret = USB_ERR_UNKOWN_ERROR;
1554 goto ERR2;
1555 }
1556
1557 print_ReportEnum(HidDev);
1558 print_parser(parser);
1559
1560 if(parser){
1561 hal_free(parser);
1562 }
1563
1564 //正常解析成功,释放vmalloc到的parser解释器结构体内存.
1565 return USB_ERR_SUCCESS;
1566 }
1567 }
1568
1569 // 报告描述脚本有误
1570 hal_log_err("ERR: item fetching failed at offset %d\n", (int)(end - ReportData));
1571
1572 ret = USB_ERR_UNKOWN_ERROR;
1573
1574 ERR2:
1575 hid_free_device(HidDev);
1576 hal_free(HidDev->collection);
1577 HidDev->collection = NULL;
1578
1579 ERR1:
1580 hal_free(parser);
1581
1582 ERR0:
1583
1584 return ret;
1585 }
1586
1587 /*
1588 *******************************************************************************
1589 * HidParseReport
1590 *
1591 * Description:
1592 *
1593 *
1594 * Parameters:
1595 *
1596 *
1597 * Return value:
1598 *
1599 *
1600 * note:
1601 * 无
1602 *
1603 *******************************************************************************
1604 */
HidFreeReport(HidDev_t * HidDev)1605 int HidFreeReport(HidDev_t *HidDev)
1606 {
1607
1608 hid_free_device(HidDev);
1609
1610 if(HidDev->collection){
1611 hal_free(HidDev->collection);
1612 HidDev->collection = NULL;
1613 }
1614
1615 return USB_ERR_SUCCESS;
1616
1617 }
1618
1619