xref: /netbsd-src/sys/dev/acpi/acpi_util.c (revision b62fc9e20372b08e1785ff6d769312d209fa2005)
1 /*	$NetBSD: acpi_util.c,v 1.2 2010/04/15 04:03:39 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2003, 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum of By Noon Software, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright 2001, 2003 Wasabi Systems, Inc.
34  * All rights reserved.
35  *
36  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed for the NetBSD Project by
49  *	Wasabi Systems, Inc.
50  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
51  *    or promote products derived from this software without specific prior
52  *    written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
58  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64  * POSSIBILITY OF SUCH DAMAGE.
65  */
66 
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: acpi_util.c,v 1.2 2010/04/15 04:03:39 jruoho Exp $");
69 
70 #include <sys/param.h>
71 
72 #include <dev/acpi/acpireg.h>
73 #include <dev/acpi/acpivar.h>
74 
75 #define _COMPONENT		ACPI_BUS_COMPONENT
76 ACPI_MODULE_NAME		("acpi_util")
77 
78 /*
79  * Evaluate an integer object.
80  */
81 ACPI_STATUS
82 acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
83 {
84 	ACPI_OBJECT obj;
85 	ACPI_BUFFER buf;
86 	ACPI_STATUS rv;
87 
88 	if (handle == NULL)
89 		handle = ACPI_ROOT_OBJECT;
90 
91 	buf.Pointer = &obj;
92 	buf.Length = sizeof(obj);
93 
94 	rv = AcpiEvaluateObject(handle, path, NULL, &buf);
95 
96 	if (ACPI_FAILURE(rv))
97 		return rv;
98 
99 	if (obj.Type != ACPI_TYPE_INTEGER)
100 		return AE_TYPE;
101 
102 	if (valp != NULL)
103 		*valp = obj.Integer.Value;
104 
105 	return AE_OK;
106 }
107 
108 /*
109  * Evaluate an integer object with a single integer input parameter.
110  */
111 ACPI_STATUS
112 acpi_eval_set_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER val)
113 {
114 	ACPI_OBJECT_LIST arg;
115 	ACPI_OBJECT obj;
116 
117 	if (handle == NULL)
118 		handle = ACPI_ROOT_OBJECT;
119 
120 	obj.Type = ACPI_TYPE_INTEGER;
121 	obj.Integer.Value = val;
122 
123 	arg.Count = 1;
124 	arg.Pointer = &obj;
125 
126 	return AcpiEvaluateObject(handle, path, &arg, NULL);
127 }
128 
129 /*
130  * Evaluate a (Unicode) string object.
131  */
132 ACPI_STATUS
133 acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
134 {
135 	ACPI_OBJECT *obj;
136 	ACPI_BUFFER buf;
137 	ACPI_STATUS rv;
138 
139 	rv = acpi_eval_struct(handle, path, &buf);
140 
141 	if (ACPI_FAILURE(rv))
142 		return rv;
143 
144 	obj = buf.Pointer;
145 
146 	if (obj->Type != ACPI_TYPE_STRING) {
147 		rv = AE_TYPE;
148 		goto out;
149 	}
150 
151 	if (obj->String.Length == 0) {
152 		rv = AE_BAD_DATA;
153 		goto out;
154 	}
155 
156 	*stringp = ACPI_ALLOCATE(obj->String.Length + 1);
157 
158 	if (*stringp == NULL) {
159 		rv = AE_NO_MEMORY;
160 		goto out;
161 	}
162 
163 	(void)memcpy(*stringp, obj->String.Pointer, obj->String.Length);
164 
165 	(*stringp)[obj->String.Length] = '\0';
166 
167 out:
168 	ACPI_FREE(buf.Pointer);
169 
170 	return rv;
171 }
172 
173 /*
174  * Evaluate a structure. Caller must free buf.Pointer by ACPI_FREE().
175  */
176 ACPI_STATUS
177 acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *buf)
178 {
179 
180 	if (handle == NULL)
181 		handle = ACPI_ROOT_OBJECT;
182 
183 	buf->Pointer = NULL;
184 	buf->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
185 
186 	return AcpiEvaluateObject(handle, path, NULL, buf);
187 }
188 
189 /*
190  * Evaluate a reference handle from an element in a package.
191  */
192 ACPI_STATUS
193 acpi_eval_reference_handle(ACPI_OBJECT *elm, ACPI_HANDLE *handle)
194 {
195 
196 	if (elm == NULL || handle == NULL)
197 		return AE_BAD_PARAMETER;
198 
199 	switch (elm->Type) {
200 
201 	case ACPI_TYPE_ANY:
202 	case ACPI_TYPE_LOCAL_REFERENCE:
203 
204 		if (elm->Reference.Handle == NULL)
205 			return AE_NULL_ENTRY;
206 
207 		*handle = elm->Reference.Handle;
208 
209 		return AE_OK;
210 
211 	case ACPI_TYPE_STRING:
212 		return AcpiGetHandle(NULL, elm->String.Pointer, handle);
213 
214 	default:
215 		return AE_TYPE;
216 	}
217 }
218 
219 /*
220  * Iterate over all objects in a package, and pass them all
221  * to a function. If the called function returns non-AE_OK,
222  * the iteration is stopped and that value is returned.
223  */
224 ACPI_STATUS
225 acpi_foreach_package_object(ACPI_OBJECT *pkg,
226     ACPI_STATUS (*func)(ACPI_OBJECT *, void *), void *arg)
227 {
228 	ACPI_STATUS rv = AE_OK;
229 	uint32_t i;
230 
231 	if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
232 		return AE_BAD_PARAMETER;
233 
234 	for (i = 0; i < pkg->Package.Count; i++) {
235 
236 		rv = (*func)(&pkg->Package.Elements[i], arg);
237 
238 		if (ACPI_FAILURE(rv))
239 			break;
240 	}
241 
242 	return rv;
243 }
244 
245 /*
246  * Fetch data info the specified (empty) ACPI buffer.
247  * Caller must free buf.Pointer by ACPI_FREE().
248  */
249 ACPI_STATUS
250 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
251     ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
252 {
253 
254 	buf->Pointer = NULL;
255 	buf->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
256 
257 	return (*getit)(handle, buf);
258 }
259 
260 /*
261  * Return a complete pathname from a handle.
262  *
263  * Note that the function uses static data storage;
264  * if the data is needed for future use, it should be
265  * copied before any subsequent calls overwrite it.
266  */
267 const char *
268 acpi_name(ACPI_HANDLE handle)
269 {
270 	static char name[80];
271 	ACPI_BUFFER buf;
272 	ACPI_STATUS rv;
273 
274 	buf.Pointer = name;
275 	buf.Length = sizeof(name);
276 
277 	rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
278 
279 	if (ACPI_FAILURE(rv))
280 		return "UNKNOWN";
281 
282 	return name;
283 }
284 
285 /*
286  * Match given IDs against _HID and _CIDs.
287  */
288 int
289 acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
290 {
291 	uint32_t i, n;
292 	char *id;
293 
294 	while (*ids) {
295 
296 		if ((ad->Valid & ACPI_VALID_HID) != 0) {
297 
298 			if (pmatch(ad->HardwareId.String, *ids, NULL) == 2)
299 				return 1;
300 		}
301 
302 		if ((ad->Valid & ACPI_VALID_CID) != 0) {
303 
304 			n = ad->CompatibleIdList.Count;
305 
306 			for (i = 0; i < n; i++) {
307 
308 				id = ad->CompatibleIdList.Ids[i].String;
309 
310 				if (pmatch(id, *ids, NULL) == 2)
311 					return 1;
312 			}
313 		}
314 
315 		ids++;
316 	}
317 
318 	return 0;
319 }
320 
321