Linux Kernel Input subsystem analysis

Source: Internet
Author: User
Linux Kernel Input subsystem analysis

8422 people read comments (16) collect reports

Author: Liu Hongtao,Hua Qing vision embedded college lecturer.

Android, X Windows, QT, and many other applications support input devices, such as the keyboard, mouse, and touch screen, in Linux systems, or increasingly prefer the standard input subsystem.

Because the input subsystem has completed the character-driven file operation interface, the core task of writing the driver is to complete the interface set aside by the input system, with little workload. But if you want to apply it more flexibly, You need to analyze the input subsystem.

I. Input subsystem framework

It is the framework of the input subsystem. The input subsystem consists of three parts: the input core, the driver, and the event handler. An input event, such as moving the mouse, pressing the keyboard, and moving joystick, arrives at the user space through input driver-> input core-> event handler-> userspace and passes it to the application.Program.

Note: keyboard. C does not generate nodes under/dev/input, but serves as the input of the ttyn terminal (excluding the serial port terminal.

II. Key Points of writing input driver

1, Allocating, registering, and canceling input devices

Struct input_dev * input_allocate_device (void)

Int input_register_device (struct input_dev * Dev)

Void input_unregister_device (struct input_dev * Dev)

2Set the event type, Event code, event value range, input_id, and other information supported by the input device.

See USB keyboard driver: usbkbd. c

Usb_to_input_id (Dev, & input_dev-> ID); // you can specify bustype, vendo, and product.

Input_dev-> evbit [0] = bit (ev_key) | bit (ev_led) | bit (ev_rep); // supported event types

Input_dev-> ledbit [0] = bit (led_numl) | bit (led_capsl) | bit (led_scrolll) | bit (led_compose) | bit (led_kana ); // event code supported by the ev_led event

For (I = 0; I <255; I ++)

Set_bit (usb_kbd_keycode [I], input_dev-> keybit); // event code supported by the ev_key event

Include/Linux/input. h defines the supported types (the 2.6.22 kernel is listed below)

# Define ev_syn 0x00

# Define ev_key 0x01

# Define ev_rel 0x02

# Define ev_abs 0x03

# Define ev_msc 0x04

# Define ev_sw 0x05

# Define ev_led 0x11

# Define ev_snd 0x12

# Define ev_rep 0x14

# Define ev_ff 0x15

# Define ev_pwr 0x16

# Define ev_ff_status 0x17

# Define ev_max 0x1f

One device supports one or more event types. You also need to set the trigger event code for each event type. For example, for an ev_key event, you need to define which key event codes are supported.

3Set the processing method when the input device is enabled, disabled, or written to data if needed.

See USB keyboard driver: usbkbd. c

Input_dev-> open = usb_kbd_open;

Input_dev-> close = usb_kbd_close;

Input_dev-> event = usb_kbd_event;

4When an input event occurs, report the event to the subsystem

Functions used to report events such as ev_key, ev_rel, and ev_abs include:

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)

If you are in trouble, you can remember only one function (because the above functions are implemented through it)

Void input_event (struct input_dev * Dev, unsigned int type, unsigned int code, int value)

Iii. event handler layer Parsing

1, InputInput subsystem Data Structure Diagram

2, Input_handler struct

Take evdev_handler in evdev. C as an example:

Static struct input_handler evdev_handler = {

. Event = evdev_event, // report the input event to the system. The system reads the event using the read method.

. Connect = evdev_connect, // matches with input_dev and calls connect to build

. Disconnect = evdev_disconnect,

. Fops = & evdev_fops, // operation method of the event Device File

. Minor = evdev_minor_base, // reference value of the sub-device number

. Name = "evdev ",

. Id_table = evdev_ids, // matching rule

};

3, Input character device registration process

Drivers/input. C:

Static int _ init input_init (void)

{

Int err;

Err = class_register (& input_class );

......

Err = register_chrdev (input_major, "input", & input_fops );

......

}

Input_fops definition:

Static const struct file_operations input_fops = {

. Owner = this_module,

. Open = input_open_file,

};

After input_dev matches with input_handler, call input_handler's connect. Take evdev_handler as an example:

Static int evdev_connect (struct input_handler * Handler, struct input_dev * Dev,

Const struct input_device_id * ID)

{

Struct evdev * evdev;

Struct class_device * cdev;

Dev_t devt;

Int minor;

Int error;

For (minor = 0; minor <evdev_minors & evdev_table [minor]; minor ++ );

If (minor = evdev_minors ){

Printk (kern_err "evdev: no more free evdev devices/N ");

Return-enfile;

}

Evdev = kzarloc (sizeof (struct evdev), gfp_kernel); // create an evdev for each device that matches evdev_handler.

If (! Evdev)

Return-enomem;

Init_list_head (& evdev-> client_list );

Init_waitqueue_head (& evdev-> wait );

Evdev-> exist = 1;

Evdev-> minor = minor;

Evdev-> handle. Dev = dev;

Evdev-> handle. Name = evdev-> name;

Evdev-> handle. Handler = handler;

Evdev-> handle. Private = evdev;

Sprintf (evdev-> name, "event % d", minor );

Evdev_table [minor] = evdev; // records the position of evdev. When the character device/dev/input/evnetx is accessed, the corresponding evdev is found in evdev_open according to the device ID and evdev_minor_base.

Devt = mkdev (input_major, evdev_minor_base + minor ),

Cdev = class_device_create (& input_class, & Dev-> cdev, devt,

Dev-> cdev. Dev, evdev-> name );//An event character device node is created.

......

}

4, Input character device opening process

Static int input_open_file (struct inode * inode, struct file * file)

{

Struct input_handler * Handler = input_table [iminor (inode)> 5];

// Obtain the corresponding input_handler

Const struct file_operations * old_fops, * new_fops = NULL;

Int err;

If (! Handler |! (New_fops = fops_get (Handler-> FoPs )))

// Retrieve the file_operations corresponding to input_handler

Return-enodev;

If (! New_fops-> open ){

Fops_put (new_fops );

Return-enodev;

}

Old_fops = file-> f_op;

File-> f_op = new_fops; // Operation Method for relocating opened Device Files

Err = new_fops-> open (inode, file );

If (ERR ){

Fops_put (file-> f_op );

File-> f_op = fops_get (old_fops );

}

Fops_put (old_fops );

Return err;

}

5Other operations on the input character device

Since the operation methods of the device files have been relocated to the specific input_handler in the open stage, other interface operations (such as read, write, and IOCTL) are determined by the FoPs methods of each input_handler. For example, evdev_fops in evdev. C.

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.