blob: b19283b336c74b156e8723fbb8756a3afb7cb1b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/acpi.h>
Alok N Kataria74523c92008-06-13 12:54:24 -040010#include <linux/signal.h>
11#include <linux/kthread.h>
Darrick J. Wong222e82a2010-03-24 14:38:37 +010012#include <linux/dmi.h>
Lance Ortizd1efe3c2012-10-02 12:43:23 -060013#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Rafael J. Wysockid7831562013-11-22 21:52:12 +010015#include <asm/pgtable.h>
16
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060017#include "internal.h"
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050020ACPI_MODULE_NAME("scan");
Len Brown4be44fc2005-08-05 00:44:28 -040021extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020024#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define ACPI_BUS_DEVICE_NAME "System Bus"
26
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +000027#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
28
Rafael J. Wysockid7831562013-11-22 21:52:12 +010029#define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
30
Rafael J. Wysocki683058e2013-05-03 00:26:16 +020031/*
32 * If set, devices will be hot-removed even if they cannot be put offline
33 * gracefully (from the kernel's standpoint).
34 */
35bool acpi_force_hot_remove;
36
Thomas Renninger620e1122010-10-01 10:54:00 +020037static const char *dummy_hid = "device";
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020038
Lan Tianyu40e7fcb2014-11-23 21:22:54 +080039static LIST_HEAD(acpi_dep_list);
40static DEFINE_MUTEX(acpi_dep_list_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080041static LIST_HEAD(acpi_bus_id_list);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +010042static DEFINE_MUTEX(acpi_scan_lock);
Rafael J. Wysockica589f92013-01-30 14:27:29 +010043static LIST_HEAD(acpi_scan_handlers_list);
Shaohua Li90905892009-04-07 10:24:29 +080044DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045LIST_HEAD(acpi_wakeup_device_list);
Rafael J. Wysockie5255062014-02-04 00:43:17 +010046static DEFINE_MUTEX(acpi_hp_context_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Lan Tianyu40e7fcb2014-11-23 21:22:54 +080048struct acpi_dep_data {
49 struct list_head node;
50 acpi_handle master;
51 acpi_handle slave;
52};
53
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080054struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080055 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080056 unsigned int instance_no;
57 struct list_head node;
58};
Thomas Renninger29b71a12007-07-23 14:43:51 +020059
Rafael J. Wysocki3757b942013-02-13 14:36:47 +010060void acpi_scan_lock_acquire(void)
61{
62 mutex_lock(&acpi_scan_lock);
63}
64EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
65
66void acpi_scan_lock_release(void)
67{
68 mutex_unlock(&acpi_scan_lock);
69}
70EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
71
Rafael J. Wysockie5255062014-02-04 00:43:17 +010072void acpi_lock_hp_context(void)
73{
74 mutex_lock(&acpi_hp_context_lock);
75}
76
77void acpi_unlock_hp_context(void)
78{
79 mutex_unlock(&acpi_hp_context_lock);
80}
81
Rafael J. Wysocki5d513202014-02-22 00:48:31 +010082void 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. Wysockiba574dc2014-07-15 22:03:22 +020088 hp->notify = notify;
89 hp->uevent = uevent;
90 acpi_set_hp_context(adev, hp);
Rafael J. Wysocki5d513202014-02-22 00:48:31 +010091 acpi_unlock_hp_context();
92}
93EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
94
Rafael J. Wysockica589f92013-01-30 14:27:29 +010095int acpi_scan_add_handler(struct acpi_scan_handler *handler)
96{
Rafael J. Wysockid34afa92014-05-30 04:27:31 +020097 if (!handler)
Rafael J. Wysockica589f92013-01-30 14:27:29 +010098 return -EINVAL;
99
100 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
101 return 0;
102}
103
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +0100104int 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
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200117/**
118 * create_pnp_modalias - Create hid/cid(s) string for modalias and uevent
119 * @acpi_dev: ACPI device object.
120 * @modalias: Buffer to print into.
121 * @size: Size of the buffer.
122 *
Thomas Renninger29b71a12007-07-23 14:43:51 +0200123 * Creates hid/cid(s) string needed for modalias and uevent
124 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
125 * char *modalias: "acpi:IBM0001:ACPI0001"
Zhang Rui3d8e0092014-01-14 16:46:35 +0800126 * Return: 0: no _HID and no _CID
127 * -EINVAL: output error
128 * -ENOMEM: output is truncated
Thomas Renninger29b71a12007-07-23 14:43:51 +0200129*/
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200130static int create_pnp_modalias(struct acpi_device *acpi_dev, char *modalias,
131 int size)
Adrian Bunkb3e572d2007-08-14 23:22:35 +0200132{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200133 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800134 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600135 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200136
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200137 /*
Rafael J. Wysockiee892092015-05-22 04:24:34 +0200138 * Since we skip ACPI_DT_NAMESPACE_HID from the modalias below, 0 should
139 * be returned if ACPI_DT_NAMESPACE_HID is the only ACPI/PNP ID in the
140 * device's list.
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200141 */
142 count = 0;
143 list_for_each_entry(id, &acpi_dev->pnp.ids, list)
Rafael J. Wysockiee892092015-05-22 04:24:34 +0200144 if (strcmp(id->id, ACPI_DT_NAMESPACE_HID))
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200145 count++;
146
147 if (!count)
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200148 return 0;
149
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200150 len = snprintf(modalias, size, "acpi:");
151 if (len <= 0)
152 return len;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200153
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200154 size -= len;
Mika Westerberg733e6252014-10-21 13:33:56 +0200155
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200156 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
Rafael J. Wysockiee892092015-05-22 04:24:34 +0200157 if (!strcmp(id->id, ACPI_DT_NAMESPACE_HID))
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200158 continue;
Mika Westerberg733e6252014-10-21 13:33:56 +0200159
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200160 count = snprintf(&modalias[len], size, "%s:", id->id);
161 if (count < 0)
162 return -EINVAL;
Mika Westerberg733e6252014-10-21 13:33:56 +0200163
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200164 if (count >= size)
165 return -ENOMEM;
Mika Westerberg733e6252014-10-21 13:33:56 +0200166
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200167 len += count;
168 size -= count;
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800169 }
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200170 modalias[len] = '\0';
171 return len;
172}
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800173
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200174/**
175 * create_of_modalias - Creates DT compatible string for modalias and uevent
176 * @acpi_dev: ACPI device object.
177 * @modalias: Buffer to print into.
178 * @size: Size of the buffer.
179 *
180 * Expose DT compatible modalias as of:NnameTCcompatible. This function should
Rafael J. Wysockiee892092015-05-22 04:24:34 +0200181 * only be called for devices having ACPI_DT_NAMESPACE_HID in their list of
182 * ACPI/PNP IDs.
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200183 */
184static int create_of_modalias(struct acpi_device *acpi_dev, char *modalias,
185 int size)
186{
187 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
188 const union acpi_object *of_compatible, *obj;
189 int len, count;
190 int i, nval;
191 char *c;
192
193 acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf);
194 /* DT strings are all in lower case */
195 for (c = buf.pointer; *c != '\0'; c++)
196 *c = tolower(*c);
197
198 len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer);
199 ACPI_FREE(buf.pointer);
200
201 if (len <= 0)
202 return len;
203
204 of_compatible = acpi_dev->data.of_compatible;
205 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
206 nval = of_compatible->package.count;
207 obj = of_compatible->package.elements;
208 } else { /* Must be ACPI_TYPE_STRING. */
209 nval = 1;
210 obj = of_compatible;
211 }
212 for (i = 0; i < nval; i++, obj++) {
213 count = snprintf(&modalias[len], size, "C%s",
214 obj->string.pointer);
215 if (count < 0)
216 return -EINVAL;
217
218 if (count >= size)
219 return -ENOMEM;
220
221 len += count;
222 size -= count;
223 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200224 modalias[len] = '\0';
225 return len;
226}
227
Zhang Rui6eb24512014-01-14 16:46:36 +0800228/*
Mika Westerberg52870782014-10-24 16:40:54 +0200229 * acpi_companion_match() - Can we match via ACPI companion device
230 * @dev: Device in question
231 *
232 * Check if the given device has an ACPI companion and if that companion has
233 * a valid list of PNP IDs, and if the device is the first (primary) physical
Rafael J. Wysockie1acdeb2015-04-10 16:07:38 +0200234 * device associated with it. Return the companion pointer if that's the case
235 * or NULL otherwise.
Mika Westerberg52870782014-10-24 16:40:54 +0200236 *
237 * If multiple physical devices are attached to a single ACPI companion, we need
238 * to be careful. The usage scenario for this kind of relationship is that all
239 * of the physical devices in question use resources provided by the ACPI
240 * companion. A typical case is an MFD device where all the sub-devices share
241 * the parent's ACPI companion. In such cases we can only allow the primary
242 * (first) physical device to be matched with the help of the companion's PNP
243 * IDs.
244 *
245 * Additional physical devices sharing the ACPI companion can still use
246 * resources available from it but they will be matched normally using functions
247 * provided by their bus types (and analogously for their modalias).
248 */
Rafael J. Wysockie1acdeb2015-04-10 16:07:38 +0200249static struct acpi_device *acpi_companion_match(const struct device *dev)
Mika Westerberg52870782014-10-24 16:40:54 +0200250{
251 struct acpi_device *adev;
Rafael J. Wysocki5f2e3272015-04-13 21:35:51 +0200252 struct mutex *physical_node_lock;
Mika Westerberg52870782014-10-24 16:40:54 +0200253
254 adev = ACPI_COMPANION(dev);
255 if (!adev)
Rafael J. Wysockie1acdeb2015-04-10 16:07:38 +0200256 return NULL;
Mika Westerberg52870782014-10-24 16:40:54 +0200257
258 if (list_empty(&adev->pnp.ids))
Rafael J. Wysockie1acdeb2015-04-10 16:07:38 +0200259 return NULL;
Mika Westerberg52870782014-10-24 16:40:54 +0200260
Rafael J. Wysocki5f2e3272015-04-13 21:35:51 +0200261 physical_node_lock = &adev->physical_node_lock;
262 mutex_lock(physical_node_lock);
Mika Westerberg52870782014-10-24 16:40:54 +0200263 if (list_empty(&adev->physical_node_list)) {
Rafael J. Wysockie1acdeb2015-04-10 16:07:38 +0200264 adev = NULL;
Mika Westerberg52870782014-10-24 16:40:54 +0200265 } else {
266 const struct acpi_device_physical_node *node;
267
268 node = list_first_entry(&adev->physical_node_list,
269 struct acpi_device_physical_node, node);
Rafael J. Wysockie1acdeb2015-04-10 16:07:38 +0200270 if (node->dev != dev)
271 adev = NULL;
Mika Westerberg52870782014-10-24 16:40:54 +0200272 }
Rafael J. Wysocki5f2e3272015-04-13 21:35:51 +0200273 mutex_unlock(physical_node_lock);
Mika Westerberg52870782014-10-24 16:40:54 +0200274
Rafael J. Wysockie1acdeb2015-04-10 16:07:38 +0200275 return adev;
Mika Westerberg52870782014-10-24 16:40:54 +0200276}
277
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200278static int __acpi_device_uevent_modalias(struct acpi_device *adev,
279 struct kobj_uevent_env *env)
280{
281 int len;
282
283 if (!adev)
284 return -ENODEV;
285
286 if (list_empty(&adev->pnp.ids))
287 return 0;
288
289 if (add_uevent_var(env, "MODALIAS="))
290 return -ENOMEM;
291
292 len = create_pnp_modalias(adev, &env->buf[env->buflen - 1],
293 sizeof(env->buf) - env->buflen);
294 if (len < 0)
295 return len;
296
297 env->buflen += len;
298 if (!adev->data.of_compatible)
299 return 0;
300
301 if (len > 0 && add_uevent_var(env, "MODALIAS="))
302 return -ENOMEM;
303
304 len = create_of_modalias(adev, &env->buf[env->buflen - 1],
305 sizeof(env->buf) - env->buflen);
306 if (len < 0)
307 return len;
308
309 env->buflen += len;
310
311 return 0;
312}
313
Mika Westerberg52870782014-10-24 16:40:54 +0200314/*
Zhang Rui6eb24512014-01-14 16:46:36 +0800315 * Creates uevent modalias field for ACPI enumerated devices.
316 * Because the other buses does not support ACPI HIDs & CIDs.
317 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
318 * "acpi:IBM0001:ACPI0001"
319 */
320int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
321{
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200322 return __acpi_device_uevent_modalias(acpi_companion_match(dev), env);
Zhang Rui6eb24512014-01-14 16:46:36 +0800323}
324EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
325
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200326static int __acpi_device_modalias(struct acpi_device *adev, char *buf, int size)
327{
328 int len, count;
329
330 if (!adev)
331 return -ENODEV;
332
333 if (list_empty(&adev->pnp.ids))
334 return 0;
335
336 len = create_pnp_modalias(adev, buf, size - 1);
337 if (len < 0) {
338 return len;
339 } else if (len > 0) {
340 buf[len++] = '\n';
341 size -= len;
342 }
343 if (!adev->data.of_compatible)
344 return len;
345
346 count = create_of_modalias(adev, buf + len, size - 1);
347 if (count < 0) {
348 return count;
349 } else if (count > 0) {
350 len += count;
351 buf[len++] = '\n';
352 }
353
354 return len;
355}
356
Zhang Rui6eb24512014-01-14 16:46:36 +0800357/*
358 * Creates modalias sysfs attribute for ACPI enumerated devices.
359 * Because the other buses does not support ACPI HIDs & CIDs.
360 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
361 * "acpi:IBM0001:ACPI0001"
362 */
363int acpi_device_modalias(struct device *dev, char *buf, int size)
364{
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200365 return __acpi_device_modalias(acpi_companion_match(dev), buf, size);
Zhang Rui6eb24512014-01-14 16:46:36 +0800366}
367EXPORT_SYMBOL_GPL(acpi_device_modalias);
368
Thomas Renninger29b71a12007-07-23 14:43:51 +0200369static ssize_t
370acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200371 return __acpi_device_modalias(to_acpi_device(dev), buf, 1024);
Thomas Renninger29b71a12007-07-23 14:43:51 +0200372}
373static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
374
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100375bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100376{
377 struct acpi_device_physical_node *pn;
378 bool offline = true;
379
Rafael J. Wysocki4c533c82015-04-18 01:25:46 +0200380 /*
381 * acpi_container_offline() calls this for all of the container's
382 * children under the container's physical_node_lock lock.
383 */
384 mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100385
386 list_for_each_entry(pn, &adev->physical_node_list, node)
387 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100388 if (uevent)
389 kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
390
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100391 offline = false;
392 break;
393 }
394
395 mutex_unlock(&adev->physical_node_lock);
396 return offline;
397}
398
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100399static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
400 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200401{
402 struct acpi_device *device = NULL;
403 struct acpi_device_physical_node *pn;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200404 bool second_pass = (bool)data;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200405 acpi_status status = AE_OK;
406
407 if (acpi_bus_get_device(handle, &device))
408 return AE_OK;
409
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100410 if (device->handler && !device->handler->hotplug.enabled) {
411 *ret_p = &device->dev;
412 return AE_SUPPORT;
413 }
414
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200415 mutex_lock(&device->physical_node_lock);
416
417 list_for_each_entry(pn, &device->physical_node_list, node) {
418 int ret;
419
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200420 if (second_pass) {
421 /* Skip devices offlined by the first pass. */
422 if (pn->put_online)
423 continue;
424 } else {
425 pn->put_online = false;
426 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200427 ret = device_offline(pn->dev);
428 if (acpi_force_hot_remove)
429 continue;
430
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200431 if (ret >= 0) {
432 pn->put_online = !ret;
433 } else {
434 *ret_p = pn->dev;
435 if (second_pass) {
436 status = AE_ERROR;
437 break;
438 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200439 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200440 }
441
442 mutex_unlock(&device->physical_node_lock);
443
444 return status;
445}
446
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100447static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
448 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200449{
450 struct acpi_device *device = NULL;
451 struct acpi_device_physical_node *pn;
452
453 if (acpi_bus_get_device(handle, &device))
454 return AE_OK;
455
456 mutex_lock(&device->physical_node_lock);
457
458 list_for_each_entry(pn, &device->physical_node_list, node)
459 if (pn->put_online) {
460 device_online(pn->dev);
461 pn->put_online = false;
462 }
463
464 mutex_unlock(&device->physical_node_lock);
465
466 return AE_OK;
467}
468
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100469static int acpi_scan_try_to_offline(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Yinghai Lu5993c462013-01-11 22:40:41 +0000471 acpi_handle handle = device->handle;
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100472 struct device *errdev = NULL;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100473 acpi_status status;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100474
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200475 /*
476 * Carry out two passes here and ignore errors in the first pass,
477 * because if the devices in question are memory blocks and
478 * CONFIG_MEMCG is set, one of the blocks may hold data structures
479 * that the other blocks depend on, but it is not known in advance which
480 * block holds them.
481 *
482 * If the first pass is successful, the second one isn't needed, though.
483 */
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100484 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
485 NULL, acpi_bus_offline, (void *)false,
486 (void **)&errdev);
487 if (status == AE_SUPPORT) {
488 dev_warn(errdev, "Offline disabled.\n");
489 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
490 acpi_bus_online, NULL, NULL, NULL);
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100491 return -EPERM;
492 }
493 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200494 if (errdev) {
495 errdev = NULL;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200496 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100497 NULL, acpi_bus_offline, (void *)true,
498 (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200499 if (!errdev || acpi_force_hot_remove)
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100500 acpi_bus_offline(handle, 0, (void *)true,
501 (void **)&errdev);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200502
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200503 if (errdev && !acpi_force_hot_remove) {
504 dev_warn(errdev, "Offline failed.\n");
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100505 acpi_bus_online(handle, 0, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200506 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100507 ACPI_UINT32_MAX, acpi_bus_online,
508 NULL, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200509 return -EBUSY;
510 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200511 }
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100512 return 0;
513}
514
515static int acpi_scan_hot_remove(struct acpi_device *device)
516{
517 acpi_handle handle = device->handle;
518 unsigned long long sta;
519 acpi_status status;
520
Tang Chendee15922014-08-08 10:30:45 +0800521 if (device->handler && device->handler->hotplug.demand_offline
522 && !acpi_force_hot_remove) {
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100523 if (!acpi_scan_is_offline(device, true))
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100524 return -EBUSY;
525 } else {
526 int error = acpi_scan_try_to_offline(device);
527 if (error)
528 return error;
529 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200530
Zhang Rui26d46862008-04-29 02:35:48 -0400531 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100532 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400533
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100534 acpi_bus_trim(device);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200535
Jiang Liu7d2421f2013-06-29 00:24:40 +0800536 acpi_evaluate_lck(handle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /*
538 * TBD: _EJD support.
539 */
Jiang Liu7d2421f2013-06-29 00:24:40 +0800540 status = acpi_evaluate_ej0(handle);
541 if (status == AE_NOT_FOUND)
542 return -ENODEV;
543 else if (ACPI_FAILURE(status))
544 return -EIO;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100545
Toshi Kani882fd122013-03-13 19:29:26 +0000546 /*
547 * Verify if eject was indeed successful. If not, log an error
548 * message. No need to call _OST since _EJ0 call was made OK.
549 */
550 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
551 if (ACPI_FAILURE(status)) {
552 acpi_handle_warn(handle,
553 "Status check after eject failed (0x%x)\n", status);
554 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
555 acpi_handle_warn(handle,
556 "Eject incomplete - status 0x%llx\n", sta);
557 }
558
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100559 return 0;
560}
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100561
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100562static int acpi_scan_device_not_present(struct acpi_device *adev)
563{
564 if (!acpi_device_enumerated(adev)) {
565 dev_warn(&adev->dev, "Still not present\n");
566 return -EALREADY;
567 }
568 acpi_bus_trim(adev);
569 return 0;
570}
571
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100572static int acpi_scan_device_check(struct acpi_device *adev)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100573{
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200574 int error;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100575
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100576 acpi_bus_get_status(adev);
577 if (adev->status.present || adev->status.functional) {
578 /*
579 * This function is only called for device objects for which
580 * matching scan handlers exist. The only situation in which
581 * the scan handler is not attached to this device object yet
582 * is when the device has just appeared (either it wasn't
583 * present at all before or it was removed and then added
584 * again).
585 */
586 if (adev->handler) {
587 dev_warn(&adev->dev, "Already enumerated\n");
588 return -EALREADY;
589 }
590 error = acpi_bus_scan(adev->handle);
591 if (error) {
592 dev_warn(&adev->dev, "Namespace scan failure\n");
593 return error;
594 }
595 if (!adev->handler) {
596 dev_warn(&adev->dev, "Enumeration failure\n");
597 error = -ENODEV;
598 }
599 } else {
600 error = acpi_scan_device_not_present(adev);
601 }
602 return error;
603}
604
605static int acpi_scan_bus_check(struct acpi_device *adev)
606{
607 struct acpi_scan_handler *handler = adev->handler;
608 struct acpi_device *child;
609 int error;
610
611 acpi_bus_get_status(adev);
612 if (!(adev->status.present || adev->status.functional)) {
613 acpi_scan_device_not_present(adev);
614 return 0;
615 }
616 if (handler && handler->hotplug.scan_dependent)
617 return handler->hotplug.scan_dependent(adev);
618
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100619 error = acpi_bus_scan(adev->handle);
620 if (error) {
621 dev_warn(&adev->dev, "Namespace scan failure\n");
622 return error;
623 }
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100624 list_for_each_entry(child, &adev->children, node) {
625 error = acpi_scan_bus_check(child);
626 if (error)
627 return error;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100628 }
629 return 0;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100630}
631
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100632static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
633{
634 switch (type) {
635 case ACPI_NOTIFY_BUS_CHECK:
636 return acpi_scan_bus_check(adev);
637 case ACPI_NOTIFY_DEVICE_CHECK:
638 return acpi_scan_device_check(adev);
639 case ACPI_NOTIFY_EJECT_REQUEST:
640 case ACPI_OST_EC_OSPM_EJECT:
Rafael J. Wysockidd2151b2014-02-04 00:44:02 +0100641 if (adev->handler && !adev->handler->hotplug.enabled) {
642 dev_info(&adev->dev, "Eject disabled\n");
643 return -EPERM;
644 }
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100645 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
646 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100647 return acpi_scan_hot_remove(adev);
648 }
649 return -EINVAL;
650}
651
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +0100652void acpi_device_hotplug(struct acpi_device *adev, u32 src)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100653{
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100654 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100655 int error = -ENODEV;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100656
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200657 lock_device_hotplug();
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200658 mutex_lock(&acpi_scan_lock);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100659
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100660 /*
661 * The device object's ACPI handle cannot become invalid as long as we
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100662 * are holding acpi_scan_lock, but it might have become invalid before
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100663 * that lock was acquired.
664 */
665 if (adev->handle == INVALID_ACPI_HANDLE)
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100666 goto err_out;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100667
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100668 if (adev->flags.is_dock_station) {
669 error = dock_notify(adev, src);
670 } else if (adev->flags.hotplug_notify) {
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100671 error = acpi_generic_hotplug_event(adev, src);
Rafael J. Wysockidd2151b2014-02-04 00:44:02 +0100672 if (error == -EPERM) {
673 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
674 goto err_out;
675 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100676 } else {
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100677 int (*notify)(struct acpi_device *, u32);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100678
679 acpi_lock_hp_context();
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100680 notify = adev->hp ? adev->hp->notify : NULL;
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100681 acpi_unlock_hp_context();
682 /*
683 * There may be additional notify handlers for device objects
684 * without the .event() callback, so ignore them here.
685 */
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100686 if (notify)
687 error = notify(adev, src);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100688 else
689 goto out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100690 }
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100691 if (!error)
692 ost_code = ACPI_OST_SC_SUCCESS;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100693
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100694 err_out:
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100695 acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100696
697 out:
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +0100698 acpi_bus_put_acpi_device(adev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100699 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200700 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100701}
702
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100703static ssize_t real_power_state_show(struct device *dev,
704 struct device_attribute *attr, char *buf)
705{
706 struct acpi_device *adev = to_acpi_device(dev);
707 int state;
708 int ret;
709
710 ret = acpi_device_get_power(adev, &state);
711 if (ret)
712 return ret;
713
714 return sprintf(buf, "%s\n", acpi_power_state_string(state));
715}
716
717static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
718
719static ssize_t power_state_show(struct device *dev,
720 struct device_attribute *attr, char *buf)
721{
722 struct acpi_device *adev = to_acpi_device(dev);
723
724 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
725}
726
727static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800730acpi_eject_store(struct device *d, struct device_attribute *attr,
731 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800733 struct acpi_device *acpi_device = to_acpi_device(d);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100734 acpi_object_type not_used;
735 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100737 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100740 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
741 && !acpi_device->driver)
742 return -ENODEV;
743
744 status = acpi_get_type(acpi_device->handle, &not_used);
745 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
746 return -ENODEV;
747
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100748 get_device(&acpi_device->dev);
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +0100749 status = acpi_hotplug_schedule(acpi_device, ACPI_OST_EC_OSPM_EJECT);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200750 if (ACPI_SUCCESS(status))
751 return count;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100752
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200753 put_device(&acpi_device->dev);
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100754 acpi_evaluate_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
755 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100756 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800759static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800761static ssize_t
762acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
763 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600765 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800766}
767static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
768
Lv Zheng923d4a42012-10-30 14:43:26 +0100769static ssize_t acpi_device_uid_show(struct device *dev,
770 struct device_attribute *attr, char *buf)
771{
772 struct acpi_device *acpi_dev = to_acpi_device(dev);
773
774 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
775}
776static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
777
778static ssize_t acpi_device_adr_show(struct device *dev,
779 struct device_attribute *attr, char *buf)
780{
781 struct acpi_device *acpi_dev = to_acpi_device(dev);
782
783 return sprintf(buf, "0x%08x\n",
784 (unsigned int)(acpi_dev->pnp.bus_address));
785}
786static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
787
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800788static ssize_t
789acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
790 struct acpi_device *acpi_dev = to_acpi_device(dev);
791 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
792 int result;
793
794 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600795 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800796 goto end;
797
798 result = sprintf(buf, "%s\n", (char*)path.pointer);
799 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600800end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800801 return result;
802}
803static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
804
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600805/* sysfs file that shows description text from the ACPI _STR method */
806static ssize_t description_show(struct device *dev,
807 struct device_attribute *attr,
808 char *buf) {
809 struct acpi_device *acpi_dev = to_acpi_device(dev);
810 int result;
811
812 if (acpi_dev->pnp.str_obj == NULL)
813 return 0;
814
815 /*
816 * The _STR object contains a Unicode identifier for a device.
817 * We need to convert to utf-8 so it can be displayed.
818 */
819 result = utf16s_to_utf8s(
820 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
821 acpi_dev->pnp.str_obj->buffer.length,
822 UTF16_LITTLE_ENDIAN, buf,
823 PAGE_SIZE);
824
825 buf[result++] = '\n';
826
827 return result;
828}
829static DEVICE_ATTR(description, 0444, description_show, NULL);
830
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100831static ssize_t
832acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
833 char *buf) {
834 struct acpi_device *acpi_dev = to_acpi_device(dev);
Yasuaki Ishimatsua383b682014-09-03 13:39:13 +0900835 acpi_status status;
836 unsigned long long sun;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100837
Yasuaki Ishimatsua383b682014-09-03 13:39:13 +0900838 status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun);
839 if (ACPI_FAILURE(status))
840 return -ENODEV;
841
842 return sprintf(buf, "%llu\n", sun);
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100843}
844static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
845
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800846static ssize_t status_show(struct device *dev, struct device_attribute *attr,
847 char *buf) {
848 struct acpi_device *acpi_dev = to_acpi_device(dev);
849 acpi_status status;
850 unsigned long long sta;
851
852 status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
853 if (ACPI_FAILURE(status))
854 return -ENODEV;
855
856 return sprintf(buf, "%llu\n", sta);
857}
858static DEVICE_ATTR_RO(status);
859
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800860static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600862 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800863 acpi_status status;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800864 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800866 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800867 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800868 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600869 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800870 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600871 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800872 goto end;
873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200875 if (!list_empty(&dev->pnp.ids)) {
876 result = device_create_file(&dev->dev, &dev_attr_hid);
877 if (result)
878 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200880 result = device_create_file(&dev->dev, &dev_attr_modalias);
881 if (result)
882 goto end;
883 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200884
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600885 /*
886 * If device has _STR, 'description' file is created
887 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800888 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600889 status = acpi_evaluate_object(dev->handle, "_STR",
890 NULL, &buffer);
891 if (ACPI_FAILURE(status))
892 buffer.pointer = NULL;
893 dev->pnp.str_obj = buffer.pointer;
894 result = device_create_file(&dev->dev, &dev_attr_description);
895 if (result)
896 goto end;
897 }
898
Toshi Kanid4e1a692013-03-04 21:30:41 +0000899 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100900 result = device_create_file(&dev->dev, &dev_attr_adr);
901 if (dev->pnp.unique_id)
902 result = device_create_file(&dev->dev, &dev_attr_uid);
903
Yasuaki Ishimatsua383b682014-09-03 13:39:13 +0900904 if (acpi_has_method(dev->handle, "_SUN")) {
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100905 result = device_create_file(&dev->dev, &dev_attr_sun);
906 if (result)
907 goto end;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100908 }
909
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800910 if (acpi_has_method(dev->handle, "_STA")) {
911 result = device_create_file(&dev->dev, &dev_attr_status);
912 if (result)
913 goto end;
914 }
915
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800916 /*
917 * If device has _EJ0, 'eject' file is created that is used to trigger
918 * hot-removal function from userland.
919 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800920 if (acpi_has_method(dev->handle, "_EJ0")) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800921 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100922 if (result)
923 return result;
924 }
925
926 if (dev->flags.power_manageable) {
927 result = device_create_file(&dev->dev, &dev_attr_power_state);
928 if (result)
929 return result;
930
931 if (dev->power.flags.power_resources)
932 result = device_create_file(&dev->dev,
933 &dev_attr_real_power_state);
934 }
935
Alex Chiang0c526d92009-05-14 08:31:37 -0600936end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800937 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800938}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800940static void acpi_device_remove_files(struct acpi_device *dev)
941{
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100942 if (dev->flags.power_manageable) {
943 device_remove_file(&dev->dev, &dev_attr_power_state);
944 if (dev->power.flags.power_resources)
945 device_remove_file(&dev->dev,
946 &dev_attr_real_power_state);
947 }
948
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800949 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600950 * If device has _STR, remove 'description' file
951 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800952 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600953 kfree(dev->pnp.str_obj);
954 device_remove_file(&dev->dev, &dev_attr_description);
955 }
956 /*
957 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800958 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800959 if (acpi_has_method(dev->handle, "_EJ0"))
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800960 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800961
Jiang Liu952c63e2013-06-29 00:24:38 +0800962 if (acpi_has_method(dev->handle, "_SUN"))
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100963 device_remove_file(&dev->dev, &dev_attr_sun);
964
Lv Zheng923d4a42012-10-30 14:43:26 +0100965 if (dev->pnp.unique_id)
966 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000967 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100968 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600969 device_remove_file(&dev->dev, &dev_attr_modalias);
970 device_remove_file(&dev->dev, &dev_attr_hid);
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800971 if (acpi_has_method(dev->handle, "_STA"))
972 device_remove_file(&dev->dev, &dev_attr_status);
Alex Chiang0c526d92009-05-14 08:31:37 -0600973 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800974 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800975}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800977 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200979
Mika Westerbergcf761af2012-10-31 22:44:41 +0100980/**
Rafael J. Wysocki2b9c6982015-04-10 16:07:51 +0200981 * acpi_of_match_device - Match device object using the "compatible" property.
982 * @adev: ACPI device object to match.
Rafael J. Wysocki54fe9ce2015-04-10 16:07:30 +0200983 * @of_match_table: List of device IDs to match against.
984 *
Rafael J. Wysockiee892092015-05-22 04:24:34 +0200985 * If @dev has an ACPI companion which has ACPI_DT_NAMESPACE_HID in its list of
986 * identifiers and a _DSD object with the "compatible" property, use that
987 * property to match against the given list of identifiers.
Rafael J. Wysocki54fe9ce2015-04-10 16:07:30 +0200988 */
Rafael J. Wysocki2b9c6982015-04-10 16:07:51 +0200989static bool acpi_of_match_device(struct acpi_device *adev,
Rafael J. Wysocki54fe9ce2015-04-10 16:07:30 +0200990 const struct of_device_id *of_match_table)
Mika Westerberg733e6252014-10-21 13:33:56 +0200991{
992 const union acpi_object *of_compatible, *obj;
Mika Westerberg733e6252014-10-21 13:33:56 +0200993 int i, nval;
994
Mika Westerberg733e6252014-10-21 13:33:56 +0200995 if (!adev)
996 return false;
997
998 of_compatible = adev->data.of_compatible;
Rafael J. Wysocki54fe9ce2015-04-10 16:07:30 +0200999 if (!of_match_table || !of_compatible)
Mika Westerberg733e6252014-10-21 13:33:56 +02001000 return false;
1001
1002 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
1003 nval = of_compatible->package.count;
1004 obj = of_compatible->package.elements;
1005 } else { /* Must be ACPI_TYPE_STRING. */
1006 nval = 1;
1007 obj = of_compatible;
1008 }
1009 /* Now we can look for the driver DT compatible strings */
1010 for (i = 0; i < nval; i++, obj++) {
1011 const struct of_device_id *id;
1012
Rafael J. Wysocki54fe9ce2015-04-10 16:07:30 +02001013 for (id = of_match_table; id->compatible[0]; id++)
Mika Westerberg733e6252014-10-21 13:33:56 +02001014 if (!strcasecmp(obj->string.pointer, id->compatible))
1015 return true;
1016 }
1017
1018 return false;
1019}
1020
Rafael J. Wysocki2b9c6982015-04-10 16:07:51 +02001021static const struct acpi_device_id *__acpi_match_device(
1022 struct acpi_device *device,
1023 const struct acpi_device_id *ids,
1024 const struct of_device_id *of_ids)
1025{
1026 const struct acpi_device_id *id;
1027 struct acpi_hardware_id *hwid;
1028
1029 /*
1030 * If the device is not present, it is unnecessary to load device
1031 * driver for it.
1032 */
1033 if (!device || !device->status.present)
1034 return NULL;
1035
1036 list_for_each_entry(hwid, &device->pnp.ids, list) {
1037 /* First, check the ACPI/PNP IDs provided by the caller. */
1038 for (id = ids; id->id[0]; id++)
1039 if (!strcmp((char *) id->id, hwid->id))
1040 return id;
1041
1042 /*
Rafael J. Wysockiee892092015-05-22 04:24:34 +02001043 * Next, check ACPI_DT_NAMESPACE_HID and try to match the
Rafael J. Wysocki2b9c6982015-04-10 16:07:51 +02001044 * "compatible" property if found.
1045 *
1046 * The id returned by the below is not valid, but the only
1047 * caller passing non-NULL of_ids here is only interested in
1048 * whether or not the return value is NULL.
1049 */
Rafael J. Wysockiee892092015-05-22 04:24:34 +02001050 if (!strcmp(ACPI_DT_NAMESPACE_HID, hwid->id)
Rafael J. Wysocki2b9c6982015-04-10 16:07:51 +02001051 && acpi_of_match_device(device, of_ids))
1052 return id;
1053 }
1054 return NULL;
1055}
1056
1057/**
1058 * acpi_match_device - Match a struct device against a given list of ACPI IDs
1059 * @ids: Array of struct acpi_device_id object to match against.
1060 * @dev: The device structure to match.
1061 *
1062 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
1063 * object for that handle and use that object to match against a given list of
1064 * device IDs.
1065 *
1066 * Return a pointer to the first matching ID on success or %NULL on failure.
1067 */
1068const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
1069 const struct device *dev)
1070{
1071 return __acpi_match_device(acpi_companion_match(dev), ids, NULL);
1072}
1073EXPORT_SYMBOL_GPL(acpi_match_device);
1074
1075int acpi_match_device_ids(struct acpi_device *device,
1076 const struct acpi_device_id *ids)
1077{
1078 return __acpi_match_device(device, ids, NULL) ? 0 : -ENOENT;
1079}
1080EXPORT_SYMBOL(acpi_match_device_ids);
1081
Mika Westerberg733e6252014-10-21 13:33:56 +02001082bool acpi_driver_match_device(struct device *dev,
1083 const struct device_driver *drv)
1084{
1085 if (!drv->acpi_match_table)
Rafael J. Wysocki2b9c6982015-04-10 16:07:51 +02001086 return acpi_of_match_device(ACPI_COMPANION(dev),
1087 drv->of_match_table);
Mika Westerberg733e6252014-10-21 13:33:56 +02001088
Rafael J. Wysocki2b9c6982015-04-10 16:07:51 +02001089 return !!__acpi_match_device(acpi_companion_match(dev),
1090 drv->acpi_match_table, drv->of_match_table);
Mika Westerberg733e6252014-10-21 13:33:56 +02001091}
1092EXPORT_SYMBOL_GPL(acpi_driver_match_device);
1093
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001094static void acpi_free_power_resources_lists(struct acpi_device *device)
1095{
1096 int i;
1097
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001098 if (device->wakeup.flags.valid)
1099 acpi_power_resources_list_free(&device->wakeup.resources);
1100
Rafael J. Wysocki1b1f3e12015-01-01 23:38:28 +01001101 if (!device->power.flags.power_resources)
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001102 return;
1103
1104 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
1105 struct acpi_device_power_state *ps = &device->power.states[i];
1106 acpi_power_resources_list_free(&ps->resources);
1107 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001108}
1109
Patrick Mochel1890a972006-12-07 20:56:31 +08001110static void acpi_device_release(struct device *dev)
1111{
1112 struct acpi_device *acpi_dev = to_acpi_device(dev);
1113
Mika Westerbergffdcd952014-10-21 13:33:55 +02001114 acpi_free_properties(acpi_dev);
Toshi Kanic0af4172013-03-04 21:30:42 +00001115 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001116 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +08001117 kfree(acpi_dev);
1118}
1119
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001120static int acpi_bus_match(struct device *dev, struct device_driver *drv)
1121{
1122 struct acpi_device *acpi_dev = to_acpi_device(dev);
1123 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
1124
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +01001125 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001126 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001127}
1128
Kay Sievers7eff2e72007-08-14 15:15:12 +02001129static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001130{
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +02001131 return __acpi_device_uevent_modalias(to_acpi_device(dev), env);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001134static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
1135{
1136 struct acpi_device *device = data;
1137
1138 device->driver->ops.notify(device, event);
1139}
1140
Lan Tianyu236105d2014-08-26 01:29:24 +02001141static void acpi_device_notify_fixed(void *data)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001142{
1143 struct acpi_device *device = data;
1144
Bjorn Helgaas53de5352009-08-31 22:32:20 +00001145 /* Fixed hardware devices have no handles */
1146 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Lan Tianyu236105d2014-08-26 01:29:24 +02001147}
1148
Aaron Lufd9caef2015-03-13 13:52:50 +08001149static u32 acpi_device_fixed_event(void *data)
Lan Tianyu236105d2014-08-26 01:29:24 +02001150{
1151 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_device_notify_fixed, data);
Aaron Lufd9caef2015-03-13 13:52:50 +08001152 return ACPI_INTERRUPT_HANDLED;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001153}
1154
1155static int acpi_device_install_notify_handler(struct acpi_device *device)
1156{
1157 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001158
Bjorn Helgaasccba2a32009-09-21 19:29:15 +00001159 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001160 status =
1161 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +02001162 acpi_device_fixed_event,
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001163 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +00001164 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001165 status =
1166 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +02001167 acpi_device_fixed_event,
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001168 device);
1169 else
1170 status = acpi_install_notify_handler(device->handle,
1171 ACPI_DEVICE_NOTIFY,
1172 acpi_device_notify,
1173 device);
1174
1175 if (ACPI_FAILURE(status))
1176 return -EINVAL;
1177 return 0;
1178}
1179
1180static void acpi_device_remove_notify_handler(struct acpi_device *device)
1181{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +00001182 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001183 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +02001184 acpi_device_fixed_event);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +00001185 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001186 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +02001187 acpi_device_fixed_event);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001188 else
1189 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
1190 acpi_device_notify);
1191}
1192
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001193static int acpi_device_probe(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +08001194{
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001195 struct acpi_device *acpi_dev = to_acpi_device(dev);
1196 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
1197 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Rafael J. Wysockifc2e0a82014-08-26 01:29:21 +02001199 if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev))
Rafael J. Wysocki24071f42013-06-16 00:36:41 +02001200 return -EINVAL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001201
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001202 if (!acpi_drv->ops.add)
1203 return -ENOSYS;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001204
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001205 ret = acpi_drv->ops.add(acpi_dev);
1206 if (ret)
1207 return ret;
1208
1209 acpi_dev->driver = acpi_drv;
1210 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1211 "Driver [%s] successfully bound to device [%s]\n",
1212 acpi_drv->name, acpi_dev->pnp.bus_id));
1213
1214 if (acpi_drv->ops.notify) {
1215 ret = acpi_device_install_notify_handler(acpi_dev);
1216 if (ret) {
1217 if (acpi_drv->ops.remove)
1218 acpi_drv->ops.remove(acpi_dev);
1219
1220 acpi_dev->driver = NULL;
1221 acpi_dev->driver_data = NULL;
1222 return ret;
1223 }
Zhang Rui9e89dde2006-12-07 20:56:16 +08001224 }
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001225
1226 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
1227 acpi_drv->name, acpi_dev->pnp.bus_id));
1228 get_device(dev);
1229 return 0;
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001230}
1231
1232static int acpi_device_remove(struct device * dev)
1233{
1234 struct acpi_device *acpi_dev = to_acpi_device(dev);
1235 struct acpi_driver *acpi_drv = acpi_dev->driver;
1236
1237 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001238 if (acpi_drv->ops.notify)
1239 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001240 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001241 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001242 }
1243 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001244 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001245
1246 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +08001247 return 0;
1248}
1249
David Brownell55955aa2007-05-08 00:28:35 -07001250struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +08001251 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001252 .match = acpi_bus_match,
1253 .probe = acpi_device_probe,
1254 .remove = acpi_device_remove,
1255 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256};
1257
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001258static void acpi_device_del(struct acpi_device *device)
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001259{
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001260 mutex_lock(&acpi_device_lock);
1261 if (device->parent)
1262 list_del(&device->node);
1263
1264 list_del(&device->wakeup_list);
1265 mutex_unlock(&acpi_device_lock);
1266
1267 acpi_power_add_remove_device(device, false);
1268 acpi_device_remove_files(device);
1269 if (device->remove)
1270 device->remove(device);
1271
1272 device_del(&device->dev);
1273}
1274
1275static LIST_HEAD(acpi_device_del_list);
1276static DEFINE_MUTEX(acpi_device_del_lock);
1277
1278static void acpi_device_del_work_fn(struct work_struct *work_not_used)
1279{
1280 for (;;) {
1281 struct acpi_device *adev;
1282
1283 mutex_lock(&acpi_device_del_lock);
1284
1285 if (list_empty(&acpi_device_del_list)) {
1286 mutex_unlock(&acpi_device_del_lock);
1287 break;
1288 }
1289 adev = list_first_entry(&acpi_device_del_list,
1290 struct acpi_device, del_list);
1291 list_del(&adev->del_list);
1292
1293 mutex_unlock(&acpi_device_del_lock);
1294
1295 acpi_device_del(adev);
1296 /*
1297 * Drop references to all power resources that might have been
1298 * used by the device.
1299 */
1300 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
1301 put_device(&adev->dev);
1302 }
1303}
1304
1305/**
1306 * acpi_scan_drop_device - Drop an ACPI device object.
1307 * @handle: Handle of an ACPI namespace node, not used.
1308 * @context: Address of the ACPI device object to drop.
1309 *
1310 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
1311 * namespace node the device object pointed to by @context is attached to.
1312 *
1313 * The unregistration is carried out asynchronously to avoid running
1314 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
1315 * ensure the correct ordering (the device objects must be unregistered in the
1316 * same order in which the corresponding namespace nodes are deleted).
1317 */
1318static void acpi_scan_drop_device(acpi_handle handle, void *context)
1319{
1320 static DECLARE_WORK(work, acpi_device_del_work_fn);
1321 struct acpi_device *adev = context;
1322
1323 mutex_lock(&acpi_device_del_lock);
1324
1325 /*
1326 * Use the ACPI hotplug workqueue which is ordered, so this work item
1327 * won't run after any hotplug work items submitted subsequently. That
1328 * prevents attempts to register device objects identical to those being
1329 * deleted from happening concurrently (such attempts result from
1330 * hotplug events handled via the ACPI hotplug workqueue). It also will
1331 * run after all of the work items submitted previosuly, which helps
1332 * those work items to ensure that they are not accessing stale device
1333 * objects.
1334 */
1335 if (list_empty(&acpi_device_del_list))
1336 acpi_queue_hotplug_work(&work);
1337
1338 list_add_tail(&adev->del_list, &acpi_device_del_list);
1339 /* Make acpi_ns_validate_handle() return NULL for this handle. */
1340 adev->handle = INVALID_ACPI_HANDLE;
1341
1342 mutex_unlock(&acpi_device_del_lock);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001343}
1344
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001345static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
1346 void (*callback)(void *))
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001347{
1348 acpi_status status;
1349
1350 if (!device)
1351 return -EINVAL;
1352
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001353 status = acpi_get_data_full(handle, acpi_scan_drop_device,
1354 (void **)device, callback);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001355 if (ACPI_FAILURE(status) || !*device) {
1356 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
1357 handle));
1358 return -ENODEV;
1359 }
1360 return 0;
1361}
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001362
1363int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
1364{
1365 return acpi_get_device_data(handle, device, NULL);
1366}
Rafael J. Wysocki65859252013-10-01 23:02:43 +02001367EXPORT_SYMBOL(acpi_bus_get_device);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001368
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001369static void get_acpi_device(void *dev)
1370{
1371 if (dev)
1372 get_device(&((struct acpi_device *)dev)->dev);
1373}
1374
1375struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
1376{
1377 struct acpi_device *adev = NULL;
1378
1379 acpi_get_device_data(handle, &adev, get_acpi_device);
1380 return adev;
1381}
1382
1383void acpi_bus_put_acpi_device(struct acpi_device *adev)
1384{
1385 put_device(&adev->dev);
1386}
1387
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001388int acpi_device_add(struct acpi_device *device,
1389 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001391 int result;
1392 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1393 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001394
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001395 if (device->handle) {
1396 acpi_status status;
1397
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001398 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001399 device);
1400 if (ACPI_FAILURE(status)) {
1401 acpi_handle_err(device->handle,
1402 "Unable to attach device data\n");
1403 return -ENODEV;
1404 }
1405 }
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 /*
1408 * Linkage
1409 * -------
1410 * Link this device to its parent and siblings.
1411 */
1412 INIT_LIST_HEAD(&device->children);
1413 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001415 INIT_LIST_HEAD(&device->physical_node_list);
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001416 INIT_LIST_HEAD(&device->del_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001417 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001419 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1420 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001421 pr_err(PREFIX "Memory allocation error\n");
1422 result = -ENOMEM;
1423 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001424 }
1425
Shaohua Li90905892009-04-07 10:24:29 +08001426 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001427 /*
1428 * Find suitable bus_id and instance number in acpi_bus_id_list
1429 * If failed, create one and link it into acpi_bus_id_list
1430 */
1431 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001432 if (!strcmp(acpi_device_bus_id->bus_id,
1433 acpi_device_hid(device))) {
1434 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001435 found = 1;
1436 kfree(new_bus_id);
1437 break;
1438 }
1439 }
Alex Chiang0c526d92009-05-14 08:31:37 -06001440 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001441 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001442 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001443 acpi_device_bus_id->instance_no = 0;
1444 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1445 }
Kay Sievers07944692008-10-30 01:18:59 +01001446 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001447
Len Brown33b57152008-12-15 22:09:26 -05001448 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -05001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (device->wakeup.flags.valid)
1452 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +08001453 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Patrick Mochel1890a972006-12-07 20:56:31 +08001455 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001456 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +08001457 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001458 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001459 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -06001460 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -06001461 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001462 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001463 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001464
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001465 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -06001466 if (result)
Kay Sievers07944692008-10-30 01:18:59 +01001467 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1468 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001469
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001470 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001471
1472 err:
Shaohua Li90905892009-04-07 10:24:29 +08001473 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001474 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001475 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001476 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001477 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001478
1479 err_detach:
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001480 acpi_detach_data(device->handle, acpi_scan_drop_device);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001481 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001484struct acpi_device *acpi_get_next_child(struct device *dev,
1485 struct acpi_device *child)
1486{
1487 struct acpi_device *adev = ACPI_COMPANION(dev);
1488 struct list_head *head, *next;
1489
1490 if (!adev)
1491 return NULL;
1492
1493 head = &adev->children;
1494 if (list_empty(head))
1495 return NULL;
1496
1497 if (!child)
1498 return list_first_entry(head, struct acpi_device, node);
1499
1500 next = child->node.next;
1501 return next == head ? NULL : list_entry(next, struct acpi_device, node);
1502}
1503
Zhang Rui9e89dde2006-12-07 20:56:16 +08001504/* --------------------------------------------------------------------------
1505 Driver Management
1506 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001508 * acpi_bus_register_driver - register a driver with the ACPI bus
1509 * @driver: driver being registered
1510 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001512 * devices that match the driver's criteria and binds. Returns zero for
1513 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 */
Len Brown4be44fc2005-08-05 00:44:28 -04001515int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Patrick Mochel1890a972006-12-07 20:56:31 +08001517 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001520 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001521 driver->drv.name = driver->name;
1522 driver->drv.bus = &acpi_bus_type;
1523 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Patrick Mochel1890a972006-12-07 20:56:31 +08001525 ret = driver_register(&driver->drv);
1526 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Len Brown4be44fc2005-08-05 00:44:28 -04001529EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531/**
Hanjun Guob27b14c2013-09-22 15:42:41 +08001532 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001533 * @driver: driver to unregister
1534 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1536 * devices that match the driver's criteria and unbinds.
1537 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001538void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Patrick Mochel1890a972006-12-07 20:56:31 +08001540 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
Len Brown4be44fc2005-08-05 00:44:28 -04001542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543EXPORT_SYMBOL(acpi_bus_unregister_driver);
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545/* --------------------------------------------------------------------------
1546 Device Enumeration
1547 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001548static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1549{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001550 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001551 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001552
1553 /*
1554 * Fixed hardware devices do not appear in the namespace and do not
1555 * have handles, but we fabricate acpi_devices for them, so we have
1556 * to deal with them specially.
1557 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001558 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001559 return acpi_root;
1560
1561 do {
1562 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001563 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001564 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1565 } while (acpi_bus_get_device(handle, &device));
1566 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001567}
1568
Len Brownc8f7a622006-07-09 17:22:28 -04001569acpi_status
1570acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1571{
1572 acpi_status status;
1573 acpi_handle tmp;
1574 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1575 union acpi_object *obj;
1576
1577 status = acpi_get_handle(handle, "_EJD", &tmp);
1578 if (ACPI_FAILURE(status))
1579 return status;
1580
1581 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1582 if (ACPI_SUCCESS(status)) {
1583 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001584 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1585 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001586 kfree(buffer.pointer);
1587 }
1588 return status;
1589}
1590EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1591
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001592static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1593 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001595 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1596 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001598 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001599 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001601 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001602 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001604 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001606 /* _PRW */
1607 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1608 if (ACPI_FAILURE(status)) {
1609 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001610 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001611 }
1612
1613 package = (union acpi_object *)buffer.pointer;
1614
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001615 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001616 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001619 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001620 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (element->type == ACPI_TYPE_PACKAGE) {
1623 if ((element->package.count < 2) ||
1624 (element->package.elements[0].type !=
1625 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001626 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001627 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001628
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001629 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001631 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 (u32) element->package.elements[1].integer.value;
1633 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001634 wakeup->gpe_device = NULL;
1635 wakeup->gpe_number = element->integer.value;
1636 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001637 goto out;
1638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001641 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001642 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001643
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001644 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001646 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1647 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001648 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001650 if (!list_empty(&wakeup->resources)) {
1651 int sleep_state;
1652
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001653 err = acpi_power_wakeup_list_init(&wakeup->resources,
1654 &sleep_state);
1655 if (err) {
1656 acpi_handle_warn(handle, "Retrieving current states "
1657 "of wakeup power resources failed\n");
1658 acpi_power_resources_list_free(&wakeup->resources);
1659 goto out;
1660 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001661 if (sleep_state < wakeup->sleep_state) {
1662 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1663 "(S%d) by S%d from power resources\n",
1664 (int)wakeup->sleep_state, sleep_state);
1665 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001668
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001669 out:
1670 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001671 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001674static void acpi_wakeup_gpe_init(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001676 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001677 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001678 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001679 {"PNP0C0E", 0},
1680 {"", 0},
1681 };
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001682 struct acpi_device_wakeup *wakeup = &device->wakeup;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001683 acpi_status status;
1684 acpi_event_status event_status;
1685
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001686 wakeup->flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001687
1688 /* Power button, Lid switch always enable wakeup */
1689 if (!acpi_match_device_ids(device, button_device_ids)) {
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001690 wakeup->flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001691 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1692 /* Do not use Lid/sleep button for S5 wakeup */
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001693 if (wakeup->sleep_state == ACPI_STATE_S5)
1694 wakeup->sleep_state = ACPI_STATE_S4;
Zhang Ruib7e38302012-12-04 23:23:16 +01001695 }
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001696 acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001697 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001698 return;
1699 }
1700
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001701 acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
1702 wakeup->gpe_number);
1703 status = acpi_get_gpe_status(wakeup->gpe_device, wakeup->gpe_number,
1704 &event_status);
1705 if (ACPI_FAILURE(status))
1706 return;
1707
Lv Zheng2f857232014-10-10 10:40:05 +08001708 wakeup->flags.run_wake = !!(event_status & ACPI_EVENT_FLAG_HAS_HANDLER);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001709}
1710
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001711static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001712{
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001713 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001714
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001715 /* Presence of _PRW indicates wake capable */
Jiang Liu952c63e2013-06-29 00:24:38 +08001716 if (!acpi_has_method(device->handle, "_PRW"))
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001717 return;
1718
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001719 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1720 &device->wakeup);
1721 if (err) {
1722 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001723 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001727 device->wakeup.prepare_count = 0;
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001728 acpi_wakeup_gpe_init(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001729 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1730 * system for the ACPI device with the _PRW object.
1731 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1732 * So it is necessary to call _DSW object first. Only when it is not
1733 * present will the _PSW object used.
1734 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001735 err = acpi_device_sleep_wake(device, 0, 0, 0);
1736 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001737 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1738 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001741static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001743 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001744 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1745 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001746 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001747
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001748 INIT_LIST_HEAD(&ps->resources);
1749
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001750 /* Evaluate "_PRx" to get referenced power resources */
1751 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1752 if (ACPI_SUCCESS(status)) {
1753 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001754
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001755 if (buffer.length && package
1756 && package->type == ACPI_TYPE_PACKAGE
1757 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001758 int err = acpi_extract_power_resources(package, 0,
1759 &ps->resources);
1760 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001761 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001762 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001763 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001764 }
1765
1766 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001767 pathname[2] = 'S';
Jiang Liu952c63e2013-06-29 00:24:38 +08001768 if (acpi_has_method(device->handle, pathname))
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001769 ps->flags.explicit_set = 1;
1770
1771 /*
1772 * State is valid if there are means to put the device into it.
1773 * D3hot is only valid if _PR3 present.
1774 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001775 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001776 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1777 ps->flags.valid = 1;
1778 ps->flags.os_accessible = 1;
1779 }
1780
1781 ps->power = -1; /* Unknown - driver assigned */
1782 ps->latency = -1; /* Unknown - driver assigned */
1783}
1784
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001785static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001787 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001789 /* Presence of _PS0|_PR0 indicates 'power manageable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001790 if (!acpi_has_method(device->handle, "_PS0") &&
1791 !acpi_has_method(device->handle, "_PR0"))
1792 return;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001793
1794 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001797 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 */
Jiang Liu952c63e2013-06-29 00:24:38 +08001799 if (acpi_has_method(device->handle, "_PSC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001800 device->power.flags.explicit_get = 1;
Rafael J. Wysockif25c0ae2014-05-17 00:18:13 +02001801
Jiang Liu952c63e2013-06-29 00:24:38 +08001802 if (acpi_has_method(device->handle, "_IRC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001803 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Rafael J. Wysockif25c0ae2014-05-17 00:18:13 +02001805 if (acpi_has_method(device->handle, "_DSW"))
1806 device->power.flags.dsw_present = 1;
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001809 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001811 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1812 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001814 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Zhang Rui9e89dde2006-12-07 20:56:16 +08001816 /* Set defaults for D0 and D3 states (always valid) */
1817 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1818 device->power.states[ACPI_STATE_D0].power = 100;
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +02001819 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1820 device->power.states[ACPI_STATE_D3_COLD].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001822 /* Set D3cold's explicit_set flag if _PS3 exists. */
1823 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1824 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1825
Aaron Lu1399dfc2012-11-21 23:33:40 +01001826 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1827 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1828 device->power.flags.power_resources)
1829 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1830
Rafael J. Wysocki1b1f3e12015-01-01 23:38:28 +01001831 if (acpi_bus_init_power(device))
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001832 device->flags.power_manageable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001835static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 /* Presence of _STA indicates 'dynamic_status' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001838 if (acpi_has_method(device->handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 device->flags.dynamic_status = 1;
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 /* Presence of _RMV indicates 'removable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001842 if (acpi_has_method(device->handle, "_RMV"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 device->flags.removable = 1;
1844
1845 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001846 if (acpi_has_method(device->handle, "_EJD") ||
1847 acpi_has_method(device->handle, "_EJ0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 device->flags.ejectable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849}
1850
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001851static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
Len Brown4be44fc2005-08-05 00:44:28 -04001853 char bus_id[5] = { '?', 0 };
1854 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1855 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 /*
1858 * Bus ID
1859 * ------
1860 * The device's Bus ID is simply the object name.
1861 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1862 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001863 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001865 return;
1866 }
1867
1868 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 case ACPI_BUS_TYPE_POWER_BUTTON:
1870 strcpy(device->pnp.bus_id, "PWRF");
1871 break;
1872 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1873 strcpy(device->pnp.bus_id, "SLPF");
1874 break;
1875 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001876 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 /* Clean up trailing underscores (if any) */
1878 for (i = 3; i > 1; i--) {
1879 if (bus_id[i] == '_')
1880 bus_id[i] = '\0';
1881 else
1882 break;
1883 }
1884 strcpy(device->pnp.bus_id, bus_id);
1885 break;
1886 }
1887}
1888
Zhang Rui54735262007-01-11 02:09:09 -05001889/*
Jiang Liuebf4df82013-06-29 00:24:41 +08001890 * acpi_ata_match - see if an acpi object is an ATA device
1891 *
1892 * If an acpi object has one of the ACPI ATA methods defined,
1893 * then we can safely call it an ATA device.
1894 */
1895bool acpi_ata_match(acpi_handle handle)
1896{
1897 return acpi_has_method(handle, "_GTF") ||
1898 acpi_has_method(handle, "_GTM") ||
1899 acpi_has_method(handle, "_STM") ||
1900 acpi_has_method(handle, "_SDD");
1901}
1902
1903/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001904 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001905 *
1906 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1907 * then we can safely call it an ejectable drive bay
1908 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001909bool acpi_bay_match(acpi_handle handle)
Toshi Kanid4e1a692013-03-04 21:30:41 +00001910{
Zhang Rui54735262007-01-11 02:09:09 -05001911 acpi_handle phandle;
1912
Jiang Liu952c63e2013-06-29 00:24:38 +08001913 if (!acpi_has_method(handle, "_EJ0"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001914 return false;
1915 if (acpi_ata_match(handle))
1916 return true;
1917 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1918 return false;
Zhang Rui54735262007-01-11 02:09:09 -05001919
Jiang Liuebf4df82013-06-29 00:24:41 +08001920 return acpi_ata_match(phandle);
Zhang Rui54735262007-01-11 02:09:09 -05001921}
1922
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001923bool acpi_device_is_battery(struct acpi_device *adev)
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001924{
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001925 struct acpi_hardware_id *hwid;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001926
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001927 list_for_each_entry(hwid, &adev->pnp.ids, list)
1928 if (!strcmp("PNP0C0A", hwid->id))
1929 return true;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001930
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001931 return false;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001932}
1933
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001934static bool is_ejectable_bay(struct acpi_device *adev)
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001935{
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001936 acpi_handle handle = adev->handle;
1937
1938 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001939 return true;
1940
1941 return acpi_bay_match(handle);
1942}
1943
Frank Seidel3620f2f2007-12-07 13:20:34 +01001944/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001945 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001946 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001947bool acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001948{
Jiang Liuebf4df82013-06-29 00:24:41 +08001949 return acpi_has_method(handle, "_DCK");
Frank Seidel3620f2f2007-12-07 13:20:34 +01001950}
1951
Thomas Renninger620e1122010-10-01 10:54:00 +02001952const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001953{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001954 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001955
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001956 if (list_empty(&device->pnp.ids))
1957 return dummy_hid;
1958
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001959 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1960 return hid->id;
1961}
1962EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001963
Toshi Kanid4e1a692013-03-04 21:30:41 +00001964static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001965{
1966 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001967
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001968 id = kmalloc(sizeof(*id), GFP_KERNEL);
1969 if (!id)
1970 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001971
Thomas Meyer581de592011-08-06 11:32:56 +02001972 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001973 if (!id->id) {
1974 kfree(id);
1975 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001976 }
1977
Toshi Kanid4e1a692013-03-04 21:30:41 +00001978 list_add_tail(&id->list, &pnp->ids);
1979 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001980}
1981
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001982/*
1983 * Old IBM workstations have a DSDT bug wherein the SMBus object
1984 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1985 * prefix. Work around this.
1986 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001987static bool acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001988{
Jiang Liuebf4df82013-06-29 00:24:41 +08001989 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1990 struct acpi_buffer path = { sizeof(node_name), node_name };
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001991
1992 if (!dmi_name_in_vendors("IBM"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001993 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001994
1995 /* Look for SMBS object */
Jiang Liuebf4df82013-06-29 00:24:41 +08001996 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1997 strcmp("SMBS", path.pointer))
1998 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001999
2000 /* Does it have the necessary (but misnamed) methods? */
Jiang Liu952c63e2013-06-29 00:24:38 +08002001 if (acpi_has_method(handle, "SBI") &&
2002 acpi_has_method(handle, "SBR") &&
2003 acpi_has_method(handle, "SBW"))
Jiang Liuebf4df82013-06-29 00:24:41 +08002004 return true;
2005
2006 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01002007}
2008
Jiang Liuae3caa82014-02-19 14:02:19 +08002009static bool acpi_object_is_system_bus(acpi_handle handle)
2010{
2011 acpi_handle tmp;
2012
2013 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
2014 tmp == handle)
2015 return true;
2016 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
2017 tmp == handle)
2018 return true;
2019
2020 return false;
2021}
2022
Toshi Kanic0af4172013-03-04 21:30:42 +00002023static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
2024 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Len Brown4be44fc2005-08-05 00:44:28 -04002026 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06002027 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00002028 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06002029 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Toshi Kanic0af4172013-03-04 21:30:42 +00002031 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00002033 if (handle == ACPI_ROOT_OBJECT) {
2034 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00002035 break;
2036 }
2037
Toshi Kanic0af4172013-03-04 21:30:42 +00002038 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00002040 pr_err(PREFIX "%s: Error reading device info\n",
2041 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return;
2043 }
2044
Rafael J. Wysocki549e6842014-05-30 04:26:18 +02002045 if (info->valid & ACPI_VALID_HID) {
Toshi Kanic0af4172013-03-04 21:30:42 +00002046 acpi_add_id(pnp, info->hardware_id.string);
Rafael J. Wysocki549e6842014-05-30 04:26:18 +02002047 pnp->type.platform_id = 1;
2048 }
Bjorn Helgaas57f36742009-09-21 13:35:40 -06002049 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08002050 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06002051 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00002052 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06002053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00002055 pnp->bus_address = info->address;
2056 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 }
Lv Zhengccf78042012-10-30 14:41:07 +01002058 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00002059 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01002060 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08002061
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04002062 kfree(info);
2063
Bjorn Helgaas57f36742009-09-21 13:35:40 -06002064 /*
2065 * Some devices don't reliably have _HIDs & _CIDs, so add
2066 * synthetic HIDs to make sure drivers can find them.
2067 */
Toshi Kanic0af4172013-03-04 21:30:42 +00002068 if (acpi_is_video_device(handle))
2069 acpi_add_id(pnp, ACPI_VIDEO_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08002070 else if (acpi_bay_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00002071 acpi_add_id(pnp, ACPI_BAY_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08002072 else if (acpi_dock_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00002073 acpi_add_id(pnp, ACPI_DOCK_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08002074 else if (acpi_ibm_smbus_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00002075 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
Jiang Liuae3caa82014-02-19 14:02:19 +08002076 else if (list_empty(&pnp->ids) &&
2077 acpi_object_is_system_bus(handle)) {
2078 /* \_SB, \_TZ, LNXSYBUS */
2079 acpi_add_id(pnp, ACPI_BUS_HID);
Toshi Kanic0af4172013-03-04 21:30:42 +00002080 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
2081 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06002082 }
Zhang Rui54735262007-01-11 02:09:09 -05002083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 break;
2085 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00002086 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 break;
2088 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00002089 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00002092 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 break;
2094 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00002095 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 break;
2097 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00002098 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 break;
2100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
Toshi Kanic0af4172013-03-04 21:30:42 +00002103void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
2104{
2105 struct acpi_hardware_id *id, *tmp;
2106
2107 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
2108 kfree(id->id);
2109 kfree(id);
2110 }
2111 kfree(pnp->unique_id);
2112}
2113
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01002114void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
2115 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002117 INIT_LIST_HEAD(&device->pnp.ids);
2118 device->device_type = type;
2119 device->handle = handle;
2120 device->parent = acpi_bus_get_parent(handle);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01002121 device->fwnode.type = FWNODE_ACPI;
Rafael J. Wysocki25db1152013-11-22 21:56:06 +01002122 acpi_set_device_status(device, sta);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002123 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00002124 acpi_set_pnp_ids(handle, &device->pnp, type);
Mika Westerbergffdcd952014-10-21 13:33:55 +02002125 acpi_init_properties(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002126 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002127 device->flags.match_driver = false;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002128 device->flags.initialized = true;
2129 device->flags.visited = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01002130 device_initialize(&device->dev);
2131 dev_set_uevent_suppress(&device->dev, true);
2132}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00002133
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01002134void acpi_device_add_finalize(struct acpi_device *device)
2135{
2136 dev_set_uevent_suppress(&device->dev, false);
2137 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002140static int acpi_add_single_object(struct acpi_device **child,
2141 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002142 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00002144 int result;
2145 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00002146 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Burman Yan36bcbec2006-12-19 12:56:11 -08002148 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04002150 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04002151 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002154 acpi_init_device_object(device, handle, type, sta);
2155 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01002156 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01002158 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002159 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00002160 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002161 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002164 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01002165 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002166 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
2167 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
2168 dev_name(&device->dev), (char *) buffer.pointer,
2169 device->parent ? dev_name(&device->parent->dev) : "(null)"));
2170 kfree(buffer.pointer);
2171 *child = device;
2172 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01002173}
2174
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00002175static int acpi_bus_type_and_status(acpi_handle handle, int *type,
2176 unsigned long long *sta)
2177{
2178 acpi_status status;
2179 acpi_object_type acpi_type;
2180
2181 status = acpi_get_type(handle, &acpi_type);
2182 if (ACPI_FAILURE(status))
2183 return -ENODEV;
2184
2185 switch (acpi_type) {
2186 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
2187 case ACPI_TYPE_DEVICE:
2188 *type = ACPI_BUS_TYPE_DEVICE;
2189 status = acpi_bus_get_status_handle(handle, sta);
2190 if (ACPI_FAILURE(status))
2191 return -ENODEV;
2192 break;
2193 case ACPI_TYPE_PROCESSOR:
2194 *type = ACPI_BUS_TYPE_PROCESSOR;
2195 status = acpi_bus_get_status_handle(handle, sta);
2196 if (ACPI_FAILURE(status))
2197 return -ENODEV;
2198 break;
2199 case ACPI_TYPE_THERMAL:
2200 *type = ACPI_BUS_TYPE_THERMAL;
2201 *sta = ACPI_STA_DEFAULT;
2202 break;
2203 case ACPI_TYPE_POWER:
2204 *type = ACPI_BUS_TYPE_POWER;
2205 *sta = ACPI_STA_DEFAULT;
2206 break;
2207 default:
2208 return -ENODEV;
2209 }
2210
2211 return 0;
2212}
2213
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002214bool acpi_device_is_present(struct acpi_device *adev)
2215{
2216 if (adev->status.present || adev->status.functional)
2217 return true;
2218
2219 adev->flags.initialized = false;
2220 return false;
2221}
2222
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01002223static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
2224 char *idstr,
2225 const struct acpi_device_id **matchid)
2226{
2227 const struct acpi_device_id *devid;
2228
Rafael J. Wysockiaca0a4e2014-05-30 04:21:52 +02002229 if (handler->match)
2230 return handler->match(idstr, matchid);
2231
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01002232 for (devid = handler->ids; devid->id[0]; devid++)
2233 if (!strcmp((char *)devid->id, idstr)) {
2234 if (matchid)
2235 *matchid = devid;
2236
2237 return true;
2238 }
2239
2240 return false;
2241}
2242
Toshi Kani6b772e8f2013-03-04 21:30:43 +00002243static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
2244 const struct acpi_device_id **matchid)
2245{
2246 struct acpi_scan_handler *handler;
2247
2248 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
2249 if (acpi_scan_handler_matching(handler, idstr, matchid))
2250 return handler;
2251
2252 return NULL;
2253}
2254
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01002255void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
2256{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01002257 if (!!hotplug->enabled == !!val)
2258 return;
2259
2260 mutex_lock(&acpi_scan_lock);
2261
2262 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01002263
2264 mutex_unlock(&acpi_scan_lock);
2265}
2266
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002267static void acpi_scan_init_hotplug(struct acpi_device *adev)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002268{
Toshi Kani6b772e8f2013-03-04 21:30:43 +00002269 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002270
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01002271 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01002272 acpi_dock_add(adev);
2273 return;
2274 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002275 list_for_each_entry(hwid, &adev->pnp.ids, list) {
2276 struct acpi_scan_handler *handler;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002277
Toshi Kani6b772e8f2013-03-04 21:30:43 +00002278 handler = acpi_scan_match_handler(hwid->id, NULL);
Rafael J. Wysocki1a699472014-02-06 13:58:13 +01002279 if (handler) {
2280 adev->flags.hotplug_notify = true;
2281 break;
2282 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002283 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01002284}
2285
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08002286static void acpi_device_dep_initialize(struct acpi_device *adev)
2287{
2288 struct acpi_dep_data *dep;
2289 struct acpi_handle_list dep_devices;
2290 acpi_status status;
2291 int i;
2292
2293 if (!acpi_has_method(adev->handle, "_DEP"))
2294 return;
2295
2296 status = acpi_evaluate_reference(adev->handle, "_DEP", NULL,
2297 &dep_devices);
2298 if (ACPI_FAILURE(status)) {
Rafael J. Wysocki80167a22014-12-12 22:48:44 +01002299 dev_dbg(&adev->dev, "Failed to evaluate _DEP.\n");
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08002300 return;
2301 }
2302
2303 for (i = 0; i < dep_devices.count; i++) {
2304 struct acpi_device_info *info;
2305 int skip;
2306
2307 status = acpi_get_object_info(dep_devices.handles[i], &info);
2308 if (ACPI_FAILURE(status)) {
Rafael J. Wysocki80167a22014-12-12 22:48:44 +01002309 dev_dbg(&adev->dev, "Error reading _DEP device info\n");
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08002310 continue;
2311 }
2312
2313 /*
2314 * Skip the dependency of Windows System Power
2315 * Management Controller
2316 */
2317 skip = info->valid & ACPI_VALID_HID &&
2318 !strcmp(info->hardware_id.string, "INT3396");
2319
2320 kfree(info);
2321
2322 if (skip)
2323 continue;
2324
2325 dep = kzalloc(sizeof(struct acpi_dep_data), GFP_KERNEL);
2326 if (!dep)
2327 return;
2328
2329 dep->master = dep_devices.handles[i];
2330 dep->slave = adev->handle;
2331 adev->dep_unmet++;
2332
2333 mutex_lock(&acpi_dep_list_lock);
2334 list_add_tail(&dep->node , &acpi_dep_list);
2335 mutex_unlock(&acpi_dep_list_lock);
2336 }
2337}
2338
Rafael J. Wysockie3863092012-12-21 00:36:47 +01002339static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
2340 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01002342 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00002343 int type;
2344 unsigned long long sta;
2345 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002346
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01002347 acpi_bus_get_device(handle, &device);
2348 if (device)
2349 goto out;
2350
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00002351 result = acpi_bus_type_and_status(handle, &type, &sta);
2352 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002353 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01002355 if (type == ACPI_BUS_TYPE_POWER) {
2356 acpi_add_power_resource(handle);
2357 return AE_OK;
2358 }
2359
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002360 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00002361 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002362 return AE_CTRL_DEPTH;
2363
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002364 acpi_scan_init_hotplug(device);
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08002365 acpi_device_dep_initialize(device);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002366
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01002367 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002368 if (!*return_value)
2369 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01002370
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002371 return AE_OK;
2372}
2373
Zhang Rui48459342014-05-30 14:35:34 +02002374static int acpi_check_spi_i2c_slave(struct acpi_resource *ares, void *data)
2375{
2376 bool *is_spi_i2c_slave_p = data;
2377
2378 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
2379 return 1;
2380
2381 /*
2382 * devices that are connected to UART still need to be enumerated to
2383 * platform bus
2384 */
2385 if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
2386 *is_spi_i2c_slave_p = true;
2387
2388 /* no need to do more checking */
2389 return -1;
2390}
2391
2392static void acpi_default_enumeration(struct acpi_device *device)
2393{
2394 struct list_head resource_list;
2395 bool is_spi_i2c_slave = false;
2396
Zhang Rui48459342014-05-30 14:35:34 +02002397 /*
2398 * Do not enemerate SPI/I2C slaves as they will be enuerated by their
2399 * respective parents.
2400 */
2401 INIT_LIST_HEAD(&resource_list);
2402 acpi_dev_get_resources(device, &resource_list, acpi_check_spi_i2c_slave,
2403 &is_spi_i2c_slave);
2404 acpi_dev_free_resource_list(&resource_list);
2405 if (!is_spi_i2c_slave)
2406 acpi_create_platform_device(device);
2407}
2408
Rafael J. Wysocki7d284352015-04-24 02:18:01 +02002409static const struct acpi_device_id generic_device_ids[] = {
Rafael J. Wysockiee892092015-05-22 04:24:34 +02002410 {ACPI_DT_NAMESPACE_HID, },
Rafael J. Wysocki7d284352015-04-24 02:18:01 +02002411 {"", },
2412};
2413
2414static int acpi_generic_device_attach(struct acpi_device *adev,
2415 const struct acpi_device_id *not_used)
2416{
2417 /*
Rafael J. Wysockiee892092015-05-22 04:24:34 +02002418 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
2419 * below can be unconditional.
Rafael J. Wysocki7d284352015-04-24 02:18:01 +02002420 */
2421 if (adev->data.of_compatible)
2422 acpi_default_enumeration(adev);
2423
2424 return 1;
2425}
2426
2427static struct acpi_scan_handler generic_device_handler = {
2428 .ids = generic_device_ids,
2429 .attach = acpi_generic_device_attach,
2430};
2431
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002432static int acpi_scan_attach_handler(struct acpi_device *device)
2433{
2434 struct acpi_hardware_id *hwid;
2435 int ret = 0;
2436
2437 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002438 const struct acpi_device_id *devid;
2439 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002440
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002441 handler = acpi_scan_match_handler(hwid->id, &devid);
2442 if (handler) {
Rafael J. Wysockid34afa92014-05-30 04:27:31 +02002443 if (!handler->attach) {
2444 device->pnp.type.platform_id = 0;
2445 continue;
2446 }
Rafael J. Wysocki9cb32ac2014-02-11 00:35:46 +01002447 device->handler = handler;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002448 ret = handler->attach(device, devid);
Rafael J. Wysocki9cb32ac2014-02-11 00:35:46 +01002449 if (ret > 0)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002450 break;
Rafael J. Wysocki9cb32ac2014-02-11 00:35:46 +01002451
2452 device->handler = NULL;
2453 if (ret < 0)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002454 break;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002455 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002456 }
Zhang Rui48459342014-05-30 14:35:34 +02002457
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002458 return ret;
2459}
2460
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002461static void acpi_bus_attach(struct acpi_device *device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002462{
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002463 struct acpi_device *child;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01002464 acpi_handle ejd;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002465 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002466
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01002467 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
2468 register_dock_dependent_device(device, ejd);
2469
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002470 acpi_bus_get_status(device);
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002471 /* Skip devices that are not present. */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002472 if (!acpi_device_is_present(device)) {
2473 device->flags.visited = false;
Rafael J. Wysocki1b1f3e12015-01-01 23:38:28 +01002474 device->flags.power_manageable = 0;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002475 return;
2476 }
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02002477 if (device->handler)
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002478 goto ok;
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02002479
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002480 if (!device->flags.initialized) {
Rafael J. Wysocki1b1f3e12015-01-01 23:38:28 +01002481 device->flags.power_manageable =
2482 device->power.states[ACPI_STATE_D0].flags.valid;
2483 if (acpi_bus_init_power(device))
2484 device->flags.power_manageable = 0;
2485
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002486 device->flags.initialized = true;
2487 }
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002488 device->flags.visited = false;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002489 ret = acpi_scan_attach_handler(device);
Rafael J. Wysocki69310072013-11-07 01:41:01 +01002490 if (ret < 0)
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002491 return;
Rafael J. Wysocki69310072013-11-07 01:41:01 +01002492
2493 device->flags.match_driver = true;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002494 if (!ret) {
2495 ret = device_attach(&device->dev);
2496 if (ret < 0)
2497 return;
Rafael J. Wysocki7d284352015-04-24 02:18:01 +02002498
2499 if (!ret && device->pnp.type.platform_id)
2500 acpi_default_enumeration(device);
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002501 }
2502 device->flags.visited = true;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002503
2504 ok:
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002505 list_for_each_entry(child, &device->children, node)
2506 acpi_bus_attach(child);
Rafael J. Wysocki8ab17fc2014-09-21 02:58:18 +02002507
2508 if (device->handler && device->handler->hotplug.notify_online)
2509 device->handler->hotplug.notify_online(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08002512void acpi_walk_dep_device_list(acpi_handle handle)
2513{
2514 struct acpi_dep_data *dep, *tmp;
2515 struct acpi_device *adev;
2516
2517 mutex_lock(&acpi_dep_list_lock);
2518 list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2519 if (dep->master == handle) {
2520 acpi_bus_get_device(dep->slave, &adev);
2521 if (!adev)
2522 continue;
2523
2524 adev->dep_unmet--;
2525 if (!adev->dep_unmet)
2526 acpi_bus_attach(adev);
2527 list_del(&dep->node);
2528 kfree(dep);
2529 }
2530 }
2531 mutex_unlock(&acpi_dep_list_lock);
2532}
2533EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
2534
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002535/**
2536 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2537 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01002538 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002539 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2540 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01002541 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002542 * If no devices were found, -ENODEV is returned, but it does not mean that
2543 * there has been a real error. There just have been no suitable ACPI objects
2544 * in the table trunk from which the kernel could create a device and add an
2545 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002546 *
2547 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01002548 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002549int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07002550{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 void *device = NULL;
Rajesh Shah3fb02732005-04-28 00:25:52 -07002552
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002553 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01002555 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002556
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002557 if (device) {
2558 acpi_bus_attach(device);
2559 return 0;
2560 }
2561 return -ENODEV;
Rajesh Shah3fb02732005-04-28 00:25:52 -07002562}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002563EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002565/**
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002566 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2567 * @adev: Root of the ACPI namespace scope to walk.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002568 *
2569 * Must be called under acpi_scan_lock.
2570 */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002571void acpi_bus_trim(struct acpi_device *adev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572{
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002573 struct acpi_scan_handler *handler = adev->handler;
2574 struct acpi_device *child;
2575
2576 list_for_each_entry_reverse(child, &adev->children, node)
2577 acpi_bus_trim(child);
2578
Rafael J. Wysockia951c772014-01-27 23:08:09 +01002579 adev->flags.match_driver = false;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002580 if (handler) {
2581 if (handler->detach)
2582 handler->detach(adev);
2583
2584 adev->handler = NULL;
2585 } else {
2586 device_release_driver(&adev->dev);
2587 }
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002588 /*
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002589 * Most likely, the device is going away, so put it into D3cold before
2590 * that.
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002591 */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002592 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2593 adev->flags.initialized = false;
2594 adev->flags.visited = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
Kristen Accardiceaba662006-02-23 17:56:01 -08002596EXPORT_SYMBOL_GPL(acpi_bus_trim);
2597
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00002598static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
Len Brown4be44fc2005-08-05 00:44:28 -04002600 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08002601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 /*
2603 * Enumerate all fixed-feature devices.
2604 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002605 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2606 struct acpi_device *device = NULL;
2607
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002608 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002609 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002610 ACPI_STA_DEFAULT);
2611 if (result)
2612 return result;
2613
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002614 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002615 result = device_attach(&device->dev);
2616 if (result < 0)
2617 return result;
2618
Daniel Drakec10d7a12012-05-10 00:08:43 +01002619 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002622 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2623 struct acpi_device *device = NULL;
2624
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002625 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002626 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002627 ACPI_STA_DEFAULT);
2628 if (result)
2629 return result;
2630
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002631 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002632 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002635 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636}
2637
Bjorn Helgaase747f272009-03-24 16:49:43 -06002638int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
2640 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Patrick Mochel5b327262006-05-10 10:33:00 -04002642 result = bus_register(&acpi_bus_type);
2643 if (result) {
2644 /* We don't want to quit even if we failed to add suspend/resume */
2645 printk(KERN_ERR PREFIX "Could not register bus type\n");
2646 }
2647
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01002648 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01002649 acpi_pci_link_init();
Rafael J. Wysockiac212b62013-05-03 00:26:22 +02002650 acpi_processor_init();
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01002651 acpi_lpss_init();
Ken Xue92082a82015-02-06 08:27:51 +08002652 acpi_apd_init();
Lan Tianyu2fa97fe2013-06-05 02:27:50 +00002653 acpi_cmos_rtc_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01002654 acpi_container_init();
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01002655 acpi_memory_hotplug_init();
Zhang Ruieec15ed2014-05-30 04:23:01 +02002656 acpi_pnp_init();
Zhang Rui3230bbf2014-03-14 00:34:05 +08002657 acpi_int340x_thermal_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01002658
Rafael J. Wysocki7d284352015-04-24 02:18:01 +02002659 acpi_scan_add_handler(&generic_device_handler);
2660
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002661 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 * Enumerate devices in the ACPI namespace.
2664 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002665 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002667 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002669 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002671 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Aaron Lu2650ef42014-04-28 10:38:04 +08002673 /* Fixed feature devices do not exist on HW-reduced platform */
2674 if (!acpi_gbl_reduced_hardware) {
2675 result = acpi_bus_scan_fixed();
2676 if (result) {
2677 acpi_detach_data(acpi_root->handle,
2678 acpi_scan_drop_device);
2679 acpi_device_del(acpi_root);
2680 put_device(&acpi_root->dev);
2681 goto out;
2682 }
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002683 }
2684
2685 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002686
2687 out:
2688 mutex_unlock(&acpi_scan_lock);
2689 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690}