在保护模式下读取键盘 [英] Read the keyboard in protected mode

查看:28
本文介绍了在保护模式下读取键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一个 PS/2 键盘控制器,但我无法让它工作.

I'm trying to do a PS/2 keyboard controller, and I can't get it work.

outb(0x60, 0xED);
outb(0x60, 2); /* Turn on CapsLock LED (doesn't works)*/
char c = 0;
while (c != 1)
{
    if (inb(0x60) != c)
    {
        c = inb(0x60);
        if (c > 0)
            putch(scan2char(c));
    }
}

scan2char 函数:

char scan2char(char scn)
{
    char keych;
    switch (scn)
    {
        case 0x15:
            keych = 'q';
            break;
        case 0x1D:
            keych = 'w';
            break;
        case 0x24:
            keych = 'e';
            break;
        case 0x2D:
            keych = 'r';
            break;
        case 0x2C:
            keych = 't';
            break;
        case 0x35:
            keych = 'y';
            break;
    }
    return keych;
}

它读取按键,但键盘布局以一种奇怪的方式移动;例如:如果我按 9 我得到 q,如果我按 0 我得到 w,你明白.一开始我还以为是putch函数的问题,但是做了一些测试,发现不是putch的问题.

It reads the keys, but the keyboard layout shifted in an weird way; e.x: if I press 9 I get q, if I press 0 I get w, you understand. At first I thought it might be the putch function, but doing some tests, I saw that putch wasn't the problem.

推荐答案

涉及 2 个或更多独立的硬件:

There's 2 or more separate pieces of hardware involved:

  • PS/2 控制器(主要只是一个美化的串行端口控制器)
  • 插入第一个 PS/2 端口(键盘、鼠标、触摸屏、条形码扫描仪等)的任何东西
  • 无论发生什么情况插入第二个 PS/2 端口

当涉及不同的硬件时,最好有单独的驱动程序:

When there's separate pieces of hardware involved it's best to have separate drivers:

  • 处理 PS/2 控制器的东西;包括处理热插入设备"、设备识别和为识别的设备启动适当的驱动程序;并包括提供get_byte()/send_byte()"接口供其他驱动程序使用.

  • something that handles the PS/2 controller; including handling "hot-insert device", device identification and starting the appropriate driver for the identified device; and including providing a "get_byte()/send_byte()" interface for other drivers to use.

可能插入的各种设备的驱动程序(键盘、鼠标、触摸屏、条码扫描仪等);它不接触任何 PS/2 控制器的 IO 端口,仅与 PS/2 控制器驱动程序通信(通过get_byte()/send_byte()" 由 PS/2 控制器驱动程序提供的接口).

a driver for each kind of device that might be plugged in (keyboard, mouse, touch screen, bar-code scanner, ...); which doesn't touch any of the PS/2 controller's IO ports and only communicates with the PS/2 controller driver (via. the "get_byte()/send_byte()" interface provided by the PS/2 controller driver).

请注意(通常,不包括笔记本电脑中的硬连线设备"场景)绝对没有理由不能插入两个 PS/2 键盘(有 2 个完全相同的PS/2 键盘驱动程序"运行);或两个 PS/2 鼠标,或一个条码扫描器和一个触摸屏(无键盘和鼠标),或任何 PS/2 端口中任何类型设备的任何其他组合.此外(如果你喜欢便携性)没有理由为什么相同的 PS/2 键盘/鼠标/任何设备驱动程序不能在完全不同的 PS/2 控制器的完全不同的体系结构上按原样"(重新编译,仅此而已)(例如某些ARM系统的PL050 PS/2控制器),仅仅是因为完全不同的PS/2控制器的驱动程序可以提供完全相同的get_byte()/send_byte()"界面.

Note that (in general, excluding "hard-wired device" scenarios in laptops) there is absolutely no reason why you can't have two PS/2 keyboards plugged in (with 2 separate instances of the exact same "PS/2 keyboard driver" running); or two PS/2 mouses, or a bar code scanner and a touch screen (with no keyboard and no mouse), or any other combination of any types of devices in any PS/2 ports. Also (if you like portability) there's no reason why the same PS/2 keyboard/mouse/whatever device drivers can't work "as is" (with a recompile and nothing more) on completely different architectures with completely different PS/2 controllers (e.g. the PL050 PS/2 controller that some ARM systems have), simply because the driver for the completely different PS/2 controller can provide the exact same "get_byte()/send_byte()" interface.

对于 80x86 PC 上的8042"PS/2 控制器;您可能应该阅读(如果不遵循)此处描述的初始化序列:https://wiki.osdev.org/%228042%22_PS/2_Controller#Initialising_the_PS.2F2_Controller

For the "8042" PS/2 controller on 80x86 PCs; you should probably read (if not follow) the initialisation sequence described here: https://wiki.osdev.org/%228042%22_PS/2_Controller#Initialising_the_PS.2F2_Controller

如果你没有正确地初始化 PS/2 控制器(例如,只是使用随机的任何状态,引导加载程序留下的东西")那么它可能会留下一个糟糕的翻译功能,其中数据来自该设备被控制器故意破坏,以向后兼容原始 IBM XT 机器(在存在扫描代码集 2 之前);现代键盘为扫描代码集 2 发送字节,但 PS/2 控制器将它们转换为扫描代码集 1 兼容"值,导致诸如(例如)如果你按 9 你得到 q,如果你按 0 你得到 w,...".

If you don't initialise the PS/2 controller properly (e.g. just use "random whatever state the thing happened to be left in by boot loader") then it may have been left with an awful translation feature enabled where data from the device is deliberately mangled by the controller for backward compatibility with the original IBM XT machines (from before scan code set 2 existed); where a modern keyboard sends bytes for scan code set 2, but the PS/2 controller converts them into "scan code set 1 compatible" values, causing things like (e.g.) "if you press 9 you get q, if you press 0 you get w, ...".

这篇关于在保护模式下读取键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆