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