27,9 → 27,8 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
|
/** @addtogroup kbdia32 ia32 |
* @brief HelenOS ia32 / amd64 arch dependent parts of uspace keyboard handler. |
* @ingroup kbd |
/** @addtogroup kbdia32 |
* @brief Scancodes for PC keyboards. |
* @{ |
*/ |
/** @file |
36,67 → 35,10 |
* @ingroup kbdamd64 |
*/ |
|
#include <arch/kbd.h> |
#include <ipc/ipc.h> |
#include <unistd.h> |
#include <kbd.h> |
#include <keys.h> |
#include <genarch/scanc.h> |
|
/* Interesting bits for status register */ |
#define i8042_OUTPUT_FULL 0x1 |
#define i8042_INPUT_FULL 0x2 |
#define i8042_MOUSE_DATA 0x20 |
|
/* Command constants */ |
#define i8042_CMD_KBD 0x60 |
#define i8042_CMD_MOUSE 0xd4 |
|
/* Keyboard cmd byte */ |
#define i8042_KBD_IE 0x1 |
#define i8042_MOUSE_IE 0x2 |
#define i8042_KBD_DISABLE 0x10 |
#define i8042_MOUSE_DISABLE 0x20 |
#define i8042_KBD_TRANSLATE 0x40 |
|
/* Mouse constants */ |
#define MOUSE_OUT_INIT 0xf4 |
#define MOUSE_ACK 0xfa |
|
|
#define SPECIAL 255 |
#define KEY_RELEASE 0x80 |
|
/** |
* These codes read from i8042 data register are silently ignored. |
*/ |
#define IGNORE_CODE 0x7f |
|
#define PRESSED_SHIFT (1<<0) |
#define PRESSED_CAPSLOCK (1<<1) |
#define LOCKED_CAPSLOCK (1<<0) |
|
/** Scancodes. */ |
#define SC_ESC 0x01 |
#define SC_BACKSPACE 0x0e |
#define SC_LSHIFT 0x2a |
#define SC_RSHIFT 0x36 |
#define SC_CAPSLOCK 0x3a |
#define SC_SPEC_ESCAPE 0xe0 |
#define SC_LEFTARR 0x4b |
#define SC_RIGHTARR 0x4d |
#define SC_UPARR 0x48 |
#define SC_DOWNARR 0x50 |
#define SC_DELETE 0x53 |
#define SC_HOME 0x47 |
#define SC_END 0x4f |
|
#define FUNCTION_KEYS 0x100 |
|
static volatile int keyflags; /**< Tracking of multiple keypresses. */ |
static volatile int lockflags; /**< Tracking of multiple keys lockings. */ |
|
/** Primary meaning of scancodes. */ |
static int sc_primary_map[] = { |
int sc_primary_map[] = { |
SPECIAL, /* 0x00 */ |
SPECIAL, /* 0x01 - Esc */ |
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', |
176,7 → 118,7 |
}; |
|
/** Secondary meaning of scancodes. */ |
static int sc_secondary_map[] = { |
int sc_secondary_map[] = { |
SPECIAL, /* 0x00 */ |
0x1b, /* 0x01 - Esc */ |
'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', |
255,222 → 197,6 |
SPECIAL, /* 0x7f */ |
}; |
|
irq_cmd_t i8042_cmds[2] = { |
{ CMD_PORT_READ_1, (void *)0x64, 0, 1 }, |
{ CMD_PORT_READ_1, (void *)0x60, 0, 2 } |
}; |
|
irq_code_t i8042_kbd = { |
2, |
i8042_cmds |
}; |
|
static void key_released(keybuffer_t *keybuffer, unsigned char key) |
{ |
switch (key) { |
case SC_LSHIFT: |
case SC_RSHIFT: |
keyflags &= ~PRESSED_SHIFT; |
break; |
case SC_CAPSLOCK: |
keyflags &= ~PRESSED_CAPSLOCK; |
if (lockflags & LOCKED_CAPSLOCK) |
lockflags &= ~LOCKED_CAPSLOCK; |
else |
lockflags |= LOCKED_CAPSLOCK; |
break; |
default: |
break; |
} |
} |
|
static void key_pressed(keybuffer_t *keybuffer, unsigned char key) |
{ |
int *map = sc_primary_map; |
int ascii = sc_primary_map[key]; |
int shift, capslock; |
int letter = 0; |
|
static int esc_count=0; |
|
|
if ( key == SC_ESC ) { |
esc_count++; |
if ( esc_count == 3 ) { |
__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE); |
} |
} else { |
esc_count=0; |
} |
|
|
|
switch (key) { |
case SC_LSHIFT: |
case SC_RSHIFT: |
keyflags |= PRESSED_SHIFT; |
break; |
case SC_CAPSLOCK: |
keyflags |= PRESSED_CAPSLOCK; |
break; |
case SC_SPEC_ESCAPE: |
break; |
/* case SC_LEFTARR: |
if (keybuffer_available(keybuffer) >= 3) { |
keybuffer_push(keybuffer, 0x1b); |
keybuffer_push(keybuffer, 0x5b); |
keybuffer_push(keybuffer, 0x44); |
} |
break; |
case SC_RIGHTARR: |
if (keybuffer_available(keybuffer) >= 3) { |
keybuffer_push(keybuffer, 0x1b); |
keybuffer_push(keybuffer, 0x5b); |
keybuffer_push(keybuffer, 0x43); |
} |
break; |
case SC_UPARR: |
if (keybuffer_available(keybuffer) >= 3) { |
keybuffer_push(keybuffer, 0x1b); |
keybuffer_push(keybuffer, 0x5b); |
keybuffer_push(keybuffer, 0x41); |
} |
break; |
case SC_DOWNARR: |
if (keybuffer_available(keybuffer) >= 3) { |
keybuffer_push(keybuffer, 0x1b); |
keybuffer_push(keybuffer, 0x5b); |
keybuffer_push(keybuffer, 0x42); |
} |
break; |
case SC_HOME: |
if (keybuffer_available(keybuffer) >= 3) { |
keybuffer_push(keybuffer, 0x1b); |
keybuffer_push(keybuffer, 0x4f); |
keybuffer_push(keybuffer, 0x48); |
} |
break; |
case SC_END: |
if (keybuffer_available(keybuffer) >= 3) { |
keybuffer_push(keybuffer, 0x1b); |
keybuffer_push(keybuffer, 0x4f); |
keybuffer_push(keybuffer, 0x46); |
} |
break; |
case SC_DELETE: |
if (keybuffer_available(keybuffer) >= 4) { |
keybuffer_push(keybuffer, 0x1b); |
keybuffer_push(keybuffer, 0x5b); |
keybuffer_push(keybuffer, 0x33); |
keybuffer_push(keybuffer, 0x7e); |
} |
break; |
*/ default: |
letter = ((ascii >= 'a') && (ascii <= 'z')); |
capslock = (keyflags & PRESSED_CAPSLOCK) || (lockflags & LOCKED_CAPSLOCK); |
shift = keyflags & PRESSED_SHIFT; |
if (letter && capslock) |
shift = !shift; |
if (shift) |
map = sc_secondary_map; |
if (map[key] != SPECIAL) |
keybuffer_push(keybuffer, map[key]); |
break; |
} |
} |
|
|
static void wait_ready(void) { |
while (i8042_status_read() & i8042_INPUT_FULL) |
; |
} |
|
/** Register uspace irq handler |
* @return |
*/ |
int kbd_arch_init(void) |
{ |
int i; |
int mouseenabled = 0; |
|
iospace_enable(task_get_id(),(void *)i8042_DATA, 5); |
/* Disable kbd, enable mouse */ |
i8042_command_write(i8042_CMD_KBD); |
wait_ready(); |
i8042_command_write(i8042_CMD_KBD); |
wait_ready(); |
i8042_data_write(i8042_KBD_DISABLE); |
wait_ready(); |
|
/* Flush all current IO */ |
while (i8042_status_read() & i8042_OUTPUT_FULL) |
i8042_data_read(); |
/* Initialize mouse */ |
i8042_command_write(i8042_CMD_MOUSE); |
wait_ready(); |
i8042_data_write(MOUSE_OUT_INIT); |
wait_ready(); |
|
int mouseanswer = 0; |
for (i=0;i < 1000; i++) { |
int status = i8042_status_read(); |
if (status & i8042_OUTPUT_FULL) { |
int data = i8042_data_read(); |
if (status & i8042_MOUSE_DATA) { |
mouseanswer = data; |
break; |
} |
} |
usleep(1000); |
} |
if (mouseanswer == MOUSE_ACK) { |
/* enable mouse */ |
mouseenabled = 1; |
|
ipc_register_irq(MOUSE_IRQ, &i8042_kbd); |
} |
/* Enable kbd */ |
ipc_register_irq(KBD_IRQ, &i8042_kbd); |
/* Register for irq restart */ |
ipc_register_irq(IPC_IRQ_KBDRESTART, NULL); |
|
int newcontrol = i8042_KBD_IE | i8042_KBD_TRANSLATE; |
if (mouseenabled) |
newcontrol |= i8042_MOUSE_IE; |
|
i8042_command_write(i8042_CMD_KBD); |
wait_ready(); |
i8042_data_write(newcontrol); |
wait_ready(); |
|
return 0; |
} |
|
/** Process keyboard & mouse events */ |
int kbd_arch_process(keybuffer_t *keybuffer, ipc_call_t *call) |
{ |
int status = IPC_GET_ARG1(*call); |
|
if (IPC_GET_METHOD(*call) == IPC_IRQ_KBDRESTART) { |
kbd_arch_init(); |
return 1; |
} |
|
if ((status & i8042_MOUSE_DATA)) |
return 0; |
|
int scan_code = IPC_GET_ARG2(*call); |
|
if (scan_code != IGNORE_CODE) { |
if (scan_code & KEY_RELEASE) |
key_released(keybuffer, scan_code ^ KEY_RELEASE); |
else |
key_pressed(keybuffer, scan_code); |
} |
return 1; |
} |
|
/** |
* @} |
*/ |
|