On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / drivers / acpi / scan.c
blobfcd2d0ba39e8e1909bd5adf5b1715289d5946430
1 /*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/kernel.h>
8 #include <linux/acpi.h>
9 #include <linux/signal.h>
10 #include <linux/kthread.h>
12 #include <acpi/acpi_drivers.h>
14 #include "internal.h"
16 #define _COMPONENT ACPI_BUS_COMPONENT
17 ACPI_MODULE_NAME("scan");
18 #define STRUCT_TO_INT(s) (*((int*)&s))
19 extern struct acpi_device *acpi_root;
21 #define ACPI_BUS_CLASS "system_bus"
22 #define ACPI_BUS_HID "LNXSYBUS"
23 #define ACPI_BUS_DEVICE_NAME "System Bus"
25 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
27 static LIST_HEAD(acpi_device_list);
28 static LIST_HEAD(acpi_bus_id_list);
29 DEFINE_MUTEX(acpi_device_lock);
30 LIST_HEAD(acpi_wakeup_device_list);
32 struct acpi_device_bus_id{
33 char bus_id[15];
34 unsigned int instance_no;
35 struct list_head node;
39 * Creates hid/cid(s) string needed for modalias and uevent
40 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
41 * char *modalias: "acpi:IBM0001:ACPI0001"
43 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
44 int size)
46 int len;
47 int count;
48 struct acpi_hardware_id *id;
50 len = snprintf(modalias, size, "acpi:");
51 size -= len;
53 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
54 count = snprintf(&modalias[len], size, "%s:", id->id);
55 if (count < 0 || count >= size)
56 return -EINVAL;
57 len += count;
58 size -= count;
61 modalias[len] = '\0';
62 return len;
65 static ssize_t
66 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
67 struct acpi_device *acpi_dev = to_acpi_device(dev);
68 int len;
70 /* Device has no HID and no CID or string is >1024 */
71 len = create_modalias(acpi_dev, buf, 1024);
72 if (len <= 0)
73 return 0;
74 buf[len++] = '\n';
75 return len;
77 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
79 static void acpi_bus_hot_remove_device(void *context)
81 struct acpi_device *device;
82 acpi_handle handle = context;
83 struct acpi_object_list arg_list;
84 union acpi_object arg;
85 acpi_status status = AE_OK;
87 if (acpi_bus_get_device(handle, &device))
88 return;
90 if (!device)
91 return;
93 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
94 "Hot-removing device %s...\n", dev_name(&device->dev)));
96 if (acpi_bus_trim(device, 1)) {
97 printk(KERN_ERR PREFIX
98 "Removing device failed\n");
99 return;
102 /* power off device */
103 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
104 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
105 printk(KERN_WARNING PREFIX
106 "Power-off device failed\n");
108 if (device->flags.lockable) {
109 arg_list.count = 1;
110 arg_list.pointer = &arg;
111 arg.type = ACPI_TYPE_INTEGER;
112 arg.integer.value = 0;
113 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
116 arg_list.count = 1;
117 arg_list.pointer = &arg;
118 arg.type = ACPI_TYPE_INTEGER;
119 arg.integer.value = 1;
122 * TBD: _EJD support.
124 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
125 if (ACPI_FAILURE(status))
126 printk(KERN_WARNING PREFIX
127 "Eject device failed\n");
129 return;
132 static ssize_t
133 acpi_eject_store(struct device *d, struct device_attribute *attr,
134 const char *buf, size_t count)
136 int ret = count;
137 acpi_status status;
138 acpi_object_type type = 0;
139 struct acpi_device *acpi_device = to_acpi_device(d);
141 if ((!count) || (buf[0] != '1')) {
142 return -EINVAL;
144 #ifndef FORCE_EJECT
145 if (acpi_device->driver == NULL) {
146 ret = -ENODEV;
147 goto err;
149 #endif
150 status = acpi_get_type(acpi_device->handle, &type);
151 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
152 ret = -ENODEV;
153 goto err;
156 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
157 err:
158 return ret;
161 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
163 static ssize_t
164 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
165 struct acpi_device *acpi_dev = to_acpi_device(dev);
167 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
169 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
171 static ssize_t
172 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
173 struct acpi_device *acpi_dev = to_acpi_device(dev);
174 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
175 int result;
177 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
178 if (result)
179 goto end;
181 result = sprintf(buf, "%s\n", (char*)path.pointer);
182 kfree(path.pointer);
183 end:
184 return result;
186 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
188 static int acpi_device_setup_files(struct acpi_device *dev)
190 acpi_status status;
191 acpi_handle temp;
192 int result = 0;
195 * Devices gotten from FADT don't have a "path" attribute
197 if (dev->handle) {
198 result = device_create_file(&dev->dev, &dev_attr_path);
199 if (result)
200 goto end;
203 result = device_create_file(&dev->dev, &dev_attr_hid);
204 if (result)
205 goto end;
207 result = device_create_file(&dev->dev, &dev_attr_modalias);
208 if (result)
209 goto end;
212 * If device has _EJ0, 'eject' file is created that is used to trigger
213 * hot-removal function from userland.
215 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
216 if (ACPI_SUCCESS(status))
217 result = device_create_file(&dev->dev, &dev_attr_eject);
218 end:
219 return result;
222 static void acpi_device_remove_files(struct acpi_device *dev)
224 acpi_status status;
225 acpi_handle temp;
228 * If device has _EJ0, 'eject' file is created that is used to trigger
229 * hot-removal function from userland.
231 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
232 if (ACPI_SUCCESS(status))
233 device_remove_file(&dev->dev, &dev_attr_eject);
235 device_remove_file(&dev->dev, &dev_attr_modalias);
236 device_remove_file(&dev->dev, &dev_attr_hid);
237 if (dev->handle)
238 device_remove_file(&dev->dev, &dev_attr_path);
240 /* --------------------------------------------------------------------------
241 ACPI Bus operations
242 -------------------------------------------------------------------------- */
244 int acpi_match_device_ids(struct acpi_device *device,
245 const struct acpi_device_id *ids)
247 const struct acpi_device_id *id;
248 struct acpi_hardware_id *hwid;
251 * If the device is not present, it is unnecessary to load device
252 * driver for it.
254 if (!device->status.present)
255 return -ENODEV;
257 for (id = ids; id->id[0]; id++)
258 list_for_each_entry(hwid, &device->pnp.ids, list)
259 if (!strcmp((char *) id->id, hwid->id))
260 return 0;
262 return -ENOENT;
264 EXPORT_SYMBOL(acpi_match_device_ids);
266 static void acpi_free_ids(struct acpi_device *device)
268 struct acpi_hardware_id *id, *tmp;
270 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
271 kfree(id->id);
272 kfree(id);
276 static void acpi_device_release(struct device *dev)
278 struct acpi_device *acpi_dev = to_acpi_device(dev);
280 acpi_free_ids(acpi_dev);
281 kfree(acpi_dev);
284 static int acpi_device_suspend(struct device *dev, pm_message_t state)
286 struct acpi_device *acpi_dev = to_acpi_device(dev);
287 struct acpi_driver *acpi_drv = acpi_dev->driver;
289 if (acpi_drv && acpi_drv->ops.suspend)
290 return acpi_drv->ops.suspend(acpi_dev, state);
291 return 0;
294 static int acpi_device_resume(struct device *dev)
296 struct acpi_device *acpi_dev = to_acpi_device(dev);
297 struct acpi_driver *acpi_drv = acpi_dev->driver;
299 if (acpi_drv && acpi_drv->ops.resume)
300 return acpi_drv->ops.resume(acpi_dev);
301 return 0;
304 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
306 struct acpi_device *acpi_dev = to_acpi_device(dev);
307 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
309 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
312 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
314 struct acpi_device *acpi_dev = to_acpi_device(dev);
315 int len;
317 if (add_uevent_var(env, "MODALIAS="))
318 return -ENOMEM;
319 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
320 sizeof(env->buf) - env->buflen);
321 if (len >= (sizeof(env->buf) - env->buflen))
322 return -ENOMEM;
323 env->buflen += len;
324 return 0;
327 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
329 struct acpi_device *device = data;
331 device->driver->ops.notify(device, event);
334 static acpi_status acpi_device_notify_fixed(void *data)
336 struct acpi_device *device = data;
338 /* Fixed hardware devices have no handles */
339 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
340 return AE_OK;
343 static int acpi_device_install_notify_handler(struct acpi_device *device)
345 acpi_status status;
347 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
348 status =
349 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
350 acpi_device_notify_fixed,
351 device);
352 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
353 status =
354 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
355 acpi_device_notify_fixed,
356 device);
357 else
358 status = acpi_install_notify_handler(device->handle,
359 ACPI_DEVICE_NOTIFY,
360 acpi_device_notify,
361 device);
363 if (ACPI_FAILURE(status))
364 return -EINVAL;
365 return 0;
368 static void acpi_device_remove_notify_handler(struct acpi_device *device)
370 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
371 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
372 acpi_device_notify_fixed);
373 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
374 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
375 acpi_device_notify_fixed);
376 else
377 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
378 acpi_device_notify);
381 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
382 static int acpi_start_single_object(struct acpi_device *);
383 static int acpi_device_probe(struct device * dev)
385 struct acpi_device *acpi_dev = to_acpi_device(dev);
386 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
387 int ret;
389 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
390 if (!ret) {
391 if (acpi_dev->bus_ops.acpi_op_start)
392 acpi_start_single_object(acpi_dev);
394 if (acpi_drv->ops.notify) {
395 ret = acpi_device_install_notify_handler(acpi_dev);
396 if (ret) {
397 if (acpi_drv->ops.remove)
398 acpi_drv->ops.remove(acpi_dev,
399 acpi_dev->removal_type);
400 return ret;
404 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
405 "Found driver [%s] for device [%s]\n",
406 acpi_drv->name, acpi_dev->pnp.bus_id));
407 get_device(dev);
409 return ret;
412 static int acpi_device_remove(struct device * dev)
414 struct acpi_device *acpi_dev = to_acpi_device(dev);
415 struct acpi_driver *acpi_drv = acpi_dev->driver;
417 if (acpi_drv) {
418 if (acpi_drv->ops.notify)
419 acpi_device_remove_notify_handler(acpi_dev);
420 if (acpi_drv->ops.remove)
421 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
423 acpi_dev->driver = NULL;
424 acpi_dev->driver_data = NULL;
426 put_device(dev);
427 return 0;
430 static void acpi_device_shutdown(struct device *dev)
432 struct acpi_device *acpi_dev = to_acpi_device(dev);
433 struct acpi_driver *acpi_drv = acpi_dev->driver;
435 if (acpi_drv && acpi_drv->ops.shutdown)
436 acpi_drv->ops.shutdown(acpi_dev);
438 return ;
441 struct bus_type acpi_bus_type = {
442 .name = "acpi",
443 .suspend = acpi_device_suspend,
444 .resume = acpi_device_resume,
445 .shutdown = acpi_device_shutdown,
446 .match = acpi_bus_match,
447 .probe = acpi_device_probe,
448 .remove = acpi_device_remove,
449 .uevent = acpi_device_uevent,
452 static int acpi_device_register(struct acpi_device *device)
454 int result;
455 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
456 int found = 0;
459 * Linkage
460 * -------
461 * Link this device to its parent and siblings.
463 INIT_LIST_HEAD(&device->children);
464 INIT_LIST_HEAD(&device->node);
465 INIT_LIST_HEAD(&device->wakeup_list);
467 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
468 if (!new_bus_id) {
469 printk(KERN_ERR PREFIX "Memory allocation error\n");
470 return -ENOMEM;
473 mutex_lock(&acpi_device_lock);
475 * Find suitable bus_id and instance number in acpi_bus_id_list
476 * If failed, create one and link it into acpi_bus_id_list
478 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
479 if (!strcmp(acpi_device_bus_id->bus_id,
480 acpi_device_hid(device))) {
481 acpi_device_bus_id->instance_no++;
482 found = 1;
483 kfree(new_bus_id);
484 break;
487 if (!found) {
488 acpi_device_bus_id = new_bus_id;
489 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
490 acpi_device_bus_id->instance_no = 0;
491 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
493 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
495 if (device->parent)
496 list_add_tail(&device->node, &device->parent->children);
498 if (device->wakeup.flags.valid)
499 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
500 mutex_unlock(&acpi_device_lock);
502 if (device->parent)
503 device->dev.parent = &device->parent->dev;
504 device->dev.bus = &acpi_bus_type;
505 device->dev.release = &acpi_device_release;
506 result = device_register(&device->dev);
507 if (result) {
508 dev_err(&device->dev, "Error registering device\n");
509 goto end;
512 result = acpi_device_setup_files(device);
513 if (result)
514 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
515 dev_name(&device->dev));
517 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
518 device_enable_async_suspend(&device->dev, true);
519 return 0;
520 end:
521 mutex_lock(&acpi_device_lock);
522 if (device->parent)
523 list_del(&device->node);
524 list_del(&device->wakeup_list);
525 mutex_unlock(&acpi_device_lock);
526 return result;
529 static void acpi_device_unregister(struct acpi_device *device, int type)
531 mutex_lock(&acpi_device_lock);
532 if (device->parent)
533 list_del(&device->node);
535 list_del(&device->wakeup_list);
536 mutex_unlock(&acpi_device_lock);
538 acpi_detach_data(device->handle, acpi_bus_data_handler);
540 acpi_device_remove_files(device);
541 device_unregister(&device->dev);
544 /* --------------------------------------------------------------------------
545 Driver Management
546 -------------------------------------------------------------------------- */
548 * acpi_bus_driver_init - add a device to a driver
549 * @device: the device to add and initialize
550 * @driver: driver for the device
552 * Used to initialize a device via its device driver. Called whenever a
553 * driver is bound to a device. Invokes the driver's add() ops.
555 static int
556 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
558 int result = 0;
560 if (!device || !driver)
561 return -EINVAL;
563 if (!driver->ops.add)
564 return -ENOSYS;
566 result = driver->ops.add(device);
567 if (result) {
568 device->driver = NULL;
569 device->driver_data = NULL;
570 return result;
573 device->driver = driver;
576 * TBD - Configuration Management: Assign resources to device based
577 * upon possible configuration and currently allocated resources.
580 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
581 "Driver successfully bound to device\n"));
582 return 0;
585 static int acpi_start_single_object(struct acpi_device *device)
587 int result = 0;
588 struct acpi_driver *driver;
591 if (!(driver = device->driver))
592 return 0;
594 if (driver->ops.start) {
595 result = driver->ops.start(device);
596 if (result && driver->ops.remove)
597 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
600 return result;
604 * acpi_bus_register_driver - register a driver with the ACPI bus
605 * @driver: driver being registered
607 * Registers a driver with the ACPI bus. Searches the namespace for all
608 * devices that match the driver's criteria and binds. Returns zero for
609 * success or a negative error status for failure.
611 int acpi_bus_register_driver(struct acpi_driver *driver)
613 int ret;
615 if (acpi_disabled)
616 return -ENODEV;
617 driver->drv.name = driver->name;
618 driver->drv.bus = &acpi_bus_type;
619 driver->drv.owner = driver->owner;
621 ret = driver_register(&driver->drv);
622 return ret;
625 EXPORT_SYMBOL(acpi_bus_register_driver);
628 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
629 * @driver: driver to unregister
631 * Unregisters a driver with the ACPI bus. Searches the namespace for all
632 * devices that match the driver's criteria and unbinds.
634 void acpi_bus_unregister_driver(struct acpi_driver *driver)
636 driver_unregister(&driver->drv);
639 EXPORT_SYMBOL(acpi_bus_unregister_driver);
641 /* --------------------------------------------------------------------------
642 Device Enumeration
643 -------------------------------------------------------------------------- */
644 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
646 acpi_status status;
647 int ret;
648 struct acpi_device *device;
651 * Fixed hardware devices do not appear in the namespace and do not
652 * have handles, but we fabricate acpi_devices for them, so we have
653 * to deal with them specially.
655 if (handle == NULL)
656 return acpi_root;
658 do {
659 status = acpi_get_parent(handle, &handle);
660 if (status == AE_NULL_ENTRY)
661 return NULL;
662 if (ACPI_FAILURE(status))
663 return acpi_root;
665 ret = acpi_bus_get_device(handle, &device);
666 if (ret == 0)
667 return device;
668 } while (1);
671 acpi_status
672 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
674 acpi_status status;
675 acpi_handle tmp;
676 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
677 union acpi_object *obj;
679 status = acpi_get_handle(handle, "_EJD", &tmp);
680 if (ACPI_FAILURE(status))
681 return status;
683 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
684 if (ACPI_SUCCESS(status)) {
685 obj = buffer.pointer;
686 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
687 ejd);
688 kfree(buffer.pointer);
690 return status;
692 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
694 void acpi_bus_data_handler(acpi_handle handle, void *context)
697 /* TBD */
699 return;
702 static int acpi_bus_get_perf_flags(struct acpi_device *device)
704 device->performance.state = ACPI_STATE_UNKNOWN;
705 return 0;
708 static acpi_status
709 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
710 union acpi_object *package)
712 int i = 0;
713 union acpi_object *element = NULL;
715 if (!device || !package || (package->package.count < 2))
716 return AE_BAD_PARAMETER;
718 element = &(package->package.elements[0]);
719 if (!element)
720 return AE_BAD_PARAMETER;
721 if (element->type == ACPI_TYPE_PACKAGE) {
722 if ((element->package.count < 2) ||
723 (element->package.elements[0].type !=
724 ACPI_TYPE_LOCAL_REFERENCE)
725 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
726 return AE_BAD_DATA;
727 device->wakeup.gpe_device =
728 element->package.elements[0].reference.handle;
729 device->wakeup.gpe_number =
730 (u32) element->package.elements[1].integer.value;
731 } else if (element->type == ACPI_TYPE_INTEGER) {
732 device->wakeup.gpe_number = element->integer.value;
733 } else
734 return AE_BAD_DATA;
736 element = &(package->package.elements[1]);
737 if (element->type != ACPI_TYPE_INTEGER) {
738 return AE_BAD_DATA;
740 device->wakeup.sleep_state = element->integer.value;
742 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
743 return AE_NO_MEMORY;
745 device->wakeup.resources.count = package->package.count - 2;
746 for (i = 0; i < device->wakeup.resources.count; i++) {
747 element = &(package->package.elements[i + 2]);
748 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
749 return AE_BAD_DATA;
751 device->wakeup.resources.handles[i] = element->reference.handle;
754 return AE_OK;
757 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
759 acpi_status status = 0;
760 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
761 union acpi_object *package = NULL;
762 int psw_error;
764 struct acpi_device_id button_device_ids[] = {
765 {"PNP0C0D", 0},
766 {"PNP0C0C", 0},
767 {"PNP0C0E", 0},
768 {"", 0},
771 /* _PRW */
772 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
773 if (ACPI_FAILURE(status)) {
774 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
775 goto end;
778 package = (union acpi_object *)buffer.pointer;
779 status = acpi_bus_extract_wakeup_device_power_package(device, package);
780 if (ACPI_FAILURE(status)) {
781 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
782 goto end;
785 kfree(buffer.pointer);
787 device->wakeup.flags.valid = 1;
788 device->wakeup.prepare_count = 0;
789 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
790 * system for the ACPI device with the _PRW object.
791 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
792 * So it is necessary to call _DSW object first. Only when it is not
793 * present will the _PSW object used.
795 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
796 if (psw_error)
797 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
798 "error in _DSW or _PSW evaluation\n"));
800 /* Power button, Lid switch always enable wakeup */
801 if (!acpi_match_device_ids(device, button_device_ids))
802 device->wakeup.flags.run_wake = 1;
804 end:
805 if (ACPI_FAILURE(status))
806 device->flags.wake_capable = 0;
807 return 0;
810 static int acpi_bus_get_power_flags(struct acpi_device *device)
812 acpi_status status = 0;
813 acpi_handle handle = NULL;
814 u32 i = 0;
818 * Power Management Flags
820 status = acpi_get_handle(device->handle, "_PSC", &handle);
821 if (ACPI_SUCCESS(status))
822 device->power.flags.explicit_get = 1;
823 status = acpi_get_handle(device->handle, "_IRC", &handle);
824 if (ACPI_SUCCESS(status))
825 device->power.flags.inrush_current = 1;
828 * Enumerate supported power management states
830 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
831 struct acpi_device_power_state *ps = &device->power.states[i];
832 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
834 /* Evaluate "_PRx" to se if power resources are referenced */
835 acpi_evaluate_reference(device->handle, object_name, NULL,
836 &ps->resources);
837 if (ps->resources.count) {
838 device->power.flags.power_resources = 1;
839 ps->flags.valid = 1;
842 /* Evaluate "_PSx" to see if we can do explicit sets */
843 object_name[2] = 'S';
844 status = acpi_get_handle(device->handle, object_name, &handle);
845 if (ACPI_SUCCESS(status)) {
846 ps->flags.explicit_set = 1;
847 ps->flags.valid = 1;
850 /* State is valid if we have some power control */
851 if (ps->resources.count || ps->flags.explicit_set)
852 ps->flags.valid = 1;
854 ps->power = -1; /* Unknown - driver assigned */
855 ps->latency = -1; /* Unknown - driver assigned */
858 /* Set defaults for D0 and D3 states (always valid) */
859 device->power.states[ACPI_STATE_D0].flags.valid = 1;
860 device->power.states[ACPI_STATE_D0].power = 100;
861 device->power.states[ACPI_STATE_D3].flags.valid = 1;
862 device->power.states[ACPI_STATE_D3].power = 0;
864 /* TBD: System wake support and resource requirements. */
866 device->power.state = ACPI_STATE_UNKNOWN;
867 acpi_bus_get_power(device->handle, &(device->power.state));
869 return 0;
872 static int acpi_bus_get_flags(struct acpi_device *device)
874 acpi_status status = AE_OK;
875 acpi_handle temp = NULL;
878 /* Presence of _STA indicates 'dynamic_status' */
879 status = acpi_get_handle(device->handle, "_STA", &temp);
880 if (ACPI_SUCCESS(status))
881 device->flags.dynamic_status = 1;
883 /* Presence of _RMV indicates 'removable' */
884 status = acpi_get_handle(device->handle, "_RMV", &temp);
885 if (ACPI_SUCCESS(status))
886 device->flags.removable = 1;
888 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
889 status = acpi_get_handle(device->handle, "_EJD", &temp);
890 if (ACPI_SUCCESS(status))
891 device->flags.ejectable = 1;
892 else {
893 status = acpi_get_handle(device->handle, "_EJ0", &temp);
894 if (ACPI_SUCCESS(status))
895 device->flags.ejectable = 1;
898 /* Presence of _LCK indicates 'lockable' */
899 status = acpi_get_handle(device->handle, "_LCK", &temp);
900 if (ACPI_SUCCESS(status))
901 device->flags.lockable = 1;
903 /* Presence of _PS0|_PR0 indicates 'power manageable' */
904 status = acpi_get_handle(device->handle, "_PS0", &temp);
905 if (ACPI_FAILURE(status))
906 status = acpi_get_handle(device->handle, "_PR0", &temp);
907 if (ACPI_SUCCESS(status))
908 device->flags.power_manageable = 1;
910 /* Presence of _PRW indicates wake capable */
911 status = acpi_get_handle(device->handle, "_PRW", &temp);
912 if (ACPI_SUCCESS(status))
913 device->flags.wake_capable = 1;
915 /* TBD: Performance management */
917 return 0;
920 static void acpi_device_get_busid(struct acpi_device *device)
922 char bus_id[5] = { '?', 0 };
923 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
924 int i = 0;
927 * Bus ID
928 * ------
929 * The device's Bus ID is simply the object name.
930 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
932 if (ACPI_IS_ROOT_DEVICE(device)) {
933 strcpy(device->pnp.bus_id, "ACPI");
934 return;
937 switch (device->device_type) {
938 case ACPI_BUS_TYPE_POWER_BUTTON:
939 strcpy(device->pnp.bus_id, "PWRF");
940 break;
941 case ACPI_BUS_TYPE_SLEEP_BUTTON:
942 strcpy(device->pnp.bus_id, "SLPF");
943 break;
944 default:
945 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
946 /* Clean up trailing underscores (if any) */
947 for (i = 3; i > 1; i--) {
948 if (bus_id[i] == '_')
949 bus_id[i] = '\0';
950 else
951 break;
953 strcpy(device->pnp.bus_id, bus_id);
954 break;
959 * acpi_bay_match - see if a device is an ejectable driver bay
961 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
962 * then we can safely call it an ejectable drive bay
964 static int acpi_bay_match(struct acpi_device *device){
965 acpi_status status;
966 acpi_handle handle;
967 acpi_handle tmp;
968 acpi_handle phandle;
970 handle = device->handle;
972 status = acpi_get_handle(handle, "_EJ0", &tmp);
973 if (ACPI_FAILURE(status))
974 return -ENODEV;
976 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
977 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
978 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
979 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
980 return 0;
982 if (acpi_get_parent(handle, &phandle))
983 return -ENODEV;
985 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
986 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
987 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
988 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
989 return 0;
991 return -ENODEV;
995 * acpi_dock_match - see if a device has a _DCK method
997 static int acpi_dock_match(struct acpi_device *device)
999 acpi_handle tmp;
1000 return acpi_get_handle(device->handle, "_DCK", &tmp);
1003 char *acpi_device_hid(struct acpi_device *device)
1005 struct acpi_hardware_id *hid;
1007 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1008 return hid->id;
1010 EXPORT_SYMBOL(acpi_device_hid);
1012 static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1014 struct acpi_hardware_id *id;
1016 id = kmalloc(sizeof(*id), GFP_KERNEL);
1017 if (!id)
1018 return;
1020 id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
1021 if (!id->id) {
1022 kfree(id);
1023 return;
1026 strcpy(id->id, dev_id);
1027 list_add_tail(&id->list, &device->pnp.ids);
1030 static void acpi_device_set_id(struct acpi_device *device)
1032 acpi_status status;
1033 struct acpi_device_info *info;
1034 struct acpica_device_id_list *cid_list;
1035 int i;
1037 switch (device->device_type) {
1038 case ACPI_BUS_TYPE_DEVICE:
1039 if (ACPI_IS_ROOT_DEVICE(device)) {
1040 acpi_add_id(device, ACPI_SYSTEM_HID);
1041 break;
1042 } else if (ACPI_IS_ROOT_DEVICE(device->parent)) {
1043 /* \_SB_, the only root-level namespace device */
1044 acpi_add_id(device, ACPI_BUS_HID);
1045 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1046 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1047 break;
1050 status = acpi_get_object_info(device->handle, &info);
1051 if (ACPI_FAILURE(status)) {
1052 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1053 return;
1056 if (info->valid & ACPI_VALID_HID)
1057 acpi_add_id(device, info->hardware_id.string);
1058 if (info->valid & ACPI_VALID_CID) {
1059 cid_list = &info->compatible_id_list;
1060 for (i = 0; i < cid_list->count; i++)
1061 acpi_add_id(device, cid_list->ids[i].string);
1063 if (info->valid & ACPI_VALID_ADR) {
1064 device->pnp.bus_address = info->address;
1065 device->flags.bus_address = 1;
1068 kfree(info);
1071 * Some devices don't reliably have _HIDs & _CIDs, so add
1072 * synthetic HIDs to make sure drivers can find them.
1074 if (acpi_is_video_device(device))
1075 acpi_add_id(device, ACPI_VIDEO_HID);
1076 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1077 acpi_add_id(device, ACPI_BAY_HID);
1078 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1079 acpi_add_id(device, ACPI_DOCK_HID);
1081 break;
1082 case ACPI_BUS_TYPE_POWER:
1083 acpi_add_id(device, ACPI_POWER_HID);
1084 break;
1085 case ACPI_BUS_TYPE_PROCESSOR:
1086 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1087 break;
1088 case ACPI_BUS_TYPE_THERMAL:
1089 acpi_add_id(device, ACPI_THERMAL_HID);
1090 break;
1091 case ACPI_BUS_TYPE_POWER_BUTTON:
1092 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1093 break;
1094 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1095 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1096 break;
1100 * We build acpi_devices for some objects that don't have _HID or _CID,
1101 * e.g., PCI bridges and slots. Drivers can't bind to these objects,
1102 * but we do use them indirectly by traversing the acpi_device tree.
1103 * This generic ID isn't useful for driver binding, but it provides
1104 * the useful property that "every acpi_device has an ID."
1106 if (list_empty(&device->pnp.ids))
1107 acpi_add_id(device, "device");
1110 static int acpi_device_set_context(struct acpi_device *device)
1112 acpi_status status;
1115 * Context
1116 * -------
1117 * Attach this 'struct acpi_device' to the ACPI object. This makes
1118 * resolutions from handle->device very efficient. Fixed hardware
1119 * devices have no handles, so we skip them.
1121 if (!device->handle)
1122 return 0;
1124 status = acpi_attach_data(device->handle,
1125 acpi_bus_data_handler, device);
1126 if (ACPI_SUCCESS(status))
1127 return 0;
1129 printk(KERN_ERR PREFIX "Error attaching device data\n");
1130 return -ENODEV;
1133 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1135 if (!dev)
1136 return -EINVAL;
1138 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1139 device_release_driver(&dev->dev);
1141 if (!rmdevice)
1142 return 0;
1145 * unbind _ADR-Based Devices when hot removal
1147 if (dev->flags.bus_address) {
1148 if ((dev->parent) && (dev->parent->ops.unbind))
1149 dev->parent->ops.unbind(dev);
1151 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1153 return 0;
1156 static int acpi_add_single_object(struct acpi_device **child,
1157 acpi_handle handle, int type,
1158 unsigned long long sta,
1159 struct acpi_bus_ops *ops)
1161 int result;
1162 struct acpi_device *device;
1163 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1165 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1166 if (!device) {
1167 printk(KERN_ERR PREFIX "Memory allocation error\n");
1168 return -ENOMEM;
1171 INIT_LIST_HEAD(&device->pnp.ids);
1172 device->device_type = type;
1173 device->handle = handle;
1174 device->parent = acpi_bus_get_parent(handle);
1175 device->bus_ops = *ops; /* workround for not call .start */
1176 STRUCT_TO_INT(device->status) = sta;
1178 acpi_device_get_busid(device);
1181 * Flags
1182 * -----
1183 * Note that we only look for object handles -- cannot evaluate objects
1184 * until we know the device is present and properly initialized.
1186 result = acpi_bus_get_flags(device);
1187 if (result)
1188 goto end;
1191 * Initialize Device
1192 * -----------------
1193 * TBD: Synch with Core's enumeration/initialization process.
1195 acpi_device_set_id(device);
1198 * Power Management
1199 * ----------------
1201 if (device->flags.power_manageable) {
1202 result = acpi_bus_get_power_flags(device);
1203 if (result)
1204 goto end;
1208 * Wakeup device management
1209 *-----------------------
1211 if (device->flags.wake_capable) {
1212 result = acpi_bus_get_wakeup_device_flags(device);
1213 if (result)
1214 goto end;
1218 * Performance Management
1219 * ----------------------
1221 if (device->flags.performance_manageable) {
1222 result = acpi_bus_get_perf_flags(device);
1223 if (result)
1224 goto end;
1227 if ((result = acpi_device_set_context(device)))
1228 goto end;
1230 result = acpi_device_register(device);
1233 * Bind _ADR-Based Devices when hot add
1235 if (device->flags.bus_address) {
1236 if (device->parent && device->parent->ops.bind)
1237 device->parent->ops.bind(device);
1240 end:
1241 if (!result) {
1242 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1243 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1244 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1245 (char *) buffer.pointer,
1246 device->parent ? dev_name(&device->parent->dev) :
1247 "(null)"));
1248 kfree(buffer.pointer);
1249 *child = device;
1250 } else
1251 acpi_device_release(&device->dev);
1253 return result;
1256 #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1257 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1259 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1260 unsigned long long *sta)
1262 acpi_status status;
1263 acpi_object_type acpi_type;
1265 status = acpi_get_type(handle, &acpi_type);
1266 if (ACPI_FAILURE(status))
1267 return -ENODEV;
1269 switch (acpi_type) {
1270 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1271 case ACPI_TYPE_DEVICE:
1272 *type = ACPI_BUS_TYPE_DEVICE;
1273 status = acpi_bus_get_status_handle(handle, sta);
1274 if (ACPI_FAILURE(status))
1275 return -ENODEV;
1276 break;
1277 case ACPI_TYPE_PROCESSOR:
1278 *type = ACPI_BUS_TYPE_PROCESSOR;
1279 status = acpi_bus_get_status_handle(handle, sta);
1280 if (ACPI_FAILURE(status))
1281 return -ENODEV;
1282 break;
1283 case ACPI_TYPE_THERMAL:
1284 *type = ACPI_BUS_TYPE_THERMAL;
1285 *sta = ACPI_STA_DEFAULT;
1286 break;
1287 case ACPI_TYPE_POWER:
1288 *type = ACPI_BUS_TYPE_POWER;
1289 *sta = ACPI_STA_DEFAULT;
1290 break;
1291 default:
1292 return -ENODEV;
1295 return 0;
1298 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1299 void *context, void **return_value)
1301 struct acpi_bus_ops *ops = context;
1302 int type;
1303 unsigned long long sta;
1304 struct acpi_device *device;
1305 acpi_status status;
1306 int result;
1308 result = acpi_bus_type_and_status(handle, &type, &sta);
1309 if (result)
1310 return AE_OK;
1312 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1313 !(sta & ACPI_STA_DEVICE_FUNCTIONING))
1314 return AE_CTRL_DEPTH;
1317 * We may already have an acpi_device from a previous enumeration. If
1318 * so, we needn't add it again, but we may still have to start it.
1320 device = NULL;
1321 acpi_bus_get_device(handle, &device);
1322 if (ops->acpi_op_add && !device)
1323 acpi_add_single_object(&device, handle, type, sta, ops);
1325 if (!device)
1326 return AE_CTRL_DEPTH;
1328 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1329 status = acpi_start_single_object(device);
1330 if (ACPI_FAILURE(status))
1331 return AE_CTRL_DEPTH;
1334 if (!*return_value)
1335 *return_value = device;
1336 return AE_OK;
1339 static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1340 struct acpi_device **child)
1342 acpi_status status;
1343 void *device = NULL;
1345 status = acpi_bus_check_add(handle, 0, ops, &device);
1346 if (ACPI_SUCCESS(status))
1347 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1348 acpi_bus_check_add, ops, &device);
1350 if (child)
1351 *child = device;
1352 return 0;
1356 acpi_bus_add(struct acpi_device **child,
1357 struct acpi_device *parent, acpi_handle handle, int type)
1359 struct acpi_bus_ops ops;
1361 memset(&ops, 0, sizeof(ops));
1362 ops.acpi_op_add = 1;
1364 acpi_bus_scan(handle, &ops, child);
1365 return 0;
1367 EXPORT_SYMBOL(acpi_bus_add);
1369 int acpi_bus_start(struct acpi_device *device)
1371 struct acpi_bus_ops ops;
1373 memset(&ops, 0, sizeof(ops));
1374 ops.acpi_op_start = 1;
1376 acpi_bus_scan(device->handle, &ops, NULL);
1377 return 0;
1379 EXPORT_SYMBOL(acpi_bus_start);
1381 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1383 acpi_status status;
1384 struct acpi_device *parent, *child;
1385 acpi_handle phandle, chandle;
1386 acpi_object_type type;
1387 u32 level = 1;
1388 int err = 0;
1390 parent = start;
1391 phandle = start->handle;
1392 child = chandle = NULL;
1394 while ((level > 0) && parent && (!err)) {
1395 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1396 chandle, &chandle);
1399 * If this scope is exhausted then move our way back up.
1401 if (ACPI_FAILURE(status)) {
1402 level--;
1403 chandle = phandle;
1404 acpi_get_parent(phandle, &phandle);
1405 child = parent;
1406 parent = parent->parent;
1408 if (level == 0)
1409 err = acpi_bus_remove(child, rmdevice);
1410 else
1411 err = acpi_bus_remove(child, 1);
1413 continue;
1416 status = acpi_get_type(chandle, &type);
1417 if (ACPI_FAILURE(status)) {
1418 continue;
1421 * If there is a device corresponding to chandle then
1422 * parse it (depth-first).
1424 if (acpi_bus_get_device(chandle, &child) == 0) {
1425 level++;
1426 phandle = chandle;
1427 chandle = NULL;
1428 parent = child;
1430 continue;
1432 return err;
1434 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1436 static int acpi_bus_scan_fixed(void)
1438 int result = 0;
1439 struct acpi_device *device = NULL;
1440 struct acpi_bus_ops ops;
1442 memset(&ops, 0, sizeof(ops));
1443 ops.acpi_op_add = 1;
1444 ops.acpi_op_start = 1;
1447 * Enumerate all fixed-feature devices.
1449 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1450 result = acpi_add_single_object(&device, NULL,
1451 ACPI_BUS_TYPE_POWER_BUTTON,
1452 ACPI_STA_DEFAULT,
1453 &ops);
1456 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1457 result = acpi_add_single_object(&device, NULL,
1458 ACPI_BUS_TYPE_SLEEP_BUTTON,
1459 ACPI_STA_DEFAULT,
1460 &ops);
1463 return result;
1466 int __init acpi_scan_init(void)
1468 int result;
1469 struct acpi_bus_ops ops;
1471 memset(&ops, 0, sizeof(ops));
1472 ops.acpi_op_add = 1;
1473 ops.acpi_op_start = 1;
1475 result = bus_register(&acpi_bus_type);
1476 if (result) {
1477 /* We don't want to quit even if we failed to add suspend/resume */
1478 printk(KERN_ERR PREFIX "Could not register bus type\n");
1482 * Enumerate devices in the ACPI namespace.
1484 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
1486 if (!result)
1487 result = acpi_bus_scan_fixed();
1489 if (result)
1490 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1492 return result;