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