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);
34 dprintf(3, "init serial\n");
36 count += detect_serial(0x3f8, 0x0a, count);
37 count += detect_serial(0x2f8, 0x0a, count);
38 count += detect_serial(0x3e8, 0x0a, count);
39 count += detect_serial(0x2e8, 0x0a, count);
41 // Equipment word bits 9..11 determing # serial ports
42 u16 eqb = GET_BDA(equipment_list_flags);
43 SET_BDA(equipment_list_flags, (eqb & 0xf1ff) | (count << 9));
47 getComAddr(struct bregs *regs)
53 u16 addr = GET_BDA(port_com[regs->dx]);
59 // SERIAL - INITIALIZE PORT
61 handle_1400(struct bregs *regs)
63 u16 addr = getComAddr(regs);
66 outb(inb(addr+3) | 0x80, addr+3);
67 if ((regs->al & 0xE0) == 0) {
71 u16 val16 = 0x600 >> ((regs->al & 0xE0) >> 5);
72 outb(val16 & 0xFF, addr);
73 outb(val16 >> 8, addr+1);
75 outb(regs->al & 0x1F, addr+3);
76 regs->ah = inb(addr+5);
77 regs->al = inb(addr+6);
81 // SERIAL - WRITE CHARACTER TO PORT
83 handle_1401(struct bregs *regs)
85 u16 addr = getComAddr(regs);
88 u16 timer = GET_BDA(timer_counter);
89 u16 timeout = GET_BDA(com_timeout[regs->dx]);
90 while (((inb(addr+5) & 0x60) != 0x60) && (timeout)) {
91 u16 val16 = GET_BDA(timer_counter);
99 regs->ah = inb(addr+5);
105 // SERIAL - READ CHARACTER FROM PORT
107 handle_1402(struct bregs *regs)
109 u16 addr = getComAddr(regs);
112 u16 timer = GET_BDA(timer_counter);
113 u16 timeout = GET_BDA(com_timeout[regs->dx]);
114 while (((inb(addr+5) & 0x01) == 0) && (timeout)) {
115 u16 val16 = GET_BDA(timer_counter);
116 if (val16 != timer) {
123 regs->al = inb(addr);
125 regs->ah = inb(addr+5);
130 // SERIAL - GET PORT STATUS
132 handle_1403(struct bregs *regs)
134 u16 addr = getComAddr(regs);
137 regs->ah = inb(addr+5);
138 regs->al = inb(addr+6);
143 handle_14XX(struct bregs *regs)
149 // INT 14h Serial Communications Service Entry Point
151 handle_14(struct bregs *regs)
153 debug_enter(regs, DEBUG_HDL_14);
158 case 0x00: handle_1400(regs); break;
159 case 0x01: handle_1401(regs); break;
160 case 0x02: handle_1402(regs); break;
161 case 0x03: handle_1403(regs); break;
162 default: handle_14XX(regs); break;
167 /****************************************************************
169 ****************************************************************/
172 detect_parport(u16 port, u8 timeout, u8 count)
175 outb(inb(port+2) & 0xdf, port+2);
178 if (inb(port) != 0xaa)
181 SET_BDA(port_lpt[count], port);
182 SET_BDA(lpt_timeout[count], timeout);
189 dprintf(3, "init lpt\n");
191 count += detect_parport(0x378, 0x14, count);
192 count += detect_parport(0x278, 0x14, count);
194 // Equipment word bits 14..15 determing # parallel ports
195 u16 eqb = GET_BDA(equipment_list_flags);
196 SET_BDA(equipment_list_flags, (eqb & 0x3fff) | (count << 14));
200 getLptAddr(struct bregs *regs)
206 u16 addr = GET_BDA(port_lpt[regs->dx]);
213 lpt_ret(struct bregs *regs, u16 addr, u16 timeout)
215 u8 val8 = inb(addr+1);
216 regs->ah = (val8 ^ 0x48);
222 // INT 17 - PRINTER - WRITE CHARACTER
224 handle_1700(struct bregs *regs)
226 u16 addr = getLptAddr(regs);
229 u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
231 outb(regs->al, addr);
232 u8 val8 = inb(addr+2);
233 outb(val8 | 0x01, addr+2); // send strobe
235 outb(val8 & ~0x01, addr+2);
236 while (((inb(addr+1) & 0x40) == 0x40) && (timeout))
239 lpt_ret(regs, addr, timeout);
242 // INT 17 - PRINTER - INITIALIZE PORT
244 handle_1701(struct bregs *regs)
246 u16 addr = getLptAddr(regs);
249 u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
251 u8 val8 = inb(addr+2);
252 outb(val8 & ~0x04, addr+2); // send init
254 outb(val8 | 0x04, addr+2);
256 lpt_ret(regs, addr, timeout);
259 // INT 17 - PRINTER - GET STATUS
261 handle_1702(struct bregs *regs)
263 u16 addr = getLptAddr(regs);
266 u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
268 lpt_ret(regs, addr, timeout);
272 handle_17XX(struct bregs *regs)
278 // INT17h : Printer Service Entry Point
280 handle_17(struct bregs *regs)
282 debug_enter(regs, DEBUG_HDL_17);
287 case 0x00: handle_1700(regs); break;
288 case 0x01: handle_1701(regs); break;
289 case 0x02: handle_1702(regs); break;
290 default: handle_17XX(regs); break;