blob: e77d1feb759efbf95d57aa3b809c84b9da605f97 [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>
6 *
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 */
21
Bjorn Helgaase139aa52005-09-06 15:17:05 -070022#include <linux/config.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/acpi.h>
24#include <linux/pnp.h>
25#include <acpi/acpi_bus.h>
26#include "pnpacpi.h"
27
28static int num = 0;
29
30static char __initdata excluded_id_list[] =
31 "PNP0C0A," /* Battery */
32 "PNP0C0C,PNP0C0E,PNP0C0D," /* Button */
33 "PNP0C09," /* EC */
34 "PNP0C0B," /* Fan */
35 "PNP0A03," /* PCI root */
36 "PNP0C0F," /* Link device */
37 "PNP0000," /* PIC */
38 "PNP0100," /* Timer */
39 ;
40static inline int is_exclusive_device(struct acpi_device *dev)
41{
42 return (!acpi_match_ids(dev, excluded_id_list));
43}
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
46 * Compatible Device IDs
47 */
48#define TEST_HEX(c) \
49 if (!(('0' <= (c) && (c) <= '9') || ('A' <= (c) && (c) <= 'F'))) \
50 return 0
51#define TEST_ALPHA(c) \
52 if (!('@' <= (c) || (c) <= 'Z')) \
53 return 0
54static int __init ispnpidacpi(char *id)
55{
56 TEST_ALPHA(id[0]);
57 TEST_ALPHA(id[1]);
58 TEST_ALPHA(id[2]);
59 TEST_HEX(id[3]);
60 TEST_HEX(id[4]);
61 TEST_HEX(id[5]);
62 TEST_HEX(id[6]);
63 if (id[7] != '\0')
64 return 0;
65 return 1;
66}
67
68static void __init pnpidacpi_to_pnpid(char *id, char *str)
69{
70 str[0] = id[0];
71 str[1] = id[1];
72 str[2] = id[2];
73 str[3] = tolower(id[3]);
74 str[4] = tolower(id[4]);
75 str[5] = tolower(id[5]);
76 str[6] = tolower(id[6]);
77 str[7] = '\0';
78}
79
80static int pnpacpi_get_resources(struct pnp_dev * dev, struct pnp_resource_table * res)
81{
82 acpi_status status;
83 status = pnpacpi_parse_allocated_resource((acpi_handle)dev->data,
84 &dev->res);
85 return ACPI_FAILURE(status) ? -ENODEV : 0;
86}
87
88static int pnpacpi_set_resources(struct pnp_dev * dev, struct pnp_resource_table * res)
89{
90 acpi_handle handle = dev->data;
91 struct acpi_buffer buffer;
92 int ret = 0;
93 acpi_status status;
94
95 ret = pnpacpi_build_resource_template(handle, &buffer);
96 if (ret)
97 return ret;
98 ret = pnpacpi_encode_resources(res, &buffer);
99 if (ret) {
100 kfree(buffer.pointer);
101 return ret;
102 }
103 status = acpi_set_current_resources(handle, &buffer);
104 if (ACPI_FAILURE(status))
105 ret = -EINVAL;
106 kfree(buffer.pointer);
107 return ret;
108}
109
110static int pnpacpi_disable_resources(struct pnp_dev *dev)
111{
112 acpi_status status;
113
114 /* acpi_unregister_gsi(pnp_irq(dev, 0)); */
115 status = acpi_evaluate_object((acpi_handle)dev->data,
116 "_DIS", NULL, NULL);
117 return ACPI_FAILURE(status) ? -ENODEV : 0;
118}
119
Adrian Bunkb449f632005-11-07 01:01:48 -0800120static struct pnp_protocol pnpacpi_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .name = "Plug and Play ACPI",
122 .get = pnpacpi_get_resources,
123 .set = pnpacpi_set_resources,
124 .disable = pnpacpi_disable_resources,
125};
126
127static int __init pnpacpi_add_device(struct acpi_device *device)
128{
129 acpi_handle temp = NULL;
130 acpi_status status;
131 struct pnp_id *dev_id;
132 struct pnp_dev *dev;
133
matthieu castet07b01202006-01-06 01:31:00 -0500134 status = acpi_get_handle(device->handle, "_CRS", &temp);
135 if (ACPI_FAILURE(status) || !ispnpidacpi(acpi_device_hid(device)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 is_exclusive_device(device))
137 return 0;
138
139 pnp_dbg("ACPI device : hid %s", acpi_device_hid(device));
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700140 dev = kcalloc(1, sizeof(struct pnp_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (!dev) {
142 pnp_err("Out of memory");
143 return -ENOMEM;
144 }
145 dev->data = device->handle;
146 /* .enabled means if the device can decode the resources */
147 dev->active = device->status.enabled;
148 status = acpi_get_handle(device->handle, "_SRS", &temp);
149 if (ACPI_SUCCESS(status))
150 dev->capabilities |= PNP_CONFIGURABLE;
151 dev->capabilities |= PNP_READ;
152 if (device->flags.dynamic_status)
153 dev->capabilities |= PNP_WRITE;
154 if (device->flags.removable)
155 dev->capabilities |= PNP_REMOVABLE;
156 status = acpi_get_handle(device->handle, "_DIS", &temp);
157 if (ACPI_SUCCESS(status))
158 dev->capabilities |= PNP_DISABLE;
159
160 dev->protocol = &pnpacpi_protocol;
161
162 if (strlen(acpi_device_name(device)))
163 strncpy(dev->name, acpi_device_name(device), sizeof(dev->name));
164 else
165 strncpy(dev->name, acpi_device_bid(device), sizeof(dev->name));
166
167 dev->number = num;
168
169 /* set the initial values for the PnP device */
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700170 dev_id = kcalloc(1, sizeof(struct pnp_id), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (!dev_id)
172 goto err;
173 pnpidacpi_to_pnpid(acpi_device_hid(device), dev_id->id);
174 pnp_add_id(dev_id, dev);
175
176 if(dev->active) {
177 /* parse allocated resource */
178 status = pnpacpi_parse_allocated_resource(device->handle, &dev->res);
179 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
180 pnp_err("PnPACPI: METHOD_NAME__CRS failure for %s", dev_id->id);
181 goto err1;
182 }
183 }
184
185 if(dev->capabilities & PNP_CONFIGURABLE) {
186 status = pnpacpi_parse_resource_option_data(device->handle,
187 dev);
188 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
189 pnp_err("PnPACPI: METHOD_NAME__PRS failure for %s", dev_id->id);
190 goto err1;
191 }
192 }
193
194 /* parse compatible ids */
195 if (device->flags.compatible_ids) {
196 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
197 int i;
198
199 for (i = 0; i < cid_list->count; i++) {
200 if (!ispnpidacpi(cid_list->id[i].value))
201 continue;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700202 dev_id = kcalloc(1, sizeof(struct pnp_id), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (!dev_id)
204 continue;
205
206 pnpidacpi_to_pnpid(cid_list->id[i].value, dev_id->id);
207 pnp_add_id(dev_id, dev);
208 }
209 }
210
211 /* clear out the damaged flags */
212 if (!dev->active)
213 pnp_init_resource_table(&dev->res);
214 pnp_add_device(dev);
215 num ++;
216
217 return AE_OK;
218err1:
219 kfree(dev_id);
220err:
221 kfree(dev);
222 return -EINVAL;
223}
224
225static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
226 u32 lvl, void *context, void **rv)
227{
228 struct acpi_device *device;
229
230 if (!acpi_bus_get_device(handle, &device))
231 pnpacpi_add_device(device);
232 else
233 return AE_CTRL_DEPTH;
234 return AE_OK;
235}
236
237int pnpacpi_disabled __initdata;
Adrian Bunkb449f632005-11-07 01:01:48 -0800238static int __init pnpacpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 if (acpi_disabled || pnpacpi_disabled) {
241 pnp_info("PnP ACPI: disabled");
242 return 0;
243 }
244 pnp_info("PnP ACPI init");
245 pnp_register_protocol(&pnpacpi_protocol);
246 acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
247 pnp_info("PnP ACPI: found %d devices", num);
248 return 0;
249}
250subsys_initcall(pnpacpi_init);
251
252static int __init pnpacpi_setup(char *str)
253{
254 if (str == NULL)
255 return 1;
256 if (!strncmp(str, "off", 3))
257 pnpacpi_disabled = 1;
258 return 1;
259}
260__setup("pnpacpi=", pnpacpi_setup);
261
Adrian Bunkb449f632005-11-07 01:01:48 -0800262#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263EXPORT_SYMBOL(pnpacpi_protocol);
Adrian Bunkb449f632005-11-07 01:01:48 -0800264#endif