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