2 * (C) Copyright 2002 ELTEC Elektronik AG
3 * Frank Gottschling <fgottschling@eltec.de>
5 * SPDX-License-Identifier: GPL-2.0+
8 /* i8042.c - Intel 8042 keyboard driver routines */
18 #define out8(p, v) outb(v, p)
22 static int kbd_input = -1; /* no input yet */
23 static int kbd_mapping = KBD_US; /* default US keyboard */
24 static int kbd_flags = NORMAL; /* after reset */
25 static int kbd_state; /* unshift code */
27 static unsigned char kbd_fct_map[144] = {
28 /* kbd_fct_map table for scan code */
29 0, AS, AS, AS, AS, AS, AS, AS, /* scan 00-07 */
30 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 08-0F */
31 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 10-17 */
32 AS, AS, AS, AS, AS, CN, AS, AS, /* scan 18-1F */
33 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 20-27 */
34 AS, AS, SH, AS, AS, AS, AS, AS, /* scan 28-2F */
35 AS, AS, AS, AS, AS, AS, SH, AS, /* scan 30-37 */
36 AS, AS, CP, 0, 0, 0, 0, 0, /* scan 38-3F */
37 0, 0, 0, 0, 0, NM, ST, ES, /* scan 40-47 */
38 ES, ES, ES, ES, ES, ES, ES, ES, /* scan 48-4F */
39 ES, ES, ES, ES, 0, 0, AS, 0, /* scan 50-57 */
40 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
41 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
42 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
43 AS, 0, 0, AS, 0, 0, AS, 0, /* scan 70-77 */
44 0, AS, 0, 0, 0, AS, 0, 0, /* scan 78-7F */
45 AS, CN, AS, AS, AK, ST, EX, EX, /* enhanced */
46 AS, EX, EX, AS, EX, AS, EX, EX /* enhanced */
49 static unsigned char kbd_key_map[2][5][144] = {
52 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 00-07 */
53 '7', '8', '9', '0', '-', '=', 0x08, '\t', /* scan 08-0F */
54 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', /* scan 10-17 */
55 'o', 'p', '[', ']', '\r', CN, 'a', 's', /* scan 18-1F */
56 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* scan 20-27 */
57 '\'', '`', SH, '\\', 'z', 'x', 'c', 'v', /* scan 28-2F */
58 'b', 'n', 'm', ',', '.', '/', SH, '*', /* scan 30-37 */
59 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
60 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
61 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
62 '2', '3', '0', '.', 0, 0, 0, 0, /* scan 50-57 */
63 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
64 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
65 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
66 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
67 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
68 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
69 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
72 0, 0x1b, '!', '@', '#', '$', '%', '^', /* scan 00-07 */
73 '&', '*', '(', ')', '_', '+', 0x08, '\t', /* scan 08-0F */
74 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', /* scan 10-17 */
75 'O', 'P', '{', '}', '\r', CN, 'A', 'S', /* scan 18-1F */
76 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', /* scan 20-27 */
77 '"', '~', SH, '|', 'Z', 'X', 'C', 'V', /* scan 28-2F */
78 'B', 'N', 'M', '<', '>', '?', SH, '*', /* scan 30-37 */
79 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
80 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
81 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
82 '2', '3', '0', '.', 0, 0, 0, 0, /* scan 50-57 */
83 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
84 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
85 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
86 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
87 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
88 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
89 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
92 0xff, 0x1b, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, /* scan 00-07 */
93 0x1e, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, '\t', /* scan 08-0F */
94 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, /* scan 10-17 */
95 0x0f, 0x10, 0x1b, 0x1d, '\r', CN, 0x01, 0x13, /* scan 18-1F */
96 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0xff, /* scan 20-27 */
97 0xff, 0x1c, SH, 0xff, 0x1a, 0x18, 0x03, 0x16, /* scan 28-2F */
98 0x02, 0x0e, 0x0d, 0xff, 0xff, 0xff, SH, 0xff, /* scan 30-37 */
99 0xff, 0xff, CP, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
100 0xff, 0xff, 0xff, 0xff, 0xff, NM, ST, 0xff, /* scan 40-47 */
101 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
103 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
104 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
105 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
106 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
107 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
108 '\r', CN, '/', '*', ' ', ST, 0xff, 0xff, /* extended */
109 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
111 { /* non numeric code */
112 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 00-07 */
113 '7', '8', '9', '0', '-', '=', 0x08, '\t', /* scan 08-0F */
114 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', /* scan 10-17 */
115 'o', 'p', '[', ']', '\r', CN, 'a', 's', /* scan 18-1F */
116 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* scan 20-27 */
117 '\'', '`', SH, '\\', 'z', 'x', 'c', 'v', /* scan 28-2F */
118 'b', 'n', 'm', ',', '.', '/', SH, '*', /* scan 30-37 */
119 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
120 0, 0, 0, 0, 0, NM, ST, 'w', /* scan 40-47 */
121 'x', 'y', 'l', 't', 'u', 'v', 'm', 'q', /* scan 48-4F */
122 'r', 's', 'p', 'n', 0, 0, 0, 0, /* scan 50-57 */
123 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
124 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
125 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
126 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
127 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
128 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
129 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
131 { /* right alt mode - not used in US keyboard */
132 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 00-07 */
133 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 08-0F */
134 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10-17 */
135 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 18-1F */
136 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20-27 */
137 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28-2F */
138 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30-37 */
139 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
140 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40-47 */
141 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
142 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
143 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
144 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
145 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
146 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
147 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
148 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* extended */
149 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
152 { /* German keyboard */
154 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 00-07 */
155 '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 08-0F */
156 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
157 'o', 'p', 0x81, '+', '\r', CN, 'a', 's', /* scan 18-1F */
158 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
159 0x84, '^', SH, '#', 'y', 'x', 'c', 'v', /* scan 28-2F */
160 'b', 'n', 'm', ',', '.', '-', SH, '*', /* scan 30-37 */
161 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
162 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
163 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
164 '2', '3', '0', ',', 0, 0, '<', 0, /* scan 50-57 */
165 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
166 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
167 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
168 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
169 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
170 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
171 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
174 0, 0x1b, '!', '"', 0x15, '$', '%', '&', /* scan 00-07 */
175 '/', '(', ')', '=', '?', '`', 0x08, '\t', /* scan 08-0F */
176 'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', /* scan 10-17 */
177 'O', 'P', 0x9a, '*', '\r', CN, 'A', 'S', /* scan 18-1F */
178 'D', 'F', 'G', 'H', 'J', 'K', 'L', 0x99, /* scan 20-27 */
179 0x8e, 0xf8, SH, '\'', 'Y', 'X', 'C', 'V', /* scan 28-2F */
180 'B', 'N', 'M', ';', ':', '_', SH, '*', /* scan 30-37 */
181 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
182 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
183 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
184 '2', '3', '0', ',', 0, 0, '>', 0, /* scan 50-57 */
185 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
186 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
187 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
188 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
189 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
190 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
191 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
194 0xff, 0x1b, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, /* scan 00-07 */
195 0x1e, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, '\t', /* scan 08-0F */
196 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, /* scan 10-17 */
197 0x0f, 0x10, 0x1b, 0x1d, '\r', CN, 0x01, 0x13, /* scan 18-1F */
198 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0xff, /* scan 20-27 */
199 0xff, 0x1c, SH, 0xff, 0x1a, 0x18, 0x03, 0x16, /* scan 28-2F */
200 0x02, 0x0e, 0x0d, 0xff, 0xff, 0xff, SH, 0xff, /* scan 30-37 */
201 0xff, 0xff, CP, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
202 0xff, 0xff, 0xff, 0xff, 0xff, NM, ST, 0xff, /* scan 40-47 */
203 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
204 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
205 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
206 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
207 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
208 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
209 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
210 '\r', CN, '/', '*', ' ', ST, 0xff, 0xff, /* extended */
211 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
213 { /* non numeric code */
214 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 00-07 */
215 '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 08-0F */
216 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
217 'o', 'p', 0x81, '+', '\r', CN, 'a', 's', /* scan 18-1F */
218 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
219 0x84, '^', SH, 0, 'y', 'x', 'c', 'v', /* scan 28-2F */
220 'b', 'n', 'm', ',', '.', '-', SH, '*', /* scan 30-37 */
221 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
222 0, 0, 0, 0, 0, NM, ST, 'w', /* scan 40-47 */
223 'x', 'y', 'l', 't', 'u', 'v', 'm', 'q', /* scan 48-4F */
224 'r', 's', 'p', 'n', 0, 0, '<', 0, /* scan 50-57 */
225 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
226 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
227 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
228 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
229 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
230 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
231 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
233 { /* right alt mode - is used in German keyboard */
234 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 00-07 */
235 '{', '[', ']', '}', '\\', 0xff, 0xff, 0xff, /* scan 08-0F */
236 '@', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10-17 */
237 0xff, 0xff, 0xff, '~', 0xff, 0xff, 0xff, 0xff, /* scan 18-1F */
238 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20-27 */
239 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28-2F */
240 0xff, 0xff, 0xe6, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30-37 */
241 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
242 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40-47 */
243 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
244 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '|', 0xff, /* scan 50-57 */
245 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
246 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
247 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
248 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
249 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
250 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* extended */
251 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
256 static unsigned char ext_key_map[] = {
257 0x1c, /* keypad enter */
258 0x1d, /* right control */
259 0x35, /* keypad slash */
260 0x37, /* print screen */
261 0x38, /* right alt */
263 0x47, /* editpad home */
264 0x48, /* editpad up */
265 0x49, /* editpad pgup */
266 0x4b, /* editpad left */
267 0x4d, /* editpad right */
268 0x4f, /* editpad end */
269 0x50, /* editpad dn */
270 0x51, /* editpad pgdn */
271 0x52, /* editpad ins */
272 0x53, /* editpad del */
276 static int kbd_input_empty(void)
278 int kbd_timeout = KBD_TIMEOUT * 1000;
280 while ((in8(I8042_STS_REG) & STATUS_IBF) && kbd_timeout--)
283 return kbd_timeout != -1;
286 static int kbd_output_full(void)
288 int kbd_timeout = KBD_TIMEOUT * 1000;
290 while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--)
293 return kbd_timeout != -1;
296 static void kbd_led_set(void)
299 out8(I8042_DATA_REG, CMD_SET_KBD_LED);
301 out8(I8042_DATA_REG, (kbd_flags & 0x7));
304 static void kbd_normal(unsigned char scan_code)
308 if ((kbd_flags & BRK) == NORMAL) {
309 chr = kbd_key_map[kbd_mapping][kbd_state][scan_code];
310 if ((chr == 0xff) || (chr == 0x00))
313 /* if caps lock convert upper to lower */
314 if (((kbd_flags & CAPS) == CAPS) &&
315 (chr >= 'a' && chr <= 'z')) {
322 static void kbd_shift(unsigned char scan_code)
324 if ((kbd_flags & BRK) == BRK) {
326 kbd_flags &= (~SHIFT);
333 static void kbd_ctrl(unsigned char scan_code)
335 if ((kbd_flags & BRK) == BRK) {
337 kbd_flags &= (~CTRL);
344 static void kbd_num(unsigned char scan_code)
346 if ((kbd_flags & BRK) == NORMAL) {
348 kbd_state = (kbd_flags & NUM) ? AS : NM;
353 static void kbd_alt(unsigned char scan_code)
355 if ((kbd_flags & BRK) == BRK) {
364 static void kbd_caps(unsigned char scan_code)
366 if ((kbd_flags & BRK) == NORMAL) {
372 static void kbd_scroll(unsigned char scan_code)
374 if ((kbd_flags & BRK) == NORMAL) {
384 static void kbd_conv_char(unsigned char scan_code)
386 if (scan_code == 0xe0) {
391 /* if high bit of scan_code, set break flag */
392 if (scan_code & 0x80)
397 if ((scan_code == 0xe1) || (kbd_flags & E1)) {
398 if (scan_code == 0xe1) {
399 kbd_flags ^= BRK; /* reset the break flag */
400 kbd_flags ^= E1; /* bitwise EXOR with E1 flag */
407 if (kbd_flags & EXT) {
411 for (i = 0; ext_key_map[i]; i++) {
412 if (ext_key_map[i] == scan_code) {
413 scan_code = 0x80 + i;
422 switch (kbd_fct_map[scan_code]) {
424 kbd_normal(scan_code);
427 kbd_shift(scan_code);
442 kbd_scroll(scan_code);
449 static int kbd_write(int reg, int value)
451 if (!kbd_input_empty())
458 static int kbd_read(int reg)
460 if (!kbd_output_full())
466 static int kbd_cmd_read(int cmd)
468 if (kbd_write(I8042_CMD_REG, cmd))
471 return kbd_read(I8042_DATA_REG);
474 static int kbd_cmd_write(int cmd, int data)
476 if (kbd_write(I8042_CMD_REG, cmd))
479 return kbd_write(I8042_DATA_REG, data);
482 static int kbd_reset(void)
486 /* controller self test */
487 if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK)
491 if (kbd_write(I8042_DATA_REG, CMD_RESET_KBD) ||
492 kbd_read(I8042_DATA_REG) != KBD_ACK ||
493 kbd_read(I8042_DATA_REG) != KBD_POR)
496 /* set AT translation and disable irq */
497 config = kbd_cmd_read(CMD_RD_CONFIG);
501 config |= CONFIG_AT_TRANS;
502 config &= ~(CONFIG_KIRQ_EN | CONFIG_MIRQ_EN);
503 if (kbd_cmd_write(CMD_WR_CONFIG, config))
506 /* enable keyboard */
507 if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) ||
514 static int kbd_controller_present(void)
516 return in8(I8042_STS_REG) != 0xff;
520 * Implement a weak default function for boards that optionally
521 * need to skip the i8042 initialization.
523 int __weak board_i8042_skip(void)
525 /* As default, don't skip */
529 void i8042_flush(void)
534 * The delay is to give the keyboard controller some time
535 * to fill the next byte.
538 timeout = 100; /* wait for no longer than 100us */
539 while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) {
544 /* Try to pull next byte if not timeout */
545 if (in8(I8042_STS_REG) & STATUS_OBF)
552 int i8042_disable(void)
554 if (kbd_input_empty() == 0)
557 /* Disable keyboard */
558 out8(I8042_CMD_REG, CMD_KBD_DIS);
560 if (kbd_input_empty() == 0)
566 /* i8042_kbd_init - reset keyboard and init state flags */
567 int i8042_kbd_init(void)
572 if (!kbd_controller_present() || board_i8042_skip()) {
573 debug("i8042 keyboard controller is not present\n");
577 /* Init keyboard device (default US layout) */
579 penv = getenv("keymap");
581 if (strncmp(penv, "de", 3) == 0)
585 for (try = 0; try < KBD_RESET_TRIES; try++) {
586 if (kbd_reset() == 0) {
587 kbd_mapping = keymap;
600 * i8042_tstc - test if keyboard input is available
602 * option: cursor blinking if called in a loop
604 int i8042_tstc(struct stdio_dev *dev)
606 unsigned char scan_code = 0;
608 if ((in8(I8042_STS_REG) & STATUS_OBF) == 0) {
611 scan_code = in8(I8042_DATA_REG);
612 if (scan_code == 0xfa)
615 kbd_conv_char(scan_code);
625 * i8042_getc - wait till keyboard input is available
627 * option: turn on/off cursor while waiting
629 int i8042_getc(struct stdio_dev *dev)
632 unsigned char scan_code;
634 while (kbd_input == -1) {
635 while ((in8(I8042_STS_REG) & STATUS_OBF) == 0) {
637 scan_code = in8(I8042_DATA_REG);
638 if (scan_code != 0xfa)
639 kbd_conv_char(scan_code);