学习博客:https://blog.csdn.net/mary0712/article/details/114410105
#include <linux/kobject.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/module.h>
#include <linux/init.h>
static int foo;
static int baz;
static int bar;
static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
printk("%s, kobj->name %s\n", __func__, kobj->name); // 打印的是 kobj->name kobject_example
return sprintf(buf, "%d\n", foo);
}
static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
printk("%s, kobj->name %s\n", __func__, kobj->name);
sscanf(buf, "%du", &foo);
return count;
}
static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) {
int var;
printk("%s, kobj->name %s\n", __func__, kobj->name);
if (strcmp(attr->attr.name, "baz") == 0)
var = baz;
else
var = bar;
return sprintf(buf, "%d\n", var);
}
static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
int var;
printk("%s, kobj->name %s\n", __func__, kobj->name);
sscanf(buf, "%du", &var);
if (strcmp(attr->attr.name, "baz") == 0)
baz = var;
else
bar = var;
return count;
}
// 这里如果将权限设置为 666 ,则会有如下报错:
// negative width in bit-field ‘<anonymous>
static struct kobj_attribute foo_attribute = __ATTR(foo, 0664, foo_show, foo_store);
static struct kobj_attribute baz_attribute = __ATTR(baz, 0664, b_show, b_store);
static struct kobj_attribute bar_attribute = __ATTR(bar, 0664, b_show, b_store);
/*
* Create a group of attributes so that we can create and destroy them all at once.
* 创建一组属性,以便我们可以同时创建和销毁它们
*/
static struct attribute *attrs[] = {
&foo_attribute.attr,
&baz_attribute.attr,
&bar_attribute.attr,
NULL, /* need to NULL terminate the list of attributes ,注意这个数组最后一个元素一定要是NULL */
};
/*
* An unnamed attribute group will put all of the attributes directly in
* the kobject directory. If we specify a name, a subdirectory will be
* created for the attributes with the directory being the name of the
* attribute group.
* 未命名的属性组将把所有属性直接放在kobject目录中。
* 如果我们指定一个名称,将为属性创建一个子目录,该目录是属性组的名称。
*/
static struct attribute_group attr_group = {
.name = NULL, /* 这里若为NULL或不写则不会创建子文件夹,写了就会创建子文件夹 */
.attrs = attrs,
};
static struct kobject *example_kobj;
static int __init example_init(void)
{
int retval;
example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);
if (!example_kobj)
return -ENOMEM;
/* Create the files associated with this kobject */
/*创建与此kobject关联的文件*/
retval = sysfs_create_group(example_kobj, &attr_group);
if (retval)
kobject_put(example_kobj);
return retval;
}
static void __exit example_exit(void)
{
kobject_put(example_kobj);
}
module_init(example_init);
module_exit(example_exit);
MODULE_LICENSE("GPL");
//===========================================================================
obj-m:=my_demo.o
KERNELDIR:=/lib/modules/`uname -r`/build
PWD :=$(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *o *.mod.c *.order *.symvers *.ur-safe
//===========================================================================
最近项目一个问题的总结,类似代码如下:
(1) container_of 的误用
struct test {
char s;
int d;
struct kobject *kobj;
};
static struct test test1;
static ssize_t foo_show(struct kobject *ckobj, struct kobj_attribute *attr, char *buf)
{
// printk("%s, ckobj's val 0x%x\n", __func__, ckobj);
// printk("%s, test1.kobj's val 0x%x\n", __func__, test1.kobj);
// printk("%s, test1.kobj's addr 0x%x\n", __func__, &(test1.kobj));
struct test *t;
//注意这里第一个参数是结构体成员的地址,所以需要传入指针的地址
//不然会报如下错误
//pointer type mismatch in container_of() _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
t = container_of(&ckobj, struct test, kobj);
printk("%s, test1's addr 0x%x\n", __func__, &test1);
printk("%s, t's val 0x%x\n", __func__, t);
//struct test *t;
//t = container_of(&ckobj, struct test, kobj);
//printk("%s, ckobj->name %s\n", __func__, ckobj->name);
//printk("%s, t->d %d\n", __func__, t->d);
}
static struct kobj_attribute foo_attribute = __ATTR(foo, 0664, foo_show, NULL);
static struct attribute *attrs[] = {
&foo_attribute.attr,
NULL,
};
static struct attribute_group attr_group = {
.name = NULL,
.attrs = attrs,
};
static struct kobject *example_kobj;
static int __init example_init(void)
{
int retval;
example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);
if (!example_kobj)
return -ENOMEM;
test1.kobj = example_kobj;
test1.d = 100;
test1.s = 's';
retval = sysfs_create_group(example_kobj, &attr_group);
if (retval)
kobject_put(example_kobj);
return retval;
}
container_of中通过指针获取结构体,必须是结构体对象的成员的地址,不是某个外部指针(该指针的值跟结构体对象的成员的值相等)的地址
(2) 如何让 kobj 携带开发人员指定的数据
sys/kernel/目录下的kobject不携带任何kernel私有数据
所以有两种方式可以做到kobject和kernel某个结构体对应:
(1)通过kobject->name来匹配kernel私有数据
(2)kernel保存kobject_create_and_add创建的指针,这样当上层调用sys/kernel/目录下某个attr时,就能遍历kernel链表,用kobj来匹配
场景:
sys/kernel/目录下创建了两个kobject,一个是A,下面有若干attr,另一个是B,下面有同样的attr,这些 attr 可以 read 和 write (对应上层就是 cat 和 echo),开发人员的初衷是A目录给X设备服务,B目录给Y设备服务,但是因为某些原因,用的文件系统不是debugfs的,也不是proc的,而是 sysfs 下的 kernel_kobj .
当上层进行cat或者echo某个attr时,kernel中可见的只有 static ssize_t xxx_show(struct kobject *ckobj, struct kobj_attribute *attr, char *buf) 这三个实参,kernel如何通过这三个实参确定是X设备的还是Y设备的呢?
这就需要上面讲的两个方法:
函数逻辑调用到 kernel 层面时,kernel 可以通过 kobj 来获取 kobj->name, 这样就能知道是哪个设备的了,因为A和B的文件夹名肯定不一样,可以将A和B的命名命名为对驱动有用的信息:i2c-x_dev-y (比如是i2c设备,x是i2c的序号,y是设备序号),通过名字来决定,接下来是用 X设备 的私有数据,还是用 Y设备 的私有数据
另一种办法,就是X设备/Y设备的驱动在创建A/B时,保存下各自的 kobj ,这样通过上层传入的kobj,也能遍历每个设备保存的 kobj,来达到确认 X设备 还是 Y设备 的效果
附录:
kernel_kobj 没有相关的 priv 成员给开发人员用,即使有,也是给 attr 用的,不信请看源码