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