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 | |
| 36 | static volatile char kbd_buffer[KBD_BUFFER_LEN]; |
| 37 | static volatile int in_pointer = 0; |
| 38 | static volatile int out_pointer = 0; |
| 39 | |
| 40 | static unsigned char leds = 0; |
| 41 | static unsigned char num_lock = 0; |
| 42 | static unsigned char caps_lock = 0; |
| 43 | static unsigned char scroll_lock = 0; |
| 44 | static unsigned char shift = 0; |
| 45 | static unsigned char ctrl = 0; |
| 46 | static unsigned char alt = 0; |
| 47 | static unsigned char e0 = 0; |
| 48 | |
| 49 | /****************************************************************** |
| 50 | * Queue handling |
| 51 | ******************************************************************/ |
| 52 | |
| 53 | /* puts character in the queue and sets up the in and out pointer */ |
| 54 | static void kbd_put_queue(char data) |
| 55 | { |
| 56 | if((in_pointer+1)==KBD_BUFFER_LEN) { |
| 57 | if(out_pointer==0) { |
| 58 | return; /* buffer full */ |
| 59 | } else{ |
| 60 | in_pointer=0; |
| 61 | } |
| 62 | } else { |
| 63 | if((in_pointer+1)==out_pointer) |
| 64 | return; /* buffer full */ |
| 65 | in_pointer++; |
| 66 | } |
| 67 | kbd_buffer[in_pointer]=data; |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | /* test if a character is in the queue */ |
| 72 | static int kbd_testc(void) |
| 73 | { |
| 74 | if(in_pointer==out_pointer) |
| 75 | return(0); /* no data */ |
| 76 | else |
| 77 | return(1); |
| 78 | } |
| 79 | |
| 80 | /* gets the character from the queue */ |
| 81 | static int kbd_getc(void) |
| 82 | { |
| 83 | char c; |
| 84 | while(in_pointer==out_pointer); |
| 85 | if((out_pointer+1)==KBD_BUFFER_LEN) |
| 86 | out_pointer=0; |
| 87 | else |
| 88 | out_pointer++; |
| 89 | c=kbd_buffer[out_pointer]; |
| 90 | return (int)c; |
| 91 | |
| 92 | } |
| 93 | |
| 94 | /* Simple translation table for the keys */ |
| 95 | |
| 96 | static unsigned char kbd_plain_xlate[] = { |
| 97 | 0xff,0x1b, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=','\b','\t', /* 0x00 - 0x0f */ |
| 98 | 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']','\r',0xff, 'a', 's', /* 0x10 - 0x1f */ |
| 99 | 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';','\'', '`',0xff,'\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */ |
| 100 | 'b', 'n', 'm', ',', '.', '/',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */ |
| 101 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */ |
| 102 | '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */ |
| 103 | '\r',0xff,0xff |
| 104 | }; |
| 105 | |
| 106 | static unsigned char kbd_shift_xlate[] = { |
| 107 | 0xff,0x1b, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+','\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 */ |
| 113 | '\r',0xff,0xff |
| 114 | }; |
| 115 | |
| 116 | static unsigned char kbd_ctrl_xlate[] = { |
| 117 | 0xff,0x1b, '1',0x00, '3', '4', '5',0x1E, '7', '8', '9', '0',0x1F, '=','\b','\t', /* 0x00 - 0x0f */ |
| 118 | 0x11,0x17,0x05,0x12,0x14,0x18,0x15,0x09,0x0f,0x10,0x1b,0x1d,'\n',0xff,0x01,0x13, /* 0x10 - 0x1f */ |
| 119 | 0x04,0x06,0x08,0x09,0x0a,0x0b,0x0c, ';','\'', '~',0x00,0x1c,0x1a,0x18,0x03,0x16, /* 0x20 - 0x2f */ |
| 120 | 0x02,0x0e,0x0d, '<', '>', '?',0xff,0xff,0xff,0x00,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 */ |
| 123 | '\r',0xff,0xff |
| 124 | }; |
| 125 | |
| 126 | |
| 127 | void handle_scancode(unsigned char scancode) |
| 128 | { |
| 129 | unsigned char keycode; |
| 130 | |
| 131 | /* Convert scancode to keycode */ |
| 132 | PRINTF("scancode %x\n",scancode); |
| 133 | if(scancode==0xe0) { |
| 134 | e0=1; /* special charakters */ |
| 135 | return; |
| 136 | } |
| 137 | if(e0==1) { |
| 138 | e0=0; /* delete flag */ |
| 139 | if(!( ((scancode&0x7F)==0x38)|| /* the right ctrl key */ |
| 140 | ((scancode&0x7F)==0x1D)|| /* the right alt key */ |
| 141 | ((scancode&0x7F)==0x35)|| /* the right '/' key */ |
| 142 | ((scancode&0x7F)==0x1C) )) /* the right enter key */ |
| 143 | /* we swallow unknown e0 codes */ |
| 144 | return; |
| 145 | } |
| 146 | /* special cntrl keys */ |
| 147 | switch(scancode) { |
| 148 | case 0x2A: |
| 149 | case 0x36: /* shift pressed */ |
| 150 | shift=1; |
| 151 | return; /* do nothing else */ |
| 152 | case 0xAA: |
| 153 | case 0xB6: /* shift released */ |
| 154 | shift=0; |
| 155 | return; /* do nothing else */ |
| 156 | case 0x38: /* alt pressed */ |
| 157 | alt=1; |
| 158 | return; /* do nothing else */ |
| 159 | case 0xB8: /* alt released */ |
| 160 | alt=0; |
| 161 | return; /* do nothing else */ |
| 162 | case 0x1d: /* ctrl pressed */ |
| 163 | ctrl=1; |
| 164 | return; /* do nothing else */ |
| 165 | case 0x9d: /* ctrl released */ |
| 166 | ctrl=0; |
| 167 | return; /* do nothing else */ |
| 168 | case 0x46: /* scrollock pressed */ |
| 169 | scroll_lock=~scroll_lock; |
| 170 | if(scroll_lock==0) |
| 171 | leds&=~LED_SCR; /* switch LED off */ |
| 172 | else |
| 173 | leds|=LED_SCR; /* switch on LED */ |
| 174 | pckbd_leds(leds); |
| 175 | return; /* do nothing else */ |
| 176 | case 0x3A: /* capslock pressed */ |
| 177 | caps_lock=~caps_lock; |
| 178 | if(caps_lock==0) |
| 179 | leds&=~LED_CAP; /* switch caps_lock off */ |
| 180 | else |
| 181 | leds|=LED_CAP; /* switch on LED */ |
| 182 | pckbd_leds(leds); |
| 183 | return; |
| 184 | case 0x45: /* numlock pressed */ |
| 185 | num_lock=~num_lock; |
| 186 | if(num_lock==0) |
| 187 | leds&=~LED_NUM; /* switch LED off */ |
| 188 | else |
| 189 | leds|=LED_NUM; /* switch on LED */ |
| 190 | pckbd_leds(leds); |
| 191 | return; |
| 192 | case 0xC6: /* scroll lock released */ |
| 193 | case 0xC5: /* num lock released */ |
| 194 | case 0xBA: /* caps lock released */ |
| 195 | return; /* just swallow */ |
| 196 | } |
| 197 | if((scancode&0x80)==0x80) /* key released */ |
| 198 | return; |
| 199 | /* now, decide which table we need */ |
| 200 | if(scancode > (sizeof(kbd_plain_xlate)/sizeof(kbd_plain_xlate[0]))) { /* scancode not in list */ |
| 201 | PRINTF("unkown scancode %X\n",scancode); |
| 202 | return; /* swallow it */ |
| 203 | } |
| 204 | /* setup plain code first */ |
| 205 | keycode=kbd_plain_xlate[scancode]; |
| 206 | if(caps_lock==1) { /* caps_lock is pressed, overwrite plain code */ |
| 207 | if(scancode > (sizeof(kbd_shift_xlate)/sizeof(kbd_shift_xlate[0]))) { /* scancode not in list */ |
| 208 | PRINTF("unkown caps-locked scancode %X\n",scancode); |
| 209 | return; /* swallow it */ |
| 210 | } |
| 211 | keycode=kbd_shift_xlate[scancode]; |
| 212 | if(keycode<'A') { /* we only want the alphas capital */ |
| 213 | keycode=kbd_plain_xlate[scancode]; |
| 214 | } |
| 215 | } |
| 216 | if(shift==1) { /* shift overwrites caps_lock */ |
| 217 | if(scancode > (sizeof(kbd_shift_xlate)/sizeof(kbd_shift_xlate[0]))) { /* scancode not in list */ |
| 218 | PRINTF("unkown shifted scancode %X\n",scancode); |
| 219 | return; /* swallow it */ |
| 220 | } |
| 221 | keycode=kbd_shift_xlate[scancode]; |
| 222 | } |
| 223 | if(ctrl==1) { /* ctrl overwrites caps_lock and shift */ |
| 224 | if(scancode > (sizeof(kbd_ctrl_xlate)/sizeof(kbd_ctrl_xlate[0]))) { /* scancode not in list */ |
| 225 | PRINTF("unkown ctrl scancode %X\n",scancode); |
| 226 | return; /* swallow it */ |
| 227 | } |
| 228 | keycode=kbd_ctrl_xlate[scancode]; |
| 229 | } |
| 230 | /* check if valid keycode */ |
| 231 | if(keycode==0xff) { |
| 232 | PRINTF("unkown scancode %X\n",scancode); |
| 233 | return; /* swallow unknown codes */ |
| 234 | } |
| 235 | |
| 236 | kbd_put_queue(keycode); |
| 237 | PRINTF("%x\n",keycode); |
| 238 | } |
| 239 | |
| 240 | /****************************************************************** |
| 241 | * Init |
| 242 | ******************************************************************/ |
| 243 | |
| 244 | #ifdef CFG_CONSOLE_OVERWRITE_ROUTINE |
| 245 | extern int overwrite_console (void); |
| 246 | #else |
| 247 | int overwrite_console (void) |
| 248 | { |
| 249 | return (0); |
| 250 | } |
| 251 | #endif |
| 252 | |
| 253 | int kbd_init (void) |
| 254 | { |
| 255 | int error; |
| 256 | device_t kbddev ; |
| 257 | char *stdinname = getenv ("stdin"); |
| 258 | |
| 259 | if(kbd_init_hw()==-1) |
| 260 | return -1; |
| 261 | memset (&kbddev, 0, sizeof(kbddev)); |
| 262 | strcpy(kbddev.name, DEVNAME); |
| 263 | kbddev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM; |
| 264 | kbddev.putc = NULL ; |
| 265 | kbddev.puts = NULL ; |
| 266 | kbddev.getc = kbd_getc ; |
| 267 | kbddev.tstc = kbd_testc ; |
| 268 | |
| 269 | error = device_register (&kbddev); |
| 270 | if(error==0) { |
| 271 | /* check if this is the standard input device */ |
| 272 | if(strcmp(stdinname,DEVNAME)==0) { |
| 273 | /* reassign the console */ |
| 274 | if(overwrite_console()) { |
| 275 | return 1; |
| 276 | } |
| 277 | error=console_assign(stdin,DEVNAME); |
| 278 | if(error==0) |
| 279 | return 1; |
| 280 | else |
| 281 | return error; |
| 282 | } |
| 283 | return 1; |
| 284 | } |
| 285 | return error; |
| 286 | } |
| 287 | |
| 288 | #endif /* CONFIG_PS2KBD */ |