注册自定义总线

news2024/9/21 16:23:53

1、在/sys/bus下注册一个自定义总线

#include<linux/module.h>
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/kobject.h>
#include<linux/slab.h>
#include<linux/sysfs.h>
#include<linux/device.h>
#include "my_bus.h"

#if 0
struct bus_type {
	const char		*name;
	const char		*dev_name;
	struct device		*dev_root;
	struct device_attribute	*dev_attrs;	/* use dev_groups instead */
	const struct attribute_group **bus_groups;
	const struct attribute_group **dev_groups;
	const struct attribute_group **drv_groups;

	int (*match)(struct device *dev, struct device_driver *drv);
	int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
	int (*probe)(struct device *dev);
	int (*remove)(struct device *dev);
	void (*shutdown)(struct device *dev);

	int (*online)(struct device *dev);
	int (*offline)(struct device *dev);

	int (*suspend)(struct device *dev, pm_message_t state);
	int (*resume)(struct device *dev);

	const struct dev_pm_ops *pm;

	const struct iommu_ops *iommu_ops;

	struct subsys_private *p;
	struct lock_class_key lock_key;
};
int bus_register(struct bus_type *bus)
#endif

struct bus_type my_bus = {
	.name = "my_bus",
	.match = my_bus_match,
	.probe = my_bus_probe,
};

int my_bus_match(struct device *dev, struct device_driver *drv)
{
	
	return (strcmp(dev_name(dev),drv->name) == 0);
}

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

static int my_bus_init(void)
{
     int ret;
	ret = bus_register(&my_bus);
	
     return ret;
}

static void my_bus_exit(void)
{
     bus_unregister(&my_bus);
}

module_init(my_bus_init);
module_exit(my_bus_exit);
MODULE_LICENSE("GPL");
//my_bus.h
#ifndef _ATTR_H_
#define _ATTR_H_

int my_bus_match(struct device *dev, struct device_driver *drv);
int my_bus_probe(struct device *dev);

#endif

在这里插入图片描述

2、在总线目录下创建自己的属性文件

#include<linux/module.h>
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/kobject.h>
#include<linux/slab.h>
#include<linux/sysfs.h>
#include<linux/device.h>
#include "my_bus.h"

#if 0
struct bus_type {
	const char		*name;
	const char		*dev_name;
	struct device		*dev_root;
	struct device_attribute	*dev_attrs;	/* use dev_groups instead */
	const struct attribute_group **bus_groups;
	const struct attribute_group **dev_groups;
	const struct attribute_group **drv_groups;

	int (*match)(struct device *dev, struct device_driver *drv);
	int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
	int (*probe)(struct device *dev);
	int (*remove)(struct device *dev);
	void (*shutdown)(struct device *dev);

	int (*online)(struct device *dev);
	int (*offline)(struct device *dev);

	int (*suspend)(struct device *dev, pm_message_t state);
	int (*resume)(struct device *dev);

	const struct dev_pm_ops *pm;

	const struct iommu_ops *iommu_ops;

	struct subsys_private *p;
	struct lock_class_key lock_key;
};
int bus_register(struct bus_type *bus)
static struct bus_attribute bus_attr_uevent = __ATTR(uevent, S_IWUSR, NULL,
						     bus_uevent_store);

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

struct bus_attribute {
	struct attribute	attr;
	ssize_t (*show)(struct bus_type *bus, char *buf);
	ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
};
void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
#endif

struct bus_type my_bus = {
	.name = "my_bus",
	.match = my_bus_match,
	.probe = my_bus_probe,
};
static struct bus_attribute my_bus_attr = __ATTR(attr1, 0660, my_bus_attr_show,my_bus_attr_store);

ssize_t my_bus_attr_show(struct bus_type *bus, char *buf)
{
	ssize_t ret;
	ret = sprintf(buf,"this is in my_bus_attr_show\n");

	return ret;
}
ssize_t my_bus_attr_store(struct bus_type *bus, const char *buf, size_t count)
{
	printk("my_bus_attr_store:buf = %s\n",buf);
	return count;
}

int my_bus_match(struct device *dev, struct device_driver *drv)
{
	
	return (strcmp(dev_name(dev),drv->name) == 0);
}

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

static int my_bus_init(void)
{
     int ret;
	ret = bus_register(&my_bus);
	ret = bus_create_file(&my_bus, &my_bus_attr);
	
     return ret;
}

static void my_bus_exit(void)
{
     bus_remove_file(&my_bus, &my_bus_attr);
	bus_unregister(&my_bus);
}

module_init(my_bus_init);
module_exit(my_bus_exit);
MODULE_LICENSE("GPL");
#ifndef _ATTR_H_
#define _ATTR_H_

int my_bus_match(struct device *dev, struct device_driver *drv);
int my_bus_probe(struct device *dev);

ssize_t my_bus_attr_show(struct bus_type *bus, char *buf);
ssize_t my_bus_attr_store(struct bus_type *bus, const char *buf, size_t count);
#endif

在这里插入图片描述

3、一些结构体和api介绍

3.1 struct bus_type

The bus type of the device
在这里插入图片描述

3.2 bus_register

注册一个总线
在这里插入图片描述

3.3 bus_unregister

去除一个总线
在这里插入图片描述

3.4 bus_create_file

总线目录下创建属性文件api
在这里插入图片描述

3.5 struct bus_attribute

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1921926.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Linux】1w详解如何实现一个简单的shell

目录 实现思路 1. 交互 获取命令行 2. 子串分割 解析命令行 3. 指令的判断 内建命令 4. 普通命令的执行 补充&#xff1a;vim 文本替换 整体代码 重点思考 1.getenv和putenv是什么意思 2.代码extern char **environ; 3.内建命令是什么 4.lastcode WEXITSTATUS(sta…

Java-final关键字详解

Java-final关键字详解 一、引言 二、什么是 final 关键字&#xff1f; 三、final 变量 final 局部变量 final 实例变量 final 静态变量 四、final 方法 五、final 类 六、final 关键字的实际应用 1. 定义常量 2. 防止方法被重写 3. 创建不可变类 4. 优化性能 七、…

GitHub网页打开慢的解决办法

有时候看资料絮叨github网页打不开&#xff0c;经百度后&#xff0c;发下下面的方法有效。 1&#xff09;获取github官网ip 我们首先要获取github官网的ip地址&#xff0c;方法就是打开cmd&#xff0c;然后ping 找到github的地址&#xff1a;20.205.243.166 2&#xff09;配…

数据结构(Java):队列Queue集合力扣面试OJ题

1、队列 1.1 队列的概念 队列是一个特殊的线性表&#xff0c;只允许在一端&#xff08;队尾&#xff09;进行插入数据操作&#xff0c;在另一端&#xff08;对头&#xff09;进行删除数据。队列具有先进先出FIFO(First In First Out)的特性。 入队&#xff1a;数据只能从队尾…

【办公软件】PPT使用轮子动画做圈动作

在实际的PPT制作中&#xff0c;我们可能会用到画圈的动作来强调重点。如下所示为最基础的画圈动作。 那么如何来做一个这样的动作呢&#xff1f; 首先在PPT中选择插入&#xff0c;选择形状椭圆 然后按Shift画图&#xff0c;即可画出一个正圆 然后使用绘图工具&#xff0c;将开关…

程序的控制结构——if-else语句(双分支结构)【互三互三】

目录 &#x1f341; 引言 &#x1f341;if-else语句&#xff08;双分支结构&#xff09; &#x1f449;格式1&#xff1a; &#x1f449;功能&#xff1a; &#x1f449;程序设计风格提示&#xff1a; &#x1f449;例题 &#x1f449;格式2&#xff1a; &#x1f449;…

公司用哪些软件监控员工电脑?五大精选的电脑监控软件盘点

在现代企业管理中&#xff0c;员工电脑监控软件系统成为了不可或缺的工具。这些软件不仅能提高员工工作效率&#xff0c;还能有效保护企业的信息安全。本文将介绍几款主流的员工电脑监控软件系统&#xff0c;帮助企业选择最适合自己的解决方案。固信员工电脑监控软件https://ww…

python1(命名,输入输出,数据类型,与C语言在运算符上的区别)

包名&#xff1a;name_hpy 文件名&#xff1a;name.hpy 对变量命名时不能使用关键字/不能用数字开头/严格区分大小写/不建议使用中文 要在文件中调用关键字时需要输入&#xff1a;import keyword 查看保留字&#xff1a;print&#xff08;keyword.kwlist&#xff09; 查看…

JeecgBoot 前端 vue3 项目,配置项目多页面入口

前端 vue3配置项目多页面入口 1.项目根目录新建home.html <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8" /><meta name"viewport" content"widthdevice-width, initial-scale1.0" /><…

树莓派pico入坑笔记,ssd1306使用

目录 关于树莓派pico和circuitpython的更多玩法&#xff0c;请看树莓派pico专栏 说明 后附进阶玩法&#xff1a;显示中文&#xff0c;外加简单库实现 官方模块使用 使用样例 方法说明 下面是绘图支持的方法 进阶玩法&#xff0c;显示中文 方法&#xff0c;对汉字取字模…

STM32的TIM1之PWM互补输出_死区时间和刹车配置

STM32的TIM1之PWM互补输出_死区时间和刹车配置 1、定时器1的PWM输出通道 STM32高级定时器TIM1在用作PWM互补输出时&#xff0c;共有4个输出通道&#xff0c;其中有3个是互补输出通道&#xff0c;如下&#xff1a; 通道1&#xff1a;TIM1_CH1对应PA8引脚,TIM1_CH1N对应PB13引…

HDFS Decommission节点的长尾分析和问题解决

文章目录 前言Decommission过慢的分析过程NameNode页面并不显示Decommission的进度和剩余块数量增加每次调度的块数量增加Stream Limit以避免节点被Skip节点被Skip时应该在DEBUG时打印原因在大量节点被Skip的时候加快有效调度其他可能改进 基本流程解析用户通过节点刷新触发Dec…

怎样在 C 语言中进行结构体的内存布局控制?

&#x1f345;关注博主&#x1f397;️ 带你畅游技术世界&#xff0c;不错过每一次成长机会&#xff01; &#x1f4d9;C 语言百万年薪修炼课程 【https://dwz.mosong.cc/cyyjc】通俗易懂&#xff0c;深入浅出&#xff0c;匠心打磨&#xff0c;死磕细节&#xff0c;6年迭代&…

【JavaEE】网络原理——网络层+数据链路层

&#x1f921;&#x1f921;&#x1f921;个人主页&#x1f921;&#x1f921;&#x1f921; &#x1f921;&#x1f921;&#x1f921;JavaEE专栏&#x1f921;&#x1f921;&#x1f921; &#x1f921;&#x1f921;&#x1f921;上一篇文章&#xff1a;【JavaEE】网络原理—…

RuoYi3.0之sql代码审计

1 SQL注入漏洞代码审计 单点漏洞代码审计首当其冲当然要先看SQL注入漏洞是否存在,全局搜索关键字$,并限定文件类型为.xml,发现sysDeptMapper.xml和sysUserMapper.xml有存在SQL注入的地方,如下图所示: 1.1 SQL注入漏洞代码审计1 单点漏洞代码审计首当其冲当然要先看SQL注…

Elasticsearch 实现 Word、PDF,TXT 文件的全文内容提取与检索

文章目录 一、安装软件:1.通过docker安装好Es、kibana安装kibana:2.安装原文检索与分词插件:之后我们可以通过doc命令查看下载的镜像以及运行的状态:二、创建管道pipeline名称为attachment二、创建索引映射:用于存放上传文件的信息三、SpringBoot整合对于原文检索1、导入依赖…

基于PicoScope示波器理解CAN/CAN-FD的报文帧格式

&#x1f345; 我是蚂蚁小兵&#xff0c;专注于车载诊断领域&#xff0c;尤其擅长于对CANoe工具的使用&#x1f345; 寻找组织 &#xff0c;答疑解惑&#xff0c;摸鱼聊天&#xff0c;博客源码&#xff0c;点击加入&#x1f449;【相亲相爱一家人】&#x1f345; 玩转CANoe&…

Qt:21.事件(事件的介绍、事件的基类、用户输入事件、窗口和界面事件、其他系统事件、事件处理的思路)

目录 1.事件的介绍&#xff1a; 2.事件的基类&#xff1a; 3.派生类——用户输入事件&#xff1a; 4.派生类——窗口和界面事件&#xff1a; 5.派生类——其他系统事件&#xff1a; 6.事件处理的思路&#xff1a; 1.事件的介绍&#xff1a; Qt 的事件&#xff08;Event&…

druid(德鲁伊)数据线程池连接MySQL数据库

文章目录 1、druid连接MySQL2、编写JDBCUtils 工具类 1、druid连接MySQL 初学JDBC时&#xff0c;连接数据库是先建立连接&#xff0c;用完直接关闭。这就需要不断的创建和销毁连接&#xff0c;会消耗系统的资源。 借鉴线程池的思想&#xff0c;数据连接池就这么被设计出来了。…

Java多线程性能调优

Synchronized同步锁优化方法 1.6之前比较重量级&#xff0c;1.6后经过优化性能大大提升 使用Synchronized实现同步锁住要是两种方式&#xff1a;方法、代码块。 1.代码块 Synchronized在修饰同步代码块时&#xff0c;是由 monitorenter和monitorexit指令来实现同步的。进入mo…