blob: 0446d0d5efa5ef6296b5cd3154dc2f6bb2d85acc [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
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080039static LIST_HEAD(acpi_bus_id_list);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +010040static DEFINE_MUTEX(acpi_scan_lock);
Rafael J. Wysockica589f92013-01-30 14:27:29 +010041static LIST_HEAD(acpi_scan_handlers_list);
Shaohua Li90905892009-04-07 10:24:29 +080042DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043LIST_HEAD(acpi_wakeup_device_list);
Rafael J. Wysockie5255062014-02-04 00:43:17 +010044static DEFINE_MUTEX(acpi_hp_context_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080046struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080047 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080048 unsigned int instance_no;
49 struct list_head node;
50};
Thomas Renninger29b71a12007-07-23 14:43:51 +020051
Rafael J. Wysocki3757b942013-02-13 14:36:47 +010052void acpi_scan_lock_acquire(void)
53{
54 mutex_lock(&acpi_scan_lock);
55}
56EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
57
58void acpi_scan_lock_release(void)
59{
60 mutex_unlock(&acpi_scan_lock);
61}
62EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
63
Rafael J. Wysockie5255062014-02-04 00:43:17 +010064void acpi_lock_hp_context(void)
65{
66 mutex_lock(&acpi_hp_context_lock);
67}
68
69void acpi_unlock_hp_context(void)
70{
71 mutex_unlock(&acpi_hp_context_lock);
72}
73
Rafael J. Wysocki5d513202014-02-22 00:48:31 +010074void acpi_initialize_hp_context(struct acpi_device *adev,
75 struct acpi_hotplug_context *hp,
76 int (*notify)(struct acpi_device *, u32),
77 void (*uevent)(struct acpi_device *, u32))
78{
79 acpi_lock_hp_context();
Rafael J. Wysockiba574dc2014-07-15 22:03:22 +020080 hp->notify = notify;
81 hp->uevent = uevent;
82 acpi_set_hp_context(adev, hp);
Rafael J. Wysocki5d513202014-02-22 00:48:31 +010083 acpi_unlock_hp_context();
84}
85EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
86
Rafael J. Wysockica589f92013-01-30 14:27:29 +010087int acpi_scan_add_handler(struct acpi_scan_handler *handler)
88{
Rafael J. Wysockid34afa92014-05-30 04:27:31 +020089 if (!handler)
Rafael J. Wysockica589f92013-01-30 14:27:29 +010090 return -EINVAL;
91
92 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
93 return 0;
94}
95
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +010096int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
97 const char *hotplug_profile_name)
98{
99 int error;
100
101 error = acpi_scan_add_handler(handler);
102 if (error)
103 return error;
104
105 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
106 return 0;
107}
108
Thomas Renninger29b71a12007-07-23 14:43:51 +0200109/*
110 * Creates hid/cid(s) string needed for modalias and uevent
111 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
112 * char *modalias: "acpi:IBM0001:ACPI0001"
Zhang Rui3d8e0092014-01-14 16:46:35 +0800113 * Return: 0: no _HID and no _CID
114 * -EINVAL: output error
115 * -ENOMEM: output is truncated
Thomas Renninger29b71a12007-07-23 14:43:51 +0200116*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +0200117static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
118 int size)
119{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200120 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800121 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600122 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200123
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200124 if (list_empty(&acpi_dev->pnp.ids))
125 return 0;
126
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800127 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +0200128 size -= len;
129
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600130 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
131 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui3d8e0092014-01-14 16:46:35 +0800132 if (count < 0)
Mika Westerberg98d28d02014-09-12 11:33:10 +0300133 return -EINVAL;
Zhang Rui3d8e0092014-01-14 16:46:35 +0800134 if (count >= size)
135 return -ENOMEM;
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800136 len += count;
137 size -= count;
138 }
139
Thomas Renninger29b71a12007-07-23 14:43:51 +0200140 modalias[len] = '\0';
141 return len;
142}
143
Zhang Rui6eb24512014-01-14 16:46:36 +0800144/*
Mika Westerberg52870782014-10-24 16:40:54 +0200145 * acpi_companion_match() - Can we match via ACPI companion device
146 * @dev: Device in question
147 *
148 * Check if the given device has an ACPI companion and if that companion has
149 * a valid list of PNP IDs, and if the device is the first (primary) physical
150 * device associated with it.
151 *
152 * If multiple physical devices are attached to a single ACPI companion, we need
153 * to be careful. The usage scenario for this kind of relationship is that all
154 * of the physical devices in question use resources provided by the ACPI
155 * companion. A typical case is an MFD device where all the sub-devices share
156 * the parent's ACPI companion. In such cases we can only allow the primary
157 * (first) physical device to be matched with the help of the companion's PNP
158 * IDs.
159 *
160 * Additional physical devices sharing the ACPI companion can still use
161 * resources available from it but they will be matched normally using functions
162 * provided by their bus types (and analogously for their modalias).
163 */
164static bool acpi_companion_match(const struct device *dev)
165{
166 struct acpi_device *adev;
167 bool ret;
168
169 adev = ACPI_COMPANION(dev);
170 if (!adev)
171 return false;
172
173 if (list_empty(&adev->pnp.ids))
174 return false;
175
176 mutex_lock(&adev->physical_node_lock);
177 if (list_empty(&adev->physical_node_list)) {
178 ret = false;
179 } else {
180 const struct acpi_device_physical_node *node;
181
182 node = list_first_entry(&adev->physical_node_list,
183 struct acpi_device_physical_node, node);
184 ret = node->dev == dev;
185 }
186 mutex_unlock(&adev->physical_node_lock);
187
188 return ret;
189}
190
191/*
Zhang Rui6eb24512014-01-14 16:46:36 +0800192 * Creates uevent modalias field for ACPI enumerated devices.
193 * Because the other buses does not support ACPI HIDs & CIDs.
194 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
195 * "acpi:IBM0001:ACPI0001"
196 */
197int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
198{
Zhang Rui6eb24512014-01-14 16:46:36 +0800199 int len;
200
Mika Westerberg52870782014-10-24 16:40:54 +0200201 if (!acpi_companion_match(dev))
Zhang Rui6eb24512014-01-14 16:46:36 +0800202 return -ENODEV;
203
204 if (add_uevent_var(env, "MODALIAS="))
205 return -ENOMEM;
Mika Westerberg52870782014-10-24 16:40:54 +0200206 len = create_modalias(ACPI_COMPANION(dev), &env->buf[env->buflen - 1],
Zhang Rui6eb24512014-01-14 16:46:36 +0800207 sizeof(env->buf) - env->buflen);
208 if (len <= 0)
209 return len;
210 env->buflen += len;
211 return 0;
212}
213EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
214
215/*
216 * Creates modalias sysfs attribute for ACPI enumerated devices.
217 * Because the other buses does not support ACPI HIDs & CIDs.
218 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
219 * "acpi:IBM0001:ACPI0001"
220 */
221int acpi_device_modalias(struct device *dev, char *buf, int size)
222{
Zhang Rui6eb24512014-01-14 16:46:36 +0800223 int len;
224
Mika Westerberg52870782014-10-24 16:40:54 +0200225 if (!acpi_companion_match(dev))
Zhang Rui6eb24512014-01-14 16:46:36 +0800226 return -ENODEV;
227
Mika Westerberg52870782014-10-24 16:40:54 +0200228 len = create_modalias(ACPI_COMPANION(dev), buf, size -1);
Zhang Rui6eb24512014-01-14 16:46:36 +0800229 if (len <= 0)
230 return len;
231 buf[len++] = '\n';
232 return len;
233}
234EXPORT_SYMBOL_GPL(acpi_device_modalias);
235
Thomas Renninger29b71a12007-07-23 14:43:51 +0200236static ssize_t
237acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
238 struct acpi_device *acpi_dev = to_acpi_device(dev);
239 int len;
240
Thomas Renninger29b71a12007-07-23 14:43:51 +0200241 len = create_modalias(acpi_dev, buf, 1024);
242 if (len <= 0)
Zhang Rui3d8e0092014-01-14 16:46:35 +0800243 return len;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200244 buf[len++] = '\n';
245 return len;
246}
247static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
248
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100249bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100250{
251 struct acpi_device_physical_node *pn;
252 bool offline = true;
253
Rafael J. Wysockifc69ac42015-04-18 01:25:46 +0200254 /*
255 * acpi_container_offline() calls this for all of the container's
256 * children under the container's physical_node_lock lock.
257 */
258 mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100259
260 list_for_each_entry(pn, &adev->physical_node_list, node)
261 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100262 if (uevent)
263 kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
264
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100265 offline = false;
266 break;
267 }
268
269 mutex_unlock(&adev->physical_node_lock);
270 return offline;
271}
272
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100273static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
274 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200275{
276 struct acpi_device *device = NULL;
277 struct acpi_device_physical_node *pn;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200278 bool second_pass = (bool)data;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200279 acpi_status status = AE_OK;
280
281 if (acpi_bus_get_device(handle, &device))
282 return AE_OK;
283
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100284 if (device->handler && !device->handler->hotplug.enabled) {
285 *ret_p = &device->dev;
286 return AE_SUPPORT;
287 }
288
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200289 mutex_lock(&device->physical_node_lock);
290
291 list_for_each_entry(pn, &device->physical_node_list, node) {
292 int ret;
293
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200294 if (second_pass) {
295 /* Skip devices offlined by the first pass. */
296 if (pn->put_online)
297 continue;
298 } else {
299 pn->put_online = false;
300 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200301 ret = device_offline(pn->dev);
302 if (acpi_force_hot_remove)
303 continue;
304
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200305 if (ret >= 0) {
306 pn->put_online = !ret;
307 } else {
308 *ret_p = pn->dev;
309 if (second_pass) {
310 status = AE_ERROR;
311 break;
312 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200313 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200314 }
315
316 mutex_unlock(&device->physical_node_lock);
317
318 return status;
319}
320
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100321static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
322 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200323{
324 struct acpi_device *device = NULL;
325 struct acpi_device_physical_node *pn;
326
327 if (acpi_bus_get_device(handle, &device))
328 return AE_OK;
329
330 mutex_lock(&device->physical_node_lock);
331
332 list_for_each_entry(pn, &device->physical_node_list, node)
333 if (pn->put_online) {
334 device_online(pn->dev);
335 pn->put_online = false;
336 }
337
338 mutex_unlock(&device->physical_node_lock);
339
340 return AE_OK;
341}
342
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100343static int acpi_scan_try_to_offline(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Yinghai Lu5993c462013-01-11 22:40:41 +0000345 acpi_handle handle = device->handle;
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100346 struct device *errdev = NULL;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100347 acpi_status status;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100348
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200349 /*
350 * Carry out two passes here and ignore errors in the first pass,
351 * because if the devices in question are memory blocks and
352 * CONFIG_MEMCG is set, one of the blocks may hold data structures
353 * that the other blocks depend on, but it is not known in advance which
354 * block holds them.
355 *
356 * If the first pass is successful, the second one isn't needed, though.
357 */
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100358 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
359 NULL, acpi_bus_offline, (void *)false,
360 (void **)&errdev);
361 if (status == AE_SUPPORT) {
362 dev_warn(errdev, "Offline disabled.\n");
363 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
364 acpi_bus_online, NULL, NULL, NULL);
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100365 return -EPERM;
366 }
367 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200368 if (errdev) {
369 errdev = NULL;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200370 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100371 NULL, acpi_bus_offline, (void *)true,
372 (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200373 if (!errdev || acpi_force_hot_remove)
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100374 acpi_bus_offline(handle, 0, (void *)true,
375 (void **)&errdev);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200376
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200377 if (errdev && !acpi_force_hot_remove) {
378 dev_warn(errdev, "Offline failed.\n");
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100379 acpi_bus_online(handle, 0, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200380 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100381 ACPI_UINT32_MAX, acpi_bus_online,
382 NULL, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200383 return -EBUSY;
384 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200385 }
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100386 return 0;
387}
388
389static int acpi_scan_hot_remove(struct acpi_device *device)
390{
391 acpi_handle handle = device->handle;
392 unsigned long long sta;
393 acpi_status status;
394
Tang Chendee15922014-08-08 10:30:45 +0800395 if (device->handler && device->handler->hotplug.demand_offline
396 && !acpi_force_hot_remove) {
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100397 if (!acpi_scan_is_offline(device, true))
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100398 return -EBUSY;
399 } else {
400 int error = acpi_scan_try_to_offline(device);
401 if (error)
402 return error;
403 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200404
Zhang Rui26d46862008-04-29 02:35:48 -0400405 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100406 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400407
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100408 acpi_bus_trim(device);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200409
Jiang Liu7d2421f2013-06-29 00:24:40 +0800410 acpi_evaluate_lck(handle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /*
412 * TBD: _EJD support.
413 */
Jiang Liu7d2421f2013-06-29 00:24:40 +0800414 status = acpi_evaluate_ej0(handle);
415 if (status == AE_NOT_FOUND)
416 return -ENODEV;
417 else if (ACPI_FAILURE(status))
418 return -EIO;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100419
Toshi Kani882fd122013-03-13 19:29:26 +0000420 /*
421 * Verify if eject was indeed successful. If not, log an error
422 * message. No need to call _OST since _EJ0 call was made OK.
423 */
424 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
425 if (ACPI_FAILURE(status)) {
426 acpi_handle_warn(handle,
427 "Status check after eject failed (0x%x)\n", status);
428 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
429 acpi_handle_warn(handle,
430 "Eject incomplete - status 0x%llx\n", sta);
431 }
432
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100433 return 0;
434}
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100435
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100436static int acpi_scan_device_not_present(struct acpi_device *adev)
437{
438 if (!acpi_device_enumerated(adev)) {
439 dev_warn(&adev->dev, "Still not present\n");
440 return -EALREADY;
441 }
442 acpi_bus_trim(adev);
443 return 0;
444}
445
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100446static int acpi_scan_device_check(struct acpi_device *adev)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100447{
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200448 int error;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100449
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100450 acpi_bus_get_status(adev);
451 if (adev->status.present || adev->status.functional) {
452 /*
453 * This function is only called for device objects for which
454 * matching scan handlers exist. The only situation in which
455 * the scan handler is not attached to this device object yet
456 * is when the device has just appeared (either it wasn't
457 * present at all before or it was removed and then added
458 * again).
459 */
460 if (adev->handler) {
461 dev_warn(&adev->dev, "Already enumerated\n");
462 return -EALREADY;
463 }
464 error = acpi_bus_scan(adev->handle);
465 if (error) {
466 dev_warn(&adev->dev, "Namespace scan failure\n");
467 return error;
468 }
469 if (!adev->handler) {
470 dev_warn(&adev->dev, "Enumeration failure\n");
471 error = -ENODEV;
472 }
473 } else {
474 error = acpi_scan_device_not_present(adev);
475 }
476 return error;
477}
478
479static int acpi_scan_bus_check(struct acpi_device *adev)
480{
481 struct acpi_scan_handler *handler = adev->handler;
482 struct acpi_device *child;
483 int error;
484
485 acpi_bus_get_status(adev);
486 if (!(adev->status.present || adev->status.functional)) {
487 acpi_scan_device_not_present(adev);
488 return 0;
489 }
490 if (handler && handler->hotplug.scan_dependent)
491 return handler->hotplug.scan_dependent(adev);
492
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100493 error = acpi_bus_scan(adev->handle);
494 if (error) {
495 dev_warn(&adev->dev, "Namespace scan failure\n");
496 return error;
497 }
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100498 list_for_each_entry(child, &adev->children, node) {
499 error = acpi_scan_bus_check(child);
500 if (error)
501 return error;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100502 }
503 return 0;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100504}
505
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100506static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
507{
508 switch (type) {
509 case ACPI_NOTIFY_BUS_CHECK:
510 return acpi_scan_bus_check(adev);
511 case ACPI_NOTIFY_DEVICE_CHECK:
512 return acpi_scan_device_check(adev);
513 case ACPI_NOTIFY_EJECT_REQUEST:
514 case ACPI_OST_EC_OSPM_EJECT:
Rafael J. Wysockidd2151b2014-02-04 00:44:02 +0100515 if (adev->handler && !adev->handler->hotplug.enabled) {
516 dev_info(&adev->dev, "Eject disabled\n");
517 return -EPERM;
518 }
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100519 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
520 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100521 return acpi_scan_hot_remove(adev);
522 }
523 return -EINVAL;
524}
525
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +0100526void acpi_device_hotplug(struct acpi_device *adev, u32 src)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100527{
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100528 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100529 int error = -ENODEV;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100530
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200531 lock_device_hotplug();
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200532 mutex_lock(&acpi_scan_lock);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100533
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100534 /*
535 * The device object's ACPI handle cannot become invalid as long as we
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100536 * are holding acpi_scan_lock, but it might have become invalid before
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100537 * that lock was acquired.
538 */
539 if (adev->handle == INVALID_ACPI_HANDLE)
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100540 goto err_out;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100541
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100542 if (adev->flags.is_dock_station) {
543 error = dock_notify(adev, src);
544 } else if (adev->flags.hotplug_notify) {
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100545 error = acpi_generic_hotplug_event(adev, src);
Rafael J. Wysockidd2151b2014-02-04 00:44:02 +0100546 if (error == -EPERM) {
547 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
548 goto err_out;
549 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100550 } else {
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100551 int (*notify)(struct acpi_device *, u32);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100552
553 acpi_lock_hp_context();
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100554 notify = adev->hp ? adev->hp->notify : NULL;
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100555 acpi_unlock_hp_context();
556 /*
557 * There may be additional notify handlers for device objects
558 * without the .event() callback, so ignore them here.
559 */
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100560 if (notify)
561 error = notify(adev, src);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100562 else
563 goto out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100564 }
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100565 if (!error)
566 ost_code = ACPI_OST_SC_SUCCESS;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100567
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100568 err_out:
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100569 acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100570
571 out:
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +0100572 acpi_bus_put_acpi_device(adev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100573 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200574 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100575}
576
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100577static ssize_t real_power_state_show(struct device *dev,
578 struct device_attribute *attr, char *buf)
579{
580 struct acpi_device *adev = to_acpi_device(dev);
581 int state;
582 int ret;
583
584 ret = acpi_device_get_power(adev, &state);
585 if (ret)
586 return ret;
587
588 return sprintf(buf, "%s\n", acpi_power_state_string(state));
589}
590
591static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
592
593static ssize_t power_state_show(struct device *dev,
594 struct device_attribute *attr, char *buf)
595{
596 struct acpi_device *adev = to_acpi_device(dev);
597
598 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
599}
600
601static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800604acpi_eject_store(struct device *d, struct device_attribute *attr,
605 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800607 struct acpi_device *acpi_device = to_acpi_device(d);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100608 acpi_object_type not_used;
609 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100611 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100614 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
615 && !acpi_device->driver)
616 return -ENODEV;
617
618 status = acpi_get_type(acpi_device->handle, &not_used);
619 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
620 return -ENODEV;
621
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100622 get_device(&acpi_device->dev);
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +0100623 status = acpi_hotplug_schedule(acpi_device, ACPI_OST_EC_OSPM_EJECT);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200624 if (ACPI_SUCCESS(status))
625 return count;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100626
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200627 put_device(&acpi_device->dev);
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100628 acpi_evaluate_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
629 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100630 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800633static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800635static ssize_t
636acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
637 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600639 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800640}
641static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
642
Lv Zheng923d4a42012-10-30 14:43:26 +0100643static ssize_t acpi_device_uid_show(struct device *dev,
644 struct device_attribute *attr, char *buf)
645{
646 struct acpi_device *acpi_dev = to_acpi_device(dev);
647
648 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
649}
650static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
651
652static ssize_t acpi_device_adr_show(struct device *dev,
653 struct device_attribute *attr, char *buf)
654{
655 struct acpi_device *acpi_dev = to_acpi_device(dev);
656
657 return sprintf(buf, "0x%08x\n",
658 (unsigned int)(acpi_dev->pnp.bus_address));
659}
660static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
661
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800662static ssize_t
663acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
664 struct acpi_device *acpi_dev = to_acpi_device(dev);
665 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
666 int result;
667
668 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600669 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800670 goto end;
671
672 result = sprintf(buf, "%s\n", (char*)path.pointer);
673 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600674end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800675 return result;
676}
677static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
678
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600679/* sysfs file that shows description text from the ACPI _STR method */
680static ssize_t description_show(struct device *dev,
681 struct device_attribute *attr,
682 char *buf) {
683 struct acpi_device *acpi_dev = to_acpi_device(dev);
684 int result;
685
686 if (acpi_dev->pnp.str_obj == NULL)
687 return 0;
688
689 /*
690 * The _STR object contains a Unicode identifier for a device.
691 * We need to convert to utf-8 so it can be displayed.
692 */
693 result = utf16s_to_utf8s(
694 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
695 acpi_dev->pnp.str_obj->buffer.length,
696 UTF16_LITTLE_ENDIAN, buf,
697 PAGE_SIZE);
698
699 buf[result++] = '\n';
700
701 return result;
702}
703static DEVICE_ATTR(description, 0444, description_show, NULL);
704
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100705static ssize_t
706acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
707 char *buf) {
708 struct acpi_device *acpi_dev = to_acpi_device(dev);
Yasuaki Ishimatsua383b682014-09-03 13:39:13 +0900709 acpi_status status;
710 unsigned long long sun;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100711
Yasuaki Ishimatsua383b682014-09-03 13:39:13 +0900712 status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun);
713 if (ACPI_FAILURE(status))
714 return -ENODEV;
715
716 return sprintf(buf, "%llu\n", sun);
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100717}
718static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
719
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800720static ssize_t status_show(struct device *dev, struct device_attribute *attr,
721 char *buf) {
722 struct acpi_device *acpi_dev = to_acpi_device(dev);
723 acpi_status status;
724 unsigned long long sta;
725
726 status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
727 if (ACPI_FAILURE(status))
728 return -ENODEV;
729
730 return sprintf(buf, "%llu\n", sta);
731}
732static DEVICE_ATTR_RO(status);
733
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800734static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600736 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800737 acpi_status status;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800738 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800740 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800741 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800742 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600743 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800744 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600745 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800746 goto end;
747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200749 if (!list_empty(&dev->pnp.ids)) {
750 result = device_create_file(&dev->dev, &dev_attr_hid);
751 if (result)
752 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200754 result = device_create_file(&dev->dev, &dev_attr_modalias);
755 if (result)
756 goto end;
757 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200758
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600759 /*
760 * If device has _STR, 'description' file is created
761 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800762 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600763 status = acpi_evaluate_object(dev->handle, "_STR",
764 NULL, &buffer);
765 if (ACPI_FAILURE(status))
766 buffer.pointer = NULL;
767 dev->pnp.str_obj = buffer.pointer;
768 result = device_create_file(&dev->dev, &dev_attr_description);
769 if (result)
770 goto end;
771 }
772
Toshi Kanid4e1a692013-03-04 21:30:41 +0000773 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100774 result = device_create_file(&dev->dev, &dev_attr_adr);
775 if (dev->pnp.unique_id)
776 result = device_create_file(&dev->dev, &dev_attr_uid);
777
Yasuaki Ishimatsua383b682014-09-03 13:39:13 +0900778 if (acpi_has_method(dev->handle, "_SUN")) {
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100779 result = device_create_file(&dev->dev, &dev_attr_sun);
780 if (result)
781 goto end;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100782 }
783
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800784 if (acpi_has_method(dev->handle, "_STA")) {
785 result = device_create_file(&dev->dev, &dev_attr_status);
786 if (result)
787 goto end;
788 }
789
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800790 /*
791 * If device has _EJ0, 'eject' file is created that is used to trigger
792 * hot-removal function from userland.
793 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800794 if (acpi_has_method(dev->handle, "_EJ0")) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800795 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100796 if (result)
797 return result;
798 }
799
800 if (dev->flags.power_manageable) {
801 result = device_create_file(&dev->dev, &dev_attr_power_state);
802 if (result)
803 return result;
804
805 if (dev->power.flags.power_resources)
806 result = device_create_file(&dev->dev,
807 &dev_attr_real_power_state);
808 }
809
Alex Chiang0c526d92009-05-14 08:31:37 -0600810end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800811 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800812}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800814static void acpi_device_remove_files(struct acpi_device *dev)
815{
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100816 if (dev->flags.power_manageable) {
817 device_remove_file(&dev->dev, &dev_attr_power_state);
818 if (dev->power.flags.power_resources)
819 device_remove_file(&dev->dev,
820 &dev_attr_real_power_state);
821 }
822
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800823 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600824 * If device has _STR, remove 'description' file
825 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800826 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600827 kfree(dev->pnp.str_obj);
828 device_remove_file(&dev->dev, &dev_attr_description);
829 }
830 /*
831 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800832 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800833 if (acpi_has_method(dev->handle, "_EJ0"))
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800834 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800835
Jiang Liu952c63e2013-06-29 00:24:38 +0800836 if (acpi_has_method(dev->handle, "_SUN"))
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100837 device_remove_file(&dev->dev, &dev_attr_sun);
838
Lv Zheng923d4a42012-10-30 14:43:26 +0100839 if (dev->pnp.unique_id)
840 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000841 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100842 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600843 device_remove_file(&dev->dev, &dev_attr_modalias);
844 device_remove_file(&dev->dev, &dev_attr_hid);
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800845 if (acpi_has_method(dev->handle, "_STA"))
846 device_remove_file(&dev->dev, &dev_attr_status);
Alex Chiang0c526d92009-05-14 08:31:37 -0600847 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800848 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800849}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800851 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200853
Mika Westerbergcf761af2012-10-31 22:44:41 +0100854static const struct acpi_device_id *__acpi_match_device(
855 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200856{
857 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600858 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200859
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800860 /*
861 * If the device is not present, it is unnecessary to load device
862 * driver for it.
863 */
864 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100865 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800866
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600867 for (id = ids; id->id[0]; id++)
868 list_for_each_entry(hwid, &device->pnp.ids, list)
869 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100870 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200871
Mika Westerbergcf761af2012-10-31 22:44:41 +0100872 return NULL;
873}
874
875/**
876 * acpi_match_device - Match a struct device against a given list of ACPI IDs
877 * @ids: Array of struct acpi_device_id object to match against.
878 * @dev: The device structure to match.
879 *
880 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
881 * object for that handle and use that object to match against a given list of
882 * device IDs.
883 *
884 * Return a pointer to the first matching ID on success or %NULL on failure.
885 */
886const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
887 const struct device *dev)
888{
889 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100890 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100891
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100892 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100893 return NULL;
894
Mika Westerberg52870782014-10-24 16:40:54 +0200895 if (!acpi_companion_match(dev))
896 return NULL;
897
Mika Westerbergcf761af2012-10-31 22:44:41 +0100898 return __acpi_match_device(adev, ids);
899}
900EXPORT_SYMBOL_GPL(acpi_match_device);
901
902int acpi_match_device_ids(struct acpi_device *device,
903 const struct acpi_device_id *ids)
904{
905 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200906}
907EXPORT_SYMBOL(acpi_match_device_ids);
908
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100909static void acpi_free_power_resources_lists(struct acpi_device *device)
910{
911 int i;
912
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100913 if (device->wakeup.flags.valid)
914 acpi_power_resources_list_free(&device->wakeup.resources);
915
Rafael J. Wysocki3dcf1572015-01-01 23:38:28 +0100916 if (!device->power.flags.power_resources)
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100917 return;
918
919 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
920 struct acpi_device_power_state *ps = &device->power.states[i];
921 acpi_power_resources_list_free(&ps->resources);
922 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600923}
924
Patrick Mochel1890a972006-12-07 20:56:31 +0800925static void acpi_device_release(struct device *dev)
926{
927 struct acpi_device *acpi_dev = to_acpi_device(dev);
928
Toshi Kanic0af4172013-03-04 21:30:42 +0000929 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100930 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800931 kfree(acpi_dev);
932}
933
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800934static int acpi_bus_match(struct device *dev, struct device_driver *drv)
935{
936 struct acpi_device *acpi_dev = to_acpi_device(dev);
937 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
938
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100939 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +0100940 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800941}
942
Kay Sievers7eff2e72007-08-14 15:15:12 +0200943static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800944{
945 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200946 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800947
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200948 if (list_empty(&acpi_dev->pnp.ids))
949 return 0;
950
Kay Sievers7eff2e72007-08-14 15:15:12 +0200951 if (add_uevent_var(env, "MODALIAS="))
952 return -ENOMEM;
953 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
954 sizeof(env->buf) - env->buflen);
Zhang Rui3d8e0092014-01-14 16:46:35 +0800955 if (len <= 0)
956 return len;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200957 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return 0;
959}
960
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000961static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
962{
963 struct acpi_device *device = data;
964
965 device->driver->ops.notify(device, event);
966}
967
Lan Tianyu236105d2014-08-26 01:29:24 +0200968static void acpi_device_notify_fixed(void *data)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000969{
970 struct acpi_device *device = data;
971
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000972 /* Fixed hardware devices have no handles */
973 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Lan Tianyu236105d2014-08-26 01:29:24 +0200974}
975
976static acpi_status acpi_device_fixed_event(void *data)
977{
978 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_device_notify_fixed, data);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000979 return AE_OK;
980}
981
982static int acpi_device_install_notify_handler(struct acpi_device *device)
983{
984 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000985
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000986 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000987 status =
988 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +0200989 acpi_device_fixed_event,
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000990 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000991 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000992 status =
993 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +0200994 acpi_device_fixed_event,
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000995 device);
996 else
997 status = acpi_install_notify_handler(device->handle,
998 ACPI_DEVICE_NOTIFY,
999 acpi_device_notify,
1000 device);
1001
1002 if (ACPI_FAILURE(status))
1003 return -EINVAL;
1004 return 0;
1005}
1006
1007static void acpi_device_remove_notify_handler(struct acpi_device *device)
1008{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +00001009 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001010 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +02001011 acpi_device_fixed_event);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +00001012 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001013 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +02001014 acpi_device_fixed_event);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001015 else
1016 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
1017 acpi_device_notify);
1018}
1019
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001020static int acpi_device_probe(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +08001021{
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001022 struct acpi_device *acpi_dev = to_acpi_device(dev);
1023 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
1024 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Rafael J. Wysockifc2e0a82014-08-26 01:29:21 +02001026 if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev))
Rafael J. Wysocki24071f42013-06-16 00:36:41 +02001027 return -EINVAL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001028
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001029 if (!acpi_drv->ops.add)
1030 return -ENOSYS;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001031
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001032 ret = acpi_drv->ops.add(acpi_dev);
1033 if (ret)
1034 return ret;
1035
1036 acpi_dev->driver = acpi_drv;
1037 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1038 "Driver [%s] successfully bound to device [%s]\n",
1039 acpi_drv->name, acpi_dev->pnp.bus_id));
1040
1041 if (acpi_drv->ops.notify) {
1042 ret = acpi_device_install_notify_handler(acpi_dev);
1043 if (ret) {
1044 if (acpi_drv->ops.remove)
1045 acpi_drv->ops.remove(acpi_dev);
1046
1047 acpi_dev->driver = NULL;
1048 acpi_dev->driver_data = NULL;
1049 return ret;
1050 }
Zhang Rui9e89dde2006-12-07 20:56:16 +08001051 }
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001052
1053 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
1054 acpi_drv->name, acpi_dev->pnp.bus_id));
1055 get_device(dev);
1056 return 0;
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001057}
1058
1059static int acpi_device_remove(struct device * dev)
1060{
1061 struct acpi_device *acpi_dev = to_acpi_device(dev);
1062 struct acpi_driver *acpi_drv = acpi_dev->driver;
1063
1064 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001065 if (acpi_drv->ops.notify)
1066 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001067 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001068 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001069 }
1070 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001071 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001072
1073 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +08001074 return 0;
1075}
1076
David Brownell55955aa2007-05-08 00:28:35 -07001077struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +08001078 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001079 .match = acpi_bus_match,
1080 .probe = acpi_device_probe,
1081 .remove = acpi_device_remove,
1082 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083};
1084
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001085static void acpi_device_del(struct acpi_device *device)
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001086{
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001087 mutex_lock(&acpi_device_lock);
1088 if (device->parent)
1089 list_del(&device->node);
1090
1091 list_del(&device->wakeup_list);
1092 mutex_unlock(&acpi_device_lock);
1093
1094 acpi_power_add_remove_device(device, false);
1095 acpi_device_remove_files(device);
1096 if (device->remove)
1097 device->remove(device);
1098
1099 device_del(&device->dev);
1100}
1101
1102static LIST_HEAD(acpi_device_del_list);
1103static DEFINE_MUTEX(acpi_device_del_lock);
1104
1105static void acpi_device_del_work_fn(struct work_struct *work_not_used)
1106{
1107 for (;;) {
1108 struct acpi_device *adev;
1109
1110 mutex_lock(&acpi_device_del_lock);
1111
1112 if (list_empty(&acpi_device_del_list)) {
1113 mutex_unlock(&acpi_device_del_lock);
1114 break;
1115 }
1116 adev = list_first_entry(&acpi_device_del_list,
1117 struct acpi_device, del_list);
1118 list_del(&adev->del_list);
1119
1120 mutex_unlock(&acpi_device_del_lock);
1121
1122 acpi_device_del(adev);
1123 /*
1124 * Drop references to all power resources that might have been
1125 * used by the device.
1126 */
1127 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
1128 put_device(&adev->dev);
1129 }
1130}
1131
1132/**
1133 * acpi_scan_drop_device - Drop an ACPI device object.
1134 * @handle: Handle of an ACPI namespace node, not used.
1135 * @context: Address of the ACPI device object to drop.
1136 *
1137 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
1138 * namespace node the device object pointed to by @context is attached to.
1139 *
1140 * The unregistration is carried out asynchronously to avoid running
1141 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
1142 * ensure the correct ordering (the device objects must be unregistered in the
1143 * same order in which the corresponding namespace nodes are deleted).
1144 */
1145static void acpi_scan_drop_device(acpi_handle handle, void *context)
1146{
1147 static DECLARE_WORK(work, acpi_device_del_work_fn);
1148 struct acpi_device *adev = context;
1149
1150 mutex_lock(&acpi_device_del_lock);
1151
1152 /*
1153 * Use the ACPI hotplug workqueue which is ordered, so this work item
1154 * won't run after any hotplug work items submitted subsequently. That
1155 * prevents attempts to register device objects identical to those being
1156 * deleted from happening concurrently (such attempts result from
1157 * hotplug events handled via the ACPI hotplug workqueue). It also will
1158 * run after all of the work items submitted previosuly, which helps
1159 * those work items to ensure that they are not accessing stale device
1160 * objects.
1161 */
1162 if (list_empty(&acpi_device_del_list))
1163 acpi_queue_hotplug_work(&work);
1164
1165 list_add_tail(&adev->del_list, &acpi_device_del_list);
1166 /* Make acpi_ns_validate_handle() return NULL for this handle. */
1167 adev->handle = INVALID_ACPI_HANDLE;
1168
1169 mutex_unlock(&acpi_device_del_lock);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001170}
1171
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001172static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
1173 void (*callback)(void *))
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001174{
1175 acpi_status status;
1176
1177 if (!device)
1178 return -EINVAL;
1179
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001180 status = acpi_get_data_full(handle, acpi_scan_drop_device,
1181 (void **)device, callback);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001182 if (ACPI_FAILURE(status) || !*device) {
1183 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
1184 handle));
1185 return -ENODEV;
1186 }
1187 return 0;
1188}
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001189
1190int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
1191{
1192 return acpi_get_device_data(handle, device, NULL);
1193}
Rafael J. Wysocki65859252013-10-01 23:02:43 +02001194EXPORT_SYMBOL(acpi_bus_get_device);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001195
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001196static void get_acpi_device(void *dev)
1197{
1198 if (dev)
1199 get_device(&((struct acpi_device *)dev)->dev);
1200}
1201
1202struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
1203{
1204 struct acpi_device *adev = NULL;
1205
1206 acpi_get_device_data(handle, &adev, get_acpi_device);
1207 return adev;
1208}
1209
1210void acpi_bus_put_acpi_device(struct acpi_device *adev)
1211{
1212 put_device(&adev->dev);
1213}
1214
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001215int acpi_device_add(struct acpi_device *device,
1216 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001218 int result;
1219 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1220 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001221
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001222 if (device->handle) {
1223 acpi_status status;
1224
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001225 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001226 device);
1227 if (ACPI_FAILURE(status)) {
1228 acpi_handle_err(device->handle,
1229 "Unable to attach device data\n");
1230 return -ENODEV;
1231 }
1232 }
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 /*
1235 * Linkage
1236 * -------
1237 * Link this device to its parent and siblings.
1238 */
1239 INIT_LIST_HEAD(&device->children);
1240 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001242 INIT_LIST_HEAD(&device->physical_node_list);
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001243 INIT_LIST_HEAD(&device->del_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001244 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001246 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1247 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001248 pr_err(PREFIX "Memory allocation error\n");
1249 result = -ENOMEM;
1250 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001251 }
1252
Shaohua Li90905892009-04-07 10:24:29 +08001253 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001254 /*
1255 * Find suitable bus_id and instance number in acpi_bus_id_list
1256 * If failed, create one and link it into acpi_bus_id_list
1257 */
1258 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001259 if (!strcmp(acpi_device_bus_id->bus_id,
1260 acpi_device_hid(device))) {
1261 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001262 found = 1;
1263 kfree(new_bus_id);
1264 break;
1265 }
1266 }
Alex Chiang0c526d92009-05-14 08:31:37 -06001267 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001268 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001269 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001270 acpi_device_bus_id->instance_no = 0;
1271 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1272 }
Kay Sievers07944692008-10-30 01:18:59 +01001273 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 +08001274
Len Brown33b57152008-12-15 22:09:26 -05001275 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -05001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (device->wakeup.flags.valid)
1279 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +08001280 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Patrick Mochel1890a972006-12-07 20:56:31 +08001282 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001283 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +08001284 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001285 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001286 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -06001287 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -06001288 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001289 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001290 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001291
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001292 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -06001293 if (result)
Kay Sievers07944692008-10-30 01:18:59 +01001294 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1295 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001296
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001297 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001298
1299 err:
Shaohua Li90905892009-04-07 10:24:29 +08001300 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001301 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001302 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001303 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001304 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001305
1306 err_detach:
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001307 acpi_detach_data(device->handle, acpi_scan_drop_device);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001308 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
Zhang Rui9e89dde2006-12-07 20:56:16 +08001311/* --------------------------------------------------------------------------
1312 Driver Management
1313 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001315 * acpi_bus_register_driver - register a driver with the ACPI bus
1316 * @driver: driver being registered
1317 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001319 * devices that match the driver's criteria and binds. Returns zero for
1320 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 */
Len Brown4be44fc2005-08-05 00:44:28 -04001322int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Patrick Mochel1890a972006-12-07 20:56:31 +08001324 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001327 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001328 driver->drv.name = driver->name;
1329 driver->drv.bus = &acpi_bus_type;
1330 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Patrick Mochel1890a972006-12-07 20:56:31 +08001332 ret = driver_register(&driver->drv);
1333 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Len Brown4be44fc2005-08-05 00:44:28 -04001336EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338/**
Hanjun Guob27b14c2013-09-22 15:42:41 +08001339 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001340 * @driver: driver to unregister
1341 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1343 * devices that match the driver's criteria and unbinds.
1344 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001345void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Patrick Mochel1890a972006-12-07 20:56:31 +08001347 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
Len Brown4be44fc2005-08-05 00:44:28 -04001349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350EXPORT_SYMBOL(acpi_bus_unregister_driver);
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352/* --------------------------------------------------------------------------
1353 Device Enumeration
1354 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001355static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1356{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001357 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001358 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001359
1360 /*
1361 * Fixed hardware devices do not appear in the namespace and do not
1362 * have handles, but we fabricate acpi_devices for them, so we have
1363 * to deal with them specially.
1364 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001365 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001366 return acpi_root;
1367
1368 do {
1369 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001370 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001371 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1372 } while (acpi_bus_get_device(handle, &device));
1373 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001374}
1375
Len Brownc8f7a622006-07-09 17:22:28 -04001376acpi_status
1377acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1378{
1379 acpi_status status;
1380 acpi_handle tmp;
1381 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1382 union acpi_object *obj;
1383
1384 status = acpi_get_handle(handle, "_EJD", &tmp);
1385 if (ACPI_FAILURE(status))
1386 return status;
1387
1388 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1389 if (ACPI_SUCCESS(status)) {
1390 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001391 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1392 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001393 kfree(buffer.pointer);
1394 }
1395 return status;
1396}
1397EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1398
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001399static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1400 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001402 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1403 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001405 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001406 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001408 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001409 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001411 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001413 /* _PRW */
1414 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1415 if (ACPI_FAILURE(status)) {
1416 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001417 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001418 }
1419
1420 package = (union acpi_object *)buffer.pointer;
1421
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001422 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001423 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001426 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001427 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (element->type == ACPI_TYPE_PACKAGE) {
1430 if ((element->package.count < 2) ||
1431 (element->package.elements[0].type !=
1432 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001433 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001434 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001435
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001436 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001438 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 (u32) element->package.elements[1].integer.value;
1440 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001441 wakeup->gpe_device = NULL;
1442 wakeup->gpe_number = element->integer.value;
1443 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001444 goto out;
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001448 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001449 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001450
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001451 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001453 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1454 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001455 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001457 if (!list_empty(&wakeup->resources)) {
1458 int sleep_state;
1459
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001460 err = acpi_power_wakeup_list_init(&wakeup->resources,
1461 &sleep_state);
1462 if (err) {
1463 acpi_handle_warn(handle, "Retrieving current states "
1464 "of wakeup power resources failed\n");
1465 acpi_power_resources_list_free(&wakeup->resources);
1466 goto out;
1467 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001468 if (sleep_state < wakeup->sleep_state) {
1469 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1470 "(S%d) by S%d from power resources\n",
1471 (int)wakeup->sleep_state, sleep_state);
1472 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001475
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001476 out:
1477 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001478 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001481static void acpi_wakeup_gpe_init(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001483 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001484 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001485 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001486 {"PNP0C0E", 0},
1487 {"", 0},
1488 };
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001489 struct acpi_device_wakeup *wakeup = &device->wakeup;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001490 acpi_status status;
1491 acpi_event_status event_status;
1492
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001493 wakeup->flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001494
1495 /* Power button, Lid switch always enable wakeup */
1496 if (!acpi_match_device_ids(device, button_device_ids)) {
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001497 wakeup->flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001498 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1499 /* Do not use Lid/sleep button for S5 wakeup */
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001500 if (wakeup->sleep_state == ACPI_STATE_S5)
1501 wakeup->sleep_state = ACPI_STATE_S4;
Zhang Ruib7e38302012-12-04 23:23:16 +01001502 }
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001503 acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001504 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001505 return;
1506 }
1507
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001508 acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
1509 wakeup->gpe_number);
1510 status = acpi_get_gpe_status(wakeup->gpe_device, wakeup->gpe_number,
1511 &event_status);
1512 if (ACPI_FAILURE(status))
1513 return;
1514
Lv Zheng2f857232014-10-10 10:40:05 +08001515 wakeup->flags.run_wake = !!(event_status & ACPI_EVENT_FLAG_HAS_HANDLER);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001516}
1517
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001518static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001519{
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001520 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001521
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001522 /* Presence of _PRW indicates wake capable */
Jiang Liu952c63e2013-06-29 00:24:38 +08001523 if (!acpi_has_method(device->handle, "_PRW"))
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001524 return;
1525
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001526 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1527 &device->wakeup);
1528 if (err) {
1529 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001530 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001534 device->wakeup.prepare_count = 0;
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001535 acpi_wakeup_gpe_init(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001536 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1537 * system for the ACPI device with the _PRW object.
1538 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1539 * So it is necessary to call _DSW object first. Only when it is not
1540 * present will the _PSW object used.
1541 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001542 err = acpi_device_sleep_wake(device, 0, 0, 0);
1543 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001544 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1545 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001548static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001550 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001551 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1552 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001553 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001554
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001555 INIT_LIST_HEAD(&ps->resources);
1556
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001557 /* Evaluate "_PRx" to get referenced power resources */
1558 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1559 if (ACPI_SUCCESS(status)) {
1560 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001561
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001562 if (buffer.length && package
1563 && package->type == ACPI_TYPE_PACKAGE
1564 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001565 int err = acpi_extract_power_resources(package, 0,
1566 &ps->resources);
1567 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001568 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001569 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001570 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001571 }
1572
1573 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001574 pathname[2] = 'S';
Jiang Liu952c63e2013-06-29 00:24:38 +08001575 if (acpi_has_method(device->handle, pathname))
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001576 ps->flags.explicit_set = 1;
1577
1578 /*
1579 * State is valid if there are means to put the device into it.
1580 * D3hot is only valid if _PR3 present.
1581 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001582 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001583 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1584 ps->flags.valid = 1;
1585 ps->flags.os_accessible = 1;
1586 }
1587
1588 ps->power = -1; /* Unknown - driver assigned */
1589 ps->latency = -1; /* Unknown - driver assigned */
1590}
1591
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001592static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001594 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001596 /* Presence of _PS0|_PR0 indicates 'power manageable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001597 if (!acpi_has_method(device->handle, "_PS0") &&
1598 !acpi_has_method(device->handle, "_PR0"))
1599 return;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001600
1601 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001604 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 */
Jiang Liu952c63e2013-06-29 00:24:38 +08001606 if (acpi_has_method(device->handle, "_PSC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001607 device->power.flags.explicit_get = 1;
Rafael J. Wysockif25c0ae2014-05-17 00:18:13 +02001608
Jiang Liu952c63e2013-06-29 00:24:38 +08001609 if (acpi_has_method(device->handle, "_IRC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001610 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Rafael J. Wysockif25c0ae2014-05-17 00:18:13 +02001612 if (acpi_has_method(device->handle, "_DSW"))
1613 device->power.flags.dsw_present = 1;
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001616 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001618 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1619 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001621 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Zhang Rui9e89dde2006-12-07 20:56:16 +08001623 /* Set defaults for D0 and D3 states (always valid) */
1624 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1625 device->power.states[ACPI_STATE_D0].power = 100;
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +02001626 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1627 device->power.states[ACPI_STATE_D3_COLD].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001629 /* Set D3cold's explicit_set flag if _PS3 exists. */
1630 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1631 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1632
Aaron Lu1399dfc2012-11-21 23:33:40 +01001633 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1634 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1635 device->power.flags.power_resources)
1636 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1637
Rafael J. Wysocki3dcf1572015-01-01 23:38:28 +01001638 if (acpi_bus_init_power(device))
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001639 device->flags.power_manageable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640}
1641
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001642static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 /* Presence of _STA indicates 'dynamic_status' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001645 if (acpi_has_method(device->handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 device->flags.dynamic_status = 1;
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 /* Presence of _RMV indicates 'removable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001649 if (acpi_has_method(device->handle, "_RMV"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 device->flags.removable = 1;
1651
1652 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001653 if (acpi_has_method(device->handle, "_EJD") ||
1654 acpi_has_method(device->handle, "_EJ0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 device->flags.ejectable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001658static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Len Brown4be44fc2005-08-05 00:44:28 -04001660 char bus_id[5] = { '?', 0 };
1661 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1662 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 /*
1665 * Bus ID
1666 * ------
1667 * The device's Bus ID is simply the object name.
1668 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1669 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001670 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001672 return;
1673 }
1674
1675 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 case ACPI_BUS_TYPE_POWER_BUTTON:
1677 strcpy(device->pnp.bus_id, "PWRF");
1678 break;
1679 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1680 strcpy(device->pnp.bus_id, "SLPF");
1681 break;
1682 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001683 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 /* Clean up trailing underscores (if any) */
1685 for (i = 3; i > 1; i--) {
1686 if (bus_id[i] == '_')
1687 bus_id[i] = '\0';
1688 else
1689 break;
1690 }
1691 strcpy(device->pnp.bus_id, bus_id);
1692 break;
1693 }
1694}
1695
Zhang Rui54735262007-01-11 02:09:09 -05001696/*
Jiang Liuebf4df82013-06-29 00:24:41 +08001697 * acpi_ata_match - see if an acpi object is an ATA device
1698 *
1699 * If an acpi object has one of the ACPI ATA methods defined,
1700 * then we can safely call it an ATA device.
1701 */
1702bool acpi_ata_match(acpi_handle handle)
1703{
1704 return acpi_has_method(handle, "_GTF") ||
1705 acpi_has_method(handle, "_GTM") ||
1706 acpi_has_method(handle, "_STM") ||
1707 acpi_has_method(handle, "_SDD");
1708}
1709
1710/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001711 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001712 *
1713 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1714 * then we can safely call it an ejectable drive bay
1715 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001716bool acpi_bay_match(acpi_handle handle)
Toshi Kanid4e1a692013-03-04 21:30:41 +00001717{
Zhang Rui54735262007-01-11 02:09:09 -05001718 acpi_handle phandle;
1719
Jiang Liu952c63e2013-06-29 00:24:38 +08001720 if (!acpi_has_method(handle, "_EJ0"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001721 return false;
1722 if (acpi_ata_match(handle))
1723 return true;
1724 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1725 return false;
Zhang Rui54735262007-01-11 02:09:09 -05001726
Jiang Liuebf4df82013-06-29 00:24:41 +08001727 return acpi_ata_match(phandle);
Zhang Rui54735262007-01-11 02:09:09 -05001728}
1729
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001730bool acpi_device_is_battery(struct acpi_device *adev)
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001731{
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001732 struct acpi_hardware_id *hwid;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001733
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001734 list_for_each_entry(hwid, &adev->pnp.ids, list)
1735 if (!strcmp("PNP0C0A", hwid->id))
1736 return true;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001737
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001738 return false;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001739}
1740
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001741static bool is_ejectable_bay(struct acpi_device *adev)
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001742{
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001743 acpi_handle handle = adev->handle;
1744
1745 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001746 return true;
1747
1748 return acpi_bay_match(handle);
1749}
1750
Frank Seidel3620f2f2007-12-07 13:20:34 +01001751/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001752 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001753 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001754bool acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001755{
Jiang Liuebf4df82013-06-29 00:24:41 +08001756 return acpi_has_method(handle, "_DCK");
Frank Seidel3620f2f2007-12-07 13:20:34 +01001757}
1758
Thomas Renninger620e1122010-10-01 10:54:00 +02001759const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001760{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001761 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001762
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001763 if (list_empty(&device->pnp.ids))
1764 return dummy_hid;
1765
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001766 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1767 return hid->id;
1768}
1769EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001770
Toshi Kanid4e1a692013-03-04 21:30:41 +00001771static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001772{
1773 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001774
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001775 id = kmalloc(sizeof(*id), GFP_KERNEL);
1776 if (!id)
1777 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001778
Thomas Meyer581de592011-08-06 11:32:56 +02001779 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001780 if (!id->id) {
1781 kfree(id);
1782 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001783 }
1784
Toshi Kanid4e1a692013-03-04 21:30:41 +00001785 list_add_tail(&id->list, &pnp->ids);
1786 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001787}
1788
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001789/*
1790 * Old IBM workstations have a DSDT bug wherein the SMBus object
1791 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1792 * prefix. Work around this.
1793 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001794static bool acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001795{
Jiang Liuebf4df82013-06-29 00:24:41 +08001796 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1797 struct acpi_buffer path = { sizeof(node_name), node_name };
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001798
1799 if (!dmi_name_in_vendors("IBM"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001800 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001801
1802 /* Look for SMBS object */
Jiang Liuebf4df82013-06-29 00:24:41 +08001803 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1804 strcmp("SMBS", path.pointer))
1805 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001806
1807 /* Does it have the necessary (but misnamed) methods? */
Jiang Liu952c63e2013-06-29 00:24:38 +08001808 if (acpi_has_method(handle, "SBI") &&
1809 acpi_has_method(handle, "SBR") &&
1810 acpi_has_method(handle, "SBW"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001811 return true;
1812
1813 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001814}
1815
Jiang Liuae3caa82014-02-19 14:02:19 +08001816static bool acpi_object_is_system_bus(acpi_handle handle)
1817{
1818 acpi_handle tmp;
1819
1820 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1821 tmp == handle)
1822 return true;
1823 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1824 tmp == handle)
1825 return true;
1826
1827 return false;
1828}
1829
Toshi Kanic0af4172013-03-04 21:30:42 +00001830static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1831 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Len Brown4be44fc2005-08-05 00:44:28 -04001833 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001834 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001835 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001836 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Toshi Kanic0af4172013-03-04 21:30:42 +00001838 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00001840 if (handle == ACPI_ROOT_OBJECT) {
1841 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001842 break;
1843 }
1844
Toshi Kanic0af4172013-03-04 21:30:42 +00001845 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001847 pr_err(PREFIX "%s: Error reading device info\n",
1848 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return;
1850 }
1851
Rafael J. Wysocki549e6842014-05-30 04:26:18 +02001852 if (info->valid & ACPI_VALID_HID) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001853 acpi_add_id(pnp, info->hardware_id.string);
Rafael J. Wysocki549e6842014-05-30 04:26:18 +02001854 pnp->type.platform_id = 1;
1855 }
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001856 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001857 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001858 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00001859 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001862 pnp->bus_address = info->address;
1863 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
Lv Zhengccf78042012-10-30 14:41:07 +01001865 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001866 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01001867 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001868
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001869 kfree(info);
1870
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001871 /*
1872 * Some devices don't reliably have _HIDs & _CIDs, so add
1873 * synthetic HIDs to make sure drivers can find them.
1874 */
Toshi Kanic0af4172013-03-04 21:30:42 +00001875 if (acpi_is_video_device(handle))
1876 acpi_add_id(pnp, ACPI_VIDEO_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001877 else if (acpi_bay_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001878 acpi_add_id(pnp, ACPI_BAY_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001879 else if (acpi_dock_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001880 acpi_add_id(pnp, ACPI_DOCK_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001881 else if (acpi_ibm_smbus_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001882 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
Jiang Liuae3caa82014-02-19 14:02:19 +08001883 else if (list_empty(&pnp->ids) &&
1884 acpi_object_is_system_bus(handle)) {
1885 /* \_SB, \_TZ, LNXSYBUS */
1886 acpi_add_id(pnp, ACPI_BUS_HID);
Toshi Kanic0af4172013-03-04 21:30:42 +00001887 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1888 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001889 }
Zhang Rui54735262007-01-11 02:09:09 -05001890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 break;
1892 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00001893 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 break;
1895 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00001896 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00001899 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 break;
1901 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001902 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 break;
1904 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001905 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 break;
1907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
Toshi Kanic0af4172013-03-04 21:30:42 +00001910void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1911{
1912 struct acpi_hardware_id *id, *tmp;
1913
1914 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1915 kfree(id->id);
1916 kfree(id);
1917 }
1918 kfree(pnp->unique_id);
1919}
1920
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001921void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1922 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001924 INIT_LIST_HEAD(&device->pnp.ids);
1925 device->device_type = type;
1926 device->handle = handle;
1927 device->parent = acpi_bus_get_parent(handle);
Rafael J. Wysocki25db1152013-11-22 21:56:06 +01001928 acpi_set_device_status(device, sta);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001929 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00001930 acpi_set_pnp_ids(handle, &device->pnp, type);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001931 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001932 device->flags.match_driver = false;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001933 device->flags.initialized = true;
1934 device->flags.visited = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001935 device_initialize(&device->dev);
1936 dev_set_uevent_suppress(&device->dev, true);
1937}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001938
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001939void acpi_device_add_finalize(struct acpi_device *device)
1940{
1941 dev_set_uevent_suppress(&device->dev, false);
1942 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001945static int acpi_add_single_object(struct acpi_device **child,
1946 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001947 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001949 int result;
1950 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001951 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Burman Yan36bcbec2006-12-19 12:56:11 -08001953 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001955 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001956 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001959 acpi_init_device_object(device, handle, type, sta);
1960 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001961 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001963 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001964 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001965 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001966 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
1968
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001969 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001970 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001971 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1972 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1973 dev_name(&device->dev), (char *) buffer.pointer,
1974 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1975 kfree(buffer.pointer);
1976 *child = device;
1977 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001978}
1979
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001980static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1981 unsigned long long *sta)
1982{
1983 acpi_status status;
1984 acpi_object_type acpi_type;
1985
1986 status = acpi_get_type(handle, &acpi_type);
1987 if (ACPI_FAILURE(status))
1988 return -ENODEV;
1989
1990 switch (acpi_type) {
1991 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1992 case ACPI_TYPE_DEVICE:
1993 *type = ACPI_BUS_TYPE_DEVICE;
1994 status = acpi_bus_get_status_handle(handle, sta);
1995 if (ACPI_FAILURE(status))
1996 return -ENODEV;
1997 break;
1998 case ACPI_TYPE_PROCESSOR:
1999 *type = ACPI_BUS_TYPE_PROCESSOR;
2000 status = acpi_bus_get_status_handle(handle, sta);
2001 if (ACPI_FAILURE(status))
2002 return -ENODEV;
2003 break;
2004 case ACPI_TYPE_THERMAL:
2005 *type = ACPI_BUS_TYPE_THERMAL;
2006 *sta = ACPI_STA_DEFAULT;
2007 break;
2008 case ACPI_TYPE_POWER:
2009 *type = ACPI_BUS_TYPE_POWER;
2010 *sta = ACPI_STA_DEFAULT;
2011 break;
2012 default:
2013 return -ENODEV;
2014 }
2015
2016 return 0;
2017}
2018
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002019bool acpi_device_is_present(struct acpi_device *adev)
2020{
2021 if (adev->status.present || adev->status.functional)
2022 return true;
2023
2024 adev->flags.initialized = false;
2025 return false;
2026}
2027
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01002028static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
2029 char *idstr,
2030 const struct acpi_device_id **matchid)
2031{
2032 const struct acpi_device_id *devid;
2033
Rafael J. Wysockiaca0a4e2014-05-30 04:21:52 +02002034 if (handler->match)
2035 return handler->match(idstr, matchid);
2036
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01002037 for (devid = handler->ids; devid->id[0]; devid++)
2038 if (!strcmp((char *)devid->id, idstr)) {
2039 if (matchid)
2040 *matchid = devid;
2041
2042 return true;
2043 }
2044
2045 return false;
2046}
2047
Toshi Kani6b772e8f2013-03-04 21:30:43 +00002048static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
2049 const struct acpi_device_id **matchid)
2050{
2051 struct acpi_scan_handler *handler;
2052
2053 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
2054 if (acpi_scan_handler_matching(handler, idstr, matchid))
2055 return handler;
2056
2057 return NULL;
2058}
2059
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01002060void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
2061{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01002062 if (!!hotplug->enabled == !!val)
2063 return;
2064
2065 mutex_lock(&acpi_scan_lock);
2066
2067 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01002068
2069 mutex_unlock(&acpi_scan_lock);
2070}
2071
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002072static void acpi_scan_init_hotplug(struct acpi_device *adev)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002073{
Toshi Kani6b772e8f2013-03-04 21:30:43 +00002074 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002075
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01002076 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01002077 acpi_dock_add(adev);
2078 return;
2079 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002080 list_for_each_entry(hwid, &adev->pnp.ids, list) {
2081 struct acpi_scan_handler *handler;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002082
Toshi Kani6b772e8f2013-03-04 21:30:43 +00002083 handler = acpi_scan_match_handler(hwid->id, NULL);
Rafael J. Wysocki1a699472014-02-06 13:58:13 +01002084 if (handler) {
2085 adev->flags.hotplug_notify = true;
2086 break;
2087 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002088 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01002089}
2090
Rafael J. Wysockie3863092012-12-21 00:36:47 +01002091static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
2092 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01002094 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00002095 int type;
2096 unsigned long long sta;
2097 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002098
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01002099 acpi_bus_get_device(handle, &device);
2100 if (device)
2101 goto out;
2102
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00002103 result = acpi_bus_type_and_status(handle, &type, &sta);
2104 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002105 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01002107 if (type == ACPI_BUS_TYPE_POWER) {
2108 acpi_add_power_resource(handle);
2109 return AE_OK;
2110 }
2111
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002112 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00002113 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002114 return AE_CTRL_DEPTH;
2115
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002116 acpi_scan_init_hotplug(device);
2117
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01002118 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002119 if (!*return_value)
2120 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01002121
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002122 return AE_OK;
2123}
2124
Zhang Rui48459342014-05-30 14:35:34 +02002125static int acpi_check_spi_i2c_slave(struct acpi_resource *ares, void *data)
2126{
2127 bool *is_spi_i2c_slave_p = data;
2128
2129 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
2130 return 1;
2131
2132 /*
2133 * devices that are connected to UART still need to be enumerated to
2134 * platform bus
2135 */
2136 if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
2137 *is_spi_i2c_slave_p = true;
2138
2139 /* no need to do more checking */
2140 return -1;
2141}
2142
2143static void acpi_default_enumeration(struct acpi_device *device)
2144{
2145 struct list_head resource_list;
2146 bool is_spi_i2c_slave = false;
2147
2148 if (!device->pnp.type.platform_id || device->handler)
2149 return;
2150
2151 /*
2152 * Do not enemerate SPI/I2C slaves as they will be enuerated by their
2153 * respective parents.
2154 */
2155 INIT_LIST_HEAD(&resource_list);
2156 acpi_dev_get_resources(device, &resource_list, acpi_check_spi_i2c_slave,
2157 &is_spi_i2c_slave);
2158 acpi_dev_free_resource_list(&resource_list);
2159 if (!is_spi_i2c_slave)
2160 acpi_create_platform_device(device);
2161}
2162
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002163static int acpi_scan_attach_handler(struct acpi_device *device)
2164{
2165 struct acpi_hardware_id *hwid;
2166 int ret = 0;
2167
2168 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002169 const struct acpi_device_id *devid;
2170 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002171
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002172 handler = acpi_scan_match_handler(hwid->id, &devid);
2173 if (handler) {
Rafael J. Wysockid34afa92014-05-30 04:27:31 +02002174 if (!handler->attach) {
2175 device->pnp.type.platform_id = 0;
2176 continue;
2177 }
Rafael J. Wysocki9cb32ac2014-02-11 00:35:46 +01002178 device->handler = handler;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002179 ret = handler->attach(device, devid);
Rafael J. Wysocki9cb32ac2014-02-11 00:35:46 +01002180 if (ret > 0)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002181 break;
Rafael J. Wysocki9cb32ac2014-02-11 00:35:46 +01002182
2183 device->handler = NULL;
2184 if (ret < 0)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002185 break;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002186 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002187 }
Zhang Rui48459342014-05-30 14:35:34 +02002188 if (!ret)
2189 acpi_default_enumeration(device);
2190
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002191 return ret;
2192}
2193
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002194static void acpi_bus_attach(struct acpi_device *device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002195{
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002196 struct acpi_device *child;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01002197 acpi_handle ejd;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002198 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002199
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01002200 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
2201 register_dock_dependent_device(device, ejd);
2202
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002203 acpi_bus_get_status(device);
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002204 /* Skip devices that are not present. */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002205 if (!acpi_device_is_present(device)) {
2206 device->flags.visited = false;
Rafael J. Wysocki3dcf1572015-01-01 23:38:28 +01002207 device->flags.power_manageable = 0;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002208 return;
2209 }
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02002210 if (device->handler)
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002211 goto ok;
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02002212
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002213 if (!device->flags.initialized) {
Rafael J. Wysocki3dcf1572015-01-01 23:38:28 +01002214 device->flags.power_manageable =
2215 device->power.states[ACPI_STATE_D0].flags.valid;
2216 if (acpi_bus_init_power(device))
2217 device->flags.power_manageable = 0;
2218
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002219 device->flags.initialized = true;
2220 }
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002221 device->flags.visited = false;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002222 ret = acpi_scan_attach_handler(device);
Rafael J. Wysocki69310072013-11-07 01:41:01 +01002223 if (ret < 0)
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002224 return;
Rafael J. Wysocki69310072013-11-07 01:41:01 +01002225
2226 device->flags.match_driver = true;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002227 if (!ret) {
2228 ret = device_attach(&device->dev);
2229 if (ret < 0)
2230 return;
2231 }
2232 device->flags.visited = true;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002233
2234 ok:
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002235 list_for_each_entry(child, &device->children, node)
2236 acpi_bus_attach(child);
Rafael J. Wysocki8ab17fc2014-09-21 02:58:18 +02002237
2238 if (device->handler && device->handler->hotplug.notify_online)
2239 device->handler->hotplug.notify_online(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002242/**
2243 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2244 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01002245 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002246 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2247 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01002248 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002249 * If no devices were found, -ENODEV is returned, but it does not mean that
2250 * there has been a real error. There just have been no suitable ACPI objects
2251 * in the table trunk from which the kernel could create a device and add an
2252 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002253 *
2254 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01002255 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002256int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07002257{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 void *device = NULL;
Rajesh Shah3fb02732005-04-28 00:25:52 -07002259
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002260 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01002262 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002263
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002264 if (device) {
2265 acpi_bus_attach(device);
2266 return 0;
2267 }
2268 return -ENODEV;
Rajesh Shah3fb02732005-04-28 00:25:52 -07002269}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002270EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002272/**
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002273 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2274 * @adev: Root of the ACPI namespace scope to walk.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002275 *
2276 * Must be called under acpi_scan_lock.
2277 */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002278void acpi_bus_trim(struct acpi_device *adev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002280 struct acpi_scan_handler *handler = adev->handler;
2281 struct acpi_device *child;
2282
2283 list_for_each_entry_reverse(child, &adev->children, node)
2284 acpi_bus_trim(child);
2285
Rafael J. Wysockia951c772014-01-27 23:08:09 +01002286 adev->flags.match_driver = false;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002287 if (handler) {
2288 if (handler->detach)
2289 handler->detach(adev);
2290
2291 adev->handler = NULL;
2292 } else {
2293 device_release_driver(&adev->dev);
2294 }
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002295 /*
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002296 * Most likely, the device is going away, so put it into D3cold before
2297 * that.
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002298 */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002299 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2300 adev->flags.initialized = false;
2301 adev->flags.visited = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
Kristen Accardiceaba662006-02-23 17:56:01 -08002303EXPORT_SYMBOL_GPL(acpi_bus_trim);
2304
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00002305static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Len Brown4be44fc2005-08-05 00:44:28 -04002307 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08002308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 /*
2310 * Enumerate all fixed-feature devices.
2311 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002312 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2313 struct acpi_device *device = NULL;
2314
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002315 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002316 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002317 ACPI_STA_DEFAULT);
2318 if (result)
2319 return result;
2320
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002321 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002322 result = device_attach(&device->dev);
2323 if (result < 0)
2324 return result;
2325
Daniel Drakec10d7a12012-05-10 00:08:43 +01002326 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002329 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2330 struct acpi_device *device = NULL;
2331
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002332 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002333 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002334 ACPI_STA_DEFAULT);
2335 if (result)
2336 return result;
2337
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002338 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002339 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002342 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
Bjorn Helgaase747f272009-03-24 16:49:43 -06002345int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
2347 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Patrick Mochel5b327262006-05-10 10:33:00 -04002349 result = bus_register(&acpi_bus_type);
2350 if (result) {
2351 /* We don't want to quit even if we failed to add suspend/resume */
2352 printk(KERN_ERR PREFIX "Could not register bus type\n");
2353 }
2354
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01002355 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01002356 acpi_pci_link_init();
Rafael J. Wysockiac212b62013-05-03 00:26:22 +02002357 acpi_processor_init();
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01002358 acpi_lpss_init();
Lan Tianyu2fa97fe2013-06-05 02:27:50 +00002359 acpi_cmos_rtc_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01002360 acpi_container_init();
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01002361 acpi_memory_hotplug_init();
Zhang Ruieec15ed2014-05-30 04:23:01 +02002362 acpi_pnp_init();
Zhang Rui3230bbf2014-03-14 00:34:05 +08002363 acpi_int340x_thermal_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01002364
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002365 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 * Enumerate devices in the ACPI namespace.
2368 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002369 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002371 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002373 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002375 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Aaron Lu2650ef42014-04-28 10:38:04 +08002377 /* Fixed feature devices do not exist on HW-reduced platform */
2378 if (!acpi_gbl_reduced_hardware) {
2379 result = acpi_bus_scan_fixed();
2380 if (result) {
2381 acpi_detach_data(acpi_root->handle,
2382 acpi_scan_drop_device);
2383 acpi_device_del(acpi_root);
2384 put_device(&acpi_root->dev);
2385 goto out;
2386 }
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002387 }
2388
2389 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002390
2391 out:
2392 mutex_unlock(&acpi_scan_lock);
2393 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}