summaryrefslogtreecommitdiff
path: root/sys/dev/acpi/dsdt.c
blob: 1f7ab5a973933bbaf75a60685a570e3631e1ad03 (plain)
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/* $OpenBSD: dsdt.c,v 1.5 2005/12/07 22:34:20 jordan Exp $ */
/*
 * Copyright (c) 2005 Jordan Hargrave <jordan@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>

#include <machine/bus.h>

#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>
#include <dev/acpi/amltypes.h>

struct dsdt_softc {
	struct device		sc_dev;

	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_ioh;
};

int	dsdtmatch(struct device *, void *, void *);
void	dsdtattach(struct device *, struct device *, void *);
int	dsdt_parse_aml(struct dsdt_softc *, u_int8_t *, u_int32_t);

struct cfattach dsdt_ca = {
	sizeof(struct dsdt_softc), dsdtmatch, dsdtattach
};

struct cfdriver dsdt_cd = {
	NULL, "dsdt", DV_DULL
};

#ifdef AML_DEBUG
int amldebug=3;
#define dprintf(x...)     do { if (amldebug) printf(x); } while(0)
#define dnprintf(n,x...)  do { if (amldebug > (n)) printf(x); } while(0)
#else
#define dprintf(x...)
#define dnprintf(n,x...)
#endif

int
dsdtmatch(struct device *parent, void *match, void *aux)
{
	struct acpi_attach_args		*aaa = aux;
	struct acpi_table_header	*hdr;

	/* if we do not have a table, it is not us */
	if (aaa->aaa_table == NULL)
		return (0);

	/* if it is an DSDT table, we can attach */
	hdr = (struct acpi_table_header *)aaa->aaa_table;
	if (memcmp(hdr->signature, DSDT_SIG, sizeof(DSDT_SIG) - 1) == 0)
		return (1);

	/* Attach SSDT tables */
	if (memcmp(hdr->signature, SSDT_SIG, sizeof(SSDT_SIG) - 1) == 0)
		return (1);

	return (0);
}

void
dsdtattach(struct device *parent, struct device *self, void *aux)
{
	struct acpi_attach_args	*aa = aux;
	struct dsdt_softc	*sc = (struct dsdt_softc *) self;
	struct acpi_dsdt	*dsdt = (struct acpi_dsdt *)aa->aaa_table;

	dsdt_parse_aml(sc, dsdt->aml, dsdt->hdr_length - sizeof(dsdt->hdr));
}

struct aml_node
{
	struct aml_node *parent;
	struct aml_node *child;
	struct aml_node *sibling;

	u_int16_t   opcode;
	u_int8_t   *start;
	u_int8_t   *end;
	u_int8_t    flag;
	const char *name;
};

struct aml_optable
{
	u_int16_t    opcode;
	const char  *mnem;
	const char  *args;
};

int aml_isnamedop(uint16_t);u_int8_t *aml_decodelength(u_int8_t *, int *);
u_int8_t *aml_decodename(u_int8_t *, const char **);
u_int8_t *aml_getopcode(u_int8_t *, u_int16_t *);
u_int8_t *aml_parseargs(struct dsdt_softc *, struct aml_node *, u_int8_t *, const char *);
u_int8_t *aml_parse_object(struct dsdt_softc *, struct aml_node *, u_int8_t *);
int aml_lsb(uint32_t val);
int aml_msb(uint32_t val);
void aml_addchildnode(struct aml_node *, struct aml_node *);

long aml_evalmath(u_int16_t, long, long);
int  aml_evallogical(u_int16_t, long, long);

#if 0
void aml_eval_opregion(struct dsdt_softc *sc, struct aml_value *result, 
		       struct aml_node *node)
{
	result->type = AML_TYPE_GAS;
	result->v_gas.address_space_id = node->flag;
	result->v_gas.register_bit_width = ..;
	result->v_gas.register_bit_offset = ..;
	result->v_gas.access_size = aml_eval_integer(result, node->child[0]);
	result->v_gas.address = aml_eval_integer(result, node->child[1]);
}
#endif

/* Decode AML Package length
 * Upper two bits of first byte denote length
 *   0x00 = length is in lower 6 bits
 *   0x40 = length is lower 4 bits + 1 byte
 *   0x80 = length is lower 4 bits + 2 bytes
 *   0xC0 = length is lower 4 bits + 3 bytes
 */
u_int8_t *
aml_decodelength(u_int8_t *pos, int *length)
{
	u_int8_t lcode;

	lcode = *(pos++);

	*length = (lcode & 0xF);
	switch(lcode >> 6) {
	case 0x01:
		*length += (pos[0] << 4L);
		return pos+1;
	case 0x02:
		*length += (pos[0] << 4L) + (pos[1] << 12L);
		return pos+2;
	case 0x03:
		*length += (pos[0] << 4L) + (pos[1] << 12L) + (pos[2] << 20L);
		return pos+3;
	default:
		*length = (lcode & 0x3F);
		return pos;
	}
}

u_int8_t *
aml_decodename(u_int8_t *pos, const char **ref)
{
	int count, pfxlen, idx;
	char *name;
	u_int8_t *base;

	base = pos;
	if (*pos == AMLOP_ROOTCHAR) {
		pos++;
	}
	while (*pos == AMLOP_PARENTPREFIX) {
		pos++;
	}
	pfxlen = pos - base;

	count = 0;
	if (*pos == AMLOP_MULTINAMEPREFIX) {
		count = *(++pos);
		pos++;
	}
	else if (*pos == AMLOP_DUALNAMEPREFIX) {
		count = 2;
		pos++;
	}
	else if (*pos == AMLOP_NAMECHAR || (*pos >= 'A' && *pos <= 'Z')) {
		count = 1;
	}

	name = malloc(pfxlen + count * 5, M_DEVBUF, M_WAITOK);
	if (name != NULL) {
		if (pfxlen > 0) {
			memcpy(name, base, pfxlen);
		}
		/* Copy name segments in chunks of 4 bytes */
		base = name+pfxlen;
		for(idx=0; idx<count; idx++) {
			if (idx) *(base++) = '.';
			memcpy(base, pos, 4);
			pos += 4;
			base += 4;
		}
		*base = 0;
		dprintf("acpi_name: %s\n", name);
		if (ref != NULL) {
			*ref = name;
		}
		else {
			free(name, M_DEVBUF);
		}
	}

	return pos;
}

/* Is this opcode an encoded name? */
int
aml_isnamedop(uint16_t opcode)
{
	switch (opcode) {
	case AMLOP_ROOTCHAR:
        case AMLOP_PARENTPREFIX:
        case AMLOP_MULTINAMEPREFIX:
        case AMLOP_DUALNAMEPREFIX:
        case AMLOP_NAMECHAR:
		return (1);
	}

	if (opcode >= 'A' && opcode <= 'Z')
		return (1);

	return (0);
}

/* Calculate LSB */
int aml_lsb(uint32_t val)
{
	int n = 31;

	if (!val) return -1;
	if (val & 0x0000FFFF) { val <<= 16; n -= 16; };
	if (val & 0x00FF0000) { val <<= 8;  n -= 8; };
	if (val & 0x0F000000) { val <<= 4;  n -= 4; };
	if (val & 0x30000000) { val <<= 2;  n -= 2; };
	return (val & 0x40000000) ? n-1 : n;
}

/* Calculate MSB */
int aml_msb(uint32_t val)
{
	int n=0;

	if (!val) return -1;
	if (val & 0xFFFF0000) { val >>= 16; n += 16; };
	if (val & 0x0000FF00) { val >>= 8;  n += 8; };
	if (val & 0x000000F0) { val >>= 4;  n += 4; };
	if (val & 0x0000000C) { val >>= 2;  n += 2; };
	return (val & 0x00000002) ? n+1 : n;
}

/* Evaluate Math operands */
long
aml_evalmath(u_int16_t opcode, long lhs, long rhs)
{
	switch (opcode) {
	case AMLOP_ADD:
		return (lhs + rhs);
	case AMLOP_SUBTRACT:
		return (lhs - rhs);
	case AMLOP_MULTIPLY:
		return (lhs * rhs);
	case AMLOP_DIVIDE:
		return (lhs / rhs);
	case AMLOP_MOD:
		return (lhs % rhs);
	case AMLOP_SHL:
		return (lhs << rhs);
	case AMLOP_SHR:
		return (lhs >> rhs);
	case AMLOP_AND:
		return (lhs & rhs);
	case AMLOP_NAND:
		return ~(lhs & rhs);
	case AMLOP_OR:
		return (lhs | rhs); 
	case AMLOP_NOR:
		return ~(lhs | rhs); 
	case AMLOP_XOR:
		return (lhs ^ rhs);
	case AMLOP_INCREMENT:
		return (lhs + 1);
	case AMLOP_DECREMENT:
		return (lhs - 1);
	case AMLOP_FINDSETLEFTBIT:
		return aml_msb(lhs);
	case AMLOP_FINDSETRIGHTBIT:
		return aml_lsb(lhs);
	case AMLOP_NOT:
		return ~(lhs);
	}

	return (0);
}

/* Evaluate logical test operands */
int
aml_evallogical(u_int16_t opcode, long lhs, long rhs)
{
	switch(opcode) {
	case AMLOP_LAND:
		return (lhs && rhs);
	case AMLOP_LOR:
		return (lhs || rhs);
	case AMLOP_LNOT:
		return (!lhs);
	case AMLOP_LNOTEQUAL:
		return (lhs != rhs);
	case AMLOP_LLESSEQUAL:
		return (lhs <= rhs);
	case AMLOP_LGREATEREQUAL:
		return (lhs >= rhs);
	case AMLOP_LEQUAL:
		return (lhs == rhs);
	case AMLOP_LGREATER:
		return (lhs > rhs);
	case AMLOP_LLESS:
		return (lhs < rhs);
	}
	return 0;
}

/* Extract opcode from AML bytestream 
 *
 * Some opcodes are multibyte
 * Strings can also be embedded within the stream
 */
u_int8_t *
aml_getopcode(u_int8_t *pos, uint16_t *opcode)
{
	u_int16_t twocode;

	/* Check for encoded name */
	if (aml_isnamedop(*pos)) {
		*opcode = AMLOP_NAMECHAR;
		return pos;
	}

	*opcode = *(pos++);
	twocode = (*opcode << 8L) + *pos;

	/* Check multi-byte opcodes */
	if (twocode == AMLOP_LNOTEQUAL ||
	    twocode == AMLOP_LLESSEQUAL ||
	    twocode == AMLOP_LGREATEREQUAL ||
	    *opcode == AMLOP_EXTPREFIX) {
		pos++;
		*opcode = twocode;
	}

	return pos;
}

struct aml_optable aml_table[] = {
	/* Simple types */
	{ AMLOP_ONES,             "Ones",            "",   },
	{ AMLOP_ZERO,             "Zero",            "", },
	{ AMLOP_ONE,              "One",             "",  },
	{ AMLOP_BYTEPREFIX,       "Byte",            "b",  },
	{ AMLOP_WORDPREFIX,       "Word",            "w",  },
	{ AMLOP_DWORDPREFIX,      "DWord",           "d",  },
	{ AMLOP_QWORDPREFIX,      "QWord",           "q",  },
	{ AMLOP_REVISION,         "Revision",        "",   },
	{ AMLOP_STRINGPREFIX,     "String",          "s",  },
	{ AMLOP_BUFFER,           "Buffer",          "piB", },

	/* Simple objects */
	{ AMLOP_DEBUG,            "DebugOp",         "",    },
	{ AMLOP_LOCAL0,           "Local0",          "",    },
	{ AMLOP_LOCAL1,           "Local1",          "",    },
	{ AMLOP_LOCAL2,           "Local2",          "",    },
	{ AMLOP_LOCAL3,           "Local3",          "",    },
	{ AMLOP_LOCAL4,           "Local4",          "",    },
	{ AMLOP_LOCAL5,           "Local5",          "",    },
	{ AMLOP_LOCAL6,           "Local6",          "",    },
	{ AMLOP_LOCAL7,           "Local7",          "",    },
	{ AMLOP_ARG0,             "Arg0",            "",    },
	{ AMLOP_ARG1,             "Arg1",            "",    },
	{ AMLOP_ARG2,             "Arg2",            "",    },
	{ AMLOP_ARG3,             "Arg3",            "",    },
	{ AMLOP_ARG4,             "Arg4",            "",    },
	{ AMLOP_ARG5,             "Arg5",            "",    },
	{ AMLOP_ARG6,             "Arg6",            "",    },

	/* Control flow */
	{ AMLOP_IF,               "If",              "piT",  },
	{ AMLOP_ELSE,             "Else",            "pT",   },
	{ AMLOP_WHILE,            "While",           "ptT",  },
	{ AMLOP_BREAK,            "Break",           "",     },
	{ AMLOP_CONTINUE,         "Continue",        "",     },
	{ AMLOP_RETURN,           "Return",          "t",     },

	{ AMLOP_FATAL,            "Fatal",           "bdi", },
	{ AMLOP_NOP,              "Nop",             "",    },
	{ AMLOP_BREAKPOINT,       "BreakPoint",      "",    },

	/* Arithmetic operations */
	{ AMLOP_INCREMENT,        "Increment",       "S",     },
	{ AMLOP_DECREMENT,        "Decrement",       "S",     },
	{ AMLOP_ADD,              "Add",             "iir",   },
	{ AMLOP_SUBTRACT,         "Subtract",        "iir",   },
	{ AMLOP_MULTIPLY,         "Multiply",        "iir",   },
	{ AMLOP_DIVIDE,           "Divide",          "iirr",  },
	{ AMLOP_SHL,              "ShiftLeft",       "iir",   },
	{ AMLOP_SHR,              "ShiftRight",      "iir",   },
	{ AMLOP_AND,              "And",             "iir",   },
	{ AMLOP_NAND,             "Nand",            "iir",   },
	{ AMLOP_OR,               "Or",              "iir",   },
	{ AMLOP_NOR,              "Nor",             "iir",   },
	{ AMLOP_XOR,              "Xor",             "iir",   },
	{ AMLOP_NOT,              "Not",             "ir",    },
	{ AMLOP_MOD,              "Mod",             "iir",   },
	{ AMLOP_FINDSETLEFTBIT,   "FindSetLeftBit",  "ir",    },
	{ AMLOP_FINDSETRIGHTBIT,  "FindSetRightBit", "ir",    },

	/* Logical test operations */
	{ AMLOP_LAND,             "LAnd",            "ii",    },
	{ AMLOP_LOR,              "LOr",             "ii",    },
	{ AMLOP_LNOT,             "LNot",            "i",     },
	{ AMLOP_LNOTEQUAL,        "LNotEqual",       "tt",    },
	{ AMLOP_LLESSEQUAL,       "LLessEqual",      "tt",    },
	{ AMLOP_LGREATEREQUAL,    "LGreaterEqual",   "tt",    },
	{ AMLOP_LEQUAL,           "LEqual",          "tt",    },
	{ AMLOP_LGREATER,         "LGreater",        "tt",    },
	{ AMLOP_LLESS,            "LLess",           "tt",    },

	/* Named objects */
	{ AMLOP_NAME,             "Name",            "No",  },
	{ AMLOP_SCOPE,            "Scope",           "pNT" },
	{ AMLOP_DATAREGION,       "DataRegion",      "Nttt" },
	{ AMLOP_OPREGION,         "OpRegion",        "Nfii" },
	{ AMLOP_DEVICE,           "Device",          "pNO" },
	{ AMLOP_POWERRSRC,        "Power Resource",  "pNbwO" },
	{ AMLOP_THERMALZONE,      "ThermalZone",     "pNT" },
	{ AMLOP_METHOD,           "Method",          "pNfT",  },
	{ AMLOP_PROCESSOR,        "Processor",       "pNbdbO", },
	{ AMLOP_FIELD,            "Field",           "pNfF" },
	{ AMLOP_INDEXFIELD,       "IndexField",      "pNnbF" },
	{ AMLOP_BANKFIELD,        "BankField",       "pNnibF" },
	{ AMLOP_MUTEX,            "Mutex",           "Nf",  },
	{ AMLOP_EVENT,            "Event",           "N",   },
	{ AMLOP_ALIAS,            "Alias",           "Nn",  },

	/* Field operations */
	{ AMLOP_CREATEFIELD,      "CreateField",     "tiiN",   },
	{ AMLOP_CREATEQWORDFIELD, "CreateQWordField","tiN",    },
	{ AMLOP_CREATEDWORDFIELD, "CreateDWordField","tiN",    },
	{ AMLOP_CREATEWORDFIELD,  "CreateWordField", "tiN",    },
	{ AMLOP_CREATEBYTEFIELD,  "CreateByteField", "tiN",    },
	{ AMLOP_CREATEBITFIELD,   "CreateBitField",  "tiN",    },

	/* Conversion operations */
	{ AMLOP_TOINTEGER,        "ToInteger",       "tr",     },
	{ AMLOP_TOBUFFER,         "ToBuffer",        "tr",     },
	{ AMLOP_TODECSTRING,      "ToDecString",     "ir",     },
	{ AMLOP_TOHEXSTRING,      "ToHexString",     "ir",     }, 
	{ AMLOP_TOSTRING,         "ToString",        "t",      },
	{ AMLOP_FROMBCD,          "FromBCD",         "ir",     },
	{ AMLOP_TOBCD,            "ToBCD",           "ir",     },
	{ AMLOP_MID,              "Mid",             "tiir",   },

	/* Mutex/Signal operations */
	{ AMLOP_ACQUIRE,          "Acquire",         "Sw",     },
	{ AMLOP_RELEASE,          "Release",         "S",      },
	{ AMLOP_SIGNAL,           "Signal",          "S",      },
	{ AMLOP_WAIT,             "Wait",            "Si",     },
	{ AMLOP_RESET,            "Reset",           "S",      },
 
	{ AMLOP_INDEX,            "Index",           "ttr",    },
	{ AMLOP_PACKAGE,          "Package",         "pfT",    },
	{ AMLOP_VARPACKAGE,       "VarPackage",      "piT",    },
	{ AMLOP_DEREFOF,          "DerefOf",         "t",      },
	{ AMLOP_REFOF,            "RefOf",           "S",      },
	{ AMLOP_CONDREFOF,        "CondRef",         "SS",     },

	{ AMLOP_LOADTABLE,        "LoadTable",       "tttttt" },
	{ AMLOP_STALL,            "Stall",           "i",      },
	{ AMLOP_SLEEP,            "Sleep",           "i",      },
	{ AMLOP_LOAD,             "Load",            "NS" },
	{ AMLOP_UNLOAD,           "Unload",          "S" }, 
	{ AMLOP_STORE,            "Store",           "tS",     },
	{ AMLOP_CONCAT,           "Concat",          "ttr" },
	{ AMLOP_CONCATRES,        "ConcatRes",       "ttr" },
	{ AMLOP_NOTIFY,           "Notify",          "Si" },
	{ AMLOP_SIZEOF,           "Sizeof",          "S",      },
	{ AMLOP_MATCH,            "Match",           "tbibii", },
	{ AMLOP_OBJECTTYPE,       "ObjectType",      "S", },
	{ AMLOP_COPYOBJECT,       "CopyObject",      "tn" },
	{ 0xFFFF }
};

u_int8_t *
aml_parseargs(struct dsdt_softc *sc, struct aml_node *node, u_int8_t *pos, const char *arg)
{
	int len;
	u_int8_t *nxtpos;

	nxtpos = pos;
	while (*arg) {
		switch (*arg) {
		case AML_ARG_FLAG:
			dprintf("flag: %x\n", *(u_int8_t *)pos);
			node->flag = *(u_int8_t *)pos;
			nxtpos = pos+1;
			break;
		case AML_ARG_BYTE:
			dprintf("byte: %x\n", *(u_int8_t *)pos);
			nxtpos = pos+1;
			break;
		case AML_ARG_WORD:
			dprintf("word: %x\n", *(u_int16_t *)pos);
			nxtpos = pos+2;
			break;
                case AML_ARG_DWORD:
			dprintf("dword: %x\n", *(u_int32_t *)pos);
			nxtpos = pos+4;
			break;
		case AML_ARG_QWORD:
			dprintf("qword: %x\n", *(u_int32_t *)pos);
			nxtpos = pos+8;
			break;
		case AML_ARG_FIELDLIST:
			dprintf("fieldlist\n");
			nxtpos = node->end;
			break;
		case AML_ARG_BYTELIST:
			dprintf("bytelist\n");
			nxtpos = node->end;
			break;
		case AML_ARG_STRING:
			dprintf("string: %s\n", pos);
			len = strlen((const char *)pos);
			nxtpos = pos + len + 1;
			break;
		case AML_ARG_NAMESTRING:
			nxtpos = aml_decodename(pos, NULL);
			break;
		case AML_ARG_OBJLEN:
			nxtpos = aml_decodelength(pos, &len);
			node->end = pos + len;
			break;
		case AML_ARG_INTEGER:
		case AML_ARG_DATAOBJ:
		case AML_ARG_TERMOBJ:
		case AML_ARG_RESULT:
		case AML_ARG_SUPERNAME:
			nxtpos = aml_parse_object(sc, node, pos);
			break;
		case AML_ARG_TERMOBJLIST:
		case AML_ARG_DATAOBJLIST:
			while (nxtpos && nxtpos < node->end) {
				nxtpos = aml_parse_object(sc, node, nxtpos);
			}
			break;
		default:
			printf("Unknown arg: %c\n", *arg);
			break;
		}
		pos = nxtpos;

		arg++;
	}
	return pos;
}

void
aml_addchildnode(struct aml_node *parent, struct aml_node *child)
{
	struct aml_node *psib;

	child->parent = parent;
	child->sibling = NULL;
	for (psib = parent->child; psib; psib = psib->sibling) {
		if (psib->sibling == NULL) {
			psib->sibling = child;
			return;
		}
	}
	parent->child = child;
}

u_int8_t *
aml_parse_object(struct dsdt_softc *sc, struct aml_node *parent, u_int8_t *pos)
{
	struct aml_optable *optab = aml_table;
	u_int8_t  *nxtpos;
	struct aml_node *node;

	node = malloc(sizeof(struct aml_node), M_DEVBUF, M_WAITOK);
	if (node == NULL) 
		return pos;
	memset(node, 0, sizeof(struct aml_node));

	/* Get AML Opcode; if it is an embedded name, extract name */
	node->start = pos;
	nxtpos = aml_getopcode(pos, &node->opcode);
	if (node->opcode == AMLOP_NAMECHAR) {
		return aml_decodename(pos, &node->name);
	}
	while (optab->opcode != 0xFFFF) {
		if  (optab->opcode == node->opcode) {
			dprintf("opcode: %.4x = %s\n", node->opcode, optab->mnem);
			aml_addchildnode(parent, node);

			return aml_parseargs(sc, node, nxtpos, optab->args);
		}
		optab++;
	}
	printf("Invalid AML Opcode : %.4x\n", node->opcode);
	free(node, M_DEVBUF);

	return NULL;
}

int
dsdt_parse_aml(struct dsdt_softc *sc, u_int8_t *start, u_int32_t length)
{
	u_int8_t  *pos, *nxtpos;
	struct aml_node root;

	memset(&root, 0, sizeof(root));
	for (pos = start; pos && pos < start+length; pos=nxtpos) {
		nxtpos = aml_parse_object(sc, &root, pos);
	}
	printf(" : parsed %d AML bytes\n", length);
	return (0);
}