Move debugging messages to appropriate functions.
[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 detect_serial(u16 port, u8 timeout, u8 count)
18 {
19     outb(0x02, port+1);
20     if (inb(port+1) != 0x02)
21         return 0;
22     if (inb(port+2) != 0x02)
23         return 0;
24     outb(0x00, port+1);
25     SET_BDA(port_com[count], port);
26     SET_BDA(com_timeout[count], timeout);
27     return 1;
28 }
29
30 void
31 serial_setup()
32 {
33     dprintf(3, "init serial\n");
34     u16 count = 0;
35     count += detect_serial(0x3f8, 0x0a, count);
36     count += detect_serial(0x2f8, 0x0a, count);
37     count += detect_serial(0x3e8, 0x0a, count);
38     count += detect_serial(0x2e8, 0x0a, count);
39
40     // Equipment word bits 9..11 determing # serial ports
41     u16 eqb = GET_BDA(equipment_list_flags);
42     SET_BDA(equipment_list_flags, (eqb & 0xf1ff) | (count << 9));
43 }
44
45 static u16
46 getComAddr(struct bregs *regs)
47 {
48     if (regs->dx >= 4) {
49         set_fail(regs);
50         return 0;
51     }
52     u16 addr = GET_BDA(port_com[regs->dx]);
53     if (! addr)
54         set_fail(regs);
55     return addr;
56 }
57
58 // SERIAL - INITIALIZE PORT
59 static void
60 handle_1400(struct bregs *regs)
61 {
62     u16 addr = getComAddr(regs);
63     if (!addr)
64         return;
65     outb(inb(addr+3) | 0x80, addr+3);
66     if ((regs->al & 0xE0) == 0) {
67         outb(0x17, addr);
68         outb(0x04, addr+1);
69     } else {
70         u16 val16 = 0x600 >> ((regs->al & 0xE0) >> 5);
71         outb(val16 & 0xFF, addr);
72         outb(val16 >> 8, addr+1);
73     }
74     outb(regs->al & 0x1F, addr+3);
75     regs->ah = inb(addr+5);
76     regs->al = inb(addr+6);
77     set_success(regs);
78 }
79
80 // SERIAL - WRITE CHARACTER TO PORT
81 static void
82 handle_1401(struct bregs *regs)
83 {
84     u16 addr = getComAddr(regs);
85     if (!addr)
86         return;
87     u16 timer = GET_BDA(timer_counter);
88     u16 timeout = GET_BDA(com_timeout[regs->dx]);
89     while (((inb(addr+5) & 0x60) != 0x60) && (timeout)) {
90         u16 val16 = GET_BDA(timer_counter);
91         if (val16 != timer) {
92             timer = val16;
93             timeout--;
94         }
95     }
96     if (timeout)
97         outb(regs->al, addr);
98     regs->ah = inb(addr+5);
99     if (!timeout)
100         regs->ah |= 0x80;
101     set_success(regs);
102 }
103
104 // SERIAL - READ CHARACTER FROM PORT
105 static void
106 handle_1402(struct bregs *regs)
107 {
108     u16 addr = getComAddr(regs);
109     if (!addr)
110         return;
111     u16 timer = GET_BDA(timer_counter);
112     u16 timeout = GET_BDA(com_timeout[regs->dx]);
113     while (((inb(addr+5) & 0x01) == 0) && (timeout)) {
114         u16 val16 = GET_BDA(timer_counter);
115         if (val16 != timer) {
116             timer = val16;
117             timeout--;
118         }
119     }
120     if (timeout) {
121         regs->ah = 0;
122         regs->al = inb(addr);
123     } else {
124         regs->ah = inb(addr+5);
125     }
126     set_success(regs);
127 }
128
129 // SERIAL - GET PORT STATUS
130 static void
131 handle_1403(struct bregs *regs)
132 {
133     u16 addr = getComAddr(regs);
134     if (!addr)
135         return;
136     regs->ah = inb(addr+5);
137     regs->al = inb(addr+6);
138     set_success(regs);
139 }
140
141 static void
142 handle_14XX(struct bregs *regs)
143 {
144     // Unsupported
145     set_fail(regs);
146 }
147
148 // INT 14h Serial Communications Service Entry Point
149 void VISIBLE16
150 handle_14(struct bregs *regs)
151 {
152     debug_enter(regs);
153
154     irq_enable();
155
156     switch (regs->ah) {
157     case 0x00: handle_1400(regs); break;
158     case 0x01: handle_1401(regs); break;
159     case 0x02: handle_1402(regs); break;
160     case 0x03: handle_1403(regs); break;
161     default:   handle_14XX(regs); break;
162     }
163 }
164
165
166 /****************************************************************
167  * LPT ports
168  ****************************************************************/
169
170 static u16
171 detect_parport(u16 port, u8 timeout, u8 count)
172 {
173     // clear input mode
174     outb(inb(port+2) & 0xdf, port+2);
175
176     outb(0xaa, port);
177     if (inb(port) != 0xaa)
178         // Not present
179         return 0;
180     SET_BDA(port_lpt[count], port);
181     SET_BDA(lpt_timeout[count], timeout);
182     return 1;
183 }
184
185 void
186 lpt_setup()
187 {
188     dprintf(3, "init lpt\n");
189     u16 count = 0;
190     count += detect_parport(0x378, 0x14, count);
191     count += detect_parport(0x278, 0x14, count);
192
193     // Equipment word bits 14..15 determing # parallel ports
194     u16 eqb = GET_BDA(equipment_list_flags);
195     SET_BDA(equipment_list_flags, (eqb & 0x3fff) | (count << 14));
196 }
197
198 static u16
199 getLptAddr(struct bregs *regs)
200 {
201     if (regs->dx >= 3) {
202         set_fail(regs);
203         return 0;
204     }
205     u16 addr = GET_BDA(port_lpt[regs->dx]);
206     if (! addr)
207         set_fail(regs);
208     return addr;
209 }
210
211 static void
212 lpt_ret(struct bregs *regs, u16 addr, u16 timeout)
213 {
214     u8 val8 = inb(addr+1);
215     regs->ah = (val8 ^ 0x48);
216     if (!timeout)
217         regs->ah |= 0x01;
218     set_success(regs);
219 }
220
221 // INT 17 - PRINTER - WRITE CHARACTER
222 static void
223 handle_1700(struct bregs *regs)
224 {
225     u16 addr = getLptAddr(regs);
226     if (!addr)
227         return;
228     u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
229
230     outb(regs->al, addr);
231     u8 val8 = inb(addr+2);
232     outb(val8 | 0x01, addr+2); // send strobe
233     nop();
234     outb(val8 & ~0x01, addr+2);
235     while (((inb(addr+1) & 0x40) == 0x40) && (timeout))
236         timeout--;
237
238     lpt_ret(regs, addr, timeout);
239 }
240
241 // INT 17 - PRINTER - INITIALIZE PORT
242 static void
243 handle_1701(struct bregs *regs)
244 {
245     u16 addr = getLptAddr(regs);
246     if (!addr)
247         return;
248     u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
249
250     u8 val8 = inb(addr+2);
251     outb(val8 & ~0x04, addr+2); // send init
252     nop();
253     outb(val8 | 0x04, addr+2);
254
255     lpt_ret(regs, addr, timeout);
256 }
257
258 // INT 17 - PRINTER - GET STATUS
259 static void
260 handle_1702(struct bregs *regs)
261 {
262     u16 addr = getLptAddr(regs);
263     if (!addr)
264         return;
265     u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
266
267     lpt_ret(regs, addr, timeout);
268 }
269
270 static void
271 handle_17XX(struct bregs *regs)
272 {
273     // Unsupported
274     set_fail(regs);
275 }
276
277 // INT17h : Printer Service Entry Point
278 void VISIBLE16
279 handle_17(struct bregs *regs)
280 {
281     debug_enter(regs);
282
283     irq_enable();
284
285     switch (regs->ah) {
286     case 0x00: handle_1700(regs); break;
287     case 0x01: handle_1701(regs); break;
288     case 0x02: handle_1702(regs); break;
289     default:   handle_17XX(regs); break;
290     }
291 }