Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * scan.c - support for transforming the ACPI namespace into individual objects |
| 3 | */ |
| 4 | |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 7 | #include <linux/slab.h> |
Randy Dunlap | 9b6d97b | 2006-07-12 02:08:00 -0400 | [diff] [blame] | 8 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/acpi.h> |
Alok N Kataria | 74523c9 | 2008-06-13 12:54:24 -0400 | [diff] [blame] | 10 | #include <linux/signal.h> |
| 11 | #include <linux/kthread.h> |
Darrick J. Wong | 222e82a | 2010-03-24 14:38:37 +0100 | [diff] [blame] | 12 | #include <linux/dmi.h> |
Lance Ortiz | d1efe3c | 2012-10-02 12:43:23 -0600 | [diff] [blame] | 13 | #include <linux/nls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
Rafael J. Wysocki | d783156 | 2013-11-22 21:52:12 +0100 | [diff] [blame] | 15 | #include <asm/pgtable.h> |
| 16 | |
Bjorn Helgaas | e60cc7a | 2009-03-13 12:08:26 -0600 | [diff] [blame] | 17 | #include "internal.h" |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #define _COMPONENT ACPI_BUS_COMPONENT |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 20 | ACPI_MODULE_NAME("scan"); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 21 | extern struct acpi_device *acpi_root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #define ACPI_BUS_CLASS "system_bus" |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 24 | #define ACPI_BUS_HID "LNXSYBUS" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #define ACPI_BUS_DEVICE_NAME "System Bus" |
| 26 | |
Bjorn Helgaas | 859ac9a | 2009-09-21 19:29:50 +0000 | [diff] [blame] | 27 | #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent) |
| 28 | |
Rafael J. Wysocki | d783156 | 2013-11-22 21:52:12 +0100 | [diff] [blame] | 29 | #define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page) |
| 30 | |
Rafael J. Wysocki | 683058e | 2013-05-03 00:26:16 +0200 | [diff] [blame] | 31 | /* |
| 32 | * If set, devices will be hot-removed even if they cannot be put offline |
| 33 | * gracefully (from the kernel's standpoint). |
| 34 | */ |
| 35 | bool acpi_force_hot_remove; |
| 36 | |
Thomas Renninger | 620e112 | 2010-10-01 10:54:00 +0200 | [diff] [blame] | 37 | static const char *dummy_hid = "device"; |
Thomas Renninger | 2b2ae7c | 2010-10-01 10:53:59 +0200 | [diff] [blame] | 38 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 39 | static LIST_HEAD(acpi_bus_id_list); |
Rafael J. Wysocki | c511cc1 | 2013-01-27 21:17:29 +0100 | [diff] [blame] | 40 | static DEFINE_MUTEX(acpi_scan_lock); |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 41 | static LIST_HEAD(acpi_scan_handlers_list); |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 42 | DEFINE_MUTEX(acpi_device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | LIST_HEAD(acpi_wakeup_device_list); |
Rafael J. Wysocki | e525506 | 2014-02-04 00:43:17 +0100 | [diff] [blame] | 44 | static DEFINE_MUTEX(acpi_hp_context_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 46 | struct acpi_device_bus_id{ |
Zhang Rui | bb09585 | 2007-01-04 15:03:18 +0800 | [diff] [blame] | 47 | char bus_id[15]; |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 48 | unsigned int instance_no; |
| 49 | struct list_head node; |
| 50 | }; |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 51 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 52 | void acpi_scan_lock_acquire(void) |
| 53 | { |
| 54 | mutex_lock(&acpi_scan_lock); |
| 55 | } |
| 56 | EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire); |
| 57 | |
| 58 | void acpi_scan_lock_release(void) |
| 59 | { |
| 60 | mutex_unlock(&acpi_scan_lock); |
| 61 | } |
| 62 | EXPORT_SYMBOL_GPL(acpi_scan_lock_release); |
| 63 | |
Rafael J. Wysocki | e525506 | 2014-02-04 00:43:17 +0100 | [diff] [blame] | 64 | void acpi_lock_hp_context(void) |
| 65 | { |
| 66 | mutex_lock(&acpi_hp_context_lock); |
| 67 | } |
| 68 | |
| 69 | void acpi_unlock_hp_context(void) |
| 70 | { |
| 71 | mutex_unlock(&acpi_hp_context_lock); |
| 72 | } |
| 73 | |
Rafael J. Wysocki | 5d51320 | 2014-02-22 00:48:31 +0100 | [diff] [blame] | 74 | void acpi_initialize_hp_context(struct acpi_device *adev, |
| 75 | struct acpi_hotplug_context *hp, |
| 76 | int (*notify)(struct acpi_device *, u32), |
| 77 | void (*uevent)(struct acpi_device *, u32)) |
| 78 | { |
| 79 | acpi_lock_hp_context(); |
Rafael J. Wysocki | ba574dc | 2014-07-15 22:03:22 +0200 | [diff] [blame] | 80 | hp->notify = notify; |
| 81 | hp->uevent = uevent; |
| 82 | acpi_set_hp_context(adev, hp); |
Rafael J. Wysocki | 5d51320 | 2014-02-22 00:48:31 +0100 | [diff] [blame] | 83 | acpi_unlock_hp_context(); |
| 84 | } |
| 85 | EXPORT_SYMBOL_GPL(acpi_initialize_hp_context); |
| 86 | |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 87 | int acpi_scan_add_handler(struct acpi_scan_handler *handler) |
| 88 | { |
Rafael J. Wysocki | d34afa9 | 2014-05-30 04:27:31 +0200 | [diff] [blame] | 89 | if (!handler) |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 90 | return -EINVAL; |
| 91 | |
| 92 | list_add_tail(&handler->list_node, &acpi_scan_handlers_list); |
| 93 | return 0; |
| 94 | } |
| 95 | |
Rafael J. Wysocki | 3f8055c | 2013-03-03 23:08:16 +0100 | [diff] [blame] | 96 | int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler, |
| 97 | const char *hotplug_profile_name) |
| 98 | { |
| 99 | int error; |
| 100 | |
| 101 | error = acpi_scan_add_handler(handler); |
| 102 | if (error) |
| 103 | return error; |
| 104 | |
| 105 | acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name); |
| 106 | return 0; |
| 107 | } |
| 108 | |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 109 | /* |
| 110 | * Creates hid/cid(s) string needed for modalias and uevent |
| 111 | * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get: |
| 112 | * char *modalias: "acpi:IBM0001:ACPI0001" |
Zhang Rui | 3d8e009 | 2014-01-14 16:46:35 +0800 | [diff] [blame] | 113 | * Return: 0: no _HID and no _CID |
| 114 | * -EINVAL: output error |
| 115 | * -ENOMEM: output is truncated |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 116 | */ |
Adrian Bunk | b3e572d | 2007-08-14 23:22:35 +0200 | [diff] [blame] | 117 | static int create_modalias(struct acpi_device *acpi_dev, char *modalias, |
| 118 | int size) |
| 119 | { |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 120 | int len; |
Zhang Rui | 5c9fcb5 | 2008-03-20 16:40:32 +0800 | [diff] [blame] | 121 | int count; |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 122 | struct acpi_hardware_id *id; |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 123 | |
Thomas Renninger | 2b2ae7c | 2010-10-01 10:53:59 +0200 | [diff] [blame] | 124 | if (list_empty(&acpi_dev->pnp.ids)) |
| 125 | return 0; |
| 126 | |
Mika Westerberg | 733e625 | 2014-10-21 13:33:56 +0200 | [diff] [blame^] | 127 | /* |
| 128 | * If the device has PRP0001 we expose DT compatible modalias |
| 129 | * instead in form of of:NnameTCcompatible. |
| 130 | */ |
| 131 | if (acpi_dev->data.of_compatible) { |
| 132 | struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER }; |
| 133 | const union acpi_object *of_compatible, *obj; |
| 134 | int i, nval; |
| 135 | char *c; |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 136 | |
Mika Westerberg | 733e625 | 2014-10-21 13:33:56 +0200 | [diff] [blame^] | 137 | acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf); |
| 138 | /* DT strings are all in lower case */ |
| 139 | for (c = buf.pointer; *c != '\0'; c++) |
| 140 | *c = tolower(*c); |
| 141 | |
| 142 | len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer); |
| 143 | ACPI_FREE(buf.pointer); |
| 144 | |
| 145 | of_compatible = acpi_dev->data.of_compatible; |
| 146 | if (of_compatible->type == ACPI_TYPE_PACKAGE) { |
| 147 | nval = of_compatible->package.count; |
| 148 | obj = of_compatible->package.elements; |
| 149 | } else { /* Must be ACPI_TYPE_STRING. */ |
| 150 | nval = 1; |
| 151 | obj = of_compatible; |
| 152 | } |
| 153 | for (i = 0; i < nval; i++, obj++) { |
| 154 | count = snprintf(&modalias[len], size, "C%s", |
| 155 | obj->string.pointer); |
| 156 | if (count < 0) |
| 157 | return -EINVAL; |
| 158 | if (count >= size) |
| 159 | return -ENOMEM; |
| 160 | |
| 161 | len += count; |
| 162 | size -= count; |
| 163 | } |
| 164 | } else { |
| 165 | len = snprintf(modalias, size, "acpi:"); |
| 166 | size -= len; |
| 167 | |
| 168 | list_for_each_entry(id, &acpi_dev->pnp.ids, list) { |
| 169 | count = snprintf(&modalias[len], size, "%s:", id->id); |
| 170 | if (count < 0) |
| 171 | return -EINVAL; |
| 172 | if (count >= size) |
| 173 | return -ENOMEM; |
| 174 | len += count; |
| 175 | size -= count; |
| 176 | } |
Zhang Rui | 5c9fcb5 | 2008-03-20 16:40:32 +0800 | [diff] [blame] | 177 | } |
| 178 | |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 179 | modalias[len] = '\0'; |
| 180 | return len; |
| 181 | } |
| 182 | |
Zhang Rui | 6eb2451 | 2014-01-14 16:46:36 +0800 | [diff] [blame] | 183 | /* |
Mika Westerberg | 5287078 | 2014-10-24 16:40:54 +0200 | [diff] [blame] | 184 | * acpi_companion_match() - Can we match via ACPI companion device |
| 185 | * @dev: Device in question |
| 186 | * |
| 187 | * Check if the given device has an ACPI companion and if that companion has |
| 188 | * a valid list of PNP IDs, and if the device is the first (primary) physical |
| 189 | * device associated with it. |
| 190 | * |
| 191 | * If multiple physical devices are attached to a single ACPI companion, we need |
| 192 | * to be careful. The usage scenario for this kind of relationship is that all |
| 193 | * of the physical devices in question use resources provided by the ACPI |
| 194 | * companion. A typical case is an MFD device where all the sub-devices share |
| 195 | * the parent's ACPI companion. In such cases we can only allow the primary |
| 196 | * (first) physical device to be matched with the help of the companion's PNP |
| 197 | * IDs. |
| 198 | * |
| 199 | * Additional physical devices sharing the ACPI companion can still use |
| 200 | * resources available from it but they will be matched normally using functions |
| 201 | * provided by their bus types (and analogously for their modalias). |
| 202 | */ |
| 203 | static bool acpi_companion_match(const struct device *dev) |
| 204 | { |
| 205 | struct acpi_device *adev; |
| 206 | bool ret; |
| 207 | |
| 208 | adev = ACPI_COMPANION(dev); |
| 209 | if (!adev) |
| 210 | return false; |
| 211 | |
| 212 | if (list_empty(&adev->pnp.ids)) |
| 213 | return false; |
| 214 | |
| 215 | mutex_lock(&adev->physical_node_lock); |
| 216 | if (list_empty(&adev->physical_node_list)) { |
| 217 | ret = false; |
| 218 | } else { |
| 219 | const struct acpi_device_physical_node *node; |
| 220 | |
| 221 | node = list_first_entry(&adev->physical_node_list, |
| 222 | struct acpi_device_physical_node, node); |
| 223 | ret = node->dev == dev; |
| 224 | } |
| 225 | mutex_unlock(&adev->physical_node_lock); |
| 226 | |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | /* |
Zhang Rui | 6eb2451 | 2014-01-14 16:46:36 +0800 | [diff] [blame] | 231 | * Creates uevent modalias field for ACPI enumerated devices. |
| 232 | * Because the other buses does not support ACPI HIDs & CIDs. |
| 233 | * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get: |
| 234 | * "acpi:IBM0001:ACPI0001" |
| 235 | */ |
| 236 | int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env) |
| 237 | { |
Zhang Rui | 6eb2451 | 2014-01-14 16:46:36 +0800 | [diff] [blame] | 238 | int len; |
| 239 | |
Mika Westerberg | 5287078 | 2014-10-24 16:40:54 +0200 | [diff] [blame] | 240 | if (!acpi_companion_match(dev)) |
Zhang Rui | 6eb2451 | 2014-01-14 16:46:36 +0800 | [diff] [blame] | 241 | return -ENODEV; |
| 242 | |
| 243 | if (add_uevent_var(env, "MODALIAS=")) |
| 244 | return -ENOMEM; |
Mika Westerberg | 5287078 | 2014-10-24 16:40:54 +0200 | [diff] [blame] | 245 | len = create_modalias(ACPI_COMPANION(dev), &env->buf[env->buflen - 1], |
Zhang Rui | 6eb2451 | 2014-01-14 16:46:36 +0800 | [diff] [blame] | 246 | sizeof(env->buf) - env->buflen); |
| 247 | if (len <= 0) |
| 248 | return len; |
| 249 | env->buflen += len; |
| 250 | return 0; |
| 251 | } |
| 252 | EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias); |
| 253 | |
| 254 | /* |
| 255 | * Creates modalias sysfs attribute for ACPI enumerated devices. |
| 256 | * Because the other buses does not support ACPI HIDs & CIDs. |
| 257 | * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get: |
| 258 | * "acpi:IBM0001:ACPI0001" |
| 259 | */ |
| 260 | int acpi_device_modalias(struct device *dev, char *buf, int size) |
| 261 | { |
Zhang Rui | 6eb2451 | 2014-01-14 16:46:36 +0800 | [diff] [blame] | 262 | int len; |
| 263 | |
Mika Westerberg | 5287078 | 2014-10-24 16:40:54 +0200 | [diff] [blame] | 264 | if (!acpi_companion_match(dev)) |
Zhang Rui | 6eb2451 | 2014-01-14 16:46:36 +0800 | [diff] [blame] | 265 | return -ENODEV; |
| 266 | |
Mika Westerberg | 5287078 | 2014-10-24 16:40:54 +0200 | [diff] [blame] | 267 | len = create_modalias(ACPI_COMPANION(dev), buf, size -1); |
Zhang Rui | 6eb2451 | 2014-01-14 16:46:36 +0800 | [diff] [blame] | 268 | if (len <= 0) |
| 269 | return len; |
| 270 | buf[len++] = '\n'; |
| 271 | return len; |
| 272 | } |
| 273 | EXPORT_SYMBOL_GPL(acpi_device_modalias); |
| 274 | |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 275 | static ssize_t |
| 276 | acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) { |
| 277 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 278 | int len; |
| 279 | |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 280 | len = create_modalias(acpi_dev, buf, 1024); |
| 281 | if (len <= 0) |
Zhang Rui | 3d8e009 | 2014-01-14 16:46:35 +0800 | [diff] [blame] | 282 | return len; |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 283 | buf[len++] = '\n'; |
| 284 | return len; |
| 285 | } |
| 286 | static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL); |
| 287 | |
Rafael J. Wysocki | caa73ea | 2013-12-29 15:25:48 +0100 | [diff] [blame] | 288 | bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent) |
Rafael J. Wysocki | d22ddcb | 2013-12-29 15:25:35 +0100 | [diff] [blame] | 289 | { |
| 290 | struct acpi_device_physical_node *pn; |
| 291 | bool offline = true; |
| 292 | |
| 293 | mutex_lock(&adev->physical_node_lock); |
| 294 | |
| 295 | list_for_each_entry(pn, &adev->physical_node_list, node) |
| 296 | if (device_supports_offline(pn->dev) && !pn->dev->offline) { |
Rafael J. Wysocki | caa73ea | 2013-12-29 15:25:48 +0100 | [diff] [blame] | 297 | if (uevent) |
| 298 | kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE); |
| 299 | |
Rafael J. Wysocki | d22ddcb | 2013-12-29 15:25:35 +0100 | [diff] [blame] | 300 | offline = false; |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | mutex_unlock(&adev->physical_node_lock); |
| 305 | return offline; |
| 306 | } |
| 307 | |
Rafael J. Wysocki | 7f28dde | 2013-11-07 01:41:14 +0100 | [diff] [blame] | 308 | static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data, |
| 309 | void **ret_p) |
Rafael J. Wysocki | 683058e | 2013-05-03 00:26:16 +0200 | [diff] [blame] | 310 | { |
| 311 | struct acpi_device *device = NULL; |
| 312 | struct acpi_device_physical_node *pn; |
Rafael J. Wysocki | 303bfdb | 2013-05-23 10:43:13 +0200 | [diff] [blame] | 313 | bool second_pass = (bool)data; |
Rafael J. Wysocki | 683058e | 2013-05-03 00:26:16 +0200 | [diff] [blame] | 314 | acpi_status status = AE_OK; |
| 315 | |
| 316 | if (acpi_bus_get_device(handle, &device)) |
| 317 | return AE_OK; |
| 318 | |
Rafael J. Wysocki | 7f28dde | 2013-11-07 01:41:14 +0100 | [diff] [blame] | 319 | if (device->handler && !device->handler->hotplug.enabled) { |
| 320 | *ret_p = &device->dev; |
| 321 | return AE_SUPPORT; |
| 322 | } |
| 323 | |
Rafael J. Wysocki | 683058e | 2013-05-03 00:26:16 +0200 | [diff] [blame] | 324 | mutex_lock(&device->physical_node_lock); |
| 325 | |
| 326 | list_for_each_entry(pn, &device->physical_node_list, node) { |
| 327 | int ret; |
| 328 | |
Rafael J. Wysocki | 303bfdb | 2013-05-23 10:43:13 +0200 | [diff] [blame] | 329 | if (second_pass) { |
| 330 | /* Skip devices offlined by the first pass. */ |
| 331 | if (pn->put_online) |
| 332 | continue; |
| 333 | } else { |
| 334 | pn->put_online = false; |
| 335 | } |
Rafael J. Wysocki | 683058e | 2013-05-03 00:26:16 +0200 | [diff] [blame] | 336 | ret = device_offline(pn->dev); |
| 337 | if (acpi_force_hot_remove) |
| 338 | continue; |
| 339 | |
Rafael J. Wysocki | 303bfdb | 2013-05-23 10:43:13 +0200 | [diff] [blame] | 340 | if (ret >= 0) { |
| 341 | pn->put_online = !ret; |
| 342 | } else { |
| 343 | *ret_p = pn->dev; |
| 344 | if (second_pass) { |
| 345 | status = AE_ERROR; |
| 346 | break; |
| 347 | } |
Rafael J. Wysocki | 683058e | 2013-05-03 00:26:16 +0200 | [diff] [blame] | 348 | } |
Rafael J. Wysocki | 683058e | 2013-05-03 00:26:16 +0200 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | mutex_unlock(&device->physical_node_lock); |
| 352 | |
| 353 | return status; |
| 354 | } |
| 355 | |
Rafael J. Wysocki | 7f28dde | 2013-11-07 01:41:14 +0100 | [diff] [blame] | 356 | static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data, |
| 357 | void **ret_p) |
Rafael J. Wysocki | 683058e | 2013-05-03 00:26:16 +0200 | [diff] [blame] | 358 | { |
| 359 | struct acpi_device *device = NULL; |
| 360 | struct acpi_device_physical_node *pn; |
| 361 | |
| 362 | if (acpi_bus_get_device(handle, &device)) |
| 363 | return AE_OK; |
| 364 | |
| 365 | mutex_lock(&device->physical_node_lock); |
| 366 | |
| 367 | list_for_each_entry(pn, &device->physical_node_list, node) |
| 368 | if (pn->put_online) { |
| 369 | device_online(pn->dev); |
| 370 | pn->put_online = false; |
| 371 | } |
| 372 | |
| 373 | mutex_unlock(&device->physical_node_lock); |
| 374 | |
| 375 | return AE_OK; |
| 376 | } |
| 377 | |
Rafael J. Wysocki | d22ddcb | 2013-12-29 15:25:35 +0100 | [diff] [blame] | 378 | static int acpi_scan_try_to_offline(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | { |
Yinghai Lu | 5993c46 | 2013-01-11 22:40:41 +0000 | [diff] [blame] | 380 | acpi_handle handle = device->handle; |
Rafael J. Wysocki | d22ddcb | 2013-12-29 15:25:35 +0100 | [diff] [blame] | 381 | struct device *errdev = NULL; |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 382 | acpi_status status; |
Rafael J. Wysocki | f058cdf | 2013-02-09 15:29:11 +0100 | [diff] [blame] | 383 | |
Rafael J. Wysocki | 303bfdb | 2013-05-23 10:43:13 +0200 | [diff] [blame] | 384 | /* |
| 385 | * Carry out two passes here and ignore errors in the first pass, |
| 386 | * because if the devices in question are memory blocks and |
| 387 | * CONFIG_MEMCG is set, one of the blocks may hold data structures |
| 388 | * that the other blocks depend on, but it is not known in advance which |
| 389 | * block holds them. |
| 390 | * |
| 391 | * If the first pass is successful, the second one isn't needed, though. |
| 392 | */ |
Rafael J. Wysocki | 7f28dde | 2013-11-07 01:41:14 +0100 | [diff] [blame] | 393 | status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, |
| 394 | NULL, acpi_bus_offline, (void *)false, |
| 395 | (void **)&errdev); |
| 396 | if (status == AE_SUPPORT) { |
| 397 | dev_warn(errdev, "Offline disabled.\n"); |
| 398 | acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, |
| 399 | acpi_bus_online, NULL, NULL, NULL); |
Rafael J. Wysocki | 7f28dde | 2013-11-07 01:41:14 +0100 | [diff] [blame] | 400 | return -EPERM; |
| 401 | } |
| 402 | acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev); |
Rafael J. Wysocki | 303bfdb | 2013-05-23 10:43:13 +0200 | [diff] [blame] | 403 | if (errdev) { |
| 404 | errdev = NULL; |
Rafael J. Wysocki | 683058e | 2013-05-03 00:26:16 +0200 | [diff] [blame] | 405 | acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, |
Rafael J. Wysocki | 7f28dde | 2013-11-07 01:41:14 +0100 | [diff] [blame] | 406 | NULL, acpi_bus_offline, (void *)true, |
| 407 | (void **)&errdev); |
Rafael J. Wysocki | 303bfdb | 2013-05-23 10:43:13 +0200 | [diff] [blame] | 408 | if (!errdev || acpi_force_hot_remove) |
Rafael J. Wysocki | 7f28dde | 2013-11-07 01:41:14 +0100 | [diff] [blame] | 409 | acpi_bus_offline(handle, 0, (void *)true, |
| 410 | (void **)&errdev); |
Rafael J. Wysocki | 683058e | 2013-05-03 00:26:16 +0200 | [diff] [blame] | 411 | |
Rafael J. Wysocki | 303bfdb | 2013-05-23 10:43:13 +0200 | [diff] [blame] | 412 | if (errdev && !acpi_force_hot_remove) { |
| 413 | dev_warn(errdev, "Offline failed.\n"); |
Rafael J. Wysocki | 7f28dde | 2013-11-07 01:41:14 +0100 | [diff] [blame] | 414 | acpi_bus_online(handle, 0, NULL, NULL); |
Rafael J. Wysocki | 303bfdb | 2013-05-23 10:43:13 +0200 | [diff] [blame] | 415 | acpi_walk_namespace(ACPI_TYPE_ANY, handle, |
Rafael J. Wysocki | 7f28dde | 2013-11-07 01:41:14 +0100 | [diff] [blame] | 416 | ACPI_UINT32_MAX, acpi_bus_online, |
| 417 | NULL, NULL, NULL); |
Rafael J. Wysocki | 303bfdb | 2013-05-23 10:43:13 +0200 | [diff] [blame] | 418 | return -EBUSY; |
| 419 | } |
Rafael J. Wysocki | 683058e | 2013-05-03 00:26:16 +0200 | [diff] [blame] | 420 | } |
Rafael J. Wysocki | d22ddcb | 2013-12-29 15:25:35 +0100 | [diff] [blame] | 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static int acpi_scan_hot_remove(struct acpi_device *device) |
| 425 | { |
| 426 | acpi_handle handle = device->handle; |
| 427 | unsigned long long sta; |
| 428 | acpi_status status; |
| 429 | |
Tang Chen | dee1592 | 2014-08-08 10:30:45 +0800 | [diff] [blame] | 430 | if (device->handler && device->handler->hotplug.demand_offline |
| 431 | && !acpi_force_hot_remove) { |
Rafael J. Wysocki | caa73ea | 2013-12-29 15:25:48 +0100 | [diff] [blame] | 432 | if (!acpi_scan_is_offline(device, true)) |
Rafael J. Wysocki | d22ddcb | 2013-12-29 15:25:35 +0100 | [diff] [blame] | 433 | return -EBUSY; |
| 434 | } else { |
| 435 | int error = acpi_scan_try_to_offline(device); |
| 436 | if (error) |
| 437 | return error; |
| 438 | } |
Rafael J. Wysocki | 683058e | 2013-05-03 00:26:16 +0200 | [diff] [blame] | 439 | |
Zhang Rui | 26d4686 | 2008-04-29 02:35:48 -0400 | [diff] [blame] | 440 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
Kay Sievers | 0794469 | 2008-10-30 01:18:59 +0100 | [diff] [blame] | 441 | "Hot-removing device %s...\n", dev_name(&device->dev))); |
Zhang Rui | 26d4686 | 2008-04-29 02:35:48 -0400 | [diff] [blame] | 442 | |
Rafael J. Wysocki | bfee26d | 2013-01-26 00:27:44 +0100 | [diff] [blame] | 443 | acpi_bus_trim(device); |
Rafael J. Wysocki | 683058e | 2013-05-03 00:26:16 +0200 | [diff] [blame] | 444 | |
Jiang Liu | 7d2421f | 2013-06-29 00:24:40 +0800 | [diff] [blame] | 445 | acpi_evaluate_lck(handle, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | /* |
| 447 | * TBD: _EJD support. |
| 448 | */ |
Jiang Liu | 7d2421f | 2013-06-29 00:24:40 +0800 | [diff] [blame] | 449 | status = acpi_evaluate_ej0(handle); |
| 450 | if (status == AE_NOT_FOUND) |
| 451 | return -ENODEV; |
| 452 | else if (ACPI_FAILURE(status)) |
| 453 | return -EIO; |
Rafael J. Wysocki | f058cdf | 2013-02-09 15:29:11 +0100 | [diff] [blame] | 454 | |
Toshi Kani | 882fd12 | 2013-03-13 19:29:26 +0000 | [diff] [blame] | 455 | /* |
| 456 | * Verify if eject was indeed successful. If not, log an error |
| 457 | * message. No need to call _OST since _EJ0 call was made OK. |
| 458 | */ |
| 459 | status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); |
| 460 | if (ACPI_FAILURE(status)) { |
| 461 | acpi_handle_warn(handle, |
| 462 | "Status check after eject failed (0x%x)\n", status); |
| 463 | } else if (sta & ACPI_STA_DEVICE_ENABLED) { |
| 464 | acpi_handle_warn(handle, |
| 465 | "Eject incomplete - status 0x%llx\n", sta); |
| 466 | } |
| 467 | |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 468 | return 0; |
| 469 | } |
Rafael J. Wysocki | f058cdf | 2013-02-09 15:29:11 +0100 | [diff] [blame] | 470 | |
Rafael J. Wysocki | 443fc82 | 2013-11-22 21:55:43 +0100 | [diff] [blame] | 471 | static int acpi_scan_device_not_present(struct acpi_device *adev) |
| 472 | { |
| 473 | if (!acpi_device_enumerated(adev)) { |
| 474 | dev_warn(&adev->dev, "Still not present\n"); |
| 475 | return -EALREADY; |
| 476 | } |
| 477 | acpi_bus_trim(adev); |
| 478 | return 0; |
| 479 | } |
| 480 | |
Rafael J. Wysocki | c27b2c3 | 2013-11-22 21:55:07 +0100 | [diff] [blame] | 481 | static int acpi_scan_device_check(struct acpi_device *adev) |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 482 | { |
Rafael J. Wysocki | f943db4 | 2013-08-28 21:41:07 +0200 | [diff] [blame] | 483 | int error; |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 484 | |
Rafael J. Wysocki | 443fc82 | 2013-11-22 21:55:43 +0100 | [diff] [blame] | 485 | acpi_bus_get_status(adev); |
| 486 | if (adev->status.present || adev->status.functional) { |
| 487 | /* |
| 488 | * This function is only called for device objects for which |
| 489 | * matching scan handlers exist. The only situation in which |
| 490 | * the scan handler is not attached to this device object yet |
| 491 | * is when the device has just appeared (either it wasn't |
| 492 | * present at all before or it was removed and then added |
| 493 | * again). |
| 494 | */ |
| 495 | if (adev->handler) { |
| 496 | dev_warn(&adev->dev, "Already enumerated\n"); |
| 497 | return -EALREADY; |
| 498 | } |
| 499 | error = acpi_bus_scan(adev->handle); |
| 500 | if (error) { |
| 501 | dev_warn(&adev->dev, "Namespace scan failure\n"); |
| 502 | return error; |
| 503 | } |
| 504 | if (!adev->handler) { |
| 505 | dev_warn(&adev->dev, "Enumeration failure\n"); |
| 506 | error = -ENODEV; |
| 507 | } |
| 508 | } else { |
| 509 | error = acpi_scan_device_not_present(adev); |
| 510 | } |
| 511 | return error; |
| 512 | } |
| 513 | |
| 514 | static int acpi_scan_bus_check(struct acpi_device *adev) |
| 515 | { |
| 516 | struct acpi_scan_handler *handler = adev->handler; |
| 517 | struct acpi_device *child; |
| 518 | int error; |
| 519 | |
| 520 | acpi_bus_get_status(adev); |
| 521 | if (!(adev->status.present || adev->status.functional)) { |
| 522 | acpi_scan_device_not_present(adev); |
| 523 | return 0; |
| 524 | } |
| 525 | if (handler && handler->hotplug.scan_dependent) |
| 526 | return handler->hotplug.scan_dependent(adev); |
| 527 | |
Rafael J. Wysocki | c27b2c3 | 2013-11-22 21:55:07 +0100 | [diff] [blame] | 528 | error = acpi_bus_scan(adev->handle); |
| 529 | if (error) { |
| 530 | dev_warn(&adev->dev, "Namespace scan failure\n"); |
| 531 | return error; |
| 532 | } |
Rafael J. Wysocki | 443fc82 | 2013-11-22 21:55:43 +0100 | [diff] [blame] | 533 | list_for_each_entry(child, &adev->children, node) { |
| 534 | error = acpi_scan_bus_check(child); |
| 535 | if (error) |
| 536 | return error; |
Rafael J. Wysocki | c27b2c3 | 2013-11-22 21:55:07 +0100 | [diff] [blame] | 537 | } |
| 538 | return 0; |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 539 | } |
| 540 | |
Rafael J. Wysocki | 3c2cc7f | 2014-02-06 17:31:37 +0100 | [diff] [blame] | 541 | static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type) |
| 542 | { |
| 543 | switch (type) { |
| 544 | case ACPI_NOTIFY_BUS_CHECK: |
| 545 | return acpi_scan_bus_check(adev); |
| 546 | case ACPI_NOTIFY_DEVICE_CHECK: |
| 547 | return acpi_scan_device_check(adev); |
| 548 | case ACPI_NOTIFY_EJECT_REQUEST: |
| 549 | case ACPI_OST_EC_OSPM_EJECT: |
Rafael J. Wysocki | dd2151b | 2014-02-04 00:44:02 +0100 | [diff] [blame] | 550 | if (adev->handler && !adev->handler->hotplug.enabled) { |
| 551 | dev_info(&adev->dev, "Eject disabled\n"); |
| 552 | return -EPERM; |
| 553 | } |
Rafael J. Wysocki | 700b842 | 2014-02-21 01:07:17 +0100 | [diff] [blame] | 554 | acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST, |
| 555 | ACPI_OST_SC_EJECT_IN_PROGRESS, NULL); |
Rafael J. Wysocki | 3c2cc7f | 2014-02-06 17:31:37 +0100 | [diff] [blame] | 556 | return acpi_scan_hot_remove(adev); |
| 557 | } |
| 558 | return -EINVAL; |
| 559 | } |
| 560 | |
Rafael J. Wysocki | 1e3bcb5 | 2014-03-03 00:40:38 +0100 | [diff] [blame] | 561 | void acpi_device_hotplug(struct acpi_device *adev, u32 src) |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 562 | { |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 563 | u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; |
Rafael J. Wysocki | 3c2cc7f | 2014-02-06 17:31:37 +0100 | [diff] [blame] | 564 | int error = -ENODEV; |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 565 | |
Rafael J. Wysocki | 683058e | 2013-05-03 00:26:16 +0200 | [diff] [blame] | 566 | lock_device_hotplug(); |
Rafael J. Wysocki | e0ae8fe | 2013-08-30 14:19:29 +0200 | [diff] [blame] | 567 | mutex_lock(&acpi_scan_lock); |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 568 | |
Rafael J. Wysocki | c27b2c3 | 2013-11-22 21:55:07 +0100 | [diff] [blame] | 569 | /* |
| 570 | * The device object's ACPI handle cannot become invalid as long as we |
Rafael J. Wysocki | 3c2cc7f | 2014-02-06 17:31:37 +0100 | [diff] [blame] | 571 | * are holding acpi_scan_lock, but it might have become invalid before |
Rafael J. Wysocki | c27b2c3 | 2013-11-22 21:55:07 +0100 | [diff] [blame] | 572 | * that lock was acquired. |
| 573 | */ |
| 574 | if (adev->handle == INVALID_ACPI_HANDLE) |
Rafael J. Wysocki | 3c2cc7f | 2014-02-06 17:31:37 +0100 | [diff] [blame] | 575 | goto err_out; |
Rafael J. Wysocki | c27b2c3 | 2013-11-22 21:55:07 +0100 | [diff] [blame] | 576 | |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 577 | if (adev->flags.is_dock_station) { |
| 578 | error = dock_notify(adev, src); |
| 579 | } else if (adev->flags.hotplug_notify) { |
Rafael J. Wysocki | 3c2cc7f | 2014-02-06 17:31:37 +0100 | [diff] [blame] | 580 | error = acpi_generic_hotplug_event(adev, src); |
Rafael J. Wysocki | dd2151b | 2014-02-04 00:44:02 +0100 | [diff] [blame] | 581 | if (error == -EPERM) { |
| 582 | ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED; |
| 583 | goto err_out; |
| 584 | } |
Rafael J. Wysocki | 3c2cc7f | 2014-02-06 17:31:37 +0100 | [diff] [blame] | 585 | } else { |
Rafael J. Wysocki | be27b3d | 2014-02-21 01:10:27 +0100 | [diff] [blame] | 586 | int (*notify)(struct acpi_device *, u32); |
Rafael J. Wysocki | 3c2cc7f | 2014-02-06 17:31:37 +0100 | [diff] [blame] | 587 | |
| 588 | acpi_lock_hp_context(); |
Rafael J. Wysocki | be27b3d | 2014-02-21 01:10:27 +0100 | [diff] [blame] | 589 | notify = adev->hp ? adev->hp->notify : NULL; |
Rafael J. Wysocki | 3c2cc7f | 2014-02-06 17:31:37 +0100 | [diff] [blame] | 590 | acpi_unlock_hp_context(); |
| 591 | /* |
| 592 | * There may be additional notify handlers for device objects |
| 593 | * without the .event() callback, so ignore them here. |
| 594 | */ |
Rafael J. Wysocki | be27b3d | 2014-02-21 01:10:27 +0100 | [diff] [blame] | 595 | if (notify) |
| 596 | error = notify(adev, src); |
Rafael J. Wysocki | 3c2cc7f | 2014-02-06 17:31:37 +0100 | [diff] [blame] | 597 | else |
| 598 | goto out; |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 599 | } |
Rafael J. Wysocki | c27b2c3 | 2013-11-22 21:55:07 +0100 | [diff] [blame] | 600 | if (!error) |
| 601 | ost_code = ACPI_OST_SC_SUCCESS; |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 602 | |
Rafael J. Wysocki | 3c2cc7f | 2014-02-06 17:31:37 +0100 | [diff] [blame] | 603 | err_out: |
Rafael J. Wysocki | 700b842 | 2014-02-21 01:07:17 +0100 | [diff] [blame] | 604 | acpi_evaluate_ost(adev->handle, src, ost_code, NULL); |
Rafael J. Wysocki | 3c2cc7f | 2014-02-06 17:31:37 +0100 | [diff] [blame] | 605 | |
| 606 | out: |
Rafael J. Wysocki | 78ea463 | 2014-02-04 00:43:05 +0100 | [diff] [blame] | 607 | acpi_bus_put_acpi_device(adev); |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 608 | mutex_unlock(&acpi_scan_lock); |
Rafael J. Wysocki | e0ae8fe | 2013-08-30 14:19:29 +0200 | [diff] [blame] | 609 | unlock_device_hotplug(); |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 610 | } |
| 611 | |
Rafael J. Wysocki | 836aedb | 2013-01-24 12:49:59 +0100 | [diff] [blame] | 612 | static ssize_t real_power_state_show(struct device *dev, |
| 613 | struct device_attribute *attr, char *buf) |
| 614 | { |
| 615 | struct acpi_device *adev = to_acpi_device(dev); |
| 616 | int state; |
| 617 | int ret; |
| 618 | |
| 619 | ret = acpi_device_get_power(adev, &state); |
| 620 | if (ret) |
| 621 | return ret; |
| 622 | |
| 623 | return sprintf(buf, "%s\n", acpi_power_state_string(state)); |
| 624 | } |
| 625 | |
| 626 | static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL); |
| 627 | |
| 628 | static ssize_t power_state_show(struct device *dev, |
| 629 | struct device_attribute *attr, char *buf) |
| 630 | { |
| 631 | struct acpi_device *adev = to_acpi_device(dev); |
| 632 | |
| 633 | return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state)); |
| 634 | } |
| 635 | |
| 636 | static DEVICE_ATTR(power_state, 0444, power_state_show, NULL); |
| 637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | static ssize_t |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 639 | acpi_eject_store(struct device *d, struct device_attribute *attr, |
| 640 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | { |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 642 | struct acpi_device *acpi_device = to_acpi_device(d); |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 643 | acpi_object_type not_used; |
| 644 | acpi_status status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 646 | if (!count || buf[0] != '1') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 649 | if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled) |
| 650 | && !acpi_device->driver) |
| 651 | return -ENODEV; |
| 652 | |
| 653 | status = acpi_get_type(acpi_device->handle, ¬_used); |
| 654 | if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable) |
| 655 | return -ENODEV; |
| 656 | |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 657 | get_device(&acpi_device->dev); |
Rafael J. Wysocki | 1e3bcb5 | 2014-03-03 00:40:38 +0100 | [diff] [blame] | 658 | status = acpi_hotplug_schedule(acpi_device, ACPI_OST_EC_OSPM_EJECT); |
Rafael J. Wysocki | f943db4 | 2013-08-28 21:41:07 +0200 | [diff] [blame] | 659 | if (ACPI_SUCCESS(status)) |
| 660 | return count; |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 661 | |
Rafael J. Wysocki | f943db4 | 2013-08-28 21:41:07 +0200 | [diff] [blame] | 662 | put_device(&acpi_device->dev); |
Rafael J. Wysocki | 700b842 | 2014-02-21 01:07:17 +0100 | [diff] [blame] | 663 | acpi_evaluate_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT, |
| 664 | ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL); |
Rafael J. Wysocki | 5add99c | 2013-11-07 01:41:39 +0100 | [diff] [blame] | 665 | return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 668 | static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 670 | static ssize_t |
| 671 | acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) { |
| 672 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
Bjorn Helgaas | ea8d82f | 2009-09-21 13:35:09 -0600 | [diff] [blame] | 674 | return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev)); |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 675 | } |
| 676 | static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL); |
| 677 | |
Lv Zheng | 923d4a4 | 2012-10-30 14:43:26 +0100 | [diff] [blame] | 678 | static ssize_t acpi_device_uid_show(struct device *dev, |
| 679 | struct device_attribute *attr, char *buf) |
| 680 | { |
| 681 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 682 | |
| 683 | return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id); |
| 684 | } |
| 685 | static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL); |
| 686 | |
| 687 | static ssize_t acpi_device_adr_show(struct device *dev, |
| 688 | struct device_attribute *attr, char *buf) |
| 689 | { |
| 690 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 691 | |
| 692 | return sprintf(buf, "0x%08x\n", |
| 693 | (unsigned int)(acpi_dev->pnp.bus_address)); |
| 694 | } |
| 695 | static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL); |
| 696 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 697 | static ssize_t |
| 698 | acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) { |
| 699 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 700 | struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL}; |
| 701 | int result; |
| 702 | |
| 703 | result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path); |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 704 | if (result) |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 705 | goto end; |
| 706 | |
| 707 | result = sprintf(buf, "%s\n", (char*)path.pointer); |
| 708 | kfree(path.pointer); |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 709 | end: |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 710 | return result; |
| 711 | } |
| 712 | static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL); |
| 713 | |
Lance Ortiz | d1efe3c | 2012-10-02 12:43:23 -0600 | [diff] [blame] | 714 | /* sysfs file that shows description text from the ACPI _STR method */ |
| 715 | static ssize_t description_show(struct device *dev, |
| 716 | struct device_attribute *attr, |
| 717 | char *buf) { |
| 718 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 719 | int result; |
| 720 | |
| 721 | if (acpi_dev->pnp.str_obj == NULL) |
| 722 | return 0; |
| 723 | |
| 724 | /* |
| 725 | * The _STR object contains a Unicode identifier for a device. |
| 726 | * We need to convert to utf-8 so it can be displayed. |
| 727 | */ |
| 728 | result = utf16s_to_utf8s( |
| 729 | (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer, |
| 730 | acpi_dev->pnp.str_obj->buffer.length, |
| 731 | UTF16_LITTLE_ENDIAN, buf, |
| 732 | PAGE_SIZE); |
| 733 | |
| 734 | buf[result++] = '\n'; |
| 735 | |
| 736 | return result; |
| 737 | } |
| 738 | static DEVICE_ATTR(description, 0444, description_show, NULL); |
| 739 | |
Yasuaki Ishimatsu | bb74ac2 | 2012-11-16 02:56:59 +0100 | [diff] [blame] | 740 | static ssize_t |
| 741 | acpi_device_sun_show(struct device *dev, struct device_attribute *attr, |
| 742 | char *buf) { |
| 743 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
Yasuaki Ishimatsu | a383b68 | 2014-09-03 13:39:13 +0900 | [diff] [blame] | 744 | acpi_status status; |
| 745 | unsigned long long sun; |
Yasuaki Ishimatsu | bb74ac2 | 2012-11-16 02:56:59 +0100 | [diff] [blame] | 746 | |
Yasuaki Ishimatsu | a383b68 | 2014-09-03 13:39:13 +0900 | [diff] [blame] | 747 | status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun); |
| 748 | if (ACPI_FAILURE(status)) |
| 749 | return -ENODEV; |
| 750 | |
| 751 | return sprintf(buf, "%llu\n", sun); |
Yasuaki Ishimatsu | bb74ac2 | 2012-11-16 02:56:59 +0100 | [diff] [blame] | 752 | } |
| 753 | static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL); |
| 754 | |
Srinivas Pandruvada | c713cd7 | 2014-01-10 16:00:05 -0800 | [diff] [blame] | 755 | static ssize_t status_show(struct device *dev, struct device_attribute *attr, |
| 756 | char *buf) { |
| 757 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 758 | acpi_status status; |
| 759 | unsigned long long sta; |
| 760 | |
| 761 | status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta); |
| 762 | if (ACPI_FAILURE(status)) |
| 763 | return -ENODEV; |
| 764 | |
| 765 | return sprintf(buf, "%llu\n", sta); |
| 766 | } |
| 767 | static DEVICE_ATTR_RO(status); |
| 768 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 769 | static int acpi_device_setup_files(struct acpi_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | { |
Lance Ortiz | d1efe3c | 2012-10-02 12:43:23 -0600 | [diff] [blame] | 771 | struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 772 | acpi_status status; |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 773 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 775 | /* |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 776 | * Devices gotten from FADT don't have a "path" attribute |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 777 | */ |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 778 | if (dev->handle) { |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 779 | result = device_create_file(&dev->dev, &dev_attr_path); |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 780 | if (result) |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 781 | goto end; |
| 782 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
Thomas Renninger | 2b2ae7c | 2010-10-01 10:53:59 +0200 | [diff] [blame] | 784 | if (!list_empty(&dev->pnp.ids)) { |
| 785 | result = device_create_file(&dev->dev, &dev_attr_hid); |
| 786 | if (result) |
| 787 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | |
Thomas Renninger | 2b2ae7c | 2010-10-01 10:53:59 +0200 | [diff] [blame] | 789 | result = device_create_file(&dev->dev, &dev_attr_modalias); |
| 790 | if (result) |
| 791 | goto end; |
| 792 | } |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 793 | |
Lance Ortiz | d1efe3c | 2012-10-02 12:43:23 -0600 | [diff] [blame] | 794 | /* |
| 795 | * If device has _STR, 'description' file is created |
| 796 | */ |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 797 | if (acpi_has_method(dev->handle, "_STR")) { |
Lance Ortiz | d1efe3c | 2012-10-02 12:43:23 -0600 | [diff] [blame] | 798 | status = acpi_evaluate_object(dev->handle, "_STR", |
| 799 | NULL, &buffer); |
| 800 | if (ACPI_FAILURE(status)) |
| 801 | buffer.pointer = NULL; |
| 802 | dev->pnp.str_obj = buffer.pointer; |
| 803 | result = device_create_file(&dev->dev, &dev_attr_description); |
| 804 | if (result) |
| 805 | goto end; |
| 806 | } |
| 807 | |
Toshi Kani | d4e1a69 | 2013-03-04 21:30:41 +0000 | [diff] [blame] | 808 | if (dev->pnp.type.bus_address) |
Lv Zheng | 923d4a4 | 2012-10-30 14:43:26 +0100 | [diff] [blame] | 809 | result = device_create_file(&dev->dev, &dev_attr_adr); |
| 810 | if (dev->pnp.unique_id) |
| 811 | result = device_create_file(&dev->dev, &dev_attr_uid); |
| 812 | |
Yasuaki Ishimatsu | a383b68 | 2014-09-03 13:39:13 +0900 | [diff] [blame] | 813 | if (acpi_has_method(dev->handle, "_SUN")) { |
Yasuaki Ishimatsu | bb74ac2 | 2012-11-16 02:56:59 +0100 | [diff] [blame] | 814 | result = device_create_file(&dev->dev, &dev_attr_sun); |
| 815 | if (result) |
| 816 | goto end; |
Yasuaki Ishimatsu | bb74ac2 | 2012-11-16 02:56:59 +0100 | [diff] [blame] | 817 | } |
| 818 | |
Srinivas Pandruvada | c713cd7 | 2014-01-10 16:00:05 -0800 | [diff] [blame] | 819 | if (acpi_has_method(dev->handle, "_STA")) { |
| 820 | result = device_create_file(&dev->dev, &dev_attr_status); |
| 821 | if (result) |
| 822 | goto end; |
| 823 | } |
| 824 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 825 | /* |
| 826 | * If device has _EJ0, 'eject' file is created that is used to trigger |
| 827 | * hot-removal function from userland. |
| 828 | */ |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 829 | if (acpi_has_method(dev->handle, "_EJ0")) { |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 830 | result = device_create_file(&dev->dev, &dev_attr_eject); |
Rafael J. Wysocki | 836aedb | 2013-01-24 12:49:59 +0100 | [diff] [blame] | 831 | if (result) |
| 832 | return result; |
| 833 | } |
| 834 | |
| 835 | if (dev->flags.power_manageable) { |
| 836 | result = device_create_file(&dev->dev, &dev_attr_power_state); |
| 837 | if (result) |
| 838 | return result; |
| 839 | |
| 840 | if (dev->power.flags.power_resources) |
| 841 | result = device_create_file(&dev->dev, |
| 842 | &dev_attr_real_power_state); |
| 843 | } |
| 844 | |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 845 | end: |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 846 | return result; |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 847 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 849 | static void acpi_device_remove_files(struct acpi_device *dev) |
| 850 | { |
Rafael J. Wysocki | 836aedb | 2013-01-24 12:49:59 +0100 | [diff] [blame] | 851 | if (dev->flags.power_manageable) { |
| 852 | device_remove_file(&dev->dev, &dev_attr_power_state); |
| 853 | if (dev->power.flags.power_resources) |
| 854 | device_remove_file(&dev->dev, |
| 855 | &dev_attr_real_power_state); |
| 856 | } |
| 857 | |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 858 | /* |
Lance Ortiz | d1efe3c | 2012-10-02 12:43:23 -0600 | [diff] [blame] | 859 | * If device has _STR, remove 'description' file |
| 860 | */ |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 861 | if (acpi_has_method(dev->handle, "_STR")) { |
Lance Ortiz | d1efe3c | 2012-10-02 12:43:23 -0600 | [diff] [blame] | 862 | kfree(dev->pnp.str_obj); |
| 863 | device_remove_file(&dev->dev, &dev_attr_description); |
| 864 | } |
| 865 | /* |
| 866 | * If device has _EJ0, remove 'eject' file. |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 867 | */ |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 868 | if (acpi_has_method(dev->handle, "_EJ0")) |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 869 | device_remove_file(&dev->dev, &dev_attr_eject); |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 870 | |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 871 | if (acpi_has_method(dev->handle, "_SUN")) |
Yasuaki Ishimatsu | bb74ac2 | 2012-11-16 02:56:59 +0100 | [diff] [blame] | 872 | device_remove_file(&dev->dev, &dev_attr_sun); |
| 873 | |
Lv Zheng | 923d4a4 | 2012-10-30 14:43:26 +0100 | [diff] [blame] | 874 | if (dev->pnp.unique_id) |
| 875 | device_remove_file(&dev->dev, &dev_attr_uid); |
Toshi Kani | d4e1a69 | 2013-03-04 21:30:41 +0000 | [diff] [blame] | 876 | if (dev->pnp.type.bus_address) |
Lv Zheng | 923d4a4 | 2012-10-30 14:43:26 +0100 | [diff] [blame] | 877 | device_remove_file(&dev->dev, &dev_attr_adr); |
Bjorn Helgaas | 1131b93 | 2009-09-21 13:35:29 -0600 | [diff] [blame] | 878 | device_remove_file(&dev->dev, &dev_attr_modalias); |
| 879 | device_remove_file(&dev->dev, &dev_attr_hid); |
Srinivas Pandruvada | c713cd7 | 2014-01-10 16:00:05 -0800 | [diff] [blame] | 880 | if (acpi_has_method(dev->handle, "_STA")) |
| 881 | device_remove_file(&dev->dev, &dev_attr_status); |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 882 | if (dev->handle) |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 883 | device_remove_file(&dev->dev, &dev_attr_path); |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 884 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | /* -------------------------------------------------------------------------- |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 886 | ACPI Bus operations |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | -------------------------------------------------------------------------- */ |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 888 | |
Mika Westerberg | cf761af | 2012-10-31 22:44:41 +0100 | [diff] [blame] | 889 | static const struct acpi_device_id *__acpi_match_device( |
| 890 | struct acpi_device *device, const struct acpi_device_id *ids) |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 891 | { |
| 892 | const struct acpi_device_id *id; |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 893 | struct acpi_hardware_id *hwid; |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 894 | |
Zhao Yakui | 39a0ad8 | 2008-08-11 13:40:22 +0800 | [diff] [blame] | 895 | /* |
| 896 | * If the device is not present, it is unnecessary to load device |
| 897 | * driver for it. |
| 898 | */ |
| 899 | if (!device->status.present) |
Mika Westerberg | cf761af | 2012-10-31 22:44:41 +0100 | [diff] [blame] | 900 | return NULL; |
Zhao Yakui | 39a0ad8 | 2008-08-11 13:40:22 +0800 | [diff] [blame] | 901 | |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 902 | for (id = ids; id->id[0]; id++) |
| 903 | list_for_each_entry(hwid, &device->pnp.ids, list) |
| 904 | if (!strcmp((char *) id->id, hwid->id)) |
Mika Westerberg | cf761af | 2012-10-31 22:44:41 +0100 | [diff] [blame] | 905 | return id; |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 906 | |
Mika Westerberg | cf761af | 2012-10-31 22:44:41 +0100 | [diff] [blame] | 907 | return NULL; |
| 908 | } |
| 909 | |
| 910 | /** |
| 911 | * acpi_match_device - Match a struct device against a given list of ACPI IDs |
| 912 | * @ids: Array of struct acpi_device_id object to match against. |
| 913 | * @dev: The device structure to match. |
| 914 | * |
| 915 | * Check if @dev has a valid ACPI handle and if there is a struct acpi_device |
| 916 | * object for that handle and use that object to match against a given list of |
| 917 | * device IDs. |
| 918 | * |
| 919 | * Return a pointer to the first matching ID on success or %NULL on failure. |
| 920 | */ |
| 921 | const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids, |
| 922 | const struct device *dev) |
| 923 | { |
| 924 | struct acpi_device *adev; |
Rafael J. Wysocki | 0613e1f | 2013-01-31 20:54:05 +0100 | [diff] [blame] | 925 | acpi_handle handle = ACPI_HANDLE(dev); |
Mika Westerberg | cf761af | 2012-10-31 22:44:41 +0100 | [diff] [blame] | 926 | |
Rafael J. Wysocki | 0613e1f | 2013-01-31 20:54:05 +0100 | [diff] [blame] | 927 | if (!ids || !handle || acpi_bus_get_device(handle, &adev)) |
Mika Westerberg | cf761af | 2012-10-31 22:44:41 +0100 | [diff] [blame] | 928 | return NULL; |
| 929 | |
Mika Westerberg | 5287078 | 2014-10-24 16:40:54 +0200 | [diff] [blame] | 930 | if (!acpi_companion_match(dev)) |
| 931 | return NULL; |
| 932 | |
Mika Westerberg | cf761af | 2012-10-31 22:44:41 +0100 | [diff] [blame] | 933 | return __acpi_match_device(adev, ids); |
| 934 | } |
| 935 | EXPORT_SYMBOL_GPL(acpi_match_device); |
| 936 | |
| 937 | int acpi_match_device_ids(struct acpi_device *device, |
| 938 | const struct acpi_device_id *ids) |
| 939 | { |
| 940 | return __acpi_match_device(device, ids) ? 0 : -ENOENT; |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 941 | } |
| 942 | EXPORT_SYMBOL(acpi_match_device_ids); |
| 943 | |
Mika Westerberg | 733e625 | 2014-10-21 13:33:56 +0200 | [diff] [blame^] | 944 | /* Performs match against special "PRP0001" shoehorn ACPI ID */ |
| 945 | static bool acpi_of_driver_match_device(struct device *dev, |
| 946 | const struct device_driver *drv) |
| 947 | { |
| 948 | const union acpi_object *of_compatible, *obj; |
| 949 | struct acpi_device *adev; |
| 950 | int i, nval; |
| 951 | |
| 952 | adev = ACPI_COMPANION(dev); |
| 953 | if (!adev) |
| 954 | return false; |
| 955 | |
| 956 | of_compatible = adev->data.of_compatible; |
| 957 | if (!drv->of_match_table || !of_compatible) |
| 958 | return false; |
| 959 | |
| 960 | if (of_compatible->type == ACPI_TYPE_PACKAGE) { |
| 961 | nval = of_compatible->package.count; |
| 962 | obj = of_compatible->package.elements; |
| 963 | } else { /* Must be ACPI_TYPE_STRING. */ |
| 964 | nval = 1; |
| 965 | obj = of_compatible; |
| 966 | } |
| 967 | /* Now we can look for the driver DT compatible strings */ |
| 968 | for (i = 0; i < nval; i++, obj++) { |
| 969 | const struct of_device_id *id; |
| 970 | |
| 971 | for (id = drv->of_match_table; id->compatible[0]; id++) |
| 972 | if (!strcasecmp(obj->string.pointer, id->compatible)) |
| 973 | return true; |
| 974 | } |
| 975 | |
| 976 | return false; |
| 977 | } |
| 978 | |
| 979 | bool acpi_driver_match_device(struct device *dev, |
| 980 | const struct device_driver *drv) |
| 981 | { |
| 982 | if (!drv->acpi_match_table) |
| 983 | return acpi_of_driver_match_device(dev, drv); |
| 984 | |
| 985 | return !!acpi_match_device(drv->acpi_match_table, dev); |
| 986 | } |
| 987 | EXPORT_SYMBOL_GPL(acpi_driver_match_device); |
| 988 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 989 | static void acpi_free_power_resources_lists(struct acpi_device *device) |
| 990 | { |
| 991 | int i; |
| 992 | |
Rafael J. Wysocki | 993cbe5 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 993 | if (device->wakeup.flags.valid) |
| 994 | acpi_power_resources_list_free(&device->wakeup.resources); |
| 995 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 996 | if (!device->flags.power_manageable) |
| 997 | return; |
| 998 | |
| 999 | for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { |
| 1000 | struct acpi_device_power_state *ps = &device->power.states[i]; |
| 1001 | acpi_power_resources_list_free(&ps->resources); |
| 1002 | } |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 1003 | } |
| 1004 | |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 1005 | static void acpi_device_release(struct device *dev) |
| 1006 | { |
| 1007 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 1008 | |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 1009 | acpi_free_properties(acpi_dev); |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1010 | acpi_free_pnp_ids(&acpi_dev->pnp); |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1011 | acpi_free_power_resources_lists(acpi_dev); |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 1012 | kfree(acpi_dev); |
| 1013 | } |
| 1014 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 1015 | static int acpi_bus_match(struct device *dev, struct device_driver *drv) |
| 1016 | { |
| 1017 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 1018 | struct acpi_driver *acpi_drv = to_acpi_driver(drv); |
| 1019 | |
Rafael J. Wysocki | 209d3b1 | 2012-12-21 00:36:48 +0100 | [diff] [blame] | 1020 | return acpi_dev->flags.match_driver |
Rafael J. Wysocki | 805d410 | 2012-12-21 00:36:39 +0100 | [diff] [blame] | 1021 | && !acpi_match_device_ids(acpi_dev, acpi_drv->ids); |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 1022 | } |
| 1023 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1024 | static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 1025 | { |
| 1026 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1027 | int len; |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 1028 | |
Thomas Renninger | 2b2ae7c | 2010-10-01 10:53:59 +0200 | [diff] [blame] | 1029 | if (list_empty(&acpi_dev->pnp.ids)) |
| 1030 | return 0; |
| 1031 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1032 | if (add_uevent_var(env, "MODALIAS=")) |
| 1033 | return -ENOMEM; |
| 1034 | len = create_modalias(acpi_dev, &env->buf[env->buflen - 1], |
| 1035 | sizeof(env->buf) - env->buflen); |
Zhang Rui | 3d8e009 | 2014-01-14 16:46:35 +0800 | [diff] [blame] | 1036 | if (len <= 0) |
| 1037 | return len; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 1038 | env->buflen += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | return 0; |
| 1040 | } |
| 1041 | |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 1042 | static void acpi_device_notify(acpi_handle handle, u32 event, void *data) |
| 1043 | { |
| 1044 | struct acpi_device *device = data; |
| 1045 | |
| 1046 | device->driver->ops.notify(device, event); |
| 1047 | } |
| 1048 | |
Lan Tianyu | 236105d | 2014-08-26 01:29:24 +0200 | [diff] [blame] | 1049 | static void acpi_device_notify_fixed(void *data) |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 1050 | { |
| 1051 | struct acpi_device *device = data; |
| 1052 | |
Bjorn Helgaas | 53de535 | 2009-08-31 22:32:20 +0000 | [diff] [blame] | 1053 | /* Fixed hardware devices have no handles */ |
| 1054 | acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device); |
Lan Tianyu | 236105d | 2014-08-26 01:29:24 +0200 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | static acpi_status acpi_device_fixed_event(void *data) |
| 1058 | { |
| 1059 | acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_device_notify_fixed, data); |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 1060 | return AE_OK; |
| 1061 | } |
| 1062 | |
| 1063 | static int acpi_device_install_notify_handler(struct acpi_device *device) |
| 1064 | { |
| 1065 | acpi_status status; |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 1066 | |
Bjorn Helgaas | ccba2a3 | 2009-09-21 19:29:15 +0000 | [diff] [blame] | 1067 | if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON) |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 1068 | status = |
| 1069 | acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON, |
Lan Tianyu | 236105d | 2014-08-26 01:29:24 +0200 | [diff] [blame] | 1070 | acpi_device_fixed_event, |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 1071 | device); |
Bjorn Helgaas | ccba2a3 | 2009-09-21 19:29:15 +0000 | [diff] [blame] | 1072 | else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON) |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 1073 | status = |
| 1074 | acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON, |
Lan Tianyu | 236105d | 2014-08-26 01:29:24 +0200 | [diff] [blame] | 1075 | acpi_device_fixed_event, |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 1076 | device); |
| 1077 | else |
| 1078 | status = acpi_install_notify_handler(device->handle, |
| 1079 | ACPI_DEVICE_NOTIFY, |
| 1080 | acpi_device_notify, |
| 1081 | device); |
| 1082 | |
| 1083 | if (ACPI_FAILURE(status)) |
| 1084 | return -EINVAL; |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | static void acpi_device_remove_notify_handler(struct acpi_device *device) |
| 1089 | { |
Bjorn Helgaas | ccba2a3 | 2009-09-21 19:29:15 +0000 | [diff] [blame] | 1090 | if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON) |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 1091 | acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON, |
Lan Tianyu | 236105d | 2014-08-26 01:29:24 +0200 | [diff] [blame] | 1092 | acpi_device_fixed_event); |
Bjorn Helgaas | ccba2a3 | 2009-09-21 19:29:15 +0000 | [diff] [blame] | 1093 | else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON) |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 1094 | acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON, |
Lan Tianyu | 236105d | 2014-08-26 01:29:24 +0200 | [diff] [blame] | 1095 | acpi_device_fixed_event); |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 1096 | else |
| 1097 | acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY, |
| 1098 | acpi_device_notify); |
| 1099 | } |
| 1100 | |
Rafael J. Wysocki | d9e455f | 2013-06-10 13:00:50 +0200 | [diff] [blame] | 1101 | static int acpi_device_probe(struct device *dev) |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 1102 | { |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 1103 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 1104 | struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver); |
| 1105 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | |
Rafael J. Wysocki | fc2e0a8 | 2014-08-26 01:29:21 +0200 | [diff] [blame] | 1107 | if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev)) |
Rafael J. Wysocki | 24071f4 | 2013-06-16 00:36:41 +0200 | [diff] [blame] | 1108 | return -EINVAL; |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 1109 | |
Rafael J. Wysocki | d9e455f | 2013-06-10 13:00:50 +0200 | [diff] [blame] | 1110 | if (!acpi_drv->ops.add) |
| 1111 | return -ENOSYS; |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1112 | |
Rafael J. Wysocki | d9e455f | 2013-06-10 13:00:50 +0200 | [diff] [blame] | 1113 | ret = acpi_drv->ops.add(acpi_dev); |
| 1114 | if (ret) |
| 1115 | return ret; |
| 1116 | |
| 1117 | acpi_dev->driver = acpi_drv; |
| 1118 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 1119 | "Driver [%s] successfully bound to device [%s]\n", |
| 1120 | acpi_drv->name, acpi_dev->pnp.bus_id)); |
| 1121 | |
| 1122 | if (acpi_drv->ops.notify) { |
| 1123 | ret = acpi_device_install_notify_handler(acpi_dev); |
| 1124 | if (ret) { |
| 1125 | if (acpi_drv->ops.remove) |
| 1126 | acpi_drv->ops.remove(acpi_dev); |
| 1127 | |
| 1128 | acpi_dev->driver = NULL; |
| 1129 | acpi_dev->driver_data = NULL; |
| 1130 | return ret; |
| 1131 | } |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 1132 | } |
Rafael J. Wysocki | d9e455f | 2013-06-10 13:00:50 +0200 | [diff] [blame] | 1133 | |
| 1134 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n", |
| 1135 | acpi_drv->name, acpi_dev->pnp.bus_id)); |
| 1136 | get_device(dev); |
| 1137 | return 0; |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | static int acpi_device_remove(struct device * dev) |
| 1141 | { |
| 1142 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 1143 | struct acpi_driver *acpi_drv = acpi_dev->driver; |
| 1144 | |
| 1145 | if (acpi_drv) { |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 1146 | if (acpi_drv->ops.notify) |
| 1147 | acpi_device_remove_notify_handler(acpi_dev); |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 1148 | if (acpi_drv->ops.remove) |
Rafael J. Wysocki | 51fac83 | 2013-01-24 00:24:48 +0100 | [diff] [blame] | 1149 | acpi_drv->ops.remove(acpi_dev); |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 1150 | } |
| 1151 | acpi_dev->driver = NULL; |
Pavel Machek | db89b4f | 2008-09-22 14:37:34 -0700 | [diff] [blame] | 1152 | acpi_dev->driver_data = NULL; |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 1153 | |
| 1154 | put_device(dev); |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 1155 | return 0; |
| 1156 | } |
| 1157 | |
David Brownell | 55955aa | 2007-05-08 00:28:35 -0700 | [diff] [blame] | 1158 | struct bus_type acpi_bus_type = { |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 1159 | .name = "acpi", |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 1160 | .match = acpi_bus_match, |
| 1161 | .probe = acpi_device_probe, |
| 1162 | .remove = acpi_device_remove, |
| 1163 | .uevent = acpi_device_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | }; |
| 1165 | |
Rafael J. Wysocki | d783156 | 2013-11-22 21:52:12 +0100 | [diff] [blame] | 1166 | static void acpi_device_del(struct acpi_device *device) |
Rafael J. Wysocki | caf5c03 | 2013-07-30 14:38:34 +0200 | [diff] [blame] | 1167 | { |
Rafael J. Wysocki | d783156 | 2013-11-22 21:52:12 +0100 | [diff] [blame] | 1168 | mutex_lock(&acpi_device_lock); |
| 1169 | if (device->parent) |
| 1170 | list_del(&device->node); |
| 1171 | |
| 1172 | list_del(&device->wakeup_list); |
| 1173 | mutex_unlock(&acpi_device_lock); |
| 1174 | |
| 1175 | acpi_power_add_remove_device(device, false); |
| 1176 | acpi_device_remove_files(device); |
| 1177 | if (device->remove) |
| 1178 | device->remove(device); |
| 1179 | |
| 1180 | device_del(&device->dev); |
| 1181 | } |
| 1182 | |
| 1183 | static LIST_HEAD(acpi_device_del_list); |
| 1184 | static DEFINE_MUTEX(acpi_device_del_lock); |
| 1185 | |
| 1186 | static void acpi_device_del_work_fn(struct work_struct *work_not_used) |
| 1187 | { |
| 1188 | for (;;) { |
| 1189 | struct acpi_device *adev; |
| 1190 | |
| 1191 | mutex_lock(&acpi_device_del_lock); |
| 1192 | |
| 1193 | if (list_empty(&acpi_device_del_list)) { |
| 1194 | mutex_unlock(&acpi_device_del_lock); |
| 1195 | break; |
| 1196 | } |
| 1197 | adev = list_first_entry(&acpi_device_del_list, |
| 1198 | struct acpi_device, del_list); |
| 1199 | list_del(&adev->del_list); |
| 1200 | |
| 1201 | mutex_unlock(&acpi_device_del_lock); |
| 1202 | |
| 1203 | acpi_device_del(adev); |
| 1204 | /* |
| 1205 | * Drop references to all power resources that might have been |
| 1206 | * used by the device. |
| 1207 | */ |
| 1208 | acpi_power_transition(adev, ACPI_STATE_D3_COLD); |
| 1209 | put_device(&adev->dev); |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * acpi_scan_drop_device - Drop an ACPI device object. |
| 1215 | * @handle: Handle of an ACPI namespace node, not used. |
| 1216 | * @context: Address of the ACPI device object to drop. |
| 1217 | * |
| 1218 | * This is invoked by acpi_ns_delete_node() during the removal of the ACPI |
| 1219 | * namespace node the device object pointed to by @context is attached to. |
| 1220 | * |
| 1221 | * The unregistration is carried out asynchronously to avoid running |
| 1222 | * acpi_device_del() under the ACPICA's namespace mutex and the list is used to |
| 1223 | * ensure the correct ordering (the device objects must be unregistered in the |
| 1224 | * same order in which the corresponding namespace nodes are deleted). |
| 1225 | */ |
| 1226 | static void acpi_scan_drop_device(acpi_handle handle, void *context) |
| 1227 | { |
| 1228 | static DECLARE_WORK(work, acpi_device_del_work_fn); |
| 1229 | struct acpi_device *adev = context; |
| 1230 | |
| 1231 | mutex_lock(&acpi_device_del_lock); |
| 1232 | |
| 1233 | /* |
| 1234 | * Use the ACPI hotplug workqueue which is ordered, so this work item |
| 1235 | * won't run after any hotplug work items submitted subsequently. That |
| 1236 | * prevents attempts to register device objects identical to those being |
| 1237 | * deleted from happening concurrently (such attempts result from |
| 1238 | * hotplug events handled via the ACPI hotplug workqueue). It also will |
| 1239 | * run after all of the work items submitted previosuly, which helps |
| 1240 | * those work items to ensure that they are not accessing stale device |
| 1241 | * objects. |
| 1242 | */ |
| 1243 | if (list_empty(&acpi_device_del_list)) |
| 1244 | acpi_queue_hotplug_work(&work); |
| 1245 | |
| 1246 | list_add_tail(&adev->del_list, &acpi_device_del_list); |
| 1247 | /* Make acpi_ns_validate_handle() return NULL for this handle. */ |
| 1248 | adev->handle = INVALID_ACPI_HANDLE; |
| 1249 | |
| 1250 | mutex_unlock(&acpi_device_del_lock); |
Rafael J. Wysocki | caf5c03 | 2013-07-30 14:38:34 +0200 | [diff] [blame] | 1251 | } |
| 1252 | |
Rafael J. Wysocki | 78ea463 | 2014-02-04 00:43:05 +0100 | [diff] [blame] | 1253 | static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device, |
| 1254 | void (*callback)(void *)) |
Rafael J. Wysocki | caf5c03 | 2013-07-30 14:38:34 +0200 | [diff] [blame] | 1255 | { |
| 1256 | acpi_status status; |
| 1257 | |
| 1258 | if (!device) |
| 1259 | return -EINVAL; |
| 1260 | |
Rafael J. Wysocki | 78ea463 | 2014-02-04 00:43:05 +0100 | [diff] [blame] | 1261 | status = acpi_get_data_full(handle, acpi_scan_drop_device, |
| 1262 | (void **)device, callback); |
Rafael J. Wysocki | caf5c03 | 2013-07-30 14:38:34 +0200 | [diff] [blame] | 1263 | if (ACPI_FAILURE(status) || !*device) { |
| 1264 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n", |
| 1265 | handle)); |
| 1266 | return -ENODEV; |
| 1267 | } |
| 1268 | return 0; |
| 1269 | } |
Rafael J. Wysocki | 78ea463 | 2014-02-04 00:43:05 +0100 | [diff] [blame] | 1270 | |
| 1271 | int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device) |
| 1272 | { |
| 1273 | return acpi_get_device_data(handle, device, NULL); |
| 1274 | } |
Rafael J. Wysocki | 6585925 | 2013-10-01 23:02:43 +0200 | [diff] [blame] | 1275 | EXPORT_SYMBOL(acpi_bus_get_device); |
Rafael J. Wysocki | caf5c03 | 2013-07-30 14:38:34 +0200 | [diff] [blame] | 1276 | |
Rafael J. Wysocki | 78ea463 | 2014-02-04 00:43:05 +0100 | [diff] [blame] | 1277 | static void get_acpi_device(void *dev) |
| 1278 | { |
| 1279 | if (dev) |
| 1280 | get_device(&((struct acpi_device *)dev)->dev); |
| 1281 | } |
| 1282 | |
| 1283 | struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle) |
| 1284 | { |
| 1285 | struct acpi_device *adev = NULL; |
| 1286 | |
| 1287 | acpi_get_device_data(handle, &adev, get_acpi_device); |
| 1288 | return adev; |
| 1289 | } |
| 1290 | |
| 1291 | void acpi_bus_put_acpi_device(struct acpi_device *adev) |
| 1292 | { |
| 1293 | put_device(&adev->dev); |
| 1294 | } |
| 1295 | |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 1296 | int acpi_device_add(struct acpi_device *device, |
| 1297 | void (*release)(struct device *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | { |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 1299 | int result; |
| 1300 | struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id; |
| 1301 | int found = 0; |
Bjorn Helgaas | 66b7ed4 | 2009-09-21 19:29:05 +0000 | [diff] [blame] | 1302 | |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1303 | if (device->handle) { |
| 1304 | acpi_status status; |
| 1305 | |
Rafael J. Wysocki | d783156 | 2013-11-22 21:52:12 +0100 | [diff] [blame] | 1306 | status = acpi_attach_data(device->handle, acpi_scan_drop_device, |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1307 | device); |
| 1308 | if (ACPI_FAILURE(status)) { |
| 1309 | acpi_handle_err(device->handle, |
| 1310 | "Unable to attach device data\n"); |
| 1311 | return -ENODEV; |
| 1312 | } |
| 1313 | } |
| 1314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | /* |
| 1316 | * Linkage |
| 1317 | * ------- |
| 1318 | * Link this device to its parent and siblings. |
| 1319 | */ |
| 1320 | INIT_LIST_HEAD(&device->children); |
| 1321 | INIT_LIST_HEAD(&device->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | INIT_LIST_HEAD(&device->wakeup_list); |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 1323 | INIT_LIST_HEAD(&device->physical_node_list); |
Rafael J. Wysocki | d783156 | 2013-11-22 21:52:12 +0100 | [diff] [blame] | 1324 | INIT_LIST_HEAD(&device->del_list); |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 1325 | mutex_init(&device->physical_node_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 1327 | new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL); |
| 1328 | if (!new_bus_id) { |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1329 | pr_err(PREFIX "Memory allocation error\n"); |
| 1330 | result = -ENOMEM; |
| 1331 | goto err_detach; |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 1332 | } |
| 1333 | |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 1334 | mutex_lock(&acpi_device_lock); |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 1335 | /* |
| 1336 | * Find suitable bus_id and instance number in acpi_bus_id_list |
| 1337 | * If failed, create one and link it into acpi_bus_id_list |
| 1338 | */ |
| 1339 | list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) { |
Bjorn Helgaas | 1131b93 | 2009-09-21 13:35:29 -0600 | [diff] [blame] | 1340 | if (!strcmp(acpi_device_bus_id->bus_id, |
| 1341 | acpi_device_hid(device))) { |
| 1342 | acpi_device_bus_id->instance_no++; |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 1343 | found = 1; |
| 1344 | kfree(new_bus_id); |
| 1345 | break; |
| 1346 | } |
| 1347 | } |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 1348 | if (!found) { |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 1349 | acpi_device_bus_id = new_bus_id; |
Bjorn Helgaas | 1131b93 | 2009-09-21 13:35:29 -0600 | [diff] [blame] | 1350 | strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device)); |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 1351 | acpi_device_bus_id->instance_no = 0; |
| 1352 | list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list); |
| 1353 | } |
Kay Sievers | 0794469 | 2008-10-30 01:18:59 +0100 | [diff] [blame] | 1354 | dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no); |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 1355 | |
Len Brown | 33b5715 | 2008-12-15 22:09:26 -0500 | [diff] [blame] | 1356 | if (device->parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | list_add_tail(&device->node, &device->parent->children); |
Len Brown | 33b5715 | 2008-12-15 22:09:26 -0500 | [diff] [blame] | 1358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | if (device->wakeup.flags.valid) |
| 1360 | list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list); |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 1361 | mutex_unlock(&acpi_device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 1363 | if (device->parent) |
Bjorn Helgaas | 66b7ed4 | 2009-09-21 19:29:05 +0000 | [diff] [blame] | 1364 | device->dev.parent = &device->parent->dev; |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 1365 | device->dev.bus = &acpi_bus_type; |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1366 | device->dev.release = release; |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 1367 | result = device_add(&device->dev); |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 1368 | if (result) { |
Alex Chiang | 8b12b92 | 2009-05-14 08:31:32 -0600 | [diff] [blame] | 1369 | dev_err(&device->dev, "Error registering device\n"); |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1370 | goto err; |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 1371 | } |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 1372 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 1373 | result = acpi_device_setup_files(device); |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 1374 | if (result) |
Kay Sievers | 0794469 | 2008-10-30 01:18:59 +0100 | [diff] [blame] | 1375 | printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n", |
| 1376 | dev_name(&device->dev)); |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 1377 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 1378 | return 0; |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1379 | |
| 1380 | err: |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 1381 | mutex_lock(&acpi_device_lock); |
Len Brown | 33b5715 | 2008-12-15 22:09:26 -0500 | [diff] [blame] | 1382 | if (device->parent) |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 1383 | list_del(&device->node); |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 1384 | list_del(&device->wakeup_list); |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 1385 | mutex_unlock(&acpi_device_lock); |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1386 | |
| 1387 | err_detach: |
Rafael J. Wysocki | d783156 | 2013-11-22 21:52:12 +0100 | [diff] [blame] | 1388 | acpi_detach_data(device->handle, acpi_scan_drop_device); |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 1389 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | } |
| 1391 | |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 1392 | /* -------------------------------------------------------------------------- |
| 1393 | Driver Management |
| 1394 | -------------------------------------------------------------------------- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | /** |
Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 1396 | * acpi_bus_register_driver - register a driver with the ACPI bus |
| 1397 | * @driver: driver being registered |
| 1398 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | * Registers a driver with the ACPI bus. Searches the namespace for all |
Bjorn Helgaas | 9d9f749 | 2006-03-28 17:04:00 -0500 | [diff] [blame] | 1400 | * devices that match the driver's criteria and binds. Returns zero for |
| 1401 | * success or a negative error status for failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1403 | int acpi_bus_register_driver(struct acpi_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | { |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 1405 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | |
| 1407 | if (acpi_disabled) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1408 | return -ENODEV; |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 1409 | driver->drv.name = driver->name; |
| 1410 | driver->drv.bus = &acpi_bus_type; |
| 1411 | driver->drv.owner = driver->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 1413 | ret = driver_register(&driver->drv); |
| 1414 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1417 | EXPORT_SYMBOL(acpi_bus_register_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | |
| 1419 | /** |
Hanjun Guo | b27b14c | 2013-09-22 15:42:41 +0800 | [diff] [blame] | 1420 | * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus |
Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 1421 | * @driver: driver to unregister |
| 1422 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | * Unregisters a driver with the ACPI bus. Searches the namespace for all |
| 1424 | * devices that match the driver's criteria and unbinds. |
| 1425 | */ |
Bjorn Helgaas | 06ea8e08 | 2006-04-27 05:25:00 -0400 | [diff] [blame] | 1426 | void acpi_bus_unregister_driver(struct acpi_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | { |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 1428 | driver_unregister(&driver->drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | EXPORT_SYMBOL(acpi_bus_unregister_driver); |
| 1432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | /* -------------------------------------------------------------------------- |
| 1434 | Device Enumeration |
| 1435 | -------------------------------------------------------------------------- */ |
Bjorn Helgaas | 5c478f4 | 2009-09-21 19:29:35 +0000 | [diff] [blame] | 1436 | static struct acpi_device *acpi_bus_get_parent(acpi_handle handle) |
| 1437 | { |
Rafael J. Wysocki | 456de89 | 2013-01-31 20:57:40 +0100 | [diff] [blame] | 1438 | struct acpi_device *device = NULL; |
Bjorn Helgaas | 5c478f4 | 2009-09-21 19:29:35 +0000 | [diff] [blame] | 1439 | acpi_status status; |
Bjorn Helgaas | 5c478f4 | 2009-09-21 19:29:35 +0000 | [diff] [blame] | 1440 | |
| 1441 | /* |
| 1442 | * Fixed hardware devices do not appear in the namespace and do not |
| 1443 | * have handles, but we fabricate acpi_devices for them, so we have |
| 1444 | * to deal with them specially. |
| 1445 | */ |
Rafael J. Wysocki | 456de89 | 2013-01-31 20:57:40 +0100 | [diff] [blame] | 1446 | if (!handle) |
Bjorn Helgaas | 5c478f4 | 2009-09-21 19:29:35 +0000 | [diff] [blame] | 1447 | return acpi_root; |
| 1448 | |
| 1449 | do { |
| 1450 | status = acpi_get_parent(handle, &handle); |
Bjorn Helgaas | 5c478f4 | 2009-09-21 19:29:35 +0000 | [diff] [blame] | 1451 | if (ACPI_FAILURE(status)) |
Rafael J. Wysocki | 456de89 | 2013-01-31 20:57:40 +0100 | [diff] [blame] | 1452 | return status == AE_NULL_ENTRY ? NULL : acpi_root; |
| 1453 | } while (acpi_bus_get_device(handle, &device)); |
| 1454 | return device; |
Bjorn Helgaas | 5c478f4 | 2009-09-21 19:29:35 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 1457 | acpi_status |
| 1458 | acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd) |
| 1459 | { |
| 1460 | acpi_status status; |
| 1461 | acpi_handle tmp; |
| 1462 | struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; |
| 1463 | union acpi_object *obj; |
| 1464 | |
| 1465 | status = acpi_get_handle(handle, "_EJD", &tmp); |
| 1466 | if (ACPI_FAILURE(status)) |
| 1467 | return status; |
| 1468 | |
| 1469 | status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer); |
| 1470 | if (ACPI_SUCCESS(status)) { |
| 1471 | obj = buffer.pointer; |
Holger Macht | 3b5fee5 | 2008-02-14 13:40:34 +0100 | [diff] [blame] | 1472 | status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer, |
| 1473 | ejd); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 1474 | kfree(buffer.pointer); |
| 1475 | } |
| 1476 | return status; |
| 1477 | } |
| 1478 | EXPORT_SYMBOL_GPL(acpi_bus_get_ejd); |
| 1479 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1480 | static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle, |
| 1481 | struct acpi_device_wakeup *wakeup) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | { |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1483 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 1484 | union acpi_object *package = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | union acpi_object *element = NULL; |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1486 | acpi_status status; |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1487 | int err = -ENODATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1489 | if (!wakeup) |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1490 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | |
Rafael J. Wysocki | 993cbe5 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1492 | INIT_LIST_HEAD(&wakeup->resources); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1494 | /* _PRW */ |
| 1495 | status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer); |
| 1496 | if (ACPI_FAILURE(status)) { |
| 1497 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW")); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1498 | return err; |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | package = (union acpi_object *)buffer.pointer; |
| 1502 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1503 | if (!package || package->package.count < 2) |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1504 | goto out; |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | element = &(package->package.elements[0]); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1507 | if (!element) |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1508 | goto out; |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | if (element->type == ACPI_TYPE_PACKAGE) { |
| 1511 | if ((element->package.count < 2) || |
| 1512 | (element->package.elements[0].type != |
| 1513 | ACPI_TYPE_LOCAL_REFERENCE) |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1514 | || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1515 | goto out; |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1516 | |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1517 | wakeup->gpe_device = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | element->package.elements[0].reference.handle; |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1519 | wakeup->gpe_number = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | (u32) element->package.elements[1].integer.value; |
| 1521 | } else if (element->type == ACPI_TYPE_INTEGER) { |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1522 | wakeup->gpe_device = NULL; |
| 1523 | wakeup->gpe_number = element->integer.value; |
| 1524 | } else { |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1525 | goto out; |
| 1526 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | |
| 1528 | element = &(package->package.elements[1]); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1529 | if (element->type != ACPI_TYPE_INTEGER) |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1530 | goto out; |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1531 | |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1532 | wakeup->sleep_state = element->integer.value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1534 | err = acpi_extract_power_resources(package, 2, &wakeup->resources); |
| 1535 | if (err) |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1536 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | |
Rafael J. Wysocki | 0596a52 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1538 | if (!list_empty(&wakeup->resources)) { |
| 1539 | int sleep_state; |
| 1540 | |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 1541 | err = acpi_power_wakeup_list_init(&wakeup->resources, |
| 1542 | &sleep_state); |
| 1543 | if (err) { |
| 1544 | acpi_handle_warn(handle, "Retrieving current states " |
| 1545 | "of wakeup power resources failed\n"); |
| 1546 | acpi_power_resources_list_free(&wakeup->resources); |
| 1547 | goto out; |
| 1548 | } |
Rafael J. Wysocki | 0596a52 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1549 | if (sleep_state < wakeup->sleep_state) { |
| 1550 | acpi_handle_warn(handle, "Overriding _PRW sleep state " |
| 1551 | "(S%d) by S%d from power resources\n", |
| 1552 | (int)wakeup->sleep_state, sleep_state); |
| 1553 | wakeup->sleep_state = sleep_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | } |
Rafael J. Wysocki | 9874647 | 2010-07-08 00:43:36 +0200 | [diff] [blame] | 1556 | |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1557 | out: |
| 1558 | kfree(buffer.pointer); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1559 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | } |
| 1561 | |
Rafael J. Wysocki | bd9b2f9 | 2014-07-14 22:41:41 +0200 | [diff] [blame] | 1562 | static void acpi_wakeup_gpe_init(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | { |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 1564 | struct acpi_device_id button_device_ids[] = { |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 1565 | {"PNP0C0C", 0}, |
Zhang Rui | b7e3830 | 2012-12-04 23:23:16 +0100 | [diff] [blame] | 1566 | {"PNP0C0D", 0}, |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 1567 | {"PNP0C0E", 0}, |
| 1568 | {"", 0}, |
| 1569 | }; |
Rafael J. Wysocki | bd9b2f9 | 2014-07-14 22:41:41 +0200 | [diff] [blame] | 1570 | struct acpi_device_wakeup *wakeup = &device->wakeup; |
Rafael J. Wysocki | f517709 | 2010-02-17 23:41:49 +0100 | [diff] [blame] | 1571 | acpi_status status; |
| 1572 | acpi_event_status event_status; |
| 1573 | |
Rafael J. Wysocki | bd9b2f9 | 2014-07-14 22:41:41 +0200 | [diff] [blame] | 1574 | wakeup->flags.notifier_present = 0; |
Rafael J. Wysocki | f517709 | 2010-02-17 23:41:49 +0100 | [diff] [blame] | 1575 | |
| 1576 | /* Power button, Lid switch always enable wakeup */ |
| 1577 | if (!acpi_match_device_ids(device, button_device_ids)) { |
Rafael J. Wysocki | bd9b2f9 | 2014-07-14 22:41:41 +0200 | [diff] [blame] | 1578 | wakeup->flags.run_wake = 1; |
Zhang Rui | b7e3830 | 2012-12-04 23:23:16 +0100 | [diff] [blame] | 1579 | if (!acpi_match_device_ids(device, &button_device_ids[1])) { |
| 1580 | /* Do not use Lid/sleep button for S5 wakeup */ |
Rafael J. Wysocki | bd9b2f9 | 2014-07-14 22:41:41 +0200 | [diff] [blame] | 1581 | if (wakeup->sleep_state == ACPI_STATE_S5) |
| 1582 | wakeup->sleep_state = ACPI_STATE_S4; |
Zhang Rui | b7e3830 | 2012-12-04 23:23:16 +0100 | [diff] [blame] | 1583 | } |
Rafael J. Wysocki | bd9b2f9 | 2014-07-14 22:41:41 +0200 | [diff] [blame] | 1584 | acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number); |
Rafael J. Wysocki | f2b56bc | 2011-01-06 23:34:22 +0100 | [diff] [blame] | 1585 | device_set_wakeup_capable(&device->dev, true); |
Rafael J. Wysocki | f517709 | 2010-02-17 23:41:49 +0100 | [diff] [blame] | 1586 | return; |
| 1587 | } |
| 1588 | |
Rafael J. Wysocki | bd9b2f9 | 2014-07-14 22:41:41 +0200 | [diff] [blame] | 1589 | acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device, |
| 1590 | wakeup->gpe_number); |
| 1591 | status = acpi_get_gpe_status(wakeup->gpe_device, wakeup->gpe_number, |
| 1592 | &event_status); |
| 1593 | if (ACPI_FAILURE(status)) |
| 1594 | return; |
| 1595 | |
Lv Zheng | 2f85723 | 2014-10-10 10:40:05 +0800 | [diff] [blame] | 1596 | wakeup->flags.run_wake = !!(event_status & ACPI_EVENT_FLAG_HAS_HANDLER); |
Rafael J. Wysocki | f517709 | 2010-02-17 23:41:49 +0100 | [diff] [blame] | 1597 | } |
| 1598 | |
Rafael J. Wysocki | d57d09a | 2011-01-06 23:41:27 +0100 | [diff] [blame] | 1599 | static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device) |
Rafael J. Wysocki | f517709 | 2010-02-17 23:41:49 +0100 | [diff] [blame] | 1600 | { |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1601 | int err; |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 1602 | |
Rafael J. Wysocki | d57d09a | 2011-01-06 23:41:27 +0100 | [diff] [blame] | 1603 | /* Presence of _PRW indicates wake capable */ |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 1604 | if (!acpi_has_method(device->handle, "_PRW")) |
Rafael J. Wysocki | d57d09a | 2011-01-06 23:41:27 +0100 | [diff] [blame] | 1605 | return; |
| 1606 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1607 | err = acpi_bus_extract_wakeup_device_power_package(device->handle, |
| 1608 | &device->wakeup); |
| 1609 | if (err) { |
| 1610 | dev_err(&device->dev, "_PRW evaluation error: %d\n", err); |
Rafael J. Wysocki | d57d09a | 2011-01-06 23:41:27 +0100 | [diff] [blame] | 1611 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | device->wakeup.flags.valid = 1; |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 1615 | device->wakeup.prepare_count = 0; |
Rafael J. Wysocki | bd9b2f9 | 2014-07-14 22:41:41 +0200 | [diff] [blame] | 1616 | acpi_wakeup_gpe_init(device); |
Zhao Yakui | 729b2bd | 2008-03-19 13:26:54 +0800 | [diff] [blame] | 1617 | /* Call _PSW/_DSW object to disable its ability to wake the sleeping |
| 1618 | * system for the ACPI device with the _PRW object. |
| 1619 | * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW. |
| 1620 | * So it is necessary to call _DSW object first. Only when it is not |
| 1621 | * present will the _PSW object used. |
| 1622 | */ |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1623 | err = acpi_device_sleep_wake(device, 0, 0, 0); |
| 1624 | if (err) |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 1625 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 1626 | "error in _DSW or _PSW evaluation\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | } |
| 1628 | |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1629 | static void acpi_bus_init_power_state(struct acpi_device *device, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | { |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1631 | struct acpi_device_power_state *ps = &device->power.states[state]; |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1632 | char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' }; |
| 1633 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1634 | acpi_status status; |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 1635 | |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1636 | INIT_LIST_HEAD(&ps->resources); |
| 1637 | |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1638 | /* Evaluate "_PRx" to get referenced power resources */ |
| 1639 | status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer); |
| 1640 | if (ACPI_SUCCESS(status)) { |
| 1641 | union acpi_object *package = buffer.pointer; |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1642 | |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1643 | if (buffer.length && package |
| 1644 | && package->type == ACPI_TYPE_PACKAGE |
| 1645 | && package->package.count) { |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1646 | int err = acpi_extract_power_resources(package, 0, |
| 1647 | &ps->resources); |
| 1648 | if (!err) |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1649 | device->power.flags.power_resources = 1; |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1650 | } |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1651 | ACPI_FREE(buffer.pointer); |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | /* Evaluate "_PSx" to see if we can do explicit sets */ |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1655 | pathname[2] = 'S'; |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 1656 | if (acpi_has_method(device->handle, pathname)) |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1657 | ps->flags.explicit_set = 1; |
| 1658 | |
| 1659 | /* |
| 1660 | * State is valid if there are means to put the device into it. |
| 1661 | * D3hot is only valid if _PR3 present. |
| 1662 | */ |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1663 | if (!list_empty(&ps->resources) |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1664 | || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) { |
| 1665 | ps->flags.valid = 1; |
| 1666 | ps->flags.os_accessible = 1; |
| 1667 | } |
| 1668 | |
| 1669 | ps->power = -1; /* Unknown - driver assigned */ |
| 1670 | ps->latency = -1; /* Unknown - driver assigned */ |
| 1671 | } |
| 1672 | |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1673 | static void acpi_bus_get_power_flags(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | { |
Rafael J. Wysocki | 8bc5053 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1675 | u32 i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1677 | /* Presence of _PS0|_PR0 indicates 'power manageable' */ |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 1678 | if (!acpi_has_method(device->handle, "_PS0") && |
| 1679 | !acpi_has_method(device->handle, "_PR0")) |
| 1680 | return; |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1681 | |
| 1682 | device->flags.power_manageable = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | |
| 1684 | /* |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 1685 | * Power Management Flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | */ |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 1687 | if (acpi_has_method(device->handle, "_PSC")) |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 1688 | device->power.flags.explicit_get = 1; |
Rafael J. Wysocki | f25c0ae | 2014-05-17 00:18:13 +0200 | [diff] [blame] | 1689 | |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 1690 | if (acpi_has_method(device->handle, "_IRC")) |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 1691 | device->power.flags.inrush_current = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | |
Rafael J. Wysocki | f25c0ae | 2014-05-17 00:18:13 +0200 | [diff] [blame] | 1693 | if (acpi_has_method(device->handle, "_DSW")) |
| 1694 | device->power.flags.dsw_present = 1; |
| 1695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | /* |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 1697 | * Enumerate supported power management states |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | */ |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1699 | for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) |
| 1700 | acpi_bus_init_power_state(device, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1702 | INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 1704 | /* Set defaults for D0 and D3 states (always valid) */ |
| 1705 | device->power.states[ACPI_STATE_D0].flags.valid = 1; |
| 1706 | device->power.states[ACPI_STATE_D0].power = 100; |
Rafael J. Wysocki | 8ad928d | 2013-07-30 14:36:20 +0200 | [diff] [blame] | 1707 | device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1; |
| 1708 | device->power.states[ACPI_STATE_D3_COLD].power = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | |
Rafael J. Wysocki | 5c7dd71 | 2012-05-18 00:39:35 +0200 | [diff] [blame] | 1710 | /* Set D3cold's explicit_set flag if _PS3 exists. */ |
| 1711 | if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set) |
| 1712 | device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1; |
| 1713 | |
Aaron Lu | 1399dfc | 2012-11-21 23:33:40 +0100 | [diff] [blame] | 1714 | /* Presence of _PS3 or _PRx means we can put the device into D3 cold */ |
| 1715 | if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set || |
| 1716 | device->power.flags.power_resources) |
| 1717 | device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1; |
| 1718 | |
Rafael J. Wysocki | b378549 | 2013-02-01 23:43:02 +0100 | [diff] [blame] | 1719 | if (acpi_bus_init_power(device)) { |
| 1720 | acpi_free_power_resources_lists(device); |
| 1721 | device->flags.power_manageable = 0; |
| 1722 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1725 | static void acpi_bus_get_flags(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | /* Presence of _STA indicates 'dynamic_status' */ |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 1728 | if (acpi_has_method(device->handle, "_STA")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | device->flags.dynamic_status = 1; |
| 1730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | /* Presence of _RMV indicates 'removable' */ |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 1732 | if (acpi_has_method(device->handle, "_RMV")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | device->flags.removable = 1; |
| 1734 | |
| 1735 | /* Presence of _EJD|_EJ0 indicates 'ejectable' */ |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 1736 | if (acpi_has_method(device->handle, "_EJD") || |
| 1737 | acpi_has_method(device->handle, "_EJ0")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | device->flags.ejectable = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | } |
| 1740 | |
Bjorn Helgaas | c7bcb4e | 2009-09-21 19:29:25 +0000 | [diff] [blame] | 1741 | static void acpi_device_get_busid(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1743 | char bus_id[5] = { '?', 0 }; |
| 1744 | struct acpi_buffer buffer = { sizeof(bus_id), bus_id }; |
| 1745 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | |
| 1747 | /* |
| 1748 | * Bus ID |
| 1749 | * ------ |
| 1750 | * The device's Bus ID is simply the object name. |
| 1751 | * TBD: Shouldn't this value be unique (within the ACPI namespace)? |
| 1752 | */ |
Bjorn Helgaas | 859ac9a | 2009-09-21 19:29:50 +0000 | [diff] [blame] | 1753 | if (ACPI_IS_ROOT_DEVICE(device)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | strcpy(device->pnp.bus_id, "ACPI"); |
Bjorn Helgaas | 859ac9a | 2009-09-21 19:29:50 +0000 | [diff] [blame] | 1755 | return; |
| 1756 | } |
| 1757 | |
| 1758 | switch (device->device_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | case ACPI_BUS_TYPE_POWER_BUTTON: |
| 1760 | strcpy(device->pnp.bus_id, "PWRF"); |
| 1761 | break; |
| 1762 | case ACPI_BUS_TYPE_SLEEP_BUTTON: |
| 1763 | strcpy(device->pnp.bus_id, "SLPF"); |
| 1764 | break; |
| 1765 | default: |
Bjorn Helgaas | 66b7ed4 | 2009-09-21 19:29:05 +0000 | [diff] [blame] | 1766 | acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | /* Clean up trailing underscores (if any) */ |
| 1768 | for (i = 3; i > 1; i--) { |
| 1769 | if (bus_id[i] == '_') |
| 1770 | bus_id[i] = '\0'; |
| 1771 | else |
| 1772 | break; |
| 1773 | } |
| 1774 | strcpy(device->pnp.bus_id, bus_id); |
| 1775 | break; |
| 1776 | } |
| 1777 | } |
| 1778 | |
Zhang Rui | 5473526 | 2007-01-11 02:09:09 -0500 | [diff] [blame] | 1779 | /* |
Jiang Liu | ebf4df8 | 2013-06-29 00:24:41 +0800 | [diff] [blame] | 1780 | * acpi_ata_match - see if an acpi object is an ATA device |
| 1781 | * |
| 1782 | * If an acpi object has one of the ACPI ATA methods defined, |
| 1783 | * then we can safely call it an ATA device. |
| 1784 | */ |
| 1785 | bool acpi_ata_match(acpi_handle handle) |
| 1786 | { |
| 1787 | return acpi_has_method(handle, "_GTF") || |
| 1788 | acpi_has_method(handle, "_GTM") || |
| 1789 | acpi_has_method(handle, "_STM") || |
| 1790 | acpi_has_method(handle, "_SDD"); |
| 1791 | } |
| 1792 | |
| 1793 | /* |
Toshi Kani | d4e1a69 | 2013-03-04 21:30:41 +0000 | [diff] [blame] | 1794 | * acpi_bay_match - see if an acpi object is an ejectable driver bay |
Zhang Rui | 5473526 | 2007-01-11 02:09:09 -0500 | [diff] [blame] | 1795 | * |
| 1796 | * If an acpi object is ejectable and has one of the ACPI ATA methods defined, |
| 1797 | * then we can safely call it an ejectable drive bay |
| 1798 | */ |
Jiang Liu | ebf4df8 | 2013-06-29 00:24:41 +0800 | [diff] [blame] | 1799 | bool acpi_bay_match(acpi_handle handle) |
Toshi Kani | d4e1a69 | 2013-03-04 21:30:41 +0000 | [diff] [blame] | 1800 | { |
Zhang Rui | 5473526 | 2007-01-11 02:09:09 -0500 | [diff] [blame] | 1801 | acpi_handle phandle; |
| 1802 | |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 1803 | if (!acpi_has_method(handle, "_EJ0")) |
Jiang Liu | ebf4df8 | 2013-06-29 00:24:41 +0800 | [diff] [blame] | 1804 | return false; |
| 1805 | if (acpi_ata_match(handle)) |
| 1806 | return true; |
| 1807 | if (ACPI_FAILURE(acpi_get_parent(handle, &phandle))) |
| 1808 | return false; |
Zhang Rui | 5473526 | 2007-01-11 02:09:09 -0500 | [diff] [blame] | 1809 | |
Jiang Liu | ebf4df8 | 2013-06-29 00:24:41 +0800 | [diff] [blame] | 1810 | return acpi_ata_match(phandle); |
Zhang Rui | 5473526 | 2007-01-11 02:09:09 -0500 | [diff] [blame] | 1811 | } |
| 1812 | |
Rafael J. Wysocki | b43109f | 2014-02-16 00:09:34 +0100 | [diff] [blame] | 1813 | bool acpi_device_is_battery(struct acpi_device *adev) |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 1814 | { |
Rafael J. Wysocki | b43109f | 2014-02-16 00:09:34 +0100 | [diff] [blame] | 1815 | struct acpi_hardware_id *hwid; |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 1816 | |
Rafael J. Wysocki | b43109f | 2014-02-16 00:09:34 +0100 | [diff] [blame] | 1817 | list_for_each_entry(hwid, &adev->pnp.ids, list) |
| 1818 | if (!strcmp("PNP0C0A", hwid->id)) |
| 1819 | return true; |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 1820 | |
Rafael J. Wysocki | b43109f | 2014-02-16 00:09:34 +0100 | [diff] [blame] | 1821 | return false; |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 1822 | } |
| 1823 | |
Rafael J. Wysocki | b43109f | 2014-02-16 00:09:34 +0100 | [diff] [blame] | 1824 | static bool is_ejectable_bay(struct acpi_device *adev) |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 1825 | { |
Rafael J. Wysocki | b43109f | 2014-02-16 00:09:34 +0100 | [diff] [blame] | 1826 | acpi_handle handle = adev->handle; |
| 1827 | |
| 1828 | if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev)) |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 1829 | return true; |
| 1830 | |
| 1831 | return acpi_bay_match(handle); |
| 1832 | } |
| 1833 | |
Frank Seidel | 3620f2f | 2007-12-07 13:20:34 +0100 | [diff] [blame] | 1834 | /* |
Toshi Kani | d4e1a69 | 2013-03-04 21:30:41 +0000 | [diff] [blame] | 1835 | * acpi_dock_match - see if an acpi object has a _DCK method |
Frank Seidel | 3620f2f | 2007-12-07 13:20:34 +0100 | [diff] [blame] | 1836 | */ |
Jiang Liu | ebf4df8 | 2013-06-29 00:24:41 +0800 | [diff] [blame] | 1837 | bool acpi_dock_match(acpi_handle handle) |
Frank Seidel | 3620f2f | 2007-12-07 13:20:34 +0100 | [diff] [blame] | 1838 | { |
Jiang Liu | ebf4df8 | 2013-06-29 00:24:41 +0800 | [diff] [blame] | 1839 | return acpi_has_method(handle, "_DCK"); |
Frank Seidel | 3620f2f | 2007-12-07 13:20:34 +0100 | [diff] [blame] | 1840 | } |
| 1841 | |
Thomas Renninger | 620e112 | 2010-10-01 10:54:00 +0200 | [diff] [blame] | 1842 | const char *acpi_device_hid(struct acpi_device *device) |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 1843 | { |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 1844 | struct acpi_hardware_id *hid; |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 1845 | |
Thomas Renninger | 2b2ae7c | 2010-10-01 10:53:59 +0200 | [diff] [blame] | 1846 | if (list_empty(&device->pnp.ids)) |
| 1847 | return dummy_hid; |
| 1848 | |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 1849 | hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list); |
| 1850 | return hid->id; |
| 1851 | } |
| 1852 | EXPORT_SYMBOL(acpi_device_hid); |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 1853 | |
Toshi Kani | d4e1a69 | 2013-03-04 21:30:41 +0000 | [diff] [blame] | 1854 | static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id) |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 1855 | { |
| 1856 | struct acpi_hardware_id *id; |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 1857 | |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 1858 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
| 1859 | if (!id) |
| 1860 | return; |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 1861 | |
Thomas Meyer | 581de59 | 2011-08-06 11:32:56 +0200 | [diff] [blame] | 1862 | id->id = kstrdup(dev_id, GFP_KERNEL); |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 1863 | if (!id->id) { |
| 1864 | kfree(id); |
| 1865 | return; |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 1866 | } |
| 1867 | |
Toshi Kani | d4e1a69 | 2013-03-04 21:30:41 +0000 | [diff] [blame] | 1868 | list_add_tail(&id->list, &pnp->ids); |
| 1869 | pnp->type.hardware_id = 1; |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 1870 | } |
| 1871 | |
Darrick J. Wong | 222e82a | 2010-03-24 14:38:37 +0100 | [diff] [blame] | 1872 | /* |
| 1873 | * Old IBM workstations have a DSDT bug wherein the SMBus object |
| 1874 | * lacks the SMBUS01 HID and the methods do not have the necessary "_" |
| 1875 | * prefix. Work around this. |
| 1876 | */ |
Jiang Liu | ebf4df8 | 2013-06-29 00:24:41 +0800 | [diff] [blame] | 1877 | static bool acpi_ibm_smbus_match(acpi_handle handle) |
Darrick J. Wong | 222e82a | 2010-03-24 14:38:37 +0100 | [diff] [blame] | 1878 | { |
Jiang Liu | ebf4df8 | 2013-06-29 00:24:41 +0800 | [diff] [blame] | 1879 | char node_name[ACPI_PATH_SEGMENT_LENGTH]; |
| 1880 | struct acpi_buffer path = { sizeof(node_name), node_name }; |
Darrick J. Wong | 222e82a | 2010-03-24 14:38:37 +0100 | [diff] [blame] | 1881 | |
| 1882 | if (!dmi_name_in_vendors("IBM")) |
Jiang Liu | ebf4df8 | 2013-06-29 00:24:41 +0800 | [diff] [blame] | 1883 | return false; |
Darrick J. Wong | 222e82a | 2010-03-24 14:38:37 +0100 | [diff] [blame] | 1884 | |
| 1885 | /* Look for SMBS object */ |
Jiang Liu | ebf4df8 | 2013-06-29 00:24:41 +0800 | [diff] [blame] | 1886 | if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) || |
| 1887 | strcmp("SMBS", path.pointer)) |
| 1888 | return false; |
Darrick J. Wong | 222e82a | 2010-03-24 14:38:37 +0100 | [diff] [blame] | 1889 | |
| 1890 | /* Does it have the necessary (but misnamed) methods? */ |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 1891 | if (acpi_has_method(handle, "SBI") && |
| 1892 | acpi_has_method(handle, "SBR") && |
| 1893 | acpi_has_method(handle, "SBW")) |
Jiang Liu | ebf4df8 | 2013-06-29 00:24:41 +0800 | [diff] [blame] | 1894 | return true; |
| 1895 | |
| 1896 | return false; |
Darrick J. Wong | 222e82a | 2010-03-24 14:38:37 +0100 | [diff] [blame] | 1897 | } |
| 1898 | |
Jiang Liu | ae3caa8 | 2014-02-19 14:02:19 +0800 | [diff] [blame] | 1899 | static bool acpi_object_is_system_bus(acpi_handle handle) |
| 1900 | { |
| 1901 | acpi_handle tmp; |
| 1902 | |
| 1903 | if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) && |
| 1904 | tmp == handle) |
| 1905 | return true; |
| 1906 | if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) && |
| 1907 | tmp == handle) |
| 1908 | return true; |
| 1909 | |
| 1910 | return false; |
| 1911 | } |
| 1912 | |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1913 | static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp, |
| 1914 | int device_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1916 | acpi_status status; |
Bjorn Helgaas | 57f3674 | 2009-09-21 13:35:40 -0600 | [diff] [blame] | 1917 | struct acpi_device_info *info; |
Lv Zheng | 78e25fe | 2012-10-31 02:25:24 +0000 | [diff] [blame] | 1918 | struct acpi_pnp_device_id_list *cid_list; |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 1919 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1921 | switch (device_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | case ACPI_BUS_TYPE_DEVICE: |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1923 | if (handle == ACPI_ROOT_OBJECT) { |
| 1924 | acpi_add_id(pnp, ACPI_SYSTEM_HID); |
Bjorn Helgaas | 859ac9a | 2009-09-21 19:29:50 +0000 | [diff] [blame] | 1925 | break; |
| 1926 | } |
| 1927 | |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1928 | status = acpi_get_object_info(handle, &info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | if (ACPI_FAILURE(status)) { |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1930 | pr_err(PREFIX "%s: Error reading device info\n", |
| 1931 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | return; |
| 1933 | } |
| 1934 | |
Rafael J. Wysocki | 549e684 | 2014-05-30 04:26:18 +0200 | [diff] [blame] | 1935 | if (info->valid & ACPI_VALID_HID) { |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1936 | acpi_add_id(pnp, info->hardware_id.string); |
Rafael J. Wysocki | 549e684 | 2014-05-30 04:26:18 +0200 | [diff] [blame] | 1937 | pnp->type.platform_id = 1; |
| 1938 | } |
Bjorn Helgaas | 57f3674 | 2009-09-21 13:35:40 -0600 | [diff] [blame] | 1939 | if (info->valid & ACPI_VALID_CID) { |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 1940 | cid_list = &info->compatible_id_list; |
Bjorn Helgaas | 57f3674 | 2009-09-21 13:35:40 -0600 | [diff] [blame] | 1941 | for (i = 0; i < cid_list->count; i++) |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1942 | acpi_add_id(pnp, cid_list->ids[i].string); |
Bjorn Helgaas | 57f3674 | 2009-09-21 13:35:40 -0600 | [diff] [blame] | 1943 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | if (info->valid & ACPI_VALID_ADR) { |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1945 | pnp->bus_address = info->address; |
| 1946 | pnp->type.bus_address = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | } |
Lv Zheng | ccf7804 | 2012-10-30 14:41:07 +0100 | [diff] [blame] | 1948 | if (info->valid & ACPI_VALID_UID) |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1949 | pnp->unique_id = kstrdup(info->unique_id.string, |
Lv Zheng | ccf7804 | 2012-10-30 14:41:07 +0100 | [diff] [blame] | 1950 | GFP_KERNEL); |
Zhang Rui | ae84333 | 2006-12-07 20:57:10 +0800 | [diff] [blame] | 1951 | |
Bjorn Helgaas | a83893ae | 2009-10-02 11:03:12 -0400 | [diff] [blame] | 1952 | kfree(info); |
| 1953 | |
Bjorn Helgaas | 57f3674 | 2009-09-21 13:35:40 -0600 | [diff] [blame] | 1954 | /* |
| 1955 | * Some devices don't reliably have _HIDs & _CIDs, so add |
| 1956 | * synthetic HIDs to make sure drivers can find them. |
| 1957 | */ |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1958 | if (acpi_is_video_device(handle)) |
| 1959 | acpi_add_id(pnp, ACPI_VIDEO_HID); |
Jiang Liu | ebf4df8 | 2013-06-29 00:24:41 +0800 | [diff] [blame] | 1960 | else if (acpi_bay_match(handle)) |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1961 | acpi_add_id(pnp, ACPI_BAY_HID); |
Jiang Liu | ebf4df8 | 2013-06-29 00:24:41 +0800 | [diff] [blame] | 1962 | else if (acpi_dock_match(handle)) |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1963 | acpi_add_id(pnp, ACPI_DOCK_HID); |
Jiang Liu | ebf4df8 | 2013-06-29 00:24:41 +0800 | [diff] [blame] | 1964 | else if (acpi_ibm_smbus_match(handle)) |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1965 | acpi_add_id(pnp, ACPI_SMBUS_IBM_HID); |
Jiang Liu | ae3caa8 | 2014-02-19 14:02:19 +0800 | [diff] [blame] | 1966 | else if (list_empty(&pnp->ids) && |
| 1967 | acpi_object_is_system_bus(handle)) { |
| 1968 | /* \_SB, \_TZ, LNXSYBUS */ |
| 1969 | acpi_add_id(pnp, ACPI_BUS_HID); |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1970 | strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME); |
| 1971 | strcpy(pnp->device_class, ACPI_BUS_CLASS); |
Bjorn Helgaas | b7b30de | 2010-03-24 10:44:33 -0600 | [diff] [blame] | 1972 | } |
Zhang Rui | 5473526 | 2007-01-11 02:09:09 -0500 | [diff] [blame] | 1973 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | break; |
| 1975 | case ACPI_BUS_TYPE_POWER: |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1976 | acpi_add_id(pnp, ACPI_POWER_HID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | break; |
| 1978 | case ACPI_BUS_TYPE_PROCESSOR: |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1979 | acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | case ACPI_BUS_TYPE_THERMAL: |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1982 | acpi_add_id(pnp, ACPI_THERMAL_HID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | break; |
| 1984 | case ACPI_BUS_TYPE_POWER_BUTTON: |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1985 | acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | break; |
| 1987 | case ACPI_BUS_TYPE_SLEEP_BUTTON: |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1988 | acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | break; |
| 1990 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | } |
| 1992 | |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 1993 | void acpi_free_pnp_ids(struct acpi_device_pnp *pnp) |
| 1994 | { |
| 1995 | struct acpi_hardware_id *id, *tmp; |
| 1996 | |
| 1997 | list_for_each_entry_safe(id, tmp, &pnp->ids, list) { |
| 1998 | kfree(id->id); |
| 1999 | kfree(id); |
| 2000 | } |
| 2001 | kfree(pnp->unique_id); |
| 2002 | } |
| 2003 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 2004 | void acpi_init_device_object(struct acpi_device *device, acpi_handle handle, |
| 2005 | int type, unsigned long long sta) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | { |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 2007 | INIT_LIST_HEAD(&device->pnp.ids); |
| 2008 | device->device_type = type; |
| 2009 | device->handle = handle; |
| 2010 | device->parent = acpi_bus_get_parent(handle); |
Rafael J. Wysocki | 25db115 | 2013-11-22 21:56:06 +0100 | [diff] [blame] | 2011 | acpi_set_device_status(device, sta); |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 2012 | acpi_device_get_busid(device); |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 2013 | acpi_set_pnp_ids(handle, &device->pnp, type); |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 2014 | acpi_init_properties(device); |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 2015 | acpi_bus_get_flags(device); |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 2016 | device->flags.match_driver = false; |
Rafael J. Wysocki | 202317a | 2013-11-22 21:54:37 +0100 | [diff] [blame] | 2017 | device->flags.initialized = true; |
| 2018 | device->flags.visited = false; |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 2019 | device_initialize(&device->dev); |
| 2020 | dev_set_uevent_suppress(&device->dev, true); |
| 2021 | } |
Bjorn Helgaas | bc3b077 | 2009-09-21 19:29:20 +0000 | [diff] [blame] | 2022 | |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 2023 | void acpi_device_add_finalize(struct acpi_device *device) |
| 2024 | { |
| 2025 | dev_set_uevent_suppress(&device->dev, false); |
| 2026 | kobject_uevent(&device->dev.kobj, KOBJ_ADD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | } |
| 2028 | |
Bjorn Helgaas | 5c478f4 | 2009-09-21 19:29:35 +0000 | [diff] [blame] | 2029 | static int acpi_add_single_object(struct acpi_device **child, |
| 2030 | acpi_handle handle, int type, |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 2031 | unsigned long long sta) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | { |
Bjorn Helgaas | 77c2488 | 2009-09-21 19:29:30 +0000 | [diff] [blame] | 2033 | int result; |
| 2034 | struct acpi_device *device; |
Bjorn Helgaas | 29aaefa | 2009-09-21 19:28:54 +0000 | [diff] [blame] | 2035 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 2037 | device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | if (!device) { |
Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 2039 | printk(KERN_ERR PREFIX "Memory allocation error\n"); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 2040 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 2043 | acpi_init_device_object(device, handle, type, sta); |
| 2044 | acpi_bus_get_power_flags(device); |
Rafael J. Wysocki | d57d09a | 2011-01-06 23:41:27 +0100 | [diff] [blame] | 2045 | acpi_bus_get_wakeup_device_flags(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 2047 | result = acpi_device_add(device, acpi_device_release); |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 2048 | if (result) { |
Hugh Dickins | 718fb0d | 2009-08-06 23:18:12 +0000 | [diff] [blame] | 2049 | acpi_device_release(&device->dev); |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 2050 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | } |
| 2052 | |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 2053 | acpi_power_add_remove_device(device, true); |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 2054 | acpi_device_add_finalize(device); |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 2055 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
| 2056 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n", |
| 2057 | dev_name(&device->dev), (char *) buffer.pointer, |
| 2058 | device->parent ? dev_name(&device->parent->dev) : "(null)")); |
| 2059 | kfree(buffer.pointer); |
| 2060 | *child = device; |
| 2061 | return 0; |
Rafael J. Wysocki | bf325f9 | 2010-11-25 00:10:44 +0100 | [diff] [blame] | 2062 | } |
| 2063 | |
Bjorn Helgaas | 778cbc1 | 2009-09-21 19:30:06 +0000 | [diff] [blame] | 2064 | static int acpi_bus_type_and_status(acpi_handle handle, int *type, |
| 2065 | unsigned long long *sta) |
| 2066 | { |
| 2067 | acpi_status status; |
| 2068 | acpi_object_type acpi_type; |
| 2069 | |
| 2070 | status = acpi_get_type(handle, &acpi_type); |
| 2071 | if (ACPI_FAILURE(status)) |
| 2072 | return -ENODEV; |
| 2073 | |
| 2074 | switch (acpi_type) { |
| 2075 | case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */ |
| 2076 | case ACPI_TYPE_DEVICE: |
| 2077 | *type = ACPI_BUS_TYPE_DEVICE; |
| 2078 | status = acpi_bus_get_status_handle(handle, sta); |
| 2079 | if (ACPI_FAILURE(status)) |
| 2080 | return -ENODEV; |
| 2081 | break; |
| 2082 | case ACPI_TYPE_PROCESSOR: |
| 2083 | *type = ACPI_BUS_TYPE_PROCESSOR; |
| 2084 | status = acpi_bus_get_status_handle(handle, sta); |
| 2085 | if (ACPI_FAILURE(status)) |
| 2086 | return -ENODEV; |
| 2087 | break; |
| 2088 | case ACPI_TYPE_THERMAL: |
| 2089 | *type = ACPI_BUS_TYPE_THERMAL; |
| 2090 | *sta = ACPI_STA_DEFAULT; |
| 2091 | break; |
| 2092 | case ACPI_TYPE_POWER: |
| 2093 | *type = ACPI_BUS_TYPE_POWER; |
| 2094 | *sta = ACPI_STA_DEFAULT; |
| 2095 | break; |
| 2096 | default: |
| 2097 | return -ENODEV; |
| 2098 | } |
| 2099 | |
| 2100 | return 0; |
| 2101 | } |
| 2102 | |
Rafael J. Wysocki | 202317a | 2013-11-22 21:54:37 +0100 | [diff] [blame] | 2103 | bool acpi_device_is_present(struct acpi_device *adev) |
| 2104 | { |
| 2105 | if (adev->status.present || adev->status.functional) |
| 2106 | return true; |
| 2107 | |
| 2108 | adev->flags.initialized = false; |
| 2109 | return false; |
| 2110 | } |
| 2111 | |
Rafael J. Wysocki | 4b59cc1 | 2013-03-03 23:06:21 +0100 | [diff] [blame] | 2112 | static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler, |
| 2113 | char *idstr, |
| 2114 | const struct acpi_device_id **matchid) |
| 2115 | { |
| 2116 | const struct acpi_device_id *devid; |
| 2117 | |
Rafael J. Wysocki | aca0a4e | 2014-05-30 04:21:52 +0200 | [diff] [blame] | 2118 | if (handler->match) |
| 2119 | return handler->match(idstr, matchid); |
| 2120 | |
Rafael J. Wysocki | 4b59cc1 | 2013-03-03 23:06:21 +0100 | [diff] [blame] | 2121 | for (devid = handler->ids; devid->id[0]; devid++) |
| 2122 | if (!strcmp((char *)devid->id, idstr)) { |
| 2123 | if (matchid) |
| 2124 | *matchid = devid; |
| 2125 | |
| 2126 | return true; |
| 2127 | } |
| 2128 | |
| 2129 | return false; |
| 2130 | } |
| 2131 | |
Toshi Kani | 6b772e8f | 2013-03-04 21:30:43 +0000 | [diff] [blame] | 2132 | static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr, |
| 2133 | const struct acpi_device_id **matchid) |
| 2134 | { |
| 2135 | struct acpi_scan_handler *handler; |
| 2136 | |
| 2137 | list_for_each_entry(handler, &acpi_scan_handlers_list, list_node) |
| 2138 | if (acpi_scan_handler_matching(handler, idstr, matchid)) |
| 2139 | return handler; |
| 2140 | |
| 2141 | return NULL; |
| 2142 | } |
| 2143 | |
Rafael J. Wysocki | 3f8055c | 2013-03-03 23:08:16 +0100 | [diff] [blame] | 2144 | void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val) |
| 2145 | { |
Rafael J. Wysocki | 3f8055c | 2013-03-03 23:08:16 +0100 | [diff] [blame] | 2146 | if (!!hotplug->enabled == !!val) |
| 2147 | return; |
| 2148 | |
| 2149 | mutex_lock(&acpi_scan_lock); |
| 2150 | |
| 2151 | hotplug->enabled = val; |
Rafael J. Wysocki | 3f8055c | 2013-03-03 23:08:16 +0100 | [diff] [blame] | 2152 | |
| 2153 | mutex_unlock(&acpi_scan_lock); |
| 2154 | } |
| 2155 | |
Rafael J. Wysocki | 3c2cc7f | 2014-02-06 17:31:37 +0100 | [diff] [blame] | 2156 | static void acpi_scan_init_hotplug(struct acpi_device *adev) |
Rafael J. Wysocki | c569807 | 2013-03-03 23:05:14 +0100 | [diff] [blame] | 2157 | { |
Toshi Kani | 6b772e8f | 2013-03-04 21:30:43 +0000 | [diff] [blame] | 2158 | struct acpi_hardware_id *hwid; |
Rafael J. Wysocki | c569807 | 2013-03-03 23:05:14 +0100 | [diff] [blame] | 2159 | |
Rafael J. Wysocki | b43109f | 2014-02-16 00:09:34 +0100 | [diff] [blame] | 2160 | if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) { |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 2161 | acpi_dock_add(adev); |
| 2162 | return; |
| 2163 | } |
Rafael J. Wysocki | 3c2cc7f | 2014-02-06 17:31:37 +0100 | [diff] [blame] | 2164 | list_for_each_entry(hwid, &adev->pnp.ids, list) { |
| 2165 | struct acpi_scan_handler *handler; |
Rafael J. Wysocki | c569807 | 2013-03-03 23:05:14 +0100 | [diff] [blame] | 2166 | |
Toshi Kani | 6b772e8f | 2013-03-04 21:30:43 +0000 | [diff] [blame] | 2167 | handler = acpi_scan_match_handler(hwid->id, NULL); |
Rafael J. Wysocki | 1a69947 | 2014-02-06 13:58:13 +0100 | [diff] [blame] | 2168 | if (handler) { |
| 2169 | adev->flags.hotplug_notify = true; |
| 2170 | break; |
| 2171 | } |
Rafael J. Wysocki | 3c2cc7f | 2014-02-06 17:31:37 +0100 | [diff] [blame] | 2172 | } |
Rafael J. Wysocki | a33ec39 | 2013-03-03 23:05:29 +0100 | [diff] [blame] | 2173 | } |
| 2174 | |
Rafael J. Wysocki | e386309 | 2012-12-21 00:36:47 +0100 | [diff] [blame] | 2175 | static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used, |
| 2176 | void *not_used, void **return_value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | { |
Rafael J. Wysocki | 805d410 | 2012-12-21 00:36:39 +0100 | [diff] [blame] | 2178 | struct acpi_device *device = NULL; |
Bjorn Helgaas | 778cbc1 | 2009-09-21 19:30:06 +0000 | [diff] [blame] | 2179 | int type; |
| 2180 | unsigned long long sta; |
| 2181 | int result; |
Bjorn Helgaas | 51a85fa | 2009-09-21 19:29:56 +0000 | [diff] [blame] | 2182 | |
Rafael J. Wysocki | 4002bf3 | 2012-12-21 00:36:44 +0100 | [diff] [blame] | 2183 | acpi_bus_get_device(handle, &device); |
| 2184 | if (device) |
| 2185 | goto out; |
| 2186 | |
Bjorn Helgaas | 778cbc1 | 2009-09-21 19:30:06 +0000 | [diff] [blame] | 2187 | result = acpi_bus_type_and_status(handle, &type, &sta); |
| 2188 | if (result) |
Bjorn Helgaas | 51a85fa | 2009-09-21 19:29:56 +0000 | [diff] [blame] | 2189 | return AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 2191 | if (type == ACPI_BUS_TYPE_POWER) { |
| 2192 | acpi_add_power_resource(handle); |
| 2193 | return AE_OK; |
| 2194 | } |
| 2195 | |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 2196 | acpi_add_single_object(&device, handle, type, sta); |
Bjorn Helgaas | e3b87f8 | 2009-09-21 19:30:11 +0000 | [diff] [blame] | 2197 | if (!device) |
Bjorn Helgaas | 51a85fa | 2009-09-21 19:29:56 +0000 | [diff] [blame] | 2198 | return AE_CTRL_DEPTH; |
| 2199 | |
Rafael J. Wysocki | 3c2cc7f | 2014-02-06 17:31:37 +0100 | [diff] [blame] | 2200 | acpi_scan_init_hotplug(device); |
| 2201 | |
Rafael J. Wysocki | 4002bf3 | 2012-12-21 00:36:44 +0100 | [diff] [blame] | 2202 | out: |
Bjorn Helgaas | 51a85fa | 2009-09-21 19:29:56 +0000 | [diff] [blame] | 2203 | if (!*return_value) |
| 2204 | *return_value = device; |
Rafael J. Wysocki | 805d410 | 2012-12-21 00:36:39 +0100 | [diff] [blame] | 2205 | |
Bjorn Helgaas | 51a85fa | 2009-09-21 19:29:56 +0000 | [diff] [blame] | 2206 | return AE_OK; |
| 2207 | } |
| 2208 | |
Zhang Rui | 4845934 | 2014-05-30 14:35:34 +0200 | [diff] [blame] | 2209 | static int acpi_check_spi_i2c_slave(struct acpi_resource *ares, void *data) |
| 2210 | { |
| 2211 | bool *is_spi_i2c_slave_p = data; |
| 2212 | |
| 2213 | if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS) |
| 2214 | return 1; |
| 2215 | |
| 2216 | /* |
| 2217 | * devices that are connected to UART still need to be enumerated to |
| 2218 | * platform bus |
| 2219 | */ |
| 2220 | if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART) |
| 2221 | *is_spi_i2c_slave_p = true; |
| 2222 | |
| 2223 | /* no need to do more checking */ |
| 2224 | return -1; |
| 2225 | } |
| 2226 | |
| 2227 | static void acpi_default_enumeration(struct acpi_device *device) |
| 2228 | { |
| 2229 | struct list_head resource_list; |
| 2230 | bool is_spi_i2c_slave = false; |
| 2231 | |
| 2232 | if (!device->pnp.type.platform_id || device->handler) |
| 2233 | return; |
| 2234 | |
| 2235 | /* |
| 2236 | * Do not enemerate SPI/I2C slaves as they will be enuerated by their |
| 2237 | * respective parents. |
| 2238 | */ |
| 2239 | INIT_LIST_HEAD(&resource_list); |
| 2240 | acpi_dev_get_resources(device, &resource_list, acpi_check_spi_i2c_slave, |
| 2241 | &is_spi_i2c_slave); |
| 2242 | acpi_dev_free_resource_list(&resource_list); |
| 2243 | if (!is_spi_i2c_slave) |
| 2244 | acpi_create_platform_device(device); |
| 2245 | } |
| 2246 | |
Rafael J. Wysocki | 87b85b3 | 2013-02-06 13:05:22 +0100 | [diff] [blame] | 2247 | static int acpi_scan_attach_handler(struct acpi_device *device) |
| 2248 | { |
| 2249 | struct acpi_hardware_id *hwid; |
| 2250 | int ret = 0; |
| 2251 | |
| 2252 | list_for_each_entry(hwid, &device->pnp.ids, list) { |
Rafael J. Wysocki | c569807 | 2013-03-03 23:05:14 +0100 | [diff] [blame] | 2253 | const struct acpi_device_id *devid; |
| 2254 | struct acpi_scan_handler *handler; |
Rafael J. Wysocki | 87b85b3 | 2013-02-06 13:05:22 +0100 | [diff] [blame] | 2255 | |
Rafael J. Wysocki | c569807 | 2013-03-03 23:05:14 +0100 | [diff] [blame] | 2256 | handler = acpi_scan_match_handler(hwid->id, &devid); |
| 2257 | if (handler) { |
Rafael J. Wysocki | d34afa9 | 2014-05-30 04:27:31 +0200 | [diff] [blame] | 2258 | if (!handler->attach) { |
| 2259 | device->pnp.type.platform_id = 0; |
| 2260 | continue; |
| 2261 | } |
Rafael J. Wysocki | 9cb32ac | 2014-02-11 00:35:46 +0100 | [diff] [blame] | 2262 | device->handler = handler; |
Rafael J. Wysocki | c569807 | 2013-03-03 23:05:14 +0100 | [diff] [blame] | 2263 | ret = handler->attach(device, devid); |
Rafael J. Wysocki | 9cb32ac | 2014-02-11 00:35:46 +0100 | [diff] [blame] | 2264 | if (ret > 0) |
Rafael J. Wysocki | c569807 | 2013-03-03 23:05:14 +0100 | [diff] [blame] | 2265 | break; |
Rafael J. Wysocki | 9cb32ac | 2014-02-11 00:35:46 +0100 | [diff] [blame] | 2266 | |
| 2267 | device->handler = NULL; |
| 2268 | if (ret < 0) |
Rafael J. Wysocki | c569807 | 2013-03-03 23:05:14 +0100 | [diff] [blame] | 2269 | break; |
Rafael J. Wysocki | c569807 | 2013-03-03 23:05:14 +0100 | [diff] [blame] | 2270 | } |
Rafael J. Wysocki | 87b85b3 | 2013-02-06 13:05:22 +0100 | [diff] [blame] | 2271 | } |
Zhang Rui | 4845934 | 2014-05-30 14:35:34 +0200 | [diff] [blame] | 2272 | if (!ret) |
| 2273 | acpi_default_enumeration(device); |
| 2274 | |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 2275 | return ret; |
| 2276 | } |
| 2277 | |
Rafael J. Wysocki | 2c22e65 | 2013-11-25 00:52:21 +0100 | [diff] [blame] | 2278 | static void acpi_bus_attach(struct acpi_device *device) |
Bjorn Helgaas | 51a85fa | 2009-09-21 19:29:56 +0000 | [diff] [blame] | 2279 | { |
Rafael J. Wysocki | 2c22e65 | 2013-11-25 00:52:21 +0100 | [diff] [blame] | 2280 | struct acpi_device *child; |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 2281 | acpi_handle ejd; |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 2282 | int ret; |
Bjorn Helgaas | 51a85fa | 2009-09-21 19:29:56 +0000 | [diff] [blame] | 2283 | |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 2284 | if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd))) |
| 2285 | register_dock_dependent_device(device, ejd); |
| 2286 | |
Rafael J. Wysocki | 2c22e65 | 2013-11-25 00:52:21 +0100 | [diff] [blame] | 2287 | acpi_bus_get_status(device); |
Rafael J. Wysocki | 202317a | 2013-11-22 21:54:37 +0100 | [diff] [blame] | 2288 | /* Skip devices that are not present. */ |
Rafael J. Wysocki | 2c22e65 | 2013-11-25 00:52:21 +0100 | [diff] [blame] | 2289 | if (!acpi_device_is_present(device)) { |
| 2290 | device->flags.visited = false; |
| 2291 | return; |
| 2292 | } |
Rafael J. Wysocki | 3a391a3 | 2013-07-12 13:45:59 +0200 | [diff] [blame] | 2293 | if (device->handler) |
Rafael J. Wysocki | 2c22e65 | 2013-11-25 00:52:21 +0100 | [diff] [blame] | 2294 | goto ok; |
Rafael J. Wysocki | 3a391a3 | 2013-07-12 13:45:59 +0200 | [diff] [blame] | 2295 | |
Rafael J. Wysocki | 202317a | 2013-11-22 21:54:37 +0100 | [diff] [blame] | 2296 | if (!device->flags.initialized) { |
| 2297 | acpi_bus_update_power(device, NULL); |
| 2298 | device->flags.initialized = true; |
| 2299 | } |
Rafael J. Wysocki | 2c22e65 | 2013-11-25 00:52:21 +0100 | [diff] [blame] | 2300 | device->flags.visited = false; |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 2301 | ret = acpi_scan_attach_handler(device); |
Rafael J. Wysocki | 6931007 | 2013-11-07 01:41:01 +0100 | [diff] [blame] | 2302 | if (ret < 0) |
Rafael J. Wysocki | 2c22e65 | 2013-11-25 00:52:21 +0100 | [diff] [blame] | 2303 | return; |
Rafael J. Wysocki | 6931007 | 2013-11-07 01:41:01 +0100 | [diff] [blame] | 2304 | |
| 2305 | device->flags.match_driver = true; |
Rafael J. Wysocki | 2c22e65 | 2013-11-25 00:52:21 +0100 | [diff] [blame] | 2306 | if (!ret) { |
| 2307 | ret = device_attach(&device->dev); |
| 2308 | if (ret < 0) |
| 2309 | return; |
| 2310 | } |
| 2311 | device->flags.visited = true; |
Rafael J. Wysocki | 202317a | 2013-11-22 21:54:37 +0100 | [diff] [blame] | 2312 | |
| 2313 | ok: |
Rafael J. Wysocki | 2c22e65 | 2013-11-25 00:52:21 +0100 | [diff] [blame] | 2314 | list_for_each_entry(child, &device->children, node) |
| 2315 | acpi_bus_attach(child); |
Rafael J. Wysocki | 8ab17fc | 2014-09-21 02:58:18 +0200 | [diff] [blame] | 2316 | |
| 2317 | if (device->handler && device->handler->hotplug.notify_online) |
| 2318 | device->handler->hotplug.notify_online(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | |
Rafael J. Wysocki | b8bd759 | 2013-01-19 01:27:35 +0100 | [diff] [blame] | 2321 | /** |
| 2322 | * acpi_bus_scan - Add ACPI device node objects in a given namespace scope. |
| 2323 | * @handle: Root of the namespace scope to scan. |
Thomas Renninger | 7779688 | 2010-01-29 17:48:52 +0100 | [diff] [blame] | 2324 | * |
Rafael J. Wysocki | b8bd759 | 2013-01-19 01:27:35 +0100 | [diff] [blame] | 2325 | * Scan a given ACPI tree (probably recently hot-plugged) and create and add |
| 2326 | * found devices. |
Thomas Renninger | 7779688 | 2010-01-29 17:48:52 +0100 | [diff] [blame] | 2327 | * |
Rafael J. Wysocki | b8bd759 | 2013-01-19 01:27:35 +0100 | [diff] [blame] | 2328 | * If no devices were found, -ENODEV is returned, but it does not mean that |
| 2329 | * there has been a real error. There just have been no suitable ACPI objects |
| 2330 | * in the table trunk from which the kernel could create a device and add an |
| 2331 | * appropriate driver. |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 2332 | * |
| 2333 | * Must be called under acpi_scan_lock. |
Thomas Renninger | 7779688 | 2010-01-29 17:48:52 +0100 | [diff] [blame] | 2334 | */ |
Rafael J. Wysocki | b8bd759 | 2013-01-19 01:27:35 +0100 | [diff] [blame] | 2335 | int acpi_bus_scan(acpi_handle handle) |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 2336 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | void *device = NULL; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 2338 | |
Rafael J. Wysocki | 0cd6ac5 | 2012-12-21 00:36:49 +0100 | [diff] [blame] | 2339 | if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, |
Rafael J. Wysocki | e386309 | 2012-12-21 00:36:47 +0100 | [diff] [blame] | 2341 | acpi_bus_check_add, NULL, NULL, &device); |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 2342 | |
Rafael J. Wysocki | 2c22e65 | 2013-11-25 00:52:21 +0100 | [diff] [blame] | 2343 | if (device) { |
| 2344 | acpi_bus_attach(device); |
| 2345 | return 0; |
| 2346 | } |
| 2347 | return -ENODEV; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 2348 | } |
Rafael J. Wysocki | b8bd759 | 2013-01-19 01:27:35 +0100 | [diff] [blame] | 2349 | EXPORT_SYMBOL(acpi_bus_scan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 2351 | /** |
Rafael J. Wysocki | 2c22e65 | 2013-11-25 00:52:21 +0100 | [diff] [blame] | 2352 | * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects. |
| 2353 | * @adev: Root of the ACPI namespace scope to walk. |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 2354 | * |
| 2355 | * Must be called under acpi_scan_lock. |
| 2356 | */ |
Rafael J. Wysocki | 2c22e65 | 2013-11-25 00:52:21 +0100 | [diff] [blame] | 2357 | void acpi_bus_trim(struct acpi_device *adev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | { |
Rafael J. Wysocki | 2c22e65 | 2013-11-25 00:52:21 +0100 | [diff] [blame] | 2359 | struct acpi_scan_handler *handler = adev->handler; |
| 2360 | struct acpi_device *child; |
| 2361 | |
| 2362 | list_for_each_entry_reverse(child, &adev->children, node) |
| 2363 | acpi_bus_trim(child); |
| 2364 | |
Rafael J. Wysocki | a951c77 | 2014-01-27 23:08:09 +0100 | [diff] [blame] | 2365 | adev->flags.match_driver = false; |
Rafael J. Wysocki | 2c22e65 | 2013-11-25 00:52:21 +0100 | [diff] [blame] | 2366 | if (handler) { |
| 2367 | if (handler->detach) |
| 2368 | handler->detach(adev); |
| 2369 | |
| 2370 | adev->handler = NULL; |
| 2371 | } else { |
| 2372 | device_release_driver(&adev->dev); |
| 2373 | } |
Rafael J. Wysocki | cecdb19 | 2013-01-15 13:24:02 +0100 | [diff] [blame] | 2374 | /* |
Rafael J. Wysocki | 2c22e65 | 2013-11-25 00:52:21 +0100 | [diff] [blame] | 2375 | * Most likely, the device is going away, so put it into D3cold before |
| 2376 | * that. |
Rafael J. Wysocki | 05404d8 | 2013-01-15 13:24:13 +0100 | [diff] [blame] | 2377 | */ |
Rafael J. Wysocki | 2c22e65 | 2013-11-25 00:52:21 +0100 | [diff] [blame] | 2378 | acpi_device_set_power(adev, ACPI_STATE_D3_COLD); |
| 2379 | adev->flags.initialized = false; |
| 2380 | adev->flags.visited = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | } |
Kristen Accardi | ceaba66 | 2006-02-23 17:56:01 -0800 | [diff] [blame] | 2382 | EXPORT_SYMBOL_GPL(acpi_bus_trim); |
| 2383 | |
Bjorn Helgaas | e8b945c | 2009-09-21 19:28:59 +0000 | [diff] [blame] | 2384 | static int acpi_bus_scan_fixed(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 2386 | int result = 0; |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 2387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | /* |
| 2389 | * Enumerate all fixed-feature devices. |
| 2390 | */ |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 2391 | if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) { |
| 2392 | struct acpi_device *device = NULL; |
| 2393 | |
Bjorn Helgaas | 5c478f4 | 2009-09-21 19:29:35 +0000 | [diff] [blame] | 2394 | result = acpi_add_single_object(&device, NULL, |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 2395 | ACPI_BUS_TYPE_POWER_BUTTON, |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 2396 | ACPI_STA_DEFAULT); |
| 2397 | if (result) |
| 2398 | return result; |
| 2399 | |
Rafael J. Wysocki | 8834616 | 2013-11-18 14:18:47 +0100 | [diff] [blame] | 2400 | device->flags.match_driver = true; |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 2401 | result = device_attach(&device->dev); |
| 2402 | if (result < 0) |
| 2403 | return result; |
| 2404 | |
Daniel Drake | c10d7a1 | 2012-05-10 00:08:43 +0100 | [diff] [blame] | 2405 | device_init_wakeup(&device->dev, true); |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 2406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 2408 | if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) { |
| 2409 | struct acpi_device *device = NULL; |
| 2410 | |
Bjorn Helgaas | 5c478f4 | 2009-09-21 19:29:35 +0000 | [diff] [blame] | 2411 | result = acpi_add_single_object(&device, NULL, |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 2412 | ACPI_BUS_TYPE_SLEEP_BUTTON, |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 2413 | ACPI_STA_DEFAULT); |
| 2414 | if (result) |
| 2415 | return result; |
| 2416 | |
Rafael J. Wysocki | 8834616 | 2013-11-18 14:18:47 +0100 | [diff] [blame] | 2417 | device->flags.match_driver = true; |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 2418 | result = device_attach(&device->dev); |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 2419 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 2421 | return result < 0 ? result : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | } |
| 2423 | |
Bjorn Helgaas | e747f27 | 2009-03-24 16:49:43 -0600 | [diff] [blame] | 2424 | int __init acpi_scan_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2425 | { |
| 2426 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | |
Patrick Mochel | 5b32726 | 2006-05-10 10:33:00 -0400 | [diff] [blame] | 2428 | result = bus_register(&acpi_bus_type); |
| 2429 | if (result) { |
| 2430 | /* We don't want to quit even if we failed to add suspend/resume */ |
| 2431 | printk(KERN_ERR PREFIX "Could not register bus type\n"); |
| 2432 | } |
| 2433 | |
Rafael J. Wysocki | 92ef2a2 | 2012-12-21 00:36:40 +0100 | [diff] [blame] | 2434 | acpi_pci_root_init(); |
Rafael J. Wysocki | 4daeaf6 | 2013-01-30 14:27:37 +0100 | [diff] [blame] | 2435 | acpi_pci_link_init(); |
Rafael J. Wysocki | ac212b6 | 2013-05-03 00:26:22 +0200 | [diff] [blame] | 2436 | acpi_processor_init(); |
Rafael J. Wysocki | f58b082 | 2013-03-06 23:46:20 +0100 | [diff] [blame] | 2437 | acpi_lpss_init(); |
Lan Tianyu | 2fa97fe | 2013-06-05 02:27:50 +0000 | [diff] [blame] | 2438 | acpi_cmos_rtc_init(); |
Rafael J. Wysocki | 737f1a9 | 2013-02-08 23:52:39 +0100 | [diff] [blame] | 2439 | acpi_container_init(); |
Rafael J. Wysocki | 0a34764 | 2013-03-03 23:18:03 +0100 | [diff] [blame] | 2440 | acpi_memory_hotplug_init(); |
Zhang Rui | eec15ed | 2014-05-30 04:23:01 +0200 | [diff] [blame] | 2441 | acpi_pnp_init(); |
Zhang Rui | 3230bbf | 2014-03-14 00:34:05 +0800 | [diff] [blame] | 2442 | acpi_int340x_thermal_init(); |
Rafael J. Wysocki | 97d9a9e | 2010-11-25 00:10:02 +0100 | [diff] [blame] | 2443 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 2444 | mutex_lock(&acpi_scan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | * Enumerate devices in the ACPI namespace. |
| 2447 | */ |
Rafael J. Wysocki | 0cd6ac5 | 2012-12-21 00:36:49 +0100 | [diff] [blame] | 2448 | result = acpi_bus_scan(ACPI_ROOT_OBJECT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | if (result) |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 2450 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | |
Rafael J. Wysocki | 0cd6ac5 | 2012-12-21 00:36:49 +0100 | [diff] [blame] | 2452 | result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | if (result) |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 2454 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | |
Aaron Lu | 2650ef4 | 2014-04-28 10:38:04 +0800 | [diff] [blame] | 2456 | /* Fixed feature devices do not exist on HW-reduced platform */ |
| 2457 | if (!acpi_gbl_reduced_hardware) { |
| 2458 | result = acpi_bus_scan_fixed(); |
| 2459 | if (result) { |
| 2460 | acpi_detach_data(acpi_root->handle, |
| 2461 | acpi_scan_drop_device); |
| 2462 | acpi_device_del(acpi_root); |
| 2463 | put_device(&acpi_root->dev); |
| 2464 | goto out; |
| 2465 | } |
Rafael J. Wysocki | b8bd759 | 2013-01-19 01:27:35 +0100 | [diff] [blame] | 2466 | } |
| 2467 | |
| 2468 | acpi_update_all_gpes(); |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 2469 | |
| 2470 | out: |
| 2471 | mutex_unlock(&acpi_scan_lock); |
| 2472 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | } |