blob: 405056b95b05b34a29b9d90c283032d11614047a [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>
Suthikulpanit, Suraveed0562672015-06-10 11:08:52 -050014#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Rafael J. Wysockid7831562013-11-22 21:52:12 +010016#include <asm/pgtable.h>
17
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060018#include "internal.h"
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050021ACPI_MODULE_NAME("scan");
Len Brown4be44fc2005-08-05 00:44:28 -040022extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020025#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define ACPI_BUS_DEVICE_NAME "System Bus"
27
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +000028#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
29
Rafael J. Wysockid7831562013-11-22 21:52:12 +010030#define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
31
Rafael J. Wysocki683058e2013-05-03 00:26:16 +020032/*
33 * If set, devices will be hot-removed even if they cannot be put offline
34 * gracefully (from the kernel's standpoint).
35 */
36bool acpi_force_hot_remove;
37
Thomas Renninger620e1122010-10-01 10:54:00 +020038static const char *dummy_hid = "device";
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020039
Lan Tianyu40e7fcb2014-11-23 21:22:54 +080040static LIST_HEAD(acpi_dep_list);
41static DEFINE_MUTEX(acpi_dep_list_lock);
Lukas Wunner2d12b6b2015-11-25 21:19:55 +010042LIST_HEAD(acpi_bus_id_list);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +010043static DEFINE_MUTEX(acpi_scan_lock);
Rafael J. Wysockica589f92013-01-30 14:27:29 +010044static LIST_HEAD(acpi_scan_handlers_list);
Shaohua Li90905892009-04-07 10:24:29 +080045DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046LIST_HEAD(acpi_wakeup_device_list);
Rafael J. Wysockie5255062014-02-04 00:43:17 +010047static DEFINE_MUTEX(acpi_hp_context_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Lan Tianyu40e7fcb2014-11-23 21:22:54 +080049struct acpi_dep_data {
50 struct list_head node;
51 acpi_handle master;
52 acpi_handle slave;
53};
54
Rafael J. Wysocki3757b942013-02-13 14:36:47 +010055void acpi_scan_lock_acquire(void)
56{
57 mutex_lock(&acpi_scan_lock);
58}
59EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
60
61void acpi_scan_lock_release(void)
62{
63 mutex_unlock(&acpi_scan_lock);
64}
65EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
66
Rafael J. Wysockie5255062014-02-04 00:43:17 +010067void acpi_lock_hp_context(void)
68{
69 mutex_lock(&acpi_hp_context_lock);
70}
71
72void acpi_unlock_hp_context(void)
73{
74 mutex_unlock(&acpi_hp_context_lock);
75}
76
Rafael J. Wysocki5d513202014-02-22 00:48:31 +010077void acpi_initialize_hp_context(struct acpi_device *adev,
78 struct acpi_hotplug_context *hp,
79 int (*notify)(struct acpi_device *, u32),
80 void (*uevent)(struct acpi_device *, u32))
81{
82 acpi_lock_hp_context();
Rafael J. Wysockiba574dc2014-07-15 22:03:22 +020083 hp->notify = notify;
84 hp->uevent = uevent;
85 acpi_set_hp_context(adev, hp);
Rafael J. Wysocki5d513202014-02-22 00:48:31 +010086 acpi_unlock_hp_context();
87}
88EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
89
Rafael J. Wysockica589f92013-01-30 14:27:29 +010090int acpi_scan_add_handler(struct acpi_scan_handler *handler)
91{
Rafael J. Wysockid34afa92014-05-30 04:27:31 +020092 if (!handler)
Rafael J. Wysockica589f92013-01-30 14:27:29 +010093 return -EINVAL;
94
95 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
96 return 0;
97}
98
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +010099int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
100 const char *hotplug_profile_name)
101{
102 int error;
103
104 error = acpi_scan_add_handler(handler);
105 if (error)
106 return error;
107
108 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
109 return 0;
110}
111
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100112bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100113{
114 struct acpi_device_physical_node *pn;
115 bool offline = true;
116
Rafael J. Wysocki4c533c82015-04-18 01:25:46 +0200117 /*
118 * acpi_container_offline() calls this for all of the container's
119 * children under the container's physical_node_lock lock.
120 */
121 mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100122
123 list_for_each_entry(pn, &adev->physical_node_list, node)
124 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100125 if (uevent)
126 kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
127
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100128 offline = false;
129 break;
130 }
131
132 mutex_unlock(&adev->physical_node_lock);
133 return offline;
134}
135
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100136static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
137 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200138{
139 struct acpi_device *device = NULL;
140 struct acpi_device_physical_node *pn;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200141 bool second_pass = (bool)data;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200142 acpi_status status = AE_OK;
143
144 if (acpi_bus_get_device(handle, &device))
145 return AE_OK;
146
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100147 if (device->handler && !device->handler->hotplug.enabled) {
148 *ret_p = &device->dev;
149 return AE_SUPPORT;
150 }
151
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200152 mutex_lock(&device->physical_node_lock);
153
154 list_for_each_entry(pn, &device->physical_node_list, node) {
155 int ret;
156
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200157 if (second_pass) {
158 /* Skip devices offlined by the first pass. */
159 if (pn->put_online)
160 continue;
161 } else {
162 pn->put_online = false;
163 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200164 ret = device_offline(pn->dev);
165 if (acpi_force_hot_remove)
166 continue;
167
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200168 if (ret >= 0) {
169 pn->put_online = !ret;
170 } else {
171 *ret_p = pn->dev;
172 if (second_pass) {
173 status = AE_ERROR;
174 break;
175 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200176 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200177 }
178
179 mutex_unlock(&device->physical_node_lock);
180
181 return status;
182}
183
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100184static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
185 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200186{
187 struct acpi_device *device = NULL;
188 struct acpi_device_physical_node *pn;
189
190 if (acpi_bus_get_device(handle, &device))
191 return AE_OK;
192
193 mutex_lock(&device->physical_node_lock);
194
195 list_for_each_entry(pn, &device->physical_node_list, node)
196 if (pn->put_online) {
197 device_online(pn->dev);
198 pn->put_online = false;
199 }
200
201 mutex_unlock(&device->physical_node_lock);
202
203 return AE_OK;
204}
205
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100206static int acpi_scan_try_to_offline(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Yinghai Lu5993c462013-01-11 22:40:41 +0000208 acpi_handle handle = device->handle;
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100209 struct device *errdev = NULL;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100210 acpi_status status;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100211
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200212 /*
213 * Carry out two passes here and ignore errors in the first pass,
214 * because if the devices in question are memory blocks and
215 * CONFIG_MEMCG is set, one of the blocks may hold data structures
216 * that the other blocks depend on, but it is not known in advance which
217 * block holds them.
218 *
219 * If the first pass is successful, the second one isn't needed, though.
220 */
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100221 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
222 NULL, acpi_bus_offline, (void *)false,
223 (void **)&errdev);
224 if (status == AE_SUPPORT) {
225 dev_warn(errdev, "Offline disabled.\n");
226 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
227 acpi_bus_online, NULL, NULL, NULL);
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100228 return -EPERM;
229 }
230 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200231 if (errdev) {
232 errdev = NULL;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200233 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100234 NULL, acpi_bus_offline, (void *)true,
235 (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200236 if (!errdev || acpi_force_hot_remove)
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100237 acpi_bus_offline(handle, 0, (void *)true,
238 (void **)&errdev);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200239
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200240 if (errdev && !acpi_force_hot_remove) {
241 dev_warn(errdev, "Offline failed.\n");
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100242 acpi_bus_online(handle, 0, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200243 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100244 ACPI_UINT32_MAX, acpi_bus_online,
245 NULL, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200246 return -EBUSY;
247 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200248 }
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100249 return 0;
250}
251
252static int acpi_scan_hot_remove(struct acpi_device *device)
253{
254 acpi_handle handle = device->handle;
255 unsigned long long sta;
256 acpi_status status;
257
Tang Chendee15922014-08-08 10:30:45 +0800258 if (device->handler && device->handler->hotplug.demand_offline
259 && !acpi_force_hot_remove) {
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100260 if (!acpi_scan_is_offline(device, true))
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100261 return -EBUSY;
262 } else {
263 int error = acpi_scan_try_to_offline(device);
264 if (error)
265 return error;
266 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200267
Zhang Rui26d46862008-04-29 02:35:48 -0400268 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100269 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400270
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100271 acpi_bus_trim(device);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200272
Jiang Liu7d2421f2013-06-29 00:24:40 +0800273 acpi_evaluate_lck(handle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /*
275 * TBD: _EJD support.
276 */
Jiang Liu7d2421f2013-06-29 00:24:40 +0800277 status = acpi_evaluate_ej0(handle);
278 if (status == AE_NOT_FOUND)
279 return -ENODEV;
280 else if (ACPI_FAILURE(status))
281 return -EIO;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100282
Toshi Kani882fd122013-03-13 19:29:26 +0000283 /*
284 * Verify if eject was indeed successful. If not, log an error
285 * message. No need to call _OST since _EJ0 call was made OK.
286 */
287 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
288 if (ACPI_FAILURE(status)) {
289 acpi_handle_warn(handle,
290 "Status check after eject failed (0x%x)\n", status);
291 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
292 acpi_handle_warn(handle,
293 "Eject incomplete - status 0x%llx\n", sta);
294 }
295
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100296 return 0;
297}
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100298
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100299static int acpi_scan_device_not_present(struct acpi_device *adev)
300{
301 if (!acpi_device_enumerated(adev)) {
302 dev_warn(&adev->dev, "Still not present\n");
303 return -EALREADY;
304 }
305 acpi_bus_trim(adev);
306 return 0;
307}
308
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100309static int acpi_scan_device_check(struct acpi_device *adev)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100310{
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200311 int error;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100312
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100313 acpi_bus_get_status(adev);
314 if (adev->status.present || adev->status.functional) {
315 /*
316 * This function is only called for device objects for which
317 * matching scan handlers exist. The only situation in which
318 * the scan handler is not attached to this device object yet
319 * is when the device has just appeared (either it wasn't
320 * present at all before or it was removed and then added
321 * again).
322 */
323 if (adev->handler) {
324 dev_warn(&adev->dev, "Already enumerated\n");
325 return -EALREADY;
326 }
327 error = acpi_bus_scan(adev->handle);
328 if (error) {
329 dev_warn(&adev->dev, "Namespace scan failure\n");
330 return error;
331 }
332 if (!adev->handler) {
333 dev_warn(&adev->dev, "Enumeration failure\n");
334 error = -ENODEV;
335 }
336 } else {
337 error = acpi_scan_device_not_present(adev);
338 }
339 return error;
340}
341
342static int acpi_scan_bus_check(struct acpi_device *adev)
343{
344 struct acpi_scan_handler *handler = adev->handler;
345 struct acpi_device *child;
346 int error;
347
348 acpi_bus_get_status(adev);
349 if (!(adev->status.present || adev->status.functional)) {
350 acpi_scan_device_not_present(adev);
351 return 0;
352 }
353 if (handler && handler->hotplug.scan_dependent)
354 return handler->hotplug.scan_dependent(adev);
355
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100356 error = acpi_bus_scan(adev->handle);
357 if (error) {
358 dev_warn(&adev->dev, "Namespace scan failure\n");
359 return error;
360 }
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100361 list_for_each_entry(child, &adev->children, node) {
362 error = acpi_scan_bus_check(child);
363 if (error)
364 return error;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100365 }
366 return 0;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100367}
368
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100369static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
370{
371 switch (type) {
372 case ACPI_NOTIFY_BUS_CHECK:
373 return acpi_scan_bus_check(adev);
374 case ACPI_NOTIFY_DEVICE_CHECK:
375 return acpi_scan_device_check(adev);
376 case ACPI_NOTIFY_EJECT_REQUEST:
377 case ACPI_OST_EC_OSPM_EJECT:
Rafael J. Wysockidd2151b2014-02-04 00:44:02 +0100378 if (adev->handler && !adev->handler->hotplug.enabled) {
379 dev_info(&adev->dev, "Eject disabled\n");
380 return -EPERM;
381 }
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100382 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
383 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100384 return acpi_scan_hot_remove(adev);
385 }
386 return -EINVAL;
387}
388
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +0100389void acpi_device_hotplug(struct acpi_device *adev, u32 src)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100390{
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100391 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100392 int error = -ENODEV;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100393
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200394 lock_device_hotplug();
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200395 mutex_lock(&acpi_scan_lock);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100396
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100397 /*
398 * The device object's ACPI handle cannot become invalid as long as we
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100399 * are holding acpi_scan_lock, but it might have become invalid before
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100400 * that lock was acquired.
401 */
402 if (adev->handle == INVALID_ACPI_HANDLE)
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100403 goto err_out;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100404
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100405 if (adev->flags.is_dock_station) {
406 error = dock_notify(adev, src);
407 } else if (adev->flags.hotplug_notify) {
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100408 error = acpi_generic_hotplug_event(adev, src);
Rafael J. Wysockidd2151b2014-02-04 00:44:02 +0100409 if (error == -EPERM) {
410 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
411 goto err_out;
412 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100413 } else {
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100414 int (*notify)(struct acpi_device *, u32);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100415
416 acpi_lock_hp_context();
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100417 notify = adev->hp ? adev->hp->notify : NULL;
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100418 acpi_unlock_hp_context();
419 /*
420 * There may be additional notify handlers for device objects
421 * without the .event() callback, so ignore them here.
422 */
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100423 if (notify)
424 error = notify(adev, src);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100425 else
426 goto out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100427 }
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100428 if (!error)
429 ost_code = ACPI_OST_SC_SUCCESS;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100430
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100431 err_out:
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100432 acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100433
434 out:
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +0100435 acpi_bus_put_acpi_device(adev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100436 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200437 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100438}
439
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100440static void acpi_free_power_resources_lists(struct acpi_device *device)
441{
442 int i;
443
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100444 if (device->wakeup.flags.valid)
445 acpi_power_resources_list_free(&device->wakeup.resources);
446
Rafael J. Wysocki1b1f3e12015-01-01 23:38:28 +0100447 if (!device->power.flags.power_resources)
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100448 return;
449
450 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
451 struct acpi_device_power_state *ps = &device->power.states[i];
452 acpi_power_resources_list_free(&ps->resources);
453 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600454}
455
Patrick Mochel1890a972006-12-07 20:56:31 +0800456static void acpi_device_release(struct device *dev)
457{
458 struct acpi_device *acpi_dev = to_acpi_device(dev);
459
Mika Westerbergffdcd952014-10-21 13:33:55 +0200460 acpi_free_properties(acpi_dev);
Toshi Kanic0af4172013-03-04 21:30:42 +0000461 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100462 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800463 kfree(acpi_dev);
464}
465
Rafael J. Wysockid7831562013-11-22 21:52:12 +0100466static void acpi_device_del(struct acpi_device *device)
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +0200467{
Lukas Wunnerca9dc8d2015-11-25 21:19:55 +0100468 struct acpi_device_bus_id *acpi_device_bus_id;
469
Rafael J. Wysockid7831562013-11-22 21:52:12 +0100470 mutex_lock(&acpi_device_lock);
471 if (device->parent)
472 list_del(&device->node);
473
Lukas Wunnerca9dc8d2015-11-25 21:19:55 +0100474 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
475 if (!strcmp(acpi_device_bus_id->bus_id,
476 acpi_device_hid(device))) {
477 if (acpi_device_bus_id->instance_no > 0)
478 acpi_device_bus_id->instance_no--;
479 else {
480 list_del(&acpi_device_bus_id->node);
481 kfree(acpi_device_bus_id);
482 }
483 break;
484 }
485
Rafael J. Wysockid7831562013-11-22 21:52:12 +0100486 list_del(&device->wakeup_list);
487 mutex_unlock(&acpi_device_lock);
488
489 acpi_power_add_remove_device(device, false);
490 acpi_device_remove_files(device);
491 if (device->remove)
492 device->remove(device);
493
494 device_del(&device->dev);
495}
496
Octavian Purdila68bdb672016-07-08 19:13:09 +0300497static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);
498
Rafael J. Wysockid7831562013-11-22 21:52:12 +0100499static LIST_HEAD(acpi_device_del_list);
500static DEFINE_MUTEX(acpi_device_del_lock);
501
502static void acpi_device_del_work_fn(struct work_struct *work_not_used)
503{
504 for (;;) {
505 struct acpi_device *adev;
506
507 mutex_lock(&acpi_device_del_lock);
508
509 if (list_empty(&acpi_device_del_list)) {
510 mutex_unlock(&acpi_device_del_lock);
511 break;
512 }
513 adev = list_first_entry(&acpi_device_del_list,
514 struct acpi_device, del_list);
515 list_del(&adev->del_list);
516
517 mutex_unlock(&acpi_device_del_lock);
518
Octavian Purdila68bdb672016-07-08 19:13:09 +0300519 blocking_notifier_call_chain(&acpi_reconfig_chain,
520 ACPI_RECONFIG_DEVICE_REMOVE, adev);
521
Rafael J. Wysockid7831562013-11-22 21:52:12 +0100522 acpi_device_del(adev);
523 /*
524 * Drop references to all power resources that might have been
525 * used by the device.
526 */
527 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
528 put_device(&adev->dev);
529 }
530}
531
532/**
533 * acpi_scan_drop_device - Drop an ACPI device object.
534 * @handle: Handle of an ACPI namespace node, not used.
535 * @context: Address of the ACPI device object to drop.
536 *
537 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
538 * namespace node the device object pointed to by @context is attached to.
539 *
540 * The unregistration is carried out asynchronously to avoid running
541 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
542 * ensure the correct ordering (the device objects must be unregistered in the
543 * same order in which the corresponding namespace nodes are deleted).
544 */
545static void acpi_scan_drop_device(acpi_handle handle, void *context)
546{
547 static DECLARE_WORK(work, acpi_device_del_work_fn);
548 struct acpi_device *adev = context;
549
550 mutex_lock(&acpi_device_del_lock);
551
552 /*
553 * Use the ACPI hotplug workqueue which is ordered, so this work item
554 * won't run after any hotplug work items submitted subsequently. That
555 * prevents attempts to register device objects identical to those being
556 * deleted from happening concurrently (such attempts result from
557 * hotplug events handled via the ACPI hotplug workqueue). It also will
558 * run after all of the work items submitted previosuly, which helps
559 * those work items to ensure that they are not accessing stale device
560 * objects.
561 */
562 if (list_empty(&acpi_device_del_list))
563 acpi_queue_hotplug_work(&work);
564
565 list_add_tail(&adev->del_list, &acpi_device_del_list);
566 /* Make acpi_ns_validate_handle() return NULL for this handle. */
567 adev->handle = INVALID_ACPI_HANDLE;
568
569 mutex_unlock(&acpi_device_del_lock);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +0200570}
571
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +0100572static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
573 void (*callback)(void *))
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +0200574{
575 acpi_status status;
576
577 if (!device)
578 return -EINVAL;
579
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +0100580 status = acpi_get_data_full(handle, acpi_scan_drop_device,
581 (void **)device, callback);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +0200582 if (ACPI_FAILURE(status) || !*device) {
583 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
584 handle));
585 return -ENODEV;
586 }
587 return 0;
588}
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +0100589
590int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
591{
592 return acpi_get_device_data(handle, device, NULL);
593}
Rafael J. Wysocki65859252013-10-01 23:02:43 +0200594EXPORT_SYMBOL(acpi_bus_get_device);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +0200595
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +0100596static void get_acpi_device(void *dev)
597{
598 if (dev)
599 get_device(&((struct acpi_device *)dev)->dev);
600}
601
602struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
603{
604 struct acpi_device *adev = NULL;
605
606 acpi_get_device_data(handle, &adev, get_acpi_device);
607 return adev;
608}
609
610void acpi_bus_put_acpi_device(struct acpi_device *adev)
611{
612 put_device(&adev->dev);
613}
614
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100615int acpi_device_add(struct acpi_device *device,
616 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800618 int result;
619 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
620 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000621
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100622 if (device->handle) {
623 acpi_status status;
624
Rafael J. Wysockid7831562013-11-22 21:52:12 +0100625 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100626 device);
627 if (ACPI_FAILURE(status)) {
628 acpi_handle_err(device->handle,
629 "Unable to attach device data\n");
630 return -ENODEV;
631 }
632 }
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /*
635 * Linkage
636 * -------
637 * Link this device to its parent and siblings.
638 */
639 INIT_LIST_HEAD(&device->children);
640 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800642 INIT_LIST_HEAD(&device->physical_node_list);
Rafael J. Wysockid7831562013-11-22 21:52:12 +0100643 INIT_LIST_HEAD(&device->del_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800644 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800646 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
647 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100648 pr_err(PREFIX "Memory allocation error\n");
649 result = -ENOMEM;
650 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800651 }
652
Shaohua Li90905892009-04-07 10:24:29 +0800653 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800654 /*
655 * Find suitable bus_id and instance number in acpi_bus_id_list
656 * If failed, create one and link it into acpi_bus_id_list
657 */
658 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600659 if (!strcmp(acpi_device_bus_id->bus_id,
660 acpi_device_hid(device))) {
661 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800662 found = 1;
663 kfree(new_bus_id);
664 break;
665 }
666 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600667 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800668 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600669 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800670 acpi_device_bus_id->instance_no = 0;
671 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
672 }
Kay Sievers07944692008-10-30 01:18:59 +0100673 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 +0800674
Len Brown33b57152008-12-15 22:09:26 -0500675 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (device->wakeup.flags.valid)
679 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800680 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Patrick Mochel1890a972006-12-07 20:56:31 +0800682 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000683 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800684 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100685 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100686 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600687 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600688 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100689 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800690 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800691
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800692 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600693 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100694 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
695 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800696
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800697 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100698
699 err:
Shaohua Li90905892009-04-07 10:24:29 +0800700 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500701 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800702 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800703 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800704 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100705
706 err_detach:
Rafael J. Wysockid7831562013-11-22 21:52:12 +0100707 acpi_detach_data(device->handle, acpi_scan_drop_device);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800708 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Zhang Rui9e89dde2006-12-07 20:56:16 +0800711/* --------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 Device Enumeration
713 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000714static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
715{
Rafael J. Wysocki456de892013-01-31 20:57:40 +0100716 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000717 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000718
719 /*
720 * Fixed hardware devices do not appear in the namespace and do not
721 * have handles, but we fabricate acpi_devices for them, so we have
722 * to deal with them specially.
723 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +0100724 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000725 return acpi_root;
726
727 do {
728 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000729 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +0100730 return status == AE_NULL_ENTRY ? NULL : acpi_root;
731 } while (acpi_bus_get_device(handle, &device));
732 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000733}
734
Len Brownc8f7a62c2006-07-09 17:22:28 -0400735acpi_status
736acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
737{
738 acpi_status status;
739 acpi_handle tmp;
740 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
741 union acpi_object *obj;
742
743 status = acpi_get_handle(handle, "_EJD", &tmp);
744 if (ACPI_FAILURE(status))
745 return status;
746
747 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
748 if (ACPI_SUCCESS(status)) {
749 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100750 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
751 ejd);
Len Brownc8f7a62c2006-07-09 17:22:28 -0400752 kfree(buffer.pointer);
753 }
754 return status;
755}
756EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
757
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100758static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
759 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100761 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
762 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100764 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100765 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100767 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100768 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100770 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100772 /* _PRW */
773 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
774 if (ACPI_FAILURE(status)) {
775 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100776 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100777 }
778
779 package = (union acpi_object *)buffer.pointer;
780
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100781 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100782 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100785 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100786 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (element->type == ACPI_TYPE_PACKAGE) {
789 if ((element->package.count < 2) ||
790 (element->package.elements[0].type !=
791 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100792 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100793 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100794
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100795 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100797 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 (u32) element->package.elements[1].integer.value;
799 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100800 wakeup->gpe_device = NULL;
801 wakeup->gpe_number = element->integer.value;
802 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100803 goto out;
804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100807 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100808 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100809
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100810 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100812 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
813 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100814 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100816 if (!list_empty(&wakeup->resources)) {
817 int sleep_state;
818
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100819 err = acpi_power_wakeup_list_init(&wakeup->resources,
820 &sleep_state);
821 if (err) {
822 acpi_handle_warn(handle, "Retrieving current states "
823 "of wakeup power resources failed\n");
824 acpi_power_resources_list_free(&wakeup->resources);
825 goto out;
826 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100827 if (sleep_state < wakeup->sleep_state) {
828 acpi_handle_warn(handle, "Overriding _PRW sleep state "
829 "(S%d) by S%d from power resources\n",
830 (int)wakeup->sleep_state, sleep_state);
831 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
Rafael J. Wysocki98746472010-07-08 00:43:36 +0200834
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100835 out:
836 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100837 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +0200840static void acpi_wakeup_gpe_init(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Mathias Krause0519ade2015-06-13 14:26:58 +0200842 static const struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +0200843 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +0100844 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +0200845 {"PNP0C0E", 0},
846 {"", 0},
847 };
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +0200848 struct acpi_device_wakeup *wakeup = &device->wakeup;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100849 acpi_status status;
850 acpi_event_status event_status;
851
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +0200852 wakeup->flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100853
854 /* Power button, Lid switch always enable wakeup */
855 if (!acpi_match_device_ids(device, button_device_ids)) {
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +0200856 wakeup->flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +0100857 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
858 /* Do not use Lid/sleep button for S5 wakeup */
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +0200859 if (wakeup->sleep_state == ACPI_STATE_S5)
860 wakeup->sleep_state = ACPI_STATE_S4;
Zhang Ruib7e38302012-12-04 23:23:16 +0100861 }
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +0200862 acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +0100863 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100864 return;
865 }
866
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +0200867 acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
868 wakeup->gpe_number);
869 status = acpi_get_gpe_status(wakeup->gpe_device, wakeup->gpe_number,
870 &event_status);
871 if (ACPI_FAILURE(status))
872 return;
873
Lv Zheng2f857232014-10-10 10:40:05 +0800874 wakeup->flags.run_wake = !!(event_status & ACPI_EVENT_FLAG_HAS_HANDLER);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100875}
876
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100877static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100878{
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100879 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200880
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100881 /* Presence of _PRW indicates wake capable */
Jiang Liu952c63e2013-06-29 00:24:38 +0800882 if (!acpi_has_method(device->handle, "_PRW"))
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100883 return;
884
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100885 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
886 &device->wakeup);
887 if (err) {
888 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100889 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200893 device->wakeup.prepare_count = 0;
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +0200894 acpi_wakeup_gpe_init(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800895 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
896 * system for the ACPI device with the _PRW object.
897 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
898 * So it is necessary to call _DSW object first. Only when it is not
899 * present will the _PSW object used.
900 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100901 err = acpi_device_sleep_wake(device, 0, 0, 0);
902 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200903 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
904 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
Rafael J. Wysockif33ce562013-01-17 14:11:06 +0100907static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +0100909 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100910 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
911 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +0100912 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800913
Rafael J. Wysockif33ce562013-01-17 14:11:06 +0100914 INIT_LIST_HEAD(&ps->resources);
915
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100916 /* Evaluate "_PRx" to get referenced power resources */
917 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
918 if (ACPI_SUCCESS(status)) {
919 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +0100920
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100921 if (buffer.length && package
922 && package->type == ACPI_TYPE_PACKAGE
923 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100924 int err = acpi_extract_power_resources(package, 0,
925 &ps->resources);
926 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100927 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +0100928 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100929 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +0100930 }
931
932 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100933 pathname[2] = 'S';
Jiang Liu952c63e2013-06-29 00:24:38 +0800934 if (acpi_has_method(device->handle, pathname))
Rafael J. Wysockif33ce562013-01-17 14:11:06 +0100935 ps->flags.explicit_set = 1;
936
Rafael J. Wysocki20dacb72015-05-16 01:55:35 +0200937 /* State is valid if there are means to put the device into it. */
938 if (!list_empty(&ps->resources) || ps->flags.explicit_set)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +0100939 ps->flags.valid = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +0100940
941 ps->power = -1; /* Unknown - driver assigned */
942 ps->latency = -1; /* Unknown - driver assigned */
943}
944
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100945static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +0100947 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100949 /* Presence of _PS0|_PR0 indicates 'power manageable' */
Jiang Liu952c63e2013-06-29 00:24:38 +0800950 if (!acpi_has_method(device->handle, "_PS0") &&
951 !acpi_has_method(device->handle, "_PR0"))
952 return;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100953
954 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800957 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800959 if (acpi_has_method(device->handle, "_PSC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800960 device->power.flags.explicit_get = 1;
Rafael J. Wysockif25c0ae2014-05-17 00:18:13 +0200961
Jiang Liu952c63e2013-06-29 00:24:38 +0800962 if (acpi_has_method(device->handle, "_IRC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800963 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Rafael J. Wysockif25c0ae2014-05-17 00:18:13 +0200965 if (acpi_has_method(device->handle, "_DSW"))
966 device->power.flags.dsw_present = 1;
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800969 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +0100971 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
972 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100974 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Rafael J. Wysocki20dacb72015-05-16 01:55:35 +0200975 if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
976 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Rafael J. Wysocki20dacb72015-05-16 01:55:35 +0200978 /* Set defaults for D0 and D3hot states (always valid) */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800979 device->power.states[ACPI_STATE_D0].flags.valid = 1;
980 device->power.states[ACPI_STATE_D0].power = 100;
Rafael J. Wysocki20dacb72015-05-16 01:55:35 +0200981 device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
Aaron Lu1399dfc2012-11-21 23:33:40 +0100982
Rafael J. Wysocki1b1f3e12015-01-01 23:38:28 +0100983 if (acpi_bus_init_power(device))
Rafael J. Wysockib3785492013-02-01 23:43:02 +0100984 device->flags.power_manageable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100987static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /* Presence of _STA indicates 'dynamic_status' */
Jiang Liu952c63e2013-06-29 00:24:38 +0800990 if (acpi_has_method(device->handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 device->flags.dynamic_status = 1;
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 /* Presence of _RMV indicates 'removable' */
Jiang Liu952c63e2013-06-29 00:24:38 +0800994 if (acpi_has_method(device->handle, "_RMV"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 device->flags.removable = 1;
996
997 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
Jiang Liu952c63e2013-06-29 00:24:38 +0800998 if (acpi_has_method(device->handle, "_EJD") ||
999 acpi_has_method(device->handle, "_EJ0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 device->flags.ejectable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001003static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Len Brown4be44fc2005-08-05 00:44:28 -04001005 char bus_id[5] = { '?', 0 };
1006 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1007 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 /*
1010 * Bus ID
1011 * ------
1012 * The device's Bus ID is simply the object name.
1013 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1014 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001015 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001017 return;
1018 }
1019
1020 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 case ACPI_BUS_TYPE_POWER_BUTTON:
1022 strcpy(device->pnp.bus_id, "PWRF");
1023 break;
1024 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1025 strcpy(device->pnp.bus_id, "SLPF");
1026 break;
1027 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001028 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 /* Clean up trailing underscores (if any) */
1030 for (i = 3; i > 1; i--) {
1031 if (bus_id[i] == '_')
1032 bus_id[i] = '\0';
1033 else
1034 break;
1035 }
1036 strcpy(device->pnp.bus_id, bus_id);
1037 break;
1038 }
1039}
1040
Zhang Rui54735262007-01-11 02:09:09 -05001041/*
Jiang Liuebf4df82013-06-29 00:24:41 +08001042 * acpi_ata_match - see if an acpi object is an ATA device
1043 *
1044 * If an acpi object has one of the ACPI ATA methods defined,
1045 * then we can safely call it an ATA device.
1046 */
1047bool acpi_ata_match(acpi_handle handle)
1048{
1049 return acpi_has_method(handle, "_GTF") ||
1050 acpi_has_method(handle, "_GTM") ||
1051 acpi_has_method(handle, "_STM") ||
1052 acpi_has_method(handle, "_SDD");
1053}
1054
1055/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001056 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001057 *
1058 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1059 * then we can safely call it an ejectable drive bay
1060 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001061bool acpi_bay_match(acpi_handle handle)
Toshi Kanid4e1a692013-03-04 21:30:41 +00001062{
Zhang Rui54735262007-01-11 02:09:09 -05001063 acpi_handle phandle;
1064
Jiang Liu952c63e2013-06-29 00:24:38 +08001065 if (!acpi_has_method(handle, "_EJ0"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001066 return false;
1067 if (acpi_ata_match(handle))
1068 return true;
1069 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1070 return false;
Zhang Rui54735262007-01-11 02:09:09 -05001071
Jiang Liuebf4df82013-06-29 00:24:41 +08001072 return acpi_ata_match(phandle);
Zhang Rui54735262007-01-11 02:09:09 -05001073}
1074
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001075bool acpi_device_is_battery(struct acpi_device *adev)
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001076{
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001077 struct acpi_hardware_id *hwid;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001078
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001079 list_for_each_entry(hwid, &adev->pnp.ids, list)
1080 if (!strcmp("PNP0C0A", hwid->id))
1081 return true;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001082
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001083 return false;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001084}
1085
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001086static bool is_ejectable_bay(struct acpi_device *adev)
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001087{
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001088 acpi_handle handle = adev->handle;
1089
1090 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001091 return true;
1092
1093 return acpi_bay_match(handle);
1094}
1095
Frank Seidel3620f2f2007-12-07 13:20:34 +01001096/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001097 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001098 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001099bool acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001100{
Jiang Liuebf4df82013-06-29 00:24:41 +08001101 return acpi_has_method(handle, "_DCK");
Frank Seidel3620f2f2007-12-07 13:20:34 +01001102}
1103
Hans de Goedeadc8bb82015-06-16 16:27:45 +02001104static acpi_status
1105acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
1106 void **return_value)
1107{
1108 long *cap = context;
1109
1110 if (acpi_has_method(handle, "_BCM") &&
1111 acpi_has_method(handle, "_BCL")) {
1112 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found generic backlight "
1113 "support\n"));
1114 *cap |= ACPI_VIDEO_BACKLIGHT;
1115 if (!acpi_has_method(handle, "_BQC"))
1116 printk(KERN_WARNING FW_BUG PREFIX "No _BQC method, "
1117 "cannot determine initial brightness\n");
1118 /* We have backlight support, no need to scan further */
1119 return AE_CTRL_TERMINATE;
1120 }
1121 return 0;
1122}
1123
1124/* Returns true if the ACPI object is a video device which can be
1125 * handled by video.ko.
1126 * The device will get a Linux specific CID added in scan.c to
1127 * identify the device as an ACPI graphics device
1128 * Be aware that the graphics device may not be physically present
1129 * Use acpi_video_get_capabilities() to detect general ACPI video
1130 * capabilities of present cards
1131 */
1132long acpi_is_video_device(acpi_handle handle)
1133{
1134 long video_caps = 0;
1135
1136 /* Is this device able to support video switching ? */
1137 if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
1138 video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
1139
1140 /* Is this device able to retrieve a video ROM ? */
1141 if (acpi_has_method(handle, "_ROM"))
1142 video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
1143
1144 /* Is this device able to configure which video head to be POSTed ? */
1145 if (acpi_has_method(handle, "_VPO") &&
1146 acpi_has_method(handle, "_GPD") &&
1147 acpi_has_method(handle, "_SPD"))
1148 video_caps |= ACPI_VIDEO_DEVICE_POSTING;
1149
1150 /* Only check for backlight functionality if one of the above hit. */
1151 if (video_caps)
1152 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1153 ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
1154 &video_caps, NULL);
1155
1156 return video_caps;
1157}
1158EXPORT_SYMBOL(acpi_is_video_device);
1159
Thomas Renninger620e1122010-10-01 10:54:00 +02001160const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001161{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001162 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001163
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001164 if (list_empty(&device->pnp.ids))
1165 return dummy_hid;
1166
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001167 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1168 return hid->id;
1169}
1170EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001171
Toshi Kanid4e1a692013-03-04 21:30:41 +00001172static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001173{
1174 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001175
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001176 id = kmalloc(sizeof(*id), GFP_KERNEL);
1177 if (!id)
1178 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001179
Rasmus Villemoes6a0d12e2015-09-09 23:59:43 +02001180 id->id = kstrdup_const(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001181 if (!id->id) {
1182 kfree(id);
1183 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001184 }
1185
Toshi Kanid4e1a692013-03-04 21:30:41 +00001186 list_add_tail(&id->list, &pnp->ids);
1187 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001188}
1189
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001190/*
1191 * Old IBM workstations have a DSDT bug wherein the SMBus object
1192 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1193 * prefix. Work around this.
1194 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001195static bool acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001196{
Jiang Liuebf4df82013-06-29 00:24:41 +08001197 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1198 struct acpi_buffer path = { sizeof(node_name), node_name };
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001199
1200 if (!dmi_name_in_vendors("IBM"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001201 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001202
1203 /* Look for SMBS object */
Jiang Liuebf4df82013-06-29 00:24:41 +08001204 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1205 strcmp("SMBS", path.pointer))
1206 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001207
1208 /* Does it have the necessary (but misnamed) methods? */
Jiang Liu952c63e2013-06-29 00:24:38 +08001209 if (acpi_has_method(handle, "SBI") &&
1210 acpi_has_method(handle, "SBR") &&
1211 acpi_has_method(handle, "SBW"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001212 return true;
1213
1214 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001215}
1216
Jiang Liuae3caa82014-02-19 14:02:19 +08001217static bool acpi_object_is_system_bus(acpi_handle handle)
1218{
1219 acpi_handle tmp;
1220
1221 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1222 tmp == handle)
1223 return true;
1224 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1225 tmp == handle)
1226 return true;
1227
1228 return false;
1229}
1230
Toshi Kanic0af4172013-03-04 21:30:42 +00001231static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1232 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Len Brown4be44fc2005-08-05 00:44:28 -04001234 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001235 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001236 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001237 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Toshi Kanic0af4172013-03-04 21:30:42 +00001239 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00001241 if (handle == ACPI_ROOT_OBJECT) {
1242 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001243 break;
1244 }
1245
Toshi Kanic0af4172013-03-04 21:30:42 +00001246 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001248 pr_err(PREFIX "%s: Error reading device info\n",
1249 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return;
1251 }
1252
Rafael J. Wysocki549e6842014-05-30 04:26:18 +02001253 if (info->valid & ACPI_VALID_HID) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001254 acpi_add_id(pnp, info->hardware_id.string);
Rafael J. Wysocki549e6842014-05-30 04:26:18 +02001255 pnp->type.platform_id = 1;
1256 }
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001257 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001258 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001259 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00001260 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001263 pnp->bus_address = info->address;
1264 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Lv Zhengccf78042012-10-30 14:41:07 +01001266 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001267 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01001268 GFP_KERNEL);
Suthikulpanit, Suravee26095a02015-07-07 01:55:20 +02001269 if (info->valid & ACPI_VALID_CLS)
1270 acpi_add_id(pnp, info->class_code.string);
Zhang Ruiae843332006-12-07 20:57:10 +08001271
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001272 kfree(info);
1273
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001274 /*
1275 * Some devices don't reliably have _HIDs & _CIDs, so add
1276 * synthetic HIDs to make sure drivers can find them.
1277 */
Toshi Kanic0af4172013-03-04 21:30:42 +00001278 if (acpi_is_video_device(handle))
1279 acpi_add_id(pnp, ACPI_VIDEO_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001280 else if (acpi_bay_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001281 acpi_add_id(pnp, ACPI_BAY_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001282 else if (acpi_dock_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001283 acpi_add_id(pnp, ACPI_DOCK_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001284 else if (acpi_ibm_smbus_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001285 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
Jiang Liuae3caa82014-02-19 14:02:19 +08001286 else if (list_empty(&pnp->ids) &&
1287 acpi_object_is_system_bus(handle)) {
1288 /* \_SB, \_TZ, LNXSYBUS */
1289 acpi_add_id(pnp, ACPI_BUS_HID);
Toshi Kanic0af4172013-03-04 21:30:42 +00001290 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1291 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001292 }
Zhang Rui54735262007-01-11 02:09:09 -05001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 break;
1295 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00001296 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 break;
1298 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00001299 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00001302 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 break;
1304 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001305 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 break;
1307 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001308 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 break;
1310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
Toshi Kanic0af4172013-03-04 21:30:42 +00001313void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1314{
1315 struct acpi_hardware_id *id, *tmp;
1316
1317 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
Rasmus Villemoes6a0d12e2015-09-09 23:59:43 +02001318 kfree_const(id->id);
Toshi Kanic0af4172013-03-04 21:30:42 +00001319 kfree(id);
1320 }
1321 kfree(pnp->unique_id);
1322}
1323
Suthikulpanit, Suraveeb84f1962015-10-28 15:50:48 -07001324/**
1325 * acpi_dma_supported - Check DMA support for the specified device.
1326 * @adev: The pointer to acpi device
1327 *
1328 * Return false if DMA is not supported. Otherwise, return true
1329 */
1330bool acpi_dma_supported(struct acpi_device *adev)
1331{
1332 if (!adev)
1333 return false;
1334
1335 if (adev->flags.cca_seen)
1336 return true;
1337
1338 /*
1339 * Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1340 * DMA on "Intel platforms". Presumably that includes all x86 and
1341 * ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1342 */
1343 if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1344 return true;
1345
1346 return false;
1347}
1348
1349/**
1350 * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1351 * @adev: The pointer to acpi device
1352 *
1353 * Return enum dev_dma_attr.
1354 */
1355enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1356{
1357 if (!acpi_dma_supported(adev))
1358 return DEV_DMA_NOT_SUPPORTED;
1359
1360 if (adev->flags.coherent_dma)
1361 return DEV_DMA_COHERENT;
1362 else
1363 return DEV_DMA_NON_COHERENT;
1364}
1365
Suthikulpanit, Suraveed0562672015-06-10 11:08:52 -05001366static void acpi_init_coherency(struct acpi_device *adev)
1367{
1368 unsigned long long cca = 0;
1369 acpi_status status;
1370 struct acpi_device *parent = adev->parent;
1371
1372 if (parent && parent->flags.cca_seen) {
1373 /*
1374 * From ACPI spec, OSPM will ignore _CCA if an ancestor
1375 * already saw one.
1376 */
1377 adev->flags.cca_seen = 1;
1378 cca = parent->flags.coherent_dma;
1379 } else {
1380 status = acpi_evaluate_integer(adev->handle, "_CCA",
1381 NULL, &cca);
1382 if (ACPI_SUCCESS(status))
1383 adev->flags.cca_seen = 1;
1384 else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1385 /*
1386 * If architecture does not specify that _CCA is
1387 * required for DMA-able devices (e.g. x86),
1388 * we default to _CCA=1.
1389 */
1390 cca = 1;
1391 else
1392 acpi_handle_debug(adev->handle,
1393 "ACPI device is missing _CCA.\n");
1394 }
1395
1396 adev->flags.coherent_dma = cca;
1397}
1398
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001399void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1400 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001402 INIT_LIST_HEAD(&device->pnp.ids);
1403 device->device_type = type;
1404 device->handle = handle;
1405 device->parent = acpi_bus_get_parent(handle);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001406 device->fwnode.type = FWNODE_ACPI;
Rafael J. Wysocki25db1152013-11-22 21:56:06 +01001407 acpi_set_device_status(device, sta);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001408 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00001409 acpi_set_pnp_ids(handle, &device->pnp, type);
Mika Westerbergffdcd952014-10-21 13:33:55 +02001410 acpi_init_properties(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001411 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001412 device->flags.match_driver = false;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001413 device->flags.initialized = true;
Octavian Purdila10c7e202016-07-08 19:13:08 +03001414 acpi_device_clear_enumerated(device);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001415 device_initialize(&device->dev);
1416 dev_set_uevent_suppress(&device->dev, true);
Suthikulpanit, Suraveed0562672015-06-10 11:08:52 -05001417 acpi_init_coherency(device);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001418}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001419
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001420void acpi_device_add_finalize(struct acpi_device *device)
1421{
1422 dev_set_uevent_suppress(&device->dev, false);
1423 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001426static int acpi_add_single_object(struct acpi_device **child,
1427 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001428 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001430 int result;
1431 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001432 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Burman Yan36bcbec2006-12-19 12:56:11 -08001434 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001436 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001437 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001440 acpi_init_device_object(device, handle, type, sta);
1441 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001442 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001444 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001445 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001446 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001447 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001450 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001451 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001452 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1453 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1454 dev_name(&device->dev), (char *) buffer.pointer,
1455 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1456 kfree(buffer.pointer);
1457 *child = device;
1458 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001459}
1460
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001461static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1462 unsigned long long *sta)
1463{
1464 acpi_status status;
1465 acpi_object_type acpi_type;
1466
1467 status = acpi_get_type(handle, &acpi_type);
1468 if (ACPI_FAILURE(status))
1469 return -ENODEV;
1470
1471 switch (acpi_type) {
1472 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1473 case ACPI_TYPE_DEVICE:
1474 *type = ACPI_BUS_TYPE_DEVICE;
1475 status = acpi_bus_get_status_handle(handle, sta);
1476 if (ACPI_FAILURE(status))
Aaron Lue3990372015-11-19 11:05:25 +08001477 *sta = 0;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001478 break;
1479 case ACPI_TYPE_PROCESSOR:
1480 *type = ACPI_BUS_TYPE_PROCESSOR;
1481 status = acpi_bus_get_status_handle(handle, sta);
1482 if (ACPI_FAILURE(status))
1483 return -ENODEV;
1484 break;
1485 case ACPI_TYPE_THERMAL:
1486 *type = ACPI_BUS_TYPE_THERMAL;
1487 *sta = ACPI_STA_DEFAULT;
1488 break;
1489 case ACPI_TYPE_POWER:
1490 *type = ACPI_BUS_TYPE_POWER;
1491 *sta = ACPI_STA_DEFAULT;
1492 break;
1493 default:
1494 return -ENODEV;
1495 }
1496
1497 return 0;
1498}
1499
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001500bool acpi_device_is_present(struct acpi_device *adev)
1501{
1502 if (adev->status.present || adev->status.functional)
1503 return true;
1504
1505 adev->flags.initialized = false;
1506 return false;
1507}
1508
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001509static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
Rasmus Villemoes844142c2015-09-09 23:59:42 +02001510 const char *idstr,
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001511 const struct acpi_device_id **matchid)
1512{
1513 const struct acpi_device_id *devid;
1514
Rafael J. Wysockiaca0a4e2014-05-30 04:21:52 +02001515 if (handler->match)
1516 return handler->match(idstr, matchid);
1517
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001518 for (devid = handler->ids; devid->id[0]; devid++)
1519 if (!strcmp((char *)devid->id, idstr)) {
1520 if (matchid)
1521 *matchid = devid;
1522
1523 return true;
1524 }
1525
1526 return false;
1527}
1528
Rasmus Villemoes844142c2015-09-09 23:59:42 +02001529static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001530 const struct acpi_device_id **matchid)
1531{
1532 struct acpi_scan_handler *handler;
1533
1534 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1535 if (acpi_scan_handler_matching(handler, idstr, matchid))
1536 return handler;
1537
1538 return NULL;
1539}
1540
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001541void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1542{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001543 if (!!hotplug->enabled == !!val)
1544 return;
1545
1546 mutex_lock(&acpi_scan_lock);
1547
1548 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001549
1550 mutex_unlock(&acpi_scan_lock);
1551}
1552
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01001553static void acpi_scan_init_hotplug(struct acpi_device *adev)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001554{
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001555 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001556
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001557 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001558 acpi_dock_add(adev);
1559 return;
1560 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01001561 list_for_each_entry(hwid, &adev->pnp.ids, list) {
1562 struct acpi_scan_handler *handler;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001563
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001564 handler = acpi_scan_match_handler(hwid->id, NULL);
Rafael J. Wysocki1a699472014-02-06 13:58:13 +01001565 if (handler) {
1566 adev->flags.hotplug_notify = true;
1567 break;
1568 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01001569 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001570}
1571
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08001572static void acpi_device_dep_initialize(struct acpi_device *adev)
1573{
1574 struct acpi_dep_data *dep;
1575 struct acpi_handle_list dep_devices;
1576 acpi_status status;
1577 int i;
1578
1579 if (!acpi_has_method(adev->handle, "_DEP"))
1580 return;
1581
1582 status = acpi_evaluate_reference(adev->handle, "_DEP", NULL,
1583 &dep_devices);
1584 if (ACPI_FAILURE(status)) {
Rafael J. Wysocki80167a22014-12-12 22:48:44 +01001585 dev_dbg(&adev->dev, "Failed to evaluate _DEP.\n");
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08001586 return;
1587 }
1588
1589 for (i = 0; i < dep_devices.count; i++) {
1590 struct acpi_device_info *info;
1591 int skip;
1592
1593 status = acpi_get_object_info(dep_devices.handles[i], &info);
1594 if (ACPI_FAILURE(status)) {
Rafael J. Wysocki80167a22014-12-12 22:48:44 +01001595 dev_dbg(&adev->dev, "Error reading _DEP device info\n");
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08001596 continue;
1597 }
1598
1599 /*
1600 * Skip the dependency of Windows System Power
1601 * Management Controller
1602 */
1603 skip = info->valid & ACPI_VALID_HID &&
1604 !strcmp(info->hardware_id.string, "INT3396");
1605
1606 kfree(info);
1607
1608 if (skip)
1609 continue;
1610
1611 dep = kzalloc(sizeof(struct acpi_dep_data), GFP_KERNEL);
1612 if (!dep)
1613 return;
1614
1615 dep->master = dep_devices.handles[i];
1616 dep->slave = adev->handle;
1617 adev->dep_unmet++;
1618
1619 mutex_lock(&acpi_dep_list_lock);
1620 list_add_tail(&dep->node , &acpi_dep_list);
1621 mutex_unlock(&acpi_dep_list_lock);
1622 }
1623}
1624
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001625static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1626 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001628 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001629 int type;
1630 unsigned long long sta;
1631 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001632
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001633 acpi_bus_get_device(handle, &device);
1634 if (device)
1635 goto out;
1636
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001637 result = acpi_bus_type_and_status(handle, &type, &sta);
1638 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001639 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001641 if (type == ACPI_BUS_TYPE_POWER) {
1642 acpi_add_power_resource(handle);
1643 return AE_OK;
1644 }
1645
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001646 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001647 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001648 return AE_CTRL_DEPTH;
1649
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01001650 acpi_scan_init_hotplug(device);
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08001651 acpi_device_dep_initialize(device);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01001652
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001653 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001654 if (!*return_value)
1655 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001656
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001657 return AE_OK;
1658}
1659
Zhang Rui48459342014-05-30 14:35:34 +02001660static int acpi_check_spi_i2c_slave(struct acpi_resource *ares, void *data)
1661{
1662 bool *is_spi_i2c_slave_p = data;
1663
1664 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
1665 return 1;
1666
1667 /*
1668 * devices that are connected to UART still need to be enumerated to
1669 * platform bus
1670 */
1671 if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
1672 *is_spi_i2c_slave_p = true;
1673
1674 /* no need to do more checking */
1675 return -1;
1676}
1677
1678static void acpi_default_enumeration(struct acpi_device *device)
1679{
1680 struct list_head resource_list;
1681 bool is_spi_i2c_slave = false;
1682
Zhang Rui48459342014-05-30 14:35:34 +02001683 /*
Octavian Purdila68bdb672016-07-08 19:13:09 +03001684 * Do not enumerate SPI/I2C slaves as they will be enumerated by their
Zhang Rui48459342014-05-30 14:35:34 +02001685 * respective parents.
1686 */
1687 INIT_LIST_HEAD(&resource_list);
1688 acpi_dev_get_resources(device, &resource_list, acpi_check_spi_i2c_slave,
1689 &is_spi_i2c_slave);
1690 acpi_dev_free_resource_list(&resource_list);
Octavian Purdila10c7e202016-07-08 19:13:08 +03001691 if (!is_spi_i2c_slave) {
Zhang Rui48459342014-05-30 14:35:34 +02001692 acpi_create_platform_device(device);
Octavian Purdila10c7e202016-07-08 19:13:08 +03001693 acpi_device_set_enumerated(device);
Octavian Purdila68bdb672016-07-08 19:13:09 +03001694 } else {
1695 blocking_notifier_call_chain(&acpi_reconfig_chain,
1696 ACPI_RECONFIG_DEVICE_ADD, device);
Octavian Purdila10c7e202016-07-08 19:13:08 +03001697 }
Zhang Rui48459342014-05-30 14:35:34 +02001698}
1699
Rafael J. Wysocki7d284352015-04-24 02:18:01 +02001700static const struct acpi_device_id generic_device_ids[] = {
Rafael J. Wysockiee892092015-05-22 04:24:34 +02001701 {ACPI_DT_NAMESPACE_HID, },
Rafael J. Wysocki7d284352015-04-24 02:18:01 +02001702 {"", },
1703};
1704
1705static int acpi_generic_device_attach(struct acpi_device *adev,
1706 const struct acpi_device_id *not_used)
1707{
1708 /*
Rafael J. Wysockiee892092015-05-22 04:24:34 +02001709 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
1710 * below can be unconditional.
Rafael J. Wysocki7d284352015-04-24 02:18:01 +02001711 */
1712 if (adev->data.of_compatible)
1713 acpi_default_enumeration(adev);
1714
1715 return 1;
1716}
1717
1718static struct acpi_scan_handler generic_device_handler = {
1719 .ids = generic_device_ids,
1720 .attach = acpi_generic_device_attach,
1721};
1722
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001723static int acpi_scan_attach_handler(struct acpi_device *device)
1724{
1725 struct acpi_hardware_id *hwid;
1726 int ret = 0;
1727
1728 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001729 const struct acpi_device_id *devid;
1730 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001731
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001732 handler = acpi_scan_match_handler(hwid->id, &devid);
1733 if (handler) {
Rafael J. Wysockid34afa92014-05-30 04:27:31 +02001734 if (!handler->attach) {
1735 device->pnp.type.platform_id = 0;
1736 continue;
1737 }
Rafael J. Wysocki9cb32ac2014-02-11 00:35:46 +01001738 device->handler = handler;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001739 ret = handler->attach(device, devid);
Rafael J. Wysocki9cb32ac2014-02-11 00:35:46 +01001740 if (ret > 0)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001741 break;
Rafael J. Wysocki9cb32ac2014-02-11 00:35:46 +01001742
1743 device->handler = NULL;
1744 if (ret < 0)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001745 break;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001746 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001747 }
Zhang Rui48459342014-05-30 14:35:34 +02001748
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001749 return ret;
1750}
1751
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001752static void acpi_bus_attach(struct acpi_device *device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001753{
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001754 struct acpi_device *child;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001755 acpi_handle ejd;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001756 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001757
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001758 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
1759 register_dock_dependent_device(device, ejd);
1760
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001761 acpi_bus_get_status(device);
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001762 /* Skip devices that are not present. */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001763 if (!acpi_device_is_present(device)) {
Octavian Purdila10c7e202016-07-08 19:13:08 +03001764 acpi_device_clear_enumerated(device);
Rafael J. Wysocki1b1f3e12015-01-01 23:38:28 +01001765 device->flags.power_manageable = 0;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001766 return;
1767 }
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02001768 if (device->handler)
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001769 goto ok;
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02001770
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001771 if (!device->flags.initialized) {
Rafael J. Wysocki1b1f3e12015-01-01 23:38:28 +01001772 device->flags.power_manageable =
1773 device->power.states[ACPI_STATE_D0].flags.valid;
1774 if (acpi_bus_init_power(device))
1775 device->flags.power_manageable = 0;
1776
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001777 device->flags.initialized = true;
1778 }
Octavian Purdila10c7e202016-07-08 19:13:08 +03001779
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001780 ret = acpi_scan_attach_handler(device);
Rafael J. Wysocki69310072013-11-07 01:41:01 +01001781 if (ret < 0)
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001782 return;
Rafael J. Wysocki69310072013-11-07 01:41:01 +01001783
1784 device->flags.match_driver = true;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001785 if (!ret) {
1786 ret = device_attach(&device->dev);
1787 if (ret < 0)
1788 return;
Rafael J. Wysocki7d284352015-04-24 02:18:01 +02001789
1790 if (!ret && device->pnp.type.platform_id)
1791 acpi_default_enumeration(device);
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001792 }
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001793
1794 ok:
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001795 list_for_each_entry(child, &device->children, node)
1796 acpi_bus_attach(child);
Rafael J. Wysocki8ab17fc2014-09-21 02:58:18 +02001797
1798 if (device->handler && device->handler->hotplug.notify_online)
1799 device->handler->hotplug.notify_online(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08001802void acpi_walk_dep_device_list(acpi_handle handle)
1803{
1804 struct acpi_dep_data *dep, *tmp;
1805 struct acpi_device *adev;
1806
1807 mutex_lock(&acpi_dep_list_lock);
1808 list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
1809 if (dep->master == handle) {
1810 acpi_bus_get_device(dep->slave, &adev);
1811 if (!adev)
1812 continue;
1813
1814 adev->dep_unmet--;
1815 if (!adev->dep_unmet)
1816 acpi_bus_attach(adev);
1817 list_del(&dep->node);
1818 kfree(dep);
1819 }
1820 }
1821 mutex_unlock(&acpi_dep_list_lock);
1822}
1823EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
1824
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001825/**
1826 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1827 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01001828 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001829 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1830 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01001831 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001832 * If no devices were found, -ENODEV is returned, but it does not mean that
1833 * there has been a real error. There just have been no suitable ACPI objects
1834 * in the table trunk from which the kernel could create a device and add an
1835 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001836 *
1837 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01001838 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001839int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001840{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 void *device = NULL;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001842
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001843 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001845 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001846
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001847 if (device) {
1848 acpi_bus_attach(device);
1849 return 0;
1850 }
1851 return -ENODEV;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001852}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001853EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001855/**
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001856 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
1857 * @adev: Root of the ACPI namespace scope to walk.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001858 *
1859 * Must be called under acpi_scan_lock.
1860 */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001861void acpi_bus_trim(struct acpi_device *adev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001863 struct acpi_scan_handler *handler = adev->handler;
1864 struct acpi_device *child;
1865
1866 list_for_each_entry_reverse(child, &adev->children, node)
1867 acpi_bus_trim(child);
1868
Rafael J. Wysockia951c772014-01-27 23:08:09 +01001869 adev->flags.match_driver = false;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001870 if (handler) {
1871 if (handler->detach)
1872 handler->detach(adev);
1873
1874 adev->handler = NULL;
1875 } else {
1876 device_release_driver(&adev->dev);
1877 }
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01001878 /*
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001879 * Most likely, the device is going away, so put it into D3cold before
1880 * that.
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001881 */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001882 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
1883 adev->flags.initialized = false;
Octavian Purdila10c7e202016-07-08 19:13:08 +03001884 acpi_device_clear_enumerated(adev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
Kristen Accardiceaba662006-02-23 17:56:01 -08001886EXPORT_SYMBOL_GPL(acpi_bus_trim);
1887
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001888static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Len Brown4be44fc2005-08-05 00:44:28 -04001890 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 /*
1893 * Enumerate all fixed-feature devices.
1894 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001895 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
1896 struct acpi_device *device = NULL;
1897
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001898 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001899 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001900 ACPI_STA_DEFAULT);
1901 if (result)
1902 return result;
1903
Rafael J. Wysocki88346162013-11-18 14:18:47 +01001904 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001905 result = device_attach(&device->dev);
1906 if (result < 0)
1907 return result;
1908
Daniel Drakec10d7a132012-05-10 00:08:43 +01001909 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001912 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
1913 struct acpi_device *device = NULL;
1914
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001915 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001916 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001917 ACPI_STA_DEFAULT);
1918 if (result)
1919 return result;
1920
Rafael J. Wysocki88346162013-11-18 14:18:47 +01001921 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001922 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001925 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Octavian Purdila68bdb672016-07-08 19:13:09 +03001928static bool acpi_scan_initialized;
1929
Bjorn Helgaase747f272009-03-24 16:49:43 -06001930int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01001934 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01001935 acpi_pci_link_init();
Rafael J. Wysockiac212b62013-05-03 00:26:22 +02001936 acpi_processor_init();
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01001937 acpi_lpss_init();
Ken Xue92082a82015-02-06 08:27:51 +08001938 acpi_apd_init();
Lan Tianyu2fa97fe2013-06-05 02:27:50 +00001939 acpi_cmos_rtc_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01001940 acpi_container_init();
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01001941 acpi_memory_hotplug_init();
Zhang Ruieec15ed2014-05-30 04:23:01 +02001942 acpi_pnp_init();
Zhang Rui3230bbf2014-03-14 00:34:05 +08001943 acpi_int340x_thermal_init();
Graeme Gregory6ce2e182016-01-20 20:29:27 +06001944 acpi_amba_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01001945
Rafael J. Wysocki7d284352015-04-24 02:18:01 +02001946 acpi_scan_add_handler(&generic_device_handler);
1947
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001948 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 * Enumerate devices in the ACPI namespace.
1951 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001952 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001954 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001956 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001958 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Aaron Lu2650ef42014-04-28 10:38:04 +08001960 /* Fixed feature devices do not exist on HW-reduced platform */
1961 if (!acpi_gbl_reduced_hardware) {
1962 result = acpi_bus_scan_fixed();
1963 if (result) {
1964 acpi_detach_data(acpi_root->handle,
1965 acpi_scan_drop_device);
1966 acpi_device_del(acpi_root);
1967 put_device(&acpi_root->dev);
1968 goto out;
1969 }
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001970 }
1971
1972 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001973
Octavian Purdila68bdb672016-07-08 19:13:09 +03001974 acpi_scan_initialized = true;
1975
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001976 out:
1977 mutex_unlock(&acpi_scan_lock);
1978 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
Marc Zyngiere647b532015-09-28 15:49:12 +01001980
1981static struct acpi_probe_entry *ape;
1982static int acpi_probe_count;
1983static DEFINE_SPINLOCK(acpi_probe_lock);
1984
1985static int __init acpi_match_madt(struct acpi_subtable_header *header,
1986 const unsigned long end)
1987{
1988 if (!ape->subtable_valid || ape->subtable_valid(header, ape))
1989 if (!ape->probe_subtbl(header, end))
1990 acpi_probe_count++;
1991
1992 return 0;
1993}
1994
1995int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
1996{
1997 int count = 0;
1998
1999 if (acpi_disabled)
2000 return 0;
2001
2002 spin_lock(&acpi_probe_lock);
2003 for (ape = ap_head; nr; ape++, nr--) {
2004 if (ACPI_COMPARE_NAME(ACPI_SIG_MADT, ape->id)) {
2005 acpi_probe_count = 0;
2006 acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
2007 count += acpi_probe_count;
2008 } else {
2009 int res;
2010 res = acpi_table_parse(ape->id, ape->probe_table);
2011 if (!res)
2012 count++;
2013 }
2014 }
2015 spin_unlock(&acpi_probe_lock);
2016
2017 return count;
2018}
Octavian Purdila68bdb672016-07-08 19:13:09 +03002019
2020struct acpi_table_events_work {
2021 struct work_struct work;
2022 void *table;
2023 u32 event;
2024};
2025
2026static void acpi_table_events_fn(struct work_struct *work)
2027{
2028 struct acpi_table_events_work *tew;
2029
2030 tew = container_of(work, struct acpi_table_events_work, work);
2031
2032 if (tew->event == ACPI_TABLE_EVENT_LOAD) {
2033 acpi_scan_lock_acquire();
2034 acpi_bus_scan(ACPI_ROOT_OBJECT);
2035 acpi_scan_lock_release();
2036 }
2037
2038 kfree(tew);
2039}
2040
2041void acpi_scan_table_handler(u32 event, void *table, void *context)
2042{
2043 struct acpi_table_events_work *tew;
2044
2045 if (!acpi_scan_initialized)
2046 return;
2047
2048 if (event != ACPI_TABLE_EVENT_LOAD)
2049 return;
2050
2051 tew = kmalloc(sizeof(*tew), GFP_KERNEL);
2052 if (!tew)
2053 return;
2054
2055 INIT_WORK(&tew->work, acpi_table_events_fn);
2056 tew->table = table;
2057 tew->event = event;
2058
2059 schedule_work(&tew->work);
2060}
2061
2062int acpi_reconfig_notifier_register(struct notifier_block *nb)
2063{
2064 return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
2065}
2066EXPORT_SYMBOL(acpi_reconfig_notifier_register);
2067
2068int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
2069{
2070 return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
2071}
2072EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);