2

I would like to get my keyboard current scan code set. Code that I am using looks more or less like this:

#define STATUSPORT 64h
#define DATAPORT 60h
#define PS2controllerOutputFull 0x20
#define PS2controllerInputFull 2h

void sendByte()
{
    __asm
    {
        push eax
    repeat :
        in al, STATUSPORT
        test al, PS2controllerInputFull
        jne repeat
        pop eax
        out DATAPORT, al
        clc
    }
}

UCHAR getByte()
{
    __asm
    {
    repeat:
        in al, STATUSPORT
        test al, PS2controllerOutputFull
        jne repeat
        in al, DATAPORT
        clc
    }
}

UCHAR getScanCodeSet()
{
    __asm
    {
        mov al, 0xF0
        call sendByte

        mov al, 0x00
        call sendByte

        call getByte
        cmp al, 0xFA
        jne unknownError

        call getByte; AL = Scan Code Set
        jmp exit

    unknownError:
        mov al, 0xDD

    exit:
    }
}

Instead of getting current control set number I receive 0xFA from keyboard(Acknowledge). I'd be very grateful for any suggestions.

(I ran this code from windows driver)

ladan
  • 423
  • 4
  • 10
  • Shoouldn't SendByte look at OutputBufferFull and GetByte look at InputBufferFull? – stark Jan 16 '15 at 18:21
  • Hmm, even if it's wrong, this check passes and I end getting ack. Maybe I should read the result from other port? – ladan Jan 16 '15 at 19:45
  • I would look at the disassembly of your generated object files. The compiler could be inserting additional unwanted instructions after your `__asm` blocks that trash the return value. This is what [`__declspec( naked )`](http://msdn.microsoft.com/en-us/library/21d5kd3a.aspx) is for. – Jonathon Reinhart Jan 17 '15 at 02:14
  • Also, you're running this as a driver in a *running* windows system? Did you ever consider you may be conflicting with the kernel who is also interacting with the keyboard controller? – Jonathon Reinhart Jan 17 '15 at 02:15
  • The compiled code is ok (checked it while debugging in windbg). I can easilly change IRQL or clear interrupts, I am more curious how to correctly implement sending and receiving result of this command. Should I send a command to disable keyboard firstly, add some timeout... ? – ladan Jan 17 '15 at 02:45
  • So your question is not about coding in assembly but rather about how to use PS/2 keyboard protocol? In the later case Google query: `ps/2 keyboard protocol` seems to be the way to go – xmojmr Jan 17 '15 at 06:45
  • I wanted to know how to get current control set. I found the documentation, it said something like: "Writing 0xf0 (to 0x60) followed by 0 queries the mode, resulting in a scancode byte 43, 41 or 3f" I found similar samples, but I end getting ACK. I would like to know if this code should work (but let's say it doesn't because it's interfering with Windows internals), or am I missing some other necessary commands? – ladan Jan 17 '15 at 10:44

0 Answers0