<span style="font-size:14px;">#include <linux/init.h>
#include <linux/module.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#include <linux/string.h>
static int sysfs_value;
static ssize_t sysfs_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
return sprintf(buf, "%d\n", sysfs_value);
}
static ssize_t sysfs_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
sscanf(buf, "%du", &sysfs_value);
return count;
}
static struct kobj_attribute sysfs_value_attribute = __ATTR(sysfs_value, S_IRUGO|S_IWUSR, sysfs_show, sysfs_store);
static struct kobject *sysfs_kobj;
static int __init sysfs_test_init(void)
{
int retval;
sysfs_kobj = kobject_create_and_add("sysfs", kernel_kobj);
if (!sysfs_kobj)
return -ENOMEM;
retval = sysfs_create_file(sysfs_kobj, &sysfs_value_attribute.attr);
if (retval)
kobject_put(sysfs_kobj);
return retval;
}
static void __exit sysfs_test_exit(void)
{
kobject_put(sysfs_kobj);
}
module_init(sysfs_test_init);
module_exit(sysfs_test_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("luzheng");
</span>
<span style="font-size:14px;">makefile</span>
<span style="font-size:14px;">obj-m:=sysfs.o
KERNELDIR:=/lib/modules/3.13.6/build
PWD:=$(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install</span>
<span style="font-size:14px;">clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
</span>
问题:如果只有show函数,kobj_attribute sysfs_value_attribute该如何设置?