]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/pnp/pnpacpi/core.c
ACPI: Clean up inclusions of ACPI header files
[mirror_ubuntu-zesty-kernel.git] / drivers / pnp / pnpacpi / core.c
CommitLineData
1da177e4
LT
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>
55955aad 6 *
1da177e4
LT
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 */
55955aad 21
26e6e9e5 22#include <linux/export.h>
1da177e4
LT
23#include <linux/acpi.h>
24#include <linux/pnp.h>
5a0e3ad6 25#include <linux/slab.h>
29b71a1c 26#include <linux/mod_devicetable.h>
29b71a1c 27
1692b27b 28#include "../base.h"
1da177e4
LT
29#include "pnpacpi.h"
30
420a0f66 31static int num;
1da177e4 32
c4bb6f5a
MC
33/* We need only to blacklist devices that have already an acpi driver that
34 * can't use pnp layer. We don't need to blacklist device that are directly
35 * used by the kernel (PCI root, ...), as it is harmless and there were
36 * already present in pnpbios. But there is an exception for devices that
37 * have irqs (PIC, Timer) because we call acpi_register_gsi.
07d4e9af 38 * Finally, only devices that have a CRS method need to be in this list.
c4bb6f5a 39 */
6bf69b5e 40static struct acpi_device_id excluded_id_list[] __initdata = {
9dd78466
BH
41 {"PNP0C09", 0}, /* EC */
42 {"PNP0C0F", 0}, /* Link device */
43 {"PNP0000", 0}, /* PIC */
44 {"PNP0100", 0}, /* Timer */
29b71a1c
TR
45 {"", 0},
46};
47
9448b0d4 48static inline int __init is_exclusive_device(struct acpi_device *dev)
1da177e4 49{
29b71a1c 50 return (!acpi_match_device_ids(dev, excluded_id_list));
1da177e4
LT
51}
52
1da177e4
LT
53/*
54 * Compatible Device IDs
55 */
56#define TEST_HEX(c) \
57 if (!(('0' <= (c) && (c) <= '9') || ('A' <= (c) && (c) <= 'F'))) \
58 return 0
59#define TEST_ALPHA(c) \
cdc87c5a 60 if (!('A' <= (c) && (c) <= 'Z')) \
1da177e4 61 return 0
620e112c 62static int __init ispnpidacpi(const char *id)
1da177e4
LT
63{
64 TEST_ALPHA(id[0]);
65 TEST_ALPHA(id[1]);
66 TEST_ALPHA(id[2]);
67 TEST_HEX(id[3]);
68 TEST_HEX(id[4]);
69 TEST_HEX(id[5]);
70 TEST_HEX(id[6]);
71 if (id[7] != '\0')
72 return 0;
73 return 1;
74}
75
59284cb4 76static int pnpacpi_get_resources(struct pnp_dev *dev)
1da177e4 77{
2f53432c 78 pnp_dbg(&dev->dev, "get resources\n");
d152cf5d 79 return pnpacpi_parse_allocated_resource(dev);
1da177e4
LT
80}
81
59284cb4 82static int pnpacpi_set_resources(struct pnp_dev *dev)
1da177e4 83{
cc8e7a35
RW
84 struct acpi_device *acpi_dev;
85 acpi_handle handle;
1da177e4 86 struct acpi_buffer buffer;
cdef6254 87 int ret;
1da177e4 88
2f53432c 89 pnp_dbg(&dev->dev, "set resources\n");
cc8e7a35 90
3a83f992 91 handle = ACPI_HANDLE(&dev->dev);
cfc57557 92 if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
cc8e7a35
RW
93 dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
94 return -ENODEV;
95 }
96
a6b5e88c
RW
97 if (WARN_ON_ONCE(acpi_dev != dev->data))
98 dev->data = acpi_dev;
99
cdef6254 100 ret = pnpacpi_build_resource_template(dev, &buffer);
1da177e4
LT
101 if (ret)
102 return ret;
4ab55d8d 103 ret = pnpacpi_encode_resources(dev, &buffer);
1da177e4
LT
104 if (ret) {
105 kfree(buffer.pointer);
106 return ret;
107 }
6328a574 108 if (ACPI_FAILURE(acpi_set_current_resources(handle, &buffer)))
1da177e4 109 ret = -EINVAL;
6328a574
WS
110 else if (acpi_bus_power_manageable(handle))
111 ret = acpi_bus_set_power(handle, ACPI_STATE_D0);
1da177e4
LT
112 kfree(buffer.pointer);
113 return ret;
114}
115
116static int pnpacpi_disable_resources(struct pnp_dev *dev)
117{
cc8e7a35
RW
118 struct acpi_device *acpi_dev;
119 acpi_handle handle;
6328a574
WS
120 int ret;
121
122 dev_dbg(&dev->dev, "disable resources\n");
55955aad 123
3a83f992 124 handle = ACPI_HANDLE(&dev->dev);
cfc57557 125 if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
cc8e7a35
RW
126 dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
127 return 0;
128 }
129
1da177e4 130 /* acpi_unregister_gsi(pnp_irq(dev, 0)); */
6328a574 131 ret = 0;
19bde778 132 if (acpi_bus_power_manageable(handle))
8ad928d5 133 acpi_bus_set_power(handle, ACPI_STATE_D3_COLD);
19bde778 134 /* continue even if acpi_bus_set_power() fails */
6328a574
WS
135 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_DIS", NULL, NULL)))
136 ret = -ENODEV;
137 return ret;
1da177e4
LT
138}
139
673d5b43 140#ifdef CONFIG_ACPI_SLEEP
b14e033e
AS
141static bool pnpacpi_can_wakeup(struct pnp_dev *dev)
142{
cc8e7a35
RW
143 struct acpi_device *acpi_dev;
144 acpi_handle handle;
145
3a83f992 146 handle = ACPI_HANDLE(&dev->dev);
cfc57557 147 if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
cc8e7a35
RW
148 dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
149 return false;
150 }
b14e033e
AS
151
152 return acpi_bus_can_wakeup(handle);
153}
154
fc30e68e
SL
155static int pnpacpi_suspend(struct pnp_dev *dev, pm_message_t state)
156{
cc8e7a35
RW
157 struct acpi_device *acpi_dev;
158 acpi_handle handle;
159 int error = 0;
160
3a83f992 161 handle = ACPI_HANDLE(&dev->dev);
cfc57557 162 if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
cc8e7a35
RW
163 dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
164 return 0;
165 }
36e02b62 166
b14e033e 167 if (device_can_wakeup(&dev->dev)) {
cc8e7a35 168 error = acpi_pm_device_sleep_wake(&dev->dev,
b14e033e 169 device_may_wakeup(&dev->dev));
cc8e7a35
RW
170 if (error)
171 return error;
172 }
173
174 if (acpi_bus_power_manageable(handle)) {
ee85f543 175 int power_state = acpi_pm_device_sleep_state(&dev->dev, NULL,
8ad928d5 176 ACPI_STATE_D3_COLD);
cc8e7a35
RW
177 if (power_state < 0)
178 power_state = (state.event == PM_EVENT_ON) ?
8ad928d5 179 ACPI_STATE_D0 : ACPI_STATE_D3_COLD;
cc8e7a35
RW
180
181 /*
182 * acpi_bus_set_power() often fails (keyboard port can't be
183 * powered-down?), and in any case, our return value is ignored
184 * by pnp_bus_suspend(). Hence we don't revert the wakeup
185 * setting if the set_power fails.
186 */
187 error = acpi_bus_set_power(handle, power_state);
b14e033e 188 }
cc8e7a35
RW
189
190 return error;
fc30e68e
SL
191}
192
193static int pnpacpi_resume(struct pnp_dev *dev)
194{
cc8e7a35 195 struct acpi_device *acpi_dev;
3a83f992 196 acpi_handle handle = ACPI_HANDLE(&dev->dev);
cc8e7a35
RW
197 int error = 0;
198
cfc57557 199 if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
cc8e7a35
RW
200 dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
201 return -ENODEV;
202 }
c4da6940 203
b14e033e
AS
204 if (device_may_wakeup(&dev->dev))
205 acpi_pm_device_sleep_wake(&dev->dev, false);
cc8e7a35
RW
206
207 if (acpi_bus_power_manageable(handle))
208 error = acpi_bus_set_power(handle, ACPI_STATE_D0);
209
210 return error;
fc30e68e 211}
673d5b43 212#endif
fc30e68e 213
9065ce45 214struct pnp_protocol pnpacpi_protocol = {
07d4e9af
BH
215 .name = "Plug and Play ACPI",
216 .get = pnpacpi_get_resources,
217 .set = pnpacpi_set_resources,
1da177e4 218 .disable = pnpacpi_disable_resources,
673d5b43 219#ifdef CONFIG_ACPI_SLEEP
b14e033e 220 .can_wakeup = pnpacpi_can_wakeup,
fc30e68e
SL
221 .suspend = pnpacpi_suspend,
222 .resume = pnpacpi_resume,
673d5b43 223#endif
1da177e4 224};
9065ce45 225EXPORT_SYMBOL(pnpacpi_protocol);
1da177e4 226
66c3ec4f 227static char *__init pnpacpi_get_id(struct acpi_device *device)
420a0f66
DT
228{
229 struct acpi_hardware_id *id;
230
231 list_for_each_entry(id, &device->pnp.ids, list) {
232 if (ispnpidacpi(id->id))
233 return id->id;
234 }
235
236 return NULL;
237}
238
1da177e4
LT
239static int __init pnpacpi_add_device(struct acpi_device *device)
240{
1da177e4 241 struct pnp_dev *dev;
420a0f66 242 char *pnpid;
7f47fa6c 243 struct acpi_hardware_id *id;
1da177e4 244
29058753
AH
245 /* Skip devices that are already bound */
246 if (device->physical_node_count)
247 return 0;
248
39a0ad87
ZY
249 /*
250 * If a PnPacpi device is not present , the device
251 * driver should not be loaded.
252 */
0e77e2c4 253 if (!acpi_has_method(device->handle, "_CRS"))
420a0f66
DT
254 return 0;
255
256 pnpid = pnpacpi_get_id(device);
257 if (!pnpid)
258 return 0;
259
260 if (is_exclusive_device(device) || !device->status.present)
1da177e4
LT
261 return 0;
262
420a0f66 263 dev = pnp_alloc_dev(&pnpacpi_protocol, num, pnpid);
bda1e4e5 264 if (!dev)
1da177e4 265 return -ENOMEM;
bda1e4e5 266
c4da6940 267 dev->data = device;
07d4e9af 268 /* .enabled means the device can decode the resources */
1da177e4 269 dev->active = device->status.enabled;
0e77e2c4 270 if (acpi_has_method(device->handle, "_SRS"))
1da177e4
LT
271 dev->capabilities |= PNP_CONFIGURABLE;
272 dev->capabilities |= PNP_READ;
856608ee 273 if (device->flags.dynamic_status && (dev->capabilities & PNP_CONFIGURABLE))
1da177e4
LT
274 dev->capabilities |= PNP_WRITE;
275 if (device->flags.removable)
276 dev->capabilities |= PNP_REMOVABLE;
0e77e2c4 277 if (acpi_has_method(device->handle, "_DIS"))
1da177e4
LT
278 dev->capabilities |= PNP_DISABLE;
279
1da177e4
LT
280 if (strlen(acpi_device_name(device)))
281 strncpy(dev->name, acpi_device_name(device), sizeof(dev->name));
282 else
283 strncpy(dev->name, acpi_device_bid(device), sizeof(dev->name));
284
d152cf5d
BH
285 if (dev->active)
286 pnpacpi_parse_allocated_resource(dev);
1da177e4 287
d152cf5d
BH
288 if (dev->capabilities & PNP_CONFIGURABLE)
289 pnpacpi_parse_resource_option_data(dev);
55955aad 290
7f47fa6c 291 list_for_each_entry(id, &device->pnp.ids, list) {
420a0f66 292 if (!strcmp(id->id, pnpid))
7f47fa6c
BH
293 continue;
294 if (!ispnpidacpi(id->id))
295 continue;
296 pnp_add_id(dev, id->id);
1da177e4
LT
297 }
298
299 /* clear out the damaged flags */
300 if (!dev->active)
f4490002 301 pnp_init_resources(dev);
1da177e4 302 pnp_add_device(dev);
9dd78466 303 num++;
1da177e4
LT
304
305 return AE_OK;
1da177e4
LT
306}
307
308static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
9dd78466
BH
309 u32 lvl, void *context,
310 void **rv)
1da177e4
LT
311{
312 struct acpi_device *device;
313
314 if (!acpi_bus_get_device(handle, &device))
315 pnpacpi_add_device(device);
316 else
317 return AE_CTRL_DEPTH;
318 return AE_OK;
319}
320
55955aad
DB
321static int __init acpi_pnp_match(struct device *dev, void *_pnp)
322{
9dd78466
BH
323 struct acpi_device *acpi = to_acpi_device(dev);
324 struct pnp_dev *pnp = _pnp;
55955aad
DB
325
326 /* true means it matched */
1033f904 327 return !acpi->physical_node_count
ea8d82fd 328 && compare_pnp_id(pnp->id, acpi_device_hid(acpi));
55955aad
DB
329}
330
9dd78466 331static int __init acpi_pnp_find_device(struct device *dev, acpi_handle * handle)
55955aad 332{
9dd78466
BH
333 struct device *adev;
334 struct acpi_device *acpi;
55955aad
DB
335
336 adev = bus_find_device(&acpi_bus_type, NULL,
9dd78466 337 to_pnp_dev(dev), acpi_pnp_match);
55955aad
DB
338 if (!adev)
339 return -ENODEV;
340
341 acpi = to_acpi_device(adev);
342 *handle = acpi->handle;
343 put_device(adev);
344 return 0;
345}
346
347/* complete initialization of a PNPACPI device includes having
348 * pnpdev->dev.archdata.acpi_handle point to its ACPI sibling.
349 */
53540098
RW
350static bool acpi_pnp_bus_match(struct device *dev)
351{
352 return dev->bus == &pnp_bus_type;
353}
354
55955aad 355static struct acpi_bus_type __initdata acpi_pnp_bus = {
53540098
RW
356 .name = "PNP",
357 .match = acpi_pnp_bus_match,
55955aad
DB
358 .find_device = acpi_pnp_find_device,
359};
360
1da177e4 361int pnpacpi_disabled __initdata;
b449f63c 362static int __init pnpacpi_init(void)
1da177e4
LT
363{
364 if (acpi_disabled || pnpacpi_disabled) {
c865d2f6 365 printk(KERN_INFO "pnp: PnP ACPI: disabled\n");
1da177e4
LT
366 return 0;
367 }
c865d2f6 368 printk(KERN_INFO "pnp: PnP ACPI init\n");
1da177e4 369 pnp_register_protocol(&pnpacpi_protocol);
55955aad 370 register_acpi_bus_type(&acpi_pnp_bus);
1da177e4 371 acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
c865d2f6 372 printk(KERN_INFO "pnp: PnP ACPI: found %d devices\n", num);
55955aad 373 unregister_acpi_bus_type(&acpi_pnp_bus);
8f81dd14 374 pnp_platform_devices = 1;
1da177e4
LT
375 return 0;
376}
9dd78466 377
ed458df4 378fs_initcall(pnpacpi_init);
1da177e4
LT
379
380static int __init pnpacpi_setup(char *str)
381{
382 if (str == NULL)
383 return 1;
384 if (!strncmp(str, "off", 3))
385 pnpacpi_disabled = 1;
386 return 1;
387}
9dd78466 388
1da177e4 389__setup("pnpacpi=", pnpacpi_setup);