⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hardware.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 5 页
字号:
  ULONG i;
  LONG Error;
  ULONG Size;

  DbgPrint((DPRINT_HWDETECT, "DetectSerialPorts()\n"));

  ControllerNumber = 0;
  BasePtr = (PUSHORT)0x400;
  for (i = 0; i < 4; i++, BasePtr++)
    {
      Base = (ULONG)*BasePtr;
      if (Base == 0)
        continue;

      DbgPrint((DPRINT_HWDETECT,
		"Found COM%u port at 0x%x\n",
		i + 1,
		Base));

      /* Create controller key */
      swprintf(Buffer,
	      L"SerialController\\%u",
	      ControllerNumber);

      Error = RegCreateKey(BusKey,
			   Buffer,
			   &ControllerKey);
      if (Error != ERROR_SUCCESS)
	{
	  DbgPrint((DPRINT_HWDETECT, "Failed to create controller key\n"));
	  continue;
	}
      DbgPrint((DPRINT_HWDETECT, "Created key: %S\n", Buffer));

      /* Set 'ComponentInformation' value */
      SetComponentInformation(ControllerKey,
			      0x78,
			      ControllerNumber,
			      0xFFFFFFFF);

      /* Build full device descriptor */
      Size = sizeof(CM_FULL_RESOURCE_DESCRIPTOR) +
	     2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) +
	     sizeof(CM_SERIAL_DEVICE_DATA);
      FullResourceDescriptor = MmAllocateMemory(Size);
      if (FullResourceDescriptor == NULL)
	{
	  DbgPrint((DPRINT_HWDETECT,
		    "Failed to allocate resource descriptor\n"));
	  continue;
	}
      memset(FullResourceDescriptor, 0, Size);

      /* Initialize resource descriptor */
      FullResourceDescriptor->InterfaceType = Isa;
      FullResourceDescriptor->BusNumber = 0;
      FullResourceDescriptor->PartialResourceList.Version = 1;
      FullResourceDescriptor->PartialResourceList.Revision = 1;
      FullResourceDescriptor->PartialResourceList.Count = 3;

      /* Set IO Port */
      PartialDescriptor = &FullResourceDescriptor->PartialResourceList.PartialDescriptors[0];
      PartialDescriptor->Type = CmResourceTypePort;
      PartialDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
      PartialDescriptor->Flags = CM_RESOURCE_PORT_IO;
      PartialDescriptor->u.Port.Start.LowPart = Base;
      PartialDescriptor->u.Port.Start.HighPart = 0x0;
      PartialDescriptor->u.Port.Length = 7;

      /* Set Interrupt */
      PartialDescriptor = &FullResourceDescriptor->PartialResourceList.PartialDescriptors[1];
      PartialDescriptor->Type = CmResourceTypeInterrupt;
      PartialDescriptor->ShareDisposition = CmResourceShareUndetermined;
      PartialDescriptor->Flags = CM_RESOURCE_INTERRUPT_LATCHED;
      PartialDescriptor->u.Interrupt.Level = Irq[i];
      PartialDescriptor->u.Interrupt.Vector = 0;
      PartialDescriptor->u.Interrupt.Affinity = 0xFFFFFFFF;

      /* Set serial data (device specific) */
      PartialDescriptor = &FullResourceDescriptor->PartialResourceList.PartialDescriptors[2];
      PartialDescriptor->Type = CmResourceTypeDeviceSpecific;
      PartialDescriptor->ShareDisposition = CmResourceShareUndetermined;
      PartialDescriptor->Flags = 0;
      PartialDescriptor->u.DeviceSpecificData.DataSize = sizeof(CM_SERIAL_DEVICE_DATA);

      SerialDeviceData =
	(PCM_SERIAL_DEVICE_DATA)&FullResourceDescriptor->PartialResourceList.PartialDescriptors[3];
      SerialDeviceData->BaudClock = 1843200; /* UART Clock frequency (Hertz) */

      /* Set 'Configuration Data' value */
      Error = RegSetValue(ControllerKey,
			  L"Configuration Data",
			  REG_FULL_RESOURCE_DESCRIPTOR,
			  (PCHAR) FullResourceDescriptor,
			  Size);
      MmFreeMemory(FullResourceDescriptor);
      if (Error != ERROR_SUCCESS)
	{
	  DbgPrint((DPRINT_HWDETECT,
		    "RegSetValue(Configuration Data) failed (Error %u)\n",
		    (int)Error));
	}

      /* Set 'Identifier' value */
      swprintf(Buffer,
	       L"COM%u",
	       i + 1);
      Error = RegSetValue(ControllerKey,
			  L"Identifier",
			  REG_SZ,
			  (PCHAR)Buffer,
			  (wcslen(Buffer) + 1) * sizeof(WCHAR));
      if (Error != ERROR_SUCCESS)
	{
	  DbgPrint((DPRINT_HWDETECT,
		    "RegSetValue() failed (Error %u)\n",
		    (int)Error));
	  continue;
	}
      DbgPrint((DPRINT_HWDETECT,
		"Created value: Identifier %s\n",
		Buffer));

      if (!Rs232PortInUse(Base))
        {
          /* Detect serial mouse */
          DetectSerialPointerPeripheral(ControllerKey, Base);
        }

      ControllerNumber++;
    }
}


static VOID
DetectParallelPorts(FRLDRHKEY BusKey)
{
  PCM_FULL_RESOURCE_DESCRIPTOR FullResourceDescriptor;
  PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor;
  ULONG Irq[3] = {7, 5, (ULONG)-1};
  WCHAR Buffer[80];
  FRLDRHKEY ControllerKey;
  PUSHORT BasePtr;
  ULONG Base;
  ULONG ControllerNumber;
  ULONG i;
  LONG Error;
  ULONG Size;

  DbgPrint((DPRINT_HWDETECT, "DetectParallelPorts() called\n"));

  ControllerNumber = 0;
  BasePtr = (PUSHORT)0x408;
  for (i = 0; i < 3; i++, BasePtr++)
    {
      Base = (ULONG)*BasePtr;
      if (Base == 0)
        continue;

      DbgPrint((DPRINT_HWDETECT,
		"Parallel port %u: %x\n",
		ControllerNumber,
		Base));

      /* Create controller key */
      swprintf(Buffer,
	       L"ParallelController\\%u",
	       ControllerNumber);

      Error = RegCreateKey(BusKey,
			   Buffer,
			   &ControllerKey);
      if (Error != ERROR_SUCCESS)
	{
	  DbgPrint((DPRINT_HWDETECT, "Failed to create controller key\n"));
	  continue;
	}
      DbgPrint((DPRINT_HWDETECT, "Created key: %S\n", Buffer));

      /* Set 'ComponentInformation' value */
      SetComponentInformation(ControllerKey,
			      0x40,
			      ControllerNumber,
			      0xFFFFFFFF);

      /* Build full device descriptor */
      Size = sizeof(CM_FULL_RESOURCE_DESCRIPTOR);
      if (Irq[i] != (ULONG)-1)
	Size += sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR);

      FullResourceDescriptor = MmAllocateMemory(Size);
      if (FullResourceDescriptor == NULL)
	{
	  DbgPrint((DPRINT_HWDETECT,
		    "Failed to allocate resource descriptor\n"));
	  continue;
	}
      memset(FullResourceDescriptor, 0, Size);

      /* Initialize resource descriptor */
      FullResourceDescriptor->InterfaceType = Isa;
      FullResourceDescriptor->BusNumber = 0;
      FullResourceDescriptor->PartialResourceList.Version = 1;
      FullResourceDescriptor->PartialResourceList.Revision = 1;
      FullResourceDescriptor->PartialResourceList.Count = (Irq[i] != (ULONG)-1) ? 2 : 1;

      /* Set IO Port */
      PartialDescriptor = &FullResourceDescriptor->PartialResourceList.PartialDescriptors[0];
      PartialDescriptor->Type = CmResourceTypePort;
      PartialDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
      PartialDescriptor->Flags = CM_RESOURCE_PORT_IO;
      PartialDescriptor->u.Port.Start.LowPart = Base;
      PartialDescriptor->u.Port.Start.HighPart = 0x0;
      PartialDescriptor->u.Port.Length = 3;

      /* Set Interrupt */
      if (Irq[i] != (ULONG)-1)
	{
	  PartialDescriptor = &FullResourceDescriptor->PartialResourceList.PartialDescriptors[1];
	  PartialDescriptor->Type = CmResourceTypeInterrupt;
	  PartialDescriptor->ShareDisposition = CmResourceShareUndetermined;
	  PartialDescriptor->Flags = CM_RESOURCE_INTERRUPT_LATCHED;
	  PartialDescriptor->u.Interrupt.Level = Irq[i];
	  PartialDescriptor->u.Interrupt.Vector = 0;
	  PartialDescriptor->u.Interrupt.Affinity = 0xFFFFFFFF;
	}

      /* Set 'Configuration Data' value */
      Error = RegSetValue(ControllerKey,
			  L"Configuration Data",
			  REG_FULL_RESOURCE_DESCRIPTOR,
			  (PCHAR) FullResourceDescriptor,
			  Size);
      MmFreeMemory(FullResourceDescriptor);
      if (Error != ERROR_SUCCESS)
	{
	  DbgPrint((DPRINT_HWDETECT,
		    "RegSetValue(Configuration Data) failed (Error %u)\n",
		    (int)Error));
	}

      /* Set 'Identifier' value */
      swprintf(Buffer,
	      L"PARALLEL%u",
	      i + 1);
      Error = RegSetValue(ControllerKey,
			  L"Identifier",
			  REG_SZ,
			  (PCHAR)Buffer,
			  (wcslen(Buffer) + 1) * sizeof(WCHAR));
      if (Error != ERROR_SUCCESS)
	{
	  DbgPrint((DPRINT_HWDETECT,
		    "RegSetValue() failed (Error %u)\n",
		    (int)Error));
	  continue;
	}
      DbgPrint((DPRINT_HWDETECT,
		"Created value: Identifier %s\n",
		Buffer));

      ControllerNumber++;
    }

  DbgPrint((DPRINT_HWDETECT, "DetectParallelPorts() done\n"));
}


static BOOLEAN
DetectKeyboardDevice(VOID)
{
  UCHAR Status;
  UCHAR Scancode;
  ULONG Loops;
  BOOLEAN Result = TRUE;

  /* Identify device */
  WRITE_PORT_UCHAR((PUCHAR)CONTROLLER_REGISTER_DATA,
		   0xF2);

  /* Wait for reply */
  for (Loops = 0; Loops < 100; Loops++)
    {
      StallExecutionProcessor(10000);
      Status = READ_PORT_UCHAR((PUCHAR)CONTROLLER_REGISTER_STATUS);
      if ((Status & CONTROLLER_STATUS_OUTPUT_BUFFER_FULL) != 0)
        break;
    }

  if ((Status & CONTROLLER_STATUS_OUTPUT_BUFFER_FULL) == 0)
    {
      /* PC/XT keyboard or no keyboard */
      Result = FALSE;
    }

  Scancode = READ_PORT_UCHAR((PUCHAR)CONTROLLER_REGISTER_DATA);
  if (Scancode != 0xFA)
    {
      /* No ACK received */
      Result = FALSE;
    }

  StallExecutionProcessor(10000);

  Status = READ_PORT_UCHAR((PUCHAR)CONTROLLER_REGISTER_STATUS);
  if ((Status & CONTROLLER_STATUS_OUTPUT_BUFFER_FULL) == 0)
    {
      /* Found AT keyboard */
      return Result;
    }

  Scancode = READ_PORT_UCHAR((PUCHAR)CONTROLLER_REGISTER_DATA);
  if (Scancode != 0xAB)
    {
      /* No 0xAB received */
      Result = FALSE;
    }

  StallExecutionProcessor(10000);

  Status = READ_PORT_UCHAR((PUCHAR)CONTROLLER_REGISTER_STATUS);
  if ((Status & CONTROLLER_STATUS_OUTPUT_BUFFER_FULL) == 0)
    {
      /* No byte in buffer */
      Result = FALSE;
    }

  Scancode = READ_PORT_UCHAR((PUCHAR)CONTROLLER_REGISTER_DATA);
  if (Scancode != 0x41)
    {
      /* No 0x41 received */
      Result = FALSE;
    }

  /* Found MF-II keyboard */
  return Result;
}


static VOID
DetectKeyboardPeripheral(FRLDRHKEY ControllerKey)
{
  PCM_FULL_RESOURCE_DESCRIPTOR FullResourceDescriptor;
  PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor;
  PCM_KEYBOARD_DEVICE_DATA KeyboardData;
  FRLDRHKEY PeripheralKey;
  ULONG Size;
  LONG Error;

  /* HACK: don't call DetectKeyboardDevice() as it fails in Qemu 0.8.2 */
  if (TRUE || DetectKeyboardDevice())
  {
    /* Create controller key */
    Error = RegCreateKey(ControllerKey,
			 L"KeyboardPeripheral\\0",
			 &PeripheralKey);
    if (Error != ERROR_SUCCESS)
    {
      DbgPrint((DPRINT_HWDETECT, "Failed to create peripheral key\n"));
      return;
    }
    DbgPrint((DPRINT_HWDETECT, "Created key: KeyboardPeripheral\\0\n"));

    /* Set 'ComponentInformation' value */
    SetComponentInformation(ControllerKey,
			    0x28,
			    0,
			    0xFFFFFFFF);

    /* Set 'Configuration Data' value */
    Size = sizeof(CM_FULL_RESOURCE_DESCRIPTOR) +
	   sizeof(CM_KEYBOARD_DEVICE_DATA);
    FullResourceDescriptor = MmAllocateMemory(Size);
    if (FullResourceDescriptor == NULL)
    {
      DbgPrint((DPRINT_HWDETECT,
		"Failed to allocate resource descriptor\n"));
      return;
    }

    /* Initialize resource descriptor */
    memset(FullResourceDescriptor, 0, Size);
    FullResourceDescriptor->InterfaceType = Isa;
    FullResourceDescriptor->BusNumber = 0;
    FullResourceDescriptor->PartialResourceList.Version = 1;
    FullResourceDescriptor->PartialResourceList.Revision = 1;
    FullResourceDescriptor->PartialResourceList.Count = 1;

    PartialDescriptor = &FullResourceDescriptor->PartialResourceList.PartialDescriptors[0];
    PartialDescriptor->Type = CmResourceTypeDeviceSpecific;
    PartialDescriptor->ShareDisposition = CmResourceShareUndetermined;
    PartialDescriptor->u.DeviceSpecificData.DataSize = sizeof(CM_KEYBOARD_DEVICE_DATA);

    KeyboardData = (PCM_KEYBOARD_DEVICE_DATA)(PartialDescriptor + 1);
    KeyboardData->Version = 1;
    KeyboardData->Revision = 1;
    KeyboardData->Type = 4;
    KeyboardData->Subtype = 0;
    KeyboardData->KeyboardFlags = 0x20;

    /* Set 'Configuration Data' value */
    Error = RegSetValue(PeripheralKey,
			L"Configuration Data",
			REG_FULL_RESOURCE_DESCRIPTOR,
			(PCHAR)FullResourceDescriptor,
			Size);
    MmFreeMemory(FullResourceDescriptor);
    if (Error != ERROR_SUCCESS)
    {
      DbgPrint((DPRINT_HWDETECT,
		"RegSetValue(Configuration Data) failed (Error %u)\n",
		(int)Error));
    }

    /* Set 'Identifier' value */
    Error = RegSetValue(PeripheralKey,
			L"Identifier",
			REG_SZ,
			(PCHAR)L"PCAT_ENHANCED",
			14 * sizeof(WCHAR));
    if (Error != ERROR_SUCCESS)
    {
      DbgPrint((DPRINT_HWDETECT,
		"RegSetValue() failed (Error %u)\n",
		(int)Error));
    }
  }
}


static VOID
DetectKeyboardController(FRLDRHKEY BusKey)
{
  PCM_FULL_RESOURCE_DESCRIPTOR FullResourceDescriptor;
  PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor;
  FRLDRHKEY ControllerKey;
  ULONG Size;
  LONG Error;

  /* Create controller key */
  Error = RegCreateKey(BusKey,
		       L"KeyboardController\\0",
		       &ControllerKey);
  if (Error != ERROR_SUCCESS)
    {
      DbgPrint((DPRINT_HWDETECT, "Failed to create controller key\n"));
      return;
    }
  DbgPrint((DPRINT_HWDETECT, "Created key: KeyboardController\\0\n"));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -