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 27
#include <linux/mod_devicetable.h>

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

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

33 34 35 36 37
/* 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 已提交
38
 * Finally, only devices that have a CRS method need to be in this list.
39
 */
40
static struct acpi_device_id excluded_id_list[] __initdata = {
B
Bjorn Helgaas 已提交
41 42 43 44
	{"PNP0C09", 0},		/* EC */
	{"PNP0C0F", 0},		/* Link device */
	{"PNP0000", 0},		/* PIC */
	{"PNP0100", 0},		/* Timer */
45 46 47
	{"", 0},
};

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

/*
 * Compatible Device IDs
 */
#define TEST_HEX(c) \
	if (!(('0' <= (c) && (c) <= '9') || ('A' <= (c) && (c) <= 'F'))) \
		return 0
#define TEST_ALPHA(c) \
60
	if (!('A' <= (c) && (c) <= 'Z')) \
L
Linus Torvalds 已提交
61
		return 0
62
static int __init ispnpidacpi(const char *id)
L
Linus Torvalds 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75
{
	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;
}

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

82
static int pnpacpi_set_resources(struct pnp_dev *dev)
L
Linus Torvalds 已提交
83
{
84 85
	struct acpi_device *acpi_dev;
	acpi_handle handle;
86
	int ret = 0;
L
Linus Torvalds 已提交
87

B
Bjorn Helgaas 已提交
88
	pnp_dbg(&dev->dev, "set resources\n");
89

90
	handle = ACPI_HANDLE(&dev->dev);
91
	if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
92 93 94 95
		dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
		return -ENODEV;
	}

96 97 98
	if (WARN_ON_ONCE(acpi_dev != dev->data))
		dev->data = acpi_dev;

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	if (acpi_has_method(handle, METHOD_NAME__SRS)) {
		struct acpi_buffer buffer;

		ret = pnpacpi_build_resource_template(dev, &buffer);
		if (ret)
			return ret;

		ret = pnpacpi_encode_resources(dev, &buffer);
		if (!ret) {
			acpi_status status;

			status = acpi_set_current_resources(handle, &buffer);
			if (ACPI_FAILURE(status))
				ret = -EIO;
		}
L
Linus Torvalds 已提交
114 115
		kfree(buffer.pointer);
	}
116
	if (!ret && acpi_bus_power_manageable(handle))
117
		ret = acpi_bus_set_power(handle, ACPI_STATE_D0);
118

L
Linus Torvalds 已提交
119 120 121 122 123
	return ret;
}

static int pnpacpi_disable_resources(struct pnp_dev *dev)
{
124 125
	struct acpi_device *acpi_dev;
	acpi_handle handle;
126
	acpi_status status;
127 128

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

130
	handle = ACPI_HANDLE(&dev->dev);
131
	if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
132 133 134 135
		dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
		return 0;
	}

L
Linus Torvalds 已提交
136
	/* acpi_unregister_gsi(pnp_irq(dev, 0)); */
137
	if (acpi_bus_power_manageable(handle))
138
		acpi_bus_set_power(handle, ACPI_STATE_D3_COLD);
139 140 141 142 143 144 145

	/* continue even if acpi_bus_set_power() fails */
	status = acpi_evaluate_object(handle, "_DIS", NULL, NULL);
	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
		return -ENODEV;

	return 0;
L
Linus Torvalds 已提交
146 147
}

L
Len Brown 已提交
148
#ifdef CONFIG_ACPI_SLEEP
149 150
static bool pnpacpi_can_wakeup(struct pnp_dev *dev)
{
151 152 153
	struct acpi_device *acpi_dev;
	acpi_handle handle;

154
	handle = ACPI_HANDLE(&dev->dev);
155
	if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
156 157 158
		dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
		return false;
	}
159 160 161 162

	return acpi_bus_can_wakeup(handle);
}

163 164
static int pnpacpi_suspend(struct pnp_dev *dev, pm_message_t state)
{
165 166 167 168
	struct acpi_device *acpi_dev;
	acpi_handle handle;
	int error = 0;

169
	handle = ACPI_HANDLE(&dev->dev);
170
	if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
171 172 173
		dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
		return 0;
	}
174

175
	if (device_can_wakeup(&dev->dev)) {
176
		error = acpi_pm_device_sleep_wake(&dev->dev,
177
				device_may_wakeup(&dev->dev));
178 179 180 181 182
		if (error)
			return error;
	}

	if (acpi_bus_power_manageable(handle)) {
183
		int power_state = acpi_pm_device_sleep_state(&dev->dev, NULL,
184
							ACPI_STATE_D3_COLD);
185 186
		if (power_state < 0)
			power_state = (state.event == PM_EVENT_ON) ?
187
					ACPI_STATE_D0 : ACPI_STATE_D3_COLD;
188 189 190 191 192 193 194 195

		/*
		 * 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);
196
	}
197 198

	return error;
199 200 201 202
}

static int pnpacpi_resume(struct pnp_dev *dev)
{
203
	struct acpi_device *acpi_dev;
204
	acpi_handle handle = ACPI_HANDLE(&dev->dev);
205 206
	int error = 0;

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

212 213
	if (device_may_wakeup(&dev->dev))
		acpi_pm_device_sleep_wake(&dev->dev, false);
214 215 216 217 218

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

	return error;
219
}
L
Len Brown 已提交
220
#endif
221

222
struct pnp_protocol pnpacpi_protocol = {
B
Bjorn Helgaas 已提交
223 224 225
	.name	 = "Plug and Play ACPI",
	.get	 = pnpacpi_get_resources,
	.set	 = pnpacpi_set_resources,
L
Linus Torvalds 已提交
226
	.disable = pnpacpi_disable_resources,
L
Len Brown 已提交
227
#ifdef CONFIG_ACPI_SLEEP
228
	.can_wakeup = pnpacpi_can_wakeup,
229 230
	.suspend = pnpacpi_suspend,
	.resume = pnpacpi_resume,
L
Len Brown 已提交
231
#endif
L
Linus Torvalds 已提交
232
};
233
EXPORT_SYMBOL(pnpacpi_protocol);
L
Linus Torvalds 已提交
234

235
static char *__init pnpacpi_get_id(struct acpi_device *device)
236 237 238 239 240 241 242 243 244 245 246
{
	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 已提交
247 248 249
static int __init pnpacpi_add_device(struct acpi_device *device)
{
	struct pnp_dev *dev;
250
	char *pnpid;
251
	struct acpi_hardware_id *id;
252
	int error;
L
Linus Torvalds 已提交
253

254 255 256 257
	/* Skip devices that are already bound */
	if (device->physical_node_count)
		return 0;

258 259 260 261
	/*
	 * If a PnPacpi device is not present , the device
	 * driver should not be loaded.
	 */
262
	if (!acpi_has_method(device->handle, "_CRS"))
263 264 265 266 267 268 269
		return 0;

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

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

272
	dev = pnp_alloc_dev(&pnpacpi_protocol, num, pnpid);
B
Bjorn Helgaas 已提交
273
	if (!dev)
L
Linus Torvalds 已提交
274
		return -ENOMEM;
B
Bjorn Helgaas 已提交
275

276
	dev->data = device;
B
Bjorn Helgaas 已提交
277
	/* .enabled means the device can decode the resources */
L
Linus Torvalds 已提交
278
	dev->active = device->status.enabled;
279
	if (acpi_has_method(device->handle, "_SRS"))
L
Linus Torvalds 已提交
280 281
		dev->capabilities |= PNP_CONFIGURABLE;
	dev->capabilities |= PNP_READ;
282
	if (device->flags.dynamic_status && (dev->capabilities & PNP_CONFIGURABLE))
L
Linus Torvalds 已提交
283 284 285
		dev->capabilities |= PNP_WRITE;
	if (device->flags.removable)
		dev->capabilities |= PNP_REMOVABLE;
286
	if (acpi_has_method(device->handle, "_DIS"))
L
Linus Torvalds 已提交
287 288 289 290 291 292 293
		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));

294 295
	if (dev->active)
		pnpacpi_parse_allocated_resource(dev);
L
Linus Torvalds 已提交
296

297 298
	if (dev->capabilities & PNP_CONFIGURABLE)
		pnpacpi_parse_resource_option_data(dev);
299

300
	list_for_each_entry(id, &device->pnp.ids, list) {
301
		if (!strcmp(id->id, pnpid))
302 303 304 305
			continue;
		if (!ispnpidacpi(id->id))
			continue;
		pnp_add_id(dev, id->id);
L
Linus Torvalds 已提交
306 307 308 309
	}

	/* clear out the damaged flags */
	if (!dev->active)
310
		pnp_init_resources(dev);
311 312 313 314 315 316 317

	error = pnp_add_device(dev);
	if (error) {
		put_device(&dev->dev);
		return error;
	}

B
Bjorn Helgaas 已提交
318
	num++;
L
Linus Torvalds 已提交
319

320
	return 0;
L
Linus Torvalds 已提交
321 322 323
}

static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
B
Bjorn Helgaas 已提交
324 325
						     u32 lvl, void *context,
						     void **rv)
L
Linus Torvalds 已提交
326 327 328 329 330 331 332 333 334 335
{
	struct acpi_device *device;

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

336 337
static int __init acpi_pnp_match(struct device *dev, void *_pnp)
{
B
Bjorn Helgaas 已提交
338 339
	struct acpi_device *acpi = to_acpi_device(dev);
	struct pnp_dev *pnp = _pnp;
340 341

	/* true means it matched */
342
	return !acpi->physical_node_count
343
	    && compare_pnp_id(pnp->id, acpi_device_hid(acpi));
344 345
}

346
static struct acpi_device * __init acpi_pnp_find_companion(struct device *dev)
347
{
348 349 350 351
	dev = bus_find_device(&acpi_bus_type, NULL, to_pnp_dev(dev),
			      acpi_pnp_match);
	if (!dev)
		return NULL;
352

353 354
	put_device(dev);
	return to_acpi_device(dev);
355 356 357 358 359
}

/* complete initialization of a PNPACPI device includes having
 * pnpdev->dev.archdata.acpi_handle point to its ACPI sibling.
 */
360 361 362 363 364
static bool acpi_pnp_bus_match(struct device *dev)
{
	return dev->bus == &pnp_bus_type;
}

365
static struct acpi_bus_type __initdata acpi_pnp_bus = {
366 367
	.name	     = "PNP",
	.match	     = acpi_pnp_bus_match,
368
	.find_companion = acpi_pnp_find_companion,
369 370
};

L
Linus Torvalds 已提交
371
int pnpacpi_disabled __initdata;
A
Adrian Bunk 已提交
372
static int __init pnpacpi_init(void)
L
Linus Torvalds 已提交
373 374
{
	if (acpi_disabled || pnpacpi_disabled) {
375
		printk(KERN_INFO "pnp: PnP ACPI: disabled\n");
L
Linus Torvalds 已提交
376 377
		return 0;
	}
378
	printk(KERN_INFO "pnp: PnP ACPI init\n");
L
Linus Torvalds 已提交
379
	pnp_register_protocol(&pnpacpi_protocol);
380
	register_acpi_bus_type(&acpi_pnp_bus);
L
Linus Torvalds 已提交
381
	acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
382
	printk(KERN_INFO "pnp: PnP ACPI: found %d devices\n", num);
383
	unregister_acpi_bus_type(&acpi_pnp_bus);
384
	pnp_platform_devices = 1;
L
Linus Torvalds 已提交
385 386
	return 0;
}
B
Bjorn Helgaas 已提交
387

388
fs_initcall(pnpacpi_init);
L
Linus Torvalds 已提交
389 390 391 392 393 394 395 396 397

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 已提交
398

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