文章目录
一、kobject:驱动的基石
构建一个 kobject对象 。
构建一个sysfs中的目录项( kernfs_node )。
把他们关联起来。
struct kernfs_node {
atomic_t count;
atomic_t active;
#ifdef CONFIG_DEBUG_LOCK_ALLOC
struct lockdep_map dep_map;
#endif
/*
* Use kernfs_get_parent() and kernfs_name/path() instead of
* accessing the following two fields directly. If the node is
* never moved to a different parent, it is safe to access the
* parent directly.
*/
struct kernfs_node *parent;
const char *name;
struct rb_node rb;
const void *ns; /* namespace tag */
unsigned int hash; /* ns + name hash */
union {
struct kernfs_elem_dir dir;
struct kernfs_elem_symlink symlink;
struct kernfs_elem_attr attr;
};
void *priv;
unsigned short flags;
umode_t mode;
unsigned int ino;
struct kernfs_iattrs *iattr;
};
二、重点
关注 sysfs目录项 与 kobject对象 的关联过程。
关注 kobject对象 默认的属性文件操作接口 (操作接口 -> 目录 -> kobject)。
使用 kobject_create_and_add 函数:创建一个 kobject 对象,指定其名字和父节点;并在 sysfs 中创建一个对应的目录项。
三、kobject_create_and_add()函数
定义在:lib/kobject.c
/* 可以分为两步 */
struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
{
struct kobject *kobj;
int retval;
/* 1、创建并初始化一个kobject对象。
* 定义见下。
*/
kobj = kobject_create();
if (!kobj)
return NULL;
/* 2、创建1个sysfs目录项 kernfs_node,并与 kobject对象 关联。
* 详见下。
*/
retval = kobject_add(kobj, parent, "%s", name);
if (retval) {
pr_warn("%s: kobject_add error: %d\n", __func__, retval);
kobject_put(kobj);
kobj = NULL;
}
return kobj;
}
1、kobject_create()函数
lib/kobject.c
struct kobject *kobject_create(void

最低0.47元/天 解锁文章
7922

被折叠的 条评论
为什么被折叠?



