【usb】linux kernel USB keyboard driver analysis--common key value reporting and conversion

1. Overview

Two, explore

  • The range of the for loop variable i is 2-7, because the common key value is stored in the 2-7th element of the array. The first one stores special key values, and the second one is reserved.
  • kbd->old[i] > 3This judgment is because the key values ​​0-3 are reserved and have no corresponding physical keys. So we don't need to pay attention. See section 10 of hut1_4 for specific instructions .
		if (kbd->old[i] > 3 && memscan(kbd->new + 2, kbd->old[i], 6) == kbd->new + 8) {
    
    
			if (usb_kbd_keycode[kbd->old[i]])
				input_report_key(kbd->dev, usb_kbd_keycode[kbd->old[i]], 0);
  • The above code, first of all, the function of memscan is to find a character in a piece of memory, that is, kbd->new + 2to find a character in this piece of memory kbd->old[i]. 6 indicates that kbd->new + 2the size of this memory is 6. If found, return the character address, otherwise return the end address of the memory block + 1.
  • So what this code means is to find the old key value in the newly reported key value. If it is not found and the if (usb_kbd_keycode[kbd->old[i]])old key value is a valid key value, then report that the old key value has been released (the key has been released /lift).
if (kbd->new[i] > 3 && memscan(kbd->old + 2, kbd->new[i], 6) == kbd->old + 8) {
    
    
			if (usb_kbd_keycode[kbd->new[i]])
				input_report_key(kbd->dev, usb_kbd_keycode[kbd->new[i]], 1);
  • The logic of this code is similar to the one above. Look for the new key value in the old key value. If the new key value is not found and the new key value is valid, then report that the new key value is pressed .

3. Summary

  • kbd->oldThe key value reported last time is saved in , and the kbd->newkey value reported this time is in . If a key value was reported last time, but not reported this time, it means that the key has been released, so report the old release event of the case. If a button was not reported last time but it is reported this time, it means that the button was pressed this time, so the button press event should be reported.

4. References

hut1_4
memscan(9) — linux-manual-4.8

Guess you like

Origin blog.csdn.net/C2681595858/article/details/129783857