blob: f9988a3e0d65f6b6b8a4e87a557692767b435ba5 [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// 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
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050011
12/****************************************************************
13 * COM ports
14 ****************************************************************/
15
16static u16
Kevin O'Connor913cc2e2008-04-13 17:31:45 -040017detect_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
30void
31serial_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
44static u16
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050045getComAddr(struct bregs *regs)
46{
47 if (regs->dx >= 4) {
Kevin O'Connor6c781222008-03-09 12:19:23 -040048 set_fail(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050049 return 0;
50 }
51 u16 addr = GET_BDA(port_com[regs->dx]);
52 if (! addr)
Kevin O'Connor6c781222008-03-09 12:19:23 -040053 set_fail(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050054 return addr;
55}
56
Kevin O'Connor1812e202008-05-07 21:29:50 -040057// SERIAL - INITIALIZE PORT
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050058static void
59handle_1400(struct bregs *regs)
60{
61 u16 addr = getComAddr(regs);
62 if (!addr)
63 return;
64 outb(inb(addr+3) | 0x80, addr+3);
65 if ((regs->al & 0xE0) == 0) {
66 outb(0x17, addr);
67 outb(0x04, addr+1);
68 } else {
69 u16 val16 = 0x600 >> ((regs->al & 0xE0) >> 5);
70 outb(val16 & 0xFF, addr);
71 outb(val16 >> 8, addr+1);
72 }
73 outb(regs->al & 0x1F, addr+3);
74 regs->ah = inb(addr+5);
75 regs->al = inb(addr+6);
Kevin O'Connor6c781222008-03-09 12:19:23 -040076 set_success(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050077}
78
Kevin O'Connor1812e202008-05-07 21:29:50 -040079// SERIAL - WRITE CHARACTER TO PORT
80static void
81handle_1401(struct bregs *regs)
82{
83 u16 addr = getComAddr(regs);
84 if (!addr)
85 return;
Kevin O'Connor5c732402008-06-07 10:43:07 -040086 u16 timer = GET_BDA(timer_counter);
Kevin O'Connor1812e202008-05-07 21:29:50 -040087 u16 timeout = GET_BDA(com_timeout[regs->dx]);
Kevin O'Connor5c732402008-06-07 10:43:07 -040088 while (((inb(addr+5) & 0x60) != 0x60) && (timeout)) {
89 u16 val16 = GET_BDA(timer_counter);
90 if (val16 != timer) {
91 timer = val16;
92 timeout--;
93 }
94 }
95 if (timeout)
96 outb(regs->al, addr);
Kevin O'Connor1812e202008-05-07 21:29:50 -040097 regs->ah = inb(addr+5);
Kevin O'Connor5c732402008-06-07 10:43:07 -040098 if (!timeout)
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050099 regs->ah |= 0x80;
Kevin O'Connor6c781222008-03-09 12:19:23 -0400100 set_success(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500101}
102
Kevin O'Connor1812e202008-05-07 21:29:50 -0400103// SERIAL - READ CHARACTER FROM PORT
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500104static void
105handle_1402(struct bregs *regs)
106{
107 u16 addr = getComAddr(regs);
108 if (!addr)
109 return;
110 u16 timer = GET_BDA(timer_counter);
111 u16 timeout = GET_BDA(com_timeout[regs->dx]);
112 while (((inb(addr+5) & 0x01) == 0) && (timeout)) {
113 u16 val16 = GET_BDA(timer_counter);
114 if (val16 != timer) {
115 timer = val16;
116 timeout--;
117 }
118 }
119 if (timeout) {
120 regs->ah = 0;
121 regs->al = inb(addr);
122 } else {
123 regs->ah = inb(addr+5);
124 }
Kevin O'Connor6c781222008-03-09 12:19:23 -0400125 set_success(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500126}
127
Kevin O'Connor1812e202008-05-07 21:29:50 -0400128// SERIAL - GET PORT STATUS
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500129static void
130handle_1403(struct bregs *regs)
131{
132 u16 addr = getComAddr(regs);
133 if (!addr)
134 return;
135 regs->ah = inb(addr+5);
136 regs->al = inb(addr+6);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400137 set_success(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500138}
139
140static void
141handle_14XX(struct bregs *regs)
142{
143 // Unsupported
Kevin O'Connor6c781222008-03-09 12:19:23 -0400144 set_fail(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500145}
146
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500147// INT 14h Serial Communications Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500148void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500149handle_14(struct bregs *regs)
150{
151 debug_enter(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500152
153 irq_enable();
154
155 switch (regs->ah) {
156 case 0x00: handle_1400(regs); break;
157 case 0x01: handle_1401(regs); break;
158 case 0x02: handle_1402(regs); break;
159 case 0x03: handle_1403(regs); break;
160 default: handle_14XX(regs); break;
161 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500162}
163
164
165/****************************************************************
166 * LPT ports
167 ****************************************************************/
168
169static u16
Kevin O'Connor913cc2e2008-04-13 17:31:45 -0400170detect_parport(u16 port, u8 timeout, u8 count)
171{
172 // clear input mode
173 outb(inb(port+2) & 0xdf, port+2);
174
175 outb(0xaa, port);
176 if (inb(port) != 0xaa)
177 // Not present
178 return 0;
179 SET_BDA(port_lpt[count], port);
180 SET_BDA(lpt_timeout[count], timeout);
181 return 1;
182}
183
184void
185lpt_setup()
186{
187 u16 count = 0;
188 count += detect_parport(0x378, 0x14, count);
189 count += detect_parport(0x278, 0x14, count);
190
191 // Equipment word bits 14..15 determing # parallel ports
192 u16 eqb = GET_BDA(equipment_list_flags);
193 SET_BDA(equipment_list_flags, (eqb & 0x3fff) | (count << 14));
194}
195
196static u16
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500197getLptAddr(struct bregs *regs)
198{
199 if (regs->dx >= 3) {
Kevin O'Connor6c781222008-03-09 12:19:23 -0400200 set_fail(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500201 return 0;
202 }
203 u16 addr = GET_BDA(port_lpt[regs->dx]);
204 if (! addr)
Kevin O'Connor6c781222008-03-09 12:19:23 -0400205 set_fail(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500206 return addr;
207}
208
209static void
210lpt_ret(struct bregs *regs, u16 addr, u16 timeout)
211{
212 u8 val8 = inb(addr+1);
213 regs->ah = (val8 ^ 0x48);
214 if (!timeout)
215 regs->ah |= 0x01;
Kevin O'Connor6c781222008-03-09 12:19:23 -0400216 set_success(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500217}
218
219// INT 17 - PRINTER - WRITE CHARACTER
220static void
221handle_1700(struct bregs *regs)
222{
223 u16 addr = getLptAddr(regs);
224 if (!addr)
225 return;
226 u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
227
228 outb(regs->al, addr);
229 u8 val8 = inb(addr+2);
230 outb(val8 | 0x01, addr+2); // send strobe
231 nop();
232 outb(val8 & ~0x01, addr+2);
233 while (((inb(addr+1) & 0x40) == 0x40) && (timeout))
234 timeout--;
235
236 lpt_ret(regs, addr, timeout);
237}
238
239// INT 17 - PRINTER - INITIALIZE PORT
240static void
241handle_1701(struct bregs *regs)
242{
243 u16 addr = getLptAddr(regs);
244 if (!addr)
245 return;
246 u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
247
248 u8 val8 = inb(addr+2);
249 outb(val8 & ~0x04, addr+2); // send init
250 nop();
251 outb(val8 | 0x04, addr+2);
252
253 lpt_ret(regs, addr, timeout);
254}
255
256// INT 17 - PRINTER - GET STATUS
257static void
258handle_1702(struct bregs *regs)
259{
260 u16 addr = getLptAddr(regs);
261 if (!addr)
262 return;
263 u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
264
265 lpt_ret(regs, addr, timeout);
266}
267
268static void
269handle_17XX(struct bregs *regs)
270{
271 // Unsupported
Kevin O'Connor6c781222008-03-09 12:19:23 -0400272 set_fail(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500273}
274
275// INT17h : Printer Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500276void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500277handle_17(struct bregs *regs)
278{
279 debug_enter(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500280
281 irq_enable();
282
283 switch (regs->ah) {
284 case 0x00: handle_1700(regs); break;
285 case 0x01: handle_1701(regs); break;
286 case 0x02: handle_1702(regs); break;
287 default: handle_17XX(regs); break;
288 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500289}