wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * John W. Linville, linville@tuxdriver.com |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 4 | * |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 5 | * Modified from code for support of MIP405 and PIP405 boards. Previous |
| 6 | * copyright follows. |
| 7 | * |
| 8 | * (C) Copyright 2001 |
| 9 | * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch |
| 10 | * |
| 11 | * See file CREDITS for list of people who contributed to this |
| 12 | * project. |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of |
| 17 | * the License, or (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 27 | * MA 02111-1307 USA |
| 28 | * |
| 29 | * |
| 30 | * Source partly derived from: |
| 31 | * linux/drivers/char/pc_keyb.c |
| 32 | * |
| 33 | * |
| 34 | */ |
| 35 | #include <common.h> |
| 36 | #include <asm/processor.h> |
| 37 | #include <devices.h> |
| 38 | #include "ps2kbd.h" |
| 39 | |
| 40 | |
| 41 | unsigned char kbd_read_status(void); |
| 42 | unsigned char kbd_read_input(void); |
| 43 | void kbd_send_data(unsigned char data); |
| 44 | void i8259_mask_irq(unsigned int irq); |
| 45 | void i8259_unmask_irq(unsigned int irq); |
| 46 | |
| 47 | /* used only by send_data - set by keyboard_interrupt */ |
| 48 | |
| 49 | |
| 50 | #undef KBG_DEBUG |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 51 | |
| 52 | #ifdef KBG_DEBUG |
| 53 | #define PRINTF(fmt,args...) printf (fmt ,##args) |
| 54 | #else |
| 55 | #define PRINTF(fmt,args...) |
| 56 | #endif |
| 57 | |
| 58 | #define KBD_STAT_KOBF 0x01 |
| 59 | #define KBD_STAT_IBF 0x02 |
| 60 | #define KBD_STAT_SYS 0x04 |
| 61 | #define KBD_STAT_CD 0x08 |
| 62 | #define KBD_STAT_LOCK 0x10 |
| 63 | #define KBD_STAT_MOBF 0x20 |
| 64 | #define KBD_STAT_TI_OUT 0x40 |
| 65 | #define KBD_STAT_PARERR 0x80 |
| 66 | |
| 67 | #define KBD_INIT_TIMEOUT 2000 /* Timeout in ms for initializing the keyboard */ |
| 68 | #define KBC_TIMEOUT 250 /* Timeout in ms for sending to keyboard controller */ |
| 69 | #define KBD_TIMEOUT 2000 /* Timeout in ms for keyboard command acknowledge */ |
| 70 | /* |
| 71 | * Keyboard Controller Commands |
| 72 | */ |
| 73 | |
| 74 | #define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */ |
| 75 | #define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */ |
| 76 | #define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */ |
| 77 | #define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */ |
| 78 | #define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */ |
| 79 | #define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */ |
| 80 | #define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */ |
| 81 | #define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */ |
| 82 | #define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */ |
| 83 | #define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */ |
| 84 | #define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if |
| 85 | initiated by the auxiliary device */ |
| 86 | #define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */ |
| 87 | |
| 88 | /* |
| 89 | * Keyboard Commands |
| 90 | */ |
| 91 | |
| 92 | #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */ |
| 93 | #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */ |
| 94 | #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */ |
| 95 | #define KBD_CMD_DISABLE 0xF5 /* Disable scanning */ |
| 96 | #define KBD_CMD_RESET 0xFF /* Reset */ |
| 97 | |
| 98 | /* |
| 99 | * Keyboard Replies |
| 100 | */ |
| 101 | |
| 102 | #define KBD_REPLY_POR 0xAA /* Power on reset */ |
| 103 | #define KBD_REPLY_ACK 0xFA /* Command ACK */ |
| 104 | #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */ |
| 105 | |
| 106 | /* |
| 107 | * Status Register Bits |
| 108 | */ |
| 109 | |
| 110 | #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */ |
| 111 | #define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */ |
| 112 | #define KBD_STAT_SELFTEST 0x04 /* Self test successful */ |
| 113 | #define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */ |
| 114 | #define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */ |
| 115 | #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */ |
| 116 | #define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */ |
| 117 | #define KBD_STAT_PERR 0x80 /* Parity error */ |
| 118 | |
| 119 | #define AUX_STAT_OBF (KBD_STAT_OBF | KBD_STAT_MOUSE_OBF) |
| 120 | |
| 121 | /* |
| 122 | * Controller Mode Register Bits |
| 123 | */ |
| 124 | |
| 125 | #define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */ |
| 126 | #define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */ |
| 127 | #define KBD_MODE_SYS 0x04 /* The system flag (?) */ |
| 128 | #define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */ |
| 129 | #define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */ |
| 130 | #define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */ |
| 131 | #define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */ |
| 132 | #define KBD_MODE_RFU 0x80 |
| 133 | |
| 134 | |
| 135 | #define KDB_DATA_PORT 0x60 |
| 136 | #define KDB_COMMAND_PORT 0x64 |
| 137 | |
| 138 | #define LED_SCR 0x01 /* scroll lock led */ |
| 139 | #define LED_CAP 0x04 /* caps lock led */ |
| 140 | #define LED_NUM 0x02 /* num lock led */ |
| 141 | |
| 142 | #define KBD_BUFFER_LEN 0x20 /* size of the keyboardbuffer */ |
| 143 | |
| 144 | |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 145 | static volatile char kbd_buffer[KBD_BUFFER_LEN]; |
| 146 | static volatile int in_pointer = 0; |
| 147 | static volatile int out_pointer = 0; |
| 148 | |
| 149 | |
| 150 | static unsigned char num_lock = 0; |
| 151 | static unsigned char caps_lock = 0; |
| 152 | static unsigned char scroll_lock = 0; |
| 153 | static unsigned char shift = 0; |
| 154 | static unsigned char ctrl = 0; |
| 155 | static unsigned char alt = 0; |
| 156 | static unsigned char e0 = 0; |
| 157 | static unsigned char leds = 0; |
| 158 | |
| 159 | #define DEVNAME "ps2kbd" |
| 160 | |
| 161 | /* Simple translation table for the keys */ |
| 162 | |
| 163 | static unsigned char kbd_plain_xlate[] = { |
| 164 | 0xff,0x1b, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=','\b','\t', /* 0x00 - 0x0f */ |
| 165 | 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']','\r',0xff, 'a', 's', /* 0x10 - 0x1f */ |
| 166 | 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';','\'', '`',0xff,'\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */ |
| 167 | 'b', 'n', 'm', ',', '.', '/',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */ |
| 168 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */ |
| 169 | '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */ |
| 170 | '\r',0xff,0xff |
| 171 | }; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 172 | |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 173 | static unsigned char kbd_shift_xlate[] = { |
| 174 | 0xff,0x1b, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+','\b','\t', /* 0x00 - 0x0f */ |
| 175 | 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}','\r',0xff, 'A', 'S', /* 0x10 - 0x1f */ |
| 176 | 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',0xff, '|', 'Z', 'X', 'C', 'V', /* 0x20 - 0x2f */ |
| 177 | 'B', 'N', 'M', '<', '>', '?',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */ |
| 178 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */ |
| 179 | '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */ |
| 180 | '\r',0xff,0xff |
| 181 | }; |
| 182 | |
| 183 | static unsigned char kbd_ctrl_xlate[] = { |
| 184 | 0xff,0x1b, '1',0x00, '3', '4', '5',0x1E, '7', '8', '9', '0',0x1F, '=','\b','\t', /* 0x00 - 0x0f */ |
| 185 | 0x11,0x17,0x05,0x12,0x14,0x18,0x15,0x09,0x0f,0x10,0x1b,0x1d,'\n',0xff,0x01,0x13, /* 0x10 - 0x1f */ |
| 186 | 0x04,0x06,0x08,0x09,0x0a,0x0b,0x0c, ';','\'', '~',0x00,0x1c,0x1a,0x18,0x03,0x16, /* 0x20 - 0x2f */ |
| 187 | 0x02,0x0e,0x0d, '<', '>', '?',0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */ |
| 188 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */ |
| 189 | '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */ |
| 190 | '\r',0xff,0xff |
| 191 | }; |
| 192 | |
| 193 | /****************************************************************** |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 194 | * Init |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 195 | ******************************************************************/ |
| 196 | |
| 197 | int isa_kbd_init(void) |
| 198 | { |
| 199 | char* result; |
| 200 | result=kbd_initialize(); |
| 201 | if (result != NULL) |
| 202 | { |
| 203 | result = kbd_initialize(); |
| 204 | } |
| 205 | if(result==NULL) { |
| 206 | printf("AT Keyboard initialized\n"); |
| 207 | irq_install_handler(KBD_INTERRUPT, (interrupt_handler_t *)kbd_interrupt, NULL); |
| 208 | return (1); |
| 209 | } |
| 210 | else { |
| 211 | printf("%s\n",result); |
| 212 | return (-1); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | #ifdef CFG_CONSOLE_OVERWRITE_ROUTINE |
| 217 | extern int overwrite_console (void); |
| 218 | #else |
| 219 | int overwrite_console (void) |
| 220 | { |
| 221 | return (0); |
| 222 | } |
| 223 | #endif |
| 224 | |
| 225 | int drv_isa_kbd_init (void) |
| 226 | { |
| 227 | int error; |
| 228 | device_t kbddev ; |
| 229 | char *stdinname = getenv ("stdin"); |
| 230 | |
| 231 | if(isa_kbd_init()==-1) |
| 232 | return -1; |
| 233 | memset (&kbddev, 0, sizeof(kbddev)); |
| 234 | strcpy(kbddev.name, DEVNAME); |
| 235 | kbddev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM; |
| 236 | kbddev.putc = NULL ; |
| 237 | kbddev.puts = NULL ; |
| 238 | kbddev.getc = kbd_getc ; |
| 239 | kbddev.tstc = kbd_testc ; |
| 240 | |
| 241 | error = device_register (&kbddev); |
| 242 | if(error==0) { |
| 243 | /* check if this is the standard input device */ |
| 244 | if(strcmp(stdinname,DEVNAME)==0) { |
| 245 | /* reassign the console */ |
| 246 | if(overwrite_console()) { |
| 247 | return 1; |
| 248 | } |
| 249 | error=console_assign(stdin,DEVNAME); |
| 250 | if(error==0) |
| 251 | return 1; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 252 | else |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 253 | return error; |
| 254 | } |
| 255 | return 1; |
| 256 | } |
| 257 | return error; |
| 258 | } |
| 259 | |
| 260 | /****************************************************************** |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 261 | * Queue handling |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 262 | ******************************************************************/ |
| 263 | /* puts character in the queue and sets up the in and out pointer */ |
| 264 | void kbd_put_queue(char data) |
| 265 | { |
| 266 | if((in_pointer+1)==KBD_BUFFER_LEN) { |
| 267 | if(out_pointer==0) { |
| 268 | return; /* buffer full */ |
| 269 | } else{ |
| 270 | in_pointer=0; |
| 271 | } |
| 272 | } else { |
| 273 | if((in_pointer+1)==out_pointer) |
| 274 | return; /* buffer full */ |
| 275 | in_pointer++; |
| 276 | } |
| 277 | kbd_buffer[in_pointer]=data; |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | /* test if a character is in the queue */ |
| 282 | int kbd_testc(void) |
| 283 | { |
| 284 | if(in_pointer==out_pointer) |
| 285 | return(0); /* no data */ |
| 286 | else |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 287 | return(1); |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 288 | } |
| 289 | /* gets the character from the queue */ |
| 290 | int kbd_getc(void) |
| 291 | { |
| 292 | char c; |
| 293 | |
| 294 | while(in_pointer==out_pointer); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 295 | if((out_pointer+1)==KBD_BUFFER_LEN) |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 296 | out_pointer=0; |
| 297 | else |
| 298 | out_pointer++; |
| 299 | c=kbd_buffer[out_pointer]; |
| 300 | return (int)c; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 301 | |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | |
| 305 | /* set LEDs */ |
| 306 | |
| 307 | void kbd_set_leds(void) |
| 308 | { |
| 309 | if(caps_lock==0) |
| 310 | leds&=~LED_CAP; /* switch caps_lock off */ |
| 311 | else |
| 312 | leds|=LED_CAP; /* switch on LED */ |
| 313 | if(num_lock==0) |
| 314 | leds&=~LED_NUM; /* switch LED off */ |
| 315 | else |
| 316 | leds|=LED_NUM; /* switch on LED */ |
| 317 | if(scroll_lock==0) |
| 318 | leds&=~LED_SCR; /* switch LED off */ |
| 319 | else |
| 320 | leds|=LED_SCR; /* switch on LED */ |
| 321 | kbd_send_data(KBD_CMD_SET_LEDS); |
| 322 | kbd_send_data(leds); |
| 323 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 324 | |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 325 | |
| 326 | void handle_keyboard_event(unsigned char scancode) |
| 327 | { |
| 328 | unsigned char keycode; |
| 329 | |
| 330 | /* Convert scancode to keycode */ |
| 331 | PRINTF("scancode %x\n",scancode); |
| 332 | if(scancode==0xe0) { |
| 333 | e0=1; /* special charakters */ |
| 334 | return; |
| 335 | } |
| 336 | if(e0==1) { |
| 337 | e0=0; /* delete flag */ |
| 338 | if(!( ((scancode&0x7F)==0x38)|| /* the right ctrl key */ |
| 339 | ((scancode&0x7F)==0x1D)|| /* the right alt key */ |
| 340 | ((scancode&0x7F)==0x35)|| /* the right '/' key */ |
| 341 | ((scancode&0x7F)==0x1C)|| /* the right enter key */ |
| 342 | ((scancode)==0x48)|| /* arrow up */ |
| 343 | ((scancode)==0x50)|| /* arrow down */ |
| 344 | ((scancode)==0x4b)|| /* arrow left */ |
| 345 | ((scancode)==0x4d))) /* arrow right */ |
| 346 | /* we swallow unknown e0 codes */ |
| 347 | return; |
| 348 | } |
| 349 | /* special cntrl keys */ |
| 350 | switch(scancode) |
| 351 | { |
| 352 | case 0x48: |
| 353 | kbd_put_queue(27); |
| 354 | kbd_put_queue(91); |
| 355 | kbd_put_queue('A'); |
| 356 | return; |
| 357 | case 0x50: |
| 358 | kbd_put_queue(27); |
| 359 | kbd_put_queue(91); |
| 360 | kbd_put_queue('B'); |
| 361 | return; |
| 362 | case 0x4b: |
| 363 | kbd_put_queue(27); |
| 364 | kbd_put_queue(91); |
| 365 | kbd_put_queue('D'); |
| 366 | return; |
| 367 | case 0x4D: |
| 368 | kbd_put_queue(27); |
| 369 | kbd_put_queue(91); |
| 370 | kbd_put_queue('C'); |
| 371 | return; |
| 372 | case 0x58: /* F12 key */ |
| 373 | if (ctrl == 1) |
| 374 | { |
| 375 | extern int console_changed; |
| 376 | setenv("stdin", DEVNAME); |
| 377 | setenv("stdout", "vga"); |
| 378 | console_changed = 1; |
| 379 | } |
| 380 | return; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 381 | case 0x2A: |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 382 | case 0x36: /* shift pressed */ |
| 383 | shift=1; |
| 384 | return; /* do nothing else */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 385 | case 0xAA: |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 386 | case 0xB6: /* shift released */ |
| 387 | shift=0; |
| 388 | return; /* do nothing else */ |
| 389 | case 0x38: /* alt pressed */ |
| 390 | alt=1; |
| 391 | return; /* do nothing else */ |
| 392 | case 0xB8: /* alt released */ |
| 393 | alt=0; |
| 394 | return; /* do nothing else */ |
| 395 | case 0x1d: /* ctrl pressed */ |
| 396 | ctrl=1; |
| 397 | return; /* do nothing else */ |
| 398 | case 0x9d: /* ctrl released */ |
| 399 | ctrl=0; |
| 400 | return; /* do nothing else */ |
| 401 | case 0x46: /* scrollock pressed */ |
| 402 | scroll_lock=~scroll_lock; |
| 403 | kbd_set_leds(); |
| 404 | return; /* do nothing else */ |
| 405 | case 0x3A: /* capslock pressed */ |
| 406 | caps_lock=~caps_lock; |
| 407 | kbd_set_leds(); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 408 | return; |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 409 | case 0x45: /* numlock pressed */ |
| 410 | num_lock=~num_lock; |
| 411 | kbd_set_leds(); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 412 | return; |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 413 | case 0xC6: /* scroll lock released */ |
| 414 | case 0xC5: /* num lock released */ |
| 415 | case 0xBA: /* caps lock released */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 416 | return; /* just swallow */ |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 417 | } |
| 418 | if((scancode&0x80)==0x80) /* key released */ |
| 419 | return; |
| 420 | /* now, decide which table we need */ |
| 421 | if(scancode > (sizeof(kbd_plain_xlate)/sizeof(kbd_plain_xlate[0]))) { /* scancode not in list */ |
| 422 | PRINTF("unkown scancode %X\n",scancode); |
| 423 | return; /* swallow it */ |
| 424 | } |
| 425 | /* setup plain code first */ |
| 426 | keycode=kbd_plain_xlate[scancode]; |
| 427 | if(caps_lock==1) { /* caps_lock is pressed, overwrite plain code */ |
| 428 | if(scancode > (sizeof(kbd_shift_xlate)/sizeof(kbd_shift_xlate[0]))) { /* scancode not in list */ |
| 429 | PRINTF("unkown caps-locked scancode %X\n",scancode); |
| 430 | return; /* swallow it */ |
| 431 | } |
| 432 | keycode=kbd_shift_xlate[scancode]; |
| 433 | if(keycode<'A') { /* we only want the alphas capital */ |
| 434 | keycode=kbd_plain_xlate[scancode]; |
| 435 | } |
| 436 | } |
| 437 | if(shift==1) { /* shift overwrites caps_lock */ |
| 438 | if(scancode > (sizeof(kbd_shift_xlate)/sizeof(kbd_shift_xlate[0]))) { /* scancode not in list */ |
| 439 | PRINTF("unkown shifted scancode %X\n",scancode); |
| 440 | return; /* swallow it */ |
| 441 | } |
| 442 | keycode=kbd_shift_xlate[scancode]; |
| 443 | } |
| 444 | if(ctrl==1) { /* ctrl overwrites caps_lock and shift */ |
| 445 | if(scancode > (sizeof(kbd_ctrl_xlate)/sizeof(kbd_ctrl_xlate[0]))) { /* scancode not in list */ |
| 446 | PRINTF("unkown ctrl scancode %X\n",scancode); |
| 447 | return; /* swallow it */ |
| 448 | } |
| 449 | keycode=kbd_ctrl_xlate[scancode]; |
| 450 | } |
| 451 | /* check if valid keycode */ |
| 452 | if(keycode==0xff) { |
| 453 | PRINTF("unkown scancode %X\n",scancode); |
| 454 | return; /* swallow unknown codes */ |
| 455 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 456 | |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 457 | kbd_put_queue(keycode); |
| 458 | PRINTF("%x\n",keycode); |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * This reads the keyboard status port, and does the |
| 463 | * appropriate action. |
| 464 | * |
| 465 | */ |
| 466 | unsigned char handle_kbd_event(void) |
| 467 | { |
| 468 | unsigned char status = kbd_read_status(); |
| 469 | unsigned int work = 10000; |
| 470 | |
| 471 | while ((--work > 0) && (status & KBD_STAT_OBF)) { |
| 472 | unsigned char scancode; |
| 473 | |
| 474 | scancode = kbd_read_input(); |
| 475 | |
| 476 | /* Error bytes must be ignored to make the |
| 477 | Synaptics touchpads compaq use work */ |
| 478 | /* Ignore error bytes */ |
| 479 | if (!(status & (KBD_STAT_GTO | KBD_STAT_PERR))) |
| 480 | { |
| 481 | if (status & KBD_STAT_MOUSE_OBF) |
| 482 | ; /* not supported: handle_mouse_event(scancode); */ |
| 483 | else |
| 484 | handle_keyboard_event(scancode); |
| 485 | } |
| 486 | status = kbd_read_status(); |
| 487 | } |
| 488 | if (!work) |
| 489 | PRINTF("pc_keyb: controller jammed (0x%02X).\n", status); |
| 490 | return status; |
| 491 | } |
| 492 | |
| 493 | |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 494 | /****************************************************************************** |
| 495 | * Lowlevel Part of keyboard section |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 496 | */ |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 497 | unsigned char kbd_read_status(void) |
| 498 | { |
| 499 | return(in8(CFG_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT)); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 500 | } |
| 501 | |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 502 | unsigned char kbd_read_input(void) |
| 503 | { |
| 504 | return(in8(CFG_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT)); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 505 | } |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 506 | |
| 507 | void kbd_write_command(unsigned char cmd) |
| 508 | { |
| 509 | out8(CFG_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT,cmd); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 510 | } |
| 511 | |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 512 | void kbd_write_output(unsigned char data) |
| 513 | { |
| 514 | out8(CFG_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT, data); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 515 | } |
| 516 | |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 517 | int kbd_read_data(void) |
| 518 | { |
| 519 | int val; |
| 520 | unsigned char status; |
| 521 | |
| 522 | val=-1; |
| 523 | status = kbd_read_status(); |
| 524 | if (status & KBD_STAT_OBF) { |
| 525 | val = kbd_read_input(); |
| 526 | if (status & (KBD_STAT_GTO | KBD_STAT_PERR)) |
| 527 | val = -2; |
| 528 | } |
| 529 | return val; |
| 530 | } |
| 531 | |
| 532 | int kbd_wait_for_input(void) |
| 533 | { |
| 534 | unsigned long timeout; |
| 535 | int val; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 536 | |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 537 | timeout = KBD_TIMEOUT; |
| 538 | val=kbd_read_data(); |
| 539 | while(val < 0) |
| 540 | { |
| 541 | if(timeout--==0) |
| 542 | return -1; |
| 543 | udelay(1000); |
| 544 | val=kbd_read_data(); |
| 545 | } |
| 546 | return val; |
| 547 | } |
| 548 | |
| 549 | |
| 550 | int kb_wait(void) |
| 551 | { |
| 552 | unsigned long timeout = KBC_TIMEOUT * 10; |
| 553 | |
| 554 | do { |
| 555 | unsigned char status = handle_kbd_event(); |
| 556 | if (!(status & KBD_STAT_IBF)) |
| 557 | return 0; /* ok */ |
| 558 | udelay(1000); |
| 559 | timeout--; |
| 560 | } while (timeout); |
| 561 | return 1; |
| 562 | } |
| 563 | |
| 564 | void kbd_write_command_w(int data) |
| 565 | { |
| 566 | if(kb_wait()) |
| 567 | PRINTF("timeout in kbd_write_command_w\n"); |
| 568 | kbd_write_command(data); |
| 569 | } |
| 570 | |
| 571 | void kbd_write_output_w(int data) |
| 572 | { |
| 573 | if(kb_wait()) |
| 574 | PRINTF("timeout in kbd_write_output_w\n"); |
| 575 | kbd_write_output(data); |
| 576 | } |
| 577 | |
| 578 | void kbd_send_data(unsigned char data) |
| 579 | { |
| 580 | unsigned char status; |
| 581 | i8259_mask_irq(KBD_INTERRUPT); /* disable interrupt */ |
| 582 | kbd_write_output_w(data); |
| 583 | status = kbd_wait_for_input(); |
| 584 | if (status == KBD_REPLY_ACK) |
| 585 | i8259_unmask_irq(KBD_INTERRUPT); /* enable interrupt */ |
| 586 | } |
| 587 | |
| 588 | |
| 589 | char * kbd_initialize(void) |
| 590 | { |
| 591 | int status; |
| 592 | |
| 593 | in_pointer = 0; /* delete in Buffer */ |
| 594 | out_pointer = 0; |
| 595 | /* |
| 596 | * Test the keyboard interface. |
| 597 | * This seems to be the only way to get it going. |
| 598 | * If the test is successful a x55 is placed in the input buffer. |
| 599 | */ |
| 600 | kbd_write_command_w(KBD_CCMD_SELF_TEST); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 601 | if (kbd_wait_for_input() != 0x55) |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 602 | return "Kbd: failed self test"; |
| 603 | /* |
| 604 | * Perform a keyboard interface test. This causes the controller |
| 605 | * to test the keyboard clock and data lines. The results of the |
| 606 | * test are placed in the input buffer. |
| 607 | */ |
| 608 | kbd_write_command_w(KBD_CCMD_KBD_TEST); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 609 | if (kbd_wait_for_input() != 0x00) |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 610 | return "Kbd: interface failed self test"; |
| 611 | /* |
| 612 | * Enable the keyboard by allowing the keyboard clock to run. |
| 613 | */ |
| 614 | kbd_write_command_w(KBD_CCMD_KBD_ENABLE); |
| 615 | status = kbd_wait_for_input(); |
| 616 | /* |
| 617 | * Reset keyboard. If the read times out |
| 618 | * then the assumption is that no keyboard is |
| 619 | * plugged into the machine. |
| 620 | * This defaults the keyboard to scan-code set 2. |
| 621 | * |
| 622 | * Set up to try again if the keyboard asks for RESEND. |
| 623 | */ |
| 624 | do { |
| 625 | kbd_write_output_w(KBD_CMD_RESET); |
| 626 | status = kbd_wait_for_input(); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 627 | if (status == KBD_REPLY_ACK) |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 628 | break; |
| 629 | if (status != KBD_REPLY_RESEND) |
| 630 | { |
| 631 | PRINTF("status: %X\n",status); |
| 632 | return "Kbd: reset failed, no ACK"; |
| 633 | } |
| 634 | } while (1); |
| 635 | if (kbd_wait_for_input() != KBD_REPLY_POR) |
| 636 | return "Kbd: reset failed, no POR"; |
| 637 | |
| 638 | /* |
| 639 | * Set keyboard controller mode. During this, the keyboard should be |
| 640 | * in the disabled state. |
| 641 | * |
| 642 | * Set up to try again if the keyboard asks for RESEND. |
| 643 | */ |
| 644 | do { |
| 645 | kbd_write_output_w(KBD_CMD_DISABLE); |
| 646 | status = kbd_wait_for_input(); |
| 647 | if (status == KBD_REPLY_ACK) |
| 648 | break; |
| 649 | if (status != KBD_REPLY_RESEND) |
| 650 | return "Kbd: disable keyboard: no ACK"; |
| 651 | } while (1); |
| 652 | |
| 653 | kbd_write_command_w(KBD_CCMD_WRITE_MODE); |
| 654 | kbd_write_output_w(KBD_MODE_KBD_INT |
| 655 | | KBD_MODE_SYS |
| 656 | | KBD_MODE_DISABLE_MOUSE |
| 657 | | KBD_MODE_KCC); |
| 658 | |
Wolfgang Denk | 0ee7077 | 2005-09-23 11:05:55 +0200 | [diff] [blame] | 659 | /* AMCC powerpc portables need this to use scan-code set 1 -- Cort */ |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 660 | kbd_write_command_w(KBD_CCMD_READ_MODE); |
| 661 | if (!(kbd_wait_for_input() & KBD_MODE_KCC)) { |
| 662 | /* |
| 663 | * If the controller does not support conversion, |
| 664 | * Set the keyboard to scan-code set 1. |
| 665 | */ |
| 666 | kbd_write_output_w(0xF0); |
| 667 | kbd_wait_for_input(); |
| 668 | kbd_write_output_w(0x01); |
| 669 | kbd_wait_for_input(); |
| 670 | } |
| 671 | kbd_write_output_w(KBD_CMD_ENABLE); |
| 672 | if (kbd_wait_for_input() != KBD_REPLY_ACK) |
| 673 | return "Kbd: enable keyboard: no ACK"; |
| 674 | |
| 675 | /* |
| 676 | * Finally, set the typematic rate to maximum. |
| 677 | */ |
| 678 | kbd_write_output_w(KBD_CMD_SET_RATE); |
| 679 | if (kbd_wait_for_input() != KBD_REPLY_ACK) |
| 680 | return "Kbd: Set rate: no ACK"; |
| 681 | kbd_write_output_w(0x00); |
| 682 | if (kbd_wait_for_input() != KBD_REPLY_ACK) |
| 683 | return "Kbd: Set rate: no ACK"; |
| 684 | return NULL; |
| 685 | } |
| 686 | |
| 687 | void kbd_interrupt(void) |
| 688 | { |
| 689 | handle_kbd_event(); |
| 690 | } |