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