SJain SJain - 1 year ago 133
Linux Question

What is platform_get_resource in Linux driver?

Can somebody please explain why and how to use

function ?

I've seen
is used at many places, like the one here, as the second parameter, what does this mean?

I've gone through the links below but could not get the proper explanation.

Answer Source

platform_get_resource() is used in the __init function of a driver to get information on the structure of the device resource, like start adress and end adress, in order to find the resource memory size so you can map it in memory.

the declaration of platform_get_resource function is the following

struct resource * platform_get_resource (   struct platform_device * dev,
                                            unsigned int    type,
                                            unsigned int    num);

The first parameter tells the function which device we are interested in, so it can extract the info we need.

The second parameter depends on what kind of resource you are handling. If it is memory( or anything that can be mapped as memory :-)) then it's IORESOURCE_MEM. You can see all the macros at include/linux/ioport.h

For the last parameter, says:

The last parameter says which resource of that type is desired, with zero indicating the first one. Thus, for example, a driver could find its second MMIO region with:

r = platform_get_resource(pdev, IORESOURCE_MEM, 1);

The return value is a pointer to a type struct resource var.

Here is an example

unsigned long *base_addr; /* Virtual Base Address */
struct resource *res; /* Device Resource Structure */
unsigned long remap_size; /* Device Memory Size */

static int __devinit bram_io_probe(struct platform_device *pdev) 
   res = platform_get_resource(pdev, IORESOURCE_MEM, 0); // get resource info
   remap_size = res->end - res->start + 1;               // get resource memory size
   base_addr = ioremap(res->start, remap_size);          // map it