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";
32 static LIST_HEAD(acpi_device_list);
33 static LIST_HEAD(acpi_bus_id_list);
34 static DEFINE_MUTEX(acpi_scan_lock);
35 static LIST_HEAD(acpi_scan_handlers_list);
36 DEFINE_MUTEX(acpi_device_lock);
37 LIST_HEAD(acpi_wakeup_device_list);
39 struct acpi_device_bus_id{
41 unsigned int instance_no;
42 struct list_head node;
45 void acpi_scan_lock_acquire(void)
47 mutex_lock(&acpi_scan_lock);
49 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
51 void acpi_scan_lock_release(void)
53 mutex_unlock(&acpi_scan_lock);
55 EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
57 int acpi_scan_add_handler(struct acpi_scan_handler *handler)
59 if (!handler || !handler->attach)
62 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
66 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
67 const char *hotplug_profile_name)
71 error = acpi_scan_add_handler(handler);
75 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
80 * Creates hid/cid(s) string needed for modalias and uevent
81 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
82 * char *modalias: "acpi:IBM0001:ACPI0001"
84 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
89 struct acpi_hardware_id *id;
91 if (list_empty(&acpi_dev->pnp.ids))
94 len = snprintf(modalias, size, "acpi:");
97 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
98 count = snprintf(&modalias[len], size, "%s:", id->id);
99 if (count < 0 || count >= size)
105 modalias[len] = '\0';
110 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
111 struct acpi_device *acpi_dev = to_acpi_device(dev);
114 /* Device has no HID and no CID or string is >1024 */
115 len = create_modalias(acpi_dev, buf, 1024);
121 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
123 static int acpi_scan_hot_remove(struct acpi_device *device)
125 acpi_handle handle = device->handle;
126 acpi_handle not_used;
127 struct acpi_object_list arg_list;
128 union acpi_object arg;
131 /* If there is no handle, the device node has been unregistered. */
133 dev_dbg(&device->dev, "ACPI handle missing\n");
134 put_device(&device->dev);
138 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
139 "Hot-removing device %s...\n", dev_name(&device->dev)));
141 acpi_bus_trim(device);
142 /* Device node has been unregistered. */
143 put_device(&device->dev);
146 if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", ¬_used))) {
148 arg_list.pointer = &arg;
149 arg.type = ACPI_TYPE_INTEGER;
150 arg.integer.value = 0;
151 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
155 arg_list.pointer = &arg;
156 arg.type = ACPI_TYPE_INTEGER;
157 arg.integer.value = 1;
162 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
163 if (ACPI_FAILURE(status)) {
164 if (status == AE_NOT_FOUND) {
167 acpi_handle_warn(handle, "Eject failed\n");
174 static void acpi_bus_device_eject(void *context)
176 acpi_handle handle = context;
177 struct acpi_device *device = NULL;
178 struct acpi_scan_handler *handler;
179 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
181 mutex_lock(&acpi_scan_lock);
183 acpi_bus_get_device(handle, &device);
187 handler = device->handler;
188 if (!handler || !handler->hotplug.enabled) {
189 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
192 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
193 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
194 if (handler->hotplug.mode == AHM_CONTAINER) {
195 device->flags.eject_pending = true;
196 kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
200 get_device(&device->dev);
201 error = acpi_scan_hot_remove(device);
207 mutex_unlock(&acpi_scan_lock);
211 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST, ost_code,
216 static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
218 struct acpi_device *device = NULL;
219 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
222 mutex_lock(&acpi_scan_lock);
224 acpi_bus_get_device(handle, &device);
226 dev_warn(&device->dev, "Attempt to re-insert\n");
229 acpi_evaluate_hotplug_ost(handle, ost_source,
230 ACPI_OST_SC_INSERT_IN_PROGRESS, NULL);
231 error = acpi_bus_scan(handle);
233 acpi_handle_warn(handle, "Namespace scan failure\n");
236 error = acpi_bus_get_device(handle, &device);
238 acpi_handle_warn(handle, "Missing device node object\n");
241 ost_code = ACPI_OST_SC_SUCCESS;
242 if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
243 kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
246 acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
247 mutex_unlock(&acpi_scan_lock);
250 static void acpi_scan_bus_check(void *context)
252 acpi_scan_bus_device_check((acpi_handle)context,
253 ACPI_NOTIFY_BUS_CHECK);
256 static void acpi_scan_device_check(void *context)
258 acpi_scan_bus_device_check((acpi_handle)context,
259 ACPI_NOTIFY_DEVICE_CHECK);
262 static void acpi_hotplug_unsupported(acpi_handle handle, u32 type)
267 case ACPI_NOTIFY_BUS_CHECK:
268 acpi_handle_debug(handle,
269 "ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
270 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
272 case ACPI_NOTIFY_DEVICE_CHECK:
273 acpi_handle_debug(handle,
274 "ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
275 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
277 case ACPI_NOTIFY_EJECT_REQUEST:
278 acpi_handle_debug(handle,
279 "ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
280 ost_status = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
283 /* non-hotplug event; possibly handled by other handler */
287 acpi_evaluate_hotplug_ost(handle, type, ost_status, NULL);
290 static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
292 acpi_osd_exec_callback callback;
293 struct acpi_scan_handler *handler = data;
296 if (!handler->hotplug.enabled)
297 return acpi_hotplug_unsupported(handle, type);
300 case ACPI_NOTIFY_BUS_CHECK:
301 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
302 callback = acpi_scan_bus_check;
304 case ACPI_NOTIFY_DEVICE_CHECK:
305 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
306 callback = acpi_scan_device_check;
308 case ACPI_NOTIFY_EJECT_REQUEST:
309 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
310 callback = acpi_bus_device_eject;
313 /* non-hotplug event; possibly handled by other handler */
316 status = acpi_os_hotplug_execute(callback, handle);
317 if (ACPI_FAILURE(status))
318 acpi_evaluate_hotplug_ost(handle, type,
319 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
324 * acpi_bus_hot_remove_device: hot-remove a device and its children
325 * @context: struct acpi_eject_event pointer (freed in this func)
327 * Hot-remove a device and its children. This function frees up the
328 * memory space passed by arg context, so that the caller may call
329 * this function asynchronously through acpi_os_hotplug_execute().
331 void acpi_bus_hot_remove_device(void *context)
333 struct acpi_eject_event *ej_event = context;
334 struct acpi_device *device = ej_event->device;
335 acpi_handle handle = device->handle;
338 mutex_lock(&acpi_scan_lock);
340 error = acpi_scan_hot_remove(device);
342 acpi_evaluate_hotplug_ost(handle, ej_event->event,
343 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
346 mutex_unlock(&acpi_scan_lock);
349 EXPORT_SYMBOL(acpi_bus_hot_remove_device);
351 static ssize_t real_power_state_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
354 struct acpi_device *adev = to_acpi_device(dev);
358 ret = acpi_device_get_power(adev, &state);
362 return sprintf(buf, "%s\n", acpi_power_state_string(state));
365 static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
367 static ssize_t power_state_show(struct device *dev,
368 struct device_attribute *attr, char *buf)
370 struct acpi_device *adev = to_acpi_device(dev);
372 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
375 static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
378 acpi_eject_store(struct device *d, struct device_attribute *attr,
379 const char *buf, size_t count)
381 struct acpi_device *acpi_device = to_acpi_device(d);
382 struct acpi_eject_event *ej_event;
383 acpi_object_type not_used;
388 if (!count || buf[0] != '1')
391 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
392 && !acpi_device->driver)
395 status = acpi_get_type(acpi_device->handle, ¬_used);
396 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
399 mutex_lock(&acpi_scan_lock);
401 if (acpi_device->flags.eject_pending) {
402 /* ACPI eject notification event. */
403 ost_source = ACPI_NOTIFY_EJECT_REQUEST;
404 acpi_device->flags.eject_pending = 0;
406 /* Eject initiated by user space. */
407 ost_source = ACPI_OST_EC_OSPM_EJECT;
409 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
414 acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
415 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
416 ej_event->device = acpi_device;
417 ej_event->event = ost_source;
418 get_device(&acpi_device->dev);
419 status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device, ej_event);
420 if (ACPI_FAILURE(status)) {
421 put_device(&acpi_device->dev);
423 ret = status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
429 mutex_unlock(&acpi_scan_lock);
433 acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
434 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
438 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
441 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
442 struct acpi_device *acpi_dev = to_acpi_device(dev);
444 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
446 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
448 static ssize_t acpi_device_uid_show(struct device *dev,
449 struct device_attribute *attr, char *buf)
451 struct acpi_device *acpi_dev = to_acpi_device(dev);
453 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
455 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
457 static ssize_t acpi_device_adr_show(struct device *dev,
458 struct device_attribute *attr, char *buf)
460 struct acpi_device *acpi_dev = to_acpi_device(dev);
462 return sprintf(buf, "0x%08x\n",
463 (unsigned int)(acpi_dev->pnp.bus_address));
465 static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
468 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
469 struct acpi_device *acpi_dev = to_acpi_device(dev);
470 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
473 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
477 result = sprintf(buf, "%s\n", (char*)path.pointer);
482 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
484 /* sysfs file that shows description text from the ACPI _STR method */
485 static ssize_t description_show(struct device *dev,
486 struct device_attribute *attr,
488 struct acpi_device *acpi_dev = to_acpi_device(dev);
491 if (acpi_dev->pnp.str_obj == NULL)
495 * The _STR object contains a Unicode identifier for a device.
496 * We need to convert to utf-8 so it can be displayed.
498 result = utf16s_to_utf8s(
499 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
500 acpi_dev->pnp.str_obj->buffer.length,
501 UTF16_LITTLE_ENDIAN, buf,
504 buf[result++] = '\n';
508 static DEVICE_ATTR(description, 0444, description_show, NULL);
511 acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
513 struct acpi_device *acpi_dev = to_acpi_device(dev);
515 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
517 static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
519 static int acpi_device_setup_files(struct acpi_device *dev)
521 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
524 unsigned long long sun;
528 * Devices gotten from FADT don't have a "path" attribute
531 result = device_create_file(&dev->dev, &dev_attr_path);
536 if (!list_empty(&dev->pnp.ids)) {
537 result = device_create_file(&dev->dev, &dev_attr_hid);
541 result = device_create_file(&dev->dev, &dev_attr_modalias);
547 * If device has _STR, 'description' file is created
549 status = acpi_get_handle(dev->handle, "_STR", &temp);
550 if (ACPI_SUCCESS(status)) {
551 status = acpi_evaluate_object(dev->handle, "_STR",
553 if (ACPI_FAILURE(status))
554 buffer.pointer = NULL;
555 dev->pnp.str_obj = buffer.pointer;
556 result = device_create_file(&dev->dev, &dev_attr_description);
561 if (dev->pnp.type.bus_address)
562 result = device_create_file(&dev->dev, &dev_attr_adr);
563 if (dev->pnp.unique_id)
564 result = device_create_file(&dev->dev, &dev_attr_uid);
566 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
567 if (ACPI_SUCCESS(status)) {
568 dev->pnp.sun = (unsigned long)sun;
569 result = device_create_file(&dev->dev, &dev_attr_sun);
573 dev->pnp.sun = (unsigned long)-1;
577 * If device has _EJ0, 'eject' file is created that is used to trigger
578 * hot-removal function from userland.
580 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
581 if (ACPI_SUCCESS(status)) {
582 result = device_create_file(&dev->dev, &dev_attr_eject);
587 if (dev->flags.power_manageable) {
588 result = device_create_file(&dev->dev, &dev_attr_power_state);
592 if (dev->power.flags.power_resources)
593 result = device_create_file(&dev->dev,
594 &dev_attr_real_power_state);
601 static void acpi_device_remove_files(struct acpi_device *dev)
606 if (dev->flags.power_manageable) {
607 device_remove_file(&dev->dev, &dev_attr_power_state);
608 if (dev->power.flags.power_resources)
609 device_remove_file(&dev->dev,
610 &dev_attr_real_power_state);
614 * If device has _STR, remove 'description' file
616 status = acpi_get_handle(dev->handle, "_STR", &temp);
617 if (ACPI_SUCCESS(status)) {
618 kfree(dev->pnp.str_obj);
619 device_remove_file(&dev->dev, &dev_attr_description);
622 * If device has _EJ0, remove 'eject' file.
624 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
625 if (ACPI_SUCCESS(status))
626 device_remove_file(&dev->dev, &dev_attr_eject);
628 status = acpi_get_handle(dev->handle, "_SUN", &temp);
629 if (ACPI_SUCCESS(status))
630 device_remove_file(&dev->dev, &dev_attr_sun);
632 if (dev->pnp.unique_id)
633 device_remove_file(&dev->dev, &dev_attr_uid);
634 if (dev->pnp.type.bus_address)
635 device_remove_file(&dev->dev, &dev_attr_adr);
636 device_remove_file(&dev->dev, &dev_attr_modalias);
637 device_remove_file(&dev->dev, &dev_attr_hid);
639 device_remove_file(&dev->dev, &dev_attr_path);
641 /* --------------------------------------------------------------------------
643 -------------------------------------------------------------------------- */
645 static const struct acpi_device_id *__acpi_match_device(
646 struct acpi_device *device, const struct acpi_device_id *ids)
648 const struct acpi_device_id *id;
649 struct acpi_hardware_id *hwid;
652 * If the device is not present, it is unnecessary to load device
655 if (!device->status.present)
658 for (id = ids; id->id[0]; id++)
659 list_for_each_entry(hwid, &device->pnp.ids, list)
660 if (!strcmp((char *) id->id, hwid->id))
667 * acpi_match_device - Match a struct device against a given list of ACPI IDs
668 * @ids: Array of struct acpi_device_id object to match against.
669 * @dev: The device structure to match.
671 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
672 * object for that handle and use that object to match against a given list of
675 * Return a pointer to the first matching ID on success or %NULL on failure.
677 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
678 const struct device *dev)
680 struct acpi_device *adev;
681 acpi_handle handle = ACPI_HANDLE(dev);
683 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
686 return __acpi_match_device(adev, ids);
688 EXPORT_SYMBOL_GPL(acpi_match_device);
690 int acpi_match_device_ids(struct acpi_device *device,
691 const struct acpi_device_id *ids)
693 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
695 EXPORT_SYMBOL(acpi_match_device_ids);
697 static void acpi_free_power_resources_lists(struct acpi_device *device)
701 if (device->wakeup.flags.valid)
702 acpi_power_resources_list_free(&device->wakeup.resources);
704 if (!device->flags.power_manageable)
707 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
708 struct acpi_device_power_state *ps = &device->power.states[i];
709 acpi_power_resources_list_free(&ps->resources);
713 static void acpi_device_release(struct device *dev)
715 struct acpi_device *acpi_dev = to_acpi_device(dev);
717 acpi_free_pnp_ids(&acpi_dev->pnp);
718 acpi_free_power_resources_lists(acpi_dev);
722 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
724 struct acpi_device *acpi_dev = to_acpi_device(dev);
725 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
727 return acpi_dev->flags.match_driver
728 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
731 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
733 struct acpi_device *acpi_dev = to_acpi_device(dev);
736 if (list_empty(&acpi_dev->pnp.ids))
739 if (add_uevent_var(env, "MODALIAS="))
741 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
742 sizeof(env->buf) - env->buflen);
743 if (len >= (sizeof(env->buf) - env->buflen))
749 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
751 struct acpi_device *device = data;
753 device->driver->ops.notify(device, event);
756 static acpi_status acpi_device_notify_fixed(void *data)
758 struct acpi_device *device = data;
760 /* Fixed hardware devices have no handles */
761 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
765 static int acpi_device_install_notify_handler(struct acpi_device *device)
769 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
771 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
772 acpi_device_notify_fixed,
774 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
776 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
777 acpi_device_notify_fixed,
780 status = acpi_install_notify_handler(device->handle,
785 if (ACPI_FAILURE(status))
790 static void acpi_device_remove_notify_handler(struct acpi_device *device)
792 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
793 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
794 acpi_device_notify_fixed);
795 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
796 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
797 acpi_device_notify_fixed);
799 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
803 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
804 static int acpi_device_probe(struct device * dev)
806 struct acpi_device *acpi_dev = to_acpi_device(dev);
807 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
810 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
812 if (acpi_drv->ops.notify) {
813 ret = acpi_device_install_notify_handler(acpi_dev);
815 if (acpi_drv->ops.remove)
816 acpi_drv->ops.remove(acpi_dev);
817 acpi_dev->driver = NULL;
818 acpi_dev->driver_data = NULL;
823 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
824 "Found driver [%s] for device [%s]\n",
825 acpi_drv->name, acpi_dev->pnp.bus_id));
831 static int acpi_device_remove(struct device * dev)
833 struct acpi_device *acpi_dev = to_acpi_device(dev);
834 struct acpi_driver *acpi_drv = acpi_dev->driver;
837 if (acpi_drv->ops.notify)
838 acpi_device_remove_notify_handler(acpi_dev);
839 if (acpi_drv->ops.remove)
840 acpi_drv->ops.remove(acpi_dev);
842 acpi_dev->driver = NULL;
843 acpi_dev->driver_data = NULL;
849 struct bus_type acpi_bus_type = {
851 .match = acpi_bus_match,
852 .probe = acpi_device_probe,
853 .remove = acpi_device_remove,
854 .uevent = acpi_device_uevent,
857 int acpi_device_add(struct acpi_device *device,
858 void (*release)(struct device *))
861 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
864 if (device->handle) {
867 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
869 if (ACPI_FAILURE(status)) {
870 acpi_handle_err(device->handle,
871 "Unable to attach device data\n");
879 * Link this device to its parent and siblings.
881 INIT_LIST_HEAD(&device->children);
882 INIT_LIST_HEAD(&device->node);
883 INIT_LIST_HEAD(&device->wakeup_list);
884 INIT_LIST_HEAD(&device->physical_node_list);
885 mutex_init(&device->physical_node_lock);
886 INIT_LIST_HEAD(&device->power_dependent);
888 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
890 pr_err(PREFIX "Memory allocation error\n");
895 mutex_lock(&acpi_device_lock);
897 * Find suitable bus_id and instance number in acpi_bus_id_list
898 * If failed, create one and link it into acpi_bus_id_list
900 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
901 if (!strcmp(acpi_device_bus_id->bus_id,
902 acpi_device_hid(device))) {
903 acpi_device_bus_id->instance_no++;
910 acpi_device_bus_id = new_bus_id;
911 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
912 acpi_device_bus_id->instance_no = 0;
913 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
915 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
918 list_add_tail(&device->node, &device->parent->children);
920 if (device->wakeup.flags.valid)
921 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
922 mutex_unlock(&acpi_device_lock);
925 device->dev.parent = &device->parent->dev;
926 device->dev.bus = &acpi_bus_type;
927 device->dev.release = release;
928 result = device_add(&device->dev);
930 dev_err(&device->dev, "Error registering device\n");
934 result = acpi_device_setup_files(device);
936 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
937 dev_name(&device->dev));
939 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
943 mutex_lock(&acpi_device_lock);
945 list_del(&device->node);
946 list_del(&device->wakeup_list);
947 mutex_unlock(&acpi_device_lock);
950 acpi_detach_data(device->handle, acpi_bus_data_handler);
954 static void acpi_device_unregister(struct acpi_device *device)
956 mutex_lock(&acpi_device_lock);
958 list_del(&device->node);
960 list_del(&device->wakeup_list);
961 mutex_unlock(&acpi_device_lock);
963 acpi_detach_data(device->handle, acpi_bus_data_handler);
965 acpi_power_add_remove_device(device, false);
966 acpi_device_remove_files(device);
968 device->remove(device);
970 device_del(&device->dev);
972 * Transition the device to D3cold to drop the reference counts of all
973 * power resources the device depends on and turn off the ones that have
974 * no more references.
976 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
977 device->handle = NULL;
978 put_device(&device->dev);
981 /* --------------------------------------------------------------------------
983 -------------------------------------------------------------------------- */
985 * acpi_bus_driver_init - add a device to a driver
986 * @device: the device to add and initialize
987 * @driver: driver for the device
989 * Used to initialize a device via its device driver. Called whenever a
990 * driver is bound to a device. Invokes the driver's add() ops.
993 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
997 if (!device || !driver)
1000 if (!driver->ops.add)
1003 result = driver->ops.add(device);
1005 device->driver = NULL;
1006 device->driver_data = NULL;
1010 device->driver = driver;
1013 * TBD - Configuration Management: Assign resources to device based
1014 * upon possible configuration and currently allocated resources.
1017 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1018 "Driver successfully bound to device\n"));
1023 * acpi_bus_register_driver - register a driver with the ACPI bus
1024 * @driver: driver being registered
1026 * Registers a driver with the ACPI bus. Searches the namespace for all
1027 * devices that match the driver's criteria and binds. Returns zero for
1028 * success or a negative error status for failure.
1030 int acpi_bus_register_driver(struct acpi_driver *driver)
1036 driver->drv.name = driver->name;
1037 driver->drv.bus = &acpi_bus_type;
1038 driver->drv.owner = driver->owner;
1040 ret = driver_register(&driver->drv);
1044 EXPORT_SYMBOL(acpi_bus_register_driver);
1047 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
1048 * @driver: driver to unregister
1050 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1051 * devices that match the driver's criteria and unbinds.
1053 void acpi_bus_unregister_driver(struct acpi_driver *driver)
1055 driver_unregister(&driver->drv);
1058 EXPORT_SYMBOL(acpi_bus_unregister_driver);
1060 /* --------------------------------------------------------------------------
1062 -------------------------------------------------------------------------- */
1063 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1065 struct acpi_device *device = NULL;
1069 * Fixed hardware devices do not appear in the namespace and do not
1070 * have handles, but we fabricate acpi_devices for them, so we have
1071 * to deal with them specially.
1077 status = acpi_get_parent(handle, &handle);
1078 if (ACPI_FAILURE(status))
1079 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1080 } while (acpi_bus_get_device(handle, &device));
1085 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1089 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1090 union acpi_object *obj;
1092 status = acpi_get_handle(handle, "_EJD", &tmp);
1093 if (ACPI_FAILURE(status))
1096 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1097 if (ACPI_SUCCESS(status)) {
1098 obj = buffer.pointer;
1099 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1101 kfree(buffer.pointer);
1105 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1107 void acpi_bus_data_handler(acpi_handle handle, void *context)
1115 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1116 struct acpi_device_wakeup *wakeup)
1118 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1119 union acpi_object *package = NULL;
1120 union acpi_object *element = NULL;
1127 INIT_LIST_HEAD(&wakeup->resources);
1130 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1131 if (ACPI_FAILURE(status)) {
1132 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
1136 package = (union acpi_object *)buffer.pointer;
1138 if (!package || package->package.count < 2)
1141 element = &(package->package.elements[0]);
1145 if (element->type == ACPI_TYPE_PACKAGE) {
1146 if ((element->package.count < 2) ||
1147 (element->package.elements[0].type !=
1148 ACPI_TYPE_LOCAL_REFERENCE)
1149 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
1152 wakeup->gpe_device =
1153 element->package.elements[0].reference.handle;
1154 wakeup->gpe_number =
1155 (u32) element->package.elements[1].integer.value;
1156 } else if (element->type == ACPI_TYPE_INTEGER) {
1157 wakeup->gpe_device = NULL;
1158 wakeup->gpe_number = element->integer.value;
1163 element = &(package->package.elements[1]);
1164 if (element->type != ACPI_TYPE_INTEGER)
1167 wakeup->sleep_state = element->integer.value;
1169 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1173 if (!list_empty(&wakeup->resources)) {
1176 err = acpi_power_wakeup_list_init(&wakeup->resources,
1179 acpi_handle_warn(handle, "Retrieving current states "
1180 "of wakeup power resources failed\n");
1181 acpi_power_resources_list_free(&wakeup->resources);
1184 if (sleep_state < wakeup->sleep_state) {
1185 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1186 "(S%d) by S%d from power resources\n",
1187 (int)wakeup->sleep_state, sleep_state);
1188 wakeup->sleep_state = sleep_state;
1191 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
1194 kfree(buffer.pointer);
1198 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
1200 struct acpi_device_id button_device_ids[] = {
1207 acpi_event_status event_status;
1209 device->wakeup.flags.notifier_present = 0;
1211 /* Power button, Lid switch always enable wakeup */
1212 if (!acpi_match_device_ids(device, button_device_ids)) {
1213 device->wakeup.flags.run_wake = 1;
1214 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1215 /* Do not use Lid/sleep button for S5 wakeup */
1216 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1217 device->wakeup.sleep_state = ACPI_STATE_S4;
1219 device_set_wakeup_capable(&device->dev, true);
1223 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1224 device->wakeup.gpe_number,
1226 if (status == AE_OK)
1227 device->wakeup.flags.run_wake =
1228 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1231 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1234 acpi_status status = 0;
1237 /* Presence of _PRW indicates wake capable */
1238 status = acpi_get_handle(device->handle, "_PRW", &temp);
1239 if (ACPI_FAILURE(status))
1242 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1245 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1249 device->wakeup.flags.valid = 1;
1250 device->wakeup.prepare_count = 0;
1251 acpi_bus_set_run_wake_flags(device);
1252 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1253 * system for the ACPI device with the _PRW object.
1254 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1255 * So it is necessary to call _DSW object first. Only when it is not
1256 * present will the _PSW object used.
1258 err = acpi_device_sleep_wake(device, 0, 0, 0);
1260 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1261 "error in _DSW or _PSW evaluation\n"));
1264 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1266 struct acpi_device_power_state *ps = &device->power.states[state];
1267 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1268 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1272 INIT_LIST_HEAD(&ps->resources);
1274 /* Evaluate "_PRx" to get referenced power resources */
1275 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1276 if (ACPI_SUCCESS(status)) {
1277 union acpi_object *package = buffer.pointer;
1279 if (buffer.length && package
1280 && package->type == ACPI_TYPE_PACKAGE
1281 && package->package.count) {
1282 int err = acpi_extract_power_resources(package, 0,
1285 device->power.flags.power_resources = 1;
1287 ACPI_FREE(buffer.pointer);
1290 /* Evaluate "_PSx" to see if we can do explicit sets */
1292 status = acpi_get_handle(device->handle, pathname, &handle);
1293 if (ACPI_SUCCESS(status))
1294 ps->flags.explicit_set = 1;
1297 * State is valid if there are means to put the device into it.
1298 * D3hot is only valid if _PR3 present.
1300 if (!list_empty(&ps->resources)
1301 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1302 ps->flags.valid = 1;
1303 ps->flags.os_accessible = 1;
1306 ps->power = -1; /* Unknown - driver assigned */
1307 ps->latency = -1; /* Unknown - driver assigned */
1310 static void acpi_bus_get_power_flags(struct acpi_device *device)
1316 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1317 status = acpi_get_handle(device->handle, "_PS0", &handle);
1318 if (ACPI_FAILURE(status)) {
1319 status = acpi_get_handle(device->handle, "_PR0", &handle);
1320 if (ACPI_FAILURE(status))
1324 device->flags.power_manageable = 1;
1327 * Power Management Flags
1329 status = acpi_get_handle(device->handle, "_PSC", &handle);
1330 if (ACPI_SUCCESS(status))
1331 device->power.flags.explicit_get = 1;
1332 status = acpi_get_handle(device->handle, "_IRC", &handle);
1333 if (ACPI_SUCCESS(status))
1334 device->power.flags.inrush_current = 1;
1337 * Enumerate supported power management states
1339 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1340 acpi_bus_init_power_state(device, i);
1342 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1344 /* Set defaults for D0 and D3 states (always valid) */
1345 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1346 device->power.states[ACPI_STATE_D0].power = 100;
1347 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1348 device->power.states[ACPI_STATE_D3].power = 0;
1350 /* Set D3cold's explicit_set flag if _PS3 exists. */
1351 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1352 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1354 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1355 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1356 device->power.flags.power_resources)
1357 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1359 if (acpi_bus_init_power(device)) {
1360 acpi_free_power_resources_lists(device);
1361 device->flags.power_manageable = 0;
1365 static void acpi_bus_get_flags(struct acpi_device *device)
1367 acpi_status status = AE_OK;
1368 acpi_handle temp = NULL;
1370 /* Presence of _STA indicates 'dynamic_status' */
1371 status = acpi_get_handle(device->handle, "_STA", &temp);
1372 if (ACPI_SUCCESS(status))
1373 device->flags.dynamic_status = 1;
1375 /* Presence of _RMV indicates 'removable' */
1376 status = acpi_get_handle(device->handle, "_RMV", &temp);
1377 if (ACPI_SUCCESS(status))
1378 device->flags.removable = 1;
1380 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1381 status = acpi_get_handle(device->handle, "_EJD", &temp);
1382 if (ACPI_SUCCESS(status))
1383 device->flags.ejectable = 1;
1385 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1386 if (ACPI_SUCCESS(status))
1387 device->flags.ejectable = 1;
1391 static void acpi_device_get_busid(struct acpi_device *device)
1393 char bus_id[5] = { '?', 0 };
1394 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1400 * The device's Bus ID is simply the object name.
1401 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1403 if (ACPI_IS_ROOT_DEVICE(device)) {
1404 strcpy(device->pnp.bus_id, "ACPI");
1408 switch (device->device_type) {
1409 case ACPI_BUS_TYPE_POWER_BUTTON:
1410 strcpy(device->pnp.bus_id, "PWRF");
1412 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1413 strcpy(device->pnp.bus_id, "SLPF");
1416 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1417 /* Clean up trailing underscores (if any) */
1418 for (i = 3; i > 1; i--) {
1419 if (bus_id[i] == '_')
1424 strcpy(device->pnp.bus_id, bus_id);
1430 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1432 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1433 * then we can safely call it an ejectable drive bay
1435 static int acpi_bay_match(acpi_handle handle)
1439 acpi_handle phandle;
1441 status = acpi_get_handle(handle, "_EJ0", &tmp);
1442 if (ACPI_FAILURE(status))
1445 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1446 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1447 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1448 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1451 if (acpi_get_parent(handle, &phandle))
1454 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1455 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1456 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1457 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1464 * acpi_dock_match - see if an acpi object has a _DCK method
1466 static int acpi_dock_match(acpi_handle handle)
1469 return acpi_get_handle(handle, "_DCK", &tmp);
1472 const char *acpi_device_hid(struct acpi_device *device)
1474 struct acpi_hardware_id *hid;
1476 if (list_empty(&device->pnp.ids))
1479 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1482 EXPORT_SYMBOL(acpi_device_hid);
1484 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1486 struct acpi_hardware_id *id;
1488 id = kmalloc(sizeof(*id), GFP_KERNEL);
1492 id->id = kstrdup(dev_id, GFP_KERNEL);
1498 list_add_tail(&id->list, &pnp->ids);
1499 pnp->type.hardware_id = 1;
1503 * Old IBM workstations have a DSDT bug wherein the SMBus object
1504 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1505 * prefix. Work around this.
1507 static int acpi_ibm_smbus_match(acpi_handle handle)
1509 acpi_handle h_dummy;
1510 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1513 if (!dmi_name_in_vendors("IBM"))
1516 /* Look for SMBS object */
1517 result = acpi_get_name(handle, ACPI_SINGLE_NAME, &path);
1521 if (strcmp("SMBS", path.pointer)) {
1526 /* Does it have the necessary (but misnamed) methods? */
1528 if (ACPI_SUCCESS(acpi_get_handle(handle, "SBI", &h_dummy)) &&
1529 ACPI_SUCCESS(acpi_get_handle(handle, "SBR", &h_dummy)) &&
1530 ACPI_SUCCESS(acpi_get_handle(handle, "SBW", &h_dummy)))
1533 kfree(path.pointer);
1537 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1541 struct acpi_device_info *info;
1542 struct acpi_pnp_device_id_list *cid_list;
1545 switch (device_type) {
1546 case ACPI_BUS_TYPE_DEVICE:
1547 if (handle == ACPI_ROOT_OBJECT) {
1548 acpi_add_id(pnp, ACPI_SYSTEM_HID);
1552 status = acpi_get_object_info(handle, &info);
1553 if (ACPI_FAILURE(status)) {
1554 pr_err(PREFIX "%s: Error reading device info\n",
1559 if (info->valid & ACPI_VALID_HID)
1560 acpi_add_id(pnp, info->hardware_id.string);
1561 if (info->valid & ACPI_VALID_CID) {
1562 cid_list = &info->compatible_id_list;
1563 for (i = 0; i < cid_list->count; i++)
1564 acpi_add_id(pnp, cid_list->ids[i].string);
1566 if (info->valid & ACPI_VALID_ADR) {
1567 pnp->bus_address = info->address;
1568 pnp->type.bus_address = 1;
1570 if (info->valid & ACPI_VALID_UID)
1571 pnp->unique_id = kstrdup(info->unique_id.string,
1577 * Some devices don't reliably have _HIDs & _CIDs, so add
1578 * synthetic HIDs to make sure drivers can find them.
1580 if (acpi_is_video_device(handle))
1581 acpi_add_id(pnp, ACPI_VIDEO_HID);
1582 else if (ACPI_SUCCESS(acpi_bay_match(handle)))
1583 acpi_add_id(pnp, ACPI_BAY_HID);
1584 else if (ACPI_SUCCESS(acpi_dock_match(handle)))
1585 acpi_add_id(pnp, ACPI_DOCK_HID);
1586 else if (!acpi_ibm_smbus_match(handle))
1587 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1588 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1589 acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1590 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1591 strcpy(pnp->device_class, ACPI_BUS_CLASS);
1595 case ACPI_BUS_TYPE_POWER:
1596 acpi_add_id(pnp, ACPI_POWER_HID);
1598 case ACPI_BUS_TYPE_PROCESSOR:
1599 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1601 case ACPI_BUS_TYPE_THERMAL:
1602 acpi_add_id(pnp, ACPI_THERMAL_HID);
1604 case ACPI_BUS_TYPE_POWER_BUTTON:
1605 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1607 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1608 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1613 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1615 struct acpi_hardware_id *id, *tmp;
1617 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1621 kfree(pnp->unique_id);
1624 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1625 int type, unsigned long long sta)
1627 INIT_LIST_HEAD(&device->pnp.ids);
1628 device->device_type = type;
1629 device->handle = handle;
1630 device->parent = acpi_bus_get_parent(handle);
1631 STRUCT_TO_INT(device->status) = sta;
1632 acpi_device_get_busid(device);
1633 acpi_set_pnp_ids(handle, &device->pnp, type);
1634 acpi_bus_get_flags(device);
1635 device->flags.match_driver = false;
1636 device_initialize(&device->dev);
1637 dev_set_uevent_suppress(&device->dev, true);
1640 void acpi_device_add_finalize(struct acpi_device *device)
1642 device->flags.match_driver = true;
1643 dev_set_uevent_suppress(&device->dev, false);
1644 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1647 static int acpi_add_single_object(struct acpi_device **child,
1648 acpi_handle handle, int type,
1649 unsigned long long sta)
1652 struct acpi_device *device;
1653 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1655 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1657 printk(KERN_ERR PREFIX "Memory allocation error\n");
1661 acpi_init_device_object(device, handle, type, sta);
1662 acpi_bus_get_power_flags(device);
1663 acpi_bus_get_wakeup_device_flags(device);
1665 result = acpi_device_add(device, acpi_device_release);
1667 acpi_device_release(&device->dev);
1671 acpi_power_add_remove_device(device, true);
1672 acpi_device_add_finalize(device);
1673 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1674 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1675 dev_name(&device->dev), (char *) buffer.pointer,
1676 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1677 kfree(buffer.pointer);
1682 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1683 unsigned long long *sta)
1686 acpi_object_type acpi_type;
1688 status = acpi_get_type(handle, &acpi_type);
1689 if (ACPI_FAILURE(status))
1692 switch (acpi_type) {
1693 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1694 case ACPI_TYPE_DEVICE:
1695 *type = ACPI_BUS_TYPE_DEVICE;
1696 status = acpi_bus_get_status_handle(handle, sta);
1697 if (ACPI_FAILURE(status))
1700 case ACPI_TYPE_PROCESSOR:
1701 *type = ACPI_BUS_TYPE_PROCESSOR;
1702 status = acpi_bus_get_status_handle(handle, sta);
1703 if (ACPI_FAILURE(status))
1706 case ACPI_TYPE_THERMAL:
1707 *type = ACPI_BUS_TYPE_THERMAL;
1708 *sta = ACPI_STA_DEFAULT;
1710 case ACPI_TYPE_POWER:
1711 *type = ACPI_BUS_TYPE_POWER;
1712 *sta = ACPI_STA_DEFAULT;
1721 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1723 const struct acpi_device_id **matchid)
1725 const struct acpi_device_id *devid;
1727 for (devid = handler->ids; devid->id[0]; devid++)
1728 if (!strcmp((char *)devid->id, idstr)) {
1738 static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1739 const struct acpi_device_id **matchid)
1741 struct acpi_scan_handler *handler;
1743 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1744 if (acpi_scan_handler_matching(handler, idstr, matchid))
1750 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1752 if (!!hotplug->enabled == !!val)
1755 mutex_lock(&acpi_scan_lock);
1757 hotplug->enabled = val;
1759 mutex_unlock(&acpi_scan_lock);
1762 static void acpi_scan_init_hotplug(acpi_handle handle, int type)
1764 struct acpi_device_pnp pnp = {};
1765 struct acpi_hardware_id *hwid;
1766 struct acpi_scan_handler *handler;
1768 INIT_LIST_HEAD(&pnp.ids);
1769 acpi_set_pnp_ids(handle, &pnp, type);
1771 if (!pnp.type.hardware_id)
1775 * This relies on the fact that acpi_install_notify_handler() will not
1776 * install the same notify handler routine twice for the same handle.
1778 list_for_each_entry(hwid, &pnp.ids, list) {
1779 handler = acpi_scan_match_handler(hwid->id, NULL);
1781 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1782 acpi_hotplug_notify_cb, handler);
1787 acpi_free_pnp_ids(&pnp);
1790 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1791 void *not_used, void **return_value)
1793 struct acpi_device *device = NULL;
1795 unsigned long long sta;
1799 acpi_bus_get_device(handle, &device);
1803 result = acpi_bus_type_and_status(handle, &type, &sta);
1807 if (type == ACPI_BUS_TYPE_POWER) {
1808 acpi_add_power_resource(handle);
1812 acpi_scan_init_hotplug(handle, type);
1814 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1815 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
1816 struct acpi_device_wakeup wakeup;
1819 status = acpi_get_handle(handle, "_PRW", &temp);
1820 if (ACPI_SUCCESS(status)) {
1821 acpi_bus_extract_wakeup_device_power_package(handle,
1823 acpi_power_resources_list_free(&wakeup.resources);
1825 return AE_CTRL_DEPTH;
1828 acpi_add_single_object(&device, handle, type, sta);
1830 return AE_CTRL_DEPTH;
1834 *return_value = device;
1839 static int acpi_scan_attach_handler(struct acpi_device *device)
1841 struct acpi_hardware_id *hwid;
1844 list_for_each_entry(hwid, &device->pnp.ids, list) {
1845 const struct acpi_device_id *devid;
1846 struct acpi_scan_handler *handler;
1848 handler = acpi_scan_match_handler(hwid->id, &devid);
1850 ret = handler->attach(device, devid);
1852 device->handler = handler;
1854 } else if (ret < 0) {
1862 static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1863 void *not_used, void **ret_not_used)
1865 struct acpi_device *device;
1866 unsigned long long sta_not_used;
1870 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1871 * namespace walks prematurely.
1873 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
1876 if (acpi_bus_get_device(handle, &device))
1877 return AE_CTRL_DEPTH;
1879 ret = acpi_scan_attach_handler(device);
1881 return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
1883 ret = device_attach(&device->dev);
1884 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
1888 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1889 * @handle: Root of the namespace scope to scan.
1891 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1894 * If no devices were found, -ENODEV is returned, but it does not mean that
1895 * there has been a real error. There just have been no suitable ACPI objects
1896 * in the table trunk from which the kernel could create a device and add an
1897 * appropriate driver.
1899 * Must be called under acpi_scan_lock.
1901 int acpi_bus_scan(acpi_handle handle)
1903 void *device = NULL;
1906 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
1907 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1908 acpi_bus_check_add, NULL, NULL, &device);
1912 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
1913 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1914 acpi_bus_device_attach, NULL, NULL, NULL);
1918 EXPORT_SYMBOL(acpi_bus_scan);
1920 static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1921 void *not_used, void **ret_not_used)
1923 struct acpi_device *device = NULL;
1925 if (!acpi_bus_get_device(handle, &device)) {
1926 struct acpi_scan_handler *dev_handler = device->handler;
1928 device->removal_type = ACPI_BUS_REMOVAL_EJECT;
1930 if (dev_handler->detach)
1931 dev_handler->detach(device);
1933 device->handler = NULL;
1935 device_release_driver(&device->dev);
1941 static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1942 void *not_used, void **ret_not_used)
1944 struct acpi_device *device = NULL;
1946 if (!acpi_bus_get_device(handle, &device))
1947 acpi_device_unregister(device);
1953 * acpi_bus_trim - Remove ACPI device node and all of its descendants
1954 * @start: Root of the ACPI device nodes subtree to remove.
1956 * Must be called under acpi_scan_lock.
1958 void acpi_bus_trim(struct acpi_device *start)
1961 * Execute acpi_bus_device_detach() as a post-order callback to detach
1962 * all ACPI drivers from the device nodes being removed.
1964 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1965 acpi_bus_device_detach, NULL, NULL);
1966 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
1968 * Execute acpi_bus_remove() as a post-order callback to remove device
1969 * nodes in the given namespace scope.
1971 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1972 acpi_bus_remove, NULL, NULL);
1973 acpi_bus_remove(start->handle, 0, NULL, NULL);
1975 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1977 static int acpi_bus_scan_fixed(void)
1982 * Enumerate all fixed-feature devices.
1984 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
1985 struct acpi_device *device = NULL;
1987 result = acpi_add_single_object(&device, NULL,
1988 ACPI_BUS_TYPE_POWER_BUTTON,
1993 result = device_attach(&device->dev);
1997 device_init_wakeup(&device->dev, true);
2000 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2001 struct acpi_device *device = NULL;
2003 result = acpi_add_single_object(&device, NULL,
2004 ACPI_BUS_TYPE_SLEEP_BUTTON,
2009 result = device_attach(&device->dev);
2012 return result < 0 ? result : 0;
2015 int __init acpi_scan_init(void)
2019 result = bus_register(&acpi_bus_type);
2021 /* We don't want to quit even if we failed to add suspend/resume */
2022 printk(KERN_ERR PREFIX "Could not register bus type\n");
2025 acpi_pci_root_init();
2026 acpi_pci_link_init();
2027 acpi_platform_init();
2029 acpi_container_init();
2030 acpi_pci_slot_init();
2031 acpi_memory_hotplug_init();
2033 mutex_lock(&acpi_scan_lock);
2035 * Enumerate devices in the ACPI namespace.
2037 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2041 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2045 result = acpi_bus_scan_fixed();
2047 acpi_device_unregister(acpi_root);
2051 acpi_update_all_gpes();
2053 acpi_pci_root_hp_init();
2056 mutex_unlock(&acpi_scan_lock);