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"
124*26285e68SXabier Marquiegui " -F chan Enable single channel mask and keep device open for debugfs verification.\n"
1258dbbf854SShuah Khan " -g get the ptp clock time\n"
1268dbbf854SShuah Khan " -h prints this message\n"
1278dbbf854SShuah Khan " -i val index for event/trigger\n"
1288dbbf854SShuah Khan " -k val measure the time offset between system and phc clock\n"
1298dbbf854SShuah Khan " for 'val' times (Maximum 25)\n"
1308dbbf854SShuah Khan " -l list the current pin configuration\n"
1318dbbf854SShuah Khan " -L pin,val configure pin index 'pin' with function 'val'\n"
1328dbbf854SShuah Khan " the channel index is taken from the '-i' option\n"
1338dbbf854SShuah Khan " 'val' specifies the auxiliary function:\n"
1348dbbf854SShuah Khan " 0 - none\n"
1358dbbf854SShuah Khan " 1 - external time stamp\n"
1368dbbf854SShuah Khan " 2 - periodic output\n"
137f64ae40dSMaciek Machnikowski " -n val shift the ptp clock time by 'val' nanoseconds\n"
1383a9a9a61SRahul Rameshbabu " -o val phase offset (in nanoseconds) to be provided to the PHC servo\n"
1398dbbf854SShuah Khan " -p val enable output with a period of 'val' nanoseconds\n"
1407570ebe0SVladimir Oltean " -H val set output phase to 'val' nanoseconds (requires -p)\n"
1417570ebe0SVladimir Oltean " -w val set output pulse width to 'val' nanoseconds (requires -p)\n"
1428dbbf854SShuah Khan " -P val enable or disable (val=1|0) the system clock PPS\n"
1438dbbf854SShuah Khan " -s set the ptp clock time from the system time\n"
1448dbbf854SShuah Khan " -S set the system time from the ptp clock time\n"
1458dbbf854SShuah Khan " -t val shift the ptp clock time by 'val' seconds\n"
1466eb54cbbSRichard Cochran " -T val set the ptp clock time to 'val' seconds\n"
147c8ba75c4SAlex Maftei " -x val get an extended ptp clock time with the desired number of samples (up to %d)\n"
1483cf119adSAlex Maftei " -X get a ptp clock cross timestamp\n"
1496eb54cbbSRichard Cochran " -z test combinations of rising/falling external time stamp flags\n",
150c8ba75c4SAlex Maftei progname, PTP_MAX_SAMPLES);
1518dbbf854SShuah Khan }
1528dbbf854SShuah Khan
main(int argc,char * argv[])1538dbbf854SShuah Khan int main(int argc, char *argv[])
1548dbbf854SShuah Khan {
1558dbbf854SShuah Khan struct ptp_clock_caps caps;
1568dbbf854SShuah Khan struct ptp_extts_event event;
1578dbbf854SShuah Khan struct ptp_extts_request extts_request;
1588dbbf854SShuah Khan struct ptp_perout_request perout_request;
1598dbbf854SShuah Khan struct ptp_pin_desc desc;
1608dbbf854SShuah Khan struct timespec ts;
1618dbbf854SShuah Khan struct timex tx;
1628dbbf854SShuah Khan struct ptp_clock_time *pct;
1638dbbf854SShuah Khan struct ptp_sys_offset *sysoff;
164c8ba75c4SAlex Maftei struct ptp_sys_offset_extended *soe;
1653cf119adSAlex Maftei struct ptp_sys_offset_precise *xts;
1668dbbf854SShuah Khan
1678dbbf854SShuah Khan char *progname;
1688dbbf854SShuah Khan unsigned int i;
1698dbbf854SShuah Khan int c, cnt, fd;
1708dbbf854SShuah Khan
1718dbbf854SShuah Khan char *device = DEVICE;
1728dbbf854SShuah Khan clockid_t clkid;
1738dbbf854SShuah Khan int adjfreq = 0x7fffffff;
1748dbbf854SShuah Khan int adjtime = 0;
175f64ae40dSMaciek Machnikowski int adjns = 0;
1763a9a9a61SRahul Rameshbabu int adjphase = 0;
1778dbbf854SShuah Khan int capabilities = 0;
1788dbbf854SShuah Khan int extts = 0;
1796eb54cbbSRichard Cochran int flagtest = 0;
1808dbbf854SShuah Khan int gettime = 0;
1818dbbf854SShuah Khan int index = 0;
1828dbbf854SShuah Khan int list_pins = 0;
1838dbbf854SShuah Khan int pct_offset = 0;
184c8ba75c4SAlex Maftei int getextended = 0;
1853cf119adSAlex Maftei int getcross = 0;
1868dbbf854SShuah Khan int n_samples = 0;
1878dbbf854SShuah Khan int pin_index = -1, pin_func;
1888dbbf854SShuah Khan int pps = -1;
1898dbbf854SShuah Khan int seconds = 0;
1908dbbf854SShuah Khan int settime = 0;
191*26285e68SXabier Marquiegui int channel = -1;
1928dbbf854SShuah Khan
1938dbbf854SShuah Khan int64_t t1, t2, tp;
1948dbbf854SShuah Khan int64_t interval, offset;
1957570ebe0SVladimir Oltean int64_t perout_phase = -1;
1967570ebe0SVladimir Oltean int64_t pulsewidth = -1;
1974a09a981SVladimir Oltean int64_t perout = -1;
1988dbbf854SShuah Khan
1998dbbf854SShuah Khan progname = strrchr(argv[0], '/');
2008dbbf854SShuah Khan progname = progname ? 1+progname : argv[0];
201*26285e68SXabier Marquiegui while (EOF != (c = getopt(argc, argv, "cd:e:f:F:ghH:i:k:lL:n:o:p:P:sSt:T:w:x:Xz"))) {
2028dbbf854SShuah Khan switch (c) {
2038dbbf854SShuah Khan case 'c':
2048dbbf854SShuah Khan capabilities = 1;
2058dbbf854SShuah Khan break;
2068dbbf854SShuah Khan case 'd':
2078dbbf854SShuah Khan device = optarg;
2088dbbf854SShuah Khan break;
2098dbbf854SShuah Khan case 'e':
2108dbbf854SShuah Khan extts = atoi(optarg);
2118dbbf854SShuah Khan break;
2128dbbf854SShuah Khan case 'f':
2138dbbf854SShuah Khan adjfreq = atoi(optarg);
2148dbbf854SShuah Khan break;
215*26285e68SXabier Marquiegui case 'F':
216*26285e68SXabier Marquiegui channel = atoi(optarg);
217*26285e68SXabier Marquiegui break;
2188dbbf854SShuah Khan case 'g':
2198dbbf854SShuah Khan gettime = 1;
2208dbbf854SShuah Khan break;
2217570ebe0SVladimir Oltean case 'H':
2227570ebe0SVladimir Oltean perout_phase = atoll(optarg);
2237570ebe0SVladimir Oltean break;
2248dbbf854SShuah Khan case 'i':
2258dbbf854SShuah Khan index = atoi(optarg);
2268dbbf854SShuah Khan break;
2278dbbf854SShuah Khan case 'k':
2288dbbf854SShuah Khan pct_offset = 1;
2298dbbf854SShuah Khan n_samples = atoi(optarg);
2308dbbf854SShuah Khan break;
2318dbbf854SShuah Khan case 'l':
2328dbbf854SShuah Khan list_pins = 1;
2338dbbf854SShuah Khan break;
2348dbbf854SShuah Khan case 'L':
2358dbbf854SShuah Khan cnt = sscanf(optarg, "%d,%d", &pin_index, &pin_func);
2368dbbf854SShuah Khan if (cnt != 2) {
2378dbbf854SShuah Khan usage(progname);
2388dbbf854SShuah Khan return -1;
2398dbbf854SShuah Khan }
2408dbbf854SShuah Khan break;
241f64ae40dSMaciek Machnikowski case 'n':
242f64ae40dSMaciek Machnikowski adjns = atoi(optarg);
243f64ae40dSMaciek Machnikowski break;
2443a9a9a61SRahul Rameshbabu case 'o':
2453a9a9a61SRahul Rameshbabu adjphase = atoi(optarg);
2463a9a9a61SRahul Rameshbabu break;
2478dbbf854SShuah Khan case 'p':
2484a09a981SVladimir Oltean perout = atoll(optarg);
2498dbbf854SShuah Khan break;
2508dbbf854SShuah Khan case 'P':
2518dbbf854SShuah Khan pps = atoi(optarg);
2528dbbf854SShuah Khan break;
2538dbbf854SShuah Khan case 's':
2548dbbf854SShuah Khan settime = 1;
2558dbbf854SShuah Khan break;
2568dbbf854SShuah Khan case 'S':
2578dbbf854SShuah Khan settime = 2;
2588dbbf854SShuah Khan break;
2598dbbf854SShuah Khan case 't':
2608dbbf854SShuah Khan adjtime = atoi(optarg);
2618dbbf854SShuah Khan break;
2628dbbf854SShuah Khan case 'T':
2638dbbf854SShuah Khan settime = 3;
2648dbbf854SShuah Khan seconds = atoi(optarg);
2658dbbf854SShuah Khan break;
2667570ebe0SVladimir Oltean case 'w':
2677570ebe0SVladimir Oltean pulsewidth = atoi(optarg);
2687570ebe0SVladimir Oltean break;
269c8ba75c4SAlex Maftei case 'x':
270c8ba75c4SAlex Maftei getextended = atoi(optarg);
271c8ba75c4SAlex Maftei if (getextended < 1 || getextended > PTP_MAX_SAMPLES) {
272c8ba75c4SAlex Maftei fprintf(stderr,
273c8ba75c4SAlex Maftei "number of extended timestamp samples must be between 1 and %d; was asked for %d\n",
274c8ba75c4SAlex Maftei PTP_MAX_SAMPLES, getextended);
275c8ba75c4SAlex Maftei return -1;
276c8ba75c4SAlex Maftei }
277c8ba75c4SAlex Maftei break;
2783cf119adSAlex Maftei case 'X':
2793cf119adSAlex Maftei getcross = 1;
2803cf119adSAlex Maftei break;
2816eb54cbbSRichard Cochran case 'z':
2826eb54cbbSRichard Cochran flagtest = 1;
2836eb54cbbSRichard Cochran break;
2848dbbf854SShuah Khan case 'h':
2858dbbf854SShuah Khan usage(progname);
2868dbbf854SShuah Khan return 0;
2878dbbf854SShuah Khan case '?':
2888dbbf854SShuah Khan default:
2898dbbf854SShuah Khan usage(progname);
2908dbbf854SShuah Khan return -1;
2918dbbf854SShuah Khan }
2928dbbf854SShuah Khan }
2938dbbf854SShuah Khan
2948dbbf854SShuah Khan fd = open(device, O_RDWR);
2958dbbf854SShuah Khan if (fd < 0) {
2968dbbf854SShuah Khan fprintf(stderr, "opening %s: %s\n", device, strerror(errno));
2978dbbf854SShuah Khan return -1;
2988dbbf854SShuah Khan }
2998dbbf854SShuah Khan
3008dbbf854SShuah Khan clkid = get_clockid(fd);
3018dbbf854SShuah Khan if (CLOCK_INVALID == clkid) {
3028dbbf854SShuah Khan fprintf(stderr, "failed to read clock id\n");
3038dbbf854SShuah Khan return -1;
3048dbbf854SShuah Khan }
3058dbbf854SShuah Khan
3068dbbf854SShuah Khan if (capabilities) {
3078dbbf854SShuah Khan if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
3088dbbf854SShuah Khan perror("PTP_CLOCK_GETCAPS");
3098dbbf854SShuah Khan } else {
3108dbbf854SShuah Khan printf("capabilities:\n"
3118dbbf854SShuah Khan " %d maximum frequency adjustment (ppb)\n"
3128dbbf854SShuah Khan " %d programmable alarms\n"
3138dbbf854SShuah Khan " %d external time stamp channels\n"
3148dbbf854SShuah Khan " %d programmable periodic signals\n"
3158dbbf854SShuah Khan " %d pulse per second\n"
3168dbbf854SShuah Khan " %d programmable pins\n"
317d3f1cbd2SVincent Cheng " %d cross timestamping\n"
318c3b60ab7SRahul Rameshbabu " %d adjust_phase\n"
319c3b60ab7SRahul Rameshbabu " %d maximum phase adjustment (ns)\n",
3208dbbf854SShuah Khan caps.max_adj,
3218dbbf854SShuah Khan caps.n_alarm,
3228dbbf854SShuah Khan caps.n_ext_ts,
3238dbbf854SShuah Khan caps.n_per_out,
3248dbbf854SShuah Khan caps.pps,
3258dbbf854SShuah Khan caps.n_pins,
326d3f1cbd2SVincent Cheng caps.cross_timestamping,
327c3b60ab7SRahul Rameshbabu caps.adjust_phase,
328c3b60ab7SRahul Rameshbabu caps.max_phase_adj);
3298dbbf854SShuah Khan }
3308dbbf854SShuah Khan }
3318dbbf854SShuah Khan
3328dbbf854SShuah Khan if (0x7fffffff != adjfreq) {
3338dbbf854SShuah Khan memset(&tx, 0, sizeof(tx));
3348dbbf854SShuah Khan tx.modes = ADJ_FREQUENCY;
3358dbbf854SShuah Khan tx.freq = ppb_to_scaled_ppm(adjfreq);
3368dbbf854SShuah Khan if (clock_adjtime(clkid, &tx)) {
3378dbbf854SShuah Khan perror("clock_adjtime");
3388dbbf854SShuah Khan } else {
3398dbbf854SShuah Khan puts("frequency adjustment okay");
3408dbbf854SShuah Khan }
3418dbbf854SShuah Khan }
3428dbbf854SShuah Khan
343f64ae40dSMaciek Machnikowski if (adjtime || adjns) {
3448dbbf854SShuah Khan memset(&tx, 0, sizeof(tx));
345f64ae40dSMaciek Machnikowski tx.modes = ADJ_SETOFFSET | ADJ_NANO;
3468dbbf854SShuah Khan tx.time.tv_sec = adjtime;
347f64ae40dSMaciek Machnikowski tx.time.tv_usec = adjns;
348f64ae40dSMaciek Machnikowski while (tx.time.tv_usec < 0) {
349f64ae40dSMaciek Machnikowski tx.time.tv_sec -= 1;
350048f6d99SRahul Rameshbabu tx.time.tv_usec += NSEC_PER_SEC;
351f64ae40dSMaciek Machnikowski }
352f64ae40dSMaciek Machnikowski
3538dbbf854SShuah Khan if (clock_adjtime(clkid, &tx) < 0) {
3548dbbf854SShuah Khan perror("clock_adjtime");
3558dbbf854SShuah Khan } else {
3568dbbf854SShuah Khan puts("time shift okay");
3578dbbf854SShuah Khan }
3588dbbf854SShuah Khan }
3598dbbf854SShuah Khan
3603a9a9a61SRahul Rameshbabu if (adjphase) {
3613a9a9a61SRahul Rameshbabu memset(&tx, 0, sizeof(tx));
3623a9a9a61SRahul Rameshbabu tx.modes = ADJ_OFFSET | ADJ_NANO;
3633a9a9a61SRahul Rameshbabu tx.offset = adjphase;
3643a9a9a61SRahul Rameshbabu
3653a9a9a61SRahul Rameshbabu if (clock_adjtime(clkid, &tx) < 0) {
3663a9a9a61SRahul Rameshbabu perror("clock_adjtime");
3673a9a9a61SRahul Rameshbabu } else {
3683a9a9a61SRahul Rameshbabu puts("phase adjustment okay");
3693a9a9a61SRahul Rameshbabu }
3703a9a9a61SRahul Rameshbabu }
3713a9a9a61SRahul Rameshbabu
3728dbbf854SShuah Khan if (gettime) {
3738dbbf854SShuah Khan if (clock_gettime(clkid, &ts)) {
3748dbbf854SShuah Khan perror("clock_gettime");
3758dbbf854SShuah Khan } else {
3768dbbf854SShuah Khan printf("clock time: %ld.%09ld or %s",
3778dbbf854SShuah Khan ts.tv_sec, ts.tv_nsec, ctime(&ts.tv_sec));
3788dbbf854SShuah Khan }
3798dbbf854SShuah Khan }
3808dbbf854SShuah Khan
3818dbbf854SShuah Khan if (settime == 1) {
3828dbbf854SShuah Khan clock_gettime(CLOCK_REALTIME, &ts);
3838dbbf854SShuah Khan if (clock_settime(clkid, &ts)) {
3848dbbf854SShuah Khan perror("clock_settime");
3858dbbf854SShuah Khan } else {
3868dbbf854SShuah Khan puts("set time okay");
3878dbbf854SShuah Khan }
3888dbbf854SShuah Khan }
3898dbbf854SShuah Khan
3908dbbf854SShuah Khan if (settime == 2) {
3918dbbf854SShuah Khan clock_gettime(clkid, &ts);
3928dbbf854SShuah Khan if (clock_settime(CLOCK_REALTIME, &ts)) {
3938dbbf854SShuah Khan perror("clock_settime");
3948dbbf854SShuah Khan } else {
3958dbbf854SShuah Khan puts("set time okay");
3968dbbf854SShuah Khan }
3978dbbf854SShuah Khan }
3988dbbf854SShuah Khan
3998dbbf854SShuah Khan if (settime == 3) {
4008dbbf854SShuah Khan ts.tv_sec = seconds;
4018dbbf854SShuah Khan ts.tv_nsec = 0;
4028dbbf854SShuah Khan if (clock_settime(clkid, &ts)) {
4038dbbf854SShuah Khan perror("clock_settime");
4048dbbf854SShuah Khan } else {
4058dbbf854SShuah Khan puts("set time okay");
4068dbbf854SShuah Khan }
4078dbbf854SShuah Khan }
4088dbbf854SShuah Khan
40987eee9c5SMiroslav Lichvar if (pin_index >= 0) {
41087eee9c5SMiroslav Lichvar memset(&desc, 0, sizeof(desc));
41187eee9c5SMiroslav Lichvar desc.index = pin_index;
41287eee9c5SMiroslav Lichvar desc.func = pin_func;
41387eee9c5SMiroslav Lichvar desc.chan = index;
41487eee9c5SMiroslav Lichvar if (ioctl(fd, PTP_PIN_SETFUNC, &desc)) {
41587eee9c5SMiroslav Lichvar perror("PTP_PIN_SETFUNC");
41687eee9c5SMiroslav Lichvar } else {
41787eee9c5SMiroslav Lichvar puts("set pin function okay");
41887eee9c5SMiroslav Lichvar }
41987eee9c5SMiroslav Lichvar }
42087eee9c5SMiroslav Lichvar
4218dbbf854SShuah Khan if (extts) {
4228dbbf854SShuah Khan memset(&extts_request, 0, sizeof(extts_request));
4238dbbf854SShuah Khan extts_request.index = index;
4248dbbf854SShuah Khan extts_request.flags = PTP_ENABLE_FEATURE;
4258dbbf854SShuah Khan if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
4268dbbf854SShuah Khan perror("PTP_EXTTS_REQUEST");
4278dbbf854SShuah Khan extts = 0;
4288dbbf854SShuah Khan } else {
4298dbbf854SShuah Khan puts("external time stamp request okay");
4308dbbf854SShuah Khan }
4318dbbf854SShuah Khan for (; extts; extts--) {
4328dbbf854SShuah Khan cnt = read(fd, &event, sizeof(event));
4338dbbf854SShuah Khan if (cnt != sizeof(event)) {
4348dbbf854SShuah Khan perror("read");
4358dbbf854SShuah Khan break;
4368dbbf854SShuah Khan }
4378dbbf854SShuah Khan printf("event index %u at %lld.%09u\n", event.index,
4388dbbf854SShuah Khan event.t.sec, event.t.nsec);
4398dbbf854SShuah Khan fflush(stdout);
4408dbbf854SShuah Khan }
4418dbbf854SShuah Khan /* Disable the feature again. */
4428dbbf854SShuah Khan extts_request.flags = 0;
4438dbbf854SShuah Khan if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
4448dbbf854SShuah Khan perror("PTP_EXTTS_REQUEST");
4458dbbf854SShuah Khan }
4468dbbf854SShuah Khan }
4478dbbf854SShuah Khan
4486eb54cbbSRichard Cochran if (flagtest) {
4496eb54cbbSRichard Cochran do_flag_test(fd, index);
4506eb54cbbSRichard Cochran }
4516eb54cbbSRichard Cochran
4528dbbf854SShuah Khan if (list_pins) {
4538dbbf854SShuah Khan int n_pins = 0;
4548dbbf854SShuah Khan if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
4558dbbf854SShuah Khan perror("PTP_CLOCK_GETCAPS");
4568dbbf854SShuah Khan } else {
4578dbbf854SShuah Khan n_pins = caps.n_pins;
4588dbbf854SShuah Khan }
4598dbbf854SShuah Khan for (i = 0; i < n_pins; i++) {
4608dbbf854SShuah Khan desc.index = i;
4618dbbf854SShuah Khan if (ioctl(fd, PTP_PIN_GETFUNC, &desc)) {
4628dbbf854SShuah Khan perror("PTP_PIN_GETFUNC");
4638dbbf854SShuah Khan break;
4648dbbf854SShuah Khan }
4658dbbf854SShuah Khan printf("name %s index %u func %u chan %u\n",
4668dbbf854SShuah Khan desc.name, desc.index, desc.func, desc.chan);
4678dbbf854SShuah Khan }
4688dbbf854SShuah Khan }
4698dbbf854SShuah Khan
4707570ebe0SVladimir Oltean if (pulsewidth >= 0 && perout < 0) {
4717570ebe0SVladimir Oltean puts("-w can only be specified together with -p");
4727570ebe0SVladimir Oltean return -1;
4737570ebe0SVladimir Oltean }
4747570ebe0SVladimir Oltean
4757570ebe0SVladimir Oltean if (perout_phase >= 0 && perout < 0) {
4767570ebe0SVladimir Oltean puts("-H can only be specified together with -p");
4777570ebe0SVladimir Oltean return -1;
4787570ebe0SVladimir Oltean }
4797570ebe0SVladimir Oltean
4808dbbf854SShuah Khan if (perout >= 0) {
4818dbbf854SShuah Khan if (clock_gettime(clkid, &ts)) {
4828dbbf854SShuah Khan perror("clock_gettime");
4838dbbf854SShuah Khan return -1;
4848dbbf854SShuah Khan }
4858dbbf854SShuah Khan memset(&perout_request, 0, sizeof(perout_request));
4868dbbf854SShuah Khan perout_request.index = index;
4874a09a981SVladimir Oltean perout_request.period.sec = perout / NSEC_PER_SEC;
4884a09a981SVladimir Oltean perout_request.period.nsec = perout % NSEC_PER_SEC;
4897570ebe0SVladimir Oltean perout_request.flags = 0;
4907570ebe0SVladimir Oltean if (pulsewidth >= 0) {
4917570ebe0SVladimir Oltean perout_request.flags |= PTP_PEROUT_DUTY_CYCLE;
4927570ebe0SVladimir Oltean perout_request.on.sec = pulsewidth / NSEC_PER_SEC;
4937570ebe0SVladimir Oltean perout_request.on.nsec = pulsewidth % NSEC_PER_SEC;
4947570ebe0SVladimir Oltean }
4957570ebe0SVladimir Oltean if (perout_phase >= 0) {
4967570ebe0SVladimir Oltean perout_request.flags |= PTP_PEROUT_PHASE;
4977570ebe0SVladimir Oltean perout_request.phase.sec = perout_phase / NSEC_PER_SEC;
4987570ebe0SVladimir Oltean perout_request.phase.nsec = perout_phase % NSEC_PER_SEC;
4997570ebe0SVladimir Oltean } else {
5007570ebe0SVladimir Oltean perout_request.start.sec = ts.tv_sec + 2;
5017570ebe0SVladimir Oltean perout_request.start.nsec = 0;
5027570ebe0SVladimir Oltean }
5037570ebe0SVladimir Oltean
5047570ebe0SVladimir Oltean if (ioctl(fd, PTP_PEROUT_REQUEST2, &perout_request)) {
5058dbbf854SShuah Khan perror("PTP_PEROUT_REQUEST");
5068dbbf854SShuah Khan } else {
5078dbbf854SShuah Khan puts("periodic output request okay");
5088dbbf854SShuah Khan }
5098dbbf854SShuah Khan }
5108dbbf854SShuah Khan
5118dbbf854SShuah Khan if (pps != -1) {
5128dbbf854SShuah Khan int enable = pps ? 1 : 0;
5138dbbf854SShuah Khan if (ioctl(fd, PTP_ENABLE_PPS, enable)) {
5148dbbf854SShuah Khan perror("PTP_ENABLE_PPS");
5158dbbf854SShuah Khan } else {
5168dbbf854SShuah Khan puts("pps for system time request okay");
5178dbbf854SShuah Khan }
5188dbbf854SShuah Khan }
5198dbbf854SShuah Khan
5208dbbf854SShuah Khan if (pct_offset) {
5218dbbf854SShuah Khan if (n_samples <= 0 || n_samples > 25) {
5228dbbf854SShuah Khan puts("n_samples should be between 1 and 25");
5238dbbf854SShuah Khan usage(progname);
5248dbbf854SShuah Khan return -1;
5258dbbf854SShuah Khan }
5268dbbf854SShuah Khan
5278dbbf854SShuah Khan sysoff = calloc(1, sizeof(*sysoff));
5288dbbf854SShuah Khan if (!sysoff) {
5298dbbf854SShuah Khan perror("calloc");
5308dbbf854SShuah Khan return -1;
5318dbbf854SShuah Khan }
5328dbbf854SShuah Khan sysoff->n_samples = n_samples;
5338dbbf854SShuah Khan
5348dbbf854SShuah Khan if (ioctl(fd, PTP_SYS_OFFSET, sysoff))
5358dbbf854SShuah Khan perror("PTP_SYS_OFFSET");
5368dbbf854SShuah Khan else
5378dbbf854SShuah Khan puts("system and phc clock time offset request okay");
5388dbbf854SShuah Khan
5398dbbf854SShuah Khan pct = &sysoff->ts[0];
5408dbbf854SShuah Khan for (i = 0; i < sysoff->n_samples; i++) {
5418dbbf854SShuah Khan t1 = pctns(pct+2*i);
5428dbbf854SShuah Khan tp = pctns(pct+2*i+1);
5438dbbf854SShuah Khan t2 = pctns(pct+2*i+2);
5448dbbf854SShuah Khan interval = t2 - t1;
5458dbbf854SShuah Khan offset = (t2 + t1) / 2 - tp;
5468dbbf854SShuah Khan
54776a4c8b8SAlex Maftei printf("system time: %lld.%09u\n",
5488dbbf854SShuah Khan (pct+2*i)->sec, (pct+2*i)->nsec);
54976a4c8b8SAlex Maftei printf("phc time: %lld.%09u\n",
5508dbbf854SShuah Khan (pct+2*i+1)->sec, (pct+2*i+1)->nsec);
55176a4c8b8SAlex Maftei printf("system time: %lld.%09u\n",
5528dbbf854SShuah Khan (pct+2*i+2)->sec, (pct+2*i+2)->nsec);
5538dbbf854SShuah Khan printf("system/phc clock time offset is %" PRId64 " ns\n"
5548dbbf854SShuah Khan "system clock time delay is %" PRId64 " ns\n",
5558dbbf854SShuah Khan offset, interval);
5568dbbf854SShuah Khan }
5578dbbf854SShuah Khan
5588dbbf854SShuah Khan free(sysoff);
5598dbbf854SShuah Khan }
5608dbbf854SShuah Khan
561c8ba75c4SAlex Maftei if (getextended) {
562c8ba75c4SAlex Maftei soe = calloc(1, sizeof(*soe));
563c8ba75c4SAlex Maftei if (!soe) {
564c8ba75c4SAlex Maftei perror("calloc");
565c8ba75c4SAlex Maftei return -1;
566c8ba75c4SAlex Maftei }
567c8ba75c4SAlex Maftei
568c8ba75c4SAlex Maftei soe->n_samples = getextended;
569c8ba75c4SAlex Maftei
570c8ba75c4SAlex Maftei if (ioctl(fd, PTP_SYS_OFFSET_EXTENDED, soe)) {
571c8ba75c4SAlex Maftei perror("PTP_SYS_OFFSET_EXTENDED");
572c8ba75c4SAlex Maftei } else {
573c8ba75c4SAlex Maftei printf("extended timestamp request returned %d samples\n",
574c8ba75c4SAlex Maftei getextended);
575c8ba75c4SAlex Maftei
576c8ba75c4SAlex Maftei for (i = 0; i < getextended; i++) {
577c8ba75c4SAlex Maftei printf("sample #%2d: system time before: %lld.%09u\n",
578c8ba75c4SAlex Maftei i, soe->ts[i][0].sec, soe->ts[i][0].nsec);
579c8ba75c4SAlex Maftei printf(" phc time: %lld.%09u\n",
580c8ba75c4SAlex Maftei soe->ts[i][1].sec, soe->ts[i][1].nsec);
581c8ba75c4SAlex Maftei printf(" system time after: %lld.%09u\n",
582c8ba75c4SAlex Maftei soe->ts[i][2].sec, soe->ts[i][2].nsec);
583c8ba75c4SAlex Maftei }
584c8ba75c4SAlex Maftei }
585c8ba75c4SAlex Maftei
586c8ba75c4SAlex Maftei free(soe);
587c8ba75c4SAlex Maftei }
588c8ba75c4SAlex Maftei
5893cf119adSAlex Maftei if (getcross) {
5903cf119adSAlex Maftei xts = calloc(1, sizeof(*xts));
5913cf119adSAlex Maftei if (!xts) {
5923cf119adSAlex Maftei perror("calloc");
5933cf119adSAlex Maftei return -1;
5943cf119adSAlex Maftei }
5953cf119adSAlex Maftei
5963cf119adSAlex Maftei if (ioctl(fd, PTP_SYS_OFFSET_PRECISE, xts)) {
5973cf119adSAlex Maftei perror("PTP_SYS_OFFSET_PRECISE");
5983cf119adSAlex Maftei } else {
5993cf119adSAlex Maftei puts("system and phc crosstimestamping request okay");
6003cf119adSAlex Maftei
6013cf119adSAlex Maftei printf("device time: %lld.%09u\n",
6023cf119adSAlex Maftei xts->device.sec, xts->device.nsec);
6033cf119adSAlex Maftei printf("system time: %lld.%09u\n",
6043cf119adSAlex Maftei xts->sys_realtime.sec, xts->sys_realtime.nsec);
6053cf119adSAlex Maftei printf("monoraw time: %lld.%09u\n",
6063cf119adSAlex Maftei xts->sys_monoraw.sec, xts->sys_monoraw.nsec);
6073cf119adSAlex Maftei }
6083cf119adSAlex Maftei
6093cf119adSAlex Maftei free(xts);
6103cf119adSAlex Maftei }
6113cf119adSAlex Maftei
612*26285e68SXabier Marquiegui if (channel >= 0) {
613*26285e68SXabier Marquiegui if (ioctl(fd, PTP_MASK_CLEAR_ALL)) {
614*26285e68SXabier Marquiegui perror("PTP_MASK_CLEAR_ALL");
615*26285e68SXabier Marquiegui } else if (ioctl(fd, PTP_MASK_EN_SINGLE, (unsigned int *)&channel)) {
616*26285e68SXabier Marquiegui perror("PTP_MASK_EN_SINGLE");
617*26285e68SXabier Marquiegui } else {
618*26285e68SXabier Marquiegui printf("Channel %d exclusively enabled. Check on debugfs.\n", channel);
619*26285e68SXabier Marquiegui printf("Press any key to continue\n.");
620*26285e68SXabier Marquiegui getchar();
621*26285e68SXabier Marquiegui }
622*26285e68SXabier Marquiegui }
623*26285e68SXabier Marquiegui
6248dbbf854SShuah Khan close(fd);
6258dbbf854SShuah Khan return 0;
6268dbbf854SShuah Khan }
627