1. Input Subsystem
1. Input subsystem structure definition
struct input_dev{
const char *name; device name const char *phys; device path in the system const char *uniq; struct input_id id; used to match input hander parameters unsigned long propbit[BITS_TO_LONGS(INPUT_PROP_CNT)];
unsigned long evbit[BITS_TO_LONGS(EV_CNT)]; //Event types supported by the device, mainly EV_SYNC, EV_KEY, EV_REL, EV_ABS, etc.
unsigned long keybit[BITS_TO_LONGS(KEY_CNT)]; //The bitmap corresponding to the key
unsigned long relbit[BITS_TO_LONGS(REL_CNT)]; //Relative coordinates corresponding to the bitmap
unsigned long absbit[BITS_TO_LONGS(ABS_CNT)]; //Absolute coordinates corresponding to the bitmap
unsigned long mscbit[BITS_TO_LONGS(MSC_CNT)];
unsigned long ledbit[BITS_TO_LONGS(LED_CNT)];
unsigned long sndbit[BITS_TO_LONGS(SND_CNT)];
unsigned long ffbit[BITS_TO_LONGS(FF_CNT)];
unsigned long swbit[BITS_TO_LONGS(SW_CNT)];
unsigned int hint_events_per_packet;
unsigned int keycodemax;
unsigned int keycodesize;
void *keycode;
int (*setkeycode)(struct input_dev *dev, const struct input_keymap_entry *ke, unsigned int *old_keycode);
int (*getkeycode)(struct input_dev *dev, struct input_keymap_entry *ke);
struct ff_device *ff;
unsigned int repeat_key;
struct timer_list timer;
int rep[REP_CNT];
struct input_mt_slot *mt;
int mtsize;
int slot;
int trkid;
struct input_absinfo *absinfo;
unsigned long key[BITS_TO_LONGS(KEY_CNT)]; //Key value corresponding to the key
unsigned long led[BITS_TO_LONGS(LED_CNT)]; //LED corresponding indicator light status
unsigned long snd[BITS_TO_LONGS(SND_CNT)];
unsigned long sw[BITS_TO_LONGS(SW_CNT)];
int (*open)(struct input_dev *dev);
void (*close)(struct input_dev *dev);
int (*flush)(struct input_dev *dev, struct file *file);
int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value); //Event processing function, mainly receives commands issued by users, such as lighting up LEDs;
struct input_handle __rcu *grab;
spinlock_t event_lock;
struct mutex mutex;
unsigned int users;
bool going_away;
bool sync;
struct device dev;
struct list_headh_list; //input handle supported by the device;
struct list_headnode;
}; |
Enter device information:
Input device information. The following parameters are mainly used when matching input hander. struct input_id { __u16 bustype; bus type __u16 vendor; manufacturer number __u16 product; Product number __u16 version; version information }; |
2. Input device event processing structure
Data structure for input device event processing: struct input_handler { void *private;
void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
bool (*filter)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
bool (*match)(struct input_handler *handler, struct input_dev *dev);
int (*connect)(struct input_handler *handler, struct input_dev *dev, const struct input_device_id *id); //This function is called when the input device matches the input handler;
void (*disconnect)(struct input_handle *handle);
void (*start)(struct input_handle *handle);
const struct file_operations *fops; //Supported file operation operations;
int minor;
const char *name;
const struct input_device_id *id_table; //All supported input devices;
struct list_headh_list;
struct list_headnode; }; |
3. Link the input_dev and input_handler structures
The data structure connecting input-dev and input handler: struct input_handle { void *private; int open; const char *name; struct input_dev *dev; input dev struct input_handler *handler; input handler struct list_headd_node; struct list_head_node; }; |
4. Registration and deregistration functions
int input_register_device(struct input_dev *dev) int input_unregister_device(struct input_dev *dev) |
5. Driver event support
The device driver tells the input subsystem which events and keys it supports via set_bit(). For example:
set_bit(EV_KEY,button_dev.evbit) Two members in struct input_dev: evbit--event type keybit--key type
Event Type: EV_RST--reset EV_REL--Relative coordinates EV_MSC--Other EV_SND--Sound EV_FF--Force Feedback EV_KEY--Key EV_ABS--Absolute coordinates EV_LED--led EV_REP--repeat
When the event type is EV_KEY, the key type must also be specified: BTN_LEFT--left mouse button BTN_RIGHT--Right mouse button BTN_MIDDLE--Middle mouse button BTN_0--Digital 0 key BTN_1--Number 1 key |
6. Incident Reporting
Void input_event(struct input_dev *dev,unsigned int type,unsigned int code,int value); //Report input events of specified type and code Void input_report_key(struct input_dev *dev,unsigned int code,int value); //Report key value Void input_report_rel(struct input_dev *dev,unsigned int code,int value); //Report relative coordinates Void input_report_abs(struct input_dev *dev,unsigned int code,int value); //Report absolute coordinates Void input_sync(struct input_dev *dev); //Report synchronization events In the touch screen driver design, the entire reporting process of a coordinate and press status is as follows: Input_report_abs(input_dev,ABS_X,x); //X coordinate Input_report_abs(input_dev,ABS_Y,y); //Y坐标 Input_report_abs(input_dev,ABS_PRESSURE,pres); //压力
input_sync(struct input_dev *dev); //同步
structinput_event { struct timeval time; __u16 type; __u16 code; __s32 value; }; |
① code
The event code. If the event type is EV_KEY, the code is the keyboard code of the device. For example, the key code value on the keyboard is 0--127, and the mouse button code is 0x110---0x116, where 0x110 (BTN_LEFT) is the left mouse button, and 0x111 (BTN_RIGHT) is the right mouse button. For the meaning of other codes, refer to the include/linux/input.h file.
②value
The value of the event. If the event type is EV_KEY, the value is 1 when the key is pressed and 0 when it is released.
After the report is completed, input_sync() notifies the system to accept it and tells the input core that the report has ended.
2. Program Analysis
1. Define the input subsystem structure
2. Set the GPIO pin mode and initialize the GPIO interrupt
3. Input subsystem related settings
As shown in the figure:
Line 80 allocates an input_dev structure
Line 82 sets the input subsystem to support key operations
Line 83 sets the supported key type to BTN_0
Line 84 sets the name and initializes the name
Line 86 registers the input subsystem
4. Initialize the timer for anti-jitter
5. When a key operation occurs, enter the interrupt interrupt and start the timer
As shown in the figure, the pin where the interrupt occurs is saved globally, then the timer is started and processed in the timer interrupt function.
6. Timer interrupt function
As shown in the figure, in the timer interrupt function, the key value of the key is obtained, and then the input_report_key function is used to report the event to the system. Then the input_sync function is used to notify the receiver that the report is complete.
7. Log out in the exit function
As shown in the figure, in the exit function, we release the GPIO port and GPIO interrupt we applied for earlier, and then delete the timer.
The next step is to unregister our input subsystem and release the input subsystem structure.
8. Test: Open the device in the application
As shown in the figure, there is event1 under /dev/input/.
9. Read Input Subsystem
As shown in the figure,
We use the read function to read the data of the input subsystem. Note that what we get here is an input_event structure.
Then, if our information is a key press, we print some key information, if it is a notification message, we print the syn event.
10. Compile and test
Attached driver:
1 /******************************
2 linux key_inputSystem
3 *****************************/
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
Previous article:IMX257 Input Subsystem (Part 2) Keyboard Simulation
Next article:IMX257 miscdevice driver
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- The upcoming Circuit Playground Bluefruit
- C5000 Low Power DSP – Tools and Software
- Measuring a signal with an oscilloscope (first time using an oscilloscope)
- [GigaDevice GD32F310 Review] + UART Application
- Designing efficient, powerful, and fast electric vehicle charging stations
- 《GitHub Introduction and Practice》
- EEWorld invites you to disassemble (Issue 7) - Disassemble the weight loss tool and see what is inside the skipping rope
- A new generation of linear and efficient base station power amplifier technology
- I would like to ask how to calculate the gain of the PCB antenna
- Multi-channel RF transceiver clocking for radar and wireless 5G testers