2440按键驱动分析

  一.驱动代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/poll.h>
 
 
 
static struct class *sixthdrv_class;
static struct class_device  *sixthdrv_class_dev;
 
//volatile unsigned long *gpfcon;
//volatile unsigned long *gpfdat;
 
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
 
/* 中断事件标志, 中断服务程序将它置1,sixth_drv_read将它清0 */
static volatile int ev_press = 0;
 
//static struct fasync_struct *button_async;     //定义一个结构
 
 
struct pin_desc{                               //定义结构体
     unsigned int pin;
     unsigned int key_val;
};
 
 
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
static unsigned char key_val;
 
/*
  * K1,K2,K3,K4对应GPG0,GPG3,GPG5,GPG6
  */
 
struct pin_desc pins_desc[4] = {
     {S3C2410_GPG0, 0x01},
     {S3C2410_GPG3, 0x02},
     {S3C2410_GPG5, 0x03},
     {S3C2410_GPG6, 0x04},
};
 
//static atomic_t canopen = ATOMIC_INIT(1);     //定义原子变量并初始化为1
 
static DECLARE_MUTEX(button_lock);     //定义互斥锁
 
/*
   * 确定按键值
   */
static irqreturn_t buttons_irq( int irq, void *dev_id)    //参数中断号,和ID
{
     struct pin_desc * pindesc = ( struct pin_desc *)dev_id;     //?定义一个结构体指针使他的初值为ID
     unsigned int pinval;
     
     pinval = s3c2410_gpio_getpin(pindesc->pin);        //系统函数独处引脚值(GPF0)
 
     if (pinval)
     {
         /* 松开 */
         key_val = 0x80 | pindesc->key_val;
     }
     else
     {
         /* 按下 */
         key_val = pindesc->key_val;
     }
 
     ev_press = 1;                  /* 表示中断发生了 */
     wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */
     
     //kill_fasync (&button_async, SIGIO, POLL_IN);
     
     return IRQ_RETVAL(IRQ_HANDLED);
}
 
static int sixth_drv_open( static int sixth_drv_open( struct inode *inode, struct file *file)
{
#if 0
/*
*刚开始定义为1然后自减变为0返回1,再取非则不执行函数。如果在这个函数没关闭之前又想打开则
*先自减变为-1返回0,再去非则变为1,则运行函数。先自加变为0,然后返回繁忙
struct file *file)
{
#if 0
/*
*刚开始定义为1然后自减变为0返回1,再取非则不执行函数。如果在这个函数没关闭之前又想打开则
*先自减变为-1返回0,再去非则变为1,则运行函数。先自加变为0,然后返回繁忙
*/ 
     if file *file)
相关文章
相关标签/搜索