linux输入子系统(input subsystem)之按键输入和LED控制

实验现象:在控制台打印按键值,并且通过按键控制相应的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!

时间: 2024-07-31 14:34:52

linux输入子系统(input subsystem)之按键输入和LED控制的相关文章

Linux输入子系统(Input Subsystem)

Linux输入子系统(Input Subsystem) http://blog.csdn.net/lbmygf/article/details/7360084 input子系统分析  http://blog.chinaunix.net/uid-27717694-id-3758334.html

Linux学习:输入子系统 input

一.输入子系统 针对输入设备设计:触摸屏.键盘.按键.传感器.鼠标...... 二.每种设备都属于字符设备驱动,程序的写法步骤也相同 1.实现入口函数 xxx_init() 和卸载函数 xxx_exit() 2.申请设备号 register_chrdev() --- 与内核相关 3.创建设备文件(节点) class_create() 和 device_create() --- 与内核相关 4.硬件初始化 GPIO操作 --- 与硬件相关 注册中断 --- 与硬件相关 初始化等待队列 --- 与内

Linux输入子系统(转)

Linux输入子系统(Input Subsystem) 1.1.input子系统概述 输入设备(如按键,键盘,触摸屏,鼠标等)是典型的字符设备,其一般的工作机制是低层在按键,触摸等动作发生时产生一个中断(或驱动通过timer定时查询),然后cpu通过SPI,I2C或者外部存储器总线读取键值,坐标等数据,放一个缓冲区,字符设备驱动管理该缓冲区,而驱动的read()接口让用户可以读取键值,坐标等数据. 在Linux中,输入子系统是由输入子系统设备驱动层.输入子系统核心层(Input Core)和输入

linux输入子系统(6)-input子系统介绍及结构图

注:本系列转自: http://www.ourunix.org/post/290.html input子系统介绍         输入设备(如按键,键盘,触摸屏,鼠标,蜂鸣器等)是典型的字符设备,其一般的工作机制是底层在按键,触摸等动作发生时产生一个中断(或驱动通过timer定时查询),然后cpu通过SPI,I2C或者外部存储器总线读取键值,坐标等数据,放一个缓冲区,字符设备驱动管理该缓冲区,而驱动的read()接口让用户可以读取键值,坐标等数据.         在Linux中,输入子系统是由

Linux 输入子系统

Technorati 标签: Kernel 输入子系统 Input      在Linux中,输入设备(如按键.键盘.触摸屏.鼠标等)是典型的字符设备,其一般的工作机理,是底层在按键.触摸时,触发一个中断,或者驱动通过定时器定时查询,通过这两种方式通知CPU,CPU然后通过SPI.I2C或I/O接口读取键值.坐标等数据,放入缓冲区,字符设备驱动管理该缓冲区,向上提供read接口供应用程序使用.      在上述的工作流程中,只有终端.读取数值是根具体硬件设备相关,而输入事件的缓冲区管理以及字符设

Linux输入子系统框架分析(1)

在Linux下的输入设备键盘.触摸屏.鼠标等都可以用输入子系统来实现驱动.输入子系统分为三层,核心层和设备驱动层,事件层.核心层和事件层由Linux输入子系统本身实现,设备驱动层由我们实现.我们在设备驱动层将输入事件上报给核心层input.c,核心层找到匹配的事件层,将事件交给事件层处理,事件层处理完后传递到用户空间. 我们最终要搞清楚的是在用户空间调用open和read最终在内核中是怎样处理的,向内核上报的事件又是谁处理的,处理完后是怎样传递到用户空间的? 上面两个图是输入子系统的框架. 下面

Android底层开发之Linux输入子系统要不要推断系统休眠状态上报键值

Android底层开发之Linux输入子系统要不要推断系统休眠状态上报键值 题外话:一个问题研究到最后,那边记录文档的前半部分基本上都是没用的,甚至是错误的. 重点在最后,前边不过一些假想猜測. http://blog.csdn.net/kangear/article/details/40072707 在调试一下红外遥控器input驱动时,直接採用的是一个半成品的驱动在上边实现的自己的设备的匹配,但同一时候遇到了一些关于input输入子系统的疑惑. 按键一般有「按下和抬起」两个状态一般使用0和1

Android底层开发之Linux输入子系统要不要判断系统休眠状态上报键值

Android底层开发之Linux输入子系统要不要判断系统休眠状态上报键值 题外话:一个问题研究到最后,那边记录文档的前半部分基本上都是无用的,甚至是错误的.重点在最后,前边仅仅是一些假想推测. http://blog.csdn.net/kangear/article/details/40072707 在调试一下红外遥控器input驱动时,直接采用的是一个半成品的驱动在上边实现的自己的设备的匹配,但同时遇到了一些关于input输入子系统的疑惑. 按键一般有「按下和抬起」两个状态一般使用0和1来分

Linux 驱动——Button8(输入子系统)

输入子系统由驱动层.输入子系统核心.事件处理层三部分组成.一个输入事件,如鼠标移动.键盘按下等通过Driver->Inputcore->Event handler->userspace的顺序到达用户控件的应用程序. 其中核心层提供一些设备层与事件层公用的函数,比如说注册函数.反注册函数.事件到来的处理函数等等:事件层其实在Linux内核里面已经帮我们写好了很多有关的事件:而设备层就跟我们新添加到输入系统的具体设备相关了.这里以JZ2440开发板上的4个按键作为输入子系统的按键:它定义的功