歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> kobject.c 添加注釋,kobject.c注釋

kobject.c 添加注釋,kobject.c注釋

日期:2017/3/6 9:29:55   编辑:學習Linux

kobject.c 添加注釋,kobject.c注釋


kobject.c 添加注釋,kobject.c注釋


最近結合《Linux Device Drivers》對kobject的理解,對kobject.c文件添加注釋,僅供參考!

1 /**

2 * populate_dir - populate directory with attributes.
3 * @kobj: object we're working on.
4 *
5 * Most subsystems have a set of default attributes that
6 * are associated with an object that registers with them.
7 * This is a helper called during object registration that
8 * loops through the default attributes of the subsystem
9 * and creates attributes files for them in sysfs.
10 *
11 */
12
13 static int populate_dir(struct kobject * kobj)
14 {
15 /* [cgw]: 從kobject取出kobj_type成員指針 */
16 struct kobj_type * t = get_ktype(kobj);
17 struct attribute * attr;
18 int error = 0;
19 int i;
20 /* [cgw]: kobj_type中默認屬性default_attrs不為空,即已經添加一個或以上
21 * 屬性項
22 */
23 if (t && t->default_attrs) {
24 /* [cgw]: 輪詢default_attrs中的指針不為空,即已經添加屬性 */
25 for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
26 /* [cgw]: 在sysfs中為這個kobj創建一個屬性文件 */
27 if ((error = sysfs_create_file(kobj,attr)))
28 break;
29 }
30 }
31 return error;
32 }

1 static int create_dir(struct kobject * kobj, struct dentry *shadow_parent)

2 {
3 int error = 0;
4 /* [cgw]: 獲得這個kobj的名字,名字不為空 */
5 if (kobject_name(kobj)) {
6 /* [cgw]: 為kobj創建一個目錄
7 * @shadow_parent: parent parent object. (父對象)
8 *
9 */
10 error = sysfs_create_dir(kobj, shadow_parent);
11 if (!error) {
12 /* [cgw]: 添加kobj屬性*/
13 if ((error = populate_dir(kobj)))
14 /* [cgw]: 添加失敗,刪除kobj對象目錄 */
15 sysfs_remove_dir(kobj);
16 }
17 }
18 return error;
19 }

1 static inline struct kobject * to_kobj(struct list_head * entry)

2 {
3 /* [cgw]: 從鏈表list_head取出一個entry
4 * 根據這個entry,找到包含這個entry的
5 * kobject結構體地址
6 */
7 return container_of(entry,struct kobject,entry);
8 }

1 static int get_kobj_path_length(struct kobject *kobj)

2 {
3 int length = 1;
4 struct kobject * parent = kobj;
5
6 /* walk up the ancestors until we hit the one pointing to the
7 * root.
8 * Add 1 to strlen for leading '/' of each level.
9 */
10 /* [cgw]: 逐個節點查找對象的名字,計算每節點對象的名字長度,
11 * 並加1,加1是每個名字前有個'/',並累計所有名字長度
12 */
13 do {
14 /* [cgw]: 名字不為空 */
15 if (kobject_name(parent) == NULL)
16 return 0;
17 /* [cgw]: 累計名字長度 */
18 length += strlen(kobject_name(parent)) + 1;
19 /* [cgw]: 指向上一節點(當前對象的父輩) */
20 parent = parent->parent;
21 } while (parent);
22 return length;
23 }

1 static void fill_kobj_path(struct kobject *kobj, char *path, int length)

2 {
3 struct kobject * parent;
4
5 /* [cgw]: 逐個節點查找對象的名字,並填裝到path中 */
6 --length;
7 for (parent = kobj; parent; parent = parent->parent) {
8 /* [cgw]: 計算當前節點對象名字長度 */
9 int cur = strlen(kobject_name(parent));
10 /* back up enough to print this name with '/' */
11 /* [cgw]: 總長度減去一個名字的長度 */
12 length -= cur;
13 /* [cgw]: 把這個對象名字填裝到path的path + length位置 */
14 strncpy (path + length, kobject_name(parent), cur);
15 /* [cgw]: 在path的path + length -1的位置插入'/' */
16 *(path + --length) = '/';
17 }
18
19 pr_debug("%s: path = '%s'\n",__FUNCTION__,path);
20 }

1 /**

2 * kobject_get_path - generate and return the path associated with a given kobj and kset pair.
3 *
4 * @kobj: kobject in question, with which to build the path
5 * @gfp_mask: the allocation type used to allocate the path
6 *
7 * The result must be freed by the caller with kfree().
8 */
9 char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
10 {
11 char *path;
12 int len;
13
14 /* [cgw]: 根據當前節點kobj,統計從當前節點開始到所有父輩節點的名字
15 * 總長度
16 */
17 len = get_kobj_path_length(kobj);
18 /* [cgw]: 長度為0,錯誤 */
19 if (len == 0)
20 return NULL;
21 /* [cgw]: 分配長度len個字節的內存空間,並清0內存 */
22 path = kzalloc(len, gfp_mask);
23 if (!path)
24 return NULL;
25 /* [cgw]: 把所有節點對象名字填裝到path中 */
26 fill_kobj_path(kobj, path, len);
27
28 return path;
29 }
30 EXPORT_SYMBOL_GPL(kobject_get_path);

1 /**
2 * kobject_init - initialize object.
3 * @kobj: object in question.
4 */
5 void kobject_init(struct kobject * kobj)
6 {
7 /* [cgw]: kobj為空,錯誤 */
8 if (!kobj)
9 return;
10 /* [cgw]: 初始化kobj->kref引用計數為1 */
11 kref_init(&kobj->kref);
12 /* [cgw]: 初始化kobj->entry鏈表 */
13 INIT_LIST_HEAD(&kobj->entry);
14 /* [cgw]: 初始化kobj->poll */
15 init_waitqueue_head(&kobj->poll);
16 /* [cgw]: 獲得kset成員的指針 */
17 kobj->kset = kset_get(kobj->kset);
18 }

/**

* unlink - remove kobject from kset list.
* @kobj: kobject.
*
* Remove the kobject from the kset list and decrement
* its parent's refcount.
* This is separated out, so we can use it in both
* kobject_del() and kobject_add() on error.
*/

static void unlink(struct kobject * kobj)
{
/* [cgw]: kobj所在kobj->kset集合存在 */
if (kobj->kset) {
spin_lock(&kobj->kset->list_lock);
/* [cgw]: 從鏈表中刪除一個kobj條目 */
list_del_init(&kobj->entry);
spin_unlock(&kobj->kset->list_lock);
}
/* [cgw]: kobj引用計數-1 */
kobject_put(kobj);
}

/**
* kobject_shadow_add - add an object to the hierarchy.
* @kobj: object.
* @shadow_parent: sysfs directory to add to.
*/

int kobject_shadow_add(struct kobject * kobj, struct dentry *shadow_parent)
{
int error = 0;
struct kobject * parent;

/* [cgw]: kobj引用計數+1,並返回kobj指針 */
if (!(kobj = kobject_get(kobj)))
return -ENOENT;
/* [cgw]: kobj對象名字為空,即指針為空 */
if (!kobj->k_name)
/* [cgw]: 添加名字,k_name指針指向name */
kobj->k_name = kobj->name;
/* [cgw]: kobj對象名字為空,即指針為空 */
if (!*kobj->k_name) {
pr_debug("kobject attempted to be registered with no name!\n");
WARN_ON(1);
/* [cgw]: kobj引用計數-1,並返回kobj指針
* 如果kobj引用計數為0,則kobj將被移除
*/
kobject_put(kobj);
return -EINVAL;
}
/* [cgw]: 找到kobj的父節點 */
parent = kobject_get(kobj->parent);

/* [cgw]: 打印當前節點和上一節點kobj的名字,和所屬集合kset的kobj名字*/
pr_debug("kobject %s: registering. parent: %s, set: %s\n",
kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>",
kobj->kset ? kobj->kset->kobj.name : "<NULL>" );
/* [cgw]: kobj所在集合kset指針存在 */
if (kobj->kset) {
/* [cgw]: 進入臨界區 */
spin_lock(&kobj->kset->list_lock);
/* [cgw]: kobj父節點不為空 */
if (!parent)
/* [cgw]: 找出kobj的成員kset的kobj對象 */
parent = kobject_get(&kobj->kset->kobj);
/* [cgw]: 把當前kobj成員entry插入到kobj->kset->list鏈表中,
* entry插入到kobj->kset->list節點前
*/
list_add_tail(&kobj->entry,&kobj->kset->list);
/* [cgw]: 退出臨界區 */
spin_unlock(&kobj->kset->list_lock);
/* [cgw]: kobj的父節點指針指向kobj的成員kset的kobj
* 因為entry已經加入到kobj->kset->list中,即kobj已經加入了kset
* 集合
*/
kobj->parent = parent;
}

/* [cgw]: 為這個kobj創建目錄,即加入到sysfs目錄,
* 並為這個kobj添加屬性
*/
error = create_dir(kobj, shadow_parent);
/* [cgw]: 創建kobj目錄發生錯誤 */
if (error) {
/* unlink does the kobject_put() for us */
/* [cgw]: 移除當前kobj->entery節點 */
unlink(kobj);
/* [cgw]: kobj父節點引用計數-1*/
kobject_put(parent);

/* be noisy on error issues */
if (error == -EEXIST)
/* [cgw]: 在相同目錄下出現相同的對象名 */
printk(KERN_ERR "kobject_add failed for %s with "
"-EEXIST, don't try to register things with "
"the same name in the same directory.\n",
kobject_name(kobj));
else
/* [cgw]: kobject_add操作失敗,因本函數是直接在kobject_add調用
*
*/
printk(KERN_ERR "kobject_add failed for %s (%d)\n",
kobject_name(kobj), error);
dump_stack();
}

return error;
}

1 /**

2 * kobject_add - add an object to the hierarchy.
3 * @kobj: object.
4 */
5 int kobject_add(struct kobject * kobj)
6 {
7 /* [cgw]: 添加kobj到層,實際上是把kobj添加到kset集合
8 * entry插入到kobj->kset->list鏈表中, 為這個kobj創建目錄,
9 * 即加入到sysfs目錄,並為這個kobj添加屬性
10 */
11 return kobject_shadow_add(kobj, NULL);
12 }

1 /**

2 * kobject_register - initialize and add an object.
3 * @kobj: object in question.
4 */
5
6 int kobject_register(struct kobject * kobj)
7 {
8 int error = -EINVAL;
9 /* [cgw]: 已經定義了一個kobj對象 */
10 if (kobj) {
11 /* [cgw]: 初始化這個kobj對象 */
12 kobject_init(kobj);
13 /* [cgw]: 添加這個kobj到sysfs目錄,kset集合 */
14 error = kobject_add(kobj);
15 /* [cgw]: 添加成功 */
16 if (!error)
17 /* [cgw]: 通知用戶空間發生了KOBJ_ADD事件 */
18 kobject_uevent(kobj, KOBJ_ADD);
19 }
20 return error;
21 }

1 /**

2 * kobject_set_name - Set the name of an object
3 * @kobj: object.
4 * @fmt: format string used to build the name
5 *
6 * If strlen(name) >= KOBJ_NAME_LEN, then use a dynamically allocated
7 * string that @kobj->k_name points to. Otherwise, use the static
8 * @kobj->name array.
9 */
10 int kobject_set_name(struct kobject * kobj, const char * fmt, ...)
11 {
12 int error = 0;、
13 /* [cgw]: kobj名字長度限制 */
14 int limit = KOBJ_NAME_LEN;
15 int need;
16 va_list args;
17 char * name;
18
19 /*
20 * First, try the static array
21 */
22 /* [cgw]: 格式化參數中的字符到kobj->name數組 */
23 va_start(args,fmt);
24 need = vsnprintf(kobj->name,limit,fmt,args);
25 va_end(args);
26 /* [cgw]: 長度在限制以內 */
27 if (need < limit)
28 /* [cgw]: name指向kobj->name */
29 name = kobj->name;
30 else {
31 /*
32 * Need more space? Allocate it and try again
33 */
34 /* [cgw]: 長度超出限制以外 */
35 limit = need + 1;
36 /* [cgw]: 分配長度為need + 1個字節的內存空間 */
37 name = kmalloc(limit,GFP_KERNEL);
38 /* [cgw]: 分配失敗 */
39 if (!name) {
40 error = -ENOMEM;
41 goto Done;
42 }
43 /* [cgw]: 重新格式化到name */
44 va_start(args,fmt);
45 need = vsnprintf(name,limit,fmt,args);
46 va_end(args);
47
48 /* Still? Give up. */
49 /* [cgw]: 還是不夠? 算了,有心無力了 */
50 if (need >= limit) {
51 /* [cgw]: 釋放分配給name的內存空間 */
52 kfree(name);
53 error = -EFAULT;
54 goto Done;
55 }
56 }
57
58 /* Free the old name, if necessary. */
59 /* [cgw]: 舊的名字不要了 */
60 if (kobj->k_name && kobj->k_name != kobj->name)
61 /* [cgw]: 釋放舊名字的內存空間 */
62 kfree(kobj->k_name);
63
64 /* Now, set the new name */
65 /* [cgw]: 重置新名字 */
66 kobj->k_name = name;
67 Done:
68 return error;
69 }
70
71 EXPORT_SYMBOL(kobject_set_name);

1 /**

2 * kobject_rename - change the name of an object
3 * @kobj: object in question.
4 * @new_name: object's new name
5 */
6
7 int kobject_rename(struct kobject * kobj, const char *new_name)
8 {
9 int error = 0;
10 const char *devpath = NULL;
11 char *devpath_string = NULL;
12 char *envp[2];
13
14 /* [cgw]: kobj引用計數+1 */
15 kobj = kobject_get(kobj);
16 /* [cgw]: kobj為空 */
17 if (!kobj)
18 return -EINVAL;
19 /* [cgw]: kobj的父節點為空 */
20 if (!kobj->parent)
21 return -EINVAL;
22 /* [cgw]: 獲得當前節點kobj和所有父節點的名字 */
23 devpath = kobject_get_path(kobj, GFP_KERNEL);
24 /* [cgw]: 獲取失敗 */
25 if (!devpath) {
26 error = -ENOMEM;
27 goto out;
28 }
29 /* [cgw]: 分配strlen(devpath) + 15字節內存空間 */
30 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
31 /* [cgw]: 分配失敗 */
32 if (!devpath_string) {
33 error = -ENOMEM;
34 goto out;
35 }
36 /* [cgw]: 格式化"DEVPATH_OLD=%s" + devpath 存到devpath_string */
37 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
38 /* [cgw]: 存入環境變量 */
39 envp[0] = devpath_string;
40 envp[1] = NULL;
41 /* Note : if we want to send the new name alone, not the full path,
42 * we could probably use kobject_name(kobj); */
43 /* [cgw]: 重命名kobj對象 */
44 error = sysfs_rename_dir(kobj, kobj->parent->dentry, new_name);
45
46 /* This function is mostly/only used for network interface.
47 * Some hotplug package track interfaces by their name and
48 * therefore want to know when the name is changed by the user. */
49 /* [cgw]: 重命名成功 */
50 if (!error)
51 /* [cgw]: 發送一個用戶事件KOBJ_MOVE,和環境變量 */
52 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
53
54 out:
55 /* [cgw]: 釋放devpath_string和devpath內存 */
56 kfree(devpath_string);
57 kfree(devpath);
58 /* [cgw]: kobj引用計數-1,並刪除kobj */
59 kobject_put(kobj);
60
61 return error;
62 }

1 /**

2 * kobject_rename - change the name of an object
3 * @kobj: object in question.
4 * @new_parent: object's new parent
5 * @new_name: object's new name
6 */
7
8 int kobject_shadow_rename(struct kobject * kobj, struct dentry *new_parent,
9 const char *new_name)
10 {
11 int error = 0;
12 /* [cgw]: kobj引用計數+1*/
13 kobj = kobject_get(kobj);
14 if (!kobj)
15 return -EINVAL;
16 /* [cgw]: 重命名kobj對象 */
17 error = sysfs_rename_dir(kobj, new_parent, new_name);
18 /* [cgw]: kobj引用計數-1,並刪除kobj */
19 kobject_put(kobj);
20
21 return error;
22 }

1 /**

2 * kobject_move - move object to another parent
3 * @kobj: object in question.
4 * @new_parent: object's new parent (can be NULL)
5 */
6
7 int kobject_move(struct kobject *kobj, struct kobject *new_parent)
8 {
9 int error;
10 struct kobject *old_parent;
11 const char *devpath = NULL;
12 char *devpath_string = NULL;
13 char *envp[2];
14
15 /* [cgw]: kobj引用計數+1*/
16 kobj = kobject_get(kobj);
17 /* [cgw]: kobj指針為空*/
18 if (!kobj)
19 return -EINVAL;
20 /* [cgw]: kobj父節點引用計數+1*/
21 new_parent = kobject_get(new_parent);
22 /* [cgw]: kobj指針為空*/
23 if (!new_parent) {
24 /* [cgw]: kobj所屬kset存在 */
25 if (kobj->kset)
26 /* [cgw]: kobj->kset->kobj引用計數+1
27 */
28 new_parent = kobject_get(&kobj->kset->kobj);
29 }
30 /* old object path */
31 /* [cgw]: 獲得kobj 和所有父節點對象名字,即路徑名*/
32 devpath = kobject_get_path(kobj, GFP_KERNEL);
33 /* [cgw]: 獲取失敗 */
34 if (!devpath) {
35 error = -ENOMEM;
36 goto out;
37 }
38 /* [cgw]: 分配strlen(devpath) + 15字節內存空間 */
39 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
40 /* [cgw]: 分配失敗 */
41 if (!devpath_string) {
42 error = -ENOMEM;
43 goto out;
44 }
45 /* [cgw]: 格式化"DEVPATH_OLD=%s" + devpath 存到devpath_string */
46 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
47 /* [cgw]: 存入環境變量 */
48 envp[0] = devpath_string;
49 envp[1] = NULL;
50 /* [cgw]: 移動kobj到新的父節點對象 */
51 error = sysfs_move_dir(kobj, new_parent);
52 /* [cgw]: 失敗 */
53 if (error)
54 goto out;
55 /* [cgw]: 記錄kobj舊的的父節點對象 */
56 old_parent = kobj->parent;
57 /* [cgw]: 用新的父節點對象代替舊的 */
58 kobj->parent = new_parent;
59 new_parent = NULL;
60 /* [cgw]: kobj舊的父節點對象引用計數-1, 並刪除舊的父節點對象 */
61 kobject_put(old_parent);
62 /* [cgw]: 發送一個用戶事件KOBJ_MOVE,和環境變量 */
63 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
64 out:
65 /* [cgw]: kobj新的父節點對象引用計數-1, 並刪除新的父節點對象 */
66 kobject_put(new_parent);
67 /* [cgw]: kobj引用計數-1, 並刪除kobj */
68 kobject_put(kobj);
69 /* [cgw]: 釋放devpath_string和devpath內存 */
70 kfree(devpath_string);
71 kfree(devpath);
72 return error;
73 }

1 /**

2 * kobject_del - unlink kobject from hierarchy.
3 * @kobj: object.
4 */
5
6 void kobject_del(struct kobject * kobj)
7 {
8 /* [cgw]: kobj對象不存在 */
9 if (!kobj)
10 return;
11 /* [cgw]: 從sysfs目錄移除kobj對象,清0 kobj->dentry節點 */
12 sysfs_remove_dir(kobj);
13 /* [cgw]: 刪除kobj->dentry節點 */
14 unlink(kobj);
15 }

1 /**

2 * kobject_unregister - remove object from hierarchy and decrement refcount.
3 * @kobj: object going away.
4 */
5
6 void kobject_unregister(struct kobject * kobj)
7 {
8 if (!kobj)
9 return;
10 pr_debug("kobject %s: unregistering\n",kobject_name(kobj));
11 /* [cgw]: 通知用戶空間發生了KOBJ_REMOVE事件 */
12 kobject_uevent(kobj, KOBJ_REMOVE);
13 /* [cgw]: 從sysfs目錄刪除kobj對象(刪除kobj->entery, kobj->kset->list節點) */
14 kobject_del(kobj);
15 /* [cgw]: 刪除kobj對象,釋放內存空間 */
16 kobject_put(kobj);
17 }

1 /**

2 * kobject_get - increment refcount for object.
3 * @kobj: object.
4 */
5
6 struct kobject * kobject_get(struct kobject * kobj)
7 {
8 /* [cgw]: 指針不為空 */
9 if (kobj)
10 /* [cgw]: 引用計數+1 */
11 kref_get(&kobj->kref);
12 return kobj;
13 }

1 /**

2 * kobject_cleanup - free kobject resources.
3 * @kobj: object.
4 */
5
6 void kobject_cleanup(struct kobject * kobj)
7 {
8 /* [cgw]: 找到kobj->ktype */
9 struct kobj_type * t = get_ktype(kobj);
10 struct kset * s = kobj->kset;
11 struct kobject * parent = kobj->parent;
12
13 pr_debug("kobject %s: cleaning up\n",kobject_name(kobj));
14 /* [cgw]: k_name沒有指向name */
15 if (kobj->k_name != kobj->name)
16 /* [cgw]: 釋放k_name內存空間 */
17 kfree(kobj->k_name);
18 /* [cgw]: k_name指針清零 */
19 kobj->k_name = NULL;
20 /* [cgw]: ktype定義的release函數 */
21 if (t && t->release)
22 /* [cgw]: 刪除kobj,並釋放內存 */
23 t->release(kobj);
24 if (s)
25 /* [cgw]: kobj->kset->kobj引用計數-1 */
26 kset_put(s);
27 /* [cgw]: kobj父節點引用計數-1 */
28 kobject_put(parent);
29 }

1 static void kobject_release(struct kref *kref)

2 {
3 /* [cgw]: 刪除kobj->k_name, kobj, kobj->parent, kobj->kset,
4 * 調用kobj->ktype->release
5 */
6 kobject_cleanup(container_of(kref, struct kobject, kref));
7 }

1 /**

2 * kobject_put - decrement refcount for object.
3 * @kobj: object.
4 *
5 * Decrement the refcount, and if 0, call kobject_cleanup().
6 */
7 void kobject_put(struct kobject * kobj)
8 {
9 /* [cgw]: 指針不為空 */
10 if (kobj)
11 /* [cgw]: 引用計數-1 ,kobj->kref為0時,調用kobject_release */
12 kref_put(&kobj->kref, kobject_release);
13 }

1 static void dir_release(struct kobject *kobj)

2 {
3 /* [cgw]: 釋放kobj內存空間 */
4 kfree(kobj);
5 }

1 static struct kobj_type dir_ktype = {

2 .release = dir_release,
3 .sysfs_ops = NULL,
4 .default_attrs = NULL,
5 };

1 /**

2 * kobject_kset_add_dir - add sub directory of object.
3 * @kset: kset the directory is belongs to.
4 * @parent: object in which a directory is created.
5 * @name: directory name.
6 *
7 * Add a plain directory object as child of given object.
8 */
9 struct kobject *kobject_kset_add_dir(struct kset *kset,
10 struct kobject *parent, const char *name)
11 {
12 struct kobject *k;
13 int ret;
14
15 /* [cgw]: parent指針為空 */
16 if (!parent)
17 return NULL;
18
19 /* [cgw]: 分配一個struct kobject的內存空間 */
20 k = kzalloc(sizeof(*k), GFP_KERNEL);
21 /* [cgw]: 分配失敗 */
22 if (!k)
23 return NULL;
24
25 /* [cgw]: 分配kobj->kset */
26 k->kset = kset;
27 /* [cgw]: 分配kobj->parent */
28 k->parent = parent;
29 /* [cgw]: 分配kobj->ktype */
30 k->ktype = &dir_ktype;
31 /* [cgw]: 給kobj->k_name分配名字 */
32 kobject_set_name(k, name);
33 /* [cgw]: 注冊kobj */
34 ret = kobject_register(k);
35 /* [cgw]: 注冊失敗 */
36 if (ret < 0) {
37 printk(KERN_WARNING "%s: kobject_register error: %d\n",
38 __func__, ret);
39 /* [cgw]: 刪除kobj */
40 kobject_del(k);
41 return NULL;
42 }
43
44 return k;
45 }

1 /**

2 * kobject_add_dir - add sub directory of object.
3 * @parent: object in which a directory is created.
4 * @name: directory name.
5 *
6 * Add a plain directory object as child of given object.
7 */
8 struct kobject *kobject_add_dir(struct kobject *parent, const char *name)
9 {
10 /* [cgw]: 添加一個kobj對象子目錄
11 * kobj->kset為空
12 */
13 return kobject_kset_add_dir(NULL, parent, name);
14 }

1 /**

2 * kset_init - initialize a kset for use
3 * @k: kset
4 */
5
6 void kset_init(struct kset * k)
7 {
8 /* [cgw]: 初始化kset成員kobj對象 */
9 kobject_init(&k->kobj);
10 /* [cgw]: 初始化kset成員list鏈表 */
11 INIT_LIST_HEAD(&k->list);
12 /* [cgw]: 初始化保護kset成員list鏈表的自旋鎖 */
13 spin_lock_init(&k->list_lock);
14 }

1 /**

2 * kset_add - add a kset object to the hierarchy.
3 * @k: kset.
4 */
5
6 int kset_add(struct kset * k)
7 {
8 /* [cgw]: 添加kset成員kobj對象
9 *
10 */
11 return kobject_add(&k->kobj);
12 }

1 /**

2 * kset_register - initialize and add a kset.
3 * @k: kset.
4 */
5
6 int kset_register(struct kset * k)
7 {
8 /* [cgw]: 指針為空 */
9 if (!k)
10 return -EINVAL;
11 /* [cgw]: 初始化並添加kset */
12 kset_init(k);
13 return kset_add(k);
14 }

1 /**

2 * kset_unregister - remove a kset.
3 * @k: kset.
4 */
5
6 void kset_unregister(struct kset * k)
7 {
8 /* [cgw]: 指針為空 */
9 if (!k)
10 return;
11 /* [cgw]: 刪除kset,實際是刪除kset->kobj */
12 kobject_unregister(&k->kobj);
13 }

1 /**

2 * kset_find_obj - search for object in kset.
3 * @kset: kset we're looking in.
4 * @name: object's name.
5 *
6 * Lock kset via @kset->subsys, and iterate over @kset->list,
7 * looking for a matching kobject. If matching object is found
8 * take a reference and return the object.
9 */
10
11 struct kobject * kset_find_obj(struct kset * kset, const char * name)
12 {
13 struct list_head * entry;
14 struct kobject * ret = NULL;
15
16 /* [cgw]: 進入臨界區 */
17 spin_lock(&kset->list_lock);
18 /* [cgw]: 以kset->list為頭節點開始輪詢kset->list鏈表中
19 * 每個節點,取出存到entry
20 */
21 list_for_each(entry,&kset->list) {
22 /* [cgw]: 取出一個entry,根據這個entry,找到對應kobj
23 */
24 struct kobject * k = to_kobj(entry);
25 /* [cgw]: 找到對應的kobj,這個kobj的名字是否是要找的kobj的名字匹配 */
26 if (kobject_name(k) && !strcmp(kobject_name(k),name)) {
27 /* [cgw]: 找到了這個kobj,引用計數+1 */
28 ret = kobject_get(k);
29 break;
30 }
31 }
32 /* [cgw]: 退出臨界區 */
33 spin_unlock(&kset->list_lock);
34 return ret;
35 }

1 void subsystem_init(struct kset *s)

2 {
3 /* [cgw]: 初始化kset,每一個 kset都屬於一個子系統
4 * Every kset must belong to a subsystem. The subsystem
5 * membership helps establish the kset's position in the hierarchy
6 * <Linux Device Drivers>
7 */
8 kset_init(s);
9 }
10
11 int subsystem_register(struct kset *s)
12 {
13 /* [cgw]: 注冊kset */
14 return kset_register(s);
15 }
16
17 void subsystem_unregister(struct kset *s)
18 {
19 /* [cgw]: 注銷kset */
20 kset_unregister(s);
21 }
22
23 /**
24 * subsystem_create_file - export sysfs attribute file.
25 * @s: subsystem.
26 * @a: subsystem attribute descriptor.
27 */
28
29 int subsys_create_file(struct kset *s, struct subsys_attribute *a)
30 {
31 int error = 0;
32
33 if (!s || !a)
34 return -EINVAL;
35 /* [cgw]: 這個操作最終導致,kset->kobj引用計數+1
36 * 並返回ket指針
37 */
38 if (subsys_get(s)) {
39 /* [cgw]: 為kset->kobj這個對象創建一個屬性文件 */
40 error = sysfs_create_file(&s->kobj, &a->attr);
41 /* [cgw]: 這個操作最終導致,kset->kobj引用計數-1
42 * 並返回kset指針
43 */
44 subsys_put(s);
45 }
46 return error;
47 }
48
49 EXPORT_SYMBOL(kobject_init);
50 EXPORT_SYMBOL(kobject_register);
51 EXPORT_SYMBOL(kobject_unregister);
52 EXPORT_SYMBOL(kobject_get);
53 EXPORT_SYMBOL(kobject_put);
54 EXPORT_SYMBOL(kobject_add);
55 EXPORT_SYMBOL(kobject_del);
56
57 EXPORT_SYMBOL(kset_register);
58 EXPORT_SYMBOL(kset_unregister);
59
60 EXPORT_SYMBOL(subsystem_register);
61 EXPORT_SYMBOL(subsystem_unregister);
62 EXPORT_SYMBOL(subsys_create_file);


http://xxxxxx/Linuxjc/1144280.html TechArticle

Copyright © Linux教程網 All Rights Reserved