JIURL keyboard driver 1

zhaozj2021-02-16  49

JIURL keyboard driver 1 Author: JIURL Home: http://jiurl.yeah.net Date: 2003-12-13

0 Overview We will discuss the drive of the PS / 2 keyboard. The main discussion has, the hardware of the PS / 2 keyboard, using the keyboard drive application layer, the keyboard driver initialization, how to complete your own work, and some related content involved. It should be noted that the keyboard we mentioned later, if there is no special instruction, all the PS / 2 keyboard. 1 PS / 2 keyboard hardware to write a hardware driver, you need to have a certain understanding of this hardware, but don't need too deep, as long as it is enough to write enough. With regard to the hardware knowledge of the PS / 2 keyboard, we also have enough to discuss the keyboard driving, and some details of the hardware implementation of the driver, we do not discuss. 1.1 PS / 2 Keyboard Hardware Overview For drivers, and the most important hardware related to the keyboard is two chips. One is an Intel 8042 chip, on the motherboard, the CPU communicates directly with this chip through the IO port, and obtains the scan code of the button or sends a variety of keyboard commands. The other is an Intel 8048 chip or a compatible chip, located in the keyboard, the main function of this chip is to obtain the scan code generated by the posed button in the hardware of the keyboard, communicate with the I8042, and control the keyboard itself. When there is a key on the keyboard being pressed, the I8048 directly obtains the scan code generated by the keyboard hardware. The I8048 is also responsible for the control of the keyboard itself, such as lighting the LED indicator, extinguing the LED indicator. The I8048 communicates through the PS / 2 port and I8042, transmitting the obtained scan code to I8042. The CPU can directly read the data in the I8042 into the registers of the CPU, or write data in the CPU register into i8042. PS / 2 has 6 pins, you can unplug the PS / 2 plug, this 6 pins are, clock, data, power supply, power 5V, and 2 pins are not used . Since only one pin is transmitted, the data transfer on the PS / 2 is serial. The following figure below is the inside of a keyboard, which can be seen to generate a key matrix (Key Martix), which can see the chip in the keyboard (here is not I8048, is a compatible number of other models). Note that I8042 does not necessarily appear separately on the motherboard, which may be integrated into a single chip. 1.2 Scan code, make code, break code, typematic When the keyboard is pressed, loosen, press and hold, the keyboard will generate scan code, which will be directly obtained by I8048. There are two types of scan code, make code and break code. When a button is pressed or pressed, it is made, when a button is released, Break Code is generated. Each key is assigned a unique Make Code and Break Code so that the host can know which key is known by the scan code. Simply put, press the button to generate a make code. Loosen the key to create a Break code. And for the case where you don't put it. We can open a notepad, press the 'A' button and don't put it, you can see that 'a' will generate 'a' until we release. This is because when a button is pressed, it will be TypeMatic, which is automatically hit. Every time, a Make Code that is pressed and pressed is automatically generated until the key is released.

There are two important parameters for TypeMatic, one is TypeMatic DELAY, which determines how long it is in TypeMatic, and the other is TypeMatic Rate, which determines how many Make Code can generate after entering TypeMatic. Now let's open the notepad, hold down 'a' does not put, carefully observe, will see the first 'A' and the second 'A' interval time is significantly longer than other points, and each The time interval between 'A' is the same. And for the case of simultaneous pressing multiple keys. At a button, it was pressed, and it created Make Code, not released, and did not generate Break code, then press another button, and generated another key Make Code, even these two keys were simultaneously Press. After that, the two keys are turned to open, each produces their respective Break Code. The case of more keys is true. For example, press Ctrl and A, the following, even if the Ctrl and A are pressed simultaneously. Press CTRL to generate Ctrl Make Code, then press A to generate a Make Code, then release, each produces respective Break Code. And when you don't put it, press another button again. We can open a notepad, hold the 'A' button, don't loosen, then press the 's' button. We can see when you press 's', 'A' button is not loose, but there is no 'a' appear again, but 's' begins to appear, then even if the 's', 'a' is not Will continue. 1.3 Scan code set to the SCAN Code Set, and the second set of PS / 2 keyboard is used by default. However, you can set I8042, let I8042 translate the obtained Scan Code into Scan Code in Scan Code Set 1, so keyboard drive all Scan Code from I8042 is the first set of Scan Code in the first set (actually driver is also doing this ). So we only discuss SCAN Code Set 1. It should be noted that the Scan Code and the ASCII code are completely different. In Scan Code Set 1, MAKE CODE, Break Code of most keys is one byte. The highest bit of their make code is 0, that is, their make codes are less than 0x7f. And their Break Code is its make code or operation of 80h, that is, the low 7 digits of make Code, the highest bit is set to 1. There are also some extensions, their Scan Code is double bytes. Their first bytes are E0H, indicating that this is an extension key. The second byte is the same as the single-byte Scan Code. There is also a special key, the PaUSE / BREAK button, its Make Code is E1, 1D, 45 E1, 9D, C5, note that the beginning of E1H. And it doesn't have Break Code.

The value of the value of our button, lists all the scan code in Scan Code Set 1 Key Make Breakesc 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 L_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 '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 L_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 E 0,4d E0, CD 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 is a few sentences The driver has no help, it is unclear because there is a guess for the value of Scan Code, in order to collect Scan Code in this order, but first, SCAN Code will have a speculation of the value of Scan Code. In short, use this Make Code's order, and the layout of the key on the keyboard now, we can guess why the Make Code value of the A key is 0x1e, why the h key's Make Code value is 0x23. We took a small section of this example, A 1e, s 1f, d 20, f 21, g 22, h 23, see the position of A, S, D, F, G, and H on the keyboard. What can I feel, I can't feel it, this and the drive are independent. From Scan Code Set 1, it may also be speculated as the earliest keyboard. And some changes that happen on the keyboard.

We noticed that F10 and F11, the Make Code of F12 is not even together, and it is estimated that the earlier keyboard has only 10 function keys, not the current 12 function keys. From the key code of the key, there may be some keys that have been used now, no appearance on the keyboard. There is also a notes that if there is a key code for the 0x60, then its BREAK code should be 0x60 0x80 = 0xE0. Then the Break Code of this button will be mixed with 0xE0 indicating the extension code. However, it is ok, there is no key code of 0x60, so it will not have a case where it will happen. 1.4 I8042 Keyboard Controller Keyboard Drive Directly reads and writes the I8042 chip, and the I8042 is indirectly sent to the i8048 in the keyboard. Therefore, for the driving, only I8042 directly contact, so we only introduce I8042, and does not introduce I8048. The chip like I8042, I8048 is itself a small processor, which has its own processor, has its own RAM, has its own register, and so on. I8042 has 4 8 BITS registers, they are Status Register, Output Buffer (Output Buffer), Input Buffer, Control Register, and Control Register. Use two IO ports, 60h and 64h. The Status Register status register is an 8-bit read-only register and can be read by the CPU at any time. It defines the following bit7: Parity-Even (p_e): Dippoints obtained from the keyboard Bit6: RCV-TMOUT (R_T): Receive timeout, set 1bit5: TRANS_TMOUT: Send timeout, set 1bit4: kybd_inh K_i): It is 1, the keyboard is not banned. For 0, the keyboard is prohibited. Bit3: cmd_data (c_d): is 1, the content in the input buffer is command, 0, and the content in the input buffer is data. Bit2: Sys_FLAG (S_F): System Sign, Power Distribution 0, self-test through the rear 1bit1: input_buf_full (i_b_f): Input buffer full 1, I8042 takes away 0BitO: OUT_BUF_FULL (O_B_F): Output buffer Full 1, the CPU reads the rear 0Otput buffer output buffer is an 8-bit read-only register. Drives read data from this register. These data include, scanning code, respond to I8042 command, indirect response to I8048 commands. Input Buffer input buffer is an 8-bit only write register. Buffer driver issued. These contents include commands to I8042, and simply send commands to I8048 through the I8042, and data as command parameters. Control Register also is also referred to as a Controller Command Byte (controller command byte).

It is defined as follows: reserved, should be 0bit6: Translate the second set of scanning code into the first set Bit5: set, prohibit mouse bit4: Set, prohibit keyboard bit3: set, ignore the bit4bit2: Settings in the status register Bit2bit1: Set 1, Enable Mouse Interrupt Bito: Set 1, Enable Keyboard Interrupt 2 Port 0x60,0x64 Driver Put 0x64 Call Command Port Driver Put 0x64 Call Command Port 1.5 command driver directly to i8042 command, You can send the i8048 to i8048 by i8042. Command This part of the content is directly from . 1.5.1 Sending a command to the I8042 to send a command to the keyboard controller to be implemented by writing port 64h, a total of 12 commands, 20h preparing the 8042 chip's Command Byte; its behavior is the content of the current 8042 Command Byte Place in Output Register, the next read operation from the 60H port will read it. 60h is ready to write the 8042 chip's Command Byte; the next byte written by 60h will be placed in Command Byte. A4H Tests whether the keyboard password is set; the test result is placed in Output Register, and then you can read it through 60h. The test results can have two values: FAH = password is set; F1H = no password. A5H Set the keyboard password. The result is placed in the Input Register with a 60H port according to the order. The final password is an empty byte (content is 0). A6H allows the password to take effect. Before publish this command, you must first use the A5H command to set the password. AAH self-test. The diagnostic results are placed in Output Register and can be read through 60h. 55H = OK. ADH prohibits the keyboard interface. The BIT-4 of Command Byte is set. When this command is released, Keyboard will be disabled from sending data to Output Register. AEH opens the keyboard interface. The BIT-4 of Command Byte is cleared. When this command is released, Keyboard will be allowed to send data to Output Register. C0H is ready to read the Input Port. The content of the Input Port is placed in Output Register, which can then be read through the 60H port. D0H is ready to read the Outport port. The result is placed in Output Register and then reads through the 60H port. D1H is ready to write Output port. The byte written by the 60H port will then be placed in Output Port. D2H is ready to write data into Output Register. The byte that is then written to the Input Register via 60h will be placed in Output Register, which is used to simulate data from the keyboard. If the interrupt is allowed, an interrupt will be triggered. 1.5.2 The command issued to 8048 has 10 commands, which sets the LEDs for EDH. Keyboard After receiving this command, a LED setting session begins. Keyboard first replys an ACK (FAH), then wait for the LED setting byte written from the 60H port, if you wait, reply to an ACK again, then set the LED according to this byte. Then wait. . . Until, the LED setting session ends at this time until a non-LED setting byte (high position is set). Diagnose Echo. This command is purely in order to detect whether Keyboard is normal. If it is normal, it will reply to an EEH byte when Keyboard receives this command. F0H Select Scan Code Set. The Keyboard system may have 3 Scan Code Sets.

When KeyBoard receives this command, a ACK will be returned and wait for a Scan Code Set code from the 60H port. The system must be sent to Keyboard a scan code set code after this command. When KeyBoard receives this code, you will reply to an ACK again, and then set the Scan Code Set to the received Scan Code Set code. F2H reads the keyboard ID. Since the 8042 chip is not only able to pick Keyboard. This command is to read the device ID connected after 8042. The device ID is 2 bytes, and the keyboard ID is 83ABH. When the keyboard receives this command, you will first reply to a ACK, then a 2-byte keyboard ID is returned back. F3H Set TypeMatic Rate / Delay. When KeyBoard receives this command, a ACK will be replied. Then wait for a setting byte from 60h. Once received, a ACK will be replied, then the keyboard rate / delay is set to the corresponding value. F4H Clean up the Output Buffer of the keyboard. Once Keyboard receives this command, you will empty the Output Buffer and then reply to an ACK. Then continue to accept keyboard keys. F5H Set the default status (W / Disable). Once Keyboard receives this command, KeyBoard will fully initialize the default state. Previously all the settings of it will be invalid --output buffer, and TypeMatic Rate / Delay is set to the default value. Then reply to an ACK, then wait for the next command. It should be noted that this command is executed, the keyboard keyboard is prohibited. If you want the keyboard to accept the hit button, you must enable Keyboard. F6H Set the default status. The only difference between F5 command is that after this command is executed, the keyboard is allowed to receive. Fehresend. If Keyboard receives this command, you must re-send the data you just sent to 8042 Output Register. When the system detects an error from the keyboard, you can use the self-command to resend the key to the byte that is just sent. FfhReset Keyboard. If Keyboard receives this command, first reply to an ACK, then start your own RESET program, and make your own basic correctness test. After all this, it will return to a single-byte end code (AAH = Success, FCH = FAILED), and set the data of the keyboard to 2.1.5.3 to read the data 00h / ffh When an error is detected when the key or release key is detected, then this byte is placed after Output Bufr, and if Output Buffer is full, the last byte of Output Buffer is replaced with this byte. Using the Scan Code Set 1 using 00h, Scan Code 2, and Scan Code 3 use FFH. Aahbat completes the code. This byte is sent to 8042 Output Register if the keyboard is successful. Eehecho response. Keyboard uses EEH responding to Echo request from 60 hours. F0H is used as the prefix of Break Code in Scan Code Set 2 and Scan Code Set 3. Fahack. When Keyboard receives a legal command or legal data from the 60H port, it replies a FAH. FCHBAT failed code. This byte is sent to 8042 Output Register if the keyboard detection fails. Fehresend.

When KeyBoard receives an illegal command or illegal data from the 60H port, or the parity error of the data replys a feH, which requires the system to resend the relevant command or data. 83ABH When the keyboard receives a F2h command from 60h, it will reply 83h, abh. 83ab is the ID of the keyboard. In addition to those special bytes described above, SCAN CODE is SCAN CODE. 1.6 Port operation first introduces the read and write operation of the port, using the function read_port_uchar in the drive, read_port_uchar, using the CPU read port instruction, in. Use the function write_port_uchar in the drive, write port instructions in Write_Port_uchar, OUT. 1.6.1 Read the status register read the status register, read the 64H port. 1.6.2 Read the data that needs to be read, the I8042's scan code obtained from the I8048, the I8042 command ACK, I8042 Ack from I8048 I8048 command, requires the resend of the resend, some commands that need to return the result The results obtained. When there is a data that needs to be driven, the data is placed in the output buffer while setting the bit0 (OUTPUT_BUFFER_FULL) of the Status Register 1, and the keyboard interrupt (IRQ is 1) is interrupted. Due to the keyboard interrupt, it is performed by the keyboard interrupt service routine provided by the keyboard drive. In the keyboard interrupt service routine, the drive will read the data from the I8042. Once the data read is complete, the bit0 of the status register will be cleared 0. Method of reading data, first, read status registers, determine Bit0, status register bit0 is 1, indicating that there is data in the output buffer. Ensure that the status register bit0 is 1, and then the 60H port is read, read the data. Here we have to talk about the outline of a very useful question, the IRQ mentioned earlier, is an Interrupt Request Line, an interrupt request line, is a hardware line, it is different from the interrupt vector. The interrupt vector is used to find the serial number of the interrupt service routine in the Interrupt Description Table (IDT). The IRQ of the keyboard is 1, the interrupt vector corresponding to the keyboard interrupt service routine is not 1. This will be appreciated. 1.6.3 When the command is sent to I8042 When the command is sent to I8042, the command is placed in the input buffer, and the bit set of the status register is caused to indicate the input buffer, and the bit2 set 1 of the status register is caused. The input buffer is a command. To the i8042, first, read the status register, determine Bit1, and status register bit1 is 0, indicating that the input buffer is empty, and can be written. Ensure that the status register bit1 is 0, then write the 64H port, write the command. 1.6.4 Indirect to I8048 Sending Commands to I8042, I8042 forwards I8048, command is placed in the input buffer, and causes Bit1 set by the status register to indicate the input buffer, and cause the bit2 set of the status register 1 , Means a command to write input buffer. Here we have to pay attention to the I8048 command, by writing a 60H port, and the parameters of the back will also write a 60h port. I8042 How to determine whether the content in the input buffer is a command or a parameter, we discussed together in the parameters of the later command.

转载请注明原文地址:https://www.9cbs.com/read-26131.html

New Post(0)