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