1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * William Jolitz. 7 * 8 * %sccs.include.redist.c% 9 * 10 * @(#)kbd.c 7.3 (Berkeley) 04/28/91 11 */ 12 13 #define L 0x01 /* locking function */ 14 #define SHF 0x02 /* keyboard shift */ 15 #define ALT 0x04 /* alternate shift -- alternate chars */ 16 #define NUM 0x08 /* numeric shift cursors vs. numeric */ 17 #define CTL 0x10 /* control shift -- allows ctl function */ 18 #define CPS 0x20 /* caps shift -- swaps case of letter */ 19 #define ASCII 0x40 /* ascii code for this key */ 20 #define STP 0x80 /* stop output */ 21 22 typedef unsigned char u_char; 23 24 u_char inb(); 25 26 u_char action[] = { 27 0, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 0- 7 */ 28 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 8-15 */ 29 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 16-23 */ 30 ASCII, ASCII, ASCII, ASCII, ASCII, CTL, ASCII, ASCII, /* scan 24-31 */ 31 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 32-39 */ 32 ASCII, ASCII, SHF , ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 40-47 */ 33 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, SHF, ASCII, /* scan 48-55 */ 34 ALT, ASCII, CPS|L, 0, 0, ASCII, 0, 0, /* scan 56-63 */ 35 0, 0, 0, 0, 0, NUM|L, STP|L, ASCII, /* scan 64-71 */ 36 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 72-79 */ 37 ASCII, ASCII, ASCII, ASCII, 0, 0, 0, 0, /* scan 80-87 */ 38 0,0,0,0,0,0,0,0, 39 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 40 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ; 41 42 u_char unshift[] = { /* no shift */ 43 0, 033 , '1' , '2' , '3' , '4' , '5' , '6' , /* scan 0- 7 */ 44 '7' , '8' , '9' , '0' , '-' , '=' , 0177 ,'\t' , /* scan 8-15 */ 45 46 'q' , 'w' , 'e' , 'r' , 't' , 'y' , 'u' , 'i' , /* scan 16-23 */ 47 'o' , 'p' , '[' , ']' , '\r' , CTL , 'a' , 's' , /* scan 24-31 */ 48 49 'd' , 'f' , 'g' , 'h' , 'j' , 'k' , 'l' , ';' , /* scan 32-39 */ 50 '\'' , '`' , SHF , '\\' , 'z' , 'x' , 'c' , 'v' , /* scan 40-47 */ 51 52 'b' , 'n' , 'm' , ',' , '.' , '/' , SHF , '*', /* scan 48-55 */ 53 ALT , ' ' , CPS|L, 0, 0, ' ' , 0, 0, /* scan 56-63 */ 54 55 0, 0, 0, 0, 0, NUM|L, STP|L, '7', /* scan 64-71 */ 56 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 72-79 */ 57 58 '2', '3', '0', '.', 0, 0, 0, 0, /* scan 80-87 */ 59 0,0,0,0,0,0,0,0, 60 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 61 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ; 62 63 u_char shift[] = { /* shift shift */ 64 0, 033 , '!' , '@' , '#' , '$' , '%' , '^' , /* scan 0- 7 */ 65 '&' , '*' , '(' , ')' , '_' , '+' , 0177 ,'\t' , /* scan 8-15 */ 66 'Q' , 'W' , 'E' , 'R' , 'T' , 'Y' , 'U' , 'I' , /* scan 16-23 */ 67 'O' , 'P' , '[' , ']' , '\r' , CTL , 'A' , 'S' , /* scan 24-31 */ 68 'D' , 'F' , 'G' , 'H' , 'J' , 'K' , 'L' , ':' , /* scan 32-39 */ 69 '"' , '~' , SHF , '|' , 'Z' , 'X' , 'C' , 'V' , /* scan 40-47 */ 70 'B' , 'N' , 'M' , '<' , '>' , '?' , SHF , '*', /* scan 48-55 */ 71 ALT , ' ' , CPS|L, 0, 0, ' ' , 0, 0, /* scan 56-63 */ 72 0, 0, 0, 0, 0, NUM|L, STP|L, '7', /* scan 64-71 */ 73 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 72-79 */ 74 '2', '3', '0', '.', 0, 0, 0, 0, /* scan 80-87 */ 75 0,0,0,0,0,0,0,0, 76 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 77 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ; 78 79 u_char ctl[] = { /* CTL shift */ 80 0, 033 , '!' , 000 , '#' , '$' , '%' , 036 , /* scan 0- 7 */ 81 '&' , '*' , '(' , ')' , 037 , '+' , 034 ,'\177', /* scan 8-15 */ 82 021 , 027 , 005 , 022 , 024 , 031 , 025 , 011 , /* scan 16-23 */ 83 017 , 020 , 033 , 035 , '\r' , CTL , 001 , 013 , /* scan 24-31 */ 84 004 , 006 , 007 , 010 , 012 , 013 , 014 , ';' , /* scan 32-39 */ 85 '\'' , '`' , SHF , 034 , 032 , 030 , 003 , 026 , /* scan 40-47 */ 86 002 , 016 , 015 , '<' , '>' , '?' , SHF , '*', /* scan 48-55 */ 87 ALT , ' ' , CPS|L, 0, 0, ' ' , 0, 0, /* scan 56-63 */ 88 CPS|L, 0, 0, 0, 0, 0, 0, 0, /* scan 64-71 */ 89 0, 0, 0, 0, 0, 0, 0, 0, /* scan 72-79 */ 90 0, 0, 0, 0, 0, 0, 0, 0, /* scan 80-87 */ 91 0, 0, 033, '7' , '4' , '1' , 0, NUM|L, /* scan 88-95 */ 92 '8' , '5' , '2' , 0, STP|L, '9' , '6' , '3' , /*scan 96-103*/ 93 '.' , 0, '*' , '-' , '+' , 0, 0, 0, /*scan 104-111*/ 94 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ; 95 96 #ifdef notdef 97 struct key { 98 u_short action; /* how this key functions */ 99 char ascii[8]; /* ascii result character indexed by shifts */ 100 }; 101 #endif 102 103 u_char shfts, ctls, alts, caps, num, stp; 104 105 #define KBSTATP 0x64 /* kbd status port */ 106 #define KBS_INP_BUF_FUL 0x02 /* kbd char ready */ 107 #define KBDATAP 0x60 /* kbd data port */ 108 #define KBSTATUSPORT 0x61 /* kbd status */ 109 110 u_char odt; 111 112 u_char kbd() { 113 u_char dt, brk, act; 114 115 loop: 116 while(inb(0x64)&1 == 0); 117 dt = inb(0x60); 118 do { 119 while(inb(0x64)&1 == 0); 120 } while(dt == inb(0x60)); 121 odt = dt; 122 123 brk = dt & 0x80 ; dt = dt & 0x7f ; 124 125 act = action[dt]; 126 if (act&SHF) { 127 if(brk) shfts = 0; else shfts = 1; 128 } 129 if (act&ALT) { 130 if(brk) alts = 0; else alts = 1; 131 } 132 if (act&NUM) { 133 if (act&L) { 134 if(!brk) num ^= 1; 135 } else if(brk) num = 0; else num = 1; 136 } 137 if (act&CTL) { 138 if(brk) ctls = 0; else ctls = 1; 139 } 140 if (act&CPS) { 141 if (act&L) { 142 if(!brk) caps ^= 1; 143 } else if(brk) caps = 0; else caps = 1; 144 } 145 if (act&STP) { 146 if (act&L) { 147 if(!brk) stp ^= 1; 148 } else if(brk) stp = 0; else stp = 1; 149 } 150 if(ctl && alts && dt == 83) exit(); 151 if ((act&ASCII) && !brk) { 152 u_char chr; 153 154 if (shfts){ 155 chr = shift[dt] ; } else { 156 if (ctls) { 157 chr = ctl[dt] ; } else { 158 chr = unshift[dt] ; } } 159 if (caps && (chr >= 'a' && chr <= 'z')) { 160 chr -= 'a' - 'A' ; 161 } 162 /*do 163 while(inb(0x64)&1 == 0) ; 164 while (inb(0x60) == (chr | 0x80)); 165 while(inb(0x64)&1 == 1) inb(0x60);A*/ 166 return(chr); 167 } 168 goto loop; 169 } 170 171 scankbd() { 172 u_char c; 173 174 c = inb(0x60); 175 if (c == 0xaa) { odt = 0x2a; return (0); } 176 if (c == 0xfa) { odt = 0x7a; return (0); } 177 c &= 0x7f; 178 179 if ( (odt&0x7f) == c )return(0); 180 if(odt == 0) { odt = c; return(0); } 181 return(kbd()); 182 } 183 184 kbdreset() 185 { 186 u_char c; 187 188 /* Enable interrupts and keyboard controller */ 189 while (inb(0x64)&2); outb(0x64,0x60); 190 while (inb(0x64)&2); outb(0x60,0x4D); 191 192 /* Start keyboard stuff RESET */ 193 while (inb(0x64)&2); /* wait input ready */ 194 outb(0x60,0xFF); /* RESET */ 195 196 while((c=inb(0x60))!=0xFA) ; 197 198 /* While we are here, defeat gatea20 */ 199 while (inb(0x64)&2); /* wait input ready */ 200 outb(0x64,0xd1); 201 while (inb(0x64)&2); /* wait input ready */ 202 outb(0x60,0xdf); 203 } 204 205 u_char getchar() { 206 u_char c; 207 208 c = kbd(); 209 if (c == '\r') c = '\n'; 210 putchar(c); 211 return(c); 212 } 213 214 reset_cpu() { 215 216 while (inb(0x64)&2); /* wait input ready */ 217 outb(0x64,0xFE); /* Reset Command */ 218 wait(4000000); 219 /* NOTREACHED */ 220 } 221