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