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