1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
/*
* This file is part of the libpayload project.
*
* Copyright (C) 2008 Advanced Micro Devices, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <keycodes.h>
#include <libpayload-config.h>
#include <libpayload.h>
#define I8042_CMD_DIS_KB 0xad
struct layout_maps {
const char *country;
const unsigned short map[4][0x57];
};
static struct layout_maps *map;
static struct layout_maps keyboard_layouts[] = {
#if IS_ENABLED(CONFIG_LP_PC_KEYBOARD_LAYOUT_US)
{ .country = "us", .map = {
{ /* No modifier */
0x00, 0x1B, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
0x37, 0x38, 0x39, 0x30, 0x2D, 0x3D, 0x08, 0x09,
0x71, 0x77, 0x65, 0x72, 0x74, 0x79, 0x75, 0x69,
0x6F, 0x70, 0x5B, 0x5D, 0x0A, 0x00, 0x61, 0x73,
0x64, 0x66, 0x67, 0x68, 0x6A, 0x6B, 0x6C, 0x3B,
0x27, 0x60, 0x00, 0x5C, 0x7A, 0x78, 0x63, 0x76,
0x62, 0x6E, 0x6D, 0x2C, 0x2E, 0x2F, 0x00, 0x2A,
0x00, 0x20, 0x00, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5),
KEY_F(6), KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10), 0x00, 0x00, KEY_HOME,
KEY_UP, KEY_NPAGE, 0x00, KEY_LEFT, 0x00, KEY_RIGHT, 0x00, KEY_END,
KEY_DOWN, KEY_PPAGE, 0x00, KEY_DC, 0x00, 0x00, 0x00
},
{ /* Shift */
0x00, 0x1B, 0x21, 0x40, 0x23, 0x24, 0x25, 0x5E,
0x26, 0x2A, 0x28, 0x29, 0x5F, 0x2B, 0x08, 0x00,
0x51, 0x57, 0x45, 0x52, 0x54, 0x59, 0x55, 0x49,
0x4F, 0x50, 0x7B, 0x7D, 0x0A, 0x00, 0x41, 0x53,
0x44, 0x46, 0x47, 0x48, 0x4A, 0x4B, 0x4C, 0x3A,
0x22, 0x7E, 0x00, 0x7C, 0x5A, 0x58, 0x43, 0x56,
0x42, 0x4E, 0x4D, 0x3C, 0x3E, 0x3F, 0x00, 0x2A,
0x00, 0x20, 0x00, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5),
KEY_F(6), KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10), 0x00, 0x00, KEY_HOME,
KEY_UP, KEY_NPAGE, 0x00, KEY_LEFT, 0x00, KEY_RIGHT, 0x00, KEY_END,
KEY_DOWN, KEY_PPAGE, 0x00, KEY_DC, 0x00, 0x00, 0x00
},
{ /* ALT */
0x00, 0x1B, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
0x37, 0x38, 0x39, 0x30, 0x2D, 0x3D, 0x08, 0x09,
0x71, 0x77, 0x65, 0x72, 0x74, 0x79, 0x75, 0x69,
0x6F, 0x70, 0x5B, 0x5D, 0x0A, 0x00, 0x61, 0x73,
0x64, 0x66, 0x67, 0x68, 0x6A, 0x6B, 0x6C, 0x3B,
0x27, 0x60, 0x00, 0x5C, 0x7A, 0x78, 0x63, 0x76,
0x62, 0x6E, 0x6D, 0x2C, 0x2E, 0x2F, 0x00, 0x2A,
0x00, 0x20, 0x00, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5),
KEY_F(6), KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10), 0x00, 0x00, KEY_HOME,
KEY_UP, KEY_NPAGE, 0x00, KEY_LEFT, 0x00, KEY_RIGHT, 0x00, KEY_END,
KEY_DOWN, KEY_PPAGE, 0x00, KEY_DC, 0x00, 0x00, 0x00
},
{ /* Shift-ALT */
0x00, 0x1B, 0x21, 0x40, 0x23, 0x24, 0x25, 0x5E,
0x26, 0x2A, 0x28, 0x29, 0x5F, 0x2B, 0x08, 0x00,
0x51, 0x57, 0x45, 0x52, 0x54, 0x59, 0x55, 0x49,
0x4F, 0x50, 0x7B, 0x7D, 0x0A, 0x00, 0x41, 0x53,
0x44, 0x46, 0x47, 0x48, 0x4A, 0x4B, 0x4C, 0x3A,
0x22, 0x7E, 0x00, 0x7C, 0x5A, 0x58, 0x43, 0x56,
0x42, 0x4E, 0x4D, 0x3C, 0x3E, 0x3F, 0x00, 0x2A,
0x00, 0x20, 0x00, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5),
KEY_F(6), KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10), 0x00, 0x00, KEY_HOME,
KEY_UP, KEY_NPAGE, 0x00, KEY_LEFT, 0x00, KEY_RIGHT, 0x00, KEY_END,
KEY_DOWN, KEY_PPAGE, 0x00, KEY_DC, 0x00, 0x00, 0x00
}
}},
#endif
#if IS_ENABLED(CONFIG_LP_PC_KEYBOARD_LAYOUT_DE)
{ .country = "de", .map = {
{ /* No modifier */
0x00, 0x1B, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
0x37, 0x38, 0x39, 0x30, 0x00, 0x27, 0x08, 0x09,
0x71, 0x77, 0x65, 0x72, 0x74, 0x7A, 0x75, 0x69,
0x6F, 0x70, 0x00, 0x2B, 0x0A, 0x00, 0x61, 0x73,
0x64, 0x66, 0x67, 0x68, 0x6A, 0x6B, 0x6C, 0x00,
0x00, 0x5E, 0x00, 0x23, 0x79, 0x78, 0x63, 0x76,
0x62, 0x6E, 0x6D, 0x2C, 0x2E, 0x2D, 0x00, 0x2A,
0x00, 0x20, 0x00, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5),
KEY_F(6), KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10), 0x00, 0x00, KEY_HOME,
KEY_UP, KEY_NPAGE, 0x00, KEY_LEFT, 0x00, KEY_RIGHT, 0x00, KEY_END,
KEY_DOWN, KEY_PPAGE, 0x00, KEY_DC, 0x00, 0x00, 0x3C
},
{ /* Shift */
0x00, 0x1B, 0x21, 0x22, 0xA7, 0x24, 0x25, 0x26,
0x2F, 0x28, 0x29, 0x3D, 0x3F, 0x60, 0x08, 0x00,
0x51, 0x57, 0x45, 0x52, 0x54, 0x5A, 0x55, 0x49,
0x4F, 0x50, 0x00, 0x2A, 0x0A, 0x00, 0x41, 0x53,
0x44, 0x46, 0x47, 0x48, 0x4A, 0x4B, 0x4C, 0x00,
0x00, 0x7E, 0x00, 0x27, 0x59, 0x58, 0x43, 0x56,
0x42, 0x4E, 0x4D, 0x3B, 0x3A, 0x5F, 0x00, 0x2A,
0x00, 0x20, 0x00, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5),
KEY_F(6), KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10), 0x00, 0x00, KEY_HOME,
KEY_UP, KEY_NPAGE, 0x00, KEY_LEFT, 0x00, KEY_RIGHT, 0x00, KEY_END,
KEY_DOWN, KEY_PPAGE, 0x00, KEY_DC, 0x00, 0x00, 0x3E
},
{ /* ALT */
0x00, 0x1B, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
0x7B, 0x5B, 0x5D, 0x7D, 0x5C, 0x3D, 0x08, 0x09,
0x40, 0x77, 0x65, 0x72, 0x74, 0x79, 0x75, 0x69,
0x6F, 0x70, 0x5B, 0x7E, 0x0A, 0x00, 0x61, 0x73,
0x64, 0x66, 0x67, 0x68, 0x6A, 0x6B, 0x6C, 0x3B,
0x27, 0x60, 0x00, 0x5C, 0x7A, 0x78, 0x63, 0x76,
0x62, 0x6E, 0x6D, 0x2C, 0x2E, 0x2F, 0x00, 0x2A,
0x00, 0x20, 0x00, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5),
KEY_F(6), KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10), 0x00, 0x00, KEY_HOME,
KEY_UP, KEY_NPAGE, 0x00, KEY_LEFT, 0x00, KEY_RIGHT, 0x00, KEY_END,
KEY_DOWN, KEY_PPAGE, 0x00, KEY_DC, 0x00, 0x00, 0x7C
},
{ /* Shift-ALT */
/* copied from US */
0x00, 0x1B, 0x21, 0x40, 0x23, 0x24, 0x25, 0x5E,
0x26, 0x2A, 0x28, 0x29, 0x5F, 0x2B, 0x08, 0x00,
0x51, 0x57, 0x45, 0x52, 0x54, 0x59, 0x55, 0x49,
0x4F, 0x50, 0x7B, 0x7D, 0x0A, 0x00, 0x41, 0x53,
0x44, 0x46, 0x47, 0x48, 0x4A, 0x4B, 0x4C, 0x3A,
0x22, 0x7E, 0x00, 0x7C, 0x5A, 0x58, 0x43, 0x56,
0x42, 0x4E, 0x4D, 0x3C, 0x3E, 0x3F, 0x00, 0x2A,
0x00, 0x20, 0x00, KEY_F(1), KEY_F(2), KEY_F(3), KEY_F(4), KEY_F(5),
KEY_F(6), KEY_F(7), KEY_F(8), KEY_F(9), KEY_F(10), 0x00, 0x00, KEY_HOME,
KEY_UP, KEY_NPAGE, 0x00, KEY_LEFT, 0x00, KEY_RIGHT, 0x00, KEY_END,
KEY_DOWN, KEY_PPAGE, 0x00, KEY_DC, 0x00, 0x00, 0x00
}
}},
#endif
};
#define MOD_SHIFT (1 << 0)
#define MOD_CTRL (1 << 1)
#define MOD_CAPSLOCK (1 << 2)
#define MOD_ALT (1 << 3)
static void keyboard_cmd(unsigned char cmd, unsigned char val)
{
while (inb(0x64) & 2);
outb(cmd, 0x60);
mdelay(20);
while (inb(0x64) & 2);
outb(val, 0x60);
mdelay(20);
}
int keyboard_havechar(void)
{
unsigned char c = inb(0x64);
return (c == 0xFF) ? 0 : c & 1;
}
unsigned char keyboard_get_scancode(void)
{
unsigned char ch = 0;
if (keyboard_havechar())
ch = inb(0x60);
return ch;
}
int keyboard_getchar(void)
{
static int modifier = 0;
unsigned char ch;
int shift;
int ret = 0;
while (!keyboard_havechar()) ;
ch = keyboard_get_scancode();
switch (ch) {
case 0x36:
case 0x2a:
modifier |= MOD_SHIFT;
break;
case 0x80 | 0x36:
case 0x80 | 0x2a:
modifier &= ~MOD_SHIFT;
break;
case 0x38:
modifier |= MOD_ALT;
break;
case 0x80 | 0x38:
modifier &= ~MOD_ALT;
break;
case 0x1d:
modifier |= MOD_CTRL;
break;
case 0x80 | 0x1d:
modifier &= ~MOD_CTRL;
break;
case 0x3a:
if (modifier & MOD_CAPSLOCK) {
modifier &= ~MOD_CAPSLOCK;
keyboard_cmd(0xed, (0 << 2));
} else {
modifier |= MOD_CAPSLOCK;
keyboard_cmd(0xed, (1 << 2));
}
break;
}
if (!(ch & 0x80) && ch < 0x57) {
shift =
(modifier & MOD_SHIFT) ^ (modifier & MOD_CAPSLOCK) ? 1 : 0;
if (modifier & MOD_ALT)
shift += 2;
ret = map->map[shift][ch];
if (modifier & MOD_CTRL) {
switch (ret) {
case 'a' ... 'z':
ret &= 0x1f;
break;
case KEY_DC:
/* vulcan nerve pinch */
if ((modifier & MOD_ALT) && reset_handler)
reset_handler();
default:
ret = 0;
}
}
}
return ret;
}
static int keyboard_wait_write(void)
{
int retries = 10000;
while(retries-- && (inb(0x64) & 0x02))
udelay(50);
return (retries <= 0) ? -1 : 0;
}
/**
* Set keyboard layout
* @param country string describing the keyboard layout language.
* Valid values are "us", "de".
*/
int keyboard_set_layout(char *country)
{
int i;
for (i=0; i<ARRAY_SIZE(keyboard_layouts); i++) {
if (strncmp(keyboard_layouts[i].country, country,
strlen(keyboard_layouts[i].country)))
continue;
/* Found, changing keyboard layout */
map = &keyboard_layouts[i];
return 0;
}
/* Nothing found, not changed */
return -1;
}
static struct console_input_driver cons = {
.havekey = keyboard_havechar,
.getchar = keyboard_getchar
};
void keyboard_init(void)
{
map = &keyboard_layouts[0];
/* If 0x64 returns 0xff, then we have no keyboard
* controller */
if (inb(0x64) == 0xFF)
return;
/* Empty keyboard buffer */
while (keyboard_havechar()) keyboard_getchar();
console_add_input_driver(&cons);
}
void keyboard_disconnect(void)
{
/* If 0x64 returns 0xff, then we have no keyboard
* controller */
if (inb(0x64) == 0xFF)
return;
/* Empty keyboard buffer */
while (keyboard_havechar())
keyboard_getchar();
/* Send keyboard disconnect command */
outb(I8042_CMD_DIS_KB, 0x64);
keyboard_wait_write();
/* Hand off with empty buffer */
while (keyboard_havechar())
keyboard_getchar();
}
|