• Linux设备驱动实现自己主动创建设备节点


    #include <linux/kernel.h>
    #include <linux/module.h>
    #include <linux/init.h>
    #include <linux/device.h> 
    #include <linux/cdev.h>
    #include <linux/fs.h>
    #include <linux/ioctl.h>
    
    #define DRIVERNAME "xxx"
    #define DEV_MINOR 0
    static dev_t xxx_devno = 0;
    static struct class * xxx_class;
    
    struct xxx_dev 
    {
    	struct cdev cdev;	
    }xxxdev;
    
    /*-----------------------------------------------------------------------------------*/
    
    static int xxx_open(struct inode *inode, struct file *filp)
    {	
    	return 0;
    }
    
    static ssize_t xxx_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
    {
    	return 0;
    }
    
    static ssize_t xxx_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos)
    {
    	return 0;
    }
    
    static int xxx_release(struct inode *inode, struct file *filp)
    {
    	return 0;
    }
    
    static int xxx_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
    {
    	int ret = 0;	
    
    	switch (cmd) 
    	{
    		case CPLD_VERSION:
    			break;
    		case CPLD_RESET:
    			break;
    		case CPLD_RELAY:
    			break;
    		case CPLD_RJ45:
    			break;
    		case CPLD_INTERUPT:
    				break;
    		case CPLD_FAN:
    			break;
    		default:
    			printk("Error xxx cmd
    ");
    			break;
    	}
    	
    	return 0;
    }
    
    /*-----------------------------------------------------------------------------------*/
    
    static struct file_operations xxx_fops = {
    	.owner = THIS_MODULE,
    	.open = xxx_open,
    	.read = xxx_read,
    	.write = xxx_write,
    	.ioctl = xxx_ioctl,
    	.close = xxx_release,
    };
    
    static int xxx_init(void)
    {
    	int ret = 0;
    
    	/*动态申请设备号*/
    	ret = alloc_chrdev_region(&xxx_devno, DEV_MINOR, 1, DRIVERNAME);
    	if (ret < 0)
    	{
    		printk("xxx register char dev failed
    ");
    		goto out;
    	}
    	/*初始化cdev结构,注冊cdev*/
    	cdev_init(&(xxxdev.cdev), &xxx_fops);
    	xxxdev.cdev.owner = THIS_MODULE;
    	ret = cdev_add(&(xxxdev.cdev), xxx_devno, 1);
    	if (ret) 
    	{
    		printk("Error adding xxx device
    ");
    		goto add_err;
    	}
    	
    	/*创建设备节点/sys/class/xxx和/dev/xxx*/
    	xxx_class = class_create(THIS_MODULE, DRIVERNAME);
    	if (IS_ERR(xxx_class)) 
    	{
    		ret = PTR_ERR(xxx_class);
    		goto class_err;
    	}
    	device_create(xxx_class, NULL, xxx_devno, NULL, DRIVERNAME); 
    	
    	printk("xxx init
    ");
    	return 0;
    
    class_err:
    	cdev_del(&(xxxdev.cdev));
    add_err:	
    	unregister_chrdev_region(xxx_devno, 1);	
    out:
    	return ret;
    }
    
    static void xxx_exit(void)
    {
    	device_destroy(xxx_class, xxx_devno);
    	class_destroy(xxx_class);
    	cdev_del(&(xxxdev.cdev));
    	unregister_chrdev_region(xxx_devno, 1);
    	printk("module xxx exit
    ");
    }
    
    module_init(xxx_init);
    module_exit(xxx_exit);
    
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("liuwei");
    MODULE_DESCRIPTION("char driver");
    

  • 相关阅读:
    CTR预估(3)--正则,交叉验证、特征提取、变量分类
    CTR预估(1)--CTR基础
    svm支持向量机系列(4) --软间隔支持向量机
    机器为什么可以学习(4)---错误和噪声
    svm支持向量机系列(3) -- kernel支持向量机
    svm支持向量机系列(2) -- 对偶支持向量机
    svm支持向量机系列(1) -- 线性支持向量机
    机器为什么可以学习(3)----vc维
    机器为什么可以学习(2)---一般化理论
    GitLab 数据库
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4079906.html
Copyright © 2020-2023  润新知