62 lines
1.4 KiB
C
62 lines
1.4 KiB
C
/*
|
|
* hello-sysfs.c sysfs 示例
|
|
*/
|
|
#include <linux/fs.h>
|
|
#include <linux/init.h>
|
|
#include <linux/kobject.h>
|
|
#include <linux/module.h>
|
|
#include <linux/string.h>
|
|
#include <linux/sysfs.h>
|
|
|
|
static struct kobject *mymodule;
|
|
|
|
/* 你希望能够更改的变量 */
|
|
static int myvariable = 0;
|
|
|
|
static ssize_t myvariable_show(struct kobject *kobj,
|
|
struct kobj_attribute *attr, char *buf)
|
|
{
|
|
return sprintf(buf, "%d\n", myvariable);
|
|
}
|
|
|
|
static ssize_t myvariable_store(struct kobject *kobj,
|
|
struct kobj_attribute *attr, char *buf,
|
|
size_t count)
|
|
{
|
|
sscanf(buf, "%du", &myvariable);
|
|
return count;
|
|
}
|
|
|
|
static struct kobj_attribute myvariable_attribute =
|
|
__ATTR(myvariable, 0660, myvariable_show, (void *)myvariable_store);
|
|
|
|
static int __init mymodule_init(void)
|
|
{
|
|
int error = 0;
|
|
|
|
pr_info("我的模块: 初始化完成\n");
|
|
|
|
mymodule = kobject_create_and_add("我的模块", kernel_kobj);
|
|
if (!mymodule)
|
|
return -ENOMEM;
|
|
|
|
error = sysfs_create_file(mymodule, &myvariable_attribute.attr);
|
|
if (error) {
|
|
pr_info("在 /sys/kernel/我的模块 创建 myvariable 文件失败\n");
|
|
}
|
|
|
|
return error;
|
|
}
|
|
|
|
static void __exit mymodule_exit(void)
|
|
{
|
|
pr_info("我的模块: 退出成功\n");
|
|
kobject_put(mymodule);
|
|
}
|
|
|
|
module_init(mymodule_init);
|
|
module_exit(mymodule_exit);
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|