Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Linux 驅動子系統之輸入子系統(Input)全解析

[內容概要]

本文介紹 input 子系統的三大組成部分,並對其核心結構體與關鍵函數逐一剖析,闡明它們之間的關聯。

[概述]

內核的輸入子系統,是對各類分散且形態各異的輸入設備(如鍵盤、鼠標、跟蹤球、操作桿、觸摸屏、加速計和手寫板等)進行統一處理的驅動框架。它帶來的好處主要有三:

統一了物理形態各異卻功能相似的輸入設備的處理方式。例如各種鼠標,無論是 PS/2、USB 還是藍牙,都能以相同的方式處理;

提供了簡潔的事件接口,用於把輸入報告分發給用戶空間的應用程序;

抽取出了輸入驅動的通用部分,既簡化了驅動開發,又引入了一致性。

[input 子系統架構圖]

[input-core 核心層]

核心層對下向設備驅動層提供編程接口,對上則向事件處理層提供編程接口。

[相關文件]

drivers/input/input.c

include/linux/input.h

[關鍵的數據結構]

struct input_dev——物理輸入設備的基本數據結構,保存著設備的相關信息:

struct input_dev {
     const char *name;
     const char *phys;
     const char *uniq;
     struct input_id id;
     unsigned long evbit[BITS_TO_LONGS(EV_CNT)];
     unsigned long keybit[BITS_TO_LONGS(KEY_CNT)];
     unsigned long relbit[BITS_TO_LONGS(REL_CNT)];
     unsigned long absbit[BITS_TO_LONGS(ABS_CNT)];
     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)];
     ……
     struct device dev;
     struct list_head      h_list;
     struct list_head      node;
};

Name:輸入設備的名字;

Evbit:事件支持的位圖;

Keybit:按鍵支持的位圖;

Dev:內嵌的設備結構體;

h_list:鏈表,表示與該 input_dev 相聯繫的 handler;

node:鏈表,將此 input_dev 掛接到全局的 input_dev_list 上,內核中所有的 input_dev 都連接於此。

struct input_handler——事件處理結構體,定義了處理事件的方法:

struct input_handler {
     void *private;
     void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
     int (*connect)(struct input_handler *handler, struct input_dev *dev, const struct input_device_id *id);
     void (*disconnect)(struct input_handle *handle);
     void (*start)(struct input_handle *handle);
     const struct file_operations *fops;
     int minor;
     const char *name;
     const struct input_device_id *id_table;
     const struct input_device_id *blacklist;
     struct list_head      h_list;
     struct list_head      node;
};

event() 函數被輸入子系統用於處理發送給設備的事件;

connect() 用來把 handler 與 input_dev 連接起來;

h_list:鏈表,表示與該 input_handler 相聯繫的 handle;

node:鏈表,將此 input_handler 掛接到全局的 input_handler_list 上,內核中所有的 input_handler 都連接於此。

struct input_handle——用來建立 input_dev 與 input_handler 之間關聯的結構體:

struct input_handle {
     void *private;
     int open;
     const char *name;
     struct input_dev *dev;
     struct input_handler *handler;
     struct list_head      d_node;
     struct list_head      h_node;
};

d_node:把 handle 放入設備相關的鏈表,即 input_dev->h_list 所表示的鏈表;

h_node:把 handle 放入 handler 相關的鏈表,即 input_handler->h_list 所表示的鏈表。

[子系統初始化函數]

static int __init input_init(void)
{
     int err;
     input_init_abs_bypass();
     err = class_register(&input_class);
     if (err) {
             printk(KERN_ERR "input: unable to register input_dev class\n");
             return err;
     }
     err = input_proc_init();
     if (err)
             goto fail1;
     err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
     if (err) {
             printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
             goto fail2;
     }
     return 0;
fail2:         input_proc_exit();
fail1:         class_unregister(&input_class);
     return err;
}

這個初始化函數主要完成三件事:

1、class_register(&input_class):注册一個 input 類;

2、input_proc_init():在 proc 下建立相關的交互文件;

3、register_chrdev(INPUT_MAJOR, "input", &input_fops):將其注册為字符設備。

[file_operations]

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

你也許會疑惑,為什麼 file_operations 中只實現了 open 方法?我們來看看 input_open_file 的源碼:

static int input_open_file(struct inode *inode, struct file *file)
{
     struct input_handler *handler;
     const struct file_operations *old_fops, *new_fops = NULL;
     int err;
     lock_kernel();
     handler = input_table[iminor(inode) >> 5];
     if (!handler || !(new_fops = fops_get(handler->fops))) {
             err = -ENODEV;
             goto out;
     }
     if (!new_fops->open) {
             fops_put(new_fops);
             err = -ENODEV;
             goto out;
     }
     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);
out:      unlock_kernel();
     return err;
}

這個 open 方法完成了兩件事:

1、把 file->fops 重定向到 input_handler->fops。由於 input_handler->fops 定義了對具體事件的操作方法,通過 file->fops,我們便能對不同設備進行操作;

2、調用 input_handler->fops 的 open 方法來打開設備。

[input device drivers 設備驅動層]

驅動層負責對硬件各寄存器進行讀寫訪問,並把底層硬件對用戶輸入的響應,轉換為標準的輸入事件,再通過核心層提交給事件處理層。

[相關文件]

drivers/input/mouse

drivers/input/touchscreen

drivers/input/joystick

……

[input_dev]

struct input_dev 代表一個輸入設備,是驅動層的主要結構體,我們所做的工作,本質上就是填充這個結構體。

[分配一個輸入設備]

struct input_dev *input_allocate_device(void)

[注册與注銷]

int input_register_device(struct input_dev *dev)

void input_unregister_device(struct input_dev *dev)

[input_register_device 分析]

int input_register_device(struct input_dev *dev)
{
     static atomic_t input_no = ATOMIC_INIT(0);
     struct input_handler *handler;
     const char *path;
     int error;
     __set_bit(EV_SYN, dev->evbit);   /* support all event */
     init_timer(&dev->timer);
     if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
             dev->timer.data = (long) dev;
             dev->timer.function = input_repeat_key;
             dev->rep[REP_DELAY] = 250;
             dev->rep[REP_PERIOD] = 33;
     }
     if (!dev->getkeycode)
             dev->getkeycode = input_default_getkeycode;
     if (!dev->setkeycode)
             dev->setkeycode = input_default_setkeycode;
     dev_set_name(&dev->dev, "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
     error = device_add(&dev->dev);
     if (error)
             return error;
     path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
     printk(KERN_INFO "input: %s as %s\n", dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
     kfree(path);
     error = mutex_lock_interruptible(&input_mutex);
     if (error) {
             device_del(&dev->dev);
             return error;
     }
     list_add_tail(&dev->node, &input_dev_list);
     list_for_each_entry(handler, &input_handler_list, node)
             input_attach_handler(dev, handler);
     input_wakeup_procfs_readers();
     mutex_unlock(&input_mutex);
     return 0;
}

Input_register_device 函數主要做了兩件事:

1、通過 device_add(&dev->dev),把 input_dev 內嵌的 device 結構注册到 Linux 設備模型中;

2、調用 list_add_tail 函數,將 input_dev 加入 input_dev_list 鏈表,而這條鏈表包含了系統中所有的 input_dev 設備。

其中,list_for_each_entry 函數用來遍歷 input_handler_list;input_attach_handler 函數則用來匹配 input_dev 與 handler,只有匹配成功,才會調用 connect 函數,從而把 input_dev 與 handler 關聯起來。

[input event driver 事件處理層]

input event driver 也就是 event handler,即事件處理層,它為用戶空間的應用程序提供了統一訪問設備的接口,並負責處理驅動層提交上來的事件。

[相關文件]

drivers/input/evdev.c

drivers/input/joydev.c

drivers/input/mousedev.c

……

[注册 input_handler]

int input_register_handler(struct input_handler *handler)
{
     struct input_dev *dev;
     int retval;
     retval = mutex_lock_interruptible(&input_mutex);
     if (retval)
             return retval;
     INIT_LIST_HEAD(&handler->h_list);
     if (handler->fops != NULL) {
             if (input_table[handler->minor >> 5]) {
                 retval = -EBUSY;
                 goto out;
             }
             input_table[handler->minor >> 5] = handler;
     }
     list_add_tail(&handler->node, &input_handler_list);
     list_for_each_entry(dev, &input_dev_list, node)
             input_attach_handler(dev, handler);
     input_wakeup_procfs_readers();
out:      mutex_unlock(&input_mutex);
     return retval;
}

這裡注册了一個新的 input_handler 處理器:

list_add_tail(&handler->node, &input_handler_list) 把 handler 加入到全局鏈表 input_handler_list 中;

list_for_each_entry 函數用來遍歷 input_dev_list;

input_attach_handler 函數用來匹配 input_dev 與 handler,只有匹配成功,才會調用 connect 函數,把二者關聯起來。

[input_register_handle]

int input_register_handle(struct input_handle *handle)
{
     struct input_handler *handler = handle->handler;
     struct input_dev *dev = handle->dev;
     int error;
     error = mutex_lock_interruptible(&dev->mutex);
     if (error)
             return error;
     list_add_tail_rcu(&handle->d_node, &dev->h_list);
     mutex_unlock(&dev->mutex);
     list_add_tail(&handle->h_node, &handler->h_list);
     if (handler->start)
             handler->start(handle);
     return 0;
}

Input_register_handle 的主要工作,是把 input_dev 與 input_handler 關聯起來,這與「設備—驅動—總線」的驅動模型非常相似。

[input_dev、handler 與 handle 三者的關系]

Input_register_device、input_register_handler 與 input_register_handle 三者之間的關系,在下圖中有很好的體現:

Copyright © Linux教程網 All Rights Reserved