流程分析
kobject/kset
的相關(guān)代碼比較簡(jiǎn)單,畢竟它只是作為一個(gè)結(jié)構(gòu)體嵌入其他high-level的結(jié)構(gòu)中,充當(dāng)紐帶的作用。不過(guò),我還是簡(jiǎn)單的上一張圖吧:
- 完成的工作基本就是分配結(jié)構(gòu)體,初始化各個(gè)結(jié)構(gòu)體字段,構(gòu)建拓?fù)潢P(guān)系(主要是添加到kset的list中,parent的指向等)等,看懂了結(jié)構(gòu)體的組織,這部分的代碼理解起來(lái)就很輕松了;
示例
先上一個(gè)原理圖:
代碼
#include < linux/kernel.h >
#include < linux/module.h >
#include < linux/slab.h >
#include < linux/kobject.h >
//自定義一個(gè)結(jié)構(gòu),包含了struct kobject子結(jié)構(gòu)
struct test_kobj {
int value;
struct kobject kobj;
};
//自定義個(gè)屬性結(jié)構(gòu)體,包含了struct attribute結(jié)構(gòu)
struct test_kobj_attribute {
struct attribute attr;
ssize_t (*show)(struct test_kobj *obj, struct test_kobj_attribute *attr, char *buf);
ssize_t (*store)(struct test_kobj *obj, struct test_kobj_attribute *attr, const char *buf, size_t count);
};
//聲明一個(gè)全局結(jié)構(gòu)用于測(cè)試
struct test_kobj *obj;
//用于初始化sysfs_ops中的函數(shù)指針
static ssize_t test_kobj_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
{
struct test_kobj_attribute *test_kobj_attr;
ssize_t ret = -EIO;
test_kobj_attr = container_of(attr, struct test_kobj_attribute, attr);
//回調(diào)到具體的實(shí)現(xiàn)函數(shù)
if (test_kobj_attr- >show)
ret = test_kobj_attr- >show(container_of(kobj, struct test_kobj, kobj), test_kobj_attr, buf);
return ret;
}
//用于初始化sysfs_ops中的函數(shù)指針
static ssize_t test_kobj_attr_store(struct kobject *kobj, struct attribute *attr, const char *buf, size_t count)
{
struct test_kobj_attribute *test_kobj_attr;
ssize_t ret = -EIO;
test_kobj_attr = container_of(attr, struct test_kobj_attribute, attr);
//回調(diào)到具體的實(shí)現(xiàn)函數(shù)
if (test_kobj_attr- >store)
ret = test_kobj_attr- >store(container_of(kobj, struct test_kobj, kobj), test_kobj_attr, buf, count);
return ret;
}
//用于初始化kobj_ktype
const struct sysfs_ops test_kobj_sysfs_ops = {
.show = test_kobj_attr_show,
.store = test_kobj_attr_store,
};
//用于初始化kobj_ktype,最終用于釋放kobject
void obj_release(struct kobject *kobj)
{
struct test_kobj *obj = container_of(kobj, struct test_kobj, kobj);
printk(KERN_INFO "test kobject release %sn", kobject_name(&obj- >kobj));
kfree(obj);
}
//定義kobj_ktype,用于指定kobject的類型,初始化的時(shí)候使用
static struct kobj_type test_kobj_ktype = {
.release = obj_release,
.sysfs_ops = &test_kobj_sysfs_ops,
};
//show函數(shù)的具體實(shí)現(xiàn)
ssize_t name_show(struct test_kobj *obj, struct test_kobj_attribute *attr, char *buffer)
{
return sprintf(buffer, "%sn", kobject_name(&obj- >kobj));
}
//show函數(shù)的具體實(shí)現(xiàn)
ssize_t value_show(struct test_kobj *obj, struct test_kobj_attribute *attr, char *buffer)
{
return sprintf(buffer, "%dn", obj- >value);
}
//store函數(shù)的具體實(shí)現(xiàn)
ssize_t value_store(struct test_kobj *obj, struct test_kobj_attribute *attr, const char *buffer, size_t size)
{
sscanf(buffer, "%d", &obj- >value);
return size;
}
//定義屬性,最終注冊(cè)進(jìn)sysfs系統(tǒng)
struct test_kobj_attribute name_attribute = __ATTR(name, 0664, name_show, NULL);
struct test_kobj_attribute value_attribute = __ATTR(value, 0664, value_show, value_store);
struct attribute *test_kobj_attrs[] = {
&name_attribute.attr,
&value_attribute.attr,
NULL,
};
//定義組
struct attribute_group test_kobj_group = {
.name = "test_kobj_group",
.attrs = test_kobj_attrs,
};
//模塊初始化函數(shù)
static int __init test_kobj_init(void)
{
int retval;
printk(KERN_INFO "test_kobj_initn");
obj = kmalloc(sizeof(struct test_kobj), GFP_KERNEL);
if (!obj) {
return -ENOMEM;
}
obj- >value = 1;
memset(&obj- >kobj, 0, sizeof(struct kobject));
//添加進(jìn)sysfs系統(tǒng)
kobject_init_and_add(&obj- >kobj, &test_kobj_ktype, NULL, "test_kobj");
//在sys文件夾下創(chuàng)建文件
retval = sysfs_create_files(&obj- >kobj, (const struct attribute **)test_kobj_attrs);
if (retval) {
kobject_put(&obj- >kobj);
return retval;
}
//在sys文件夾下創(chuàng)建group
retval = sysfs_create_group(&obj- >kobj, &test_kobj_group);
if (retval) {
kobject_put(&obj- >kobj);
return retval;
}
return 0;
}
//模塊清理函數(shù)
static void __exit test_kobj_exit(void)
{
printk(KERN_INFO "test_kobj_exitn");
kobject_del(&obj- >kobj);
kobject_put(&obj- >kobj);
return;
}
module_init(test_kobj_init);
module_exit(test_kobj_exit);
MODULE_AUTHOR("LoyenWang");
MODULE_LICENSE("GPL");
Makefile
ifneq ($(KERNELRELEASE),)
obj-m:=test_kobject.o
else
KERDIR := /lib/modules/$(shell uname -r)/build
PWD:=$(shell pwd)
all:
make -C $(KERDIR) M=$(PWD) modules
clean:
rm -f *.ko *.o *.symvers *.cmd *.cmd.o modules.* *.mod.c
endif
Makefile
沒(méi)有太多好說(shuō)的,注意Tab
的使用,否則容易出錯(cuò);
測(cè)試結(jié)果
- 在/sys目錄下創(chuàng)建了test_kobj文件夾,在該文件夾下除了
name
和value
外,還有一個(gè)test_kobj_group
的子文件夾; - 可以通過(guò)
cat/echo
的操作,來(lái)操作name
和value
,分別會(huì)調(diào)用到底層的xxx_show
和xxx_store
函數(shù); - 對(duì)著代碼看這個(gè)圖,一目了然;
-
嵌入式
+關(guān)注
關(guān)注
5152文章
19675瀏覽量
317602 -
Linux
+關(guān)注
關(guān)注
87文章
11511瀏覽量
213823 -
設(shè)備
+關(guān)注
關(guān)注
2文章
4668瀏覽量
71763 -
模型
+關(guān)注
關(guān)注
1文章
3521瀏覽量
50427 -
結(jié)構(gòu)體
+關(guān)注
關(guān)注
1文章
131瀏覽量
11115
發(fā)布評(píng)論請(qǐng)先 登錄
詳解linux設(shè)備驅(qū)動(dòng)模型架構(gòu)
Linux設(shè)備驅(qū)動(dòng)模型摘抄
Linux設(shè)備模型之一:Kobject
Linux設(shè)備模型:Bus
Linux設(shè)備模型學(xué)習(xí)筆記(1)

如何在代碼V中使用示例模型

評(píng)論