- Move atsmedia.h to PSDK, introduce pragma once and remove the comments block and...
[reactos.git] / dll / win32 / hid / hid.c
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$
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
31 HINSTANCE hDllInstance;
32
33 /* device interface GUID for HIDClass devices */
34 const GUID HidClassGuid = {0x4D1E55B2, 0xF16F, 0x11CF, {0x88,0xCB,0x00,0x11,0x11,0x00,0x00,0x30}};
35
36 BOOL WINAPI
37 DllMain(HINSTANCE hinstDLL,
38 DWORD dwReason,
39 LPVOID lpvReserved)
40 {
41 switch(dwReason)
42 {
43 case DLL_PROCESS_ATTACH:
44 hDllInstance = hinstDLL;
45 break;
46
47 case DLL_THREAD_ATTACH:
48 break;
49
50 case DLL_THREAD_DETACH:
51 break;
52
53 case DLL_PROCESS_DETACH:
54 break;
55 }
56 return TRUE;
57 }
58
59
60 /*
61 * HidD_FlushQueue EXPORTED
62 *
63 * @implemented
64 */
65 HIDAPI
66 BOOLEAN WINAPI
67 HidD_FlushQueue(IN HANDLE HidDeviceObject)
68 {
69 DWORD RetLen;
70 return DeviceIoControl(HidDeviceObject, IOCTL_HID_FLUSH_QUEUE,
71 NULL, 0,
72 NULL, 0,
73 &RetLen, NULL);
74 }
75
76
77 /*
78 * HidD_FreePreparsedData EXPORTED
79 *
80 * @implemented
81 */
82 HIDAPI
83 BOOLEAN WINAPI
84 HidD_FreePreparsedData(IN PHIDP_PREPARSED_DATA PreparsedData)
85 {
86 return (LocalFree((HLOCAL)PreparsedData) == NULL);
87 }
88
89
90 /*
91 * HidD_GetAttributes EXPORTED
92 *
93 * @implemented
94 */
95 HIDAPI
96 BOOLEAN WINAPI
97 HidD_GetAttributes(IN HANDLE HidDeviceObject,
98 OUT PHIDD_ATTRIBUTES Attributes)
99 {
100 HID_COLLECTION_INFORMATION hci;
101 DWORD RetLen;
102
103 if(!DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_COLLECTION_INFORMATION,
104 NULL, 0,
105 &hci, sizeof(HID_COLLECTION_INFORMATION),
106 &RetLen, NULL))
107 {
108 return FALSE;
109 }
110
111 /* copy the fields */
112 Attributes->Size = sizeof(HIDD_ATTRIBUTES);
113 Attributes->VendorID = hci.VendorID;
114 Attributes->ProductID = hci.ProductID;
115 Attributes->VersionNumber = hci.VersionNumber;
116
117 return TRUE;
118 }
119
120
121 /*
122 * HidP_GetButtonCaps EXPORTED
123 *
124 * @implemented
125 */
126 HIDAPI
127 NTSTATUS WINAPI
128 HidP_GetButtonCaps(IN HIDP_REPORT_TYPE ReportType,
129 OUT PHIDP_BUTTON_CAPS ButtonCaps,
130 IN OUT PULONG ButtonCapsLength,
131 IN PHIDP_PREPARSED_DATA PreparsedData)
132 {
133 return HidP_GetSpecificButtonCaps(ReportType, 0, 0, 0, ButtonCaps,
134 ButtonCapsLength, PreparsedData);
135 }
136
137
138 /*
139 * HidD_GetFeature EXPORTED
140 *
141 * @implemented
142 */
143 HIDAPI
144 BOOLEAN WINAPI
145 HidD_GetFeature(IN HANDLE HidDeviceObject,
146 OUT PVOID ReportBuffer,
147 IN ULONG ReportBufferLength)
148 {
149 DWORD RetLen;
150 return DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_FEATURE,
151 NULL, 0,
152 ReportBuffer, ReportBufferLength,
153 &RetLen, NULL);
154 }
155
156
157 /*
158 * HidD_GetHidGuid EXPORTED
159 *
160 * @implemented
161 */
162 HIDAPI
163 VOID WINAPI
164 HidD_GetHidGuid(OUT LPGUID HidGuid)
165 {
166 *HidGuid = HidClassGuid;
167 }
168
169
170 /*
171 * HidD_GetInputReport EXPORTED
172 *
173 * @implemented
174 */
175 HIDAPI
176 BOOLEAN WINAPI
177 HidD_GetInputReport(IN HANDLE HidDeviceObject,
178 IN OUT PVOID ReportBuffer,
179 IN ULONG ReportBufferLength)
180 {
181 DWORD RetLen;
182 return DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_INPUT_REPORT,
183 NULL, 0,
184 ReportBuffer, ReportBufferLength,
185 &RetLen, NULL);
186 }
187
188
189 /*
190 * HidD_GetManufacturerString EXPORTED
191 *
192 * @implemented
193 */
194 HIDAPI
195 BOOLEAN WINAPI
196 HidD_GetManufacturerString(IN HANDLE HidDeviceObject,
197 OUT PVOID Buffer,
198 IN ULONG BufferLength)
199 {
200 DWORD RetLen;
201 return DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_MANUFACTURER_STRING,
202 NULL, 0,
203 Buffer, BufferLength,
204 &RetLen, NULL);
205 }
206
207
208 /*
209 * HidD_GetNumInputBuffers EXPORTED
210 *
211 * @implemented
212 */
213 HIDAPI
214 BOOLEAN WINAPI
215 HidD_GetNumInputBuffers(IN HANDLE HidDeviceObject,
216 OUT PULONG NumberBuffers)
217 {
218 DWORD RetLen;
219 return DeviceIoControl(HidDeviceObject, IOCTL_GET_NUM_DEVICE_INPUT_BUFFERS,
220 NULL, 0,
221 NumberBuffers, sizeof(ULONG),
222 &RetLen, NULL);
223 }
224
225
226 /*
227 * HidD_GetPhysicalDescriptor EXPORTED
228 *
229 * @implemented
230 */
231 HIDAPI
232 BOOLEAN WINAPI
233 HidD_GetPhysicalDescriptor(IN HANDLE HidDeviceObject,
234 OUT PVOID Buffer,
235 IN ULONG BufferLength)
236 {
237 DWORD RetLen;
238 return DeviceIoControl(HidDeviceObject, IOCTL_GET_PHYSICAL_DESCRIPTOR,
239 NULL, 0,
240 Buffer, BufferLength,
241 &RetLen, NULL);
242 }
243
244
245 /*
246 * HidD_GetPreparsedData EXPORTED
247 *
248 * @implemented
249 */
250 HIDAPI
251 BOOLEAN WINAPI
252 HidD_GetPreparsedData(IN HANDLE HidDeviceObject,
253 OUT PHIDP_PREPARSED_DATA *PreparsedData)
254 {
255 HID_COLLECTION_INFORMATION hci;
256 DWORD RetLen;
257 BOOL Ret;
258
259 if(PreparsedData == NULL)
260 {
261 return FALSE;
262 }
263
264 if(!DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_COLLECTION_INFORMATION,
265 NULL, 0,
266 &hci, sizeof(HID_COLLECTION_INFORMATION),
267 &RetLen, NULL))
268 {
269 return FALSE;
270 }
271
272 *PreparsedData = LocalAlloc(LHND, hci.DescriptorSize);
273 if(*PreparsedData == NULL)
274 {
275 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
276 return FALSE;
277 }
278
279 Ret = DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_COLLECTION_DESCRIPTOR,
280 NULL, 0,
281 *PreparsedData, hci.DescriptorSize,
282 &RetLen, NULL);
283
284 if(!Ret)
285 {
286 /* FIXME - Free the buffer in case we failed to get the descriptor? */
287 LocalFree((HLOCAL)*PreparsedData);
288 }
289 #if 0
290 else
291 {
292 /* should we truncate the memory in case RetLen < hci.DescriptorSize? */
293 }
294 #endif
295
296 return Ret;
297 }
298
299
300 /*
301 * HidD_GetProductString EXPORTED
302 *
303 * @implemented
304 */
305 HIDAPI
306 BOOLEAN WINAPI
307 HidD_GetProductString(IN HANDLE HidDeviceObject,
308 OUT PVOID Buffer,
309 IN ULONG BufferLength)
310 {
311 DWORD RetLen;
312 return DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_PRODUCT_STRING,
313 NULL, 0,
314 Buffer, BufferLength,
315 &RetLen, NULL);
316 }
317
318
319 /*
320 * HidD_GetSerialNumberString EXPORTED
321 *
322 * @implemented
323 */
324 HIDAPI
325 BOOLEAN WINAPI
326 HidD_GetSerialNumberString(IN HANDLE HidDeviceObject,
327 OUT PVOID Buffer,
328 IN ULONG BufferLength)
329 {
330 DWORD RetLen;
331 return DeviceIoControl(HidDeviceObject, IOCTL_HID_GET_SERIALNUMBER_STRING,
332 NULL, 0,
333 Buffer, BufferLength,
334 &RetLen, NULL);
335 }
336
337
338 /*
339 * HidP_GetValueCaps EXPORTED
340 *
341 * @implemented
342 */
343 HIDAPI
344 NTSTATUS WINAPI
345 HidP_GetValueCaps(IN HIDP_REPORT_TYPE ReportType,
346 OUT PHIDP_VALUE_CAPS ValueCaps,
347 IN OUT PULONG ValueCapsLength,
348 IN PHIDP_PREPARSED_DATA PreparsedData)
349 {
350 return HidP_GetSpecificValueCaps(ReportType, 0, 0, 0, ValueCaps,
351 ValueCapsLength, PreparsedData);
352 }
353
354
355 /*
356 * HidD_Hello EXPORTED
357 *
358 * Undocumented easter egg function. It fills the buffer with "Hello\n"
359 * and returns number of bytes filled in (lstrlen(Buffer) + 1 == 7)
360 *
361 * Bugs: - doesn't check Buffer for NULL
362 * - always returns 7 even if BufferLength < 7 but doesn't produce a buffer overflow
363 *
364 * @implemented
365 */
366 HIDAPI
367 ULONG WINAPI
368 HidD_Hello(OUT PCHAR Buffer,
369 IN ULONG BufferLength)
370 {
371 const CHAR HelloString[] = "Hello\n";
372
373 if(BufferLength > 0)
374 {
375 memcpy(Buffer, HelloString, min(sizeof(HelloString), BufferLength));
376 }
377
378 return sizeof(HelloString);
379 }
380
381
382 /*
383 * HidD_SetFeature EXPORTED
384 *
385 * @implemented
386 */
387 HIDAPI
388 BOOLEAN WINAPI
389 HidD_SetFeature(IN HANDLE HidDeviceObject,
390 IN PVOID ReportBuffer,
391 IN ULONG ReportBufferLength)
392 {
393 DWORD RetLen;
394 return DeviceIoControl(HidDeviceObject, IOCTL_HID_SET_FEATURE,
395 ReportBuffer, ReportBufferLength,
396 NULL, 0,
397 &RetLen, NULL);
398 }
399
400
401 /*
402 * HidD_SetNumInputBuffers EXPORTED
403 *
404 * @implemented
405 */
406 HIDAPI
407 BOOLEAN WINAPI
408 HidD_SetNumInputBuffers(IN HANDLE HidDeviceObject,
409 IN ULONG NumberBuffers)
410 {
411 DWORD RetLen;
412 return DeviceIoControl(HidDeviceObject, IOCTL_SET_NUM_DEVICE_INPUT_BUFFERS,
413 &NumberBuffers, sizeof(ULONG),
414 NULL, 0,
415 &RetLen, NULL);
416 }
417
418
419 /*
420 * HidD_SetOutputReport EXPORTED
421 *
422 * @implemented
423 */
424 HIDAPI
425 BOOLEAN WINAPI
426 HidD_SetOutputReport(IN HANDLE HidDeviceObject,
427 IN PVOID ReportBuffer,
428 IN ULONG ReportBufferLength)
429 {
430 DWORD RetLen;
431 return DeviceIoControl(HidDeviceObject, IOCTL_HID_SET_OUTPUT_REPORT,
432 ReportBuffer, ReportBufferLength,
433 NULL, 0,
434 &RetLen, NULL);
435 }
436
437 /* EOF */