xref: /reactos/drivers/battery/compbatt/compmisc.c (revision 22577aed)
1 /*
2  * PROJECT:     ReactOS Composite Battery Driver
3  * LICENSE:     MIT (https://spdx.org/licenses/MIT)
4  * PURPOSE:     Miscellaneous Support Routines
5  * COPYRIGHT:   Copyright 2010 ReactOS Portable Systems Group <ros.arm@reactos.org>
6  */
7 
8 /* INCLUDES *******************************************************************/
9 
10 #include "compbatt.h"
11 
12 /* FUNCTIONS ******************************************************************/
13 
14 NTSTATUS
15 NTAPI
16 BatteryIoctl(
17     _In_ ULONG IoControlCode,
18     _In_ PDEVICE_OBJECT DeviceObject,
19     _In_ PVOID InputBuffer,
20     _In_ ULONG InputBufferLength,
21     _Out_ PVOID OutputBuffer,
22     _Inout_ ULONG OutputBufferLength,
23     _In_ BOOLEAN InternalDeviceIoControl)
24 {
25     IO_STATUS_BLOCK IoStatusBlock;
26     KEVENT Event;
27     NTSTATUS Status;
28     PIRP Irp;
29     PAGED_CODE();
30     if (CompBattDebug & COMPBATT_DEBUG_TRACE) DbgPrint("CompBatt: ENTERING BatteryIoctl\n");
31 
32     /* Initialize the event and IRP */
33     KeInitializeEvent(&Event, SynchronizationEvent, 0);
34     Irp = IoBuildDeviceIoControlRequest(IoControlCode,
35                                         DeviceObject,
36                                         InputBuffer,
37                                         InputBufferLength,
38                                         OutputBuffer,
39                                         OutputBufferLength,
40                                         InternalDeviceIoControl,
41                                         &Event,
42                                         &IoStatusBlock);
43     if (Irp)
44     {
45         /* Call the class driver miniport */
46         Status = IoCallDriver(DeviceObject, Irp);
47         if (Status == STATUS_PENDING)
48         {
49             /* Wait for result */
50             KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
51             Status = IoStatusBlock.Status;
52         }
53 
54         /* Print failure */
55         if (!(NT_SUCCESS(Status)) && (CompBattDebug & COMPBATT_DEBUG_ERR))
56             DbgPrint("BatteryIoctl: Irp failed - %x\n", Status);
57 
58         /* Done */
59         if (CompBattDebug & COMPBATT_DEBUG_TRACE) DbgPrint("CompBatt: EXITING BatteryIoctl\n");
60     }
61     else
62     {
63         /* Out of memory */
64         if (CompBattDebug & COMPBATT_DEBUG_ERR) DbgPrint("BatteryIoctl: couldn't create Irp\n");
65         Status = STATUS_INSUFFICIENT_RESOURCES;
66     }
67 
68     /* Return status */
69     return Status;
70 }
71 
72 NTSTATUS
73 NTAPI
74 CompBattGetDeviceObjectPointer(
75     _In_ PUNICODE_STRING DeviceName,
76     _In_ ACCESS_MASK DesiredAccess,
77     _Out_ PFILE_OBJECT *FileObject,
78     _Out_ PDEVICE_OBJECT *DeviceObject)
79 {
80     NTSTATUS Status;
81     OBJECT_ATTRIBUTES ObjectAttributes;
82     IO_STATUS_BLOCK IoStatusBlock;
83     PFILE_OBJECT LocalFileObject;
84     HANDLE DeviceHandle;
85     PAGED_CODE();
86 
87     /* Open a file object handle to the device */
88     InitializeObjectAttributes(&ObjectAttributes,
89                                DeviceName,
90                                OBJ_KERNEL_HANDLE,
91                                NULL,
92                                NULL);
93     Status = ZwCreateFile(&DeviceHandle,
94                           DesiredAccess,
95                           &ObjectAttributes,
96                           &IoStatusBlock,
97                           NULL,
98                           0,
99                           FILE_SHARE_READ | FILE_SHARE_WRITE,
100                           FILE_OPEN,
101                           0,
102                           NULL,
103                           0);
104     if (NT_SUCCESS(Status))
105     {
106         /* Reference the file object */
107         Status = ObReferenceObjectByHandle(DeviceHandle,
108                                            0,
109                                            *IoFileObjectType,
110                                            KernelMode,
111                                            (PVOID)&LocalFileObject,
112                                            NULL);
113         if (NT_SUCCESS(Status))
114         {
115             /* Return the FO and the associated DO */
116             *FileObject = LocalFileObject;
117             *DeviceObject = IoGetRelatedDeviceObject(LocalFileObject);
118         }
119 
120         /* Close the handle */
121         ZwClose(DeviceHandle);
122     }
123 
124     /* Return status */
125     return Status;
126 }
127 
128 /* EOF */
129