Keyboard scancode set question

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
NOTNULL

Keyboard scancode set question

Post by NOTNULL »

Hi all,
I wrote 0xF0 followed by 0x00 to the port 0x60 and when I read the value from 0x60, I received the number '2'. But, I read in the article linked from the OS FAQ as:
Writing 0xf0 followed by 0 queries the mode, resulting in a scancode byte 43, 41 or 3f from the keyboard.
Does this mean what I did?

Thanks.
bluecode

Re:Keyboard scancode set question

Post by bluecode »

hi,

if you read 0x02 from the keyboard, this is correct. Valid scancode set id's are 0x01, 0x02, 0x03. I think the faq is wrong. I'll look it up...
btw: where does this stand?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:Keyboard scancode set question

Post by Brendan »

Hi,
NOTNULL wrote: I wrote 0xF0 followed by 0x00 to the port 0x60 and when I read the value from 0x60, I received the number '2'. But, I read in the article linked from the OS FAQ as:
Writing 0xf0 followed by 0 queries the mode, resulting in a scancode byte 43, 41 or 3f from the keyboard.
Does this mean what I did?
After the keyboard receives the 0xF0, it sends an "ACK" and then waits for the next byte. If the next byte is "0x00" then the keyboard itself will send 0x01, 0x02 or 0x03 (corresponding to scan code set 1, scan code set 2 or scan code set 3).

Before your code gets this data it goes through the keyboard controller chip inside the computer. Normally, this keyboard controller chip will convert data from the keyboard into "scan code set 1" compatible values. In this case, 0x01 will be converted to 0x43, 0x02 becomes 0x41 and 0x03 becomes 0x3F.

This leads me to one of 3 possibilities:
  • 1. You're code is buggy
    2. You're code is working perfectly, but you've disabled the default scan code translation that the keyboard controller chip does by default
    3. You've got the keyboard plugged in where the mouse would normally go (where the keyboard controller chip can't do any translation).
I like option 2, because I like to disable the scancode translation, but I don't know enough about your code to be sure.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Keyboard scancode set question

Post by Pype.Clicker »

i'd like to insist on the fact that, except if you have good reasons to do this, you shouldn't change the scancode used by the 8042. The reason is your chipset is smart enough to translate the newer scancode sets into the "usual" scancode set, which itself contains all the keys you might wish to use.

The faq is http://www.osdev.org/osfaq2/.

May i ask you what page you're referring to ?
NOTNULL

Re:Keyboard scancode set question

Post by NOTNULL »

Pype.Clicker wrote: i'd like to insist on the fact that, except if you have good reasons to do this, you shouldn't change the scancode used by the 8042. The reason is your chipset is smart enough to translate the newer scancode sets into the "usual" scancode set, which itself contains all the keys you might wish to use.
I have not changed the default scancode of 8042. I just tried to get the default scan code used by 8042.

Here is my code:

Code: Select all

outportb (0x60, 0xF0);
outportb (0x60, 0x00);

scode_set = inportb (0x60);
putint (scode_set);              /* Here where, I got 2 */
Pype.Clicker wrote: May i ask you what page you're referring to ?

Yes, sure. http://www.win.tue.nl/~aeb/linux/kbd/scancodes.html, linked from http://www.osdev.org/osfaq2/index.php/Getting%20Keyboard%20Input.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:Keyboard scancode set question

Post by Brendan »

Hi,
NOTNULL wrote:Here is my code:

Code: Select all

outportb (0x60, 0xF0);
outportb (0x60, 0x00);

scode_set = inportb (0x60);
putint (scode_set);              /* Here where, I got 2 */
That'd be the problem then - it takes time for data to be sent (one bit at a time) from the keyboard controller to the keyboard itself, and even more time for a response to come back. Also, the first byte you get back will be an "ACK" not the scan code set.

This means you need to wait for the keyboard controller to become ready before you send a byte, and give the keyboard time to send a reply. You're doing none of this, so the 0x02 you read was probably left behind from the last key pressed or something.

For waiting until the controller is ready, there's a "status" port containing flags that must be checked. For example:

Code: Select all

%define STATUSPORT   0x64
%define COMMANDPORT   0x64
%define DATAPORT   0x60

;Status Port Bits

%define PS2controllerOutputFull      0x01
%define PS2controllerOutputBfull   0x20
%define PS2controllerInputFull      0x02


sendByte:
    push eax
.sd1:
    in al,STATUSPORT
    test al,PS2controllerInputFull
    je .sd2
    pop eax
    out DATAPORT,al
    clc
    ret

getByte:
.gb1:
    in al,STATUSPORT
    test al,PS2controllerOutputFull
    jne .gb2
    in al,DATAPORT
    clc
    ret
Of course for these you'd also want to add time-outs, so that if no keybord is present (or if there's a hardware fault) it doesn't loop forever.

Once you've got these, then reading the scancode set would go something like:

Code: Select all

getScanCodeSet:
    mov al,0xF0
    call sendByte
    jc timeOutError

    mov al,0x00
    call sendByte
    jc timeOutError

    call getByte
    jc timeOutError
    cmp al,0xFA      ;Is it an "ACK"?
    jne unknownError

    call getByte        ;AL = Scan Code Set
    jc timeOutError

    ret

Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
NOTNULL

Re:Keyboard scancode set question

Post by NOTNULL »

Thanks a lot Brendan.
Post Reply