]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/acpi/scan.c
Merge branches 'acpi-fan', 'acpi-video' and 'acpi-ec'
[mirror_ubuntu-focal-kernel.git] / drivers / acpi / scan.c
CommitLineData
1da177e4
LT
1/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
5a0e3ad6 7#include <linux/slab.h>
9b6d97b6 8#include <linux/kernel.h>
1da177e4 9#include <linux/acpi.h>
74523c90
AK
10#include <linux/signal.h>
11#include <linux/kthread.h>
222e82ac 12#include <linux/dmi.h>
d1efe3c3 13#include <linux/nls.h>
1da177e4 14
d783156e
RW
15#include <asm/pgtable.h>
16
e60cc7a6
BH
17#include "internal.h"
18
1da177e4 19#define _COMPONENT ACPI_BUS_COMPONENT
f52fd66d 20ACPI_MODULE_NAME("scan");
4be44fcd 21extern struct acpi_device *acpi_root;
1da177e4
LT
22
23#define ACPI_BUS_CLASS "system_bus"
29b71a1c 24#define ACPI_BUS_HID "LNXSYBUS"
1da177e4
LT
25#define ACPI_BUS_DEVICE_NAME "System Bus"
26
859ac9a4
BH
27#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
28
d783156e
RW
29#define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
30
683058e3
RW
31/*
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
620e112c 37static const char *dummy_hid = "device";
2b2ae7c7 38
40e7fcb1
LT
39static LIST_HEAD(acpi_dep_list);
40static DEFINE_MUTEX(acpi_dep_list_lock);
e49bd2dd 41static LIST_HEAD(acpi_bus_id_list);
c511cc19 42static DEFINE_MUTEX(acpi_scan_lock);
ca589f94 43static LIST_HEAD(acpi_scan_handlers_list);
9090589d 44DEFINE_MUTEX(acpi_device_lock);
1da177e4 45LIST_HEAD(acpi_wakeup_device_list);
e525506f 46static DEFINE_MUTEX(acpi_hp_context_lock);
1da177e4 47
40e7fcb1
LT
48struct acpi_dep_data {
49 struct list_head node;
50 acpi_handle master;
51 acpi_handle slave;
52};
53
e49bd2dd 54struct acpi_device_bus_id{
bb095854 55 char bus_id[15];
e49bd2dd
ZR
56 unsigned int instance_no;
57 struct list_head node;
1da177e4 58};
29b71a1c 59
3757b948
RW
60void acpi_scan_lock_acquire(void)
61{
62 mutex_lock(&acpi_scan_lock);
63}
64EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
65
66void acpi_scan_lock_release(void)
67{
68 mutex_unlock(&acpi_scan_lock);
69}
70EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
71
e525506f
RW
72void acpi_lock_hp_context(void)
73{
74 mutex_lock(&acpi_hp_context_lock);
75}
76
77void acpi_unlock_hp_context(void)
78{
79 mutex_unlock(&acpi_hp_context_lock);
80}
81
5d513205
RW
82void acpi_initialize_hp_context(struct acpi_device *adev,
83 struct acpi_hotplug_context *hp,
84 int (*notify)(struct acpi_device *, u32),
85 void (*uevent)(struct acpi_device *, u32))
86{
87 acpi_lock_hp_context();
ba574dc8
RW
88 hp->notify = notify;
89 hp->uevent = uevent;
90 acpi_set_hp_context(adev, hp);
5d513205
RW
91 acpi_unlock_hp_context();
92}
93EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
94
ca589f94
RW
95int acpi_scan_add_handler(struct acpi_scan_handler *handler)
96{
d34afa9d 97 if (!handler)
ca589f94
RW
98 return -EINVAL;
99
100 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
101 return 0;
102}
103
3f8055c3
RW
104int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
105 const char *hotplug_profile_name)
106{
107 int error;
108
109 error = acpi_scan_add_handler(handler);
110 if (error)
111 return error;
112
113 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
114 return 0;
115}
116
29b71a1c
TR
117/*
118 * Creates hid/cid(s) string needed for modalias and uevent
119 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
120 * char *modalias: "acpi:IBM0001:ACPI0001"
3d8e0090
ZR
121 * Return: 0: no _HID and no _CID
122 * -EINVAL: output error
123 * -ENOMEM: output is truncated
29b71a1c 124*/
b3e572d2
AB
125static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
126 int size)
127{
29b71a1c 128 int len;
5c9fcb5d 129 int count;
7f47fa6c 130 struct acpi_hardware_id *id;
29b71a1c 131
2b2ae7c7
TR
132 if (list_empty(&acpi_dev->pnp.ids))
133 return 0;
134
733e6251
MW
135 /*
136 * If the device has PRP0001 we expose DT compatible modalias
137 * instead in form of of:NnameTCcompatible.
138 */
139 if (acpi_dev->data.of_compatible) {
140 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
141 const union acpi_object *of_compatible, *obj;
142 int i, nval;
143 char *c;
144
145 acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf);
146 /* DT strings are all in lower case */
147 for (c = buf.pointer; *c != '\0'; c++)
148 *c = tolower(*c);
149
150 len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer);
151 ACPI_FREE(buf.pointer);
152
153 of_compatible = acpi_dev->data.of_compatible;
154 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
155 nval = of_compatible->package.count;
156 obj = of_compatible->package.elements;
157 } else { /* Must be ACPI_TYPE_STRING. */
158 nval = 1;
159 obj = of_compatible;
160 }
161 for (i = 0; i < nval; i++, obj++) {
162 count = snprintf(&modalias[len], size, "C%s",
163 obj->string.pointer);
164 if (count < 0)
165 return -EINVAL;
166 if (count >= size)
167 return -ENOMEM;
168
169 len += count;
170 size -= count;
171 }
172 } else {
173 len = snprintf(modalias, size, "acpi:");
174 size -= len;
175
176 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
177 count = snprintf(&modalias[len], size, "%s:", id->id);
178 if (count < 0)
179 return -EINVAL;
180 if (count >= size)
181 return -ENOMEM;
182 len += count;
183 size -= count;
184 }
5c9fcb5d
ZR
185 }
186
29b71a1c
TR
187 modalias[len] = '\0';
188 return len;
189}
190
52870786
MW
191/*
192 * acpi_companion_match() - Can we match via ACPI companion device
193 * @dev: Device in question
194 *
195 * Check if the given device has an ACPI companion and if that companion has
196 * a valid list of PNP IDs, and if the device is the first (primary) physical
197 * device associated with it.
198 *
199 * If multiple physical devices are attached to a single ACPI companion, we need
200 * to be careful. The usage scenario for this kind of relationship is that all
201 * of the physical devices in question use resources provided by the ACPI
202 * companion. A typical case is an MFD device where all the sub-devices share
203 * the parent's ACPI companion. In such cases we can only allow the primary
204 * (first) physical device to be matched with the help of the companion's PNP
205 * IDs.
206 *
207 * Additional physical devices sharing the ACPI companion can still use
208 * resources available from it but they will be matched normally using functions
209 * provided by their bus types (and analogously for their modalias).
210 */
211static bool acpi_companion_match(const struct device *dev)
212{
213 struct acpi_device *adev;
214 bool ret;
215
216 adev = ACPI_COMPANION(dev);
217 if (!adev)
218 return false;
219
220 if (list_empty(&adev->pnp.ids))
221 return false;
222
223 mutex_lock(&adev->physical_node_lock);
224 if (list_empty(&adev->physical_node_list)) {
225 ret = false;
226 } else {
227 const struct acpi_device_physical_node *node;
228
229 node = list_first_entry(&adev->physical_node_list,
230 struct acpi_device_physical_node, node);
231 ret = node->dev == dev;
232 }
233 mutex_unlock(&adev->physical_node_lock);
234
235 return ret;
236}
237
6eb2451f
ZR
238/*
239 * Creates uevent modalias field for ACPI enumerated devices.
240 * Because the other buses does not support ACPI HIDs & CIDs.
241 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
242 * "acpi:IBM0001:ACPI0001"
243 */
244int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
245{
6eb2451f
ZR
246 int len;
247
52870786 248 if (!acpi_companion_match(dev))
6eb2451f
ZR
249 return -ENODEV;
250
251 if (add_uevent_var(env, "MODALIAS="))
252 return -ENOMEM;
52870786 253 len = create_modalias(ACPI_COMPANION(dev), &env->buf[env->buflen - 1],
6eb2451f
ZR
254 sizeof(env->buf) - env->buflen);
255 if (len <= 0)
256 return len;
257 env->buflen += len;
258 return 0;
259}
260EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
261
262/*
263 * Creates modalias sysfs attribute for ACPI enumerated devices.
264 * Because the other buses does not support ACPI HIDs & CIDs.
265 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
266 * "acpi:IBM0001:ACPI0001"
267 */
268int acpi_device_modalias(struct device *dev, char *buf, int size)
269{
6eb2451f
ZR
270 int len;
271
52870786 272 if (!acpi_companion_match(dev))
6eb2451f
ZR
273 return -ENODEV;
274
52870786 275 len = create_modalias(ACPI_COMPANION(dev), buf, size -1);
6eb2451f
ZR
276 if (len <= 0)
277 return len;
278 buf[len++] = '\n';
279 return len;
280}
281EXPORT_SYMBOL_GPL(acpi_device_modalias);
282
29b71a1c
TR
283static ssize_t
284acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
285 struct acpi_device *acpi_dev = to_acpi_device(dev);
286 int len;
287
29b71a1c
TR
288 len = create_modalias(acpi_dev, buf, 1024);
289 if (len <= 0)
3d8e0090 290 return len;
29b71a1c
TR
291 buf[len++] = '\n';
292 return len;
293}
294static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
295
caa73ea1 296bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
d22ddcbc
RW
297{
298 struct acpi_device_physical_node *pn;
299 bool offline = true;
300
301 mutex_lock(&adev->physical_node_lock);
302
303 list_for_each_entry(pn, &adev->physical_node_list, node)
304 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
caa73ea1
RW
305 if (uevent)
306 kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
307
d22ddcbc
RW
308 offline = false;
309 break;
310 }
311
312 mutex_unlock(&adev->physical_node_lock);
313 return offline;
314}
315
7f28ddec
RW
316static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
317 void **ret_p)
683058e3
RW
318{
319 struct acpi_device *device = NULL;
320 struct acpi_device_physical_node *pn;
303bfdb1 321 bool second_pass = (bool)data;
683058e3
RW
322 acpi_status status = AE_OK;
323
324 if (acpi_bus_get_device(handle, &device))
325 return AE_OK;
326
7f28ddec
RW
327 if (device->handler && !device->handler->hotplug.enabled) {
328 *ret_p = &device->dev;
329 return AE_SUPPORT;
330 }
331
683058e3
RW
332 mutex_lock(&device->physical_node_lock);
333
334 list_for_each_entry(pn, &device->physical_node_list, node) {
335 int ret;
336
303bfdb1
RW
337 if (second_pass) {
338 /* Skip devices offlined by the first pass. */
339 if (pn->put_online)
340 continue;
341 } else {
342 pn->put_online = false;
343 }
683058e3
RW
344 ret = device_offline(pn->dev);
345 if (acpi_force_hot_remove)
346 continue;
347
303bfdb1
RW
348 if (ret >= 0) {
349 pn->put_online = !ret;
350 } else {
351 *ret_p = pn->dev;
352 if (second_pass) {
353 status = AE_ERROR;
354 break;
355 }
683058e3 356 }
683058e3
RW
357 }
358
359 mutex_unlock(&device->physical_node_lock);
360
361 return status;
362}
363
7f28ddec
RW
364static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
365 void **ret_p)
683058e3
RW
366{
367 struct acpi_device *device = NULL;
368 struct acpi_device_physical_node *pn;
369
370 if (acpi_bus_get_device(handle, &device))
371 return AE_OK;
372
373 mutex_lock(&device->physical_node_lock);
374
375 list_for_each_entry(pn, &device->physical_node_list, node)
376 if (pn->put_online) {
377 device_online(pn->dev);
378 pn->put_online = false;
379 }
380
381 mutex_unlock(&device->physical_node_lock);
382
383 return AE_OK;
384}
385
d22ddcbc 386static int acpi_scan_try_to_offline(struct acpi_device *device)
1da177e4 387{
5993c467 388 acpi_handle handle = device->handle;
d22ddcbc 389 struct device *errdev = NULL;
a33ec399 390 acpi_status status;
f058cdf4 391
303bfdb1
RW
392 /*
393 * Carry out two passes here and ignore errors in the first pass,
394 * because if the devices in question are memory blocks and
395 * CONFIG_MEMCG is set, one of the blocks may hold data structures
396 * that the other blocks depend on, but it is not known in advance which
397 * block holds them.
398 *
399 * If the first pass is successful, the second one isn't needed, though.
400 */
7f28ddec
RW
401 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
402 NULL, acpi_bus_offline, (void *)false,
403 (void **)&errdev);
404 if (status == AE_SUPPORT) {
405 dev_warn(errdev, "Offline disabled.\n");
406 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
407 acpi_bus_online, NULL, NULL, NULL);
7f28ddec
RW
408 return -EPERM;
409 }
410 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
303bfdb1
RW
411 if (errdev) {
412 errdev = NULL;
683058e3 413 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
7f28ddec
RW
414 NULL, acpi_bus_offline, (void *)true,
415 (void **)&errdev);
303bfdb1 416 if (!errdev || acpi_force_hot_remove)
7f28ddec
RW
417 acpi_bus_offline(handle, 0, (void *)true,
418 (void **)&errdev);
303bfdb1
RW
419
420 if (errdev && !acpi_force_hot_remove) {
421 dev_warn(errdev, "Offline failed.\n");
7f28ddec 422 acpi_bus_online(handle, 0, NULL, NULL);
303bfdb1 423 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
7f28ddec
RW
424 ACPI_UINT32_MAX, acpi_bus_online,
425 NULL, NULL, NULL);
303bfdb1
RW
426 return -EBUSY;
427 }
683058e3 428 }
d22ddcbc
RW
429 return 0;
430}
431
432static int acpi_scan_hot_remove(struct acpi_device *device)
433{
434 acpi_handle handle = device->handle;
435 unsigned long long sta;
436 acpi_status status;
437
dee15926
TC
438 if (device->handler && device->handler->hotplug.demand_offline
439 && !acpi_force_hot_remove) {
caa73ea1 440 if (!acpi_scan_is_offline(device, true))
d22ddcbc
RW
441 return -EBUSY;
442 } else {
443 int error = acpi_scan_try_to_offline(device);
444 if (error)
445 return error;
446 }
683058e3 447
26d46867 448 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
0794469d 449 "Hot-removing device %s...\n", dev_name(&device->dev)));
26d46867 450
bfee26db 451 acpi_bus_trim(device);
683058e3 452
7d2421f8 453 acpi_evaluate_lck(handle, 0);
1da177e4
LT
454 /*
455 * TBD: _EJD support.
456 */
7d2421f8
JL
457 status = acpi_evaluate_ej0(handle);
458 if (status == AE_NOT_FOUND)
459 return -ENODEV;
460 else if (ACPI_FAILURE(status))
461 return -EIO;
1da177e4 462
882fd12e
TK
463 /*
464 * Verify if eject was indeed successful. If not, log an error
465 * message. No need to call _OST since _EJ0 call was made OK.
466 */
467 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
468 if (ACPI_FAILURE(status)) {
469 acpi_handle_warn(handle,
470 "Status check after eject failed (0x%x)\n", status);
471 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
472 acpi_handle_warn(handle,
473 "Eject incomplete - status 0x%llx\n", sta);
474 }
475
a33ec399
RW
476 return 0;
477}
1da177e4 478
443fc820
RW
479static int acpi_scan_device_not_present(struct acpi_device *adev)
480{
481 if (!acpi_device_enumerated(adev)) {
482 dev_warn(&adev->dev, "Still not present\n");
483 return -EALREADY;
484 }
485 acpi_bus_trim(adev);
486 return 0;
487}
488
c27b2c33 489static int acpi_scan_device_check(struct acpi_device *adev)
a33ec399 490{
f943db40 491 int error;
a33ec399 492
443fc820
RW
493 acpi_bus_get_status(adev);
494 if (adev->status.present || adev->status.functional) {
495 /*
496 * This function is only called for device objects for which
497 * matching scan handlers exist. The only situation in which
498 * the scan handler is not attached to this device object yet
499 * is when the device has just appeared (either it wasn't
500 * present at all before or it was removed and then added
501 * again).
502 */
503 if (adev->handler) {
504 dev_warn(&adev->dev, "Already enumerated\n");
505 return -EALREADY;
506 }
507 error = acpi_bus_scan(adev->handle);
508 if (error) {
509 dev_warn(&adev->dev, "Namespace scan failure\n");
510 return error;
511 }
512 if (!adev->handler) {
513 dev_warn(&adev->dev, "Enumeration failure\n");
514 error = -ENODEV;
515 }
516 } else {
517 error = acpi_scan_device_not_present(adev);
518 }
519 return error;
520}
521
522static int acpi_scan_bus_check(struct acpi_device *adev)
523{
524 struct acpi_scan_handler *handler = adev->handler;
525 struct acpi_device *child;
526 int error;
527
528 acpi_bus_get_status(adev);
529 if (!(adev->status.present || adev->status.functional)) {
530 acpi_scan_device_not_present(adev);
531 return 0;
532 }
533 if (handler && handler->hotplug.scan_dependent)
534 return handler->hotplug.scan_dependent(adev);
535
c27b2c33
RW
536 error = acpi_bus_scan(adev->handle);
537 if (error) {
538 dev_warn(&adev->dev, "Namespace scan failure\n");
539 return error;
540 }
443fc820
RW
541 list_for_each_entry(child, &adev->children, node) {
542 error = acpi_scan_bus_check(child);
543 if (error)
544 return error;
c27b2c33
RW
545 }
546 return 0;
a33ec399
RW
547}
548
3c2cc7ff
RW
549static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
550{
551 switch (type) {
552 case ACPI_NOTIFY_BUS_CHECK:
553 return acpi_scan_bus_check(adev);
554 case ACPI_NOTIFY_DEVICE_CHECK:
555 return acpi_scan_device_check(adev);
556 case ACPI_NOTIFY_EJECT_REQUEST:
557 case ACPI_OST_EC_OSPM_EJECT:
dd2151be
RW
558 if (adev->handler && !adev->handler->hotplug.enabled) {
559 dev_info(&adev->dev, "Eject disabled\n");
560 return -EPERM;
561 }
700b8422
RW
562 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
563 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
3c2cc7ff
RW
564 return acpi_scan_hot_remove(adev);
565 }
566 return -EINVAL;
567}
568
1e3bcb59 569void acpi_device_hotplug(struct acpi_device *adev, u32 src)
a33ec399 570{
a33ec399 571 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
3c2cc7ff 572 int error = -ENODEV;
a33ec399 573
683058e3 574 lock_device_hotplug();
e0ae8fee 575 mutex_lock(&acpi_scan_lock);
a33ec399 576
c27b2c33
RW
577 /*
578 * The device object's ACPI handle cannot become invalid as long as we
3c2cc7ff 579 * are holding acpi_scan_lock, but it might have become invalid before
c27b2c33
RW
580 * that lock was acquired.
581 */
582 if (adev->handle == INVALID_ACPI_HANDLE)
3c2cc7ff 583 goto err_out;
c27b2c33 584
1e2380cd
RW
585 if (adev->flags.is_dock_station) {
586 error = dock_notify(adev, src);
587 } else if (adev->flags.hotplug_notify) {
3c2cc7ff 588 error = acpi_generic_hotplug_event(adev, src);
dd2151be
RW
589 if (error == -EPERM) {
590 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
591 goto err_out;
592 }
3c2cc7ff 593 } else {
be27b3dc 594 int (*notify)(struct acpi_device *, u32);
3c2cc7ff
RW
595
596 acpi_lock_hp_context();
be27b3dc 597 notify = adev->hp ? adev->hp->notify : NULL;
3c2cc7ff
RW
598 acpi_unlock_hp_context();
599 /*
600 * There may be additional notify handlers for device objects
601 * without the .event() callback, so ignore them here.
602 */
be27b3dc
RW
603 if (notify)
604 error = notify(adev, src);
3c2cc7ff
RW
605 else
606 goto out;
a33ec399 607 }
c27b2c33
RW
608 if (!error)
609 ost_code = ACPI_OST_SC_SUCCESS;
a33ec399 610
3c2cc7ff 611 err_out:
700b8422 612 acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
3c2cc7ff
RW
613
614 out:
78ea4639 615 acpi_bus_put_acpi_device(adev);
a33ec399 616 mutex_unlock(&acpi_scan_lock);
e0ae8fee 617 unlock_device_hotplug();
a33ec399
RW
618}
619
836aedb1
RW
620static ssize_t real_power_state_show(struct device *dev,
621 struct device_attribute *attr, char *buf)
622{
623 struct acpi_device *adev = to_acpi_device(dev);
624 int state;
625 int ret;
626
627 ret = acpi_device_get_power(adev, &state);
628 if (ret)
629 return ret;
630
631 return sprintf(buf, "%s\n", acpi_power_state_string(state));
632}
633
634static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
635
636static ssize_t power_state_show(struct device *dev,
637 struct device_attribute *attr, char *buf)
638{
639 struct acpi_device *adev = to_acpi_device(dev);
640
641 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
642}
643
644static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
645
1da177e4 646static ssize_t
f883d9db
PM
647acpi_eject_store(struct device *d, struct device_attribute *attr,
648 const char *buf, size_t count)
1da177e4 649{
f883d9db 650 struct acpi_device *acpi_device = to_acpi_device(d);
a33ec399
RW
651 acpi_object_type not_used;
652 acpi_status status;
1da177e4 653
a33ec399 654 if (!count || buf[0] != '1')
1da177e4 655 return -EINVAL;
1da177e4 656
a33ec399
RW
657 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
658 && !acpi_device->driver)
659 return -ENODEV;
660
661 status = acpi_get_type(acpi_device->handle, &not_used);
662 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
663 return -ENODEV;
664
a33ec399 665 get_device(&acpi_device->dev);
1e3bcb59 666 status = acpi_hotplug_schedule(acpi_device, ACPI_OST_EC_OSPM_EJECT);
f943db40
RW
667 if (ACPI_SUCCESS(status))
668 return count;
a33ec399 669
f943db40 670 put_device(&acpi_device->dev);
700b8422
RW
671 acpi_evaluate_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
672 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
5add99cf 673 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
1da177e4
LT
674}
675
f883d9db 676static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
1da177e4 677
e49bd2dd
ZR
678static ssize_t
679acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
680 struct acpi_device *acpi_dev = to_acpi_device(dev);
1da177e4 681
ea8d82fd 682 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
1da177e4 683}
e49bd2dd 684static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
1da177e4 685
923d4a45
LZ
686static ssize_t acpi_device_uid_show(struct device *dev,
687 struct device_attribute *attr, char *buf)
688{
689 struct acpi_device *acpi_dev = to_acpi_device(dev);
690
691 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
692}
693static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
694
695static ssize_t acpi_device_adr_show(struct device *dev,
696 struct device_attribute *attr, char *buf)
697{
698 struct acpi_device *acpi_dev = to_acpi_device(dev);
699
700 return sprintf(buf, "0x%08x\n",
701 (unsigned int)(acpi_dev->pnp.bus_address));
702}
703static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
704
e49bd2dd
ZR
705static ssize_t
706acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
707 struct acpi_device *acpi_dev = to_acpi_device(dev);
708 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
709 int result;
1da177e4 710
e49bd2dd 711 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
0c526d96 712 if (result)
e49bd2dd 713 goto end;
1da177e4 714
e49bd2dd
ZR
715 result = sprintf(buf, "%s\n", (char*)path.pointer);
716 kfree(path.pointer);
0c526d96 717end:
e49bd2dd 718 return result;
1da177e4 719}
e49bd2dd 720static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
1da177e4 721
d1efe3c3
LO
722/* sysfs file that shows description text from the ACPI _STR method */
723static ssize_t description_show(struct device *dev,
724 struct device_attribute *attr,
725 char *buf) {
726 struct acpi_device *acpi_dev = to_acpi_device(dev);
727 int result;
728
729 if (acpi_dev->pnp.str_obj == NULL)
730 return 0;
731
732 /*
733 * The _STR object contains a Unicode identifier for a device.
734 * We need to convert to utf-8 so it can be displayed.
735 */
736 result = utf16s_to_utf8s(
737 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
738 acpi_dev->pnp.str_obj->buffer.length,
739 UTF16_LITTLE_ENDIAN, buf,
740 PAGE_SIZE);
741
742 buf[result++] = '\n';
743
744 return result;
745}
746static DEVICE_ATTR(description, 0444, description_show, NULL);
747
bb74ac23
YI
748static ssize_t
749acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
750 char *buf) {
751 struct acpi_device *acpi_dev = to_acpi_device(dev);
a383b68d
YI
752 acpi_status status;
753 unsigned long long sun;
bb74ac23 754
a383b68d
YI
755 status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun);
756 if (ACPI_FAILURE(status))
757 return -ENODEV;
758
759 return sprintf(buf, "%llu\n", sun);
bb74ac23
YI
760}
761static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
762
c713cd7f
SP
763static ssize_t status_show(struct device *dev, struct device_attribute *attr,
764 char *buf) {
765 struct acpi_device *acpi_dev = to_acpi_device(dev);
766 acpi_status status;
767 unsigned long long sta;
768
769 status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
770 if (ACPI_FAILURE(status))
771 return -ENODEV;
772
773 return sprintf(buf, "%llu\n", sta);
774}
775static DEVICE_ATTR_RO(status);
776
e49bd2dd 777static int acpi_device_setup_files(struct acpi_device *dev)
1da177e4 778{
d1efe3c3 779 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
f883d9db 780 acpi_status status;
e49bd2dd 781 int result = 0;
1da177e4
LT
782
783 /*
e49bd2dd 784 * Devices gotten from FADT don't have a "path" attribute
1da177e4 785 */
0c526d96 786 if (dev->handle) {
e49bd2dd 787 result = device_create_file(&dev->dev, &dev_attr_path);
0c526d96 788 if (result)
e49bd2dd 789 goto end;
1da177e4
LT
790 }
791
2b2ae7c7
TR
792 if (!list_empty(&dev->pnp.ids)) {
793 result = device_create_file(&dev->dev, &dev_attr_hid);
794 if (result)
795 goto end;
1da177e4 796
2b2ae7c7
TR
797 result = device_create_file(&dev->dev, &dev_attr_modalias);
798 if (result)
799 goto end;
800 }
29b71a1c 801
d1efe3c3
LO
802 /*
803 * If device has _STR, 'description' file is created
804 */
952c63e9 805 if (acpi_has_method(dev->handle, "_STR")) {
d1efe3c3
LO
806 status = acpi_evaluate_object(dev->handle, "_STR",
807 NULL, &buffer);
808 if (ACPI_FAILURE(status))
809 buffer.pointer = NULL;
810 dev->pnp.str_obj = buffer.pointer;
811 result = device_create_file(&dev->dev, &dev_attr_description);
812 if (result)
813 goto end;
814 }
815
d4e1a692 816 if (dev->pnp.type.bus_address)
923d4a45
LZ
817 result = device_create_file(&dev->dev, &dev_attr_adr);
818 if (dev->pnp.unique_id)
819 result = device_create_file(&dev->dev, &dev_attr_uid);
820
a383b68d 821 if (acpi_has_method(dev->handle, "_SUN")) {
bb74ac23
YI
822 result = device_create_file(&dev->dev, &dev_attr_sun);
823 if (result)
824 goto end;
bb74ac23
YI
825 }
826
c713cd7f
SP
827 if (acpi_has_method(dev->handle, "_STA")) {
828 result = device_create_file(&dev->dev, &dev_attr_status);
829 if (result)
830 goto end;
831 }
832
e49bd2dd
ZR
833 /*
834 * If device has _EJ0, 'eject' file is created that is used to trigger
835 * hot-removal function from userland.
836 */
952c63e9 837 if (acpi_has_method(dev->handle, "_EJ0")) {
e49bd2dd 838 result = device_create_file(&dev->dev, &dev_attr_eject);
836aedb1
RW
839 if (result)
840 return result;
841 }
842
843 if (dev->flags.power_manageable) {
844 result = device_create_file(&dev->dev, &dev_attr_power_state);
845 if (result)
846 return result;
847
848 if (dev->power.flags.power_resources)
849 result = device_create_file(&dev->dev,
850 &dev_attr_real_power_state);
851 }
852
0c526d96 853end:
e49bd2dd 854 return result;
1da177e4
LT
855}
856
f883d9db 857static void acpi_device_remove_files(struct acpi_device *dev)
1da177e4 858{
836aedb1
RW
859 if (dev->flags.power_manageable) {
860 device_remove_file(&dev->dev, &dev_attr_power_state);
861 if (dev->power.flags.power_resources)
862 device_remove_file(&dev->dev,
863 &dev_attr_real_power_state);
864 }
865
f883d9db 866 /*
d1efe3c3
LO
867 * If device has _STR, remove 'description' file
868 */
952c63e9 869 if (acpi_has_method(dev->handle, "_STR")) {
d1efe3c3
LO
870 kfree(dev->pnp.str_obj);
871 device_remove_file(&dev->dev, &dev_attr_description);
872 }
873 /*
874 * If device has _EJ0, remove 'eject' file.
f883d9db 875 */
952c63e9 876 if (acpi_has_method(dev->handle, "_EJ0"))
f883d9db 877 device_remove_file(&dev->dev, &dev_attr_eject);
1da177e4 878
952c63e9 879 if (acpi_has_method(dev->handle, "_SUN"))
bb74ac23
YI
880 device_remove_file(&dev->dev, &dev_attr_sun);
881
923d4a45
LZ
882 if (dev->pnp.unique_id)
883 device_remove_file(&dev->dev, &dev_attr_uid);
d4e1a692 884 if (dev->pnp.type.bus_address)
923d4a45 885 device_remove_file(&dev->dev, &dev_attr_adr);
1131b938
BH
886 device_remove_file(&dev->dev, &dev_attr_modalias);
887 device_remove_file(&dev->dev, &dev_attr_hid);
c713cd7f
SP
888 if (acpi_has_method(dev->handle, "_STA"))
889 device_remove_file(&dev->dev, &dev_attr_status);
0c526d96 890 if (dev->handle)
e49bd2dd 891 device_remove_file(&dev->dev, &dev_attr_path);
1da177e4 892}
1da177e4 893/* --------------------------------------------------------------------------
9e89dde2 894 ACPI Bus operations
1da177e4 895 -------------------------------------------------------------------------- */
29b71a1c 896
cf761af9
MW
897static const struct acpi_device_id *__acpi_match_device(
898 struct acpi_device *device, const struct acpi_device_id *ids)
29b71a1c
TR
899{
900 const struct acpi_device_id *id;
7f47fa6c 901 struct acpi_hardware_id *hwid;
29b71a1c 902
39a0ad87
ZY
903 /*
904 * If the device is not present, it is unnecessary to load device
905 * driver for it.
906 */
907 if (!device->status.present)
cf761af9 908 return NULL;
39a0ad87 909
7f47fa6c
BH
910 for (id = ids; id->id[0]; id++)
911 list_for_each_entry(hwid, &device->pnp.ids, list)
912 if (!strcmp((char *) id->id, hwid->id))
cf761af9
MW
913 return id;
914
915 return NULL;
916}
917
918/**
919 * acpi_match_device - Match a struct device against a given list of ACPI IDs
920 * @ids: Array of struct acpi_device_id object to match against.
921 * @dev: The device structure to match.
922 *
923 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
924 * object for that handle and use that object to match against a given list of
925 * device IDs.
926 *
927 * Return a pointer to the first matching ID on success or %NULL on failure.
928 */
929const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
930 const struct device *dev)
931{
932 struct acpi_device *adev;
0613e1f7 933 acpi_handle handle = ACPI_HANDLE(dev);
cf761af9 934
0613e1f7 935 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
cf761af9
MW
936 return NULL;
937
52870786
MW
938 if (!acpi_companion_match(dev))
939 return NULL;
940
cf761af9
MW
941 return __acpi_match_device(adev, ids);
942}
943EXPORT_SYMBOL_GPL(acpi_match_device);
29b71a1c 944
cf761af9
MW
945int acpi_match_device_ids(struct acpi_device *device,
946 const struct acpi_device_id *ids)
947{
948 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
29b71a1c
TR
949}
950EXPORT_SYMBOL(acpi_match_device_ids);
951
733e6251
MW
952/* Performs match against special "PRP0001" shoehorn ACPI ID */
953static bool acpi_of_driver_match_device(struct device *dev,
954 const struct device_driver *drv)
955{
956 const union acpi_object *of_compatible, *obj;
957 struct acpi_device *adev;
958 int i, nval;
959
960 adev = ACPI_COMPANION(dev);
961 if (!adev)
962 return false;
963
964 of_compatible = adev->data.of_compatible;
965 if (!drv->of_match_table || !of_compatible)
966 return false;
967
968 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
969 nval = of_compatible->package.count;
970 obj = of_compatible->package.elements;
971 } else { /* Must be ACPI_TYPE_STRING. */
972 nval = 1;
973 obj = of_compatible;
974 }
975 /* Now we can look for the driver DT compatible strings */
976 for (i = 0; i < nval; i++, obj++) {
977 const struct of_device_id *id;
978
979 for (id = drv->of_match_table; id->compatible[0]; id++)
980 if (!strcasecmp(obj->string.pointer, id->compatible))
981 return true;
982 }
983
984 return false;
985}
986
987bool acpi_driver_match_device(struct device *dev,
988 const struct device_driver *drv)
989{
990 if (!drv->acpi_match_table)
991 return acpi_of_driver_match_device(dev, drv);
992
993 return !!acpi_match_device(drv->acpi_match_table, dev);
994}
995EXPORT_SYMBOL_GPL(acpi_driver_match_device);
996
0b224527
RW
997static void acpi_free_power_resources_lists(struct acpi_device *device)
998{
999 int i;
1000
993cbe59
RW
1001 if (device->wakeup.flags.valid)
1002 acpi_power_resources_list_free(&device->wakeup.resources);
1003
0b224527
RW
1004 if (!device->flags.power_manageable)
1005 return;
1006
1007 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
1008 struct acpi_device_power_state *ps = &device->power.states[i];
1009 acpi_power_resources_list_free(&ps->resources);
1010 }
7f47fa6c
BH
1011}
1012
1890a97a 1013static void acpi_device_release(struct device *dev)
1da177e4 1014{
1890a97a 1015 struct acpi_device *acpi_dev = to_acpi_device(dev);
1da177e4 1016
ffdcd955 1017 acpi_free_properties(acpi_dev);
c0af4175 1018 acpi_free_pnp_ids(&acpi_dev->pnp);
0b224527 1019 acpi_free_power_resources_lists(acpi_dev);
1890a97a 1020 kfree(acpi_dev);
1da177e4
LT
1021}
1022
5d9464a4 1023static int acpi_bus_match(struct device *dev, struct device_driver *drv)
9e89dde2 1024{
5d9464a4
PM
1025 struct acpi_device *acpi_dev = to_acpi_device(dev);
1026 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
1da177e4 1027
209d3b17 1028 return acpi_dev->flags.match_driver
805d410f 1029 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
5d9464a4 1030}
1da177e4 1031
7eff2e7a 1032static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
1da177e4 1033{
5d9464a4 1034 struct acpi_device *acpi_dev = to_acpi_device(dev);
7eff2e7a 1035 int len;
5d9464a4 1036
2b2ae7c7
TR
1037 if (list_empty(&acpi_dev->pnp.ids))
1038 return 0;
1039
7eff2e7a
KS
1040 if (add_uevent_var(env, "MODALIAS="))
1041 return -ENOMEM;
1042 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
1043 sizeof(env->buf) - env->buflen);
3d8e0090
ZR
1044 if (len <= 0)
1045 return len;
7eff2e7a 1046 env->buflen += len;
9e89dde2 1047 return 0;
1da177e4
LT
1048}
1049
46ec8598
BH
1050static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
1051{
1052 struct acpi_device *device = data;
1053
1054 device->driver->ops.notify(device, event);
1055}
1056
236105db 1057static void acpi_device_notify_fixed(void *data)
46ec8598
BH
1058{
1059 struct acpi_device *device = data;
1060
53de5356
BH
1061 /* Fixed hardware devices have no handles */
1062 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
236105db
LT
1063}
1064
1065static acpi_status acpi_device_fixed_event(void *data)
1066{
1067 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_device_notify_fixed, data);
46ec8598
BH
1068 return AE_OK;
1069}
1070
1071static int acpi_device_install_notify_handler(struct acpi_device *device)
1072{
1073 acpi_status status;
46ec8598 1074
ccba2a36 1075 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
46ec8598
BH
1076 status =
1077 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
236105db 1078 acpi_device_fixed_event,
46ec8598 1079 device);
ccba2a36 1080 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
46ec8598
BH
1081 status =
1082 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
236105db 1083 acpi_device_fixed_event,
46ec8598
BH
1084 device);
1085 else
1086 status = acpi_install_notify_handler(device->handle,
1087 ACPI_DEVICE_NOTIFY,
1088 acpi_device_notify,
1089 device);
1090
1091 if (ACPI_FAILURE(status))
1092 return -EINVAL;
1093 return 0;
1094}
1095
1096static void acpi_device_remove_notify_handler(struct acpi_device *device)
1097{
ccba2a36 1098 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
46ec8598 1099 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
236105db 1100 acpi_device_fixed_event);
ccba2a36 1101 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
46ec8598 1102 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
236105db 1103 acpi_device_fixed_event);
46ec8598
BH
1104 else
1105 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
1106 acpi_device_notify);
1107}
1108
d9e455f5 1109static int acpi_device_probe(struct device *dev)
1da177e4 1110{
5d9464a4
PM
1111 struct acpi_device *acpi_dev = to_acpi_device(dev);
1112 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
1113 int ret;
1114
fc2e0a83 1115 if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev))
24071f47
RW
1116 return -EINVAL;
1117
d9e455f5
RW
1118 if (!acpi_drv->ops.add)
1119 return -ENOSYS;
46ec8598 1120
d9e455f5
RW
1121 ret = acpi_drv->ops.add(acpi_dev);
1122 if (ret)
1123 return ret;
46ec8598 1124
d9e455f5
RW
1125 acpi_dev->driver = acpi_drv;
1126 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1127 "Driver [%s] successfully bound to device [%s]\n",
1128 acpi_drv->name, acpi_dev->pnp.bus_id));
1129
1130 if (acpi_drv->ops.notify) {
1131 ret = acpi_device_install_notify_handler(acpi_dev);
1132 if (ret) {
1133 if (acpi_drv->ops.remove)
1134 acpi_drv->ops.remove(acpi_dev);
1135
1136 acpi_dev->driver = NULL;
1137 acpi_dev->driver_data = NULL;
1138 return ret;
1139 }
5d9464a4 1140 }
d9e455f5
RW
1141
1142 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
1143 acpi_drv->name, acpi_dev->pnp.bus_id));
1144 get_device(dev);
1145 return 0;
5d9464a4 1146}
1da177e4 1147
5d9464a4
PM
1148static int acpi_device_remove(struct device * dev)
1149{
1150 struct acpi_device *acpi_dev = to_acpi_device(dev);
1151 struct acpi_driver *acpi_drv = acpi_dev->driver;
1152
1153 if (acpi_drv) {
46ec8598
BH
1154 if (acpi_drv->ops.notify)
1155 acpi_device_remove_notify_handler(acpi_dev);
5d9464a4 1156 if (acpi_drv->ops.remove)
51fac838 1157 acpi_drv->ops.remove(acpi_dev);
1da177e4 1158 }
5d9464a4 1159 acpi_dev->driver = NULL;
db89b4f0 1160 acpi_dev->driver_data = NULL;
1da177e4 1161
5d9464a4 1162 put_device(dev);
9e89dde2
ZR
1163 return 0;
1164}
1da177e4 1165
55955aad 1166struct bus_type acpi_bus_type = {
9e89dde2 1167 .name = "acpi",
5d9464a4
PM
1168 .match = acpi_bus_match,
1169 .probe = acpi_device_probe,
1170 .remove = acpi_device_remove,
1171 .uevent = acpi_device_uevent,
9e89dde2
ZR
1172};
1173
d783156e
RW
1174static void acpi_device_del(struct acpi_device *device)
1175{
1176 mutex_lock(&acpi_device_lock);
1177 if (device->parent)
1178 list_del(&device->node);
1179
1180 list_del(&device->wakeup_list);
1181 mutex_unlock(&acpi_device_lock);
1182
1183 acpi_power_add_remove_device(device, false);
1184 acpi_device_remove_files(device);
1185 if (device->remove)
1186 device->remove(device);
1187
1188 device_del(&device->dev);
1189}
1190
1191static LIST_HEAD(acpi_device_del_list);
1192static DEFINE_MUTEX(acpi_device_del_lock);
1193
1194static void acpi_device_del_work_fn(struct work_struct *work_not_used)
1195{
1196 for (;;) {
1197 struct acpi_device *adev;
1198
1199 mutex_lock(&acpi_device_del_lock);
1200
1201 if (list_empty(&acpi_device_del_list)) {
1202 mutex_unlock(&acpi_device_del_lock);
1203 break;
1204 }
1205 adev = list_first_entry(&acpi_device_del_list,
1206 struct acpi_device, del_list);
1207 list_del(&adev->del_list);
1208
1209 mutex_unlock(&acpi_device_del_lock);
1210
1211 acpi_device_del(adev);
1212 /*
1213 * Drop references to all power resources that might have been
1214 * used by the device.
1215 */
1216 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
1217 put_device(&adev->dev);
1218 }
1219}
1220
1221/**
1222 * acpi_scan_drop_device - Drop an ACPI device object.
1223 * @handle: Handle of an ACPI namespace node, not used.
1224 * @context: Address of the ACPI device object to drop.
1225 *
1226 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
1227 * namespace node the device object pointed to by @context is attached to.
1228 *
1229 * The unregistration is carried out asynchronously to avoid running
1230 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
1231 * ensure the correct ordering (the device objects must be unregistered in the
1232 * same order in which the corresponding namespace nodes are deleted).
1233 */
1234static void acpi_scan_drop_device(acpi_handle handle, void *context)
caf5c03f 1235{
d783156e
RW
1236 static DECLARE_WORK(work, acpi_device_del_work_fn);
1237 struct acpi_device *adev = context;
1238
1239 mutex_lock(&acpi_device_del_lock);
1240
1241 /*
1242 * Use the ACPI hotplug workqueue which is ordered, so this work item
1243 * won't run after any hotplug work items submitted subsequently. That
1244 * prevents attempts to register device objects identical to those being
1245 * deleted from happening concurrently (such attempts result from
1246 * hotplug events handled via the ACPI hotplug workqueue). It also will
1247 * run after all of the work items submitted previosuly, which helps
1248 * those work items to ensure that they are not accessing stale device
1249 * objects.
1250 */
1251 if (list_empty(&acpi_device_del_list))
1252 acpi_queue_hotplug_work(&work);
1253
1254 list_add_tail(&adev->del_list, &acpi_device_del_list);
1255 /* Make acpi_ns_validate_handle() return NULL for this handle. */
1256 adev->handle = INVALID_ACPI_HANDLE;
1257
1258 mutex_unlock(&acpi_device_del_lock);
caf5c03f
RW
1259}
1260
78ea4639
RW
1261static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
1262 void (*callback)(void *))
caf5c03f
RW
1263{
1264 acpi_status status;
1265
1266 if (!device)
1267 return -EINVAL;
1268
78ea4639
RW
1269 status = acpi_get_data_full(handle, acpi_scan_drop_device,
1270 (void **)device, callback);
caf5c03f
RW
1271 if (ACPI_FAILURE(status) || !*device) {
1272 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
1273 handle));
1274 return -ENODEV;
1275 }
1276 return 0;
1277}
78ea4639
RW
1278
1279int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
1280{
1281 return acpi_get_device_data(handle, device, NULL);
1282}
6585925b 1283EXPORT_SYMBOL(acpi_bus_get_device);
caf5c03f 1284
78ea4639
RW
1285static void get_acpi_device(void *dev)
1286{
1287 if (dev)
1288 get_device(&((struct acpi_device *)dev)->dev);
1289}
1290
1291struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
1292{
1293 struct acpi_device *adev = NULL;
1294
1295 acpi_get_device_data(handle, &adev, get_acpi_device);
1296 return adev;
1297}
1298
1299void acpi_bus_put_acpi_device(struct acpi_device *adev)
1300{
1301 put_device(&adev->dev);
1302}
1303
cf860be6
RW
1304int acpi_device_add(struct acpi_device *device,
1305 void (*release)(struct device *))
1da177e4 1306{
4be44fcd 1307 int result;
e49bd2dd
ZR
1308 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1309 int found = 0;
66b7ed40 1310
d43e167d
RW
1311 if (device->handle) {
1312 acpi_status status;
1313
d783156e 1314 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
d43e167d
RW
1315 device);
1316 if (ACPI_FAILURE(status)) {
1317 acpi_handle_err(device->handle,
1318 "Unable to attach device data\n");
1319 return -ENODEV;
1320 }
1321 }
1322
9e89dde2
ZR
1323 /*
1324 * Linkage
1325 * -------
1326 * Link this device to its parent and siblings.
1327 */
1328 INIT_LIST_HEAD(&device->children);
1329 INIT_LIST_HEAD(&device->node);
9e89dde2 1330 INIT_LIST_HEAD(&device->wakeup_list);
1033f904 1331 INIT_LIST_HEAD(&device->physical_node_list);
d783156e 1332 INIT_LIST_HEAD(&device->del_list);
1033f904 1333 mutex_init(&device->physical_node_lock);
1da177e4 1334
e49bd2dd
ZR
1335 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1336 if (!new_bus_id) {
d43e167d
RW
1337 pr_err(PREFIX "Memory allocation error\n");
1338 result = -ENOMEM;
1339 goto err_detach;
1da177e4 1340 }
e49bd2dd 1341
9090589d 1342 mutex_lock(&acpi_device_lock);
e49bd2dd
ZR
1343 /*
1344 * Find suitable bus_id and instance number in acpi_bus_id_list
1345 * If failed, create one and link it into acpi_bus_id_list
1346 */
1347 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
1131b938
BH
1348 if (!strcmp(acpi_device_bus_id->bus_id,
1349 acpi_device_hid(device))) {
1350 acpi_device_bus_id->instance_no++;
e49bd2dd
ZR
1351 found = 1;
1352 kfree(new_bus_id);
1353 break;
1354 }
1da177e4 1355 }
0c526d96 1356 if (!found) {
e49bd2dd 1357 acpi_device_bus_id = new_bus_id;
1131b938 1358 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
e49bd2dd
ZR
1359 acpi_device_bus_id->instance_no = 0;
1360 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1da177e4 1361 }
0794469d 1362 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
1da177e4 1363
33b57150 1364 if (device->parent)
9e89dde2 1365 list_add_tail(&device->node, &device->parent->children);
33b57150 1366
9e89dde2
ZR
1367 if (device->wakeup.flags.valid)
1368 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
9090589d 1369 mutex_unlock(&acpi_device_lock);
1da177e4 1370
1890a97a 1371 if (device->parent)
66b7ed40 1372 device->dev.parent = &device->parent->dev;
1890a97a 1373 device->dev.bus = &acpi_bus_type;
82c7d5ef 1374 device->dev.release = release;
cf860be6 1375 result = device_add(&device->dev);
0c526d96 1376 if (result) {
8b12b922 1377 dev_err(&device->dev, "Error registering device\n");
d43e167d 1378 goto err;
1da177e4 1379 }
1da177e4 1380
e49bd2dd 1381 result = acpi_device_setup_files(device);
0c526d96 1382 if (result)
0794469d
KS
1383 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1384 dev_name(&device->dev));
1da177e4 1385
1da177e4 1386 return 0;
d43e167d
RW
1387
1388 err:
9090589d 1389 mutex_lock(&acpi_device_lock);
33b57150 1390 if (device->parent)
e49bd2dd 1391 list_del(&device->node);
e49bd2dd 1392 list_del(&device->wakeup_list);
9090589d 1393 mutex_unlock(&acpi_device_lock);
d43e167d
RW
1394
1395 err_detach:
d783156e 1396 acpi_detach_data(device->handle, acpi_scan_drop_device);
e49bd2dd 1397 return result;
1da177e4
LT
1398}
1399
8a0662d9
RW
1400struct acpi_device *acpi_get_next_child(struct device *dev,
1401 struct acpi_device *child)
1402{
1403 struct acpi_device *adev = ACPI_COMPANION(dev);
1404 struct list_head *head, *next;
1405
1406 if (!adev)
1407 return NULL;
1408
1409 head = &adev->children;
1410 if (list_empty(head))
1411 return NULL;
1412
1413 if (!child)
1414 return list_first_entry(head, struct acpi_device, node);
1415
1416 next = child->node.next;
1417 return next == head ? NULL : list_entry(next, struct acpi_device, node);
1418}
1419
9e89dde2
ZR
1420/* --------------------------------------------------------------------------
1421 Driver Management
1422 -------------------------------------------------------------------------- */
9e89dde2
ZR
1423/**
1424 * acpi_bus_register_driver - register a driver with the ACPI bus
1425 * @driver: driver being registered
1426 *
1427 * Registers a driver with the ACPI bus. Searches the namespace for all
1428 * devices that match the driver's criteria and binds. Returns zero for
1429 * success or a negative error status for failure.
1430 */
1431int acpi_bus_register_driver(struct acpi_driver *driver)
1da177e4 1432{
1890a97a 1433 int ret;
1da177e4 1434
9e89dde2
ZR
1435 if (acpi_disabled)
1436 return -ENODEV;
1890a97a
PM
1437 driver->drv.name = driver->name;
1438 driver->drv.bus = &acpi_bus_type;
1439 driver->drv.owner = driver->owner;
1da177e4 1440
1890a97a
PM
1441 ret = driver_register(&driver->drv);
1442 return ret;
9e89dde2 1443}
1da177e4 1444
9e89dde2
ZR
1445EXPORT_SYMBOL(acpi_bus_register_driver);
1446
1447/**
b27b14ce 1448 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
9e89dde2
ZR
1449 * @driver: driver to unregister
1450 *
1451 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1452 * devices that match the driver's criteria and unbinds.
1453 */
1454void acpi_bus_unregister_driver(struct acpi_driver *driver)
1455{
1890a97a 1456 driver_unregister(&driver->drv);
9e89dde2
ZR
1457}
1458
1459EXPORT_SYMBOL(acpi_bus_unregister_driver);
1460
9e89dde2
ZR
1461/* --------------------------------------------------------------------------
1462 Device Enumeration
1463 -------------------------------------------------------------------------- */
5c478f49
BH
1464static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1465{
456de893 1466 struct acpi_device *device = NULL;
5c478f49 1467 acpi_status status;
5c478f49
BH
1468
1469 /*
1470 * Fixed hardware devices do not appear in the namespace and do not
1471 * have handles, but we fabricate acpi_devices for them, so we have
1472 * to deal with them specially.
1473 */
456de893 1474 if (!handle)
5c478f49
BH
1475 return acpi_root;
1476
1477 do {
1478 status = acpi_get_parent(handle, &handle);
5c478f49 1479 if (ACPI_FAILURE(status))
456de893
RW
1480 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1481 } while (acpi_bus_get_device(handle, &device));
1482 return device;
5c478f49
BH
1483}
1484
9e89dde2
ZR
1485acpi_status
1486acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1487{
1488 acpi_status status;
1489 acpi_handle tmp;
1490 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1491 union acpi_object *obj;
1492
1493 status = acpi_get_handle(handle, "_EJD", &tmp);
1494 if (ACPI_FAILURE(status))
1495 return status;
1496
1497 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1498 if (ACPI_SUCCESS(status)) {
1499 obj = buffer.pointer;
3b5fee59
HM
1500 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1501 ejd);
9e89dde2
ZR
1502 kfree(buffer.pointer);
1503 }
1504 return status;
1505}
1506EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1507
e88c9c60
RW
1508static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1509 struct acpi_device_wakeup *wakeup)
9e89dde2 1510{
b581a7f9
RW
1511 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1512 union acpi_object *package = NULL;
9e89dde2 1513 union acpi_object *element = NULL;
b581a7f9 1514 acpi_status status;
e88c9c60 1515 int err = -ENODATA;
9e89dde2 1516
b581a7f9 1517 if (!wakeup)
e88c9c60 1518 return -EINVAL;
9e89dde2 1519
993cbe59 1520 INIT_LIST_HEAD(&wakeup->resources);
9e89dde2 1521
b581a7f9
RW
1522 /* _PRW */
1523 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1524 if (ACPI_FAILURE(status)) {
1525 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
e88c9c60 1526 return err;
b581a7f9
RW
1527 }
1528
1529 package = (union acpi_object *)buffer.pointer;
1530
e88c9c60 1531 if (!package || package->package.count < 2)
b581a7f9 1532 goto out;
b581a7f9 1533
9e89dde2 1534 element = &(package->package.elements[0]);
e88c9c60 1535 if (!element)
b581a7f9 1536 goto out;
e88c9c60 1537
9e89dde2
ZR
1538 if (element->type == ACPI_TYPE_PACKAGE) {
1539 if ((element->package.count < 2) ||
1540 (element->package.elements[0].type !=
1541 ACPI_TYPE_LOCAL_REFERENCE)
e88c9c60 1542 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
b581a7f9 1543 goto out;
e88c9c60 1544
b581a7f9 1545 wakeup->gpe_device =
9e89dde2 1546 element->package.elements[0].reference.handle;
b581a7f9 1547 wakeup->gpe_number =
9e89dde2
ZR
1548 (u32) element->package.elements[1].integer.value;
1549 } else if (element->type == ACPI_TYPE_INTEGER) {
b581a7f9
RW
1550 wakeup->gpe_device = NULL;
1551 wakeup->gpe_number = element->integer.value;
1552 } else {
b581a7f9
RW
1553 goto out;
1554 }
9e89dde2
ZR
1555
1556 element = &(package->package.elements[1]);
e88c9c60 1557 if (element->type != ACPI_TYPE_INTEGER)
b581a7f9 1558 goto out;
e88c9c60 1559
b581a7f9 1560 wakeup->sleep_state = element->integer.value;
9e89dde2 1561
e88c9c60
RW
1562 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1563 if (err)
b581a7f9 1564 goto out;
9e89dde2 1565
0596a52b
RW
1566 if (!list_empty(&wakeup->resources)) {
1567 int sleep_state;
9e89dde2 1568
b5d667eb
RW
1569 err = acpi_power_wakeup_list_init(&wakeup->resources,
1570 &sleep_state);
1571 if (err) {
1572 acpi_handle_warn(handle, "Retrieving current states "
1573 "of wakeup power resources failed\n");
1574 acpi_power_resources_list_free(&wakeup->resources);
1575 goto out;
1576 }
0596a52b
RW
1577 if (sleep_state < wakeup->sleep_state) {
1578 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1579 "(S%d) by S%d from power resources\n",
1580 (int)wakeup->sleep_state, sleep_state);
1581 wakeup->sleep_state = sleep_state;
1582 }
1583 }
9874647b 1584
b581a7f9
RW
1585 out:
1586 kfree(buffer.pointer);
e88c9c60 1587 return err;
1da177e4
LT
1588}
1589
bd9b2f9a 1590static void acpi_wakeup_gpe_init(struct acpi_device *device)
1da177e4 1591{
29b71a1c 1592 struct acpi_device_id button_device_ids[] = {
29b71a1c 1593 {"PNP0C0C", 0},
b7e38304 1594 {"PNP0C0D", 0},
29b71a1c
TR
1595 {"PNP0C0E", 0},
1596 {"", 0},
1597 };
bd9b2f9a 1598 struct acpi_device_wakeup *wakeup = &device->wakeup;
f517709d
RW
1599 acpi_status status;
1600 acpi_event_status event_status;
1601
bd9b2f9a 1602 wakeup->flags.notifier_present = 0;
f517709d
RW
1603
1604 /* Power button, Lid switch always enable wakeup */
1605 if (!acpi_match_device_ids(device, button_device_ids)) {
bd9b2f9a 1606 wakeup->flags.run_wake = 1;
b7e38304
ZR
1607 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1608 /* Do not use Lid/sleep button for S5 wakeup */
bd9b2f9a
RW
1609 if (wakeup->sleep_state == ACPI_STATE_S5)
1610 wakeup->sleep_state = ACPI_STATE_S4;
b7e38304 1611 }
bd9b2f9a 1612 acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
f2b56bc8 1613 device_set_wakeup_capable(&device->dev, true);
f517709d
RW
1614 return;
1615 }
1616
bd9b2f9a
RW
1617 acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
1618 wakeup->gpe_number);
1619 status = acpi_get_gpe_status(wakeup->gpe_device, wakeup->gpe_number,
1620 &event_status);
1621 if (ACPI_FAILURE(status))
1622 return;
1623
2f857234 1624 wakeup->flags.run_wake = !!(event_status & ACPI_EVENT_FLAG_HAS_HANDLER);
f517709d
RW
1625}
1626
d57d09a4 1627static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
f517709d 1628{
e88c9c60 1629 int err;
29b71a1c 1630
d57d09a4 1631 /* Presence of _PRW indicates wake capable */
952c63e9 1632 if (!acpi_has_method(device->handle, "_PRW"))
d57d09a4
RW
1633 return;
1634
e88c9c60
RW
1635 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1636 &device->wakeup);
1637 if (err) {
1638 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
d57d09a4 1639 return;
9e89dde2 1640 }
1da177e4 1641
9e89dde2 1642 device->wakeup.flags.valid = 1;
9b83ccd2 1643 device->wakeup.prepare_count = 0;
bd9b2f9a 1644 acpi_wakeup_gpe_init(device);
729b2bdb
ZY
1645 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1646 * system for the ACPI device with the _PRW object.
1647 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1648 * So it is necessary to call _DSW object first. Only when it is not
1649 * present will the _PSW object used.
1650 */
e88c9c60
RW
1651 err = acpi_device_sleep_wake(device, 0, 0, 0);
1652 if (err)
77e76609
RW
1653 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1654 "error in _DSW or _PSW evaluation\n"));
1da177e4 1655}
1da177e4 1656
f33ce568
RW
1657static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1658{
1659 struct acpi_device_power_state *ps = &device->power.states[state];
ef85bdbe
RW
1660 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1661 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
f33ce568 1662 acpi_status status;
bf325f95 1663
f33ce568
RW
1664 INIT_LIST_HEAD(&ps->resources);
1665
ef85bdbe
RW
1666 /* Evaluate "_PRx" to get referenced power resources */
1667 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1668 if (ACPI_SUCCESS(status)) {
1669 union acpi_object *package = buffer.pointer;
1670
1671 if (buffer.length && package
1672 && package->type == ACPI_TYPE_PACKAGE
1673 && package->package.count) {
e88c9c60
RW
1674 int err = acpi_extract_power_resources(package, 0,
1675 &ps->resources);
1676 if (!err)
ef85bdbe 1677 device->power.flags.power_resources = 1;
f33ce568 1678 }
ef85bdbe 1679 ACPI_FREE(buffer.pointer);
f33ce568
RW
1680 }
1681
1682 /* Evaluate "_PSx" to see if we can do explicit sets */
ef85bdbe 1683 pathname[2] = 'S';
952c63e9 1684 if (acpi_has_method(device->handle, pathname))
f33ce568
RW
1685 ps->flags.explicit_set = 1;
1686
1687 /*
1688 * State is valid if there are means to put the device into it.
1689 * D3hot is only valid if _PR3 present.
1690 */
ef85bdbe 1691 if (!list_empty(&ps->resources)
f33ce568
RW
1692 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1693 ps->flags.valid = 1;
1694 ps->flags.os_accessible = 1;
1695 }
1696
1697 ps->power = -1; /* Unknown - driver assigned */
1698 ps->latency = -1; /* Unknown - driver assigned */
1699}
1700
d43e167d 1701static void acpi_bus_get_power_flags(struct acpi_device *device)
1da177e4 1702{
8bc5053b 1703 u32 i;
1a365616 1704
d43e167d 1705 /* Presence of _PS0|_PR0 indicates 'power manageable' */
952c63e9
JL
1706 if (!acpi_has_method(device->handle, "_PS0") &&
1707 !acpi_has_method(device->handle, "_PR0"))
1708 return;
1a365616 1709
d43e167d 1710 device->flags.power_manageable = 1;
4be44fcd 1711
9e89dde2
ZR
1712 /*
1713 * Power Management Flags
1714 */
952c63e9 1715 if (acpi_has_method(device->handle, "_PSC"))
9e89dde2 1716 device->power.flags.explicit_get = 1;
f25c0ae2 1717
952c63e9 1718 if (acpi_has_method(device->handle, "_IRC"))
9e89dde2 1719 device->power.flags.inrush_current = 1;
1da177e4 1720
f25c0ae2
RW
1721 if (acpi_has_method(device->handle, "_DSW"))
1722 device->power.flags.dsw_present = 1;
1723
9e89dde2
ZR
1724 /*
1725 * Enumerate supported power management states
1726 */
f33ce568
RW
1727 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1728 acpi_bus_init_power_state(device, i);
bf325f95 1729
0b224527 1730 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1da177e4 1731
9e89dde2
ZR
1732 /* Set defaults for D0 and D3 states (always valid) */
1733 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1734 device->power.states[ACPI_STATE_D0].power = 100;
8ad928d5
RW
1735 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1736 device->power.states[ACPI_STATE_D3_COLD].power = 0;
c8f7a62c 1737
5c7dd710
RW
1738 /* Set D3cold's explicit_set flag if _PS3 exists. */
1739 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1740 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1741
1399dfcd
AL
1742 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1743 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1744 device->power.flags.power_resources)
1745 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1746
b3785492
RW
1747 if (acpi_bus_init_power(device)) {
1748 acpi_free_power_resources_lists(device);
1749 device->flags.power_manageable = 0;
1750 }
9e89dde2 1751}
c8f7a62c 1752
d43e167d 1753static void acpi_bus_get_flags(struct acpi_device *device)
1da177e4 1754{
1da177e4 1755 /* Presence of _STA indicates 'dynamic_status' */
952c63e9 1756 if (acpi_has_method(device->handle, "_STA"))
1da177e4
LT
1757 device->flags.dynamic_status = 1;
1758
1da177e4 1759 /* Presence of _RMV indicates 'removable' */
952c63e9 1760 if (acpi_has_method(device->handle, "_RMV"))
1da177e4
LT
1761 device->flags.removable = 1;
1762
1763 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
952c63e9
JL
1764 if (acpi_has_method(device->handle, "_EJD") ||
1765 acpi_has_method(device->handle, "_EJ0"))
1da177e4 1766 device->flags.ejectable = 1;
1da177e4
LT
1767}
1768
c7bcb4e9 1769static void acpi_device_get_busid(struct acpi_device *device)
1da177e4 1770{
4be44fcd
LB
1771 char bus_id[5] = { '?', 0 };
1772 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1773 int i = 0;
1da177e4
LT
1774
1775 /*
1776 * Bus ID
1777 * ------
1778 * The device's Bus ID is simply the object name.
1779 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1780 */
859ac9a4 1781 if (ACPI_IS_ROOT_DEVICE(device)) {
1da177e4 1782 strcpy(device->pnp.bus_id, "ACPI");
859ac9a4
BH
1783 return;
1784 }
1785
1786 switch (device->device_type) {
1da177e4
LT
1787 case ACPI_BUS_TYPE_POWER_BUTTON:
1788 strcpy(device->pnp.bus_id, "PWRF");
1789 break;
1790 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1791 strcpy(device->pnp.bus_id, "SLPF");
1792 break;
1793 default:
66b7ed40 1794 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1da177e4
LT
1795 /* Clean up trailing underscores (if any) */
1796 for (i = 3; i > 1; i--) {
1797 if (bus_id[i] == '_')
1798 bus_id[i] = '\0';
1799 else
1800 break;
1801 }
1802 strcpy(device->pnp.bus_id, bus_id);
1803 break;
1804 }
1805}
1806
ebf4df8d
JL
1807/*
1808 * acpi_ata_match - see if an acpi object is an ATA device
1809 *
1810 * If an acpi object has one of the ACPI ATA methods defined,
1811 * then we can safely call it an ATA device.
1812 */
1813bool acpi_ata_match(acpi_handle handle)
1814{
1815 return acpi_has_method(handle, "_GTF") ||
1816 acpi_has_method(handle, "_GTM") ||
1817 acpi_has_method(handle, "_STM") ||
1818 acpi_has_method(handle, "_SDD");
1819}
1820
54735266 1821/*
d4e1a692 1822 * acpi_bay_match - see if an acpi object is an ejectable driver bay
54735266
ZR
1823 *
1824 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1825 * then we can safely call it an ejectable drive bay
1826 */
ebf4df8d 1827bool acpi_bay_match(acpi_handle handle)
d4e1a692 1828{
54735266
ZR
1829 acpi_handle phandle;
1830
952c63e9 1831 if (!acpi_has_method(handle, "_EJ0"))
ebf4df8d
JL
1832 return false;
1833 if (acpi_ata_match(handle))
1834 return true;
1835 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1836 return false;
54735266 1837
ebf4df8d 1838 return acpi_ata_match(phandle);
54735266
ZR
1839}
1840
b43109fa 1841bool acpi_device_is_battery(struct acpi_device *adev)
1e2380cd 1842{
b43109fa 1843 struct acpi_hardware_id *hwid;
1e2380cd 1844
b43109fa
RW
1845 list_for_each_entry(hwid, &adev->pnp.ids, list)
1846 if (!strcmp("PNP0C0A", hwid->id))
1847 return true;
1e2380cd 1848
b43109fa 1849 return false;
1e2380cd
RW
1850}
1851
b43109fa 1852static bool is_ejectable_bay(struct acpi_device *adev)
1e2380cd 1853{
b43109fa
RW
1854 acpi_handle handle = adev->handle;
1855
1856 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
1e2380cd
RW
1857 return true;
1858
1859 return acpi_bay_match(handle);
1860}
1861
3620f2f2 1862/*
d4e1a692 1863 * acpi_dock_match - see if an acpi object has a _DCK method
3620f2f2 1864 */
ebf4df8d 1865bool acpi_dock_match(acpi_handle handle)
3620f2f2 1866{
ebf4df8d 1867 return acpi_has_method(handle, "_DCK");
3620f2f2
FS
1868}
1869
620e112c 1870const char *acpi_device_hid(struct acpi_device *device)
15b8dd53 1871{
7f47fa6c 1872 struct acpi_hardware_id *hid;
15b8dd53 1873
2b2ae7c7
TR
1874 if (list_empty(&device->pnp.ids))
1875 return dummy_hid;
1876
7f47fa6c
BH
1877 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1878 return hid->id;
1879}
1880EXPORT_SYMBOL(acpi_device_hid);
15b8dd53 1881
d4e1a692 1882static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
7f47fa6c
BH
1883{
1884 struct acpi_hardware_id *id;
15b8dd53 1885
7f47fa6c
BH
1886 id = kmalloc(sizeof(*id), GFP_KERNEL);
1887 if (!id)
1888 return;
15b8dd53 1889
581de59e 1890 id->id = kstrdup(dev_id, GFP_KERNEL);
7f47fa6c
BH
1891 if (!id->id) {
1892 kfree(id);
1893 return;
15b8dd53
BM
1894 }
1895
d4e1a692
TK
1896 list_add_tail(&id->list, &pnp->ids);
1897 pnp->type.hardware_id = 1;
15b8dd53
BM
1898}
1899
222e82ac
DW
1900/*
1901 * Old IBM workstations have a DSDT bug wherein the SMBus object
1902 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1903 * prefix. Work around this.
1904 */
ebf4df8d 1905static bool acpi_ibm_smbus_match(acpi_handle handle)
222e82ac 1906{
ebf4df8d
JL
1907 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1908 struct acpi_buffer path = { sizeof(node_name), node_name };
222e82ac
DW
1909
1910 if (!dmi_name_in_vendors("IBM"))
ebf4df8d 1911 return false;
222e82ac
DW
1912
1913 /* Look for SMBS object */
ebf4df8d
JL
1914 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1915 strcmp("SMBS", path.pointer))
1916 return false;
222e82ac
DW
1917
1918 /* Does it have the necessary (but misnamed) methods? */
952c63e9
JL
1919 if (acpi_has_method(handle, "SBI") &&
1920 acpi_has_method(handle, "SBR") &&
1921 acpi_has_method(handle, "SBW"))
ebf4df8d
JL
1922 return true;
1923
1924 return false;
222e82ac
DW
1925}
1926
ae3caa80
JL
1927static bool acpi_object_is_system_bus(acpi_handle handle)
1928{
1929 acpi_handle tmp;
1930
1931 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1932 tmp == handle)
1933 return true;
1934 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1935 tmp == handle)
1936 return true;
1937
1938 return false;
1939}
1940
c0af4175
TK
1941static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1942 int device_type)
1da177e4 1943{
4be44fcd 1944 acpi_status status;
57f3674f 1945 struct acpi_device_info *info;
78e25fef 1946 struct acpi_pnp_device_id_list *cid_list;
7f47fa6c 1947 int i;
1da177e4 1948
c0af4175 1949 switch (device_type) {
1da177e4 1950 case ACPI_BUS_TYPE_DEVICE:
c0af4175
TK
1951 if (handle == ACPI_ROOT_OBJECT) {
1952 acpi_add_id(pnp, ACPI_SYSTEM_HID);
859ac9a4
BH
1953 break;
1954 }
1955
c0af4175 1956 status = acpi_get_object_info(handle, &info);
1da177e4 1957 if (ACPI_FAILURE(status)) {
c0af4175
TK
1958 pr_err(PREFIX "%s: Error reading device info\n",
1959 __func__);
1da177e4
LT
1960 return;
1961 }
1962
549e6845 1963 if (info->valid & ACPI_VALID_HID) {
c0af4175 1964 acpi_add_id(pnp, info->hardware_id.string);
549e6845
RW
1965 pnp->type.platform_id = 1;
1966 }
57f3674f 1967 if (info->valid & ACPI_VALID_CID) {
15b8dd53 1968 cid_list = &info->compatible_id_list;
57f3674f 1969 for (i = 0; i < cid_list->count; i++)
c0af4175 1970 acpi_add_id(pnp, cid_list->ids[i].string);
57f3674f 1971 }
1da177e4 1972 if (info->valid & ACPI_VALID_ADR) {
c0af4175
TK
1973 pnp->bus_address = info->address;
1974 pnp->type.bus_address = 1;
1da177e4 1975 }
ccf78040 1976 if (info->valid & ACPI_VALID_UID)
c0af4175 1977 pnp->unique_id = kstrdup(info->unique_id.string,
ccf78040 1978 GFP_KERNEL);
ae843332 1979
a83893ae
BH
1980 kfree(info);
1981
57f3674f
BH
1982 /*
1983 * Some devices don't reliably have _HIDs & _CIDs, so add
1984 * synthetic HIDs to make sure drivers can find them.
1985 */
c0af4175
TK
1986 if (acpi_is_video_device(handle))
1987 acpi_add_id(pnp, ACPI_VIDEO_HID);
ebf4df8d 1988 else if (acpi_bay_match(handle))
c0af4175 1989 acpi_add_id(pnp, ACPI_BAY_HID);
ebf4df8d 1990 else if (acpi_dock_match(handle))
c0af4175 1991 acpi_add_id(pnp, ACPI_DOCK_HID);
ebf4df8d 1992 else if (acpi_ibm_smbus_match(handle))
c0af4175 1993 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
ae3caa80
JL
1994 else if (list_empty(&pnp->ids) &&
1995 acpi_object_is_system_bus(handle)) {
1996 /* \_SB, \_TZ, LNXSYBUS */
1997 acpi_add_id(pnp, ACPI_BUS_HID);
c0af4175
TK
1998 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1999 strcpy(pnp->device_class, ACPI_BUS_CLASS);
b7b30de5 2000 }
54735266 2001
1da177e4
LT
2002 break;
2003 case ACPI_BUS_TYPE_POWER:
c0af4175 2004 acpi_add_id(pnp, ACPI_POWER_HID);
1da177e4
LT
2005 break;
2006 case ACPI_BUS_TYPE_PROCESSOR:
c0af4175 2007 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1da177e4 2008 break;
1da177e4 2009 case ACPI_BUS_TYPE_THERMAL:
c0af4175 2010 acpi_add_id(pnp, ACPI_THERMAL_HID);
1da177e4
LT
2011 break;
2012 case ACPI_BUS_TYPE_POWER_BUTTON:
c0af4175 2013 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1da177e4
LT
2014 break;
2015 case ACPI_BUS_TYPE_SLEEP_BUTTON:
c0af4175 2016 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1da177e4
LT
2017 break;
2018 }
1da177e4
LT
2019}
2020
c0af4175
TK
2021void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
2022{
2023 struct acpi_hardware_id *id, *tmp;
2024
2025 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
2026 kfree(id->id);
2027 kfree(id);
2028 }
2029 kfree(pnp->unique_id);
2030}
2031
82c7d5ef
RW
2032void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
2033 int type, unsigned long long sta)
1da177e4 2034{
d43e167d
RW
2035 INIT_LIST_HEAD(&device->pnp.ids);
2036 device->device_type = type;
2037 device->handle = handle;
2038 device->parent = acpi_bus_get_parent(handle);
8a0662d9 2039 device->fwnode.type = FWNODE_ACPI;
25db115b 2040 acpi_set_device_status(device, sta);
d43e167d 2041 acpi_device_get_busid(device);
c0af4175 2042 acpi_set_pnp_ids(handle, &device->pnp, type);
ffdcd955 2043 acpi_init_properties(device);
d43e167d 2044 acpi_bus_get_flags(device);
2c0d4fe0 2045 device->flags.match_driver = false;
202317a5
RW
2046 device->flags.initialized = true;
2047 device->flags.visited = false;
cf860be6
RW
2048 device_initialize(&device->dev);
2049 dev_set_uevent_suppress(&device->dev, true);
2050}
bc3b0772 2051
cf860be6
RW
2052void acpi_device_add_finalize(struct acpi_device *device)
2053{
2054 dev_set_uevent_suppress(&device->dev, false);
2055 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1da177e4
LT
2056}
2057
5c478f49
BH
2058static int acpi_add_single_object(struct acpi_device **child,
2059 acpi_handle handle, int type,
2c0d4fe0 2060 unsigned long long sta)
1da177e4 2061{
77c24888
BH
2062 int result;
2063 struct acpi_device *device;
29aaefa6 2064 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1da177e4 2065
36bcbec7 2066 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1da177e4 2067 if (!device) {
6468463a 2068 printk(KERN_ERR PREFIX "Memory allocation error\n");
d550d98d 2069 return -ENOMEM;
1da177e4 2070 }
1da177e4 2071
d43e167d
RW
2072 acpi_init_device_object(device, handle, type, sta);
2073 acpi_bus_get_power_flags(device);
d57d09a4 2074 acpi_bus_get_wakeup_device_flags(device);
1da177e4 2075
cf860be6 2076 result = acpi_device_add(device, acpi_device_release);
d43e167d 2077 if (result) {
718fb0de 2078 acpi_device_release(&device->dev);
d43e167d
RW
2079 return result;
2080 }
1da177e4 2081
d43e167d 2082 acpi_power_add_remove_device(device, true);
cf860be6 2083 acpi_device_add_finalize(device);
d43e167d
RW
2084 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
2085 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
2086 dev_name(&device->dev), (char *) buffer.pointer,
2087 device->parent ? dev_name(&device->parent->dev) : "(null)"));
2088 kfree(buffer.pointer);
2089 *child = device;
2090 return 0;
bf325f95
RW
2091}
2092
778cbc1d
BH
2093static int acpi_bus_type_and_status(acpi_handle handle, int *type,
2094 unsigned long long *sta)
1da177e4 2095{
778cbc1d
BH
2096 acpi_status status;
2097 acpi_object_type acpi_type;
1da177e4 2098
778cbc1d 2099 status = acpi_get_type(handle, &acpi_type);
51a85faf 2100 if (ACPI_FAILURE(status))
778cbc1d 2101 return -ENODEV;
4be44fcd 2102
778cbc1d 2103 switch (acpi_type) {
51a85faf
BH
2104 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
2105 case ACPI_TYPE_DEVICE:
778cbc1d
BH
2106 *type = ACPI_BUS_TYPE_DEVICE;
2107 status = acpi_bus_get_status_handle(handle, sta);
2108 if (ACPI_FAILURE(status))
2109 return -ENODEV;
51a85faf
BH
2110 break;
2111 case ACPI_TYPE_PROCESSOR:
778cbc1d
BH
2112 *type = ACPI_BUS_TYPE_PROCESSOR;
2113 status = acpi_bus_get_status_handle(handle, sta);
2114 if (ACPI_FAILURE(status))
2115 return -ENODEV;
51a85faf
BH
2116 break;
2117 case ACPI_TYPE_THERMAL:
778cbc1d
BH
2118 *type = ACPI_BUS_TYPE_THERMAL;
2119 *sta = ACPI_STA_DEFAULT;
51a85faf
BH
2120 break;
2121 case ACPI_TYPE_POWER:
778cbc1d
BH
2122 *type = ACPI_BUS_TYPE_POWER;
2123 *sta = ACPI_STA_DEFAULT;
51a85faf
BH
2124 break;
2125 default:
778cbc1d 2126 return -ENODEV;
51a85faf 2127 }
1da177e4 2128
778cbc1d
BH
2129 return 0;
2130}
2131
202317a5
RW
2132bool acpi_device_is_present(struct acpi_device *adev)
2133{
2134 if (adev->status.present || adev->status.functional)
2135 return true;
2136
2137 adev->flags.initialized = false;
2138 return false;
2139}
2140
4b59cc1f
RW
2141static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
2142 char *idstr,
2143 const struct acpi_device_id **matchid)
2144{
2145 const struct acpi_device_id *devid;
2146
aca0a4eb
RW
2147 if (handler->match)
2148 return handler->match(idstr, matchid);
2149
4b59cc1f
RW
2150 for (devid = handler->ids; devid->id[0]; devid++)
2151 if (!strcmp((char *)devid->id, idstr)) {
2152 if (matchid)
2153 *matchid = devid;
2154
2155 return true;
2156 }
2157
2158 return false;
2159}
2160
6b772e8f
TK
2161static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
2162 const struct acpi_device_id **matchid)
2163{
2164 struct acpi_scan_handler *handler;
2165
2166 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
2167 if (acpi_scan_handler_matching(handler, idstr, matchid))
2168 return handler;
2169
2170 return NULL;
2171}
2172
3f8055c3
RW
2173void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
2174{
3f8055c3
RW
2175 if (!!hotplug->enabled == !!val)
2176 return;
2177
2178 mutex_lock(&acpi_scan_lock);
2179
2180 hotplug->enabled = val;
3f8055c3
RW
2181
2182 mutex_unlock(&acpi_scan_lock);
2183}
2184
3c2cc7ff 2185static void acpi_scan_init_hotplug(struct acpi_device *adev)
a33ec399 2186{
6b772e8f 2187 struct acpi_hardware_id *hwid;
a33ec399 2188
b43109fa 2189 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
1e2380cd
RW
2190 acpi_dock_add(adev);
2191 return;
2192 }
3c2cc7ff
RW
2193 list_for_each_entry(hwid, &adev->pnp.ids, list) {
2194 struct acpi_scan_handler *handler;
a33ec399 2195
6b772e8f 2196 handler = acpi_scan_match_handler(hwid->id, NULL);
1a699476
RW
2197 if (handler) {
2198 adev->flags.hotplug_notify = true;
2199 break;
2200 }
3c2cc7ff 2201 }
a33ec399
RW
2202}
2203
40e7fcb1
LT
2204static void acpi_device_dep_initialize(struct acpi_device *adev)
2205{
2206 struct acpi_dep_data *dep;
2207 struct acpi_handle_list dep_devices;
2208 acpi_status status;
2209 int i;
2210
2211 if (!acpi_has_method(adev->handle, "_DEP"))
2212 return;
2213
2214 status = acpi_evaluate_reference(adev->handle, "_DEP", NULL,
2215 &dep_devices);
2216 if (ACPI_FAILURE(status)) {
80167a24 2217 dev_dbg(&adev->dev, "Failed to evaluate _DEP.\n");
40e7fcb1
LT
2218 return;
2219 }
2220
2221 for (i = 0; i < dep_devices.count; i++) {
2222 struct acpi_device_info *info;
2223 int skip;
2224
2225 status = acpi_get_object_info(dep_devices.handles[i], &info);
2226 if (ACPI_FAILURE(status)) {
80167a24 2227 dev_dbg(&adev->dev, "Error reading _DEP device info\n");
40e7fcb1
LT
2228 continue;
2229 }
2230
2231 /*
2232 * Skip the dependency of Windows System Power
2233 * Management Controller
2234 */
2235 skip = info->valid & ACPI_VALID_HID &&
2236 !strcmp(info->hardware_id.string, "INT3396");
2237
2238 kfree(info);
2239
2240 if (skip)
2241 continue;
2242
2243 dep = kzalloc(sizeof(struct acpi_dep_data), GFP_KERNEL);
2244 if (!dep)
2245 return;
2246
2247 dep->master = dep_devices.handles[i];
2248 dep->slave = adev->handle;
2249 adev->dep_unmet++;
2250
2251 mutex_lock(&acpi_dep_list_lock);
2252 list_add_tail(&dep->node , &acpi_dep_list);
2253 mutex_unlock(&acpi_dep_list_lock);
2254 }
2255}
2256
e3863094
RW
2257static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
2258 void *not_used, void **return_value)
778cbc1d 2259{
805d410f 2260 struct acpi_device *device = NULL;
778cbc1d
BH
2261 int type;
2262 unsigned long long sta;
2263 int result;
2264
4002bf38
RW
2265 acpi_bus_get_device(handle, &device);
2266 if (device)
2267 goto out;
2268
778cbc1d
BH
2269 result = acpi_bus_type_and_status(handle, &type, &sta);
2270 if (result)
2271 return AE_OK;
2272
82c7d5ef
RW
2273 if (type == ACPI_BUS_TYPE_POWER) {
2274 acpi_add_power_resource(handle);
2275 return AE_OK;
2276 }
2277
2c0d4fe0 2278 acpi_add_single_object(&device, handle, type, sta);
e3b87f8a 2279 if (!device)
51a85faf 2280 return AE_CTRL_DEPTH;
1da177e4 2281
3c2cc7ff 2282 acpi_scan_init_hotplug(device);
40e7fcb1 2283 acpi_device_dep_initialize(device);
3c2cc7ff 2284
4002bf38 2285 out:
51a85faf
BH
2286 if (!*return_value)
2287 *return_value = device;
805d410f 2288
51a85faf
BH
2289 return AE_OK;
2290}
3fb02738 2291
48459340
ZR
2292static int acpi_check_spi_i2c_slave(struct acpi_resource *ares, void *data)
2293{
2294 bool *is_spi_i2c_slave_p = data;
2295
2296 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
2297 return 1;
2298
2299 /*
2300 * devices that are connected to UART still need to be enumerated to
2301 * platform bus
2302 */
2303 if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
2304 *is_spi_i2c_slave_p = true;
2305
2306 /* no need to do more checking */
2307 return -1;
2308}
2309
2310static void acpi_default_enumeration(struct acpi_device *device)
2311{
2312 struct list_head resource_list;
2313 bool is_spi_i2c_slave = false;
2314
2315 if (!device->pnp.type.platform_id || device->handler)
2316 return;
2317
2318 /*
2319 * Do not enemerate SPI/I2C slaves as they will be enuerated by their
2320 * respective parents.
2321 */
2322 INIT_LIST_HEAD(&resource_list);
2323 acpi_dev_get_resources(device, &resource_list, acpi_check_spi_i2c_slave,
2324 &is_spi_i2c_slave);
2325 acpi_dev_free_resource_list(&resource_list);
2326 if (!is_spi_i2c_slave)
2327 acpi_create_platform_device(device);
2328}
2329
c5698074 2330static int acpi_scan_attach_handler(struct acpi_device *device)
ca589f94 2331{
c5698074
RW
2332 struct acpi_hardware_id *hwid;
2333 int ret = 0;
ca589f94 2334
c5698074 2335 list_for_each_entry(hwid, &device->pnp.ids, list) {
87b85b3c 2336 const struct acpi_device_id *devid;
c5698074 2337 struct acpi_scan_handler *handler;
ca589f94 2338
c5698074
RW
2339 handler = acpi_scan_match_handler(hwid->id, &devid);
2340 if (handler) {
d34afa9d
RW
2341 if (!handler->attach) {
2342 device->pnp.type.platform_id = 0;
2343 continue;
2344 }
9cb32acf 2345 device->handler = handler;
87b85b3c 2346 ret = handler->attach(device, devid);
9cb32acf 2347 if (ret > 0)
c5698074 2348 break;
9cb32acf
RW
2349
2350 device->handler = NULL;
2351 if (ret < 0)
c5698074 2352 break;
ca589f94
RW
2353 }
2354 }
48459340
ZR
2355 if (!ret)
2356 acpi_default_enumeration(device);
2357
ca589f94
RW
2358 return ret;
2359}
2360
2c22e652 2361static void acpi_bus_attach(struct acpi_device *device)
51a85faf 2362{
2c22e652 2363 struct acpi_device *child;
1e2380cd 2364 acpi_handle ejd;
ca589f94 2365 int ret;
3fb02738 2366
1e2380cd
RW
2367 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
2368 register_dock_dependent_device(device, ejd);
2369
2c22e652 2370 acpi_bus_get_status(device);
202317a5 2371 /* Skip devices that are not present. */
2c22e652
RW
2372 if (!acpi_device_is_present(device)) {
2373 device->flags.visited = false;
2374 return;
2375 }
3a391a39 2376 if (device->handler)
2c22e652 2377 goto ok;
3a391a39 2378
202317a5
RW
2379 if (!device->flags.initialized) {
2380 acpi_bus_update_power(device, NULL);
2381 device->flags.initialized = true;
2382 }
2c22e652 2383 device->flags.visited = false;
ca589f94 2384 ret = acpi_scan_attach_handler(device);
6931007c 2385 if (ret < 0)
2c22e652 2386 return;
6931007c
RW
2387
2388 device->flags.match_driver = true;
2c22e652
RW
2389 if (!ret) {
2390 ret = device_attach(&device->dev);
2391 if (ret < 0)
2392 return;
2393 }
202317a5 2394 device->flags.visited = true;
202317a5 2395
2c22e652
RW
2396 ok:
2397 list_for_each_entry(child, &device->children, node)
2398 acpi_bus_attach(child);
8ab17fc9
RW
2399
2400 if (device->handler && device->handler->hotplug.notify_online)
2401 device->handler->hotplug.notify_online(device);
1da177e4 2402}
1da177e4 2403
40e7fcb1
LT
2404void acpi_walk_dep_device_list(acpi_handle handle)
2405{
2406 struct acpi_dep_data *dep, *tmp;
2407 struct acpi_device *adev;
2408
2409 mutex_lock(&acpi_dep_list_lock);
2410 list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2411 if (dep->master == handle) {
2412 acpi_bus_get_device(dep->slave, &adev);
2413 if (!adev)
2414 continue;
2415
2416 adev->dep_unmet--;
2417 if (!adev->dep_unmet)
2418 acpi_bus_attach(adev);
2419 list_del(&dep->node);
2420 kfree(dep);
2421 }
2422 }
2423 mutex_unlock(&acpi_dep_list_lock);
2424}
2425EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
2426
636458de 2427/**
b8bd759a 2428 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
636458de 2429 * @handle: Root of the namespace scope to scan.
7779688f 2430 *
636458de
RW
2431 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2432 * found devices.
7779688f 2433 *
636458de
RW
2434 * If no devices were found, -ENODEV is returned, but it does not mean that
2435 * there has been a real error. There just have been no suitable ACPI objects
2436 * in the table trunk from which the kernel could create a device and add an
2437 * appropriate driver.
3757b948
RW
2438 *
2439 * Must be called under acpi_scan_lock.
7779688f 2440 */
b8bd759a 2441int acpi_bus_scan(acpi_handle handle)
3fb02738 2442{
b8bd759a 2443 void *device = NULL;
3fb02738 2444
b8bd759a
RW
2445 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
2446 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2447 acpi_bus_check_add, NULL, NULL, &device);
3fb02738 2448
2c22e652
RW
2449 if (device) {
2450 acpi_bus_attach(device);
2451 return 0;
05404d8f 2452 }
2c22e652 2453 return -ENODEV;
3fb02738 2454}
2c22e652 2455EXPORT_SYMBOL(acpi_bus_scan);
1da177e4 2456
3757b948 2457/**
2c22e652
RW
2458 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2459 * @adev: Root of the ACPI namespace scope to walk.
3757b948
RW
2460 *
2461 * Must be called under acpi_scan_lock.
2462 */
2c22e652 2463void acpi_bus_trim(struct acpi_device *adev)
1da177e4 2464{
2c22e652
RW
2465 struct acpi_scan_handler *handler = adev->handler;
2466 struct acpi_device *child;
2467
2468 list_for_each_entry_reverse(child, &adev->children, node)
2469 acpi_bus_trim(child);
2470
a951c773 2471 adev->flags.match_driver = false;
2c22e652
RW
2472 if (handler) {
2473 if (handler->detach)
2474 handler->detach(adev);
2475
2476 adev->handler = NULL;
2477 } else {
2478 device_release_driver(&adev->dev);
2479 }
05404d8f 2480 /*
2c22e652
RW
2481 * Most likely, the device is going away, so put it into D3cold before
2482 * that.
05404d8f 2483 */
2c22e652
RW
2484 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2485 adev->flags.initialized = false;
2486 adev->flags.visited = false;
1da177e4 2487}
ceaba663
KA
2488EXPORT_SYMBOL_GPL(acpi_bus_trim);
2489
e8b945c9 2490static int acpi_bus_scan_fixed(void)
1da177e4 2491{
4be44fcd 2492 int result = 0;
c4168bff 2493
1da177e4
LT
2494 /*
2495 * Enumerate all fixed-feature devices.
2496 */
2c0d4fe0
RW
2497 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2498 struct acpi_device *device = NULL;
2499
5c478f49 2500 result = acpi_add_single_object(&device, NULL,
c4168bff 2501 ACPI_BUS_TYPE_POWER_BUTTON,
2c0d4fe0
RW
2502 ACPI_STA_DEFAULT);
2503 if (result)
2504 return result;
2505
88346167 2506 device->flags.match_driver = true;
2c0d4fe0
RW
2507 result = device_attach(&device->dev);
2508 if (result < 0)
2509 return result;
2510
c10d7a13 2511 device_init_wakeup(&device->dev, true);
3fb02738 2512 }
1da177e4 2513
2c0d4fe0
RW
2514 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2515 struct acpi_device *device = NULL;
2516
5c478f49 2517 result = acpi_add_single_object(&device, NULL,
c4168bff 2518 ACPI_BUS_TYPE_SLEEP_BUTTON,
2c0d4fe0
RW
2519 ACPI_STA_DEFAULT);
2520 if (result)
2521 return result;
2522
88346167 2523 device->flags.match_driver = true;
2c0d4fe0 2524 result = device_attach(&device->dev);
3fb02738 2525 }
1da177e4 2526
2c0d4fe0 2527 return result < 0 ? result : 0;
1da177e4
LT
2528}
2529
e747f274 2530int __init acpi_scan_init(void)
1da177e4
LT
2531{
2532 int result;
1da177e4 2533
5b327265
PM
2534 result = bus_register(&acpi_bus_type);
2535 if (result) {
2536 /* We don't want to quit even if we failed to add suspend/resume */
2537 printk(KERN_ERR PREFIX "Could not register bus type\n");
2538 }
2539
92ef2a25 2540 acpi_pci_root_init();
4daeaf68 2541 acpi_pci_link_init();
ac212b69 2542 acpi_processor_init();
f58b082a 2543 acpi_lpss_init();
2fa97feb 2544 acpi_cmos_rtc_init();
737f1a9f 2545 acpi_container_init();
0a347644 2546 acpi_memory_hotplug_init();
eec15edb 2547 acpi_pnp_init();
3230bbfc 2548 acpi_int340x_thermal_init();
97d9a9e9 2549
3757b948 2550 mutex_lock(&acpi_scan_lock);
1da177e4
LT
2551 /*
2552 * Enumerate devices in the ACPI namespace.
2553 */
0cd6ac52
RW
2554 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2555 if (result)
3757b948 2556 goto out;
1da177e4 2557
0cd6ac52 2558 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
1da177e4 2559 if (result)
3757b948 2560 goto out;
1da177e4 2561
2650ef42
AL
2562 /* Fixed feature devices do not exist on HW-reduced platform */
2563 if (!acpi_gbl_reduced_hardware) {
2564 result = acpi_bus_scan_fixed();
2565 if (result) {
2566 acpi_detach_data(acpi_root->handle,
2567 acpi_scan_drop_device);
2568 acpi_device_del(acpi_root);
2569 put_device(&acpi_root->dev);
2570 goto out;
2571 }
b8bd759a 2572 }
1da177e4 2573
b8bd759a 2574 acpi_update_all_gpes();
3757b948
RW
2575
2576 out:
2577 mutex_unlock(&acpi_scan_lock);
2578 return result;
1da177e4 2579}