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 "bregs.h" // struct bregs
10 #include "hw/serialio.h" // SEROFF_IER
11 #include "output.h" // debug_enter
12 #include "romfile.h" // romfile_loadint
13 #include "stacks.h" // yield
14 #include "util.h" // serial_setup
17 /****************************************************************
19 ****************************************************************/
22 detect_serial(u16 port, u8 timeout, u8 count)
24 if (CONFIG_DEBUG_SERIAL && port == CONFIG_DEBUG_SERIAL_PORT
25 && !romfile_loadint("etc/advertise-serial-debug-port", 1))
27 outb(0x02, port+SEROFF_IER);
28 u8 ier = inb(port+SEROFF_IER);
31 u8 iir = inb(port+SEROFF_IIR);
32 if ((iir & 0x3f) != 0x02)
35 outb(0x00, port+SEROFF_IER);
36 SET_BDA(port_com[count], port);
37 SET_BDA(com_timeout[count], timeout);
46 dprintf(3, "init serial\n");
49 count += detect_serial(PORT_SERIAL1, 0x0a, count);
50 count += detect_serial(PORT_SERIAL2, 0x0a, count);
51 count += detect_serial(PORT_SERIAL3, 0x0a, count);
52 count += detect_serial(PORT_SERIAL4, 0x0a, count);
53 dprintf(1, "Found %d serial ports\n", count);
55 // Equipment word bits 9..11 determing # serial ports
56 set_equipment_flags(0xe00, count << 9);
60 getComAddr(struct bregs *regs)
66 u16 addr = GET_BDA(port_com[regs->dx]);
72 // SERIAL - INITIALIZE PORT
74 handle_1400(struct bregs *regs)
76 u16 addr = getComAddr(regs);
79 outb(inb(addr+SEROFF_LCR) | 0x80, addr+SEROFF_LCR);
80 if ((regs->al & 0xE0) == 0) {
81 outb(0x17, addr+SEROFF_DLL);
82 outb(0x04, addr+SEROFF_DLH);
84 u16 val16 = 0x600 >> ((regs->al & 0xE0) >> 5);
85 outb(val16 & 0xFF, addr+SEROFF_DLL);
86 outb(val16 >> 8, addr+SEROFF_DLH);
88 outb(regs->al & 0x1F, addr+SEROFF_LCR);
89 regs->ah = inb(addr+SEROFF_LSR);
90 regs->al = inb(addr+SEROFF_MSR);
94 // SERIAL - WRITE CHARACTER TO PORT
96 handle_1401(struct bregs *regs)
98 u16 addr = getComAddr(regs);
101 u32 end = irqtimer_calc_ticks(GET_BDA(com_timeout[regs->dx]));
103 u8 lsr = inb(addr+SEROFF_LSR);
104 if ((lsr & 0x60) == 0x60) {
105 // Success - can write data
106 outb(regs->al, addr+SEROFF_DATA);
111 if (irqtimer_check(end)) {
112 // Timed out - can't write data.
113 regs->ah = lsr | 0x80;
121 // SERIAL - READ CHARACTER FROM PORT
123 handle_1402(struct bregs *regs)
125 u16 addr = getComAddr(regs);
128 u32 end = irqtimer_calc_ticks(GET_BDA(com_timeout[regs->dx]));
130 u8 lsr = inb(addr+SEROFF_LSR);
132 // Success - can read data
133 regs->al = inb(addr+SEROFF_DATA);
137 if (irqtimer_check(end)) {
138 // Timed out - can't read data.
139 regs->ah = lsr | 0x80;
147 // SERIAL - GET PORT STATUS
149 handle_1403(struct bregs *regs)
151 u16 addr = getComAddr(regs);
154 regs->ah = inb(addr+SEROFF_LSR);
155 regs->al = inb(addr+SEROFF_MSR);
160 handle_14XX(struct bregs *regs)
162 set_unimplemented(regs);
165 // INT 14h Serial Communications Service Entry Point
167 handle_14(struct bregs *regs)
169 debug_enter(regs, DEBUG_HDL_14);
170 if (! CONFIG_SERIAL) {
176 case 0x00: handle_1400(regs); break;
177 case 0x01: handle_1401(regs); break;
178 case 0x02: handle_1402(regs); break;
179 case 0x03: handle_1403(regs); break;
180 default: handle_14XX(regs); break;
185 /****************************************************************
187 ****************************************************************/
190 detect_parport(u16 port, u8 timeout, u8 count)
193 outb(inb(port+2) & 0xdf, port+2);
196 if (inb(port) != 0xaa)
199 SET_BDA(port_lpt[count], port);
200 SET_BDA(lpt_timeout[count], timeout);
209 dprintf(3, "init lpt\n");
212 count += detect_parport(PORT_LPT1, 0x14, count);
213 count += detect_parport(PORT_LPT2, 0x14, count);
214 dprintf(1, "Found %d lpt ports\n", count);
216 // Equipment word bits 14..15 determing # parallel ports
217 set_equipment_flags(0xc000, count << 14);
221 getLptAddr(struct bregs *regs)
227 u16 addr = GET_BDA(port_lpt[regs->dx]);
233 // INT 17 - PRINTER - WRITE CHARACTER
235 handle_1700(struct bregs *regs)
237 u16 addr = getLptAddr(regs);
241 u32 end = irqtimer_calc_ticks(GET_BDA(lpt_timeout[regs->dx]));
243 outb(regs->al, addr);
244 u8 val8 = inb(addr+2);
245 outb(val8 | 0x01, addr+2); // send strobe
247 outb(val8 & ~0x01, addr+2);
256 if (irqtimer_check(end)) {
258 regs->ah = (v ^ 0x48) | 0x01;
267 // INT 17 - PRINTER - INITIALIZE PORT
269 handle_1701(struct bregs *regs)
271 u16 addr = getLptAddr(regs);
275 u8 val8 = inb(addr+2);
276 outb(val8 & ~0x04, addr+2); // send init
278 outb(val8 | 0x04, addr+2);
280 regs->ah = inb(addr+1) ^ 0x48;
284 // INT 17 - PRINTER - GET STATUS
286 handle_1702(struct bregs *regs)
288 u16 addr = getLptAddr(regs);
291 regs->ah = inb(addr+1) ^ 0x48;
296 handle_17XX(struct bregs *regs)
298 set_unimplemented(regs);
301 // INT17h : Printer Service Entry Point
303 handle_17(struct bregs *regs)
305 debug_enter(regs, DEBUG_HDL_17);
312 case 0x00: handle_1700(regs); break;
313 case 0x01: handle_1701(regs); break;
314 case 0x02: handle_1702(regs); break;
315 default: handle_17XX(regs); break;