注:代码都有完整的注释,方便阅读
开发环境: 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>
#define DEVICE_NAME "second_drv" //设备的名称需要和文件系统中设备节点名称区分开
/* 1.1、确定主设备号,如果没有确定则由内核自动分配 */
static int major;
static struct cdev second_cdev;
static dev_t devid;
static struct class * seconddrv_class;
//定义按键GPIO寄存器指针
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
volatile unsigned long *gpgcon = NULL;
volatile unsigned long *gpgdat = NULL;
static int second_drv_open(struct inode *inode, struct file *file)
{
//查看芯片手册,配置4个按键的寄存器
*gpfcon &= ~( (0x3<<0) | (0x3<<4) );
*gpgcon &= ~( (0x3<<6) | (0x3<<22) );
return 0;
}
static ssize_t second_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
unsigned char key_val[4]; //用来返回四个按键值
int regval = 0;
if(size != sizeof(key_val))
return -EINVAL;
regval = *gpfdat;
key_val[0] = (regval & 1<<0) ? 1 : 0;
key_val[1] = (regval & 1<<2) ? 1 : 0;
regval = *gpgdat;
key_val[2] = (regval & 1<<3) ? 1 : 0;
key_val[3] = (regval & 1<<11) ? 1 : 0;
copy_to_user(buf, key_val, sizeof(key_val));
return sizeof(key_val);
}
/* 1.2、构造file_operations结构体,里面是操作设备的函数 */
static struct file_operations second_drv_fops = {
.owner = THIS_MODULE,
.open = second_drv_open,
.read = second_drv_read,
};
/* 2、定义入口函数 */
static int second_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 用second_drv_fops结构体初始化设备描述结构体cdev,建立关联 */
cdev_init(&second_cdev, &second_drv_fops);
/* 2.3 注册字符设备,将字符设备结构体添加到内核 */
cdev_add(&second_cdev, devid, 2);
/* 3、使用udev机制自动创建的设备节点 */
/* 3.1 注册一个类,使得mdev在/dev目录下自动创建设备节点,不用再使用mknod命令 */
seconddrv_class = class_create(THIS_MODULE, DEVICE_NAME);//创建一个seconddrv类 /sys/class/DEVICE_NAME
/* 在文件系统中创建/dev/seconddrv 这个设备文件,应用程序调用 */
device_create(seconddrv_class, NULL, MKDEV(major, 0), NULL, "BUTTON"); //dev/button
/* 4、硬件操作,内存映射用于操作IO口 */
gpfcon = (volatile unsigned long*)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long*)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
return 0;
}
/* 3、定义出口函数 */
static void second_drv_exit(void)
{
iounmap(gpfcon); //取消内存映射
iounmap(gpgcon);
device_destroy(seconddrv_class, MKDEV(major, 0)); //取消自动创建设备节点
class_destroy(seconddrv_class); //释放seconddrv_class
cdev_del(&second_cdev); //从内核中卸载字符设备描述结构体
unregister_chrdev_region(devid, 2); //取消设备号的申请
}
/* 4、修饰入口函数和出口函数 */
module_init(second_drv_init);
module_exit(second_drv_exit);
MODULE_LICENSE("GPL");
测试程序如下:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd = 0;
unsigned char Key_val[4] = {0};
int cnt = 0;
fd = open("/dev/BUTTON", O_RDWR);
if(fd < 0)
{
printf("connot open !\n");
return -1;
}
while(1)
{
read(fd, Key_val, sizeof(Key_val));
if(!Key_val[0]||!Key_val[1]||!Key_val[2]||!Key_val[3])
{
printf("%04d key value pressed:%d %d %d %d\n",cnt++, Key_val[0],
Key_val[1],Key_val[2],Key_val[3]);
}
}
return 0;
}