您的位置:首页 > 其它

Platform_device的platform_get_resource分析移植

2017-12-06 17:07 375 查看
 
一、平台设备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,

4000
},
[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_MEM的CTRL资源;
platform_get_resource(pdev,IORESOURCE_IRQ,FIFO_IOMEM_ID);可以定位第二份IORESOURCE_MEM的FIFO资源。
          之所以能定位到资源,在于函数实现中的这一行代码: if (type == resource_type(r) && num-- == 0)
该行代码,如果没有匹配资源类型,num-- == 0不会执行而重新提取下一份资源,只有资源匹配了才会寻找该类型的第几份资源,即使这些资源排放不连续。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: