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 | */ |
Paul Gortmaker | 214f2c9 | 2011-10-26 16:22:14 -0400 | [diff] [blame] | 9 | #include <linux/export.h> |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 10 | #include <linux/init.h> |
| 11 | #include <linux/list.h> |
| 12 | #include <linux/device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 14 | #include <linux/rwsem.h> |
| 15 | #include <linux/acpi.h> |
| 16 | |
Len Brown | a192a95 | 2009-07-28 16:45:54 -0400 | [diff] [blame] | 17 | #include "internal.h" |
| 18 | |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 19 | #define ACPI_GLUE_DEBUG 0 |
| 20 | #if ACPI_GLUE_DEBUG |
| 21 | #define DBG(x...) printk(PREFIX x) |
| 22 | #else |
Dave Jones | 4ebf83c | 2007-07-09 11:33:14 -0700 | [diff] [blame] | 23 | #define DBG(x...) do { } while(0) |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 24 | #endif |
| 25 | static LIST_HEAD(bus_type_list); |
| 26 | static DECLARE_RWSEM(bus_type_sem); |
| 27 | |
| 28 | int register_acpi_bus_type(struct acpi_bus_type *type) |
| 29 | { |
| 30 | if (acpi_disabled) |
| 31 | return -ENODEV; |
| 32 | if (type && type->bus && type->find_device) { |
| 33 | down_write(&bus_type_sem); |
| 34 | list_add_tail(&type->list, &bus_type_list); |
| 35 | up_write(&bus_type_sem); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 36 | printk(KERN_INFO PREFIX "bus type %s registered\n", |
| 37 | type->bus->name); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 38 | return 0; |
| 39 | } |
| 40 | return -ENODEV; |
| 41 | } |
| 42 | |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 43 | int unregister_acpi_bus_type(struct acpi_bus_type *type) |
| 44 | { |
| 45 | if (acpi_disabled) |
| 46 | return 0; |
| 47 | if (type) { |
| 48 | down_write(&bus_type_sem); |
| 49 | list_del_init(&type->list); |
| 50 | up_write(&bus_type_sem); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 51 | printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n", |
| 52 | type->bus->name); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 53 | return 0; |
| 54 | } |
| 55 | return -ENODEV; |
| 56 | } |
| 57 | |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 58 | static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type) |
| 59 | { |
| 60 | struct acpi_bus_type *tmp, *ret = NULL; |
| 61 | |
| 62 | down_read(&bus_type_sem); |
| 63 | list_for_each_entry(tmp, &bus_type_list, list) { |
| 64 | if (tmp->bus == type) { |
| 65 | ret = tmp; |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | up_read(&bus_type_sem); |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle) |
| 74 | { |
| 75 | struct acpi_bus_type *tmp; |
| 76 | int ret = -ENODEV; |
| 77 | |
| 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)) { |
| 81 | ret = 0; |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | up_read(&bus_type_sem); |
| 86 | return ret; |
| 87 | } |
| 88 | |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 89 | /* Get device's handler per its address under its parent */ |
| 90 | struct acpi_find_child { |
| 91 | acpi_handle handle; |
Lin Ming | 439913f | 2010-01-28 10:53:19 +0800 | [diff] [blame] | 92 | u64 address; |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | static acpi_status |
| 96 | do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 97 | { |
| 98 | acpi_status status; |
| 99 | struct acpi_device_info *info; |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 100 | struct acpi_find_child *find = context; |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 101 | |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 102 | status = acpi_get_object_info(handle, &info); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 103 | if (ACPI_SUCCESS(status)) { |
Zhao Yakui | 108029f | 2010-07-13 03:36:08 +0000 | [diff] [blame] | 104 | if ((info->address == find->address) |
| 105 | && (info->valid & ACPI_VALID_ADR)) |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 106 | find->handle = handle; |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 107 | kfree(info); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 108 | } |
| 109 | return AE_OK; |
| 110 | } |
| 111 | |
Lin Ming | 439913f | 2010-01-28 10:53:19 +0800 | [diff] [blame] | 112 | acpi_handle acpi_get_child(acpi_handle parent, u64 address) |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 113 | { |
| 114 | struct acpi_find_child find = { NULL, address }; |
| 115 | |
| 116 | if (!parent) |
| 117 | return NULL; |
| 118 | acpi_walk_namespace(ACPI_TYPE_DEVICE, parent, |
Lin Ming | 2263576 | 2009-11-13 10:06:08 +0800 | [diff] [blame] | 119 | 1, do_acpi_find_child, NULL, &find, NULL); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 120 | return find.handle; |
| 121 | } |
| 122 | |
| 123 | EXPORT_SYMBOL(acpi_get_child); |
| 124 | |
| 125 | /* Link ACPI devices with physical devices */ |
| 126 | static void acpi_glue_data_handler(acpi_handle handle, |
Bob Moore | 8e4319c | 2009-06-29 13:43:27 +0800 | [diff] [blame] | 127 | void *context) |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 128 | { |
| 129 | /* we provide an empty handler */ |
| 130 | } |
| 131 | |
| 132 | /* Note: a success call will increase reference count by one */ |
| 133 | struct device *acpi_get_physical_device(acpi_handle handle) |
| 134 | { |
| 135 | acpi_status status; |
| 136 | struct device *dev; |
| 137 | |
| 138 | status = acpi_get_data(handle, acpi_glue_data_handler, (void **)&dev); |
| 139 | if (ACPI_SUCCESS(status)) |
| 140 | return get_device(dev); |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | EXPORT_SYMBOL(acpi_get_physical_device); |
| 145 | |
| 146 | static int acpi_bind_one(struct device *dev, acpi_handle handle) |
| 147 | { |
David Brownell | 1071695 | 2008-02-22 21:54:24 -0800 | [diff] [blame] | 148 | struct acpi_device *acpi_dev; |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 149 | acpi_status status; |
| 150 | |
Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 151 | if (dev->archdata.acpi_handle) { |
Greg Kroah-Hartman | fc3a882 | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 152 | dev_warn(dev, "Drivers changed 'acpi_handle'\n"); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 153 | return -EINVAL; |
| 154 | } |
| 155 | get_device(dev); |
| 156 | status = acpi_attach_data(handle, acpi_glue_data_handler, dev); |
| 157 | if (ACPI_FAILURE(status)) { |
| 158 | put_device(dev); |
| 159 | return -EINVAL; |
| 160 | } |
Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 161 | dev->archdata.acpi_handle = handle; |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 162 | |
David Brownell | 1071695 | 2008-02-22 21:54:24 -0800 | [diff] [blame] | 163 | status = acpi_bus_get_device(handle, &acpi_dev); |
| 164 | if (!ACPI_FAILURE(status)) { |
| 165 | int ret; |
| 166 | |
| 167 | ret = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj, |
| 168 | "firmware_node"); |
| 169 | ret = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj, |
| 170 | "physical_node"); |
Rafael J. Wysocki | 7fa69ba | 2011-01-06 23:35:10 +0100 | [diff] [blame] | 171 | if (acpi_dev->wakeup.flags.valid) |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 172 | device_set_wakeup_capable(dev, true); |
David Brownell | 1071695 | 2008-02-22 21:54:24 -0800 | [diff] [blame] | 173 | } |
| 174 | |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static int acpi_unbind_one(struct device *dev) |
| 179 | { |
Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 180 | if (!dev->archdata.acpi_handle) |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 181 | return 0; |
Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 182 | if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) { |
David Brownell | 1071695 | 2008-02-22 21:54:24 -0800 | [diff] [blame] | 183 | struct acpi_device *acpi_dev; |
| 184 | |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 185 | /* acpi_get_physical_device increase refcnt by one */ |
| 186 | put_device(dev); |
David Brownell | 1071695 | 2008-02-22 21:54:24 -0800 | [diff] [blame] | 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 | |
Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 194 | acpi_detach_data(dev->archdata.acpi_handle, |
| 195 | acpi_glue_data_handler); |
| 196 | dev->archdata.acpi_handle = NULL; |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 197 | /* acpi_bind_one increase refcnt by one */ |
| 198 | put_device(dev); |
| 199 | } else { |
Greg Kroah-Hartman | fc3a882 | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 200 | dev_err(dev, "Oops, 'acpi_handle' corrupt\n"); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 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) { |
Kay Sievers | db1461a | 2009-01-25 23:40:56 +0100 | [diff] [blame] | 218 | DBG("No ACPI bus support for %s\n", dev_name(dev)); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 219 | ret = -EINVAL; |
| 220 | goto end; |
| 221 | } |
| 222 | if ((ret = type->find_device(dev, &handle)) != 0) |
Kay Sievers | db1461a | 2009-01-25 23:40:56 +0100 | [diff] [blame] | 223 | DBG("Can't get handler for %s\n", dev_name(dev)); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 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 | |
Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 232 | acpi_get_name(dev->archdata.acpi_handle, |
| 233 | ACPI_FULL_PATHNAME, &buffer); |
Kay Sievers | db1461a | 2009-01-25 23:40:56 +0100 | [diff] [blame] | 234 | DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer); |
Len Brown | 02438d8 | 2006-06-30 03:19:10 -0400 | [diff] [blame] | 235 | kfree(buffer.pointer); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 236 | } else |
Kay Sievers | db1461a | 2009-01-25 23:40:56 +0100 | [diff] [blame] | 237 | DBG("Device %s -> No ACPI support\n", dev_name(dev)); |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 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 | |
Bjorn Helgaas | 0e46517 | 2009-03-24 16:50:09 -0600 | [diff] [blame] | 249 | int __init init_acpi_device_notify(void) |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 250 | { |
David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 251 | if (platform_notify || platform_notify_remove) { |
| 252 | printk(KERN_ERR PREFIX "Can't use platform_notify\n"); |
| 253 | return 0; |
| 254 | } |
| 255 | platform_notify = acpi_platform_notify; |
| 256 | platform_notify_remove = acpi_platform_notify_remove; |
| 257 | return 0; |
| 258 | } |