2 * Link physical devices with ACPI devices support
4 * Copyright (c) 2005 David Shaohua Li <shaohua.li@intel.com>
5 * Copyright (c) 2005 Intel Corp.
7 * This file is released under the GPLv2.
9 #include <linux/init.h>
10 #include <linux/list.h>
11 #include <linux/device.h>
12 #include <linux/rwsem.h>
13 #include <linux/acpi.h>
15 #define ACPI_GLUE_DEBUG 0
17 #define DBG(x...) printk(PREFIX x)
19 #define DBG(x...) do { } while(0)
21 static LIST_HEAD(bus_type_list
);
22 static DECLARE_RWSEM(bus_type_sem
);
24 int register_acpi_bus_type(struct acpi_bus_type
*type
)
28 if (type
&& type
->bus
&& type
->find_device
) {
29 down_write(&bus_type_sem
);
30 list_add_tail(&type
->list
, &bus_type_list
);
31 up_write(&bus_type_sem
);
32 printk(KERN_INFO PREFIX
"bus type %s registered\n",
39 int unregister_acpi_bus_type(struct acpi_bus_type
*type
)
44 down_write(&bus_type_sem
);
45 list_del_init(&type
->list
);
46 up_write(&bus_type_sem
);
47 printk(KERN_INFO PREFIX
"ACPI bus type %s unregistered\n",
54 static struct acpi_bus_type
*acpi_get_bus_type(struct bus_type
*type
)
56 struct acpi_bus_type
*tmp
, *ret
= NULL
;
58 down_read(&bus_type_sem
);
59 list_for_each_entry(tmp
, &bus_type_list
, list
) {
60 if (tmp
->bus
== type
) {
65 up_read(&bus_type_sem
);
69 static int acpi_find_bridge_device(struct device
*dev
, acpi_handle
* handle
)
71 struct acpi_bus_type
*tmp
;
74 down_read(&bus_type_sem
);
75 list_for_each_entry(tmp
, &bus_type_list
, list
) {
76 if (tmp
->find_bridge
&& !tmp
->find_bridge(dev
, handle
)) {
81 up_read(&bus_type_sem
);
85 /* Get device's handler per its address under its parent */
86 struct acpi_find_child
{
92 do_acpi_find_child(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
95 struct acpi_device_info
*info
;
96 struct acpi_find_child
*find
= context
;
98 status
= acpi_get_object_info(handle
, &info
);
99 if (ACPI_SUCCESS(status
)) {
100 if (info
->address
== find
->address
)
101 find
->handle
= handle
;
107 acpi_handle
acpi_get_child(acpi_handle parent
, acpi_integer address
)
109 struct acpi_find_child find
= { NULL
, address
};
113 acpi_walk_namespace(ACPI_TYPE_DEVICE
, parent
,
114 1, do_acpi_find_child
, &find
, NULL
);
118 EXPORT_SYMBOL(acpi_get_child
);
120 /* Link ACPI devices with physical devices */
121 static void acpi_glue_data_handler(acpi_handle handle
,
122 u32 function
, void *context
)
124 /* we provide an empty handler */
127 /* Note: a success call will increase reference count by one */
128 struct device
*acpi_get_physical_device(acpi_handle handle
)
133 status
= acpi_get_data(handle
, acpi_glue_data_handler
, (void **)&dev
);
134 if (ACPI_SUCCESS(status
))
135 return get_device(dev
);
139 EXPORT_SYMBOL(acpi_get_physical_device
);
141 static int acpi_bind_one(struct device
*dev
, acpi_handle handle
)
143 struct acpi_device
*acpi_dev
;
146 if (dev
->archdata
.acpi_handle
) {
147 dev_warn(dev
, "Drivers changed 'acpi_handle'\n");
151 status
= acpi_attach_data(handle
, acpi_glue_data_handler
, dev
);
152 if (ACPI_FAILURE(status
)) {
156 dev
->archdata
.acpi_handle
= handle
;
158 status
= acpi_bus_get_device(handle
, &acpi_dev
);
159 if (!ACPI_FAILURE(status
)) {
162 ret
= sysfs_create_link(&dev
->kobj
, &acpi_dev
->dev
.kobj
,
164 ret
= sysfs_create_link(&acpi_dev
->dev
.kobj
, &dev
->kobj
,
166 if (acpi_dev
->wakeup
.flags
.valid
) {
167 device_set_wakeup_capable(dev
, true);
168 device_set_wakeup_enable(dev
,
169 acpi_dev
->wakeup
.state
.enabled
);
176 static int acpi_unbind_one(struct device
*dev
)
178 if (!dev
->archdata
.acpi_handle
)
180 if (dev
== acpi_get_physical_device(dev
->archdata
.acpi_handle
)) {
181 struct acpi_device
*acpi_dev
;
183 /* acpi_get_physical_device increase refcnt by one */
186 if (!acpi_bus_get_device(dev
->archdata
.acpi_handle
,
188 sysfs_remove_link(&dev
->kobj
, "firmware_node");
189 sysfs_remove_link(&acpi_dev
->dev
.kobj
, "physical_node");
192 acpi_detach_data(dev
->archdata
.acpi_handle
,
193 acpi_glue_data_handler
);
194 dev
->archdata
.acpi_handle
= NULL
;
195 /* acpi_bind_one increase refcnt by one */
198 dev_err(dev
, "Oops, 'acpi_handle' corrupt\n");
203 static int acpi_platform_notify(struct device
*dev
)
205 struct acpi_bus_type
*type
;
209 if (!dev
->bus
|| !dev
->parent
) {
210 /* bridge devices genernally haven't bus or parent */
211 ret
= acpi_find_bridge_device(dev
, &handle
);
214 type
= acpi_get_bus_type(dev
->bus
);
216 DBG("No ACPI bus support for %s\n", dev_name(dev
));
220 if ((ret
= type
->find_device(dev
, &handle
)) != 0)
221 DBG("Can't get handler for %s\n", dev_name(dev
));
224 acpi_bind_one(dev
, handle
);
228 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
230 acpi_get_name(dev
->archdata
.acpi_handle
,
231 ACPI_FULL_PATHNAME
, &buffer
);
232 DBG("Device %s -> %s\n", dev_name(dev
), (char *)buffer
.pointer
);
233 kfree(buffer
.pointer
);
235 DBG("Device %s -> No ACPI support\n", dev_name(dev
));
241 static int acpi_platform_notify_remove(struct device
*dev
)
243 acpi_unbind_one(dev
);
247 int __init
init_acpi_device_notify(void)
249 if (platform_notify
|| platform_notify_remove
) {
250 printk(KERN_ERR PREFIX
"Can't use platform_notify\n");
253 platform_notify
= acpi_platform_notify
;
254 platform_notify_remove
= acpi_platform_notify_remove
;