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]
m (Repeat rate typo)
(The keyboard reset transaction was not clear, the reset function does return ACK before the test result)
 
(36 intermediate revisions by 20 users not shown)
Line 3:
The PS/2 Keyboard is a device that talks to a PS/2 controller using [[PS/2|serial communication]]. Ideally, each different type of PS/2 controller driver should provide some sort of standard/simple "send byte/receive byte" interface, and the PS/2 Keyboard driver would use this interface without caring about lower level details (like what type of PS/2 controller the device is plugged into).
 
The PS/2 Keyboard accepts commands and sends responses to those commands, and also sends scan codes indicating when a key was pressed or released. Keep in mind that these scan codes (from the tables below) may be altered (i.e. translated) by the PS/2 controller, depending on the controller's settings.
 
== Commands ==
 
A PS/2 Keyboard accepts many types of commands. A command is one byte. Some commands have data byte/s which must be sent after the command byte. The keyboard typically responds to a command by sending either an "ACK" (to acknowledge the command) or a "Resend" (to say something was wrong with the previous command) back. Don't forget to wait between the command, the data and the response from keyboard.
 
The commands that a PS/2 Keyboard accepts are:
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 201 ⟶ 216:
Modern keyboards should support all three scan code sets, however some don't. Scan code set 2 (the default) is the only scan code set that is guaranteed to be supported. In theory (I haven't tried it) it should be possible to attempt to set scan code set 1 or scan code set 3, and then ask the keyboard which scan code it is currently using and see if it actually is using the requested scan code set. In this way it may be possible to determine which scan code sets the keyboard does support.
 
Scan codes themselves are sequences of one or more bytes. In some cases the sequence can be as many as 6 bytes (e.g. the "print screen"Pause/Break key in scan code set 1
generates the sequence 0xE1, 0x1D, 0x45, 0xE1, 0x9D, 0xC5 when pressed). This situation isn't really ideal. In general (for later processing) you want to convert these "one or more byte sequences" into a single integer that uniquely identifies a specific key, that can be used effectively in things like lookup tables (without having sparsely used "many GiB" lookup tables).
 
Line 207 ⟶ 222:
 
Basically, when the keyboard driver's state machine knows it has received a complete scan code, the next step is to convert the "one or more byte" scan code into a key code.
 
 
=== Key Codes, Key States and Key Mappings ===
Line 251 ⟶ 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 674 ⟶ 689:
|
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
| align="right" | 0xE0, 0x10
| (multimedia) previous track pressed
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|
|
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
|
|
| style="background: #E8E8E8" align="right" | 0xE0, 0x19
| style="background: #E8E8E8" | (multimedia) next track pressed
|
|
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
Line 684 ⟶ 717:
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
| align="right" | 0xE0, 0x20
| (multimedia) mute pressed
| style="background: #E8E8E8" align="right" | 0xE0, 0x21
| style="background: #E8E8E8" | (multimedia) calculator pressed
| align="right" | 0xE0, 0x22
| (multimedia) play pressed
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
| align="right" | 0xE0, 0x24
| (multimedia) stop pressed
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|
|
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
|
|
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
| align="right" | 0xE0, 0x2E
| (multimedia) volume down pressed
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
| align="right" | 0xE0, 0x30
| (multimedia) volume up pressed
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
| align="right" | 0xE0, 0x32
| (multimedia) WWW home pressed
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
|
|
Line 752 ⟶ 821:
| style="background: #E8E8E8" align="right" | 0xE0, 0x5D
| style="background: #E8E8E8" | "apps" pressed
| align="right" | 0xE0, 0x5E
| (ACPI) power pressed
| style="background: #E8E8E8" align="right" | 0xE0, 0x5F
| style="background: #E8E8E8" | (ACPI) sleep pressed
|-
|
|
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|
|
| style="background: #E8E8E8" align="right" | 0xE0, 0x63
| style="background: #E8E8E8" | (ACPI) wake pressed
|-
|
|
| style="background: #E8E8E8" align="right" | 0xE0, 0x65
| style="background: #E8E8E8" | (multimedia) WWW search pressed
| align="right" | 0xE0, 0x66
| (multimedia) WWW favorites pressed
| style="background: #E8E8E8" align="right" | 0xE0, 0x67
| style="background: #E8E8E8" | (multimedia) WWW refresh pressed
|-
| align="right" | 0xE0, 0x68
| (multimedia) WWW stop pressed
| style="background: #E8E8E8" align="right" | 0xE0, 0x69
| style="background: #E8E8E8" | (multimedia) WWW forward pressed
| align="right" | 0xE0, 0x6A
| (multimedia) WWW back pressed
| style="background: #E8E8E8" align="right" | 0xE0, 0x6B
| style="background: #E8E8E8" | (multimedia) my computer pressed
|-
| align="right" | 0xE0, 0x6C
| (multimedia) email pressed
| style="background: #E8E8E8" align="right" | 0xE0, 0x6D
| style="background: #E8E8E8" | (multimedia) media select pressed
|
|
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
| align="right" | 0xE0, 0x90
| (multimedia) previous track released
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|
|
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
|
|
| style="background: #E8E8E8" align="right" | 0xE0, 0x99
| style="background: #E8E8E8" | (multimedia) next track released
|
|
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
Line 765 ⟶ 888:
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
| align="right" | 0xE0, 0xA0
| (multimedia) mute released
| style="background: #E8E8E8" align="right" | 0xE0, 0xA1
| style="background: #E8E8E8" | (multimedia) calculator released
| align="right" | 0xE0, 0xA2
| (multimedia) play released
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
| align="right" | 0xE0, 0xA4
| (multimedia) stop released
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|
|
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
|
|
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
| align="right" | 0xE0, 0xAE
| (multimedia) volume down released
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
| align="right" | 0xE0, 0xB0
| (multimedia) volume up released
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
| align="right" | 0xE0, 0xB2
| (multimedia) WWW home released
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
|
|
Line 833 ⟶ 992:
| style="background: #E8E8E8" align="right" | 0xE0, 0xDD
| style="background: #E8E8E8" | "apps" released
| align="right" | 0xE0, 0xDE
| (ACPI) power released
| style="background: #E8E8E8" align="right" | 0xE0, 0xDF
| style="background: #E8E8E8" | (ACPI) sleep released
|-
|
|
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
|
|
| style="background: #E8E8E8" align="right" | 0xE0, 0xE3
| style="background: #E8E8E8" | (ACPI) wake released
|-
|
|
| style="background: #E8E8E8" align="right" | 0xE0, 0xE5
| style="background: #E8E8E8" | (multimedia) WWW search released
| align="right" | 0xE0, 0xE6
| (multimedia) WWW favorites released
| style="background: #E8E8E8" align="right" | 0xE0, 0xE7
| style="background: #E8E8E8" | (multimedia) WWW refresh released
|-
| align="right" | 0xE0, 0xE8
| (multimedia) WWW stop released
| style="background: #E8E8E8" align="right" | 0xE0, 0xE9
| style="background: #E8E8E8" | (multimedia) WWW forward released
| align="right" | 0xE0, 0xEA
| (multimedia) WWW back released
| style="background: #E8E8E8" align="right" | 0xE0, 0xEB
| style="background: #E8E8E8" | (multimedia) my computer released
|-
| align="right" | 0xE0, 0xEC
| (multimedia) email released
| style="background: #E8E8E8" align="right" | 0xE0, 0xED
| style="background: #E8E8E8" | (multimedia) media select released
|
|
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
|
|
Line 868 ⟶ 1,063:
 
Note: There is no scan code for "pause key released" (it behaves as if it is released as soon as it's pressed)
 
 
=== Scan Code Set 2 ===
Line 1,096 ⟶ 1,290:
| style="background: #E8E8E8" align="right" | 0x5D
| style="background: #E8E8E8" | \ pressed
|
|
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
|
|
| style="background: #E8E8E8" |  
| style="background: #E8E8E8" |
|
|
Line 1,600 ⟶ 1,803:
| style="background: #E8E8E8" align="right" | 0xF0, 0x5D
| style="background: #E8E8E8" | \ released
|
|
| style="background: #E8E8E8" |
| style="background: #E8E8E8" |
|-
|
|
| style="background: #E8E8E8" |  
| style="background: #E8E8E8" |
|
|
Line 1,927 ⟶ 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}}
'''TODO'''
|-
 
!Key
!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 1,936 ⟶ 2,204:
*[[PL050 PS/2 Controller | PL050 PS/2 Controller (ARM)]]
*[[PS/2 Mouse]]
===Forum Threads===
*[[Topic:9746|Keyboard input]]
*[[Topic:9761|Up or down press?]]
Line 1,947 ⟶ 2,215:
 
===External Links===
*[http://www.computer-engineering.org www.Computer-Engineering.org]
*[http://www.webmasteren.eu/viden/os/PS2.pdf KMT dk's ps2 keyboard and controller referance]
*[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/Stichting-MINIX-Research-Foundation/minix/blob/master/minix/drivers/hid/pckbd/pckbd.c Minix]
 
[[Category:Human Interface Device]]
Anonymous user