分析platform_get_resource

 

有如下实例:

static struct resource net_resources[] = {

[0] = {

        .start       = ETHERNET_BASE,

        .end = ETHERNET_BASE + SZ_4K - 1,

        .flags      = IORESOURCE_MEM,

},

 

[1] = {

        .start       = IRQ_ETHERNET,

        .end = IRQ_ETHERNET,

        .flags      = IORESOURCE_IRQ,

},

};

当获取平台资源时

res = platform_get_resource(pdev, IORESOURCE_MEM, 0);

irq = platform_get_irq(pdev, 0);

源码:

/**

 * platform_get_resource - get a resource for a device

 * @dev: platform device

 * @type: resource type

 * @num: resource index

 */

struct resource *platform_get_resource(struct platform_device *dev,

                                   unsigned int type, unsigned int num)

{

       int i;

 

       for (i = 0; i < dev->num_resources; i++) {

              struct resource *r = &dev->resource[i];

 

              if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|

                             IORESOURCE_IRQ|IORESOURCE_DMA)) == type)

                     if (num-- == 0)

                            return r;

       }

       return NULL;

}

根据实例和源码可以得出结论:num代表的不是resource数组的数组号,而是具有相同资源类型的序号(从0开始的序号)。在实例中,因为IORESOURCE_IRQ类型的资源只有一个,故platform_get_irq()函数的第二个参数是0,而不是resource数组的序号1

完毕

你可能感兴趣的:(分析platform_get_resource)