blob: eda0978b57c6946b9580ab24869794a2bc14b2bd [file] [log] [blame]
David Shaohua Li4e10d122005-03-18 18:45:35 -05001/*
2 * Link physical devices with ACPI devices support
3 *
4 * Copyright (c) 2005 David Shaohua Li <shaohua.li@intel.com>
5 * Copyright (c) 2005 Intel Corp.
6 *
7 * This file is released under the GPLv2.
8 */
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>
14
15#define ACPI_GLUE_DEBUG 0
16#if ACPI_GLUE_DEBUG
17#define DBG(x...) printk(PREFIX x)
18#else
Dave Jones4ebf83c2007-07-09 11:33:14 -070019#define DBG(x...) do { } while(0)
David Shaohua Li4e10d122005-03-18 18:45:35 -050020#endif
21static LIST_HEAD(bus_type_list);
22static DECLARE_RWSEM(bus_type_sem);
23
24int register_acpi_bus_type(struct acpi_bus_type *type)
25{
26 if (acpi_disabled)
27 return -ENODEV;
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);
Len Brown4be44fc2005-08-05 00:44:28 -040032 printk(KERN_INFO PREFIX "bus type %s registered\n",
33 type->bus->name);
David Shaohua Li4e10d122005-03-18 18:45:35 -050034 return 0;
35 }
36 return -ENODEV;
37}
38
David Shaohua Li4e10d122005-03-18 18:45:35 -050039int unregister_acpi_bus_type(struct acpi_bus_type *type)
40{
41 if (acpi_disabled)
42 return 0;
43 if (type) {
44 down_write(&bus_type_sem);
45 list_del_init(&type->list);
46 up_write(&bus_type_sem);
Len Brown4be44fc2005-08-05 00:44:28 -040047 printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n",
48 type->bus->name);
David Shaohua Li4e10d122005-03-18 18:45:35 -050049 return 0;
50 }
51 return -ENODEV;
52}
53
David Shaohua Li4e10d122005-03-18 18:45:35 -050054static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
55{
56 struct acpi_bus_type *tmp, *ret = NULL;
57
58 down_read(&bus_type_sem);
59 list_for_each_entry(tmp, &bus_type_list, list) {
60 if (tmp->bus == type) {
61 ret = tmp;
62 break;
63 }
64 }
65 up_read(&bus_type_sem);
66 return ret;
67}
68
69static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
70{
71 struct acpi_bus_type *tmp;
72 int ret = -ENODEV;
73
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)) {
77 ret = 0;
78 break;
79 }
80 }
81 up_read(&bus_type_sem);
82 return ret;
83}
84
David Shaohua Li4e10d122005-03-18 18:45:35 -050085/* Get device's handler per its address under its parent */
86struct acpi_find_child {
87 acpi_handle handle;
88 acpi_integer address;
89};
90
91static acpi_status
92do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
93{
94 acpi_status status;
95 struct acpi_device_info *info;
96 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Jan Engelhardt50dd0962006-10-01 00:28:50 +020097 struct acpi_find_child *find = context;
David Shaohua Li4e10d122005-03-18 18:45:35 -050098
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;
Len Brown02438d82006-06-30 03:19:10 -0400104 kfree(buffer.pointer);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500105 }
106 return AE_OK;
107}
108
109acpi_handle acpi_get_child(acpi_handle parent, acpi_integer address)
110{
111 struct acpi_find_child find = { NULL, address };
112
113 if (!parent)
114 return NULL;
115 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
116 1, do_acpi_find_child, &find, NULL);
117 return find.handle;
118}
119
120EXPORT_SYMBOL(acpi_get_child);
121
122/* Link ACPI devices with physical devices */
123static void acpi_glue_data_handler(acpi_handle handle,
124 u32 function, void *context)
125{
126 /* we provide an empty handler */
127}
128
129/* Note: a success call will increase reference count by one */
130struct device *acpi_get_physical_device(acpi_handle handle)
131{
132 acpi_status status;
133 struct device *dev;
134
135 status = acpi_get_data(handle, acpi_glue_data_handler, (void **)&dev);
136 if (ACPI_SUCCESS(status))
137 return get_device(dev);
138 return NULL;
139}
140
141EXPORT_SYMBOL(acpi_get_physical_device);
142
143static int acpi_bind_one(struct device *dev, acpi_handle handle)
144{
145 acpi_status status;
146
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100147 if (dev->archdata.acpi_handle) {
David Shaohua Li4e10d122005-03-18 18:45:35 -0500148 printk(KERN_WARNING PREFIX
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100149 "Drivers changed 'acpi_handle' for %s\n", dev->bus_id);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500150 return -EINVAL;
151 }
152 get_device(dev);
153 status = acpi_attach_data(handle, acpi_glue_data_handler, dev);
154 if (ACPI_FAILURE(status)) {
155 put_device(dev);
156 return -EINVAL;
157 }
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100158 dev->archdata.acpi_handle = handle;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500159
160 return 0;
161}
162
163static int acpi_unbind_one(struct device *dev)
164{
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100165 if (!dev->archdata.acpi_handle)
David Shaohua Li4e10d122005-03-18 18:45:35 -0500166 return 0;
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100167 if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) {
David Shaohua Li4e10d122005-03-18 18:45:35 -0500168 /* acpi_get_physical_device increase refcnt by one */
169 put_device(dev);
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100170 acpi_detach_data(dev->archdata.acpi_handle,
171 acpi_glue_data_handler);
172 dev->archdata.acpi_handle = NULL;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500173 /* acpi_bind_one increase refcnt by one */
174 put_device(dev);
175 } else {
176 printk(KERN_ERR PREFIX
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100177 "Oops, 'acpi_handle' corrupt for %s\n", dev->bus_id);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500178 }
179 return 0;
180}
181
182static int acpi_platform_notify(struct device *dev)
183{
184 struct acpi_bus_type *type;
185 acpi_handle handle;
186 int ret = -EINVAL;
187
188 if (!dev->bus || !dev->parent) {
189 /* bridge devices genernally haven't bus or parent */
190 ret = acpi_find_bridge_device(dev, &handle);
191 goto end;
192 }
193 type = acpi_get_bus_type(dev->bus);
194 if (!type) {
David Shaohua Lief7b06c2005-04-18 22:59:23 -0400195 DBG("No ACPI bus support for %s\n", dev->bus_id);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500196 ret = -EINVAL;
197 goto end;
198 }
199 if ((ret = type->find_device(dev, &handle)) != 0)
David Shaohua Lief7b06c2005-04-18 22:59:23 -0400200 DBG("Can't get handler for %s\n", dev->bus_id);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500201 end:
202 if (!ret)
203 acpi_bind_one(dev, handle);
204
205#if ACPI_GLUE_DEBUG
206 if (!ret) {
207 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
208
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100209 acpi_get_name(dev->archdata.acpi_handle,
210 ACPI_FULL_PATHNAME, &buffer);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500211 DBG("Device %s -> %s\n", dev->bus_id, (char *)buffer.pointer);
Len Brown02438d82006-06-30 03:19:10 -0400212 kfree(buffer.pointer);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500213 } else
214 DBG("Device %s -> No ACPI support\n", dev->bus_id);
215#endif
216
217 return ret;
218}
219
220static int acpi_platform_notify_remove(struct device *dev)
221{
222 acpi_unbind_one(dev);
223 return 0;
224}
225
226static int __init init_acpi_device_notify(void)
227{
228 if (acpi_disabled)
229 return 0;
230 if (platform_notify || platform_notify_remove) {
231 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
232 return 0;
233 }
234 platform_notify = acpi_platform_notify;
235 platform_notify_remove = acpi_platform_notify_remove;
236 return 0;
237}
238
239arch_initcall(init_acpi_device_notify);
David Brownella74388e2007-02-05 16:09:11 -0800240
241
242#if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE)
243
David Brownellf5f72b42007-05-08 00:34:02 -0700244#ifdef CONFIG_PM
245static u32 rtc_handler(void *context)
246{
247 acpi_clear_event(ACPI_EVENT_RTC);
248 acpi_disable_event(ACPI_EVENT_RTC, 0);
249 return ACPI_INTERRUPT_HANDLED;
250}
251
252static inline void rtc_wake_setup(void)
253{
254 acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
255}
256
257static void rtc_wake_on(struct device *dev)
258{
259 acpi_clear_event(ACPI_EVENT_RTC);
260 acpi_enable_event(ACPI_EVENT_RTC, 0);
261}
262
263static void rtc_wake_off(struct device *dev)
264{
265 acpi_disable_event(ACPI_EVENT_RTC, 0);
266}
267#else
268#define rtc_wake_setup() do{}while(0)
269#define rtc_wake_on NULL
270#define rtc_wake_off NULL
271#endif
272
David Brownella74388e2007-02-05 16:09:11 -0800273/* Every ACPI platform has a mc146818 compatible "cmos rtc". Here we find
274 * its device node and pass extra config data. This helps its driver use
275 * capabilities that the now-obsolete mc146818 didn't have, and informs it
276 * that this board's RTC is wakeup-capable (per ACPI spec).
277 */
278#include <linux/mc146818rtc.h>
279
280static struct cmos_rtc_board_info rtc_info;
281
282
David Brownella74388e2007-02-05 16:09:11 -0800283/* PNP devices are registered in a subsys_initcall();
284 * ACPI specifies the PNP IDs to use.
285 */
286#include <linux/pnp.h>
287
288static int __init pnp_match(struct device *dev, void *data)
289{
290 static const char *ids[] = { "PNP0b00", "PNP0b01", "PNP0b02", };
291 struct pnp_dev *pnp = to_pnp_dev(dev);
292 int i;
293
294 for (i = 0; i < ARRAY_SIZE(ids); i++) {
295 if (compare_pnp_id(pnp->id, ids[i]) != 0)
296 return 1;
297 }
298 return 0;
299}
300
301static struct device *__init get_rtc_dev(void)
302{
303 return bus_find_device(&pnp_bus_type, NULL, NULL, pnp_match);
304}
305
David Brownella74388e2007-02-05 16:09:11 -0800306static int __init acpi_rtc_init(void)
307{
308 struct device *dev = get_rtc_dev();
309
310 if (dev) {
David Brownellf5f72b42007-05-08 00:34:02 -0700311 rtc_wake_setup();
312 rtc_info.wake_on = rtc_wake_on;
313 rtc_info.wake_off = rtc_wake_off;
314
David Brownell19bfe372007-05-08 00:34:03 -0700315 /* workaround bug in some ACPI tables */
316 if (acpi_gbl_FADT.month_alarm && !acpi_gbl_FADT.day_alarm) {
317 DBG("bogus FADT month_alarm\n");
318 acpi_gbl_FADT.month_alarm = 0;
319 }
320
David Brownella74388e2007-02-05 16:09:11 -0800321 rtc_info.rtc_day_alarm = acpi_gbl_FADT.day_alarm;
322 rtc_info.rtc_mon_alarm = acpi_gbl_FADT.month_alarm;
323 rtc_info.rtc_century = acpi_gbl_FADT.century;
324
David Brownellf5f72b42007-05-08 00:34:02 -0700325 /* NOTE: S4_RTC_WAKE is NOT currently useful to Linux */
326 if (acpi_gbl_FADT.flags & ACPI_FADT_S4_RTC_WAKE)
David Brownell19bfe372007-05-08 00:34:03 -0700327 printk(PREFIX "RTC can wake from S4\n");
328
David Brownella74388e2007-02-05 16:09:11 -0800329
330 dev->platform_data = &rtc_info;
331
332 /* RTC always wakes from S1/S2/S3, and often S4/STD */
333 device_init_wakeup(dev, 1);
334
335 put_device(dev);
336 } else
David Brownell19bfe372007-05-08 00:34:03 -0700337 DBG("RTC unavailable?\n");
David Brownella74388e2007-02-05 16:09:11 -0800338 return 0;
339}
340/* do this between RTC subsys_initcall() and rtc_cmos driver_initcall() */
341fs_initcall(acpi_rtc_init);
342
343#endif