0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

Linux驱动debugfs接口代码实现

麦辣鸡腿堡 来源:嵌入式Linux充电站 作者:Vincent 2023-09-27 11:12 次阅读

实现效果

/sys/kernel/debug/目录下创建一个ion/test文件,通过catecho的方式进行读写操作:

图片

图片

前期准备

内核配置打开debugfs:

CONFIG_DEBUG_FS=y

挂载debugfs文件系统:

mount -t debugfs none /sys/kernel/debug

代码实现

读写变量:

#include < linux/debugfs.h >
#include < linux/module.h >
#include < linux/types.h >

static struct dentry *ion_dir;
static u64 test_u64 = 0;

static int __init debugfs_init(void)
{

    //创建一个/sys/kernel/debug/ion目录
    ion_dir = debugfs_create_dir("ion", NULL);
    if (!ion_dir) {
        printk("ion_dir is nulln");
        return -1;
    }

    /* 创建/sys/kernel/debug/ion/test_u64文件 */
    debugfs_create_u64("test_u64", 0644,
                        ion_dir, &test_u64);

    return 0;
}

static void __exit debugfs_exit(void)
{
    debugfs_remove_recursive(ion_dir);
}

module_init(debugfs_init);
module_exit(debugfs_exit);
MODULE_LICENSE("GPL");

运行结果:

图片

读写字符串:

#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] = "hellon";
static struct dentry *ion_dir;

static int ion_open(struct inode *inode, struct file *filp)
{
    //printk("ion openn");
    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:%ldn", 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:%ldn", 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 MODULEn");

    //创建一个/sys/kernel/debug/ion目录
    ion_dir = debugfs_create_dir("ion", NULL);
    if (!ion_dir) {
        printk("ion_dir is nulln");
        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 nulln");
        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");

运行结果:

图片

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 接口
    +关注

    关注

    33

    文章

    7639

    浏览量

    148495
  • 驱动
    +关注

    关注

    11

    文章

    1717

    浏览量

    84345
  • Linux
    +关注

    关注

    87

    文章

    10991

    浏览量

    206736
  • 代码
    +关注

    关注

    30

    文章

    4555

    浏览量

    66772
收藏 人收藏

    评论

    相关推荐

    Linux驱动中创建procfs接口的方法

    上篇介绍了Linux驱动中sysfs接口的创建,今天介绍procfs接口的创建。
    发表于 05-31 16:48 569次阅读
    <b class='flag-5'>Linux</b><b class='flag-5'>驱动</b>中创建procfs<b class='flag-5'>接口</b>的方法

    Linux驱动中创建debugfs接口的方法

    上篇介绍了procfs接口的创建,今天再介绍一种debugfs接口的创建。
    发表于 05-31 16:53 732次阅读
    <b class='flag-5'>Linux</b><b class='flag-5'>驱动</b>中创建<b class='flag-5'>debugfs</b><b class='flag-5'>接口</b>的方法

    如何去实现嵌入式LINUX驱动的软件代码

    如何对嵌入式LINUX驱动的硬件信息进行配置呢?如何去实现嵌入式LINUX驱动的软件代码呢?
    发表于 12-24 07:31

    Linux MTD 源代码分析

    Linux MTD 源代码分析 Linux MTD介绍:设备层和原始设备层的函数调用关系(红色部分需要我们实现):NOR型Flash芯片驱动
    发表于 02-08 16:43 9次下载

    基于Linux的步进电机驱动程序设计

    介绍了Linux驱动程序的实现机制,在分析步进电机和驱动程序接口的基础上,给出了一个在嵌入式Linux平台上编写步进电机
    发表于 06-15 14:22 5536次阅读
    基于<b class='flag-5'>Linux</b>的步进电机<b class='flag-5'>驱动</b>程序设计

    嵌入式Linux下ARM_DSP通信接口设计及驱动开发

    嵌入式Linux下ARM_DSP通信接口设计及驱动开发
    发表于 05-19 11:17 0次下载

    基于嵌入式Linux下ARM_DSP通信接口设计及驱动开发

    基于嵌入式Linux下ARM_DSP通信接口设计及驱动开发
    发表于 10-19 10:38 12次下载
    基于嵌入式<b class='flag-5'>Linux</b>下ARM_DSP通信<b class='flag-5'>接口</b>设计及<b class='flag-5'>驱动</b>开发

    基于Linux下的LCD驱动程序实现

    基于Linux下的LCD驱动程序实现
    发表于 10-30 16:45 12次下载
    基于<b class='flag-5'>Linux</b>下的LCD<b class='flag-5'>驱动</b>程序<b class='flag-5'>实现</b>

    你知道Linux内核里的DebugFS

    DebugFS,顾名思义,是一种用于内核调试的虚拟文件系统,内核开发者通过debugfs和用户空间交换数据。
    发表于 04-25 18:55 1775次阅读
    你知道<b class='flag-5'>Linux</b>内核里的<b class='flag-5'>DebugFS</b>?

    嵌入式Linux系统的驱动原理和使用ARM Linux实现SPI驱动程序的说明

    介绍嵌入式Linux系统的驱动原理;分析SPI协议的通信原理和微处理器S3C2440A中SPI接口的硬件结构;阐述SPI驱动程序的实现过程。
    发表于 11-14 16:36 11次下载
    嵌入式<b class='flag-5'>Linux</b>系统的<b class='flag-5'>驱动</b>原理和使用ARM <b class='flag-5'>Linux</b><b class='flag-5'>实现</b>SPI<b class='flag-5'>驱动</b>程序的说明

    linux spi应用层驱动以及回环测试代码

    linux spi应用层驱动以及回环测试代码
    发表于 10-22 15:47 2次下载

    linux系统的驱动实现原理

    原理就是将硬件操作的接口全都放到驱动链表上,在驱动实现device的open、read、write等操作。当然这样做也有弊端,就是驱动fi
    发表于 11-02 09:59 673次阅读

    Linux内核代码60%都是驱动

    为什么Linux内核代码60%都是驱动? 如果每支持新的设备就加入驱动,内核会不会变得越来越臃肿?
    的头像 发表于 07-11 11:48 450次阅读
    <b class='flag-5'>Linux</b>内核<b class='flag-5'>代码</b>60%都是<b class='flag-5'>驱动</b>?

    linux内核中的debugfs该怎样去使用呢?

    debugfs可用于内核向用户空间提供信息,debugfs是个小型的文件系统,与/proc和sysfs不同,debugfs没有较为严苛的规则和定义,我们可以在里面放置想要的任何信息,以便于系统开发和调试。
    的头像 发表于 08-21 09:01 1144次阅读
    <b class='flag-5'>linux</b>内核中的<b class='flag-5'>debugfs</b>该怎样去使用呢?

    Linux驱动函数接口说明

    函数接口说明 创建目录、文件函数: /* 创建目录 */ struct dentry *debugfs_create_dir( const char *name, struct dentry
    的头像 发表于 09-27 11:20 248次阅读