当前位置: 首页 > >

字符设备驱动之同步互斥阻塞

发布时间:

注:代码有完整的注释,方便阅读


开发环境: ubuntu18.04
*台: JZ2440
kernel: Linux-3.4.2
交叉编译工具: arm-linux-gcc-4.4.3


驱动程序如下:


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


#define DEVICE_NAME "sixth_drv" //设备的名称需要和文件系统中设备节点名称区分开

/* 1.1、确定主设备号,如果没有确定则由内核自动分配 */
static int major;

static struct cdev sixth_cdev;

static dev_t devid;
static struct class * sixthdrv_class;

/* 定义按键GPIO寄存器指针 */
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
volatile unsigned long *gpgcon = NULL;
volatile unsigned long *gpgdat = NULL;

/* 按键描述结构体 */
static struct pin_desc{
unsigned int pin;
unsigned int key_val;
};

/* 键值 按下时;0x01 0x02 0x03 0x04 */
/* 键值 松开后;0x81 0x82 0x83 0x84 */
static unsigned char key_val;

static struct pin_desc key_pin_desc[4] = {
{S3C2410_GPF(0), 0x01},
{S3C2410_GPF(2), 0x02},
{S3C2410_GPG(3), 0x03},
{S3C2410_GPG(11), 0x04},
};

/* 等待序列 */
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/* 唤醒进程标志,在中断函数中将它置1,在read将它置0, */
static volatile int evpress = 0;

/* 定义fasync_struct结构体 */
static struct fasync_struct *button_async;



//static atomic_t canopen = ATOMIC_INIT(1); //定义原子变量并初始化为1

//static DECLARE_MUTEX(button_lock); //定义互斥锁
struct semaphore button_lock;


/* 中断处理函数 */
static irqreturn_t buttons_irq(int irq, void* dev_id)
{
/* 先获得资源描述 */
struct pin_desc* pindesc = (struct pin_desc *)dev_id;

unsigned int pinval = 0;

/* 根据得到的资源描述结构体,获得引脚电* */
pinval = s3c2410_gpio_getpin(pindesc->pin);
if(pinval)
key_val = 0x80 | pindesc->key_val;
else
key_val = pindesc->key_val;

/* 唤醒进程 */
evpress = 1;
wake_up_interruptible(&button_waitq);

/* 在设备资源可获得时,调用 kill_fasync()函数发相应的信号 */
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
if (!atomic_dec_and_test(&canopen))
{
atomic_inc(&canopen);
return -EBUSY;
}
#endif
/* 非阻塞的方式打开设备 */
if(file->f_flags & O_NONBLOCK)
{
/* 尝试获取信号量,如果失败,直接返回 */
if(down_trylock(&button_lock))
return -EBUSY;
}
else
{
/* 阻塞,直到获取信号量 */
down(&button_lock);
}

/* 申请中断 配置GPF0,2 GPG3,11为输入引脚 */
request_irq(IRQ_EINT0, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S2", &key_pin_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S3", &key_pin_desc[1]);
request_irq(IRQ_EINT11, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S4", &key_pin_desc[2]);
request_irq(IRQ_EINT19, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S5", &key_pin_desc[3]);
return 0;
}

static 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)
{
/* 如果ev_press不等于1,说明没有进入按键中断,没有按键事件产生,直接返回 */
if (!evpress)
return -EAGAIN;
}
else
{
/* 在读函数中,如果按键没有操作,就在此处休眠,当evpress=1时,说明发生按键中断,则继续向下执行 */
wait_event_interruptible(button_waitq, evpress);
}


/* evpress在中断函数中置1,在此处置0 */
evpress = 0;

copy_to_user(buf, &key_val, sizeof(key_val));

return sizeof(key_val);
}

static int sixth_drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT0, &key_pin_desc[0]);
free_irq(IRQ_EINT2, &key_pin_desc[1]);
free_irq(IRQ_EINT11, &key_pin_desc[2]);
free_irq(IRQ_EINT19, &key_pin_desc[3]);

/* 释放信号量 */
up(&button_lock);
return 0;
}

static unsigned int sixth_drv_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait);// 不会立即休眠
if(evpress)
mask |= POLLIN | POLLRDNORM;
return mask;
}


static int sixth_drv_fasync (int fd, struct file *filp, int on)
{
printk("driver: sixth_drv_fasync
");
/* 发给谁肯定是这个fasync_struct结构中定义的。这个结构“button_async”要做些初始化 */
return fasync_helper(fd, filp, on, &button_async);
}



/* 1.2、构造file_operations结构体,里面是操作设备的函数 */
static struct file_operations sixth_drv_fops = {
.owner = THIS_MODULE,
.open = sixth_drv_open,
.read = sixth_drv_read,
.release = sixth_drv_close,
.poll = sixth_drv_poll,
.fasync = sixth_drv_fasync,
};

/* 2、定义入口函数 */
static int sixth_drv_init(void)
{
/* 2.1 分配设备号(动态和静态两种) */
if(major)
{
//如果确定了主设备号,就是用静态分配函数
devid = MKDEV(major, 0);
register_chrdev_region(devid, 2, DEVICE_NAME);
}
else
{
alloc_chrdev_region(&devid, 0, 2, DEVICE_NAME); //cat /proc/device
major = MAJOR(devid);
devid = MKDEV(major, 0);
}

/* 2.2 用sixth_drv_fops结构体初始化设备描述结构体cdev,建立关联 */
cdev_init(&sixth_cdev, &sixth_drv_fops);

/* 2.3 注册字符设备,将字符设备结构体添加到内核 */
cdev_add(&sixth_cdev, devid, 2);

/* 3、使用udev机制自动创建的设备节点 */
/* 3.1 注册一个类,使得mdev在/dev目录下自动创建设备节点,不用再使用mknod命令 */
sixthdrv_class = class_create(THIS_MODULE, DEVICE_NAME);//创建一个sixth_drv类 /sys/class/DEVICE_NAME

/* 在文件系统中创建/dev/irqbtn 这个设备文件,应用程序调用 */
device_create(sixthdrv_class, NULL, MKDEV(major, 0), NULL, "irqbtn"); //dev/irqbtn

/* 4、硬件操作,内存映射用于操作IO口 */
gpfcon = (volatile unsigned long*)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long*)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;

/* 初始化信号量为1 */
sema_init(&button_lock, 1);

return 0;
}


/* 3、定义出口函数 */
static void sixth_drv_exit(void)
{
iounmap(gpfcon); //取消内存映射
iounmap(gpgcon);
device_destroy(sixthdrv_class, MKDEV(major, 0)); //取消自动创建设备节点
class_destroy(sixthdrv_class); //释放sixthdrv_class
cdev_del(&sixth_cdev); //从内核中卸载字符设备描述结构体
unregister_chrdev_region(devid, 2); //取消设备号的申请
}

/* 4、修饰入口函数和出口函数 */
module_init(sixth_drv_init);
module_exit(sixth_drv_exit);

MODULE_LICENSE("GPL");




应用程序如下:


#include
#include
#include
#include
#include
#include
#include
#include

int fd = 0;


/* 信号处理函数 */
void my_signal_fun(int signum)
{
unsigned char key_val = 0;
read(fd, &key_val, 1);
printf("key_val: 0x%x
", key_val);
}

int main(int argc, char **argv)
{

unsigned char key_val;
int Oflags = 0;
int ret = 0;

/* 监听信号 */
// signal(SIGIO, my_signal_fun);

/* 非阻塞方式打开设备文件 */
fd = open("/dev/irqbtn", O_RDWR | O_NONBLOCK);
if(fd < 0)
{
printf("cannot open!!
");
return -1;
}
/* 应用程序告诉内核,信号发给谁 */
// fcntl(fd, F_SETOWN, getpid());
// /* 通过“F_GETFL”读出“flags”,在 flags 上置上“FASYNC”位 */
// Oflags = fcntl(fd, F_GETFL);
// fcntl(fd, F_SETFL, Oflags|FASYNC);


while(1)
{
ret = read(fd, &key_val, 1);
printf("key_val: 0x%x, ret = %d
", key_val, ret);
sleep(5);
}

return 0;
}







友情链接: