PageRenderTime 4548ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/dll/win32/hid/hid.c

https://bitbucket.org/arty/arty-newcc-reactos
C | 542 lines | 323 code | 64 blank | 155 comment | 27 complexity | 36616f09fe3e3bcfaabba81681eafcb3 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-3.0, CC-BY-SA-3.0, AGPL-3.0, GPL-3.0, CPL-1.0
  1. /*
  2. * ReactOS Hid User Library
  3. * Copyright (C) 2004-2005 ReactOS Team
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /* $Id: hid.c 55555 2012-02-12 04:59:51Z cgutman $
  20. *
  21. * PROJECT: ReactOS Hid User Library
  22. * FILE: lib/hid/hid.c
  23. * PURPOSE: ReactOS Hid User Library
  24. * PROGRAMMER: Thomas Weidenmueller <w3seek@reactos.com>
  25. *
  26. * UPDATE HISTORY:
  27. * 07/12/2004 Created
  28. */
  29. #include <precomp.h>
  30. HINSTANCE hDllInstance;
  31. /* device interface GUID for HIDClass devices */
  32. const GUID HidClassGuid = {0x4D1E55B2, 0xF16F, 0x11CF, {0x88,0xCB,0x00,0x11,0x11,0x00,0x00,0x30}};
  33. BOOL WINAPI
  34. DllMain(HINSTANCE hinstDLL,
  35. DWORD dwReason,
  36. LPVOID lpvReserved)
  37. {
  38. switch(dwReason)
  39. {
  40. case DLL_PROCESS_ATTACH:
  41. hDllInstance = hinstDLL;
  42. break;
  43. case DLL_THREAD_ATTACH:
  44. break;
  45. case DLL_THREAD_DETACH:
  46. break;
  47. case DLL_PROCESS_DETACH:
  48. break;
  49. }
  50. return TRUE;
  51. }
  52. /*
  53. * HidD_FlushQueue EXPORTED
  54. *
  55. * @implemented
  56. */
  57. HIDAPI
  58. BOOLEAN WINAPI
  59. HidD_FlushQueue(IN HANDLE HidDeviceObject)
  60. {
  61. DWORD RetLen;
  62. return DeviceIoControl(HidDeviceObject, IOCTL_HID_FLUSH_QUEUE,
  63. NULL, 0,
  64. NULL, 0,
  65. &RetLen, NULL) != 0;
  66. }
  67. /*
  68. * HidD_FreePreparsedData EXPORTED
  69. *
  70. * @implemented
  71. */
  72. HIDAPI
  73. BOOLEAN WINAPI
  74. HidD_FreePreparsedData(IN PHIDP_PREPARSED_DATA PreparsedData)
  75. {
  76. return (LocalFree((HLOCAL)PreparsedData) == NULL);
  77. }
  78. /*
  79. * HidD_GetAttributes EXPORTED
  80. *
  81. * @implemented
  82. */
  83. HIDAPI
  84. BOOLEAN WINAPI
  85. HidD_GetAttributes(IN HANDLE HidDeviceObject,
  86. OUT PHIDD_ATTRIBUTES Attributes)
  87. {
  88. HID_COLLECTION_INFORMATION hci;
  89. DWORD RetLen;
  90. if(!DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_COLLECTION_INFORMATION,
  91. NULL, 0,
  92. &hci, sizeof(HID_COLLECTION_INFORMATION),
  93. &RetLen, NULL))
  94. {
  95. return FALSE;
  96. }
  97. /* copy the fields */
  98. Attributes->Size = sizeof(HIDD_ATTRIBUTES);
  99. Attributes->VendorID = hci.VendorID;
  100. Attributes->ProductID = hci.ProductID;
  101. Attributes->VersionNumber = hci.VersionNumber;
  102. return TRUE;
  103. }
  104. /*
  105. * HidP_GetButtonCaps EXPORTED
  106. *
  107. * @implemented
  108. */
  109. HIDAPI
  110. NTSTATUS WINAPI
  111. HidP_GetButtonCaps(IN HIDP_REPORT_TYPE ReportType,
  112. OUT PHIDP_BUTTON_CAPS ButtonCaps,
  113. IN OUT PULONG ButtonCapsLength,
  114. IN PHIDP_PREPARSED_DATA PreparsedData)
  115. {
  116. return HidP_GetSpecificButtonCaps(ReportType, 0, 0, 0, ButtonCaps,
  117. ButtonCapsLength, PreparsedData);
  118. }
  119. /*
  120. * HidD_GetFeature EXPORTED
  121. *
  122. * @implemented
  123. */
  124. HIDAPI
  125. BOOLEAN WINAPI
  126. HidD_GetFeature(IN HANDLE HidDeviceObject,
  127. OUT PVOID ReportBuffer,
  128. IN ULONG ReportBufferLength)
  129. {
  130. DWORD RetLen;
  131. return DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_FEATURE,
  132. NULL, 0,
  133. ReportBuffer, ReportBufferLength,
  134. &RetLen, NULL) != 0;
  135. }
  136. /*
  137. * HidD_GetHidGuid EXPORTED
  138. *
  139. * @implemented
  140. */
  141. HIDAPI
  142. VOID WINAPI
  143. HidD_GetHidGuid(OUT LPGUID HidGuid)
  144. {
  145. *HidGuid = HidClassGuid;
  146. }
  147. /*
  148. * HidD_GetInputReport EXPORTED
  149. *
  150. * @implemented
  151. */
  152. HIDAPI
  153. BOOLEAN WINAPI
  154. HidD_GetInputReport(IN HANDLE HidDeviceObject,
  155. IN OUT PVOID ReportBuffer,
  156. IN ULONG ReportBufferLength)
  157. {
  158. DWORD RetLen;
  159. return DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_INPUT_REPORT,
  160. NULL, 0,
  161. ReportBuffer, ReportBufferLength,
  162. &RetLen, NULL) != 0;
  163. }
  164. /*
  165. * HidD_GetManufacturerString EXPORTED
  166. *
  167. * @implemented
  168. */
  169. HIDAPI
  170. BOOLEAN WINAPI
  171. HidD_GetManufacturerString(IN HANDLE HidDeviceObject,
  172. OUT PVOID Buffer,
  173. IN ULONG BufferLength)
  174. {
  175. DWORD RetLen;
  176. return DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_MANUFACTURER_STRING,
  177. NULL, 0,
  178. Buffer, BufferLength,
  179. &RetLen, NULL) != 0;
  180. }
  181. /*
  182. * HidD_GetNumInputBuffers EXPORTED
  183. *
  184. * @implemented
  185. */
  186. HIDAPI
  187. BOOLEAN WINAPI
  188. HidD_GetNumInputBuffers(IN HANDLE HidDeviceObject,
  189. OUT PULONG NumberBuffers)
  190. {
  191. DWORD RetLen;
  192. return DeviceIoControl(HidDeviceObject, IOCTL_GET_NUM_DEVICE_INPUT_BUFFERS,
  193. NULL, 0,
  194. NumberBuffers, sizeof(ULONG),
  195. &RetLen, NULL) != 0;
  196. }
  197. /*
  198. * HidD_GetPhysicalDescriptor EXPORTED
  199. *
  200. * @implemented
  201. */
  202. HIDAPI
  203. BOOLEAN WINAPI
  204. HidD_GetPhysicalDescriptor(IN HANDLE HidDeviceObject,
  205. OUT PVOID Buffer,
  206. IN ULONG BufferLength)
  207. {
  208. DWORD RetLen;
  209. return DeviceIoControl(HidDeviceObject, IOCTL_GET_PHYSICAL_DESCRIPTOR,
  210. NULL, 0,
  211. Buffer, BufferLength,
  212. &RetLen, NULL) != 0;
  213. }
  214. /*
  215. * HidD_GetPreparsedData EXPORTED
  216. *
  217. * @implemented
  218. */
  219. HIDAPI
  220. BOOLEAN WINAPI
  221. HidD_GetPreparsedData(IN HANDLE HidDeviceObject,
  222. OUT PHIDP_PREPARSED_DATA *PreparsedData)
  223. {
  224. HID_COLLECTION_INFORMATION hci;
  225. DWORD RetLen;
  226. BOOLEAN Ret;
  227. if(PreparsedData == NULL)
  228. {
  229. return FALSE;
  230. }
  231. if(!DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_COLLECTION_INFORMATION,
  232. NULL, 0,
  233. &hci, sizeof(HID_COLLECTION_INFORMATION),
  234. &RetLen, NULL))
  235. {
  236. return FALSE;
  237. }
  238. *PreparsedData = LocalAlloc(LHND, hci.DescriptorSize);
  239. if(*PreparsedData == NULL)
  240. {
  241. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  242. return FALSE;
  243. }
  244. Ret = DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_COLLECTION_DESCRIPTOR,
  245. NULL, 0,
  246. *PreparsedData, hci.DescriptorSize,
  247. &RetLen, NULL) != 0;
  248. if(!Ret)
  249. {
  250. /* FIXME - Free the buffer in case we failed to get the descriptor? */
  251. LocalFree((HLOCAL)*PreparsedData);
  252. }
  253. #if 0
  254. else
  255. {
  256. /* should we truncate the memory in case RetLen < hci.DescriptorSize? */
  257. }
  258. #endif
  259. return Ret;
  260. }
  261. /*
  262. * HidD_GetProductString EXPORTED
  263. *
  264. * @implemented
  265. */
  266. HIDAPI
  267. BOOLEAN WINAPI
  268. HidD_GetProductString(IN HANDLE HidDeviceObject,
  269. OUT PVOID Buffer,
  270. IN ULONG BufferLength)
  271. {
  272. DWORD RetLen;
  273. return DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_PRODUCT_STRING,
  274. NULL, 0,
  275. Buffer, BufferLength,
  276. &RetLen, NULL) != 0;
  277. }
  278. /*
  279. * HidD_GetSerialNumberString EXPORTED
  280. *
  281. * @implemented
  282. */
  283. HIDAPI
  284. BOOLEAN WINAPI
  285. HidD_GetSerialNumberString(IN HANDLE HidDeviceObject,
  286. OUT PVOID Buffer,
  287. IN ULONG BufferLength)
  288. {
  289. DWORD RetLen;
  290. return DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_SERIALNUMBER_STRING,
  291. NULL, 0,
  292. Buffer, BufferLength,
  293. &RetLen, NULL) != 0;
  294. }
  295. /*
  296. * HidP_GetValueCaps EXPORTED
  297. *
  298. * @implemented
  299. */
  300. HIDAPI
  301. NTSTATUS WINAPI
  302. HidP_GetValueCaps(IN HIDP_REPORT_TYPE ReportType,
  303. OUT PHIDP_VALUE_CAPS ValueCaps,
  304. IN OUT PULONG ValueCapsLength,
  305. IN PHIDP_PREPARSED_DATA PreparsedData)
  306. {
  307. return HidP_GetSpecificValueCaps(ReportType, 0, 0, 0, ValueCaps,
  308. ValueCapsLength, PreparsedData);
  309. }
  310. /*
  311. * HidD_Hello EXPORTED
  312. *
  313. * Undocumented easter egg function. It fills the buffer with "Hello\n"
  314. * and returns number of bytes filled in (lstrlen(Buffer) + 1 == 7)
  315. *
  316. * Bugs: - doesn't check Buffer for NULL
  317. * - always returns 7 even if BufferLength < 7 but doesn't produce a buffer overflow
  318. *
  319. * @implemented
  320. */
  321. HIDAPI
  322. ULONG WINAPI
  323. HidD_Hello(OUT PCHAR Buffer,
  324. IN ULONG BufferLength)
  325. {
  326. const CHAR HelloString[] = "Hello\n";
  327. if(BufferLength > 0)
  328. {
  329. memcpy(Buffer, HelloString, min(sizeof(HelloString), BufferLength));
  330. }
  331. return sizeof(HelloString);
  332. }
  333. /*
  334. * HidD_SetFeature EXPORTED
  335. *
  336. * @implemented
  337. */
  338. HIDAPI
  339. BOOLEAN WINAPI
  340. HidD_SetFeature(IN HANDLE HidDeviceObject,
  341. IN PVOID ReportBuffer,
  342. IN ULONG ReportBufferLength)
  343. {
  344. DWORD RetLen;
  345. return DeviceIoControl(HidDeviceObject, IOCTL_HID_SET_FEATURE,
  346. ReportBuffer, ReportBufferLength,
  347. NULL, 0,
  348. &RetLen, NULL) != 0;
  349. }
  350. /*
  351. * HidD_SetNumInputBuffers EXPORTED
  352. *
  353. * @implemented
  354. */
  355. HIDAPI
  356. BOOLEAN WINAPI
  357. HidD_SetNumInputBuffers(IN HANDLE HidDeviceObject,
  358. IN ULONG NumberBuffers)
  359. {
  360. DWORD RetLen;
  361. return DeviceIoControl(HidDeviceObject, IOCTL_SET_NUM_DEVICE_INPUT_BUFFERS,
  362. &NumberBuffers, sizeof(ULONG),
  363. NULL, 0,
  364. &RetLen, NULL) != 0;
  365. }
  366. /*
  367. * HidD_SetOutputReport EXPORTED
  368. *
  369. * @implemented
  370. */
  371. HIDAPI
  372. BOOLEAN WINAPI
  373. HidD_SetOutputReport(IN HANDLE HidDeviceObject,
  374. IN PVOID ReportBuffer,
  375. IN ULONG ReportBufferLength)
  376. {
  377. DWORD RetLen;
  378. return DeviceIoControl(HidDeviceObject, IOCTL_HID_SET_OUTPUT_REPORT,
  379. ReportBuffer, ReportBufferLength,
  380. NULL, 0,
  381. &RetLen, NULL) != 0;
  382. }
  383. /*
  384. * HidD_GetIndexedString EXPORTED
  385. *
  386. * @implemented
  387. */
  388. HIDAPI
  389. BOOLEAN WINAPI
  390. HidD_GetIndexedString(IN HANDLE HidDeviceObject,
  391. IN ULONG StringIndex,
  392. OUT PVOID Buffer,
  393. IN ULONG BufferLength)
  394. {
  395. DWORD RetLen;
  396. return DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_INDEXED_STRING,
  397. &StringIndex, sizeof(ULONG),
  398. Buffer, BufferLength,
  399. &RetLen, NULL) != 0;
  400. }
  401. /*
  402. * HidD_GetMsGenreDescriptor EXPORTED
  403. *
  404. * @implemented
  405. */
  406. HIDAPI
  407. BOOLEAN WINAPI
  408. HidD_GetMsGenreDescriptor(IN HANDLE HidDeviceObject,
  409. OUT PVOID Buffer,
  410. IN ULONG BufferLength)
  411. {
  412. DWORD RetLen;
  413. return DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_MS_GENRE_DESCRIPTOR,
  414. 0, 0,
  415. Buffer, BufferLength,
  416. &RetLen, NULL) != 0;
  417. }
  418. /*
  419. * HidD_GetConfiguration EXPORTED
  420. *
  421. * @implemented
  422. */
  423. HIDAPI
  424. BOOLEAN WINAPI
  425. HidD_GetConfiguration(IN HANDLE HidDeviceObject,
  426. OUT PHIDD_CONFIGURATION Configuration,
  427. IN ULONG ConfigurationLength)
  428. {
  429. // magic cookie
  430. Configuration->cookie = (PVOID)HidD_GetConfiguration;
  431. return DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_DRIVER_CONFIG,
  432. 0, 0,
  433. &Configuration->size, ConfigurationLength - sizeof(ULONG),
  434. (PULONG)&Configuration->cookie, NULL) != 0;
  435. }
  436. /*
  437. * HidD_SetConfiguration EXPORTED
  438. *
  439. * @implemented
  440. */
  441. HIDAPI
  442. BOOLEAN WINAPI
  443. HidD_SetConfiguration(IN HANDLE HidDeviceObject,
  444. IN PHIDD_CONFIGURATION Configuration,
  445. IN ULONG ConfigurationLength)
  446. {
  447. BOOLEAN Ret = FALSE;
  448. if (Configuration->cookie == (PVOID)HidD_GetConfiguration)
  449. {
  450. Ret = DeviceIoControl(HidDeviceObject, IOCTL_HID_SET_DRIVER_CONFIG,
  451. 0, 0,
  452. (PVOID)&Configuration->size, ConfigurationLength - sizeof(ULONG),
  453. (PULONG)&Configuration->cookie, NULL) != 0;
  454. }
  455. else
  456. {
  457. SetLastError(ERROR_INVALID_PARAMETER);
  458. }
  459. return Ret;
  460. }
  461. /*
  462. * HidP_GetUsagesEx EXPORTED
  463. *
  464. * @implemented
  465. */
  466. HIDAPI
  467. NTSTATUS WINAPI
  468. HidP_GetUsagesEx(IN HIDP_REPORT_TYPE ReportType,
  469. IN USHORT LinkCollection,
  470. OUT PUSAGE_AND_PAGE ButtonList,
  471. IN OUT ULONG *UsageLength,
  472. IN PHIDP_PREPARSED_DATA PreparsedData,
  473. IN PCHAR Report,
  474. IN ULONG ReportLength)
  475. {
  476. return HidP_GetUsages(ReportType, ButtonList->UsagePage, LinkCollection, &ButtonList->Usage, UsageLength, PreparsedData, Report, ReportLength);
  477. }
  478. /* EOF */