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" // SET_BDA
9 #include "util.h" // debug_enter
10 #include "bregs.h" // struct bregs
13 /****************************************************************
15 ****************************************************************/
18 detect_serial(u16 port, u8 timeout, u8 count)
21 if (inb(port+1) != 0x02)
23 if (inb(port+2) != 0x02)
26 SET_BDA(port_com[count], port);
27 SET_BDA(com_timeout[count], timeout);
36 dprintf(3, "init serial\n");
39 count += detect_serial(0x3f8, 0x0a, count);
40 count += detect_serial(0x2f8, 0x0a, count);
41 count += detect_serial(0x3e8, 0x0a, count);
42 count += detect_serial(0x2e8, 0x0a, count);
44 // Equipment word bits 9..11 determing # serial ports
45 u16 eqb = GET_BDA(equipment_list_flags);
46 SET_BDA(equipment_list_flags, (eqb & 0xf1ff) | (count << 9));
50 getComAddr(struct bregs *regs)
56 u16 addr = GET_BDA(port_com[regs->dx]);
62 // SERIAL - INITIALIZE PORT
64 handle_1400(struct bregs *regs)
66 u16 addr = getComAddr(regs);
69 outb(inb(addr+3) | 0x80, addr+3);
70 if ((regs->al & 0xE0) == 0) {
74 u16 val16 = 0x600 >> ((regs->al & 0xE0) >> 5);
75 outb(val16 & 0xFF, addr);
76 outb(val16 >> 8, addr+1);
78 outb(regs->al & 0x1F, addr+3);
79 regs->ah = inb(addr+5);
80 regs->al = inb(addr+6);
84 // SERIAL - WRITE CHARACTER TO PORT
86 handle_1401(struct bregs *regs)
88 u16 addr = getComAddr(regs);
91 u16 timer = GET_BDA(timer_counter);
92 u16 timeout = GET_BDA(com_timeout[regs->dx]);
93 while (((inb(addr+5) & 0x60) != 0x60) && (timeout)) {
94 u16 val16 = GET_BDA(timer_counter);
101 outb(regs->al, addr);
102 regs->ah = inb(addr+5);
108 // SERIAL - READ CHARACTER FROM PORT
110 handle_1402(struct bregs *regs)
112 u16 addr = getComAddr(regs);
115 u16 timer = GET_BDA(timer_counter);
116 u16 timeout = GET_BDA(com_timeout[regs->dx]);
117 while (((inb(addr+5) & 0x01) == 0) && (timeout)) {
118 u16 val16 = GET_BDA(timer_counter);
119 if (val16 != timer) {
126 regs->al = inb(addr);
128 regs->ah = inb(addr+5);
133 // SERIAL - GET PORT STATUS
135 handle_1403(struct bregs *regs)
137 u16 addr = getComAddr(regs);
140 regs->ah = inb(addr+5);
141 regs->al = inb(addr+6);
146 handle_14XX(struct bregs *regs)
152 // INT 14h Serial Communications Service Entry Point
154 handle_14(struct bregs *regs)
156 debug_enter(regs, DEBUG_HDL_14);
157 if (! CONFIG_SERIAL) {
165 case 0x00: handle_1400(regs); break;
166 case 0x01: handle_1401(regs); break;
167 case 0x02: handle_1402(regs); break;
168 case 0x03: handle_1403(regs); break;
169 default: handle_14XX(regs); break;
174 /****************************************************************
176 ****************************************************************/
179 detect_parport(u16 port, u8 timeout, u8 count)
182 outb(inb(port+2) & 0xdf, port+2);
185 if (inb(port) != 0xaa)
188 SET_BDA(port_lpt[count], port);
189 SET_BDA(lpt_timeout[count], timeout);
198 dprintf(3, "init lpt\n");
201 count += detect_parport(0x378, 0x14, count);
202 count += detect_parport(0x278, 0x14, count);
204 // Equipment word bits 14..15 determing # parallel ports
205 u16 eqb = GET_BDA(equipment_list_flags);
206 SET_BDA(equipment_list_flags, (eqb & 0x3fff) | (count << 14));
210 getLptAddr(struct bregs *regs)
216 u16 addr = GET_BDA(port_lpt[regs->dx]);
223 lpt_ret(struct bregs *regs, u16 addr, u16 timeout)
225 u8 val8 = inb(addr+1);
226 regs->ah = (val8 ^ 0x48);
232 // INT 17 - PRINTER - WRITE CHARACTER
234 handle_1700(struct bregs *regs)
236 u16 addr = getLptAddr(regs);
239 u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
241 outb(regs->al, addr);
242 u8 val8 = inb(addr+2);
243 outb(val8 | 0x01, addr+2); // send strobe
245 outb(val8 & ~0x01, addr+2);
246 while (((inb(addr+1) & 0x40) == 0x40) && (timeout))
249 lpt_ret(regs, addr, timeout);
252 // INT 17 - PRINTER - INITIALIZE PORT
254 handle_1701(struct bregs *regs)
256 u16 addr = getLptAddr(regs);
259 u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
261 u8 val8 = inb(addr+2);
262 outb(val8 & ~0x04, addr+2); // send init
264 outb(val8 | 0x04, addr+2);
266 lpt_ret(regs, addr, timeout);
269 // INT 17 - PRINTER - GET STATUS
271 handle_1702(struct bregs *regs)
273 u16 addr = getLptAddr(regs);
276 u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
278 lpt_ret(regs, addr, timeout);
282 handle_17XX(struct bregs *regs)
288 // INT17h : Printer Service Entry Point
290 handle_17(struct bregs *regs)
292 debug_enter(regs, DEBUG_HDL_17);
301 case 0x00: handle_1700(regs); break;
302 case 0x01: handle_1701(regs); break;
303 case 0x02: handle_1702(regs); break;
304 default: handle_17XX(regs); break;