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.

770 lines
21 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. CmbWmi.c
  5. Abstract:
  6. Wmi section for Control Method Battery Miniport Driver
  7. Author:
  8. Michael Hills
  9. Environment:
  10. Kernel mode
  11. Revision History:
  12. --*/
  13. #include "CmBattp.h"
  14. #include <initguid.h>
  15. #include <wdmguid.h>
  16. #include <wmistr.h>
  17. #include <wmilib.h>
  18. NTSTATUS
  19. CmBattSetWmiDataItem(
  20. IN PDEVICE_OBJECT DeviceObject,
  21. IN PIRP Irp,
  22. IN ULONG GuidIndex,
  23. IN ULONG InstanceIndex,
  24. IN ULONG DataItemId,
  25. IN ULONG BufferSize,
  26. IN PUCHAR Buffer
  27. );
  28. NTSTATUS
  29. CmBattSetWmiDataBlock(
  30. IN PDEVICE_OBJECT DeviceObject,
  31. IN PIRP Irp,
  32. IN ULONG GuidIndex,
  33. IN ULONG InstanceIndex,
  34. IN ULONG BufferSize,
  35. IN PUCHAR Buffer
  36. );
  37. NTSTATUS
  38. CmBattQueryWmiDataBlock(
  39. IN PDEVICE_OBJECT DeviceObject,
  40. IN PIRP Irp,
  41. IN ULONG GuidIndex,
  42. IN ULONG InstanceIndex,
  43. IN ULONG InstanceCount,
  44. IN OUT PULONG InstanceLengthArray,
  45. IN ULONG BufferAvail,
  46. OUT PUCHAR Buffer
  47. );
  48. NTSTATUS
  49. CmBattQueryWmiRegInfo(
  50. IN PDEVICE_OBJECT DeviceObject,
  51. OUT ULONG *RegFlags,
  52. OUT PUNICODE_STRING InstanceName,
  53. OUT PUNICODE_STRING *RegistryPath,
  54. OUT PUNICODE_STRING MofResourceName,
  55. OUT PDEVICE_OBJECT *Pdo
  56. );
  57. #if DEBUG
  58. PCHAR
  59. WMIMinorFunctionString (
  60. UCHAR MinorFunction
  61. );
  62. #endif
  63. #ifdef ALLOC_PRAGMA
  64. #pragma alloc_text(PAGE,CmBattWmiRegistration)
  65. #pragma alloc_text(PAGE,CmBattWmiDeRegistration)
  66. #pragma alloc_text(PAGE,CmBattSystemControl)
  67. #pragma alloc_text(PAGE,CmBattSetWmiDataItem)
  68. #pragma alloc_text(PAGE,CmBattSetWmiDataBlock)
  69. #pragma alloc_text(PAGE,CmBattQueryWmiDataBlock)
  70. #pragma alloc_text(PAGE,CmBattQueryWmiRegInfo)
  71. #endif
  72. //
  73. // WMI info
  74. //
  75. #define MOFRESOURCENAME L"BATTWMI"
  76. typedef struct _MSPower_DeviceWakeEnable
  77. {
  78. //
  79. BOOLEAN Enable;
  80. #define MSPower_DeviceWakeEnable_Enable_SIZE sizeof(BOOLEAN)
  81. #define MSPower_DeviceWakeEnable_Enable_ID 1
  82. } MSPower_DeviceWakeEnable, *PMSPower_DeviceWakeEnable;
  83. WMIGUIDREGINFO CmBattWmiGuidList[1] =
  84. {
  85. {
  86. &GUID_POWER_DEVICE_WAKE_ENABLE, 1, 0
  87. }
  88. };
  89. NTSTATUS
  90. CmBattSystemControl(
  91. IN PDEVICE_OBJECT DeviceObject,
  92. IN PIRP Irp
  93. )
  94. /*++
  95. Routine Description:
  96. This routine passes the request down the stack
  97. Arguments:
  98. DeviceObject - The target
  99. Irp - The request
  100. Return Value:
  101. NTSTATUS
  102. --*/
  103. {
  104. NTSTATUS status = STATUS_NOT_SUPPORTED;
  105. PCM_BATT CmBatt;
  106. PIO_STACK_LOCATION stack;
  107. SYSCTL_IRP_DISPOSITION disposition = IrpForward;
  108. PAGED_CODE();
  109. stack = IoGetCurrentIrpStackLocation (Irp);
  110. CmBattPrint((CMBATT_TRACE), ("CmBatt: SystemControl: %s\n",
  111. WMIMinorFunctionString(stack->MinorFunction)));
  112. CmBatt = (PCM_BATT) DeviceObject->DeviceExtension;
  113. //
  114. // Aquire remove lock
  115. //
  116. InterlockedIncrement (&CmBatt->InUseCount);
  117. if (CmBatt->WantToRemove == TRUE) {
  118. if (0 == InterlockedDecrement(&CmBatt->InUseCount)) {
  119. KeSetEvent (&CmBatt->ReadyToRemove, IO_NO_INCREMENT, FALSE);
  120. }
  121. status = STATUS_DEVICE_REMOVED;
  122. Irp->IoStatus.Status = status;
  123. IoCompleteRequest (Irp, IO_NO_INCREMENT);
  124. return status;
  125. }
  126. if (CmBatt->Type == CM_BATTERY_TYPE) {
  127. status = BatteryClassSystemControl(CmBatt->Class,
  128. &CmBatt->WmiLibContext,
  129. DeviceObject,
  130. Irp,
  131. &disposition);
  132. } else {
  133. status = WmiSystemControl(&CmBatt->WmiLibContext,
  134. DeviceObject,
  135. Irp,
  136. &disposition);
  137. }
  138. switch(disposition)
  139. {
  140. case IrpProcessed:
  141. {
  142. //
  143. // This irp has been processed and may be completed or pending.
  144. CmBattPrint((CMBATT_TRACE), ("CmBatt: SystemControl: Irp Processed\n"));
  145. break;
  146. }
  147. case IrpNotCompleted:
  148. {
  149. //
  150. // This irp has not been completed, but has been fully processed.
  151. // we will complete it now
  152. CmBattPrint((CMBATT_TRACE), ("CmBatt: SystemControl: Irp Not Completed.\n"));
  153. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  154. break;
  155. }
  156. case IrpForward:
  157. case IrpNotWmi:
  158. {
  159. //
  160. // This irp is either not a WMI irp or is a WMI irp targeted
  161. // at a device lower in the stack.
  162. CmBattPrint((CMBATT_TRACE), ("CmBatt: SystemControl: Irp Forward.\n"));
  163. IoSkipCurrentIrpStackLocation (Irp);
  164. status = IoCallDriver (CmBatt->LowerDeviceObject, Irp);
  165. break;
  166. }
  167. default:
  168. {
  169. //
  170. // We really should never get here, but if we do just forward....
  171. ASSERT(FALSE);
  172. IoSkipCurrentIrpStackLocation (Irp);
  173. status = IoCallDriver (CmBatt->LowerDeviceObject, Irp);
  174. break;
  175. }
  176. }
  177. //
  178. // Release Removal Lock
  179. //
  180. if (0 == InterlockedDecrement(&CmBatt->InUseCount)) {
  181. KeSetEvent (&CmBatt->ReadyToRemove, IO_NO_INCREMENT, FALSE);
  182. }
  183. return status;
  184. }
  185. NTSTATUS
  186. CmBattWmiRegistration(
  187. PCM_BATT CmBatt
  188. )
  189. /*++
  190. Routine Description
  191. Registers with WMI as a data provider for this
  192. instance of the device
  193. --*/
  194. {
  195. NTSTATUS status;
  196. PAGED_CODE();
  197. CmBatt->WmiLibContext.GuidCount = sizeof (CmBattWmiGuidList) /
  198. sizeof (WMIGUIDREGINFO);
  199. ASSERT (1 == CmBatt->WmiLibContext.GuidCount);
  200. CmBatt->WmiLibContext.GuidList = CmBattWmiGuidList;
  201. CmBatt->WmiLibContext.QueryWmiRegInfo = CmBattQueryWmiRegInfo;
  202. CmBatt->WmiLibContext.QueryWmiDataBlock = CmBattQueryWmiDataBlock;
  203. CmBatt->WmiLibContext.SetWmiDataBlock = CmBattSetWmiDataBlock;
  204. CmBatt->WmiLibContext.SetWmiDataItem = CmBattSetWmiDataItem;
  205. CmBatt->WmiLibContext.ExecuteWmiMethod = NULL;
  206. CmBatt->WmiLibContext.WmiFunctionControl = NULL;
  207. //
  208. // Register with WMI
  209. //
  210. status = IoWMIRegistrationControl(CmBatt->Fdo,
  211. WMIREG_ACTION_REGISTER
  212. );
  213. return status;
  214. }
  215. NTSTATUS
  216. CmBattWmiDeRegistration(
  217. PCM_BATT CmBatt
  218. )
  219. /*++
  220. Routine Description
  221. Inform WMI to remove this DeviceObject from its
  222. list of providers. This function also
  223. decrements the reference count of the deviceobject.
  224. --*/
  225. {
  226. PAGED_CODE();
  227. return IoWMIRegistrationControl(CmBatt->Fdo,
  228. WMIREG_ACTION_DEREGISTER
  229. );
  230. }
  231. //
  232. // WMI System Call back functions
  233. //
  234. NTSTATUS
  235. CmBattSetWmiDataItem(
  236. IN PDEVICE_OBJECT DeviceObject,
  237. IN PIRP Irp,
  238. IN ULONG GuidIndex,
  239. IN ULONG InstanceIndex,
  240. IN ULONG DataItemId,
  241. IN ULONG BufferSize,
  242. IN PUCHAR Buffer
  243. )
  244. /*++
  245. Routine Description:
  246. This routine is a callback into the driver to set for the contents of
  247. a data block. When the driver has finished filling the data block it
  248. must call WmiCompleteRequest to complete the irp. The driver can
  249. return STATUS_PENDING if the irp cannot be completed immediately.
  250. Arguments:
  251. DeviceObject is the device whose data block is being queried
  252. Irp is the Irp that makes this request
  253. GuidIndex is the index into the list of guids provided when the
  254. device registered
  255. InstanceIndex is the index that denotes which instance of the data block
  256. is being queried.
  257. DataItemId has the id of the data item being set
  258. BufferSize has the size of the data item passed
  259. Buffer has the new values for the data item
  260. Return Value:
  261. status
  262. --*/
  263. {
  264. PCM_BATT CmBatt = (PCM_BATT) DeviceObject->DeviceExtension;
  265. NTSTATUS status;
  266. HANDLE devInstRegKey;
  267. UNICODE_STRING valueName;
  268. ULONG tmp;
  269. PAGED_CODE();
  270. CmBattPrint ((CMBATT_TRACE), ("Entered CmBattSetWmiDataItem\n"));
  271. switch(GuidIndex) {
  272. case 0:
  273. if(DataItemId == 0) {
  274. if (CmBatt->WakeEnabled != *((PBOOLEAN)Buffer)) {
  275. CmBatt->WakeEnabled = *((PBOOLEAN)Buffer);
  276. //
  277. // Save setting in registry
  278. //
  279. if ((NT_SUCCESS(IoOpenDeviceRegistryKey (CmBatt->Pdo,
  280. PLUGPLAY_REGKEY_DEVICE,
  281. STANDARD_RIGHTS_ALL,
  282. &devInstRegKey)))) {
  283. RtlInitUnicodeString (&valueName, WaitWakeEnableKey);
  284. tmp = (ULONG) CmBatt->WakeEnabled;
  285. ZwSetValueKey (devInstRegKey,
  286. &valueName,
  287. 0,
  288. REG_DWORD,
  289. &tmp,
  290. sizeof(tmp));
  291. ZwClose (devInstRegKey);
  292. }
  293. if (CmBatt->WakeEnabled) {
  294. if (CmBatt->WakeSupportedState.SystemState == PowerSystemUnspecified) {
  295. CmBatt->WakeEnabled = FALSE;
  296. status = STATUS_UNSUCCESSFUL;
  297. } else if (!CmBatt->WaitWakeIrp) {
  298. status = PoRequestPowerIrp(
  299. CmBatt->DeviceObject,
  300. IRP_MN_WAIT_WAKE,
  301. CmBatt->WakeSupportedState,
  302. CmBattWaitWakeLoop,
  303. NULL,
  304. &(CmBatt->WaitWakeIrp)
  305. );
  306. CmBattPrint (CMBATT_PNP, ("CmBattSetWmiDataItem: wait/Wake irp sent.\n"));
  307. }
  308. } else {
  309. if (CmBatt->WaitWakeIrp) {
  310. status = IoCancelIrp(CmBatt->WaitWakeIrp);
  311. CmBattPrint (CMBATT_PNP, ("CmBattSetWmiDataItem: Canceled wait/Wake irp.\n"));
  312. }
  313. }
  314. }
  315. status = STATUS_SUCCESS;
  316. } else {
  317. status = STATUS_WMI_READ_ONLY;
  318. }
  319. break;
  320. default:
  321. status = STATUS_WMI_GUID_NOT_FOUND;
  322. }
  323. status = WmiCompleteRequest( DeviceObject,
  324. Irp,
  325. status,
  326. 0,
  327. IO_NO_INCREMENT);
  328. return status;
  329. }
  330. NTSTATUS
  331. CmBattSetWmiDataBlock(
  332. IN PDEVICE_OBJECT DeviceObject,
  333. IN PIRP Irp,
  334. IN ULONG GuidIndex,
  335. IN ULONG InstanceIndex,
  336. IN ULONG BufferSize,
  337. IN PUCHAR Buffer
  338. )
  339. /*++
  340. Routine Description:
  341. This routine is a callback into the driver to set the contents of
  342. a data block. When the driver has finished filling the data block it
  343. must call WmiCompleteRequest to complete the irp. The driver can
  344. return STATUS_PENDING if the irp cannot be completed immediately.
  345. Arguments:
  346. DeviceObject is the device whose data block is being queried
  347. Irp is the Irp that makes this request
  348. GuidIndex is the index into the list of guids provided when the
  349. device registered
  350. InstanceIndex is the index that denotes which instance of the data block
  351. is being queried.
  352. BufferSize has the size of the data block passed
  353. Buffer has the new values for the data block
  354. Return Value:
  355. status
  356. --*/
  357. {
  358. PCM_BATT CmBatt = (PCM_BATT) DeviceObject->DeviceExtension;
  359. NTSTATUS status;
  360. HANDLE devInstRegKey;
  361. UNICODE_STRING valueName;
  362. ULONG tmp;
  363. PAGED_CODE();
  364. CmBattPrint ((CMBATT_TRACE), ("Entered CmBattSetWmiDataBlock\n"));
  365. switch(GuidIndex) {
  366. case 0:
  367. //
  368. // We will update only writable elements.
  369. //
  370. if (CmBatt->WakeEnabled != *((PBOOLEAN)Buffer)) {
  371. CmBatt->WakeEnabled = *((PBOOLEAN)Buffer);
  372. //
  373. // Save setting in registry
  374. //
  375. if ((NT_SUCCESS(IoOpenDeviceRegistryKey (CmBatt->Pdo,
  376. PLUGPLAY_REGKEY_DEVICE,
  377. STANDARD_RIGHTS_ALL,
  378. &devInstRegKey)))) {
  379. RtlInitUnicodeString (&valueName, WaitWakeEnableKey);
  380. tmp = (ULONG) CmBatt->WakeEnabled;
  381. ZwSetValueKey (devInstRegKey,
  382. &valueName,
  383. 0,
  384. REG_DWORD,
  385. &tmp,
  386. sizeof(tmp));
  387. ZwClose (devInstRegKey);
  388. }
  389. if (CmBatt->WakeEnabled) {
  390. if (CmBatt->WakeSupportedState.SystemState == PowerSystemUnspecified) {
  391. CmBatt->WakeEnabled = FALSE;
  392. status = STATUS_UNSUCCESSFUL;
  393. } else if (!CmBatt->WaitWakeIrp) {
  394. status = PoRequestPowerIrp(
  395. CmBatt->DeviceObject,
  396. IRP_MN_WAIT_WAKE,
  397. CmBatt->WakeSupportedState,
  398. CmBattWaitWakeLoop,
  399. NULL,
  400. &(CmBatt->WaitWakeIrp)
  401. );
  402. CmBattPrint (CMBATT_PNP, ("CmBattSetWmiDataItem: wait/Wake irp sent.\n"));
  403. }
  404. } else {
  405. if (CmBatt->WaitWakeIrp) {
  406. status = IoCancelIrp(CmBatt->WaitWakeIrp);
  407. CmBattPrint (CMBATT_PNP, ("CmBattSetWmiDataItem: Canceled wait/Wake irp.\n"));
  408. }
  409. }
  410. }
  411. status = STATUS_SUCCESS;
  412. break;
  413. default:
  414. status = STATUS_WMI_GUID_NOT_FOUND;
  415. }
  416. status = WmiCompleteRequest( DeviceObject,
  417. Irp,
  418. status,
  419. 0,
  420. IO_NO_INCREMENT);
  421. return(status);
  422. }
  423. NTSTATUS
  424. CmBattQueryWmiDataBlock(
  425. IN PDEVICE_OBJECT DeviceObject,
  426. IN PIRP Irp,
  427. IN ULONG GuidIndex,
  428. IN ULONG InstanceIndex,
  429. IN ULONG InstanceCount,
  430. IN OUT PULONG InstanceLengthArray,
  431. IN ULONG OutBufferSize,
  432. OUT PUCHAR Buffer
  433. )
  434. /*++
  435. Routine Description:
  436. This routine is a callback into the driver to query for the contents of
  437. a data block. When the driver has finished filling the data block it
  438. must call WmiCompleteRequest to complete the irp. The driver can
  439. return STATUS_PENDING if the irp cannot be completed immediately.
  440. Arguments:
  441. DeviceObject is the device whose data block is being queried
  442. Irp is the Irp that makes this request
  443. GuidIndex is the index into the list of guids provided when the
  444. device registered
  445. InstanceIndex is the index that denotes which instance of the data block
  446. is being queried.
  447. InstanceCount is the number of instances expected to be returned for
  448. the data block.
  449. InstanceLengthArray is a pointer to an array of ULONG that returns the
  450. lengths of each instance of the data block. If this is NULL then
  451. there was not enough space in the output buffer to fulfill the request
  452. so the irp should be completed with the buffer needed.
  453. BufferAvail on has the maximum size available to write the data
  454. block.
  455. Buffer on return is filled with the returned data block
  456. Return Value:
  457. status
  458. --*/
  459. {
  460. PCM_BATT CmBatt = (PCM_BATT) DeviceObject->DeviceExtension;
  461. NTSTATUS status;
  462. ULONG size = 0;
  463. PAGED_CODE();
  464. CmBattPrint ((CMBATT_TRACE), ("Entered CmBattQueryWmiDataBlock\n"));
  465. //
  466. // Only ever registers 1 instance per guid
  467. ASSERT((InstanceIndex == 0) &&
  468. (InstanceCount == 1));
  469. if (CmBatt->Type == CM_BATTERY_TYPE) {
  470. status = BatteryClassQueryWmiDataBlock(
  471. CmBatt->Class,
  472. DeviceObject,
  473. Irp,
  474. GuidIndex,
  475. InstanceLengthArray,
  476. OutBufferSize,
  477. Buffer);
  478. if (status != STATUS_WMI_GUID_NOT_FOUND) {
  479. CmBattPrint ((CMBATT_TRACE), ("CmBattQueryWmiDataBlock: Handled by Battery Class.\n"));
  480. return status;
  481. }
  482. }
  483. CmBattPrint ((CMBATT_TRACE), ("CmBattQueryWmiDataBlock: Handling.\n"));
  484. switch (GuidIndex) {
  485. case 0:
  486. if (CmBatt->WakeSupportedState.SystemState != PowerSystemUnspecified) {
  487. size = sizeof (BOOLEAN);
  488. if (OutBufferSize < size ) {
  489. status = STATUS_BUFFER_TOO_SMALL;
  490. break;
  491. }
  492. (BOOLEAN) (* Buffer) = CmBatt->WakeEnabled;
  493. *InstanceLengthArray = size;
  494. status = STATUS_SUCCESS;
  495. } else {
  496. status = STATUS_WMI_GUID_NOT_FOUND;
  497. }
  498. break;
  499. default:
  500. status = STATUS_WMI_GUID_NOT_FOUND;
  501. }
  502. status = WmiCompleteRequest( DeviceObject,
  503. Irp,
  504. status,
  505. size,
  506. IO_NO_INCREMENT);
  507. return status;
  508. }
  509. NTSTATUS
  510. CmBattQueryWmiRegInfo(
  511. IN PDEVICE_OBJECT DeviceObject,
  512. OUT ULONG *RegFlags,
  513. OUT PUNICODE_STRING InstanceName,
  514. OUT PUNICODE_STRING *RegistryPath,
  515. OUT PUNICODE_STRING MofResourceName,
  516. OUT PDEVICE_OBJECT *Pdo
  517. )
  518. /*++
  519. Routine Description:
  520. This routine is a callback into the driver to retrieve the list of
  521. guids or data blocks that the driver wants to register with WMI. This
  522. routine may not pend or block. Driver should NOT call
  523. WmiCompleteRequest.
  524. Arguments:
  525. DeviceObject is the device whose data block is being queried
  526. *RegFlags returns with a set of flags that describe the guids being
  527. registered for this device. If the device wants enable and disable
  528. collection callbacks before receiving queries for the registered
  529. guids then it should return the WMIREG_FLAG_EXPENSIVE flag. Also the
  530. returned flags may specify WMIREG_FLAG_INSTANCE_PDO in which case
  531. the instance name is determined from the PDO associated with the
  532. device object. Note that the PDO must have an associated devnode. If
  533. WMIREG_FLAG_INSTANCE_PDO is not set then Name must return a unique
  534. name for the device.
  535. InstanceName returns with the instance name for the guids if
  536. WMIREG_FLAG_INSTANCE_PDO is not set in the returned *RegFlags. The
  537. caller will call ExFreePool with the buffer returned.
  538. *RegistryPath returns with the registry path of the driver
  539. *MofResourceName returns with the name of the MOF resource attached to
  540. the binary file. If the driver does not have a mof resource attached
  541. then this can be returned as NULL.
  542. *Pdo returns with the device object for the PDO associated with this
  543. device if the WMIREG_FLAG_INSTANCE_PDO flag is returned in
  544. *RegFlags.
  545. Return Value:
  546. status
  547. --*/
  548. {
  549. PCM_BATT CmBatt = DeviceObject->DeviceExtension;
  550. PAGED_CODE();
  551. CmBattPrint ((CMBATT_TRACE), ("CmBatt: Entered CmBattQueryWmiRegInfo\n"));
  552. *RegFlags = WMIREG_FLAG_INSTANCE_PDO;
  553. *RegistryPath = &GlobalRegistryPath;
  554. *Pdo = CmBatt->Pdo;
  555. // RtlInitUnicodeString(MofResourceName, MOFRESOURCENAME);
  556. return STATUS_SUCCESS;
  557. }
  558. #if DEBUG
  559. PCHAR
  560. WMIMinorFunctionString (
  561. UCHAR MinorFunction
  562. )
  563. {
  564. switch (MinorFunction)
  565. {
  566. case IRP_MN_CHANGE_SINGLE_INSTANCE:
  567. return "IRP_MN_CHANGE_SINGLE_INSTANCE";
  568. case IRP_MN_CHANGE_SINGLE_ITEM:
  569. return "IRP_MN_CHANGE_SINGLE_ITEM";
  570. case IRP_MN_DISABLE_COLLECTION:
  571. return "IRP_MN_DISABLE_COLLECTION";
  572. case IRP_MN_DISABLE_EVENTS:
  573. return "IRP_MN_DISABLE_EVENTS";
  574. case IRP_MN_ENABLE_COLLECTION:
  575. return "IRP_MN_ENABLE_COLLECTION";
  576. case IRP_MN_ENABLE_EVENTS:
  577. return "IRP_MN_ENABLE_EVENTS";
  578. case IRP_MN_EXECUTE_METHOD:
  579. return "IRP_MN_EXECUTE_METHOD";
  580. case IRP_MN_QUERY_ALL_DATA:
  581. return "IRP_MN_QUERY_ALL_DATA";
  582. case IRP_MN_QUERY_SINGLE_INSTANCE:
  583. return "IRP_MN_QUERY_SINGLE_INSTANCE";
  584. case IRP_MN_REGINFO:
  585. return "IRP_MN_REGINFO";
  586. default:
  587. return "IRP_MN_?????";
  588. }
  589. }
  590. #endif