input子系統——架構、驅動、應用程式,input子系統
一、input子系統架構
input子系統由驅動層drivers,輸入子系統核心層input core,事件處理層event handler組成。
一個輸入事件,通過輸入裝置發給系統如滑鼠移動,鍵盤按鍵按下等通過device driver->input core(handler->event函數)->event handler->user space的順序到達使用者空間傳給應用程式。
一個輸出事件,通過系統發給輸入裝置,通過user space->event handler->input core(dev->event函數)->device driver
1、驅動功能層:負責和底層的硬體裝置打交道,將底層硬體裝置對使用者輸入的響應轉換為標準的輸入事件以後再向上發送給輸入系統核心層
2、Input系統核心層:由driver/input/input.c及相關標頭檔實現,他對下提供了裝置驅動層的介面,對上提供了事件處理層的變成介面。
3、事件處理層將硬體裝置上報的事件分發到使用者空間和核心。
結構圖如下:
二、編寫input驅動需要的函數
1)包含標頭檔<linux/input.h>,他是input子系統的介面,提供了必要的定義訊息
2)Input_allocate_device()
分配了一個Input device的結構,設定他的bit field來告訴input子系統他能產生或者接收什麼事件。
3)input_register_device(struct input_dev *dev)
將dev結構體添加到input_dev_list全域鏈表中去
通過input_attach_handler(struct input_dev *dev, struct input_handler *handler)來尋找對應的handler,
input_attach_handler裡面實際調用了input_match_device(const struct input_device_id *id,struct input_dev *dev)
一旦input_attach_handler找到了對應的handler,就執行handler->connect
4)input_report_key(struct input_dev *dev, unsigned int code, int value)
5)input_sync(struct input_dev *dev)
告訴事件的接收者,到此為止為一次完整的訊息。比如我們在touch screen上獲得了x、y的值,要使作為一次事件,那麼將input_sync加在report x、y值得後面。
6)其他的事件type,輸出事件處理
其他的事件有:
EV_LED:用作鍵盤的LED燈
EV_SND:用作鍵盤的蜂鳴器
他和鍵盤事件很相似,只不過鍵盤事件是INPUT_PASS_TO_DEVICE,而輸出事件是INPUT_PASS_TO_HANDLERS,從系統到輸入裝置的驅動程式,如果你的驅動程式要處理這些事件,必須設定evbit中相應位,而且要實現一個回呼函數。
struct input_dev *button_dev;
button_dev->event = button_event;這個便是處理輸出事件的回呼函數
三、普通按鍵實現input驅動例子
/*drivers->input core->event handlerfunction: this file is button driverdate: 20150101author: lei_wang*/#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/irq.h>#include <asm/irq.h>#include <asm/io.h>#include <mach/regs-gpio.h>#include <mach/hardware.h>#include <linux/interrupt.h>#include <linux/gpio.h>#include <linux/input.h>static struct input_dev *button_dev;static irqreturn_t button_intr(int irq, void *dev_id){int val;val = s3c2410_gpio_getpin(S3C2410_GPG(0));//printk(KERN_INFO "key value is %d\n", val);input_report_key(button_dev, BTN_0, val);input_sync(button_dev);return IRQ_RETVAL(IRQ_HANDLED);}static int __init button_init(void){int ret;ret = request_irq(IRQ_EINT8, button_intr, IRQ_TYPE_EDGE_BOTH, "button0", NULL);if (ret) {printk(KERN_ERR "%s request failed\n", __func__);return -ENODEV;}button_dev = input_allocate_device();if (!button_dev) {printk(KERN_ERR "button.c: Not enough memory\n");free_irq(IRQ_EINT8, NULL);return -ENOMEM;}button_dev->name = "button0";button_dev->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY);button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);ret = input_register_device(button_dev);if (ret) {printk(KERN_ERR "button.c: Failed to register device\n");input_free_device(button_dev);free_irq(IRQ_EINT8, NULL);return -ENODEV;}printk(KERN_INFO "button init ok!\n");return 0;}static void __exit button_exit(void){input_unregister_device(button_dev);input_free_device(button_dev);free_irq(IRQ_EINT8, NULL);printk(KERN_INFO "button exit ok!\n");}module_init(button_init);module_exit(button_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("Realsil Luckywang");
Makefile如下:
obj-m = button.oKERNELDIR ?=/home/lei/linux-2.6.32.2modules:$(MAKE) -C $(KERNELDIR) M=$(shell pwd) modulesclean:rm -rf *.o *.mod.c *.order *.symvers
Include/linux/bitops.h中定義了
#define BIT(nr) (1UL << (nr))
#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
#define BTN_0 0x100
button_dev->evbit[0] = BIT_MASK(EV_KEY);
button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);
說明:
1)上面的0x100表示BTN_0這個bit在所有的bit中是0x100(bit 256)位,那麼
BIT_WORD(BTN_0)代表bit 256在keybit這個數組的第幾個數組(第8個)
BIT_MASK(BTN_0)代表bit 256在keybit這個數組的第幾個數組裡面的值(第8個數組的bit0)
2)事件類型type——編碼code——值value
evbit是事件數目組,evbit這個事件數目組裡面可以放很多事件類型,比如key、abs等
事件key裡面又有很多具體編碼BTN_0、BTN_TOUCH等
事件abs裡面也有很多具體編碼ABS_X、ABS_Y等
不同編碼有不同的值
另外http://blog.csdn.net/ylyuanlu/article/details/6704744 這篇部落格對以下說的挺詳細的
1)input子系統的struct input_dev、struct handler的註冊
2)struct input_dev與struct input_handler怎麼互相匹配(類似於device和driver匹配)
3)事件處理過程
四、例子對應的應用程式
/*20150101just a simple input test codelei_wang*/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#include <string.h>#include <linux/input.h>int main(){int fd;int version;int ret;struct input_event ev;fd = open("/dev/input/event1", O_RDONLY);if (fd < 0) {printf("open file failed\n");exit(1);}ioctl(fd, EVIOCGVERSION, &version);printf("evdev driver version is 0x%x: %d.%d.%d\n",version, version>>16, (version>>8) & 0xff, version & 0xff);while (1) {ret = read(fd, &ev, sizeof(struct input_event));if (ret < 0) {printf("read event error!\n");exit(1);}if (ev.type == EV_KEY)printf("type %d,code %d, value %d\n", ev.type, ev.code, ev.value);}return 0;}以上只是一個簡單的應用程式測試。當你按下K1的時候,串口終端會有顯示的input dev上報的按鍵的訊息。
另外還有很多裡面ioctl調用的內容沒有實驗,具體可以參考這篇部落格http://www.cnblogs.com/leaven/archive/2011/02/12/1952793.html,對ioctl的每個case以及read調用都試一遍,找到自己的體會。