PS/2 Keyboard: Difference between revisions

The keyboard reset transaction was not clear, the reset function does return ACK before the test result
[unchecked revision][unchecked revision]
mNo edit summary
(The keyboard reset transaction was not clear, the reset function does return ACK before the test result)
 
(20 intermediate revisions by 9 users not shown)
Line 62:
| Set scan code set 3
|}
| Get/set current scan code set
| 0xFA (ACK) or 0xFE (Resend) if scan code is being set; 0xFA (ACK) then the scan code set number, or 0xFE (Resend) if you're getting the scancode. If getting the scancode the table indicates the value that identify each set:
{| {{wikitable}}
|-
! Value
! Use
|-
| 43
| Scan code set 1
|-
| 41
| Scan code set 2
|-
| 3f
| Scan code set 3
|}
 
|-
| 0xF2
| None
| Identify keyboard
| 0xFA (ACK) followed by none or more ID bytes (see [[http:"8042" PS//wiki.osdev.org/%228042%22_PS/2_Controller2 Controller#Detecting_PSDetecting PS.2F2_Device_Types2F2 Device Types| "Detecting Device Types"]])
|-
| 0xF3
Line 148 ⟶ 163:
| None
| Reset and start self-test
| 0xFA (ACK) or 0xFE (Resend) followed by 0xAA (self-test passed), 0xFC or 0xFD (self test failed), or 0xFE (Resend)
|}
 
Line 224 ⟶ 239:
* Various other key states (shift, alt, control, etc)
* Various "toggle" states (CapsLock, ScrollLock, NumberLock, etc)
 
== Enough, give me code! ==
 
So, first read the scan code set table, and now start to code. If you too lazy, here's the full code:
 
(inb/outb can be found here: [[Inline Assembly/Examples#I.2FO access|Inline Assembly Examples - I/O Access]])
<source lang="c">
/*
PS/2 keyboard code.
Dependencies:
inb function and scancode table.
*/
char getScancode() {
char c=0;
do {
if(inb(0x60)!=c) {
c=inb(0x60);
if(c>0)
return c;
}
} while(1);
}
 
char getchar() {
return scancode[getScancode()+1];
}
</source>
 
If you have not set up interrupts yet, the code for the function that reads scancodes will be slightly different:
 
<source lang="C">
char getScancode()
{
while (!(inb(0x64) & 1));
return inb(0x60);
}
</source>
The reason we are reading port 0x64 first is because in an environment without interrupts, one must first read the status port (0x64) and see if the first bit is set to one. If the first bit is set to one, the loop ends and we read port 0x60, returning a scancode. If it is not set to one, we do not read it and loop until it is set to one. This will assure that we are not infinitely releasing scancodes (since we do not have interrupts yet to make sure they are only pressed once).
 
== Scan Code Sets ==
Line 288 ⟶ 265:
The following table shows which scan codes correspond to which keys when using scan code set 1 (for a "US QWERTY" keyboard only):
 
Note that scancodes with extended byte (E0) generates two different interrupts: the first containing the E0 byte, the second containing the scancode
{| {{wikitable}}
|-
Line 2,161 ⟶ 2,139:
The following table shows which scan codes correspond to which keys when using scan code set 3 (for a "US QWERTY" keyboard only):
 
{| {{wikitable}}
[http://www.computer-engineering.org/ps2keyboard/scancodes3.html Scan Codes]
|-
 
!Key
'''TODO'''
!Code for Scan code set 3
 
|-
|A || 1C
|-
|B || 32
|-
|C || 21
|-
|D || 23
|-
|E || 24
|-
|F || 2B
|-
|G || 34
|-
|H || 33
|-
|I || 43
|-
|J || 3B
|-
|K || 42
|-
|L || 4B
|-
|M || 3A
|-
|N || 31
|-
|O || 44
|-
|P || 4D
|-
|Q || 15
|-
|R || 2D
|-
|S || 1B
|-
|T || 2C
|-
|U || 3C
|-
|V || 2A
|-
|W || 1D
|-
|X || 22
|-
|Y || 35
|-
|Z || 1A
|}
 
[https://web.archive.org/web/20170108131104/http://www.computer-engineering.org/ps2keyboard/scancodes3.html Scan code]
 
==See Also==
Line 2,183 ⟶ 2,215:
 
===External Links===
*[http://www.computer-engineering.org www.Computer-Engineering.org]
*[http://www.win.tue.nl/~aeb/linux/kbd/scancodes.html Keyboard scancodes] - A complete reference on all scancodes you might encounter.
*[https://web.archive.org/web/20030621203107/http://www.microsoft.com/whdc/hwdev/tech/input/Scancode.mspx USB HID to PS/2 Translation Table] - Microsoft's table of scancodes and USB equivalents
*[https://web.archive.org/web/20190301075756/http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc Keyboard Scan Code Specification] - Microsoft's specification for scancodes
*[https://www.youtube.com/playlist?list=PLUZozxlhse-NUto5JeJ0EDXEUFloWBdAj PS/2 keyboard interface playlist] - A video playlist of building a PS/2 keyboard interface on bread boards, by Ben Eater.
 
==== Implementations ====
* [http://lxr.linux.no/#linux+v3.5.4/drivers/input/keyboard/atkbd.c Linux] (C,GPL)
* [https://github.com/minix3Stichting-MINIX-Research-Foundation/minix/blob/e1131d9c96fe00bd07aa66540e0830a91dbbf31emaster/minix/drivers/hid/pckbd/pckbd.c Minix]
 
[[Category:Human Interface Device]]
Anonymous user