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(
struct
inode *inode,
struct
file *file)
{
#if 0
/*
*刚开始定义为1然后自减变为0返回1,再取非则不执行函数。如果在这个函数没关闭之前又想打开则
*先自减变为-1返回0,再去非则变为1,则运行函数。先自加变为0,然后返回繁忙
*/
if
(!atomic_dec_and_test(&canopen))
//原子操作,自减操作后测试其是否为0,为0则返回true,否则返回false。
{
atomic_inc(&canopen);
return
-EBUSY;
}
#endif
if
(file->f_flags & O_NONBLOCK)
{
if
(down_trylock(&button_lock))
return
-EBUSY;
}
else
{
/* 获取信号量 */
down(&button_lock);
}
/* GPG0,GPG3,GPG5,GPG6为中断引脚: EINT8,EINT11,EINT13,EINT14 */
request_irq(IRQ_EINT8, buttons_irq, IRQT_BOTHEDGE,
"K1"
, &pins_desc[0]);
request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE,
"K2"
, &pins_desc[1]);
request_irq(IRQ_EINT13, buttons_irq, IRQT_BOTHEDGE,
"K3"
, &pins_desc[2]);
request_irq(IRQ_EINT14, buttons_irq, IRQT_BOTHEDGE,
"K4"
, &pins_desc[3]);
return
0;
}
ssize_t sixth_drv_read(
struct
file *file,
char
__user *buf,
size_t
size, loff_t *ppos)
{
if
(size != 1)
return
-EINVAL;
if
(file->f_flags & O_NONBLOCK)
{
if
(!ev_press)
return
-EAGAIN;
}
else
{
/* 如果没有按键动作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);
}
/* 如果有按键动作, 返回键值 */
copy_to_user(buf, &key_val, 1);
ev_press = 0;
return
1;
}
int
sixth_drv_close(
struct
inode *inode,
struct
file *file)
{
//atomic_inc(&canopen); //原子加1
free_irq(IRQ_EINT8, &pins_desc[0]);
free_irq(IRQ_EINT11, &pins_desc[1]);
free_irq(IRQ_EINT13, &pins_desc[2]);
free_irq(IRQ_EINT14, &pins_desc[3]);
up(&button_lock);
return
0;
}
# if 0
static
unsigned sixth_drv_poll(
struct
file *file, poll_table *wait)
{
unsigned
int
mask = 0;
poll_wait(file, &button_waitq, wait);
// 不会立即休眠
if
(ev_press)
mask |= POLLIN | POLLRDNORM;
return
mask;
}
#endif
# if 0
static
int
sixth_drv_fasync (
int
fd,
struct
file *filp,
int
on)
{
printk(
"driver: sixth_drv_fasync\n"
);
return
fasync_helper (fd, filp, on, &button_async);
}
#endif
static
struct
file_operations sencod_drv_fops = {
.owner = THIS_MODULE,
/* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = sixth_drv_open,
.read = sixth_drv_read,
.release = sixth_drv_close,
//.poll = sixth_drv_poll,
//.fasync = sixth_drv_fasync,
};
int
major;
static
int
sixth_drv_init(
void
)
{
major = register_chrdev(0,
"sixth_drv"
, &sencod_drv_fops);
sixthdrv_class = class_create(THIS_MODULE,
"sixth_drv"
);
sixthdrv_class_dev = class_device_create(sixthdrv_class, NULL, MKDEV(major, 0), NULL,
"buttons"
);
/* /dev/buttons */
// gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
// gpfdat = gpfcon + 1;
return
0;
}
static
void
sixth_drv_exit(
void
)
{
unregister_chrdev(major,
"sixth_drv"
);
class_device_unregister(sixthdrv_class_dev);
class_destroy(sixthdrv_class);
// iounmap(gpfcon);
return
0;
}
module_init(sixth_drv_init);
module_exit(sixth_drv_exit);
MODULE_LICENSE(
"GPL"
);
二.应用程序代码
三.分析 1. 原子操作 首先我们来看这张图,这张图就是上次我们写驱动的驱动,我在让驱动在后台运行,你惊讶的发现他还能再启动一个应用程序。这是怎么回事呢。我们的驱动程序能同时让多个应用程序打开(很明显这样有些时候会产生很多问题)。有什么办法避免这种情况?让一个应用程序只能打开一个驱动。原子操作是一种方法,信号量也是一种。下面我来分别介绍下。 1. 原子操作 首先我们定义了原子变量
假定我们没有打开。初始值是1,当应用程序第一次打开时候自减值变为0返回true然后取非为假了,不执行if()语句。当这个程序没运行结束也就是没关闭。此事原子的值为0,若再有人要打开则在if()中先自减为-1,然后返回false,再取非则为真,要执行if(),if()里面干了啥?首先当然是让原子回复正常的状态0啦。然后返回一个错误。这样就能实现应用程序只能调用一次驱动了。 下面是图片 2. 信号量 相比于上面的应用程序,下面的信号量的方法解决问题。 信号量(semaphore)是用于保护临界区的一种常用方法,只有得到信号量的进程才能执行临界区代码。 三部曲: 一.定义
三.释放
3. 阻塞与非阻塞 阻塞操作 非阻塞操作 应用函数实现:
驱动实现: 在open()函数中:
如果非阻塞方式,打开后检测是否能上锁。如果已经上锁则返回错误。如果是阻塞方式,检测是否上锁,如果已经上锁则休眠 在read()函数中:
对比下应该很容易理解阻塞与非阻塞。 |