ReactOS 0.4.16-dev-550-g2186ce3
battc.c File Reference
#include <battc.h>
#include <debug.h>
Include dependency graph for battc.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

NTSTATUS NTAPI DriverEntry (PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
 
BCLASSAPI NTSTATUS NTAPI BatteryClassUnload (PVOID ClassData)
 
BCLASSAPI NTSTATUS NTAPI BatteryClassSystemControl (PVOID ClassData, PVOID WmiLibContext, PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Disposition)
 
BCLASSAPI NTSTATUS NTAPI BatteryClassQueryWmiDataBlock (PVOID ClassData, PDEVICE_OBJECT DeviceObject, PIRP Irp, ULONG GuidIndex, PULONG InstanceLengthArray, ULONG OutBufferSize, PUCHAR Buffer)
 
BCLASSAPI NTSTATUS NTAPI BatteryClassStatusNotify (_In_ PVOID ClassData)
 
BCLASSAPI NTSTATUS NTAPI BatteryClassInitializeDevice (PBATTERY_MINIPORT_INFO MiniportInfo, PVOID *ClassData)
 
BCLASSAPI NTSTATUS NTAPI BatteryClassIoctl (PVOID ClassData, PIRP Irp)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 11 of file battc.c.

Function Documentation

◆ BatteryClassInitializeDevice()

BCLASSAPI NTSTATUS NTAPI BatteryClassInitializeDevice ( PBATTERY_MINIPORT_INFO  MiniportInfo,
PVOID ClassData 
)

Definition at line 146 of file battc.c.

148{
150 PBATTERY_CLASS_DATA BattClass;
151
153 sizeof(BATTERY_CLASS_DATA),
155 if (BattClass == NULL)
157
158 RtlZeroMemory(BattClass, sizeof(BATTERY_CLASS_DATA));
159
160 RtlCopyMemory(&BattClass->MiniportInfo,
161 MiniportInfo,
162 sizeof(BattClass->MiniportInfo));
163
165
166 ExInitializeFastMutex(&BattClass->Mutex);
167
168 if (MiniportInfo->Pdo != NULL)
169 {
170 Status = IoRegisterDeviceInterface(MiniportInfo->Pdo,
171 &GUID_DEVICE_BATTERY,
172 NULL,
173 &BattClass->InterfaceName);
174 if (NT_SUCCESS(Status))
175 {
176 DPRINT("Initialized battery interface: %wZ\n", &BattClass->InterfaceName);
179 {
180 DPRINT1("Got STATUS_OBJECT_NAME_EXISTS for SetDeviceInterfaceState\n");
182 }
183 }
184 else
185 {
186 DPRINT1("IoRegisterDeviceInterface failed (0x%x)\n", Status);
187 }
188 }
189
190 *ClassData = BattClass;
191
192 return STATUS_SUCCESS;
193}
LONG NTSTATUS
Definition: precomp.h:26
#define DPRINT1
Definition: precomp.h:8
_Out_ PVOID * ClassData
Definition: batclass.h:336
#define BATTERY_CLASS_DATA_TAG
Definition: battc.h:28
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define NonPagedPool
Definition: env_spec_w32.h:307
Status
Definition: gdiplustypes.h:25
@ SynchronizationEvent
NTSTATUS NTAPI IoRegisterDeviceInterface(IN PDEVICE_OBJECT PhysicalDeviceObject, IN CONST GUID *InterfaceClassGuid, IN PUNICODE_STRING ReferenceString OPTIONAL, OUT PUNICODE_STRING SymbolicLinkName)
Definition: deviface.c:955
NTSTATUS NTAPI IoSetDeviceInterfaceState(IN PUNICODE_STRING SymbolicLinkName, IN BOOLEAN Enable)
Definition: deviface.c:1311
#define STATUS_OBJECT_NAME_EXISTS
Definition: ntstatus.h:114
#define STATUS_SUCCESS
Definition: shellext.h:65
#define DPRINT
Definition: sndvol32.h:73
UNICODE_STRING InterfaceName
Definition: battc.h:24
KEVENT WaitEvent
Definition: battc.h:19
FAST_MUTEX Mutex
Definition: battc.h:21
BATTERY_MINIPORT_INFO MiniportInfo
Definition: battc.h:18
PDEVICE_OBJECT Pdo
Definition: batclass.h:257
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
FORCEINLINE VOID ExInitializeFastMutex(_Out_ PFAST_MUTEX FastMutex)
Definition: exfuncs.h:274

Referenced by CmBattAddBattery(), and CompBattAddDevice().

◆ BatteryClassIoctl()

BCLASSAPI NTSTATUS NTAPI BatteryClassIoctl ( PVOID  ClassData,
PIRP  Irp 
)

Definition at line 198 of file battc.c.

200{
201 PBATTERY_CLASS_DATA BattClass;
204 ULONG WaitTime;
205 BATTERY_WAIT_STATUS BattWait;
206 PBATTERY_QUERY_INFORMATION BattQueryInfo;
207 PBATTERY_SET_INFORMATION BattSetInfo;
209 PBATTERY_STATUS BattStatus;
210 BATTERY_NOTIFY BattNotify;
212
213 DPRINT("BatteryClassIoctl(%p %p)\n", ClassData, Irp);
214
215 BattClass = ClassData;
216
218 Irp->IoStatus.Information = 0;
219
220 DPRINT("Received IOCTL %x for %p\n", IrpSp->Parameters.DeviceIoControl.IoControlCode,
221 ClassData);
222
223 switch (IrpSp->Parameters.DeviceIoControl.IoControlCode)
224 {
226 if ((IrpSp->Parameters.DeviceIoControl.InputBufferLength != sizeof(ULONG) && IrpSp->Parameters.DeviceIoControl.InputBufferLength != 0) ||
227 IrpSp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(ULONG))
228 {
230 break;
231 }
232
233 WaitTime = IrpSp->Parameters.DeviceIoControl.InputBufferLength == sizeof(ULONG) ? *(PULONG)Irp->AssociatedIrp.SystemBuffer : 0;
234
235 Timeout.QuadPart = Int32x32To64(WaitTime, -10000);
236
237 Status = BattClass->MiniportInfo.QueryTag(BattClass->MiniportInfo.Context,
238 (PULONG)Irp->AssociatedIrp.SystemBuffer);
239 if (!NT_SUCCESS(Status))
240 {
241 ExAcquireFastMutex(&BattClass->Mutex);
242 BattClass->EventTrigger = EVENT_BATTERY_TAG;
243 BattClass->Waiting = TRUE;
244 ExReleaseFastMutex(&BattClass->Mutex);
245
247 Executive,
249 FALSE,
250 WaitTime != -1 ? &Timeout : NULL);
251
252 ExAcquireFastMutex(&BattClass->Mutex);
253 BattClass->Waiting = FALSE;
254 ExReleaseFastMutex(&BattClass->Mutex);
255
256 if (Status == STATUS_SUCCESS)
257 {
258 Status = BattClass->MiniportInfo.QueryTag(BattClass->MiniportInfo.Context,
259 (PULONG)Irp->AssociatedIrp.SystemBuffer);
260 if (NT_SUCCESS(Status))
261 Irp->IoStatus.Information = sizeof(ULONG);
262 }
263 else
264 {
266 }
267 }
268 else
269 {
270 Irp->IoStatus.Information = sizeof(ULONG);
271 }
272 break;
273
275 if (IrpSp->Parameters.DeviceIoControl.InputBufferLength < sizeof(BattWait) ||
276 IrpSp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(BATTERY_STATUS))
277 {
279 break;
280 }
281
282 BattWait = *(PBATTERY_WAIT_STATUS)Irp->AssociatedIrp.SystemBuffer;
283
284 Timeout.QuadPart = Int32x32To64(BattWait.Timeout, -10000);
285
286 BattStatus = Irp->AssociatedIrp.SystemBuffer;
287 Status = BattClass->MiniportInfo.QueryStatus(BattClass->MiniportInfo.Context,
288 BattWait.BatteryTag,
289 BattStatus);
290
291 if (!NT_SUCCESS(Status) ||
292 (BattWait.PowerState == BattStatus->PowerState &&
293 BattWait.HighCapacity >= BattStatus->Capacity &&
294 BattWait.LowCapacity <= BattStatus->Capacity))
295 {
296 BattNotify.PowerState = BattWait.PowerState;
297 BattNotify.HighCapacity = BattWait.HighCapacity;
298 BattNotify.LowCapacity = BattWait.LowCapacity;
299
300 BattClass->MiniportInfo.SetStatusNotify(BattClass->MiniportInfo.Context,
301 BattWait.BatteryTag,
302 &BattNotify);
303
304 ExAcquireFastMutex(&BattClass->Mutex);
306 BattClass->EventTriggerContext = &BattWait;
307 BattClass->Waiting = TRUE;
308 ExReleaseFastMutex(&BattClass->Mutex);
309
311 Executive,
313 FALSE,
314 BattWait.Timeout != -1 ? &Timeout : NULL);
315 if (Status == STATUS_TIMEOUT)
317
318 ExAcquireFastMutex(&BattClass->Mutex);
319 BattClass->Waiting = FALSE;
320 ExReleaseFastMutex(&BattClass->Mutex);
321
323 }
324 else
325 {
326 Irp->IoStatus.Information = sizeof(BATTERY_STATUS);
327 }
328 break;
329
331 if (IrpSp->Parameters.DeviceIoControl.InputBufferLength < sizeof(*BattQueryInfo))
332 {
334 break;
335 }
336
337 BattQueryInfo = Irp->AssociatedIrp.SystemBuffer;
338
340 BattQueryInfo->BatteryTag,
341 BattQueryInfo->InformationLevel,
342 BattQueryInfo->AtRate,
343 Irp->AssociatedIrp.SystemBuffer,
344 IrpSp->Parameters.DeviceIoControl.OutputBufferLength,
346 Irp->IoStatus.Information = ReturnedLength;
347 if (!NT_SUCCESS(Status))
348 {
349 DPRINT1("QueryInformation failed (0x%x)\n", Status);
350 }
351 break;
352
354 if (IrpSp->Parameters.DeviceIoControl.InputBufferLength < sizeof(*BattSetInfo))
355 {
357 break;
358 }
359
360 BattSetInfo = Irp->AssociatedIrp.SystemBuffer;
361
362 Status = BattClass->MiniportInfo.SetInformation(BattClass->MiniportInfo.Context,
363 BattSetInfo->BatteryTag,
364 BattSetInfo->InformationLevel,
365 BattSetInfo->Buffer);
366 if (!NT_SUCCESS(Status))
367 {
368 DPRINT1("SetInformation failed (0x%x)\n", Status);
369 }
370 break;
371
372 default:
373 DPRINT1("Received unsupported IRP %x\n", IrpSp->Parameters.DeviceIoControl.IoControlCode);
374 /* Do NOT complete the irp */
376 }
377
378 Irp->IoStatus.Status = Status;
380
381 return Status;
382}
static PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(PIRP Irp)
_In_ ULONG _In_ BATTERY_QUERY_INFORMATION_LEVEL _In_ LONG _In_ ULONG _Out_ PULONG ReturnedLength
Definition: batclass.h:188
struct _BATTERY_WAIT_STATUS * PBATTERY_WAIT_STATUS
#define IOCTL_BATTERY_QUERY_TAG
Definition: batclass.h:84
#define IOCTL_BATTERY_SET_INFORMATION
Definition: batclass.h:88
#define IOCTL_BATTERY_QUERY_INFORMATION
Definition: batclass.h:86
struct _BATTERY_STATUS BATTERY_STATUS
#define IOCTL_BATTERY_QUERY_STATUS
Definition: batclass.h:90
#define EVENT_BATTERY_STATUS
Definition: battc.h:32
#define EVENT_BATTERY_TAG
Definition: battc.h:31
_In_ PIRP Irp
Definition: csq.h:116
#define STATUS_TIMEOUT
Definition: d3dkmdt.h:49
#define STATUS_NOT_SUPPORTED
Definition: d3dkmdt.h:48
_In_ PIO_STACK_LOCATION IrpSp
Definition: create.c:4138
#define KeWaitForSingleObject(pEvt, foo, a, b, c)
Definition: env_spec_w32.h:478
VOID FASTCALL ExAcquireFastMutex(IN PFAST_MUTEX FastMutex)
Definition: fmutex.c:23
VOID FASTCALL ExReleaseFastMutex(IN PFAST_MUTEX FastMutex)
Definition: fmutex.c:31
#define KernelMode
Definition: asm.h:38
#define Int32x32To64(a, b)
#define IoCompleteRequest
Definition: irp.c:1240
static ULONG Timeout
Definition: ping.c:61
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:69
UCHAR EventTrigger
Definition: battc.h:22
PVOID EventTriggerContext
Definition: battc.h:23
BOOLEAN Waiting
Definition: battc.h:20
BCLASS_QUERY_INFORMATION QueryInformation
Definition: batclass.h:252
BCLASS_SET_STATUS_NOTIFY SetStatusNotify
Definition: batclass.h:255
BCLASS_SET_INFORMATION SetInformation
Definition: batclass.h:253
BCLASS_DISABLE_STATUS_NOTIFY DisableStatusNotify
Definition: batclass.h:256
BCLASS_QUERY_STATUS QueryStatus
Definition: batclass.h:254
BCLASS_QUERY_TAG QueryTag
Definition: batclass.h:251
ULONG LowCapacity
Definition: batclass.h:204
ULONG HighCapacity
Definition: batclass.h:205
ULONG PowerState
Definition: batclass.h:203
BATTERY_QUERY_INFORMATION_LEVEL InformationLevel
Definition: batclass.h:110
BATTERY_SET_INFORMATION_LEVEL InformationLevel
Definition: batclass.h:141
ULONG PowerState
Definition: batclass.h:154
ULONG Capacity
Definition: batclass.h:155
struct _IO_STACK_LOCATION::@1583::@1584 DeviceIoControl
union _IO_STACK_LOCATION::@1583 Parameters
uint32_t * PULONG
Definition: typedefs.h:59
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_NO_SUCH_DEVICE
Definition: udferr_usr.h:136
#define IO_NO_INCREMENT
Definition: iotypes.h:598
@ Executive
Definition: ketypes.h:415

Referenced by CmBattIoctl(), and CompBattIoctl().

◆ BatteryClassQueryWmiDataBlock()

BCLASSAPI NTSTATUS NTAPI BatteryClassQueryWmiDataBlock ( PVOID  ClassData,
PDEVICE_OBJECT  DeviceObject,
PIRP  Irp,
ULONG  GuidIndex,
PULONG  InstanceLengthArray,
ULONG  OutBufferSize,
PUCHAR  Buffer 
)

Definition at line 63 of file battc.c.

70{
72
74}
#define UNIMPLEMENTED
Definition: ntoskrnl.c:15
#define STATUS_WMI_GUID_NOT_FOUND
Definition: ntstatus.h:776

◆ BatteryClassStatusNotify()

BCLASSAPI NTSTATUS NTAPI BatteryClassStatusNotify ( _In_ PVOID  ClassData)

Definition at line 79 of file battc.c.

81{
82 PBATTERY_CLASS_DATA BattClass;
83 PBATTERY_WAIT_STATUS BattWait;
84 BATTERY_STATUS BattStatus;
85 ULONG Tag;
87
88 DPRINT("Received battery status notification from %p\n", ClassData);
89
90 BattClass = ClassData;
91 BattWait = BattClass->EventTriggerContext;
92
93 ExAcquireFastMutex(&BattClass->Mutex);
94 if (!BattClass->Waiting)
95 {
96 ExReleaseFastMutex(&BattClass->Mutex);
97 return STATUS_SUCCESS;
98 }
99
100 switch (BattClass->EventTrigger)
101 {
103 ExReleaseFastMutex(&BattClass->Mutex);
104 Status = BattClass->MiniportInfo.QueryTag(BattClass->MiniportInfo.Context,
105 &Tag);
106 if (!NT_SUCCESS(Status))
107 return Status;
108
109 ExAcquireFastMutex(&BattClass->Mutex);
111 ExReleaseFastMutex(&BattClass->Mutex);
112 break;
113
115 ExReleaseFastMutex(&BattClass->Mutex);
116 Status = BattClass->MiniportInfo.QueryStatus(BattClass->MiniportInfo.Context,
117 BattWait->BatteryTag,
118 &BattStatus);
119 if (!NT_SUCCESS(Status))
120 return Status;
121
122 ExAcquireFastMutex(&BattClass->Mutex);
123
124 if (BattWait->PowerState != BattStatus.PowerState ||
125 BattWait->HighCapacity < BattStatus.Capacity ||
126 BattWait->LowCapacity > BattStatus.Capacity)
127 {
129 }
130
131 ExReleaseFastMutex(&BattClass->Mutex);
132 break;
133
134 default:
135 ExReleaseFastMutex(&BattClass->Mutex);
136 ASSERT(FALSE);
137 break;
138 }
139
140 return STATUS_SUCCESS;
141}
#define KeSetEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:476
#define ASSERT(a)
Definition: mode.c:44
_Must_inspect_result_ _In_ WDFDEVICE _In_ BOOLEAN _In_opt_ PVOID Tag
Definition: wdfdevice.h:4065

Referenced by CmBattNotifyHandler(), CmBattWakeDpc(), CompBattMonitorIrpCompleteWorker(), and CompBattRemoveBattery().

◆ BatteryClassSystemControl()

BCLASSAPI NTSTATUS NTAPI BatteryClassSystemControl ( PVOID  ClassData,
PVOID  WmiLibContext,
PDEVICE_OBJECT  DeviceObject,
PIRP  Irp,
PVOID  Disposition 
)

Definition at line 49 of file battc.c.

54{
56
58}

Referenced by CmBattSystemControl().

◆ BatteryClassUnload()

BCLASSAPI NTSTATUS NTAPI BatteryClassUnload ( PVOID  ClassData)

Definition at line 27 of file battc.c.

28{
29 PBATTERY_CLASS_DATA BattClass;
30
31 DPRINT("Battery %p is being unloaded\n", ClassData);
32
33 BattClass = ClassData;
34 if (BattClass->InterfaceName.Length != 0)
35 {
38 }
39
40 ExFreePoolWithTag(BattClass,
42
43 return STATUS_SUCCESS;
44}
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
NTSYSAPI VOID NTAPI RtlFreeUnicodeString(PUNICODE_STRING UnicodeString)

Referenced by CmBattAddBattery(), and CmBattRemoveDevice().

◆ DriverEntry()

NTSTATUS NTAPI DriverEntry ( PDRIVER_OBJECT  DriverObject,
PUNICODE_STRING  RegistryPath 
)

Definition at line 16 of file battc.c.

18{
19 DPRINT("Battery class driver initialized\n");
20
21 return STATUS_SUCCESS;
22}