您的位置:首页 > 其它

platform_device_add()函数分析

2012-03-31 11:00 806 查看
int platform_device_add(struct platform_device *pdev)

{

int i, ret = 0;

if (!pdev) /*验证指针的有效性 */

return -EINVAL;

if (!pdev->dev.parent)/*都说总线有两个链表,一个是设备链表(通过device 内嵌)一个是驱动链表(通过device_driver内嵌)这里如果pdev->dev.parent为0,说明设备链表还没有设备,因此处理办法是将platform_bus作为设备链表的开始,一直感觉platform_bus和platform_bus_type很难区分,不过在这里清楚了platform_bus是一个设备,platform_bus_type才是真正的总线*/

pdev->dev.parent = &platform_bus;/*device 的父结点*/

pdev->dev.bus = &platform_bus_type;/*device 要挂接在platform_bus_type这个总线上拉,看到了,设备和总线是这么勾搭上滴,很直接,很干脆*/

if (pdev->id != -1)

snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%d", pdev->name,

pdev->id);/*这个如果看不懂,可以参考LINUX的格式化输出的相关资料*/

else

strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE);

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

struct resource *p, *r = &pdev->resource[i];

if (r->name == NULL) /*name一般为NULL*/

r->name = pdev->dev.bus_id; /*资源的名称赋值为pdev->dev.bus_id,如果一个platform_device有多个resource 则出现同名现象*/

p = r->parent;

if (!p) { /*父资源为0,说明不是从一个大的资源里面切割出来的*/

if (r->flags & IORESOURCE_MEM)

p = &iomem_resource;

else if (r->flags & IORESOURCE_IO)

p = &ioport_resource;

}

if (p && insert_resource(p, r)) { /*如果从父资源里面切割失败,则进行如下处理*/

printk(KERN_ERR

"%s: failed to claim resource %d/n",

pdev->dev.bus_id, i);

ret = -EBUSY;

goto failed;

}

}

pr_debug("Registering platform device '%s'. Parent at %s/n",

pdev->dev.bus_id, pdev->dev.parent->bus_id);

ret = device_add(&pdev->dev);/*资源也分配好了,准备工作也做足,终于可以把设备添加到设备链表里面了*/

if (ret == 0)

return ret;

failed: /*失败处理*/

while (--i >= 0)

if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO))

release_resource(&pdev->resource[i]);

return ret;

}

EXPORT_SYMBOL_GPL(platform_device_add);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: