有时我们需要在/sys下面创建一些目录, 下面给出了一个示例。 在加载驱动模块后, 在/sys下面会创建一个名为sysfs_demo的目录,并在其中在创建几个文件和目录。

[root@tiny4412 mnt]# ls -R /sys/sysfs_demo/
/sys/sysfs_demo/:
node_one node_two sysfs_demo_2 /sys/sysfs_demo/sysfs_demo_2:
node_four node_three

这里用到的两个函数分别是: kobject_create_and_add 和 sysfs_create_group。前一个函数用于在/sys下面创建目录, 后一个函数用于创建文件。

示例驱动:

 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__

 #include <linux/init.h>
#include <linux/module.h>
#include <linux/kobject.h>
#include <linux/device.h> static struct kobject *k_obj = NULL;
static struct kobject *k_obj_2 = NULL; static char node_one_buf[] = {}; static ssize_t sysfs_demo_show_node_one(struct kobject *kobj, struct kobj_attribute *attr, char * buf)
{
pr_info("enter, node: %s\n", attr->attr.name);
return sprintf(buf, "%s: %s\n", attr->attr.name, node_one_buf);
} static ssize_t sysfs_demo_store_node_one(struct kobject *kobj, struct kobj_attribute *attr, const char * buf, size_t n)
{
pr_info("enter, node: %s\n", attr->attr.name); sprintf(node_one_buf, "%s", buf); return n;
} static struct kobj_attribute node_one_attribute =
__ATTR(node_one, S_IWUSR|S_IRUGO, sysfs_demo_show_node_one, sysfs_demo_store_node_one); static ssize_t sysfs_demo_show_node_two(struct kobject *kobj, struct kobj_attribute *attr, char * buf)
{ return sprintf(buf, "%s\n", attr->attr.name);
} static struct kobj_attribute node_two_attribute =
__ATTR(node_two, S_IWUSR|S_IRUGO, sysfs_demo_show_node_two, NULL); static struct attribute *sysfs_demo_attributes[] = {
&node_one_attribute.attr,
&node_two_attribute.attr,
NULL
}; static const struct attribute_group sysfs_demo_attr_group = {
.attrs = sysfs_demo_attributes,
}; static char node_three_buf[] = {}; static ssize_t sysfs_demo_show_node_three(struct kobject *kobj, struct kobj_attribute *attr, char * buf)
{
pr_info("enter, node: %s\n", attr->attr.name);
return sprintf(buf, "%s: %s\n", attr->attr.name, node_three_buf);
} static ssize_t sysfs_demo_store_node_three(struct kobject *kobj, struct kobj_attribute *attr, const char * buf, size_t n)
{
pr_info("enter, node: %s\n", attr->attr.name); sprintf(node_three_buf, "%s", buf); return n;
} static struct kobj_attribute node_three_attribute =
__ATTR(node_three, S_IWUSR|S_IRUGO, sysfs_demo_show_node_three, sysfs_demo_store_node_three); static ssize_t sysfs_demo_show_node_four(struct kobject *kobj, struct kobj_attribute *attr, char * buf)
{ return sprintf(buf, "%s\n", attr->attr.name);
} static struct kobj_attribute node_four_attribute =
__ATTR(node_four, S_IWUSR|S_IRUGO, sysfs_demo_show_node_four, NULL); static struct attribute *sysfs_demo2_attributes[] = {
&node_three_attribute.attr,
&node_four_attribute.attr,
NULL
}; static const struct attribute_group sysfs_demo2_attr_group = {
.attrs = sysfs_demo2_attributes,
}; static int __init sysfs_demo_init(void)
{
if ((k_obj = kobject_create_and_add("sysfs_demo", NULL)) == NULL ) {
pr_err("sysfs_demo sys node create error \n");
goto out;
} if(sysfs_create_group(k_obj, &sysfs_demo_attr_group) ) {
pr_err("sysfs_create_group failed\n");
goto out2;
} if ((k_obj_2 = kobject_create_and_add("sysfs_demo_2", k_obj)) == NULL ) {
pr_err("hwinfo sys node create error \n");
goto out3;
} if(sysfs_create_group(k_obj_2, &sysfs_demo2_attr_group) ) {
pr_err("sysfs_create_group failed\n");
goto out4;
} pr_info("enter.\n");
return ;
out4:
kobject_put(k_obj_2);
out3:
sysfs_remove_group(k_obj, &sysfs_demo_attr_group);
out2:
kobject_put(k_obj);
out:
return -;
}
module_init(sysfs_demo_init); static void __exit sysfs_demo_exit(void)
{
pr_info("enter.\n"); if (k_obj) {
sysfs_remove_group(k_obj, &sysfs_demo_attr_group);
if (k_obj_2) {
sysfs_remove_group(k_obj_2, &sysfs_demo2_attr_group);
kobject_put(k_obj_2);
}
kobject_put(k_obj);
} }
module_exit(sysfs_demo_exit); MODULE_LICENSE("GPL");

Linux驱动学习 —— 在/sys下面创建目录示例的更多相关文章

  1. linux 驱动学习笔记01--Linux 内核的编译

    由于用的学习材料是<linux设备驱动开发详解(第二版)>,所以linux驱动学习笔记大部分文字描述来自于这本书,学习笔记系列用于自己学习理解的一种查阅和复习方式. #make confi ...

  2. linux驱动学习(二) Makefile高级【转】

    转自:http://blog.csdn.net/ghostyu/article/details/6866863 版权声明:本文为博主原创文章,未经博主允许不得转载. 在我前一篇写的[ linux驱动学 ...

  3. Linux驱动学习之常用的模块操作命令

    1.常用的模块操作命令 (1)lsmod(list module,将模块列表显示),功能是打印出当前内核中已经安装的模块列表 (2)insmod(install module,安装模块),功能是向当前 ...

  4. Linux驱动学习步骤(转载)

    1. 学会写简单的makefile 2. 编一应用程序,可以用makefile跑起来 3. 学会写驱动的makefile 4. 写一简单char驱动,makefile编译通过,可以insmod, ls ...

  5. 树莓派linux驱动学习之hello world

    最近想学习一下linux驱动,看了一些书和教学视频,大概了解了一下,不过要想深入,肯定需要实践.手上有几块linux的板子,最终选择了树莓派作为我的实验平台,资料比较丰富,接口也比较简单. 程序员的入 ...

  6. Linux驱动学习1.hello world;

    最近项目需要使用Linux系统开发,借此机会学习一下Linux驱动开发 hello word代码hello.c #include <linux/module.h> #include < ...

  7. 【Linux驱动学习】SD卡规范学习

    摘要: 学习SD卡的相关规范,包括定义,硬件特性,数据传输,命令系统等.不涉及代码. 文章针对Linux驱动开发而写,以助于理解SD卡驱动,不会涉及过多硬件内容. 纲要: 1. SD卡介绍 2. SD ...

  8. linux驱动学习_1

    目前项目需要,需要做linux驱动了,记录一下 学习驱动,大家一定都会写一个hello world代码,网上也有很多范例,但是记录一下遇到的问题. 1.make之后,使用insmod加载,终端没有打印 ...

  9. Linux驱动学习之驱动开发准备工作

    一.开启驱动开发之路 1.驱动开发的准备工作 (1)正常运行linux系统的开发板.要求开发板中的linux的zImage必须是自己编译的,不能是别人编译的.原因在于在安装模块的时候会进行安全性校验 ...

随机推荐

  1. [虾扯蛋] android界面框架-Window

    从纯sdk及framwork的角度看,android中界面框架相关的类型有:Window,WindowManager,View等.下面就以这几个类为出发点来概览下安卓开发的"界面架构&quo ...

  2. Web性能优化:图片优化

    程序员都是懒孩子,想直接看自动优化的点:传送门 我自己的Blog:http://cabbit.me/web-image-optimization/ HTTP Archieve有个统计,图片内容已经占到 ...

  3. Js 变量声明提升和函数声明提升

    Js代码分为两个阶段:编译阶段和执行阶段 Js代码的编译阶段会找到所有的声明,并用合适的作用域将它们关联起来,这是词法作用域的核心内容 包括变量声明(var a)和函数声明(function a(){ ...

  4. iOS开发之Masonry框架源码深度解析

    Masonry是iOS在控件布局中经常使用的一个轻量级框架,Masonry让NSLayoutConstraint使用起来更为简洁.Masonry简化了NSLayoutConstraint的使用方式,让 ...

  5. 设计模式之单例模式(Singleton)

    设计模式之单例模式(Singleton) 设计模式是前辈的一些经验总结之后的精髓,学习设计模式可以针对不同的问题给出更加优雅的解答 单例模式可分为俩种:懒汉模式和饿汉模式.俩种模式分别有不同的优势和缺 ...

  6. 流程表单中js如何清空SheetUser控件数据?

    昨天有人问我js怎么清空.我试了试,发现简单的赋给他空值,并没有用.只能给他赋一个真实存在的值才有用.于是跟踪了一下他的删除按钮. 效果如下 使用场景:可以根据字段的不同类别变更人员. js代码如下, ...

  7. TabLayout + ViewPager

    一.实现思路 1.在build.gradle中添加依赖,例如: compile 'com.android.support:support-v4:23.4.0'compile 'com.android. ...

  8. mysql join 和left join 对于索引的问题

    今天遇到一个left join优化的问题,搞了一下午,中间查了不少资料,对MySQL的查询计划还有查询优化有了更进一步的了解,做一个简单的记录: select c.* from hotel_info_ ...

  9. C#与C++的发展历程第一 - 由C#3.0起

    俗话说学以致用,本系列的出发点就在于总结C#和C++的一些新特性,并给出实例说明这些新特性的使用场景.前几篇文章将以C#的新特性为纲领,并同时介绍C++中相似的功能的新特性,最后一篇文章将总结之前几篇 ...

  10. Fedora 24中的日志管理

    Introduction Log files are files that contain messages about the system, including the kernel, servi ...