- Add registry entry in HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP for each keyboard class DO
- Don't search for legacy port drivers in DriverEntry, but in first AddDevice
- Don't dereference non-referenced objects
Modified: trunk/reactos/drivers/input/kbdclass/kbdclass.c
Modified: trunk/reactos/drivers/input/kbdclass/kbdclass.h

Modified: trunk/reactos/drivers/input/kbdclass/kbdclass.c
--- trunk/reactos/drivers/input/kbdclass/kbdclass.c	2005-11-09 11:13:02 UTC (rev 19093)
+++ trunk/reactos/drivers/input/kbdclass/kbdclass.c	2005-11-09 11:15:42 UTC (rev 19094)
@@ -13,6 +13,11 @@
 #define INITGUID
 #include "kbdclass.h"
 
+static NTSTATUS
+SearchForLegacyDrivers(
+	IN PDRIVER_OBJECT DriverObject,
+	IN PCLASS_DRIVER_EXTENSION DriverExtension);
+
 static VOID NTAPI
 DriverUnload(IN PDRIVER_OBJECT DriverObject)
 {
@@ -206,7 +211,7 @@
 	Parameters[2].EntryContext = &DriverExtension->DeviceBaseName;
 	Parameters[2].DefaultType = REG_SZ;
 	Parameters[2].DefaultData = &DefaultDeviceBaseName;
-	Parameters[2].DefaultLength = sizeof(ULONG);
+	Parameters[2].DefaultLength = 0;
 
 	Status = RtlQueryRegistryValues(
 		RTL_REGISTRY_ABSOLUTE,
@@ -327,7 +332,14 @@
 	Fdo->Flags |= DO_POWER_PAGABLE | DO_BUFFERED_IO;
 	Fdo->Flags &= ~DO_DEVICE_INITIALIZING;
 
-	/* FIXME: create registry entry in HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP */
+	/* Add entry entry to HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\[DeviceBaseName] */
+	RtlWriteRegistryValue(
+		RTL_REGISTRY_DEVICEMAP,
+		DriverExtension->DeviceBaseName.Buffer,
+		DeviceNameU.Buffer,
+		REG_SZ,
+		DriverExtension->RegistryPath.Buffer,
+		DriverExtension->RegistryPath.MaximumLength);
 
 	/* HACK: 1st stage setup needs a keyboard to open it in user-mode
 	 * Create a link to user space... */
@@ -464,7 +476,7 @@
 	else
 		IoStatus.Status = Status;
 
-	if (NT_SUCCESS(Status))
+	if (NT_SUCCESS(IoStatus.Status))
 		ObReferenceObject(PortDO);
 
 	return IoStatus.Status;
@@ -482,11 +494,13 @@
 
 	DPRINT("ClassAddDevice called. Pdo = 0x%p\n", Pdo);
 
+	DriverExtension = IoGetDriverObjectExtension(DriverObject, DriverObject);
+
 	if (Pdo == NULL)
-		return STATUS_SUCCESS;
+		/* We're getting a NULL Pdo at the first call as we're a legacy driver.
+		 * Use it to search for legacy port drivers. */
+		return SearchForLegacyDrivers(DriverObject, DriverExtension);
 
-	DriverExtension = IoGetDriverObjectExtension(DriverObject, DriverObject);
-
 	/* Create new device object */
 	Status = IoCreateDevice(
 		DriverObject,
@@ -686,7 +700,6 @@
 			{
 				/* FIXME: Log the error */
 				DPRINT("ConnectPortDriver() failed with status 0x%08lx\n", Status);
-				ObDereferenceObject(PortDeviceObject);
 			}
 		}
 		else
@@ -697,7 +710,6 @@
 			{
 				/* FIXME: Log the error */
 				DPRINT("CreatePointerClassDeviceObject() failed with status 0x%08lx\n", Status);
-				ObDereferenceObject(PortDeviceObject);
 				continue;
 			}
 			Status = ConnectPortDriver(PortDeviceObject, ClassDO);
@@ -705,7 +717,6 @@
 			{
 				/* FIXME: Log the error */
 				DPRINT("ConnectPortDriver() failed with status 0x%08lx\n", Status);
-				ObDereferenceObject(PortDeviceObject);
 				IoDeleteDevice(ClassDO);
 			}
 		}
@@ -747,6 +758,16 @@
 	}
 	RtlZeroMemory(DriverExtension, sizeof(CLASS_DRIVER_EXTENSION));
 
+	Status = RtlDuplicateUnicodeString(
+		RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE,
+		RegistryPath,
+		&DriverExtension->RegistryPath);
+	if (!NT_SUCCESS(Status))
+	{
+		DPRINT("RtlDuplicateUnicodeString() failed with status 0x%08lx\n", Status);
+		return Status;
+	}
+
 	Status = ReadRegistryEntries(RegistryPath, DriverExtension);
 	if (!NT_SUCCESS(Status))
 	{
@@ -779,7 +800,5 @@
 	DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = ClassDeviceControl;
 	DriverObject->DriverStartIo                        = ClassStartIo;
 
-	Status = SearchForLegacyDrivers(DriverObject, DriverExtension);
-
-	return Status;
+	return STATUS_SUCCESS;
 }

Modified: trunk/reactos/drivers/input/kbdclass/kbdclass.h
--- trunk/reactos/drivers/input/kbdclass/kbdclass.h	2005-11-09 11:13:02 UTC (rev 19093)
+++ trunk/reactos/drivers/input/kbdclass/kbdclass.h	2005-11-09 11:15:42 UTC (rev 19094)
@@ -24,6 +24,8 @@
 
 typedef struct _CLASS_DRIVER_EXTENSION
 {
+	UNICODE_STRING RegistryPath;
+
 	/* Registry settings */
 	ULONG ConnectMultiplePorts;
 	ULONG DataQueueSize;