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