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 static 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 status
= acpi_get_handle(device
->handle
, "_CRS", &temp
);
135 if (ACPI_FAILURE(status
) || !ispnpidacpi(acpi_device_hid(device
)) ||
136 is_exclusive_device(device
))
139 pnp_dbg("ACPI device : hid %s", acpi_device_hid(device
));
140 dev
= kcalloc(1, sizeof(struct pnp_dev
), GFP_KERNEL
);
142 pnp_err("Out of memory");
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
;
160 dev
->protocol
= &pnpacpi_protocol
;
162 if (strlen(acpi_device_name(device
)))
163 strncpy(dev
->name
, acpi_device_name(device
), sizeof(dev
->name
));
165 strncpy(dev
->name
, acpi_device_bid(device
), sizeof(dev
->name
));
169 /* set the initial values for the PnP device */
170 dev_id
= kcalloc(1, sizeof(struct pnp_id
), GFP_KERNEL
);
173 pnpidacpi_to_pnpid(acpi_device_hid(device
), dev_id
->id
);
174 pnp_add_id(dev_id
, dev
);
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
);
185 if(dev
->capabilities
& PNP_CONFIGURABLE
) {
186 status
= pnpacpi_parse_resource_option_data(device
->handle
,
188 if (ACPI_FAILURE(status
) && (status
!= AE_NOT_FOUND
)) {
189 pnp_err("PnPACPI: METHOD_NAME__PRS failure for %s", dev_id
->id
);
194 /* parse compatible ids */
195 if (device
->flags
.compatible_ids
) {
196 struct acpi_compatible_id_list
*cid_list
= device
->pnp
.cid_list
;
199 for (i
= 0; i
< cid_list
->count
; i
++) {
200 if (!ispnpidacpi(cid_list
->id
[i
].value
))
202 dev_id
= kcalloc(1, sizeof(struct pnp_id
), GFP_KERNEL
);
206 pnpidacpi_to_pnpid(cid_list
->id
[i
].value
, dev_id
->id
);
207 pnp_add_id(dev_id
, dev
);
211 /* clear out the damaged flags */
213 pnp_init_resource_table(&dev
->res
);
225 static acpi_status __init
pnpacpi_add_device_handler(acpi_handle handle
,
226 u32 lvl
, void *context
, void **rv
)
228 struct acpi_device
*device
;
230 if (!acpi_bus_get_device(handle
, &device
))
231 pnpacpi_add_device(device
);
233 return AE_CTRL_DEPTH
;
237 int pnpacpi_disabled __initdata
;
238 static int __init
pnpacpi_init(void)
240 if (acpi_disabled
|| pnpacpi_disabled
) {
241 pnp_info("PnP ACPI: disabled");
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
);
250 subsys_initcall(pnpacpi_init
);
252 static int __init
pnpacpi_setup(char *str
)
256 if (!strncmp(str
, "off", 3))
257 pnpacpi_disabled
= 1;
260 __setup("pnpacpi=", pnpacpi_setup
);
263 EXPORT_SYMBOL(pnpacpi_protocol
);