Allow more than one USB controller
Modified: trunk/reactos/drivers/usb/miniport/common/main.c

Modified: trunk/reactos/drivers/usb/miniport/common/main.c
--- trunk/reactos/drivers/usb/miniport/common/main.c	2005-12-24 17:50:01 UTC (rev 20322)
+++ trunk/reactos/drivers/usb/miniport/common/main.c	2005-12-24 17:56:23 UTC (rev 20323)
@@ -197,7 +197,8 @@
 	UNICODE_STRING LinkDeviceName;
 	PUSBMP_DRIVER_EXTENSION DriverExtension;
 	PUSBMP_DEVICE_EXTENSION DeviceExtension;
-	ULONG DeviceNumber;
+	static ULONG DeviceNumber = 0;
+	BOOL AlreadyRestarted = FALSE;
 
 	// Allocate driver extension now
 	DriverExtension = IoGetDriverObjectExtension(DriverObject, DriverObject);
@@ -216,18 +217,33 @@
 		}
 	}
 
-	// Create a unicode device name
-	DeviceNumber = 0; //TODO: Allocate new device number every time
-	swprintf(DeviceBuffer, L"\\Device\\USBFDO-%lu", DeviceNumber);
-	RtlInitUnicodeString(&DeviceName, DeviceBuffer);
+	/* Create a unicode device name. Allocate a new device number every time */
+	do
+	{
+		DeviceNumber++;
+		if (DeviceNumber == 9999)
+		{
+			/* Hmm. We don't have a free number. */ 
+			if (AlreadyRestarted)
+			{
+				Status = STATUS_UNSUCCESSFUL;
+				break;
+			}
+			/* Start again at DeviceNumber = 0 to find a free number */
+			DeviceNumber = 0;
+			AlreadyRestarted = TRUE;
+		}
+		swprintf(DeviceBuffer, L"\\Device\\USBFDO-%lu", DeviceNumber);
+		RtlInitUnicodeString(&DeviceName, DeviceBuffer);
 
-	Status = IoCreateDevice(DriverObject,
-				sizeof(USBMP_DEVICE_EXTENSION),
-				&DeviceName,
-				FILE_DEVICE_BUS_EXTENDER,
-				0,
-				FALSE,
-				&fdo);
+		Status = IoCreateDevice(DriverObject,
+					sizeof(USBMP_DEVICE_EXTENSION),
+					&DeviceName,
+					FILE_DEVICE_BUS_EXTENDER,
+					0,
+					FALSE,
+					&fdo);
+	} while (Status == STATUS_OBJECT_NAME_COLLISION);
 
 	if (!NT_SUCCESS(Status))
 	{