驱动程序之_1_字符设备_6_互斥锁与信号量
上一篇文章的要求,除了原子操作,也可以使用本文的互斥锁代替实现(很多地方还不大理解,有些地方可能写得不太对,欢迎大家指出~)
互斥锁的作用是:保证共享数据操作的完整性,每个对象都对应于一个可称为" 互斥锁" 的标记,这个标记用来保证在任一时刻,只能有一个线程访问该对象
定义互斥锁
static DECLARE_MUTEX(button_lock);
初始化信号量
void sema_init (struct semaphore *sem, int val);
void init_MUTEX(struct semaphore *sem);//初始化为0
获得信号量,获取不成功就休眠,这样可能会导致僵尸进程的出现
void down(struct semaphore * sem);
获取信号量,获取不成功就返回1,不会休眠
int down_trylock(struct semaphore * sem);
释放信号量
void up(struct semaphore * sem);
附上驱动程序代码,测试用的应用程序代码与前两篇文章的相同
#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 DECLARE_MUTEX(button_lock);
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)
{
down(&button_lock);
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("requested\r\n");
return 0;
}
int my_irq_drv_close(struct inode *inode,struct file *file)
{
up(&button_lock);
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");