1 /*-
2 * Copyright (c) 2003 Nate Lawson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: releng/5.2/sys/dev/acpica/acpi_package.c 122765 2003-11-15 19:20:46Z njl $
27 */
28
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/bus.h>
32 #include <sys/sbuf.h>
33
34 #include <machine/bus_pio.h>
35 #include <machine/bus.h>
36 #include <machine/resource.h>
37 #include <sys/rman.h>
38
39 #include "acpi.h"
40 #include <dev/acpica/acpivar.h>
41
42 /*
43 * Package manipulation convenience functions
44 */
45
46 int
47 acpi_PkgInt(device_t dev, ACPI_OBJECT *res, int idx, ACPI_INTEGER *dst)
48 {
49 ACPI_OBJECT *obj;
50
51 obj = &res->Package.Elements[idx];
52 if (obj == NULL || obj->Type != ACPI_TYPE_INTEGER) {
53 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
54 "PkgInt error, pkg[%d] at %d %p\n",
55 res->Package.Count, idx, obj);
56 return (-1);
57 }
58
59 *dst = obj->Integer.Value;
60 return (0);
61 }
62
63 int
64 acpi_PkgInt32(device_t dev, ACPI_OBJECT *res, int idx, uint32_t *dst)
65 {
66 ACPI_INTEGER tmp;
67 int error;
68
69 error = acpi_PkgInt(dev, res, idx, &tmp);
70 if (error == 0)
71 *dst = (uint32_t)tmp;
72 return (error);
73 }
74
75 int
76 acpi_PkgStr(device_t dev, ACPI_OBJECT *res, int idx, void *dst, size_t size)
77 {
78 ACPI_OBJECT *obj;
79 void *ptr;
80 size_t length;
81
82 obj = &res->Package.Elements[idx];
83 if (obj == NULL) {
84 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
85 "PkgStr NULL object at %d\n", idx);
86 return (-1);
87 }
88 bzero(dst, sizeof(dst));
89
90 switch (obj->Type) {
91 case ACPI_TYPE_STRING:
92 ptr = obj->String.Pointer;
93 length = obj->String.Length;
94 break;
95 case ACPI_TYPE_BUFFER:
96 ptr = obj->Buffer.Pointer;
97 length = obj->Buffer.Length;
98 break;
99 default:
100 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
101 "PkgStr: invalid object type %d at %d\n", obj->Type, idx);
102 return (-1);
103 }
104
105 /* Make sure string will fit, including terminating NUL */
106 if (++length > size) {
107 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
108 "PkgStr string too long (%zu bytes) at %d\n", length, idx);
109 return (-1);
110 }
111
112 strlcpy(dst, ptr, length);
113 return (0);
114 }
115
116 int
117 acpi_PkgGas(device_t dev, ACPI_OBJECT *res, int idx, int *rid,
118 struct resource **dst)
119 {
120 ACPI_GENERIC_ADDRESS gas;
121 ACPI_OBJECT *obj;
122
123 obj = &res->Package.Elements[idx];
124 if (obj == NULL || obj->Type != ACPI_TYPE_BUFFER ||
125 obj->Buffer.Length < sizeof(ACPI_GENERIC_ADDRESS) + 3) {
126
127 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
128 "PkgGas error at %d\n", idx);
129 return (-1);
130 }
131
132 memcpy(&gas, obj->Buffer.Pointer + 3, sizeof(gas));
133 *dst = acpi_bus_alloc_gas(dev, rid, &gas);
134 if (*dst == NULL) {
135 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
136 "PkgGas error at %d\n", idx);
137 return (-1);
138 }
139
140 return (0);
141 }
Cache object: 6e5199b2463734b9edab1424f9377fac
|