0x03 嵌入式---驱动设备的注册

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>

struct file_operations dev_fops;
static unsigned int dev_major = 255;
static struct class* dev_class;
static struct device* dev_device;

static int __init dev_init(void)
{
    int ret;
    ret = register_chrdev(dev_major, "myself_dev", &dev_fops); //注册字符设备
    if ( !ret )
    {
        printk(KERN_INFO "register ok!\n");
    }
    else
    {
        printk(KERN_INFO "register failed!\n");
        return -1;
    }

	dev_class = class_create(THIS_MODULE, "chr_dev");
    if(IS_ERR(dev_class)) 
    {
    	printk("Error: failed in creating device.\n");
    	return -1; 
    }
    
    dev_device = device_create(dev_class, NULL, MKDEV(dev_major, 0), NULL, "myself_dev");
    if(IS_ERR(dev_device)) 
    {
    	printk("Error: failed in creating device.\n");
    	return -1; 
    }
 
	return 0;
}

static void __exit dev_exit(void)
{
    device_destroy(dev_class, MKDEV(dev_major, 0));
    class_destroy(dev_class);
    unregister_chrdev(dev_major, "myself_dev");
	printk(KERN_INFO "exit ok!\n ");
}

module_init(dev_init);
module_exit(dev_exit);

MODULE_AUTHOR("weyciu");
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("A simple chr dev Module");
MODULE_ALIAS("a simplest module");

发布了31 篇原创文章 · 获赞 17 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/q759451733/article/details/102812972
今日推荐