ReactOS  0.4.13-dev-100-gc8611ae
pnp.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Generic CPU Driver
3  * LICENSE: GNU GPLv2 only as published by the Free Software Foundation
4  * FILE: drivers/processor/processr/pnp.c
5  * PURPOSE: Plug N Play routines
6  * PROGRAMMERS: Eric Kohl <eric.kohl@reactos.org>
7  */
8 
9 /* INCLUDES *******************************************************************/
10 
11 #include "processr.h"
12 
13 #include <stdio.h>
14 #define NDEBUG
15 #include <debug.h>
16 
17 /* FUNCTIONS ******************************************************************/
18 
19 static
24  PWSTR *DeviceId)
25 {
26  PIO_STACK_LOCATION IrpStack;
28  PDEVICE_OBJECT TargetObject;
29  KEVENT Event;
30  PIRP Irp;
32 
33  PAGED_CODE();
34 
35  /* Initialize the event */
37 
39 
40  /* Build the IRP */
42  TargetObject,
43  NULL,
44  0,
45  NULL,
46  &Event,
47  &IoStatus);
48  if (Irp == NULL)
49  {
51  goto done;
52  }
53 
54  /* PNP IRPs all begin life as STATUS_NOT_SUPPORTED */
55  Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
56 
57  /* Get the top of stack */
58  IrpStack = IoGetNextIrpStackLocation(Irp);
59 
60  /* Set the top of stack */
61  RtlZeroMemory(IrpStack, sizeof(IO_STACK_LOCATION));
62  IrpStack->MajorFunction = IRP_MJ_PNP;
63  IrpStack->MinorFunction = IRP_MN_QUERY_ID;
64  IrpStack->Parameters.QueryId.IdType = IdType;
65 
66  /* Call the driver */
67  Status = IoCallDriver(TargetObject, Irp);
68  if (Status == STATUS_PENDING)
69  {
71  Executive,
72  KernelMode,
73  FALSE,
74  NULL);
75 
76  Status = IoStatus.Status;
77  }
78 
79  if (NT_SUCCESS(Status))
80  {
81  *DeviceId = (PWSTR)IoStatus.Information;
82  }
83 
84 done:
85  /* Dereference the target device object */
86  ObDereferenceObject(TargetObject);
87 
88  return Status;
89 }
90 
91 
92 
93 static
94 VOID
97 {
100  UNICODE_STRING HardwareKeyName, ValueName, EnumKeyName;
102  ULONG DataLength = 0;
103  ULONG BufferLength = 0;
105  PWSTR KeyNameBuffer = NULL;
106  PWSTR DeviceId = NULL;
108  PWSTR pszPrefix = L"\\Registry\\Machine\\System\\CurrentcontrolSet\\Enum";
109 
110  RtlInitUnicodeString(&HardwareKeyName,
111  L"\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
113  &HardwareKeyName,
115  NULL,
116  NULL);
117  Status = ZwOpenKey(&KeyHandle,
118  KEY_READ,
120  if (!NT_SUCCESS(Status))
121  {
122  DPRINT1("ZwOpenKey() failed (Status 0x%08lx)\n", Status);
123  return;
124  }
125 
127  L"ProcessorNameString");
128  Status = ZwQueryValueKey(KeyHandle,
129  &ValueName,
131  NULL,
132  0,
133  &DataLength);
135  {
136  DPRINT1("ZwQueryValueKey() failed (Status 0x%08lx)\n", Status);
137  goto done;
138  }
139 
142  if (Buffer == NULL)
143  {
144  DPRINT1("ExAllocatePool() failed\n");
145  goto done;
146  }
147 
148  Status = ZwQueryValueKey(KeyHandle,
149  &ValueName,
151  Buffer,
153  &DataLength);
154  if (!NT_SUCCESS(Status))
155  {
156  DPRINT1("ZwQueryValueKey() failed (Status 0x%08lx)\n", Status);
157  goto done;
158  }
159 
160  DPRINT("ProcessorNameString: %S\n", (PWSTR)&Buffer->Data[0]);
161 
163  KeyHandle = NULL;
164 
167  &DeviceId);
168  if (!NT_SUCCESS(Status))
169  {
170  DPRINT1("GetDeviceId() failed (Status 0x%08lx)\n", Status);
171  goto done;
172  }
173 
174  DPRINT("DeviceId: %S\n", DeviceId);
175 
178  &InstanceId);
179  if (!NT_SUCCESS(Status))
180  {
181  DPRINT1("GetDeviceId() failed (Status 0x%08lx)\n", Status);
182  goto done;
183  }
184 
185  DPRINT("InstanceId: %S\n", InstanceId);
186 
187  BufferLength = wcslen(pszPrefix) + 1 + wcslen(DeviceId) + 1 + wcslen(InstanceId) + 1;
188 
189  KeyNameBuffer = ExAllocatePool(PagedPool, BufferLength * sizeof(WCHAR));
190  if (KeyNameBuffer == NULL)
191  {
192  DPRINT1("ExAllocatePool() failed\n");
193  goto done;
194  }
195 
196  swprintf(KeyNameBuffer, L"%s\\%s\\%s", pszPrefix, DeviceId, InstanceId);
197 
198  RtlInitUnicodeString(&EnumKeyName, KeyNameBuffer);
200  &EnumKeyName,
202  NULL,
203  NULL);
204  Status = ZwOpenKey(&KeyHandle,
205  KEY_WRITE,
207  if (!NT_SUCCESS(Status))
208  {
209  DPRINT1("ZwOpenKey() failed (Status 0x%08lx)\n", Status);
210  goto done;
211  }
212 
214  L"FriendlyName");
215  Status = ZwSetValueKey(KeyHandle,
216  &ValueName,
217  0,
218  REG_SZ,
219  (PVOID)&Buffer->Data[0],
220  Buffer->DataLength);
221  if (!NT_SUCCESS(Status))
222  {
223  DPRINT1("ZwSetValueKey() failed (Status 0x%08lx)\n", Status);
224  goto done;
225  }
226 
227 done:
228  if (KeyHandle != NULL)
230 
231  if (KeyNameBuffer != NULL)
232  ExFreePool(KeyNameBuffer);
233 
234  if (InstanceId != NULL)
236 
237  if (DeviceId != NULL)
238  ExFreePool(DeviceId);
239 
240  if (Buffer != NULL)
242 }
243 
244 
245 static
246 NTSTATUS
250  IN PCM_RESOURCE_LIST ResourceListTranslated)
251 {
252  DPRINT("ProcessorStartDevice()\n");
253 
255 
256  return STATUS_SUCCESS;
257 }
258 
259 
260 NTSTATUS
261 NTAPI
264  IN PIRP Irp)
265 {
269 
270  DPRINT("ProcessorPnp()\n");
271 
273 
274  switch (IrpSp->MinorFunction)
275  {
276  case IRP_MN_START_DEVICE:
277  DPRINT(" IRP_MN_START_DEVICE received\n");
278 
279  /* Call lower driver */
281  if (NT_SUCCESS(Status))
282  {
284  IrpSp->Parameters.StartDevice.AllocatedResources,
285  IrpSp->Parameters.StartDevice.AllocatedResourcesTranslated);
286  }
287  break;
288 
290  DPRINT(" IRP_MN_QUERY_REMOVE_DEVICE\n");
292 
294  DPRINT(" IRP_MN_REMOVE_DEVICE received\n");
296 
298  DPRINT(" IRP_MN_CANCEL_REMOVE_DEVICE\n");
300 
301  case IRP_MN_STOP_DEVICE:
302  DPRINT(" IRP_MN_STOP_DEVICE received\n");
304 
306  DPRINT(" IRP_MN_QUERY_STOP_DEVICE received\n");
308 
310  DPRINT(" IRP_MN_CANCEL_STOP_DEVICE\n");
312 
314  DPRINT(" IRP_MN_QUERY_DEVICE_RELATIONS\n");
315 
316  switch (IrpSp->Parameters.QueryDeviceRelations.Type)
317  {
318  case BusRelations:
319  DPRINT(" IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / BusRelations\n");
321  break;
322 
323  case RemovalRelations:
324  DPRINT(" IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / RemovalRelations\n");
326 
327  default:
328  DPRINT(" IRP_MJ_PNP / IRP_MN_QUERY_DEVICE_RELATIONS / Unknown type 0x%lx\n",
329  IrpSp->Parameters.QueryDeviceRelations.Type);
331  }
332  break;
333 
335  DPRINT(" IRP_MN_SURPRISE_REMOVAL received\n");
337 
338  case IRP_MN_FILTER_RESOURCE_REQUIREMENTS: /* (optional) 0xd */
339  DPRINT(" IRP_MN_FILTER_RESOURCE_REQUIREMENTS\n");
341 
342  default:
343  DPRINT(" Unknown IOCTL 0x%lx\n", IrpSp->MinorFunction);
345  }
346 
347  Irp->IoStatus.Information = Information;
348  Irp->IoStatus.Status = Status;
350 
351  return Status;
352 }
353 
354 
355 NTSTATUS
356 NTAPI
360 {
361  PDEVICE_EXTENSION DeviceExtension = NULL;
362  PDEVICE_OBJECT Fdo = NULL;
364 
365  DPRINT("ProcessorAddDevice()\n");
366 
368  ASSERT(Pdo);
369 
370  /* Create functional device object */
372  sizeof(DEVICE_EXTENSION),
373  NULL,
376  FALSE,
377  &Fdo);
378  if (NT_SUCCESS(Status))
379  {
380  DeviceExtension = (PDEVICE_EXTENSION)Fdo->DeviceExtension;
381  RtlZeroMemory(DeviceExtension, sizeof(DEVICE_EXTENSION));
382 
383  DeviceExtension->DeviceObject = Fdo;
384 
385  Status = IoAttachDeviceToDeviceStackSafe(Fdo, Pdo, &DeviceExtension->LowerDevice);
386  if (!NT_SUCCESS(Status))
387  {
388  DPRINT1("IoAttachDeviceToDeviceStackSafe() failed with status 0x%08lx\n", Status);
389  IoDeleteDevice(Fdo);
390  return Status;
391  }
392 
393  Fdo->Flags |= DO_DIRECT_IO;
394  Fdo->Flags |= DO_POWER_PAGABLE;
395 
396  Fdo->Flags &= ~DO_DEVICE_INITIALIZING;
397  }
398 
399  return Status;
400 }
401 
402 /* EOF */
#define DO_DEVICE_INITIALIZING
Definition: env_spec_w32.h:399
#define IRP_MN_CANCEL_REMOVE_DEVICE
#define DO_POWER_PAGABLE
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
#define IN
Definition: typedefs.h:38
enum _BUS_QUERY_ID_TYPE BUS_QUERY_ID_TYPE
#define IRP_MN_REMOVE_DEVICE
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define IRP_MN_QUERY_ID
_In_ PDEVICE_OBJECT Pdo
Definition: classpnp.h:301
NTSTATUS NTAPI ProcessorPnp(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: pnp.c:262
NTSTATUS NTAPI ProcessorAddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT Pdo)
Definition: pnp.c:357
_Must_inspect_result_ _Out_ PNDIS_STATUS _In_ NDIS_HANDLE _In_ ULONG _Out_ PNDIS_STRING _Out_ PNDIS_HANDLE KeyHandle
Definition: ndis.h:4693
IN PLARGE_INTEGER IN ULONG IN BOOLEAN IN ULONG IN BOOLEAN OUT PIO_STATUS_BLOCK IoStatus
Definition: fatprocs.h:2650
PIRP NTAPI IoBuildSynchronousFsdRequest(IN ULONG MajorFunction, IN PDEVICE_OBJECT DeviceObject, IN PVOID Buffer, IN ULONG Length, IN PLARGE_INTEGER StartingOffset, IN PKEVENT Event, IN PIO_STATUS_BLOCK IoStatusBlock)
Definition: irp.c:1069
_In_ PIRP Irp
Definition: csq.h:116
#define KEY_READ
Definition: nt_native.h:1023
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
#define IRP_MN_FILTER_RESOURCE_REQUIREMENTS
uint16_t * PWSTR
Definition: typedefs.h:54
#define IRP_MJ_PNP
Definition: cdrw_usr.h:52
DRIVER_DISPATCH ForwardIrpAndForget
Definition: i8042prt.h:341
LONG NTSTATUS
Definition: precomp.h:26
static VOID ProcessorSetFriendlyName(PDEVICE_OBJECT DeviceObject)
Definition: pnp.c:95
static NTSTATUS GetDeviceId(PDEVICE_OBJECT DeviceObject, BUS_QUERY_ID_TYPE IdType, PWSTR *DeviceId)
Definition: pnp.c:21
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
const MUI_LANGUAGE_RESOURCE ResourceList[]
Definition: muilanguages.h:414
#define PAGED_CODE()
Definition: video.h:57
#define DO_DIRECT_IO
Definition: env_spec_w32.h:396
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:64
uint32_t ULONG_PTR
Definition: typedefs.h:63
_In_ PUNICODE_STRING ValueName
Definition: cmfuncs.h:264
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define IRP_MN_QUERY_REMOVE_DEVICE
_In_ ULONG BufferLength
Definition: usbdlib.h:225
_In_ PVOID _In_ ULONG Event
Definition: iotypes.h:434
_Must_inspect_result_ _In_opt_ PVOID _In_opt_ PVOID InstanceId
Definition: fsrtlfuncs.h:907
PVOID DeviceExtension
Definition: env_spec_w32.h:418
smooth NULL
Definition: ftsmooth.c:416
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
#define IoCompleteRequest
Definition: irp.c:1240
void DPRINT(...)
Definition: polytest.cpp:61
Definition: bufpool.h:45
_In_ BUS_QUERY_ID_TYPE IdType
Definition: classpnp.h:357
PDEVICE_OBJECT NTAPI IoGetAttachedDeviceReference(PDEVICE_OBJECT DeviceObject)
Definition: device.c:1405
#define IRP_MN_QUERY_STOP_DEVICE
#define KEY_WRITE
Definition: nt_native.h:1031
#define IRP_MN_SURPRISE_REMOVAL
Definition: ntifs_ex.h:408
#define IRP_MN_STOP_DEVICE
__wchar_t WCHAR
Definition: xmlstorage.h:180
NTSTATUS NTAPI IoAttachDeviceToDeviceStackSafe(IN PDEVICE_OBJECT SourceDevice, IN PDEVICE_OBJECT TargetDevice, IN OUT PDEVICE_OBJECT *AttachedToDeviceObject)
Definition: device.c:980
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_PENDING
Definition: ntstatus.h:82
#define swprintf(buf, format,...)
Definition: sprintf.c:56
#define IRP_MN_START_DEVICE
DRIVER_DISPATCH ForwardIrpAndWait
Definition: i8042prt.h:343
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
static const WCHAR L[]
Definition: oid.c:1250
#define ExAllocatePool(type, size)
Definition: fbtusb.h:44
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetNextIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2647
Status
Definition: gdiplustypes.h:24
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
VOID NTAPI IoDeleteDevice(IN PDEVICE_OBJECT DeviceObject)
Definition: device.c:1250
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define STATUS_BUFFER_OVERFLOW
Definition: shellext.h:61
_In_ PIO_STACK_LOCATION IrpSp
Definition: create.c:4157
#define FILE_DEVICE_UNKNOWN
Definition: winioctl.h:139
NTSTATUS NTAPI IoCallDriver(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: irp.c:1218
#define IRP_MN_QUERY_DEVICE_RELATIONS
#define DPRINT1
Definition: precomp.h:8
#define STATUS_NOT_SUPPORTED
Definition: ntstatus.h:409
unsigned int ULONG
Definition: retypes.h:1
#define IO_NO_INCREMENT
Definition: iotypes.h:565
NTSTATUS NTAPI IoCreateDevice(IN PDRIVER_OBJECT DriverObject, IN ULONG DeviceExtensionSize, IN PUNICODE_STRING DeviceName, IN DEVICE_TYPE DeviceType, IN ULONG DeviceCharacteristics, IN BOOLEAN Exclusive, OUT PDEVICE_OBJECT *DeviceObject)
Definition: device.c:1031
static NTSTATUS ProcessorStartDevice(IN PDEVICE_OBJECT DeviceObject, IN PCM_RESOURCE_LIST ResourceList, IN PCM_RESOURCE_LIST ResourceListTranslated)
Definition: pnp.c:247
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define FILE_DEVICE_SECURE_OPEN
Definition: cdrw_usr.h:46
#define IRP_MN_CANCEL_STOP_DEVICE
_Must_inspect_result_ _Out_writes_to_ DataLength PHIDP_DATA _Inout_ PULONG DataLength
Definition: hidpi.h:333
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2771
return STATUS_SUCCESS
Definition: btrfs.c:2725
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
struct _BEEP_DEVICE_EXTENSION * PDEVICE_EXTENSION
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
Iosb Information
Definition: create.c:4377
#define REG_SZ
Definition: layer.c:22