Sometimes we need to create some directories under/sys, and here's an example. After the driver module is loaded, a directory named Sysfs_demo is created below/sys and in which several files and directories are created.
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
The two functions used here are: Kobject_create_and_add and Sysfs_create_group. The previous function is used to create a directory under/sys, and the latter function is used to create the file.
Example driver:
1 #definePR_FMT (FMT) kbuild_modname ":%s:" FMT, __func__2 3#include <linux/init.h>4#include <linux/module.h>5#include <linux/kobject.h>6#include <linux/device.h>7 8 Static structKobject *k_obj =NULL;9 Static structKobject *k_obj_2 =NULL;Ten One Static Charnode_one_buf[ -] = {0}; A - Staticssize_t Sysfs_demo_show_node_one (structKobject *kobj,structKobj_attribute *attr,Char*buf) - { thePr_info ("Enter, node:%s\n", attr->attr.name); - returnsprintf (BUF,"%s:%s\n", attr->Attr.name, node_one_buf); - } - + Staticssize_t Sysfs_demo_store_node_one (structKobject *kobj,structKobj_attribute *attr,Const Char*buf, size_t N) - { +Pr_info ("Enter, node:%s\n", attr->attr.name); A atsprintf (Node_one_buf,"%s", buf); - - returnN; - } - - Static structKobj_attribute Node_one_attribute = in__attr (Node_one, s_iwusr|S_irugo, Sysfs_demo_show_node_one, sysfs_demo_store_node_one); - to Staticssize_t Sysfs_demo_show_node_two (structKobject *kobj,structKobj_attribute *attr,Char*buf) + { - the returnsprintf (BUF,"%s\n", attr->attr.name); * } $ Panax Notoginseng Static structKobj_attribute Node_two_attribute = -__attr (Node_two, s_iwusr|S_irugo, Sysfs_demo_show_node_two, NULL); the + Static structAttribute *sysfs_demo_attributes[] = { A&Node_one_attribute.attr, the&Node_two_attribute.attr, + NULL - }; $ $ Static Const structAttribute_group Sysfs_demo_attr_group = { -. Attrs =Sysfs_demo_attributes, - }; the - Static Charnode_three_buf[ -] = {0};Wuyi the Staticssize_t Sysfs_demo_show_node_three (structKobject *kobj,structKobj_attribute *attr,Char*buf) - { WuPr_info ("Enter, node:%s\n", attr->attr.name); - returnsprintf (BUF,"%s:%s\n", attr->Attr.name, node_three_buf); About } $ - Staticssize_t Sysfs_demo_store_node_three (structKobject *kobj,structKobj_attribute *attr,Const Char*buf, size_t N) - { -Pr_info ("Enter, node:%s\n", attr->attr.name); A +sprintf (Node_three_buf,"%s", buf); the - returnN; $ } the the Static structKobj_attribute Node_three_attribute = the__attr (Node_three, s_iwusr|S_irugo, Sysfs_demo_show_node_three, sysfs_demo_store_node_three); the - Staticssize_t Sysfs_demo_show_node_four (structKobject *kobj,structKobj_attribute *attr,Char*buf) in { the the returnsprintf (BUF,"%s\n", attr->attr.name); About } the the Static structKobj_attribute Node_four_attribute = the__attr (Node_four, s_iwusr|S_irugo, Sysfs_demo_show_node_four, NULL); + - Static structAttribute *sysfs_demo2_attributes[] = { the&Node_three_attribute.attr,Bayi&Node_four_attribute.attr, the NULL the }; - - Static Const structAttribute_group Sysfs_demo2_attr_group = { the. Attrs =Sysfs_demo2_attributes, the }; the the Static int__init Sysfs_demo_init (void) - { the if(K_obj = Kobject_create_and_add ("Sysfs_demo", NULL)) = =NULL) { thePr_err ("Sysfs_demo sys node create error \ n"); the Goto out;94 } the the if(Sysfs_create_group (K_obj, &Sysfs_demo_attr_group)) { thePr_err ("Sysfs_create_group failed\n");98 GotoOut2; About } - 101 if(K_obj_2 = Kobject_create_and_add ("sysfs_demo_2", k_obj)) = =NULL) {102Pr_err ("hwinfo sys node create error \ n");103 Gotoout3;104 } the 106 if(Sysfs_create_group (K_obj_2, &Sysfs_demo2_attr_group)) {107Pr_err ("Sysfs_create_group failed\n");108 GotoOut4;109 } the 111Pr_info ("enter.\n"); the return 0;113 Out4: the Kobject_put (k_obj_2); the OUT3: theSysfs_remove_group (K_obj, &sysfs_demo_attr_group);117 Out2:118 Kobject_put (k_obj);119 out: - return-1;121 }122 Module_init (sysfs_demo_init);123 124 Static void__exit Sysfs_demo_exit (void) the {126Pr_info ("enter.\n");127 - if(k_obj) {129Sysfs_remove_group (K_obj, &sysfs_demo_attr_group); the if(k_obj_2) {131Sysfs_remove_group (K_obj_2, &sysfs_demo2_attr_group); the Kobject_put (k_obj_2);133 }134 Kobject_put (k_obj);135 }136 137 }138 Module_exit (sysfs_demo_exit);139 $Module_license ("GPL");
Linux-driven Learning-Create a directory sample below/sys