blob: 99f97ac64aa48909cf913c0dba7618c9638fe805 [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
Thomas Renninger29b71a12007-07-23 14:43:51 +0200117/*
118 * Creates hid/cid(s) string needed for modalias and uevent
119 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
120 * char *modalias: "acpi:IBM0001:ACPI0001"
Zhang Rui3d8e0092014-01-14 16:46:35 +0800121 * Return: 0: no _HID and no _CID
122 * -EINVAL: output error
123 * -ENOMEM: output is truncated
Thomas Renninger29b71a12007-07-23 14:43:51 +0200124*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +0200125static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
126 int size)
127{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200128 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800129 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600130 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200131
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200132 if (list_empty(&acpi_dev->pnp.ids))
133 return 0;
134
Mika Westerberg733e6252014-10-21 13:33:56 +0200135 /*
136 * If the device has PRP0001 we expose DT compatible modalias
137 * instead in form of of:NnameTCcompatible.
138 */
139 if (acpi_dev->data.of_compatible) {
140 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
141 const union acpi_object *of_compatible, *obj;
142 int i, nval;
143 char *c;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200144
Mika Westerberg733e6252014-10-21 13:33:56 +0200145 acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf);
146 /* DT strings are all in lower case */
147 for (c = buf.pointer; *c != '\0'; c++)
148 *c = tolower(*c);
149
150 len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer);
151 ACPI_FREE(buf.pointer);
152
153 of_compatible = acpi_dev->data.of_compatible;
154 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
155 nval = of_compatible->package.count;
156 obj = of_compatible->package.elements;
157 } else { /* Must be ACPI_TYPE_STRING. */
158 nval = 1;
159 obj = of_compatible;
160 }
161 for (i = 0; i < nval; i++, obj++) {
162 count = snprintf(&modalias[len], size, "C%s",
163 obj->string.pointer);
164 if (count < 0)
165 return -EINVAL;
166 if (count >= size)
167 return -ENOMEM;
168
169 len += count;
170 size -= count;
171 }
172 } else {
173 len = snprintf(modalias, size, "acpi:");
174 size -= len;
175
176 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
177 count = snprintf(&modalias[len], size, "%s:", id->id);
178 if (count < 0)
179 return -EINVAL;
180 if (count >= size)
181 return -ENOMEM;
182 len += count;
183 size -= count;
184 }
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800185 }
186
Thomas Renninger29b71a12007-07-23 14:43:51 +0200187 modalias[len] = '\0';
188 return len;
189}
190
Zhang Rui6eb24512014-01-14 16:46:36 +0800191/*
Mika Westerberg52870782014-10-24 16:40:54 +0200192 * acpi_companion_match() - Can we match via ACPI companion device
193 * @dev: Device in question
194 *
195 * Check if the given device has an ACPI companion and if that companion has
196 * a valid list of PNP IDs, and if the device is the first (primary) physical
197 * device associated with it.
198 *
199 * If multiple physical devices are attached to a single ACPI companion, we need
200 * to be careful. The usage scenario for this kind of relationship is that all
201 * of the physical devices in question use resources provided by the ACPI
202 * companion. A typical case is an MFD device where all the sub-devices share
203 * the parent's ACPI companion. In such cases we can only allow the primary
204 * (first) physical device to be matched with the help of the companion's PNP
205 * IDs.
206 *
207 * Additional physical devices sharing the ACPI companion can still use
208 * resources available from it but they will be matched normally using functions
209 * provided by their bus types (and analogously for their modalias).
210 */
211static bool acpi_companion_match(const struct device *dev)
212{
213 struct acpi_device *adev;
214 bool ret;
215
216 adev = ACPI_COMPANION(dev);
217 if (!adev)
218 return false;
219
220 if (list_empty(&adev->pnp.ids))
221 return false;
222
223 mutex_lock(&adev->physical_node_lock);
224 if (list_empty(&adev->physical_node_list)) {
225 ret = false;
226 } else {
227 const struct acpi_device_physical_node *node;
228
229 node = list_first_entry(&adev->physical_node_list,
230 struct acpi_device_physical_node, node);
231 ret = node->dev == dev;
232 }
233 mutex_unlock(&adev->physical_node_lock);
234
235 return ret;
236}
237
238/*
Zhang Rui6eb24512014-01-14 16:46:36 +0800239 * Creates uevent modalias field for ACPI enumerated devices.
240 * Because the other buses does not support ACPI HIDs & CIDs.
241 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
242 * "acpi:IBM0001:ACPI0001"
243 */
244int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
245{
Zhang Rui6eb24512014-01-14 16:46:36 +0800246 int len;
247
Mika Westerberg52870782014-10-24 16:40:54 +0200248 if (!acpi_companion_match(dev))
Zhang Rui6eb24512014-01-14 16:46:36 +0800249 return -ENODEV;
250
251 if (add_uevent_var(env, "MODALIAS="))
252 return -ENOMEM;
Mika Westerberg52870782014-10-24 16:40:54 +0200253 len = create_modalias(ACPI_COMPANION(dev), &env->buf[env->buflen - 1],
Zhang Rui6eb24512014-01-14 16:46:36 +0800254 sizeof(env->buf) - env->buflen);
255 if (len <= 0)
256 return len;
257 env->buflen += len;
258 return 0;
259}
260EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
261
262/*
263 * Creates modalias sysfs attribute for ACPI enumerated devices.
264 * Because the other buses does not support ACPI HIDs & CIDs.
265 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
266 * "acpi:IBM0001:ACPI0001"
267 */
268int acpi_device_modalias(struct device *dev, char *buf, int size)
269{
Zhang Rui6eb24512014-01-14 16:46:36 +0800270 int len;
271
Mika Westerberg52870782014-10-24 16:40:54 +0200272 if (!acpi_companion_match(dev))
Zhang Rui6eb24512014-01-14 16:46:36 +0800273 return -ENODEV;
274
Mika Westerberg52870782014-10-24 16:40:54 +0200275 len = create_modalias(ACPI_COMPANION(dev), buf, size -1);
Zhang Rui6eb24512014-01-14 16:46:36 +0800276 if (len <= 0)
277 return len;
278 buf[len++] = '\n';
279 return len;
280}
281EXPORT_SYMBOL_GPL(acpi_device_modalias);
282
Thomas Renninger29b71a12007-07-23 14:43:51 +0200283static ssize_t
284acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
285 struct acpi_device *acpi_dev = to_acpi_device(dev);
286 int len;
287
Thomas Renninger29b71a12007-07-23 14:43:51 +0200288 len = create_modalias(acpi_dev, buf, 1024);
289 if (len <= 0)
Zhang Rui3d8e0092014-01-14 16:46:35 +0800290 return len;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200291 buf[len++] = '\n';
292 return len;
293}
294static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
295
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100296bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100297{
298 struct acpi_device_physical_node *pn;
299 bool offline = true;
300
301 mutex_lock(&adev->physical_node_lock);
302
303 list_for_each_entry(pn, &adev->physical_node_list, node)
304 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100305 if (uevent)
306 kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
307
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100308 offline = false;
309 break;
310 }
311
312 mutex_unlock(&adev->physical_node_lock);
313 return offline;
314}
315
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100316static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
317 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200318{
319 struct acpi_device *device = NULL;
320 struct acpi_device_physical_node *pn;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200321 bool second_pass = (bool)data;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200322 acpi_status status = AE_OK;
323
324 if (acpi_bus_get_device(handle, &device))
325 return AE_OK;
326
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100327 if (device->handler && !device->handler->hotplug.enabled) {
328 *ret_p = &device->dev;
329 return AE_SUPPORT;
330 }
331
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200332 mutex_lock(&device->physical_node_lock);
333
334 list_for_each_entry(pn, &device->physical_node_list, node) {
335 int ret;
336
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200337 if (second_pass) {
338 /* Skip devices offlined by the first pass. */
339 if (pn->put_online)
340 continue;
341 } else {
342 pn->put_online = false;
343 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200344 ret = device_offline(pn->dev);
345 if (acpi_force_hot_remove)
346 continue;
347
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200348 if (ret >= 0) {
349 pn->put_online = !ret;
350 } else {
351 *ret_p = pn->dev;
352 if (second_pass) {
353 status = AE_ERROR;
354 break;
355 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200356 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200357 }
358
359 mutex_unlock(&device->physical_node_lock);
360
361 return status;
362}
363
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100364static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
365 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200366{
367 struct acpi_device *device = NULL;
368 struct acpi_device_physical_node *pn;
369
370 if (acpi_bus_get_device(handle, &device))
371 return AE_OK;
372
373 mutex_lock(&device->physical_node_lock);
374
375 list_for_each_entry(pn, &device->physical_node_list, node)
376 if (pn->put_online) {
377 device_online(pn->dev);
378 pn->put_online = false;
379 }
380
381 mutex_unlock(&device->physical_node_lock);
382
383 return AE_OK;
384}
385
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100386static int acpi_scan_try_to_offline(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Yinghai Lu5993c462013-01-11 22:40:41 +0000388 acpi_handle handle = device->handle;
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100389 struct device *errdev = NULL;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100390 acpi_status status;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100391
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200392 /*
393 * Carry out two passes here and ignore errors in the first pass,
394 * because if the devices in question are memory blocks and
395 * CONFIG_MEMCG is set, one of the blocks may hold data structures
396 * that the other blocks depend on, but it is not known in advance which
397 * block holds them.
398 *
399 * If the first pass is successful, the second one isn't needed, though.
400 */
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100401 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
402 NULL, acpi_bus_offline, (void *)false,
403 (void **)&errdev);
404 if (status == AE_SUPPORT) {
405 dev_warn(errdev, "Offline disabled.\n");
406 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
407 acpi_bus_online, NULL, NULL, NULL);
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100408 return -EPERM;
409 }
410 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200411 if (errdev) {
412 errdev = NULL;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200413 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100414 NULL, acpi_bus_offline, (void *)true,
415 (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200416 if (!errdev || acpi_force_hot_remove)
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100417 acpi_bus_offline(handle, 0, (void *)true,
418 (void **)&errdev);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200419
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200420 if (errdev && !acpi_force_hot_remove) {
421 dev_warn(errdev, "Offline failed.\n");
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100422 acpi_bus_online(handle, 0, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200423 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100424 ACPI_UINT32_MAX, acpi_bus_online,
425 NULL, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200426 return -EBUSY;
427 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200428 }
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100429 return 0;
430}
431
432static int acpi_scan_hot_remove(struct acpi_device *device)
433{
434 acpi_handle handle = device->handle;
435 unsigned long long sta;
436 acpi_status status;
437
Tang Chendee15922014-08-08 10:30:45 +0800438 if (device->handler && device->handler->hotplug.demand_offline
439 && !acpi_force_hot_remove) {
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100440 if (!acpi_scan_is_offline(device, true))
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100441 return -EBUSY;
442 } else {
443 int error = acpi_scan_try_to_offline(device);
444 if (error)
445 return error;
446 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200447
Zhang Rui26d46862008-04-29 02:35:48 -0400448 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100449 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400450
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100451 acpi_bus_trim(device);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200452
Jiang Liu7d2421f2013-06-29 00:24:40 +0800453 acpi_evaluate_lck(handle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /*
455 * TBD: _EJD support.
456 */
Jiang Liu7d2421f2013-06-29 00:24:40 +0800457 status = acpi_evaluate_ej0(handle);
458 if (status == AE_NOT_FOUND)
459 return -ENODEV;
460 else if (ACPI_FAILURE(status))
461 return -EIO;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100462
Toshi Kani882fd122013-03-13 19:29:26 +0000463 /*
464 * Verify if eject was indeed successful. If not, log an error
465 * message. No need to call _OST since _EJ0 call was made OK.
466 */
467 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
468 if (ACPI_FAILURE(status)) {
469 acpi_handle_warn(handle,
470 "Status check after eject failed (0x%x)\n", status);
471 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
472 acpi_handle_warn(handle,
473 "Eject incomplete - status 0x%llx\n", sta);
474 }
475
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100476 return 0;
477}
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100478
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100479static int acpi_scan_device_not_present(struct acpi_device *adev)
480{
481 if (!acpi_device_enumerated(adev)) {
482 dev_warn(&adev->dev, "Still not present\n");
483 return -EALREADY;
484 }
485 acpi_bus_trim(adev);
486 return 0;
487}
488
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100489static int acpi_scan_device_check(struct acpi_device *adev)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100490{
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200491 int error;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100492
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100493 acpi_bus_get_status(adev);
494 if (adev->status.present || adev->status.functional) {
495 /*
496 * This function is only called for device objects for which
497 * matching scan handlers exist. The only situation in which
498 * the scan handler is not attached to this device object yet
499 * is when the device has just appeared (either it wasn't
500 * present at all before or it was removed and then added
501 * again).
502 */
503 if (adev->handler) {
504 dev_warn(&adev->dev, "Already enumerated\n");
505 return -EALREADY;
506 }
507 error = acpi_bus_scan(adev->handle);
508 if (error) {
509 dev_warn(&adev->dev, "Namespace scan failure\n");
510 return error;
511 }
512 if (!adev->handler) {
513 dev_warn(&adev->dev, "Enumeration failure\n");
514 error = -ENODEV;
515 }
516 } else {
517 error = acpi_scan_device_not_present(adev);
518 }
519 return error;
520}
521
522static int acpi_scan_bus_check(struct acpi_device *adev)
523{
524 struct acpi_scan_handler *handler = adev->handler;
525 struct acpi_device *child;
526 int error;
527
528 acpi_bus_get_status(adev);
529 if (!(adev->status.present || adev->status.functional)) {
530 acpi_scan_device_not_present(adev);
531 return 0;
532 }
533 if (handler && handler->hotplug.scan_dependent)
534 return handler->hotplug.scan_dependent(adev);
535
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100536 error = acpi_bus_scan(adev->handle);
537 if (error) {
538 dev_warn(&adev->dev, "Namespace scan failure\n");
539 return error;
540 }
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100541 list_for_each_entry(child, &adev->children, node) {
542 error = acpi_scan_bus_check(child);
543 if (error)
544 return error;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100545 }
546 return 0;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100547}
548
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100549static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
550{
551 switch (type) {
552 case ACPI_NOTIFY_BUS_CHECK:
553 return acpi_scan_bus_check(adev);
554 case ACPI_NOTIFY_DEVICE_CHECK:
555 return acpi_scan_device_check(adev);
556 case ACPI_NOTIFY_EJECT_REQUEST:
557 case ACPI_OST_EC_OSPM_EJECT:
Rafael J. Wysockidd2151b2014-02-04 00:44:02 +0100558 if (adev->handler && !adev->handler->hotplug.enabled) {
559 dev_info(&adev->dev, "Eject disabled\n");
560 return -EPERM;
561 }
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100562 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
563 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100564 return acpi_scan_hot_remove(adev);
565 }
566 return -EINVAL;
567}
568
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +0100569void acpi_device_hotplug(struct acpi_device *adev, u32 src)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100570{
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100571 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100572 int error = -ENODEV;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100573
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200574 lock_device_hotplug();
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200575 mutex_lock(&acpi_scan_lock);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100576
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100577 /*
578 * The device object's ACPI handle cannot become invalid as long as we
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100579 * are holding acpi_scan_lock, but it might have become invalid before
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100580 * that lock was acquired.
581 */
582 if (adev->handle == INVALID_ACPI_HANDLE)
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100583 goto err_out;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100584
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100585 if (adev->flags.is_dock_station) {
586 error = dock_notify(adev, src);
587 } else if (adev->flags.hotplug_notify) {
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100588 error = acpi_generic_hotplug_event(adev, src);
Rafael J. Wysockidd2151b2014-02-04 00:44:02 +0100589 if (error == -EPERM) {
590 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
591 goto err_out;
592 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100593 } else {
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100594 int (*notify)(struct acpi_device *, u32);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100595
596 acpi_lock_hp_context();
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100597 notify = adev->hp ? adev->hp->notify : NULL;
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100598 acpi_unlock_hp_context();
599 /*
600 * There may be additional notify handlers for device objects
601 * without the .event() callback, so ignore them here.
602 */
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100603 if (notify)
604 error = notify(adev, src);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100605 else
606 goto out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100607 }
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100608 if (!error)
609 ost_code = ACPI_OST_SC_SUCCESS;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100610
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100611 err_out:
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100612 acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100613
614 out:
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +0100615 acpi_bus_put_acpi_device(adev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100616 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200617 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100618}
619
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100620static ssize_t real_power_state_show(struct device *dev,
621 struct device_attribute *attr, char *buf)
622{
623 struct acpi_device *adev = to_acpi_device(dev);
624 int state;
625 int ret;
626
627 ret = acpi_device_get_power(adev, &state);
628 if (ret)
629 return ret;
630
631 return sprintf(buf, "%s\n", acpi_power_state_string(state));
632}
633
634static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
635
636static ssize_t power_state_show(struct device *dev,
637 struct device_attribute *attr, char *buf)
638{
639 struct acpi_device *adev = to_acpi_device(dev);
640
641 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
642}
643
644static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800647acpi_eject_store(struct device *d, struct device_attribute *attr,
648 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800650 struct acpi_device *acpi_device = to_acpi_device(d);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100651 acpi_object_type not_used;
652 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100654 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100657 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
658 && !acpi_device->driver)
659 return -ENODEV;
660
661 status = acpi_get_type(acpi_device->handle, &not_used);
662 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
663 return -ENODEV;
664
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100665 get_device(&acpi_device->dev);
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +0100666 status = acpi_hotplug_schedule(acpi_device, ACPI_OST_EC_OSPM_EJECT);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200667 if (ACPI_SUCCESS(status))
668 return count;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100669
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200670 put_device(&acpi_device->dev);
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100671 acpi_evaluate_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
672 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100673 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800676static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800678static ssize_t
679acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
680 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600682 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800683}
684static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
685
Lv Zheng923d4a42012-10-30 14:43:26 +0100686static ssize_t acpi_device_uid_show(struct device *dev,
687 struct device_attribute *attr, char *buf)
688{
689 struct acpi_device *acpi_dev = to_acpi_device(dev);
690
691 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
692}
693static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
694
695static ssize_t acpi_device_adr_show(struct device *dev,
696 struct device_attribute *attr, char *buf)
697{
698 struct acpi_device *acpi_dev = to_acpi_device(dev);
699
700 return sprintf(buf, "0x%08x\n",
701 (unsigned int)(acpi_dev->pnp.bus_address));
702}
703static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
704
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800705static ssize_t
706acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
707 struct acpi_device *acpi_dev = to_acpi_device(dev);
708 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
709 int result;
710
711 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600712 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800713 goto end;
714
715 result = sprintf(buf, "%s\n", (char*)path.pointer);
716 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600717end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800718 return result;
719}
720static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
721
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600722/* sysfs file that shows description text from the ACPI _STR method */
723static ssize_t description_show(struct device *dev,
724 struct device_attribute *attr,
725 char *buf) {
726 struct acpi_device *acpi_dev = to_acpi_device(dev);
727 int result;
728
729 if (acpi_dev->pnp.str_obj == NULL)
730 return 0;
731
732 /*
733 * The _STR object contains a Unicode identifier for a device.
734 * We need to convert to utf-8 so it can be displayed.
735 */
736 result = utf16s_to_utf8s(
737 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
738 acpi_dev->pnp.str_obj->buffer.length,
739 UTF16_LITTLE_ENDIAN, buf,
740 PAGE_SIZE);
741
742 buf[result++] = '\n';
743
744 return result;
745}
746static DEVICE_ATTR(description, 0444, description_show, NULL);
747
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100748static ssize_t
749acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
750 char *buf) {
751 struct acpi_device *acpi_dev = to_acpi_device(dev);
Yasuaki Ishimatsua383b682014-09-03 13:39:13 +0900752 acpi_status status;
753 unsigned long long sun;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100754
Yasuaki Ishimatsua383b682014-09-03 13:39:13 +0900755 status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun);
756 if (ACPI_FAILURE(status))
757 return -ENODEV;
758
759 return sprintf(buf, "%llu\n", sun);
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100760}
761static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
762
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800763static ssize_t status_show(struct device *dev, struct device_attribute *attr,
764 char *buf) {
765 struct acpi_device *acpi_dev = to_acpi_device(dev);
766 acpi_status status;
767 unsigned long long sta;
768
769 status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
770 if (ACPI_FAILURE(status))
771 return -ENODEV;
772
773 return sprintf(buf, "%llu\n", sta);
774}
775static DEVICE_ATTR_RO(status);
776
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800777static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600779 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800780 acpi_status status;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800781 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800783 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800784 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800785 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600786 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800787 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600788 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800789 goto end;
790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200792 if (!list_empty(&dev->pnp.ids)) {
793 result = device_create_file(&dev->dev, &dev_attr_hid);
794 if (result)
795 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200797 result = device_create_file(&dev->dev, &dev_attr_modalias);
798 if (result)
799 goto end;
800 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200801
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600802 /*
803 * If device has _STR, 'description' file is created
804 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800805 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600806 status = acpi_evaluate_object(dev->handle, "_STR",
807 NULL, &buffer);
808 if (ACPI_FAILURE(status))
809 buffer.pointer = NULL;
810 dev->pnp.str_obj = buffer.pointer;
811 result = device_create_file(&dev->dev, &dev_attr_description);
812 if (result)
813 goto end;
814 }
815
Toshi Kanid4e1a692013-03-04 21:30:41 +0000816 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100817 result = device_create_file(&dev->dev, &dev_attr_adr);
818 if (dev->pnp.unique_id)
819 result = device_create_file(&dev->dev, &dev_attr_uid);
820
Yasuaki Ishimatsua383b682014-09-03 13:39:13 +0900821 if (acpi_has_method(dev->handle, "_SUN")) {
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100822 result = device_create_file(&dev->dev, &dev_attr_sun);
823 if (result)
824 goto end;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100825 }
826
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800827 if (acpi_has_method(dev->handle, "_STA")) {
828 result = device_create_file(&dev->dev, &dev_attr_status);
829 if (result)
830 goto end;
831 }
832
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800833 /*
834 * If device has _EJ0, 'eject' file is created that is used to trigger
835 * hot-removal function from userland.
836 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800837 if (acpi_has_method(dev->handle, "_EJ0")) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800838 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100839 if (result)
840 return result;
841 }
842
843 if (dev->flags.power_manageable) {
844 result = device_create_file(&dev->dev, &dev_attr_power_state);
845 if (result)
846 return result;
847
848 if (dev->power.flags.power_resources)
849 result = device_create_file(&dev->dev,
850 &dev_attr_real_power_state);
851 }
852
Alex Chiang0c526d92009-05-14 08:31:37 -0600853end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800854 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800855}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800857static void acpi_device_remove_files(struct acpi_device *dev)
858{
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100859 if (dev->flags.power_manageable) {
860 device_remove_file(&dev->dev, &dev_attr_power_state);
861 if (dev->power.flags.power_resources)
862 device_remove_file(&dev->dev,
863 &dev_attr_real_power_state);
864 }
865
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800866 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600867 * If device has _STR, remove 'description' file
868 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800869 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600870 kfree(dev->pnp.str_obj);
871 device_remove_file(&dev->dev, &dev_attr_description);
872 }
873 /*
874 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800875 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800876 if (acpi_has_method(dev->handle, "_EJ0"))
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800877 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800878
Jiang Liu952c63e2013-06-29 00:24:38 +0800879 if (acpi_has_method(dev->handle, "_SUN"))
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100880 device_remove_file(&dev->dev, &dev_attr_sun);
881
Lv Zheng923d4a42012-10-30 14:43:26 +0100882 if (dev->pnp.unique_id)
883 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000884 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100885 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600886 device_remove_file(&dev->dev, &dev_attr_modalias);
887 device_remove_file(&dev->dev, &dev_attr_hid);
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800888 if (acpi_has_method(dev->handle, "_STA"))
889 device_remove_file(&dev->dev, &dev_attr_status);
Alex Chiang0c526d92009-05-14 08:31:37 -0600890 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800891 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800892}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800894 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200896
Mika Westerbergcf761af2012-10-31 22:44:41 +0100897static const struct acpi_device_id *__acpi_match_device(
898 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200899{
900 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600901 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200902
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800903 /*
904 * If the device is not present, it is unnecessary to load device
905 * driver for it.
906 */
907 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100908 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800909
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600910 for (id = ids; id->id[0]; id++)
911 list_for_each_entry(hwid, &device->pnp.ids, list)
912 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100913 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200914
Mika Westerbergcf761af2012-10-31 22:44:41 +0100915 return NULL;
916}
917
918/**
919 * acpi_match_device - Match a struct device against a given list of ACPI IDs
920 * @ids: Array of struct acpi_device_id object to match against.
921 * @dev: The device structure to match.
922 *
923 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
924 * object for that handle and use that object to match against a given list of
925 * device IDs.
926 *
927 * Return a pointer to the first matching ID on success or %NULL on failure.
928 */
929const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
930 const struct device *dev)
931{
932 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100933 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100934
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100935 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100936 return NULL;
937
Mika Westerberg52870782014-10-24 16:40:54 +0200938 if (!acpi_companion_match(dev))
939 return NULL;
940
Mika Westerbergcf761af2012-10-31 22:44:41 +0100941 return __acpi_match_device(adev, ids);
942}
943EXPORT_SYMBOL_GPL(acpi_match_device);
944
945int acpi_match_device_ids(struct acpi_device *device,
946 const struct acpi_device_id *ids)
947{
948 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200949}
950EXPORT_SYMBOL(acpi_match_device_ids);
951
Rafael J. Wysocki54fe9ce2015-04-10 16:07:30 +0200952/**
953 * acpi_of_match_device - Match device using the "compatible" property.
954 * @dev: Device to match.
955 * @of_match_table: List of device IDs to match against.
956 *
957 * If @dev has an ACPI companion which has the special PRP0001 device ID in its
958 * list of identifiers and a _DSD object with the "compatible" property, use
959 * that property to match against the given list of identifiers.
960 */
961static bool acpi_of_match_device(struct device *dev,
962 const struct of_device_id *of_match_table)
Mika Westerberg733e6252014-10-21 13:33:56 +0200963{
964 const union acpi_object *of_compatible, *obj;
965 struct acpi_device *adev;
966 int i, nval;
967
968 adev = ACPI_COMPANION(dev);
969 if (!adev)
970 return false;
971
972 of_compatible = adev->data.of_compatible;
Rafael J. Wysocki54fe9ce2015-04-10 16:07:30 +0200973 if (!of_match_table || !of_compatible)
Mika Westerberg733e6252014-10-21 13:33:56 +0200974 return false;
975
976 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
977 nval = of_compatible->package.count;
978 obj = of_compatible->package.elements;
979 } else { /* Must be ACPI_TYPE_STRING. */
980 nval = 1;
981 obj = of_compatible;
982 }
983 /* Now we can look for the driver DT compatible strings */
984 for (i = 0; i < nval; i++, obj++) {
985 const struct of_device_id *id;
986
Rafael J. Wysocki54fe9ce2015-04-10 16:07:30 +0200987 for (id = of_match_table; id->compatible[0]; id++)
Mika Westerberg733e6252014-10-21 13:33:56 +0200988 if (!strcasecmp(obj->string.pointer, id->compatible))
989 return true;
990 }
991
992 return false;
993}
994
995bool acpi_driver_match_device(struct device *dev,
996 const struct device_driver *drv)
997{
998 if (!drv->acpi_match_table)
Rafael J. Wysocki54fe9ce2015-04-10 16:07:30 +0200999 return acpi_of_match_device(dev, drv->of_match_table);
Mika Westerberg733e6252014-10-21 13:33:56 +02001000
1001 return !!acpi_match_device(drv->acpi_match_table, dev);
1002}
1003EXPORT_SYMBOL_GPL(acpi_driver_match_device);
1004
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001005static void acpi_free_power_resources_lists(struct acpi_device *device)
1006{
1007 int i;
1008
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001009 if (device->wakeup.flags.valid)
1010 acpi_power_resources_list_free(&device->wakeup.resources);
1011
Rafael J. Wysocki1b1f3e12015-01-01 23:38:28 +01001012 if (!device->power.flags.power_resources)
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001013 return;
1014
1015 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
1016 struct acpi_device_power_state *ps = &device->power.states[i];
1017 acpi_power_resources_list_free(&ps->resources);
1018 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001019}
1020
Patrick Mochel1890a972006-12-07 20:56:31 +08001021static void acpi_device_release(struct device *dev)
1022{
1023 struct acpi_device *acpi_dev = to_acpi_device(dev);
1024
Mika Westerbergffdcd952014-10-21 13:33:55 +02001025 acpi_free_properties(acpi_dev);
Toshi Kanic0af4172013-03-04 21:30:42 +00001026 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001027 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +08001028 kfree(acpi_dev);
1029}
1030
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001031static int acpi_bus_match(struct device *dev, struct device_driver *drv)
1032{
1033 struct acpi_device *acpi_dev = to_acpi_device(dev);
1034 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
1035
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +01001036 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001037 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001038}
1039
Kay Sievers7eff2e72007-08-14 15:15:12 +02001040static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001041{
1042 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +02001043 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001044
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001045 if (list_empty(&acpi_dev->pnp.ids))
1046 return 0;
1047
Kay Sievers7eff2e72007-08-14 15:15:12 +02001048 if (add_uevent_var(env, "MODALIAS="))
1049 return -ENOMEM;
1050 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
1051 sizeof(env->buf) - env->buflen);
Zhang Rui3d8e0092014-01-14 16:46:35 +08001052 if (len <= 0)
1053 return len;
Kay Sievers7eff2e72007-08-14 15:15:12 +02001054 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return 0;
1056}
1057
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001058static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
1059{
1060 struct acpi_device *device = data;
1061
1062 device->driver->ops.notify(device, event);
1063}
1064
Lan Tianyu236105d2014-08-26 01:29:24 +02001065static void acpi_device_notify_fixed(void *data)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001066{
1067 struct acpi_device *device = data;
1068
Bjorn Helgaas53de5352009-08-31 22:32:20 +00001069 /* Fixed hardware devices have no handles */
1070 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Lan Tianyu236105d2014-08-26 01:29:24 +02001071}
1072
Aaron Lufd9caef2015-03-13 13:52:50 +08001073static u32 acpi_device_fixed_event(void *data)
Lan Tianyu236105d2014-08-26 01:29:24 +02001074{
1075 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_device_notify_fixed, data);
Aaron Lufd9caef2015-03-13 13:52:50 +08001076 return ACPI_INTERRUPT_HANDLED;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001077}
1078
1079static int acpi_device_install_notify_handler(struct acpi_device *device)
1080{
1081 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001082
Bjorn Helgaasccba2a32009-09-21 19:29:15 +00001083 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001084 status =
1085 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +02001086 acpi_device_fixed_event,
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001087 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +00001088 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001089 status =
1090 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +02001091 acpi_device_fixed_event,
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001092 device);
1093 else
1094 status = acpi_install_notify_handler(device->handle,
1095 ACPI_DEVICE_NOTIFY,
1096 acpi_device_notify,
1097 device);
1098
1099 if (ACPI_FAILURE(status))
1100 return -EINVAL;
1101 return 0;
1102}
1103
1104static void acpi_device_remove_notify_handler(struct acpi_device *device)
1105{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +00001106 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001107 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +02001108 acpi_device_fixed_event);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +00001109 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001110 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +02001111 acpi_device_fixed_event);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001112 else
1113 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
1114 acpi_device_notify);
1115}
1116
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001117static int acpi_device_probe(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +08001118{
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001119 struct acpi_device *acpi_dev = to_acpi_device(dev);
1120 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
1121 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Rafael J. Wysockifc2e0a82014-08-26 01:29:21 +02001123 if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev))
Rafael J. Wysocki24071f42013-06-16 00:36:41 +02001124 return -EINVAL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001125
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001126 if (!acpi_drv->ops.add)
1127 return -ENOSYS;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001128
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001129 ret = acpi_drv->ops.add(acpi_dev);
1130 if (ret)
1131 return ret;
1132
1133 acpi_dev->driver = acpi_drv;
1134 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1135 "Driver [%s] successfully bound to device [%s]\n",
1136 acpi_drv->name, acpi_dev->pnp.bus_id));
1137
1138 if (acpi_drv->ops.notify) {
1139 ret = acpi_device_install_notify_handler(acpi_dev);
1140 if (ret) {
1141 if (acpi_drv->ops.remove)
1142 acpi_drv->ops.remove(acpi_dev);
1143
1144 acpi_dev->driver = NULL;
1145 acpi_dev->driver_data = NULL;
1146 return ret;
1147 }
Zhang Rui9e89dde2006-12-07 20:56:16 +08001148 }
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001149
1150 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
1151 acpi_drv->name, acpi_dev->pnp.bus_id));
1152 get_device(dev);
1153 return 0;
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001154}
1155
1156static int acpi_device_remove(struct device * dev)
1157{
1158 struct acpi_device *acpi_dev = to_acpi_device(dev);
1159 struct acpi_driver *acpi_drv = acpi_dev->driver;
1160
1161 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001162 if (acpi_drv->ops.notify)
1163 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001164 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001165 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001166 }
1167 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001168 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001169
1170 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +08001171 return 0;
1172}
1173
David Brownell55955aa2007-05-08 00:28:35 -07001174struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +08001175 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001176 .match = acpi_bus_match,
1177 .probe = acpi_device_probe,
1178 .remove = acpi_device_remove,
1179 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180};
1181
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001182static void acpi_device_del(struct acpi_device *device)
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001183{
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001184 mutex_lock(&acpi_device_lock);
1185 if (device->parent)
1186 list_del(&device->node);
1187
1188 list_del(&device->wakeup_list);
1189 mutex_unlock(&acpi_device_lock);
1190
1191 acpi_power_add_remove_device(device, false);
1192 acpi_device_remove_files(device);
1193 if (device->remove)
1194 device->remove(device);
1195
1196 device_del(&device->dev);
1197}
1198
1199static LIST_HEAD(acpi_device_del_list);
1200static DEFINE_MUTEX(acpi_device_del_lock);
1201
1202static void acpi_device_del_work_fn(struct work_struct *work_not_used)
1203{
1204 for (;;) {
1205 struct acpi_device *adev;
1206
1207 mutex_lock(&acpi_device_del_lock);
1208
1209 if (list_empty(&acpi_device_del_list)) {
1210 mutex_unlock(&acpi_device_del_lock);
1211 break;
1212 }
1213 adev = list_first_entry(&acpi_device_del_list,
1214 struct acpi_device, del_list);
1215 list_del(&adev->del_list);
1216
1217 mutex_unlock(&acpi_device_del_lock);
1218
1219 acpi_device_del(adev);
1220 /*
1221 * Drop references to all power resources that might have been
1222 * used by the device.
1223 */
1224 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
1225 put_device(&adev->dev);
1226 }
1227}
1228
1229/**
1230 * acpi_scan_drop_device - Drop an ACPI device object.
1231 * @handle: Handle of an ACPI namespace node, not used.
1232 * @context: Address of the ACPI device object to drop.
1233 *
1234 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
1235 * namespace node the device object pointed to by @context is attached to.
1236 *
1237 * The unregistration is carried out asynchronously to avoid running
1238 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
1239 * ensure the correct ordering (the device objects must be unregistered in the
1240 * same order in which the corresponding namespace nodes are deleted).
1241 */
1242static void acpi_scan_drop_device(acpi_handle handle, void *context)
1243{
1244 static DECLARE_WORK(work, acpi_device_del_work_fn);
1245 struct acpi_device *adev = context;
1246
1247 mutex_lock(&acpi_device_del_lock);
1248
1249 /*
1250 * Use the ACPI hotplug workqueue which is ordered, so this work item
1251 * won't run after any hotplug work items submitted subsequently. That
1252 * prevents attempts to register device objects identical to those being
1253 * deleted from happening concurrently (such attempts result from
1254 * hotplug events handled via the ACPI hotplug workqueue). It also will
1255 * run after all of the work items submitted previosuly, which helps
1256 * those work items to ensure that they are not accessing stale device
1257 * objects.
1258 */
1259 if (list_empty(&acpi_device_del_list))
1260 acpi_queue_hotplug_work(&work);
1261
1262 list_add_tail(&adev->del_list, &acpi_device_del_list);
1263 /* Make acpi_ns_validate_handle() return NULL for this handle. */
1264 adev->handle = INVALID_ACPI_HANDLE;
1265
1266 mutex_unlock(&acpi_device_del_lock);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001267}
1268
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001269static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
1270 void (*callback)(void *))
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001271{
1272 acpi_status status;
1273
1274 if (!device)
1275 return -EINVAL;
1276
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001277 status = acpi_get_data_full(handle, acpi_scan_drop_device,
1278 (void **)device, callback);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001279 if (ACPI_FAILURE(status) || !*device) {
1280 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
1281 handle));
1282 return -ENODEV;
1283 }
1284 return 0;
1285}
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001286
1287int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
1288{
1289 return acpi_get_device_data(handle, device, NULL);
1290}
Rafael J. Wysocki65859252013-10-01 23:02:43 +02001291EXPORT_SYMBOL(acpi_bus_get_device);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001292
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001293static void get_acpi_device(void *dev)
1294{
1295 if (dev)
1296 get_device(&((struct acpi_device *)dev)->dev);
1297}
1298
1299struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
1300{
1301 struct acpi_device *adev = NULL;
1302
1303 acpi_get_device_data(handle, &adev, get_acpi_device);
1304 return adev;
1305}
1306
1307void acpi_bus_put_acpi_device(struct acpi_device *adev)
1308{
1309 put_device(&adev->dev);
1310}
1311
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001312int acpi_device_add(struct acpi_device *device,
1313 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001315 int result;
1316 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1317 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001318
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001319 if (device->handle) {
1320 acpi_status status;
1321
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001322 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001323 device);
1324 if (ACPI_FAILURE(status)) {
1325 acpi_handle_err(device->handle,
1326 "Unable to attach device data\n");
1327 return -ENODEV;
1328 }
1329 }
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 /*
1332 * Linkage
1333 * -------
1334 * Link this device to its parent and siblings.
1335 */
1336 INIT_LIST_HEAD(&device->children);
1337 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001339 INIT_LIST_HEAD(&device->physical_node_list);
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001340 INIT_LIST_HEAD(&device->del_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001341 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001343 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1344 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001345 pr_err(PREFIX "Memory allocation error\n");
1346 result = -ENOMEM;
1347 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001348 }
1349
Shaohua Li90905892009-04-07 10:24:29 +08001350 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001351 /*
1352 * Find suitable bus_id and instance number in acpi_bus_id_list
1353 * If failed, create one and link it into acpi_bus_id_list
1354 */
1355 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001356 if (!strcmp(acpi_device_bus_id->bus_id,
1357 acpi_device_hid(device))) {
1358 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001359 found = 1;
1360 kfree(new_bus_id);
1361 break;
1362 }
1363 }
Alex Chiang0c526d92009-05-14 08:31:37 -06001364 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001365 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001366 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001367 acpi_device_bus_id->instance_no = 0;
1368 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1369 }
Kay Sievers07944692008-10-30 01:18:59 +01001370 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 +08001371
Len Brown33b57152008-12-15 22:09:26 -05001372 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -05001374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (device->wakeup.flags.valid)
1376 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +08001377 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Patrick Mochel1890a972006-12-07 20:56:31 +08001379 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001380 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +08001381 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001382 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001383 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -06001384 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -06001385 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001386 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001387 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001388
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001389 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -06001390 if (result)
Kay Sievers07944692008-10-30 01:18:59 +01001391 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1392 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001393
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001394 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001395
1396 err:
Shaohua Li90905892009-04-07 10:24:29 +08001397 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001398 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001399 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001400 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001401 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001402
1403 err_detach:
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001404 acpi_detach_data(device->handle, acpi_scan_drop_device);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001405 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406}
1407
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001408struct acpi_device *acpi_get_next_child(struct device *dev,
1409 struct acpi_device *child)
1410{
1411 struct acpi_device *adev = ACPI_COMPANION(dev);
1412 struct list_head *head, *next;
1413
1414 if (!adev)
1415 return NULL;
1416
1417 head = &adev->children;
1418 if (list_empty(head))
1419 return NULL;
1420
1421 if (!child)
1422 return list_first_entry(head, struct acpi_device, node);
1423
1424 next = child->node.next;
1425 return next == head ? NULL : list_entry(next, struct acpi_device, node);
1426}
1427
Zhang Rui9e89dde2006-12-07 20:56:16 +08001428/* --------------------------------------------------------------------------
1429 Driver Management
1430 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001432 * acpi_bus_register_driver - register a driver with the ACPI bus
1433 * @driver: driver being registered
1434 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001436 * devices that match the driver's criteria and binds. Returns zero for
1437 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 */
Len Brown4be44fc2005-08-05 00:44:28 -04001439int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Patrick Mochel1890a972006-12-07 20:56:31 +08001441 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001444 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001445 driver->drv.name = driver->name;
1446 driver->drv.bus = &acpi_bus_type;
1447 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Patrick Mochel1890a972006-12-07 20:56:31 +08001449 ret = driver_register(&driver->drv);
1450 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Len Brown4be44fc2005-08-05 00:44:28 -04001453EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455/**
Hanjun Guob27b14c2013-09-22 15:42:41 +08001456 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001457 * @driver: driver to unregister
1458 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1460 * devices that match the driver's criteria and unbinds.
1461 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001462void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Patrick Mochel1890a972006-12-07 20:56:31 +08001464 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
Len Brown4be44fc2005-08-05 00:44:28 -04001466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467EXPORT_SYMBOL(acpi_bus_unregister_driver);
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469/* --------------------------------------------------------------------------
1470 Device Enumeration
1471 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001472static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1473{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001474 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001475 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001476
1477 /*
1478 * Fixed hardware devices do not appear in the namespace and do not
1479 * have handles, but we fabricate acpi_devices for them, so we have
1480 * to deal with them specially.
1481 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001482 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001483 return acpi_root;
1484
1485 do {
1486 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001487 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001488 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1489 } while (acpi_bus_get_device(handle, &device));
1490 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001491}
1492
Len Brownc8f7a622006-07-09 17:22:28 -04001493acpi_status
1494acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1495{
1496 acpi_status status;
1497 acpi_handle tmp;
1498 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1499 union acpi_object *obj;
1500
1501 status = acpi_get_handle(handle, "_EJD", &tmp);
1502 if (ACPI_FAILURE(status))
1503 return status;
1504
1505 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1506 if (ACPI_SUCCESS(status)) {
1507 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001508 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1509 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001510 kfree(buffer.pointer);
1511 }
1512 return status;
1513}
1514EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1515
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001516static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1517 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001519 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1520 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001522 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001523 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001525 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001526 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001528 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001530 /* _PRW */
1531 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1532 if (ACPI_FAILURE(status)) {
1533 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001534 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001535 }
1536
1537 package = (union acpi_object *)buffer.pointer;
1538
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001539 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001540 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001543 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001544 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (element->type == ACPI_TYPE_PACKAGE) {
1547 if ((element->package.count < 2) ||
1548 (element->package.elements[0].type !=
1549 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001550 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001551 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001552
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001553 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001555 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 (u32) element->package.elements[1].integer.value;
1557 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001558 wakeup->gpe_device = NULL;
1559 wakeup->gpe_number = element->integer.value;
1560 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001561 goto out;
1562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001565 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001566 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001567
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001568 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001570 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1571 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001572 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001574 if (!list_empty(&wakeup->resources)) {
1575 int sleep_state;
1576
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001577 err = acpi_power_wakeup_list_init(&wakeup->resources,
1578 &sleep_state);
1579 if (err) {
1580 acpi_handle_warn(handle, "Retrieving current states "
1581 "of wakeup power resources failed\n");
1582 acpi_power_resources_list_free(&wakeup->resources);
1583 goto out;
1584 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001585 if (sleep_state < wakeup->sleep_state) {
1586 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1587 "(S%d) by S%d from power resources\n",
1588 (int)wakeup->sleep_state, sleep_state);
1589 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001592
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001593 out:
1594 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001595 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001598static void acpi_wakeup_gpe_init(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001600 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001601 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001602 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001603 {"PNP0C0E", 0},
1604 {"", 0},
1605 };
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001606 struct acpi_device_wakeup *wakeup = &device->wakeup;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001607 acpi_status status;
1608 acpi_event_status event_status;
1609
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001610 wakeup->flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001611
1612 /* Power button, Lid switch always enable wakeup */
1613 if (!acpi_match_device_ids(device, button_device_ids)) {
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001614 wakeup->flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001615 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1616 /* Do not use Lid/sleep button for S5 wakeup */
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001617 if (wakeup->sleep_state == ACPI_STATE_S5)
1618 wakeup->sleep_state = ACPI_STATE_S4;
Zhang Ruib7e38302012-12-04 23:23:16 +01001619 }
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001620 acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001621 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001622 return;
1623 }
1624
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001625 acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
1626 wakeup->gpe_number);
1627 status = acpi_get_gpe_status(wakeup->gpe_device, wakeup->gpe_number,
1628 &event_status);
1629 if (ACPI_FAILURE(status))
1630 return;
1631
Lv Zheng2f857232014-10-10 10:40:05 +08001632 wakeup->flags.run_wake = !!(event_status & ACPI_EVENT_FLAG_HAS_HANDLER);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001633}
1634
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001635static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001636{
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001637 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001638
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001639 /* Presence of _PRW indicates wake capable */
Jiang Liu952c63e2013-06-29 00:24:38 +08001640 if (!acpi_has_method(device->handle, "_PRW"))
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001641 return;
1642
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001643 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1644 &device->wakeup);
1645 if (err) {
1646 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001647 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001651 device->wakeup.prepare_count = 0;
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001652 acpi_wakeup_gpe_init(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001653 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1654 * system for the ACPI device with the _PRW object.
1655 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1656 * So it is necessary to call _DSW object first. Only when it is not
1657 * present will the _PSW object used.
1658 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001659 err = acpi_device_sleep_wake(device, 0, 0, 0);
1660 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001661 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1662 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001665static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001667 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001668 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1669 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001670 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001671
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001672 INIT_LIST_HEAD(&ps->resources);
1673
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001674 /* Evaluate "_PRx" to get referenced power resources */
1675 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1676 if (ACPI_SUCCESS(status)) {
1677 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001678
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001679 if (buffer.length && package
1680 && package->type == ACPI_TYPE_PACKAGE
1681 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001682 int err = acpi_extract_power_resources(package, 0,
1683 &ps->resources);
1684 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001685 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001686 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001687 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001688 }
1689
1690 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001691 pathname[2] = 'S';
Jiang Liu952c63e2013-06-29 00:24:38 +08001692 if (acpi_has_method(device->handle, pathname))
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001693 ps->flags.explicit_set = 1;
1694
1695 /*
1696 * State is valid if there are means to put the device into it.
1697 * D3hot is only valid if _PR3 present.
1698 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001699 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001700 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1701 ps->flags.valid = 1;
1702 ps->flags.os_accessible = 1;
1703 }
1704
1705 ps->power = -1; /* Unknown - driver assigned */
1706 ps->latency = -1; /* Unknown - driver assigned */
1707}
1708
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001709static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001711 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001713 /* Presence of _PS0|_PR0 indicates 'power manageable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001714 if (!acpi_has_method(device->handle, "_PS0") &&
1715 !acpi_has_method(device->handle, "_PR0"))
1716 return;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001717
1718 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001721 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 */
Jiang Liu952c63e2013-06-29 00:24:38 +08001723 if (acpi_has_method(device->handle, "_PSC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001724 device->power.flags.explicit_get = 1;
Rafael J. Wysockif25c0ae2014-05-17 00:18:13 +02001725
Jiang Liu952c63e2013-06-29 00:24:38 +08001726 if (acpi_has_method(device->handle, "_IRC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001727 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Rafael J. Wysockif25c0ae2014-05-17 00:18:13 +02001729 if (acpi_has_method(device->handle, "_DSW"))
1730 device->power.flags.dsw_present = 1;
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001733 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001735 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1736 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001738 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Zhang Rui9e89dde2006-12-07 20:56:16 +08001740 /* Set defaults for D0 and D3 states (always valid) */
1741 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1742 device->power.states[ACPI_STATE_D0].power = 100;
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +02001743 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1744 device->power.states[ACPI_STATE_D3_COLD].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001746 /* Set D3cold's explicit_set flag if _PS3 exists. */
1747 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1748 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1749
Aaron Lu1399dfc2012-11-21 23:33:40 +01001750 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1751 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1752 device->power.flags.power_resources)
1753 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1754
Rafael J. Wysocki1b1f3e12015-01-01 23:38:28 +01001755 if (acpi_bus_init_power(device))
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001756 device->flags.power_manageable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757}
1758
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001759static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 /* Presence of _STA indicates 'dynamic_status' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001762 if (acpi_has_method(device->handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 device->flags.dynamic_status = 1;
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 /* Presence of _RMV indicates 'removable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001766 if (acpi_has_method(device->handle, "_RMV"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 device->flags.removable = 1;
1768
1769 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001770 if (acpi_has_method(device->handle, "_EJD") ||
1771 acpi_has_method(device->handle, "_EJ0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 device->flags.ejectable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773}
1774
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001775static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Len Brown4be44fc2005-08-05 00:44:28 -04001777 char bus_id[5] = { '?', 0 };
1778 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1779 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 /*
1782 * Bus ID
1783 * ------
1784 * The device's Bus ID is simply the object name.
1785 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1786 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001787 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001789 return;
1790 }
1791
1792 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 case ACPI_BUS_TYPE_POWER_BUTTON:
1794 strcpy(device->pnp.bus_id, "PWRF");
1795 break;
1796 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1797 strcpy(device->pnp.bus_id, "SLPF");
1798 break;
1799 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001800 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 /* Clean up trailing underscores (if any) */
1802 for (i = 3; i > 1; i--) {
1803 if (bus_id[i] == '_')
1804 bus_id[i] = '\0';
1805 else
1806 break;
1807 }
1808 strcpy(device->pnp.bus_id, bus_id);
1809 break;
1810 }
1811}
1812
Zhang Rui54735262007-01-11 02:09:09 -05001813/*
Jiang Liuebf4df82013-06-29 00:24:41 +08001814 * acpi_ata_match - see if an acpi object is an ATA device
1815 *
1816 * If an acpi object has one of the ACPI ATA methods defined,
1817 * then we can safely call it an ATA device.
1818 */
1819bool acpi_ata_match(acpi_handle handle)
1820{
1821 return acpi_has_method(handle, "_GTF") ||
1822 acpi_has_method(handle, "_GTM") ||
1823 acpi_has_method(handle, "_STM") ||
1824 acpi_has_method(handle, "_SDD");
1825}
1826
1827/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001828 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001829 *
1830 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1831 * then we can safely call it an ejectable drive bay
1832 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001833bool acpi_bay_match(acpi_handle handle)
Toshi Kanid4e1a692013-03-04 21:30:41 +00001834{
Zhang Rui54735262007-01-11 02:09:09 -05001835 acpi_handle phandle;
1836
Jiang Liu952c63e2013-06-29 00:24:38 +08001837 if (!acpi_has_method(handle, "_EJ0"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001838 return false;
1839 if (acpi_ata_match(handle))
1840 return true;
1841 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1842 return false;
Zhang Rui54735262007-01-11 02:09:09 -05001843
Jiang Liuebf4df82013-06-29 00:24:41 +08001844 return acpi_ata_match(phandle);
Zhang Rui54735262007-01-11 02:09:09 -05001845}
1846
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001847bool acpi_device_is_battery(struct acpi_device *adev)
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001848{
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001849 struct acpi_hardware_id *hwid;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001850
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001851 list_for_each_entry(hwid, &adev->pnp.ids, list)
1852 if (!strcmp("PNP0C0A", hwid->id))
1853 return true;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001854
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001855 return false;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001856}
1857
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001858static bool is_ejectable_bay(struct acpi_device *adev)
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001859{
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001860 acpi_handle handle = adev->handle;
1861
1862 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001863 return true;
1864
1865 return acpi_bay_match(handle);
1866}
1867
Frank Seidel3620f2f2007-12-07 13:20:34 +01001868/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001869 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001870 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001871bool acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001872{
Jiang Liuebf4df82013-06-29 00:24:41 +08001873 return acpi_has_method(handle, "_DCK");
Frank Seidel3620f2f2007-12-07 13:20:34 +01001874}
1875
Thomas Renninger620e1122010-10-01 10:54:00 +02001876const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001877{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001878 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001879
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001880 if (list_empty(&device->pnp.ids))
1881 return dummy_hid;
1882
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001883 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1884 return hid->id;
1885}
1886EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001887
Toshi Kanid4e1a692013-03-04 21:30:41 +00001888static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001889{
1890 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001891
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001892 id = kmalloc(sizeof(*id), GFP_KERNEL);
1893 if (!id)
1894 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001895
Thomas Meyer581de592011-08-06 11:32:56 +02001896 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001897 if (!id->id) {
1898 kfree(id);
1899 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001900 }
1901
Toshi Kanid4e1a692013-03-04 21:30:41 +00001902 list_add_tail(&id->list, &pnp->ids);
1903 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001904}
1905
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001906/*
1907 * Old IBM workstations have a DSDT bug wherein the SMBus object
1908 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1909 * prefix. Work around this.
1910 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001911static bool acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001912{
Jiang Liuebf4df82013-06-29 00:24:41 +08001913 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1914 struct acpi_buffer path = { sizeof(node_name), node_name };
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001915
1916 if (!dmi_name_in_vendors("IBM"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001917 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001918
1919 /* Look for SMBS object */
Jiang Liuebf4df82013-06-29 00:24:41 +08001920 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1921 strcmp("SMBS", path.pointer))
1922 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001923
1924 /* Does it have the necessary (but misnamed) methods? */
Jiang Liu952c63e2013-06-29 00:24:38 +08001925 if (acpi_has_method(handle, "SBI") &&
1926 acpi_has_method(handle, "SBR") &&
1927 acpi_has_method(handle, "SBW"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001928 return true;
1929
1930 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001931}
1932
Jiang Liuae3caa82014-02-19 14:02:19 +08001933static bool acpi_object_is_system_bus(acpi_handle handle)
1934{
1935 acpi_handle tmp;
1936
1937 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1938 tmp == handle)
1939 return true;
1940 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1941 tmp == handle)
1942 return true;
1943
1944 return false;
1945}
1946
Toshi Kanic0af4172013-03-04 21:30:42 +00001947static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1948 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Len Brown4be44fc2005-08-05 00:44:28 -04001950 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001951 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001952 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001953 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Toshi Kanic0af4172013-03-04 21:30:42 +00001955 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00001957 if (handle == ACPI_ROOT_OBJECT) {
1958 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001959 break;
1960 }
1961
Toshi Kanic0af4172013-03-04 21:30:42 +00001962 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001964 pr_err(PREFIX "%s: Error reading device info\n",
1965 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return;
1967 }
1968
Rafael J. Wysocki549e6842014-05-30 04:26:18 +02001969 if (info->valid & ACPI_VALID_HID) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001970 acpi_add_id(pnp, info->hardware_id.string);
Rafael J. Wysocki549e6842014-05-30 04:26:18 +02001971 pnp->type.platform_id = 1;
1972 }
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001973 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001974 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001975 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00001976 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001979 pnp->bus_address = info->address;
1980 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
Lv Zhengccf78042012-10-30 14:41:07 +01001982 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001983 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01001984 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001985
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001986 kfree(info);
1987
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001988 /*
1989 * Some devices don't reliably have _HIDs & _CIDs, so add
1990 * synthetic HIDs to make sure drivers can find them.
1991 */
Toshi Kanic0af4172013-03-04 21:30:42 +00001992 if (acpi_is_video_device(handle))
1993 acpi_add_id(pnp, ACPI_VIDEO_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001994 else if (acpi_bay_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001995 acpi_add_id(pnp, ACPI_BAY_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001996 else if (acpi_dock_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001997 acpi_add_id(pnp, ACPI_DOCK_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001998 else if (acpi_ibm_smbus_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001999 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
Jiang Liuae3caa82014-02-19 14:02:19 +08002000 else if (list_empty(&pnp->ids) &&
2001 acpi_object_is_system_bus(handle)) {
2002 /* \_SB, \_TZ, LNXSYBUS */
2003 acpi_add_id(pnp, ACPI_BUS_HID);
Toshi Kanic0af4172013-03-04 21:30:42 +00002004 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
2005 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06002006 }
Zhang Rui54735262007-01-11 02:09:09 -05002007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 break;
2009 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00002010 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 break;
2012 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00002013 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00002016 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 break;
2018 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00002019 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 break;
2021 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00002022 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 break;
2024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
Toshi Kanic0af4172013-03-04 21:30:42 +00002027void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
2028{
2029 struct acpi_hardware_id *id, *tmp;
2030
2031 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
2032 kfree(id->id);
2033 kfree(id);
2034 }
2035 kfree(pnp->unique_id);
2036}
2037
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01002038void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
2039 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002041 INIT_LIST_HEAD(&device->pnp.ids);
2042 device->device_type = type;
2043 device->handle = handle;
2044 device->parent = acpi_bus_get_parent(handle);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01002045 device->fwnode.type = FWNODE_ACPI;
Rafael J. Wysocki25db1152013-11-22 21:56:06 +01002046 acpi_set_device_status(device, sta);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002047 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00002048 acpi_set_pnp_ids(handle, &device->pnp, type);
Mika Westerbergffdcd952014-10-21 13:33:55 +02002049 acpi_init_properties(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002050 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002051 device->flags.match_driver = false;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002052 device->flags.initialized = true;
2053 device->flags.visited = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01002054 device_initialize(&device->dev);
2055 dev_set_uevent_suppress(&device->dev, true);
2056}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00002057
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01002058void acpi_device_add_finalize(struct acpi_device *device)
2059{
2060 dev_set_uevent_suppress(&device->dev, false);
2061 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002064static int acpi_add_single_object(struct acpi_device **child,
2065 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002066 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00002068 int result;
2069 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00002070 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Burman Yan36bcbec2006-12-19 12:56:11 -08002072 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04002074 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04002075 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002078 acpi_init_device_object(device, handle, type, sta);
2079 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01002080 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01002082 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002083 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00002084 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002085 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002088 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01002089 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002090 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
2091 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
2092 dev_name(&device->dev), (char *) buffer.pointer,
2093 device->parent ? dev_name(&device->parent->dev) : "(null)"));
2094 kfree(buffer.pointer);
2095 *child = device;
2096 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01002097}
2098
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00002099static int acpi_bus_type_and_status(acpi_handle handle, int *type,
2100 unsigned long long *sta)
2101{
2102 acpi_status status;
2103 acpi_object_type acpi_type;
2104
2105 status = acpi_get_type(handle, &acpi_type);
2106 if (ACPI_FAILURE(status))
2107 return -ENODEV;
2108
2109 switch (acpi_type) {
2110 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
2111 case ACPI_TYPE_DEVICE:
2112 *type = ACPI_BUS_TYPE_DEVICE;
2113 status = acpi_bus_get_status_handle(handle, sta);
2114 if (ACPI_FAILURE(status))
2115 return -ENODEV;
2116 break;
2117 case ACPI_TYPE_PROCESSOR:
2118 *type = ACPI_BUS_TYPE_PROCESSOR;
2119 status = acpi_bus_get_status_handle(handle, sta);
2120 if (ACPI_FAILURE(status))
2121 return -ENODEV;
2122 break;
2123 case ACPI_TYPE_THERMAL:
2124 *type = ACPI_BUS_TYPE_THERMAL;
2125 *sta = ACPI_STA_DEFAULT;
2126 break;
2127 case ACPI_TYPE_POWER:
2128 *type = ACPI_BUS_TYPE_POWER;
2129 *sta = ACPI_STA_DEFAULT;
2130 break;
2131 default:
2132 return -ENODEV;
2133 }
2134
2135 return 0;
2136}
2137
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002138bool acpi_device_is_present(struct acpi_device *adev)
2139{
2140 if (adev->status.present || adev->status.functional)
2141 return true;
2142
2143 adev->flags.initialized = false;
2144 return false;
2145}
2146
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01002147static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
2148 char *idstr,
2149 const struct acpi_device_id **matchid)
2150{
2151 const struct acpi_device_id *devid;
2152
Rafael J. Wysockiaca0a4e2014-05-30 04:21:52 +02002153 if (handler->match)
2154 return handler->match(idstr, matchid);
2155
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01002156 for (devid = handler->ids; devid->id[0]; devid++)
2157 if (!strcmp((char *)devid->id, idstr)) {
2158 if (matchid)
2159 *matchid = devid;
2160
2161 return true;
2162 }
2163
2164 return false;
2165}
2166
Toshi Kani6b772e8f2013-03-04 21:30:43 +00002167static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
2168 const struct acpi_device_id **matchid)
2169{
2170 struct acpi_scan_handler *handler;
2171
2172 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
2173 if (acpi_scan_handler_matching(handler, idstr, matchid))
2174 return handler;
2175
2176 return NULL;
2177}
2178
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01002179void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
2180{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01002181 if (!!hotplug->enabled == !!val)
2182 return;
2183
2184 mutex_lock(&acpi_scan_lock);
2185
2186 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01002187
2188 mutex_unlock(&acpi_scan_lock);
2189}
2190
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002191static void acpi_scan_init_hotplug(struct acpi_device *adev)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002192{
Toshi Kani6b772e8f2013-03-04 21:30:43 +00002193 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002194
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01002195 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01002196 acpi_dock_add(adev);
2197 return;
2198 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002199 list_for_each_entry(hwid, &adev->pnp.ids, list) {
2200 struct acpi_scan_handler *handler;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002201
Toshi Kani6b772e8f2013-03-04 21:30:43 +00002202 handler = acpi_scan_match_handler(hwid->id, NULL);
Rafael J. Wysocki1a699472014-02-06 13:58:13 +01002203 if (handler) {
2204 adev->flags.hotplug_notify = true;
2205 break;
2206 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002207 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01002208}
2209
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08002210static void acpi_device_dep_initialize(struct acpi_device *adev)
2211{
2212 struct acpi_dep_data *dep;
2213 struct acpi_handle_list dep_devices;
2214 acpi_status status;
2215 int i;
2216
2217 if (!acpi_has_method(adev->handle, "_DEP"))
2218 return;
2219
2220 status = acpi_evaluate_reference(adev->handle, "_DEP", NULL,
2221 &dep_devices);
2222 if (ACPI_FAILURE(status)) {
Rafael J. Wysocki80167a22014-12-12 22:48:44 +01002223 dev_dbg(&adev->dev, "Failed to evaluate _DEP.\n");
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08002224 return;
2225 }
2226
2227 for (i = 0; i < dep_devices.count; i++) {
2228 struct acpi_device_info *info;
2229 int skip;
2230
2231 status = acpi_get_object_info(dep_devices.handles[i], &info);
2232 if (ACPI_FAILURE(status)) {
Rafael J. Wysocki80167a22014-12-12 22:48:44 +01002233 dev_dbg(&adev->dev, "Error reading _DEP device info\n");
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08002234 continue;
2235 }
2236
2237 /*
2238 * Skip the dependency of Windows System Power
2239 * Management Controller
2240 */
2241 skip = info->valid & ACPI_VALID_HID &&
2242 !strcmp(info->hardware_id.string, "INT3396");
2243
2244 kfree(info);
2245
2246 if (skip)
2247 continue;
2248
2249 dep = kzalloc(sizeof(struct acpi_dep_data), GFP_KERNEL);
2250 if (!dep)
2251 return;
2252
2253 dep->master = dep_devices.handles[i];
2254 dep->slave = adev->handle;
2255 adev->dep_unmet++;
2256
2257 mutex_lock(&acpi_dep_list_lock);
2258 list_add_tail(&dep->node , &acpi_dep_list);
2259 mutex_unlock(&acpi_dep_list_lock);
2260 }
2261}
2262
Rafael J. Wysockie3863092012-12-21 00:36:47 +01002263static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
2264 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01002266 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00002267 int type;
2268 unsigned long long sta;
2269 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002270
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01002271 acpi_bus_get_device(handle, &device);
2272 if (device)
2273 goto out;
2274
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00002275 result = acpi_bus_type_and_status(handle, &type, &sta);
2276 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002277 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01002279 if (type == ACPI_BUS_TYPE_POWER) {
2280 acpi_add_power_resource(handle);
2281 return AE_OK;
2282 }
2283
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002284 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00002285 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002286 return AE_CTRL_DEPTH;
2287
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002288 acpi_scan_init_hotplug(device);
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08002289 acpi_device_dep_initialize(device);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002290
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01002291 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002292 if (!*return_value)
2293 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01002294
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002295 return AE_OK;
2296}
2297
Zhang Rui48459342014-05-30 14:35:34 +02002298static int acpi_check_spi_i2c_slave(struct acpi_resource *ares, void *data)
2299{
2300 bool *is_spi_i2c_slave_p = data;
2301
2302 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
2303 return 1;
2304
2305 /*
2306 * devices that are connected to UART still need to be enumerated to
2307 * platform bus
2308 */
2309 if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
2310 *is_spi_i2c_slave_p = true;
2311
2312 /* no need to do more checking */
2313 return -1;
2314}
2315
2316static void acpi_default_enumeration(struct acpi_device *device)
2317{
2318 struct list_head resource_list;
2319 bool is_spi_i2c_slave = false;
2320
2321 if (!device->pnp.type.platform_id || device->handler)
2322 return;
2323
2324 /*
2325 * Do not enemerate SPI/I2C slaves as they will be enuerated by their
2326 * respective parents.
2327 */
2328 INIT_LIST_HEAD(&resource_list);
2329 acpi_dev_get_resources(device, &resource_list, acpi_check_spi_i2c_slave,
2330 &is_spi_i2c_slave);
2331 acpi_dev_free_resource_list(&resource_list);
2332 if (!is_spi_i2c_slave)
2333 acpi_create_platform_device(device);
2334}
2335
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002336static int acpi_scan_attach_handler(struct acpi_device *device)
2337{
2338 struct acpi_hardware_id *hwid;
2339 int ret = 0;
2340
2341 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002342 const struct acpi_device_id *devid;
2343 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002344
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002345 handler = acpi_scan_match_handler(hwid->id, &devid);
2346 if (handler) {
Rafael J. Wysockid34afa92014-05-30 04:27:31 +02002347 if (!handler->attach) {
2348 device->pnp.type.platform_id = 0;
2349 continue;
2350 }
Rafael J. Wysocki9cb32ac2014-02-11 00:35:46 +01002351 device->handler = handler;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002352 ret = handler->attach(device, devid);
Rafael J. Wysocki9cb32ac2014-02-11 00:35:46 +01002353 if (ret > 0)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002354 break;
Rafael J. Wysocki9cb32ac2014-02-11 00:35:46 +01002355
2356 device->handler = NULL;
2357 if (ret < 0)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002358 break;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002359 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002360 }
Zhang Rui48459342014-05-30 14:35:34 +02002361 if (!ret)
2362 acpi_default_enumeration(device);
2363
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002364 return ret;
2365}
2366
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002367static void acpi_bus_attach(struct acpi_device *device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002368{
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002369 struct acpi_device *child;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01002370 acpi_handle ejd;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002371 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002372
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01002373 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
2374 register_dock_dependent_device(device, ejd);
2375
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002376 acpi_bus_get_status(device);
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002377 /* Skip devices that are not present. */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002378 if (!acpi_device_is_present(device)) {
2379 device->flags.visited = false;
Rafael J. Wysocki1b1f3e12015-01-01 23:38:28 +01002380 device->flags.power_manageable = 0;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002381 return;
2382 }
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02002383 if (device->handler)
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002384 goto ok;
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02002385
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002386 if (!device->flags.initialized) {
Rafael J. Wysocki1b1f3e12015-01-01 23:38:28 +01002387 device->flags.power_manageable =
2388 device->power.states[ACPI_STATE_D0].flags.valid;
2389 if (acpi_bus_init_power(device))
2390 device->flags.power_manageable = 0;
2391
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002392 device->flags.initialized = true;
2393 }
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002394 device->flags.visited = false;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002395 ret = acpi_scan_attach_handler(device);
Rafael J. Wysocki69310072013-11-07 01:41:01 +01002396 if (ret < 0)
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002397 return;
Rafael J. Wysocki69310072013-11-07 01:41:01 +01002398
2399 device->flags.match_driver = true;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002400 if (!ret) {
2401 ret = device_attach(&device->dev);
2402 if (ret < 0)
2403 return;
2404 }
2405 device->flags.visited = true;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002406
2407 ok:
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002408 list_for_each_entry(child, &device->children, node)
2409 acpi_bus_attach(child);
Rafael J. Wysocki8ab17fc2014-09-21 02:58:18 +02002410
2411 if (device->handler && device->handler->hotplug.notify_online)
2412 device->handler->hotplug.notify_online(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08002415void acpi_walk_dep_device_list(acpi_handle handle)
2416{
2417 struct acpi_dep_data *dep, *tmp;
2418 struct acpi_device *adev;
2419
2420 mutex_lock(&acpi_dep_list_lock);
2421 list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2422 if (dep->master == handle) {
2423 acpi_bus_get_device(dep->slave, &adev);
2424 if (!adev)
2425 continue;
2426
2427 adev->dep_unmet--;
2428 if (!adev->dep_unmet)
2429 acpi_bus_attach(adev);
2430 list_del(&dep->node);
2431 kfree(dep);
2432 }
2433 }
2434 mutex_unlock(&acpi_dep_list_lock);
2435}
2436EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
2437
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002438/**
2439 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2440 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01002441 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002442 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2443 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01002444 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002445 * If no devices were found, -ENODEV is returned, but it does not mean that
2446 * there has been a real error. There just have been no suitable ACPI objects
2447 * in the table trunk from which the kernel could create a device and add an
2448 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002449 *
2450 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01002451 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002452int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07002453{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 void *device = NULL;
Rajesh Shah3fb02732005-04-28 00:25:52 -07002455
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002456 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01002458 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002459
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002460 if (device) {
2461 acpi_bus_attach(device);
2462 return 0;
2463 }
2464 return -ENODEV;
Rajesh Shah3fb02732005-04-28 00:25:52 -07002465}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002466EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002468/**
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002469 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2470 * @adev: Root of the ACPI namespace scope to walk.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002471 *
2472 * Must be called under acpi_scan_lock.
2473 */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002474void acpi_bus_trim(struct acpi_device *adev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002476 struct acpi_scan_handler *handler = adev->handler;
2477 struct acpi_device *child;
2478
2479 list_for_each_entry_reverse(child, &adev->children, node)
2480 acpi_bus_trim(child);
2481
Rafael J. Wysockia951c772014-01-27 23:08:09 +01002482 adev->flags.match_driver = false;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002483 if (handler) {
2484 if (handler->detach)
2485 handler->detach(adev);
2486
2487 adev->handler = NULL;
2488 } else {
2489 device_release_driver(&adev->dev);
2490 }
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002491 /*
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002492 * Most likely, the device is going away, so put it into D3cold before
2493 * that.
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002494 */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002495 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2496 adev->flags.initialized = false;
2497 adev->flags.visited = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498}
Kristen Accardiceaba662006-02-23 17:56:01 -08002499EXPORT_SYMBOL_GPL(acpi_bus_trim);
2500
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00002501static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502{
Len Brown4be44fc2005-08-05 00:44:28 -04002503 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08002504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 /*
2506 * Enumerate all fixed-feature devices.
2507 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002508 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2509 struct acpi_device *device = NULL;
2510
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002511 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002512 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002513 ACPI_STA_DEFAULT);
2514 if (result)
2515 return result;
2516
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002517 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002518 result = device_attach(&device->dev);
2519 if (result < 0)
2520 return result;
2521
Daniel Drakec10d7a12012-05-10 00:08:43 +01002522 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002525 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2526 struct acpi_device *device = NULL;
2527
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002528 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002529 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002530 ACPI_STA_DEFAULT);
2531 if (result)
2532 return result;
2533
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002534 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002535 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002538 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
2540
Bjorn Helgaase747f272009-03-24 16:49:43 -06002541int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
2543 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Patrick Mochel5b327262006-05-10 10:33:00 -04002545 result = bus_register(&acpi_bus_type);
2546 if (result) {
2547 /* We don't want to quit even if we failed to add suspend/resume */
2548 printk(KERN_ERR PREFIX "Could not register bus type\n");
2549 }
2550
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01002551 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01002552 acpi_pci_link_init();
Rafael J. Wysockiac212b62013-05-03 00:26:22 +02002553 acpi_processor_init();
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01002554 acpi_lpss_init();
Ken Xue92082a82015-02-06 08:27:51 +08002555 acpi_apd_init();
Lan Tianyu2fa97fe2013-06-05 02:27:50 +00002556 acpi_cmos_rtc_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01002557 acpi_container_init();
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01002558 acpi_memory_hotplug_init();
Zhang Ruieec15ed2014-05-30 04:23:01 +02002559 acpi_pnp_init();
Zhang Rui3230bbf2014-03-14 00:34:05 +08002560 acpi_int340x_thermal_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01002561
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002562 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 * Enumerate devices in the ACPI namespace.
2565 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002566 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002568 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002570 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002572 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Aaron Lu2650ef42014-04-28 10:38:04 +08002574 /* Fixed feature devices do not exist on HW-reduced platform */
2575 if (!acpi_gbl_reduced_hardware) {
2576 result = acpi_bus_scan_fixed();
2577 if (result) {
2578 acpi_detach_data(acpi_root->handle,
2579 acpi_scan_drop_device);
2580 acpi_device_del(acpi_root);
2581 put_device(&acpi_root->dev);
2582 goto out;
2583 }
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002584 }
2585
2586 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002587
2588 out:
2589 mutex_unlock(&acpi_scan_lock);
2590 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}