2 * pnpacpi -- PnP ACPI driver
4 * Copyright (c) 2004 Matthieu Castet <castet.matthieu@free.fr>
5 * Copyright (c) 2004 Li Shaohua <shaohua.li@intel.com>
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
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.
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
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>
34 * Compatible Device IDs
37 if (!(('0' <= (c) && (c) <= '9') || ('A' <= (c) && (c) <= 'F'))) \
39 #define TEST_ALPHA(c) \
40 if (!('A' <= (c) && (c) <= 'Z')) \
42 static int __init ispnpidacpi(const char *id)
56 static int pnpacpi_get_resources(struct pnp_dev *dev)
58 pnp_dbg(&dev->dev, "get resources\n");
59 return pnpacpi_parse_allocated_resource(dev);
62 static int pnpacpi_set_resources(struct pnp_dev *dev)
64 struct acpi_device *acpi_dev;
68 pnp_dbg(&dev->dev, "set resources\n");
70 handle = ACPI_HANDLE(&dev->dev);
71 if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
72 dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
76 if (WARN_ON_ONCE(acpi_dev != dev->data))
79 if (acpi_has_method(handle, METHOD_NAME__SRS)) {
80 struct acpi_buffer buffer;
82 ret = pnpacpi_build_resource_template(dev, &buffer);
86 ret = pnpacpi_encode_resources(dev, &buffer);
90 status = acpi_set_current_resources(handle, &buffer);
91 if (ACPI_FAILURE(status))
94 kfree(buffer.pointer);
96 if (!ret && acpi_bus_power_manageable(handle))
97 ret = acpi_bus_set_power(handle, ACPI_STATE_D0);
102 static int pnpacpi_disable_resources(struct pnp_dev *dev)
104 struct acpi_device *acpi_dev;
108 dev_dbg(&dev->dev, "disable resources\n");
110 handle = ACPI_HANDLE(&dev->dev);
111 if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
112 dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
116 /* acpi_unregister_gsi(pnp_irq(dev, 0)); */
117 if (acpi_bus_power_manageable(handle))
118 acpi_bus_set_power(handle, ACPI_STATE_D3_COLD);
120 /* continue even if acpi_bus_set_power() fails */
121 status = acpi_evaluate_object(handle, "_DIS", NULL, NULL);
122 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
128 #ifdef CONFIG_ACPI_SLEEP
129 static bool pnpacpi_can_wakeup(struct pnp_dev *dev)
131 struct acpi_device *acpi_dev;
134 handle = ACPI_HANDLE(&dev->dev);
135 if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
136 dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
140 return acpi_bus_can_wakeup(handle);
143 static int pnpacpi_suspend(struct pnp_dev *dev, pm_message_t state)
145 struct acpi_device *acpi_dev;
149 handle = ACPI_HANDLE(&dev->dev);
150 if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
151 dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
155 if (device_can_wakeup(&dev->dev)) {
156 error = acpi_pm_device_sleep_wake(&dev->dev,
157 device_may_wakeup(&dev->dev));
162 if (acpi_bus_power_manageable(handle)) {
163 int power_state = acpi_pm_device_sleep_state(&dev->dev, NULL,
166 power_state = (state.event == PM_EVENT_ON) ?
167 ACPI_STATE_D0 : ACPI_STATE_D3_COLD;
170 * acpi_bus_set_power() often fails (keyboard port can't be
171 * powered-down?), and in any case, our return value is ignored
172 * by pnp_bus_suspend(). Hence we don't revert the wakeup
173 * setting if the set_power fails.
175 error = acpi_bus_set_power(handle, power_state);
181 static int pnpacpi_resume(struct pnp_dev *dev)
183 struct acpi_device *acpi_dev;
184 acpi_handle handle = ACPI_HANDLE(&dev->dev);
187 if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
188 dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
192 if (device_may_wakeup(&dev->dev))
193 acpi_pm_device_sleep_wake(&dev->dev, false);
195 if (acpi_bus_power_manageable(handle))
196 error = acpi_bus_set_power(handle, ACPI_STATE_D0);
202 struct pnp_protocol pnpacpi_protocol = {
203 .name = "Plug and Play ACPI",
204 .get = pnpacpi_get_resources,
205 .set = pnpacpi_set_resources,
206 .disable = pnpacpi_disable_resources,
207 #ifdef CONFIG_ACPI_SLEEP
208 .can_wakeup = pnpacpi_can_wakeup,
209 .suspend = pnpacpi_suspend,
210 .resume = pnpacpi_resume,
213 EXPORT_SYMBOL(pnpacpi_protocol);
215 static char *__init pnpacpi_get_id(struct acpi_device *device)
217 struct acpi_hardware_id *id;
219 list_for_each_entry(id, &device->pnp.ids, list) {
220 if (ispnpidacpi(id->id))
227 static int __init pnpacpi_add_device(struct acpi_device *device)
231 struct acpi_hardware_id *id;
234 /* Skip devices that are already bound */
235 if (device->physical_node_count)
239 * If a PnPacpi device is not present , the device
240 * driver should not be loaded.
242 if (!acpi_has_method(device->handle, "_CRS"))
245 pnpid = pnpacpi_get_id(device);
249 if (!device->status.present)
252 dev = pnp_alloc_dev(&pnpacpi_protocol, num, pnpid);
257 /* .enabled means the device can decode the resources */
258 dev->active = device->status.enabled;
259 if (acpi_has_method(device->handle, "_SRS"))
260 dev->capabilities |= PNP_CONFIGURABLE;
261 dev->capabilities |= PNP_READ;
262 if (device->flags.dynamic_status && (dev->capabilities & PNP_CONFIGURABLE))
263 dev->capabilities |= PNP_WRITE;
264 if (device->flags.removable)
265 dev->capabilities |= PNP_REMOVABLE;
266 if (acpi_has_method(device->handle, "_DIS"))
267 dev->capabilities |= PNP_DISABLE;
269 if (strlen(acpi_device_name(device)))
270 strncpy(dev->name, acpi_device_name(device), sizeof(dev->name));
272 strncpy(dev->name, acpi_device_bid(device), sizeof(dev->name));
275 pnpacpi_parse_allocated_resource(dev);
277 if (dev->capabilities & PNP_CONFIGURABLE)
278 pnpacpi_parse_resource_option_data(dev);
280 list_for_each_entry(id, &device->pnp.ids, list) {
281 if (!strcmp(id->id, pnpid))
283 if (!ispnpidacpi(id->id))
285 pnp_add_id(dev, id->id);
288 /* clear out the damaged flags */
290 pnp_init_resources(dev);
292 error = pnp_add_device(dev);
294 put_device(&dev->dev);
303 static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
304 u32 lvl, void *context,
307 struct acpi_device *device;
309 if (acpi_bus_get_device(handle, &device))
310 return AE_CTRL_DEPTH;
311 if (acpi_is_pnp_device(device))
312 pnpacpi_add_device(device);
316 static int __init acpi_pnp_match(struct device *dev, void *_pnp)
318 struct acpi_device *acpi = to_acpi_device(dev);
319 struct pnp_dev *pnp = _pnp;
321 /* true means it matched */
322 return !acpi->physical_node_count
323 && compare_pnp_id(pnp->id, acpi_device_hid(acpi));
326 static struct acpi_device * __init acpi_pnp_find_companion(struct device *dev)
328 dev = bus_find_device(&acpi_bus_type, NULL, to_pnp_dev(dev),
334 return to_acpi_device(dev);
337 /* complete initialization of a PNPACPI device includes having
338 * pnpdev->dev.archdata.acpi_handle point to its ACPI sibling.
340 static bool acpi_pnp_bus_match(struct device *dev)
342 return dev->bus == &pnp_bus_type;
345 static struct acpi_bus_type __initdata acpi_pnp_bus = {
347 .match = acpi_pnp_bus_match,
348 .find_companion = acpi_pnp_find_companion,
351 int pnpacpi_disabled __initdata;
352 static int __init pnpacpi_init(void)
354 if (acpi_disabled || pnpacpi_disabled) {
355 printk(KERN_INFO "pnp: PnP ACPI: disabled\n");
358 printk(KERN_INFO "pnp: PnP ACPI init\n");
359 pnp_register_protocol(&pnpacpi_protocol);
360 register_acpi_bus_type(&acpi_pnp_bus);
361 acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
362 printk(KERN_INFO "pnp: PnP ACPI: found %d devices\n", num);
363 unregister_acpi_bus_type(&acpi_pnp_bus);
364 pnp_platform_devices = 1;
368 fs_initcall(pnpacpi_init);
370 static int __init pnpacpi_setup(char *str)
374 if (!strncmp(str, "off", 3))
375 pnpacpi_disabled = 1;
379 __setup("pnpacpi=", pnpacpi_setup);