1 /***********************************************************************
4 * DENX Software Engineering
5 * Wolfgang Denk, wd@denx.de
10 ***********************************************************************/
14 #include <stdio_dev.h>
20 #define PRINTF(fmt,args...) printf (fmt ,##args)
22 #define PRINTF(fmt,args...)
28 #define LED_SCR 0x01 /* scroll lock led */
29 #define LED_CAP 0x04 /* caps lock led */
30 #define LED_NUM 0x02 /* num lock led */
32 #define KBD_BUFFER_LEN 0x20 /* size of the keyboardbuffer */
34 #if defined(CONFIG_MPC5xxx) || defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
35 int ps2ser_check(void);
38 static volatile char kbd_buffer[KBD_BUFFER_LEN];
39 static volatile int in_pointer = 0;
40 static volatile int out_pointer = 0;
42 static unsigned char leds = 0;
43 static unsigned char num_lock = 0;
44 static unsigned char caps_lock = 0;
45 static unsigned char scroll_lock = 0;
46 static unsigned char shift = 0;
47 static unsigned char ctrl = 0;
48 static unsigned char alt = 0;
49 static unsigned char e0 = 0;
51 /******************************************************************
53 ******************************************************************/
55 /* puts character in the queue and sets up the in and out pointer */
56 static void kbd_put_queue(char data)
58 if((in_pointer+1)==KBD_BUFFER_LEN) {
60 return; /* buffer full */
65 if((in_pointer+1)==out_pointer)
66 return; /* buffer full */
69 kbd_buffer[in_pointer]=data;
73 /* test if a character is in the queue */
74 static int kbd_testc(void)
76 #if defined(CONFIG_MPC5xxx) || defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
77 /* no ISR is used, so received chars must be polled */
80 if(in_pointer==out_pointer)
81 return(0); /* no data */
86 /* gets the character from the queue */
87 static int kbd_getc(void)
90 while(in_pointer==out_pointer) {
91 #if defined(CONFIG_MPC5xxx) || defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
92 /* no ISR is used, so received chars must be polled */
96 if((out_pointer+1)==KBD_BUFFER_LEN)
100 c=kbd_buffer[out_pointer];
105 /* Simple translation table for the keys */
107 static unsigned char kbd_plain_xlate[] = {
108 0xff,0x1b, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=','\b','\t', /* 0x00 - 0x0f */
109 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']','\r',0xff, 'a', 's', /* 0x10 - 0x1f */
110 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';','\'', '`',0xff,'\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */
111 'b', 'n', 'm', ',', '.', '/',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
112 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
113 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
117 static unsigned char kbd_shift_xlate[] = {
118 0xff,0x1b, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+','\b','\t', /* 0x00 - 0x0f */
119 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}','\r',0xff, 'A', 'S', /* 0x10 - 0x1f */
120 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',0xff, '|', 'Z', 'X', 'C', 'V', /* 0x20 - 0x2f */
121 'B', 'N', 'M', '<', '>', '?',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
122 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
123 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
127 static unsigned char kbd_ctrl_xlate[] = {
128 0xff,0x1b, '1',0x00, '3', '4', '5',0x1E, '7', '8', '9', '0',0x1F, '=','\b','\t', /* 0x00 - 0x0f */
129 0x11,0x17,0x05,0x12,0x14,0x18,0x15,0x09,0x0f,0x10,0x1b,0x1d,'\n',0xff,0x01,0x13, /* 0x10 - 0x1f */
130 0x04,0x06,0x08,0x09,0x0a,0x0b,0x0c, ';','\'', '~',0x00,0x1c,0x1a,0x18,0x03,0x16, /* 0x20 - 0x2f */
131 0x02,0x0e,0x0d, '<', '>', '?',0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
132 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
133 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
138 void handle_scancode(unsigned char scancode)
140 unsigned char keycode;
142 /* Convert scancode to keycode */
143 PRINTF("scancode %x\n",scancode);
145 e0=1; /* special charakters */
149 e0=0; /* delete flag */
150 if(!( ((scancode&0x7F)==0x38)|| /* the right ctrl key */
151 ((scancode&0x7F)==0x1D)|| /* the right alt key */
152 ((scancode&0x7F)==0x35)|| /* the right '/' key */
153 ((scancode&0x7F)==0x1C) )) /* the right enter key */
154 /* we swallow unknown e0 codes */
157 /* special cntrl keys */
160 case 0x36: /* shift pressed */
162 return; /* do nothing else */
164 case 0xB6: /* shift released */
166 return; /* do nothing else */
167 case 0x38: /* alt pressed */
169 return; /* do nothing else */
170 case 0xB8: /* alt released */
172 return; /* do nothing else */
173 case 0x1d: /* ctrl pressed */
175 return; /* do nothing else */
176 case 0x9d: /* ctrl released */
178 return; /* do nothing else */
179 case 0x46: /* scrollock pressed */
180 scroll_lock=~scroll_lock;
182 leds&=~LED_SCR; /* switch LED off */
184 leds|=LED_SCR; /* switch on LED */
186 return; /* do nothing else */
187 case 0x3A: /* capslock pressed */
188 caps_lock=~caps_lock;
190 leds&=~LED_CAP; /* switch caps_lock off */
192 leds|=LED_CAP; /* switch on LED */
195 case 0x45: /* numlock pressed */
198 leds&=~LED_NUM; /* switch LED off */
200 leds|=LED_NUM; /* switch on LED */
203 case 0xC6: /* scroll lock released */
204 case 0xC5: /* num lock released */
205 case 0xBA: /* caps lock released */
206 return; /* just swallow */
209 if((scancode&0x80)==0x80) /* key released */
212 if((scancode&0x80)==0x00) /* key pressed */
216 /* now, decide which table we need */
217 if(scancode > (sizeof(kbd_plain_xlate)/sizeof(kbd_plain_xlate[0]))) { /* scancode not in list */
218 PRINTF("unkown scancode %X\n",scancode);
219 return; /* swallow it */
221 /* setup plain code first */
222 keycode=kbd_plain_xlate[scancode];
223 if(caps_lock==1) { /* caps_lock is pressed, overwrite plain code */
224 if(scancode > (sizeof(kbd_shift_xlate)/sizeof(kbd_shift_xlate[0]))) { /* scancode not in list */
225 PRINTF("unkown caps-locked scancode %X\n",scancode);
226 return; /* swallow it */
228 keycode=kbd_shift_xlate[scancode];
229 if(keycode<'A') { /* we only want the alphas capital */
230 keycode=kbd_plain_xlate[scancode];
233 if(shift==1) { /* shift overwrites caps_lock */
234 if(scancode > (sizeof(kbd_shift_xlate)/sizeof(kbd_shift_xlate[0]))) { /* scancode not in list */
235 PRINTF("unkown shifted scancode %X\n",scancode);
236 return; /* swallow it */
238 keycode=kbd_shift_xlate[scancode];
240 if(ctrl==1) { /* ctrl overwrites caps_lock and shift */
241 if(scancode > (sizeof(kbd_ctrl_xlate)/sizeof(kbd_ctrl_xlate[0]))) { /* scancode not in list */
242 PRINTF("unkown ctrl scancode %X\n",scancode);
243 return; /* swallow it */
245 keycode=kbd_ctrl_xlate[scancode];
247 /* check if valid keycode */
249 PRINTF("unkown scancode %X\n",scancode);
250 return; /* swallow unknown codes */
253 kbd_put_queue(keycode);
254 PRINTF("%x\n",keycode);
257 /******************************************************************
259 ******************************************************************/
261 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
262 extern int overwrite_console (void);
263 #define OVERWRITE_CONSOLE overwrite_console ()
265 #define OVERWRITE_CONSOLE 0
266 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
271 struct stdio_dev kbddev ;
272 char *stdinname = getenv ("stdin");
274 if(kbd_init_hw()==-1)
276 memset (&kbddev, 0, sizeof(kbddev));
277 strcpy(kbddev.name, DEVNAME);
278 kbddev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
281 kbddev.getc = kbd_getc ;
282 kbddev.tstc = kbd_testc ;
284 error = stdio_register (&kbddev);
286 /* check if this is the standard input device */
287 if(strcmp(stdinname,DEVNAME)==0) {
288 /* reassign the console */
289 if(OVERWRITE_CONSOLE) {
292 error=console_assign(stdin,DEVNAME);