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