1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
// 16bit code to handle serial and printer services.
//
// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
// Copyright (C) 2002 MandrakeSoft S.A.
//
// This file may be distributed under the terms of the GNU LGPLv3 license.
#include "biosvar.h" // SET_BDA
#include "util.h" // debug_enter
#include "bregs.h" // struct bregs
/****************************************************************
* COM ports
****************************************************************/
static u16
detect_serial(u16 port, u8 timeout, u8 count)
{
outb(0x02, port+1);
if (inb(port+1) != 0x02)
return 0;
if (inb(port+2) != 0x02)
return 0;
outb(0x00, port+1);
SET_BDA(port_com[count], port);
SET_BDA(com_timeout[count], timeout);
return 1;
}
void
serial_setup()
{
if (! CONFIG_SERIAL)
return;
dprintf(3, "init serial\n");
u16 count = 0;
count += detect_serial(0x3f8, 0x0a, count);
count += detect_serial(0x2f8, 0x0a, count);
count += detect_serial(0x3e8, 0x0a, count);
count += detect_serial(0x2e8, 0x0a, count);
// Equipment word bits 9..11 determing # serial ports
u16 eqb = GET_BDA(equipment_list_flags);
SET_BDA(equipment_list_flags, (eqb & 0xf1ff) | (count << 9));
}
static u16
getComAddr(struct bregs *regs)
{
if (regs->dx >= 4) {
set_fail(regs);
return 0;
}
u16 addr = GET_BDA(port_com[regs->dx]);
if (! addr)
set_fail(regs);
return addr;
}
// SERIAL - INITIALIZE PORT
static void
handle_1400(struct bregs *regs)
{
u16 addr = getComAddr(regs);
if (!addr)
return;
outb(inb(addr+3) | 0x80, addr+3);
if ((regs->al & 0xE0) == 0) {
outb(0x17, addr);
outb(0x04, addr+1);
} else {
u16 val16 = 0x600 >> ((regs->al & 0xE0) >> 5);
outb(val16 & 0xFF, addr);
outb(val16 >> 8, addr+1);
}
outb(regs->al & 0x1F, addr+3);
regs->ah = inb(addr+5);
regs->al = inb(addr+6);
set_success(regs);
}
// SERIAL - WRITE CHARACTER TO PORT
static void
handle_1401(struct bregs *regs)
{
u16 addr = getComAddr(regs);
if (!addr)
return;
u16 timer = GET_BDA(timer_counter);
u16 timeout = GET_BDA(com_timeout[regs->dx]);
while (((inb(addr+5) & 0x60) != 0x60) && (timeout)) {
u16 val16 = GET_BDA(timer_counter);
if (val16 != timer) {
timer = val16;
timeout--;
}
}
if (timeout)
outb(regs->al, addr);
regs->ah = inb(addr+5);
if (!timeout)
regs->ah |= 0x80;
set_success(regs);
}
// SERIAL - READ CHARACTER FROM PORT
static void
handle_1402(struct bregs *regs)
{
u16 addr = getComAddr(regs);
if (!addr)
return;
u16 timer = GET_BDA(timer_counter);
u16 timeout = GET_BDA(com_timeout[regs->dx]);
while (((inb(addr+5) & 0x01) == 0) && (timeout)) {
u16 val16 = GET_BDA(timer_counter);
if (val16 != timer) {
timer = val16;
timeout--;
}
}
if (timeout) {
regs->ah = 0;
regs->al = inb(addr);
} else {
regs->ah = inb(addr+5);
}
set_success(regs);
}
// SERIAL - GET PORT STATUS
static void
handle_1403(struct bregs *regs)
{
u16 addr = getComAddr(regs);
if (!addr)
return;
regs->ah = inb(addr+5);
regs->al = inb(addr+6);
set_success(regs);
}
static void
handle_14XX(struct bregs *regs)
{
// Unsupported
set_fail(regs);
}
// INT 14h Serial Communications Service Entry Point
void VISIBLE16
handle_14(struct bregs *regs)
{
debug_enter(regs, DEBUG_HDL_14);
if (! CONFIG_SERIAL) {
handle_14XX(regs);
return;
}
irq_enable();
switch (regs->ah) {
case 0x00: handle_1400(regs); break;
case 0x01: handle_1401(regs); break;
case 0x02: handle_1402(regs); break;
case 0x03: handle_1403(regs); break;
default: handle_14XX(regs); break;
}
}
// XXX - Baud Rate Generator Table
u8 BaudTable[16] VAR16FIXED(0xe729);
/****************************************************************
* LPT ports
****************************************************************/
static u16
detect_parport(u16 port, u8 timeout, u8 count)
{
// clear input mode
outb(inb(port+2) & 0xdf, port+2);
outb(0xaa, port);
if (inb(port) != 0xaa)
// Not present
return 0;
SET_BDA(port_lpt[count], port);
SET_BDA(lpt_timeout[count], timeout);
return 1;
}
void
lpt_setup()
{
if (! CONFIG_LPT)
return;
dprintf(3, "init lpt\n");
u16 count = 0;
count += detect_parport(0x378, 0x14, count);
count += detect_parport(0x278, 0x14, count);
// Equipment word bits 14..15 determing # parallel ports
u16 eqb = GET_BDA(equipment_list_flags);
SET_BDA(equipment_list_flags, (eqb & 0x3fff) | (count << 14));
}
static u16
getLptAddr(struct bregs *regs)
{
if (regs->dx >= 3) {
set_fail(regs);
return 0;
}
u16 addr = GET_BDA(port_lpt[regs->dx]);
if (! addr)
set_fail(regs);
return addr;
}
static void
lpt_ret(struct bregs *regs, u16 addr, u16 timeout)
{
u8 val8 = inb(addr+1);
regs->ah = (val8 ^ 0x48);
if (!timeout)
regs->ah |= 0x01;
set_success(regs);
}
// INT 17 - PRINTER - WRITE CHARACTER
static void
handle_1700(struct bregs *regs)
{
u16 addr = getLptAddr(regs);
if (!addr)
return;
u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
outb(regs->al, addr);
u8 val8 = inb(addr+2);
outb(val8 | 0x01, addr+2); // send strobe
nop();
outb(val8 & ~0x01, addr+2);
// XXX - implement better timeout code.
while (((inb(addr+1) & 0x40) == 0x40) && (timeout))
timeout--;
lpt_ret(regs, addr, timeout);
}
// INT 17 - PRINTER - INITIALIZE PORT
static void
handle_1701(struct bregs *regs)
{
u16 addr = getLptAddr(regs);
if (!addr)
return;
u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
u8 val8 = inb(addr+2);
outb(val8 & ~0x04, addr+2); // send init
nop();
outb(val8 | 0x04, addr+2);
lpt_ret(regs, addr, timeout);
}
// INT 17 - PRINTER - GET STATUS
static void
handle_1702(struct bregs *regs)
{
u16 addr = getLptAddr(regs);
if (!addr)
return;
u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
lpt_ret(regs, addr, timeout);
}
static void
handle_17XX(struct bregs *regs)
{
// Unsupported
set_fail(regs);
}
// INT17h : Printer Service Entry Point
void VISIBLE16
handle_17(struct bregs *regs)
{
debug_enter(regs, DEBUG_HDL_17);
if (! CONFIG_LPT) {
handle_17XX(regs);
return;
}
irq_enable();
switch (regs->ah) {
case 0x00: handle_1700(regs); break;
case 0x01: handle_1701(regs); break;
case 0x02: handle_1702(regs); break;
default: handle_17XX(regs); break;
}
}
|