acpi_create_platform_device

acpi_create_platform_device 這個(gè)函數(shù)會(huì)為沒有platform_driver 自動(dòng)創(chuàng)建platform_device設(shè)備。只有當(dāng)用戶只調(diào)用register_platform_driver就可以匹配device和driver
其源碼分析如下:
struct platform_device *acpi_create_platform_device(struct acpi_device *adev,
                    struct property_entry *properties)
{
    struct platform_device *pdev = NULL;
    struct platform_device_info pdevinfo;
    struct resource_entry *rentry;
    struct list_head resource_list;
    struct resource *resources = NULL;
    int count;

    /* If the ACPI node already has a physical device attached, skip it. */
    #如果這個(gè)設(shè)備已經(jīng)有一個(gè)物理節(jié)點(diǎn)attach了,則退出。簡單說一個(gè)設(shè)備只能調(diào)用一次這個(gè)函數(shù)
    if (adev->physical_node_count)
        return NULL;
    #保存在forbidden_id_list 中的設(shè)備是禁止加載的。如果遇到這種設(shè)備則退出
    if (!acpi_match_device_ids(adev, forbidden_id_list))
        return ERR_PTR(-EINVAL);
    #初始化保存設(shè)備資源的鏈表
    INIT_LIST_HEAD(&resource_list);
    #獲取這個(gè)device的資源,如果count小于零說明獲取資源失敗,則退出
    count = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
    if (count < 0) {
        return NULL;
    } else if (count > 0) {
        #根據(jù)count數(shù)來申請(qǐng)內(nèi)存老保存資源
        resources = kzalloc(count * sizeof(struct resource),
                    GFP_KERNEL);
        if (!resources) {
            dev_err(&adev->dev, "No memory for resources\n");
            acpi_dev_free_resource_list(&resource_list);
            return ERR_PTR(-ENOMEM);
        }
        count = 0;
        #將所有的資源保存在resource中
        list_for_each_entry(rentry, &resource_list, node)
            acpi_platform_fill_resource(adev, rentry->res,
                            &resources[count++]);

        acpi_dev_free_resource_list(&resource_list);
    }
    #清空pdevinfo 結(jié)構(gòu)體
    memset(&pdevinfo, 0, sizeof(pdevinfo));
    /*
     * If the ACPI node has a parent and that parent has a physical device
     * attached to it, that physical device should be the parent of the
     * platform device we are about to create.
     */
     #給pdeinfo 賦值
    pdevinfo.parent = adev->parent ?
        acpi_get_first_physical_node(adev->parent) : NULL;
    pdevinfo.name = dev_name(&adev->dev);
    pdevinfo.id = -1;
    pdevinfo.res = resources;
    pdevinfo.num_res = count;
    pdevinfo.fwnode = acpi_fwnode_handle(adev);
    pdevinfo.properties = properties;
    #檢車這個(gè)device是否支持dma,如果支持的話,把dma mask設(shè)置為32bit
    if (acpi_dma_supported(adev))
        pdevinfo.dma_mask = DMA_BIT_MASK(32);
    else
        pdevinfo.dma_mask = 0;
    #前面的準(zhǔn)備工作結(jié)束后,調(diào)用下面這個(gè)函數(shù)把pdevinfo注冊(cè)給kernel
    #platform_device_register_full->platform_device_add->device_add 
    pdev = platform_device_register_full(&pdevinfo);
    if (IS_ERR(pdev))
        dev_err(&adev->dev, "platform device creation failed: %ld\n",
            PTR_ERR(pdev));
    else {
        set_dev_node(&pdev->dev, acpi_get_node(adev->handle));
        dev_dbg(&adev->dev, "created platform device %s\n",
            dev_name(&pdev->dev));
    }

    kfree(resources);

    return pdev;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容