2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS text-mode setup
4 * FILE: subsys/system/usetup/interface/devinst.c
5 * PURPOSE: Device installation
6 * PROGRAMMER: Hervé Poussineau (hpoussin@reactos.org)
18 PLUGPLAY_CONTROL_RESET_DEVICE_DATA ResetDeviceData
;
21 RtlInitUnicodeString(&ResetDeviceData
.DeviceInstance
, DeviceId
);
22 Status
= NtPlugPlayControl(PlugPlayControlResetDevice
, &ResetDeviceData
, sizeof(PLUGPLAY_CONTROL_RESET_DEVICE_DATA
));
23 if (!NT_SUCCESS(Status
))
25 DPRINT1("NtPlugPlayControl() failed with status 0x%08x\n", Status
);
37 IN LPCWSTR HardwareId
)
39 UNICODE_STRING PathPrefix
= RTL_CONSTANT_STRING(L
"System32\\DRIVERS\\");
40 UNICODE_STRING ServiceU
= RTL_CONSTANT_STRING(L
"Service");
41 UNICODE_STRING ErrorControlU
= RTL_CONSTANT_STRING(L
"ErrorControl");
42 UNICODE_STRING ImagePathU
= RTL_CONSTANT_STRING(L
"ImagePath");
43 UNICODE_STRING StartU
= RTL_CONSTANT_STRING(L
"Start");
44 UNICODE_STRING TypeU
= RTL_CONSTANT_STRING(L
"Type");
45 UNICODE_STRING StringU
;
46 OBJECT_ATTRIBUTES ObjectAttributes
;
49 LPWSTR Driver
, ImagePath
, FullImagePath
;
53 BOOLEAN deviceInstalled
= FALSE
;
54 UNICODE_STRING UpperFiltersU
= RTL_CONSTANT_STRING(L
"UpperFilters");
55 LPWSTR keyboardClass
= L
"kbdclass\0";
56 BOOLEAN keyboardDevice
= FALSE
;
58 /* Check if we know the hardware */
59 if (!SetupFindFirstLineW(hInf
, L
"HardwareIdsDatabase", HardwareId
, &Context
))
61 if (!INF_GetDataField(&Context
, 1, &Driver
))
64 /* Find associated driver name */
65 /* FIXME: check in other sections too! */
66 if (!SetupFindFirstLineW(hInf
, L
"BootBusExtenders.Load", Driver
, &Context
)
67 && !SetupFindFirstLineW(hInf
, L
"BusExtenders.Load", Driver
, &Context
)
68 && !SetupFindFirstLineW(hInf
, L
"SCSI.Load", Driver
, &Context
)
69 && !SetupFindFirstLineW(hInf
, L
"InputDevicesSupport.Load", Driver
, &Context
))
71 if (!SetupFindFirstLineW(hInf
, L
"Keyboard.Load", Driver
, &Context
))
74 keyboardDevice
= TRUE
;
77 if (!INF_GetDataField(&Context
, 1, &ImagePath
))
80 /* Prepare full driver path */
81 dwValue
= PathPrefix
.MaximumLength
+ wcslen(ImagePath
) * sizeof(WCHAR
);
82 FullImagePath
= (LPWSTR
)RtlAllocateHeap(ProcessHeap
, 0, dwValue
);
85 DPRINT1("RtlAllocateHeap() failed\n");
88 RtlCopyMemory(FullImagePath
, PathPrefix
.Buffer
, PathPrefix
.MaximumLength
);
89 wcscat(FullImagePath
, ImagePath
);
91 DPRINT1("Using driver '%S' for device '%S'\n", ImagePath
, DeviceId
);
93 /* Create service key */
94 RtlInitUnicodeString(&StringU
, Driver
);
95 InitializeObjectAttributes(&ObjectAttributes
, &StringU
, 0, hServices
, NULL
);
96 Status
= NtCreateKey(&hService
, KEY_SET_VALUE
, &ObjectAttributes
, 0, NULL
, 0, &Disposition
);
97 if (!NT_SUCCESS(Status
))
99 DPRINT1("NtCreateKey('%wZ') failed with status 0x%08x\n", &StringU
, Status
);
100 RtlFreeHeap(ProcessHeap
, 0, FullImagePath
);
104 /* Fill service key */
105 if (Disposition
== REG_CREATED_NEW_KEY
)
123 dwValue
= SERVICE_KERNEL_DRIVER
;
132 /* HACK: don't put any path in registry */
139 (wcslen(ImagePath
) + 1) * sizeof(WCHAR
));
143 DPRINT1("Installing keyboard class driver for '%S'\n", DeviceId
);
144 NtSetValueKey(hDeviceKey
,
149 (wcslen(keyboardClass
) + 2) * sizeof(WCHAR
));
152 /* Associate device with the service we just filled */
153 Status
= NtSetValueKey(
159 (wcslen(Driver
) + 1) * sizeof(WCHAR
));
160 if (NT_SUCCESS(Status
))
162 /* Restart the device, so it will use the driver we registred */
163 deviceInstalled
= ResetDevice(DeviceId
);
166 /* HACK: Update driver path */
173 (wcslen(FullImagePath
) + 1) * sizeof(WCHAR
));
174 RtlFreeHeap(ProcessHeap
, 0, FullImagePath
);
177 return deviceInstalled
;
187 UNICODE_STRING HardwareIDU
= RTL_CONSTANT_STRING(L
"HardwareID");
188 UNICODE_STRING CompatibleIDsU
= RTL_CONSTANT_STRING(L
"CompatibleIDs");
189 UNICODE_STRING DeviceIdU
;
190 OBJECT_ATTRIBUTES ObjectAttributes
;
192 PKEY_VALUE_PARTIAL_INFORMATION pPartialInformation
= NULL
;
195 BOOLEAN bDriverInstalled
= FALSE
;
198 RtlInitUnicodeString(&DeviceIdU
, DeviceId
);
199 InitializeObjectAttributes(&ObjectAttributes
, &DeviceIdU
, 0, hEnum
, NULL
);
200 Status
= NtOpenKey(&hDeviceKey
, KEY_QUERY_VALUE
| KEY_SET_VALUE
, &ObjectAttributes
);
201 if (!NT_SUCCESS(Status
))
203 DPRINT("Unable to open subkey '%S'\n", DeviceId
);
207 Status
= NtQueryValueKey(
210 KeyValuePartialInformation
,
214 if (Status
== STATUS_BUFFER_TOO_SMALL
)
216 pPartialInformation
= (PKEY_VALUE_PARTIAL_INFORMATION
)RtlAllocateHeap(ProcessHeap
, 0, ulRequired
);
217 if (!pPartialInformation
)
219 DPRINT1("RtlAllocateHeap() failed\n");
223 Status
= NtQueryValueKey(
226 KeyValuePartialInformation
,
231 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
235 else if (!NT_SUCCESS(Status
))
237 DPRINT1("NtQueryValueKey() failed with status 0x%08x\n", Status
);
238 if (pPartialInformation
)
239 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
243 else if (pPartialInformation
)
245 for (HardwareID
= (LPCWSTR
)pPartialInformation
->Data
;
246 (PUCHAR
)HardwareID
< pPartialInformation
->Data
+ pPartialInformation
->DataLength
248 && !bDriverInstalled
;
249 HardwareID
+= wcslen(HardwareID
) + 1)
251 bDriverInstalled
= InstallDriver(hInf
, hServices
,hDeviceKey
, DeviceId
, HardwareID
);
255 if (!bDriverInstalled
)
257 if (pPartialInformation
)
259 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
260 pPartialInformation
= NULL
;
262 Status
= NtQueryValueKey(
265 KeyValuePartialInformation
,
269 if (Status
== STATUS_BUFFER_TOO_SMALL
)
271 pPartialInformation
= (PKEY_VALUE_PARTIAL_INFORMATION
)RtlAllocateHeap(ProcessHeap
, 0, ulRequired
);
272 if (!pPartialInformation
)
274 DPRINT("RtlAllocateHeap() failed\n");
278 Status
= NtQueryValueKey(
281 KeyValuePartialInformation
,
286 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
290 else if (!NT_SUCCESS(Status
))
292 if (pPartialInformation
)
293 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
295 DPRINT1("NtQueryValueKey() failed with status 0x%08x\n", Status
);
298 else if (pPartialInformation
)
300 for (HardwareID
= (LPCWSTR
)pPartialInformation
->Data
;
301 (PUCHAR
)HardwareID
< pPartialInformation
->Data
+ pPartialInformation
->DataLength
303 && !bDriverInstalled
;
304 HardwareID
+= wcslen(HardwareID
) + 1)
306 bDriverInstalled
= InstallDriver(hInf
, hServices
,hDeviceKey
, DeviceId
, HardwareID
);
310 if (!bDriverInstalled
)
311 DPRINT("No driver available for %S\n", DeviceId
);
313 RtlFreeHeap(ProcessHeap
, 0, pPartialInformation
);
318 EventThread(IN LPVOID lpParameter
)
320 UNICODE_STRING EnumU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Enum");
321 UNICODE_STRING ServicesU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Services");
322 PPLUGPLAY_EVENT_BLOCK PnpEvent
;
323 OBJECT_ATTRIBUTES ObjectAttributes
;
326 HANDLE hEnum
, hServices
;
329 hInf
= *(HINF
*)lpParameter
;
331 InitializeObjectAttributes(&ObjectAttributes
, &EnumU
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
332 Status
= NtOpenKey(&hEnum
, KEY_QUERY_VALUE
, &ObjectAttributes
);
333 if (!NT_SUCCESS(Status
))
335 DPRINT1("NtOpenKey('%wZ') failed with status 0x%08lx\n", &EnumU
, Status
);
339 InitializeObjectAttributes(&ObjectAttributes
, &ServicesU
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
340 Status
= NtCreateKey(&hServices
, 0, &ObjectAttributes
, 0, NULL
, 0, NULL
);
341 if (!NT_SUCCESS(Status
))
343 DPRINT1("NtCreateKey('%wZ') failed with status 0x%08lx\n", &ServicesU
, Status
);
348 PnpEventSize
= 0x1000;
349 PnpEvent
= (PPLUGPLAY_EVENT_BLOCK
)RtlAllocateHeap(ProcessHeap
, 0, PnpEventSize
);
350 if (PnpEvent
== NULL
)
354 return STATUS_NO_MEMORY
;
359 DPRINT("Calling NtGetPlugPlayEvent()\n");
361 /* Wait for the next pnp event */
362 Status
= NtGetPlugPlayEvent(0, 0, PnpEvent
, PnpEventSize
);
364 /* Resize the buffer for the PnP event if it's too small. */
365 if (Status
== STATUS_BUFFER_TOO_SMALL
)
367 PnpEventSize
+= 0x400;
368 RtlFreeHeap(ProcessHeap
, 0, PnpEvent
);
369 PnpEvent
= (PPLUGPLAY_EVENT_BLOCK
)RtlAllocateHeap(ProcessHeap
, 0, PnpEventSize
);
370 if (PnpEvent
== NULL
)
374 return STATUS_NO_MEMORY
;
379 if (!NT_SUCCESS(Status
))
381 DPRINT("NtPlugPlayEvent() failed (Status %lx)\n", Status
);
385 /* Process the pnp event */
386 DPRINT("Received PnP Event\n");
387 if (IsEqualIID(&PnpEvent
->EventGuid
, (REFGUID
)&GUID_DEVICE_ENUMERATED
))
389 DPRINT("Device arrival event: %S\n", PnpEvent
->TargetDevice
.DeviceIds
);
390 InstallDevice(hInf
, hEnum
, hServices
, PnpEvent
->TargetDevice
.DeviceIds
);
394 DPRINT("Unknown event\n");
397 /* Dequeue the current pnp event and signal the next one */
398 NtPlugPlayControl(PlugPlayControlUserResponse
, NULL
, 0);
401 RtlFreeHeap(ProcessHeap
, 0, PnpEvent
);
405 return STATUS_SUCCESS
;
409 PnpEventThread(IN LPVOID lpParameter
)
412 Status
= EventThread(lpParameter
);
413 NtTerminateThread(NtCurrentThread(), Status
);