| 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 |  | 
| Thomas Renninger | 22c13f9 | 2008-08-01 17:37:54 +0200 | [diff] [blame] | 143 | /* ToDo: When a PCI bridge is found, return the PCI device behind the bridge | 
|  | 144 | *       This should work in general, but did not on a Lenovo T61 for the | 
|  | 145 | *	 graphics card. But this must be fixed when the PCI device is | 
|  | 146 | *       bound and the kernel device struct is attached to the acpi device | 
|  | 147 | * Note: A success call will increase reference count by one | 
|  | 148 | *       Do call put_device(dev) on the returned device then | 
|  | 149 | */ | 
|  | 150 | struct device *acpi_get_physical_pci_device(acpi_handle handle) | 
|  | 151 | { | 
|  | 152 | struct device *dev; | 
|  | 153 | long long device_id; | 
|  | 154 | acpi_status status; | 
|  | 155 |  | 
|  | 156 | status = | 
|  | 157 | acpi_evaluate_integer(handle, "_ADR", NULL, &device_id); | 
|  | 158 |  | 
|  | 159 | if (ACPI_FAILURE(status)) | 
|  | 160 | return NULL; | 
|  | 161 |  | 
|  | 162 | /* We need to attempt to determine whether the _ADR refers to a | 
|  | 163 | PCI device or not. There's no terribly good way to do this, | 
|  | 164 | so the best we can hope for is to assume that there'll never | 
|  | 165 | be a device in the host bridge */ | 
|  | 166 | if (device_id >= 0x10000) { | 
|  | 167 | /* It looks like a PCI device. Does it exist? */ | 
|  | 168 | dev = acpi_get_physical_device(handle); | 
|  | 169 | } else { | 
|  | 170 | /* It doesn't look like a PCI device. Does its parent | 
|  | 171 | exist? */ | 
|  | 172 | acpi_handle phandle; | 
|  | 173 | if (acpi_get_parent(handle, &phandle)) | 
|  | 174 | return NULL; | 
|  | 175 | dev = acpi_get_physical_device(phandle); | 
|  | 176 | } | 
|  | 177 | if (!dev) | 
|  | 178 | return NULL; | 
|  | 179 | return dev; | 
|  | 180 | } | 
|  | 181 | EXPORT_SYMBOL(acpi_get_physical_pci_device); | 
|  | 182 |  | 
| David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 183 | static int acpi_bind_one(struct device *dev, acpi_handle handle) | 
|  | 184 | { | 
| David Brownell | 1071695 | 2008-02-22 21:54:24 -0800 | [diff] [blame] | 185 | struct acpi_device *acpi_dev; | 
| David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 186 | acpi_status status; | 
|  | 187 |  | 
| Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 188 | if (dev->archdata.acpi_handle) { | 
| Greg Kroah-Hartman | fc3a882 | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 189 | dev_warn(dev, "Drivers changed 'acpi_handle'\n"); | 
| David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 190 | return -EINVAL; | 
|  | 191 | } | 
|  | 192 | get_device(dev); | 
|  | 193 | status = acpi_attach_data(handle, acpi_glue_data_handler, dev); | 
|  | 194 | if (ACPI_FAILURE(status)) { | 
|  | 195 | put_device(dev); | 
|  | 196 | return -EINVAL; | 
|  | 197 | } | 
| Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 198 | dev->archdata.acpi_handle = handle; | 
| David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 199 |  | 
| David Brownell | 1071695 | 2008-02-22 21:54:24 -0800 | [diff] [blame] | 200 | status = acpi_bus_get_device(handle, &acpi_dev); | 
|  | 201 | if (!ACPI_FAILURE(status)) { | 
|  | 202 | int ret; | 
|  | 203 |  | 
|  | 204 | ret = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj, | 
|  | 205 | "firmware_node"); | 
|  | 206 | ret = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj, | 
|  | 207 | "physical_node"); | 
| Rafael J. Wysocki | 76acae0 | 2008-10-03 15:23:49 -0700 | [diff] [blame] | 208 | if (acpi_dev->wakeup.flags.valid) { | 
| Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 209 | device_set_wakeup_capable(dev, true); | 
| Rafael J. Wysocki | 76acae0 | 2008-10-03 15:23:49 -0700 | [diff] [blame] | 210 | device_set_wakeup_enable(dev, | 
|  | 211 | acpi_dev->wakeup.state.enabled); | 
|  | 212 | } | 
| David Brownell | 1071695 | 2008-02-22 21:54:24 -0800 | [diff] [blame] | 213 | } | 
|  | 214 |  | 
| David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 215 | return 0; | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | static int acpi_unbind_one(struct device *dev) | 
|  | 219 | { | 
| Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 220 | if (!dev->archdata.acpi_handle) | 
| David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 221 | return 0; | 
| Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 222 | if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) { | 
| David Brownell | 1071695 | 2008-02-22 21:54:24 -0800 | [diff] [blame] | 223 | struct acpi_device *acpi_dev; | 
|  | 224 |  | 
| David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 225 | /* acpi_get_physical_device increase refcnt by one */ | 
|  | 226 | put_device(dev); | 
| David Brownell | 1071695 | 2008-02-22 21:54:24 -0800 | [diff] [blame] | 227 |  | 
|  | 228 | if (!acpi_bus_get_device(dev->archdata.acpi_handle, | 
|  | 229 | &acpi_dev)) { | 
|  | 230 | sysfs_remove_link(&dev->kobj, "firmware_node"); | 
|  | 231 | sysfs_remove_link(&acpi_dev->dev.kobj, "physical_node"); | 
|  | 232 | } | 
|  | 233 |  | 
| Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 234 | acpi_detach_data(dev->archdata.acpi_handle, | 
|  | 235 | acpi_glue_data_handler); | 
|  | 236 | dev->archdata.acpi_handle = NULL; | 
| David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 237 | /* acpi_bind_one increase refcnt by one */ | 
|  | 238 | put_device(dev); | 
|  | 239 | } else { | 
| Greg Kroah-Hartman | fc3a882 | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 240 | dev_err(dev, "Oops, 'acpi_handle' corrupt\n"); | 
| David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 241 | } | 
|  | 242 | return 0; | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | static int acpi_platform_notify(struct device *dev) | 
|  | 246 | { | 
|  | 247 | struct acpi_bus_type *type; | 
|  | 248 | acpi_handle handle; | 
|  | 249 | int ret = -EINVAL; | 
|  | 250 |  | 
|  | 251 | if (!dev->bus || !dev->parent) { | 
|  | 252 | /* bridge devices genernally haven't bus or parent */ | 
|  | 253 | ret = acpi_find_bridge_device(dev, &handle); | 
|  | 254 | goto end; | 
|  | 255 | } | 
|  | 256 | type = acpi_get_bus_type(dev->bus); | 
|  | 257 | if (!type) { | 
| Kay Sievers | db1461a | 2009-01-25 23:40:56 +0100 | [diff] [blame] | 258 | DBG("No ACPI bus support for %s\n", dev_name(dev)); | 
| David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 259 | ret = -EINVAL; | 
|  | 260 | goto end; | 
|  | 261 | } | 
|  | 262 | if ((ret = type->find_device(dev, &handle)) != 0) | 
| Kay Sievers | db1461a | 2009-01-25 23:40:56 +0100 | [diff] [blame] | 263 | DBG("Can't get handler for %s\n", dev_name(dev)); | 
| David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 264 | end: | 
|  | 265 | if (!ret) | 
|  | 266 | acpi_bind_one(dev, handle); | 
|  | 267 |  | 
|  | 268 | #if ACPI_GLUE_DEBUG | 
|  | 269 | if (!ret) { | 
|  | 270 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | 
|  | 271 |  | 
| Benjamin Herrenschmidt | 465ae64 | 2006-11-11 17:18:42 +1100 | [diff] [blame] | 272 | acpi_get_name(dev->archdata.acpi_handle, | 
|  | 273 | ACPI_FULL_PATHNAME, &buffer); | 
| Kay Sievers | db1461a | 2009-01-25 23:40:56 +0100 | [diff] [blame] | 274 | DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer); | 
| Len Brown | 02438d8 | 2006-06-30 03:19:10 -0400 | [diff] [blame] | 275 | kfree(buffer.pointer); | 
| David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 276 | } else | 
| Kay Sievers | db1461a | 2009-01-25 23:40:56 +0100 | [diff] [blame] | 277 | DBG("Device %s -> No ACPI support\n", dev_name(dev)); | 
| David Shaohua Li | 4e10d12 | 2005-03-18 18:45:35 -0500 | [diff] [blame] | 278 | #endif | 
|  | 279 |  | 
|  | 280 | return ret; | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 | static int acpi_platform_notify_remove(struct device *dev) | 
|  | 284 | { | 
|  | 285 | acpi_unbind_one(dev); | 
|  | 286 | return 0; | 
|  | 287 | } | 
|  | 288 |  | 
|  | 289 | static int __init init_acpi_device_notify(void) | 
|  | 290 | { | 
|  | 291 | if (acpi_disabled) | 
|  | 292 | return 0; | 
|  | 293 | if (platform_notify || platform_notify_remove) { | 
|  | 294 | printk(KERN_ERR PREFIX "Can't use platform_notify\n"); | 
|  | 295 | return 0; | 
|  | 296 | } | 
|  | 297 | platform_notify = acpi_platform_notify; | 
|  | 298 | platform_notify_remove = acpi_platform_notify_remove; | 
|  | 299 | return 0; | 
|  | 300 | } | 
|  | 301 |  | 
|  | 302 | arch_initcall(init_acpi_device_notify); |