-
Notifications
You must be signed in to change notification settings - Fork 2
/
vfeature.c
250 lines (193 loc) · 7.09 KB
/
vfeature.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.
Module Name:
vfeature.c
Abstract:
This module sets the state of a vendor specific HID feature.
Environment:
Kernel mode
--*/
#include "firefly.h"
#pragma warning(disable:4201) // nameless struct/union
#pragma warning(disable:4214) // bit field types other than int
#include <hidpddi.h>
#include <hidclass.h>
#pragma warning(default:4201)
#pragma warning(default:4214)
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, FireflySetFeature)
#endif
NTSTATUS
FireflySetFeature(
IN PDEVICE_CONTEXT DeviceContext,
IN UCHAR PageId,
IN USHORT FeatureId,
IN BOOLEAN EnableFeature
)
/*++
Routine Description:
This routine sets the HID feature by sending HID ioctls to our device.
These IOCTLs will be handled by HIDUSB and converted into USB requests
and send to the device.
Arguments:
DeviceContext - Context for our device
PageID - UsagePage of the light control feature.
FeatureId - Usage ID of the feature.
EnanbleFeature - True to turn the light on, Falst to turn if off.
Return Value:
NT Status code
--*/
{
WDF_MEMORY_DESCRIPTOR inputDescriptor, outputDescriptor;
NTSTATUS status;
HID_COLLECTION_INFORMATION collectionInformation = {0};
PHIDP_PREPARSED_DATA preparsedData;
HIDP_CAPS caps;
USAGE usage;
ULONG usageLength;
PCHAR report;
WDFIOTARGET hidTarget;
WDF_IO_TARGET_OPEN_PARAMS openParams;
PAGED_CODE();
//
// Preinit for error.
//
preparsedData = NULL;
report = NULL;
hidTarget = NULL;
status = WdfIoTargetCreate(WdfObjectContextGetObject(DeviceContext),
WDF_NO_OBJECT_ATTRIBUTES,
&hidTarget);
if (!NT_SUCCESS(status)) {
KdPrint(("FireFly: WdfIoTargetCreate failed 0x%x\n", status));
return status;
}
//
// Open it up, write access only!
//
WDF_IO_TARGET_OPEN_PARAMS_INIT_OPEN_BY_NAME(
&openParams,
&DeviceContext->PdoName,
FILE_WRITE_ACCESS);
//
// We will let the framework to respond automatically to the pnp
// state changes of the target by closing and opening the handle.
//
openParams.ShareAccess = FILE_SHARE_WRITE | FILE_SHARE_READ;
status = WdfIoTargetOpen(hidTarget, &openParams);
if (!NT_SUCCESS(status)) {
KdPrint(("FireFly: WdfIoTargetOpen failed 0x%x\n", status));
goto ExitAndFree;
}
WDF_MEMORY_DESCRIPTOR_INIT_BUFFER(&outputDescriptor,
(PVOID) &collectionInformation,
sizeof(HID_COLLECTION_INFORMATION));
//
// Now get the collection information for this device
//
status = WdfIoTargetSendIoctlSynchronously(hidTarget,
NULL,
IOCTL_HID_GET_COLLECTION_INFORMATION,
NULL,
&outputDescriptor,
NULL,
NULL);
if (!NT_SUCCESS(status)) {
KdPrint(("FireFly: WdfIoTargetSendIoctlSynchronously failed 0x%x\n", status));
goto ExitAndFree;
}
preparsedData = (PHIDP_PREPARSED_DATA) ExAllocatePoolWithTag(
NonPagedPoolNx, collectionInformation.DescriptorSize, 'ffly');
if (preparsedData == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto ExitAndFree;
}
WDF_MEMORY_DESCRIPTOR_INIT_BUFFER(&outputDescriptor,
(PVOID) preparsedData,
collectionInformation.DescriptorSize);
status = WdfIoTargetSendIoctlSynchronously(hidTarget,
NULL,
IOCTL_HID_GET_COLLECTION_DESCRIPTOR,
NULL,
&outputDescriptor,
NULL,
NULL);
if (!NT_SUCCESS(status)) {
KdPrint(("FireFly: WdfIoTargetSendIoctlSynchronously failed 0x%x\n", status));
goto ExitAndFree;
}
//
// Now get the capabilities.
//
RtlZeroMemory(&caps, sizeof(HIDP_CAPS));
status = HidP_GetCaps(preparsedData, &caps);
if (!NT_SUCCESS(status)) {
goto ExitAndFree;
}
//
// Create a report to send to the device.
//
report = (PCHAR) ExAllocatePoolWithTag(
NonPagedPoolNx, caps.FeatureReportByteLength, 'ffly');
if (report == NULL) {
goto ExitAndFree;
}
//
// Start with a zeroed report. If we are disabling the feature, this might
// be all we need to do.
//
RtlZeroMemory(report, caps.FeatureReportByteLength);
status = STATUS_SUCCESS;
if (EnableFeature) {
//
// Edit the report to reflect the enabled feature
//
usage = FeatureId;
usageLength = 1;
status = HidP_SetUsages(
HidP_Feature,
PageId,
0,
&usage, // pointer to the usage list
&usageLength, // number of usages in the usage list
preparsedData,
report,
caps.FeatureReportByteLength
);
if (!NT_SUCCESS(status)) {
KdPrint(("FireFly: HidP_SetUsages failed 0x%x\n", status));
goto ExitAndFree;
}
}
WDF_MEMORY_DESCRIPTOR_INIT_BUFFER(&inputDescriptor,
report,
caps.FeatureReportByteLength);
status = WdfIoTargetSendIoctlSynchronously(hidTarget,
NULL,
IOCTL_HID_SET_FEATURE,
&inputDescriptor,
NULL,
NULL,
NULL);
if (!NT_SUCCESS(status)) {
KdPrint(("FireFly: WdfIoTargetSendIoctlSynchronously failed 0x%x\n", status));
goto ExitAndFree;
}
ExitAndFree:
if (preparsedData != NULL) {
ExFreePool(preparsedData);
preparsedData = NULL;
}
if (report != NULL) {
ExFreePool(report);
report = NULL;
}
if (hidTarget != NULL) {
WdfObjectDelete(hidTarget);
}
return status;
}