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"
28 #include "wine/debug.h"
29 #include "wine/list.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(hid
);
33 static NTSTATUS WINAPI
internalComplete(DEVICE_OBJECT
*deviceObject
, IRP
*irp
,
36 HANDLE event
= context
;
38 return STATUS_MORE_PROCESSING_REQUIRED
;
41 static NTSTATUS
get_device_id(DEVICE_OBJECT
*device
, BUS_QUERY_ID_TYPE type
, WCHAR
*id
)
44 IO_STACK_LOCATION
*irpsp
;
45 IO_STATUS_BLOCK irp_status
;
49 irp
= IoBuildSynchronousFsdRequest(IRP_MJ_PNP
, device
, NULL
, 0, NULL
, NULL
, &irp_status
);
51 return STATUS_NO_MEMORY
;
53 event
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
);
54 irpsp
= IoGetNextIrpStackLocation(irp
);
55 irpsp
->MinorFunction
= IRP_MN_QUERY_ID
;
56 irpsp
->Parameters
.QueryId
.IdType
= type
;
58 IoSetCompletionRoutine(irp
, internalComplete
, event
, TRUE
, TRUE
, TRUE
);
59 status
= IoCallDriver(device
, irp
);
60 if (status
== STATUS_PENDING
)
61 WaitForSingleObject(event
, INFINITE
);
63 lstrcpyW(id
, (WCHAR
*)irp
->IoStatus
.Information
);
64 ExFreePool( (WCHAR
*)irp
->IoStatus
.Information
);
65 status
= irp
->IoStatus
.u
.Status
;
66 IoCompleteRequest(irp
, IO_NO_INCREMENT
);
72 NTSTATUS WINAPI
PNP_AddDevice(DRIVER_OBJECT
*driver
, DEVICE_OBJECT
*PDO
)
74 WCHAR device_id
[MAX_DEVICE_ID_LEN
], instance_id
[MAX_DEVICE_ID_LEN
];
76 DEVICE_OBJECT
*device
= NULL
;
78 minidriver
*minidriver
;
79 HID_DEVICE_ATTRIBUTES attr
;
80 BASE_DEVICE_EXTENSION
*ext
= NULL
;
81 HID_DESCRIPTOR descriptor
;
82 BYTE
*reportDescriptor
;
85 if ((status
= get_device_id(PDO
, BusQueryDeviceID
, device_id
)))
87 ERR("Failed to get PDO device id, status %#x.\n", status
);
91 if ((status
= get_device_id(PDO
, BusQueryInstanceID
, instance_id
)))
93 ERR("Failed to get PDO instance id, status %#x.\n", status
);
97 TRACE("Adding device to PDO %p, id %s\\%s.\n", PDO
, debugstr_w(device_id
), debugstr_w(instance_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, hiddev
);
111 device
= hiddev
->device
;
113 ext
= device
->DeviceExtension
;
114 InitializeListHead(&ext
->irp_queue
);
115 KeInitializeSpinLock(&ext
->irp_queue_lock
);
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 HID_DeleteDevice(device
);
123 HeapFree(GetProcessHeap(), 0, hiddev
);
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(device
);
134 HeapFree(GetProcessHeap(), 0, hiddev
);
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(device
);
149 HeapFree(GetProcessHeap(), 0, hiddev
);
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(device
);
160 HeapFree(GetProcessHeap(), 0, hiddev
);
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(device
);
171 HeapFree(GetProcessHeap(), 0, reportDescriptor
);
172 HeapFree(GetProcessHeap(), 0, hiddev
);
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(device
);
183 HeapFree(GetProcessHeap(), 0, hiddev
);
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
, instance_id
);
193 lstrcpyW(ext
->device_id
, L
"HID");
194 lstrcatW(ext
->device_id
, L
"\\");
195 lstrcatW(ext
->device_id
, wcschr(device_id
, '\\') + 1);
197 HID_LinkDevice(device
);
199 ext
->poll_interval
= DEFAULT_POLL_INTERVAL
;
201 ext
->ring_buffer
= RingBuffer_Create(sizeof(HID_XFER_PACKET
) + ext
->preparseData
->caps
.InputReportByteLength
);
203 HID_StartDeviceThread(device
);
205 return STATUS_SUCCESS
;
208 NTSTATUS
PNP_RemoveDevice(minidriver
*minidriver
, DEVICE_OBJECT
*device
, IRP
*irp
)
210 BASE_DEVICE_EXTENSION
*ext
= device
->DeviceExtension
;
212 NTSTATUS rc
= STATUS_NOT_SUPPORTED
;
214 rc
= IoSetDeviceInterfaceState(&ext
->link_name
, FALSE
);
217 FIXME("failed to disable interface %x\n", rc
);
222 IoSetDeviceInterfaceState(&ext
->mouse_link_name
, FALSE
);
225 rc
= minidriver
->PNPDispatch(device
, irp
);
226 HID_DeleteDevice(device
);
227 LIST_FOR_EACH_ENTRY(hiddev
, &minidriver
->device_list
, hid_device
, entry
)
229 if (hiddev
->device
== device
)
231 list_remove(&hiddev
->entry
);
232 HeapFree(GetProcessHeap(), 0, hiddev
);
239 NTSTATUS WINAPI
HID_PNP_Dispatch(DEVICE_OBJECT
*device
, IRP
*irp
)
241 NTSTATUS rc
= STATUS_NOT_SUPPORTED
;
242 IO_STACK_LOCATION
*irpsp
= IoGetCurrentIrpStackLocation(irp
);
243 minidriver
*minidriver
= find_minidriver(device
->DriverObject
);
245 TRACE("%p, %p\n", device
, irp
);
247 switch(irpsp
->MinorFunction
)
249 case IRP_MN_QUERY_ID
:
251 BASE_DEVICE_EXTENSION
*ext
= device
->DeviceExtension
;
252 WCHAR
*id
= ExAllocatePool(PagedPool
, sizeof(WCHAR
) * REGSTR_VAL_MAX_HCID_LEN
);
253 TRACE("IRP_MN_QUERY_ID[%i]\n", irpsp
->Parameters
.QueryId
.IdType
);
254 switch (irpsp
->Parameters
.QueryId
.IdType
)
256 case BusQueryHardwareIDs
:
257 case BusQueryCompatibleIDs
:
261 /* Device instance ID */
262 lstrcpyW(ptr
, ext
->device_id
);
263 ptr
+= lstrlenW(ext
->device_id
);
264 lstrcpyW(ptr
, L
"\\");
266 lstrcpyW(ptr
, ext
->instance_id
);
267 ptr
+= lstrlenW(ext
->instance_id
) + 1;
269 lstrcpyW(ptr
, ext
->device_id
);
270 ptr
+= lstrlenW(ext
->device_id
) + 1;
272 lstrcpyW(ptr
, L
"HID");
273 ptr
+= lstrlenW(L
"HID") + 1;
275 irp
->IoStatus
.Information
= (ULONG_PTR
)id
;
279 case BusQueryDeviceID
:
280 lstrcpyW(id
, ext
->device_id
);
281 irp
->IoStatus
.Information
= (ULONG_PTR
)id
;
284 case BusQueryInstanceID
:
285 lstrcpyW(id
, ext
->instance_id
);
286 irp
->IoStatus
.Information
= (ULONG_PTR
)id
;
289 case BusQueryDeviceSerialNumber
:
290 FIXME("BusQueryDeviceSerialNumber not implemented\n");
296 case IRP_MN_START_DEVICE
:
298 BASE_DEVICE_EXTENSION
*ext
= device
->DeviceExtension
;
300 rc
= minidriver
->PNPDispatch(device
, irp
);
302 IoSetDeviceInterfaceState(&ext
->link_name
, TRUE
);
304 IoSetDeviceInterfaceState(&ext
->mouse_link_name
, TRUE
);
307 case IRP_MN_REMOVE_DEVICE
:
309 return PNP_RemoveDevice(minidriver
, device
, irp
);
313 /* Forward IRP to the minidriver */
314 return minidriver
->PNPDispatch(device
, irp
);
318 irp
->IoStatus
.u
.Status
= rc
;
319 IoCompleteRequest( irp
, IO_NO_INCREMENT
);