Support scancode set 2

This commit is contained in:
Vladimir 'phcoder' Serbinenko 2010-08-22 22:53:31 +02:00
parent efc3e75f4d
commit 5ea70ca5fa
5 changed files with 410 additions and 214 deletions

View file

@ -19,21 +19,13 @@
#ifndef GRUB_AT_KEYBOARD_HEADER #ifndef GRUB_AT_KEYBOARD_HEADER
#define GRUB_AT_KEYBOARD_HEADER 1 #define GRUB_AT_KEYBOARD_HEADER 1
#define SHIFT_L 0x2a
#define SHIFT_R 0x36
#define CTRL 0x1d
#define ALT 0x38
#define CAPS_LOCK 0x3a
#define NUM_LOCK 0x45
#define SCROLL_LOCK 0x46
/* Used for sending commands to the controller. */ /* Used for sending commands to the controller. */
#define KEYBOARD_COMMAND_ISREADY(x) !((x) & 0x02) #define KEYBOARD_COMMAND_ISREADY(x) !((x) & 0x02)
#define KEYBOARD_COMMAND_READ 0x20 #define KEYBOARD_COMMAND_READ 0x20
#define KEYBOARD_COMMAND_WRITE 0x60 #define KEYBOARD_COMMAND_WRITE 0x60
#define KEYBOARD_COMMAND_REBOOT 0xfe #define KEYBOARD_COMMAND_REBOOT 0xfe
#define KEYBOARD_SCANCODE_SET1 0x40 #define KEYBOARD_AT_TRANSLATE 0x40
#define KEYBOARD_ISMAKE(x) !((x) & 0x80) #define KEYBOARD_ISMAKE(x) !((x) & 0x80)
#define KEYBOARD_ISREADY(x) ((x) & 0x01) #define KEYBOARD_ISREADY(x) ((x) & 0x01)

View file

@ -1,111 +0,0 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2010 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GRUB_AT_KEYMAP_HEADER
#define GRUB_AT_KEYMAP_HEADER 1
static inline int
grub_at_map_to_usb (grub_uint8_t keycode)
{
/* Modifier keys (ctrl, alt, shift, capslock, numlock and scrolllock
are handled by driver and hence here are mapped to 0)*/
static const grub_uint8_t at_to_usb_map[] =
{
/* 0x00 */ 0x00 /* Unused */, 0x29 /* Escape */,
/* 0x02 */ 0x1e /* 1 */, 0x1f /* 2 */,
/* 0x04 */ 0x20 /* 3 */, 0x21 /* 4 */,
/* 0x06 */ 0x22 /* 5 */, 0x23 /* 6 */,
/* 0x08 */ 0x24 /* 7 */, 0x25 /* 8 */,
/* 0x0a */ 0x26 /* 9 */, 0x27 /* 0 */,
/* 0x0c */ 0x2d /* - */, 0x2e /* = */,
/* 0x0e */ 0x2a /* \b */, 0x2b /* \t */,
/* 0x10 */ 0x14 /* q */, 0x1a /* w */,
/* 0x12 */ 0x08 /* e */, 0x15 /* r */,
/* 0x14 */ 0x17 /* t */, 0x1c /* y */,
/* 0x16 */ 0x18 /* u */, 0x0c /* i */,
/* 0x18 */ 0x12 /* o */, 0x13 /* p */,
/* 0x1a */ 0x2f /* [ */, 0x30 /* ] */,
/* 0x1c */ 0x28 /* Enter */, 0x00 /* Left CTRL */,
/* 0x1e */ 0x04 /* a */, 0x16 /* s */,
/* 0x20 */ 0x07 /* d */, 0x09 /* f */,
/* 0x22 */ 0x0a /* g */, 0x0b /* h */,
/* 0x24 */ 0x0d /* j */, 0x0e /* k */,
/* 0x26 */ 0x0f /* l */, 0x33 /* ; */,
/* 0x28 */ 0x34 /* " */, 0x35 /* ` */,
/* 0x2a */ 0x00 /* Left Shift */, 0x32 /* \ */,
/* 0x2c */ 0x1d /* z */, 0x1b /* x */,
/* 0x2e */ 0x06 /* c */, 0x19 /* v */,
/* 0x30 */ 0x05 /* b */, 0x11 /* n */,
/* 0x32 */ 0x10 /* m */, 0x36 /* , */,
/* 0x34 */ 0x37 /* . */, 0x38 /* / */,
/* 0x36 */ 0x00 /* Right Shift */, 0x55 /* Num * */,
/* 0x38 */ 0x00 /* Left ALT */, 0x2c /* Space */,
/* 0x3a */ 0x39 /* Caps Lock */, 0x3a /* F1 */,
/* 0x3c */ 0x3b /* F2 */, 0x3c /* F3 */,
/* 0x3e */ 0x3d /* F4 */, 0x3e /* F5 */,
/* 0x40 */ 0x3f /* F6 */, 0x40 /* F7 */,
/* 0x42 */ 0x41 /* F8 */, 0x42 /* F9 */,
/* 0x44 */ 0x43 /* F10 */, 0x53 /* NumLock */,
/* 0x46 */ 0x47 /* Scroll Lock */, 0x5f /* Num 7 */,
/* 0x48 */ 0x60 /* Num 8 */, 0x61 /* Num 9 */,
/* 0x4a */ 0x56 /* Num - */, 0x5c /* Num 4 */,
/* 0x4c */ 0x5d /* Num 5 */, 0x5e /* Num 6 */,
/* 0x4e */ 0x57 /* Num + */, 0x59 /* Num 1 */,
/* 0x50 */ 0x5a /* Num 2 */, 0x5b /* Num 3 */,
/* 0x52 */ 0x62 /* Num 0 */, 0x63 /* Num . */,
/* 0x54 */ 0x00, 0x00,
/* 0x56 */ 0x64 /* 102nd key. */, 0x44 /* F11 */,
/* 0x58 */ 0x45 /* F12 */, 0x00
};
static const struct
{
grub_uint8_t from, to;
} at_to_usb_extended[] =
{
/* OLPC keys. Just mapped to normal keys. */
{0x65, 0x52 /* Up */ },
{0x66, 0x51 /* Down */ },
{0x67, 0x50 /* Left */ },
{0x68, 0x4f /* Right */ },
{0x9c, 0x58 /* Num \n */},
{0xb5, 0x54 /* Num / */ },
{0xc7, 0x4a /* Home */ },
{0xc8, 0x52 /* Up */ },
{0xc9, 0x4e /* NPage */ },
{0xcb, 0x50 /* Left */ },
{0xcd, 0x4f /* Right */ },
{0xcf, 0x4d /* End */ },
{0xd0, 0x51 /* Down */ },
{0xd1, 0x4b /* PPage */ },
{0xd2, 0x49 /* Insert */},
{0xd3, 0x4c /* DC */ },
};
if (keycode >= ARRAY_SIZE (at_to_usb_map))
{
unsigned i;
for (i = 0; i < ARRAY_SIZE (at_to_usb_extended); i++)
if (at_to_usb_extended[i].from == keycode)
return at_to_usb_extended[i].to;
return 0;
}
return at_to_usb_map[keycode];
}
#endif

View file

@ -33,6 +33,19 @@ struct grub_keyboard_layout
grub_uint32_t keyboard_map_shift_l3[GRUB_KEYBOARD_LAYOUTS_ARRAY_SIZE]; grub_uint32_t keyboard_map_shift_l3[GRUB_KEYBOARD_LAYOUTS_ARRAY_SIZE];
}; };
unsigned EXPORT_FUNC(grub_term_map_key) (int code, int status); typedef enum grub_keyboard_key
{
GRUB_KEYBOARD_KEY_CAPS_LOCK = 0x39,
GRUB_KEYBOARD_KEY_SCROLL_LOCK = 0x47,
GRUB_KEYBOARD_KEY_NUM_LOCK = 0x53,
GRUB_KEYBOARD_KEY_LEFT_CTRL = 0xe0,
GRUB_KEYBOARD_KEY_LEFT_SHIFT = 0xe1,
GRUB_KEYBOARD_KEY_LEFT_ALT = 0xe2,
GRUB_KEYBOARD_KEY_RIGHT_CTRL = 0xe4,
GRUB_KEYBOARD_KEY_RIGHT_SHIFT = 0xe5,
GRUB_KEYBOARD_KEY_RIGHT_ALT = 0xe6,
} grub_keyboard_key_t;
unsigned EXPORT_FUNC(grub_term_map_key) (grub_keyboard_key_t code, int status);
#endif /* GRUB_KEYBOARD_LAYOUTS */ #endif /* GRUB_KEYBOARD_LAYOUTS */

View file

@ -23,10 +23,10 @@
#include <grub/misc.h> #include <grub/misc.h>
#include <grub/term.h> #include <grub/term.h>
#include <grub/keyboard_layouts.h> #include <grub/keyboard_layouts.h>
#include <grub/atkeymap.h>
static short at_keyboard_status = 0; static short at_keyboard_status = 0;
static int extended_pending = 0; static int e0_received = 0;
static int f0_received = 0;
static int pending_key = -1; static int pending_key = -1;
static grub_uint8_t led_status; static grub_uint8_t led_status;
@ -36,6 +36,145 @@ static grub_uint8_t led_status;
#define KEYBOARD_LED_CAPS (1 << 2) #define KEYBOARD_LED_CAPS (1 << 2)
static grub_uint8_t grub_keyboard_controller_orig; static grub_uint8_t grub_keyboard_controller_orig;
static grub_uint8_t grub_keyboard_orig_set;
static grub_uint8_t current_set;
static const grub_uint8_t set1_mapping[128] =
{
/* 0x00 */ 0x00 /* Unused */, 0x29 /* Escape */,
/* 0x02 */ 0x1e /* 1 */, 0x1f /* 2 */,
/* 0x04 */ 0x20 /* 3 */, 0x21 /* 4 */,
/* 0x06 */ 0x22 /* 5 */, 0x23 /* 6 */,
/* 0x08 */ 0x24 /* 7 */, 0x25 /* 8 */,
/* 0x0a */ 0x26 /* 9 */, 0x27 /* 0 */,
/* 0x0c */ 0x2d /* - */, 0x2e /* = */,
/* 0x0e */ 0x2a /* \b */, 0x2b /* \t */,
/* 0x10 */ 0x14 /* q */, 0x1a /* w */,
/* 0x12 */ 0x08 /* e */, 0x15 /* r */,
/* 0x14 */ 0x17 /* t */, 0x1c /* y */,
/* 0x16 */ 0x18 /* u */, 0x0c /* i */,
/* 0x18 */ 0x12 /* o */, 0x13 /* p */,
/* 0x1a */ 0x2f /* [ */, 0x30 /* ] */,
/* 0x1c */ 0x28 /* Enter */, 0xe0 /* Left CTRL */,
/* 0x1e */ 0x04 /* a */, 0x16 /* s */,
/* 0x20 */ 0x07 /* d */, 0x09 /* f */,
/* 0x22 */ 0x0a /* g */, 0x0b /* h */,
/* 0x24 */ 0x0d /* j */, 0x0e /* k */,
/* 0x26 */ 0x0f /* l */, 0x33 /* ; */,
/* 0x28 */ 0x34 /* " */, 0x35 /* ` */,
/* 0x2a */ 0xe1 /* Left Shift */, 0x32 /* \ */,
/* 0x2c */ 0x1d /* z */, 0x1b /* x */,
/* 0x2e */ 0x06 /* c */, 0x19 /* v */,
/* 0x30 */ 0x05 /* b */, 0x11 /* n */,
/* 0x32 */ 0x10 /* m */, 0x36 /* , */,
/* 0x34 */ 0x37 /* . */, 0x38 /* / */,
/* 0x36 */ 0xe5 /* Right Shift */, 0x55 /* Num * */,
/* 0x38 */ 0xe2 /* Left ALT */, 0x2c /* Space */,
/* 0x3a */ 0x39 /* Caps Lock */, 0x3a /* F1 */,
/* 0x3c */ 0x3b /* F2 */, 0x3c /* F3 */,
/* 0x3e */ 0x3d /* F4 */, 0x3e /* F5 */,
/* 0x40 */ 0x3f /* F6 */, 0x40 /* F7 */,
/* 0x42 */ 0x41 /* F8 */, 0x42 /* F9 */,
/* 0x44 */ 0x43 /* F10 */, 0x53 /* NumLock */,
/* 0x46 */ 0x47 /* Scroll Lock */, 0x5f /* Num 7 */,
/* 0x48 */ 0x60 /* Num 8 */, 0x61 /* Num 9 */,
/* 0x4a */ 0x56 /* Num - */, 0x5c /* Num 4 */,
/* 0x4c */ 0x5d /* Num 5 */, 0x5e /* Num 6 */,
/* 0x4e */ 0x57 /* Num + */, 0x59 /* Num 1 */,
/* 0x50 */ 0x5a /* Num 2 */, 0x5b /* Num 3 */,
/* 0x52 */ 0x62 /* Num 0 */, 0x63 /* Num . */,
/* 0x54 */ 0x00, 0x00,
/* 0x56 */ 0x64 /* 102nd key. */, 0x44 /* F11 */,
/* 0x58 */ 0x45 /* F12 */, 0x00,
/* 0x5a */ 0x00, 0x00,
/* 0x5c */ 0x00, 0x00,
/* 0x5e */ 0x00, 0x00,
/* 0x60 */ 0x00, 0x00,
/* 0x62 */ 0x00, 0x00,
/* OLPC keys. Just mapped to normal keys. */
/* 0x64 */ 0x00, 0x52 /* Up */,
/* 0x66 */ 0x51 /* Down */, 0x50 /* Left */,
/* 0x68 */ 0x4f /* Right */
};
static const struct
{
grub_uint8_t from, to;
} set1_e0_mapping[] =
{
{0x1c, 0x58 /* Num \n */},
{0x1d, 0xe4 /* Right CTRL */},
{0x35, 0x54 /* Num / */ },
{0x38, 0xe6 /* Right ALT */},
{0x47, 0x4a /* Home */ },
{0x48, 0x52 /* Up */ },
{0x49, 0x4e /* NPage */ },
{0x4b, 0x50 /* Left */ },
{0x4d, 0x4f /* Right */ },
{0x4f, 0x4d /* End */ },
{0x50, 0x51 /* Down */ },
{0x51, 0x4b /* PPage */ },
{0x52, 0x49 /* Insert */},
{0x53, 0x4c /* DC */ },
};
static const grub_uint8_t set2_mapping[256] =
{
/* 0x00 */ 0x00, 0x42 /* F9 */, 0x00, 0x3e /* F5 */,
/* 0x04 */ 0x3c /* F3 */, 0x3a /* F1 */, 0x3b /* F2 */, 0x45 /* F12 */,
/* 0x08 */ 0x00, 0x43 /* F10 */, 0x41 /* F8 */, 0x3f /* F6 */,
/* 0x0c */ 0x3d /* F4 */, 0x2b /* \t */, 0x35 /* ` */, 0x00,
/* 0x10 */ 0x00, GRUB_KEYBOARD_KEY_LEFT_ALT, GRUB_KEYBOARD_KEY_LEFT_SHIFT, 0x00,
/* 0x14 */ GRUB_KEYBOARD_KEY_LEFT_CTRL, 0x14 /* q */, 0x1e /* 1 */, 0x00,
/* 0x18 */ 0x00, 0x00, 0x1d /* s */, 0x16 /* s */,
/* 0x1c */ 0x04 /* a */, 0x1a /* w */, 0x1f /* 2 */, 0x00,
/* 0x20 */ 0x00, 0x06 /* c */, 0x1b /* x */, 0x07 /* d */,
/* 0x24 */ 0x08 /* e */, 0x21 /* 4 */, 0x20 /* 3 */, 0x00,
/* 0x28 */ 0x00, 0x2c /* Space */, 0x19 /* v */, 0x09 /* f */,
/* 0x2c */ 0x17 /* t */, 0x15 /* r */, 0x22 /* 5 */, 0x00,
/* 0x30 */ 0x00, 0x11 /* n */, 0x05 /* b */, 0x0b /* h */,
/* 0x34 */ 0x0a /* g */, 0x1c /* y */, 0x23 /* 6 */, 0x00,
/* 0x38 */ 0x00, 0x00, 0x10 /* m */, 0x0d /* j */,
/* 0x3c */ 0x18 /* u */, 0x24 /* 7 */, 0x25 /* 8 */, 0x00,
/* 0x40 */ 0x00, 0x37 /* . */, 0x0e /* k */, 0x0c /* i */,
/* 0x44 */ 0x12 /* o */, 0x27 /* 0 */, 0x26 /* 9 */, 0x00,
/* 0x48 */ 0x00, 0x36 /* , */, 0x38 /* / */, 0x0f /* l */,
/* 0x4c */ 0x33 /* ; */, 0x13 /* p */, 0x2d /* - */, 0x00,
/* 0x50 */ 0x00, 0x00, 0x34 /* ' */, 0x00,
/* 0x54 */ 0x2f /* [ */, 0x2e /* = */, 0x00, 0x00,
/* 0x58 */ GRUB_KEYBOARD_KEY_CAPS_LOCK, GRUB_KEYBOARD_KEY_RIGHT_SHIFT, 0x28 /* \n */,0x30 /* ] */,
/* 0x5c */ 0x00, 0x32 /* \ */, 0x00, 0x00,
/* 0x60 */ 0x00, 0x64 /* 102nd key. */, 0x00, 0x00,
/* 0x64 */ 0x00, 0x00, 0x2a /* \b */, 0x00,
/* 0x68 */ 0x00, 0x59 /* Num 1 */, 0x00, 0x5c /* Num 4 */,
/* 0x6c */ 0x5f /* Num 7 */, 0x00, 0x00, 0x00,
/* 0x70 */ 0x62 /* Num 0 */, 0x63 /* Num 0 */, 0x5a /* Num 2 */, 0x5d /* Num 5 */,
/* 0x74 */ 0x5e /* Num 6 */, 0x60 /* Num 8 */, 0x29 /* \e */, 0x53 /* NumLock */,
/* 0x78 */ 0x44 /* F11 */, 0x57 /* Num + */, 0x5b /* Num 3 */, 0x56 /* Num - */,
/* 0x7c */ 0x55 /* Num * */, 0x61 /* Num 9 */, 0x47 /* ScrollLock */, 0x00,
/* 0x80 */ 0x00, 0x00, 0x00, 0x40 /* F7 */,
};
static const struct
{
grub_uint8_t from, to;
} set2_e0_mapping[] =
{
{0x11, GRUB_KEYBOARD_KEY_RIGHT_ALT},
{0x14, GRUB_KEYBOARD_KEY_RIGHT_CTRL},
{0x4a, 0x54}, /* Num / */
{0x5a, 0x58}, /* Num enter */
{0x69, 0x4d}, /* End */
{0x6b, 0x50}, /* Left */
{0x6c, 0x4a}, /* Home */
{0x70, 0x49}, /* Insert */
{0x71, 0x4c}, /* Delete */
{0x72, 0x51}, /* Down */
{0x74, 0x4f}, /* Right */
{0x75, 0x52}, /* Up */
{0x7a, 0x4e}, /* PageDown */
{0x7d, 0x4b}, /* PageUp */
};
static void static void
keyboard_controller_wait_until_ready (void) keyboard_controller_wait_until_ready (void)
@ -51,6 +190,55 @@ grub_keyboard_controller_write (grub_uint8_t c)
grub_outb (c, KEYBOARD_REG_DATA); grub_outb (c, KEYBOARD_REG_DATA);
} }
static grub_uint8_t
query_mode (int mode)
{
keyboard_controller_wait_until_ready ();
grub_outb (0xf0, KEYBOARD_REG_DATA);
keyboard_controller_wait_until_ready ();
grub_inb (KEYBOARD_REG_DATA);
keyboard_controller_wait_until_ready ();
grub_outb (mode, KEYBOARD_REG_DATA);
keyboard_controller_wait_until_ready ();
return grub_inb (KEYBOARD_REG_DATA);
}
/* QEMU translates the set even in no-translate mode. */
static inline int
recover_mode (grub_uint8_t report)
{
if (report == 0x43 || report == 1)
return 1;
if (report == 0x41 || report == 2)
return 2;
if (report == 0x3f || report == 3)
return 3;
return -1;
}
static void
set_scancodes (void)
{
grub_keyboard_controller_write (grub_keyboard_controller_orig
& ~KEYBOARD_AT_TRANSLATE);
grub_keyboard_orig_set = recover_mode (query_mode (0));
query_mode (2);
current_set = query_mode (0);
current_set = recover_mode (current_set);
if (current_set == 2)
return;
query_mode (1);
current_set = query_mode (0);
current_set = recover_mode (current_set);
if (current_set == 1)
return;
grub_printf ("No supported scancode set found\n");
}
static grub_uint8_t static grub_uint8_t
grub_keyboard_controller_read (void) grub_keyboard_controller_read (void)
{ {
@ -68,48 +256,126 @@ keyboard_controller_led (grub_uint8_t leds)
grub_outb (leds & 0x7, KEYBOARD_REG_DATA); grub_outb (leds & 0x7, KEYBOARD_REG_DATA);
} }
static int
fetch_key (int *is_break)
{
int was_ext = 0;
grub_uint8_t at_key;
int ret = 0;
if (! KEYBOARD_ISREADY (grub_inb (KEYBOARD_REG_STATUS)))
return -1;
at_key = grub_inb (KEYBOARD_REG_DATA);
if (at_key == 0xe0)
{
e0_received = 1;
return -1;
}
was_ext = e0_received;
e0_received = 0;
switch (current_set)
{
case 1:
*is_break = !!(at_key & 0x80);
if (!was_ext)
ret = set1_mapping[at_key & 0x7f];
else
{
unsigned i;
for (i = 0; i < ARRAY_SIZE (set1_e0_mapping); i++)
if (set1_e0_mapping[i].from == (at_key & 0x80))
{
ret = set1_e0_mapping[i].to;
break;
}
}
break;
case 2:
if (at_key == 0xf0)
{
f0_received = 1;
return -1;
}
*is_break = f0_received;
f0_received = 0;
if (!was_ext)
ret = set2_mapping[at_key];
else
{
unsigned i;
for (i = 0; i < ARRAY_SIZE (set1_e0_mapping); i++)
if (set1_e0_mapping[i].from == (at_key & 0x80))
{
ret = set1_e0_mapping[i].to;
break;
}
}
break;
default:
return -1;
}
if (!ret)
{
grub_printf ("Unknown key 0x%02x from set %d\n\n", at_key, current_set);
return -1;
}
return ret;
}
/* FIXME: This should become an interrupt service routine. For now /* FIXME: This should become an interrupt service routine. For now
it's just used to catch events from control keys. */ it's just used to catch events from control keys. */
static void static int
grub_keyboard_isr (grub_uint8_t key) grub_keyboard_isr (grub_keyboard_key_t key, int is_break)
{ {
if (KEYBOARD_ISMAKE (key)) if (!is_break)
switch (KEYBOARD_SCANCODE (key)) switch (key)
{ {
case SHIFT_L: case GRUB_KEYBOARD_KEY_LEFT_SHIFT:
at_keyboard_status |= GRUB_TERM_STATUS_LSHIFT; at_keyboard_status |= GRUB_TERM_STATUS_LSHIFT;
break; return 1;
case SHIFT_R: case GRUB_KEYBOARD_KEY_RIGHT_SHIFT:
at_keyboard_status |= GRUB_TERM_STATUS_RSHIFT; at_keyboard_status |= GRUB_TERM_STATUS_RSHIFT;
break; return 1;
case CTRL: case GRUB_KEYBOARD_KEY_LEFT_CTRL:
at_keyboard_status |= GRUB_TERM_STATUS_LCTRL; at_keyboard_status |= GRUB_TERM_STATUS_LCTRL;
break; return 1;
case ALT: case GRUB_KEYBOARD_KEY_RIGHT_CTRL:
if (extended_pending) at_keyboard_status |= GRUB_TERM_STATUS_RCTRL;
at_keyboard_status |= GRUB_TERM_STATUS_RALT; return 1;
else case GRUB_KEYBOARD_KEY_RIGHT_ALT:
at_keyboard_status |= GRUB_TERM_STATUS_LALT; at_keyboard_status |= GRUB_TERM_STATUS_RALT;
break; return 1;
case GRUB_KEYBOARD_KEY_LEFT_ALT:
at_keyboard_status |= GRUB_TERM_STATUS_LALT;
return 1;
default:
return 0;
} }
else else
switch (KEYBOARD_SCANCODE (key)) switch (KEYBOARD_SCANCODE (key))
{ {
case SHIFT_L: case GRUB_KEYBOARD_KEY_LEFT_SHIFT:
at_keyboard_status &= ~GRUB_TERM_STATUS_LSHIFT; at_keyboard_status &= ~GRUB_TERM_STATUS_LSHIFT;
break; return 1;
case SHIFT_R: case GRUB_KEYBOARD_KEY_RIGHT_SHIFT:
at_keyboard_status &= ~GRUB_TERM_STATUS_RSHIFT; at_keyboard_status &= ~GRUB_TERM_STATUS_RSHIFT;
break; return 1;
case CTRL: case GRUB_KEYBOARD_KEY_LEFT_CTRL:
at_keyboard_status &= ~GRUB_TERM_STATUS_LCTRL; at_keyboard_status &= ~GRUB_TERM_STATUS_LCTRL;
break; return 1;
case ALT: case GRUB_KEYBOARD_KEY_RIGHT_CTRL:
if (extended_pending) at_keyboard_status &= ~GRUB_TERM_STATUS_RCTRL;
at_keyboard_status &= ~GRUB_TERM_STATUS_RALT; return 1;
else case GRUB_KEYBOARD_KEY_RIGHT_ALT:
at_keyboard_status &= ~GRUB_TERM_STATUS_LALT; at_keyboard_status &= ~GRUB_TERM_STATUS_RALT;
break; return 1;
case GRUB_KEYBOARD_KEY_LEFT_ALT:
at_keyboard_status &= ~GRUB_TERM_STATUS_LALT;
return 1;
default:
return 0;
} }
} }
@ -117,31 +383,19 @@ grub_keyboard_isr (grub_uint8_t key)
static int static int
grub_keyboard_getkey (void) grub_keyboard_getkey (void)
{ {
grub_uint8_t key; int key;
grub_uint8_t ret; int is_break;
if (! KEYBOARD_ISREADY (grub_inb (KEYBOARD_REG_STATUS)))
return -1;
key = grub_inb (KEYBOARD_REG_DATA);
if (key == 0xe0)
{
extended_pending = 1;
return -1;
}
/* FIXME */ grub_keyboard_isr (key);
ret = KEYBOARD_SCANCODE (key);
if (ret == SHIFT_L || ret == SHIFT_R || ret == ALT || ret == CTRL)
{
extended_pending = 0;
return -1;
}
if (extended_pending)
ret |= 0x80;
extended_pending = 0;
if (! KEYBOARD_ISMAKE (key))
return -1;
return ret;
}
key = fetch_key (&is_break);
if (key == -1)
return -1;
if (grub_keyboard_isr (key, is_break))
return -1;
if (is_break)
return -1;
return key;
}
/* If there is a character pending, return it; otherwise return -1. */ /* If there is a character pending, return it; otherwise return -1. */
static int static int
@ -156,7 +410,7 @@ grub_at_keyboard_getkey_noblock (void)
#endif #endif
switch (code) switch (code)
{ {
case CAPS_LOCK: case GRUB_KEYBOARD_KEY_CAPS_LOCK:
at_keyboard_status ^= GRUB_TERM_STATUS_CAPS; at_keyboard_status ^= GRUB_TERM_STATUS_CAPS;
led_status ^= KEYBOARD_LED_CAPS; led_status ^= KEYBOARD_LED_CAPS;
keyboard_controller_led (led_status); keyboard_controller_led (led_status);
@ -165,7 +419,7 @@ grub_at_keyboard_getkey_noblock (void)
grub_dprintf ("atkeyb", "caps_lock = %d\n", !!(at_keyboard_status & KEYBOARD_STATUS_CAPS_LOCK)); grub_dprintf ("atkeyb", "caps_lock = %d\n", !!(at_keyboard_status & KEYBOARD_STATUS_CAPS_LOCK));
#endif #endif
return -1; return -1;
case NUM_LOCK: case GRUB_KEYBOARD_KEY_NUM_LOCK:
at_keyboard_status ^= GRUB_TERM_STATUS_NUM; at_keyboard_status ^= GRUB_TERM_STATUS_NUM;
led_status ^= KEYBOARD_LED_NUM; led_status ^= KEYBOARD_LED_NUM;
keyboard_controller_led (led_status); keyboard_controller_led (led_status);
@ -174,14 +428,13 @@ grub_at_keyboard_getkey_noblock (void)
grub_dprintf ("atkeyb", "num_lock = %d\n", !!(at_keyboard_status & KEYBOARD_STATUS_NUM_LOCK)); grub_dprintf ("atkeyb", "num_lock = %d\n", !!(at_keyboard_status & KEYBOARD_STATUS_NUM_LOCK));
#endif #endif
return -1; return -1;
case SCROLL_LOCK: case GRUB_KEYBOARD_KEY_SCROLL_LOCK:
at_keyboard_status ^= GRUB_TERM_STATUS_SCROLL; at_keyboard_status ^= GRUB_TERM_STATUS_SCROLL;
led_status ^= KEYBOARD_LED_SCROLL; led_status ^= KEYBOARD_LED_SCROLL;
keyboard_controller_led (led_status); keyboard_controller_led (led_status);
return -1; return -1;
default: default:
return grub_term_map_key (grub_at_map_to_usb (code), return grub_term_map_key (code, at_keyboard_status);
at_keyboard_status);
} }
} }
@ -222,7 +475,7 @@ grub_keyboard_controller_init (struct grub_term_input *term __attribute__ ((unus
pending_key = -1; pending_key = -1;
at_keyboard_status = 0; at_keyboard_status = 0;
grub_keyboard_controller_orig = grub_keyboard_controller_read (); grub_keyboard_controller_orig = grub_keyboard_controller_read ();
grub_keyboard_controller_write (grub_keyboard_controller_orig | KEYBOARD_SCANCODE_SET1); set_scancodes ();
keyboard_controller_led (led_status); keyboard_controller_led (led_status);
/* Drain input buffer. */ /* Drain input buffer. */
while (KEYBOARD_ISREADY (grub_inb (KEYBOARD_REG_STATUS))) while (KEYBOARD_ISREADY (grub_inb (KEYBOARD_REG_STATUS)))
@ -234,6 +487,7 @@ grub_keyboard_controller_init (struct grub_term_input *term __attribute__ ((unus
static grub_err_t static grub_err_t
grub_keyboard_controller_fini (struct grub_term_input *term __attribute__ ((unused))) grub_keyboard_controller_fini (struct grub_term_input *term __attribute__ ((unused)))
{ {
query_mode (grub_keyboard_orig_set);
grub_keyboard_controller_write (grub_keyboard_controller_orig); grub_keyboard_controller_write (grub_keyboard_controller_orig);
return GRUB_ERR_NONE; return GRUB_ERR_NONE;
} }

View file

@ -20,7 +20,6 @@
#include <grub/i18n.h> #include <grub/i18n.h>
#include <grub/term.h> #include <grub/term.h>
#include <grub/keyboard_layouts.h> #include <grub/keyboard_layouts.h>
#include <grub/atkeymap.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -192,6 +191,65 @@ static struct console_grub_equivalence console_grub_equivalences_common[] = {
{NULL, '\0'} {NULL, '\0'}
}; };
static grub_uint8_t linux_to_usb_map[128] = {
/* 0x00 */ 0x00 /* Unused */, 0x29 /* Escape */,
/* 0x02 */ 0x1e /* 1 */, 0x1f /* 2 */,
/* 0x04 */ 0x20 /* 3 */, 0x21 /* 4 */,
/* 0x06 */ 0x22 /* 5 */, 0x23 /* 6 */,
/* 0x08 */ 0x24 /* 7 */, 0x25 /* 8 */,
/* 0x0a */ 0x26 /* 9 */, 0x27 /* 0 */,
/* 0x0c */ 0x2d /* - */, 0x2e /* = */,
/* 0x0e */ 0x2a /* \b */, 0x2b /* \t */,
/* 0x10 */ 0x14 /* q */, 0x1a /* w */,
/* 0x12 */ 0x08 /* e */, 0x15 /* r */,
/* 0x14 */ 0x17 /* t */, 0x1c /* y */,
/* 0x16 */ 0x18 /* u */, 0x0c /* i */,
/* 0x18 */ 0x12 /* o */, 0x13 /* p */,
/* 0x1a */ 0x2f /* [ */, 0x30 /* ] */,
/* 0x1c */ 0x28 /* Enter */, 0x00 /* Left CTRL */,
/* 0x1e */ 0x04 /* a */, 0x16 /* s */,
/* 0x20 */ 0x07 /* d */, 0x09 /* f */,
/* 0x22 */ 0x0a /* g */, 0x0b /* h */,
/* 0x24 */ 0x0d /* j */, 0x0e /* k */,
/* 0x26 */ 0x0f /* l */, 0x33 /* ; */,
/* 0x28 */ 0x34 /* " */, 0x35 /* ` */,
/* 0x2a */ 0x00 /* Left Shift */, 0x32 /* \ */,
/* 0x2c */ 0x1d /* z */, 0x1b /* x */,
/* 0x2e */ 0x06 /* c */, 0x19 /* v */,
/* 0x30 */ 0x05 /* b */, 0x11 /* n */,
/* 0x32 */ 0x10 /* m */, 0x36 /* , */,
/* 0x34 */ 0x37 /* . */, 0x38 /* / */,
/* 0x36 */ 0x00 /* Right Shift */, 0x55 /* Num * */,
/* 0x38 */ 0x00 /* Left ALT */, 0x2c /* Space */,
/* 0x3a */ 0x39 /* Caps Lock */, 0x3a /* F1 */,
/* 0x3c */ 0x3b /* F2 */, 0x3c /* F3 */,
/* 0x3e */ 0x3d /* F4 */, 0x3e /* F5 */,
/* 0x40 */ 0x3f /* F6 */, 0x40 /* F7 */,
/* 0x42 */ 0x41 /* F8 */, 0x42 /* F9 */,
/* 0x44 */ 0x43 /* F10 */, 0x53 /* NumLock */,
/* 0x46 */ 0x47 /* Scroll Lock */, 0x5f /* Num 7 */,
/* 0x48 */ 0x60 /* Num 8 */, 0x61 /* Num 9 */,
/* 0x4a */ 0x56 /* Num - */, 0x5c /* Num 4 */,
/* 0x4c */ 0x5d /* Num 5 */, 0x5e /* Num 6 */,
/* 0x4e */ 0x57 /* Num + */, 0x59 /* Num 1 */,
/* 0x50 */ 0x5a /* Num 2 */, 0x5b /* Num 3 */,
/* 0x52 */ 0x62 /* Num 0 */, 0x63 /* Num . */,
/* 0x54 */ 0x00, 0x00,
/* 0x56 */ 0x64 /* 102nd key. */, 0x44 /* F11 */,
/* 0x58 */ 0x45 /* F12 */, 0x00,
/* 0x5a */ 0x00, 0x00,
/* 0x5c */ 0x00, 0x00,
/* 0x5e */ 0x00, 0x00,
/* 0x60 */ 0x58 /* Num \n */, 0x00 /* Right CTRL */,
/* 0x62 */ 0x54 /* Num / */, 0x00,
/* 0x64 */ 0x00 /* Right ALT */, 0x00,
/* 0x66 */ 0x4a /* Home */, 0x52 /* Up */,
/* 0x68 */ 0x4e /* NPage */, 0x50 /* Left */,
/* 0x6a */ 0x4f /* Right */, 0x4d /* End */,
/* 0x6c */ 0x51 /* Down */, 0x4b /* PPage */,
/* 0x6e */ 0x49 /* Insert */, 0x4c /* DC */
};
static void static void
usage (int status) usage (int status)
{ {
@ -298,50 +356,40 @@ write_keymaps (FILE *in, FILE *out)
{ {
if (strncmp (line, "keycode", sizeof ("keycode") - 1) == 0) if (strncmp (line, "keycode", sizeof ("keycode") - 1) == 0)
{ {
unsigned keycode_at, orig; unsigned keycode_linux;
unsigned keycode_usb; unsigned keycode_usb;
char normal[64]; char normal[64];
char shift[64]; char shift[64];
char normalalt[64]; char normalalt[64];
char shiftalt[64]; char shiftalt[64];
static grub_uint8_t e0_remap[] = {
0x9c /* Num \n */, 0x9d /* Right CTRL */, 0xb5 /* Num / */,
0, 0xb8 /* Right ALT */, 0,
0xc7 /* Home */, 0xc8 /* Up */, 0xc9 /* NPage*/, 0xcb /* Left */,
0xcd /* Right */, 0xcf /* End */, 0xd0 /* Down */, 0xd1 /* PPage */,
0xd2 /* Insert */, 0xd3 /* Delete */
};
sscanf (line, "keycode %u = %60s %60s %60s %60s", &keycode_at, sscanf (line, "keycode %u = %60s %60s %60s %60s", &keycode_linux,
normal, shift, normalalt, shiftalt); normal, shift, normalalt, shiftalt);
orig = keycode_at;
/* Not used. */ /* Not used. */
if (keycode_at == 0x77 /* Pause */ if (keycode_linux == 0x77 /* Pause */
/* Some obscure keys */ /* Some obscure keys */
|| keycode_at == 0x63 || keycode_at == 0x7d || keycode_at == 0x7e) || keycode_linux == 0x63 || keycode_linux == 0x7d
|| keycode_linux == 0x7e)
continue; continue;
if (keycode_at >= 96 && keycode_at < 96 + ARRAY_SIZE (e0_remap))
keycode_at = e0_remap[keycode_at - 96];
/* Not remappable. */ /* Not remappable. */
if (keycode_at == 0x1d /* Left CTRL */ if (keycode_linux == 0x1d /* Left CTRL */
|| keycode_at == 0x9d /* Right CTRL */ || keycode_linux == 0x9d /* Right CTRL */
|| keycode_at == 0x2a /* Left Shift. */ || keycode_linux == 0x2a /* Left Shift. */
|| keycode_at == 0x36 /* Right Shift. */ || keycode_linux == 0x36 /* Right Shift. */
|| keycode_at == 0x38 /* Left ALT. */ || keycode_linux == 0x38 /* Left ALT. */
|| keycode_at == 0xb8 /* Right ALT. */ || keycode_linux == 0xb8 /* Right ALT. */
|| keycode_at == 0x3a /* CapsLock. */ || keycode_linux == 0x3a /* CapsLock. */
|| keycode_at == 0x45 /* NumLock. */ || keycode_linux == 0x45 /* NumLock. */
|| keycode_at == 0x46 /* ScrollLock. */) || keycode_linux == 0x46 /* ScrollLock. */)
continue; continue;
keycode_usb = grub_at_map_to_usb (keycode_at); keycode_usb = linux_to_usb_map[keycode_linux];
if (keycode_usb == 0 if (keycode_usb == 0
|| keycode_usb >= GRUB_KEYBOARD_LAYOUTS_ARRAY_SIZE) || keycode_usb >= GRUB_KEYBOARD_LAYOUTS_ARRAY_SIZE)
{ {
fprintf (stderr, "Unknown keycode 0x%02x\n", orig); fprintf (stderr, "Unknown keycode 0x%02x\n", keycode_linux);
continue; continue;
} }
if (keycode_usb < GRUB_KEYBOARD_LAYOUTS_ARRAY_SIZE) if (keycode_usb < GRUB_KEYBOARD_LAYOUTS_ARRAY_SIZE)