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