- Remove Mou and Mouse strings in names to share more code with kbdclass
[reactos.git] / reactos / drivers / input / mouclass / mouclass.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Mouse class driver
4 * FILE: drivers/mouclass/mouclass.c
5 * PURPOSE: Mouse class driver
6 *
7 * PROGRAMMERS: Hervé Poussineau (hpoussin@reactos.org)
8 */
9
10 #define NDEBUG
11 #include <debug.h>
12
13 #define INITGUID
14 #include "mouclass.h"
15
16 static VOID NTAPI
17 DriverUnload(IN PDRIVER_OBJECT DriverObject)
18 {
19 // nothing to do here yet
20 }
21
22 static NTSTATUS NTAPI
23 ClassCreate(
24 IN PDEVICE_OBJECT DeviceObject,
25 IN PIRP Irp)
26 {
27 DPRINT("IRP_MJ_CREATE\n");
28
29 if (!((PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsClassDO)
30 return ForwardIrpAndForget(DeviceObject, Irp);
31
32 /* FIXME: open all associated Port devices */
33 return STATUS_SUCCESS;
34 }
35
36 static NTSTATUS NTAPI
37 ClassClose(
38 IN PDEVICE_OBJECT DeviceObject,
39 IN PIRP Irp)
40 {
41 DPRINT("IRP_MJ_CLOSE\n");
42
43 if (!((PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsClassDO)
44 return ForwardIrpAndForget(DeviceObject, Irp);
45
46 /* FIXME: close all associated Port devices */
47 return STATUS_SUCCESS;
48 }
49
50 static NTSTATUS NTAPI
51 ClassCleanup(
52 IN PDEVICE_OBJECT DeviceObject,
53 IN PIRP Irp)
54 {
55 DPRINT("IRP_MJ_CLEANUP\n");
56
57 if (!((PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsClassDO)
58 return ForwardIrpAndForget(DeviceObject, Irp);
59
60 /* FIXME: cleanup all associated Port devices */
61 return STATUS_SUCCESS;
62 }
63
64 static NTSTATUS NTAPI
65 ClassRead(
66 IN PDEVICE_OBJECT DeviceObject,
67 IN PIRP Irp)
68 {
69 DPRINT("IRP_MJ_READ\n");
70
71 if (!((PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsClassDO)
72 return ForwardIrpAndForget(DeviceObject, Irp);
73
74 if (IoGetCurrentIrpStackLocation(Irp)->Parameters.Read.Length < sizeof(MOUSE_INPUT_DATA))
75 {
76 Irp->IoStatus.Status = STATUS_BUFFER_TOO_SMALL;
77 Irp->IoStatus.Information = 0;
78 IoCompleteRequest(Irp, IO_NO_INCREMENT);
79
80 return STATUS_BUFFER_TOO_SMALL;
81 }
82
83 IoMarkIrpPending(Irp);
84 IoStartPacket(DeviceObject, Irp, NULL, NULL);
85 return STATUS_PENDING;
86 }
87
88 static NTSTATUS NTAPI
89 ClassDeviceControl(
90 IN PDEVICE_OBJECT DeviceObject,
91 IN PIRP Irp)
92 {
93 PCLASS_DEVICE_EXTENSION DeviceExtension;
94 NTSTATUS Status;
95
96 DPRINT("IRP_MJ_DEVICE_CONTROL\n");
97
98 if (!((PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsClassDO)
99 return ForwardIrpAndForget(DeviceObject, Irp);
100
101 DeviceExtension = (PCLASS_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
102
103 switch (IoGetCurrentIrpStackLocation(Irp)->Parameters.DeviceIoControl.IoControlCode)
104 {
105 case IOCTL_MOUSE_QUERY_ATTRIBUTES:
106 /* FIXME */
107 Status = STATUS_NOT_SUPPORTED;
108 break;
109 default:
110 DPRINT1("IRP_MJ_DEVICE_CONTROL / unknown I/O control code 0x%lx\n",
111 IoGetCurrentIrpStackLocation(Irp)->Parameters.DeviceIoControl.IoControlCode);
112 Status = STATUS_NOT_SUPPORTED;
113 }
114
115 Irp->IoStatus.Status = Status;
116 Irp->IoStatus.Information = 0;
117 IoCompleteRequest(Irp, IO_NO_INCREMENT);
118
119 return Status;
120 }
121
122 static NTSTATUS NTAPI
123 IrpStub(
124 IN PDEVICE_OBJECT DeviceObject,
125 IN PIRP Irp)
126 {
127 NTSTATUS Status = STATUS_NOT_SUPPORTED;
128
129 if (!((PCOMMON_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->IsClassDO)
130 {
131 /* Forward some IRPs to lower device */
132 switch (IoGetCurrentIrpStackLocation(Irp)->MajorFunction)
133 {
134 case IRP_MJ_INTERNAL_DEVICE_CONTROL:
135 return ForwardIrpAndForget(DeviceObject, Irp);
136 default:
137 {
138 DPRINT1("Port DO stub for major function 0x%lx\n",
139 IoGetCurrentIrpStackLocation(Irp)->MajorFunction);
140 ASSERT(FALSE);
141 Status = Irp->IoStatus.Status;
142 }
143 }
144 }
145 else
146 {
147 DPRINT1("Class DO stub for major function 0x%lx\n",
148 IoGetCurrentIrpStackLocation(Irp)->MajorFunction);
149 ASSERT(FALSE);
150 Status = Irp->IoStatus.Status;
151 }
152
153 Irp->IoStatus.Status = Status;
154 IoCompleteRequest(Irp, IO_NO_INCREMENT);
155 return Status;
156 }
157
158 static NTSTATUS
159 ReadRegistryEntries(
160 IN PUNICODE_STRING RegistryPath,
161 IN PCLASS_DRIVER_EXTENSION DriverExtension)
162 {
163 UNICODE_STRING ParametersRegistryKey;
164 RTL_QUERY_REGISTRY_TABLE Parameters[4];
165 NTSTATUS Status;
166
167 ULONG DefaultConnectMultiplePorts = 1;
168 ULONG DefaultDataQueueSize = 0x64;
169 UNICODE_STRING DefaultDeviceBaseName = RTL_CONSTANT_STRING(L"PointerClass");
170
171 ParametersRegistryKey.Length = 0;
172 ParametersRegistryKey.MaximumLength = RegistryPath->Length + sizeof(L"\\Parameters") + sizeof(UNICODE_NULL);
173 ParametersRegistryKey.Buffer = ExAllocatePool(PagedPool, ParametersRegistryKey.MaximumLength);
174 if (!ParametersRegistryKey.Buffer)
175 {
176 DPRINT("ExAllocatePool() failed\n");
177 return STATUS_INSUFFICIENT_RESOURCES;
178 }
179 RtlCopyUnicodeString(&ParametersRegistryKey, RegistryPath);
180 RtlAppendUnicodeToString(&ParametersRegistryKey, L"\\Parameters");
181 ParametersRegistryKey.Buffer[ParametersRegistryKey.Length / sizeof(WCHAR)] = UNICODE_NULL;
182
183 RtlZeroMemory(Parameters, sizeof(Parameters));
184
185 Parameters[0].Flags = RTL_QUERY_REGISTRY_DIRECT | RTL_REGISTRY_OPTIONAL;
186 Parameters[0].Name = L"ConnectMultiplePorts";
187 Parameters[0].EntryContext = &DriverExtension->ConnectMultiplePorts;
188 Parameters[0].DefaultType = REG_DWORD;
189 Parameters[0].DefaultData = &DefaultConnectMultiplePorts;
190 Parameters[0].DefaultLength = sizeof(ULONG);
191
192 Parameters[1].Flags = RTL_QUERY_REGISTRY_DIRECT | RTL_REGISTRY_OPTIONAL;
193 Parameters[1].Name = L"MouseDataQueueSize";
194 Parameters[1].EntryContext = &DriverExtension->DataQueueSize;
195 Parameters[1].DefaultType = REG_DWORD;
196 Parameters[1].DefaultData = &DefaultDataQueueSize;
197 Parameters[1].DefaultLength = sizeof(ULONG);
198
199 Parameters[2].Flags = RTL_QUERY_REGISTRY_DIRECT | RTL_REGISTRY_OPTIONAL;
200 Parameters[2].Name = L"PointerDeviceBaseName";
201 Parameters[2].EntryContext = &DriverExtension->DeviceBaseName;
202 Parameters[2].DefaultType = REG_SZ;
203 Parameters[2].DefaultData = &DefaultDeviceBaseName;
204 Parameters[2].DefaultLength = sizeof(ULONG);
205
206 Status = RtlQueryRegistryValues(
207 RTL_REGISTRY_ABSOLUTE,
208 ParametersRegistryKey.Buffer,
209 Parameters,
210 NULL,
211 NULL);
212
213 if (NT_SUCCESS(Status))
214 {
215 /* Check values */
216 if (DriverExtension->ConnectMultiplePorts != 0
217 && DriverExtension->ConnectMultiplePorts != 1)
218 {
219 DriverExtension->ConnectMultiplePorts = DefaultConnectMultiplePorts;
220 }
221 if (DriverExtension->DataQueueSize == 0)
222 {
223 DriverExtension->DataQueueSize = DefaultDataQueueSize;
224 }
225 }
226 else if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
227 {
228 /* Registry path doesn't exist. Set defaults */
229 DriverExtension->ConnectMultiplePorts = DefaultConnectMultiplePorts;
230 DriverExtension->DataQueueSize = DefaultDataQueueSize;
231 Status = RtlDuplicateUnicodeString(
232 RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE,
233 &DefaultDeviceBaseName,
234 &DriverExtension->DeviceBaseName);
235 }
236
237 return Status;
238 }
239
240 static NTSTATUS
241 CreateClassDeviceObject(
242 IN PDRIVER_OBJECT DriverObject,
243 OUT PDEVICE_OBJECT *ClassDO OPTIONAL)
244 {
245 PCLASS_DRIVER_EXTENSION DriverExtension;
246 ULONG DeviceId = 0;
247 ULONG PrefixLength;
248 UNICODE_STRING DeviceNameU;
249 PWSTR DeviceIdW = NULL; /* Pointer into DeviceNameU.Buffer */
250 PDEVICE_OBJECT Fdo;
251 PCLASS_DEVICE_EXTENSION DeviceExtension;
252 NTSTATUS Status;
253
254 DPRINT("CreateClassDeviceObject(0x%p)\n", DriverObject);
255
256 /* Create new device object */
257 DriverExtension = IoGetDriverObjectExtension(DriverObject, DriverObject);
258 DeviceNameU.Length = 0;
259 DeviceNameU.MaximumLength =
260 wcslen(L"\\Device\\") * sizeof(WCHAR) /* "\Device\" */
261 + DriverExtension->DeviceBaseName.Length /* "PointerClass" */
262 + 4 * sizeof(WCHAR) /* Id between 0 and 9999 */
263 + sizeof(UNICODE_NULL); /* Final NULL char */
264 DeviceNameU.Buffer = ExAllocatePool(PagedPool, DeviceNameU.MaximumLength);
265 if (!DeviceNameU.Buffer)
266 {
267 DPRINT("ExAllocatePool() failed\n");
268 return STATUS_INSUFFICIENT_RESOURCES;
269 }
270 Status = RtlAppendUnicodeToString(&DeviceNameU, L"\\Device\\");
271 if (!NT_SUCCESS(Status))
272 {
273 DPRINT("RtlAppendUnicodeToString() failed with status 0x%08lx\n", Status);
274 goto cleanup;
275 }
276 Status = RtlAppendUnicodeStringToString(&DeviceNameU, &DriverExtension->DeviceBaseName);
277 if (!NT_SUCCESS(Status))
278 {
279 DPRINT("RtlAppendUnicodeStringToString() failed with status 0x%08lx\n", Status);
280 goto cleanup;
281 }
282 PrefixLength = DeviceNameU.MaximumLength - 4 * sizeof(WCHAR) - sizeof(UNICODE_NULL);
283 DeviceIdW = &DeviceNameU.Buffer[PrefixLength / sizeof(WCHAR)];
284 while (DeviceId < 9999)
285 {
286 DeviceNameU.Length = PrefixLength + swprintf(DeviceIdW, L"%lu", DeviceId) * sizeof(WCHAR);
287 Status = IoCreateDevice(
288 DriverObject,
289 sizeof(CLASS_DEVICE_EXTENSION),
290 &DeviceNameU,
291 FILE_DEVICE_MOUSE,
292 FILE_DEVICE_SECURE_OPEN,
293 TRUE,
294 &Fdo);
295 if (NT_SUCCESS(Status))
296 goto cleanup;
297 else if (Status != STATUS_OBJECT_NAME_COLLISION)
298 {
299 DPRINT("IoCreateDevice() failed with status 0x%08lx\n", Status);
300 goto cleanup;
301 }
302 DeviceId++;
303 }
304 DPRINT("Too much devices starting with '\\Device\\%wZ'\n", &DriverExtension->DeviceBaseName);
305 Status = STATUS_UNSUCCESSFUL;
306 cleanup:
307 if (!NT_SUCCESS(Status))
308 {
309 ExFreePool(DeviceNameU.Buffer);
310 return Status;
311 }
312
313 DeviceExtension = (PCLASS_DEVICE_EXTENSION)Fdo->DeviceExtension;
314 RtlZeroMemory(DeviceExtension, sizeof(CLASS_DEVICE_EXTENSION));
315 DeviceExtension->Common.IsClassDO = TRUE;
316 DeviceExtension->DriverExtension = DriverExtension;
317 KeInitializeSpinLock(&(DeviceExtension->SpinLock));
318 DeviceExtension->ReadIsPending = FALSE;
319 DeviceExtension->InputCount = 0;
320 DeviceExtension->PortData = ExAllocatePool(NonPagedPool, DeviceExtension->DriverExtension->DataQueueSize * sizeof(MOUSE_INPUT_DATA));
321 Fdo->Flags |= DO_POWER_PAGABLE;
322 Fdo->Flags &= ~DO_DEVICE_INITIALIZING;
323
324 /* FIXME: create registry entry in HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP */
325
326 ExFreePool(DeviceNameU.Buffer);
327
328 if (ClassDO)
329 *ClassDO = Fdo;
330
331 return STATUS_SUCCESS;
332 }
333
334 static BOOLEAN
335 ClassCallback(
336 IN PDEVICE_OBJECT ClassDeviceObject,
337 IN OUT PMOUSE_INPUT_DATA DataStart,
338 IN PMOUSE_INPUT_DATA DataEnd,
339 IN OUT PULONG ConsumedCount)
340 {
341 PCLASS_DEVICE_EXTENSION ClassDeviceExtension = ClassDeviceObject->DeviceExtension;
342 PIRP Irp = NULL;
343 KIRQL OldIrql;
344 PIO_STACK_LOCATION Stack;
345 ULONG InputCount = DataEnd - DataStart;
346 ULONG ReadSize;
347
348 ASSERT(ClassDeviceExtension->Common.IsClassDO);
349
350 DPRINT("ClassCallback()\n");
351 /* A filter driver might have consumed all the data already; I'm
352 * not sure if they are supposed to move the packets when they
353 * consume them though.
354 */
355 if (ClassDeviceExtension->ReadIsPending == TRUE && InputCount)
356 {
357 Irp = ClassDeviceObject->CurrentIrp;
358 ClassDeviceObject->CurrentIrp = NULL;
359 Stack = IoGetCurrentIrpStackLocation(Irp);
360
361 /* A read request is waiting for input, so go straight to it */
362 /* FIXME: use SEH */
363 RtlCopyMemory(
364 Irp->MdlAddress ? MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority) : Irp->UserBuffer,
365 DataStart,
366 sizeof(MOUSE_INPUT_DATA));
367
368 /* Go to next packet and complete this request with STATUS_SUCCESS */
369 Irp->IoStatus.Status = STATUS_SUCCESS;
370 Irp->IoStatus.Information = sizeof(MOUSE_INPUT_DATA);
371 Stack->Parameters.Read.Length = sizeof(MOUSE_INPUT_DATA);
372
373 ClassDeviceExtension->ReadIsPending = FALSE;
374
375 /* Skip the packet we just sent away */
376 DataStart++;
377 (*ConsumedCount)++;
378 InputCount--;
379 }
380
381 /* If we have data from the port driver and a higher service to send the data to */
382 if (InputCount != 0)
383 {
384 KeAcquireSpinLock(&ClassDeviceExtension->SpinLock, &OldIrql);
385
386 if (ClassDeviceExtension->InputCount + InputCount > ClassDeviceExtension->DriverExtension->DataQueueSize)
387 ReadSize = ClassDeviceExtension->DriverExtension->DataQueueSize - ClassDeviceExtension->InputCount;
388 else
389 ReadSize = InputCount;
390
391 /*
392 * FIXME: If we exceed the buffer, data gets thrown away.. better
393 * solution?
394 */
395
396 /*
397 * Move the input data from the port data queue to our class data
398 * queue.
399 */
400 RtlMoveMemory(
401 ClassDeviceExtension->PortData,
402 (PCHAR)DataStart,
403 sizeof(MOUSE_INPUT_DATA) * ReadSize);
404
405 /* Move the pointer and counter up */
406 ClassDeviceExtension->PortData += ReadSize;
407 ClassDeviceExtension->InputCount += ReadSize;
408
409 KeReleaseSpinLock(&ClassDeviceExtension->SpinLock, OldIrql);
410 (*ConsumedCount) += ReadSize;
411 }
412 else
413 {
414 DPRINT("ClassCallBack() entered, InputCount = %lu - DOING NOTHING\n", InputCount);
415 }
416
417 if (Irp != NULL)
418 {
419 IoStartNextPacket(ClassDeviceObject, FALSE);
420 IoCompleteRequest(Irp, IO_MOUSE_INCREMENT);
421 }
422
423 DPRINT("Leaving ClassCallback()\n");
424 return TRUE;
425 }
426
427 /* Send IOCTL_INTERNAL_*_CONNECT to port */
428 static NTSTATUS
429 ConnectPortDriver(
430 IN PDEVICE_OBJECT PortDO,
431 IN PDEVICE_OBJECT ClassDO)
432 {
433 KEVENT Event;
434 PIRP Irp;
435 IO_STATUS_BLOCK IoStatus;
436 CONNECT_DATA ConnectData;
437 NTSTATUS Status;
438
439 KeInitializeEvent(&Event, NotificationEvent, FALSE);
440
441 ConnectData.ClassDeviceObject = ClassDO;
442 ConnectData.ClassService = ClassCallback;
443
444 Irp = IoBuildDeviceIoControlRequest(IOCTL_INTERNAL_MOUSE_CONNECT,
445 PortDO,
446 &ConnectData, sizeof(CONNECT_DATA),
447 NULL, 0,
448 TRUE, &Event, &IoStatus);
449
450 Status = IoCallDriver(PortDO, Irp);
451
452 if (Status == STATUS_PENDING)
453 KeWaitForSingleObject(&Event, Suspended, KernelMode, FALSE, NULL);
454 else
455 IoStatus.Status = Status;
456
457 if (NT_SUCCESS(IoStatus.Status))
458 ObReferenceObject(PortDO);
459
460 return IoStatus.Status;
461 }
462
463 static NTSTATUS NTAPI
464 ClassAddDevice(
465 IN PDRIVER_OBJECT DriverObject,
466 IN PDEVICE_OBJECT Pdo)
467 {
468 PCLASS_DRIVER_EXTENSION DriverExtension;
469 PDEVICE_OBJECT Fdo;
470 PPORT_DEVICE_EXTENSION DeviceExtension;
471 NTSTATUS Status;
472
473 DPRINT("ClassAddDevice called. Pdo = 0x%p\n", Pdo);
474
475 DriverExtension = IoGetDriverObjectExtension(DriverObject, DriverObject);
476
477 /* Create new device object */
478 Status = IoCreateDevice(
479 DriverObject,
480 sizeof(PORT_DEVICE_EXTENSION),
481 NULL,
482 Pdo->DeviceType,
483 FILE_DEVICE_SECURE_OPEN,
484 TRUE,
485 &Fdo);
486 if (!NT_SUCCESS(Status))
487 {
488 DPRINT("IoCreateDevice() failed with status 0x%08lx\n", Status);
489 return Status;
490 }
491
492 DeviceExtension = (PPORT_DEVICE_EXTENSION)Fdo->DeviceExtension;
493 RtlZeroMemory(DeviceExtension, sizeof(CLASS_DEVICE_EXTENSION));
494 DeviceExtension->Common.IsClassDO = FALSE;
495 DeviceExtension->PnpState = dsStopped;
496 Fdo->Flags |= DO_POWER_PAGABLE;
497 Status = IoAttachDeviceToDeviceStackSafe(Fdo, Pdo, &DeviceExtension->LowerDevice);
498 if (!NT_SUCCESS(Status))
499 {
500 DPRINT("IoAttachDeviceToDeviceStackSafe() failed with status 0x%08lx\n", Status);
501 IoDeleteDevice(Fdo);
502 return Status;
503 }
504 Fdo->Flags |= DO_BUFFERED_IO;
505
506 if (DriverExtension->ConnectMultiplePorts)
507 Status = ConnectPortDriver(Fdo, DriverExtension->MainClassDeviceObject);
508 else
509 Status = ConnectPortDriver(Fdo, Fdo);
510 if (!NT_SUCCESS(Status))
511 {
512 DPRINT("ConnectPortDriver() failed with status 0x%08lx\n", Status);
513 IoDetachDevice(DeviceExtension->LowerDevice);
514 /* FIXME: why can't I cleanup without error? */
515 //IoDeleteDevice(Fdo);
516 return Status;
517 }
518 Fdo->Flags &= ~DO_DEVICE_INITIALIZING;
519
520 /* Register interface */
521 Status = IoRegisterDeviceInterface(
522 Pdo,
523 &GUID_DEVINTERFACE_MOUSE,
524 NULL,
525 &DeviceExtension->InterfaceName);
526 if (!NT_SUCCESS(Status))
527 {
528 DPRINT("IoRegisterDeviceInterface() failed with status 0x%08lx\n", Status);
529 return Status;
530 }
531
532 return STATUS_SUCCESS;
533 }
534
535 static VOID NTAPI
536 ClassStartIo(
537 IN PDEVICE_OBJECT DeviceObject,
538 IN PIRP Irp)
539 {
540 PCLASS_DEVICE_EXTENSION DeviceExtension = DeviceObject->DeviceExtension;
541 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
542
543 ASSERT(DeviceExtension->Common.IsClassDO);
544
545 if (DeviceExtension->InputCount > 0)
546 {
547 KIRQL oldIrql;
548
549 KeAcquireSpinLock(&DeviceExtension->SpinLock, &oldIrql);
550
551 /* FIXME: use SEH */
552 RtlCopyMemory(
553 Irp->MdlAddress ? MmGetSystemAddressForMdlSafe(Irp->MdlAddress, NormalPagePriority) : Irp->UserBuffer,
554 DeviceExtension->PortData - DeviceExtension->InputCount,
555 sizeof(MOUSE_INPUT_DATA));
556
557 if (DeviceExtension->InputCount > 1)
558 {
559 RtlMoveMemory(
560 DeviceExtension->PortData - DeviceExtension->InputCount,
561 DeviceExtension->PortData - DeviceExtension->InputCount + 1,
562 (DeviceExtension->InputCount - 1) * sizeof(MOUSE_INPUT_DATA));
563 }
564 DeviceExtension->PortData--;
565 DeviceExtension->InputCount--;
566 DeviceExtension->ReadIsPending = FALSE;
567
568 /* Go to next packet and complete this request with STATUS_SUCCESS */
569 Irp->IoStatus.Status = STATUS_SUCCESS;
570 Irp->IoStatus.Information = sizeof(MOUSE_INPUT_DATA);
571 Stack->Parameters.Read.Length = sizeof(MOUSE_INPUT_DATA);
572 IoCompleteRequest(Irp, IO_MOUSE_INCREMENT);
573
574 IoStartNextPacket(DeviceObject, FALSE);
575 KeReleaseSpinLock(&DeviceExtension->SpinLock, oldIrql);
576 }
577 else
578 {
579 DeviceExtension->ReadIsPending = TRUE;
580 }
581 }
582
583 static NTSTATUS
584 SearchForLegacyDrivers(
585 IN PDRIVER_OBJECT DriverObject,
586 IN PCLASS_DRIVER_EXTENSION DriverExtension)
587 {
588 UNICODE_STRING DeviceMapKeyU = RTL_CONSTANT_STRING(L"\\REGISTRY\\MACHINE\\HARDWARE\\DEVICEMAP");
589 UNICODE_STRING PortBaseName = {0, };
590 PKEY_VALUE_BASIC_INFORMATION KeyValueInformation = NULL;
591 OBJECT_ATTRIBUTES ObjectAttributes;
592 HANDLE hDeviceMapKey = (HANDLE)-1;
593 HANDLE hPortKey = (HANDLE)-1;
594 ULONG Index = 0;
595 ULONG Size, ResultLength;
596 NTSTATUS Status;
597
598 /* Create port base name, by replacing Class by Port at the end of the class base name */
599 Status = RtlDuplicateUnicodeString(
600 RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE,
601 &DriverExtension->DeviceBaseName,
602 &PortBaseName);
603 if (!NT_SUCCESS(Status))
604 {
605 DPRINT("RtlDuplicateUnicodeString() failed with status 0x%08lx\n", Status);
606 goto cleanup;
607 }
608 PortBaseName.Length -= (sizeof(L"Class") - sizeof(UNICODE_NULL));
609 RtlAppendUnicodeToString(&PortBaseName, L"Port");
610
611 /* Allocate memory */
612 Size = sizeof(KEY_VALUE_BASIC_INFORMATION) + MAX_PATH;
613 KeyValueInformation = ExAllocatePool(PagedPool, Size);
614 if (!KeyValueInformation)
615 {
616 DPRINT("ExAllocatePool() failed\n");
617 Status = STATUS_INSUFFICIENT_RESOURCES;
618 goto cleanup;
619 }
620
621 /* Open HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP */
622 InitializeObjectAttributes(&ObjectAttributes, &DeviceMapKeyU, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, NULL);
623 Status = ZwOpenKey(&hDeviceMapKey, 0, &ObjectAttributes);
624 if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
625 {
626 DPRINT("HKLM\\HARDWARE\\DEVICEMAP is non-existent\n");
627 Status = STATUS_SUCCESS;
628 goto cleanup;
629 }
630 else if (!NT_SUCCESS(Status))
631 {
632 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status);
633 goto cleanup;
634 }
635
636 /* Open sub key */
637 InitializeObjectAttributes(&ObjectAttributes, &PortBaseName, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, hDeviceMapKey, NULL);
638 Status = ZwOpenKey(&hPortKey, KEY_QUERY_VALUE, &ObjectAttributes);
639 if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
640 {
641 DPRINT("HKLM\\HARDWARE\\DEVICEMAP\\%wZ is non-existent\n", &PortBaseName);
642 Status = STATUS_SUCCESS;
643 goto cleanup;
644 }
645 else if (!NT_SUCCESS(Status))
646 {
647 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status);
648 goto cleanup;
649 }
650
651 /* Read each value name */
652 while (ZwEnumerateValueKey(hPortKey, Index++, KeyValueBasicInformation, KeyValueInformation, Size, &ResultLength) == STATUS_SUCCESS)
653 {
654 UNICODE_STRING PortName;
655 PDEVICE_OBJECT PortDeviceObject = NULL;
656 PFILE_OBJECT FileObject = NULL;
657
658 PortName.Length = PortName.MaximumLength = KeyValueInformation->NameLength;
659 PortName.Buffer = KeyValueInformation->Name;
660
661 /* Open the device object pointer */
662 Status = IoGetDeviceObjectPointer(&PortName, FILE_READ_ATTRIBUTES, &FileObject, &PortDeviceObject);
663 if (!NT_SUCCESS(Status))
664 {
665 DPRINT("IoGetDeviceObjectPointer(%wZ) failed with status 0x%08lx\n", Status);
666 }
667
668 /* Connect the port device object */
669 if (DriverExtension->ConnectMultiplePorts)
670 {
671 Status = ConnectPortDriver(PortDeviceObject, DriverExtension->MainClassDeviceObject);
672 if (!NT_SUCCESS(Status))
673 {
674 /* FIXME: Log the error */
675 DPRINT("ConnectPortDriver() failed with status 0x%08lx\n", Status);
676 }
677 }
678 else
679 {
680 PDEVICE_OBJECT ClassDO;
681 Status = CreateClassDeviceObject(DriverObject, &ClassDO);
682 if (!NT_SUCCESS(Status))
683 {
684 /* FIXME: Log the error */
685 DPRINT("CreatePointerClassDeviceObject() failed with status 0x%08lx\n", Status);
686 continue;
687 }
688 Status = ConnectPortDriver(PortDeviceObject, ClassDO);
689 if (!NT_SUCCESS(Status))
690 {
691 /* FIXME: Log the error */
692 DPRINT("ConnectPortDriver() failed with status 0x%08lx\n", Status);
693 IoDeleteDevice(ClassDO);
694 }
695 }
696 }
697 if (Status == STATUS_NO_MORE_ENTRIES)
698 Status = STATUS_SUCCESS;
699
700 cleanup:
701 if (KeyValueInformation != NULL)
702 ExFreePool(KeyValueInformation);
703 if (hDeviceMapKey != (HANDLE)-1)
704 ZwClose(hDeviceMapKey);
705 if (hPortKey != (HANDLE)-1)
706 ZwClose(hPortKey);
707 return Status;
708 }
709
710 /*
711 * Standard DriverEntry method.
712 */
713 NTSTATUS NTAPI
714 DriverEntry(
715 IN PDRIVER_OBJECT DriverObject,
716 IN PUNICODE_STRING RegistryPath)
717 {
718 PCLASS_DRIVER_EXTENSION DriverExtension;
719 ULONG i;
720 NTSTATUS Status;
721
722 Status = IoAllocateDriverObjectExtension(
723 DriverObject,
724 DriverObject,
725 sizeof(CLASS_DRIVER_EXTENSION),
726 (PVOID*)&DriverExtension);
727 if (!NT_SUCCESS(Status))
728 {
729 DPRINT("IoAllocateDriverObjectExtension() failed with status 0x%08lx\n", Status);
730 return Status;
731 }
732 RtlZeroMemory(DriverExtension, sizeof(CLASS_DRIVER_EXTENSION));
733
734 Status = ReadRegistryEntries(RegistryPath, DriverExtension);
735 if (!NT_SUCCESS(Status))
736 {
737 DPRINT("ReadRegistryEntries() failed with status 0x%08lx\n", Status);
738 return Status;
739 }
740
741 if (DriverExtension->ConnectMultiplePorts == 1)
742 {
743 Status = CreateClassDeviceObject(
744 DriverObject,
745 &DriverExtension->MainClassDeviceObject);
746 if (!NT_SUCCESS(Status))
747 {
748 DPRINT("CreateClassDeviceObject() failed with status 0x%08lx\n", Status);
749 return Status;
750 }
751 }
752
753 DriverObject->DriverExtension->AddDevice = ClassAddDevice;
754 DriverObject->DriverUnload = DriverUnload;
755
756 for (i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++)
757 DriverObject->MajorFunction[i] = IrpStub;
758
759 DriverObject->MajorFunction[IRP_MJ_CREATE] = ClassCreate;
760 DriverObject->MajorFunction[IRP_MJ_CLOSE] = ClassClose;
761 DriverObject->MajorFunction[IRP_MJ_CLEANUP] = ClassCleanup;
762 DriverObject->MajorFunction[IRP_MJ_READ] = ClassRead;
763 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = ClassDeviceControl;
764 DriverObject->DriverStartIo = ClassStartIo;
765
766 Status = SearchForLegacyDrivers(DriverObject, DriverExtension);
767
768 return Status;
769 }