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