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/config.h>
23 #include <linux/acpi.h>
24 #include <linux/pnp.h>
25 #include <acpi/acpi_bus.h>
30 static char __initdata excluded_id_list[] =
31 "PNP0C0A," /* Battery */
32 "PNP0C0C,PNP0C0E,PNP0C0D," /* Button */
35 "PNP0A03," /* PCI root */
36 "PNP0C0F," /* Link device */
38 "PNP0100," /* Timer */
40 static inline int is_exclusive_device(struct acpi_device *dev)
42 return (!acpi_match_ids(dev, excluded_id_list));
46 * Compatible Device IDs
49 if (!(('0' <= (c) && (c) <= '9') || ('A' <= (c) && (c) <= 'F'))) \
51 #define TEST_ALPHA(c) \
52 if (!('@' <= (c) || (c) <= 'Z')) \
54 static int __init ispnpidacpi(char *id)
68 static void __init pnpidacpi_to_pnpid(char *id, char *str)
73 str[3] = tolower(id[3]);
74 str[4] = tolower(id[4]);
75 str[5] = tolower(id[5]);
76 str[6] = tolower(id[6]);
80 static int pnpacpi_get_resources(struct pnp_dev * dev, struct pnp_resource_table * res)
83 status = pnpacpi_parse_allocated_resource((acpi_handle)dev->data,
85 return ACPI_FAILURE(status) ? -ENODEV : 0;
88 static int pnpacpi_set_resources(struct pnp_dev * dev, struct pnp_resource_table * res)
90 acpi_handle handle = dev->data;
91 struct acpi_buffer buffer;
95 ret = pnpacpi_build_resource_template(handle, &buffer);
98 ret = pnpacpi_encode_resources(res, &buffer);
100 kfree(buffer.pointer);
103 status = acpi_set_current_resources(handle, &buffer);
104 if (ACPI_FAILURE(status))
106 kfree(buffer.pointer);
110 static int pnpacpi_disable_resources(struct pnp_dev *dev)
114 /* acpi_unregister_gsi(pnp_irq(dev, 0)); */
115 status = acpi_evaluate_object((acpi_handle)dev->data,
117 return ACPI_FAILURE(status) ? -ENODEV : 0;
120 struct pnp_protocol pnpacpi_protocol = {
121 .name = "Plug and Play ACPI",
122 .get = pnpacpi_get_resources,
123 .set = pnpacpi_set_resources,
124 .disable = pnpacpi_disable_resources,
127 static int __init pnpacpi_add_device(struct acpi_device *device)
129 acpi_handle temp = NULL;
131 struct pnp_id *dev_id;
134 if (!ispnpidacpi(acpi_device_hid(device)) ||
135 is_exclusive_device(device))
138 pnp_dbg("ACPI device : hid %s", acpi_device_hid(device));
139 dev = kcalloc(1, sizeof(struct pnp_dev), GFP_KERNEL);
141 pnp_err("Out of memory");
144 dev->data = device->handle;
145 /* .enabled means if the device can decode the resources */
146 dev->active = device->status.enabled;
147 status = acpi_get_handle(device->handle, "_SRS", &temp);
148 if (ACPI_SUCCESS(status))
149 dev->capabilities |= PNP_CONFIGURABLE;
150 dev->capabilities |= PNP_READ;
151 if (device->flags.dynamic_status)
152 dev->capabilities |= PNP_WRITE;
153 if (device->flags.removable)
154 dev->capabilities |= PNP_REMOVABLE;
155 status = acpi_get_handle(device->handle, "_DIS", &temp);
156 if (ACPI_SUCCESS(status))
157 dev->capabilities |= PNP_DISABLE;
159 dev->protocol = &pnpacpi_protocol;
161 if (strlen(acpi_device_name(device)))
162 strncpy(dev->name, acpi_device_name(device), sizeof(dev->name));
164 strncpy(dev->name, acpi_device_bid(device), sizeof(dev->name));
168 /* set the initial values for the PnP device */
169 dev_id = kcalloc(1, sizeof(struct pnp_id), GFP_KERNEL);
172 pnpidacpi_to_pnpid(acpi_device_hid(device), dev_id->id);
173 pnp_add_id(dev_id, dev);
176 /* parse allocated resource */
177 status = pnpacpi_parse_allocated_resource(device->handle, &dev->res);
178 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
179 pnp_err("PnPACPI: METHOD_NAME__CRS failure for %s", dev_id->id);
184 if(dev->capabilities & PNP_CONFIGURABLE) {
185 status = pnpacpi_parse_resource_option_data(device->handle,
187 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
188 pnp_err("PnPACPI: METHOD_NAME__PRS failure for %s", dev_id->id);
193 /* parse compatible ids */
194 if (device->flags.compatible_ids) {
195 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
198 for (i = 0; i < cid_list->count; i++) {
199 if (!ispnpidacpi(cid_list->id[i].value))
201 dev_id = kcalloc(1, sizeof(struct pnp_id), GFP_KERNEL);
205 pnpidacpi_to_pnpid(cid_list->id[i].value, dev_id->id);
206 pnp_add_id(dev_id, dev);
210 /* clear out the damaged flags */
212 pnp_init_resource_table(&dev->res);
224 static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
225 u32 lvl, void *context, void **rv)
227 struct acpi_device *device;
229 if (!acpi_bus_get_device(handle, &device))
230 pnpacpi_add_device(device);
232 return AE_CTRL_DEPTH;
236 int pnpacpi_disabled __initdata;
237 int __init pnpacpi_init(void)
239 if (acpi_disabled || pnpacpi_disabled) {
240 pnp_info("PnP ACPI: disabled");
243 pnp_info("PnP ACPI init");
244 pnp_register_protocol(&pnpacpi_protocol);
245 acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
246 pnp_info("PnP ACPI: found %d devices", num);
249 subsys_initcall(pnpacpi_init);
251 static int __init pnpacpi_setup(char *str)
255 if (!strncmp(str, "off", 3))
256 pnpacpi_disabled = 1;
259 __setup("pnpacpi=", pnpacpi_setup);
261 EXPORT_SYMBOL(pnpacpi_protocol);