2 * WINE HID Pseudo-Plug and Play support
4 * Copyright 2015 Aric Stewart
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define NONAMELESSUNION
25 #include "ddk/hidtypes.h"
27 #include "wine/debug.h"
28 #include "wine/unicode.h"
29 #include "wine/list.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(hid
);
33 static const WCHAR device_enumeratorW
[] = {'H','I','D',0};
34 static const WCHAR separator_W
[] = {'\\',0};
35 static const WCHAR device_deviceid_fmtW
[] = {'%','s','\\',
36 'v','i','d','_','%','0','4','x','&','p','i','d','_','%', '0','4','x',0};
38 static NTSTATUS WINAPI
internalComplete(DEVICE_OBJECT
*deviceObject
, IRP
*irp
,
41 HANDLE event
= context
;
43 return STATUS_MORE_PROCESSING_REQUIRED
;
46 static NTSTATUS
get_device_id(DEVICE_OBJECT
*device
, BUS_QUERY_ID_TYPE type
, WCHAR
**id
)
49 IO_STACK_LOCATION
*irpsp
;
50 IO_STATUS_BLOCK irp_status
;
54 irp
= IoBuildSynchronousFsdRequest(IRP_MJ_PNP
, device
, NULL
, 0, NULL
, NULL
, &irp_status
);
56 return STATUS_NO_MEMORY
;
58 event
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
59 irpsp
= IoGetNextIrpStackLocation(irp
);
60 irpsp
->MinorFunction
= IRP_MN_QUERY_ID
;
61 irpsp
->Parameters
.QueryId
.IdType
= type
;
63 IoSetCompletionRoutine(irp
, internalComplete
, event
, TRUE
, TRUE
, TRUE
);
64 status
= IoCallDriver(device
, irp
);
65 if (status
== STATUS_PENDING
)
66 WaitForSingleObject(event
, INFINITE
);
68 *id
= (WCHAR
*)irp
->IoStatus
.Information
;
69 status
= irp
->IoStatus
.u
.Status
;
70 IoCompleteRequest(irp
, IO_NO_INCREMENT
);
76 NTSTATUS WINAPI
PNP_AddDevice(DRIVER_OBJECT
*driver
, DEVICE_OBJECT
*PDO
)
79 DEVICE_OBJECT
*device
= NULL
;
81 minidriver
*minidriver
;
82 HID_DEVICE_ATTRIBUTES attr
;
83 BASE_DEVICE_EXTENSION
*ext
= NULL
;
84 HID_DESCRIPTOR descriptor
;
85 BYTE
*reportDescriptor
;
90 status
= get_device_id(PDO
, BusQueryInstanceID
, &PDO_id
);
91 if (status
!= STATUS_SUCCESS
)
93 ERR("Failed to get PDO id(%x)\n",status
);
97 TRACE("PDO add device(%p:%s)\n", PDO
, debugstr_w(PDO_id
));
98 minidriver
= find_minidriver(driver
);
100 hiddev
= HeapAlloc(GetProcessHeap(), 0, sizeof(*hiddev
));
102 return STATUS_NO_MEMORY
;
104 status
= HID_CreateDevice(PDO
, &minidriver
->minidriver
, &hiddev
->device
);
105 if (status
!= STATUS_SUCCESS
)
107 ERR("Failed to create HID object (%x)\n",status
);
108 HeapFree(GetProcessHeap(), 0, PDO_id
);
109 HeapFree(GetProcessHeap(), 0, hiddev
);
112 device
= hiddev
->device
;
114 ext
= device
->DeviceExtension
;
115 InitializeListHead(&ext
->irp_queue
);
117 TRACE("Created device %p\n",device
);
118 status
= minidriver
->AddDevice(minidriver
->minidriver
.DriverObject
, device
);
119 if (status
!= STATUS_SUCCESS
)
121 ERR("Minidriver AddDevice failed (%x)\n",status
);
122 HeapFree(GetProcessHeap(), 0, PDO_id
);
123 HID_DeleteDevice(&minidriver
->minidriver
, device
);
127 status
= call_minidriver(IOCTL_HID_GET_DEVICE_ATTRIBUTES
, device
,
128 NULL
, 0, &attr
, sizeof(attr
));
130 if (status
!= STATUS_SUCCESS
)
132 ERR("Minidriver failed to get Attributes(%x)\n",status
);
133 HID_DeleteDevice(&minidriver
->minidriver
, device
);
134 HeapFree(GetProcessHeap(), 0, PDO_id
);
138 ext
->information
.VendorID
= attr
.VendorID
;
139 ext
->information
.ProductID
= attr
.ProductID
;
140 ext
->information
.VersionNumber
= attr
.VersionNumber
;
141 ext
->information
.Polled
= minidriver
->minidriver
.DevicesArePolled
;
143 status
= call_minidriver(IOCTL_HID_GET_DEVICE_DESCRIPTOR
, device
, NULL
, 0,
144 &descriptor
, sizeof(descriptor
));
145 if (status
!= STATUS_SUCCESS
)
147 ERR("Cannot get Device Descriptor(%x)\n",status
);
148 HID_DeleteDevice(&minidriver
->minidriver
, device
);
149 HeapFree(GetProcessHeap(), 0, PDO_id
);
152 for (i
= 0; i
< descriptor
.bNumDescriptors
; i
++)
153 if (descriptor
.DescriptorList
[i
].bReportType
== HID_REPORT_DESCRIPTOR_TYPE
)
156 if (i
>= descriptor
.bNumDescriptors
)
158 ERR("No Report Descriptor found in reply\n");
159 HID_DeleteDevice(&minidriver
->minidriver
, device
);
160 HeapFree(GetProcessHeap(), 0, PDO_id
);
164 reportDescriptor
= HeapAlloc(GetProcessHeap(), 0, descriptor
.DescriptorList
[i
].wReportLength
);
165 status
= call_minidriver(IOCTL_HID_GET_REPORT_DESCRIPTOR
, device
, NULL
, 0,
166 reportDescriptor
, descriptor
.DescriptorList
[i
].wReportLength
);
167 if (status
!= STATUS_SUCCESS
)
169 ERR("Cannot get Report Descriptor(%x)\n",status
);
170 HID_DeleteDevice(&minidriver
->minidriver
, device
);
171 HeapFree(GetProcessHeap(), 0, reportDescriptor
);
172 HeapFree(GetProcessHeap(), 0, PDO_id
);
176 ext
->preparseData
= ParseDescriptor(reportDescriptor
, descriptor
.DescriptorList
[0].wReportLength
);
178 HeapFree(GetProcessHeap(), 0, reportDescriptor
);
179 if (!ext
->preparseData
)
181 ERR("Cannot parse Report Descriptor\n");
182 HID_DeleteDevice(&minidriver
->minidriver
, device
);
183 HeapFree(GetProcessHeap(), 0, PDO_id
);
184 return STATUS_NOT_SUPPORTED
;
187 list_add_tail(&(minidriver
->device_list
), &hiddev
->entry
);
189 ext
->information
.DescriptorSize
= ext
->preparseData
->dwSize
;
191 lstrcpyW(ext
->instance_id
, device_enumeratorW
);
192 strcatW(ext
->instance_id
, separator_W
);
193 /* Skip the original enumerator */
194 id_ptr
= strchrW(PDO_id
, '\\');
196 strcatW(ext
->instance_id
, id_ptr
);
197 HeapFree(GetProcessHeap(), 0, PDO_id
);
199 sprintfW(ext
->device_id
, device_deviceid_fmtW
, device_enumeratorW
, ext
->information
.VendorID
, ext
->information
.ProductID
);
201 HID_LinkDevice(device
);
203 ext
->poll_interval
= DEFAULT_POLL_INTERVAL
;
205 ext
->ring_buffer
= RingBuffer_Create(sizeof(HID_XFER_PACKET
) + ext
->preparseData
->caps
.InputReportByteLength
);
207 HID_StartDeviceThread(device
);
209 return STATUS_SUCCESS
;
212 NTSTATUS
PNP_RemoveDevice(minidriver
*minidriver
, DEVICE_OBJECT
*device
, IRP
*irp
)
215 NTSTATUS rc
= STATUS_NOT_SUPPORTED
;
218 rc
= minidriver
->PNPDispatch(device
, irp
);
219 HID_DeleteDevice(&minidriver
->minidriver
, device
);
220 LIST_FOR_EACH_ENTRY(hiddev
, &minidriver
->device_list
, hid_device
, entry
)
222 if (hiddev
->device
== device
)
224 list_remove(&hiddev
->entry
);
225 HeapFree(GetProcessHeap(), 0, hiddev
);
232 NTSTATUS WINAPI
HID_PNP_Dispatch(DEVICE_OBJECT
*device
, IRP
*irp
)
234 NTSTATUS rc
= STATUS_NOT_SUPPORTED
;
235 IO_STACK_LOCATION
*irpsp
= IoGetCurrentIrpStackLocation(irp
);
236 minidriver
*minidriver
= find_minidriver(device
->DriverObject
);
238 TRACE("%p, %p\n", device
, irp
);
240 switch(irpsp
->MinorFunction
)
242 case IRP_MN_QUERY_ID
:
244 BASE_DEVICE_EXTENSION
*ext
= device
->DeviceExtension
;
245 WCHAR
*id
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WCHAR
)*REGSTR_VAL_MAX_HCID_LEN
);
246 TRACE("IRP_MN_QUERY_ID[%i]\n", irpsp
->Parameters
.QueryId
.IdType
);
247 switch (irpsp
->Parameters
.QueryId
.IdType
)
249 case BusQueryHardwareIDs
:
250 case BusQueryCompatibleIDs
:
255 strcpyW(ptr
, ext
->instance_id
);
256 ptr
+= lstrlenW(ext
->instance_id
) + 1;
258 strcpyW(ptr
, ext
->device_id
);
259 ptr
+= lstrlenW(ext
->device_id
) + 1;
261 strcpyW(ptr
, device_enumeratorW
);
262 ptr
+= lstrlenW(device_enumeratorW
) + 1;
264 irp
->IoStatus
.Information
= (ULONG_PTR
)id
;
268 case BusQueryDeviceID
:
269 strcpyW(id
, ext
->device_id
);
270 irp
->IoStatus
.Information
= (ULONG_PTR
)id
;
273 case BusQueryInstanceID
:
274 strcpyW(id
, ext
->instance_id
);
275 irp
->IoStatus
.Information
= (ULONG_PTR
)id
;
278 case BusQueryDeviceSerialNumber
:
279 FIXME("BusQueryDeviceSerialNumber not implemented\n");
280 HeapFree(GetProcessHeap(), 0, id
);
285 case IRP_MN_REMOVE_DEVICE
:
287 return PNP_RemoveDevice(minidriver
, device
, irp
);
291 /* Forward IRP to the minidriver */
292 return minidriver
->PNPDispatch(device
, irp
);
296 irp
->IoStatus
.u
.Status
= rc
;
297 IoCompleteRequest( irp
, IO_NO_INCREMENT
);