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(acpi_handle handle, u32 lvl, void *data,
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 if (device->handler && !device->handler->hotplug.enabled) {
140 *ret_p = &device->dev;
144 mutex_lock(&device->physical_node_lock);
146 list_for_each_entry(pn, &device->physical_node_list, node) {
150 /* Skip devices offlined by the first pass. */
154 pn->put_online = false;
156 ret = device_offline(pn->dev);
157 if (acpi_force_hot_remove)
161 pn->put_online = !ret;
171 mutex_unlock(&device->physical_node_lock);
176 static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
179 struct acpi_device *device = NULL;
180 struct acpi_device_physical_node *pn;
182 if (acpi_bus_get_device(handle, &device))
185 mutex_lock(&device->physical_node_lock);
187 list_for_each_entry(pn, &device->physical_node_list, node)
188 if (pn->put_online) {
189 device_online(pn->dev);
190 pn->put_online = false;
193 mutex_unlock(&device->physical_node_lock);
198 static int acpi_scan_hot_remove(struct acpi_device *device)
200 acpi_handle handle = device->handle;
201 struct device *errdev;
203 unsigned long long sta;
205 /* If there is no handle, the device node has been unregistered. */
207 dev_dbg(&device->dev, "ACPI handle missing\n");
208 put_device(&device->dev);
213 * Carry out two passes here and ignore errors in the first pass,
214 * because if the devices in question are memory blocks and
215 * CONFIG_MEMCG is set, one of the blocks may hold data structures
216 * that the other blocks depend on, but it is not known in advance which
219 * If the first pass is successful, the second one isn't needed, though.
222 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
223 NULL, acpi_bus_offline, (void *)false,
225 if (status == AE_SUPPORT) {
226 dev_warn(errdev, "Offline disabled.\n");
227 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
228 acpi_bus_online, NULL, NULL, NULL);
229 put_device(&device->dev);
232 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
235 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
236 NULL, acpi_bus_offline, (void *)true,
238 if (!errdev || acpi_force_hot_remove)
239 acpi_bus_offline(handle, 0, (void *)true,
242 if (errdev && !acpi_force_hot_remove) {
243 dev_warn(errdev, "Offline failed.\n");
244 acpi_bus_online(handle, 0, NULL, NULL);
245 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
246 ACPI_UINT32_MAX, acpi_bus_online,
248 put_device(&device->dev);
253 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
254 "Hot-removing device %s...\n", dev_name(&device->dev)));
256 acpi_bus_trim(device);
258 /* Device node has been unregistered. */
259 put_device(&device->dev);
262 acpi_evaluate_lck(handle, 0);
266 status = acpi_evaluate_ej0(handle);
267 if (status == AE_NOT_FOUND)
269 else if (ACPI_FAILURE(status))
273 * Verify if eject was indeed successful. If not, log an error
274 * message. No need to call _OST since _EJ0 call was made OK.
276 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
277 if (ACPI_FAILURE(status)) {
278 acpi_handle_warn(handle,
279 "Status check after eject failed (0x%x)\n", status);
280 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
281 acpi_handle_warn(handle,
282 "Eject incomplete - status 0x%llx\n", sta);
288 static void acpi_bus_device_eject(struct acpi_device *device, u32 ost_src)
290 acpi_handle handle = device->handle;
291 struct acpi_scan_handler *handler;
292 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
295 lock_device_hotplug();
296 mutex_lock(&acpi_scan_lock);
298 handler = device->handler;
299 if (!handler || !handler->hotplug.enabled) {
300 put_device(&device->dev);
304 if (ost_src == ACPI_NOTIFY_EJECT_REQUEST)
305 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
306 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
308 if (handler->hotplug.mode == AHM_CONTAINER)
309 kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
311 error = acpi_scan_hot_remove(device);
312 if (error == -EPERM) {
319 mutex_unlock(&acpi_scan_lock);
320 unlock_device_hotplug();
324 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
326 acpi_evaluate_hotplug_ost(handle, ost_src, ost_code, NULL);
330 static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
332 struct acpi_device *device = NULL;
333 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
336 lock_device_hotplug();
337 mutex_lock(&acpi_scan_lock);
339 if (ost_source != ACPI_NOTIFY_BUS_CHECK) {
340 acpi_bus_get_device(handle, &device);
342 dev_warn(&device->dev, "Attempt to re-insert\n");
346 acpi_evaluate_hotplug_ost(handle, ost_source,
347 ACPI_OST_SC_INSERT_IN_PROGRESS, NULL);
348 error = acpi_bus_scan(handle);
350 acpi_handle_warn(handle, "Namespace scan failure\n");
353 error = acpi_bus_get_device(handle, &device);
355 acpi_handle_warn(handle, "Missing device node object\n");
358 ost_code = ACPI_OST_SC_SUCCESS;
359 if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
360 kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
363 acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
364 mutex_unlock(&acpi_scan_lock);
365 unlock_device_hotplug();
368 static void acpi_scan_bus_check(void *context)
370 acpi_scan_bus_device_check((acpi_handle)context,
371 ACPI_NOTIFY_BUS_CHECK);
374 static void acpi_scan_device_check(void *context)
376 acpi_scan_bus_device_check((acpi_handle)context,
377 ACPI_NOTIFY_DEVICE_CHECK);
380 static void acpi_hotplug_unsupported(acpi_handle handle, u32 type)
385 case ACPI_NOTIFY_BUS_CHECK:
386 acpi_handle_debug(handle,
387 "ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
388 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
390 case ACPI_NOTIFY_DEVICE_CHECK:
391 acpi_handle_debug(handle,
392 "ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
393 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
395 case ACPI_NOTIFY_EJECT_REQUEST:
396 acpi_handle_debug(handle,
397 "ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
398 ost_status = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
401 /* non-hotplug event; possibly handled by other handler */
405 acpi_evaluate_hotplug_ost(handle, type, ost_status, NULL);
409 * acpi_bus_hot_remove_device: Hot-remove a device and its children.
410 * @context: Address of the ACPI device object to hot-remove.
412 void acpi_bus_hot_remove_device(void *context)
414 acpi_bus_device_eject(context, ACPI_NOTIFY_EJECT_REQUEST);
417 static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
419 acpi_osd_exec_callback callback;
420 struct acpi_scan_handler *handler = data;
421 struct acpi_device *adev;
424 if (!handler->hotplug.enabled)
425 return acpi_hotplug_unsupported(handle, type);
428 case ACPI_NOTIFY_BUS_CHECK:
429 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
430 callback = acpi_scan_bus_check;
432 case ACPI_NOTIFY_DEVICE_CHECK:
433 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
434 callback = acpi_scan_device_check;
436 case ACPI_NOTIFY_EJECT_REQUEST:
437 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
438 status = acpi_bus_get_device(handle, &adev);
439 if (ACPI_FAILURE(status))
442 get_device(&adev->dev);
443 callback = acpi_bus_hot_remove_device;
444 status = acpi_os_hotplug_execute(callback, adev);
445 if (ACPI_SUCCESS(status))
448 put_device(&adev->dev);
451 /* non-hotplug event; possibly handled by other handler */
454 status = acpi_os_hotplug_execute(callback, handle);
455 if (ACPI_SUCCESS(status))
459 acpi_evaluate_hotplug_ost(handle, type,
460 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
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);
489 static void acpi_eject_store_work(void *context)
491 acpi_bus_device_eject(context, ACPI_OST_EC_OSPM_EJECT);
495 acpi_eject_store(struct device *d, struct device_attribute *attr,
496 const char *buf, size_t count)
498 struct acpi_device *acpi_device = to_acpi_device(d);
499 acpi_object_type not_used;
502 if (!count || buf[0] != '1')
505 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
506 && !acpi_device->driver)
509 status = acpi_get_type(acpi_device->handle, ¬_used);
510 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
513 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
514 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
515 get_device(&acpi_device->dev);
516 status = acpi_os_hotplug_execute(acpi_eject_store_work, acpi_device);
517 if (ACPI_SUCCESS(status))
520 put_device(&acpi_device->dev);
521 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
522 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
523 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
526 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
529 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
530 struct acpi_device *acpi_dev = to_acpi_device(dev);
532 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
534 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
536 static ssize_t acpi_device_uid_show(struct device *dev,
537 struct device_attribute *attr, char *buf)
539 struct acpi_device *acpi_dev = to_acpi_device(dev);
541 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
543 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
545 static ssize_t acpi_device_adr_show(struct device *dev,
546 struct device_attribute *attr, char *buf)
548 struct acpi_device *acpi_dev = to_acpi_device(dev);
550 return sprintf(buf, "0x%08x\n",
551 (unsigned int)(acpi_dev->pnp.bus_address));
553 static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
556 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
557 struct acpi_device *acpi_dev = to_acpi_device(dev);
558 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
561 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
565 result = sprintf(buf, "%s\n", (char*)path.pointer);
570 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
572 /* sysfs file that shows description text from the ACPI _STR method */
573 static ssize_t description_show(struct device *dev,
574 struct device_attribute *attr,
576 struct acpi_device *acpi_dev = to_acpi_device(dev);
579 if (acpi_dev->pnp.str_obj == NULL)
583 * The _STR object contains a Unicode identifier for a device.
584 * We need to convert to utf-8 so it can be displayed.
586 result = utf16s_to_utf8s(
587 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
588 acpi_dev->pnp.str_obj->buffer.length,
589 UTF16_LITTLE_ENDIAN, buf,
592 buf[result++] = '\n';
596 static DEVICE_ATTR(description, 0444, description_show, NULL);
599 acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
601 struct acpi_device *acpi_dev = to_acpi_device(dev);
603 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
605 static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
607 static int acpi_device_setup_files(struct acpi_device *dev)
609 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
611 unsigned long long sun;
615 * Devices gotten from FADT don't have a "path" attribute
618 result = device_create_file(&dev->dev, &dev_attr_path);
623 if (!list_empty(&dev->pnp.ids)) {
624 result = device_create_file(&dev->dev, &dev_attr_hid);
628 result = device_create_file(&dev->dev, &dev_attr_modalias);
634 * If device has _STR, 'description' file is created
636 if (acpi_has_method(dev->handle, "_STR")) {
637 status = acpi_evaluate_object(dev->handle, "_STR",
639 if (ACPI_FAILURE(status))
640 buffer.pointer = NULL;
641 dev->pnp.str_obj = buffer.pointer;
642 result = device_create_file(&dev->dev, &dev_attr_description);
647 if (dev->pnp.type.bus_address)
648 result = device_create_file(&dev->dev, &dev_attr_adr);
649 if (dev->pnp.unique_id)
650 result = device_create_file(&dev->dev, &dev_attr_uid);
652 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
653 if (ACPI_SUCCESS(status)) {
654 dev->pnp.sun = (unsigned long)sun;
655 result = device_create_file(&dev->dev, &dev_attr_sun);
659 dev->pnp.sun = (unsigned long)-1;
663 * If device has _EJ0, 'eject' file is created that is used to trigger
664 * hot-removal function from userland.
666 if (acpi_has_method(dev->handle, "_EJ0")) {
667 result = device_create_file(&dev->dev, &dev_attr_eject);
672 if (dev->flags.power_manageable) {
673 result = device_create_file(&dev->dev, &dev_attr_power_state);
677 if (dev->power.flags.power_resources)
678 result = device_create_file(&dev->dev,
679 &dev_attr_real_power_state);
686 static void acpi_device_remove_files(struct acpi_device *dev)
688 if (dev->flags.power_manageable) {
689 device_remove_file(&dev->dev, &dev_attr_power_state);
690 if (dev->power.flags.power_resources)
691 device_remove_file(&dev->dev,
692 &dev_attr_real_power_state);
696 * If device has _STR, remove 'description' file
698 if (acpi_has_method(dev->handle, "_STR")) {
699 kfree(dev->pnp.str_obj);
700 device_remove_file(&dev->dev, &dev_attr_description);
703 * If device has _EJ0, remove 'eject' file.
705 if (acpi_has_method(dev->handle, "_EJ0"))
706 device_remove_file(&dev->dev, &dev_attr_eject);
708 if (acpi_has_method(dev->handle, "_SUN"))
709 device_remove_file(&dev->dev, &dev_attr_sun);
711 if (dev->pnp.unique_id)
712 device_remove_file(&dev->dev, &dev_attr_uid);
713 if (dev->pnp.type.bus_address)
714 device_remove_file(&dev->dev, &dev_attr_adr);
715 device_remove_file(&dev->dev, &dev_attr_modalias);
716 device_remove_file(&dev->dev, &dev_attr_hid);
718 device_remove_file(&dev->dev, &dev_attr_path);
720 /* --------------------------------------------------------------------------
722 -------------------------------------------------------------------------- */
724 static const struct acpi_device_id *__acpi_match_device(
725 struct acpi_device *device, const struct acpi_device_id *ids)
727 const struct acpi_device_id *id;
728 struct acpi_hardware_id *hwid;
731 * If the device is not present, it is unnecessary to load device
734 if (!device->status.present)
737 for (id = ids; id->id[0]; id++)
738 list_for_each_entry(hwid, &device->pnp.ids, list)
739 if (!strcmp((char *) id->id, hwid->id))
746 * acpi_match_device - Match a struct device against a given list of ACPI IDs
747 * @ids: Array of struct acpi_device_id object to match against.
748 * @dev: The device structure to match.
750 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
751 * object for that handle and use that object to match against a given list of
754 * Return a pointer to the first matching ID on success or %NULL on failure.
756 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
757 const struct device *dev)
759 struct acpi_device *adev;
760 acpi_handle handle = ACPI_HANDLE(dev);
762 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
765 return __acpi_match_device(adev, ids);
767 EXPORT_SYMBOL_GPL(acpi_match_device);
769 int acpi_match_device_ids(struct acpi_device *device,
770 const struct acpi_device_id *ids)
772 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
774 EXPORT_SYMBOL(acpi_match_device_ids);
776 static void acpi_free_power_resources_lists(struct acpi_device *device)
780 if (device->wakeup.flags.valid)
781 acpi_power_resources_list_free(&device->wakeup.resources);
783 if (!device->flags.power_manageable)
786 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
787 struct acpi_device_power_state *ps = &device->power.states[i];
788 acpi_power_resources_list_free(&ps->resources);
792 static void acpi_device_release(struct device *dev)
794 struct acpi_device *acpi_dev = to_acpi_device(dev);
796 acpi_free_pnp_ids(&acpi_dev->pnp);
797 acpi_free_power_resources_lists(acpi_dev);
801 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
803 struct acpi_device *acpi_dev = to_acpi_device(dev);
804 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
806 return acpi_dev->flags.match_driver
807 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
810 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
812 struct acpi_device *acpi_dev = to_acpi_device(dev);
815 if (list_empty(&acpi_dev->pnp.ids))
818 if (add_uevent_var(env, "MODALIAS="))
820 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
821 sizeof(env->buf) - env->buflen);
822 if (len >= (sizeof(env->buf) - env->buflen))
828 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
830 struct acpi_device *device = data;
832 device->driver->ops.notify(device, event);
835 static acpi_status acpi_device_notify_fixed(void *data)
837 struct acpi_device *device = data;
839 /* Fixed hardware devices have no handles */
840 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
844 static int acpi_device_install_notify_handler(struct acpi_device *device)
848 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
850 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
851 acpi_device_notify_fixed,
853 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
855 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
856 acpi_device_notify_fixed,
859 status = acpi_install_notify_handler(device->handle,
864 if (ACPI_FAILURE(status))
869 static void acpi_device_remove_notify_handler(struct acpi_device *device)
871 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
872 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
873 acpi_device_notify_fixed);
874 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
875 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
876 acpi_device_notify_fixed);
878 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
882 static int acpi_device_probe(struct device *dev)
884 struct acpi_device *acpi_dev = to_acpi_device(dev);
885 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
888 if (acpi_dev->handler)
891 if (!acpi_drv->ops.add)
894 ret = acpi_drv->ops.add(acpi_dev);
898 acpi_dev->driver = acpi_drv;
899 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
900 "Driver [%s] successfully bound to device [%s]\n",
901 acpi_drv->name, acpi_dev->pnp.bus_id));
903 if (acpi_drv->ops.notify) {
904 ret = acpi_device_install_notify_handler(acpi_dev);
906 if (acpi_drv->ops.remove)
907 acpi_drv->ops.remove(acpi_dev);
909 acpi_dev->driver = NULL;
910 acpi_dev->driver_data = NULL;
915 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
916 acpi_drv->name, acpi_dev->pnp.bus_id));
921 static int acpi_device_remove(struct device * dev)
923 struct acpi_device *acpi_dev = to_acpi_device(dev);
924 struct acpi_driver *acpi_drv = acpi_dev->driver;
927 if (acpi_drv->ops.notify)
928 acpi_device_remove_notify_handler(acpi_dev);
929 if (acpi_drv->ops.remove)
930 acpi_drv->ops.remove(acpi_dev);
932 acpi_dev->driver = NULL;
933 acpi_dev->driver_data = NULL;
939 struct bus_type acpi_bus_type = {
941 .match = acpi_bus_match,
942 .probe = acpi_device_probe,
943 .remove = acpi_device_remove,
944 .uevent = acpi_device_uevent,
947 static void acpi_bus_data_handler(acpi_handle handle, void *context)
949 /* Intentionally empty. */
952 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
959 status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
960 if (ACPI_FAILURE(status) || !*device) {
961 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
967 EXPORT_SYMBOL_GPL(acpi_bus_get_device);
969 int acpi_device_add(struct acpi_device *device,
970 void (*release)(struct device *))
973 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
976 if (device->handle) {
979 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
981 if (ACPI_FAILURE(status)) {
982 acpi_handle_err(device->handle,
983 "Unable to attach device data\n");
991 * Link this device to its parent and siblings.
993 INIT_LIST_HEAD(&device->children);
994 INIT_LIST_HEAD(&device->node);
995 INIT_LIST_HEAD(&device->wakeup_list);
996 INIT_LIST_HEAD(&device->physical_node_list);
997 mutex_init(&device->physical_node_lock);
998 INIT_LIST_HEAD(&device->power_dependent);
1000 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1002 pr_err(PREFIX "Memory allocation error\n");
1007 mutex_lock(&acpi_device_lock);
1009 * Find suitable bus_id and instance number in acpi_bus_id_list
1010 * If failed, create one and link it into acpi_bus_id_list
1012 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
1013 if (!strcmp(acpi_device_bus_id->bus_id,
1014 acpi_device_hid(device))) {
1015 acpi_device_bus_id->instance_no++;
1022 acpi_device_bus_id = new_bus_id;
1023 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
1024 acpi_device_bus_id->instance_no = 0;
1025 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1027 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
1030 list_add_tail(&device->node, &device->parent->children);
1032 if (device->wakeup.flags.valid)
1033 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
1034 mutex_unlock(&acpi_device_lock);
1037 device->dev.parent = &device->parent->dev;
1038 device->dev.bus = &acpi_bus_type;
1039 device->dev.release = release;
1040 result = device_add(&device->dev);
1042 dev_err(&device->dev, "Error registering device\n");
1046 result = acpi_device_setup_files(device);
1048 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1049 dev_name(&device->dev));
1054 mutex_lock(&acpi_device_lock);
1056 list_del(&device->node);
1057 list_del(&device->wakeup_list);
1058 mutex_unlock(&acpi_device_lock);
1061 acpi_detach_data(device->handle, acpi_bus_data_handler);
1065 static void acpi_device_unregister(struct acpi_device *device)
1067 mutex_lock(&acpi_device_lock);
1069 list_del(&device->node);
1071 list_del(&device->wakeup_list);
1072 mutex_unlock(&acpi_device_lock);
1074 acpi_detach_data(device->handle, acpi_bus_data_handler);
1076 acpi_power_add_remove_device(device, false);
1077 acpi_device_remove_files(device);
1079 device->remove(device);
1081 device_del(&device->dev);
1083 * Transition the device to D3cold to drop the reference counts of all
1084 * power resources the device depends on and turn off the ones that have
1085 * no more references.
1087 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
1088 device->handle = NULL;
1089 put_device(&device->dev);
1092 /* --------------------------------------------------------------------------
1094 -------------------------------------------------------------------------- */
1096 * acpi_bus_register_driver - register a driver with the ACPI bus
1097 * @driver: driver being registered
1099 * Registers a driver with the ACPI bus. Searches the namespace for all
1100 * devices that match the driver's criteria and binds. Returns zero for
1101 * success or a negative error status for failure.
1103 int acpi_bus_register_driver(struct acpi_driver *driver)
1109 driver->drv.name = driver->name;
1110 driver->drv.bus = &acpi_bus_type;
1111 driver->drv.owner = driver->owner;
1113 ret = driver_register(&driver->drv);
1117 EXPORT_SYMBOL(acpi_bus_register_driver);
1120 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
1121 * @driver: driver to unregister
1123 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1124 * devices that match the driver's criteria and unbinds.
1126 void acpi_bus_unregister_driver(struct acpi_driver *driver)
1128 driver_unregister(&driver->drv);
1131 EXPORT_SYMBOL(acpi_bus_unregister_driver);
1133 /* --------------------------------------------------------------------------
1135 -------------------------------------------------------------------------- */
1136 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1138 struct acpi_device *device = NULL;
1142 * Fixed hardware devices do not appear in the namespace and do not
1143 * have handles, but we fabricate acpi_devices for them, so we have
1144 * to deal with them specially.
1150 status = acpi_get_parent(handle, &handle);
1151 if (ACPI_FAILURE(status))
1152 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1153 } while (acpi_bus_get_device(handle, &device));
1158 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1162 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1163 union acpi_object *obj;
1165 status = acpi_get_handle(handle, "_EJD", &tmp);
1166 if (ACPI_FAILURE(status))
1169 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1170 if (ACPI_SUCCESS(status)) {
1171 obj = buffer.pointer;
1172 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1174 kfree(buffer.pointer);
1178 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1180 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1181 struct acpi_device_wakeup *wakeup)
1183 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1184 union acpi_object *package = NULL;
1185 union acpi_object *element = NULL;
1192 INIT_LIST_HEAD(&wakeup->resources);
1195 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1196 if (ACPI_FAILURE(status)) {
1197 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
1201 package = (union acpi_object *)buffer.pointer;
1203 if (!package || package->package.count < 2)
1206 element = &(package->package.elements[0]);
1210 if (element->type == ACPI_TYPE_PACKAGE) {
1211 if ((element->package.count < 2) ||
1212 (element->package.elements[0].type !=
1213 ACPI_TYPE_LOCAL_REFERENCE)
1214 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
1217 wakeup->gpe_device =
1218 element->package.elements[0].reference.handle;
1219 wakeup->gpe_number =
1220 (u32) element->package.elements[1].integer.value;
1221 } else if (element->type == ACPI_TYPE_INTEGER) {
1222 wakeup->gpe_device = NULL;
1223 wakeup->gpe_number = element->integer.value;
1228 element = &(package->package.elements[1]);
1229 if (element->type != ACPI_TYPE_INTEGER)
1232 wakeup->sleep_state = element->integer.value;
1234 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1238 if (!list_empty(&wakeup->resources)) {
1241 err = acpi_power_wakeup_list_init(&wakeup->resources,
1244 acpi_handle_warn(handle, "Retrieving current states "
1245 "of wakeup power resources failed\n");
1246 acpi_power_resources_list_free(&wakeup->resources);
1249 if (sleep_state < wakeup->sleep_state) {
1250 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1251 "(S%d) by S%d from power resources\n",
1252 (int)wakeup->sleep_state, sleep_state);
1253 wakeup->sleep_state = sleep_state;
1256 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
1259 kfree(buffer.pointer);
1263 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
1265 struct acpi_device_id button_device_ids[] = {
1272 acpi_event_status event_status;
1274 device->wakeup.flags.notifier_present = 0;
1276 /* Power button, Lid switch always enable wakeup */
1277 if (!acpi_match_device_ids(device, button_device_ids)) {
1278 device->wakeup.flags.run_wake = 1;
1279 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1280 /* Do not use Lid/sleep button for S5 wakeup */
1281 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1282 device->wakeup.sleep_state = ACPI_STATE_S4;
1284 device_set_wakeup_capable(&device->dev, true);
1288 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1289 device->wakeup.gpe_number,
1291 if (status == AE_OK)
1292 device->wakeup.flags.run_wake =
1293 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1296 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1300 /* Presence of _PRW indicates wake capable */
1301 if (!acpi_has_method(device->handle, "_PRW"))
1304 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1307 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1311 device->wakeup.flags.valid = 1;
1312 device->wakeup.prepare_count = 0;
1313 acpi_bus_set_run_wake_flags(device);
1314 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1315 * system for the ACPI device with the _PRW object.
1316 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1317 * So it is necessary to call _DSW object first. Only when it is not
1318 * present will the _PSW object used.
1320 err = acpi_device_sleep_wake(device, 0, 0, 0);
1322 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1323 "error in _DSW or _PSW evaluation\n"));
1326 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1328 struct acpi_device_power_state *ps = &device->power.states[state];
1329 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1330 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1333 INIT_LIST_HEAD(&ps->resources);
1335 /* Evaluate "_PRx" to get referenced power resources */
1336 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1337 if (ACPI_SUCCESS(status)) {
1338 union acpi_object *package = buffer.pointer;
1340 if (buffer.length && package
1341 && package->type == ACPI_TYPE_PACKAGE
1342 && package->package.count) {
1343 int err = acpi_extract_power_resources(package, 0,
1346 device->power.flags.power_resources = 1;
1348 ACPI_FREE(buffer.pointer);
1351 /* Evaluate "_PSx" to see if we can do explicit sets */
1353 if (acpi_has_method(device->handle, pathname))
1354 ps->flags.explicit_set = 1;
1357 * State is valid if there are means to put the device into it.
1358 * D3hot is only valid if _PR3 present.
1360 if (!list_empty(&ps->resources)
1361 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1362 ps->flags.valid = 1;
1363 ps->flags.os_accessible = 1;
1366 ps->power = -1; /* Unknown - driver assigned */
1367 ps->latency = -1; /* Unknown - driver assigned */
1370 static void acpi_bus_get_power_flags(struct acpi_device *device)
1374 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1375 if (!acpi_has_method(device->handle, "_PS0") &&
1376 !acpi_has_method(device->handle, "_PR0"))
1379 device->flags.power_manageable = 1;
1382 * Power Management Flags
1384 if (acpi_has_method(device->handle, "_PSC"))
1385 device->power.flags.explicit_get = 1;
1386 if (acpi_has_method(device->handle, "_IRC"))
1387 device->power.flags.inrush_current = 1;
1390 * Enumerate supported power management states
1392 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1393 acpi_bus_init_power_state(device, i);
1395 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1397 /* Set defaults for D0 and D3 states (always valid) */
1398 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1399 device->power.states[ACPI_STATE_D0].power = 100;
1400 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1401 device->power.states[ACPI_STATE_D3_COLD].power = 0;
1403 /* Set D3cold's explicit_set flag if _PS3 exists. */
1404 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1405 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1407 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1408 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1409 device->power.flags.power_resources)
1410 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1412 if (acpi_bus_init_power(device)) {
1413 acpi_free_power_resources_lists(device);
1414 device->flags.power_manageable = 0;
1418 static void acpi_bus_get_flags(struct acpi_device *device)
1420 /* Presence of _STA indicates 'dynamic_status' */
1421 if (acpi_has_method(device->handle, "_STA"))
1422 device->flags.dynamic_status = 1;
1424 /* Presence of _RMV indicates 'removable' */
1425 if (acpi_has_method(device->handle, "_RMV"))
1426 device->flags.removable = 1;
1428 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1429 if (acpi_has_method(device->handle, "_EJD") ||
1430 acpi_has_method(device->handle, "_EJ0"))
1431 device->flags.ejectable = 1;
1434 static void acpi_device_get_busid(struct acpi_device *device)
1436 char bus_id[5] = { '?', 0 };
1437 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1443 * The device's Bus ID is simply the object name.
1444 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1446 if (ACPI_IS_ROOT_DEVICE(device)) {
1447 strcpy(device->pnp.bus_id, "ACPI");
1451 switch (device->device_type) {
1452 case ACPI_BUS_TYPE_POWER_BUTTON:
1453 strcpy(device->pnp.bus_id, "PWRF");
1455 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1456 strcpy(device->pnp.bus_id, "SLPF");
1459 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1460 /* Clean up trailing underscores (if any) */
1461 for (i = 3; i > 1; i--) {
1462 if (bus_id[i] == '_')
1467 strcpy(device->pnp.bus_id, bus_id);
1473 * acpi_ata_match - see if an acpi object is an ATA device
1475 * If an acpi object has one of the ACPI ATA methods defined,
1476 * then we can safely call it an ATA device.
1478 bool acpi_ata_match(acpi_handle handle)
1480 return acpi_has_method(handle, "_GTF") ||
1481 acpi_has_method(handle, "_GTM") ||
1482 acpi_has_method(handle, "_STM") ||
1483 acpi_has_method(handle, "_SDD");
1487 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1489 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1490 * then we can safely call it an ejectable drive bay
1492 bool acpi_bay_match(acpi_handle handle)
1494 acpi_handle phandle;
1496 if (!acpi_has_method(handle, "_EJ0"))
1498 if (acpi_ata_match(handle))
1500 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1503 return acpi_ata_match(phandle);
1507 * acpi_dock_match - see if an acpi object has a _DCK method
1509 bool acpi_dock_match(acpi_handle handle)
1511 return acpi_has_method(handle, "_DCK");
1514 const char *acpi_device_hid(struct acpi_device *device)
1516 struct acpi_hardware_id *hid;
1518 if (list_empty(&device->pnp.ids))
1521 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1524 EXPORT_SYMBOL(acpi_device_hid);
1526 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1528 struct acpi_hardware_id *id;
1530 id = kmalloc(sizeof(*id), GFP_KERNEL);
1534 id->id = kstrdup(dev_id, GFP_KERNEL);
1540 list_add_tail(&id->list, &pnp->ids);
1541 pnp->type.hardware_id = 1;
1545 * Old IBM workstations have a DSDT bug wherein the SMBus object
1546 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1547 * prefix. Work around this.
1549 static bool acpi_ibm_smbus_match(acpi_handle handle)
1551 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1552 struct acpi_buffer path = { sizeof(node_name), node_name };
1554 if (!dmi_name_in_vendors("IBM"))
1557 /* Look for SMBS object */
1558 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1559 strcmp("SMBS", path.pointer))
1562 /* Does it have the necessary (but misnamed) methods? */
1563 if (acpi_has_method(handle, "SBI") &&
1564 acpi_has_method(handle, "SBR") &&
1565 acpi_has_method(handle, "SBW"))
1571 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1575 struct acpi_device_info *info;
1576 struct acpi_pnp_device_id_list *cid_list;
1579 switch (device_type) {
1580 case ACPI_BUS_TYPE_DEVICE:
1581 if (handle == ACPI_ROOT_OBJECT) {
1582 acpi_add_id(pnp, ACPI_SYSTEM_HID);
1586 status = acpi_get_object_info(handle, &info);
1587 if (ACPI_FAILURE(status)) {
1588 pr_err(PREFIX "%s: Error reading device info\n",
1593 if (info->valid & ACPI_VALID_HID)
1594 acpi_add_id(pnp, info->hardware_id.string);
1595 if (info->valid & ACPI_VALID_CID) {
1596 cid_list = &info->compatible_id_list;
1597 for (i = 0; i < cid_list->count; i++)
1598 acpi_add_id(pnp, cid_list->ids[i].string);
1600 if (info->valid & ACPI_VALID_ADR) {
1601 pnp->bus_address = info->address;
1602 pnp->type.bus_address = 1;
1604 if (info->valid & ACPI_VALID_UID)
1605 pnp->unique_id = kstrdup(info->unique_id.string,
1611 * Some devices don't reliably have _HIDs & _CIDs, so add
1612 * synthetic HIDs to make sure drivers can find them.
1614 if (acpi_is_video_device(handle))
1615 acpi_add_id(pnp, ACPI_VIDEO_HID);
1616 else if (acpi_bay_match(handle))
1617 acpi_add_id(pnp, ACPI_BAY_HID);
1618 else if (acpi_dock_match(handle))
1619 acpi_add_id(pnp, ACPI_DOCK_HID);
1620 else if (acpi_ibm_smbus_match(handle))
1621 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1622 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1623 acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1624 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1625 strcpy(pnp->device_class, ACPI_BUS_CLASS);
1629 case ACPI_BUS_TYPE_POWER:
1630 acpi_add_id(pnp, ACPI_POWER_HID);
1632 case ACPI_BUS_TYPE_PROCESSOR:
1633 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1635 case ACPI_BUS_TYPE_THERMAL:
1636 acpi_add_id(pnp, ACPI_THERMAL_HID);
1638 case ACPI_BUS_TYPE_POWER_BUTTON:
1639 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1641 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1642 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1647 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1649 struct acpi_hardware_id *id, *tmp;
1651 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1655 kfree(pnp->unique_id);
1658 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1659 int type, unsigned long long sta)
1661 INIT_LIST_HEAD(&device->pnp.ids);
1662 device->device_type = type;
1663 device->handle = handle;
1664 device->parent = acpi_bus_get_parent(handle);
1665 STRUCT_TO_INT(device->status) = sta;
1666 acpi_device_get_busid(device);
1667 acpi_set_pnp_ids(handle, &device->pnp, type);
1668 acpi_bus_get_flags(device);
1669 device->flags.match_driver = false;
1670 device_initialize(&device->dev);
1671 dev_set_uevent_suppress(&device->dev, true);
1674 void acpi_device_add_finalize(struct acpi_device *device)
1676 dev_set_uevent_suppress(&device->dev, false);
1677 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1680 static int acpi_add_single_object(struct acpi_device **child,
1681 acpi_handle handle, int type,
1682 unsigned long long sta)
1685 struct acpi_device *device;
1686 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1688 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1690 printk(KERN_ERR PREFIX "Memory allocation error\n");
1694 acpi_init_device_object(device, handle, type, sta);
1695 acpi_bus_get_power_flags(device);
1696 acpi_bus_get_wakeup_device_flags(device);
1698 result = acpi_device_add(device, acpi_device_release);
1700 acpi_device_release(&device->dev);
1704 acpi_power_add_remove_device(device, true);
1705 acpi_device_add_finalize(device);
1706 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1707 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1708 dev_name(&device->dev), (char *) buffer.pointer,
1709 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1710 kfree(buffer.pointer);
1715 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1716 unsigned long long *sta)
1719 acpi_object_type acpi_type;
1721 status = acpi_get_type(handle, &acpi_type);
1722 if (ACPI_FAILURE(status))
1725 switch (acpi_type) {
1726 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1727 case ACPI_TYPE_DEVICE:
1728 *type = ACPI_BUS_TYPE_DEVICE;
1729 status = acpi_bus_get_status_handle(handle, sta);
1730 if (ACPI_FAILURE(status))
1733 case ACPI_TYPE_PROCESSOR:
1734 *type = ACPI_BUS_TYPE_PROCESSOR;
1735 status = acpi_bus_get_status_handle(handle, sta);
1736 if (ACPI_FAILURE(status))
1739 case ACPI_TYPE_THERMAL:
1740 *type = ACPI_BUS_TYPE_THERMAL;
1741 *sta = ACPI_STA_DEFAULT;
1743 case ACPI_TYPE_POWER:
1744 *type = ACPI_BUS_TYPE_POWER;
1745 *sta = ACPI_STA_DEFAULT;
1754 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1756 const struct acpi_device_id **matchid)
1758 const struct acpi_device_id *devid;
1760 for (devid = handler->ids; devid->id[0]; devid++)
1761 if (!strcmp((char *)devid->id, idstr)) {
1771 static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1772 const struct acpi_device_id **matchid)
1774 struct acpi_scan_handler *handler;
1776 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1777 if (acpi_scan_handler_matching(handler, idstr, matchid))
1783 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1785 if (!!hotplug->enabled == !!val)
1788 mutex_lock(&acpi_scan_lock);
1790 hotplug->enabled = val;
1792 mutex_unlock(&acpi_scan_lock);
1795 static void acpi_scan_init_hotplug(acpi_handle handle, int type)
1797 struct acpi_device_pnp pnp = {};
1798 struct acpi_hardware_id *hwid;
1799 struct acpi_scan_handler *handler;
1801 INIT_LIST_HEAD(&pnp.ids);
1802 acpi_set_pnp_ids(handle, &pnp, type);
1804 if (!pnp.type.hardware_id)
1808 * This relies on the fact that acpi_install_notify_handler() will not
1809 * install the same notify handler routine twice for the same handle.
1811 list_for_each_entry(hwid, &pnp.ids, list) {
1812 handler = acpi_scan_match_handler(hwid->id, NULL);
1814 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1815 acpi_hotplug_notify_cb, handler);
1821 acpi_free_pnp_ids(&pnp);
1824 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1825 void *not_used, void **return_value)
1827 struct acpi_device *device = NULL;
1829 unsigned long long sta;
1832 acpi_bus_get_device(handle, &device);
1836 result = acpi_bus_type_and_status(handle, &type, &sta);
1840 if (type == ACPI_BUS_TYPE_POWER) {
1841 acpi_add_power_resource(handle);
1845 acpi_scan_init_hotplug(handle, type);
1847 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1848 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
1849 struct acpi_device_wakeup wakeup;
1851 if (acpi_has_method(handle, "_PRW")) {
1852 acpi_bus_extract_wakeup_device_power_package(handle,
1854 acpi_power_resources_list_free(&wakeup.resources);
1856 return AE_CTRL_DEPTH;
1859 acpi_add_single_object(&device, handle, type, sta);
1861 return AE_CTRL_DEPTH;
1865 *return_value = device;
1870 static int acpi_scan_attach_handler(struct acpi_device *device)
1872 struct acpi_hardware_id *hwid;
1875 list_for_each_entry(hwid, &device->pnp.ids, list) {
1876 const struct acpi_device_id *devid;
1877 struct acpi_scan_handler *handler;
1879 handler = acpi_scan_match_handler(hwid->id, &devid);
1881 ret = handler->attach(device, devid);
1883 device->handler = handler;
1885 } else if (ret < 0) {
1893 static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1894 void *not_used, void **ret_not_used)
1896 struct acpi_device *device;
1897 unsigned long long sta_not_used;
1901 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1902 * namespace walks prematurely.
1904 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
1907 if (acpi_bus_get_device(handle, &device))
1908 return AE_CTRL_DEPTH;
1910 if (device->handler)
1913 ret = acpi_scan_attach_handler(device);
1915 return AE_CTRL_DEPTH;
1917 device->flags.match_driver = true;
1921 ret = device_attach(&device->dev);
1922 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
1926 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1927 * @handle: Root of the namespace scope to scan.
1929 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1932 * If no devices were found, -ENODEV is returned, but it does not mean that
1933 * there has been a real error. There just have been no suitable ACPI objects
1934 * in the table trunk from which the kernel could create a device and add an
1935 * appropriate driver.
1937 * Must be called under acpi_scan_lock.
1939 int acpi_bus_scan(acpi_handle handle)
1941 void *device = NULL;
1944 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
1945 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1946 acpi_bus_check_add, NULL, NULL, &device);
1950 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
1951 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1952 acpi_bus_device_attach, NULL, NULL, NULL);
1956 EXPORT_SYMBOL(acpi_bus_scan);
1958 static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1959 void *not_used, void **ret_not_used)
1961 struct acpi_device *device = NULL;
1963 if (!acpi_bus_get_device(handle, &device)) {
1964 struct acpi_scan_handler *dev_handler = device->handler;
1967 if (dev_handler->detach)
1968 dev_handler->detach(device);
1970 device->handler = NULL;
1972 device_release_driver(&device->dev);
1978 static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1979 void *not_used, void **ret_not_used)
1981 struct acpi_device *device = NULL;
1983 if (!acpi_bus_get_device(handle, &device))
1984 acpi_device_unregister(device);
1990 * acpi_bus_trim - Remove ACPI device node and all of its descendants
1991 * @start: Root of the ACPI device nodes subtree to remove.
1993 * Must be called under acpi_scan_lock.
1995 void acpi_bus_trim(struct acpi_device *start)
1998 * Execute acpi_bus_device_detach() as a post-order callback to detach
1999 * all ACPI drivers from the device nodes being removed.
2001 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2002 acpi_bus_device_detach, NULL, NULL);
2003 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
2005 * Execute acpi_bus_remove() as a post-order callback to remove device
2006 * nodes in the given namespace scope.
2008 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2009 acpi_bus_remove, NULL, NULL);
2010 acpi_bus_remove(start->handle, 0, NULL, NULL);
2012 EXPORT_SYMBOL_GPL(acpi_bus_trim);
2014 static int acpi_bus_scan_fixed(void)
2019 * Enumerate all fixed-feature devices.
2021 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2022 struct acpi_device *device = NULL;
2024 result = acpi_add_single_object(&device, NULL,
2025 ACPI_BUS_TYPE_POWER_BUTTON,
2030 result = device_attach(&device->dev);
2034 device_init_wakeup(&device->dev, true);
2037 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2038 struct acpi_device *device = NULL;
2040 result = acpi_add_single_object(&device, NULL,
2041 ACPI_BUS_TYPE_SLEEP_BUTTON,
2046 result = device_attach(&device->dev);
2049 return result < 0 ? result : 0;
2052 int __init acpi_scan_init(void)
2056 result = bus_register(&acpi_bus_type);
2058 /* We don't want to quit even if we failed to add suspend/resume */
2059 printk(KERN_ERR PREFIX "Could not register bus type\n");
2062 acpi_pci_root_init();
2063 acpi_pci_link_init();
2064 acpi_processor_init();
2065 acpi_platform_init();
2067 acpi_cmos_rtc_init();
2068 acpi_container_init();
2069 acpi_memory_hotplug_init();
2072 mutex_lock(&acpi_scan_lock);
2074 * Enumerate devices in the ACPI namespace.
2076 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2080 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2084 result = acpi_bus_scan_fixed();
2086 acpi_device_unregister(acpi_root);
2090 acpi_update_all_gpes();
2092 acpi_pci_root_hp_init();
2095 mutex_unlock(&acpi_scan_lock);