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.

551 lines
15 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. CmBattp.h
  5. Abstract:
  6. Control Method Battery Miniport Driver
  7. Author:
  8. Ron Mosgrove (Intel)
  9. Environment:
  10. Kernel mode
  11. Revision History:
  12. --*/
  13. #ifndef FAR
  14. #define FAR
  15. #endif
  16. #include <wdm.h>
  17. #include <wmilib.h>
  18. #include <batclass.h>
  19. #include <devioctl.h>
  20. #include <acpiioct.h>
  21. #define DIRECT_ACCESS DBG
  22. #if DIRECT_ACCESS
  23. #define CMB_DIRECT_IOCTL_ONLY 1
  24. #include "cmbdrect.h"
  25. #endif
  26. //
  27. // Debug
  28. //
  29. #define DEBUG DBG
  30. #if DEBUG
  31. extern ULONG CmBattDebug;
  32. #define CmBattPrint(l,m) if(l & CmBattDebug) DbgPrint m
  33. #else
  34. #define CmBattPrint(l,m)
  35. #endif
  36. #define CMBATT_LOW 0x00000001
  37. #define CMBATT_NOTE 0x00000002
  38. #define CMBATT_WARN 0x00000004
  39. #define CMBATT_ERROR_ONLY 0x00000008
  40. #define CMBATT_ERROR (CMBATT_ERROR_ONLY | CMBATT_WARN)
  41. #define CMBATT_POWER 0x00000010
  42. #define CMBATT_PNP 0x00000020
  43. #define CMBATT_CM_EXE 0x00000040
  44. #define CMBATT_DATA 0x00000100
  45. #define CMBATT_TRACE 0x00000200
  46. #define CMBATT_BIOS 0x00000400 // Show message to verify BIOS/HW functionality
  47. #define CMBATT_MINI 0x00000800 // Show message to verify miniport retun data
  48. extern UNICODE_STRING GlobalRegistryPath;
  49. extern PDEVICE_OBJECT AcAdapterPdo;
  50. extern KDPC CmBattWakeDpcObject;
  51. extern KTIMER CmBattWakeDpcTimerObject;
  52. //
  53. // Delay before notifications on wake = 0 seconds * 10,000,000 (100-ns/s)
  54. //
  55. #define WAKE_DPC_DELAY {0,0}
  56. //
  57. // Delay on switch to DC before showing estimated time.
  58. // 15 seconds * 10,000,000 (100-ns/s)
  59. //
  60. #define CM_ESTIMATED_TIME_DELAY 150000000
  61. extern LARGE_INTEGER CmBattWakeDpcDelay;
  62. #define MAX_DEVICE_NAME_LENGTH 100
  63. //
  64. // WaitWake registry key
  65. //
  66. extern PCWSTR WaitWakeEnableKey;
  67. //
  68. // Host Controller Device object extenstion
  69. //
  70. #define CM_MAX_DATA_SIZE 64
  71. #define CM_MAX_STRING_LENGTH 256
  72. //
  73. // Control Methods defined for the Control Method Batteries
  74. //
  75. #define CM_BIF_METHOD (ULONG) ('FIB_')
  76. #define CM_BST_METHOD (ULONG) ('TSB_')
  77. #define CM_BTP_METHOD (ULONG) ('PTB_')
  78. #define CM_PCL_METHOD (ULONG) ('LCP_')
  79. #define CM_PSR_METHOD (ULONG) ('RSP_')
  80. #define CM_STA_METHOD (ULONG) ('ATS_')
  81. #define CM_UID_METHOD (ULONG) ('DIU_')
  82. #define CM_OP_TYPE_READ 0
  83. #define CM_OP_TYPE_WRITE 1
  84. #define NUMBER_OF_BIF_ELEMENTS 13
  85. #define NUMBER_OF_BST_ELEMENTS 4
  86. //
  87. // Value to send to _BTP to clear the trip point.
  88. //
  89. #define CM_BATT_CLEAR_TRIP_POINT 0x00000000
  90. //
  91. // Special values retuned from control methods.
  92. //
  93. #define CM_UNKNOWN_VALUE 0xffffffff
  94. #define CM_MAX_VALUE 0x7fffffff
  95. //
  96. // STA control method return values
  97. //
  98. #define STA_DEVICE_PRESENT 0x10
  99. #define STA_DEVICE_FUNCTIONAL 0x80
  100. //
  101. // Control method battery device notification values
  102. //
  103. #define BATTERY_DEVICE_CHECK 0x00
  104. #define BATTERY_EJECT 0x01
  105. #define BATTERY_STATUS_CHANGE 0x80
  106. #define BATTERY_INFO_CHANGE 0x81
  107. //
  108. // This is the static data defined by the ACPI spec for the control method battery
  109. // It is returned by the _BIF control method
  110. //
  111. typedef struct {
  112. ULONG PowerUnit; // units used by interface 0:mWh or 1:mAh
  113. ULONG DesignCapacity; // Nominal capacity of a new battery
  114. ULONG LastFullChargeCapacity; // Predicted capacity when fully charged
  115. ULONG BatteryTechnology; // 0:Primary (not rechargable), 1:Secondary (rechargable)
  116. ULONG DesignVoltage; // Nominal voltage of a new battery
  117. ULONG DesignCapacityOfWarning; // OEM-designed battery warning capacity
  118. ULONG DesignCapacityOfLow; // OEM-designed battery low capacity
  119. ULONG BatteryCapacityGran_1; // capacity granularity between low and warning
  120. ULONG BatteryCapacityGran_2; // capacity granularity between warning and full
  121. UCHAR ModelNumber[CM_MAX_STRING_LENGTH];
  122. UCHAR SerialNumber[CM_MAX_STRING_LENGTH];
  123. UCHAR BatteryType[CM_MAX_STRING_LENGTH];
  124. UCHAR OEMInformation[CM_MAX_STRING_LENGTH];
  125. } CM_BIF_BAT_INFO, *PCM_BIF_BAT_INFO;
  126. //
  127. // Definitions for the PowerUnit field of CM_BIF_BAT_INFO
  128. //
  129. #define CM_BIF_UNITS_WATTS 0 // All units are in mWh
  130. #define CM_BIF_UNITS_AMPS 1 // All units are in mAh
  131. //
  132. // This is the battery status data defined by the ACPI spec for a control method battery
  133. // It is returned by the _BST control method
  134. //
  135. typedef struct {
  136. ULONG BatteryState; // Charging/Discharging/Critical
  137. ULONG PresentRate; // Present draw rate in units defined by PowerUnit
  138. // Unsigned value, direction is determined by BatteryState
  139. ULONG RemainingCapacity; // Estimated remaining capacity, units defined by PowerUnit
  140. ULONG PresentVoltage; // Present voltage across the battery terminals
  141. } CM_BST_BAT_INFO, *PCM_BST_BAT_INFO;
  142. //
  143. // Bit definitions for the BatteryState field of CM_BST_BAT_INFO
  144. //
  145. #define CM_BST_STATE_DISCHARGING 0x00000001 // Battery is discharging
  146. #define CM_BST_STATE_CHARGING 0x00000002 // Battery is charging
  147. #define CM_BST_STATE_CRITICAL 0x00000004 // Battery is critical
  148. //
  149. // Cached battery info
  150. //
  151. typedef struct {
  152. ULONG Tag; // Unique tag for this battery,
  153. ULONG ModelNumLen; // Length of ModelNumber string in StringBuffer
  154. PUCHAR ModelNum; // Ptr to ModelNumber in StringBuffer
  155. ULONG SerialNumLen; // Length of SerialNumber string in StringBuffer
  156. PUCHAR SerialNum; // Ptr to SerialNumber in StringBuffer
  157. ULONG OEMInfoLen; // Length of OEMInformation string in StringBuffer
  158. PUCHAR OEMInfo; // Ptr to OEMInformation in StringBuffer
  159. CM_BST_BAT_INFO Status; // Last Status read from battery
  160. CM_BIF_BAT_INFO StaticData; // Last valid data
  161. ULONG StaticDataTag; // Tag when static data was last read
  162. BATTERY_STATUS ApiStatus; // Status info, class driver structure
  163. BATTERY_INFORMATION ApiInfo; // Battery info, class driver structure
  164. ULONG ApiGranularity_1;
  165. ULONG ApiGranularity_2;
  166. BOOLEAN BtpExists; // Remeber if _BTP method exists
  167. } STATIC_BAT_INFO, *PSTATIC_BAT_INFO;
  168. typedef struct {
  169. ULONG Granularity;
  170. ULONG Capacity;
  171. } BATTERY_REMAINING_SCALE, *PBATTERY_REMAINING_SCALE;
  172. typedef struct {
  173. ULONG Setting; // The alarm value
  174. BOOLEAN Supported; // Set to false when _BTP fails
  175. // don't bother calling _BTP again
  176. } BAT_ALARM_INFO, *PBAT_ALARM_INFO;
  177. #define CM_ALARM_INVALID 0xffffffff;
  178. //
  179. // Types for the different FDOs created by this driver.
  180. //
  181. #define AC_ADAPTER_TYPE 0x00
  182. #define CM_BATTERY_TYPE 0x01
  183. //
  184. // Pagable device extension for control method battery
  185. //
  186. typedef struct _CM_BATT {
  187. ULONG Type; // Battery or AC Adapter
  188. PDEVICE_OBJECT DeviceObject; // Battery device object
  189. PDEVICE_OBJECT Fdo; // Functional Device Object
  190. PDEVICE_OBJECT Pdo; // Physical Device Object
  191. PDEVICE_OBJECT LowerDeviceObject; // Detected at AddDevice time
  192. FAST_MUTEX OpenCloseMutex;
  193. ULONG OpenCount; // Count open file handles to device
  194. PIRP WaitWakeIrp; // Pointer to Wait Wake Irp
  195. POWER_STATE WakeSupportedState;
  196. WMILIB_CONTEXT WmiLibContext;
  197. BOOLEAN WakeEnabled;
  198. BOOLEAN WantToRemove; // Syncronize device removal
  199. LONG InUseCount;
  200. KEVENT ReadyToRemove;
  201. ULONG DeviceNumber;
  202. PUNICODE_STRING DeviceName;
  203. ACPI_INTERFACE_STANDARD AcpiInterfaces;
  204. BOOLEAN Sleeping;
  205. UCHAR ActionRequired;
  206. //
  207. // All fields before this point are common between _CM_BATT and _AC_ADAPTER
  208. //
  209. PVOID Class; // Battery Class handle
  210. BOOLEAN IsStarted; // if non zero, the device is started
  211. BOOLEAN ReCheckSta;
  212. LONG CacheState; // 0 = invalid
  213. // 1 = updating values
  214. // 2 = valid
  215. //
  216. // Battery
  217. //
  218. ULONG TagCount; // Tag for next battery
  219. STATIC_BAT_INFO Info;
  220. BAT_ALARM_INFO Alarm;
  221. ULONGLONG DischargeTime; // Time battery started discharging.
  222. } CM_BATT, *PCM_BATT;
  223. //
  224. // Pagable device extension for AC Adapter
  225. //
  226. typedef struct _AC_ADAPTER {
  227. ULONG Type; // Battery or AC Adapter
  228. PDEVICE_OBJECT DeviceObject; // Battery device object
  229. PDEVICE_OBJECT Fdo; // Functional Device Object
  230. PDEVICE_OBJECT Pdo; // Physical Device Object
  231. PDEVICE_OBJECT LowerDeviceObject; // Detected at AddDevice time
  232. FAST_MUTEX OpenCloseMutex;
  233. ULONG OpenCount; // Count open file handles to device
  234. PIRP WaitWakeIrp; // Pointer to Wait Wake Irp
  235. POWER_STATE WakeSupportedState;
  236. WMILIB_CONTEXT WmiLibContext;
  237. BOOLEAN WakeEnabled;
  238. BOOLEAN WantToRemove; // Syncronize device removal
  239. LONG InUseCount;
  240. KEVENT ReadyToRemove;
  241. ULONG DeviceNumber;
  242. PUNICODE_STRING DeviceName;
  243. ACPI_INTERFACE_STANDARD AcpiInterfaces;
  244. BOOLEAN Sleeping;
  245. UCHAR ActionRequired;
  246. } AC_ADAPTER, *PAC_ADAPTER;
  247. // Action required (AR) flags
  248. #define CMBATT_AR_NO_ACTION 0
  249. #define CMBATT_AR_NOTIFY 1
  250. #define CMBATT_AR_INVALIDATE_CACHE 2
  251. #define CMBATT_AR_INVALIDATE_TAG 4
  252. //
  253. // Use the IoSkipCurrentIrpStackLocation routine because the we
  254. // don't need to change arguments, or a completion routine
  255. //
  256. #define CmBattCallLowerDriver(Status, DeviceObject, Irp) { \
  257. IoSkipCurrentIrpStackLocation(Irp); \
  258. Status = IoCallDriver(DeviceObject,Irp); \
  259. }
  260. #define GetTid() PsGetCurrentThread()
  261. //
  262. // Prototypes
  263. //
  264. VOID
  265. CmBattAlarm (
  266. IN PVOID Context,
  267. IN UCHAR Address,
  268. IN USHORT Data
  269. );
  270. NTSTATUS
  271. CmBattVerifyStaticInfo (
  272. IN PCM_BATT CmBatt,
  273. IN ULONG BatteryTag
  274. );
  275. NTSTATUS
  276. CmBattPnpDispatch(
  277. IN PDEVICE_OBJECT DeviceObject,
  278. IN PIRP Irp
  279. );
  280. NTSTATUS
  281. CmBattPowerDispatch(
  282. IN PDEVICE_OBJECT DeviceObject,
  283. IN PIRP Irp
  284. );
  285. NTSTATUS
  286. CmBattSystemControl(
  287. IN PDEVICE_OBJECT DeviceObject,
  288. IN PIRP Irp
  289. );
  290. NTSTATUS
  291. CmBattForwardRequest(
  292. IN PDEVICE_OBJECT DeviceObject,
  293. IN PIRP Irp
  294. );
  295. NTSTATUS
  296. CmBattAddDevice(
  297. IN PDRIVER_OBJECT DriverObject,
  298. IN PDEVICE_OBJECT Pdo
  299. );
  300. NTSTATUS
  301. CmBattQueryTag (
  302. IN PVOID Context,
  303. OUT PULONG BatteryTag
  304. );
  305. NTSTATUS
  306. CmBattSetStatusNotify (
  307. IN PVOID Context,
  308. IN ULONG BatteryTag,
  309. IN PBATTERY_NOTIFY BatteryNotify
  310. );
  311. NTSTATUS
  312. CmBattDisableStatusNotify (
  313. IN PVOID Context
  314. );
  315. NTSTATUS
  316. CmBattQueryStatus (
  317. IN PVOID Context,
  318. IN ULONG BatteryTag,
  319. OUT PBATTERY_STATUS BatteryStatus
  320. );
  321. NTSTATUS
  322. CmBattGetBifData(
  323. IN PCM_BATT CmBatt,
  324. OUT PCM_BIF_BAT_INFO BifBuf
  325. );
  326. NTSTATUS
  327. CmBattGetUniqueId(
  328. IN PDEVICE_OBJECT Pdo,
  329. OUT PULONG UniqueId
  330. );
  331. NTSTATUS
  332. CmBattGetStaData(
  333. IN PDEVICE_OBJECT Pdo,
  334. OUT PULONG ReturnStatus
  335. );
  336. NTSTATUS
  337. CmBattGetPsrData(
  338. IN PDEVICE_OBJECT Pdo,
  339. OUT PULONG ReturnStatus
  340. );
  341. NTSTATUS
  342. CmBattIoCompletion(
  343. IN PDEVICE_OBJECT DeviceObject,
  344. IN PIRP Irp,
  345. IN PKEVENT pdoIoCompletedEvent
  346. );
  347. NTSTATUS
  348. CmBattSetTripPpoint(
  349. IN PCM_BATT CmBatt,
  350. IN ULONG TripPoint
  351. );
  352. NTSTATUS
  353. CmBattGetBstData(
  354. IN PCM_BATT CmBatt,
  355. OUT PCM_BST_BAT_INFO BstBuf
  356. );
  357. NTSTATUS
  358. GetDwordElement (
  359. IN PACPI_METHOD_ARGUMENT Argument,
  360. OUT PULONG PDword
  361. );
  362. NTSTATUS
  363. GetStringElement (
  364. IN PACPI_METHOD_ARGUMENT Argument,
  365. OUT PUCHAR PBuffer
  366. );
  367. VOID
  368. CmBattPowerCallBack(
  369. IN PVOID CallBackContext,
  370. IN PVOID Argument1,
  371. IN PVOID Argument2
  372. );
  373. VOID
  374. CmBattWakeDpc (
  375. IN PKDPC Dpc,
  376. IN PVOID DefferedContext,
  377. IN PVOID SystemArgument1,
  378. IN PVOID SystemArgument2
  379. );
  380. VOID
  381. CmBattNotifyHandler (
  382. IN PVOID Context,
  383. IN ULONG NotifyValue
  384. );
  385. NTSTATUS
  386. CmBattQueryInformation (
  387. IN PVOID Context,
  388. IN ULONG BatteryTag,
  389. IN BATTERY_QUERY_INFORMATION_LEVEL Level,
  390. IN LONG AtRate OPTIONAL,
  391. OUT PVOID Buffer,
  392. IN ULONG BufferLength,
  393. OUT PULONG ReturnedLength
  394. );
  395. NTSTATUS
  396. CmBattSendDownStreamIrp(
  397. IN PDEVICE_OBJECT Pdo,
  398. IN ULONG Ioctl,
  399. IN PVOID InputBuffer,
  400. IN ULONG InputSize,
  401. IN PVOID OutputBuffer,
  402. IN ULONG OutputSize
  403. );
  404. NTSTATUS
  405. CmBattWaitWakeLoop(
  406. IN PDEVICE_OBJECT DeviceObject,
  407. IN UCHAR MinorFunction,
  408. IN POWER_STATE PowerState,
  409. IN PVOID Context,
  410. IN PIO_STATUS_BLOCK IoStatus
  411. );
  412. NTSTATUS
  413. CmBattWmiRegistration(
  414. PCM_BATT CmBatt
  415. );
  416. NTSTATUS
  417. CmBattWmiDeRegistration(
  418. PCM_BATT CmBatt
  419. );
  420. #ifndef _WIN32_WINNT
  421. VOID
  422. CmBattNotifyVPOWERDOfPowerChange (
  423. IN ULONG PowerSourceChange
  424. );
  425. #endif