shithub: util

Download patch

ref: 9de25964fd0da3631675116bd12e700d9779eb4e
parent: f167469be26610df3c32e8c6c6f40720dc067a7b
author: glenda <glenda@kingship>
date: Thu Dec 18 03:29:45 EST 2025

mcp7940x ctl file cleanup

--- a/mcp7940x.c
+++ b/mcp7940x.c
@@ -196,8 +196,10 @@
 	return fd;
 }
 
+
+
 uchar
-rtcctl(int writemode, int crstrim)
+rtccrstrim(int writemode, int crstrim)
 {
 	int fd;
 	uchar reg;
@@ -246,7 +248,7 @@
 	int offs;
 	int len;
 	int i;
-	uchar ctl;
+	int crs;
 
 	clk[0] = 0;
 
@@ -278,26 +280,6 @@
 		snprint(buf, 256, "%11lud ", t);
 		readstr(r, buf);
 		respond(r, nil);
-	} else if ((intptr)(r->fid->file->aux) == 2) {
-		fd = datafd();
-
-		clk[0] = OscTrim;
-		write(fd, clk, 1);
-		read(fd, clk, 1);
-		close(fd);
-
-		sign = 1;
-		if ((clk[0] & 0x80) == 0) {
-			sign = -1;
-		}
-
-		trim = clk[0] & 0x7F;
-		trim <<= 1;
-		trim *= sign;
-
-		snprint(buf, 256, "%11d ", trim);
-		readstr(r, buf);
-		respond(r, nil);
 	} else if ((intptr)(r->fid->file->aux) == 3) {
 		offs = r->ifcall.offset;
 
@@ -328,10 +310,26 @@
 
 		readbuf(r, buf, len);
 		respond(r, nil);
-	} else if ((intptr)(r->fid->file->aux) == 4) {
-		ctl = rtcctl(0, 0);
+	} else if ((intptr)(r->fid->file->aux) == 2) {
+		crs = rtccrstrim(0, 0);
 
-		snprint(buf, 256, "crstrim %d\n" , ctl & CrsTrim? 1: 0);
+		fd = datafd();
+
+		clk[0] = OscTrim;
+		write(fd, clk, 1);
+		read(fd, clk, 1);
+		close(fd);
+
+		sign = 1;
+		if ((clk[0] & 0x80) == 0) {
+			sign = -1;
+		}
+
+		trim = clk[0] & 0x7F;
+		trim <<= 1;
+		trim *= sign;
+
+		snprint(buf, 256, "crstrim %d\ntrimval %d\n" , crs & CrsTrim? 1: 0, trim);
 		len = strlen(buf);
 
 		readbuf(r, buf, len);
@@ -389,43 +387,6 @@
 		close(fd);
 
 		respond(r, nil);
-	} else if ((intptr)(r->fid->file->aux) == 2) {
-		p = r->ifcall.data;
-		ep = p + r->ifcall.count;
-
-		sign = 1;
-		while(p < ep) {
-			if (*p == '-')
-				sign = -1;
-			else if (*p >= '0' && *p <= '9')
-				break;
-			p++;
-		}
-
-		trim = strtol(p, 0, 0);
-		if (trim < 0 || trim > 254) {
-			respond(r, "invalid trim value");
-			return;
-		}
-
-		trim >>= 1;
-
-		reg[0] = OscTrim;
-		reg[1] = 0;
-		if (sign == 1)
-			reg[1] = 0x80;
-		reg[1] |= (uchar)trim;
-
-		fd = datafd();
-
-		if (write(fd, reg, 2) != 2)
-			sysfatal("write: %r");
-
-		close(fd);
-
-		r->ofcall.count = r->ifcall.count;
-
-		respond(r, nil);
 	} else if ((intptr)(r->fid->file->aux) == 3) {
 		p = r->ifcall.data;
 		len = r->ifcall.count;
@@ -449,7 +410,7 @@
 		r->ofcall.count = len;
 
 		respond(r, nil);
-	} else if ((intptr)(r->fid->file->aux) == 4) {
+	} else if ((intptr)(r->fid->file->aux) == 2) {
 		p = r->ifcall.data;
 
 		if (cistrncmp(p, "crstrim ", 8) == 0) {
@@ -456,10 +417,42 @@
 			p += 8;
 			i = atoi(p);
 
-			rtcctl(1, i);
+			rtccrstrim(1, i);
 
 			respond(r, nil);
 			return;
+		} else if (cistrncmp(p, "trimval ", 8) == 0) {
+			p += 8;
+			sign = 1;
+			trim = atoi(p);
+
+			if (trim < 0) {
+				sign = -1;
+				trim = -trim;
+			}
+
+			if (trim < 0 || trim > 254) {
+				respond(r, "invalid trim value");
+				return;
+			}
+
+			trim >>= 1;
+
+			reg[0] = OscTrim;
+			reg[1] = 0;
+			if (sign == 1)
+				reg[1] = 0x80;
+			reg[1] |= (uchar)trim;
+
+			fd = datafd();
+
+			if (write(fd, reg, 2) != 2)
+				sysfatal("write: %r");
+
+			close(fd);
+
+			respond(r, nil);
+			return;
 		}
 
 		respond(r, "invalid ctl message");
@@ -491,8 +484,7 @@
 
 	fs.tree = alloctree("mcp7940x", "mcp7940x", DMDIR|0555, nil);
 	createfile(fs.tree->root, "rtc", "mcp7940x", 0666, (void*)1);
-	createfile(fs.tree->root, "rtctrim", "mcp7940x", 0666, (void*)2);
+	createfile(fs.tree->root, "rtcctl", "mcp7940x", 0666, (void*)2);
 	createfile(fs.tree->root, "rtcsram", "mcp7940x", 0666, (void*)3);
-	createfile(fs.tree->root, "rtcctl", "mcp7940x", 0666, (void*)4);
 	postmountsrv(&fs, "mcp7940x", "/dev", MBEFORE);
 }
--