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