Linux system provides the input subsystem, keys, touch screen, keyboard, mouse and other input can use input interface function to achieve device-driven, so the 12.1~12.2 section of the button and touch screen device driver can be used as input device driver to achieve.
In the Linux kernel, the input device is described by Input_dev structure, and the input subsystem is used to drive, the core work is to report the key, touch screen, keyboard, mouse and other input events to the system (event, through the input_event structure Body description), you no longer need to care about the file operation interface, because the input subsystem has completed the file operation interface. Drive-reported events eventually reach user space through Inputcore and EventHandler.
Through the input subsystem, the specific input device driver only needs to complete the following work.
L tells the input subsystem what events it can report in the module load function.
The device driver tells the input subsystem what events it supports by Set_bit (), as follows:
Set_bit (Ev_key, button_dev.evbit);
L REGISTERS the input device in the module load function.
The function to register an input device is:
int Input_register_device (struct Input_dev
*dev);
L reports events and corresponding key values/coordinates by Input_ report_xxx () when the key is pressed/lifted, touch-screen is touched/lifted/moved, mouse is moved/clicked/raised.
The main event types include Ev_key (key events), Ev_rel (relative values, such as cursor movement, report relative to last position offset), and ev_abs (absolute value, such as touch screen and joystick, which work in absolute coordinate systems).
The functions used to report Ev_key, Ev_rel, and Ev_abs events are:
void Input_report_key (struct input_dev
*dev, unsigned int code, int value);
void Input_report_rel (struct input_dev
*dev, unsigned int code, int value);
void Input_report_abs (struct input_dev
*dev, unsigned int code, int value);
Input_sync () is used for event synchronization, and it informs the event that the recipient driver has issued a complete report.
For example, in a touch-screen device driver, the entire reporting process for a single coordinate and pressed state is as follows:
Input_report_abs (Input_dev
, abs_x, X); X coordinates
Input_report_abs (Input_dev
, abs_y, Y); Y-coordinate
Input_report_abs (Input_dev
, Abs_pressure, pres); Pressure
Input_sync (Input_dev
); Synchronous
Unregister the input device in the module unload function. The function to unregister an input device is:
void Input_unregister_device (struct input_dev
*dev);
Code Listing 12.28 gives an example of the simplest use of the input interface to implement a key device driver, which reports keystrokes and synchronization events to the system in the Interrupt service program.
Code Listing 12.28 The simplest input device driver
1/* Report the event in the key interrupt * *
2 static void Button_interrupt (int irq, void *dummy, struct pt_regs *fp)
3 {
4 Input_report_key (&button_dev, btn_1, INB (button_port) &1);
5 Input_sync (&button_dev);
6}
7
8 static int _ _init button_init (void)
9 {
10/* Application Interrupt * *
One if (Request_irq (BUTTON_IRQ, button_interrupt, 0, "button", NULL))
12 {
PRINTK (kern_err "Button.c:can ' t allocate IRQ%d/n", BUTTON_IRQ);
Return-ebusy;
15}
16
Button_dev.evbit[0] = BIT (Ev_key); Support for Ev_key events
Button_dev.keybit[long (btn_0)] = BIT (BTN_0);
19
Input_register_device (&button_dev); Register input Device
21}
22
% static void _ _exit button_exit (void)
24 {
Input_unregister_device (&button_dev); Logout input Device
FREE_IRQ (BUTTON_IRQ, button_interrupt); Release interrupts
27}
This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/wwwxxlby520/archive/2009/07/20/4363696.aspx