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/list.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(hid);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:22:16 +02:00
|
|
|
static NTSTATUS get_device_id(DEVICE_OBJECT *device, BUS_QUERY_ID_TYPE type, WCHAR *id)
|
2016-08-12 15:17:55 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2019-06-05 16:22:16 +02:00
|
|
|
lstrcpyW(id, (WCHAR *)irp->IoStatus.Information);
|
|
|
|
ExFreePool( (WCHAR *)irp->IoStatus.Information );
|
2016-08-12 15:17:55 +02:00
|
|
|
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)
|
|
|
|
{
|
2019-06-05 16:22:16 +02:00
|
|
|
WCHAR device_id[MAX_DEVICE_ID_LEN], instance_id[MAX_DEVICE_ID_LEN];
|
2015-09-09 17:20:11 +02:00
|
|
|
DEVICE_OBJECT *device = NULL;
|
|
|
|
NTSTATUS status;
|
|
|
|
minidriver *minidriver;
|
2021-04-14 16:39:59 +02:00
|
|
|
HID_DEVICE_ATTRIBUTES attr = {0};
|
2015-09-09 17:20:11 +02:00
|
|
|
BASE_DEVICE_EXTENSION *ext = NULL;
|
2015-09-14 16:58:30 +02:00
|
|
|
HID_DESCRIPTOR descriptor;
|
|
|
|
BYTE *reportDescriptor;
|
|
|
|
INT i;
|
2015-09-09 17:20:11 +02:00
|
|
|
|
2019-06-05 16:22:16 +02:00
|
|
|
if ((status = get_device_id(PDO, BusQueryDeviceID, device_id)))
|
|
|
|
{
|
|
|
|
ERR("Failed to get PDO device id, status %#x.\n", status);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((status = get_device_id(PDO, BusQueryInstanceID, instance_id)))
|
2016-08-12 15:17:55 +02:00
|
|
|
{
|
2019-06-05 16:22:16 +02:00
|
|
|
ERR("Failed to get PDO instance id, status %#x.\n", status);
|
2016-08-12 15:17:55 +02:00
|
|
|
return status;
|
|
|
|
}
|
2015-09-09 17:20:11 +02:00
|
|
|
|
2019-06-05 16:22:16 +02:00
|
|
|
TRACE("Adding device to PDO %p, id %s\\%s.\n", PDO, debugstr_w(device_id), debugstr_w(instance_id));
|
2015-09-09 17:20:11 +02:00
|
|
|
minidriver = find_minidriver(driver);
|
|
|
|
|
2021-02-12 01:56:48 +01:00
|
|
|
status = HID_CreateDevice(PDO, &minidriver->minidriver, &device);
|
2015-09-09 17:20:11 +02:00
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
ERR("Failed to create HID object (%x)\n",status);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2015-12-18 07:40:16 +01:00
|
|
|
ext = device->DeviceExtension;
|
|
|
|
InitializeListHead(&ext->irp_queue);
|
2019-05-08 23:09:03 +02:00
|
|
|
KeInitializeSpinLock(&ext->irp_queue_lock);
|
2015-12-18 07:40:16 +01:00
|
|
|
|
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);
|
2019-06-06 17:12:50 +02:00
|
|
|
HID_DeleteDevice(device);
|
2015-09-09 17:20:11 +02:00
|
|
|
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);
|
2019-06-06 17:12:50 +02:00
|
|
|
HID_DeleteDevice(device);
|
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);
|
2019-06-06 17:12:50 +02:00
|
|
|
HID_DeleteDevice(device);
|
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");
|
2019-06-06 17:12:50 +02:00
|
|
|
HID_DeleteDevice(device);
|
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);
|
2019-06-06 17:12:50 +02:00
|
|
|
HID_DeleteDevice(device);
|
2015-09-14 16:58:30 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, reportDescriptor);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-03-24 14:57:59 +01:00
|
|
|
ext->preparseData = ParseDescriptor(reportDescriptor, descriptor.DescriptorList[i].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");
|
2019-06-06 17:12:50 +02:00
|
|
|
HID_DeleteDevice(device);
|
2016-03-03 19:53:06 +01:00
|
|
|
return STATUS_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
ext->information.DescriptorSize = ext->preparseData->dwSize;
|
2015-09-14 16:58:30 +02:00
|
|
|
|
2019-06-05 16:22:16 +02:00
|
|
|
lstrcpyW(ext->instance_id, instance_id);
|
2015-09-09 17:20:11 +02:00
|
|
|
|
2020-11-15 23:11:21 +01:00
|
|
|
lstrcpyW(ext->device_id, L"HID");
|
|
|
|
lstrcatW(ext->device_id, L"\\");
|
2019-06-05 16:22:16 +02:00
|
|
|
lstrcatW(ext->device_id, wcschr(device_id, '\\') + 1);
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-12 01:56:47 +01:00
|
|
|
static NTSTATUS remove_device(minidriver *minidriver, DEVICE_OBJECT *device, IRP *irp)
|
2018-08-06 16:21:12 +02:00
|
|
|
{
|
2018-11-14 03:48:18 +01:00
|
|
|
BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
|
2018-08-06 16:21:12 +02:00
|
|
|
NTSTATUS rc = STATUS_NOT_SUPPORTED;
|
|
|
|
|
2018-11-14 03:48:18 +01:00
|
|
|
rc = IoSetDeviceInterfaceState(&ext->link_name, FALSE);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
FIXME("failed to disable interface %x\n", rc);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-07-03 01:07:58 +02:00
|
|
|
if (ext->is_mouse)
|
|
|
|
IoSetDeviceInterfaceState(&ext->mouse_link_name, FALSE);
|
|
|
|
|
2018-08-06 16:21:12 +02:00
|
|
|
if (irp)
|
|
|
|
rc = minidriver->PNPDispatch(device, irp);
|
2019-06-06 17:12:50 +02:00
|
|
|
HID_DeleteDevice(device);
|
2018-08-06 16:21:12 +02:00
|
|
|
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;
|
2019-06-05 16:22:14 +02:00
|
|
|
WCHAR *id = ExAllocatePool(PagedPool, 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;
|
2019-06-05 16:22:16 +02:00
|
|
|
/* Device instance ID */
|
|
|
|
lstrcpyW(ptr, ext->device_id);
|
|
|
|
ptr += lstrlenW(ext->device_id);
|
2020-11-15 23:11:21 +01:00
|
|
|
lstrcpyW(ptr, L"\\");
|
2019-06-05 16:22:16 +02:00
|
|
|
ptr += 1;
|
2019-06-05 08:36:39 +02:00
|
|
|
lstrcpyW(ptr, ext->instance_id);
|
2016-04-05 15:45:34 +02:00
|
|
|
ptr += lstrlenW(ext->instance_id) + 1;
|
|
|
|
/* Device ID */
|
2019-06-05 08:36:39 +02:00
|
|
|
lstrcpyW(ptr, ext->device_id);
|
2016-04-05 15:45:34 +02:00
|
|
|
ptr += lstrlenW(ext->device_id) + 1;
|
|
|
|
/* Bus ID */
|
2020-11-15 23:11:21 +01:00
|
|
|
lstrcpyW(ptr, L"HID");
|
|
|
|
ptr += lstrlenW(L"HID") + 1;
|
2016-04-05 15:45:34 +02:00
|
|
|
*ptr = 0;
|
|
|
|
irp->IoStatus.Information = (ULONG_PTR)id;
|
|
|
|
rc = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BusQueryDeviceID:
|
2019-06-05 08:36:39 +02:00
|
|
|
lstrcpyW(id, ext->device_id);
|
2016-04-05 15:45:34 +02:00
|
|
|
irp->IoStatus.Information = (ULONG_PTR)id;
|
|
|
|
rc = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
case BusQueryInstanceID:
|
2019-06-05 08:36:39 +02:00
|
|
|
lstrcpyW(id, ext->instance_id);
|
2016-04-05 15:45:34 +02:00
|
|
|
irp->IoStatus.Information = (ULONG_PTR)id;
|
|
|
|
rc = STATUS_SUCCESS;
|
|
|
|
break;
|
2021-04-11 04:57:15 +02:00
|
|
|
|
|
|
|
case BusQueryContainerID:
|
2016-04-23 20:03:10 +02:00
|
|
|
case BusQueryDeviceSerialNumber:
|
2021-04-11 04:57:15 +02:00
|
|
|
FIXME("unimplemented id type %#x\n", irpsp->Parameters.QueryId.IdType);
|
2019-08-22 06:16:50 +02:00
|
|
|
ExFreePool(id);
|
2016-04-23 20:03:10 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
rc = minidriver->PNPDispatch(device, irp);
|
|
|
|
|
2018-11-02 14:45:42 +01:00
|
|
|
IoSetDeviceInterfaceState(&ext->link_name, TRUE);
|
2019-07-03 01:07:58 +02:00
|
|
|
if (ext->is_mouse)
|
|
|
|
IoSetDeviceInterfaceState(&ext->mouse_link_name, TRUE);
|
2018-08-17 01:37:42 +02:00
|
|
|
return rc;
|
|
|
|
}
|
2016-08-17 17:30:52 +02:00
|
|
|
case IRP_MN_REMOVE_DEVICE:
|
|
|
|
{
|
2021-02-12 01:56:47 +01:00
|
|
|
return remove_device(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;
|
|
|
|
}
|