kbd.c revision 1.1 1 /* $Id: kbd.c,v 1.1 1998/10/26 00:45:47 sakamoto Exp $ */
2
3 /*-
4 * Copyright (C) 1995-1997 Gary Thomas (gdt (at) linuxppc.org)
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Gary Thomas.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #if defined(CONS_BE) || defined(CONS_VGA)
34 /*
35 * Keyboard handler
36 */
37 #define L 0x0001 /* locking function */
38 #define SHF 0x0002 /* keyboard shift */
39 #define ALT 0x0004 /* alternate shift -- alternate chars */
40 #define NUM 0x0008 /* numeric shift cursors vs. numeric */
41 #define CTL 0x0010 /* control shift -- allows ctl function */
42 #define CPS 0x0020 /* caps shift -- swaps case of letter */
43 #define ASCII 0x0040 /* ascii code for this key */
44 #define STP 0x0080 /* stop output */
45 #define FUNC 0x0100 /* function key */
46 #define SCROLL 0x0200 /* scroll lock key */
47
48 #include "pcconstab.US"
49
50 unsigned char shfts, ctls, alts, caps, num, stp;
51
52 #define KBDATAP 0x60 /* kbd data port */
53 #define KBSTATUSPORT 0x61 /* kbd status */
54 #define KBSTATP 0x64 /* kbd status port */
55 #define KBINRDY 0x01
56 #define KBOUTRDY 0x02
57
58 #define _x__ 0x00 /* Unknown / unmapped */
59
60 const unsigned char keycode[] = {
61 _x__, 0x43, 0x41, 0x3F, 0x3D, 0x3B, 0x3C, _x__, /* 0x00-0x07 */
62 _x__, 0x44, 0x42, 0x40, 0x3E, 0x0F, 0x29, _x__, /* 0x08-0x0F */
63 _x__, 0x38, 0x2A, _x__, 0x1D, 0x10, 0x02, _x__, /* 0x10-0x17 */
64 _x__, _x__, 0x2C, 0x1F, 0x1E, 0x11, 0x03, _x__, /* 0x18-0x1F */
65 _x__, 0x2E, 0x2D, 0x20, 0x12, 0x05, 0x04, _x__, /* 0x20-0x27 */
66 _x__, 0x39, 0x2F, 0x21, 0x14, 0x13, 0x06, _x__, /* 0x28-0x2F */
67 _x__, 0x31, 0x30, 0x23, 0x22, 0x15, 0x07, _x__, /* 0x30-0x37 */
68 _x__, _x__, 0x32, 0x24, 0x16, 0x08, 0x09, _x__, /* 0x38-0x3F */
69 _x__, 0x33, 0x25, 0x17, 0x18, 0x0B, 0x0A, _x__, /* 0x40-0x47 */
70 _x__, 0x34, 0x35, 0x26, 0x27, 0x19, 0x0C, _x__, /* 0x48-0x4F */
71 _x__, _x__, 0x28, _x__, 0x1A, 0x0D, _x__, _x__, /* 0x50-0x57 */
72 0x3A, 0x36, 0x1C, 0x1B, _x__, 0x2B, _x__, _x__, /* 0x58-0x5F */
73 _x__, _x__, _x__, _x__, _x__, _x__, 0x0E, _x__, /* 0x60-0x67 */
74 _x__, 0x4F, _x__, 0x4B, 0x47, _x__, _x__, _x__, /* 0x68-0x6F */
75 0x52, 0x53, 0x50, 0x4C, 0x4D, 0x48, 0x01, 0x45, /* 0x70-0x77 */
76 _x__, 0x4E, 0x51, 0x4A, _x__, 0x49, 0x46, 0x54, /* 0x78-0x7F */
77 };
78
79 static NOP() {}
80
81 int
82 kbd(noblock)
83 int noblock;
84 {
85 unsigned char dt, brk, act;
86 int first = 1;
87 loop:
88 if (noblock) {
89 if ((inb(KBSTATP) & KBINRDY) == 0)
90 return (-1);
91 } else while((inb(KBSTATP) & KBINRDY) == 0) ;
92
93 dt = inb(KBDATAP);
94
95 brk = dt & 0x80; /* brk == 1 on key release */
96 dt = dt & 0x7f; /* keycode */
97
98 act = action[dt];
99 if (act&SHF)
100 shfts = brk ? 0 : 1;
101 if (act&ALT)
102 alts = brk ? 0 : 1;
103 if (act&NUM)
104 if (act&L) {
105 /* NUM lock */
106 if(!brk)
107 num = !num;
108 } else
109 num = brk ? 0 : 1;
110 if (act&CTL)
111 ctls = brk ? 0 : 1;
112 if (act&CPS)
113 if (act&L) {
114 /* CAPS lock */
115 if(!brk)
116 caps = !caps;
117 } else
118 caps = brk ? 0 : 1;
119 if (act&STP)
120 if (act&L) {
121 if(!brk)
122 stp = !stp;
123 } else
124 stp = brk ? 0 : 1;
125
126 if ((act&ASCII) && !brk) {
127 unsigned char chr;
128
129 if (shfts)
130 chr = shift[dt];
131 else if (ctls)
132 chr = ctl[dt];
133 else
134 chr = unshift[dt];
135
136 if (alts)
137 chr |= 0x80;
138
139 if (caps && (chr >= 'a' && chr <= 'z'))
140 chr -= 'a' - 'A' ;
141 #define CTRL(s) (s & 0x1F)
142 if ((chr == '\r') || (chr == '\n') ||
143 (chr == CTRL('A')) || (chr == CTRL('S'))) {
144
145 /* Wait for key up */
146 while (1)
147 {
148 while((inb(KBSTATP) & KBINRDY) == 0) ;
149 dt = inb(KBDATAP);
150 if (dt & 0x80) /* key up */ break;
151 }
152 }
153 return (chr);
154 }
155
156 if (first && brk)
157 return (0); /* Ignore initial 'key up' codes */
158 goto loop;
159 }
160
161 kbdreset()
162 {
163 unsigned char c;
164 int i;
165
166 /* Send self-test */
167 while (inb(KBSTATP) & KBOUTRDY) ;
168 outb(KBSTATP,0xAA);
169 while ((inb(KBSTATP) & KBINRDY) == 0) ; /* wait input ready */
170 if ((c = inb(KBDATAP)) != 0x55) {
171 printf("Keyboard self test failed - result: %x\n", c);
172 }
173 /* Enable interrupts and keyboard controller */
174 while (inb(KBSTATP) & KBOUTRDY) ;
175 outb(KBSTATP,0x60);
176 while (inb(KBSTATP) & KBOUTRDY) ;
177 outb(KBDATAP,0x45);
178 for (i = 0; i < 10000; i++) NOP();
179 while (inb(KBSTATP) & KBOUTRDY) ;
180 outb(KBSTATP,0xAE);
181 }
182
183 kbd_getc()
184 {
185 int c;
186 while ((c = kbd(0)) == 0)
187 ;
188 return (c);
189 }
190
191 kbd_test()
192 {
193 return ((inb(KBSTATP) & KBINRDY) != 0);
194 }
195 #endif /* CONS_BE || CONS_VGA*/
196