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
12 // Timers based on 18.2Hz clock irq.
14 u16 last_tick, remaining;
18 initTickTimer(u16 count)
20 struct tick_timer_s tt = {GET_BDA(timer_counter), count};
25 checkTickTimer(struct tick_timer_s *tt)
27 u16 timer = GET_BDA(timer_counter);
28 if (tt->last_tick != timer) {
29 tt->last_tick = timer;
38 /****************************************************************
40 ****************************************************************/
43 detect_serial(u16 port, u8 timeout, u8 count)
45 outb(0x02, port+SEROFF_IER);
46 u8 ier = inb(port+SEROFF_IER);
49 u8 iir = inb(port+SEROFF_IIR);
50 if ((iir & 0x3f) != 0x02)
53 outb(0x00, port+SEROFF_IER);
54 SET_BDA(port_com[count], port);
55 SET_BDA(com_timeout[count], timeout);
64 dprintf(3, "init serial\n");
67 count += detect_serial(PORT_SERIAL1, 0x0a, count);
68 count += detect_serial(PORT_SERIAL2, 0x0a, count);
69 count += detect_serial(PORT_SERIAL3, 0x0a, count);
70 count += detect_serial(PORT_SERIAL4, 0x0a, count);
71 dprintf(1, "Found %d serial ports\n", count);
73 // Equipment word bits 9..11 determing # serial ports
74 u16 eqb = GET_BDA(equipment_list_flags);
75 SET_BDA(equipment_list_flags, (eqb & 0xf1ff) | (count << 9));
79 getComAddr(struct bregs *regs)
85 u16 addr = GET_BDA(port_com[regs->dx]);
91 // SERIAL - INITIALIZE PORT
93 handle_1400(struct bregs *regs)
95 u16 addr = getComAddr(regs);
98 outb(inb(addr+SEROFF_LCR) | 0x80, addr+SEROFF_LCR);
99 if ((regs->al & 0xE0) == 0) {
100 outb(0x17, addr+SEROFF_DLL);
101 outb(0x04, addr+SEROFF_DLH);
103 u16 val16 = 0x600 >> ((regs->al & 0xE0) >> 5);
104 outb(val16 & 0xFF, addr+SEROFF_DLL);
105 outb(val16 >> 8, addr+SEROFF_DLH);
107 outb(regs->al & 0x1F, addr+SEROFF_LCR);
108 regs->ah = inb(addr+SEROFF_LSR);
109 regs->al = inb(addr+SEROFF_MSR);
113 // SERIAL - WRITE CHARACTER TO PORT
115 handle_1401(struct bregs *regs)
117 u16 addr = getComAddr(regs);
120 struct tick_timer_s tt = initTickTimer(GET_BDA(com_timeout[regs->dx]));
122 u8 lsr = inb(addr+SEROFF_LSR);
123 if ((lsr & 0x60) == 0x60) {
124 // Success - can write data
125 outb(regs->al, addr+SEROFF_DATA);
130 if (checkTickTimer(&tt)) {
131 // Timed out - can't write data.
132 regs->ah = lsr | 0x80;
140 // SERIAL - READ CHARACTER FROM PORT
142 handle_1402(struct bregs *regs)
144 u16 addr = getComAddr(regs);
147 struct tick_timer_s tt = initTickTimer(GET_BDA(com_timeout[regs->dx]));
149 u8 lsr = inb(addr+SEROFF_LSR);
151 // Success - can read data
152 regs->al = inb(addr+SEROFF_DATA);
156 if (checkTickTimer(&tt)) {
157 // Timed out - can't read data.
158 regs->ah = lsr | 0x80;
166 // SERIAL - GET PORT STATUS
168 handle_1403(struct bregs *regs)
170 u16 addr = getComAddr(regs);
173 regs->ah = inb(addr+SEROFF_LSR);
174 regs->al = inb(addr+SEROFF_MSR);
179 handle_14XX(struct bregs *regs)
181 set_unimplemented(regs);
184 // INT 14h Serial Communications Service Entry Point
186 handle_14(struct bregs *regs)
188 debug_enter(regs, DEBUG_HDL_14);
189 if (! CONFIG_SERIAL) {
195 case 0x00: handle_1400(regs); break;
196 case 0x01: handle_1401(regs); break;
197 case 0x02: handle_1402(regs); break;
198 case 0x03: handle_1403(regs); break;
199 default: handle_14XX(regs); break;
203 // XXX - Baud Rate Generator Table
204 u8 BaudTable[16] VAR16FIXED(0xe729);
207 /****************************************************************
209 ****************************************************************/
212 detect_parport(u16 port, u8 timeout, u8 count)
215 outb(inb(port+2) & 0xdf, port+2);
218 if (inb(port) != 0xaa)
221 SET_BDA(port_lpt[count], port);
222 SET_BDA(lpt_timeout[count], timeout);
231 dprintf(3, "init lpt\n");
234 count += detect_parport(PORT_LPT1, 0x14, count);
235 count += detect_parport(PORT_LPT2, 0x14, count);
236 dprintf(1, "Found %d lpt ports\n", count);
238 // Equipment word bits 14..15 determing # parallel ports
239 u16 eqb = GET_BDA(equipment_list_flags);
240 SET_BDA(equipment_list_flags, (eqb & 0x3fff) | (count << 14));
244 getLptAddr(struct bregs *regs)
250 u16 addr = GET_BDA(port_lpt[regs->dx]);
256 // INT 17 - PRINTER - WRITE CHARACTER
258 handle_1700(struct bregs *regs)
260 u16 addr = getLptAddr(regs);
264 struct tick_timer_s tt = initTickTimer(GET_BDA(lpt_timeout[regs->dx]));
266 outb(regs->al, addr);
267 u8 val8 = inb(addr+2);
268 outb(val8 | 0x01, addr+2); // send strobe
270 outb(val8 & ~0x01, addr+2);
279 if (checkTickTimer(&tt)) {
281 regs->ah = (v ^ 0x48) | 0x01;
290 // INT 17 - PRINTER - INITIALIZE PORT
292 handle_1701(struct bregs *regs)
294 u16 addr = getLptAddr(regs);
298 u8 val8 = inb(addr+2);
299 outb(val8 & ~0x04, addr+2); // send init
301 outb(val8 | 0x04, addr+2);
303 regs->ah = inb(addr+1) ^ 0x48;
307 // INT 17 - PRINTER - GET STATUS
309 handle_1702(struct bregs *regs)
311 u16 addr = getLptAddr(regs);
314 regs->ah = inb(addr+1) ^ 0x48;
319 handle_17XX(struct bregs *regs)
321 set_unimplemented(regs);
324 // INT17h : Printer Service Entry Point
326 handle_17(struct bregs *regs)
328 debug_enter(regs, DEBUG_HDL_17);
335 case 0x00: handle_1700(regs); break;
336 case 0x01: handle_1701(regs); break;
337 case 0x02: handle_1702(regs); break;
338 default: handle_17XX(regs); break;