xref: /openbmc/linux/drivers/acpi/scan.c (revision 8832f7e43fa7f0f19bd54e13766a825dd1ed4d6f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * scan.c - support for transforming the ACPI namespace into individual objects
31da177e4SLinus Torvalds  */
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <linux/module.h>
61da177e4SLinus Torvalds #include <linux/init.h>
75a0e3ad6STejun Heo #include <linux/slab.h>
89b6d97b6SRandy Dunlap #include <linux/kernel.h>
91da177e4SLinus Torvalds #include <linux/acpi.h>
1074523c90SAlok N Kataria #include <linux/signal.h>
1174523c90SAlok N Kataria #include <linux/kthread.h>
12222e82acSDarrick J. Wong #include <linux/dmi.h>
13d1efe3c3SLance Ortiz #include <linux/nls.h>
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #include <acpi/acpi_drivers.h>
161da177e4SLinus Torvalds 
17e60cc7a6SBjorn Helgaas #include "internal.h"
18e60cc7a6SBjorn Helgaas 
191da177e4SLinus Torvalds #define _COMPONENT		ACPI_BUS_COMPONENT
20f52fd66dSLen Brown ACPI_MODULE_NAME("scan");
211da177e4SLinus Torvalds #define STRUCT_TO_INT(s)	(*((int*)&s))
221da177e4SLinus Torvalds extern struct acpi_device *acpi_root;
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #define ACPI_BUS_CLASS			"system_bus"
2529b71a1cSThomas Renninger #define ACPI_BUS_HID			"LNXSYBUS"
261da177e4SLinus Torvalds #define ACPI_BUS_DEVICE_NAME		"System Bus"
271da177e4SLinus Torvalds 
28859ac9a4SBjorn Helgaas #define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
29859ac9a4SBjorn Helgaas 
30683058e3SRafael J. Wysocki /*
31683058e3SRafael J. Wysocki  * If set, devices will be hot-removed even if they cannot be put offline
32683058e3SRafael J. Wysocki  * gracefully (from the kernel's standpoint).
33683058e3SRafael J. Wysocki  */
34683058e3SRafael J. Wysocki bool acpi_force_hot_remove;
35683058e3SRafael J. Wysocki 
36620e112cSThomas Renninger static const char *dummy_hid = "device";
372b2ae7c7SThomas Renninger 
38e49bd2ddSZhang Rui static LIST_HEAD(acpi_bus_id_list);
39c511cc19SRafael J. Wysocki static DEFINE_MUTEX(acpi_scan_lock);
40ca589f94SRafael J. Wysocki static LIST_HEAD(acpi_scan_handlers_list);
419090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock);
421da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
431da177e4SLinus Torvalds 
44e49bd2ddSZhang Rui struct acpi_device_bus_id{
45bb095854SZhang Rui 	char bus_id[15];
46e49bd2ddSZhang Rui 	unsigned int instance_no;
47e49bd2ddSZhang Rui 	struct list_head node;
48e49bd2ddSZhang Rui };
4929b71a1cSThomas Renninger 
503757b948SRafael J. Wysocki void acpi_scan_lock_acquire(void)
513757b948SRafael J. Wysocki {
523757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
533757b948SRafael J. Wysocki }
543757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
553757b948SRafael J. Wysocki 
563757b948SRafael J. Wysocki void acpi_scan_lock_release(void)
573757b948SRafael J. Wysocki {
583757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
593757b948SRafael J. Wysocki }
603757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
613757b948SRafael J. Wysocki 
62ca589f94SRafael J. Wysocki int acpi_scan_add_handler(struct acpi_scan_handler *handler)
63ca589f94SRafael J. Wysocki {
64ca589f94SRafael J. Wysocki 	if (!handler || !handler->attach)
65ca589f94SRafael J. Wysocki 		return -EINVAL;
66ca589f94SRafael J. Wysocki 
67ca589f94SRafael J. Wysocki 	list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
68ca589f94SRafael J. Wysocki 	return 0;
69ca589f94SRafael J. Wysocki }
70ca589f94SRafael J. Wysocki 
713f8055c3SRafael J. Wysocki int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
723f8055c3SRafael J. Wysocki 				       const char *hotplug_profile_name)
733f8055c3SRafael J. Wysocki {
743f8055c3SRafael J. Wysocki 	int error;
753f8055c3SRafael J. Wysocki 
763f8055c3SRafael J. Wysocki 	error = acpi_scan_add_handler(handler);
773f8055c3SRafael J. Wysocki 	if (error)
783f8055c3SRafael J. Wysocki 		return error;
793f8055c3SRafael J. Wysocki 
803f8055c3SRafael J. Wysocki 	acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
813f8055c3SRafael J. Wysocki 	return 0;
823f8055c3SRafael J. Wysocki }
833f8055c3SRafael J. Wysocki 
8429b71a1cSThomas Renninger /*
8529b71a1cSThomas Renninger  * Creates hid/cid(s) string needed for modalias and uevent
8629b71a1cSThomas Renninger  * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
8729b71a1cSThomas Renninger  * char *modalias: "acpi:IBM0001:ACPI0001"
8829b71a1cSThomas Renninger */
89b3e572d2SAdrian Bunk static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
90b3e572d2SAdrian Bunk 			   int size)
91b3e572d2SAdrian Bunk {
9229b71a1cSThomas Renninger 	int len;
935c9fcb5dSZhang Rui 	int count;
947f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
9529b71a1cSThomas Renninger 
962b2ae7c7SThomas Renninger 	if (list_empty(&acpi_dev->pnp.ids))
972b2ae7c7SThomas Renninger 		return 0;
982b2ae7c7SThomas Renninger 
995c9fcb5dSZhang Rui 	len = snprintf(modalias, size, "acpi:");
10029b71a1cSThomas Renninger 	size -= len;
10129b71a1cSThomas Renninger 
1027f47fa6cSBjorn Helgaas 	list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
1037f47fa6cSBjorn Helgaas 		count = snprintf(&modalias[len], size, "%s:", id->id);
1045c9fcb5dSZhang Rui 		if (count < 0 || count >= size)
1055c9fcb5dSZhang Rui 			return -EINVAL;
1065c9fcb5dSZhang Rui 		len += count;
1075c9fcb5dSZhang Rui 		size -= count;
1085c9fcb5dSZhang Rui 	}
1095c9fcb5dSZhang Rui 
11029b71a1cSThomas Renninger 	modalias[len] = '\0';
11129b71a1cSThomas Renninger 	return len;
11229b71a1cSThomas Renninger }
11329b71a1cSThomas Renninger 
11429b71a1cSThomas Renninger static ssize_t
11529b71a1cSThomas Renninger acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
11629b71a1cSThomas Renninger 	struct acpi_device *acpi_dev = to_acpi_device(dev);
11729b71a1cSThomas Renninger 	int len;
11829b71a1cSThomas Renninger 
11929b71a1cSThomas Renninger 	/* Device has no HID and no CID or string is >1024 */
12029b71a1cSThomas Renninger 	len = create_modalias(acpi_dev, buf, 1024);
12129b71a1cSThomas Renninger 	if (len <= 0)
12229b71a1cSThomas Renninger 		return 0;
12329b71a1cSThomas Renninger 	buf[len++] = '\n';
12429b71a1cSThomas Renninger 	return len;
12529b71a1cSThomas Renninger }
12629b71a1cSThomas Renninger static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
12729b71a1cSThomas Renninger 
128683058e3SRafael J. Wysocki static acpi_status acpi_bus_offline_companions(acpi_handle handle, u32 lvl,
129683058e3SRafael J. Wysocki 					       void *data, void **ret_p)
130683058e3SRafael J. Wysocki {
131683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
132683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
133303bfdb1SRafael J. Wysocki 	bool second_pass = (bool)data;
134683058e3SRafael J. Wysocki 	acpi_status status = AE_OK;
135683058e3SRafael J. Wysocki 
136683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
137683058e3SRafael J. Wysocki 		return AE_OK;
138683058e3SRafael J. Wysocki 
139683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
140683058e3SRafael J. Wysocki 
141683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node) {
142683058e3SRafael J. Wysocki 		int ret;
143683058e3SRafael J. Wysocki 
144303bfdb1SRafael J. Wysocki 		if (second_pass) {
145303bfdb1SRafael J. Wysocki 			/* Skip devices offlined by the first pass. */
146303bfdb1SRafael J. Wysocki 			if (pn->put_online)
147303bfdb1SRafael J. Wysocki 				continue;
148303bfdb1SRafael J. Wysocki 		} else {
149303bfdb1SRafael J. Wysocki 			pn->put_online = false;
150303bfdb1SRafael J. Wysocki 		}
151683058e3SRafael J. Wysocki 		ret = device_offline(pn->dev);
152683058e3SRafael J. Wysocki 		if (acpi_force_hot_remove)
153683058e3SRafael J. Wysocki 			continue;
154683058e3SRafael J. Wysocki 
155303bfdb1SRafael J. Wysocki 		if (ret >= 0) {
156303bfdb1SRafael J. Wysocki 			pn->put_online = !ret;
157303bfdb1SRafael J. Wysocki 		} else {
158303bfdb1SRafael J. Wysocki 			*ret_p = pn->dev;
159303bfdb1SRafael J. Wysocki 			if (second_pass) {
160683058e3SRafael J. Wysocki 				status = AE_ERROR;
161683058e3SRafael J. Wysocki 				break;
162683058e3SRafael J. Wysocki 			}
163303bfdb1SRafael J. Wysocki 		}
164683058e3SRafael J. Wysocki 	}
165683058e3SRafael J. Wysocki 
166683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
167683058e3SRafael J. Wysocki 
168683058e3SRafael J. Wysocki 	return status;
169683058e3SRafael J. Wysocki }
170683058e3SRafael J. Wysocki 
171683058e3SRafael J. Wysocki static acpi_status acpi_bus_online_companions(acpi_handle handle, u32 lvl,
172683058e3SRafael J. Wysocki 					      void *data, void **ret_p)
173683058e3SRafael J. Wysocki {
174683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
175683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
176683058e3SRafael J. Wysocki 
177683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
178683058e3SRafael J. Wysocki 		return AE_OK;
179683058e3SRafael J. Wysocki 
180683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
181683058e3SRafael J. Wysocki 
182683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node)
183683058e3SRafael J. Wysocki 		if (pn->put_online) {
184683058e3SRafael J. Wysocki 			device_online(pn->dev);
185683058e3SRafael J. Wysocki 			pn->put_online = false;
186683058e3SRafael J. Wysocki 		}
187683058e3SRafael J. Wysocki 
188683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
189683058e3SRafael J. Wysocki 
190683058e3SRafael J. Wysocki 	return AE_OK;
191683058e3SRafael J. Wysocki }
192683058e3SRafael J. Wysocki 
193a33ec399SRafael J. Wysocki static int acpi_scan_hot_remove(struct acpi_device *device)
1941da177e4SLinus Torvalds {
1955993c467SYinghai Lu 	acpi_handle handle = device->handle;
196a33ec399SRafael J. Wysocki 	acpi_handle not_used;
1971da177e4SLinus Torvalds 	struct acpi_object_list arg_list;
1981da177e4SLinus Torvalds 	union acpi_object arg;
199303bfdb1SRafael J. Wysocki 	struct device *errdev;
200a33ec399SRafael J. Wysocki 	acpi_status status;
201882fd12eSToshi Kani 	unsigned long long sta;
202f058cdf4SRafael J. Wysocki 
2033757b948SRafael J. Wysocki 	/* If there is no handle, the device node has been unregistered. */
204a33ec399SRafael J. Wysocki 	if (!handle) {
2053757b948SRafael J. Wysocki 		dev_dbg(&device->dev, "ACPI handle missing\n");
2063757b948SRafael J. Wysocki 		put_device(&device->dev);
207a33ec399SRafael J. Wysocki 		return -EINVAL;
2083757b948SRafael J. Wysocki 	}
2093757b948SRafael J. Wysocki 
210683058e3SRafael J. Wysocki 	lock_device_hotplug();
211683058e3SRafael J. Wysocki 
212303bfdb1SRafael J. Wysocki 	/*
213303bfdb1SRafael J. Wysocki 	 * Carry out two passes here and ignore errors in the first pass,
214303bfdb1SRafael J. Wysocki 	 * because if the devices in question are memory blocks and
215303bfdb1SRafael J. Wysocki 	 * CONFIG_MEMCG is set, one of the blocks may hold data structures
216303bfdb1SRafael J. Wysocki 	 * that the other blocks depend on, but it is not known in advance which
217303bfdb1SRafael J. Wysocki 	 * block holds them.
218303bfdb1SRafael J. Wysocki 	 *
219303bfdb1SRafael J. Wysocki 	 * If the first pass is successful, the second one isn't needed, though.
220303bfdb1SRafael J. Wysocki 	 */
221303bfdb1SRafael J. Wysocki 	errdev = NULL;
222683058e3SRafael J. Wysocki 	acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
223303bfdb1SRafael J. Wysocki 			    NULL, acpi_bus_offline_companions,
224303bfdb1SRafael J. Wysocki 			    (void *)false, (void **)&errdev);
225303bfdb1SRafael J. Wysocki 	acpi_bus_offline_companions(handle, 0, (void *)false, (void **)&errdev);
226303bfdb1SRafael J. Wysocki 	if (errdev) {
227303bfdb1SRafael J. Wysocki 		errdev = NULL;
228303bfdb1SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
229303bfdb1SRafael J. Wysocki 				    NULL, acpi_bus_offline_companions,
230303bfdb1SRafael J. Wysocki 				    (void *)true , (void **)&errdev);
231303bfdb1SRafael J. Wysocki 		if (!errdev || acpi_force_hot_remove)
232303bfdb1SRafael J. Wysocki 			acpi_bus_offline_companions(handle, 0, (void *)true,
233303bfdb1SRafael J. Wysocki 						    (void **)&errdev);
234303bfdb1SRafael J. Wysocki 
235303bfdb1SRafael J. Wysocki 		if (errdev && !acpi_force_hot_remove) {
236303bfdb1SRafael J. Wysocki 			dev_warn(errdev, "Offline failed.\n");
237303bfdb1SRafael J. Wysocki 			acpi_bus_online_companions(handle, 0, NULL, NULL);
238303bfdb1SRafael J. Wysocki 			acpi_walk_namespace(ACPI_TYPE_ANY, handle,
239303bfdb1SRafael J. Wysocki 					    ACPI_UINT32_MAX,
240303bfdb1SRafael J. Wysocki 					    acpi_bus_online_companions, NULL,
241303bfdb1SRafael J. Wysocki 					    NULL, NULL);
242683058e3SRafael J. Wysocki 
243683058e3SRafael J. Wysocki 			unlock_device_hotplug();
244683058e3SRafael J. Wysocki 
245683058e3SRafael J. Wysocki 			put_device(&device->dev);
246683058e3SRafael J. Wysocki 			return -EBUSY;
247683058e3SRafael J. Wysocki 		}
248303bfdb1SRafael J. Wysocki 	}
249683058e3SRafael J. Wysocki 
25026d46867SZhang Rui 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2510794469dSKay Sievers 		"Hot-removing device %s...\n", dev_name(&device->dev)));
25226d46867SZhang Rui 
253bfee26dbSRafael J. Wysocki 	acpi_bus_trim(device);
254683058e3SRafael J. Wysocki 
255683058e3SRafael J. Wysocki 	unlock_device_hotplug();
256683058e3SRafael J. Wysocki 
2573757b948SRafael J. Wysocki 	/* Device node has been unregistered. */
2583757b948SRafael J. Wysocki 	put_device(&device->dev);
259b3c450c3SToshi Kani 	device = NULL;
260b3c450c3SToshi Kani 
261a33ec399SRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &not_used))) {
2621da177e4SLinus Torvalds 		arg_list.count = 1;
2631da177e4SLinus Torvalds 		arg_list.pointer = &arg;
2641da177e4SLinus Torvalds 		arg.type = ACPI_TYPE_INTEGER;
2651da177e4SLinus Torvalds 		arg.integer.value = 0;
2661da177e4SLinus Torvalds 		acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
2671da177e4SLinus Torvalds 	}
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	arg_list.count = 1;
2701da177e4SLinus Torvalds 	arg_list.pointer = &arg;
2711da177e4SLinus Torvalds 	arg.type = ACPI_TYPE_INTEGER;
2721da177e4SLinus Torvalds 	arg.integer.value = 1;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	/*
2751da177e4SLinus Torvalds 	 * TBD: _EJD support.
2761da177e4SLinus Torvalds 	 */
2771da177e4SLinus Torvalds 	status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
278c4753e57SToshi Kani 	if (ACPI_FAILURE(status)) {
279a33ec399SRafael J. Wysocki 		if (status == AE_NOT_FOUND) {
280a33ec399SRafael J. Wysocki 			return -ENODEV;
281a33ec399SRafael J. Wysocki 		} else {
282882fd12eSToshi Kani 			acpi_handle_warn(handle, "Eject failed (0x%x)\n",
283882fd12eSToshi Kani 								status);
284a33ec399SRafael J. Wysocki 			return -EIO;
285a33ec399SRafael J. Wysocki 		}
286a33ec399SRafael J. Wysocki 	}
287f058cdf4SRafael J. Wysocki 
288882fd12eSToshi Kani 	/*
289882fd12eSToshi Kani 	 * Verify if eject was indeed successful.  If not, log an error
290882fd12eSToshi Kani 	 * message.  No need to call _OST since _EJ0 call was made OK.
291882fd12eSToshi Kani 	 */
292882fd12eSToshi Kani 	status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
293882fd12eSToshi Kani 	if (ACPI_FAILURE(status)) {
294882fd12eSToshi Kani 		acpi_handle_warn(handle,
295882fd12eSToshi Kani 			"Status check after eject failed (0x%x)\n", status);
296882fd12eSToshi Kani 	} else if (sta & ACPI_STA_DEVICE_ENABLED) {
297882fd12eSToshi Kani 		acpi_handle_warn(handle,
298882fd12eSToshi Kani 			"Eject incomplete - status 0x%llx\n", sta);
299882fd12eSToshi Kani 	}
300882fd12eSToshi Kani 
301a33ec399SRafael J. Wysocki 	return 0;
302a33ec399SRafael J. Wysocki }
303f058cdf4SRafael J. Wysocki 
304a33ec399SRafael J. Wysocki static void acpi_bus_device_eject(void *context)
305a33ec399SRafael J. Wysocki {
306a33ec399SRafael J. Wysocki 	acpi_handle handle = context;
307a33ec399SRafael J. Wysocki 	struct acpi_device *device = NULL;
308a33ec399SRafael J. Wysocki 	struct acpi_scan_handler *handler;
309a33ec399SRafael J. Wysocki 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
310a33ec399SRafael J. Wysocki 
311a33ec399SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
312a33ec399SRafael J. Wysocki 
313a33ec399SRafael J. Wysocki 	acpi_bus_get_device(handle, &device);
314a33ec399SRafael J. Wysocki 	if (!device)
315a33ec399SRafael J. Wysocki 		goto err_out;
316a33ec399SRafael J. Wysocki 
317a33ec399SRafael J. Wysocki 	handler = device->handler;
318a33ec399SRafael J. Wysocki 	if (!handler || !handler->hotplug.enabled) {
319a33ec399SRafael J. Wysocki 		ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
320a33ec399SRafael J. Wysocki 		goto err_out;
321a33ec399SRafael J. Wysocki 	}
322a33ec399SRafael J. Wysocki 	acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
323a33ec399SRafael J. Wysocki 				  ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
324a33ec399SRafael J. Wysocki 	if (handler->hotplug.mode == AHM_CONTAINER) {
325a33ec399SRafael J. Wysocki 		device->flags.eject_pending = true;
326a33ec399SRafael J. Wysocki 		kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
327a33ec399SRafael J. Wysocki 	} else {
328a33ec399SRafael J. Wysocki 		int error;
329a33ec399SRafael J. Wysocki 
330a33ec399SRafael J. Wysocki 		get_device(&device->dev);
331a33ec399SRafael J. Wysocki 		error = acpi_scan_hot_remove(device);
332a33ec399SRafael J. Wysocki 		if (error)
333a33ec399SRafael J. Wysocki 			goto err_out;
334c4753e57SToshi Kani 	}
3351da177e4SLinus Torvalds 
3363757b948SRafael J. Wysocki  out:
337f058cdf4SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
338c8d72a5eSZhang Rui 	return;
339a33ec399SRafael J. Wysocki 
340a33ec399SRafael J. Wysocki  err_out:
341a33ec399SRafael J. Wysocki 	acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST, ost_code,
342a33ec399SRafael J. Wysocki 				  NULL);
343a33ec399SRafael J. Wysocki 	goto out;
344a33ec399SRafael J. Wysocki }
345a33ec399SRafael J. Wysocki 
346a33ec399SRafael J. Wysocki static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
347a33ec399SRafael J. Wysocki {
348a33ec399SRafael J. Wysocki 	struct acpi_device *device = NULL;
349a33ec399SRafael J. Wysocki 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
350a33ec399SRafael J. Wysocki 	int error;
351a33ec399SRafael J. Wysocki 
352a33ec399SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
353683058e3SRafael J. Wysocki 	lock_device_hotplug();
354a33ec399SRafael J. Wysocki 
355*8832f7e4SRafael J. Wysocki 	if (ost_source != ACPI_NOTIFY_BUS_CHECK) {
356a33ec399SRafael J. Wysocki 		acpi_bus_get_device(handle, &device);
357a33ec399SRafael J. Wysocki 		if (device) {
358a33ec399SRafael J. Wysocki 			dev_warn(&device->dev, "Attempt to re-insert\n");
359a33ec399SRafael J. Wysocki 			goto out;
360a33ec399SRafael J. Wysocki 		}
361*8832f7e4SRafael J. Wysocki 	}
362a33ec399SRafael J. Wysocki 	acpi_evaluate_hotplug_ost(handle, ost_source,
363a33ec399SRafael J. Wysocki 				  ACPI_OST_SC_INSERT_IN_PROGRESS, NULL);
364a33ec399SRafael J. Wysocki 	error = acpi_bus_scan(handle);
365a33ec399SRafael J. Wysocki 	if (error) {
366a33ec399SRafael J. Wysocki 		acpi_handle_warn(handle, "Namespace scan failure\n");
367a33ec399SRafael J. Wysocki 		goto out;
368a33ec399SRafael J. Wysocki 	}
369a33ec399SRafael J. Wysocki 	error = acpi_bus_get_device(handle, &device);
370a33ec399SRafael J. Wysocki 	if (error) {
371a33ec399SRafael J. Wysocki 		acpi_handle_warn(handle, "Missing device node object\n");
372a33ec399SRafael J. Wysocki 		goto out;
373a33ec399SRafael J. Wysocki 	}
374a33ec399SRafael J. Wysocki 	ost_code = ACPI_OST_SC_SUCCESS;
375a33ec399SRafael J. Wysocki 	if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
376a33ec399SRafael J. Wysocki 		kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
377a33ec399SRafael J. Wysocki 
378a33ec399SRafael J. Wysocki  out:
379683058e3SRafael J. Wysocki 	unlock_device_hotplug();
380a33ec399SRafael J. Wysocki 	acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
381a33ec399SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
382a33ec399SRafael J. Wysocki }
383a33ec399SRafael J. Wysocki 
384a33ec399SRafael J. Wysocki static void acpi_scan_bus_check(void *context)
385a33ec399SRafael J. Wysocki {
386a33ec399SRafael J. Wysocki 	acpi_scan_bus_device_check((acpi_handle)context,
387a33ec399SRafael J. Wysocki 				   ACPI_NOTIFY_BUS_CHECK);
388a33ec399SRafael J. Wysocki }
389a33ec399SRafael J. Wysocki 
390a33ec399SRafael J. Wysocki static void acpi_scan_device_check(void *context)
391a33ec399SRafael J. Wysocki {
392a33ec399SRafael J. Wysocki 	acpi_scan_bus_device_check((acpi_handle)context,
393a33ec399SRafael J. Wysocki 				   ACPI_NOTIFY_DEVICE_CHECK);
394a33ec399SRafael J. Wysocki }
395a33ec399SRafael J. Wysocki 
3962cbb14fbSToshi Kani static void acpi_hotplug_unsupported(acpi_handle handle, u32 type)
3972cbb14fbSToshi Kani {
3982cbb14fbSToshi Kani 	u32 ost_status;
3992cbb14fbSToshi Kani 
4002cbb14fbSToshi Kani 	switch (type) {
4012cbb14fbSToshi Kani 	case ACPI_NOTIFY_BUS_CHECK:
4022cbb14fbSToshi Kani 		acpi_handle_debug(handle,
4032cbb14fbSToshi Kani 			"ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
4042cbb14fbSToshi Kani 		ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
4052cbb14fbSToshi Kani 		break;
4062cbb14fbSToshi Kani 	case ACPI_NOTIFY_DEVICE_CHECK:
4072cbb14fbSToshi Kani 		acpi_handle_debug(handle,
4082cbb14fbSToshi Kani 			"ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
4092cbb14fbSToshi Kani 		ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
4102cbb14fbSToshi Kani 		break;
4112cbb14fbSToshi Kani 	case ACPI_NOTIFY_EJECT_REQUEST:
4122cbb14fbSToshi Kani 		acpi_handle_debug(handle,
4132cbb14fbSToshi Kani 			"ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
4142cbb14fbSToshi Kani 		ost_status = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
4152cbb14fbSToshi Kani 		break;
4162cbb14fbSToshi Kani 	default:
4172cbb14fbSToshi Kani 		/* non-hotplug event; possibly handled by other handler */
4182cbb14fbSToshi Kani 		return;
4192cbb14fbSToshi Kani 	}
4202cbb14fbSToshi Kani 
4212cbb14fbSToshi Kani 	acpi_evaluate_hotplug_ost(handle, type, ost_status, NULL);
4222cbb14fbSToshi Kani }
4232cbb14fbSToshi Kani 
4242cbb14fbSToshi Kani static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
425a33ec399SRafael J. Wysocki {
426a33ec399SRafael J. Wysocki 	acpi_osd_exec_callback callback;
4272cbb14fbSToshi Kani 	struct acpi_scan_handler *handler = data;
428a33ec399SRafael J. Wysocki 	acpi_status status;
429a33ec399SRafael J. Wysocki 
4302cbb14fbSToshi Kani 	if (!handler->hotplug.enabled)
4312cbb14fbSToshi Kani 		return acpi_hotplug_unsupported(handle, type);
4322cbb14fbSToshi Kani 
433a33ec399SRafael J. Wysocki 	switch (type) {
434a33ec399SRafael J. Wysocki 	case ACPI_NOTIFY_BUS_CHECK:
435a33ec399SRafael J. Wysocki 		acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
436a33ec399SRafael J. Wysocki 		callback = acpi_scan_bus_check;
437a33ec399SRafael J. Wysocki 		break;
438a33ec399SRafael J. Wysocki 	case ACPI_NOTIFY_DEVICE_CHECK:
439a33ec399SRafael J. Wysocki 		acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
440a33ec399SRafael J. Wysocki 		callback = acpi_scan_device_check;
441a33ec399SRafael J. Wysocki 		break;
442a33ec399SRafael J. Wysocki 	case ACPI_NOTIFY_EJECT_REQUEST:
443a33ec399SRafael J. Wysocki 		acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
444a33ec399SRafael J. Wysocki 		callback = acpi_bus_device_eject;
445a33ec399SRafael J. Wysocki 		break;
446a33ec399SRafael J. Wysocki 	default:
447a33ec399SRafael J. Wysocki 		/* non-hotplug event; possibly handled by other handler */
448a33ec399SRafael J. Wysocki 		return;
449a33ec399SRafael J. Wysocki 	}
450a33ec399SRafael J. Wysocki 	status = acpi_os_hotplug_execute(callback, handle);
451a33ec399SRafael J. Wysocki 	if (ACPI_FAILURE(status))
452a33ec399SRafael J. Wysocki 		acpi_evaluate_hotplug_ost(handle, type,
453a33ec399SRafael J. Wysocki 					  ACPI_OST_SC_NON_SPECIFIC_FAILURE,
454a33ec399SRafael J. Wysocki 					  NULL);
455a33ec399SRafael J. Wysocki }
456a33ec399SRafael J. Wysocki 
457a33ec399SRafael J. Wysocki /**
458a33ec399SRafael J. Wysocki  * acpi_bus_hot_remove_device: hot-remove a device and its children
459a33ec399SRafael J. Wysocki  * @context: struct acpi_eject_event pointer (freed in this func)
460a33ec399SRafael J. Wysocki  *
461a33ec399SRafael J. Wysocki  * Hot-remove a device and its children. This function frees up the
462a33ec399SRafael J. Wysocki  * memory space passed by arg context, so that the caller may call
463a33ec399SRafael J. Wysocki  * this function asynchronously through acpi_os_hotplug_execute().
464a33ec399SRafael J. Wysocki  */
465a33ec399SRafael J. Wysocki void acpi_bus_hot_remove_device(void *context)
466a33ec399SRafael J. Wysocki {
467a33ec399SRafael J. Wysocki 	struct acpi_eject_event *ej_event = context;
468a33ec399SRafael J. Wysocki 	struct acpi_device *device = ej_event->device;
469a33ec399SRafael J. Wysocki 	acpi_handle handle = device->handle;
470a33ec399SRafael J. Wysocki 	int error;
471a33ec399SRafael J. Wysocki 
472a33ec399SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
473a33ec399SRafael J. Wysocki 
474a33ec399SRafael J. Wysocki 	error = acpi_scan_hot_remove(device);
475a33ec399SRafael J. Wysocki 	if (error && handle)
476a33ec399SRafael J. Wysocki 		acpi_evaluate_hotplug_ost(handle, ej_event->event,
477a33ec399SRafael J. Wysocki 					  ACPI_OST_SC_NON_SPECIFIC_FAILURE,
478a33ec399SRafael J. Wysocki 					  NULL);
479a33ec399SRafael J. Wysocki 
480a33ec399SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
481a33ec399SRafael J. Wysocki 	kfree(context);
4821da177e4SLinus Torvalds }
48361622accSToshi Kani EXPORT_SYMBOL(acpi_bus_hot_remove_device);
4841da177e4SLinus Torvalds 
485836aedb1SRafael J. Wysocki static ssize_t real_power_state_show(struct device *dev,
486836aedb1SRafael J. Wysocki 				     struct device_attribute *attr, char *buf)
487836aedb1SRafael J. Wysocki {
488836aedb1SRafael J. Wysocki 	struct acpi_device *adev = to_acpi_device(dev);
489836aedb1SRafael J. Wysocki 	int state;
490836aedb1SRafael J. Wysocki 	int ret;
491836aedb1SRafael J. Wysocki 
492836aedb1SRafael J. Wysocki 	ret = acpi_device_get_power(adev, &state);
493836aedb1SRafael J. Wysocki 	if (ret)
494836aedb1SRafael J. Wysocki 		return ret;
495836aedb1SRafael J. Wysocki 
496836aedb1SRafael J. Wysocki 	return sprintf(buf, "%s\n", acpi_power_state_string(state));
497836aedb1SRafael J. Wysocki }
498836aedb1SRafael J. Wysocki 
499836aedb1SRafael J. Wysocki static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
500836aedb1SRafael J. Wysocki 
501836aedb1SRafael J. Wysocki static ssize_t power_state_show(struct device *dev,
502836aedb1SRafael J. Wysocki 				struct device_attribute *attr, char *buf)
503836aedb1SRafael J. Wysocki {
504836aedb1SRafael J. Wysocki 	struct acpi_device *adev = to_acpi_device(dev);
505836aedb1SRafael J. Wysocki 
506836aedb1SRafael J. Wysocki 	return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
507836aedb1SRafael J. Wysocki }
508836aedb1SRafael J. Wysocki 
509836aedb1SRafael J. Wysocki static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
510836aedb1SRafael J. Wysocki 
5111da177e4SLinus Torvalds static ssize_t
512f883d9dbSPatrick Mochel acpi_eject_store(struct device *d, struct device_attribute *attr,
513f883d9dbSPatrick Mochel 		const char *buf, size_t count)
5141da177e4SLinus Torvalds {
515f883d9dbSPatrick Mochel 	struct acpi_device *acpi_device = to_acpi_device(d);
516c4753e57SToshi Kani 	struct acpi_eject_event *ej_event;
517a33ec399SRafael J. Wysocki 	acpi_object_type not_used;
518a33ec399SRafael J. Wysocki 	acpi_status status;
519a33ec399SRafael J. Wysocki 	u32 ost_source;
520a33ec399SRafael J. Wysocki 	int ret;
5211da177e4SLinus Torvalds 
522a33ec399SRafael J. Wysocki 	if (!count || buf[0] != '1')
5231da177e4SLinus Torvalds 		return -EINVAL;
5241da177e4SLinus Torvalds 
525a33ec399SRafael J. Wysocki 	if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
526a33ec399SRafael J. Wysocki 	    && !acpi_device->driver)
527a33ec399SRafael J. Wysocki 		return -ENODEV;
528a33ec399SRafael J. Wysocki 
529a33ec399SRafael J. Wysocki 	status = acpi_get_type(acpi_device->handle, &not_used);
530a33ec399SRafael J. Wysocki 	if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
531a33ec399SRafael J. Wysocki 		return -ENODEV;
532a33ec399SRafael J. Wysocki 
533a33ec399SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
534a33ec399SRafael J. Wysocki 
535a33ec399SRafael J. Wysocki 	if (acpi_device->flags.eject_pending) {
536a33ec399SRafael J. Wysocki 		/* ACPI eject notification event. */
537a33ec399SRafael J. Wysocki 		ost_source = ACPI_NOTIFY_EJECT_REQUEST;
538a33ec399SRafael J. Wysocki 		acpi_device->flags.eject_pending = 0;
539a33ec399SRafael J. Wysocki 	} else {
540a33ec399SRafael J. Wysocki 		/* Eject initiated by user space. */
541a33ec399SRafael J. Wysocki 		ost_source = ACPI_OST_EC_OSPM_EJECT;
542a33ec399SRafael J. Wysocki 	}
543c4753e57SToshi Kani 	ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
544c4753e57SToshi Kani 	if (!ej_event) {
545c4753e57SToshi Kani 		ret = -ENOMEM;
546a33ec399SRafael J. Wysocki 		goto err_out;
547c4753e57SToshi Kani 	}
548a33ec399SRafael J. Wysocki 	acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
549a33ec399SRafael J. Wysocki 				  ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
5505993c467SYinghai Lu 	ej_event->device = acpi_device;
551a33ec399SRafael J. Wysocki 	ej_event->event = ost_source;
552a33ec399SRafael J. Wysocki 	get_device(&acpi_device->dev);
5533757b948SRafael J. Wysocki 	status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device, ej_event);
5543757b948SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
5553757b948SRafael J. Wysocki 		put_device(&acpi_device->dev);
5563757b948SRafael J. Wysocki 		kfree(ej_event);
557a33ec399SRafael J. Wysocki 		ret = status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
558a33ec399SRafael J. Wysocki 		goto err_out;
5593757b948SRafael J. Wysocki 	}
560a33ec399SRafael J. Wysocki 	ret = count;
561a33ec399SRafael J. Wysocki 
562a33ec399SRafael J. Wysocki  out:
563a33ec399SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
5641da177e4SLinus Torvalds 	return ret;
565a33ec399SRafael J. Wysocki 
566a33ec399SRafael J. Wysocki  err_out:
567a33ec399SRafael J. Wysocki 	acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
568a33ec399SRafael J. Wysocki 				  ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
569a33ec399SRafael J. Wysocki 	goto out;
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds 
572f883d9dbSPatrick Mochel static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
573f883d9dbSPatrick Mochel 
574e49bd2ddSZhang Rui static ssize_t
575e49bd2ddSZhang Rui acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
576e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
577e49bd2ddSZhang Rui 
578ea8d82fdSBjorn Helgaas 	return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
579e49bd2ddSZhang Rui }
580e49bd2ddSZhang Rui static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
581e49bd2ddSZhang Rui 
582923d4a45SLv Zheng static ssize_t acpi_device_uid_show(struct device *dev,
583923d4a45SLv Zheng 				    struct device_attribute *attr, char *buf)
584923d4a45SLv Zheng {
585923d4a45SLv Zheng 	struct acpi_device *acpi_dev = to_acpi_device(dev);
586923d4a45SLv Zheng 
587923d4a45SLv Zheng 	return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
588923d4a45SLv Zheng }
589923d4a45SLv Zheng static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
590923d4a45SLv Zheng 
591923d4a45SLv Zheng static ssize_t acpi_device_adr_show(struct device *dev,
592923d4a45SLv Zheng 				    struct device_attribute *attr, char *buf)
593923d4a45SLv Zheng {
594923d4a45SLv Zheng 	struct acpi_device *acpi_dev = to_acpi_device(dev);
595923d4a45SLv Zheng 
596923d4a45SLv Zheng 	return sprintf(buf, "0x%08x\n",
597923d4a45SLv Zheng 		       (unsigned int)(acpi_dev->pnp.bus_address));
598923d4a45SLv Zheng }
599923d4a45SLv Zheng static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
600923d4a45SLv Zheng 
601e49bd2ddSZhang Rui static ssize_t
602e49bd2ddSZhang Rui acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
603e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
604e49bd2ddSZhang Rui 	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
605e49bd2ddSZhang Rui 	int result;
606e49bd2ddSZhang Rui 
607e49bd2ddSZhang Rui 	result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
608e49bd2ddSZhang Rui 	if (result)
609e49bd2ddSZhang Rui 		goto end;
610e49bd2ddSZhang Rui 
611e49bd2ddSZhang Rui 	result = sprintf(buf, "%s\n", (char*)path.pointer);
612e49bd2ddSZhang Rui 	kfree(path.pointer);
613e49bd2ddSZhang Rui end:
614e49bd2ddSZhang Rui 	return result;
615e49bd2ddSZhang Rui }
616e49bd2ddSZhang Rui static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
617e49bd2ddSZhang Rui 
618d1efe3c3SLance Ortiz /* sysfs file that shows description text from the ACPI _STR method */
619d1efe3c3SLance Ortiz static ssize_t description_show(struct device *dev,
620d1efe3c3SLance Ortiz 				struct device_attribute *attr,
621d1efe3c3SLance Ortiz 				char *buf) {
622d1efe3c3SLance Ortiz 	struct acpi_device *acpi_dev = to_acpi_device(dev);
623d1efe3c3SLance Ortiz 	int result;
624d1efe3c3SLance Ortiz 
625d1efe3c3SLance Ortiz 	if (acpi_dev->pnp.str_obj == NULL)
626d1efe3c3SLance Ortiz 		return 0;
627d1efe3c3SLance Ortiz 
628d1efe3c3SLance Ortiz 	/*
629d1efe3c3SLance Ortiz 	 * The _STR object contains a Unicode identifier for a device.
630d1efe3c3SLance Ortiz 	 * We need to convert to utf-8 so it can be displayed.
631d1efe3c3SLance Ortiz 	 */
632d1efe3c3SLance Ortiz 	result = utf16s_to_utf8s(
633d1efe3c3SLance Ortiz 		(wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
634d1efe3c3SLance Ortiz 		acpi_dev->pnp.str_obj->buffer.length,
635d1efe3c3SLance Ortiz 		UTF16_LITTLE_ENDIAN, buf,
636d1efe3c3SLance Ortiz 		PAGE_SIZE);
637d1efe3c3SLance Ortiz 
638d1efe3c3SLance Ortiz 	buf[result++] = '\n';
639d1efe3c3SLance Ortiz 
640d1efe3c3SLance Ortiz 	return result;
641d1efe3c3SLance Ortiz }
642d1efe3c3SLance Ortiz static DEVICE_ATTR(description, 0444, description_show, NULL);
643d1efe3c3SLance Ortiz 
644bb74ac23SYasuaki Ishimatsu static ssize_t
645bb74ac23SYasuaki Ishimatsu acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
646bb74ac23SYasuaki Ishimatsu 		     char *buf) {
647bb74ac23SYasuaki Ishimatsu 	struct acpi_device *acpi_dev = to_acpi_device(dev);
648bb74ac23SYasuaki Ishimatsu 
649bb74ac23SYasuaki Ishimatsu 	return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
650bb74ac23SYasuaki Ishimatsu }
651bb74ac23SYasuaki Ishimatsu static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
652bb74ac23SYasuaki Ishimatsu 
653e49bd2ddSZhang Rui static int acpi_device_setup_files(struct acpi_device *dev)
654f883d9dbSPatrick Mochel {
655d1efe3c3SLance Ortiz 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
656f883d9dbSPatrick Mochel 	acpi_status status;
657f883d9dbSPatrick Mochel 	acpi_handle temp;
658bb74ac23SYasuaki Ishimatsu 	unsigned long long sun;
659e49bd2ddSZhang Rui 	int result = 0;
660e49bd2ddSZhang Rui 
661e49bd2ddSZhang Rui 	/*
662e49bd2ddSZhang Rui 	 * Devices gotten from FADT don't have a "path" attribute
663e49bd2ddSZhang Rui 	 */
664e49bd2ddSZhang Rui 	if (dev->handle) {
665e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_path);
666e49bd2ddSZhang Rui 		if (result)
667e49bd2ddSZhang Rui 			goto end;
668e49bd2ddSZhang Rui 	}
669e49bd2ddSZhang Rui 
6702b2ae7c7SThomas Renninger 	if (!list_empty(&dev->pnp.ids)) {
671e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_hid);
672e49bd2ddSZhang Rui 		if (result)
673e49bd2ddSZhang Rui 			goto end;
674f883d9dbSPatrick Mochel 
67529b71a1cSThomas Renninger 		result = device_create_file(&dev->dev, &dev_attr_modalias);
67629b71a1cSThomas Renninger 		if (result)
67729b71a1cSThomas Renninger 			goto end;
6782b2ae7c7SThomas Renninger 	}
67929b71a1cSThomas Renninger 
680f883d9dbSPatrick Mochel 	/*
681d1efe3c3SLance Ortiz 	 * If device has _STR, 'description' file is created
682d1efe3c3SLance Ortiz 	 */
683d1efe3c3SLance Ortiz 	status = acpi_get_handle(dev->handle, "_STR", &temp);
684d1efe3c3SLance Ortiz 	if (ACPI_SUCCESS(status)) {
685d1efe3c3SLance Ortiz 		status = acpi_evaluate_object(dev->handle, "_STR",
686d1efe3c3SLance Ortiz 					NULL, &buffer);
687d1efe3c3SLance Ortiz 		if (ACPI_FAILURE(status))
688d1efe3c3SLance Ortiz 			buffer.pointer = NULL;
689d1efe3c3SLance Ortiz 		dev->pnp.str_obj = buffer.pointer;
690d1efe3c3SLance Ortiz 		result = device_create_file(&dev->dev, &dev_attr_description);
691d1efe3c3SLance Ortiz 		if (result)
692d1efe3c3SLance Ortiz 			goto end;
693d1efe3c3SLance Ortiz 	}
694d1efe3c3SLance Ortiz 
695d4e1a692SToshi Kani 	if (dev->pnp.type.bus_address)
696923d4a45SLv Zheng 		result = device_create_file(&dev->dev, &dev_attr_adr);
697923d4a45SLv Zheng 	if (dev->pnp.unique_id)
698923d4a45SLv Zheng 		result = device_create_file(&dev->dev, &dev_attr_uid);
699923d4a45SLv Zheng 
700bb74ac23SYasuaki Ishimatsu 	status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
701bb74ac23SYasuaki Ishimatsu 	if (ACPI_SUCCESS(status)) {
702bb74ac23SYasuaki Ishimatsu 		dev->pnp.sun = (unsigned long)sun;
703bb74ac23SYasuaki Ishimatsu 		result = device_create_file(&dev->dev, &dev_attr_sun);
704bb74ac23SYasuaki Ishimatsu 		if (result)
705bb74ac23SYasuaki Ishimatsu 			goto end;
706bb74ac23SYasuaki Ishimatsu 	} else {
707bb74ac23SYasuaki Ishimatsu 		dev->pnp.sun = (unsigned long)-1;
708bb74ac23SYasuaki Ishimatsu 	}
709bb74ac23SYasuaki Ishimatsu 
710d1efe3c3SLance Ortiz         /*
711f883d9dbSPatrick Mochel          * If device has _EJ0, 'eject' file is created that is used to trigger
712f883d9dbSPatrick Mochel          * hot-removal function from userland.
713f883d9dbSPatrick Mochel          */
714f883d9dbSPatrick Mochel 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
715836aedb1SRafael J. Wysocki 	if (ACPI_SUCCESS(status)) {
716e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_eject);
717836aedb1SRafael J. Wysocki 		if (result)
718836aedb1SRafael J. Wysocki 			return result;
719836aedb1SRafael J. Wysocki 	}
720836aedb1SRafael J. Wysocki 
721836aedb1SRafael J. Wysocki 	if (dev->flags.power_manageable) {
722836aedb1SRafael J. Wysocki 		result = device_create_file(&dev->dev, &dev_attr_power_state);
723836aedb1SRafael J. Wysocki 		if (result)
724836aedb1SRafael J. Wysocki 			return result;
725836aedb1SRafael J. Wysocki 
726836aedb1SRafael J. Wysocki 		if (dev->power.flags.power_resources)
727836aedb1SRafael J. Wysocki 			result = device_create_file(&dev->dev,
728836aedb1SRafael J. Wysocki 						    &dev_attr_real_power_state);
729836aedb1SRafael J. Wysocki 	}
730836aedb1SRafael J. Wysocki 
731e49bd2ddSZhang Rui end:
732e49bd2ddSZhang Rui 	return result;
733f883d9dbSPatrick Mochel }
734f883d9dbSPatrick Mochel 
735f883d9dbSPatrick Mochel static void acpi_device_remove_files(struct acpi_device *dev)
736f883d9dbSPatrick Mochel {
737f883d9dbSPatrick Mochel 	acpi_status status;
738f883d9dbSPatrick Mochel 	acpi_handle temp;
739f883d9dbSPatrick Mochel 
740836aedb1SRafael J. Wysocki 	if (dev->flags.power_manageable) {
741836aedb1SRafael J. Wysocki 		device_remove_file(&dev->dev, &dev_attr_power_state);
742836aedb1SRafael J. Wysocki 		if (dev->power.flags.power_resources)
743836aedb1SRafael J. Wysocki 			device_remove_file(&dev->dev,
744836aedb1SRafael J. Wysocki 					   &dev_attr_real_power_state);
745836aedb1SRafael J. Wysocki 	}
746836aedb1SRafael J. Wysocki 
747f883d9dbSPatrick Mochel 	/*
748d1efe3c3SLance Ortiz 	 * If device has _STR, remove 'description' file
749d1efe3c3SLance Ortiz 	 */
750d1efe3c3SLance Ortiz 	status = acpi_get_handle(dev->handle, "_STR", &temp);
751d1efe3c3SLance Ortiz 	if (ACPI_SUCCESS(status)) {
752d1efe3c3SLance Ortiz 		kfree(dev->pnp.str_obj);
753d1efe3c3SLance Ortiz 		device_remove_file(&dev->dev, &dev_attr_description);
754d1efe3c3SLance Ortiz 	}
755d1efe3c3SLance Ortiz 	/*
756d1efe3c3SLance Ortiz 	 * If device has _EJ0, remove 'eject' file.
757f883d9dbSPatrick Mochel 	 */
758f883d9dbSPatrick Mochel 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
759f883d9dbSPatrick Mochel 	if (ACPI_SUCCESS(status))
760f883d9dbSPatrick Mochel 		device_remove_file(&dev->dev, &dev_attr_eject);
761e49bd2ddSZhang Rui 
762bb74ac23SYasuaki Ishimatsu 	status = acpi_get_handle(dev->handle, "_SUN", &temp);
763bb74ac23SYasuaki Ishimatsu 	if (ACPI_SUCCESS(status))
764bb74ac23SYasuaki Ishimatsu 		device_remove_file(&dev->dev, &dev_attr_sun);
765bb74ac23SYasuaki Ishimatsu 
766923d4a45SLv Zheng 	if (dev->pnp.unique_id)
767923d4a45SLv Zheng 		device_remove_file(&dev->dev, &dev_attr_uid);
768d4e1a692SToshi Kani 	if (dev->pnp.type.bus_address)
769923d4a45SLv Zheng 		device_remove_file(&dev->dev, &dev_attr_adr);
77029b71a1cSThomas Renninger 	device_remove_file(&dev->dev, &dev_attr_modalias);
771e49bd2ddSZhang Rui 	device_remove_file(&dev->dev, &dev_attr_hid);
772e49bd2ddSZhang Rui 	if (dev->handle)
773e49bd2ddSZhang Rui 		device_remove_file(&dev->dev, &dev_attr_path);
774f883d9dbSPatrick Mochel }
7751da177e4SLinus Torvalds /* --------------------------------------------------------------------------
7769e89dde2SZhang Rui 			ACPI Bus operations
7771da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
77829b71a1cSThomas Renninger 
779cf761af9SMika Westerberg static const struct acpi_device_id *__acpi_match_device(
780cf761af9SMika Westerberg 	struct acpi_device *device, const struct acpi_device_id *ids)
78129b71a1cSThomas Renninger {
78229b71a1cSThomas Renninger 	const struct acpi_device_id *id;
7837f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hwid;
78429b71a1cSThomas Renninger 
78539a0ad87SZhao Yakui 	/*
78639a0ad87SZhao Yakui 	 * If the device is not present, it is unnecessary to load device
78739a0ad87SZhao Yakui 	 * driver for it.
78839a0ad87SZhao Yakui 	 */
78939a0ad87SZhao Yakui 	if (!device->status.present)
790cf761af9SMika Westerberg 		return NULL;
79139a0ad87SZhao Yakui 
7927f47fa6cSBjorn Helgaas 	for (id = ids; id->id[0]; id++)
7937f47fa6cSBjorn Helgaas 		list_for_each_entry(hwid, &device->pnp.ids, list)
7947f47fa6cSBjorn Helgaas 			if (!strcmp((char *) id->id, hwid->id))
795cf761af9SMika Westerberg 				return id;
79629b71a1cSThomas Renninger 
797cf761af9SMika Westerberg 	return NULL;
798cf761af9SMika Westerberg }
799cf761af9SMika Westerberg 
800cf761af9SMika Westerberg /**
801cf761af9SMika Westerberg  * acpi_match_device - Match a struct device against a given list of ACPI IDs
802cf761af9SMika Westerberg  * @ids: Array of struct acpi_device_id object to match against.
803cf761af9SMika Westerberg  * @dev: The device structure to match.
804cf761af9SMika Westerberg  *
805cf761af9SMika Westerberg  * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
806cf761af9SMika Westerberg  * object for that handle and use that object to match against a given list of
807cf761af9SMika Westerberg  * device IDs.
808cf761af9SMika Westerberg  *
809cf761af9SMika Westerberg  * Return a pointer to the first matching ID on success or %NULL on failure.
810cf761af9SMika Westerberg  */
811cf761af9SMika Westerberg const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
812cf761af9SMika Westerberg 					       const struct device *dev)
813cf761af9SMika Westerberg {
814cf761af9SMika Westerberg 	struct acpi_device *adev;
8150613e1f7SRafael J. Wysocki 	acpi_handle handle = ACPI_HANDLE(dev);
816cf761af9SMika Westerberg 
8170613e1f7SRafael J. Wysocki 	if (!ids || !handle || acpi_bus_get_device(handle, &adev))
818cf761af9SMika Westerberg 		return NULL;
819cf761af9SMika Westerberg 
820cf761af9SMika Westerberg 	return __acpi_match_device(adev, ids);
821cf761af9SMika Westerberg }
822cf761af9SMika Westerberg EXPORT_SYMBOL_GPL(acpi_match_device);
823cf761af9SMika Westerberg 
824cf761af9SMika Westerberg int acpi_match_device_ids(struct acpi_device *device,
825cf761af9SMika Westerberg 			  const struct acpi_device_id *ids)
826cf761af9SMika Westerberg {
827cf761af9SMika Westerberg 	return __acpi_match_device(device, ids) ? 0 : -ENOENT;
82829b71a1cSThomas Renninger }
82929b71a1cSThomas Renninger EXPORT_SYMBOL(acpi_match_device_ids);
83029b71a1cSThomas Renninger 
8310b224527SRafael J. Wysocki static void acpi_free_power_resources_lists(struct acpi_device *device)
8320b224527SRafael J. Wysocki {
8330b224527SRafael J. Wysocki 	int i;
8340b224527SRafael J. Wysocki 
835993cbe59SRafael J. Wysocki 	if (device->wakeup.flags.valid)
836993cbe59SRafael J. Wysocki 		acpi_power_resources_list_free(&device->wakeup.resources);
837993cbe59SRafael J. Wysocki 
8380b224527SRafael J. Wysocki 	if (!device->flags.power_manageable)
8390b224527SRafael J. Wysocki 		return;
8400b224527SRafael J. Wysocki 
8410b224527SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
8420b224527SRafael J. Wysocki 		struct acpi_device_power_state *ps = &device->power.states[i];
8430b224527SRafael J. Wysocki 		acpi_power_resources_list_free(&ps->resources);
8440b224527SRafael J. Wysocki 	}
8457f47fa6cSBjorn Helgaas }
8467f47fa6cSBjorn Helgaas 
8471890a97aSPatrick Mochel static void acpi_device_release(struct device *dev)
8481890a97aSPatrick Mochel {
8491890a97aSPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
8501890a97aSPatrick Mochel 
851c0af4175SToshi Kani 	acpi_free_pnp_ids(&acpi_dev->pnp);
8520b224527SRafael J. Wysocki 	acpi_free_power_resources_lists(acpi_dev);
8531890a97aSPatrick Mochel 	kfree(acpi_dev);
8541890a97aSPatrick Mochel }
8551890a97aSPatrick Mochel 
8565d9464a4SPatrick Mochel static int acpi_bus_match(struct device *dev, struct device_driver *drv)
8571da177e4SLinus Torvalds {
8585d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
8595d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(drv);
8605d9464a4SPatrick Mochel 
861209d3b17SRafael J. Wysocki 	return acpi_dev->flags.match_driver
862805d410fSRafael J. Wysocki 		&& !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
8635d9464a4SPatrick Mochel }
8645d9464a4SPatrick Mochel 
8657eff2e7aSKay Sievers static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
8665d9464a4SPatrick Mochel {
8675d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
8687eff2e7aSKay Sievers 	int len;
8695d9464a4SPatrick Mochel 
8702b2ae7c7SThomas Renninger 	if (list_empty(&acpi_dev->pnp.ids))
8712b2ae7c7SThomas Renninger 		return 0;
8722b2ae7c7SThomas Renninger 
8737eff2e7aSKay Sievers 	if (add_uevent_var(env, "MODALIAS="))
8747eff2e7aSKay Sievers 		return -ENOMEM;
8757eff2e7aSKay Sievers 	len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
8767eff2e7aSKay Sievers 			      sizeof(env->buf) - env->buflen);
8777eff2e7aSKay Sievers 	if (len >= (sizeof(env->buf) - env->buflen))
8787eff2e7aSKay Sievers 		return -ENOMEM;
8797eff2e7aSKay Sievers 	env->buflen += len;
8801da177e4SLinus Torvalds 	return 0;
8811da177e4SLinus Torvalds }
8821da177e4SLinus Torvalds 
88346ec8598SBjorn Helgaas static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
88446ec8598SBjorn Helgaas {
88546ec8598SBjorn Helgaas 	struct acpi_device *device = data;
88646ec8598SBjorn Helgaas 
88746ec8598SBjorn Helgaas 	device->driver->ops.notify(device, event);
88846ec8598SBjorn Helgaas }
88946ec8598SBjorn Helgaas 
89046ec8598SBjorn Helgaas static acpi_status acpi_device_notify_fixed(void *data)
89146ec8598SBjorn Helgaas {
89246ec8598SBjorn Helgaas 	struct acpi_device *device = data;
89346ec8598SBjorn Helgaas 
89453de5356SBjorn Helgaas 	/* Fixed hardware devices have no handles */
89553de5356SBjorn Helgaas 	acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
89646ec8598SBjorn Helgaas 	return AE_OK;
89746ec8598SBjorn Helgaas }
89846ec8598SBjorn Helgaas 
89946ec8598SBjorn Helgaas static int acpi_device_install_notify_handler(struct acpi_device *device)
90046ec8598SBjorn Helgaas {
90146ec8598SBjorn Helgaas 	acpi_status status;
90246ec8598SBjorn Helgaas 
903ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
90446ec8598SBjorn Helgaas 		status =
90546ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
90646ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
90746ec8598SBjorn Helgaas 						     device);
908ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
90946ec8598SBjorn Helgaas 		status =
91046ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
91146ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
91246ec8598SBjorn Helgaas 						     device);
91346ec8598SBjorn Helgaas 	else
91446ec8598SBjorn Helgaas 		status = acpi_install_notify_handler(device->handle,
91546ec8598SBjorn Helgaas 						     ACPI_DEVICE_NOTIFY,
91646ec8598SBjorn Helgaas 						     acpi_device_notify,
91746ec8598SBjorn Helgaas 						     device);
91846ec8598SBjorn Helgaas 
91946ec8598SBjorn Helgaas 	if (ACPI_FAILURE(status))
92046ec8598SBjorn Helgaas 		return -EINVAL;
92146ec8598SBjorn Helgaas 	return 0;
92246ec8598SBjorn Helgaas }
92346ec8598SBjorn Helgaas 
92446ec8598SBjorn Helgaas static void acpi_device_remove_notify_handler(struct acpi_device *device)
92546ec8598SBjorn Helgaas {
926ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
92746ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
92846ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
929ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
93046ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
93146ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
93246ec8598SBjorn Helgaas 	else
93346ec8598SBjorn Helgaas 		acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
93446ec8598SBjorn Helgaas 					   acpi_device_notify);
93546ec8598SBjorn Helgaas }
93646ec8598SBjorn Helgaas 
9375d9464a4SPatrick Mochel static int acpi_device_probe(struct device *dev)
9385d9464a4SPatrick Mochel {
9395d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
9405d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
9415d9464a4SPatrick Mochel 	int ret;
9425d9464a4SPatrick Mochel 
94324071f47SRafael J. Wysocki 	if (acpi_dev->handler)
94424071f47SRafael J. Wysocki 		return -EINVAL;
94524071f47SRafael J. Wysocki 
946d9e455f5SRafael J. Wysocki 	if (!acpi_drv->ops.add)
947d9e455f5SRafael J. Wysocki 		return -ENOSYS;
948d9e455f5SRafael J. Wysocki 
949d9e455f5SRafael J. Wysocki 	ret = acpi_drv->ops.add(acpi_dev);
950d9e455f5SRafael J. Wysocki 	if (ret)
951d9e455f5SRafael J. Wysocki 		return ret;
952d9e455f5SRafael J. Wysocki 
953d9e455f5SRafael J. Wysocki 	acpi_dev->driver = acpi_drv;
954d9e455f5SRafael J. Wysocki 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
955d9e455f5SRafael J. Wysocki 			  "Driver [%s] successfully bound to device [%s]\n",
956d9e455f5SRafael J. Wysocki 			  acpi_drv->name, acpi_dev->pnp.bus_id));
957d9e455f5SRafael J. Wysocki 
95846ec8598SBjorn Helgaas 	if (acpi_drv->ops.notify) {
95946ec8598SBjorn Helgaas 		ret = acpi_device_install_notify_handler(acpi_dev);
96046ec8598SBjorn Helgaas 		if (ret) {
96146ec8598SBjorn Helgaas 			if (acpi_drv->ops.remove)
96251fac838SRafael J. Wysocki 				acpi_drv->ops.remove(acpi_dev);
963d9e455f5SRafael J. Wysocki 
9645f27ee8eSToshi Kani 			acpi_dev->driver = NULL;
9655f27ee8eSToshi Kani 			acpi_dev->driver_data = NULL;
96646ec8598SBjorn Helgaas 			return ret;
96746ec8598SBjorn Helgaas 		}
96846ec8598SBjorn Helgaas 	}
96946ec8598SBjorn Helgaas 
970d9e455f5SRafael J. Wysocki 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
9715d9464a4SPatrick Mochel 			  acpi_drv->name, acpi_dev->pnp.bus_id));
9725d9464a4SPatrick Mochel 	get_device(dev);
973d9e455f5SRafael J. Wysocki 	return 0;
9745d9464a4SPatrick Mochel }
9755d9464a4SPatrick Mochel 
9765d9464a4SPatrick Mochel static int acpi_device_remove(struct device * dev)
9775d9464a4SPatrick Mochel {
9785d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
9795d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
9805d9464a4SPatrick Mochel 
9815d9464a4SPatrick Mochel 	if (acpi_drv) {
98246ec8598SBjorn Helgaas 		if (acpi_drv->ops.notify)
98346ec8598SBjorn Helgaas 			acpi_device_remove_notify_handler(acpi_dev);
9845d9464a4SPatrick Mochel 		if (acpi_drv->ops.remove)
98551fac838SRafael J. Wysocki 			acpi_drv->ops.remove(acpi_dev);
9865d9464a4SPatrick Mochel 	}
9875d9464a4SPatrick Mochel 	acpi_dev->driver = NULL;
988db89b4f0SPavel Machek 	acpi_dev->driver_data = NULL;
9895d9464a4SPatrick Mochel 
9905d9464a4SPatrick Mochel 	put_device(dev);
9915d9464a4SPatrick Mochel 	return 0;
9925d9464a4SPatrick Mochel }
9935d9464a4SPatrick Mochel 
99455955aadSDavid Brownell struct bus_type acpi_bus_type = {
9959e89dde2SZhang Rui 	.name		= "acpi",
9965d9464a4SPatrick Mochel 	.match		= acpi_bus_match,
9975d9464a4SPatrick Mochel 	.probe		= acpi_device_probe,
9985d9464a4SPatrick Mochel 	.remove		= acpi_device_remove,
9995d9464a4SPatrick Mochel 	.uevent		= acpi_device_uevent,
10001da177e4SLinus Torvalds };
10011da177e4SLinus Torvalds 
1002cf860be6SRafael J. Wysocki int acpi_device_add(struct acpi_device *device,
100382c7d5efSRafael J. Wysocki 		    void (*release)(struct device *))
10041da177e4SLinus Torvalds {
1005e49bd2ddSZhang Rui 	int result;
1006e49bd2ddSZhang Rui 	struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1007e49bd2ddSZhang Rui 	int found = 0;
100866b7ed40SBjorn Helgaas 
1009d43e167dSRafael J. Wysocki 	if (device->handle) {
1010d43e167dSRafael J. Wysocki 		acpi_status status;
1011d43e167dSRafael J. Wysocki 
1012d43e167dSRafael J. Wysocki 		status = acpi_attach_data(device->handle, acpi_bus_data_handler,
1013d43e167dSRafael J. Wysocki 					  device);
1014d43e167dSRafael J. Wysocki 		if (ACPI_FAILURE(status)) {
1015d43e167dSRafael J. Wysocki 			acpi_handle_err(device->handle,
1016d43e167dSRafael J. Wysocki 					"Unable to attach device data\n");
1017d43e167dSRafael J. Wysocki 			return -ENODEV;
1018d43e167dSRafael J. Wysocki 		}
1019d43e167dSRafael J. Wysocki 	}
1020d43e167dSRafael J. Wysocki 
10211da177e4SLinus Torvalds 	/*
10221da177e4SLinus Torvalds 	 * Linkage
10231da177e4SLinus Torvalds 	 * -------
10241da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
10251da177e4SLinus Torvalds 	 */
10261da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->children);
10271da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->node);
10281da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
10291033f904SLan Tianyu 	INIT_LIST_HEAD(&device->physical_node_list);
10301033f904SLan Tianyu 	mutex_init(&device->physical_node_lock);
1031bc9b6407SRafael J. Wysocki 	INIT_LIST_HEAD(&device->power_dependent);
10321da177e4SLinus Torvalds 
1033e49bd2ddSZhang Rui 	new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1034e49bd2ddSZhang Rui 	if (!new_bus_id) {
1035d43e167dSRafael J. Wysocki 		pr_err(PREFIX "Memory allocation error\n");
1036d43e167dSRafael J. Wysocki 		result = -ENOMEM;
1037d43e167dSRafael J. Wysocki 		goto err_detach;
1038e49bd2ddSZhang Rui 	}
1039e49bd2ddSZhang Rui 
10409090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
1041e49bd2ddSZhang Rui 	/*
1042e49bd2ddSZhang Rui 	 * Find suitable bus_id and instance number in acpi_bus_id_list
1043e49bd2ddSZhang Rui 	 * If failed, create one and link it into acpi_bus_id_list
1044e49bd2ddSZhang Rui 	 */
1045e49bd2ddSZhang Rui 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
10461131b938SBjorn Helgaas 		if (!strcmp(acpi_device_bus_id->bus_id,
10471131b938SBjorn Helgaas 			    acpi_device_hid(device))) {
1048e49bd2ddSZhang Rui 			acpi_device_bus_id->instance_no++;
1049e49bd2ddSZhang Rui 			found = 1;
1050e49bd2ddSZhang Rui 			kfree(new_bus_id);
1051e49bd2ddSZhang Rui 			break;
1052e49bd2ddSZhang Rui 		}
1053e49bd2ddSZhang Rui 	}
1054e49bd2ddSZhang Rui 	if (!found) {
1055e49bd2ddSZhang Rui 		acpi_device_bus_id = new_bus_id;
10561131b938SBjorn Helgaas 		strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
1057e49bd2ddSZhang Rui 		acpi_device_bus_id->instance_no = 0;
1058e49bd2ddSZhang Rui 		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1059e49bd2ddSZhang Rui 	}
10600794469dSKay Sievers 	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
1061e49bd2ddSZhang Rui 
106233b57150SLen Brown 	if (device->parent)
10631da177e4SLinus Torvalds 		list_add_tail(&device->node, &device->parent->children);
106433b57150SLen Brown 
10651da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
10661da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
10679090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
10681da177e4SLinus Torvalds 
10691890a97aSPatrick Mochel 	if (device->parent)
107066b7ed40SBjorn Helgaas 		device->dev.parent = &device->parent->dev;
10711890a97aSPatrick Mochel 	device->dev.bus = &acpi_bus_type;
107282c7d5efSRafael J. Wysocki 	device->dev.release = release;
1073cf860be6SRafael J. Wysocki 	result = device_add(&device->dev);
1074e49bd2ddSZhang Rui 	if (result) {
10758b12b922SAlex Chiang 		dev_err(&device->dev, "Error registering device\n");
1076d43e167dSRafael J. Wysocki 		goto err;
1077e49bd2ddSZhang Rui 	}
1078f883d9dbSPatrick Mochel 
1079e49bd2ddSZhang Rui 	result = acpi_device_setup_files(device);
1080e49bd2ddSZhang Rui 	if (result)
10810794469dSKay Sievers 		printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
10820794469dSKay Sievers 		       dev_name(&device->dev));
1083e49bd2ddSZhang Rui 
1084e49bd2ddSZhang Rui 	return 0;
1085d43e167dSRafael J. Wysocki 
1086d43e167dSRafael J. Wysocki  err:
10879090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
108833b57150SLen Brown 	if (device->parent)
1089e49bd2ddSZhang Rui 		list_del(&device->node);
1090e49bd2ddSZhang Rui 	list_del(&device->wakeup_list);
10919090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
1092d43e167dSRafael J. Wysocki 
1093d43e167dSRafael J. Wysocki  err_detach:
1094d43e167dSRafael J. Wysocki 	acpi_detach_data(device->handle, acpi_bus_data_handler);
1095e49bd2ddSZhang Rui 	return result;
10961da177e4SLinus Torvalds }
10971da177e4SLinus Torvalds 
1098b17b537aSRafael J. Wysocki static void acpi_device_unregister(struct acpi_device *device)
10991da177e4SLinus Torvalds {
11009090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
110133b57150SLen Brown 	if (device->parent)
11021da177e4SLinus Torvalds 		list_del(&device->node);
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	list_del(&device->wakeup_list);
11059090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 	acpi_detach_data(device->handle, acpi_bus_data_handler);
11081890a97aSPatrick Mochel 
1109bc9b6407SRafael J. Wysocki 	acpi_power_add_remove_device(device, false);
1110f883d9dbSPatrick Mochel 	acpi_device_remove_files(device);
1111b1c0f99bSRafael J. Wysocki 	if (device->remove)
1112b1c0f99bSRafael J. Wysocki 		device->remove(device);
1113b1c0f99bSRafael J. Wysocki 
1114cf860be6SRafael J. Wysocki 	device_del(&device->dev);
1115bc9b6407SRafael J. Wysocki 	/*
11160aa120a0SRafael J. Wysocki 	 * Transition the device to D3cold to drop the reference counts of all
11170aa120a0SRafael J. Wysocki 	 * power resources the device depends on and turn off the ones that have
11180aa120a0SRafael J. Wysocki 	 * no more references.
1119bc9b6407SRafael J. Wysocki 	 */
11200aa120a0SRafael J. Wysocki 	acpi_device_set_power(device, ACPI_STATE_D3_COLD);
11213757b948SRafael J. Wysocki 	device->handle = NULL;
1122cf860be6SRafael J. Wysocki 	put_device(&device->dev);
11231da177e4SLinus Torvalds }
11241da177e4SLinus Torvalds 
11259e89dde2SZhang Rui /* --------------------------------------------------------------------------
11269e89dde2SZhang Rui                                  Driver Management
11279e89dde2SZhang Rui    -------------------------------------------------------------------------- */
11281da177e4SLinus Torvalds /**
1129d758a8faSRandy Dunlap  * acpi_bus_register_driver - register a driver with the ACPI bus
1130d758a8faSRandy Dunlap  * @driver: driver being registered
1131d758a8faSRandy Dunlap  *
11321da177e4SLinus Torvalds  * Registers a driver with the ACPI bus.  Searches the namespace for all
11339d9f749bSBjorn Helgaas  * devices that match the driver's criteria and binds.  Returns zero for
11349d9f749bSBjorn Helgaas  * success or a negative error status for failure.
11351da177e4SLinus Torvalds  */
11364be44fcdSLen Brown int acpi_bus_register_driver(struct acpi_driver *driver)
11371da177e4SLinus Torvalds {
11381890a97aSPatrick Mochel 	int ret;
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	if (acpi_disabled)
1141d550d98dSPatrick Mochel 		return -ENODEV;
11421890a97aSPatrick Mochel 	driver->drv.name = driver->name;
11431890a97aSPatrick Mochel 	driver->drv.bus = &acpi_bus_type;
11441890a97aSPatrick Mochel 	driver->drv.owner = driver->owner;
11451da177e4SLinus Torvalds 
11461890a97aSPatrick Mochel 	ret = driver_register(&driver->drv);
11471890a97aSPatrick Mochel 	return ret;
11481da177e4SLinus Torvalds }
11491da177e4SLinus Torvalds 
11504be44fcdSLen Brown EXPORT_SYMBOL(acpi_bus_register_driver);
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds /**
1153d758a8faSRandy Dunlap  * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
1154d758a8faSRandy Dunlap  * @driver: driver to unregister
1155d758a8faSRandy Dunlap  *
11561da177e4SLinus Torvalds  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
11571da177e4SLinus Torvalds  * devices that match the driver's criteria and unbinds.
11581da177e4SLinus Torvalds  */
115906ea8e08SBjorn Helgaas void acpi_bus_unregister_driver(struct acpi_driver *driver)
11601da177e4SLinus Torvalds {
11611890a97aSPatrick Mochel 	driver_unregister(&driver->drv);
11621da177e4SLinus Torvalds }
11634be44fcdSLen Brown 
11641da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_unregister_driver);
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds /* --------------------------------------------------------------------------
11671da177e4SLinus Torvalds                                  Device Enumeration
11681da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
11695c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
11705c478f49SBjorn Helgaas {
1171456de893SRafael J. Wysocki 	struct acpi_device *device = NULL;
11725c478f49SBjorn Helgaas 	acpi_status status;
11735c478f49SBjorn Helgaas 
11745c478f49SBjorn Helgaas 	/*
11755c478f49SBjorn Helgaas 	 * Fixed hardware devices do not appear in the namespace and do not
11765c478f49SBjorn Helgaas 	 * have handles, but we fabricate acpi_devices for them, so we have
11775c478f49SBjorn Helgaas 	 * to deal with them specially.
11785c478f49SBjorn Helgaas 	 */
1179456de893SRafael J. Wysocki 	if (!handle)
11805c478f49SBjorn Helgaas 		return acpi_root;
11815c478f49SBjorn Helgaas 
11825c478f49SBjorn Helgaas 	do {
11835c478f49SBjorn Helgaas 		status = acpi_get_parent(handle, &handle);
11845c478f49SBjorn Helgaas 		if (ACPI_FAILURE(status))
1185456de893SRafael J. Wysocki 			return status == AE_NULL_ENTRY ? NULL : acpi_root;
1186456de893SRafael J. Wysocki 	} while (acpi_bus_get_device(handle, &device));
11875c478f49SBjorn Helgaas 	return device;
11885c478f49SBjorn Helgaas }
11895c478f49SBjorn Helgaas 
1190c8f7a62cSLen Brown acpi_status
1191c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1192c8f7a62cSLen Brown {
1193c8f7a62cSLen Brown 	acpi_status status;
1194c8f7a62cSLen Brown 	acpi_handle tmp;
1195c8f7a62cSLen Brown 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1196c8f7a62cSLen Brown 	union acpi_object *obj;
1197c8f7a62cSLen Brown 
1198c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_EJD", &tmp);
1199c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
1200c8f7a62cSLen Brown 		return status;
1201c8f7a62cSLen Brown 
1202c8f7a62cSLen Brown 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1203c8f7a62cSLen Brown 	if (ACPI_SUCCESS(status)) {
1204c8f7a62cSLen Brown 		obj = buffer.pointer;
12053b5fee59SHolger Macht 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
12063b5fee59SHolger Macht 					 ejd);
1207c8f7a62cSLen Brown 		kfree(buffer.pointer);
1208c8f7a62cSLen Brown 	}
1209c8f7a62cSLen Brown 	return status;
1210c8f7a62cSLen Brown }
1211c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1212c8f7a62cSLen Brown 
12138e4319c4SBob Moore void acpi_bus_data_handler(acpi_handle handle, void *context)
12141da177e4SLinus Torvalds {
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 	/* TBD */
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	return;
12191da177e4SLinus Torvalds }
12201da177e4SLinus Torvalds 
1221e88c9c60SRafael J. Wysocki static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1222b581a7f9SRafael J. Wysocki 					struct acpi_device_wakeup *wakeup)
12231da177e4SLinus Torvalds {
1224b581a7f9SRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1225b581a7f9SRafael J. Wysocki 	union acpi_object *package = NULL;
12261da177e4SLinus Torvalds 	union acpi_object *element = NULL;
1227b581a7f9SRafael J. Wysocki 	acpi_status status;
1228e88c9c60SRafael J. Wysocki 	int err = -ENODATA;
12291da177e4SLinus Torvalds 
1230b581a7f9SRafael J. Wysocki 	if (!wakeup)
1231e88c9c60SRafael J. Wysocki 		return -EINVAL;
12321da177e4SLinus Torvalds 
1233993cbe59SRafael J. Wysocki 	INIT_LIST_HEAD(&wakeup->resources);
12341da177e4SLinus Torvalds 
1235b581a7f9SRafael J. Wysocki 	/* _PRW */
1236b581a7f9SRafael J. Wysocki 	status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1237b581a7f9SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
1238b581a7f9SRafael J. Wysocki 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
1239e88c9c60SRafael J. Wysocki 		return err;
1240b581a7f9SRafael J. Wysocki 	}
1241b581a7f9SRafael J. Wysocki 
1242b581a7f9SRafael J. Wysocki 	package = (union acpi_object *)buffer.pointer;
1243b581a7f9SRafael J. Wysocki 
1244e88c9c60SRafael J. Wysocki 	if (!package || package->package.count < 2)
1245b581a7f9SRafael J. Wysocki 		goto out;
1246b581a7f9SRafael J. Wysocki 
12471da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
1248e88c9c60SRafael J. Wysocki 	if (!element)
1249b581a7f9SRafael J. Wysocki 		goto out;
1250e88c9c60SRafael J. Wysocki 
12511da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
12521da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
12531da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
12541da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
1255e88c9c60SRafael J. Wysocki 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
1256b581a7f9SRafael J. Wysocki 			goto out;
1257e88c9c60SRafael J. Wysocki 
1258b581a7f9SRafael J. Wysocki 		wakeup->gpe_device =
12591da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
1260b581a7f9SRafael J. Wysocki 		wakeup->gpe_number =
12611da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
12621da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
1263b581a7f9SRafael J. Wysocki 		wakeup->gpe_device = NULL;
1264b581a7f9SRafael J. Wysocki 		wakeup->gpe_number = element->integer.value;
1265b581a7f9SRafael J. Wysocki 	} else {
1266b581a7f9SRafael J. Wysocki 		goto out;
1267b581a7f9SRafael J. Wysocki 	}
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
1270e88c9c60SRafael J. Wysocki 	if (element->type != ACPI_TYPE_INTEGER)
1271b581a7f9SRafael J. Wysocki 		goto out;
1272e88c9c60SRafael J. Wysocki 
1273b581a7f9SRafael J. Wysocki 	wakeup->sleep_state = element->integer.value;
12741da177e4SLinus Torvalds 
1275e88c9c60SRafael J. Wysocki 	err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1276e88c9c60SRafael J. Wysocki 	if (err)
1277b581a7f9SRafael J. Wysocki 		goto out;
12781da177e4SLinus Torvalds 
12790596a52bSRafael J. Wysocki 	if (!list_empty(&wakeup->resources)) {
12800596a52bSRafael J. Wysocki 		int sleep_state;
12819874647bSRafael J. Wysocki 
1282b5d667ebSRafael J. Wysocki 		err = acpi_power_wakeup_list_init(&wakeup->resources,
1283b5d667ebSRafael J. Wysocki 						  &sleep_state);
1284b5d667ebSRafael J. Wysocki 		if (err) {
1285b5d667ebSRafael J. Wysocki 			acpi_handle_warn(handle, "Retrieving current states "
1286b5d667ebSRafael J. Wysocki 					 "of wakeup power resources failed\n");
1287b5d667ebSRafael J. Wysocki 			acpi_power_resources_list_free(&wakeup->resources);
1288b5d667ebSRafael J. Wysocki 			goto out;
1289b5d667ebSRafael J. Wysocki 		}
12900596a52bSRafael J. Wysocki 		if (sleep_state < wakeup->sleep_state) {
12910596a52bSRafael J. Wysocki 			acpi_handle_warn(handle, "Overriding _PRW sleep state "
12920596a52bSRafael J. Wysocki 					 "(S%d) by S%d from power resources\n",
12930596a52bSRafael J. Wysocki 					 (int)wakeup->sleep_state, sleep_state);
12940596a52bSRafael J. Wysocki 			wakeup->sleep_state = sleep_state;
12950596a52bSRafael J. Wysocki 		}
12960596a52bSRafael J. Wysocki 	}
1297bba63a29SLin Ming 	acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
1298b581a7f9SRafael J. Wysocki 
1299b581a7f9SRafael J. Wysocki  out:
1300b581a7f9SRafael J. Wysocki 	kfree(buffer.pointer);
1301e88c9c60SRafael J. Wysocki 	return err;
13021da177e4SLinus Torvalds }
13031da177e4SLinus Torvalds 
1304f517709dSRafael J. Wysocki static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
13051da177e4SLinus Torvalds {
130629b71a1cSThomas Renninger 	struct acpi_device_id button_device_ids[] = {
130729b71a1cSThomas Renninger 		{"PNP0C0C", 0},
1308b7e38304SZhang Rui 		{"PNP0C0D", 0},
130929b71a1cSThomas Renninger 		{"PNP0C0E", 0},
131029b71a1cSThomas Renninger 		{"", 0},
131129b71a1cSThomas Renninger 	};
1312f517709dSRafael J. Wysocki 	acpi_status status;
1313f517709dSRafael J. Wysocki 	acpi_event_status event_status;
1314f517709dSRafael J. Wysocki 
1315b67ea761SRafael J. Wysocki 	device->wakeup.flags.notifier_present = 0;
1316f517709dSRafael J. Wysocki 
1317f517709dSRafael J. Wysocki 	/* Power button, Lid switch always enable wakeup */
1318f517709dSRafael J. Wysocki 	if (!acpi_match_device_ids(device, button_device_ids)) {
1319f517709dSRafael J. Wysocki 		device->wakeup.flags.run_wake = 1;
1320b7e38304SZhang Rui 		if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1321b7e38304SZhang Rui 			/* Do not use Lid/sleep button for S5 wakeup */
1322b7e38304SZhang Rui 			if (device->wakeup.sleep_state == ACPI_STATE_S5)
1323b7e38304SZhang Rui 				device->wakeup.sleep_state = ACPI_STATE_S4;
1324b7e38304SZhang Rui 		}
1325f2b56bc8SRafael J. Wysocki 		device_set_wakeup_capable(&device->dev, true);
1326f517709dSRafael J. Wysocki 		return;
1327f517709dSRafael J. Wysocki 	}
1328f517709dSRafael J. Wysocki 
1329e8e18c95SRafael J. Wysocki 	status = acpi_get_gpe_status(device->wakeup.gpe_device,
1330e8e18c95SRafael J. Wysocki 					device->wakeup.gpe_number,
13310f849d2cSLin Ming 						&event_status);
1332f517709dSRafael J. Wysocki 	if (status == AE_OK)
1333f517709dSRafael J. Wysocki 		device->wakeup.flags.run_wake =
1334f517709dSRafael J. Wysocki 				!!(event_status & ACPI_EVENT_FLAG_HANDLE);
1335f517709dSRafael J. Wysocki }
1336f517709dSRafael J. Wysocki 
1337d57d09a4SRafael J. Wysocki static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1338f517709dSRafael J. Wysocki {
1339d57d09a4SRafael J. Wysocki 	acpi_handle temp;
1340f517709dSRafael J. Wysocki 	acpi_status status = 0;
1341e88c9c60SRafael J. Wysocki 	int err;
134229b71a1cSThomas Renninger 
1343d57d09a4SRafael J. Wysocki 	/* Presence of _PRW indicates wake capable */
1344d57d09a4SRafael J. Wysocki 	status = acpi_get_handle(device->handle, "_PRW", &temp);
1345d57d09a4SRafael J. Wysocki 	if (ACPI_FAILURE(status))
1346d57d09a4SRafael J. Wysocki 		return;
1347d57d09a4SRafael J. Wysocki 
1348e88c9c60SRafael J. Wysocki 	err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1349b581a7f9SRafael J. Wysocki 							   &device->wakeup);
1350e88c9c60SRafael J. Wysocki 	if (err) {
1351e88c9c60SRafael J. Wysocki 		dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1352d57d09a4SRafael J. Wysocki 		return;
13531da177e4SLinus Torvalds 	}
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	device->wakeup.flags.valid = 1;
13569b83ccd2SRafael J. Wysocki 	device->wakeup.prepare_count = 0;
1357f517709dSRafael J. Wysocki 	acpi_bus_set_run_wake_flags(device);
1358729b2bdbSZhao Yakui 	/* Call _PSW/_DSW object to disable its ability to wake the sleeping
1359729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
1360729b2bdbSZhao Yakui 	 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1361729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
1362729b2bdbSZhao Yakui 	 * present will the _PSW object used.
1363729b2bdbSZhao Yakui 	 */
1364e88c9c60SRafael J. Wysocki 	err = acpi_device_sleep_wake(device, 0, 0, 0);
1365e88c9c60SRafael J. Wysocki 	if (err)
136677e76609SRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
136777e76609SRafael J. Wysocki 				"error in _DSW or _PSW evaluation\n"));
13681da177e4SLinus Torvalds }
13691da177e4SLinus Torvalds 
1370f33ce568SRafael J. Wysocki static void acpi_bus_init_power_state(struct acpi_device *device, int state)
13711da177e4SLinus Torvalds {
1372f33ce568SRafael J. Wysocki 	struct acpi_device_power_state *ps = &device->power.states[state];
1373ef85bdbeSRafael J. Wysocki 	char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1374ef85bdbeSRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1375f33ce568SRafael J. Wysocki 	acpi_handle handle;
1376f33ce568SRafael J. Wysocki 	acpi_status status;
13779e89dde2SZhang Rui 
1378f33ce568SRafael J. Wysocki 	INIT_LIST_HEAD(&ps->resources);
1379f33ce568SRafael J. Wysocki 
1380ef85bdbeSRafael J. Wysocki 	/* Evaluate "_PRx" to get referenced power resources */
1381ef85bdbeSRafael J. Wysocki 	status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1382ef85bdbeSRafael J. Wysocki 	if (ACPI_SUCCESS(status)) {
1383ef85bdbeSRafael J. Wysocki 		union acpi_object *package = buffer.pointer;
1384f33ce568SRafael J. Wysocki 
1385ef85bdbeSRafael J. Wysocki 		if (buffer.length && package
1386ef85bdbeSRafael J. Wysocki 		    && package->type == ACPI_TYPE_PACKAGE
1387ef85bdbeSRafael J. Wysocki 		    && package->package.count) {
1388e88c9c60SRafael J. Wysocki 			int err = acpi_extract_power_resources(package, 0,
1389ef85bdbeSRafael J. Wysocki 							       &ps->resources);
1390e88c9c60SRafael J. Wysocki 			if (!err)
1391f33ce568SRafael J. Wysocki 				device->power.flags.power_resources = 1;
1392f33ce568SRafael J. Wysocki 		}
1393ef85bdbeSRafael J. Wysocki 		ACPI_FREE(buffer.pointer);
1394f33ce568SRafael J. Wysocki 	}
1395f33ce568SRafael J. Wysocki 
1396f33ce568SRafael J. Wysocki 	/* Evaluate "_PSx" to see if we can do explicit sets */
1397ef85bdbeSRafael J. Wysocki 	pathname[2] = 'S';
1398ef85bdbeSRafael J. Wysocki 	status = acpi_get_handle(device->handle, pathname, &handle);
1399f33ce568SRafael J. Wysocki 	if (ACPI_SUCCESS(status))
1400f33ce568SRafael J. Wysocki 		ps->flags.explicit_set = 1;
1401f33ce568SRafael J. Wysocki 
1402f33ce568SRafael J. Wysocki 	/*
1403f33ce568SRafael J. Wysocki 	 * State is valid if there are means to put the device into it.
1404f33ce568SRafael J. Wysocki 	 * D3hot is only valid if _PR3 present.
1405f33ce568SRafael J. Wysocki 	 */
1406ef85bdbeSRafael J. Wysocki 	if (!list_empty(&ps->resources)
1407f33ce568SRafael J. Wysocki 	    || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1408f33ce568SRafael J. Wysocki 		ps->flags.valid = 1;
1409f33ce568SRafael J. Wysocki 		ps->flags.os_accessible = 1;
1410f33ce568SRafael J. Wysocki 	}
1411f33ce568SRafael J. Wysocki 
1412f33ce568SRafael J. Wysocki 	ps->power = -1;		/* Unknown - driver assigned */
1413f33ce568SRafael J. Wysocki 	ps->latency = -1;	/* Unknown - driver assigned */
1414f33ce568SRafael J. Wysocki }
1415f33ce568SRafael J. Wysocki 
1416d43e167dSRafael J. Wysocki static void acpi_bus_get_power_flags(struct acpi_device *device)
14171da177e4SLinus Torvalds {
14188bc5053bSRafael J. Wysocki 	acpi_status status;
14198bc5053bSRafael J. Wysocki 	acpi_handle handle;
14208bc5053bSRafael J. Wysocki 	u32 i;
14211da177e4SLinus Torvalds 
1422d43e167dSRafael J. Wysocki 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
1423d43e167dSRafael J. Wysocki 	status = acpi_get_handle(device->handle, "_PS0", &handle);
1424d43e167dSRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
1425d43e167dSRafael J. Wysocki 		status = acpi_get_handle(device->handle, "_PR0", &handle);
1426d43e167dSRafael J. Wysocki 		if (ACPI_FAILURE(status))
1427d43e167dSRafael J. Wysocki 			return;
1428d43e167dSRafael J. Wysocki 	}
1429d43e167dSRafael J. Wysocki 
1430d43e167dSRafael J. Wysocki 	device->flags.power_manageable = 1;
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 	/*
14339e89dde2SZhang Rui 	 * Power Management Flags
14341da177e4SLinus Torvalds 	 */
14359e89dde2SZhang Rui 	status = acpi_get_handle(device->handle, "_PSC", &handle);
14361da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
14379e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
14389e89dde2SZhang Rui 	status = acpi_get_handle(device->handle, "_IRC", &handle);
14399e89dde2SZhang Rui 	if (ACPI_SUCCESS(status))
14409e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds 	/*
14439e89dde2SZhang Rui 	 * Enumerate supported power management states
14441da177e4SLinus Torvalds 	 */
1445f33ce568SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1446f33ce568SRafael J. Wysocki 		acpi_bus_init_power_state(device, i);
14471da177e4SLinus Torvalds 
14480b224527SRafael J. Wysocki 	INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
14499e89dde2SZhang Rui 
14509e89dde2SZhang Rui 	/* Set defaults for D0 and D3 states (always valid) */
14519e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
14529e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
14539e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D3].flags.valid = 1;
14549e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D3].power = 0;
14559e89dde2SZhang Rui 
14565c7dd710SRafael J. Wysocki 	/* Set D3cold's explicit_set flag if _PS3 exists. */
14575c7dd710SRafael J. Wysocki 	if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
14585c7dd710SRafael J. Wysocki 		device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
14595c7dd710SRafael J. Wysocki 
14601399dfcdSAaron Lu 	/* Presence of _PS3 or _PRx means we can put the device into D3 cold */
14611399dfcdSAaron Lu 	if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
14621399dfcdSAaron Lu 			device->power.flags.power_resources)
14631399dfcdSAaron Lu 		device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
14641399dfcdSAaron Lu 
1465b3785492SRafael J. Wysocki 	if (acpi_bus_init_power(device)) {
1466b3785492SRafael J. Wysocki 		acpi_free_power_resources_lists(device);
1467b3785492SRafael J. Wysocki 		device->flags.power_manageable = 0;
1468b3785492SRafael J. Wysocki 	}
14699e89dde2SZhang Rui }
1470c8f7a62cSLen Brown 
1471d43e167dSRafael J. Wysocki static void acpi_bus_get_flags(struct acpi_device *device)
14721da177e4SLinus Torvalds {
14731da177e4SLinus Torvalds 	acpi_status status = AE_OK;
14741da177e4SLinus Torvalds 	acpi_handle temp = NULL;
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
14771da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_STA", &temp);
14781da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
14791da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
14821da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_RMV", &temp);
14831da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
14841da177e4SLinus Torvalds 		device->flags.removable = 1;
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
14871da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_EJD", &temp);
14881da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
14891da177e4SLinus Torvalds 		device->flags.ejectable = 1;
14901da177e4SLinus Torvalds 	else {
14911da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_EJ0", &temp);
14921da177e4SLinus Torvalds 		if (ACPI_SUCCESS(status))
14931da177e4SLinus Torvalds 			device->flags.ejectable = 1;
14941da177e4SLinus Torvalds 	}
14951da177e4SLinus Torvalds }
14961da177e4SLinus Torvalds 
1497c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device)
14981da177e4SLinus Torvalds {
14991da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
15001da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
15011da177e4SLinus Torvalds 	int i = 0;
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds 	/*
15041da177e4SLinus Torvalds 	 * Bus ID
15051da177e4SLinus Torvalds 	 * ------
15061da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
15071da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
15081da177e4SLinus Torvalds 	 */
1509859ac9a4SBjorn Helgaas 	if (ACPI_IS_ROOT_DEVICE(device)) {
15101da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
1511859ac9a4SBjorn Helgaas 		return;
1512859ac9a4SBjorn Helgaas 	}
1513859ac9a4SBjorn Helgaas 
1514859ac9a4SBjorn Helgaas 	switch (device->device_type) {
15151da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
15161da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
15171da177e4SLinus Torvalds 		break;
15181da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
15191da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
15201da177e4SLinus Torvalds 		break;
15211da177e4SLinus Torvalds 	default:
152266b7ed40SBjorn Helgaas 		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
15231da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
15241da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
15251da177e4SLinus Torvalds 			if (bus_id[i] == '_')
15261da177e4SLinus Torvalds 				bus_id[i] = '\0';
15271da177e4SLinus Torvalds 			else
15281da177e4SLinus Torvalds 				break;
15291da177e4SLinus Torvalds 		}
15301da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
15311da177e4SLinus Torvalds 		break;
15321da177e4SLinus Torvalds 	}
15331da177e4SLinus Torvalds }
15341da177e4SLinus Torvalds 
153554735266SZhang Rui /*
1536d4e1a692SToshi Kani  * acpi_bay_match - see if an acpi object is an ejectable driver bay
153754735266SZhang Rui  *
153854735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
153954735266SZhang Rui  * then we can safely call it an ejectable drive bay
154054735266SZhang Rui  */
1541d4e1a692SToshi Kani static int acpi_bay_match(acpi_handle handle)
1542d4e1a692SToshi Kani {
154354735266SZhang Rui 	acpi_status status;
154454735266SZhang Rui 	acpi_handle tmp;
154554735266SZhang Rui 	acpi_handle phandle;
154654735266SZhang Rui 
154754735266SZhang Rui 	status = acpi_get_handle(handle, "_EJ0", &tmp);
154854735266SZhang Rui 	if (ACPI_FAILURE(status))
154954735266SZhang Rui 		return -ENODEV;
155054735266SZhang Rui 
155154735266SZhang Rui 	if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
155254735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
155354735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
155454735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
155554735266SZhang Rui 		return 0;
155654735266SZhang Rui 
155754735266SZhang Rui 	if (acpi_get_parent(handle, &phandle))
155854735266SZhang Rui 		return -ENODEV;
155954735266SZhang Rui 
156054735266SZhang Rui         if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
156154735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
156254735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
156354735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
156454735266SZhang Rui                 return 0;
156554735266SZhang Rui 
156654735266SZhang Rui 	return -ENODEV;
156754735266SZhang Rui }
156854735266SZhang Rui 
15693620f2f2SFrank Seidel /*
1570d4e1a692SToshi Kani  * acpi_dock_match - see if an acpi object has a _DCK method
15713620f2f2SFrank Seidel  */
1572d4e1a692SToshi Kani static int acpi_dock_match(acpi_handle handle)
15733620f2f2SFrank Seidel {
15743620f2f2SFrank Seidel 	acpi_handle tmp;
1575d4e1a692SToshi Kani 	return acpi_get_handle(handle, "_DCK", &tmp);
15763620f2f2SFrank Seidel }
15773620f2f2SFrank Seidel 
1578620e112cSThomas Renninger const char *acpi_device_hid(struct acpi_device *device)
157915b8dd53SBob Moore {
15807f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hid;
158115b8dd53SBob Moore 
15822b2ae7c7SThomas Renninger 	if (list_empty(&device->pnp.ids))
15832b2ae7c7SThomas Renninger 		return dummy_hid;
15842b2ae7c7SThomas Renninger 
15857f47fa6cSBjorn Helgaas 	hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
15867f47fa6cSBjorn Helgaas 	return hid->id;
15877f47fa6cSBjorn Helgaas }
15887f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid);
158915b8dd53SBob Moore 
1590d4e1a692SToshi Kani static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
15917f47fa6cSBjorn Helgaas {
15927f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
159315b8dd53SBob Moore 
15947f47fa6cSBjorn Helgaas 	id = kmalloc(sizeof(*id), GFP_KERNEL);
15957f47fa6cSBjorn Helgaas 	if (!id)
15967f47fa6cSBjorn Helgaas 		return;
159715b8dd53SBob Moore 
1598581de59eSThomas Meyer 	id->id = kstrdup(dev_id, GFP_KERNEL);
15997f47fa6cSBjorn Helgaas 	if (!id->id) {
16007f47fa6cSBjorn Helgaas 		kfree(id);
16017f47fa6cSBjorn Helgaas 		return;
160215b8dd53SBob Moore 	}
160315b8dd53SBob Moore 
1604d4e1a692SToshi Kani 	list_add_tail(&id->list, &pnp->ids);
1605d4e1a692SToshi Kani 	pnp->type.hardware_id = 1;
160615b8dd53SBob Moore }
160715b8dd53SBob Moore 
1608222e82acSDarrick J. Wong /*
1609222e82acSDarrick J. Wong  * Old IBM workstations have a DSDT bug wherein the SMBus object
1610222e82acSDarrick J. Wong  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1611222e82acSDarrick J. Wong  * prefix.  Work around this.
1612222e82acSDarrick J. Wong  */
1613d4e1a692SToshi Kani static int acpi_ibm_smbus_match(acpi_handle handle)
1614222e82acSDarrick J. Wong {
1615222e82acSDarrick J. Wong 	acpi_handle h_dummy;
1616222e82acSDarrick J. Wong 	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1617222e82acSDarrick J. Wong 	int result;
1618222e82acSDarrick J. Wong 
1619222e82acSDarrick J. Wong 	if (!dmi_name_in_vendors("IBM"))
1620222e82acSDarrick J. Wong 		return -ENODEV;
1621222e82acSDarrick J. Wong 
1622222e82acSDarrick J. Wong 	/* Look for SMBS object */
1623d4e1a692SToshi Kani 	result = acpi_get_name(handle, ACPI_SINGLE_NAME, &path);
1624222e82acSDarrick J. Wong 	if (result)
1625222e82acSDarrick J. Wong 		return result;
1626222e82acSDarrick J. Wong 
1627222e82acSDarrick J. Wong 	if (strcmp("SMBS", path.pointer)) {
1628222e82acSDarrick J. Wong 		result = -ENODEV;
1629222e82acSDarrick J. Wong 		goto out;
1630222e82acSDarrick J. Wong 	}
1631222e82acSDarrick J. Wong 
1632222e82acSDarrick J. Wong 	/* Does it have the necessary (but misnamed) methods? */
1633222e82acSDarrick J. Wong 	result = -ENODEV;
1634d4e1a692SToshi Kani 	if (ACPI_SUCCESS(acpi_get_handle(handle, "SBI", &h_dummy)) &&
1635d4e1a692SToshi Kani 	    ACPI_SUCCESS(acpi_get_handle(handle, "SBR", &h_dummy)) &&
1636d4e1a692SToshi Kani 	    ACPI_SUCCESS(acpi_get_handle(handle, "SBW", &h_dummy)))
1637222e82acSDarrick J. Wong 		result = 0;
1638222e82acSDarrick J. Wong out:
1639222e82acSDarrick J. Wong 	kfree(path.pointer);
1640222e82acSDarrick J. Wong 	return result;
1641222e82acSDarrick J. Wong }
1642222e82acSDarrick J. Wong 
1643c0af4175SToshi Kani static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1644c0af4175SToshi Kani 				int device_type)
16451da177e4SLinus Torvalds {
16461da177e4SLinus Torvalds 	acpi_status status;
164757f3674fSBjorn Helgaas 	struct acpi_device_info *info;
164878e25fefSLv Zheng 	struct acpi_pnp_device_id_list *cid_list;
16497f47fa6cSBjorn Helgaas 	int i;
16501da177e4SLinus Torvalds 
1651c0af4175SToshi Kani 	switch (device_type) {
16521da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
1653c0af4175SToshi Kani 		if (handle == ACPI_ROOT_OBJECT) {
1654c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SYSTEM_HID);
1655859ac9a4SBjorn Helgaas 			break;
1656859ac9a4SBjorn Helgaas 		}
1657859ac9a4SBjorn Helgaas 
1658c0af4175SToshi Kani 		status = acpi_get_object_info(handle, &info);
16591da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
1660c0af4175SToshi Kani 			pr_err(PREFIX "%s: Error reading device info\n",
1661c0af4175SToshi Kani 					__func__);
16621da177e4SLinus Torvalds 			return;
16631da177e4SLinus Torvalds 		}
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_HID)
1666c0af4175SToshi Kani 			acpi_add_id(pnp, info->hardware_id.string);
166757f3674fSBjorn Helgaas 		if (info->valid & ACPI_VALID_CID) {
166815b8dd53SBob Moore 			cid_list = &info->compatible_id_list;
166957f3674fSBjorn Helgaas 			for (i = 0; i < cid_list->count; i++)
1670c0af4175SToshi Kani 				acpi_add_id(pnp, cid_list->ids[i].string);
167157f3674fSBjorn Helgaas 		}
16721da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
1673c0af4175SToshi Kani 			pnp->bus_address = info->address;
1674c0af4175SToshi Kani 			pnp->type.bus_address = 1;
16751da177e4SLinus Torvalds 		}
1676ccf78040SLv Zheng 		if (info->valid & ACPI_VALID_UID)
1677c0af4175SToshi Kani 			pnp->unique_id = kstrdup(info->unique_id.string,
1678ccf78040SLv Zheng 							GFP_KERNEL);
1679ae843332SZhang Rui 
1680a83893aeSBjorn Helgaas 		kfree(info);
1681a83893aeSBjorn Helgaas 
168257f3674fSBjorn Helgaas 		/*
168357f3674fSBjorn Helgaas 		 * Some devices don't reliably have _HIDs & _CIDs, so add
168457f3674fSBjorn Helgaas 		 * synthetic HIDs to make sure drivers can find them.
16853620f2f2SFrank Seidel 		 */
1686c0af4175SToshi Kani 		if (acpi_is_video_device(handle))
1687c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_VIDEO_HID);
1688c0af4175SToshi Kani 		else if (ACPI_SUCCESS(acpi_bay_match(handle)))
1689c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_BAY_HID);
1690c0af4175SToshi Kani 		else if (ACPI_SUCCESS(acpi_dock_match(handle)))
1691c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_DOCK_HID);
1692c0af4175SToshi Kani 		else if (!acpi_ibm_smbus_match(handle))
1693c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1694c0af4175SToshi Kani 		else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1695c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1696c0af4175SToshi Kani 			strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1697c0af4175SToshi Kani 			strcpy(pnp->device_class, ACPI_BUS_CLASS);
1698b7b30de5SBjorn Helgaas 		}
169954735266SZhang Rui 
17001da177e4SLinus Torvalds 		break;
17011da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
1702c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_POWER_HID);
17031da177e4SLinus Torvalds 		break;
17041da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
1705c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
17061da177e4SLinus Torvalds 		break;
17071da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
1708c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_THERMAL_HID);
17091da177e4SLinus Torvalds 		break;
17101da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
1711c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
17121da177e4SLinus Torvalds 		break;
17131da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
1714c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
17151da177e4SLinus Torvalds 		break;
17161da177e4SLinus Torvalds 	}
17171da177e4SLinus Torvalds }
17181da177e4SLinus Torvalds 
1719c0af4175SToshi Kani void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1720c0af4175SToshi Kani {
1721c0af4175SToshi Kani 	struct acpi_hardware_id *id, *tmp;
1722c0af4175SToshi Kani 
1723c0af4175SToshi Kani 	list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1724c0af4175SToshi Kani 		kfree(id->id);
1725c0af4175SToshi Kani 		kfree(id);
1726c0af4175SToshi Kani 	}
1727c0af4175SToshi Kani 	kfree(pnp->unique_id);
1728c0af4175SToshi Kani }
1729c0af4175SToshi Kani 
173082c7d5efSRafael J. Wysocki void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1731d43e167dSRafael J. Wysocki 			     int type, unsigned long long sta)
17321da177e4SLinus Torvalds {
1733d43e167dSRafael J. Wysocki 	INIT_LIST_HEAD(&device->pnp.ids);
1734d43e167dSRafael J. Wysocki 	device->device_type = type;
1735d43e167dSRafael J. Wysocki 	device->handle = handle;
1736d43e167dSRafael J. Wysocki 	device->parent = acpi_bus_get_parent(handle);
1737d43e167dSRafael J. Wysocki 	STRUCT_TO_INT(device->status) = sta;
1738d43e167dSRafael J. Wysocki 	acpi_device_get_busid(device);
1739c0af4175SToshi Kani 	acpi_set_pnp_ids(handle, &device->pnp, type);
1740d43e167dSRafael J. Wysocki 	acpi_bus_get_flags(device);
17412c0d4fe0SRafael J. Wysocki 	device->flags.match_driver = false;
1742cf860be6SRafael J. Wysocki 	device_initialize(&device->dev);
1743cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, true);
1744cf860be6SRafael J. Wysocki }
1745bc3b0772SBjorn Helgaas 
1746cf860be6SRafael J. Wysocki void acpi_device_add_finalize(struct acpi_device *device)
1747cf860be6SRafael J. Wysocki {
17482c0d4fe0SRafael J. Wysocki 	device->flags.match_driver = true;
1749cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, false);
1750cf860be6SRafael J. Wysocki 	kobject_uevent(&device->dev.kobj, KOBJ_ADD);
17511da177e4SLinus Torvalds }
17521da177e4SLinus Torvalds 
17535c478f49SBjorn Helgaas static int acpi_add_single_object(struct acpi_device **child,
17545c478f49SBjorn Helgaas 				  acpi_handle handle, int type,
17552c0d4fe0SRafael J. Wysocki 				  unsigned long long sta)
17561da177e4SLinus Torvalds {
175777c24888SBjorn Helgaas 	int result;
175877c24888SBjorn Helgaas 	struct acpi_device *device;
175929aaefa6SBjorn Helgaas 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
17601da177e4SLinus Torvalds 
176136bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
17621da177e4SLinus Torvalds 	if (!device) {
17636468463aSLen Brown 		printk(KERN_ERR PREFIX "Memory allocation error\n");
1764d550d98dSPatrick Mochel 		return -ENOMEM;
17651da177e4SLinus Torvalds 	}
17661da177e4SLinus Torvalds 
1767d43e167dSRafael J. Wysocki 	acpi_init_device_object(device, handle, type, sta);
1768d43e167dSRafael J. Wysocki 	acpi_bus_get_power_flags(device);
1769d57d09a4SRafael J. Wysocki 	acpi_bus_get_wakeup_device_flags(device);
17701da177e4SLinus Torvalds 
1771cf860be6SRafael J. Wysocki 	result = acpi_device_add(device, acpi_device_release);
1772d43e167dSRafael J. Wysocki 	if (result) {
1773718fb0deSHugh Dickins 		acpi_device_release(&device->dev);
1774d550d98dSPatrick Mochel 		return result;
17751da177e4SLinus Torvalds 	}
17761da177e4SLinus Torvalds 
1777bc9b6407SRafael J. Wysocki 	acpi_power_add_remove_device(device, true);
1778cf860be6SRafael J. Wysocki 	acpi_device_add_finalize(device);
17791da177e4SLinus Torvalds 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1780d43e167dSRafael J. Wysocki 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1781d43e167dSRafael J. Wysocki 		dev_name(&device->dev), (char *) buffer.pointer,
1782d43e167dSRafael J. Wysocki 		device->parent ? dev_name(&device->parent->dev) : "(null)"));
17831da177e4SLinus Torvalds 	kfree(buffer.pointer);
17841da177e4SLinus Torvalds 	*child = device;
1785d43e167dSRafael J. Wysocki 	return 0;
1786bf325f95SRafael J. Wysocki }
1787bf325f95SRafael J. Wysocki 
1788778cbc1dSBjorn Helgaas static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1789778cbc1dSBjorn Helgaas 				    unsigned long long *sta)
1790778cbc1dSBjorn Helgaas {
1791778cbc1dSBjorn Helgaas 	acpi_status status;
1792778cbc1dSBjorn Helgaas 	acpi_object_type acpi_type;
1793778cbc1dSBjorn Helgaas 
1794778cbc1dSBjorn Helgaas 	status = acpi_get_type(handle, &acpi_type);
1795778cbc1dSBjorn Helgaas 	if (ACPI_FAILURE(status))
1796778cbc1dSBjorn Helgaas 		return -ENODEV;
1797778cbc1dSBjorn Helgaas 
1798778cbc1dSBjorn Helgaas 	switch (acpi_type) {
1799778cbc1dSBjorn Helgaas 	case ACPI_TYPE_ANY:		/* for ACPI_ROOT_OBJECT */
1800778cbc1dSBjorn Helgaas 	case ACPI_TYPE_DEVICE:
1801778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_DEVICE;
1802778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1803778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1804778cbc1dSBjorn Helgaas 			return -ENODEV;
1805778cbc1dSBjorn Helgaas 		break;
1806778cbc1dSBjorn Helgaas 	case ACPI_TYPE_PROCESSOR:
1807778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_PROCESSOR;
1808778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1809778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1810778cbc1dSBjorn Helgaas 			return -ENODEV;
1811778cbc1dSBjorn Helgaas 		break;
1812778cbc1dSBjorn Helgaas 	case ACPI_TYPE_THERMAL:
1813778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_THERMAL;
1814778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1815778cbc1dSBjorn Helgaas 		break;
1816778cbc1dSBjorn Helgaas 	case ACPI_TYPE_POWER:
1817778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_POWER;
1818778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1819778cbc1dSBjorn Helgaas 		break;
1820778cbc1dSBjorn Helgaas 	default:
1821778cbc1dSBjorn Helgaas 		return -ENODEV;
1822778cbc1dSBjorn Helgaas 	}
1823778cbc1dSBjorn Helgaas 
1824778cbc1dSBjorn Helgaas 	return 0;
1825778cbc1dSBjorn Helgaas }
1826778cbc1dSBjorn Helgaas 
18274b59cc1fSRafael J. Wysocki static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
18284b59cc1fSRafael J. Wysocki 				       char *idstr,
1829c5698074SRafael J. Wysocki 				       const struct acpi_device_id **matchid)
1830c5698074SRafael J. Wysocki {
1831c5698074SRafael J. Wysocki 	const struct acpi_device_id *devid;
1832c5698074SRafael J. Wysocki 
1833c5698074SRafael J. Wysocki 	for (devid = handler->ids; devid->id[0]; devid++)
1834c5698074SRafael J. Wysocki 		if (!strcmp((char *)devid->id, idstr)) {
1835c5698074SRafael J. Wysocki 			if (matchid)
1836c5698074SRafael J. Wysocki 				*matchid = devid;
1837c5698074SRafael J. Wysocki 
18384b59cc1fSRafael J. Wysocki 			return true;
18394b59cc1fSRafael J. Wysocki 		}
18404b59cc1fSRafael J. Wysocki 
18414b59cc1fSRafael J. Wysocki 	return false;
18424b59cc1fSRafael J. Wysocki }
18434b59cc1fSRafael J. Wysocki 
18446b772e8fSToshi Kani static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
18456b772e8fSToshi Kani 					const struct acpi_device_id **matchid)
18466b772e8fSToshi Kani {
18476b772e8fSToshi Kani 	struct acpi_scan_handler *handler;
18486b772e8fSToshi Kani 
18496b772e8fSToshi Kani 	list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
18506b772e8fSToshi Kani 		if (acpi_scan_handler_matching(handler, idstr, matchid))
18516b772e8fSToshi Kani 			return handler;
18526b772e8fSToshi Kani 
18536b772e8fSToshi Kani 	return NULL;
18546b772e8fSToshi Kani }
18556b772e8fSToshi Kani 
18563f8055c3SRafael J. Wysocki void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
18573f8055c3SRafael J. Wysocki {
18583f8055c3SRafael J. Wysocki 	if (!!hotplug->enabled == !!val)
18593f8055c3SRafael J. Wysocki 		return;
18603f8055c3SRafael J. Wysocki 
18613f8055c3SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
18623f8055c3SRafael J. Wysocki 
18633f8055c3SRafael J. Wysocki 	hotplug->enabled = val;
18643f8055c3SRafael J. Wysocki 
18653f8055c3SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
18663f8055c3SRafael J. Wysocki }
18673f8055c3SRafael J. Wysocki 
18686b772e8fSToshi Kani static void acpi_scan_init_hotplug(acpi_handle handle, int type)
18694b59cc1fSRafael J. Wysocki {
18706b772e8fSToshi Kani 	struct acpi_device_pnp pnp = {};
18716b772e8fSToshi Kani 	struct acpi_hardware_id *hwid;
18724b59cc1fSRafael J. Wysocki 	struct acpi_scan_handler *handler;
18734b59cc1fSRafael J. Wysocki 
18746b772e8fSToshi Kani 	INIT_LIST_HEAD(&pnp.ids);
18756b772e8fSToshi Kani 	acpi_set_pnp_ids(handle, &pnp, type);
18764b59cc1fSRafael J. Wysocki 
18776b772e8fSToshi Kani 	if (!pnp.type.hardware_id)
18787a26b530SCatalin Marinas 		goto out;
1879a33ec399SRafael J. Wysocki 
1880a33ec399SRafael J. Wysocki 	/*
1881a33ec399SRafael J. Wysocki 	 * This relies on the fact that acpi_install_notify_handler() will not
1882a33ec399SRafael J. Wysocki 	 * install the same notify handler routine twice for the same handle.
1883a33ec399SRafael J. Wysocki 	 */
18846b772e8fSToshi Kani 	list_for_each_entry(hwid, &pnp.ids, list) {
18856b772e8fSToshi Kani 		handler = acpi_scan_match_handler(hwid->id, NULL);
18866b772e8fSToshi Kani 		if (handler) {
18872cbb14fbSToshi Kani 			acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
18882cbb14fbSToshi Kani 					acpi_hotplug_notify_cb, handler);
18896b772e8fSToshi Kani 			break;
18906b772e8fSToshi Kani 		}
18916b772e8fSToshi Kani 	}
18926b772e8fSToshi Kani 
18937a26b530SCatalin Marinas out:
18946b772e8fSToshi Kani 	acpi_free_pnp_ids(&pnp);
1895a33ec399SRafael J. Wysocki }
1896a33ec399SRafael J. Wysocki 
1897e3863094SRafael J. Wysocki static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1898e3863094SRafael J. Wysocki 				      void *not_used, void **return_value)
18991da177e4SLinus Torvalds {
1900805d410fSRafael J. Wysocki 	struct acpi_device *device = NULL;
1901778cbc1dSBjorn Helgaas 	int type;
1902778cbc1dSBjorn Helgaas 	unsigned long long sta;
1903e3b87f8aSBjorn Helgaas 	acpi_status status;
1904778cbc1dSBjorn Helgaas 	int result;
19051da177e4SLinus Torvalds 
19064002bf38SRafael J. Wysocki 	acpi_bus_get_device(handle, &device);
19074002bf38SRafael J. Wysocki 	if (device)
19084002bf38SRafael J. Wysocki 		goto out;
19094002bf38SRafael J. Wysocki 
1910778cbc1dSBjorn Helgaas 	result = acpi_bus_type_and_status(handle, &type, &sta);
1911778cbc1dSBjorn Helgaas 	if (result)
191251a85fafSBjorn Helgaas 		return AE_OK;
19131da177e4SLinus Torvalds 
191482c7d5efSRafael J. Wysocki 	if (type == ACPI_BUS_TYPE_POWER) {
191582c7d5efSRafael J. Wysocki 		acpi_add_power_resource(handle);
191682c7d5efSRafael J. Wysocki 		return AE_OK;
191782c7d5efSRafael J. Wysocki 	}
191882c7d5efSRafael J. Wysocki 
19196b772e8fSToshi Kani 	acpi_scan_init_hotplug(handle, type);
1920a33ec399SRafael J. Wysocki 
1921778cbc1dSBjorn Helgaas 	if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1922b581a7f9SRafael J. Wysocki 	    !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
192386e4e20eSRafael J. Wysocki 		struct acpi_device_wakeup wakeup;
192486e4e20eSRafael J. Wysocki 		acpi_handle temp;
192586e4e20eSRafael J. Wysocki 
192686e4e20eSRafael J. Wysocki 		status = acpi_get_handle(handle, "_PRW", &temp);
1927993cbe59SRafael J. Wysocki 		if (ACPI_SUCCESS(status)) {
192886e4e20eSRafael J. Wysocki 			acpi_bus_extract_wakeup_device_power_package(handle,
192986e4e20eSRafael J. Wysocki 								     &wakeup);
1930993cbe59SRafael J. Wysocki 			acpi_power_resources_list_free(&wakeup.resources);
1931993cbe59SRafael J. Wysocki 		}
1932778cbc1dSBjorn Helgaas 		return AE_CTRL_DEPTH;
1933b581a7f9SRafael J. Wysocki 	}
19341da177e4SLinus Torvalds 
19352c0d4fe0SRafael J. Wysocki 	acpi_add_single_object(&device, handle, type, sta);
1936e3b87f8aSBjorn Helgaas 	if (!device)
193751a85fafSBjorn Helgaas 		return AE_CTRL_DEPTH;
19381da177e4SLinus Torvalds 
19394002bf38SRafael J. Wysocki  out:
194051a85fafSBjorn Helgaas 	if (!*return_value)
194151a85fafSBjorn Helgaas 		*return_value = device;
1942805d410fSRafael J. Wysocki 
194351a85fafSBjorn Helgaas 	return AE_OK;
19441da177e4SLinus Torvalds }
19451da177e4SLinus Torvalds 
194687b85b3cSRafael J. Wysocki static int acpi_scan_attach_handler(struct acpi_device *device)
194787b85b3cSRafael J. Wysocki {
194887b85b3cSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
194987b85b3cSRafael J. Wysocki 	int ret = 0;
195087b85b3cSRafael J. Wysocki 
195187b85b3cSRafael J. Wysocki 	list_for_each_entry(hwid, &device->pnp.ids, list) {
1952c5698074SRafael J. Wysocki 		const struct acpi_device_id *devid;
1953c5698074SRafael J. Wysocki 		struct acpi_scan_handler *handler;
195487b85b3cSRafael J. Wysocki 
1955c5698074SRafael J. Wysocki 		handler = acpi_scan_match_handler(hwid->id, &devid);
1956c5698074SRafael J. Wysocki 		if (handler) {
1957c5698074SRafael J. Wysocki 			ret = handler->attach(device, devid);
1958c5698074SRafael J. Wysocki 			if (ret > 0) {
1959c5698074SRafael J. Wysocki 				device->handler = handler;
1960c5698074SRafael J. Wysocki 				break;
1961c5698074SRafael J. Wysocki 			} else if (ret < 0) {
1962c5698074SRafael J. Wysocki 				break;
1963c5698074SRafael J. Wysocki 			}
1964c5698074SRafael J. Wysocki 		}
196587b85b3cSRafael J. Wysocki 	}
1966ca589f94SRafael J. Wysocki 	return ret;
1967ca589f94SRafael J. Wysocki }
1968ca589f94SRafael J. Wysocki 
19690fc300b0SRafael J. Wysocki static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
19700fc300b0SRafael J. Wysocki 					  void *not_used, void **ret_not_used)
197151a85fafSBjorn Helgaas {
1972805d410fSRafael J. Wysocki 	struct acpi_device *device;
1973805d410fSRafael J. Wysocki 	unsigned long long sta_not_used;
1974ca589f94SRafael J. Wysocki 	int ret;
19751da177e4SLinus Torvalds 
19767779688fSThomas Renninger 	/*
1977805d410fSRafael J. Wysocki 	 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1978805d410fSRafael J. Wysocki 	 * namespace walks prematurely.
19797779688fSThomas Renninger 	 */
1980ca589f94SRafael J. Wysocki 	if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
1981805d410fSRafael J. Wysocki 		return AE_OK;
19827779688fSThomas Renninger 
1983805d410fSRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
1984805d410fSRafael J. Wysocki 		return AE_CTRL_DEPTH;
19853fb02738SRajesh Shah 
19863a391a39SRafael J. Wysocki 	if (device->handler)
19873a391a39SRafael J. Wysocki 		return AE_OK;
19883a391a39SRafael J. Wysocki 
1989ca589f94SRafael J. Wysocki 	ret = acpi_scan_attach_handler(device);
1990ca589f94SRafael J. Wysocki 	if (ret)
1991ca589f94SRafael J. Wysocki 		return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
1992ca589f94SRafael J. Wysocki 
1993ca589f94SRafael J. Wysocki 	ret = device_attach(&device->dev);
1994ca589f94SRafael J. Wysocki 	return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
1995805d410fSRafael J. Wysocki }
19963fb02738SRajesh Shah 
1997b8bd759aSRafael J. Wysocki /**
1998b8bd759aSRafael J. Wysocki  * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1999b8bd759aSRafael J. Wysocki  * @handle: Root of the namespace scope to scan.
2000b8bd759aSRafael J. Wysocki  *
2001b8bd759aSRafael J. Wysocki  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2002b8bd759aSRafael J. Wysocki  * found devices.
2003b8bd759aSRafael J. Wysocki  *
2004b8bd759aSRafael J. Wysocki  * If no devices were found, -ENODEV is returned, but it does not mean that
2005b8bd759aSRafael J. Wysocki  * there has been a real error.  There just have been no suitable ACPI objects
2006b8bd759aSRafael J. Wysocki  * in the table trunk from which the kernel could create a device and add an
2007b8bd759aSRafael J. Wysocki  * appropriate driver.
20083757b948SRafael J. Wysocki  *
20093757b948SRafael J. Wysocki  * Must be called under acpi_scan_lock.
2010b8bd759aSRafael J. Wysocki  */
2011b8bd759aSRafael J. Wysocki int acpi_bus_scan(acpi_handle handle)
20123fb02738SRajesh Shah {
20131da177e4SLinus Torvalds 	void *device = NULL;
2014c511cc19SRafael J. Wysocki 	int error = 0;
2015c511cc19SRafael J. Wysocki 
20160cd6ac52SRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
20171da177e4SLinus Torvalds 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2018e3863094SRafael J. Wysocki 				    acpi_bus_check_add, NULL, NULL, &device);
20193fb02738SRajesh Shah 
2020d2f6650aSThomas Renninger 	if (!device)
2021c511cc19SRafael J. Wysocki 		error = -ENODEV;
2022c511cc19SRafael J. Wysocki 	else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
2023805d410fSRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
20240fc300b0SRafael J. Wysocki 				    acpi_bus_device_attach, NULL, NULL, NULL);
2025d2f6650aSThomas Renninger 
2026c511cc19SRafael J. Wysocki 	return error;
20273fb02738SRajesh Shah }
2028b8bd759aSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_scan);
20291da177e4SLinus Torvalds 
203005404d8fSRafael J. Wysocki static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
203105404d8fSRafael J. Wysocki 					  void *not_used, void **ret_not_used)
20321da177e4SLinus Torvalds {
203305404d8fSRafael J. Wysocki 	struct acpi_device *device = NULL;
20341da177e4SLinus Torvalds 
203505404d8fSRafael J. Wysocki 	if (!acpi_bus_get_device(handle, &device)) {
2036ca589f94SRafael J. Wysocki 		struct acpi_scan_handler *dev_handler = device->handler;
2037ca589f94SRafael J. Wysocki 
2038ca589f94SRafael J. Wysocki 		if (dev_handler) {
2039ca589f94SRafael J. Wysocki 			if (dev_handler->detach)
2040ca589f94SRafael J. Wysocki 				dev_handler->detach(device);
2041ca589f94SRafael J. Wysocki 
2042ca589f94SRafael J. Wysocki 			device->handler = NULL;
2043ca589f94SRafael J. Wysocki 		} else {
204405404d8fSRafael J. Wysocki 			device_release_driver(&device->dev);
204505404d8fSRafael J. Wysocki 		}
2046ca589f94SRafael J. Wysocki 	}
204705404d8fSRafael J. Wysocki 	return AE_OK;
204805404d8fSRafael J. Wysocki }
20491da177e4SLinus Torvalds 
205005404d8fSRafael J. Wysocki static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
205105404d8fSRafael J. Wysocki 				   void *not_used, void **ret_not_used)
205205404d8fSRafael J. Wysocki {
205305404d8fSRafael J. Wysocki 	struct acpi_device *device = NULL;
205405404d8fSRafael J. Wysocki 
205505404d8fSRafael J. Wysocki 	if (!acpi_bus_get_device(handle, &device))
205605404d8fSRafael J. Wysocki 		acpi_device_unregister(device);
205705404d8fSRafael J. Wysocki 
205805404d8fSRafael J. Wysocki 	return AE_OK;
205905404d8fSRafael J. Wysocki }
206005404d8fSRafael J. Wysocki 
20613757b948SRafael J. Wysocki /**
20623757b948SRafael J. Wysocki  * acpi_bus_trim - Remove ACPI device node and all of its descendants
20633757b948SRafael J. Wysocki  * @start: Root of the ACPI device nodes subtree to remove.
20643757b948SRafael J. Wysocki  *
20653757b948SRafael J. Wysocki  * Must be called under acpi_scan_lock.
20663757b948SRafael J. Wysocki  */
2067bfee26dbSRafael J. Wysocki void acpi_bus_trim(struct acpi_device *start)
20681da177e4SLinus Torvalds {
20691da177e4SLinus Torvalds 	/*
207005404d8fSRafael J. Wysocki 	 * Execute acpi_bus_device_detach() as a post-order callback to detach
207105404d8fSRafael J. Wysocki 	 * all ACPI drivers from the device nodes being removed.
20721da177e4SLinus Torvalds 	 */
207305404d8fSRafael J. Wysocki 	acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
207405404d8fSRafael J. Wysocki 			    acpi_bus_device_detach, NULL, NULL);
207505404d8fSRafael J. Wysocki 	acpi_bus_device_detach(start->handle, 0, NULL, NULL);
20761da177e4SLinus Torvalds 	/*
2077cecdb193SRafael J. Wysocki 	 * Execute acpi_bus_remove() as a post-order callback to remove device
2078cecdb193SRafael J. Wysocki 	 * nodes in the given namespace scope.
20791da177e4SLinus Torvalds 	 */
2080cecdb193SRafael J. Wysocki 	acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2081cecdb193SRafael J. Wysocki 			    acpi_bus_remove, NULL, NULL);
2082cecdb193SRafael J. Wysocki 	acpi_bus_remove(start->handle, 0, NULL, NULL);
20831da177e4SLinus Torvalds }
2084ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim);
2085ceaba663SKristen Accardi 
2086e8b945c9SBjorn Helgaas static int acpi_bus_scan_fixed(void)
20871da177e4SLinus Torvalds {
20881da177e4SLinus Torvalds 	int result = 0;
2089c4168bffSLi Shaohua 
20901da177e4SLinus Torvalds 	/*
20911da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
20921da177e4SLinus Torvalds 	 */
20932c0d4fe0SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
20942c0d4fe0SRafael J. Wysocki 		struct acpi_device *device = NULL;
20952c0d4fe0SRafael J. Wysocki 
20965c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
2097c4168bffSLi Shaohua 						ACPI_BUS_TYPE_POWER_BUTTON,
20982c0d4fe0SRafael J. Wysocki 						ACPI_STA_DEFAULT);
20992c0d4fe0SRafael J. Wysocki 		if (result)
21002c0d4fe0SRafael J. Wysocki 			return result;
21012c0d4fe0SRafael J. Wysocki 
21022c0d4fe0SRafael J. Wysocki 		result = device_attach(&device->dev);
21032c0d4fe0SRafael J. Wysocki 		if (result < 0)
21042c0d4fe0SRafael J. Wysocki 			return result;
21052c0d4fe0SRafael J. Wysocki 
2106c10d7a13SDaniel Drake 		device_init_wakeup(&device->dev, true);
21073fb02738SRajesh Shah 	}
21081da177e4SLinus Torvalds 
21092c0d4fe0SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
21102c0d4fe0SRafael J. Wysocki 		struct acpi_device *device = NULL;
21112c0d4fe0SRafael J. Wysocki 
21125c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
2113c4168bffSLi Shaohua 						ACPI_BUS_TYPE_SLEEP_BUTTON,
21142c0d4fe0SRafael J. Wysocki 						ACPI_STA_DEFAULT);
21152c0d4fe0SRafael J. Wysocki 		if (result)
21162c0d4fe0SRafael J. Wysocki 			return result;
21172c0d4fe0SRafael J. Wysocki 
21182c0d4fe0SRafael J. Wysocki 		result = device_attach(&device->dev);
21193fb02738SRajesh Shah 	}
21201da177e4SLinus Torvalds 
21212c0d4fe0SRafael J. Wysocki 	return result < 0 ? result : 0;
21221da177e4SLinus Torvalds }
21231da177e4SLinus Torvalds 
2124e747f274SBjorn Helgaas int __init acpi_scan_init(void)
21251da177e4SLinus Torvalds {
21261da177e4SLinus Torvalds 	int result;
21271da177e4SLinus Torvalds 
21285b327265SPatrick Mochel 	result = bus_register(&acpi_bus_type);
21295b327265SPatrick Mochel 	if (result) {
21305b327265SPatrick Mochel 		/* We don't want to quit even if we failed to add suspend/resume */
21315b327265SPatrick Mochel 		printk(KERN_ERR PREFIX "Could not register bus type\n");
21325b327265SPatrick Mochel 	}
21335b327265SPatrick Mochel 
213492ef2a25SRafael J. Wysocki 	acpi_pci_root_init();
21354daeaf68SRafael J. Wysocki 	acpi_pci_link_init();
2136ac212b69SRafael J. Wysocki 	acpi_processor_init();
2137141a297bSRafael J. Wysocki 	acpi_platform_init();
2138f58b082aSRafael J. Wysocki 	acpi_lpss_init();
21392fa97febSLan Tianyu 	acpi_cmos_rtc_init();
2140737f1a9fSRafael J. Wysocki 	acpi_container_init();
21410a347644SRafael J. Wysocki 	acpi_memory_hotplug_init();
214294add0f8SJiang Liu 	acpi_dock_init();
214397d9a9e9SRafael J. Wysocki 
21443757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
21451da177e4SLinus Torvalds 	/*
21461da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
21471da177e4SLinus Torvalds 	 */
21480cd6ac52SRafael J. Wysocki 	result = acpi_bus_scan(ACPI_ROOT_OBJECT);
21491da177e4SLinus Torvalds 	if (result)
21503757b948SRafael J. Wysocki 		goto out;
21511da177e4SLinus Torvalds 
21520cd6ac52SRafael J. Wysocki 	result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
21531da177e4SLinus Torvalds 	if (result)
21543757b948SRafael J. Wysocki 		goto out;
2155b8bd759aSRafael J. Wysocki 
2156b8bd759aSRafael J. Wysocki 	result = acpi_bus_scan_fixed();
2157b8bd759aSRafael J. Wysocki 	if (result) {
2158b8bd759aSRafael J. Wysocki 		acpi_device_unregister(acpi_root);
21593757b948SRafael J. Wysocki 		goto out;
2160b8bd759aSRafael J. Wysocki 	}
2161b8bd759aSRafael J. Wysocki 
2162b8bd759aSRafael J. Wysocki 	acpi_update_all_gpes();
21633757b948SRafael J. Wysocki 
2164668192b6SYinghai Lu 	acpi_pci_root_hp_init();
2165668192b6SYinghai Lu 
21663757b948SRafael J. Wysocki  out:
21673757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
21683757b948SRafael J. Wysocki 	return result;
21691da177e4SLinus Torvalds }
2170