2 * scan.c - support for transforming the ACPI namespace into individual objects
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/slab.h>
8 #include <linux/kernel.h>
9 #include <linux/acpi.h>
10 #include <linux/signal.h>
11 #include <linux/kthread.h>
12 #include <linux/dmi.h>
13 #include <linux/nls.h>
15 #include <acpi/acpi_drivers.h>
19 #define _COMPONENT ACPI_BUS_COMPONENT
20 ACPI_MODULE_NAME("scan");
21 #define STRUCT_TO_INT(s) (*((int*)&s))
22 extern struct acpi_device *acpi_root;
24 #define ACPI_BUS_CLASS "system_bus"
25 #define ACPI_BUS_HID "LNXSYBUS"
26 #define ACPI_BUS_DEVICE_NAME "System Bus"
28 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
30 static const char *dummy_hid = "device";
33 * The following ACPI IDs are known to be suitable for representing as
36 static const struct acpi_device_id acpi_platform_device_ids[] = {
43 static LIST_HEAD(acpi_device_list);
44 static LIST_HEAD(acpi_bus_id_list);
45 DEFINE_MUTEX(acpi_device_lock);
46 LIST_HEAD(acpi_wakeup_device_list);
48 struct acpi_device_bus_id{
50 unsigned int instance_no;
51 struct list_head node;
55 * Creates hid/cid(s) string needed for modalias and uevent
56 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
57 * char *modalias: "acpi:IBM0001:ACPI0001"
59 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
64 struct acpi_hardware_id *id;
66 if (list_empty(&acpi_dev->pnp.ids))
69 len = snprintf(modalias, size, "acpi:");
72 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
73 count = snprintf(&modalias[len], size, "%s:", id->id);
74 if (count < 0 || count >= size)
85 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
86 struct acpi_device *acpi_dev = to_acpi_device(dev);
89 /* Device has no HID and no CID or string is >1024 */
90 len = create_modalias(acpi_dev, buf, 1024);
96 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
99 * acpi_bus_hot_remove_device: hot-remove a device and its children
100 * @context: struct acpi_eject_event pointer (freed in this func)
102 * Hot-remove a device and its children. This function frees up the
103 * memory space passed by arg context, so that the caller may call
104 * this function asynchronously through acpi_os_hotplug_execute().
106 void acpi_bus_hot_remove_device(void *context)
108 struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context;
109 struct acpi_device *device;
110 acpi_handle handle = ej_event->handle;
111 struct acpi_object_list arg_list;
112 union acpi_object arg;
113 acpi_status status = AE_OK;
114 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
116 if (acpi_bus_get_device(handle, &device))
122 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
123 "Hot-removing device %s...\n", dev_name(&device->dev)));
125 if (acpi_bus_trim(device, 1)) {
126 printk(KERN_ERR PREFIX
127 "Removing device failed\n");
131 /* power off device */
132 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
133 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
134 printk(KERN_WARNING PREFIX
135 "Power-off device failed\n");
137 if (device->flags.lockable) {
139 arg_list.pointer = &arg;
140 arg.type = ACPI_TYPE_INTEGER;
141 arg.integer.value = 0;
142 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
146 arg_list.pointer = &arg;
147 arg.type = ACPI_TYPE_INTEGER;
148 arg.integer.value = 1;
153 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
154 if (ACPI_FAILURE(status)) {
155 if (status != AE_NOT_FOUND)
156 printk(KERN_WARNING PREFIX
157 "Eject device failed\n");
165 /* Inform firmware the hot-remove operation has completed w/ error */
166 (void) acpi_evaluate_hotplug_ost(handle,
167 ej_event->event, ost_code, NULL);
173 acpi_eject_store(struct device *d, struct device_attribute *attr,
174 const char *buf, size_t count)
178 acpi_object_type type = 0;
179 struct acpi_device *acpi_device = to_acpi_device(d);
180 struct acpi_eject_event *ej_event;
182 if ((!count) || (buf[0] != '1')) {
186 if (acpi_device->driver == NULL) {
191 status = acpi_get_type(acpi_device->handle, &type);
192 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
197 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
203 ej_event->handle = acpi_device->handle;
204 if (acpi_device->flags.eject_pending) {
205 /* event originated from ACPI eject notification */
206 ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
207 acpi_device->flags.eject_pending = 0;
209 /* event originated from user */
210 ej_event->event = ACPI_OST_EC_OSPM_EJECT;
211 (void) acpi_evaluate_hotplug_ost(ej_event->handle,
212 ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
215 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, (void *)ej_event);
220 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
223 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
224 struct acpi_device *acpi_dev = to_acpi_device(dev);
226 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
228 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
231 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
232 struct acpi_device *acpi_dev = to_acpi_device(dev);
233 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
236 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
240 result = sprintf(buf, "%s\n", (char*)path.pointer);
245 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
247 /* sysfs file that shows description text from the ACPI _STR method */
248 static ssize_t description_show(struct device *dev,
249 struct device_attribute *attr,
251 struct acpi_device *acpi_dev = to_acpi_device(dev);
254 if (acpi_dev->pnp.str_obj == NULL)
258 * The _STR object contains a Unicode identifier for a device.
259 * We need to convert to utf-8 so it can be displayed.
261 result = utf16s_to_utf8s(
262 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
263 acpi_dev->pnp.str_obj->buffer.length,
264 UTF16_LITTLE_ENDIAN, buf,
267 buf[result++] = '\n';
271 static DEVICE_ATTR(description, 0444, description_show, NULL);
273 static int acpi_device_setup_files(struct acpi_device *dev)
275 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
281 * Devices gotten from FADT don't have a "path" attribute
284 result = device_create_file(&dev->dev, &dev_attr_path);
289 if (!list_empty(&dev->pnp.ids)) {
290 result = device_create_file(&dev->dev, &dev_attr_hid);
294 result = device_create_file(&dev->dev, &dev_attr_modalias);
300 * If device has _STR, 'description' file is created
302 status = acpi_get_handle(dev->handle, "_STR", &temp);
303 if (ACPI_SUCCESS(status)) {
304 status = acpi_evaluate_object(dev->handle, "_STR",
306 if (ACPI_FAILURE(status))
307 buffer.pointer = NULL;
308 dev->pnp.str_obj = buffer.pointer;
309 result = device_create_file(&dev->dev, &dev_attr_description);
315 * If device has _EJ0, 'eject' file is created that is used to trigger
316 * hot-removal function from userland.
318 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
319 if (ACPI_SUCCESS(status))
320 result = device_create_file(&dev->dev, &dev_attr_eject);
325 static void acpi_device_remove_files(struct acpi_device *dev)
331 * If device has _STR, remove 'description' file
333 status = acpi_get_handle(dev->handle, "_STR", &temp);
334 if (ACPI_SUCCESS(status)) {
335 kfree(dev->pnp.str_obj);
336 device_remove_file(&dev->dev, &dev_attr_description);
339 * If device has _EJ0, remove 'eject' file.
341 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
342 if (ACPI_SUCCESS(status))
343 device_remove_file(&dev->dev, &dev_attr_eject);
345 device_remove_file(&dev->dev, &dev_attr_modalias);
346 device_remove_file(&dev->dev, &dev_attr_hid);
348 device_remove_file(&dev->dev, &dev_attr_path);
350 /* --------------------------------------------------------------------------
352 -------------------------------------------------------------------------- */
354 static const struct acpi_device_id *__acpi_match_device(
355 struct acpi_device *device, const struct acpi_device_id *ids)
357 const struct acpi_device_id *id;
358 struct acpi_hardware_id *hwid;
361 * If the device is not present, it is unnecessary to load device
364 if (!device->status.present)
367 for (id = ids; id->id[0]; id++)
368 list_for_each_entry(hwid, &device->pnp.ids, list)
369 if (!strcmp((char *) id->id, hwid->id))
376 * acpi_match_device - Match a struct device against a given list of ACPI IDs
377 * @ids: Array of struct acpi_device_id object to match against.
378 * @dev: The device structure to match.
380 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
381 * object for that handle and use that object to match against a given list of
384 * Return a pointer to the first matching ID on success or %NULL on failure.
386 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
387 const struct device *dev)
389 struct acpi_device *adev;
391 if (!ids || !ACPI_HANDLE(dev)
392 || ACPI_FAILURE(acpi_bus_get_device(ACPI_HANDLE(dev), &adev)))
395 return __acpi_match_device(adev, ids);
397 EXPORT_SYMBOL_GPL(acpi_match_device);
399 int acpi_match_device_ids(struct acpi_device *device,
400 const struct acpi_device_id *ids)
402 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
404 EXPORT_SYMBOL(acpi_match_device_ids);
406 static void acpi_free_ids(struct acpi_device *device)
408 struct acpi_hardware_id *id, *tmp;
410 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
416 static void acpi_device_release(struct device *dev)
418 struct acpi_device *acpi_dev = to_acpi_device(dev);
420 acpi_free_ids(acpi_dev);
424 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
426 struct acpi_device *acpi_dev = to_acpi_device(dev);
427 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
429 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
432 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
434 struct acpi_device *acpi_dev = to_acpi_device(dev);
437 if (list_empty(&acpi_dev->pnp.ids))
440 if (add_uevent_var(env, "MODALIAS="))
442 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
443 sizeof(env->buf) - env->buflen);
444 if (len >= (sizeof(env->buf) - env->buflen))
450 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
452 struct acpi_device *device = data;
454 device->driver->ops.notify(device, event);
457 static acpi_status acpi_device_notify_fixed(void *data)
459 struct acpi_device *device = data;
461 /* Fixed hardware devices have no handles */
462 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
466 static int acpi_device_install_notify_handler(struct acpi_device *device)
470 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
472 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
473 acpi_device_notify_fixed,
475 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
477 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
478 acpi_device_notify_fixed,
481 status = acpi_install_notify_handler(device->handle,
486 if (ACPI_FAILURE(status))
491 static void acpi_device_remove_notify_handler(struct acpi_device *device)
493 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
494 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
495 acpi_device_notify_fixed);
496 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
497 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
498 acpi_device_notify_fixed);
500 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
504 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
505 static int acpi_start_single_object(struct acpi_device *);
506 static int acpi_device_probe(struct device * dev)
508 struct acpi_device *acpi_dev = to_acpi_device(dev);
509 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
512 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
514 if (acpi_dev->bus_ops.acpi_op_start)
515 acpi_start_single_object(acpi_dev);
517 if (acpi_drv->ops.notify) {
518 ret = acpi_device_install_notify_handler(acpi_dev);
520 if (acpi_drv->ops.remove)
521 acpi_drv->ops.remove(acpi_dev,
522 acpi_dev->removal_type);
527 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
528 "Found driver [%s] for device [%s]\n",
529 acpi_drv->name, acpi_dev->pnp.bus_id));
535 static int acpi_device_remove(struct device * dev)
537 struct acpi_device *acpi_dev = to_acpi_device(dev);
538 struct acpi_driver *acpi_drv = acpi_dev->driver;
541 if (acpi_drv->ops.notify)
542 acpi_device_remove_notify_handler(acpi_dev);
543 if (acpi_drv->ops.remove)
544 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
546 acpi_dev->driver = NULL;
547 acpi_dev->driver_data = NULL;
553 struct bus_type acpi_bus_type = {
555 .match = acpi_bus_match,
556 .probe = acpi_device_probe,
557 .remove = acpi_device_remove,
558 .uevent = acpi_device_uevent,
561 static int acpi_device_register(struct acpi_device *device)
564 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
570 * Link this device to its parent and siblings.
572 INIT_LIST_HEAD(&device->children);
573 INIT_LIST_HEAD(&device->node);
574 INIT_LIST_HEAD(&device->wakeup_list);
575 INIT_LIST_HEAD(&device->physical_node_list);
576 mutex_init(&device->physical_node_lock);
578 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
580 printk(KERN_ERR PREFIX "Memory allocation error\n");
584 mutex_lock(&acpi_device_lock);
586 * Find suitable bus_id and instance number in acpi_bus_id_list
587 * If failed, create one and link it into acpi_bus_id_list
589 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
590 if (!strcmp(acpi_device_bus_id->bus_id,
591 acpi_device_hid(device))) {
592 acpi_device_bus_id->instance_no++;
599 acpi_device_bus_id = new_bus_id;
600 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
601 acpi_device_bus_id->instance_no = 0;
602 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
604 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
607 list_add_tail(&device->node, &device->parent->children);
609 if (device->wakeup.flags.valid)
610 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
611 mutex_unlock(&acpi_device_lock);
614 device->dev.parent = &device->parent->dev;
615 device->dev.bus = &acpi_bus_type;
616 device->dev.release = &acpi_device_release;
617 result = device_register(&device->dev);
619 dev_err(&device->dev, "Error registering device\n");
623 result = acpi_device_setup_files(device);
625 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
626 dev_name(&device->dev));
628 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
631 mutex_lock(&acpi_device_lock);
633 list_del(&device->node);
634 list_del(&device->wakeup_list);
635 mutex_unlock(&acpi_device_lock);
639 static void acpi_device_unregister(struct acpi_device *device, int type)
641 mutex_lock(&acpi_device_lock);
643 list_del(&device->node);
645 list_del(&device->wakeup_list);
646 mutex_unlock(&acpi_device_lock);
648 acpi_detach_data(device->handle, acpi_bus_data_handler);
650 acpi_device_remove_files(device);
651 device_unregister(&device->dev);
654 /* --------------------------------------------------------------------------
656 -------------------------------------------------------------------------- */
658 * acpi_bus_driver_init - add a device to a driver
659 * @device: the device to add and initialize
660 * @driver: driver for the device
662 * Used to initialize a device via its device driver. Called whenever a
663 * driver is bound to a device. Invokes the driver's add() ops.
666 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
670 if (!device || !driver)
673 if (!driver->ops.add)
676 result = driver->ops.add(device);
678 device->driver = NULL;
679 device->driver_data = NULL;
683 device->driver = driver;
686 * TBD - Configuration Management: Assign resources to device based
687 * upon possible configuration and currently allocated resources.
690 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
691 "Driver successfully bound to device\n"));
695 static int acpi_start_single_object(struct acpi_device *device)
698 struct acpi_driver *driver;
701 if (!(driver = device->driver))
704 if (driver->ops.start) {
705 result = driver->ops.start(device);
706 if (result && driver->ops.remove)
707 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
714 * acpi_bus_register_driver - register a driver with the ACPI bus
715 * @driver: driver being registered
717 * Registers a driver with the ACPI bus. Searches the namespace for all
718 * devices that match the driver's criteria and binds. Returns zero for
719 * success or a negative error status for failure.
721 int acpi_bus_register_driver(struct acpi_driver *driver)
727 driver->drv.name = driver->name;
728 driver->drv.bus = &acpi_bus_type;
729 driver->drv.owner = driver->owner;
731 ret = driver_register(&driver->drv);
735 EXPORT_SYMBOL(acpi_bus_register_driver);
738 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
739 * @driver: driver to unregister
741 * Unregisters a driver with the ACPI bus. Searches the namespace for all
742 * devices that match the driver's criteria and unbinds.
744 void acpi_bus_unregister_driver(struct acpi_driver *driver)
746 driver_unregister(&driver->drv);
749 EXPORT_SYMBOL(acpi_bus_unregister_driver);
751 /* --------------------------------------------------------------------------
753 -------------------------------------------------------------------------- */
754 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
758 struct acpi_device *device;
761 * Fixed hardware devices do not appear in the namespace and do not
762 * have handles, but we fabricate acpi_devices for them, so we have
763 * to deal with them specially.
769 status = acpi_get_parent(handle, &handle);
770 if (status == AE_NULL_ENTRY)
772 if (ACPI_FAILURE(status))
775 ret = acpi_bus_get_device(handle, &device);
782 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
786 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
787 union acpi_object *obj;
789 status = acpi_get_handle(handle, "_EJD", &tmp);
790 if (ACPI_FAILURE(status))
793 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
794 if (ACPI_SUCCESS(status)) {
795 obj = buffer.pointer;
796 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
798 kfree(buffer.pointer);
802 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
804 void acpi_bus_data_handler(acpi_handle handle, void *context)
812 static int acpi_bus_get_perf_flags(struct acpi_device *device)
814 device->performance.state = ACPI_STATE_UNKNOWN;
819 acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
820 struct acpi_device_wakeup *wakeup)
822 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
823 union acpi_object *package = NULL;
824 union acpi_object *element = NULL;
829 return AE_BAD_PARAMETER;
832 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
833 if (ACPI_FAILURE(status)) {
834 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
838 package = (union acpi_object *)buffer.pointer;
840 if (!package || (package->package.count < 2)) {
841 status = AE_BAD_DATA;
845 element = &(package->package.elements[0]);
847 status = AE_BAD_DATA;
850 if (element->type == ACPI_TYPE_PACKAGE) {
851 if ((element->package.count < 2) ||
852 (element->package.elements[0].type !=
853 ACPI_TYPE_LOCAL_REFERENCE)
854 || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
855 status = AE_BAD_DATA;
859 element->package.elements[0].reference.handle;
861 (u32) element->package.elements[1].integer.value;
862 } else if (element->type == ACPI_TYPE_INTEGER) {
863 wakeup->gpe_device = NULL;
864 wakeup->gpe_number = element->integer.value;
866 status = AE_BAD_DATA;
870 element = &(package->package.elements[1]);
871 if (element->type != ACPI_TYPE_INTEGER) {
872 status = AE_BAD_DATA;
875 wakeup->sleep_state = element->integer.value;
877 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
878 status = AE_NO_MEMORY;
881 wakeup->resources.count = package->package.count - 2;
882 for (i = 0; i < wakeup->resources.count; i++) {
883 element = &(package->package.elements[i + 2]);
884 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
885 status = AE_BAD_DATA;
889 wakeup->resources.handles[i] = element->reference.handle;
892 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
895 kfree(buffer.pointer);
900 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
902 struct acpi_device_id button_device_ids[] = {
909 acpi_event_status event_status;
911 device->wakeup.flags.notifier_present = 0;
913 /* Power button, Lid switch always enable wakeup */
914 if (!acpi_match_device_ids(device, button_device_ids)) {
915 device->wakeup.flags.run_wake = 1;
916 device_set_wakeup_capable(&device->dev, true);
920 status = acpi_get_gpe_status(device->wakeup.gpe_device,
921 device->wakeup.gpe_number,
924 device->wakeup.flags.run_wake =
925 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
928 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
931 acpi_status status = 0;
934 /* Presence of _PRW indicates wake capable */
935 status = acpi_get_handle(device->handle, "_PRW", &temp);
936 if (ACPI_FAILURE(status))
939 status = acpi_bus_extract_wakeup_device_power_package(device->handle,
941 if (ACPI_FAILURE(status)) {
942 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
946 device->wakeup.flags.valid = 1;
947 device->wakeup.prepare_count = 0;
948 acpi_bus_set_run_wake_flags(device);
949 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
950 * system for the ACPI device with the _PRW object.
951 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
952 * So it is necessary to call _DSW object first. Only when it is not
953 * present will the _PSW object used.
955 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
957 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
958 "error in _DSW or _PSW evaluation\n"));
961 static void acpi_bus_add_power_resource(acpi_handle handle);
963 static int acpi_bus_get_power_flags(struct acpi_device *device)
965 acpi_status status = 0;
966 acpi_handle handle = NULL;
971 * Power Management Flags
973 status = acpi_get_handle(device->handle, "_PSC", &handle);
974 if (ACPI_SUCCESS(status))
975 device->power.flags.explicit_get = 1;
976 status = acpi_get_handle(device->handle, "_IRC", &handle);
977 if (ACPI_SUCCESS(status))
978 device->power.flags.inrush_current = 1;
981 * Enumerate supported power management states
983 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
984 struct acpi_device_power_state *ps = &device->power.states[i];
985 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
987 /* Evaluate "_PRx" to se if power resources are referenced */
988 acpi_evaluate_reference(device->handle, object_name, NULL,
990 if (ps->resources.count) {
993 device->power.flags.power_resources = 1;
994 for (j = 0; j < ps->resources.count; j++)
995 acpi_bus_add_power_resource(ps->resources.handles[j]);
998 /* Evaluate "_PSx" to see if we can do explicit sets */
999 object_name[2] = 'S';
1000 status = acpi_get_handle(device->handle, object_name, &handle);
1001 if (ACPI_SUCCESS(status))
1002 ps->flags.explicit_set = 1;
1005 * State is valid if there are means to put the device into it.
1006 * D3hot is only valid if _PR3 present.
1008 if (ps->resources.count ||
1009 (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT)) {
1010 ps->flags.valid = 1;
1011 ps->flags.os_accessible = 1;
1014 ps->power = -1; /* Unknown - driver assigned */
1015 ps->latency = -1; /* Unknown - driver assigned */
1018 /* Set defaults for D0 and D3 states (always valid) */
1019 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1020 device->power.states[ACPI_STATE_D0].power = 100;
1021 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1022 device->power.states[ACPI_STATE_D3].power = 0;
1024 /* Set D3cold's explicit_set flag if _PS3 exists. */
1025 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1026 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1028 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1029 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1030 device->power.flags.power_resources)
1031 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1033 acpi_bus_init_power(device);
1038 static int acpi_bus_get_flags(struct acpi_device *device)
1040 acpi_status status = AE_OK;
1041 acpi_handle temp = NULL;
1044 /* Presence of _STA indicates 'dynamic_status' */
1045 status = acpi_get_handle(device->handle, "_STA", &temp);
1046 if (ACPI_SUCCESS(status))
1047 device->flags.dynamic_status = 1;
1049 /* Presence of _RMV indicates 'removable' */
1050 status = acpi_get_handle(device->handle, "_RMV", &temp);
1051 if (ACPI_SUCCESS(status))
1052 device->flags.removable = 1;
1054 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1055 status = acpi_get_handle(device->handle, "_EJD", &temp);
1056 if (ACPI_SUCCESS(status))
1057 device->flags.ejectable = 1;
1059 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1060 if (ACPI_SUCCESS(status))
1061 device->flags.ejectable = 1;
1064 /* Presence of _LCK indicates 'lockable' */
1065 status = acpi_get_handle(device->handle, "_LCK", &temp);
1066 if (ACPI_SUCCESS(status))
1067 device->flags.lockable = 1;
1069 /* Power resources cannot be power manageable. */
1070 if (device->device_type == ACPI_BUS_TYPE_POWER)
1073 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1074 status = acpi_get_handle(device->handle, "_PS0", &temp);
1075 if (ACPI_FAILURE(status))
1076 status = acpi_get_handle(device->handle, "_PR0", &temp);
1077 if (ACPI_SUCCESS(status))
1078 device->flags.power_manageable = 1;
1080 /* TBD: Performance management */
1085 static void acpi_device_get_busid(struct acpi_device *device)
1087 char bus_id[5] = { '?', 0 };
1088 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1094 * The device's Bus ID is simply the object name.
1095 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1097 if (ACPI_IS_ROOT_DEVICE(device)) {
1098 strcpy(device->pnp.bus_id, "ACPI");
1102 switch (device->device_type) {
1103 case ACPI_BUS_TYPE_POWER_BUTTON:
1104 strcpy(device->pnp.bus_id, "PWRF");
1106 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1107 strcpy(device->pnp.bus_id, "SLPF");
1110 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1111 /* Clean up trailing underscores (if any) */
1112 for (i = 3; i > 1; i--) {
1113 if (bus_id[i] == '_')
1118 strcpy(device->pnp.bus_id, bus_id);
1124 * acpi_bay_match - see if a device is an ejectable driver bay
1126 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1127 * then we can safely call it an ejectable drive bay
1129 static int acpi_bay_match(struct acpi_device *device){
1133 acpi_handle phandle;
1135 handle = device->handle;
1137 status = acpi_get_handle(handle, "_EJ0", &tmp);
1138 if (ACPI_FAILURE(status))
1141 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1142 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1143 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1144 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1147 if (acpi_get_parent(handle, &phandle))
1150 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1151 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1152 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1153 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1160 * acpi_dock_match - see if a device has a _DCK method
1162 static int acpi_dock_match(struct acpi_device *device)
1165 return acpi_get_handle(device->handle, "_DCK", &tmp);
1168 const char *acpi_device_hid(struct acpi_device *device)
1170 struct acpi_hardware_id *hid;
1172 if (list_empty(&device->pnp.ids))
1175 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1178 EXPORT_SYMBOL(acpi_device_hid);
1180 static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1182 struct acpi_hardware_id *id;
1184 id = kmalloc(sizeof(*id), GFP_KERNEL);
1188 id->id = kstrdup(dev_id, GFP_KERNEL);
1194 list_add_tail(&id->list, &device->pnp.ids);
1198 * Old IBM workstations have a DSDT bug wherein the SMBus object
1199 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1200 * prefix. Work around this.
1202 static int acpi_ibm_smbus_match(struct acpi_device *device)
1204 acpi_handle h_dummy;
1205 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1208 if (!dmi_name_in_vendors("IBM"))
1211 /* Look for SMBS object */
1212 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1216 if (strcmp("SMBS", path.pointer)) {
1221 /* Does it have the necessary (but misnamed) methods? */
1223 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1224 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1225 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1228 kfree(path.pointer);
1232 static void acpi_device_set_id(struct acpi_device *device)
1235 struct acpi_device_info *info;
1236 struct acpi_pnp_device_id_list *cid_list;
1239 switch (device->device_type) {
1240 case ACPI_BUS_TYPE_DEVICE:
1241 if (ACPI_IS_ROOT_DEVICE(device)) {
1242 acpi_add_id(device, ACPI_SYSTEM_HID);
1246 status = acpi_get_object_info(device->handle, &info);
1247 if (ACPI_FAILURE(status)) {
1248 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1252 if (info->valid & ACPI_VALID_HID)
1253 acpi_add_id(device, info->hardware_id.string);
1254 if (info->valid & ACPI_VALID_CID) {
1255 cid_list = &info->compatible_id_list;
1256 for (i = 0; i < cid_list->count; i++)
1257 acpi_add_id(device, cid_list->ids[i].string);
1259 if (info->valid & ACPI_VALID_ADR) {
1260 device->pnp.bus_address = info->address;
1261 device->flags.bus_address = 1;
1267 * Some devices don't reliably have _HIDs & _CIDs, so add
1268 * synthetic HIDs to make sure drivers can find them.
1270 if (acpi_is_video_device(device))
1271 acpi_add_id(device, ACPI_VIDEO_HID);
1272 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1273 acpi_add_id(device, ACPI_BAY_HID);
1274 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1275 acpi_add_id(device, ACPI_DOCK_HID);
1276 else if (!acpi_ibm_smbus_match(device))
1277 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
1278 else if (!acpi_device_hid(device) &&
1279 ACPI_IS_ROOT_DEVICE(device->parent)) {
1280 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1281 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1282 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1286 case ACPI_BUS_TYPE_POWER:
1287 acpi_add_id(device, ACPI_POWER_HID);
1289 case ACPI_BUS_TYPE_PROCESSOR:
1290 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1292 case ACPI_BUS_TYPE_THERMAL:
1293 acpi_add_id(device, ACPI_THERMAL_HID);
1295 case ACPI_BUS_TYPE_POWER_BUTTON:
1296 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1298 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1299 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1304 static int acpi_device_set_context(struct acpi_device *device)
1311 * Attach this 'struct acpi_device' to the ACPI object. This makes
1312 * resolutions from handle->device very efficient. Fixed hardware
1313 * devices have no handles, so we skip them.
1315 if (!device->handle)
1318 status = acpi_attach_data(device->handle,
1319 acpi_bus_data_handler, device);
1320 if (ACPI_SUCCESS(status))
1323 printk(KERN_ERR PREFIX "Error attaching device data\n");
1327 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1332 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1333 device_release_driver(&dev->dev);
1339 * unbind _ADR-Based Devices when hot removal
1341 if (dev->flags.bus_address) {
1342 if ((dev->parent) && (dev->parent->ops.unbind))
1343 dev->parent->ops.unbind(dev);
1345 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1350 static int acpi_add_single_object(struct acpi_device **child,
1351 acpi_handle handle, int type,
1352 unsigned long long sta,
1353 struct acpi_bus_ops *ops)
1356 struct acpi_device *device;
1357 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1359 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1361 printk(KERN_ERR PREFIX "Memory allocation error\n");
1365 INIT_LIST_HEAD(&device->pnp.ids);
1366 device->device_type = type;
1367 device->handle = handle;
1368 device->parent = acpi_bus_get_parent(handle);
1369 device->bus_ops = *ops; /* workround for not call .start */
1370 STRUCT_TO_INT(device->status) = sta;
1372 acpi_device_get_busid(device);
1377 * Note that we only look for object handles -- cannot evaluate objects
1378 * until we know the device is present and properly initialized.
1380 result = acpi_bus_get_flags(device);
1387 * TBD: Synch with Core's enumeration/initialization process.
1389 acpi_device_set_id(device);
1395 if (device->flags.power_manageable) {
1396 result = acpi_bus_get_power_flags(device);
1402 * Wakeup device management
1403 *-----------------------
1405 acpi_bus_get_wakeup_device_flags(device);
1408 * Performance Management
1409 * ----------------------
1411 if (device->flags.performance_manageable) {
1412 result = acpi_bus_get_perf_flags(device);
1417 if ((result = acpi_device_set_context(device)))
1420 result = acpi_device_register(device);
1423 * Bind _ADR-Based Devices when hot add
1425 if (device->flags.bus_address) {
1426 if (device->parent && device->parent->ops.bind)
1427 device->parent->ops.bind(device);
1432 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1433 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1434 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1435 (char *) buffer.pointer,
1436 device->parent ? dev_name(&device->parent->dev) :
1438 kfree(buffer.pointer);
1441 acpi_device_release(&device->dev);
1446 #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1447 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1449 static void acpi_bus_add_power_resource(acpi_handle handle)
1451 struct acpi_bus_ops ops = {
1455 struct acpi_device *device = NULL;
1457 acpi_bus_get_device(handle, &device);
1459 acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
1460 ACPI_STA_DEFAULT, &ops);
1463 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1464 unsigned long long *sta)
1467 acpi_object_type acpi_type;
1469 status = acpi_get_type(handle, &acpi_type);
1470 if (ACPI_FAILURE(status))
1473 switch (acpi_type) {
1474 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1475 case ACPI_TYPE_DEVICE:
1476 *type = ACPI_BUS_TYPE_DEVICE;
1477 status = acpi_bus_get_status_handle(handle, sta);
1478 if (ACPI_FAILURE(status))
1481 case ACPI_TYPE_PROCESSOR:
1482 *type = ACPI_BUS_TYPE_PROCESSOR;
1483 status = acpi_bus_get_status_handle(handle, sta);
1484 if (ACPI_FAILURE(status))
1487 case ACPI_TYPE_THERMAL:
1488 *type = ACPI_BUS_TYPE_THERMAL;
1489 *sta = ACPI_STA_DEFAULT;
1491 case ACPI_TYPE_POWER:
1492 *type = ACPI_BUS_TYPE_POWER;
1493 *sta = ACPI_STA_DEFAULT;
1502 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1503 void *context, void **return_value)
1505 struct acpi_bus_ops *ops = context;
1507 unsigned long long sta;
1508 struct acpi_device *device;
1512 result = acpi_bus_type_and_status(handle, &type, &sta);
1516 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1517 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
1518 struct acpi_device_wakeup wakeup;
1521 status = acpi_get_handle(handle, "_PRW", &temp);
1522 if (ACPI_SUCCESS(status))
1523 acpi_bus_extract_wakeup_device_power_package(handle,
1525 return AE_CTRL_DEPTH;
1529 * We may already have an acpi_device from a previous enumeration. If
1530 * so, we needn't add it again, but we may still have to start it.
1533 acpi_bus_get_device(handle, &device);
1534 if (ops->acpi_op_add && !device) {
1535 acpi_add_single_object(&device, handle, type, sta, ops);
1536 /* Is the device a known good platform device? */
1538 && !acpi_match_device_ids(device, acpi_platform_device_ids))
1539 acpi_create_platform_device(device);
1543 return AE_CTRL_DEPTH;
1545 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1546 status = acpi_start_single_object(device);
1547 if (ACPI_FAILURE(status))
1548 return AE_CTRL_DEPTH;
1552 *return_value = device;
1556 static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1557 struct acpi_device **child)
1560 void *device = NULL;
1562 status = acpi_bus_check_add(handle, 0, ops, &device);
1563 if (ACPI_SUCCESS(status))
1564 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1565 acpi_bus_check_add, NULL, ops, &device);
1577 * acpi_bus_add and acpi_bus_start
1579 * scan a given ACPI tree and (probably recently hot-plugged)
1580 * create and add or starts found devices.
1582 * If no devices were found -ENODEV is returned which does not
1583 * mean that this is a real error, there just have been no suitable
1584 * ACPI objects in the table trunk from which the kernel could create
1585 * a device and add/start an appropriate driver.
1589 acpi_bus_add(struct acpi_device **child,
1590 struct acpi_device *parent, acpi_handle handle, int type)
1592 struct acpi_bus_ops ops;
1594 memset(&ops, 0, sizeof(ops));
1595 ops.acpi_op_add = 1;
1597 return acpi_bus_scan(handle, &ops, child);
1599 EXPORT_SYMBOL(acpi_bus_add);
1601 int acpi_bus_start(struct acpi_device *device)
1603 struct acpi_bus_ops ops;
1609 memset(&ops, 0, sizeof(ops));
1610 ops.acpi_op_start = 1;
1612 result = acpi_bus_scan(device->handle, &ops, NULL);
1614 acpi_update_all_gpes();
1618 EXPORT_SYMBOL(acpi_bus_start);
1620 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1623 struct acpi_device *parent, *child;
1624 acpi_handle phandle, chandle;
1625 acpi_object_type type;
1630 phandle = start->handle;
1631 child = chandle = NULL;
1633 while ((level > 0) && parent && (!err)) {
1634 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1638 * If this scope is exhausted then move our way back up.
1640 if (ACPI_FAILURE(status)) {
1643 acpi_get_parent(phandle, &phandle);
1645 parent = parent->parent;
1648 err = acpi_bus_remove(child, rmdevice);
1650 err = acpi_bus_remove(child, 1);
1655 status = acpi_get_type(chandle, &type);
1656 if (ACPI_FAILURE(status)) {
1660 * If there is a device corresponding to chandle then
1661 * parse it (depth-first).
1663 if (acpi_bus_get_device(chandle, &child) == 0) {
1673 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1675 static int acpi_bus_scan_fixed(void)
1678 struct acpi_device *device = NULL;
1679 struct acpi_bus_ops ops;
1681 memset(&ops, 0, sizeof(ops));
1682 ops.acpi_op_add = 1;
1683 ops.acpi_op_start = 1;
1686 * Enumerate all fixed-feature devices.
1688 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1689 result = acpi_add_single_object(&device, NULL,
1690 ACPI_BUS_TYPE_POWER_BUTTON,
1693 device_init_wakeup(&device->dev, true);
1696 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1697 result = acpi_add_single_object(&device, NULL,
1698 ACPI_BUS_TYPE_SLEEP_BUTTON,
1706 int __init acpi_scan_init(void)
1709 struct acpi_bus_ops ops;
1711 memset(&ops, 0, sizeof(ops));
1712 ops.acpi_op_add = 1;
1713 ops.acpi_op_start = 1;
1715 result = bus_register(&acpi_bus_type);
1717 /* We don't want to quit even if we failed to add suspend/resume */
1718 printk(KERN_ERR PREFIX "Could not register bus type\n");
1724 * Enumerate devices in the ACPI namespace.
1726 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
1729 result = acpi_bus_scan_fixed();
1732 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1734 acpi_update_all_gpes();