Linux驱动---自动创建设备节点

在这里插入图片描述

1、自动创建设备节点需要依赖于几个函数:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述函数device_create差不多就是将一些硬件信息什么的导入用户空间,创建一个设备节点

在这里插入图片描述

2、代码

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/cdev.h>

static int major = 237;
static int minor = 0;
static dev_t devno;
static struct cdev cdev;
struct device *class_dev = NULL;
struct class *cls;


static int hello_open (struct inode *inode, struct file *filep)
{
	printk("hello_open()\n");
	return 0;
}
static int hello_release (struct inode *inode, struct file *filep)
{
	printk("hello_release()\n");

	return 0;
}
static struct file_operations hello_ops = 
{
	.open = hello_open,
	.release = hello_release,
};
static int hello_init(void)
{
	int result;
	int error;
	
	printk("hello_init \n");
	result = register_chrdev( major, "hello", &hello_ops);
	if(result < 0)
	{
		printk("register_chrdev fail \n");
		return result;
	}
	cls = class_create(THIS_MODULE, "hellocls");
	if (IS_ERR(cls)) {
		printk(KERN_ERR "class_create() failed for cls\n");
		result = PTR_ERR(cls);
		goto out_err_1;
	}
	//如果不知道class_create的用法,可以搜索内核其他代码中用到这个函数的地方
	devno = MKDEV(major, minor);
	
	class_dev = device_create(cls, NULL, devno, NULL, "hellodev");
	if (IS_ERR(class_dev)) {
		result = PTR_ERR(class_dev);
		goto out_err_2;
	}
	
	return 0;

out_err_2:
	class_destroy(cls);
out_err_1:
	unregister_chrdev(major,"hello");//先释放设备号
	return 	result;
}
static void hello_exit(void)
{
	printk("hello_exit \n");
	device_destroy(cls, devno);
	class_destroy(cls);
	unregister_chrdev(major,"hello");
	return;
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
//proc/devices

3、放虚拟机上加载

在这里插入图片描述
这个加载成功以后:
在这里插入图片描述

在这里插入图片描述
我们看到在hello_cls下的hello_dev是一个链接文件,class_dev = device_create(cls, NULL, devno, NULL, "hellodev");因为device_create()依赖于cls创建了一个叫“hellodev”的设备

设备节点被创建在/dev下

在这里插入图片描述我们发现设备节点就创建好了

4、测试程序

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
main()
{
        int fd;
        fd = open("/dev/hellodev",O_RDWR);
        if(fd<0)
        {
                perror("open fail \n");
                return;
        }
        printf("open ok \n ");
}

在这里插入图片描述
执行dmesg后:
在这里插入图片描述
测试成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值