xref: /openbmc/linux/drivers/acpi/scan.c (revision b27b14cebfdb687e6d281d2cf82858a30796dc2a)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * scan.c - support for transforming the ACPI namespace into individual objects
31da177e4SLinus Torvalds  */
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <linux/module.h>
61da177e4SLinus Torvalds #include <linux/init.h>
75a0e3ad6STejun Heo #include <linux/slab.h>
89b6d97b6SRandy Dunlap #include <linux/kernel.h>
91da177e4SLinus Torvalds #include <linux/acpi.h>
1074523c90SAlok N Kataria #include <linux/signal.h>
1174523c90SAlok N Kataria #include <linux/kthread.h>
12222e82acSDarrick J. Wong #include <linux/dmi.h>
13d1efe3c3SLance Ortiz #include <linux/nls.h>
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #include <acpi/acpi_drivers.h>
161da177e4SLinus Torvalds 
17e60cc7a6SBjorn Helgaas #include "internal.h"
18e60cc7a6SBjorn Helgaas 
191da177e4SLinus Torvalds #define _COMPONENT		ACPI_BUS_COMPONENT
20f52fd66dSLen Brown ACPI_MODULE_NAME("scan");
211da177e4SLinus Torvalds #define STRUCT_TO_INT(s)	(*((int*)&s))
221da177e4SLinus Torvalds extern struct acpi_device *acpi_root;
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #define ACPI_BUS_CLASS			"system_bus"
2529b71a1cSThomas Renninger #define ACPI_BUS_HID			"LNXSYBUS"
261da177e4SLinus Torvalds #define ACPI_BUS_DEVICE_NAME		"System Bus"
271da177e4SLinus Torvalds 
28859ac9a4SBjorn Helgaas #define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
29859ac9a4SBjorn Helgaas 
30683058e3SRafael J. Wysocki /*
31683058e3SRafael J. Wysocki  * If set, devices will be hot-removed even if they cannot be put offline
32683058e3SRafael J. Wysocki  * gracefully (from the kernel's standpoint).
33683058e3SRafael J. Wysocki  */
34683058e3SRafael J. Wysocki bool acpi_force_hot_remove;
35683058e3SRafael J. Wysocki 
36620e112cSThomas Renninger static const char *dummy_hid = "device";
372b2ae7c7SThomas Renninger 
38e49bd2ddSZhang Rui static LIST_HEAD(acpi_bus_id_list);
39c511cc19SRafael J. Wysocki static DEFINE_MUTEX(acpi_scan_lock);
40ca589f94SRafael J. Wysocki static LIST_HEAD(acpi_scan_handlers_list);
419090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock);
421da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
431da177e4SLinus Torvalds 
44e49bd2ddSZhang Rui struct acpi_device_bus_id{
45bb095854SZhang Rui 	char bus_id[15];
46e49bd2ddSZhang Rui 	unsigned int instance_no;
47e49bd2ddSZhang Rui 	struct list_head node;
48e49bd2ddSZhang Rui };
4929b71a1cSThomas Renninger 
503757b948SRafael J. Wysocki void acpi_scan_lock_acquire(void)
513757b948SRafael J. Wysocki {
523757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
533757b948SRafael J. Wysocki }
543757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
553757b948SRafael J. Wysocki 
563757b948SRafael J. Wysocki void acpi_scan_lock_release(void)
573757b948SRafael J. Wysocki {
583757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
593757b948SRafael J. Wysocki }
603757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
613757b948SRafael J. Wysocki 
62ca589f94SRafael J. Wysocki int acpi_scan_add_handler(struct acpi_scan_handler *handler)
63ca589f94SRafael J. Wysocki {
64ca589f94SRafael J. Wysocki 	if (!handler || !handler->attach)
65ca589f94SRafael J. Wysocki 		return -EINVAL;
66ca589f94SRafael J. Wysocki 
67ca589f94SRafael J. Wysocki 	list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
68ca589f94SRafael J. Wysocki 	return 0;
69ca589f94SRafael J. Wysocki }
70ca589f94SRafael J. Wysocki 
713f8055c3SRafael J. Wysocki int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
723f8055c3SRafael J. Wysocki 				       const char *hotplug_profile_name)
733f8055c3SRafael J. Wysocki {
743f8055c3SRafael J. Wysocki 	int error;
753f8055c3SRafael J. Wysocki 
763f8055c3SRafael J. Wysocki 	error = acpi_scan_add_handler(handler);
773f8055c3SRafael J. Wysocki 	if (error)
783f8055c3SRafael J. Wysocki 		return error;
793f8055c3SRafael J. Wysocki 
803f8055c3SRafael J. Wysocki 	acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
813f8055c3SRafael J. Wysocki 	return 0;
823f8055c3SRafael J. Wysocki }
833f8055c3SRafael J. Wysocki 
8429b71a1cSThomas Renninger /*
8529b71a1cSThomas Renninger  * Creates hid/cid(s) string needed for modalias and uevent
8629b71a1cSThomas Renninger  * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
8729b71a1cSThomas Renninger  * char *modalias: "acpi:IBM0001:ACPI0001"
8829b71a1cSThomas Renninger */
89b3e572d2SAdrian Bunk static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
90b3e572d2SAdrian Bunk 			   int size)
91b3e572d2SAdrian Bunk {
9229b71a1cSThomas Renninger 	int len;
935c9fcb5dSZhang Rui 	int count;
947f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
9529b71a1cSThomas Renninger 
962b2ae7c7SThomas Renninger 	if (list_empty(&acpi_dev->pnp.ids))
972b2ae7c7SThomas Renninger 		return 0;
982b2ae7c7SThomas Renninger 
995c9fcb5dSZhang Rui 	len = snprintf(modalias, size, "acpi:");
10029b71a1cSThomas Renninger 	size -= len;
10129b71a1cSThomas Renninger 
1027f47fa6cSBjorn Helgaas 	list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
1037f47fa6cSBjorn Helgaas 		count = snprintf(&modalias[len], size, "%s:", id->id);
1045c9fcb5dSZhang Rui 		if (count < 0 || count >= size)
1055c9fcb5dSZhang Rui 			return -EINVAL;
1065c9fcb5dSZhang Rui 		len += count;
1075c9fcb5dSZhang Rui 		size -= count;
1085c9fcb5dSZhang Rui 	}
1095c9fcb5dSZhang Rui 
11029b71a1cSThomas Renninger 	modalias[len] = '\0';
11129b71a1cSThomas Renninger 	return len;
11229b71a1cSThomas Renninger }
11329b71a1cSThomas Renninger 
11429b71a1cSThomas Renninger static ssize_t
11529b71a1cSThomas Renninger acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
11629b71a1cSThomas Renninger 	struct acpi_device *acpi_dev = to_acpi_device(dev);
11729b71a1cSThomas Renninger 	int len;
11829b71a1cSThomas Renninger 
11929b71a1cSThomas Renninger 	/* Device has no HID and no CID or string is >1024 */
12029b71a1cSThomas Renninger 	len = create_modalias(acpi_dev, buf, 1024);
12129b71a1cSThomas Renninger 	if (len <= 0)
12229b71a1cSThomas Renninger 		return 0;
12329b71a1cSThomas Renninger 	buf[len++] = '\n';
12429b71a1cSThomas Renninger 	return len;
12529b71a1cSThomas Renninger }
12629b71a1cSThomas Renninger static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
12729b71a1cSThomas Renninger 
128683058e3SRafael J. Wysocki static acpi_status acpi_bus_offline_companions(acpi_handle handle, u32 lvl,
129683058e3SRafael J. Wysocki 					       void *data, void **ret_p)
130683058e3SRafael J. Wysocki {
131683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
132683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
133303bfdb1SRafael J. Wysocki 	bool second_pass = (bool)data;
134683058e3SRafael J. Wysocki 	acpi_status status = AE_OK;
135683058e3SRafael J. Wysocki 
136683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
137683058e3SRafael J. Wysocki 		return AE_OK;
138683058e3SRafael J. Wysocki 
139683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
140683058e3SRafael J. Wysocki 
141683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node) {
142683058e3SRafael J. Wysocki 		int ret;
143683058e3SRafael J. Wysocki 
144303bfdb1SRafael J. Wysocki 		if (second_pass) {
145303bfdb1SRafael J. Wysocki 			/* Skip devices offlined by the first pass. */
146303bfdb1SRafael J. Wysocki 			if (pn->put_online)
147303bfdb1SRafael J. Wysocki 				continue;
148303bfdb1SRafael J. Wysocki 		} else {
149303bfdb1SRafael J. Wysocki 			pn->put_online = false;
150303bfdb1SRafael J. Wysocki 		}
151683058e3SRafael J. Wysocki 		ret = device_offline(pn->dev);
152683058e3SRafael J. Wysocki 		if (acpi_force_hot_remove)
153683058e3SRafael J. Wysocki 			continue;
154683058e3SRafael J. Wysocki 
155303bfdb1SRafael J. Wysocki 		if (ret >= 0) {
156303bfdb1SRafael J. Wysocki 			pn->put_online = !ret;
157303bfdb1SRafael J. Wysocki 		} else {
158303bfdb1SRafael J. Wysocki 			*ret_p = pn->dev;
159303bfdb1SRafael J. Wysocki 			if (second_pass) {
160683058e3SRafael J. Wysocki 				status = AE_ERROR;
161683058e3SRafael J. Wysocki 				break;
162683058e3SRafael J. Wysocki 			}
163303bfdb1SRafael J. Wysocki 		}
164683058e3SRafael J. Wysocki 	}
165683058e3SRafael J. Wysocki 
166683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
167683058e3SRafael J. Wysocki 
168683058e3SRafael J. Wysocki 	return status;
169683058e3SRafael J. Wysocki }
170683058e3SRafael J. Wysocki 
171683058e3SRafael J. Wysocki static acpi_status acpi_bus_online_companions(acpi_handle handle, u32 lvl,
172683058e3SRafael J. Wysocki 					      void *data, void **ret_p)
173683058e3SRafael J. Wysocki {
174683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
175683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
176683058e3SRafael J. Wysocki 
177683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
178683058e3SRafael J. Wysocki 		return AE_OK;
179683058e3SRafael J. Wysocki 
180683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
181683058e3SRafael J. Wysocki 
182683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node)
183683058e3SRafael J. Wysocki 		if (pn->put_online) {
184683058e3SRafael J. Wysocki 			device_online(pn->dev);
185683058e3SRafael J. Wysocki 			pn->put_online = false;
186683058e3SRafael J. Wysocki 		}
187683058e3SRafael J. Wysocki 
188683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
189683058e3SRafael J. Wysocki 
190683058e3SRafael J. Wysocki 	return AE_OK;
191683058e3SRafael J. Wysocki }
192683058e3SRafael J. Wysocki 
193a33ec399SRafael J. Wysocki static int acpi_scan_hot_remove(struct acpi_device *device)
1941da177e4SLinus Torvalds {
1955993c467SYinghai Lu 	acpi_handle handle = device->handle;
196303bfdb1SRafael J. Wysocki 	struct device *errdev;
197a33ec399SRafael J. Wysocki 	acpi_status status;
198882fd12eSToshi Kani 	unsigned long long sta;
199f058cdf4SRafael J. Wysocki 
2003757b948SRafael J. Wysocki 	/* If there is no handle, the device node has been unregistered. */
201a33ec399SRafael J. Wysocki 	if (!handle) {
2023757b948SRafael J. Wysocki 		dev_dbg(&device->dev, "ACPI handle missing\n");
2033757b948SRafael J. Wysocki 		put_device(&device->dev);
204a33ec399SRafael J. Wysocki 		return -EINVAL;
2053757b948SRafael J. Wysocki 	}
2063757b948SRafael J. Wysocki 
207303bfdb1SRafael J. Wysocki 	/*
208303bfdb1SRafael J. Wysocki 	 * Carry out two passes here and ignore errors in the first pass,
209303bfdb1SRafael J. Wysocki 	 * because if the devices in question are memory blocks and
210303bfdb1SRafael J. Wysocki 	 * CONFIG_MEMCG is set, one of the blocks may hold data structures
211303bfdb1SRafael J. Wysocki 	 * that the other blocks depend on, but it is not known in advance which
212303bfdb1SRafael J. Wysocki 	 * block holds them.
213303bfdb1SRafael J. Wysocki 	 *
214303bfdb1SRafael J. Wysocki 	 * If the first pass is successful, the second one isn't needed, though.
215303bfdb1SRafael J. Wysocki 	 */
216303bfdb1SRafael J. Wysocki 	errdev = NULL;
217683058e3SRafael J. Wysocki 	acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
218303bfdb1SRafael J. Wysocki 			    NULL, acpi_bus_offline_companions,
219303bfdb1SRafael J. Wysocki 			    (void *)false, (void **)&errdev);
220303bfdb1SRafael J. Wysocki 	acpi_bus_offline_companions(handle, 0, (void *)false, (void **)&errdev);
221303bfdb1SRafael J. Wysocki 	if (errdev) {
222303bfdb1SRafael J. Wysocki 		errdev = NULL;
223303bfdb1SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
224303bfdb1SRafael J. Wysocki 				    NULL, acpi_bus_offline_companions,
225303bfdb1SRafael J. Wysocki 				    (void *)true , (void **)&errdev);
226303bfdb1SRafael J. Wysocki 		if (!errdev || acpi_force_hot_remove)
227303bfdb1SRafael J. Wysocki 			acpi_bus_offline_companions(handle, 0, (void *)true,
228303bfdb1SRafael J. Wysocki 						    (void **)&errdev);
229303bfdb1SRafael J. Wysocki 
230303bfdb1SRafael J. Wysocki 		if (errdev && !acpi_force_hot_remove) {
231303bfdb1SRafael J. Wysocki 			dev_warn(errdev, "Offline failed.\n");
232303bfdb1SRafael J. Wysocki 			acpi_bus_online_companions(handle, 0, NULL, NULL);
233303bfdb1SRafael J. Wysocki 			acpi_walk_namespace(ACPI_TYPE_ANY, handle,
234303bfdb1SRafael J. Wysocki 					    ACPI_UINT32_MAX,
235303bfdb1SRafael J. Wysocki 					    acpi_bus_online_companions, NULL,
236303bfdb1SRafael J. Wysocki 					    NULL, NULL);
237683058e3SRafael J. Wysocki 			put_device(&device->dev);
238683058e3SRafael J. Wysocki 			return -EBUSY;
239683058e3SRafael J. Wysocki 		}
240303bfdb1SRafael J. Wysocki 	}
241683058e3SRafael J. Wysocki 
24226d46867SZhang Rui 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2430794469dSKay Sievers 		"Hot-removing device %s...\n", dev_name(&device->dev)));
24426d46867SZhang Rui 
245bfee26dbSRafael J. Wysocki 	acpi_bus_trim(device);
246683058e3SRafael J. Wysocki 
2473757b948SRafael J. Wysocki 	/* Device node has been unregistered. */
2483757b948SRafael J. Wysocki 	put_device(&device->dev);
249b3c450c3SToshi Kani 	device = NULL;
250b3c450c3SToshi Kani 
2517d2421f8SJiang Liu 	acpi_evaluate_lck(handle, 0);
2521da177e4SLinus Torvalds 	/*
2531da177e4SLinus Torvalds 	 * TBD: _EJD support.
2541da177e4SLinus Torvalds 	 */
2557d2421f8SJiang Liu 	status = acpi_evaluate_ej0(handle);
2567d2421f8SJiang Liu 	if (status == AE_NOT_FOUND)
257a33ec399SRafael J. Wysocki 		return -ENODEV;
2587d2421f8SJiang Liu 	else if (ACPI_FAILURE(status))
259a33ec399SRafael J. Wysocki 		return -EIO;
260f058cdf4SRafael J. Wysocki 
261882fd12eSToshi Kani 	/*
262882fd12eSToshi Kani 	 * Verify if eject was indeed successful.  If not, log an error
263882fd12eSToshi Kani 	 * message.  No need to call _OST since _EJ0 call was made OK.
264882fd12eSToshi Kani 	 */
265882fd12eSToshi Kani 	status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
266882fd12eSToshi Kani 	if (ACPI_FAILURE(status)) {
267882fd12eSToshi Kani 		acpi_handle_warn(handle,
268882fd12eSToshi Kani 			"Status check after eject failed (0x%x)\n", status);
269882fd12eSToshi Kani 	} else if (sta & ACPI_STA_DEVICE_ENABLED) {
270882fd12eSToshi Kani 		acpi_handle_warn(handle,
271882fd12eSToshi Kani 			"Eject incomplete - status 0x%llx\n", sta);
272882fd12eSToshi Kani 	}
273882fd12eSToshi Kani 
274a33ec399SRafael J. Wysocki 	return 0;
275a33ec399SRafael J. Wysocki }
276f058cdf4SRafael J. Wysocki 
277a33ec399SRafael J. Wysocki static void acpi_bus_device_eject(void *context)
278a33ec399SRafael J. Wysocki {
279a33ec399SRafael J. Wysocki 	acpi_handle handle = context;
280a33ec399SRafael J. Wysocki 	struct acpi_device *device = NULL;
281a33ec399SRafael J. Wysocki 	struct acpi_scan_handler *handler;
282a33ec399SRafael J. Wysocki 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
283f943db40SRafael J. Wysocki 	int error;
284a33ec399SRafael J. Wysocki 
285e0ae8feeSRafael J. Wysocki 	lock_device_hotplug();
286a33ec399SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
287a33ec399SRafael J. Wysocki 
288a33ec399SRafael J. Wysocki 	acpi_bus_get_device(handle, &device);
289a33ec399SRafael J. Wysocki 	if (!device)
290a33ec399SRafael J. Wysocki 		goto err_out;
291a33ec399SRafael J. Wysocki 
292a33ec399SRafael J. Wysocki 	handler = device->handler;
293a33ec399SRafael J. Wysocki 	if (!handler || !handler->hotplug.enabled) {
294a33ec399SRafael J. Wysocki 		ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
295a33ec399SRafael J. Wysocki 		goto err_out;
296a33ec399SRafael J. Wysocki 	}
297a33ec399SRafael J. Wysocki 	acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
298a33ec399SRafael J. Wysocki 				  ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
299f943db40SRafael J. Wysocki 	if (handler->hotplug.mode == AHM_CONTAINER)
300a33ec399SRafael J. Wysocki 		kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
301a33ec399SRafael J. Wysocki 
302a33ec399SRafael J. Wysocki 	get_device(&device->dev);
303a33ec399SRafael J. Wysocki 	error = acpi_scan_hot_remove(device);
304a33ec399SRafael J. Wysocki 	if (error)
305a33ec399SRafael J. Wysocki 		goto err_out;
3061da177e4SLinus Torvalds 
3073757b948SRafael J. Wysocki  out:
308f058cdf4SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
309e0ae8feeSRafael J. Wysocki 	unlock_device_hotplug();
310c8d72a5eSZhang Rui 	return;
311a33ec399SRafael J. Wysocki 
312a33ec399SRafael J. Wysocki  err_out:
313a33ec399SRafael J. Wysocki 	acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST, ost_code,
314a33ec399SRafael J. Wysocki 				  NULL);
315a33ec399SRafael J. Wysocki 	goto out;
316a33ec399SRafael J. Wysocki }
317a33ec399SRafael J. Wysocki 
318a33ec399SRafael J. Wysocki static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
319a33ec399SRafael J. Wysocki {
320a33ec399SRafael J. Wysocki 	struct acpi_device *device = NULL;
321a33ec399SRafael J. Wysocki 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
322a33ec399SRafael J. Wysocki 	int error;
323a33ec399SRafael J. Wysocki 
324683058e3SRafael J. Wysocki 	lock_device_hotplug();
325e0ae8feeSRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
326a33ec399SRafael J. Wysocki 
3278832f7e4SRafael J. Wysocki 	if (ost_source != ACPI_NOTIFY_BUS_CHECK) {
328a33ec399SRafael J. Wysocki 		acpi_bus_get_device(handle, &device);
329a33ec399SRafael J. Wysocki 		if (device) {
330a33ec399SRafael J. Wysocki 			dev_warn(&device->dev, "Attempt to re-insert\n");
331a33ec399SRafael J. Wysocki 			goto out;
332a33ec399SRafael J. Wysocki 		}
3338832f7e4SRafael J. Wysocki 	}
334a33ec399SRafael J. Wysocki 	acpi_evaluate_hotplug_ost(handle, ost_source,
335a33ec399SRafael J. Wysocki 				  ACPI_OST_SC_INSERT_IN_PROGRESS, NULL);
336a33ec399SRafael J. Wysocki 	error = acpi_bus_scan(handle);
337a33ec399SRafael J. Wysocki 	if (error) {
338a33ec399SRafael J. Wysocki 		acpi_handle_warn(handle, "Namespace scan failure\n");
339a33ec399SRafael J. Wysocki 		goto out;
340a33ec399SRafael J. Wysocki 	}
341a33ec399SRafael J. Wysocki 	error = acpi_bus_get_device(handle, &device);
342a33ec399SRafael J. Wysocki 	if (error) {
343a33ec399SRafael J. Wysocki 		acpi_handle_warn(handle, "Missing device node object\n");
344a33ec399SRafael J. Wysocki 		goto out;
345a33ec399SRafael J. Wysocki 	}
346a33ec399SRafael J. Wysocki 	ost_code = ACPI_OST_SC_SUCCESS;
347a33ec399SRafael J. Wysocki 	if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
348a33ec399SRafael J. Wysocki 		kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
349a33ec399SRafael J. Wysocki 
350a33ec399SRafael J. Wysocki  out:
351a33ec399SRafael J. Wysocki 	acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
352a33ec399SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
353e0ae8feeSRafael J. Wysocki 	unlock_device_hotplug();
354a33ec399SRafael J. Wysocki }
355a33ec399SRafael J. Wysocki 
356a33ec399SRafael J. Wysocki static void acpi_scan_bus_check(void *context)
357a33ec399SRafael J. Wysocki {
358a33ec399SRafael J. Wysocki 	acpi_scan_bus_device_check((acpi_handle)context,
359a33ec399SRafael J. Wysocki 				   ACPI_NOTIFY_BUS_CHECK);
360a33ec399SRafael J. Wysocki }
361a33ec399SRafael J. Wysocki 
362a33ec399SRafael J. Wysocki static void acpi_scan_device_check(void *context)
363a33ec399SRafael J. Wysocki {
364a33ec399SRafael J. Wysocki 	acpi_scan_bus_device_check((acpi_handle)context,
365a33ec399SRafael J. Wysocki 				   ACPI_NOTIFY_DEVICE_CHECK);
366a33ec399SRafael J. Wysocki }
367a33ec399SRafael J. Wysocki 
3682cbb14fbSToshi Kani static void acpi_hotplug_unsupported(acpi_handle handle, u32 type)
3692cbb14fbSToshi Kani {
3702cbb14fbSToshi Kani 	u32 ost_status;
3712cbb14fbSToshi Kani 
3722cbb14fbSToshi Kani 	switch (type) {
3732cbb14fbSToshi Kani 	case ACPI_NOTIFY_BUS_CHECK:
3742cbb14fbSToshi Kani 		acpi_handle_debug(handle,
3752cbb14fbSToshi Kani 			"ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
3762cbb14fbSToshi Kani 		ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
3772cbb14fbSToshi Kani 		break;
3782cbb14fbSToshi Kani 	case ACPI_NOTIFY_DEVICE_CHECK:
3792cbb14fbSToshi Kani 		acpi_handle_debug(handle,
3802cbb14fbSToshi Kani 			"ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
3812cbb14fbSToshi Kani 		ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
3822cbb14fbSToshi Kani 		break;
3832cbb14fbSToshi Kani 	case ACPI_NOTIFY_EJECT_REQUEST:
3842cbb14fbSToshi Kani 		acpi_handle_debug(handle,
3852cbb14fbSToshi Kani 			"ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
3862cbb14fbSToshi Kani 		ost_status = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
3872cbb14fbSToshi Kani 		break;
3882cbb14fbSToshi Kani 	default:
3892cbb14fbSToshi Kani 		/* non-hotplug event; possibly handled by other handler */
3902cbb14fbSToshi Kani 		return;
3912cbb14fbSToshi Kani 	}
3922cbb14fbSToshi Kani 
3932cbb14fbSToshi Kani 	acpi_evaluate_hotplug_ost(handle, type, ost_status, NULL);
3942cbb14fbSToshi Kani }
3952cbb14fbSToshi Kani 
3962cbb14fbSToshi Kani static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
397a33ec399SRafael J. Wysocki {
398a33ec399SRafael J. Wysocki 	acpi_osd_exec_callback callback;
3992cbb14fbSToshi Kani 	struct acpi_scan_handler *handler = data;
400a33ec399SRafael J. Wysocki 	acpi_status status;
401a33ec399SRafael J. Wysocki 
4022cbb14fbSToshi Kani 	if (!handler->hotplug.enabled)
4032cbb14fbSToshi Kani 		return acpi_hotplug_unsupported(handle, type);
4042cbb14fbSToshi Kani 
405a33ec399SRafael J. Wysocki 	switch (type) {
406a33ec399SRafael J. Wysocki 	case ACPI_NOTIFY_BUS_CHECK:
407a33ec399SRafael J. Wysocki 		acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
408a33ec399SRafael J. Wysocki 		callback = acpi_scan_bus_check;
409a33ec399SRafael J. Wysocki 		break;
410a33ec399SRafael J. Wysocki 	case ACPI_NOTIFY_DEVICE_CHECK:
411a33ec399SRafael J. Wysocki 		acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
412a33ec399SRafael J. Wysocki 		callback = acpi_scan_device_check;
413a33ec399SRafael J. Wysocki 		break;
414a33ec399SRafael J. Wysocki 	case ACPI_NOTIFY_EJECT_REQUEST:
415a33ec399SRafael J. Wysocki 		acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
416a33ec399SRafael J. Wysocki 		callback = acpi_bus_device_eject;
417a33ec399SRafael J. Wysocki 		break;
418a33ec399SRafael J. Wysocki 	default:
419a33ec399SRafael J. Wysocki 		/* non-hotplug event; possibly handled by other handler */
420a33ec399SRafael J. Wysocki 		return;
421a33ec399SRafael J. Wysocki 	}
422a33ec399SRafael J. Wysocki 	status = acpi_os_hotplug_execute(callback, handle);
423a33ec399SRafael J. Wysocki 	if (ACPI_FAILURE(status))
424a33ec399SRafael J. Wysocki 		acpi_evaluate_hotplug_ost(handle, type,
425a33ec399SRafael J. Wysocki 					  ACPI_OST_SC_NON_SPECIFIC_FAILURE,
426a33ec399SRafael J. Wysocki 					  NULL);
427a33ec399SRafael J. Wysocki }
428a33ec399SRafael J. Wysocki 
429a33ec399SRafael J. Wysocki /**
430a33ec399SRafael J. Wysocki  * acpi_bus_hot_remove_device: hot-remove a device and its children
431a33ec399SRafael J. Wysocki  * @context: struct acpi_eject_event pointer (freed in this func)
432a33ec399SRafael J. Wysocki  *
433a33ec399SRafael J. Wysocki  * Hot-remove a device and its children. This function frees up the
434a33ec399SRafael J. Wysocki  * memory space passed by arg context, so that the caller may call
435a33ec399SRafael J. Wysocki  * this function asynchronously through acpi_os_hotplug_execute().
436a33ec399SRafael J. Wysocki  */
437a33ec399SRafael J. Wysocki void acpi_bus_hot_remove_device(void *context)
438a33ec399SRafael J. Wysocki {
439a33ec399SRafael J. Wysocki 	struct acpi_eject_event *ej_event = context;
440a33ec399SRafael J. Wysocki 	struct acpi_device *device = ej_event->device;
441a33ec399SRafael J. Wysocki 	acpi_handle handle = device->handle;
442a33ec399SRafael J. Wysocki 	int error;
443a33ec399SRafael J. Wysocki 
444e0ae8feeSRafael J. Wysocki 	lock_device_hotplug();
445a33ec399SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
446a33ec399SRafael J. Wysocki 
447a33ec399SRafael J. Wysocki 	error = acpi_scan_hot_remove(device);
448a33ec399SRafael J. Wysocki 	if (error && handle)
449a33ec399SRafael J. Wysocki 		acpi_evaluate_hotplug_ost(handle, ej_event->event,
450a33ec399SRafael J. Wysocki 					  ACPI_OST_SC_NON_SPECIFIC_FAILURE,
451a33ec399SRafael J. Wysocki 					  NULL);
452a33ec399SRafael J. Wysocki 
453a33ec399SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
454e0ae8feeSRafael J. Wysocki 	unlock_device_hotplug();
455a33ec399SRafael J. Wysocki 	kfree(context);
4561da177e4SLinus Torvalds }
45761622accSToshi Kani EXPORT_SYMBOL(acpi_bus_hot_remove_device);
4581da177e4SLinus Torvalds 
459836aedb1SRafael J. Wysocki static ssize_t real_power_state_show(struct device *dev,
460836aedb1SRafael J. Wysocki 				     struct device_attribute *attr, char *buf)
461836aedb1SRafael J. Wysocki {
462836aedb1SRafael J. Wysocki 	struct acpi_device *adev = to_acpi_device(dev);
463836aedb1SRafael J. Wysocki 	int state;
464836aedb1SRafael J. Wysocki 	int ret;
465836aedb1SRafael J. Wysocki 
466836aedb1SRafael J. Wysocki 	ret = acpi_device_get_power(adev, &state);
467836aedb1SRafael J. Wysocki 	if (ret)
468836aedb1SRafael J. Wysocki 		return ret;
469836aedb1SRafael J. Wysocki 
470836aedb1SRafael J. Wysocki 	return sprintf(buf, "%s\n", acpi_power_state_string(state));
471836aedb1SRafael J. Wysocki }
472836aedb1SRafael J. Wysocki 
473836aedb1SRafael J. Wysocki static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
474836aedb1SRafael J. Wysocki 
475836aedb1SRafael J. Wysocki static ssize_t power_state_show(struct device *dev,
476836aedb1SRafael J. Wysocki 				struct device_attribute *attr, char *buf)
477836aedb1SRafael J. Wysocki {
478836aedb1SRafael J. Wysocki 	struct acpi_device *adev = to_acpi_device(dev);
479836aedb1SRafael J. Wysocki 
480836aedb1SRafael J. Wysocki 	return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
481836aedb1SRafael J. Wysocki }
482836aedb1SRafael J. Wysocki 
483836aedb1SRafael J. Wysocki static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
484836aedb1SRafael J. Wysocki 
4851da177e4SLinus Torvalds static ssize_t
486f883d9dbSPatrick Mochel acpi_eject_store(struct device *d, struct device_attribute *attr,
487f883d9dbSPatrick Mochel 		const char *buf, size_t count)
4881da177e4SLinus Torvalds {
489f883d9dbSPatrick Mochel 	struct acpi_device *acpi_device = to_acpi_device(d);
490c4753e57SToshi Kani 	struct acpi_eject_event *ej_event;
491a33ec399SRafael J. Wysocki 	acpi_object_type not_used;
492a33ec399SRafael J. Wysocki 	acpi_status status;
493a33ec399SRafael J. Wysocki 	int ret;
4941da177e4SLinus Torvalds 
495a33ec399SRafael J. Wysocki 	if (!count || buf[0] != '1')
4961da177e4SLinus Torvalds 		return -EINVAL;
4971da177e4SLinus Torvalds 
498a33ec399SRafael J. Wysocki 	if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
499a33ec399SRafael J. Wysocki 	    && !acpi_device->driver)
500a33ec399SRafael J. Wysocki 		return -ENODEV;
501a33ec399SRafael J. Wysocki 
502a33ec399SRafael J. Wysocki 	status = acpi_get_type(acpi_device->handle, &not_used);
503a33ec399SRafael J. Wysocki 	if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
504a33ec399SRafael J. Wysocki 		return -ENODEV;
505a33ec399SRafael J. Wysocki 
506c4753e57SToshi Kani 	ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
507c4753e57SToshi Kani 	if (!ej_event) {
508c4753e57SToshi Kani 		ret = -ENOMEM;
509a33ec399SRafael J. Wysocki 		goto err_out;
510c4753e57SToshi Kani 	}
511f943db40SRafael J. Wysocki 	acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
512a33ec399SRafael J. Wysocki 				  ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
5135993c467SYinghai Lu 	ej_event->device = acpi_device;
514f943db40SRafael J. Wysocki 	ej_event->event = ACPI_OST_EC_OSPM_EJECT;
515a33ec399SRafael J. Wysocki 	get_device(&acpi_device->dev);
5163757b948SRafael J. Wysocki 	status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device, ej_event);
517f943db40SRafael J. Wysocki 	if (ACPI_SUCCESS(status))
518f943db40SRafael J. Wysocki 		return count;
519f943db40SRafael J. Wysocki 
5203757b948SRafael J. Wysocki 	put_device(&acpi_device->dev);
5213757b948SRafael J. Wysocki 	kfree(ej_event);
522a33ec399SRafael J. Wysocki 	ret = status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
523a33ec399SRafael J. Wysocki 
524a33ec399SRafael J. Wysocki  err_out:
525f943db40SRafael J. Wysocki 	acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
526a33ec399SRafael J. Wysocki 				  ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
527f943db40SRafael J. Wysocki 	return ret;
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
530f883d9dbSPatrick Mochel static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
531f883d9dbSPatrick Mochel 
532e49bd2ddSZhang Rui static ssize_t
533e49bd2ddSZhang Rui acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
534e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
535e49bd2ddSZhang Rui 
536ea8d82fdSBjorn Helgaas 	return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
537e49bd2ddSZhang Rui }
538e49bd2ddSZhang Rui static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
539e49bd2ddSZhang Rui 
540923d4a45SLv Zheng static ssize_t acpi_device_uid_show(struct device *dev,
541923d4a45SLv Zheng 				    struct device_attribute *attr, char *buf)
542923d4a45SLv Zheng {
543923d4a45SLv Zheng 	struct acpi_device *acpi_dev = to_acpi_device(dev);
544923d4a45SLv Zheng 
545923d4a45SLv Zheng 	return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
546923d4a45SLv Zheng }
547923d4a45SLv Zheng static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
548923d4a45SLv Zheng 
549923d4a45SLv Zheng static ssize_t acpi_device_adr_show(struct device *dev,
550923d4a45SLv Zheng 				    struct device_attribute *attr, char *buf)
551923d4a45SLv Zheng {
552923d4a45SLv Zheng 	struct acpi_device *acpi_dev = to_acpi_device(dev);
553923d4a45SLv Zheng 
554923d4a45SLv Zheng 	return sprintf(buf, "0x%08x\n",
555923d4a45SLv Zheng 		       (unsigned int)(acpi_dev->pnp.bus_address));
556923d4a45SLv Zheng }
557923d4a45SLv Zheng static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
558923d4a45SLv Zheng 
559e49bd2ddSZhang Rui static ssize_t
560e49bd2ddSZhang Rui acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
561e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
562e49bd2ddSZhang Rui 	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
563e49bd2ddSZhang Rui 	int result;
564e49bd2ddSZhang Rui 
565e49bd2ddSZhang Rui 	result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
566e49bd2ddSZhang Rui 	if (result)
567e49bd2ddSZhang Rui 		goto end;
568e49bd2ddSZhang Rui 
569e49bd2ddSZhang Rui 	result = sprintf(buf, "%s\n", (char*)path.pointer);
570e49bd2ddSZhang Rui 	kfree(path.pointer);
571e49bd2ddSZhang Rui end:
572e49bd2ddSZhang Rui 	return result;
573e49bd2ddSZhang Rui }
574e49bd2ddSZhang Rui static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
575e49bd2ddSZhang Rui 
576d1efe3c3SLance Ortiz /* sysfs file that shows description text from the ACPI _STR method */
577d1efe3c3SLance Ortiz static ssize_t description_show(struct device *dev,
578d1efe3c3SLance Ortiz 				struct device_attribute *attr,
579d1efe3c3SLance Ortiz 				char *buf) {
580d1efe3c3SLance Ortiz 	struct acpi_device *acpi_dev = to_acpi_device(dev);
581d1efe3c3SLance Ortiz 	int result;
582d1efe3c3SLance Ortiz 
583d1efe3c3SLance Ortiz 	if (acpi_dev->pnp.str_obj == NULL)
584d1efe3c3SLance Ortiz 		return 0;
585d1efe3c3SLance Ortiz 
586d1efe3c3SLance Ortiz 	/*
587d1efe3c3SLance Ortiz 	 * The _STR object contains a Unicode identifier for a device.
588d1efe3c3SLance Ortiz 	 * We need to convert to utf-8 so it can be displayed.
589d1efe3c3SLance Ortiz 	 */
590d1efe3c3SLance Ortiz 	result = utf16s_to_utf8s(
591d1efe3c3SLance Ortiz 		(wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
592d1efe3c3SLance Ortiz 		acpi_dev->pnp.str_obj->buffer.length,
593d1efe3c3SLance Ortiz 		UTF16_LITTLE_ENDIAN, buf,
594d1efe3c3SLance Ortiz 		PAGE_SIZE);
595d1efe3c3SLance Ortiz 
596d1efe3c3SLance Ortiz 	buf[result++] = '\n';
597d1efe3c3SLance Ortiz 
598d1efe3c3SLance Ortiz 	return result;
599d1efe3c3SLance Ortiz }
600d1efe3c3SLance Ortiz static DEVICE_ATTR(description, 0444, description_show, NULL);
601d1efe3c3SLance Ortiz 
602bb74ac23SYasuaki Ishimatsu static ssize_t
603bb74ac23SYasuaki Ishimatsu acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
604bb74ac23SYasuaki Ishimatsu 		     char *buf) {
605bb74ac23SYasuaki Ishimatsu 	struct acpi_device *acpi_dev = to_acpi_device(dev);
606bb74ac23SYasuaki Ishimatsu 
607bb74ac23SYasuaki Ishimatsu 	return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
608bb74ac23SYasuaki Ishimatsu }
609bb74ac23SYasuaki Ishimatsu static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
610bb74ac23SYasuaki Ishimatsu 
611e49bd2ddSZhang Rui static int acpi_device_setup_files(struct acpi_device *dev)
612f883d9dbSPatrick Mochel {
613d1efe3c3SLance Ortiz 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
614f883d9dbSPatrick Mochel 	acpi_status status;
615bb74ac23SYasuaki Ishimatsu 	unsigned long long sun;
616e49bd2ddSZhang Rui 	int result = 0;
617e49bd2ddSZhang Rui 
618e49bd2ddSZhang Rui 	/*
619e49bd2ddSZhang Rui 	 * Devices gotten from FADT don't have a "path" attribute
620e49bd2ddSZhang Rui 	 */
621e49bd2ddSZhang Rui 	if (dev->handle) {
622e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_path);
623e49bd2ddSZhang Rui 		if (result)
624e49bd2ddSZhang Rui 			goto end;
625e49bd2ddSZhang Rui 	}
626e49bd2ddSZhang Rui 
6272b2ae7c7SThomas Renninger 	if (!list_empty(&dev->pnp.ids)) {
628e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_hid);
629e49bd2ddSZhang Rui 		if (result)
630e49bd2ddSZhang Rui 			goto end;
631f883d9dbSPatrick Mochel 
63229b71a1cSThomas Renninger 		result = device_create_file(&dev->dev, &dev_attr_modalias);
63329b71a1cSThomas Renninger 		if (result)
63429b71a1cSThomas Renninger 			goto end;
6352b2ae7c7SThomas Renninger 	}
63629b71a1cSThomas Renninger 
637f883d9dbSPatrick Mochel 	/*
638d1efe3c3SLance Ortiz 	 * If device has _STR, 'description' file is created
639d1efe3c3SLance Ortiz 	 */
640952c63e9SJiang Liu 	if (acpi_has_method(dev->handle, "_STR")) {
641d1efe3c3SLance Ortiz 		status = acpi_evaluate_object(dev->handle, "_STR",
642d1efe3c3SLance Ortiz 					NULL, &buffer);
643d1efe3c3SLance Ortiz 		if (ACPI_FAILURE(status))
644d1efe3c3SLance Ortiz 			buffer.pointer = NULL;
645d1efe3c3SLance Ortiz 		dev->pnp.str_obj = buffer.pointer;
646d1efe3c3SLance Ortiz 		result = device_create_file(&dev->dev, &dev_attr_description);
647d1efe3c3SLance Ortiz 		if (result)
648d1efe3c3SLance Ortiz 			goto end;
649d1efe3c3SLance Ortiz 	}
650d1efe3c3SLance Ortiz 
651d4e1a692SToshi Kani 	if (dev->pnp.type.bus_address)
652923d4a45SLv Zheng 		result = device_create_file(&dev->dev, &dev_attr_adr);
653923d4a45SLv Zheng 	if (dev->pnp.unique_id)
654923d4a45SLv Zheng 		result = device_create_file(&dev->dev, &dev_attr_uid);
655923d4a45SLv Zheng 
656bb74ac23SYasuaki Ishimatsu 	status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
657bb74ac23SYasuaki Ishimatsu 	if (ACPI_SUCCESS(status)) {
658bb74ac23SYasuaki Ishimatsu 		dev->pnp.sun = (unsigned long)sun;
659bb74ac23SYasuaki Ishimatsu 		result = device_create_file(&dev->dev, &dev_attr_sun);
660bb74ac23SYasuaki Ishimatsu 		if (result)
661bb74ac23SYasuaki Ishimatsu 			goto end;
662bb74ac23SYasuaki Ishimatsu 	} else {
663bb74ac23SYasuaki Ishimatsu 		dev->pnp.sun = (unsigned long)-1;
664bb74ac23SYasuaki Ishimatsu 	}
665bb74ac23SYasuaki Ishimatsu 
666d1efe3c3SLance Ortiz         /*
667f883d9dbSPatrick Mochel          * If device has _EJ0, 'eject' file is created that is used to trigger
668f883d9dbSPatrick Mochel          * hot-removal function from userland.
669f883d9dbSPatrick Mochel          */
670952c63e9SJiang Liu 	if (acpi_has_method(dev->handle, "_EJ0")) {
671e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_eject);
672836aedb1SRafael J. Wysocki 		if (result)
673836aedb1SRafael J. Wysocki 			return result;
674836aedb1SRafael J. Wysocki 	}
675836aedb1SRafael J. Wysocki 
676836aedb1SRafael J. Wysocki 	if (dev->flags.power_manageable) {
677836aedb1SRafael J. Wysocki 		result = device_create_file(&dev->dev, &dev_attr_power_state);
678836aedb1SRafael J. Wysocki 		if (result)
679836aedb1SRafael J. Wysocki 			return result;
680836aedb1SRafael J. Wysocki 
681836aedb1SRafael J. Wysocki 		if (dev->power.flags.power_resources)
682836aedb1SRafael J. Wysocki 			result = device_create_file(&dev->dev,
683836aedb1SRafael J. Wysocki 						    &dev_attr_real_power_state);
684836aedb1SRafael J. Wysocki 	}
685836aedb1SRafael J. Wysocki 
686e49bd2ddSZhang Rui end:
687e49bd2ddSZhang Rui 	return result;
688f883d9dbSPatrick Mochel }
689f883d9dbSPatrick Mochel 
690f883d9dbSPatrick Mochel static void acpi_device_remove_files(struct acpi_device *dev)
691f883d9dbSPatrick Mochel {
692836aedb1SRafael J. Wysocki 	if (dev->flags.power_manageable) {
693836aedb1SRafael J. Wysocki 		device_remove_file(&dev->dev, &dev_attr_power_state);
694836aedb1SRafael J. Wysocki 		if (dev->power.flags.power_resources)
695836aedb1SRafael J. Wysocki 			device_remove_file(&dev->dev,
696836aedb1SRafael J. Wysocki 					   &dev_attr_real_power_state);
697836aedb1SRafael J. Wysocki 	}
698836aedb1SRafael J. Wysocki 
699f883d9dbSPatrick Mochel 	/*
700d1efe3c3SLance Ortiz 	 * If device has _STR, remove 'description' file
701d1efe3c3SLance Ortiz 	 */
702952c63e9SJiang Liu 	if (acpi_has_method(dev->handle, "_STR")) {
703d1efe3c3SLance Ortiz 		kfree(dev->pnp.str_obj);
704d1efe3c3SLance Ortiz 		device_remove_file(&dev->dev, &dev_attr_description);
705d1efe3c3SLance Ortiz 	}
706d1efe3c3SLance Ortiz 	/*
707d1efe3c3SLance Ortiz 	 * If device has _EJ0, remove 'eject' file.
708f883d9dbSPatrick Mochel 	 */
709952c63e9SJiang Liu 	if (acpi_has_method(dev->handle, "_EJ0"))
710f883d9dbSPatrick Mochel 		device_remove_file(&dev->dev, &dev_attr_eject);
711e49bd2ddSZhang Rui 
712952c63e9SJiang Liu 	if (acpi_has_method(dev->handle, "_SUN"))
713bb74ac23SYasuaki Ishimatsu 		device_remove_file(&dev->dev, &dev_attr_sun);
714bb74ac23SYasuaki Ishimatsu 
715923d4a45SLv Zheng 	if (dev->pnp.unique_id)
716923d4a45SLv Zheng 		device_remove_file(&dev->dev, &dev_attr_uid);
717d4e1a692SToshi Kani 	if (dev->pnp.type.bus_address)
718923d4a45SLv Zheng 		device_remove_file(&dev->dev, &dev_attr_adr);
71929b71a1cSThomas Renninger 	device_remove_file(&dev->dev, &dev_attr_modalias);
720e49bd2ddSZhang Rui 	device_remove_file(&dev->dev, &dev_attr_hid);
721e49bd2ddSZhang Rui 	if (dev->handle)
722e49bd2ddSZhang Rui 		device_remove_file(&dev->dev, &dev_attr_path);
723f883d9dbSPatrick Mochel }
7241da177e4SLinus Torvalds /* --------------------------------------------------------------------------
7259e89dde2SZhang Rui 			ACPI Bus operations
7261da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
72729b71a1cSThomas Renninger 
728cf761af9SMika Westerberg static const struct acpi_device_id *__acpi_match_device(
729cf761af9SMika Westerberg 	struct acpi_device *device, const struct acpi_device_id *ids)
73029b71a1cSThomas Renninger {
73129b71a1cSThomas Renninger 	const struct acpi_device_id *id;
7327f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hwid;
73329b71a1cSThomas Renninger 
73439a0ad87SZhao Yakui 	/*
73539a0ad87SZhao Yakui 	 * If the device is not present, it is unnecessary to load device
73639a0ad87SZhao Yakui 	 * driver for it.
73739a0ad87SZhao Yakui 	 */
73839a0ad87SZhao Yakui 	if (!device->status.present)
739cf761af9SMika Westerberg 		return NULL;
74039a0ad87SZhao Yakui 
7417f47fa6cSBjorn Helgaas 	for (id = ids; id->id[0]; id++)
7427f47fa6cSBjorn Helgaas 		list_for_each_entry(hwid, &device->pnp.ids, list)
7437f47fa6cSBjorn Helgaas 			if (!strcmp((char *) id->id, hwid->id))
744cf761af9SMika Westerberg 				return id;
74529b71a1cSThomas Renninger 
746cf761af9SMika Westerberg 	return NULL;
747cf761af9SMika Westerberg }
748cf761af9SMika Westerberg 
749cf761af9SMika Westerberg /**
750cf761af9SMika Westerberg  * acpi_match_device - Match a struct device against a given list of ACPI IDs
751cf761af9SMika Westerberg  * @ids: Array of struct acpi_device_id object to match against.
752cf761af9SMika Westerberg  * @dev: The device structure to match.
753cf761af9SMika Westerberg  *
754cf761af9SMika Westerberg  * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
755cf761af9SMika Westerberg  * object for that handle and use that object to match against a given list of
756cf761af9SMika Westerberg  * device IDs.
757cf761af9SMika Westerberg  *
758cf761af9SMika Westerberg  * Return a pointer to the first matching ID on success or %NULL on failure.
759cf761af9SMika Westerberg  */
760cf761af9SMika Westerberg const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
761cf761af9SMika Westerberg 					       const struct device *dev)
762cf761af9SMika Westerberg {
763cf761af9SMika Westerberg 	struct acpi_device *adev;
7640613e1f7SRafael J. Wysocki 	acpi_handle handle = ACPI_HANDLE(dev);
765cf761af9SMika Westerberg 
7660613e1f7SRafael J. Wysocki 	if (!ids || !handle || acpi_bus_get_device(handle, &adev))
767cf761af9SMika Westerberg 		return NULL;
768cf761af9SMika Westerberg 
769cf761af9SMika Westerberg 	return __acpi_match_device(adev, ids);
770cf761af9SMika Westerberg }
771cf761af9SMika Westerberg EXPORT_SYMBOL_GPL(acpi_match_device);
772cf761af9SMika Westerberg 
773cf761af9SMika Westerberg int acpi_match_device_ids(struct acpi_device *device,
774cf761af9SMika Westerberg 			  const struct acpi_device_id *ids)
775cf761af9SMika Westerberg {
776cf761af9SMika Westerberg 	return __acpi_match_device(device, ids) ? 0 : -ENOENT;
77729b71a1cSThomas Renninger }
77829b71a1cSThomas Renninger EXPORT_SYMBOL(acpi_match_device_ids);
77929b71a1cSThomas Renninger 
7800b224527SRafael J. Wysocki static void acpi_free_power_resources_lists(struct acpi_device *device)
7810b224527SRafael J. Wysocki {
7820b224527SRafael J. Wysocki 	int i;
7830b224527SRafael J. Wysocki 
784993cbe59SRafael J. Wysocki 	if (device->wakeup.flags.valid)
785993cbe59SRafael J. Wysocki 		acpi_power_resources_list_free(&device->wakeup.resources);
786993cbe59SRafael J. Wysocki 
7870b224527SRafael J. Wysocki 	if (!device->flags.power_manageable)
7880b224527SRafael J. Wysocki 		return;
7890b224527SRafael J. Wysocki 
7900b224527SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
7910b224527SRafael J. Wysocki 		struct acpi_device_power_state *ps = &device->power.states[i];
7920b224527SRafael J. Wysocki 		acpi_power_resources_list_free(&ps->resources);
7930b224527SRafael J. Wysocki 	}
7947f47fa6cSBjorn Helgaas }
7957f47fa6cSBjorn Helgaas 
7961890a97aSPatrick Mochel static void acpi_device_release(struct device *dev)
7971890a97aSPatrick Mochel {
7981890a97aSPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
7991890a97aSPatrick Mochel 
800c0af4175SToshi Kani 	acpi_free_pnp_ids(&acpi_dev->pnp);
8010b224527SRafael J. Wysocki 	acpi_free_power_resources_lists(acpi_dev);
8021890a97aSPatrick Mochel 	kfree(acpi_dev);
8031890a97aSPatrick Mochel }
8041890a97aSPatrick Mochel 
8055d9464a4SPatrick Mochel static int acpi_bus_match(struct device *dev, struct device_driver *drv)
8061da177e4SLinus Torvalds {
8075d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
8085d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(drv);
8095d9464a4SPatrick Mochel 
810209d3b17SRafael J. Wysocki 	return acpi_dev->flags.match_driver
811805d410fSRafael J. Wysocki 		&& !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
8125d9464a4SPatrick Mochel }
8135d9464a4SPatrick Mochel 
8147eff2e7aSKay Sievers static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
8155d9464a4SPatrick Mochel {
8165d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
8177eff2e7aSKay Sievers 	int len;
8185d9464a4SPatrick Mochel 
8192b2ae7c7SThomas Renninger 	if (list_empty(&acpi_dev->pnp.ids))
8202b2ae7c7SThomas Renninger 		return 0;
8212b2ae7c7SThomas Renninger 
8227eff2e7aSKay Sievers 	if (add_uevent_var(env, "MODALIAS="))
8237eff2e7aSKay Sievers 		return -ENOMEM;
8247eff2e7aSKay Sievers 	len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
8257eff2e7aSKay Sievers 			      sizeof(env->buf) - env->buflen);
8267eff2e7aSKay Sievers 	if (len >= (sizeof(env->buf) - env->buflen))
8277eff2e7aSKay Sievers 		return -ENOMEM;
8287eff2e7aSKay Sievers 	env->buflen += len;
8291da177e4SLinus Torvalds 	return 0;
8301da177e4SLinus Torvalds }
8311da177e4SLinus Torvalds 
83246ec8598SBjorn Helgaas static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
83346ec8598SBjorn Helgaas {
83446ec8598SBjorn Helgaas 	struct acpi_device *device = data;
83546ec8598SBjorn Helgaas 
83646ec8598SBjorn Helgaas 	device->driver->ops.notify(device, event);
83746ec8598SBjorn Helgaas }
83846ec8598SBjorn Helgaas 
83946ec8598SBjorn Helgaas static acpi_status acpi_device_notify_fixed(void *data)
84046ec8598SBjorn Helgaas {
84146ec8598SBjorn Helgaas 	struct acpi_device *device = data;
84246ec8598SBjorn Helgaas 
84353de5356SBjorn Helgaas 	/* Fixed hardware devices have no handles */
84453de5356SBjorn Helgaas 	acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
84546ec8598SBjorn Helgaas 	return AE_OK;
84646ec8598SBjorn Helgaas }
84746ec8598SBjorn Helgaas 
84846ec8598SBjorn Helgaas static int acpi_device_install_notify_handler(struct acpi_device *device)
84946ec8598SBjorn Helgaas {
85046ec8598SBjorn Helgaas 	acpi_status status;
85146ec8598SBjorn Helgaas 
852ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
85346ec8598SBjorn Helgaas 		status =
85446ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
85546ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
85646ec8598SBjorn Helgaas 						     device);
857ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
85846ec8598SBjorn Helgaas 		status =
85946ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
86046ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
86146ec8598SBjorn Helgaas 						     device);
86246ec8598SBjorn Helgaas 	else
86346ec8598SBjorn Helgaas 		status = acpi_install_notify_handler(device->handle,
86446ec8598SBjorn Helgaas 						     ACPI_DEVICE_NOTIFY,
86546ec8598SBjorn Helgaas 						     acpi_device_notify,
86646ec8598SBjorn Helgaas 						     device);
86746ec8598SBjorn Helgaas 
86846ec8598SBjorn Helgaas 	if (ACPI_FAILURE(status))
86946ec8598SBjorn Helgaas 		return -EINVAL;
87046ec8598SBjorn Helgaas 	return 0;
87146ec8598SBjorn Helgaas }
87246ec8598SBjorn Helgaas 
87346ec8598SBjorn Helgaas static void acpi_device_remove_notify_handler(struct acpi_device *device)
87446ec8598SBjorn Helgaas {
875ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
87646ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
87746ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
878ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
87946ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
88046ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
88146ec8598SBjorn Helgaas 	else
88246ec8598SBjorn Helgaas 		acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
88346ec8598SBjorn Helgaas 					   acpi_device_notify);
88446ec8598SBjorn Helgaas }
88546ec8598SBjorn Helgaas 
8865d9464a4SPatrick Mochel static int acpi_device_probe(struct device *dev)
8875d9464a4SPatrick Mochel {
8885d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
8895d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
8905d9464a4SPatrick Mochel 	int ret;
8915d9464a4SPatrick Mochel 
89224071f47SRafael J. Wysocki 	if (acpi_dev->handler)
89324071f47SRafael J. Wysocki 		return -EINVAL;
89424071f47SRafael J. Wysocki 
895d9e455f5SRafael J. Wysocki 	if (!acpi_drv->ops.add)
896d9e455f5SRafael J. Wysocki 		return -ENOSYS;
897d9e455f5SRafael J. Wysocki 
898d9e455f5SRafael J. Wysocki 	ret = acpi_drv->ops.add(acpi_dev);
899d9e455f5SRafael J. Wysocki 	if (ret)
900d9e455f5SRafael J. Wysocki 		return ret;
901d9e455f5SRafael J. Wysocki 
902d9e455f5SRafael J. Wysocki 	acpi_dev->driver = acpi_drv;
903d9e455f5SRafael J. Wysocki 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
904d9e455f5SRafael J. Wysocki 			  "Driver [%s] successfully bound to device [%s]\n",
905d9e455f5SRafael J. Wysocki 			  acpi_drv->name, acpi_dev->pnp.bus_id));
906d9e455f5SRafael J. Wysocki 
90746ec8598SBjorn Helgaas 	if (acpi_drv->ops.notify) {
90846ec8598SBjorn Helgaas 		ret = acpi_device_install_notify_handler(acpi_dev);
90946ec8598SBjorn Helgaas 		if (ret) {
91046ec8598SBjorn Helgaas 			if (acpi_drv->ops.remove)
91151fac838SRafael J. Wysocki 				acpi_drv->ops.remove(acpi_dev);
912d9e455f5SRafael J. Wysocki 
9135f27ee8eSToshi Kani 			acpi_dev->driver = NULL;
9145f27ee8eSToshi Kani 			acpi_dev->driver_data = NULL;
91546ec8598SBjorn Helgaas 			return ret;
91646ec8598SBjorn Helgaas 		}
91746ec8598SBjorn Helgaas 	}
91846ec8598SBjorn Helgaas 
919d9e455f5SRafael J. Wysocki 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
9205d9464a4SPatrick Mochel 			  acpi_drv->name, acpi_dev->pnp.bus_id));
9215d9464a4SPatrick Mochel 	get_device(dev);
922d9e455f5SRafael J. Wysocki 	return 0;
9235d9464a4SPatrick Mochel }
9245d9464a4SPatrick Mochel 
9255d9464a4SPatrick Mochel static int acpi_device_remove(struct device * dev)
9265d9464a4SPatrick Mochel {
9275d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
9285d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
9295d9464a4SPatrick Mochel 
9305d9464a4SPatrick Mochel 	if (acpi_drv) {
93146ec8598SBjorn Helgaas 		if (acpi_drv->ops.notify)
93246ec8598SBjorn Helgaas 			acpi_device_remove_notify_handler(acpi_dev);
9335d9464a4SPatrick Mochel 		if (acpi_drv->ops.remove)
93451fac838SRafael J. Wysocki 			acpi_drv->ops.remove(acpi_dev);
9355d9464a4SPatrick Mochel 	}
9365d9464a4SPatrick Mochel 	acpi_dev->driver = NULL;
937db89b4f0SPavel Machek 	acpi_dev->driver_data = NULL;
9385d9464a4SPatrick Mochel 
9395d9464a4SPatrick Mochel 	put_device(dev);
9405d9464a4SPatrick Mochel 	return 0;
9415d9464a4SPatrick Mochel }
9425d9464a4SPatrick Mochel 
94355955aadSDavid Brownell struct bus_type acpi_bus_type = {
9449e89dde2SZhang Rui 	.name		= "acpi",
9455d9464a4SPatrick Mochel 	.match		= acpi_bus_match,
9465d9464a4SPatrick Mochel 	.probe		= acpi_device_probe,
9475d9464a4SPatrick Mochel 	.remove		= acpi_device_remove,
9485d9464a4SPatrick Mochel 	.uevent		= acpi_device_uevent,
9491da177e4SLinus Torvalds };
9501da177e4SLinus Torvalds 
951caf5c03fSRafael J. Wysocki static void acpi_bus_data_handler(acpi_handle handle, void *context)
952caf5c03fSRafael J. Wysocki {
953caf5c03fSRafael J. Wysocki 	/* Intentionally empty. */
954caf5c03fSRafael J. Wysocki }
955caf5c03fSRafael J. Wysocki 
956caf5c03fSRafael J. Wysocki int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
957caf5c03fSRafael J. Wysocki {
958caf5c03fSRafael J. Wysocki 	acpi_status status;
959caf5c03fSRafael J. Wysocki 
960caf5c03fSRafael J. Wysocki 	if (!device)
961caf5c03fSRafael J. Wysocki 		return -EINVAL;
962caf5c03fSRafael J. Wysocki 
963caf5c03fSRafael J. Wysocki 	status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
964caf5c03fSRafael J. Wysocki 	if (ACPI_FAILURE(status) || !*device) {
965caf5c03fSRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
966caf5c03fSRafael J. Wysocki 				  handle));
967caf5c03fSRafael J. Wysocki 		return -ENODEV;
968caf5c03fSRafael J. Wysocki 	}
969caf5c03fSRafael J. Wysocki 	return 0;
970caf5c03fSRafael J. Wysocki }
971caf5c03fSRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_bus_get_device);
972caf5c03fSRafael J. Wysocki 
973cf860be6SRafael J. Wysocki int acpi_device_add(struct acpi_device *device,
97482c7d5efSRafael J. Wysocki 		    void (*release)(struct device *))
9751da177e4SLinus Torvalds {
976e49bd2ddSZhang Rui 	int result;
977e49bd2ddSZhang Rui 	struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
978e49bd2ddSZhang Rui 	int found = 0;
97966b7ed40SBjorn Helgaas 
980d43e167dSRafael J. Wysocki 	if (device->handle) {
981d43e167dSRafael J. Wysocki 		acpi_status status;
982d43e167dSRafael J. Wysocki 
983d43e167dSRafael J. Wysocki 		status = acpi_attach_data(device->handle, acpi_bus_data_handler,
984d43e167dSRafael J. Wysocki 					  device);
985d43e167dSRafael J. Wysocki 		if (ACPI_FAILURE(status)) {
986d43e167dSRafael J. Wysocki 			acpi_handle_err(device->handle,
987d43e167dSRafael J. Wysocki 					"Unable to attach device data\n");
988d43e167dSRafael J. Wysocki 			return -ENODEV;
989d43e167dSRafael J. Wysocki 		}
990d43e167dSRafael J. Wysocki 	}
991d43e167dSRafael J. Wysocki 
9921da177e4SLinus Torvalds 	/*
9931da177e4SLinus Torvalds 	 * Linkage
9941da177e4SLinus Torvalds 	 * -------
9951da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
9961da177e4SLinus Torvalds 	 */
9971da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->children);
9981da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->node);
9991da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
10001033f904SLan Tianyu 	INIT_LIST_HEAD(&device->physical_node_list);
10011033f904SLan Tianyu 	mutex_init(&device->physical_node_lock);
1002bc9b6407SRafael J. Wysocki 	INIT_LIST_HEAD(&device->power_dependent);
10031da177e4SLinus Torvalds 
1004e49bd2ddSZhang Rui 	new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1005e49bd2ddSZhang Rui 	if (!new_bus_id) {
1006d43e167dSRafael J. Wysocki 		pr_err(PREFIX "Memory allocation error\n");
1007d43e167dSRafael J. Wysocki 		result = -ENOMEM;
1008d43e167dSRafael J. Wysocki 		goto err_detach;
1009e49bd2ddSZhang Rui 	}
1010e49bd2ddSZhang Rui 
10119090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
1012e49bd2ddSZhang Rui 	/*
1013e49bd2ddSZhang Rui 	 * Find suitable bus_id and instance number in acpi_bus_id_list
1014e49bd2ddSZhang Rui 	 * If failed, create one and link it into acpi_bus_id_list
1015e49bd2ddSZhang Rui 	 */
1016e49bd2ddSZhang Rui 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
10171131b938SBjorn Helgaas 		if (!strcmp(acpi_device_bus_id->bus_id,
10181131b938SBjorn Helgaas 			    acpi_device_hid(device))) {
1019e49bd2ddSZhang Rui 			acpi_device_bus_id->instance_no++;
1020e49bd2ddSZhang Rui 			found = 1;
1021e49bd2ddSZhang Rui 			kfree(new_bus_id);
1022e49bd2ddSZhang Rui 			break;
1023e49bd2ddSZhang Rui 		}
1024e49bd2ddSZhang Rui 	}
1025e49bd2ddSZhang Rui 	if (!found) {
1026e49bd2ddSZhang Rui 		acpi_device_bus_id = new_bus_id;
10271131b938SBjorn Helgaas 		strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
1028e49bd2ddSZhang Rui 		acpi_device_bus_id->instance_no = 0;
1029e49bd2ddSZhang Rui 		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1030e49bd2ddSZhang Rui 	}
10310794469dSKay Sievers 	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
1032e49bd2ddSZhang Rui 
103333b57150SLen Brown 	if (device->parent)
10341da177e4SLinus Torvalds 		list_add_tail(&device->node, &device->parent->children);
103533b57150SLen Brown 
10361da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
10371da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
10389090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
10391da177e4SLinus Torvalds 
10401890a97aSPatrick Mochel 	if (device->parent)
104166b7ed40SBjorn Helgaas 		device->dev.parent = &device->parent->dev;
10421890a97aSPatrick Mochel 	device->dev.bus = &acpi_bus_type;
104382c7d5efSRafael J. Wysocki 	device->dev.release = release;
1044cf860be6SRafael J. Wysocki 	result = device_add(&device->dev);
1045e49bd2ddSZhang Rui 	if (result) {
10468b12b922SAlex Chiang 		dev_err(&device->dev, "Error registering device\n");
1047d43e167dSRafael J. Wysocki 		goto err;
1048e49bd2ddSZhang Rui 	}
1049f883d9dbSPatrick Mochel 
1050e49bd2ddSZhang Rui 	result = acpi_device_setup_files(device);
1051e49bd2ddSZhang Rui 	if (result)
10520794469dSKay Sievers 		printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
10530794469dSKay Sievers 		       dev_name(&device->dev));
1054e49bd2ddSZhang Rui 
1055e49bd2ddSZhang Rui 	return 0;
1056d43e167dSRafael J. Wysocki 
1057d43e167dSRafael J. Wysocki  err:
10589090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
105933b57150SLen Brown 	if (device->parent)
1060e49bd2ddSZhang Rui 		list_del(&device->node);
1061e49bd2ddSZhang Rui 	list_del(&device->wakeup_list);
10629090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
1063d43e167dSRafael J. Wysocki 
1064d43e167dSRafael J. Wysocki  err_detach:
1065d43e167dSRafael J. Wysocki 	acpi_detach_data(device->handle, acpi_bus_data_handler);
1066e49bd2ddSZhang Rui 	return result;
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
1069b17b537aSRafael J. Wysocki static void acpi_device_unregister(struct acpi_device *device)
10701da177e4SLinus Torvalds {
10719090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
107233b57150SLen Brown 	if (device->parent)
10731da177e4SLinus Torvalds 		list_del(&device->node);
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	list_del(&device->wakeup_list);
10769090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	acpi_detach_data(device->handle, acpi_bus_data_handler);
10791890a97aSPatrick Mochel 
1080bc9b6407SRafael J. Wysocki 	acpi_power_add_remove_device(device, false);
1081f883d9dbSPatrick Mochel 	acpi_device_remove_files(device);
1082b1c0f99bSRafael J. Wysocki 	if (device->remove)
1083b1c0f99bSRafael J. Wysocki 		device->remove(device);
1084b1c0f99bSRafael J. Wysocki 
1085cf860be6SRafael J. Wysocki 	device_del(&device->dev);
1086bc9b6407SRafael J. Wysocki 	/*
10870aa120a0SRafael J. Wysocki 	 * Transition the device to D3cold to drop the reference counts of all
10880aa120a0SRafael J. Wysocki 	 * power resources the device depends on and turn off the ones that have
10890aa120a0SRafael J. Wysocki 	 * no more references.
1090bc9b6407SRafael J. Wysocki 	 */
10910aa120a0SRafael J. Wysocki 	acpi_device_set_power(device, ACPI_STATE_D3_COLD);
10923757b948SRafael J. Wysocki 	device->handle = NULL;
1093cf860be6SRafael J. Wysocki 	put_device(&device->dev);
10941da177e4SLinus Torvalds }
10951da177e4SLinus Torvalds 
10969e89dde2SZhang Rui /* --------------------------------------------------------------------------
10979e89dde2SZhang Rui                                  Driver Management
10989e89dde2SZhang Rui    -------------------------------------------------------------------------- */
10991da177e4SLinus Torvalds /**
1100d758a8faSRandy Dunlap  * acpi_bus_register_driver - register a driver with the ACPI bus
1101d758a8faSRandy Dunlap  * @driver: driver being registered
1102d758a8faSRandy Dunlap  *
11031da177e4SLinus Torvalds  * Registers a driver with the ACPI bus.  Searches the namespace for all
11049d9f749bSBjorn Helgaas  * devices that match the driver's criteria and binds.  Returns zero for
11059d9f749bSBjorn Helgaas  * success or a negative error status for failure.
11061da177e4SLinus Torvalds  */
11074be44fcdSLen Brown int acpi_bus_register_driver(struct acpi_driver *driver)
11081da177e4SLinus Torvalds {
11091890a97aSPatrick Mochel 	int ret;
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 	if (acpi_disabled)
1112d550d98dSPatrick Mochel 		return -ENODEV;
11131890a97aSPatrick Mochel 	driver->drv.name = driver->name;
11141890a97aSPatrick Mochel 	driver->drv.bus = &acpi_bus_type;
11151890a97aSPatrick Mochel 	driver->drv.owner = driver->owner;
11161da177e4SLinus Torvalds 
11171890a97aSPatrick Mochel 	ret = driver_register(&driver->drv);
11181890a97aSPatrick Mochel 	return ret;
11191da177e4SLinus Torvalds }
11201da177e4SLinus Torvalds 
11214be44fcdSLen Brown EXPORT_SYMBOL(acpi_bus_register_driver);
11221da177e4SLinus Torvalds 
11231da177e4SLinus Torvalds /**
1124*b27b14ceSHanjun Guo  * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
1125d758a8faSRandy Dunlap  * @driver: driver to unregister
1126d758a8faSRandy Dunlap  *
11271da177e4SLinus Torvalds  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
11281da177e4SLinus Torvalds  * devices that match the driver's criteria and unbinds.
11291da177e4SLinus Torvalds  */
113006ea8e08SBjorn Helgaas void acpi_bus_unregister_driver(struct acpi_driver *driver)
11311da177e4SLinus Torvalds {
11321890a97aSPatrick Mochel 	driver_unregister(&driver->drv);
11331da177e4SLinus Torvalds }
11344be44fcdSLen Brown 
11351da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_unregister_driver);
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds /* --------------------------------------------------------------------------
11381da177e4SLinus Torvalds                                  Device Enumeration
11391da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
11405c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
11415c478f49SBjorn Helgaas {
1142456de893SRafael J. Wysocki 	struct acpi_device *device = NULL;
11435c478f49SBjorn Helgaas 	acpi_status status;
11445c478f49SBjorn Helgaas 
11455c478f49SBjorn Helgaas 	/*
11465c478f49SBjorn Helgaas 	 * Fixed hardware devices do not appear in the namespace and do not
11475c478f49SBjorn Helgaas 	 * have handles, but we fabricate acpi_devices for them, so we have
11485c478f49SBjorn Helgaas 	 * to deal with them specially.
11495c478f49SBjorn Helgaas 	 */
1150456de893SRafael J. Wysocki 	if (!handle)
11515c478f49SBjorn Helgaas 		return acpi_root;
11525c478f49SBjorn Helgaas 
11535c478f49SBjorn Helgaas 	do {
11545c478f49SBjorn Helgaas 		status = acpi_get_parent(handle, &handle);
11555c478f49SBjorn Helgaas 		if (ACPI_FAILURE(status))
1156456de893SRafael J. Wysocki 			return status == AE_NULL_ENTRY ? NULL : acpi_root;
1157456de893SRafael J. Wysocki 	} while (acpi_bus_get_device(handle, &device));
11585c478f49SBjorn Helgaas 	return device;
11595c478f49SBjorn Helgaas }
11605c478f49SBjorn Helgaas 
1161c8f7a62cSLen Brown acpi_status
1162c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1163c8f7a62cSLen Brown {
1164c8f7a62cSLen Brown 	acpi_status status;
1165c8f7a62cSLen Brown 	acpi_handle tmp;
1166c8f7a62cSLen Brown 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1167c8f7a62cSLen Brown 	union acpi_object *obj;
1168c8f7a62cSLen Brown 
1169c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_EJD", &tmp);
1170c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
1171c8f7a62cSLen Brown 		return status;
1172c8f7a62cSLen Brown 
1173c8f7a62cSLen Brown 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1174c8f7a62cSLen Brown 	if (ACPI_SUCCESS(status)) {
1175c8f7a62cSLen Brown 		obj = buffer.pointer;
11763b5fee59SHolger Macht 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
11773b5fee59SHolger Macht 					 ejd);
1178c8f7a62cSLen Brown 		kfree(buffer.pointer);
1179c8f7a62cSLen Brown 	}
1180c8f7a62cSLen Brown 	return status;
1181c8f7a62cSLen Brown }
1182c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1183c8f7a62cSLen Brown 
1184e88c9c60SRafael J. Wysocki static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1185b581a7f9SRafael J. Wysocki 					struct acpi_device_wakeup *wakeup)
11861da177e4SLinus Torvalds {
1187b581a7f9SRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1188b581a7f9SRafael J. Wysocki 	union acpi_object *package = NULL;
11891da177e4SLinus Torvalds 	union acpi_object *element = NULL;
1190b581a7f9SRafael J. Wysocki 	acpi_status status;
1191e88c9c60SRafael J. Wysocki 	int err = -ENODATA;
11921da177e4SLinus Torvalds 
1193b581a7f9SRafael J. Wysocki 	if (!wakeup)
1194e88c9c60SRafael J. Wysocki 		return -EINVAL;
11951da177e4SLinus Torvalds 
1196993cbe59SRafael J. Wysocki 	INIT_LIST_HEAD(&wakeup->resources);
11971da177e4SLinus Torvalds 
1198b581a7f9SRafael J. Wysocki 	/* _PRW */
1199b581a7f9SRafael J. Wysocki 	status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1200b581a7f9SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
1201b581a7f9SRafael J. Wysocki 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
1202e88c9c60SRafael J. Wysocki 		return err;
1203b581a7f9SRafael J. Wysocki 	}
1204b581a7f9SRafael J. Wysocki 
1205b581a7f9SRafael J. Wysocki 	package = (union acpi_object *)buffer.pointer;
1206b581a7f9SRafael J. Wysocki 
1207e88c9c60SRafael J. Wysocki 	if (!package || package->package.count < 2)
1208b581a7f9SRafael J. Wysocki 		goto out;
1209b581a7f9SRafael J. Wysocki 
12101da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
1211e88c9c60SRafael J. Wysocki 	if (!element)
1212b581a7f9SRafael J. Wysocki 		goto out;
1213e88c9c60SRafael J. Wysocki 
12141da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
12151da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
12161da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
12171da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
1218e88c9c60SRafael J. Wysocki 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
1219b581a7f9SRafael J. Wysocki 			goto out;
1220e88c9c60SRafael J. Wysocki 
1221b581a7f9SRafael J. Wysocki 		wakeup->gpe_device =
12221da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
1223b581a7f9SRafael J. Wysocki 		wakeup->gpe_number =
12241da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
12251da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
1226b581a7f9SRafael J. Wysocki 		wakeup->gpe_device = NULL;
1227b581a7f9SRafael J. Wysocki 		wakeup->gpe_number = element->integer.value;
1228b581a7f9SRafael J. Wysocki 	} else {
1229b581a7f9SRafael J. Wysocki 		goto out;
1230b581a7f9SRafael J. Wysocki 	}
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
1233e88c9c60SRafael J. Wysocki 	if (element->type != ACPI_TYPE_INTEGER)
1234b581a7f9SRafael J. Wysocki 		goto out;
1235e88c9c60SRafael J. Wysocki 
1236b581a7f9SRafael J. Wysocki 	wakeup->sleep_state = element->integer.value;
12371da177e4SLinus Torvalds 
1238e88c9c60SRafael J. Wysocki 	err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1239e88c9c60SRafael J. Wysocki 	if (err)
1240b581a7f9SRafael J. Wysocki 		goto out;
12411da177e4SLinus Torvalds 
12420596a52bSRafael J. Wysocki 	if (!list_empty(&wakeup->resources)) {
12430596a52bSRafael J. Wysocki 		int sleep_state;
12449874647bSRafael J. Wysocki 
1245b5d667ebSRafael J. Wysocki 		err = acpi_power_wakeup_list_init(&wakeup->resources,
1246b5d667ebSRafael J. Wysocki 						  &sleep_state);
1247b5d667ebSRafael J. Wysocki 		if (err) {
1248b5d667ebSRafael J. Wysocki 			acpi_handle_warn(handle, "Retrieving current states "
1249b5d667ebSRafael J. Wysocki 					 "of wakeup power resources failed\n");
1250b5d667ebSRafael J. Wysocki 			acpi_power_resources_list_free(&wakeup->resources);
1251b5d667ebSRafael J. Wysocki 			goto out;
1252b5d667ebSRafael J. Wysocki 		}
12530596a52bSRafael J. Wysocki 		if (sleep_state < wakeup->sleep_state) {
12540596a52bSRafael J. Wysocki 			acpi_handle_warn(handle, "Overriding _PRW sleep state "
12550596a52bSRafael J. Wysocki 					 "(S%d) by S%d from power resources\n",
12560596a52bSRafael J. Wysocki 					 (int)wakeup->sleep_state, sleep_state);
12570596a52bSRafael J. Wysocki 			wakeup->sleep_state = sleep_state;
12580596a52bSRafael J. Wysocki 		}
12590596a52bSRafael J. Wysocki 	}
1260bba63a29SLin Ming 	acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
1261b581a7f9SRafael J. Wysocki 
1262b581a7f9SRafael J. Wysocki  out:
1263b581a7f9SRafael J. Wysocki 	kfree(buffer.pointer);
1264e88c9c60SRafael J. Wysocki 	return err;
12651da177e4SLinus Torvalds }
12661da177e4SLinus Torvalds 
1267f517709dSRafael J. Wysocki static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
12681da177e4SLinus Torvalds {
126929b71a1cSThomas Renninger 	struct acpi_device_id button_device_ids[] = {
127029b71a1cSThomas Renninger 		{"PNP0C0C", 0},
1271b7e38304SZhang Rui 		{"PNP0C0D", 0},
127229b71a1cSThomas Renninger 		{"PNP0C0E", 0},
127329b71a1cSThomas Renninger 		{"", 0},
127429b71a1cSThomas Renninger 	};
1275f517709dSRafael J. Wysocki 	acpi_status status;
1276f517709dSRafael J. Wysocki 	acpi_event_status event_status;
1277f517709dSRafael J. Wysocki 
1278b67ea761SRafael J. Wysocki 	device->wakeup.flags.notifier_present = 0;
1279f517709dSRafael J. Wysocki 
1280f517709dSRafael J. Wysocki 	/* Power button, Lid switch always enable wakeup */
1281f517709dSRafael J. Wysocki 	if (!acpi_match_device_ids(device, button_device_ids)) {
1282f517709dSRafael J. Wysocki 		device->wakeup.flags.run_wake = 1;
1283b7e38304SZhang Rui 		if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1284b7e38304SZhang Rui 			/* Do not use Lid/sleep button for S5 wakeup */
1285b7e38304SZhang Rui 			if (device->wakeup.sleep_state == ACPI_STATE_S5)
1286b7e38304SZhang Rui 				device->wakeup.sleep_state = ACPI_STATE_S4;
1287b7e38304SZhang Rui 		}
1288f2b56bc8SRafael J. Wysocki 		device_set_wakeup_capable(&device->dev, true);
1289f517709dSRafael J. Wysocki 		return;
1290f517709dSRafael J. Wysocki 	}
1291f517709dSRafael J. Wysocki 
1292e8e18c95SRafael J. Wysocki 	status = acpi_get_gpe_status(device->wakeup.gpe_device,
1293e8e18c95SRafael J. Wysocki 					device->wakeup.gpe_number,
12940f849d2cSLin Ming 						&event_status);
1295f517709dSRafael J. Wysocki 	if (status == AE_OK)
1296f517709dSRafael J. Wysocki 		device->wakeup.flags.run_wake =
1297f517709dSRafael J. Wysocki 				!!(event_status & ACPI_EVENT_FLAG_HANDLE);
1298f517709dSRafael J. Wysocki }
1299f517709dSRafael J. Wysocki 
1300d57d09a4SRafael J. Wysocki static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1301f517709dSRafael J. Wysocki {
1302e88c9c60SRafael J. Wysocki 	int err;
130329b71a1cSThomas Renninger 
1304d57d09a4SRafael J. Wysocki 	/* Presence of _PRW indicates wake capable */
1305952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PRW"))
1306d57d09a4SRafael J. Wysocki 		return;
1307d57d09a4SRafael J. Wysocki 
1308e88c9c60SRafael J. Wysocki 	err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1309b581a7f9SRafael J. Wysocki 							   &device->wakeup);
1310e88c9c60SRafael J. Wysocki 	if (err) {
1311e88c9c60SRafael J. Wysocki 		dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1312d57d09a4SRafael J. Wysocki 		return;
13131da177e4SLinus Torvalds 	}
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 	device->wakeup.flags.valid = 1;
13169b83ccd2SRafael J. Wysocki 	device->wakeup.prepare_count = 0;
1317f517709dSRafael J. Wysocki 	acpi_bus_set_run_wake_flags(device);
1318729b2bdbSZhao Yakui 	/* Call _PSW/_DSW object to disable its ability to wake the sleeping
1319729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
1320729b2bdbSZhao Yakui 	 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1321729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
1322729b2bdbSZhao Yakui 	 * present will the _PSW object used.
1323729b2bdbSZhao Yakui 	 */
1324e88c9c60SRafael J. Wysocki 	err = acpi_device_sleep_wake(device, 0, 0, 0);
1325e88c9c60SRafael J. Wysocki 	if (err)
132677e76609SRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
132777e76609SRafael J. Wysocki 				"error in _DSW or _PSW evaluation\n"));
13281da177e4SLinus Torvalds }
13291da177e4SLinus Torvalds 
1330f33ce568SRafael J. Wysocki static void acpi_bus_init_power_state(struct acpi_device *device, int state)
13311da177e4SLinus Torvalds {
1332f33ce568SRafael J. Wysocki 	struct acpi_device_power_state *ps = &device->power.states[state];
1333ef85bdbeSRafael J. Wysocki 	char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1334ef85bdbeSRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1335f33ce568SRafael J. Wysocki 	acpi_status status;
13369e89dde2SZhang Rui 
1337f33ce568SRafael J. Wysocki 	INIT_LIST_HEAD(&ps->resources);
1338f33ce568SRafael J. Wysocki 
1339ef85bdbeSRafael J. Wysocki 	/* Evaluate "_PRx" to get referenced power resources */
1340ef85bdbeSRafael J. Wysocki 	status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1341ef85bdbeSRafael J. Wysocki 	if (ACPI_SUCCESS(status)) {
1342ef85bdbeSRafael J. Wysocki 		union acpi_object *package = buffer.pointer;
1343f33ce568SRafael J. Wysocki 
1344ef85bdbeSRafael J. Wysocki 		if (buffer.length && package
1345ef85bdbeSRafael J. Wysocki 		    && package->type == ACPI_TYPE_PACKAGE
1346ef85bdbeSRafael J. Wysocki 		    && package->package.count) {
1347e88c9c60SRafael J. Wysocki 			int err = acpi_extract_power_resources(package, 0,
1348ef85bdbeSRafael J. Wysocki 							       &ps->resources);
1349e88c9c60SRafael J. Wysocki 			if (!err)
1350f33ce568SRafael J. Wysocki 				device->power.flags.power_resources = 1;
1351f33ce568SRafael J. Wysocki 		}
1352ef85bdbeSRafael J. Wysocki 		ACPI_FREE(buffer.pointer);
1353f33ce568SRafael J. Wysocki 	}
1354f33ce568SRafael J. Wysocki 
1355f33ce568SRafael J. Wysocki 	/* Evaluate "_PSx" to see if we can do explicit sets */
1356ef85bdbeSRafael J. Wysocki 	pathname[2] = 'S';
1357952c63e9SJiang Liu 	if (acpi_has_method(device->handle, pathname))
1358f33ce568SRafael J. Wysocki 		ps->flags.explicit_set = 1;
1359f33ce568SRafael J. Wysocki 
1360f33ce568SRafael J. Wysocki 	/*
1361f33ce568SRafael J. Wysocki 	 * State is valid if there are means to put the device into it.
1362f33ce568SRafael J. Wysocki 	 * D3hot is only valid if _PR3 present.
1363f33ce568SRafael J. Wysocki 	 */
1364ef85bdbeSRafael J. Wysocki 	if (!list_empty(&ps->resources)
1365f33ce568SRafael J. Wysocki 	    || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1366f33ce568SRafael J. Wysocki 		ps->flags.valid = 1;
1367f33ce568SRafael J. Wysocki 		ps->flags.os_accessible = 1;
1368f33ce568SRafael J. Wysocki 	}
1369f33ce568SRafael J. Wysocki 
1370f33ce568SRafael J. Wysocki 	ps->power = -1;		/* Unknown - driver assigned */
1371f33ce568SRafael J. Wysocki 	ps->latency = -1;	/* Unknown - driver assigned */
1372f33ce568SRafael J. Wysocki }
1373f33ce568SRafael J. Wysocki 
1374d43e167dSRafael J. Wysocki static void acpi_bus_get_power_flags(struct acpi_device *device)
13751da177e4SLinus Torvalds {
13768bc5053bSRafael J. Wysocki 	u32 i;
13771da177e4SLinus Torvalds 
1378d43e167dSRafael J. Wysocki 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
1379952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PS0") &&
1380952c63e9SJiang Liu 	    !acpi_has_method(device->handle, "_PR0"))
1381d43e167dSRafael J. Wysocki 		return;
1382d43e167dSRafael J. Wysocki 
1383d43e167dSRafael J. Wysocki 	device->flags.power_manageable = 1;
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	/*
13869e89dde2SZhang Rui 	 * Power Management Flags
13871da177e4SLinus Torvalds 	 */
1388952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_PSC"))
13899e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
1390952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_IRC"))
13919e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	/*
13949e89dde2SZhang Rui 	 * Enumerate supported power management states
13951da177e4SLinus Torvalds 	 */
1396f33ce568SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1397f33ce568SRafael J. Wysocki 		acpi_bus_init_power_state(device, i);
13981da177e4SLinus Torvalds 
13990b224527SRafael J. Wysocki 	INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
14009e89dde2SZhang Rui 
14019e89dde2SZhang Rui 	/* Set defaults for D0 and D3 states (always valid) */
14029e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
14039e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
14048ad928d5SRafael J. Wysocki 	device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
14058ad928d5SRafael J. Wysocki 	device->power.states[ACPI_STATE_D3_COLD].power = 0;
14069e89dde2SZhang Rui 
14075c7dd710SRafael J. Wysocki 	/* Set D3cold's explicit_set flag if _PS3 exists. */
14085c7dd710SRafael J. Wysocki 	if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
14095c7dd710SRafael J. Wysocki 		device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
14105c7dd710SRafael J. Wysocki 
14111399dfcdSAaron Lu 	/* Presence of _PS3 or _PRx means we can put the device into D3 cold */
14121399dfcdSAaron Lu 	if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
14131399dfcdSAaron Lu 			device->power.flags.power_resources)
14141399dfcdSAaron Lu 		device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
14151399dfcdSAaron Lu 
1416b3785492SRafael J. Wysocki 	if (acpi_bus_init_power(device)) {
1417b3785492SRafael J. Wysocki 		acpi_free_power_resources_lists(device);
1418b3785492SRafael J. Wysocki 		device->flags.power_manageable = 0;
1419b3785492SRafael J. Wysocki 	}
14209e89dde2SZhang Rui }
1421c8f7a62cSLen Brown 
1422d43e167dSRafael J. Wysocki static void acpi_bus_get_flags(struct acpi_device *device)
14231da177e4SLinus Torvalds {
14241da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
1425952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_STA"))
14261da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
1429952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_RMV"))
14301da177e4SLinus Torvalds 		device->flags.removable = 1;
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
1433952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_EJD") ||
1434952c63e9SJiang Liu 	    acpi_has_method(device->handle, "_EJ0"))
14351da177e4SLinus Torvalds 		device->flags.ejectable = 1;
14361da177e4SLinus Torvalds }
14371da177e4SLinus Torvalds 
1438c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device)
14391da177e4SLinus Torvalds {
14401da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
14411da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
14421da177e4SLinus Torvalds 	int i = 0;
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	/*
14451da177e4SLinus Torvalds 	 * Bus ID
14461da177e4SLinus Torvalds 	 * ------
14471da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
14481da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
14491da177e4SLinus Torvalds 	 */
1450859ac9a4SBjorn Helgaas 	if (ACPI_IS_ROOT_DEVICE(device)) {
14511da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
1452859ac9a4SBjorn Helgaas 		return;
1453859ac9a4SBjorn Helgaas 	}
1454859ac9a4SBjorn Helgaas 
1455859ac9a4SBjorn Helgaas 	switch (device->device_type) {
14561da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
14571da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
14581da177e4SLinus Torvalds 		break;
14591da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
14601da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
14611da177e4SLinus Torvalds 		break;
14621da177e4SLinus Torvalds 	default:
146366b7ed40SBjorn Helgaas 		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
14641da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
14651da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
14661da177e4SLinus Torvalds 			if (bus_id[i] == '_')
14671da177e4SLinus Torvalds 				bus_id[i] = '\0';
14681da177e4SLinus Torvalds 			else
14691da177e4SLinus Torvalds 				break;
14701da177e4SLinus Torvalds 		}
14711da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
14721da177e4SLinus Torvalds 		break;
14731da177e4SLinus Torvalds 	}
14741da177e4SLinus Torvalds }
14751da177e4SLinus Torvalds 
147654735266SZhang Rui /*
1477ebf4df8dSJiang Liu  * acpi_ata_match - see if an acpi object is an ATA device
1478ebf4df8dSJiang Liu  *
1479ebf4df8dSJiang Liu  * If an acpi object has one of the ACPI ATA methods defined,
1480ebf4df8dSJiang Liu  * then we can safely call it an ATA device.
1481ebf4df8dSJiang Liu  */
1482ebf4df8dSJiang Liu bool acpi_ata_match(acpi_handle handle)
1483ebf4df8dSJiang Liu {
1484ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_GTF") ||
1485ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_GTM") ||
1486ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_STM") ||
1487ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_SDD");
1488ebf4df8dSJiang Liu }
1489ebf4df8dSJiang Liu 
1490ebf4df8dSJiang Liu /*
1491d4e1a692SToshi Kani  * acpi_bay_match - see if an acpi object is an ejectable driver bay
149254735266SZhang Rui  *
149354735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
149454735266SZhang Rui  * then we can safely call it an ejectable drive bay
149554735266SZhang Rui  */
1496ebf4df8dSJiang Liu bool acpi_bay_match(acpi_handle handle)
1497d4e1a692SToshi Kani {
149854735266SZhang Rui 	acpi_handle phandle;
149954735266SZhang Rui 
1500952c63e9SJiang Liu 	if (!acpi_has_method(handle, "_EJ0"))
1501ebf4df8dSJiang Liu 		return false;
1502ebf4df8dSJiang Liu 	if (acpi_ata_match(handle))
1503ebf4df8dSJiang Liu 		return true;
1504ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1505ebf4df8dSJiang Liu 		return false;
150654735266SZhang Rui 
1507ebf4df8dSJiang Liu 	return acpi_ata_match(phandle);
150854735266SZhang Rui }
150954735266SZhang Rui 
15103620f2f2SFrank Seidel /*
1511d4e1a692SToshi Kani  * acpi_dock_match - see if an acpi object has a _DCK method
15123620f2f2SFrank Seidel  */
1513ebf4df8dSJiang Liu bool acpi_dock_match(acpi_handle handle)
15143620f2f2SFrank Seidel {
1515ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_DCK");
15163620f2f2SFrank Seidel }
15173620f2f2SFrank Seidel 
1518620e112cSThomas Renninger const char *acpi_device_hid(struct acpi_device *device)
151915b8dd53SBob Moore {
15207f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hid;
152115b8dd53SBob Moore 
15222b2ae7c7SThomas Renninger 	if (list_empty(&device->pnp.ids))
15232b2ae7c7SThomas Renninger 		return dummy_hid;
15242b2ae7c7SThomas Renninger 
15257f47fa6cSBjorn Helgaas 	hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
15267f47fa6cSBjorn Helgaas 	return hid->id;
15277f47fa6cSBjorn Helgaas }
15287f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid);
152915b8dd53SBob Moore 
1530d4e1a692SToshi Kani static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
15317f47fa6cSBjorn Helgaas {
15327f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
153315b8dd53SBob Moore 
15347f47fa6cSBjorn Helgaas 	id = kmalloc(sizeof(*id), GFP_KERNEL);
15357f47fa6cSBjorn Helgaas 	if (!id)
15367f47fa6cSBjorn Helgaas 		return;
153715b8dd53SBob Moore 
1538581de59eSThomas Meyer 	id->id = kstrdup(dev_id, GFP_KERNEL);
15397f47fa6cSBjorn Helgaas 	if (!id->id) {
15407f47fa6cSBjorn Helgaas 		kfree(id);
15417f47fa6cSBjorn Helgaas 		return;
154215b8dd53SBob Moore 	}
154315b8dd53SBob Moore 
1544d4e1a692SToshi Kani 	list_add_tail(&id->list, &pnp->ids);
1545d4e1a692SToshi Kani 	pnp->type.hardware_id = 1;
154615b8dd53SBob Moore }
154715b8dd53SBob Moore 
1548222e82acSDarrick J. Wong /*
1549222e82acSDarrick J. Wong  * Old IBM workstations have a DSDT bug wherein the SMBus object
1550222e82acSDarrick J. Wong  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1551222e82acSDarrick J. Wong  * prefix.  Work around this.
1552222e82acSDarrick J. Wong  */
1553ebf4df8dSJiang Liu static bool acpi_ibm_smbus_match(acpi_handle handle)
1554222e82acSDarrick J. Wong {
1555ebf4df8dSJiang Liu 	char node_name[ACPI_PATH_SEGMENT_LENGTH];
1556ebf4df8dSJiang Liu 	struct acpi_buffer path = { sizeof(node_name), node_name };
1557222e82acSDarrick J. Wong 
1558222e82acSDarrick J. Wong 	if (!dmi_name_in_vendors("IBM"))
1559ebf4df8dSJiang Liu 		return false;
1560222e82acSDarrick J. Wong 
1561222e82acSDarrick J. Wong 	/* Look for SMBS object */
1562ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1563ebf4df8dSJiang Liu 	    strcmp("SMBS", path.pointer))
1564ebf4df8dSJiang Liu 		return false;
1565222e82acSDarrick J. Wong 
1566222e82acSDarrick J. Wong 	/* Does it have the necessary (but misnamed) methods? */
1567952c63e9SJiang Liu 	if (acpi_has_method(handle, "SBI") &&
1568952c63e9SJiang Liu 	    acpi_has_method(handle, "SBR") &&
1569952c63e9SJiang Liu 	    acpi_has_method(handle, "SBW"))
1570ebf4df8dSJiang Liu 		return true;
1571ebf4df8dSJiang Liu 
1572ebf4df8dSJiang Liu 	return false;
1573222e82acSDarrick J. Wong }
1574222e82acSDarrick J. Wong 
1575c0af4175SToshi Kani static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1576c0af4175SToshi Kani 				int device_type)
15771da177e4SLinus Torvalds {
15781da177e4SLinus Torvalds 	acpi_status status;
157957f3674fSBjorn Helgaas 	struct acpi_device_info *info;
158078e25fefSLv Zheng 	struct acpi_pnp_device_id_list *cid_list;
15817f47fa6cSBjorn Helgaas 	int i;
15821da177e4SLinus Torvalds 
1583c0af4175SToshi Kani 	switch (device_type) {
15841da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
1585c0af4175SToshi Kani 		if (handle == ACPI_ROOT_OBJECT) {
1586c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SYSTEM_HID);
1587859ac9a4SBjorn Helgaas 			break;
1588859ac9a4SBjorn Helgaas 		}
1589859ac9a4SBjorn Helgaas 
1590c0af4175SToshi Kani 		status = acpi_get_object_info(handle, &info);
15911da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
1592c0af4175SToshi Kani 			pr_err(PREFIX "%s: Error reading device info\n",
1593c0af4175SToshi Kani 					__func__);
15941da177e4SLinus Torvalds 			return;
15951da177e4SLinus Torvalds 		}
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_HID)
1598c0af4175SToshi Kani 			acpi_add_id(pnp, info->hardware_id.string);
159957f3674fSBjorn Helgaas 		if (info->valid & ACPI_VALID_CID) {
160015b8dd53SBob Moore 			cid_list = &info->compatible_id_list;
160157f3674fSBjorn Helgaas 			for (i = 0; i < cid_list->count; i++)
1602c0af4175SToshi Kani 				acpi_add_id(pnp, cid_list->ids[i].string);
160357f3674fSBjorn Helgaas 		}
16041da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
1605c0af4175SToshi Kani 			pnp->bus_address = info->address;
1606c0af4175SToshi Kani 			pnp->type.bus_address = 1;
16071da177e4SLinus Torvalds 		}
1608ccf78040SLv Zheng 		if (info->valid & ACPI_VALID_UID)
1609c0af4175SToshi Kani 			pnp->unique_id = kstrdup(info->unique_id.string,
1610ccf78040SLv Zheng 							GFP_KERNEL);
1611ae843332SZhang Rui 
1612a83893aeSBjorn Helgaas 		kfree(info);
1613a83893aeSBjorn Helgaas 
161457f3674fSBjorn Helgaas 		/*
161557f3674fSBjorn Helgaas 		 * Some devices don't reliably have _HIDs & _CIDs, so add
161657f3674fSBjorn Helgaas 		 * synthetic HIDs to make sure drivers can find them.
16173620f2f2SFrank Seidel 		 */
1618c0af4175SToshi Kani 		if (acpi_is_video_device(handle))
1619c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_VIDEO_HID);
1620ebf4df8dSJiang Liu 		else if (acpi_bay_match(handle))
1621c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_BAY_HID);
1622ebf4df8dSJiang Liu 		else if (acpi_dock_match(handle))
1623c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_DOCK_HID);
1624ebf4df8dSJiang Liu 		else if (acpi_ibm_smbus_match(handle))
1625c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1626c0af4175SToshi Kani 		else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1627c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1628c0af4175SToshi Kani 			strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1629c0af4175SToshi Kani 			strcpy(pnp->device_class, ACPI_BUS_CLASS);
1630b7b30de5SBjorn Helgaas 		}
163154735266SZhang Rui 
16321da177e4SLinus Torvalds 		break;
16331da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
1634c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_POWER_HID);
16351da177e4SLinus Torvalds 		break;
16361da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
1637c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
16381da177e4SLinus Torvalds 		break;
16391da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
1640c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_THERMAL_HID);
16411da177e4SLinus Torvalds 		break;
16421da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
1643c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
16441da177e4SLinus Torvalds 		break;
16451da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
1646c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
16471da177e4SLinus Torvalds 		break;
16481da177e4SLinus Torvalds 	}
16491da177e4SLinus Torvalds }
16501da177e4SLinus Torvalds 
1651c0af4175SToshi Kani void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1652c0af4175SToshi Kani {
1653c0af4175SToshi Kani 	struct acpi_hardware_id *id, *tmp;
1654c0af4175SToshi Kani 
1655c0af4175SToshi Kani 	list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1656c0af4175SToshi Kani 		kfree(id->id);
1657c0af4175SToshi Kani 		kfree(id);
1658c0af4175SToshi Kani 	}
1659c0af4175SToshi Kani 	kfree(pnp->unique_id);
1660c0af4175SToshi Kani }
1661c0af4175SToshi Kani 
166282c7d5efSRafael J. Wysocki void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1663d43e167dSRafael J. Wysocki 			     int type, unsigned long long sta)
16641da177e4SLinus Torvalds {
1665d43e167dSRafael J. Wysocki 	INIT_LIST_HEAD(&device->pnp.ids);
1666d43e167dSRafael J. Wysocki 	device->device_type = type;
1667d43e167dSRafael J. Wysocki 	device->handle = handle;
1668d43e167dSRafael J. Wysocki 	device->parent = acpi_bus_get_parent(handle);
1669d43e167dSRafael J. Wysocki 	STRUCT_TO_INT(device->status) = sta;
1670d43e167dSRafael J. Wysocki 	acpi_device_get_busid(device);
1671c0af4175SToshi Kani 	acpi_set_pnp_ids(handle, &device->pnp, type);
1672d43e167dSRafael J. Wysocki 	acpi_bus_get_flags(device);
16732c0d4fe0SRafael J. Wysocki 	device->flags.match_driver = false;
1674cf860be6SRafael J. Wysocki 	device_initialize(&device->dev);
1675cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, true);
1676cf860be6SRafael J. Wysocki }
1677bc3b0772SBjorn Helgaas 
1678cf860be6SRafael J. Wysocki void acpi_device_add_finalize(struct acpi_device *device)
1679cf860be6SRafael J. Wysocki {
16802c0d4fe0SRafael J. Wysocki 	device->flags.match_driver = true;
1681cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, false);
1682cf860be6SRafael J. Wysocki 	kobject_uevent(&device->dev.kobj, KOBJ_ADD);
16831da177e4SLinus Torvalds }
16841da177e4SLinus Torvalds 
16855c478f49SBjorn Helgaas static int acpi_add_single_object(struct acpi_device **child,
16865c478f49SBjorn Helgaas 				  acpi_handle handle, int type,
16872c0d4fe0SRafael J. Wysocki 				  unsigned long long sta)
16881da177e4SLinus Torvalds {
168977c24888SBjorn Helgaas 	int result;
169077c24888SBjorn Helgaas 	struct acpi_device *device;
169129aaefa6SBjorn Helgaas 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
16921da177e4SLinus Torvalds 
169336bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
16941da177e4SLinus Torvalds 	if (!device) {
16956468463aSLen Brown 		printk(KERN_ERR PREFIX "Memory allocation error\n");
1696d550d98dSPatrick Mochel 		return -ENOMEM;
16971da177e4SLinus Torvalds 	}
16981da177e4SLinus Torvalds 
1699d43e167dSRafael J. Wysocki 	acpi_init_device_object(device, handle, type, sta);
1700d43e167dSRafael J. Wysocki 	acpi_bus_get_power_flags(device);
1701d57d09a4SRafael J. Wysocki 	acpi_bus_get_wakeup_device_flags(device);
17021da177e4SLinus Torvalds 
1703cf860be6SRafael J. Wysocki 	result = acpi_device_add(device, acpi_device_release);
1704d43e167dSRafael J. Wysocki 	if (result) {
1705718fb0deSHugh Dickins 		acpi_device_release(&device->dev);
1706d550d98dSPatrick Mochel 		return result;
17071da177e4SLinus Torvalds 	}
17081da177e4SLinus Torvalds 
1709bc9b6407SRafael J. Wysocki 	acpi_power_add_remove_device(device, true);
1710cf860be6SRafael J. Wysocki 	acpi_device_add_finalize(device);
17111da177e4SLinus Torvalds 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1712d43e167dSRafael J. Wysocki 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1713d43e167dSRafael J. Wysocki 		dev_name(&device->dev), (char *) buffer.pointer,
1714d43e167dSRafael J. Wysocki 		device->parent ? dev_name(&device->parent->dev) : "(null)"));
17151da177e4SLinus Torvalds 	kfree(buffer.pointer);
17161da177e4SLinus Torvalds 	*child = device;
1717d43e167dSRafael J. Wysocki 	return 0;
1718bf325f95SRafael J. Wysocki }
1719bf325f95SRafael J. Wysocki 
1720778cbc1dSBjorn Helgaas static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1721778cbc1dSBjorn Helgaas 				    unsigned long long *sta)
1722778cbc1dSBjorn Helgaas {
1723778cbc1dSBjorn Helgaas 	acpi_status status;
1724778cbc1dSBjorn Helgaas 	acpi_object_type acpi_type;
1725778cbc1dSBjorn Helgaas 
1726778cbc1dSBjorn Helgaas 	status = acpi_get_type(handle, &acpi_type);
1727778cbc1dSBjorn Helgaas 	if (ACPI_FAILURE(status))
1728778cbc1dSBjorn Helgaas 		return -ENODEV;
1729778cbc1dSBjorn Helgaas 
1730778cbc1dSBjorn Helgaas 	switch (acpi_type) {
1731778cbc1dSBjorn Helgaas 	case ACPI_TYPE_ANY:		/* for ACPI_ROOT_OBJECT */
1732778cbc1dSBjorn Helgaas 	case ACPI_TYPE_DEVICE:
1733778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_DEVICE;
1734778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1735778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1736778cbc1dSBjorn Helgaas 			return -ENODEV;
1737778cbc1dSBjorn Helgaas 		break;
1738778cbc1dSBjorn Helgaas 	case ACPI_TYPE_PROCESSOR:
1739778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_PROCESSOR;
1740778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1741778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1742778cbc1dSBjorn Helgaas 			return -ENODEV;
1743778cbc1dSBjorn Helgaas 		break;
1744778cbc1dSBjorn Helgaas 	case ACPI_TYPE_THERMAL:
1745778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_THERMAL;
1746778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1747778cbc1dSBjorn Helgaas 		break;
1748778cbc1dSBjorn Helgaas 	case ACPI_TYPE_POWER:
1749778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_POWER;
1750778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1751778cbc1dSBjorn Helgaas 		break;
1752778cbc1dSBjorn Helgaas 	default:
1753778cbc1dSBjorn Helgaas 		return -ENODEV;
1754778cbc1dSBjorn Helgaas 	}
1755778cbc1dSBjorn Helgaas 
1756778cbc1dSBjorn Helgaas 	return 0;
1757778cbc1dSBjorn Helgaas }
1758778cbc1dSBjorn Helgaas 
17594b59cc1fSRafael J. Wysocki static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
17604b59cc1fSRafael J. Wysocki 				       char *idstr,
1761c5698074SRafael J. Wysocki 				       const struct acpi_device_id **matchid)
1762c5698074SRafael J. Wysocki {
1763c5698074SRafael J. Wysocki 	const struct acpi_device_id *devid;
1764c5698074SRafael J. Wysocki 
1765c5698074SRafael J. Wysocki 	for (devid = handler->ids; devid->id[0]; devid++)
1766c5698074SRafael J. Wysocki 		if (!strcmp((char *)devid->id, idstr)) {
1767c5698074SRafael J. Wysocki 			if (matchid)
1768c5698074SRafael J. Wysocki 				*matchid = devid;
1769c5698074SRafael J. Wysocki 
17704b59cc1fSRafael J. Wysocki 			return true;
17714b59cc1fSRafael J. Wysocki 		}
17724b59cc1fSRafael J. Wysocki 
17734b59cc1fSRafael J. Wysocki 	return false;
17744b59cc1fSRafael J. Wysocki }
17754b59cc1fSRafael J. Wysocki 
17766b772e8fSToshi Kani static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
17776b772e8fSToshi Kani 					const struct acpi_device_id **matchid)
17786b772e8fSToshi Kani {
17796b772e8fSToshi Kani 	struct acpi_scan_handler *handler;
17806b772e8fSToshi Kani 
17816b772e8fSToshi Kani 	list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
17826b772e8fSToshi Kani 		if (acpi_scan_handler_matching(handler, idstr, matchid))
17836b772e8fSToshi Kani 			return handler;
17846b772e8fSToshi Kani 
17856b772e8fSToshi Kani 	return NULL;
17866b772e8fSToshi Kani }
17876b772e8fSToshi Kani 
17883f8055c3SRafael J. Wysocki void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
17893f8055c3SRafael J. Wysocki {
17903f8055c3SRafael J. Wysocki 	if (!!hotplug->enabled == !!val)
17913f8055c3SRafael J. Wysocki 		return;
17923f8055c3SRafael J. Wysocki 
17933f8055c3SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
17943f8055c3SRafael J. Wysocki 
17953f8055c3SRafael J. Wysocki 	hotplug->enabled = val;
17963f8055c3SRafael J. Wysocki 
17973f8055c3SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
17983f8055c3SRafael J. Wysocki }
17993f8055c3SRafael J. Wysocki 
18006b772e8fSToshi Kani static void acpi_scan_init_hotplug(acpi_handle handle, int type)
18014b59cc1fSRafael J. Wysocki {
18026b772e8fSToshi Kani 	struct acpi_device_pnp pnp = {};
18036b772e8fSToshi Kani 	struct acpi_hardware_id *hwid;
18044b59cc1fSRafael J. Wysocki 	struct acpi_scan_handler *handler;
18054b59cc1fSRafael J. Wysocki 
18066b772e8fSToshi Kani 	INIT_LIST_HEAD(&pnp.ids);
18076b772e8fSToshi Kani 	acpi_set_pnp_ids(handle, &pnp, type);
18084b59cc1fSRafael J. Wysocki 
18096b772e8fSToshi Kani 	if (!pnp.type.hardware_id)
18107a26b530SCatalin Marinas 		goto out;
1811a33ec399SRafael J. Wysocki 
1812a33ec399SRafael J. Wysocki 	/*
1813a33ec399SRafael J. Wysocki 	 * This relies on the fact that acpi_install_notify_handler() will not
1814a33ec399SRafael J. Wysocki 	 * install the same notify handler routine twice for the same handle.
1815a33ec399SRafael J. Wysocki 	 */
18166b772e8fSToshi Kani 	list_for_each_entry(hwid, &pnp.ids, list) {
18176b772e8fSToshi Kani 		handler = acpi_scan_match_handler(hwid->id, NULL);
18186b772e8fSToshi Kani 		if (handler) {
18192cbb14fbSToshi Kani 			acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
18202cbb14fbSToshi Kani 					acpi_hotplug_notify_cb, handler);
18216b772e8fSToshi Kani 			break;
18226b772e8fSToshi Kani 		}
18236b772e8fSToshi Kani 	}
18246b772e8fSToshi Kani 
18257a26b530SCatalin Marinas out:
18266b772e8fSToshi Kani 	acpi_free_pnp_ids(&pnp);
1827a33ec399SRafael J. Wysocki }
1828a33ec399SRafael J. Wysocki 
1829e3863094SRafael J. Wysocki static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1830e3863094SRafael J. Wysocki 				      void *not_used, void **return_value)
18311da177e4SLinus Torvalds {
1832805d410fSRafael J. Wysocki 	struct acpi_device *device = NULL;
1833778cbc1dSBjorn Helgaas 	int type;
1834778cbc1dSBjorn Helgaas 	unsigned long long sta;
1835778cbc1dSBjorn Helgaas 	int result;
18361da177e4SLinus Torvalds 
18374002bf38SRafael J. Wysocki 	acpi_bus_get_device(handle, &device);
18384002bf38SRafael J. Wysocki 	if (device)
18394002bf38SRafael J. Wysocki 		goto out;
18404002bf38SRafael J. Wysocki 
1841778cbc1dSBjorn Helgaas 	result = acpi_bus_type_and_status(handle, &type, &sta);
1842778cbc1dSBjorn Helgaas 	if (result)
184351a85fafSBjorn Helgaas 		return AE_OK;
18441da177e4SLinus Torvalds 
184582c7d5efSRafael J. Wysocki 	if (type == ACPI_BUS_TYPE_POWER) {
184682c7d5efSRafael J. Wysocki 		acpi_add_power_resource(handle);
184782c7d5efSRafael J. Wysocki 		return AE_OK;
184882c7d5efSRafael J. Wysocki 	}
184982c7d5efSRafael J. Wysocki 
18506b772e8fSToshi Kani 	acpi_scan_init_hotplug(handle, type);
1851a33ec399SRafael J. Wysocki 
1852778cbc1dSBjorn Helgaas 	if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1853b581a7f9SRafael J. Wysocki 	    !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
185486e4e20eSRafael J. Wysocki 		struct acpi_device_wakeup wakeup;
185586e4e20eSRafael J. Wysocki 
1856952c63e9SJiang Liu 		if (acpi_has_method(handle, "_PRW")) {
185786e4e20eSRafael J. Wysocki 			acpi_bus_extract_wakeup_device_power_package(handle,
185886e4e20eSRafael J. Wysocki 								     &wakeup);
1859993cbe59SRafael J. Wysocki 			acpi_power_resources_list_free(&wakeup.resources);
1860993cbe59SRafael J. Wysocki 		}
1861778cbc1dSBjorn Helgaas 		return AE_CTRL_DEPTH;
1862b581a7f9SRafael J. Wysocki 	}
18631da177e4SLinus Torvalds 
18642c0d4fe0SRafael J. Wysocki 	acpi_add_single_object(&device, handle, type, sta);
1865e3b87f8aSBjorn Helgaas 	if (!device)
186651a85fafSBjorn Helgaas 		return AE_CTRL_DEPTH;
18671da177e4SLinus Torvalds 
18684002bf38SRafael J. Wysocki  out:
186951a85fafSBjorn Helgaas 	if (!*return_value)
187051a85fafSBjorn Helgaas 		*return_value = device;
1871805d410fSRafael J. Wysocki 
187251a85fafSBjorn Helgaas 	return AE_OK;
18731da177e4SLinus Torvalds }
18741da177e4SLinus Torvalds 
187587b85b3cSRafael J. Wysocki static int acpi_scan_attach_handler(struct acpi_device *device)
187687b85b3cSRafael J. Wysocki {
187787b85b3cSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
187887b85b3cSRafael J. Wysocki 	int ret = 0;
187987b85b3cSRafael J. Wysocki 
188087b85b3cSRafael J. Wysocki 	list_for_each_entry(hwid, &device->pnp.ids, list) {
1881c5698074SRafael J. Wysocki 		const struct acpi_device_id *devid;
1882c5698074SRafael J. Wysocki 		struct acpi_scan_handler *handler;
188387b85b3cSRafael J. Wysocki 
1884c5698074SRafael J. Wysocki 		handler = acpi_scan_match_handler(hwid->id, &devid);
1885c5698074SRafael J. Wysocki 		if (handler) {
1886c5698074SRafael J. Wysocki 			ret = handler->attach(device, devid);
1887c5698074SRafael J. Wysocki 			if (ret > 0) {
1888c5698074SRafael J. Wysocki 				device->handler = handler;
1889c5698074SRafael J. Wysocki 				break;
1890c5698074SRafael J. Wysocki 			} else if (ret < 0) {
1891c5698074SRafael J. Wysocki 				break;
1892c5698074SRafael J. Wysocki 			}
1893c5698074SRafael J. Wysocki 		}
189487b85b3cSRafael J. Wysocki 	}
1895ca589f94SRafael J. Wysocki 	return ret;
1896ca589f94SRafael J. Wysocki }
1897ca589f94SRafael J. Wysocki 
18980fc300b0SRafael J. Wysocki static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
18990fc300b0SRafael J. Wysocki 					  void *not_used, void **ret_not_used)
190051a85fafSBjorn Helgaas {
1901805d410fSRafael J. Wysocki 	struct acpi_device *device;
1902805d410fSRafael J. Wysocki 	unsigned long long sta_not_used;
1903ca589f94SRafael J. Wysocki 	int ret;
19041da177e4SLinus Torvalds 
19057779688fSThomas Renninger 	/*
1906805d410fSRafael J. Wysocki 	 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1907805d410fSRafael J. Wysocki 	 * namespace walks prematurely.
19087779688fSThomas Renninger 	 */
1909ca589f94SRafael J. Wysocki 	if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
1910805d410fSRafael J. Wysocki 		return AE_OK;
19117779688fSThomas Renninger 
1912805d410fSRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
1913805d410fSRafael J. Wysocki 		return AE_CTRL_DEPTH;
19143fb02738SRajesh Shah 
19153a391a39SRafael J. Wysocki 	if (device->handler)
19163a391a39SRafael J. Wysocki 		return AE_OK;
19173a391a39SRafael J. Wysocki 
1918ca589f94SRafael J. Wysocki 	ret = acpi_scan_attach_handler(device);
1919ca589f94SRafael J. Wysocki 	if (ret)
1920ca589f94SRafael J. Wysocki 		return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
1921ca589f94SRafael J. Wysocki 
1922ca589f94SRafael J. Wysocki 	ret = device_attach(&device->dev);
1923ca589f94SRafael J. Wysocki 	return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
1924805d410fSRafael J. Wysocki }
19253fb02738SRajesh Shah 
1926b8bd759aSRafael J. Wysocki /**
1927b8bd759aSRafael J. Wysocki  * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1928b8bd759aSRafael J. Wysocki  * @handle: Root of the namespace scope to scan.
1929b8bd759aSRafael J. Wysocki  *
1930b8bd759aSRafael J. Wysocki  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1931b8bd759aSRafael J. Wysocki  * found devices.
1932b8bd759aSRafael J. Wysocki  *
1933b8bd759aSRafael J. Wysocki  * If no devices were found, -ENODEV is returned, but it does not mean that
1934b8bd759aSRafael J. Wysocki  * there has been a real error.  There just have been no suitable ACPI objects
1935b8bd759aSRafael J. Wysocki  * in the table trunk from which the kernel could create a device and add an
1936b8bd759aSRafael J. Wysocki  * appropriate driver.
19373757b948SRafael J. Wysocki  *
19383757b948SRafael J. Wysocki  * Must be called under acpi_scan_lock.
1939b8bd759aSRafael J. Wysocki  */
1940b8bd759aSRafael J. Wysocki int acpi_bus_scan(acpi_handle handle)
19413fb02738SRajesh Shah {
19421da177e4SLinus Torvalds 	void *device = NULL;
1943c511cc19SRafael J. Wysocki 	int error = 0;
1944c511cc19SRafael J. Wysocki 
19450cd6ac52SRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
19461da177e4SLinus Torvalds 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1947e3863094SRafael J. Wysocki 				    acpi_bus_check_add, NULL, NULL, &device);
19483fb02738SRajesh Shah 
1949d2f6650aSThomas Renninger 	if (!device)
1950c511cc19SRafael J. Wysocki 		error = -ENODEV;
1951c511cc19SRafael J. Wysocki 	else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
1952805d410fSRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
19530fc300b0SRafael J. Wysocki 				    acpi_bus_device_attach, NULL, NULL, NULL);
1954d2f6650aSThomas Renninger 
1955c511cc19SRafael J. Wysocki 	return error;
19563fb02738SRajesh Shah }
1957b8bd759aSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_scan);
19581da177e4SLinus Torvalds 
195905404d8fSRafael J. Wysocki static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
196005404d8fSRafael J. Wysocki 					  void *not_used, void **ret_not_used)
19611da177e4SLinus Torvalds {
196205404d8fSRafael J. Wysocki 	struct acpi_device *device = NULL;
19631da177e4SLinus Torvalds 
196405404d8fSRafael J. Wysocki 	if (!acpi_bus_get_device(handle, &device)) {
1965ca589f94SRafael J. Wysocki 		struct acpi_scan_handler *dev_handler = device->handler;
1966ca589f94SRafael J. Wysocki 
1967ca589f94SRafael J. Wysocki 		if (dev_handler) {
1968ca589f94SRafael J. Wysocki 			if (dev_handler->detach)
1969ca589f94SRafael J. Wysocki 				dev_handler->detach(device);
1970ca589f94SRafael J. Wysocki 
1971ca589f94SRafael J. Wysocki 			device->handler = NULL;
1972ca589f94SRafael J. Wysocki 		} else {
197305404d8fSRafael J. Wysocki 			device_release_driver(&device->dev);
197405404d8fSRafael J. Wysocki 		}
1975ca589f94SRafael J. Wysocki 	}
197605404d8fSRafael J. Wysocki 	return AE_OK;
197705404d8fSRafael J. Wysocki }
19781da177e4SLinus Torvalds 
197905404d8fSRafael J. Wysocki static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
198005404d8fSRafael J. Wysocki 				   void *not_used, void **ret_not_used)
198105404d8fSRafael J. Wysocki {
198205404d8fSRafael J. Wysocki 	struct acpi_device *device = NULL;
198305404d8fSRafael J. Wysocki 
198405404d8fSRafael J. Wysocki 	if (!acpi_bus_get_device(handle, &device))
198505404d8fSRafael J. Wysocki 		acpi_device_unregister(device);
198605404d8fSRafael J. Wysocki 
198705404d8fSRafael J. Wysocki 	return AE_OK;
198805404d8fSRafael J. Wysocki }
198905404d8fSRafael J. Wysocki 
19903757b948SRafael J. Wysocki /**
19913757b948SRafael J. Wysocki  * acpi_bus_trim - Remove ACPI device node and all of its descendants
19923757b948SRafael J. Wysocki  * @start: Root of the ACPI device nodes subtree to remove.
19933757b948SRafael J. Wysocki  *
19943757b948SRafael J. Wysocki  * Must be called under acpi_scan_lock.
19953757b948SRafael J. Wysocki  */
1996bfee26dbSRafael J. Wysocki void acpi_bus_trim(struct acpi_device *start)
19971da177e4SLinus Torvalds {
19981da177e4SLinus Torvalds 	/*
199905404d8fSRafael J. Wysocki 	 * Execute acpi_bus_device_detach() as a post-order callback to detach
200005404d8fSRafael J. Wysocki 	 * all ACPI drivers from the device nodes being removed.
20011da177e4SLinus Torvalds 	 */
200205404d8fSRafael J. Wysocki 	acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
200305404d8fSRafael J. Wysocki 			    acpi_bus_device_detach, NULL, NULL);
200405404d8fSRafael J. Wysocki 	acpi_bus_device_detach(start->handle, 0, NULL, NULL);
20051da177e4SLinus Torvalds 	/*
2006cecdb193SRafael J. Wysocki 	 * Execute acpi_bus_remove() as a post-order callback to remove device
2007cecdb193SRafael J. Wysocki 	 * nodes in the given namespace scope.
20081da177e4SLinus Torvalds 	 */
2009cecdb193SRafael J. Wysocki 	acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2010cecdb193SRafael J. Wysocki 			    acpi_bus_remove, NULL, NULL);
2011cecdb193SRafael J. Wysocki 	acpi_bus_remove(start->handle, 0, NULL, NULL);
20121da177e4SLinus Torvalds }
2013ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim);
2014ceaba663SKristen Accardi 
2015e8b945c9SBjorn Helgaas static int acpi_bus_scan_fixed(void)
20161da177e4SLinus Torvalds {
20171da177e4SLinus Torvalds 	int result = 0;
2018c4168bffSLi Shaohua 
20191da177e4SLinus Torvalds 	/*
20201da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
20211da177e4SLinus Torvalds 	 */
20222c0d4fe0SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
20232c0d4fe0SRafael J. Wysocki 		struct acpi_device *device = NULL;
20242c0d4fe0SRafael J. Wysocki 
20255c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
2026c4168bffSLi Shaohua 						ACPI_BUS_TYPE_POWER_BUTTON,
20272c0d4fe0SRafael J. Wysocki 						ACPI_STA_DEFAULT);
20282c0d4fe0SRafael J. Wysocki 		if (result)
20292c0d4fe0SRafael J. Wysocki 			return result;
20302c0d4fe0SRafael J. Wysocki 
20312c0d4fe0SRafael J. Wysocki 		result = device_attach(&device->dev);
20322c0d4fe0SRafael J. Wysocki 		if (result < 0)
20332c0d4fe0SRafael J. Wysocki 			return result;
20342c0d4fe0SRafael J. Wysocki 
2035c10d7a13SDaniel Drake 		device_init_wakeup(&device->dev, true);
20363fb02738SRajesh Shah 	}
20371da177e4SLinus Torvalds 
20382c0d4fe0SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
20392c0d4fe0SRafael J. Wysocki 		struct acpi_device *device = NULL;
20402c0d4fe0SRafael J. Wysocki 
20415c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
2042c4168bffSLi Shaohua 						ACPI_BUS_TYPE_SLEEP_BUTTON,
20432c0d4fe0SRafael J. Wysocki 						ACPI_STA_DEFAULT);
20442c0d4fe0SRafael J. Wysocki 		if (result)
20452c0d4fe0SRafael J. Wysocki 			return result;
20462c0d4fe0SRafael J. Wysocki 
20472c0d4fe0SRafael J. Wysocki 		result = device_attach(&device->dev);
20483fb02738SRajesh Shah 	}
20491da177e4SLinus Torvalds 
20502c0d4fe0SRafael J. Wysocki 	return result < 0 ? result : 0;
20511da177e4SLinus Torvalds }
20521da177e4SLinus Torvalds 
2053e747f274SBjorn Helgaas int __init acpi_scan_init(void)
20541da177e4SLinus Torvalds {
20551da177e4SLinus Torvalds 	int result;
20561da177e4SLinus Torvalds 
20575b327265SPatrick Mochel 	result = bus_register(&acpi_bus_type);
20585b327265SPatrick Mochel 	if (result) {
20595b327265SPatrick Mochel 		/* We don't want to quit even if we failed to add suspend/resume */
20605b327265SPatrick Mochel 		printk(KERN_ERR PREFIX "Could not register bus type\n");
20615b327265SPatrick Mochel 	}
20625b327265SPatrick Mochel 
206392ef2a25SRafael J. Wysocki 	acpi_pci_root_init();
20644daeaf68SRafael J. Wysocki 	acpi_pci_link_init();
2065ac212b69SRafael J. Wysocki 	acpi_processor_init();
2066141a297bSRafael J. Wysocki 	acpi_platform_init();
2067f58b082aSRafael J. Wysocki 	acpi_lpss_init();
20682fa97febSLan Tianyu 	acpi_cmos_rtc_init();
2069737f1a9fSRafael J. Wysocki 	acpi_container_init();
20700a347644SRafael J. Wysocki 	acpi_memory_hotplug_init();
207194add0f8SJiang Liu 	acpi_dock_init();
207297d9a9e9SRafael J. Wysocki 
20733757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
20741da177e4SLinus Torvalds 	/*
20751da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
20761da177e4SLinus Torvalds 	 */
20770cd6ac52SRafael J. Wysocki 	result = acpi_bus_scan(ACPI_ROOT_OBJECT);
20781da177e4SLinus Torvalds 	if (result)
20793757b948SRafael J. Wysocki 		goto out;
20801da177e4SLinus Torvalds 
20810cd6ac52SRafael J. Wysocki 	result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
20821da177e4SLinus Torvalds 	if (result)
20833757b948SRafael J. Wysocki 		goto out;
2084b8bd759aSRafael J. Wysocki 
2085b8bd759aSRafael J. Wysocki 	result = acpi_bus_scan_fixed();
2086b8bd759aSRafael J. Wysocki 	if (result) {
2087b8bd759aSRafael J. Wysocki 		acpi_device_unregister(acpi_root);
20883757b948SRafael J. Wysocki 		goto out;
2089b8bd759aSRafael J. Wysocki 	}
2090b8bd759aSRafael J. Wysocki 
2091b8bd759aSRafael J. Wysocki 	acpi_update_all_gpes();
20923757b948SRafael J. Wysocki 
2093668192b6SYinghai Lu 	acpi_pci_root_hp_init();
2094668192b6SYinghai Lu 
20953757b948SRafael J. Wysocki  out:
20963757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
20973757b948SRafael J. Wysocki 	return result;
20981da177e4SLinus Torvalds }
2099