]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/acpi/scan.c
iwlwifi: don't include iwl-dev.h from iwl-devtrace.h
[net-next-2.6.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>
9b6d97b6 7#include <linux/kernel.h>
1da177e4 8#include <linux/acpi.h>
74523c90
AK
9#include <linux/signal.h>
10#include <linux/kthread.h>
222e82ac 11#include <linux/dmi.h>
1da177e4
LT
12
13#include <acpi/acpi_drivers.h>
1da177e4 14
e60cc7a6
BH
15#include "internal.h"
16
1da177e4 17#define _COMPONENT ACPI_BUS_COMPONENT
f52fd66d 18ACPI_MODULE_NAME("scan");
1da177e4 19#define STRUCT_TO_INT(s) (*((int*)&s))
4be44fcd 20extern struct acpi_device *acpi_root;
1da177e4
LT
21
22#define ACPI_BUS_CLASS "system_bus"
29b71a1c 23#define ACPI_BUS_HID "LNXSYBUS"
1da177e4
LT
24#define ACPI_BUS_DEVICE_NAME "System Bus"
25
859ac9a4
BH
26#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
27
1da177e4 28static LIST_HEAD(acpi_device_list);
e49bd2dd 29static LIST_HEAD(acpi_bus_id_list);
9090589d 30DEFINE_MUTEX(acpi_device_lock);
1da177e4
LT
31LIST_HEAD(acpi_wakeup_device_list);
32
e49bd2dd 33struct acpi_device_bus_id{
bb095854 34 char bus_id[15];
e49bd2dd
ZR
35 unsigned int instance_no;
36 struct list_head node;
1da177e4 37};
29b71a1c
TR
38
39/*
40 * Creates hid/cid(s) string needed for modalias and uevent
41 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
42 * char *modalias: "acpi:IBM0001:ACPI0001"
43*/
b3e572d2
AB
44static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
45 int size)
46{
29b71a1c 47 int len;
5c9fcb5d 48 int count;
7f47fa6c 49 struct acpi_hardware_id *id;
29b71a1c 50
5c9fcb5d 51 len = snprintf(modalias, size, "acpi:");
29b71a1c
TR
52 size -= len;
53
7f47fa6c
BH
54 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
55 count = snprintf(&modalias[len], size, "%s:", id->id);
5c9fcb5d
ZR
56 if (count < 0 || count >= size)
57 return -EINVAL;
58 len += count;
59 size -= count;
60 }
61
29b71a1c
TR
62 modalias[len] = '\0';
63 return len;
64}
65
66static ssize_t
67acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
68 struct acpi_device *acpi_dev = to_acpi_device(dev);
69 int len;
70
71 /* Device has no HID and no CID or string is >1024 */
72 len = create_modalias(acpi_dev, buf, 1024);
73 if (len <= 0)
74 return 0;
75 buf[len++] = '\n';
76 return len;
77}
78static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
79
c8d72a5e 80static void acpi_bus_hot_remove_device(void *context)
1da177e4 81{
26d46867
ZR
82 struct acpi_device *device;
83 acpi_handle handle = context;
1da177e4
LT
84 struct acpi_object_list arg_list;
85 union acpi_object arg;
86 acpi_status status = AE_OK;
1da177e4 87
26d46867 88 if (acpi_bus_get_device(handle, &device))
c8d72a5e 89 return;
26d46867
ZR
90
91 if (!device)
c8d72a5e 92 return;
26d46867
ZR
93
94 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
0794469d 95 "Hot-removing device %s...\n", dev_name(&device->dev)));
26d46867
ZR
96
97 if (acpi_bus_trim(device, 1)) {
55ac9a01
LM
98 printk(KERN_ERR PREFIX
99 "Removing device failed\n");
c8d72a5e 100 return;
26d46867 101 }
1da177e4 102
26d46867
ZR
103 /* power off device */
104 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
105 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
55ac9a01
LM
106 printk(KERN_WARNING PREFIX
107 "Power-off device failed\n");
26d46867
ZR
108
109 if (device->flags.lockable) {
1da177e4
LT
110 arg_list.count = 1;
111 arg_list.pointer = &arg;
112 arg.type = ACPI_TYPE_INTEGER;
113 arg.integer.value = 0;
114 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
115 }
1da177e4 116
1da177e4
LT
117 arg_list.count = 1;
118 arg_list.pointer = &arg;
119 arg.type = ACPI_TYPE_INTEGER;
120 arg.integer.value = 1;
1da177e4 121
1da177e4
LT
122 /*
123 * TBD: _EJD support.
124 */
1da177e4 125 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
26d46867 126 if (ACPI_FAILURE(status))
c8d72a5e
ZR
127 printk(KERN_WARNING PREFIX
128 "Eject device failed\n");
1da177e4 129
c8d72a5e 130 return;
1da177e4
LT
131}
132
1da177e4 133static ssize_t
f883d9db
PM
134acpi_eject_store(struct device *d, struct device_attribute *attr,
135 const char *buf, size_t count)
1da177e4 136{
4be44fcd 137 int ret = count;
4be44fcd 138 acpi_status status;
4be44fcd 139 acpi_object_type type = 0;
f883d9db 140 struct acpi_device *acpi_device = to_acpi_device(d);
1da177e4 141
1da177e4
LT
142 if ((!count) || (buf[0] != '1')) {
143 return -EINVAL;
144 }
1da177e4 145#ifndef FORCE_EJECT
f883d9db 146 if (acpi_device->driver == NULL) {
1da177e4
LT
147 ret = -ENODEV;
148 goto err;
149 }
150#endif
f883d9db
PM
151 status = acpi_get_type(acpi_device->handle, &type);
152 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
1da177e4
LT
153 ret = -ENODEV;
154 goto err;
155 }
1da177e4 156
c8d72a5e 157 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
74523c90 158err:
1da177e4 159 return ret;
1da177e4
LT
160}
161
f883d9db 162static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
1da177e4 163
e49bd2dd
ZR
164static ssize_t
165acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
166 struct acpi_device *acpi_dev = to_acpi_device(dev);
1da177e4 167
ea8d82fd 168 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
1da177e4 169}
e49bd2dd 170static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
1da177e4 171
e49bd2dd
ZR
172static ssize_t
173acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
174 struct acpi_device *acpi_dev = to_acpi_device(dev);
175 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
176 int result;
1da177e4 177
e49bd2dd 178 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
0c526d96 179 if (result)
e49bd2dd 180 goto end;
1da177e4 181
e49bd2dd
ZR
182 result = sprintf(buf, "%s\n", (char*)path.pointer);
183 kfree(path.pointer);
0c526d96 184end:
e49bd2dd 185 return result;
1da177e4 186}
e49bd2dd 187static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
1da177e4 188
e49bd2dd 189static int acpi_device_setup_files(struct acpi_device *dev)
1da177e4 190{
f883d9db
PM
191 acpi_status status;
192 acpi_handle temp;
e49bd2dd 193 int result = 0;
1da177e4
LT
194
195 /*
e49bd2dd 196 * Devices gotten from FADT don't have a "path" attribute
1da177e4 197 */
0c526d96 198 if (dev->handle) {
e49bd2dd 199 result = device_create_file(&dev->dev, &dev_attr_path);
0c526d96 200 if (result)
e49bd2dd 201 goto end;
1da177e4
LT
202 }
203
1131b938
BH
204 result = device_create_file(&dev->dev, &dev_attr_hid);
205 if (result)
206 goto end;
1da177e4 207
1131b938
BH
208 result = device_create_file(&dev->dev, &dev_attr_modalias);
209 if (result)
210 goto end;
29b71a1c 211
e49bd2dd
ZR
212 /*
213 * If device has _EJ0, 'eject' file is created that is used to trigger
214 * hot-removal function from userland.
215 */
f883d9db
PM
216 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
217 if (ACPI_SUCCESS(status))
e49bd2dd 218 result = device_create_file(&dev->dev, &dev_attr_eject);
0c526d96 219end:
e49bd2dd 220 return result;
1da177e4
LT
221}
222
f883d9db 223static void acpi_device_remove_files(struct acpi_device *dev)
1da177e4 224{
f883d9db
PM
225 acpi_status status;
226 acpi_handle temp;
1da177e4 227
f883d9db
PM
228 /*
229 * If device has _EJ0, 'eject' file is created that is used to trigger
230 * hot-removal function from userland.
231 */
232 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
233 if (ACPI_SUCCESS(status))
234 device_remove_file(&dev->dev, &dev_attr_eject);
1da177e4 235
1131b938
BH
236 device_remove_file(&dev->dev, &dev_attr_modalias);
237 device_remove_file(&dev->dev, &dev_attr_hid);
0c526d96 238 if (dev->handle)
e49bd2dd 239 device_remove_file(&dev->dev, &dev_attr_path);
1da177e4 240}
1da177e4 241/* --------------------------------------------------------------------------
9e89dde2 242 ACPI Bus operations
1da177e4 243 -------------------------------------------------------------------------- */
29b71a1c
TR
244
245int acpi_match_device_ids(struct acpi_device *device,
246 const struct acpi_device_id *ids)
247{
248 const struct acpi_device_id *id;
7f47fa6c 249 struct acpi_hardware_id *hwid;
29b71a1c 250
39a0ad87
ZY
251 /*
252 * If the device is not present, it is unnecessary to load device
253 * driver for it.
254 */
255 if (!device->status.present)
256 return -ENODEV;
257
7f47fa6c
BH
258 for (id = ids; id->id[0]; id++)
259 list_for_each_entry(hwid, &device->pnp.ids, list)
260 if (!strcmp((char *) id->id, hwid->id))
29b71a1c 261 return 0;
29b71a1c
TR
262
263 return -ENOENT;
264}
265EXPORT_SYMBOL(acpi_match_device_ids);
266
7f47fa6c
BH
267static void acpi_free_ids(struct acpi_device *device)
268{
269 struct acpi_hardware_id *id, *tmp;
270
271 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
272 kfree(id->id);
273 kfree(id);
274 }
275}
276
1890a97a 277static void acpi_device_release(struct device *dev)
1da177e4 278{
1890a97a 279 struct acpi_device *acpi_dev = to_acpi_device(dev);
1da177e4 280
7f47fa6c 281 acpi_free_ids(acpi_dev);
1890a97a 282 kfree(acpi_dev);
1da177e4
LT
283}
284
5d9464a4 285static int acpi_device_suspend(struct device *dev, pm_message_t state)
1da177e4 286{
5d9464a4
PM
287 struct acpi_device *acpi_dev = to_acpi_device(dev);
288 struct acpi_driver *acpi_drv = acpi_dev->driver;
1da177e4 289
5d9464a4
PM
290 if (acpi_drv && acpi_drv->ops.suspend)
291 return acpi_drv->ops.suspend(acpi_dev, state);
1da177e4
LT
292 return 0;
293}
1da177e4 294
5d9464a4 295static int acpi_device_resume(struct device *dev)
9e89dde2 296{
5d9464a4
PM
297 struct acpi_device *acpi_dev = to_acpi_device(dev);
298 struct acpi_driver *acpi_drv = acpi_dev->driver;
1da177e4 299
5d9464a4
PM
300 if (acpi_drv && acpi_drv->ops.resume)
301 return acpi_drv->ops.resume(acpi_dev);
d550d98d 302 return 0;
1da177e4
LT
303}
304
5d9464a4 305static int acpi_bus_match(struct device *dev, struct device_driver *drv)
9e89dde2 306{
5d9464a4
PM
307 struct acpi_device *acpi_dev = to_acpi_device(dev);
308 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
1da177e4 309
29b71a1c 310 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
5d9464a4 311}
1da177e4 312
7eff2e7a 313static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
1da177e4 314{
5d9464a4 315 struct acpi_device *acpi_dev = to_acpi_device(dev);
7eff2e7a 316 int len;
5d9464a4 317
7eff2e7a
KS
318 if (add_uevent_var(env, "MODALIAS="))
319 return -ENOMEM;
320 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
321 sizeof(env->buf) - env->buflen);
322 if (len >= (sizeof(env->buf) - env->buflen))
323 return -ENOMEM;
324 env->buflen += len;
9e89dde2 325 return 0;
1da177e4
LT
326}
327
46ec8598
BH
328static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
329{
330 struct acpi_device *device = data;
331
332 device->driver->ops.notify(device, event);
333}
334
335static acpi_status acpi_device_notify_fixed(void *data)
336{
337 struct acpi_device *device = data;
338
53de5356
BH
339 /* Fixed hardware devices have no handles */
340 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
46ec8598
BH
341 return AE_OK;
342}
343
344static int acpi_device_install_notify_handler(struct acpi_device *device)
345{
346 acpi_status status;
46ec8598 347
ccba2a36 348 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
46ec8598
BH
349 status =
350 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
351 acpi_device_notify_fixed,
352 device);
ccba2a36 353 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
46ec8598
BH
354 status =
355 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
356 acpi_device_notify_fixed,
357 device);
358 else
359 status = acpi_install_notify_handler(device->handle,
360 ACPI_DEVICE_NOTIFY,
361 acpi_device_notify,
362 device);
363
364 if (ACPI_FAILURE(status))
365 return -EINVAL;
366 return 0;
367}
368
369static void acpi_device_remove_notify_handler(struct acpi_device *device)
370{
ccba2a36 371 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
46ec8598
BH
372 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
373 acpi_device_notify_fixed);
ccba2a36 374 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
46ec8598
BH
375 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
376 acpi_device_notify_fixed);
377 else
378 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
379 acpi_device_notify);
380}
381
5d9464a4
PM
382static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
383static int acpi_start_single_object(struct acpi_device *);
384static int acpi_device_probe(struct device * dev)
1da177e4 385{
5d9464a4
PM
386 struct acpi_device *acpi_dev = to_acpi_device(dev);
387 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
388 int ret;
389
390 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
391 if (!ret) {
c4168bff
LS
392 if (acpi_dev->bus_ops.acpi_op_start)
393 acpi_start_single_object(acpi_dev);
46ec8598
BH
394
395 if (acpi_drv->ops.notify) {
396 ret = acpi_device_install_notify_handler(acpi_dev);
397 if (ret) {
46ec8598
BH
398 if (acpi_drv->ops.remove)
399 acpi_drv->ops.remove(acpi_dev,
400 acpi_dev->removal_type);
401 return ret;
402 }
403 }
404
5d9464a4
PM
405 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
406 "Found driver [%s] for device [%s]\n",
407 acpi_drv->name, acpi_dev->pnp.bus_id));
408 get_device(dev);
409 }
410 return ret;
411}
1da177e4 412
5d9464a4
PM
413static int acpi_device_remove(struct device * dev)
414{
415 struct acpi_device *acpi_dev = to_acpi_device(dev);
416 struct acpi_driver *acpi_drv = acpi_dev->driver;
417
418 if (acpi_drv) {
46ec8598
BH
419 if (acpi_drv->ops.notify)
420 acpi_device_remove_notify_handler(acpi_dev);
5d9464a4 421 if (acpi_drv->ops.remove)
96333578 422 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
1da177e4 423 }
5d9464a4 424 acpi_dev->driver = NULL;
db89b4f0 425 acpi_dev->driver_data = NULL;
1da177e4 426
5d9464a4 427 put_device(dev);
9e89dde2
ZR
428 return 0;
429}
1da177e4 430
55955aad 431struct bus_type acpi_bus_type = {
9e89dde2
ZR
432 .name = "acpi",
433 .suspend = acpi_device_suspend,
434 .resume = acpi_device_resume,
5d9464a4
PM
435 .match = acpi_bus_match,
436 .probe = acpi_device_probe,
437 .remove = acpi_device_remove,
438 .uevent = acpi_device_uevent,
9e89dde2
ZR
439};
440
66b7ed40 441static int acpi_device_register(struct acpi_device *device)
1da177e4 442{
4be44fcd 443 int result;
e49bd2dd
ZR
444 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
445 int found = 0;
66b7ed40 446
9e89dde2
ZR
447 /*
448 * Linkage
449 * -------
450 * Link this device to its parent and siblings.
451 */
452 INIT_LIST_HEAD(&device->children);
453 INIT_LIST_HEAD(&device->node);
9e89dde2 454 INIT_LIST_HEAD(&device->wakeup_list);
1da177e4 455
e49bd2dd
ZR
456 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
457 if (!new_bus_id) {
458 printk(KERN_ERR PREFIX "Memory allocation error\n");
459 return -ENOMEM;
1da177e4 460 }
e49bd2dd 461
9090589d 462 mutex_lock(&acpi_device_lock);
e49bd2dd
ZR
463 /*
464 * Find suitable bus_id and instance number in acpi_bus_id_list
465 * If failed, create one and link it into acpi_bus_id_list
466 */
467 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
1131b938
BH
468 if (!strcmp(acpi_device_bus_id->bus_id,
469 acpi_device_hid(device))) {
470 acpi_device_bus_id->instance_no++;
e49bd2dd
ZR
471 found = 1;
472 kfree(new_bus_id);
473 break;
474 }
1da177e4 475 }
0c526d96 476 if (!found) {
e49bd2dd 477 acpi_device_bus_id = new_bus_id;
1131b938 478 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
e49bd2dd
ZR
479 acpi_device_bus_id->instance_no = 0;
480 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1da177e4 481 }
0794469d 482 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
1da177e4 483
33b57150 484 if (device->parent)
9e89dde2 485 list_add_tail(&device->node, &device->parent->children);
33b57150 486
9e89dde2
ZR
487 if (device->wakeup.flags.valid)
488 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
9090589d 489 mutex_unlock(&acpi_device_lock);
1da177e4 490
1890a97a 491 if (device->parent)
66b7ed40 492 device->dev.parent = &device->parent->dev;
1890a97a 493 device->dev.bus = &acpi_bus_type;
1890a97a 494 device->dev.release = &acpi_device_release;
8b12b922 495 result = device_register(&device->dev);
0c526d96 496 if (result) {
8b12b922 497 dev_err(&device->dev, "Error registering device\n");
e49bd2dd 498 goto end;
1da177e4 499 }
1da177e4 500
e49bd2dd 501 result = acpi_device_setup_files(device);
0c526d96 502 if (result)
0794469d
KS
503 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
504 dev_name(&device->dev));
1da177e4 505
96333578 506 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
1da177e4 507 return 0;
0c526d96 508end:
9090589d 509 mutex_lock(&acpi_device_lock);
33b57150 510 if (device->parent)
e49bd2dd 511 list_del(&device->node);
e49bd2dd 512 list_del(&device->wakeup_list);
9090589d 513 mutex_unlock(&acpi_device_lock);
e49bd2dd 514 return result;
1da177e4
LT
515}
516
9e89dde2
ZR
517static void acpi_device_unregister(struct acpi_device *device, int type)
518{
9090589d 519 mutex_lock(&acpi_device_lock);
33b57150 520 if (device->parent)
9e89dde2 521 list_del(&device->node);
1da177e4 522
9e89dde2 523 list_del(&device->wakeup_list);
9090589d 524 mutex_unlock(&acpi_device_lock);
1da177e4 525
9e89dde2 526 acpi_detach_data(device->handle, acpi_bus_data_handler);
1890a97a 527
f883d9db 528 acpi_device_remove_files(device);
1890a97a 529 device_unregister(&device->dev);
1da177e4
LT
530}
531
9e89dde2
ZR
532/* --------------------------------------------------------------------------
533 Driver Management
534 -------------------------------------------------------------------------- */
1da177e4 535/**
d758a8fa
RD
536 * acpi_bus_driver_init - add a device to a driver
537 * @device: the device to add and initialize
538 * @driver: driver for the device
539 *
0c526d96 540 * Used to initialize a device via its device driver. Called whenever a
1890a97a 541 * driver is bound to a device. Invokes the driver's add() ops.
1da177e4
LT
542 */
543static int
4be44fcd 544acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
1da177e4 545{
4be44fcd 546 int result = 0;
1da177e4 547
1da177e4 548 if (!device || !driver)
d550d98d 549 return -EINVAL;
1da177e4
LT
550
551 if (!driver->ops.add)
d550d98d 552 return -ENOSYS;
1da177e4
LT
553
554 result = driver->ops.add(device);
555 if (result) {
556 device->driver = NULL;
db89b4f0 557 device->driver_data = NULL;
d550d98d 558 return result;
1da177e4
LT
559 }
560
561 device->driver = driver;
562
563 /*
564 * TBD - Configuration Management: Assign resources to device based
565 * upon possible configuration and currently allocated resources.
566 */
567
4be44fcd
LB
568 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
569 "Driver successfully bound to device\n"));
d550d98d 570 return 0;
3fb02738
RS
571}
572
8713cbef 573static int acpi_start_single_object(struct acpi_device *device)
3fb02738
RS
574{
575 int result = 0;
576 struct acpi_driver *driver;
577
3fb02738
RS
578
579 if (!(driver = device->driver))
d550d98d 580 return 0;
3fb02738 581
1da177e4
LT
582 if (driver->ops.start) {
583 result = driver->ops.start(device);
584 if (result && driver->ops.remove)
585 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
1da177e4
LT
586 }
587
d550d98d 588 return result;
1da177e4
LT
589}
590
9e89dde2
ZR
591/**
592 * acpi_bus_register_driver - register a driver with the ACPI bus
593 * @driver: driver being registered
594 *
595 * Registers a driver with the ACPI bus. Searches the namespace for all
596 * devices that match the driver's criteria and binds. Returns zero for
597 * success or a negative error status for failure.
598 */
599int acpi_bus_register_driver(struct acpi_driver *driver)
1da177e4 600{
1890a97a 601 int ret;
1da177e4 602
9e89dde2
ZR
603 if (acpi_disabled)
604 return -ENODEV;
1890a97a
PM
605 driver->drv.name = driver->name;
606 driver->drv.bus = &acpi_bus_type;
607 driver->drv.owner = driver->owner;
1da177e4 608
1890a97a
PM
609 ret = driver_register(&driver->drv);
610 return ret;
9e89dde2 611}
1da177e4 612
9e89dde2
ZR
613EXPORT_SYMBOL(acpi_bus_register_driver);
614
615/**
616 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
617 * @driver: driver to unregister
618 *
619 * Unregisters a driver with the ACPI bus. Searches the namespace for all
620 * devices that match the driver's criteria and unbinds.
621 */
622void acpi_bus_unregister_driver(struct acpi_driver *driver)
623{
1890a97a 624 driver_unregister(&driver->drv);
9e89dde2
ZR
625}
626
627EXPORT_SYMBOL(acpi_bus_unregister_driver);
628
9e89dde2
ZR
629/* --------------------------------------------------------------------------
630 Device Enumeration
631 -------------------------------------------------------------------------- */
5c478f49
BH
632static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
633{
634 acpi_status status;
635 int ret;
636 struct acpi_device *device;
637
638 /*
639 * Fixed hardware devices do not appear in the namespace and do not
640 * have handles, but we fabricate acpi_devices for them, so we have
641 * to deal with them specially.
642 */
643 if (handle == NULL)
644 return acpi_root;
645
646 do {
647 status = acpi_get_parent(handle, &handle);
648 if (status == AE_NULL_ENTRY)
649 return NULL;
650 if (ACPI_FAILURE(status))
651 return acpi_root;
652
653 ret = acpi_bus_get_device(handle, &device);
654 if (ret == 0)
655 return device;
656 } while (1);
657}
658
9e89dde2
ZR
659acpi_status
660acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
661{
662 acpi_status status;
663 acpi_handle tmp;
664 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
665 union acpi_object *obj;
666
667 status = acpi_get_handle(handle, "_EJD", &tmp);
668 if (ACPI_FAILURE(status))
669 return status;
670
671 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
672 if (ACPI_SUCCESS(status)) {
673 obj = buffer.pointer;
3b5fee59
HM
674 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
675 ejd);
9e89dde2
ZR
676 kfree(buffer.pointer);
677 }
678 return status;
679}
680EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
681
8e4319c4 682void acpi_bus_data_handler(acpi_handle handle, void *context)
9e89dde2
ZR
683{
684
685 /* TBD */
686
687 return;
688}
689
9e89dde2
ZR
690static int acpi_bus_get_perf_flags(struct acpi_device *device)
691{
692 device->performance.state = ACPI_STATE_UNKNOWN;
693 return 0;
694}
695
696static acpi_status
697acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
698 union acpi_object *package)
699{
700 int i = 0;
701 union acpi_object *element = NULL;
702
703 if (!device || !package || (package->package.count < 2))
704 return AE_BAD_PARAMETER;
705
706 element = &(package->package.elements[0]);
707 if (!element)
708 return AE_BAD_PARAMETER;
709 if (element->type == ACPI_TYPE_PACKAGE) {
710 if ((element->package.count < 2) ||
711 (element->package.elements[0].type !=
712 ACPI_TYPE_LOCAL_REFERENCE)
713 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
714 return AE_BAD_DATA;
715 device->wakeup.gpe_device =
716 element->package.elements[0].reference.handle;
717 device->wakeup.gpe_number =
718 (u32) element->package.elements[1].integer.value;
719 } else if (element->type == ACPI_TYPE_INTEGER) {
720 device->wakeup.gpe_number = element->integer.value;
721 } else
722 return AE_BAD_DATA;
723
724 element = &(package->package.elements[1]);
725 if (element->type != ACPI_TYPE_INTEGER) {
726 return AE_BAD_DATA;
727 }
728 device->wakeup.sleep_state = element->integer.value;
729
730 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
731 return AE_NO_MEMORY;
732 }
733 device->wakeup.resources.count = package->package.count - 2;
734 for (i = 0; i < device->wakeup.resources.count; i++) {
735 element = &(package->package.elements[i + 2]);
cd0b2248 736 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
9e89dde2 737 return AE_BAD_DATA;
9e89dde2
ZR
738
739 device->wakeup.resources.handles[i] = element->reference.handle;
1da177e4 740 }
9e89dde2
ZR
741
742 return AE_OK;
1da177e4
LT
743}
744
f517709d 745static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
1da177e4 746{
29b71a1c
TR
747 struct acpi_device_id button_device_ids[] = {
748 {"PNP0C0D", 0},
749 {"PNP0C0C", 0},
750 {"PNP0C0E", 0},
751 {"", 0},
752 };
f517709d
RW
753 acpi_status status;
754 acpi_event_status event_status;
755
756 device->wakeup.run_wake_count = 0;
b67ea761 757 device->wakeup.flags.notifier_present = 0;
f517709d
RW
758
759 /* Power button, Lid switch always enable wakeup */
760 if (!acpi_match_device_ids(device, button_device_ids)) {
761 device->wakeup.flags.run_wake = 1;
762 device->wakeup.flags.always_enabled = 1;
763 return;
764 }
765
766 status = acpi_get_gpe_status(NULL, device->wakeup.gpe_number,
767 ACPI_NOT_ISR, &event_status);
768 if (status == AE_OK)
769 device->wakeup.flags.run_wake =
770 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
771}
772
773static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
774{
775 acpi_status status = 0;
776 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
777 union acpi_object *package = NULL;
778 int psw_error;
29b71a1c 779
9e89dde2
ZR
780 /* _PRW */
781 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
782 if (ACPI_FAILURE(status)) {
783 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
784 goto end;
1da177e4 785 }
1da177e4 786
9e89dde2
ZR
787 package = (union acpi_object *)buffer.pointer;
788 status = acpi_bus_extract_wakeup_device_power_package(device, package);
789 if (ACPI_FAILURE(status)) {
790 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
791 goto end;
792 }
1da177e4 793
9e89dde2 794 kfree(buffer.pointer);
1da177e4 795
9e89dde2 796 device->wakeup.flags.valid = 1;
9b83ccd2 797 device->wakeup.prepare_count = 0;
f517709d 798 acpi_bus_set_run_wake_flags(device);
729b2bdb
ZY
799 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
800 * system for the ACPI device with the _PRW object.
801 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
802 * So it is necessary to call _DSW object first. Only when it is not
803 * present will the _PSW object used.
804 */
77e76609
RW
805 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
806 if (psw_error)
807 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
808 "error in _DSW or _PSW evaluation\n"));
809
0c526d96 810end:
9e89dde2
ZR
811 if (ACPI_FAILURE(status))
812 device->flags.wake_capable = 0;
d550d98d 813 return 0;
1da177e4 814}
1da177e4 815
9e89dde2 816static int acpi_bus_get_power_flags(struct acpi_device *device)
1da177e4 817{
9e89dde2
ZR
818 acpi_status status = 0;
819 acpi_handle handle = NULL;
820 u32 i = 0;
1a365616 821
4be44fcd 822
9e89dde2
ZR
823 /*
824 * Power Management Flags
825 */
826 status = acpi_get_handle(device->handle, "_PSC", &handle);
827 if (ACPI_SUCCESS(status))
828 device->power.flags.explicit_get = 1;
829 status = acpi_get_handle(device->handle, "_IRC", &handle);
830 if (ACPI_SUCCESS(status))
831 device->power.flags.inrush_current = 1;
1da177e4 832
9e89dde2
ZR
833 /*
834 * Enumerate supported power management states
835 */
836 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
837 struct acpi_device_power_state *ps = &device->power.states[i];
838 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
1da177e4 839
9e89dde2
ZR
840 /* Evaluate "_PRx" to se if power resources are referenced */
841 acpi_evaluate_reference(device->handle, object_name, NULL,
842 &ps->resources);
843 if (ps->resources.count) {
844 device->power.flags.power_resources = 1;
845 ps->flags.valid = 1;
1da177e4 846 }
1da177e4 847
9e89dde2
ZR
848 /* Evaluate "_PSx" to see if we can do explicit sets */
849 object_name[2] = 'S';
850 status = acpi_get_handle(device->handle, object_name, &handle);
851 if (ACPI_SUCCESS(status)) {
852 ps->flags.explicit_set = 1;
853 ps->flags.valid = 1;
1da177e4 854 }
1da177e4 855
9e89dde2
ZR
856 /* State is valid if we have some power control */
857 if (ps->resources.count || ps->flags.explicit_set)
858 ps->flags.valid = 1;
1da177e4 859
9e89dde2
ZR
860 ps->power = -1; /* Unknown - driver assigned */
861 ps->latency = -1; /* Unknown - driver assigned */
862 }
1da177e4 863
9e89dde2
ZR
864 /* Set defaults for D0 and D3 states (always valid) */
865 device->power.states[ACPI_STATE_D0].flags.valid = 1;
866 device->power.states[ACPI_STATE_D0].power = 100;
867 device->power.states[ACPI_STATE_D3].flags.valid = 1;
868 device->power.states[ACPI_STATE_D3].power = 0;
c8f7a62c 869
9e89dde2 870 /* TBD: System wake support and resource requirements. */
c8f7a62c 871
9e89dde2 872 device->power.state = ACPI_STATE_UNKNOWN;
a51e145f 873 acpi_bus_get_power(device->handle, &(device->power.state));
c8f7a62c 874
9e89dde2
ZR
875 return 0;
876}
c8f7a62c 877
4be44fcd 878static int acpi_bus_get_flags(struct acpi_device *device)
1da177e4 879{
4be44fcd
LB
880 acpi_status status = AE_OK;
881 acpi_handle temp = NULL;
1da177e4 882
1da177e4
LT
883
884 /* Presence of _STA indicates 'dynamic_status' */
885 status = acpi_get_handle(device->handle, "_STA", &temp);
886 if (ACPI_SUCCESS(status))
887 device->flags.dynamic_status = 1;
888
1da177e4
LT
889 /* Presence of _RMV indicates 'removable' */
890 status = acpi_get_handle(device->handle, "_RMV", &temp);
891 if (ACPI_SUCCESS(status))
892 device->flags.removable = 1;
893
894 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
895 status = acpi_get_handle(device->handle, "_EJD", &temp);
896 if (ACPI_SUCCESS(status))
897 device->flags.ejectable = 1;
898 else {
899 status = acpi_get_handle(device->handle, "_EJ0", &temp);
900 if (ACPI_SUCCESS(status))
901 device->flags.ejectable = 1;
902 }
903
904 /* Presence of _LCK indicates 'lockable' */
905 status = acpi_get_handle(device->handle, "_LCK", &temp);
906 if (ACPI_SUCCESS(status))
907 device->flags.lockable = 1;
908
909 /* Presence of _PS0|_PR0 indicates 'power manageable' */
910 status = acpi_get_handle(device->handle, "_PS0", &temp);
911 if (ACPI_FAILURE(status))
912 status = acpi_get_handle(device->handle, "_PR0", &temp);
913 if (ACPI_SUCCESS(status))
914 device->flags.power_manageable = 1;
915
916 /* Presence of _PRW indicates wake capable */
917 status = acpi_get_handle(device->handle, "_PRW", &temp);
918 if (ACPI_SUCCESS(status))
919 device->flags.wake_capable = 1;
920
3c5f9be4 921 /* TBD: Performance management */
1da177e4 922
d550d98d 923 return 0;
1da177e4
LT
924}
925
c7bcb4e9 926static void acpi_device_get_busid(struct acpi_device *device)
1da177e4 927{
4be44fcd
LB
928 char bus_id[5] = { '?', 0 };
929 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
930 int i = 0;
1da177e4
LT
931
932 /*
933 * Bus ID
934 * ------
935 * The device's Bus ID is simply the object name.
936 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
937 */
859ac9a4 938 if (ACPI_IS_ROOT_DEVICE(device)) {
1da177e4 939 strcpy(device->pnp.bus_id, "ACPI");
859ac9a4
BH
940 return;
941 }
942
943 switch (device->device_type) {
1da177e4
LT
944 case ACPI_BUS_TYPE_POWER_BUTTON:
945 strcpy(device->pnp.bus_id, "PWRF");
946 break;
947 case ACPI_BUS_TYPE_SLEEP_BUTTON:
948 strcpy(device->pnp.bus_id, "SLPF");
949 break;
950 default:
66b7ed40 951 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1da177e4
LT
952 /* Clean up trailing underscores (if any) */
953 for (i = 3; i > 1; i--) {
954 if (bus_id[i] == '_')
955 bus_id[i] = '\0';
956 else
957 break;
958 }
959 strcpy(device->pnp.bus_id, bus_id);
960 break;
961 }
962}
963
54735266
ZR
964/*
965 * acpi_bay_match - see if a device is an ejectable driver bay
966 *
967 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
968 * then we can safely call it an ejectable drive bay
969 */
970static int acpi_bay_match(struct acpi_device *device){
971 acpi_status status;
972 acpi_handle handle;
973 acpi_handle tmp;
974 acpi_handle phandle;
975
976 handle = device->handle;
977
978 status = acpi_get_handle(handle, "_EJ0", &tmp);
979 if (ACPI_FAILURE(status))
980 return -ENODEV;
981
982 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
983 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
984 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
985 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
986 return 0;
987
988 if (acpi_get_parent(handle, &phandle))
989 return -ENODEV;
990
991 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
992 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
993 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
994 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
995 return 0;
996
997 return -ENODEV;
998}
999
3620f2f2
FS
1000/*
1001 * acpi_dock_match - see if a device has a _DCK method
1002 */
1003static int acpi_dock_match(struct acpi_device *device)
1004{
1005 acpi_handle tmp;
1006 return acpi_get_handle(device->handle, "_DCK", &tmp);
1007}
1008
7f47fa6c 1009char *acpi_device_hid(struct acpi_device *device)
15b8dd53 1010{
7f47fa6c 1011 struct acpi_hardware_id *hid;
15b8dd53 1012
7f47fa6c
BH
1013 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1014 return hid->id;
1015}
1016EXPORT_SYMBOL(acpi_device_hid);
15b8dd53 1017
7f47fa6c
BH
1018static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1019{
1020 struct acpi_hardware_id *id;
15b8dd53 1021
7f47fa6c
BH
1022 id = kmalloc(sizeof(*id), GFP_KERNEL);
1023 if (!id)
1024 return;
15b8dd53 1025
7f47fa6c
BH
1026 id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
1027 if (!id->id) {
1028 kfree(id);
1029 return;
15b8dd53
BM
1030 }
1031
7f47fa6c
BH
1032 strcpy(id->id, dev_id);
1033 list_add_tail(&id->list, &device->pnp.ids);
15b8dd53
BM
1034}
1035
222e82ac
DW
1036/*
1037 * Old IBM workstations have a DSDT bug wherein the SMBus object
1038 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1039 * prefix. Work around this.
1040 */
1041static int acpi_ibm_smbus_match(struct acpi_device *device)
1042{
1043 acpi_handle h_dummy;
1044 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1045 int result;
1046
1047 if (!dmi_name_in_vendors("IBM"))
1048 return -ENODEV;
1049
1050 /* Look for SMBS object */
1051 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1052 if (result)
1053 return result;
1054
1055 if (strcmp("SMBS", path.pointer)) {
1056 result = -ENODEV;
1057 goto out;
1058 }
1059
1060 /* Does it have the necessary (but misnamed) methods? */
1061 result = -ENODEV;
1062 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1063 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1064 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1065 result = 0;
1066out:
1067 kfree(path.pointer);
1068 return result;
1069}
1070
c7bcb4e9 1071static void acpi_device_set_id(struct acpi_device *device)
1da177e4 1072{
4be44fcd 1073 acpi_status status;
57f3674f
BH
1074 struct acpi_device_info *info;
1075 struct acpica_device_id_list *cid_list;
7f47fa6c 1076 int i;
1da177e4 1077
c7bcb4e9 1078 switch (device->device_type) {
1da177e4 1079 case ACPI_BUS_TYPE_DEVICE:
859ac9a4 1080 if (ACPI_IS_ROOT_DEVICE(device)) {
57f3674f 1081 acpi_add_id(device, ACPI_SYSTEM_HID);
859ac9a4 1082 break;
78b8e141
BH
1083 } else if (ACPI_IS_ROOT_DEVICE(device->parent)) {
1084 /* \_SB_, the only root-level namespace device */
57f3674f 1085 acpi_add_id(device, ACPI_BUS_HID);
78b8e141
BH
1086 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1087 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1088 break;
859ac9a4
BH
1089 }
1090
66b7ed40 1091 status = acpi_get_object_info(device->handle, &info);
1da177e4 1092 if (ACPI_FAILURE(status)) {
96b2dd1f 1093 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1da177e4
LT
1094 return;
1095 }
1096
1da177e4 1097 if (info->valid & ACPI_VALID_HID)
57f3674f
BH
1098 acpi_add_id(device, info->hardware_id.string);
1099 if (info->valid & ACPI_VALID_CID) {
15b8dd53 1100 cid_list = &info->compatible_id_list;
57f3674f
BH
1101 for (i = 0; i < cid_list->count; i++)
1102 acpi_add_id(device, cid_list->ids[i].string);
1103 }
1da177e4
LT
1104 if (info->valid & ACPI_VALID_ADR) {
1105 device->pnp.bus_address = info->address;
1106 device->flags.bus_address = 1;
1107 }
ae843332 1108
a83893ae
BH
1109 kfree(info);
1110
57f3674f
BH
1111 /*
1112 * Some devices don't reliably have _HIDs & _CIDs, so add
1113 * synthetic HIDs to make sure drivers can find them.
1114 */
c3d6de69 1115 if (acpi_is_video_device(device))
57f3674f 1116 acpi_add_id(device, ACPI_VIDEO_HID);
3620f2f2 1117 else if (ACPI_SUCCESS(acpi_bay_match(device)))
57f3674f 1118 acpi_add_id(device, ACPI_BAY_HID);
3620f2f2 1119 else if (ACPI_SUCCESS(acpi_dock_match(device)))
57f3674f 1120 acpi_add_id(device, ACPI_DOCK_HID);
222e82ac
DW
1121 else if (!acpi_ibm_smbus_match(device))
1122 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
54735266 1123
1da177e4
LT
1124 break;
1125 case ACPI_BUS_TYPE_POWER:
57f3674f 1126 acpi_add_id(device, ACPI_POWER_HID);
1da177e4
LT
1127 break;
1128 case ACPI_BUS_TYPE_PROCESSOR:
57f3674f 1129 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1da177e4 1130 break;
1da177e4 1131 case ACPI_BUS_TYPE_THERMAL:
57f3674f 1132 acpi_add_id(device, ACPI_THERMAL_HID);
1da177e4
LT
1133 break;
1134 case ACPI_BUS_TYPE_POWER_BUTTON:
57f3674f 1135 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1da177e4
LT
1136 break;
1137 case ACPI_BUS_TYPE_SLEEP_BUTTON:
57f3674f 1138 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1da177e4
LT
1139 break;
1140 }
1141
b1fbfb2a
BH
1142 /*
1143 * We build acpi_devices for some objects that don't have _HID or _CID,
1144 * e.g., PCI bridges and slots. Drivers can't bind to these objects,
1145 * but we do use them indirectly by traversing the acpi_device tree.
1146 * This generic ID isn't useful for driver binding, but it provides
1147 * the useful property that "every acpi_device has an ID."
1148 */
57f3674f
BH
1149 if (list_empty(&device->pnp.ids))
1150 acpi_add_id(device, "device");
1da177e4
LT
1151}
1152
bc3b0772 1153static int acpi_device_set_context(struct acpi_device *device)
1da177e4 1154{
bc3b0772
BH
1155 acpi_status status;
1156
1da177e4
LT
1157 /*
1158 * Context
1159 * -------
1160 * Attach this 'struct acpi_device' to the ACPI object. This makes
bc3b0772
BH
1161 * resolutions from handle->device very efficient. Fixed hardware
1162 * devices have no handles, so we skip them.
1da177e4 1163 */
bc3b0772
BH
1164 if (!device->handle)
1165 return 0;
1da177e4 1166
bc3b0772
BH
1167 status = acpi_attach_data(device->handle,
1168 acpi_bus_data_handler, device);
1169 if (ACPI_SUCCESS(status))
1170 return 0;
1171
1172 printk(KERN_ERR PREFIX "Error attaching device data\n");
1173 return -ENODEV;
1da177e4
LT
1174}
1175
4be44fcd 1176static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1da177e4 1177{
1da177e4 1178 if (!dev)
d550d98d 1179 return -EINVAL;
1da177e4 1180
96333578 1181 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1890a97a 1182 device_release_driver(&dev->dev);
1da177e4
LT
1183
1184 if (!rmdevice)
d550d98d 1185 return 0;
1da177e4 1186
2786f6e3
RZ
1187 /*
1188 * unbind _ADR-Based Devices when hot removal
1189 */
1da177e4
LT
1190 if (dev->flags.bus_address) {
1191 if ((dev->parent) && (dev->parent->ops.unbind))
1192 dev->parent->ops.unbind(dev);
1193 }
1da177e4
LT
1194 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1195
d550d98d 1196 return 0;
1da177e4
LT
1197}
1198
5c478f49
BH
1199static int acpi_add_single_object(struct acpi_device **child,
1200 acpi_handle handle, int type,
778cbc1d 1201 unsigned long long sta,
5c478f49 1202 struct acpi_bus_ops *ops)
1da177e4 1203{
77c24888
BH
1204 int result;
1205 struct acpi_device *device;
29aaefa6 1206 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1da177e4 1207
36bcbec7 1208 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1da177e4 1209 if (!device) {
6468463a 1210 printk(KERN_ERR PREFIX "Memory allocation error\n");
d550d98d 1211 return -ENOMEM;
1da177e4 1212 }
1da177e4 1213
7f47fa6c 1214 INIT_LIST_HEAD(&device->pnp.ids);
caaa6efb 1215 device->device_type = type;
1da177e4 1216 device->handle = handle;
5c478f49 1217 device->parent = acpi_bus_get_parent(handle);
c4168bff 1218 device->bus_ops = *ops; /* workround for not call .start */
778cbc1d 1219 STRUCT_TO_INT(device->status) = sta;
c4168bff 1220
c7bcb4e9 1221 acpi_device_get_busid(device);
1da177e4
LT
1222
1223 /*
1224 * Flags
1225 * -----
778cbc1d
BH
1226 * Note that we only look for object handles -- cannot evaluate objects
1227 * until we know the device is present and properly initialized.
1da177e4
LT
1228 */
1229 result = acpi_bus_get_flags(device);
1230 if (result)
1231 goto end;
1232
1da177e4
LT
1233 /*
1234 * Initialize Device
1235 * -----------------
1236 * TBD: Synch with Core's enumeration/initialization process.
1237 */
c7bcb4e9 1238 acpi_device_set_id(device);
1da177e4
LT
1239
1240 /*
1241 * Power Management
1242 * ----------------
1243 */
1244 if (device->flags.power_manageable) {
1245 result = acpi_bus_get_power_flags(device);
1246 if (result)
1247 goto end;
1248 }
1249
4be44fcd 1250 /*
1da177e4
LT
1251 * Wakeup device management
1252 *-----------------------
1253 */
1254 if (device->flags.wake_capable) {
1255 result = acpi_bus_get_wakeup_device_flags(device);
1256 if (result)
1257 goto end;
1258 }
1259
1260 /*
1261 * Performance Management
1262 * ----------------------
1263 */
1264 if (device->flags.performance_manageable) {
1265 result = acpi_bus_get_perf_flags(device);
1266 if (result)
1267 goto end;
1268 }
1269
bc3b0772 1270 if ((result = acpi_device_set_context(device)))
f61f9258 1271 goto end;
1da177e4 1272
66b7ed40 1273 result = acpi_device_register(device);
1da177e4
LT
1274
1275 /*
2786f6e3 1276 * Bind _ADR-Based Devices when hot add
1da177e4
LT
1277 */
1278 if (device->flags.bus_address) {
1279 if (device->parent && device->parent->ops.bind)
1280 device->parent->ops.bind(device);
1281 }
1282
0c526d96 1283end:
29aaefa6
BH
1284 if (!result) {
1285 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1286 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1287 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1288 (char *) buffer.pointer,
1289 device->parent ? dev_name(&device->parent->dev) :
1290 "(null)"));
1291 kfree(buffer.pointer);
1da177e4 1292 *child = device;
29aaefa6 1293 } else
718fb0de 1294 acpi_device_release(&device->dev);
1da177e4 1295
d550d98d 1296 return result;
1da177e4 1297}
1da177e4 1298
778cbc1d
BH
1299#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1300 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1301
1302static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1303 unsigned long long *sta)
1da177e4 1304{
778cbc1d
BH
1305 acpi_status status;
1306 acpi_object_type acpi_type;
1da177e4 1307
778cbc1d 1308 status = acpi_get_type(handle, &acpi_type);
51a85faf 1309 if (ACPI_FAILURE(status))
778cbc1d 1310 return -ENODEV;
4be44fcd 1311
778cbc1d 1312 switch (acpi_type) {
51a85faf
BH
1313 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1314 case ACPI_TYPE_DEVICE:
778cbc1d
BH
1315 *type = ACPI_BUS_TYPE_DEVICE;
1316 status = acpi_bus_get_status_handle(handle, sta);
1317 if (ACPI_FAILURE(status))
1318 return -ENODEV;
51a85faf
BH
1319 break;
1320 case ACPI_TYPE_PROCESSOR:
778cbc1d
BH
1321 *type = ACPI_BUS_TYPE_PROCESSOR;
1322 status = acpi_bus_get_status_handle(handle, sta);
1323 if (ACPI_FAILURE(status))
1324 return -ENODEV;
51a85faf
BH
1325 break;
1326 case ACPI_TYPE_THERMAL:
778cbc1d
BH
1327 *type = ACPI_BUS_TYPE_THERMAL;
1328 *sta = ACPI_STA_DEFAULT;
51a85faf
BH
1329 break;
1330 case ACPI_TYPE_POWER:
778cbc1d
BH
1331 *type = ACPI_BUS_TYPE_POWER;
1332 *sta = ACPI_STA_DEFAULT;
51a85faf
BH
1333 break;
1334 default:
778cbc1d 1335 return -ENODEV;
51a85faf 1336 }
1da177e4 1337
778cbc1d
BH
1338 return 0;
1339}
1340
1341static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1342 void *context, void **return_value)
1343{
1344 struct acpi_bus_ops *ops = context;
778cbc1d
BH
1345 int type;
1346 unsigned long long sta;
e3b87f8a
BH
1347 struct acpi_device *device;
1348 acpi_status status;
778cbc1d
BH
1349 int result;
1350
1351 result = acpi_bus_type_and_status(handle, &type, &sta);
1352 if (result)
1353 return AE_OK;
1354
1355 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1356 !(sta & ACPI_STA_DEVICE_FUNCTIONING))
1357 return AE_CTRL_DEPTH;
1358
e3b87f8a
BH
1359 /*
1360 * We may already have an acpi_device from a previous enumeration. If
1361 * so, we needn't add it again, but we may still have to start it.
1362 */
1363 device = NULL;
1364 acpi_bus_get_device(handle, &device);
1365 if (ops->acpi_op_add && !device)
1366 acpi_add_single_object(&device, handle, type, sta, ops);
1da177e4 1367
e3b87f8a 1368 if (!device)
51a85faf 1369 return AE_CTRL_DEPTH;
1da177e4 1370
51a85faf
BH
1371 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1372 status = acpi_start_single_object(device);
1da177e4 1373 if (ACPI_FAILURE(status))
51a85faf
BH
1374 return AE_CTRL_DEPTH;
1375 }
1da177e4 1376
51a85faf
BH
1377 if (!*return_value)
1378 *return_value = device;
1379 return AE_OK;
1380}
3fb02738 1381
51a85faf
BH
1382static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1383 struct acpi_device **child)
1384{
1385 acpi_status status;
51a85faf 1386 void *device = NULL;
3fb02738 1387
51a85faf
BH
1388 status = acpi_bus_check_add(handle, 0, ops, &device);
1389 if (ACPI_SUCCESS(status))
1390 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2263576c 1391 acpi_bus_check_add, NULL, ops, &device);
1da177e4 1392
51a85faf
BH
1393 if (child)
1394 *child = device;
7779688f
TR
1395
1396 if (device)
1397 return 0;
1398 else
1399 return -ENODEV;
1da177e4 1400}
1da177e4 1401
7779688f
TR
1402/*
1403 * acpi_bus_add and acpi_bus_start
1404 *
1405 * scan a given ACPI tree and (probably recently hot-plugged)
1406 * create and add or starts found devices.
1407 *
1408 * If no devices were found -ENODEV is returned which does not
1409 * mean that this is a real error, there just have been no suitable
1410 * ACPI objects in the table trunk from which the kernel could create
1411 * a device and add/start an appropriate driver.
1412 */
1413
3fb02738 1414int
4be44fcd
LB
1415acpi_bus_add(struct acpi_device **child,
1416 struct acpi_device *parent, acpi_handle handle, int type)
3fb02738 1417{
3fb02738
RS
1418 struct acpi_bus_ops ops;
1419
c4168bff
LS
1420 memset(&ops, 0, sizeof(ops));
1421 ops.acpi_op_add = 1;
3fb02738 1422
7779688f 1423 return acpi_bus_scan(handle, &ops, child);
3fb02738
RS
1424}
1425EXPORT_SYMBOL(acpi_bus_add);
1426
4be44fcd 1427int acpi_bus_start(struct acpi_device *device)
3fb02738 1428{
3fb02738
RS
1429 struct acpi_bus_ops ops;
1430
d2f6650a
TR
1431 if (!device)
1432 return -EINVAL;
1433
8e029bf0
BH
1434 memset(&ops, 0, sizeof(ops));
1435 ops.acpi_op_start = 1;
3fb02738 1436
7779688f 1437 return acpi_bus_scan(device->handle, &ops, NULL);
3fb02738
RS
1438}
1439EXPORT_SYMBOL(acpi_bus_start);
1da177e4 1440
ceaba663 1441int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1da177e4 1442{
4be44fcd
LB
1443 acpi_status status;
1444 struct acpi_device *parent, *child;
1445 acpi_handle phandle, chandle;
1446 acpi_object_type type;
1447 u32 level = 1;
1448 int err = 0;
1449
1450 parent = start;
1da177e4
LT
1451 phandle = start->handle;
1452 child = chandle = NULL;
1453
1454 while ((level > 0) && parent && (!err)) {
1455 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
4be44fcd 1456 chandle, &chandle);
1da177e4
LT
1457
1458 /*
1459 * If this scope is exhausted then move our way back up.
1460 */
1461 if (ACPI_FAILURE(status)) {
1462 level--;
1463 chandle = phandle;
1464 acpi_get_parent(phandle, &phandle);
1465 child = parent;
1466 parent = parent->parent;
1467
1468 if (level == 0)
1469 err = acpi_bus_remove(child, rmdevice);
1470 else
1471 err = acpi_bus_remove(child, 1);
1472
1473 continue;
1474 }
1475
1476 status = acpi_get_type(chandle, &type);
1477 if (ACPI_FAILURE(status)) {
1478 continue;
1479 }
1480 /*
1481 * If there is a device corresponding to chandle then
1482 * parse it (depth-first).
1483 */
1484 if (acpi_bus_get_device(chandle, &child) == 0) {
1485 level++;
1486 phandle = chandle;
1487 chandle = NULL;
1488 parent = child;
1489 }
1490 continue;
1491 }
1492 return err;
1493}
ceaba663
KA
1494EXPORT_SYMBOL_GPL(acpi_bus_trim);
1495
e8b945c9 1496static int acpi_bus_scan_fixed(void)
1da177e4 1497{
4be44fcd
LB
1498 int result = 0;
1499 struct acpi_device *device = NULL;
c4168bff 1500 struct acpi_bus_ops ops;
1da177e4 1501
c4168bff
LS
1502 memset(&ops, 0, sizeof(ops));
1503 ops.acpi_op_add = 1;
1504 ops.acpi_op_start = 1;
1505
1da177e4
LT
1506 /*
1507 * Enumerate all fixed-feature devices.
1508 */
cee324b1 1509 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
5c478f49 1510 result = acpi_add_single_object(&device, NULL,
c4168bff 1511 ACPI_BUS_TYPE_POWER_BUTTON,
778cbc1d 1512 ACPI_STA_DEFAULT,
c4168bff 1513 &ops);
3fb02738 1514 }
1da177e4 1515
cee324b1 1516 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
5c478f49 1517 result = acpi_add_single_object(&device, NULL,
c4168bff 1518 ACPI_BUS_TYPE_SLEEP_BUTTON,
778cbc1d 1519 ACPI_STA_DEFAULT,
c4168bff 1520 &ops);
3fb02738 1521 }
1da177e4 1522
d550d98d 1523 return result;
1da177e4
LT
1524}
1525
e747f274 1526int __init acpi_scan_init(void)
1da177e4
LT
1527{
1528 int result;
3fb02738 1529 struct acpi_bus_ops ops;
1da177e4 1530
c4168bff
LS
1531 memset(&ops, 0, sizeof(ops));
1532 ops.acpi_op_add = 1;
1533 ops.acpi_op_start = 1;
1da177e4 1534
5b327265
PM
1535 result = bus_register(&acpi_bus_type);
1536 if (result) {
1537 /* We don't want to quit even if we failed to add suspend/resume */
1538 printk(KERN_ERR PREFIX "Could not register bus type\n");
1539 }
1540
1da177e4
LT
1541 /*
1542 * Enumerate devices in the ACPI namespace.
1543 */
51a85faf 1544 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
c04209a7 1545
c4168bff 1546 if (!result)
adc08e20 1547 result = acpi_bus_scan_fixed();
1da177e4
LT
1548
1549 if (result)
1550 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1551
d550d98d 1552 return result;
1da177e4 1553}