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