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