1 /***********************************************************************
4 * DENX Software Engineering
5 * Wolfgang Denk, wd@denx.de
9 ***********************************************************************/
13 #include <stdio_dev.h>
19 #define PRINTF(fmt,args...) printf (fmt ,##args)
21 #define PRINTF(fmt,args...)
27 #define LED_SCR 0x01 /* scroll lock led */
28 #define LED_CAP 0x04 /* caps lock led */
29 #define LED_NUM 0x02 /* num lock led */
31 #define KBD_BUFFER_LEN 0x20 /* size of the keyboardbuffer */
33 #if defined(CONFIG_MPC5xxx) || defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
34 int ps2ser_check(void);
37 static volatile char kbd_buffer[KBD_BUFFER_LEN];
38 static volatile int in_pointer = 0;
39 static volatile int out_pointer = 0;
41 static unsigned char leds = 0;
42 static unsigned char num_lock = 0;
43 static unsigned char caps_lock = 0;
44 static unsigned char scroll_lock = 0;
45 static unsigned char shift = 0;
46 static unsigned char ctrl = 0;
47 static unsigned char alt = 0;
48 static unsigned char e0 = 0;
50 /******************************************************************
52 ******************************************************************/
54 /* puts character in the queue and sets up the in and out pointer */
55 static void kbd_put_queue(char data)
57 if((in_pointer+1)==KBD_BUFFER_LEN) {
59 return; /* buffer full */
64 if((in_pointer+1)==out_pointer)
65 return; /* buffer full */
68 kbd_buffer[in_pointer]=data;
72 /* test if a character is in the queue */
73 static int kbd_testc(void)
75 #if defined(CONFIG_MPC5xxx) || defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
76 /* no ISR is used, so received chars must be polled */
79 if(in_pointer==out_pointer)
80 return(0); /* no data */
85 /* gets the character from the queue */
86 static int kbd_getc(void)
89 while(in_pointer==out_pointer) {
90 #if defined(CONFIG_MPC5xxx) || defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
91 /* no ISR is used, so received chars must be polled */
95 if((out_pointer+1)==KBD_BUFFER_LEN)
99 c=kbd_buffer[out_pointer];
104 /* Simple translation table for the keys */
106 static unsigned char kbd_plain_xlate[] = {
107 0xff,0x1b, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=','\b','\t', /* 0x00 - 0x0f */
108 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']','\r',0xff, 'a', 's', /* 0x10 - 0x1f */
109 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';','\'', '`',0xff,'\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */
110 'b', 'n', 'm', ',', '.', '/',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
111 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
112 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
116 static unsigned char kbd_shift_xlate[] = {
117 0xff,0x1b, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+','\b','\t', /* 0x00 - 0x0f */
118 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}','\r',0xff, 'A', 'S', /* 0x10 - 0x1f */
119 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',0xff, '|', 'Z', 'X', 'C', 'V', /* 0x20 - 0x2f */
120 'B', 'N', 'M', '<', '>', '?',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
121 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
122 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
126 static unsigned char kbd_ctrl_xlate[] = {
127 0xff,0x1b, '1',0x00, '3', '4', '5',0x1E, '7', '8', '9', '0',0x1F, '=','\b','\t', /* 0x00 - 0x0f */
128 0x11,0x17,0x05,0x12,0x14,0x18,0x15,0x09,0x0f,0x10,0x1b,0x1d,'\n',0xff,0x01,0x13, /* 0x10 - 0x1f */
129 0x04,0x06,0x08,0x09,0x0a,0x0b,0x0c, ';','\'', '~',0x00,0x1c,0x1a,0x18,0x03,0x16, /* 0x20 - 0x2f */
130 0x02,0x0e,0x0d, '<', '>', '?',0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
131 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
132 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
137 void handle_scancode(unsigned char scancode)
139 unsigned char keycode;
141 /* Convert scancode to keycode */
142 PRINTF("scancode %x\n",scancode);
144 e0=1; /* special charakters */
148 e0=0; /* delete flag */
149 if(!( ((scancode&0x7F)==0x38)|| /* the right ctrl key */
150 ((scancode&0x7F)==0x1D)|| /* the right alt key */
151 ((scancode&0x7F)==0x35)|| /* the right '/' key */
152 ((scancode&0x7F)==0x1C) )) /* the right enter key */
153 /* we swallow unknown e0 codes */
156 /* special cntrl keys */
159 case 0x36: /* shift pressed */
161 return; /* do nothing else */
163 case 0xB6: /* shift released */
165 return; /* do nothing else */
166 case 0x38: /* alt pressed */
168 return; /* do nothing else */
169 case 0xB8: /* alt released */
171 return; /* do nothing else */
172 case 0x1d: /* ctrl pressed */
174 return; /* do nothing else */
175 case 0x9d: /* ctrl released */
177 return; /* do nothing else */
178 case 0x46: /* scrollock pressed */
179 scroll_lock=~scroll_lock;
181 leds&=~LED_SCR; /* switch LED off */
183 leds|=LED_SCR; /* switch on LED */
185 return; /* do nothing else */
186 case 0x3A: /* capslock pressed */
187 caps_lock=~caps_lock;
189 leds&=~LED_CAP; /* switch caps_lock off */
191 leds|=LED_CAP; /* switch on LED */
194 case 0x45: /* numlock pressed */
197 leds&=~LED_NUM; /* switch LED off */
199 leds|=LED_NUM; /* switch on LED */
202 case 0xC6: /* scroll lock released */
203 case 0xC5: /* num lock released */
204 case 0xBA: /* caps lock released */
205 return; /* just swallow */
208 if((scancode&0x80)==0x80) /* key released */
211 if((scancode&0x80)==0x00) /* key pressed */
215 /* now, decide which table we need */
216 if(scancode > (sizeof(kbd_plain_xlate)/sizeof(kbd_plain_xlate[0]))) { /* scancode not in list */
217 PRINTF("unkown scancode %X\n",scancode);
218 return; /* swallow it */
220 /* setup plain code first */
221 keycode=kbd_plain_xlate[scancode];
222 if(caps_lock==1) { /* caps_lock is pressed, overwrite plain code */
223 if(scancode > (sizeof(kbd_shift_xlate)/sizeof(kbd_shift_xlate[0]))) { /* scancode not in list */
224 PRINTF("unkown caps-locked scancode %X\n",scancode);
225 return; /* swallow it */
227 keycode=kbd_shift_xlate[scancode];
228 if(keycode<'A') { /* we only want the alphas capital */
229 keycode=kbd_plain_xlate[scancode];
232 if(shift==1) { /* shift overwrites caps_lock */
233 if(scancode > (sizeof(kbd_shift_xlate)/sizeof(kbd_shift_xlate[0]))) { /* scancode not in list */
234 PRINTF("unkown shifted scancode %X\n",scancode);
235 return; /* swallow it */
237 keycode=kbd_shift_xlate[scancode];
239 if(ctrl==1) { /* ctrl overwrites caps_lock and shift */
240 if(scancode > (sizeof(kbd_ctrl_xlate)/sizeof(kbd_ctrl_xlate[0]))) { /* scancode not in list */
241 PRINTF("unkown ctrl scancode %X\n",scancode);
242 return; /* swallow it */
244 keycode=kbd_ctrl_xlate[scancode];
246 /* check if valid keycode */
248 PRINTF("unkown scancode %X\n",scancode);
249 return; /* swallow unknown codes */
252 kbd_put_queue(keycode);
253 PRINTF("%x\n",keycode);
256 /******************************************************************
258 ******************************************************************/
260 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
261 extern int overwrite_console (void);
262 #define OVERWRITE_CONSOLE overwrite_console ()
264 #define OVERWRITE_CONSOLE 0
265 #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
270 struct stdio_dev kbddev ;
271 char *stdinname = getenv ("stdin");
273 if(kbd_init_hw()==-1)
275 memset (&kbddev, 0, sizeof(kbddev));
276 strcpy(kbddev.name, DEVNAME);
277 kbddev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
278 kbddev.getc = kbd_getc ;
279 kbddev.tstc = kbd_testc ;
281 error = stdio_register (&kbddev);
283 /* check if this is the standard input device */
284 if(strcmp(stdinname,DEVNAME)==0) {
285 /* reassign the console */
286 if(OVERWRITE_CONSOLE) {
289 error=console_assign(stdin,DEVNAME);