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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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
))
441 if (DrivePartition
== 0xff)
443 /* This is a CD-ROM device */
448 /* This is either a floppy disk device (DrivePartition == 0) or
449 * a hard disk device (DrivePartition != 0 && DrivePartition != 0xFF) but
450 * it doesn't matter which one because they both have 512 bytes per sector */
454 if (DrivePartition
!= 0xff && DrivePartition
!= 0)
456 if (!DiskGetPartitionEntry(DriveNumber
, DrivePartition
, &PartitionTableEntry
))
458 SectorOffset
= PartitionTableEntry
.SectorCountBeforePartition
;
459 SectorCount
= PartitionTableEntry
.PartitionSectorCount
;
462 Context
= MmHeapAlloc(sizeof(DISKCONTEXT
));
465 Context
->DriveNumber
= DriveNumber
;
466 Context
->SectorSize
= SectorSize
;
467 Context
->SectorOffset
= SectorOffset
;
468 Context
->SectorCount
= SectorCount
;
469 Context
->SectorNumber
= 0;
470 FsSetDeviceSpecific(*FileId
, Context
);
475 static LONG
DiskRead(ULONG FileId
, VOID
* Buffer
, ULONG N
, ULONG
* Count
)
477 DISKCONTEXT
* Context
= FsGetDeviceSpecific(FileId
);
478 UCHAR
* Ptr
= (UCHAR
*)Buffer
;
487 if (Length
> Context
->SectorSize
)
488 Length
= Context
->SectorSize
;
489 ret
= MachDiskReadLogicalSectors(
490 Context
->DriveNumber
,
491 Context
->SectorNumber
+ Context
->SectorOffset
+ i
,
493 (PVOID
)DISKREADBUFFER
);
496 RtlCopyMemory(Ptr
, (PVOID
)DISKREADBUFFER
, Length
);
506 static LONG
DiskSeek(ULONG FileId
, LARGE_INTEGER
* Position
, SEEKMODE SeekMode
)
508 DISKCONTEXT
* Context
= FsGetDeviceSpecific(FileId
);
510 if (SeekMode
!= SeekAbsolute
)
512 if (Position
->LowPart
& (Context
->SectorSize
- 1))
515 /* FIXME: take HighPart into account */
516 Context
->SectorNumber
= Position
->LowPart
/ Context
->SectorSize
;
520 static const DEVVTBL DiskVtbl
= {
522 DiskGetFileInformation
,
529 GetHarddiskIdentifier(PCHAR Identifier
,
532 PMASTER_BOOT_RECORD Mbr
;
538 PARTITION_TABLE_ENTRY PartitionTableEntry
;
541 if (!MachDiskReadLogicalSectors(DriveNumber
, 0ULL, 1, (PVOID
)DISKREADBUFFER
))
543 DPRINTM(DPRINT_HWDETECT
, "Reading MBR failed\n");
547 Buffer
= (ULONG
*)DISKREADBUFFER
;
548 Mbr
= (PMASTER_BOOT_RECORD
)DISKREADBUFFER
;
550 Signature
= Mbr
->Signature
;
551 DPRINTM(DPRINT_HWDETECT
, "Signature: %x\n", Signature
);
553 /* Calculate the MBR checksum */
555 for (i
= 0; i
< 128; i
++)
557 Checksum
+= Buffer
[i
];
559 Checksum
= ~Checksum
+ 1;
560 DPRINTM(DPRINT_HWDETECT
, "Checksum: %x\n", Checksum
);
562 /* Fill out the ARC disk block */
563 reactos_arc_disk_info
[reactos_disk_count
].Signature
= Signature
;
564 reactos_arc_disk_info
[reactos_disk_count
].CheckSum
= Checksum
;
565 sprintf(ArcName
, "multi(0)disk(0)rdisk(%lu)", reactos_disk_count
);
566 strcpy(reactos_arc_strings
[reactos_disk_count
], ArcName
);
567 reactos_arc_disk_info
[reactos_disk_count
].ArcName
=
568 reactos_arc_strings
[reactos_disk_count
];
569 reactos_disk_count
++;
571 sprintf(ArcName
, "multi(0)disk(0)rdisk(%lu)partition(0)", DriveNumber
- 0x80);
572 FsRegisterDevice(ArcName
, &DiskVtbl
);
576 DiskReportError(FALSE
);
577 while (DiskGetPartitionEntry(DriveNumber
, i
, &PartitionTableEntry
))
579 if (PartitionTableEntry
.SystemIndicator
!= PARTITION_ENTRY_UNUSED
)
581 sprintf(ArcName
, "multi(0)disk(0)rdisk(%lu)partition(%lu)", DriveNumber
- 0x80, i
);
582 FsRegisterDevice(ArcName
, &DiskVtbl
);
586 DiskReportError(TRUE
);
588 /* Convert checksum and signature to identifier string */
589 Identifier
[0] = Hex
[(Checksum
>> 28) & 0x0F];
590 Identifier
[1] = Hex
[(Checksum
>> 24) & 0x0F];
591 Identifier
[2] = Hex
[(Checksum
>> 20) & 0x0F];
592 Identifier
[3] = Hex
[(Checksum
>> 16) & 0x0F];
593 Identifier
[4] = Hex
[(Checksum
>> 12) & 0x0F];
594 Identifier
[5] = Hex
[(Checksum
>> 8) & 0x0F];
595 Identifier
[6] = Hex
[(Checksum
>> 4) & 0x0F];
596 Identifier
[7] = Hex
[Checksum
& 0x0F];
598 Identifier
[9] = Hex
[(Signature
>> 28) & 0x0F];
599 Identifier
[10] = Hex
[(Signature
>> 24) & 0x0F];
600 Identifier
[11] = Hex
[(Signature
>> 20) & 0x0F];
601 Identifier
[12] = Hex
[(Signature
>> 16) & 0x0F];
602 Identifier
[13] = Hex
[(Signature
>> 12) & 0x0F];
603 Identifier
[14] = Hex
[(Signature
>> 8) & 0x0F];
604 Identifier
[15] = Hex
[(Signature
>> 4) & 0x0F];
605 Identifier
[16] = Hex
[Signature
& 0x0F];
606 Identifier
[17] = '-';
607 Identifier
[18] = 'A';
609 DPRINTM(DPRINT_HWDETECT
, "Identifier: %s\n", Identifier
);
617 WRITE_PORT_UCHAR((PUCHAR
)0x70, 0x10);
618 Data
= READ_PORT_UCHAR((PUCHAR
)0x71);
620 return ((Data
& 0xF0) ? 1 : 0) + ((Data
& 0x0F) ? 1 : 0);
625 GetFloppyType(UCHAR DriveNumber
)
629 WRITE_PORT_UCHAR((PUCHAR
)0x70, 0x10);
630 Data
= READ_PORT_UCHAR((PUCHAR
)0x71);
632 if (DriveNumber
== 0)
634 else if (DriveNumber
== 1)
644 PUSHORT SegPtr
= (PUSHORT
)0x7A;
645 PUSHORT OfsPtr
= (PUSHORT
)0x78;
647 return (PVOID
)((ULONG_PTR
)(((ULONG
)(*SegPtr
)) << 4) + (ULONG
)(*OfsPtr
));
652 DetectBiosFloppyPeripheral(PCONFIGURATION_COMPONENT_DATA ControllerKey
)
654 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
655 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
656 PCM_FLOPPY_DEVICE_DATA FloppyData
;
658 PCONFIGURATION_COMPONENT_DATA PeripheralKey
;
662 ULONG MaxDensity
[6] = {0, 360, 1200, 720, 1440, 2880};
665 for (FloppyNumber
= 0; FloppyNumber
< 2; FloppyNumber
++)
667 FloppyType
= GetFloppyType(FloppyNumber
);
669 if ((FloppyType
> 5) || (FloppyType
== 0))
672 DiskResetController(FloppyNumber
);
674 Ptr
= GetInt1eTable();
676 /* Set 'Identifier' value */
677 sprintf(Identifier
, "FLOPPY%ld", FloppyNumber
+ 1);
679 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
680 sizeof(CM_FLOPPY_DEVICE_DATA
);
681 PartialResourceList
= MmHeapAlloc(Size
);
682 if (PartialResourceList
== NULL
)
684 DPRINTM(DPRINT_HWDETECT
,
685 "Failed to allocate resource descriptor\n");
689 memset(PartialResourceList
, 0, Size
);
690 PartialResourceList
->Version
= 1;
691 PartialResourceList
->Revision
= 1;
692 PartialResourceList
->Count
= 1;
694 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
695 PartialDescriptor
->Type
= CmResourceTypeDeviceSpecific
;
696 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
697 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
= sizeof(CM_FLOPPY_DEVICE_DATA
);
699 FloppyData
= (PVOID
)(((ULONG_PTR
)PartialResourceList
) + sizeof(CM_PARTIAL_RESOURCE_LIST
));
700 FloppyData
->Version
= 2;
701 FloppyData
->Revision
= 0;
702 FloppyData
->MaxDensity
= MaxDensity
[FloppyType
];
703 FloppyData
->MountDensity
= 0;
704 RtlCopyMemory(&FloppyData
->StepRateHeadUnloadTime
,
707 FloppyData
->MaximumTrackValue
= (FloppyType
== 1) ? 39 : 79;
708 FloppyData
->DataTransferRate
= 0;
710 FldrCreateComponentKey(ControllerKey
,
712 FloppyDiskPeripheral
,
721 MmHeapFree(PartialResourceList
);
727 DetectBiosFloppyController(PCONFIGURATION_COMPONENT_DATA BusKey
)
729 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
730 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
731 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
735 FloppyCount
= GetFloppyCount();
736 DPRINTM(DPRINT_HWDETECT
,
737 "Floppy count: %u\n",
740 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
741 2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
742 PartialResourceList
= MmHeapAlloc(Size
);
743 if (PartialResourceList
== NULL
)
745 DPRINTM(DPRINT_HWDETECT
,
746 "Failed to allocate resource descriptor\n");
749 memset(PartialResourceList
, 0, Size
);
751 /* Initialize resource descriptor */
752 PartialResourceList
->Version
= 1;
753 PartialResourceList
->Revision
= 1;
754 PartialResourceList
->Count
= 3;
757 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
758 PartialDescriptor
->Type
= CmResourceTypePort
;
759 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
760 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
761 PartialDescriptor
->u
.Port
.Start
.LowPart
= 0x03F0;
762 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
763 PartialDescriptor
->u
.Port
.Length
= 8;
766 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[1];
767 PartialDescriptor
->Type
= CmResourceTypeInterrupt
;
768 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
769 PartialDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
770 PartialDescriptor
->u
.Interrupt
.Level
= 6;
771 PartialDescriptor
->u
.Interrupt
.Vector
= 6;
772 PartialDescriptor
->u
.Interrupt
.Affinity
= 0xFFFFFFFF;
774 /* Set DMA channel */
775 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[2];
776 PartialDescriptor
->Type
= CmResourceTypeDma
;
777 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
778 PartialDescriptor
->Flags
= 0;
779 PartialDescriptor
->u
.Dma
.Channel
= 2;
780 PartialDescriptor
->u
.Dma
.Port
= 0;
782 /* Create floppy disk controller */
783 FldrCreateComponentKey(BusKey
,
793 DPRINTM(DPRINT_HWDETECT
, "Created key: DiskController\\0\n");
795 MmHeapFree(PartialResourceList
);
797 if (FloppyCount
) DetectBiosFloppyPeripheral(ControllerKey
);
800 static PCONFIGURATION_COMPONENT_DATA
803 PCONFIGURATION_COMPONENT_DATA SystemKey
;
804 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
805 PCM_INT13_DRIVE_PARAMETER Int13Drives
;
812 /* Count the number of visible drives */
813 DiskReportError(FALSE
);
816 /* There are some really broken BIOSes out there. There are even BIOSes
817 * that happily report success when you ask them to read from non-existent
818 * harddisks. So, we set the buffer to known contents first, then try to
819 * read. If the BIOS reports success but the buffer contents haven't
820 * changed then we fail anyway */
821 memset((PVOID
) DISKREADBUFFER
, 0xcd, 512);
822 while (MachDiskReadLogicalSectors(0x80 + DiskCount
, 0ULL, 1, (PVOID
)DISKREADBUFFER
))
825 for (i
= 0; ! Changed
&& i
< 512; i
++)
827 Changed
= ((PUCHAR
)DISKREADBUFFER
)[i
] != 0xcd;
831 DPRINTM(DPRINT_HWDETECT
, "BIOS reports success for disk %d but data didn't change\n",
836 memset((PVOID
) DISKREADBUFFER
, 0xcd, 512);
838 DiskReportError(TRUE
);
839 DPRINTM(DPRINT_HWDETECT
, "BIOS reports %d harddisk%s\n",
840 (int)DiskCount
, (DiskCount
== 1) ? "": "s");
842 /* Allocate resource descriptor */
843 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
844 sizeof(CM_INT13_DRIVE_PARAMETER
) * DiskCount
;
845 PartialResourceList
= MmHeapAlloc(Size
);
846 if (PartialResourceList
== NULL
)
848 DPRINTM(DPRINT_HWDETECT
,
849 "Failed to allocate resource descriptor\n");
853 /* Initialize resource descriptor */
854 memset(PartialResourceList
, 0, Size
);
855 PartialResourceList
->Version
= 1;
856 PartialResourceList
->Revision
= 1;
857 PartialResourceList
->Count
= 1;
858 PartialResourceList
->PartialDescriptors
[0].Type
= CmResourceTypeDeviceSpecific
;
859 PartialResourceList
->PartialDescriptors
[0].ShareDisposition
= 0;
860 PartialResourceList
->PartialDescriptors
[0].Flags
= 0;
861 PartialResourceList
->PartialDescriptors
[0].u
.DeviceSpecificData
.DataSize
=
862 sizeof(CM_INT13_DRIVE_PARAMETER
) * DiskCount
;
864 /* Get harddisk Int13 geometry data */
865 Int13Drives
= (PVOID
)(((ULONG_PTR
)PartialResourceList
) + sizeof(CM_PARTIAL_RESOURCE_LIST
));
866 for (i
= 0; i
< DiskCount
; i
++)
868 if (MachDiskGetDriveGeometry(0x80 + i
, &Geometry
))
870 Int13Drives
[i
].DriveSelect
= 0x80 + i
;
871 Int13Drives
[i
].MaxCylinders
= Geometry
.Cylinders
- 1;
872 Int13Drives
[i
].SectorsPerTrack
= Geometry
.Sectors
;
873 Int13Drives
[i
].MaxHeads
= Geometry
.Heads
- 1;
874 Int13Drives
[i
].NumberDrives
= DiskCount
;
876 DPRINTM(DPRINT_HWDETECT
,
877 "Disk %x: %u Cylinders %u Heads %u Sectors %u Bytes\n",
879 Geometry
.Cylinders
- 1,
882 Geometry
.BytesPerSector
);
886 FldrCreateComponentKey(NULL
,
897 MmHeapFree(PartialResourceList
);
903 GetDiskCount(PCONFIGURATION_COMPONENT_DATA BusKey
)
905 PCONFIGURATION_COMPONENT_DATA System
;
906 ULONG ConfigurationDataLength
;
910 // Get root component
913 while (System
->Parent
)
914 System
= System
->Parent
;
917 // Get root configuration data length
919 ConfigurationDataLength
= System
->ComponentEntry
.ConfigurationDataLength
;
922 // We assume that nothing wrong happened, and that configuration
923 // only consists of one CM_PARTIAL_RESOURCE_LIST entry, followed
924 // by n entries of CM_INT13_DRIVE_PARAMETER
926 if (ConfigurationDataLength
> 0)
927 DiskCount
= (ConfigurationDataLength
- sizeof(CM_PARTIAL_RESOURCE_LIST
))
928 / sizeof(CM_INT13_DRIVE_PARAMETER
);
931 // Return number of disks
933 DPRINTM(DPRINT_HWDETECT
, "Retrieving %lu INT13 disks\\0\n", DiskCount
);
938 DetectBiosDisks(PCONFIGURATION_COMPONENT_DATA BusKey
)
940 PCONFIGURATION_COMPONENT_DATA DiskKey
, ControllerKey
;
941 BOOLEAN BootDriveReported
= FALSE
;
943 ULONG DiskCount
= GetDiskCount(BusKey
);
946 FldrCreateComponentKey(BusKey
,
956 DPRINTM(DPRINT_HWDETECT
, "Created key: DiskController\\0\n");
958 /* Create and fill subkey for each harddisk */
959 for (i
= 0; i
< DiskCount
; i
++)
961 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
965 if (BootDrive
== 0x80 + i
)
966 BootDriveReported
= TRUE
;
968 /* Get disk values */
969 PartialResourceList
= GetHarddiskConfigurationData(0x80 + i
, &Size
);
970 GetHarddiskIdentifier(Identifier
, 0x80 + i
);
972 /* Create disk key */
973 FldrCreateComponentKey(ControllerKey
,
985 /* Get the drive we're booting from */
986 MachDiskGetBootPath(BootPath
, sizeof(BootPath
));
988 /* Add it, if it's a floppy or cdrom */
989 if ((BootDrive
>= 0x80 && !BootDriveReported
) ||
990 DiskIsDriveRemovable(BootDrive
))
992 /* TODO: Check if it's really a cdrom drive */
997 if (!MachDiskReadLogicalSectors(BootDrive
, 16ULL, 1, (PVOID
)DISKREADBUFFER
))
999 DPRINTM(DPRINT_HWDETECT
, "Reading MBR failed\n");
1003 Buffer
= (ULONG
*)DISKREADBUFFER
;
1005 /* Calculate the MBR checksum */
1006 for (i
= 0; i
< 2048 / sizeof(ULONG
); i
++) Checksum
+= Buffer
[i
];
1007 Checksum
= ~Checksum
+ 1;
1008 DPRINTM(DPRINT_HWDETECT
, "Checksum: %x\n", Checksum
);
1010 /* Fill out the ARC disk block */
1011 reactos_arc_disk_info
[reactos_disk_count
].CheckSum
= Checksum
;
1012 strcpy(reactos_arc_strings
[reactos_disk_count
], BootPath
);
1013 reactos_arc_disk_info
[reactos_disk_count
].ArcName
=
1014 reactos_arc_strings
[reactos_disk_count
];
1015 reactos_disk_count
++;
1017 FsRegisterDevice(BootPath
, &DiskVtbl
);
1022 InitializeSerialPort(PUCHAR Port
,
1025 WRITE_PORT_UCHAR(Port
+ 3, 0x80); /* set DLAB on */
1026 WRITE_PORT_UCHAR(Port
, 0x60); /* speed LO byte */
1027 WRITE_PORT_UCHAR(Port
+ 1, 0); /* speed HI byte */
1028 WRITE_PORT_UCHAR(Port
+ 3, LineControl
);
1029 WRITE_PORT_UCHAR(Port
+ 1, 0); /* set comm and DLAB to 0 */
1030 WRITE_PORT_UCHAR(Port
+ 4, 0x09); /* DR int enable */
1031 READ_PORT_UCHAR(Port
+ 5); /* clear error bits */
1036 DetectSerialMouse(PUCHAR Port
)
1043 /* Shutdown mouse or something like that */
1044 LineControl
= READ_PORT_UCHAR(Port
+ 4);
1045 WRITE_PORT_UCHAR(Port
+ 4, (LineControl
& ~0x02) | 0x01);
1046 StallExecutionProcessor(100000);
1050 * Maybe there is no serial port although BIOS reported one (this
1051 * is the case on Apple hardware), or the serial port is misbehaving,
1052 * therefore we must give up after some time.
1055 while (READ_PORT_UCHAR(Port
+ 5) & 0x01)
1058 return MOUSE_TYPE_NONE
;
1059 READ_PORT_UCHAR(Port
);
1063 * Send modem control with 'Data Terminal Ready', 'Request To Send' and
1064 * 'Output Line 2' message. This enables mouse to identify.
1066 WRITE_PORT_UCHAR(Port
+ 4, 0x0b);
1068 /* Wait 10 milliseconds for the mouse getting ready */
1069 StallExecutionProcessor(10000);
1071 /* Read first four bytes, which contains Microsoft Mouse signs */
1073 for (i
= 0; i
< 4; i
++)
1075 while (((READ_PORT_UCHAR(Port
+ 5) & 1) == 0) && (TimeOut
> 0))
1077 StallExecutionProcessor(1000);
1080 return MOUSE_TYPE_NONE
;
1082 Buffer
[i
] = READ_PORT_UCHAR(Port
);
1085 DPRINTM(DPRINT_HWDETECT
,
1086 "Mouse data: %x %x %x %x\n",
1087 Buffer
[0],Buffer
[1],Buffer
[2],Buffer
[3]);
1089 /* Check that four bytes for signs */
1090 for (i
= 0; i
< 4; ++i
)
1092 if (Buffer
[i
] == 'B')
1094 /* Sign for Microsoft Ballpoint */
1095 // DbgPrint("Microsoft Ballpoint device detected\n");
1096 // DbgPrint("THIS DEVICE IS NOT SUPPORTED, YET\n");
1097 return MOUSE_TYPE_NONE
;
1099 else if (Buffer
[i
] == 'M')
1101 /* Sign for Microsoft Mouse protocol followed by button specifier */
1104 /* Overflow Error */
1105 return MOUSE_TYPE_NONE
;
1108 switch (Buffer
[i
+ 1])
1111 DPRINTM(DPRINT_HWDETECT
,
1112 "Microsoft Mouse with 3-buttons detected\n");
1113 return MOUSE_TYPE_LOGITECH
;
1116 DPRINTM(DPRINT_HWDETECT
,
1117 "Microsoft Wheel Mouse detected\n");
1118 return MOUSE_TYPE_WHEELZ
;
1122 DPRINTM(DPRINT_HWDETECT
,
1123 "Microsoft Mouse with 2-buttons detected\n");
1124 return MOUSE_TYPE_MICROSOFT
;
1129 return MOUSE_TYPE_NONE
;
1134 GetSerialMousePnpId(PUCHAR Port
, char *Buffer
)
1141 WRITE_PORT_UCHAR(Port
+ 4, 0x09);
1143 /* Wait 10 milliseconds for the mouse getting ready */
1144 StallExecutionProcessor(10000);
1146 WRITE_PORT_UCHAR(Port
+ 4, 0x0b);
1148 StallExecutionProcessor(10000);
1153 while (((READ_PORT_UCHAR(Port
+ 5) & 1) == 0) && (TimeOut
> 0))
1155 StallExecutionProcessor(1000);
1163 c
= READ_PORT_UCHAR(Port
);
1164 if (c
== 0x08 || c
== 0x28)
1174 while (((READ_PORT_UCHAR(Port
+ 5) & 1) == 0) && (TimeOut
> 0))
1176 StallExecutionProcessor(1000);
1181 c
= READ_PORT_UCHAR(Port
);
1194 DetectSerialPointerPeripheral(PCONFIGURATION_COMPONENT_DATA ControllerKey
,
1197 CM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1199 CHAR Identifier
[256];
1200 PCONFIGURATION_COMPONENT_DATA PeripheralKey
;
1207 DPRINTM(DPRINT_HWDETECT
,
1208 "DetectSerialPointerPeripheral()\n");
1212 InitializeSerialPort(Base
, 2);
1213 MouseType
= DetectSerialMouse(Base
);
1215 if (MouseType
!= MOUSE_TYPE_NONE
)
1217 Length
= GetSerialMousePnpId(Base
, Buffer
);
1218 DPRINTM(DPRINT_HWDETECT
,
1219 "PnP ID length: %u\n",
1224 /* Convert PnP sting to ASCII */
1225 if (Buffer
[0] == 0x08)
1227 for (i
= 0; i
< Length
; i
++)
1232 DPRINTM(DPRINT_HWDETECT
,
1233 "PnP ID string: %s\n",
1236 /* Copy PnpId string */
1237 for (i
= 0; i
< 7; i
++)
1239 Identifier
[i
] = Buffer
[3+i
];
1241 memcpy(&Identifier
[7],
1245 /* Skip device serial number */
1247 if (Buffer
[i
] == '\\')
1249 for (j
= ++i
; i
< Length
; ++i
)
1251 if (Buffer
[i
] == '\\')
1258 /* Skip PnP class */
1259 if (Buffer
[i
] == '\\')
1261 for (j
= ++i
; i
< Length
; ++i
)
1263 if (Buffer
[i
] == '\\')
1271 /* Skip compatible PnP Id */
1272 if (Buffer
[i
] == '\\')
1274 for (j
= ++i
; i
< Length
; ++i
)
1276 if (Buffer
[i
] == '\\')
1279 if (Buffer
[j
] == '*')
1285 /* Get product description */
1286 if (Buffer
[i
] == '\\')
1288 for (j
= ++i
; i
< Length
; ++i
)
1290 if (Buffer
[i
] == ';')
1297 for (k
= 0; k
< i
- j
; k
++)
1299 Identifier
[k
+ 10] = Buffer
[k
+ j
];
1301 Identifier
[10 + (i
-j
)] = 0;
1305 DPRINTM(DPRINT_HWDETECT
,
1306 "Identifier string: %s\n",
1310 if (Length
== 0 || strlen(Identifier
) < 11)
1314 case MOUSE_TYPE_LOGITECH
:
1316 "LOGITECH SERIAL MOUSE");
1319 case MOUSE_TYPE_WHEELZ
:
1321 "MICROSOFT SERIAL MOUSE WITH WHEEL");
1324 case MOUSE_TYPE_MICROSOFT
:
1327 "MICROSOFT SERIAL MOUSE");
1332 /* Set 'Configuration Data' value */
1333 memset(&PartialResourceList
, 0, sizeof(CM_PARTIAL_RESOURCE_LIST
));
1334 PartialResourceList
.Version
= 1;
1335 PartialResourceList
.Revision
= 1;
1336 PartialResourceList
.Count
= 0;
1338 /* Create 'PointerPeripheral' key */
1339 FldrCreateComponentKey(ControllerKey
,
1346 &PartialResourceList
,
1347 sizeof(CM_PARTIAL_RESOURCE_LIST
) -
1348 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
),
1351 DPRINTM(DPRINT_HWDETECT
,
1352 "Created key: PointerPeripheral\\0\n");
1358 DetectSerialPorts(PCONFIGURATION_COMPONENT_DATA BusKey
)
1360 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1361 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
1362 PCM_SERIAL_DEVICE_DATA SerialDeviceData
;
1363 ULONG Irq
[4] = {4, 3, 4, 3};
1367 ULONG ControllerNumber
= 0;
1368 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1372 DPRINTM(DPRINT_HWDETECT
, "DetectSerialPorts()\n");
1374 ControllerNumber
= 0;
1375 BasePtr
= (PUSHORT
)0x400;
1376 for (i
= 0; i
< 4; i
++, BasePtr
++)
1378 Base
= (ULONG
)*BasePtr
;
1382 DPRINTM(DPRINT_HWDETECT
,
1383 "Found COM%u port at 0x%x\n",
1387 /* Set 'Identifier' value */
1388 sprintf(Buffer
, "COM%ld", i
+ 1);
1390 /* Build full device descriptor */
1391 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
1392 2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
) +
1393 sizeof(CM_SERIAL_DEVICE_DATA
);
1394 PartialResourceList
= MmHeapAlloc(Size
);
1395 if (PartialResourceList
== NULL
)
1397 DPRINTM(DPRINT_HWDETECT
,
1398 "Failed to allocate resource descriptor\n");
1401 memset(PartialResourceList
, 0, Size
);
1403 /* Initialize resource descriptor */
1404 PartialResourceList
->Version
= 1;
1405 PartialResourceList
->Revision
= 1;
1406 PartialResourceList
->Count
= 3;
1409 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
1410 PartialDescriptor
->Type
= CmResourceTypePort
;
1411 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
1412 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
1413 PartialDescriptor
->u
.Port
.Start
.LowPart
= Base
;
1414 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
1415 PartialDescriptor
->u
.Port
.Length
= 7;
1418 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[1];
1419 PartialDescriptor
->Type
= CmResourceTypeInterrupt
;
1420 PartialDescriptor
->ShareDisposition
= CmResourceShareShared
;
1421 PartialDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
1422 PartialDescriptor
->u
.Interrupt
.Level
= Irq
[i
];
1423 PartialDescriptor
->u
.Interrupt
.Vector
= Irq
[i
];
1424 PartialDescriptor
->u
.Interrupt
.Affinity
= 0xFFFFFFFF;
1426 /* Set serial data (device specific) */
1427 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[2];
1428 PartialDescriptor
->Type
= CmResourceTypeDeviceSpecific
;
1429 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
1430 PartialDescriptor
->Flags
= 0;
1431 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
= sizeof(CM_SERIAL_DEVICE_DATA
);
1434 (PCM_SERIAL_DEVICE_DATA
)&PartialResourceList
->PartialDescriptors
[3];
1435 SerialDeviceData
->BaudClock
= 1843200; /* UART Clock frequency (Hertz) */
1437 /* Create controller key */
1438 FldrCreateComponentKey(BusKey
,
1441 Output
| Input
| ConsoleIn
| ConsoleOut
,
1445 PartialResourceList
,
1449 MmHeapFree(PartialResourceList
);
1451 if (!Rs232PortInUse(Base
))
1453 /* Detect serial mouse */
1454 DetectSerialPointerPeripheral(ControllerKey
, UlongToPtr(Base
));
1463 DetectParallelPorts(PCONFIGURATION_COMPONENT_DATA BusKey
)
1465 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1466 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
1467 ULONG Irq
[3] = {7, 5, (ULONG
)-1};
1469 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1472 ULONG ControllerNumber
;
1476 DPRINTM(DPRINT_HWDETECT
, "DetectParallelPorts() called\n");
1478 ControllerNumber
= 0;
1479 BasePtr
= (PUSHORT
)0x408;
1480 for (i
= 0; i
< 3; i
++, BasePtr
++)
1482 Base
= (ULONG
)*BasePtr
;
1486 DPRINTM(DPRINT_HWDETECT
,
1487 "Parallel port %u: %x\n",
1491 /* Set 'Identifier' value */
1492 sprintf(Buffer
, "PARALLEL%ld", i
+ 1);
1494 /* Build full device descriptor */
1495 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
);
1496 if (Irq
[i
] != (ULONG
)-1)
1497 Size
+= sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
1499 PartialResourceList
= MmHeapAlloc(Size
);
1500 if (PartialResourceList
== NULL
)
1502 DPRINTM(DPRINT_HWDETECT
,
1503 "Failed to allocate resource descriptor\n");
1506 memset(PartialResourceList
, 0, Size
);
1508 /* Initialize resource descriptor */
1509 PartialResourceList
->Version
= 1;
1510 PartialResourceList
->Revision
= 1;
1511 PartialResourceList
->Count
= (Irq
[i
] != (ULONG
)-1) ? 2 : 1;
1514 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
1515 PartialDescriptor
->Type
= CmResourceTypePort
;
1516 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
1517 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
1518 PartialDescriptor
->u
.Port
.Start
.LowPart
= Base
;
1519 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
1520 PartialDescriptor
->u
.Port
.Length
= 3;
1523 if (Irq
[i
] != (ULONG
)-1)
1525 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[1];
1526 PartialDescriptor
->Type
= CmResourceTypeInterrupt
;
1527 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
1528 PartialDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
1529 PartialDescriptor
->u
.Interrupt
.Level
= Irq
[i
];
1530 PartialDescriptor
->u
.Interrupt
.Vector
= Irq
[i
];
1531 PartialDescriptor
->u
.Interrupt
.Affinity
= 0xFFFFFFFF;
1534 /* Create controller key */
1535 FldrCreateComponentKey(BusKey
,
1542 PartialResourceList
,
1546 MmHeapFree(PartialResourceList
);
1551 DPRINTM(DPRINT_HWDETECT
, "DetectParallelPorts() done\n");
1556 DetectKeyboardDevice(VOID
)
1561 BOOLEAN Result
= TRUE
;
1563 /* Identify device */
1564 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
,
1567 /* Wait for reply */
1568 for (Loops
= 0; Loops
< 100; Loops
++)
1570 StallExecutionProcessor(10000);
1571 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1572 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) != 0)
1576 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) == 0)
1578 /* PC/XT keyboard or no keyboard */
1582 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1583 if (Scancode
!= 0xFA)
1585 /* No ACK received */
1589 StallExecutionProcessor(10000);
1591 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1592 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) == 0)
1594 /* Found AT keyboard */
1598 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1599 if (Scancode
!= 0xAB)
1601 /* No 0xAB received */
1605 StallExecutionProcessor(10000);
1607 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1608 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) == 0)
1610 /* No byte in buffer */
1614 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1615 if (Scancode
!= 0x41)
1617 /* No 0x41 received */
1621 /* Found MF-II keyboard */
1627 DetectKeyboardPeripheral(PCONFIGURATION_COMPONENT_DATA ControllerKey
)
1629 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1630 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
1631 PCM_KEYBOARD_DEVICE_DATA KeyboardData
;
1632 PCONFIGURATION_COMPONENT_DATA PeripheralKey
;
1635 /* HACK: don't call DetectKeyboardDevice() as it fails in Qemu 0.8.2 */
1636 if (TRUE
|| DetectKeyboardDevice())
1638 /* Set 'Configuration Data' value */
1639 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
1640 sizeof(CM_KEYBOARD_DEVICE_DATA
);
1641 PartialResourceList
= MmHeapAlloc(Size
);
1642 if (PartialResourceList
== NULL
)
1644 DPRINTM(DPRINT_HWDETECT
,
1645 "Failed to allocate resource descriptor\n");
1649 /* Initialize resource descriptor */
1650 memset(PartialResourceList
, 0, Size
);
1651 PartialResourceList
->Version
= 1;
1652 PartialResourceList
->Revision
= 1;
1653 PartialResourceList
->Count
= 1;
1655 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
1656 PartialDescriptor
->Type
= CmResourceTypeDeviceSpecific
;
1657 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
1658 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
= sizeof(CM_KEYBOARD_DEVICE_DATA
);
1660 KeyboardData
= (PCM_KEYBOARD_DEVICE_DATA
)(PartialDescriptor
+ 1);
1661 KeyboardData
->Version
= 1;
1662 KeyboardData
->Revision
= 1;
1663 KeyboardData
->Type
= 4;
1664 KeyboardData
->Subtype
= 0;
1665 KeyboardData
->KeyboardFlags
= 0x20;
1667 /* Create controller key */
1668 FldrCreateComponentKey(ControllerKey
,
1675 PartialResourceList
,
1678 DPRINTM(DPRINT_HWDETECT
, "Created key: KeyboardPeripheral\\0\n");
1680 MmHeapFree(PartialResourceList
);
1686 DetectKeyboardController(PCONFIGURATION_COMPONENT_DATA BusKey
)
1688 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1689 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
1690 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1693 /* Set 'Configuration Data' value */
1694 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
1695 2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
1696 PartialResourceList
= MmHeapAlloc(Size
);
1697 if (PartialResourceList
== NULL
)
1699 DPRINTM(DPRINT_HWDETECT
,
1700 "Failed to allocate resource descriptor\n");
1704 /* Initialize resource descriptor */
1705 memset(PartialResourceList
, 0, Size
);
1706 PartialResourceList
->Version
= 1;
1707 PartialResourceList
->Revision
= 1;
1708 PartialResourceList
->Count
= 3;
1711 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
1712 PartialDescriptor
->Type
= CmResourceTypeInterrupt
;
1713 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
1714 PartialDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
1715 PartialDescriptor
->u
.Interrupt
.Level
= 1;
1716 PartialDescriptor
->u
.Interrupt
.Vector
= 1;
1717 PartialDescriptor
->u
.Interrupt
.Affinity
= 0xFFFFFFFF;
1719 /* Set IO Port 0x60 */
1720 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[1];
1721 PartialDescriptor
->Type
= CmResourceTypePort
;
1722 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
1723 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
1724 PartialDescriptor
->u
.Port
.Start
.LowPart
= 0x60;
1725 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
1726 PartialDescriptor
->u
.Port
.Length
= 1;
1728 /* Set IO Port 0x64 */
1729 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[2];
1730 PartialDescriptor
->Type
= CmResourceTypePort
;
1731 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
1732 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
1733 PartialDescriptor
->u
.Port
.Start
.LowPart
= 0x64;
1734 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
1735 PartialDescriptor
->u
.Port
.Length
= 1;
1737 /* Create controller key */
1738 FldrCreateComponentKey(BusKey
,
1745 PartialResourceList
,
1748 DPRINTM(DPRINT_HWDETECT
, "Created key: KeyboardController\\0\n");
1750 MmHeapFree(PartialResourceList
);
1752 DetectKeyboardPeripheral(ControllerKey
);
1757 PS2ControllerWait(VOID
)
1762 for (Timeout
= 0; Timeout
< CONTROLLER_TIMEOUT
; Timeout
++)
1764 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1765 if ((Status
& CONTROLLER_STATUS_INPUT_BUFFER_FULL
) == 0)
1768 /* Sleep for one millisecond */
1769 StallExecutionProcessor(1000);
1775 DetectPS2AuxPort(VOID
)
1778 /* Current detection is too unreliable. Just do as if
1779 * the PS/2 aux port is always present
1786 /* Put the value 0x5A in the output buffer using the
1787 * "WriteAuxiliary Device Output Buffer" command (0xD3).
1788 * Poll the Status Register for a while to see if the value really turns up
1789 * in the Data Register. If the KEYBOARD_STATUS_MOUSE_OBF bit is also set
1790 * to 1 in the Status Register, we assume this controller has an
1791 * Auxiliary Port (a.k.a. Mouse Port).
1793 PS2ControllerWait();
1794 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_CONTROL
,
1795 CONTROLLER_COMMAND_WRITE_MOUSE_OUTPUT_BUFFER
);
1796 PS2ControllerWait();
1798 /* 0x5A is a random dummy value */
1799 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
,
1802 for (Loops
= 0; Loops
< 10; Loops
++)
1804 StallExecutionProcessor(10000);
1805 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1806 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) != 0)
1810 READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1812 return (Status
& CONTROLLER_STATUS_MOUSE_OUTPUT_BUFFER_FULL
);
1818 DetectPS2AuxDevice(VOID
)
1823 BOOLEAN Result
= TRUE
;
1825 PS2ControllerWait();
1826 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_CONTROL
,
1827 CONTROLLER_COMMAND_WRITE_MOUSE
);
1828 PS2ControllerWait();
1830 /* Identify device */
1831 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
,
1834 /* Wait for reply */
1835 for (Loops
= 0; Loops
< 100; Loops
++)
1837 StallExecutionProcessor(10000);
1838 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1839 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) != 0)
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
!= 0xFA)
1851 StallExecutionProcessor(10000);
1853 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1854 if ((Status
& CONTROLLER_STATUS_MOUSE_OUTPUT_BUFFER_FULL
) == 0)
1857 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1858 if (Scancode
!= 0x00)
1866 DetectPS2Mouse(PCONFIGURATION_COMPONENT_DATA BusKey
)
1868 CM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1869 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1870 PCONFIGURATION_COMPONENT_DATA PeripheralKey
;
1872 if (DetectPS2AuxPort())
1874 DPRINTM(DPRINT_HWDETECT
, "Detected PS2 port\n");
1876 memset(&PartialResourceList
, 0, sizeof(CM_PARTIAL_RESOURCE_LIST
));
1878 /* Initialize resource descriptor */
1879 PartialResourceList
.Version
= 1;
1880 PartialResourceList
.Revision
= 1;
1881 PartialResourceList
.Count
= 1;
1884 PartialResourceList
.PartialDescriptors
[0].Type
= CmResourceTypeInterrupt
;
1885 PartialResourceList
.PartialDescriptors
[0].ShareDisposition
= CmResourceShareUndetermined
;
1886 PartialResourceList
.PartialDescriptors
[0].Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
1887 PartialResourceList
.PartialDescriptors
[0].u
.Interrupt
.Level
= 12;
1888 PartialResourceList
.PartialDescriptors
[0].u
.Interrupt
.Vector
= 12;
1889 PartialResourceList
.PartialDescriptors
[0].u
.Interrupt
.Affinity
= 0xFFFFFFFF;
1891 /* Create controller key */
1892 FldrCreateComponentKey(BusKey
,
1899 &PartialResourceList
,
1900 sizeof(CM_PARTIAL_RESOURCE_LIST
),
1902 DPRINTM(DPRINT_HWDETECT
, "Created key: PointerController\\0\n");
1904 if (DetectPS2AuxDevice())
1906 DPRINTM(DPRINT_HWDETECT
, "Detected PS2 mouse\n");
1908 /* Initialize resource descriptor */
1909 memset(&PartialResourceList
, 0, sizeof(CM_PARTIAL_RESOURCE_LIST
));
1910 PartialResourceList
.Version
= 1;
1911 PartialResourceList
.Revision
= 1;
1912 PartialResourceList
.Count
= 0;
1914 /* Create peripheral key */
1915 FldrCreateComponentKey(ControllerKey
,
1921 "MICROSOFT PS2 MOUSE",
1922 &PartialResourceList
,
1923 sizeof(CM_PARTIAL_RESOURCE_LIST
) -
1924 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
),
1926 DPRINTM(DPRINT_HWDETECT
, "Created key: PointerPeripheral\\0\n");
1933 DetectDisplayController(PCONFIGURATION_COMPONENT_DATA BusKey
)
1936 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1939 /* FIXME: Set 'ComponentInformation' value */
1941 VesaVersion
= BiosIsVesaSupported();
1942 if (VesaVersion
!= 0)
1944 DPRINTM(DPRINT_HWDETECT
,
1945 "VESA version %c.%c\n",
1946 (VesaVersion
>> 8) + '0',
1947 (VesaVersion
& 0xFF) + '0');
1951 DPRINTM(DPRINT_HWDETECT
,
1952 "VESA not supported\n");
1955 if (VesaVersion
>= 0x0200)
1966 FldrCreateComponentKey(BusKey
,
1976 DPRINTM(DPRINT_HWDETECT
, "Created key: DisplayController\\0\n");
1978 /* FIXME: Add display peripheral (monitor) data */
1983 DetectIsaBios(PCONFIGURATION_COMPONENT_DATA SystemKey
, ULONG
*BusNumber
)
1985 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1986 PCONFIGURATION_COMPONENT_DATA BusKey
;
1989 /* Increment bus number */
1992 /* Set 'Configuration Data' value */
1993 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) -
1994 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
1995 PartialResourceList
= MmHeapAlloc(Size
);
1996 if (PartialResourceList
== NULL
)
1998 DPRINTM(DPRINT_HWDETECT
,
1999 "Failed to allocate resource descriptor\n");
2003 /* Initialize resource descriptor */
2004 memset(PartialResourceList
, 0, Size
);
2005 PartialResourceList
->Version
= 1;
2006 PartialResourceList
->Revision
= 1;
2007 PartialResourceList
->Count
= 0;
2009 /* Create new bus key */
2010 FldrCreateComponentKey(SystemKey
,
2012 MultiFunctionAdapter
,
2017 PartialResourceList
,
2021 MmHeapFree(PartialResourceList
);
2023 /* Detect ISA/BIOS devices */
2024 DetectBiosDisks(BusKey
);
2026 DetectBiosFloppyController(BusKey
);
2028 DetectSerialPorts(BusKey
);
2030 DetectParallelPorts(BusKey
);
2032 DetectKeyboardController(BusKey
);
2034 DetectPS2Mouse(BusKey
);
2036 DetectDisplayController(BusKey
);
2038 /* FIXME: Detect more ISA devices */
2042 PCONFIGURATION_COMPONENT_DATA
2045 PCONFIGURATION_COMPONENT_DATA SystemKey
;
2046 ULONG BusNumber
= 0;
2048 DPRINTM(DPRINT_HWDETECT
, "DetectHardware()\n");
2050 /* Create the 'System' key */
2051 SystemKey
= DetectSystem();
2054 DetectPciBios(SystemKey
, &BusNumber
);
2055 DetectApmBios(SystemKey
, &BusNumber
);
2056 DetectPnpBios(SystemKey
, &BusNumber
);
2057 DetectIsaBios(SystemKey
, &BusNumber
);
2058 DetectAcpiBios(SystemKey
, &BusNumber
);
2060 DPRINTM(DPRINT_HWDETECT
, "DetectHardware() Done\n");