Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is platform_get_resource in Linux driver?

Can somebody please explain why and how to use platform_get_resource function ?

I've seen IORESOURCE_MEM 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.

  • http://lwn.net/Articles/448499/
  • http://www.gnugeneration.com/books/linux/2.6.20/kernel-api/re720.html
like image 641
Sagar Jain Avatar asked Apr 09 '14 11:04

Sagar Jain


People also ask

What are the two types of drivers in Linux?

There are various types of drivers present in GNU/Linux such as Character, Block, Network and USB drivers. In this column, we will explore only character drivers. Character drivers are the most common drivers. They provide unbuffered, direct access to hardware devices.

What is driver model in Linux?

The Linux Kernel Driver Model is a unification of all the disparate driver models that were previously used in the kernel. It is intended to augment the bus-specific drivers for bridges and devices by consolidating a set of data and operations into globally accessible data structures.

What is Ioremap in Linux?

I/O memory is a set of contiguous addresses which are provided by a device to the CPU through a bus. None of the memory-mapped I/O addresses are used by the kernel directly. There is a special ioremap function which allows us to convert the physical address on a bus to a kernel virtual address.

How does Linux load drivers?

How does the Linux kernel know which drivers to load at boot? The kernel generates events for devices on e.g. the PCI bus when they are plugged (either hot or cold; events are queued until userspace runs AFAIR).


1 Answers

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, http://lwn.net/Articles/448499/ 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
}
like image 136
Manolis Ragkousis Avatar answered Sep 22 '22 18:09

Manolis Ragkousis