What is a cleaner way to get a pointer to a structural device in Linux?

I will need to get a pointer to a specific device registered in Linux. In short, this device represents a mii_bus object. The problem is that this device does not seem to apply to the bus (its dev->bus is NULL ), so I cannot use, for example, the bus_for_each_dev function. However, the device is registered at the Open Firmware level, 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 class , so I can find it in /sys/class/mdio_bus . Now the questions are:

  • Is it possible to get a pointer by pointing to a pointer of_device , which is the parent device of the device we need?

  • How can I get a pointer to an already created class using only the name? If it were possible, I could iterate over devices of this class.

Any other advice would be very helpful! Thanks to everyone.

+4
source share
1 answer

I have found a way. I will explain it briefly, maybe it can be useful. The method we could use is device_find_child . The method takes as a third parameter a pointer to a function that implements the comparison logic. If the function returns non-zero when called with a specific device as the first parameter, device_find_child will return this 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; } 
+5
source

All Articles