linux驱动—在自己的总线目录下创建属性文件

在总线目录下创建属性文件以扩展其功能。 通过创建属性文件, 我们可以为总线添加额外的信息和控制选项, 以便与设备和驱动进行交互。 简单就是,属性文件,可以完成用户空间和内核空间的数据交互, 比如在应用层快速修改gpio引脚的电平。

介绍一下API

bus_create_file() 函数用于在总线的 sysfs 目录下创建一个属性文件。

参数说明:​

bus: 指向总线类型结构体 struct bus_type 的指针, 表示要在这个目录下创建属性文件的 总线。​

attr:指向属性 struct bus_attribute的指针, 表示要创建的属性文件的属性。

函数原型:

int bus_create_file(struct bus_type *bus,struct bus_attribute *attr);

在调用 bus_create_file()函数之前, 需要先定义好属性结构体 struct bus_attribute *attr, 并将其相关字段填充好。 通常, 属性结构体会包含以下字段:

第一个字段,attr

复制代码
static ssize_t my_show (struct bus_type *bus, char *buf)
{
    // 在 sysfs 中显示总线的值​
    return sprintf(buf, "%s\n", "mybus_show");
}

static ssize_t my_store(struct bus_type *bus, const char *buf, size_t count)
{
    // 在 sysfs 中显示总线的值​
    return sprintf(buf, "%s\n", "mybus_show");
}

struct bus_attribute mybus_attr = {
    /*属性文件的名字和操作权限*/
    .attr = {
        .name = "value",
        .mode = 0664,
    },
    .show = my_show,
    .store= my_store,

};

代码编写

复制代码
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/configfs.h>
#include <linux/kernel.h>
#include <linux/kobject.h>
#include <linux/device.h>
#include <linux/sysfs.h>


int mybus_match(struct device *dev, struct device_driver *drv)
{
    // 检查设备名称和驱动程序名称是否匹配
    return (strcmp(dev_name(dev), drv->name) == 0);
}

int mybus_probe(struct device *dev)
{
    struct device_driver *drv = dev->driver;
    if(drv->probe)
    {
        drv->probe(dev);
    }
    return 0;
}

static ssize_t my_show (struct bus_type *bus, char *buf)
{
    // 在 sysfs 中显示总线的值​
    return sprintf(buf, "%s\n", "mybus_show");
}

static ssize_t my_store(struct bus_type *bus, const char *buf, size_t count)
{
    // 在 sysfs 中显示总线的值​
    return sprintf(buf, "%s\n", "mybus_show");
}

struct bus_attribute mybus_attr = {
    /*属性文件的名字和操作权限*/
    .attr = {
        .name = "value",
        .mode = 0664,
    },
    .show = my_show,
    .store= my_store,

};

struct bus_type mybus ={
    .name  = "mabus",        // 总线的名称
    .match =  mybus_match,   // 设备和驱动程序匹配的回调函数
    .probe =  mybus_probe,   //设备探测的回调函数
}

EXPORT_SYMBOL_GPL(mybus);             // 导出总线符号

static int bus_attr_init (void)
{
    int ret;
    ret = bus_register(&mybus);      // 注册总线
    buf_create_file(&mybus, &mybus_attr);
    return 0;
}

static void bus_attr_exit (void)
{
    bus_remove_file(&mybus, &mybus_attr);  // 从 sysfs 中移除属性文件​
    bus_unregister(&mybus);                // 取消注册总线
}


/* 加载和卸载*/
module_init(bus_attr_init);
module_eixt(bus_attr_exit );
MODULE_LICENSE("GPL");
MODULE_AUTHOR("xiaoyang");

看现象

相关推荐
在肯德基吃麻辣烫31 分钟前
Netdata在Ubuntu环境下的安装与配置:构建实时系统监控与性能分析平台
linux·运维·ubuntu
愚戏师2 小时前
Linux复习笔记(六)shell编程
linux·笔记·shell
大胆飞猪2 小时前
Linux操作系统--进程间通信(system V共享内存)
linux
LunarCod3 小时前
Ubuntu使用Docker搭建SonarQube企业版(含破解方法)
linux·运维·服务器·ubuntu·docker·开源·sonarqube
betazhou3 小时前
基于Linux环境实现Oracle goldengate远程抽取MySQL同步数据到MySQL
linux·数据库·mysql·oracle·ogg
什么半岛铁盒3 小时前
Linux信号的保存
linux·运维·网络
百锦再3 小时前
大数据技术的主要方向及其应用详解
大数据·linux·网络·python·django·pygame
2301_803554523 小时前
vim,gcc/g++,makefile,cmake
linux·编辑器·vim
惜.己4 小时前
Linux常用命令(十四)
linux·运维·服务器
linkingvision4 小时前
H5S 视频监控AWS S3 对象存储
linux·运维·aws·视频监控s3对象存储