ReactOS  0.4.13-dev-92-gf251225
fbtwmi.c
Go to the documentation of this file.
1 // Copyright (c) 2004, Antony C. Roberts
2 
3 // Use of this file is subject to the terms
4 // described in the LICENSE.TXT file that
5 // accompanies this file.
6 //
7 // Your use of this file indicates your
8 // acceptance of the terms described in
9 // LICENSE.TXT.
10 //
11 // http://www.freebt.net
12 
13 #include "fbtusb.h"
14 #include "fbtpwr.h"
15 #include "fbtpnp.h"
16 #include "fbtdev.h"
17 #include "fbtrwr.h"
18 #include "fbtwmi.h"
19 
20 #include "fbtusr.h"
21 
22 #define MOFRESOURCENAME L"MofResourceName"
23 
24 #define WMI_FREEBT_DRIVER_INFORMATION 0
25 
26 DEFINE_GUID (FREEBT_WMI_STD_DATA_GUID, 0x871B1A60, 0xD3EA, 0x4f2f, 0x81, 0x7b, 0x46, 0x5e, 0x44, 0x86, 0x7b, 0xf5);
27 
29 {
30  {
31  &FREEBT_WMI_STD_DATA_GUID, 1, 0 // driver information
32 
33  }
34 
35 };
36 
38 {
39  NTSTATUS ntStatus;
40 
41  PAGED_CODE();
42 
43  DeviceExtension->WmiLibInfo.GuidCount = sizeof (FreeBTWmiGuidList) / sizeof (WMIGUIDREGINFO);
44  DeviceExtension->WmiLibInfo.GuidList = FreeBTWmiGuidList;
45  DeviceExtension->WmiLibInfo.QueryWmiRegInfo = FreeBT_QueryWmiRegInfo;
46  DeviceExtension->WmiLibInfo.QueryWmiDataBlock = FreeBT_QueryWmiDataBlock;
47  DeviceExtension->WmiLibInfo.SetWmiDataBlock = FreeBT_SetWmiDataBlock;
48  DeviceExtension->WmiLibInfo.SetWmiDataItem = FreeBT_SetWmiDataItem;
49  DeviceExtension->WmiLibInfo.ExecuteWmiMethod = NULL;
50  DeviceExtension->WmiLibInfo.WmiFunctionControl = NULL;
51 
52  // Register with WMI
53  ntStatus = IoWMIRegistrationControl(DeviceExtension->FunctionalDeviceObject, WMIREG_ACTION_REGISTER);
54 
55  return ntStatus;
56 
57 }
58 
60 {
61  PAGED_CODE();
62  return IoWMIRegistrationControl(DeviceExtension->FunctionalDeviceObject, WMIREG_ACTION_DEREGISTER);
63 
64 }
65 
67 {
68  PDEVICE_EXTENSION deviceExtension;
69  SYSCTL_IRP_DISPOSITION disposition;
70  NTSTATUS ntStatus;
71  PIO_STACK_LOCATION irpStack;
72 
73  PAGED_CODE();
74 
75  irpStack = IoGetCurrentIrpStackLocation (Irp);
76  deviceExtension = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
77 
78  FreeBT_DbgPrint(3, ("FBTUSB: "));
80  if (Removed == deviceExtension->DeviceState)
81  {
82  ntStatus = STATUS_DELETE_PENDING;
83 
84  Irp->IoStatus.Status = ntStatus;
85  Irp->IoStatus.Information = 0;
87 
88  return ntStatus;
89 
90  }
91 
92  FreeBT_DbgPrint(3, ("FBTUSB: FreeBT_DispatchSysCtrl::"));
93  FreeBT_IoIncrement(deviceExtension);
94 
95  ntStatus = WmiSystemControl(&deviceExtension->WmiLibInfo,
97  Irp,
98  &disposition);
99 
100  switch(disposition)
101  {
102  case IrpProcessed:
103  {
104  // This irp has been processed and may be completed or pending.
105  break;
106 
107  }
108 
109  case IrpNotCompleted:
110  {
111  // This irp has not been completed, but has been fully processed.
112  // we will complete it now
114  break;
115 
116  }
117 
118  case IrpForward:
119  case IrpNotWmi:
120  {
121  // This irp is either not a WMI irp or is a WMI irp targeted
122  // at a device lower in the stack.
124  ntStatus = IoCallDriver(deviceExtension->TopOfStackDeviceObject, Irp);
125  break;
126  }
127 
128  default:
129  {
130  // We really should never get here, but if we do just forward....
131  ASSERT(FALSE);
133  ntStatus = IoCallDriver(deviceExtension->TopOfStackDeviceObject, Irp);
134  break;
135 
136  }
137 
138  }
139 
140  FreeBT_DbgPrint(3, ("FBTUSB: FreeBT_DispatchSysCtrl::"));
141  FreeBT_IoDecrement(deviceExtension);
142 
143  return ntStatus;
144 
145 }
146 
149  OUT ULONG *RegFlags,
154  )
155 /*++
156 
157 Routine Description:
158 
159  This routine is a callback into the driver to retrieve the list of
160  guids or data blocks that the driver wants to register with WMI. This
161  routine may not pend or block. Driver should NOT call
162  WmiCompleteRequest.
163 
164 Arguments:
165 
166  DeviceObject is the device whose data block is being queried
167 
168  *RegFlags returns with a set of flags that describe the guids being
169  registered for this device. If the device wants enable and disable
170  collection callbacks before receiving queries for the registered
171  guids then it should return the WMIREG_FLAG_EXPENSIVE flag. Also the
172  returned flags may specify WMIREG_FLAG_INSTANCE_PDO in which case
173  the instance name is determined from the PDO associated with the
174  device object. Note that the PDO must have an associated devnode. If
175  WMIREG_FLAG_INSTANCE_PDO is not set then Name must return a unique
176  name for the device.
177 
178  InstanceName returns with the instance name for the guids if
179  WMIREG_FLAG_INSTANCE_PDO is not set in the returned *RegFlags. The
180  caller will call ExFreePool with the buffer returned.
181 
182  *RegistryPath returns with the registry path of the driver
183 
184  *MofResourceName returns with the name of the MOF resource attached to
185  the binary file. If the driver does not have a mof resource attached
186  then this can be returned as NULL.
187 
188  *Pdo returns with the device object for the PDO associated with this
189  device if the WMIREG_FLAG_INSTANCE_PDO flag is returned in
190  *RegFlags.
191 
192 Return Value:
193 
194  status
195 
196 --*/
197 {
198  PDEVICE_EXTENSION deviceExtension;
199 
200  PAGED_CODE();
201 
202  FreeBT_DbgPrint(3, ("FBTUSB: FreeBT_QueryWmiRegInfo: Entered\n"));
203 
204  deviceExtension = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
205 
207  *RegistryPath = &Globals.FreeBT_RegistryPath;
208  *Pdo = deviceExtension->PhysicalDeviceObject;
210 
211  FreeBT_DbgPrint(3, ("FBTUSB: FreeBT_QueryWmiRegInfo: Leaving\n"));
212 
213  return STATUS_SUCCESS;
214 
215 }
216 
219  IN PIRP Irp,
223  IN OUT PULONG InstanceLengthArray,
226  )
227 /*++
228 
229 Routine Description:
230 
231  This routine is a callback into the driver to query for the contents of
232  a data block. When the driver has finished filling the data block it
233  must call WmiCompleteRequest to complete the irp. The driver can
234  return STATUS_PENDING if the irp cannot be completed immediately.
235 
236 Arguments:
237 
238  DeviceObject is the device whose data block is being queried
239 
240  Irp is the Irp that makes this request
241 
242  GuidIndex is the index into the list of guids provided when the
243  device registered
244 
245  InstanceIndex is the index that denotes which instance of the data block
246  is being queried.
247 
248  InstanceCount is the number of instances expected to be returned for
249  the data block.
250 
251  InstanceLengthArray is a pointer to an array of ULONG that returns the
252  lengths of each instance of the data block. If this is NULL then
253  there was not enough space in the output buffer to fulfill the request
254  so the irp should be completed with the buffer needed.
255 
256  OutBufferSize has the maximum size available to write the data
257  block.
258 
259  Buffer on return is filled with the returned data block
260 
261 
262 Return Value:
263 
264  status
265 
266 --*/
267 {
268  PDEVICE_EXTENSION deviceExtension;
269  NTSTATUS ntStatus;
270  ULONG size;
271  WCHAR modelName[] = L"Aishverya\0\0";
272  USHORT modelNameLen;
273 
274  PAGED_CODE();
275 
276  FreeBT_DbgPrint(3, ("FBTUSB: FreeBT_QueryWmiDataBlock: Entered\n"));
277 
278  size = 0;
279  modelNameLen = (wcslen(modelName) + 1) * sizeof(WCHAR);
280 
281  // Only ever registers 1 instance per guid
282  ASSERT((InstanceIndex == 0) && (InstanceCount == 1));
283 
284  deviceExtension = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
285  switch (GuidIndex)
286  {
288  size = sizeof(ULONG) + modelNameLen + sizeof(USHORT);
289  if (OutBufferSize < size )
290  {
291  FreeBT_DbgPrint(3, ("FBTUSB: OutBuffer too small\n"));
292  ntStatus = STATUS_BUFFER_TOO_SMALL;
293  break;
294 
295  }
296 
297  * (PULONG) Buffer = DebugLevel;
298  Buffer += sizeof(ULONG);
299 
300  // put length of string ahead of string
301  *((PUSHORT)Buffer) = modelNameLen;
302  Buffer = (PUCHAR)Buffer + sizeof(USHORT);
303  RtlCopyBytes((PVOID)Buffer, (PVOID)modelName, modelNameLen);
304  *InstanceLengthArray = size ;
305 
306  ntStatus = STATUS_SUCCESS;
307  break;
308 
309  default:
310  ntStatus = STATUS_WMI_GUID_NOT_FOUND;
311 
312  }
313 
314  ntStatus = WmiCompleteRequest(DeviceObject,
315  Irp,
316  ntStatus,
317  size,
319 
320  FreeBT_DbgPrint(3, ("FBTUSB: FreeBT_QueryWmiDataBlock: Leaving\n"));
321 
322  return ntStatus;
323 
324 }
325 
326 
329  IN PIRP Irp,
335  )
336 /*++
337 
338 Routine Description:
339 
340  This routine is a callback into the driver to set for the contents of
341  a data block. When the driver has finished filling the data block it
342  must call WmiCompleteRequest to complete the irp. The driver can
343  return STATUS_PENDING if the irp cannot be completed immediately.
344 
345 Arguments:
346 
347  DeviceObject is the device whose data block is being queried
348 
349  Irp is the Irp that makes this request
350 
351  GuidIndex is the index into the list of guids provided when the
352  device registered
353 
354  InstanceIndex is the index that denotes which instance of the data block
355  is being queried.
356 
357  DataItemId has the id of the data item being set
358 
359  BufferSize has the size of the data item passed
360 
361  Buffer has the new values for the data item
362 
363 
364 Return Value:
365 
366  status
367 
368 --*/
369 {
370  PDEVICE_EXTENSION deviceExtension;
371  NTSTATUS ntStatus;
372  ULONG info;
373 
374  PAGED_CODE();
375 
376  FreeBT_DbgPrint(3, ("FBTUSB: FreeBT_SetWmiDataItem: Entered\n"));
377 
378  deviceExtension = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
379  info = 0;
380 
381  switch(GuidIndex)
382  {
384  if(DataItemId == 1)
385  {
386  if(BufferSize == sizeof(ULONG))
387  {
388  DebugLevel = *((PULONG)Buffer);
389  ntStatus = STATUS_SUCCESS;
390  info = sizeof(ULONG);
391 
392  }
393 
394  else
395  {
396  ntStatus = STATUS_INFO_LENGTH_MISMATCH;
397 
398  }
399 
400  }
401 
402  else
403  {
404  ntStatus = STATUS_WMI_READ_ONLY;
405 
406  }
407 
408  break;
409 
410  default:
411  ntStatus = STATUS_WMI_GUID_NOT_FOUND;
412 
413  }
414 
415  ntStatus = WmiCompleteRequest(DeviceObject,
416  Irp,
417  ntStatus,
418  info,
420 
421  FreeBT_DbgPrint(3, ("FBTUSB: FreeBT_SetWmiDataItem: Leaving\n"));
422 
423  return ntStatus;
424 
425 }
426 
429  IN PIRP Irp,
434  )
435 /*++
436 
437 Routine Description:
438 
439  This routine is a callback into the driver to set the contents of
440  a data block. When the driver has finished filling the data block it
441  must call WmiCompleteRequest to complete the irp. The driver can
442  return STATUS_PENDING if the irp cannot be completed immediately.
443 
444 Arguments:
445 
446  DeviceObject is the device whose data block is being queried
447 
448  Irp is the Irp that makes this request
449 
450  GuidIndex is the index into the list of guids provided when the
451  device registered
452 
453  InstanceIndex is the index that denotes which instance of the data block
454  is being queried.
455 
456  BufferSize has the size of the data block passed
457 
458  Buffer has the new values for the data block
459 
460 --*/
461 {
462  PDEVICE_EXTENSION deviceExtension;
463  NTSTATUS ntStatus;
464  ULONG info;
465 
466  PAGED_CODE();
467 
468  FreeBT_DbgPrint(3, ("FBTUSB: FreeBT_SetWmiDataBlock: Entered\n"));
469 
470  deviceExtension = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
471  info = 0;
472 
473  switch(GuidIndex)
474  {
476  if(BufferSize == sizeof(ULONG))
477  {
478  DebugLevel = *(PULONG) Buffer;
479  ntStatus = STATUS_SUCCESS;
480  info = sizeof(ULONG);
481 
482  }
483 
484  else
485  {
486  ntStatus = STATUS_INFO_LENGTH_MISMATCH;
487 
488  }
489 
490  break;
491 
492  default:
493  ntStatus = STATUS_WMI_GUID_NOT_FOUND;
494 
495  }
496 
497  ntStatus = WmiCompleteRequest(DeviceObject,
498  Irp,
499  ntStatus,
500  info,
502 
503  FreeBT_DbgPrint(3, ("FBTUSB: FreeBT_SetWmiDataBlock: Leaving\n"));
504 
505  return ntStatus;
506 
507 }
508 
510 {
511  switch (MinorFunction)
512  {
514  return "IRP_MN_CHANGE_SINGLE_INSTANCE\n";
515 
517  return "IRP_MN_CHANGE_SINGLE_ITEM\n";
518 
520  return "IRP_MN_DISABLE_COLLECTION\n";
521 
523  return "IRP_MN_DISABLE_EVENTS\n";
524 
526  return "IRP_MN_ENABLE_COLLECTION\n";
527 
529  return "IRP_MN_ENABLE_EVENTS\n";
530 
532  return "IRP_MN_EXECUTE_METHOD\n";
533 
535  return "IRP_MN_QUERY_ALL_DATA\n";
536 
538  return "IRP_MN_QUERY_SINGLE_INSTANCE\n";
539 
540  case IRP_MN_REGINFO:
541  return "IRP_MN_REGINFO\n";
542 
543  default:
544  return "IRP_MN_?????\n";
545 
546  }
547 
548 }
NTSTATUS NTAPI FreeBT_WmiRegistration(IN OUT PDEVICE_EXTENSION DeviceExtension)
Definition: fbtwmi.c:37
LONG NTAPI FreeBT_IoDecrement(IN OUT PDEVICE_EXTENSION DeviceExtension)
Definition: fbtpnp.c:1742
signed char * PCHAR
Definition: retypes.h:7
#define IN
Definition: typedefs.h:38
#define STATUS_DELETE_PENDING
Definition: ntstatus.h:308
NTSTATUS NTAPI FreeBT_SetWmiDataItem(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN ULONG GuidIndex, IN ULONG InstanceIndex, IN ULONG DataItemId, IN ULONG BufferSize, IN PUCHAR Buffer)
Definition: fbtwmi.c:327
#define IRP_MN_REGINFO
_In_ PDEVICE_OBJECT Pdo
Definition: classpnp.h:301
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
#define STATUS_WMI_GUID_NOT_FOUND
Definition: ntstatus.h:762
_In_ PIRP Irp
Definition: csq.h:116
_In_ PIRP _In_ ULONG _In_ ULONG DataItemId
Definition: classpnp.h:419
#define WMIREG_ACTION_DEREGISTER
unsigned char * PUCHAR
Definition: retypes.h:3
NTSTATUS NTAPI WmiSystemControl(IN PWMILIB_CONTEXT WmiLibInfo, IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, OUT PSYSCTL_IRP_DISPOSITION IrpDisposition)
Definition: wmilib.c:48
LONG NTSTATUS
Definition: precomp.h:26
LONG NTAPI FreeBT_IoIncrement(IN OUT PDEVICE_EXTENSION DeviceExtension)
Definition: fbtpnp.c:1722
_In_ LPGUID _In_ ULONG InstanceIndex
Definition: classpnp.h:1057
_In_ PIRP _In_ ULONG _In_ ULONG _In_ ULONG _In_ ULONG OutBufferSize
Definition: classpnp.h:429
NTSTATUS NTAPI WmiCompleteRequest(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN NTSTATUS Status, IN ULONG BufferUsed, IN CCHAR PriorityBoost)
Definition: wmilib.c:24
NTSTATUS NTAPI FreeBT_SetWmiDataBlock(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN ULONG GuidIndex, IN ULONG InstanceIndex, IN ULONG BufferSize, IN PUCHAR Buffer)
Definition: fbtwmi.c:427
#define IRP_MN_QUERY_SINGLE_INSTANCE
#define STATUS_WMI_READ_ONLY
Definition: ntstatus.h:804
WMIGUIDREGINFO FreeBTWmiGuidList[1]
Definition: fbtwmi.c:28
PCHAR NTAPI WMIMinorFunctionString(UCHAR MinorFunction)
Definition: fbtwmi.c:509
ULONG DebugLevel
Definition: fbtusb.c:26
#define PAGED_CODE()
Definition: video.h:57
#define IRP_MN_ENABLE_COLLECTION
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:64
DEFINE_GUID(FREEBT_WMI_STD_DATA_GUID, 0x871B1A60, 0xD3EA, 0x4f2f, 0x81, 0x7b, 0x46, 0x5e, 0x44, 0x86, 0x7b, 0xf5)
struct _test_info info[]
Definition: SetCursorPos.c:19
#define RtlCopyBytes
Definition: rtlfuncs.h:282
CLIPBOARD_GLOBALS Globals
Definition: clipbrd.c:13
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define IRP_MN_CHANGE_SINGLE_ITEM
#define IRP_MN_DISABLE_COLLECTION
PVOID DeviceExtension
Definition: env_spec_w32.h:418
#define FreeBT_DbgPrint(level, _x_)
Definition: fbtusb.h:55
_Out_ ULONG * RegFlags
Definition: classpnp.h:386
_Out_ ULONG _Out_ PUNICODE_STRING _Out_ PUNICODE_STRING MofResourceName
Definition: classpnp.h:386
smooth NULL
Definition: ftsmooth.c:416
#define IoCompleteRequest
Definition: irp.c:1240
_In_ PIRP _In_ ULONG GuidIndex
Definition: classpnp.h:401
#define WMIREG_FLAG_INSTANCE_PDO
Definition: wmistr.h:69
Definition: bufpool.h:45
NTSTATUS NTAPI FreeBT_WmiDeRegistration(IN OUT PDEVICE_EXTENSION DeviceExtension)
Definition: fbtwmi.c:59
switch(r->id)
Definition: btrfs.c:2691
NTSTATUS NTAPI IoWMIRegistrationControl(IN PDEVICE_OBJECT DeviceObject, IN ULONG Action)
Definition: wmi.c:68
#define WMIREG_ACTION_REGISTER
enum _SYSCTL_IRP_DISPOSITION SYSCTL_IRP_DISPOSITION
GLsizeiptr size
Definition: glext.h:5919
#define IRP_MN_ENABLE_EVENTS
__wchar_t WCHAR
Definition: xmlstorage.h:180
NTSTATUS NTAPI FreeBT_QueryWmiDataBlock(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN ULONG GuidIndex, IN ULONG InstanceIndex, IN ULONG InstanceCount, IN OUT PULONG InstanceLengthArray, IN ULONG OutBufferSize, OUT PUCHAR Buffer)
Definition: fbtwmi.c:217
#define BufferSize
Definition: classpnp.h:419
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
unsigned char UCHAR
Definition: xmlstorage.h:181
#define IRP_MN_DISABLE_EVENTS
static const WCHAR L[]
Definition: oid.c:1250
#define IRP_MN_EXECUTE_METHOD
_In_ UCHAR MinorFunction
Definition: pofuncs.h:42
#define MOFRESOURCENAME
Definition: fbtwmi.c:22
#define IRP_MN_CHANGE_SINGLE_INSTANCE
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
unsigned short USHORT
Definition: pedump.c:61
#define IRP_MN_QUERY_ALL_DATA
NTSTATUS NTAPI FreeBT_QueryWmiRegInfo(IN PDEVICE_OBJECT DeviceObject, OUT ULONG *RegFlags, OUT PUNICODE_STRING InstanceName, OUT PUNICODE_STRING *RegistryPath, OUT PUNICODE_STRING MofResourceName, OUT PDEVICE_OBJECT *Pdo)
Definition: fbtwmi.c:147
unsigned int * PULONG
Definition: retypes.h:1
NTSTATUS NTAPI IoCallDriver(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: irp.c:1218
#define IoSkipCurrentIrpStackLocation(Irp)
Definition: ntifs_ex.h:421
NTSTATUS NTAPI FreeBT_DispatchSysCtrl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: fbtwmi.c:66
_Must_inspect_result_ _Inout_ PFLT_VOLUME _In_opt_ PCUNICODE_STRING InstanceName
Definition: fltkernel.h:1162
#define OUT
Definition: typedefs.h:39
unsigned int ULONG
Definition: retypes.h:1
#define IO_NO_INCREMENT
Definition: iotypes.h:565
_In_ ULONG InstanceCount
Definition: iofuncs.h:1319
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
_In_ PUNICODE_STRING RegistryPath
Definition: wmip.h:27
Definition: fbtusb.h:86
#define WMI_FREEBT_DRIVER_INFORMATION
Definition: fbtwmi.c:24
return STATUS_SUCCESS
Definition: btrfs.c:2725
struct _BEEP_DEVICE_EXTENSION * PDEVICE_EXTENSION
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
unsigned short * PUSHORT
Definition: retypes.h:2