code: plan9front

Download patch

ref: 93837692bbaab28e01ea1178d8867047bbcad11d
parent: 0883ed0a61c38898ad926ea67eb0e9386dff4ecd
author: mia soweli <inbox@tachibana-labs.org>
date: Thu May 18 16:13:14 EDT 2023

libaml: tow eisaid inside the environment

--- a/sys/man/2/aml
+++ b/sys/man/2/aml
@@ -1,6 +1,6 @@
 .TH AML 2
 .SH NAME
-amltag, amlval, amlint, amllen, amlnew, amlinit, amlexit, amlload, amlwalk, amleval, amlenum, amltake, amldrop - ACPI machine language interpreter
+amltag, amlval, amlint, amllen, amlnew, amlinit, amlexit, amlload, amlwalk, amleval, amlenum, amltake, amldrop, amleisaid - ACPI machine language interpreter
 .SH SYNOPSIS
 .\" .ta 0.75i 1.5i 2.25i 3i 3.75i 4.5i
 .ta 0.7i +0.7i +0.7i +0.7i +0.7i +0.7i +0.7i
@@ -27,6 +27,8 @@
 void	amltake(void *);
 void	amldrop(void *);
 
+char*	amleisaid(void *);
+
 void*	amlroot;
 int	amldebug;
 uvlong	amlintmask;
@@ -203,6 +205,13 @@
 Objects stay valid as long as they are reachable from
 .IR amlroot .
 .bp
+.TP
+.BI amleisaid( p )
+.I Amleisaid
+returns the string value of an EISA id.
+For strings, it simply returns the string.
+For integers, it decodes the compressed EISA id into it's string representation.
+For any other type, the return value is nil.
 .PP
 The aml library can be linked into userspace programs
 and the kernel which have different means of hardware access
--- a/sys/src/9/pc/archacpi.c
+++ b/sys/src/9/pc/archacpi.c
@@ -274,26 +274,6 @@
 	bus->aintr = ai;
 }
 
-static char*
-eisaid(void *v)
-{
-	static char id[8];
-	ulong b, l;
-	int i;
-
-	if(amltag(v) == 's')
-		return v;
-	b = amlint(v);
-	for(l = 0, i=24; i>=0; i -= 8, b >>= 8)
-		l |= (b & 0xFF) << i;
-	id[7] = 0;
-	for(i=6; i>=3; i--, l >>= 4)
-		id[i] = "0123456789ABCDEF"[l & 0xF];
-	for(i=2; i>=0; i--, l >>= 5)
-		id[i] = '@' + (l & 0x1F);
-	return id;
-}
-
 static int
 pcibusno(void *dot)
 {
@@ -305,7 +285,7 @@
 	id = nil;
 	if((x = amlwalk(dot, "^_HID")) != nil)
 		if((p = amlval(x)) != nil)
-			id = eisaid(p);
+			id = amleisaid(p);
 	if((x = amlwalk(dot, "^_BBN")) == nil)
 		if((x = amlwalk(dot, "^_ADR")) == nil)
 			return -1;
@@ -515,7 +495,7 @@
 	char *id;
 
 	b = nil;
-	id = eisaid(amlval(amlwalk(dot, "^_HID")));
+	id = amleisaid(amlval(amlwalk(dot, "^_HID")));
 	if(id == nil || strcmp(id, "PNP0C09") != 0)
 		return 1;
 	if((x = amlwalk(dot, "^_CRS")) == nil)
--- a/sys/src/cmd/aux/acpi.c
+++ b/sys/src/cmd/aux/acpi.c
@@ -63,32 +63,14 @@
 static Therm therms[16];
 static Bat bats[4];
 
-static char*
-eisaid(void *v)
-{
-	static char id[8];
-	ulong b, l;
-	int i;
-
-	if(amltag(v) == 's')
-		return v;
-	b = amlint(v);
-	for(l = 0, i = 24; i >= 0; i -= 8, b >>= 8)
-		l |= (b & 0xFF) << i;
-	id[7] = 0;
-	for(i = 6; i >= 3; i--, l >>= 4)
-		id[i] = "0123456789ABCDEF"[l & 0xF];
-	for(i = 2; i >= 0; i--, l >>= 5)
-		id[i] = '@' + (l & 0x1F);
-	return id;
-}
-
 static int
 enumec(void *dot, void *)
 {
 	void *p;
 	char *id;
-	id = eisaid(amlval(amlwalk(dot, "^_HID")));
+
+	p = amlval(amlwalk(dot, "^_HID"));
+	id = amleisaid(p);
 	if(id == nil || strcmp(id, "PNP0C09") != 0)
 		return 1;
 	p = amlwalk(dot, "^_REG");
--- a/sys/src/libaml/aml.c
+++ b/sys/src/libaml/aml.c
@@ -2463,3 +2463,38 @@
 	if(p != nil)
 		D2H(p)->mark &= ~2;
 }
+
+char*
+amleisaid(void *v)
+{
+	int i;
+	ulong l, m, n;
+	static char s[8];
+
+	if(amltag(v) == 's')
+		return v;
+
+	if(amltag(v) == 'i') {
+		m = 0;
+		l = amlint(v);
+		for(i = 24; i >= 0; i -= 8) {
+			m |= (l & 0xff) << i;
+			l >>= 8;
+		}
+
+		s[7] = 0;
+		for(i = 6; i >= 3; i--) {
+			n = m & 0xf;
+			s[i] = "0123456789ABCDEF"[n]; m >>= 4;
+		}
+
+		for(i = 2; i >= 0; i--) {
+			n = m & 0x1f;
+			s[i] = '@' + n; m >>= 5;
+		}
+
+		return s;
+	}
+
+	return nil;
+}