xref: /reactos/drivers/battery/compbatt/compbatt.h (revision a8da29e8)
1 /*
2  * PROJECT:     ReactOS Composite Battery Driver
3  * LICENSE:     MIT (https://spdx.org/licenses/MIT)
4  * PURPOSE:     Composite battery main header file
5  * COPYRIGHT:   Copyright 2010 ReactOS Portable Systems Group <ros.arm@reactos.org>
6  *              Copyright 2024 George Bișoc <george.bisoc@reactos.org>
7  */
8 
9 /* INCLUDES *******************************************************************/
10 
11 #ifndef _COMPBATT_PCH_
12 #define _COMPBATT_PCH_
13 
14 #include <wdm.h>
15 #include <batclass.h>
16 
17 /* DEFINES ********************************************************************/
18 
19 //
20 // I/O remove lock allocate tag
21 //
22 #define COMPBATT_TAG                            'aBoC'
23 
24 //
25 // Composite battery flags
26 //
27 #define COMPBATT_BATTERY_INFORMATION_PRESENT    0x04
28 #define COMPBATT_STATUS_NOTIFY_SET              0x10
29 #define COMPBATT_TAG_ASSIGNED                   0x80
30 
31 //
32 // IRP complete worker mode states
33 //
34 #define COMPBATT_QUERY_TAG                      1
35 #define COMPBATT_READ_STATUS                    2
36 
37 //
38 // Low/High capacity wait constants
39 //
40 #define COMPBATT_WAIT_MIN_LOW_CAPACITY          0
41 #define COMPBATT_WAIT_MAX_HIGH_CAPACITY         0x7FFFFFFF
42 
43 //
44 // One hour in seconds, used to calculate the total rate of each battery for time estimation
45 //
46 #define COMPBATT_ATRATE_HOUR_IN_SECS            3600
47 
48 //
49 // Time constant of which the battery status data is considered fresh (50000000 * 100ns == 5s)
50 //
51 #define COMPBATT_FRESH_STATUS_TIME              50000000
52 
53 //
54 // Macro that calculates the delta of a battery's capacity
55 //
56 #define COMPUTE_BATT_CAP_DELTA(LowDiff, Batt, TotalRate)            \
57     ((ULONG)(((LONGLONG)LowDiff * (Batt)->BatteryStatus.Rate) / TotalRate))
58 
59 //
60 // Macro that calculates the "At Rate" time drain of the battery
61 //
62 #define COMPUTE_ATRATE_DRAIN(Batt, Time)                            \
63     ((LONG)((Batt)->BatteryStatus.Capacity) * COMPBATT_ATRATE_HOUR_IN_SECS / Time)
64 
65 //
66 // Composite battery debug levels
67 //
68 #define COMPBATT_DEBUG_INFO                     0x1
69 #define COMPBATT_DEBUG_TRACE                    0x2
70 #define COMPBATT_DEBUG_WARN                     0x4
71 #define COMPBATT_DEBUG_ERR                      0x10
72 #define COMPBATT_DEBUG_ALL_LEVELS               (COMPBATT_DEBUG_INFO | COMPBATT_DEBUG_TRACE | COMPBATT_DEBUG_WARN | COMPBATT_DEBUG_ERR)
73 
74 /* STRUCTURES *****************************************************************/
75 
76 //
77 // Individual ACPI battery data
78 //
79 typedef struct _COMPBATT_BATTERY_DATA
80 {
81     /* The linked battery with the Composite Battery */
82     LIST_ENTRY BatteryLink;
83 
84     /* I/O remove lock which protects the battery from being removed */
85     IO_REMOVE_LOCK RemoveLock;
86 
87     /*
88      * The associated device object (usually CMBATT) and the I/O battery packet
89      * which is used to transport and gather battery data.
90      */
91     PDEVICE_OBJECT DeviceObject;
92     PIRP Irp;
93 
94     /*
95      * The Executive work item, which serves as a worker item for the
96      * IRP battery monitor worker.
97      */
98     WORK_QUEUE_ITEM WorkItem;
99 
100     /*
101      * Execution state mode of the individual battery. Only two modes are valid:
102      *
103      * COMPBATT_QUERY_TAG - The battery is currently waiting for a tag to get assigned;
104      * COMPBATT_READ_STATUS - The battery is querying battery status.
105      */
106     UCHAR Mode;
107 
108     /*
109      * The battery wait configuration settings, set up by the SetStatusNotify method.
110      * These values are used to instruct CMBATT when the battery status should be retrieved.
111      */
112     BATTERY_WAIT_STATUS WaitStatus;
113 
114     /*
115      * A union that serves as the buffer which holds battery monitor IRP data, specifically
116      * managed by CompBattMonitorIrpCompleteWorker, to avoid allocating separate memory pools.
117      */
118     union
119     {
120         BATTERY_WAIT_STATUS WorkerWaitStatus;
121         BATTERY_STATUS WorkerStatus;
122         ULONG WorkerTag;
123     } WorkerBuffer;
124 
125     /* The ID of the battery that associates the identification of this battery */
126     ULONG Tag;
127 
128     /*
129      * The battery flags that govern the behavior of the battery. The valid flags are:
130      *
131      * COMPBATT_BATTERY_INFORMATION_PRESENT - The static battery information ha been
132      * queried. Re-querying such information is not needed.
133      *
134      * COMPBATT_STATUS_NOTIFY_SET - The current notification wait settings are valid.
135      *
136      * COMPBATT_TAG_ASSIGNED - The battery has a tag assigned and it can be read.
137      */
138     ULONG Flags;
139 
140     /* The static battery information and battery status */
141     BATTERY_INFORMATION BatteryInformation;
142     BATTERY_STATUS BatteryStatus;
143 
144     /* The interrupt time of which the battery status was last read */
145     ULONGLONG InterruptTime;
146 
147     /* A uniquely given name of the battery that associates it */
148     UNICODE_STRING BatteryName;
149 } COMPBATT_BATTERY_DATA, *PCOMPBATT_BATTERY_DATA;
150 
151 //
152 // Composite battery device extension data
153 //
154 typedef struct _COMPBATT_DEVICE_EXTENSION
155 {
156     /*
157      * The class data initialized and used by Battery Class. It contains information
158      * such as miniport data used for registration and communication between the
159      * Composite Battery and Battery Class, wait and context events, etc.
160      */
161     PVOID ClassData;
162 
163     /*
164      * The successor computed tag. This field is used when there are more upcoming
165      * batteries to be connected with the Composite Battery, of which the tag is
166      * incremented by 1 by each new battery that is connected.
167      */
168     ULONG NextTag;
169 
170     /* A list of linked batteries connected with the Composite Battery */
171     LIST_ENTRY BatteryList;
172 
173     /* A mutex lock which ensures proper synchronization of Composite Battery operations */
174     FAST_MUTEX Lock;
175 
176     /* The ID of the Composite Battery */
177     ULONG Tag;
178 
179     /*
180      * The battery flags that govern the behavior of the battery. The valid flags are:
181      *
182      * COMPBATT_BATTERY_INFORMATION_PRESENT - The static battery information has been
183      * queried. Re-querying such information is not needed.
184      *
185      * COMPBATT_STATUS_NOTIFY_SET - The current notification wait settings are valid.
186      *
187      * COMPBATT_TAG_ASSIGNED - The battery has a tag assigned and it can be read.
188      */
189     ULONG Flags;
190 
191     /*
192      * The Composite Battery static information, status and wait status settings.
193      * Note that both the battery information and status are combined, based upon
194      * the individual information and status of each linked battery.
195      */
196     BATTERY_INFORMATION BatteryInformation;
197     BATTERY_STATUS BatteryStatus;
198     BATTERY_WAIT_STATUS WaitNotifyStatus;
199 
200     /* The interrupt time of which the battery status was last read */
201     ULONGLONG InterruptTime;
202 
203     /*
204      * The physical device object that associates the Composite Battery and
205      * the attached device, typically the ACPI driver.
206      */
207     PDEVICE_OBJECT AttachedDevice;
208     PDEVICE_OBJECT DeviceObject;
209 
210     /* The notification entry that identifies the registered I/O PnP notification */
211     PVOID NotificationEntry;
212 } COMPBATT_DEVICE_EXTENSION, *PCOMPBATT_DEVICE_EXTENSION;
213 
214 /* PROTOTYPES *****************************************************************/
215 
216 NTSTATUS
217 NTAPI
218 CompBattAddDevice(
219     _In_ PDRIVER_OBJECT DriverObject,
220     _In_ PDEVICE_OBJECT PdoDeviceObject
221 );
222 
223 NTSTATUS
224 NTAPI
225 CompBattPowerDispatch(
226     _In_ PDEVICE_OBJECT DeviceObject,
227     _In_ PIRP Irp
228 );
229 
230 NTSTATUS
231 NTAPI
232 CompBattPnpDispatch(
233     _In_ PDEVICE_OBJECT DeviceObject,
234     _In_ PIRP Irp
235 );
236 
237 NTSTATUS
238 NTAPI
239 CompBattQueryInformation(
240     _In_ PCOMPBATT_DEVICE_EXTENSION FdoExtension,
241     _In_ ULONG Tag,
242     _In_ BATTERY_QUERY_INFORMATION_LEVEL InfoLevel,
243     _In_opt_ LONG AtRate,
244     _In_ PVOID Buffer,
245     _In_ ULONG BufferLength,
246     _Out_ PULONG ReturnedLength
247 );
248 
249 NTSTATUS
250 NTAPI
251 CompBattQueryStatus(
252     _In_ PCOMPBATT_DEVICE_EXTENSION DeviceExtension,
253     _In_ ULONG Tag,
254     _Out_ PBATTERY_STATUS BatteryStatus
255 );
256 
257 NTSTATUS
258 NTAPI
259 CompBattGetEstimatedTime(
260     _Out_ PULONG Time,
261     _In_ PCOMPBATT_DEVICE_EXTENSION DeviceExtension
262 );
263 
264 NTSTATUS
265 NTAPI
266 CompBattSetStatusNotify(
267     _In_ PCOMPBATT_DEVICE_EXTENSION DeviceExtension,
268     _In_ ULONG BatteryTag,
269     _In_ PBATTERY_NOTIFY BatteryNotify
270 );
271 
272 NTSTATUS
273 NTAPI
274 CompBattDisableStatusNotify(
275     _In_ PCOMPBATT_DEVICE_EXTENSION DeviceExtension
276 );
277 
278 NTSTATUS
279 NTAPI
280 CompBattQueryTag(
281     _In_ PCOMPBATT_DEVICE_EXTENSION DeviceExtension,
282     _Out_ PULONG Tag
283 );
284 
285 NTSTATUS
286 NTAPI
287 CompBattMonitorIrpComplete(
288     _In_ PDEVICE_OBJECT DeviceObject,
289     _In_ PIRP Irp,
290     _In_ PVOID Context
291 );
292 
293 VOID
294 NTAPI
295 CompBattMonitorIrpCompleteWorker(
296     _In_ PCOMPBATT_BATTERY_DATA BatteryData
297 );
298 
299 NTSTATUS
300 NTAPI
301 CompBattGetDeviceObjectPointer(
302     _In_ PUNICODE_STRING DeviceName,
303     _In_ ACCESS_MASK DesiredAccess,
304     _Out_ PFILE_OBJECT *FileObject,
305     _Out_ PDEVICE_OBJECT *DeviceObject
306 );
307 
308 NTSTATUS
309 NTAPI
310 BatteryIoctl(
311     _In_ ULONG IoControlCode,
312     _In_ PDEVICE_OBJECT DeviceObject,
313     _In_ PVOID InputBuffer,
314     _In_ ULONG InputBufferLength,
315     _Out_ PVOID OutputBuffer,
316     _Inout_ ULONG OutputBufferLength,
317     _In_ BOOLEAN InternalDeviceIoControl
318 );
319 
320 NTSTATUS
321 NTAPI
322 CompBattRemoveBattery(
323     _In_ PUNICODE_STRING BatteryName,
324     _In_ PCOMPBATT_DEVICE_EXTENSION DeviceExtension
325 );
326 
327 extern ULONG CompBattDebug;
328 
329 #endif /* _COMPBATT_PCH_ */
330