summaryrefslogtreecommitdiff
path: root/drivers/input/i8042.c
blob: 87220cad356c3d1ddb93a85e89a0a123c579da0c (plain)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/*
 * (C) Copyright 2002 ELTEC Elektronik AG
 * Frank Gottschling <fgottschling@eltec.de>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

/* i8042.c - Intel 8042 keyboard driver routines */

/* includes */

#include <common.h>
#include <i8042.h>

/* defines */

#ifdef CONFIG_CONSOLE_CURSOR
extern void console_cursor(int state);
static int blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
static int cursor_state;
#endif

/* locals */

static int  kbd_input	 = -1;		/* no input yet */
static int  kbd_mapping	 = KBD_US;	/* default US keyboard */
static int  kbd_flags	 = NORMAL;	/* after reset */
static int  kbd_state;			/* unshift code */

static void kbd_conv_char(unsigned char scan_code);
static void kbd_led_set(void);
static void kbd_normal(unsigned char scan_code);
static void kbd_shift(unsigned char scan_code);
static void kbd_ctrl(unsigned char scan_code);
static void kbd_num(unsigned char scan_code);
static void kbd_caps(unsigned char scan_code);
static void kbd_scroll(unsigned char scan_code);
static void kbd_alt(unsigned char scan_code);
static int  kbd_input_empty(void);
static int  kbd_reset(void);

static unsigned char kbd_fct_map[144] = {
	/* kbd_fct_map table for scan code */
	 0,  AS,  AS,  AS,  AS,  AS,  AS,  AS, /* scan  0- 7 */
	AS,  AS,  AS,  AS,  AS,  AS,  AS,  AS, /* scan  8- F */
	AS,  AS,  AS,  AS,  AS,  AS,  AS,  AS, /* scan 10-17 */
	AS,  AS,  AS,  AS,  AS,  CN,  AS,  AS, /* scan 18-1F */
	AS,  AS,  AS,  AS,  AS,  AS,  AS,  AS, /* scan 20-27 */
	AS,  AS,  SH,  AS,  AS,  AS,  AS,  AS, /* scan 28-2F */
	AS,  AS,  AS,  AS,  AS,  AS,  SH,  AS, /* scan 30-37 */
	AS,  AS,  CP,   0,   0,   0,   0,   0, /* scan 38-3F */
	 0,   0,   0,   0,   0,  NM,  ST,  ES, /* scan 40-47 */
	ES,  ES,  ES,  ES,  ES,  ES,  ES,  ES, /* scan 48-4F */
	ES,  ES,  ES,  ES,   0,   0,  AS,   0, /* scan 50-57 */
	 0,   0,   0,   0,   0,   0,   0,   0, /* scan 58-5F */
	 0,   0,   0,   0,   0,   0,   0,   0, /* scan 60-67 */
	 0,   0,   0,   0,   0,   0,   0,   0, /* scan 68-6F */
	AS,   0,   0,  AS,   0,   0,  AS,   0, /* scan 70-77 */
	 0,  AS,   0,   0,   0,  AS,   0,   0, /* scan 78-7F */
	AS,  CN,  AS,  AS,  AK,  ST,  EX,  EX, /* enhanced */
	AS,  EX,  EX,  AS,  EX,  AS,  EX,  EX  /* enhanced */
	};

static unsigned char kbd_key_map[2][5][144] = {
	{ /* US keyboard */
	{ /* unshift code */
	   0, 0x1b,  '1',  '2',  '3',  '4',  '5',  '6', /* scan  0- 7 */
	 '7',  '8',  '9',  '0',  '-',  '=', 0x08, '\t', /* scan  8- F */
	 'q',  'w',  'e',  'r',  't',  'y',  'u',  'i', /* scan 10-17 */
	 'o',  'p',  '[',  ']', '\r',   CN,  'a',  's', /* scan 18-1F */
	 'd',  'f',  'g',  'h',  'j',  'k',  'l',  ';', /* scan 20-27 */
	'\'',  '`',   SH, '\\',  'z',  'x',  'c',  'v', /* scan 28-2F */
	 'b',  'n',  'm',  ',',  '.',  '/',   SH,  '*', /* scan 30-37 */
	 ' ',  ' ',   CP,    0,    0,    0,    0,    0, /* scan 38-3F */
	   0,    0,    0,    0,    0,   NM,   ST,  '7', /* scan 40-47 */
	 '8',  '9',  '-',  '4',  '5',  '6',  '+',  '1', /* scan 48-4F */
	 '2',  '3',  '0',  '.',    0,    0,    0,    0, /* scan 50-57 */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 58-5F */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 60-67 */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 68-6F */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 70-77 */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 78-7F */
	'\r',   CN,  '/',  '*',  ' ',   ST,  'F',  'A', /* extended */
	   0,  'D',  'C',    0,  'B',    0,  '@',  'P'  /* extended */
	},
	{ /* shift code */
	   0, 0x1b,  '!',  '@',  '#',  '$',  '%',  '^', /* scan  0- 7 */
	 '&',  '*',  '(',  ')',  '_',  '+', 0x08, '\t', /* scan  8- F */
	 'Q',  'W',  'E',  'R',  'T',  'Y',  'U',  'I', /* scan 10-17 */
	 'O',  'P',  '{',  '}', '\r',   CN,  'A',  'S', /* scan 18-1F */
	 'D',  'F',  'G',  'H',  'J',  'K',  'L',  ':', /* scan 20-27 */
	 '"',  '~',   SH,  '|',  'Z',  'X',  'C',  'V', /* scan 28-2F */
	 'B',  'N',  'M',  '<',  '>',  '?',   SH,  '*', /* scan 30-37 */
	 ' ',  ' ',   CP,    0,    0,    0,    0,    0, /* scan 38-3F */
	   0,    0,    0,    0,    0,   NM,   ST,  '7', /* scan 40-47 */
	 '8',  '9',  '-',  '4',  '5',  '6',  '+',  '1', /* scan 48-4F */
	 '2',  '3',  '0',  '.',    0,    0,    0,    0, /* scan 50-57 */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 58-5F */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 60-67 */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 68-6F */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 70-77 */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 78-7F */
	'\r',   CN,  '/',  '*',  ' ',   ST,  'F',  'A', /* extended */
	   0,  'D',  'C',    0,  'B',    0,  '@',  'P'  /* extended */
	},
	{ /* control code */
	0xff, 0x1b, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, /* scan  0- 7 */
	0x1e, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, '\t', /* scan  8- F */
	0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, /* scan 10-17 */
	0x0f, 0x10, 0x1b, 0x1d, '\r',   CN, 0x01, 0x13, /* scan 18-1F */
	0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0xff, /* scan 20-27 */
	0xff, 0x1c,   SH, 0xff, 0x1a, 0x18, 0x03, 0x16, /* scan 28-2F */
	0x02, 0x0e, 0x0d, 0xff, 0xff, 0xff,   SH, 0xff, /* scan 30-37 */
	0xff, 0xff,   CP, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
	0xff, 0xff, 0xff, 0xff, 0xff,   NM,   ST, 0xff, /* scan 40-47 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
	'\r',   CN,  '/',  '*',  ' ',   ST, 0xff, 0xff, /* extended */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff  /* extended */
	},
	{ /* non numeric code */
	   0, 0x1b,  '1',  '2',  '3',  '4',  '5',  '6', /* scan  0- 7 */
	 '7',  '8',  '9',  '0',  '-',  '=', 0x08, '\t', /* scan  8- F */
	 'q',  'w',  'e',  'r',  't',  'y',  'u',  'i', /* scan 10-17 */
	 'o',  'p',  '[',  ']', '\r',   CN,  'a',  's', /* scan 18-1F */
	 'd',  'f',  'g',  'h',  'j',  'k',  'l',  ';', /* scan 20-27 */
	'\'',  '`',   SH, '\\',  'z',  'x',  'c',  'v', /* scan 28-2F */
	 'b',  'n',  'm',  ',',  '.',  '/',   SH,  '*', /* scan 30-37 */
	 ' ',  ' ',   CP,    0,    0,    0,    0,    0, /* scan 38-3F */
	   0,    0,    0,    0,    0,   NM,   ST,  'w', /* scan 40-47 */
	 'x',  'y',  'l',  't',  'u',  'v',  'm',  'q', /* scan 48-4F */
	 'r',  's',  'p',  'n',    0,    0,    0,    0, /* scan 50-57 */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 58-5F */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 60-67 */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 68-6F */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 70-77 */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 78-7F */
	'\r',   CN,  '/',  '*',  ' ',   ST,  'F',  'A', /* extended */
	   0,  'D',  'C',    0,  'B',    0,  '@',  'P'  /* extended */
	},
	{ /* right alt mode - not used in US keyboard */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan  0 - 7 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 8 - F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10 -17 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 18 -1F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20 -27 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28 -2F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30 -37 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38 -3F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40 -47 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48 -4F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50 -57 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58 -5F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60 -67 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68 -6F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70 -77 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78 -7F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* extended */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff  /* extended */
	}
	},
	{ /* german keyboard */
	{ /* unshift code */
	   0, 0x1b,  '1',  '2',  '3',  '4',  '5',  '6', /* scan  0- 7 */
	 '7',  '8',  '9',  '0', 0xe1, '\'', 0x08, '\t', /* scan  8- F */
	 'q',  'w',  'e',  'r',  't',  'z',  'u',  'i', /* scan 10-17 */
	 'o',  'p', 0x81,  '+', '\r',   CN,  'a',  's', /* scan 18-1F */
	 'd',  'f',  'g',  'h',  'j',  'k',  'l', 0x94, /* scan 20-27 */
	0x84,  '^',   SH,  '#',  'y',  'x',  'c',  'v', /* scan 28-2F */
	 'b',  'n',  'm',  ',',  '.',  '-',   SH,  '*', /* scan 30-37 */
	 ' ',  ' ',   CP,    0,    0,    0,    0,    0, /* scan 38-3F */
	   0,    0,    0,    0,    0,   NM,   ST,  '7', /* scan 40-47 */
	 '8',  '9',  '-',  '4',  '5',  '6',  '+',  '1', /* scan 48-4F */
	 '2',  '3',  '0',  ',',    0,    0,  '<',    0, /* scan 50-57 */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 58-5F */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 60-67 */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 68-6F */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 70-77 */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 78-7F */
	'\r',   CN,  '/',  '*',  ' ',   ST,  'F',  'A', /* extended */
	   0,  'D',  'C',    0,  'B',    0,  '@',  'P'  /* extended */
	},
	{ /* shift code */
	   0, 0x1b,  '!',  '"', 0x15,  '$',  '%',  '&', /* scan  0- 7 */
	 '/',  '(',  ')',  '=',  '?',  '`', 0x08, '\t', /* scan  8- F */
	 'Q',  'W',  'E',  'R',  'T',  'Z',  'U',  'I', /* scan 10-17 */
	 'O',  'P', 0x9a,  '*', '\r',   CN,  'A',  'S', /* scan 18-1F */
	 'D',  'F',  'G',  'H',  'J',  'K',  'L', 0x99, /* scan 20-27 */
	0x8e, 0xf8,   SH, '\'',  'Y',  'X',  'C',  'V', /* scan 28-2F */
	 'B',  'N',  'M',  ';',  ':',  '_',   SH,  '*', /* scan 30-37 */
	 ' ',  ' ',   CP,    0,    0,    0,    0,    0, /* scan 38-3F */
	   0,    0,    0,    0,    0,   NM,   ST,  '7', /* scan 40-47 */
	 '8',  '9',  '-',  '4',  '5',  '6',  '+',  '1', /* scan 48-4F */
	 '2',  '3',  '0',  ',',    0,    0,  '>',    0, /* scan 50-57 */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 58-5F */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 60-67 */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 68-6F */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 70-77 */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 78-7F */
	'\r',   CN,  '/',  '*',  ' ',   ST,  'F',  'A', /* extended */
	   0,  'D',  'C',    0,  'B',    0,  '@',  'P'  /* extended */
	},
	{ /* control code */
	0xff, 0x1b, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, /* scan  0- 7 */
	0x1e, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, '\t', /* scan  8- F */
	0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, /* scan 10-17 */
	0x0f, 0x10, 0x1b, 0x1d, '\r',   CN, 0x01, 0x13, /* scan 18-1F */
	0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0xff, /* scan 20-27 */
	0xff, 0x1c,   SH, 0xff, 0x1a, 0x18, 0x03, 0x16, /* scan 28-2F */
	0x02, 0x0e, 0x0d, 0xff, 0xff, 0xff,   SH, 0xff, /* scan 30-37 */
	0xff, 0xff,   CP, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
	0xff, 0xff, 0xff, 0xff, 0xff,   NM,   ST, 0xff, /* scan 40-47 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
	'\r',   CN,  '/',  '*',  ' ',   ST, 0xff, 0xff, /* extended */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff  /* extended */
	},
	{ /* non numeric code */
	   0, 0x1b,  '1',  '2',  '3',  '4',  '5',  '6', /* scan  0- 7 */
	 '7',  '8',  '9',  '0', 0xe1, '\'', 0x08, '\t', /* scan  8- F */
	 'q',  'w',  'e',  'r',  't',  'z',  'u',  'i', /* scan 10-17 */
	 'o',  'p', 0x81,  '+', '\r',   CN,  'a',  's', /* scan 18-1F */
	 'd',  'f',  'g',  'h',  'j',  'k',  'l', 0x94, /* scan 20-27 */
	0x84,  '^',   SH,    0,  'y',  'x',  'c',  'v', /* scan 28-2F */
	 'b',  'n',  'm',  ',',  '.',  '-',   SH,  '*', /* scan 30-37 */
	 ' ',  ' ',   CP,    0,    0,    0,    0,    0, /* scan 38-3F */
	   0,    0,    0,    0,    0,   NM,   ST,  'w', /* scan 40-47 */
	 'x',  'y',  'l',  't',  'u',  'v',  'm',  'q', /* scan 48-4F */
	 'r',  's',  'p',  'n',    0,    0,  '<',    0, /* scan 50-57 */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 58-5F */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 60-67 */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 68-6F */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 70-77 */
	   0,    0,    0,    0,    0,    0,    0,    0, /* scan 78-7F */
	'\r',   CN,  '/',  '*',  ' ',   ST,  'F',  'A', /* extended */
	   0,  'D',  'C',    0,  'B',    0,  '@',  'P'  /* extended */
	},
	{ /* Right alt mode - is used in German keyboard */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan  0 - 7 */
	 '{',  '[',  ']',  '}', '\\', 0xff, 0xff, 0xff, /* scan  8 - F */
	 '@', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10 -17 */
	0xff, 0xff, 0xff,  '~', 0xff, 0xff, 0xff, 0xff, /* scan 18 -1F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20 -27 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28 -2F */
	0xff, 0xff, 0xe6, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30 -37 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38 -3F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40 -47 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48 -4F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff,  '|', 0xff, /* scan 50 -57 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58 -5F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60 -67 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68 -6F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70 -77 */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78 -7F */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* extended */
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff  /* extended */
	}
	}
	};

static unsigned char ext_key_map[] = {
	0x1c, /* keypad enter */
	0x1d, /* right control */
	0x35, /* keypad slash */
	0x37, /* print screen */
	0x38, /* right alt */
	0x46, /* break */
	0x47, /* editpad home */
	0x48, /* editpad up */
	0x49, /* editpad pgup */
	0x4b, /* editpad left */
	0x4d, /* editpad right */
	0x4f, /* editpad end */
	0x50, /* editpad dn */
	0x51, /* editpad pgdn */
	0x52, /* editpad ins */
	0x53, /* editpad del */
	0x00  /* map end */
	};

/******************************************************************************/

static int kbd_controller_present(void)
{
	return in8(I8042_STATUS_REG) != 0xff;
}

/*
 * Implement a weak default function for boards that optionally
 * need to skip the i8042 initialization.
 */
int __weak board_i8042_skip(void)
{
	/* As default, don't skip */
	return 0;
}

void i8042_flush(void)
{
	int timeout;

	/*
	 * The delay is to give the keyboard controller some time to fill the
	 * next byte.
	 */
	while (1) {
		timeout = 100;  /* wait for no longer than 100us */
		while (timeout > 0 && !(in8(I8042_STATUS_REG) & 0x01)) {
			udelay(1);
			timeout--;
		}

		/* Try to pull next byte if not timeout. */
		if (in8(I8042_STATUS_REG) & 0x01)
			in8(I8042_DATA_REG);
		else
			break;
	}
}

int i8042_disable(void)
{
	if (kbd_input_empty() == 0)
		return -1;

	/* Disable keyboard */
	out8(I8042_COMMAND_REG, 0xad);

	if (kbd_input_empty() == 0)
		return -1;

	return 0;
}


/*******************************************************************************
 *
 * i8042_kbd_init - reset keyboard and init state flags
 */
int i8042_kbd_init(void)
{
	int keymap, try;
	char *penv;

	if (!kbd_controller_present() || board_i8042_skip())
		return -1;

	/* Init keyboard device (default US layout) */
	keymap = KBD_US;
	penv = getenv("keymap");
	if (penv != NULL) {
		if (strncmp(penv, "de", 3) == 0)
			keymap = KBD_GER;
	}

	for (try = 0; try < KBD_RESET_TRIES; try++) {
		if (kbd_reset() == 0) {
			kbd_mapping = keymap;
			kbd_flags   = NORMAL;
			kbd_state   = 0;
			kbd_led_set();
			return 0;
		}
	}
	return -1;
}


/*******************************************************************************
 *
 * i8042_tstc - test if keyboard input is available
 *		option: cursor blinking if called in a loop
 */
int i8042_tstc(struct stdio_dev *dev)
{
	unsigned char scan_code = 0;

#ifdef CONFIG_CONSOLE_CURSOR
	if (--blinkCount == 0) {
		cursor_state ^= 1;
		console_cursor(cursor_state);
		blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
		udelay(10);
	}
#endif

	if ((in8(I8042_STATUS_REG) & 0x01) == 0) {
		return 0;
	} else {
		scan_code = in8(I8042_DATA_REG);
		if (scan_code == 0xfa)
			return 0;

		kbd_conv_char(scan_code);

		if (kbd_input != -1)
			return 1;
	}
	return 0;
}


/*******************************************************************************
 *
 * i8042_getc - wait till keyboard input is available
 *		option: turn on/off cursor while waiting
 */
int i8042_getc(struct stdio_dev *dev)
{
	int ret_chr;
	unsigned char scan_code;

	while (kbd_input == -1) {
		while ((in8(I8042_STATUS_REG) & 0x01) == 0) {
#ifdef CONFIG_CONSOLE_CURSOR
			if (--blinkCount == 0) {
				cursor_state ^= 1;
				console_cursor(cursor_state);
				blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
			}
			udelay(10);
#endif
		}
		scan_code = in8(I8042_DATA_REG);
		if (scan_code != 0xfa)
			kbd_conv_char (scan_code);
	}
	ret_chr = kbd_input;
	kbd_input = -1;
	return ret_chr;
}


/******************************************************************************/

static void kbd_conv_char(unsigned char scan_code)
{
	if (scan_code == 0xe0) {
		kbd_flags |= EXT;
		return;
	}

	/* if high bit of scan_code, set break flag */
	if (scan_code & 0x80)
		kbd_flags |=  BRK;
	else
		kbd_flags &= ~BRK;

	if ((scan_code == 0xe1) || (kbd_flags & E1)) {
		if (scan_code == 0xe1) {
			kbd_flags ^= BRK;    /* reset the break flag */
			kbd_flags ^= E1;     /* bitwise EXOR with E1 flag */
		}
		return;
	}

	scan_code &= 0x7f;

	if (kbd_flags & EXT) {
		int i;

		kbd_flags ^= EXT;
		for (i = 0; ext_key_map[i]; i++) {
			if (ext_key_map[i] == scan_code) {
				scan_code = 0x80 + i;
				break;
			}
		}
		/* not found ? */
		if (!ext_key_map[i])
			return;
	}

	switch (kbd_fct_map[scan_code]) {
	case AS:
		kbd_normal(scan_code);
		break;
	case SH:
		kbd_shift(scan_code);
		break;
	case CN:
		kbd_ctrl(scan_code);
		break;
	case NM:
		kbd_num(scan_code);
		break;
	case CP:
		kbd_caps(scan_code);
		break;
	case ST:
		kbd_scroll(scan_code);
		break;
	case AK:
		kbd_alt(scan_code);
		break;
	}
	return;
}


/******************************************************************************/

static void kbd_normal(unsigned char scan_code)
{
	unsigned char chr;

	if ((kbd_flags & BRK) == NORMAL) {
		chr = kbd_key_map[kbd_mapping][kbd_state][scan_code];
		if ((chr == 0xff) || (chr == 0x00))
			return;

		/* if caps lock convert upper to lower */
		if (((kbd_flags & CAPS) == CAPS) &&
				(chr >= 'a' && chr <= 'z')) {
			chr -= 'a' - 'A';
		}
		kbd_input = chr;
	}
}


/******************************************************************************/

static void kbd_shift(unsigned char scan_code)
{
	if ((kbd_flags & BRK) == BRK) {
		kbd_state = AS;
		kbd_flags &= (~SHIFT);
	} else {
		kbd_state = SH;
		kbd_flags |= SHIFT;
	}
}


/******************************************************************************/

static void kbd_ctrl(unsigned char scan_code)
{
	if ((kbd_flags & BRK) == BRK) {
		kbd_state = AS;
		kbd_flags &= (~CTRL);
	} else {
		kbd_state = CN;
		kbd_flags |= CTRL;
	}
}


/******************************************************************************/

static void kbd_caps(unsigned char scan_code)
{
	if ((kbd_flags & BRK) == NORMAL) {
		kbd_flags ^= CAPS;
		kbd_led_set();    /* update keyboard LED */
	}
}


/******************************************************************************/

static void kbd_num(unsigned char scan_code)
{
	if ((kbd_flags & BRK) == NORMAL) {
		kbd_flags ^= NUM;
		kbd_state = (kbd_flags & NUM) ? AS : NM;
		kbd_led_set();    /* update keyboard LED */
	}
}


/******************************************************************************/

static void kbd_scroll(unsigned char scan_code)
{
	if ((kbd_flags & BRK) == NORMAL) {
		kbd_flags ^= STP;
		kbd_led_set();    /* update keyboard LED */
		if (kbd_flags & STP)
			kbd_input = 0x13;
		else
			kbd_input = 0x11;
	}
}

/******************************************************************************/

static void kbd_alt(unsigned char scan_code)
{
	if ((kbd_flags & BRK) == BRK) {
		kbd_state = AS;
		kbd_flags &= (~ALT);
	} else {
		kbd_state = AK;
		kbd_flags &= ALT;
	}
}


/******************************************************************************/

static void kbd_led_set(void)
{
	kbd_input_empty();
	out8(I8042_DATA_REG, 0xed);    /* SET LED command */
	kbd_input_empty();
	out8(I8042_DATA_REG, (kbd_flags & 0x7));    /* LED bits only */
}


/******************************************************************************/

static int kbd_input_empty(void)
{
	int kbdTimeout = KBD_TIMEOUT * 1000;

	while ((in8(I8042_STATUS_REG) & I8042_STATUS_IN_DATA) && kbdTimeout--)
		udelay(1);

	return kbdTimeout != -1;
}

/******************************************************************************/

static int wait_until_kbd_output_full(void)
{
	int kbdTimeout = KBD_TIMEOUT * 1000;

	while (((in8(I8042_STATUS_REG) & 0x01) == 0) && kbdTimeout--)
		udelay(1);

	return kbdTimeout != -1;
}

/******************************************************************************/

static int kbd_reset(void)
{
	/* KB Reset */
	if (kbd_input_empty() == 0)
		return -1;

	out8(I8042_DATA_REG, 0xff);

	if (wait_until_kbd_output_full() == 0)
		return -1;

	if (in8(I8042_DATA_REG) != 0xfa) /* ACK */
		return -1;

	if (wait_until_kbd_output_full() == 0)
		return -1;

	if (in8(I8042_DATA_REG) != 0xaa) /* Test Pass*/
		return -1;

	if (kbd_input_empty() == 0)
		return -1;

	/* Set KBC mode */
	out8(I8042_COMMAND_REG, 0x60);

	if (kbd_input_empty() == 0)
		return -1;

	out8(I8042_DATA_REG, 0x45);

	if (kbd_input_empty() == 0)
		return -1;

	/* Enable Keyboard */
	out8(I8042_COMMAND_REG, 0xae);
	if (kbd_input_empty() == 0)
		return -1;

	out8(I8042_COMMAND_REG, 0x60);
	if (kbd_input_empty() == 0)
		return -1;

	out8(I8042_DATA_REG, 0xf4);
	if (kbd_input_empty() == 0)
		return -1;

	return 0;
}