blob: 9113876487edd43037038357726208dd828e9a2a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
David Brownell55955aa2007-05-08 00:28:35 -07006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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 */
David Brownell55955aa2007-05-08 00:28:35 -070021
Paul Gortmaker26e6e9e2011-10-26 18:06:20 -040022#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/acpi.h>
24#include <linux/pnp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Thomas Renninger29b71a12007-07-23 14:43:51 +020026#include <linux/mod_devicetable.h>
Thomas Renninger29b71a12007-07-23 14:43:51 +020027
Bjorn Helgaas1692b27b2008-04-28 16:33:51 -060028#include "../base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "pnpacpi.h"
30
Dmitry Torokhov420a0f62010-09-18 10:11:09 -070031static int num;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
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) \
Alan Coxcdc87c52012-12-07 23:11:14 +010040 if (!('A' <= (c) && (c) <= 'Z')) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 return 0
Thomas Renninger620e1122010-10-01 10:54:00 +020042static int __init ispnpidacpi(const char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
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
Bjorn Helgaas59284cb2008-04-28 16:34:05 -060056static int pnpacpi_get_resources(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Bjorn Helgaas2f534322008-08-19 16:53:47 -060058 pnp_dbg(&dev->dev, "get resources\n");
Bjorn Helgaasd152cf52008-04-28 16:34:39 -060059 return pnpacpi_parse_allocated_resource(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Bjorn Helgaas59284cb2008-04-28 16:34:05 -060062static int pnpacpi_set_resources(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +010064 struct acpi_device *acpi_dev;
65 acpi_handle handle;
Rafael J. Wysockia8d22392014-04-30 22:36:33 +020066 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Bjorn Helgaas2f534322008-08-19 16:53:47 -060068 pnp_dbg(&dev->dev, "set resources\n");
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +010069
Rafael J. Wysockie70dba62014-07-23 01:03:06 +020070 acpi_dev = ACPI_COMPANION(&dev->dev);
71 if (!acpi_dev) {
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +010072 dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
73 return -ENODEV;
74 }
75
Rafael J. Wysockia6b5e882012-11-30 13:05:05 +010076 if (WARN_ON_ONCE(acpi_dev != dev->data))
77 dev->data = acpi_dev;
78
Rafael J. Wysockie70dba62014-07-23 01:03:06 +020079 handle = acpi_dev->handle;
Rafael J. Wysockia8d22392014-04-30 22:36:33 +020080 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
Rafael J. Wysockie70dba62014-07-23 01:03:06 +020097 if (!ret && acpi_device_power_manageable(acpi_dev))
98 ret = acpi_device_set_power(acpi_dev, ACPI_STATE_D0);
Rafael J. Wysockia8d22392014-04-30 22:36:33 +020099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return ret;
101}
102
103static int pnpacpi_disable_resources(struct pnp_dev *dev)
104{
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +0100105 struct acpi_device *acpi_dev;
Rafael J. Wysockia8d22392014-04-30 22:36:33 +0200106 acpi_status status;
Witold Szczeponik6328a572009-03-30 19:31:06 +0200107
108 dev_dbg(&dev->dev, "disable resources\n");
David Brownell55955aa2007-05-08 00:28:35 -0700109
Rafael J. Wysockie70dba62014-07-23 01:03:06 +0200110 acpi_dev = ACPI_COMPANION(&dev->dev);
111 if (!acpi_dev) {
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +0100112 dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
113 return 0;
114 }
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /* acpi_unregister_gsi(pnp_irq(dev, 0)); */
Rafael J. Wysockie70dba62014-07-23 01:03:06 +0200117 if (acpi_device_power_manageable(acpi_dev))
118 acpi_device_set_power(acpi_dev, ACPI_STATE_D3_COLD);
Rafael J. Wysockia8d22392014-04-30 22:36:33 +0200119
Rafael J. Wysockie70dba62014-07-23 01:03:06 +0200120 /* continue even if acpi_device_set_power() fails */
121 status = acpi_evaluate_object(acpi_dev->handle, "_DIS", NULL, NULL);
Rafael J. Wysockia8d22392014-04-30 22:36:33 +0200122 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
123 return -ENODEV;
124
125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Len Brown673d5b42007-07-28 03:33:16 -0400128#ifdef CONFIG_ACPI_SLEEP
Alan Sternb14e0332010-06-29 22:49:24 +0200129static bool pnpacpi_can_wakeup(struct pnp_dev *dev)
130{
Rafael J. Wysockie70dba62014-07-23 01:03:06 +0200131 struct acpi_device *acpi_dev = ACPI_COMPANION(&dev->dev);
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +0100132
Rafael J. Wysockie70dba62014-07-23 01:03:06 +0200133 if (!acpi_dev) {
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +0100134 dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
135 return false;
136 }
Alan Sternb14e0332010-06-29 22:49:24 +0200137
Rafael J. Wysockie70dba62014-07-23 01:03:06 +0200138 return acpi_bus_can_wakeup(acpi_dev->handle);
Alan Sternb14e0332010-06-29 22:49:24 +0200139}
140
Shaohua Lifc30e682007-07-20 10:03:20 +0800141static int pnpacpi_suspend(struct pnp_dev *dev, pm_message_t state)
142{
Rafael J. Wysockie70dba62014-07-23 01:03:06 +0200143 struct acpi_device *acpi_dev = ACPI_COMPANION(&dev->dev);
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +0100144 int error = 0;
145
Rafael J. Wysockie70dba62014-07-23 01:03:06 +0200146 if (!acpi_dev) {
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +0100147 dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
148 return 0;
149 }
Rafael J. Wysocki36e02b62007-10-16 23:31:06 -0700150
Alan Sternb14e0332010-06-29 22:49:24 +0200151 if (device_can_wakeup(&dev->dev)) {
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +0100152 error = acpi_pm_device_sleep_wake(&dev->dev,
Alan Sternb14e0332010-06-29 22:49:24 +0200153 device_may_wakeup(&dev->dev));
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +0100154 if (error)
155 return error;
Alan Sternb14e0332010-06-29 22:49:24 +0200156 }
Rafael J. Wysocki36e02b62007-10-16 23:31:06 -0700157
Rafael J. Wysockie70dba62014-07-23 01:03:06 +0200158 if (acpi_device_power_manageable(acpi_dev)) {
Huang Yingee85f542012-06-23 10:23:48 +0800159 int power_state = acpi_pm_device_sleep_state(&dev->dev, NULL,
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +0200160 ACPI_STATE_D3_COLD);
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +0100161 if (power_state < 0)
162 power_state = (state.event == PM_EVENT_ON) ?
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +0200163 ACPI_STATE_D0 : ACPI_STATE_D3_COLD;
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +0100164
165 /*
Rafael J. Wysockie70dba62014-07-23 01:03:06 +0200166 * acpi_device_set_power() can fail (keyboard port can't be
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +0100167 * 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 */
Rafael J. Wysockie70dba62014-07-23 01:03:06 +0200171 error = acpi_device_set_power(acpi_dev, power_state);
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +0100172 }
173
174 return error;
Shaohua Lifc30e682007-07-20 10:03:20 +0800175}
176
177static int pnpacpi_resume(struct pnp_dev *dev)
178{
Rafael J. Wysockie70dba62014-07-23 01:03:06 +0200179 struct acpi_device *acpi_dev = ACPI_COMPANION(&dev->dev);
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +0100180 int error = 0;
181
Rafael J. Wysockie70dba62014-07-23 01:03:06 +0200182 if (!acpi_dev) {
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +0100183 dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
184 return -ENODEV;
185 }
Bjorn Helgaasc4da6942009-11-17 17:05:14 -0700186
Alan Sternb14e0332010-06-29 22:49:24 +0200187 if (device_may_wakeup(&dev->dev))
188 acpi_pm_device_sleep_wake(&dev->dev, false);
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +0100189
Rafael J. Wysockie70dba62014-07-23 01:03:06 +0200190 if (acpi_device_power_manageable(acpi_dev))
191 error = acpi_device_set_power(acpi_dev, ACPI_STATE_D0);
Rafael J. Wysockicc8e7a32011-01-10 21:23:16 +0100192
193 return error;
Shaohua Lifc30e682007-07-20 10:03:20 +0800194}
Len Brown673d5b42007-07-28 03:33:16 -0400195#endif
Shaohua Lifc30e682007-07-20 10:03:20 +0800196
Bjorn Helgaas9065ce42009-11-17 17:05:19 -0700197struct pnp_protocol pnpacpi_protocol = {
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700198 .name = "Plug and Play ACPI",
199 .get = pnpacpi_get_resources,
200 .set = pnpacpi_set_resources,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 .disable = pnpacpi_disable_resources,
Len Brown673d5b42007-07-28 03:33:16 -0400202#ifdef CONFIG_ACPI_SLEEP
Alan Sternb14e0332010-06-29 22:49:24 +0200203 .can_wakeup = pnpacpi_can_wakeup,
Shaohua Lifc30e682007-07-20 10:03:20 +0800204 .suspend = pnpacpi_suspend,
205 .resume = pnpacpi_resume,
Len Brown673d5b42007-07-28 03:33:16 -0400206#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
Bjorn Helgaas9065ce42009-11-17 17:05:19 -0700208EXPORT_SYMBOL(pnpacpi_protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Rasmus Villemoes844142c2015-09-09 23:59:42 +0200210static const char *__init pnpacpi_get_id(struct acpi_device *device)
Dmitry Torokhov420a0f62010-09-18 10:11:09 -0700211{
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static int __init pnpacpi_add_device(struct acpi_device *device)
223{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 struct pnp_dev *dev;
Rasmus Villemoes844142c2015-09-09 23:59:42 +0200225 const char *pnpid;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600226 struct acpi_hardware_id *id;
Dmitry Torokhov249135d2013-12-15 04:10:11 -0800227 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Adrian Hunter29058752012-11-23 21:07:12 +0100229 /* Skip devices that are already bound */
230 if (device->physical_node_count)
231 return 0;
232
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800233 /*
234 * If a PnPacpi device is not present , the device
235 * driver should not be loaded.
236 */
Zhang Rui0e77e2c2013-09-03 08:32:08 +0800237 if (!acpi_has_method(device->handle, "_CRS"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return 0;
239
Dmitry Torokhov420a0f62010-09-18 10:11:09 -0700240 pnpid = pnpacpi_get_id(device);
241 if (!pnpid)
242 return 0;
243
Zhang Ruieec15ed2014-05-30 04:23:01 +0200244 if (!device->status.present)
Dmitry Torokhov420a0f62010-09-18 10:11:09 -0700245 return 0;
246
247 dev = pnp_alloc_dev(&pnpacpi_protocol, num, pnpid);
Bjorn Helgaasbda1e4e2008-04-28 16:33:54 -0600248 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return -ENOMEM;
Bjorn Helgaasbda1e4e2008-04-28 16:33:54 -0600250
Rafael J. Wysocki2eb1eb02015-03-13 01:45:49 +0100251 ACPI_COMPANION_SET(&dev->dev, device);
Bjorn Helgaasc4da6942009-11-17 17:05:14 -0700252 dev->data = device;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700253 /* .enabled means the device can decode the resources */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 dev->active = device->status.enabled;
Zhang Rui0e77e2c2013-09-03 08:32:08 +0800255 if (acpi_has_method(device->handle, "_SRS"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 dev->capabilities |= PNP_CONFIGURABLE;
257 dev->capabilities |= PNP_READ;
Shaohua Li856608e2008-01-12 19:37:49 -0500258 if (device->flags.dynamic_status && (dev->capabilities & PNP_CONFIGURABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 dev->capabilities |= PNP_WRITE;
260 if (device->flags.removable)
261 dev->capabilities |= PNP_REMOVABLE;
Zhang Rui0e77e2c2013-09-03 08:32:08 +0800262 if (acpi_has_method(device->handle, "_DIS"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 dev->capabilities |= PNP_DISABLE;
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 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
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600270 if (dev->active)
271 pnpacpi_parse_allocated_resource(dev);
David Brownell55955aa2007-05-08 00:28:35 -0700272
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600273 if (dev->capabilities & PNP_CONFIGURABLE)
274 pnpacpi_parse_resource_option_data(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600276 list_for_each_entry(id, &device->pnp.ids, list) {
Dmitry Torokhov420a0f62010-09-18 10:11:09 -0700277 if (!strcmp(id->id, pnpid))
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600278 continue;
279 if (!ispnpidacpi(id->id))
280 continue;
281 pnp_add_id(dev, id->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283
284 /* clear out the damaged flags */
285 if (!dev->active)
Bjorn Helgaasf4490002008-04-28 16:34:09 -0600286 pnp_init_resources(dev);
Dmitry Torokhov249135d2013-12-15 04:10:11 -0800287
288 error = pnp_add_device(dev);
289 if (error) {
290 put_device(&dev->dev);
291 return error;
292 }
293
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700294 num++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Rafael J. Wysocki2eb1eb02015-03-13 01:45:49 +0100296 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700300 u32 lvl, void *context,
301 void **rv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct acpi_device *device;
304
Zhang Ruieec15ed2014-05-30 04:23:01 +0200305 if (acpi_bus_get_device(handle, &device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return AE_CTRL_DEPTH;
Zhang Ruieec15ed2014-05-30 04:23:01 +0200307 if (acpi_is_pnp_device(device))
308 pnpacpi_add_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return AE_OK;
310}
311
312int pnpacpi_disabled __initdata;
Adrian Bunkb449f632005-11-07 01:01:48 -0800313static int __init pnpacpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 if (acpi_disabled || pnpacpi_disabled) {
Bjorn Helgaasc865d2f2008-08-19 16:53:26 -0600316 printk(KERN_INFO "pnp: PnP ACPI: disabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return 0;
318 }
Bjorn Helgaasc865d2f2008-08-19 16:53:26 -0600319 printk(KERN_INFO "pnp: PnP ACPI init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 pnp_register_protocol(&pnpacpi_protocol);
321 acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
Bjorn Helgaasc865d2f2008-08-19 16:53:26 -0600322 printk(KERN_INFO "pnp: PnP ACPI: found %d devices\n", num);
Bjorn Helgaas8f81dd12007-05-08 00:35:54 -0700323 pnp_platform_devices = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return 0;
325}
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700326
Linus Torvaldsed458df2008-10-10 08:00:17 -0700327fs_initcall(pnpacpi_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
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}
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338__setup("pnpacpi=", pnpacpi_setup);