Linux input subsystem

Source: Internet
Author: User

The input device (buttons, keyboard, touch screen, and mouse) is a typical character device, working mechanism is the underlying
When buttons, touch, and other actions occur, an interrupt occurs (or the timer query of the driver timer), and then the CPU
Reads key-value, coordinate, and other data through SPI, I2C, or external memory bus.
The input core layer provides the APIS required for the underlying input device driver, such as allocating/releasing an input device.
Struct input_dev * input_allocate_device (void );
Void input_free_device (struct input_dev * Dev );
Input_allocate_device () returns an input_dev struct, which is used to characterize one input device.
Interfaces used to register or deregister an input device
Input_register_device (struct input_dev *);
Void input_unregister_device (struct input_dev *);
Interface for reporting input events, specifying type, code, value
Void input_event (struct input_dev * Dev, unsigned int type, unsigned int code, int value );

Input Common Data Structure
1. input_dev is the basic device structure of the input device. Each input driver must be allocated with the initialization structure.
Include/Linux/input. h
Struct input_dev {
Const char * Name; // device name
Const char * phys; // physical path of the device in the system
Const char * uniq; // unified ID
Struct input_id ID; // device ID

Unsigned long evbit [bits_to_longs (ev_cnt)]; // event
Unsigned long keybit [bits_to_longs (key_cnt)]; // press the key
Unsigned long relbit [bits_to_longs (rel_cnt)]; // relative Device
Unsigned long absbit [bits_to_longs (abs_cnt)]; // absolute Device
Unsigned long mscbit [bits_to_longs (msc_cnt)]; // miscellaneous Device
Unsigned long ledbit [bits_to_longs (led_cnt)]; // led
Unsigned long sndbit [bits_to_longs (snd_cnt)]; // sound Device
Unsigned long ffbit [bits_to_longs (ff_cnt)]; // Force Feedback Device
Unsigned long swbit [bits_to_longs (sw_cnt)]; // switch the device

Unsigned int keycodemax; // maximum value of the key code
Unsigned int keycodesize; // key code size
Void * keycode; // key code
INT (* setkeycode) (struct input_dev * Dev,
Unsigned int scancode, unsigned int keycode );
INT (* getkeycode) (struct input_dev * Dev,
Unsigned int scancode, unsigned int * keycode );

Struct ff_device * ff;

Unsigned int repeat_key;
Struct timer_list timer;

Int sync;

Int ABS [abs_cnt];
Int rep [rep_max + 1];

Unsigned long key [bits_to_longs (key_cnt)];
Unsigned long led [bits_to_longs (led_cnt)];
Unsigned long snd [bits_to_longs (snd_cnt)];
Unsigned long SW [bits_to_longs (sw_cnt)];

Int absmax [abs_cnt];
Int absmin [abs_cnt];
Int absfuzz [abs_cnt];
Int absflat [abs_cnt];
Int absres [abs_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 );

Struct input_handle * Grab;

Spinlock_t event_lock;
Struct mutex;

Unsigned int users;
Bool going_away;

Struct device dev;

Struct list_headh_list;
Struct list_headnode;
};

2. The input_event driver layer reports the event function to the input subsystem core.
Void input_event (struct input_dev * Dev,
Unsigned int type, unsigned int code, int value );
 
Reports of different device content are completed using the input_event function. Different parameters are selected.
@ Type: type (ev_key keyboard, relative to ev_rel, absolute ev_abs)
@ Code: Code (Event code, such as keyboard code)
@ Value: Value (ev_key: when the key is pressed, the value is 1 and the release value is 0. If the event is ev_rel, the value is positive.
Values and negative values represent two different directions respectively)

The keyboard belongs to the key device ev_key.
The trajectory belongs to the relative device ev_rel.
The touch screen is an absolute device ev_abs.

3. Allocate and initialize input_dev
Input_allocate_device ()

4. register an input device with the kernel
Input_register_device ()

5. input-driven process
1) define the input device structure
Struct input_dev * input;
2) Allocate and initialize the input_dev Interface
Input = input_allocate_device ();
Input-> name = "gpio-keys ";
Input-> phys = "gpio-keys/input0 ";
Input-> ID. bustype = bus_host;
Input-> ID. Vendor = 0x0001;
Input-> ID. Product = 0x0001;
Input-> ID. Version = 0x0100;
3) record the events that the device is interested in (and process them) input_set_capability ();
Define/Drivers/input. c
Void input_set_capability (struct input_dev * Dev, unsigned int type, unsigned int code)
Input_set_capability (input, ev_key, 101 );

4) register an input device with the kernel
Input_register_device (input );

5) record the button status
Int state = (gpio_get_value (initi64xx_gpn (0 ))? 1: 0) ^ 1;

6) Report input events
Input_event (input, ev_key, 101 ,!! State );

7) wait until the input event is processed
Input_sync (input );

8) cancel an input device.
Input_unregister_device (input );

/*********************** Input gpio_keys example ************ ********/

/*** Interrupt key kernel 2.6.35.7 */# include <Linux/module. h> # include <Linux/init. h> # include <Linux/interrupt. h> # include <Linux/Fs. h> # include <Mach/gpio. h> # include <Linux/input. h> # include <Linux/slab. h> # include <Linux/delay. h> # include <Linux/Timer. h> # define key_major 233 # define device_name "gpio_key" struct gpio_button_data {struct input_dev * input; struct timer_list timer; struct work_struct work;} * d ATA; static void gpio_keys_report_event (void) {struct input_dev * input = data-> input; // records the int state of the key state = (gpio_get_value (initi64xx_gpn (0 ))? 1: 0) ^ 1; // report the input event unsigned int type = ev_key; int code = 139; printk ("type = % d, code = % d, state = % d \ n ", type, code, State);/** 5. report events */input_event (input, type, code ,!! State); // wait for the input event to complete input_sync (input);} static void gpio_key_work_func (struct work_struct * Work) {// struct gpio_button_data * Data = // container_of (work, struct gpio_button_data, work); Evaluate ();} static void gpio_keys_timer (unsigned long _ DATA) {schedule_work (& Data-> work);} static irqreturn_t gpio_key_isr (int irq, void * dev_id) {mod_timer (& Data-> timer, jiffies + msecs_to_jiffies (0); schedule_work (& Data-> work); Return irq_handled;} static int keys_init (void) {int ret = 0; int IRQ; // interrupt number unsigned long irqflags; setup_timer (& Data-> timer, gpio_keys_timer, (unsigned long) data ); add_timer (& Data-> timer); // apply for Pin gpio_request (initi64xx_gpn (0), "home"); // set it to input gpio_direction_input (initi64xx_gpn (0 )); IRQ = gpio_to_irq (initi64xx_gpn (0); printk ("The gpio_key IRQ is [% d] \ n", IRQ); irqflags = kernel | kernel; ret = request_irq (IRQ, gpio_key_isr, irqflags, "home", null); If (RET) {printk ("can't get gpio IRQ \ n"); Return-1;} return 0 ;} static int gpio_keys_probe (void) {int ret = 0; // struct gpio_button_data * data;/** 1. define the input_dev structure */struct input_dev * input;/** 2. allocate and initialize the structure */input = input_allocate_device (); input-> name = "gpio-key"; input-> phys = "gpio-key/input0 "; input-> ID. bustype = bus_host; input-> ID. vendor = 0x0001; input-> ID. product = 0x0001; input-> ID. version = 0x0100; Data = kzarloc (sizeof (struct gpio_button_data), gfp_kernel); data-> input = input; keys_init ();/** 3. events of Interest */input_set_capability (input, ev_key, 139);/** 4. register an input device */ret = input_register_device (input) to the kernel; If (RET) {printk ("unable to register input device \ n"); Return-1 ;} return 0;} int key_ioctl (struct inode * inode, struct file * filp, unsigned int cmd, unsigned long Arg) {return 0;} static const struct file_operations key_ops = {. owner = this_module ,. IOCTL = key_ioctl,}; static int _ init gpio_keys_init (void) {int ret = 0; ret = register_chrdev (key_major, device_name, & key_ops); If (Ret <0) {printk ("can't register gpio_keys_number \ n"); Return-1;} gpio_keys_probe (); init_work (& Data-> Work, gpio_key_work_func ); printk ("gpio_keys init \ n"); Return 0;} static void _ exit gpio_keys_exit (void) {unregister_chrdev (key_major, device_name);} module_init (gpio_keys_init ); module_exit (gpio_keys_exit); module_license ("GPL ");

Test in serial port

/# Getevent

Add device 2:/dev/input/event0
Name: "gpio-key"
Cocould not get driver version for/dev/input/mice, not a typewriter
Type = 1, code = 139, state = 1
Type = 1, code = 139, state = 1
/Dev/input/event0: 0001 008b 00000001
/Dev/input/event0: 0000 0000 00000000

0001 indicates ev_key, 008b is the key scan code, 1 Press, 0 lift

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.