Commit | Line | Data |
---|---|---|
4e10d12a DSL |
1 | /* |
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 | |
4ebf83c8 | 19 | #define DBG(x...) do { } while(0) |
4e10d12a DSL |
20 | #endif |
21 | static LIST_HEAD(bus_type_list); | |
22 | static DECLARE_RWSEM(bus_type_sem); | |
23 | ||
24 | int 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); | |
4be44fcd LB |
32 | printk(KERN_INFO PREFIX "bus type %s registered\n", |
33 | type->bus->name); | |
4e10d12a DSL |
34 | return 0; |
35 | } | |
36 | return -ENODEV; | |
37 | } | |
38 | ||
4e10d12a DSL |
39 | int 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); | |
4be44fcd LB |
47 | printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n", |
48 | type->bus->name); | |
4e10d12a DSL |
49 | return 0; |
50 | } | |
51 | return -ENODEV; | |
52 | } | |
53 | ||
4e10d12a DSL |
54 | static 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 | ||
69 | static 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 | ||
4e10d12a DSL |
85 | /* Get device's handler per its address under its parent */ |
86 | struct acpi_find_child { | |
87 | acpi_handle handle; | |
88 | acpi_integer address; | |
89 | }; | |
90 | ||
91 | static acpi_status | |
92 | do_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 }; | |
50dd0969 | 97 | struct acpi_find_child *find = context; |
4e10d12a DSL |
98 | |
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; | |
02438d87 | 104 | kfree(buffer.pointer); |
4e10d12a DSL |
105 | } |
106 | return AE_OK; | |
107 | } | |
108 | ||
109 | acpi_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 | ||
120 | EXPORT_SYMBOL(acpi_get_child); | |
121 | ||
122 | /* Link ACPI devices with physical devices */ | |
123 | static 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 */ | |
130 | struct 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 | ||
141 | EXPORT_SYMBOL(acpi_get_physical_device); | |
142 | ||
143 | static int acpi_bind_one(struct device *dev, acpi_handle handle) | |
144 | { | |
1071695f | 145 | struct acpi_device *acpi_dev; |
4e10d12a DSL |
146 | acpi_status status; |
147 | ||
465ae641 | 148 | if (dev->archdata.acpi_handle) { |
fc3a8828 | 149 | dev_warn(dev, "Drivers changed 'acpi_handle'\n"); |
4e10d12a DSL |
150 | 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 | } | |
465ae641 | 158 | dev->archdata.acpi_handle = handle; |
4e10d12a | 159 | |
1071695f DB |
160 | status = acpi_bus_get_device(handle, &acpi_dev); |
161 | if (!ACPI_FAILURE(status)) { | |
162 | int ret; | |
163 | ||
164 | ret = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj, | |
165 | "firmware_node"); | |
166 | ret = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj, | |
167 | "physical_node"); | |
76acae04 | 168 | if (acpi_dev->wakeup.flags.valid) { |
eb9d0fe4 | 169 | device_set_wakeup_capable(dev, true); |
76acae04 RW |
170 | device_set_wakeup_enable(dev, |
171 | acpi_dev->wakeup.state.enabled); | |
172 | } | |
1071695f DB |
173 | } |
174 | ||
4e10d12a DSL |
175 | return 0; |
176 | } | |
177 | ||
178 | static int acpi_unbind_one(struct device *dev) | |
179 | { | |
465ae641 | 180 | if (!dev->archdata.acpi_handle) |
4e10d12a | 181 | return 0; |
465ae641 | 182 | if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) { |
1071695f DB |
183 | struct acpi_device *acpi_dev; |
184 | ||
4e10d12a DSL |
185 | /* acpi_get_physical_device increase refcnt by one */ |
186 | put_device(dev); | |
1071695f DB |
187 | |
188 | if (!acpi_bus_get_device(dev->archdata.acpi_handle, | |
189 | &acpi_dev)) { | |
190 | sysfs_remove_link(&dev->kobj, "firmware_node"); | |
191 | sysfs_remove_link(&acpi_dev->dev.kobj, "physical_node"); | |
192 | } | |
193 | ||
465ae641 BH |
194 | acpi_detach_data(dev->archdata.acpi_handle, |
195 | acpi_glue_data_handler); | |
196 | dev->archdata.acpi_handle = NULL; | |
4e10d12a DSL |
197 | /* acpi_bind_one increase refcnt by one */ |
198 | put_device(dev); | |
199 | } else { | |
fc3a8828 | 200 | dev_err(dev, "Oops, 'acpi_handle' corrupt\n"); |
4e10d12a DSL |
201 | } |
202 | return 0; | |
203 | } | |
204 | ||
205 | static int acpi_platform_notify(struct device *dev) | |
206 | { | |
207 | struct acpi_bus_type *type; | |
208 | acpi_handle handle; | |
209 | int ret = -EINVAL; | |
210 | ||
211 | if (!dev->bus || !dev->parent) { | |
212 | /* bridge devices genernally haven't bus or parent */ | |
213 | ret = acpi_find_bridge_device(dev, &handle); | |
214 | goto end; | |
215 | } | |
216 | type = acpi_get_bus_type(dev->bus); | |
217 | if (!type) { | |
ef7b06cd | 218 | DBG("No ACPI bus support for %s\n", dev->bus_id); |
4e10d12a DSL |
219 | ret = -EINVAL; |
220 | goto end; | |
221 | } | |
222 | if ((ret = type->find_device(dev, &handle)) != 0) | |
ef7b06cd | 223 | DBG("Can't get handler for %s\n", dev->bus_id); |
4e10d12a DSL |
224 | end: |
225 | if (!ret) | |
226 | acpi_bind_one(dev, handle); | |
227 | ||
228 | #if ACPI_GLUE_DEBUG | |
229 | if (!ret) { | |
230 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
231 | ||
465ae641 BH |
232 | acpi_get_name(dev->archdata.acpi_handle, |
233 | ACPI_FULL_PATHNAME, &buffer); | |
4e10d12a | 234 | DBG("Device %s -> %s\n", dev->bus_id, (char *)buffer.pointer); |
02438d87 | 235 | kfree(buffer.pointer); |
4e10d12a DSL |
236 | } else |
237 | DBG("Device %s -> No ACPI support\n", dev->bus_id); | |
238 | #endif | |
239 | ||
240 | return ret; | |
241 | } | |
242 | ||
243 | static int acpi_platform_notify_remove(struct device *dev) | |
244 | { | |
245 | acpi_unbind_one(dev); | |
246 | return 0; | |
247 | } | |
248 | ||
249 | static int __init init_acpi_device_notify(void) | |
250 | { | |
251 | if (acpi_disabled) | |
252 | return 0; | |
253 | if (platform_notify || platform_notify_remove) { | |
254 | printk(KERN_ERR PREFIX "Can't use platform_notify\n"); | |
255 | return 0; | |
256 | } | |
257 | platform_notify = acpi_platform_notify; | |
258 | platform_notify_remove = acpi_platform_notify_remove; | |
259 | return 0; | |
260 | } | |
261 | ||
262 | arch_initcall(init_acpi_device_notify); | |
a74388e2 DB |
263 | |
264 | ||
265 | #if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE) | |
266 | ||
f5f72b46 DB |
267 | #ifdef CONFIG_PM |
268 | static u32 rtc_handler(void *context) | |
269 | { | |
270 | acpi_clear_event(ACPI_EVENT_RTC); | |
271 | acpi_disable_event(ACPI_EVENT_RTC, 0); | |
272 | return ACPI_INTERRUPT_HANDLED; | |
273 | } | |
274 | ||
275 | static inline void rtc_wake_setup(void) | |
276 | { | |
277 | acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL); | |
e1094bfa ZY |
278 | /* |
279 | * After the RTC handler is installed, the Fixed_RTC event should | |
280 | * be disabled. Only when the RTC alarm is set will it be enabled. | |
281 | */ | |
282 | acpi_clear_event(ACPI_EVENT_RTC); | |
283 | acpi_disable_event(ACPI_EVENT_RTC, 0); | |
f5f72b46 DB |
284 | } |
285 | ||
286 | static void rtc_wake_on(struct device *dev) | |
287 | { | |
288 | acpi_clear_event(ACPI_EVENT_RTC); | |
289 | acpi_enable_event(ACPI_EVENT_RTC, 0); | |
290 | } | |
291 | ||
292 | static void rtc_wake_off(struct device *dev) | |
293 | { | |
294 | acpi_disable_event(ACPI_EVENT_RTC, 0); | |
295 | } | |
296 | #else | |
297 | #define rtc_wake_setup() do{}while(0) | |
298 | #define rtc_wake_on NULL | |
299 | #define rtc_wake_off NULL | |
300 | #endif | |
301 | ||
a74388e2 DB |
302 | /* Every ACPI platform has a mc146818 compatible "cmos rtc". Here we find |
303 | * its device node and pass extra config data. This helps its driver use | |
304 | * capabilities that the now-obsolete mc146818 didn't have, and informs it | |
305 | * that this board's RTC is wakeup-capable (per ACPI spec). | |
306 | */ | |
307 | #include <linux/mc146818rtc.h> | |
308 | ||
309 | static struct cmos_rtc_board_info rtc_info; | |
310 | ||
311 | ||
a74388e2 DB |
312 | /* PNP devices are registered in a subsys_initcall(); |
313 | * ACPI specifies the PNP IDs to use. | |
314 | */ | |
315 | #include <linux/pnp.h> | |
316 | ||
317 | static int __init pnp_match(struct device *dev, void *data) | |
318 | { | |
319 | static const char *ids[] = { "PNP0b00", "PNP0b01", "PNP0b02", }; | |
320 | struct pnp_dev *pnp = to_pnp_dev(dev); | |
321 | int i; | |
322 | ||
323 | for (i = 0; i < ARRAY_SIZE(ids); i++) { | |
324 | if (compare_pnp_id(pnp->id, ids[i]) != 0) | |
325 | return 1; | |
326 | } | |
327 | return 0; | |
328 | } | |
329 | ||
330 | static struct device *__init get_rtc_dev(void) | |
331 | { | |
332 | return bus_find_device(&pnp_bus_type, NULL, NULL, pnp_match); | |
333 | } | |
334 | ||
a74388e2 DB |
335 | static int __init acpi_rtc_init(void) |
336 | { | |
337 | struct device *dev = get_rtc_dev(); | |
338 | ||
4389ed2f VN |
339 | if (acpi_disabled) |
340 | return 0; | |
341 | ||
a74388e2 | 342 | if (dev) { |
f5f72b46 DB |
343 | rtc_wake_setup(); |
344 | rtc_info.wake_on = rtc_wake_on; | |
345 | rtc_info.wake_off = rtc_wake_off; | |
346 | ||
19bfe37c DB |
347 | /* workaround bug in some ACPI tables */ |
348 | if (acpi_gbl_FADT.month_alarm && !acpi_gbl_FADT.day_alarm) { | |
349 | DBG("bogus FADT month_alarm\n"); | |
350 | acpi_gbl_FADT.month_alarm = 0; | |
351 | } | |
352 | ||
a74388e2 DB |
353 | rtc_info.rtc_day_alarm = acpi_gbl_FADT.day_alarm; |
354 | rtc_info.rtc_mon_alarm = acpi_gbl_FADT.month_alarm; | |
355 | rtc_info.rtc_century = acpi_gbl_FADT.century; | |
356 | ||
f5f72b46 DB |
357 | /* NOTE: S4_RTC_WAKE is NOT currently useful to Linux */ |
358 | if (acpi_gbl_FADT.flags & ACPI_FADT_S4_RTC_WAKE) | |
19bfe37c DB |
359 | printk(PREFIX "RTC can wake from S4\n"); |
360 | ||
a74388e2 DB |
361 | |
362 | dev->platform_data = &rtc_info; | |
363 | ||
364 | /* RTC always wakes from S1/S2/S3, and often S4/STD */ | |
365 | device_init_wakeup(dev, 1); | |
366 | ||
367 | put_device(dev); | |
368 | } else | |
19bfe37c | 369 | DBG("RTC unavailable?\n"); |
a74388e2 DB |
370 | return 0; |
371 | } | |
372 | /* do this between RTC subsys_initcall() and rtc_cmos driver_initcall() */ | |
373 | fs_initcall(acpi_rtc_init); | |
374 | ||
375 | #endif |