xref: /reactos/drivers/bus/acpi/compbatt/compmisc.c (revision bbabe248)
1 /*
2  * PROJECT:         ReactOS Composite Battery Driver
3  * LICENSE:         BSD - See COPYING.ARM in the top level directory
4  * FILE:            boot/drivers/bus/acpi/compbatt/compmisc.c
5  * PURPOSE:         Miscellaneous Support Routines
6  * PROGRAMMERS:     ReactOS Portable Systems Group
7  */
8 
9 /* INCLUDES *******************************************************************/
10 
11 #include "compbatt.h"
12 
13 /* FUNCTIONS ******************************************************************/
14 
15 NTSTATUS
16 NTAPI
17 BatteryIoctl(IN ULONG IoControlCode,
18              IN PDEVICE_OBJECT DeviceObject,
19              IN PVOID InputBuffer,
20              IN ULONG InputBufferLength,
21              IN PVOID OutputBuffer,
22              IN 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 & 0x100) 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 & 8))
56             DbgPrint("BatteryIoctl: Irp failed - %x\n", Status);
57 
58         /* Done */
59         if (CompBattDebug & 0x100) DbgPrint("CompBatt: EXITING BatteryIoctl\n");
60     }
61     else
62     {
63         /* Out of memory */
64         if (CompBattDebug & 8) 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(IN PUNICODE_STRING DeviceName,
75                                IN ACCESS_MASK DesiredAccess,
76                                OUT PFILE_OBJECT *FileObject,
77                                OUT PDEVICE_OBJECT *DeviceObject)
78 {
79     NTSTATUS Status;
80     OBJECT_ATTRIBUTES ObjectAttributes;
81     IO_STATUS_BLOCK IoStatusBlock;
82     PFILE_OBJECT LocalFileObject;
83     HANDLE DeviceHandle;
84     PAGED_CODE();
85 
86     /* Open a file object handle to the device */
87     InitializeObjectAttributes(&ObjectAttributes,
88                                DeviceName,
89                                OBJ_KERNEL_HANDLE,
90                                NULL,
91                                NULL);
92     Status = ZwCreateFile(&DeviceHandle,
93                           DesiredAccess,
94                           &ObjectAttributes,
95                           &IoStatusBlock,
96                           NULL,
97                           0,
98                           FILE_SHARE_READ | FILE_SHARE_WRITE,
99                           FILE_OPEN,
100                           0,
101                           NULL,
102                           0);
103     if (NT_SUCCESS(Status))
104     {
105         /* Reference the file object */
106         Status = ObReferenceObjectByHandle(DeviceHandle,
107                                            0,
108                                            *IoFileObjectType,
109                                            KernelMode,
110                                            (PVOID)&LocalFileObject,
111                                            NULL);
112         if (NT_SUCCESS(Status))
113         {
114             /* Return the FO and the associated DO */
115             *FileObject = LocalFileObject;
116             *DeviceObject = IoGetRelatedDeviceObject(LocalFileObject);
117         }
118 
119         /* Close the handle */
120         ZwClose(DeviceHandle);
121     }
122 
123     /* Return status */
124     return Status;
125 }
126 
127 /* EOF */
128