xref: /openbmc/linux/drivers/acpi/scan.c (revision d783156ea38431b20af0d4f910a6f9f9054d33b9)
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 
17*d783156eSRafael J. Wysocki #include <asm/pgtable.h>
18*d783156eSRafael J. Wysocki 
19e60cc7a6SBjorn Helgaas #include "internal.h"
20e60cc7a6SBjorn Helgaas 
211da177e4SLinus Torvalds #define _COMPONENT		ACPI_BUS_COMPONENT
22f52fd66dSLen Brown ACPI_MODULE_NAME("scan");
231da177e4SLinus Torvalds #define STRUCT_TO_INT(s)	(*((int*)&s))
241da177e4SLinus Torvalds extern struct acpi_device *acpi_root;
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #define ACPI_BUS_CLASS			"system_bus"
2729b71a1cSThomas Renninger #define ACPI_BUS_HID			"LNXSYBUS"
281da177e4SLinus Torvalds #define ACPI_BUS_DEVICE_NAME		"System Bus"
291da177e4SLinus Torvalds 
30859ac9a4SBjorn Helgaas #define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
31859ac9a4SBjorn Helgaas 
32*d783156eSRafael J. Wysocki #define INVALID_ACPI_HANDLE	((acpi_handle)empty_zero_page)
33*d783156eSRafael J. Wysocki 
34683058e3SRafael J. Wysocki /*
35683058e3SRafael J. Wysocki  * If set, devices will be hot-removed even if they cannot be put offline
36683058e3SRafael J. Wysocki  * gracefully (from the kernel's standpoint).
37683058e3SRafael J. Wysocki  */
38683058e3SRafael J. Wysocki bool acpi_force_hot_remove;
39683058e3SRafael J. Wysocki 
40620e112cSThomas Renninger static const char *dummy_hid = "device";
412b2ae7c7SThomas Renninger 
42e49bd2ddSZhang Rui static LIST_HEAD(acpi_bus_id_list);
43c511cc19SRafael J. Wysocki static DEFINE_MUTEX(acpi_scan_lock);
44ca589f94SRafael J. Wysocki static LIST_HEAD(acpi_scan_handlers_list);
459090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock);
461da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
471da177e4SLinus Torvalds 
48e49bd2ddSZhang Rui struct acpi_device_bus_id{
49bb095854SZhang Rui 	char bus_id[15];
50e49bd2ddSZhang Rui 	unsigned int instance_no;
51e49bd2ddSZhang Rui 	struct list_head node;
52e49bd2ddSZhang Rui };
5329b71a1cSThomas Renninger 
543757b948SRafael J. Wysocki void acpi_scan_lock_acquire(void)
553757b948SRafael J. Wysocki {
563757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
573757b948SRafael J. Wysocki }
583757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
593757b948SRafael J. Wysocki 
603757b948SRafael J. Wysocki void acpi_scan_lock_release(void)
613757b948SRafael J. Wysocki {
623757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
633757b948SRafael J. Wysocki }
643757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
653757b948SRafael J. Wysocki 
66ca589f94SRafael J. Wysocki int acpi_scan_add_handler(struct acpi_scan_handler *handler)
67ca589f94SRafael J. Wysocki {
68ca589f94SRafael J. Wysocki 	if (!handler || !handler->attach)
69ca589f94SRafael J. Wysocki 		return -EINVAL;
70ca589f94SRafael J. Wysocki 
71ca589f94SRafael J. Wysocki 	list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
72ca589f94SRafael J. Wysocki 	return 0;
73ca589f94SRafael J. Wysocki }
74ca589f94SRafael J. Wysocki 
753f8055c3SRafael J. Wysocki int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
763f8055c3SRafael J. Wysocki 				       const char *hotplug_profile_name)
773f8055c3SRafael J. Wysocki {
783f8055c3SRafael J. Wysocki 	int error;
793f8055c3SRafael J. Wysocki 
803f8055c3SRafael J. Wysocki 	error = acpi_scan_add_handler(handler);
813f8055c3SRafael J. Wysocki 	if (error)
823f8055c3SRafael J. Wysocki 		return error;
833f8055c3SRafael J. Wysocki 
843f8055c3SRafael J. Wysocki 	acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
853f8055c3SRafael J. Wysocki 	return 0;
863f8055c3SRafael J. Wysocki }
873f8055c3SRafael J. Wysocki 
8829b71a1cSThomas Renninger /*
8929b71a1cSThomas Renninger  * Creates hid/cid(s) string needed for modalias and uevent
9029b71a1cSThomas Renninger  * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
9129b71a1cSThomas Renninger  * char *modalias: "acpi:IBM0001:ACPI0001"
9229b71a1cSThomas Renninger */
93b3e572d2SAdrian Bunk static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
94b3e572d2SAdrian Bunk 			   int size)
95b3e572d2SAdrian Bunk {
9629b71a1cSThomas Renninger 	int len;
975c9fcb5dSZhang Rui 	int count;
987f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
9929b71a1cSThomas Renninger 
1002b2ae7c7SThomas Renninger 	if (list_empty(&acpi_dev->pnp.ids))
1012b2ae7c7SThomas Renninger 		return 0;
1022b2ae7c7SThomas Renninger 
1035c9fcb5dSZhang Rui 	len = snprintf(modalias, size, "acpi:");
10429b71a1cSThomas Renninger 	size -= len;
10529b71a1cSThomas Renninger 
1067f47fa6cSBjorn Helgaas 	list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
1077f47fa6cSBjorn Helgaas 		count = snprintf(&modalias[len], size, "%s:", id->id);
1085c9fcb5dSZhang Rui 		if (count < 0 || count >= size)
1095c9fcb5dSZhang Rui 			return -EINVAL;
1105c9fcb5dSZhang Rui 		len += count;
1115c9fcb5dSZhang Rui 		size -= count;
1125c9fcb5dSZhang Rui 	}
1135c9fcb5dSZhang Rui 
11429b71a1cSThomas Renninger 	modalias[len] = '\0';
11529b71a1cSThomas Renninger 	return len;
11629b71a1cSThomas Renninger }
11729b71a1cSThomas Renninger 
11829b71a1cSThomas Renninger static ssize_t
11929b71a1cSThomas Renninger acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
12029b71a1cSThomas Renninger 	struct acpi_device *acpi_dev = to_acpi_device(dev);
12129b71a1cSThomas Renninger 	int len;
12229b71a1cSThomas Renninger 
12329b71a1cSThomas Renninger 	/* Device has no HID and no CID or string is >1024 */
12429b71a1cSThomas Renninger 	len = create_modalias(acpi_dev, buf, 1024);
12529b71a1cSThomas Renninger 	if (len <= 0)
12629b71a1cSThomas Renninger 		return 0;
12729b71a1cSThomas Renninger 	buf[len++] = '\n';
12829b71a1cSThomas Renninger 	return len;
12929b71a1cSThomas Renninger }
13029b71a1cSThomas Renninger static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
13129b71a1cSThomas Renninger 
1327f28ddecSRafael J. Wysocki static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
1337f28ddecSRafael J. Wysocki 				    void **ret_p)
134683058e3SRafael J. Wysocki {
135683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
136683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
137303bfdb1SRafael J. Wysocki 	bool second_pass = (bool)data;
138683058e3SRafael J. Wysocki 	acpi_status status = AE_OK;
139683058e3SRafael J. Wysocki 
140683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
141683058e3SRafael J. Wysocki 		return AE_OK;
142683058e3SRafael J. Wysocki 
1437f28ddecSRafael J. Wysocki 	if (device->handler && !device->handler->hotplug.enabled) {
1447f28ddecSRafael J. Wysocki 		*ret_p = &device->dev;
1457f28ddecSRafael J. Wysocki 		return AE_SUPPORT;
1467f28ddecSRafael J. Wysocki 	}
1477f28ddecSRafael J. Wysocki 
148683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
149683058e3SRafael J. Wysocki 
150683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node) {
151683058e3SRafael J. Wysocki 		int ret;
152683058e3SRafael J. Wysocki 
153303bfdb1SRafael J. Wysocki 		if (second_pass) {
154303bfdb1SRafael J. Wysocki 			/* Skip devices offlined by the first pass. */
155303bfdb1SRafael J. Wysocki 			if (pn->put_online)
156303bfdb1SRafael J. Wysocki 				continue;
157303bfdb1SRafael J. Wysocki 		} else {
158303bfdb1SRafael J. Wysocki 			pn->put_online = false;
159303bfdb1SRafael J. Wysocki 		}
160683058e3SRafael J. Wysocki 		ret = device_offline(pn->dev);
161683058e3SRafael J. Wysocki 		if (acpi_force_hot_remove)
162683058e3SRafael J. Wysocki 			continue;
163683058e3SRafael J. Wysocki 
164303bfdb1SRafael J. Wysocki 		if (ret >= 0) {
165303bfdb1SRafael J. Wysocki 			pn->put_online = !ret;
166303bfdb1SRafael J. Wysocki 		} else {
167303bfdb1SRafael J. Wysocki 			*ret_p = pn->dev;
168303bfdb1SRafael J. Wysocki 			if (second_pass) {
169683058e3SRafael J. Wysocki 				status = AE_ERROR;
170683058e3SRafael J. Wysocki 				break;
171683058e3SRafael J. Wysocki 			}
172303bfdb1SRafael J. Wysocki 		}
173683058e3SRafael J. Wysocki 	}
174683058e3SRafael J. Wysocki 
175683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
176683058e3SRafael J. Wysocki 
177683058e3SRafael J. Wysocki 	return status;
178683058e3SRafael J. Wysocki }
179683058e3SRafael J. Wysocki 
1807f28ddecSRafael J. Wysocki static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
1817f28ddecSRafael J. Wysocki 				   void **ret_p)
182683058e3SRafael J. Wysocki {
183683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
184683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
185683058e3SRafael J. Wysocki 
186683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
187683058e3SRafael J. Wysocki 		return AE_OK;
188683058e3SRafael J. Wysocki 
189683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
190683058e3SRafael J. Wysocki 
191683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node)
192683058e3SRafael J. Wysocki 		if (pn->put_online) {
193683058e3SRafael J. Wysocki 			device_online(pn->dev);
194683058e3SRafael J. Wysocki 			pn->put_online = false;
195683058e3SRafael J. Wysocki 		}
196683058e3SRafael J. Wysocki 
197683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
198683058e3SRafael J. Wysocki 
199683058e3SRafael J. Wysocki 	return AE_OK;
200683058e3SRafael J. Wysocki }
201683058e3SRafael J. Wysocki 
202a33ec399SRafael J. Wysocki static int acpi_scan_hot_remove(struct acpi_device *device)
2031da177e4SLinus Torvalds {
2045993c467SYinghai Lu 	acpi_handle handle = device->handle;
205303bfdb1SRafael J. Wysocki 	struct device *errdev;
206a33ec399SRafael J. Wysocki 	acpi_status status;
207882fd12eSToshi Kani 	unsigned long long sta;
208f058cdf4SRafael J. Wysocki 
2093757b948SRafael J. Wysocki 	/* If there is no handle, the device node has been unregistered. */
210a33ec399SRafael J. Wysocki 	if (!handle) {
2113757b948SRafael J. Wysocki 		dev_dbg(&device->dev, "ACPI handle missing\n");
2123757b948SRafael J. Wysocki 		put_device(&device->dev);
213a33ec399SRafael J. Wysocki 		return -EINVAL;
2143757b948SRafael J. Wysocki 	}
2153757b948SRafael J. Wysocki 
216303bfdb1SRafael J. Wysocki 	/*
217303bfdb1SRafael J. Wysocki 	 * Carry out two passes here and ignore errors in the first pass,
218303bfdb1SRafael J. Wysocki 	 * because if the devices in question are memory blocks and
219303bfdb1SRafael J. Wysocki 	 * CONFIG_MEMCG is set, one of the blocks may hold data structures
220303bfdb1SRafael J. Wysocki 	 * that the other blocks depend on, but it is not known in advance which
221303bfdb1SRafael J. Wysocki 	 * block holds them.
222303bfdb1SRafael J. Wysocki 	 *
223303bfdb1SRafael J. Wysocki 	 * If the first pass is successful, the second one isn't needed, though.
224303bfdb1SRafael J. Wysocki 	 */
225303bfdb1SRafael J. Wysocki 	errdev = NULL;
2267f28ddecSRafael J. Wysocki 	status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2277f28ddecSRafael J. Wysocki 				     NULL, acpi_bus_offline, (void *)false,
2287f28ddecSRafael J. Wysocki 				     (void **)&errdev);
2297f28ddecSRafael J. Wysocki 	if (status == AE_SUPPORT) {
2307f28ddecSRafael J. Wysocki 		dev_warn(errdev, "Offline disabled.\n");
231683058e3SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2327f28ddecSRafael J. Wysocki 				    acpi_bus_online, NULL, NULL, NULL);
2337f28ddecSRafael J. Wysocki 		put_device(&device->dev);
2347f28ddecSRafael J. Wysocki 		return -EPERM;
2357f28ddecSRafael J. Wysocki 	}
2367f28ddecSRafael J. Wysocki 	acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
237303bfdb1SRafael J. Wysocki 	if (errdev) {
238303bfdb1SRafael J. Wysocki 		errdev = NULL;
239303bfdb1SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2407f28ddecSRafael J. Wysocki 				    NULL, acpi_bus_offline, (void *)true,
2417f28ddecSRafael J. Wysocki 				    (void **)&errdev);
242303bfdb1SRafael J. Wysocki 		if (!errdev || acpi_force_hot_remove)
2437f28ddecSRafael J. Wysocki 			acpi_bus_offline(handle, 0, (void *)true,
244303bfdb1SRafael J. Wysocki 					 (void **)&errdev);
245303bfdb1SRafael J. Wysocki 
246303bfdb1SRafael J. Wysocki 		if (errdev && !acpi_force_hot_remove) {
247303bfdb1SRafael J. Wysocki 			dev_warn(errdev, "Offline failed.\n");
2487f28ddecSRafael J. Wysocki 			acpi_bus_online(handle, 0, NULL, NULL);
249303bfdb1SRafael J. Wysocki 			acpi_walk_namespace(ACPI_TYPE_ANY, handle,
2507f28ddecSRafael J. Wysocki 					    ACPI_UINT32_MAX, acpi_bus_online,
2517f28ddecSRafael J. Wysocki 					    NULL, NULL, NULL);
252683058e3SRafael J. Wysocki 			put_device(&device->dev);
253683058e3SRafael J. Wysocki 			return -EBUSY;
254683058e3SRafael J. Wysocki 		}
255303bfdb1SRafael J. Wysocki 	}
256683058e3SRafael J. Wysocki 
25726d46867SZhang Rui 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2580794469dSKay Sievers 		"Hot-removing device %s...\n", dev_name(&device->dev)));
25926d46867SZhang Rui 
260bfee26dbSRafael J. Wysocki 	acpi_bus_trim(device);
261683058e3SRafael J. Wysocki 
2623757b948SRafael J. Wysocki 	/* Device node has been unregistered. */
2633757b948SRafael J. Wysocki 	put_device(&device->dev);
264b3c450c3SToshi Kani 	device = NULL;
265b3c450c3SToshi Kani 
2667d2421f8SJiang Liu 	acpi_evaluate_lck(handle, 0);
2671da177e4SLinus Torvalds 	/*
2681da177e4SLinus Torvalds 	 * TBD: _EJD support.
2691da177e4SLinus Torvalds 	 */
2707d2421f8SJiang Liu 	status = acpi_evaluate_ej0(handle);
2717d2421f8SJiang Liu 	if (status == AE_NOT_FOUND)
272a33ec399SRafael J. Wysocki 		return -ENODEV;
2737d2421f8SJiang Liu 	else if (ACPI_FAILURE(status))
274a33ec399SRafael J. Wysocki 		return -EIO;
275f058cdf4SRafael J. Wysocki 
276882fd12eSToshi Kani 	/*
277882fd12eSToshi Kani 	 * Verify if eject was indeed successful.  If not, log an error
278882fd12eSToshi Kani 	 * message.  No need to call _OST since _EJ0 call was made OK.
279882fd12eSToshi Kani 	 */
280882fd12eSToshi Kani 	status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
281882fd12eSToshi Kani 	if (ACPI_FAILURE(status)) {
282882fd12eSToshi Kani 		acpi_handle_warn(handle,
283882fd12eSToshi Kani 			"Status check after eject failed (0x%x)\n", status);
284882fd12eSToshi Kani 	} else if (sta & ACPI_STA_DEVICE_ENABLED) {
285882fd12eSToshi Kani 		acpi_handle_warn(handle,
286882fd12eSToshi Kani 			"Eject incomplete - status 0x%llx\n", sta);
287882fd12eSToshi Kani 	}
288882fd12eSToshi Kani 
289a33ec399SRafael J. Wysocki 	return 0;
290a33ec399SRafael J. Wysocki }
291f058cdf4SRafael J. Wysocki 
2927b98118aSRafael J. Wysocki void acpi_bus_device_eject(void *data, u32 ost_src)
293a33ec399SRafael J. Wysocki {
2947b98118aSRafael J. Wysocki 	struct acpi_device *device = data;
295a3b1b1efSRafael J. Wysocki 	acpi_handle handle = device->handle;
296a33ec399SRafael J. Wysocki 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
297f943db40SRafael J. Wysocki 	int error;
298a33ec399SRafael J. Wysocki 
299e0ae8feeSRafael J. Wysocki 	lock_device_hotplug();
300a33ec399SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
301a33ec399SRafael J. Wysocki 
302a3b1b1efSRafael J. Wysocki 	if (ost_src == ACPI_NOTIFY_EJECT_REQUEST)
303a33ec399SRafael J. Wysocki 		acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
304a33ec399SRafael J. Wysocki 					  ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
305a3b1b1efSRafael J. Wysocki 
306c1beb0bdSRafael J. Wysocki 	if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
307a33ec399SRafael J. Wysocki 		kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
308a33ec399SRafael J. Wysocki 
309a33ec399SRafael J. Wysocki 	error = acpi_scan_hot_remove(device);
3107f28ddecSRafael J. Wysocki 	if (error == -EPERM) {
3117f28ddecSRafael J. Wysocki 		goto err_support;
3127f28ddecSRafael J. Wysocki 	} else if (error) {
313a33ec399SRafael J. Wysocki 		goto err_out;
3147f28ddecSRafael J. Wysocki 	}
3151da177e4SLinus Torvalds 
3163757b948SRafael J. Wysocki  out:
317f058cdf4SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
318e0ae8feeSRafael J. Wysocki 	unlock_device_hotplug();
319c8d72a5eSZhang Rui 	return;
320a33ec399SRafael J. Wysocki 
3217f28ddecSRafael J. Wysocki  err_support:
3227f28ddecSRafael J. Wysocki 	ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
323a33ec399SRafael J. Wysocki  err_out:
324a3b1b1efSRafael J. Wysocki 	acpi_evaluate_hotplug_ost(handle, ost_src, ost_code, NULL);
325a33ec399SRafael J. Wysocki 	goto out;
326a33ec399SRafael J. Wysocki }
327a33ec399SRafael J. Wysocki 
3287b98118aSRafael J. Wysocki static void acpi_scan_bus_device_check(void *data, u32 ost_source)
329a33ec399SRafael J. Wysocki {
3307b98118aSRafael J. Wysocki 	acpi_handle handle = data;
331a33ec399SRafael J. Wysocki 	struct acpi_device *device = NULL;
332a33ec399SRafael J. Wysocki 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
333a33ec399SRafael J. Wysocki 	int error;
334a33ec399SRafael J. Wysocki 
335683058e3SRafael J. Wysocki 	lock_device_hotplug();
336e0ae8feeSRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
337a33ec399SRafael J. Wysocki 
3388832f7e4SRafael J. Wysocki 	if (ost_source != ACPI_NOTIFY_BUS_CHECK) {
339a33ec399SRafael J. Wysocki 		acpi_bus_get_device(handle, &device);
340a33ec399SRafael J. Wysocki 		if (device) {
341a33ec399SRafael J. Wysocki 			dev_warn(&device->dev, "Attempt to re-insert\n");
342a33ec399SRafael J. Wysocki 			goto out;
343a33ec399SRafael J. Wysocki 		}
3448832f7e4SRafael J. Wysocki 	}
345a33ec399SRafael J. Wysocki 	error = acpi_bus_scan(handle);
346a33ec399SRafael J. Wysocki 	if (error) {
347a33ec399SRafael J. Wysocki 		acpi_handle_warn(handle, "Namespace scan failure\n");
348a33ec399SRafael J. Wysocki 		goto out;
349a33ec399SRafael J. Wysocki 	}
350a33ec399SRafael J. Wysocki 	error = acpi_bus_get_device(handle, &device);
351a33ec399SRafael J. Wysocki 	if (error) {
352a33ec399SRafael J. Wysocki 		acpi_handle_warn(handle, "Missing device node object\n");
353a33ec399SRafael J. Wysocki 		goto out;
354a33ec399SRafael J. Wysocki 	}
355a33ec399SRafael J. Wysocki 	ost_code = ACPI_OST_SC_SUCCESS;
356a33ec399SRafael J. Wysocki 	if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
357a33ec399SRafael J. Wysocki 		kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
358a33ec399SRafael J. Wysocki 
359a33ec399SRafael J. Wysocki  out:
360a33ec399SRafael J. Wysocki 	acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
361a33ec399SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
362e0ae8feeSRafael J. Wysocki 	unlock_device_hotplug();
363a33ec399SRafael J. Wysocki }
364a33ec399SRafael J. Wysocki 
3652cbb14fbSToshi Kani static void acpi_hotplug_unsupported(acpi_handle handle, u32 type)
3662cbb14fbSToshi Kani {
3672cbb14fbSToshi Kani 	u32 ost_status;
3682cbb14fbSToshi Kani 
3692cbb14fbSToshi Kani 	switch (type) {
3702cbb14fbSToshi Kani 	case ACPI_NOTIFY_BUS_CHECK:
3712cbb14fbSToshi Kani 		acpi_handle_debug(handle,
3722cbb14fbSToshi Kani 			"ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
3732cbb14fbSToshi Kani 		ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
3742cbb14fbSToshi Kani 		break;
3752cbb14fbSToshi Kani 	case ACPI_NOTIFY_DEVICE_CHECK:
3762cbb14fbSToshi Kani 		acpi_handle_debug(handle,
3772cbb14fbSToshi Kani 			"ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
3782cbb14fbSToshi Kani 		ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
3792cbb14fbSToshi Kani 		break;
3802cbb14fbSToshi Kani 	case ACPI_NOTIFY_EJECT_REQUEST:
3812cbb14fbSToshi Kani 		acpi_handle_debug(handle,
3822cbb14fbSToshi Kani 			"ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
3832cbb14fbSToshi Kani 		ost_status = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
3842cbb14fbSToshi Kani 		break;
3852cbb14fbSToshi Kani 	default:
3862cbb14fbSToshi Kani 		/* non-hotplug event; possibly handled by other handler */
3872cbb14fbSToshi Kani 		return;
3882cbb14fbSToshi Kani 	}
3892cbb14fbSToshi Kani 
3902cbb14fbSToshi Kani 	acpi_evaluate_hotplug_ost(handle, type, ost_status, NULL);
3912cbb14fbSToshi Kani }
3922cbb14fbSToshi Kani 
3932cbb14fbSToshi Kani static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
394a33ec399SRafael J. Wysocki {
3952cbb14fbSToshi Kani 	struct acpi_scan_handler *handler = data;
396a3b1b1efSRafael J. Wysocki 	struct acpi_device *adev;
397a33ec399SRafael J. Wysocki 	acpi_status status;
398a33ec399SRafael J. Wysocki 
3992cbb14fbSToshi Kani 	if (!handler->hotplug.enabled)
4002cbb14fbSToshi Kani 		return acpi_hotplug_unsupported(handle, type);
4012cbb14fbSToshi Kani 
402a33ec399SRafael J. Wysocki 	switch (type) {
403a33ec399SRafael J. Wysocki 	case ACPI_NOTIFY_BUS_CHECK:
404a33ec399SRafael J. Wysocki 		acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
405a33ec399SRafael J. Wysocki 		break;
406a33ec399SRafael J. Wysocki 	case ACPI_NOTIFY_DEVICE_CHECK:
407a33ec399SRafael J. Wysocki 		acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
408a33ec399SRafael J. Wysocki 		break;
409a33ec399SRafael J. Wysocki 	case ACPI_NOTIFY_EJECT_REQUEST:
410a33ec399SRafael J. Wysocki 		acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
4115beaee4fSRafael J. Wysocki 		if (acpi_bus_get_device(handle, &adev))
412a3b1b1efSRafael J. Wysocki 			goto err_out;
413a3b1b1efSRafael J. Wysocki 
414a3b1b1efSRafael J. Wysocki 		get_device(&adev->dev);
4157b98118aSRafael J. Wysocki 		status = acpi_hotplug_execute(acpi_bus_device_eject, adev, type);
416a3b1b1efSRafael J. Wysocki 		if (ACPI_SUCCESS(status))
417a3b1b1efSRafael J. Wysocki 			return;
418a3b1b1efSRafael J. Wysocki 
419a3b1b1efSRafael J. Wysocki 		put_device(&adev->dev);
420a3b1b1efSRafael J. Wysocki 		goto err_out;
421a33ec399SRafael J. Wysocki 	default:
422a33ec399SRafael J. Wysocki 		/* non-hotplug event; possibly handled by other handler */
423a33ec399SRafael J. Wysocki 		return;
424a33ec399SRafael J. Wysocki 	}
4257b98118aSRafael J. Wysocki 	status = acpi_hotplug_execute(acpi_scan_bus_device_check, handle, type);
426a3b1b1efSRafael J. Wysocki 	if (ACPI_SUCCESS(status))
427a3b1b1efSRafael J. Wysocki 		return;
428a3b1b1efSRafael J. Wysocki 
429a3b1b1efSRafael J. Wysocki  err_out:
430a33ec399SRafael J. Wysocki 	acpi_evaluate_hotplug_ost(handle, type,
431a3b1b1efSRafael J. Wysocki 				  ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
432a33ec399SRafael J. Wysocki }
433a33ec399SRafael J. Wysocki 
434836aedb1SRafael J. Wysocki static ssize_t real_power_state_show(struct device *dev,
435836aedb1SRafael J. Wysocki 				     struct device_attribute *attr, char *buf)
436836aedb1SRafael J. Wysocki {
437836aedb1SRafael J. Wysocki 	struct acpi_device *adev = to_acpi_device(dev);
438836aedb1SRafael J. Wysocki 	int state;
439836aedb1SRafael J. Wysocki 	int ret;
440836aedb1SRafael J. Wysocki 
441836aedb1SRafael J. Wysocki 	ret = acpi_device_get_power(adev, &state);
442836aedb1SRafael J. Wysocki 	if (ret)
443836aedb1SRafael J. Wysocki 		return ret;
444836aedb1SRafael J. Wysocki 
445836aedb1SRafael J. Wysocki 	return sprintf(buf, "%s\n", acpi_power_state_string(state));
446836aedb1SRafael J. Wysocki }
447836aedb1SRafael J. Wysocki 
448836aedb1SRafael J. Wysocki static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
449836aedb1SRafael J. Wysocki 
450836aedb1SRafael J. Wysocki static ssize_t power_state_show(struct device *dev,
451836aedb1SRafael J. Wysocki 				struct device_attribute *attr, char *buf)
452836aedb1SRafael J. Wysocki {
453836aedb1SRafael J. Wysocki 	struct acpi_device *adev = to_acpi_device(dev);
454836aedb1SRafael J. Wysocki 
455836aedb1SRafael J. Wysocki 	return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
456836aedb1SRafael J. Wysocki }
457836aedb1SRafael J. Wysocki 
458836aedb1SRafael J. Wysocki static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
459836aedb1SRafael J. Wysocki 
4601da177e4SLinus Torvalds static ssize_t
461f883d9dbSPatrick Mochel acpi_eject_store(struct device *d, struct device_attribute *attr,
462f883d9dbSPatrick Mochel 		const char *buf, size_t count)
4631da177e4SLinus Torvalds {
464f883d9dbSPatrick Mochel 	struct acpi_device *acpi_device = to_acpi_device(d);
465a33ec399SRafael J. Wysocki 	acpi_object_type not_used;
466a33ec399SRafael J. Wysocki 	acpi_status status;
4671da177e4SLinus Torvalds 
468a33ec399SRafael J. Wysocki 	if (!count || buf[0] != '1')
4691da177e4SLinus Torvalds 		return -EINVAL;
4701da177e4SLinus Torvalds 
471a33ec399SRafael J. Wysocki 	if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
472a33ec399SRafael J. Wysocki 	    && !acpi_device->driver)
473a33ec399SRafael J. Wysocki 		return -ENODEV;
474a33ec399SRafael J. Wysocki 
475a33ec399SRafael J. Wysocki 	status = acpi_get_type(acpi_device->handle, &not_used);
476a33ec399SRafael J. Wysocki 	if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
477a33ec399SRafael J. Wysocki 		return -ENODEV;
478a33ec399SRafael J. Wysocki 
479f943db40SRafael J. Wysocki 	acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
480a33ec399SRafael J. Wysocki 				  ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
481a33ec399SRafael J. Wysocki 	get_device(&acpi_device->dev);
4827b98118aSRafael J. Wysocki 	status = acpi_hotplug_execute(acpi_bus_device_eject, acpi_device,
4837b98118aSRafael J. Wysocki 				      ACPI_OST_EC_OSPM_EJECT);
484f943db40SRafael J. Wysocki 	if (ACPI_SUCCESS(status))
485f943db40SRafael J. Wysocki 		return count;
486f943db40SRafael J. Wysocki 
4873757b948SRafael J. Wysocki 	put_device(&acpi_device->dev);
488f943db40SRafael J. Wysocki 	acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
489a33ec399SRafael J. Wysocki 				  ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
4905add99cfSRafael J. Wysocki 	return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
493f883d9dbSPatrick Mochel static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
494f883d9dbSPatrick Mochel 
495e49bd2ddSZhang Rui static ssize_t
496e49bd2ddSZhang Rui acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
497e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
498e49bd2ddSZhang Rui 
499ea8d82fdSBjorn Helgaas 	return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
500e49bd2ddSZhang Rui }
501e49bd2ddSZhang Rui static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
502e49bd2ddSZhang Rui 
503923d4a45SLv Zheng static ssize_t acpi_device_uid_show(struct device *dev,
504923d4a45SLv Zheng 				    struct device_attribute *attr, char *buf)
505923d4a45SLv Zheng {
506923d4a45SLv Zheng 	struct acpi_device *acpi_dev = to_acpi_device(dev);
507923d4a45SLv Zheng 
508923d4a45SLv Zheng 	return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
509923d4a45SLv Zheng }
510923d4a45SLv Zheng static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
511923d4a45SLv Zheng 
512923d4a45SLv Zheng static ssize_t acpi_device_adr_show(struct device *dev,
513923d4a45SLv Zheng 				    struct device_attribute *attr, char *buf)
514923d4a45SLv Zheng {
515923d4a45SLv Zheng 	struct acpi_device *acpi_dev = to_acpi_device(dev);
516923d4a45SLv Zheng 
517923d4a45SLv Zheng 	return sprintf(buf, "0x%08x\n",
518923d4a45SLv Zheng 		       (unsigned int)(acpi_dev->pnp.bus_address));
519923d4a45SLv Zheng }
520923d4a45SLv Zheng static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
521923d4a45SLv Zheng 
522e49bd2ddSZhang Rui static ssize_t
523e49bd2ddSZhang Rui acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
524e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
525e49bd2ddSZhang Rui 	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
526e49bd2ddSZhang Rui 	int result;
527e49bd2ddSZhang Rui 
528e49bd2ddSZhang Rui 	result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
529e49bd2ddSZhang Rui 	if (result)
530e49bd2ddSZhang Rui 		goto end;
531e49bd2ddSZhang Rui 
532e49bd2ddSZhang Rui 	result = sprintf(buf, "%s\n", (char*)path.pointer);
533e49bd2ddSZhang Rui 	kfree(path.pointer);
534e49bd2ddSZhang Rui end:
535e49bd2ddSZhang Rui 	return result;
536e49bd2ddSZhang Rui }
537e49bd2ddSZhang Rui static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
538e49bd2ddSZhang Rui 
539d1efe3c3SLance Ortiz /* sysfs file that shows description text from the ACPI _STR method */
540d1efe3c3SLance Ortiz static ssize_t description_show(struct device *dev,
541d1efe3c3SLance Ortiz 				struct device_attribute *attr,
542d1efe3c3SLance Ortiz 				char *buf) {
543d1efe3c3SLance Ortiz 	struct acpi_device *acpi_dev = to_acpi_device(dev);
544d1efe3c3SLance Ortiz 	int result;
545d1efe3c3SLance Ortiz 
546d1efe3c3SLance Ortiz 	if (acpi_dev->pnp.str_obj == NULL)
547d1efe3c3SLance Ortiz 		return 0;
548d1efe3c3SLance Ortiz 
549d1efe3c3SLance Ortiz 	/*
550d1efe3c3SLance Ortiz 	 * The _STR object contains a Unicode identifier for a device.
551d1efe3c3SLance Ortiz 	 * We need to convert to utf-8 so it can be displayed.
552d1efe3c3SLance Ortiz 	 */
553d1efe3c3SLance Ortiz 	result = utf16s_to_utf8s(
554d1efe3c3SLance Ortiz 		(wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
555d1efe3c3SLance Ortiz 		acpi_dev->pnp.str_obj->buffer.length,
556d1efe3c3SLance Ortiz 		UTF16_LITTLE_ENDIAN, buf,
557d1efe3c3SLance Ortiz 		PAGE_SIZE);
558d1efe3c3SLance Ortiz 
559d1efe3c3SLance Ortiz 	buf[result++] = '\n';
560d1efe3c3SLance Ortiz 
561d1efe3c3SLance Ortiz 	return result;
562d1efe3c3SLance Ortiz }
563d1efe3c3SLance Ortiz static DEVICE_ATTR(description, 0444, description_show, NULL);
564d1efe3c3SLance Ortiz 
565bb74ac23SYasuaki Ishimatsu static ssize_t
566bb74ac23SYasuaki Ishimatsu acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
567bb74ac23SYasuaki Ishimatsu 		     char *buf) {
568bb74ac23SYasuaki Ishimatsu 	struct acpi_device *acpi_dev = to_acpi_device(dev);
569bb74ac23SYasuaki Ishimatsu 
570bb74ac23SYasuaki Ishimatsu 	return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
571bb74ac23SYasuaki Ishimatsu }
572bb74ac23SYasuaki Ishimatsu static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
573bb74ac23SYasuaki Ishimatsu 
574e49bd2ddSZhang Rui static int acpi_device_setup_files(struct acpi_device *dev)
575f883d9dbSPatrick Mochel {
576d1efe3c3SLance Ortiz 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
577f883d9dbSPatrick Mochel 	acpi_status status;
578bb74ac23SYasuaki Ishimatsu 	unsigned long long sun;
579e49bd2ddSZhang Rui 	int result = 0;
580e49bd2ddSZhang Rui 
581e49bd2ddSZhang Rui 	/*
582e49bd2ddSZhang Rui 	 * Devices gotten from FADT don't have a "path" attribute
583e49bd2ddSZhang Rui 	 */
584e49bd2ddSZhang Rui 	if (dev->handle) {
585e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_path);
586e49bd2ddSZhang Rui 		if (result)
587e49bd2ddSZhang Rui 			goto end;
588e49bd2ddSZhang Rui 	}
589e49bd2ddSZhang Rui 
5902b2ae7c7SThomas Renninger 	if (!list_empty(&dev->pnp.ids)) {
591e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_hid);
592e49bd2ddSZhang Rui 		if (result)
593e49bd2ddSZhang Rui 			goto end;
594f883d9dbSPatrick Mochel 
59529b71a1cSThomas Renninger 		result = device_create_file(&dev->dev, &dev_attr_modalias);
59629b71a1cSThomas Renninger 		if (result)
59729b71a1cSThomas Renninger 			goto end;
5982b2ae7c7SThomas Renninger 	}
59929b71a1cSThomas Renninger 
600f883d9dbSPatrick Mochel 	/*
601d1efe3c3SLance Ortiz 	 * If device has _STR, 'description' file is created
602d1efe3c3SLance Ortiz 	 */
603952c63e9SJiang Liu 	if (acpi_has_method(dev->handle, "_STR")) {
604d1efe3c3SLance Ortiz 		status = acpi_evaluate_object(dev->handle, "_STR",
605d1efe3c3SLance Ortiz 					NULL, &buffer);
606d1efe3c3SLance Ortiz 		if (ACPI_FAILURE(status))
607d1efe3c3SLance Ortiz 			buffer.pointer = NULL;
608d1efe3c3SLance Ortiz 		dev->pnp.str_obj = buffer.pointer;
609d1efe3c3SLance Ortiz 		result = device_create_file(&dev->dev, &dev_attr_description);
610d1efe3c3SLance Ortiz 		if (result)
611d1efe3c3SLance Ortiz 			goto end;
612d1efe3c3SLance Ortiz 	}
613d1efe3c3SLance Ortiz 
614d4e1a692SToshi Kani 	if (dev->pnp.type.bus_address)
615923d4a45SLv Zheng 		result = device_create_file(&dev->dev, &dev_attr_adr);
616923d4a45SLv Zheng 	if (dev->pnp.unique_id)
617923d4a45SLv Zheng 		result = device_create_file(&dev->dev, &dev_attr_uid);
618923d4a45SLv Zheng 
619bb74ac23SYasuaki Ishimatsu 	status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
620bb74ac23SYasuaki Ishimatsu 	if (ACPI_SUCCESS(status)) {
621bb74ac23SYasuaki Ishimatsu 		dev->pnp.sun = (unsigned long)sun;
622bb74ac23SYasuaki Ishimatsu 		result = device_create_file(&dev->dev, &dev_attr_sun);
623bb74ac23SYasuaki Ishimatsu 		if (result)
624bb74ac23SYasuaki Ishimatsu 			goto end;
625bb74ac23SYasuaki Ishimatsu 	} else {
626bb74ac23SYasuaki Ishimatsu 		dev->pnp.sun = (unsigned long)-1;
627bb74ac23SYasuaki Ishimatsu 	}
628bb74ac23SYasuaki Ishimatsu 
629d1efe3c3SLance Ortiz         /*
630f883d9dbSPatrick Mochel          * If device has _EJ0, 'eject' file is created that is used to trigger
631f883d9dbSPatrick Mochel          * hot-removal function from userland.
632f883d9dbSPatrick Mochel          */
633952c63e9SJiang Liu 	if (acpi_has_method(dev->handle, "_EJ0")) {
634e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_eject);
635836aedb1SRafael J. Wysocki 		if (result)
636836aedb1SRafael J. Wysocki 			return result;
637836aedb1SRafael J. Wysocki 	}
638836aedb1SRafael J. Wysocki 
639836aedb1SRafael J. Wysocki 	if (dev->flags.power_manageable) {
640836aedb1SRafael J. Wysocki 		result = device_create_file(&dev->dev, &dev_attr_power_state);
641836aedb1SRafael J. Wysocki 		if (result)
642836aedb1SRafael J. Wysocki 			return result;
643836aedb1SRafael J. Wysocki 
644836aedb1SRafael J. Wysocki 		if (dev->power.flags.power_resources)
645836aedb1SRafael J. Wysocki 			result = device_create_file(&dev->dev,
646836aedb1SRafael J. Wysocki 						    &dev_attr_real_power_state);
647836aedb1SRafael J. Wysocki 	}
648836aedb1SRafael J. Wysocki 
649e49bd2ddSZhang Rui end:
650e49bd2ddSZhang Rui 	return result;
651f883d9dbSPatrick Mochel }
652f883d9dbSPatrick Mochel 
653f883d9dbSPatrick Mochel static void acpi_device_remove_files(struct acpi_device *dev)
654f883d9dbSPatrick Mochel {
655836aedb1SRafael J. Wysocki 	if (dev->flags.power_manageable) {
656836aedb1SRafael J. Wysocki 		device_remove_file(&dev->dev, &dev_attr_power_state);
657836aedb1SRafael J. Wysocki 		if (dev->power.flags.power_resources)
658836aedb1SRafael J. Wysocki 			device_remove_file(&dev->dev,
659836aedb1SRafael J. Wysocki 					   &dev_attr_real_power_state);
660836aedb1SRafael J. Wysocki 	}
661836aedb1SRafael J. Wysocki 
662f883d9dbSPatrick Mochel 	/*
663d1efe3c3SLance Ortiz 	 * If device has _STR, remove 'description' file
664d1efe3c3SLance Ortiz 	 */
665952c63e9SJiang Liu 	if (acpi_has_method(dev->handle, "_STR")) {
666d1efe3c3SLance Ortiz 		kfree(dev->pnp.str_obj);
667d1efe3c3SLance Ortiz 		device_remove_file(&dev->dev, &dev_attr_description);
668d1efe3c3SLance Ortiz 	}
669d1efe3c3SLance Ortiz 	/*
670d1efe3c3SLance Ortiz 	 * If device has _EJ0, remove 'eject' file.
671f883d9dbSPatrick Mochel 	 */
672952c63e9SJiang Liu 	if (acpi_has_method(dev->handle, "_EJ0"))
673f883d9dbSPatrick Mochel 		device_remove_file(&dev->dev, &dev_attr_eject);
674e49bd2ddSZhang Rui 
675952c63e9SJiang Liu 	if (acpi_has_method(dev->handle, "_SUN"))
676bb74ac23SYasuaki Ishimatsu 		device_remove_file(&dev->dev, &dev_attr_sun);
677bb74ac23SYasuaki Ishimatsu 
678923d4a45SLv Zheng 	if (dev->pnp.unique_id)
679923d4a45SLv Zheng 		device_remove_file(&dev->dev, &dev_attr_uid);
680d4e1a692SToshi Kani 	if (dev->pnp.type.bus_address)
681923d4a45SLv Zheng 		device_remove_file(&dev->dev, &dev_attr_adr);
68229b71a1cSThomas Renninger 	device_remove_file(&dev->dev, &dev_attr_modalias);
683e49bd2ddSZhang Rui 	device_remove_file(&dev->dev, &dev_attr_hid);
684e49bd2ddSZhang Rui 	if (dev->handle)
685e49bd2ddSZhang Rui 		device_remove_file(&dev->dev, &dev_attr_path);
686f883d9dbSPatrick Mochel }
6871da177e4SLinus Torvalds /* --------------------------------------------------------------------------
6889e89dde2SZhang Rui 			ACPI Bus operations
6891da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
69029b71a1cSThomas Renninger 
691cf761af9SMika Westerberg static const struct acpi_device_id *__acpi_match_device(
692cf761af9SMika Westerberg 	struct acpi_device *device, const struct acpi_device_id *ids)
69329b71a1cSThomas Renninger {
69429b71a1cSThomas Renninger 	const struct acpi_device_id *id;
6957f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hwid;
69629b71a1cSThomas Renninger 
69739a0ad87SZhao Yakui 	/*
69839a0ad87SZhao Yakui 	 * If the device is not present, it is unnecessary to load device
69939a0ad87SZhao Yakui 	 * driver for it.
70039a0ad87SZhao Yakui 	 */
70139a0ad87SZhao Yakui 	if (!device->status.present)
702cf761af9SMika Westerberg 		return NULL;
70339a0ad87SZhao Yakui 
7047f47fa6cSBjorn Helgaas 	for (id = ids; id->id[0]; id++)
7057f47fa6cSBjorn Helgaas 		list_for_each_entry(hwid, &device->pnp.ids, list)
7067f47fa6cSBjorn Helgaas 			if (!strcmp((char *) id->id, hwid->id))
707cf761af9SMika Westerberg 				return id;
70829b71a1cSThomas Renninger 
709cf761af9SMika Westerberg 	return NULL;
710cf761af9SMika Westerberg }
711cf761af9SMika Westerberg 
712cf761af9SMika Westerberg /**
713cf761af9SMika Westerberg  * acpi_match_device - Match a struct device against a given list of ACPI IDs
714cf761af9SMika Westerberg  * @ids: Array of struct acpi_device_id object to match against.
715cf761af9SMika Westerberg  * @dev: The device structure to match.
716cf761af9SMika Westerberg  *
717cf761af9SMika Westerberg  * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
718cf761af9SMika Westerberg  * object for that handle and use that object to match against a given list of
719cf761af9SMika Westerberg  * device IDs.
720cf761af9SMika Westerberg  *
721cf761af9SMika Westerberg  * Return a pointer to the first matching ID on success or %NULL on failure.
722cf761af9SMika Westerberg  */
723cf761af9SMika Westerberg const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
724cf761af9SMika Westerberg 					       const struct device *dev)
725cf761af9SMika Westerberg {
726cf761af9SMika Westerberg 	struct acpi_device *adev;
7270613e1f7SRafael J. Wysocki 	acpi_handle handle = ACPI_HANDLE(dev);
728cf761af9SMika Westerberg 
7290613e1f7SRafael J. Wysocki 	if (!ids || !handle || acpi_bus_get_device(handle, &adev))
730cf761af9SMika Westerberg 		return NULL;
731cf761af9SMika Westerberg 
732cf761af9SMika Westerberg 	return __acpi_match_device(adev, ids);
733cf761af9SMika Westerberg }
734cf761af9SMika Westerberg EXPORT_SYMBOL_GPL(acpi_match_device);
735cf761af9SMika Westerberg 
736cf761af9SMika Westerberg int acpi_match_device_ids(struct acpi_device *device,
737cf761af9SMika Westerberg 			  const struct acpi_device_id *ids)
738cf761af9SMika Westerberg {
739cf761af9SMika Westerberg 	return __acpi_match_device(device, ids) ? 0 : -ENOENT;
74029b71a1cSThomas Renninger }
74129b71a1cSThomas Renninger EXPORT_SYMBOL(acpi_match_device_ids);
74229b71a1cSThomas Renninger 
7430b224527SRafael J. Wysocki static void acpi_free_power_resources_lists(struct acpi_device *device)
7440b224527SRafael J. Wysocki {
7450b224527SRafael J. Wysocki 	int i;
7460b224527SRafael J. Wysocki 
747993cbe59SRafael J. Wysocki 	if (device->wakeup.flags.valid)
748993cbe59SRafael J. Wysocki 		acpi_power_resources_list_free(&device->wakeup.resources);
749993cbe59SRafael J. Wysocki 
7500b224527SRafael J. Wysocki 	if (!device->flags.power_manageable)
7510b224527SRafael J. Wysocki 		return;
7520b224527SRafael J. Wysocki 
7530b224527SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
7540b224527SRafael J. Wysocki 		struct acpi_device_power_state *ps = &device->power.states[i];
7550b224527SRafael J. Wysocki 		acpi_power_resources_list_free(&ps->resources);
7560b224527SRafael J. Wysocki 	}
7577f47fa6cSBjorn Helgaas }
7587f47fa6cSBjorn Helgaas 
7591890a97aSPatrick Mochel static void acpi_device_release(struct device *dev)
7601890a97aSPatrick Mochel {
7611890a97aSPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
7621890a97aSPatrick Mochel 
763c0af4175SToshi Kani 	acpi_free_pnp_ids(&acpi_dev->pnp);
7640b224527SRafael J. Wysocki 	acpi_free_power_resources_lists(acpi_dev);
7651890a97aSPatrick Mochel 	kfree(acpi_dev);
7661890a97aSPatrick Mochel }
7671890a97aSPatrick Mochel 
7685d9464a4SPatrick Mochel static int acpi_bus_match(struct device *dev, struct device_driver *drv)
7691da177e4SLinus Torvalds {
7705d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
7715d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(drv);
7725d9464a4SPatrick Mochel 
773209d3b17SRafael J. Wysocki 	return acpi_dev->flags.match_driver
774805d410fSRafael J. Wysocki 		&& !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
7755d9464a4SPatrick Mochel }
7765d9464a4SPatrick Mochel 
7777eff2e7aSKay Sievers static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
7785d9464a4SPatrick Mochel {
7795d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
7807eff2e7aSKay Sievers 	int len;
7815d9464a4SPatrick Mochel 
7822b2ae7c7SThomas Renninger 	if (list_empty(&acpi_dev->pnp.ids))
7832b2ae7c7SThomas Renninger 		return 0;
7842b2ae7c7SThomas Renninger 
7857eff2e7aSKay Sievers 	if (add_uevent_var(env, "MODALIAS="))
7867eff2e7aSKay Sievers 		return -ENOMEM;
7877eff2e7aSKay Sievers 	len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
7887eff2e7aSKay Sievers 			      sizeof(env->buf) - env->buflen);
7897eff2e7aSKay Sievers 	if (len >= (sizeof(env->buf) - env->buflen))
7907eff2e7aSKay Sievers 		return -ENOMEM;
7917eff2e7aSKay Sievers 	env->buflen += len;
7921da177e4SLinus Torvalds 	return 0;
7931da177e4SLinus Torvalds }
7941da177e4SLinus Torvalds 
79546ec8598SBjorn Helgaas static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
79646ec8598SBjorn Helgaas {
79746ec8598SBjorn Helgaas 	struct acpi_device *device = data;
79846ec8598SBjorn Helgaas 
79946ec8598SBjorn Helgaas 	device->driver->ops.notify(device, event);
80046ec8598SBjorn Helgaas }
80146ec8598SBjorn Helgaas 
80246ec8598SBjorn Helgaas static acpi_status acpi_device_notify_fixed(void *data)
80346ec8598SBjorn Helgaas {
80446ec8598SBjorn Helgaas 	struct acpi_device *device = data;
80546ec8598SBjorn Helgaas 
80653de5356SBjorn Helgaas 	/* Fixed hardware devices have no handles */
80753de5356SBjorn Helgaas 	acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
80846ec8598SBjorn Helgaas 	return AE_OK;
80946ec8598SBjorn Helgaas }
81046ec8598SBjorn Helgaas 
81146ec8598SBjorn Helgaas static int acpi_device_install_notify_handler(struct acpi_device *device)
81246ec8598SBjorn Helgaas {
81346ec8598SBjorn Helgaas 	acpi_status status;
81446ec8598SBjorn Helgaas 
815ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
81646ec8598SBjorn Helgaas 		status =
81746ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
81846ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
81946ec8598SBjorn Helgaas 						     device);
820ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
82146ec8598SBjorn Helgaas 		status =
82246ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
82346ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
82446ec8598SBjorn Helgaas 						     device);
82546ec8598SBjorn Helgaas 	else
82646ec8598SBjorn Helgaas 		status = acpi_install_notify_handler(device->handle,
82746ec8598SBjorn Helgaas 						     ACPI_DEVICE_NOTIFY,
82846ec8598SBjorn Helgaas 						     acpi_device_notify,
82946ec8598SBjorn Helgaas 						     device);
83046ec8598SBjorn Helgaas 
83146ec8598SBjorn Helgaas 	if (ACPI_FAILURE(status))
83246ec8598SBjorn Helgaas 		return -EINVAL;
83346ec8598SBjorn Helgaas 	return 0;
83446ec8598SBjorn Helgaas }
83546ec8598SBjorn Helgaas 
83646ec8598SBjorn Helgaas static void acpi_device_remove_notify_handler(struct acpi_device *device)
83746ec8598SBjorn Helgaas {
838ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
83946ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
84046ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
841ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
84246ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
84346ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
84446ec8598SBjorn Helgaas 	else
84546ec8598SBjorn Helgaas 		acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
84646ec8598SBjorn Helgaas 					   acpi_device_notify);
84746ec8598SBjorn Helgaas }
84846ec8598SBjorn Helgaas 
8495d9464a4SPatrick Mochel static int acpi_device_probe(struct device *dev)
8505d9464a4SPatrick Mochel {
8515d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
8525d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
8535d9464a4SPatrick Mochel 	int ret;
8545d9464a4SPatrick Mochel 
85524071f47SRafael J. Wysocki 	if (acpi_dev->handler)
85624071f47SRafael J. Wysocki 		return -EINVAL;
85724071f47SRafael J. Wysocki 
858d9e455f5SRafael J. Wysocki 	if (!acpi_drv->ops.add)
859d9e455f5SRafael J. Wysocki 		return -ENOSYS;
860d9e455f5SRafael J. Wysocki 
861d9e455f5SRafael J. Wysocki 	ret = acpi_drv->ops.add(acpi_dev);
862d9e455f5SRafael J. Wysocki 	if (ret)
863d9e455f5SRafael J. Wysocki 		return ret;
864d9e455f5SRafael J. Wysocki 
865d9e455f5SRafael J. Wysocki 	acpi_dev->driver = acpi_drv;
866d9e455f5SRafael J. Wysocki 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
867d9e455f5SRafael J. Wysocki 			  "Driver [%s] successfully bound to device [%s]\n",
868d9e455f5SRafael J. Wysocki 			  acpi_drv->name, acpi_dev->pnp.bus_id));
869d9e455f5SRafael J. Wysocki 
87046ec8598SBjorn Helgaas 	if (acpi_drv->ops.notify) {
87146ec8598SBjorn Helgaas 		ret = acpi_device_install_notify_handler(acpi_dev);
87246ec8598SBjorn Helgaas 		if (ret) {
87346ec8598SBjorn Helgaas 			if (acpi_drv->ops.remove)
87451fac838SRafael J. Wysocki 				acpi_drv->ops.remove(acpi_dev);
875d9e455f5SRafael J. Wysocki 
8765f27ee8eSToshi Kani 			acpi_dev->driver = NULL;
8775f27ee8eSToshi Kani 			acpi_dev->driver_data = NULL;
87846ec8598SBjorn Helgaas 			return ret;
87946ec8598SBjorn Helgaas 		}
88046ec8598SBjorn Helgaas 	}
88146ec8598SBjorn Helgaas 
882d9e455f5SRafael J. Wysocki 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
8835d9464a4SPatrick Mochel 			  acpi_drv->name, acpi_dev->pnp.bus_id));
8845d9464a4SPatrick Mochel 	get_device(dev);
885d9e455f5SRafael J. Wysocki 	return 0;
8865d9464a4SPatrick Mochel }
8875d9464a4SPatrick Mochel 
8885d9464a4SPatrick Mochel static int acpi_device_remove(struct device * dev)
8895d9464a4SPatrick Mochel {
8905d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
8915d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
8925d9464a4SPatrick Mochel 
8935d9464a4SPatrick Mochel 	if (acpi_drv) {
89446ec8598SBjorn Helgaas 		if (acpi_drv->ops.notify)
89546ec8598SBjorn Helgaas 			acpi_device_remove_notify_handler(acpi_dev);
8965d9464a4SPatrick Mochel 		if (acpi_drv->ops.remove)
89751fac838SRafael J. Wysocki 			acpi_drv->ops.remove(acpi_dev);
8985d9464a4SPatrick Mochel 	}
8995d9464a4SPatrick Mochel 	acpi_dev->driver = NULL;
900db89b4f0SPavel Machek 	acpi_dev->driver_data = NULL;
9015d9464a4SPatrick Mochel 
9025d9464a4SPatrick Mochel 	put_device(dev);
9035d9464a4SPatrick Mochel 	return 0;
9045d9464a4SPatrick Mochel }
9055d9464a4SPatrick Mochel 
90655955aadSDavid Brownell struct bus_type acpi_bus_type = {
9079e89dde2SZhang Rui 	.name		= "acpi",
9085d9464a4SPatrick Mochel 	.match		= acpi_bus_match,
9095d9464a4SPatrick Mochel 	.probe		= acpi_device_probe,
9105d9464a4SPatrick Mochel 	.remove		= acpi_device_remove,
9115d9464a4SPatrick Mochel 	.uevent		= acpi_device_uevent,
9121da177e4SLinus Torvalds };
9131da177e4SLinus Torvalds 
914*d783156eSRafael J. Wysocki static void acpi_device_del(struct acpi_device *device)
915caf5c03fSRafael J. Wysocki {
916*d783156eSRafael J. Wysocki 	mutex_lock(&acpi_device_lock);
917*d783156eSRafael J. Wysocki 	if (device->parent)
918*d783156eSRafael J. Wysocki 		list_del(&device->node);
919*d783156eSRafael J. Wysocki 
920*d783156eSRafael J. Wysocki 	list_del(&device->wakeup_list);
921*d783156eSRafael J. Wysocki 	mutex_unlock(&acpi_device_lock);
922*d783156eSRafael J. Wysocki 
923*d783156eSRafael J. Wysocki 	acpi_power_add_remove_device(device, false);
924*d783156eSRafael J. Wysocki 	acpi_device_remove_files(device);
925*d783156eSRafael J. Wysocki 	if (device->remove)
926*d783156eSRafael J. Wysocki 		device->remove(device);
927*d783156eSRafael J. Wysocki 
928*d783156eSRafael J. Wysocki 	device_del(&device->dev);
929*d783156eSRafael J. Wysocki }
930*d783156eSRafael J. Wysocki 
931*d783156eSRafael J. Wysocki static LIST_HEAD(acpi_device_del_list);
932*d783156eSRafael J. Wysocki static DEFINE_MUTEX(acpi_device_del_lock);
933*d783156eSRafael J. Wysocki 
934*d783156eSRafael J. Wysocki static void acpi_device_del_work_fn(struct work_struct *work_not_used)
935*d783156eSRafael J. Wysocki {
936*d783156eSRafael J. Wysocki 	for (;;) {
937*d783156eSRafael J. Wysocki 		struct acpi_device *adev;
938*d783156eSRafael J. Wysocki 
939*d783156eSRafael J. Wysocki 		mutex_lock(&acpi_device_del_lock);
940*d783156eSRafael J. Wysocki 
941*d783156eSRafael J. Wysocki 		if (list_empty(&acpi_device_del_list)) {
942*d783156eSRafael J. Wysocki 			mutex_unlock(&acpi_device_del_lock);
943*d783156eSRafael J. Wysocki 			break;
944*d783156eSRafael J. Wysocki 		}
945*d783156eSRafael J. Wysocki 		adev = list_first_entry(&acpi_device_del_list,
946*d783156eSRafael J. Wysocki 					struct acpi_device, del_list);
947*d783156eSRafael J. Wysocki 		list_del(&adev->del_list);
948*d783156eSRafael J. Wysocki 
949*d783156eSRafael J. Wysocki 		mutex_unlock(&acpi_device_del_lock);
950*d783156eSRafael J. Wysocki 
951*d783156eSRafael J. Wysocki 		acpi_device_del(adev);
952*d783156eSRafael J. Wysocki 		/*
953*d783156eSRafael J. Wysocki 		 * Drop references to all power resources that might have been
954*d783156eSRafael J. Wysocki 		 * used by the device.
955*d783156eSRafael J. Wysocki 		 */
956*d783156eSRafael J. Wysocki 		acpi_power_transition(adev, ACPI_STATE_D3_COLD);
957*d783156eSRafael J. Wysocki 		put_device(&adev->dev);
958*d783156eSRafael J. Wysocki 	}
959*d783156eSRafael J. Wysocki }
960*d783156eSRafael J. Wysocki 
961*d783156eSRafael J. Wysocki /**
962*d783156eSRafael J. Wysocki  * acpi_scan_drop_device - Drop an ACPI device object.
963*d783156eSRafael J. Wysocki  * @handle: Handle of an ACPI namespace node, not used.
964*d783156eSRafael J. Wysocki  * @context: Address of the ACPI device object to drop.
965*d783156eSRafael J. Wysocki  *
966*d783156eSRafael J. Wysocki  * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
967*d783156eSRafael J. Wysocki  * namespace node the device object pointed to by @context is attached to.
968*d783156eSRafael J. Wysocki  *
969*d783156eSRafael J. Wysocki  * The unregistration is carried out asynchronously to avoid running
970*d783156eSRafael J. Wysocki  * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
971*d783156eSRafael J. Wysocki  * ensure the correct ordering (the device objects must be unregistered in the
972*d783156eSRafael J. Wysocki  * same order in which the corresponding namespace nodes are deleted).
973*d783156eSRafael J. Wysocki  */
974*d783156eSRafael J. Wysocki static void acpi_scan_drop_device(acpi_handle handle, void *context)
975*d783156eSRafael J. Wysocki {
976*d783156eSRafael J. Wysocki 	static DECLARE_WORK(work, acpi_device_del_work_fn);
977*d783156eSRafael J. Wysocki 	struct acpi_device *adev = context;
978*d783156eSRafael J. Wysocki 
979*d783156eSRafael J. Wysocki 	mutex_lock(&acpi_device_del_lock);
980*d783156eSRafael J. Wysocki 
981*d783156eSRafael J. Wysocki 	/*
982*d783156eSRafael J. Wysocki 	 * Use the ACPI hotplug workqueue which is ordered, so this work item
983*d783156eSRafael J. Wysocki 	 * won't run after any hotplug work items submitted subsequently.  That
984*d783156eSRafael J. Wysocki 	 * prevents attempts to register device objects identical to those being
985*d783156eSRafael J. Wysocki 	 * deleted from happening concurrently (such attempts result from
986*d783156eSRafael J. Wysocki 	 * hotplug events handled via the ACPI hotplug workqueue).  It also will
987*d783156eSRafael J. Wysocki 	 * run after all of the work items submitted previosuly, which helps
988*d783156eSRafael J. Wysocki 	 * those work items to ensure that they are not accessing stale device
989*d783156eSRafael J. Wysocki 	 * objects.
990*d783156eSRafael J. Wysocki 	 */
991*d783156eSRafael J. Wysocki 	if (list_empty(&acpi_device_del_list))
992*d783156eSRafael J. Wysocki 		acpi_queue_hotplug_work(&work);
993*d783156eSRafael J. Wysocki 
994*d783156eSRafael J. Wysocki 	list_add_tail(&adev->del_list, &acpi_device_del_list);
995*d783156eSRafael J. Wysocki 	/* Make acpi_ns_validate_handle() return NULL for this handle. */
996*d783156eSRafael J. Wysocki 	adev->handle = INVALID_ACPI_HANDLE;
997*d783156eSRafael J. Wysocki 
998*d783156eSRafael J. Wysocki 	mutex_unlock(&acpi_device_del_lock);
999caf5c03fSRafael J. Wysocki }
1000caf5c03fSRafael J. Wysocki 
1001caf5c03fSRafael J. Wysocki int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
1002caf5c03fSRafael J. Wysocki {
1003caf5c03fSRafael J. Wysocki 	acpi_status status;
1004caf5c03fSRafael J. Wysocki 
1005caf5c03fSRafael J. Wysocki 	if (!device)
1006caf5c03fSRafael J. Wysocki 		return -EINVAL;
1007caf5c03fSRafael J. Wysocki 
1008*d783156eSRafael J. Wysocki 	status = acpi_get_data(handle, acpi_scan_drop_device, (void **)device);
1009caf5c03fSRafael J. Wysocki 	if (ACPI_FAILURE(status) || !*device) {
1010caf5c03fSRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
1011caf5c03fSRafael J. Wysocki 				  handle));
1012caf5c03fSRafael J. Wysocki 		return -ENODEV;
1013caf5c03fSRafael J. Wysocki 	}
1014caf5c03fSRafael J. Wysocki 	return 0;
1015caf5c03fSRafael J. Wysocki }
10166585925bSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_get_device);
1017caf5c03fSRafael J. Wysocki 
1018cf860be6SRafael J. Wysocki int acpi_device_add(struct acpi_device *device,
101982c7d5efSRafael J. Wysocki 		    void (*release)(struct device *))
10201da177e4SLinus Torvalds {
1021e49bd2ddSZhang Rui 	int result;
1022e49bd2ddSZhang Rui 	struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1023e49bd2ddSZhang Rui 	int found = 0;
102466b7ed40SBjorn Helgaas 
1025d43e167dSRafael J. Wysocki 	if (device->handle) {
1026d43e167dSRafael J. Wysocki 		acpi_status status;
1027d43e167dSRafael J. Wysocki 
1028*d783156eSRafael J. Wysocki 		status = acpi_attach_data(device->handle, acpi_scan_drop_device,
1029d43e167dSRafael J. Wysocki 					  device);
1030d43e167dSRafael J. Wysocki 		if (ACPI_FAILURE(status)) {
1031d43e167dSRafael J. Wysocki 			acpi_handle_err(device->handle,
1032d43e167dSRafael J. Wysocki 					"Unable to attach device data\n");
1033d43e167dSRafael J. Wysocki 			return -ENODEV;
1034d43e167dSRafael J. Wysocki 		}
1035d43e167dSRafael J. Wysocki 	}
1036d43e167dSRafael J. Wysocki 
10371da177e4SLinus Torvalds 	/*
10381da177e4SLinus Torvalds 	 * Linkage
10391da177e4SLinus Torvalds 	 * -------
10401da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
10411da177e4SLinus Torvalds 	 */
10421da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->children);
10431da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->node);
10441da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
10451033f904SLan Tianyu 	INIT_LIST_HEAD(&device->physical_node_list);
1046*d783156eSRafael J. Wysocki 	INIT_LIST_HEAD(&device->del_list);
10471033f904SLan Tianyu 	mutex_init(&device->physical_node_lock);
10481da177e4SLinus Torvalds 
1049e49bd2ddSZhang Rui 	new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1050e49bd2ddSZhang Rui 	if (!new_bus_id) {
1051d43e167dSRafael J. Wysocki 		pr_err(PREFIX "Memory allocation error\n");
1052d43e167dSRafael J. Wysocki 		result = -ENOMEM;
1053d43e167dSRafael J. Wysocki 		goto err_detach;
1054e49bd2ddSZhang Rui 	}
1055e49bd2ddSZhang Rui 
10569090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
1057e49bd2ddSZhang Rui 	/*
1058e49bd2ddSZhang Rui 	 * Find suitable bus_id and instance number in acpi_bus_id_list
1059e49bd2ddSZhang Rui 	 * If failed, create one and link it into acpi_bus_id_list
1060e49bd2ddSZhang Rui 	 */
1061e49bd2ddSZhang Rui 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
10621131b938SBjorn Helgaas 		if (!strcmp(acpi_device_bus_id->bus_id,
10631131b938SBjorn Helgaas 			    acpi_device_hid(device))) {
1064e49bd2ddSZhang Rui 			acpi_device_bus_id->instance_no++;
1065e49bd2ddSZhang Rui 			found = 1;
1066e49bd2ddSZhang Rui 			kfree(new_bus_id);
1067e49bd2ddSZhang Rui 			break;
1068e49bd2ddSZhang Rui 		}
1069e49bd2ddSZhang Rui 	}
1070e49bd2ddSZhang Rui 	if (!found) {
1071e49bd2ddSZhang Rui 		acpi_device_bus_id = new_bus_id;
10721131b938SBjorn Helgaas 		strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
1073e49bd2ddSZhang Rui 		acpi_device_bus_id->instance_no = 0;
1074e49bd2ddSZhang Rui 		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1075e49bd2ddSZhang Rui 	}
10760794469dSKay Sievers 	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
1077e49bd2ddSZhang Rui 
107833b57150SLen Brown 	if (device->parent)
10791da177e4SLinus Torvalds 		list_add_tail(&device->node, &device->parent->children);
108033b57150SLen Brown 
10811da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
10821da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
10839090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
10841da177e4SLinus Torvalds 
10851890a97aSPatrick Mochel 	if (device->parent)
108666b7ed40SBjorn Helgaas 		device->dev.parent = &device->parent->dev;
10871890a97aSPatrick Mochel 	device->dev.bus = &acpi_bus_type;
108882c7d5efSRafael J. Wysocki 	device->dev.release = release;
1089cf860be6SRafael J. Wysocki 	result = device_add(&device->dev);
1090e49bd2ddSZhang Rui 	if (result) {
10918b12b922SAlex Chiang 		dev_err(&device->dev, "Error registering device\n");
1092d43e167dSRafael J. Wysocki 		goto err;
1093e49bd2ddSZhang Rui 	}
1094f883d9dbSPatrick Mochel 
1095e49bd2ddSZhang Rui 	result = acpi_device_setup_files(device);
1096e49bd2ddSZhang Rui 	if (result)
10970794469dSKay Sievers 		printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
10980794469dSKay Sievers 		       dev_name(&device->dev));
1099e49bd2ddSZhang Rui 
1100e49bd2ddSZhang Rui 	return 0;
1101d43e167dSRafael J. Wysocki 
1102d43e167dSRafael J. Wysocki  err:
11039090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
110433b57150SLen Brown 	if (device->parent)
1105e49bd2ddSZhang Rui 		list_del(&device->node);
1106e49bd2ddSZhang Rui 	list_del(&device->wakeup_list);
11079090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
1108d43e167dSRafael J. Wysocki 
1109d43e167dSRafael J. Wysocki  err_detach:
1110*d783156eSRafael J. Wysocki 	acpi_detach_data(device->handle, acpi_scan_drop_device);
1111e49bd2ddSZhang Rui 	return result;
11121da177e4SLinus Torvalds }
11131da177e4SLinus Torvalds 
1114b17b537aSRafael J. Wysocki static void acpi_device_unregister(struct acpi_device *device)
11151da177e4SLinus Torvalds {
1116*d783156eSRafael J. Wysocki 	acpi_detach_data(device->handle, acpi_scan_drop_device);
1117*d783156eSRafael J. Wysocki 	acpi_device_del(device);
1118bc9b6407SRafael J. Wysocki 	/*
11190aa120a0SRafael J. Wysocki 	 * Transition the device to D3cold to drop the reference counts of all
11200aa120a0SRafael J. Wysocki 	 * power resources the device depends on and turn off the ones that have
11210aa120a0SRafael J. Wysocki 	 * no more references.
1122bc9b6407SRafael J. Wysocki 	 */
11230aa120a0SRafael J. Wysocki 	acpi_device_set_power(device, ACPI_STATE_D3_COLD);
11243757b948SRafael J. Wysocki 	device->handle = NULL;
1125cf860be6SRafael J. Wysocki 	put_device(&device->dev);
11261da177e4SLinus Torvalds }
11271da177e4SLinus Torvalds 
11289e89dde2SZhang Rui /* --------------------------------------------------------------------------
11299e89dde2SZhang Rui                                  Driver Management
11309e89dde2SZhang Rui    -------------------------------------------------------------------------- */
11311da177e4SLinus Torvalds /**
1132d758a8faSRandy Dunlap  * acpi_bus_register_driver - register a driver with the ACPI bus
1133d758a8faSRandy Dunlap  * @driver: driver being registered
1134d758a8faSRandy Dunlap  *
11351da177e4SLinus Torvalds  * Registers a driver with the ACPI bus.  Searches the namespace for all
11369d9f749bSBjorn Helgaas  * devices that match the driver's criteria and binds.  Returns zero for
11379d9f749bSBjorn Helgaas  * success or a negative error status for failure.
11381da177e4SLinus Torvalds  */
11394be44fcdSLen Brown int acpi_bus_register_driver(struct acpi_driver *driver)
11401da177e4SLinus Torvalds {
11411890a97aSPatrick Mochel 	int ret;
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	if (acpi_disabled)
1144d550d98dSPatrick Mochel 		return -ENODEV;
11451890a97aSPatrick Mochel 	driver->drv.name = driver->name;
11461890a97aSPatrick Mochel 	driver->drv.bus = &acpi_bus_type;
11471890a97aSPatrick Mochel 	driver->drv.owner = driver->owner;
11481da177e4SLinus Torvalds 
11491890a97aSPatrick Mochel 	ret = driver_register(&driver->drv);
11501890a97aSPatrick Mochel 	return ret;
11511da177e4SLinus Torvalds }
11521da177e4SLinus Torvalds 
11534be44fcdSLen Brown EXPORT_SYMBOL(acpi_bus_register_driver);
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds /**
1156b27b14ceSHanjun Guo  * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
1157d758a8faSRandy Dunlap  * @driver: driver to unregister
1158d758a8faSRandy Dunlap  *
11591da177e4SLinus Torvalds  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
11601da177e4SLinus Torvalds  * devices that match the driver's criteria and unbinds.
11611da177e4SLinus Torvalds  */
116206ea8e08SBjorn Helgaas void acpi_bus_unregister_driver(struct acpi_driver *driver)
11631da177e4SLinus Torvalds {
11641890a97aSPatrick Mochel 	driver_unregister(&driver->drv);
11651da177e4SLinus Torvalds }
11664be44fcdSLen Brown 
11671da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_unregister_driver);
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds /* --------------------------------------------------------------------------
11701da177e4SLinus Torvalds                                  Device Enumeration
11711da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
11725c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
11735c478f49SBjorn Helgaas {
1174456de893SRafael J. Wysocki 	struct acpi_device *device = NULL;
11755c478f49SBjorn Helgaas 	acpi_status status;
11765c478f49SBjorn Helgaas 
11775c478f49SBjorn Helgaas 	/*
11785c478f49SBjorn Helgaas 	 * Fixed hardware devices do not appear in the namespace and do not
11795c478f49SBjorn Helgaas 	 * have handles, but we fabricate acpi_devices for them, so we have
11805c478f49SBjorn Helgaas 	 * to deal with them specially.
11815c478f49SBjorn Helgaas 	 */
1182456de893SRafael J. Wysocki 	if (!handle)
11835c478f49SBjorn Helgaas 		return acpi_root;
11845c478f49SBjorn Helgaas 
11855c478f49SBjorn Helgaas 	do {
11865c478f49SBjorn Helgaas 		status = acpi_get_parent(handle, &handle);
11875c478f49SBjorn Helgaas 		if (ACPI_FAILURE(status))
1188456de893SRafael J. Wysocki 			return status == AE_NULL_ENTRY ? NULL : acpi_root;
1189456de893SRafael J. Wysocki 	} while (acpi_bus_get_device(handle, &device));
11905c478f49SBjorn Helgaas 	return device;
11915c478f49SBjorn Helgaas }
11925c478f49SBjorn Helgaas 
1193c8f7a62cSLen Brown acpi_status
1194c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1195c8f7a62cSLen Brown {
1196c8f7a62cSLen Brown 	acpi_status status;
1197c8f7a62cSLen Brown 	acpi_handle tmp;
1198c8f7a62cSLen Brown 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1199c8f7a62cSLen Brown 	union acpi_object *obj;
1200c8f7a62cSLen Brown 
1201c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_EJD", &tmp);
1202c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
1203c8f7a62cSLen Brown 		return status;
1204c8f7a62cSLen Brown 
1205c8f7a62cSLen Brown 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1206c8f7a62cSLen Brown 	if (ACPI_SUCCESS(status)) {
1207c8f7a62cSLen Brown 		obj = buffer.pointer;
12083b5fee59SHolger Macht 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
12093b5fee59SHolger Macht 					 ejd);
1210c8f7a62cSLen Brown 		kfree(buffer.pointer);
1211c8f7a62cSLen Brown 	}
1212c8f7a62cSLen Brown 	return status;
1213c8f7a62cSLen Brown }
1214c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1215c8f7a62cSLen Brown 
1216e88c9c60SRafael J. Wysocki static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1217b581a7f9SRafael J. Wysocki 					struct acpi_device_wakeup *wakeup)
12181da177e4SLinus Torvalds {
1219b581a7f9SRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1220b581a7f9SRafael J. Wysocki 	union acpi_object *package = NULL;
12211da177e4SLinus Torvalds 	union acpi_object *element = NULL;
1222b581a7f9SRafael J. Wysocki 	acpi_status status;
1223e88c9c60SRafael J. Wysocki 	int err = -ENODATA;
12241da177e4SLinus Torvalds 
1225b581a7f9SRafael J. Wysocki 	if (!wakeup)
1226e88c9c60SRafael J. Wysocki 		return -EINVAL;
12271da177e4SLinus Torvalds 
1228993cbe59SRafael J. Wysocki 	INIT_LIST_HEAD(&wakeup->resources);
12291da177e4SLinus Torvalds 
1230b581a7f9SRafael J. Wysocki 	/* _PRW */
1231b581a7f9SRafael J. Wysocki 	status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1232b581a7f9SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
1233b581a7f9SRafael J. Wysocki 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
1234e88c9c60SRafael J. Wysocki 		return err;
1235b581a7f9SRafael J. Wysocki 	}
1236b581a7f9SRafael J. Wysocki 
1237b581a7f9SRafael J. Wysocki 	package = (union acpi_object *)buffer.pointer;
1238b581a7f9SRafael J. Wysocki 
1239e88c9c60SRafael J. Wysocki 	if (!package || package->package.count < 2)
1240b581a7f9SRafael J. Wysocki 		goto out;
1241b581a7f9SRafael J. Wysocki 
12421da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
1243e88c9c60SRafael J. Wysocki 	if (!element)
1244b581a7f9SRafael J. Wysocki 		goto out;
1245e88c9c60SRafael J. Wysocki 
12461da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
12471da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
12481da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
12491da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
1250e88c9c60SRafael J. Wysocki 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
1251b581a7f9SRafael J. Wysocki 			goto out;
1252e88c9c60SRafael J. Wysocki 
1253b581a7f9SRafael J. Wysocki 		wakeup->gpe_device =
12541da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
1255b581a7f9SRafael J. Wysocki 		wakeup->gpe_number =
12561da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
12571da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
1258b581a7f9SRafael J. Wysocki 		wakeup->gpe_device = NULL;
1259b581a7f9SRafael J. Wysocki 		wakeup->gpe_number = element->integer.value;
1260b581a7f9SRafael J. Wysocki 	} else {
1261b581a7f9SRafael J. Wysocki 		goto out;
1262b581a7f9SRafael J. Wysocki 	}
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
1265e88c9c60SRafael J. Wysocki 	if (element->type != ACPI_TYPE_INTEGER)
1266b581a7f9SRafael J. Wysocki 		goto out;
1267e88c9c60SRafael J. Wysocki 
1268b581a7f9SRafael J. Wysocki 	wakeup->sleep_state = element->integer.value;
12691da177e4SLinus Torvalds 
1270e88c9c60SRafael J. Wysocki 	err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1271e88c9c60SRafael J. Wysocki 	if (err)
1272b581a7f9SRafael J. Wysocki 		goto out;
12731da177e4SLinus Torvalds 
12740596a52bSRafael J. Wysocki 	if (!list_empty(&wakeup->resources)) {
12750596a52bSRafael J. Wysocki 		int sleep_state;
12769874647bSRafael J. Wysocki 
1277b5d667ebSRafael J. Wysocki 		err = acpi_power_wakeup_list_init(&wakeup->resources,
1278b5d667ebSRafael J. Wysocki 						  &sleep_state);
1279b5d667ebSRafael J. Wysocki 		if (err) {
1280b5d667ebSRafael J. Wysocki 			acpi_handle_warn(handle, "Retrieving current states "
1281b5d667ebSRafael J. Wysocki 					 "of wakeup power resources failed\n");
1282b5d667ebSRafael J. Wysocki 			acpi_power_resources_list_free(&wakeup->resources);
1283b5d667ebSRafael J. Wysocki 			goto out;
1284b5d667ebSRafael J. Wysocki 		}
12850596a52bSRafael J. Wysocki 		if (sleep_state < wakeup->sleep_state) {
12860596a52bSRafael J. Wysocki 			acpi_handle_warn(handle, "Overriding _PRW sleep state "
12870596a52bSRafael J. Wysocki 					 "(S%d) by S%d from power resources\n",
12880596a52bSRafael J. Wysocki 					 (int)wakeup->sleep_state, sleep_state);
12890596a52bSRafael J. Wysocki 			wakeup->sleep_state = sleep_state;
12900596a52bSRafael J. Wysocki 		}
12910596a52bSRafael J. Wysocki 	}
1292bba63a29SLin Ming 	acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
1293b581a7f9SRafael J. Wysocki 
1294b581a7f9SRafael J. Wysocki  out:
1295b581a7f9SRafael J. Wysocki 	kfree(buffer.pointer);
1296e88c9c60SRafael J. Wysocki 	return err;
12971da177e4SLinus Torvalds }
12981da177e4SLinus Torvalds 
1299f517709dSRafael J. Wysocki static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
13001da177e4SLinus Torvalds {
130129b71a1cSThomas Renninger 	struct acpi_device_id button_device_ids[] = {
130229b71a1cSThomas Renninger 		{"PNP0C0C", 0},
1303b7e38304SZhang Rui 		{"PNP0C0D", 0},
130429b71a1cSThomas Renninger 		{"PNP0C0E", 0},
130529b71a1cSThomas Renninger 		{"", 0},
130629b71a1cSThomas Renninger 	};
1307f517709dSRafael J. Wysocki 	acpi_status status;
1308f517709dSRafael J. Wysocki 	acpi_event_status event_status;
1309f517709dSRafael J. Wysocki 
1310b67ea761SRafael J. Wysocki 	device->wakeup.flags.notifier_present = 0;
1311f517709dSRafael J. Wysocki 
1312f517709dSRafael J. Wysocki 	/* Power button, Lid switch always enable wakeup */
1313f517709dSRafael J. Wysocki 	if (!acpi_match_device_ids(device, button_device_ids)) {
1314f517709dSRafael J. Wysocki 		device->wakeup.flags.run_wake = 1;
1315b7e38304SZhang Rui 		if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1316b7e38304SZhang Rui 			/* Do not use Lid/sleep button for S5 wakeup */
1317b7e38304SZhang Rui 			if (device->wakeup.sleep_state == ACPI_STATE_S5)
1318b7e38304SZhang Rui 				device->wakeup.sleep_state = ACPI_STATE_S4;
1319b7e38304SZhang Rui 		}
1320f2b56bc8SRafael J. Wysocki 		device_set_wakeup_capable(&device->dev, true);
1321f517709dSRafael J. Wysocki 		return;
1322f517709dSRafael J. Wysocki 	}
1323f517709dSRafael J. Wysocki 
1324e8e18c95SRafael J. Wysocki 	status = acpi_get_gpe_status(device->wakeup.gpe_device,
1325e8e18c95SRafael J. Wysocki 					device->wakeup.gpe_number,
13260f849d2cSLin Ming 						&event_status);
1327f517709dSRafael J. Wysocki 	if (status == AE_OK)
1328f517709dSRafael J. Wysocki 		device->wakeup.flags.run_wake =
1329f517709dSRafael J. Wysocki 				!!(event_status & ACPI_EVENT_FLAG_HANDLE);
1330f517709dSRafael J. Wysocki }
1331f517709dSRafael J. Wysocki 
1332d57d09a4SRafael J. Wysocki static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1333f517709dSRafael J. Wysocki {
1334e88c9c60SRafael J. Wysocki 	int err;
133529b71a1cSThomas Renninger 
1336d57d09a4SRafael J. Wysocki 	/* Presence of _PRW indicates wake capable */
1337952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PRW"))
1338d57d09a4SRafael J. Wysocki 		return;
1339d57d09a4SRafael J. Wysocki 
1340e88c9c60SRafael J. Wysocki 	err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1341b581a7f9SRafael J. Wysocki 							   &device->wakeup);
1342e88c9c60SRafael J. Wysocki 	if (err) {
1343e88c9c60SRafael J. Wysocki 		dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1344d57d09a4SRafael J. Wysocki 		return;
13451da177e4SLinus Torvalds 	}
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 	device->wakeup.flags.valid = 1;
13489b83ccd2SRafael J. Wysocki 	device->wakeup.prepare_count = 0;
1349f517709dSRafael J. Wysocki 	acpi_bus_set_run_wake_flags(device);
1350729b2bdbSZhao Yakui 	/* Call _PSW/_DSW object to disable its ability to wake the sleeping
1351729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
1352729b2bdbSZhao Yakui 	 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1353729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
1354729b2bdbSZhao Yakui 	 * present will the _PSW object used.
1355729b2bdbSZhao Yakui 	 */
1356e88c9c60SRafael J. Wysocki 	err = acpi_device_sleep_wake(device, 0, 0, 0);
1357e88c9c60SRafael J. Wysocki 	if (err)
135877e76609SRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
135977e76609SRafael J. Wysocki 				"error in _DSW or _PSW evaluation\n"));
13601da177e4SLinus Torvalds }
13611da177e4SLinus Torvalds 
1362f33ce568SRafael J. Wysocki static void acpi_bus_init_power_state(struct acpi_device *device, int state)
13631da177e4SLinus Torvalds {
1364f33ce568SRafael J. Wysocki 	struct acpi_device_power_state *ps = &device->power.states[state];
1365ef85bdbeSRafael J. Wysocki 	char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1366ef85bdbeSRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1367f33ce568SRafael J. Wysocki 	acpi_status status;
13689e89dde2SZhang Rui 
1369f33ce568SRafael J. Wysocki 	INIT_LIST_HEAD(&ps->resources);
1370f33ce568SRafael J. Wysocki 
1371ef85bdbeSRafael J. Wysocki 	/* Evaluate "_PRx" to get referenced power resources */
1372ef85bdbeSRafael J. Wysocki 	status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1373ef85bdbeSRafael J. Wysocki 	if (ACPI_SUCCESS(status)) {
1374ef85bdbeSRafael J. Wysocki 		union acpi_object *package = buffer.pointer;
1375f33ce568SRafael J. Wysocki 
1376ef85bdbeSRafael J. Wysocki 		if (buffer.length && package
1377ef85bdbeSRafael J. Wysocki 		    && package->type == ACPI_TYPE_PACKAGE
1378ef85bdbeSRafael J. Wysocki 		    && package->package.count) {
1379e88c9c60SRafael J. Wysocki 			int err = acpi_extract_power_resources(package, 0,
1380ef85bdbeSRafael J. Wysocki 							       &ps->resources);
1381e88c9c60SRafael J. Wysocki 			if (!err)
1382f33ce568SRafael J. Wysocki 				device->power.flags.power_resources = 1;
1383f33ce568SRafael J. Wysocki 		}
1384ef85bdbeSRafael J. Wysocki 		ACPI_FREE(buffer.pointer);
1385f33ce568SRafael J. Wysocki 	}
1386f33ce568SRafael J. Wysocki 
1387f33ce568SRafael J. Wysocki 	/* Evaluate "_PSx" to see if we can do explicit sets */
1388ef85bdbeSRafael J. Wysocki 	pathname[2] = 'S';
1389952c63e9SJiang Liu 	if (acpi_has_method(device->handle, pathname))
1390f33ce568SRafael J. Wysocki 		ps->flags.explicit_set = 1;
1391f33ce568SRafael J. Wysocki 
1392f33ce568SRafael J. Wysocki 	/*
1393f33ce568SRafael J. Wysocki 	 * State is valid if there are means to put the device into it.
1394f33ce568SRafael J. Wysocki 	 * D3hot is only valid if _PR3 present.
1395f33ce568SRafael J. Wysocki 	 */
1396ef85bdbeSRafael J. Wysocki 	if (!list_empty(&ps->resources)
1397f33ce568SRafael J. Wysocki 	    || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1398f33ce568SRafael J. Wysocki 		ps->flags.valid = 1;
1399f33ce568SRafael J. Wysocki 		ps->flags.os_accessible = 1;
1400f33ce568SRafael J. Wysocki 	}
1401f33ce568SRafael J. Wysocki 
1402f33ce568SRafael J. Wysocki 	ps->power = -1;		/* Unknown - driver assigned */
1403f33ce568SRafael J. Wysocki 	ps->latency = -1;	/* Unknown - driver assigned */
1404f33ce568SRafael J. Wysocki }
1405f33ce568SRafael J. Wysocki 
1406d43e167dSRafael J. Wysocki static void acpi_bus_get_power_flags(struct acpi_device *device)
14071da177e4SLinus Torvalds {
14088bc5053bSRafael J. Wysocki 	u32 i;
14091da177e4SLinus Torvalds 
1410d43e167dSRafael J. Wysocki 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
1411952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PS0") &&
1412952c63e9SJiang Liu 	    !acpi_has_method(device->handle, "_PR0"))
1413d43e167dSRafael J. Wysocki 		return;
1414d43e167dSRafael J. Wysocki 
1415d43e167dSRafael J. Wysocki 	device->flags.power_manageable = 1;
14161da177e4SLinus Torvalds 
14171da177e4SLinus Torvalds 	/*
14189e89dde2SZhang Rui 	 * Power Management Flags
14191da177e4SLinus Torvalds 	 */
1420952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_PSC"))
14219e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
1422952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_IRC"))
14239e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 	/*
14269e89dde2SZhang Rui 	 * Enumerate supported power management states
14271da177e4SLinus Torvalds 	 */
1428f33ce568SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1429f33ce568SRafael J. Wysocki 		acpi_bus_init_power_state(device, i);
14301da177e4SLinus Torvalds 
14310b224527SRafael J. Wysocki 	INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
14329e89dde2SZhang Rui 
14339e89dde2SZhang Rui 	/* Set defaults for D0 and D3 states (always valid) */
14349e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
14359e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
14368ad928d5SRafael J. Wysocki 	device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
14378ad928d5SRafael J. Wysocki 	device->power.states[ACPI_STATE_D3_COLD].power = 0;
14389e89dde2SZhang Rui 
14395c7dd710SRafael J. Wysocki 	/* Set D3cold's explicit_set flag if _PS3 exists. */
14405c7dd710SRafael J. Wysocki 	if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
14415c7dd710SRafael J. Wysocki 		device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
14425c7dd710SRafael J. Wysocki 
14431399dfcdSAaron Lu 	/* Presence of _PS3 or _PRx means we can put the device into D3 cold */
14441399dfcdSAaron Lu 	if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
14451399dfcdSAaron Lu 			device->power.flags.power_resources)
14461399dfcdSAaron Lu 		device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
14471399dfcdSAaron Lu 
1448b3785492SRafael J. Wysocki 	if (acpi_bus_init_power(device)) {
1449b3785492SRafael J. Wysocki 		acpi_free_power_resources_lists(device);
1450b3785492SRafael J. Wysocki 		device->flags.power_manageable = 0;
1451b3785492SRafael J. Wysocki 	}
14529e89dde2SZhang Rui }
1453c8f7a62cSLen Brown 
1454d43e167dSRafael J. Wysocki static void acpi_bus_get_flags(struct acpi_device *device)
14551da177e4SLinus Torvalds {
14561da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
1457952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_STA"))
14581da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
1461952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_RMV"))
14621da177e4SLinus Torvalds 		device->flags.removable = 1;
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
1465952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_EJD") ||
1466952c63e9SJiang Liu 	    acpi_has_method(device->handle, "_EJ0"))
14671da177e4SLinus Torvalds 		device->flags.ejectable = 1;
14681da177e4SLinus Torvalds }
14691da177e4SLinus Torvalds 
1470c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device)
14711da177e4SLinus Torvalds {
14721da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
14731da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
14741da177e4SLinus Torvalds 	int i = 0;
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 	/*
14771da177e4SLinus Torvalds 	 * Bus ID
14781da177e4SLinus Torvalds 	 * ------
14791da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
14801da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
14811da177e4SLinus Torvalds 	 */
1482859ac9a4SBjorn Helgaas 	if (ACPI_IS_ROOT_DEVICE(device)) {
14831da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
1484859ac9a4SBjorn Helgaas 		return;
1485859ac9a4SBjorn Helgaas 	}
1486859ac9a4SBjorn Helgaas 
1487859ac9a4SBjorn Helgaas 	switch (device->device_type) {
14881da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
14891da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
14901da177e4SLinus Torvalds 		break;
14911da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
14921da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
14931da177e4SLinus Torvalds 		break;
14941da177e4SLinus Torvalds 	default:
149566b7ed40SBjorn Helgaas 		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
14961da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
14971da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
14981da177e4SLinus Torvalds 			if (bus_id[i] == '_')
14991da177e4SLinus Torvalds 				bus_id[i] = '\0';
15001da177e4SLinus Torvalds 			else
15011da177e4SLinus Torvalds 				break;
15021da177e4SLinus Torvalds 		}
15031da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
15041da177e4SLinus Torvalds 		break;
15051da177e4SLinus Torvalds 	}
15061da177e4SLinus Torvalds }
15071da177e4SLinus Torvalds 
150854735266SZhang Rui /*
1509ebf4df8dSJiang Liu  * acpi_ata_match - see if an acpi object is an ATA device
1510ebf4df8dSJiang Liu  *
1511ebf4df8dSJiang Liu  * If an acpi object has one of the ACPI ATA methods defined,
1512ebf4df8dSJiang Liu  * then we can safely call it an ATA device.
1513ebf4df8dSJiang Liu  */
1514ebf4df8dSJiang Liu bool acpi_ata_match(acpi_handle handle)
1515ebf4df8dSJiang Liu {
1516ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_GTF") ||
1517ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_GTM") ||
1518ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_STM") ||
1519ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_SDD");
1520ebf4df8dSJiang Liu }
1521ebf4df8dSJiang Liu 
1522ebf4df8dSJiang Liu /*
1523d4e1a692SToshi Kani  * acpi_bay_match - see if an acpi object is an ejectable driver bay
152454735266SZhang Rui  *
152554735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
152654735266SZhang Rui  * then we can safely call it an ejectable drive bay
152754735266SZhang Rui  */
1528ebf4df8dSJiang Liu bool acpi_bay_match(acpi_handle handle)
1529d4e1a692SToshi Kani {
153054735266SZhang Rui 	acpi_handle phandle;
153154735266SZhang Rui 
1532952c63e9SJiang Liu 	if (!acpi_has_method(handle, "_EJ0"))
1533ebf4df8dSJiang Liu 		return false;
1534ebf4df8dSJiang Liu 	if (acpi_ata_match(handle))
1535ebf4df8dSJiang Liu 		return true;
1536ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1537ebf4df8dSJiang Liu 		return false;
153854735266SZhang Rui 
1539ebf4df8dSJiang Liu 	return acpi_ata_match(phandle);
154054735266SZhang Rui }
154154735266SZhang Rui 
15423620f2f2SFrank Seidel /*
1543d4e1a692SToshi Kani  * acpi_dock_match - see if an acpi object has a _DCK method
15443620f2f2SFrank Seidel  */
1545ebf4df8dSJiang Liu bool acpi_dock_match(acpi_handle handle)
15463620f2f2SFrank Seidel {
1547ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_DCK");
15483620f2f2SFrank Seidel }
15493620f2f2SFrank Seidel 
1550620e112cSThomas Renninger const char *acpi_device_hid(struct acpi_device *device)
155115b8dd53SBob Moore {
15527f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hid;
155315b8dd53SBob Moore 
15542b2ae7c7SThomas Renninger 	if (list_empty(&device->pnp.ids))
15552b2ae7c7SThomas Renninger 		return dummy_hid;
15562b2ae7c7SThomas Renninger 
15577f47fa6cSBjorn Helgaas 	hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
15587f47fa6cSBjorn Helgaas 	return hid->id;
15597f47fa6cSBjorn Helgaas }
15607f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid);
156115b8dd53SBob Moore 
1562d4e1a692SToshi Kani static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
15637f47fa6cSBjorn Helgaas {
15647f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
156515b8dd53SBob Moore 
15667f47fa6cSBjorn Helgaas 	id = kmalloc(sizeof(*id), GFP_KERNEL);
15677f47fa6cSBjorn Helgaas 	if (!id)
15687f47fa6cSBjorn Helgaas 		return;
156915b8dd53SBob Moore 
1570581de59eSThomas Meyer 	id->id = kstrdup(dev_id, GFP_KERNEL);
15717f47fa6cSBjorn Helgaas 	if (!id->id) {
15727f47fa6cSBjorn Helgaas 		kfree(id);
15737f47fa6cSBjorn Helgaas 		return;
157415b8dd53SBob Moore 	}
157515b8dd53SBob Moore 
1576d4e1a692SToshi Kani 	list_add_tail(&id->list, &pnp->ids);
1577d4e1a692SToshi Kani 	pnp->type.hardware_id = 1;
157815b8dd53SBob Moore }
157915b8dd53SBob Moore 
1580222e82acSDarrick J. Wong /*
1581222e82acSDarrick J. Wong  * Old IBM workstations have a DSDT bug wherein the SMBus object
1582222e82acSDarrick J. Wong  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1583222e82acSDarrick J. Wong  * prefix.  Work around this.
1584222e82acSDarrick J. Wong  */
1585ebf4df8dSJiang Liu static bool acpi_ibm_smbus_match(acpi_handle handle)
1586222e82acSDarrick J. Wong {
1587ebf4df8dSJiang Liu 	char node_name[ACPI_PATH_SEGMENT_LENGTH];
1588ebf4df8dSJiang Liu 	struct acpi_buffer path = { sizeof(node_name), node_name };
1589222e82acSDarrick J. Wong 
1590222e82acSDarrick J. Wong 	if (!dmi_name_in_vendors("IBM"))
1591ebf4df8dSJiang Liu 		return false;
1592222e82acSDarrick J. Wong 
1593222e82acSDarrick J. Wong 	/* Look for SMBS object */
1594ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1595ebf4df8dSJiang Liu 	    strcmp("SMBS", path.pointer))
1596ebf4df8dSJiang Liu 		return false;
1597222e82acSDarrick J. Wong 
1598222e82acSDarrick J. Wong 	/* Does it have the necessary (but misnamed) methods? */
1599952c63e9SJiang Liu 	if (acpi_has_method(handle, "SBI") &&
1600952c63e9SJiang Liu 	    acpi_has_method(handle, "SBR") &&
1601952c63e9SJiang Liu 	    acpi_has_method(handle, "SBW"))
1602ebf4df8dSJiang Liu 		return true;
1603ebf4df8dSJiang Liu 
1604ebf4df8dSJiang Liu 	return false;
1605222e82acSDarrick J. Wong }
1606222e82acSDarrick J. Wong 
1607c0af4175SToshi Kani static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1608c0af4175SToshi Kani 				int device_type)
16091da177e4SLinus Torvalds {
16101da177e4SLinus Torvalds 	acpi_status status;
161157f3674fSBjorn Helgaas 	struct acpi_device_info *info;
161278e25fefSLv Zheng 	struct acpi_pnp_device_id_list *cid_list;
16137f47fa6cSBjorn Helgaas 	int i;
16141da177e4SLinus Torvalds 
1615c0af4175SToshi Kani 	switch (device_type) {
16161da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
1617c0af4175SToshi Kani 		if (handle == ACPI_ROOT_OBJECT) {
1618c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SYSTEM_HID);
1619859ac9a4SBjorn Helgaas 			break;
1620859ac9a4SBjorn Helgaas 		}
1621859ac9a4SBjorn Helgaas 
1622c0af4175SToshi Kani 		status = acpi_get_object_info(handle, &info);
16231da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
1624c0af4175SToshi Kani 			pr_err(PREFIX "%s: Error reading device info\n",
1625c0af4175SToshi Kani 					__func__);
16261da177e4SLinus Torvalds 			return;
16271da177e4SLinus Torvalds 		}
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_HID)
1630c0af4175SToshi Kani 			acpi_add_id(pnp, info->hardware_id.string);
163157f3674fSBjorn Helgaas 		if (info->valid & ACPI_VALID_CID) {
163215b8dd53SBob Moore 			cid_list = &info->compatible_id_list;
163357f3674fSBjorn Helgaas 			for (i = 0; i < cid_list->count; i++)
1634c0af4175SToshi Kani 				acpi_add_id(pnp, cid_list->ids[i].string);
163557f3674fSBjorn Helgaas 		}
16361da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
1637c0af4175SToshi Kani 			pnp->bus_address = info->address;
1638c0af4175SToshi Kani 			pnp->type.bus_address = 1;
16391da177e4SLinus Torvalds 		}
1640ccf78040SLv Zheng 		if (info->valid & ACPI_VALID_UID)
1641c0af4175SToshi Kani 			pnp->unique_id = kstrdup(info->unique_id.string,
1642ccf78040SLv Zheng 							GFP_KERNEL);
1643ae843332SZhang Rui 
1644a83893aeSBjorn Helgaas 		kfree(info);
1645a83893aeSBjorn Helgaas 
164657f3674fSBjorn Helgaas 		/*
164757f3674fSBjorn Helgaas 		 * Some devices don't reliably have _HIDs & _CIDs, so add
164857f3674fSBjorn Helgaas 		 * synthetic HIDs to make sure drivers can find them.
16493620f2f2SFrank Seidel 		 */
1650c0af4175SToshi Kani 		if (acpi_is_video_device(handle))
1651c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_VIDEO_HID);
1652ebf4df8dSJiang Liu 		else if (acpi_bay_match(handle))
1653c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_BAY_HID);
1654ebf4df8dSJiang Liu 		else if (acpi_dock_match(handle))
1655c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_DOCK_HID);
1656ebf4df8dSJiang Liu 		else if (acpi_ibm_smbus_match(handle))
1657c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1658c0af4175SToshi Kani 		else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1659c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1660c0af4175SToshi Kani 			strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1661c0af4175SToshi Kani 			strcpy(pnp->device_class, ACPI_BUS_CLASS);
1662b7b30de5SBjorn Helgaas 		}
166354735266SZhang Rui 
16641da177e4SLinus Torvalds 		break;
16651da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
1666c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_POWER_HID);
16671da177e4SLinus Torvalds 		break;
16681da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
1669c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
16701da177e4SLinus Torvalds 		break;
16711da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
1672c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_THERMAL_HID);
16731da177e4SLinus Torvalds 		break;
16741da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
1675c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
16761da177e4SLinus Torvalds 		break;
16771da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
1678c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
16791da177e4SLinus Torvalds 		break;
16801da177e4SLinus Torvalds 	}
16811da177e4SLinus Torvalds }
16821da177e4SLinus Torvalds 
1683c0af4175SToshi Kani void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1684c0af4175SToshi Kani {
1685c0af4175SToshi Kani 	struct acpi_hardware_id *id, *tmp;
1686c0af4175SToshi Kani 
1687c0af4175SToshi Kani 	list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1688c0af4175SToshi Kani 		kfree(id->id);
1689c0af4175SToshi Kani 		kfree(id);
1690c0af4175SToshi Kani 	}
1691c0af4175SToshi Kani 	kfree(pnp->unique_id);
1692c0af4175SToshi Kani }
1693c0af4175SToshi Kani 
169482c7d5efSRafael J. Wysocki void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1695d43e167dSRafael J. Wysocki 			     int type, unsigned long long sta)
16961da177e4SLinus Torvalds {
1697d43e167dSRafael J. Wysocki 	INIT_LIST_HEAD(&device->pnp.ids);
1698d43e167dSRafael J. Wysocki 	device->device_type = type;
1699d43e167dSRafael J. Wysocki 	device->handle = handle;
1700d43e167dSRafael J. Wysocki 	device->parent = acpi_bus_get_parent(handle);
1701d43e167dSRafael J. Wysocki 	STRUCT_TO_INT(device->status) = sta;
1702d43e167dSRafael J. Wysocki 	acpi_device_get_busid(device);
1703c0af4175SToshi Kani 	acpi_set_pnp_ids(handle, &device->pnp, type);
1704d43e167dSRafael J. Wysocki 	acpi_bus_get_flags(device);
17052c0d4fe0SRafael J. Wysocki 	device->flags.match_driver = false;
1706cf860be6SRafael J. Wysocki 	device_initialize(&device->dev);
1707cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, true);
1708cf860be6SRafael J. Wysocki }
1709bc3b0772SBjorn Helgaas 
1710cf860be6SRafael J. Wysocki void acpi_device_add_finalize(struct acpi_device *device)
1711cf860be6SRafael J. Wysocki {
1712cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, false);
1713cf860be6SRafael J. Wysocki 	kobject_uevent(&device->dev.kobj, KOBJ_ADD);
17141da177e4SLinus Torvalds }
17151da177e4SLinus Torvalds 
17165c478f49SBjorn Helgaas static int acpi_add_single_object(struct acpi_device **child,
17175c478f49SBjorn Helgaas 				  acpi_handle handle, int type,
17182c0d4fe0SRafael J. Wysocki 				  unsigned long long sta)
17191da177e4SLinus Torvalds {
172077c24888SBjorn Helgaas 	int result;
172177c24888SBjorn Helgaas 	struct acpi_device *device;
172229aaefa6SBjorn Helgaas 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
17231da177e4SLinus Torvalds 
172436bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
17251da177e4SLinus Torvalds 	if (!device) {
17266468463aSLen Brown 		printk(KERN_ERR PREFIX "Memory allocation error\n");
1727d550d98dSPatrick Mochel 		return -ENOMEM;
17281da177e4SLinus Torvalds 	}
17291da177e4SLinus Torvalds 
1730d43e167dSRafael J. Wysocki 	acpi_init_device_object(device, handle, type, sta);
1731d43e167dSRafael J. Wysocki 	acpi_bus_get_power_flags(device);
1732d57d09a4SRafael J. Wysocki 	acpi_bus_get_wakeup_device_flags(device);
17331da177e4SLinus Torvalds 
1734cf860be6SRafael J. Wysocki 	result = acpi_device_add(device, acpi_device_release);
1735d43e167dSRafael J. Wysocki 	if (result) {
1736718fb0deSHugh Dickins 		acpi_device_release(&device->dev);
1737d550d98dSPatrick Mochel 		return result;
17381da177e4SLinus Torvalds 	}
17391da177e4SLinus Torvalds 
1740bc9b6407SRafael J. Wysocki 	acpi_power_add_remove_device(device, true);
1741cf860be6SRafael J. Wysocki 	acpi_device_add_finalize(device);
17421da177e4SLinus Torvalds 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1743d43e167dSRafael J. Wysocki 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1744d43e167dSRafael J. Wysocki 		dev_name(&device->dev), (char *) buffer.pointer,
1745d43e167dSRafael J. Wysocki 		device->parent ? dev_name(&device->parent->dev) : "(null)"));
17461da177e4SLinus Torvalds 	kfree(buffer.pointer);
17471da177e4SLinus Torvalds 	*child = device;
1748d43e167dSRafael J. Wysocki 	return 0;
1749bf325f95SRafael J. Wysocki }
1750bf325f95SRafael J. Wysocki 
1751778cbc1dSBjorn Helgaas static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1752778cbc1dSBjorn Helgaas 				    unsigned long long *sta)
1753778cbc1dSBjorn Helgaas {
1754778cbc1dSBjorn Helgaas 	acpi_status status;
1755778cbc1dSBjorn Helgaas 	acpi_object_type acpi_type;
1756778cbc1dSBjorn Helgaas 
1757778cbc1dSBjorn Helgaas 	status = acpi_get_type(handle, &acpi_type);
1758778cbc1dSBjorn Helgaas 	if (ACPI_FAILURE(status))
1759778cbc1dSBjorn Helgaas 		return -ENODEV;
1760778cbc1dSBjorn Helgaas 
1761778cbc1dSBjorn Helgaas 	switch (acpi_type) {
1762778cbc1dSBjorn Helgaas 	case ACPI_TYPE_ANY:		/* for ACPI_ROOT_OBJECT */
1763778cbc1dSBjorn Helgaas 	case ACPI_TYPE_DEVICE:
1764778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_DEVICE;
1765778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1766778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1767778cbc1dSBjorn Helgaas 			return -ENODEV;
1768778cbc1dSBjorn Helgaas 		break;
1769778cbc1dSBjorn Helgaas 	case ACPI_TYPE_PROCESSOR:
1770778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_PROCESSOR;
1771778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1772778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1773778cbc1dSBjorn Helgaas 			return -ENODEV;
1774778cbc1dSBjorn Helgaas 		break;
1775778cbc1dSBjorn Helgaas 	case ACPI_TYPE_THERMAL:
1776778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_THERMAL;
1777778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1778778cbc1dSBjorn Helgaas 		break;
1779778cbc1dSBjorn Helgaas 	case ACPI_TYPE_POWER:
1780778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_POWER;
1781778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1782778cbc1dSBjorn Helgaas 		break;
1783778cbc1dSBjorn Helgaas 	default:
1784778cbc1dSBjorn Helgaas 		return -ENODEV;
1785778cbc1dSBjorn Helgaas 	}
1786778cbc1dSBjorn Helgaas 
1787778cbc1dSBjorn Helgaas 	return 0;
1788778cbc1dSBjorn Helgaas }
1789778cbc1dSBjorn Helgaas 
17904b59cc1fSRafael J. Wysocki static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
17914b59cc1fSRafael J. Wysocki 				       char *idstr,
1792c5698074SRafael J. Wysocki 				       const struct acpi_device_id **matchid)
1793c5698074SRafael J. Wysocki {
1794c5698074SRafael J. Wysocki 	const struct acpi_device_id *devid;
1795c5698074SRafael J. Wysocki 
1796c5698074SRafael J. Wysocki 	for (devid = handler->ids; devid->id[0]; devid++)
1797c5698074SRafael J. Wysocki 		if (!strcmp((char *)devid->id, idstr)) {
1798c5698074SRafael J. Wysocki 			if (matchid)
1799c5698074SRafael J. Wysocki 				*matchid = devid;
1800c5698074SRafael J. Wysocki 
18014b59cc1fSRafael J. Wysocki 			return true;
18024b59cc1fSRafael J. Wysocki 		}
18034b59cc1fSRafael J. Wysocki 
18044b59cc1fSRafael J. Wysocki 	return false;
18054b59cc1fSRafael J. Wysocki }
18064b59cc1fSRafael J. Wysocki 
18076b772e8fSToshi Kani static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
18086b772e8fSToshi Kani 					const struct acpi_device_id **matchid)
18096b772e8fSToshi Kani {
18106b772e8fSToshi Kani 	struct acpi_scan_handler *handler;
18116b772e8fSToshi Kani 
18126b772e8fSToshi Kani 	list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
18136b772e8fSToshi Kani 		if (acpi_scan_handler_matching(handler, idstr, matchid))
18146b772e8fSToshi Kani 			return handler;
18156b772e8fSToshi Kani 
18166b772e8fSToshi Kani 	return NULL;
18176b772e8fSToshi Kani }
18186b772e8fSToshi Kani 
18193f8055c3SRafael J. Wysocki void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
18203f8055c3SRafael J. Wysocki {
18213f8055c3SRafael J. Wysocki 	if (!!hotplug->enabled == !!val)
18223f8055c3SRafael J. Wysocki 		return;
18233f8055c3SRafael J. Wysocki 
18243f8055c3SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
18253f8055c3SRafael J. Wysocki 
18263f8055c3SRafael J. Wysocki 	hotplug->enabled = val;
18273f8055c3SRafael J. Wysocki 
18283f8055c3SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
18293f8055c3SRafael J. Wysocki }
18303f8055c3SRafael J. Wysocki 
18316b772e8fSToshi Kani static void acpi_scan_init_hotplug(acpi_handle handle, int type)
18324b59cc1fSRafael J. Wysocki {
18336b772e8fSToshi Kani 	struct acpi_device_pnp pnp = {};
18346b772e8fSToshi Kani 	struct acpi_hardware_id *hwid;
18354b59cc1fSRafael J. Wysocki 	struct acpi_scan_handler *handler;
18364b59cc1fSRafael J. Wysocki 
18376b772e8fSToshi Kani 	INIT_LIST_HEAD(&pnp.ids);
18386b772e8fSToshi Kani 	acpi_set_pnp_ids(handle, &pnp, type);
18394b59cc1fSRafael J. Wysocki 
18406b772e8fSToshi Kani 	if (!pnp.type.hardware_id)
18417a26b530SCatalin Marinas 		goto out;
1842a33ec399SRafael J. Wysocki 
1843a33ec399SRafael J. Wysocki 	/*
1844a33ec399SRafael J. Wysocki 	 * This relies on the fact that acpi_install_notify_handler() will not
1845a33ec399SRafael J. Wysocki 	 * install the same notify handler routine twice for the same handle.
1846a33ec399SRafael J. Wysocki 	 */
18476b772e8fSToshi Kani 	list_for_each_entry(hwid, &pnp.ids, list) {
18486b772e8fSToshi Kani 		handler = acpi_scan_match_handler(hwid->id, NULL);
1849ca499fc8SToshi Kani 		if (handler && !handler->hotplug.ignore) {
18502cbb14fbSToshi Kani 			acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
18512cbb14fbSToshi Kani 					acpi_hotplug_notify_cb, handler);
18526b772e8fSToshi Kani 			break;
18536b772e8fSToshi Kani 		}
18546b772e8fSToshi Kani 	}
18556b772e8fSToshi Kani 
18567a26b530SCatalin Marinas out:
18576b772e8fSToshi Kani 	acpi_free_pnp_ids(&pnp);
1858a33ec399SRafael J. Wysocki }
1859a33ec399SRafael J. Wysocki 
1860e3863094SRafael J. Wysocki static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1861e3863094SRafael J. Wysocki 				      void *not_used, void **return_value)
18621da177e4SLinus Torvalds {
1863805d410fSRafael J. Wysocki 	struct acpi_device *device = NULL;
1864778cbc1dSBjorn Helgaas 	int type;
1865778cbc1dSBjorn Helgaas 	unsigned long long sta;
1866778cbc1dSBjorn Helgaas 	int result;
18671da177e4SLinus Torvalds 
18684002bf38SRafael J. Wysocki 	acpi_bus_get_device(handle, &device);
18694002bf38SRafael J. Wysocki 	if (device)
18704002bf38SRafael J. Wysocki 		goto out;
18714002bf38SRafael J. Wysocki 
1872778cbc1dSBjorn Helgaas 	result = acpi_bus_type_and_status(handle, &type, &sta);
1873778cbc1dSBjorn Helgaas 	if (result)
187451a85fafSBjorn Helgaas 		return AE_OK;
18751da177e4SLinus Torvalds 
187682c7d5efSRafael J. Wysocki 	if (type == ACPI_BUS_TYPE_POWER) {
187782c7d5efSRafael J. Wysocki 		acpi_add_power_resource(handle);
187882c7d5efSRafael J. Wysocki 		return AE_OK;
187982c7d5efSRafael J. Wysocki 	}
188082c7d5efSRafael J. Wysocki 
18816b772e8fSToshi Kani 	acpi_scan_init_hotplug(handle, type);
1882a33ec399SRafael J. Wysocki 
1883778cbc1dSBjorn Helgaas 	if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1884b581a7f9SRafael J. Wysocki 	    !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
188586e4e20eSRafael J. Wysocki 		struct acpi_device_wakeup wakeup;
188686e4e20eSRafael J. Wysocki 
1887952c63e9SJiang Liu 		if (acpi_has_method(handle, "_PRW")) {
188886e4e20eSRafael J. Wysocki 			acpi_bus_extract_wakeup_device_power_package(handle,
188986e4e20eSRafael J. Wysocki 								     &wakeup);
1890993cbe59SRafael J. Wysocki 			acpi_power_resources_list_free(&wakeup.resources);
1891993cbe59SRafael J. Wysocki 		}
1892778cbc1dSBjorn Helgaas 		return AE_CTRL_DEPTH;
1893b581a7f9SRafael J. Wysocki 	}
18941da177e4SLinus Torvalds 
18952c0d4fe0SRafael J. Wysocki 	acpi_add_single_object(&device, handle, type, sta);
1896e3b87f8aSBjorn Helgaas 	if (!device)
189751a85fafSBjorn Helgaas 		return AE_CTRL_DEPTH;
18981da177e4SLinus Torvalds 
18994002bf38SRafael J. Wysocki  out:
190051a85fafSBjorn Helgaas 	if (!*return_value)
190151a85fafSBjorn Helgaas 		*return_value = device;
1902805d410fSRafael J. Wysocki 
190351a85fafSBjorn Helgaas 	return AE_OK;
19041da177e4SLinus Torvalds }
19051da177e4SLinus Torvalds 
190687b85b3cSRafael J. Wysocki static int acpi_scan_attach_handler(struct acpi_device *device)
190787b85b3cSRafael J. Wysocki {
190887b85b3cSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
190987b85b3cSRafael J. Wysocki 	int ret = 0;
191087b85b3cSRafael J. Wysocki 
191187b85b3cSRafael J. Wysocki 	list_for_each_entry(hwid, &device->pnp.ids, list) {
1912c5698074SRafael J. Wysocki 		const struct acpi_device_id *devid;
1913c5698074SRafael J. Wysocki 		struct acpi_scan_handler *handler;
191487b85b3cSRafael J. Wysocki 
1915c5698074SRafael J. Wysocki 		handler = acpi_scan_match_handler(hwid->id, &devid);
1916c5698074SRafael J. Wysocki 		if (handler) {
1917c5698074SRafael J. Wysocki 			ret = handler->attach(device, devid);
1918c5698074SRafael J. Wysocki 			if (ret > 0) {
1919c5698074SRafael J. Wysocki 				device->handler = handler;
1920c5698074SRafael J. Wysocki 				break;
1921c5698074SRafael J. Wysocki 			} else if (ret < 0) {
1922c5698074SRafael J. Wysocki 				break;
1923c5698074SRafael J. Wysocki 			}
1924c5698074SRafael J. Wysocki 		}
192587b85b3cSRafael J. Wysocki 	}
1926ca589f94SRafael J. Wysocki 	return ret;
1927ca589f94SRafael J. Wysocki }
1928ca589f94SRafael J. Wysocki 
19290fc300b0SRafael J. Wysocki static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
19300fc300b0SRafael J. Wysocki 					  void *not_used, void **ret_not_used)
193151a85fafSBjorn Helgaas {
1932805d410fSRafael J. Wysocki 	struct acpi_device *device;
1933805d410fSRafael J. Wysocki 	unsigned long long sta_not_used;
1934ca589f94SRafael J. Wysocki 	int ret;
19351da177e4SLinus Torvalds 
19367779688fSThomas Renninger 	/*
1937805d410fSRafael J. Wysocki 	 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1938805d410fSRafael J. Wysocki 	 * namespace walks prematurely.
19397779688fSThomas Renninger 	 */
1940ca589f94SRafael J. Wysocki 	if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
1941805d410fSRafael J. Wysocki 		return AE_OK;
19427779688fSThomas Renninger 
1943805d410fSRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
1944805d410fSRafael J. Wysocki 		return AE_CTRL_DEPTH;
19453fb02738SRajesh Shah 
19463a391a39SRafael J. Wysocki 	if (device->handler)
19473a391a39SRafael J. Wysocki 		return AE_OK;
19483a391a39SRafael J. Wysocki 
1949ca589f94SRafael J. Wysocki 	ret = acpi_scan_attach_handler(device);
19506931007cSRafael J. Wysocki 	if (ret < 0)
19516931007cSRafael J. Wysocki 		return AE_CTRL_DEPTH;
19526931007cSRafael J. Wysocki 
19536931007cSRafael J. Wysocki 	device->flags.match_driver = true;
19546931007cSRafael J. Wysocki 	if (ret > 0)
19556931007cSRafael J. Wysocki 		return AE_OK;
1956ca589f94SRafael J. Wysocki 
1957ca589f94SRafael J. Wysocki 	ret = device_attach(&device->dev);
1958ca589f94SRafael J. Wysocki 	return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
1959805d410fSRafael J. Wysocki }
19603fb02738SRajesh Shah 
1961b8bd759aSRafael J. Wysocki /**
1962b8bd759aSRafael J. Wysocki  * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1963b8bd759aSRafael J. Wysocki  * @handle: Root of the namespace scope to scan.
1964b8bd759aSRafael J. Wysocki  *
1965b8bd759aSRafael J. Wysocki  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1966b8bd759aSRafael J. Wysocki  * found devices.
1967b8bd759aSRafael J. Wysocki  *
1968b8bd759aSRafael J. Wysocki  * If no devices were found, -ENODEV is returned, but it does not mean that
1969b8bd759aSRafael J. Wysocki  * there has been a real error.  There just have been no suitable ACPI objects
1970b8bd759aSRafael J. Wysocki  * in the table trunk from which the kernel could create a device and add an
1971b8bd759aSRafael J. Wysocki  * appropriate driver.
19723757b948SRafael J. Wysocki  *
19733757b948SRafael J. Wysocki  * Must be called under acpi_scan_lock.
1974b8bd759aSRafael J. Wysocki  */
1975b8bd759aSRafael J. Wysocki int acpi_bus_scan(acpi_handle handle)
19763fb02738SRajesh Shah {
19771da177e4SLinus Torvalds 	void *device = NULL;
1978c511cc19SRafael J. Wysocki 	int error = 0;
1979c511cc19SRafael J. Wysocki 
19800cd6ac52SRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
19811da177e4SLinus Torvalds 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1982e3863094SRafael J. Wysocki 				    acpi_bus_check_add, NULL, NULL, &device);
19833fb02738SRajesh Shah 
1984d2f6650aSThomas Renninger 	if (!device)
1985c511cc19SRafael J. Wysocki 		error = -ENODEV;
1986c511cc19SRafael J. Wysocki 	else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
1987805d410fSRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
19880fc300b0SRafael J. Wysocki 				    acpi_bus_device_attach, NULL, NULL, NULL);
1989d2f6650aSThomas Renninger 
1990c511cc19SRafael J. Wysocki 	return error;
19913fb02738SRajesh Shah }
1992b8bd759aSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_scan);
19931da177e4SLinus Torvalds 
199405404d8fSRafael J. Wysocki static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
199505404d8fSRafael J. Wysocki 					  void *not_used, void **ret_not_used)
19961da177e4SLinus Torvalds {
199705404d8fSRafael J. Wysocki 	struct acpi_device *device = NULL;
19981da177e4SLinus Torvalds 
199905404d8fSRafael J. Wysocki 	if (!acpi_bus_get_device(handle, &device)) {
2000ca589f94SRafael J. Wysocki 		struct acpi_scan_handler *dev_handler = device->handler;
2001ca589f94SRafael J. Wysocki 
2002ca589f94SRafael J. Wysocki 		if (dev_handler) {
2003ca589f94SRafael J. Wysocki 			if (dev_handler->detach)
2004ca589f94SRafael J. Wysocki 				dev_handler->detach(device);
2005ca589f94SRafael J. Wysocki 
2006ca589f94SRafael J. Wysocki 			device->handler = NULL;
2007ca589f94SRafael J. Wysocki 		} else {
200805404d8fSRafael J. Wysocki 			device_release_driver(&device->dev);
200905404d8fSRafael J. Wysocki 		}
2010ca589f94SRafael J. Wysocki 	}
201105404d8fSRafael J. Wysocki 	return AE_OK;
201205404d8fSRafael J. Wysocki }
20131da177e4SLinus Torvalds 
201405404d8fSRafael J. Wysocki static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
201505404d8fSRafael J. Wysocki 				   void *not_used, void **ret_not_used)
201605404d8fSRafael J. Wysocki {
201705404d8fSRafael J. Wysocki 	struct acpi_device *device = NULL;
201805404d8fSRafael J. Wysocki 
201905404d8fSRafael J. Wysocki 	if (!acpi_bus_get_device(handle, &device))
202005404d8fSRafael J. Wysocki 		acpi_device_unregister(device);
202105404d8fSRafael J. Wysocki 
202205404d8fSRafael J. Wysocki 	return AE_OK;
202305404d8fSRafael J. Wysocki }
202405404d8fSRafael J. Wysocki 
20253757b948SRafael J. Wysocki /**
20263757b948SRafael J. Wysocki  * acpi_bus_trim - Remove ACPI device node and all of its descendants
20273757b948SRafael J. Wysocki  * @start: Root of the ACPI device nodes subtree to remove.
20283757b948SRafael J. Wysocki  *
20293757b948SRafael J. Wysocki  * Must be called under acpi_scan_lock.
20303757b948SRafael J. Wysocki  */
2031bfee26dbSRafael J. Wysocki void acpi_bus_trim(struct acpi_device *start)
20321da177e4SLinus Torvalds {
20331da177e4SLinus Torvalds 	/*
203405404d8fSRafael J. Wysocki 	 * Execute acpi_bus_device_detach() as a post-order callback to detach
203505404d8fSRafael J. Wysocki 	 * all ACPI drivers from the device nodes being removed.
20361da177e4SLinus Torvalds 	 */
203705404d8fSRafael J. Wysocki 	acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
203805404d8fSRafael J. Wysocki 			    acpi_bus_device_detach, NULL, NULL);
203905404d8fSRafael J. Wysocki 	acpi_bus_device_detach(start->handle, 0, NULL, NULL);
20401da177e4SLinus Torvalds 	/*
2041cecdb193SRafael J. Wysocki 	 * Execute acpi_bus_remove() as a post-order callback to remove device
2042cecdb193SRafael J. Wysocki 	 * nodes in the given namespace scope.
20431da177e4SLinus Torvalds 	 */
2044cecdb193SRafael J. Wysocki 	acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2045cecdb193SRafael J. Wysocki 			    acpi_bus_remove, NULL, NULL);
2046cecdb193SRafael J. Wysocki 	acpi_bus_remove(start->handle, 0, NULL, NULL);
20471da177e4SLinus Torvalds }
2048ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim);
2049ceaba663SKristen Accardi 
2050e8b945c9SBjorn Helgaas static int acpi_bus_scan_fixed(void)
20511da177e4SLinus Torvalds {
20521da177e4SLinus Torvalds 	int result = 0;
2053c4168bffSLi Shaohua 
20541da177e4SLinus Torvalds 	/*
20551da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
20561da177e4SLinus Torvalds 	 */
20572c0d4fe0SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
20582c0d4fe0SRafael J. Wysocki 		struct acpi_device *device = NULL;
20592c0d4fe0SRafael J. Wysocki 
20605c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
2061c4168bffSLi Shaohua 						ACPI_BUS_TYPE_POWER_BUTTON,
20622c0d4fe0SRafael J. Wysocki 						ACPI_STA_DEFAULT);
20632c0d4fe0SRafael J. Wysocki 		if (result)
20642c0d4fe0SRafael J. Wysocki 			return result;
20652c0d4fe0SRafael J. Wysocki 
206688346167SRafael J. Wysocki 		device->flags.match_driver = true;
20672c0d4fe0SRafael J. Wysocki 		result = device_attach(&device->dev);
20682c0d4fe0SRafael J. Wysocki 		if (result < 0)
20692c0d4fe0SRafael J. Wysocki 			return result;
20702c0d4fe0SRafael J. Wysocki 
2071c10d7a13SDaniel Drake 		device_init_wakeup(&device->dev, true);
20723fb02738SRajesh Shah 	}
20731da177e4SLinus Torvalds 
20742c0d4fe0SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
20752c0d4fe0SRafael J. Wysocki 		struct acpi_device *device = NULL;
20762c0d4fe0SRafael J. Wysocki 
20775c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
2078c4168bffSLi Shaohua 						ACPI_BUS_TYPE_SLEEP_BUTTON,
20792c0d4fe0SRafael J. Wysocki 						ACPI_STA_DEFAULT);
20802c0d4fe0SRafael J. Wysocki 		if (result)
20812c0d4fe0SRafael J. Wysocki 			return result;
20822c0d4fe0SRafael J. Wysocki 
208388346167SRafael J. Wysocki 		device->flags.match_driver = true;
20842c0d4fe0SRafael J. Wysocki 		result = device_attach(&device->dev);
20853fb02738SRajesh Shah 	}
20861da177e4SLinus Torvalds 
20872c0d4fe0SRafael J. Wysocki 	return result < 0 ? result : 0;
20881da177e4SLinus Torvalds }
20891da177e4SLinus Torvalds 
2090e747f274SBjorn Helgaas int __init acpi_scan_init(void)
20911da177e4SLinus Torvalds {
20921da177e4SLinus Torvalds 	int result;
20931da177e4SLinus Torvalds 
20945b327265SPatrick Mochel 	result = bus_register(&acpi_bus_type);
20955b327265SPatrick Mochel 	if (result) {
20965b327265SPatrick Mochel 		/* We don't want to quit even if we failed to add suspend/resume */
20975b327265SPatrick Mochel 		printk(KERN_ERR PREFIX "Could not register bus type\n");
20985b327265SPatrick Mochel 	}
20995b327265SPatrick Mochel 
210092ef2a25SRafael J. Wysocki 	acpi_pci_root_init();
21014daeaf68SRafael J. Wysocki 	acpi_pci_link_init();
2102ac212b69SRafael J. Wysocki 	acpi_processor_init();
2103141a297bSRafael J. Wysocki 	acpi_platform_init();
2104f58b082aSRafael J. Wysocki 	acpi_lpss_init();
21052fa97febSLan Tianyu 	acpi_cmos_rtc_init();
2106737f1a9fSRafael J. Wysocki 	acpi_container_init();
21070a347644SRafael J. Wysocki 	acpi_memory_hotplug_init();
210894add0f8SJiang Liu 	acpi_dock_init();
210997d9a9e9SRafael J. Wysocki 
21103757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
21111da177e4SLinus Torvalds 	/*
21121da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
21131da177e4SLinus Torvalds 	 */
21140cd6ac52SRafael J. Wysocki 	result = acpi_bus_scan(ACPI_ROOT_OBJECT);
21151da177e4SLinus Torvalds 	if (result)
21163757b948SRafael J. Wysocki 		goto out;
21171da177e4SLinus Torvalds 
21180cd6ac52SRafael J. Wysocki 	result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
21191da177e4SLinus Torvalds 	if (result)
21203757b948SRafael J. Wysocki 		goto out;
2121b8bd759aSRafael J. Wysocki 
2122b8bd759aSRafael J. Wysocki 	result = acpi_bus_scan_fixed();
2123b8bd759aSRafael J. Wysocki 	if (result) {
2124b8bd759aSRafael J. Wysocki 		acpi_device_unregister(acpi_root);
21253757b948SRafael J. Wysocki 		goto out;
2126b8bd759aSRafael J. Wysocki 	}
2127b8bd759aSRafael J. Wysocki 
2128b8bd759aSRafael J. Wysocki 	acpi_update_all_gpes();
21293757b948SRafael J. Wysocki 
2130668192b6SYinghai Lu 	acpi_pci_root_hp_init();
2131668192b6SYinghai Lu 
21323757b948SRafael J. Wysocki  out:
21333757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
21343757b948SRafael J. Wysocki 	return result;
21351da177e4SLinus Torvalds }
2136