David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 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 |
Dave Jones | 4ebf83c | 2007-07-09 11:33:14 -0700 | [diff] [blame] | 19 | #define DBG(x...) do { } while(0) |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 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); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 32 | printk(KERN_INFO PREFIX "bus type %s registered\n", |
| 33 | type->bus->name); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 34 | return 0; |
| 35 | } |
| 36 | return -ENODEV; |
| 37 | } |
| 38 | |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 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); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 47 | printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n", |
| 48 | type->bus->name); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 49 | return 0; |
| 50 | } |
| 51 | return -ENODEV; |
| 52 | } |
| 53 | |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 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 | |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 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 }; |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 97 | struct acpi_find_child *find = context; |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 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; |
Len Brown | 02438d8 | 2006-06-30 03:19:10 -0400 | [diff] [blame] | 104 | kfree(buffer.pointer); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 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 | { |
David Brownell | 1071695 | 2008-02-22 21:54:24 -0800 | [diff] [blame] | 145 | struct acpi_device *acpi_dev; |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 146 | acpi_status status; |
| 147 | |
Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 148 | if (dev->archdata.acpi_handle) { |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 149 | printk(KERN_WARNING PREFIX |
Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 150 | "Drivers changed 'acpi_handle' for %s\n", dev->bus_id); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 151 | return -EINVAL; |
| 152 | } |
| 153 | get_device(dev); |
| 154 | status = acpi_attach_data(handle, acpi_glue_data_handler, dev); |
| 155 | if (ACPI_FAILURE(status)) { |
| 156 | put_device(dev); |
| 157 | return -EINVAL; |
| 158 | } |
Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 159 | dev->archdata.acpi_handle = handle; |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 160 | |
David Brownell | 1071695 | 2008-02-22 21:54:24 -0800 | [diff] [blame] | 161 | status = acpi_bus_get_device(handle, &acpi_dev); |
| 162 | if (!ACPI_FAILURE(status)) { |
| 163 | int ret; |
| 164 | |
| 165 | ret = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj, |
| 166 | "firmware_node"); |
| 167 | ret = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj, |
| 168 | "physical_node"); |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 169 | if (acpi_dev->wakeup.flags.valid) |
| 170 | device_set_wakeup_capable(dev, true); |
David Brownell | 1071695 | 2008-02-22 21:54:24 -0800 | [diff] [blame] | 171 | } |
| 172 | |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static int acpi_unbind_one(struct device *dev) |
| 177 | { |
Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 178 | if (!dev->archdata.acpi_handle) |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 179 | return 0; |
Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 180 | if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) { |
David Brownell | 1071695 | 2008-02-22 21:54:24 -0800 | [diff] [blame] | 181 | struct acpi_device *acpi_dev; |
| 182 | |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 183 | /* acpi_get_physical_device increase refcnt by one */ |
| 184 | put_device(dev); |
David Brownell | 1071695 | 2008-02-22 21:54:24 -0800 | [diff] [blame] | 185 | |
| 186 | if (!acpi_bus_get_device(dev->archdata.acpi_handle, |
| 187 | &acpi_dev)) { |
| 188 | sysfs_remove_link(&dev->kobj, "firmware_node"); |
| 189 | sysfs_remove_link(&acpi_dev->dev.kobj, "physical_node"); |
| 190 | } |
| 191 | |
Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 192 | acpi_detach_data(dev->archdata.acpi_handle, |
| 193 | acpi_glue_data_handler); |
| 194 | dev->archdata.acpi_handle = NULL; |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 195 | /* acpi_bind_one increase refcnt by one */ |
| 196 | put_device(dev); |
| 197 | } else { |
| 198 | printk(KERN_ERR PREFIX |
Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 199 | "Oops, 'acpi_handle' corrupt for %s\n", dev->bus_id); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 200 | } |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int acpi_platform_notify(struct device *dev) |
| 205 | { |
| 206 | struct acpi_bus_type *type; |
| 207 | acpi_handle handle; |
| 208 | int ret = -EINVAL; |
| 209 | |
| 210 | if (!dev->bus || !dev->parent) { |
| 211 | /* bridge devices genernally haven't bus or parent */ |
| 212 | ret = acpi_find_bridge_device(dev, &handle); |
| 213 | goto end; |
| 214 | } |
| 215 | type = acpi_get_bus_type(dev->bus); |
| 216 | if (!type) { |
David Shaohua Li | ef7b06c | 2005-04-18 22:59:23 -0400 | [diff] [blame] | 217 | DBG("No ACPI bus support for %s\n", dev->bus_id); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 218 | ret = -EINVAL; |
| 219 | goto end; |
| 220 | } |
| 221 | if ((ret = type->find_device(dev, &handle)) != 0) |
David Shaohua Li | ef7b06c | 2005-04-18 22:59:23 -0400 | [diff] [blame] | 222 | DBG("Can't get handler for %s\n", dev->bus_id); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 223 | end: |
| 224 | if (!ret) |
| 225 | acpi_bind_one(dev, handle); |
| 226 | |
| 227 | #if ACPI_GLUE_DEBUG |
| 228 | if (!ret) { |
| 229 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 230 | |
Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 231 | acpi_get_name(dev->archdata.acpi_handle, |
| 232 | ACPI_FULL_PATHNAME, &buffer); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 233 | DBG("Device %s -> %s\n", dev->bus_id, (char *)buffer.pointer); |
Len Brown | 02438d8 | 2006-06-30 03:19:10 -0400 | [diff] [blame] | 234 | kfree(buffer.pointer); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 235 | } else |
| 236 | DBG("Device %s -> No ACPI support\n", dev->bus_id); |
| 237 | #endif |
| 238 | |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | static int acpi_platform_notify_remove(struct device *dev) |
| 243 | { |
| 244 | acpi_unbind_one(dev); |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static int __init init_acpi_device_notify(void) |
| 249 | { |
| 250 | if (acpi_disabled) |
| 251 | return 0; |
| 252 | if (platform_notify || platform_notify_remove) { |
| 253 | printk(KERN_ERR PREFIX "Can't use platform_notify\n"); |
| 254 | return 0; |
| 255 | } |
| 256 | platform_notify = acpi_platform_notify; |
| 257 | platform_notify_remove = acpi_platform_notify_remove; |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | arch_initcall(init_acpi_device_notify); |
David Brownell | a74388e | 2007-02-05 16:09:11 -0800 | [diff] [blame] | 262 | |
| 263 | |
| 264 | #if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE) |
| 265 | |
David Brownell | f5f72b4 | 2007-05-08 00:34:02 -0700 | [diff] [blame] | 266 | #ifdef CONFIG_PM |
| 267 | static u32 rtc_handler(void *context) |
| 268 | { |
| 269 | acpi_clear_event(ACPI_EVENT_RTC); |
| 270 | acpi_disable_event(ACPI_EVENT_RTC, 0); |
| 271 | return ACPI_INTERRUPT_HANDLED; |
| 272 | } |
| 273 | |
| 274 | static inline void rtc_wake_setup(void) |
| 275 | { |
| 276 | acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL); |
Zhao Yakui | e1094bf | 2008-05-14 11:32:59 +0800 | [diff] [blame] | 277 | /* |
| 278 | * After the RTC handler is installed, the Fixed_RTC event should |
| 279 | * be disabled. Only when the RTC alarm is set will it be enabled. |
| 280 | */ |
| 281 | acpi_clear_event(ACPI_EVENT_RTC); |
| 282 | acpi_disable_event(ACPI_EVENT_RTC, 0); |
David Brownell | f5f72b4 | 2007-05-08 00:34:02 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static void rtc_wake_on(struct device *dev) |
| 286 | { |
| 287 | acpi_clear_event(ACPI_EVENT_RTC); |
| 288 | acpi_enable_event(ACPI_EVENT_RTC, 0); |
| 289 | } |
| 290 | |
| 291 | static void rtc_wake_off(struct device *dev) |
| 292 | { |
| 293 | acpi_disable_event(ACPI_EVENT_RTC, 0); |
| 294 | } |
| 295 | #else |
| 296 | #define rtc_wake_setup() do{}while(0) |
| 297 | #define rtc_wake_on NULL |
| 298 | #define rtc_wake_off NULL |
| 299 | #endif |
| 300 | |
David Brownell | a74388e | 2007-02-05 16:09:11 -0800 | [diff] [blame] | 301 | /* Every ACPI platform has a mc146818 compatible "cmos rtc". Here we find |
| 302 | * its device node and pass extra config data. This helps its driver use |
| 303 | * capabilities that the now-obsolete mc146818 didn't have, and informs it |
| 304 | * that this board's RTC is wakeup-capable (per ACPI spec). |
| 305 | */ |
| 306 | #include <linux/mc146818rtc.h> |
| 307 | |
| 308 | static struct cmos_rtc_board_info rtc_info; |
| 309 | |
| 310 | |
David Brownell | a74388e | 2007-02-05 16:09:11 -0800 | [diff] [blame] | 311 | /* PNP devices are registered in a subsys_initcall(); |
| 312 | * ACPI specifies the PNP IDs to use. |
| 313 | */ |
| 314 | #include <linux/pnp.h> |
| 315 | |
| 316 | static int __init pnp_match(struct device *dev, void *data) |
| 317 | { |
| 318 | static const char *ids[] = { "PNP0b00", "PNP0b01", "PNP0b02", }; |
| 319 | struct pnp_dev *pnp = to_pnp_dev(dev); |
| 320 | int i; |
| 321 | |
| 322 | for (i = 0; i < ARRAY_SIZE(ids); i++) { |
| 323 | if (compare_pnp_id(pnp->id, ids[i]) != 0) |
| 324 | return 1; |
| 325 | } |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static struct device *__init get_rtc_dev(void) |
| 330 | { |
| 331 | return bus_find_device(&pnp_bus_type, NULL, NULL, pnp_match); |
| 332 | } |
| 333 | |
David Brownell | a74388e | 2007-02-05 16:09:11 -0800 | [diff] [blame] | 334 | static int __init acpi_rtc_init(void) |
| 335 | { |
| 336 | struct device *dev = get_rtc_dev(); |
| 337 | |
Vegard Nossum | 4389ed2 | 2008-06-20 15:39:09 +0200 | [diff] [blame] | 338 | if (acpi_disabled) |
| 339 | return 0; |
| 340 | |
David Brownell | a74388e | 2007-02-05 16:09:11 -0800 | [diff] [blame] | 341 | if (dev) { |
David Brownell | f5f72b4 | 2007-05-08 00:34:02 -0700 | [diff] [blame] | 342 | rtc_wake_setup(); |
| 343 | rtc_info.wake_on = rtc_wake_on; |
| 344 | rtc_info.wake_off = rtc_wake_off; |
| 345 | |
David Brownell | 19bfe37 | 2007-05-08 00:34:03 -0700 | [diff] [blame] | 346 | /* workaround bug in some ACPI tables */ |
| 347 | if (acpi_gbl_FADT.month_alarm && !acpi_gbl_FADT.day_alarm) { |
| 348 | DBG("bogus FADT month_alarm\n"); |
| 349 | acpi_gbl_FADT.month_alarm = 0; |
| 350 | } |
| 351 | |
David Brownell | a74388e | 2007-02-05 16:09:11 -0800 | [diff] [blame] | 352 | rtc_info.rtc_day_alarm = acpi_gbl_FADT.day_alarm; |
| 353 | rtc_info.rtc_mon_alarm = acpi_gbl_FADT.month_alarm; |
| 354 | rtc_info.rtc_century = acpi_gbl_FADT.century; |
| 355 | |
David Brownell | f5f72b4 | 2007-05-08 00:34:02 -0700 | [diff] [blame] | 356 | /* NOTE: S4_RTC_WAKE is NOT currently useful to Linux */ |
| 357 | if (acpi_gbl_FADT.flags & ACPI_FADT_S4_RTC_WAKE) |
David Brownell | 19bfe37 | 2007-05-08 00:34:03 -0700 | [diff] [blame] | 358 | printk(PREFIX "RTC can wake from S4\n"); |
| 359 | |
David Brownell | a74388e | 2007-02-05 16:09:11 -0800 | [diff] [blame] | 360 | |
| 361 | dev->platform_data = &rtc_info; |
| 362 | |
| 363 | /* RTC always wakes from S1/S2/S3, and often S4/STD */ |
| 364 | device_init_wakeup(dev, 1); |
| 365 | |
| 366 | put_device(dev); |
| 367 | } else |
David Brownell | 19bfe37 | 2007-05-08 00:34:03 -0700 | [diff] [blame] | 368 | DBG("RTC unavailable?\n"); |
David Brownell | a74388e | 2007-02-05 16:09:11 -0800 | [diff] [blame] | 369 | return 0; |
| 370 | } |
| 371 | /* do this between RTC subsys_initcall() and rtc_cmos driver_initcall() */ |
| 372 | fs_initcall(acpi_rtc_init); |
| 373 | |
| 374 | #endif |