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, with the CPU passing through Io
The port directly communicates with the chip to obtain the scan code of the key or send various keyboard commands. The other is Intel 8048.
The chip or its compatible chip is located in the keyboard. This chip is mainly used to obtain the scan code generated by the pressed key from the keyboard hardware, communicate with i8042, and 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
Turn off the LED. I8048 communicates with i8042 through the PS/2 port and transmits the obtained scan code to i8042. CPU
Through the read/write port, you can directly read the data in i8042 into the CPU register, or write the data in the CPU Register into i8042. PS/2
There are 6 pins in the port. You can unplug the PS/2 connector and check that the six pins are respectively, clock, Data, power supply location, power supply + 5 V, and 2 more.
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 on the keyboard is pressed, loosen, hold down, the keyboard will generate scan code, which will be i8048
. There are two types of scan codes: Make code and break code. Make code is generated when a key is pressed or pressed
When a key is released, the break code is generated. Each key is assigned a unique make code and break code.
In this way, 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 $
Until we release it. This is because, when you hold down a key, typematic, that is, it is automatically played. At a certain time, make automatically generates a hold-down key
Code until the key is released. There are two important parameters for typematic. One is typematic delay, which determines how long the press will take
Typematic, the other is typematic rate, which determines how many make codes can be generated in one second after entering typematic.
. Now open notepad, hold down $1 $ A $1 $, and observe carefully, the first $1 $ A $1 $ and the second $1 $ A $1 $
The interval is obviously longer than that of others, and the interval between each $1 $ A $1 $ is the same.
However, when you press multiple keys at the same time. When a key is pressed, make code is generated, instead of being released, and break is not generated.
When the code is used, another key is pressed, And the make code of the other key is generated, even if the two keys are simultaneously pressed. When the two keys are released
Break code. 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
CTRL's make code, and then press a to generate a's make code, and then release each of them to generate their own break code.
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 $1 $ S.
$1 $ do not place the 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 $ starts to appear.
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, let i8042 translate the obtained scan code into scan code in scan code set 1, so that the keyboard driver
All scan code obtained by i8042 is the scan code in the first set (this is also true for the driver in practice ). So we only discuss scan code
Set 1. It must be noted that the scan code and ASCII code are completely different.
In scan code set 1, make code and break code of most keys are one byte. Their
The maximum bit of make code is 0, that is, their make code is smaller than 0x7f. And their break code is its make
The code or operation is 80 h, 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, which is the same as the single-byte scan code.
There is also a special key, pause/break. Its make code is e1, 1d, 45 E1, 9d, C5. Note that it starts with 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 19
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 make code
In contrast to the current keyboard key layout, We can roughly guess why the make code value of key A is 0x1e and why the make
The code value is 0x23. Let's take a short example, A 1E, s 1f, D 20, F 21, G 22, H 23, and look at the keyboard.
A, S, d, f, g, h. If you can feel something, forget it if you don't feel it. This is irrelevant to the driver. From scan code set
1. The earliest keyboard appearance may be inferred. And some changes on the keyboard. We noticed the make code of F10, F11, and F12.
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 code
The key is 0x60, so no obfuscation will occur.
1.4 i8042 keyboard controller
The keyboard driver directly reads and writes the i8042 chip, and indirectly sends a command to the i8048 In the keyboard through i8042. Therefore, for the driver, only i8042 is directly associated, so we only introduce i8042, not i8048.
A chip like i8042 and i8048 is itself a small processor. It has its own processor, its own ram, its own registers, and so on.
I8042 has four 8 bits registers, they are Status Register (Status Register), output
Buffer (output buffer), input buffer (input buffer), and control register (control register ). Use two Io
Port, 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 indirect responses sent to the i8048 command.
Input buffer)
The input buffer is an 8-bit write-only register. Buffer the content sent by the driver. These include commands sent to i8042, commands indirectly sent to i8048 through i8042, and data 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 is directly 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. The behavior is to place the current 8042 command byte content in output register, and 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 results are placed in the input register one by one through the 60 h port in order. 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, the keyboard will be banned from sending data to the output register.
Aeh
Open the keyboard interface. Command byte bit-4 is cleared. After the 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. After 60 h, the bytes written to the input register will be put into the output 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
Wait for the LED setting byte written from Port 60 h. If you wait for one, return an ACK again, and then set the LED according to this byte. Then wait... Wait until a non-led setting word
Section (high is set), and the LED sets the session to end.
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.
Set code. 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 an ACK is received, an ACK is returned and the keyboard rate/delay value is set as the corresponding value.
F4h
Clear the output buffer of the keyboard. Once the keyboard receives this command, it clears the output buffer and replies with an ACK. 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. Before
All 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 resend the data that was just sent to 8042 output register. 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, starts its own reset program, and performs its own basic correctness check (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
If the buffer is full, the last byte of the output buffer is replaced by this byte. Use 00 h when using scan code set 1, scan
Code 2 and scan code 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 the bit0 (output_buffer_full) of the Status Register is set to 1, causing the key
Disk interruption (the IRQ of keyboard interruption 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
After 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
Yes. 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 bit2 of the Status Register is set to 1, indicates that a command is written into 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. I8042 for Example
How can we determine whether the content in the input buffer is a command or a parameter? We will discuss it in the following command parameters.
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. I8042 how to judge
Whether the content is a command or a parameter. I8042 is determined in this way, if the current status register 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 bit3 of the Status Register
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, and then perform write operations on port 60 h to write parameters.
Transferred from: http://pengcd.spaces.live.com/blog/cns! 2d5631531cc867bd! 228. Entry