HidProtocol.c 44 KB

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