ReactOS 0.4.15-dev-7897-g78dc504
hid.c
Go to the documentation of this file.
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/*
20 * PROJECT: ReactOS Hid User Library
21 * FILE: lib/hid/hid.c
22 * PURPOSE: ReactOS Hid User Library
23 * PROGRAMMER: Thomas Weidenmueller <w3seek@reactos.com>
24 *
25 * UPDATE HISTORY:
26 * 07/12/2004 Created
27 */
28
29#include "precomp.h"
30#include <stdarg.h>
31
32#include <winbase.h>
33
34#define NDEBUG
35#include <debug.h>
36#include "hidp.h"
37
39
40/* device interface GUID for HIDClass devices */
41const GUID HidClassGuid = {0x4D1E55B2, 0xF16F, 0x11CF, {0x88,0xCB,0x00,0x11,0x11,0x00,0x00,0x30}};
42
47{
48 return LocalAlloc(LHND, ItemSize);
49}
50
51VOID
55{
57}
58
59VOID
64{
65 memset(Item, 0, ItemSize);
66}
67
68VOID
74{
76}
77
78VOID
81 IN LPCSTR FormatStr, ...)
82{
83#if 0
85 va_start(list, FormatStr);
86 vDbgPrintEx(FormatStr, list);
87 va_end(list);
88#endif
89}
90
95{
96 switch(dwReason)
97 {
99 hDllInstance = hinstDLL;
100 break;
101
103 break;
104
106 break;
107
109 break;
110 }
111 return TRUE;
112}
113
114
115/*
116 * HidD_FlushQueue EXPORTED
117 *
118 * @implemented
119 */
120HIDAPI
122HidD_FlushQueue(IN HANDLE HidDeviceObject)
123{
124 DWORD RetLen;
125 return DeviceIoControl(HidDeviceObject, IOCTL_HID_FLUSH_QUEUE,
126 NULL, 0,
127 NULL, 0,
128 &RetLen, NULL) != 0;
129}
130
131
132/*
133 * HidD_FreePreparsedData EXPORTED
134 *
135 * @implemented
136 */
137HIDAPI
140{
141 return (LocalFree((HLOCAL)PreparsedData) == NULL);
142}
143
144
145/*
146 * HidD_GetAttributes EXPORTED
147 *
148 * @implemented
149 */
150HIDAPI
154{
156 DWORD RetLen;
157
159 NULL, 0,
160 &hci, sizeof(HID_COLLECTION_INFORMATION),
161 &RetLen, NULL))
162 {
163 return FALSE;
164 }
165
166 /* copy the fields */
167 Attributes->Size = sizeof(HIDD_ATTRIBUTES);
168 Attributes->VendorID = hci.VendorID;
169 Attributes->ProductID = hci.ProductID;
170 Attributes->VersionNumber = hci.VersionNumber;
171
172 return TRUE;
173}
174
175
176/*
177 * HidD_GetFeature EXPORTED
178 *
179 * @implemented
180 */
181HIDAPI
183HidD_GetFeature(IN HANDLE HidDeviceObject,
184 OUT PVOID ReportBuffer,
185 IN ULONG ReportBufferLength)
186{
187 DWORD RetLen;
188 return DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_FEATURE,
189 NULL, 0,
190 ReportBuffer, ReportBufferLength,
191 &RetLen, NULL) != 0;
192}
193
194
195/*
196 * HidD_GetHidGuid EXPORTED
197 *
198 * @implemented
199 */
200HIDAPI
203{
204 *HidGuid = HidClassGuid;
205}
206
207
208/*
209 * HidD_GetInputReport EXPORTED
210 *
211 * @implemented
212 */
213HIDAPI
216 IN OUT PVOID ReportBuffer,
217 IN ULONG ReportBufferLength)
218{
219 DWORD RetLen;
220 return DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_INPUT_REPORT,
221 NULL, 0,
222 ReportBuffer, ReportBufferLength,
223 &RetLen, NULL) != 0;
224}
225
226
227/*
228 * HidD_GetManufacturerString EXPORTED
229 *
230 * @implemented
231 */
232HIDAPI
237{
238 DWORD RetLen;
240 NULL, 0,
242 &RetLen, NULL) != 0;
243}
244
245
246/*
247 * HidD_GetNumInputBuffers EXPORTED
248 *
249 * @implemented
250 */
251HIDAPI
254 OUT PULONG NumberBuffers)
255{
256 DWORD RetLen;
258 NULL, 0,
259 NumberBuffers, sizeof(ULONG),
260 &RetLen, NULL) != 0;
261}
262
263
264/*
265 * HidD_GetPhysicalDescriptor EXPORTED
266 *
267 * @implemented
268 */
269HIDAPI
274{
275 DWORD RetLen;
276 return DeviceIoControl(HidDeviceObject, IOCTL_GET_PHYSICAL_DESCRIPTOR,
277 NULL, 0,
279 &RetLen, NULL) != 0;
280}
281
282
283/*
284 * HidD_GetPreparsedData EXPORTED
285 *
286 * @implemented
287 */
288HIDAPI
292{
294 DWORD RetLen;
295 BOOLEAN Ret;
296
297 if(PreparsedData == NULL)
298 {
299 return FALSE;
300 }
301
303 NULL, 0,
304 &hci, sizeof(HID_COLLECTION_INFORMATION),
305 &RetLen, NULL))
306 {
307 return FALSE;
308 }
309
311 if(*PreparsedData == NULL)
312 {
314 return FALSE;
315 }
316
318 NULL, 0,
320 &RetLen, NULL) != 0;
321
322 if(!Ret)
323 {
324 /* FIXME - Free the buffer in case we failed to get the descriptor? */
326 }
327#if 0
328 else
329 {
330 /* should we truncate the memory in case RetLen < hci.DescriptorSize? */
331 }
332#endif
333
334 return Ret;
335}
336
337
338/*
339 * HidD_GetProductString EXPORTED
340 *
341 * @implemented
342 */
343HIDAPI
348{
349 DWORD RetLen;
350 return DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_PRODUCT_STRING,
351 NULL, 0,
353 &RetLen, NULL) != 0;
354}
355
356
357/*
358 * HidD_GetSerialNumberString EXPORTED
359 *
360 * @implemented
361 */
362HIDAPI
367{
368 DWORD RetLen;
370 NULL, 0,
372 &RetLen, NULL) != 0;
373}
374
375
376/*
377 * HidD_Hello EXPORTED
378 *
379 * Undocumented easter egg function. It fills the buffer with "Hello\n"
380 * and returns number of bytes filled in (lstrlen(Buffer) + 1 == 7)
381 *
382 * Bugs: - doesn't check Buffer for NULL
383 * - always returns 7 even if BufferLength < 7 but doesn't produce a buffer overflow
384 *
385 * @implemented
386 */
387HIDAPI
391{
392 const CHAR HelloString[] = "Hello\n";
393
394 if(BufferLength > 0)
395 {
396 memcpy(Buffer, HelloString, min(sizeof(HelloString), BufferLength));
397 }
398
399 return sizeof(HelloString);
400}
401
402
403/*
404 * HidD_SetFeature EXPORTED
405 *
406 * @implemented
407 */
408HIDAPI
410HidD_SetFeature(IN HANDLE HidDeviceObject,
411 IN PVOID ReportBuffer,
412 IN ULONG ReportBufferLength)
413{
414 DWORD RetLen;
415 return DeviceIoControl(HidDeviceObject, IOCTL_HID_SET_FEATURE,
416 ReportBuffer, ReportBufferLength,
417 NULL, 0,
418 &RetLen, NULL) != 0;
419}
420
421
422/*
423 * HidD_SetNumInputBuffers EXPORTED
424 *
425 * @implemented
426 */
427HIDAPI
430 IN ULONG NumberBuffers)
431{
432 DWORD RetLen;
434 &NumberBuffers, sizeof(ULONG),
435 NULL, 0,
436 &RetLen, NULL) != 0;
437}
438
439
440/*
441 * HidD_SetOutputReport EXPORTED
442 *
443 * @implemented
444 */
445HIDAPI
448 IN PVOID ReportBuffer,
449 IN ULONG ReportBufferLength)
450{
451 DWORD RetLen;
452 return DeviceIoControl(HidDeviceObject, IOCTL_HID_SET_OUTPUT_REPORT,
453 ReportBuffer, ReportBufferLength,
454 NULL, 0,
455 &RetLen, NULL) != 0;
456}
457
458/*
459 * HidD_GetIndexedString EXPORTED
460 *
461 * @implemented
462 */
463HIDAPI
469{
470 DWORD RetLen;
471 return DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_INDEXED_STRING,
472 &StringIndex, sizeof(ULONG),
474 &RetLen, NULL) != 0;
475}
476
477/*
478 * HidD_GetMsGenreDescriptor EXPORTED
479 *
480 * @implemented
481 */
482HIDAPI
487{
488 DWORD RetLen;
490 0, 0,
492 &RetLen, NULL) != 0;
493}
494
495/*
496 * HidD_GetConfiguration EXPORTED
497 *
498 * @implemented
499 */
500HIDAPI
504 IN ULONG ConfigurationLength)
505{
506
507 // magic cookie
509
510 return DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_DRIVER_CONFIG,
511 0, 0,
512 &Configuration->size, ConfigurationLength - sizeof(ULONG),
513 (PULONG)&Configuration->cookie, NULL) != 0;
514}
515
516/*
517 * HidD_SetConfiguration EXPORTED
518 *
519 * @implemented
520 */
521HIDAPI
525 IN ULONG ConfigurationLength)
526{
527 BOOLEAN Ret = FALSE;
528
530 {
531 Ret = DeviceIoControl(HidDeviceObject, IOCTL_HID_SET_DRIVER_CONFIG,
532 0, 0,
533 (PVOID)&Configuration->size, ConfigurationLength - sizeof(ULONG),
534 (PULONG)&Configuration->cookie, NULL) != 0;
535 }
536 else
537 {
539 }
540
541 return Ret;
542}
543
544/* EOF */
unsigned char BOOLEAN
#define __cdecl
Definition: accygwin.h:79
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define va_arg(ap, T)
Definition: acmsvcex.h:89
NTSYSAPI ULONG NTAPI vDbgPrintEx(_In_ ULONG ComponentId, _In_ ULONG Level, _In_z_ PCCH Format, _In_ va_list ap)
DWORD dwReason
Definition: misc.cpp:154
Definition: bufpool.h:45
Definition: list.h:37
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
BOOL WINAPI DeviceIoControl(IN HANDLE hDevice, IN DWORD dwIoControlCode, IN LPVOID lpInBuffer OPTIONAL, IN DWORD nInBufferSize OPTIONAL, OUT LPVOID lpOutBuffer OPTIONAL, IN DWORD nOutBufferSize OPTIONAL, OUT LPDWORD lpBytesReturned OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: deviceio.c:136
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
#define DLL_THREAD_DETACH
Definition: compat.h:133
#define DLL_PROCESS_ATTACH
Definition: compat.h:131
#define DLL_PROCESS_DETACH
Definition: compat.h:130
#define SetLastError(x)
Definition: compat.h:752
#define DLL_THREAD_ATTACH
Definition: compat.h:132
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1390
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1594
HIDAPI BOOLEAN WINAPI HidD_GetFeature(IN HANDLE HidDeviceObject, OUT PVOID ReportBuffer, IN ULONG ReportBufferLength)
Definition: hid.c:183
VOID NTAPI ZeroFunction(IN PVOID Item, IN ULONG ItemSize)
Definition: hid.c:61
HIDAPI BOOLEAN WINAPI HidD_GetManufacturerString(IN HANDLE HidDeviceObject, OUT PVOID Buffer, IN ULONG BufferLength)
Definition: hid.c:234
HIDAPI BOOLEAN WINAPI HidD_SetConfiguration(IN HANDLE HidDeviceObject, IN PHIDD_CONFIGURATION Configuration, IN ULONG ConfigurationLength)
Definition: hid.c:523
HIDAPI BOOLEAN WINAPI HidD_SetOutputReport(IN HANDLE HidDeviceObject, IN PVOID ReportBuffer, IN ULONG ReportBufferLength)
Definition: hid.c:447
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpvReserved)
Definition: hid.c:92
HIDAPI BOOLEAN WINAPI HidD_GetConfiguration(IN HANDLE HidDeviceObject, OUT PHIDD_CONFIGURATION Configuration, IN ULONG ConfigurationLength)
Definition: hid.c:502
HIDAPI BOOLEAN WINAPI HidD_GetSerialNumberString(IN HANDLE HidDeviceObject, OUT PVOID Buffer, IN ULONG BufferLength)
Definition: hid.c:364
HIDAPI BOOLEAN WINAPI HidD_SetFeature(IN HANDLE HidDeviceObject, IN PVOID ReportBuffer, IN ULONG ReportBufferLength)
Definition: hid.c:410
HIDAPI BOOLEAN WINAPI HidD_GetPreparsedData(IN HANDLE HidDeviceObject, OUT PHIDP_PREPARSED_DATA *PreparsedData)
Definition: hid.c:290
VOID __cdecl DebugFunction(IN LPCSTR FormatStr,...)
Definition: hid.c:80
HIDAPI BOOLEAN WINAPI HidD_GetNumInputBuffers(IN HANDLE HidDeviceObject, OUT PULONG NumberBuffers)
Definition: hid.c:253
HIDAPI ULONG WINAPI HidD_Hello(OUT PCHAR Buffer, IN ULONG BufferLength)
Definition: hid.c:389
HIDAPI VOID WINAPI HidD_GetHidGuid(OUT LPGUID HidGuid)
Definition: hid.c:202
const GUID HidClassGuid
Definition: hid.c:41
HIDAPI BOOLEAN WINAPI HidD_GetAttributes(IN HANDLE HidDeviceObject, OUT PHIDD_ATTRIBUTES Attributes)
Definition: hid.c:152
HIDAPI BOOLEAN WINAPI HidD_GetIndexedString(IN HANDLE HidDeviceObject, IN ULONG StringIndex, OUT PVOID Buffer, IN ULONG BufferLength)
Definition: hid.c:465
HIDAPI BOOLEAN WINAPI HidD_GetInputReport(IN HANDLE HidDeviceObject, IN OUT PVOID ReportBuffer, IN ULONG ReportBufferLength)
Definition: hid.c:215
HIDAPI BOOLEAN WINAPI HidD_GetProductString(IN HANDLE HidDeviceObject, OUT PVOID Buffer, IN ULONG BufferLength)
Definition: hid.c:345
HIDAPI BOOLEAN WINAPI HidD_FreePreparsedData(IN PHIDP_PREPARSED_DATA PreparsedData)
Definition: hid.c:139
PVOID NTAPI AllocFunction(IN ULONG ItemSize)
Definition: hid.c:45
HIDAPI BOOLEAN WINAPI HidD_GetPhysicalDescriptor(IN HANDLE HidDeviceObject, OUT PVOID Buffer, IN ULONG BufferLength)
Definition: hid.c:271
VOID NTAPI FreeFunction(IN PVOID Item)
Definition: hid.c:53
HIDAPI BOOLEAN WINAPI HidD_SetNumInputBuffers(IN HANDLE HidDeviceObject, IN ULONG NumberBuffers)
Definition: hid.c:429
HIDAPI BOOLEAN WINAPI HidD_FlushQueue(IN HANDLE HidDeviceObject)
Definition: hid.c:122
HIDAPI BOOLEAN WINAPI HidD_GetMsGenreDescriptor(IN HANDLE HidDeviceObject, OUT PVOID Buffer, IN ULONG BufferLength)
Definition: hid.c:484
HINSTANCE hDllInstance
Definition: hid.c:38
#define IOCTL_HID_GET_DRIVER_CONFIG
Definition: hidclass.h:74
#define IOCTL_HID_GET_MANUFACTURER_STRING
Definition: hidclass.h:64
#define IOCTL_HID_SET_OUTPUT_REPORT
Definition: hidclass.h:71
#define IOCTL_HID_GET_COLLECTION_DESCRIPTOR
Definition: hidclass.h:58
#define IOCTL_HID_GET_PRODUCT_STRING
Definition: hidclass.h:67
#define IOCTL_GET_NUM_DEVICE_INPUT_BUFFERS
Definition: hidclass.h:65
#define IOCTL_HID_GET_INDEXED_STRING
Definition: hidclass.h:76
#define IOCTL_SET_NUM_DEVICE_INPUT_BUFFERS
Definition: hidclass.h:70
#define IOCTL_HID_GET_SERIALNUMBER_STRING
Definition: hidclass.h:68
#define IOCTL_HID_GET_INPUT_REPORT
Definition: hidclass.h:63
#define IOCTL_HID_GET_MS_GENRE_DESCRIPTOR
Definition: hidclass.h:77
#define IOCTL_HID_SET_FEATURE
Definition: hidclass.h:69
#define IOCTL_HID_FLUSH_QUEUE
Definition: hidclass.h:57
#define IOCTL_GET_PHYSICAL_DESCRIPTOR
Definition: hidclass.h:56
#define IOCTL_HID_SET_DRIVER_CONFIG
Definition: hidclass.h:75
#define IOCTL_HID_GET_FEATURE
Definition: hidclass.h:60
#define IOCTL_HID_GET_COLLECTION_INFORMATION
Definition: hidclass.h:59
_Must_inspect_result_ _Out_writes_to_ DataLength PHIDP_DATA _Inout_ PULONG _In_ PHIDP_PREPARSED_DATA PreparsedData
Definition: hidpi.h:335
#define HIDAPI
Definition: hidpi.h:35
struct _HIDP_PREPARSED_DATA * PHIDP_PREPARSED_DATA
Definition: hidpi.h:39
struct _HIDD_ATTRIBUTES HIDD_ATTRIBUTES
static IN DWORD IN LPVOID lpvReserved
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define min(a, b)
Definition: monoChain.cc:55
_In_ UINT _In_ UINT _In_ PNDIS_PACKET Source
Definition: ndis.h:3169
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
#define list
Definition: rosglue.h:35
static UCHAR ItemSize[4]
Definition: parser.c:16
#define memset(x, y, z)
Definition: compat.h:39
uint32_t * PULONG
Definition: typedefs.h:59
#define NTAPI
Definition: typedefs.h:36
void * PVOID
Definition: typedefs.h:50
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
char * PCHAR
Definition: typedefs.h:51
_In_ WDFCOLLECTION _In_ WDFOBJECT Item
_Must_inspect_result_ _In_ WDFDMAENABLER _In_ _In_opt_ PWDF_OBJECT_ATTRIBUTES Attributes
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ ULONG BufferLength
Definition: wdfdevice.h:3771
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_INTERRUPT_CONFIG Configuration
Definition: wdfinterrupt.h:374
_In_ WDFIOTARGET Target
Definition: wdfrequest.h:306
_Must_inspect_result_ _In_ WDFUSBDEVICE _In_opt_ WDFREQUEST _In_opt_ PWDF_REQUEST_SEND_OPTIONS _Out_writes_opt_ NumCharacters PUSHORT _Inout_ PUSHORT _In_ UCHAR StringIndex
Definition: wdfusb.h:1080
#define LHND
Definition: winbase.h:382
#define WINAPI
Definition: msvc.h:6
_Inout_ PSIZE_T _In_opt_ PMDLX _In_ MM_ROTATE_DIRECTION _In_ PMM_ROTATE_COPY_CALLBACK_FUNCTION CopyFunction
Definition: mmfuncs.h:775
const char * LPCSTR
Definition: xmlstorage.h:183
char CHAR
Definition: xmlstorage.h:175