linux輸入子系統之按鍵驅動

2021-06-20 00:23:59 字數 4516 閱讀 6644

上一節中,我們講解了linux  input子系統的框架,到核心原始碼裡詳細分析了輸入子系統的分離分層的框架等。

這一節,我們來以輸入子系統的框架來寫乙個按鍵驅動。

問:怎麼寫符合輸入子系統框架的驅動程式? 答:

1. 分配乙個input_dev結構體

2. 設定

3. 註冊

4. 硬體相關的**,比如在中斷服務程式裡上報事件

問:如何分配input_dev結構體?

答:使用input_allocate_device函式

input_dev結構體的重要成員

struct input_dev
問:第二步的設定,應該怎麼設定,應該設定什麼?

答:舉例,在此按鍵驅動裡

/* 2.設定 */

/* 2.1 設定按鍵能產生哪類事件 */

set_bit(ev_key,buttons_dev->evbit);

set_bit(ev_rep,buttons_dev->evbit);

/* 2.2 設定能產生這類操作的哪些事件 */

set_bit(key_l,buttons_dev->keybit);

set_bit(key_s,buttons_dev->keybit);

set_bit(key_enter,buttons_dev->keybit);

set_bit(key_leftshift,buttons_dev->keybit);

問:有哪些類呢?

答:在input.h裡有以下類

#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

#define ev_cnt (ev_max+1)

問:如何註冊?

答:使用input_register_device(struct input_dev *dev)函式來註冊

問:此按鍵驅動的硬體操作包括哪些操作?

答:申請定時器、申請中斷操作

驅動原始碼:

#include #include #include #include #include #include #include #include #include #include //class_create

#include //s3c2410_gpf1

//#include #include //#include #include //wait_event_interruptible

#include //poll

#include #include static struct pin_desc;

static struct pin_desc pins_desc[4] = ,

, ,

,};

static struct pin_desc *irq_pd;

static struct input_dev *buttons_dev;

static struct timer_list buttons_timer;

/* 使用者中斷處理函式 */

static irqreturn_t buttons_irq(int irq, void *dev_id)

/* 定時器處理函式 */

static void buttons_timer_function(unsigned long data)

else }

/* 驅動入口函式 */

static int buttons_input_init(void)

return 0;

}/* 驅動出口函式 */

static void buttons_input_exit(void)

del_timer(&buttons_timer);

input_unregister_device(buttons_dev);

input_free_device(buttons_dev);

}module_init(buttons_input_init); //用於修飾入口函式

module_exit(buttons_input_exit); //用於修飾出口函式

module_author("lwj");

module_description("just for demon");

module_license("gpl"); //遵循gpl協議

測試步驟方法一:

[wj2440]# ls

qt first_test second_test

tqledtest fourth_drv.ko sixth_drv.ko

bin home sixthdrvtest

buttons_all_drv.ko lib sys

buttons_all_test linuxrc third_drv.ko

buttons_input.ko mnt third_test

dev opt tmp

driver_test proc udisk

etc root usr

fifth_drv.ko sbin var

fifth_test sddisk web

first_drv.ko second_drv.ko

[wj2440]# ls /dev/event* -l

crw-rw---- 1 root root 13, 64 jan 2 06:04 /dev/event0

[wj2440]# insmod buttons_input.ko

input: unspecified device as /devices/virtual/input/input1

[wj2440]# ls /dev/event* -l

crw-rw---- 1 root root 13, 64 jan 2 06:04 /dev/event0

crw-rw---- 1 root root 13, 65 jan 2 06:06 /dev/event1

[wj2440]# cat /dev/tty1

[wj2440]# cat /dev/tty1

lsls

輸入cat /dev/tty1命令後,順序按下k1,k2,k3則會顯示ls

測試步驟方法二、

[wj2440]# hexdump /dev/event1

0000000 b738 495d 8456 0007 0001 0026 0001 0000

0000010 b738 495d 846f 0007 0000 0000 0000 0000

0000020 b738 495d 2fb8 000a 0001 0026 0000 0000

0000030 b738 495d 2fc7 000a 0000 0000 0000 0000

分析:

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

struct input_event ;

struct timeval ;

按鍵驅動之使用輸入子系統架構

有關輸入子系統我們可以參考 gpio keys.c這個 注 這只是乙個例子,沒有實際的作用 static struct input dev button dev 1.分配乙個input device結構體 參考gpio keys.c這個例子,可以知道其分配函式是input allocate devi...

020 linux驅動之 輸入子系統按鍵應用

一 分配乙個輸入子系統結構體 static struct input dev buttons dev 分配乙個input dev結構體 二 設定這個輸入子系統需要的動作 1.分配乙個input dev結構體 buttons dev input allocate device 使用這個函式分配乙個in...

linux驅動之輸入子系統

輸入子系統框架,把核心開啟 搜尋input.c 輸入子系統的 在 driver input目錄下面 最上一層,我們稱它為核心層 要看乙個驅動程式我們應該從他的入口函式開始看 有一行 err register chrdev region mkdev input major,0 input max ch...