有时我们需要在/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. socket读写返回值的处理

    在调用socket读写函数read(),write()时,都会有返回值.如果没有正确处理返回值,就可能引入一些问题 总结了以下几点 1当read()或者write()函数返回值大于0时,表示实际从缓冲 ...

  2. 关于这段时间学习 EntityFramework的 一点感悟

    Ado.Net,用了N多年,Entity Framework也关注了很多年. 每当项目转型的时候,就花费大巴的时间,学习一番,潮流的东西. 这个Orm很多,这个EF很火,这么多年了,我还是不敢用,虽然 ...

  3. x:bind不支持样式文件 或 此Xaml文件必须又代码隐藏类才能使用{x:Bind} 解决办法

    这两天学习UWP开发,发现一个很有趣的问题,就是我题目中的描述的. 我习惯了在ResourceDictionary中写样式文件,但是发现用x:Bind时会有问题 如果是写在Style里,则提示 “x: ...

  4. winform异步加载数据到界面

    做一个学习记录. 有两个需求: 1.点击按钮,异步加载数据,不卡顿UI. 2.把获取的数据加载到gridview上面. 对于需求1,2,代码如下: public delegate void ShowD ...

  5. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...

  6. SQL字符串函数

    LEN() :计算字符串长度(字符的个数.)datalength();//计算字符串所占用的字节数,不属于字符串函数.测试varchar变量与nvarchar变量存储字符串a的区别.见备注1.LOWE ...

  7. 算是休息了这么长时间吧!准备学习下python文本处理了,哪位大大有好书推荐的说下!

    算是休息了这么长时间吧!准备学习下python文本处理了,哪位大大有好书推荐的说下!

  8. TFS 生成发布代理

    下载Agent 后,执行配置命令     参考 安装TFS(2015)工作组模式代理服务器(Agent)

  9. SpringMvc中的数据校验

    SpringMvc中的数据校验 Hibernate校验框架中提供了很多注解的校验,如下: 注解 运行时检查 @AssertFalse 被注解的元素必须为false @AssertTrue 被注解的元素 ...

  10. Linux.NET实战手记—自己动手改泥鳅(下)

    在上回合中,我们不痛不痒的把小泥鳅的数据库从只能供在Windows下运行的Access数据库改为支持跨平台的MYSQL数据库,毫无营养的修改,本回合中,我们将把我们修改后得来的项目往Linux中部署. ...