下载后可任意编辑Linux 设备模型之 input 子系统详解 (2024-10-17 08:56) 标签: 模型 设备 Linux input 分类: 设备驱动 ------------------------------------------ 本文系本站原创,欢迎转载!转载请注明出处:http://ericxiao.cublog.cn/------------------------------------------一:前言在键盘驱动代码分析的笔记中,接触到了 input 子系统.键盘驱动,键盘驱动将检测到的所有按键都上报给了 input 子系统。Input 子系统是所有 I/O 设备驱动的中间层,为上层提供了一个统一的界面。例如,在终端系统中,我们不需要去管有多少个键盘,多少个鼠标。它只要从 input 子系统中去取对应的事件(按键,鼠标移位等)就可以了。今日就对 input 子系统做一个详尽的分析.下面的代码是基于 linux kernel 2.6.25.分析的代码主要位于 kernel2.6.25/drivers/input 下面.二:使用 input 子系统的例子在内核自带的文档 Documentation/input/input-programming.txt 中。有一个使用 input 子系统的例子,并附带相应的说明。以此为例分析如下:#include
#include #include #include #include static void button_interrupt(int irq, void *dummy, struct pt_regs *fp){ input_report_key(&button_dev, BTN_1, inb(BUTTON_PORT) & 1); input_sync(&button_dev);} static int __init button_init(void){ if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL)) { printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq); return -EBUSY; } button_dev.evbit[0] = BIT(EV_KEY); button_dev.keybit[LONG(BTN_0)] = BIT(BTN_0); 下载后可任意编辑 input_register_device(&button_dev);} static void __exit button_exit(void){ input_unregister_device(&button_dev); free_irq(BUTTON_IRQ, button_interrupt);} module_init(button_init);module_exit(button_exit); 这个示例 module 代码还是比较简单,在初始化函数里注册了一个中断处理例程。然后注册了一个 input device.在中断处理程序里,将接收到的按键上报给input 子系统。文档的作者在之后的分析里又对这个 module 作了优化。主要是在注册中断处理的时序上。在修改过后的代码里,为 input device 定义...