blob: 22c2a4e3a02da185783e0a1064ceca2f54f0866d [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2002 ELTEC Elektronik AG
3 * Frank Gottschling <fgottschling@eltec.de>
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Wolfgang Denkbfc81252006-03-06 13:03:37 +010015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenkc6097192002-11-03 00:24:07 +000016 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24/* i8042.c - Intel 8042 keyboard driver routines */
25
26/* includes */
27
28#include <common.h>
29
30#ifdef CONFIG_I8042_KBD
31
Stefan Roesea7b9fb92006-01-18 20:05:34 +010032#ifdef CONFIG_USE_CPCIDVI
33extern u8 gt_cpcidvi_in8(u32 offset);
34extern void gt_cpcidvi_out8(u32 offset, u8 data);
35
Wolfgang Denkbfc81252006-03-06 13:03:37 +010036#define in8(a) gt_cpcidvi_in8(a)
Stefan Roesea7b9fb92006-01-18 20:05:34 +010037#define out8(a, b) gt_cpcidvi_out8(a,b)
38#endif
39
wdenkc6097192002-11-03 00:24:07 +000040#include <i8042.h>
41
42/* defines */
43
44#ifdef CONFIG_CONSOLE_CURSOR
45extern void console_cursor (int state);
46static int blinkCount = CFG_CONSOLE_BLINK_COUNT;
47static int cursor_state = 0;
48#endif
49
50/* locals */
51
Wolfgang Denkbfc81252006-03-06 13:03:37 +010052static int kbd_input = -1; /* no input yet */
53static int kbd_mapping = KBD_US; /* default US keyboard */
54static int kbd_flags = NORMAL; /* after reset */
55static int kbd_state = 0; /* unshift code */
wdenkc6097192002-11-03 00:24:07 +000056
57static void kbd_conv_char (unsigned char scan_code);
58static void kbd_led_set (void);
59static void kbd_normal (unsigned char scan_code);
60static void kbd_shift (unsigned char scan_code);
61static void kbd_ctrl (unsigned char scan_code);
62static void kbd_num (unsigned char scan_code);
63static void kbd_caps (unsigned char scan_code);
64static void kbd_scroll (unsigned char scan_code);
65static void kbd_alt (unsigned char scan_code);
66static int kbd_input_empty (void);
67static int kbd_reset (void);
68
69static unsigned char kbd_fct_map [144] =
70 { /* kbd_fct_map table for scan code */
Wolfgang Denkbfc81252006-03-06 13:03:37 +010071 0, AS, AS, AS, AS, AS, AS, AS, /* scan 0- 7 */
72 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 8- F */
73 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 10-17 */
74 AS, AS, AS, AS, AS, CN, AS, AS, /* scan 18-1F */
75 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 20-27 */
76 AS, AS, SH, AS, AS, AS, AS, AS, /* scan 28-2F */
77 AS, AS, AS, AS, AS, AS, SH, AS, /* scan 30-37 */
78 AS, AS, CP, 0, 0, 0, 0, 0, /* scan 38-3F */
79 0, 0, 0, 0, 0, NM, ST, ES, /* scan 40-47 */
80 ES, ES, ES, ES, ES, ES, ES, ES, /* scan 48-4F */
81 ES, ES, ES, ES, 0, 0, AS, 0, /* scan 50-57 */
82 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
83 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
84 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
85 AS, 0, 0, AS, 0, 0, AS, 0, /* scan 70-77 */
86 0, AS, 0, 0, 0, AS, 0, 0, /* scan 78-7F */
87 AS, CN, AS, AS, AK, ST, EX, EX, /* enhanced */
88 AS, EX, EX, AS, EX, AS, EX, EX /* enhanced */
wdenkc6097192002-11-03 00:24:07 +000089 };
90
91static unsigned char kbd_key_map [2][5][144] =
92 {
93 { /* US keyboard */
94 { /* unshift code */
Wolfgang Denkbfc81252006-03-06 13:03:37 +010095 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
96 '7', '8', '9', '0', '-', '=', 0x08, '\t', /* scan 8- F */
97 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', /* scan 10-17 */
98 'o', 'p', '[', ']', '\r', CN, 'a', 's', /* scan 18-1F */
99 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* scan 20-27 */
100 '\'', '`', SH, '\\', 'z', 'x', 'c', 'v', /* scan 28-2F */
101 'b', 'n', 'm', ',', '.', '/', SH, '*', /* scan 30-37 */
102 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
103 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
104 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
105 '2', '3', '0', '.', 0, 0, 0, 0, /* scan 50-57 */
106 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
107 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
108 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
109 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
110 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
111 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
112 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
wdenkc6097192002-11-03 00:24:07 +0000113 },
114 { /* shift code */
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100115 0, 0x1b, '!', '@', '#', '$', '%', '^', /* scan 0- 7 */
116 '&', '*', '(', ')', '_', '+', 0x08, '\t', /* scan 8- F */
117 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', /* scan 10-17 */
118 'O', 'P', '{', '}', '\r', CN, 'A', 'S', /* scan 18-1F */
119 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', /* scan 20-27 */
120 '"', '~', SH, '|', 'Z', 'X', 'C', 'V', /* scan 28-2F */
121 'B', 'N', 'M', '<', '>', '?', SH, '*', /* scan 30-37 */
122 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
123 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
124 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
125 '2', '3', '0', '.', 0, 0, 0, 0, /* scan 50-57 */
126 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
127 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
128 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
129 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
130 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
131 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
132 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
wdenkc6097192002-11-03 00:24:07 +0000133 },
134 { /* control code */
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100135 0xff, 0x1b, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, /* scan 0- 7 */
136 0x1e, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, '\t', /* scan 8- F */
137 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, /* scan 10-17 */
138 0x0f, 0x10, 0x1b, 0x1d, '\r', CN, 0x01, 0x13, /* scan 18-1F */
139 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0xff, /* scan 20-27 */
140 0xff, 0x1c, SH, 0xff, 0x1a, 0x18, 0x03, 0x16, /* scan 28-2F */
141 0x02, 0x0e, 0x0d, 0xff, 0xff, 0xff, SH, 0xff, /* scan 30-37 */
142 0xff, 0xff, CP, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
143 0xff, 0xff, 0xff, 0xff, 0xff, NM, ST, 0xff, /* scan 40-47 */
144 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
145 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
146 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
147 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
148 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
149 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
150 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
151 '\r', CN, '/', '*', ' ', ST, 0xff, 0xff, /* extended */
152 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
wdenkc6097192002-11-03 00:24:07 +0000153 },
154 { /* non numeric code */
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100155 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
156 '7', '8', '9', '0', '-', '=', 0x08, '\t', /* scan 8- F */
157 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', /* scan 10-17 */
158 'o', 'p', '[', ']', '\r', CN, 'a', 's', /* scan 18-1F */
159 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* scan 20-27 */
160 '\'', '`', SH, '\\', 'z', 'x', 'c', 'v', /* scan 28-2F */
161 'b', 'n', 'm', ',', '.', '/', SH, '*', /* scan 30-37 */
162 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
163 0, 0, 0, 0, 0, NM, ST, 'w', /* scan 40-47 */
164 'x', 'y', 'l', 't', 'u', 'v', 'm', 'q', /* scan 48-4F */
165 'r', 's', 'p', 'n', 0, 0, 0, 0, /* scan 50-57 */
166 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
167 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
168 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
169 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
170 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
171 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
172 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
wdenkc6097192002-11-03 00:24:07 +0000173 },
174 { /* right alt mode - not used in US keyboard */
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100175 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 0 - 7 */
176 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 8 - F */
177 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10 -17 */
178 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 18 -1F */
179 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20 -27 */
180 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28 -2F */
181 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30 -37 */
182 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38 -3F */
183 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40 -47 */
184 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48 -4F */
185 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50 -57 */
186 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58 -5F */
187 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60 -67 */
188 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68 -6F */
189 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70 -77 */
190 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78 -7F */
191 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* extended */
192 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
wdenkc6097192002-11-03 00:24:07 +0000193 }
194 },
195 { /* german keyboard */
196 { /* unshift code */
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100197 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
198 '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 8- F */
199 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
200 'o', 'p', 0x81, '+', '\r', CN, 'a', 's', /* scan 18-1F */
201 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
202 0x84, '^', SH, '#', 'y', 'x', 'c', 'v', /* scan 28-2F */
203 'b', 'n', 'm', ',', '.', '-', SH, '*', /* scan 30-37 */
204 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
205 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
206 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
207 '2', '3', '0', ',', 0, 0, '<', 0, /* scan 50-57 */
208 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
209 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
210 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
211 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
212 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
213 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
214 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
wdenkc6097192002-11-03 00:24:07 +0000215 },
216 { /* shift code */
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100217 0, 0x1b, '!', '"', 0x15, '$', '%', '&', /* scan 0- 7 */
218 '/', '(', ')', '=', '?', '`', 0x08, '\t', /* scan 8- F */
219 'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', /* scan 10-17 */
220 'O', 'P', 0x9a, '*', '\r', CN, 'A', 'S', /* scan 18-1F */
221 'D', 'F', 'G', 'H', 'J', 'K', 'L', 0x99, /* scan 20-27 */
222 0x8e, 0xf8, SH, '\'', 'Y', 'X', 'C', 'V', /* scan 28-2F */
223 'B', 'N', 'M', ';', ':', '_', SH, '*', /* scan 30-37 */
224 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
225 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
226 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
227 '2', '3', '0', ',', 0, 0, '>', 0, /* scan 50-57 */
228 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
229 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
230 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
231 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
232 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
233 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
234 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
wdenkc6097192002-11-03 00:24:07 +0000235 },
236 { /* control code */
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100237 0xff, 0x1b, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, /* scan 0- 7 */
238 0x1e, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, '\t', /* scan 8- F */
239 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, /* scan 10-17 */
240 0x0f, 0x10, 0x1b, 0x1d, '\r', CN, 0x01, 0x13, /* scan 18-1F */
241 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0xff, /* scan 20-27 */
242 0xff, 0x1c, SH, 0xff, 0x1a, 0x18, 0x03, 0x16, /* scan 28-2F */
243 0x02, 0x0e, 0x0d, 0xff, 0xff, 0xff, SH, 0xff, /* scan 30-37 */
244 0xff, 0xff, CP, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
245 0xff, 0xff, 0xff, 0xff, 0xff, NM, ST, 0xff, /* scan 40-47 */
246 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
247 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
248 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
249 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
250 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
251 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
252 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
253 '\r', CN, '/', '*', ' ', ST, 0xff, 0xff, /* extended */
254 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
wdenkc6097192002-11-03 00:24:07 +0000255 },
256 { /* non numeric code */
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100257 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
258 '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 8- F */
259 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
260 'o', 'p', 0x81, '+', '\r', CN, 'a', 's', /* scan 18-1F */
261 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
262 0x84, '^', SH, 0, 'y', 'x', 'c', 'v', /* scan 28-2F */
263 'b', 'n', 'm', ',', '.', '-', SH, '*', /* scan 30-37 */
264 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
265 0, 0, 0, 0, 0, NM, ST, 'w', /* scan 40-47 */
266 'x', 'y', 'l', 't', 'u', 'v', 'm', 'q', /* scan 48-4F */
267 'r', 's', 'p', 'n', 0, 0, '<', 0, /* scan 50-57 */
268 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
269 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
270 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
271 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
272 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
273 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
274 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
wdenkc6097192002-11-03 00:24:07 +0000275 },
276 { /* Right alt mode - is used in German keyboard */
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100277 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 0 - 7 */
278 '{', '[', ']', '}', '\\', 0xff, 0xff, 0xff, /* scan 8 - F */
279 '@', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10 -17 */
280 0xff, 0xff, 0xff, '~', 0xff, 0xff, 0xff, 0xff, /* scan 18 -1F */
281 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20 -27 */
282 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28 -2F */
283 0xff, 0xff, 0xe6, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30 -37 */
284 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38 -3F */
285 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40 -47 */
286 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48 -4F */
287 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '|', 0xff, /* scan 50 -57 */
288 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58 -5F */
289 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60 -67 */
290 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68 -6F */
291 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70 -77 */
292 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78 -7F */
293 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* extended */
294 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
wdenkc6097192002-11-03 00:24:07 +0000295 }
296 }
297 };
298
299static unsigned char ext_key_map [] =
300 {
301 0x1c, /* keypad enter */
302 0x1d, /* right control */
303 0x35, /* keypad slash */
304 0x37, /* print screen */
305 0x38, /* right alt */
306 0x46, /* break */
307 0x47, /* editpad home */
308 0x48, /* editpad up */
309 0x49, /* editpad pgup */
310 0x4b, /* editpad left */
311 0x4d, /* editpad right */
312 0x4f, /* editpad end */
313 0x50, /* editpad dn */
314 0x51, /* editpad pgdn */
315 0x52, /* editpad ins */
316 0x53, /* editpad del */
317 0x00 /* map end */
318 };
319
320/*******************************************************************************
321 *
322 * i8042_kbd_init - reset keyboard and init state flags
323 */
324int i8042_kbd_init (void)
325{
326 int keymap, try;
327 char *penv;
328
Stefan Roesea7b9fb92006-01-18 20:05:34 +0100329#ifdef CONFIG_USE_CPCIDVI
330 if ((penv = getenv ("console")) != NULL) {
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100331 if (strncmp (penv, "serial", 7) == 0) {
332 return -1;
Stefan Roesea7b9fb92006-01-18 20:05:34 +0100333 }
334 }
335#endif
wdenkc6097192002-11-03 00:24:07 +0000336 /* Init keyboard device (default US layout) */
337 keymap = KBD_US;
338 if ((penv = getenv ("keymap")) != NULL)
339 {
wdenk8bde7f72003-06-27 21:31:46 +0000340 if (strncmp (penv, "de", 3) == 0)
341 keymap = KBD_GER;
wdenkc6097192002-11-03 00:24:07 +0000342 }
343
344 for (try = 0; try < KBD_RESET_TRIES; try++)
345 {
wdenk8bde7f72003-06-27 21:31:46 +0000346 if (kbd_reset() == 0)
347 {
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100348 kbd_mapping = keymap;
349 kbd_flags = NORMAL;
350 kbd_state = 0;
wdenk8bde7f72003-06-27 21:31:46 +0000351 kbd_led_set();
352 return 0;
353 }
wdenkc6097192002-11-03 00:24:07 +0000354 }
355 return -1;
356}
357
358
359/*******************************************************************************
360 *
361 * i8042_tstc - test if keyboard input is available
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100362 * option: cursor blinking if called in a loop
wdenkc6097192002-11-03 00:24:07 +0000363 */
364int i8042_tstc (void)
365{
366 unsigned char scan_code = 0;
367
368#ifdef CONFIG_CONSOLE_CURSOR
369 if (--blinkCount == 0)
370 {
wdenk8bde7f72003-06-27 21:31:46 +0000371 cursor_state ^= 1;
372 console_cursor (cursor_state);
373 blinkCount = CFG_CONSOLE_BLINK_COUNT;
374 udelay (10);
wdenkc6097192002-11-03 00:24:07 +0000375 }
376#endif
377
378 if ((in8 (I8042_STATUS_REG) & 0x01) == 0)
wdenk8bde7f72003-06-27 21:31:46 +0000379 return 0;
wdenkc6097192002-11-03 00:24:07 +0000380 else
381 {
wdenk8bde7f72003-06-27 21:31:46 +0000382 scan_code = in8 (I8042_DATA_REG);
383 if (scan_code == 0xfa)
384 return 0;
wdenkc6097192002-11-03 00:24:07 +0000385
wdenk8bde7f72003-06-27 21:31:46 +0000386 kbd_conv_char(scan_code);
wdenkc6097192002-11-03 00:24:07 +0000387
wdenk8bde7f72003-06-27 21:31:46 +0000388 if (kbd_input != -1)
389 return 1;
wdenkc6097192002-11-03 00:24:07 +0000390 }
391 return 0;
392}
393
394
395/*******************************************************************************
396 *
397 * i8042_getc - wait till keyboard input is available
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100398 * option: turn on/off cursor while waiting
wdenkc6097192002-11-03 00:24:07 +0000399 */
400int i8042_getc (void)
401{
402 int ret_chr;
403 unsigned char scan_code;
404
405 while (kbd_input == -1)
406 {
wdenk8bde7f72003-06-27 21:31:46 +0000407 while ((in8 (I8042_STATUS_REG) & 0x01) == 0)
408 {
wdenkc6097192002-11-03 00:24:07 +0000409#ifdef CONFIG_CONSOLE_CURSOR
wdenk8bde7f72003-06-27 21:31:46 +0000410 if (--blinkCount==0)
411 {
412 cursor_state ^= 1;
413 console_cursor (cursor_state);
414 blinkCount = CFG_CONSOLE_BLINK_COUNT;
415 }
416 udelay (10);
wdenkc6097192002-11-03 00:24:07 +0000417#endif
wdenk8bde7f72003-06-27 21:31:46 +0000418 }
wdenkc6097192002-11-03 00:24:07 +0000419
wdenk8bde7f72003-06-27 21:31:46 +0000420 scan_code = in8 (I8042_DATA_REG);
wdenkc6097192002-11-03 00:24:07 +0000421
wdenk8bde7f72003-06-27 21:31:46 +0000422 if (scan_code != 0xfa)
423 kbd_conv_char (scan_code);
wdenkc6097192002-11-03 00:24:07 +0000424 }
425 ret_chr = kbd_input;
426 kbd_input = -1;
427 return ret_chr;
428}
429
430
431/******************************************************************************/
432
433static void kbd_conv_char (unsigned char scan_code)
434{
435 if (scan_code == 0xe0)
436 {
wdenk8bde7f72003-06-27 21:31:46 +0000437 kbd_flags |= EXT;
438 return;
wdenkc6097192002-11-03 00:24:07 +0000439 }
440
441 /* if high bit of scan_code, set break flag */
442 if (scan_code & 0x80)
wdenk8bde7f72003-06-27 21:31:46 +0000443 kbd_flags |= BRK;
wdenkc6097192002-11-03 00:24:07 +0000444 else
wdenk8bde7f72003-06-27 21:31:46 +0000445 kbd_flags &= ~BRK;
wdenkc6097192002-11-03 00:24:07 +0000446
447 if ((scan_code == 0xe1) || (kbd_flags & E1))
448 {
wdenk8bde7f72003-06-27 21:31:46 +0000449 if (scan_code == 0xe1)
450 {
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100451 kbd_flags ^= BRK; /* reset the break flag */
452 kbd_flags ^= E1; /* bitwise EXOR with E1 flag */
wdenk8bde7f72003-06-27 21:31:46 +0000453 }
454 return;
wdenkc6097192002-11-03 00:24:07 +0000455 }
456
457 scan_code &= 0x7f;
458
459 if (kbd_flags & EXT)
460 {
wdenk8bde7f72003-06-27 21:31:46 +0000461 int i;
wdenkc6097192002-11-03 00:24:07 +0000462
wdenk8bde7f72003-06-27 21:31:46 +0000463 kbd_flags ^= EXT;
464 for (i=0; ext_key_map[i]; i++)
465 {
466 if (ext_key_map[i] == scan_code)
467 {
468 scan_code = 0x80 + i;
469 break;
470 }
471 }
472 /* not found ? */
473 if (!ext_key_map[i])
474 return;
wdenkc6097192002-11-03 00:24:07 +0000475 }
476
477 switch (kbd_fct_map [scan_code])
478 {
479 case AS: kbd_normal (scan_code);
wdenk8bde7f72003-06-27 21:31:46 +0000480 break;
wdenkc6097192002-11-03 00:24:07 +0000481 case SH: kbd_shift (scan_code);
wdenk8bde7f72003-06-27 21:31:46 +0000482 break;
wdenkc6097192002-11-03 00:24:07 +0000483 case CN: kbd_ctrl (scan_code);
wdenk8bde7f72003-06-27 21:31:46 +0000484 break;
wdenkc6097192002-11-03 00:24:07 +0000485 case NM: kbd_num (scan_code);
wdenk8bde7f72003-06-27 21:31:46 +0000486 break;
wdenkc6097192002-11-03 00:24:07 +0000487 case CP: kbd_caps (scan_code);
wdenk8bde7f72003-06-27 21:31:46 +0000488 break;
wdenkc6097192002-11-03 00:24:07 +0000489 case ST: kbd_scroll (scan_code);
wdenk8bde7f72003-06-27 21:31:46 +0000490 break;
wdenkc6097192002-11-03 00:24:07 +0000491 case AK: kbd_alt (scan_code);
wdenk8bde7f72003-06-27 21:31:46 +0000492 break;
wdenkc6097192002-11-03 00:24:07 +0000493 }
494 return;
495}
496
497
498/******************************************************************************/
499
500static void kbd_normal (unsigned char scan_code)
501{
502 unsigned char chr;
503
504 if ((kbd_flags & BRK) == NORMAL)
505 {
506 chr = kbd_key_map [kbd_mapping][kbd_state][scan_code];
507 if ((chr == 0xff) || (chr == 0x00))
wdenk8bde7f72003-06-27 21:31:46 +0000508 {
509 return;
510 }
wdenkc6097192002-11-03 00:24:07 +0000511
wdenk8bde7f72003-06-27 21:31:46 +0000512 /* if caps lock convert upper to lower */
513 if (((kbd_flags & CAPS) == CAPS) && (chr >= 'a' && chr <= 'z'))
wdenkc6097192002-11-03 00:24:07 +0000514 {
wdenk8bde7f72003-06-27 21:31:46 +0000515 chr -= 'a' - 'A';
wdenkc6097192002-11-03 00:24:07 +0000516 }
517 kbd_input = chr;
518 }
519}
520
521
522/******************************************************************************/
523
524static void kbd_shift (unsigned char scan_code)
525{
526 if ((kbd_flags & BRK) == BRK)
527 {
wdenk8bde7f72003-06-27 21:31:46 +0000528 kbd_state = AS;
529 kbd_flags &= (~SHIFT);
wdenkc6097192002-11-03 00:24:07 +0000530 }
531 else
532 {
533 kbd_state = SH;
534 kbd_flags |= SHIFT;
535 }
536}
537
538
539/******************************************************************************/
540
541static void kbd_ctrl (unsigned char scan_code)
542{
543 if ((kbd_flags & BRK) == BRK)
544 {
545 kbd_state = AS;
546 kbd_flags &= (~CTRL);
547 }
548 else
549 {
550 kbd_state = CN;
551 kbd_flags |= CTRL;
552 }
553}
554
555
556/******************************************************************************/
557
558static void kbd_caps (unsigned char scan_code)
559{
560 if ((kbd_flags & BRK) == NORMAL)
561 {
562 kbd_flags ^= CAPS;
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100563 kbd_led_set (); /* update keyboard LED */
wdenkc6097192002-11-03 00:24:07 +0000564 }
565}
566
567
568/******************************************************************************/
569
570static void kbd_num (unsigned char scan_code)
571{
572 if ((kbd_flags & BRK) == NORMAL)
573 {
574 kbd_flags ^= NUM;
575 kbd_state = (kbd_flags & NUM) ? AS : NM;
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100576 kbd_led_set (); /* update keyboard LED */
wdenkc6097192002-11-03 00:24:07 +0000577 }
578}
579
580
581/******************************************************************************/
582
583static void kbd_scroll (unsigned char scan_code)
584{
585 if ((kbd_flags & BRK) == NORMAL)
586 {
wdenk8bde7f72003-06-27 21:31:46 +0000587 kbd_flags ^= STP;
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100588 kbd_led_set (); /* update keyboard LED */
wdenk8bde7f72003-06-27 21:31:46 +0000589 if (kbd_flags & STP)
590 kbd_input = 0x13;
591 else
592 kbd_input = 0x11;
wdenkc6097192002-11-03 00:24:07 +0000593 }
594}
595
596/******************************************************************************/
597
598static void kbd_alt (unsigned char scan_code)
599{
600 if ((kbd_flags & BRK) == BRK)
601 {
wdenk8bde7f72003-06-27 21:31:46 +0000602 kbd_state = AS;
603 kbd_flags &= (~ALT);
wdenkc6097192002-11-03 00:24:07 +0000604 }
605 else
606 {
wdenk8bde7f72003-06-27 21:31:46 +0000607 kbd_state = AK;
608 kbd_flags &= ALT;
wdenkc6097192002-11-03 00:24:07 +0000609 }
610}
611
612
613/******************************************************************************/
614
615static void kbd_led_set (void)
616{
617 kbd_input_empty();
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100618 out8 (I8042_DATA_REG, 0xed); /* SET LED command */
wdenkc6097192002-11-03 00:24:07 +0000619 kbd_input_empty();
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100620 out8 (I8042_DATA_REG, (kbd_flags & 0x7)); /* LED bits only */
wdenkc6097192002-11-03 00:24:07 +0000621}
622
623
624/******************************************************************************/
625
626static int kbd_input_empty (void)
627{
628 int kbdTimeout = KBD_TIMEOUT;
629
630 /* wait for input buf empty */
631 while ((in8 (I8042_STATUS_REG) & 0x02) && kbdTimeout--)
wdenk8bde7f72003-06-27 21:31:46 +0000632 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +0000633
634 return kbdTimeout;
635}
636
637/******************************************************************************/
638
639static int kbd_reset (void)
640{
641 if (kbd_input_empty() == 0)
wdenk8bde7f72003-06-27 21:31:46 +0000642 return -1;
wdenkc6097192002-11-03 00:24:07 +0000643
644 out8 (I8042_DATA_REG, 0xff);
645
646 udelay(250000);
647
648 if (kbd_input_empty() == 0)
wdenk8bde7f72003-06-27 21:31:46 +0000649 return -1;
wdenkc6097192002-11-03 00:24:07 +0000650
Stefan Roesea7b9fb92006-01-18 20:05:34 +0100651#ifdef CONFIG_USE_CPCIDVI
652 out8 (I8042_COMMAND_REG, 0x60);
653#else
wdenkc6097192002-11-03 00:24:07 +0000654 out8 (I8042_DATA_REG, 0x60);
Stefan Roesea7b9fb92006-01-18 20:05:34 +0100655#endif
wdenkc6097192002-11-03 00:24:07 +0000656
657 if (kbd_input_empty() == 0)
wdenk8bde7f72003-06-27 21:31:46 +0000658 return -1;
wdenkc6097192002-11-03 00:24:07 +0000659
660 out8 (I8042_DATA_REG, 0x45);
661
662
663 if (kbd_input_empty() == 0)
wdenk8bde7f72003-06-27 21:31:46 +0000664 return -1;
wdenkc6097192002-11-03 00:24:07 +0000665
666 out8 (I8042_COMMAND_REG, 0xae);
667
668 if (kbd_input_empty() == 0)
wdenk8bde7f72003-06-27 21:31:46 +0000669 return -1;
wdenkc6097192002-11-03 00:24:07 +0000670
671 return 0;
672}
673
674#endif /* CONFIG_I8042_KBD */