本次使用register_chrdev_region来注册设备号
注:代码都有完整的注释,方便阅读
开发环境: 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 "first_dir" //设备的名称需要和文件系统中设备节点名称区分开
/* 1.1、确定主设备号,如果没有确定则由内核自动分配 */
static int major;
static struct cdev first_dev;
static dev_t devid;
static struct class * firstdrv_class;
//定义寄存器指针
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
static int first_drv_open(struct inode *inode, struct file *file)
{
//查看芯片手册,配置3个LED灯的寄存器
*gpfcon &= ~((0x3<<8) | (0x3<<10) | (0x3>>12) );
*gpfcon |= (0x1<<8) | (0x1<<10) | (0x1<<12);
return 0;
}
static ssize_t first_drv_write(struct file *file, const char __user *userbuf,size_t bytes, loff_t *off)
{
int val;
copy_from_user(&val, userbuf, bytes);
if(val)
*gpfdat &= ~( (1<<4) | (1<<5) | (1<<6) );
else
*gpfdat |= (1<<4) | (1<<5) | (1<<6);
return 0;
}
/* 1.2、构造file_operations结构体,里面是操作设备的函数 */
static struct file_operations first_drv_fops = {
.owner = THIS_MODULE,
.open = first_drv_open,
.write = first_drv_write,
};
/* 2、定义入口函数 */
static int first_drv_init(void)
{
/* 2.1 分配设备号(动态和静态两种) */
if(major)
{
//如果确定了主设备号,就是用静态分配函数
devid = MKDEV(major, 0);
register_chrdev_region(devid, 2, "First_dev");
}
else
{
alloc_chrdev_region(&devid, 0, 2, "First_dev"); //cat /proc/device
major = MAJOR(devid);
devid = MKDEV(major, 0);
}
/* 2.2 用first_drv_fops结构体初始化设备描述结构体cdev,建立关联 */
cdev_init(&first_dev, &first_drv_fops);
/* 2.3 注册字符设备,将字符设备结构体添加到内核 */
cdev_add(&first_dev, devid, 2);
/* 3、使用udev机制自动创建的设备节点 */
/* 3.1 注册一个类,使得mdev在/dev目录下自动创建设备节点,不用再使用mknod命令 */
firstdrv_class = class_create(THIS_MODULE, DEVICE_NAME);//创建一个firstdrv类 /sys/class/DEVICE_NAME
/* 在文件系统中创建/dev/firstdrv 这个设备文件,应用程序调用 */
device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); //dev/xyz
/* 4、硬件操作,内存映射用于操作IO口 */
gpfcon = (volatile unsigned long*)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
return 0;
}
/* 3、定义出口函数 */
static void first_drv_exit(void)
{
iounmap(gpfcon); //取消内存映射
device_destroy(firstdrv_class, MKDEV(major, 0)); //取消自动创建设备节点
class_destroy(firstdrv_class); //释放firstdrv_class
cdev_del(&first_dev); //从内核中卸载字符设备描述结构体
unregister_chrdev_region(devid, 2); //取消设备号的申请
}
/* 4、修饰入口函数和出口函数 */
module_init(first_drv_init);
module_exit(first_drv_exit);
MODULE_LICENSE("GPL");
测试程序如下:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/* firstdrvtest on
* firstdrvtest off
*/
int main(int argc, char **argv)
{
int fd;
int val = 1;
fd = open("/dev/xyz", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
if (argc != 2)
{
printf("Usage :\n");
printf("%s <on|off>\n", argv[0]);
return 0;
}
if (strcmp(argv[1], "on") == 0)
{
val = 1;
}
else
{
val = 0;
}
write(fd, &val, 4);
return 0;
}