wdenk | 1c43771 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 1 | /*********************************************************************** |
| 2 | * |
| 3 | * (C) Copyright 2004 |
| 4 | * DENX Software Engineering |
| 5 | * Wolfgang Denk, wd@denx.de |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Keyboard driver |
| 9 | * |
| 10 | ***********************************************************************/ |
| 11 | |
| 12 | #include <common.h> |
| 13 | |
| 14 | #ifdef CONFIG_PS2KBD |
| 15 | |
| 16 | #include <devices.h> |
| 17 | #include <keyboard.h> |
| 18 | |
| 19 | #undef KBG_DEBUG |
| 20 | |
| 21 | #ifdef KBG_DEBUG |
| 22 | #define PRINTF(fmt,args...) printf (fmt ,##args) |
| 23 | #else |
| 24 | #define PRINTF(fmt,args...) |
| 25 | #endif |
| 26 | |
| 27 | |
| 28 | #define DEVNAME "kbd" |
| 29 | |
| 30 | #define LED_SCR 0x01 /* scroll lock led */ |
| 31 | #define LED_CAP 0x04 /* caps lock led */ |
| 32 | #define LED_NUM 0x02 /* num lock led */ |
| 33 | |
| 34 | #define KBD_BUFFER_LEN 0x20 /* size of the keyboardbuffer */ |
| 35 | |
wdenk | 7e6bf35 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 36 | #ifdef CONFIG_MPC5xxx |
| 37 | int ps2ser_check(void); |
| 38 | #endif |
| 39 | |
wdenk | 1c43771 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 40 | static volatile char kbd_buffer[KBD_BUFFER_LEN]; |
| 41 | static volatile int in_pointer = 0; |
| 42 | static volatile int out_pointer = 0; |
| 43 | |
| 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; |
| 52 | |
| 53 | /****************************************************************** |
| 54 | * Queue handling |
| 55 | ******************************************************************/ |
| 56 | |
| 57 | /* puts character in the queue and sets up the in and out pointer */ |
| 58 | static void kbd_put_queue(char data) |
| 59 | { |
| 60 | if((in_pointer+1)==KBD_BUFFER_LEN) { |
| 61 | if(out_pointer==0) { |
| 62 | return; /* buffer full */ |
| 63 | } else{ |
| 64 | in_pointer=0; |
| 65 | } |
| 66 | } else { |
| 67 | if((in_pointer+1)==out_pointer) |
| 68 | return; /* buffer full */ |
| 69 | in_pointer++; |
| 70 | } |
| 71 | kbd_buffer[in_pointer]=data; |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | /* test if a character is in the queue */ |
| 76 | static int kbd_testc(void) |
| 77 | { |
wdenk | 7e6bf35 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 78 | #ifdef CONFIG_MPC5xxx |
| 79 | /* no ISR is used, so received chars must be polled */ |
| 80 | ps2ser_check(); |
| 81 | #endif |
wdenk | 1c43771 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 82 | if(in_pointer==out_pointer) |
| 83 | return(0); /* no data */ |
| 84 | else |
| 85 | return(1); |
| 86 | } |
| 87 | |
| 88 | /* gets the character from the queue */ |
| 89 | static int kbd_getc(void) |
| 90 | { |
| 91 | char c; |
wdenk | 7e6bf35 | 2004-12-12 22:06:17 +0000 | [diff] [blame] | 92 | while(in_pointer==out_pointer) { |
| 93 | #ifdef CONFIG_MPC5xxx |
| 94 | /* no ISR is used, so received chars must be polled */ |
| 95 | ps2ser_check(); |
| 96 | #endif |
| 97 | ;} |
wdenk | 1c43771 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 98 | if((out_pointer+1)==KBD_BUFFER_LEN) |
| 99 | out_pointer=0; |
| 100 | else |
| 101 | out_pointer++; |
| 102 | c=kbd_buffer[out_pointer]; |
| 103 | return (int)c; |
| 104 | |
| 105 | } |
| 106 | |
| 107 | /* Simple translation table for the keys */ |
| 108 | |
| 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 */ |
| 116 | '\r',0xff,0xff |
| 117 | }; |
| 118 | |
| 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 */ |
| 126 | '\r',0xff,0xff |
| 127 | }; |
| 128 | |
| 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 */ |
| 136 | '\r',0xff,0xff |
| 137 | }; |
| 138 | |
| 139 | |
| 140 | void handle_scancode(unsigned char scancode) |
| 141 | { |
| 142 | unsigned char keycode; |
| 143 | |
| 144 | /* Convert scancode to keycode */ |
| 145 | PRINTF("scancode %x\n",scancode); |
| 146 | if(scancode==0xe0) { |
| 147 | e0=1; /* special charakters */ |
| 148 | return; |
| 149 | } |
| 150 | if(e0==1) { |
| 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 */ |
| 157 | return; |
| 158 | } |
| 159 | /* special cntrl keys */ |
| 160 | switch(scancode) { |
| 161 | case 0x2A: |
| 162 | case 0x36: /* shift pressed */ |
| 163 | shift=1; |
| 164 | return; /* do nothing else */ |
| 165 | case 0xAA: |
| 166 | case 0xB6: /* shift released */ |
| 167 | shift=0; |
| 168 | return; /* do nothing else */ |
| 169 | case 0x38: /* alt pressed */ |
| 170 | alt=1; |
| 171 | return; /* do nothing else */ |
| 172 | case 0xB8: /* alt released */ |
| 173 | alt=0; |
| 174 | return; /* do nothing else */ |
| 175 | case 0x1d: /* ctrl pressed */ |
| 176 | ctrl=1; |
| 177 | return; /* do nothing else */ |
| 178 | case 0x9d: /* ctrl released */ |
| 179 | ctrl=0; |
| 180 | return; /* do nothing else */ |
| 181 | case 0x46: /* scrollock pressed */ |
| 182 | scroll_lock=~scroll_lock; |
| 183 | if(scroll_lock==0) |
| 184 | leds&=~LED_SCR; /* switch LED off */ |
| 185 | else |
| 186 | leds|=LED_SCR; /* switch on LED */ |
| 187 | pckbd_leds(leds); |
| 188 | return; /* do nothing else */ |
| 189 | case 0x3A: /* capslock pressed */ |
| 190 | caps_lock=~caps_lock; |
| 191 | if(caps_lock==0) |
| 192 | leds&=~LED_CAP; /* switch caps_lock off */ |
| 193 | else |
| 194 | leds|=LED_CAP; /* switch on LED */ |
| 195 | pckbd_leds(leds); |
| 196 | return; |
| 197 | case 0x45: /* numlock pressed */ |
| 198 | num_lock=~num_lock; |
| 199 | if(num_lock==0) |
| 200 | leds&=~LED_NUM; /* switch LED off */ |
| 201 | else |
| 202 | leds|=LED_NUM; /* switch on LED */ |
| 203 | pckbd_leds(leds); |
| 204 | return; |
| 205 | case 0xC6: /* scroll lock released */ |
| 206 | case 0xC5: /* num lock released */ |
| 207 | case 0xBA: /* caps lock released */ |
| 208 | return; /* just swallow */ |
| 209 | } |
wdenk | ef97873 | 2004-01-21 20:46:28 +0000 | [diff] [blame] | 210 | #if 1 |
wdenk | 1c43771 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 211 | if((scancode&0x80)==0x80) /* key released */ |
| 212 | return; |
wdenk | c837dcb | 2004-01-20 23:12:12 +0000 | [diff] [blame] | 213 | #else |
| 214 | if((scancode&0x80)==0x00) /* key pressed */ |
| 215 | return; |
| 216 | scancode &= ~0x80; |
| 217 | #endif |
wdenk | 1c43771 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 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 */ |
| 222 | } |
| 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 */ |
| 229 | } |
| 230 | keycode=kbd_shift_xlate[scancode]; |
| 231 | if(keycode<'A') { /* we only want the alphas capital */ |
| 232 | keycode=kbd_plain_xlate[scancode]; |
| 233 | } |
| 234 | } |
| 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 */ |
| 239 | } |
| 240 | keycode=kbd_shift_xlate[scancode]; |
| 241 | } |
| 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 */ |
| 246 | } |
| 247 | keycode=kbd_ctrl_xlate[scancode]; |
| 248 | } |
| 249 | /* check if valid keycode */ |
| 250 | if(keycode==0xff) { |
| 251 | PRINTF("unkown scancode %X\n",scancode); |
| 252 | return; /* swallow unknown codes */ |
| 253 | } |
| 254 | |
| 255 | kbd_put_queue(keycode); |
| 256 | PRINTF("%x\n",keycode); |
| 257 | } |
| 258 | |
| 259 | /****************************************************************** |
| 260 | * Init |
| 261 | ******************************************************************/ |
| 262 | |
| 263 | #ifdef CFG_CONSOLE_OVERWRITE_ROUTINE |
| 264 | extern int overwrite_console (void); |
wdenk | 83e40ba | 2005-03-31 18:42:15 +0000 | [diff] [blame^] | 265 | #define OVERWRITE_CONSOLE overwrite_console () |
wdenk | 1c43771 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 266 | #else |
wdenk | 83e40ba | 2005-03-31 18:42:15 +0000 | [diff] [blame^] | 267 | #define OVERWRITE_CONSOLE 0 |
| 268 | #endif /* CFG_CONSOLE_OVERWRITE_ROUTINE */ |
wdenk | 1c43771 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 269 | |
| 270 | int kbd_init (void) |
| 271 | { |
| 272 | int error; |
| 273 | device_t kbddev ; |
| 274 | char *stdinname = getenv ("stdin"); |
| 275 | |
| 276 | if(kbd_init_hw()==-1) |
| 277 | return -1; |
| 278 | memset (&kbddev, 0, sizeof(kbddev)); |
| 279 | strcpy(kbddev.name, DEVNAME); |
| 280 | kbddev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM; |
| 281 | kbddev.putc = NULL ; |
| 282 | kbddev.puts = NULL ; |
| 283 | kbddev.getc = kbd_getc ; |
| 284 | kbddev.tstc = kbd_testc ; |
| 285 | |
| 286 | error = device_register (&kbddev); |
| 287 | if(error==0) { |
| 288 | /* check if this is the standard input device */ |
| 289 | if(strcmp(stdinname,DEVNAME)==0) { |
| 290 | /* reassign the console */ |
wdenk | 83e40ba | 2005-03-31 18:42:15 +0000 | [diff] [blame^] | 291 | if(OVERWRITE_CONSOLE) { |
wdenk | 1c43771 | 2004-01-16 00:30:56 +0000 | [diff] [blame] | 292 | return 1; |
| 293 | } |
| 294 | error=console_assign(stdin,DEVNAME); |
| 295 | if(error==0) |
| 296 | return 1; |
| 297 | else |
| 298 | return error; |
| 299 | } |
| 300 | return 1; |
| 301 | } |
| 302 | return error; |
| 303 | } |
| 304 | |
| 305 | #endif /* CONFIG_PS2KBD */ |