blob: 518aae461a00f6549a66b3117b1843e64bcc5bc7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/acpi.h>
Alok N Kataria74523c92008-06-13 12:54:24 -040010#include <linux/signal.h>
11#include <linux/kthread.h>
Darrick J. Wong222e82a2010-03-24 14:38:37 +010012#include <linux/dmi.h>
Lance Ortizd1efe3c2012-10-02 12:43:23 -060013#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Rafael J. Wysockid7831562013-11-22 21:52:12 +010015#include <asm/pgtable.h>
16
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060017#include "internal.h"
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050020ACPI_MODULE_NAME("scan");
Len Brown4be44fc2005-08-05 00:44:28 -040021extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020024#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define ACPI_BUS_DEVICE_NAME "System Bus"
26
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +000027#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
28
Rafael J. Wysockid7831562013-11-22 21:52:12 +010029#define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
30
Rafael J. Wysocki683058e2013-05-03 00:26:16 +020031/*
32 * If set, devices will be hot-removed even if they cannot be put offline
33 * gracefully (from the kernel's standpoint).
34 */
35bool acpi_force_hot_remove;
36
Thomas Renninger620e1122010-10-01 10:54:00 +020037static const char *dummy_hid = "device";
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020038
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080039static LIST_HEAD(acpi_bus_id_list);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +010040static DEFINE_MUTEX(acpi_scan_lock);
Rafael J. Wysockica589f92013-01-30 14:27:29 +010041static LIST_HEAD(acpi_scan_handlers_list);
Shaohua Li90905892009-04-07 10:24:29 +080042DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043LIST_HEAD(acpi_wakeup_device_list);
Rafael J. Wysockie5255062014-02-04 00:43:17 +010044static DEFINE_MUTEX(acpi_hp_context_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080046struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080047 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080048 unsigned int instance_no;
49 struct list_head node;
50};
Thomas Renninger29b71a12007-07-23 14:43:51 +020051
Rafael J. Wysocki3757b942013-02-13 14:36:47 +010052void acpi_scan_lock_acquire(void)
53{
54 mutex_lock(&acpi_scan_lock);
55}
56EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
57
58void acpi_scan_lock_release(void)
59{
60 mutex_unlock(&acpi_scan_lock);
61}
62EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
63
Rafael J. Wysockie5255062014-02-04 00:43:17 +010064void acpi_lock_hp_context(void)
65{
66 mutex_lock(&acpi_hp_context_lock);
67}
68
69void acpi_unlock_hp_context(void)
70{
71 mutex_unlock(&acpi_hp_context_lock);
72}
73
Rafael J. Wysockica589f92013-01-30 14:27:29 +010074int acpi_scan_add_handler(struct acpi_scan_handler *handler)
75{
76 if (!handler || !handler->attach)
77 return -EINVAL;
78
79 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
80 return 0;
81}
82
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +010083int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
84 const char *hotplug_profile_name)
85{
86 int error;
87
88 error = acpi_scan_add_handler(handler);
89 if (error)
90 return error;
91
92 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
93 return 0;
94}
95
Thomas Renninger29b71a12007-07-23 14:43:51 +020096/*
97 * Creates hid/cid(s) string needed for modalias and uevent
98 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
99 * char *modalias: "acpi:IBM0001:ACPI0001"
Zhang Rui3d8e0092014-01-14 16:46:35 +0800100 * Return: 0: no _HID and no _CID
101 * -EINVAL: output error
102 * -ENOMEM: output is truncated
Thomas Renninger29b71a12007-07-23 14:43:51 +0200103*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +0200104static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
105 int size)
106{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200107 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800108 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600109 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200110
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200111 if (list_empty(&acpi_dev->pnp.ids))
112 return 0;
113
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800114 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +0200115 size -= len;
116
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600117 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
118 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui3d8e0092014-01-14 16:46:35 +0800119 if (count < 0)
120 return EINVAL;
121 if (count >= size)
122 return -ENOMEM;
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800123 len += count;
124 size -= count;
125 }
126
Thomas Renninger29b71a12007-07-23 14:43:51 +0200127 modalias[len] = '\0';
128 return len;
129}
130
Zhang Rui6eb24512014-01-14 16:46:36 +0800131/*
132 * Creates uevent modalias field for ACPI enumerated devices.
133 * Because the other buses does not support ACPI HIDs & CIDs.
134 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
135 * "acpi:IBM0001:ACPI0001"
136 */
137int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
138{
139 struct acpi_device *acpi_dev;
140 int len;
141
142 acpi_dev = ACPI_COMPANION(dev);
143 if (!acpi_dev)
144 return -ENODEV;
145
146 /* Fall back to bus specific way of modalias exporting */
147 if (list_empty(&acpi_dev->pnp.ids))
148 return -ENODEV;
149
150 if (add_uevent_var(env, "MODALIAS="))
151 return -ENOMEM;
152 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
153 sizeof(env->buf) - env->buflen);
154 if (len <= 0)
155 return len;
156 env->buflen += len;
157 return 0;
158}
159EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
160
161/*
162 * Creates modalias sysfs attribute for ACPI enumerated devices.
163 * Because the other buses does not support ACPI HIDs & CIDs.
164 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
165 * "acpi:IBM0001:ACPI0001"
166 */
167int acpi_device_modalias(struct device *dev, char *buf, int size)
168{
169 struct acpi_device *acpi_dev;
170 int len;
171
172 acpi_dev = ACPI_COMPANION(dev);
173 if (!acpi_dev)
174 return -ENODEV;
175
176 /* Fall back to bus specific way of modalias exporting */
177 if (list_empty(&acpi_dev->pnp.ids))
178 return -ENODEV;
179
180 len = create_modalias(acpi_dev, buf, size -1);
181 if (len <= 0)
182 return len;
183 buf[len++] = '\n';
184 return len;
185}
186EXPORT_SYMBOL_GPL(acpi_device_modalias);
187
Thomas Renninger29b71a12007-07-23 14:43:51 +0200188static ssize_t
189acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
190 struct acpi_device *acpi_dev = to_acpi_device(dev);
191 int len;
192
Thomas Renninger29b71a12007-07-23 14:43:51 +0200193 len = create_modalias(acpi_dev, buf, 1024);
194 if (len <= 0)
Zhang Rui3d8e0092014-01-14 16:46:35 +0800195 return len;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200196 buf[len++] = '\n';
197 return len;
198}
199static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
200
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100201bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100202{
203 struct acpi_device_physical_node *pn;
204 bool offline = true;
205
206 mutex_lock(&adev->physical_node_lock);
207
208 list_for_each_entry(pn, &adev->physical_node_list, node)
209 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100210 if (uevent)
211 kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
212
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100213 offline = false;
214 break;
215 }
216
217 mutex_unlock(&adev->physical_node_lock);
218 return offline;
219}
220
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100221static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
222 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200223{
224 struct acpi_device *device = NULL;
225 struct acpi_device_physical_node *pn;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200226 bool second_pass = (bool)data;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200227 acpi_status status = AE_OK;
228
229 if (acpi_bus_get_device(handle, &device))
230 return AE_OK;
231
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100232 if (device->handler && !device->handler->hotplug.enabled) {
233 *ret_p = &device->dev;
234 return AE_SUPPORT;
235 }
236
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200237 mutex_lock(&device->physical_node_lock);
238
239 list_for_each_entry(pn, &device->physical_node_list, node) {
240 int ret;
241
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200242 if (second_pass) {
243 /* Skip devices offlined by the first pass. */
244 if (pn->put_online)
245 continue;
246 } else {
247 pn->put_online = false;
248 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200249 ret = device_offline(pn->dev);
250 if (acpi_force_hot_remove)
251 continue;
252
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200253 if (ret >= 0) {
254 pn->put_online = !ret;
255 } else {
256 *ret_p = pn->dev;
257 if (second_pass) {
258 status = AE_ERROR;
259 break;
260 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200261 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200262 }
263
264 mutex_unlock(&device->physical_node_lock);
265
266 return status;
267}
268
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100269static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
270 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200271{
272 struct acpi_device *device = NULL;
273 struct acpi_device_physical_node *pn;
274
275 if (acpi_bus_get_device(handle, &device))
276 return AE_OK;
277
278 mutex_lock(&device->physical_node_lock);
279
280 list_for_each_entry(pn, &device->physical_node_list, node)
281 if (pn->put_online) {
282 device_online(pn->dev);
283 pn->put_online = false;
284 }
285
286 mutex_unlock(&device->physical_node_lock);
287
288 return AE_OK;
289}
290
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100291static int acpi_scan_try_to_offline(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Yinghai Lu5993c462013-01-11 22:40:41 +0000293 acpi_handle handle = device->handle;
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100294 struct device *errdev = NULL;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100295 acpi_status status;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100296
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200297 /*
298 * Carry out two passes here and ignore errors in the first pass,
299 * because if the devices in question are memory blocks and
300 * CONFIG_MEMCG is set, one of the blocks may hold data structures
301 * that the other blocks depend on, but it is not known in advance which
302 * block holds them.
303 *
304 * If the first pass is successful, the second one isn't needed, though.
305 */
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100306 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
307 NULL, acpi_bus_offline, (void *)false,
308 (void **)&errdev);
309 if (status == AE_SUPPORT) {
310 dev_warn(errdev, "Offline disabled.\n");
311 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
312 acpi_bus_online, NULL, NULL, NULL);
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100313 return -EPERM;
314 }
315 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200316 if (errdev) {
317 errdev = NULL;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200318 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100319 NULL, acpi_bus_offline, (void *)true,
320 (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200321 if (!errdev || acpi_force_hot_remove)
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100322 acpi_bus_offline(handle, 0, (void *)true,
323 (void **)&errdev);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200324
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200325 if (errdev && !acpi_force_hot_remove) {
326 dev_warn(errdev, "Offline failed.\n");
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100327 acpi_bus_online(handle, 0, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200328 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100329 ACPI_UINT32_MAX, acpi_bus_online,
330 NULL, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200331 return -EBUSY;
332 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200333 }
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100334 return 0;
335}
336
337static int acpi_scan_hot_remove(struct acpi_device *device)
338{
339 acpi_handle handle = device->handle;
340 unsigned long long sta;
341 acpi_status status;
342
343 if (device->handler->hotplug.demand_offline && !acpi_force_hot_remove) {
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100344 if (!acpi_scan_is_offline(device, true))
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100345 return -EBUSY;
346 } else {
347 int error = acpi_scan_try_to_offline(device);
348 if (error)
349 return error;
350 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200351
Zhang Rui26d46862008-04-29 02:35:48 -0400352 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100353 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400354
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100355 acpi_bus_trim(device);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200356
Jiang Liu7d2421f2013-06-29 00:24:40 +0800357 acpi_evaluate_lck(handle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /*
359 * TBD: _EJD support.
360 */
Jiang Liu7d2421f2013-06-29 00:24:40 +0800361 status = acpi_evaluate_ej0(handle);
362 if (status == AE_NOT_FOUND)
363 return -ENODEV;
364 else if (ACPI_FAILURE(status))
365 return -EIO;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100366
Toshi Kani882fd122013-03-13 19:29:26 +0000367 /*
368 * Verify if eject was indeed successful. If not, log an error
369 * message. No need to call _OST since _EJ0 call was made OK.
370 */
371 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
372 if (ACPI_FAILURE(status)) {
373 acpi_handle_warn(handle,
374 "Status check after eject failed (0x%x)\n", status);
375 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
376 acpi_handle_warn(handle,
377 "Eject incomplete - status 0x%llx\n", sta);
378 }
379
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100380 return 0;
381}
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100382
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100383static int acpi_scan_device_not_present(struct acpi_device *adev)
384{
385 if (!acpi_device_enumerated(adev)) {
386 dev_warn(&adev->dev, "Still not present\n");
387 return -EALREADY;
388 }
389 acpi_bus_trim(adev);
390 return 0;
391}
392
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100393static int acpi_scan_device_check(struct acpi_device *adev)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100394{
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200395 int error;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100396
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100397 acpi_bus_get_status(adev);
398 if (adev->status.present || adev->status.functional) {
399 /*
400 * This function is only called for device objects for which
401 * matching scan handlers exist. The only situation in which
402 * the scan handler is not attached to this device object yet
403 * is when the device has just appeared (either it wasn't
404 * present at all before or it was removed and then added
405 * again).
406 */
407 if (adev->handler) {
408 dev_warn(&adev->dev, "Already enumerated\n");
409 return -EALREADY;
410 }
411 error = acpi_bus_scan(adev->handle);
412 if (error) {
413 dev_warn(&adev->dev, "Namespace scan failure\n");
414 return error;
415 }
416 if (!adev->handler) {
417 dev_warn(&adev->dev, "Enumeration failure\n");
418 error = -ENODEV;
419 }
420 } else {
421 error = acpi_scan_device_not_present(adev);
422 }
423 return error;
424}
425
426static int acpi_scan_bus_check(struct acpi_device *adev)
427{
428 struct acpi_scan_handler *handler = adev->handler;
429 struct acpi_device *child;
430 int error;
431
432 acpi_bus_get_status(adev);
433 if (!(adev->status.present || adev->status.functional)) {
434 acpi_scan_device_not_present(adev);
435 return 0;
436 }
437 if (handler && handler->hotplug.scan_dependent)
438 return handler->hotplug.scan_dependent(adev);
439
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100440 error = acpi_bus_scan(adev->handle);
441 if (error) {
442 dev_warn(&adev->dev, "Namespace scan failure\n");
443 return error;
444 }
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100445 list_for_each_entry(child, &adev->children, node) {
446 error = acpi_scan_bus_check(child);
447 if (error)
448 return error;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100449 }
450 return 0;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100451}
452
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100453static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
454{
455 switch (type) {
456 case ACPI_NOTIFY_BUS_CHECK:
457 return acpi_scan_bus_check(adev);
458 case ACPI_NOTIFY_DEVICE_CHECK:
459 return acpi_scan_device_check(adev);
460 case ACPI_NOTIFY_EJECT_REQUEST:
461 case ACPI_OST_EC_OSPM_EJECT:
Rafael J. Wysockidd2151b2014-02-04 00:44:02 +0100462 if (adev->handler && !adev->handler->hotplug.enabled) {
463 dev_info(&adev->dev, "Eject disabled\n");
464 return -EPERM;
465 }
466 acpi_evaluate_hotplug_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
467 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100468 return acpi_scan_hot_remove(adev);
469 }
470 return -EINVAL;
471}
472
Rafael J. Wysocki1a699472014-02-06 13:58:13 +0100473void acpi_device_hotplug(void *data, u32 src)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100474{
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100475 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100476 struct acpi_device *adev = data;
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100477 int error = -ENODEV;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100478
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200479 lock_device_hotplug();
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200480 mutex_lock(&acpi_scan_lock);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100481
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100482 /*
483 * The device object's ACPI handle cannot become invalid as long as we
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100484 * are holding acpi_scan_lock, but it might have become invalid before
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100485 * that lock was acquired.
486 */
487 if (adev->handle == INVALID_ACPI_HANDLE)
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100488 goto err_out;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100489
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100490 if (adev->flags.is_dock_station) {
491 error = dock_notify(adev, src);
492 } else if (adev->flags.hotplug_notify) {
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100493 error = acpi_generic_hotplug_event(adev, src);
Rafael J. Wysockidd2151b2014-02-04 00:44:02 +0100494 if (error == -EPERM) {
495 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
496 goto err_out;
497 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100498 } else {
499 int (*event)(struct acpi_device *, u32);
500
501 acpi_lock_hp_context();
502 event = adev->hp ? adev->hp->event : NULL;
503 acpi_unlock_hp_context();
504 /*
505 * There may be additional notify handlers for device objects
506 * without the .event() callback, so ignore them here.
507 */
508 if (event)
509 error = event(adev, src);
510 else
511 goto out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100512 }
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100513 if (!error)
514 ost_code = ACPI_OST_SC_SUCCESS;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100515
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100516 err_out:
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100517 acpi_evaluate_hotplug_ost(adev->handle, src, ost_code, NULL);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100518
519 out:
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +0100520 acpi_bus_put_acpi_device(adev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100521 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200522 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100523}
524
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100525static ssize_t real_power_state_show(struct device *dev,
526 struct device_attribute *attr, char *buf)
527{
528 struct acpi_device *adev = to_acpi_device(dev);
529 int state;
530 int ret;
531
532 ret = acpi_device_get_power(adev, &state);
533 if (ret)
534 return ret;
535
536 return sprintf(buf, "%s\n", acpi_power_state_string(state));
537}
538
539static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
540
541static ssize_t power_state_show(struct device *dev,
542 struct device_attribute *attr, char *buf)
543{
544 struct acpi_device *adev = to_acpi_device(dev);
545
546 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
547}
548
549static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800552acpi_eject_store(struct device *d, struct device_attribute *attr,
553 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800555 struct acpi_device *acpi_device = to_acpi_device(d);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100556 acpi_object_type not_used;
557 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100559 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100562 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
563 && !acpi_device->driver)
564 return -ENODEV;
565
566 status = acpi_get_type(acpi_device->handle, &not_used);
567 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
568 return -ENODEV;
569
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100570 get_device(&acpi_device->dev);
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100571 status = acpi_hotplug_execute(acpi_device_hotplug, acpi_device,
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100572 ACPI_OST_EC_OSPM_EJECT);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200573 if (ACPI_SUCCESS(status))
574 return count;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100575
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200576 put_device(&acpi_device->dev);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200577 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100578 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100579 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800582static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800584static ssize_t
585acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
586 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600588 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800589}
590static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
591
Lv Zheng923d4a42012-10-30 14:43:26 +0100592static ssize_t acpi_device_uid_show(struct device *dev,
593 struct device_attribute *attr, char *buf)
594{
595 struct acpi_device *acpi_dev = to_acpi_device(dev);
596
597 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
598}
599static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
600
601static ssize_t acpi_device_adr_show(struct device *dev,
602 struct device_attribute *attr, char *buf)
603{
604 struct acpi_device *acpi_dev = to_acpi_device(dev);
605
606 return sprintf(buf, "0x%08x\n",
607 (unsigned int)(acpi_dev->pnp.bus_address));
608}
609static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
610
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800611static ssize_t
612acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
613 struct acpi_device *acpi_dev = to_acpi_device(dev);
614 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
615 int result;
616
617 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600618 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800619 goto end;
620
621 result = sprintf(buf, "%s\n", (char*)path.pointer);
622 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600623end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800624 return result;
625}
626static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
627
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600628/* sysfs file that shows description text from the ACPI _STR method */
629static ssize_t description_show(struct device *dev,
630 struct device_attribute *attr,
631 char *buf) {
632 struct acpi_device *acpi_dev = to_acpi_device(dev);
633 int result;
634
635 if (acpi_dev->pnp.str_obj == NULL)
636 return 0;
637
638 /*
639 * The _STR object contains a Unicode identifier for a device.
640 * We need to convert to utf-8 so it can be displayed.
641 */
642 result = utf16s_to_utf8s(
643 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
644 acpi_dev->pnp.str_obj->buffer.length,
645 UTF16_LITTLE_ENDIAN, buf,
646 PAGE_SIZE);
647
648 buf[result++] = '\n';
649
650 return result;
651}
652static DEVICE_ATTR(description, 0444, description_show, NULL);
653
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100654static ssize_t
655acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
656 char *buf) {
657 struct acpi_device *acpi_dev = to_acpi_device(dev);
658
659 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
660}
661static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
662
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800663static ssize_t status_show(struct device *dev, struct device_attribute *attr,
664 char *buf) {
665 struct acpi_device *acpi_dev = to_acpi_device(dev);
666 acpi_status status;
667 unsigned long long sta;
668
669 status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
670 if (ACPI_FAILURE(status))
671 return -ENODEV;
672
673 return sprintf(buf, "%llu\n", sta);
674}
675static DEVICE_ATTR_RO(status);
676
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800677static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600679 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800680 acpi_status status;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100681 unsigned long long sun;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800682 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800684 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800685 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800686 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600687 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800688 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600689 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800690 goto end;
691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200693 if (!list_empty(&dev->pnp.ids)) {
694 result = device_create_file(&dev->dev, &dev_attr_hid);
695 if (result)
696 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200698 result = device_create_file(&dev->dev, &dev_attr_modalias);
699 if (result)
700 goto end;
701 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200702
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600703 /*
704 * If device has _STR, 'description' file is created
705 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800706 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600707 status = acpi_evaluate_object(dev->handle, "_STR",
708 NULL, &buffer);
709 if (ACPI_FAILURE(status))
710 buffer.pointer = NULL;
711 dev->pnp.str_obj = buffer.pointer;
712 result = device_create_file(&dev->dev, &dev_attr_description);
713 if (result)
714 goto end;
715 }
716
Toshi Kanid4e1a692013-03-04 21:30:41 +0000717 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100718 result = device_create_file(&dev->dev, &dev_attr_adr);
719 if (dev->pnp.unique_id)
720 result = device_create_file(&dev->dev, &dev_attr_uid);
721
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100722 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
723 if (ACPI_SUCCESS(status)) {
724 dev->pnp.sun = (unsigned long)sun;
725 result = device_create_file(&dev->dev, &dev_attr_sun);
726 if (result)
727 goto end;
728 } else {
729 dev->pnp.sun = (unsigned long)-1;
730 }
731
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800732 if (acpi_has_method(dev->handle, "_STA")) {
733 result = device_create_file(&dev->dev, &dev_attr_status);
734 if (result)
735 goto end;
736 }
737
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800738 /*
739 * If device has _EJ0, 'eject' file is created that is used to trigger
740 * hot-removal function from userland.
741 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800742 if (acpi_has_method(dev->handle, "_EJ0")) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800743 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100744 if (result)
745 return result;
746 }
747
748 if (dev->flags.power_manageable) {
749 result = device_create_file(&dev->dev, &dev_attr_power_state);
750 if (result)
751 return result;
752
753 if (dev->power.flags.power_resources)
754 result = device_create_file(&dev->dev,
755 &dev_attr_real_power_state);
756 }
757
Alex Chiang0c526d92009-05-14 08:31:37 -0600758end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800759 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800760}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800762static void acpi_device_remove_files(struct acpi_device *dev)
763{
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100764 if (dev->flags.power_manageable) {
765 device_remove_file(&dev->dev, &dev_attr_power_state);
766 if (dev->power.flags.power_resources)
767 device_remove_file(&dev->dev,
768 &dev_attr_real_power_state);
769 }
770
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800771 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600772 * If device has _STR, remove 'description' file
773 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800774 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600775 kfree(dev->pnp.str_obj);
776 device_remove_file(&dev->dev, &dev_attr_description);
777 }
778 /*
779 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800780 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800781 if (acpi_has_method(dev->handle, "_EJ0"))
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800782 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800783
Jiang Liu952c63e2013-06-29 00:24:38 +0800784 if (acpi_has_method(dev->handle, "_SUN"))
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100785 device_remove_file(&dev->dev, &dev_attr_sun);
786
Lv Zheng923d4a42012-10-30 14:43:26 +0100787 if (dev->pnp.unique_id)
788 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000789 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100790 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600791 device_remove_file(&dev->dev, &dev_attr_modalias);
792 device_remove_file(&dev->dev, &dev_attr_hid);
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800793 if (acpi_has_method(dev->handle, "_STA"))
794 device_remove_file(&dev->dev, &dev_attr_status);
Alex Chiang0c526d92009-05-14 08:31:37 -0600795 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800796 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800797}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800799 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200801
Mika Westerbergcf761af2012-10-31 22:44:41 +0100802static const struct acpi_device_id *__acpi_match_device(
803 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200804{
805 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600806 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200807
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800808 /*
809 * If the device is not present, it is unnecessary to load device
810 * driver for it.
811 */
812 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100813 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800814
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600815 for (id = ids; id->id[0]; id++)
816 list_for_each_entry(hwid, &device->pnp.ids, list)
817 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100818 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200819
Mika Westerbergcf761af2012-10-31 22:44:41 +0100820 return NULL;
821}
822
823/**
824 * acpi_match_device - Match a struct device against a given list of ACPI IDs
825 * @ids: Array of struct acpi_device_id object to match against.
826 * @dev: The device structure to match.
827 *
828 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
829 * object for that handle and use that object to match against a given list of
830 * device IDs.
831 *
832 * Return a pointer to the first matching ID on success or %NULL on failure.
833 */
834const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
835 const struct device *dev)
836{
837 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100838 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100839
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100840 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100841 return NULL;
842
843 return __acpi_match_device(adev, ids);
844}
845EXPORT_SYMBOL_GPL(acpi_match_device);
846
847int acpi_match_device_ids(struct acpi_device *device,
848 const struct acpi_device_id *ids)
849{
850 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200851}
852EXPORT_SYMBOL(acpi_match_device_ids);
853
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100854static void acpi_free_power_resources_lists(struct acpi_device *device)
855{
856 int i;
857
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100858 if (device->wakeup.flags.valid)
859 acpi_power_resources_list_free(&device->wakeup.resources);
860
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100861 if (!device->flags.power_manageable)
862 return;
863
864 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
865 struct acpi_device_power_state *ps = &device->power.states[i];
866 acpi_power_resources_list_free(&ps->resources);
867 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600868}
869
Patrick Mochel1890a972006-12-07 20:56:31 +0800870static void acpi_device_release(struct device *dev)
871{
872 struct acpi_device *acpi_dev = to_acpi_device(dev);
873
Toshi Kanic0af4172013-03-04 21:30:42 +0000874 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100875 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800876 kfree(acpi_dev);
877}
878
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800879static int acpi_bus_match(struct device *dev, struct device_driver *drv)
880{
881 struct acpi_device *acpi_dev = to_acpi_device(dev);
882 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
883
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100884 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +0100885 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800886}
887
Kay Sievers7eff2e72007-08-14 15:15:12 +0200888static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800889{
890 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200891 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800892
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200893 if (list_empty(&acpi_dev->pnp.ids))
894 return 0;
895
Kay Sievers7eff2e72007-08-14 15:15:12 +0200896 if (add_uevent_var(env, "MODALIAS="))
897 return -ENOMEM;
898 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
899 sizeof(env->buf) - env->buflen);
Zhang Rui3d8e0092014-01-14 16:46:35 +0800900 if (len <= 0)
901 return len;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200902 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return 0;
904}
905
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000906static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
907{
908 struct acpi_device *device = data;
909
910 device->driver->ops.notify(device, event);
911}
912
913static acpi_status acpi_device_notify_fixed(void *data)
914{
915 struct acpi_device *device = data;
916
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000917 /* Fixed hardware devices have no handles */
918 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000919 return AE_OK;
920}
921
922static int acpi_device_install_notify_handler(struct acpi_device *device)
923{
924 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000925
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000926 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000927 status =
928 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
929 acpi_device_notify_fixed,
930 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000931 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000932 status =
933 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
934 acpi_device_notify_fixed,
935 device);
936 else
937 status = acpi_install_notify_handler(device->handle,
938 ACPI_DEVICE_NOTIFY,
939 acpi_device_notify,
940 device);
941
942 if (ACPI_FAILURE(status))
943 return -EINVAL;
944 return 0;
945}
946
947static void acpi_device_remove_notify_handler(struct acpi_device *device)
948{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000949 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000950 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
951 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000952 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000953 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
954 acpi_device_notify_fixed);
955 else
956 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
957 acpi_device_notify);
958}
959
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200960static int acpi_device_probe(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800961{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800962 struct acpi_device *acpi_dev = to_acpi_device(dev);
963 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
964 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Rafael J. Wysocki24071f42013-06-16 00:36:41 +0200966 if (acpi_dev->handler)
967 return -EINVAL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000968
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200969 if (!acpi_drv->ops.add)
970 return -ENOSYS;
Li Shaohuac4168bf2006-12-07 20:56:41 +0800971
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200972 ret = acpi_drv->ops.add(acpi_dev);
973 if (ret)
974 return ret;
975
976 acpi_dev->driver = acpi_drv;
977 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
978 "Driver [%s] successfully bound to device [%s]\n",
979 acpi_drv->name, acpi_dev->pnp.bus_id));
980
981 if (acpi_drv->ops.notify) {
982 ret = acpi_device_install_notify_handler(acpi_dev);
983 if (ret) {
984 if (acpi_drv->ops.remove)
985 acpi_drv->ops.remove(acpi_dev);
986
987 acpi_dev->driver = NULL;
988 acpi_dev->driver_data = NULL;
989 return ret;
990 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800991 }
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200992
993 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
994 acpi_drv->name, acpi_dev->pnp.bus_id));
995 get_device(dev);
996 return 0;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800997}
998
999static int acpi_device_remove(struct device * dev)
1000{
1001 struct acpi_device *acpi_dev = to_acpi_device(dev);
1002 struct acpi_driver *acpi_drv = acpi_dev->driver;
1003
1004 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001005 if (acpi_drv->ops.notify)
1006 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001007 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001008 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001009 }
1010 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001011 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001012
1013 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +08001014 return 0;
1015}
1016
David Brownell55955aa2007-05-08 00:28:35 -07001017struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +08001018 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001019 .match = acpi_bus_match,
1020 .probe = acpi_device_probe,
1021 .remove = acpi_device_remove,
1022 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023};
1024
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001025static void acpi_device_del(struct acpi_device *device)
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001026{
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001027 mutex_lock(&acpi_device_lock);
1028 if (device->parent)
1029 list_del(&device->node);
1030
1031 list_del(&device->wakeup_list);
1032 mutex_unlock(&acpi_device_lock);
1033
1034 acpi_power_add_remove_device(device, false);
1035 acpi_device_remove_files(device);
1036 if (device->remove)
1037 device->remove(device);
1038
1039 device_del(&device->dev);
1040}
1041
1042static LIST_HEAD(acpi_device_del_list);
1043static DEFINE_MUTEX(acpi_device_del_lock);
1044
1045static void acpi_device_del_work_fn(struct work_struct *work_not_used)
1046{
1047 for (;;) {
1048 struct acpi_device *adev;
1049
1050 mutex_lock(&acpi_device_del_lock);
1051
1052 if (list_empty(&acpi_device_del_list)) {
1053 mutex_unlock(&acpi_device_del_lock);
1054 break;
1055 }
1056 adev = list_first_entry(&acpi_device_del_list,
1057 struct acpi_device, del_list);
1058 list_del(&adev->del_list);
1059
1060 mutex_unlock(&acpi_device_del_lock);
1061
1062 acpi_device_del(adev);
1063 /*
1064 * Drop references to all power resources that might have been
1065 * used by the device.
1066 */
1067 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
1068 put_device(&adev->dev);
1069 }
1070}
1071
1072/**
1073 * acpi_scan_drop_device - Drop an ACPI device object.
1074 * @handle: Handle of an ACPI namespace node, not used.
1075 * @context: Address of the ACPI device object to drop.
1076 *
1077 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
1078 * namespace node the device object pointed to by @context is attached to.
1079 *
1080 * The unregistration is carried out asynchronously to avoid running
1081 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
1082 * ensure the correct ordering (the device objects must be unregistered in the
1083 * same order in which the corresponding namespace nodes are deleted).
1084 */
1085static void acpi_scan_drop_device(acpi_handle handle, void *context)
1086{
1087 static DECLARE_WORK(work, acpi_device_del_work_fn);
1088 struct acpi_device *adev = context;
1089
1090 mutex_lock(&acpi_device_del_lock);
1091
1092 /*
1093 * Use the ACPI hotplug workqueue which is ordered, so this work item
1094 * won't run after any hotplug work items submitted subsequently. That
1095 * prevents attempts to register device objects identical to those being
1096 * deleted from happening concurrently (such attempts result from
1097 * hotplug events handled via the ACPI hotplug workqueue). It also will
1098 * run after all of the work items submitted previosuly, which helps
1099 * those work items to ensure that they are not accessing stale device
1100 * objects.
1101 */
1102 if (list_empty(&acpi_device_del_list))
1103 acpi_queue_hotplug_work(&work);
1104
1105 list_add_tail(&adev->del_list, &acpi_device_del_list);
1106 /* Make acpi_ns_validate_handle() return NULL for this handle. */
1107 adev->handle = INVALID_ACPI_HANDLE;
1108
1109 mutex_unlock(&acpi_device_del_lock);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001110}
1111
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001112static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
1113 void (*callback)(void *))
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001114{
1115 acpi_status status;
1116
1117 if (!device)
1118 return -EINVAL;
1119
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001120 status = acpi_get_data_full(handle, acpi_scan_drop_device,
1121 (void **)device, callback);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001122 if (ACPI_FAILURE(status) || !*device) {
1123 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
1124 handle));
1125 return -ENODEV;
1126 }
1127 return 0;
1128}
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001129
1130int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
1131{
1132 return acpi_get_device_data(handle, device, NULL);
1133}
Rafael J. Wysocki65859252013-10-01 23:02:43 +02001134EXPORT_SYMBOL(acpi_bus_get_device);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001135
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001136static void get_acpi_device(void *dev)
1137{
1138 if (dev)
1139 get_device(&((struct acpi_device *)dev)->dev);
1140}
1141
1142struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
1143{
1144 struct acpi_device *adev = NULL;
1145
1146 acpi_get_device_data(handle, &adev, get_acpi_device);
1147 return adev;
1148}
1149
1150void acpi_bus_put_acpi_device(struct acpi_device *adev)
1151{
1152 put_device(&adev->dev);
1153}
1154
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001155int acpi_device_add(struct acpi_device *device,
1156 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001158 int result;
1159 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1160 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001161
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001162 if (device->handle) {
1163 acpi_status status;
1164
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001165 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001166 device);
1167 if (ACPI_FAILURE(status)) {
1168 acpi_handle_err(device->handle,
1169 "Unable to attach device data\n");
1170 return -ENODEV;
1171 }
1172 }
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 /*
1175 * Linkage
1176 * -------
1177 * Link this device to its parent and siblings.
1178 */
1179 INIT_LIST_HEAD(&device->children);
1180 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001182 INIT_LIST_HEAD(&device->physical_node_list);
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001183 INIT_LIST_HEAD(&device->del_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001184 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001186 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1187 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001188 pr_err(PREFIX "Memory allocation error\n");
1189 result = -ENOMEM;
1190 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001191 }
1192
Shaohua Li90905892009-04-07 10:24:29 +08001193 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001194 /*
1195 * Find suitable bus_id and instance number in acpi_bus_id_list
1196 * If failed, create one and link it into acpi_bus_id_list
1197 */
1198 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001199 if (!strcmp(acpi_device_bus_id->bus_id,
1200 acpi_device_hid(device))) {
1201 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001202 found = 1;
1203 kfree(new_bus_id);
1204 break;
1205 }
1206 }
Alex Chiang0c526d92009-05-14 08:31:37 -06001207 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001208 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001209 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001210 acpi_device_bus_id->instance_no = 0;
1211 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1212 }
Kay Sievers07944692008-10-30 01:18:59 +01001213 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 +08001214
Len Brown33b57152008-12-15 22:09:26 -05001215 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -05001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (device->wakeup.flags.valid)
1219 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +08001220 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Patrick Mochel1890a972006-12-07 20:56:31 +08001222 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001223 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +08001224 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001225 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001226 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -06001227 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -06001228 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001229 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001230 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001231
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001232 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -06001233 if (result)
Kay Sievers07944692008-10-30 01:18:59 +01001234 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1235 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001236
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001237 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001238
1239 err:
Shaohua Li90905892009-04-07 10:24:29 +08001240 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001241 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001242 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001243 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001244 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001245
1246 err_detach:
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001247 acpi_detach_data(device->handle, acpi_scan_drop_device);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001248 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Zhang Rui9e89dde2006-12-07 20:56:16 +08001251/* --------------------------------------------------------------------------
1252 Driver Management
1253 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001255 * acpi_bus_register_driver - register a driver with the ACPI bus
1256 * @driver: driver being registered
1257 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001259 * devices that match the driver's criteria and binds. Returns zero for
1260 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 */
Len Brown4be44fc2005-08-05 00:44:28 -04001262int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Patrick Mochel1890a972006-12-07 20:56:31 +08001264 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001267 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001268 driver->drv.name = driver->name;
1269 driver->drv.bus = &acpi_bus_type;
1270 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Patrick Mochel1890a972006-12-07 20:56:31 +08001272 ret = driver_register(&driver->drv);
1273 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Len Brown4be44fc2005-08-05 00:44:28 -04001276EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278/**
Hanjun Guob27b14c2013-09-22 15:42:41 +08001279 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001280 * @driver: driver to unregister
1281 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1283 * devices that match the driver's criteria and unbinds.
1284 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001285void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Patrick Mochel1890a972006-12-07 20:56:31 +08001287 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
Len Brown4be44fc2005-08-05 00:44:28 -04001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290EXPORT_SYMBOL(acpi_bus_unregister_driver);
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292/* --------------------------------------------------------------------------
1293 Device Enumeration
1294 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001295static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1296{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001297 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001298 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001299
1300 /*
1301 * Fixed hardware devices do not appear in the namespace and do not
1302 * have handles, but we fabricate acpi_devices for them, so we have
1303 * to deal with them specially.
1304 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001305 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001306 return acpi_root;
1307
1308 do {
1309 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001310 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001311 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1312 } while (acpi_bus_get_device(handle, &device));
1313 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001314}
1315
Len Brownc8f7a622006-07-09 17:22:28 -04001316acpi_status
1317acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1318{
1319 acpi_status status;
1320 acpi_handle tmp;
1321 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1322 union acpi_object *obj;
1323
1324 status = acpi_get_handle(handle, "_EJD", &tmp);
1325 if (ACPI_FAILURE(status))
1326 return status;
1327
1328 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1329 if (ACPI_SUCCESS(status)) {
1330 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001331 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1332 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001333 kfree(buffer.pointer);
1334 }
1335 return status;
1336}
1337EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1338
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001339static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1340 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001342 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1343 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001345 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001346 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001348 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001349 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001351 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001353 /* _PRW */
1354 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1355 if (ACPI_FAILURE(status)) {
1356 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001357 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001358 }
1359
1360 package = (union acpi_object *)buffer.pointer;
1361
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001362 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001363 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001366 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001367 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (element->type == ACPI_TYPE_PACKAGE) {
1370 if ((element->package.count < 2) ||
1371 (element->package.elements[0].type !=
1372 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001373 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001374 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001375
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001376 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001378 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 (u32) element->package.elements[1].integer.value;
1380 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001381 wakeup->gpe_device = NULL;
1382 wakeup->gpe_number = element->integer.value;
1383 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001384 goto out;
1385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001388 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001389 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001390
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001391 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001393 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1394 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001395 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001397 if (!list_empty(&wakeup->resources)) {
1398 int sleep_state;
1399
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001400 err = acpi_power_wakeup_list_init(&wakeup->resources,
1401 &sleep_state);
1402 if (err) {
1403 acpi_handle_warn(handle, "Retrieving current states "
1404 "of wakeup power resources failed\n");
1405 acpi_power_resources_list_free(&wakeup->resources);
1406 goto out;
1407 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001408 if (sleep_state < wakeup->sleep_state) {
1409 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1410 "(S%d) by S%d from power resources\n",
1411 (int)wakeup->sleep_state, sleep_state);
1412 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
Lin Mingbba63a22010-12-13 13:39:17 +08001415 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001416
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001417 out:
1418 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001419 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001422static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001424 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001425 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001426 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001427 {"PNP0C0E", 0},
1428 {"", 0},
1429 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001430 acpi_status status;
1431 acpi_event_status event_status;
1432
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001433 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001434
1435 /* Power button, Lid switch always enable wakeup */
1436 if (!acpi_match_device_ids(device, button_device_ids)) {
1437 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001438 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1439 /* Do not use Lid/sleep button for S5 wakeup */
1440 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1441 device->wakeup.sleep_state = ACPI_STATE_S4;
1442 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001443 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001444 return;
1445 }
1446
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +02001447 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1448 device->wakeup.gpe_number,
1449 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001450 if (status == AE_OK)
1451 device->wakeup.flags.run_wake =
1452 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1453}
1454
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001455static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001456{
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001457 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001458
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001459 /* Presence of _PRW indicates wake capable */
Jiang Liu952c63e2013-06-29 00:24:38 +08001460 if (!acpi_has_method(device->handle, "_PRW"))
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001461 return;
1462
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001463 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1464 &device->wakeup);
1465 if (err) {
1466 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001467 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001471 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001472 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001473 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1474 * system for the ACPI device with the _PRW object.
1475 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1476 * So it is necessary to call _DSW object first. Only when it is not
1477 * present will the _PSW object used.
1478 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001479 err = acpi_device_sleep_wake(device, 0, 0, 0);
1480 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001481 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1482 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001485static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001487 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001488 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1489 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001490 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001491
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001492 INIT_LIST_HEAD(&ps->resources);
1493
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001494 /* Evaluate "_PRx" to get referenced power resources */
1495 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1496 if (ACPI_SUCCESS(status)) {
1497 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001498
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001499 if (buffer.length && package
1500 && package->type == ACPI_TYPE_PACKAGE
1501 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001502 int err = acpi_extract_power_resources(package, 0,
1503 &ps->resources);
1504 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001505 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001506 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001507 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001508 }
1509
1510 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001511 pathname[2] = 'S';
Jiang Liu952c63e2013-06-29 00:24:38 +08001512 if (acpi_has_method(device->handle, pathname))
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001513 ps->flags.explicit_set = 1;
1514
1515 /*
1516 * State is valid if there are means to put the device into it.
1517 * D3hot is only valid if _PR3 present.
1518 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001519 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001520 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1521 ps->flags.valid = 1;
1522 ps->flags.os_accessible = 1;
1523 }
1524
1525 ps->power = -1; /* Unknown - driver assigned */
1526 ps->latency = -1; /* Unknown - driver assigned */
1527}
1528
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001529static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001531 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001533 /* Presence of _PS0|_PR0 indicates 'power manageable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001534 if (!acpi_has_method(device->handle, "_PS0") &&
1535 !acpi_has_method(device->handle, "_PR0"))
1536 return;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001537
1538 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001541 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 */
Jiang Liu952c63e2013-06-29 00:24:38 +08001543 if (acpi_has_method(device->handle, "_PSC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001544 device->power.flags.explicit_get = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001545 if (acpi_has_method(device->handle, "_IRC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001546 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001549 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001551 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1552 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001554 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Zhang Rui9e89dde2006-12-07 20:56:16 +08001556 /* Set defaults for D0 and D3 states (always valid) */
1557 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1558 device->power.states[ACPI_STATE_D0].power = 100;
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +02001559 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1560 device->power.states[ACPI_STATE_D3_COLD].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001562 /* Set D3cold's explicit_set flag if _PS3 exists. */
1563 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1564 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1565
Aaron Lu1399dfc2012-11-21 23:33:40 +01001566 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1567 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1568 device->power.flags.power_resources)
1569 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1570
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001571 if (acpi_bus_init_power(device)) {
1572 acpi_free_power_resources_lists(device);
1573 device->flags.power_manageable = 0;
1574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001577static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 /* Presence of _STA indicates 'dynamic_status' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001580 if (acpi_has_method(device->handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 device->flags.dynamic_status = 1;
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 /* Presence of _RMV indicates 'removable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001584 if (acpi_has_method(device->handle, "_RMV"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 device->flags.removable = 1;
1586
1587 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001588 if (acpi_has_method(device->handle, "_EJD") ||
1589 acpi_has_method(device->handle, "_EJ0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 device->flags.ejectable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001593static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Len Brown4be44fc2005-08-05 00:44:28 -04001595 char bus_id[5] = { '?', 0 };
1596 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1597 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 /*
1600 * Bus ID
1601 * ------
1602 * The device's Bus ID is simply the object name.
1603 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1604 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001605 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001607 return;
1608 }
1609
1610 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 case ACPI_BUS_TYPE_POWER_BUTTON:
1612 strcpy(device->pnp.bus_id, "PWRF");
1613 break;
1614 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1615 strcpy(device->pnp.bus_id, "SLPF");
1616 break;
1617 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001618 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 /* Clean up trailing underscores (if any) */
1620 for (i = 3; i > 1; i--) {
1621 if (bus_id[i] == '_')
1622 bus_id[i] = '\0';
1623 else
1624 break;
1625 }
1626 strcpy(device->pnp.bus_id, bus_id);
1627 break;
1628 }
1629}
1630
Zhang Rui54735262007-01-11 02:09:09 -05001631/*
Jiang Liuebf4df82013-06-29 00:24:41 +08001632 * acpi_ata_match - see if an acpi object is an ATA device
1633 *
1634 * If an acpi object has one of the ACPI ATA methods defined,
1635 * then we can safely call it an ATA device.
1636 */
1637bool acpi_ata_match(acpi_handle handle)
1638{
1639 return acpi_has_method(handle, "_GTF") ||
1640 acpi_has_method(handle, "_GTM") ||
1641 acpi_has_method(handle, "_STM") ||
1642 acpi_has_method(handle, "_SDD");
1643}
1644
1645/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001646 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001647 *
1648 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1649 * then we can safely call it an ejectable drive bay
1650 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001651bool acpi_bay_match(acpi_handle handle)
Toshi Kanid4e1a692013-03-04 21:30:41 +00001652{
Zhang Rui54735262007-01-11 02:09:09 -05001653 acpi_handle phandle;
1654
Jiang Liu952c63e2013-06-29 00:24:38 +08001655 if (!acpi_has_method(handle, "_EJ0"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001656 return false;
1657 if (acpi_ata_match(handle))
1658 return true;
1659 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1660 return false;
Zhang Rui54735262007-01-11 02:09:09 -05001661
Jiang Liuebf4df82013-06-29 00:24:41 +08001662 return acpi_ata_match(phandle);
Zhang Rui54735262007-01-11 02:09:09 -05001663}
1664
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001665bool acpi_device_is_battery(struct acpi_device *adev)
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001666{
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001667 struct acpi_hardware_id *hwid;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001668
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001669 list_for_each_entry(hwid, &adev->pnp.ids, list)
1670 if (!strcmp("PNP0C0A", hwid->id))
1671 return true;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001672
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001673 return false;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001674}
1675
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001676static bool is_ejectable_bay(struct acpi_device *adev)
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001677{
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001678 acpi_handle handle = adev->handle;
1679
1680 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001681 return true;
1682
1683 return acpi_bay_match(handle);
1684}
1685
Frank Seidel3620f2f2007-12-07 13:20:34 +01001686/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001687 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001688 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001689bool acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001690{
Jiang Liuebf4df82013-06-29 00:24:41 +08001691 return acpi_has_method(handle, "_DCK");
Frank Seidel3620f2f2007-12-07 13:20:34 +01001692}
1693
Thomas Renninger620e1122010-10-01 10:54:00 +02001694const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001695{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001696 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001697
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001698 if (list_empty(&device->pnp.ids))
1699 return dummy_hid;
1700
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001701 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1702 return hid->id;
1703}
1704EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001705
Toshi Kanid4e1a692013-03-04 21:30:41 +00001706static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001707{
1708 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001709
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001710 id = kmalloc(sizeof(*id), GFP_KERNEL);
1711 if (!id)
1712 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001713
Thomas Meyer581de592011-08-06 11:32:56 +02001714 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001715 if (!id->id) {
1716 kfree(id);
1717 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001718 }
1719
Toshi Kanid4e1a692013-03-04 21:30:41 +00001720 list_add_tail(&id->list, &pnp->ids);
1721 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001722}
1723
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001724/*
1725 * Old IBM workstations have a DSDT bug wherein the SMBus object
1726 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1727 * prefix. Work around this.
1728 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001729static bool acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001730{
Jiang Liuebf4df82013-06-29 00:24:41 +08001731 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1732 struct acpi_buffer path = { sizeof(node_name), node_name };
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001733
1734 if (!dmi_name_in_vendors("IBM"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001735 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001736
1737 /* Look for SMBS object */
Jiang Liuebf4df82013-06-29 00:24:41 +08001738 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1739 strcmp("SMBS", path.pointer))
1740 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001741
1742 /* Does it have the necessary (but misnamed) methods? */
Jiang Liu952c63e2013-06-29 00:24:38 +08001743 if (acpi_has_method(handle, "SBI") &&
1744 acpi_has_method(handle, "SBR") &&
1745 acpi_has_method(handle, "SBW"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001746 return true;
1747
1748 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001749}
1750
Toshi Kanic0af4172013-03-04 21:30:42 +00001751static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1752 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
Len Brown4be44fc2005-08-05 00:44:28 -04001754 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001755 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001756 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001757 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Toshi Kanic0af4172013-03-04 21:30:42 +00001759 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00001761 if (handle == ACPI_ROOT_OBJECT) {
1762 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001763 break;
1764 }
1765
Toshi Kanic0af4172013-03-04 21:30:42 +00001766 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001768 pr_err(PREFIX "%s: Error reading device info\n",
1769 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 return;
1771 }
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (info->valid & ACPI_VALID_HID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001774 acpi_add_id(pnp, info->hardware_id.string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001775 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001776 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001777 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00001778 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001781 pnp->bus_address = info->address;
1782 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
Lv Zhengccf78042012-10-30 14:41:07 +01001784 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001785 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01001786 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001787
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001788 kfree(info);
1789
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001790 /*
1791 * Some devices don't reliably have _HIDs & _CIDs, so add
1792 * synthetic HIDs to make sure drivers can find them.
1793 */
Toshi Kanic0af4172013-03-04 21:30:42 +00001794 if (acpi_is_video_device(handle))
1795 acpi_add_id(pnp, ACPI_VIDEO_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001796 else if (acpi_bay_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001797 acpi_add_id(pnp, ACPI_BAY_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001798 else if (acpi_dock_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001799 acpi_add_id(pnp, ACPI_DOCK_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001800 else if (acpi_ibm_smbus_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001801 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1802 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1803 acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1804 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1805 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001806 }
Zhang Rui54735262007-01-11 02:09:09 -05001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 break;
1809 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00001810 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 break;
1812 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00001813 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00001816 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 break;
1818 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001819 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 break;
1821 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001822 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 break;
1824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Toshi Kanic0af4172013-03-04 21:30:42 +00001827void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1828{
1829 struct acpi_hardware_id *id, *tmp;
1830
1831 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1832 kfree(id->id);
1833 kfree(id);
1834 }
1835 kfree(pnp->unique_id);
1836}
1837
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001838void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1839 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001841 INIT_LIST_HEAD(&device->pnp.ids);
1842 device->device_type = type;
1843 device->handle = handle;
1844 device->parent = acpi_bus_get_parent(handle);
Rafael J. Wysocki25db1152013-11-22 21:56:06 +01001845 acpi_set_device_status(device, sta);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001846 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00001847 acpi_set_pnp_ids(handle, &device->pnp, type);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001848 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001849 device->flags.match_driver = false;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001850 device->flags.initialized = true;
1851 device->flags.visited = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001852 device_initialize(&device->dev);
1853 dev_set_uevent_suppress(&device->dev, true);
1854}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001855
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001856void acpi_device_add_finalize(struct acpi_device *device)
1857{
1858 dev_set_uevent_suppress(&device->dev, false);
1859 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001862static int acpi_add_single_object(struct acpi_device **child,
1863 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001864 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001866 int result;
1867 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001868 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Burman Yan36bcbec2006-12-19 12:56:11 -08001870 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001872 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001873 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001876 acpi_init_device_object(device, handle, type, sta);
1877 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001878 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001880 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001881 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001882 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001883 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
1885
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001886 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001887 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001888 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1889 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1890 dev_name(&device->dev), (char *) buffer.pointer,
1891 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1892 kfree(buffer.pointer);
1893 *child = device;
1894 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001895}
1896
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001897static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1898 unsigned long long *sta)
1899{
1900 acpi_status status;
1901 acpi_object_type acpi_type;
1902
1903 status = acpi_get_type(handle, &acpi_type);
1904 if (ACPI_FAILURE(status))
1905 return -ENODEV;
1906
1907 switch (acpi_type) {
1908 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1909 case ACPI_TYPE_DEVICE:
1910 *type = ACPI_BUS_TYPE_DEVICE;
1911 status = acpi_bus_get_status_handle(handle, sta);
1912 if (ACPI_FAILURE(status))
1913 return -ENODEV;
1914 break;
1915 case ACPI_TYPE_PROCESSOR:
1916 *type = ACPI_BUS_TYPE_PROCESSOR;
1917 status = acpi_bus_get_status_handle(handle, sta);
1918 if (ACPI_FAILURE(status))
1919 return -ENODEV;
1920 break;
1921 case ACPI_TYPE_THERMAL:
1922 *type = ACPI_BUS_TYPE_THERMAL;
1923 *sta = ACPI_STA_DEFAULT;
1924 break;
1925 case ACPI_TYPE_POWER:
1926 *type = ACPI_BUS_TYPE_POWER;
1927 *sta = ACPI_STA_DEFAULT;
1928 break;
1929 default:
1930 return -ENODEV;
1931 }
1932
1933 return 0;
1934}
1935
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001936bool acpi_device_is_present(struct acpi_device *adev)
1937{
1938 if (adev->status.present || adev->status.functional)
1939 return true;
1940
1941 adev->flags.initialized = false;
1942 return false;
1943}
1944
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001945static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1946 char *idstr,
1947 const struct acpi_device_id **matchid)
1948{
1949 const struct acpi_device_id *devid;
1950
1951 for (devid = handler->ids; devid->id[0]; devid++)
1952 if (!strcmp((char *)devid->id, idstr)) {
1953 if (matchid)
1954 *matchid = devid;
1955
1956 return true;
1957 }
1958
1959 return false;
1960}
1961
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001962static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1963 const struct acpi_device_id **matchid)
1964{
1965 struct acpi_scan_handler *handler;
1966
1967 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1968 if (acpi_scan_handler_matching(handler, idstr, matchid))
1969 return handler;
1970
1971 return NULL;
1972}
1973
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001974void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1975{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001976 if (!!hotplug->enabled == !!val)
1977 return;
1978
1979 mutex_lock(&acpi_scan_lock);
1980
1981 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001982
1983 mutex_unlock(&acpi_scan_lock);
1984}
1985
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01001986static void acpi_scan_init_hotplug(struct acpi_device *adev)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001987{
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001988 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001989
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001990 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001991 acpi_dock_add(adev);
1992 return;
1993 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01001994 list_for_each_entry(hwid, &adev->pnp.ids, list) {
1995 struct acpi_scan_handler *handler;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001996
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001997 handler = acpi_scan_match_handler(hwid->id, NULL);
Rafael J. Wysocki1a699472014-02-06 13:58:13 +01001998 if (handler) {
1999 adev->flags.hotplug_notify = true;
2000 break;
2001 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002002 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01002003}
2004
Rafael J. Wysockie3863092012-12-21 00:36:47 +01002005static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
2006 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01002008 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00002009 int type;
2010 unsigned long long sta;
2011 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002012
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01002013 acpi_bus_get_device(handle, &device);
2014 if (device)
2015 goto out;
2016
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00002017 result = acpi_bus_type_and_status(handle, &type, &sta);
2018 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002019 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01002021 if (type == ACPI_BUS_TYPE_POWER) {
2022 acpi_add_power_resource(handle);
2023 return AE_OK;
2024 }
2025
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002026 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00002027 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002028 return AE_CTRL_DEPTH;
2029
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002030 acpi_scan_init_hotplug(device);
2031
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01002032 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002033 if (!*return_value)
2034 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01002035
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002036 return AE_OK;
2037}
2038
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002039static int acpi_scan_attach_handler(struct acpi_device *device)
2040{
2041 struct acpi_hardware_id *hwid;
2042 int ret = 0;
2043
2044 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002045 const struct acpi_device_id *devid;
2046 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002047
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002048 handler = acpi_scan_match_handler(hwid->id, &devid);
2049 if (handler) {
2050 ret = handler->attach(device, devid);
2051 if (ret > 0) {
2052 device->handler = handler;
2053 break;
2054 } else if (ret < 0) {
2055 break;
2056 }
2057 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002058 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002059 return ret;
2060}
2061
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002062static void acpi_bus_attach(struct acpi_device *device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002063{
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002064 struct acpi_device *child;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01002065 acpi_handle ejd;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002066 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002067
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01002068 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
2069 register_dock_dependent_device(device, ejd);
2070
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002071 acpi_bus_get_status(device);
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002072 /* Skip devices that are not present. */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002073 if (!acpi_device_is_present(device)) {
2074 device->flags.visited = false;
2075 return;
2076 }
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02002077 if (device->handler)
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002078 goto ok;
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02002079
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002080 if (!device->flags.initialized) {
2081 acpi_bus_update_power(device, NULL);
2082 device->flags.initialized = true;
2083 }
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002084 device->flags.visited = false;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002085 ret = acpi_scan_attach_handler(device);
Rafael J. Wysocki69310072013-11-07 01:41:01 +01002086 if (ret < 0)
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002087 return;
Rafael J. Wysocki69310072013-11-07 01:41:01 +01002088
2089 device->flags.match_driver = true;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002090 if (!ret) {
2091 ret = device_attach(&device->dev);
2092 if (ret < 0)
2093 return;
2094 }
2095 device->flags.visited = true;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002096
2097 ok:
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002098 list_for_each_entry(child, &device->children, node)
2099 acpi_bus_attach(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002102/**
2103 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2104 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01002105 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002106 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2107 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01002108 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002109 * If no devices were found, -ENODEV is returned, but it does not mean that
2110 * there has been a real error. There just have been no suitable ACPI objects
2111 * in the table trunk from which the kernel could create a device and add an
2112 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002113 *
2114 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01002115 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002116int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07002117{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 void *device = NULL;
Rajesh Shah3fb02732005-04-28 00:25:52 -07002119
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002120 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01002122 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002123
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002124 if (device) {
2125 acpi_bus_attach(device);
2126 return 0;
2127 }
2128 return -ENODEV;
Rajesh Shah3fb02732005-04-28 00:25:52 -07002129}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002130EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002132/**
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002133 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2134 * @adev: Root of the ACPI namespace scope to walk.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002135 *
2136 * Must be called under acpi_scan_lock.
2137 */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002138void acpi_bus_trim(struct acpi_device *adev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002140 struct acpi_scan_handler *handler = adev->handler;
2141 struct acpi_device *child;
2142
2143 list_for_each_entry_reverse(child, &adev->children, node)
2144 acpi_bus_trim(child);
2145
Rafael J. Wysockia951c772014-01-27 23:08:09 +01002146 adev->flags.match_driver = false;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002147 if (handler) {
2148 if (handler->detach)
2149 handler->detach(adev);
2150
2151 adev->handler = NULL;
2152 } else {
2153 device_release_driver(&adev->dev);
2154 }
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002155 /*
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002156 * Most likely, the device is going away, so put it into D3cold before
2157 * that.
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002158 */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002159 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2160 adev->flags.initialized = false;
2161 adev->flags.visited = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
Kristen Accardiceaba662006-02-23 17:56:01 -08002163EXPORT_SYMBOL_GPL(acpi_bus_trim);
2164
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00002165static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
Len Brown4be44fc2005-08-05 00:44:28 -04002167 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08002168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 /*
2170 * Enumerate all fixed-feature devices.
2171 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002172 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2173 struct acpi_device *device = NULL;
2174
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002175 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002176 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002177 ACPI_STA_DEFAULT);
2178 if (result)
2179 return result;
2180
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002181 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002182 result = device_attach(&device->dev);
2183 if (result < 0)
2184 return result;
2185
Daniel Drakec10d7a12012-05-10 00:08:43 +01002186 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002189 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2190 struct acpi_device *device = NULL;
2191
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002192 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002193 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002194 ACPI_STA_DEFAULT);
2195 if (result)
2196 return result;
2197
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002198 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002199 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002202 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
2204
Bjorn Helgaase747f272009-03-24 16:49:43 -06002205int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
2207 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Patrick Mochel5b327262006-05-10 10:33:00 -04002209 result = bus_register(&acpi_bus_type);
2210 if (result) {
2211 /* We don't want to quit even if we failed to add suspend/resume */
2212 printk(KERN_ERR PREFIX "Could not register bus type\n");
2213 }
2214
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01002215 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01002216 acpi_pci_link_init();
Rafael J. Wysockiac212b62013-05-03 00:26:22 +02002217 acpi_processor_init();
Rafael J. Wysocki141a2972013-01-30 14:27:40 +01002218 acpi_platform_init();
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01002219 acpi_lpss_init();
Lan Tianyu2fa97fe2013-06-05 02:27:50 +00002220 acpi_cmos_rtc_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01002221 acpi_container_init();
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01002222 acpi_memory_hotplug_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01002223
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002224 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 * Enumerate devices in the ACPI namespace.
2227 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002228 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002230 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002232 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002234 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002236 result = acpi_bus_scan_fixed();
2237 if (result) {
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002238 acpi_detach_data(acpi_root->handle, acpi_scan_drop_device);
2239 acpi_device_del(acpi_root);
2240 put_device(&acpi_root->dev);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002241 goto out;
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002242 }
2243
2244 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002245
2246 out:
2247 mutex_unlock(&acpi_scan_lock);
2248 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}