Linux 的輸入子系統把「硬件動作」轉化成「標準化事件」,整條鏈路可以概括為:用戶操作輸入設備 → 硬件產生中斷 → CPU 透過總線或 I/O 端口把數據搬進內核緩衝區 → 各層驅動逐級封裝 → 最終送進用戶空間。
理解這條鏈路,是读懂 Input 子系統的起點:中斷只負責「通知」,真正的數據搬運與事件封裝,是由設備驅動與事件處理層分工完成的。
app
-------------------- input_event(用戶空間讀取的事件)
-------------------- input_core(核心層:註冊、匹配、事件分發)
-------------------- input_device_driver(設備驅動:完善 input_dev 並上報)
-------------------- hardware(中斷、定時器、GPIO 等硬件相關代碼)
核心層 input_core 是樞紐:它維護設備鏈表與 handler 鏈表,負責兩者匹配,並把設備驅動上報的事件路由到對應的 handler。
Input 子系統的四個核心結構分別是 input_dev(設備)、input_handler(事件處理者)、input_handle(兩者的連接)與 input_event(上報的事件本身)。
/* 輸入設備對象 */
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)];
unsigned int keycodemax;
unsigned int keycodesize;
void *keycode;
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;
/* ...... */
struct device dev;
struct list_head h_list;
struct list_head node;
};
/* 描述一類事件的處理者 */
struct input_handler {
void *private;
/* 設備驅動報告的事件最終由這個函數處理 */
void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
bool (*filter)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
/* 用來把 handler 和 input_dev 關聯起來 */
bool (*match)(struct input_handler *handler, struct input_dev *dev);
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;
struct list_head h_list;
struct list_head node;
};
/* 設備與 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;
};
/* 上報給用戶空間的事件 */
struct input_event {
struct timeval time; /* 事件發生的時間戳 */
__u16 type; /* 事件類型 */
__u16 code; /* 事件代碼 */
__s32 value; /* 事件值:坐標偏移、1 表示按下、0 表示釋放 */
};
Input 子系統對外以主設備號 13 的字符設備呈現,用戶空間打開 /dev/eventX 時,內核會根據次設備號找到對應的 handler,再把文件操作集「掉包」成 handler 自己的 fops。
#define INPUT_MAJOR 13
static int __init input_init(void)
{
err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
/* ...... */
}
static const struct file_operations input_fops = {
.owner = THIS_MODULE,
.open = input_open_file,
};
static int input_open_file(struct inode *inode, struct file *file)
{
struct input_handler *handler;
handler = input_table[iminor(inode) >> 5]; /* 由次設備號取出 handler */
if (handler)
new_fops = fops_get(handler->fops); /* 取得 handler 的 fops */
/* ...... */
old_fops = file->f_op;
file->f_op = new_fops;
}
那麼 input_table 是在哪裡被填上的?答案在 input_register_handler():
int input_register_handler(struct input_handler *handler)
{
if (handler->fops != NULL) {
if (input_table[handler->minor >> 5]) {
retval = -EBUSY;
goto out;
}
input_table[handler->minor >> 5] = handler;
}
/* 後續:遍歷 input_dev_list,逐一嘗試掛接 */
list_for_each_entry(dev, &input_dev_list, node)
input_attach_handler(dev, handler);
}
內核中現成的 handler 有多個,分別由各自的初始化函數註冊:
joydev.c:input_register_handler(&joydev_handler)keyboard.c:input_register_handler(&kbd_handler)mousedev.c:input_register_handler(&mousedev_handler)evdev.c:input_register_handler(&evdev_handler)static int __init evdev_init(void)
{
return input_register_handler(&evdev_handler);
}
static struct input_handler evdev_handler = {
.event = evdev_event,
.connect = evdev_connect,
.disconnect = evdev_disconnect,
.fops = &evdev_fops,
.minor = EVDEV_MINOR_BASE, /* 64 */
.name = "evdev",
.id_table = evdev_ids,
};
static const struct file_operations evdev_fops = {
.owner = THIS_MODULE,
.read = evdev_read,
.write = evdev_write,
.poll = evdev_poll,
.open = evdev_open,
.release = evdev_release,
.unlocked_ioctl = evdev_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = evdev_ioctl_compat,
#endif
.fasync = evdev_fasync,
.flush = evdev_flush,
};
用戶空間的 open() 最終落到 evdev_open(),它為每個打開者分配一份讀緩衝區:
static int evdev_open(struct inode *inode, struct file *file)
{
struct evdev_client *client;
evdev = evdev_table[i];
client = kzalloc(sizeof(struct evdev_client), GFP_KERNEL); /* 分配緩衝區 */
file->private_data = client;
}
因此完整的打開路徑是:app open → input_open_file → evdev_open。
讀取路徑為 read → sys_read → evdev_read。核心邏輯是:緩衝區空則阻塞等待,有數據則循環取出並 copy_to_user。
static ssize_t evdev_read(struct file *file, char __user *buffer,
size_t count, loff_t *ppos)
{
struct evdev_client *client = file->private_data;
struct evdev *evdev = client->evdev;
struct input_event event;
/* 隊列為空且非阻塞模式,直接返回 */
if (client->head == client->tail && evdev->exist &&
(file->f_flags & O_NONBLOCK))
return -EAGAIN;
/* 阻塞等待 */
retval = wait_event_interruptible(evdev->wait,
client->head != client->tail || !evdev->exist);
/* 逐個 copy_to_user */
while (retval + input_event_size() <= count &&
evdev_fetch_next_event(client, &event)) {
if (input_event_to_user(buffer + retval, &event))
return -EFAULT;
retval += input_event_size();
}
}
註冊設備時,核心層會遍歷 handler 鏈表,用雙方的 bit 位圖做「能力交集」比對,匹配成功就調用 handler 的 connect:
int input_register_device(struct input_dev *dev)
{
/* 遍歷 input_handler_list,與 dev 匹配 */
list_for_each_entry(handler, &input_handler_list, node)
input_attach_handler(dev, handler);
}
/* 比對 handler 的 input_device_id 與 dev 的 bit 數組 */
id = input_match_device(handler, dev);
MATCH_BIT(evbit, EV_MAX);
MATCH_BIT(keybit, KEY_MAX);
MATCH_BIT(relbit, REL_MAX);
MATCH_BIT(absbit, ABS_MAX);
MATCH_BIT(mscbit, MSC_MAX);
MATCH_BIT(ledbit, LED_MAX);
MATCH_BIT(sndbit, SND_MAX);
MATCH_BIT(ffbit, FF_MAX);
MATCH_BIT(swbit, SW_MAX);
if (!handler->match || handler->match(handler, dev))
error = handler->connect(handler, dev, id);
evdev_connect() 會建立一條數據通道,並把 evdev 掛進全局表:
static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
const struct input_device_id *id)
{
struct evdev *evdev;
evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
init_waitqueue_head(&evdev->wait);
dev_set_name(&evdev->dev, "event%d", minor);
evdev->minor = minor;
/* 初始化 handle */
evdev->handle.dev = input_get_device(dev);
evdev->handle.name = dev_name(&evdev->dev);
evdev->handle.handler = handler;
evdev->handle.private = evdev;
error = input_register_handle(&evdev->handle);
error = evdev_install_chrdev(evdev);
evdev_table[evdev->minor] = evdev; /* 放入全局表 */
error = device_add(&evdev->dev); /* 添加設備 */
}
下面這個 S3C2410 按鍵驅動展示了「硬件相關部分」與「Input 相關部分」如何分工:中斷只做去抖與記錄,定時器到期後才真正上報事件。
#include <linux/module.h>
#include <linux/version.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/irq.h>
#include <linux/gpio_keys.h>
static struct input_dev *input_button; /* 輸入按鍵設備 */
static struct timer_list timer_button; /* 延時去抖動的定時器 */
static struct pin_desc {
int irq; /* 中斷號 */
char *irqname; /* 中斷名稱 */
unsigned int pin; /* 引腳 */
unsigned int key_val; /* 事件值 */
};
static struct pin_desc *pin_desc_pd;
static struct pin_desc pins_desc[4] = {
{ IRQ_EINT0, "S2", S3C2410_GPF0, KEY_L },
{ IRQ_EINT2, "S3", S3C2410_GPF2, KEY_S },
{ IRQ_EINT11, "S4", S3C2410_GPG3, KEY_ENTER },
{ IRQ_EINT19, "S5", S3C2410_GPG11, KEY_LEFTSHIFT }
};
/* 中斷處理:只啟動 10ms 定時器去抖 */
static irqreturn_t irq_input_button_handler(int irq, void *dev_id)
{
pin_desc_pd = (struct pin_desc *)dev_id;
mod_timer(&timer_button, jiffies + HZ / 100);
return IRQ_RETVAL(IRQ_HANDLED);
}
/* 定時器到期:真正上報事件 */
static void timer_button_function(unsigned long dat)
{
struct pin_desc *pin_val = pin_desc_pd;
unsigned int val;
if (pin_val == NULL)
return;
val = s3c2410_gpio_getpin(pin_val->pin);
if (val) {
input_event(input_button, EV_KEY, pin_val->key_val, 0);
input_sync(input_button);
} else {
input_event(input_button, EV_KEY, pin_val->key_val, 1);
input_sync(input_button);
}
}
static int input_button_init(void)
{
int i;
/* 1. 分配 input_dev */
input_button = input_allocate_device();
if (!input_button)
return -ENOMEM;
/* 2. 設置事件類 */
set_bit(EV_KEY, input_button->evbit);
/* 3. 設置該事件類下的具體事件 */
set_bit(KEY_L, input_button->keybit);
set_bit(KEY_S, input_button->keybit);
set_bit(KEY_ENTER, input_button->keybit);
set_bit(KEY_LEFTSHIFT, input_button->keybit);
/* 4. 註冊 input_dev */
input_register_device(input_button);
/* 硬件相關:中斷與定時器 */
init_timer(&timer_button);
timer_button.function = timer_button_function;
add_timer(&timer_button);
for (i = 0; i < 4; i++) {
request_irq(pins_desc[i].irq, irq_input_button_handler,
IRQT_BOTHEDGE, pins_desc[i].irqname, &pins_desc[i]);
}
return 0;
}
static void input_button_exit(void)
{
int i;
for (i = 0; i < 4; i++)
free_irq(pins_desc[i].irq, &pins_desc[i]);
del_timer(&timer_button);
input_unregister_device(input_button);
input_free_device(input_button);
}
module_init(input_button_init);
module_exit(input_button_exit);
MODULE_LICENSE("GPL");
小結:撰寫輸入設備驅動,本質上就是「完善一個 input_dev 對象」加上「在恰當的時機上報觸發事件」。
void input_event(struct input_dev *dev, unsigned int type,
unsigned int code, int value);
→ input_handle_event(dev, type, code, value);
→ input_start_autorepeat(dev, code);
→ input_pass_event(dev, type, code, value);
→ handle->handler->event(handle, type, code, value);
此時流程又回到 evdev.c 的 evdev_event():
static void evdev_event(struct input_handle *handle,
unsigned int type, unsigned int code, int value)
{
struct evdev *evdev = handle->private; /* 由 handle 找回 evdev */
struct input_event event;
event.type = type;
event.code = code;
event.value = value;
/* 分發給所有打開過該設備的客戶端 */
list_for_each_entry_rcu(client, &evdev->client_list, node)
evdev_pass_event(client, &event);
/* 一組事件以 SYN_REPORT 收尾,此時才喚醒等待隊列 */
if (type == EV_SYN && code == SYN_REPORT)
wake_up_interruptible(&evdev->wait); /* 喚醒 evdev_read */
}
這也解釋了一個實踐要點:上報事件時必須以 EV_SYN/SYN_REPORT 結束,否則阻塞在 read() 的應用永遠等不到數據。
/* 分配與釋放 */
struct input_dev *input_allocate_device(void);
void input_free_device(struct input_dev *dev); /* 未註冊成功時 */
void input_unregister_device(struct input_dev *dev); /* 已註冊時 */
/* 註冊 */
int input_register_device(struct input_dev *dev);
/* 上報事件:type 為事件類,code 為子事件,value 為值 */
void input_event(struct input_dev *dev, unsigned int type,
unsigned int code, int value);
/* 常用封裝 */
static inline void input_report_key(struct input_dev *dev, unsigned int code, int value);
static inline void input_report_rel(struct input_dev *dev, unsigned int code, int value);
static inline void input_report_abs(struct input_dev *dev, unsigned int code, int value);
static inline void input_sync(struct input_dev *dev);
/* 置位某個事件位 */
extern __inline__ int set_bit(int nr, long *addr);
/* 設置絕對坐標的範圍(觸摸屏等) */
static inline void input_set_abs_params(struct input_dev *dev, int axis,
int min, int max, int fuzz, int flat);
電阻觸摸屏的典型用法:
input_set_abs_params(dev, ABS_X, 0, 0x3FF, 0, 0);
input_set_abs_params(dev, ABS_Y, 0, 0x3FF, 0, 0);
input_set_abs_params(dev, ABS_PRESSURE, 0, 1, 0, 0);
調試階段可以直接 hexdump 設備節點,人工比對字段:
hexdump /dev/event1 # open(/dev/event1) 後 read()
# 秒 微秒 類 code value
0000000 0bb2 0000 0e48 000c 0001 0026 0001 0000
0000010 0bb2 0000 0e54 000c 0000 0000 0000 0000
0000020 0bb2 0000 5815 000e 0001 0026 0000 0000
0000030 0bb2 0000 581f 000e 0000 0000 0000 0000
每一行 16 字節,正好對應一個 struct input_event:timeval(8 字節)+ type(2)+ code(2)+ value(4)。學會讀這份 dump,排查「事件沒上報」還是「上報了但應用沒收到」就非常直觀了。