[EXT2] Upgrade to 0.69
[reactos.git] / boot / freeldr / freeldr / lib / comm / rs232.c
1 /*
2 * FreeLoader
3 * Copyright (C) 2001 Brian Palmer <brianp@sginet.com>
4 * Copyright (C) 2001 Eric Kohl
5 * Copyright (C) 2001 Emanuele Aliberti
6 *
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.
11 *
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.
16 *
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.
20 */
21
22 #ifndef _M_ARM
23
24 /* INCLUDES *******************************************************************/
25
26 #include <freeldr.h>
27 #include <cportlib/cportlib.h>
28
29 #if DBG
30
31 /* STATIC VARIABLES ***********************************************************/
32
33 #define DEFAULT_BAUD_RATE 19200
34
35 #if defined(_M_IX86) || defined(_M_AMD64)
36 static const ULONG BaseArray[] = {0, 0x3F8, 0x2F8, 0x3E8, 0x2E8};
37 #elif defined(_M_PPC)
38 static const ULONG BaseArray[] = {0, 0x800003F8};
39 #elif defined(_M_MIPS)
40 static const ULONG BaseArray[] = {0, 0x80006000, 0x80007000};
41 #elif defined(_M_ARM)
42 static const ULONG BaseArray[] = {0, 0xF1012000};
43 #else
44 #error Unknown architecture
45 #endif
46
47 #define MAX_COM_PORTS (sizeof(BaseArray) / sizeof(BaseArray[0]) - 1)
48
49 /* The COM port must only be initialized once! */
50 static ULONG Rs232ComPort = 0;
51 static CPPORT Rs232ComPortInfo;
52
53 /* FUNCTIONS ******************************************************************/
54
55 BOOLEAN Rs232PortInitialize(IN ULONG ComPort,
56 IN ULONG BaudRate)
57 {
58 NTSTATUS Status;
59 PUCHAR Address;
60
61 /*
62 * Check whether it's the first time we initialize a COM port.
63 * If not, check whether the specified one was already initialized.
64 */
65 if ((Rs232ComPort != 0) && (Rs232ComPort == ComPort))
66 return TRUE;
67
68 if (BaudRate == 0)
69 BaudRate = DEFAULT_BAUD_RATE;
70
71 if (ComPort == 0)
72 {
73 /*
74 * Start enumerating COM ports from the last one to the first one,
75 * and break when we find a valid port.
76 * If we reach the first element of the list, the invalid COM port,
77 * then it means that no valid port was found.
78 */
79 for (ComPort = MAX_COM_PORTS; ComPort > 0; ComPort--)
80 {
81 if (CpDoesPortExist(UlongToPtr(BaseArray[ComPort])))
82 {
83 Address = UlongToPtr(BaseArray[ComPort]);
84 break;
85 }
86 }
87 if (ComPort == 0)
88 return FALSE;
89 }
90 else if (ComPort <= MAX_COM_PORTS)
91 {
92 if (CpDoesPortExist(UlongToPtr(BaseArray[ComPort])))
93 Address = UlongToPtr(BaseArray[ComPort]);
94 else
95 return FALSE;
96 }
97 else
98 {
99 return FALSE;
100 }
101
102 Status = CpInitialize(&Rs232ComPortInfo, Address, BaudRate);
103 if (!NT_SUCCESS(Status)) return FALSE;
104
105 Rs232ComPort = ComPort;
106
107 return TRUE;
108 }
109
110 BOOLEAN Rs232PortGetByte(PUCHAR ByteReceived)
111 {
112 if (Rs232ComPort == 0) return FALSE;
113 return (CpGetByte(&Rs232ComPortInfo, ByteReceived, TRUE, FALSE) == CP_GET_SUCCESS);
114 }
115
116 /*
117 BOOLEAN Rs232PortPollByte(PUCHAR ByteReceived)
118 {
119 if (Rs232ComPort == 0) return FALSE;
120 return (CpGetByte(&Rs232ComPortInfo, ByteReceived, FALSE, FALSE) == CP_GET_SUCCESS);
121 }
122 */
123
124 VOID Rs232PortPutByte(UCHAR ByteToSend)
125 {
126 if (Rs232ComPort == 0) return;
127 CpPutByte(&Rs232ComPortInfo, ByteToSend);
128 }
129
130 #endif /* DBG */
131
132 BOOLEAN Rs232PortInUse(PUCHAR Base)
133 {
134 #if DBG
135 return ( ((Rs232ComPort != 0) && (Rs232ComPortInfo.Address == Base)) ? TRUE : FALSE );
136 #else
137 return FALSE;
138 #endif
139 }
140
141 #endif /* not _M_ARM */