]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/i8042.c
* Switch LWMON board default config from FRAM to EEPROM;
[karo-tx-uboot.git] / drivers / i8042.c
1 /*
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
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * 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
32 #include <i8042.h>
33
34 /* defines */
35
36 #ifdef CONFIG_CONSOLE_CURSOR
37 extern void console_cursor (int state);
38 static int blinkCount = CFG_CONSOLE_BLINK_COUNT;
39 static int cursor_state = 0;
40 #endif
41
42 /* locals */
43
44 static int  kbd_input    = -1;          /* no input yet */
45 static int  kbd_mapping  = KBD_US;      /* default US keyboard */
46 static int  kbd_flags    = NORMAL;      /* after reset */
47 static int  kbd_state    = 0;           /* unshift code */
48
49 static void kbd_conv_char (unsigned char scan_code);
50 static void kbd_led_set (void);
51 static void kbd_normal (unsigned char scan_code);
52 static void kbd_shift (unsigned char scan_code);
53 static void kbd_ctrl (unsigned char scan_code);
54 static void kbd_num (unsigned char scan_code);
55 static void kbd_caps (unsigned char scan_code);
56 static void kbd_scroll (unsigned char scan_code);
57 static void kbd_alt (unsigned char scan_code);
58 static int  kbd_input_empty (void);
59 static int  kbd_reset (void);
60
61 static unsigned char kbd_fct_map [144] =
62     { /* kbd_fct_map table for scan code */
63     0,   AS,   AS,   AS,   AS,   AS,   AS,   AS, /* scan  0- 7 */
64    AS,   AS,   AS,   AS,   AS,   AS,   AS,   AS, /* scan  8- F */
65    AS,   AS,   AS,   AS,   AS,   AS,   AS,   AS, /* scan 10-17 */
66    AS,   AS,   AS,   AS,   AS,   CN,   AS,   AS, /* scan 18-1F */
67    AS,   AS,   AS,   AS,   AS,   AS,   AS,   AS, /* scan 20-27 */
68    AS,   AS,   SH,   AS,   AS,   AS,   AS,   AS, /* scan 28-2F */
69    AS,   AS,   AS,   AS,   AS,   AS,   SH,   AS, /* scan 30-37 */
70    AS,   AS,   CP,   0,    0,    0,    0,     0, /* scan 38-3F */
71     0,   0,    0,    0,    0,    NM,   ST,   ES, /* scan 40-47 */
72    ES,   ES,   ES,   ES,   ES,   ES,   ES,   ES, /* scan 48-4F */
73    ES,   ES,   ES,   ES,   0,    0,    AS,    0, /* scan 50-57 */
74     0,   0,    0,    0,    0,    0,    0,     0, /* scan 58-5F */
75     0,   0,    0,    0,    0,    0,    0,     0, /* scan 60-67 */
76     0,   0,    0,    0,    0,    0,    0,     0, /* scan 68-6F */
77    AS,   0,    0,    AS,   0,    0,    AS,    0, /* scan 70-77 */
78     0,   AS,   0,    0,    0,    AS,   0,     0, /* scan 78-7F */
79    AS,   CN,   AS,   AS,   AK,   ST,   EX,   EX, /* enhanced   */
80    AS,   EX,   EX,   AS,   EX,   AS,   EX,   EX  /* enhanced   */
81     };
82
83 static unsigned char kbd_key_map [2][5][144] =
84     {
85     { /* US keyboard */
86     { /* unshift code */
87     0,  0x1b,   '1',   '2',   '3',   '4',   '5',   '6',    /* scan  0- 7 */
88   '7',   '8',   '9',   '0',   '-',   '=',  0x08,  '\t',    /* scan  8- F */
89   'q',   'w',   'e',   'r',   't',   'y',   'u',   'i',    /* scan 10-17 */
90   'o',   'p',   '[',   ']',  '\r',   CN,    'a',   's',    /* scan 18-1F */
91   'd',   'f',   'g',   'h',   'j',   'k',   'l',   ';',    /* scan 20-27 */
92  '\'',   '`',   SH,   '\\',   'z',   'x',   'c',   'v',    /* scan 28-2F */
93   'b',   'n',   'm',   ',',   '.',   '/',   SH,    '*',    /* scan 30-37 */
94   ' ',   ' ',   CP,      0,     0,     0,     0,     0,    /* scan 38-3F */
95     0,     0,     0,     0,     0,   NM,    ST,    '7',    /* scan 40-47 */
96   '8',   '9',   '-',   '4',   '5',   '6',   '+',   '1',    /* scan 48-4F */
97   '2',   '3',   '0',   '.',     0,     0,     0,     0,    /* scan 50-57 */
98     0,     0,     0,     0,     0,     0,     0,     0,    /* scan 58-5F */
99     0,     0,     0,     0,     0,     0,     0,     0,    /* scan 60-67 */
100     0,     0,     0,     0,     0,     0,     0,     0,    /* scan 68-6F */
101     0,     0,     0,     0,     0,     0,     0,     0,    /* scan 70-77 */
102     0,     0,     0,     0,     0,     0,     0,     0,    /* scan 78-7F */
103   '\r',   CN,   '/',   '*',   ' ',    ST,   'F',   'A',    /* extended */
104     0,   'D',   'C',     0,   'B',     0,    '@',  'P'     /* extended */
105     },
106     { /* shift code */
107     0,  0x1b,   '!',   '@',   '#',   '$',   '%',   '^',    /* scan  0- 7 */
108   '&',   '*',   '(',   ')',   '_',   '+',  0x08,  '\t',    /* scan  8- F */
109   'Q',   'W',   'E',   'R',   'T',   'Y',   'U',   'I',    /* scan 10-17 */
110   'O',   'P',   '{',   '}',  '\r',   CN,    'A',   'S',    /* scan 18-1F */
111   'D',   'F',   'G',   'H',   'J',   'K',   'L',   ':',    /* scan 20-27 */
112   '"',   '~',   SH,    '|',   'Z',   'X',   'C',   'V',    /* scan 28-2F */
113   'B',   'N',   'M',   '<',   '>',   '?',   SH,    '*',    /* scan 30-37 */
114   ' ',   ' ',   CP,      0,     0,     0,     0,     0,    /* scan 38-3F */
115     0,     0,     0,     0,     0,   NM,    ST,    '7',    /* scan 40-47 */
116   '8',   '9',   '-',   '4',   '5',   '6',   '+',   '1',    /* scan 48-4F */
117   '2',   '3',   '0',   '.',     0,     0,     0,     0,    /* scan 50-57 */
118     0,     0,     0,     0,     0,     0,     0,     0,    /* scan 58-5F */
119     0,     0,     0,     0,     0,     0,     0,     0,    /* scan 60-67 */
120     0,     0,     0,     0,     0,     0,     0,     0,    /* scan 68-6F */
121     0,     0,     0,     0,     0,     0,     0,     0,    /* scan 70-77 */
122     0,     0,     0,     0,     0,     0,     0,     0,    /* scan 78-7F */
123   '\r',   CN,   '/',   '*',   ' ',    ST,   'F',   'A',    /* extended */
124     0,   'D',   'C',     0,   'B',     0,   '@',   'P'     /* extended */
125     },
126     { /* control code */
127  0xff,  0x1b,  0xff,  0x00,  0xff,  0xff,  0xff,  0xff,    /* scan  0- 7 */
128  0x1e,  0xff,  0xff,  0xff,  0x1f,  0xff,  0xff,  '\t',    /* scan  8- F */
129  0x11,  0x17,  0x05,  0x12,  0x14,  0x19,  0x15,  0x09,    /* scan 10-17 */
130  0x0f,  0x10,  0x1b,  0x1d,  '\r',   CN,   0x01,  0x13,    /* scan 18-1F */
131  0x04,  0x06,  0x07,  0x08,  0x0a,  0x0b,  0x0c,  0xff,    /* scan 20-27 */
132  0xff,  0x1c,   SH,   0xff,  0x1a,  0x18,  0x03,  0x16,    /* scan 28-2F */
133  0x02,  0x0e,  0x0d,  0xff,  0xff,  0xff,   SH,   0xff,    /* scan 30-37 */
134  0xff,  0xff,   CP,   0xff,  0xff,  0xff,  0xff,  0xff,    /* scan 38-3F */
135  0xff,  0xff,  0xff,  0xff,  0xff,   NM,    ST,   0xff,    /* scan 40-47 */
136  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,    /* scan 48-4F */
137  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,    /* scan 50-57 */
138  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,    /* scan 58-5F */
139  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,    /* scan 60-67 */
140  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,    /* scan 68-6F */
141  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,    /* scan 70-77 */
142  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,    /* scan 78-7F */
143   '\r',   CN,   '/',   '*',   ' ',    ST,  0xff,  0xff,    /* extended */
144  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff     /* extended */
145     },
146     { /* non numeric code */
147     0,  0x1b,   '1',   '2',   '3',   '4',   '5',   '6',    /* scan  0- 7 */
148   '7',   '8',   '9',   '0',   '-',   '=',  0x08,  '\t',    /* scan  8- F */
149   'q',   'w',   'e',   'r',   't',   'y',   'u',   'i',    /* scan 10-17 */
150   'o',   'p',   '[',   ']',  '\r',   CN,    'a',   's',    /* scan 18-1F */
151   'd',   'f',   'g',   'h',   'j',   'k',   'l',   ';',    /* scan 20-27 */
152  '\'',   '`',   SH,   '\\',   'z',   'x',   'c',   'v',    /* scan 28-2F */
153   'b',   'n',   'm',   ',',   '.',   '/',   SH,    '*',    /* scan 30-37 */
154   ' ',   ' ',   CP,      0,     0,     0,     0,     0,    /* scan 38-3F */
155     0,     0,     0,     0,     0,   NM,    ST,    'w',    /* scan 40-47 */
156   'x',   'y',   'l',   't',   'u',   'v',   'm',   'q',    /* scan 48-4F */
157   'r',   's',   'p',   'n',     0,     0,     0,     0,    /* scan 50-57 */
158     0,     0,     0,     0,     0,     0,     0,     0,    /* scan 58-5F */
159     0,     0,     0,     0,     0,     0,     0,     0,    /* scan 60-67 */
160     0,     0,     0,     0,     0,     0,     0,     0,    /* scan 68-6F */
161     0,     0,     0,     0,     0,     0,     0,     0,    /* scan 70-77 */
162     0,     0,     0,     0,     0,     0,     0,     0,    /* scan 78-7F */
163   '\r',   CN,   '/',   '*',   ' ',    ST,   'F',   'A',    /* extended */
164     0,   'D',   'C',     0,   'B',     0,    '@',  'P'     /* extended */
165     },
166     { /* right alt mode - not used in US keyboard */
167  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan  0 - 7 */
168  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan  8 - F */
169  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 10 -17 */
170  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 18 -1F */
171  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 20 -27 */
172  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 28 -2F */
173  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 30 -37 */
174  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 38 -3F */
175  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 40 -47 */
176  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 48 -4F */
177  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 50 -57 */
178  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 58 -5F */
179  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 60 -67 */
180  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 68 -6F */
181  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 70 -77 */
182  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 78 -7F */
183  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* extended    */
184  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff  /* extended    */
185     }
186     },
187     { /* german keyboard */
188     { /* unshift code */
189     0,  0x1b,   '1',   '2',   '3',   '4',   '5',   '6', /* scan  0- 7 */
190   '7',   '8',   '9',   '0',  0xe1,  '\'',  0x08,  '\t', /* scan  8- F */
191   'q',   'w',   'e',   'r',   't',   'z',   'u',   'i', /* scan 10-17 */
192   'o',   'p',  0x81,   '+',  '\r',   CN,    'a',   's', /* scan 18-1F */
193   'd',   'f',   'g',   'h',   'j',   'k',   'l',  0x94, /* scan 20-27 */
194  0x84,   '^',   SH,    '#',   'y',   'x',   'c',   'v', /* scan 28-2F */
195   'b',   'n',   'm',   ',',   '.',   '-',   SH,    '*', /* scan 30-37 */
196   ' ',   ' ',   CP,      0,     0,     0,     0,     0, /* scan 38-3F */
197     0,     0,     0,     0,     0,   NM,    ST,    '7', /* scan 40-47 */
198   '8',   '9',   '-',   '4',   '5',   '6',   '+',   '1', /* scan 48-4F */
199   '2',   '3',   '0',   ',',     0,     0,   '<',     0, /* scan 50-57 */
200     0,     0,     0,     0,     0,     0,     0,     0, /* scan 58-5F */
201     0,     0,     0,     0,     0,     0,     0,     0, /* scan 60-67 */
202     0,     0,     0,     0,     0,     0,     0,     0, /* scan 68-6F */
203     0,     0,     0,     0,     0,     0,     0,     0, /* scan 70-77 */
204     0,     0,     0,     0,     0,     0,     0,     0, /* scan 78-7F */
205   '\r',   CN,   '/',   '*',   ' ',    ST,   'F',   'A', /* extended */
206     0,   'D',   'C',     0,   'B',     0,    '@',  'P'  /* extended */
207     },
208     { /* shift code */
209     0,  0x1b,   '!',   '"',  0x15,   '$',   '%',   '&', /* scan  0- 7 */
210   '/',   '(',   ')',   '=',   '?',   '`',  0x08,  '\t', /* scan  8- F */
211   'Q',   'W',   'E',   'R',   'T',   'Z',   'U',   'I', /* scan 10-17 */
212   'O',   'P',  0x9a,   '*',  '\r',   CN,    'A',   'S', /* scan 18-1F */
213   'D',   'F',   'G',   'H',   'J',   'K',   'L',  0x99, /* scan 20-27 */
214  0x8e,  0xf8,   SH,   '\'',   'Y',   'X',   'C',   'V', /* scan 28-2F */
215   'B',   'N',   'M',   ';',   ':',   '_',   SH,    '*', /* scan 30-37 */
216   ' ',   ' ',   CP,      0,     0,     0,     0,     0, /* scan 38-3F */
217     0,     0,     0,     0,     0,   NM,    ST,    '7', /* scan 40-47 */
218   '8',   '9',   '-',   '4',   '5',   '6',   '+',   '1', /* scan 48-4F */
219   '2',   '3',   '0',   ',',     0,     0,   '>',     0, /* scan 50-57 */
220     0,     0,     0,     0,     0,     0,     0,     0, /* scan 58-5F */
221     0,     0,     0,     0,     0,     0,     0,     0, /* scan 60-67 */
222     0,     0,     0,     0,     0,     0,     0,     0, /* scan 68-6F */
223     0,     0,     0,     0,     0,     0,     0,     0, /* scan 70-77 */
224     0,     0,     0,     0,     0,     0,     0,     0, /* scan 78-7F */
225   '\r',   CN,   '/',   '*',   ' ',    ST,   'F',   'A', /* extended */
226     0,   'D',   'C',     0,   'B',     0,   '@',   'P'  /* extended */
227     },
228     { /* control code */
229  0xff,  0x1b,  0xff,  0x00,  0xff,  0xff,  0xff,  0xff, /* scan  0- 7 */
230  0x1e,  0xff,  0xff,  0xff,  0x1f,  0xff,  0xff,  '\t', /* scan  8- F */
231  0x11,  0x17,  0x05,  0x12,  0x14,  0x19,  0x15,  0x09, /* scan 10-17 */
232  0x0f,  0x10,  0x1b,  0x1d,  '\r',   CN,   0x01,  0x13, /* scan 18-1F */
233  0x04,  0x06,  0x07,  0x08,  0x0a,  0x0b,  0x0c,  0xff, /* scan 20-27 */
234  0xff,  0x1c,   SH,   0xff,  0x1a,  0x18,  0x03,  0x16, /* scan 28-2F */
235  0x02,  0x0e,  0x0d,  0xff,  0xff,  0xff,   SH,   0xff, /* scan 30-37 */
236  0xff,  0xff,   CP,   0xff,  0xff,  0xff,  0xff,  0xff, /* scan 38-3F */
237  0xff,  0xff,  0xff,  0xff,  0xff,   NM,    ST,   0xff, /* scan 40-47 */
238  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 48-4F */
239  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 50-57 */
240  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 58-5F */
241  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 60-67 */
242  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 68-6F */
243  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 70-77 */
244  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 78-7F */
245   '\r',   CN,   '/',   '*',   ' ',    ST,  0xff,  0xff, /* extended */
246  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff  /* extended */
247     },
248     { /* non numeric code */
249     0,  0x1b,   '1',   '2',   '3',   '4',   '5',   '6', /* scan  0- 7 */
250   '7',   '8',   '9',   '0',  0xe1,  '\'',  0x08,  '\t', /* scan  8- F */
251   'q',   'w',   'e',   'r',   't',   'z',   'u',   'i', /* scan 10-17 */
252   'o',   'p',  0x81,   '+',  '\r',   CN,    'a',   's', /* scan 18-1F */
253   'd',   'f',   'g',   'h',   'j',   'k',   'l',  0x94, /* scan 20-27 */
254  0x84,   '^',   SH,      0,   'y',   'x',   'c',   'v', /* scan 28-2F */
255   'b',   'n',   'm',   ',',   '.',   '-',   SH,    '*', /* scan 30-37 */
256   ' ',   ' ',   CP,      0,     0,     0,     0,     0, /* scan 38-3F */
257     0,     0,     0,     0,     0,   NM,    ST,    'w', /* scan 40-47 */
258   'x',   'y',   'l',   't',   'u',   'v',   'm',   'q', /* scan 48-4F */
259   'r',   's',   'p',   'n',     0,     0,   '<',     0, /* scan 50-57 */
260     0,     0,     0,     0,     0,     0,     0,     0, /* scan 58-5F */
261     0,     0,     0,     0,     0,     0,     0,     0, /* scan 60-67 */
262     0,     0,     0,     0,     0,     0,     0,     0, /* scan 68-6F */
263     0,     0,     0,     0,     0,     0,     0,     0, /* scan 70-77 */
264     0,     0,     0,     0,     0,     0,     0,     0, /* scan 78-7F */
265   '\r',   CN,   '/',   '*',   ' ',    ST,   'F',   'A', /* extended */
266     0,   'D',   'C',     0,   'B',     0,    '@',  'P'  /* extended */
267     },
268     { /* Right alt mode - is used in German keyboard */
269  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan  0 - 7 */
270   '{',   '[',   ']',   '}',  '\\',  0xff,  0xff,  0xff, /* scan  8 - F */
271   '@',  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 10 -17 */
272  0xff,  0xff,  0xff,   '~',  0xff,  0xff,  0xff,  0xff, /* scan 18 -1F */
273  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 20 -27 */
274  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 28 -2F */
275  0xff,  0xff,  0xe6,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 30 -37 */
276  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 38 -3F */
277  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 40 -47 */
278  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 48 -4F */
279  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,   '|',  0xff, /* scan 50 -57 */
280  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 58 -5F */
281  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 60 -67 */
282  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 68 -6F */
283  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 70 -77 */
284  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* scan 78 -7F */
285  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, /* extended    */
286  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff  /* extended    */
287     }
288     }
289     };
290
291 static unsigned char ext_key_map [] =
292     {
293     0x1c,   /* keypad enter */
294     0x1d,   /* right control */
295     0x35,   /* keypad slash */
296     0x37,   /* print screen */
297     0x38,   /* right alt */
298     0x46,   /* break */
299     0x47,   /* editpad home */
300     0x48,   /* editpad up */
301     0x49,   /* editpad pgup */
302     0x4b,   /* editpad left */
303     0x4d,   /* editpad right */
304     0x4f,   /* editpad end */
305     0x50,   /* editpad dn */
306     0x51,   /* editpad pgdn */
307     0x52,   /* editpad ins */
308     0x53,   /* editpad del */
309     0x00    /* map end */
310     };
311
312 /*******************************************************************************
313  *
314  * i8042_kbd_init - reset keyboard and init state flags
315  */
316 int i8042_kbd_init (void)
317 {
318     int keymap, try;
319     char *penv;
320
321     /* Init keyboard device (default US layout) */
322     keymap = KBD_US;
323     if ((penv = getenv ("keymap")) != NULL)
324     {
325         if (strncmp (penv, "de", 3) == 0)
326         keymap = KBD_GER;
327     }
328
329     for (try = 0; try < KBD_RESET_TRIES; try++)
330     {
331         if (kbd_reset() == 0)
332         {
333             kbd_mapping   = keymap;
334             kbd_flags     = NORMAL;
335             kbd_state     = 0;
336             kbd_led_set();
337             return 0;
338             }
339     }
340     return -1;
341 }
342
343
344 /*******************************************************************************
345  *
346  * i8042_tstc - test if keyboard input is available
347  *              option: cursor blinking if called in a loop
348  */
349 int i8042_tstc (void)
350 {
351     unsigned char scan_code = 0;
352
353 #ifdef CONFIG_CONSOLE_CURSOR
354     if (--blinkCount == 0)
355     {
356         cursor_state ^= 1;
357         console_cursor (cursor_state);
358         blinkCount = CFG_CONSOLE_BLINK_COUNT;
359         udelay (10);
360     }
361 #endif
362
363     if ((in8 (I8042_STATUS_REG) & 0x01) == 0)
364         return 0;
365     else
366     {
367         scan_code = in8 (I8042_DATA_REG);
368         if (scan_code == 0xfa)
369             return 0;
370
371         kbd_conv_char(scan_code);
372
373         if (kbd_input != -1)
374             return 1;
375     }
376     return 0;
377 }
378
379
380 /*******************************************************************************
381  *
382  * i8042_getc - wait till keyboard input is available
383  *              option: turn on/off cursor while waiting
384  */
385 int i8042_getc (void)
386 {
387     int ret_chr;
388     unsigned char scan_code;
389
390     while (kbd_input == -1)
391     {
392         while ((in8 (I8042_STATUS_REG) & 0x01) == 0)
393         {
394 #ifdef CONFIG_CONSOLE_CURSOR
395             if (--blinkCount==0)
396             {
397                 cursor_state ^= 1;
398                 console_cursor (cursor_state);
399                 blinkCount = CFG_CONSOLE_BLINK_COUNT;
400             }
401             udelay (10);
402 #endif
403         }
404
405         scan_code = in8 (I8042_DATA_REG);
406
407         if (scan_code != 0xfa)
408         kbd_conv_char (scan_code);
409     }
410     ret_chr = kbd_input;
411     kbd_input = -1;
412     return ret_chr;
413 }
414
415
416 /******************************************************************************/
417
418 static void kbd_conv_char (unsigned char scan_code)
419 {
420     if (scan_code == 0xe0)
421     {
422         kbd_flags |= EXT;
423         return;
424     }
425
426     /* if high bit of scan_code, set break flag */
427     if (scan_code & 0x80)
428         kbd_flags |=  BRK;
429     else
430         kbd_flags &= ~BRK;
431
432     if ((scan_code == 0xe1) || (kbd_flags & E1))
433     {
434         if (scan_code == 0xe1)
435         {
436             kbd_flags ^= BRK;     /* reset the break flag */
437             kbd_flags ^= E1;      /* bitwise EXOR with E1 flag */
438         }
439         return;
440     }
441
442     scan_code &= 0x7f;
443
444     if (kbd_flags & EXT)
445     {
446         int i;
447
448         kbd_flags ^= EXT;
449         for (i=0; ext_key_map[i]; i++)
450         {
451             if (ext_key_map[i] == scan_code)
452             {
453                 scan_code = 0x80 + i;
454                 break;
455             }
456         }
457         /* not found ? */
458         if (!ext_key_map[i])
459             return;
460     }
461
462     switch (kbd_fct_map [scan_code])
463     {
464     case AS:  kbd_normal (scan_code);
465         break;
466     case SH:  kbd_shift (scan_code);
467         break;
468     case CN:  kbd_ctrl (scan_code);
469         break;
470     case NM:  kbd_num (scan_code);
471         break;
472     case CP:  kbd_caps (scan_code);
473         break;
474     case ST:  kbd_scroll (scan_code);
475         break;
476     case AK:  kbd_alt (scan_code);
477         break;
478     }
479     return;
480 }
481
482
483 /******************************************************************************/
484
485 static void kbd_normal (unsigned char scan_code)
486 {
487     unsigned char chr;
488
489     if ((kbd_flags & BRK) == NORMAL)
490     {
491        chr = kbd_key_map [kbd_mapping][kbd_state][scan_code];
492        if ((chr == 0xff) || (chr == 0x00))
493         {
494             return;
495         }
496
497         /* if caps lock convert upper to lower */
498         if (((kbd_flags & CAPS) == CAPS) && (chr >= 'a' && chr <= 'z'))
499        {
500            chr -= 'a' - 'A';
501        }
502        kbd_input = chr;
503     }
504 }
505
506
507 /******************************************************************************/
508
509 static void kbd_shift (unsigned char scan_code)
510 {
511     if ((kbd_flags & BRK) == BRK)
512     {
513         kbd_state = AS;
514         kbd_flags &= (~SHIFT);
515     }
516     else
517     {
518        kbd_state = SH;
519        kbd_flags |= SHIFT;
520     }
521 }
522
523
524 /******************************************************************************/
525
526 static void kbd_ctrl (unsigned char scan_code)
527 {
528     if ((kbd_flags & BRK) == BRK)
529     {
530        kbd_state = AS;
531        kbd_flags &= (~CTRL);
532     }
533     else
534     {
535        kbd_state = CN;
536        kbd_flags |= CTRL;
537     }
538 }
539
540
541 /******************************************************************************/
542
543 static void kbd_caps (unsigned char scan_code)
544 {
545     if ((kbd_flags & BRK) == NORMAL)
546     {
547        kbd_flags ^= CAPS;
548        kbd_led_set ();            /* update keyboard LED */
549     }
550 }
551
552
553 /******************************************************************************/
554
555 static void kbd_num (unsigned char scan_code)
556 {
557     if ((kbd_flags & BRK) == NORMAL)
558     {
559        kbd_flags ^= NUM;
560        kbd_state = (kbd_flags & NUM) ? AS : NM;
561        kbd_led_set ();            /* update keyboard LED */
562     }
563 }
564
565
566 /******************************************************************************/
567
568 static void kbd_scroll (unsigned char scan_code)
569 {
570     if ((kbd_flags & BRK) == NORMAL)
571     {
572         kbd_flags ^= STP;
573         kbd_led_set ();            /* update keyboard LED */
574         if (kbd_flags & STP)
575             kbd_input = 0x13;
576         else
577             kbd_input = 0x11;
578     }
579 }
580
581 /******************************************************************************/
582
583 static void kbd_alt (unsigned char scan_code)
584 {
585     if ((kbd_flags & BRK) == BRK)
586     {
587         kbd_state = AS;
588         kbd_flags &= (~ALT);
589     }
590     else
591     {
592         kbd_state = AK;
593         kbd_flags &= ALT;
594     }
595 }
596
597
598 /******************************************************************************/
599
600 static void kbd_led_set (void)
601 {
602     kbd_input_empty();
603     out8 (I8042_DATA_REG, 0xed);        /* SET LED command */
604     kbd_input_empty();
605     out8 (I8042_DATA_REG, (kbd_flags & 0x7));    /* LED bits only */
606 }
607
608
609 /******************************************************************************/
610
611 static int kbd_input_empty (void)
612 {
613     int kbdTimeout = KBD_TIMEOUT;
614
615     /* wait for input buf empty */
616     while ((in8 (I8042_STATUS_REG) & 0x02) && kbdTimeout--)
617         udelay(1000);
618
619     return kbdTimeout;
620 }
621
622 /******************************************************************************/
623
624 static int kbd_reset (void)
625 {
626     if (kbd_input_empty() == 0)
627         return -1;
628
629     out8 (I8042_DATA_REG, 0xff);
630
631     udelay(250000);
632
633     if (kbd_input_empty() == 0)
634         return -1;
635
636     out8 (I8042_DATA_REG, 0x60);
637
638     if (kbd_input_empty() == 0)
639         return -1;
640
641     out8 (I8042_DATA_REG, 0x45);
642
643
644     if (kbd_input_empty() == 0)
645         return -1;
646
647     out8 (I8042_COMMAND_REG, 0xae);
648
649     if (kbd_input_empty() == 0)
650         return -1;
651
652     return 0;
653 }
654
655 #endif /* CONFIG_I8042_KBD */