在Linux中获取指向struct设备的指针的更干净的方法是哪种? [英] Which is the cleaner way to get a pointer to a struct device in linux?
问题描述
我需要获取一个指向在Linux中注册的特定设备的指针.简而言之,此设备代表一个mii_bus
对象.问题在于该设备似乎不属于总线(其dev->bus
为NULL
),因此我无法使用例如功能bus_for_each_dev
.但是,该设备已由开放固件层注册,并且我可以在/sys/bus/of_platform
中看到相对的of_device
(这是我感兴趣的设备的父级).我的设备也在class
中注册,因此我可以在/sys/class/mdio_bus
中找到它.现在的问题:
i'd need to obtain a pointer to a particular device registered in linux. Briefly, this device represents a mii_bus
object. The problem is that this device seems doesn't belong to a bus (its dev->bus
is NULL
) so i can't use for example the function bus_for_each_dev
. The device is however registered by the Open Firmware layer and i can see the relative of_device
(which is the parent of the device i'm interested in) in /sys/bus/of_platform
. My device is also registered in a class
so i can find it in /sys/class/mdio_bus
. Now the questions:
-
是否可以使用指向
of_device
的指针来获取指针,该指针是我们想要的设备的父级?
It's possible to obtain the pointer using the pointer to the
of_device
that is the parent of the device we want?
如何仅使用名称获取指向已实例化的类的指针?如果可能,我可以遍历该类的设备.
How can i get a pointer to an already instantiated class by using only the name?If it was possible i could iterate over the devices of that class.
任何其他建议将非常有用!谢谢大家.
Any other advice would be very useful! Thank you all.
推荐答案
我找到了方法.我简要地解释一下,也许它可能有用.我们可以使用的方法是device_find_child
.该方法将指向实现比较逻辑的函数的指针作为第三个参数.如果以特定设备作为第一个参数调用时函数返回的值不为零,则device_find_child
将返回该指针.
I found the way. I explain it briefly, maybe it could be useful. The method we could use is device_find_child
. The method takes as third parameter a pointer to a function that implements the comparison logic. If the function returns not zero when called with a particular device as first parameter, device_find_child
will return that pointer.
#include <linux/device.h>
#include <linux/of_platform.h>
static int custom_match_dev(struct device *dev, void *data)
{
/* this function implements the comaparison logic. Return not zero if device
pointed by dev is the device you are searching for.
*/
}
static struct device *find_dev()
{
struct device *ofdev = bus_find_device_by_name(&of_platform_bus_type,
NULL, "OF_device_name");
if (ofdev)
{
/* of device is the parent of device we are interested in */
struct device *real_dev = device_find_child(ofdev,
NULL, /* passed in the second param to custom_match_dev */
custom_match_dev);
if (real_dev)
return real_dev;
}
return NULL;
}
这篇关于在Linux中获取指向struct设备的指针的更干净的方法是哪种?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!