Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
6.4 KiB

  1. #define BATTERYCLASS 1
  2. #ifndef FAR
  3. #define FAR
  4. #endif
  5. #include <wdm.h>
  6. #include <wmistr.h>
  7. #include <wmilib.h>
  8. #include <batclass.h>
  9. //
  10. // Debug
  11. //
  12. #define DEBUG DBG
  13. #if DEBUG
  14. extern ULONG BattDebug;
  15. extern ULONG NextDeviceNum;
  16. #define BattPrint(l,m) if(l & BattDebug) DbgPrint m
  17. #else
  18. #define BattPrint(l,m)
  19. #endif
  20. #define BATT_LOW 0x00000001
  21. #define BATT_NOTE 0x00000002
  22. #define BATT_WARN 0x00000004
  23. #define BATT_ERROR 0x00000008
  24. #define BATT_TRACE 0x00000010
  25. #define BATT_MP_ERROR 0x00000100
  26. #define BATT_MP_DATA 0x00000200
  27. #define BATT_IOCTL 0x00001000
  28. #define BATT_IOCTL_DATA 0x00002000
  29. #define BATT_IOCTL_QUEUE 0x00004000
  30. #define BATT_WMI 0x00008000
  31. #define BATT_LOCK 0x00010000
  32. #define BATT_DEBUG 0x80000000
  33. //
  34. // Battery class info
  35. //
  36. #define NTMS 10000L // 1 millisecond is ten thousand 100ns
  37. #define NTSEC (NTMS * 1000L)
  38. #define NTMIN ((ULONGLONG) 60 * NTSEC)
  39. #define SEC 1000
  40. #define MIN (60 * SEC)
  41. #define MIN_STATUS_POLL_RATE (3L * NTMIN)
  42. // This is the slowest rate at which we should ever poll
  43. // the battery when doing polling.
  44. #define MAX_STATUS_POLL_RATE (20 * NTSEC)
  45. // This is in general the fastest we should ever poll the battery.
  46. #define INVALID_DATA_POLL_RATE (1 * NTSEC)
  47. // If the battery returned invalid information, we want to poll
  48. // it more frequesntly, since invalid information generally
  49. // indicates that the battery was in a transition state. The user
  50. // will not want to wait 20 seconds for the UI to update, but we don't
  51. // want to poll too fast and hurt the performance of a machine with a
  52. // poorly designed battery too much.
  53. #define INVALID_DATA_MAX_RETRY 10
  54. // Only retry 20 time before giving up.
  55. // This should be reset on any notifiation from the battery.
  56. #define STATUS_VALID_TIME (2 * NTSEC)
  57. // If a request is received within STATUS_VALID_TIME of the last request
  58. // time information was read, and there hasn't been a notification from
  59. // the battery, the driver will assume that the cached values are good enough.
  60. //
  61. // WMI info
  62. //
  63. #define MOFRESOURCENAME L"BATTCWMI"
  64. #define MOFREGISTRYPATH L"\\REGISTRY\\MACHINE\\SYSTEM\\CurrentControlSet\\Services\\BattC"
  65. //#define MOFREGISTRYPATH L"\\REGISTRY\\MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Class\\{72631E54-78A4-11D0-BCF7-00AA00B7B32A}"
  66. typedef enum {
  67. BattWmiStatusId,
  68. BattWmiRuntimeId,
  69. BattWmiTemperatureId,
  70. BattWmiFullChargedCapacityId,
  71. BattWmiCycleCountId,
  72. BattWmiStaticDataId,
  73. BattWmiStatusChangeId,
  74. BattWmiTagChangeId,
  75. BattWmiTotalGuids
  76. } BATT_WMI_GUID_INDEX;
  77. //
  78. // Non-paged battery class information
  79. //
  80. typedef struct {
  81. //
  82. // Pointer to paged information
  83. //
  84. struct _BATT_INFO *BattInfo; // Pointer to paged portion
  85. //
  86. // General
  87. //
  88. KTIMER WorkerTimer; // Timer to get worker thread
  89. KDPC WorkerDpc; // DPC to get worker thread
  90. KTIMER TagTimer; // Timer for query tag requests
  91. KDPC TagDpc;
  92. WORK_QUEUE_ITEM WorkerThread; // WORK_QUEUE to get worker thread
  93. ULONG WorkerActive;
  94. ULONG CheckStatus; // Worker to check status
  95. ULONG CheckTag; // Worker to check for battery tag
  96. ULONG StatusNotified; // Notification has occured (must re-read)
  97. ULONG TagNotified;
  98. FAST_MUTEX Mutex; // Synchorize with worker thread
  99. BOOLEAN WantToRemove; // Syncronize device removal
  100. LONG InUseCount;
  101. KEVENT ReadyToRemove;
  102. #if DEBUG
  103. ULONG DeviceNum; // Device number for debug prints
  104. #endif
  105. } BATT_NP_INFO, *PBATT_NP_INFO;
  106. //
  107. // Paged battery class information
  108. //
  109. typedef struct _BATT_INFO {
  110. WMILIB_CONTEXT WmiLibContext;
  111. ULONG WmiGuidIndex; // Used to ignore miniclass WMI
  112. // GUIDs
  113. //
  114. // IO
  115. //
  116. ULONG Tag; // Current battery tag
  117. LIST_ENTRY IoQueue; // IRPs waiting to be processed
  118. LIST_ENTRY StatusQueue; // Waiting status requests
  119. LIST_ENTRY TagQueue; // Waiting battery tag requests
  120. LIST_ENTRY WmiQueue;
  121. ULONGLONG TagTime; // Time when tag was read
  122. ULONGLONG StatusTime; // Time when status was read
  123. BATTERY_STATUS Status; // The status
  124. ULONG InvalidRetryCount; // How many times ni a row has the battery returned invalid data?
  125. #if DEBUG
  126. ULONG FullChargedCap;
  127. PBATT_NP_INFO BattNPInfo;
  128. #endif
  129. ULONG NotifyTimeout; // LCD timeout of wat
  130. BATTERY_MINIPORT_INFO Mp; // Miniport Info
  131. UNICODE_STRING SymbolicLinkName; // Name returned by IoRegisterDeviceInterface
  132. } BATT_INFO, *PBATT_INFO;
  133. //
  134. // WmiQueue entry
  135. //
  136. typedef struct _BATT_WMI_REQUEST {
  137. LIST_ENTRY ListEntry;
  138. PDEVICE_OBJECT DeviceObject;
  139. PIRP Irp;
  140. BATT_WMI_GUID_INDEX GuidIndex;
  141. IN OUT PULONG InstanceLengthArray;
  142. IN ULONG OutBufferSize;
  143. OUT PUCHAR Buffer;
  144. } BATT_WMI_REQUEST, *PBATT_WMI_REQUEST;
  145. //
  146. // Prototypes
  147. //
  148. VOID
  149. BattCWorkerDpc (
  150. IN struct _KDPC *Dpc,
  151. IN PVOID DeferredContext,
  152. IN PVOID SystemArgument1,
  153. IN PVOID SystemArgument2
  154. );
  155. VOID
  156. BattCWorkerThread (
  157. IN PVOID Context
  158. );
  159. VOID
  160. BattCQueueWorker (
  161. IN PBATT_NP_INFO BattNPInfo,
  162. IN BOOLEAN CheckStatus
  163. );
  164. NTSTATUS
  165. BatteryIoctl(
  166. IN ULONG Ioctl,
  167. IN PDEVICE_OBJECT DeviceObject,
  168. IN PVOID InputBuffer,
  169. IN ULONG InputBufferLength,
  170. IN PVOID OutputBuffer,
  171. IN ULONG OutputBufferLength,
  172. IN BOOLEAN PrivateIoctl
  173. );
  174. VOID
  175. BattCTagDpc (
  176. IN struct _KDPC *Dpc,
  177. IN PVOID DeferredContext,
  178. IN PVOID SystemArgument1,
  179. IN PVOID SystemArgument2
  180. );
  181. VOID
  182. BattCCancelTag (
  183. IN PDEVICE_OBJECT DeviceObject,
  184. IN PIRP Irp
  185. );