注:代码有完整的注释,方便阅读
开发环境: ubuntu18.04
平台: JZ2440
kernel: Linux-3.4.2
交叉编译工具: arm-linux-gcc-4.4.3
驱动程序如下:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>
#include <linux/cdev.h>
#include <mach/gpio.h>
#include <linux/interrupt.h>
#include <linux/device.h>
#include <linux/sched.h>
#include <linux/poll.h>
#define DEVICE_NAME "seventh_drv" //设备的名称需要和文件系统中设备节点名称区分开
/* 1.1、确定主设备号,如果没有确定则由内核自动分配 */
static int major;
static struct cdev seventh_cdev;
static dev_t devid;
static struct class * seventhdrv_class;
/* 声明定时器 */
static struct timer_list buttons_timer;
/* 定义按键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},
};
/* 定义全局pin_desc结构变量 */
static struct pin_desc *irq_pd;
/* 等待序列 */
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)
{
/* 10ms之后启动定时器 */
irq_pd = (struct pin_desc *)dev_id;
mod_timer(&buttons_timer, jiffies+HZ/100);
return IRQ_RETVAL(IRQ_HANDLED);
}
static int seventh_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 seventh_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 seventh_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 seventh_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 seventh_drv_fasync (int fd, struct file *filp, int on)
{
printk("driver: seventh_drv_fasync\n");
/* 发给谁肯定是这个fasync_struct结构中定义的。这个结构“button_async”要做些初始化 */
return fasync_helper(fd, filp, on, &button_async);
}
/* 1.2、构造file_operations结构体,里面是操作设备的函数 */
static struct file_operations seventh_drv_fops = {
.owner = THIS_MODULE,
.open = seventh_drv_open,
.read = seventh_drv_read,
.release = seventh_drv_close,
.poll = seventh_drv_poll,
.fasync = seventh_drv_fasync,
};
/* 定时器处理函数 */
static void buttons_timer_function(unsigned long data)
{
/* 先获得资源描述 */
struct pin_desc* pindesc = irq_pd;
unsigned int pinval = 0;
if(!pindesc)
{
return ;
}
/* 根据得到的资源描述结构体,获得引脚电平 */
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);
}
/* 2、定义入口函数 */
static int seventh_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 用seventh_drv_fops结构体初始化设备描述结构体cdev,建立关联 */
cdev_init(&seventh_cdev, &seventh_drv_fops);
/* 2.3 注册字符设备,将字符设备结构体添加到内核 */
cdev_add(&seventh_cdev, devid, 2);
/* 3、使用udev机制自动创建的设备节点 */
/* 3.1 注册一个类,使得mdev在/dev目录下自动创建设备节点,不用再使用mknod命令 */
seventhdrv_class = class_create(THIS_MODULE, DEVICE_NAME);//创建一个seventh_drv类 /sys/class/DEVICE_NAME
/* 在文件系统中创建/dev/irqbtn 这个设备文件,应用程序调用 */
device_create(seventhdrv_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);
/* 初始化定时器 */
init_timer(&buttons_timer);
buttons_timer.function = buttons_timer_function;
add_timer(&buttons_timer);
return 0;
}
/* 3、定义出口函数 */
static void seventh_drv_exit(void)
{
iounmap(gpfcon); //取消内存映射
iounmap(gpgcon);
device_destroy(seventhdrv_class, MKDEV(major, 0)); //取消自动创建设备节点
class_destroy(seventhdrv_class); //释放seventhdrv_class
cdev_del(&seventh_cdev); //从内核中卸载字符设备描述结构体
unregister_chrdev_region(devid, 2); //取消设备号的申请
}
/* 4、修饰入口函数和出口函数 */
module_init(seventh_drv_init);
module_exit(seventh_drv_exit);
MODULE_LICENSE("GPL");
应用程序如下:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
int fd = 0;
/* 信号处理函数 */
void my_signal_fun(int signum)
{
unsigned char key_val = 0;
read(fd, &key_val, 1);
printf("key_val: 0x%x\n", 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);
if(fd < 0)
{
printf("cannot open!!\n");
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\n", key_val, ret);
}
return 0;
}