Linux驱动 | debugfs接口创建
最编程
2024-08-13 20:31:57
...
#include <linux/debugfs.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/errno.h>
#include <linux/dcache.h>
#include <linux/types.h>
static char ion_buf[512] = "hello\n";
static struct dentry *ion_dir;
static int ion_open(struct inode *inode, struct file *filp)
{
//printk("ion open\n");
return 0;
}
ssize_t ion_read(struct file *filp, char __user *buf, size_t count, loff_t *offp)
{
int retval = 0;
if ((*offp + count) > 512)
count = 512 - *offp;
if (copy_to_user(buf, ion_buf+*offp, count)) {
printk("copy to user failed, count:%ld\n", count);
retval = -EFAULT;
goto out;
}
*offp += count;
retval = count;
out:
return retval;
}
ssize_t ion_write(struct file *filp, const char __user *buff, size_t count, loff_t *offp)
{
int retval;
if (*offp > 512)
return 0;
if (*offp + count > 512)
count = 512 - *offp;
if (copy_from_user(ion_buf+*offp, buff, count)) {
printk("copy from user failed, count:%ld\n", count);
retval = -EFAULT;
goto out;
}
*offp += count;
retval = count;
out:
return retval;
}
struct file_operations my_fops = {
.owner = THIS_MODULE,
.read = ion_read,
.write = ion_write,
.open = ion_open,
};
static int __init debugfs_init(void)
{
printk("INIT MODULE\n");
//创建一个/sys/kernel/debug/ion目录
ion_dir = debugfs_create_dir("ion", NULL);
if (!ion_dir) {
printk("ion_dir is null\n");
return -1;
}
/* 创建/sys/kernel/debug/ion/test文件 */
struct dentry *filent = debugfs_create_file("test", 0644, ion_dir, NULL, &my_fops);
if (!filent) {
printk("test file is null\n");
return -1;
}
return 0;
}
static void __exit debugfs_exit(void)
{
debugfs_remove_recursive(ion_dir);
}
module_init(debugfs_init);
module_exit(debugfs_exit);
MODULE_LICENSE("GPL");
上一篇: 如何恢复已删除的Linux文件系统