Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * PTP 1588 clock support - User space test program
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2010 OMICRON electronics GmbH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #define __SANE_USERSPACE_TYPES__        /* For PPC64, to get LL64 types */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <math.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <sys/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <sys/timex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/ptp_clock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define DEVICE "/dev/ptp0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #ifndef ADJ_SETOFFSET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define ADJ_SETOFFSET 0x0100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #ifndef CLOCK_INVALID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define CLOCK_INVALID -1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define NSEC_PER_SEC 1000000000LL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) /* clock_adjtime is not available in GLIBC < 2.14 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #if !__GLIBC_PREREQ(2, 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #include <sys/syscall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static int clock_adjtime(clockid_t id, struct timex *tx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	return syscall(__NR_clock_adjtime, id, tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static void show_flag_test(int rq_index, unsigned int flags, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	printf("PTP_EXTTS_REQUEST%c flags 0x%08x : (%d) %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	       rq_index ? '1' + rq_index : ' ',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	       flags, err, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	/* sigh, uClibc ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	errno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static void do_flag_test(int fd, unsigned int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct ptp_extts_request extts_request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	unsigned long request[2] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		PTP_EXTTS_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		PTP_EXTTS_REQUEST2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	unsigned int enable_flags[5] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		PTP_ENABLE_FEATURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		PTP_ENABLE_FEATURE | PTP_RISING_EDGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		PTP_ENABLE_FEATURE | PTP_FALLING_EDGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		PTP_ENABLE_FEATURE | PTP_RISING_EDGE | PTP_FALLING_EDGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		PTP_ENABLE_FEATURE | (PTP_EXTTS_VALID_FLAGS + 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	int err, i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	memset(&extts_request, 0, sizeof(extts_request));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	extts_request.index = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		for (j = 0; j < 5; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			extts_request.flags = enable_flags[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			err = ioctl(fd, request[i], &extts_request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			show_flag_test(i, extts_request.flags, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			extts_request.flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			err = ioctl(fd, request[i], &extts_request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static clockid_t get_clockid(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) #define CLOCKFD 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	return (((unsigned int) ~fd) << 3) | CLOCKFD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) static long ppb_to_scaled_ppm(int ppb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	 * The 'freq' field in the 'struct timex' is in parts per
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	 * million, but with a 16 bit binary fractional field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	 * Instead of calculating either one of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	 *    scaled_ppm = (ppb / 1000) << 16  [1]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	 *    scaled_ppm = (ppb << 16) / 1000  [2]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	 * we simply use double precision math, in order to avoid the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	 * truncation in [1] and the possible overflow in [2].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	return (long) (ppb * 65.536);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static int64_t pctns(struct ptp_clock_time *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	return t->sec * 1000000000LL + t->nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static void usage(char *progname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		"usage: %s [options]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		" -c         query the ptp clock's capabilities\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		" -d name    device to open\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		" -e val     read 'val' external time stamp events\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		" -f val     adjust the ptp clock frequency by 'val' ppb\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		" -g         get the ptp clock time\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		" -h         prints this message\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		" -i val     index for event/trigger\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		" -k val     measure the time offset between system and phc clock\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		"            for 'val' times (Maximum 25)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		" -l         list the current pin configuration\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		" -L pin,val configure pin index 'pin' with function 'val'\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		"            the channel index is taken from the '-i' option\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		"            'val' specifies the auxiliary function:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		"            0 - none\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		"            1 - external time stamp\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		"            2 - periodic output\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		" -p val     enable output with a period of 'val' nanoseconds\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		" -H val     set output phase to 'val' nanoseconds (requires -p)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		" -w val     set output pulse width to 'val' nanoseconds (requires -p)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		" -P val     enable or disable (val=1|0) the system clock PPS\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		" -s         set the ptp clock time from the system time\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		" -S         set the system time from the ptp clock time\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		" -t val     shift the ptp clock time by 'val' seconds\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		" -T val     set the ptp clock time to 'val' seconds\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		" -z         test combinations of rising/falling external time stamp flags\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		progname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	struct ptp_clock_caps caps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	struct ptp_extts_event event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct ptp_extts_request extts_request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	struct ptp_perout_request perout_request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	struct ptp_pin_desc desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	struct timespec ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	struct timex tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	struct ptp_clock_time *pct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	struct ptp_sys_offset *sysoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	char *progname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	int c, cnt, fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	char *device = DEVICE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	clockid_t clkid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	int adjfreq = 0x7fffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	int adjtime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	int capabilities = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	int extts = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	int flagtest = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	int gettime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	int index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	int list_pins = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	int pct_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	int n_samples = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	int pin_index = -1, pin_func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	int pps = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	int seconds = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	int settime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	int64_t t1, t2, tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	int64_t interval, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	int64_t perout_phase = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	int64_t pulsewidth = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	int64_t perout = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	progname = strrchr(argv[0], '/');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	progname = progname ? 1+progname : argv[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	while (EOF != (c = getopt(argc, argv, "cd:e:f:ghH:i:k:lL:p:P:sSt:T:w:z"))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		switch (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		case 'c':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			capabilities = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		case 'd':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			device = optarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		case 'e':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			extts = atoi(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		case 'f':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			adjfreq = atoi(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		case 'g':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			gettime = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		case 'H':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			perout_phase = atoll(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		case 'i':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			index = atoi(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		case 'k':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			pct_offset = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			n_samples = atoi(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		case 'l':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			list_pins = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		case 'L':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			cnt = sscanf(optarg, "%d,%d", &pin_index, &pin_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			if (cnt != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 				usage(progname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		case 'p':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 			perout = atoll(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		case 'P':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			pps = atoi(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		case 's':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			settime = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		case 'S':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			settime = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		case 't':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			adjtime = atoi(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		case 'T':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			settime = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			seconds = atoi(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		case 'w':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			pulsewidth = atoi(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		case 'z':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			flagtest = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		case 'h':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			usage(progname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		case '?':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			usage(progname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	fd = open(device, O_RDWR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		fprintf(stderr, "opening %s: %s\n", device, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	clkid = get_clockid(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if (CLOCK_INVALID == clkid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		fprintf(stderr, "failed to read clock id\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	if (capabilities) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			perror("PTP_CLOCK_GETCAPS");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			printf("capabilities:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			       "  %d maximum frequency adjustment (ppb)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			       "  %d programmable alarms\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			       "  %d external time stamp channels\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			       "  %d programmable periodic signals\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			       "  %d pulse per second\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			       "  %d programmable pins\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			       "  %d cross timestamping\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			       "  %d adjust_phase\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 			       caps.max_adj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			       caps.n_alarm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			       caps.n_ext_ts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			       caps.n_per_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			       caps.pps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			       caps.n_pins,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			       caps.cross_timestamping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			       caps.adjust_phase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	if (0x7fffffff != adjfreq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		memset(&tx, 0, sizeof(tx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		tx.modes = ADJ_FREQUENCY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		tx.freq = ppb_to_scaled_ppm(adjfreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		if (clock_adjtime(clkid, &tx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			perror("clock_adjtime");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			puts("frequency adjustment okay");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (adjtime) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		memset(&tx, 0, sizeof(tx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		tx.modes = ADJ_SETOFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		tx.time.tv_sec = adjtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		tx.time.tv_usec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		if (clock_adjtime(clkid, &tx) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			perror("clock_adjtime");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			puts("time shift okay");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	if (gettime) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		if (clock_gettime(clkid, &ts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			perror("clock_gettime");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			printf("clock time: %ld.%09ld or %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			       ts.tv_sec, ts.tv_nsec, ctime(&ts.tv_sec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	if (settime == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		clock_gettime(CLOCK_REALTIME, &ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		if (clock_settime(clkid, &ts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			perror("clock_settime");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			puts("set time okay");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	if (settime == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		clock_gettime(clkid, &ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		if (clock_settime(CLOCK_REALTIME, &ts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			perror("clock_settime");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			puts("set time okay");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	if (settime == 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		ts.tv_sec = seconds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		ts.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		if (clock_settime(clkid, &ts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			perror("clock_settime");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			puts("set time okay");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	if (extts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		memset(&extts_request, 0, sizeof(extts_request));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		extts_request.index = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		extts_request.flags = PTP_ENABLE_FEATURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 			perror("PTP_EXTTS_REQUEST");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			extts = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 			puts("external time stamp request okay");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		for (; extts; extts--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			cnt = read(fd, &event, sizeof(event));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			if (cnt != sizeof(event)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 				perror("read");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 			printf("event index %u at %lld.%09u\n", event.index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			       event.t.sec, event.t.nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			fflush(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		/* Disable the feature again. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		extts_request.flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 			perror("PTP_EXTTS_REQUEST");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	if (flagtest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		do_flag_test(fd, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	if (list_pins) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		int n_pins = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 			perror("PTP_CLOCK_GETCAPS");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 			n_pins = caps.n_pins;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		for (i = 0; i < n_pins; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			desc.index = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 			if (ioctl(fd, PTP_PIN_GETFUNC, &desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 				perror("PTP_PIN_GETFUNC");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 			printf("name %s index %u func %u chan %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 			       desc.name, desc.index, desc.func, desc.chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	if (pulsewidth >= 0 && perout < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		puts("-w can only be specified together with -p");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	if (perout_phase >= 0 && perout < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		puts("-H can only be specified together with -p");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	if (perout >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		if (clock_gettime(clkid, &ts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 			perror("clock_gettime");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		memset(&perout_request, 0, sizeof(perout_request));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		perout_request.index = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		perout_request.period.sec = perout / NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		perout_request.period.nsec = perout % NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		perout_request.flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		if (pulsewidth >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 			perout_request.flags |= PTP_PEROUT_DUTY_CYCLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 			perout_request.on.sec = pulsewidth / NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 			perout_request.on.nsec = pulsewidth % NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		if (perout_phase >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 			perout_request.flags |= PTP_PEROUT_PHASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 			perout_request.phase.sec = perout_phase / NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 			perout_request.phase.nsec = perout_phase % NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 			perout_request.start.sec = ts.tv_sec + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 			perout_request.start.nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		if (ioctl(fd, PTP_PEROUT_REQUEST2, &perout_request)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			perror("PTP_PEROUT_REQUEST");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 			puts("periodic output request okay");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	if (pin_index >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		memset(&desc, 0, sizeof(desc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		desc.index = pin_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		desc.func = pin_func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		desc.chan = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		if (ioctl(fd, PTP_PIN_SETFUNC, &desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 			perror("PTP_PIN_SETFUNC");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 			puts("set pin function okay");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	if (pps != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		int enable = pps ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		if (ioctl(fd, PTP_ENABLE_PPS, enable)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 			perror("PTP_ENABLE_PPS");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 			puts("pps for system time request okay");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	if (pct_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		if (n_samples <= 0 || n_samples > 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 			puts("n_samples should be between 1 and 25");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 			usage(progname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		sysoff = calloc(1, sizeof(*sysoff));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		if (!sysoff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 			perror("calloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		sysoff->n_samples = n_samples;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		if (ioctl(fd, PTP_SYS_OFFSET, sysoff))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 			perror("PTP_SYS_OFFSET");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 			puts("system and phc clock time offset request okay");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		pct = &sysoff->ts[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		for (i = 0; i < sysoff->n_samples; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 			t1 = pctns(pct+2*i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 			tp = pctns(pct+2*i+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 			t2 = pctns(pct+2*i+2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 			interval = t2 - t1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 			offset = (t2 + t1) / 2 - tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			printf("system time: %lld.%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 				(pct+2*i)->sec, (pct+2*i)->nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 			printf("phc    time: %lld.%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 				(pct+2*i+1)->sec, (pct+2*i+1)->nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 			printf("system time: %lld.%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 				(pct+2*i+2)->sec, (pct+2*i+2)->nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 			printf("system/phc clock time offset is %" PRId64 " ns\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 			       "system     clock time delay  is %" PRId64 " ns\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 				offset, interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		free(sysoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }