1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
/*-
* Copyright (c) 1999 Takanori Watanabe
* Copyright (c) 1999, 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: aml_common.c,v 1.9 2000/08/09 14:47:43 iwasaki Exp $
* $FreeBSD$
*/
#include <sys/param.h>
#ifndef _KERNEL
#include <assert.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#else /* _KERNEL */
#include "opt_acpi.h"
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#endif /* !_KERNEL */
#include <dev/acpi/aml/aml_common.h>
#include <dev/acpi/aml/aml_env.h>
#include <dev/acpi/aml/aml_evalobj.h>
#include <dev/acpi/aml/aml_name.h>
#include <dev/acpi/aml/aml_obj.h>
#include <dev/acpi/aml/aml_parse.h>
#include <dev/acpi/aml/aml_status.h>
#include <dev/acpi/aml/aml_store.h>
/* for debugging */
#ifdef AML_DEBUG
int aml_debug = 1;
#else /* !AML_DEBUG */
int aml_debug = 0;
#endif /* AML_DEBUG */
#ifdef _KERNEL
SYSCTL_INT(_debug, OID_AUTO, aml_debug, CTLFLAG_RW, &aml_debug, 1, "");
#endif /* _KERNEL */
static void aml_print_nameseg(u_int8_t *dp);
static void
aml_print_nameseg(u_int8_t *dp)
{
if (dp[3] != '_') {
AML_DEBUGPRINT("%c%c%c%c", dp[0], dp[1], dp[2], dp[3]);
} else if (dp[2] != '_') {
AML_DEBUGPRINT("%c%c%c_", dp[0], dp[1], dp[2]);
} else if (dp[1] != '_') {
AML_DEBUGPRINT("%c%c__", dp[0], dp[1]);
} else if (dp[0] != '_') {
AML_DEBUGPRINT("%c___", dp[0]);
}
}
void
aml_print_namestring(u_int8_t *dp)
{
int segcount;
int i;
if (dp[0] == '\\') {
AML_DEBUGPRINT("%c", dp[0]);
dp++;
} else if (dp[0] == '^') {
while (dp[0] == '^') {
AML_DEBUGPRINT("%c", dp[0]);
dp++;
}
}
if (dp[0] == 0x00) { /* NullName */
/* AML_DEBUGPRINT("<null>"); */
dp++;
} else if (dp[0] == 0x2e) { /* DualNamePrefix */
aml_print_nameseg(dp + 1);
AML_DEBUGPRINT("%c", '.');
aml_print_nameseg(dp + 5);
} else if (dp[0] == 0x2f) { /* MultiNamePrefix */
segcount = dp[1];
for (i = 0, dp += 2; i < segcount; i++, dp += 4) {
if (i > 0) {
AML_DEBUGPRINT("%c", '.');
}
aml_print_nameseg(dp);
}
} else /* NameSeg */
aml_print_nameseg(dp);
}
int
aml_print_curname(struct aml_name *name)
{
struct aml_name *root;
root = aml_get_rootname();
if (name == root) {
AML_DEBUGPRINT("\\");
return (0);
} else {
aml_print_curname(name->parent);
}
aml_print_nameseg(name->name);
AML_DEBUGPRINT(".");
return (0);
}
void
aml_print_indent(int indent)
{
int i;
for (i = 0; i < indent; i++)
AML_DEBUGPRINT(" ");
}
void
aml_showobject(union aml_object * obj)
{
int debug;
int i;
if (obj == NULL) {
printf("NO object\n");
return;
}
debug = aml_debug;
aml_debug = 1;
switch (obj->type) {
case aml_t_num:
printf("Num:0x%x\n", obj->num.number);
break;
case aml_t_processor:
printf("Processor:No %d,Port 0x%x length 0x%x\n",
obj->proc.id, obj->proc.addr, obj->proc.len);
break;
case aml_t_mutex:
printf("Mutex:Level %d\n", obj->mutex.level);
break;
case aml_t_powerres:
printf("PowerResource:Level %d Order %d\n",
obj->pres.level, obj->pres.order);
break;
case aml_t_opregion:
printf("OprationRegion:Busspace%d, Offset 0x%x Length 0x%x\n",
obj->opregion.space, obj->opregion.offset,
obj->opregion.length);
break;
case aml_t_field:
printf("Fieldelement:flag 0x%x offset 0x%x len 0x%x {",
obj->field.flags, obj->field.bitoffset,
obj->field.bitlen);
switch (obj->field.f.ftype) {
case f_t_field:
aml_print_namestring(obj->field.f.fld.regname);
break;
case f_t_index:
aml_print_namestring(obj->field.f.ifld.indexname);
printf(" ");
aml_print_namestring(obj->field.f.ifld.dataname);
break;
case f_t_bank:
aml_print_namestring(obj->field.f.bfld.regname);
printf(" ");
aml_print_namestring(obj->field.f.bfld.bankname);
printf("0x%x", obj->field.f.bfld.bankvalue);
break;
}
printf("}\n");
break;
case aml_t_method:
printf("Method: Arg %d From %p To %p\n", obj->meth.argnum,
obj->meth.from, obj->meth.to);
break;
case aml_t_buffer:
printf("Buffer: size:0x%x Data %p\n", obj->buffer.size,
obj->buffer.data);
break;
case aml_t_device:
printf("Device\n");
break;
case aml_t_bufferfield:
printf("Bufferfield:offset 0x%x len 0x%x Origin %p\n",
obj->bfld.bitoffset, obj->bfld.bitlen, obj->bfld.origin);
break;
case aml_t_string:
printf("String:%s\n", obj->str.string);
break;
case aml_t_package:
printf("Package:elements %d \n", obj->package.elements);
for (i = 0; i < obj->package.elements; i++) {
if (obj->package.objects[i] == NULL) {
break;
}
if (obj->package.objects[i]->type < 0) {
continue;
}
printf(" ");
aml_showobject(obj->package.objects[i]);
}
break;
case aml_t_therm:
printf("Thermalzone\n");
break;
case aml_t_event:
printf("Event\n");
break;
case aml_t_ddbhandle:
printf("DDBHANDLE\n");
break;
case aml_t_objref:
if (obj->objref.alias == 1) {
printf("Alias");
} else {
printf("Object reference");
if (obj->objref.offset >= 0) {
printf(" (offset 0x%x)", obj->objref.offset);
}
}
printf(" of ");
aml_showobject(obj->objref.ref);
break;
default:
printf("UNK ID=%d\n", obj->type);
}
aml_debug = debug;
}
void
aml_showtree(struct aml_name * aname, int lev)
{
int i;
struct aml_name *ptr;
char name[5];
for (i = 0; i < lev; i++) {
printf(" ");
}
strncpy(name, aname->name, 4);
name[4] = 0;
printf("%s ", name);
if (aname->property != NULL) {
aml_showobject(aname->property);
} else {
printf("\n");
}
for (ptr = aname->child; ptr; ptr = ptr->brother)
aml_showtree(ptr, lev + 1);
}
/*
* BufferField I/O
*/
#define AML_BUFFER_INPUT 0
#define AML_BUFFER_OUTPUT 1
static int aml_bufferfield_io(int io, u_int32_t *valuep,
u_int8_t *origin, u_int32_t bitoffset,
u_int32_t bitlen);
static int
aml_bufferfield_io(int io, u_int32_t *valuep, u_int8_t *origin,
u_int32_t bitoffset, u_int32_t bitlen)
{
u_int8_t val, tmp, masklow, maskhigh;
u_int8_t offsetlow, offsethigh;
u_int8_t *addr;
int value = *valuep, readval;
int i;
u_int32_t byteoffset, bytelen;
masklow = maskhigh = 0xff;
val = readval = 0;
value = *valuep;
byteoffset = bitoffset / 8;
bytelen = bitlen / 8 + ((bitlen % 8) ? 1 : 0);
addr = origin + byteoffset;
offsetlow = bitoffset % 8;
if (bytelen > 1) {
offsethigh = (bitlen - (8 - offsetlow)) % 8;
} else {
offsethigh = 0;
}
if (offsetlow) {
masklow = (~((1 << bitlen) - 1) << offsetlow) | ~(0xff << offsetlow);
AML_DEBUGPRINT("\t[offsetlow = 0x%x, masklow = 0x%x, ~masklow = 0x%x]\n",
offsetlow, masklow, ~masklow & 0xff);
}
if (offsethigh) {
maskhigh = 0xff << offsethigh;
AML_DEBUGPRINT("\t[offsethigh = 0x%x, maskhigh = 0x%x, ~maskhigh = 0x%x]\n",
offsethigh, maskhigh, ~maskhigh & 0xff);
}
for (i = bytelen; i > 0; i--, addr++) {
val = *addr;
AML_DEBUGPRINT("\t[bufferfield:0x%02x@%p]", val, addr);
switch (io) {
case AML_BUFFER_INPUT:
tmp = val;
/* the lowest byte? */
if (i == bytelen) {
if (offsetlow) {
readval = tmp & ~masklow;
} else {
readval = tmp;
}
} else {
if (i == 1 && offsethigh) {
tmp = tmp & ~maskhigh;
}
readval = (tmp << (8 * (bytelen - i))) | readval;
}
AML_DEBUGPRINT("\n");
/* goto to next byte... */
if (i > 1) {
continue;
}
/* final adjustment before finishing region access */
if (offsetlow) {
readval = readval >> offsetlow;
}
AML_DEBUGPRINT("[read(bufferfield, %p)&mask:0x%x]\n",
addr, readval);
*valuep = readval;
break;
case AML_BUFFER_OUTPUT:
tmp = value & 0xff;
/* the lowest byte? */
if (i == bytelen) {
if (offsetlow) {
tmp = (val & masklow) | tmp << offsetlow;
}
value = value >> (8 - offsetlow);
} else {
if (i == 1 && offsethigh) {
tmp = (val & maskhigh) | tmp;
}
value = value >> 8;
}
AML_DEBUGPRINT("->[bufferfield:0x%02x@%p]\n",
tmp, addr);
*addr = tmp;
}
}
return (0);
}
u_int32_t
aml_bufferfield_read(u_int8_t *origin, u_int32_t bitoffset,
u_int32_t bitlen)
{
int value;
value = 0;
aml_bufferfield_io(AML_BUFFER_INPUT, &value, origin,
bitoffset, bitlen);
return (value);
}
int
aml_bufferfield_write(u_int32_t value, u_int8_t *origin,
u_int32_t bitoffset, u_int32_t bitlen)
{
int status;
status = aml_bufferfield_io(AML_BUFFER_OUTPUT, &value,
origin, bitoffset, bitlen);
return (status);
}
|