Keyboard driver Principle

Source: Internet
Author: User
Keyboard driver Principle

0 Overview

We will discuss the driver of the PS/2 Keyboard. This article mainly discusses the PS/2 Keyboard hardware, the application layer of the keyboard driver, the initialization of the keyboard driver, how the keyboard driver completes its work, and some related content. Note that the keyboard we mentioned later, if not specified, is a PS/2 Keyboard.

1 PS/2 Keyboard hardware

For the purpose of writing a hardware driver, you need to have a certain understanding of the hardware, but it does not need to be too deep, as long as the write driver is enough. We also want to discuss the hardware knowledge of the PS/2 keyboard. We will not discuss the hardware implementation details that are not helpful to the driver.

Overview of 1.1 PS/2 Keyboard hardware

For the driver, the most important hardware related to the keyboard is two chips. One is an Intel 8042 chip located on the motherboard. The CPU communicates directly with the chip through the I/O port to obtain the scan code for the buttons or send various keyboard commands. The other is the intel 8048 chip or its compatible chip, which is located on the keyboard. The chip is mainly used to obtain the scan code generated by the pressed key from the keyboard hardware and communicate with i8042, control the keyboard itself.

When a key is pressed on the keyboard, i8048 directly obtains the scan code generated by the keyboard hardware. I8048 is also responsible for the control of the keyboard itself, such as lighting the LED indicator, off the LED indicator. I8048 communicates with i8042 through the PS/2 port and transmits the obtained scan code to i8042. Through the read/write port, the CPU can directly read the data in i8042 into the CPU register, or write the data in the CPU Register into i8042. PS/2 port has a total of 6 pins, You can unplug the PS/2 plug to see, these 6 pins are respectively, clock, Data, power source, power + 5 V, two other pins are not used. Since there is only one pin for data transmission, data transmission on the PS/2 port is serial.

The following figure shows the key martix used to generate the scan code. You can see the chip in the keyboard (not i8048, is a compatible chip of other models ).

 

 

Note that the i8042 may not appear on the motherboard alone and may be integrated into a certain chip.

1.2 scan code, make code, break code, typematic

When a key is pressed, released, and pressed on the keyboard, scan code is generated on the keyboard, which is obtained directly by i8048. There are two types of scan codes: Make code and break code. Make code is generated when a key is pressed or pressed, and break code is generated when a key is released. Each key is assigned a unique make code and break code, so that the host can know which key it is by scanning the code. Simply put, press the key to generate a make code. Release the key to generate a break code.

But for the situation where you can't hold it down. We can open a notepad and hold down the $1 $ A $1 $ key. We can see that $1 $ A $1 $ is generated continuously until we release it. This is because, when you hold down a key, typematic, that is, it is automatically played. At a certain time, a hold-down key make code is automatically generated until the key is released. There are two important parameters for typematic. One is typematic delay, which determines how long the press will take to enter the typematic, and the other is the typematic rate. after entering the typematic, the number of make codes that can be generated in one second. Now open notepad, hold down $1 $ A $1 $, and observe carefully, the interval between the first $1 $ A $1 $ and the second $1 $ A $1 $ is much longer than the other one, the interval between $1 $ A $1 $ is the same.

However, when you press multiple keys at the same time. When a key is pressed, The make code is generated, but not released, and the break code is not generated, another key is pressed, And the make code of another key is generated, even if these two keys are simultaneously pressed. When the two keys are released, their respective break codes are generated. The same applies to more keys. For example, press Ctrl and A. In the following example, press Ctrl and A at the same time. Press ctrl to generate the CTRL make code, and then press a to generate the make code of A, and then release them to generate their respective break codes.

When you press a key, you can press another key. We can open a notepad, hold down the $1 $ A $1 $ key, do not release it, and then press the $1 $ S $1 $ key. We can see that when you press $1 $ S $1 $, the $1 $ A $1 $ key is not loose, but there is no $1 $ A $1 $, instead, $1 $ S $1 $ appears. Even if $1 $ S $1 $ is released, $1 $ A $1 $ will not appear again.

1.3 scan code set


So far, there have been three sets of scan code sets, and the second set is used by default for the PS/2 Keyboard. However, you can set i8042 to make i8042
Translate the obtained scan code into scan code in scan code set 1, so that the keyboard driver
All the obtained scan code is the scan code in the first set (in practice, the driver does the same ). So we only discuss scan code
Set 1. It must be noted that the scan code and ASCII code are completely different.

In the scan
In code set 1, make code and break code of most keys are one byte. Their make code
The maximum bits are 0, that is, their make code is smaller than 0x7f. And their break code
For its make code or 80 h operation, that is, the lower 7 bits of make Code remain unchanged, and the highest bits are set to 1.


There are also some extension buttons whose scan code is dubyte. Their first byte is e0h, indicating that this is an extension key. 2nd bytes, and single-byte
The scan code is the same.


There is also a special key, pause/break, whose make code is e1, 1d, 45 E1, 9d, C5, note that it is e1h
. And it does not have the break code.

The size of the make code value to list all scan codes in scan code set 1.

Key make break

ESC 01 81
1 02 82
2 03 83
3 04 84
4 05 85
5 06 86
6 07 87
7 08 88
8 09 89
9 0a 8a
0 0b 8b
-0C 8C
= 0d 8d
Bksp 0e 8E

Tab 0f 8f
Q 10 90
W 11 91
E 12 92
R 13 93
T 14 94
Y 15 95
U 16 96
I 17 97
O 18 98
P 19 99
[1A 9A
] 1B 9B
Enter 1C 9C

Rochelle CTRL 1D 9d
A 1E 9e
S 1f 9f
D 20 A0
F 21 A1
G 22 A2
H 23 A3
J 24 A4
K 25 A5
L 26 A6
; 27 A7
$1 $28 A8

'29 89
L_shft 2a AA
\ 2B AB
Z 2C AC
X 2D ad
C 2E AE
V 2f af
B 30 B0
N 31 B1
M 32 B2
, 33 B3
. 34 B4
/35 B5
R_shft 36 B6

KP * 37 B7
Rochelle alt 38 B8
Space 39 B9
Caps 3A Ba

F1 3B bb
F2 3C BC
F3 3D BD
F4 3E be
F5 3f BF
F6 40 C0
F7 41 C1
F8 42 C2
F9 43 C3
F10 44 C4

Num 45 C5
Scroll 46 C6

KP 7 47 C7
KP 8 48 C8
KP 9 49 C9
KP-4A ca

KP 4 4B CB
KP 5 4C CC
KP 6 4D CD
KP + 4E CE

KP 1 4f cf
KP 2 50 D0
KP 3 51 d1

KP 0 52 D2
KP. 53 D3

F11 57 D7
F12 58 D8

KP en E0, 1C E0, 9C
R_ctrl E0, 1D E0, 9d

KP/E0, 35 E0, B5
R_alt E0, 38 E0, B8

Home E0, 47 E0, C7
Up Arrow E0, 48 E0, C8
PG up E0, 49 E0, C9

L arrow E0, 4B E0, CB
R arrow E0, 4D E0, CD

End E0, 4f E0, cf
D arrow E0, 50 E0, D0
Pg dn E0, 51 E0, d1
Insert E0, 52 E0, D2
Delete E0, 53 E0, D3

L gui E0, 5b E0, DB
R GUI E0, 5C E0, DC
Apps E0, 5d E0, dd

Prnt scrn E0, 2a, E0, 37 E0, B7, E0, AA

Pause E1, 1d, 45 E1, 9d, C5-None

Here are a few irrelevant words for the driver. I cannot remember that the scan code column is listed in this order only after I had a guess about the scan code value.
In this way, we can guess the scan code value. In short, use this
The order of make code is in comparison with the layout of the current key on the keyboard. We can roughly guess why the make code value of key A is
0x1e: Why is the make code value of the H Key 0x23. Let's take a short example, A 1E, s 1f, D 20, F 21, G
22, H 23. Check the locations A, S, d, f, g, and H on the keyboard. If you can feel something, forget it if you don't feel it. This is irrelevant to the driver. From Scan
Code set 1, you may still be able to figure out the earliest keyboard look. And some changes on the keyboard. We noticed that F11 and F12
Make code is not connected together. It is estimated that there are only 10 function keys on the keyboard earlier than the current 12 function keys. Make code of the slave key
The keys that may have been used are not displayed on the keyboard.

It is worth noting that if there is a make code key of 0x60, its break code should be
0x60 + 0x80 = 0xe0. Then the break code of this key will be mixed with 0xe0, which represents the extended code. But fortunately, there is no make
If the code is 0x60, no obfuscation will occur.

1.4 i8042 keyboard controller

Keyboard driver direct read/write
The i8042 chip uses i8042 to indirectly send commands to the i8048 on the keyboard. Therefore, for the driver, only the i8042
So we will only introduce i8042, not i8048.

Image
An i8042 or i8048 chip is itself a small processor. It has its own processor, its own ram, its own registers, and so on.

I8042 has 4
8 bits registers, which are Status Register, output buffer ),
Input buffer and control register ). Use two I/O Ports, 60 h and
64 h.

Status Register)

The Status Register is an 8-bit read-only register that can be read by the CPU at any time. The definitions are as follows:

Bit7: parity-even (P_e): Data parity error obtained from the keyboard
Bit6: RCV-TMOUT (r_t): receive timeout, set 1
Bit5: trans_tmout (t_t): sending timeout, set to 1
Bit4: kybd_inh (K_ I): 1. the keyboard is not disabled. The value 0 indicates that the keyboard is disabled.
Bit3: 1__data (C_D): 1. The input buffer contains the command, 0, and data.
Bit2: sys_flag (s_f)
Bit1: input_buf_full (I _ B _f): input buffer full set 1, i8042 Remove post 0
Bito: out_buf_full (o_ B _f)

Output buffer)

The output buffer is an 8-bit read-only register. The driver reads data from this register. The data includes scan codes, responses sent to the i8042 command, and indirectly sent
I8048 command response.

Input buffer)

The input buffer is an 8-bit write-only register. Buffer the content sent by the driver. These include the commands sent to i8042, which are indirectly sent
I8048 command and data used as command parameters.

Control Register)

Also known as controller command byte (Controller command byte ). The definitions are as follows:

Bit7: reserved, it should be 0
Bit6: translate the second scan code into the first one.
Bit5: Set 1 to disable the mouse
Bit4: Set 1 to disable the keyboard.
Bit3: Set 1 to ignore bit4 in the Status Register.
Bit2: Set bit2 in the Status Register
Bit1: set to 1, enable mouse interruption
Bito: set to 1, enable keyboard interruption

2 ports 0x60, 0x64

Call 0x60 as the data port in the driver.
Call 0x64 as the command port in the driver.

1.5 command

The driver can send commands directly to i8042 and indirectly to i8048 through i8042. The command directly comes from
<Reference [1]>.

1.5.1 commands sent to i8042

The driver sends commands to the keyboard controller through 64 h writing port. There are a total of 12 commands, respectively

20 h
Prepare to read the command byte of the 8042 chip. Its behavior is to place the current 8042 command byte content in the output
In register, the next read operation from Port 60 will read it.

60 h
Command byte for writing 8042 chips; the next byte written through 60 h will be placed in command byte.

A4h
Test whether the keyboard password is set. The test result is placed in output.
Register, which can be read by 60 h. There are two possible values in the test result: Fah = password set; f1h = no password.

A5h
Set the keyboard password. The result is put in the input one by one through the 60 h port in order.
Register. The password ends with an empty byte (the content is 0 ).

A6h
Make the password take effect. Before publishing this command, you must first use the a5h command to set the password.

Aah
Self-check. The diagnostic results are stored in output register and can be read through 60 h. 55 H = OK.

ADH
Disable the keyboard interface. Bit-4 of command byte is set. When this command is published, keyboard will be banned from sending data to the output
Register.

Aeh
Open the keyboard interface. Command byte bit-4 is cleared. When this command is published, the keyboard is allowed to send data to the output
Register.

C0h
Prepare to read the input port. The content of the input port is placed in output register and can be read through port 60 h.

D0h
Prepare to read The outport. The result is stored in output register and then read through port 60 h.

D1h
Prepare to write the output port. The bytes written through port 60 h will be placed in the output port.

D2H
Prepare to write data to output register. The bytes written to the input register after 60 h will be placed in the output
In register, this function is used to simulate data sent from the keyboard. If the interrupt is allowed, an interrupt is triggered.

1.5.2 commands sent to 8048

There are 10 commands in total, which are

EDH
Set the LED. When the keyboard receives this command, an LED sets the session to start. The keyboard first replies with an ACK (Fah), and then waits for the LED written from Port 60 h.
Sets the byte. If one is waited, an ACK is returned again, and an LED is set based on this byte. Then wait... Wait until a non-led byte is set (high level is set), then the LED
Sets the end of the session.

Eeh
Diagnose echo. This command is purely used to check whether the keyboard is normal. If it is normal, when the keyboard receives this command, it will return an eeh byte.

F0h
Select scan code set. The keyboard system may have three scan codes.
Set. When the keyboard receives this command, it returns an ACK and waits for a scan code from Port 60.
SetCode. The system must send a scan code to the keyboard after this command.
Set code. When the keyboard receives this code, it will reply to an ACK again, and then set the scan code set to the received scan code
Set code.

F2h
Read keyboard
Id. Because the 8042 chip can not only be connected to the keyboard. This command is used to read the device ID after 8042. The device ID is 2 bytes, keyboard
The ID is 83abh. When the keyboard receives this command, it first returns an ACK and then returns the two-byte keyboard ID one by one.

F3h
Set typematic
Rate/delay. When the keyboard receives this command, it returns an ACK. Then wait for the setting byte from 60 h. Once received, an ACK will be replied, and then the keyboard
Rate/delay is set to the corresponding value.

F4h
Clear the output buffer of the keyboard. Once the keyboard receives this command, the output
Buffer is cleared and an ACK is returned. Then continue to accept the keyboard.

F5h
Set the default status (W/disable ). Once the keyboard receives this command, the keyboard is fully initialized to the default state. All previous settings will be invalid -- output
Buffer is cleared, and typematic rate/delay is set to the default value. Then reply to
Ack, and then wait for the next command. It should be noted that after this command is executed, the keyboard's access to the keys is forbidden. Enable is required if you want the keyboard to accept the key input.
Keyboard.

F6h
Set the default status. The only difference between the F5 command and the command is that after the command is executed, the keyboard can receive the keys.

Feh
Resend. If the keyboard receives this command, you must send the code to the 8042 output register
And then resend the data. After the system detects an error from the keyboard, you can use the self-command to re-Send the sent bytes to the keyboard.

FFH
Reset
Keyboard. If the keyboard receives this command, it first returns an ACK and starts its own reset.ProgramAnd performs basic correctness detection (Bat-Basic ).
Assurance test ). After all these ends, a single-byte ending code (AAH = success,
FCH = failed), and set the scan code set of the keyboard to 2.

1.5.3 read data

00 h/FFH
When an error is detected when you press or release the key, put this Byte after the output bufer. If the output buffer is full
The last byte of the buffer replaces this byte. Use 00 h, scan Code 2, and scan code when using scan code set 1
3. Use FFH.

Aah
Bat completes the code. If the keyboard detection is successful, the byte is sent to 8042 output register.

Eeh
Echo response. The keyboard uses eeh to respond to the echo request sent from 60 h.

F0h
Scan code set 2 and scan code set 3 are used as the break code prefix.

Fah
Ack. When the keyboard receives a valid command or data from Port 60 at any time, it returns a Fah.

FCH
Bat failure code. If the keyboard detection fails, the byte is sent to the 8042 output register.

Feh
Resend. When the keyboard receives an invalid command or data from Port 60 at any time, or the data parity error, it returns a Feh and requires the system to resend the command or data.

83abh
After receiving an f2h command from 60 h, the keyboard replies 83 h and abh in sequence. 83ab is the ID of the keyboard.

Scan code
Except for the special bytes above, the rest are scan code.

Port 1.6


First, we will introduce the read and write operations on the port. The read_port_uchar function is used in the driver to perform read operations. read_port_uchar
In. Use the write_port_uchar function in the driver to perform write operations. write_port_uchar
Use the CPU to write the PORT command, out.

1.6.1 read Status Register

The method of reading status registers to read 64 h ports.

1.6.2 read data

The data to be read includes the scan code of the i8042 key obtained from i8048, The ack of the i8042 command, and the ack of the i8048 command obtained from i8048. the resend command must be resending, the result of some commands that need to return results.

When data needs to be read by the driver, the data is put into the output buffer, and bit0 (output_buffer_full) of the Status Register is set to 1, causing keyboard interruption (in the keyboard
The broken IRQ is 1 ). The keyboard interrupt service routine provided by the keyboard driver is executed due to keyboard interruption. In the keyboard interrupt service routine, the driver reads data from i8042. Once the data is read,
Bit0 of the Status Register is cleared.

To read data, first read the Status Register and Judge bit0. The Status Register bit0 is 1, indicating that there is data in the output buffer. Ensure that the Status Register bit0 is 1, and then perform read operations on port 60 h to read data.

Here we will talk about some useful digress. The IRQ mentioned above is interrupt request.
The interrupt request line is a hardware line, which is different from the interrupt vector. The interrupt vector is the sequence number used to find the interrupt service routine in the Interrupt Descriptor Table (IDT. Keyboard
IRQ is 1, and the interrupt vector corresponding to the keyboard interrupt service routine is not 1. This should be clarified.

1.6.3 send a command to i8042

When the command is sent to i8042, the command is put into the input buffer, and the bit1 of the Status Register is set to 1, indicating that the input buffer is full and the Status Register
If bit2 is set to 1, a command is written to the input buffer.

To send a command to i8042, read the Status Register and Judge bit1. The Status Register bit1 is 0, which indicates that the input buffer is empty and can be written. Ensure that the Status Register bit1 is 0, write the 64 h port, and write the command.

1.6.4 send commands indirectly to i8048

Send these commands to i8042, i8042 will forward i8048, the command is put into the input buffer, and cause the Status Register bit1
Set 1 to indicate that the input buffer is full and the bit2 of the Status Register is also caused.
Set 1 to indicate that a command is written to the input buffer. Here, we should note that the command to i8048 is to write the 60 h port, while the parameter to be followed by the command is also to write the 60 h port. How can I determine whether the content in the input buffer is a command or a parameter? We will discuss it in the parameters of the subsequent commands.

To send a command to i8048, read the Status Register and Judge bit1. The Status Register bit1 is 0, indicating that the input buffer is empty and can be written. Ensure that the Status Register bit1 is 0, and then write the 60 h port to write the command.

1.6.5 command sending Parameters

Some commands require parameters. After sending the command, we send its parameters. The parameters are placed in the input buffer, causing bit1 of the Status Register.
Set 1 to indicate that the input buffer is full. Here, we should note that the command to i8048 is to write the 60 h port, the command parameter, and the 60 h port. How can I determine whether the content in the input buffer is a command or a parameter. I8042
If the current status register's bit3
1 indicates that a command has been written before. Now, by writing the 60 h port into the input buffer, it will be treated as a parameter of the previous command and cause the Status Register
Bit3 is set to 0. If bit3 of the Current Status Register
If the value is 0, it indicates that no command has been written before. Now, by writing the content of port 60 into the input buffer, it is used as a command that is indirectly sent to i8048 and causes the Status Register
Set bit3 to 1.

To send parameters to i8048, read the Status Register and Judge bit1. The Status Register bit1 is 0, indicating that the input buffer is empty and can be written. Ensure that the Status Register bit1 is 0, write the 60 h port, and write the parameters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.