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.

574 lines
15 KiB

  1. /*++
  2. Copyright (C) 1998-99 Microsoft Corporation
  3. Module Name:
  4. wmi.c
  5. Abstract:
  6. --*/
  7. #if defined (IDEPORT_WMI_SUPPORT)
  8. #include <initguid.h>
  9. #include "ideport.h"
  10. #include <wmistr.h>
  11. //
  12. // Instantiate the GUIDs define in ntddscsi.h in this module.
  13. //
  14. #include <devguid.h>
  15. #ifdef ALLOC_PRAGMA
  16. #pragma alloc_text(PAGE, IdePortWmiRegister)
  17. #pragma alloc_text(PAGE, IdePortWmiDeregister)
  18. #pragma alloc_text(PAGE, IdePortWmiSystemControl)
  19. #pragma alloc_text(PAGE, DeviceQueryWmiDataBlock)
  20. #pragma alloc_text(PAGE, DeviceSetWmiDataBlock)
  21. #pragma alloc_text(PAGE, DeviceSetWmiDataItem)
  22. #pragma alloc_text(PAGE, DeviceQueryWmiRegInfo)
  23. #endif
  24. typedef enum {
  25. WmiScsiAddress = 0
  26. } WMI_DATA_BLOCK_TYPE;
  27. #define NUMBER_OF_WMI_GUID 1
  28. WMIGUIDREGINFO IdePortWmiGuidList[NUMBER_OF_WMI_GUID];
  29. VOID
  30. IdePortWmiInit (VOID)
  31. {
  32. PAGED_CODE();
  33. IdePortWmiGuidList[WmiScsiAddress].Guid = &WmiScsiAddressGuid;
  34. IdePortWmiGuidList[WmiScsiAddress].InstanceCount = 1;
  35. IdePortWmiGuidList[WmiScsiAddress].Flags = 0;
  36. return;
  37. }
  38. NTSTATUS
  39. IdePortWmiRegister(
  40. PDEVICE_EXTENSION_HEADER DoCommonExtension
  41. )
  42. {
  43. NTSTATUS status;
  44. PAGED_CODE();
  45. ASSERT(DoCommonExtension->AttacheePdo == NULL);
  46. DoCommonExtension->WmiLibInfo.GuidCount = NUMBER_OF_WMI_GUID;
  47. DoCommonExtension->WmiLibInfo.GuidList = IdePortWmiGuidList;
  48. DoCommonExtension->WmiLibInfo.QueryWmiDataBlock = DeviceQueryWmiDataBlock;
  49. DoCommonExtension->WmiLibInfo.QueryWmiRegInfo = DeviceQueryWmiRegInfo;
  50. DoCommonExtension->WmiLibInfo.SetWmiDataBlock = DeviceSetWmiDataBlock;
  51. DoCommonExtension->WmiLibInfo.SetWmiDataItem = DeviceSetWmiDataItem;
  52. DoCommonExtension->WmiLibInfo.ExecuteWmiMethod = NULL;
  53. DoCommonExtension->WmiLibInfo.WmiFunctionControl = NULL;
  54. status = IoWMIRegistrationControl(
  55. DoCommonExtension->DeviceObject,
  56. WMIREG_ACTION_REGISTER
  57. );
  58. if (!NT_SUCCESS(status)) {
  59. DebugPrint((
  60. DBG_ALWAYS,
  61. "IdePortWmiRegister: IoWMIRegistrationControl(%x, WMI_ACTION_REGISTER) failed\n",
  62. DoCommonExtension->DeviceObject
  63. ));
  64. }
  65. return status;
  66. }
  67. NTSTATUS
  68. IdePortWmiDeregister(
  69. PDEVICE_EXTENSION_HEADER DoCommonExtension
  70. )
  71. {
  72. NTSTATUS status;
  73. PAGED_CODE();
  74. status = IoWMIRegistrationControl(
  75. DoCommonExtension->DeviceObject,
  76. WMIREG_ACTION_DEREGISTER
  77. );
  78. if (!NT_SUCCESS(status)) {
  79. DebugPrint((
  80. DBG_ALWAYS,
  81. "IdePortWmiDeregister: IoWMIRegistrationControl(%x, WMIREG_ACTION_DEREGISTER) failed\n",
  82. DoCommonExtension->DeviceObject
  83. ));
  84. }
  85. return status;
  86. }
  87. NTSTATUS
  88. IdePortWmiSystemControl(
  89. IN PDEVICE_OBJECT DeviceObject,
  90. IN PIRP Irp
  91. )
  92. /*++
  93. Routine Description
  94. We have just received a System Control IRP.
  95. Assume that this is a WMI IRP and call into the WMI system library and let
  96. it handle this IRP for us.
  97. --*/
  98. {
  99. PPDO_EXTENSION pdoExtension;
  100. SYSCTL_IRP_DISPOSITION disposition;
  101. NTSTATUS status;
  102. PAGED_CODE();
  103. pdoExtension = RefPdoWithTag (DeviceObject, FALSE, (PVOID) ~(ULONG_PTR)Irp);
  104. if (pdoExtension) {
  105. status = WmiSystemControl( &pdoExtension->WmiLibInfo,
  106. DeviceObject,
  107. Irp,
  108. &disposition);
  109. switch(disposition)
  110. {
  111. case IrpProcessed:
  112. {
  113. //
  114. // This irp has been processed and may be completed
  115. // or pending.
  116. break;
  117. }
  118. case IrpNotCompleted:
  119. {
  120. //
  121. // This irp has not been completed, but has been fully
  122. // processed. We will complete it now
  123. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  124. break;
  125. }
  126. case IrpForward:
  127. case IrpNotWmi:
  128. {
  129. //Fail the irp
  130. Irp->IoStatus.Status = status = STATUS_NOT_SUPPORTED;
  131. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  132. break;
  133. }
  134. default:
  135. {
  136. //
  137. // We really should never get here, but if we do just fail the irp
  138. ASSERT(FALSE);
  139. Irp->IoStatus.Status = status = STATUS_NOT_SUPPORTED;
  140. IoCompleteRequest(Irp, IO_NO_INCREMENT);
  141. break;
  142. }
  143. }
  144. UnrefPdoWithTag (
  145. pdoExtension,
  146. (PVOID) ~(ULONG_PTR)Irp
  147. );
  148. } else {
  149. ASSERT(!"got WMI irp after the device is removed!\n");
  150. Irp->IoStatus.Status = status = STATUS_UNSUCCESSFUL;
  151. IoCompleteRequest( Irp, IO_NO_INCREMENT );
  152. }
  153. return status;
  154. }
  155. NTSTATUS
  156. DeviceQueryWmiDataBlock(
  157. IN PDEVICE_OBJECT DeviceObject,
  158. IN PIRP Irp,
  159. IN ULONG GuidIndex,
  160. IN ULONG InstanceIndex,
  161. IN ULONG InstanceCount,
  162. IN OUT PULONG InstanceLengthArray,
  163. IN ULONG OutBufferSize,
  164. OUT PUCHAR Buffer
  165. )
  166. /*++
  167. Routine Description:
  168. This routine is a callback into the driver to query for the contents of
  169. a data block. When the driver has finished filling the data block it
  170. must call ClassWmiCompleteRequest to complete the irp. The driver can
  171. return STATUS_PENDING if the irp cannot be completed immediately.
  172. Arguments:
  173. DeviceObject is the device whose data block is being queried
  174. Irp is the Irp that makes this request
  175. GuidIndex is the index into the list of guids provided when the
  176. device registered
  177. InstanceIndex is the index that denotes which instance of the data block
  178. is being queried.
  179. InstanceCount is the number of instnaces expected to be returned for
  180. the data block.
  181. InstanceLengthArray is a pointer to an array of ULONG that returns the
  182. lengths of each instance of the data block. If this is NULL then
  183. there was not enough space in the output buffer to fufill the request
  184. so the irp should be completed with the buffer needed.
  185. BufferAvail on has the maximum size available to write the data
  186. block.
  187. Buffer on return is filled with the returned data block
  188. Return Value:
  189. status
  190. --*/
  191. {
  192. PPDO_EXTENSION pdoExtension;
  193. NTSTATUS status;
  194. ULONG numBytesReturned = sizeof(WMI_SCSI_ADDRESS);
  195. PAGED_CODE();
  196. ASSERT((InstanceIndex == 0) && (InstanceCount == 1));
  197. pdoExtension = RefPdoWithTag (DeviceObject, FALSE, Irp);
  198. if (!pdoExtension) {
  199. ASSERT(!"got WMI irp after the device is removed!\n");
  200. Irp->IoStatus.Status = status = STATUS_UNSUCCESSFUL;
  201. IoCompleteRequest( Irp, IO_NO_INCREMENT );
  202. return status;
  203. }
  204. switch (GuidIndex) {
  205. case WmiScsiAddress: {
  206. PWMI_SCSI_ADDRESS scsiAddress;
  207. if (OutBufferSize < sizeof(WMI_SCSI_ADDRESS)) {
  208. status = STATUS_BUFFER_TOO_SMALL;
  209. } else {
  210. scsiAddress = (PWMI_SCSI_ADDRESS) Buffer;
  211. scsiAddress->Bus = pdoExtension->PathId;
  212. scsiAddress->Target = pdoExtension->TargetId;
  213. scsiAddress->Lun = pdoExtension->Lun;
  214. *InstanceLengthArray = sizeof(WMI_SCSI_ADDRESS);
  215. status = STATUS_SUCCESS;
  216. }
  217. break;
  218. }
  219. default:
  220. status = STATUS_WMI_GUID_NOT_FOUND;
  221. break;
  222. }
  223. status = WmiCompleteRequest( DeviceObject,
  224. Irp,
  225. status,
  226. numBytesReturned,
  227. IO_NO_INCREMENT
  228. );
  229. UnrefPdoWithTag (
  230. pdoExtension,
  231. Irp
  232. );
  233. return status;
  234. }
  235. NTSTATUS
  236. DeviceQueryWmiRegInfo(
  237. IN PDEVICE_OBJECT DeviceObject,
  238. OUT PULONG RegFlags,
  239. OUT PUNICODE_STRING InstanceName,
  240. OUT PUNICODE_STRING *RegistryPath,
  241. OUT PUNICODE_STRING MofResourceName,
  242. OUT PDEVICE_OBJECT *Pdo
  243. )
  244. /*++
  245. Routine Description:
  246. This routine is a callback into the driver to retrieve the list of
  247. guids or data blocks that the driver wants to register with WMI. This
  248. routine may not pend or block. Driver should NOT call
  249. ClassWmiCompleteRequest.
  250. Arguments:
  251. DeviceObject is the device whose data block is being queried
  252. *RegFlags returns with a set of flags that describe the guids being
  253. registered for this device. If the device wants enable and disable
  254. collection callbacks before receiving queries for the registered
  255. guids then it should return the WMIREG_FLAG_EXPENSIVE flag. Also the
  256. returned flags may specify WMIREG_FLAG_INSTANCE_PDO in which case
  257. the instance name is determined from the PDO associated with the
  258. device object. Note that the PDO must have an associated devnode. If
  259. WMIREG_FLAG_INSTANCE_PDO is not set then Name must return a unique
  260. name for the device.
  261. InstanceName returns with the instance name for the guids if
  262. WMIREG_FLAG_INSTANCE_PDO is not set in the returned *RegFlags. The
  263. caller will call ExFreePool with the buffer returned.
  264. *RegistryPath returns with the registry path of the driver
  265. *MofResourceName returns with the name of the MOF resource attached to
  266. the binary file. If the driver does not have a mof resource attached
  267. then this can be returned as NULL.
  268. *Pdo returns with the device object for the PDO associated with this
  269. device if the WMIREG_FLAG_INSTANCE_PDO flag is retured in
  270. *RegFlags.
  271. Return Value:
  272. status
  273. --*/
  274. {
  275. PIDEDRIVER_EXTENSION ideDriverExtension;
  276. PPDO_EXTENSION pdoExtension;
  277. NTSTATUS status;
  278. PAGED_CODE();
  279. pdoExtension = RefPdoWithTag (DeviceObject, FALSE, DeviceQueryWmiRegInfo);
  280. if (!pdoExtension) {
  281. ASSERT(!"got WMI callback after the device is removed!\n");
  282. status = STATUS_UNSUCCESSFUL;
  283. } else {
  284. ideDriverExtension = IoGetDriverObjectExtension(
  285. pdoExtension->DriverObject,
  286. DRIVER_OBJECT_EXTENSION_ID
  287. );
  288. if (!ideDriverExtension) {
  289. status = STATUS_UNSUCCESSFUL;
  290. } else {
  291. *RegFlags = WMIREG_FLAG_INSTANCE_PDO;
  292. *RegistryPath = &ideDriverExtension->RegistryPath;
  293. *Pdo = pdoExtension->DeviceObject;
  294. status = STATUS_SUCCESS;
  295. }
  296. UnrefPdoWithTag (
  297. pdoExtension,
  298. DeviceQueryWmiRegInfo
  299. );
  300. }
  301. return status;
  302. }
  303. NTSTATUS
  304. DeviceSetWmiDataBlock(
  305. IN PDEVICE_OBJECT DeviceObject,
  306. IN PIRP Irp,
  307. IN ULONG GuidIndex,
  308. IN ULONG InstanceIndex,
  309. IN ULONG BufferSize,
  310. IN PUCHAR Buffer
  311. )
  312. /*++
  313. Routine Description:
  314. This routine is a callback into the driver to set the contents of
  315. a data block. When the driver has finished filling the data block it
  316. must call ClassWmiCompleteRequest to complete the irp. The driver can
  317. return STATUS_PENDING if the irp cannot be completed immediately.
  318. Arguments:
  319. DeviceObject is the device whose data block is being queried
  320. Irp is the Irp that makes this request
  321. GuidIndex is the index into the list of guids provided when the
  322. device registered
  323. InstanceIndex is the index that denotes which instance of the data block
  324. is being set.
  325. BufferSize has the size of the data block passed
  326. Buffer has the new values for the data block
  327. Return Value:
  328. status
  329. --*/
  330. {
  331. PPDO_EXTENSION pdoExtension;
  332. NTSTATUS status;
  333. PAGED_CODE();
  334. pdoExtension = RefPdoWithTag (DeviceObject, FALSE, Irp);
  335. if (!pdoExtension) {
  336. ASSERT(!"got WMI callback after the device is removed!\n");
  337. status = STATUS_UNSUCCESSFUL;
  338. } else {
  339. switch (GuidIndex) {
  340. case WmiScsiAddress: {
  341. status = STATUS_WMI_READ_ONLY;
  342. break;
  343. }
  344. default:
  345. status = STATUS_WMI_GUID_NOT_FOUND;
  346. }
  347. status = WmiCompleteRequest( DeviceObject,
  348. Irp,
  349. status,
  350. 0,
  351. IO_NO_INCREMENT
  352. );
  353. UnrefPdoWithTag (
  354. pdoExtension,
  355. Irp
  356. );
  357. }
  358. return status;
  359. }
  360. NTSTATUS
  361. DeviceSetWmiDataItem(
  362. IN PDEVICE_OBJECT DeviceObject,
  363. IN PIRP Irp,
  364. IN ULONG GuidIndex,
  365. IN ULONG InstanceIndex,
  366. IN ULONG DataItemId,
  367. IN ULONG BufferSize,
  368. IN PUCHAR Buffer
  369. )
  370. /*++
  371. Routine Description:
  372. This routine is a callback into the driver to set for the contents of
  373. a data block. When the driver has finished filling the data block it
  374. must call ClassWmiCompleteRequest to complete the irp. The driver can
  375. return STATUS_PENDING if the irp cannot be completed immediately.
  376. Arguments:
  377. DeviceObject is the device whose data block is being queried
  378. Irp is the Irp that makes this request
  379. GuidIndex is the index into the list of guids provided when the
  380. device registered
  381. InstanceIndex is the index that denotes which instance of the data block
  382. is being set.
  383. DataItemId has the id of the data item being set
  384. BufferSize has the size of the data item passed
  385. Buffer has the new values for the data item
  386. Return Value:
  387. status
  388. --*/
  389. {
  390. PPDO_EXTENSION pdoExtension;
  391. NTSTATUS status;
  392. PAGED_CODE();
  393. pdoExtension = RefPdoWithTag (DeviceObject, FALSE, Irp);
  394. if (!pdoExtension) {
  395. ASSERT(!"got WMI callback after the device is removed!\n");
  396. status = STATUS_UNSUCCESSFUL;
  397. } else {
  398. switch(GuidIndex) {
  399. case WmiScsiAddress: {
  400. status = STATUS_WMI_READ_ONLY;
  401. break;
  402. }
  403. default:
  404. status = STATUS_WMI_GUID_NOT_FOUND;
  405. break;
  406. }
  407. status = WmiCompleteRequest( DeviceObject,
  408. Irp,
  409. status,
  410. 0,
  411. IO_NO_INCREMENT
  412. );
  413. UnrefPdoWithTag (
  414. pdoExtension,
  415. Irp
  416. );
  417. }
  418. return status;
  419. }
  420. #endif // IDEPORT_WMI_SUPPORT