Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

501 lines
16 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. report.c
  5. Abstract:
  6. This module contains the code for translating HID reports to something
  7. useful.
  8. Environment:
  9. Kernel & user mode
  10. Revision History:
  11. Nov-96 : Created by Kenneth D. Ray
  12. --*/
  13. #include <stdlib.h>
  14. #include <wtypes.h>
  15. #include "hidsdi.h"
  16. #include "hid.h"
  17. #include "mylog.h"
  18. #include "mymem.h"
  19. BOOL
  20. UnpackReport (
  21. IN PCHAR ReportBuffer,
  22. IN USHORT ReportBufferLength,
  23. IN HIDP_REPORT_TYPE ReportType,
  24. IN OUT PHID_DATA Data,
  25. IN ULONG DataLength,
  26. IN PHIDP_PREPARSED_DATA Ppd
  27. );
  28. // Haven't used this with Write .. let's see whether i need this with set/get feature
  29. BOOL
  30. PackReport (
  31. OUT PCHAR ReportBuffer,
  32. IN USHORT ReportBufferLength,
  33. IN HIDP_REPORT_TYPE ReportType,
  34. IN PHID_DATA Data,
  35. IN ULONG DataLength,
  36. IN PHIDP_PREPARSED_DATA Ppd
  37. );
  38. VOID CALLBACK
  39. WriteIOCompletionRoutine (
  40. DWORD dwErrorCode,
  41. DWORD dwNumberofBytesTransferrred,
  42. LPOVERLAPPED lpOverlapped
  43. )
  44. {
  45. PHID_DEVICE pHidDevice = (PHID_DEVICE) lpOverlapped->hEvent;
  46. LOG((PHONESP_TRACE, "WriteIOCompletionRoutine - enter"));
  47. if(dwErrorCode)
  48. {
  49. LOG((PHONESP_ERROR, "Error occured while writing" ));
  50. }
  51. MemFree(lpOverlapped);
  52. LOG((PHONESP_TRACE, "WriteIOCompletionRoutine - exit"));
  53. }
  54. BOOL
  55. Write (
  56. PHID_DEVICE HidDevice
  57. )
  58. /*++
  59. RoutineDescription:
  60. Given a struct _HID_DEVICE, take the information in the HID_DATA array
  61. pack it into multiple write reports and send each report to the HID device
  62. --*/
  63. {
  64. DWORD bytesWritten;
  65. PHID_DATA pData;
  66. ULONG Index;
  67. BOOL Status;
  68. BOOL WriteStatus;
  69. LPOVERLAPPED lpOverlapped;
  70. LOG((PHONESP_TRACE, "Write - enter"));
  71. lpOverlapped = (LPOVERLAPPED) MemAlloc (sizeof(OVERLAPPED));
  72. if (lpOverlapped == NULL)
  73. {
  74. LOG((PHONESP_ERROR,"Write - out of memory"));
  75. return FALSE;
  76. }
  77. lpOverlapped->Offset = 0;
  78. lpOverlapped->OffsetHigh = 0;
  79. lpOverlapped->hEvent = (HANDLE) HidDevice;
  80. LOG((PHONESP_TRACE,"Write - Report Packed"));
  81. WriteStatus = WriteFileEx (HidDevice->HidDevice,
  82. HidDevice->OutputReportBuffer,
  83. HidDevice->Caps.OutputReportByteLength,
  84. lpOverlapped,
  85. &WriteIOCompletionRoutine);
  86. SleepEx(INFINITE, TRUE);
  87. LOG((PHONESP_TRACE, "Write - Report sent"));
  88. LOG((PHONESP_TRACE, "Write - exit"));
  89. return TRUE;
  90. }
  91. BOOL
  92. SetFeature (
  93. PHID_DEVICE HidDevice
  94. )
  95. /*++
  96. RoutineDescription:
  97. Given a struct _HID_DEVICE, take the information in the HID_DATA array
  98. pack it into multiple reports and send it to the hid device via HidD_SetFeature()
  99. --*/
  100. {
  101. PHID_DATA pData;
  102. ULONG Index;
  103. BOOL Status;
  104. BOOL FeatureStatus;
  105. DWORD ErrorCode;
  106. /*
  107. // Begin by looping through the HID_DEVICE's HID_DATA structure and setting
  108. // the IsDataSet field to FALSE to indicate that each structure has
  109. // not yet been set for this SetFeature() call.
  110. */
  111. pData = HidDevice -> FeatureData;
  112. for (Index = 0; Index < HidDevice -> FeatureDataLength; Index++, pData++) {
  113. pData -> IsDataSet = FALSE;
  114. }
  115. /*
  116. // In setting all the data in the reports, we need to pack a report buffer
  117. // and call WriteFile for each report ID that is represented by the
  118. // device structure. To do so, the IsDataSet field will be used to
  119. // determine if a given report field has already been set.
  120. */
  121. Status = TRUE;
  122. pData = HidDevice -> FeatureData;
  123. for (Index = 0; Index < HidDevice -> FeatureDataLength; Index++, pData++) {
  124. if (!pData -> IsDataSet) {
  125. /*
  126. // Package the report for this data structure. PackReport will
  127. // set the IsDataSet fields of this structure and any other
  128. // structures that it includes in the report with this structure
  129. */
  130. PackReport (HidDevice->FeatureReportBuffer,
  131. HidDevice->Caps.FeatureReportByteLength,
  132. HidP_Feature,
  133. pData,
  134. HidDevice->FeatureDataLength - Index,
  135. HidDevice->Ppd);
  136. /*
  137. // Now a report has been packaged up...Send it down to the device
  138. */
  139. FeatureStatus =(HidD_SetFeature (HidDevice->HidDevice,
  140. HidDevice->FeatureReportBuffer,
  141. HidDevice->Caps.FeatureReportByteLength));
  142. ErrorCode = GetLastError();
  143. Status = Status && FeatureStatus;
  144. }
  145. }
  146. return (Status);
  147. }
  148. BOOL
  149. GetFeature (
  150. PHID_DEVICE HidDevice
  151. )
  152. /*++
  153. RoutineDescription:
  154. Given a struct _HID_DEVICE, fill in the feature data structures with
  155. all features on the device. May issue multiple HidD_GetFeature() calls to
  156. deal with multiple report IDs.
  157. --*/
  158. {
  159. ULONG Index;
  160. PHID_DATA pData;
  161. BOOL FeatureStatus;
  162. BOOL Status;
  163. /*
  164. // As with writing data, the IsDataSet value in all the structures should be
  165. // set to FALSE to indicate that the value has yet to have been set
  166. */
  167. LOG((PHONESP_TRACE,"GetFeature - enter"));
  168. pData = HidDevice->FeatureData;
  169. for (Index = 0; Index < HidDevice -> FeatureDataLength; Index++, pData++)
  170. {
  171. pData -> IsDataSet = FALSE;
  172. }
  173. /*
  174. // Next, each structure in the HID_DATA buffer is filled in with a value
  175. // that is retrieved from one or more calls to HidD_GetFeature. The
  176. // number of calls is equal to the number of reportIDs on the device
  177. */
  178. Status = TRUE;
  179. pData = HidDevice -> FeatureData;
  180. for (Index = 0; Index < HidDevice -> FeatureDataLength; Index++, pData++) {
  181. /*
  182. // If a value has yet to have been set for this structure, build a report
  183. // buffer with its report ID as the first byte of the buffer and pass
  184. // it in the HidD_GetFeature call. Specifying the report ID in the
  185. // first specifies which report is actually retrieved from the device.
  186. // The rest of the buffer should be zeroed before the call
  187. */
  188. if (!pData -> IsDataSet) {
  189. memset(HidDevice -> FeatureReportBuffer, 0x00, HidDevice->Caps.FeatureReportByteLength);
  190. HidDevice -> FeatureReportBuffer[0] = (UCHAR) pData -> ReportID;
  191. FeatureStatus = HidD_GetFeature ( HidDevice->HidDevice,
  192. HidDevice->FeatureReportBuffer,
  193. HidDevice->Caps.FeatureReportByteLength);
  194. /*
  195. // If the return value is TRUE, scan through the rest of the HID_DATA
  196. // structures and fill whatever values we can from this report
  197. */
  198. if (FeatureStatus)
  199. {
  200. FeatureStatus = UnpackReport ( HidDevice->FeatureReportBuffer,
  201. HidDevice->Caps.FeatureReportByteLength,
  202. HidP_Feature,
  203. HidDevice->FeatureData,
  204. HidDevice->FeatureDataLength,
  205. HidDevice->Ppd
  206. );
  207. }
  208. else
  209. {
  210. LOG((PHONESP_ERROR, "GetFeature - HidD_GetFeature failed %d", GetLastError()));
  211. }
  212. Status = Status && FeatureStatus;
  213. }
  214. }
  215. LOG((PHONESP_TRACE, "GetFeature - exit"));
  216. return Status;
  217. }
  218. BOOL
  219. UnpackReport (
  220. IN PCHAR ReportBuffer,
  221. IN USHORT ReportBufferLength,
  222. IN HIDP_REPORT_TYPE ReportType,
  223. IN OUT PHID_DATA Data,
  224. IN ULONG DataLength,
  225. IN PHIDP_PREPARSED_DATA Ppd
  226. )
  227. /*++
  228. Routine Description:
  229. Given ReportBuffer representing a report from a HID device where the first
  230. byte of the buffer is the report ID for the report, extract all the HID_DATA
  231. in the Data list from the given report.
  232. --*/
  233. {
  234. ULONG numUsages; // Number of usages returned from GetUsages.
  235. ULONG i;
  236. UCHAR reportID;
  237. ULONG Index;
  238. ULONG nextUsage;
  239. reportID = ReportBuffer[0];
  240. for (i = 0; i < DataLength; i++, Data++) {
  241. if (reportID == Data->ReportID) {
  242. if (Data->IsButtonData) {
  243. numUsages = Data->ButtonData.MaxUsageLength;
  244. Data->Status = HidP_GetUsages (
  245. ReportType,
  246. Data->UsagePage,
  247. 0, // All collections
  248. Data->ButtonData.Usages,
  249. &numUsages,
  250. Ppd,
  251. ReportBuffer,
  252. ReportBufferLength);
  253. //
  254. // Get usages writes the list of usages into the buffer
  255. // Data->ButtonData.Usages newUsage is set to the number of usages
  256. // written into this array.
  257. // A usage cannot not be defined as zero, so we'll mark a zero
  258. // following the list of usages to indicate the end of the list of
  259. // usages
  260. //
  261. // NOTE: One anomaly of the GetUsages function is the lack of ability
  262. // to distinguish the data for one ButtonCaps from another
  263. // if two different caps structures have the same UsagePage
  264. // For instance:
  265. // Caps1 has UsagePage 07 and UsageRange of 0x00 - 0x167
  266. // Caps2 has UsagePage 07 and UsageRange of 0xe0 - 0xe7
  267. //
  268. // However, calling GetUsages for each of the data structs
  269. // will return the same list of usages. It is the
  270. // responsibility of the caller to set in the HID_DEVICE
  271. // structure which usages actually are valid for the
  272. // that structure.
  273. //
  274. /*
  275. // Search through the usage list and remove those that
  276. // correspond to usages outside the define ranged for this
  277. // data structure.
  278. */
  279. for (Index = 0, nextUsage = 0; Index < numUsages; Index++) {
  280. if (Data -> ButtonData.UsageMin <= Data -> ButtonData.Usages[Index] &&
  281. Data -> ButtonData.Usages[Index] <= Data -> ButtonData.UsageMax) {
  282. Data -> ButtonData.Usages[nextUsage++] = Data -> ButtonData.Usages[Index];
  283. }
  284. }
  285. if (nextUsage < Data -> ButtonData.MaxUsageLength) {
  286. Data->ButtonData.Usages[nextUsage] = 0;
  287. }
  288. }
  289. else {
  290. Data->Status = HidP_GetUsageValue (
  291. ReportType,
  292. Data->UsagePage,
  293. 0, // All Collections.
  294. Data->ValueData.Usage,
  295. &Data->ValueData.Value,
  296. Ppd,
  297. ReportBuffer,
  298. ReportBufferLength);
  299. Data->Status = HidP_GetScaledUsageValue (
  300. ReportType,
  301. Data->UsagePage,
  302. 0, // All Collections.
  303. Data->ValueData.Usage,
  304. &Data->ValueData.ScaledValue,
  305. Ppd,
  306. ReportBuffer,
  307. ReportBufferLength);
  308. }
  309. Data -> IsDataSet = TRUE;
  310. }
  311. }
  312. return (TRUE);
  313. }
  314. BOOL
  315. PackReport (
  316. OUT PCHAR ReportBuffer,
  317. IN USHORT ReportBufferLength,
  318. IN HIDP_REPORT_TYPE ReportType,
  319. IN PHID_DATA Data,
  320. IN ULONG DataLength,
  321. IN PHIDP_PREPARSED_DATA Ppd
  322. )
  323. /*++
  324. Routine Description:
  325. This routine takes in a list of HID_DATA structures (DATA) and builds
  326. in ReportBuffer the given report for all data values in the list that
  327. correspond to the report ID of the first item in the list.
  328. For every data structure in the list that has the same report ID as the first
  329. item in the list will be set in the report. Every data item that is
  330. set will also have it's IsDataSet field marked with TRUE.
  331. A return value of FALSE indicates an unexpected error occurred when setting
  332. a given data value. The caller should expect that assume that no values
  333. within the given data structure were set.
  334. A return value of TRUE indicates that all data values for the given report
  335. ID were set without error.
  336. --*/
  337. {
  338. ULONG numUsages; // Number of usages to set for a given report.
  339. ULONG i;
  340. ULONG CurrReportID;
  341. /*
  342. // All report buffers that are initially sent need to be zero'd out
  343. */
  344. memset (ReportBuffer, (UCHAR) 0, ReportBufferLength);
  345. /*
  346. // Go through the data structures and set all the values that correspond to
  347. // the CurrReportID which is obtained from the first data structure
  348. // in the list
  349. */
  350. CurrReportID = Data -> ReportID;
  351. for (i = 0; i < DataLength; i++, Data++) {
  352. /*
  353. // There are two different ways to determine if we set the current data
  354. // structure:
  355. // 1) Store the report ID were using and only attempt to set those
  356. // data structures that correspond to the given report ID. This
  357. // example shows this implementation.
  358. //
  359. // 2) Attempt to set all of the data structures and look for the
  360. // returned status value of HIDP_STATUS_INVALID_REPORT_ID. This
  361. // error code indicates that the given usage exists but has a
  362. // different report ID than the report ID in the current report
  363. // buffer
  364. */
  365. if (Data -> ReportID == CurrReportID) {
  366. if (Data->IsButtonData) {
  367. numUsages = Data->ButtonData.MaxUsageLength;
  368. Data->Status = HidP_SetUsages (
  369. ReportType,
  370. Data->UsagePage,
  371. 0, // All collections
  372. Data->ButtonData.Usages,
  373. &numUsages,
  374. Ppd,
  375. ReportBuffer,
  376. ReportBufferLength);
  377. } else {
  378. Data->Status = HidP_SetUsageValue (
  379. ReportType,
  380. Data->UsagePage,
  381. 0, // All Collections.
  382. Data->ValueData.Usage,
  383. Data->ValueData.Value,
  384. Ppd,
  385. ReportBuffer,
  386. ReportBufferLength);
  387. }
  388. if (HIDP_STATUS_SUCCESS != Data->Status)
  389. {
  390. return FALSE;
  391. }
  392. }
  393. }
  394. /*
  395. // At this point, all data structures that have the same ReportID as the
  396. // first one will have been set in the given report. Time to loop
  397. // through the structure again and mark all of those data structures as
  398. // having been set.
  399. */
  400. for (i = 0; i < DataLength; i++, Data++) {
  401. if (CurrReportID == Data -> ReportID) {
  402. Data -> IsDataSet = TRUE;
  403. }
  404. }
  405. return (TRUE);
  406. }