【Android驱动】sys下创建文件

创建sys节点的例子:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#include <linux/string.h>
#include <linux/device.h>
#include <linux/kdev_t.h>

static int hello_value;
static struct kobject *helloworld_kobj;
static struct class *hello_class;

//hello_china, virtual device
static ssize_t hello_show(struct device *dev, struct device_attribute *attr, char *buf)
{
    return sprintf(buf, "%d\n", hello_value);
}

static ssize_t hello_store(struct device *dev, struct device_attribte *attr, const char *buf, size_t count)
{
    sscanf(buf,"%du", &hello_value);
    return count;
}
static DEVICE_ATTR(helloChina, 0666, hello_show, hello_store);

//hello_value
static ssize_t hello_read(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
    return sprintf(buf, "%d\n", hello_value);
}

static ssize_t hello_write(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
    sscanf(buf, "%du", &hello_value);
    return count;
}
static const struct kobj_attribute hello_value_attribute = __ATTR(hello_value, 0777, hello_read, hello_write);


static int __init helloworld_init(void)
{
    int retval;
    struct device *temp;
    dev_t hello_dev_t = MKDEV(350, 0);

    hello_class = class_create(THIS_MODULE, "hello_class");

    temp = device_create(hello_class, NULL, hello_dev_t, "%s", "hello_class");

    retval = device_create_file(temp, &dev_attr_helloChina);

    helloworld_kobj = kobject_create_and_add("helloword", kernel_kobj);
    if(!helloworld_kobj)
        return -ENOMEM;
     retval = sysfs_create_file(helloworld_kobj, &(hello_value_attribute.attr));
     if(retval)
        kobject_put(helloworld_kobj);

      return retval;
}
module_init(helloworld_init)

static void __exit helloworld_exit(void)
{
    kobject_put(helloworld_kobj);
}
module_exit(helloworld_exit);

MODULE_DESCRIPTION("create hello_world and hello_value file node in /sys");
MODULE_AUTHOR("qyichao_ee@163.com");
MODULE_LICENSE("GPL");

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值