core.c 9.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 * pnpacpi -- PnP ACPI driver
 *
 * Copyright (c) 2004 Matthieu Castet <castet.matthieu@free.fr>
 * Copyright (c) 2004 Li Shaohua <shaohua.li@intel.com>
6
 *
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
21

22
#include <linux/export.h>
L
Linus Torvalds 已提交
23 24
#include <linux/acpi.h>
#include <linux/pnp.h>
25
#include <linux/slab.h>
26
#include <linux/mod_devicetable.h>
L
Linus Torvalds 已提交
27
#include <acpi/acpi_bus.h>
28

29
#include "../base.h"
L
Linus Torvalds 已提交
30 31
#include "pnpacpi.h"

32
static int num;
L
Linus Torvalds 已提交
33

34 35 36 37 38
/* We need only to blacklist devices that have already an acpi driver that
 * can't use pnp layer. We don't need to blacklist device that are directly
 * used by the kernel (PCI root, ...), as it is harmless and there were
 * already present in pnpbios. But there is an exception for devices that
 * have irqs (PIC, Timer) because we call acpi_register_gsi.
B
Bjorn Helgaas 已提交
39
 * Finally, only devices that have a CRS method need to be in this list.
40
 */
41
static struct acpi_device_id excluded_id_list[] __initdata = {
B
Bjorn Helgaas 已提交
42 43 44 45
	{"PNP0C09", 0},		/* EC */
	{"PNP0C0F", 0},		/* Link device */
	{"PNP0000", 0},		/* PIC */
	{"PNP0100", 0},		/* Timer */
46 47 48
	{"", 0},
};

49
static inline int __init is_exclusive_device(struct acpi_device *dev)
L
Linus Torvalds 已提交
50
{
51
	return (!acpi_match_device_ids(dev, excluded_id_list));
L
Linus Torvalds 已提交
52 53 54 55 56 57 58 59 60 61 62
}

/*
 * Compatible Device IDs
 */
#define TEST_HEX(c) \
	if (!(('0' <= (c) && (c) <= '9') || ('A' <= (c) && (c) <= 'F'))) \
		return 0
#define TEST_ALPHA(c) \
	if (!('@' <= (c) || (c) <= 'Z')) \
		return 0
63
static int __init ispnpidacpi(const char *id)
L
Linus Torvalds 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76
{
	TEST_ALPHA(id[0]);
	TEST_ALPHA(id[1]);
	TEST_ALPHA(id[2]);
	TEST_HEX(id[3]);
	TEST_HEX(id[4]);
	TEST_HEX(id[5]);
	TEST_HEX(id[6]);
	if (id[7] != '\0')
		return 0;
	return 1;
}

77
static int pnpacpi_get_resources(struct pnp_dev *dev)
L
Linus Torvalds 已提交
78
{
B
Bjorn Helgaas 已提交
79
	pnp_dbg(&dev->dev, "get resources\n");
80
	return pnpacpi_parse_allocated_resource(dev);
L
Linus Torvalds 已提交
81 82
}

83
static int pnpacpi_set_resources(struct pnp_dev *dev)
L
Linus Torvalds 已提交
84
{
85 86
	struct acpi_device *acpi_dev;
	acpi_handle handle;
L
Linus Torvalds 已提交
87
	struct acpi_buffer buffer;
88
	int ret;
L
Linus Torvalds 已提交
89

B
Bjorn Helgaas 已提交
90
	pnp_dbg(&dev->dev, "set resources\n");
91 92 93 94 95 96 97

	handle = DEVICE_ACPI_HANDLE(&dev->dev);
	if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &acpi_dev))) {
		dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
		return -ENODEV;
	}

98
	ret = pnpacpi_build_resource_template(dev, &buffer);
L
Linus Torvalds 已提交
99 100
	if (ret)
		return ret;
101
	ret = pnpacpi_encode_resources(dev, &buffer);
L
Linus Torvalds 已提交
102 103 104 105
	if (ret) {
		kfree(buffer.pointer);
		return ret;
	}
106
	if (ACPI_FAILURE(acpi_set_current_resources(handle, &buffer)))
L
Linus Torvalds 已提交
107
		ret = -EINVAL;
108 109
	else if (acpi_bus_power_manageable(handle))
		ret = acpi_bus_set_power(handle, ACPI_STATE_D0);
L
Linus Torvalds 已提交
110 111 112 113 114 115
	kfree(buffer.pointer);
	return ret;
}

static int pnpacpi_disable_resources(struct pnp_dev *dev)
{
116 117
	struct acpi_device *acpi_dev;
	acpi_handle handle;
118 119 120
	int ret;

	dev_dbg(&dev->dev, "disable resources\n");
121

122 123 124 125 126 127
	handle = DEVICE_ACPI_HANDLE(&dev->dev);
	if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &acpi_dev))) {
		dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
		return 0;
	}

L
Linus Torvalds 已提交
128
	/* acpi_unregister_gsi(pnp_irq(dev, 0)); */
129
	ret = 0;
130 131 132
	if (acpi_bus_power_manageable(handle))
		acpi_bus_set_power(handle, ACPI_STATE_D3);
		/* continue even if acpi_bus_set_power() fails */
133 134 135
	if (ACPI_FAILURE(acpi_evaluate_object(handle, "_DIS", NULL, NULL)))
		ret = -ENODEV;
	return ret;
L
Linus Torvalds 已提交
136 137
}

L
Len Brown 已提交
138
#ifdef CONFIG_ACPI_SLEEP
139 140
static bool pnpacpi_can_wakeup(struct pnp_dev *dev)
{
141 142 143 144 145 146 147 148
	struct acpi_device *acpi_dev;
	acpi_handle handle;

	handle = DEVICE_ACPI_HANDLE(&dev->dev);
	if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &acpi_dev))) {
		dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
		return false;
	}
149 150 151 152

	return acpi_bus_can_wakeup(handle);
}

153 154
static int pnpacpi_suspend(struct pnp_dev *dev, pm_message_t state)
{
155 156 157 158 159 160 161 162 163
	struct acpi_device *acpi_dev;
	acpi_handle handle;
	int error = 0;

	handle = DEVICE_ACPI_HANDLE(&dev->dev);
	if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &acpi_dev))) {
		dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
		return 0;
	}
164

165
	if (device_can_wakeup(&dev->dev)) {
166
		error = acpi_pm_device_sleep_wake(&dev->dev,
167
				device_may_wakeup(&dev->dev));
168 169 170 171 172
		if (error)
			return error;
	}

	if (acpi_bus_power_manageable(handle)) {
173 174
		int power_state = acpi_pm_device_sleep_state(&dev->dev, NULL,
							     ACPI_STATE_D3);
175 176 177 178 179 180 181 182 183 184 185
		if (power_state < 0)
			power_state = (state.event == PM_EVENT_ON) ?
					ACPI_STATE_D0 : ACPI_STATE_D3;

		/*
		 * acpi_bus_set_power() often fails (keyboard port can't be
		 * powered-down?), and in any case, our return value is ignored
		 * by pnp_bus_suspend().  Hence we don't revert the wakeup
		 * setting if the set_power fails.
		 */
		error = acpi_bus_set_power(handle, power_state);
186
	}
187 188

	return error;
189 190 191 192
}

static int pnpacpi_resume(struct pnp_dev *dev)
{
193 194 195 196 197 198 199 200
	struct acpi_device *acpi_dev;
	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
	int error = 0;

	if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &acpi_dev))) {
		dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
		return -ENODEV;
	}
201

202 203
	if (device_may_wakeup(&dev->dev))
		acpi_pm_device_sleep_wake(&dev->dev, false);
204 205 206 207 208

	if (acpi_bus_power_manageable(handle))
		error = acpi_bus_set_power(handle, ACPI_STATE_D0);

	return error;
209
}
L
Len Brown 已提交
210
#endif
211

212
struct pnp_protocol pnpacpi_protocol = {
B
Bjorn Helgaas 已提交
213 214 215
	.name	 = "Plug and Play ACPI",
	.get	 = pnpacpi_get_resources,
	.set	 = pnpacpi_set_resources,
L
Linus Torvalds 已提交
216
	.disable = pnpacpi_disable_resources,
L
Len Brown 已提交
217
#ifdef CONFIG_ACPI_SLEEP
218
	.can_wakeup = pnpacpi_can_wakeup,
219 220
	.suspend = pnpacpi_suspend,
	.resume = pnpacpi_resume,
L
Len Brown 已提交
221
#endif
L
Linus Torvalds 已提交
222
};
223
EXPORT_SYMBOL(pnpacpi_protocol);
L
Linus Torvalds 已提交
224

225
static char *__init pnpacpi_get_id(struct acpi_device *device)
226 227 228 229 230 231 232 233 234 235 236
{
	struct acpi_hardware_id *id;

	list_for_each_entry(id, &device->pnp.ids, list) {
		if (ispnpidacpi(id->id))
			return id->id;
	}

	return NULL;
}

L
Linus Torvalds 已提交
237 238 239 240 241
static int __init pnpacpi_add_device(struct acpi_device *device)
{
	acpi_handle temp = NULL;
	acpi_status status;
	struct pnp_dev *dev;
242
	char *pnpid;
243
	struct acpi_hardware_id *id;
L
Linus Torvalds 已提交
244

245 246 247 248
	/*
	 * If a PnPacpi device is not present , the device
	 * driver should not be loaded.
	 */
249
	status = acpi_get_handle(device->handle, "_CRS", &temp);
250 251 252 253 254 255 256 257
	if (ACPI_FAILURE(status))
		return 0;

	pnpid = pnpacpi_get_id(device);
	if (!pnpid)
		return 0;

	if (is_exclusive_device(device) || !device->status.present)
L
Linus Torvalds 已提交
258 259
		return 0;

260
	dev = pnp_alloc_dev(&pnpacpi_protocol, num, pnpid);
B
Bjorn Helgaas 已提交
261
	if (!dev)
L
Linus Torvalds 已提交
262
		return -ENOMEM;
B
Bjorn Helgaas 已提交
263

264
	dev->data = device;
B
Bjorn Helgaas 已提交
265
	/* .enabled means the device can decode the resources */
L
Linus Torvalds 已提交
266 267 268 269 270
	dev->active = device->status.enabled;
	status = acpi_get_handle(device->handle, "_SRS", &temp);
	if (ACPI_SUCCESS(status))
		dev->capabilities |= PNP_CONFIGURABLE;
	dev->capabilities |= PNP_READ;
271
	if (device->flags.dynamic_status && (dev->capabilities & PNP_CONFIGURABLE))
L
Linus Torvalds 已提交
272 273 274 275 276 277 278 279 280 281 282 283
		dev->capabilities |= PNP_WRITE;
	if (device->flags.removable)
		dev->capabilities |= PNP_REMOVABLE;
	status = acpi_get_handle(device->handle, "_DIS", &temp);
	if (ACPI_SUCCESS(status))
		dev->capabilities |= PNP_DISABLE;

	if (strlen(acpi_device_name(device)))
		strncpy(dev->name, acpi_device_name(device), sizeof(dev->name));
	else
		strncpy(dev->name, acpi_device_bid(device), sizeof(dev->name));

284 285
	if (dev->active)
		pnpacpi_parse_allocated_resource(dev);
L
Linus Torvalds 已提交
286

287 288
	if (dev->capabilities & PNP_CONFIGURABLE)
		pnpacpi_parse_resource_option_data(dev);
289

290
	list_for_each_entry(id, &device->pnp.ids, list) {
291
		if (!strcmp(id->id, pnpid))
292 293 294 295
			continue;
		if (!ispnpidacpi(id->id))
			continue;
		pnp_add_id(dev, id->id);
L
Linus Torvalds 已提交
296 297 298 299
	}

	/* clear out the damaged flags */
	if (!dev->active)
300
		pnp_init_resources(dev);
L
Linus Torvalds 已提交
301
	pnp_add_device(dev);
B
Bjorn Helgaas 已提交
302
	num++;
L
Linus Torvalds 已提交
303 304 305 306 307

	return AE_OK;
}

static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
B
Bjorn Helgaas 已提交
308 309
						     u32 lvl, void *context,
						     void **rv)
L
Linus Torvalds 已提交
310 311 312 313 314 315 316 317 318 319
{
	struct acpi_device *device;

	if (!acpi_bus_get_device(handle, &device))
		pnpacpi_add_device(device);
	else
		return AE_CTRL_DEPTH;
	return AE_OK;
}

320 321
static int __init acpi_pnp_match(struct device *dev, void *_pnp)
{
B
Bjorn Helgaas 已提交
322 323
	struct acpi_device *acpi = to_acpi_device(dev);
	struct pnp_dev *pnp = _pnp;
324 325 326 327 328
	struct device *physical_device;

	physical_device = acpi_get_physical_device(acpi->handle);
	if (physical_device)
		put_device(physical_device);
329 330

	/* true means it matched */
331
	return !physical_device
332
	    && compare_pnp_id(pnp->id, acpi_device_hid(acpi));
333 334
}

B
Bjorn Helgaas 已提交
335
static int __init acpi_pnp_find_device(struct device *dev, acpi_handle * handle)
336
{
B
Bjorn Helgaas 已提交
337 338
	struct device *adev;
	struct acpi_device *acpi;
339 340

	adev = bus_find_device(&acpi_bus_type, NULL,
B
Bjorn Helgaas 已提交
341
			       to_pnp_dev(dev), acpi_pnp_match);
342 343 344 345 346 347 348 349 350 351 352 353 354
	if (!adev)
		return -ENODEV;

	acpi = to_acpi_device(adev);
	*handle = acpi->handle;
	put_device(adev);
	return 0;
}

/* complete initialization of a PNPACPI device includes having
 * pnpdev->dev.archdata.acpi_handle point to its ACPI sibling.
 */
static struct acpi_bus_type __initdata acpi_pnp_bus = {
B
Bjorn Helgaas 已提交
355
	.bus	     = &pnp_bus_type,
356 357 358
	.find_device = acpi_pnp_find_device,
};

L
Linus Torvalds 已提交
359
int pnpacpi_disabled __initdata;
A
Adrian Bunk 已提交
360
static int __init pnpacpi_init(void)
L
Linus Torvalds 已提交
361 362
{
	if (acpi_disabled || pnpacpi_disabled) {
363
		printk(KERN_INFO "pnp: PnP ACPI: disabled\n");
L
Linus Torvalds 已提交
364 365
		return 0;
	}
366
	printk(KERN_INFO "pnp: PnP ACPI init\n");
L
Linus Torvalds 已提交
367
	pnp_register_protocol(&pnpacpi_protocol);
368
	register_acpi_bus_type(&acpi_pnp_bus);
L
Linus Torvalds 已提交
369
	acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
370
	printk(KERN_INFO "pnp: PnP ACPI: found %d devices\n", num);
371
	unregister_acpi_bus_type(&acpi_pnp_bus);
372
	pnp_platform_devices = 1;
L
Linus Torvalds 已提交
373 374
	return 0;
}
B
Bjorn Helgaas 已提交
375

376
fs_initcall(pnpacpi_init);
L
Linus Torvalds 已提交
377 378 379 380 381 382 383 384 385

static int __init pnpacpi_setup(char *str)
{
	if (str == NULL)
		return 1;
	if (!strncmp(str, "off", 3))
		pnpacpi_disabled = 1;
	return 1;
}
B
Bjorn Helgaas 已提交
386

L
Linus Torvalds 已提交
387
__setup("pnpacpi=", pnpacpi_setup);