174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
28dbbf854SShuah Khan /*
38dbbf854SShuah Khan * PTP 1588 clock support - User space test program
48dbbf854SShuah Khan *
58dbbf854SShuah Khan * Copyright (C) 2010 OMICRON electronics GmbH
68dbbf854SShuah Khan */
78dbbf854SShuah Khan #define _GNU_SOURCE
88dbbf854SShuah Khan #define __SANE_USERSPACE_TYPES__ /* For PPC64, to get LL64 types */
98dbbf854SShuah Khan #include <errno.h>
108dbbf854SShuah Khan #include <fcntl.h>
118dbbf854SShuah Khan #include <inttypes.h>
128dbbf854SShuah Khan #include <math.h>
138dbbf854SShuah Khan #include <signal.h>
148dbbf854SShuah Khan #include <stdio.h>
158dbbf854SShuah Khan #include <stdlib.h>
168dbbf854SShuah Khan #include <string.h>
178dbbf854SShuah Khan #include <sys/ioctl.h>
188dbbf854SShuah Khan #include <sys/mman.h>
198dbbf854SShuah Khan #include <sys/stat.h>
208dbbf854SShuah Khan #include <sys/time.h>
218dbbf854SShuah Khan #include <sys/timex.h>
228dbbf854SShuah Khan #include <sys/types.h>
238dbbf854SShuah Khan #include <time.h>
248dbbf854SShuah Khan #include <unistd.h>
258dbbf854SShuah Khan
268dbbf854SShuah Khan #include <linux/ptp_clock.h>
278dbbf854SShuah Khan
288dbbf854SShuah Khan #define DEVICE "/dev/ptp0"
298dbbf854SShuah Khan
308dbbf854SShuah Khan #ifndef ADJ_SETOFFSET
318dbbf854SShuah Khan #define ADJ_SETOFFSET 0x0100
328dbbf854SShuah Khan #endif
338dbbf854SShuah Khan
348dbbf854SShuah Khan #ifndef CLOCK_INVALID
358dbbf854SShuah Khan #define CLOCK_INVALID -1
368dbbf854SShuah Khan #endif
378dbbf854SShuah Khan
384a09a981SVladimir Oltean #define NSEC_PER_SEC 1000000000LL
394a09a981SVladimir Oltean
408dbbf854SShuah Khan /* clock_adjtime is not available in GLIBC < 2.14 */
418dbbf854SShuah Khan #if !__GLIBC_PREREQ(2, 14)
428dbbf854SShuah Khan #include <sys/syscall.h>
clock_adjtime(clockid_t id,struct timex * tx)438dbbf854SShuah Khan static int clock_adjtime(clockid_t id, struct timex *tx)
448dbbf854SShuah Khan {
458dbbf854SShuah Khan return syscall(__NR_clock_adjtime, id, tx);
468dbbf854SShuah Khan }
478dbbf854SShuah Khan #endif
488dbbf854SShuah Khan
show_flag_test(int rq_index,unsigned int flags,int err)496eb54cbbSRichard Cochran static void show_flag_test(int rq_index, unsigned int flags, int err)
506eb54cbbSRichard Cochran {
516eb54cbbSRichard Cochran printf("PTP_EXTTS_REQUEST%c flags 0x%08x : (%d) %s\n",
526eb54cbbSRichard Cochran rq_index ? '1' + rq_index : ' ',
536eb54cbbSRichard Cochran flags, err, strerror(errno));
546eb54cbbSRichard Cochran /* sigh, uClibc ... */
556eb54cbbSRichard Cochran errno = 0;
566eb54cbbSRichard Cochran }
576eb54cbbSRichard Cochran
do_flag_test(int fd,unsigned int index)586eb54cbbSRichard Cochran static void do_flag_test(int fd, unsigned int index)
596eb54cbbSRichard Cochran {
606eb54cbbSRichard Cochran struct ptp_extts_request extts_request;
616eb54cbbSRichard Cochran unsigned long request[2] = {
626eb54cbbSRichard Cochran PTP_EXTTS_REQUEST,
636eb54cbbSRichard Cochran PTP_EXTTS_REQUEST2,
646eb54cbbSRichard Cochran };
656eb54cbbSRichard Cochran unsigned int enable_flags[5] = {
666eb54cbbSRichard Cochran PTP_ENABLE_FEATURE,
676eb54cbbSRichard Cochran PTP_ENABLE_FEATURE | PTP_RISING_EDGE,
686eb54cbbSRichard Cochran PTP_ENABLE_FEATURE | PTP_FALLING_EDGE,
696eb54cbbSRichard Cochran PTP_ENABLE_FEATURE | PTP_RISING_EDGE | PTP_FALLING_EDGE,
706eb54cbbSRichard Cochran PTP_ENABLE_FEATURE | (PTP_EXTTS_VALID_FLAGS + 1),
716eb54cbbSRichard Cochran };
726eb54cbbSRichard Cochran int err, i, j;
736eb54cbbSRichard Cochran
746eb54cbbSRichard Cochran memset(&extts_request, 0, sizeof(extts_request));
756eb54cbbSRichard Cochran extts_request.index = index;
766eb54cbbSRichard Cochran
776eb54cbbSRichard Cochran for (i = 0; i < 2; i++) {
786eb54cbbSRichard Cochran for (j = 0; j < 5; j++) {
796eb54cbbSRichard Cochran extts_request.flags = enable_flags[j];
806eb54cbbSRichard Cochran err = ioctl(fd, request[i], &extts_request);
816eb54cbbSRichard Cochran show_flag_test(i, extts_request.flags, err);
826eb54cbbSRichard Cochran
836eb54cbbSRichard Cochran extts_request.flags = 0;
846eb54cbbSRichard Cochran err = ioctl(fd, request[i], &extts_request);
856eb54cbbSRichard Cochran }
866eb54cbbSRichard Cochran }
876eb54cbbSRichard Cochran }
886eb54cbbSRichard Cochran
get_clockid(int fd)898dbbf854SShuah Khan static clockid_t get_clockid(int fd)
908dbbf854SShuah Khan {
918dbbf854SShuah Khan #define CLOCKFD 3
9229f1b2b0SNick Desaulniers return (((unsigned int) ~fd) << 3) | CLOCKFD;
938dbbf854SShuah Khan }
948dbbf854SShuah Khan
ppb_to_scaled_ppm(int ppb)958dbbf854SShuah Khan static long ppb_to_scaled_ppm(int ppb)
968dbbf854SShuah Khan {
978dbbf854SShuah Khan /*
988dbbf854SShuah Khan * The 'freq' field in the 'struct timex' is in parts per
998dbbf854SShuah Khan * million, but with a 16 bit binary fractional field.
1008dbbf854SShuah Khan * Instead of calculating either one of
1018dbbf854SShuah Khan *
1028dbbf854SShuah Khan * scaled_ppm = (ppb / 1000) << 16 [1]
1038dbbf854SShuah Khan * scaled_ppm = (ppb << 16) / 1000 [2]
1048dbbf854SShuah Khan *
1058dbbf854SShuah Khan * we simply use double precision math, in order to avoid the
1068dbbf854SShuah Khan * truncation in [1] and the possible overflow in [2].
1078dbbf854SShuah Khan */
1088dbbf854SShuah Khan return (long) (ppb * 65.536);
1098dbbf854SShuah Khan }
1108dbbf854SShuah Khan
pctns(struct ptp_clock_time * t)1118dbbf854SShuah Khan static int64_t pctns(struct ptp_clock_time *t)
1128dbbf854SShuah Khan {
113048f6d99SRahul Rameshbabu return t->sec * NSEC_PER_SEC + t->nsec;
1148dbbf854SShuah Khan }
1158dbbf854SShuah Khan
usage(char * progname)1168dbbf854SShuah Khan static void usage(char *progname)
1178dbbf854SShuah Khan {
1188dbbf854SShuah Khan fprintf(stderr,
1198dbbf854SShuah Khan "usage: %s [options]\n"
1208dbbf854SShuah Khan " -c query the ptp clock's capabilities\n"
1218dbbf854SShuah Khan " -d name device to open\n"
1228dbbf854SShuah Khan " -e val read 'val' external time stamp events\n"
1238dbbf854SShuah Khan " -f val adjust the ptp clock frequency by 'val' ppb\n"
1248dbbf854SShuah Khan " -g get the ptp clock time\n"
1258dbbf854SShuah Khan " -h prints this message\n"
1268dbbf854SShuah Khan " -i val index for event/trigger\n"
1278dbbf854SShuah Khan " -k val measure the time offset between system and phc clock\n"
1288dbbf854SShuah Khan " for 'val' times (Maximum 25)\n"
1298dbbf854SShuah Khan " -l list the current pin configuration\n"
1308dbbf854SShuah Khan " -L pin,val configure pin index 'pin' with function 'val'\n"
1318dbbf854SShuah Khan " the channel index is taken from the '-i' option\n"
1328dbbf854SShuah Khan " 'val' specifies the auxiliary function:\n"
1338dbbf854SShuah Khan " 0 - none\n"
1348dbbf854SShuah Khan " 1 - external time stamp\n"
1358dbbf854SShuah Khan " 2 - periodic output\n"
136f64ae40dSMaciek Machnikowski " -n val shift the ptp clock time by 'val' nanoseconds\n"
1373a9a9a61SRahul Rameshbabu " -o val phase offset (in nanoseconds) to be provided to the PHC servo\n"
1388dbbf854SShuah Khan " -p val enable output with a period of 'val' nanoseconds\n"
1397570ebe0SVladimir Oltean " -H val set output phase to 'val' nanoseconds (requires -p)\n"
1407570ebe0SVladimir Oltean " -w val set output pulse width to 'val' nanoseconds (requires -p)\n"
1418dbbf854SShuah Khan " -P val enable or disable (val=1|0) the system clock PPS\n"
1428dbbf854SShuah Khan " -s set the ptp clock time from the system time\n"
1438dbbf854SShuah Khan " -S set the system time from the ptp clock time\n"
1448dbbf854SShuah Khan " -t val shift the ptp clock time by 'val' seconds\n"
1456eb54cbbSRichard Cochran " -T val set the ptp clock time to 'val' seconds\n"
146c8ba75c4SAlex Maftei " -x val get an extended ptp clock time with the desired number of samples (up to %d)\n"
147*3cf119adSAlex Maftei " -X get a ptp clock cross timestamp\n"
1486eb54cbbSRichard Cochran " -z test combinations of rising/falling external time stamp flags\n",
149c8ba75c4SAlex Maftei progname, PTP_MAX_SAMPLES);
1508dbbf854SShuah Khan }
1518dbbf854SShuah Khan
main(int argc,char * argv[])1528dbbf854SShuah Khan int main(int argc, char *argv[])
1538dbbf854SShuah Khan {
1548dbbf854SShuah Khan struct ptp_clock_caps caps;
1558dbbf854SShuah Khan struct ptp_extts_event event;
1568dbbf854SShuah Khan struct ptp_extts_request extts_request;
1578dbbf854SShuah Khan struct ptp_perout_request perout_request;
1588dbbf854SShuah Khan struct ptp_pin_desc desc;
1598dbbf854SShuah Khan struct timespec ts;
1608dbbf854SShuah Khan struct timex tx;
1618dbbf854SShuah Khan struct ptp_clock_time *pct;
1628dbbf854SShuah Khan struct ptp_sys_offset *sysoff;
163c8ba75c4SAlex Maftei struct ptp_sys_offset_extended *soe;
164*3cf119adSAlex Maftei struct ptp_sys_offset_precise *xts;
1658dbbf854SShuah Khan
1668dbbf854SShuah Khan char *progname;
1678dbbf854SShuah Khan unsigned int i;
1688dbbf854SShuah Khan int c, cnt, fd;
1698dbbf854SShuah Khan
1708dbbf854SShuah Khan char *device = DEVICE;
1718dbbf854SShuah Khan clockid_t clkid;
1728dbbf854SShuah Khan int adjfreq = 0x7fffffff;
1738dbbf854SShuah Khan int adjtime = 0;
174f64ae40dSMaciek Machnikowski int adjns = 0;
1753a9a9a61SRahul Rameshbabu int adjphase = 0;
1768dbbf854SShuah Khan int capabilities = 0;
1778dbbf854SShuah Khan int extts = 0;
1786eb54cbbSRichard Cochran int flagtest = 0;
1798dbbf854SShuah Khan int gettime = 0;
1808dbbf854SShuah Khan int index = 0;
1818dbbf854SShuah Khan int list_pins = 0;
1828dbbf854SShuah Khan int pct_offset = 0;
183c8ba75c4SAlex Maftei int getextended = 0;
184*3cf119adSAlex Maftei int getcross = 0;
1858dbbf854SShuah Khan int n_samples = 0;
1868dbbf854SShuah Khan int pin_index = -1, pin_func;
1878dbbf854SShuah Khan int pps = -1;
1888dbbf854SShuah Khan int seconds = 0;
1898dbbf854SShuah Khan int settime = 0;
1908dbbf854SShuah Khan
1918dbbf854SShuah Khan int64_t t1, t2, tp;
1928dbbf854SShuah Khan int64_t interval, offset;
1937570ebe0SVladimir Oltean int64_t perout_phase = -1;
1947570ebe0SVladimir Oltean int64_t pulsewidth = -1;
1954a09a981SVladimir Oltean int64_t perout = -1;
1968dbbf854SShuah Khan
1978dbbf854SShuah Khan progname = strrchr(argv[0], '/');
1988dbbf854SShuah Khan progname = progname ? 1+progname : argv[0];
199*3cf119adSAlex Maftei while (EOF != (c = getopt(argc, argv, "cd:e:f:ghH:i:k:lL:n:o:p:P:sSt:T:w:x:Xz"))) {
2008dbbf854SShuah Khan switch (c) {
2018dbbf854SShuah Khan case 'c':
2028dbbf854SShuah Khan capabilities = 1;
2038dbbf854SShuah Khan break;
2048dbbf854SShuah Khan case 'd':
2058dbbf854SShuah Khan device = optarg;
2068dbbf854SShuah Khan break;
2078dbbf854SShuah Khan case 'e':
2088dbbf854SShuah Khan extts = atoi(optarg);
2098dbbf854SShuah Khan break;
2108dbbf854SShuah Khan case 'f':
2118dbbf854SShuah Khan adjfreq = atoi(optarg);
2128dbbf854SShuah Khan break;
2138dbbf854SShuah Khan case 'g':
2148dbbf854SShuah Khan gettime = 1;
2158dbbf854SShuah Khan break;
2167570ebe0SVladimir Oltean case 'H':
2177570ebe0SVladimir Oltean perout_phase = atoll(optarg);
2187570ebe0SVladimir Oltean break;
2198dbbf854SShuah Khan case 'i':
2208dbbf854SShuah Khan index = atoi(optarg);
2218dbbf854SShuah Khan break;
2228dbbf854SShuah Khan case 'k':
2238dbbf854SShuah Khan pct_offset = 1;
2248dbbf854SShuah Khan n_samples = atoi(optarg);
2258dbbf854SShuah Khan break;
2268dbbf854SShuah Khan case 'l':
2278dbbf854SShuah Khan list_pins = 1;
2288dbbf854SShuah Khan break;
2298dbbf854SShuah Khan case 'L':
2308dbbf854SShuah Khan cnt = sscanf(optarg, "%d,%d", &pin_index, &pin_func);
2318dbbf854SShuah Khan if (cnt != 2) {
2328dbbf854SShuah Khan usage(progname);
2338dbbf854SShuah Khan return -1;
2348dbbf854SShuah Khan }
2358dbbf854SShuah Khan break;
236f64ae40dSMaciek Machnikowski case 'n':
237f64ae40dSMaciek Machnikowski adjns = atoi(optarg);
238f64ae40dSMaciek Machnikowski break;
2393a9a9a61SRahul Rameshbabu case 'o':
2403a9a9a61SRahul Rameshbabu adjphase = atoi(optarg);
2413a9a9a61SRahul Rameshbabu break;
2428dbbf854SShuah Khan case 'p':
2434a09a981SVladimir Oltean perout = atoll(optarg);
2448dbbf854SShuah Khan break;
2458dbbf854SShuah Khan case 'P':
2468dbbf854SShuah Khan pps = atoi(optarg);
2478dbbf854SShuah Khan break;
2488dbbf854SShuah Khan case 's':
2498dbbf854SShuah Khan settime = 1;
2508dbbf854SShuah Khan break;
2518dbbf854SShuah Khan case 'S':
2528dbbf854SShuah Khan settime = 2;
2538dbbf854SShuah Khan break;
2548dbbf854SShuah Khan case 't':
2558dbbf854SShuah Khan adjtime = atoi(optarg);
2568dbbf854SShuah Khan break;
2578dbbf854SShuah Khan case 'T':
2588dbbf854SShuah Khan settime = 3;
2598dbbf854SShuah Khan seconds = atoi(optarg);
2608dbbf854SShuah Khan break;
2617570ebe0SVladimir Oltean case 'w':
2627570ebe0SVladimir Oltean pulsewidth = atoi(optarg);
2637570ebe0SVladimir Oltean break;
264c8ba75c4SAlex Maftei case 'x':
265c8ba75c4SAlex Maftei getextended = atoi(optarg);
266c8ba75c4SAlex Maftei if (getextended < 1 || getextended > PTP_MAX_SAMPLES) {
267c8ba75c4SAlex Maftei fprintf(stderr,
268c8ba75c4SAlex Maftei "number of extended timestamp samples must be between 1 and %d; was asked for %d\n",
269c8ba75c4SAlex Maftei PTP_MAX_SAMPLES, getextended);
270c8ba75c4SAlex Maftei return -1;
271c8ba75c4SAlex Maftei }
272c8ba75c4SAlex Maftei break;
273*3cf119adSAlex Maftei case 'X':
274*3cf119adSAlex Maftei getcross = 1;
275*3cf119adSAlex Maftei break;
2766eb54cbbSRichard Cochran case 'z':
2776eb54cbbSRichard Cochran flagtest = 1;
2786eb54cbbSRichard Cochran break;
2798dbbf854SShuah Khan case 'h':
2808dbbf854SShuah Khan usage(progname);
2818dbbf854SShuah Khan return 0;
2828dbbf854SShuah Khan case '?':
2838dbbf854SShuah Khan default:
2848dbbf854SShuah Khan usage(progname);
2858dbbf854SShuah Khan return -1;
2868dbbf854SShuah Khan }
2878dbbf854SShuah Khan }
2888dbbf854SShuah Khan
2898dbbf854SShuah Khan fd = open(device, O_RDWR);
2908dbbf854SShuah Khan if (fd < 0) {
2918dbbf854SShuah Khan fprintf(stderr, "opening %s: %s\n", device, strerror(errno));
2928dbbf854SShuah Khan return -1;
2938dbbf854SShuah Khan }
2948dbbf854SShuah Khan
2958dbbf854SShuah Khan clkid = get_clockid(fd);
2968dbbf854SShuah Khan if (CLOCK_INVALID == clkid) {
2978dbbf854SShuah Khan fprintf(stderr, "failed to read clock id\n");
2988dbbf854SShuah Khan return -1;
2998dbbf854SShuah Khan }
3008dbbf854SShuah Khan
3018dbbf854SShuah Khan if (capabilities) {
3028dbbf854SShuah Khan if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
3038dbbf854SShuah Khan perror("PTP_CLOCK_GETCAPS");
3048dbbf854SShuah Khan } else {
3058dbbf854SShuah Khan printf("capabilities:\n"
3068dbbf854SShuah Khan " %d maximum frequency adjustment (ppb)\n"
3078dbbf854SShuah Khan " %d programmable alarms\n"
3088dbbf854SShuah Khan " %d external time stamp channels\n"
3098dbbf854SShuah Khan " %d programmable periodic signals\n"
3108dbbf854SShuah Khan " %d pulse per second\n"
3118dbbf854SShuah Khan " %d programmable pins\n"
312d3f1cbd2SVincent Cheng " %d cross timestamping\n"
313c3b60ab7SRahul Rameshbabu " %d adjust_phase\n"
314c3b60ab7SRahul Rameshbabu " %d maximum phase adjustment (ns)\n",
3158dbbf854SShuah Khan caps.max_adj,
3168dbbf854SShuah Khan caps.n_alarm,
3178dbbf854SShuah Khan caps.n_ext_ts,
3188dbbf854SShuah Khan caps.n_per_out,
3198dbbf854SShuah Khan caps.pps,
3208dbbf854SShuah Khan caps.n_pins,
321d3f1cbd2SVincent Cheng caps.cross_timestamping,
322c3b60ab7SRahul Rameshbabu caps.adjust_phase,
323c3b60ab7SRahul Rameshbabu caps.max_phase_adj);
3248dbbf854SShuah Khan }
3258dbbf854SShuah Khan }
3268dbbf854SShuah Khan
3278dbbf854SShuah Khan if (0x7fffffff != adjfreq) {
3288dbbf854SShuah Khan memset(&tx, 0, sizeof(tx));
3298dbbf854SShuah Khan tx.modes = ADJ_FREQUENCY;
3308dbbf854SShuah Khan tx.freq = ppb_to_scaled_ppm(adjfreq);
3318dbbf854SShuah Khan if (clock_adjtime(clkid, &tx)) {
3328dbbf854SShuah Khan perror("clock_adjtime");
3338dbbf854SShuah Khan } else {
3348dbbf854SShuah Khan puts("frequency adjustment okay");
3358dbbf854SShuah Khan }
3368dbbf854SShuah Khan }
3378dbbf854SShuah Khan
338f64ae40dSMaciek Machnikowski if (adjtime || adjns) {
3398dbbf854SShuah Khan memset(&tx, 0, sizeof(tx));
340f64ae40dSMaciek Machnikowski tx.modes = ADJ_SETOFFSET | ADJ_NANO;
3418dbbf854SShuah Khan tx.time.tv_sec = adjtime;
342f64ae40dSMaciek Machnikowski tx.time.tv_usec = adjns;
343f64ae40dSMaciek Machnikowski while (tx.time.tv_usec < 0) {
344f64ae40dSMaciek Machnikowski tx.time.tv_sec -= 1;
345048f6d99SRahul Rameshbabu tx.time.tv_usec += NSEC_PER_SEC;
346f64ae40dSMaciek Machnikowski }
347f64ae40dSMaciek Machnikowski
3488dbbf854SShuah Khan if (clock_adjtime(clkid, &tx) < 0) {
3498dbbf854SShuah Khan perror("clock_adjtime");
3508dbbf854SShuah Khan } else {
3518dbbf854SShuah Khan puts("time shift okay");
3528dbbf854SShuah Khan }
3538dbbf854SShuah Khan }
3548dbbf854SShuah Khan
3553a9a9a61SRahul Rameshbabu if (adjphase) {
3563a9a9a61SRahul Rameshbabu memset(&tx, 0, sizeof(tx));
3573a9a9a61SRahul Rameshbabu tx.modes = ADJ_OFFSET | ADJ_NANO;
3583a9a9a61SRahul Rameshbabu tx.offset = adjphase;
3593a9a9a61SRahul Rameshbabu
3603a9a9a61SRahul Rameshbabu if (clock_adjtime(clkid, &tx) < 0) {
3613a9a9a61SRahul Rameshbabu perror("clock_adjtime");
3623a9a9a61SRahul Rameshbabu } else {
3633a9a9a61SRahul Rameshbabu puts("phase adjustment okay");
3643a9a9a61SRahul Rameshbabu }
3653a9a9a61SRahul Rameshbabu }
3663a9a9a61SRahul Rameshbabu
3678dbbf854SShuah Khan if (gettime) {
3688dbbf854SShuah Khan if (clock_gettime(clkid, &ts)) {
3698dbbf854SShuah Khan perror("clock_gettime");
3708dbbf854SShuah Khan } else {
3718dbbf854SShuah Khan printf("clock time: %ld.%09ld or %s",
3728dbbf854SShuah Khan ts.tv_sec, ts.tv_nsec, ctime(&ts.tv_sec));
3738dbbf854SShuah Khan }
3748dbbf854SShuah Khan }
3758dbbf854SShuah Khan
3768dbbf854SShuah Khan if (settime == 1) {
3778dbbf854SShuah Khan clock_gettime(CLOCK_REALTIME, &ts);
3788dbbf854SShuah Khan if (clock_settime(clkid, &ts)) {
3798dbbf854SShuah Khan perror("clock_settime");
3808dbbf854SShuah Khan } else {
3818dbbf854SShuah Khan puts("set time okay");
3828dbbf854SShuah Khan }
3838dbbf854SShuah Khan }
3848dbbf854SShuah Khan
3858dbbf854SShuah Khan if (settime == 2) {
3868dbbf854SShuah Khan clock_gettime(clkid, &ts);
3878dbbf854SShuah Khan if (clock_settime(CLOCK_REALTIME, &ts)) {
3888dbbf854SShuah Khan perror("clock_settime");
3898dbbf854SShuah Khan } else {
3908dbbf854SShuah Khan puts("set time okay");
3918dbbf854SShuah Khan }
3928dbbf854SShuah Khan }
3938dbbf854SShuah Khan
3948dbbf854SShuah Khan if (settime == 3) {
3958dbbf854SShuah Khan ts.tv_sec = seconds;
3968dbbf854SShuah Khan ts.tv_nsec = 0;
3978dbbf854SShuah Khan if (clock_settime(clkid, &ts)) {
3988dbbf854SShuah Khan perror("clock_settime");
3998dbbf854SShuah Khan } else {
4008dbbf854SShuah Khan puts("set time okay");
4018dbbf854SShuah Khan }
4028dbbf854SShuah Khan }
4038dbbf854SShuah Khan
40487eee9c5SMiroslav Lichvar if (pin_index >= 0) {
40587eee9c5SMiroslav Lichvar memset(&desc, 0, sizeof(desc));
40687eee9c5SMiroslav Lichvar desc.index = pin_index;
40787eee9c5SMiroslav Lichvar desc.func = pin_func;
40887eee9c5SMiroslav Lichvar desc.chan = index;
40987eee9c5SMiroslav Lichvar if (ioctl(fd, PTP_PIN_SETFUNC, &desc)) {
41087eee9c5SMiroslav Lichvar perror("PTP_PIN_SETFUNC");
41187eee9c5SMiroslav Lichvar } else {
41287eee9c5SMiroslav Lichvar puts("set pin function okay");
41387eee9c5SMiroslav Lichvar }
41487eee9c5SMiroslav Lichvar }
41587eee9c5SMiroslav Lichvar
4168dbbf854SShuah Khan if (extts) {
4178dbbf854SShuah Khan memset(&extts_request, 0, sizeof(extts_request));
4188dbbf854SShuah Khan extts_request.index = index;
4198dbbf854SShuah Khan extts_request.flags = PTP_ENABLE_FEATURE;
4208dbbf854SShuah Khan if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
4218dbbf854SShuah Khan perror("PTP_EXTTS_REQUEST");
4228dbbf854SShuah Khan extts = 0;
4238dbbf854SShuah Khan } else {
4248dbbf854SShuah Khan puts("external time stamp request okay");
4258dbbf854SShuah Khan }
4268dbbf854SShuah Khan for (; extts; extts--) {
4278dbbf854SShuah Khan cnt = read(fd, &event, sizeof(event));
4288dbbf854SShuah Khan if (cnt != sizeof(event)) {
4298dbbf854SShuah Khan perror("read");
4308dbbf854SShuah Khan break;
4318dbbf854SShuah Khan }
4328dbbf854SShuah Khan printf("event index %u at %lld.%09u\n", event.index,
4338dbbf854SShuah Khan event.t.sec, event.t.nsec);
4348dbbf854SShuah Khan fflush(stdout);
4358dbbf854SShuah Khan }
4368dbbf854SShuah Khan /* Disable the feature again. */
4378dbbf854SShuah Khan extts_request.flags = 0;
4388dbbf854SShuah Khan if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
4398dbbf854SShuah Khan perror("PTP_EXTTS_REQUEST");
4408dbbf854SShuah Khan }
4418dbbf854SShuah Khan }
4428dbbf854SShuah Khan
4436eb54cbbSRichard Cochran if (flagtest) {
4446eb54cbbSRichard Cochran do_flag_test(fd, index);
4456eb54cbbSRichard Cochran }
4466eb54cbbSRichard Cochran
4478dbbf854SShuah Khan if (list_pins) {
4488dbbf854SShuah Khan int n_pins = 0;
4498dbbf854SShuah Khan if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
4508dbbf854SShuah Khan perror("PTP_CLOCK_GETCAPS");
4518dbbf854SShuah Khan } else {
4528dbbf854SShuah Khan n_pins = caps.n_pins;
4538dbbf854SShuah Khan }
4548dbbf854SShuah Khan for (i = 0; i < n_pins; i++) {
4558dbbf854SShuah Khan desc.index = i;
4568dbbf854SShuah Khan if (ioctl(fd, PTP_PIN_GETFUNC, &desc)) {
4578dbbf854SShuah Khan perror("PTP_PIN_GETFUNC");
4588dbbf854SShuah Khan break;
4598dbbf854SShuah Khan }
4608dbbf854SShuah Khan printf("name %s index %u func %u chan %u\n",
4618dbbf854SShuah Khan desc.name, desc.index, desc.func, desc.chan);
4628dbbf854SShuah Khan }
4638dbbf854SShuah Khan }
4648dbbf854SShuah Khan
4657570ebe0SVladimir Oltean if (pulsewidth >= 0 && perout < 0) {
4667570ebe0SVladimir Oltean puts("-w can only be specified together with -p");
4677570ebe0SVladimir Oltean return -1;
4687570ebe0SVladimir Oltean }
4697570ebe0SVladimir Oltean
4707570ebe0SVladimir Oltean if (perout_phase >= 0 && perout < 0) {
4717570ebe0SVladimir Oltean puts("-H can only be specified together with -p");
4727570ebe0SVladimir Oltean return -1;
4737570ebe0SVladimir Oltean }
4747570ebe0SVladimir Oltean
4758dbbf854SShuah Khan if (perout >= 0) {
4768dbbf854SShuah Khan if (clock_gettime(clkid, &ts)) {
4778dbbf854SShuah Khan perror("clock_gettime");
4788dbbf854SShuah Khan return -1;
4798dbbf854SShuah Khan }
4808dbbf854SShuah Khan memset(&perout_request, 0, sizeof(perout_request));
4818dbbf854SShuah Khan perout_request.index = index;
4824a09a981SVladimir Oltean perout_request.period.sec = perout / NSEC_PER_SEC;
4834a09a981SVladimir Oltean perout_request.period.nsec = perout % NSEC_PER_SEC;
4847570ebe0SVladimir Oltean perout_request.flags = 0;
4857570ebe0SVladimir Oltean if (pulsewidth >= 0) {
4867570ebe0SVladimir Oltean perout_request.flags |= PTP_PEROUT_DUTY_CYCLE;
4877570ebe0SVladimir Oltean perout_request.on.sec = pulsewidth / NSEC_PER_SEC;
4887570ebe0SVladimir Oltean perout_request.on.nsec = pulsewidth % NSEC_PER_SEC;
4897570ebe0SVladimir Oltean }
4907570ebe0SVladimir Oltean if (perout_phase >= 0) {
4917570ebe0SVladimir Oltean perout_request.flags |= PTP_PEROUT_PHASE;
4927570ebe0SVladimir Oltean perout_request.phase.sec = perout_phase / NSEC_PER_SEC;
4937570ebe0SVladimir Oltean perout_request.phase.nsec = perout_phase % NSEC_PER_SEC;
4947570ebe0SVladimir Oltean } else {
4957570ebe0SVladimir Oltean perout_request.start.sec = ts.tv_sec + 2;
4967570ebe0SVladimir Oltean perout_request.start.nsec = 0;
4977570ebe0SVladimir Oltean }
4987570ebe0SVladimir Oltean
4997570ebe0SVladimir Oltean if (ioctl(fd, PTP_PEROUT_REQUEST2, &perout_request)) {
5008dbbf854SShuah Khan perror("PTP_PEROUT_REQUEST");
5018dbbf854SShuah Khan } else {
5028dbbf854SShuah Khan puts("periodic output request okay");
5038dbbf854SShuah Khan }
5048dbbf854SShuah Khan }
5058dbbf854SShuah Khan
5068dbbf854SShuah Khan if (pps != -1) {
5078dbbf854SShuah Khan int enable = pps ? 1 : 0;
5088dbbf854SShuah Khan if (ioctl(fd, PTP_ENABLE_PPS, enable)) {
5098dbbf854SShuah Khan perror("PTP_ENABLE_PPS");
5108dbbf854SShuah Khan } else {
5118dbbf854SShuah Khan puts("pps for system time request okay");
5128dbbf854SShuah Khan }
5138dbbf854SShuah Khan }
5148dbbf854SShuah Khan
5158dbbf854SShuah Khan if (pct_offset) {
5168dbbf854SShuah Khan if (n_samples <= 0 || n_samples > 25) {
5178dbbf854SShuah Khan puts("n_samples should be between 1 and 25");
5188dbbf854SShuah Khan usage(progname);
5198dbbf854SShuah Khan return -1;
5208dbbf854SShuah Khan }
5218dbbf854SShuah Khan
5228dbbf854SShuah Khan sysoff = calloc(1, sizeof(*sysoff));
5238dbbf854SShuah Khan if (!sysoff) {
5248dbbf854SShuah Khan perror("calloc");
5258dbbf854SShuah Khan return -1;
5268dbbf854SShuah Khan }
5278dbbf854SShuah Khan sysoff->n_samples = n_samples;
5288dbbf854SShuah Khan
5298dbbf854SShuah Khan if (ioctl(fd, PTP_SYS_OFFSET, sysoff))
5308dbbf854SShuah Khan perror("PTP_SYS_OFFSET");
5318dbbf854SShuah Khan else
5328dbbf854SShuah Khan puts("system and phc clock time offset request okay");
5338dbbf854SShuah Khan
5348dbbf854SShuah Khan pct = &sysoff->ts[0];
5358dbbf854SShuah Khan for (i = 0; i < sysoff->n_samples; i++) {
5368dbbf854SShuah Khan t1 = pctns(pct+2*i);
5378dbbf854SShuah Khan tp = pctns(pct+2*i+1);
5388dbbf854SShuah Khan t2 = pctns(pct+2*i+2);
5398dbbf854SShuah Khan interval = t2 - t1;
5408dbbf854SShuah Khan offset = (t2 + t1) / 2 - tp;
5418dbbf854SShuah Khan
54276a4c8b8SAlex Maftei printf("system time: %lld.%09u\n",
5438dbbf854SShuah Khan (pct+2*i)->sec, (pct+2*i)->nsec);
54476a4c8b8SAlex Maftei printf("phc time: %lld.%09u\n",
5458dbbf854SShuah Khan (pct+2*i+1)->sec, (pct+2*i+1)->nsec);
54676a4c8b8SAlex Maftei printf("system time: %lld.%09u\n",
5478dbbf854SShuah Khan (pct+2*i+2)->sec, (pct+2*i+2)->nsec);
5488dbbf854SShuah Khan printf("system/phc clock time offset is %" PRId64 " ns\n"
5498dbbf854SShuah Khan "system clock time delay is %" PRId64 " ns\n",
5508dbbf854SShuah Khan offset, interval);
5518dbbf854SShuah Khan }
5528dbbf854SShuah Khan
5538dbbf854SShuah Khan free(sysoff);
5548dbbf854SShuah Khan }
5558dbbf854SShuah Khan
556c8ba75c4SAlex Maftei if (getextended) {
557c8ba75c4SAlex Maftei soe = calloc(1, sizeof(*soe));
558c8ba75c4SAlex Maftei if (!soe) {
559c8ba75c4SAlex Maftei perror("calloc");
560c8ba75c4SAlex Maftei return -1;
561c8ba75c4SAlex Maftei }
562c8ba75c4SAlex Maftei
563c8ba75c4SAlex Maftei soe->n_samples = getextended;
564c8ba75c4SAlex Maftei
565c8ba75c4SAlex Maftei if (ioctl(fd, PTP_SYS_OFFSET_EXTENDED, soe)) {
566c8ba75c4SAlex Maftei perror("PTP_SYS_OFFSET_EXTENDED");
567c8ba75c4SAlex Maftei } else {
568c8ba75c4SAlex Maftei printf("extended timestamp request returned %d samples\n",
569c8ba75c4SAlex Maftei getextended);
570c8ba75c4SAlex Maftei
571c8ba75c4SAlex Maftei for (i = 0; i < getextended; i++) {
572c8ba75c4SAlex Maftei printf("sample #%2d: system time before: %lld.%09u\n",
573c8ba75c4SAlex Maftei i, soe->ts[i][0].sec, soe->ts[i][0].nsec);
574c8ba75c4SAlex Maftei printf(" phc time: %lld.%09u\n",
575c8ba75c4SAlex Maftei soe->ts[i][1].sec, soe->ts[i][1].nsec);
576c8ba75c4SAlex Maftei printf(" system time after: %lld.%09u\n",
577c8ba75c4SAlex Maftei soe->ts[i][2].sec, soe->ts[i][2].nsec);
578c8ba75c4SAlex Maftei }
579c8ba75c4SAlex Maftei }
580c8ba75c4SAlex Maftei
581c8ba75c4SAlex Maftei free(soe);
582c8ba75c4SAlex Maftei }
583c8ba75c4SAlex Maftei
584*3cf119adSAlex Maftei if (getcross) {
585*3cf119adSAlex Maftei xts = calloc(1, sizeof(*xts));
586*3cf119adSAlex Maftei if (!xts) {
587*3cf119adSAlex Maftei perror("calloc");
588*3cf119adSAlex Maftei return -1;
589*3cf119adSAlex Maftei }
590*3cf119adSAlex Maftei
591*3cf119adSAlex Maftei if (ioctl(fd, PTP_SYS_OFFSET_PRECISE, xts)) {
592*3cf119adSAlex Maftei perror("PTP_SYS_OFFSET_PRECISE");
593*3cf119adSAlex Maftei } else {
594*3cf119adSAlex Maftei puts("system and phc crosstimestamping request okay");
595*3cf119adSAlex Maftei
596*3cf119adSAlex Maftei printf("device time: %lld.%09u\n",
597*3cf119adSAlex Maftei xts->device.sec, xts->device.nsec);
598*3cf119adSAlex Maftei printf("system time: %lld.%09u\n",
599*3cf119adSAlex Maftei xts->sys_realtime.sec, xts->sys_realtime.nsec);
600*3cf119adSAlex Maftei printf("monoraw time: %lld.%09u\n",
601*3cf119adSAlex Maftei xts->sys_monoraw.sec, xts->sys_monoraw.nsec);
602*3cf119adSAlex Maftei }
603*3cf119adSAlex Maftei
604*3cf119adSAlex Maftei free(xts);
605*3cf119adSAlex Maftei }
606*3cf119adSAlex Maftei
6078dbbf854SShuah Khan close(fd);
6088dbbf854SShuah Khan return 0;
6098dbbf854SShuah Khan }
610