4 * Copyright (C) 2003, 2004 Eric Kohl
5 * Copyright (C) 2009 Hervé Poussineau
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #define CLOCK_TICK_RATE (1193182)
32 #define LATCH (CLOCK_TICK_RATE / HZ)
36 #define MOUSE_TYPE_NONE 0
37 /* Microsoft Mouse with 2 buttons */
38 #define MOUSE_TYPE_MICROSOFT 1
39 /* Logitech Mouse with 3 buttons */
40 #define MOUSE_TYPE_LOGITECH 2
41 /* Microsoft Wheel Mouse (aka Z Mouse) */
42 #define MOUSE_TYPE_WHEELZ 3
43 /* Mouse Systems Mouse */
44 #define MOUSE_TYPE_MOUSESYSTEMS 4
49 /* Controller registers. */
50 #define CONTROLLER_REGISTER_STATUS 0x64
51 #define CONTROLLER_REGISTER_CONTROL 0x64
52 #define CONTROLLER_REGISTER_DATA 0x60
54 /* Controller commands. */
55 #define CONTROLLER_COMMAND_READ_MODE 0x20
56 #define CONTROLLER_COMMAND_WRITE_MODE 0x60
57 #define CONTROLLER_COMMAND_GET_VERSION 0xA1
58 #define CONTROLLER_COMMAND_MOUSE_DISABLE 0xA7
59 #define CONTROLLER_COMMAND_MOUSE_ENABLE 0xA8
60 #define CONTROLLER_COMMAND_TEST_MOUSE 0xA9
61 #define CONTROLLER_COMMAND_SELF_TEST 0xAA
62 #define CONTROLLER_COMMAND_KEYBOARD_TEST 0xAB
63 #define CONTROLLER_COMMAND_KEYBOARD_DISABLE 0xAD
64 #define CONTROLLER_COMMAND_KEYBOARD_ENABLE 0xAE
65 #define CONTROLLER_COMMAND_WRITE_MOUSE_OUTPUT_BUFFER 0xD3
66 #define CONTROLLER_COMMAND_WRITE_MOUSE 0xD4
68 /* Controller status */
69 #define CONTROLLER_STATUS_OUTPUT_BUFFER_FULL 0x01
70 #define CONTROLLER_STATUS_INPUT_BUFFER_FULL 0x02
71 #define CONTROLLER_STATUS_SELF_TEST 0x04
72 #define CONTROLLER_STATUS_COMMAND 0x08
73 #define CONTROLLER_STATUS_UNLOCKED 0x10
74 #define CONTROLLER_STATUS_MOUSE_OUTPUT_BUFFER_FULL 0x20
75 #define CONTROLLER_STATUS_GENERAL_TIMEOUT 0x40
76 #define CONTROLLER_STATUS_PARITY_ERROR 0x80
77 #define AUX_STATUS_OUTPUT_BUFFER_FULL (CONTROLLER_STATUS_OUTPUT_BUFFER_FULL | \
78 CONTROLLER_STATUS_MOUSE_OUTPUT_BUFFER_FULL)
80 /* Timeout in ms for sending to keyboard controller. */
81 #define CONTROLLER_TIMEOUT 250
83 static CHAR Hex
[] = "0123456789abcdef";
84 static unsigned int delay_count
= 1;
86 extern ULONG reactos_disk_count
;
87 extern ARC_DISK_SIGNATURE reactos_arc_disk_info
[];
88 extern char reactos_arc_strings
[32][256];
90 /* FUNCTIONS ****************************************************************/
94 __StallExecutionProcessor(ULONG Loops
)
96 register volatile unsigned int i
;
97 for (i
= 0; i
< Loops
; i
++);
101 VOID
StallExecutionProcessor(ULONG Microseconds
)
103 ULONGLONG LoopCount
= ((ULONGLONG
)delay_count
* (ULONGLONG
)Microseconds
) / 1000ULL;
104 __StallExecutionProcessor((ULONG
)LoopCount
);
113 WRITE_PORT_UCHAR((PUCHAR
)0x43, 0x00);
114 Count
= READ_PORT_UCHAR((PUCHAR
)0x40);
115 Count
|= READ_PORT_UCHAR((PUCHAR
)0x40) << 8;
122 WaitFor8254Wraparound(VOID
)
125 ULONG PrevCount
= ~0;
128 CurCount
= Read8254Timer();
132 PrevCount
= CurCount
;
133 CurCount
= Read8254Timer();
134 Delta
= CurCount
- PrevCount
;
137 * This limit for delta seems arbitrary, but it isn't, it's
138 * slightly above the level of error a buggy Mercury/Neptune
139 * chipset timer can cause.
147 HalpCalibrateStallExecution(VOID
)
153 /* Initialise timer interrupt with MILLISECOND ms interval */
154 WRITE_PORT_UCHAR((PUCHAR
)0x43, 0x34); /* binary, mode 2, LSB/MSB, ch 0 */
155 WRITE_PORT_UCHAR((PUCHAR
)0x40, LATCH
& 0xff); /* LSB */
156 WRITE_PORT_UCHAR((PUCHAR
)0x40, LATCH
>> 8); /* MSB */
158 /* Stage 1: Coarse calibration */
160 WaitFor8254Wraparound();
165 delay_count
<<= 1; /* Next delay count to try */
167 WaitFor8254Wraparound();
169 __StallExecutionProcessor(delay_count
); /* Do the delay */
171 CurCount
= Read8254Timer();
172 } while (CurCount
> LATCH
/ 2);
174 delay_count
>>= 1; /* Get bottom value for delay */
176 /* Stage 2: Fine calibration */
178 calib_bit
= delay_count
; /* Which bit are we going to test */
180 for(i
=0;i
<PRECISION
;i
++) {
181 calib_bit
>>= 1; /* Next bit to calibrate */
182 if(!calib_bit
) break; /* If we have done all bits, stop */
184 delay_count
|= calib_bit
; /* Set the bit in delay_count */
186 WaitFor8254Wraparound();
188 __StallExecutionProcessor(delay_count
); /* Do the delay */
190 CurCount
= Read8254Timer();
191 if (CurCount
<= LATCH
/ 2) /* If a tick has passed, turn the */
192 delay_count
&= ~calib_bit
; /* calibrated bit back off */
195 /* We're finished: Do the finishing touches */
196 delay_count
/= (MILLISEC
/ 2); /* Calculate delay_count for 1ms */
200 DetectPnpBios(PCONFIGURATION_COMPONENT_DATA SystemKey
, ULONG
*BusNumber
)
202 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
203 PCM_PNP_BIOS_DEVICE_NODE DeviceNode
;
204 PCM_PNP_BIOS_INSTALLATION_CHECK InstData
;
205 PCONFIGURATION_COMPONENT_DATA BusKey
;
210 ULONG FoundNodeCount
;
216 InstData
= (PCM_PNP_BIOS_INSTALLATION_CHECK
)PnpBiosSupported();
217 if (InstData
== NULL
|| strncmp((CHAR
*)InstData
->Signature
, "$PnP", 4))
219 DPRINTM(DPRINT_HWDETECT
, "PnP-BIOS not supported\n");
222 DPRINTM(DPRINT_HWDETECT
, "Signature '%c%c%c%c'\n",
223 InstData
->Signature
[0], InstData
->Signature
[1],
224 InstData
->Signature
[2], InstData
->Signature
[3]);
227 x
= PnpBiosGetDeviceNodeCount(&NodeSize
, &NodeCount
);
228 NodeCount
&= 0xFF; // needed since some fscked up BIOSes return
229 // wrong info (e.g. Mac Virtual PC)
230 // e.g. look: http://my.execpc.com/~geezer/osd/pnp/pnp16.c
231 if (x
!= 0 || NodeSize
== 0 || NodeCount
== 0)
233 DPRINTM(DPRINT_HWDETECT
, "PnP-BIOS failed to enumerate device nodes\n");
236 DPRINTM(DPRINT_HWDETECT
, "PnP-BIOS supported\n");
237 DPRINTM(DPRINT_HWDETECT
, "MaxNodeSize %u NodeCount %u\n", NodeSize
, NodeCount
);
238 DPRINTM(DPRINT_HWDETECT
, "Estimated buffer size %u\n", NodeSize
* NodeCount
);
240 /* Set 'Configuration Data' value */
241 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) + (NodeSize
* NodeCount
);
242 PartialResourceList
= MmHeapAlloc(Size
);
243 if (PartialResourceList
== NULL
)
245 DPRINTM(DPRINT_HWDETECT
,
246 "Failed to allocate resource descriptor\n");
249 memset(PartialResourceList
, 0, Size
);
251 /* Initialize resource descriptor */
252 PartialResourceList
->Version
= 1;
253 PartialResourceList
->Revision
= 1;
254 PartialResourceList
->Count
= 1;
255 PartialResourceList
->PartialDescriptors
[0].Type
=
256 CmResourceTypeDeviceSpecific
;
257 PartialResourceList
->PartialDescriptors
[0].ShareDisposition
=
258 CmResourceShareUndetermined
;
260 Ptr
= (char *)(((ULONG_PTR
)&PartialResourceList
->PartialDescriptors
[0]) +
261 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
));
263 /* Set instalation check data */
264 memcpy (Ptr
, InstData
, sizeof(CM_PNP_BIOS_INSTALLATION_CHECK
));
265 Ptr
+= sizeof(CM_PNP_BIOS_INSTALLATION_CHECK
);
267 /* Copy device nodes */
269 PnpBufferSize
= sizeof(CM_PNP_BIOS_INSTALLATION_CHECK
);
270 for (i
= 0; i
< 0xFF; i
++)
272 NodeNumber
= (UCHAR
)i
;
274 x
= PnpBiosGetDeviceNode(&NodeNumber
, (PVOID
)DISKREADBUFFER
);
277 DeviceNode
= (PCM_PNP_BIOS_DEVICE_NODE
)DISKREADBUFFER
;
279 DPRINTM(DPRINT_HWDETECT
,
280 "Node: %u Size %u (0x%x)\n",
289 Ptr
+= DeviceNode
->Size
;
290 PnpBufferSize
+= DeviceNode
->Size
;
293 if (FoundNodeCount
>= NodeCount
)
298 /* Set real data size */
299 PartialResourceList
->PartialDescriptors
[0].u
.DeviceSpecificData
.DataSize
=
301 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) + PnpBufferSize
;
303 DPRINTM(DPRINT_HWDETECT
, "Real buffer size: %u\n", PnpBufferSize
);
304 DPRINTM(DPRINT_HWDETECT
, "Resource size: %u\n", Size
);
306 /* Create component key */
307 FldrCreateComponentKey(SystemKey
,
309 MultiFunctionAdapter
,
320 MmHeapFree(PartialResourceList
);
325 static PCM_PARTIAL_RESOURCE_LIST
326 GetHarddiskConfigurationData(ULONG DriveNumber
, ULONG
* pSize
)
328 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
329 PCM_DISK_GEOMETRY_DEVICE_DATA DiskGeometry
;
330 EXTENDED_GEOMETRY ExtGeometry
;
335 // Initialize returned size
339 /* Set 'Configuration Data' value */
340 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
341 sizeof(CM_DISK_GEOMETRY_DEVICE_DATA
);
342 PartialResourceList
= MmHeapAlloc(Size
);
343 if (PartialResourceList
== NULL
)
345 DPRINTM(DPRINT_HWDETECT
,
346 "Failed to allocate a full resource descriptor\n");
350 memset(PartialResourceList
, 0, Size
);
351 PartialResourceList
->Version
= 1;
352 PartialResourceList
->Revision
= 1;
353 PartialResourceList
->Count
= 1;
354 PartialResourceList
->PartialDescriptors
[0].Type
=
355 CmResourceTypeDeviceSpecific
;
356 // PartialResourceList->PartialDescriptors[0].ShareDisposition =
357 // PartialResourceList->PartialDescriptors[0].Flags =
358 PartialResourceList
->PartialDescriptors
[0].u
.DeviceSpecificData
.DataSize
=
359 sizeof(CM_DISK_GEOMETRY_DEVICE_DATA
);
361 /* Get pointer to geometry data */
362 DiskGeometry
= (PVOID
)(((ULONG_PTR
)PartialResourceList
) + sizeof(CM_PARTIAL_RESOURCE_LIST
));
364 /* Get the disk geometry */
365 ExtGeometry
.Size
= sizeof(EXTENDED_GEOMETRY
);
366 if (DiskGetExtendedDriveParameters(DriveNumber
, &ExtGeometry
, ExtGeometry
.Size
))
368 DiskGeometry
->BytesPerSector
= ExtGeometry
.BytesPerSector
;
369 DiskGeometry
->NumberOfCylinders
= ExtGeometry
.Cylinders
;
370 DiskGeometry
->SectorsPerTrack
= ExtGeometry
.SectorsPerTrack
;
371 DiskGeometry
->NumberOfHeads
= ExtGeometry
.Heads
;
373 else if(MachDiskGetDriveGeometry(DriveNumber
, &Geometry
))
375 DiskGeometry
->BytesPerSector
= Geometry
.BytesPerSector
;
376 DiskGeometry
->NumberOfCylinders
= Geometry
.Cylinders
;
377 DiskGeometry
->SectorsPerTrack
= Geometry
.Sectors
;
378 DiskGeometry
->NumberOfHeads
= Geometry
.Heads
;
382 DPRINTM(DPRINT_HWDETECT
, "Reading disk geometry failed\n");
383 MmHeapFree(PartialResourceList
);
386 DPRINTM(DPRINT_HWDETECT
,
387 "Disk %x: %u Cylinders %u Heads %u Sectors %u Bytes\n",
389 DiskGeometry
->NumberOfCylinders
,
390 DiskGeometry
->NumberOfHeads
,
391 DiskGeometry
->SectorsPerTrack
,
392 DiskGeometry
->BytesPerSector
);
395 // Return configuration data
398 return PartialResourceList
;
401 typedef struct tagDISKCONTEXT
405 ULONGLONG SectorOffset
;
406 ULONGLONG SectorCount
;
407 ULONGLONG SectorNumber
;
410 static LONG
DiskClose(ULONG FileId
)
412 DISKCONTEXT
* Context
= FsGetDeviceSpecific(FileId
);
418 static LONG
DiskGetFileInformation(ULONG FileId
, FILEINFORMATION
* Information
)
420 DISKCONTEXT
* Context
= FsGetDeviceSpecific(FileId
);
422 RtlZeroMemory(Information
, sizeof(FILEINFORMATION
));
423 Information
->EndingAddress
.QuadPart
= (Context
->SectorOffset
+ Context
->SectorCount
) * Context
->SectorSize
;
424 Information
->CurrentAddress
.LowPart
= (Context
->SectorOffset
+ Context
->SectorNumber
) * Context
->SectorSize
;
429 static LONG
DiskOpen(CHAR
* Path
, OPENMODE OpenMode
, ULONG
* FileId
)
431 DISKCONTEXT
* Context
;
432 ULONG DriveNumber
, DrivePartition
, SectorSize
;
433 ULONGLONG SectorOffset
= 0;
434 ULONGLONG SectorCount
= 0;
435 PARTITION_TABLE_ENTRY PartitionTableEntry
;
438 if (!DissectArcPath(Path
, FileName
, &DriveNumber
, &DrivePartition
))
440 SectorSize
= (DrivePartition
== 0xff ? 2048 : 512);
441 if (DrivePartition
!= 0xff && DrivePartition
!= 0)
443 if (!DiskGetPartitionEntry(DriveNumber
, DrivePartition
, &PartitionTableEntry
))
445 SectorOffset
= PartitionTableEntry
.SectorCountBeforePartition
;
446 SectorCount
= PartitionTableEntry
.PartitionSectorCount
;
450 SectorCount
= 0; /* FIXME */
453 Context
= MmHeapAlloc(sizeof(DISKCONTEXT
));
456 Context
->DriveNumber
= DriveNumber
;
457 Context
->SectorSize
= SectorSize
;
458 Context
->SectorOffset
= SectorOffset
;
459 Context
->SectorCount
= SectorCount
;
460 Context
->SectorNumber
= 0;
461 FsSetDeviceSpecific(*FileId
, Context
);
466 static LONG
DiskRead(ULONG FileId
, VOID
* Buffer
, ULONG N
, ULONG
* Count
)
468 DISKCONTEXT
* Context
= FsGetDeviceSpecific(FileId
);
469 UCHAR
* Ptr
= (UCHAR
*)Buffer
;
478 if (Length
> Context
->SectorSize
)
479 Length
= Context
->SectorSize
;
480 ret
= MachDiskReadLogicalSectors(
481 Context
->DriveNumber
,
482 Context
->SectorNumber
+ Context
->SectorOffset
+ i
,
484 (PVOID
)DISKREADBUFFER
);
487 RtlCopyMemory(Ptr
, (PVOID
)DISKREADBUFFER
, Length
);
497 static LONG
DiskSeek(ULONG FileId
, LARGE_INTEGER
* Position
, SEEKMODE SeekMode
)
499 DISKCONTEXT
* Context
= FsGetDeviceSpecific(FileId
);
501 if (SeekMode
!= SeekAbsolute
)
503 if (Position
->LowPart
& (Context
->SectorSize
- 1))
506 /* FIXME: take HighPart into account */
507 Context
->SectorNumber
= Position
->LowPart
/ Context
->SectorSize
;
511 static const DEVVTBL DiskVtbl
= {
513 DiskGetFileInformation
,
520 GetHarddiskIdentifier(PCHAR Identifier
,
523 PMASTER_BOOT_RECORD Mbr
;
529 PARTITION_TABLE_ENTRY PartitionTableEntry
;
532 if (!MachDiskReadLogicalSectors(DriveNumber
, 0ULL, 1, (PVOID
)DISKREADBUFFER
))
534 DPRINTM(DPRINT_HWDETECT
, "Reading MBR failed\n");
538 Buffer
= (ULONG
*)DISKREADBUFFER
;
539 Mbr
= (PMASTER_BOOT_RECORD
)DISKREADBUFFER
;
541 Signature
= Mbr
->Signature
;
542 DPRINTM(DPRINT_HWDETECT
, "Signature: %x\n", Signature
);
544 /* Calculate the MBR checksum */
546 for (i
= 0; i
< 128; i
++)
548 Checksum
+= Buffer
[i
];
550 Checksum
= ~Checksum
+ 1;
551 DPRINTM(DPRINT_HWDETECT
, "Checksum: %x\n", Checksum
);
553 /* Fill out the ARC disk block */
554 reactos_arc_disk_info
[reactos_disk_count
].Signature
= Signature
;
555 reactos_arc_disk_info
[reactos_disk_count
].CheckSum
= Checksum
;
556 sprintf(ArcName
, "multi(0)disk(0)rdisk(%lu)", reactos_disk_count
);
557 strcpy(reactos_arc_strings
[reactos_disk_count
], ArcName
);
558 reactos_arc_disk_info
[reactos_disk_count
].ArcName
=
559 reactos_arc_strings
[reactos_disk_count
];
560 reactos_disk_count
++;
562 sprintf(ArcName
, "multi(0)disk(0)rdisk(%lu)partition(0)", DriveNumber
- 0x80);
563 FsRegisterDevice(ArcName
, &DiskVtbl
);
567 DiskReportError(FALSE
);
568 while (DiskGetPartitionEntry(DriveNumber
, i
, &PartitionTableEntry
))
570 if (PartitionTableEntry
.SystemIndicator
!= PARTITION_ENTRY_UNUSED
)
572 sprintf(ArcName
, "multi(0)disk(0)rdisk(%lu)partition(%lu)", DriveNumber
- 0x80, i
);
573 FsRegisterDevice(ArcName
, &DiskVtbl
);
577 DiskReportError(TRUE
);
579 /* Convert checksum and signature to identifier string */
580 Identifier
[0] = Hex
[(Checksum
>> 28) & 0x0F];
581 Identifier
[1] = Hex
[(Checksum
>> 24) & 0x0F];
582 Identifier
[2] = Hex
[(Checksum
>> 20) & 0x0F];
583 Identifier
[3] = Hex
[(Checksum
>> 16) & 0x0F];
584 Identifier
[4] = Hex
[(Checksum
>> 12) & 0x0F];
585 Identifier
[5] = Hex
[(Checksum
>> 8) & 0x0F];
586 Identifier
[6] = Hex
[(Checksum
>> 4) & 0x0F];
587 Identifier
[7] = Hex
[Checksum
& 0x0F];
589 Identifier
[9] = Hex
[(Signature
>> 28) & 0x0F];
590 Identifier
[10] = Hex
[(Signature
>> 24) & 0x0F];
591 Identifier
[11] = Hex
[(Signature
>> 20) & 0x0F];
592 Identifier
[12] = Hex
[(Signature
>> 16) & 0x0F];
593 Identifier
[13] = Hex
[(Signature
>> 12) & 0x0F];
594 Identifier
[14] = Hex
[(Signature
>> 8) & 0x0F];
595 Identifier
[15] = Hex
[(Signature
>> 4) & 0x0F];
596 Identifier
[16] = Hex
[Signature
& 0x0F];
597 Identifier
[17] = '-';
598 Identifier
[18] = 'A';
600 DPRINTM(DPRINT_HWDETECT
, "Identifier: %s\n", Identifier
);
608 WRITE_PORT_UCHAR((PUCHAR
)0x70, 0x10);
609 Data
= READ_PORT_UCHAR((PUCHAR
)0x71);
611 return ((Data
& 0xF0) ? 1 : 0) + ((Data
& 0x0F) ? 1 : 0);
616 GetFloppyType(UCHAR DriveNumber
)
620 WRITE_PORT_UCHAR((PUCHAR
)0x70, 0x10);
621 Data
= READ_PORT_UCHAR((PUCHAR
)0x71);
623 if (DriveNumber
== 0)
625 else if (DriveNumber
== 1)
635 PUSHORT SegPtr
= (PUSHORT
)0x7A;
636 PUSHORT OfsPtr
= (PUSHORT
)0x78;
638 return (PVOID
)((ULONG_PTR
)(((ULONG
)(*SegPtr
)) << 4) + (ULONG
)(*OfsPtr
));
643 DetectBiosFloppyPeripheral(PCONFIGURATION_COMPONENT_DATA ControllerKey
)
645 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
646 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
647 PCM_FLOPPY_DEVICE_DATA FloppyData
;
649 PCONFIGURATION_COMPONENT_DATA PeripheralKey
;
653 ULONG MaxDensity
[6] = {0, 360, 1200, 720, 1440, 2880};
656 for (FloppyNumber
= 0; FloppyNumber
< 2; FloppyNumber
++)
658 FloppyType
= GetFloppyType(FloppyNumber
);
660 if ((FloppyType
> 5) || (FloppyType
== 0))
663 DiskResetController(FloppyNumber
);
665 Ptr
= GetInt1eTable();
667 /* Set 'Identifier' value */
668 sprintf(Identifier
, "FLOPPY%ld", FloppyNumber
+ 1);
670 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
671 sizeof(CM_FLOPPY_DEVICE_DATA
);
672 PartialResourceList
= MmHeapAlloc(Size
);
673 if (PartialResourceList
== NULL
)
675 DPRINTM(DPRINT_HWDETECT
,
676 "Failed to allocate resource descriptor\n");
680 memset(PartialResourceList
, 0, Size
);
681 PartialResourceList
->Version
= 1;
682 PartialResourceList
->Revision
= 1;
683 PartialResourceList
->Count
= 1;
685 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
686 PartialDescriptor
->Type
= CmResourceTypeDeviceSpecific
;
687 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
688 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
= sizeof(CM_FLOPPY_DEVICE_DATA
);
690 FloppyData
= (PVOID
)(((ULONG_PTR
)PartialResourceList
) + sizeof(CM_PARTIAL_RESOURCE_LIST
));
691 FloppyData
->Version
= 2;
692 FloppyData
->Revision
= 0;
693 FloppyData
->MaxDensity
= MaxDensity
[FloppyType
];
694 FloppyData
->MountDensity
= 0;
695 RtlCopyMemory(&FloppyData
->StepRateHeadUnloadTime
,
698 FloppyData
->MaximumTrackValue
= (FloppyType
== 1) ? 39 : 79;
699 FloppyData
->DataTransferRate
= 0;
701 FldrCreateComponentKey(ControllerKey
,
703 FloppyDiskPeripheral
,
712 MmHeapFree(PartialResourceList
);
718 DetectBiosFloppyController(PCONFIGURATION_COMPONENT_DATA BusKey
)
720 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
721 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
722 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
726 FloppyCount
= GetFloppyCount();
727 DPRINTM(DPRINT_HWDETECT
,
728 "Floppy count: %u\n",
731 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
732 2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
733 PartialResourceList
= MmHeapAlloc(Size
);
734 if (PartialResourceList
== NULL
)
736 DPRINTM(DPRINT_HWDETECT
,
737 "Failed to allocate resource descriptor\n");
740 memset(PartialResourceList
, 0, Size
);
742 /* Initialize resource descriptor */
743 PartialResourceList
->Version
= 1;
744 PartialResourceList
->Revision
= 1;
745 PartialResourceList
->Count
= 3;
748 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
749 PartialDescriptor
->Type
= CmResourceTypePort
;
750 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
751 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
752 PartialDescriptor
->u
.Port
.Start
.LowPart
= 0x03F0;
753 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
754 PartialDescriptor
->u
.Port
.Length
= 8;
757 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[1];
758 PartialDescriptor
->Type
= CmResourceTypeInterrupt
;
759 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
760 PartialDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
761 PartialDescriptor
->u
.Interrupt
.Level
= 6;
762 PartialDescriptor
->u
.Interrupt
.Vector
= 6;
763 PartialDescriptor
->u
.Interrupt
.Affinity
= 0xFFFFFFFF;
765 /* Set DMA channel */
766 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[2];
767 PartialDescriptor
->Type
= CmResourceTypeDma
;
768 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
769 PartialDescriptor
->Flags
= 0;
770 PartialDescriptor
->u
.Dma
.Channel
= 2;
771 PartialDescriptor
->u
.Dma
.Port
= 0;
773 /* Create floppy disk controller */
774 FldrCreateComponentKey(BusKey
,
784 DPRINTM(DPRINT_HWDETECT
, "Created key: DiskController\\0\n");
786 MmHeapFree(PartialResourceList
);
788 if (FloppyCount
) DetectBiosFloppyPeripheral(ControllerKey
);
791 static PCONFIGURATION_COMPONENT_DATA
794 PCONFIGURATION_COMPONENT_DATA SystemKey
;
795 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
796 PCM_INT13_DRIVE_PARAMETER Int13Drives
;
803 /* Count the number of visible drives */
804 DiskReportError(FALSE
);
807 /* There are some really broken BIOSes out there. There are even BIOSes
808 * that happily report success when you ask them to read from non-existent
809 * harddisks. So, we set the buffer to known contents first, then try to
810 * read. If the BIOS reports success but the buffer contents haven't
811 * changed then we fail anyway */
812 memset((PVOID
) DISKREADBUFFER
, 0xcd, 512);
813 while (MachDiskReadLogicalSectors(0x80 + DiskCount
, 0ULL, 1, (PVOID
)DISKREADBUFFER
))
816 for (i
= 0; ! Changed
&& i
< 512; i
++)
818 Changed
= ((PUCHAR
)DISKREADBUFFER
)[i
] != 0xcd;
822 DPRINTM(DPRINT_HWDETECT
, "BIOS reports success for disk %d but data didn't change\n",
827 memset((PVOID
) DISKREADBUFFER
, 0xcd, 512);
829 DiskReportError(TRUE
);
830 DPRINTM(DPRINT_HWDETECT
, "BIOS reports %d harddisk%s\n",
831 (int)DiskCount
, (DiskCount
== 1) ? "": "s");
833 /* Allocate resource descriptor */
834 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
835 sizeof(CM_INT13_DRIVE_PARAMETER
) * DiskCount
;
836 PartialResourceList
= MmHeapAlloc(Size
);
837 if (PartialResourceList
== NULL
)
839 DPRINTM(DPRINT_HWDETECT
,
840 "Failed to allocate resource descriptor\n");
844 /* Initialize resource descriptor */
845 memset(PartialResourceList
, 0, Size
);
846 PartialResourceList
->Version
= 1;
847 PartialResourceList
->Revision
= 1;
848 PartialResourceList
->Count
= 1;
849 PartialResourceList
->PartialDescriptors
[0].Type
= CmResourceTypeDeviceSpecific
;
850 PartialResourceList
->PartialDescriptors
[0].ShareDisposition
= 0;
851 PartialResourceList
->PartialDescriptors
[0].Flags
= 0;
852 PartialResourceList
->PartialDescriptors
[0].u
.DeviceSpecificData
.DataSize
=
853 sizeof(CM_INT13_DRIVE_PARAMETER
) * DiskCount
;
855 /* Get harddisk Int13 geometry data */
856 Int13Drives
= (PVOID
)(((ULONG_PTR
)PartialResourceList
) + sizeof(CM_PARTIAL_RESOURCE_LIST
));
857 for (i
= 0; i
< DiskCount
; i
++)
859 if (MachDiskGetDriveGeometry(0x80 + i
, &Geometry
))
861 Int13Drives
[i
].DriveSelect
= 0x80 + i
;
862 Int13Drives
[i
].MaxCylinders
= Geometry
.Cylinders
- 1;
863 Int13Drives
[i
].SectorsPerTrack
= Geometry
.Sectors
;
864 Int13Drives
[i
].MaxHeads
= Geometry
.Heads
- 1;
865 Int13Drives
[i
].NumberDrives
= DiskCount
;
867 DPRINTM(DPRINT_HWDETECT
,
868 "Disk %x: %u Cylinders %u Heads %u Sectors %u Bytes\n",
870 Geometry
.Cylinders
- 1,
873 Geometry
.BytesPerSector
);
877 FldrCreateComponentKey(NULL
,
888 MmHeapFree(PartialResourceList
);
894 GetDiskCount(PCONFIGURATION_COMPONENT_DATA BusKey
)
896 PCONFIGURATION_COMPONENT_DATA System
;
897 ULONG ConfigurationDataLength
;
901 // Get root component
904 while (System
->Parent
)
905 System
= System
->Parent
;
908 // Get root configuration data length
910 ConfigurationDataLength
= System
->ComponentEntry
.ConfigurationDataLength
;
913 // We assume that nothing wrong happened, and that configuration
914 // only consists of one CM_PARTIAL_RESOURCE_LIST entry, followed
915 // by n entries of CM_INT13_DRIVE_PARAMETER
917 if (ConfigurationDataLength
> 0)
918 DiskCount
= (ConfigurationDataLength
- sizeof(CM_PARTIAL_RESOURCE_LIST
))
919 / sizeof(CM_INT13_DRIVE_PARAMETER
);
922 // Return number of disks
924 DPRINTM(DPRINT_HWDETECT
, "Retrieving %lu INT13 disks\\0\n", DiskCount
);
929 DetectBiosDisks(PCONFIGURATION_COMPONENT_DATA BusKey
)
931 PCONFIGURATION_COMPONENT_DATA DiskKey
, ControllerKey
;
932 BOOLEAN BootDriveReported
= FALSE
;
934 ULONG DiskCount
= GetDiskCount(BusKey
);
937 FldrCreateComponentKey(BusKey
,
947 DPRINTM(DPRINT_HWDETECT
, "Created key: DiskController\\0\n");
949 /* Create and fill subkey for each harddisk */
950 for (i
= 0; i
< DiskCount
; i
++)
952 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
956 if (BootDrive
== 0x80 + i
)
957 BootDriveReported
= TRUE
;
959 /* Get disk values */
960 PartialResourceList
= GetHarddiskConfigurationData(0x80 + i
, &Size
);
961 GetHarddiskIdentifier(Identifier
, 0x80 + i
);
963 /* Create disk key */
964 FldrCreateComponentKey(ControllerKey
,
976 /* Get the drive we're booting from */
977 MachDiskGetBootPath(BootPath
, sizeof(BootPath
));
979 /* Add it, if it's a floppy or cdrom */
980 if ((BootDrive
>= 0x80 && !BootDriveReported
) ||
981 DiskIsDriveRemovable(BootDrive
))
983 /* TODO: Check if it's really a cdrom drive */
988 if (!MachDiskReadLogicalSectors(BootDrive
, 16ULL, 1, (PVOID
)DISKREADBUFFER
))
990 DPRINTM(DPRINT_HWDETECT
, "Reading MBR failed\n");
994 Buffer
= (ULONG
*)DISKREADBUFFER
;
996 /* Calculate the MBR checksum */
997 for (i
= 0; i
< 2048 / sizeof(ULONG
); i
++) Checksum
+= Buffer
[i
];
998 DPRINTM(DPRINT_HWDETECT
, "Checksum: %x\n", Checksum
);
1000 /* Fill out the ARC disk block */
1001 reactos_arc_disk_info
[reactos_disk_count
].CheckSum
= Checksum
;
1002 strcpy(reactos_arc_strings
[reactos_disk_count
], BootPath
);
1003 reactos_arc_disk_info
[reactos_disk_count
].ArcName
=
1004 reactos_arc_strings
[reactos_disk_count
];
1005 reactos_disk_count
++;
1007 FsRegisterDevice(BootPath
, &DiskVtbl
);
1012 InitializeSerialPort(PUCHAR Port
,
1015 WRITE_PORT_UCHAR(Port
+ 3, 0x80); /* set DLAB on */
1016 WRITE_PORT_UCHAR(Port
, 0x60); /* speed LO byte */
1017 WRITE_PORT_UCHAR(Port
+ 1, 0); /* speed HI byte */
1018 WRITE_PORT_UCHAR(Port
+ 3, LineControl
);
1019 WRITE_PORT_UCHAR(Port
+ 1, 0); /* set comm and DLAB to 0 */
1020 WRITE_PORT_UCHAR(Port
+ 4, 0x09); /* DR int enable */
1021 READ_PORT_UCHAR(Port
+ 5); /* clear error bits */
1026 DetectSerialMouse(PUCHAR Port
)
1033 /* Shutdown mouse or something like that */
1034 LineControl
= READ_PORT_UCHAR(Port
+ 4);
1035 WRITE_PORT_UCHAR(Port
+ 4, (LineControl
& ~0x02) | 0x01);
1036 StallExecutionProcessor(100000);
1040 * Maybe there is no serial port although BIOS reported one (this
1041 * is the case on Apple hardware), or the serial port is misbehaving,
1042 * therefore we must give up after some time.
1045 while (READ_PORT_UCHAR(Port
+ 5) & 0x01)
1048 return MOUSE_TYPE_NONE
;
1049 READ_PORT_UCHAR(Port
);
1053 * Send modem control with 'Data Terminal Ready', 'Request To Send' and
1054 * 'Output Line 2' message. This enables mouse to identify.
1056 WRITE_PORT_UCHAR(Port
+ 4, 0x0b);
1058 /* Wait 10 milliseconds for the mouse getting ready */
1059 StallExecutionProcessor(10000);
1061 /* Read first four bytes, which contains Microsoft Mouse signs */
1063 for (i
= 0; i
< 4; i
++)
1065 while (((READ_PORT_UCHAR(Port
+ 5) & 1) == 0) && (TimeOut
> 0))
1067 StallExecutionProcessor(1000);
1070 return MOUSE_TYPE_NONE
;
1072 Buffer
[i
] = READ_PORT_UCHAR(Port
);
1075 DPRINTM(DPRINT_HWDETECT
,
1076 "Mouse data: %x %x %x %x\n",
1077 Buffer
[0],Buffer
[1],Buffer
[2],Buffer
[3]);
1079 /* Check that four bytes for signs */
1080 for (i
= 0; i
< 4; ++i
)
1082 if (Buffer
[i
] == 'B')
1084 /* Sign for Microsoft Ballpoint */
1085 // DbgPrint("Microsoft Ballpoint device detected\n");
1086 // DbgPrint("THIS DEVICE IS NOT SUPPORTED, YET\n");
1087 return MOUSE_TYPE_NONE
;
1089 else if (Buffer
[i
] == 'M')
1091 /* Sign for Microsoft Mouse protocol followed by button specifier */
1094 /* Overflow Error */
1095 return MOUSE_TYPE_NONE
;
1098 switch (Buffer
[i
+ 1])
1101 DPRINTM(DPRINT_HWDETECT
,
1102 "Microsoft Mouse with 3-buttons detected\n");
1103 return MOUSE_TYPE_LOGITECH
;
1106 DPRINTM(DPRINT_HWDETECT
,
1107 "Microsoft Wheel Mouse detected\n");
1108 return MOUSE_TYPE_WHEELZ
;
1112 DPRINTM(DPRINT_HWDETECT
,
1113 "Microsoft Mouse with 2-buttons detected\n");
1114 return MOUSE_TYPE_MICROSOFT
;
1119 return MOUSE_TYPE_NONE
;
1124 GetSerialMousePnpId(PUCHAR Port
, char *Buffer
)
1131 WRITE_PORT_UCHAR(Port
+ 4, 0x09);
1133 /* Wait 10 milliseconds for the mouse getting ready */
1134 StallExecutionProcessor(10000);
1136 WRITE_PORT_UCHAR(Port
+ 4, 0x0b);
1138 StallExecutionProcessor(10000);
1143 while (((READ_PORT_UCHAR(Port
+ 5) & 1) == 0) && (TimeOut
> 0))
1145 StallExecutionProcessor(1000);
1153 c
= READ_PORT_UCHAR(Port
);
1154 if (c
== 0x08 || c
== 0x28)
1164 while (((READ_PORT_UCHAR(Port
+ 5) & 1) == 0) && (TimeOut
> 0))
1166 StallExecutionProcessor(1000);
1171 c
= READ_PORT_UCHAR(Port
);
1184 DetectSerialPointerPeripheral(PCONFIGURATION_COMPONENT_DATA ControllerKey
,
1187 CM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1189 CHAR Identifier
[256];
1190 PCONFIGURATION_COMPONENT_DATA PeripheralKey
;
1197 DPRINTM(DPRINT_HWDETECT
,
1198 "DetectSerialPointerPeripheral()\n");
1202 InitializeSerialPort(Base
, 2);
1203 MouseType
= DetectSerialMouse(Base
);
1205 if (MouseType
!= MOUSE_TYPE_NONE
)
1207 Length
= GetSerialMousePnpId(Base
, Buffer
);
1208 DPRINTM(DPRINT_HWDETECT
,
1209 "PnP ID length: %u\n",
1214 /* Convert PnP sting to ASCII */
1215 if (Buffer
[0] == 0x08)
1217 for (i
= 0; i
< Length
; i
++)
1222 DPRINTM(DPRINT_HWDETECT
,
1223 "PnP ID string: %s\n",
1226 /* Copy PnpId string */
1227 for (i
= 0; i
< 7; i
++)
1229 Identifier
[i
] = Buffer
[3+i
];
1231 memcpy(&Identifier
[7],
1235 /* Skip device serial number */
1237 if (Buffer
[i
] == '\\')
1239 for (j
= ++i
; i
< Length
; ++i
)
1241 if (Buffer
[i
] == '\\')
1248 /* Skip PnP class */
1249 if (Buffer
[i
] == '\\')
1251 for (j
= ++i
; i
< Length
; ++i
)
1253 if (Buffer
[i
] == '\\')
1261 /* Skip compatible PnP Id */
1262 if (Buffer
[i
] == '\\')
1264 for (j
= ++i
; i
< Length
; ++i
)
1266 if (Buffer
[i
] == '\\')
1269 if (Buffer
[j
] == '*')
1275 /* Get product description */
1276 if (Buffer
[i
] == '\\')
1278 for (j
= ++i
; i
< Length
; ++i
)
1280 if (Buffer
[i
] == ';')
1287 for (k
= 0; k
< i
- j
; k
++)
1289 Identifier
[k
+ 10] = Buffer
[k
+ j
];
1291 Identifier
[10 + (i
-j
)] = 0;
1295 DPRINTM(DPRINT_HWDETECT
,
1296 "Identifier string: %s\n",
1300 if (Length
== 0 || strlen(Identifier
) < 11)
1304 case MOUSE_TYPE_LOGITECH
:
1306 "LOGITECH SERIAL MOUSE");
1309 case MOUSE_TYPE_WHEELZ
:
1311 "MICROSOFT SERIAL MOUSE WITH WHEEL");
1314 case MOUSE_TYPE_MICROSOFT
:
1317 "MICROSOFT SERIAL MOUSE");
1322 /* Set 'Configuration Data' value */
1323 memset(&PartialResourceList
, 0, sizeof(CM_PARTIAL_RESOURCE_LIST
));
1324 PartialResourceList
.Version
= 1;
1325 PartialResourceList
.Revision
= 1;
1326 PartialResourceList
.Count
= 0;
1328 /* Create 'PointerPeripheral' key */
1329 FldrCreateComponentKey(ControllerKey
,
1336 &PartialResourceList
,
1337 sizeof(CM_PARTIAL_RESOURCE_LIST
) -
1338 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
),
1341 DPRINTM(DPRINT_HWDETECT
,
1342 "Created key: PointerPeripheral\\0\n");
1348 DetectSerialPorts(PCONFIGURATION_COMPONENT_DATA BusKey
)
1350 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1351 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
1352 PCM_SERIAL_DEVICE_DATA SerialDeviceData
;
1353 ULONG Irq
[4] = {4, 3, 4, 3};
1357 ULONG ControllerNumber
= 0;
1358 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1362 DPRINTM(DPRINT_HWDETECT
, "DetectSerialPorts()\n");
1364 ControllerNumber
= 0;
1365 BasePtr
= (PUSHORT
)0x400;
1366 for (i
= 0; i
< 4; i
++, BasePtr
++)
1368 Base
= (ULONG
)*BasePtr
;
1372 DPRINTM(DPRINT_HWDETECT
,
1373 "Found COM%u port at 0x%x\n",
1377 /* Set 'Identifier' value */
1378 sprintf(Buffer
, "COM%ld", i
+ 1);
1380 /* Build full device descriptor */
1381 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
1382 2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
) +
1383 sizeof(CM_SERIAL_DEVICE_DATA
);
1384 PartialResourceList
= MmHeapAlloc(Size
);
1385 if (PartialResourceList
== NULL
)
1387 DPRINTM(DPRINT_HWDETECT
,
1388 "Failed to allocate resource descriptor\n");
1391 memset(PartialResourceList
, 0, Size
);
1393 /* Initialize resource descriptor */
1394 PartialResourceList
->Version
= 1;
1395 PartialResourceList
->Revision
= 1;
1396 PartialResourceList
->Count
= 3;
1399 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
1400 PartialDescriptor
->Type
= CmResourceTypePort
;
1401 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
1402 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
1403 PartialDescriptor
->u
.Port
.Start
.LowPart
= Base
;
1404 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
1405 PartialDescriptor
->u
.Port
.Length
= 7;
1408 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[1];
1409 PartialDescriptor
->Type
= CmResourceTypeInterrupt
;
1410 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
1411 PartialDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
1412 PartialDescriptor
->u
.Interrupt
.Level
= Irq
[i
];
1413 PartialDescriptor
->u
.Interrupt
.Vector
= 0;
1414 PartialDescriptor
->u
.Interrupt
.Affinity
= 0xFFFFFFFF;
1416 /* Set serial data (device specific) */
1417 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[2];
1418 PartialDescriptor
->Type
= CmResourceTypeDeviceSpecific
;
1419 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
1420 PartialDescriptor
->Flags
= 0;
1421 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
= sizeof(CM_SERIAL_DEVICE_DATA
);
1424 (PCM_SERIAL_DEVICE_DATA
)&PartialResourceList
->PartialDescriptors
[3];
1425 SerialDeviceData
->BaudClock
= 1843200; /* UART Clock frequency (Hertz) */
1427 /* Create controller key */
1428 FldrCreateComponentKey(BusKey
,
1431 Output
| Input
| ConsoleIn
| ConsoleOut
,
1435 PartialResourceList
,
1439 MmHeapFree(PartialResourceList
);
1441 if (!Rs232PortInUse(Base
))
1443 /* Detect serial mouse */
1444 DetectSerialPointerPeripheral(ControllerKey
, UlongToPtr(Base
));
1453 DetectParallelPorts(PCONFIGURATION_COMPONENT_DATA BusKey
)
1455 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1456 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
1457 ULONG Irq
[3] = {7, 5, (ULONG
)-1};
1459 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1462 ULONG ControllerNumber
;
1466 DPRINTM(DPRINT_HWDETECT
, "DetectParallelPorts() called\n");
1468 ControllerNumber
= 0;
1469 BasePtr
= (PUSHORT
)0x408;
1470 for (i
= 0; i
< 3; i
++, BasePtr
++)
1472 Base
= (ULONG
)*BasePtr
;
1476 DPRINTM(DPRINT_HWDETECT
,
1477 "Parallel port %u: %x\n",
1481 /* Set 'Identifier' value */
1482 sprintf(Buffer
, "PARALLEL%ld", i
+ 1);
1484 /* Build full device descriptor */
1485 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
);
1486 if (Irq
[i
] != (ULONG
)-1)
1487 Size
+= sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
1489 PartialResourceList
= MmHeapAlloc(Size
);
1490 if (PartialResourceList
== NULL
)
1492 DPRINTM(DPRINT_HWDETECT
,
1493 "Failed to allocate resource descriptor\n");
1496 memset(PartialResourceList
, 0, Size
);
1498 /* Initialize resource descriptor */
1499 PartialResourceList
->Version
= 1;
1500 PartialResourceList
->Revision
= 1;
1501 PartialResourceList
->Count
= (Irq
[i
] != (ULONG
)-1) ? 2 : 1;
1504 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
1505 PartialDescriptor
->Type
= CmResourceTypePort
;
1506 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
1507 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
1508 PartialDescriptor
->u
.Port
.Start
.LowPart
= Base
;
1509 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
1510 PartialDescriptor
->u
.Port
.Length
= 3;
1513 if (Irq
[i
] != (ULONG
)-1)
1515 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[1];
1516 PartialDescriptor
->Type
= CmResourceTypeInterrupt
;
1517 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
1518 PartialDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
1519 PartialDescriptor
->u
.Interrupt
.Level
= Irq
[i
];
1520 PartialDescriptor
->u
.Interrupt
.Vector
= 0;
1521 PartialDescriptor
->u
.Interrupt
.Affinity
= 0xFFFFFFFF;
1524 /* Create controller key */
1525 FldrCreateComponentKey(BusKey
,
1532 PartialResourceList
,
1536 MmHeapFree(PartialResourceList
);
1541 DPRINTM(DPRINT_HWDETECT
, "DetectParallelPorts() done\n");
1546 DetectKeyboardDevice(VOID
)
1551 BOOLEAN Result
= TRUE
;
1553 /* Identify device */
1554 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
,
1557 /* Wait for reply */
1558 for (Loops
= 0; Loops
< 100; Loops
++)
1560 StallExecutionProcessor(10000);
1561 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1562 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) != 0)
1566 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) == 0)
1568 /* PC/XT keyboard or no keyboard */
1572 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1573 if (Scancode
!= 0xFA)
1575 /* No ACK received */
1579 StallExecutionProcessor(10000);
1581 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1582 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) == 0)
1584 /* Found AT keyboard */
1588 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1589 if (Scancode
!= 0xAB)
1591 /* No 0xAB received */
1595 StallExecutionProcessor(10000);
1597 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1598 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) == 0)
1600 /* No byte in buffer */
1604 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1605 if (Scancode
!= 0x41)
1607 /* No 0x41 received */
1611 /* Found MF-II keyboard */
1617 DetectKeyboardPeripheral(PCONFIGURATION_COMPONENT_DATA ControllerKey
)
1619 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1620 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
1621 PCM_KEYBOARD_DEVICE_DATA KeyboardData
;
1622 PCONFIGURATION_COMPONENT_DATA PeripheralKey
;
1625 /* HACK: don't call DetectKeyboardDevice() as it fails in Qemu 0.8.2 */
1626 if (TRUE
|| DetectKeyboardDevice())
1628 /* Set 'Configuration Data' value */
1629 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
1630 sizeof(CM_KEYBOARD_DEVICE_DATA
);
1631 PartialResourceList
= MmHeapAlloc(Size
);
1632 if (PartialResourceList
== NULL
)
1634 DPRINTM(DPRINT_HWDETECT
,
1635 "Failed to allocate resource descriptor\n");
1639 /* Initialize resource descriptor */
1640 memset(PartialResourceList
, 0, Size
);
1641 PartialResourceList
->Version
= 1;
1642 PartialResourceList
->Revision
= 1;
1643 PartialResourceList
->Count
= 1;
1645 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
1646 PartialDescriptor
->Type
= CmResourceTypeDeviceSpecific
;
1647 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
1648 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
= sizeof(CM_KEYBOARD_DEVICE_DATA
);
1650 KeyboardData
= (PCM_KEYBOARD_DEVICE_DATA
)(PartialDescriptor
+ 1);
1651 KeyboardData
->Version
= 1;
1652 KeyboardData
->Revision
= 1;
1653 KeyboardData
->Type
= 4;
1654 KeyboardData
->Subtype
= 0;
1655 KeyboardData
->KeyboardFlags
= 0x20;
1657 /* Create controller key */
1658 FldrCreateComponentKey(ControllerKey
,
1665 PartialResourceList
,
1668 DPRINTM(DPRINT_HWDETECT
, "Created key: KeyboardPeripheral\\0\n");
1670 MmHeapFree(PartialResourceList
);
1676 DetectKeyboardController(PCONFIGURATION_COMPONENT_DATA BusKey
)
1678 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1679 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
1680 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1683 /* Set 'Configuration Data' value */
1684 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
1685 2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
1686 PartialResourceList
= MmHeapAlloc(Size
);
1687 if (PartialResourceList
== NULL
)
1689 DPRINTM(DPRINT_HWDETECT
,
1690 "Failed to allocate resource descriptor\n");
1694 /* Initialize resource descriptor */
1695 memset(PartialResourceList
, 0, Size
);
1696 PartialResourceList
->Version
= 1;
1697 PartialResourceList
->Revision
= 1;
1698 PartialResourceList
->Count
= 3;
1701 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
1702 PartialDescriptor
->Type
= CmResourceTypeInterrupt
;
1703 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
1704 PartialDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
1705 PartialDescriptor
->u
.Interrupt
.Level
= 1;
1706 PartialDescriptor
->u
.Interrupt
.Vector
= 0;
1707 PartialDescriptor
->u
.Interrupt
.Affinity
= 0xFFFFFFFF;
1709 /* Set IO Port 0x60 */
1710 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[1];
1711 PartialDescriptor
->Type
= CmResourceTypePort
;
1712 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
1713 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
1714 PartialDescriptor
->u
.Port
.Start
.LowPart
= 0x60;
1715 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
1716 PartialDescriptor
->u
.Port
.Length
= 1;
1718 /* Set IO Port 0x64 */
1719 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[2];
1720 PartialDescriptor
->Type
= CmResourceTypePort
;
1721 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
1722 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
1723 PartialDescriptor
->u
.Port
.Start
.LowPart
= 0x64;
1724 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
1725 PartialDescriptor
->u
.Port
.Length
= 1;
1727 /* Create controller key */
1728 FldrCreateComponentKey(BusKey
,
1735 PartialResourceList
,
1738 DPRINTM(DPRINT_HWDETECT
, "Created key: KeyboardController\\0\n");
1740 MmHeapFree(PartialResourceList
);
1742 DetectKeyboardPeripheral(ControllerKey
);
1747 PS2ControllerWait(VOID
)
1752 for (Timeout
= 0; Timeout
< CONTROLLER_TIMEOUT
; Timeout
++)
1754 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1755 if ((Status
& CONTROLLER_STATUS_INPUT_BUFFER_FULL
) == 0)
1758 /* Sleep for one millisecond */
1759 StallExecutionProcessor(1000);
1765 DetectPS2AuxPort(VOID
)
1768 /* Current detection is too unreliable. Just do as if
1769 * the PS/2 aux port is always present
1776 /* Put the value 0x5A in the output buffer using the
1777 * "WriteAuxiliary Device Output Buffer" command (0xD3).
1778 * Poll the Status Register for a while to see if the value really turns up
1779 * in the Data Register. If the KEYBOARD_STATUS_MOUSE_OBF bit is also set
1780 * to 1 in the Status Register, we assume this controller has an
1781 * Auxiliary Port (a.k.a. Mouse Port).
1783 PS2ControllerWait();
1784 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_CONTROL
,
1785 CONTROLLER_COMMAND_WRITE_MOUSE_OUTPUT_BUFFER
);
1786 PS2ControllerWait();
1788 /* 0x5A is a random dummy value */
1789 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
,
1792 for (Loops
= 0; Loops
< 10; Loops
++)
1794 StallExecutionProcessor(10000);
1795 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1796 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) != 0)
1800 READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1802 return (Status
& CONTROLLER_STATUS_MOUSE_OUTPUT_BUFFER_FULL
);
1808 DetectPS2AuxDevice(VOID
)
1813 BOOLEAN Result
= TRUE
;
1815 PS2ControllerWait();
1816 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_CONTROL
,
1817 CONTROLLER_COMMAND_WRITE_MOUSE
);
1818 PS2ControllerWait();
1820 /* Identify device */
1821 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
,
1824 /* Wait for reply */
1825 for (Loops
= 0; Loops
< 100; Loops
++)
1827 StallExecutionProcessor(10000);
1828 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1829 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) != 0)
1833 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1834 if ((Status
& CONTROLLER_STATUS_MOUSE_OUTPUT_BUFFER_FULL
) == 0)
1837 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1838 if (Scancode
!= 0xFA)
1841 StallExecutionProcessor(10000);
1843 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1844 if ((Status
& CONTROLLER_STATUS_MOUSE_OUTPUT_BUFFER_FULL
) == 0)
1847 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1848 if (Scancode
!= 0x00)
1856 DetectPS2Mouse(PCONFIGURATION_COMPONENT_DATA BusKey
)
1858 CM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1859 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1860 PCONFIGURATION_COMPONENT_DATA PeripheralKey
;
1862 if (DetectPS2AuxPort())
1864 DPRINTM(DPRINT_HWDETECT
, "Detected PS2 port\n");
1866 memset(&PartialResourceList
, 0, sizeof(CM_PARTIAL_RESOURCE_LIST
));
1868 /* Initialize resource descriptor */
1869 PartialResourceList
.Version
= 1;
1870 PartialResourceList
.Revision
= 1;
1871 PartialResourceList
.Count
= 1;
1874 PartialResourceList
.PartialDescriptors
[0].Type
= CmResourceTypeInterrupt
;
1875 PartialResourceList
.PartialDescriptors
[0].ShareDisposition
= CmResourceShareUndetermined
;
1876 PartialResourceList
.PartialDescriptors
[0].Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
1877 PartialResourceList
.PartialDescriptors
[0].u
.Interrupt
.Level
= 12;
1878 PartialResourceList
.PartialDescriptors
[0].u
.Interrupt
.Vector
= 0;
1879 PartialResourceList
.PartialDescriptors
[0].u
.Interrupt
.Affinity
= 0xFFFFFFFF;
1881 /* Create controller key */
1882 FldrCreateComponentKey(BusKey
,
1889 &PartialResourceList
,
1890 sizeof(CM_PARTIAL_RESOURCE_LIST
),
1892 DPRINTM(DPRINT_HWDETECT
, "Created key: PointerController\\0\n");
1894 if (DetectPS2AuxDevice())
1896 DPRINTM(DPRINT_HWDETECT
, "Detected PS2 mouse\n");
1898 /* Initialize resource descriptor */
1899 memset(&PartialResourceList
, 0, sizeof(CM_PARTIAL_RESOURCE_LIST
));
1900 PartialResourceList
.Version
= 1;
1901 PartialResourceList
.Revision
= 1;
1902 PartialResourceList
.Count
= 0;
1904 /* Create peripheral key */
1905 FldrCreateComponentKey(ControllerKey
,
1911 "MICROSOFT PS2 MOUSE",
1912 &PartialResourceList
,
1913 sizeof(CM_PARTIAL_RESOURCE_LIST
) -
1914 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
),
1916 DPRINTM(DPRINT_HWDETECT
, "Created key: PointerPeripheral\\0\n");
1923 DetectDisplayController(PCONFIGURATION_COMPONENT_DATA BusKey
)
1926 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1929 /* FIXME: Set 'ComponentInformation' value */
1931 VesaVersion
= BiosIsVesaSupported();
1932 if (VesaVersion
!= 0)
1934 DPRINTM(DPRINT_HWDETECT
,
1935 "VESA version %c.%c\n",
1936 (VesaVersion
>> 8) + '0',
1937 (VesaVersion
& 0xFF) + '0');
1941 DPRINTM(DPRINT_HWDETECT
,
1942 "VESA not supported\n");
1945 if (VesaVersion
>= 0x0200)
1956 FldrCreateComponentKey(BusKey
,
1966 DPRINTM(DPRINT_HWDETECT
, "Created key: DisplayController\\0\n");
1968 /* FIXME: Add display peripheral (monitor) data */
1973 DetectIsaBios(PCONFIGURATION_COMPONENT_DATA SystemKey
, ULONG
*BusNumber
)
1975 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1976 PCONFIGURATION_COMPONENT_DATA BusKey
;
1979 /* Increment bus number */
1982 /* Set 'Configuration Data' value */
1983 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) -
1984 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
1985 PartialResourceList
= MmHeapAlloc(Size
);
1986 if (PartialResourceList
== NULL
)
1988 DPRINTM(DPRINT_HWDETECT
,
1989 "Failed to allocate resource descriptor\n");
1993 /* Initialize resource descriptor */
1994 memset(PartialResourceList
, 0, Size
);
1995 PartialResourceList
->Version
= 1;
1996 PartialResourceList
->Revision
= 1;
1997 PartialResourceList
->Count
= 0;
1999 /* Create new bus key */
2000 FldrCreateComponentKey(SystemKey
,
2002 MultiFunctionAdapter
,
2007 PartialResourceList
,
2011 MmHeapFree(PartialResourceList
);
2013 /* Detect ISA/BIOS devices */
2014 DetectBiosDisks(BusKey
);
2016 DetectBiosFloppyController(BusKey
);
2018 DetectSerialPorts(BusKey
);
2020 DetectParallelPorts(BusKey
);
2022 DetectKeyboardController(BusKey
);
2024 DetectPS2Mouse(BusKey
);
2026 DetectDisplayController(BusKey
);
2028 /* FIXME: Detect more ISA devices */
2032 PCONFIGURATION_COMPONENT_DATA
2035 PCONFIGURATION_COMPONENT_DATA SystemKey
;
2036 ULONG BusNumber
= 0;
2038 DPRINTM(DPRINT_HWDETECT
, "DetectHardware()\n");
2040 /* Create the 'System' key */
2041 SystemKey
= DetectSystem();
2044 DetectPciBios(SystemKey
, &BusNumber
);
2045 DetectApmBios(SystemKey
, &BusNumber
);
2046 DetectPnpBios(SystemKey
, &BusNumber
);
2047 DetectIsaBios(SystemKey
, &BusNumber
);
2048 DetectAcpiBios(SystemKey
, &BusNumber
);
2050 DPRINTM(DPRINT_HWDETECT
, "DetectHardware() Done\n");