hidclass.sys: Implment IRP_MJ_CREATE and IRP_MJ_CLOSE for HID Devices.
[wine.git] / dlls / hidclass.sys / main.c
blob1e0e802cca0e531a2030025a1d1644ec2929a68f
1 /*
2 * WINE Hid device services
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
22 #include <unistd.h>
23 #include <stdarg.h>
24 #include "hid.h"
25 #include "wine/debug.h"
26 #include "wine/unicode.h"
27 #include "wine/list.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(hid);
31 static struct list minidriver_list = LIST_INIT(minidriver_list);
33 minidriver* find_minidriver(DRIVER_OBJECT *driver)
35 minidriver *md;
36 LIST_FOR_EACH_ENTRY(md, &minidriver_list, minidriver, entry)
38 if (md->minidriver.DriverObject == driver)
39 return md;
41 return NULL;
44 static VOID WINAPI UnloadDriver(DRIVER_OBJECT *driver)
46 minidriver *md;
48 TRACE("Driver Unload\n");
49 md = find_minidriver(driver);
50 if (md)
52 if (md->DriverUnload)
53 md->DriverUnload(md->minidriver.DriverObject);
54 PNP_CleanupPNP(md->minidriver.DriverObject);
55 list_remove(&md->entry);
56 HeapFree( GetProcessHeap(), 0, md );
60 NTSTATUS WINAPI HidRegisterMinidriver(HID_MINIDRIVER_REGISTRATION *registration)
62 minidriver *driver;
63 driver = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*driver));
65 if (!driver)
66 return STATUS_NO_MEMORY;
68 driver->DriverUnload = registration->DriverObject->DriverUnload;
69 registration->DriverObject->DriverUnload = UnloadDriver;
71 registration->DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = HID_Device_ioctl;
72 registration->DriverObject->MajorFunction[IRP_MJ_CREATE] = HID_Device_create;
73 registration->DriverObject->MajorFunction[IRP_MJ_CLOSE] = HID_Device_close;
75 driver->AddDevice = registration->DriverObject->DriverExtension->AddDevice;
76 registration->DriverObject->DriverExtension->AddDevice = PNP_AddDevice;
78 driver->minidriver = *registration;
79 list_add_tail(&minidriver_list, &driver->entry);
81 return STATUS_SUCCESS;
84 static NTSTATUS WINAPI internalComplete(DEVICE_OBJECT *deviceObject, IRP *irp,
85 void *context )
87 SetEvent(irp->UserEvent);
88 return STATUS_MORE_PROCESSING_REQUIRED;
91 NTSTATUS call_minidriver(ULONG code, DEVICE_OBJECT *device, void *in_buff, ULONG in_size, void *out_buff, ULONG out_size)
93 IRP *irp;
94 IO_STATUS_BLOCK irp_status;
95 IO_STACK_LOCATION *irpsp;
96 NTSTATUS status;
97 void *buffer = NULL;
99 HANDLE event = CreateEventA(NULL, FALSE, FALSE, NULL);
101 if (out_size)
103 buffer = HeapAlloc(GetProcessHeap(), 0, out_size);
104 memcpy(buffer, out_buff, out_size);
107 irp = IoBuildDeviceIoControlRequest(code, device, in_buff, in_size,
108 buffer, out_size, TRUE, event, &irp_status);
110 irpsp = IoGetNextIrpStackLocation(irp);
111 irpsp->CompletionRoutine = internalComplete;
112 irpsp->Control = SL_INVOKE_ON_SUCCESS | SL_INVOKE_ON_ERROR;
114 IoCallDriver(device, irp);
116 if (irp->IoStatus.u.Status == STATUS_PENDING)
117 WaitForSingleObject(event, INFINITE);
119 memcpy(out_buff, buffer, out_size);
120 status = irp->IoStatus.u.Status;
122 IoCompleteRequest(irp, IO_NO_INCREMENT );
123 CloseHandle(event);
125 return status;