ACPI, PNP: hook ACPI D-state to PNP suspend/resume
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / pnp / pnpacpi / core.c
blobc37a558ecd9606fb2a67284bd20f7bb4a7577874
1 /*
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
10 * later version.
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/acpi.h>
23 #include <linux/pnp.h>
24 #include <acpi/acpi_bus.h>
25 #include "pnpacpi.h"
27 static int num = 0;
29 /* We need only to blacklist devices that have already an acpi driver that
30 * can't use pnp layer. We don't need to blacklist device that are directly
31 * used by the kernel (PCI root, ...), as it is harmless and there were
32 * already present in pnpbios. But there is an exception for devices that
33 * have irqs (PIC, Timer) because we call acpi_register_gsi.
34 * Finaly only devices that have a CRS method need to be in this list.
36 static char __initdata excluded_id_list[] =
37 "PNP0C09," /* EC */
38 "PNP0C0F," /* Link device */
39 "PNP0000," /* PIC */
40 "PNP0100," /* Timer */
42 static inline int is_exclusive_device(struct acpi_device *dev)
44 return (!acpi_match_ids(dev, excluded_id_list));
48 * Compatible Device IDs
50 #define TEST_HEX(c) \
51 if (!(('0' <= (c) && (c) <= '9') || ('A' <= (c) && (c) <= 'F'))) \
52 return 0
53 #define TEST_ALPHA(c) \
54 if (!('@' <= (c) || (c) <= 'Z')) \
55 return 0
56 static int __init ispnpidacpi(char *id)
58 TEST_ALPHA(id[0]);
59 TEST_ALPHA(id[1]);
60 TEST_ALPHA(id[2]);
61 TEST_HEX(id[3]);
62 TEST_HEX(id[4]);
63 TEST_HEX(id[5]);
64 TEST_HEX(id[6]);
65 if (id[7] != '\0')
66 return 0;
67 return 1;
70 static void __init pnpidacpi_to_pnpid(char *id, char *str)
72 str[0] = id[0];
73 str[1] = id[1];
74 str[2] = id[2];
75 str[3] = tolower(id[3]);
76 str[4] = tolower(id[4]);
77 str[5] = tolower(id[5]);
78 str[6] = tolower(id[6]);
79 str[7] = '\0';
82 static int pnpacpi_get_resources(struct pnp_dev * dev, struct pnp_resource_table * res)
84 acpi_status status;
85 status = pnpacpi_parse_allocated_resource((acpi_handle)dev->data,
86 &dev->res);
87 return ACPI_FAILURE(status) ? -ENODEV : 0;
90 static int pnpacpi_set_resources(struct pnp_dev * dev, struct pnp_resource_table * res)
92 acpi_handle handle = dev->data;
93 struct acpi_buffer buffer;
94 int ret = 0;
95 acpi_status status;
97 ret = pnpacpi_build_resource_template(handle, &buffer);
98 if (ret)
99 return ret;
100 ret = pnpacpi_encode_resources(res, &buffer);
101 if (ret) {
102 kfree(buffer.pointer);
103 return ret;
105 status = acpi_set_current_resources(handle, &buffer);
106 if (ACPI_FAILURE(status))
107 ret = -EINVAL;
108 kfree(buffer.pointer);
109 return ret;
112 static int pnpacpi_disable_resources(struct pnp_dev *dev)
114 acpi_status status;
116 /* acpi_unregister_gsi(pnp_irq(dev, 0)); */
117 status = acpi_evaluate_object((acpi_handle)dev->data,
118 "_DIS", NULL, NULL);
119 return ACPI_FAILURE(status) ? -ENODEV : 0;
122 static int pnpacpi_suspend(struct pnp_dev *dev, pm_message_t state)
124 return acpi_bus_set_power((acpi_handle)dev->data,
125 acpi_pm_device_sleep_state(&dev->dev,
126 device_may_wakeup(&dev->dev), NULL));
129 static int pnpacpi_resume(struct pnp_dev *dev)
131 return acpi_bus_set_power((acpi_handle)dev->data, ACPI_STATE_D0);
134 static struct pnp_protocol pnpacpi_protocol = {
135 .name = "Plug and Play ACPI",
136 .get = pnpacpi_get_resources,
137 .set = pnpacpi_set_resources,
138 .disable = pnpacpi_disable_resources,
139 .suspend = pnpacpi_suspend,
140 .resume = pnpacpi_resume,
143 static int __init pnpacpi_add_device(struct acpi_device *device)
145 acpi_handle temp = NULL;
146 acpi_status status;
147 struct pnp_id *dev_id;
148 struct pnp_dev *dev;
150 status = acpi_get_handle(device->handle, "_CRS", &temp);
151 if (ACPI_FAILURE(status) || !ispnpidacpi(acpi_device_hid(device)) ||
152 is_exclusive_device(device))
153 return 0;
155 pnp_dbg("ACPI device : hid %s", acpi_device_hid(device));
156 dev = kzalloc(sizeof(struct pnp_dev), GFP_KERNEL);
157 if (!dev) {
158 pnp_err("Out of memory");
159 return -ENOMEM;
161 dev->data = device->handle;
162 /* .enabled means if the device can decode the resources */
163 dev->active = device->status.enabled;
164 status = acpi_get_handle(device->handle, "_SRS", &temp);
165 if (ACPI_SUCCESS(status))
166 dev->capabilities |= PNP_CONFIGURABLE;
167 dev->capabilities |= PNP_READ;
168 if (device->flags.dynamic_status)
169 dev->capabilities |= PNP_WRITE;
170 if (device->flags.removable)
171 dev->capabilities |= PNP_REMOVABLE;
172 status = acpi_get_handle(device->handle, "_DIS", &temp);
173 if (ACPI_SUCCESS(status))
174 dev->capabilities |= PNP_DISABLE;
176 dev->protocol = &pnpacpi_protocol;
178 if (strlen(acpi_device_name(device)))
179 strncpy(dev->name, acpi_device_name(device), sizeof(dev->name));
180 else
181 strncpy(dev->name, acpi_device_bid(device), sizeof(dev->name));
183 dev->number = num;
185 /* set the initial values for the PnP device */
186 dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
187 if (!dev_id)
188 goto err;
189 pnpidacpi_to_pnpid(acpi_device_hid(device), dev_id->id);
190 pnp_add_id(dev_id, dev);
192 if(dev->active) {
193 /* parse allocated resource */
194 status = pnpacpi_parse_allocated_resource(device->handle, &dev->res);
195 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
196 pnp_err("PnPACPI: METHOD_NAME__CRS failure for %s", dev_id->id);
197 goto err1;
201 if(dev->capabilities & PNP_CONFIGURABLE) {
202 status = pnpacpi_parse_resource_option_data(device->handle,
203 dev);
204 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
205 pnp_err("PnPACPI: METHOD_NAME__PRS failure for %s", dev_id->id);
206 goto err1;
210 /* parse compatible ids */
211 if (device->flags.compatible_ids) {
212 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
213 int i;
215 for (i = 0; i < cid_list->count; i++) {
216 if (!ispnpidacpi(cid_list->id[i].value))
217 continue;
218 dev_id = kzalloc(sizeof(struct pnp_id), GFP_KERNEL);
219 if (!dev_id)
220 continue;
222 pnpidacpi_to_pnpid(cid_list->id[i].value, dev_id->id);
223 pnp_add_id(dev_id, dev);
227 /* clear out the damaged flags */
228 if (!dev->active)
229 pnp_init_resource_table(&dev->res);
230 pnp_add_device(dev);
231 num ++;
233 return AE_OK;
234 err1:
235 kfree(dev_id);
236 err:
237 kfree(dev);
238 return -EINVAL;
241 static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
242 u32 lvl, void *context, void **rv)
244 struct acpi_device *device;
246 if (!acpi_bus_get_device(handle, &device))
247 pnpacpi_add_device(device);
248 else
249 return AE_CTRL_DEPTH;
250 return AE_OK;
253 static int __init acpi_pnp_match(struct device *dev, void *_pnp)
255 struct acpi_device *acpi = to_acpi_device(dev);
256 struct pnp_dev *pnp = _pnp;
258 /* true means it matched */
259 return acpi->flags.hardware_id
260 && !acpi_get_physical_device(acpi->handle)
261 && compare_pnp_id(pnp->id, acpi->pnp.hardware_id);
264 static int __init acpi_pnp_find_device(struct device *dev, acpi_handle *handle)
266 struct device *adev;
267 struct acpi_device *acpi;
269 adev = bus_find_device(&acpi_bus_type, NULL,
270 to_pnp_dev(dev),
271 acpi_pnp_match);
272 if (!adev)
273 return -ENODEV;
275 acpi = to_acpi_device(adev);
276 *handle = acpi->handle;
277 put_device(adev);
278 return 0;
281 /* complete initialization of a PNPACPI device includes having
282 * pnpdev->dev.archdata.acpi_handle point to its ACPI sibling.
284 static struct acpi_bus_type __initdata acpi_pnp_bus = {
285 .bus = &pnp_bus_type,
286 .find_device = acpi_pnp_find_device,
289 int pnpacpi_disabled __initdata;
290 static int __init pnpacpi_init(void)
292 if (acpi_disabled || pnpacpi_disabled) {
293 pnp_info("PnP ACPI: disabled");
294 return 0;
296 pnp_info("PnP ACPI init");
297 pnp_register_protocol(&pnpacpi_protocol);
298 register_acpi_bus_type(&acpi_pnp_bus);
299 acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
300 pnp_info("PnP ACPI: found %d devices", num);
301 unregister_acpi_bus_type(&acpi_pnp_bus);
302 pnp_platform_devices = 1;
303 return 0;
305 subsys_initcall(pnpacpi_init);
307 static int __init pnpacpi_setup(char *str)
309 if (str == NULL)
310 return 1;
311 if (!strncmp(str, "off", 3))
312 pnpacpi_disabled = 1;
313 return 1;
315 __setup("pnpacpi=", pnpacpi_setup);
317 #if 0
318 EXPORT_SYMBOL(pnpacpi_protocol);
319 #endif