1 // 16bit code to handle serial and printer services.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU GPLv3 license.
8 #include "biosvar.h" // struct bregs
9 #include "util.h" // debug_enter
12 /****************************************************************
14 ****************************************************************/
17 detect_serial(u16 port, u8 timeout, u8 count)
20 if (inb(port+1) != 0x02)
22 if (inb(port+2) != 0x02)
25 SET_BDA(port_com[count], port);
26 SET_BDA(com_timeout[count], timeout);
33 dprintf(3, "init serial\n");
35 count += detect_serial(0x3f8, 0x0a, count);
36 count += detect_serial(0x2f8, 0x0a, count);
37 count += detect_serial(0x3e8, 0x0a, count);
38 count += detect_serial(0x2e8, 0x0a, count);
40 // Equipment word bits 9..11 determing # serial ports
41 u16 eqb = GET_BDA(equipment_list_flags);
42 SET_BDA(equipment_list_flags, (eqb & 0xf1ff) | (count << 9));
46 getComAddr(struct bregs *regs)
52 u16 addr = GET_BDA(port_com[regs->dx]);
58 // SERIAL - INITIALIZE PORT
60 handle_1400(struct bregs *regs)
62 u16 addr = getComAddr(regs);
65 outb(inb(addr+3) | 0x80, addr+3);
66 if ((regs->al & 0xE0) == 0) {
70 u16 val16 = 0x600 >> ((regs->al & 0xE0) >> 5);
71 outb(val16 & 0xFF, addr);
72 outb(val16 >> 8, addr+1);
74 outb(regs->al & 0x1F, addr+3);
75 regs->ah = inb(addr+5);
76 regs->al = inb(addr+6);
80 // SERIAL - WRITE CHARACTER TO PORT
82 handle_1401(struct bregs *regs)
84 u16 addr = getComAddr(regs);
87 u16 timer = GET_BDA(timer_counter);
88 u16 timeout = GET_BDA(com_timeout[regs->dx]);
89 while (((inb(addr+5) & 0x60) != 0x60) && (timeout)) {
90 u16 val16 = GET_BDA(timer_counter);
98 regs->ah = inb(addr+5);
104 // SERIAL - READ CHARACTER FROM PORT
106 handle_1402(struct bregs *regs)
108 u16 addr = getComAddr(regs);
111 u16 timer = GET_BDA(timer_counter);
112 u16 timeout = GET_BDA(com_timeout[regs->dx]);
113 while (((inb(addr+5) & 0x01) == 0) && (timeout)) {
114 u16 val16 = GET_BDA(timer_counter);
115 if (val16 != timer) {
122 regs->al = inb(addr);
124 regs->ah = inb(addr+5);
129 // SERIAL - GET PORT STATUS
131 handle_1403(struct bregs *regs)
133 u16 addr = getComAddr(regs);
136 regs->ah = inb(addr+5);
137 regs->al = inb(addr+6);
142 handle_14XX(struct bregs *regs)
148 // INT 14h Serial Communications Service Entry Point
150 handle_14(struct bregs *regs)
157 case 0x00: handle_1400(regs); break;
158 case 0x01: handle_1401(regs); break;
159 case 0x02: handle_1402(regs); break;
160 case 0x03: handle_1403(regs); break;
161 default: handle_14XX(regs); break;
166 /****************************************************************
168 ****************************************************************/
171 detect_parport(u16 port, u8 timeout, u8 count)
174 outb(inb(port+2) & 0xdf, port+2);
177 if (inb(port) != 0xaa)
180 SET_BDA(port_lpt[count], port);
181 SET_BDA(lpt_timeout[count], timeout);
188 dprintf(3, "init lpt\n");
190 count += detect_parport(0x378, 0x14, count);
191 count += detect_parport(0x278, 0x14, count);
193 // Equipment word bits 14..15 determing # parallel ports
194 u16 eqb = GET_BDA(equipment_list_flags);
195 SET_BDA(equipment_list_flags, (eqb & 0x3fff) | (count << 14));
199 getLptAddr(struct bregs *regs)
205 u16 addr = GET_BDA(port_lpt[regs->dx]);
212 lpt_ret(struct bregs *regs, u16 addr, u16 timeout)
214 u8 val8 = inb(addr+1);
215 regs->ah = (val8 ^ 0x48);
221 // INT 17 - PRINTER - WRITE CHARACTER
223 handle_1700(struct bregs *regs)
225 u16 addr = getLptAddr(regs);
228 u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
230 outb(regs->al, addr);
231 u8 val8 = inb(addr+2);
232 outb(val8 | 0x01, addr+2); // send strobe
234 outb(val8 & ~0x01, addr+2);
235 while (((inb(addr+1) & 0x40) == 0x40) && (timeout))
238 lpt_ret(regs, addr, timeout);
241 // INT 17 - PRINTER - INITIALIZE PORT
243 handle_1701(struct bregs *regs)
245 u16 addr = getLptAddr(regs);
248 u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
250 u8 val8 = inb(addr+2);
251 outb(val8 & ~0x04, addr+2); // send init
253 outb(val8 | 0x04, addr+2);
255 lpt_ret(regs, addr, timeout);
258 // INT 17 - PRINTER - GET STATUS
260 handle_1702(struct bregs *regs)
262 u16 addr = getLptAddr(regs);
265 u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
267 lpt_ret(regs, addr, timeout);
271 handle_17XX(struct bregs *regs)
277 // INT17h : Printer Service Entry Point
279 handle_17(struct bregs *regs)
286 case 0x00: handle_1700(regs); break;
287 case 0x01: handle_1701(regs); break;
288 case 0x02: handle_1702(regs); break;
289 default: handle_17XX(regs); break;