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)
31 * If set, devices will be hot-removed even if they cannot be put offline
32 * gracefully (from the kernel's standpoint).
34 bool acpi_force_hot_remove;
36 static const char *dummy_hid = "device";
38 static LIST_HEAD(acpi_bus_id_list);
39 static DEFINE_MUTEX(acpi_scan_lock);
40 static LIST_HEAD(acpi_scan_handlers_list);
41 DEFINE_MUTEX(acpi_device_lock);
42 LIST_HEAD(acpi_wakeup_device_list);
44 struct acpi_device_bus_id{
46 unsigned int instance_no;
47 struct list_head node;
50 void acpi_scan_lock_acquire(void)
52 mutex_lock(&acpi_scan_lock);
54 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
56 void acpi_scan_lock_release(void)
58 mutex_unlock(&acpi_scan_lock);
60 EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
62 int acpi_scan_add_handler(struct acpi_scan_handler *handler)
64 if (!handler || !handler->attach)
67 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
71 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
72 const char *hotplug_profile_name)
76 error = acpi_scan_add_handler(handler);
80 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
85 * Creates hid/cid(s) string needed for modalias and uevent
86 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
87 * char *modalias: "acpi:IBM0001:ACPI0001"
89 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
94 struct acpi_hardware_id *id;
96 if (list_empty(&acpi_dev->pnp.ids))
99 len = snprintf(modalias, size, "acpi:");
102 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
103 count = snprintf(&modalias[len], size, "%s:", id->id);
104 if (count < 0 || count >= size)
110 modalias[len] = '\0';
115 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
116 struct acpi_device *acpi_dev = to_acpi_device(dev);
119 /* Device has no HID and no CID or string is >1024 */
120 len = create_modalias(acpi_dev, buf, 1024);
126 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
128 static acpi_status acpi_bus_offline_companions(acpi_handle handle, u32 lvl,
129 void *data, void **ret_p)
131 struct acpi_device *device = NULL;
132 struct acpi_device_physical_node *pn;
133 bool second_pass = (bool)data;
134 acpi_status status = AE_OK;
136 if (acpi_bus_get_device(handle, &device))
139 mutex_lock(&device->physical_node_lock);
141 list_for_each_entry(pn, &device->physical_node_list, node) {
145 /* Skip devices offlined by the first pass. */
149 pn->put_online = false;
151 ret = device_offline(pn->dev);
152 if (acpi_force_hot_remove)
156 pn->put_online = !ret;
166 mutex_unlock(&device->physical_node_lock);
171 static acpi_status acpi_bus_online_companions(acpi_handle handle, u32 lvl,
172 void *data, void **ret_p)
174 struct acpi_device *device = NULL;
175 struct acpi_device_physical_node *pn;
177 if (acpi_bus_get_device(handle, &device))
180 mutex_lock(&device->physical_node_lock);
182 list_for_each_entry(pn, &device->physical_node_list, node)
183 if (pn->put_online) {
184 device_online(pn->dev);
185 pn->put_online = false;
188 mutex_unlock(&device->physical_node_lock);
193 static int acpi_scan_hot_remove(struct acpi_device *device)
195 acpi_handle handle = device->handle;
196 struct device *errdev;
198 unsigned long long sta;
200 /* If there is no handle, the device node has been unregistered. */
202 dev_dbg(&device->dev, "ACPI handle missing\n");
203 put_device(&device->dev);
207 lock_device_hotplug();
210 * Carry out two passes here and ignore errors in the first pass,
211 * because if the devices in question are memory blocks and
212 * CONFIG_MEMCG is set, one of the blocks may hold data structures
213 * that the other blocks depend on, but it is not known in advance which
216 * If the first pass is successful, the second one isn't needed, though.
219 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
220 NULL, acpi_bus_offline_companions,
221 (void *)false, (void **)&errdev);
222 acpi_bus_offline_companions(handle, 0, (void *)false, (void **)&errdev);
225 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
226 NULL, acpi_bus_offline_companions,
227 (void *)true , (void **)&errdev);
228 if (!errdev || acpi_force_hot_remove)
229 acpi_bus_offline_companions(handle, 0, (void *)true,
232 if (errdev && !acpi_force_hot_remove) {
233 dev_warn(errdev, "Offline failed.\n");
234 acpi_bus_online_companions(handle, 0, NULL, NULL);
235 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
237 acpi_bus_online_companions, NULL,
240 unlock_device_hotplug();
242 put_device(&device->dev);
247 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
248 "Hot-removing device %s...\n", dev_name(&device->dev)));
250 acpi_bus_trim(device);
252 unlock_device_hotplug();
254 /* Device node has been unregistered. */
255 put_device(&device->dev);
258 acpi_evaluate_lck(handle, 0);
262 status = acpi_evaluate_ej0(handle);
263 if (status == AE_NOT_FOUND)
265 else if (ACPI_FAILURE(status))
269 * Verify if eject was indeed successful. If not, log an error
270 * message. No need to call _OST since _EJ0 call was made OK.
272 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
273 if (ACPI_FAILURE(status)) {
274 acpi_handle_warn(handle,
275 "Status check after eject failed (0x%x)\n", status);
276 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
277 acpi_handle_warn(handle,
278 "Eject incomplete - status 0x%llx\n", sta);
284 static void acpi_bus_device_eject(void *context)
286 acpi_handle handle = context;
287 struct acpi_device *device = NULL;
288 struct acpi_scan_handler *handler;
289 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
291 mutex_lock(&acpi_scan_lock);
293 acpi_bus_get_device(handle, &device);
297 handler = device->handler;
298 if (!handler || !handler->hotplug.enabled) {
299 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
302 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
303 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
304 if (handler->hotplug.mode == AHM_CONTAINER) {
305 device->flags.eject_pending = true;
306 kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
310 get_device(&device->dev);
311 error = acpi_scan_hot_remove(device);
317 mutex_unlock(&acpi_scan_lock);
321 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST, ost_code,
326 static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
328 struct acpi_device *device = NULL;
329 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
332 mutex_lock(&acpi_scan_lock);
333 lock_device_hotplug();
335 acpi_bus_get_device(handle, &device);
337 dev_warn(&device->dev, "Attempt to re-insert\n");
340 acpi_evaluate_hotplug_ost(handle, ost_source,
341 ACPI_OST_SC_INSERT_IN_PROGRESS, NULL);
342 error = acpi_bus_scan(handle);
344 acpi_handle_warn(handle, "Namespace scan failure\n");
347 error = acpi_bus_get_device(handle, &device);
349 acpi_handle_warn(handle, "Missing device node object\n");
352 ost_code = ACPI_OST_SC_SUCCESS;
353 if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
354 kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
357 unlock_device_hotplug();
358 acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
359 mutex_unlock(&acpi_scan_lock);
362 static void acpi_scan_bus_check(void *context)
364 acpi_scan_bus_device_check((acpi_handle)context,
365 ACPI_NOTIFY_BUS_CHECK);
368 static void acpi_scan_device_check(void *context)
370 acpi_scan_bus_device_check((acpi_handle)context,
371 ACPI_NOTIFY_DEVICE_CHECK);
374 static void acpi_hotplug_unsupported(acpi_handle handle, u32 type)
379 case ACPI_NOTIFY_BUS_CHECK:
380 acpi_handle_debug(handle,
381 "ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
382 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
384 case ACPI_NOTIFY_DEVICE_CHECK:
385 acpi_handle_debug(handle,
386 "ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
387 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
389 case ACPI_NOTIFY_EJECT_REQUEST:
390 acpi_handle_debug(handle,
391 "ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
392 ost_status = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
395 /* non-hotplug event; possibly handled by other handler */
399 acpi_evaluate_hotplug_ost(handle, type, ost_status, NULL);
402 static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
404 acpi_osd_exec_callback callback;
405 struct acpi_scan_handler *handler = data;
408 if (!handler->hotplug.enabled)
409 return acpi_hotplug_unsupported(handle, type);
412 case ACPI_NOTIFY_BUS_CHECK:
413 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
414 callback = acpi_scan_bus_check;
416 case ACPI_NOTIFY_DEVICE_CHECK:
417 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
418 callback = acpi_scan_device_check;
420 case ACPI_NOTIFY_EJECT_REQUEST:
421 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
422 callback = acpi_bus_device_eject;
425 /* non-hotplug event; possibly handled by other handler */
428 status = acpi_os_hotplug_execute(callback, handle);
429 if (ACPI_FAILURE(status))
430 acpi_evaluate_hotplug_ost(handle, type,
431 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
436 * acpi_bus_hot_remove_device: hot-remove a device and its children
437 * @context: struct acpi_eject_event pointer (freed in this func)
439 * Hot-remove a device and its children. This function frees up the
440 * memory space passed by arg context, so that the caller may call
441 * this function asynchronously through acpi_os_hotplug_execute().
443 void acpi_bus_hot_remove_device(void *context)
445 struct acpi_eject_event *ej_event = context;
446 struct acpi_device *device = ej_event->device;
447 acpi_handle handle = device->handle;
450 mutex_lock(&acpi_scan_lock);
452 error = acpi_scan_hot_remove(device);
454 acpi_evaluate_hotplug_ost(handle, ej_event->event,
455 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
458 mutex_unlock(&acpi_scan_lock);
461 EXPORT_SYMBOL(acpi_bus_hot_remove_device);
463 static ssize_t real_power_state_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
466 struct acpi_device *adev = to_acpi_device(dev);
470 ret = acpi_device_get_power(adev, &state);
474 return sprintf(buf, "%s\n", acpi_power_state_string(state));
477 static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
479 static ssize_t power_state_show(struct device *dev,
480 struct device_attribute *attr, char *buf)
482 struct acpi_device *adev = to_acpi_device(dev);
484 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
487 static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
490 acpi_eject_store(struct device *d, struct device_attribute *attr,
491 const char *buf, size_t count)
493 struct acpi_device *acpi_device = to_acpi_device(d);
494 struct acpi_eject_event *ej_event;
495 acpi_object_type not_used;
500 if (!count || buf[0] != '1')
503 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
504 && !acpi_device->driver)
507 status = acpi_get_type(acpi_device->handle, ¬_used);
508 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
511 mutex_lock(&acpi_scan_lock);
513 if (acpi_device->flags.eject_pending) {
514 /* ACPI eject notification event. */
515 ost_source = ACPI_NOTIFY_EJECT_REQUEST;
516 acpi_device->flags.eject_pending = 0;
518 /* Eject initiated by user space. */
519 ost_source = ACPI_OST_EC_OSPM_EJECT;
521 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
526 acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
527 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
528 ej_event->device = acpi_device;
529 ej_event->event = ost_source;
530 get_device(&acpi_device->dev);
531 status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device, ej_event);
532 if (ACPI_FAILURE(status)) {
533 put_device(&acpi_device->dev);
535 ret = status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
541 mutex_unlock(&acpi_scan_lock);
545 acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
546 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
550 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
553 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
554 struct acpi_device *acpi_dev = to_acpi_device(dev);
556 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
558 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
560 static ssize_t acpi_device_uid_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
563 struct acpi_device *acpi_dev = to_acpi_device(dev);
565 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
567 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
569 static ssize_t acpi_device_adr_show(struct device *dev,
570 struct device_attribute *attr, char *buf)
572 struct acpi_device *acpi_dev = to_acpi_device(dev);
574 return sprintf(buf, "0x%08x\n",
575 (unsigned int)(acpi_dev->pnp.bus_address));
577 static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
580 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
581 struct acpi_device *acpi_dev = to_acpi_device(dev);
582 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
585 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
589 result = sprintf(buf, "%s\n", (char*)path.pointer);
594 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
596 /* sysfs file that shows description text from the ACPI _STR method */
597 static ssize_t description_show(struct device *dev,
598 struct device_attribute *attr,
600 struct acpi_device *acpi_dev = to_acpi_device(dev);
603 if (acpi_dev->pnp.str_obj == NULL)
607 * The _STR object contains a Unicode identifier for a device.
608 * We need to convert to utf-8 so it can be displayed.
610 result = utf16s_to_utf8s(
611 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
612 acpi_dev->pnp.str_obj->buffer.length,
613 UTF16_LITTLE_ENDIAN, buf,
616 buf[result++] = '\n';
620 static DEVICE_ATTR(description, 0444, description_show, NULL);
623 acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
625 struct acpi_device *acpi_dev = to_acpi_device(dev);
627 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
629 static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
631 static int acpi_device_setup_files(struct acpi_device *dev)
633 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
635 unsigned long long sun;
639 * Devices gotten from FADT don't have a "path" attribute
642 result = device_create_file(&dev->dev, &dev_attr_path);
647 if (!list_empty(&dev->pnp.ids)) {
648 result = device_create_file(&dev->dev, &dev_attr_hid);
652 result = device_create_file(&dev->dev, &dev_attr_modalias);
658 * If device has _STR, 'description' file is created
660 if (acpi_has_method(dev->handle, "_STR")) {
661 status = acpi_evaluate_object(dev->handle, "_STR",
663 if (ACPI_FAILURE(status))
664 buffer.pointer = NULL;
665 dev->pnp.str_obj = buffer.pointer;
666 result = device_create_file(&dev->dev, &dev_attr_description);
671 if (dev->pnp.type.bus_address)
672 result = device_create_file(&dev->dev, &dev_attr_adr);
673 if (dev->pnp.unique_id)
674 result = device_create_file(&dev->dev, &dev_attr_uid);
676 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
677 if (ACPI_SUCCESS(status)) {
678 dev->pnp.sun = (unsigned long)sun;
679 result = device_create_file(&dev->dev, &dev_attr_sun);
683 dev->pnp.sun = (unsigned long)-1;
687 * If device has _EJ0, 'eject' file is created that is used to trigger
688 * hot-removal function from userland.
690 if (acpi_has_method(dev->handle, "_EJ0")) {
691 result = device_create_file(&dev->dev, &dev_attr_eject);
696 if (dev->flags.power_manageable) {
697 result = device_create_file(&dev->dev, &dev_attr_power_state);
701 if (dev->power.flags.power_resources)
702 result = device_create_file(&dev->dev,
703 &dev_attr_real_power_state);
710 static void acpi_device_remove_files(struct acpi_device *dev)
712 if (dev->flags.power_manageable) {
713 device_remove_file(&dev->dev, &dev_attr_power_state);
714 if (dev->power.flags.power_resources)
715 device_remove_file(&dev->dev,
716 &dev_attr_real_power_state);
720 * If device has _STR, remove 'description' file
722 if (acpi_has_method(dev->handle, "_STR")) {
723 kfree(dev->pnp.str_obj);
724 device_remove_file(&dev->dev, &dev_attr_description);
727 * If device has _EJ0, remove 'eject' file.
729 if (acpi_has_method(dev->handle, "_EJ0"))
730 device_remove_file(&dev->dev, &dev_attr_eject);
732 if (acpi_has_method(dev->handle, "_SUN"))
733 device_remove_file(&dev->dev, &dev_attr_sun);
735 if (dev->pnp.unique_id)
736 device_remove_file(&dev->dev, &dev_attr_uid);
737 if (dev->pnp.type.bus_address)
738 device_remove_file(&dev->dev, &dev_attr_adr);
739 device_remove_file(&dev->dev, &dev_attr_modalias);
740 device_remove_file(&dev->dev, &dev_attr_hid);
742 device_remove_file(&dev->dev, &dev_attr_path);
744 /* --------------------------------------------------------------------------
746 -------------------------------------------------------------------------- */
748 static const struct acpi_device_id *__acpi_match_device(
749 struct acpi_device *device, const struct acpi_device_id *ids)
751 const struct acpi_device_id *id;
752 struct acpi_hardware_id *hwid;
755 * If the device is not present, it is unnecessary to load device
758 if (!device->status.present)
761 for (id = ids; id->id[0]; id++)
762 list_for_each_entry(hwid, &device->pnp.ids, list)
763 if (!strcmp((char *) id->id, hwid->id))
770 * acpi_match_device - Match a struct device against a given list of ACPI IDs
771 * @ids: Array of struct acpi_device_id object to match against.
772 * @dev: The device structure to match.
774 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
775 * object for that handle and use that object to match against a given list of
778 * Return a pointer to the first matching ID on success or %NULL on failure.
780 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
781 const struct device *dev)
783 struct acpi_device *adev;
784 acpi_handle handle = ACPI_HANDLE(dev);
786 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
789 return __acpi_match_device(adev, ids);
791 EXPORT_SYMBOL_GPL(acpi_match_device);
793 int acpi_match_device_ids(struct acpi_device *device,
794 const struct acpi_device_id *ids)
796 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
798 EXPORT_SYMBOL(acpi_match_device_ids);
800 static void acpi_free_power_resources_lists(struct acpi_device *device)
804 if (device->wakeup.flags.valid)
805 acpi_power_resources_list_free(&device->wakeup.resources);
807 if (!device->flags.power_manageable)
810 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
811 struct acpi_device_power_state *ps = &device->power.states[i];
812 acpi_power_resources_list_free(&ps->resources);
816 static void acpi_device_release(struct device *dev)
818 struct acpi_device *acpi_dev = to_acpi_device(dev);
820 acpi_free_pnp_ids(&acpi_dev->pnp);
821 acpi_free_power_resources_lists(acpi_dev);
825 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
827 struct acpi_device *acpi_dev = to_acpi_device(dev);
828 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
830 return acpi_dev->flags.match_driver
831 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
834 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
836 struct acpi_device *acpi_dev = to_acpi_device(dev);
839 if (list_empty(&acpi_dev->pnp.ids))
842 if (add_uevent_var(env, "MODALIAS="))
844 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
845 sizeof(env->buf) - env->buflen);
846 if (len >= (sizeof(env->buf) - env->buflen))
852 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
854 struct acpi_device *device = data;
856 device->driver->ops.notify(device, event);
859 static acpi_status acpi_device_notify_fixed(void *data)
861 struct acpi_device *device = data;
863 /* Fixed hardware devices have no handles */
864 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
868 static int acpi_device_install_notify_handler(struct acpi_device *device)
872 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
874 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
875 acpi_device_notify_fixed,
877 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
879 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
880 acpi_device_notify_fixed,
883 status = acpi_install_notify_handler(device->handle,
888 if (ACPI_FAILURE(status))
893 static void acpi_device_remove_notify_handler(struct acpi_device *device)
895 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
896 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
897 acpi_device_notify_fixed);
898 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
899 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
900 acpi_device_notify_fixed);
902 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
906 static int acpi_device_probe(struct device *dev)
908 struct acpi_device *acpi_dev = to_acpi_device(dev);
909 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
912 if (acpi_dev->handler)
915 if (!acpi_drv->ops.add)
918 ret = acpi_drv->ops.add(acpi_dev);
922 acpi_dev->driver = acpi_drv;
923 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
924 "Driver [%s] successfully bound to device [%s]\n",
925 acpi_drv->name, acpi_dev->pnp.bus_id));
927 if (acpi_drv->ops.notify) {
928 ret = acpi_device_install_notify_handler(acpi_dev);
930 if (acpi_drv->ops.remove)
931 acpi_drv->ops.remove(acpi_dev);
933 acpi_dev->driver = NULL;
934 acpi_dev->driver_data = NULL;
939 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
940 acpi_drv->name, acpi_dev->pnp.bus_id));
945 static int acpi_device_remove(struct device * dev)
947 struct acpi_device *acpi_dev = to_acpi_device(dev);
948 struct acpi_driver *acpi_drv = acpi_dev->driver;
951 if (acpi_drv->ops.notify)
952 acpi_device_remove_notify_handler(acpi_dev);
953 if (acpi_drv->ops.remove)
954 acpi_drv->ops.remove(acpi_dev);
956 acpi_dev->driver = NULL;
957 acpi_dev->driver_data = NULL;
963 struct bus_type acpi_bus_type = {
965 .match = acpi_bus_match,
966 .probe = acpi_device_probe,
967 .remove = acpi_device_remove,
968 .uevent = acpi_device_uevent,
971 int acpi_device_add(struct acpi_device *device,
972 void (*release)(struct device *))
975 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
978 if (device->handle) {
981 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
983 if (ACPI_FAILURE(status)) {
984 acpi_handle_err(device->handle,
985 "Unable to attach device data\n");
993 * Link this device to its parent and siblings.
995 INIT_LIST_HEAD(&device->children);
996 INIT_LIST_HEAD(&device->node);
997 INIT_LIST_HEAD(&device->wakeup_list);
998 INIT_LIST_HEAD(&device->physical_node_list);
999 mutex_init(&device->physical_node_lock);
1000 INIT_LIST_HEAD(&device->power_dependent);
1002 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1004 pr_err(PREFIX "Memory allocation error\n");
1009 mutex_lock(&acpi_device_lock);
1011 * Find suitable bus_id and instance number in acpi_bus_id_list
1012 * If failed, create one and link it into acpi_bus_id_list
1014 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
1015 if (!strcmp(acpi_device_bus_id->bus_id,
1016 acpi_device_hid(device))) {
1017 acpi_device_bus_id->instance_no++;
1024 acpi_device_bus_id = new_bus_id;
1025 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
1026 acpi_device_bus_id->instance_no = 0;
1027 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1029 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
1032 list_add_tail(&device->node, &device->parent->children);
1034 if (device->wakeup.flags.valid)
1035 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
1036 mutex_unlock(&acpi_device_lock);
1039 device->dev.parent = &device->parent->dev;
1040 device->dev.bus = &acpi_bus_type;
1041 device->dev.release = release;
1042 result = device_add(&device->dev);
1044 dev_err(&device->dev, "Error registering device\n");
1048 result = acpi_device_setup_files(device);
1050 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1051 dev_name(&device->dev));
1056 mutex_lock(&acpi_device_lock);
1058 list_del(&device->node);
1059 list_del(&device->wakeup_list);
1060 mutex_unlock(&acpi_device_lock);
1063 acpi_detach_data(device->handle, acpi_bus_data_handler);
1067 static void acpi_device_unregister(struct acpi_device *device)
1069 mutex_lock(&acpi_device_lock);
1071 list_del(&device->node);
1073 list_del(&device->wakeup_list);
1074 mutex_unlock(&acpi_device_lock);
1076 acpi_detach_data(device->handle, acpi_bus_data_handler);
1078 acpi_power_add_remove_device(device, false);
1079 acpi_device_remove_files(device);
1081 device->remove(device);
1083 device_del(&device->dev);
1085 * Transition the device to D3cold to drop the reference counts of all
1086 * power resources the device depends on and turn off the ones that have
1087 * no more references.
1089 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
1090 device->handle = NULL;
1091 put_device(&device->dev);
1094 /* --------------------------------------------------------------------------
1096 -------------------------------------------------------------------------- */
1098 * acpi_bus_register_driver - register a driver with the ACPI bus
1099 * @driver: driver being registered
1101 * Registers a driver with the ACPI bus. Searches the namespace for all
1102 * devices that match the driver's criteria and binds. Returns zero for
1103 * success or a negative error status for failure.
1105 int acpi_bus_register_driver(struct acpi_driver *driver)
1111 driver->drv.name = driver->name;
1112 driver->drv.bus = &acpi_bus_type;
1113 driver->drv.owner = driver->owner;
1115 ret = driver_register(&driver->drv);
1119 EXPORT_SYMBOL(acpi_bus_register_driver);
1122 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
1123 * @driver: driver to unregister
1125 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1126 * devices that match the driver's criteria and unbinds.
1128 void acpi_bus_unregister_driver(struct acpi_driver *driver)
1130 driver_unregister(&driver->drv);
1133 EXPORT_SYMBOL(acpi_bus_unregister_driver);
1135 /* --------------------------------------------------------------------------
1137 -------------------------------------------------------------------------- */
1138 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1140 struct acpi_device *device = NULL;
1144 * Fixed hardware devices do not appear in the namespace and do not
1145 * have handles, but we fabricate acpi_devices for them, so we have
1146 * to deal with them specially.
1152 status = acpi_get_parent(handle, &handle);
1153 if (ACPI_FAILURE(status))
1154 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1155 } while (acpi_bus_get_device(handle, &device));
1160 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1164 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1165 union acpi_object *obj;
1167 status = acpi_get_handle(handle, "_EJD", &tmp);
1168 if (ACPI_FAILURE(status))
1171 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1172 if (ACPI_SUCCESS(status)) {
1173 obj = buffer.pointer;
1174 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1176 kfree(buffer.pointer);
1180 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1182 void acpi_bus_data_handler(acpi_handle handle, void *context)
1190 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1191 struct acpi_device_wakeup *wakeup)
1193 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1194 union acpi_object *package = NULL;
1195 union acpi_object *element = NULL;
1202 INIT_LIST_HEAD(&wakeup->resources);
1205 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1206 if (ACPI_FAILURE(status)) {
1207 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
1211 package = (union acpi_object *)buffer.pointer;
1213 if (!package || package->package.count < 2)
1216 element = &(package->package.elements[0]);
1220 if (element->type == ACPI_TYPE_PACKAGE) {
1221 if ((element->package.count < 2) ||
1222 (element->package.elements[0].type !=
1223 ACPI_TYPE_LOCAL_REFERENCE)
1224 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
1227 wakeup->gpe_device =
1228 element->package.elements[0].reference.handle;
1229 wakeup->gpe_number =
1230 (u32) element->package.elements[1].integer.value;
1231 } else if (element->type == ACPI_TYPE_INTEGER) {
1232 wakeup->gpe_device = NULL;
1233 wakeup->gpe_number = element->integer.value;
1238 element = &(package->package.elements[1]);
1239 if (element->type != ACPI_TYPE_INTEGER)
1242 wakeup->sleep_state = element->integer.value;
1244 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1248 if (!list_empty(&wakeup->resources)) {
1251 err = acpi_power_wakeup_list_init(&wakeup->resources,
1254 acpi_handle_warn(handle, "Retrieving current states "
1255 "of wakeup power resources failed\n");
1256 acpi_power_resources_list_free(&wakeup->resources);
1259 if (sleep_state < wakeup->sleep_state) {
1260 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1261 "(S%d) by S%d from power resources\n",
1262 (int)wakeup->sleep_state, sleep_state);
1263 wakeup->sleep_state = sleep_state;
1266 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
1269 kfree(buffer.pointer);
1273 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
1275 struct acpi_device_id button_device_ids[] = {
1282 acpi_event_status event_status;
1284 device->wakeup.flags.notifier_present = 0;
1286 /* Power button, Lid switch always enable wakeup */
1287 if (!acpi_match_device_ids(device, button_device_ids)) {
1288 device->wakeup.flags.run_wake = 1;
1289 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1290 /* Do not use Lid/sleep button for S5 wakeup */
1291 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1292 device->wakeup.sleep_state = ACPI_STATE_S4;
1294 device_set_wakeup_capable(&device->dev, true);
1298 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1299 device->wakeup.gpe_number,
1301 if (status == AE_OK)
1302 device->wakeup.flags.run_wake =
1303 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1306 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1310 /* Presence of _PRW indicates wake capable */
1311 if (!acpi_has_method(device->handle, "_PRW"))
1314 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1317 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1321 device->wakeup.flags.valid = 1;
1322 device->wakeup.prepare_count = 0;
1323 acpi_bus_set_run_wake_flags(device);
1324 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1325 * system for the ACPI device with the _PRW object.
1326 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1327 * So it is necessary to call _DSW object first. Only when it is not
1328 * present will the _PSW object used.
1330 err = acpi_device_sleep_wake(device, 0, 0, 0);
1332 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1333 "error in _DSW or _PSW evaluation\n"));
1336 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1338 struct acpi_device_power_state *ps = &device->power.states[state];
1339 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1340 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1343 INIT_LIST_HEAD(&ps->resources);
1345 /* Evaluate "_PRx" to get referenced power resources */
1346 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1347 if (ACPI_SUCCESS(status)) {
1348 union acpi_object *package = buffer.pointer;
1350 if (buffer.length && package
1351 && package->type == ACPI_TYPE_PACKAGE
1352 && package->package.count) {
1353 int err = acpi_extract_power_resources(package, 0,
1356 device->power.flags.power_resources = 1;
1358 ACPI_FREE(buffer.pointer);
1361 /* Evaluate "_PSx" to see if we can do explicit sets */
1363 if (acpi_has_method(device->handle, pathname))
1364 ps->flags.explicit_set = 1;
1367 * State is valid if there are means to put the device into it.
1368 * D3hot is only valid if _PR3 present.
1370 if (!list_empty(&ps->resources)
1371 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1372 ps->flags.valid = 1;
1373 ps->flags.os_accessible = 1;
1376 ps->power = -1; /* Unknown - driver assigned */
1377 ps->latency = -1; /* Unknown - driver assigned */
1380 static void acpi_bus_get_power_flags(struct acpi_device *device)
1384 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1385 if (!acpi_has_method(device->handle, "_PS0") &&
1386 !acpi_has_method(device->handle, "_PR0"))
1389 device->flags.power_manageable = 1;
1392 * Power Management Flags
1394 if (acpi_has_method(device->handle, "_PSC"))
1395 device->power.flags.explicit_get = 1;
1396 if (acpi_has_method(device->handle, "_IRC"))
1397 device->power.flags.inrush_current = 1;
1400 * Enumerate supported power management states
1402 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1403 acpi_bus_init_power_state(device, i);
1405 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1407 /* Set defaults for D0 and D3 states (always valid) */
1408 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1409 device->power.states[ACPI_STATE_D0].power = 100;
1410 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1411 device->power.states[ACPI_STATE_D3].power = 0;
1413 /* Set D3cold's explicit_set flag if _PS3 exists. */
1414 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1415 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1417 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1418 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1419 device->power.flags.power_resources)
1420 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1422 if (acpi_bus_init_power(device)) {
1423 acpi_free_power_resources_lists(device);
1424 device->flags.power_manageable = 0;
1428 static void acpi_bus_get_flags(struct acpi_device *device)
1430 /* Presence of _STA indicates 'dynamic_status' */
1431 if (acpi_has_method(device->handle, "_STA"))
1432 device->flags.dynamic_status = 1;
1434 /* Presence of _RMV indicates 'removable' */
1435 if (acpi_has_method(device->handle, "_RMV"))
1436 device->flags.removable = 1;
1438 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1439 if (acpi_has_method(device->handle, "_EJD") ||
1440 acpi_has_method(device->handle, "_EJ0"))
1441 device->flags.ejectable = 1;
1444 static void acpi_device_get_busid(struct acpi_device *device)
1446 char bus_id[5] = { '?', 0 };
1447 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1453 * The device's Bus ID is simply the object name.
1454 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1456 if (ACPI_IS_ROOT_DEVICE(device)) {
1457 strcpy(device->pnp.bus_id, "ACPI");
1461 switch (device->device_type) {
1462 case ACPI_BUS_TYPE_POWER_BUTTON:
1463 strcpy(device->pnp.bus_id, "PWRF");
1465 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1466 strcpy(device->pnp.bus_id, "SLPF");
1469 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1470 /* Clean up trailing underscores (if any) */
1471 for (i = 3; i > 1; i--) {
1472 if (bus_id[i] == '_')
1477 strcpy(device->pnp.bus_id, bus_id);
1483 * acpi_ata_match - see if an acpi object is an ATA device
1485 * If an acpi object has one of the ACPI ATA methods defined,
1486 * then we can safely call it an ATA device.
1488 bool acpi_ata_match(acpi_handle handle)
1490 return acpi_has_method(handle, "_GTF") ||
1491 acpi_has_method(handle, "_GTM") ||
1492 acpi_has_method(handle, "_STM") ||
1493 acpi_has_method(handle, "_SDD");
1497 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1499 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1500 * then we can safely call it an ejectable drive bay
1502 bool acpi_bay_match(acpi_handle handle)
1504 acpi_handle phandle;
1506 if (!acpi_has_method(handle, "_EJ0"))
1508 if (acpi_ata_match(handle))
1510 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1513 return acpi_ata_match(phandle);
1517 * acpi_dock_match - see if an acpi object has a _DCK method
1519 bool acpi_dock_match(acpi_handle handle)
1521 return acpi_has_method(handle, "_DCK");
1524 const char *acpi_device_hid(struct acpi_device *device)
1526 struct acpi_hardware_id *hid;
1528 if (list_empty(&device->pnp.ids))
1531 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1534 EXPORT_SYMBOL(acpi_device_hid);
1536 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1538 struct acpi_hardware_id *id;
1540 id = kmalloc(sizeof(*id), GFP_KERNEL);
1544 id->id = kstrdup(dev_id, GFP_KERNEL);
1550 list_add_tail(&id->list, &pnp->ids);
1551 pnp->type.hardware_id = 1;
1555 * Old IBM workstations have a DSDT bug wherein the SMBus object
1556 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1557 * prefix. Work around this.
1559 static bool acpi_ibm_smbus_match(acpi_handle handle)
1561 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1562 struct acpi_buffer path = { sizeof(node_name), node_name };
1564 if (!dmi_name_in_vendors("IBM"))
1567 /* Look for SMBS object */
1568 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1569 strcmp("SMBS", path.pointer))
1572 /* Does it have the necessary (but misnamed) methods? */
1573 if (acpi_has_method(handle, "SBI") &&
1574 acpi_has_method(handle, "SBR") &&
1575 acpi_has_method(handle, "SBW"))
1581 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1585 struct acpi_device_info *info;
1586 struct acpi_pnp_device_id_list *cid_list;
1589 switch (device_type) {
1590 case ACPI_BUS_TYPE_DEVICE:
1591 if (handle == ACPI_ROOT_OBJECT) {
1592 acpi_add_id(pnp, ACPI_SYSTEM_HID);
1596 status = acpi_get_object_info(handle, &info);
1597 if (ACPI_FAILURE(status)) {
1598 pr_err(PREFIX "%s: Error reading device info\n",
1603 if (info->valid & ACPI_VALID_HID)
1604 acpi_add_id(pnp, info->hardware_id.string);
1605 if (info->valid & ACPI_VALID_CID) {
1606 cid_list = &info->compatible_id_list;
1607 for (i = 0; i < cid_list->count; i++)
1608 acpi_add_id(pnp, cid_list->ids[i].string);
1610 if (info->valid & ACPI_VALID_ADR) {
1611 pnp->bus_address = info->address;
1612 pnp->type.bus_address = 1;
1614 if (info->valid & ACPI_VALID_UID)
1615 pnp->unique_id = kstrdup(info->unique_id.string,
1621 * Some devices don't reliably have _HIDs & _CIDs, so add
1622 * synthetic HIDs to make sure drivers can find them.
1624 if (acpi_is_video_device(handle))
1625 acpi_add_id(pnp, ACPI_VIDEO_HID);
1626 else if (acpi_bay_match(handle))
1627 acpi_add_id(pnp, ACPI_BAY_HID);
1628 else if (acpi_dock_match(handle))
1629 acpi_add_id(pnp, ACPI_DOCK_HID);
1630 else if (acpi_ibm_smbus_match(handle))
1631 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1632 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1633 acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1634 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1635 strcpy(pnp->device_class, ACPI_BUS_CLASS);
1639 case ACPI_BUS_TYPE_POWER:
1640 acpi_add_id(pnp, ACPI_POWER_HID);
1642 case ACPI_BUS_TYPE_PROCESSOR:
1643 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1645 case ACPI_BUS_TYPE_THERMAL:
1646 acpi_add_id(pnp, ACPI_THERMAL_HID);
1648 case ACPI_BUS_TYPE_POWER_BUTTON:
1649 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1651 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1652 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1657 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1659 struct acpi_hardware_id *id, *tmp;
1661 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1665 kfree(pnp->unique_id);
1668 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1669 int type, unsigned long long sta)
1671 INIT_LIST_HEAD(&device->pnp.ids);
1672 device->device_type = type;
1673 device->handle = handle;
1674 device->parent = acpi_bus_get_parent(handle);
1675 STRUCT_TO_INT(device->status) = sta;
1676 acpi_device_get_busid(device);
1677 acpi_set_pnp_ids(handle, &device->pnp, type);
1678 acpi_bus_get_flags(device);
1679 device->flags.match_driver = false;
1680 device_initialize(&device->dev);
1681 dev_set_uevent_suppress(&device->dev, true);
1684 void acpi_device_add_finalize(struct acpi_device *device)
1686 device->flags.match_driver = true;
1687 dev_set_uevent_suppress(&device->dev, false);
1688 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1691 static int acpi_add_single_object(struct acpi_device **child,
1692 acpi_handle handle, int type,
1693 unsigned long long sta)
1696 struct acpi_device *device;
1697 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1699 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1701 printk(KERN_ERR PREFIX "Memory allocation error\n");
1705 acpi_init_device_object(device, handle, type, sta);
1706 acpi_bus_get_power_flags(device);
1707 acpi_bus_get_wakeup_device_flags(device);
1709 result = acpi_device_add(device, acpi_device_release);
1711 acpi_device_release(&device->dev);
1715 acpi_power_add_remove_device(device, true);
1716 acpi_device_add_finalize(device);
1717 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1718 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1719 dev_name(&device->dev), (char *) buffer.pointer,
1720 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1721 kfree(buffer.pointer);
1726 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1727 unsigned long long *sta)
1730 acpi_object_type acpi_type;
1732 status = acpi_get_type(handle, &acpi_type);
1733 if (ACPI_FAILURE(status))
1736 switch (acpi_type) {
1737 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1738 case ACPI_TYPE_DEVICE:
1739 *type = ACPI_BUS_TYPE_DEVICE;
1740 status = acpi_bus_get_status_handle(handle, sta);
1741 if (ACPI_FAILURE(status))
1744 case ACPI_TYPE_PROCESSOR:
1745 *type = ACPI_BUS_TYPE_PROCESSOR;
1746 status = acpi_bus_get_status_handle(handle, sta);
1747 if (ACPI_FAILURE(status))
1750 case ACPI_TYPE_THERMAL:
1751 *type = ACPI_BUS_TYPE_THERMAL;
1752 *sta = ACPI_STA_DEFAULT;
1754 case ACPI_TYPE_POWER:
1755 *type = ACPI_BUS_TYPE_POWER;
1756 *sta = ACPI_STA_DEFAULT;
1765 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1767 const struct acpi_device_id **matchid)
1769 const struct acpi_device_id *devid;
1771 for (devid = handler->ids; devid->id[0]; devid++)
1772 if (!strcmp((char *)devid->id, idstr)) {
1782 static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1783 const struct acpi_device_id **matchid)
1785 struct acpi_scan_handler *handler;
1787 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1788 if (acpi_scan_handler_matching(handler, idstr, matchid))
1794 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1796 if (!!hotplug->enabled == !!val)
1799 mutex_lock(&acpi_scan_lock);
1801 hotplug->enabled = val;
1803 mutex_unlock(&acpi_scan_lock);
1806 static void acpi_scan_init_hotplug(acpi_handle handle, int type)
1808 struct acpi_device_pnp pnp = {};
1809 struct acpi_hardware_id *hwid;
1810 struct acpi_scan_handler *handler;
1812 INIT_LIST_HEAD(&pnp.ids);
1813 acpi_set_pnp_ids(handle, &pnp, type);
1815 if (!pnp.type.hardware_id)
1819 * This relies on the fact that acpi_install_notify_handler() will not
1820 * install the same notify handler routine twice for the same handle.
1822 list_for_each_entry(hwid, &pnp.ids, list) {
1823 handler = acpi_scan_match_handler(hwid->id, NULL);
1825 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1826 acpi_hotplug_notify_cb, handler);
1832 acpi_free_pnp_ids(&pnp);
1835 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1836 void *not_used, void **return_value)
1838 struct acpi_device *device = NULL;
1840 unsigned long long sta;
1843 acpi_bus_get_device(handle, &device);
1847 result = acpi_bus_type_and_status(handle, &type, &sta);
1851 if (type == ACPI_BUS_TYPE_POWER) {
1852 acpi_add_power_resource(handle);
1856 acpi_scan_init_hotplug(handle, type);
1858 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1859 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
1860 struct acpi_device_wakeup wakeup;
1862 if (acpi_has_method(handle, "_PRW")) {
1863 acpi_bus_extract_wakeup_device_power_package(handle,
1865 acpi_power_resources_list_free(&wakeup.resources);
1867 return AE_CTRL_DEPTH;
1870 acpi_add_single_object(&device, handle, type, sta);
1872 return AE_CTRL_DEPTH;
1876 *return_value = device;
1881 static int acpi_scan_attach_handler(struct acpi_device *device)
1883 struct acpi_hardware_id *hwid;
1886 list_for_each_entry(hwid, &device->pnp.ids, list) {
1887 const struct acpi_device_id *devid;
1888 struct acpi_scan_handler *handler;
1890 handler = acpi_scan_match_handler(hwid->id, &devid);
1892 ret = handler->attach(device, devid);
1894 device->handler = handler;
1896 } else if (ret < 0) {
1904 static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1905 void *not_used, void **ret_not_used)
1907 struct acpi_device *device;
1908 unsigned long long sta_not_used;
1912 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1913 * namespace walks prematurely.
1915 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
1918 if (acpi_bus_get_device(handle, &device))
1919 return AE_CTRL_DEPTH;
1921 ret = acpi_scan_attach_handler(device);
1923 return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
1925 ret = device_attach(&device->dev);
1926 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
1930 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1931 * @handle: Root of the namespace scope to scan.
1933 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1936 * If no devices were found, -ENODEV is returned, but it does not mean that
1937 * there has been a real error. There just have been no suitable ACPI objects
1938 * in the table trunk from which the kernel could create a device and add an
1939 * appropriate driver.
1941 * Must be called under acpi_scan_lock.
1943 int acpi_bus_scan(acpi_handle handle)
1945 void *device = NULL;
1948 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
1949 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1950 acpi_bus_check_add, NULL, NULL, &device);
1954 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
1955 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1956 acpi_bus_device_attach, NULL, NULL, NULL);
1960 EXPORT_SYMBOL(acpi_bus_scan);
1962 static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1963 void *not_used, void **ret_not_used)
1965 struct acpi_device *device = NULL;
1967 if (!acpi_bus_get_device(handle, &device)) {
1968 struct acpi_scan_handler *dev_handler = device->handler;
1971 if (dev_handler->detach)
1972 dev_handler->detach(device);
1974 device->handler = NULL;
1976 device_release_driver(&device->dev);
1982 static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1983 void *not_used, void **ret_not_used)
1985 struct acpi_device *device = NULL;
1987 if (!acpi_bus_get_device(handle, &device))
1988 acpi_device_unregister(device);
1994 * acpi_bus_trim - Remove ACPI device node and all of its descendants
1995 * @start: Root of the ACPI device nodes subtree to remove.
1997 * Must be called under acpi_scan_lock.
1999 void acpi_bus_trim(struct acpi_device *start)
2002 * Execute acpi_bus_device_detach() as a post-order callback to detach
2003 * all ACPI drivers from the device nodes being removed.
2005 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2006 acpi_bus_device_detach, NULL, NULL);
2007 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
2009 * Execute acpi_bus_remove() as a post-order callback to remove device
2010 * nodes in the given namespace scope.
2012 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2013 acpi_bus_remove, NULL, NULL);
2014 acpi_bus_remove(start->handle, 0, NULL, NULL);
2016 EXPORT_SYMBOL_GPL(acpi_bus_trim);
2018 static int acpi_bus_scan_fixed(void)
2023 * Enumerate all fixed-feature devices.
2025 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2026 struct acpi_device *device = NULL;
2028 result = acpi_add_single_object(&device, NULL,
2029 ACPI_BUS_TYPE_POWER_BUTTON,
2034 result = device_attach(&device->dev);
2038 device_init_wakeup(&device->dev, true);
2041 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2042 struct acpi_device *device = NULL;
2044 result = acpi_add_single_object(&device, NULL,
2045 ACPI_BUS_TYPE_SLEEP_BUTTON,
2050 result = device_attach(&device->dev);
2053 return result < 0 ? result : 0;
2056 int __init acpi_scan_init(void)
2060 result = bus_register(&acpi_bus_type);
2062 /* We don't want to quit even if we failed to add suspend/resume */
2063 printk(KERN_ERR PREFIX "Could not register bus type\n");
2066 acpi_pci_root_init();
2067 acpi_pci_link_init();
2068 acpi_processor_init();
2069 acpi_platform_init();
2071 acpi_cmos_rtc_init();
2072 acpi_container_init();
2073 acpi_memory_hotplug_init();
2076 mutex_lock(&acpi_scan_lock);
2078 * Enumerate devices in the ACPI namespace.
2080 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2084 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2088 result = acpi_bus_scan_fixed();
2090 acpi_device_unregister(acpi_root);
2094 acpi_update_all_gpes();
2096 acpi_pci_root_hp_init();
2099 mutex_unlock(&acpi_scan_lock);