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_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
97 struct acpi_find_child *find = context;
99 status = acpi_get_object_info(handle, &buffer);
100 if (ACPI_SUCCESS(status)) {
101 info = buffer.pointer;
102 if (info->address == find->address)
103 find->handle = handle;
104 kfree(buffer.pointer);
109 acpi_handle acpi_get_child(acpi_handle parent, acpi_integer address)
111 struct acpi_find_child find = { NULL, address };
115 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
116 1, do_acpi_find_child, &find, NULL);
120 EXPORT_SYMBOL(acpi_get_child);
122 /* Link ACPI devices with physical devices */
123 static void acpi_glue_data_handler(acpi_handle handle,
124 u32 function, void *context)
126 /* we provide an empty handler */
129 /* Note: a success call will increase reference count by one */
130 struct device *acpi_get_physical_device(acpi_handle handle)
135 status = acpi_get_data(handle, acpi_glue_data_handler, (void **)&dev);
136 if (ACPI_SUCCESS(status))
137 return get_device(dev);
141 EXPORT_SYMBOL(acpi_get_physical_device);
143 static int acpi_bind_one(struct device *dev, acpi_handle handle)
145 struct acpi_device *acpi_dev;
148 if (dev->archdata.acpi_handle) {
149 printk(KERN_WARNING PREFIX
150 "Drivers changed 'acpi_handle' for %s\n", dev->bus_id);
154 status = acpi_attach_data(handle, acpi_glue_data_handler, dev);
155 if (ACPI_FAILURE(status)) {
159 dev->archdata.acpi_handle = handle;
161 status = acpi_bus_get_device(handle, &acpi_dev);
162 if (!ACPI_FAILURE(status)) {
165 ret = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
167 ret = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
174 static int acpi_unbind_one(struct device *dev)
176 if (!dev->archdata.acpi_handle)
178 if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) {
179 struct acpi_device *acpi_dev;
181 /* acpi_get_physical_device increase refcnt by one */
184 if (!acpi_bus_get_device(dev->archdata.acpi_handle,
186 sysfs_remove_link(&dev->kobj, "firmware_node");
187 sysfs_remove_link(&acpi_dev->dev.kobj, "physical_node");
190 acpi_detach_data(dev->archdata.acpi_handle,
191 acpi_glue_data_handler);
192 dev->archdata.acpi_handle = NULL;
193 /* acpi_bind_one increase refcnt by one */
196 printk(KERN_ERR PREFIX
197 "Oops, 'acpi_handle' corrupt for %s\n", dev->bus_id);
202 static int acpi_platform_notify(struct device *dev)
204 struct acpi_bus_type *type;
208 if (!dev->bus || !dev->parent) {
209 /* bridge devices genernally haven't bus or parent */
210 ret = acpi_find_bridge_device(dev, &handle);
213 type = acpi_get_bus_type(dev->bus);
215 DBG("No ACPI bus support for %s\n", dev->bus_id);
219 if ((ret = type->find_device(dev, &handle)) != 0)
220 DBG("Can't get handler for %s\n", dev->bus_id);
223 acpi_bind_one(dev, handle);
227 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
229 acpi_get_name(dev->archdata.acpi_handle,
230 ACPI_FULL_PATHNAME, &buffer);
231 DBG("Device %s -> %s\n", dev->bus_id, (char *)buffer.pointer);
232 kfree(buffer.pointer);
234 DBG("Device %s -> No ACPI support\n", dev->bus_id);
240 static int acpi_platform_notify_remove(struct device *dev)
242 acpi_unbind_one(dev);
246 static int __init init_acpi_device_notify(void)
250 if (platform_notify || platform_notify_remove) {
251 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
254 platform_notify = acpi_platform_notify;
255 platform_notify_remove = acpi_platform_notify_remove;
259 arch_initcall(init_acpi_device_notify);
262 #if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE)
265 static u32 rtc_handler(void *context)
267 acpi_clear_event(ACPI_EVENT_RTC);
268 acpi_disable_event(ACPI_EVENT_RTC, 0);
269 return ACPI_INTERRUPT_HANDLED;
272 static inline void rtc_wake_setup(void)
274 acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
276 * After the RTC handler is installed, the Fixed_RTC event should
277 * be disabled. Only when the RTC alarm is set will it be enabled.
279 acpi_clear_event(ACPI_EVENT_RTC);
280 acpi_disable_event(ACPI_EVENT_RTC, 0);
283 static void rtc_wake_on(struct device *dev)
285 acpi_clear_event(ACPI_EVENT_RTC);
286 acpi_enable_event(ACPI_EVENT_RTC, 0);
289 static void rtc_wake_off(struct device *dev)
291 acpi_disable_event(ACPI_EVENT_RTC, 0);
294 #define rtc_wake_setup() do{}while(0)
295 #define rtc_wake_on NULL
296 #define rtc_wake_off NULL
299 /* Every ACPI platform has a mc146818 compatible "cmos rtc". Here we find
300 * its device node and pass extra config data. This helps its driver use
301 * capabilities that the now-obsolete mc146818 didn't have, and informs it
302 * that this board's RTC is wakeup-capable (per ACPI spec).
304 #include <linux/mc146818rtc.h>
306 static struct cmos_rtc_board_info rtc_info;
309 /* PNP devices are registered in a subsys_initcall();
310 * ACPI specifies the PNP IDs to use.
312 #include <linux/pnp.h>
314 static int __init pnp_match(struct device *dev, void *data)
316 static const char *ids[] = { "PNP0b00", "PNP0b01", "PNP0b02", };
317 struct pnp_dev *pnp = to_pnp_dev(dev);
320 for (i = 0; i < ARRAY_SIZE(ids); i++) {
321 if (compare_pnp_id(pnp->id, ids[i]) != 0)
327 static struct device *__init get_rtc_dev(void)
329 return bus_find_device(&pnp_bus_type, NULL, NULL, pnp_match);
332 static int __init acpi_rtc_init(void)
334 struct device *dev = get_rtc_dev();
341 rtc_info.wake_on = rtc_wake_on;
342 rtc_info.wake_off = rtc_wake_off;
344 /* workaround bug in some ACPI tables */
345 if (acpi_gbl_FADT.month_alarm && !acpi_gbl_FADT.day_alarm) {
346 DBG("bogus FADT month_alarm\n");
347 acpi_gbl_FADT.month_alarm = 0;
350 rtc_info.rtc_day_alarm = acpi_gbl_FADT.day_alarm;
351 rtc_info.rtc_mon_alarm = acpi_gbl_FADT.month_alarm;
352 rtc_info.rtc_century = acpi_gbl_FADT.century;
354 /* NOTE: S4_RTC_WAKE is NOT currently useful to Linux */
355 if (acpi_gbl_FADT.flags & ACPI_FADT_S4_RTC_WAKE)
356 printk(PREFIX "RTC can wake from S4\n");
359 dev->platform_data = &rtc_info;
361 /* RTC always wakes from S1/S2/S3, and often S4/STD */
362 device_init_wakeup(dev, 1);
366 DBG("RTC unavailable?\n");
369 /* do this between RTC subsys_initcall() and rtc_cmos driver_initcall() */
370 fs_initcall(acpi_rtc_init);