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
;
470 ULONG i
, Length
, Sectors
;
478 if (Length
> DISKREADBUFFER_SIZE
)
479 Length
= DISKREADBUFFER_SIZE
;
480 Sectors
= (Length
+ Context
->SectorSize
- 1) / Context
->SectorSize
;
481 ret
= MachDiskReadLogicalSectors(
482 Context
->DriveNumber
,
483 Context
->SectorNumber
+ Context
->SectorOffset
+ i
,
485 (PVOID
)DISKREADBUFFER
);
488 RtlCopyMemory(Ptr
, (PVOID
)DISKREADBUFFER
, Length
);
498 static LONG
DiskSeek(ULONG FileId
, LARGE_INTEGER
* Position
, SEEKMODE SeekMode
)
500 DISKCONTEXT
* Context
= FsGetDeviceSpecific(FileId
);
502 if (SeekMode
!= SeekAbsolute
)
504 if (Position
->LowPart
& (Context
->SectorSize
- 1))
507 /* FIXME: take HighPart into account */
508 Context
->SectorNumber
= Position
->LowPart
/ Context
->SectorSize
;
512 static const DEVVTBL DiskVtbl
= {
514 DiskGetFileInformation
,
521 GetHarddiskIdentifier(PCHAR Identifier
,
524 PMASTER_BOOT_RECORD Mbr
;
530 PARTITION_TABLE_ENTRY PartitionTableEntry
;
533 if (!MachDiskReadLogicalSectors(DriveNumber
, 0ULL, 1, (PVOID
)DISKREADBUFFER
))
535 DPRINTM(DPRINT_HWDETECT
, "Reading MBR failed\n");
539 Buffer
= (ULONG
*)DISKREADBUFFER
;
540 Mbr
= (PMASTER_BOOT_RECORD
)DISKREADBUFFER
;
542 Signature
= Mbr
->Signature
;
543 DPRINTM(DPRINT_HWDETECT
, "Signature: %x\n", Signature
);
545 /* Calculate the MBR checksum */
547 for (i
= 0; i
< 128; i
++)
549 Checksum
+= Buffer
[i
];
551 Checksum
= ~Checksum
+ 1;
552 DPRINTM(DPRINT_HWDETECT
, "Checksum: %x\n", Checksum
);
554 /* Fill out the ARC disk block */
555 reactos_arc_disk_info
[reactos_disk_count
].Signature
= Signature
;
556 reactos_arc_disk_info
[reactos_disk_count
].CheckSum
= Checksum
;
557 sprintf(ArcName
, "multi(0)disk(0)rdisk(%lu)", reactos_disk_count
);
558 strcpy(reactos_arc_strings
[reactos_disk_count
], ArcName
);
559 reactos_arc_disk_info
[reactos_disk_count
].ArcName
=
560 reactos_arc_strings
[reactos_disk_count
];
561 reactos_disk_count
++;
563 sprintf(ArcName
, "multi(0)disk(0)rdisk(%lu)partition(0)", DriveNumber
- 0x80);
564 FsRegisterDevice(ArcName
, &DiskVtbl
);
568 DiskReportError(FALSE
);
569 while (DiskGetPartitionEntry(DriveNumber
, i
, &PartitionTableEntry
))
571 if (PartitionTableEntry
.SystemIndicator
!= PARTITION_ENTRY_UNUSED
)
573 sprintf(ArcName
, "multi(0)disk(0)rdisk(%lu)partition(%lu)", DriveNumber
- 0x80, i
);
574 FsRegisterDevice(ArcName
, &DiskVtbl
);
578 DiskReportError(TRUE
);
580 /* Convert checksum and signature to identifier string */
581 Identifier
[0] = Hex
[(Checksum
>> 28) & 0x0F];
582 Identifier
[1] = Hex
[(Checksum
>> 24) & 0x0F];
583 Identifier
[2] = Hex
[(Checksum
>> 20) & 0x0F];
584 Identifier
[3] = Hex
[(Checksum
>> 16) & 0x0F];
585 Identifier
[4] = Hex
[(Checksum
>> 12) & 0x0F];
586 Identifier
[5] = Hex
[(Checksum
>> 8) & 0x0F];
587 Identifier
[6] = Hex
[(Checksum
>> 4) & 0x0F];
588 Identifier
[7] = Hex
[Checksum
& 0x0F];
590 Identifier
[9] = Hex
[(Signature
>> 28) & 0x0F];
591 Identifier
[10] = Hex
[(Signature
>> 24) & 0x0F];
592 Identifier
[11] = Hex
[(Signature
>> 20) & 0x0F];
593 Identifier
[12] = Hex
[(Signature
>> 16) & 0x0F];
594 Identifier
[13] = Hex
[(Signature
>> 12) & 0x0F];
595 Identifier
[14] = Hex
[(Signature
>> 8) & 0x0F];
596 Identifier
[15] = Hex
[(Signature
>> 4) & 0x0F];
597 Identifier
[16] = Hex
[Signature
& 0x0F];
598 Identifier
[17] = '-';
599 Identifier
[18] = 'A';
601 DPRINTM(DPRINT_HWDETECT
, "Identifier: %s\n", Identifier
);
609 WRITE_PORT_UCHAR((PUCHAR
)0x70, 0x10);
610 Data
= READ_PORT_UCHAR((PUCHAR
)0x71);
612 return ((Data
& 0xF0) ? 1 : 0) + ((Data
& 0x0F) ? 1 : 0);
617 GetFloppyType(UCHAR DriveNumber
)
621 WRITE_PORT_UCHAR((PUCHAR
)0x70, 0x10);
622 Data
= READ_PORT_UCHAR((PUCHAR
)0x71);
624 if (DriveNumber
== 0)
626 else if (DriveNumber
== 1)
636 PUSHORT SegPtr
= (PUSHORT
)0x7A;
637 PUSHORT OfsPtr
= (PUSHORT
)0x78;
639 return (PVOID
)((ULONG_PTR
)(((ULONG
)(*SegPtr
)) << 4) + (ULONG
)(*OfsPtr
));
644 DetectBiosFloppyPeripheral(PCONFIGURATION_COMPONENT_DATA ControllerKey
)
646 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
647 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
648 PCM_FLOPPY_DEVICE_DATA FloppyData
;
650 PCONFIGURATION_COMPONENT_DATA PeripheralKey
;
654 ULONG MaxDensity
[6] = {0, 360, 1200, 720, 1440, 2880};
657 for (FloppyNumber
= 0; FloppyNumber
< 2; FloppyNumber
++)
659 FloppyType
= GetFloppyType(FloppyNumber
);
661 if ((FloppyType
> 5) || (FloppyType
== 0))
664 DiskResetController(FloppyNumber
);
666 Ptr
= GetInt1eTable();
668 /* Set 'Identifier' value */
669 sprintf(Identifier
, "FLOPPY%ld", FloppyNumber
+ 1);
671 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
672 sizeof(CM_FLOPPY_DEVICE_DATA
);
673 PartialResourceList
= MmHeapAlloc(Size
);
674 if (PartialResourceList
== NULL
)
676 DPRINTM(DPRINT_HWDETECT
,
677 "Failed to allocate resource descriptor\n");
681 memset(PartialResourceList
, 0, Size
);
682 PartialResourceList
->Version
= 1;
683 PartialResourceList
->Revision
= 1;
684 PartialResourceList
->Count
= 1;
686 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
687 PartialDescriptor
->Type
= CmResourceTypeDeviceSpecific
;
688 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
689 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
= sizeof(CM_FLOPPY_DEVICE_DATA
);
691 FloppyData
= (PVOID
)(((ULONG_PTR
)PartialResourceList
) + sizeof(CM_PARTIAL_RESOURCE_LIST
));
692 FloppyData
->Version
= 2;
693 FloppyData
->Revision
= 0;
694 FloppyData
->MaxDensity
= MaxDensity
[FloppyType
];
695 FloppyData
->MountDensity
= 0;
696 RtlCopyMemory(&FloppyData
->StepRateHeadUnloadTime
,
699 FloppyData
->MaximumTrackValue
= (FloppyType
== 1) ? 39 : 79;
700 FloppyData
->DataTransferRate
= 0;
702 FldrCreateComponentKey(ControllerKey
,
704 FloppyDiskPeripheral
,
713 MmHeapFree(PartialResourceList
);
719 DetectBiosFloppyController(PCONFIGURATION_COMPONENT_DATA BusKey
)
721 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
722 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
723 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
727 FloppyCount
= GetFloppyCount();
728 DPRINTM(DPRINT_HWDETECT
,
729 "Floppy count: %u\n",
732 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
733 2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
734 PartialResourceList
= MmHeapAlloc(Size
);
735 if (PartialResourceList
== NULL
)
737 DPRINTM(DPRINT_HWDETECT
,
738 "Failed to allocate resource descriptor\n");
741 memset(PartialResourceList
, 0, Size
);
743 /* Initialize resource descriptor */
744 PartialResourceList
->Version
= 1;
745 PartialResourceList
->Revision
= 1;
746 PartialResourceList
->Count
= 3;
749 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
750 PartialDescriptor
->Type
= CmResourceTypePort
;
751 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
752 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
753 PartialDescriptor
->u
.Port
.Start
.LowPart
= 0x03F0;
754 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
755 PartialDescriptor
->u
.Port
.Length
= 8;
758 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[1];
759 PartialDescriptor
->Type
= CmResourceTypeInterrupt
;
760 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
761 PartialDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
762 PartialDescriptor
->u
.Interrupt
.Level
= 6;
763 PartialDescriptor
->u
.Interrupt
.Vector
= 6;
764 PartialDescriptor
->u
.Interrupt
.Affinity
= 0xFFFFFFFF;
766 /* Set DMA channel */
767 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[2];
768 PartialDescriptor
->Type
= CmResourceTypeDma
;
769 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
770 PartialDescriptor
->Flags
= 0;
771 PartialDescriptor
->u
.Dma
.Channel
= 2;
772 PartialDescriptor
->u
.Dma
.Port
= 0;
774 /* Create floppy disk controller */
775 FldrCreateComponentKey(BusKey
,
785 DPRINTM(DPRINT_HWDETECT
, "Created key: DiskController\\0\n");
787 MmHeapFree(PartialResourceList
);
789 if (FloppyCount
) DetectBiosFloppyPeripheral(ControllerKey
);
792 static PCONFIGURATION_COMPONENT_DATA
795 PCONFIGURATION_COMPONENT_DATA SystemKey
;
796 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
797 PCM_INT13_DRIVE_PARAMETER Int13Drives
;
804 /* Count the number of visible drives */
805 DiskReportError(FALSE
);
808 /* There are some really broken BIOSes out there. There are even BIOSes
809 * that happily report success when you ask them to read from non-existent
810 * harddisks. So, we set the buffer to known contents first, then try to
811 * read. If the BIOS reports success but the buffer contents haven't
812 * changed then we fail anyway */
813 memset((PVOID
) DISKREADBUFFER
, 0xcd, 512);
814 while (MachDiskReadLogicalSectors(0x80 + DiskCount
, 0ULL, 1, (PVOID
)DISKREADBUFFER
))
817 for (i
= 0; ! Changed
&& i
< 512; i
++)
819 Changed
= ((PUCHAR
)DISKREADBUFFER
)[i
] != 0xcd;
823 DPRINTM(DPRINT_HWDETECT
, "BIOS reports success for disk %d but data didn't change\n",
828 memset((PVOID
) DISKREADBUFFER
, 0xcd, 512);
830 DiskReportError(TRUE
);
831 DPRINTM(DPRINT_HWDETECT
, "BIOS reports %d harddisk%s\n",
832 (int)DiskCount
, (DiskCount
== 1) ? "": "s");
834 /* Allocate resource descriptor */
835 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
836 sizeof(CM_INT13_DRIVE_PARAMETER
) * DiskCount
;
837 PartialResourceList
= MmHeapAlloc(Size
);
838 if (PartialResourceList
== NULL
)
840 DPRINTM(DPRINT_HWDETECT
,
841 "Failed to allocate resource descriptor\n");
845 /* Initialize resource descriptor */
846 memset(PartialResourceList
, 0, Size
);
847 PartialResourceList
->Version
= 1;
848 PartialResourceList
->Revision
= 1;
849 PartialResourceList
->Count
= 1;
850 PartialResourceList
->PartialDescriptors
[0].Type
= CmResourceTypeDeviceSpecific
;
851 PartialResourceList
->PartialDescriptors
[0].ShareDisposition
= 0;
852 PartialResourceList
->PartialDescriptors
[0].Flags
= 0;
853 PartialResourceList
->PartialDescriptors
[0].u
.DeviceSpecificData
.DataSize
=
854 sizeof(CM_INT13_DRIVE_PARAMETER
) * DiskCount
;
856 /* Get harddisk Int13 geometry data */
857 Int13Drives
= (PVOID
)(((ULONG_PTR
)PartialResourceList
) + sizeof(CM_PARTIAL_RESOURCE_LIST
));
858 for (i
= 0; i
< DiskCount
; i
++)
860 if (MachDiskGetDriveGeometry(0x80 + i
, &Geometry
))
862 Int13Drives
[i
].DriveSelect
= 0x80 + i
;
863 Int13Drives
[i
].MaxCylinders
= Geometry
.Cylinders
- 1;
864 Int13Drives
[i
].SectorsPerTrack
= Geometry
.Sectors
;
865 Int13Drives
[i
].MaxHeads
= Geometry
.Heads
- 1;
866 Int13Drives
[i
].NumberDrives
= DiskCount
;
868 DPRINTM(DPRINT_HWDETECT
,
869 "Disk %x: %u Cylinders %u Heads %u Sectors %u Bytes\n",
871 Geometry
.Cylinders
- 1,
874 Geometry
.BytesPerSector
);
878 FldrCreateComponentKey(NULL
,
889 MmHeapFree(PartialResourceList
);
895 GetDiskCount(PCONFIGURATION_COMPONENT_DATA BusKey
)
897 PCONFIGURATION_COMPONENT_DATA System
;
898 ULONG ConfigurationDataLength
;
902 // Get root component
905 while (System
->Parent
)
906 System
= System
->Parent
;
909 // Get root configuration data length
911 ConfigurationDataLength
= System
->ComponentEntry
.ConfigurationDataLength
;
914 // We assume that nothing wrong happened, and that configuration
915 // only consists of one CM_PARTIAL_RESOURCE_LIST entry, followed
916 // by n entries of CM_INT13_DRIVE_PARAMETER
918 if (ConfigurationDataLength
> 0)
919 DiskCount
= (ConfigurationDataLength
- sizeof(CM_PARTIAL_RESOURCE_LIST
))
920 / sizeof(CM_INT13_DRIVE_PARAMETER
);
923 // Return number of disks
925 DPRINTM(DPRINT_HWDETECT
, "Retrieving %lu INT13 disks\\0\n", DiskCount
);
930 DetectBiosDisks(PCONFIGURATION_COMPONENT_DATA BusKey
)
932 PCONFIGURATION_COMPONENT_DATA DiskKey
, ControllerKey
;
933 BOOLEAN BootDriveReported
= FALSE
;
935 ULONG DiskCount
= GetDiskCount(BusKey
);
938 FldrCreateComponentKey(BusKey
,
948 DPRINTM(DPRINT_HWDETECT
, "Created key: DiskController\\0\n");
950 /* Create and fill subkey for each harddisk */
951 for (i
= 0; i
< DiskCount
; i
++)
953 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
957 if (BootDrive
== 0x80 + i
)
958 BootDriveReported
= TRUE
;
960 /* Get disk values */
961 PartialResourceList
= GetHarddiskConfigurationData(0x80 + i
, &Size
);
962 GetHarddiskIdentifier(Identifier
, 0x80 + i
);
964 /* Create disk key */
965 FldrCreateComponentKey(ControllerKey
,
977 /* Get the drive we're booting from */
978 MachDiskGetBootPath(BootPath
, sizeof(BootPath
));
980 /* Add it, if it's a floppy or cdrom */
981 if ((BootDrive
>= 0x80 && !BootDriveReported
) ||
982 DiskIsDriveRemovable(BootDrive
))
984 /* TODO: Check if it's really a cdrom drive */
989 if (!MachDiskReadLogicalSectors(BootDrive
, 16ULL, 1, (PVOID
)DISKREADBUFFER
))
991 DPRINTM(DPRINT_HWDETECT
, "Reading MBR failed\n");
995 Buffer
= (ULONG
*)DISKREADBUFFER
;
997 /* Calculate the MBR checksum */
998 for (i
= 0; i
< 2048 / sizeof(ULONG
); i
++) Checksum
+= Buffer
[i
];
999 DPRINTM(DPRINT_HWDETECT
, "Checksum: %x\n", Checksum
);
1001 /* Fill out the ARC disk block */
1002 reactos_arc_disk_info
[reactos_disk_count
].CheckSum
= Checksum
;
1003 strcpy(reactos_arc_strings
[reactos_disk_count
], BootPath
);
1004 reactos_arc_disk_info
[reactos_disk_count
].ArcName
=
1005 reactos_arc_strings
[reactos_disk_count
];
1006 reactos_disk_count
++;
1008 FsRegisterDevice(BootPath
, &DiskVtbl
);
1013 InitializeSerialPort(PUCHAR Port
,
1016 WRITE_PORT_UCHAR(Port
+ 3, 0x80); /* set DLAB on */
1017 WRITE_PORT_UCHAR(Port
, 0x60); /* speed LO byte */
1018 WRITE_PORT_UCHAR(Port
+ 1, 0); /* speed HI byte */
1019 WRITE_PORT_UCHAR(Port
+ 3, LineControl
);
1020 WRITE_PORT_UCHAR(Port
+ 1, 0); /* set comm and DLAB to 0 */
1021 WRITE_PORT_UCHAR(Port
+ 4, 0x09); /* DR int enable */
1022 READ_PORT_UCHAR(Port
+ 5); /* clear error bits */
1027 DetectSerialMouse(PUCHAR Port
)
1034 /* Shutdown mouse or something like that */
1035 LineControl
= READ_PORT_UCHAR(Port
+ 4);
1036 WRITE_PORT_UCHAR(Port
+ 4, (LineControl
& ~0x02) | 0x01);
1037 StallExecutionProcessor(100000);
1041 * Maybe there is no serial port although BIOS reported one (this
1042 * is the case on Apple hardware), or the serial port is misbehaving,
1043 * therefore we must give up after some time.
1046 while (READ_PORT_UCHAR(Port
+ 5) & 0x01)
1049 return MOUSE_TYPE_NONE
;
1050 READ_PORT_UCHAR(Port
);
1054 * Send modem control with 'Data Terminal Ready', 'Request To Send' and
1055 * 'Output Line 2' message. This enables mouse to identify.
1057 WRITE_PORT_UCHAR(Port
+ 4, 0x0b);
1059 /* Wait 10 milliseconds for the mouse getting ready */
1060 StallExecutionProcessor(10000);
1062 /* Read first four bytes, which contains Microsoft Mouse signs */
1064 for (i
= 0; i
< 4; i
++)
1066 while (((READ_PORT_UCHAR(Port
+ 5) & 1) == 0) && (TimeOut
> 0))
1068 StallExecutionProcessor(1000);
1071 return MOUSE_TYPE_NONE
;
1073 Buffer
[i
] = READ_PORT_UCHAR(Port
);
1076 DPRINTM(DPRINT_HWDETECT
,
1077 "Mouse data: %x %x %x %x\n",
1078 Buffer
[0],Buffer
[1],Buffer
[2],Buffer
[3]);
1080 /* Check that four bytes for signs */
1081 for (i
= 0; i
< 4; ++i
)
1083 if (Buffer
[i
] == 'B')
1085 /* Sign for Microsoft Ballpoint */
1086 // DbgPrint("Microsoft Ballpoint device detected\n");
1087 // DbgPrint("THIS DEVICE IS NOT SUPPORTED, YET\n");
1088 return MOUSE_TYPE_NONE
;
1090 else if (Buffer
[i
] == 'M')
1092 /* Sign for Microsoft Mouse protocol followed by button specifier */
1095 /* Overflow Error */
1096 return MOUSE_TYPE_NONE
;
1099 switch (Buffer
[i
+ 1])
1102 DPRINTM(DPRINT_HWDETECT
,
1103 "Microsoft Mouse with 3-buttons detected\n");
1104 return MOUSE_TYPE_LOGITECH
;
1107 DPRINTM(DPRINT_HWDETECT
,
1108 "Microsoft Wheel Mouse detected\n");
1109 return MOUSE_TYPE_WHEELZ
;
1113 DPRINTM(DPRINT_HWDETECT
,
1114 "Microsoft Mouse with 2-buttons detected\n");
1115 return MOUSE_TYPE_MICROSOFT
;
1120 return MOUSE_TYPE_NONE
;
1125 GetSerialMousePnpId(PUCHAR Port
, char *Buffer
)
1132 WRITE_PORT_UCHAR(Port
+ 4, 0x09);
1134 /* Wait 10 milliseconds for the mouse getting ready */
1135 StallExecutionProcessor(10000);
1137 WRITE_PORT_UCHAR(Port
+ 4, 0x0b);
1139 StallExecutionProcessor(10000);
1144 while (((READ_PORT_UCHAR(Port
+ 5) & 1) == 0) && (TimeOut
> 0))
1146 StallExecutionProcessor(1000);
1154 c
= READ_PORT_UCHAR(Port
);
1155 if (c
== 0x08 || c
== 0x28)
1165 while (((READ_PORT_UCHAR(Port
+ 5) & 1) == 0) && (TimeOut
> 0))
1167 StallExecutionProcessor(1000);
1172 c
= READ_PORT_UCHAR(Port
);
1185 DetectSerialPointerPeripheral(PCONFIGURATION_COMPONENT_DATA ControllerKey
,
1188 CM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1190 CHAR Identifier
[256];
1191 PCONFIGURATION_COMPONENT_DATA PeripheralKey
;
1198 DPRINTM(DPRINT_HWDETECT
,
1199 "DetectSerialPointerPeripheral()\n");
1203 InitializeSerialPort(Base
, 2);
1204 MouseType
= DetectSerialMouse(Base
);
1206 if (MouseType
!= MOUSE_TYPE_NONE
)
1208 Length
= GetSerialMousePnpId(Base
, Buffer
);
1209 DPRINTM(DPRINT_HWDETECT
,
1210 "PnP ID length: %u\n",
1215 /* Convert PnP sting to ASCII */
1216 if (Buffer
[0] == 0x08)
1218 for (i
= 0; i
< Length
; i
++)
1223 DPRINTM(DPRINT_HWDETECT
,
1224 "PnP ID string: %s\n",
1227 /* Copy PnpId string */
1228 for (i
= 0; i
< 7; i
++)
1230 Identifier
[i
] = Buffer
[3+i
];
1232 memcpy(&Identifier
[7],
1236 /* Skip device serial number */
1238 if (Buffer
[i
] == '\\')
1240 for (j
= ++i
; i
< Length
; ++i
)
1242 if (Buffer
[i
] == '\\')
1249 /* Skip PnP class */
1250 if (Buffer
[i
] == '\\')
1252 for (j
= ++i
; i
< Length
; ++i
)
1254 if (Buffer
[i
] == '\\')
1262 /* Skip compatible PnP Id */
1263 if (Buffer
[i
] == '\\')
1265 for (j
= ++i
; i
< Length
; ++i
)
1267 if (Buffer
[i
] == '\\')
1270 if (Buffer
[j
] == '*')
1276 /* Get product description */
1277 if (Buffer
[i
] == '\\')
1279 for (j
= ++i
; i
< Length
; ++i
)
1281 if (Buffer
[i
] == ';')
1288 for (k
= 0; k
< i
- j
; k
++)
1290 Identifier
[k
+ 10] = Buffer
[k
+ j
];
1292 Identifier
[10 + (i
-j
)] = 0;
1296 DPRINTM(DPRINT_HWDETECT
,
1297 "Identifier string: %s\n",
1301 if (Length
== 0 || strlen(Identifier
) < 11)
1305 case MOUSE_TYPE_LOGITECH
:
1307 "LOGITECH SERIAL MOUSE");
1310 case MOUSE_TYPE_WHEELZ
:
1312 "MICROSOFT SERIAL MOUSE WITH WHEEL");
1315 case MOUSE_TYPE_MICROSOFT
:
1318 "MICROSOFT SERIAL MOUSE");
1323 /* Set 'Configuration Data' value */
1324 memset(&PartialResourceList
, 0, sizeof(CM_PARTIAL_RESOURCE_LIST
));
1325 PartialResourceList
.Version
= 1;
1326 PartialResourceList
.Revision
= 1;
1327 PartialResourceList
.Count
= 0;
1329 /* Create 'PointerPeripheral' key */
1330 FldrCreateComponentKey(ControllerKey
,
1337 &PartialResourceList
,
1338 sizeof(CM_PARTIAL_RESOURCE_LIST
) -
1339 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
),
1342 DPRINTM(DPRINT_HWDETECT
,
1343 "Created key: PointerPeripheral\\0\n");
1349 DetectSerialPorts(PCONFIGURATION_COMPONENT_DATA BusKey
)
1351 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1352 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
1353 PCM_SERIAL_DEVICE_DATA SerialDeviceData
;
1354 ULONG Irq
[4] = {4, 3, 4, 3};
1358 ULONG ControllerNumber
= 0;
1359 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1363 DPRINTM(DPRINT_HWDETECT
, "DetectSerialPorts()\n");
1365 ControllerNumber
= 0;
1366 BasePtr
= (PUSHORT
)0x400;
1367 for (i
= 0; i
< 4; i
++, BasePtr
++)
1369 Base
= (ULONG
)*BasePtr
;
1373 DPRINTM(DPRINT_HWDETECT
,
1374 "Found COM%u port at 0x%x\n",
1378 /* Set 'Identifier' value */
1379 sprintf(Buffer
, "COM%ld", i
+ 1);
1381 /* Build full device descriptor */
1382 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
1383 2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
) +
1384 sizeof(CM_SERIAL_DEVICE_DATA
);
1385 PartialResourceList
= MmHeapAlloc(Size
);
1386 if (PartialResourceList
== NULL
)
1388 DPRINTM(DPRINT_HWDETECT
,
1389 "Failed to allocate resource descriptor\n");
1392 memset(PartialResourceList
, 0, Size
);
1394 /* Initialize resource descriptor */
1395 PartialResourceList
->Version
= 1;
1396 PartialResourceList
->Revision
= 1;
1397 PartialResourceList
->Count
= 3;
1400 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
1401 PartialDescriptor
->Type
= CmResourceTypePort
;
1402 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
1403 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
1404 PartialDescriptor
->u
.Port
.Start
.LowPart
= Base
;
1405 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
1406 PartialDescriptor
->u
.Port
.Length
= 7;
1409 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[1];
1410 PartialDescriptor
->Type
= CmResourceTypeInterrupt
;
1411 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
1412 PartialDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
1413 PartialDescriptor
->u
.Interrupt
.Level
= Irq
[i
];
1414 PartialDescriptor
->u
.Interrupt
.Vector
= 0;
1415 PartialDescriptor
->u
.Interrupt
.Affinity
= 0xFFFFFFFF;
1417 /* Set serial data (device specific) */
1418 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[2];
1419 PartialDescriptor
->Type
= CmResourceTypeDeviceSpecific
;
1420 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
1421 PartialDescriptor
->Flags
= 0;
1422 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
= sizeof(CM_SERIAL_DEVICE_DATA
);
1425 (PCM_SERIAL_DEVICE_DATA
)&PartialResourceList
->PartialDescriptors
[3];
1426 SerialDeviceData
->BaudClock
= 1843200; /* UART Clock frequency (Hertz) */
1428 /* Create controller key */
1429 FldrCreateComponentKey(BusKey
,
1432 Output
| Input
| ConsoleIn
| ConsoleOut
,
1436 PartialResourceList
,
1440 MmHeapFree(PartialResourceList
);
1442 if (!Rs232PortInUse(Base
))
1444 /* Detect serial mouse */
1445 DetectSerialPointerPeripheral(ControllerKey
, UlongToPtr(Base
));
1454 DetectParallelPorts(PCONFIGURATION_COMPONENT_DATA BusKey
)
1456 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1457 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
1458 ULONG Irq
[3] = {7, 5, (ULONG
)-1};
1460 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1463 ULONG ControllerNumber
;
1467 DPRINTM(DPRINT_HWDETECT
, "DetectParallelPorts() called\n");
1469 ControllerNumber
= 0;
1470 BasePtr
= (PUSHORT
)0x408;
1471 for (i
= 0; i
< 3; i
++, BasePtr
++)
1473 Base
= (ULONG
)*BasePtr
;
1477 DPRINTM(DPRINT_HWDETECT
,
1478 "Parallel port %u: %x\n",
1482 /* Set 'Identifier' value */
1483 sprintf(Buffer
, "PARALLEL%ld", i
+ 1);
1485 /* Build full device descriptor */
1486 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
);
1487 if (Irq
[i
] != (ULONG
)-1)
1488 Size
+= sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
1490 PartialResourceList
= MmHeapAlloc(Size
);
1491 if (PartialResourceList
== NULL
)
1493 DPRINTM(DPRINT_HWDETECT
,
1494 "Failed to allocate resource descriptor\n");
1497 memset(PartialResourceList
, 0, Size
);
1499 /* Initialize resource descriptor */
1500 PartialResourceList
->Version
= 1;
1501 PartialResourceList
->Revision
= 1;
1502 PartialResourceList
->Count
= (Irq
[i
] != (ULONG
)-1) ? 2 : 1;
1505 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
1506 PartialDescriptor
->Type
= CmResourceTypePort
;
1507 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
1508 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
1509 PartialDescriptor
->u
.Port
.Start
.LowPart
= Base
;
1510 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
1511 PartialDescriptor
->u
.Port
.Length
= 3;
1514 if (Irq
[i
] != (ULONG
)-1)
1516 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[1];
1517 PartialDescriptor
->Type
= CmResourceTypeInterrupt
;
1518 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
1519 PartialDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
1520 PartialDescriptor
->u
.Interrupt
.Level
= Irq
[i
];
1521 PartialDescriptor
->u
.Interrupt
.Vector
= 0;
1522 PartialDescriptor
->u
.Interrupt
.Affinity
= 0xFFFFFFFF;
1525 /* Create controller key */
1526 FldrCreateComponentKey(BusKey
,
1533 PartialResourceList
,
1537 MmHeapFree(PartialResourceList
);
1542 DPRINTM(DPRINT_HWDETECT
, "DetectParallelPorts() done\n");
1547 DetectKeyboardDevice(VOID
)
1552 BOOLEAN Result
= TRUE
;
1554 /* Identify device */
1555 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
,
1558 /* Wait for reply */
1559 for (Loops
= 0; Loops
< 100; Loops
++)
1561 StallExecutionProcessor(10000);
1562 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1563 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) != 0)
1567 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) == 0)
1569 /* PC/XT keyboard or no keyboard */
1573 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1574 if (Scancode
!= 0xFA)
1576 /* No ACK received */
1580 StallExecutionProcessor(10000);
1582 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1583 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) == 0)
1585 /* Found AT keyboard */
1589 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1590 if (Scancode
!= 0xAB)
1592 /* No 0xAB received */
1596 StallExecutionProcessor(10000);
1598 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1599 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) == 0)
1601 /* No byte in buffer */
1605 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1606 if (Scancode
!= 0x41)
1608 /* No 0x41 received */
1612 /* Found MF-II keyboard */
1618 DetectKeyboardPeripheral(PCONFIGURATION_COMPONENT_DATA ControllerKey
)
1620 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1621 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
1622 PCM_KEYBOARD_DEVICE_DATA KeyboardData
;
1623 PCONFIGURATION_COMPONENT_DATA PeripheralKey
;
1626 /* HACK: don't call DetectKeyboardDevice() as it fails in Qemu 0.8.2 */
1627 if (TRUE
|| DetectKeyboardDevice())
1629 /* Set 'Configuration Data' value */
1630 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
1631 sizeof(CM_KEYBOARD_DEVICE_DATA
);
1632 PartialResourceList
= MmHeapAlloc(Size
);
1633 if (PartialResourceList
== NULL
)
1635 DPRINTM(DPRINT_HWDETECT
,
1636 "Failed to allocate resource descriptor\n");
1640 /* Initialize resource descriptor */
1641 memset(PartialResourceList
, 0, Size
);
1642 PartialResourceList
->Version
= 1;
1643 PartialResourceList
->Revision
= 1;
1644 PartialResourceList
->Count
= 1;
1646 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
1647 PartialDescriptor
->Type
= CmResourceTypeDeviceSpecific
;
1648 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
1649 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
= sizeof(CM_KEYBOARD_DEVICE_DATA
);
1651 KeyboardData
= (PCM_KEYBOARD_DEVICE_DATA
)(PartialDescriptor
+ 1);
1652 KeyboardData
->Version
= 1;
1653 KeyboardData
->Revision
= 1;
1654 KeyboardData
->Type
= 4;
1655 KeyboardData
->Subtype
= 0;
1656 KeyboardData
->KeyboardFlags
= 0x20;
1658 /* Create controller key */
1659 FldrCreateComponentKey(ControllerKey
,
1666 PartialResourceList
,
1669 DPRINTM(DPRINT_HWDETECT
, "Created key: KeyboardPeripheral\\0\n");
1671 MmHeapFree(PartialResourceList
);
1677 DetectKeyboardController(PCONFIGURATION_COMPONENT_DATA BusKey
)
1679 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1680 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
1681 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1684 /* Set 'Configuration Data' value */
1685 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
1686 2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
1687 PartialResourceList
= MmHeapAlloc(Size
);
1688 if (PartialResourceList
== NULL
)
1690 DPRINTM(DPRINT_HWDETECT
,
1691 "Failed to allocate resource descriptor\n");
1695 /* Initialize resource descriptor */
1696 memset(PartialResourceList
, 0, Size
);
1697 PartialResourceList
->Version
= 1;
1698 PartialResourceList
->Revision
= 1;
1699 PartialResourceList
->Count
= 3;
1702 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
1703 PartialDescriptor
->Type
= CmResourceTypeInterrupt
;
1704 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
1705 PartialDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
1706 PartialDescriptor
->u
.Interrupt
.Level
= 1;
1707 PartialDescriptor
->u
.Interrupt
.Vector
= 0;
1708 PartialDescriptor
->u
.Interrupt
.Affinity
= 0xFFFFFFFF;
1710 /* Set IO Port 0x60 */
1711 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[1];
1712 PartialDescriptor
->Type
= CmResourceTypePort
;
1713 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
1714 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
1715 PartialDescriptor
->u
.Port
.Start
.LowPart
= 0x60;
1716 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
1717 PartialDescriptor
->u
.Port
.Length
= 1;
1719 /* Set IO Port 0x64 */
1720 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[2];
1721 PartialDescriptor
->Type
= CmResourceTypePort
;
1722 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
1723 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
1724 PartialDescriptor
->u
.Port
.Start
.LowPart
= 0x64;
1725 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
1726 PartialDescriptor
->u
.Port
.Length
= 1;
1728 /* Create controller key */
1729 FldrCreateComponentKey(BusKey
,
1736 PartialResourceList
,
1739 DPRINTM(DPRINT_HWDETECT
, "Created key: KeyboardController\\0\n");
1741 MmHeapFree(PartialResourceList
);
1743 DetectKeyboardPeripheral(ControllerKey
);
1748 PS2ControllerWait(VOID
)
1753 for (Timeout
= 0; Timeout
< CONTROLLER_TIMEOUT
; Timeout
++)
1755 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1756 if ((Status
& CONTROLLER_STATUS_INPUT_BUFFER_FULL
) == 0)
1759 /* Sleep for one millisecond */
1760 StallExecutionProcessor(1000);
1766 DetectPS2AuxPort(VOID
)
1769 /* Current detection is too unreliable. Just do as if
1770 * the PS/2 aux port is always present
1777 /* Put the value 0x5A in the output buffer using the
1778 * "WriteAuxiliary Device Output Buffer" command (0xD3).
1779 * Poll the Status Register for a while to see if the value really turns up
1780 * in the Data Register. If the KEYBOARD_STATUS_MOUSE_OBF bit is also set
1781 * to 1 in the Status Register, we assume this controller has an
1782 * Auxiliary Port (a.k.a. Mouse Port).
1784 PS2ControllerWait();
1785 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_CONTROL
,
1786 CONTROLLER_COMMAND_WRITE_MOUSE_OUTPUT_BUFFER
);
1787 PS2ControllerWait();
1789 /* 0x5A is a random dummy value */
1790 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
,
1793 for (Loops
= 0; Loops
< 10; Loops
++)
1795 StallExecutionProcessor(10000);
1796 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1797 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) != 0)
1801 READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1803 return (Status
& CONTROLLER_STATUS_MOUSE_OUTPUT_BUFFER_FULL
);
1809 DetectPS2AuxDevice(VOID
)
1814 BOOLEAN Result
= TRUE
;
1816 PS2ControllerWait();
1817 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_CONTROL
,
1818 CONTROLLER_COMMAND_WRITE_MOUSE
);
1819 PS2ControllerWait();
1821 /* Identify device */
1822 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
,
1825 /* Wait for reply */
1826 for (Loops
= 0; Loops
< 100; Loops
++)
1828 StallExecutionProcessor(10000);
1829 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1830 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) != 0)
1834 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1835 if ((Status
& CONTROLLER_STATUS_MOUSE_OUTPUT_BUFFER_FULL
) == 0)
1838 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1839 if (Scancode
!= 0xFA)
1842 StallExecutionProcessor(10000);
1844 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1845 if ((Status
& CONTROLLER_STATUS_MOUSE_OUTPUT_BUFFER_FULL
) == 0)
1848 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1849 if (Scancode
!= 0x00)
1857 DetectPS2Mouse(PCONFIGURATION_COMPONENT_DATA BusKey
)
1859 CM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1860 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1861 PCONFIGURATION_COMPONENT_DATA PeripheralKey
;
1863 if (DetectPS2AuxPort())
1865 DPRINTM(DPRINT_HWDETECT
, "Detected PS2 port\n");
1867 memset(&PartialResourceList
, 0, sizeof(CM_PARTIAL_RESOURCE_LIST
));
1869 /* Initialize resource descriptor */
1870 PartialResourceList
.Version
= 1;
1871 PartialResourceList
.Revision
= 1;
1872 PartialResourceList
.Count
= 1;
1875 PartialResourceList
.PartialDescriptors
[0].Type
= CmResourceTypeInterrupt
;
1876 PartialResourceList
.PartialDescriptors
[0].ShareDisposition
= CmResourceShareUndetermined
;
1877 PartialResourceList
.PartialDescriptors
[0].Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
1878 PartialResourceList
.PartialDescriptors
[0].u
.Interrupt
.Level
= 12;
1879 PartialResourceList
.PartialDescriptors
[0].u
.Interrupt
.Vector
= 0;
1880 PartialResourceList
.PartialDescriptors
[0].u
.Interrupt
.Affinity
= 0xFFFFFFFF;
1882 /* Create controller key */
1883 FldrCreateComponentKey(BusKey
,
1890 &PartialResourceList
,
1891 sizeof(CM_PARTIAL_RESOURCE_LIST
),
1893 DPRINTM(DPRINT_HWDETECT
, "Created key: PointerController\\0\n");
1895 if (DetectPS2AuxDevice())
1897 DPRINTM(DPRINT_HWDETECT
, "Detected PS2 mouse\n");
1899 /* Initialize resource descriptor */
1900 memset(&PartialResourceList
, 0, sizeof(CM_PARTIAL_RESOURCE_LIST
));
1901 PartialResourceList
.Version
= 1;
1902 PartialResourceList
.Revision
= 1;
1903 PartialResourceList
.Count
= 0;
1905 /* Create peripheral key */
1906 FldrCreateComponentKey(ControllerKey
,
1912 "MICROSOFT PS2 MOUSE",
1913 &PartialResourceList
,
1914 sizeof(CM_PARTIAL_RESOURCE_LIST
) -
1915 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
),
1917 DPRINTM(DPRINT_HWDETECT
, "Created key: PointerPeripheral\\0\n");
1924 DetectDisplayController(PCONFIGURATION_COMPONENT_DATA BusKey
)
1927 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1930 /* FIXME: Set 'ComponentInformation' value */
1932 VesaVersion
= BiosIsVesaSupported();
1933 if (VesaVersion
!= 0)
1935 DPRINTM(DPRINT_HWDETECT
,
1936 "VESA version %c.%c\n",
1937 (VesaVersion
>> 8) + '0',
1938 (VesaVersion
& 0xFF) + '0');
1942 DPRINTM(DPRINT_HWDETECT
,
1943 "VESA not supported\n");
1946 if (VesaVersion
>= 0x0200)
1957 FldrCreateComponentKey(BusKey
,
1967 DPRINTM(DPRINT_HWDETECT
, "Created key: DisplayController\\0\n");
1969 /* FIXME: Add display peripheral (monitor) data */
1974 DetectIsaBios(PCONFIGURATION_COMPONENT_DATA SystemKey
, ULONG
*BusNumber
)
1976 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1977 PCONFIGURATION_COMPONENT_DATA BusKey
;
1980 /* Increment bus number */
1983 /* Set 'Configuration Data' value */
1984 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) -
1985 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
1986 PartialResourceList
= MmHeapAlloc(Size
);
1987 if (PartialResourceList
== NULL
)
1989 DPRINTM(DPRINT_HWDETECT
,
1990 "Failed to allocate resource descriptor\n");
1994 /* Initialize resource descriptor */
1995 memset(PartialResourceList
, 0, Size
);
1996 PartialResourceList
->Version
= 1;
1997 PartialResourceList
->Revision
= 1;
1998 PartialResourceList
->Count
= 0;
2000 /* Create new bus key */
2001 FldrCreateComponentKey(SystemKey
,
2003 MultiFunctionAdapter
,
2008 PartialResourceList
,
2012 MmHeapFree(PartialResourceList
);
2014 /* Detect ISA/BIOS devices */
2015 DetectBiosDisks(BusKey
);
2017 DetectBiosFloppyController(BusKey
);
2019 DetectSerialPorts(BusKey
);
2021 DetectParallelPorts(BusKey
);
2023 DetectKeyboardController(BusKey
);
2025 DetectPS2Mouse(BusKey
);
2027 DetectDisplayController(BusKey
);
2029 /* FIXME: Detect more ISA devices */
2033 PCONFIGURATION_COMPONENT_DATA
2036 PCONFIGURATION_COMPONENT_DATA SystemKey
;
2037 ULONG BusNumber
= 0;
2039 DPRINTM(DPRINT_HWDETECT
, "DetectHardware()\n");
2041 /* Create the 'System' key */
2042 SystemKey
= DetectSystem();
2045 DetectPciBios(SystemKey
, &BusNumber
);
2046 DetectApmBios(SystemKey
, &BusNumber
);
2047 DetectPnpBios(SystemKey
, &BusNumber
);
2048 DetectIsaBios(SystemKey
, &BusNumber
);
2049 DetectAcpiBios(SystemKey
, &BusNumber
);
2051 DPRINTM(DPRINT_HWDETECT
, "DetectHardware() Done\n");