Hi,
The code shown does the following:
a) Tell the keyboard controller chip to disable the clock for the first PS/2 device (the keyboard). This prevents the keyboard from sending data to the PS/2 controller chip.
b) Send command 0xED to the keyboard, which is the right command to set the LEDs, but the keyboard can't send an ACK (because of the first step) so this might not actually do anything - I assume that if the keyboard can't send the ACK it doesn't move on to the "wait for next byte" part...
c) Send 0xFF to the device, which would be interpretted as "perform internal power-on reset function", otherwise known as the BAT (Basic Assurance Test). This is the same test that occurs when the keyboard first gets power (ie. you turn the computer on), which makes the LEDs flash (just like when you turn the computer on). This 0xFF
is not the second byte of the set LEDs command, as the set LEDs command didn't work.
d) Tell the PS/2 controller chip to enable the clock for the first PS/2 device (the keyboard). This allows the keyboard to send data to the PS/2 controller chip. At this point the keyboard might send an ACK or something that it failed to send earlier, or possibly an 0xFE (Resend).
Some advice (all IMHO): Windows is crap. If you plug 2 PS/2 keyboards in or you've got the mouse & keyboard around the wrong way (easy enough to do), Windows will lock up (not sure about XP here but 95/98 does). There's no reason why an OS can't detect this and display an error message, or auto-detect the devices and just plain work in most cases - the old version of my OS does work with non-standard combinations of PS/2 devices, and the new version will also (Linux can work with a pair of PS/2 keyboards, but I don't know what's involved in setting it up).
Some more advice (still IMHO): Unless you're willing to test & initialize the PS/2 controller chip correctly, don't (see
http://www.mega-tokyo.com/forum/index.p ... eadid=7794 for how I do it). Most hobby OS's (and I assume Windows too) just cross their fingers and hope everything is going to work without any of this, which is why they fail when everything
isn't as they expect.
With this in mind you may be able to use something like:
Code: Select all
InitKeyboard:
cli
;Force the keyboard's output buffer empty
L1: in al,0x64
test al,0x20
je L2
in al,0x60
jmp L1
;Make sure keyboard's input buffer is empty
L2: in al,0x64
test al,2
jnz L2
;Send "set LEDs" command
mov al,0xed
out 0x60,al
;Wait for "ACK" and ignore it without testing
L3: in al,0x64
test al,0x20
jne L3
in al,0x60
;Make sure keyboard's input buffer is empty
; Note: possibly not needed as we know it's received the last command because it sent ACK
L4: in al,0x64
test al,2
jnz L4
;Send the LED flags
mov al,0x07
out 0x60,al
sti
ret
Note: After enabling interrupts your keyboard IRQ handler should be ready to receive an ACK left over from the last byte sent to the keyboard.
My code above is barely adequate - if the user has a USB keyboard (with PS/2 emulation disabled) or no keyboard at all (server) then it will lock up waiting for the first ACK to come from a keyboard that doesn't exist.
A much better idea is to have a pile of little helper routines with time-outs, for e.g.:
Code: Select all
;Send byte to PS/2 controller
send_byte:
push ecx
push eax
mov ecx,[current_system_timer_tick]
add ecx,a_value_representing_a_50_mS_timeout_value
L1: in al,0x64
test al,2
jz L2
cmp [current_system_timer_tick],ecx
jb L1
pop eax
stc
ret
L2: pop eax
pop ecx
out 0x60,al
clc
ret
;Initialize the keyboard
InitKeyboard:
cli
call force_keyboard_output_empty
mov al,0xED ;Send "set LEDs" command
call send_byte
jc .error
call wait_for_ACK
jc .error
mov al,0x07 ;LEDs - all on
call send_byte
jc .error
sti
ret
This makes it much easier to see what the "init" code is doing (and it won't lock up when something isn't as expected).
Cheers,
Brendan