xref: /openbmc/linux/drivers/acpi/scan.c (revision b5d667eb392ed901fc7ae76869c7a130559e193c)
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 
30620e112cSThomas Renninger static const char *dummy_hid = "device";
312b2ae7c7SThomas Renninger 
3291e56878SMika Westerberg /*
3391e56878SMika Westerberg  * The following ACPI IDs are known to be suitable for representing as
3491e56878SMika Westerberg  * platform devices.
3591e56878SMika Westerberg  */
3691e56878SMika Westerberg static const struct acpi_device_id acpi_platform_device_ids[] = {
3791e56878SMika Westerberg 
38142b007bSAdrian Hunter 	{ "PNP0D40" },
39142b007bSAdrian Hunter 
405e7779f0SMika Westerberg 	/* Haswell LPSS devices */
415e7779f0SMika Westerberg 	{ "INT33C0", 0 },
425e7779f0SMika Westerberg 	{ "INT33C1", 0 },
435e7779f0SMika Westerberg 	{ "INT33C2", 0 },
445e7779f0SMika Westerberg 	{ "INT33C3", 0 },
455e7779f0SMika Westerberg 	{ "INT33C4", 0 },
465e7779f0SMika Westerberg 	{ "INT33C5", 0 },
475e7779f0SMika Westerberg 	{ "INT33C6", 0 },
485e7779f0SMika Westerberg 	{ "INT33C7", 0 },
495e7779f0SMika Westerberg 
5091e56878SMika Westerberg 	{ }
5191e56878SMika Westerberg };
5291e56878SMika Westerberg 
531da177e4SLinus Torvalds static LIST_HEAD(acpi_device_list);
54e49bd2ddSZhang Rui static LIST_HEAD(acpi_bus_id_list);
559090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock);
561da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
571da177e4SLinus Torvalds 
58e49bd2ddSZhang Rui struct acpi_device_bus_id{
59bb095854SZhang Rui 	char bus_id[15];
60e49bd2ddSZhang Rui 	unsigned int instance_no;
61e49bd2ddSZhang Rui 	struct list_head node;
62e49bd2ddSZhang Rui };
6329b71a1cSThomas Renninger 
6429b71a1cSThomas Renninger /*
6529b71a1cSThomas Renninger  * Creates hid/cid(s) string needed for modalias and uevent
6629b71a1cSThomas Renninger  * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
6729b71a1cSThomas Renninger  * char *modalias: "acpi:IBM0001:ACPI0001"
6829b71a1cSThomas Renninger */
69b3e572d2SAdrian Bunk static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
70b3e572d2SAdrian Bunk 			   int size)
71b3e572d2SAdrian Bunk {
7229b71a1cSThomas Renninger 	int len;
735c9fcb5dSZhang Rui 	int count;
747f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
7529b71a1cSThomas Renninger 
762b2ae7c7SThomas Renninger 	if (list_empty(&acpi_dev->pnp.ids))
772b2ae7c7SThomas Renninger 		return 0;
782b2ae7c7SThomas Renninger 
795c9fcb5dSZhang Rui 	len = snprintf(modalias, size, "acpi:");
8029b71a1cSThomas Renninger 	size -= len;
8129b71a1cSThomas Renninger 
827f47fa6cSBjorn Helgaas 	list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
837f47fa6cSBjorn Helgaas 		count = snprintf(&modalias[len], size, "%s:", id->id);
845c9fcb5dSZhang Rui 		if (count < 0 || count >= size)
855c9fcb5dSZhang Rui 			return -EINVAL;
865c9fcb5dSZhang Rui 		len += count;
875c9fcb5dSZhang Rui 		size -= count;
885c9fcb5dSZhang Rui 	}
895c9fcb5dSZhang Rui 
9029b71a1cSThomas Renninger 	modalias[len] = '\0';
9129b71a1cSThomas Renninger 	return len;
9229b71a1cSThomas Renninger }
9329b71a1cSThomas Renninger 
9429b71a1cSThomas Renninger static ssize_t
9529b71a1cSThomas Renninger acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
9629b71a1cSThomas Renninger 	struct acpi_device *acpi_dev = to_acpi_device(dev);
9729b71a1cSThomas Renninger 	int len;
9829b71a1cSThomas Renninger 
9929b71a1cSThomas Renninger 	/* Device has no HID and no CID or string is >1024 */
10029b71a1cSThomas Renninger 	len = create_modalias(acpi_dev, buf, 1024);
10129b71a1cSThomas Renninger 	if (len <= 0)
10229b71a1cSThomas Renninger 		return 0;
10329b71a1cSThomas Renninger 	buf[len++] = '\n';
10429b71a1cSThomas Renninger 	return len;
10529b71a1cSThomas Renninger }
10629b71a1cSThomas Renninger static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
10729b71a1cSThomas Renninger 
108c4753e57SToshi Kani /**
109c4753e57SToshi Kani  * acpi_bus_hot_remove_device: hot-remove a device and its children
110c4753e57SToshi Kani  * @context: struct acpi_eject_event pointer (freed in this func)
111c4753e57SToshi Kani  *
112c4753e57SToshi Kani  * Hot-remove a device and its children. This function frees up the
113c4753e57SToshi Kani  * memory space passed by arg context, so that the caller may call
114c4753e57SToshi Kani  * this function asynchronously through acpi_os_hotplug_execute().
115c4753e57SToshi Kani  */
116c4753e57SToshi Kani void acpi_bus_hot_remove_device(void *context)
1171da177e4SLinus Torvalds {
118c4753e57SToshi Kani 	struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context;
1195993c467SYinghai Lu 	struct acpi_device *device = ej_event->device;
1205993c467SYinghai Lu 	acpi_handle handle = device->handle;
121b3c450c3SToshi Kani 	acpi_handle temp;
1221da177e4SLinus Torvalds 	struct acpi_object_list arg_list;
1231da177e4SLinus Torvalds 	union acpi_object arg;
1241da177e4SLinus Torvalds 	acpi_status status = AE_OK;
125c4753e57SToshi Kani 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
1261da177e4SLinus Torvalds 
12726d46867SZhang Rui 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1280794469dSKay Sievers 		"Hot-removing device %s...\n", dev_name(&device->dev)));
12926d46867SZhang Rui 
130ae281795SRafael J. Wysocki 	if (acpi_bus_trim(device)) {
13155ac9a01SLin Ming 		printk(KERN_ERR PREFIX
13255ac9a01SLin Ming 				"Removing device failed\n");
133c4753e57SToshi Kani 		goto err_out;
13426d46867SZhang Rui 	}
13526d46867SZhang Rui 
136b3c450c3SToshi Kani 	/* device has been freed */
137b3c450c3SToshi Kani 	device = NULL;
138b3c450c3SToshi Kani 
13926d46867SZhang Rui 	/* power off device */
14026d46867SZhang Rui 	status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
14126d46867SZhang Rui 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
14255ac9a01SLin Ming 		printk(KERN_WARNING PREFIX
14355ac9a01SLin Ming 				"Power-off device failed\n");
14426d46867SZhang Rui 
145b3c450c3SToshi Kani 	if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &temp))) {
1461da177e4SLinus Torvalds 		arg_list.count = 1;
1471da177e4SLinus Torvalds 		arg_list.pointer = &arg;
1481da177e4SLinus Torvalds 		arg.type = ACPI_TYPE_INTEGER;
1491da177e4SLinus Torvalds 		arg.integer.value = 0;
1501da177e4SLinus Torvalds 		acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
1511da177e4SLinus Torvalds 	}
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	arg_list.count = 1;
1541da177e4SLinus Torvalds 	arg_list.pointer = &arg;
1551da177e4SLinus Torvalds 	arg.type = ACPI_TYPE_INTEGER;
1561da177e4SLinus Torvalds 	arg.integer.value = 1;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	/*
1591da177e4SLinus Torvalds 	 * TBD: _EJD support.
1601da177e4SLinus Torvalds 	 */
1611da177e4SLinus Torvalds 	status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
162c4753e57SToshi Kani 	if (ACPI_FAILURE(status)) {
163c4753e57SToshi Kani 		if (status != AE_NOT_FOUND)
164c8d72a5eSZhang Rui 			printk(KERN_WARNING PREFIX
165c8d72a5eSZhang Rui 					"Eject device failed\n");
166c4753e57SToshi Kani 		goto err_out;
167c4753e57SToshi Kani 	}
1681da177e4SLinus Torvalds 
169c4753e57SToshi Kani 	kfree(context);
170c4753e57SToshi Kani 	return;
171c4753e57SToshi Kani 
172c4753e57SToshi Kani err_out:
173c4753e57SToshi Kani 	/* Inform firmware the hot-remove operation has completed w/ error */
174c4753e57SToshi Kani 	(void) acpi_evaluate_hotplug_ost(handle,
175c4753e57SToshi Kani 				ej_event->event, ost_code, NULL);
176c4753e57SToshi Kani 	kfree(context);
177c8d72a5eSZhang Rui 	return;
1781da177e4SLinus Torvalds }
17961622accSToshi Kani EXPORT_SYMBOL(acpi_bus_hot_remove_device);
1801da177e4SLinus Torvalds 
181836aedb1SRafael J. Wysocki static ssize_t real_power_state_show(struct device *dev,
182836aedb1SRafael J. Wysocki 				     struct device_attribute *attr, char *buf)
183836aedb1SRafael J. Wysocki {
184836aedb1SRafael J. Wysocki 	struct acpi_device *adev = to_acpi_device(dev);
185836aedb1SRafael J. Wysocki 	int state;
186836aedb1SRafael J. Wysocki 	int ret;
187836aedb1SRafael J. Wysocki 
188836aedb1SRafael J. Wysocki 	ret = acpi_device_get_power(adev, &state);
189836aedb1SRafael J. Wysocki 	if (ret)
190836aedb1SRafael J. Wysocki 		return ret;
191836aedb1SRafael J. Wysocki 
192836aedb1SRafael J. Wysocki 	return sprintf(buf, "%s\n", acpi_power_state_string(state));
193836aedb1SRafael J. Wysocki }
194836aedb1SRafael J. Wysocki 
195836aedb1SRafael J. Wysocki static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
196836aedb1SRafael J. Wysocki 
197836aedb1SRafael J. Wysocki static ssize_t power_state_show(struct device *dev,
198836aedb1SRafael J. Wysocki 				struct device_attribute *attr, char *buf)
199836aedb1SRafael J. Wysocki {
200836aedb1SRafael J. Wysocki 	struct acpi_device *adev = to_acpi_device(dev);
201836aedb1SRafael J. Wysocki 
202836aedb1SRafael J. Wysocki 	return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
203836aedb1SRafael J. Wysocki }
204836aedb1SRafael J. Wysocki 
205836aedb1SRafael J. Wysocki static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
206836aedb1SRafael J. Wysocki 
2071da177e4SLinus Torvalds static ssize_t
208f883d9dbSPatrick Mochel acpi_eject_store(struct device *d, struct device_attribute *attr,
209f883d9dbSPatrick Mochel 		const char *buf, size_t count)
2101da177e4SLinus Torvalds {
2111da177e4SLinus Torvalds 	int ret = count;
2121da177e4SLinus Torvalds 	acpi_status status;
2131da177e4SLinus Torvalds 	acpi_object_type type = 0;
214f883d9dbSPatrick Mochel 	struct acpi_device *acpi_device = to_acpi_device(d);
215c4753e57SToshi Kani 	struct acpi_eject_event *ej_event;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	if ((!count) || (buf[0] != '1')) {
2181da177e4SLinus Torvalds 		return -EINVAL;
2191da177e4SLinus Torvalds 	}
2201da177e4SLinus Torvalds #ifndef FORCE_EJECT
221f883d9dbSPatrick Mochel 	if (acpi_device->driver == NULL) {
2221da177e4SLinus Torvalds 		ret = -ENODEV;
2231da177e4SLinus Torvalds 		goto err;
2241da177e4SLinus Torvalds 	}
2251da177e4SLinus Torvalds #endif
226f883d9dbSPatrick Mochel 	status = acpi_get_type(acpi_device->handle, &type);
227f883d9dbSPatrick Mochel 	if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
2281da177e4SLinus Torvalds 		ret = -ENODEV;
2291da177e4SLinus Torvalds 		goto err;
2301da177e4SLinus Torvalds 	}
2311da177e4SLinus Torvalds 
232c4753e57SToshi Kani 	ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
233c4753e57SToshi Kani 	if (!ej_event) {
234c4753e57SToshi Kani 		ret = -ENOMEM;
235c4753e57SToshi Kani 		goto err;
236c4753e57SToshi Kani 	}
237c4753e57SToshi Kani 
2385993c467SYinghai Lu 	ej_event->device = acpi_device;
239c4753e57SToshi Kani 	if (acpi_device->flags.eject_pending) {
240c4753e57SToshi Kani 		/* event originated from ACPI eject notification */
241c4753e57SToshi Kani 		ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
242c4753e57SToshi Kani 		acpi_device->flags.eject_pending = 0;
243c4753e57SToshi Kani 	} else {
244c4753e57SToshi Kani 		/* event originated from user */
245c4753e57SToshi Kani 		ej_event->event = ACPI_OST_EC_OSPM_EJECT;
2465993c467SYinghai Lu 		(void) acpi_evaluate_hotplug_ost(acpi_device->handle,
247c4753e57SToshi Kani 			ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
248c4753e57SToshi Kani 	}
249c4753e57SToshi Kani 
250c4753e57SToshi Kani 	acpi_os_hotplug_execute(acpi_bus_hot_remove_device, (void *)ej_event);
2511da177e4SLinus Torvalds err:
2521da177e4SLinus Torvalds 	return ret;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
255f883d9dbSPatrick Mochel static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
256f883d9dbSPatrick Mochel 
257e49bd2ddSZhang Rui static ssize_t
258e49bd2ddSZhang Rui acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
259e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
260e49bd2ddSZhang Rui 
261ea8d82fdSBjorn Helgaas 	return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
262e49bd2ddSZhang Rui }
263e49bd2ddSZhang Rui static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
264e49bd2ddSZhang Rui 
265923d4a45SLv Zheng static ssize_t acpi_device_uid_show(struct device *dev,
266923d4a45SLv Zheng 				    struct device_attribute *attr, char *buf)
267923d4a45SLv Zheng {
268923d4a45SLv Zheng 	struct acpi_device *acpi_dev = to_acpi_device(dev);
269923d4a45SLv Zheng 
270923d4a45SLv Zheng 	return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
271923d4a45SLv Zheng }
272923d4a45SLv Zheng static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
273923d4a45SLv Zheng 
274923d4a45SLv Zheng static ssize_t acpi_device_adr_show(struct device *dev,
275923d4a45SLv Zheng 				    struct device_attribute *attr, char *buf)
276923d4a45SLv Zheng {
277923d4a45SLv Zheng 	struct acpi_device *acpi_dev = to_acpi_device(dev);
278923d4a45SLv Zheng 
279923d4a45SLv Zheng 	return sprintf(buf, "0x%08x\n",
280923d4a45SLv Zheng 		       (unsigned int)(acpi_dev->pnp.bus_address));
281923d4a45SLv Zheng }
282923d4a45SLv Zheng static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
283923d4a45SLv Zheng 
284e49bd2ddSZhang Rui static ssize_t
285e49bd2ddSZhang Rui acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
286e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
287e49bd2ddSZhang Rui 	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
288e49bd2ddSZhang Rui 	int result;
289e49bd2ddSZhang Rui 
290e49bd2ddSZhang Rui 	result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
291e49bd2ddSZhang Rui 	if (result)
292e49bd2ddSZhang Rui 		goto end;
293e49bd2ddSZhang Rui 
294e49bd2ddSZhang Rui 	result = sprintf(buf, "%s\n", (char*)path.pointer);
295e49bd2ddSZhang Rui 	kfree(path.pointer);
296e49bd2ddSZhang Rui end:
297e49bd2ddSZhang Rui 	return result;
298e49bd2ddSZhang Rui }
299e49bd2ddSZhang Rui static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
300e49bd2ddSZhang Rui 
301d1efe3c3SLance Ortiz /* sysfs file that shows description text from the ACPI _STR method */
302d1efe3c3SLance Ortiz static ssize_t description_show(struct device *dev,
303d1efe3c3SLance Ortiz 				struct device_attribute *attr,
304d1efe3c3SLance Ortiz 				char *buf) {
305d1efe3c3SLance Ortiz 	struct acpi_device *acpi_dev = to_acpi_device(dev);
306d1efe3c3SLance Ortiz 	int result;
307d1efe3c3SLance Ortiz 
308d1efe3c3SLance Ortiz 	if (acpi_dev->pnp.str_obj == NULL)
309d1efe3c3SLance Ortiz 		return 0;
310d1efe3c3SLance Ortiz 
311d1efe3c3SLance Ortiz 	/*
312d1efe3c3SLance Ortiz 	 * The _STR object contains a Unicode identifier for a device.
313d1efe3c3SLance Ortiz 	 * We need to convert to utf-8 so it can be displayed.
314d1efe3c3SLance Ortiz 	 */
315d1efe3c3SLance Ortiz 	result = utf16s_to_utf8s(
316d1efe3c3SLance Ortiz 		(wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
317d1efe3c3SLance Ortiz 		acpi_dev->pnp.str_obj->buffer.length,
318d1efe3c3SLance Ortiz 		UTF16_LITTLE_ENDIAN, buf,
319d1efe3c3SLance Ortiz 		PAGE_SIZE);
320d1efe3c3SLance Ortiz 
321d1efe3c3SLance Ortiz 	buf[result++] = '\n';
322d1efe3c3SLance Ortiz 
323d1efe3c3SLance Ortiz 	return result;
324d1efe3c3SLance Ortiz }
325d1efe3c3SLance Ortiz static DEVICE_ATTR(description, 0444, description_show, NULL);
326d1efe3c3SLance Ortiz 
327bb74ac23SYasuaki Ishimatsu static ssize_t
328bb74ac23SYasuaki Ishimatsu acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
329bb74ac23SYasuaki Ishimatsu 		     char *buf) {
330bb74ac23SYasuaki Ishimatsu 	struct acpi_device *acpi_dev = to_acpi_device(dev);
331bb74ac23SYasuaki Ishimatsu 
332bb74ac23SYasuaki Ishimatsu 	return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
333bb74ac23SYasuaki Ishimatsu }
334bb74ac23SYasuaki Ishimatsu static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
335bb74ac23SYasuaki Ishimatsu 
336e49bd2ddSZhang Rui static int acpi_device_setup_files(struct acpi_device *dev)
337f883d9dbSPatrick Mochel {
338d1efe3c3SLance Ortiz 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
339f883d9dbSPatrick Mochel 	acpi_status status;
340f883d9dbSPatrick Mochel 	acpi_handle temp;
341bb74ac23SYasuaki Ishimatsu 	unsigned long long sun;
342e49bd2ddSZhang Rui 	int result = 0;
343e49bd2ddSZhang Rui 
344e49bd2ddSZhang Rui 	/*
345e49bd2ddSZhang Rui 	 * Devices gotten from FADT don't have a "path" attribute
346e49bd2ddSZhang Rui 	 */
347e49bd2ddSZhang Rui 	if (dev->handle) {
348e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_path);
349e49bd2ddSZhang Rui 		if (result)
350e49bd2ddSZhang Rui 			goto end;
351e49bd2ddSZhang Rui 	}
352e49bd2ddSZhang Rui 
3532b2ae7c7SThomas Renninger 	if (!list_empty(&dev->pnp.ids)) {
354e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_hid);
355e49bd2ddSZhang Rui 		if (result)
356e49bd2ddSZhang Rui 			goto end;
357f883d9dbSPatrick Mochel 
35829b71a1cSThomas Renninger 		result = device_create_file(&dev->dev, &dev_attr_modalias);
35929b71a1cSThomas Renninger 		if (result)
36029b71a1cSThomas Renninger 			goto end;
3612b2ae7c7SThomas Renninger 	}
36229b71a1cSThomas Renninger 
363f883d9dbSPatrick Mochel 	/*
364d1efe3c3SLance Ortiz 	 * If device has _STR, 'description' file is created
365d1efe3c3SLance Ortiz 	 */
366d1efe3c3SLance Ortiz 	status = acpi_get_handle(dev->handle, "_STR", &temp);
367d1efe3c3SLance Ortiz 	if (ACPI_SUCCESS(status)) {
368d1efe3c3SLance Ortiz 		status = acpi_evaluate_object(dev->handle, "_STR",
369d1efe3c3SLance Ortiz 					NULL, &buffer);
370d1efe3c3SLance Ortiz 		if (ACPI_FAILURE(status))
371d1efe3c3SLance Ortiz 			buffer.pointer = NULL;
372d1efe3c3SLance Ortiz 		dev->pnp.str_obj = buffer.pointer;
373d1efe3c3SLance Ortiz 		result = device_create_file(&dev->dev, &dev_attr_description);
374d1efe3c3SLance Ortiz 		if (result)
375d1efe3c3SLance Ortiz 			goto end;
376d1efe3c3SLance Ortiz 	}
377d1efe3c3SLance Ortiz 
378923d4a45SLv Zheng 	if (dev->flags.bus_address)
379923d4a45SLv Zheng 		result = device_create_file(&dev->dev, &dev_attr_adr);
380923d4a45SLv Zheng 	if (dev->pnp.unique_id)
381923d4a45SLv Zheng 		result = device_create_file(&dev->dev, &dev_attr_uid);
382923d4a45SLv Zheng 
383bb74ac23SYasuaki Ishimatsu 	status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
384bb74ac23SYasuaki Ishimatsu 	if (ACPI_SUCCESS(status)) {
385bb74ac23SYasuaki Ishimatsu 		dev->pnp.sun = (unsigned long)sun;
386bb74ac23SYasuaki Ishimatsu 		result = device_create_file(&dev->dev, &dev_attr_sun);
387bb74ac23SYasuaki Ishimatsu 		if (result)
388bb74ac23SYasuaki Ishimatsu 			goto end;
389bb74ac23SYasuaki Ishimatsu 	} else {
390bb74ac23SYasuaki Ishimatsu 		dev->pnp.sun = (unsigned long)-1;
391bb74ac23SYasuaki Ishimatsu 	}
392bb74ac23SYasuaki Ishimatsu 
393d1efe3c3SLance Ortiz         /*
394f883d9dbSPatrick Mochel          * If device has _EJ0, 'eject' file is created that is used to trigger
395f883d9dbSPatrick Mochel          * hot-removal function from userland.
396f883d9dbSPatrick Mochel          */
397f883d9dbSPatrick Mochel 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
398836aedb1SRafael J. Wysocki 	if (ACPI_SUCCESS(status)) {
399e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_eject);
400836aedb1SRafael J. Wysocki 		if (result)
401836aedb1SRafael J. Wysocki 			return result;
402836aedb1SRafael J. Wysocki 	}
403836aedb1SRafael J. Wysocki 
404836aedb1SRafael J. Wysocki 	if (dev->flags.power_manageable) {
405836aedb1SRafael J. Wysocki 		result = device_create_file(&dev->dev, &dev_attr_power_state);
406836aedb1SRafael J. Wysocki 		if (result)
407836aedb1SRafael J. Wysocki 			return result;
408836aedb1SRafael J. Wysocki 
409836aedb1SRafael J. Wysocki 		if (dev->power.flags.power_resources)
410836aedb1SRafael J. Wysocki 			result = device_create_file(&dev->dev,
411836aedb1SRafael J. Wysocki 						    &dev_attr_real_power_state);
412836aedb1SRafael J. Wysocki 	}
413836aedb1SRafael J. Wysocki 
414e49bd2ddSZhang Rui end:
415e49bd2ddSZhang Rui 	return result;
416f883d9dbSPatrick Mochel }
417f883d9dbSPatrick Mochel 
418f883d9dbSPatrick Mochel static void acpi_device_remove_files(struct acpi_device *dev)
419f883d9dbSPatrick Mochel {
420f883d9dbSPatrick Mochel 	acpi_status status;
421f883d9dbSPatrick Mochel 	acpi_handle temp;
422f883d9dbSPatrick Mochel 
423836aedb1SRafael J. Wysocki 	if (dev->flags.power_manageable) {
424836aedb1SRafael J. Wysocki 		device_remove_file(&dev->dev, &dev_attr_power_state);
425836aedb1SRafael J. Wysocki 		if (dev->power.flags.power_resources)
426836aedb1SRafael J. Wysocki 			device_remove_file(&dev->dev,
427836aedb1SRafael J. Wysocki 					   &dev_attr_real_power_state);
428836aedb1SRafael J. Wysocki 	}
429836aedb1SRafael J. Wysocki 
430f883d9dbSPatrick Mochel 	/*
431d1efe3c3SLance Ortiz 	 * If device has _STR, remove 'description' file
432d1efe3c3SLance Ortiz 	 */
433d1efe3c3SLance Ortiz 	status = acpi_get_handle(dev->handle, "_STR", &temp);
434d1efe3c3SLance Ortiz 	if (ACPI_SUCCESS(status)) {
435d1efe3c3SLance Ortiz 		kfree(dev->pnp.str_obj);
436d1efe3c3SLance Ortiz 		device_remove_file(&dev->dev, &dev_attr_description);
437d1efe3c3SLance Ortiz 	}
438d1efe3c3SLance Ortiz 	/*
439d1efe3c3SLance Ortiz 	 * If device has _EJ0, remove 'eject' file.
440f883d9dbSPatrick Mochel 	 */
441f883d9dbSPatrick Mochel 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
442f883d9dbSPatrick Mochel 	if (ACPI_SUCCESS(status))
443f883d9dbSPatrick Mochel 		device_remove_file(&dev->dev, &dev_attr_eject);
444e49bd2ddSZhang Rui 
445bb74ac23SYasuaki Ishimatsu 	status = acpi_get_handle(dev->handle, "_SUN", &temp);
446bb74ac23SYasuaki Ishimatsu 	if (ACPI_SUCCESS(status))
447bb74ac23SYasuaki Ishimatsu 		device_remove_file(&dev->dev, &dev_attr_sun);
448bb74ac23SYasuaki Ishimatsu 
449923d4a45SLv Zheng 	if (dev->pnp.unique_id)
450923d4a45SLv Zheng 		device_remove_file(&dev->dev, &dev_attr_uid);
451923d4a45SLv Zheng 	if (dev->flags.bus_address)
452923d4a45SLv Zheng 		device_remove_file(&dev->dev, &dev_attr_adr);
45329b71a1cSThomas Renninger 	device_remove_file(&dev->dev, &dev_attr_modalias);
454e49bd2ddSZhang Rui 	device_remove_file(&dev->dev, &dev_attr_hid);
455e49bd2ddSZhang Rui 	if (dev->handle)
456e49bd2ddSZhang Rui 		device_remove_file(&dev->dev, &dev_attr_path);
457f883d9dbSPatrick Mochel }
4581da177e4SLinus Torvalds /* --------------------------------------------------------------------------
4599e89dde2SZhang Rui 			ACPI Bus operations
4601da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
46129b71a1cSThomas Renninger 
462cf761af9SMika Westerberg static const struct acpi_device_id *__acpi_match_device(
463cf761af9SMika Westerberg 	struct acpi_device *device, const struct acpi_device_id *ids)
46429b71a1cSThomas Renninger {
46529b71a1cSThomas Renninger 	const struct acpi_device_id *id;
4667f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hwid;
46729b71a1cSThomas Renninger 
46839a0ad87SZhao Yakui 	/*
46939a0ad87SZhao Yakui 	 * If the device is not present, it is unnecessary to load device
47039a0ad87SZhao Yakui 	 * driver for it.
47139a0ad87SZhao Yakui 	 */
47239a0ad87SZhao Yakui 	if (!device->status.present)
473cf761af9SMika Westerberg 		return NULL;
47439a0ad87SZhao Yakui 
4757f47fa6cSBjorn Helgaas 	for (id = ids; id->id[0]; id++)
4767f47fa6cSBjorn Helgaas 		list_for_each_entry(hwid, &device->pnp.ids, list)
4777f47fa6cSBjorn Helgaas 			if (!strcmp((char *) id->id, hwid->id))
478cf761af9SMika Westerberg 				return id;
47929b71a1cSThomas Renninger 
480cf761af9SMika Westerberg 	return NULL;
481cf761af9SMika Westerberg }
482cf761af9SMika Westerberg 
483cf761af9SMika Westerberg /**
484cf761af9SMika Westerberg  * acpi_match_device - Match a struct device against a given list of ACPI IDs
485cf761af9SMika Westerberg  * @ids: Array of struct acpi_device_id object to match against.
486cf761af9SMika Westerberg  * @dev: The device structure to match.
487cf761af9SMika Westerberg  *
488cf761af9SMika Westerberg  * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
489cf761af9SMika Westerberg  * object for that handle and use that object to match against a given list of
490cf761af9SMika Westerberg  * device IDs.
491cf761af9SMika Westerberg  *
492cf761af9SMika Westerberg  * Return a pointer to the first matching ID on success or %NULL on failure.
493cf761af9SMika Westerberg  */
494cf761af9SMika Westerberg const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
495cf761af9SMika Westerberg 					       const struct device *dev)
496cf761af9SMika Westerberg {
497cf761af9SMika Westerberg 	struct acpi_device *adev;
498cf761af9SMika Westerberg 
49995f8a082SRafael J. Wysocki 	if (!ids || !ACPI_HANDLE(dev)
50095f8a082SRafael J. Wysocki 	    || ACPI_FAILURE(acpi_bus_get_device(ACPI_HANDLE(dev), &adev)))
501cf761af9SMika Westerberg 		return NULL;
502cf761af9SMika Westerberg 
503cf761af9SMika Westerberg 	return __acpi_match_device(adev, ids);
504cf761af9SMika Westerberg }
505cf761af9SMika Westerberg EXPORT_SYMBOL_GPL(acpi_match_device);
506cf761af9SMika Westerberg 
507cf761af9SMika Westerberg int acpi_match_device_ids(struct acpi_device *device,
508cf761af9SMika Westerberg 			  const struct acpi_device_id *ids)
509cf761af9SMika Westerberg {
510cf761af9SMika Westerberg 	return __acpi_match_device(device, ids) ? 0 : -ENOENT;
51129b71a1cSThomas Renninger }
51229b71a1cSThomas Renninger EXPORT_SYMBOL(acpi_match_device_ids);
51329b71a1cSThomas Renninger 
51482c7d5efSRafael J. Wysocki void acpi_free_ids(struct acpi_device *device)
5157f47fa6cSBjorn Helgaas {
5167f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id, *tmp;
5177f47fa6cSBjorn Helgaas 
5187f47fa6cSBjorn Helgaas 	list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
5197f47fa6cSBjorn Helgaas 		kfree(id->id);
5207f47fa6cSBjorn Helgaas 		kfree(id);
5217f47fa6cSBjorn Helgaas 	}
522d43e167dSRafael J. Wysocki 	kfree(device->pnp.unique_id);
5237f47fa6cSBjorn Helgaas }
5247f47fa6cSBjorn Helgaas 
5250b224527SRafael J. Wysocki static void acpi_free_power_resources_lists(struct acpi_device *device)
5260b224527SRafael J. Wysocki {
5270b224527SRafael J. Wysocki 	int i;
5280b224527SRafael J. Wysocki 
529993cbe59SRafael J. Wysocki 	if (device->wakeup.flags.valid)
530993cbe59SRafael J. Wysocki 		acpi_power_resources_list_free(&device->wakeup.resources);
531993cbe59SRafael J. Wysocki 
5320b224527SRafael J. Wysocki 	if (!device->flags.power_manageable)
5330b224527SRafael J. Wysocki 		return;
5340b224527SRafael J. Wysocki 
5350b224527SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
5360b224527SRafael J. Wysocki 		struct acpi_device_power_state *ps = &device->power.states[i];
5370b224527SRafael J. Wysocki 		acpi_power_resources_list_free(&ps->resources);
5380b224527SRafael J. Wysocki 	}
5390b224527SRafael J. Wysocki }
5400b224527SRafael J. Wysocki 
5411890a97aSPatrick Mochel static void acpi_device_release(struct device *dev)
5421890a97aSPatrick Mochel {
5431890a97aSPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
5441890a97aSPatrick Mochel 
5457f47fa6cSBjorn Helgaas 	acpi_free_ids(acpi_dev);
5460b224527SRafael J. Wysocki 	acpi_free_power_resources_lists(acpi_dev);
5471890a97aSPatrick Mochel 	kfree(acpi_dev);
5481890a97aSPatrick Mochel }
5491890a97aSPatrick Mochel 
5505d9464a4SPatrick Mochel static int acpi_bus_match(struct device *dev, struct device_driver *drv)
5511da177e4SLinus Torvalds {
5525d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
5535d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(drv);
5545d9464a4SPatrick Mochel 
555209d3b17SRafael J. Wysocki 	return acpi_dev->flags.match_driver
556805d410fSRafael J. Wysocki 		&& !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
5575d9464a4SPatrick Mochel }
5585d9464a4SPatrick Mochel 
5597eff2e7aSKay Sievers static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
5605d9464a4SPatrick Mochel {
5615d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
5627eff2e7aSKay Sievers 	int len;
5635d9464a4SPatrick Mochel 
5642b2ae7c7SThomas Renninger 	if (list_empty(&acpi_dev->pnp.ids))
5652b2ae7c7SThomas Renninger 		return 0;
5662b2ae7c7SThomas Renninger 
5677eff2e7aSKay Sievers 	if (add_uevent_var(env, "MODALIAS="))
5687eff2e7aSKay Sievers 		return -ENOMEM;
5697eff2e7aSKay Sievers 	len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
5707eff2e7aSKay Sievers 			      sizeof(env->buf) - env->buflen);
5717eff2e7aSKay Sievers 	if (len >= (sizeof(env->buf) - env->buflen))
5727eff2e7aSKay Sievers 		return -ENOMEM;
5737eff2e7aSKay Sievers 	env->buflen += len;
5741da177e4SLinus Torvalds 	return 0;
5751da177e4SLinus Torvalds }
5761da177e4SLinus Torvalds 
57746ec8598SBjorn Helgaas static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
57846ec8598SBjorn Helgaas {
57946ec8598SBjorn Helgaas 	struct acpi_device *device = data;
58046ec8598SBjorn Helgaas 
58146ec8598SBjorn Helgaas 	device->driver->ops.notify(device, event);
58246ec8598SBjorn Helgaas }
58346ec8598SBjorn Helgaas 
58446ec8598SBjorn Helgaas static acpi_status acpi_device_notify_fixed(void *data)
58546ec8598SBjorn Helgaas {
58646ec8598SBjorn Helgaas 	struct acpi_device *device = data;
58746ec8598SBjorn Helgaas 
58853de5356SBjorn Helgaas 	/* Fixed hardware devices have no handles */
58953de5356SBjorn Helgaas 	acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
59046ec8598SBjorn Helgaas 	return AE_OK;
59146ec8598SBjorn Helgaas }
59246ec8598SBjorn Helgaas 
59346ec8598SBjorn Helgaas static int acpi_device_install_notify_handler(struct acpi_device *device)
59446ec8598SBjorn Helgaas {
59546ec8598SBjorn Helgaas 	acpi_status status;
59646ec8598SBjorn Helgaas 
597ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
59846ec8598SBjorn Helgaas 		status =
59946ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
60046ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
60146ec8598SBjorn Helgaas 						     device);
602ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
60346ec8598SBjorn Helgaas 		status =
60446ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
60546ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
60646ec8598SBjorn Helgaas 						     device);
60746ec8598SBjorn Helgaas 	else
60846ec8598SBjorn Helgaas 		status = acpi_install_notify_handler(device->handle,
60946ec8598SBjorn Helgaas 						     ACPI_DEVICE_NOTIFY,
61046ec8598SBjorn Helgaas 						     acpi_device_notify,
61146ec8598SBjorn Helgaas 						     device);
61246ec8598SBjorn Helgaas 
61346ec8598SBjorn Helgaas 	if (ACPI_FAILURE(status))
61446ec8598SBjorn Helgaas 		return -EINVAL;
61546ec8598SBjorn Helgaas 	return 0;
61646ec8598SBjorn Helgaas }
61746ec8598SBjorn Helgaas 
61846ec8598SBjorn Helgaas static void acpi_device_remove_notify_handler(struct acpi_device *device)
61946ec8598SBjorn Helgaas {
620ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
62146ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
62246ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
623ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
62446ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
62546ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
62646ec8598SBjorn Helgaas 	else
62746ec8598SBjorn Helgaas 		acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
62846ec8598SBjorn Helgaas 					   acpi_device_notify);
62946ec8598SBjorn Helgaas }
63046ec8598SBjorn Helgaas 
6315d9464a4SPatrick Mochel static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
6325d9464a4SPatrick Mochel static int acpi_device_probe(struct device * dev)
6335d9464a4SPatrick Mochel {
6345d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
6355d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
6365d9464a4SPatrick Mochel 	int ret;
6375d9464a4SPatrick Mochel 
6385d9464a4SPatrick Mochel 	ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
6395d9464a4SPatrick Mochel 	if (!ret) {
64046ec8598SBjorn Helgaas 		if (acpi_drv->ops.notify) {
64146ec8598SBjorn Helgaas 			ret = acpi_device_install_notify_handler(acpi_dev);
64246ec8598SBjorn Helgaas 			if (ret) {
64346ec8598SBjorn Helgaas 				if (acpi_drv->ops.remove)
64446ec8598SBjorn Helgaas 					acpi_drv->ops.remove(acpi_dev,
64546ec8598SBjorn Helgaas 						     acpi_dev->removal_type);
64646ec8598SBjorn Helgaas 				return ret;
64746ec8598SBjorn Helgaas 			}
64846ec8598SBjorn Helgaas 		}
64946ec8598SBjorn Helgaas 
6505d9464a4SPatrick Mochel 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
6515d9464a4SPatrick Mochel 			"Found driver [%s] for device [%s]\n",
6525d9464a4SPatrick Mochel 			acpi_drv->name, acpi_dev->pnp.bus_id));
6535d9464a4SPatrick Mochel 		get_device(dev);
6545d9464a4SPatrick Mochel 	}
6555d9464a4SPatrick Mochel 	return ret;
6565d9464a4SPatrick Mochel }
6575d9464a4SPatrick Mochel 
6585d9464a4SPatrick Mochel static int acpi_device_remove(struct device * dev)
6595d9464a4SPatrick Mochel {
6605d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
6615d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
6625d9464a4SPatrick Mochel 
6635d9464a4SPatrick Mochel 	if (acpi_drv) {
66446ec8598SBjorn Helgaas 		if (acpi_drv->ops.notify)
66546ec8598SBjorn Helgaas 			acpi_device_remove_notify_handler(acpi_dev);
6665d9464a4SPatrick Mochel 		if (acpi_drv->ops.remove)
66796333578SLi Shaohua 			acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
6685d9464a4SPatrick Mochel 	}
6695d9464a4SPatrick Mochel 	acpi_dev->driver = NULL;
670db89b4f0SPavel Machek 	acpi_dev->driver_data = NULL;
6715d9464a4SPatrick Mochel 
6725d9464a4SPatrick Mochel 	put_device(dev);
6735d9464a4SPatrick Mochel 	return 0;
6745d9464a4SPatrick Mochel }
6755d9464a4SPatrick Mochel 
67655955aadSDavid Brownell struct bus_type acpi_bus_type = {
6779e89dde2SZhang Rui 	.name		= "acpi",
6785d9464a4SPatrick Mochel 	.match		= acpi_bus_match,
6795d9464a4SPatrick Mochel 	.probe		= acpi_device_probe,
6805d9464a4SPatrick Mochel 	.remove		= acpi_device_remove,
6815d9464a4SPatrick Mochel 	.uevent		= acpi_device_uevent,
6821da177e4SLinus Torvalds };
6831da177e4SLinus Torvalds 
684cf860be6SRafael J. Wysocki int acpi_device_add(struct acpi_device *device,
68582c7d5efSRafael J. Wysocki 		    void (*release)(struct device *))
6861da177e4SLinus Torvalds {
687e49bd2ddSZhang Rui 	int result;
688e49bd2ddSZhang Rui 	struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
689e49bd2ddSZhang Rui 	int found = 0;
69066b7ed40SBjorn Helgaas 
691d43e167dSRafael J. Wysocki 	if (device->handle) {
692d43e167dSRafael J. Wysocki 		acpi_status status;
693d43e167dSRafael J. Wysocki 
694d43e167dSRafael J. Wysocki 		status = acpi_attach_data(device->handle, acpi_bus_data_handler,
695d43e167dSRafael J. Wysocki 					  device);
696d43e167dSRafael J. Wysocki 		if (ACPI_FAILURE(status)) {
697d43e167dSRafael J. Wysocki 			acpi_handle_err(device->handle,
698d43e167dSRafael J. Wysocki 					"Unable to attach device data\n");
699d43e167dSRafael J. Wysocki 			return -ENODEV;
700d43e167dSRafael J. Wysocki 		}
701d43e167dSRafael J. Wysocki 	}
702d43e167dSRafael J. Wysocki 
7031da177e4SLinus Torvalds 	/*
7041da177e4SLinus Torvalds 	 * Linkage
7051da177e4SLinus Torvalds 	 * -------
7061da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
7071da177e4SLinus Torvalds 	 */
7081da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->children);
7091da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->node);
7101da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
7111033f904SLan Tianyu 	INIT_LIST_HEAD(&device->physical_node_list);
7121033f904SLan Tianyu 	mutex_init(&device->physical_node_lock);
713bc9b6407SRafael J. Wysocki 	INIT_LIST_HEAD(&device->power_dependent);
7141da177e4SLinus Torvalds 
715e49bd2ddSZhang Rui 	new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
716e49bd2ddSZhang Rui 	if (!new_bus_id) {
717d43e167dSRafael J. Wysocki 		pr_err(PREFIX "Memory allocation error\n");
718d43e167dSRafael J. Wysocki 		result = -ENOMEM;
719d43e167dSRafael J. Wysocki 		goto err_detach;
720e49bd2ddSZhang Rui 	}
721e49bd2ddSZhang Rui 
7229090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
723e49bd2ddSZhang Rui 	/*
724e49bd2ddSZhang Rui 	 * Find suitable bus_id and instance number in acpi_bus_id_list
725e49bd2ddSZhang Rui 	 * If failed, create one and link it into acpi_bus_id_list
726e49bd2ddSZhang Rui 	 */
727e49bd2ddSZhang Rui 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
7281131b938SBjorn Helgaas 		if (!strcmp(acpi_device_bus_id->bus_id,
7291131b938SBjorn Helgaas 			    acpi_device_hid(device))) {
730e49bd2ddSZhang Rui 			acpi_device_bus_id->instance_no++;
731e49bd2ddSZhang Rui 			found = 1;
732e49bd2ddSZhang Rui 			kfree(new_bus_id);
733e49bd2ddSZhang Rui 			break;
734e49bd2ddSZhang Rui 		}
735e49bd2ddSZhang Rui 	}
736e49bd2ddSZhang Rui 	if (!found) {
737e49bd2ddSZhang Rui 		acpi_device_bus_id = new_bus_id;
7381131b938SBjorn Helgaas 		strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
739e49bd2ddSZhang Rui 		acpi_device_bus_id->instance_no = 0;
740e49bd2ddSZhang Rui 		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
741e49bd2ddSZhang Rui 	}
7420794469dSKay Sievers 	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
743e49bd2ddSZhang Rui 
74433b57150SLen Brown 	if (device->parent)
7451da177e4SLinus Torvalds 		list_add_tail(&device->node, &device->parent->children);
74633b57150SLen Brown 
7471da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
7481da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
7499090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
7501da177e4SLinus Torvalds 
7511890a97aSPatrick Mochel 	if (device->parent)
75266b7ed40SBjorn Helgaas 		device->dev.parent = &device->parent->dev;
7531890a97aSPatrick Mochel 	device->dev.bus = &acpi_bus_type;
75482c7d5efSRafael J. Wysocki 	device->dev.release = release;
755cf860be6SRafael J. Wysocki 	result = device_add(&device->dev);
756e49bd2ddSZhang Rui 	if (result) {
7578b12b922SAlex Chiang 		dev_err(&device->dev, "Error registering device\n");
758d43e167dSRafael J. Wysocki 		goto err;
759e49bd2ddSZhang Rui 	}
760f883d9dbSPatrick Mochel 
761e49bd2ddSZhang Rui 	result = acpi_device_setup_files(device);
762e49bd2ddSZhang Rui 	if (result)
7630794469dSKay Sievers 		printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
7640794469dSKay Sievers 		       dev_name(&device->dev));
765e49bd2ddSZhang Rui 
76696333578SLi Shaohua 	device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
767e49bd2ddSZhang Rui 	return 0;
768d43e167dSRafael J. Wysocki 
769d43e167dSRafael J. Wysocki  err:
7709090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
77133b57150SLen Brown 	if (device->parent)
772e49bd2ddSZhang Rui 		list_del(&device->node);
773e49bd2ddSZhang Rui 	list_del(&device->wakeup_list);
7749090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
775d43e167dSRafael J. Wysocki 
776d43e167dSRafael J. Wysocki  err_detach:
777d43e167dSRafael J. Wysocki 	acpi_detach_data(device->handle, acpi_bus_data_handler);
778e49bd2ddSZhang Rui 	return result;
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
781b17b537aSRafael J. Wysocki static void acpi_device_unregister(struct acpi_device *device)
7821da177e4SLinus Torvalds {
7839090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
78433b57150SLen Brown 	if (device->parent)
7851da177e4SLinus Torvalds 		list_del(&device->node);
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	list_del(&device->wakeup_list);
7889090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	acpi_detach_data(device->handle, acpi_bus_data_handler);
7911890a97aSPatrick Mochel 
792bc9b6407SRafael J. Wysocki 	acpi_power_add_remove_device(device, false);
793f883d9dbSPatrick Mochel 	acpi_device_remove_files(device);
794b1c0f99bSRafael J. Wysocki 	if (device->remove)
795b1c0f99bSRafael J. Wysocki 		device->remove(device);
796b1c0f99bSRafael J. Wysocki 
797cf860be6SRafael J. Wysocki 	device_del(&device->dev);
798bc9b6407SRafael J. Wysocki 	/*
799bc9b6407SRafael J. Wysocki 	 * Drop the reference counts of all power resources the device depends
800bc9b6407SRafael J. Wysocki 	 * on and turn off the ones that have no more references.
801bc9b6407SRafael J. Wysocki 	 */
802bc9b6407SRafael J. Wysocki 	acpi_power_transition(device, ACPI_STATE_D3_COLD);
803cf860be6SRafael J. Wysocki 	put_device(&device->dev);
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
8069e89dde2SZhang Rui /* --------------------------------------------------------------------------
8079e89dde2SZhang Rui                                  Driver Management
8089e89dde2SZhang Rui    -------------------------------------------------------------------------- */
8091da177e4SLinus Torvalds /**
810d758a8faSRandy Dunlap  * acpi_bus_driver_init - add a device to a driver
811d758a8faSRandy Dunlap  * @device: the device to add and initialize
812d758a8faSRandy Dunlap  * @driver: driver for the device
813d758a8faSRandy Dunlap  *
8141da177e4SLinus Torvalds  * Used to initialize a device via its device driver.  Called whenever a
8151890a97aSPatrick Mochel  * driver is bound to a device.  Invokes the driver's add() ops.
8161da177e4SLinus Torvalds  */
8171da177e4SLinus Torvalds static int
8184be44fcdSLen Brown acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
8191da177e4SLinus Torvalds {
8201da177e4SLinus Torvalds 	int result = 0;
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 	if (!device || !driver)
823d550d98dSPatrick Mochel 		return -EINVAL;
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	if (!driver->ops.add)
826d550d98dSPatrick Mochel 		return -ENOSYS;
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	result = driver->ops.add(device);
8291da177e4SLinus Torvalds 	if (result) {
8301da177e4SLinus Torvalds 		device->driver = NULL;
831db89b4f0SPavel Machek 		device->driver_data = NULL;
832d550d98dSPatrick Mochel 		return result;
8331da177e4SLinus Torvalds 	}
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	device->driver = driver;
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	/*
8381da177e4SLinus Torvalds 	 * TBD - Configuration Management: Assign resources to device based
8391da177e4SLinus Torvalds 	 * upon possible configuration and currently allocated resources.
8401da177e4SLinus Torvalds 	 */
8411da177e4SLinus Torvalds 
8424be44fcdSLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
8434be44fcdSLen Brown 			  "Driver successfully bound to device\n"));
844d550d98dSPatrick Mochel 	return 0;
8453fb02738SRajesh Shah }
8463fb02738SRajesh Shah 
8471da177e4SLinus Torvalds /**
848d758a8faSRandy Dunlap  * acpi_bus_register_driver - register a driver with the ACPI bus
849d758a8faSRandy Dunlap  * @driver: driver being registered
850d758a8faSRandy Dunlap  *
8511da177e4SLinus Torvalds  * Registers a driver with the ACPI bus.  Searches the namespace for all
8529d9f749bSBjorn Helgaas  * devices that match the driver's criteria and binds.  Returns zero for
8539d9f749bSBjorn Helgaas  * success or a negative error status for failure.
8541da177e4SLinus Torvalds  */
8554be44fcdSLen Brown int acpi_bus_register_driver(struct acpi_driver *driver)
8561da177e4SLinus Torvalds {
8571890a97aSPatrick Mochel 	int ret;
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	if (acpi_disabled)
860d550d98dSPatrick Mochel 		return -ENODEV;
8611890a97aSPatrick Mochel 	driver->drv.name = driver->name;
8621890a97aSPatrick Mochel 	driver->drv.bus = &acpi_bus_type;
8631890a97aSPatrick Mochel 	driver->drv.owner = driver->owner;
8641da177e4SLinus Torvalds 
8651890a97aSPatrick Mochel 	ret = driver_register(&driver->drv);
8661890a97aSPatrick Mochel 	return ret;
8671da177e4SLinus Torvalds }
8681da177e4SLinus Torvalds 
8694be44fcdSLen Brown EXPORT_SYMBOL(acpi_bus_register_driver);
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds /**
872d758a8faSRandy Dunlap  * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
873d758a8faSRandy Dunlap  * @driver: driver to unregister
874d758a8faSRandy Dunlap  *
8751da177e4SLinus Torvalds  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
8761da177e4SLinus Torvalds  * devices that match the driver's criteria and unbinds.
8771da177e4SLinus Torvalds  */
87806ea8e08SBjorn Helgaas void acpi_bus_unregister_driver(struct acpi_driver *driver)
8791da177e4SLinus Torvalds {
8801890a97aSPatrick Mochel 	driver_unregister(&driver->drv);
8811da177e4SLinus Torvalds }
8824be44fcdSLen Brown 
8831da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_unregister_driver);
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds /* --------------------------------------------------------------------------
8861da177e4SLinus Torvalds                                  Device Enumeration
8871da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
8885c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
8895c478f49SBjorn Helgaas {
8905c478f49SBjorn Helgaas 	acpi_status status;
8915c478f49SBjorn Helgaas 	int ret;
8925c478f49SBjorn Helgaas 	struct acpi_device *device;
8935c478f49SBjorn Helgaas 
8945c478f49SBjorn Helgaas 	/*
8955c478f49SBjorn Helgaas 	 * Fixed hardware devices do not appear in the namespace and do not
8965c478f49SBjorn Helgaas 	 * have handles, but we fabricate acpi_devices for them, so we have
8975c478f49SBjorn Helgaas 	 * to deal with them specially.
8985c478f49SBjorn Helgaas 	 */
8995c478f49SBjorn Helgaas 	if (handle == NULL)
9005c478f49SBjorn Helgaas 		return acpi_root;
9015c478f49SBjorn Helgaas 
9025c478f49SBjorn Helgaas 	do {
9035c478f49SBjorn Helgaas 		status = acpi_get_parent(handle, &handle);
9045c478f49SBjorn Helgaas 		if (status == AE_NULL_ENTRY)
9055c478f49SBjorn Helgaas 			return NULL;
9065c478f49SBjorn Helgaas 		if (ACPI_FAILURE(status))
9075c478f49SBjorn Helgaas 			return acpi_root;
9085c478f49SBjorn Helgaas 
9095c478f49SBjorn Helgaas 		ret = acpi_bus_get_device(handle, &device);
9105c478f49SBjorn Helgaas 		if (ret == 0)
9115c478f49SBjorn Helgaas 			return device;
9125c478f49SBjorn Helgaas 	} while (1);
9135c478f49SBjorn Helgaas }
9145c478f49SBjorn Helgaas 
915c8f7a62cSLen Brown acpi_status
916c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
917c8f7a62cSLen Brown {
918c8f7a62cSLen Brown 	acpi_status status;
919c8f7a62cSLen Brown 	acpi_handle tmp;
920c8f7a62cSLen Brown 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
921c8f7a62cSLen Brown 	union acpi_object *obj;
922c8f7a62cSLen Brown 
923c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_EJD", &tmp);
924c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
925c8f7a62cSLen Brown 		return status;
926c8f7a62cSLen Brown 
927c8f7a62cSLen Brown 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
928c8f7a62cSLen Brown 	if (ACPI_SUCCESS(status)) {
929c8f7a62cSLen Brown 		obj = buffer.pointer;
9303b5fee59SHolger Macht 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
9313b5fee59SHolger Macht 					 ejd);
932c8f7a62cSLen Brown 		kfree(buffer.pointer);
933c8f7a62cSLen Brown 	}
934c8f7a62cSLen Brown 	return status;
935c8f7a62cSLen Brown }
936c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
937c8f7a62cSLen Brown 
9388e4319c4SBob Moore void acpi_bus_data_handler(acpi_handle handle, void *context)
9391da177e4SLinus Torvalds {
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	/* TBD */
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	return;
9441da177e4SLinus Torvalds }
9451da177e4SLinus Torvalds 
946e88c9c60SRafael J. Wysocki static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
947b581a7f9SRafael J. Wysocki 					struct acpi_device_wakeup *wakeup)
9481da177e4SLinus Torvalds {
949b581a7f9SRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
950b581a7f9SRafael J. Wysocki 	union acpi_object *package = NULL;
9511da177e4SLinus Torvalds 	union acpi_object *element = NULL;
952b581a7f9SRafael J. Wysocki 	acpi_status status;
953e88c9c60SRafael J. Wysocki 	int err = -ENODATA;
9541da177e4SLinus Torvalds 
955b581a7f9SRafael J. Wysocki 	if (!wakeup)
956e88c9c60SRafael J. Wysocki 		return -EINVAL;
9571da177e4SLinus Torvalds 
958993cbe59SRafael J. Wysocki 	INIT_LIST_HEAD(&wakeup->resources);
959993cbe59SRafael J. Wysocki 
960b581a7f9SRafael J. Wysocki 	/* _PRW */
961b581a7f9SRafael J. Wysocki 	status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
962b581a7f9SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
963b581a7f9SRafael J. Wysocki 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
964e88c9c60SRafael J. Wysocki 		return err;
965b581a7f9SRafael J. Wysocki 	}
966b581a7f9SRafael J. Wysocki 
967b581a7f9SRafael J. Wysocki 	package = (union acpi_object *)buffer.pointer;
968b581a7f9SRafael J. Wysocki 
969e88c9c60SRafael J. Wysocki 	if (!package || package->package.count < 2)
970b581a7f9SRafael J. Wysocki 		goto out;
971b581a7f9SRafael J. Wysocki 
9721da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
973e88c9c60SRafael J. Wysocki 	if (!element)
974b581a7f9SRafael J. Wysocki 		goto out;
975e88c9c60SRafael J. Wysocki 
9761da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
9771da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
9781da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
9791da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
980e88c9c60SRafael J. Wysocki 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
981b581a7f9SRafael J. Wysocki 			goto out;
982e88c9c60SRafael J. Wysocki 
983b581a7f9SRafael J. Wysocki 		wakeup->gpe_device =
9841da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
985b581a7f9SRafael J. Wysocki 		wakeup->gpe_number =
9861da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
9871da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
988b581a7f9SRafael J. Wysocki 		wakeup->gpe_device = NULL;
989b581a7f9SRafael J. Wysocki 		wakeup->gpe_number = element->integer.value;
990b581a7f9SRafael J. Wysocki 	} else {
991b581a7f9SRafael J. Wysocki 		goto out;
992b581a7f9SRafael J. Wysocki 	}
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
995e88c9c60SRafael J. Wysocki 	if (element->type != ACPI_TYPE_INTEGER)
996b581a7f9SRafael J. Wysocki 		goto out;
997e88c9c60SRafael J. Wysocki 
998b581a7f9SRafael J. Wysocki 	wakeup->sleep_state = element->integer.value;
9991da177e4SLinus Torvalds 
1000e88c9c60SRafael J. Wysocki 	err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1001e88c9c60SRafael J. Wysocki 	if (err)
1002b581a7f9SRafael J. Wysocki 		goto out;
10039874647bSRafael J. Wysocki 
10040596a52bSRafael J. Wysocki 	if (!list_empty(&wakeup->resources)) {
10050596a52bSRafael J. Wysocki 		int sleep_state;
10060596a52bSRafael J. Wysocki 
1007*b5d667ebSRafael J. Wysocki 		err = acpi_power_wakeup_list_init(&wakeup->resources,
1008*b5d667ebSRafael J. Wysocki 						  &sleep_state);
1009*b5d667ebSRafael J. Wysocki 		if (err) {
1010*b5d667ebSRafael J. Wysocki 			acpi_handle_warn(handle, "Retrieving current states "
1011*b5d667ebSRafael J. Wysocki 					 "of wakeup power resources failed\n");
1012*b5d667ebSRafael J. Wysocki 			acpi_power_resources_list_free(&wakeup->resources);
1013*b5d667ebSRafael J. Wysocki 			goto out;
1014*b5d667ebSRafael J. Wysocki 		}
10150596a52bSRafael J. Wysocki 		if (sleep_state < wakeup->sleep_state) {
10160596a52bSRafael J. Wysocki 			acpi_handle_warn(handle, "Overriding _PRW sleep state "
10170596a52bSRafael J. Wysocki 					 "(S%d) by S%d from power resources\n",
10180596a52bSRafael J. Wysocki 					 (int)wakeup->sleep_state, sleep_state);
10190596a52bSRafael J. Wysocki 			wakeup->sleep_state = sleep_state;
10200596a52bSRafael J. Wysocki 		}
10210596a52bSRafael J. Wysocki 	}
1022bba63a29SLin Ming 	acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
1023b581a7f9SRafael J. Wysocki 
1024b581a7f9SRafael J. Wysocki  out:
1025b581a7f9SRafael J. Wysocki 	kfree(buffer.pointer);
1026e88c9c60SRafael J. Wysocki 	return err;
10271da177e4SLinus Torvalds }
10281da177e4SLinus Torvalds 
1029f517709dSRafael J. Wysocki static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
10301da177e4SLinus Torvalds {
103129b71a1cSThomas Renninger 	struct acpi_device_id button_device_ids[] = {
103229b71a1cSThomas Renninger 		{"PNP0C0C", 0},
1033b7e38304SZhang Rui 		{"PNP0C0D", 0},
103429b71a1cSThomas Renninger 		{"PNP0C0E", 0},
103529b71a1cSThomas Renninger 		{"", 0},
103629b71a1cSThomas Renninger 	};
1037f517709dSRafael J. Wysocki 	acpi_status status;
1038f517709dSRafael J. Wysocki 	acpi_event_status event_status;
1039f517709dSRafael J. Wysocki 
1040b67ea761SRafael J. Wysocki 	device->wakeup.flags.notifier_present = 0;
1041f517709dSRafael J. Wysocki 
1042f517709dSRafael J. Wysocki 	/* Power button, Lid switch always enable wakeup */
1043f517709dSRafael J. Wysocki 	if (!acpi_match_device_ids(device, button_device_ids)) {
1044f517709dSRafael J. Wysocki 		device->wakeup.flags.run_wake = 1;
1045b7e38304SZhang Rui 		if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1046b7e38304SZhang Rui 			/* Do not use Lid/sleep button for S5 wakeup */
1047b7e38304SZhang Rui 			if (device->wakeup.sleep_state == ACPI_STATE_S5)
1048b7e38304SZhang Rui 				device->wakeup.sleep_state = ACPI_STATE_S4;
1049b7e38304SZhang Rui 		}
1050f2b56bc8SRafael J. Wysocki 		device_set_wakeup_capable(&device->dev, true);
1051f517709dSRafael J. Wysocki 		return;
1052f517709dSRafael J. Wysocki 	}
1053f517709dSRafael J. Wysocki 
1054e8e18c95SRafael J. Wysocki 	status = acpi_get_gpe_status(device->wakeup.gpe_device,
1055e8e18c95SRafael J. Wysocki 					device->wakeup.gpe_number,
10560f849d2cSLin Ming 						&event_status);
1057f517709dSRafael J. Wysocki 	if (status == AE_OK)
1058f517709dSRafael J. Wysocki 		device->wakeup.flags.run_wake =
1059f517709dSRafael J. Wysocki 				!!(event_status & ACPI_EVENT_FLAG_HANDLE);
1060f517709dSRafael J. Wysocki }
1061f517709dSRafael J. Wysocki 
1062d57d09a4SRafael J. Wysocki static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1063f517709dSRafael J. Wysocki {
1064d57d09a4SRafael J. Wysocki 	acpi_handle temp;
1065f517709dSRafael J. Wysocki 	acpi_status status = 0;
1066e88c9c60SRafael J. Wysocki 	int err;
106729b71a1cSThomas Renninger 
1068d57d09a4SRafael J. Wysocki 	/* Presence of _PRW indicates wake capable */
1069d57d09a4SRafael J. Wysocki 	status = acpi_get_handle(device->handle, "_PRW", &temp);
1070d57d09a4SRafael J. Wysocki 	if (ACPI_FAILURE(status))
1071d57d09a4SRafael J. Wysocki 		return;
1072d57d09a4SRafael J. Wysocki 
1073e88c9c60SRafael J. Wysocki 	err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1074b581a7f9SRafael J. Wysocki 							   &device->wakeup);
1075e88c9c60SRafael J. Wysocki 	if (err) {
1076e88c9c60SRafael J. Wysocki 		dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1077d57d09a4SRafael J. Wysocki 		return;
10781da177e4SLinus Torvalds 	}
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 	device->wakeup.flags.valid = 1;
10819b83ccd2SRafael J. Wysocki 	device->wakeup.prepare_count = 0;
1082f517709dSRafael J. Wysocki 	acpi_bus_set_run_wake_flags(device);
1083729b2bdbSZhao Yakui 	/* Call _PSW/_DSW object to disable its ability to wake the sleeping
1084729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
1085729b2bdbSZhao Yakui 	 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1086729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
1087729b2bdbSZhao Yakui 	 * present will the _PSW object used.
1088729b2bdbSZhao Yakui 	 */
1089e88c9c60SRafael J. Wysocki 	err = acpi_device_sleep_wake(device, 0, 0, 0);
1090e88c9c60SRafael J. Wysocki 	if (err)
109177e76609SRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
109277e76609SRafael J. Wysocki 				"error in _DSW or _PSW evaluation\n"));
10931da177e4SLinus Torvalds }
10941da177e4SLinus Torvalds 
1095f33ce568SRafael J. Wysocki static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1096f33ce568SRafael J. Wysocki {
1097f33ce568SRafael J. Wysocki 	struct acpi_device_power_state *ps = &device->power.states[state];
1098ef85bdbeSRafael J. Wysocki 	char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1099ef85bdbeSRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1100f33ce568SRafael J. Wysocki 	acpi_handle handle;
1101f33ce568SRafael J. Wysocki 	acpi_status status;
1102f33ce568SRafael J. Wysocki 
1103f33ce568SRafael J. Wysocki 	INIT_LIST_HEAD(&ps->resources);
1104f33ce568SRafael J. Wysocki 
1105ef85bdbeSRafael J. Wysocki 	/* Evaluate "_PRx" to get referenced power resources */
1106ef85bdbeSRafael J. Wysocki 	status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1107ef85bdbeSRafael J. Wysocki 	if (ACPI_SUCCESS(status)) {
1108ef85bdbeSRafael J. Wysocki 		union acpi_object *package = buffer.pointer;
1109f33ce568SRafael J. Wysocki 
1110ef85bdbeSRafael J. Wysocki 		if (buffer.length && package
1111ef85bdbeSRafael J. Wysocki 		    && package->type == ACPI_TYPE_PACKAGE
1112ef85bdbeSRafael J. Wysocki 		    && package->package.count) {
1113e88c9c60SRafael J. Wysocki 			int err = acpi_extract_power_resources(package, 0,
1114ef85bdbeSRafael J. Wysocki 							       &ps->resources);
1115e88c9c60SRafael J. Wysocki 			if (!err)
1116f33ce568SRafael J. Wysocki 				device->power.flags.power_resources = 1;
1117f33ce568SRafael J. Wysocki 		}
1118ef85bdbeSRafael J. Wysocki 		ACPI_FREE(buffer.pointer);
1119f33ce568SRafael J. Wysocki 	}
1120f33ce568SRafael J. Wysocki 
1121f33ce568SRafael J. Wysocki 	/* Evaluate "_PSx" to see if we can do explicit sets */
1122ef85bdbeSRafael J. Wysocki 	pathname[2] = 'S';
1123ef85bdbeSRafael J. Wysocki 	status = acpi_get_handle(device->handle, pathname, &handle);
1124f33ce568SRafael J. Wysocki 	if (ACPI_SUCCESS(status))
1125f33ce568SRafael J. Wysocki 		ps->flags.explicit_set = 1;
1126f33ce568SRafael J. Wysocki 
1127f33ce568SRafael J. Wysocki 	/*
1128f33ce568SRafael J. Wysocki 	 * State is valid if there are means to put the device into it.
1129f33ce568SRafael J. Wysocki 	 * D3hot is only valid if _PR3 present.
1130f33ce568SRafael J. Wysocki 	 */
1131ef85bdbeSRafael J. Wysocki 	if (!list_empty(&ps->resources)
1132f33ce568SRafael J. Wysocki 	    || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1133f33ce568SRafael J. Wysocki 		ps->flags.valid = 1;
1134f33ce568SRafael J. Wysocki 		ps->flags.os_accessible = 1;
1135f33ce568SRafael J. Wysocki 	}
1136f33ce568SRafael J. Wysocki 
1137f33ce568SRafael J. Wysocki 	ps->power = -1;		/* Unknown - driver assigned */
1138f33ce568SRafael J. Wysocki 	ps->latency = -1;	/* Unknown - driver assigned */
1139f33ce568SRafael J. Wysocki }
1140f33ce568SRafael J. Wysocki 
1141d43e167dSRafael J. Wysocki static void acpi_bus_get_power_flags(struct acpi_device *device)
11421da177e4SLinus Torvalds {
11438bc5053bSRafael J. Wysocki 	acpi_status status;
11448bc5053bSRafael J. Wysocki 	acpi_handle handle;
11458bc5053bSRafael J. Wysocki 	u32 i;
11469e89dde2SZhang Rui 
1147d43e167dSRafael J. Wysocki 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
1148d43e167dSRafael J. Wysocki 	status = acpi_get_handle(device->handle, "_PS0", &handle);
1149d43e167dSRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
1150d43e167dSRafael J. Wysocki 		status = acpi_get_handle(device->handle, "_PR0", &handle);
1151d43e167dSRafael J. Wysocki 		if (ACPI_FAILURE(status))
1152d43e167dSRafael J. Wysocki 			return;
1153d43e167dSRafael J. Wysocki 	}
1154d43e167dSRafael J. Wysocki 
1155d43e167dSRafael J. Wysocki 	device->flags.power_manageable = 1;
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds 	/*
11589e89dde2SZhang Rui 	 * Power Management Flags
11591da177e4SLinus Torvalds 	 */
11609e89dde2SZhang Rui 	status = acpi_get_handle(device->handle, "_PSC", &handle);
11611da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
11629e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
11639e89dde2SZhang Rui 	status = acpi_get_handle(device->handle, "_IRC", &handle);
11649e89dde2SZhang Rui 	if (ACPI_SUCCESS(status))
11659e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	/*
11689e89dde2SZhang Rui 	 * Enumerate supported power management states
11691da177e4SLinus Torvalds 	 */
1170f33ce568SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1171f33ce568SRafael J. Wysocki 		acpi_bus_init_power_state(device, i);
11729e89dde2SZhang Rui 
11730b224527SRafael J. Wysocki 	INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
11740b224527SRafael J. Wysocki 
11759e89dde2SZhang Rui 	/* Set defaults for D0 and D3 states (always valid) */
11769e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
11779e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
11789e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D3].flags.valid = 1;
11799e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D3].power = 0;
11809e89dde2SZhang Rui 
11815c7dd710SRafael J. Wysocki 	/* Set D3cold's explicit_set flag if _PS3 exists. */
11825c7dd710SRafael J. Wysocki 	if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
11835c7dd710SRafael J. Wysocki 		device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
11845c7dd710SRafael J. Wysocki 
11851399dfcdSAaron Lu 	/* Presence of _PS3 or _PRx means we can put the device into D3 cold */
11861399dfcdSAaron Lu 	if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
11871399dfcdSAaron Lu 			device->power.flags.power_resources)
11881399dfcdSAaron Lu 		device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
11891399dfcdSAaron Lu 
1190b3785492SRafael J. Wysocki 	if (acpi_bus_init_power(device)) {
1191b3785492SRafael J. Wysocki 		acpi_free_power_resources_lists(device);
1192b3785492SRafael J. Wysocki 		device->flags.power_manageable = 0;
1193b3785492SRafael J. Wysocki 	}
11949e89dde2SZhang Rui }
1195c8f7a62cSLen Brown 
1196d43e167dSRafael J. Wysocki static void acpi_bus_get_flags(struct acpi_device *device)
11971da177e4SLinus Torvalds {
11981da177e4SLinus Torvalds 	acpi_status status = AE_OK;
11991da177e4SLinus Torvalds 	acpi_handle temp = NULL;
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
12021da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_STA", &temp);
12031da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
12041da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
12071da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_RMV", &temp);
12081da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
12091da177e4SLinus Torvalds 		device->flags.removable = 1;
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
12121da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_EJD", &temp);
12131da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
12141da177e4SLinus Torvalds 		device->flags.ejectable = 1;
12151da177e4SLinus Torvalds 	else {
12161da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_EJ0", &temp);
12171da177e4SLinus Torvalds 		if (ACPI_SUCCESS(status))
12181da177e4SLinus Torvalds 			device->flags.ejectable = 1;
12191da177e4SLinus Torvalds 	}
12201da177e4SLinus Torvalds }
12211da177e4SLinus Torvalds 
1222c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device)
12231da177e4SLinus Torvalds {
12241da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
12251da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
12261da177e4SLinus Torvalds 	int i = 0;
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	/*
12291da177e4SLinus Torvalds 	 * Bus ID
12301da177e4SLinus Torvalds 	 * ------
12311da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
12321da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
12331da177e4SLinus Torvalds 	 */
1234859ac9a4SBjorn Helgaas 	if (ACPI_IS_ROOT_DEVICE(device)) {
12351da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
1236859ac9a4SBjorn Helgaas 		return;
1237859ac9a4SBjorn Helgaas 	}
1238859ac9a4SBjorn Helgaas 
1239859ac9a4SBjorn Helgaas 	switch (device->device_type) {
12401da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
12411da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
12421da177e4SLinus Torvalds 		break;
12431da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
12441da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
12451da177e4SLinus Torvalds 		break;
12461da177e4SLinus Torvalds 	default:
124766b7ed40SBjorn Helgaas 		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
12481da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
12491da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
12501da177e4SLinus Torvalds 			if (bus_id[i] == '_')
12511da177e4SLinus Torvalds 				bus_id[i] = '\0';
12521da177e4SLinus Torvalds 			else
12531da177e4SLinus Torvalds 				break;
12541da177e4SLinus Torvalds 		}
12551da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
12561da177e4SLinus Torvalds 		break;
12571da177e4SLinus Torvalds 	}
12581da177e4SLinus Torvalds }
12591da177e4SLinus Torvalds 
126054735266SZhang Rui /*
126154735266SZhang Rui  * acpi_bay_match - see if a device is an ejectable driver bay
126254735266SZhang Rui  *
126354735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
126454735266SZhang Rui  * then we can safely call it an ejectable drive bay
126554735266SZhang Rui  */
126654735266SZhang Rui static int acpi_bay_match(struct acpi_device *device){
126754735266SZhang Rui 	acpi_status status;
126854735266SZhang Rui 	acpi_handle handle;
126954735266SZhang Rui 	acpi_handle tmp;
127054735266SZhang Rui 	acpi_handle phandle;
127154735266SZhang Rui 
127254735266SZhang Rui 	handle = device->handle;
127354735266SZhang Rui 
127454735266SZhang Rui 	status = acpi_get_handle(handle, "_EJ0", &tmp);
127554735266SZhang Rui 	if (ACPI_FAILURE(status))
127654735266SZhang Rui 		return -ENODEV;
127754735266SZhang Rui 
127854735266SZhang Rui 	if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
127954735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
128054735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
128154735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
128254735266SZhang Rui 		return 0;
128354735266SZhang Rui 
128454735266SZhang Rui 	if (acpi_get_parent(handle, &phandle))
128554735266SZhang Rui 		return -ENODEV;
128654735266SZhang Rui 
128754735266SZhang Rui         if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
128854735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
128954735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
129054735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
129154735266SZhang Rui                 return 0;
129254735266SZhang Rui 
129354735266SZhang Rui 	return -ENODEV;
129454735266SZhang Rui }
129554735266SZhang Rui 
12963620f2f2SFrank Seidel /*
12973620f2f2SFrank Seidel  * acpi_dock_match - see if a device has a _DCK method
12983620f2f2SFrank Seidel  */
12993620f2f2SFrank Seidel static int acpi_dock_match(struct acpi_device *device)
13003620f2f2SFrank Seidel {
13013620f2f2SFrank Seidel 	acpi_handle tmp;
13023620f2f2SFrank Seidel 	return acpi_get_handle(device->handle, "_DCK", &tmp);
13033620f2f2SFrank Seidel }
13043620f2f2SFrank Seidel 
1305620e112cSThomas Renninger const char *acpi_device_hid(struct acpi_device *device)
130615b8dd53SBob Moore {
13077f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hid;
130815b8dd53SBob Moore 
13092b2ae7c7SThomas Renninger 	if (list_empty(&device->pnp.ids))
13102b2ae7c7SThomas Renninger 		return dummy_hid;
13112b2ae7c7SThomas Renninger 
13127f47fa6cSBjorn Helgaas 	hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
13137f47fa6cSBjorn Helgaas 	return hid->id;
13147f47fa6cSBjorn Helgaas }
13157f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid);
131615b8dd53SBob Moore 
13177f47fa6cSBjorn Helgaas static void acpi_add_id(struct acpi_device *device, const char *dev_id)
13187f47fa6cSBjorn Helgaas {
13197f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
132015b8dd53SBob Moore 
13217f47fa6cSBjorn Helgaas 	id = kmalloc(sizeof(*id), GFP_KERNEL);
13227f47fa6cSBjorn Helgaas 	if (!id)
13237f47fa6cSBjorn Helgaas 		return;
132415b8dd53SBob Moore 
1325581de59eSThomas Meyer 	id->id = kstrdup(dev_id, GFP_KERNEL);
13267f47fa6cSBjorn Helgaas 	if (!id->id) {
13277f47fa6cSBjorn Helgaas 		kfree(id);
13287f47fa6cSBjorn Helgaas 		return;
132915b8dd53SBob Moore 	}
133015b8dd53SBob Moore 
13317f47fa6cSBjorn Helgaas 	list_add_tail(&id->list, &device->pnp.ids);
133215b8dd53SBob Moore }
133315b8dd53SBob Moore 
1334222e82acSDarrick J. Wong /*
1335222e82acSDarrick J. Wong  * Old IBM workstations have a DSDT bug wherein the SMBus object
1336222e82acSDarrick J. Wong  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1337222e82acSDarrick J. Wong  * prefix.  Work around this.
1338222e82acSDarrick J. Wong  */
1339222e82acSDarrick J. Wong static int acpi_ibm_smbus_match(struct acpi_device *device)
1340222e82acSDarrick J. Wong {
1341222e82acSDarrick J. Wong 	acpi_handle h_dummy;
1342222e82acSDarrick J. Wong 	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1343222e82acSDarrick J. Wong 	int result;
1344222e82acSDarrick J. Wong 
1345222e82acSDarrick J. Wong 	if (!dmi_name_in_vendors("IBM"))
1346222e82acSDarrick J. Wong 		return -ENODEV;
1347222e82acSDarrick J. Wong 
1348222e82acSDarrick J. Wong 	/* Look for SMBS object */
1349222e82acSDarrick J. Wong 	result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1350222e82acSDarrick J. Wong 	if (result)
1351222e82acSDarrick J. Wong 		return result;
1352222e82acSDarrick J. Wong 
1353222e82acSDarrick J. Wong 	if (strcmp("SMBS", path.pointer)) {
1354222e82acSDarrick J. Wong 		result = -ENODEV;
1355222e82acSDarrick J. Wong 		goto out;
1356222e82acSDarrick J. Wong 	}
1357222e82acSDarrick J. Wong 
1358222e82acSDarrick J. Wong 	/* Does it have the necessary (but misnamed) methods? */
1359222e82acSDarrick J. Wong 	result = -ENODEV;
1360222e82acSDarrick J. Wong 	if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1361222e82acSDarrick J. Wong 	    ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1362222e82acSDarrick J. Wong 	    ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1363222e82acSDarrick J. Wong 		result = 0;
1364222e82acSDarrick J. Wong out:
1365222e82acSDarrick J. Wong 	kfree(path.pointer);
1366222e82acSDarrick J. Wong 	return result;
1367222e82acSDarrick J. Wong }
1368222e82acSDarrick J. Wong 
1369c7bcb4e9SBjorn Helgaas static void acpi_device_set_id(struct acpi_device *device)
13701da177e4SLinus Torvalds {
13711da177e4SLinus Torvalds 	acpi_status status;
137257f3674fSBjorn Helgaas 	struct acpi_device_info *info;
137378e25fefSLv Zheng 	struct acpi_pnp_device_id_list *cid_list;
13747f47fa6cSBjorn Helgaas 	int i;
13751da177e4SLinus Torvalds 
1376c7bcb4e9SBjorn Helgaas 	switch (device->device_type) {
13771da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
1378859ac9a4SBjorn Helgaas 		if (ACPI_IS_ROOT_DEVICE(device)) {
137957f3674fSBjorn Helgaas 			acpi_add_id(device, ACPI_SYSTEM_HID);
1380859ac9a4SBjorn Helgaas 			break;
1381859ac9a4SBjorn Helgaas 		}
1382859ac9a4SBjorn Helgaas 
138366b7ed40SBjorn Helgaas 		status = acpi_get_object_info(device->handle, &info);
13841da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
138596b2dd1fSHarvey Harrison 			printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
13861da177e4SLinus Torvalds 			return;
13871da177e4SLinus Torvalds 		}
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_HID)
139057f3674fSBjorn Helgaas 			acpi_add_id(device, info->hardware_id.string);
139157f3674fSBjorn Helgaas 		if (info->valid & ACPI_VALID_CID) {
139215b8dd53SBob Moore 			cid_list = &info->compatible_id_list;
139357f3674fSBjorn Helgaas 			for (i = 0; i < cid_list->count; i++)
139457f3674fSBjorn Helgaas 				acpi_add_id(device, cid_list->ids[i].string);
139557f3674fSBjorn Helgaas 		}
13961da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
13971da177e4SLinus Torvalds 			device->pnp.bus_address = info->address;
13981da177e4SLinus Torvalds 			device->flags.bus_address = 1;
13991da177e4SLinus Torvalds 		}
1400ccf78040SLv Zheng 		if (info->valid & ACPI_VALID_UID)
1401ccf78040SLv Zheng 			device->pnp.unique_id = kstrdup(info->unique_id.string,
1402ccf78040SLv Zheng 							GFP_KERNEL);
1403ae843332SZhang Rui 
1404a83893aeSBjorn Helgaas 		kfree(info);
1405a83893aeSBjorn Helgaas 
140657f3674fSBjorn Helgaas 		/*
140757f3674fSBjorn Helgaas 		 * Some devices don't reliably have _HIDs & _CIDs, so add
140857f3674fSBjorn Helgaas 		 * synthetic HIDs to make sure drivers can find them.
14093620f2f2SFrank Seidel 		 */
1410c3d6de69SThomas Renninger 		if (acpi_is_video_device(device))
141157f3674fSBjorn Helgaas 			acpi_add_id(device, ACPI_VIDEO_HID);
14123620f2f2SFrank Seidel 		else if (ACPI_SUCCESS(acpi_bay_match(device)))
141357f3674fSBjorn Helgaas 			acpi_add_id(device, ACPI_BAY_HID);
14143620f2f2SFrank Seidel 		else if (ACPI_SUCCESS(acpi_dock_match(device)))
141557f3674fSBjorn Helgaas 			acpi_add_id(device, ACPI_DOCK_HID);
1416222e82acSDarrick J. Wong 		else if (!acpi_ibm_smbus_match(device))
1417222e82acSDarrick J. Wong 			acpi_add_id(device, ACPI_SMBUS_IBM_HID);
14184f5f64cfSRafael J. Wysocki 		else if (list_empty(&device->pnp.ids) &&
1419b7b30de5SBjorn Helgaas 			 ACPI_IS_ROOT_DEVICE(device->parent)) {
1420b7b30de5SBjorn Helgaas 			acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1421b7b30de5SBjorn Helgaas 			strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1422b7b30de5SBjorn Helgaas 			strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1423b7b30de5SBjorn Helgaas 		}
142454735266SZhang Rui 
14251da177e4SLinus Torvalds 		break;
14261da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
142757f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_POWER_HID);
14281da177e4SLinus Torvalds 		break;
14291da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
143057f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
14311da177e4SLinus Torvalds 		break;
14321da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
143357f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_THERMAL_HID);
14341da177e4SLinus Torvalds 		break;
14351da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
143657f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
14371da177e4SLinus Torvalds 		break;
14381da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
143957f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
14401da177e4SLinus Torvalds 		break;
14411da177e4SLinus Torvalds 	}
14421da177e4SLinus Torvalds }
14431da177e4SLinus Torvalds 
144482c7d5efSRafael J. Wysocki void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1445d43e167dSRafael J. Wysocki 			     int type, unsigned long long sta)
14461da177e4SLinus Torvalds {
1447d43e167dSRafael J. Wysocki 	INIT_LIST_HEAD(&device->pnp.ids);
1448d43e167dSRafael J. Wysocki 	device->device_type = type;
1449d43e167dSRafael J. Wysocki 	device->handle = handle;
1450d43e167dSRafael J. Wysocki 	device->parent = acpi_bus_get_parent(handle);
1451d43e167dSRafael J. Wysocki 	STRUCT_TO_INT(device->status) = sta;
1452d43e167dSRafael J. Wysocki 	acpi_device_get_busid(device);
1453d43e167dSRafael J. Wysocki 	acpi_device_set_id(device);
1454d43e167dSRafael J. Wysocki 	acpi_bus_get_flags(device);
1455cf860be6SRafael J. Wysocki 	device_initialize(&device->dev);
1456cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, true);
1457cf860be6SRafael J. Wysocki }
1458cf860be6SRafael J. Wysocki 
1459cf860be6SRafael J. Wysocki void acpi_device_add_finalize(struct acpi_device *device)
1460cf860be6SRafael J. Wysocki {
1461cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, false);
1462cf860be6SRafael J. Wysocki 	kobject_uevent(&device->dev.kobj, KOBJ_ADD);
14631da177e4SLinus Torvalds }
14641da177e4SLinus Torvalds 
14655c478f49SBjorn Helgaas static int acpi_add_single_object(struct acpi_device **child,
14665c478f49SBjorn Helgaas 				  acpi_handle handle, int type,
1467209d3b17SRafael J. Wysocki 				  unsigned long long sta, bool match_driver)
14681da177e4SLinus Torvalds {
146977c24888SBjorn Helgaas 	int result;
147077c24888SBjorn Helgaas 	struct acpi_device *device;
147129aaefa6SBjorn Helgaas 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
14721da177e4SLinus Torvalds 
147336bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
14741da177e4SLinus Torvalds 	if (!device) {
14756468463aSLen Brown 		printk(KERN_ERR PREFIX "Memory allocation error\n");
1476d550d98dSPatrick Mochel 		return -ENOMEM;
14771da177e4SLinus Torvalds 	}
14781da177e4SLinus Torvalds 
1479d43e167dSRafael J. Wysocki 	acpi_init_device_object(device, handle, type, sta);
1480d43e167dSRafael J. Wysocki 	acpi_bus_get_power_flags(device);
1481d57d09a4SRafael J. Wysocki 	acpi_bus_get_wakeup_device_flags(device);
14821da177e4SLinus Torvalds 
1483209d3b17SRafael J. Wysocki 	device->flags.match_driver = match_driver;
1484cf860be6SRafael J. Wysocki 	result = acpi_device_add(device, acpi_device_release);
1485d43e167dSRafael J. Wysocki 	if (result) {
1486d43e167dSRafael J. Wysocki 		acpi_device_release(&device->dev);
1487d43e167dSRafael J. Wysocki 		return result;
1488d43e167dSRafael J. Wysocki 	}
14891da177e4SLinus Torvalds 
1490bc9b6407SRafael J. Wysocki 	acpi_power_add_remove_device(device, true);
1491cf860be6SRafael J. Wysocki 	acpi_device_add_finalize(device);
149229aaefa6SBjorn Helgaas 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1493d43e167dSRafael J. Wysocki 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1494d43e167dSRafael J. Wysocki 		dev_name(&device->dev), (char *) buffer.pointer,
1495d43e167dSRafael J. Wysocki 		device->parent ? dev_name(&device->parent->dev) : "(null)"));
149629aaefa6SBjorn Helgaas 	kfree(buffer.pointer);
14971da177e4SLinus Torvalds 	*child = device;
1498d43e167dSRafael J. Wysocki 	return 0;
14991da177e4SLinus Torvalds }
15001da177e4SLinus Torvalds 
1501778cbc1dSBjorn Helgaas static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1502778cbc1dSBjorn Helgaas 				    unsigned long long *sta)
1503778cbc1dSBjorn Helgaas {
1504778cbc1dSBjorn Helgaas 	acpi_status status;
1505778cbc1dSBjorn Helgaas 	acpi_object_type acpi_type;
1506778cbc1dSBjorn Helgaas 
1507778cbc1dSBjorn Helgaas 	status = acpi_get_type(handle, &acpi_type);
1508778cbc1dSBjorn Helgaas 	if (ACPI_FAILURE(status))
1509778cbc1dSBjorn Helgaas 		return -ENODEV;
1510778cbc1dSBjorn Helgaas 
1511778cbc1dSBjorn Helgaas 	switch (acpi_type) {
1512778cbc1dSBjorn Helgaas 	case ACPI_TYPE_ANY:		/* for ACPI_ROOT_OBJECT */
1513778cbc1dSBjorn Helgaas 	case ACPI_TYPE_DEVICE:
1514778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_DEVICE;
1515778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1516778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1517778cbc1dSBjorn Helgaas 			return -ENODEV;
1518778cbc1dSBjorn Helgaas 		break;
1519778cbc1dSBjorn Helgaas 	case ACPI_TYPE_PROCESSOR:
1520778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_PROCESSOR;
1521778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1522778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1523778cbc1dSBjorn Helgaas 			return -ENODEV;
1524778cbc1dSBjorn Helgaas 		break;
1525778cbc1dSBjorn Helgaas 	case ACPI_TYPE_THERMAL:
1526778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_THERMAL;
1527778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1528778cbc1dSBjorn Helgaas 		break;
1529778cbc1dSBjorn Helgaas 	case ACPI_TYPE_POWER:
1530778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_POWER;
1531778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1532778cbc1dSBjorn Helgaas 		break;
1533778cbc1dSBjorn Helgaas 	default:
1534778cbc1dSBjorn Helgaas 		return -ENODEV;
1535778cbc1dSBjorn Helgaas 	}
1536778cbc1dSBjorn Helgaas 
1537778cbc1dSBjorn Helgaas 	return 0;
1538778cbc1dSBjorn Helgaas }
1539778cbc1dSBjorn Helgaas 
1540e3863094SRafael J. Wysocki static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1541e3863094SRafael J. Wysocki 				      void *not_used, void **return_value)
15421da177e4SLinus Torvalds {
1543805d410fSRafael J. Wysocki 	struct acpi_device *device = NULL;
1544778cbc1dSBjorn Helgaas 	int type;
1545778cbc1dSBjorn Helgaas 	unsigned long long sta;
1546e3b87f8aSBjorn Helgaas 	acpi_status status;
1547778cbc1dSBjorn Helgaas 	int result;
15481da177e4SLinus Torvalds 
15494002bf38SRafael J. Wysocki 	acpi_bus_get_device(handle, &device);
15504002bf38SRafael J. Wysocki 	if (device)
15514002bf38SRafael J. Wysocki 		goto out;
15524002bf38SRafael J. Wysocki 
1553778cbc1dSBjorn Helgaas 	result = acpi_bus_type_and_status(handle, &type, &sta);
1554778cbc1dSBjorn Helgaas 	if (result)
155551a85fafSBjorn Helgaas 		return AE_OK;
15561da177e4SLinus Torvalds 
155782c7d5efSRafael J. Wysocki 	if (type == ACPI_BUS_TYPE_POWER) {
155882c7d5efSRafael J. Wysocki 		acpi_add_power_resource(handle);
155982c7d5efSRafael J. Wysocki 		return AE_OK;
156082c7d5efSRafael J. Wysocki 	}
156182c7d5efSRafael J. Wysocki 
1562778cbc1dSBjorn Helgaas 	if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1563b581a7f9SRafael J. Wysocki 	    !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
156486e4e20eSRafael J. Wysocki 		struct acpi_device_wakeup wakeup;
156586e4e20eSRafael J. Wysocki 		acpi_handle temp;
156686e4e20eSRafael J. Wysocki 
156786e4e20eSRafael J. Wysocki 		status = acpi_get_handle(handle, "_PRW", &temp);
1568993cbe59SRafael J. Wysocki 		if (ACPI_SUCCESS(status)) {
156986e4e20eSRafael J. Wysocki 			acpi_bus_extract_wakeup_device_power_package(handle,
157086e4e20eSRafael J. Wysocki 								     &wakeup);
1571993cbe59SRafael J. Wysocki 			acpi_power_resources_list_free(&wakeup.resources);
1572993cbe59SRafael J. Wysocki 		}
1573778cbc1dSBjorn Helgaas 		return AE_CTRL_DEPTH;
1574b581a7f9SRafael J. Wysocki 	}
15751da177e4SLinus Torvalds 
157682c7d5efSRafael J. Wysocki 	acpi_add_single_object(&device, handle, type, sta, false);
1577e3b87f8aSBjorn Helgaas 	if (!device)
157851a85fafSBjorn Helgaas 		return AE_CTRL_DEPTH;
15791da177e4SLinus Torvalds 
1580209d3b17SRafael J. Wysocki 	device->flags.match_driver = true;
15813fb02738SRajesh Shah 
15824002bf38SRafael J. Wysocki  out:
158351a85fafSBjorn Helgaas 	if (!*return_value)
158451a85fafSBjorn Helgaas 		*return_value = device;
1585805d410fSRafael J. Wysocki 
158651a85fafSBjorn Helgaas 	return AE_OK;
15871da177e4SLinus Torvalds }
15881da177e4SLinus Torvalds 
15890fc300b0SRafael J. Wysocki static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
15900fc300b0SRafael J. Wysocki 					  void *not_used, void **ret_not_used)
159151a85fafSBjorn Helgaas {
1592805d410fSRafael J. Wysocki 	acpi_status status = AE_OK;
1593805d410fSRafael J. Wysocki 	struct acpi_device *device;
1594805d410fSRafael J. Wysocki 	unsigned long long sta_not_used;
1595805d410fSRafael J. Wysocki 	int type_not_used;
15961da177e4SLinus Torvalds 
15977779688fSThomas Renninger 	/*
1598805d410fSRafael J. Wysocki 	 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1599805d410fSRafael J. Wysocki 	 * namespace walks prematurely.
16007779688fSThomas Renninger 	 */
1601805d410fSRafael J. Wysocki 	if (acpi_bus_type_and_status(handle, &type_not_used, &sta_not_used))
1602805d410fSRafael J. Wysocki 		return AE_OK;
16037779688fSThomas Renninger 
1604805d410fSRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
1605805d410fSRafael J. Wysocki 		return AE_CTRL_DEPTH;
1606805d410fSRafael J. Wysocki 
1607805d410fSRafael J. Wysocki 	if (!acpi_match_device_ids(device, acpi_platform_device_ids)) {
1608805d410fSRafael J. Wysocki 		/* This is a known good platform device. */
1609805d410fSRafael J. Wysocki 		acpi_create_platform_device(device);
1610abe99210SMika Westerberg 	} else if (device_attach(&device->dev) < 0) {
1611805d410fSRafael J. Wysocki 		status = AE_CTRL_DEPTH;
1612805d410fSRafael J. Wysocki 	}
1613805d410fSRafael J. Wysocki 	return status;
1614805d410fSRafael J. Wysocki }
1615805d410fSRafael J. Wysocki 
16160cd6ac52SRafael J. Wysocki static int acpi_bus_scan(acpi_handle handle)
16173fb02738SRajesh Shah {
16181da177e4SLinus Torvalds 	void *device = NULL;
16193fb02738SRajesh Shah 
16200cd6ac52SRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
16211da177e4SLinus Torvalds 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1622e3863094SRafael J. Wysocki 				    acpi_bus_check_add, NULL, NULL, &device);
1623c4168bffSLi Shaohua 
1624805d410fSRafael J. Wysocki 	if (!device)
16250cd6ac52SRafael J. Wysocki 		return -ENODEV;
1626805d410fSRafael J. Wysocki 
16270cd6ac52SRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
1628805d410fSRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
16290fc300b0SRafael J. Wysocki 				    acpi_bus_device_attach, NULL, NULL, NULL);
1630805d410fSRafael J. Wysocki 
16310cd6ac52SRafael J. Wysocki 	return 0;
16323fb02738SRajesh Shah }
16333fb02738SRajesh Shah 
1634636458deSRafael J. Wysocki /**
1635636458deSRafael J. Wysocki  * acpi_bus_add - Add ACPI device node objects in a given namespace scope.
1636636458deSRafael J. Wysocki  * @handle: Root of the namespace scope to scan.
16371da177e4SLinus Torvalds  *
1638636458deSRafael J. Wysocki  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1639636458deSRafael J. Wysocki  * found devices.
16403fb02738SRajesh Shah  *
1641636458deSRafael J. Wysocki  * If no devices were found, -ENODEV is returned, but it does not mean that
1642636458deSRafael J. Wysocki  * there has been a real error.  There just have been no suitable ACPI objects
1643636458deSRafael J. Wysocki  * in the table trunk from which the kernel could create a device and add an
1644636458deSRafael J. Wysocki  * appropriate driver.
16453fb02738SRajesh Shah  */
16460cd6ac52SRafael J. Wysocki int acpi_bus_add(acpi_handle handle)
16473fb02738SRajesh Shah {
164802f57c67SRafael J. Wysocki 	int err;
16493fb02738SRajesh Shah 
16500cd6ac52SRafael J. Wysocki 	err = acpi_bus_scan(handle);
165102f57c67SRafael J. Wysocki 	if (err)
165202f57c67SRafael J. Wysocki 		return err;
1653a2100801SRafael J. Wysocki 
16543a37898dSLin Ming 	acpi_update_all_gpes();
16550fc300b0SRafael J. Wysocki 	return 0;
16563fb02738SRajesh Shah }
16573fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_add);
16583fb02738SRajesh Shah 
165905404d8fSRafael J. Wysocki static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
166005404d8fSRafael J. Wysocki 					  void *not_used, void **ret_not_used)
16614be44fcdSLen Brown {
166205404d8fSRafael J. Wysocki 	struct acpi_device *device = NULL;
16633fb02738SRajesh Shah 
166405404d8fSRafael J. Wysocki 	if (!acpi_bus_get_device(handle, &device)) {
166505404d8fSRafael J. Wysocki 		device->removal_type = ACPI_BUS_REMOVAL_EJECT;
166605404d8fSRafael J. Wysocki 		device_release_driver(&device->dev);
16673fb02738SRajesh Shah 	}
166805404d8fSRafael J. Wysocki 	return AE_OK;
166905404d8fSRafael J. Wysocki }
16701da177e4SLinus Torvalds 
167105404d8fSRafael J. Wysocki static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
167205404d8fSRafael J. Wysocki 				   void *not_used, void **ret_not_used)
16731da177e4SLinus Torvalds {
167405404d8fSRafael J. Wysocki 	struct acpi_device *device = NULL;
16751da177e4SLinus Torvalds 
167605404d8fSRafael J. Wysocki 	if (!acpi_bus_get_device(handle, &device))
167705404d8fSRafael J. Wysocki 		acpi_device_unregister(device);
16781da177e4SLinus Torvalds 
167905404d8fSRafael J. Wysocki 	return AE_OK;
168005404d8fSRafael J. Wysocki }
16811da177e4SLinus Torvalds 
1682ae281795SRafael J. Wysocki int acpi_bus_trim(struct acpi_device *start)
16831da177e4SLinus Torvalds {
16841da177e4SLinus Torvalds 	/*
168505404d8fSRafael J. Wysocki 	 * Execute acpi_bus_device_detach() as a post-order callback to detach
168605404d8fSRafael J. Wysocki 	 * all ACPI drivers from the device nodes being removed.
16871da177e4SLinus Torvalds 	 */
168805404d8fSRafael J. Wysocki 	acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
168905404d8fSRafael J. Wysocki 			    acpi_bus_device_detach, NULL, NULL);
169005404d8fSRafael J. Wysocki 	acpi_bus_device_detach(start->handle, 0, NULL, NULL);
16911da177e4SLinus Torvalds 	/*
1692cecdb193SRafael J. Wysocki 	 * Execute acpi_bus_remove() as a post-order callback to remove device
1693cecdb193SRafael J. Wysocki 	 * nodes in the given namespace scope.
16941da177e4SLinus Torvalds 	 */
1695cecdb193SRafael J. Wysocki 	acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1696cecdb193SRafael J. Wysocki 			    acpi_bus_remove, NULL, NULL);
1697cecdb193SRafael J. Wysocki 	acpi_bus_remove(start->handle, 0, NULL, NULL);
1698cecdb193SRafael J. Wysocki 	return 0;
16991da177e4SLinus Torvalds }
1700ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim);
1701ceaba663SKristen Accardi 
1702e8b945c9SBjorn Helgaas static int acpi_bus_scan_fixed(void)
17031da177e4SLinus Torvalds {
17041da177e4SLinus Torvalds 	int result = 0;
17051da177e4SLinus Torvalds 	struct acpi_device *device = NULL;
1706c4168bffSLi Shaohua 
17071da177e4SLinus Torvalds 	/*
17081da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
17091da177e4SLinus Torvalds 	 */
1710cee324b1SAlexey Starikovskiy 	if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
17115c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
1712c4168bffSLi Shaohua 						ACPI_BUS_TYPE_POWER_BUTTON,
1713209d3b17SRafael J. Wysocki 						ACPI_STA_DEFAULT, true);
1714c10d7a13SDaniel Drake 		device_init_wakeup(&device->dev, true);
17153fb02738SRajesh Shah 	}
17161da177e4SLinus Torvalds 
1717cee324b1SAlexey Starikovskiy 	if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
17185c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
1719c4168bffSLi Shaohua 						ACPI_BUS_TYPE_SLEEP_BUTTON,
1720209d3b17SRafael J. Wysocki 						ACPI_STA_DEFAULT, true);
17213fb02738SRajesh Shah 	}
17221da177e4SLinus Torvalds 
1723d550d98dSPatrick Mochel 	return result;
17241da177e4SLinus Torvalds }
17251da177e4SLinus Torvalds 
1726e747f274SBjorn Helgaas int __init acpi_scan_init(void)
17271da177e4SLinus Torvalds {
17281da177e4SLinus Torvalds 	int result;
17291da177e4SLinus Torvalds 
17305b327265SPatrick Mochel 	result = bus_register(&acpi_bus_type);
17315b327265SPatrick Mochel 	if (result) {
17325b327265SPatrick Mochel 		/* We don't want to quit even if we failed to add suspend/resume */
17335b327265SPatrick Mochel 		printk(KERN_ERR PREFIX "Could not register bus type\n");
17345b327265SPatrick Mochel 	}
17355b327265SPatrick Mochel 
173692ef2a25SRafael J. Wysocki 	acpi_pci_root_init();
173797d9a9e9SRafael J. Wysocki 
17381da177e4SLinus Torvalds 	/*
17391da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
17401da177e4SLinus Torvalds 	 */
17410cd6ac52SRafael J. Wysocki 	result = acpi_bus_scan(ACPI_ROOT_OBJECT);
17420cd6ac52SRafael J. Wysocki 	if (result)
17430cd6ac52SRafael J. Wysocki 		return result;
1744c04209a7SAlexey Starikovskiy 
17450cd6ac52SRafael J. Wysocki 	result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
1746c4168bffSLi Shaohua 	if (!result)
1747adc08e20SBjorn Helgaas 		result = acpi_bus_scan_fixed();
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds 	if (result)
1750b17b537aSRafael J. Wysocki 		acpi_device_unregister(acpi_root);
1751a2100801SRafael J. Wysocki 	else
17523a37898dSLin Ming 		acpi_update_all_gpes();
17531da177e4SLinus Torvalds 
1754d550d98dSPatrick Mochel 	return result;
17551da177e4SLinus Torvalds }
1756