注冊自定義總線

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)
#endifstruct 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)
#endifstruct 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.pswp.cn/web/44417.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/44417.shtml
英文地址,請注明出處:http://en.pswp.cn/web/44417.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

bug修復 修復修復修復

好的&#xff0c;這里是更新后的代碼&#xff0c;將所有 inRange 函數的第一個變量替換為 ZoomOutimage&#xff1a; // 綠色分岔路 if (divergerColor "green" && nextColor "null") {cv::Mat frameGreen, frameRed;frame2.copyTo(frameGreen)…

如何在 Fedora 中使用 `shred` 擦除驅動器或文件

English Version: https://blog.csdn.net/sch0120/article/details/140390161 如何在 Fedora 中使用 shred 擦除驅動器或文件 安全擦除驅動器對于保護您的敏感數據免受未授權訪問至關重要。在這篇博文中&#xff0c;我們將學習如何在 Fedora 中使用 shred 命令安全擦除整個驅…

FATE Flow 源碼解析 - 作業提交處理流程

背景介紹 FATE 是隱私計算中最有名的開源項目了&#xff0c;從 star 的數量上來看也可以看出來。截止 2023 年 3 月共收獲 4.9k 個 star&#xff0c;但是 FATE 一直被認為代碼框架復雜&#xff0c;難以理解&#xff0c;作為一個相關的從業者&#xff0c;后續會持續對 FATE 項目…

React@16.x(56)Redux@4.x(5)- 實現 createStore

目錄 1&#xff0c;分析2&#xff0c;實現2.1&#xff0c;基礎實現2.2&#xff0c;優化2.2.1&#xff0c;隨機字符串2.2.2&#xff0c;action 的判斷2.2.2&#xff0c;監聽器的優化 2.3&#xff0c;最終形態 1&#xff0c;分析 createStore()&#xff0c;參數1為 reducer&…

0601STM32TIM

TOC 分為四部分&#xff0c;八小節 一部分&#xff1a;主要講定時器基本定時的功能&#xff0c;也就是定一個事件&#xff0c;讓定時器每隔這個時間產生一個中斷&#xff0c;來實現每隔一個固定時間來執行一段程序的目的&#xff0c;比如做一個時鐘、秒表&#xff0c;或者使用一…

【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. 優化性能 七、…

切割01串(牛客小白月賽98)

題意&#xff1a; 給三個整數n&#xff0c;l&#xff0c;r&#xff0c;和一個字符串s&#xff0c;滿足l<|c0-c1|<r就可以切成字符串a和字符串b&#xff0c;c0為字符串a左側出現0的次數&#xff0c;c1為字符串b右側出現1的次數&#xff0c;求最多切割次數 知識點&#x…

Onnx 1-深度學習-概述1

Onnx 1-深度學習-概述1 一: Onnx 概念1> Onnx 介紹2> Onnx 的作用3> Onnx 應用場景4> Onnx 文件格式1. Protobuf 特點2. onnx.proto3協議3> Onnx 模型基本操作二:Onnx API1> 算子詳解2> Onnx 算子介紹三: Onnx 模型1> Onnx 函數功能

昇思學習打卡-8-計算機視覺/FCN圖像語義分割

目錄 FCN介紹FCN所用的技術訓練數據的可視化模型訓練模型推理FCN的優點和不足優點不足 FCN介紹 FCN主要用于圖像分割領域&#xff0c;是一種端到端的分割方法&#xff0c;是深度學習應用在圖像語義分割的開山之作。通過進行像素級的預測直接得出與原圖大小相等的label map。因…

【C++基礎】初識C++(2)--引用、const、inline、nullptr

目錄 一、引用 1.1 引用的概念和定義 1.2 引用的特性 1.3引用的使用 1.4 const引用 1.5 指針和引用的關系 二、inline 三、nullptr 一、引用 1.1 引用的概念和定義 引?不是新定義?個變量&#xff0c;?是給已存在變量取了?個別名&#xff0c;編譯器不會為引?…

微軟的人工智能語音生成器在測試中達到與人類同等水平

微軟公司開發了一種新的神經編解碼語言模型 Vall-E&#xff0c;在自然度、語音魯棒性和說話者相似性方面都超越了以前的成果。它是同類產品中第一個在兩個流行基準測試中達到人類同等水平的產品&#xff0c;而且顯然非常逼真&#xff0c;以至于微軟不打算向公眾開放。 VALL-E …

Node.js 模塊系統

Node.js 模塊系統 Node.js 的模塊系統是其核心特性之一,它允許開發者將代碼組織成可重用的模塊。這種系統促進了代碼的模塊化,使得大型應用程序的構建和管理變得更加容易。本文將深入探討 Node.js 的模塊系統,包括其工作原理、如何創建和使用模塊,以及模塊系統的優勢和局限…

【每日一練】python類和對象現實舉例詳細講解

""" 本節課程目的&#xff1a; 1.掌握類描述現實世界實物思想 2.掌握類和對象的關系 3.理解什么事面向對象 """ #比如設計一個鬧鐘&#xff0c;在這里就新建一個類 class Clock:idNone #鬧鐘的序列號&#xff0c;也就是類的屬性priceNone #鬧…

Git最常用操作速查表

Git常用操作 文章目錄 Git常用操作1. 克隆/拉取2. 分支操作1. 查看分支2. 創建分支3. 切換到分支4. 刪除分支5. 刪除遠程分支6. 推送分支到遠程 3. 暫存庫操作4. Git團隊規范1. 原則2. 分支設計3. commit備注一般規范 1. 克隆/拉取 git clone xxx 從遠程倉庫克隆 git rebase…

【開源之美】:WinMerge Files

一、引言 強大的windows端文件比較工具&#xff0c;跟Beyond Compare相比&#xff0c;更為強大。但是這里我們推薦他的原因&#xff0c;不僅是因為作為一個使用的工具&#xff0c;主要是因為他開源&#xff0c;可以通過調試優秀的源代碼&#xff0c;進一步的提升C項目設計和編…

Alternative to Receptive field in Transformers and what factors impact it

題意&#xff1a;Transformer中感受野的替代概念及其影響因素 問題背景&#xff1a; I have two transformer networks. One with 3 heads per attention and 15 layers in total and second one with 5 heads per layer and 30 layers in total. Given an arbitrary set of d…

什么是數據模型?數據模型與數據治理有什么關系?

在企業數據治理的廣闊領域中&#xff0c;首要且關鍵的一步是明確溝通數據治理的需求。這包括對企業所持有的數據種類、數據存儲位置、以及當前數據管理的具體情況有一個清晰的了解和記錄。了解企業的數據資產是制定有效數據治理策略的基礎。企業需要識別和盤點所有類型的數據資…

AIGC產品經理學習路徑

基礎篇&#xff08;課時 2 &#xff09; AIGC 行業視角 AIGC 的行業發展演進&#xff1a;傳統模型/深度學習/大模型 AIGC 的產品設計演進&#xff1a;AI Embedded / AI Copilot / AI Agen AIGC 的行業產業全景圖 AIGC 的產品應用全景圖 AIGC 職業視角 AI 產品經理/ AIGC…

2974.最小數字游戲

1.題目描述 你有一個下標從 0 開始、長度為 偶數 的整數數組 nums &#xff0c;同時還有一個空數組 arr 。Alice 和 Bob 決定玩一個游戲&#xff0c;游戲中每一輪 Alice 和 Bob 都會各自執行一次操作。游戲規則如下&#xff1a; 每一輪&#xff0c;Alice 先從 nums 中移除一個 …