1
2
3
4
5
6
7
8
9
10
11#include <common.h>
12
13#include <stdio_dev.h>
14#include <keyboard.h>
15
16#undef KBG_DEBUG
17
18#ifdef KBG_DEBUG
19#define PRINTF(fmt,args...) printf (fmt ,##args)
20#else
21#define PRINTF(fmt,args...)
22#endif
23
24
25#define DEVNAME "kbd"
26
27#define LED_SCR 0x01
28#define LED_CAP 0x04
29#define LED_NUM 0x02
30
31#define KBD_BUFFER_LEN 0x20
32
33#if defined(CONFIG_MPC5xxx) || defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
34int ps2ser_check(void);
35#endif
36
37static volatile char kbd_buffer[KBD_BUFFER_LEN];
38static volatile int in_pointer = 0;
39static volatile int out_pointer = 0;
40
41static unsigned char leds = 0;
42static unsigned char num_lock = 0;
43static unsigned char caps_lock = 0;
44static unsigned char scroll_lock = 0;
45static unsigned char shift = 0;
46static unsigned char ctrl = 0;
47static unsigned char alt = 0;
48static unsigned char e0 = 0;
49
50
51
52
53
54
55static void kbd_put_queue(char data)
56{
57 if((in_pointer+1)==KBD_BUFFER_LEN) {
58 if(out_pointer==0) {
59 return;
60 } else{
61 in_pointer=0;
62 }
63 } else {
64 if((in_pointer+1)==out_pointer)
65 return;
66 in_pointer++;
67 }
68 kbd_buffer[in_pointer]=data;
69 return;
70}
71
72
73static int kbd_testc(void)
74{
75#if defined(CONFIG_MPC5xxx) || defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
76
77 ps2ser_check();
78#endif
79 if(in_pointer==out_pointer)
80 return(0);
81 else
82 return(1);
83}
84
85
86static int kbd_getc(void)
87{
88 char c;
89 while(in_pointer==out_pointer) {
90#if defined(CONFIG_MPC5xxx) || defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
91
92 ps2ser_check();
93#endif
94 ;}
95 if((out_pointer+1)==KBD_BUFFER_LEN)
96 out_pointer=0;
97 else
98 out_pointer++;
99 c=kbd_buffer[out_pointer];
100 return (int)c;
101
102}
103
104
105
106static unsigned char kbd_plain_xlate[] = {
107 0xff,0x1b, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=','\b','\t',
108 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']','\r',0xff, 'a', 's',
109 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';','\'', '`',0xff,'\\', 'z', 'x', 'c', 'v',
110 'b', 'n', 'm', ',', '.', '/',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff,
111 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1',
112 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
113 '\r',0xff,0xff
114 };
115
116static unsigned char kbd_shift_xlate[] = {
117 0xff,0x1b, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+','\b','\t',
118 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}','\r',0xff, 'A', 'S',
119 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',0xff, '|', 'Z', 'X', 'C', 'V',
120 'B', 'N', 'M', '<', '>', '?',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff,
121 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1',
122 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
123 '\r',0xff,0xff
124 };
125
126static unsigned char kbd_ctrl_xlate[] = {
127 0xff,0x1b, '1',0x00, '3', '4', '5',0x1E, '7', '8', '9', '0',0x1F, '=','\b','\t',
128 0x11,0x17,0x05,0x12,0x14,0x18,0x15,0x09,0x0f,0x10,0x1b,0x1d,'\n',0xff,0x01,0x13,
129 0x04,0x06,0x08,0x09,0x0a,0x0b,0x0c, ';','\'', '~',0x00,0x1c,0x1a,0x18,0x03,0x16,
130 0x02,0x0e,0x0d, '<', '>', '?',0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff,0xff,
131 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1',
132 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
133 '\r',0xff,0xff
134 };
135
136
137void handle_scancode(unsigned char scancode)
138{
139 unsigned char keycode;
140
141
142 PRINTF("scancode %x\n",scancode);
143 if(scancode==0xe0) {
144 e0=1;
145 return;
146 }
147 if(e0==1) {
148 e0=0;
149 if(!( ((scancode&0x7F)==0x38)||
150 ((scancode&0x7F)==0x1D)||
151 ((scancode&0x7F)==0x35)||
152 ((scancode&0x7F)==0x1C) ))
153
154 return;
155 }
156
157 switch(scancode) {
158 case 0x2A:
159 case 0x36:
160 shift=1;
161 return;
162 case 0xAA:
163 case 0xB6:
164 shift=0;
165 return;
166 case 0x38:
167 alt=1;
168 return;
169 case 0xB8:
170 alt=0;
171 return;
172 case 0x1d:
173 ctrl=1;
174 return;
175 case 0x9d:
176 ctrl=0;
177 return;
178 case 0x46:
179 scroll_lock=~scroll_lock;
180 if(scroll_lock==0)
181 leds&=~LED_SCR;
182 else
183 leds|=LED_SCR;
184 pckbd_leds(leds);
185 return;
186 case 0x3A:
187 caps_lock=~caps_lock;
188 if(caps_lock==0)
189 leds&=~LED_CAP;
190 else
191 leds|=LED_CAP;
192 pckbd_leds(leds);
193 return;
194 case 0x45:
195 num_lock=~num_lock;
196 if(num_lock==0)
197 leds&=~LED_NUM;
198 else
199 leds|=LED_NUM;
200 pckbd_leds(leds);
201 return;
202 case 0xC6:
203 case 0xC5:
204 case 0xBA:
205 return;
206 }
207#if 1
208 if((scancode&0x80)==0x80)
209 return;
210#else
211 if((scancode&0x80)==0x00)
212 return;
213 scancode &= ~0x80;
214#endif
215
216 if(scancode > (sizeof(kbd_plain_xlate)/sizeof(kbd_plain_xlate[0]))) {
217 PRINTF("unkown scancode %X\n",scancode);
218 return;
219 }
220
221 keycode=kbd_plain_xlate[scancode];
222 if(caps_lock==1) {
223 if(scancode > (sizeof(kbd_shift_xlate)/sizeof(kbd_shift_xlate[0]))) {
224 PRINTF("unkown caps-locked scancode %X\n",scancode);
225 return;
226 }
227 keycode=kbd_shift_xlate[scancode];
228 if(keycode<'A') {
229 keycode=kbd_plain_xlate[scancode];
230 }
231 }
232 if(shift==1) {
233 if(scancode > (sizeof(kbd_shift_xlate)/sizeof(kbd_shift_xlate[0]))) {
234 PRINTF("unkown shifted scancode %X\n",scancode);
235 return;
236 }
237 keycode=kbd_shift_xlate[scancode];
238 }
239 if(ctrl==1) {
240 if(scancode > (sizeof(kbd_ctrl_xlate)/sizeof(kbd_ctrl_xlate[0]))) {
241 PRINTF("unkown ctrl scancode %X\n",scancode);
242 return;
243 }
244 keycode=kbd_ctrl_xlate[scancode];
245 }
246
247 if(keycode==0xff) {
248 PRINTF("unkown scancode %X\n",scancode);
249 return;
250 }
251
252 kbd_put_queue(keycode);
253 PRINTF("%x\n",keycode);
254}
255
256
257
258
259
260#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
261extern int overwrite_console (void);
262#define OVERWRITE_CONSOLE overwrite_console ()
263#else
264#define OVERWRITE_CONSOLE 0
265#endif
266
267int kbd_init (void)
268{
269 int error;
270 struct stdio_dev kbddev ;
271 char *stdinname = getenv ("stdin");
272
273 if(kbd_init_hw()==-1)
274 return -1;
275 memset (&kbddev, 0, sizeof(kbddev));
276 strcpy(kbddev.name, DEVNAME);
277 kbddev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
278 kbddev.putc = NULL ;
279 kbddev.puts = NULL ;
280 kbddev.getc = kbd_getc ;
281 kbddev.tstc = kbd_testc ;
282
283 error = stdio_register (&kbddev);
284 if(error==0) {
285
286 if(strcmp(stdinname,DEVNAME)==0) {
287
288 if(OVERWRITE_CONSOLE) {
289 return 1;
290 }
291 error=console_assign(stdin,DEVNAME);
292 if(error==0)
293 return 1;
294 else
295 return error;
296 }
297 return 1;
298 }
299 return error;
300}
301