blob: 8bb48bfab1df17cf74fc361ad91703c7a5d3b940 [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. Wysocki3c2cc7f2014-02-06 17:31:37 +0100490 if (adev->flags.hotplug_notify) {
491 error = acpi_generic_hotplug_event(adev, src);
Rafael J. Wysockidd2151b2014-02-04 00:44:02 +0100492 if (error == -EPERM) {
493 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
494 goto err_out;
495 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100496 } else {
497 int (*event)(struct acpi_device *, u32);
498
499 acpi_lock_hp_context();
500 event = adev->hp ? adev->hp->event : NULL;
501 acpi_unlock_hp_context();
502 /*
503 * There may be additional notify handlers for device objects
504 * without the .event() callback, so ignore them here.
505 */
506 if (event)
507 error = event(adev, src);
508 else
509 goto out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100510 }
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100511 if (!error)
512 ost_code = ACPI_OST_SC_SUCCESS;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100513
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100514 err_out:
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100515 acpi_evaluate_hotplug_ost(adev->handle, src, ost_code, NULL);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100516
517 out:
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +0100518 acpi_bus_put_acpi_device(adev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100519 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200520 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100521}
522
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100523static ssize_t real_power_state_show(struct device *dev,
524 struct device_attribute *attr, char *buf)
525{
526 struct acpi_device *adev = to_acpi_device(dev);
527 int state;
528 int ret;
529
530 ret = acpi_device_get_power(adev, &state);
531 if (ret)
532 return ret;
533
534 return sprintf(buf, "%s\n", acpi_power_state_string(state));
535}
536
537static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
538
539static ssize_t power_state_show(struct device *dev,
540 struct device_attribute *attr, char *buf)
541{
542 struct acpi_device *adev = to_acpi_device(dev);
543
544 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
545}
546
547static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800550acpi_eject_store(struct device *d, struct device_attribute *attr,
551 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800553 struct acpi_device *acpi_device = to_acpi_device(d);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100554 acpi_object_type not_used;
555 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100557 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100560 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
561 && !acpi_device->driver)
562 return -ENODEV;
563
564 status = acpi_get_type(acpi_device->handle, &not_used);
565 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
566 return -ENODEV;
567
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100568 get_device(&acpi_device->dev);
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100569 status = acpi_hotplug_execute(acpi_device_hotplug, acpi_device,
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100570 ACPI_OST_EC_OSPM_EJECT);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200571 if (ACPI_SUCCESS(status))
572 return count;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100573
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200574 put_device(&acpi_device->dev);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200575 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100576 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100577 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800580static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800582static ssize_t
583acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
584 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600586 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800587}
588static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
589
Lv Zheng923d4a42012-10-30 14:43:26 +0100590static ssize_t acpi_device_uid_show(struct device *dev,
591 struct device_attribute *attr, char *buf)
592{
593 struct acpi_device *acpi_dev = to_acpi_device(dev);
594
595 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
596}
597static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
598
599static ssize_t acpi_device_adr_show(struct device *dev,
600 struct device_attribute *attr, char *buf)
601{
602 struct acpi_device *acpi_dev = to_acpi_device(dev);
603
604 return sprintf(buf, "0x%08x\n",
605 (unsigned int)(acpi_dev->pnp.bus_address));
606}
607static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
608
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800609static ssize_t
610acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
611 struct acpi_device *acpi_dev = to_acpi_device(dev);
612 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
613 int result;
614
615 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600616 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800617 goto end;
618
619 result = sprintf(buf, "%s\n", (char*)path.pointer);
620 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600621end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800622 return result;
623}
624static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
625
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600626/* sysfs file that shows description text from the ACPI _STR method */
627static ssize_t description_show(struct device *dev,
628 struct device_attribute *attr,
629 char *buf) {
630 struct acpi_device *acpi_dev = to_acpi_device(dev);
631 int result;
632
633 if (acpi_dev->pnp.str_obj == NULL)
634 return 0;
635
636 /*
637 * The _STR object contains a Unicode identifier for a device.
638 * We need to convert to utf-8 so it can be displayed.
639 */
640 result = utf16s_to_utf8s(
641 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
642 acpi_dev->pnp.str_obj->buffer.length,
643 UTF16_LITTLE_ENDIAN, buf,
644 PAGE_SIZE);
645
646 buf[result++] = '\n';
647
648 return result;
649}
650static DEVICE_ATTR(description, 0444, description_show, NULL);
651
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100652static ssize_t
653acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
654 char *buf) {
655 struct acpi_device *acpi_dev = to_acpi_device(dev);
656
657 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
658}
659static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
660
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800661static ssize_t status_show(struct device *dev, struct device_attribute *attr,
662 char *buf) {
663 struct acpi_device *acpi_dev = to_acpi_device(dev);
664 acpi_status status;
665 unsigned long long sta;
666
667 status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
668 if (ACPI_FAILURE(status))
669 return -ENODEV;
670
671 return sprintf(buf, "%llu\n", sta);
672}
673static DEVICE_ATTR_RO(status);
674
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800675static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600677 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800678 acpi_status status;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100679 unsigned long long sun;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800680 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800682 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800683 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800684 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600685 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800686 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600687 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800688 goto end;
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200691 if (!list_empty(&dev->pnp.ids)) {
692 result = device_create_file(&dev->dev, &dev_attr_hid);
693 if (result)
694 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200696 result = device_create_file(&dev->dev, &dev_attr_modalias);
697 if (result)
698 goto end;
699 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200700
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600701 /*
702 * If device has _STR, 'description' file is created
703 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800704 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600705 status = acpi_evaluate_object(dev->handle, "_STR",
706 NULL, &buffer);
707 if (ACPI_FAILURE(status))
708 buffer.pointer = NULL;
709 dev->pnp.str_obj = buffer.pointer;
710 result = device_create_file(&dev->dev, &dev_attr_description);
711 if (result)
712 goto end;
713 }
714
Toshi Kanid4e1a692013-03-04 21:30:41 +0000715 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100716 result = device_create_file(&dev->dev, &dev_attr_adr);
717 if (dev->pnp.unique_id)
718 result = device_create_file(&dev->dev, &dev_attr_uid);
719
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100720 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
721 if (ACPI_SUCCESS(status)) {
722 dev->pnp.sun = (unsigned long)sun;
723 result = device_create_file(&dev->dev, &dev_attr_sun);
724 if (result)
725 goto end;
726 } else {
727 dev->pnp.sun = (unsigned long)-1;
728 }
729
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800730 if (acpi_has_method(dev->handle, "_STA")) {
731 result = device_create_file(&dev->dev, &dev_attr_status);
732 if (result)
733 goto end;
734 }
735
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800736 /*
737 * If device has _EJ0, 'eject' file is created that is used to trigger
738 * hot-removal function from userland.
739 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800740 if (acpi_has_method(dev->handle, "_EJ0")) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800741 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100742 if (result)
743 return result;
744 }
745
746 if (dev->flags.power_manageable) {
747 result = device_create_file(&dev->dev, &dev_attr_power_state);
748 if (result)
749 return result;
750
751 if (dev->power.flags.power_resources)
752 result = device_create_file(&dev->dev,
753 &dev_attr_real_power_state);
754 }
755
Alex Chiang0c526d92009-05-14 08:31:37 -0600756end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800757 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800758}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800760static void acpi_device_remove_files(struct acpi_device *dev)
761{
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100762 if (dev->flags.power_manageable) {
763 device_remove_file(&dev->dev, &dev_attr_power_state);
764 if (dev->power.flags.power_resources)
765 device_remove_file(&dev->dev,
766 &dev_attr_real_power_state);
767 }
768
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800769 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600770 * If device has _STR, remove 'description' file
771 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800772 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600773 kfree(dev->pnp.str_obj);
774 device_remove_file(&dev->dev, &dev_attr_description);
775 }
776 /*
777 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800778 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800779 if (acpi_has_method(dev->handle, "_EJ0"))
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800780 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800781
Jiang Liu952c63e2013-06-29 00:24:38 +0800782 if (acpi_has_method(dev->handle, "_SUN"))
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100783 device_remove_file(&dev->dev, &dev_attr_sun);
784
Lv Zheng923d4a42012-10-30 14:43:26 +0100785 if (dev->pnp.unique_id)
786 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000787 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100788 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600789 device_remove_file(&dev->dev, &dev_attr_modalias);
790 device_remove_file(&dev->dev, &dev_attr_hid);
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800791 if (acpi_has_method(dev->handle, "_STA"))
792 device_remove_file(&dev->dev, &dev_attr_status);
Alex Chiang0c526d92009-05-14 08:31:37 -0600793 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800794 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800795}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800797 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200799
Mika Westerbergcf761af2012-10-31 22:44:41 +0100800static const struct acpi_device_id *__acpi_match_device(
801 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200802{
803 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600804 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200805
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800806 /*
807 * If the device is not present, it is unnecessary to load device
808 * driver for it.
809 */
810 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100811 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800812
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600813 for (id = ids; id->id[0]; id++)
814 list_for_each_entry(hwid, &device->pnp.ids, list)
815 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100816 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200817
Mika Westerbergcf761af2012-10-31 22:44:41 +0100818 return NULL;
819}
820
821/**
822 * acpi_match_device - Match a struct device against a given list of ACPI IDs
823 * @ids: Array of struct acpi_device_id object to match against.
824 * @dev: The device structure to match.
825 *
826 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
827 * object for that handle and use that object to match against a given list of
828 * device IDs.
829 *
830 * Return a pointer to the first matching ID on success or %NULL on failure.
831 */
832const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
833 const struct device *dev)
834{
835 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100836 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100837
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100838 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100839 return NULL;
840
841 return __acpi_match_device(adev, ids);
842}
843EXPORT_SYMBOL_GPL(acpi_match_device);
844
845int acpi_match_device_ids(struct acpi_device *device,
846 const struct acpi_device_id *ids)
847{
848 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200849}
850EXPORT_SYMBOL(acpi_match_device_ids);
851
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100852static void acpi_free_power_resources_lists(struct acpi_device *device)
853{
854 int i;
855
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100856 if (device->wakeup.flags.valid)
857 acpi_power_resources_list_free(&device->wakeup.resources);
858
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100859 if (!device->flags.power_manageable)
860 return;
861
862 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
863 struct acpi_device_power_state *ps = &device->power.states[i];
864 acpi_power_resources_list_free(&ps->resources);
865 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600866}
867
Patrick Mochel1890a972006-12-07 20:56:31 +0800868static void acpi_device_release(struct device *dev)
869{
870 struct acpi_device *acpi_dev = to_acpi_device(dev);
871
Toshi Kanic0af4172013-03-04 21:30:42 +0000872 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100873 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800874 kfree(acpi_dev);
875}
876
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800877static int acpi_bus_match(struct device *dev, struct device_driver *drv)
878{
879 struct acpi_device *acpi_dev = to_acpi_device(dev);
880 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
881
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100882 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +0100883 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800884}
885
Kay Sievers7eff2e72007-08-14 15:15:12 +0200886static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800887{
888 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200889 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800890
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200891 if (list_empty(&acpi_dev->pnp.ids))
892 return 0;
893
Kay Sievers7eff2e72007-08-14 15:15:12 +0200894 if (add_uevent_var(env, "MODALIAS="))
895 return -ENOMEM;
896 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
897 sizeof(env->buf) - env->buflen);
Zhang Rui3d8e0092014-01-14 16:46:35 +0800898 if (len <= 0)
899 return len;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200900 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return 0;
902}
903
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000904static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
905{
906 struct acpi_device *device = data;
907
908 device->driver->ops.notify(device, event);
909}
910
911static acpi_status acpi_device_notify_fixed(void *data)
912{
913 struct acpi_device *device = data;
914
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000915 /* Fixed hardware devices have no handles */
916 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000917 return AE_OK;
918}
919
920static int acpi_device_install_notify_handler(struct acpi_device *device)
921{
922 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000923
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000924 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000925 status =
926 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
927 acpi_device_notify_fixed,
928 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000929 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000930 status =
931 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
932 acpi_device_notify_fixed,
933 device);
934 else
935 status = acpi_install_notify_handler(device->handle,
936 ACPI_DEVICE_NOTIFY,
937 acpi_device_notify,
938 device);
939
940 if (ACPI_FAILURE(status))
941 return -EINVAL;
942 return 0;
943}
944
945static void acpi_device_remove_notify_handler(struct acpi_device *device)
946{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000947 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000948 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
949 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000950 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000951 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
952 acpi_device_notify_fixed);
953 else
954 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
955 acpi_device_notify);
956}
957
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200958static int acpi_device_probe(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800959{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800960 struct acpi_device *acpi_dev = to_acpi_device(dev);
961 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
962 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Rafael J. Wysocki24071f42013-06-16 00:36:41 +0200964 if (acpi_dev->handler)
965 return -EINVAL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000966
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200967 if (!acpi_drv->ops.add)
968 return -ENOSYS;
Li Shaohuac4168bf2006-12-07 20:56:41 +0800969
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200970 ret = acpi_drv->ops.add(acpi_dev);
971 if (ret)
972 return ret;
973
974 acpi_dev->driver = acpi_drv;
975 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
976 "Driver [%s] successfully bound to device [%s]\n",
977 acpi_drv->name, acpi_dev->pnp.bus_id));
978
979 if (acpi_drv->ops.notify) {
980 ret = acpi_device_install_notify_handler(acpi_dev);
981 if (ret) {
982 if (acpi_drv->ops.remove)
983 acpi_drv->ops.remove(acpi_dev);
984
985 acpi_dev->driver = NULL;
986 acpi_dev->driver_data = NULL;
987 return ret;
988 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800989 }
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200990
991 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
992 acpi_drv->name, acpi_dev->pnp.bus_id));
993 get_device(dev);
994 return 0;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800995}
996
997static int acpi_device_remove(struct device * dev)
998{
999 struct acpi_device *acpi_dev = to_acpi_device(dev);
1000 struct acpi_driver *acpi_drv = acpi_dev->driver;
1001
1002 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001003 if (acpi_drv->ops.notify)
1004 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001005 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001006 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001007 }
1008 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001009 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001010
1011 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +08001012 return 0;
1013}
1014
David Brownell55955aa2007-05-08 00:28:35 -07001015struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +08001016 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001017 .match = acpi_bus_match,
1018 .probe = acpi_device_probe,
1019 .remove = acpi_device_remove,
1020 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021};
1022
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001023static void acpi_device_del(struct acpi_device *device)
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001024{
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001025 mutex_lock(&acpi_device_lock);
1026 if (device->parent)
1027 list_del(&device->node);
1028
1029 list_del(&device->wakeup_list);
1030 mutex_unlock(&acpi_device_lock);
1031
1032 acpi_power_add_remove_device(device, false);
1033 acpi_device_remove_files(device);
1034 if (device->remove)
1035 device->remove(device);
1036
1037 device_del(&device->dev);
1038}
1039
1040static LIST_HEAD(acpi_device_del_list);
1041static DEFINE_MUTEX(acpi_device_del_lock);
1042
1043static void acpi_device_del_work_fn(struct work_struct *work_not_used)
1044{
1045 for (;;) {
1046 struct acpi_device *adev;
1047
1048 mutex_lock(&acpi_device_del_lock);
1049
1050 if (list_empty(&acpi_device_del_list)) {
1051 mutex_unlock(&acpi_device_del_lock);
1052 break;
1053 }
1054 adev = list_first_entry(&acpi_device_del_list,
1055 struct acpi_device, del_list);
1056 list_del(&adev->del_list);
1057
1058 mutex_unlock(&acpi_device_del_lock);
1059
1060 acpi_device_del(adev);
1061 /*
1062 * Drop references to all power resources that might have been
1063 * used by the device.
1064 */
1065 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
1066 put_device(&adev->dev);
1067 }
1068}
1069
1070/**
1071 * acpi_scan_drop_device - Drop an ACPI device object.
1072 * @handle: Handle of an ACPI namespace node, not used.
1073 * @context: Address of the ACPI device object to drop.
1074 *
1075 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
1076 * namespace node the device object pointed to by @context is attached to.
1077 *
1078 * The unregistration is carried out asynchronously to avoid running
1079 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
1080 * ensure the correct ordering (the device objects must be unregistered in the
1081 * same order in which the corresponding namespace nodes are deleted).
1082 */
1083static void acpi_scan_drop_device(acpi_handle handle, void *context)
1084{
1085 static DECLARE_WORK(work, acpi_device_del_work_fn);
1086 struct acpi_device *adev = context;
1087
1088 mutex_lock(&acpi_device_del_lock);
1089
1090 /*
1091 * Use the ACPI hotplug workqueue which is ordered, so this work item
1092 * won't run after any hotplug work items submitted subsequently. That
1093 * prevents attempts to register device objects identical to those being
1094 * deleted from happening concurrently (such attempts result from
1095 * hotplug events handled via the ACPI hotplug workqueue). It also will
1096 * run after all of the work items submitted previosuly, which helps
1097 * those work items to ensure that they are not accessing stale device
1098 * objects.
1099 */
1100 if (list_empty(&acpi_device_del_list))
1101 acpi_queue_hotplug_work(&work);
1102
1103 list_add_tail(&adev->del_list, &acpi_device_del_list);
1104 /* Make acpi_ns_validate_handle() return NULL for this handle. */
1105 adev->handle = INVALID_ACPI_HANDLE;
1106
1107 mutex_unlock(&acpi_device_del_lock);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001108}
1109
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001110static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
1111 void (*callback)(void *))
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001112{
1113 acpi_status status;
1114
1115 if (!device)
1116 return -EINVAL;
1117
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001118 status = acpi_get_data_full(handle, acpi_scan_drop_device,
1119 (void **)device, callback);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001120 if (ACPI_FAILURE(status) || !*device) {
1121 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
1122 handle));
1123 return -ENODEV;
1124 }
1125 return 0;
1126}
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001127
1128int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
1129{
1130 return acpi_get_device_data(handle, device, NULL);
1131}
Rafael J. Wysocki65859252013-10-01 23:02:43 +02001132EXPORT_SYMBOL(acpi_bus_get_device);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001133
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001134static void get_acpi_device(void *dev)
1135{
1136 if (dev)
1137 get_device(&((struct acpi_device *)dev)->dev);
1138}
1139
1140struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
1141{
1142 struct acpi_device *adev = NULL;
1143
1144 acpi_get_device_data(handle, &adev, get_acpi_device);
1145 return adev;
1146}
1147
1148void acpi_bus_put_acpi_device(struct acpi_device *adev)
1149{
1150 put_device(&adev->dev);
1151}
1152
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001153int acpi_device_add(struct acpi_device *device,
1154 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001156 int result;
1157 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1158 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001159
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001160 if (device->handle) {
1161 acpi_status status;
1162
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001163 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001164 device);
1165 if (ACPI_FAILURE(status)) {
1166 acpi_handle_err(device->handle,
1167 "Unable to attach device data\n");
1168 return -ENODEV;
1169 }
1170 }
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /*
1173 * Linkage
1174 * -------
1175 * Link this device to its parent and siblings.
1176 */
1177 INIT_LIST_HEAD(&device->children);
1178 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001180 INIT_LIST_HEAD(&device->physical_node_list);
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001181 INIT_LIST_HEAD(&device->del_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001182 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001184 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1185 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001186 pr_err(PREFIX "Memory allocation error\n");
1187 result = -ENOMEM;
1188 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001189 }
1190
Shaohua Li90905892009-04-07 10:24:29 +08001191 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001192 /*
1193 * Find suitable bus_id and instance number in acpi_bus_id_list
1194 * If failed, create one and link it into acpi_bus_id_list
1195 */
1196 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001197 if (!strcmp(acpi_device_bus_id->bus_id,
1198 acpi_device_hid(device))) {
1199 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001200 found = 1;
1201 kfree(new_bus_id);
1202 break;
1203 }
1204 }
Alex Chiang0c526d92009-05-14 08:31:37 -06001205 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001206 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001207 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001208 acpi_device_bus_id->instance_no = 0;
1209 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1210 }
Kay Sievers07944692008-10-30 01:18:59 +01001211 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 +08001212
Len Brown33b57152008-12-15 22:09:26 -05001213 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -05001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (device->wakeup.flags.valid)
1217 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +08001218 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Patrick Mochel1890a972006-12-07 20:56:31 +08001220 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001221 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +08001222 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001223 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001224 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -06001225 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -06001226 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001227 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001228 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001229
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001230 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -06001231 if (result)
Kay Sievers07944692008-10-30 01:18:59 +01001232 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1233 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001234
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001235 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001236
1237 err:
Shaohua Li90905892009-04-07 10:24:29 +08001238 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001239 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001240 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001241 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001242 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001243
1244 err_detach:
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001245 acpi_detach_data(device->handle, acpi_scan_drop_device);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001246 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
Zhang Rui9e89dde2006-12-07 20:56:16 +08001249/* --------------------------------------------------------------------------
1250 Driver Management
1251 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001253 * acpi_bus_register_driver - register a driver with the ACPI bus
1254 * @driver: driver being registered
1255 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001257 * devices that match the driver's criteria and binds. Returns zero for
1258 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 */
Len Brown4be44fc2005-08-05 00:44:28 -04001260int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Patrick Mochel1890a972006-12-07 20:56:31 +08001262 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001265 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001266 driver->drv.name = driver->name;
1267 driver->drv.bus = &acpi_bus_type;
1268 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Patrick Mochel1890a972006-12-07 20:56:31 +08001270 ret = driver_register(&driver->drv);
1271 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Len Brown4be44fc2005-08-05 00:44:28 -04001274EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276/**
Hanjun Guob27b14c2013-09-22 15:42:41 +08001277 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001278 * @driver: driver to unregister
1279 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1281 * devices that match the driver's criteria and unbinds.
1282 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001283void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Patrick Mochel1890a972006-12-07 20:56:31 +08001285 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
Len Brown4be44fc2005-08-05 00:44:28 -04001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288EXPORT_SYMBOL(acpi_bus_unregister_driver);
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290/* --------------------------------------------------------------------------
1291 Device Enumeration
1292 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001293static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1294{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001295 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001296 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001297
1298 /*
1299 * Fixed hardware devices do not appear in the namespace and do not
1300 * have handles, but we fabricate acpi_devices for them, so we have
1301 * to deal with them specially.
1302 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001303 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001304 return acpi_root;
1305
1306 do {
1307 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001308 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001309 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1310 } while (acpi_bus_get_device(handle, &device));
1311 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001312}
1313
Len Brownc8f7a622006-07-09 17:22:28 -04001314acpi_status
1315acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1316{
1317 acpi_status status;
1318 acpi_handle tmp;
1319 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1320 union acpi_object *obj;
1321
1322 status = acpi_get_handle(handle, "_EJD", &tmp);
1323 if (ACPI_FAILURE(status))
1324 return status;
1325
1326 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1327 if (ACPI_SUCCESS(status)) {
1328 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001329 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1330 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001331 kfree(buffer.pointer);
1332 }
1333 return status;
1334}
1335EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1336
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001337static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1338 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001340 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1341 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001343 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001344 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001346 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001347 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001349 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001351 /* _PRW */
1352 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1353 if (ACPI_FAILURE(status)) {
1354 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001355 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001356 }
1357
1358 package = (union acpi_object *)buffer.pointer;
1359
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001360 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001361 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001364 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001365 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (element->type == ACPI_TYPE_PACKAGE) {
1368 if ((element->package.count < 2) ||
1369 (element->package.elements[0].type !=
1370 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001371 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001372 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001373
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001374 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001376 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 (u32) element->package.elements[1].integer.value;
1378 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001379 wakeup->gpe_device = NULL;
1380 wakeup->gpe_number = element->integer.value;
1381 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001382 goto out;
1383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001386 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001387 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001388
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001389 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001391 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1392 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001393 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001395 if (!list_empty(&wakeup->resources)) {
1396 int sleep_state;
1397
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001398 err = acpi_power_wakeup_list_init(&wakeup->resources,
1399 &sleep_state);
1400 if (err) {
1401 acpi_handle_warn(handle, "Retrieving current states "
1402 "of wakeup power resources failed\n");
1403 acpi_power_resources_list_free(&wakeup->resources);
1404 goto out;
1405 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001406 if (sleep_state < wakeup->sleep_state) {
1407 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1408 "(S%d) by S%d from power resources\n",
1409 (int)wakeup->sleep_state, sleep_state);
1410 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
Lin Mingbba63a22010-12-13 13:39:17 +08001413 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001414
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001415 out:
1416 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001417 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001420static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001422 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001423 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001424 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001425 {"PNP0C0E", 0},
1426 {"", 0},
1427 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001428 acpi_status status;
1429 acpi_event_status event_status;
1430
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001431 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001432
1433 /* Power button, Lid switch always enable wakeup */
1434 if (!acpi_match_device_ids(device, button_device_ids)) {
1435 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001436 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1437 /* Do not use Lid/sleep button for S5 wakeup */
1438 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1439 device->wakeup.sleep_state = ACPI_STATE_S4;
1440 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001441 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001442 return;
1443 }
1444
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +02001445 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1446 device->wakeup.gpe_number,
1447 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001448 if (status == AE_OK)
1449 device->wakeup.flags.run_wake =
1450 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1451}
1452
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001453static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001454{
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001455 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001456
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001457 /* Presence of _PRW indicates wake capable */
Jiang Liu952c63e2013-06-29 00:24:38 +08001458 if (!acpi_has_method(device->handle, "_PRW"))
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001459 return;
1460
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001461 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1462 &device->wakeup);
1463 if (err) {
1464 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001465 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001469 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001470 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001471 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1472 * system for the ACPI device with the _PRW object.
1473 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1474 * So it is necessary to call _DSW object first. Only when it is not
1475 * present will the _PSW object used.
1476 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001477 err = acpi_device_sleep_wake(device, 0, 0, 0);
1478 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001479 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1480 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001483static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001485 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001486 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1487 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001488 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001489
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001490 INIT_LIST_HEAD(&ps->resources);
1491
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001492 /* Evaluate "_PRx" to get referenced power resources */
1493 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1494 if (ACPI_SUCCESS(status)) {
1495 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001496
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001497 if (buffer.length && package
1498 && package->type == ACPI_TYPE_PACKAGE
1499 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001500 int err = acpi_extract_power_resources(package, 0,
1501 &ps->resources);
1502 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001503 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001504 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001505 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001506 }
1507
1508 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001509 pathname[2] = 'S';
Jiang Liu952c63e2013-06-29 00:24:38 +08001510 if (acpi_has_method(device->handle, pathname))
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001511 ps->flags.explicit_set = 1;
1512
1513 /*
1514 * State is valid if there are means to put the device into it.
1515 * D3hot is only valid if _PR3 present.
1516 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001517 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001518 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1519 ps->flags.valid = 1;
1520 ps->flags.os_accessible = 1;
1521 }
1522
1523 ps->power = -1; /* Unknown - driver assigned */
1524 ps->latency = -1; /* Unknown - driver assigned */
1525}
1526
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001527static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001529 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001531 /* Presence of _PS0|_PR0 indicates 'power manageable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001532 if (!acpi_has_method(device->handle, "_PS0") &&
1533 !acpi_has_method(device->handle, "_PR0"))
1534 return;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001535
1536 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001539 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 */
Jiang Liu952c63e2013-06-29 00:24:38 +08001541 if (acpi_has_method(device->handle, "_PSC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001542 device->power.flags.explicit_get = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001543 if (acpi_has_method(device->handle, "_IRC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001544 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001547 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001549 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1550 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001552 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Zhang Rui9e89dde2006-12-07 20:56:16 +08001554 /* Set defaults for D0 and D3 states (always valid) */
1555 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1556 device->power.states[ACPI_STATE_D0].power = 100;
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +02001557 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1558 device->power.states[ACPI_STATE_D3_COLD].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001560 /* Set D3cold's explicit_set flag if _PS3 exists. */
1561 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1562 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1563
Aaron Lu1399dfc2012-11-21 23:33:40 +01001564 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1565 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1566 device->power.flags.power_resources)
1567 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1568
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001569 if (acpi_bus_init_power(device)) {
1570 acpi_free_power_resources_lists(device);
1571 device->flags.power_manageable = 0;
1572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001575static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 /* Presence of _STA indicates 'dynamic_status' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001578 if (acpi_has_method(device->handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 device->flags.dynamic_status = 1;
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 /* Presence of _RMV indicates 'removable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001582 if (acpi_has_method(device->handle, "_RMV"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 device->flags.removable = 1;
1584
1585 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001586 if (acpi_has_method(device->handle, "_EJD") ||
1587 acpi_has_method(device->handle, "_EJ0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 device->flags.ejectable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001591static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Len Brown4be44fc2005-08-05 00:44:28 -04001593 char bus_id[5] = { '?', 0 };
1594 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1595 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 /*
1598 * Bus ID
1599 * ------
1600 * The device's Bus ID is simply the object name.
1601 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1602 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001603 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001605 return;
1606 }
1607
1608 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 case ACPI_BUS_TYPE_POWER_BUTTON:
1610 strcpy(device->pnp.bus_id, "PWRF");
1611 break;
1612 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1613 strcpy(device->pnp.bus_id, "SLPF");
1614 break;
1615 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001616 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 /* Clean up trailing underscores (if any) */
1618 for (i = 3; i > 1; i--) {
1619 if (bus_id[i] == '_')
1620 bus_id[i] = '\0';
1621 else
1622 break;
1623 }
1624 strcpy(device->pnp.bus_id, bus_id);
1625 break;
1626 }
1627}
1628
Zhang Rui54735262007-01-11 02:09:09 -05001629/*
Jiang Liuebf4df82013-06-29 00:24:41 +08001630 * acpi_ata_match - see if an acpi object is an ATA device
1631 *
1632 * If an acpi object has one of the ACPI ATA methods defined,
1633 * then we can safely call it an ATA device.
1634 */
1635bool acpi_ata_match(acpi_handle handle)
1636{
1637 return acpi_has_method(handle, "_GTF") ||
1638 acpi_has_method(handle, "_GTM") ||
1639 acpi_has_method(handle, "_STM") ||
1640 acpi_has_method(handle, "_SDD");
1641}
1642
1643/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001644 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001645 *
1646 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1647 * then we can safely call it an ejectable drive bay
1648 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001649bool acpi_bay_match(acpi_handle handle)
Toshi Kanid4e1a692013-03-04 21:30:41 +00001650{
Zhang Rui54735262007-01-11 02:09:09 -05001651 acpi_handle phandle;
1652
Jiang Liu952c63e2013-06-29 00:24:38 +08001653 if (!acpi_has_method(handle, "_EJ0"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001654 return false;
1655 if (acpi_ata_match(handle))
1656 return true;
1657 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1658 return false;
Zhang Rui54735262007-01-11 02:09:09 -05001659
Jiang Liuebf4df82013-06-29 00:24:41 +08001660 return acpi_ata_match(phandle);
Zhang Rui54735262007-01-11 02:09:09 -05001661}
1662
Frank Seidel3620f2f2007-12-07 13:20:34 +01001663/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001664 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001665 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001666bool acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001667{
Jiang Liuebf4df82013-06-29 00:24:41 +08001668 return acpi_has_method(handle, "_DCK");
Frank Seidel3620f2f2007-12-07 13:20:34 +01001669}
1670
Thomas Renninger620e1122010-10-01 10:54:00 +02001671const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001672{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001673 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001674
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001675 if (list_empty(&device->pnp.ids))
1676 return dummy_hid;
1677
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001678 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1679 return hid->id;
1680}
1681EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001682
Toshi Kanid4e1a692013-03-04 21:30:41 +00001683static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001684{
1685 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001686
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001687 id = kmalloc(sizeof(*id), GFP_KERNEL);
1688 if (!id)
1689 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001690
Thomas Meyer581de592011-08-06 11:32:56 +02001691 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001692 if (!id->id) {
1693 kfree(id);
1694 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001695 }
1696
Toshi Kanid4e1a692013-03-04 21:30:41 +00001697 list_add_tail(&id->list, &pnp->ids);
1698 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001699}
1700
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001701/*
1702 * Old IBM workstations have a DSDT bug wherein the SMBus object
1703 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1704 * prefix. Work around this.
1705 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001706static bool acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001707{
Jiang Liuebf4df82013-06-29 00:24:41 +08001708 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1709 struct acpi_buffer path = { sizeof(node_name), node_name };
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001710
1711 if (!dmi_name_in_vendors("IBM"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001712 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001713
1714 /* Look for SMBS object */
Jiang Liuebf4df82013-06-29 00:24:41 +08001715 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1716 strcmp("SMBS", path.pointer))
1717 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001718
1719 /* Does it have the necessary (but misnamed) methods? */
Jiang Liu952c63e2013-06-29 00:24:38 +08001720 if (acpi_has_method(handle, "SBI") &&
1721 acpi_has_method(handle, "SBR") &&
1722 acpi_has_method(handle, "SBW"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001723 return true;
1724
1725 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001726}
1727
Toshi Kanic0af4172013-03-04 21:30:42 +00001728static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1729 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Len Brown4be44fc2005-08-05 00:44:28 -04001731 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001732 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001733 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001734 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Toshi Kanic0af4172013-03-04 21:30:42 +00001736 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00001738 if (handle == ACPI_ROOT_OBJECT) {
1739 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001740 break;
1741 }
1742
Toshi Kanic0af4172013-03-04 21:30:42 +00001743 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001745 pr_err(PREFIX "%s: Error reading device info\n",
1746 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 return;
1748 }
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (info->valid & ACPI_VALID_HID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001751 acpi_add_id(pnp, info->hardware_id.string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001752 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001753 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001754 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00001755 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001758 pnp->bus_address = info->address;
1759 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
Lv Zhengccf78042012-10-30 14:41:07 +01001761 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001762 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01001763 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001764
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001765 kfree(info);
1766
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001767 /*
1768 * Some devices don't reliably have _HIDs & _CIDs, so add
1769 * synthetic HIDs to make sure drivers can find them.
1770 */
Toshi Kanic0af4172013-03-04 21:30:42 +00001771 if (acpi_is_video_device(handle))
1772 acpi_add_id(pnp, ACPI_VIDEO_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001773 else if (acpi_bay_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001774 acpi_add_id(pnp, ACPI_BAY_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001775 else if (acpi_dock_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001776 acpi_add_id(pnp, ACPI_DOCK_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001777 else if (acpi_ibm_smbus_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001778 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1779 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1780 acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1781 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1782 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001783 }
Zhang Rui54735262007-01-11 02:09:09 -05001784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 break;
1786 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00001787 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 break;
1789 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00001790 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00001793 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 break;
1795 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001796 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 break;
1798 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001799 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 break;
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
Toshi Kanic0af4172013-03-04 21:30:42 +00001804void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1805{
1806 struct acpi_hardware_id *id, *tmp;
1807
1808 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1809 kfree(id->id);
1810 kfree(id);
1811 }
1812 kfree(pnp->unique_id);
1813}
1814
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001815void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1816 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001818 INIT_LIST_HEAD(&device->pnp.ids);
1819 device->device_type = type;
1820 device->handle = handle;
1821 device->parent = acpi_bus_get_parent(handle);
Rafael J. Wysocki25db1152013-11-22 21:56:06 +01001822 acpi_set_device_status(device, sta);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001823 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00001824 acpi_set_pnp_ids(handle, &device->pnp, type);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001825 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001826 device->flags.match_driver = false;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001827 device->flags.initialized = true;
1828 device->flags.visited = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001829 device_initialize(&device->dev);
1830 dev_set_uevent_suppress(&device->dev, true);
1831}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001832
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001833void acpi_device_add_finalize(struct acpi_device *device)
1834{
1835 dev_set_uevent_suppress(&device->dev, false);
1836 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
1838
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001839static int acpi_add_single_object(struct acpi_device **child,
1840 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001841 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001843 int result;
1844 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001845 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Burman Yan36bcbec2006-12-19 12:56:11 -08001847 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001849 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001850 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001853 acpi_init_device_object(device, handle, type, sta);
1854 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001855 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001857 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001858 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001859 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001860 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
1862
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001863 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001864 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001865 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1866 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1867 dev_name(&device->dev), (char *) buffer.pointer,
1868 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1869 kfree(buffer.pointer);
1870 *child = device;
1871 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001872}
1873
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001874static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1875 unsigned long long *sta)
1876{
1877 acpi_status status;
1878 acpi_object_type acpi_type;
1879
1880 status = acpi_get_type(handle, &acpi_type);
1881 if (ACPI_FAILURE(status))
1882 return -ENODEV;
1883
1884 switch (acpi_type) {
1885 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1886 case ACPI_TYPE_DEVICE:
1887 *type = ACPI_BUS_TYPE_DEVICE;
1888 status = acpi_bus_get_status_handle(handle, sta);
1889 if (ACPI_FAILURE(status))
1890 return -ENODEV;
1891 break;
1892 case ACPI_TYPE_PROCESSOR:
1893 *type = ACPI_BUS_TYPE_PROCESSOR;
1894 status = acpi_bus_get_status_handle(handle, sta);
1895 if (ACPI_FAILURE(status))
1896 return -ENODEV;
1897 break;
1898 case ACPI_TYPE_THERMAL:
1899 *type = ACPI_BUS_TYPE_THERMAL;
1900 *sta = ACPI_STA_DEFAULT;
1901 break;
1902 case ACPI_TYPE_POWER:
1903 *type = ACPI_BUS_TYPE_POWER;
1904 *sta = ACPI_STA_DEFAULT;
1905 break;
1906 default:
1907 return -ENODEV;
1908 }
1909
1910 return 0;
1911}
1912
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001913bool acpi_device_is_present(struct acpi_device *adev)
1914{
1915 if (adev->status.present || adev->status.functional)
1916 return true;
1917
1918 adev->flags.initialized = false;
1919 return false;
1920}
1921
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001922static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1923 char *idstr,
1924 const struct acpi_device_id **matchid)
1925{
1926 const struct acpi_device_id *devid;
1927
1928 for (devid = handler->ids; devid->id[0]; devid++)
1929 if (!strcmp((char *)devid->id, idstr)) {
1930 if (matchid)
1931 *matchid = devid;
1932
1933 return true;
1934 }
1935
1936 return false;
1937}
1938
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001939static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1940 const struct acpi_device_id **matchid)
1941{
1942 struct acpi_scan_handler *handler;
1943
1944 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1945 if (acpi_scan_handler_matching(handler, idstr, matchid))
1946 return handler;
1947
1948 return NULL;
1949}
1950
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001951void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1952{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001953 if (!!hotplug->enabled == !!val)
1954 return;
1955
1956 mutex_lock(&acpi_scan_lock);
1957
1958 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001959
1960 mutex_unlock(&acpi_scan_lock);
1961}
1962
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01001963static void acpi_scan_init_hotplug(struct acpi_device *adev)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001964{
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001965 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001966
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01001967 list_for_each_entry(hwid, &adev->pnp.ids, list) {
1968 struct acpi_scan_handler *handler;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001969
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001970 handler = acpi_scan_match_handler(hwid->id, NULL);
Rafael J. Wysocki1a699472014-02-06 13:58:13 +01001971 if (handler) {
1972 adev->flags.hotplug_notify = true;
1973 break;
1974 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01001975 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001976}
1977
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001978static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1979 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001981 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001982 int type;
1983 unsigned long long sta;
1984 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001985
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001986 acpi_bus_get_device(handle, &device);
1987 if (device)
1988 goto out;
1989
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001990 result = acpi_bus_type_and_status(handle, &type, &sta);
1991 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001992 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001994 if (type == ACPI_BUS_TYPE_POWER) {
1995 acpi_add_power_resource(handle);
1996 return AE_OK;
1997 }
1998
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001999 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00002000 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002001 return AE_CTRL_DEPTH;
2002
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002003 acpi_scan_init_hotplug(device);
2004
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01002005 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002006 if (!*return_value)
2007 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01002008
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002009 return AE_OK;
2010}
2011
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002012static int acpi_scan_attach_handler(struct acpi_device *device)
2013{
2014 struct acpi_hardware_id *hwid;
2015 int ret = 0;
2016
2017 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002018 const struct acpi_device_id *devid;
2019 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002020
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002021 handler = acpi_scan_match_handler(hwid->id, &devid);
2022 if (handler) {
2023 ret = handler->attach(device, devid);
2024 if (ret > 0) {
2025 device->handler = handler;
2026 break;
2027 } else if (ret < 0) {
2028 break;
2029 }
2030 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002031 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002032 return ret;
2033}
2034
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002035static void acpi_bus_attach(struct acpi_device *device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002036{
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002037 struct acpi_device *child;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002038 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002039
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002040 acpi_bus_get_status(device);
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002041 /* Skip devices that are not present. */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002042 if (!acpi_device_is_present(device)) {
2043 device->flags.visited = false;
2044 return;
2045 }
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02002046 if (device->handler)
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002047 goto ok;
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02002048
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002049 if (!device->flags.initialized) {
2050 acpi_bus_update_power(device, NULL);
2051 device->flags.initialized = true;
2052 }
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002053 device->flags.visited = false;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002054 ret = acpi_scan_attach_handler(device);
Rafael J. Wysocki69310072013-11-07 01:41:01 +01002055 if (ret < 0)
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002056 return;
Rafael J. Wysocki69310072013-11-07 01:41:01 +01002057
2058 device->flags.match_driver = true;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002059 if (!ret) {
2060 ret = device_attach(&device->dev);
2061 if (ret < 0)
2062 return;
2063 }
2064 device->flags.visited = true;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002065
2066 ok:
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002067 list_for_each_entry(child, &device->children, node)
2068 acpi_bus_attach(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002071/**
2072 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2073 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01002074 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002075 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2076 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01002077 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002078 * If no devices were found, -ENODEV is returned, but it does not mean that
2079 * there has been a real error. There just have been no suitable ACPI objects
2080 * in the table trunk from which the kernel could create a device and add an
2081 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002082 *
2083 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01002084 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002085int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07002086{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 void *device = NULL;
Rajesh Shah3fb02732005-04-28 00:25:52 -07002088
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002089 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01002091 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002092
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002093 if (device) {
2094 acpi_bus_attach(device);
2095 return 0;
2096 }
2097 return -ENODEV;
Rajesh Shah3fb02732005-04-28 00:25:52 -07002098}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002099EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002101/**
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002102 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2103 * @adev: Root of the ACPI namespace scope to walk.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002104 *
2105 * Must be called under acpi_scan_lock.
2106 */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002107void acpi_bus_trim(struct acpi_device *adev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002109 struct acpi_scan_handler *handler = adev->handler;
2110 struct acpi_device *child;
2111
2112 list_for_each_entry_reverse(child, &adev->children, node)
2113 acpi_bus_trim(child);
2114
Rafael J. Wysockia951c772014-01-27 23:08:09 +01002115 adev->flags.match_driver = false;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002116 if (handler) {
2117 if (handler->detach)
2118 handler->detach(adev);
2119
2120 adev->handler = NULL;
2121 } else {
2122 device_release_driver(&adev->dev);
2123 }
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002124 /*
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002125 * Most likely, the device is going away, so put it into D3cold before
2126 * that.
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002127 */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002128 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2129 adev->flags.initialized = false;
2130 adev->flags.visited = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}
Kristen Accardiceaba662006-02-23 17:56:01 -08002132EXPORT_SYMBOL_GPL(acpi_bus_trim);
2133
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00002134static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Len Brown4be44fc2005-08-05 00:44:28 -04002136 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08002137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 /*
2139 * Enumerate all fixed-feature devices.
2140 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002141 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2142 struct acpi_device *device = NULL;
2143
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002144 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002145 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002146 ACPI_STA_DEFAULT);
2147 if (result)
2148 return result;
2149
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002150 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002151 result = device_attach(&device->dev);
2152 if (result < 0)
2153 return result;
2154
Daniel Drakec10d7a12012-05-10 00:08:43 +01002155 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002158 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2159 struct acpi_device *device = NULL;
2160
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002161 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002162 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002163 ACPI_STA_DEFAULT);
2164 if (result)
2165 return result;
2166
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002167 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002168 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002171 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
Bjorn Helgaase747f272009-03-24 16:49:43 -06002174int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
2176 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Patrick Mochel5b327262006-05-10 10:33:00 -04002178 result = bus_register(&acpi_bus_type);
2179 if (result) {
2180 /* We don't want to quit even if we failed to add suspend/resume */
2181 printk(KERN_ERR PREFIX "Could not register bus type\n");
2182 }
2183
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01002184 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01002185 acpi_pci_link_init();
Rafael J. Wysockiac212b62013-05-03 00:26:22 +02002186 acpi_processor_init();
Rafael J. Wysocki141a2972013-01-30 14:27:40 +01002187 acpi_platform_init();
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01002188 acpi_lpss_init();
Lan Tianyu2fa97fe2013-06-05 02:27:50 +00002189 acpi_cmos_rtc_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01002190 acpi_container_init();
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01002191 acpi_memory_hotplug_init();
Jiang Liu94add0f2013-06-23 00:59:55 +02002192 acpi_dock_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01002193
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002194 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 * Enumerate devices in the ACPI namespace.
2197 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002198 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002200 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002202 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002204 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002206 result = acpi_bus_scan_fixed();
2207 if (result) {
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002208 acpi_detach_data(acpi_root->handle, acpi_scan_drop_device);
2209 acpi_device_del(acpi_root);
2210 put_device(&acpi_root->dev);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002211 goto out;
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002212 }
2213
2214 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002215
2216 out:
2217 mutex_unlock(&acpi_scan_lock);
2218 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219}