代码拉取完成,页面将自动刷新
#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");
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。