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/acpi.h>
23 #include <linux/pnp.h>
24 #include <acpi/acpi_bus.h>
29 static char __initdata excluded_id_list
[] =
30 "PNP0C0A," /* Battery */
31 "PNP0C0C,PNP0C0E,PNP0C0D," /* Button */
34 "PNP0A03," /* PCI root */
35 "PNP0C0F," /* Link device */
37 "PNP0100," /* Timer */
39 static inline int is_exclusive_device(struct acpi_device
*dev
)
41 return (!acpi_match_ids(dev
, excluded_id_list
));
44 void *pnpacpi_kmalloc(size_t size
, int f
)
46 void *p
= kmalloc(size
, f
);
53 * Compatible Device IDs
56 if (!(('0' <= (c) && (c) <= '9') || ('A' <= (c) && (c) <= 'F'))) \
58 #define TEST_ALPHA(c) \
59 if (!('@' <= (c) || (c) <= 'Z')) \
61 static int __init
ispnpidacpi(char *id
)
75 static void __init
pnpidacpi_to_pnpid(char *id
, char *str
)
80 str
[3] = tolower(id
[3]);
81 str
[4] = tolower(id
[4]);
82 str
[5] = tolower(id
[5]);
83 str
[6] = tolower(id
[6]);
87 static int pnpacpi_get_resources(struct pnp_dev
* dev
, struct pnp_resource_table
* res
)
90 status
= pnpacpi_parse_allocated_resource((acpi_handle
)dev
->data
,
92 return ACPI_FAILURE(status
) ? -ENODEV
: 0;
95 static int pnpacpi_set_resources(struct pnp_dev
* dev
, struct pnp_resource_table
* res
)
97 acpi_handle handle
= dev
->data
;
98 struct acpi_buffer buffer
;
102 ret
= pnpacpi_build_resource_template(handle
, &buffer
);
105 ret
= pnpacpi_encode_resources(res
, &buffer
);
107 kfree(buffer
.pointer
);
110 status
= acpi_set_current_resources(handle
, &buffer
);
111 if (ACPI_FAILURE(status
))
113 kfree(buffer
.pointer
);
117 static int pnpacpi_disable_resources(struct pnp_dev
*dev
)
121 /* acpi_unregister_gsi(pnp_irq(dev, 0)); */
122 status
= acpi_evaluate_object((acpi_handle
)dev
->data
,
124 return ACPI_FAILURE(status
) ? -ENODEV
: 0;
127 struct pnp_protocol pnpacpi_protocol
= {
128 .name
= "Plug and Play ACPI",
129 .get
= pnpacpi_get_resources
,
130 .set
= pnpacpi_set_resources
,
131 .disable
= pnpacpi_disable_resources
,
134 static int __init
pnpacpi_add_device(struct acpi_device
*device
)
136 acpi_handle temp
= NULL
;
138 struct pnp_id
*dev_id
;
141 if (!ispnpidacpi(acpi_device_hid(device
)) ||
142 is_exclusive_device(device
))
145 pnp_dbg("ACPI device : hid %s", acpi_device_hid(device
));
146 dev
= pnpacpi_kmalloc(sizeof(struct pnp_dev
), GFP_KERNEL
);
148 pnp_err("Out of memory");
151 dev
->data
= device
->handle
;
152 /* .enabled means if the device can decode the resources */
153 dev
->active
= device
->status
.enabled
;
154 status
= acpi_get_handle(device
->handle
, "_SRS", &temp
);
155 if (ACPI_SUCCESS(status
))
156 dev
->capabilities
|= PNP_CONFIGURABLE
;
157 dev
->capabilities
|= PNP_READ
;
158 if (device
->flags
.dynamic_status
)
159 dev
->capabilities
|= PNP_WRITE
;
160 if (device
->flags
.removable
)
161 dev
->capabilities
|= PNP_REMOVABLE
;
162 status
= acpi_get_handle(device
->handle
, "_DIS", &temp
);
163 if (ACPI_SUCCESS(status
))
164 dev
->capabilities
|= PNP_DISABLE
;
166 dev
->protocol
= &pnpacpi_protocol
;
168 if (strlen(acpi_device_name(device
)))
169 strncpy(dev
->name
, acpi_device_name(device
), sizeof(dev
->name
));
171 strncpy(dev
->name
, acpi_device_bid(device
), sizeof(dev
->name
));
175 /* set the initial values for the PnP device */
176 dev_id
= pnpacpi_kmalloc(sizeof(struct pnp_id
), GFP_KERNEL
);
179 pnpidacpi_to_pnpid(acpi_device_hid(device
), dev_id
->id
);
180 pnp_add_id(dev_id
, dev
);
183 /* parse allocated resource */
184 status
= pnpacpi_parse_allocated_resource(device
->handle
, &dev
->res
);
185 if (ACPI_FAILURE(status
) && (status
!= AE_NOT_FOUND
)) {
186 pnp_err("PnPACPI: METHOD_NAME__CRS failure for %s", dev_id
->id
);
191 if(dev
->capabilities
& PNP_CONFIGURABLE
) {
192 status
= pnpacpi_parse_resource_option_data(device
->handle
,
194 if (ACPI_FAILURE(status
) && (status
!= AE_NOT_FOUND
)) {
195 pnp_err("PnPACPI: METHOD_NAME__PRS failure for %s", dev_id
->id
);
200 /* parse compatible ids */
201 if (device
->flags
.compatible_ids
) {
202 struct acpi_compatible_id_list
*cid_list
= device
->pnp
.cid_list
;
205 for (i
= 0; i
< cid_list
->count
; i
++) {
206 if (!ispnpidacpi(cid_list
->id
[i
].value
))
208 dev_id
= pnpacpi_kmalloc(sizeof(struct pnp_id
),
213 pnpidacpi_to_pnpid(cid_list
->id
[i
].value
, dev_id
->id
);
214 pnp_add_id(dev_id
, dev
);
218 /* clear out the damaged flags */
220 pnp_init_resource_table(&dev
->res
);
232 static acpi_status __init
pnpacpi_add_device_handler(acpi_handle handle
,
233 u32 lvl
, void *context
, void **rv
)
235 struct acpi_device
*device
;
237 if (!acpi_bus_get_device(handle
, &device
))
238 pnpacpi_add_device(device
);
240 return AE_CTRL_DEPTH
;
244 int pnpacpi_disabled __initdata
;
245 int __init
pnpacpi_init(void)
247 if (acpi_disabled
|| pnpacpi_disabled
) {
248 pnp_info("PnP ACPI: disabled");
251 pnp_info("PnP ACPI init");
252 pnp_register_protocol(&pnpacpi_protocol
);
253 acpi_get_devices(NULL
, pnpacpi_add_device_handler
, NULL
, NULL
);
254 pnp_info("PnP ACPI: found %d devices", num
);
257 subsys_initcall(pnpacpi_init
);
259 static int __init
pnpacpi_setup(char *str
)
263 if (!strncmp(str
, "off", 3))
264 pnpacpi_disabled
= 1;
267 __setup("pnpacpi=", pnpacpi_setup
);
269 EXPORT_SYMBOL(pnpacpi_protocol
);