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