added my responsibility for the `cpm' port
[unix-history] / sys / i386 / stand / kbd.c
CommitLineData
15637ed4
RG
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 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
e73885af
RG
36 * from: @(#)kbd.c 7.4 (Berkeley) 5/4/91
37 * $Id$
15637ed4
RG
38 */
39
40#define L 0x01 /* locking function */
41#define SHF 0x02 /* keyboard shift */
42#define ALT 0x04 /* alternate shift -- alternate chars */
43#define NUM 0x08 /* numeric shift cursors vs. numeric */
44#define CTL 0x10 /* control shift -- allows ctl function */
45#define CPS 0x20 /* caps shift -- swaps case of letter */
46#define ASCII 0x40 /* ascii code for this key */
47#define STP 0x80 /* stop output */
48
49typedef unsigned char u_char;
50
51u_char inb();
52
53#ifdef notdef
54u_char action[] = {
550, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 0- 7 */
56ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 8-15 */
57ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 16-23 */
58ASCII, ASCII, ASCII, ASCII, ASCII, CTL, ASCII, ASCII, /* scan 24-31 */
59ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 32-39 */
60ASCII, ASCII, SHF , ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 40-47 */
61ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, SHF, ASCII, /* scan 48-55 */
62 ALT, ASCII, CPS|L, 0, 0, ASCII, 0, 0, /* scan 56-63 */
63 0, 0, 0, 0, 0, NUM|L, STP|L, ASCII, /* scan 64-71 */
64ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, /* scan 72-79 */
65ASCII, ASCII, ASCII, ASCII, 0, 0, 0, 0, /* scan 80-87 */
660,0,0,0,0,0,0,0,
670,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
680,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ;
69
70u_char unshift[] = { /* no shift */
710, 033 , '1' , '2' , '3' , '4' , '5' , '6' , /* scan 0- 7 */
72'7' , '8' , '9' , '0' , '-' , '=' , 0177 ,'\t' , /* scan 8-15 */
73
74'q' , 'w' , 'e' , 'r' , 't' , 'y' , 'u' , 'i' , /* scan 16-23 */
75'o' , 'p' , '[' , ']' , '\r' , CTL , 'a' , 's' , /* scan 24-31 */
76
77'd' , 'f' , 'g' , 'h' , 'j' , 'k' , 'l' , ';' , /* scan 32-39 */
78'\'' , '`' , SHF , '\\' , 'z' , 'x' , 'c' , 'v' , /* scan 40-47 */
79
80'b' , 'n' , 'm' , ',' , '.' , '/' , SHF , '*', /* scan 48-55 */
81ALT , ' ' , CPS|L, 0, 0, ' ' , 0, 0, /* scan 56-63 */
82
83 0, 0, 0, 0, 0, NUM|L, STP|L, '7', /* scan 64-71 */
84 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 72-79 */
85
86 '2', '3', '0', '.', 0, 0, 0, 0, /* scan 80-87 */
870,0,0,0,0,0,0,0,
880,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
890,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ;
90
91u_char shift[] = { /* shift shift */
920, 033 , '!' , '@' , '#' , '$' , '%' , '^' , /* scan 0- 7 */
93'&' , '*' , '(' , ')' , '_' , '+' , 0177 ,'\t' , /* scan 8-15 */
94'Q' , 'W' , 'E' , 'R' , 'T' , 'Y' , 'U' , 'I' , /* scan 16-23 */
95'O' , 'P' , '[' , ']' , '\r' , CTL , 'A' , 'S' , /* scan 24-31 */
96'D' , 'F' , 'G' , 'H' , 'J' , 'K' , 'L' , ':' , /* scan 32-39 */
97'"' , '~' , SHF , '|' , 'Z' , 'X' , 'C' , 'V' , /* scan 40-47 */
98'B' , 'N' , 'M' , '<' , '>' , '?' , SHF , '*', /* scan 48-55 */
99ALT , ' ' , CPS|L, 0, 0, ' ' , 0, 0, /* scan 56-63 */
100 0, 0, 0, 0, 0, NUM|L, STP|L, '7', /* scan 64-71 */
101 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 72-79 */
102 '2', '3', '0', '.', 0, 0, 0, 0, /* scan 80-87 */
1030,0,0,0,0,0,0,0,
1040,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
1050,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ;
106
107u_char ctl[] = { /* CTL shift */
1080, 033 , '!' , 000 , '#' , '$' , '%' , 036 , /* scan 0- 7 */
109'&' , '*' , '(' , ')' , 037 , '+' , 034 ,'\177', /* scan 8-15 */
110021 , 027 , 005 , 022 , 024 , 031 , 025 , 011 , /* scan 16-23 */
111017 , 020 , 033 , 035 , '\r' , CTL , 001 , 013 , /* scan 24-31 */
112004 , 006 , 007 , 010 , 012 , 013 , 014 , ';' , /* scan 32-39 */
113'\'' , '`' , SHF , 034 , 032 , 030 , 003 , 026 , /* scan 40-47 */
114002 , 016 , 015 , '<' , '>' , '?' , SHF , '*', /* scan 48-55 */
115ALT , ' ' , CPS|L, 0, 0, ' ' , 0, 0, /* scan 56-63 */
116CPS|L, 0, 0, 0, 0, 0, 0, 0, /* scan 64-71 */
117 0, 0, 0, 0, 0, 0, 0, 0, /* scan 72-79 */
118 0, 0, 0, 0, 0, 0, 0, 0, /* scan 80-87 */
119 0, 0, 033, '7' , '4' , '1' , 0, NUM|L, /* scan 88-95 */
120'8' , '5' , '2' , 0, STP|L, '9' , '6' , '3' , /*scan 96-103*/
121'.' , 0, '*' , '-' , '+' , 0, 0, 0, /*scan 104-111*/
1220,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, } ;
123
124#ifdef notdef
125struct key {
126 u_short action; /* how this key functions */
127 char ascii[8]; /* ascii result character indexed by shifts */
128};
129#endif
130
131u_char shfts, ctls, alts, caps, num, stp;
132#endif
133
134#define KBSTATP 0x64 /* kbd status port */
135#define KBS_INP_BUF_FUL 0x02 /* kbd char ready */
136#define KBDATAP 0x60 /* kbd data port */
137#define KBSTATUSPORT 0x61 /* kbd status */
138
139u_char odt, bdt;
140
141u_char kbd() {
142 u_char dt, brk, act;
143
144loop:
145 while(inb(0x64)&1 == 0);
146 dt = inb(0x60);
147 do {
148 while(inb(0x64)&1 == 0);
149 } while(dt == inb(0x60));
150 odt = dt;
151
152 brk = dt & 0x80 ; dt = dt & 0x7f ;
153
154#ifdef notdef
155 act = action[dt];
156 if (act&SHF) {
157 if(brk) shfts = 0; else shfts = 1;
158 }
159 if (act&ALT) {
160 if(brk) alts = 0; else alts = 1;
161 }
162 if (act&NUM) {
163 if (act&L) {
164 if(!brk) num ^= 1;
165 } else if(brk) num = 0; else num = 1;
166 }
167 if (act&CTL) {
168 if(brk) ctls = 0; else ctls = 1;
169 }
170 if (act&CPS) {
171 if (act&L) {
172 if(!brk) caps ^= 1;
173 } else if(brk) caps = 0; else caps = 1;
174 }
175 if (act&STP) {
176 if (act&L) {
177 if(!brk) stp ^= 1;
178 } else if(brk) stp = 0; else stp = 1;
179 }
180 if(ctls && alts && dt == 83) exit();
181 if ((act&ASCII) && !brk) {
182 u_char chr;
183
184 if (shfts){
185 chr = shift[dt] ; } else {
186 if (ctls) {
187 chr = ctl[dt] ; } else {
188 chr = unshift[dt] ; } }
189 if (caps && (chr >= 'a' && chr <= 'z')) {
190 chr -= 'a' - 'A' ;
191 }
192 /*do
193 while(inb(0x64)&1 == 0) ;
194 while (inb(0x60) == (chr | 0x80));
195 while(inb(0x64)&1 == 1) inb(0x60);A*/
196 return(chr);
197 }
198#else
199 if (brk)
200 return(1);
201#endif
202 goto loop;
203}
204
205scankbd() {
206u_char c;
207
208#ifdef notdef
209 c = inb(0x60);
210 if (c == 83) exit();
211 /*if (c == 0xaa) return (0);
212 if (c == 0xfa) return (0);*/
213
214 if (bdt == 0) { bdt = c&0x7f; return(0); }
215
216 if(odt) return(1);
217
218 c &= 0x7f;
219
220 if (bdt == c) return(0);
221 odt = c;
222#endif
223 return(1);
224}
225
226kbdreset()
227{
228 u_char c;
229
230 /* Enable interrupts and keyboard controller */
231 while (inb(0x64)&2); outb(0x64,0x60);
232 while (inb(0x64)&2); outb(0x60,0x4D);
233
234 /* Start keyboard stuff RESET */
235 while (inb(0x64)&2); /* wait input ready */
236 outb(0x60,0xFF); /* RESET */
237
238 while((c=inb(0x60))!=0xFA) ;
239
240 /* While we are here, defeat gatea20 */
241 while (inb(0x64)&2); /* wait input ready */
242 outb(0x64,0xd1);
243 while (inb(0x64)&2); /* wait input ready */
244 outb(0x60,0xdf);
245 while (inb(0x64)&2); /* wait input ready */
246 odt = bdt = 0;
247 inb(0x60);
248}
249
250#ifdef notdef;
251u_char getchar() {
252 u_char c;
253
254 c = kbd();
255 if (c == '\b' || c == '\177') return(c);
256 if (c == '\r') c = '\n';
257 putchar(c);
258 return(c);
259}
260#endif
261
262reset_cpu() {
263
264 while (inb(0x64)&2); /* wait input ready */
265 outb(0x64,0xFE); /* Reset Command */
266 wait(4000000);
267 /* NOTREACHED */
268}