先说明一下按键与S3C2440芯片的连接方式:
KEY1 <----> EINT8 <---->GPG0
KEY2 <----> EINT11<---->GPG3
KEY3 <----> EINT13<---->GPG5
KEY4 <----> EINT14<---->GPG6
KEY5 <----> EINT15<---->GPG7
KEY6 <----> EINT19<---->GPG11
驱动程序源码如下:
(drivers/char/mini2440_buttons.c)
1 #include<linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/fs.h>
4 #include <linux/init.h>
5 #include <linux/delay.h>
6 #include <linux/poll.h>
7 #include <linux/irq.h>
8 #include <asm/irq.h>
9 #include <linux/interrupt.h>
10 #include <asm/uaccess.h>
11 #include <mach/regs-gpio.h>
12 #include <mach/hardware.h>
13 #include <linux/platform_device.h>
14 #include <linux/cdev.h>
15 #include <linux/miscdevice.h>
16 #include <linux/sched.h>
17 #include <linux/gpio.h>
18
19 #defineDEVICE_NAME "buttons" //设备名称
20
21
22 /*定义中断所用的结构体*/
23
24 structbutton_irq_desc
25 {
26 int irq; //按键对应的中断号
27 int pin; //按键所对应的GPIO 端口
28 intpin_setting; //按键对应的引脚描述,实际并未用到,保留
29 int number; //定义键值,以传递给应用层/用户态
30 char *name; //每个按键的名称
31 };
32
33
34 /*结构体实体定义*/
35 static structbutton_irq_desc button_irqs [] =
36 {
37 {IRQ_EINT8, S3C2410_GPG(0) ,S3C2410_GPG0_EINT8 , 0, "KEY0"},
38 {IRQ_EINT11, S3C2410_GPG(3) ,S3C2410_GPG3_EINT11 , 1, "KEY1"},
39 {IRQ_EINT13, S3C2410_GPG(5) ,S3C2410_GPG5_EINT13 , 2, "KEY2"},
40 {IRQ_EINT14, S3C2410_GPG(6) ,S3C2410_GPG6_EINT14 , 3, "KEY3"},
41 {IRQ_EINT15, S3C2410_GPG(7) ,S3C2410_GPG7_EINT15 , 4, "KEY4"},
42 {IRQ_EINT19, S3C2410_GPG(11), S3C2410_GPG11_EINT19,5, "KEY5"},
43 };
44
45 /*开发板上按键的状态变量,注意这里是’0’,对应的ASCII码为30*/
46 static volatilechar key_values [] ={'0', '0', '0', '0', '0', '0'};
47
48 /*因为本驱动是基于中断方式的,在此创建一个等待队列,以配合中断函数使用;当有按键按下并读>取到键值时,将会唤醒此队列,并设置中断标志,以便能通过read 函数判断和读取键值传递到用户>态;当没有按键按下时,系统并不*/
49 /*会轮询按键状态,以节省时钟资源*/
50 staticDECLARE_WAIT_QUEUE_HEAD(button_waitq);
51
52 /*中断标识变量,配合上面的队列使用,中断服务程序会把它设置为1,read 函数会把它清零*/
53 static volatile int ev_press = 0;
54
55 /*本按键驱动的中断服务程序*/
56 static irqreturn_tbuttons_interrupt(intirq, void *dev_id)
57 {
58 struct button_irq_desc *button_irqs = (struct button_irq_desc *)dev_id;
59 int down;
60
61 //udelay(0);
62
63 /*获取被按下的按键状态*/
64 down = !s3c2410_gpio_getpin(button_irqs->pin);
65
66 /*状态改变,按键被按下,从这句可以看出,当按键没有被按下的时候,寄存器的值为1(上拉),
67 但按键被按下的时候,寄存器对应的值为0*/
68 if (down != (key_values[button_irqs->number] & 1)) { // Changed
69 /*如果key1 被按下,则key_value[0]就变为’1’,对应的ASCII 码为31*/
70 key_values[button_irqs->number] = '0' + down;
71 ev_press =1; /*设置中断标志为1*/
72 wake_up_interruptible(&button_waitq);/*唤醒等待队列*/
73 }
74 return IRQ_RETVAL(IRQ_HANDLED);
75 }
76
77 /**在应用程序执行open(“/dev/buttons”,…)时会调用到此函数,在这里,它的作用主要是注册6 个按
78 键的中断。
79
80 *所用的中断类型是IRQ_TYPE_EDGE_BOTH,也就是双沿触发,在上升沿和下降沿均会产生中断,这样>做
81
82 **是为了更加有效地判断按键状态
83
84 */
85
86 static int s3c24xx_buttons_open(structinode *inode,struct file *file)
87 {
88 int i;
89 int err = 0;
90 for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++)
91 {
92 if (button_irqs[i].irq < 0)
93 {
94 continue;
95 }
96
97 /*注册中断函数*/
98 err = request_irq(button_irqs[i].irq, buttons_interrupt,IRQ_TYPE_EDGE_BOTH,
99 button_irqs[i].name, (void *)&button_irqs[i]);
100 if (err)
101 break;
102 }
103
104 if (err)
105 {
106 /*如果出错,释放已经注册的中断,并返回*/
107 i--;
108 for (; i >= 0; i--)
109 {
110 if (button_irqs[i].irq < 0)
111 {
112 continue;
113 }
114 disable_irq(button_irqs[i].irq);
115 free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);
116 }
117 return -EBUSY;
118 }
119
120 /*注册成功,则中断队列标记为1,表示可以通过read 读取*/
121 ev_press =1;
122 /*正常返回*/
123 return 0;
124 }
125
126 /*
127
128 *此函数对应应用程序的系统调用close(fd)函数,在此,它的主要作用是当关闭设备时释放6 个按键
129 的中断*处理函数
130
131 */
132
133 static int s3c24xx_buttons_close(structinode *inode,struct file *file)
134 {
135 int i;
136 for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++)
137 {
138 if (button_irqs[i].irq < 0)
139 {
140 continue;
141 }
142
143 /*释放中断号,并注销中断处理函数*/
144 free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);
145 }
146 return 0;
147 }
148 /*
149
150 *对应应用程序的read(fd,…)函数,主要用来向用户空间传递键值
151
152 */
153
154 static int s3c24xx_buttons_read(structfile *filp,char __user *buff,size_t count, loff_t *offp)
155 {
156 unsigned long err;
157 if (!ev_press)
158 {
159 if (filp->f_flags& O_NONBLOCK)
160 /*当中断标识为0 时,并且该设备是以非阻塞方式打开时,返回*/
161 return -EAGAIN;
162 else
163 /*当中断标识为0 时,并且该设备是以阻塞方式打开时,进入休眠状态,等待被唤醒*/
164 wait_event_interruptible(button_waitq,ev_press);
165 }
166
167 /*把中断标识清零*/
168 ev_press =0;
169
170 /*一组键值被传递到用户空间*/
171 err = copy_to_user(buff, (const void *)key_values, min(sizeof(key_values),count));
172
173 return err ? -EFAULT : min(sizeof(key_values),count);
174 }
175
176 static unsigned int s3c24xx_buttons_poll(struct file *file,struct poll_table_struct *wait)
177 {
178 unsigned intmask = 0;
179
180 /*把调用poll 或者select 的进程挂入队列,以便被驱动程序唤醒*/
181 poll_wait(file, &button_waitq, wait);
182
183 if (ev_press)
184 mask |=POLLIN | POLLRDNORM;
185
186 return mask;
187 }
188
189 /*设备操作集*/
190 static structfile_operations dev_fops = {
191 .owner = THIS_MODULE,
192 .open = s3c24xx_buttons_open,
193 .release= s3c24xx_buttons_close,
194 .read = s3c24xx_buttons_read,
195 .poll = s3c24xx_buttons_poll,
196 };
197
198 static structmiscdevice misc = {
199 .minor = MISC_DYNAMIC_MINOR,
200 .name =DEVICE_NAME,
201 .fops = &dev_fops,
202 };
203
204 /*设备初始化,主要是注册设备*/
205 static int __init dev_init(void)
206 {
207 int ret;
208
209 /*把按键设备注册为misc 设备,其设备号是自动分配的*/
210 ret =misc_register(&misc);
211 printk (DEVICE_NAME"\tinitialized\n");
212 return ret;
213 }
214
215 /*注销设备*/
216 static void __exitdev_exit(void)
217 {
218 misc_deregister(&misc);
219 }
220
221 module_init(dev_init); //模块初始化,仅当使用insmod/podprobe 命令加载时有用,如果设备不是
222 通过模块方式加载,此处将不会被调用
223
224 module_exit(dev_exit); //卸载模块,当该设备通过模块方式加载后,可以通过rmmod 命令卸载,将