1 // 16bit code to handle serial and printer services.
3 // Copyright (C) 2008,2009 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 LGPLv3 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)
20 outb(0x02, port+SEROFF_IER);
21 u8 ier = inb(port+SEROFF_IER);
24 u8 iir = inb(port+SEROFF_IIR);
25 if ((iir & 0x3f) != 0x02)
28 outb(0x00, port+SEROFF_IER);
29 SET_BDA(port_com[count], port);
30 SET_BDA(com_timeout[count], timeout);
39 dprintf(3, "init serial\n");
42 count += detect_serial(PORT_SERIAL1, 0x0a, count);
43 count += detect_serial(PORT_SERIAL2, 0x0a, count);
44 count += detect_serial(PORT_SERIAL3, 0x0a, count);
45 count += detect_serial(PORT_SERIAL4, 0x0a, count);
46 dprintf(1, "Found %d serial ports\n", count);
48 // Equipment word bits 9..11 determing # serial ports
49 u16 eqb = GET_BDA(equipment_list_flags);
50 SET_BDA(equipment_list_flags, (eqb & 0xf1ff) | (count << 9));
54 getComAddr(struct bregs *regs)
60 u16 addr = GET_BDA(port_com[regs->dx]);
66 // SERIAL - INITIALIZE PORT
68 handle_1400(struct bregs *regs)
70 u16 addr = getComAddr(regs);
73 outb(inb(addr+SEROFF_LCR) | 0x80, addr+SEROFF_LCR);
74 if ((regs->al & 0xE0) == 0) {
75 outb(0x17, addr+SEROFF_DLL);
76 outb(0x04, addr+SEROFF_DLH);
78 u16 val16 = 0x600 >> ((regs->al & 0xE0) >> 5);
79 outb(val16 & 0xFF, addr+SEROFF_DLL);
80 outb(val16 >> 8, addr+SEROFF_DLH);
82 outb(regs->al & 0x1F, addr+SEROFF_LCR);
83 regs->ah = inb(addr+SEROFF_LSR);
84 regs->al = inb(addr+SEROFF_MSR);
88 // SERIAL - WRITE CHARACTER TO PORT
90 handle_1401(struct bregs *regs)
92 u16 addr = getComAddr(regs);
95 u16 timer = GET_BDA(timer_counter);
96 u16 timeout = GET_BDA(com_timeout[regs->dx]);
97 while (((inb(addr+SEROFF_LSR) & 0x60) != 0x60) && (timeout)) {
98 u16 val16 = GET_BDA(timer_counter);
105 outb(regs->al, addr+SEROFF_DATA);
106 regs->ah = inb(addr+SEROFF_LSR);
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+SEROFF_LSR) & 0x01) == 0) && (timeout)) {
122 u16 val16 = GET_BDA(timer_counter);
123 if (val16 != timer) {
130 regs->al = inb(addr+SEROFF_DATA);
132 regs->ah = inb(addr+SEROFF_LSR);
137 // SERIAL - GET PORT STATUS
139 handle_1403(struct bregs *regs)
141 u16 addr = getComAddr(regs);
144 regs->ah = inb(addr+SEROFF_LSR);
145 regs->al = inb(addr+SEROFF_MSR);
150 handle_14XX(struct bregs *regs)
156 // INT 14h Serial Communications Service Entry Point
158 handle_14(struct bregs *regs)
160 debug_enter(regs, DEBUG_HDL_14);
161 if (! CONFIG_SERIAL) {
169 case 0x00: handle_1400(regs); break;
170 case 0x01: handle_1401(regs); break;
171 case 0x02: handle_1402(regs); break;
172 case 0x03: handle_1403(regs); break;
173 default: handle_14XX(regs); break;
177 // XXX - Baud Rate Generator Table
178 u8 BaudTable[16] VAR16FIXED(0xe729);
181 /****************************************************************
183 ****************************************************************/
186 detect_parport(u16 port, u8 timeout, u8 count)
189 outb(inb(port+2) & 0xdf, port+2);
192 if (inb(port) != 0xaa)
195 SET_BDA(port_lpt[count], port);
196 SET_BDA(lpt_timeout[count], timeout);
205 dprintf(3, "init lpt\n");
208 count += detect_parport(0x378, 0x14, count);
209 count += detect_parport(0x278, 0x14, count);
210 dprintf(1, "Found %d lpt ports\n", count);
212 // Equipment word bits 14..15 determing # parallel ports
213 u16 eqb = GET_BDA(equipment_list_flags);
214 SET_BDA(equipment_list_flags, (eqb & 0x3fff) | (count << 14));
218 getLptAddr(struct bregs *regs)
224 u16 addr = GET_BDA(port_lpt[regs->dx]);
231 lpt_ret(struct bregs *regs, u16 addr, u16 timeout)
233 u8 val8 = inb(addr+1);
234 regs->ah = (val8 ^ 0x48);
240 // INT 17 - PRINTER - WRITE CHARACTER
242 handle_1700(struct bregs *regs)
244 u16 addr = getLptAddr(regs);
247 u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
249 outb(regs->al, addr);
250 u8 val8 = inb(addr+2);
251 outb(val8 | 0x01, addr+2); // send strobe
253 outb(val8 & ~0x01, addr+2);
254 // XXX - implement better timeout code.
255 while (((inb(addr+1) & 0x40) == 0x40) && (timeout))
258 lpt_ret(regs, addr, timeout);
261 // INT 17 - PRINTER - INITIALIZE PORT
263 handle_1701(struct bregs *regs)
265 u16 addr = getLptAddr(regs);
268 u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
270 u8 val8 = inb(addr+2);
271 outb(val8 & ~0x04, addr+2); // send init
273 outb(val8 | 0x04, addr+2);
275 lpt_ret(regs, addr, timeout);
278 // INT 17 - PRINTER - GET STATUS
280 handle_1702(struct bregs *regs)
282 u16 addr = getLptAddr(regs);
285 u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
287 lpt_ret(regs, addr, timeout);
291 handle_17XX(struct bregs *regs)
297 // INT17h : Printer Service Entry Point
299 handle_17(struct bregs *regs)
301 debug_enter(regs, DEBUG_HDL_17);
310 case 0x00: handle_1700(regs); break;
311 case 0x01: handle_1701(regs); break;
312 case 0x02: handle_1702(regs); break;
313 default: handle_17XX(regs); break;