Debian Codegraph @

p5glove/libp5glove/usb_hid_win32.c

raw symbols SCIP
1 /*
2 Copyright (c) 2003 Alan Macek <www.alanmacek.com>
3 (c) 2004 Ross Bencina <[email protected]>
4
5 Win32 USB HID I/O routines
6
7 Based on usb.c code be Alan Macek
8 */
9 /*
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25 #include "usb_hid.h"
26 #include <windows.h>
27 #include <stdio.h>
28 #include <setupapi.h>
29
30 #ifdef HAVE_HIDSDI_DDK_HEADERS
31
32 /*
33 hidsdi.h and the files it includes are part of the Windows DDK. If you
34 don't have the DDK header files, define HAVE_HIDSDI_DDK_HEADERS, otherwise
35 the definitions in the #else block will be used.
36 */
37
38 # include <hidsdi.h>
39
40 #else
41
42 #include <pshpack4.h>
43
44 void __stdcall
45 HidD_GetHidGuid (
46 OUT LPGUID HidGuid
47 );
48
49 typedef struct _HIDD_ATTRIBUTES {
50 ULONG Size; // = sizeof (struct _HIDD_ATTRIBUTES)
51
52 //
53 // Vendor ids of this hid device
54 //
55 USHORT VendorID;
56 USHORT ProductID;
57 USHORT VersionNumber;
58
59 //
60 // Additional fields will be added to the end of this structure.
61 //
62 } HIDD_ATTRIBUTES, *PHIDD_ATTRIBUTES;
63
64 BOOLEAN __stdcall
65 HidD_GetAttributes (
66 IN HANDLE HidDeviceObject,
67 OUT PHIDD_ATTRIBUTES Attributes
68 );
69
70 BOOLEAN __stdcall
71 HidD_GetSerialNumberString (
72 IN HANDLE HidDeviceObject,
73 OUT PVOID Buffer,
74 IN ULONG BufferLength
75 );
76
77
78 BOOLEAN __stdcall
79 HidD_SetFeature(
80 IN HANDLE HidDeviceObject,
81 IN PVOID ReportBuffer,
82 IN ULONG ReportBufferLength
83 );
84
85 BOOLEAN __stdcall
86 HidD_GetFeature(
87 IN HANDLE HidDeviceObject,
88 OUT PVOID ReportBuffer,
89 IN ULONG ReportBufferLength
90 );
91
92 #include <poppack.h>
93
94 #endif
95
96 /*
97 typedef for enumeration function called by EnumHidDevices for every
98 USB HID device. Return non-zero to continue enumeration
99 */
100 typedef BOOL HidEnumProc( PSP_DEVICE_INTERFACE_DATA deviceInterfaceData,
101 PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData,
102 void *userData );
103
104
105 /*
106 call enumProc with enumProcData for every UDB HID device until enumProc
107 returns zero or all devices have been enumerated.
108 */
109 void EnumHidDevices( HidEnumProc* enumProc, void *enumProcData )
110 {
111 GUID hidGUID;
112 HDEVINFO hardwareDeviceInfoSet;
113 SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
114 PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData;
115 ULONG requiredSize;
116 DWORD index;
117 DWORD result;
118 BOOL enumOk;
119 BOOL continueEnumerating = TRUE;
120
121 /* Get the HID GUID value - used as mask to get list of devices */
122 HidD_GetHidGuid( &hidGUID );
123
124 /* Get a list of devices matching the criteria (hid interface, present) */
125 hardwareDeviceInfoSet = SetupDiGetClassDevs( &hidGUID,
126 NULL, /* no enumerator (global) */
127 NULL, /* no top level window */
128 (DIGCF_PRESENT | /* Only Devices present */
129 DIGCF_DEVICEINTERFACE) ); /* Function class devices. */
130
131 if( hardwareDeviceInfoSet != INVALID_HANDLE_VALUE ){
132
133 deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
134
135 index = 0;
136 do{
137 // enumerate the list and get the interface data
138 enumOk = SetupDiEnumDeviceInterfaces( hardwareDeviceInfoSet,
139 NULL, /* infoData */
140 &hidGUID, /* interfaceClassGuid */
141 index,
142 &deviceInterfaceData );
143
144 /*
145 regarding SetupDiEnumDeviceInterfaces msdn says:
146 You should first call this function with the MemberIndex parameter set
147 to zero to obtain the first interface. Then, repeatedly increment
148 MemberIndex and retrieve an interface until this function fails and
149 GetLastError returns ERROR_NO_MORE_ITEMS.
150
151 Rather than checking for ERROR_NO_MORE_ITEMS we halt on any error.
152 */
153
154 if( enumOk ){
155
156 /* Get the details with null values to get the required size of the buffer */
157 requiredSize = 0;
158 SetupDiGetDeviceInterfaceDetail( hardwareDeviceInfoSet,
159 &deviceInterfaceData,
160 NULL, /* interfaceDetail */
161 0, /* interfaceDetailSize */
162 &requiredSize,
163 0 ); /* infoData */
164 if( requiredSize != 0 ){
165 /* Allocate the buffer */
166 deviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(requiredSize);
167
168 if( deviceInterfaceDetailData != NULL ){
169 deviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
170
171 /* Fill the buffer with the device details */
172 result = SetupDiGetDeviceInterfaceDetail( hardwareDeviceInfoSet,
173 &deviceInterfaceData,
174 deviceInterfaceDetailData,
175 requiredSize,
176 &requiredSize,
177 NULL );
178 if( result != 0 ){
179
180 /* call the enumProc */
181 continueEnumerating = enumProc( &deviceInterfaceData, deviceInterfaceDetailData, enumProcData );
182
183 }
184
185 free (deviceInterfaceDetailData);
186 }
187 }
188
189 ++index;
190 }
191
192 }while( enumOk && continueEnumerating );
193
194 SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
195 }
196 }
197
198
199 typedef struct{
200 DWORD currentIndex;
201 DWORD deviceIndex;
202 USHORT vendorId, productId, versionNumber;
203 int flags;
204 HANDLE deviceHandle;
205 } FindHidDeviceEnumData;
206
207
208 BOOL FindHidDeviceEnumProc( PSP_DEVICE_INTERFACE_DATA deviceInterfaceData,
209 PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData,
210 void *userData )
211 {
212 HANDLE deviceHandle;
213 HIDD_ATTRIBUTES deviceAttributes;
214 FindHidDeviceEnumData *data = (FindHidDeviceEnumData*)userData;
215
216 (void) deviceInterfaceData; /* unused parameter */
217
218 /* Open file on the device */
219 deviceHandle = CreateFile( deviceInterfaceDetailData->DevicePath,
220 GENERIC_READ,
221 FILE_SHARE_READ | FILE_SHARE_WRITE,
222 NULL, /* no SECURITY_ATTRIBUTES structure */
223 OPEN_EXISTING, /* No special create flags */
224 0,
225 NULL ); /* No template file */
226
227 if( deviceHandle != INVALID_HANDLE_VALUE ){
228
229 deviceAttributes.Size = sizeof(HIDD_ATTRIBUTES);
230 if( HidD_GetAttributes( deviceHandle, &deviceAttributes ) == TRUE
231 && ( !(data->flags & SELECT_VENDOR_ID_FLAG) || deviceAttributes.VendorID == data->vendorId )
232 && ( !(data->flags & SELECT_PRODUCT_ID_FLAG) || deviceAttributes.ProductID == data->productId )
233 && ( !(data->flags & SELECT_VERSION_NUMBER_FLAG) || deviceAttributes.VersionNumber == data->versionNumber ) ){
234
235 /* found a device which matches the selection criteria */
236
237 if( data->currentIndex == data->deviceIndex ){
238
239 /* found the requested device */
240
241 data->deviceHandle = deviceHandle;
242 return FALSE; /* stop enumerating */
243
244 }else{
245 CloseHandle( deviceHandle );
246 }
247
248 ++data->currentIndex;
249
250 }else{
251 CloseHandle( deviceHandle );
252 }
253 }
254
255 return TRUE; /* continue enumerating */
256 }
257
258
259 USBHID OpenUSBHID( int index, int vendorId, int productId, int versionNumber, int flags )
260 {
261 FindHidDeviceEnumData data;
262
263 data.currentIndex = 0;
264 data.deviceIndex = index;
265 data.vendorId = (USHORT)vendorId;
266 data.productId = (USHORT)productId;
267 data.versionNumber = (USHORT)versionNumber;
268 data.flags = flags;
269 data.deviceHandle = INVALID_HANDLE_VALUE;
270
271 EnumHidDevices( FindHidDeviceEnumProc, &data );
272
273 if( data.deviceHandle == INVALID_HANDLE_VALUE )
274 return INVALID_USBHID_VALUE;
275 else
276 return (USBHID)data.deviceHandle;
277 }
278
279
280 BOOL DumpHidDeviceInfoEnumProc( PSP_DEVICE_INTERFACE_DATA deviceInterfaceData,
281 PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData,
282 void *userData )
283 {
284 HANDLE deviceHandle;
285 HIDD_ATTRIBUTES deviceAttributes;
286 wchar_t s[128];
287
288 (void) deviceInterfaceData; /* unused parameter */
289 (void) userData;
290
291 printf( "Device Path: %s\n", deviceInterfaceDetailData->DevicePath );
292
293 deviceHandle = CreateFile( deviceInterfaceDetailData->DevicePath,
294 GENERIC_READ,
295 FILE_SHARE_READ | FILE_SHARE_WRITE,
296 NULL, /* no SECURITY_ATTRIBUTES structure */
297 OPEN_EXISTING, /* No special create flags */
298 0,
299 NULL ); /* No template file */
300
301 if( deviceHandle != INVALID_HANDLE_VALUE ){
302
303 deviceAttributes.Size = sizeof(HIDD_ATTRIBUTES);
304 if( HidD_GetAttributes( deviceHandle, &deviceAttributes ) ){
305
306 printf( "Vendor ID: %d\n", deviceAttributes.VendorID );
307 printf( "Product ID: %d\n", deviceAttributes.ProductID );
308 printf( "Version Number: %d\n", deviceAttributes.VersionNumber );
309 }
310
311 if( HidD_GetSerialNumberString( deviceHandle, s, 128 ) )
312 printf( "Serial Number: `%ls' (len:%d, 1st char:%d)\n", s, wcslen(s), s[0] );
313
314
315 CloseHandle( deviceHandle );
316 }
317
318 printf( "\n" );
319
320 return TRUE; /* continue enumerating */
321 }
322
323
324 void CloseUSBHID( USBHID handle )
325 {
326 CloseHandle( (HANDLE)handle );
327 }
328
329 int ReadUSBHID( USBHID handle, void *dest, int count )
330 {
331 DWORD bytesRead;
332
333 ReadFile( (HANDLE)handle, dest, count, &bytesRead, 0 );
334
335 return bytesRead;
336 }
337
338 int SetUSBHIDFeature( USBHID handle, char *report, int count )
339 {
340 return HidD_SetFeature( handle, report, count ) == TRUE ? 0 : -EINVAL;
341 }
342
343 void GetUSBHIDFeature( USBHID handle, char *report, int count )
344 {
345 return HidD_GetFeature( handle, report, count ) == TRUE ? 0 : -EINVAL;
346 }