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