Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / drivers / pnp / pnpacpi / core.c
1 /*
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  
22 #include <linux/acpi.h>
23 #include <linux/pnp.h>
24 #include <acpi/acpi_bus.h>
25 #include "pnpacpi.h"
26
27 static int num = 0;
28
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.
35  */
36 static char __initdata excluded_id_list[] =
37         "PNP0C09," /* EC */
38         "PNP0C0F," /* Link device */
39         "PNP0000," /* PIC */
40         "PNP0100," /* Timer */
41         ;
42 static inline int is_exclusive_device(struct acpi_device *dev)
43 {
44         return (!acpi_match_ids(dev, excluded_id_list));
45 }
46
47 /*
48  * Compatible Device IDs
49  */
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)
57 {
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;
68 }
69
70 static void __init pnpidacpi_to_pnpid(char *id, char *str)
71 {
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';
80 }
81
82 static int pnpacpi_get_resources(struct pnp_dev * dev, struct pnp_resource_table * res)
83 {
84         acpi_status status;
85         status = pnpacpi_parse_allocated_resource((acpi_handle)dev->data, 
86                 &dev->res);
87         return ACPI_FAILURE(status) ? -ENODEV : 0;
88 }
89
90 static int pnpacpi_set_resources(struct pnp_dev * dev, struct pnp_resource_table * res)
91 {
92         acpi_handle handle = dev->data;
93         struct acpi_buffer buffer;
94         int ret = 0;
95         acpi_status status;
96
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;
104         }
105         status = acpi_set_current_resources(handle, &buffer);
106         if (ACPI_FAILURE(status))
107                 ret = -EINVAL;
108         kfree(buffer.pointer);
109         return ret;
110 }
111
112 static int pnpacpi_disable_resources(struct pnp_dev *dev)
113 {
114         acpi_status status;
115         
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;
120 }
121
122 static struct pnp_protocol pnpacpi_protocol = {
123         .name   = "Plug and Play ACPI",
124         .get    = pnpacpi_get_resources,
125         .set    = pnpacpi_set_resources,
126         .disable = pnpacpi_disable_resources,
127 };
128
129 static int __init pnpacpi_add_device(struct acpi_device *device)
130 {
131         acpi_handle temp = NULL;
132         acpi_status status;
133         struct pnp_id *dev_id;
134         struct pnp_dev *dev;
135
136         status = acpi_get_handle(device->handle, "_CRS", &temp);
137         if (ACPI_FAILURE(status) || !ispnpidacpi(acpi_device_hid(device)) ||
138                 is_exclusive_device(device))
139                 return 0;
140
141         pnp_dbg("ACPI device : hid %s", acpi_device_hid(device));
142         dev =  kcalloc(1, sizeof(struct pnp_dev), GFP_KERNEL);
143         if (!dev) {
144                 pnp_err("Out of memory");
145                 return -ENOMEM;
146         }
147         dev->data = device->handle;
148         /* .enabled means if the device can decode the resources */
149         dev->active = device->status.enabled;
150         status = acpi_get_handle(device->handle, "_SRS", &temp);
151         if (ACPI_SUCCESS(status))
152                 dev->capabilities |= PNP_CONFIGURABLE;
153         dev->capabilities |= PNP_READ;
154         if (device->flags.dynamic_status)
155                 dev->capabilities |= PNP_WRITE;
156         if (device->flags.removable)
157                 dev->capabilities |= PNP_REMOVABLE;
158         status = acpi_get_handle(device->handle, "_DIS", &temp);
159         if (ACPI_SUCCESS(status))
160                 dev->capabilities |= PNP_DISABLE;
161
162         dev->protocol = &pnpacpi_protocol;
163
164         if (strlen(acpi_device_name(device)))
165                 strncpy(dev->name, acpi_device_name(device), sizeof(dev->name));
166         else
167                 strncpy(dev->name, acpi_device_bid(device), sizeof(dev->name));
168
169         dev->number = num;
170         
171         /* set the initial values for the PnP device */
172         dev_id = kcalloc(1, sizeof(struct pnp_id), GFP_KERNEL);
173         if (!dev_id)
174                 goto err;
175         pnpidacpi_to_pnpid(acpi_device_hid(device), dev_id->id);
176         pnp_add_id(dev_id, dev);
177
178         if(dev->active) {
179                 /* parse allocated resource */
180                 status = pnpacpi_parse_allocated_resource(device->handle, &dev->res);
181                 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
182                         pnp_err("PnPACPI: METHOD_NAME__CRS failure for %s", dev_id->id);
183                         goto err1;
184                 }
185         }
186
187         if(dev->capabilities & PNP_CONFIGURABLE) {
188                 status = pnpacpi_parse_resource_option_data(device->handle, 
189                         dev);
190                 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
191                         pnp_err("PnPACPI: METHOD_NAME__PRS failure for %s", dev_id->id);
192                         goto err1;
193                 }
194         }
195         
196         /* parse compatible ids */
197         if (device->flags.compatible_ids) {
198                 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
199                 int i;
200
201                 for (i = 0; i < cid_list->count; i++) {
202                         if (!ispnpidacpi(cid_list->id[i].value))
203                                 continue;
204                         dev_id = kcalloc(1, sizeof(struct pnp_id), GFP_KERNEL);
205                         if (!dev_id)
206                                 continue;
207
208                         pnpidacpi_to_pnpid(cid_list->id[i].value, dev_id->id);
209                         pnp_add_id(dev_id, dev);
210                 }
211         }
212
213         /* clear out the damaged flags */
214         if (!dev->active)
215                 pnp_init_resource_table(&dev->res);
216         pnp_add_device(dev);
217         num ++;
218
219         return AE_OK;
220 err1:
221         kfree(dev_id);
222 err:
223         kfree(dev);
224         return -EINVAL;
225 }
226
227 static acpi_status __init pnpacpi_add_device_handler(acpi_handle handle,
228         u32 lvl, void *context, void **rv)
229 {
230         struct acpi_device *device;
231
232         if (!acpi_bus_get_device(handle, &device))
233                 pnpacpi_add_device(device);
234         else
235                 return AE_CTRL_DEPTH;
236         return AE_OK;
237 }
238
239 int pnpacpi_disabled __initdata;
240 static int __init pnpacpi_init(void)
241 {
242         if (acpi_disabled || pnpacpi_disabled) {
243                 pnp_info("PnP ACPI: disabled");
244                 return 0;
245         }
246         pnp_info("PnP ACPI init");
247         pnp_register_protocol(&pnpacpi_protocol);
248         acpi_get_devices(NULL, pnpacpi_add_device_handler, NULL, NULL);
249         pnp_info("PnP ACPI: found %d devices", num);
250         return 0;
251 }
252 subsys_initcall(pnpacpi_init);
253
254 static int __init pnpacpi_setup(char *str)
255 {
256         if (str == NULL)
257                 return 1;
258         if (!strncmp(str, "off", 3))
259                 pnpacpi_disabled = 1;
260         return 1;
261 }
262 __setup("pnpacpi=", pnpacpi_setup);
263
264 #if 0
265 EXPORT_SYMBOL(pnpacpi_protocol);
266 #endif