diff mbox series

[RFC,3/3] ptp: Add setting esterror and reading timex structure

Message ID 20240813125602.155827-4-maciek@machnikowski.net (mailing list archive)
State RFC
Headers show
Series ptp: Add esterror support | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 7 this patch: 7
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 3 maintainers not CCed: shuah@kernel.org reibax@gmail.com linux-kselftest@vger.kernel.org
netdev/build_clang success Errors and warnings before: 7 this patch: 7
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 7 this patch: 7
netdev/checkpatch warning WARNING: line length of 96 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Maciek Machnikowski Aug. 13, 2024, 12:56 p.m. UTC
Implement setting the esterror using clock_adjtime for ptp clocks
and reading the clock setting using timex structure and
clock_adjtime

Signed-off-by: Maciek Machnikowski <maciek@machnikowski.net>
---
 tools/testing/selftests/ptp/testptp.c | 39 +++++++++++++++++++++++++--
 1 file changed, 37 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/tools/testing/selftests/ptp/testptp.c b/tools/testing/selftests/ptp/testptp.c
index 011252fe238c..38405803b881 100644
--- a/tools/testing/selftests/ptp/testptp.c
+++ b/tools/testing/selftests/ptp/testptp.c
@@ -117,6 +117,7 @@  static void usage(char *progname)
 {
 	fprintf(stderr,
 		"usage: %s [options]\n"
+		" -a val     adjust the estimated error by 'val' ns\n"
 		" -c         query the ptp clock's capabilities\n"
 		" -d name    device to open\n"
 		" -e val     read 'val' external time stamp events\n"
@@ -140,6 +141,7 @@  static void usage(char *progname)
 		" -H val     set output phase to 'val' nanoseconds (requires -p)\n"
 		" -w val     set output pulse width to 'val' nanoseconds (requires -p)\n"
 		" -P val     enable or disable (val=1|0) the system clock PPS\n"
+		" -r         read clock info in the  timex structure using clock_adjtime\n"
 		" -s         set the ptp clock time from the system time\n"
 		" -S         set the system time from the ptp clock time\n"
 		" -t val     shift the ptp clock time by 'val' seconds\n"
@@ -175,12 +177,14 @@  int main(int argc, char *argv[])
 	int adjns = 0;
 	int adjphase = 0;
 	int capabilities = 0;
+	long esterror = 0;
 	int extts = 0;
 	int flagtest = 0;
 	int gettime = 0;
 	int index = 0;
 	int list_pins = 0;
 	int pct_offset = 0;
+	int readclk = 0;
 	int getextended = 0;
 	int getcross = 0;
 	int n_samples = 0;
@@ -198,8 +202,11 @@  int main(int argc, char *argv[])
 
 	progname = strrchr(argv[0], '/');
 	progname = progname ? 1+progname : argv[0];
-	while (EOF != (c = getopt(argc, argv, "cd:e:f:F:ghH:i:k:lL:n:o:p:P:sSt:T:w:x:Xz"))) {
+	while (EOF != (c = getopt(argc, argv, "a:cd:e:f:F:ghH:i:k:lL:n:o:p:P:rsSt:T:w:x:Xz"))) {
 		switch (c) {
+		case 'a':
+			esterror = atoi(optarg);
+			break;
 		case 'c':
 			capabilities = 1;
 			break;
@@ -250,6 +257,9 @@  int main(int argc, char *argv[])
 		case 'P':
 			pps = atoi(optarg);
 			break;
+		case 'r':
+			readclk = 1;
+			break;
 		case 's':
 			settime = 1;
 			break;
@@ -290,7 +300,6 @@  int main(int argc, char *argv[])
 			return -1;
 		}
 	}
-
 	fd = open(device, O_RDWR);
 	if (fd < 0) {
 		fprintf(stderr, "opening %s: %s\n", device, strerror(errno));
@@ -621,6 +630,32 @@  int main(int argc, char *argv[])
 		}
 	}
 
+	if (esterror) {
+		memset(&tx, 0, sizeof(tx));
+		tx.modes = ADJ_ESTERROR;
+		tx.esterror = esterror;
+		if (clock_adjtime(clkid, &tx))
+			perror("clock_adjtime");
+		else
+			puts("esterror adjustment okay");
+	}
+
+	if (readclk) {
+		struct timex clk_info = {0};
+
+		memset(&tx, 0, sizeof(tx));
+		if (clock_adjtime(clkid, &tx)) {
+			perror("clock_adjtime");
+		} else {
+			printf("clock_adjtime:\n"
+			       "\tstatus %d,\n"
+			       "\toffset %ld,\n"
+			       "\tfreq %ld,\n"
+			       "\testerror %ld\n",
+			       tx.status, tx.offset, tx.freq, tx.esterror);
+		}
+	}
+
 	close(fd);
 	return 0;
 }