歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> Linux內核

Linux內核input子系統解析

Android、X windows、qt等眾多應用對於linux系統中鍵盤、鼠標、觸摸屏等輸入設備的支持都通過、或越來越傾向於標准的input輸入子系統。

因為input子系統已經完成了字符驅動的文件操作接口,所以編寫驅動的核心工作是完成input系統留出的接口,工作量不大。但如果你想更靈活的應用它,就需要好好的分析下input子系統了。

一、input輸入子系統框架

下圖是input輸入子系統框架,輸入子系統由輸入子系統核心層( Input Core ),驅動層和事件處理層(Event Handler)三部份組成。一個輸入事件,如鼠標移動,鍵盤按鍵按下,joystick的移動等等通過 input driver -> Input core -> Event handler -> userspace 到達用戶空間傳給應用程序。

注意:keyboard.c不會在/dev/input下產生節點,而是作為ttyn終端(不包括串口終端)的輸入。

二、Input driver編寫要點

1、分配、注冊、注銷input設備

struct input_dev *input_allocate_device(void)
        int input_register_device(struct input_dev *dev)
        void input_unregister_device(struct input_dev *dev)

2、設置input設備支持的事件類型、事件碼、事件值的范圍、input_id等信息

參見usb鍵盤驅動:usbkbd.c

usb_to_input_id(dev, &input_dev->id);//設置bustype、vendo、product等
        input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_LED) | BIT(EV_REP);//支持的事件類型
        input_dev->ledbit[0] = BIT(LED_NUML) | BIT(LED_CAPSL) | BIT(LED_SCROLLL) | BIT(LED_COMPOSE) | BIT(LED_KANA);// EV_LED事件支持的事件碼
        for (i = 0; i < 255; i++)
                set_bit(usb_kbd_keycode[i], input_dev->keybit); //EV_KEY事件支持的事件碼

include/linux/input.h中定義了支持的類型(下面列出的是2.6.22內核的情況)

#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

一個設備可以支持一個或多個事件類型。每個事件類型下面還需要設置具體的觸發事件碼。比如:EV_KEY事件,需要定義其支持哪些按鍵事件碼。

3、如果需要,設置input設備的打開、關閉、寫入數據時的處理方法

參見usb鍵盤驅動:usbkbd.c

input_dev->open = usb_kbd_open;
        input_dev->close = usb_kbd_close;
        input_dev->event = usb_kbd_event;

4、在發生輸入事件時,向子系統報告事件

用於報告EV_KEY、EV_REL、EV_ABS等事件的函數有:

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)

如果你覺得麻煩,你也可以只記住1個函數(因為上述函數都是通過它實現的)

void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)

三、Event Handler層解析

1、Input輸入子系統數據結構關系圖

2、input_handler結構體

以evdev.c中的evdev_handler為例:

static struct input_handler evdev_handler = {
                .event = evdev_event, //向系統報告input事件,系統通過read方法讀取
                .connect = evdev_connect, //和input_dev匹配後調用connect構建
                .disconnect = evdev_disconnect,
                .fops = &evdev_fops, //event設備文件的操作方法
                .minor = EVDEV_MINOR_BASE, //次設備號基准值
                .name = "evdev",
                .id_table = evdev_ids, //匹配規則
        };

3、input字符設備注冊過程

drivers/input/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定義:

static const struct file_operations input_fops = {
                .owner = THIS_MODULE,
                .open = input_open_file,
        };

Input_dev和input_handler匹配後調用input_handler的connect。以evdev_handler為例:

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 = kzalloc(sizeof(struct evdev), GFP_KERNEL);//為每個匹配evdev_handler的設備創建一個evdev。
                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;//記錄evdev的位置,字符設備/dev/input/evnetx訪問時根據次設備號及EVDEV_MINOR_BASE最終在evdev_open中找到對應的evdev
                devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
                cdev = class_device_create(&input_class, &dev->cdev, devt,dev->cdev.dev, evdev->name);//創建了event字符設備節點
                ……

        }

4、input字符設備的打開過程

static int input_open_file(struct inode *inode, struct file *file)
        {
                struct input_handler *handler = input_table[iminor(inode) >> 5];
                //得到對應的input_handler
                const struct file_operations *old_fops, *new_fops = NULL;
                int err;
                if (!handler || !(new_fops = fops_get(handler->fops)))
                //取出對應input_handler的file_operations
                        return -ENODEV;
                if (!new_fops->open) {
                        fops_put(new_fops);
                        return -ENODEV;
                }
                old_fops = file->f_op;
                file->f_op = new_fops;//重定位打開的設備文件的操作方法
                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;
        }

5、input字符設備的其它操作

由於在open階段已經把設備文件的操作操作方法重定位了到了具體的input_handler,所以其它接口操作(read、write、ioctl等),由各個input_handler的fops方法決定。如evdev.c中的:evdev_fops。

Copyright © Linux教程網 All Rights Reserved