4 * Copyright (C) 2003, 2004 Eric Kohl
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #define CLOCK_TICK_RATE (1193182)
31 #define LATCH (CLOCK_TICK_RATE / HZ)
35 #define MOUSE_TYPE_NONE 0
36 /* Microsoft Mouse with 2 buttons */
37 #define MOUSE_TYPE_MICROSOFT 1
38 /* Logitech Mouse with 3 buttons */
39 #define MOUSE_TYPE_LOGITECH 2
40 /* Microsoft Wheel Mouse (aka Z Mouse) */
41 #define MOUSE_TYPE_WHEELZ 3
42 /* Mouse Systems Mouse */
43 #define MOUSE_TYPE_MOUSESYSTEMS 4
48 /* Controller registers. */
49 #define CONTROLLER_REGISTER_STATUS 0x64
50 #define CONTROLLER_REGISTER_CONTROL 0x64
51 #define CONTROLLER_REGISTER_DATA 0x60
53 /* Controller commands. */
54 #define CONTROLLER_COMMAND_READ_MODE 0x20
55 #define CONTROLLER_COMMAND_WRITE_MODE 0x60
56 #define CONTROLLER_COMMAND_GET_VERSION 0xA1
57 #define CONTROLLER_COMMAND_MOUSE_DISABLE 0xA7
58 #define CONTROLLER_COMMAND_MOUSE_ENABLE 0xA8
59 #define CONTROLLER_COMMAND_TEST_MOUSE 0xA9
60 #define CONTROLLER_COMMAND_SELF_TEST 0xAA
61 #define CONTROLLER_COMMAND_KEYBOARD_TEST 0xAB
62 #define CONTROLLER_COMMAND_KEYBOARD_DISABLE 0xAD
63 #define CONTROLLER_COMMAND_KEYBOARD_ENABLE 0xAE
64 #define CONTROLLER_COMMAND_WRITE_MOUSE_OUTPUT_BUFFER 0xD3
65 #define CONTROLLER_COMMAND_WRITE_MOUSE 0xD4
67 /* Controller status */
68 #define CONTROLLER_STATUS_OUTPUT_BUFFER_FULL 0x01
69 #define CONTROLLER_STATUS_INPUT_BUFFER_FULL 0x02
70 #define CONTROLLER_STATUS_SELF_TEST 0x04
71 #define CONTROLLER_STATUS_COMMAND 0x08
72 #define CONTROLLER_STATUS_UNLOCKED 0x10
73 #define CONTROLLER_STATUS_MOUSE_OUTPUT_BUFFER_FULL 0x20
74 #define CONTROLLER_STATUS_GENERAL_TIMEOUT 0x40
75 #define CONTROLLER_STATUS_PARITY_ERROR 0x80
76 #define AUX_STATUS_OUTPUT_BUFFER_FULL (CONTROLLER_STATUS_OUTPUT_BUFFER_FULL | \
77 CONTROLLER_STATUS_MOUSE_OUTPUT_BUFFER_FULL)
79 /* Timeout in ms for sending to keyboard controller. */
80 #define CONTROLLER_TIMEOUT 250
82 static CHAR Hex
[] = "0123456789abcdef";
83 static unsigned int delay_count
= 1;
85 extern ULONG reactos_disk_count
;
86 extern ARC_DISK_SIGNATURE reactos_arc_disk_info
[];
87 extern char reactos_arc_strings
[32][256];
89 /* FUNCTIONS ****************************************************************/
93 __StallExecutionProcessor(ULONG Loops
)
95 register volatile unsigned int i
;
96 for (i
= 0; i
< Loops
; i
++);
100 VOID
StallExecutionProcessor(ULONG Microseconds
)
102 ULONGLONG LoopCount
= ((ULONGLONG
)delay_count
* (ULONGLONG
)Microseconds
) / 1000ULL;
103 __StallExecutionProcessor((ULONG
)LoopCount
);
112 WRITE_PORT_UCHAR((PUCHAR
)0x43, 0x00);
113 Count
= READ_PORT_UCHAR((PUCHAR
)0x40);
114 Count
|= READ_PORT_UCHAR((PUCHAR
)0x40) << 8;
121 WaitFor8254Wraparound(VOID
)
124 ULONG PrevCount
= ~0;
127 CurCount
= Read8254Timer();
131 PrevCount
= CurCount
;
132 CurCount
= Read8254Timer();
133 Delta
= CurCount
- PrevCount
;
136 * This limit for delta seems arbitrary, but it isn't, it's
137 * slightly above the level of error a buggy Mercury/Neptune
138 * chipset timer can cause.
146 HalpCalibrateStallExecution(VOID
)
152 /* Initialise timer interrupt with MILLISECOND ms interval */
153 WRITE_PORT_UCHAR((PUCHAR
)0x43, 0x34); /* binary, mode 2, LSB/MSB, ch 0 */
154 WRITE_PORT_UCHAR((PUCHAR
)0x40, LATCH
& 0xff); /* LSB */
155 WRITE_PORT_UCHAR((PUCHAR
)0x40, LATCH
>> 8); /* MSB */
157 /* Stage 1: Coarse calibration */
159 WaitFor8254Wraparound();
164 delay_count
<<= 1; /* Next delay count to try */
166 WaitFor8254Wraparound();
168 __StallExecutionProcessor(delay_count
); /* Do the delay */
170 CurCount
= Read8254Timer();
171 } while (CurCount
> LATCH
/ 2);
173 delay_count
>>= 1; /* Get bottom value for delay */
175 /* Stage 2: Fine calibration */
177 calib_bit
= delay_count
; /* Which bit are we going to test */
179 for(i
=0;i
<PRECISION
;i
++) {
180 calib_bit
>>= 1; /* Next bit to calibrate */
181 if(!calib_bit
) break; /* If we have done all bits, stop */
183 delay_count
|= calib_bit
; /* Set the bit in delay_count */
185 WaitFor8254Wraparound();
187 __StallExecutionProcessor(delay_count
); /* Do the delay */
189 CurCount
= Read8254Timer();
190 if (CurCount
<= LATCH
/ 2) /* If a tick has passed, turn the */
191 delay_count
&= ~calib_bit
; /* calibrated bit back off */
194 /* We're finished: Do the finishing touches */
195 delay_count
/= (MILLISEC
/ 2); /* Calculate delay_count for 1ms */
199 DetectPnpBios(PCONFIGURATION_COMPONENT_DATA SystemKey
, ULONG
*BusNumber
)
201 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
202 PCM_PNP_BIOS_DEVICE_NODE DeviceNode
;
203 PCM_PNP_BIOS_INSTALLATION_CHECK InstData
;
204 PCONFIGURATION_COMPONENT_DATA BusKey
;
209 ULONG FoundNodeCount
;
215 InstData
= (PCM_PNP_BIOS_INSTALLATION_CHECK
)PnpBiosSupported();
216 if (InstData
== NULL
|| strncmp((CHAR
*)InstData
->Signature
, "$PnP", 4))
218 DPRINTM(DPRINT_HWDETECT
, "PnP-BIOS not supported\n");
221 DPRINTM(DPRINT_HWDETECT
, "Signature '%c%c%c%c'\n",
222 InstData
->Signature
[0], InstData
->Signature
[1],
223 InstData
->Signature
[2], InstData
->Signature
[3]);
226 x
= PnpBiosGetDeviceNodeCount(&NodeSize
, &NodeCount
);
227 NodeCount
&= 0xFF; // needed since some fscked up BIOSes return
228 // wrong info (e.g. Mac Virtual PC)
229 // e.g. look: http://my.execpc.com/~geezer/osd/pnp/pnp16.c
230 if (x
!= 0 || NodeSize
== 0 || NodeCount
== 0)
232 DPRINTM(DPRINT_HWDETECT
, "PnP-BIOS failed to enumerate device nodes\n");
235 DPRINTM(DPRINT_HWDETECT
, "PnP-BIOS supported\n");
236 DPRINTM(DPRINT_HWDETECT
, "MaxNodeSize %u NodeCount %u\n", NodeSize
, NodeCount
);
237 DPRINTM(DPRINT_HWDETECT
, "Estimated buffer size %u\n", NodeSize
* NodeCount
);
239 /* Create component key */
240 FldrCreateComponentKey(SystemKey
,
242 MultiFunctionAdapter
,
250 /* Set 'Configuration Data' value */
251 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) + (NodeSize
* NodeCount
);
252 PartialResourceList
= MmHeapAlloc(Size
);
253 if (PartialResourceList
== NULL
)
255 DPRINTM(DPRINT_HWDETECT
,
256 "Failed to allocate resource descriptor\n");
259 memset(PartialResourceList
, 0, Size
);
261 /* Initialize resource descriptor */
262 PartialResourceList
->Version
= 1;
263 PartialResourceList
->Revision
= 1;
264 PartialResourceList
->Count
= 1;
265 PartialResourceList
->PartialDescriptors
[0].Type
=
266 CmResourceTypeDeviceSpecific
;
267 PartialResourceList
->PartialDescriptors
[0].ShareDisposition
=
268 CmResourceShareUndetermined
;
270 Ptr
= (char *)(((ULONG_PTR
)&PartialResourceList
->PartialDescriptors
[0]) +
271 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
));
273 /* Set instalation check data */
274 memcpy (Ptr
, InstData
, sizeof(CM_PNP_BIOS_INSTALLATION_CHECK
));
275 Ptr
+= sizeof(CM_PNP_BIOS_INSTALLATION_CHECK
);
277 /* Copy device nodes */
279 PnpBufferSize
= sizeof(CM_PNP_BIOS_INSTALLATION_CHECK
);
280 for (i
= 0; i
< 0xFF; i
++)
282 NodeNumber
= (UCHAR
)i
;
284 x
= PnpBiosGetDeviceNode(&NodeNumber
, (PVOID
)DISKREADBUFFER
);
287 DeviceNode
= (PCM_PNP_BIOS_DEVICE_NODE
)DISKREADBUFFER
;
289 DPRINTM(DPRINT_HWDETECT
,
290 "Node: %u Size %u (0x%x)\n",
299 Ptr
+= DeviceNode
->Size
;
300 PnpBufferSize
+= DeviceNode
->Size
;
303 if (FoundNodeCount
>= NodeCount
)
308 /* Set real data size */
309 PartialResourceList
->PartialDescriptors
[0].u
.DeviceSpecificData
.DataSize
=
311 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) + PnpBufferSize
;
313 DPRINTM(DPRINT_HWDETECT
, "Real buffer size: %u\n", PnpBufferSize
);
314 DPRINTM(DPRINT_HWDETECT
, "Resource size: %u\n", Size
);
316 FldrSetConfigurationData(BusKey
, PartialResourceList
, Size
);
317 MmHeapFree(PartialResourceList
);
323 SetHarddiskConfigurationData(PCONFIGURATION_COMPONENT_DATA DiskKey
,
326 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
327 PCM_DISK_GEOMETRY_DEVICE_DATA DiskGeometry
;
328 EXTENDED_GEOMETRY ExtGeometry
;
332 /* Set 'Configuration Data' value */
333 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
334 sizeof(CM_DISK_GEOMETRY_DEVICE_DATA
);
335 PartialResourceList
= MmHeapAlloc(Size
);
336 if (PartialResourceList
== NULL
)
338 DPRINTM(DPRINT_HWDETECT
,
339 "Failed to allocate a full resource descriptor\n");
343 memset(PartialResourceList
, 0, Size
);
344 PartialResourceList
->Version
= 1;
345 PartialResourceList
->Revision
= 1;
346 PartialResourceList
->Count
= 1;
347 PartialResourceList
->PartialDescriptors
[0].Type
=
348 CmResourceTypeDeviceSpecific
;
349 // PartialResourceList->PartialDescriptors[0].ShareDisposition =
350 // PartialResourceList->PartialDescriptors[0].Flags =
351 PartialResourceList
->PartialDescriptors
[0].u
.DeviceSpecificData
.DataSize
=
352 sizeof(CM_DISK_GEOMETRY_DEVICE_DATA
);
354 /* Get pointer to geometry data */
355 DiskGeometry
= (PVOID
)(((ULONG_PTR
)PartialResourceList
) + sizeof(CM_PARTIAL_RESOURCE_LIST
));
357 /* Get the disk geometry */
358 ExtGeometry
.Size
= sizeof(EXTENDED_GEOMETRY
);
359 if (DiskGetExtendedDriveParameters(DriveNumber
, &ExtGeometry
, ExtGeometry
.Size
))
361 DiskGeometry
->BytesPerSector
= ExtGeometry
.BytesPerSector
;
362 DiskGeometry
->NumberOfCylinders
= ExtGeometry
.Cylinders
;
363 DiskGeometry
->SectorsPerTrack
= ExtGeometry
.SectorsPerTrack
;
364 DiskGeometry
->NumberOfHeads
= ExtGeometry
.Heads
;
366 else if(MachDiskGetDriveGeometry(DriveNumber
, &Geometry
))
368 DiskGeometry
->BytesPerSector
= Geometry
.BytesPerSector
;
369 DiskGeometry
->NumberOfCylinders
= Geometry
.Cylinders
;
370 DiskGeometry
->SectorsPerTrack
= Geometry
.Sectors
;
371 DiskGeometry
->NumberOfHeads
= Geometry
.Heads
;
375 DPRINTM(DPRINT_HWDETECT
, "Reading disk geometry failed\n");
376 MmHeapFree(PartialResourceList
);
379 DPRINTM(DPRINT_HWDETECT
,
380 "Disk %x: %u Cylinders %u Heads %u Sectors %u Bytes\n",
382 DiskGeometry
->NumberOfCylinders
,
383 DiskGeometry
->NumberOfHeads
,
384 DiskGeometry
->SectorsPerTrack
,
385 DiskGeometry
->BytesPerSector
);
387 FldrSetConfigurationData(DiskKey
, PartialResourceList
, Size
);
388 MmHeapFree(PartialResourceList
);
391 typedef struct tagDISKCONTEXT
395 ULONGLONG SectorOffset
;
396 ULONGLONG SectorNumber
;
399 static LONG
DiskClose(ULONG FileId
)
401 DISKCONTEXT
* Context
= FsGetDeviceSpecific(FileId
);
407 static LONG
DiskGetFileInformation(ULONG FileId
, FILEINFORMATION
* Information
)
412 static LONG
DiskOpen(CHAR
* Path
, OPENMODE OpenMode
, ULONG
* FileId
)
414 DISKCONTEXT
* Context
;
415 ULONG DriveNumber
, DrivePartition
, SectorSize
;
416 ULONGLONG SectorOffset
= 0;
417 PARTITION_TABLE_ENTRY PartitionTableEntry
;
420 if (!DissectArcPath(Path
, FileName
, &DriveNumber
, &DrivePartition
))
422 SectorSize
= (DrivePartition
== 0xff ? 2048 : 512);
423 if (DrivePartition
!= 0xff && DrivePartition
!= 0)
425 if (!MachDiskGetPartitionEntry(DriveNumber
, DrivePartition
, &PartitionTableEntry
))
427 SectorOffset
= PartitionTableEntry
.SectorCountBeforePartition
;
430 Context
= MmHeapAlloc(sizeof(DISKCONTEXT
));
433 Context
->DriveNumber
= DriveNumber
;
434 Context
->SectorSize
= SectorSize
;
435 Context
->SectorOffset
= SectorOffset
;
436 Context
->SectorNumber
= 0;
437 FsSetDeviceSpecific(*FileId
, Context
);
442 static LONG
DiskRead(ULONG FileId
, VOID
* Buffer
, ULONG N
, ULONG
* Count
)
444 DISKCONTEXT
* Context
= FsGetDeviceSpecific(FileId
);
445 UCHAR
* Ptr
= (UCHAR
*)Buffer
;
450 if (N
& (Context
->SectorSize
- 1))
453 for (i
= 0; i
< N
/ Context
->SectorSize
; i
++)
455 ret
= MachDiskReadLogicalSectors(
456 Context
->DriveNumber
,
457 Context
->SectorNumber
+ Context
->SectorOffset
+ i
,
459 (PVOID
)DISKREADBUFFER
);
462 RtlCopyMemory(Ptr
, (PVOID
)DISKREADBUFFER
, Context
->SectorSize
);
463 Ptr
+= Context
->SectorSize
;
470 static LONG
DiskSeek(ULONG FileId
, LARGE_INTEGER
* Position
, SEEKMODE SeekMode
)
472 DISKCONTEXT
* Context
= FsGetDeviceSpecific(FileId
);
474 if (SeekMode
!= SeekAbsolute
)
476 if (Position
->LowPart
& (Context
->SectorSize
- 1))
479 /* FIXME: take HighPart into account */
480 Context
->SectorNumber
= Position
->LowPart
/ Context
->SectorSize
;
484 static const DEVVTBL DiskVtbl
= {
486 DiskGetFileInformation
,
493 GetHarddiskIdentifier(PCHAR Identifier
,
496 PMASTER_BOOT_RECORD Mbr
;
502 PARTITION_TABLE_ENTRY PartitionTableEntry
;
505 if (!MachDiskReadLogicalSectors(DriveNumber
, 0ULL, 1, (PVOID
)DISKREADBUFFER
))
507 DPRINTM(DPRINT_HWDETECT
, "Reading MBR failed\n");
511 Buffer
= (ULONG
*)DISKREADBUFFER
;
512 Mbr
= (PMASTER_BOOT_RECORD
)DISKREADBUFFER
;
514 Signature
= Mbr
->Signature
;
515 DPRINTM(DPRINT_HWDETECT
, "Signature: %x\n", Signature
);
517 /* Calculate the MBR checksum */
519 for (i
= 0; i
< 128; i
++)
521 Checksum
+= Buffer
[i
];
523 Checksum
= ~Checksum
+ 1;
524 DPRINTM(DPRINT_HWDETECT
, "Checksum: %x\n", Checksum
);
526 /* Fill out the ARC disk block */
527 reactos_arc_disk_info
[reactos_disk_count
].Signature
= Signature
;
528 reactos_arc_disk_info
[reactos_disk_count
].CheckSum
= Checksum
;
529 sprintf(ArcName
, "multi(0)disk(0)rdisk(%lu)", reactos_disk_count
);
530 strcpy(reactos_arc_strings
[reactos_disk_count
], ArcName
);
531 reactos_arc_disk_info
[reactos_disk_count
].ArcName
=
532 reactos_arc_strings
[reactos_disk_count
];
533 reactos_disk_count
++;
535 sprintf(ArcName
, "multi(0)disk(0)rdisk(%lu)partition(0)", DriveNumber
- 0x80);
536 FsRegisterDevice(ArcName
, &DiskVtbl
);
540 DiskReportError(FALSE
);
541 while (MachDiskGetPartitionEntry(DriveNumber
, i
, &PartitionTableEntry
))
543 if (PartitionTableEntry
.SystemIndicator
!= PARTITION_ENTRY_UNUSED
)
545 sprintf(ArcName
, "multi(0)disk(0)rdisk(%lu)partition(%lu)", DriveNumber
- 0x80, i
);
546 FsRegisterDevice(ArcName
, &DiskVtbl
);
550 DiskReportError(TRUE
);
552 /* Convert checksum and signature to identifier string */
553 Identifier
[0] = Hex
[(Checksum
>> 28) & 0x0F];
554 Identifier
[1] = Hex
[(Checksum
>> 24) & 0x0F];
555 Identifier
[2] = Hex
[(Checksum
>> 20) & 0x0F];
556 Identifier
[3] = Hex
[(Checksum
>> 16) & 0x0F];
557 Identifier
[4] = Hex
[(Checksum
>> 12) & 0x0F];
558 Identifier
[5] = Hex
[(Checksum
>> 8) & 0x0F];
559 Identifier
[6] = Hex
[(Checksum
>> 4) & 0x0F];
560 Identifier
[7] = Hex
[Checksum
& 0x0F];
562 Identifier
[9] = Hex
[(Signature
>> 28) & 0x0F];
563 Identifier
[10] = Hex
[(Signature
>> 24) & 0x0F];
564 Identifier
[11] = Hex
[(Signature
>> 20) & 0x0F];
565 Identifier
[12] = Hex
[(Signature
>> 16) & 0x0F];
566 Identifier
[13] = Hex
[(Signature
>> 12) & 0x0F];
567 Identifier
[14] = Hex
[(Signature
>> 8) & 0x0F];
568 Identifier
[15] = Hex
[(Signature
>> 4) & 0x0F];
569 Identifier
[16] = Hex
[Signature
& 0x0F];
570 Identifier
[17] = '-';
571 Identifier
[18] = 'A';
573 DPRINTM(DPRINT_HWDETECT
, "Identifier: %s\n", Identifier
);
581 WRITE_PORT_UCHAR((PUCHAR
)0x70, 0x10);
582 Data
= READ_PORT_UCHAR((PUCHAR
)0x71);
584 return ((Data
& 0xF0) ? 1 : 0) + ((Data
& 0x0F) ? 1 : 0);
589 GetFloppyType(UCHAR DriveNumber
)
593 WRITE_PORT_UCHAR((PUCHAR
)0x70, 0x10);
594 Data
= READ_PORT_UCHAR((PUCHAR
)0x71);
596 if (DriveNumber
== 0)
598 else if (DriveNumber
== 1)
608 PUSHORT SegPtr
= (PUSHORT
)0x7A;
609 PUSHORT OfsPtr
= (PUSHORT
)0x78;
611 return (PVOID
)((ULONG_PTR
)(((ULONG
)(*SegPtr
)) << 4) + (ULONG
)(*OfsPtr
));
616 DetectBiosFloppyPeripheral(PCONFIGURATION_COMPONENT_DATA ControllerKey
)
618 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
619 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
620 PCM_FLOPPY_DEVICE_DATA FloppyData
;
622 PCONFIGURATION_COMPONENT_DATA PeripheralKey
;
626 ULONG MaxDensity
[6] = {0, 360, 1200, 720, 1440, 2880};
629 for (FloppyNumber
= 0; FloppyNumber
< 2; FloppyNumber
++)
631 FloppyType
= GetFloppyType(FloppyNumber
);
633 if ((FloppyType
> 5) || (FloppyType
== 0))
636 DiskResetController(FloppyNumber
);
638 Ptr
= GetInt1eTable();
640 /* Set 'Identifier' value */
641 sprintf(Identifier
, "FLOPPY%ld", FloppyNumber
+ 1);
643 FldrCreateComponentKey(ControllerKey
,
645 FloppyDiskPeripheral
,
652 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
653 sizeof(CM_FLOPPY_DEVICE_DATA
);
654 PartialResourceList
= MmHeapAlloc(Size
);
655 if (PartialResourceList
== NULL
)
657 DPRINTM(DPRINT_HWDETECT
,
658 "Failed to allocate resource descriptor\n");
662 memset(PartialResourceList
, 0, Size
);
663 PartialResourceList
->Version
= 1;
664 PartialResourceList
->Revision
= 1;
665 PartialResourceList
->Count
= 1;
667 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
668 PartialDescriptor
->Type
= CmResourceTypeDeviceSpecific
;
669 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
670 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
= sizeof(CM_FLOPPY_DEVICE_DATA
);
672 FloppyData
= (PVOID
)(((ULONG_PTR
)PartialResourceList
) + sizeof(CM_PARTIAL_RESOURCE_LIST
));
673 FloppyData
->Version
= 2;
674 FloppyData
->Revision
= 0;
675 FloppyData
->MaxDensity
= MaxDensity
[FloppyType
];
676 FloppyData
->MountDensity
= 0;
677 RtlCopyMemory(&FloppyData
->StepRateHeadUnloadTime
,
680 FloppyData
->MaximumTrackValue
= (FloppyType
== 1) ? 39 : 79;
681 FloppyData
->DataTransferRate
= 0;
683 /* Set 'Configuration Data' value */
684 FldrSetConfigurationData(PeripheralKey
, PartialResourceList
, Size
);
685 MmHeapFree(PartialResourceList
);
691 DetectBiosFloppyController(PCONFIGURATION_COMPONENT_DATA BusKey
,
692 PCONFIGURATION_COMPONENT_DATA ControllerKey
)
694 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
695 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
699 FloppyCount
= GetFloppyCount();
700 DPRINTM(DPRINT_HWDETECT
,
701 "Floppy count: %u\n",
704 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
705 2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
706 PartialResourceList
= MmHeapAlloc(Size
);
707 if (PartialResourceList
== NULL
)
709 DPRINTM(DPRINT_HWDETECT
,
710 "Failed to allocate resource descriptor\n");
713 memset(PartialResourceList
, 0, Size
);
715 /* Initialize resource descriptor */
716 PartialResourceList
->Version
= 1;
717 PartialResourceList
->Revision
= 1;
718 PartialResourceList
->Count
= 3;
721 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
722 PartialDescriptor
->Type
= CmResourceTypePort
;
723 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
724 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
725 PartialDescriptor
->u
.Port
.Start
.LowPart
= 0x03F0;
726 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
727 PartialDescriptor
->u
.Port
.Length
= 8;
730 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[1];
731 PartialDescriptor
->Type
= CmResourceTypeInterrupt
;
732 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
733 PartialDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
734 PartialDescriptor
->u
.Interrupt
.Level
= 6;
735 PartialDescriptor
->u
.Interrupt
.Vector
= 6;
736 PartialDescriptor
->u
.Interrupt
.Affinity
= 0xFFFFFFFF;
738 /* Set DMA channel */
739 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[2];
740 PartialDescriptor
->Type
= CmResourceTypeDma
;
741 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
742 PartialDescriptor
->Flags
= 0;
743 PartialDescriptor
->u
.Dma
.Channel
= 2;
744 PartialDescriptor
->u
.Dma
.Port
= 0;
746 /* Set 'Configuration Data' value */
747 FldrSetConfigurationData(ControllerKey
, PartialResourceList
, Size
);
748 MmHeapFree(PartialResourceList
);
750 if (FloppyCount
) DetectBiosFloppyPeripheral(ControllerKey
);
754 DetectBiosDisks(PCONFIGURATION_COMPONENT_DATA SystemKey
,
755 PCONFIGURATION_COMPONENT_DATA BusKey
)
757 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
758 PCM_INT13_DRIVE_PARAMETER Int13Drives
;
760 PCONFIGURATION_COMPONENT_DATA DiskKey
, ControllerKey
;
764 BOOLEAN Changed
, BootDriveReported
= FALSE
;
767 /* Count the number of visible drives */
768 DiskReportError(FALSE
);
771 /* There are some really broken BIOSes out there. There are even BIOSes
772 * that happily report success when you ask them to read from non-existent
773 * harddisks. So, we set the buffer to known contents first, then try to
774 * read. If the BIOS reports success but the buffer contents haven't
775 * changed then we fail anyway */
776 memset((PVOID
) DISKREADBUFFER
, 0xcd, 512);
777 while (MachDiskReadLogicalSectors(0x80 + DiskCount
, 0ULL, 1, (PVOID
)DISKREADBUFFER
))
780 for (i
= 0; ! Changed
&& i
< 512; i
++)
782 Changed
= ((PUCHAR
)DISKREADBUFFER
)[i
] != 0xcd;
786 DPRINTM(DPRINT_HWDETECT
, "BIOS reports success for disk %d but data didn't change\n",
791 memset((PVOID
) DISKREADBUFFER
, 0xcd, 512);
793 DiskReportError(TRUE
);
794 DPRINTM(DPRINT_HWDETECT
, "BIOS reports %d harddisk%s\n",
795 (int)DiskCount
, (DiskCount
== 1) ? "": "s");
797 FldrCreateComponentKey(BusKey
,
800 Output
| Input
| Removable
,
805 DPRINTM(DPRINT_HWDETECT
, "Created key: DiskController\\0\n");
807 DetectBiosFloppyController(BusKey
, ControllerKey
);
809 /* Allocate resource descriptor */
810 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
811 sizeof(CM_INT13_DRIVE_PARAMETER
) * DiskCount
;
812 PartialResourceList
= MmHeapAlloc(Size
);
813 if (PartialResourceList
== NULL
)
815 DPRINTM(DPRINT_HWDETECT
,
816 "Failed to allocate resource descriptor\n");
820 /* Initialize resource descriptor */
821 memset(PartialResourceList
, 0, Size
);
822 PartialResourceList
->Version
= 1;
823 PartialResourceList
->Revision
= 1;
824 PartialResourceList
->Count
= 1;
825 PartialResourceList
->PartialDescriptors
[0].Type
= CmResourceTypeDeviceSpecific
;
826 PartialResourceList
->PartialDescriptors
[0].ShareDisposition
= 0;
827 PartialResourceList
->PartialDescriptors
[0].Flags
= 0;
828 PartialResourceList
->PartialDescriptors
[0].u
.DeviceSpecificData
.DataSize
=
829 sizeof(CM_INT13_DRIVE_PARAMETER
) * DiskCount
;
831 /* Get harddisk Int13 geometry data */
832 Int13Drives
= (PVOID
)(((ULONG_PTR
)PartialResourceList
) + sizeof(CM_PARTIAL_RESOURCE_LIST
));
833 for (i
= 0; i
< DiskCount
; i
++)
835 if (BootDrive
== 0x80 + i
)
836 BootDriveReported
= TRUE
;
838 if (MachDiskGetDriveGeometry(0x80 + i
, &Geometry
))
840 Int13Drives
[i
].DriveSelect
= 0x80 + i
;
841 Int13Drives
[i
].MaxCylinders
= Geometry
.Cylinders
- 1;
842 Int13Drives
[i
].SectorsPerTrack
= Geometry
.Sectors
;
843 Int13Drives
[i
].MaxHeads
= Geometry
.Heads
- 1;
844 Int13Drives
[i
].NumberDrives
= DiskCount
;
846 DPRINTM(DPRINT_HWDETECT
,
847 "Disk %x: %u Cylinders %u Heads %u Sectors %u Bytes\n",
849 Geometry
.Cylinders
- 1,
852 Geometry
.BytesPerSector
);
856 /* Set 'Configuration Data' value */
857 FldrSetConfigurationData(SystemKey
, PartialResourceList
, Size
);
858 MmHeapFree(PartialResourceList
);
860 /* Create and fill subkey for each harddisk */
861 for (i
= 0; i
< DiskCount
; i
++)
865 /* Get disk values */
866 GetHarddiskIdentifier(Identifier
, 0x80 + i
);
868 /* Create disk key */
869 FldrCreateComponentKey(ControllerKey
,
878 /* Set disk values */
879 SetHarddiskConfigurationData(DiskKey
, 0x80 + i
);
882 /* Get the drive we're booting from */
883 MachDiskGetBootPath(BootPath
, sizeof(BootPath
));
885 /* Add it, if it's a floppy or cdrom */
886 if ((BootDrive
>= 0x80 && !BootDriveReported
) ||
887 DiskIsDriveRemovable(BootDrive
))
889 /* TODO: Check if it's really a cdrom drive */
890 FsRegisterDevice(BootPath
, &DiskVtbl
);
895 InitializeSerialPort(PUCHAR Port
,
898 WRITE_PORT_UCHAR(Port
+ 3, 0x80); /* set DLAB on */
899 WRITE_PORT_UCHAR(Port
, 0x60); /* speed LO byte */
900 WRITE_PORT_UCHAR(Port
+ 1, 0); /* speed HI byte */
901 WRITE_PORT_UCHAR(Port
+ 3, LineControl
);
902 WRITE_PORT_UCHAR(Port
+ 1, 0); /* set comm and DLAB to 0 */
903 WRITE_PORT_UCHAR(Port
+ 4, 0x09); /* DR int enable */
904 READ_PORT_UCHAR(Port
+ 5); /* clear error bits */
909 DetectSerialMouse(PUCHAR Port
)
916 /* Shutdown mouse or something like that */
917 LineControl
= READ_PORT_UCHAR(Port
+ 4);
918 WRITE_PORT_UCHAR(Port
+ 4, (LineControl
& ~0x02) | 0x01);
919 StallExecutionProcessor(100000);
923 * Maybe there is no serial port although BIOS reported one (this
924 * is the case on Apple hardware), or the serial port is misbehaving,
925 * therefore we must give up after some time.
928 while (READ_PORT_UCHAR(Port
+ 5) & 0x01)
931 return MOUSE_TYPE_NONE
;
932 READ_PORT_UCHAR(Port
);
936 * Send modem control with 'Data Terminal Ready', 'Request To Send' and
937 * 'Output Line 2' message. This enables mouse to identify.
939 WRITE_PORT_UCHAR(Port
+ 4, 0x0b);
941 /* Wait 10 milliseconds for the mouse getting ready */
942 StallExecutionProcessor(10000);
944 /* Read first four bytes, which contains Microsoft Mouse signs */
946 for (i
= 0; i
< 4; i
++)
948 while (((READ_PORT_UCHAR(Port
+ 5) & 1) == 0) && (TimeOut
> 0))
950 StallExecutionProcessor(1000);
953 return MOUSE_TYPE_NONE
;
955 Buffer
[i
] = READ_PORT_UCHAR(Port
);
958 DPRINTM(DPRINT_HWDETECT
,
959 "Mouse data: %x %x %x %x\n",
960 Buffer
[0],Buffer
[1],Buffer
[2],Buffer
[3]);
962 /* Check that four bytes for signs */
963 for (i
= 0; i
< 4; ++i
)
965 if (Buffer
[i
] == 'B')
967 /* Sign for Microsoft Ballpoint */
968 // DbgPrint("Microsoft Ballpoint device detected\n");
969 // DbgPrint("THIS DEVICE IS NOT SUPPORTED, YET\n");
970 return MOUSE_TYPE_NONE
;
972 else if (Buffer
[i
] == 'M')
974 /* Sign for Microsoft Mouse protocol followed by button specifier */
978 return MOUSE_TYPE_NONE
;
981 switch (Buffer
[i
+ 1])
984 DPRINTM(DPRINT_HWDETECT
,
985 "Microsoft Mouse with 3-buttons detected\n");
986 return MOUSE_TYPE_LOGITECH
;
989 DPRINTM(DPRINT_HWDETECT
,
990 "Microsoft Wheel Mouse detected\n");
991 return MOUSE_TYPE_WHEELZ
;
995 DPRINTM(DPRINT_HWDETECT
,
996 "Microsoft Mouse with 2-buttons detected\n");
997 return MOUSE_TYPE_MICROSOFT
;
1002 return MOUSE_TYPE_NONE
;
1007 GetSerialMousePnpId(PUCHAR Port
, char *Buffer
)
1014 WRITE_PORT_UCHAR(Port
+ 4, 0x09);
1016 /* Wait 10 milliseconds for the mouse getting ready */
1017 StallExecutionProcessor(10000);
1019 WRITE_PORT_UCHAR(Port
+ 4, 0x0b);
1021 StallExecutionProcessor(10000);
1026 while (((READ_PORT_UCHAR(Port
+ 5) & 1) == 0) && (TimeOut
> 0))
1028 StallExecutionProcessor(1000);
1036 c
= READ_PORT_UCHAR(Port
);
1037 if (c
== 0x08 || c
== 0x28)
1047 while (((READ_PORT_UCHAR(Port
+ 5) & 1) == 0) && (TimeOut
> 0))
1049 StallExecutionProcessor(1000);
1054 c
= READ_PORT_UCHAR(Port
);
1067 DetectSerialPointerPeripheral(PCONFIGURATION_COMPONENT_DATA ControllerKey
,
1070 CM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1072 CHAR Identifier
[256];
1073 PCONFIGURATION_COMPONENT_DATA PeripheralKey
;
1080 DPRINTM(DPRINT_HWDETECT
,
1081 "DetectSerialPointerPeripheral()\n");
1085 InitializeSerialPort(Base
, 2);
1086 MouseType
= DetectSerialMouse(Base
);
1088 if (MouseType
!= MOUSE_TYPE_NONE
)
1090 Length
= GetSerialMousePnpId(Base
, Buffer
);
1091 DPRINTM(DPRINT_HWDETECT
,
1092 "PnP ID length: %u\n",
1097 /* Convert PnP sting to ASCII */
1098 if (Buffer
[0] == 0x08)
1100 for (i
= 0; i
< Length
; i
++)
1105 DPRINTM(DPRINT_HWDETECT
,
1106 "PnP ID string: %s\n",
1109 /* Copy PnpId string */
1110 for (i
= 0; i
< 7; i
++)
1112 Identifier
[i
] = Buffer
[3+i
];
1114 memcpy(&Identifier
[7],
1118 /* Skip device serial number */
1120 if (Buffer
[i
] == '\\')
1122 for (j
= ++i
; i
< Length
; ++i
)
1124 if (Buffer
[i
] == '\\')
1131 /* Skip PnP class */
1132 if (Buffer
[i
] == '\\')
1134 for (j
= ++i
; i
< Length
; ++i
)
1136 if (Buffer
[i
] == '\\')
1144 /* Skip compatible PnP Id */
1145 if (Buffer
[i
] == '\\')
1147 for (j
= ++i
; i
< Length
; ++i
)
1149 if (Buffer
[i
] == '\\')
1152 if (Buffer
[j
] == '*')
1158 /* Get product description */
1159 if (Buffer
[i
] == '\\')
1161 for (j
= ++i
; i
< Length
; ++i
)
1163 if (Buffer
[i
] == ';')
1170 for (k
= 0; k
< i
- j
; k
++)
1172 Identifier
[k
+ 10] = Buffer
[k
+ j
];
1174 Identifier
[10 + (i
-j
)] = 0;
1178 DPRINTM(DPRINT_HWDETECT
,
1179 "Identifier string: %s\n",
1183 if (Length
== 0 || strlen(Identifier
) < 11)
1187 case MOUSE_TYPE_LOGITECH
:
1189 "LOGITECH SERIAL MOUSE");
1192 case MOUSE_TYPE_WHEELZ
:
1194 "MICROSOFT SERIAL MOUSE WITH WHEEL");
1197 case MOUSE_TYPE_MICROSOFT
:
1200 "MICROSOFT SERIAL MOUSE");
1205 /* Create 'PointerPeripheral' key */
1206 FldrCreateComponentKey(ControllerKey
,
1214 DPRINTM(DPRINT_HWDETECT
,
1215 "Created key: PointerPeripheral\\0\n");
1217 /* Set 'Configuration Data' value */
1218 memset(&PartialResourceList
, 0, sizeof(CM_PARTIAL_RESOURCE_LIST
));
1219 PartialResourceList
.Version
= 1;
1220 PartialResourceList
.Revision
= 1;
1221 PartialResourceList
.Count
= 0;
1223 FldrSetConfigurationData(PeripheralKey
,
1224 &PartialResourceList
,
1225 sizeof(CM_PARTIAL_RESOURCE_LIST
) -
1226 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
));
1232 DetectSerialPorts(PCONFIGURATION_COMPONENT_DATA BusKey
)
1234 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1235 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
1236 PCM_SERIAL_DEVICE_DATA SerialDeviceData
;
1237 ULONG Irq
[4] = {4, 3, 4, 3};
1241 ULONG ControllerNumber
= 0;
1242 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1246 DPRINTM(DPRINT_HWDETECT
, "DetectSerialPorts()\n");
1248 ControllerNumber
= 0;
1249 BasePtr
= (PUSHORT
)0x400;
1250 for (i
= 0; i
< 4; i
++, BasePtr
++)
1252 Base
= (ULONG
)*BasePtr
;
1256 DPRINTM(DPRINT_HWDETECT
,
1257 "Found COM%u port at 0x%x\n",
1261 /* Set 'Identifier' value */
1262 sprintf(Buffer
, "COM%ld", i
+ 1);
1264 /* Create controller key */
1265 FldrCreateComponentKey(BusKey
,
1268 Output
| Input
| ConsoleIn
| ConsoleOut
,
1274 /* Build full device descriptor */
1275 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
1276 2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
) +
1277 sizeof(CM_SERIAL_DEVICE_DATA
);
1278 PartialResourceList
= MmHeapAlloc(Size
);
1279 if (PartialResourceList
== NULL
)
1281 DPRINTM(DPRINT_HWDETECT
,
1282 "Failed to allocate resource descriptor\n");
1285 memset(PartialResourceList
, 0, Size
);
1287 /* Initialize resource descriptor */
1288 PartialResourceList
->Version
= 1;
1289 PartialResourceList
->Revision
= 1;
1290 PartialResourceList
->Count
= 3;
1293 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
1294 PartialDescriptor
->Type
= CmResourceTypePort
;
1295 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
1296 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
1297 PartialDescriptor
->u
.Port
.Start
.LowPart
= Base
;
1298 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
1299 PartialDescriptor
->u
.Port
.Length
= 7;
1302 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[1];
1303 PartialDescriptor
->Type
= CmResourceTypeInterrupt
;
1304 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
1305 PartialDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
1306 PartialDescriptor
->u
.Interrupt
.Level
= Irq
[i
];
1307 PartialDescriptor
->u
.Interrupt
.Vector
= 0;
1308 PartialDescriptor
->u
.Interrupt
.Affinity
= 0xFFFFFFFF;
1310 /* Set serial data (device specific) */
1311 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[2];
1312 PartialDescriptor
->Type
= CmResourceTypeDeviceSpecific
;
1313 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
1314 PartialDescriptor
->Flags
= 0;
1315 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
= sizeof(CM_SERIAL_DEVICE_DATA
);
1318 (PCM_SERIAL_DEVICE_DATA
)&PartialResourceList
->PartialDescriptors
[3];
1319 SerialDeviceData
->BaudClock
= 1843200; /* UART Clock frequency (Hertz) */
1321 /* Set 'Configuration Data' value */
1322 FldrSetConfigurationData(ControllerKey
, PartialResourceList
, Size
);
1323 MmHeapFree(PartialResourceList
);
1325 if (!Rs232PortInUse(Base
))
1327 /* Detect serial mouse */
1328 DetectSerialPointerPeripheral(ControllerKey
, UlongToPtr(Base
));
1337 DetectParallelPorts(PCONFIGURATION_COMPONENT_DATA BusKey
)
1339 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1340 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
1341 ULONG Irq
[3] = {7, 5, (ULONG
)-1};
1343 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1346 ULONG ControllerNumber
;
1350 DPRINTM(DPRINT_HWDETECT
, "DetectParallelPorts() called\n");
1352 ControllerNumber
= 0;
1353 BasePtr
= (PUSHORT
)0x408;
1354 for (i
= 0; i
< 3; i
++, BasePtr
++)
1356 Base
= (ULONG
)*BasePtr
;
1360 DPRINTM(DPRINT_HWDETECT
,
1361 "Parallel port %u: %x\n",
1365 /* Set 'Identifier' value */
1366 sprintf(Buffer
, "PARALLEL%ld", i
+ 1);
1368 /* Create controller key */
1369 FldrCreateComponentKey(BusKey
,
1378 /* Build full device descriptor */
1379 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
);
1380 if (Irq
[i
] != (ULONG
)-1)
1381 Size
+= sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
1383 PartialResourceList
= MmHeapAlloc(Size
);
1384 if (PartialResourceList
== NULL
)
1386 DPRINTM(DPRINT_HWDETECT
,
1387 "Failed to allocate resource descriptor\n");
1390 memset(PartialResourceList
, 0, Size
);
1392 /* Initialize resource descriptor */
1393 PartialResourceList
->Version
= 1;
1394 PartialResourceList
->Revision
= 1;
1395 PartialResourceList
->Count
= (Irq
[i
] != (ULONG
)-1) ? 2 : 1;
1398 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
1399 PartialDescriptor
->Type
= CmResourceTypePort
;
1400 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
1401 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
1402 PartialDescriptor
->u
.Port
.Start
.LowPart
= Base
;
1403 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
1404 PartialDescriptor
->u
.Port
.Length
= 3;
1407 if (Irq
[i
] != (ULONG
)-1)
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;
1418 /* Set 'Configuration Data' value */
1419 FldrSetConfigurationData(ControllerKey
, PartialResourceList
, Size
);
1420 MmHeapFree(PartialResourceList
);
1425 DPRINTM(DPRINT_HWDETECT
, "DetectParallelPorts() done\n");
1430 DetectKeyboardDevice(VOID
)
1435 BOOLEAN Result
= TRUE
;
1437 /* Identify device */
1438 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
,
1441 /* Wait for reply */
1442 for (Loops
= 0; Loops
< 100; Loops
++)
1444 StallExecutionProcessor(10000);
1445 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1446 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) != 0)
1450 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) == 0)
1452 /* PC/XT keyboard or no keyboard */
1456 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1457 if (Scancode
!= 0xFA)
1459 /* No ACK received */
1463 StallExecutionProcessor(10000);
1465 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1466 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) == 0)
1468 /* Found AT keyboard */
1472 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1473 if (Scancode
!= 0xAB)
1475 /* No 0xAB received */
1479 StallExecutionProcessor(10000);
1481 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1482 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) == 0)
1484 /* No byte in buffer */
1488 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1489 if (Scancode
!= 0x41)
1491 /* No 0x41 received */
1495 /* Found MF-II keyboard */
1501 DetectKeyboardPeripheral(PCONFIGURATION_COMPONENT_DATA ControllerKey
)
1503 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1504 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
1505 PCM_KEYBOARD_DEVICE_DATA KeyboardData
;
1506 PCONFIGURATION_COMPONENT_DATA PeripheralKey
;
1509 /* HACK: don't call DetectKeyboardDevice() as it fails in Qemu 0.8.2 */
1510 if (TRUE
|| DetectKeyboardDevice())
1512 /* Create controller key */
1513 FldrCreateComponentKey(ControllerKey
,
1521 DPRINTM(DPRINT_HWDETECT
, "Created key: KeyboardPeripheral\\0\n");
1523 /* Set 'Configuration Data' value */
1524 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
1525 sizeof(CM_KEYBOARD_DEVICE_DATA
);
1526 PartialResourceList
= MmHeapAlloc(Size
);
1527 if (PartialResourceList
== NULL
)
1529 DPRINTM(DPRINT_HWDETECT
,
1530 "Failed to allocate resource descriptor\n");
1534 /* Initialize resource descriptor */
1535 memset(PartialResourceList
, 0, Size
);
1536 PartialResourceList
->Version
= 1;
1537 PartialResourceList
->Revision
= 1;
1538 PartialResourceList
->Count
= 1;
1540 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
1541 PartialDescriptor
->Type
= CmResourceTypeDeviceSpecific
;
1542 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
1543 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
= sizeof(CM_KEYBOARD_DEVICE_DATA
);
1545 KeyboardData
= (PCM_KEYBOARD_DEVICE_DATA
)(PartialDescriptor
+ 1);
1546 KeyboardData
->Version
= 1;
1547 KeyboardData
->Revision
= 1;
1548 KeyboardData
->Type
= 4;
1549 KeyboardData
->Subtype
= 0;
1550 KeyboardData
->KeyboardFlags
= 0x20;
1552 /* Set 'Configuration Data' value */
1553 FldrSetConfigurationData(PeripheralKey
, PartialResourceList
, Size
);
1554 MmHeapFree(PartialResourceList
);
1560 DetectKeyboardController(PCONFIGURATION_COMPONENT_DATA BusKey
)
1562 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1563 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
1564 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1567 /* Create controller key */
1568 FldrCreateComponentKey(BusKey
,
1576 DPRINTM(DPRINT_HWDETECT
, "Created key: KeyboardController\\0\n");
1578 /* Set 'Configuration Data' value */
1579 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) +
1580 2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
1581 PartialResourceList
= MmHeapAlloc(Size
);
1582 if (PartialResourceList
== NULL
)
1584 DPRINTM(DPRINT_HWDETECT
,
1585 "Failed to allocate resource descriptor\n");
1589 /* Initialize resource descriptor */
1590 memset(PartialResourceList
, 0, Size
);
1591 PartialResourceList
->Version
= 1;
1592 PartialResourceList
->Revision
= 1;
1593 PartialResourceList
->Count
= 3;
1596 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[0];
1597 PartialDescriptor
->Type
= CmResourceTypeInterrupt
;
1598 PartialDescriptor
->ShareDisposition
= CmResourceShareUndetermined
;
1599 PartialDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
1600 PartialDescriptor
->u
.Interrupt
.Level
= 1;
1601 PartialDescriptor
->u
.Interrupt
.Vector
= 0;
1602 PartialDescriptor
->u
.Interrupt
.Affinity
= 0xFFFFFFFF;
1604 /* Set IO Port 0x60 */
1605 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[1];
1606 PartialDescriptor
->Type
= CmResourceTypePort
;
1607 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
1608 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
1609 PartialDescriptor
->u
.Port
.Start
.LowPart
= 0x60;
1610 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
1611 PartialDescriptor
->u
.Port
.Length
= 1;
1613 /* Set IO Port 0x64 */
1614 PartialDescriptor
= &PartialResourceList
->PartialDescriptors
[2];
1615 PartialDescriptor
->Type
= CmResourceTypePort
;
1616 PartialDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
1617 PartialDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
1618 PartialDescriptor
->u
.Port
.Start
.LowPart
= 0x64;
1619 PartialDescriptor
->u
.Port
.Start
.HighPart
= 0x0;
1620 PartialDescriptor
->u
.Port
.Length
= 1;
1622 /* Set 'Configuration Data' value */
1623 FldrSetConfigurationData(ControllerKey
, PartialResourceList
, Size
);
1624 MmHeapFree(PartialResourceList
);
1626 DetectKeyboardPeripheral(ControllerKey
);
1631 PS2ControllerWait(VOID
)
1636 for (Timeout
= 0; Timeout
< CONTROLLER_TIMEOUT
; Timeout
++)
1638 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1639 if ((Status
& CONTROLLER_STATUS_INPUT_BUFFER_FULL
) == 0)
1642 /* Sleep for one millisecond */
1643 StallExecutionProcessor(1000);
1649 DetectPS2AuxPort(VOID
)
1652 /* Current detection is too unreliable. Just do as if
1653 * the PS/2 aux port is always present
1660 /* Put the value 0x5A in the output buffer using the
1661 * "WriteAuxiliary Device Output Buffer" command (0xD3).
1662 * Poll the Status Register for a while to see if the value really turns up
1663 * in the Data Register. If the KEYBOARD_STATUS_MOUSE_OBF bit is also set
1664 * to 1 in the Status Register, we assume this controller has an
1665 * Auxiliary Port (a.k.a. Mouse Port).
1667 PS2ControllerWait();
1668 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_CONTROL
,
1669 CONTROLLER_COMMAND_WRITE_MOUSE_OUTPUT_BUFFER
);
1670 PS2ControllerWait();
1672 /* 0x5A is a random dummy value */
1673 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
,
1676 for (Loops
= 0; Loops
< 10; Loops
++)
1678 StallExecutionProcessor(10000);
1679 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1680 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) != 0)
1684 READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1686 return (Status
& CONTROLLER_STATUS_MOUSE_OUTPUT_BUFFER_FULL
);
1692 DetectPS2AuxDevice(VOID
)
1697 BOOLEAN Result
= TRUE
;
1699 PS2ControllerWait();
1700 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_CONTROL
,
1701 CONTROLLER_COMMAND_WRITE_MOUSE
);
1702 PS2ControllerWait();
1704 /* Identify device */
1705 WRITE_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
,
1708 /* Wait for reply */
1709 for (Loops
= 0; Loops
< 100; Loops
++)
1711 StallExecutionProcessor(10000);
1712 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1713 if ((Status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) != 0)
1717 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1718 if ((Status
& CONTROLLER_STATUS_MOUSE_OUTPUT_BUFFER_FULL
) == 0)
1721 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1722 if (Scancode
!= 0xFA)
1725 StallExecutionProcessor(10000);
1727 Status
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_STATUS
);
1728 if ((Status
& CONTROLLER_STATUS_MOUSE_OUTPUT_BUFFER_FULL
) == 0)
1731 Scancode
= READ_PORT_UCHAR((PUCHAR
)CONTROLLER_REGISTER_DATA
);
1732 if (Scancode
!= 0x00)
1740 DetectPS2Mouse(PCONFIGURATION_COMPONENT_DATA BusKey
)
1742 CM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1743 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1744 PCONFIGURATION_COMPONENT_DATA PeripheralKey
;
1746 if (DetectPS2AuxPort())
1748 DPRINTM(DPRINT_HWDETECT
, "Detected PS2 port\n");
1750 /* Create controller key */
1751 FldrCreateComponentKey(BusKey
,
1759 DPRINTM(DPRINT_HWDETECT
, "Created key: PointerController\\0\n");
1761 memset(&PartialResourceList
, 0, sizeof(CM_PARTIAL_RESOURCE_LIST
));
1763 /* Initialize resource descriptor */
1764 PartialResourceList
.Version
= 1;
1765 PartialResourceList
.Revision
= 1;
1766 PartialResourceList
.Count
= 1;
1769 PartialResourceList
.PartialDescriptors
[0].Type
= CmResourceTypeInterrupt
;
1770 PartialResourceList
.PartialDescriptors
[0].ShareDisposition
= CmResourceShareUndetermined
;
1771 PartialResourceList
.PartialDescriptors
[0].Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
1772 PartialResourceList
.PartialDescriptors
[0].u
.Interrupt
.Level
= 12;
1773 PartialResourceList
.PartialDescriptors
[0].u
.Interrupt
.Vector
= 0;
1774 PartialResourceList
.PartialDescriptors
[0].u
.Interrupt
.Affinity
= 0xFFFFFFFF;
1776 /* Set 'Configuration Data' value */
1777 FldrSetConfigurationData(ControllerKey
,
1778 &PartialResourceList
,
1779 sizeof(CM_PARTIAL_RESOURCE_LIST
));
1781 if (DetectPS2AuxDevice())
1783 DPRINTM(DPRINT_HWDETECT
, "Detected PS2 mouse\n");
1785 /* Create peripheral key */
1786 FldrCreateComponentKey(ControllerKey
,
1792 "MICROSOFT PS2 MOUSE",
1794 DPRINTM(DPRINT_HWDETECT
, "Created key: PointerPeripheral\\0\n");
1796 /* Initialize resource descriptor */
1797 memset(&PartialResourceList
, 0, sizeof(CM_PARTIAL_RESOURCE_LIST
));
1798 PartialResourceList
.Version
= 1;
1799 PartialResourceList
.Revision
= 1;
1800 PartialResourceList
.Count
= 0;
1802 /* Set 'Configuration Data' value */
1803 FldrSetConfigurationData(PeripheralKey
,
1804 &PartialResourceList
,
1805 sizeof(CM_PARTIAL_RESOURCE_LIST
) -
1806 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
));
1813 DetectDisplayController(PCONFIGURATION_COMPONENT_DATA BusKey
)
1816 PCONFIGURATION_COMPONENT_DATA ControllerKey
;
1819 /* FIXME: Set 'ComponentInformation' value */
1821 VesaVersion
= BiosIsVesaSupported();
1822 if (VesaVersion
!= 0)
1824 DPRINTM(DPRINT_HWDETECT
,
1825 "VESA version %c.%c\n",
1826 (VesaVersion
>> 8) + '0',
1827 (VesaVersion
& 0xFF) + '0');
1831 DPRINTM(DPRINT_HWDETECT
,
1832 "VESA not supported\n");
1835 if (VesaVersion
>= 0x0200)
1846 FldrCreateComponentKey(BusKey
,
1854 DPRINTM(DPRINT_HWDETECT
, "Created key: DisplayController\\0\n");
1856 /* FIXME: Add display peripheral (monitor) data */
1861 DetectIsaBios(PCONFIGURATION_COMPONENT_DATA SystemKey
, ULONG
*BusNumber
)
1863 PCM_PARTIAL_RESOURCE_LIST PartialResourceList
;
1864 PCONFIGURATION_COMPONENT_DATA BusKey
;
1867 /* Create new bus key */
1868 FldrCreateComponentKey(SystemKey
,
1870 MultiFunctionAdapter
,
1877 /* Increment bus number */
1880 /* Set 'Configuration Data' value */
1881 Size
= sizeof(CM_PARTIAL_RESOURCE_LIST
) -
1882 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
1883 PartialResourceList
= MmHeapAlloc(Size
);
1884 if (PartialResourceList
== NULL
)
1886 DPRINTM(DPRINT_HWDETECT
,
1887 "Failed to allocate resource descriptor\n");
1891 /* Initialize resource descriptor */
1892 memset(PartialResourceList
, 0, Size
);
1893 PartialResourceList
->Version
= 1;
1894 PartialResourceList
->Revision
= 1;
1895 PartialResourceList
->Count
= 0;
1897 /* Set 'Configuration Data' value */
1898 FldrSetConfigurationData(BusKey
, PartialResourceList
, Size
);
1899 MmHeapFree(PartialResourceList
);
1901 /* Detect ISA/BIOS devices */
1902 DetectBiosDisks(SystemKey
, BusKey
);
1904 DetectSerialPorts(BusKey
);
1906 DetectParallelPorts(BusKey
);
1908 DetectKeyboardController(BusKey
);
1910 DetectPS2Mouse(BusKey
);
1912 DetectDisplayController(BusKey
);
1914 /* FIXME: Detect more ISA devices */
1918 PCONFIGURATION_COMPONENT_DATA
1921 PCONFIGURATION_COMPONENT_DATA SystemKey
;
1922 ULONG BusNumber
= 0;
1924 DPRINTM(DPRINT_HWDETECT
, "DetectHardware()\n");
1926 /* Create the 'System' key */
1927 FldrCreateSystemKey(&SystemKey
);
1930 DetectPciBios(SystemKey
, &BusNumber
);
1931 DetectApmBios(SystemKey
, &BusNumber
);
1932 DetectPnpBios(SystemKey
, &BusNumber
);
1933 DetectIsaBios(SystemKey
, &BusNumber
);
1934 DetectAcpiBios(SystemKey
, &BusNumber
);
1936 DPRINTM(DPRINT_HWDETECT
, "DetectHardware() Done\n");