Replace debug_exit calls with debug info while setting a failure.
[seabios.git] / src / serial.c
1 // 16bit code to handle serial and printer services.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU GPLv3 license.
7
8 #include "biosvar.h" // struct bregs
9 #include "util.h" // debug_enter
10
11
12 /****************************************************************
13  * COM ports
14  ****************************************************************/
15
16 static u16
17 getComAddr(struct bregs *regs)
18 {
19     if (regs->dx >= 4) {
20         set_fail(regs);
21         return 0;
22     }
23     u16 addr = GET_BDA(port_com[regs->dx]);
24     if (! addr)
25         set_fail(regs);
26     return addr;
27 }
28
29 static void
30 handle_1400(struct bregs *regs)
31 {
32     u16 addr = getComAddr(regs);
33     if (!addr)
34         return;
35     outb(inb(addr+3) | 0x80, addr+3);
36     if ((regs->al & 0xE0) == 0) {
37         outb(0x17, addr);
38         outb(0x04, addr+1);
39     } else {
40         u16 val16 = 0x600 >> ((regs->al & 0xE0) >> 5);
41         outb(val16 & 0xFF, addr);
42         outb(val16 >> 8, addr+1);
43     }
44     outb(regs->al & 0x1F, addr+3);
45     regs->ah = inb(addr+5);
46     regs->al = inb(addr+6);
47     set_success(regs);
48 }
49
50 static void
51 handle_1401(struct bregs *regs)
52 {
53     u16 addr = getComAddr(regs);
54     if (!addr)
55         return;
56     u16 timer = GET_BDA(timer_counter);
57     u16 timeout = GET_BDA(com_timeout[regs->dx]);
58     while (((inb(addr+5) & 0x60) != 0x60) && (timeout)) {
59         u16 val16 = GET_BDA(timer_counter);
60         if (val16 != timer) {
61             timer = val16;
62             timeout--;
63         }
64     }
65     if (timeout)
66         outb(regs->al, addr);
67     regs->ah = inb(addr+5);
68     if (!timeout)
69         regs->ah |= 0x80;
70     set_success(regs);
71 }
72
73 static void
74 handle_1402(struct bregs *regs)
75 {
76     u16 addr = getComAddr(regs);
77     if (!addr)
78         return;
79     u16 timer = GET_BDA(timer_counter);
80     u16 timeout = GET_BDA(com_timeout[regs->dx]);
81     while (((inb(addr+5) & 0x01) == 0) && (timeout)) {
82         u16 val16 = GET_BDA(timer_counter);
83         if (val16 != timer) {
84             timer = val16;
85             timeout--;
86         }
87     }
88     if (timeout) {
89         regs->ah = 0;
90         regs->al = inb(addr);
91     } else {
92         regs->ah = inb(addr+5);
93     }
94     set_success(regs);
95 }
96
97 static void
98 handle_1403(struct bregs *regs)
99 {
100     u16 addr = getComAddr(regs);
101     if (!addr)
102         return;
103     regs->ah = inb(addr+5);
104     regs->al = inb(addr+6);
105     set_success(regs);
106 }
107
108 static void
109 handle_14XX(struct bregs *regs)
110 {
111     // Unsupported
112     set_fail(regs);
113 }
114
115 // INT 14h Serial Communications Service Entry Point
116 void VISIBLE16
117 handle_14(struct bregs *regs)
118 {
119     debug_enter(regs);
120
121     irq_enable();
122
123     switch (regs->ah) {
124     case 0x00: handle_1400(regs); break;
125     case 0x01: handle_1401(regs); break;
126     case 0x02: handle_1402(regs); break;
127     case 0x03: handle_1403(regs); break;
128     default:   handle_14XX(regs); break;
129     }
130 }
131
132
133 /****************************************************************
134  * LPT ports
135  ****************************************************************/
136
137 static u16
138 getLptAddr(struct bregs *regs)
139 {
140     if (regs->dx >= 3) {
141         set_fail(regs);
142         return 0;
143     }
144     u16 addr = GET_BDA(port_lpt[regs->dx]);
145     if (! addr)
146         set_fail(regs);
147     return addr;
148 }
149
150 static void
151 lpt_ret(struct bregs *regs, u16 addr, u16 timeout)
152 {
153     u8 val8 = inb(addr+1);
154     regs->ah = (val8 ^ 0x48);
155     if (!timeout)
156         regs->ah |= 0x01;
157     set_success(regs);
158 }
159
160 // INT 17 - PRINTER - WRITE CHARACTER
161 static void
162 handle_1700(struct bregs *regs)
163 {
164     u16 addr = getLptAddr(regs);
165     if (!addr)
166         return;
167     u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
168
169     outb(regs->al, addr);
170     u8 val8 = inb(addr+2);
171     outb(val8 | 0x01, addr+2); // send strobe
172     nop();
173     outb(val8 & ~0x01, addr+2);
174     while (((inb(addr+1) & 0x40) == 0x40) && (timeout))
175         timeout--;
176
177     lpt_ret(regs, addr, timeout);
178 }
179
180 // INT 17 - PRINTER - INITIALIZE PORT
181 static void
182 handle_1701(struct bregs *regs)
183 {
184     u16 addr = getLptAddr(regs);
185     if (!addr)
186         return;
187     u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
188
189     u8 val8 = inb(addr+2);
190     outb(val8 & ~0x04, addr+2); // send init
191     nop();
192     outb(val8 | 0x04, addr+2);
193
194     lpt_ret(regs, addr, timeout);
195 }
196
197 // INT 17 - PRINTER - GET STATUS
198 static void
199 handle_1702(struct bregs *regs)
200 {
201     u16 addr = getLptAddr(regs);
202     if (!addr)
203         return;
204     u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
205
206     lpt_ret(regs, addr, timeout);
207 }
208
209 static void
210 handle_17XX(struct bregs *regs)
211 {
212     // Unsupported
213     set_fail(regs);
214 }
215
216 // INT17h : Printer Service Entry Point
217 void VISIBLE16
218 handle_17(struct bregs *regs)
219 {
220     debug_enter(regs);
221
222     irq_enable();
223
224     switch (regs->ah) {
225     case 0x00: handle_1700(regs); break;
226     case 0x01: handle_1701(regs); break;
227     case 0x02: handle_1702(regs); break;
228     default:   handle_17XX(regs); break;
229     }
230 }