驱动程序之_1_字符设备_5_原子操作
有的程序可能为了避免错误,不能被线程调度机制所打断,这时候就用到了原子操作,在进行原子操作的过程中,不会被任何事情所打断
例如,我们想要让驱动设备只能被打开一次,通常的想法就是
1、设置全局标志位
2、在open函数中对其运算,再判断是否是某个值,从而决定是否打开设备
3、在close函数中再对其进行与入口函数中相反的运算
这种做法不严谨,在系统中,应用程序只占据一个进程,系统每一定的时间会切换进程执行,如果在标志位运算已经开始,但未结束时,又打开一次设备,这时系统可能会跳到这个进程,最终导致两次打开设备都成功(运算在汇编指令中并不是一次完成的,而是进行了取值、运算、存值等过程,中间存在极小的时间间隔),对此即可采用原子操作,使得他在完成标志位运算前,不能跳到其他进程
定义原子变量(初始化为1)
static atomic_t canopen = ATOMIC_INIT(1);
自减,并判断是否为0,为0返回true
atomic_dec_and_test(&canopen)
自增
atomic_inc(&canopen);
附上驱动程序代码,测试用的应用程序与fasync中所用的相同
驱动程序
#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>
#include <linux/device.h>
#include <linux/irq.h>
static struct class *my_irq_class;
static struct class_device *my_irq_class_device;
static unsigned char key_val;
struct fasync_struct *button_fasync;
static atomic_t canopen = ATOMIC_INIT(1);
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
struct pin_desc key_pins_desc[4]={
{S3C2410_GPF0,0x01},
{S3C2410_GPF2,0x02},
{S3C2410_GPG3,0x03},
{S3C2410_GPG11,0x04},
};
static irqreturn_t buttons_irq(int irq,void *dev_id)
{
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;
pinval = s3c2410_gpio_getpin(pindesc->pin);
if(pinval) //松开
{
key_val = 0x80 | pindesc->key_val;
}
else
{
key_val = pindesc->key_val;
}
kill_fasync(&button_fasync, SIGIO, POLL_IN);
return IRQ_HANDLED;
}
static int my_irq_drv_open(struct inode *inode, struct file *file)
{
if(!atomic_dec_and_test(&canopen))
{
atomic_inc(&canopen);
return -EBUSY;
}
request_irq(IRQ_EINT0,buttons_irq,IRQT_BOTHEDGE,"S2",&key_pins_desc[0]);
request_irq(IRQ_EINT2,buttons_irq,IRQT_BOTHEDGE,"S3",&key_pins_desc[1]);
request_irq(IRQ_EINT11,buttons_irq,IRQT_BOTHEDGE,"S4",&key_pins_desc[2]);
request_irq(IRQ_EINT19,buttons_irq,IRQT_BOTHEDGE,"S5",&key_pins_desc[3]);
printk(“requestedr\n");
return 0;
}
int my_irq_drv_close(struct inode *inode,struct file *file)
{
atomic_inc(&canopen);
free_irq(IRQ_EINT0,&key_pins_desc[0]);
free_irq(IRQ_EINT2,&key_pins_desc[1]);
free_irq(IRQ_EINT11,&key_pins_desc[2]);
free_irq(IRQ_EINT19,&key_pins_desc[3]);
printk("freed\r\n");
return 0;
}
ssize_t my_irq_drv_read(struct file *file, char __user *buf, size_t size, loff_t * ppos)
{
if(size != 1)
return -EINVAL;
copy_to_user(buf,&key_val,1);
return 0;
}
static int my_irq_drv_fasync(int fd,struct file *filp,int on)
{
return fasync_helper(fd,filp,on,&button_fasync);
}
static struct file_operations irq_drv_fops = {
.owner = THIS_MODULE,
.open = my_irq_drv_open,
.read = my_irq_drv_read,
.release = my_irq_drv_close,
.fasync = my_irq_drv_fasync,
};
static int major;
static int my_irq_drv_init(void)
{
major = register_chrdev(0,"my_irq_drv",&irq_drv_fops);
my_irq_class = class_create(THIS_MODULE,"my_irq_drv");
my_irq_class_device = class_device_create(my_irq_class,NULL,MKDEV(major,0),NULL,"key_irq_drv");
return 0;
}
static void my_irq_drv_exit(void)
{
unregister_chrdev(major,"my_irq_drv");
class_device_unregister(my_irq_class_device);
class_destroy(my_irq_class);
}
module_init(my_irq_drv_init);
module_exit(my_irq_drv_exit);
MODULE_LICENSE("GPL");