Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 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 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 11 | |
| 12 | /**************************************************************** |
| 13 | * COM ports |
| 14 | ****************************************************************/ |
| 15 | |
| 16 | static u16 |
| 17 | getComAddr(struct bregs *regs) |
| 18 | { |
| 19 | if (regs->dx >= 4) { |
| 20 | set_cf(regs, 1); |
| 21 | return 0; |
| 22 | } |
| 23 | u16 addr = GET_BDA(port_com[regs->dx]); |
| 24 | if (! addr) |
| 25 | set_cf(regs, 1); |
| 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_cf(regs, 0); |
| 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_cf(regs, 0); |
| 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_cf(regs, 0); |
| 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_cf(regs, 0); |
| 106 | } |
| 107 | |
| 108 | static void |
| 109 | handle_14XX(struct bregs *regs) |
| 110 | { |
| 111 | // Unsupported |
| 112 | set_cf(regs, 1); |
| 113 | } |
| 114 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 115 | // INT 14h Serial Communications Service Entry Point |
Kevin O'Connor | 1978676 | 2008-03-05 21:09:59 -0500 | [diff] [blame^] | 116 | void VISIBLE16 |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 117 | handle_14(struct bregs *regs) |
| 118 | { |
| 119 | debug_enter(regs); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 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 | debug_exit(regs); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /**************************************************************** |
| 135 | * LPT ports |
| 136 | ****************************************************************/ |
| 137 | |
| 138 | static u16 |
| 139 | getLptAddr(struct bregs *regs) |
| 140 | { |
| 141 | if (regs->dx >= 3) { |
| 142 | set_cf(regs, 1); |
| 143 | return 0; |
| 144 | } |
| 145 | u16 addr = GET_BDA(port_lpt[regs->dx]); |
| 146 | if (! addr) |
| 147 | set_cf(regs, 1); |
| 148 | return addr; |
| 149 | } |
| 150 | |
| 151 | static void |
| 152 | lpt_ret(struct bregs *regs, u16 addr, u16 timeout) |
| 153 | { |
| 154 | u8 val8 = inb(addr+1); |
| 155 | regs->ah = (val8 ^ 0x48); |
| 156 | if (!timeout) |
| 157 | regs->ah |= 0x01; |
| 158 | set_cf(regs, 0); |
| 159 | } |
| 160 | |
| 161 | // INT 17 - PRINTER - WRITE CHARACTER |
| 162 | static void |
| 163 | handle_1700(struct bregs *regs) |
| 164 | { |
| 165 | u16 addr = getLptAddr(regs); |
| 166 | if (!addr) |
| 167 | return; |
| 168 | u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8; |
| 169 | |
| 170 | outb(regs->al, addr); |
| 171 | u8 val8 = inb(addr+2); |
| 172 | outb(val8 | 0x01, addr+2); // send strobe |
| 173 | nop(); |
| 174 | outb(val8 & ~0x01, addr+2); |
| 175 | while (((inb(addr+1) & 0x40) == 0x40) && (timeout)) |
| 176 | timeout--; |
| 177 | |
| 178 | lpt_ret(regs, addr, timeout); |
| 179 | } |
| 180 | |
| 181 | // INT 17 - PRINTER - INITIALIZE PORT |
| 182 | static void |
| 183 | handle_1701(struct bregs *regs) |
| 184 | { |
| 185 | u16 addr = getLptAddr(regs); |
| 186 | if (!addr) |
| 187 | return; |
| 188 | u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8; |
| 189 | |
| 190 | u8 val8 = inb(addr+2); |
| 191 | outb(val8 & ~0x04, addr+2); // send init |
| 192 | nop(); |
| 193 | outb(val8 | 0x04, addr+2); |
| 194 | |
| 195 | lpt_ret(regs, addr, timeout); |
| 196 | } |
| 197 | |
| 198 | // INT 17 - PRINTER - GET STATUS |
| 199 | static void |
| 200 | handle_1702(struct bregs *regs) |
| 201 | { |
| 202 | u16 addr = getLptAddr(regs); |
| 203 | if (!addr) |
| 204 | return; |
| 205 | u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8; |
| 206 | |
| 207 | lpt_ret(regs, addr, timeout); |
| 208 | } |
| 209 | |
| 210 | static void |
| 211 | handle_17XX(struct bregs *regs) |
| 212 | { |
| 213 | // Unsupported |
| 214 | set_cf(regs, 1); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // INT17h : Printer Service Entry Point |
Kevin O'Connor | 1978676 | 2008-03-05 21:09:59 -0500 | [diff] [blame^] | 218 | void VISIBLE16 |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 219 | handle_17(struct bregs *regs) |
| 220 | { |
| 221 | debug_enter(regs); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 222 | |
| 223 | irq_enable(); |
| 224 | |
| 225 | switch (regs->ah) { |
| 226 | case 0x00: handle_1700(regs); break; |
| 227 | case 0x01: handle_1701(regs); break; |
| 228 | case 0x02: handle_1702(regs); break; |
| 229 | default: handle_17XX(regs); break; |
| 230 | } |
| 231 | debug_exit(regs); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 232 | } |