How to create a folder in a folder in sysfs

I am trying to create sysfs to implement mine in android and get stuck when creating my own folder in CLASS.

My requirement:

/sys/class/example_class/my_sysfs_directory/file_one.

Code:

    #include<linux/module.h>
    #include<linux/kernel.h>
    #include<linux/device.h>
    #include <linux/err.h>

    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("Manoj");

    static ssize_t sysfs_demo_show(struct class *class,
        struct class_attribute *attr, char *buf)
    {
    pr_info("%s [%d]: \n", __func__, __LINE__);
    return sprintf(buf, "%s \n", __func__);
    }

    static ssize_t sysfs_demo_store(struct class *class,
    struct class_attribute *attr, const char *buf, size_t size)
    {
    pr_info("%s [%d]: \n", __func__, __LINE__);
    return size;
    }

    static CLASS_ATTR(file_one, 0777, sysfs_demo_show, sysfs_demo_store);
    int  sysfs_my_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
    {
    pr_info("%s [%d]: \n", __func__, __LINE__);
    return 0;
    }

    struct class *example_class;

    int sysfs_demo_init(void)
    {
    int ret;
    pr_info("%s [%d]: \n", __func__, __LINE__);
    example_class = class_create(THIS_MODULE, "demo");
    if (IS_ERR(example_class)) {
    pr_err("Failed to create sys_class\n");
    return -1;
    }
    example_class->dev_uevent = sysfs_my_dev_uevent;
    ret = class_create_file(example_class, &class_attr_file_one);
    if (ret) {
    pr_err("Failed to create class file @ parent class dirs\n");
    return -1;
    }
    return 0;
    }

    void sysfs_demo_exit(void)
    {
    pr_info("%s [%d]: \n", __func__, __LINE__);
    class_remove_file(example_class, &class_attr_file_one);
    class_destroy(example_class);
    }


    module_init(sysfs_demo_init);
    module_exit(sysfs_demo_exit);

I have inserted my code here. Please help me in this regard.

+1
source share
1 answer

It is not recommended to create the sysfs directory / file manually.

Each sysfs attribute must be associated with a device. If your goal is the driver for the device, sooner or later you will use the structure device. Here is the related answer

+2
source

Source: https://habr.com/ru/post/1618901/


All Articles