xref: /openbmc/u-boot/drivers/input/i8042.c (revision 61fb15c5)
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 #ifdef CONFIG_USE_CPCIDVI
33 extern u8  gt_cpcidvi_in8(u32 offset);
34 extern void gt_cpcidvi_out8(u32 offset, u8 data);
35 
36 #define in8(a)	   gt_cpcidvi_in8(a)
37 #define out8(a, b) gt_cpcidvi_out8(a,b)
38 #endif
39 
40 #include <i8042.h>
41 
42 /* defines */
43 
44 #ifdef CONFIG_CONSOLE_CURSOR
45 extern void console_cursor (int state);
46 static int blinkCount = CFG_CONSOLE_BLINK_COUNT;
47 static int cursor_state = 0;
48 #endif
49 
50 /* locals */
51 
52 static int  kbd_input	 = -1;		/* no input yet */
53 static int  kbd_mapping	 = KBD_US;	/* default US keyboard */
54 static int  kbd_flags	 = NORMAL;	/* after reset */
55 static int  kbd_state	 = 0;		/* unshift code */
56 
57 static void kbd_conv_char (unsigned char scan_code);
58 static void kbd_led_set (void);
59 static void kbd_normal (unsigned char scan_code);
60 static void kbd_shift (unsigned char scan_code);
61 static void kbd_ctrl (unsigned char scan_code);
62 static void kbd_num (unsigned char scan_code);
63 static void kbd_caps (unsigned char scan_code);
64 static void kbd_scroll (unsigned char scan_code);
65 static void kbd_alt (unsigned char scan_code);
66 static int  kbd_input_empty (void);
67 static int  kbd_reset (void);
68 
69 static unsigned char kbd_fct_map [144] =
70     { /* kbd_fct_map table for scan code */
71     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   */
89     };
90 
91 static unsigned char kbd_key_map [2][5][144] =
92     {
93     { /* US keyboard */
94     { /* unshift code */
95     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 */
113     },
114     { /* shift code */
115     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 */
133     },
134     { /* control code */
135  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 */
153     },
154     { /* non numeric code */
155     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 */
173     },
174     { /* right alt mode - not used in US keyboard */
175  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    */
193     }
194     },
195     { /* german keyboard */
196     { /* unshift code */
197     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 */
215     },
216     { /* shift code */
217     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 */
235     },
236     { /* control code */
237  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 */
255     },
256     { /* non numeric code */
257     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 */
275     },
276     { /* Right alt mode - is used in German keyboard */
277  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    */
295     }
296     }
297     };
298 
299 static 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  */
324 int i8042_kbd_init (void)
325 {
326     int keymap, try;
327     char *penv;
328 
329 #ifdef CONFIG_USE_CPCIDVI
330     if ((penv = getenv ("console")) != NULL) {
331 	    if (strncmp (penv, "serial", 7) == 0) {
332 		    return -1;
333 	    }
334     }
335 #endif
336     /* Init keyboard device (default US layout) */
337     keymap = KBD_US;
338     if ((penv = getenv ("keymap")) != NULL)
339     {
340 	if (strncmp (penv, "de", 3) == 0)
341 	keymap = KBD_GER;
342     }
343 
344     for (try = 0; try < KBD_RESET_TRIES; try++)
345     {
346 	if (kbd_reset() == 0)
347 	{
348 	    kbd_mapping	  = keymap;
349 	    kbd_flags	  = NORMAL;
350 	    kbd_state	  = 0;
351 	    kbd_led_set();
352 	    return 0;
353 	    }
354     }
355     return -1;
356 }
357 
358 
359 /*******************************************************************************
360  *
361  * i8042_tstc - test if keyboard input is available
362  *		option: cursor blinking if called in a loop
363  */
364 int i8042_tstc (void)
365 {
366     unsigned char scan_code = 0;
367 
368 #ifdef CONFIG_CONSOLE_CURSOR
369     if (--blinkCount == 0)
370     {
371 	cursor_state ^= 1;
372 	console_cursor (cursor_state);
373 	blinkCount = CFG_CONSOLE_BLINK_COUNT;
374 	udelay (10);
375     }
376 #endif
377 
378     if ((in8 (I8042_STATUS_REG) & 0x01) == 0)
379 	return 0;
380     else
381     {
382 	scan_code = in8 (I8042_DATA_REG);
383 	if (scan_code == 0xfa)
384 	    return 0;
385 
386 	kbd_conv_char(scan_code);
387 
388 	if (kbd_input != -1)
389 	    return 1;
390     }
391     return 0;
392 }
393 
394 
395 /*******************************************************************************
396  *
397  * i8042_getc - wait till keyboard input is available
398  *		option: turn on/off cursor while waiting
399  */
400 int i8042_getc (void)
401 {
402     int ret_chr;
403     unsigned char scan_code;
404 
405     while (kbd_input == -1)
406     {
407 	while ((in8 (I8042_STATUS_REG) & 0x01) == 0)
408 	{
409 #ifdef CONFIG_CONSOLE_CURSOR
410 	    if (--blinkCount==0)
411 	    {
412 		cursor_state ^= 1;
413 		console_cursor (cursor_state);
414 		blinkCount = CFG_CONSOLE_BLINK_COUNT;
415 	    }
416 	    udelay (10);
417 #endif
418 	}
419 
420 	scan_code = in8 (I8042_DATA_REG);
421 
422 	if (scan_code != 0xfa)
423 	kbd_conv_char (scan_code);
424     }
425     ret_chr = kbd_input;
426     kbd_input = -1;
427     return ret_chr;
428 }
429 
430 
431 /******************************************************************************/
432 
433 static void kbd_conv_char (unsigned char scan_code)
434 {
435     if (scan_code == 0xe0)
436     {
437 	kbd_flags |= EXT;
438 	return;
439     }
440 
441     /* if high bit of scan_code, set break flag */
442     if (scan_code & 0x80)
443 	kbd_flags |=  BRK;
444     else
445 	kbd_flags &= ~BRK;
446 
447     if ((scan_code == 0xe1) || (kbd_flags & E1))
448     {
449 	if (scan_code == 0xe1)
450 	{
451 	    kbd_flags ^= BRK;	  /* reset the break flag */
452 	    kbd_flags ^= E1;	  /* bitwise EXOR with E1 flag */
453 	}
454 	return;
455     }
456 
457     scan_code &= 0x7f;
458 
459     if (kbd_flags & EXT)
460     {
461 	int i;
462 
463 	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;
475     }
476 
477     switch (kbd_fct_map [scan_code])
478     {
479     case AS:  kbd_normal (scan_code);
480 	break;
481     case SH:  kbd_shift (scan_code);
482 	break;
483     case CN:  kbd_ctrl (scan_code);
484 	break;
485     case NM:  kbd_num (scan_code);
486 	break;
487     case CP:  kbd_caps (scan_code);
488 	break;
489     case ST:  kbd_scroll (scan_code);
490 	break;
491     case AK:  kbd_alt (scan_code);
492 	break;
493     }
494     return;
495 }
496 
497 
498 /******************************************************************************/
499 
500 static 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))
508 	{
509 	    return;
510 	}
511 
512 	/* if caps lock convert upper to lower */
513 	if (((kbd_flags & CAPS) == CAPS) && (chr >= 'a' && chr <= 'z'))
514        {
515 	   chr -= 'a' - 'A';
516        }
517        kbd_input = chr;
518     }
519 }
520 
521 
522 /******************************************************************************/
523 
524 static void kbd_shift (unsigned char scan_code)
525 {
526     if ((kbd_flags & BRK) == BRK)
527     {
528 	kbd_state = AS;
529 	kbd_flags &= (~SHIFT);
530     }
531     else
532     {
533        kbd_state = SH;
534        kbd_flags |= SHIFT;
535     }
536 }
537 
538 
539 /******************************************************************************/
540 
541 static 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 
558 static void kbd_caps (unsigned char scan_code)
559 {
560     if ((kbd_flags & BRK) == NORMAL)
561     {
562        kbd_flags ^= CAPS;
563        kbd_led_set ();		  /* update keyboard LED */
564     }
565 }
566 
567 
568 /******************************************************************************/
569 
570 static 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;
576        kbd_led_set ();		  /* update keyboard LED */
577     }
578 }
579 
580 
581 /******************************************************************************/
582 
583 static void kbd_scroll (unsigned char scan_code)
584 {
585     if ((kbd_flags & BRK) == NORMAL)
586     {
587 	kbd_flags ^= STP;
588 	kbd_led_set ();		   /* update keyboard LED */
589 	if (kbd_flags & STP)
590 	    kbd_input = 0x13;
591 	else
592 	    kbd_input = 0x11;
593     }
594 }
595 
596 /******************************************************************************/
597 
598 static void kbd_alt (unsigned char scan_code)
599 {
600     if ((kbd_flags & BRK) == BRK)
601     {
602 	kbd_state = AS;
603 	kbd_flags &= (~ALT);
604     }
605     else
606     {
607 	kbd_state = AK;
608 	kbd_flags &= ALT;
609     }
610 }
611 
612 
613 /******************************************************************************/
614 
615 static void kbd_led_set (void)
616 {
617     kbd_input_empty();
618     out8 (I8042_DATA_REG, 0xed);	/* SET LED command */
619     kbd_input_empty();
620     out8 (I8042_DATA_REG, (kbd_flags & 0x7));	 /* LED bits only */
621 }
622 
623 
624 /******************************************************************************/
625 
626 static 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--)
632 	udelay(1000);
633 
634     return kbdTimeout;
635 }
636 
637 /******************************************************************************/
638 
639 static int kbd_reset (void)
640 {
641     if (kbd_input_empty() == 0)
642 	return -1;
643 
644     out8 (I8042_DATA_REG, 0xff);
645 
646     udelay(250000);
647 
648     if (kbd_input_empty() == 0)
649 	return -1;
650 
651 #ifdef CONFIG_USE_CPCIDVI
652     out8 (I8042_COMMAND_REG, 0x60);
653 #else
654     out8 (I8042_DATA_REG, 0x60);
655 #endif
656 
657     if (kbd_input_empty() == 0)
658 	return -1;
659 
660     out8 (I8042_DATA_REG, 0x45);
661 
662 
663     if (kbd_input_empty() == 0)
664 	return -1;
665 
666     out8 (I8042_COMMAND_REG, 0xae);
667 
668     if (kbd_input_empty() == 0)
669 	return -1;
670 
671     return 0;
672 }
673 
674 #endif /* CONFIG_I8042_KBD */
675