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)
相關文章
相關標籤/搜索