5 Star 30 Fork 14

wei/share_open_code

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
test.c 1.97 KB
一键复制 编辑 原始数据 按行查看 历史
sunshine 提交于 2020-09-25 15:36 +08:00 . first add
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#define DEBUG(fmt, ...) \
do{ \
if(if_debug) \
printk(KERN_INFO "DEBUG > " fmt, ##__VA_ARGS__); \
}while(0)
bool if_debug = false;
/*定义一个bool类型的变量,作为一个模块入参,在装载模块时可赋值*/
module_param(if_debug, bool, S_IRUSR);
/*方式1*/
#define USE_OLD_INTERFACE 0
#define CHRDEV_NAME "test_char_dev"
#if USE_OLD_INTERFACE
#define DEV_COUNT 1
/*设备号*/
static dev_t devno;
/*定义一个字符设备*/
static struct cdev *char_dev;
#else
/*主设备号*/
static dev_t major;
#endif
static int test_open (struct inode *inode, struct file *file)
{
DEBUG("open test..\r\n");
return 0;
}
static int test_close (struct inode *inode, struct file *file)
{
DEBUG("close test..\r\n");
return 0;
}
static const struct file_operations fops = {
.owner = THIS_MODULE,
.open = test_open,
.release = test_close,
};
static int __init test_init(void)
{
#if USE_OLD_INTERFACE
/*申请设备号*/
alloc_chrdev_region(&devno,0,DEV_COUNT,CHRDEV_NAME);
/*定义一个字符设备*/
char_dev = cdev_alloc();
/*绑定字符设备*/
cdev_init(char_dev,&fops);
/*注册字符设备*/
cdev_add(char_dev,devno,DEV_COUNT);
#else
major = register_chrdev(0,CHRDEV_NAME,&fops);
#endif
return 0;
}
static void __exit test_exit(void)
{
#if USE_OLD_INTERFACE
/*注销一个字符设备*/
cdev_del(char_dev);
/*注销申请的设备号*/
unregister_chrdev_region(devno,DEV_COUNT);
#else
unregister_chrdev(major,CHRDEV_NAME);
#endif
}
/*此宏声明内核模块的初始化入口点*/
module_init(test_init);
/*此宏声明内核模块的退出入口点*/
module_exit(test_exit);
/*声明开源协议*/
MODULE_LICENSE("GPL");
/*声明作者*/
MODULE_AUTHOR("wei");
/*声明模块的描述*/
MODULE_DESCRIPTION("this is a test driver");
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/wei513723/share_open_code.git
git@gitee.com:wei513723/share_open_code.git
wei513723
share_open_code
share_open_code
master

搜索帮助