📄 test.c
字号:
#define _NO_VERSION_
#include <linux/module.h>
#include <linux/version.h>
#include <linux/config.h>
#include <asm/uaccess.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <asm/segment.h>
#include <linux/kernel.h>
extern int errno;
unsigned int test_major=0;
char kernel_version []=UTS_RELEASE;
static ssize_t read_test(struct file * file,char *buf,size_t count,loff_t * f_pos)
//读数
{
int left;
/* if(verify_area(VERIFY_WRITE,buf,count) == -EFAULT )
return -EFAULT;*/
for(left=count;left>0;left--)
{
__put_user(1,buf);
buf++;
}
return count;
}
static ssize_t write_test(struct file * file,const char *buf,size_t count,loff_t *f_pos)
//写数
{
return count;
}
static int open_test(struct inode * node,struct file * file) //打开设备
{
MOD_INC_USE_COUNT;
return 0;
}
static int release_test(struct inode * inode,struct file * file) //关闭设备
{
MOD_DEC_USE_COUNT;
return 0;
}
struct file_operations test_fops ={
read:read_test,
write:write_test,
open:open_test,
release:release_test
};
int init_module(void) //模块初始化
{
int result;
result = register_chrdev(0,"mydev",&test_fops);
if(result < 0)
{
printk("mydev: can't get major number.\n");
return result;
}
if(test_major==0) test_major=result;
return 0;
}
void cleanup_module(void) //模块删除
{
unregister_chrdev(test_major,"mydev");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -