實驗現象:在控制台打印按鍵值,並且通過按鍵控制相應的LED亮滅。
1.代碼
input_subsys_drv.c
1 #include <linux/module.h>
2 #include <linux/version.h>
3
4 #include <linux/init.h>
5 #include <linux/fs.h>
6 #include <linux/interrupt.h>
7 #include <linux/irq.h>
8 #include <linux/sched.h>
9 #include <linux/pm.h>
10 #include <linux/sysctl.h>
11 #include <linux/proc_fs.h>
12 #include <linux/delay.h>
13 #include <linux/platform_device.h>
14 #include <linux/input.h>
15 #include <linux/irq.h>
16
17 #include <asm/gpio.h>
18 #include <asm/io.h>
19 #include <asm/arch/regs-gpio.h>
20
21
22 struct pin_desc{
23 int irq;
24 char *name;
25 unsigned int pin;
26 unsigned int key_val;
27 };
28
29 struct pin_desc pins_desc[4] = {
30 {IRQ_EINT0, "S2", S3C2410_GPF0, KEY_L},
31 {IRQ_EINT2, "S3", S3C2410_GPF2, KEY_S},
32 {IRQ_EINT11, "S4", S3C2410_GPG3, KEY_ENTER},
33 {IRQ_EINT19, "S5", S3C2410_GPG11, KEY_LEFTSHIFT},
34 };
35
36 static struct input_dev *input_subsys_dev;
37 static struct pin_desc *irq_pd;
38 static struct timer_list buttons_timer;
39
40 static irqreturn_t buttons_irq(int irq, void *dev_id)
41 {
42 /* [cgw]: 按鍵IO發生邊沿中斷時重新設置定時間隔
43 * 用於按鍵消抖
44 */
45 irq_pd = (struct pin_desc *)dev_id;
46 buttons_timer.data = irq_pd->pin;
47 mod_timer(&buttons_timer, jiffies+HZ/100);
48 return IRQ_RETVAL(IRQ_HANDLED);
49 }
50
51 static void buttons_timer_function(unsigned long data)
52 {
53 struct pin_desc * pindesc = irq_pd;
54 unsigned int pinval;
55
56 if (!pindesc)
57 return;
58
59 /* [cgw]: 獲取按鍵IO狀態 */
60 pinval = s3c2410_gpio_getpin((unsigned int)data);
61
62 /* [cgw]: 根據按鍵IO狀態上報按鍵事件 */
63 if (pinval)
64 {
65 /* [cgw]: 上報按鍵彈起 */
66 input_report_key(input_subsys_dev, pindesc->key_val, 0);
67 //input_sync(input_subsys_dev);
68 }
69 else
70 {
71 /* [cgw]: 上報按鍵按下 */
72 input_report_key(input_subsys_dev, pindesc->key_val, 1);
73 //input_sync(input_subsys_dev);
74 }
75
76 //printk("timer occur!\n");
77 }
78
79
80 static int led_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
81 {
82 printk("led event!\n");
83 printk("value: 0x%x\n", value);
84
85 /* [cgw]: 根據應用程序下發的LED控制事件
86 * 亮滅LED
87 */
88 //if (code == SND_BELL) {
89 if (code == LED_MUTE) {
90 if (value == 0xAA) {
91 /* [cgw]: 點亮 */
92 s3c2410_gpio_setpin(S3C2410_GPF4, 0);
93 } else if (value == 0xEE) {
94 /* [cgw]: 熄滅 */
95 s3c2410_gpio_setpin(S3C2410_GPF4, 1);
96 }
97
98 return 0;
99 }
100
101 return -1;
102 }
103
104 int input_subsys_open(struct input_dev *dev)
105 {
106 int i, retval;
107
108 /* [cgw]: 設置按鍵IO為中斷輸入 */
109 s3c2410_gpio_cfgpin(S3C2410_GPF0, S3C2410_GPF0_EINT0);
110 s3c2410_gpio_cfgpin(S3C2410_GPF2, S3C2410_GPF2_EINT2);
111 s3c2410_gpio_cfgpin(S3C2410_GPG3, S3C2410_GPG3_EINT11);
112 s3c2410_gpio_cfgpin(S3C2410_GPG11, S3C2410_GPG11_EINT19);
113
114 /* [cgw]: 設置LED IO為輸出,初始為熄滅LED */
115 s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
116 s3c2410_gpio_setpin(S3C2410_GPF4, 1);
117
118 s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);
119 s3c2410_gpio_setpin(S3C2410_GPF5, 1);
120
121 s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);
122 s3c2410_gpio_setpin(S3C2410_GPF5, 1);
123
124 /* [cgw]: 為按鍵IO分配中斷線 */
125 for (i = 0; i < 4; i++)
126 {
127 retval = request_irq(pins_desc[i].irq, buttons_irq, IRQT_BOTHEDGE, pins_desc[i].name, &pins_desc[i]);
128 }
129
130 printk("input subsys open!\n");
131
132 return 0;
133 }
134
135
136
137 static int input_subsys_init(void)
138 {
139 /* [cgw]: 分配一個輸入設備 */
140 input_subsys_dev = input_allocate_device();
141 input_subsys_dev->name = "input_subsys_dev";
142
143 /* [cgw]: 設置支持的事件類型 */
144 set_bit(EV_KEY, input_subsys_dev->evbit);
145 //set_bit(EV_REP, input_subsys_dev->evbit);
146
147 set_bit(EV_LED, input_subsys_dev->evbit);
148 //set_bit(EV_SND, input_subsys_dev->evbit);
149
150 /* [cgw]: 設置支持的事件碼 */
151 set_bit(KEY_L, input_subsys_dev->keybit);
152 set_bit(KEY_S, input_subsys_dev->keybit);
153 set_bit(KEY_ENTER, input_subsys_dev->keybit);
154 set_bit(KEY_LEFTSHIFT, input_subsys_dev->keybit);
155
156 set_bit(LED_MUTE, input_subsys_dev->ledbit);
157 //set_bit(SND_BELL, input_subsys_dev->sndbit);
158
159 /* [cgw]: 分配輸入設備的open方法,用戶操作open(/dev/xxx, ...)時調用 */
160 input_subsys_dev->open = input_subsys_open;
161 /* [cgw]: 分配輸入設備的event方法,用戶在應用程序write()時 */
162 input_subsys_dev->event = led_event;
163
164 /* [cgw]: 注冊輸入設備 */
165 input_register_device(input_subsys_dev);
166
167 /* [cgw]: 初始化定時器,用於按鍵消抖 */
168 init_timer(&buttons_timer);
169 buttons_timer.function = buttons_timer_function;
170 add_timer(&buttons_timer);
171
172 printk("input subsys init!\n");
173
174 return 0;
175 }
176
177 static void input_subsys_exit(void)
178 {
179 int i;
180
181 /* [cgw]: 釋放按鍵IO中斷 */
182 for (i = 0; i < 4; i++)
183 {
184 free_irq(pins_desc[i].irq, &pins_desc[i]);
185 }
186
187 /* [cgw]: 刪除定時器 */
188 del_timer(&buttons_timer);
189 /* [cgw]: 注銷輸入設備 */
190 input_unregister_device(input_subsys_dev);
191 /* [cgw]: 釋放輸入設備內存空間 */
192 input_free_device(input_subsys_dev);
193 }
194
195 module_init(input_subsys_init);
196
197 module_exit(input_subsys_exit);
198
199 MODULE_LICENSE("GPL");
input_subsys_test.c
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <poll.h>
6 #include <signal.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10
11 #include <linux/input.h>
12
13
14
15 int fd;
16
17 void my_signal_fun(int signum)
18 {
19 struct input_event buttons_event, leds_event;
20
21 /* [cgw]: 異步通知產生時返回的數據 */
22 read(fd, &buttons_event, sizeof(struct input_event));
23
24 /* [cgw]: 打印事件類型,事件碼,事件值 */
25 printf("type: 0x%x code: 0x%x value: 0x%x\n",
26 buttons_event.type,
27 buttons_event.code,
28 buttons_event.value);
29
30 /* [cgw]: 返回的是KEY_L或KEY_S值 */
31 if (buttons_event.code == KEY_L || buttons_event.code == KEY_S) {
32 /* [cgw]: 按鍵彈起 */
33 if (buttons_event.value == 0) {
34
35 /* [cgw]: 構造一個EV_LED事件 */
36
37 //leds_event.type = EV_SND;
38 leds_event.type = EV_LED;
39 //leds_event.code = SND_BELL;
40 leds_event.code = LED_MUTE;
41
42 /* [cgw]: KEY_L和KEY_S控制LED的亮滅 */
43 if (buttons_event.code == KEY_L) {
44 leds_event.value = 0xAA;
45 } else if (buttons_event.code == KEY_S) {
46 leds_event.value = 0xEE;
47 }
48
49 /* [cgw]: 發送LED控制事件 */
50 write(fd, &leds_event, sizeof(struct input_event));
51
52 printf("led write!\n");
53 }
54 }
55 }
56
57 int main(int argc, char **argv)
58 {
59 int Oflags;
60
61 /* [cgw]: 設置需要處理的信號SIGIO,即輸入文件會請求一個SIGIO
62 * 信號,當有新數據到來這個信號會發給filp->f_owner進程
63 */
64 signal(SIGIO, my_signal_fun);
65
66 fd = open("/dev/event1", O_RDWR | O_NONBLOCK);
67
68 //printf("fd = 0x%x\n", fd);
69
70 if (fd < 0)
71 {
72 printf("can't open!\n");
73 }
74
75 /* [cgw]: 根據文件標識符fd,設置能夠獲得這個文件的進程(owner)
76 * getpid()獲得當前進程ID
77 */
78 fcntl(fd, F_SETOWN, getpid());
79
80 /* [cgw]: 獲得file->f_flags標志 */
81 Oflags = fcntl(fd, F_GETFL);
82
83 /* [cgw]: 置位FASYNC使能異步通知 */
84 fcntl(fd, F_SETFL, Oflags | FASYNC);
85
86 while (1)
87 {
88 /* [cgw]: 休眠 */
89 sleep(1000);
90 }
91
92 return 0;
93 }
makefile
1 KERN_DIR = /work/system/linux-2.6.22.6 2 3 all: 4 make -C $(KERN_DIR) M=`pwd` modules 5 6 clean: 7 make -C $(KERN_DIR) M=`pwd` modules clean 8 rm -rf modules.order 9 10 obj-m += input_subsys_drv.o
2. 實驗
2.1
安裝驅動程序:
insmod input_subsys_drv.ko
1 # insmod input_subsys_drv.ko 2 input: input_subsys_dev as /class/input/input1 3 input subsys open! 4 input subsys init!
運行應用程序
./input_subsys_test
1 # ./input_subsys_test 2 type: 0x1 code: 0x26 value: 0x1 3 type: 0x1 code: 0x26 value: 0x0 4 led event! 5 value: 0xaa 6 led write! 7 type: 0x11 code: 0x7 value: 0xaa 8 type: 0x1 code: 0x1f value: 0x1 9 type: 0x1 code: 0x1f value: 0x0 10 led event! 11 value: 0xee 12 led write! 13 type: 0x11 code: 0x7 value: 0xee
3. 現象分析
按一下按鍵KEY_L,終端輸出:
type: 0x1 code: 0x26 value: 0x1 //按鍵按下 type: 0x1 code: 0x26 value: 0x0 //按鍵彈起 led event! //應用程序操作write, 發送LED控制事件,調用led_event() value: 0xaa //讀到的LED亮的命令 led write! //返回應用程序繼執行 type: 0x11 code: 0x7 value: 0xaa //因為應用程序發送了事件給驅動程序,驅動把這個事件作為一個輸入事件(相當於按鍵輸入事件),同樣通過異步通知反饋到應用程序

The end!
http://xxxxxx/Linuxjc/1162668.html TechArticle