Platform_device的platform_get_resource分析移植

 

一、平台设备platform_device定义:

static struct platform_device at91_usba_udc_device = {
    .name   = "atmel_usba_udc",
    .id     = -1,
    .dev    = {
                 .platform_data  = &usba_udc_data.pdata,
    },
    .resource       = atmel_usba_resource,
    .num_resources  = ARRAY_SIZE(atmel_usba_resource),
};

二、相关结构体类型如下:

struct device {
    void        *platform_data; /* Platform specific data, device
                       core doesn't touch it */
};

struct resource {
    unsigned long   start;
    unsigned long   end;
    const char     *name;
    unsigned long   flags;
    struct resource *parent, *sibling, *child;
};

struct platform_device {
    const char   *name;
    int           id;
    struct device dev;
    u32           num_resources;
    struct resource * resource;
};


三、设备资源如下:

static struct resource atmel_usba_resource[] = {
    [0] = {
        .start  = UDPHS_FIFO_BA,                                        /*  FIFO首地址                  */
        .end    = UDPHS_FIFO_BA + 1 * LW_CFG_MB_SIZE,
        .flags  = IORESOURCE_MEM,
    },
    [1] = {
        .start  = UDPHS_BA,                                             /*  CTRL首地址                  */
        .end    = UDPHS_BA + 16 * LW_CFG_KB_SIZE,
        .flags  = IORESOURCE_MEM,
    },
    [2] = {
        .start  = VIC_UDPHS,                                            /*  中断号                      */
        .end    = VIC_UDPHS,
        .flags  = IORESOURCE_IRQ,
    }
};

四、platform_get_resource函数源码如下:

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 (type == resource_type(r) && num-- == 0)
            return r;
    }
    return NULL;
}

函数分析:

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

这行代码使得不管你是想获取哪一份资源都从第一份资源开始搜索。

if (type == resource_type(r) && num-- == 0)

这行代码首先通过type == resource_type(r)判断当前这份资源的类型是否匹配,如果匹配则再通过num-- == 0判断是否是你要的,如果不匹配重新提取下一份资源而不会执行num-- == 0这一句代码。

通过以上两步就能定位到你要找的资源了,接着把资源返回即可。如果都不匹配就返回NULL

 

五、驱动中通过下面代码拿到第一份资源:

struct resource *regs, *fifo;
regs = platform_get_resource(pdev, IORESOURCE_MEM, CTRL_IOMEM_ID);
fifo = platform_get_resource(pdev, IORESOURCE_MEM, FIFO_IOMEM_ID);


六:总结:

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

           unsigned int type决定资源的类型,unsigned int num决定type类型的第几份资源(从0开始)。即使同类型资源在资源数组中不是连续排放也可以定位得到该资源。

platform_get_resource(pdev,IORESOURCE_IRQ,CTRL_IOMEM_ID);可以定位第一份IORESOURCE_MEMCTRL资源;

platform_get_resource(pdev,IORESOURCE_IRQ,FIFO_IOMEM_ID);可以定位第二份IORESOURCE_MEMFIFO资源。

          之所以能定位到资源,在于函数实现中的这一行代码: if (type == resource_type(r) && num-- == 0)

该行代码,如果没有匹配资源类型,num-- == 0不会执行而重新提取下一份资源,只有资源匹配了才会寻找该类型的第几份资源,即使这些资源排放不连续。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值