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);
34 count += detect_serial(0x3f8, 0x0a, count);
35 count += detect_serial(0x2f8, 0x0a, count);
36 count += detect_serial(0x3e8, 0x0a, count);
37 count += detect_serial(0x2e8, 0x0a, count);
39 // Equipment word bits 9..11 determing # serial ports
40 u16 eqb = GET_BDA(equipment_list_flags);
41 SET_BDA(equipment_list_flags, (eqb & 0xf1ff) | (count << 9));
45 getComAddr(struct bregs *regs)
51 u16 addr = GET_BDA(port_com[regs->dx]);
57 // SERIAL - INITIALIZE PORT
59 handle_1400(struct bregs *regs)
61 u16 addr = getComAddr(regs);
64 outb(inb(addr+3) | 0x80, addr+3);
65 if ((regs->al & 0xE0) == 0) {
69 u16 val16 = 0x600 >> ((regs->al & 0xE0) >> 5);
70 outb(val16 & 0xFF, addr);
71 outb(val16 >> 8, addr+1);
73 outb(regs->al & 0x1F, addr+3);
74 regs->ah = inb(addr+5);
75 regs->al = inb(addr+6);
80 write_serial(u16 addr, u16 timeout, char c)
82 u16 timer = GET_BDA(timer_counter);
83 while (((inb(addr+5) & 0x60) != 0x60) && (timeout)) {
84 u16 val16 = GET_BDA(timer_counter);
97 // SERIAL - WRITE CHARACTER TO PORT
99 handle_1401(struct bregs *regs)
101 u16 addr = getComAddr(regs);
104 u16 timeout = GET_BDA(com_timeout[regs->dx]);
105 int ret = write_serial(addr, timeout, regs->al);
106 regs->ah = inb(addr+5);
112 // SERIAL - READ CHARACTER FROM PORT
114 handle_1402(struct bregs *regs)
116 u16 addr = getComAddr(regs);
119 u16 timer = GET_BDA(timer_counter);
120 u16 timeout = GET_BDA(com_timeout[regs->dx]);
121 while (((inb(addr+5) & 0x01) == 0) && (timeout)) {
122 u16 val16 = GET_BDA(timer_counter);
123 if (val16 != timer) {
130 regs->al = inb(addr);
132 regs->ah = inb(addr+5);
137 // SERIAL - GET PORT STATUS
139 handle_1403(struct bregs *regs)
141 u16 addr = getComAddr(regs);
144 regs->ah = inb(addr+5);
145 regs->al = inb(addr+6);
150 handle_14XX(struct bregs *regs)
156 // INT 14h Serial Communications Service Entry Point
158 handle_14(struct bregs *regs)
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 ****************************************************************/
178 #define BX_DEBUG_PORT 0x03f8
183 /* setup for serial logging: 8N1 */
184 outb(0x03, BX_DEBUG_PORT+3);
190 write_serial(BX_DEBUG_PORT, 0x0a, c);
194 /****************************************************************
196 ****************************************************************/
199 detect_parport(u16 port, u8 timeout, u8 count)
202 outb(inb(port+2) & 0xdf, port+2);
205 if (inb(port) != 0xaa)
208 SET_BDA(port_lpt[count], port);
209 SET_BDA(lpt_timeout[count], timeout);
217 count += detect_parport(0x378, 0x14, count);
218 count += detect_parport(0x278, 0x14, count);
220 // Equipment word bits 14..15 determing # parallel ports
221 u16 eqb = GET_BDA(equipment_list_flags);
222 SET_BDA(equipment_list_flags, (eqb & 0x3fff) | (count << 14));
226 getLptAddr(struct bregs *regs)
232 u16 addr = GET_BDA(port_lpt[regs->dx]);
239 lpt_ret(struct bregs *regs, u16 addr, u16 timeout)
241 u8 val8 = inb(addr+1);
242 regs->ah = (val8 ^ 0x48);
248 // INT 17 - PRINTER - WRITE CHARACTER
250 handle_1700(struct bregs *regs)
252 u16 addr = getLptAddr(regs);
255 u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
257 outb(regs->al, addr);
258 u8 val8 = inb(addr+2);
259 outb(val8 | 0x01, addr+2); // send strobe
261 outb(val8 & ~0x01, addr+2);
262 while (((inb(addr+1) & 0x40) == 0x40) && (timeout))
265 lpt_ret(regs, addr, timeout);
268 // INT 17 - PRINTER - INITIALIZE PORT
270 handle_1701(struct bregs *regs)
272 u16 addr = getLptAddr(regs);
275 u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
277 u8 val8 = inb(addr+2);
278 outb(val8 & ~0x04, addr+2); // send init
280 outb(val8 | 0x04, addr+2);
282 lpt_ret(regs, addr, timeout);
285 // INT 17 - PRINTER - GET STATUS
287 handle_1702(struct bregs *regs)
289 u16 addr = getLptAddr(regs);
292 u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
294 lpt_ret(regs, addr, timeout);
298 handle_17XX(struct bregs *regs)
304 // INT17h : Printer Service Entry Point
306 handle_17(struct bregs *regs)
313 case 0x00: handle_1700(regs); break;
314 case 0x01: handle_1701(regs); break;
315 case 0x02: handle_1702(regs); break;
316 default: handle_17XX(regs); break;