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