xref: /openbmc/linux/drivers/acpi/scan.c (revision 8bc5053bcdff09a6d1c6a61a79a9014884aa0a14)
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 
1811da177e4SLinus Torvalds static ssize_t
182f883d9dbSPatrick Mochel acpi_eject_store(struct device *d, struct device_attribute *attr,
183f883d9dbSPatrick Mochel 		const char *buf, size_t count)
1841da177e4SLinus Torvalds {
1851da177e4SLinus Torvalds 	int ret = count;
1861da177e4SLinus Torvalds 	acpi_status status;
1871da177e4SLinus Torvalds 	acpi_object_type type = 0;
188f883d9dbSPatrick Mochel 	struct acpi_device *acpi_device = to_acpi_device(d);
189c4753e57SToshi Kani 	struct acpi_eject_event *ej_event;
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	if ((!count) || (buf[0] != '1')) {
1921da177e4SLinus Torvalds 		return -EINVAL;
1931da177e4SLinus Torvalds 	}
1941da177e4SLinus Torvalds #ifndef FORCE_EJECT
195f883d9dbSPatrick Mochel 	if (acpi_device->driver == NULL) {
1961da177e4SLinus Torvalds 		ret = -ENODEV;
1971da177e4SLinus Torvalds 		goto err;
1981da177e4SLinus Torvalds 	}
1991da177e4SLinus Torvalds #endif
200f883d9dbSPatrick Mochel 	status = acpi_get_type(acpi_device->handle, &type);
201f883d9dbSPatrick Mochel 	if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
2021da177e4SLinus Torvalds 		ret = -ENODEV;
2031da177e4SLinus Torvalds 		goto err;
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds 
206c4753e57SToshi Kani 	ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
207c4753e57SToshi Kani 	if (!ej_event) {
208c4753e57SToshi Kani 		ret = -ENOMEM;
209c4753e57SToshi Kani 		goto err;
210c4753e57SToshi Kani 	}
211c4753e57SToshi Kani 
2125993c467SYinghai Lu 	ej_event->device = acpi_device;
213c4753e57SToshi Kani 	if (acpi_device->flags.eject_pending) {
214c4753e57SToshi Kani 		/* event originated from ACPI eject notification */
215c4753e57SToshi Kani 		ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
216c4753e57SToshi Kani 		acpi_device->flags.eject_pending = 0;
217c4753e57SToshi Kani 	} else {
218c4753e57SToshi Kani 		/* event originated from user */
219c4753e57SToshi Kani 		ej_event->event = ACPI_OST_EC_OSPM_EJECT;
2205993c467SYinghai Lu 		(void) acpi_evaluate_hotplug_ost(acpi_device->handle,
221c4753e57SToshi Kani 			ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
222c4753e57SToshi Kani 	}
223c4753e57SToshi Kani 
224c4753e57SToshi Kani 	acpi_os_hotplug_execute(acpi_bus_hot_remove_device, (void *)ej_event);
2251da177e4SLinus Torvalds err:
2261da177e4SLinus Torvalds 	return ret;
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
229f883d9dbSPatrick Mochel static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
230f883d9dbSPatrick Mochel 
231e49bd2ddSZhang Rui static ssize_t
232e49bd2ddSZhang Rui acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
233e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
234e49bd2ddSZhang Rui 
235ea8d82fdSBjorn Helgaas 	return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
236e49bd2ddSZhang Rui }
237e49bd2ddSZhang Rui static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
238e49bd2ddSZhang Rui 
239923d4a45SLv Zheng static ssize_t acpi_device_uid_show(struct device *dev,
240923d4a45SLv Zheng 				    struct device_attribute *attr, char *buf)
241923d4a45SLv Zheng {
242923d4a45SLv Zheng 	struct acpi_device *acpi_dev = to_acpi_device(dev);
243923d4a45SLv Zheng 
244923d4a45SLv Zheng 	return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
245923d4a45SLv Zheng }
246923d4a45SLv Zheng static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
247923d4a45SLv Zheng 
248923d4a45SLv Zheng static ssize_t acpi_device_adr_show(struct device *dev,
249923d4a45SLv Zheng 				    struct device_attribute *attr, char *buf)
250923d4a45SLv Zheng {
251923d4a45SLv Zheng 	struct acpi_device *acpi_dev = to_acpi_device(dev);
252923d4a45SLv Zheng 
253923d4a45SLv Zheng 	return sprintf(buf, "0x%08x\n",
254923d4a45SLv Zheng 		       (unsigned int)(acpi_dev->pnp.bus_address));
255923d4a45SLv Zheng }
256923d4a45SLv Zheng static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
257923d4a45SLv Zheng 
258e49bd2ddSZhang Rui static ssize_t
259e49bd2ddSZhang Rui acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
260e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
261e49bd2ddSZhang Rui 	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
262e49bd2ddSZhang Rui 	int result;
263e49bd2ddSZhang Rui 
264e49bd2ddSZhang Rui 	result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
265e49bd2ddSZhang Rui 	if (result)
266e49bd2ddSZhang Rui 		goto end;
267e49bd2ddSZhang Rui 
268e49bd2ddSZhang Rui 	result = sprintf(buf, "%s\n", (char*)path.pointer);
269e49bd2ddSZhang Rui 	kfree(path.pointer);
270e49bd2ddSZhang Rui end:
271e49bd2ddSZhang Rui 	return result;
272e49bd2ddSZhang Rui }
273e49bd2ddSZhang Rui static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
274e49bd2ddSZhang Rui 
275d1efe3c3SLance Ortiz /* sysfs file that shows description text from the ACPI _STR method */
276d1efe3c3SLance Ortiz static ssize_t description_show(struct device *dev,
277d1efe3c3SLance Ortiz 				struct device_attribute *attr,
278d1efe3c3SLance Ortiz 				char *buf) {
279d1efe3c3SLance Ortiz 	struct acpi_device *acpi_dev = to_acpi_device(dev);
280d1efe3c3SLance Ortiz 	int result;
281d1efe3c3SLance Ortiz 
282d1efe3c3SLance Ortiz 	if (acpi_dev->pnp.str_obj == NULL)
283d1efe3c3SLance Ortiz 		return 0;
284d1efe3c3SLance Ortiz 
285d1efe3c3SLance Ortiz 	/*
286d1efe3c3SLance Ortiz 	 * The _STR object contains a Unicode identifier for a device.
287d1efe3c3SLance Ortiz 	 * We need to convert to utf-8 so it can be displayed.
288d1efe3c3SLance Ortiz 	 */
289d1efe3c3SLance Ortiz 	result = utf16s_to_utf8s(
290d1efe3c3SLance Ortiz 		(wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
291d1efe3c3SLance Ortiz 		acpi_dev->pnp.str_obj->buffer.length,
292d1efe3c3SLance Ortiz 		UTF16_LITTLE_ENDIAN, buf,
293d1efe3c3SLance Ortiz 		PAGE_SIZE);
294d1efe3c3SLance Ortiz 
295d1efe3c3SLance Ortiz 	buf[result++] = '\n';
296d1efe3c3SLance Ortiz 
297d1efe3c3SLance Ortiz 	return result;
298d1efe3c3SLance Ortiz }
299d1efe3c3SLance Ortiz static DEVICE_ATTR(description, 0444, description_show, NULL);
300d1efe3c3SLance Ortiz 
301bb74ac23SYasuaki Ishimatsu static ssize_t
302bb74ac23SYasuaki Ishimatsu acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
303bb74ac23SYasuaki Ishimatsu 		     char *buf) {
304bb74ac23SYasuaki Ishimatsu 	struct acpi_device *acpi_dev = to_acpi_device(dev);
305bb74ac23SYasuaki Ishimatsu 
306bb74ac23SYasuaki Ishimatsu 	return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
307bb74ac23SYasuaki Ishimatsu }
308bb74ac23SYasuaki Ishimatsu static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
309bb74ac23SYasuaki Ishimatsu 
310e49bd2ddSZhang Rui static int acpi_device_setup_files(struct acpi_device *dev)
311f883d9dbSPatrick Mochel {
312d1efe3c3SLance Ortiz 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
313f883d9dbSPatrick Mochel 	acpi_status status;
314f883d9dbSPatrick Mochel 	acpi_handle temp;
315bb74ac23SYasuaki Ishimatsu 	unsigned long long sun;
316e49bd2ddSZhang Rui 	int result = 0;
317e49bd2ddSZhang Rui 
318e49bd2ddSZhang Rui 	/*
319e49bd2ddSZhang Rui 	 * Devices gotten from FADT don't have a "path" attribute
320e49bd2ddSZhang Rui 	 */
321e49bd2ddSZhang Rui 	if (dev->handle) {
322e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_path);
323e49bd2ddSZhang Rui 		if (result)
324e49bd2ddSZhang Rui 			goto end;
325e49bd2ddSZhang Rui 	}
326e49bd2ddSZhang Rui 
3272b2ae7c7SThomas Renninger 	if (!list_empty(&dev->pnp.ids)) {
328e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_hid);
329e49bd2ddSZhang Rui 		if (result)
330e49bd2ddSZhang Rui 			goto end;
331f883d9dbSPatrick Mochel 
33229b71a1cSThomas Renninger 		result = device_create_file(&dev->dev, &dev_attr_modalias);
33329b71a1cSThomas Renninger 		if (result)
33429b71a1cSThomas Renninger 			goto end;
3352b2ae7c7SThomas Renninger 	}
33629b71a1cSThomas Renninger 
337f883d9dbSPatrick Mochel 	/*
338d1efe3c3SLance Ortiz 	 * If device has _STR, 'description' file is created
339d1efe3c3SLance Ortiz 	 */
340d1efe3c3SLance Ortiz 	status = acpi_get_handle(dev->handle, "_STR", &temp);
341d1efe3c3SLance Ortiz 	if (ACPI_SUCCESS(status)) {
342d1efe3c3SLance Ortiz 		status = acpi_evaluate_object(dev->handle, "_STR",
343d1efe3c3SLance Ortiz 					NULL, &buffer);
344d1efe3c3SLance Ortiz 		if (ACPI_FAILURE(status))
345d1efe3c3SLance Ortiz 			buffer.pointer = NULL;
346d1efe3c3SLance Ortiz 		dev->pnp.str_obj = buffer.pointer;
347d1efe3c3SLance Ortiz 		result = device_create_file(&dev->dev, &dev_attr_description);
348d1efe3c3SLance Ortiz 		if (result)
349d1efe3c3SLance Ortiz 			goto end;
350d1efe3c3SLance Ortiz 	}
351d1efe3c3SLance Ortiz 
352923d4a45SLv Zheng 	if (dev->flags.bus_address)
353923d4a45SLv Zheng 		result = device_create_file(&dev->dev, &dev_attr_adr);
354923d4a45SLv Zheng 	if (dev->pnp.unique_id)
355923d4a45SLv Zheng 		result = device_create_file(&dev->dev, &dev_attr_uid);
356923d4a45SLv Zheng 
357bb74ac23SYasuaki Ishimatsu 	status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
358bb74ac23SYasuaki Ishimatsu 	if (ACPI_SUCCESS(status)) {
359bb74ac23SYasuaki Ishimatsu 		dev->pnp.sun = (unsigned long)sun;
360bb74ac23SYasuaki Ishimatsu 		result = device_create_file(&dev->dev, &dev_attr_sun);
361bb74ac23SYasuaki Ishimatsu 		if (result)
362bb74ac23SYasuaki Ishimatsu 			goto end;
363bb74ac23SYasuaki Ishimatsu 	} else {
364bb74ac23SYasuaki Ishimatsu 		dev->pnp.sun = (unsigned long)-1;
365bb74ac23SYasuaki Ishimatsu 	}
366bb74ac23SYasuaki Ishimatsu 
367d1efe3c3SLance Ortiz         /*
368f883d9dbSPatrick Mochel          * If device has _EJ0, 'eject' file is created that is used to trigger
369f883d9dbSPatrick Mochel          * hot-removal function from userland.
370f883d9dbSPatrick Mochel          */
371f883d9dbSPatrick Mochel 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
372f883d9dbSPatrick Mochel 	if (ACPI_SUCCESS(status))
373e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_eject);
374e49bd2ddSZhang Rui end:
375e49bd2ddSZhang Rui 	return result;
376f883d9dbSPatrick Mochel }
377f883d9dbSPatrick Mochel 
378f883d9dbSPatrick Mochel static void acpi_device_remove_files(struct acpi_device *dev)
379f883d9dbSPatrick Mochel {
380f883d9dbSPatrick Mochel 	acpi_status status;
381f883d9dbSPatrick Mochel 	acpi_handle temp;
382f883d9dbSPatrick Mochel 
383f883d9dbSPatrick Mochel 	/*
384d1efe3c3SLance Ortiz 	 * If device has _STR, remove 'description' file
385d1efe3c3SLance Ortiz 	 */
386d1efe3c3SLance Ortiz 	status = acpi_get_handle(dev->handle, "_STR", &temp);
387d1efe3c3SLance Ortiz 	if (ACPI_SUCCESS(status)) {
388d1efe3c3SLance Ortiz 		kfree(dev->pnp.str_obj);
389d1efe3c3SLance Ortiz 		device_remove_file(&dev->dev, &dev_attr_description);
390d1efe3c3SLance Ortiz 	}
391d1efe3c3SLance Ortiz 	/*
392d1efe3c3SLance Ortiz 	 * If device has _EJ0, remove 'eject' file.
393f883d9dbSPatrick Mochel 	 */
394f883d9dbSPatrick Mochel 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
395f883d9dbSPatrick Mochel 	if (ACPI_SUCCESS(status))
396f883d9dbSPatrick Mochel 		device_remove_file(&dev->dev, &dev_attr_eject);
397e49bd2ddSZhang Rui 
398bb74ac23SYasuaki Ishimatsu 	status = acpi_get_handle(dev->handle, "_SUN", &temp);
399bb74ac23SYasuaki Ishimatsu 	if (ACPI_SUCCESS(status))
400bb74ac23SYasuaki Ishimatsu 		device_remove_file(&dev->dev, &dev_attr_sun);
401bb74ac23SYasuaki Ishimatsu 
402923d4a45SLv Zheng 	if (dev->pnp.unique_id)
403923d4a45SLv Zheng 		device_remove_file(&dev->dev, &dev_attr_uid);
404923d4a45SLv Zheng 	if (dev->flags.bus_address)
405923d4a45SLv Zheng 		device_remove_file(&dev->dev, &dev_attr_adr);
40629b71a1cSThomas Renninger 	device_remove_file(&dev->dev, &dev_attr_modalias);
407e49bd2ddSZhang Rui 	device_remove_file(&dev->dev, &dev_attr_hid);
408e49bd2ddSZhang Rui 	if (dev->handle)
409e49bd2ddSZhang Rui 		device_remove_file(&dev->dev, &dev_attr_path);
410f883d9dbSPatrick Mochel }
4111da177e4SLinus Torvalds /* --------------------------------------------------------------------------
4129e89dde2SZhang Rui 			ACPI Bus operations
4131da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
41429b71a1cSThomas Renninger 
415cf761af9SMika Westerberg static const struct acpi_device_id *__acpi_match_device(
416cf761af9SMika Westerberg 	struct acpi_device *device, const struct acpi_device_id *ids)
41729b71a1cSThomas Renninger {
41829b71a1cSThomas Renninger 	const struct acpi_device_id *id;
4197f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hwid;
42029b71a1cSThomas Renninger 
42139a0ad87SZhao Yakui 	/*
42239a0ad87SZhao Yakui 	 * If the device is not present, it is unnecessary to load device
42339a0ad87SZhao Yakui 	 * driver for it.
42439a0ad87SZhao Yakui 	 */
42539a0ad87SZhao Yakui 	if (!device->status.present)
426cf761af9SMika Westerberg 		return NULL;
42739a0ad87SZhao Yakui 
4287f47fa6cSBjorn Helgaas 	for (id = ids; id->id[0]; id++)
4297f47fa6cSBjorn Helgaas 		list_for_each_entry(hwid, &device->pnp.ids, list)
4307f47fa6cSBjorn Helgaas 			if (!strcmp((char *) id->id, hwid->id))
431cf761af9SMika Westerberg 				return id;
43229b71a1cSThomas Renninger 
433cf761af9SMika Westerberg 	return NULL;
434cf761af9SMika Westerberg }
435cf761af9SMika Westerberg 
436cf761af9SMika Westerberg /**
437cf761af9SMika Westerberg  * acpi_match_device - Match a struct device against a given list of ACPI IDs
438cf761af9SMika Westerberg  * @ids: Array of struct acpi_device_id object to match against.
439cf761af9SMika Westerberg  * @dev: The device structure to match.
440cf761af9SMika Westerberg  *
441cf761af9SMika Westerberg  * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
442cf761af9SMika Westerberg  * object for that handle and use that object to match against a given list of
443cf761af9SMika Westerberg  * device IDs.
444cf761af9SMika Westerberg  *
445cf761af9SMika Westerberg  * Return a pointer to the first matching ID on success or %NULL on failure.
446cf761af9SMika Westerberg  */
447cf761af9SMika Westerberg const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
448cf761af9SMika Westerberg 					       const struct device *dev)
449cf761af9SMika Westerberg {
450cf761af9SMika Westerberg 	struct acpi_device *adev;
451cf761af9SMika Westerberg 
45295f8a082SRafael J. Wysocki 	if (!ids || !ACPI_HANDLE(dev)
45395f8a082SRafael J. Wysocki 	    || ACPI_FAILURE(acpi_bus_get_device(ACPI_HANDLE(dev), &adev)))
454cf761af9SMika Westerberg 		return NULL;
455cf761af9SMika Westerberg 
456cf761af9SMika Westerberg 	return __acpi_match_device(adev, ids);
457cf761af9SMika Westerberg }
458cf761af9SMika Westerberg EXPORT_SYMBOL_GPL(acpi_match_device);
459cf761af9SMika Westerberg 
460cf761af9SMika Westerberg int acpi_match_device_ids(struct acpi_device *device,
461cf761af9SMika Westerberg 			  const struct acpi_device_id *ids)
462cf761af9SMika Westerberg {
463cf761af9SMika Westerberg 	return __acpi_match_device(device, ids) ? 0 : -ENOENT;
46429b71a1cSThomas Renninger }
46529b71a1cSThomas Renninger EXPORT_SYMBOL(acpi_match_device_ids);
46629b71a1cSThomas Renninger 
46782c7d5efSRafael J. Wysocki void acpi_free_ids(struct acpi_device *device)
4687f47fa6cSBjorn Helgaas {
4697f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id, *tmp;
4707f47fa6cSBjorn Helgaas 
4717f47fa6cSBjorn Helgaas 	list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
4727f47fa6cSBjorn Helgaas 		kfree(id->id);
4737f47fa6cSBjorn Helgaas 		kfree(id);
4747f47fa6cSBjorn Helgaas 	}
475d43e167dSRafael J. Wysocki 	kfree(device->pnp.unique_id);
4767f47fa6cSBjorn Helgaas }
4777f47fa6cSBjorn Helgaas 
4780b224527SRafael J. Wysocki static void acpi_free_power_resources_lists(struct acpi_device *device)
4790b224527SRafael J. Wysocki {
4800b224527SRafael J. Wysocki 	int i;
4810b224527SRafael J. Wysocki 
482993cbe59SRafael J. Wysocki 	if (device->wakeup.flags.valid)
483993cbe59SRafael J. Wysocki 		acpi_power_resources_list_free(&device->wakeup.resources);
484993cbe59SRafael J. Wysocki 
4850b224527SRafael J. Wysocki 	if (!device->flags.power_manageable)
4860b224527SRafael J. Wysocki 		return;
4870b224527SRafael J. Wysocki 
4880b224527SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
4890b224527SRafael J. Wysocki 		struct acpi_device_power_state *ps = &device->power.states[i];
4900b224527SRafael J. Wysocki 		acpi_power_resources_list_free(&ps->resources);
4910b224527SRafael J. Wysocki 	}
4920b224527SRafael J. Wysocki }
4930b224527SRafael J. Wysocki 
4941890a97aSPatrick Mochel static void acpi_device_release(struct device *dev)
4951890a97aSPatrick Mochel {
4961890a97aSPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
4971890a97aSPatrick Mochel 
4987f47fa6cSBjorn Helgaas 	acpi_free_ids(acpi_dev);
4990b224527SRafael J. Wysocki 	acpi_free_power_resources_lists(acpi_dev);
5001890a97aSPatrick Mochel 	kfree(acpi_dev);
5011890a97aSPatrick Mochel }
5021890a97aSPatrick Mochel 
5035d9464a4SPatrick Mochel static int acpi_bus_match(struct device *dev, struct device_driver *drv)
5041da177e4SLinus Torvalds {
5055d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
5065d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(drv);
5075d9464a4SPatrick Mochel 
508209d3b17SRafael J. Wysocki 	return acpi_dev->flags.match_driver
509805d410fSRafael J. Wysocki 		&& !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
5105d9464a4SPatrick Mochel }
5115d9464a4SPatrick Mochel 
5127eff2e7aSKay Sievers static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
5135d9464a4SPatrick Mochel {
5145d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
5157eff2e7aSKay Sievers 	int len;
5165d9464a4SPatrick Mochel 
5172b2ae7c7SThomas Renninger 	if (list_empty(&acpi_dev->pnp.ids))
5182b2ae7c7SThomas Renninger 		return 0;
5192b2ae7c7SThomas Renninger 
5207eff2e7aSKay Sievers 	if (add_uevent_var(env, "MODALIAS="))
5217eff2e7aSKay Sievers 		return -ENOMEM;
5227eff2e7aSKay Sievers 	len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
5237eff2e7aSKay Sievers 			      sizeof(env->buf) - env->buflen);
5247eff2e7aSKay Sievers 	if (len >= (sizeof(env->buf) - env->buflen))
5257eff2e7aSKay Sievers 		return -ENOMEM;
5267eff2e7aSKay Sievers 	env->buflen += len;
5271da177e4SLinus Torvalds 	return 0;
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
53046ec8598SBjorn Helgaas static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
53146ec8598SBjorn Helgaas {
53246ec8598SBjorn Helgaas 	struct acpi_device *device = data;
53346ec8598SBjorn Helgaas 
53446ec8598SBjorn Helgaas 	device->driver->ops.notify(device, event);
53546ec8598SBjorn Helgaas }
53646ec8598SBjorn Helgaas 
53746ec8598SBjorn Helgaas static acpi_status acpi_device_notify_fixed(void *data)
53846ec8598SBjorn Helgaas {
53946ec8598SBjorn Helgaas 	struct acpi_device *device = data;
54046ec8598SBjorn Helgaas 
54153de5356SBjorn Helgaas 	/* Fixed hardware devices have no handles */
54253de5356SBjorn Helgaas 	acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
54346ec8598SBjorn Helgaas 	return AE_OK;
54446ec8598SBjorn Helgaas }
54546ec8598SBjorn Helgaas 
54646ec8598SBjorn Helgaas static int acpi_device_install_notify_handler(struct acpi_device *device)
54746ec8598SBjorn Helgaas {
54846ec8598SBjorn Helgaas 	acpi_status status;
54946ec8598SBjorn Helgaas 
550ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
55146ec8598SBjorn Helgaas 		status =
55246ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
55346ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
55446ec8598SBjorn Helgaas 						     device);
555ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
55646ec8598SBjorn Helgaas 		status =
55746ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
55846ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
55946ec8598SBjorn Helgaas 						     device);
56046ec8598SBjorn Helgaas 	else
56146ec8598SBjorn Helgaas 		status = acpi_install_notify_handler(device->handle,
56246ec8598SBjorn Helgaas 						     ACPI_DEVICE_NOTIFY,
56346ec8598SBjorn Helgaas 						     acpi_device_notify,
56446ec8598SBjorn Helgaas 						     device);
56546ec8598SBjorn Helgaas 
56646ec8598SBjorn Helgaas 	if (ACPI_FAILURE(status))
56746ec8598SBjorn Helgaas 		return -EINVAL;
56846ec8598SBjorn Helgaas 	return 0;
56946ec8598SBjorn Helgaas }
57046ec8598SBjorn Helgaas 
57146ec8598SBjorn Helgaas static void acpi_device_remove_notify_handler(struct acpi_device *device)
57246ec8598SBjorn Helgaas {
573ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
57446ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
57546ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
576ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
57746ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
57846ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
57946ec8598SBjorn Helgaas 	else
58046ec8598SBjorn Helgaas 		acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
58146ec8598SBjorn Helgaas 					   acpi_device_notify);
58246ec8598SBjorn Helgaas }
58346ec8598SBjorn Helgaas 
5845d9464a4SPatrick Mochel static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
5855d9464a4SPatrick Mochel static int acpi_device_probe(struct device * dev)
5865d9464a4SPatrick Mochel {
5875d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
5885d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
5895d9464a4SPatrick Mochel 	int ret;
5905d9464a4SPatrick Mochel 
5915d9464a4SPatrick Mochel 	ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
5925d9464a4SPatrick Mochel 	if (!ret) {
59346ec8598SBjorn Helgaas 		if (acpi_drv->ops.notify) {
59446ec8598SBjorn Helgaas 			ret = acpi_device_install_notify_handler(acpi_dev);
59546ec8598SBjorn Helgaas 			if (ret) {
59646ec8598SBjorn Helgaas 				if (acpi_drv->ops.remove)
59746ec8598SBjorn Helgaas 					acpi_drv->ops.remove(acpi_dev,
59846ec8598SBjorn Helgaas 						     acpi_dev->removal_type);
59946ec8598SBjorn Helgaas 				return ret;
60046ec8598SBjorn Helgaas 			}
60146ec8598SBjorn Helgaas 		}
60246ec8598SBjorn Helgaas 
6035d9464a4SPatrick Mochel 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
6045d9464a4SPatrick Mochel 			"Found driver [%s] for device [%s]\n",
6055d9464a4SPatrick Mochel 			acpi_drv->name, acpi_dev->pnp.bus_id));
6065d9464a4SPatrick Mochel 		get_device(dev);
6075d9464a4SPatrick Mochel 	}
6085d9464a4SPatrick Mochel 	return ret;
6095d9464a4SPatrick Mochel }
6105d9464a4SPatrick Mochel 
6115d9464a4SPatrick Mochel static int acpi_device_remove(struct device * dev)
6125d9464a4SPatrick Mochel {
6135d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
6145d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
6155d9464a4SPatrick Mochel 
6165d9464a4SPatrick Mochel 	if (acpi_drv) {
61746ec8598SBjorn Helgaas 		if (acpi_drv->ops.notify)
61846ec8598SBjorn Helgaas 			acpi_device_remove_notify_handler(acpi_dev);
6195d9464a4SPatrick Mochel 		if (acpi_drv->ops.remove)
62096333578SLi Shaohua 			acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
6215d9464a4SPatrick Mochel 	}
6225d9464a4SPatrick Mochel 	acpi_dev->driver = NULL;
623db89b4f0SPavel Machek 	acpi_dev->driver_data = NULL;
6245d9464a4SPatrick Mochel 
6255d9464a4SPatrick Mochel 	put_device(dev);
6265d9464a4SPatrick Mochel 	return 0;
6275d9464a4SPatrick Mochel }
6285d9464a4SPatrick Mochel 
62955955aadSDavid Brownell struct bus_type acpi_bus_type = {
6309e89dde2SZhang Rui 	.name		= "acpi",
6315d9464a4SPatrick Mochel 	.match		= acpi_bus_match,
6325d9464a4SPatrick Mochel 	.probe		= acpi_device_probe,
6335d9464a4SPatrick Mochel 	.remove		= acpi_device_remove,
6345d9464a4SPatrick Mochel 	.uevent		= acpi_device_uevent,
6351da177e4SLinus Torvalds };
6361da177e4SLinus Torvalds 
63782c7d5efSRafael J. Wysocki int acpi_device_register(struct acpi_device *device,
63882c7d5efSRafael J. Wysocki 			 void (*release)(struct device *))
6391da177e4SLinus Torvalds {
640e49bd2ddSZhang Rui 	int result;
641e49bd2ddSZhang Rui 	struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
642e49bd2ddSZhang Rui 	int found = 0;
64366b7ed40SBjorn Helgaas 
644d43e167dSRafael J. Wysocki 	if (device->handle) {
645d43e167dSRafael J. Wysocki 		acpi_status status;
646d43e167dSRafael J. Wysocki 
647d43e167dSRafael J. Wysocki 		status = acpi_attach_data(device->handle, acpi_bus_data_handler,
648d43e167dSRafael J. Wysocki 					  device);
649d43e167dSRafael J. Wysocki 		if (ACPI_FAILURE(status)) {
650d43e167dSRafael J. Wysocki 			acpi_handle_err(device->handle,
651d43e167dSRafael J. Wysocki 					"Unable to attach device data\n");
652d43e167dSRafael J. Wysocki 			return -ENODEV;
653d43e167dSRafael J. Wysocki 		}
654d43e167dSRafael J. Wysocki 	}
655d43e167dSRafael J. Wysocki 
6561da177e4SLinus Torvalds 	/*
6571da177e4SLinus Torvalds 	 * Linkage
6581da177e4SLinus Torvalds 	 * -------
6591da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
6601da177e4SLinus Torvalds 	 */
6611da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->children);
6621da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->node);
6631da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
6641033f904SLan Tianyu 	INIT_LIST_HEAD(&device->physical_node_list);
6651033f904SLan Tianyu 	mutex_init(&device->physical_node_lock);
666bc9b6407SRafael J. Wysocki 	INIT_LIST_HEAD(&device->power_dependent);
6671da177e4SLinus Torvalds 
668e49bd2ddSZhang Rui 	new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
669e49bd2ddSZhang Rui 	if (!new_bus_id) {
670d43e167dSRafael J. Wysocki 		pr_err(PREFIX "Memory allocation error\n");
671d43e167dSRafael J. Wysocki 		result = -ENOMEM;
672d43e167dSRafael J. Wysocki 		goto err_detach;
673e49bd2ddSZhang Rui 	}
674e49bd2ddSZhang Rui 
6759090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
676e49bd2ddSZhang Rui 	/*
677e49bd2ddSZhang Rui 	 * Find suitable bus_id and instance number in acpi_bus_id_list
678e49bd2ddSZhang Rui 	 * If failed, create one and link it into acpi_bus_id_list
679e49bd2ddSZhang Rui 	 */
680e49bd2ddSZhang Rui 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
6811131b938SBjorn Helgaas 		if (!strcmp(acpi_device_bus_id->bus_id,
6821131b938SBjorn Helgaas 			    acpi_device_hid(device))) {
683e49bd2ddSZhang Rui 			acpi_device_bus_id->instance_no++;
684e49bd2ddSZhang Rui 			found = 1;
685e49bd2ddSZhang Rui 			kfree(new_bus_id);
686e49bd2ddSZhang Rui 			break;
687e49bd2ddSZhang Rui 		}
688e49bd2ddSZhang Rui 	}
689e49bd2ddSZhang Rui 	if (!found) {
690e49bd2ddSZhang Rui 		acpi_device_bus_id = new_bus_id;
6911131b938SBjorn Helgaas 		strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
692e49bd2ddSZhang Rui 		acpi_device_bus_id->instance_no = 0;
693e49bd2ddSZhang Rui 		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
694e49bd2ddSZhang Rui 	}
6950794469dSKay Sievers 	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
696e49bd2ddSZhang Rui 
69733b57150SLen Brown 	if (device->parent)
6981da177e4SLinus Torvalds 		list_add_tail(&device->node, &device->parent->children);
69933b57150SLen Brown 
7001da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
7011da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
7029090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
7031da177e4SLinus Torvalds 
7041890a97aSPatrick Mochel 	if (device->parent)
70566b7ed40SBjorn Helgaas 		device->dev.parent = &device->parent->dev;
7061890a97aSPatrick Mochel 	device->dev.bus = &acpi_bus_type;
70782c7d5efSRafael J. Wysocki 	device->dev.release = release;
7088b12b922SAlex Chiang 	result = device_register(&device->dev);
709e49bd2ddSZhang Rui 	if (result) {
7108b12b922SAlex Chiang 		dev_err(&device->dev, "Error registering device\n");
711d43e167dSRafael J. Wysocki 		goto err;
712e49bd2ddSZhang Rui 	}
713f883d9dbSPatrick Mochel 
714e49bd2ddSZhang Rui 	result = acpi_device_setup_files(device);
715e49bd2ddSZhang Rui 	if (result)
7160794469dSKay Sievers 		printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
7170794469dSKay Sievers 		       dev_name(&device->dev));
718e49bd2ddSZhang Rui 
71996333578SLi Shaohua 	device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
720e49bd2ddSZhang Rui 	return 0;
721d43e167dSRafael J. Wysocki 
722d43e167dSRafael J. Wysocki  err:
7239090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
72433b57150SLen Brown 	if (device->parent)
725e49bd2ddSZhang Rui 		list_del(&device->node);
726e49bd2ddSZhang Rui 	list_del(&device->wakeup_list);
7279090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
728d43e167dSRafael J. Wysocki 
729d43e167dSRafael J. Wysocki  err_detach:
730d43e167dSRafael J. Wysocki 	acpi_detach_data(device->handle, acpi_bus_data_handler);
731e49bd2ddSZhang Rui 	return result;
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
734b17b537aSRafael J. Wysocki static void acpi_device_unregister(struct acpi_device *device)
7351da177e4SLinus Torvalds {
7369090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
73733b57150SLen Brown 	if (device->parent)
7381da177e4SLinus Torvalds 		list_del(&device->node);
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	list_del(&device->wakeup_list);
7419090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds 	acpi_detach_data(device->handle, acpi_bus_data_handler);
7441890a97aSPatrick Mochel 
745bc9b6407SRafael J. Wysocki 	acpi_power_add_remove_device(device, false);
746f883d9dbSPatrick Mochel 	acpi_device_remove_files(device);
7471890a97aSPatrick Mochel 	device_unregister(&device->dev);
748bc9b6407SRafael J. Wysocki 	/*
749bc9b6407SRafael J. Wysocki 	 * Drop the reference counts of all power resources the device depends
750bc9b6407SRafael J. Wysocki 	 * on and turn off the ones that have no more references.
751bc9b6407SRafael J. Wysocki 	 */
752bc9b6407SRafael J. Wysocki 	acpi_power_transition(device, ACPI_STATE_D3_COLD);
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
7559e89dde2SZhang Rui /* --------------------------------------------------------------------------
7569e89dde2SZhang Rui                                  Driver Management
7579e89dde2SZhang Rui    -------------------------------------------------------------------------- */
7581da177e4SLinus Torvalds /**
759d758a8faSRandy Dunlap  * acpi_bus_driver_init - add a device to a driver
760d758a8faSRandy Dunlap  * @device: the device to add and initialize
761d758a8faSRandy Dunlap  * @driver: driver for the device
762d758a8faSRandy Dunlap  *
7631da177e4SLinus Torvalds  * Used to initialize a device via its device driver.  Called whenever a
7641890a97aSPatrick Mochel  * driver is bound to a device.  Invokes the driver's add() ops.
7651da177e4SLinus Torvalds  */
7661da177e4SLinus Torvalds static int
7674be44fcdSLen Brown acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
7681da177e4SLinus Torvalds {
7691da177e4SLinus Torvalds 	int result = 0;
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 	if (!device || !driver)
772d550d98dSPatrick Mochel 		return -EINVAL;
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	if (!driver->ops.add)
775d550d98dSPatrick Mochel 		return -ENOSYS;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 	result = driver->ops.add(device);
7781da177e4SLinus Torvalds 	if (result) {
7791da177e4SLinus Torvalds 		device->driver = NULL;
780db89b4f0SPavel Machek 		device->driver_data = NULL;
781d550d98dSPatrick Mochel 		return result;
7821da177e4SLinus Torvalds 	}
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	device->driver = driver;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	/*
7871da177e4SLinus Torvalds 	 * TBD - Configuration Management: Assign resources to device based
7881da177e4SLinus Torvalds 	 * upon possible configuration and currently allocated resources.
7891da177e4SLinus Torvalds 	 */
7901da177e4SLinus Torvalds 
7914be44fcdSLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
7924be44fcdSLen Brown 			  "Driver successfully bound to device\n"));
793d550d98dSPatrick Mochel 	return 0;
7943fb02738SRajesh Shah }
7953fb02738SRajesh Shah 
7961da177e4SLinus Torvalds /**
797d758a8faSRandy Dunlap  * acpi_bus_register_driver - register a driver with the ACPI bus
798d758a8faSRandy Dunlap  * @driver: driver being registered
799d758a8faSRandy Dunlap  *
8001da177e4SLinus Torvalds  * Registers a driver with the ACPI bus.  Searches the namespace for all
8019d9f749bSBjorn Helgaas  * devices that match the driver's criteria and binds.  Returns zero for
8029d9f749bSBjorn Helgaas  * success or a negative error status for failure.
8031da177e4SLinus Torvalds  */
8044be44fcdSLen Brown int acpi_bus_register_driver(struct acpi_driver *driver)
8051da177e4SLinus Torvalds {
8061890a97aSPatrick Mochel 	int ret;
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	if (acpi_disabled)
809d550d98dSPatrick Mochel 		return -ENODEV;
8101890a97aSPatrick Mochel 	driver->drv.name = driver->name;
8111890a97aSPatrick Mochel 	driver->drv.bus = &acpi_bus_type;
8121890a97aSPatrick Mochel 	driver->drv.owner = driver->owner;
8131da177e4SLinus Torvalds 
8141890a97aSPatrick Mochel 	ret = driver_register(&driver->drv);
8151890a97aSPatrick Mochel 	return ret;
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds 
8184be44fcdSLen Brown EXPORT_SYMBOL(acpi_bus_register_driver);
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds /**
821d758a8faSRandy Dunlap  * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
822d758a8faSRandy Dunlap  * @driver: driver to unregister
823d758a8faSRandy Dunlap  *
8241da177e4SLinus Torvalds  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
8251da177e4SLinus Torvalds  * devices that match the driver's criteria and unbinds.
8261da177e4SLinus Torvalds  */
82706ea8e08SBjorn Helgaas void acpi_bus_unregister_driver(struct acpi_driver *driver)
8281da177e4SLinus Torvalds {
8291890a97aSPatrick Mochel 	driver_unregister(&driver->drv);
8301da177e4SLinus Torvalds }
8314be44fcdSLen Brown 
8321da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_unregister_driver);
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds /* --------------------------------------------------------------------------
8351da177e4SLinus Torvalds                                  Device Enumeration
8361da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
8375c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
8385c478f49SBjorn Helgaas {
8395c478f49SBjorn Helgaas 	acpi_status status;
8405c478f49SBjorn Helgaas 	int ret;
8415c478f49SBjorn Helgaas 	struct acpi_device *device;
8425c478f49SBjorn Helgaas 
8435c478f49SBjorn Helgaas 	/*
8445c478f49SBjorn Helgaas 	 * Fixed hardware devices do not appear in the namespace and do not
8455c478f49SBjorn Helgaas 	 * have handles, but we fabricate acpi_devices for them, so we have
8465c478f49SBjorn Helgaas 	 * to deal with them specially.
8475c478f49SBjorn Helgaas 	 */
8485c478f49SBjorn Helgaas 	if (handle == NULL)
8495c478f49SBjorn Helgaas 		return acpi_root;
8505c478f49SBjorn Helgaas 
8515c478f49SBjorn Helgaas 	do {
8525c478f49SBjorn Helgaas 		status = acpi_get_parent(handle, &handle);
8535c478f49SBjorn Helgaas 		if (status == AE_NULL_ENTRY)
8545c478f49SBjorn Helgaas 			return NULL;
8555c478f49SBjorn Helgaas 		if (ACPI_FAILURE(status))
8565c478f49SBjorn Helgaas 			return acpi_root;
8575c478f49SBjorn Helgaas 
8585c478f49SBjorn Helgaas 		ret = acpi_bus_get_device(handle, &device);
8595c478f49SBjorn Helgaas 		if (ret == 0)
8605c478f49SBjorn Helgaas 			return device;
8615c478f49SBjorn Helgaas 	} while (1);
8625c478f49SBjorn Helgaas }
8635c478f49SBjorn Helgaas 
864c8f7a62cSLen Brown acpi_status
865c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
866c8f7a62cSLen Brown {
867c8f7a62cSLen Brown 	acpi_status status;
868c8f7a62cSLen Brown 	acpi_handle tmp;
869c8f7a62cSLen Brown 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
870c8f7a62cSLen Brown 	union acpi_object *obj;
871c8f7a62cSLen Brown 
872c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_EJD", &tmp);
873c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
874c8f7a62cSLen Brown 		return status;
875c8f7a62cSLen Brown 
876c8f7a62cSLen Brown 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
877c8f7a62cSLen Brown 	if (ACPI_SUCCESS(status)) {
878c8f7a62cSLen Brown 		obj = buffer.pointer;
8793b5fee59SHolger Macht 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
8803b5fee59SHolger Macht 					 ejd);
881c8f7a62cSLen Brown 		kfree(buffer.pointer);
882c8f7a62cSLen Brown 	}
883c8f7a62cSLen Brown 	return status;
884c8f7a62cSLen Brown }
885c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
886c8f7a62cSLen Brown 
8878e4319c4SBob Moore void acpi_bus_data_handler(acpi_handle handle, void *context)
8881da177e4SLinus Torvalds {
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	/* TBD */
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	return;
8931da177e4SLinus Torvalds }
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds static acpi_status
896b581a7f9SRafael J. Wysocki acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
897b581a7f9SRafael J. Wysocki 					     struct acpi_device_wakeup *wakeup)
8981da177e4SLinus Torvalds {
899b581a7f9SRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
900b581a7f9SRafael J. Wysocki 	union acpi_object *package = NULL;
9011da177e4SLinus Torvalds 	union acpi_object *element = NULL;
902b581a7f9SRafael J. Wysocki 	acpi_status status;
903b581a7f9SRafael J. Wysocki 	int i = 0;
9041da177e4SLinus Torvalds 
905b581a7f9SRafael J. Wysocki 	if (!wakeup)
9061da177e4SLinus Torvalds 		return AE_BAD_PARAMETER;
9071da177e4SLinus Torvalds 
908993cbe59SRafael J. Wysocki 	INIT_LIST_HEAD(&wakeup->resources);
909993cbe59SRafael J. Wysocki 
910b581a7f9SRafael J. Wysocki 	/* _PRW */
911b581a7f9SRafael J. Wysocki 	status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
912b581a7f9SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
913b581a7f9SRafael J. Wysocki 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
914b581a7f9SRafael J. Wysocki 		return status;
915b581a7f9SRafael J. Wysocki 	}
916b581a7f9SRafael J. Wysocki 
917b581a7f9SRafael J. Wysocki 	package = (union acpi_object *)buffer.pointer;
918b581a7f9SRafael J. Wysocki 
919b581a7f9SRafael J. Wysocki 	if (!package || (package->package.count < 2)) {
920b581a7f9SRafael J. Wysocki 		status = AE_BAD_DATA;
921b581a7f9SRafael J. Wysocki 		goto out;
922b581a7f9SRafael J. Wysocki 	}
923b581a7f9SRafael J. Wysocki 
9241da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
925b581a7f9SRafael J. Wysocki 	if (!element) {
926b581a7f9SRafael J. Wysocki 		status = AE_BAD_DATA;
927b581a7f9SRafael J. Wysocki 		goto out;
928b581a7f9SRafael J. Wysocki 	}
9291da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
9301da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
9311da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
9321da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
933b581a7f9SRafael J. Wysocki 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
934b581a7f9SRafael J. Wysocki 			status = AE_BAD_DATA;
935b581a7f9SRafael J. Wysocki 			goto out;
936b581a7f9SRafael J. Wysocki 		}
937b581a7f9SRafael J. Wysocki 		wakeup->gpe_device =
9381da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
939b581a7f9SRafael J. Wysocki 		wakeup->gpe_number =
9401da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
9411da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
942b581a7f9SRafael J. Wysocki 		wakeup->gpe_device = NULL;
943b581a7f9SRafael J. Wysocki 		wakeup->gpe_number = element->integer.value;
944b581a7f9SRafael J. Wysocki 	} else {
945b581a7f9SRafael J. Wysocki 		status = AE_BAD_DATA;
946b581a7f9SRafael J. Wysocki 		goto out;
947b581a7f9SRafael J. Wysocki 	}
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
9501da177e4SLinus Torvalds 	if (element->type != ACPI_TYPE_INTEGER) {
951b581a7f9SRafael J. Wysocki 		status = AE_BAD_DATA;
952b581a7f9SRafael J. Wysocki 		goto out;
9531da177e4SLinus Torvalds 	}
954b581a7f9SRafael J. Wysocki 	wakeup->sleep_state = element->integer.value;
9551da177e4SLinus Torvalds 
956993cbe59SRafael J. Wysocki 	for (i = 2; i < package->package.count; i++) {
957993cbe59SRafael J. Wysocki 		acpi_handle rhandle;
958993cbe59SRafael J. Wysocki 
959993cbe59SRafael J. Wysocki 		element = &(package->package.elements[i]);
960b581a7f9SRafael J. Wysocki 		if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
961b581a7f9SRafael J. Wysocki 			status = AE_BAD_DATA;
962b581a7f9SRafael J. Wysocki 			goto out;
9631da177e4SLinus Torvalds 		}
964993cbe59SRafael J. Wysocki 		rhandle = element->reference.handle;
965993cbe59SRafael J. Wysocki 		acpi_add_power_resource(rhandle);
966993cbe59SRafael J. Wysocki 		acpi_power_resources_list_add(rhandle, &wakeup->resources);
967b581a7f9SRafael J. Wysocki 	}
9689874647bSRafael J. Wysocki 
969bba63a29SLin Ming 	acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
970b581a7f9SRafael J. Wysocki 
971b581a7f9SRafael J. Wysocki  out:
972b581a7f9SRafael J. Wysocki 	kfree(buffer.pointer);
973b581a7f9SRafael J. Wysocki 
974b581a7f9SRafael J. Wysocki 	return status;
9751da177e4SLinus Torvalds }
9761da177e4SLinus Torvalds 
977f517709dSRafael J. Wysocki static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
9781da177e4SLinus Torvalds {
97929b71a1cSThomas Renninger 	struct acpi_device_id button_device_ids[] = {
98029b71a1cSThomas Renninger 		{"PNP0C0C", 0},
981b7e38304SZhang Rui 		{"PNP0C0D", 0},
98229b71a1cSThomas Renninger 		{"PNP0C0E", 0},
98329b71a1cSThomas Renninger 		{"", 0},
98429b71a1cSThomas Renninger 	};
985f517709dSRafael J. Wysocki 	acpi_status status;
986f517709dSRafael J. Wysocki 	acpi_event_status event_status;
987f517709dSRafael J. Wysocki 
988b67ea761SRafael J. Wysocki 	device->wakeup.flags.notifier_present = 0;
989f517709dSRafael J. Wysocki 
990f517709dSRafael J. Wysocki 	/* Power button, Lid switch always enable wakeup */
991f517709dSRafael J. Wysocki 	if (!acpi_match_device_ids(device, button_device_ids)) {
992f517709dSRafael J. Wysocki 		device->wakeup.flags.run_wake = 1;
993b7e38304SZhang Rui 		if (!acpi_match_device_ids(device, &button_device_ids[1])) {
994b7e38304SZhang Rui 			/* Do not use Lid/sleep button for S5 wakeup */
995b7e38304SZhang Rui 			if (device->wakeup.sleep_state == ACPI_STATE_S5)
996b7e38304SZhang Rui 				device->wakeup.sleep_state = ACPI_STATE_S4;
997b7e38304SZhang Rui 		}
998f2b56bc8SRafael J. Wysocki 		device_set_wakeup_capable(&device->dev, true);
999f517709dSRafael J. Wysocki 		return;
1000f517709dSRafael J. Wysocki 	}
1001f517709dSRafael J. Wysocki 
1002e8e18c95SRafael J. Wysocki 	status = acpi_get_gpe_status(device->wakeup.gpe_device,
1003e8e18c95SRafael J. Wysocki 					device->wakeup.gpe_number,
10040f849d2cSLin Ming 						&event_status);
1005f517709dSRafael J. Wysocki 	if (status == AE_OK)
1006f517709dSRafael J. Wysocki 		device->wakeup.flags.run_wake =
1007f517709dSRafael J. Wysocki 				!!(event_status & ACPI_EVENT_FLAG_HANDLE);
1008f517709dSRafael J. Wysocki }
1009f517709dSRafael J. Wysocki 
1010d57d09a4SRafael J. Wysocki static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1011f517709dSRafael J. Wysocki {
1012d57d09a4SRafael J. Wysocki 	acpi_handle temp;
1013f517709dSRafael J. Wysocki 	acpi_status status = 0;
1014f517709dSRafael J. Wysocki 	int psw_error;
101529b71a1cSThomas Renninger 
1016d57d09a4SRafael J. Wysocki 	/* Presence of _PRW indicates wake capable */
1017d57d09a4SRafael J. Wysocki 	status = acpi_get_handle(device->handle, "_PRW", &temp);
1018d57d09a4SRafael J. Wysocki 	if (ACPI_FAILURE(status))
1019d57d09a4SRafael J. Wysocki 		return;
1020d57d09a4SRafael J. Wysocki 
1021b581a7f9SRafael J. Wysocki 	status = acpi_bus_extract_wakeup_device_power_package(device->handle,
1022b581a7f9SRafael J. Wysocki 							      &device->wakeup);
10231da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
1024993cbe59SRafael J. Wysocki 		acpi_power_resources_list_free(&device->wakeup.resources);
10251da177e4SLinus Torvalds 		ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
1026d57d09a4SRafael J. Wysocki 		return;
10271da177e4SLinus Torvalds 	}
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	device->wakeup.flags.valid = 1;
10309b83ccd2SRafael J. Wysocki 	device->wakeup.prepare_count = 0;
1031f517709dSRafael J. Wysocki 	acpi_bus_set_run_wake_flags(device);
1032729b2bdbSZhao Yakui 	/* Call _PSW/_DSW object to disable its ability to wake the sleeping
1033729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
1034729b2bdbSZhao Yakui 	 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1035729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
1036729b2bdbSZhao Yakui 	 * present will the _PSW object used.
1037729b2bdbSZhao Yakui 	 */
103877e76609SRafael J. Wysocki 	psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
103977e76609SRafael J. Wysocki 	if (psw_error)
104077e76609SRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
104177e76609SRafael J. Wysocki 				"error in _DSW or _PSW evaluation\n"));
10421da177e4SLinus Torvalds }
10431da177e4SLinus Torvalds 
1044f33ce568SRafael J. Wysocki static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1045f33ce568SRafael J. Wysocki {
1046f33ce568SRafael J. Wysocki 	struct acpi_device_power_state *ps = &device->power.states[state];
1047f33ce568SRafael J. Wysocki 	char object_name[5] = { '_', 'P', 'R', '0' + state, '\0' };
1048f33ce568SRafael J. Wysocki 	struct acpi_handle_list resources;
1049f33ce568SRafael J. Wysocki 	acpi_handle handle;
1050f33ce568SRafael J. Wysocki 	acpi_status status;
1051f33ce568SRafael J. Wysocki 
1052f33ce568SRafael J. Wysocki 	INIT_LIST_HEAD(&ps->resources);
1053f33ce568SRafael J. Wysocki 
1054f33ce568SRafael J. Wysocki 	/* Evaluate "_PRx" to se if power resources are referenced */
1055f33ce568SRafael J. Wysocki 	acpi_evaluate_reference(device->handle, object_name, NULL, &resources);
1056f33ce568SRafael J. Wysocki 	if (resources.count) {
1057f33ce568SRafael J. Wysocki 		int j;
1058f33ce568SRafael J. Wysocki 
1059f33ce568SRafael J. Wysocki 		device->power.flags.power_resources = 1;
1060f33ce568SRafael J. Wysocki 		for (j = 0; j < resources.count; j++) {
1061f33ce568SRafael J. Wysocki 			acpi_handle rhandle = resources.handles[j];
1062f33ce568SRafael J. Wysocki 
1063f33ce568SRafael J. Wysocki 			acpi_add_power_resource(rhandle);
1064f33ce568SRafael J. Wysocki 			acpi_power_resources_list_add(rhandle, &ps->resources);
1065f33ce568SRafael J. Wysocki 		}
1066f33ce568SRafael J. Wysocki 	}
1067f33ce568SRafael J. Wysocki 
1068f33ce568SRafael J. Wysocki 	/* Evaluate "_PSx" to see if we can do explicit sets */
1069f33ce568SRafael J. Wysocki 	object_name[2] = 'S';
1070f33ce568SRafael J. Wysocki 	status = acpi_get_handle(device->handle, object_name, &handle);
1071f33ce568SRafael J. Wysocki 	if (ACPI_SUCCESS(status))
1072f33ce568SRafael J. Wysocki 		ps->flags.explicit_set = 1;
1073f33ce568SRafael J. Wysocki 
1074f33ce568SRafael J. Wysocki 	/*
1075f33ce568SRafael J. Wysocki 	 * State is valid if there are means to put the device into it.
1076f33ce568SRafael J. Wysocki 	 * D3hot is only valid if _PR3 present.
1077f33ce568SRafael J. Wysocki 	 */
1078f33ce568SRafael J. Wysocki 	if (resources.count
1079f33ce568SRafael J. Wysocki 	    || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1080f33ce568SRafael J. Wysocki 		ps->flags.valid = 1;
1081f33ce568SRafael J. Wysocki 		ps->flags.os_accessible = 1;
1082f33ce568SRafael J. Wysocki 	}
1083f33ce568SRafael J. Wysocki 
1084f33ce568SRafael J. Wysocki 	ps->power = -1;		/* Unknown - driver assigned */
1085f33ce568SRafael J. Wysocki 	ps->latency = -1;	/* Unknown - driver assigned */
1086f33ce568SRafael J. Wysocki }
1087f33ce568SRafael J. Wysocki 
1088d43e167dSRafael J. Wysocki static void acpi_bus_get_power_flags(struct acpi_device *device)
10891da177e4SLinus Torvalds {
1090*8bc5053bSRafael J. Wysocki 	acpi_status status;
1091*8bc5053bSRafael J. Wysocki 	acpi_handle handle;
1092*8bc5053bSRafael J. Wysocki 	u32 i;
10939e89dde2SZhang Rui 
1094d43e167dSRafael J. Wysocki 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
1095d43e167dSRafael J. Wysocki 	status = acpi_get_handle(device->handle, "_PS0", &handle);
1096d43e167dSRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
1097d43e167dSRafael J. Wysocki 		status = acpi_get_handle(device->handle, "_PR0", &handle);
1098d43e167dSRafael J. Wysocki 		if (ACPI_FAILURE(status))
1099d43e167dSRafael J. Wysocki 			return;
1100d43e167dSRafael J. Wysocki 	}
1101d43e167dSRafael J. Wysocki 
1102d43e167dSRafael J. Wysocki 	device->flags.power_manageable = 1;
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	/*
11059e89dde2SZhang Rui 	 * Power Management Flags
11061da177e4SLinus Torvalds 	 */
11079e89dde2SZhang Rui 	status = acpi_get_handle(device->handle, "_PSC", &handle);
11081da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
11099e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
11109e89dde2SZhang Rui 	status = acpi_get_handle(device->handle, "_IRC", &handle);
11119e89dde2SZhang Rui 	if (ACPI_SUCCESS(status))
11129e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
11131da177e4SLinus Torvalds 
11141da177e4SLinus Torvalds 	/*
11159e89dde2SZhang Rui 	 * Enumerate supported power management states
11161da177e4SLinus Torvalds 	 */
1117f33ce568SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1118f33ce568SRafael J. Wysocki 		acpi_bus_init_power_state(device, i);
11199e89dde2SZhang Rui 
11200b224527SRafael J. Wysocki 	INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
11210b224527SRafael J. Wysocki 
11229e89dde2SZhang Rui 	/* Set defaults for D0 and D3 states (always valid) */
11239e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
11249e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
11259e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D3].flags.valid = 1;
11269e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D3].power = 0;
11279e89dde2SZhang Rui 
11285c7dd710SRafael J. Wysocki 	/* Set D3cold's explicit_set flag if _PS3 exists. */
11295c7dd710SRafael J. Wysocki 	if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
11305c7dd710SRafael J. Wysocki 		device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
11315c7dd710SRafael J. Wysocki 
11321399dfcdSAaron Lu 	/* Presence of _PS3 or _PRx means we can put the device into D3 cold */
11331399dfcdSAaron Lu 	if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
11341399dfcdSAaron Lu 			device->power.flags.power_resources)
11351399dfcdSAaron Lu 		device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
11361399dfcdSAaron Lu 
1137ade3e7feSRafael J. Wysocki 	acpi_bus_init_power(device);
11389e89dde2SZhang Rui }
1139c8f7a62cSLen Brown 
1140d43e167dSRafael J. Wysocki static void acpi_bus_get_flags(struct acpi_device *device)
11411da177e4SLinus Torvalds {
11421da177e4SLinus Torvalds 	acpi_status status = AE_OK;
11431da177e4SLinus Torvalds 	acpi_handle temp = NULL;
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
11461da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_STA", &temp);
11471da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
11481da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
11511da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_RMV", &temp);
11521da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
11531da177e4SLinus Torvalds 		device->flags.removable = 1;
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
11561da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_EJD", &temp);
11571da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
11581da177e4SLinus Torvalds 		device->flags.ejectable = 1;
11591da177e4SLinus Torvalds 	else {
11601da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_EJ0", &temp);
11611da177e4SLinus Torvalds 		if (ACPI_SUCCESS(status))
11621da177e4SLinus Torvalds 			device->flags.ejectable = 1;
11631da177e4SLinus Torvalds 	}
11641da177e4SLinus Torvalds }
11651da177e4SLinus Torvalds 
1166c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device)
11671da177e4SLinus Torvalds {
11681da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
11691da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
11701da177e4SLinus Torvalds 	int i = 0;
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 	/*
11731da177e4SLinus Torvalds 	 * Bus ID
11741da177e4SLinus Torvalds 	 * ------
11751da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
11761da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
11771da177e4SLinus Torvalds 	 */
1178859ac9a4SBjorn Helgaas 	if (ACPI_IS_ROOT_DEVICE(device)) {
11791da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
1180859ac9a4SBjorn Helgaas 		return;
1181859ac9a4SBjorn Helgaas 	}
1182859ac9a4SBjorn Helgaas 
1183859ac9a4SBjorn Helgaas 	switch (device->device_type) {
11841da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
11851da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
11861da177e4SLinus Torvalds 		break;
11871da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
11881da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
11891da177e4SLinus Torvalds 		break;
11901da177e4SLinus Torvalds 	default:
119166b7ed40SBjorn Helgaas 		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
11921da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
11931da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
11941da177e4SLinus Torvalds 			if (bus_id[i] == '_')
11951da177e4SLinus Torvalds 				bus_id[i] = '\0';
11961da177e4SLinus Torvalds 			else
11971da177e4SLinus Torvalds 				break;
11981da177e4SLinus Torvalds 		}
11991da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
12001da177e4SLinus Torvalds 		break;
12011da177e4SLinus Torvalds 	}
12021da177e4SLinus Torvalds }
12031da177e4SLinus Torvalds 
120454735266SZhang Rui /*
120554735266SZhang Rui  * acpi_bay_match - see if a device is an ejectable driver bay
120654735266SZhang Rui  *
120754735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
120854735266SZhang Rui  * then we can safely call it an ejectable drive bay
120954735266SZhang Rui  */
121054735266SZhang Rui static int acpi_bay_match(struct acpi_device *device){
121154735266SZhang Rui 	acpi_status status;
121254735266SZhang Rui 	acpi_handle handle;
121354735266SZhang Rui 	acpi_handle tmp;
121454735266SZhang Rui 	acpi_handle phandle;
121554735266SZhang Rui 
121654735266SZhang Rui 	handle = device->handle;
121754735266SZhang Rui 
121854735266SZhang Rui 	status = acpi_get_handle(handle, "_EJ0", &tmp);
121954735266SZhang Rui 	if (ACPI_FAILURE(status))
122054735266SZhang Rui 		return -ENODEV;
122154735266SZhang Rui 
122254735266SZhang Rui 	if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
122354735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
122454735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
122554735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
122654735266SZhang Rui 		return 0;
122754735266SZhang Rui 
122854735266SZhang Rui 	if (acpi_get_parent(handle, &phandle))
122954735266SZhang Rui 		return -ENODEV;
123054735266SZhang Rui 
123154735266SZhang Rui         if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
123254735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
123354735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
123454735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
123554735266SZhang Rui                 return 0;
123654735266SZhang Rui 
123754735266SZhang Rui 	return -ENODEV;
123854735266SZhang Rui }
123954735266SZhang Rui 
12403620f2f2SFrank Seidel /*
12413620f2f2SFrank Seidel  * acpi_dock_match - see if a device has a _DCK method
12423620f2f2SFrank Seidel  */
12433620f2f2SFrank Seidel static int acpi_dock_match(struct acpi_device *device)
12443620f2f2SFrank Seidel {
12453620f2f2SFrank Seidel 	acpi_handle tmp;
12463620f2f2SFrank Seidel 	return acpi_get_handle(device->handle, "_DCK", &tmp);
12473620f2f2SFrank Seidel }
12483620f2f2SFrank Seidel 
1249620e112cSThomas Renninger const char *acpi_device_hid(struct acpi_device *device)
125015b8dd53SBob Moore {
12517f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hid;
125215b8dd53SBob Moore 
12532b2ae7c7SThomas Renninger 	if (list_empty(&device->pnp.ids))
12542b2ae7c7SThomas Renninger 		return dummy_hid;
12552b2ae7c7SThomas Renninger 
12567f47fa6cSBjorn Helgaas 	hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
12577f47fa6cSBjorn Helgaas 	return hid->id;
12587f47fa6cSBjorn Helgaas }
12597f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid);
126015b8dd53SBob Moore 
12617f47fa6cSBjorn Helgaas static void acpi_add_id(struct acpi_device *device, const char *dev_id)
12627f47fa6cSBjorn Helgaas {
12637f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
126415b8dd53SBob Moore 
12657f47fa6cSBjorn Helgaas 	id = kmalloc(sizeof(*id), GFP_KERNEL);
12667f47fa6cSBjorn Helgaas 	if (!id)
12677f47fa6cSBjorn Helgaas 		return;
126815b8dd53SBob Moore 
1269581de59eSThomas Meyer 	id->id = kstrdup(dev_id, GFP_KERNEL);
12707f47fa6cSBjorn Helgaas 	if (!id->id) {
12717f47fa6cSBjorn Helgaas 		kfree(id);
12727f47fa6cSBjorn Helgaas 		return;
127315b8dd53SBob Moore 	}
127415b8dd53SBob Moore 
12757f47fa6cSBjorn Helgaas 	list_add_tail(&id->list, &device->pnp.ids);
127615b8dd53SBob Moore }
127715b8dd53SBob Moore 
1278222e82acSDarrick J. Wong /*
1279222e82acSDarrick J. Wong  * Old IBM workstations have a DSDT bug wherein the SMBus object
1280222e82acSDarrick J. Wong  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1281222e82acSDarrick J. Wong  * prefix.  Work around this.
1282222e82acSDarrick J. Wong  */
1283222e82acSDarrick J. Wong static int acpi_ibm_smbus_match(struct acpi_device *device)
1284222e82acSDarrick J. Wong {
1285222e82acSDarrick J. Wong 	acpi_handle h_dummy;
1286222e82acSDarrick J. Wong 	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1287222e82acSDarrick J. Wong 	int result;
1288222e82acSDarrick J. Wong 
1289222e82acSDarrick J. Wong 	if (!dmi_name_in_vendors("IBM"))
1290222e82acSDarrick J. Wong 		return -ENODEV;
1291222e82acSDarrick J. Wong 
1292222e82acSDarrick J. Wong 	/* Look for SMBS object */
1293222e82acSDarrick J. Wong 	result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1294222e82acSDarrick J. Wong 	if (result)
1295222e82acSDarrick J. Wong 		return result;
1296222e82acSDarrick J. Wong 
1297222e82acSDarrick J. Wong 	if (strcmp("SMBS", path.pointer)) {
1298222e82acSDarrick J. Wong 		result = -ENODEV;
1299222e82acSDarrick J. Wong 		goto out;
1300222e82acSDarrick J. Wong 	}
1301222e82acSDarrick J. Wong 
1302222e82acSDarrick J. Wong 	/* Does it have the necessary (but misnamed) methods? */
1303222e82acSDarrick J. Wong 	result = -ENODEV;
1304222e82acSDarrick J. Wong 	if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1305222e82acSDarrick J. Wong 	    ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1306222e82acSDarrick J. Wong 	    ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1307222e82acSDarrick J. Wong 		result = 0;
1308222e82acSDarrick J. Wong out:
1309222e82acSDarrick J. Wong 	kfree(path.pointer);
1310222e82acSDarrick J. Wong 	return result;
1311222e82acSDarrick J. Wong }
1312222e82acSDarrick J. Wong 
1313c7bcb4e9SBjorn Helgaas static void acpi_device_set_id(struct acpi_device *device)
13141da177e4SLinus Torvalds {
13151da177e4SLinus Torvalds 	acpi_status status;
131657f3674fSBjorn Helgaas 	struct acpi_device_info *info;
131778e25fefSLv Zheng 	struct acpi_pnp_device_id_list *cid_list;
13187f47fa6cSBjorn Helgaas 	int i;
13191da177e4SLinus Torvalds 
1320c7bcb4e9SBjorn Helgaas 	switch (device->device_type) {
13211da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
1322859ac9a4SBjorn Helgaas 		if (ACPI_IS_ROOT_DEVICE(device)) {
132357f3674fSBjorn Helgaas 			acpi_add_id(device, ACPI_SYSTEM_HID);
1324859ac9a4SBjorn Helgaas 			break;
1325859ac9a4SBjorn Helgaas 		}
1326859ac9a4SBjorn Helgaas 
132766b7ed40SBjorn Helgaas 		status = acpi_get_object_info(device->handle, &info);
13281da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
132996b2dd1fSHarvey Harrison 			printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
13301da177e4SLinus Torvalds 			return;
13311da177e4SLinus Torvalds 		}
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_HID)
133457f3674fSBjorn Helgaas 			acpi_add_id(device, info->hardware_id.string);
133557f3674fSBjorn Helgaas 		if (info->valid & ACPI_VALID_CID) {
133615b8dd53SBob Moore 			cid_list = &info->compatible_id_list;
133757f3674fSBjorn Helgaas 			for (i = 0; i < cid_list->count; i++)
133857f3674fSBjorn Helgaas 				acpi_add_id(device, cid_list->ids[i].string);
133957f3674fSBjorn Helgaas 		}
13401da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
13411da177e4SLinus Torvalds 			device->pnp.bus_address = info->address;
13421da177e4SLinus Torvalds 			device->flags.bus_address = 1;
13431da177e4SLinus Torvalds 		}
1344ccf78040SLv Zheng 		if (info->valid & ACPI_VALID_UID)
1345ccf78040SLv Zheng 			device->pnp.unique_id = kstrdup(info->unique_id.string,
1346ccf78040SLv Zheng 							GFP_KERNEL);
1347ae843332SZhang Rui 
1348a83893aeSBjorn Helgaas 		kfree(info);
1349a83893aeSBjorn Helgaas 
135057f3674fSBjorn Helgaas 		/*
135157f3674fSBjorn Helgaas 		 * Some devices don't reliably have _HIDs & _CIDs, so add
135257f3674fSBjorn Helgaas 		 * synthetic HIDs to make sure drivers can find them.
13533620f2f2SFrank Seidel 		 */
1354c3d6de69SThomas Renninger 		if (acpi_is_video_device(device))
135557f3674fSBjorn Helgaas 			acpi_add_id(device, ACPI_VIDEO_HID);
13563620f2f2SFrank Seidel 		else if (ACPI_SUCCESS(acpi_bay_match(device)))
135757f3674fSBjorn Helgaas 			acpi_add_id(device, ACPI_BAY_HID);
13583620f2f2SFrank Seidel 		else if (ACPI_SUCCESS(acpi_dock_match(device)))
135957f3674fSBjorn Helgaas 			acpi_add_id(device, ACPI_DOCK_HID);
1360222e82acSDarrick J. Wong 		else if (!acpi_ibm_smbus_match(device))
1361222e82acSDarrick J. Wong 			acpi_add_id(device, ACPI_SMBUS_IBM_HID);
13624f5f64cfSRafael J. Wysocki 		else if (list_empty(&device->pnp.ids) &&
1363b7b30de5SBjorn Helgaas 			 ACPI_IS_ROOT_DEVICE(device->parent)) {
1364b7b30de5SBjorn Helgaas 			acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1365b7b30de5SBjorn Helgaas 			strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1366b7b30de5SBjorn Helgaas 			strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1367b7b30de5SBjorn Helgaas 		}
136854735266SZhang Rui 
13691da177e4SLinus Torvalds 		break;
13701da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
137157f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_POWER_HID);
13721da177e4SLinus Torvalds 		break;
13731da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
137457f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
13751da177e4SLinus Torvalds 		break;
13761da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
137757f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_THERMAL_HID);
13781da177e4SLinus Torvalds 		break;
13791da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
138057f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
13811da177e4SLinus Torvalds 		break;
13821da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
138357f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
13841da177e4SLinus Torvalds 		break;
13851da177e4SLinus Torvalds 	}
13861da177e4SLinus Torvalds }
13871da177e4SLinus Torvalds 
138882c7d5efSRafael J. Wysocki void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1389d43e167dSRafael J. Wysocki 			     int type, unsigned long long sta)
13901da177e4SLinus Torvalds {
1391d43e167dSRafael J. Wysocki 	INIT_LIST_HEAD(&device->pnp.ids);
1392d43e167dSRafael J. Wysocki 	device->device_type = type;
1393d43e167dSRafael J. Wysocki 	device->handle = handle;
1394d43e167dSRafael J. Wysocki 	device->parent = acpi_bus_get_parent(handle);
1395d43e167dSRafael J. Wysocki 	STRUCT_TO_INT(device->status) = sta;
1396d43e167dSRafael J. Wysocki 	acpi_device_get_busid(device);
1397d43e167dSRafael J. Wysocki 	acpi_device_set_id(device);
1398d43e167dSRafael J. Wysocki 	acpi_bus_get_flags(device);
13991da177e4SLinus Torvalds }
14001da177e4SLinus Torvalds 
14015c478f49SBjorn Helgaas static int acpi_add_single_object(struct acpi_device **child,
14025c478f49SBjorn Helgaas 				  acpi_handle handle, int type,
1403209d3b17SRafael J. Wysocki 				  unsigned long long sta, bool match_driver)
14041da177e4SLinus Torvalds {
140577c24888SBjorn Helgaas 	int result;
140677c24888SBjorn Helgaas 	struct acpi_device *device;
140729aaefa6SBjorn Helgaas 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
14081da177e4SLinus Torvalds 
140936bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
14101da177e4SLinus Torvalds 	if (!device) {
14116468463aSLen Brown 		printk(KERN_ERR PREFIX "Memory allocation error\n");
1412d550d98dSPatrick Mochel 		return -ENOMEM;
14131da177e4SLinus Torvalds 	}
14141da177e4SLinus Torvalds 
1415d43e167dSRafael J. Wysocki 	acpi_init_device_object(device, handle, type, sta);
1416d43e167dSRafael J. Wysocki 	acpi_bus_get_power_flags(device);
1417d57d09a4SRafael J. Wysocki 	acpi_bus_get_wakeup_device_flags(device);
14181da177e4SLinus Torvalds 
1419209d3b17SRafael J. Wysocki 	device->flags.match_driver = match_driver;
142082c7d5efSRafael J. Wysocki 	result = acpi_device_register(device, acpi_device_release);
1421d43e167dSRafael J. Wysocki 	if (result) {
1422d43e167dSRafael J. Wysocki 		acpi_device_release(&device->dev);
1423d43e167dSRafael J. Wysocki 		return result;
1424d43e167dSRafael J. Wysocki 	}
14251da177e4SLinus Torvalds 
1426bc9b6407SRafael J. Wysocki 	acpi_power_add_remove_device(device, true);
142729aaefa6SBjorn Helgaas 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1428d43e167dSRafael J. Wysocki 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1429d43e167dSRafael J. Wysocki 		dev_name(&device->dev), (char *) buffer.pointer,
1430d43e167dSRafael J. Wysocki 		device->parent ? dev_name(&device->parent->dev) : "(null)"));
143129aaefa6SBjorn Helgaas 	kfree(buffer.pointer);
14321da177e4SLinus Torvalds 	*child = device;
1433d43e167dSRafael J. Wysocki 	return 0;
14341da177e4SLinus Torvalds }
14351da177e4SLinus Torvalds 
1436778cbc1dSBjorn Helgaas static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1437778cbc1dSBjorn Helgaas 				    unsigned long long *sta)
1438778cbc1dSBjorn Helgaas {
1439778cbc1dSBjorn Helgaas 	acpi_status status;
1440778cbc1dSBjorn Helgaas 	acpi_object_type acpi_type;
1441778cbc1dSBjorn Helgaas 
1442778cbc1dSBjorn Helgaas 	status = acpi_get_type(handle, &acpi_type);
1443778cbc1dSBjorn Helgaas 	if (ACPI_FAILURE(status))
1444778cbc1dSBjorn Helgaas 		return -ENODEV;
1445778cbc1dSBjorn Helgaas 
1446778cbc1dSBjorn Helgaas 	switch (acpi_type) {
1447778cbc1dSBjorn Helgaas 	case ACPI_TYPE_ANY:		/* for ACPI_ROOT_OBJECT */
1448778cbc1dSBjorn Helgaas 	case ACPI_TYPE_DEVICE:
1449778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_DEVICE;
1450778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1451778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1452778cbc1dSBjorn Helgaas 			return -ENODEV;
1453778cbc1dSBjorn Helgaas 		break;
1454778cbc1dSBjorn Helgaas 	case ACPI_TYPE_PROCESSOR:
1455778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_PROCESSOR;
1456778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1457778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1458778cbc1dSBjorn Helgaas 			return -ENODEV;
1459778cbc1dSBjorn Helgaas 		break;
1460778cbc1dSBjorn Helgaas 	case ACPI_TYPE_THERMAL:
1461778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_THERMAL;
1462778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1463778cbc1dSBjorn Helgaas 		break;
1464778cbc1dSBjorn Helgaas 	case ACPI_TYPE_POWER:
1465778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_POWER;
1466778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1467778cbc1dSBjorn Helgaas 		break;
1468778cbc1dSBjorn Helgaas 	default:
1469778cbc1dSBjorn Helgaas 		return -ENODEV;
1470778cbc1dSBjorn Helgaas 	}
1471778cbc1dSBjorn Helgaas 
1472778cbc1dSBjorn Helgaas 	return 0;
1473778cbc1dSBjorn Helgaas }
1474778cbc1dSBjorn Helgaas 
1475e3863094SRafael J. Wysocki static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1476e3863094SRafael J. Wysocki 				      void *not_used, void **return_value)
14771da177e4SLinus Torvalds {
1478805d410fSRafael J. Wysocki 	struct acpi_device *device = NULL;
1479778cbc1dSBjorn Helgaas 	int type;
1480778cbc1dSBjorn Helgaas 	unsigned long long sta;
1481e3b87f8aSBjorn Helgaas 	acpi_status status;
1482778cbc1dSBjorn Helgaas 	int result;
14831da177e4SLinus Torvalds 
14844002bf38SRafael J. Wysocki 	acpi_bus_get_device(handle, &device);
14854002bf38SRafael J. Wysocki 	if (device)
14864002bf38SRafael J. Wysocki 		goto out;
14874002bf38SRafael J. Wysocki 
1488778cbc1dSBjorn Helgaas 	result = acpi_bus_type_and_status(handle, &type, &sta);
1489778cbc1dSBjorn Helgaas 	if (result)
149051a85fafSBjorn Helgaas 		return AE_OK;
14911da177e4SLinus Torvalds 
149282c7d5efSRafael J. Wysocki 	if (type == ACPI_BUS_TYPE_POWER) {
149382c7d5efSRafael J. Wysocki 		acpi_add_power_resource(handle);
149482c7d5efSRafael J. Wysocki 		return AE_OK;
149582c7d5efSRafael J. Wysocki 	}
149682c7d5efSRafael J. Wysocki 
1497778cbc1dSBjorn Helgaas 	if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1498b581a7f9SRafael J. Wysocki 	    !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
149986e4e20eSRafael J. Wysocki 		struct acpi_device_wakeup wakeup;
150086e4e20eSRafael J. Wysocki 		acpi_handle temp;
150186e4e20eSRafael J. Wysocki 
150286e4e20eSRafael J. Wysocki 		status = acpi_get_handle(handle, "_PRW", &temp);
1503993cbe59SRafael J. Wysocki 		if (ACPI_SUCCESS(status)) {
150486e4e20eSRafael J. Wysocki 			acpi_bus_extract_wakeup_device_power_package(handle,
150586e4e20eSRafael J. Wysocki 								     &wakeup);
1506993cbe59SRafael J. Wysocki 			acpi_power_resources_list_free(&wakeup.resources);
1507993cbe59SRafael J. Wysocki 		}
1508778cbc1dSBjorn Helgaas 		return AE_CTRL_DEPTH;
1509b581a7f9SRafael J. Wysocki 	}
15101da177e4SLinus Torvalds 
151182c7d5efSRafael J. Wysocki 	acpi_add_single_object(&device, handle, type, sta, false);
1512e3b87f8aSBjorn Helgaas 	if (!device)
151351a85fafSBjorn Helgaas 		return AE_CTRL_DEPTH;
15141da177e4SLinus Torvalds 
1515209d3b17SRafael J. Wysocki 	device->flags.match_driver = true;
15163fb02738SRajesh Shah 
15174002bf38SRafael J. Wysocki  out:
151851a85fafSBjorn Helgaas 	if (!*return_value)
151951a85fafSBjorn Helgaas 		*return_value = device;
1520805d410fSRafael J. Wysocki 
152151a85fafSBjorn Helgaas 	return AE_OK;
15221da177e4SLinus Torvalds }
15231da177e4SLinus Torvalds 
15240fc300b0SRafael J. Wysocki static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
15250fc300b0SRafael J. Wysocki 					  void *not_used, void **ret_not_used)
152651a85fafSBjorn Helgaas {
1527805d410fSRafael J. Wysocki 	acpi_status status = AE_OK;
1528805d410fSRafael J. Wysocki 	struct acpi_device *device;
1529805d410fSRafael J. Wysocki 	unsigned long long sta_not_used;
1530805d410fSRafael J. Wysocki 	int type_not_used;
15311da177e4SLinus Torvalds 
15327779688fSThomas Renninger 	/*
1533805d410fSRafael J. Wysocki 	 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1534805d410fSRafael J. Wysocki 	 * namespace walks prematurely.
15357779688fSThomas Renninger 	 */
1536805d410fSRafael J. Wysocki 	if (acpi_bus_type_and_status(handle, &type_not_used, &sta_not_used))
1537805d410fSRafael J. Wysocki 		return AE_OK;
15387779688fSThomas Renninger 
1539805d410fSRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
1540805d410fSRafael J. Wysocki 		return AE_CTRL_DEPTH;
1541805d410fSRafael J. Wysocki 
1542805d410fSRafael J. Wysocki 	if (!acpi_match_device_ids(device, acpi_platform_device_ids)) {
1543805d410fSRafael J. Wysocki 		/* This is a known good platform device. */
1544805d410fSRafael J. Wysocki 		acpi_create_platform_device(device);
1545abe99210SMika Westerberg 	} else if (device_attach(&device->dev) < 0) {
1546805d410fSRafael J. Wysocki 		status = AE_CTRL_DEPTH;
1547805d410fSRafael J. Wysocki 	}
1548805d410fSRafael J. Wysocki 	return status;
1549805d410fSRafael J. Wysocki }
1550805d410fSRafael J. Wysocki 
15510cd6ac52SRafael J. Wysocki static int acpi_bus_scan(acpi_handle handle)
15523fb02738SRajesh Shah {
15531da177e4SLinus Torvalds 	void *device = NULL;
15543fb02738SRajesh Shah 
15550cd6ac52SRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
15561da177e4SLinus Torvalds 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1557e3863094SRafael J. Wysocki 				    acpi_bus_check_add, NULL, NULL, &device);
1558c4168bffSLi Shaohua 
1559805d410fSRafael J. Wysocki 	if (!device)
15600cd6ac52SRafael J. Wysocki 		return -ENODEV;
1561805d410fSRafael J. Wysocki 
15620cd6ac52SRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
1563805d410fSRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
15640fc300b0SRafael J. Wysocki 				    acpi_bus_device_attach, NULL, NULL, NULL);
1565805d410fSRafael J. Wysocki 
15660cd6ac52SRafael J. Wysocki 	return 0;
15673fb02738SRajesh Shah }
15683fb02738SRajesh Shah 
1569636458deSRafael J. Wysocki /**
1570636458deSRafael J. Wysocki  * acpi_bus_add - Add ACPI device node objects in a given namespace scope.
1571636458deSRafael J. Wysocki  * @handle: Root of the namespace scope to scan.
15721da177e4SLinus Torvalds  *
1573636458deSRafael J. Wysocki  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1574636458deSRafael J. Wysocki  * found devices.
15753fb02738SRajesh Shah  *
1576636458deSRafael J. Wysocki  * If no devices were found, -ENODEV is returned, but it does not mean that
1577636458deSRafael J. Wysocki  * there has been a real error.  There just have been no suitable ACPI objects
1578636458deSRafael J. Wysocki  * in the table trunk from which the kernel could create a device and add an
1579636458deSRafael J. Wysocki  * appropriate driver.
15803fb02738SRajesh Shah  */
15810cd6ac52SRafael J. Wysocki int acpi_bus_add(acpi_handle handle)
15823fb02738SRajesh Shah {
158302f57c67SRafael J. Wysocki 	int err;
15843fb02738SRajesh Shah 
15850cd6ac52SRafael J. Wysocki 	err = acpi_bus_scan(handle);
158602f57c67SRafael J. Wysocki 	if (err)
158702f57c67SRafael J. Wysocki 		return err;
1588a2100801SRafael J. Wysocki 
15893a37898dSLin Ming 	acpi_update_all_gpes();
15900fc300b0SRafael J. Wysocki 	return 0;
15913fb02738SRajesh Shah }
15923fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_add);
15933fb02738SRajesh Shah 
159405404d8fSRafael J. Wysocki static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
159505404d8fSRafael J. Wysocki 					  void *not_used, void **ret_not_used)
15964be44fcdSLen Brown {
159705404d8fSRafael J. Wysocki 	struct acpi_device *device = NULL;
15983fb02738SRajesh Shah 
159905404d8fSRafael J. Wysocki 	if (!acpi_bus_get_device(handle, &device)) {
160005404d8fSRafael J. Wysocki 		device->removal_type = ACPI_BUS_REMOVAL_EJECT;
160105404d8fSRafael J. Wysocki 		device_release_driver(&device->dev);
16023fb02738SRajesh Shah 	}
160305404d8fSRafael J. Wysocki 	return AE_OK;
160405404d8fSRafael J. Wysocki }
16051da177e4SLinus Torvalds 
160605404d8fSRafael J. Wysocki static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
160705404d8fSRafael J. Wysocki 				   void *not_used, void **ret_not_used)
16081da177e4SLinus Torvalds {
160905404d8fSRafael J. Wysocki 	struct acpi_device *device = NULL;
16101da177e4SLinus Torvalds 
161105404d8fSRafael J. Wysocki 	if (!acpi_bus_get_device(handle, &device))
161205404d8fSRafael J. Wysocki 		acpi_device_unregister(device);
16131da177e4SLinus Torvalds 
161405404d8fSRafael J. Wysocki 	return AE_OK;
161505404d8fSRafael J. Wysocki }
16161da177e4SLinus Torvalds 
1617ae281795SRafael J. Wysocki int acpi_bus_trim(struct acpi_device *start)
16181da177e4SLinus Torvalds {
16191da177e4SLinus Torvalds 	/*
162005404d8fSRafael J. Wysocki 	 * Execute acpi_bus_device_detach() as a post-order callback to detach
162105404d8fSRafael J. Wysocki 	 * all ACPI drivers from the device nodes being removed.
16221da177e4SLinus Torvalds 	 */
162305404d8fSRafael J. Wysocki 	acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
162405404d8fSRafael J. Wysocki 			    acpi_bus_device_detach, NULL, NULL);
162505404d8fSRafael J. Wysocki 	acpi_bus_device_detach(start->handle, 0, NULL, NULL);
16261da177e4SLinus Torvalds 	/*
1627cecdb193SRafael J. Wysocki 	 * Execute acpi_bus_remove() as a post-order callback to remove device
1628cecdb193SRafael J. Wysocki 	 * nodes in the given namespace scope.
16291da177e4SLinus Torvalds 	 */
1630cecdb193SRafael J. Wysocki 	acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1631cecdb193SRafael J. Wysocki 			    acpi_bus_remove, NULL, NULL);
1632cecdb193SRafael J. Wysocki 	acpi_bus_remove(start->handle, 0, NULL, NULL);
1633cecdb193SRafael J. Wysocki 	return 0;
16341da177e4SLinus Torvalds }
1635ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim);
1636ceaba663SKristen Accardi 
1637e8b945c9SBjorn Helgaas static int acpi_bus_scan_fixed(void)
16381da177e4SLinus Torvalds {
16391da177e4SLinus Torvalds 	int result = 0;
16401da177e4SLinus Torvalds 	struct acpi_device *device = NULL;
1641c4168bffSLi Shaohua 
16421da177e4SLinus Torvalds 	/*
16431da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
16441da177e4SLinus Torvalds 	 */
1645cee324b1SAlexey Starikovskiy 	if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
16465c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
1647c4168bffSLi Shaohua 						ACPI_BUS_TYPE_POWER_BUTTON,
1648209d3b17SRafael J. Wysocki 						ACPI_STA_DEFAULT, true);
1649c10d7a13SDaniel Drake 		device_init_wakeup(&device->dev, true);
16503fb02738SRajesh Shah 	}
16511da177e4SLinus Torvalds 
1652cee324b1SAlexey Starikovskiy 	if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
16535c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
1654c4168bffSLi Shaohua 						ACPI_BUS_TYPE_SLEEP_BUTTON,
1655209d3b17SRafael J. Wysocki 						ACPI_STA_DEFAULT, true);
16563fb02738SRajesh Shah 	}
16571da177e4SLinus Torvalds 
1658d550d98dSPatrick Mochel 	return result;
16591da177e4SLinus Torvalds }
16601da177e4SLinus Torvalds 
1661e747f274SBjorn Helgaas int __init acpi_scan_init(void)
16621da177e4SLinus Torvalds {
16631da177e4SLinus Torvalds 	int result;
16641da177e4SLinus Torvalds 
16655b327265SPatrick Mochel 	result = bus_register(&acpi_bus_type);
16665b327265SPatrick Mochel 	if (result) {
16675b327265SPatrick Mochel 		/* We don't want to quit even if we failed to add suspend/resume */
16685b327265SPatrick Mochel 		printk(KERN_ERR PREFIX "Could not register bus type\n");
16695b327265SPatrick Mochel 	}
16705b327265SPatrick Mochel 
167192ef2a25SRafael J. Wysocki 	acpi_pci_root_init();
167297d9a9e9SRafael J. Wysocki 
16731da177e4SLinus Torvalds 	/*
16741da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
16751da177e4SLinus Torvalds 	 */
16760cd6ac52SRafael J. Wysocki 	result = acpi_bus_scan(ACPI_ROOT_OBJECT);
16770cd6ac52SRafael J. Wysocki 	if (result)
16780cd6ac52SRafael J. Wysocki 		return result;
1679c04209a7SAlexey Starikovskiy 
16800cd6ac52SRafael J. Wysocki 	result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
1681c4168bffSLi Shaohua 	if (!result)
1682adc08e20SBjorn Helgaas 		result = acpi_bus_scan_fixed();
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds 	if (result)
1685b17b537aSRafael J. Wysocki 		acpi_device_unregister(acpi_root);
1686a2100801SRafael J. Wysocki 	else
16873a37898dSLin Ming 		acpi_update_all_gpes();
16881da177e4SLinus Torvalds 
1689d550d98dSPatrick Mochel 	return result;
16901da177e4SLinus Torvalds }
1691