Which is the cleaner way to get a pointer to a struct device in linux?

亡梦爱人 提交于 2019-12-23 12:38:09

问题


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:

  1. It's possible to obtain the pointer using the pointer to the of_device that is the parent of the device we want?

  2. 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.


回答1:


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;
}


来源:https://stackoverflow.com/questions/7636801/which-is-the-cleaner-way-to-get-a-pointer-to-a-struct-device-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!