在 /sys 下的某个目录下新建目录并且添加一个链接,指向某个设备
思路:
既然是sys目录操作,那肯定找sysfs的接口,创建链接 创建目录 的接口很快找到,但是在sysfs接口里没有查找。这里可使用设备树of相关接口(要按照设备树里的名称来查找),另外费了不少精力,发现使用kset查找接口也行,查找的名称就是sys目录下的名称
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kobject.h>
#include <linux/device.h>
#include <linux/of.h>
extern struct device platform_bus;
static struct kobject * test_kobj = NULL;
static int test_init(void)
{
int ret;
struct kobject * find_kobj;
//在/sys/device/platform/下创建目录
test_kobj = kobject_create_and_add("abhw", &(platform_bus.kobj));
if (NULL == abhw_kobj)
{
...
}
//在/sys/device/platform下查找设备
find_kobj = kset_find_obj(platform_bus.kobj.kset, "设备名");
if(NULL == find_kobj)
{
...
}
ret = sysfs_create_link(abhw_kobj, find_kobj, "testlink");
if(0 != ret)
{
...
}
return 0;
}
static void test_exit(void)
{
kobject_put(test_kobj);
}
module_init(test_init);
module_exit(test_exit);