[FREELDR]
authorHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Wed, 28 Nov 2012 22:16:15 +0000 (22:16 +0000)
committerHermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
Wed, 28 Nov 2012 22:16:15 +0000 (22:16 +0000)
- Make use of the previously committed cportlib code. This works great on VPC 2007 :)
- Comment the unused Rs232PortPollByte function.
- In hardware.c, add useful comments and two macros holding the maximum number of COM and LPT ports.

svn path=/trunk/; revision=57777

reactos/boot/freeldr/freeldr/arch/i386/hardware.c
reactos/boot/freeldr/freeldr/comm/rs232.c
reactos/boot/freeldr/freeldr/include/comm.h

index 279f1a4..fe547ec 100644 (file)
@@ -20,6 +20,7 @@
  */
 
 #include <freeldr.h>
+#include <cportlib/cportlib.h>
 
 #define NDEBUG
 #include <debug.h>
 #define LATCH (CLOCK_TICK_RATE / HZ)
 
 
+/* Maximum number of COM and LPT ports */
+#define MAX_COM_PORTS   4
+#define MAX_LPT_PORTS   3
+
 /* No Mouse */
 #define MOUSE_TYPE_NONE                        0
 /* Microsoft Mouse with 2 buttons */
@@ -1042,7 +1047,7 @@ DetectSerialPorts(PCONFIGURATION_COMPONENT_DATA BusKey)
   PCM_PARTIAL_RESOURCE_LIST PartialResourceList;
   PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor;
   PCM_SERIAL_DEVICE_DATA SerialDeviceData;
-  ULONG Irq[4] = {4, 3, 4, 3};
+  ULONG Irq[MAX_COM_PORTS] = {4, 3, 4, 3};
   ULONG Base;
   CHAR Buffer[80];
   PUSHORT BasePtr;
@@ -1053,12 +1058,17 @@ DetectSerialPorts(PCONFIGURATION_COMPONENT_DATA BusKey)
 
   TRACE("DetectSerialPorts()\n");
 
-  ControllerNumber = 0;
+  /*
+   * The BIOS data area 0x400 holds the address of the first valid COM port.
+   * Each COM port address is stored in a 2-byte field.
+   * Infos at: http://www.bioscentral.com/misc/bda.htm
+   */
   BasePtr = (PUSHORT)0x400;
-  for (i = 0; i < 2; i++, BasePtr++)
+
+  for (i = 0; i < MAX_COM_PORTS; i++, BasePtr++)
     {
       Base = (ULONG)*BasePtr;
-      if (Base == 0)
+      if (Base == 0 || !CpDoesPortExist((PUCHAR)Base))
         continue;
 
       TRACE("Found COM%u port at 0x%x\n", i + 1, Base);
@@ -1126,7 +1136,7 @@ DetectSerialPorts(PCONFIGURATION_COMPONENT_DATA BusKey)
 
       MmHeapFree(PartialResourceList);
 
-      if (!Rs232PortInUse(Base))
+      if (!Rs232PortInUse(UlongToPtr(Base)))
         {
           /* Detect serial mouse */
           DetectSerialPointerPeripheral(ControllerKey, UlongToPtr(Base));
@@ -1142,20 +1152,25 @@ DetectParallelPorts(PCONFIGURATION_COMPONENT_DATA BusKey)
 {
   PCM_PARTIAL_RESOURCE_LIST PartialResourceList;
   PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor;
-  ULONG Irq[3] = {7, 5, (ULONG)-1};
+  ULONG Irq[MAX_LPT_PORTS] = {7, 5, (ULONG)-1};
   CHAR Buffer[80];
   PCONFIGURATION_COMPONENT_DATA ControllerKey;
   PUSHORT BasePtr;
   ULONG Base;
-  ULONG ControllerNumber;
+  ULONG ControllerNumber = 0;
   ULONG i;
   ULONG Size;
 
   TRACE("DetectParallelPorts() called\n");
 
-  ControllerNumber = 0;
+  /*
+   * The BIOS data area 0x408 holds the address of the first valid LPT port.
+   * Each LPT port address is stored in a 2-byte field.
+   * Infos at: http://www.bioscentral.com/misc/bda.htm
+   */
   BasePtr = (PUSHORT)0x408;
-  for (i = 0; i < 3; i++, BasePtr++)
+
+  for (i = 0; i < MAX_LPT_PORTS; i++, BasePtr++)
     {
       Base = (ULONG)*BasePtr;
       if (Base == 0)
index 0ef92ce..ee46345 100644 (file)
@@ -22,6 +22,7 @@
 #ifndef _M_ARM
 
 #include <freeldr.h>
+#include <cportlib/cportlib.h>
 
 /* MACROS *******************************************************************/
 
 
 /* STATIC VARIABLES *********************************************************/
 
-static ULONG Rs232ComPort = 0;
-static ULONG Rs232BaudRate = 0;
-static PUCHAR Rs232PortBase = (PUCHAR)0;
+static ULONG BaseArray[] = {0, 0x3F8, 0x2F8, 0x3E8, 0x2E8};
 
-/* The com port must only be initialized once! */
+/* The COM port must only be initialized once! */
+static CPPORT Rs232ComPort;
 static BOOLEAN PortInitialized = FALSE;
 
-/* STATIC FUNCTIONS *********************************************************/
-
-static BOOLEAN Rs232DoesComPortExist(PUCHAR BaseAddress)
-{
-        BOOLEAN found;
-        UCHAR mcr;
-        UCHAR msr;
-
-        found = FALSE;
-
-        /* save Modem Control Register (MCR) */
-        mcr = READ_PORT_UCHAR (SER_MCR(BaseAddress));
-
-        /* enable loop mode (set Bit 4 of the MCR) */
-        WRITE_PORT_UCHAR (SER_MCR(BaseAddress), 0x10);
-
-        /* clear all modem output bits */
-        WRITE_PORT_UCHAR (SER_MCR(BaseAddress), 0x10);
-
-        /* read the Modem Status Register */
-        msr = READ_PORT_UCHAR (SER_MSR(BaseAddress));
-
-        /*
-         * the upper nibble of the MSR (modem output bits) must be
-         * equal to the lower nibble of the MCR (modem input bits)
-         */
-        if ((msr & 0xF0) == 0x00)
-        {
-                /* set all modem output bits */
-                WRITE_PORT_UCHAR (SER_MCR(BaseAddress), 0x1F);
-
-                /* read the Modem Status Register */
-                msr = READ_PORT_UCHAR (SER_MSR(BaseAddress));
-
-                /*
-                 * the upper nibble of the MSR (modem output bits) must be
-                 * equal to the lower nibble of the MCR (modem input bits)
-                 */
-                if ((msr & 0xF0) == 0xF0)
-                        found = TRUE;
-        }
-
-        /* restore MCR */
-        WRITE_PORT_UCHAR (SER_MCR(BaseAddress), mcr);
-
-        return (found);
-}
 
 /* FUNCTIONS *********************************************************/
 
-BOOLEAN Rs232PortInitialize(ULONG ComPort, ULONG BaudRate)
+BOOLEAN Rs232PortInitialize(IN ULONG ComPort,
+                            IN ULONG BaudRate)
 {
-        ULONG BaseArray[5] = {0, 0x3F8, 0x2F8, 0x3E8, 0x2E8};
-        //char buffer[80];
-        ULONG divisor;
-        UCHAR lcr;
+    NTSTATUS Status;
+    PUCHAR Address;
 
-        if (PortInitialized == FALSE)
+    if (PortInitialized == FALSE)
+    {
+        if (BaudRate == 0)
         {
-                if (BaudRate != 0)
-                {
-                        Rs232BaudRate = BaudRate;
-                }
-                else
-                {
-                        Rs232BaudRate = DEFAULT_BAUD_RATE;
-                }
-
-                if (ComPort == 0)
-                {
-                        if (Rs232DoesComPortExist ((PUCHAR)(ULONG_PTR)BaseArray[2]))
-                        {
-                                Rs232PortBase = (PUCHAR)(ULONG_PTR)BaseArray[2];
-                                Rs232ComPort = 2;
-/*#ifndef NDEBUG
-                                sprintf (buffer,
-                                         "\nSerial port COM%ld found at 0x%lx\n",
-                                         ComPort,
-                                         (ULONG)PortBase);
-                                HalDisplayString (buffer);
-#endif*/ /* NDEBUG */
-                        }
-                        else if (Rs232DoesComPortExist ((PUCHAR)(ULONG_PTR)BaseArray[1]))
-                        {
-                                Rs232PortBase = (PUCHAR)(ULONG_PTR)BaseArray[1];
-                                Rs232ComPort = 1;
-/*#ifndef NDEBUG
-                                sprintf (buffer,
-                                         "\nSerial port COM%ld found at 0x%lx\n",
-                                         ComPort,
-                                         (ULONG)PortBase);
-                                HalDisplayString (buffer);
-#endif*/ /* NDEBUG */
-                        }
-                        else
-                        {
-                                /*sprintf (buffer,
-                                         "\nKernel Debugger: No COM port found!!!\n\n");
-                                HalDisplayString (buffer);*/
-                                return FALSE;
-                        }
-                }
-                else
-                {
-                        if (Rs232DoesComPortExist ((PUCHAR)(ULONG_PTR)BaseArray[ComPort]))
-                        {
-                                Rs232PortBase = (PUCHAR)(ULONG_PTR)BaseArray[ComPort];
-                                Rs232ComPort = ComPort;
-/*#ifndef NDEBUG
-                                sprintf (buffer,
-                                         "\nSerial port COM%ld found at 0x%lx\n",
-                                         ComPort,
-                                         (ULONG)PortBase);
-                                HalDisplayString (buffer);
-#endif*/ /* NDEBUG */
-                        }
-                        else
-                        {
-                                /*sprintf (buffer,
-                                         "\nKernel Debugger: No serial port found!!!\n\n");
-                                HalDisplayString (buffer);*/
-                                return FALSE;
-                        }
-                }
-
-                PortInitialized = TRUE;
+            BaudRate = DEFAULT_BAUD_RATE;
         }
 
-        /*
-         * set baud rate and data format (8N1)
-         */
-
-        /*  turn on DTR and RTS  */
-        WRITE_PORT_UCHAR (SER_MCR(Rs232PortBase), SR_MCR_DTR | SR_MCR_RTS);
-
-        /* set DLAB */
-        lcr = READ_PORT_UCHAR (SER_LCR(Rs232PortBase)) | SR_LCR_DLAB;
-        WRITE_PORT_UCHAR (SER_LCR(Rs232PortBase), lcr);
-
-        /* set baud rate */
-        divisor = 115200 / BaudRate;
-        WRITE_PORT_UCHAR (SER_DLL(Rs232PortBase), divisor & 0xff);
-        WRITE_PORT_UCHAR (SER_DLM(Rs232PortBase), (divisor >> 8) & 0xff);
-
-        /* reset DLAB and set 8N1 format */
-        WRITE_PORT_UCHAR (SER_LCR(Rs232PortBase),
-                          SR_LCR_CS8 | SR_LCR_ST1 | SR_LCR_PNO);
-
-        /* read junk out of the RBR */
-        lcr = READ_PORT_UCHAR (SER_RBR(Rs232PortBase));
-
-        /*
-         * set global info
-         */
-        //KdComPortInUse = (ULONG)PortBase;
+        if (ComPort == 0)
+        {
+            if (CpDoesPortExist(UlongToPtr(BaseArray[2])))
+            {
+                Address = UlongToPtr(BaseArray[2]);
+            }
+            else if (CpDoesPortExist(UlongToPtr(BaseArray[1])))
+            {
+                Address = UlongToPtr(BaseArray[1]);
+            }
+            else
+            {
+                return FALSE;
+            }
+        }
+        else if (ComPort <= 4) // 4 == MAX_COM_PORTS
+        {
+            if (CpDoesPortExist(UlongToPtr(BaseArray[ComPort])))
+            {
+                Address = UlongToPtr(BaseArray[ComPort]);
+            }
+            else
+            {
+                return FALSE;
+            }
+        }
+        else
+        {
+            return FALSE;
+        }
 
-        /*
-         * print message to blue screen
-         */
-        /*sprintf (buffer,
-                 "\nKernel Debugger: COM%ld (Port 0x%lx) BaudRate %ld\n\n",
-                 ComPort,
-                 (ULONG)PortBase,
-                 BaudRate);
+        Status = CpInitialize(&Rs232ComPort, Address, BaudRate);
+        if (!NT_SUCCESS(Status)) return FALSE;
 
-        HalDisplayString (buffer);*/
+        PortInitialized = TRUE;
+    }
 
-        return TRUE;
+    return TRUE;
 }
 
-BOOLEAN Rs232PortGetByte(PUCHAR ByteRecieved)
+BOOLEAN Rs232PortGetByte(PUCHAR ByteReceived)
 {
-       if (PortInitialized == FALSE)
-               return FALSE;
-
-       if ((READ_PORT_UCHAR (SER_LSR(Rs232PortBase)) & SR_LSR_DR))
-       {
-               *ByteRecieved = READ_PORT_UCHAR (SER_RBR(Rs232PortBase));
-               return TRUE;
-       }
+    if (PortInitialized == FALSE)
+        return FALSE;
 
-       return FALSE;
+    return (CpGetByte(&Rs232ComPort, ByteReceived, FALSE, FALSE) == CP_GET_SUCCESS);
 }
 
-BOOLEAN Rs232PortPollByte(PUCHAR ByteRecieved)
+/*
+BOOLEAN Rs232PortPollByte(PUCHAR ByteReceived)
 {
-       if (PortInitialized == FALSE)
-               return FALSE;
+    if (PortInitialized == FALSE)
+        return FALSE;
 
-       while ((READ_PORT_UCHAR (SER_LSR(Rs232PortBase)) & SR_LSR_DR) == 0)
-               ;
+    while ((READ_PORT_UCHAR (SER_LSR(Rs232PortBase)) & SR_LSR_DR) == 0)
+        ;
 
-       *ByteRecieved = READ_PORT_UCHAR (SER_RBR(Rs232PortBase));
+    *ByteReceived = READ_PORT_UCHAR (SER_RBR(Rs232PortBase));
 
-       return TRUE;
+    return TRUE;
 }
+*/
 
 VOID Rs232PortPutByte(UCHAR ByteToSend)
 {
-       if (PortInitialized == FALSE)
-               return;
-
-       while ((READ_PORT_UCHAR (SER_LSR(Rs232PortBase)) & SR_LSR_TBE) == 0)
-               ;
+    if (PortInitialized == FALSE)
+        return;
 
-       WRITE_PORT_UCHAR (SER_THR(Rs232PortBase), ByteToSend);
+    CpPutByte(&Rs232ComPort, ByteToSend);
 }
 
 #endif /* DBG */
 
-BOOLEAN Rs232PortInUse(ULONG Base)
+BOOLEAN Rs232PortInUse(PUCHAR Base)
 {
 #if DBG
-    return PortInitialized && Rs232PortBase == (PUCHAR)(ULONG_PTR)Base ? TRUE : FALSE;
+    return ( (PortInitialized && (Rs232ComPort.Address == Base)) ? TRUE : FALSE );
 #else
     return FALSE;
 #endif
index 511b705..49d0943 100644 (file)
@@ -23,6 +23,6 @@
 
 BOOLEAN Rs232PortInitialize(ULONG ComPort, ULONG BaudRate);
 BOOLEAN Rs232PortGetByte(PUCHAR ByteRecieved);
-BOOLEAN Rs232PortPollByte(PUCHAR ByteRecieved);
+// BOOLEAN Rs232PortPollByte(PUCHAR ByteRecieved);
 VOID Rs232PortPutByte(UCHAR ByteToSend);
-BOOLEAN Rs232PortInUse(ULONG Base);
+BOOLEAN Rs232PortInUse(PUCHAR Base);