注册:函数调用
以音频设备注册为例,音频声卡初始化
代码目录:kernel/sound/soc/xxxx.c
static int __init audio_card_init(void) { int ret =0; xxxx_snd_device = platform_device_alloc("soc-audio", -1); if (!xxxx_snd_device) { ret = -ENOMEM; return ret; } platform_set_drvdata(xxxx_snd_device, &xxxx_snd_devdata); xxxx_snd_devdata.dev = &xxxx_snd_device->dev; ret = platform_device_add(xxxx_snd_device); if (ret) { platform_device_put(rk2818_snd_device); } return ret; }
平台注册
代码目录:kernel/drivers/base/platform.c
/** * platform_device_add - add a platform device to device hierarchy * @pdev: platform device we're adding * * This is part 2 of platform_device_register(), though may be called * separately _iff_ pdev was allocated by platform_device_alloc(). */ int platform_device_add(struct platform_device *pdev) { int i, ret = 0; if (!pdev) return -EINVAL; if (!pdev->dev.parent) pdev->dev.parent = &platform_bus; pdev->dev.bus = &platform_bus_type; if (pdev->id != -1) dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id); else dev_set_name(&pdev->dev, "%s", pdev->name); for (i = 0; i < pdev->num_resources; i++) { struct resource *p, *r = &pdev->resource[i]; if (r->name == NULL) r->name = dev_name(&pdev->dev); p = r->parent; if (!p) { if (resource_type(r) == IORESOURCE_MEM) p = &iomem_resource; else if (resource_type(r) == IORESOURCE_IO) p = &ioport_resource; } if (p && insert_resource(p, r)) { printk(KERN_ERR "%s: failed to claim resource %d\n", dev_name(&pdev->dev), i); ret = -EBUSY; goto failed; } } pr_debug("Registering platform device '%s'. Parent at %s\n", dev_name(&pdev->dev), dev_name(pdev->dev.parent)); ret = device_add(&pdev->dev); if (ret == 0) return ret; failed: while (--i >= 0) { struct resource *r = &pdev->resource[i]; unsigned long type = resource_type(r); if (type == IORESOURCE_MEM || type == IORESOURCE_IO) release_resource(r); } return ret; } EXPORT_SYMBOL_GPL(platform_device_add);
设备添加
代码目录:kernel/drivers/base/core.c