0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/acpi.h>
0012 #include <linux/device.h>
0013 #include <linux/err.h>
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/dma-mapping.h>
0017 #include <linux/pci.h>
0018 #include <linux/platform_device.h>
0019
0020 #include "internal.h"
0021
0022 static const struct acpi_device_id forbidden_id_list[] = {
0023 {"PNP0000", 0},
0024 {"PNP0100", 0},
0025 {"PNP0200", 0},
0026 {"ACPI0009", 0},
0027 {"ACPI000A", 0},
0028 {"SMB0001", 0},
0029 {"", 0},
0030 };
0031
0032 static struct platform_device *acpi_platform_device_find_by_companion(struct acpi_device *adev)
0033 {
0034 struct device *dev;
0035
0036 dev = bus_find_device_by_acpi_dev(&platform_bus_type, adev);
0037 return dev ? to_platform_device(dev) : NULL;
0038 }
0039
0040 static int acpi_platform_device_remove_notify(struct notifier_block *nb,
0041 unsigned long value, void *arg)
0042 {
0043 struct acpi_device *adev = arg;
0044 struct platform_device *pdev;
0045
0046 switch (value) {
0047 case ACPI_RECONFIG_DEVICE_ADD:
0048
0049 break;
0050 case ACPI_RECONFIG_DEVICE_REMOVE:
0051 if (!acpi_device_enumerated(adev))
0052 break;
0053
0054 pdev = acpi_platform_device_find_by_companion(adev);
0055 if (!pdev)
0056 break;
0057
0058 platform_device_unregister(pdev);
0059 put_device(&pdev->dev);
0060 break;
0061 }
0062
0063 return NOTIFY_OK;
0064 }
0065
0066 static struct notifier_block acpi_platform_notifier = {
0067 .notifier_call = acpi_platform_device_remove_notify,
0068 };
0069
0070 static void acpi_platform_fill_resource(struct acpi_device *adev,
0071 const struct resource *src, struct resource *dest)
0072 {
0073 struct device *parent;
0074
0075 *dest = *src;
0076
0077
0078
0079
0080
0081 parent = acpi_get_first_physical_node(adev->parent);
0082 if (parent && dev_is_pci(parent))
0083 dest->parent = pci_find_resource(to_pci_dev(parent), dest);
0084 }
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097 struct platform_device *acpi_create_platform_device(struct acpi_device *adev,
0098 const struct property_entry *properties)
0099 {
0100 struct platform_device *pdev = NULL;
0101 struct platform_device_info pdevinfo;
0102 struct resource_entry *rentry;
0103 struct list_head resource_list;
0104 struct resource *resources = NULL;
0105 int count;
0106
0107
0108 if (adev->physical_node_count)
0109 return NULL;
0110
0111 if (!acpi_match_device_ids(adev, forbidden_id_list))
0112 return ERR_PTR(-EINVAL);
0113
0114 INIT_LIST_HEAD(&resource_list);
0115 count = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
0116 if (count < 0) {
0117 return NULL;
0118 } else if (count > 0) {
0119 resources = kcalloc(count, sizeof(struct resource),
0120 GFP_KERNEL);
0121 if (!resources) {
0122 dev_err(&adev->dev, "No memory for resources\n");
0123 acpi_dev_free_resource_list(&resource_list);
0124 return ERR_PTR(-ENOMEM);
0125 }
0126 count = 0;
0127 list_for_each_entry(rentry, &resource_list, node)
0128 acpi_platform_fill_resource(adev, rentry->res,
0129 &resources[count++]);
0130
0131 acpi_dev_free_resource_list(&resource_list);
0132 }
0133
0134 memset(&pdevinfo, 0, sizeof(pdevinfo));
0135
0136
0137
0138
0139
0140 pdevinfo.parent = adev->parent ?
0141 acpi_get_first_physical_node(adev->parent) : NULL;
0142 pdevinfo.name = dev_name(&adev->dev);
0143 pdevinfo.id = -1;
0144 pdevinfo.res = resources;
0145 pdevinfo.num_res = count;
0146 pdevinfo.fwnode = acpi_fwnode_handle(adev);
0147 pdevinfo.properties = properties;
0148
0149 if (acpi_dma_supported(adev))
0150 pdevinfo.dma_mask = DMA_BIT_MASK(32);
0151 else
0152 pdevinfo.dma_mask = 0;
0153
0154 pdev = platform_device_register_full(&pdevinfo);
0155 if (IS_ERR(pdev))
0156 dev_err(&adev->dev, "platform device creation failed: %ld\n",
0157 PTR_ERR(pdev));
0158 else {
0159 set_dev_node(&pdev->dev, acpi_get_node(adev->handle));
0160 dev_dbg(&adev->dev, "created platform device %s\n",
0161 dev_name(&pdev->dev));
0162 }
0163
0164 kfree(resources);
0165
0166 return pdev;
0167 }
0168 EXPORT_SYMBOL_GPL(acpi_create_platform_device);
0169
0170 void __init acpi_platform_init(void)
0171 {
0172 acpi_reconfig_notifier_register(&acpi_platform_notifier);
0173 }