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