• linux字符设备驱动程序框架(老方法)


    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/init.h>
    #include <linux/fs.h>
    #include <linux/delay.h>
    #include <asm/uaccess.h>
    #include <asm/irq.h>
    #include <asm/io.h>
    #include <asm/arch/regs-gpio.h>
    #include <asm/hardware.h>
    #include <linux/cdev.h>
    #include <linux/types.h>
    
    #define	xxx_DEVICE_COUNT	1	
    
    /*自己主动创建设备节点类*/
    static struct class *xxx_dev_class;
    static struct class_device *xxx_dev_class_dev;
    
    /*
    	xxx设备相关的相关操作函数:open、read、write、close、ioctl等
    */
    static int xxx_dev_open(struct inode *inode, struct file *filp)
    {
    	printk("Open xxx device OK.
    ");
    	return 0;
    }
    
    static int xxx_dev_close(struct inode *inode, struct file *filp)
    {
    	printk("Close xxx device OK.
    ");
    	return 0;
    }
    
    static int xxx_dev_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
    {
    	printk("Write xxx device OK.
    ");
    	return 0;
    }
    
    static int xxx_dev_read(struct file *file, const char __user *buf, size_t count, loff_t ppos)
    {
    	printk("Read xxx device OK.
    ");
    	return 0;
    }
    
    static int xxx_dev_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
    {
    	printk("DRIVER : Get cmd %d.
    ", cmd);
    	return 0;
    }
    
    /*
    	xxx设备操作函数结构体
    */
    struct file_operations xxx_fops = {
    	.owner = THIS_MODULE,
    	.open = xxx_dev_open,
    	.release = xxx_dev_close,
    	.read = xxx_dev_read,
    	.write = xxx_dev_write,
    	.ioctl = xxx_dev_ioctl,
    };
    
    /*
    	xxx设备驱动模块的注冊和卸载
    */
    int xxx_major = 0;
    static int __init initialization_xxx_dev(void)
    {
    	/* 注冊设备号 */
    	printk("Before register xxx Major = %d
    ", xxx_major);
    	if (xxx_major) {
    		register_chrdev(xxx_major, "xxx", &xxx_fops);
    	} else {
    		xxx_major = register_chrdev(0, "xxx", &xxx_fops);
    	}
    	printk("After register xxx Major = %d
    ", xxx_major);
    
    	/* 自己主动生成设备节点 */
    	xxx_dev_class = class_create(THIS_MODULE, "xxx_dev");
    	xxx_dev_class_dev = class_device_create(xxx_dev_class, NULL, MKDEV(xxx_major, 0), NULL, "xxx");
    	/* 模块初始化成功必须返回0 */
    	printk("Module register OK.
    ");
    	return 0;
    }
    
    static void __exit cleanup_xxx_dev(void)
    {
    	/* 删除设备文件 */
    	unregister_chrdev(xxx_major, "xxx");
    	class_device_unregister(xxx_dev_class_dev);
    	class_destroy(xxx_dev_class);
    
    	printk("Module unregister OK.
    ");
    }
    
    /*
    	模块注冊与卸载
    */
    module_init(initialization_xxx_dev);
    module_exit(cleanup_xxx_dev);
    
    /*
    	模块传參:insmod char_driver_frame_old.ko xxx_major=xxx
    */
    module_param(xxx_major, int, S_IRUGO);
    
    /*
    	模块的相关声明
    */
    MODULE_AUTHOR("lhbo");
    MODULE_DESCRIPTION("GPIO Driver for xxx");
    MODULE_LICENSE("GPL");
    
  • 相关阅读:
    golang roadrunner中文文档(五)集成到其他服务 docker
    golang roadrunner中文文档(四)app服务器
    golang roadrunner中文文档(三)HTTPS 和 HTTP/2
    golang roadrunner中文文档(二)PHP Workers
    golang roadrunner中文文档(一)基础介绍
    Laravel Octane中文文档
    PHP海量excel数据导出方案解决 xlswriter性能测试
    中国车牌归属地数据库
    php模仿go实现多变量返回
    laravel 异步队列任务 广播系统
  • 原文地址:https://www.cnblogs.com/yfceshi/p/7207331.html
Copyright © 2020-2023  润新知