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
后:
测试成功