1 /***********************************************************************
4 * DENX Software Engineering
5 * Wolfgang Denk, wd@denx.de
10 ***********************************************************************/
22 #define PRINTF(fmt,args...) printf (fmt ,##args)
24 #define PRINTF(fmt,args...)
30 #define LED_SCR 0x01 /* scroll lock led */
31 #define LED_CAP 0x04 /* caps lock led */
32 #define LED_NUM 0x02 /* num lock led */
34 #define KBD_BUFFER_LEN 0x20 /* size of the keyboardbuffer */
36 #if defined(CONFIG_MPC5xxx) || defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
37 int ps2ser_check(void);
40 static volatile char kbd_buffer[KBD_BUFFER_LEN];
41 static volatile int in_pointer = 0;
42 static volatile int out_pointer = 0;
44 static unsigned char leds = 0;
45 static unsigned char num_lock = 0;
46 static unsigned char caps_lock = 0;
47 static unsigned char scroll_lock = 0;
48 static unsigned char shift = 0;
49 static unsigned char ctrl = 0;
50 static unsigned char alt = 0;
51 static unsigned char e0 = 0;
53 /******************************************************************
55 ******************************************************************/
57 /* puts character in the queue and sets up the in and out pointer */
58 static void kbd_put_queue(char data)
60 if((in_pointer+1)==KBD_BUFFER_LEN) {
62 return; /* buffer full */
67 if((in_pointer+1)==out_pointer)
68 return; /* buffer full */
71 kbd_buffer[in_pointer]=data;
75 /* test if a character is in the queue */
76 static int kbd_testc(void)
78 #if defined(CONFIG_MPC5xxx) || defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
79 /* no ISR is used, so received chars must be polled */
82 if(in_pointer==out_pointer)
83 return(0); /* no data */
88 /* gets the character from the queue */
89 static int kbd_getc(void)
92 while(in_pointer==out_pointer) {
93 #if defined(CONFIG_MPC5xxx) || defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
94 /* no ISR is used, so received chars must be polled */
98 if((out_pointer+1)==KBD_BUFFER_LEN)
102 c=kbd_buffer[out_pointer];
107 /* Simple translation table for the keys */
109 static unsigned char kbd_plain_xlate[] = {
110 0xff,0x1b, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=','\b','\t', /* 0x00 - 0x0f */
111 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']','\r',0xff, 'a', 's', /* 0x10 - 0x1f */
112 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';','\'', '`',0xff,'\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */
113 'b', 'n', 'm', ',', '.', '/',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
114 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
115 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
119 static unsigned char kbd_shift_xlate[] = {
120 0xff,0x1b, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+','\b','\t', /* 0x00 - 0x0f */
121 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}','\r',0xff, 'A', 'S', /* 0x10 - 0x1f */
122 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',0xff, '|', 'Z', 'X', 'C', 'V', /* 0x20 - 0x2f */
123 'B', 'N', 'M', '<', '>', '?',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
124 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
125 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
129 static unsigned char kbd_ctrl_xlate[] = {
130 0xff,0x1b, '1',0x00, '3', '4', '5',0x1E, '7', '8', '9', '0',0x1F, '=','\b','\t', /* 0x00 - 0x0f */
131 0x11,0x17,0x05,0x12,0x14,0x18,0x15,0x09,0x0f,0x10,0x1b,0x1d,'\n',0xff,0x01,0x13, /* 0x10 - 0x1f */
132 0x04,0x06,0x08,0x09,0x0a,0x0b,0x0c, ';','\'', '~',0x00,0x1c,0x1a,0x18,0x03,0x16, /* 0x20 - 0x2f */
133 0x02,0x0e,0x0d, '<', '>', '?',0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
134 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
135 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
140 void handle_scancode(unsigned char scancode)
142 unsigned char keycode;
144 /* Convert scancode to keycode */
145 PRINTF("scancode %x\n",scancode);
147 e0=1; /* special charakters */
151 e0=0; /* delete flag */
152 if(!( ((scancode&0x7F)==0x38)|| /* the right ctrl key */
153 ((scancode&0x7F)==0x1D)|| /* the right alt key */
154 ((scancode&0x7F)==0x35)|| /* the right '/' key */
155 ((scancode&0x7F)==0x1C) )) /* the right enter key */
156 /* we swallow unknown e0 codes */
159 /* special cntrl keys */
162 case 0x36: /* shift pressed */
164 return; /* do nothing else */
166 case 0xB6: /* shift released */
168 return; /* do nothing else */
169 case 0x38: /* alt pressed */
171 return; /* do nothing else */
172 case 0xB8: /* alt released */
174 return; /* do nothing else */
175 case 0x1d: /* ctrl pressed */
177 return; /* do nothing else */
178 case 0x9d: /* ctrl released */
180 return; /* do nothing else */
181 case 0x46: /* scrollock pressed */
182 scroll_lock=~scroll_lock;
184 leds&=~LED_SCR; /* switch LED off */
186 leds|=LED_SCR; /* switch on LED */
188 return; /* do nothing else */
189 case 0x3A: /* capslock pressed */
190 caps_lock=~caps_lock;
192 leds&=~LED_CAP; /* switch caps_lock off */
194 leds|=LED_CAP; /* switch on LED */
197 case 0x45: /* numlock pressed */
200 leds&=~LED_NUM; /* switch LED off */
202 leds|=LED_NUM; /* switch on LED */
205 case 0xC6: /* scroll lock released */
206 case 0xC5: /* num lock released */
207 case 0xBA: /* caps lock released */
208 return; /* just swallow */
211 if((scancode&0x80)==0x80) /* key released */
214 if((scancode&0x80)==0x00) /* key pressed */
218 /* now, decide which table we need */
219 if(scancode > (sizeof(kbd_plain_xlate)/sizeof(kbd_plain_xlate[0]))) { /* scancode not in list */
220 PRINTF("unkown scancode %X\n",scancode);
221 return; /* swallow it */
223 /* setup plain code first */
224 keycode=kbd_plain_xlate[scancode];
225 if(caps_lock==1) { /* caps_lock is pressed, overwrite plain code */
226 if(scancode > (sizeof(kbd_shift_xlate)/sizeof(kbd_shift_xlate[0]))) { /* scancode not in list */
227 PRINTF("unkown caps-locked scancode %X\n",scancode);
228 return; /* swallow it */
230 keycode=kbd_shift_xlate[scancode];
231 if(keycode<'A') { /* we only want the alphas capital */
232 keycode=kbd_plain_xlate[scancode];
235 if(shift==1) { /* shift overwrites caps_lock */
236 if(scancode > (sizeof(kbd_shift_xlate)/sizeof(kbd_shift_xlate[0]))) { /* scancode not in list */
237 PRINTF("unkown shifted scancode %X\n",scancode);
238 return; /* swallow it */
240 keycode=kbd_shift_xlate[scancode];
242 if(ctrl==1) { /* ctrl overwrites caps_lock and shift */
243 if(scancode > (sizeof(kbd_ctrl_xlate)/sizeof(kbd_ctrl_xlate[0]))) { /* scancode not in list */
244 PRINTF("unkown ctrl scancode %X\n",scancode);
245 return; /* swallow it */
247 keycode=kbd_ctrl_xlate[scancode];
249 /* check if valid keycode */
251 PRINTF("unkown scancode %X\n",scancode);
252 return; /* swallow unknown codes */
255 kbd_put_queue(keycode);
256 PRINTF("%x\n",keycode);
259 /******************************************************************
261 ******************************************************************/
263 #ifdef CFG_CONSOLE_OVERWRITE_ROUTINE
264 extern int overwrite_console (void);
265 #define OVERWRITE_CONSOLE overwrite_console ()
267 #define OVERWRITE_CONSOLE 0
268 #endif /* CFG_CONSOLE_OVERWRITE_ROUTINE */
274 char *stdinname = getenv ("stdin");
276 if(kbd_init_hw()==-1)
278 memset (&kbddev, 0, sizeof(kbddev));
279 strcpy(kbddev.name, DEVNAME);
280 kbddev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
283 kbddev.getc = kbd_getc ;
284 kbddev.tstc = kbd_testc ;
286 error = device_register (&kbddev);
288 /* check if this is the standard input device */
289 if(strcmp(stdinname,DEVNAME)==0) {
290 /* reassign the console */
291 if(OVERWRITE_CONSOLE) {
294 error=console_assign(stdin,DEVNAME);
305 #endif /* CONFIG_PS2KBD */