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)
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 EXPORT_SYMBOL(register_acpi_bus_type);
41 int unregister_acpi_bus_type(struct acpi_bus_type *type)
46 down_write(&bus_type_sem);
47 list_del_init(&type->list);
48 up_write(&bus_type_sem);
49 printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n",
56 EXPORT_SYMBOL(unregister_acpi_bus_type);
58 static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
60 struct acpi_bus_type *tmp, *ret = NULL;
62 down_read(&bus_type_sem);
63 list_for_each_entry(tmp, &bus_type_list, list) {
64 if (tmp->bus == type) {
69 up_read(&bus_type_sem);
73 static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
75 struct acpi_bus_type *tmp;
78 down_read(&bus_type_sem);
79 list_for_each_entry(tmp, &bus_type_list, list) {
80 if (tmp->find_bridge && !tmp->find_bridge(dev, handle)) {
85 up_read(&bus_type_sem);
89 /* Get device's handler per its address under its parent */
90 struct acpi_find_child {
96 do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
99 struct acpi_device_info *info;
100 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
101 struct acpi_find_child *find = context;
103 status = acpi_get_object_info(handle, &buffer);
104 if (ACPI_SUCCESS(status)) {
105 info = buffer.pointer;
106 if (info->address == find->address)
107 find->handle = handle;
108 kfree(buffer.pointer);
113 acpi_handle acpi_get_child(acpi_handle parent, acpi_integer address)
115 struct acpi_find_child find = { NULL, address };
119 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
120 1, do_acpi_find_child, &find, NULL);
124 EXPORT_SYMBOL(acpi_get_child);
126 /* Link ACPI devices with physical devices */
127 static void acpi_glue_data_handler(acpi_handle handle,
128 u32 function, void *context)
130 /* we provide an empty handler */
133 /* Note: a success call will increase reference count by one */
134 struct device *acpi_get_physical_device(acpi_handle handle)
139 status = acpi_get_data(handle, acpi_glue_data_handler, (void **)&dev);
140 if (ACPI_SUCCESS(status))
141 return get_device(dev);
145 EXPORT_SYMBOL(acpi_get_physical_device);
147 static int acpi_bind_one(struct device *dev, acpi_handle handle)
151 if (dev->archdata.acpi_handle) {
152 printk(KERN_WARNING PREFIX
153 "Drivers changed 'acpi_handle' for %s\n", dev->bus_id);
157 status = acpi_attach_data(handle, acpi_glue_data_handler, dev);
158 if (ACPI_FAILURE(status)) {
162 dev->archdata.acpi_handle = handle;
167 static int acpi_unbind_one(struct device *dev)
169 if (!dev->archdata.acpi_handle)
171 if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) {
172 /* acpi_get_physical_device increase refcnt by one */
174 acpi_detach_data(dev->archdata.acpi_handle,
175 acpi_glue_data_handler);
176 dev->archdata.acpi_handle = NULL;
177 /* acpi_bind_one increase refcnt by one */
180 printk(KERN_ERR PREFIX
181 "Oops, 'acpi_handle' corrupt for %s\n", dev->bus_id);
186 static int acpi_platform_notify(struct device *dev)
188 struct acpi_bus_type *type;
192 if (!dev->bus || !dev->parent) {
193 /* bridge devices genernally haven't bus or parent */
194 ret = acpi_find_bridge_device(dev, &handle);
197 type = acpi_get_bus_type(dev->bus);
199 DBG("No ACPI bus support for %s\n", dev->bus_id);
203 if ((ret = type->find_device(dev, &handle)) != 0)
204 DBG("Can't get handler for %s\n", dev->bus_id);
207 acpi_bind_one(dev, handle);
211 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
213 acpi_get_name(dev->archdata.acpi_handle,
214 ACPI_FULL_PATHNAME, &buffer);
215 DBG("Device %s -> %s\n", dev->bus_id, (char *)buffer.pointer);
216 kfree(buffer.pointer);
218 DBG("Device %s -> No ACPI support\n", dev->bus_id);
224 static int acpi_platform_notify_remove(struct device *dev)
226 acpi_unbind_one(dev);
230 static int __init init_acpi_device_notify(void)
234 if (platform_notify || platform_notify_remove) {
235 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
238 platform_notify = acpi_platform_notify;
239 platform_notify_remove = acpi_platform_notify_remove;
243 arch_initcall(init_acpi_device_notify);
246 #if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE)
248 /* Every ACPI platform has a mc146818 compatible "cmos rtc". Here we find
249 * its device node and pass extra config data. This helps its driver use
250 * capabilities that the now-obsolete mc146818 didn't have, and informs it
251 * that this board's RTC is wakeup-capable (per ACPI spec).
253 #include <linux/mc146818rtc.h>
255 static struct cmos_rtc_board_info rtc_info;
258 /* PNP devices are registered in a subsys_initcall();
259 * ACPI specifies the PNP IDs to use.
261 #include <linux/pnp.h>
263 static int __init pnp_match(struct device *dev, void *data)
265 static const char *ids[] = { "PNP0b00", "PNP0b01", "PNP0b02", };
266 struct pnp_dev *pnp = to_pnp_dev(dev);
269 for (i = 0; i < ARRAY_SIZE(ids); i++) {
270 if (compare_pnp_id(pnp->id, ids[i]) != 0)
276 static struct device *__init get_rtc_dev(void)
278 return bus_find_device(&pnp_bus_type, NULL, NULL, pnp_match);
281 static int __init acpi_rtc_init(void)
283 struct device *dev = get_rtc_dev();
286 rtc_info.rtc_day_alarm = acpi_gbl_FADT.day_alarm;
287 rtc_info.rtc_mon_alarm = acpi_gbl_FADT.month_alarm;
288 rtc_info.rtc_century = acpi_gbl_FADT.century;
290 /* NOTE: acpi_gbl_FADT->rtcs4 is NOT currently useful */
292 dev->platform_data = &rtc_info;
294 /* RTC always wakes from S1/S2/S3, and often S4/STD */
295 device_init_wakeup(dev, 1);
299 pr_debug("ACPI: RTC unavailable?\n");
302 /* do this between RTC subsys_initcall() and rtc_cmos driver_initcall() */
303 fs_initcall(acpi_rtc_init);