acpi_util.c revision 1.2 1 1.2 jruoho /* $NetBSD: acpi_util.c,v 1.2 2010/04/15 04:03:39 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.2 jruoho __KERNEL_RCSID(0, "$NetBSD: acpi_util.c,v 1.2 2010/04/15 04:03:39 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.2 jruoho * Evaluate an integer object.
80 1.1 jruoho */
81 1.1 jruoho ACPI_STATUS
82 1.1 jruoho acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
83 1.1 jruoho {
84 1.1 jruoho ACPI_OBJECT obj;
85 1.1 jruoho ACPI_BUFFER buf;
86 1.1 jruoho ACPI_STATUS rv;
87 1.1 jruoho
88 1.1 jruoho if (handle == NULL)
89 1.1 jruoho handle = ACPI_ROOT_OBJECT;
90 1.1 jruoho
91 1.1 jruoho buf.Pointer = &obj;
92 1.1 jruoho buf.Length = sizeof(obj);
93 1.1 jruoho
94 1.1 jruoho rv = AcpiEvaluateObject(handle, path, NULL, &buf);
95 1.1 jruoho
96 1.1 jruoho if (ACPI_FAILURE(rv))
97 1.1 jruoho return rv;
98 1.1 jruoho
99 1.1 jruoho if (obj.Type != ACPI_TYPE_INTEGER)
100 1.1 jruoho return AE_TYPE;
101 1.1 jruoho
102 1.1 jruoho if (valp != NULL)
103 1.1 jruoho *valp = obj.Integer.Value;
104 1.1 jruoho
105 1.1 jruoho return AE_OK;
106 1.1 jruoho }
107 1.1 jruoho
108 1.1 jruoho /*
109 1.2 jruoho * Evaluate an integer object with a single integer input parameter.
110 1.1 jruoho */
111 1.1 jruoho ACPI_STATUS
112 1.1 jruoho acpi_eval_set_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER val)
113 1.1 jruoho {
114 1.1 jruoho ACPI_OBJECT_LIST arg;
115 1.1 jruoho ACPI_OBJECT obj;
116 1.1 jruoho
117 1.1 jruoho if (handle == NULL)
118 1.1 jruoho handle = ACPI_ROOT_OBJECT;
119 1.1 jruoho
120 1.1 jruoho obj.Type = ACPI_TYPE_INTEGER;
121 1.1 jruoho obj.Integer.Value = val;
122 1.1 jruoho
123 1.1 jruoho arg.Count = 1;
124 1.1 jruoho arg.Pointer = &obj;
125 1.1 jruoho
126 1.1 jruoho return AcpiEvaluateObject(handle, path, &arg, NULL);
127 1.1 jruoho }
128 1.1 jruoho
129 1.1 jruoho /*
130 1.2 jruoho * Evaluate a (Unicode) string object.
131 1.1 jruoho */
132 1.1 jruoho ACPI_STATUS
133 1.1 jruoho acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
134 1.1 jruoho {
135 1.1 jruoho ACPI_OBJECT *obj;
136 1.1 jruoho ACPI_BUFFER buf;
137 1.1 jruoho ACPI_STATUS rv;
138 1.1 jruoho
139 1.1 jruoho rv = acpi_eval_struct(handle, path, &buf);
140 1.1 jruoho
141 1.1 jruoho if (ACPI_FAILURE(rv))
142 1.1 jruoho return rv;
143 1.1 jruoho
144 1.1 jruoho obj = buf.Pointer;
145 1.1 jruoho
146 1.1 jruoho if (obj->Type != ACPI_TYPE_STRING) {
147 1.1 jruoho rv = AE_TYPE;
148 1.1 jruoho goto out;
149 1.1 jruoho }
150 1.1 jruoho
151 1.1 jruoho if (obj->String.Length == 0) {
152 1.1 jruoho rv = AE_BAD_DATA;
153 1.1 jruoho goto out;
154 1.1 jruoho }
155 1.1 jruoho
156 1.1 jruoho *stringp = ACPI_ALLOCATE(obj->String.Length + 1);
157 1.1 jruoho
158 1.1 jruoho if (*stringp == NULL) {
159 1.1 jruoho rv = AE_NO_MEMORY;
160 1.1 jruoho goto out;
161 1.1 jruoho }
162 1.1 jruoho
163 1.1 jruoho (void)memcpy(*stringp, obj->String.Pointer, obj->String.Length);
164 1.1 jruoho
165 1.1 jruoho (*stringp)[obj->String.Length] = '\0';
166 1.1 jruoho
167 1.1 jruoho out:
168 1.1 jruoho ACPI_FREE(buf.Pointer);
169 1.1 jruoho
170 1.1 jruoho return rv;
171 1.1 jruoho }
172 1.1 jruoho
173 1.1 jruoho /*
174 1.2 jruoho * Evaluate a structure. Caller must free buf.Pointer by ACPI_FREE().
175 1.1 jruoho */
176 1.1 jruoho ACPI_STATUS
177 1.1 jruoho acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *buf)
178 1.1 jruoho {
179 1.1 jruoho
180 1.1 jruoho if (handle == NULL)
181 1.1 jruoho handle = ACPI_ROOT_OBJECT;
182 1.1 jruoho
183 1.1 jruoho buf->Pointer = NULL;
184 1.1 jruoho buf->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
185 1.1 jruoho
186 1.1 jruoho return AcpiEvaluateObject(handle, path, NULL, buf);
187 1.1 jruoho }
188 1.1 jruoho
189 1.1 jruoho /*
190 1.2 jruoho * Evaluate a reference handle from an element in a package.
191 1.1 jruoho */
192 1.1 jruoho ACPI_STATUS
193 1.1 jruoho acpi_eval_reference_handle(ACPI_OBJECT *elm, ACPI_HANDLE *handle)
194 1.1 jruoho {
195 1.1 jruoho
196 1.1 jruoho if (elm == NULL || handle == NULL)
197 1.1 jruoho return AE_BAD_PARAMETER;
198 1.1 jruoho
199 1.1 jruoho switch (elm->Type) {
200 1.1 jruoho
201 1.1 jruoho case ACPI_TYPE_ANY:
202 1.1 jruoho case ACPI_TYPE_LOCAL_REFERENCE:
203 1.1 jruoho
204 1.1 jruoho if (elm->Reference.Handle == NULL)
205 1.1 jruoho return AE_NULL_ENTRY;
206 1.1 jruoho
207 1.1 jruoho *handle = elm->Reference.Handle;
208 1.1 jruoho
209 1.1 jruoho return AE_OK;
210 1.1 jruoho
211 1.1 jruoho case ACPI_TYPE_STRING:
212 1.1 jruoho return AcpiGetHandle(NULL, elm->String.Pointer, handle);
213 1.1 jruoho
214 1.1 jruoho default:
215 1.1 jruoho return AE_TYPE;
216 1.1 jruoho }
217 1.1 jruoho }
218 1.1 jruoho
219 1.1 jruoho /*
220 1.2 jruoho * Iterate over all objects in a package, and pass them all
221 1.2 jruoho * to a function. If the called function returns non-AE_OK,
222 1.2 jruoho * the iteration is stopped and that value is returned.
223 1.1 jruoho */
224 1.1 jruoho ACPI_STATUS
225 1.1 jruoho acpi_foreach_package_object(ACPI_OBJECT *pkg,
226 1.1 jruoho ACPI_STATUS (*func)(ACPI_OBJECT *, void *), void *arg)
227 1.1 jruoho {
228 1.1 jruoho ACPI_STATUS rv = AE_OK;
229 1.1 jruoho uint32_t i;
230 1.1 jruoho
231 1.1 jruoho if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
232 1.1 jruoho return AE_BAD_PARAMETER;
233 1.1 jruoho
234 1.1 jruoho for (i = 0; i < pkg->Package.Count; i++) {
235 1.1 jruoho
236 1.1 jruoho rv = (*func)(&pkg->Package.Elements[i], arg);
237 1.1 jruoho
238 1.1 jruoho if (ACPI_FAILURE(rv))
239 1.1 jruoho break;
240 1.1 jruoho }
241 1.1 jruoho
242 1.1 jruoho return rv;
243 1.1 jruoho }
244 1.1 jruoho
245 1.1 jruoho /*
246 1.2 jruoho * Fetch data info the specified (empty) ACPI buffer.
247 1.2 jruoho * Caller must free buf.Pointer by ACPI_FREE().
248 1.1 jruoho */
249 1.1 jruoho ACPI_STATUS
250 1.1 jruoho acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
251 1.1 jruoho ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
252 1.1 jruoho {
253 1.1 jruoho
254 1.1 jruoho buf->Pointer = NULL;
255 1.1 jruoho buf->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
256 1.1 jruoho
257 1.1 jruoho return (*getit)(handle, buf);
258 1.1 jruoho }
259 1.1 jruoho
260 1.1 jruoho /*
261 1.2 jruoho * Return a complete pathname from a handle.
262 1.1 jruoho *
263 1.2 jruoho * Note that the function uses static data storage;
264 1.2 jruoho * if the data is needed for future use, it should be
265 1.2 jruoho * copied before any subsequent calls overwrite it.
266 1.1 jruoho */
267 1.1 jruoho const char *
268 1.1 jruoho acpi_name(ACPI_HANDLE handle)
269 1.1 jruoho {
270 1.1 jruoho static char name[80];
271 1.1 jruoho ACPI_BUFFER buf;
272 1.1 jruoho ACPI_STATUS rv;
273 1.1 jruoho
274 1.1 jruoho buf.Pointer = name;
275 1.1 jruoho buf.Length = sizeof(name);
276 1.1 jruoho
277 1.1 jruoho rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
278 1.1 jruoho
279 1.1 jruoho if (ACPI_FAILURE(rv))
280 1.1 jruoho return "UNKNOWN";
281 1.1 jruoho
282 1.1 jruoho return name;
283 1.1 jruoho }
284 1.1 jruoho
285 1.1 jruoho /*
286 1.2 jruoho * Match given IDs against _HID and _CIDs.
287 1.1 jruoho */
288 1.1 jruoho int
289 1.1 jruoho acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
290 1.1 jruoho {
291 1.1 jruoho uint32_t i, n;
292 1.1 jruoho char *id;
293 1.1 jruoho
294 1.1 jruoho while (*ids) {
295 1.1 jruoho
296 1.1 jruoho if ((ad->Valid & ACPI_VALID_HID) != 0) {
297 1.1 jruoho
298 1.1 jruoho if (pmatch(ad->HardwareId.String, *ids, NULL) == 2)
299 1.1 jruoho return 1;
300 1.1 jruoho }
301 1.1 jruoho
302 1.1 jruoho if ((ad->Valid & ACPI_VALID_CID) != 0) {
303 1.1 jruoho
304 1.1 jruoho n = ad->CompatibleIdList.Count;
305 1.1 jruoho
306 1.1 jruoho for (i = 0; i < n; i++) {
307 1.1 jruoho
308 1.1 jruoho id = ad->CompatibleIdList.Ids[i].String;
309 1.1 jruoho
310 1.1 jruoho if (pmatch(id, *ids, NULL) == 2)
311 1.1 jruoho return 1;
312 1.1 jruoho }
313 1.1 jruoho }
314 1.1 jruoho
315 1.1 jruoho ids++;
316 1.1 jruoho }
317 1.1 jruoho
318 1.1 jruoho return 0;
319 1.1 jruoho }
320 1.1 jruoho
321