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