Allow more than one USB controller (was a XBOX hack no more needed)
[reactos.git] / reactos / drivers / usb / miniport / common / main.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS USB miniport driver (Cromwell type)
4 * FILE: drivers/usb/miniport/common/main.c
5 * PURPOSE: Driver entry
6 *
7 * PROGRAMMERS: Aleksey Bragin (aleksey@reactos.com)
8 * Hervé Poussineau (hpoussin@reactos.org),
9 *
10 * Some parts of code are inspired (or even just copied) from
11 * ReactOS Videoport driver (drivers/video/videoprt)
12 */
13
14 #define NDEBUG
15 #include <debug.h>
16
17 #define INITGUID
18 #include "usbcommon.h"
19
20 // data for embedded drivers
21 CONNECT_DATA KbdClassInformation;
22 CONNECT_DATA MouseClassInformation;
23 PDEVICE_OBJECT KeyboardFdo = NULL;
24 PDEVICE_OBJECT MouseFdo = NULL;
25
26 static NTSTATUS
27 CreateRootHubPdo(
28 IN PDRIVER_OBJECT DriverObject,
29 IN PDEVICE_OBJECT Fdo,
30 OUT PDEVICE_OBJECT* pPdo)
31 {
32 PDEVICE_OBJECT Pdo;
33 PUSBMP_DEVICE_EXTENSION DeviceExtension;
34 NTSTATUS Status;
35
36 DPRINT("USBMP: CreateRootHubPdo()\n");
37
38 Status = IoCreateDevice(
39 DriverObject,
40 sizeof(USBMP_DEVICE_EXTENSION),
41 NULL, /* DeviceName */
42 FILE_DEVICE_BUS_EXTENDER,
43 FILE_DEVICE_SECURE_OPEN | FILE_AUTOGENERATED_DEVICE_NAME,
44 FALSE,
45 &Pdo);
46 if (!NT_SUCCESS(Status))
47 {
48 DPRINT("USBMP: IoCreateDevice() call failed with status 0x%08x\n", Status);
49 return Status;
50 }
51
52 Pdo->Flags |= DO_BUS_ENUMERATED_DEVICE;
53 Pdo->Flags |= DO_POWER_PAGABLE;
54
55 // zerofill device extension
56 DeviceExtension = (PUSBMP_DEVICE_EXTENSION)Pdo->DeviceExtension;
57 RtlZeroMemory(DeviceExtension, sizeof(USBMP_DEVICE_EXTENSION));
58
59 DeviceExtension->IsFDO = FALSE;
60 DeviceExtension->FunctionalDeviceObject = Fdo;
61
62 Pdo->Flags &= ~DO_DEVICE_INITIALIZING;
63
64 *pPdo = Pdo;
65 return STATUS_SUCCESS;
66 }
67
68 static NTSTATUS
69 AddRegistryEntry(
70 IN PCWSTR PortTypeName,
71 IN PUNICODE_STRING DeviceName,
72 IN PCWSTR RegistryPath)
73 {
74 UNICODE_STRING PathU = RTL_CONSTANT_STRING(L"\\REGISTRY\\MACHINE\\HARDWARE\\DEVICEMAP");
75 OBJECT_ATTRIBUTES ObjectAttributes;
76 HANDLE hDeviceMapKey = (HANDLE)-1;
77 HANDLE hPortKey = (HANDLE)-1;
78 UNICODE_STRING PortTypeNameU;
79 NTSTATUS Status;
80
81 InitializeObjectAttributes(&ObjectAttributes, &PathU, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, NULL);
82 Status = ZwOpenKey(&hDeviceMapKey, 0, &ObjectAttributes);
83 if (!NT_SUCCESS(Status))
84 {
85 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status);
86 goto cleanup;
87 }
88
89 RtlInitUnicodeString(&PortTypeNameU, PortTypeName);
90 InitializeObjectAttributes(&ObjectAttributes, &PortTypeNameU, OBJ_KERNEL_HANDLE, hDeviceMapKey, NULL);
91 Status = ZwCreateKey(&hPortKey, KEY_SET_VALUE, &ObjectAttributes, 0, NULL, REG_OPTION_VOLATILE, NULL);
92 if (!NT_SUCCESS(Status))
93 {
94 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status);
95 goto cleanup;
96 }
97
98 Status = ZwSetValueKey(hPortKey, DeviceName, 0, REG_SZ, (PVOID)RegistryPath, wcslen(RegistryPath) * sizeof(WCHAR) + sizeof(UNICODE_NULL));
99 if (!NT_SUCCESS(Status))
100 {
101 DPRINT("ZwSetValueKey() failed with status 0x%08lx\n", Status);
102 goto cleanup;
103 }
104
105 Status = STATUS_SUCCESS;
106
107 cleanup:
108 if (hDeviceMapKey != (HANDLE)-1)
109 ZwClose(hDeviceMapKey);
110 if (hPortKey != (HANDLE)-1)
111 ZwClose(hPortKey);
112 return Status;
113 }
114
115 static NTSTATUS
116 AddDevice_Keyboard(
117 IN PDRIVER_OBJECT DriverObject,
118 IN PDEVICE_OBJECT Pdo)
119 {
120 UNICODE_STRING DeviceName = RTL_CONSTANT_STRING(L"\\Device\\KeyboardPortUSB");
121 PDEVICE_OBJECT Fdo;
122 NTSTATUS Status;
123
124 Status = AddRegistryEntry(L"KeyboardPort", &DeviceName, L"REGISTRY\\MACHINE\\SYSTEM\\CurrentControlSet\\Services\\usbport");
125 if (!NT_SUCCESS(Status))
126 {
127 DPRINT1("USBMP: AddRegistryEntry() for usb keyboard driver failed with status 0x%08lx\n", Status);
128 return Status;
129 }
130
131 Status = IoCreateDevice(DriverObject,
132 8, // debug
133 &DeviceName,
134 FILE_DEVICE_KEYBOARD,
135 FILE_DEVICE_SECURE_OPEN,
136 TRUE,
137 &Fdo);
138
139 if (!NT_SUCCESS(Status))
140 {
141 DPRINT1("USBMP: IoCreateDevice() for usb keyboard driver failed with status 0x%08lx\n", Status);
142 return Status;
143 }
144 KeyboardFdo = Fdo;
145 Fdo->Flags &= ~DO_DEVICE_INITIALIZING;
146 DPRINT("USBMP: Created keyboard Fdo: %p\n", Fdo);
147
148 return STATUS_SUCCESS;
149 }
150
151 static NTSTATUS
152 AddDevice_Mouse(
153 IN PDRIVER_OBJECT DriverObject,
154 IN PDEVICE_OBJECT Pdo)
155 {
156 UNICODE_STRING DeviceName = RTL_CONSTANT_STRING(L"\\Device\\PointerPortUSB");
157 PDEVICE_OBJECT Fdo;
158 NTSTATUS Status;
159
160 Status = AddRegistryEntry(L"PointerPort", &DeviceName, L"REGISTRY\\MACHINE\\SYSTEM\\CurrentControlSet\\Services\\usbport");
161 if (!NT_SUCCESS(Status))
162 {
163 DPRINT1("USBMP: AddRegistryEntry() for usb mouse driver failed with status 0x%08lx\n", Status);
164 return Status;
165 }
166
167 Status = IoCreateDevice(DriverObject,
168 8, // debug
169 &DeviceName,
170 FILE_DEVICE_MOUSE,
171 FILE_DEVICE_SECURE_OPEN,
172 TRUE,
173 &Fdo);
174
175 if (!NT_SUCCESS(Status))
176 {
177 DPRINT1("USBMP: IoCreateDevice() for usb mouse driver failed with status 0x%08lx\n", Status);
178 return Status;
179 }
180 MouseFdo = Fdo;
181 Fdo->Flags &= ~DO_DEVICE_INITIALIZING;
182 DPRINT("USBMP: Created mouse Fdo: %p\n", Fdo);
183
184 return STATUS_SUCCESS;
185 }
186
187 NTSTATUS STDCALL
188 AddDevice(
189 IN PDRIVER_OBJECT DriverObject,
190 IN PDEVICE_OBJECT pdo)
191 {
192 PDEVICE_OBJECT fdo;
193 NTSTATUS Status;
194 WCHAR DeviceBuffer[20];
195 WCHAR LinkDeviceBuffer[20];
196 UNICODE_STRING DeviceName;
197 UNICODE_STRING LinkDeviceName;
198 PUSBMP_DRIVER_EXTENSION DriverExtension;
199 PUSBMP_DEVICE_EXTENSION DeviceExtension;
200 ULONG DeviceNumber;
201
202 // Allocate driver extension now
203 DriverExtension = IoGetDriverObjectExtension(DriverObject, DriverObject);
204 if (DriverExtension == NULL)
205 {
206 Status = IoAllocateDriverObjectExtension(
207 DriverObject,
208 DriverObject,
209 sizeof(USBMP_DRIVER_EXTENSION),
210 (PVOID *)&DriverExtension);
211
212 if (!NT_SUCCESS(Status))
213 {
214 DPRINT("USBMP: Allocating DriverObjectExtension failed.\n");
215 return Status;
216 }
217 }
218
219 // Create a unicode device name
220 DeviceNumber = 0; //TODO: Allocate new device number every time
221 swprintf(DeviceBuffer, L"\\Device\\USBFDO-%lu", DeviceNumber);
222 RtlInitUnicodeString(&DeviceName, DeviceBuffer);
223
224 Status = IoCreateDevice(DriverObject,
225 sizeof(USBMP_DEVICE_EXTENSION),
226 &DeviceName,
227 FILE_DEVICE_BUS_EXTENDER,
228 0,
229 FALSE,
230 &fdo);
231
232 if (!NT_SUCCESS(Status))
233 {
234 DPRINT("USBMP: IoCreateDevice call failed with status 0x%08lx\n", Status);
235 return Status;
236 }
237
238 // zerofill device extension
239 DeviceExtension = (PUSBMP_DEVICE_EXTENSION)fdo->DeviceExtension;
240 RtlZeroMemory(DeviceExtension, sizeof(USBMP_DEVICE_EXTENSION));
241
242 /* Create root hub Pdo */
243 Status = CreateRootHubPdo(DriverObject, fdo, &DeviceExtension->RootHubPdo);
244 if (!NT_SUCCESS(Status))
245 {
246 DPRINT("USBMP: CreateRootHubPdo() failed with status 0x%08lx\n", Status);
247 IoDeleteDevice(fdo);
248 return Status;
249 }
250
251 /* Register device interface for controller */
252 Status = IoRegisterDeviceInterface(
253 pdo,
254 &GUID_DEVINTERFACE_USB_HOST_CONTROLLER,
255 NULL,
256 &DeviceExtension->HcdInterfaceName);
257 if (!NT_SUCCESS(Status))
258 {
259 DPRINT("USBMP: IoRegisterDeviceInterface() failed with status 0x%08lx\n", Status);
260 IoDeleteDevice(DeviceExtension->RootHubPdo);
261 IoDeleteDevice(fdo);
262 return Status;
263 }
264
265 DeviceExtension->NextDeviceObject = IoAttachDeviceToDeviceStack(fdo, pdo);
266
267 // Initialize device extension
268 DeviceExtension->IsFDO = TRUE;
269 DeviceExtension->DeviceNumber = DeviceNumber;
270 DeviceExtension->PhysicalDeviceObject = pdo;
271 DeviceExtension->FunctionalDeviceObject = fdo;
272 DeviceExtension->DriverExtension = DriverExtension;
273
274 fdo->Flags &= ~DO_DEVICE_INITIALIZING;
275
276 /* FIXME: do a loop to find an available number */
277 swprintf(LinkDeviceBuffer, L"\\??\\HCD%lu", 0);
278
279 RtlInitUnicodeString(&LinkDeviceName, LinkDeviceBuffer);
280
281 Status = IoCreateSymbolicLink(&LinkDeviceName, &DeviceName);
282
283 if (NT_SUCCESS(Status))
284 Status = AddDevice_Keyboard(DriverObject, pdo);
285 if (NT_SUCCESS(Status))
286 Status = AddDevice_Mouse(DriverObject, pdo);
287
288 if (!NT_SUCCESS(Status))
289 {
290 DPRINT("USBMP: IoCreateSymbolicLink() call failed with status 0x%08x\n", Status);
291 IoDeleteDevice(DeviceExtension->RootHubPdo);
292 IoDeleteDevice(fdo);
293 return Status;
294 }
295
296
297 return STATUS_SUCCESS;
298 }
299
300 NTSTATUS STDCALL
301 IrpStub(
302 IN PDEVICE_OBJECT DeviceObject,
303 IN PIRP Irp)
304 {
305 NTSTATUS Status = STATUS_NOT_SUPPORTED;
306
307 if (((PUSBMP_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsFDO)
308 {
309 DPRINT1("USBMP: FDO stub for major function 0x%lx\n",
310 IoGetCurrentIrpStackLocation(Irp)->MajorFunction);
311 #ifndef NDEBUG
312 DbgBreakPoint();
313 #endif
314 return ForwardIrpAndForget(DeviceObject, Irp);
315 }
316 else
317 {
318 /* We can't forward request to the lower driver, because
319 * we are a Pdo, so we don't have lower driver...
320 */
321 DPRINT1("USBMP: PDO stub for major function 0x%lx\n",
322 IoGetCurrentIrpStackLocation(Irp)->MajorFunction);
323 #ifndef NDEBUG
324 DbgBreakPoint();
325 #endif
326 }
327
328 Status = Irp->IoStatus.Status;
329 IoCompleteRequest(Irp, IO_NO_INCREMENT);
330 return Status;
331 }
332
333 static NTSTATUS STDCALL
334 DispatchCreate(PDEVICE_OBJECT DeviceObject, PIRP Irp)
335 {
336 if (((PUSBMP_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsFDO)
337 return UsbMpCreate(DeviceObject, Irp);
338 else
339 return IrpStub(DeviceObject, Irp);
340 }
341
342 static NTSTATUS STDCALL
343 DispatchClose(PDEVICE_OBJECT DeviceObject, PIRP Irp)
344 {
345 if (((PUSBMP_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsFDO)
346 return UsbMpClose(DeviceObject, Irp);
347 else
348 return IrpStub(DeviceObject, Irp);
349 }
350
351 static NTSTATUS STDCALL
352 DispatchCleanup(PDEVICE_OBJECT DeviceObject, PIRP Irp)
353 {
354 if (((PUSBMP_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsFDO)
355 return UsbMpCleanup(DeviceObject, Irp);
356 else
357 return IrpStub(DeviceObject, Irp);
358 }
359
360 static NTSTATUS STDCALL
361 DispatchDeviceControl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
362 {
363 if (((PUSBMP_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsFDO)
364 return UsbMpDeviceControlFdo(DeviceObject, Irp);
365 else
366 return UsbMpDeviceControlPdo(DeviceObject, Irp);
367 }
368
369 static NTSTATUS STDCALL
370 DispatchInternalDeviceControl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
371 {
372 if (((PUSBMP_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsFDO)
373 return UsbMpInternalDeviceControlFdo(DeviceObject, Irp);
374 else
375 return IrpStub(DeviceObject, Irp);
376 }
377
378 static NTSTATUS STDCALL
379 DispatchPnp(PDEVICE_OBJECT DeviceObject, PIRP Irp)
380 {
381 if (((PUSBMP_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsFDO)
382 return UsbMpPnpFdo(DeviceObject, Irp);
383 else
384 return UsbMpPnpPdo(DeviceObject, Irp);
385 }
386
387 static NTSTATUS STDCALL
388 DispatchPower(PDEVICE_OBJECT fido, PIRP Irp)
389 {
390 DPRINT1("USBMP: IRP_MJ_POWER unimplemented\n");
391 Irp->IoStatus.Information = 0;
392 Irp->IoStatus.Status = STATUS_SUCCESS;
393 IoCompleteRequest(Irp, IO_NO_INCREMENT);
394 return STATUS_SUCCESS;
395 }
396
397 /*
398 * Standard DriverEntry method.
399 */
400 NTSTATUS STDCALL
401 DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegPath)
402 {
403 USBPORT_INTERFACE UsbPortInterface;
404 ULONG i;
405
406 DriverObject->DriverUnload = DriverUnload;
407 DriverObject->DriverExtension->AddDevice = AddDevice;
408
409 for (i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++)
410 DriverObject->MajorFunction[i] = IrpStub;
411
412 DriverObject->MajorFunction[IRP_MJ_CREATE] = DispatchCreate;
413 DriverObject->MajorFunction[IRP_MJ_CLOSE] = DispatchClose;
414 DriverObject->MajorFunction[IRP_MJ_CLEANUP] = DispatchCleanup;
415 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DispatchDeviceControl;
416 DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = DispatchInternalDeviceControl;
417 DriverObject->MajorFunction[IRP_MJ_PNP] = DispatchPnp;
418 DriverObject->MajorFunction[IRP_MJ_POWER] = DispatchPower;
419
420 // Register in usbcore.sys
421 UsbPortInterface.KbdConnectData = &KbdClassInformation;
422 UsbPortInterface.MouseConnectData = &MouseClassInformation;
423
424 KbdClassInformation.ClassService = NULL;
425 KbdClassInformation.ClassDeviceObject = NULL;
426 MouseClassInformation.ClassService = NULL;
427 MouseClassInformation.ClassDeviceObject = NULL;
428
429 RegisterPortDriver(DriverObject, &UsbPortInterface);
430
431 return STATUS_SUCCESS;
432 }