2015-09-09 17:20:11 +02:00
|
|
|
/*
|
|
|
|
* WINE HID Pseudo-Plug and Play support
|
|
|
|
*
|
|
|
|
* Copyright 2015 Aric Stewart
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define NONAMELESSUNION
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include "hid.h"
|
|
|
|
#include "ddk/hidtypes.h"
|
2018-08-17 01:37:42 +02:00
|
|
|
#include "ddk/wdm.h"
|
2016-04-05 15:45:34 +02:00
|
|
|
#include "regstr.h"
|
2015-09-09 17:20:11 +02:00
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "wine/unicode.h"
|
|
|
|
#include "wine/list.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(hid);
|
|
|
|
|
2016-04-05 15:45:34 +02:00
|
|
|
static const WCHAR device_enumeratorW[] = {'H','I','D',0};
|
2016-08-12 15:17:55 +02:00
|
|
|
static const WCHAR separator_W[] = {'\\',0};
|
2016-04-05 15:45:34 +02:00
|
|
|
static const WCHAR device_deviceid_fmtW[] = {'%','s','\\',
|
2017-11-21 21:00:00 +01:00
|
|
|
'v','i','d','_','%','0','4','x','&','p','i','d','_','%', '0','4','x',0};
|
2016-04-05 15:45:34 +02:00
|
|
|
|
2015-10-08 21:35:02 +02:00
|
|
|
static NTSTATUS WINAPI internalComplete(DEVICE_OBJECT *deviceObject, IRP *irp,
|
2016-10-18 17:07:59 +02:00
|
|
|
void *context)
|
2015-10-08 21:35:02 +02:00
|
|
|
{
|
2016-10-18 17:07:59 +02:00
|
|
|
HANDLE event = context;
|
|
|
|
SetEvent(event);
|
2015-10-08 21:35:02 +02:00
|
|
|
return STATUS_MORE_PROCESSING_REQUIRED;
|
|
|
|
}
|
|
|
|
|
2016-08-12 15:17:55 +02:00
|
|
|
static NTSTATUS get_device_id(DEVICE_OBJECT *device, BUS_QUERY_ID_TYPE type, WCHAR **id)
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
IO_STACK_LOCATION *irpsp;
|
|
|
|
IO_STATUS_BLOCK irp_status;
|
2016-08-22 23:05:36 +02:00
|
|
|
HANDLE event;
|
2016-08-12 15:17:55 +02:00
|
|
|
IRP *irp;
|
|
|
|
|
|
|
|
irp = IoBuildSynchronousFsdRequest(IRP_MJ_PNP, device, NULL, 0, NULL, NULL, &irp_status);
|
|
|
|
if (irp == NULL)
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
|
2016-10-18 17:07:59 +02:00
|
|
|
event = CreateEventA(NULL, FALSE, FALSE, NULL);
|
2016-08-12 15:17:55 +02:00
|
|
|
irpsp = IoGetNextIrpStackLocation(irp);
|
|
|
|
irpsp->MinorFunction = IRP_MN_QUERY_ID;
|
|
|
|
irpsp->Parameters.QueryId.IdType = type;
|
|
|
|
|
2016-10-18 17:07:59 +02:00
|
|
|
IoSetCompletionRoutine(irp, internalComplete, event, TRUE, TRUE, TRUE);
|
2016-10-21 05:58:47 +02:00
|
|
|
status = IoCallDriver(device, irp);
|
|
|
|
if (status == STATUS_PENDING)
|
2016-08-12 15:17:55 +02:00
|
|
|
WaitForSingleObject(event, INFINITE);
|
|
|
|
|
|
|
|
*id = (WCHAR*)irp->IoStatus.Information;
|
|
|
|
status = irp->IoStatus.u.Status;
|
|
|
|
IoCompleteRequest(irp, IO_NO_INCREMENT );
|
|
|
|
CloseHandle(event);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2015-09-09 17:20:11 +02:00
|
|
|
NTSTATUS WINAPI PNP_AddDevice(DRIVER_OBJECT *driver, DEVICE_OBJECT *PDO)
|
|
|
|
{
|
2018-08-06 16:21:12 +02:00
|
|
|
hid_device *hiddev;
|
2015-09-09 17:20:11 +02:00
|
|
|
DEVICE_OBJECT *device = NULL;
|
|
|
|
NTSTATUS status;
|
|
|
|
minidriver *minidriver;
|
|
|
|
HID_DEVICE_ATTRIBUTES attr;
|
|
|
|
BASE_DEVICE_EXTENSION *ext = NULL;
|
2015-09-14 16:58:30 +02:00
|
|
|
HID_DESCRIPTOR descriptor;
|
|
|
|
BYTE *reportDescriptor;
|
|
|
|
INT i;
|
2016-08-12 15:17:55 +02:00
|
|
|
WCHAR *PDO_id;
|
|
|
|
WCHAR *id_ptr;
|
2015-09-09 17:20:11 +02:00
|
|
|
|
2016-08-12 15:17:55 +02:00
|
|
|
status = get_device_id(PDO, BusQueryInstanceID, &PDO_id);
|
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
ERR("Failed to get PDO id(%x)\n",status);
|
|
|
|
return status;
|
|
|
|
}
|
2015-09-09 17:20:11 +02:00
|
|
|
|
2016-08-12 15:17:55 +02:00
|
|
|
TRACE("PDO add device(%p:%s)\n", PDO, debugstr_w(PDO_id));
|
2015-09-09 17:20:11 +02:00
|
|
|
minidriver = find_minidriver(driver);
|
|
|
|
|
2018-08-06 16:21:12 +02:00
|
|
|
hiddev = HeapAlloc(GetProcessHeap(), 0, sizeof(*hiddev));
|
|
|
|
if (!hiddev)
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
|
|
|
|
status = HID_CreateDevice(PDO, &minidriver->minidriver, &hiddev->device);
|
2015-09-09 17:20:11 +02:00
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
ERR("Failed to create HID object (%x)\n",status);
|
2016-08-12 15:17:55 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, PDO_id);
|
2018-08-06 16:21:12 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, hiddev);
|
2015-09-09 17:20:11 +02:00
|
|
|
return status;
|
|
|
|
}
|
2018-08-06 16:21:12 +02:00
|
|
|
device = hiddev->device;
|
2015-09-09 17:20:11 +02:00
|
|
|
|
2015-12-18 07:40:16 +01:00
|
|
|
ext = device->DeviceExtension;
|
|
|
|
InitializeListHead(&ext->irp_queue);
|
|
|
|
|
2015-09-09 17:20:11 +02:00
|
|
|
TRACE("Created device %p\n",device);
|
|
|
|
status = minidriver->AddDevice(minidriver->minidriver.DriverObject, device);
|
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
ERR("Minidriver AddDevice failed (%x)\n",status);
|
2016-08-12 15:17:55 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, PDO_id);
|
2015-09-09 17:20:11 +02:00
|
|
|
HID_DeleteDevice(&minidriver->minidriver, device);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = call_minidriver(IOCTL_HID_GET_DEVICE_ATTRIBUTES, device,
|
|
|
|
NULL, 0, &attr, sizeof(attr));
|
|
|
|
|
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
ERR("Minidriver failed to get Attributes(%x)\n",status);
|
|
|
|
HID_DeleteDevice(&minidriver->minidriver, device);
|
2016-08-12 15:17:55 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, PDO_id);
|
2015-09-09 17:20:11 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
ext->information.VendorID = attr.VendorID;
|
|
|
|
ext->information.ProductID = attr.ProductID;
|
|
|
|
ext->information.VersionNumber = attr.VersionNumber;
|
|
|
|
ext->information.Polled = minidriver->minidriver.DevicesArePolled;
|
|
|
|
|
2015-09-14 16:58:30 +02:00
|
|
|
status = call_minidriver(IOCTL_HID_GET_DEVICE_DESCRIPTOR, device, NULL, 0,
|
|
|
|
&descriptor, sizeof(descriptor));
|
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
ERR("Cannot get Device Descriptor(%x)\n",status);
|
|
|
|
HID_DeleteDevice(&minidriver->minidriver, device);
|
2016-08-12 15:17:55 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, PDO_id);
|
2015-09-14 16:58:30 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
for (i = 0; i < descriptor.bNumDescriptors; i++)
|
|
|
|
if (descriptor.DescriptorList[i].bReportType == HID_REPORT_DESCRIPTOR_TYPE)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i >= descriptor.bNumDescriptors)
|
|
|
|
{
|
|
|
|
ERR("No Report Descriptor found in reply\n");
|
|
|
|
HID_DeleteDevice(&minidriver->minidriver, device);
|
2016-08-12 15:17:55 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, PDO_id);
|
2015-09-14 16:58:30 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
reportDescriptor = HeapAlloc(GetProcessHeap(), 0, descriptor.DescriptorList[i].wReportLength);
|
|
|
|
status = call_minidriver(IOCTL_HID_GET_REPORT_DESCRIPTOR, device, NULL, 0,
|
|
|
|
reportDescriptor, descriptor.DescriptorList[i].wReportLength);
|
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
ERR("Cannot get Report Descriptor(%x)\n",status);
|
|
|
|
HID_DeleteDevice(&minidriver->minidriver, device);
|
|
|
|
HeapFree(GetProcessHeap(), 0, reportDescriptor);
|
2016-08-12 15:17:55 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, PDO_id);
|
2015-09-14 16:58:30 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
ext->preparseData = ParseDescriptor(reportDescriptor, descriptor.DescriptorList[0].wReportLength);
|
2016-03-03 19:53:06 +01:00
|
|
|
|
2015-09-14 16:58:30 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, reportDescriptor);
|
2016-03-03 19:53:06 +01:00
|
|
|
if (!ext->preparseData)
|
|
|
|
{
|
|
|
|
ERR("Cannot parse Report Descriptor\n");
|
|
|
|
HID_DeleteDevice(&minidriver->minidriver, device);
|
2016-08-12 15:17:55 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, PDO_id);
|
2016-03-03 19:53:06 +01:00
|
|
|
return STATUS_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
2018-08-06 16:21:12 +02:00
|
|
|
list_add_tail(&(minidriver->device_list), &hiddev->entry);
|
|
|
|
|
2016-03-03 19:53:06 +01:00
|
|
|
ext->information.DescriptorSize = ext->preparseData->dwSize;
|
2015-09-14 16:58:30 +02:00
|
|
|
|
2016-08-12 15:17:55 +02:00
|
|
|
lstrcpyW(ext->instance_id, device_enumeratorW);
|
|
|
|
strcatW(ext->instance_id, separator_W);
|
|
|
|
/* Skip the original enumerator */
|
|
|
|
id_ptr = strchrW(PDO_id, '\\');
|
|
|
|
id_ptr++;
|
|
|
|
strcatW(ext->instance_id, id_ptr);
|
|
|
|
HeapFree(GetProcessHeap(), 0, PDO_id);
|
2015-09-09 17:20:11 +02:00
|
|
|
|
2016-04-05 15:45:34 +02:00
|
|
|
sprintfW(ext->device_id, device_deviceid_fmtW, device_enumeratorW, ext->information.VendorID, ext->information.ProductID);
|
2015-09-09 17:20:11 +02:00
|
|
|
|
2016-04-05 15:45:48 +02:00
|
|
|
HID_LinkDevice(device);
|
2015-09-09 17:20:11 +02:00
|
|
|
|
|
|
|
ext->poll_interval = DEFAULT_POLL_INTERVAL;
|
|
|
|
|
2015-09-11 17:45:54 +02:00
|
|
|
ext->ring_buffer = RingBuffer_Create(sizeof(HID_XFER_PACKET) + ext->preparseData->caps.InputReportByteLength);
|
|
|
|
|
2015-10-08 21:34:35 +02:00
|
|
|
HID_StartDeviceThread(device);
|
|
|
|
|
2015-09-09 17:20:11 +02:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-08-06 16:21:12 +02:00
|
|
|
NTSTATUS PNP_RemoveDevice(minidriver *minidriver, DEVICE_OBJECT *device, IRP *irp)
|
|
|
|
{
|
|
|
|
hid_device *hiddev;
|
|
|
|
NTSTATUS rc = STATUS_NOT_SUPPORTED;
|
|
|
|
|
|
|
|
if (irp)
|
|
|
|
rc = minidriver->PNPDispatch(device, irp);
|
|
|
|
HID_DeleteDevice(&minidriver->minidriver, device);
|
|
|
|
LIST_FOR_EACH_ENTRY(hiddev, &minidriver->device_list, hid_device, entry)
|
|
|
|
{
|
|
|
|
if (hiddev->device == device)
|
|
|
|
{
|
|
|
|
list_remove(&hiddev->entry);
|
|
|
|
HeapFree(GetProcessHeap(), 0, hiddev);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2016-04-05 15:45:34 +02:00
|
|
|
NTSTATUS WINAPI HID_PNP_Dispatch(DEVICE_OBJECT *device, IRP *irp)
|
|
|
|
{
|
|
|
|
NTSTATUS rc = STATUS_NOT_SUPPORTED;
|
|
|
|
IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation(irp);
|
2016-08-17 17:30:52 +02:00
|
|
|
minidriver *minidriver = find_minidriver(device->DriverObject);
|
2016-04-05 15:45:34 +02:00
|
|
|
|
|
|
|
TRACE("%p, %p\n", device, irp);
|
|
|
|
|
|
|
|
switch(irpsp->MinorFunction)
|
|
|
|
{
|
|
|
|
case IRP_MN_QUERY_ID:
|
|
|
|
{
|
|
|
|
BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
|
|
|
|
WCHAR *id = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR)*REGSTR_VAL_MAX_HCID_LEN);
|
2016-04-23 20:03:10 +02:00
|
|
|
TRACE("IRP_MN_QUERY_ID[%i]\n", irpsp->Parameters.QueryId.IdType);
|
|
|
|
switch (irpsp->Parameters.QueryId.IdType)
|
2016-04-05 15:45:34 +02:00
|
|
|
{
|
|
|
|
case BusQueryHardwareIDs:
|
|
|
|
case BusQueryCompatibleIDs:
|
|
|
|
{
|
|
|
|
WCHAR *ptr;
|
|
|
|
ptr = id;
|
|
|
|
/* Instance ID */
|
|
|
|
strcpyW(ptr, ext->instance_id);
|
|
|
|
ptr += lstrlenW(ext->instance_id) + 1;
|
|
|
|
/* Device ID */
|
|
|
|
strcpyW(ptr, ext->device_id);
|
|
|
|
ptr += lstrlenW(ext->device_id) + 1;
|
|
|
|
/* Bus ID */
|
|
|
|
strcpyW(ptr, device_enumeratorW);
|
|
|
|
ptr += lstrlenW(device_enumeratorW) + 1;
|
|
|
|
*ptr = 0;
|
|
|
|
irp->IoStatus.Information = (ULONG_PTR)id;
|
|
|
|
rc = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BusQueryDeviceID:
|
|
|
|
strcpyW(id, ext->device_id);
|
|
|
|
irp->IoStatus.Information = (ULONG_PTR)id;
|
|
|
|
rc = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
case BusQueryInstanceID:
|
|
|
|
strcpyW(id, ext->instance_id);
|
|
|
|
irp->IoStatus.Information = (ULONG_PTR)id;
|
|
|
|
rc = STATUS_SUCCESS;
|
|
|
|
break;
|
2016-04-23 20:03:10 +02:00
|
|
|
case BusQueryDeviceSerialNumber:
|
|
|
|
FIXME("BusQueryDeviceSerialNumber not implemented\n");
|
|
|
|
HeapFree(GetProcessHeap(), 0, id);
|
|
|
|
break;
|
2016-04-05 15:45:34 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-08-17 01:37:42 +02:00
|
|
|
case IRP_MN_START_DEVICE:
|
|
|
|
{
|
|
|
|
BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
|
|
|
|
UNICODE_STRING linkU;
|
|
|
|
|
|
|
|
rc = minidriver->PNPDispatch(device, irp);
|
|
|
|
|
|
|
|
RtlInitUnicodeString(&linkU, ext->link_name);
|
|
|
|
IoSetDeviceInterfaceState(&linkU, TRUE);
|
|
|
|
return rc;
|
|
|
|
}
|
2016-08-17 17:30:52 +02:00
|
|
|
case IRP_MN_REMOVE_DEVICE:
|
|
|
|
{
|
2018-08-06 16:21:12 +02:00
|
|
|
return PNP_RemoveDevice(minidriver, device, irp);
|
2016-08-17 17:30:52 +02:00
|
|
|
}
|
2016-04-05 15:45:34 +02:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
/* Forward IRP to the minidriver */
|
|
|
|
return minidriver->PNPDispatch(device, irp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
irp->IoStatus.u.Status = rc;
|
|
|
|
IoCompleteRequest( irp, IO_NO_INCREMENT );
|
|
|
|
return rc;
|
|
|
|
}
|