void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
初始化 kobject 对象,主要是设置其属性。
kobject_init
kobject_init_internal
kref_init(&kobj->kref);
INIT_LIST_HEAD(&kobj->entry);
kobj->state_initialized = 1;
kobj->ktype = ktype;
int kobject_add(struct kobject *kobj, struct kobject *parent,
const char *fmt, ...)
kobject_add() 用于将 kobject 对象加入 Linux 设备层次,如果 parent 不为空,那么 kobject 对象的 parent 将指向它,如果 parent 为空,那么 kobject 对象的 parent 将被设置为内部 kset 对象所关联的 kobject 对象,如果 kset 也未被设置,那么该 kobject 对象将位于顶层 sysfs 目录下。
kobject_add
kobject_add_varg
kobject_set_name_vargs //设置kobject名字
kobj->parent = parent;
kobject_add_internal
kobject_get(kobj->parent); //如果父节点存在,引用计数加1
/* join kset if set, use it as parent if we do not already have one */
if (kobj->kset) {
if (!parent)
parent = kobject_get(&kobj->kset->kobj);
kobj_kset_join(kobj);
kobj->parent = parent;
}
create_dir //为kobject创建相应目录
kobj->state_in_sysfs = 1;
int kobject_init_and_add(struct kobject *kobj, struct kobj_type
*ktype,struct kobject *parent, const char *fmt, ...)
kobject_init_and_add() 函数将 kobject_init() 和 kobject_add() 的步骤合并起来,使用起来更加方便。
kobject_init_and_add
kobject_init
kobject_add_varg
例子:在 sys 根目录下创建一个名为 mykobject 的目录,并且具有读写功能。
/* 包含相关头文件 */
static void release(struct kobject *kobj)
{
printk("mykobj release\n");
}
static ssize_t myshow(struct kobject *kobj, struct attribute *attr,
char *buf)
{
printk("myshow:");
sprintf(buf, "%s\n", attr->name);
return strlen(attr->name);
}
static ssize_t mystore(struct kobject *kobj,struct attribute *attr,
const char *buf, size_t count)
{
printk("mystore: %s\n",buf);
return count;
}
static struct sysfs_ops sysfs_ops = {
.show = myshow,
.store = mystore,
};
struct attribute first_attr = {
.name = "mykobj_config",
.mode = S_IRWXUGO,
};
static struct attribute *def_attrs[] = {
&first_attr,
NULL,
};
static struct kobject mykobj;
static struct kobj_type myktype = {
.release = release,
.sysfs_ops = &sysfs_ops,
.default_attrs = def_attrs,
};
static int mykobj_init(void)
{
kobject_init_and_add(&mykobj, &myktype, NULL, "mykobject");
return 0;
}
static void mykobj_exit(void)
{
kobject_del(&mykobj);
}
相关资料下载:
——忠于梦想 勇于实践 linux_xpj@opencores.org
阅读(1364) | 评论(0) | 转发(0) |