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