1/*
2 * pnpacpi -- PnP ACPI driver
3 *
4 * Copyright (c) 2004 Matthieu Castet <castet.matthieu@free.fr>
5 * Copyright (c) 2004 Li Shaohua <shaohua.li@intel.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 */
21
22#include <linux/export.h>
23#include <linux/acpi.h>
24#include <linux/pnp.h>
25#include <linux/slab.h>
26#include <linux/mod_devicetable.h>
27
28#include "../base.h"
29#include "pnpacpi.h"
30
31static int num;
32
33/*
34 * Compatible Device IDs
35 */
36#define TEST_HEX(c) \
37	if (!(('0' <= (c) && (c) <= '9') || ('A' <= (c) && (c) <= 'F'))) \
38		return 0
39#define TEST_ALPHA(c) \
40	if (!('A' <= (c) && (c) <= 'Z')) \
41		return 0
42static int __init ispnpidacpi(const char *id)
43{
44	TEST_ALPHA(id[0]);
45	TEST_ALPHA(id[1]);
46	TEST_ALPHA(id[2]);
47	TEST_HEX(id[3]);
48	TEST_HEX(id[4]);
49	TEST_HEX(id[5]);
50	TEST_HEX(id[6]);
51	if (id[7] != '\0')
52		return 0;
53	return 1;
54}
55
56static int pnpacpi_get_resources(struct pnp_dev *dev)
57{
58	pnp_dbg(&dev->dev, "get resources\n");
59	return pnpacpi_parse_allocated_resource(dev);
60}
61
62static int pnpacpi_set_resources(struct pnp_dev *dev)
63{
64	struct acpi_device *acpi_dev;
65	acpi_handle handle;
66	int ret = 0;
67
68	pnp_dbg(&dev->dev, "set resources\n");
69
70	acpi_dev = ACPI_COMPANION(&dev->dev);
71	if (!acpi_dev) {
72		dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
73		return -ENODEV;
74	}
75
76	if (WARN_ON_ONCE(acpi_dev != dev->data))
77		dev->data = acpi_dev;
78
79	handle = acpi_dev->handle;
80	if (acpi_has_method(handle, METHOD_NAME__SRS)) {
81		struct acpi_buffer buffer;
82
83		ret = pnpacpi_build_resource_template(dev, &buffer);
84		if (ret)
85			return ret;
86
87		ret = pnpacpi_encode_resources(dev, &buffer);
88		if (!ret) {
89			acpi_status status;
90
91			status = acpi_set_current_resources(handle, &buffer);
92			if (ACPI_FAILURE(status))
93				ret = -EIO;
94		}
95		kfree(buffer.pointer);
96	}
97	if (!ret && acpi_device_power_manageable(acpi_dev))
98		ret = acpi_device_set_power(acpi_dev, ACPI_STATE_D0);
99
100	return ret;
101}
102
103static int pnpacpi_disable_resources(struct pnp_dev *dev)
104{
105	struct acpi_device *acpi_dev;
106	acpi_status status;
107
108	dev_dbg(&dev->dev, "disable resources\n");
109
110	acpi_dev = ACPI_COMPANION(&dev->dev);
111	if (!acpi_dev) {
112		dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
113		return 0;
114	}
115
116	/* acpi_unregister_gsi(pnp_irq(dev, 0)); */
117	if (acpi_device_power_manageable(acpi_dev))
118		acpi_device_set_power(acpi_dev, ACPI_STATE_D3_COLD);
119
120	/* continue even if acpi_device_set_power() fails */
121	status = acpi_evaluate_object(acpi_dev->handle, "_DIS", NULL, NULL);
122	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
123		return -ENODEV;
124
125	return 0;
126}
127
128#ifdef CONFIG_ACPI_SLEEP
129static bool pnpacpi_can_wakeup(struct pnp_dev *dev)
130{
131	struct acpi_device *acpi_dev = ACPI_COMPANION(&dev->dev);
132
133	if (!acpi_dev) {
134		dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
135		return false;
136	}
137
138	return acpi_bus_can_wakeup(acpi_dev->handle);
139}
140
141static int pnpacpi_suspend(struct pnp_dev *dev, pm_message_t state)
142{
143	struct acpi_device *acpi_dev = ACPI_COMPANION(&dev->dev);
144	int error = 0;
145
146	if (!acpi_dev) {
147		dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
148		return 0;
149	}
150
151	if (device_can_wakeup(&dev->dev)) {
152		error = acpi_pm_device_sleep_wake(&dev->dev,
153				device_may_wakeup(&dev->dev));
154		if (error)
155			return error;
156	}
157
158	if (acpi_device_power_manageable(acpi_dev)) {
159		int power_state = acpi_pm_device_sleep_state(&dev->dev, NULL,
160							ACPI_STATE_D3_COLD);
161		if (power_state < 0)
162			power_state = (state.event == PM_EVENT_ON) ?
163					ACPI_STATE_D0 : ACPI_STATE_D3_COLD;
164
165		/*
166		 * acpi_device_set_power() can fail (keyboard port can't be
167		 * powered-down?), and in any case, our return value is ignored
168		 * by pnp_bus_suspend().  Hence we don't revert the wakeup
169		 * setting if the set_power fails.
170		 */
171		error = acpi_device_set_power(acpi_dev, power_state);
172	}
173
174	return error;
175}
176
177static int pnpacpi_resume(struct pnp_dev *dev)
178{
179	struct acpi_device *acpi_dev = ACPI_COMPANION(&dev->dev);
180	int error = 0;
181
182	if (!acpi_dev) {
183		dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
184		return -ENODEV;
185	}
186
187	if (device_may_wakeup(&dev->dev))
188		acpi_pm_device_sleep_wake(&dev->dev, false);
189
190	if (acpi_device_power_manageable(acpi_dev))
191		error = acpi_device_set_power(acpi_dev, ACPI_STATE_D0);
192
193	return error;
194}
195#endif
196
197struct pnp_protocol pnpacpi_protocol = {
198	.name	 = "Plug and Play ACPI",
199	.get	 = pnpacpi_get_resources,
200	.set	 = pnpacpi_set_resources,
201	.disable = pnpacpi_disable_resources,
202#ifdef CONFIG_ACPI_SLEEP
203	.can_wakeup = pnpacpi_can_wakeup,
204	.suspend = pnpacpi_suspend,
205	.resume = pnpacpi_resume,
206#endif
207};
208EXPORT_SYMBOL(pnpacpi_protocol);
209
210static char *__init pnpacpi_get_id(struct acpi_device *device)
211{
212	struct acpi_hardware_id *id;
213
214	list_for_each_entry(id, &device->pnp.ids, list) {
215		if (ispnpidacpi(id->id))
216			return id->id;
217	}
218
219	return NULL;
220}
221
222static int __init pnpacpi_add_device(struct acpi_device *device)
223{
224	struct pnp_dev *dev;
225	char *pnpid;
226	struct acpi_hardware_id *id;
227	int error;
228
229	/* Skip devices that are already bound */
230	if (device->physical_node_count)
231		return 0;
232
233	/*
234	 * If a PnPacpi device is not present , the device
235	 * driver should not be loaded.
236	 */
237	if (!acpi_has_method(device->handle, "_CRS"))
238		return 0;
239
240	pnpid = pnpacpi_get_id(device);
241	if (!pnpid)
242		return 0;
243
244	if (!device->status.present)
245		return 0;
246
247	dev = pnp_alloc_dev(&pnpacpi_protocol, num, pnpid);
248	if (!dev)
249		return -ENOMEM;
250
251	ACPI_COMPANION_SET(&dev->dev, device);
252	dev->data = device;
253	/* .enabled means the device can decode the resources */
254	dev->active = device->status.enabled;
255	if (acpi_has_method(device->handle, "_SRS"))
256		dev->capabilities |= PNP_CONFIGURABLE;
257	dev->capabilities |= PNP_READ;
258	if (device->flags.dynamic_status && (dev->capabilities & PNP_CONFIGURABLE))
259		dev->capabilities |= PNP_WRITE;
260	if (device->flags.removable)
261		dev->capabilities |= PNP_REMOVABLE;
262	if (acpi_has_method(device->handle, "_DIS"))
263		dev->capabilities |= PNP_DISABLE;
264
265	if (strlen(acpi_device_name(device)))
266		strncpy(dev->name, acpi_device_name(device), sizeof(dev->name));
267	else
268		strncpy(dev->name, acpi_device_bid(device), sizeof(dev->name));
269
270	if (dev->active)
271		pnpacpi_parse_allocated_resource(dev);
272
273	if (dev->capabilities & PNP_CONFIGURABLE)
274		pnpacpi_parse_resource_option_data(dev);
275
276	list_for_each_entry(id, &device->pnp.ids, list) {
277		if (!strcmp(id->id, pnpid))
278			continue;
279		if (!ispnpidacpi(id->id))
280			continue;
281		pnp_add_id(dev, id->id);
282	}
283
284	/* clear out the damaged flags */
285	if (!dev->active)
286		pnp_init_resources(dev);
287
288	error = pnp_add_device(dev);
289	if (error) {
290		put_device(&dev->dev);
291		return error;
292	}
293
294	num++;
295
296	return 0;
297}
298
299static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
300						     u32 lvl, void *context,
301						     void **rv)
302{
303	struct acpi_device *device;
304
305	if (acpi_bus_get_device(handle, &device))
306		return AE_CTRL_DEPTH;
307	if (acpi_is_pnp_device(device))
308		pnpacpi_add_device(device);
309	return AE_OK;
310}
311
312int pnpacpi_disabled __initdata;
313static int __init pnpacpi_init(void)
314{
315	if (acpi_disabled || pnpacpi_disabled) {
316		printk(KERN_INFO "pnp: PnP ACPI: disabled\n");
317		return 0;
318	}
319	printk(KERN_INFO "pnp: PnP ACPI init\n");
320	pnp_register_protocol(&pnpacpi_protocol);
321	acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
322	printk(KERN_INFO "pnp: PnP ACPI: found %d devices\n", num);
323	pnp_platform_devices = 1;
324	return 0;
325}
326
327fs_initcall(pnpacpi_init);
328
329static int __init pnpacpi_setup(char *str)
330{
331	if (str == NULL)
332		return 1;
333	if (!strncmp(str, "off", 3))
334		pnpacpi_disabled = 1;
335	return 1;
336}
337
338__setup("pnpacpi=", pnpacpi_setup);
339