Leaked source code of windows server 2003
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.

401 lines
9.6 KiB

  1. #include <wdm.h>
  2. #include <batclass.h>
  3. #ifndef FAR
  4. #define FAR
  5. #endif
  6. //
  7. // Debug
  8. //
  9. #define DEBUG DBG
  10. #if DEBUG
  11. extern ULONG CompBattDebug;
  12. #define BattPrint(l,m) if(l & CompBattDebug) DbgPrint m
  13. #else
  14. #define BattPrint(l,m)
  15. #endif
  16. #define BATT_LOW 0x00000001
  17. #define BATT_NOTE 0x00000002
  18. #define BATT_WARN 0x00000004
  19. #define BATT_ERROR 0x00000008
  20. #define BATT_ERRORS (BATT_ERROR | BATT_WARN)
  21. #define BATT_MP 0x00000010
  22. #define BATT_DEBUG 0x00000020
  23. #define BATT_TRACE 0x00000100
  24. #define BATT_DATA 0x00000200
  25. //
  26. // Battery class info
  27. //
  28. #define NTMS 10000L // 1 millisecond is ten thousand 100ns
  29. #define NTSEC (NTMS * 1000L)
  30. #define NTMIN ((ULONGLONG) 60 * NTSEC)
  31. #define SEC 1000
  32. #define MIN (60 * SEC)
  33. //
  34. // Poll rates for when a notification alarm cannot be set
  35. //
  36. #define MIN_STATUS_POLL_RATE (3L * NTMIN)
  37. #define MAX_STATUS_POLL_RATE (20 * NTSEC)
  38. #define STATUS_VALID_TIME (2 * NTSEC)
  39. #define MAX_HIGH_CAPACITY 0x7fffffff
  40. #define MIN_LOW_CAPACITY 0x0
  41. //
  42. // Charge/Discharge policy values (in percent)
  43. //
  44. #define BATTERY_MIN_SAFE_CAPACITY 2 // Min we will attempt to run on
  45. #define BATTERY_MAX_CHARGE_CAPACITY 90 // Max we will attempt to charge
  46. //
  47. // Cache expiration timeouts -- when the cached battery status/info expires.
  48. //
  49. #define CACHE_STATUS_TIMEOUT (4 * NTSEC)
  50. #define CACHE_INFO_TIMEOUT (4 * NTSEC)
  51. //
  52. // Cached battery info
  53. //
  54. typedef struct {
  55. ULONG Tag;
  56. ULONG Valid;
  57. BATTERY_INFORMATION Info;
  58. ULONGLONG InfoTimeStamp;
  59. UCHAR ManufacturerNameLength;
  60. UCHAR ManufacturerName[MAX_BATTERY_STRING_SIZE];
  61. UCHAR DeviceNameLength;
  62. UCHAR DeviceName[MAX_BATTERY_STRING_SIZE];
  63. BATTERY_MANUFACTURE_DATE ManufacturerDate;
  64. ULONG SerialNumber;
  65. BATTERY_STATUS Status;
  66. ULONGLONG StatusTimeStamp;
  67. } STATIC_BAT_INFO, *PSTATIC_BAT_INFO;
  68. #define VALID_TAG_DATA 0x01 // manufacturer, device, serial #
  69. #define VALID_MODE 0x02
  70. #define VALID_INFO 0x04
  71. #define VALID_CYCLE_COUNT 0x08
  72. #define VALID_SANITY_CHECK 0x10
  73. #define VALID_TAG 0x80
  74. #define VALID_NOTIFY 0x100
  75. #define VALID_ALL 0x1F // (does not include tag)
  76. //
  77. // Locking mechanism for battery nodes so we don't delete out from under
  78. // ourselves. I would just use an IO_REMOVE_LOCK, but that's NT not WDM...
  79. //
  80. typedef struct _COMPBATT_DELETE_LOCK {
  81. BOOLEAN Deleted;
  82. BOOLEAN Reserved [3];
  83. LONG RefCount;
  84. KEVENT DeleteEvent;
  85. } COMPBATT_DELETE_LOCK, *PCOMPBATT_DELETE_LOCK;
  86. VOID
  87. CompbattInitializeDeleteLock (
  88. IN PCOMPBATT_DELETE_LOCK Lock
  89. );
  90. NTSTATUS
  91. CompbattAcquireDeleteLock (
  92. IN PCOMPBATT_DELETE_LOCK Lock
  93. );
  94. VOID
  95. CompbattReleaseDeleteLock (
  96. IN PCOMPBATT_DELETE_LOCK Lock
  97. );
  98. VOID
  99. CompbattReleaseDeleteLockAndWait (
  100. IN PCOMPBATT_DELETE_LOCK Lock
  101. );
  102. //
  103. // Battery node in the composite's list of batteries
  104. //
  105. typedef struct {
  106. LIST_ENTRY Batteries; // All batteries in composite
  107. COMPBATT_DELETE_LOCK DeleteLock;
  108. PDEVICE_OBJECT DeviceObject; // device object for the battery
  109. PIRP StatusIrp; // current status irp at device
  110. WORK_QUEUE_ITEM WorkItem; // Used for restarting status Irp
  111. // if it is completed at DPC level
  112. BOOLEAN NewBatt; // Is this a new battery on the list
  113. UCHAR State;
  114. BATTERY_WAIT_STATUS Wait;
  115. union {
  116. BATTERY_STATUS Status;
  117. BATTERY_WAIT_STATUS Wait;
  118. ULONG Tag;
  119. } IrpBuffer;
  120. //
  121. // Keep some static information around so we don't have to go out to the
  122. // batteries all the time.
  123. //
  124. STATIC_BAT_INFO Info;
  125. //
  126. // Symbolic link name for the battery. Since we calculate the length of this
  127. // structure based on the structure size plus the length of this string, the
  128. // string must be the last thing declared in the structure.
  129. //
  130. UNICODE_STRING BattName;
  131. } COMPOSITE_ENTRY, *PCOMPOSITE_ENTRY;
  132. #define CB_ST_GET_TAG 0
  133. #define CB_ST_GET_STATUS 1
  134. //
  135. // Composite battery device extension
  136. //
  137. typedef struct {
  138. PVOID Class; // Class information
  139. // ULONG Tag; // Current tag of composite battery
  140. ULONG NextTag; // Next tag
  141. LIST_ENTRY Batteries; // All batteries
  142. FAST_MUTEX ListMutex; // List synchronization
  143. //
  144. // Keep some static information around so we don't have to go out to the
  145. // batteries all the time.
  146. //
  147. STATIC_BAT_INFO Info;
  148. BATTERY_WAIT_STATUS Wait;
  149. PDEVICE_OBJECT LowerDevice; // PDO
  150. PDEVICE_OBJECT DeviceObject; // Compbatt Device
  151. PVOID NotificationEntry; // PnP registration handle
  152. } COMPOSITE_BATTERY, *PCOMPOSITE_BATTERY;
  153. //
  154. // Prototypes
  155. //
  156. NTSTATUS
  157. DriverEntry (
  158. IN PDRIVER_OBJECT DriverObject,
  159. IN PUNICODE_STRING RegistryPath
  160. );
  161. NTSTATUS
  162. CompBattIoctl(
  163. IN PDEVICE_OBJECT DeviceObject,
  164. IN PIRP Irp
  165. );
  166. NTSTATUS
  167. CompBattSystemControl(
  168. IN PDEVICE_OBJECT DeviceObject,
  169. IN PIRP Irp
  170. );
  171. NTSTATUS
  172. CompBattQueryTag (
  173. IN PVOID Context,
  174. OUT PULONG BatteryTag
  175. );
  176. NTSTATUS
  177. CompBattQueryInformation (
  178. IN PVOID Context,
  179. IN ULONG BatteryTag,
  180. IN BATTERY_QUERY_INFORMATION_LEVEL Level,
  181. IN LONG AtRate,
  182. OUT PVOID Buffer,
  183. IN ULONG BufferLength,
  184. OUT PULONG ReturnedLength
  185. );
  186. NTSTATUS
  187. CompBattQueryStatus (
  188. IN PVOID Context,
  189. IN ULONG BatteryTag,
  190. OUT PBATTERY_STATUS BatteryStatus
  191. );
  192. NTSTATUS
  193. CompBattSetStatusNotify (
  194. IN PVOID Context,
  195. IN ULONG BatteryTag,
  196. IN PBATTERY_NOTIFY BatteryNotify
  197. );
  198. NTSTATUS
  199. CompBattDisableStatusNotify (
  200. IN PVOID Context
  201. );
  202. NTSTATUS
  203. CompBattDriverEntry (
  204. IN PDRIVER_OBJECT DriverObject,
  205. IN PUNICODE_STRING RegistryPath
  206. );
  207. NTSTATUS
  208. CompBattGetBatteryInformation (
  209. IN PBATTERY_INFORMATION TotalBattInfo,
  210. IN PCOMPOSITE_BATTERY CompBatt
  211. );
  212. NTSTATUS
  213. CompBattGetBatteryGranularity (
  214. IN PBATTERY_REPORTING_SCALE GranularityBuffer,
  215. IN PCOMPOSITE_BATTERY CompBatt
  216. );
  217. NTSTATUS
  218. CompBattPrivateIoctl(
  219. IN PDEVICE_OBJECT DeviceObject,
  220. IN PIRP Irp
  221. );
  222. NTSTATUS
  223. CompBattGetEstimatedTime (
  224. IN PULONG TimeBuffer,
  225. IN PCOMPOSITE_BATTERY CompBatt
  226. );
  227. NTSTATUS
  228. CompBattAddDevice (
  229. IN PDRIVER_OBJECT DriverObject,
  230. IN PDEVICE_OBJECT PDO
  231. );
  232. NTSTATUS
  233. CompBattPowerDispatch(
  234. IN PDEVICE_OBJECT DeviceObject,
  235. IN PIRP Irp
  236. );
  237. NTSTATUS
  238. CompBattPnpDispatch(
  239. IN PDEVICE_OBJECT DeviceObject,
  240. IN PIRP Irp
  241. );
  242. VOID
  243. CompBattUnload(
  244. IN PDRIVER_OBJECT DriverObject
  245. );
  246. NTSTATUS
  247. CompBattOpenClose(
  248. IN PDEVICE_OBJECT DeviceObject,
  249. IN PIRP Irp
  250. );
  251. NTSTATUS
  252. BatteryIoctl(
  253. IN ULONG Ioctl,
  254. IN PDEVICE_OBJECT DeviceObject,
  255. IN PVOID InputBuffer,
  256. IN ULONG InputBufferLength,
  257. IN PVOID OutputBuffer,
  258. IN ULONG OutputBufferLength,
  259. IN BOOLEAN PrivateIoctl
  260. );
  261. NTSTATUS
  262. CompBattPnpEventHandler(
  263. IN PVOID NotificationStructure,
  264. IN PVOID Context
  265. );
  266. NTSTATUS
  267. CompBattAddNewBattery(
  268. IN PUNICODE_STRING SymbolicLinkName,
  269. IN PCOMPOSITE_BATTERY CompBatt
  270. );
  271. NTSTATUS
  272. CompBattRemoveBattery(
  273. IN PUNICODE_STRING SymbolicLinkName,
  274. IN PCOMPOSITE_BATTERY CompBatt
  275. );
  276. BOOLEAN
  277. IsBatteryAlreadyOnList(
  278. IN PUNICODE_STRING SymbolicLinkName,
  279. IN PCOMPOSITE_BATTERY CompBatt
  280. );
  281. PCOMPOSITE_ENTRY
  282. RemoveBatteryFromList(
  283. IN PUNICODE_STRING SymbolicLinkName,
  284. IN PCOMPOSITE_BATTERY CompBatt
  285. );
  286. NTSTATUS
  287. CompBattGetBatteries(
  288. IN PCOMPOSITE_BATTERY CompBatt
  289. );
  290. BOOLEAN
  291. CompBattVerifyStaticInfo (
  292. IN PCOMPOSITE_BATTERY CompBatt
  293. );
  294. VOID CompBattMonitorIrpCompleteWorker (
  295. IN PVOID Context
  296. );
  297. NTSTATUS
  298. CompBattMonitorIrpComplete (
  299. IN PDEVICE_OBJECT DeviceObject,
  300. IN PIRP Irp,
  301. IN PVOID Context
  302. );
  303. VOID
  304. CompBattRecalculateTag (
  305. IN PCOMPOSITE_BATTERY CompBatt
  306. );
  307. VOID
  308. CompBattChargeDischarge (
  309. IN PCOMPOSITE_BATTERY CompBatt
  310. );
  311. NTSTATUS
  312. CompBattGetDeviceObjectPointer(
  313. IN PUNICODE_STRING ObjectName,
  314. IN ACCESS_MASK DesiredAccess,
  315. OUT PFILE_OBJECT *FileObject,
  316. OUT PDEVICE_OBJECT *DeviceObject
  317. );