Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 414
MirkoBanchi Avatar asked Oct 03 '11 15:10

MirkoBanchi


1 Answers

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;
}
like image 200
MirkoBanchi Avatar answered Nov 15 '22 10:11

MirkoBanchi