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> 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 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 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 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 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 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 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" 137*3a9a9a61SRahul 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" 1466eb54cbbSRichard Cochran " -z test combinations of rising/falling external time stamp flags\n", 1478dbbf854SShuah Khan progname); 1488dbbf854SShuah Khan } 1498dbbf854SShuah Khan 1508dbbf854SShuah Khan int main(int argc, char *argv[]) 1518dbbf854SShuah Khan { 1528dbbf854SShuah Khan struct ptp_clock_caps caps; 1538dbbf854SShuah Khan struct ptp_extts_event event; 1548dbbf854SShuah Khan struct ptp_extts_request extts_request; 1558dbbf854SShuah Khan struct ptp_perout_request perout_request; 1568dbbf854SShuah Khan struct ptp_pin_desc desc; 1578dbbf854SShuah Khan struct timespec ts; 1588dbbf854SShuah Khan struct timex tx; 1598dbbf854SShuah Khan struct ptp_clock_time *pct; 1608dbbf854SShuah Khan struct ptp_sys_offset *sysoff; 1618dbbf854SShuah Khan 1628dbbf854SShuah Khan char *progname; 1638dbbf854SShuah Khan unsigned int i; 1648dbbf854SShuah Khan int c, cnt, fd; 1658dbbf854SShuah Khan 1668dbbf854SShuah Khan char *device = DEVICE; 1678dbbf854SShuah Khan clockid_t clkid; 1688dbbf854SShuah Khan int adjfreq = 0x7fffffff; 1698dbbf854SShuah Khan int adjtime = 0; 170f64ae40dSMaciek Machnikowski int adjns = 0; 171*3a9a9a61SRahul Rameshbabu int adjphase = 0; 1728dbbf854SShuah Khan int capabilities = 0; 1738dbbf854SShuah Khan int extts = 0; 1746eb54cbbSRichard Cochran int flagtest = 0; 1758dbbf854SShuah Khan int gettime = 0; 1768dbbf854SShuah Khan int index = 0; 1778dbbf854SShuah Khan int list_pins = 0; 1788dbbf854SShuah Khan int pct_offset = 0; 1798dbbf854SShuah Khan int n_samples = 0; 1808dbbf854SShuah Khan int pin_index = -1, pin_func; 1818dbbf854SShuah Khan int pps = -1; 1828dbbf854SShuah Khan int seconds = 0; 1838dbbf854SShuah Khan int settime = 0; 1848dbbf854SShuah Khan 1858dbbf854SShuah Khan int64_t t1, t2, tp; 1868dbbf854SShuah Khan int64_t interval, offset; 1877570ebe0SVladimir Oltean int64_t perout_phase = -1; 1887570ebe0SVladimir Oltean int64_t pulsewidth = -1; 1894a09a981SVladimir Oltean int64_t perout = -1; 1908dbbf854SShuah Khan 1918dbbf854SShuah Khan progname = strrchr(argv[0], '/'); 1928dbbf854SShuah Khan progname = progname ? 1+progname : argv[0]; 193*3a9a9a61SRahul Rameshbabu while (EOF != (c = getopt(argc, argv, "cd:e:f:ghH:i:k:lL:n:o:p:P:sSt:T:w:z"))) { 1948dbbf854SShuah Khan switch (c) { 1958dbbf854SShuah Khan case 'c': 1968dbbf854SShuah Khan capabilities = 1; 1978dbbf854SShuah Khan break; 1988dbbf854SShuah Khan case 'd': 1998dbbf854SShuah Khan device = optarg; 2008dbbf854SShuah Khan break; 2018dbbf854SShuah Khan case 'e': 2028dbbf854SShuah Khan extts = atoi(optarg); 2038dbbf854SShuah Khan break; 2048dbbf854SShuah Khan case 'f': 2058dbbf854SShuah Khan adjfreq = atoi(optarg); 2068dbbf854SShuah Khan break; 2078dbbf854SShuah Khan case 'g': 2088dbbf854SShuah Khan gettime = 1; 2098dbbf854SShuah Khan break; 2107570ebe0SVladimir Oltean case 'H': 2117570ebe0SVladimir Oltean perout_phase = atoll(optarg); 2127570ebe0SVladimir Oltean break; 2138dbbf854SShuah Khan case 'i': 2148dbbf854SShuah Khan index = atoi(optarg); 2158dbbf854SShuah Khan break; 2168dbbf854SShuah Khan case 'k': 2178dbbf854SShuah Khan pct_offset = 1; 2188dbbf854SShuah Khan n_samples = atoi(optarg); 2198dbbf854SShuah Khan break; 2208dbbf854SShuah Khan case 'l': 2218dbbf854SShuah Khan list_pins = 1; 2228dbbf854SShuah Khan break; 2238dbbf854SShuah Khan case 'L': 2248dbbf854SShuah Khan cnt = sscanf(optarg, "%d,%d", &pin_index, &pin_func); 2258dbbf854SShuah Khan if (cnt != 2) { 2268dbbf854SShuah Khan usage(progname); 2278dbbf854SShuah Khan return -1; 2288dbbf854SShuah Khan } 2298dbbf854SShuah Khan break; 230f64ae40dSMaciek Machnikowski case 'n': 231f64ae40dSMaciek Machnikowski adjns = atoi(optarg); 232f64ae40dSMaciek Machnikowski break; 233*3a9a9a61SRahul Rameshbabu case 'o': 234*3a9a9a61SRahul Rameshbabu adjphase = atoi(optarg); 235*3a9a9a61SRahul Rameshbabu break; 2368dbbf854SShuah Khan case 'p': 2374a09a981SVladimir Oltean perout = atoll(optarg); 2388dbbf854SShuah Khan break; 2398dbbf854SShuah Khan case 'P': 2408dbbf854SShuah Khan pps = atoi(optarg); 2418dbbf854SShuah Khan break; 2428dbbf854SShuah Khan case 's': 2438dbbf854SShuah Khan settime = 1; 2448dbbf854SShuah Khan break; 2458dbbf854SShuah Khan case 'S': 2468dbbf854SShuah Khan settime = 2; 2478dbbf854SShuah Khan break; 2488dbbf854SShuah Khan case 't': 2498dbbf854SShuah Khan adjtime = atoi(optarg); 2508dbbf854SShuah Khan break; 2518dbbf854SShuah Khan case 'T': 2528dbbf854SShuah Khan settime = 3; 2538dbbf854SShuah Khan seconds = atoi(optarg); 2548dbbf854SShuah Khan break; 2557570ebe0SVladimir Oltean case 'w': 2567570ebe0SVladimir Oltean pulsewidth = atoi(optarg); 2577570ebe0SVladimir Oltean break; 2586eb54cbbSRichard Cochran case 'z': 2596eb54cbbSRichard Cochran flagtest = 1; 2606eb54cbbSRichard Cochran break; 2618dbbf854SShuah Khan case 'h': 2628dbbf854SShuah Khan usage(progname); 2638dbbf854SShuah Khan return 0; 2648dbbf854SShuah Khan case '?': 2658dbbf854SShuah Khan default: 2668dbbf854SShuah Khan usage(progname); 2678dbbf854SShuah Khan return -1; 2688dbbf854SShuah Khan } 2698dbbf854SShuah Khan } 2708dbbf854SShuah Khan 2718dbbf854SShuah Khan fd = open(device, O_RDWR); 2728dbbf854SShuah Khan if (fd < 0) { 2738dbbf854SShuah Khan fprintf(stderr, "opening %s: %s\n", device, strerror(errno)); 2748dbbf854SShuah Khan return -1; 2758dbbf854SShuah Khan } 2768dbbf854SShuah Khan 2778dbbf854SShuah Khan clkid = get_clockid(fd); 2788dbbf854SShuah Khan if (CLOCK_INVALID == clkid) { 2798dbbf854SShuah Khan fprintf(stderr, "failed to read clock id\n"); 2808dbbf854SShuah Khan return -1; 2818dbbf854SShuah Khan } 2828dbbf854SShuah Khan 2838dbbf854SShuah Khan if (capabilities) { 2848dbbf854SShuah Khan if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) { 2858dbbf854SShuah Khan perror("PTP_CLOCK_GETCAPS"); 2868dbbf854SShuah Khan } else { 2878dbbf854SShuah Khan printf("capabilities:\n" 2888dbbf854SShuah Khan " %d maximum frequency adjustment (ppb)\n" 2898dbbf854SShuah Khan " %d programmable alarms\n" 2908dbbf854SShuah Khan " %d external time stamp channels\n" 2918dbbf854SShuah Khan " %d programmable periodic signals\n" 2928dbbf854SShuah Khan " %d pulse per second\n" 2938dbbf854SShuah Khan " %d programmable pins\n" 294d3f1cbd2SVincent Cheng " %d cross timestamping\n" 295d3f1cbd2SVincent Cheng " %d adjust_phase\n", 2968dbbf854SShuah Khan caps.max_adj, 2978dbbf854SShuah Khan caps.n_alarm, 2988dbbf854SShuah Khan caps.n_ext_ts, 2998dbbf854SShuah Khan caps.n_per_out, 3008dbbf854SShuah Khan caps.pps, 3018dbbf854SShuah Khan caps.n_pins, 302d3f1cbd2SVincent Cheng caps.cross_timestamping, 303d3f1cbd2SVincent Cheng caps.adjust_phase); 3048dbbf854SShuah Khan } 3058dbbf854SShuah Khan } 3068dbbf854SShuah Khan 3078dbbf854SShuah Khan if (0x7fffffff != adjfreq) { 3088dbbf854SShuah Khan memset(&tx, 0, sizeof(tx)); 3098dbbf854SShuah Khan tx.modes = ADJ_FREQUENCY; 3108dbbf854SShuah Khan tx.freq = ppb_to_scaled_ppm(adjfreq); 3118dbbf854SShuah Khan if (clock_adjtime(clkid, &tx)) { 3128dbbf854SShuah Khan perror("clock_adjtime"); 3138dbbf854SShuah Khan } else { 3148dbbf854SShuah Khan puts("frequency adjustment okay"); 3158dbbf854SShuah Khan } 3168dbbf854SShuah Khan } 3178dbbf854SShuah Khan 318f64ae40dSMaciek Machnikowski if (adjtime || adjns) { 3198dbbf854SShuah Khan memset(&tx, 0, sizeof(tx)); 320f64ae40dSMaciek Machnikowski tx.modes = ADJ_SETOFFSET | ADJ_NANO; 3218dbbf854SShuah Khan tx.time.tv_sec = adjtime; 322f64ae40dSMaciek Machnikowski tx.time.tv_usec = adjns; 323f64ae40dSMaciek Machnikowski while (tx.time.tv_usec < 0) { 324f64ae40dSMaciek Machnikowski tx.time.tv_sec -= 1; 325048f6d99SRahul Rameshbabu tx.time.tv_usec += NSEC_PER_SEC; 326f64ae40dSMaciek Machnikowski } 327f64ae40dSMaciek Machnikowski 3288dbbf854SShuah Khan if (clock_adjtime(clkid, &tx) < 0) { 3298dbbf854SShuah Khan perror("clock_adjtime"); 3308dbbf854SShuah Khan } else { 3318dbbf854SShuah Khan puts("time shift okay"); 3328dbbf854SShuah Khan } 3338dbbf854SShuah Khan } 3348dbbf854SShuah Khan 335*3a9a9a61SRahul Rameshbabu if (adjphase) { 336*3a9a9a61SRahul Rameshbabu memset(&tx, 0, sizeof(tx)); 337*3a9a9a61SRahul Rameshbabu tx.modes = ADJ_OFFSET | ADJ_NANO; 338*3a9a9a61SRahul Rameshbabu tx.offset = adjphase; 339*3a9a9a61SRahul Rameshbabu 340*3a9a9a61SRahul Rameshbabu if (clock_adjtime(clkid, &tx) < 0) { 341*3a9a9a61SRahul Rameshbabu perror("clock_adjtime"); 342*3a9a9a61SRahul Rameshbabu } else { 343*3a9a9a61SRahul Rameshbabu puts("phase adjustment okay"); 344*3a9a9a61SRahul Rameshbabu } 345*3a9a9a61SRahul Rameshbabu } 346*3a9a9a61SRahul Rameshbabu 3478dbbf854SShuah Khan if (gettime) { 3488dbbf854SShuah Khan if (clock_gettime(clkid, &ts)) { 3498dbbf854SShuah Khan perror("clock_gettime"); 3508dbbf854SShuah Khan } else { 3518dbbf854SShuah Khan printf("clock time: %ld.%09ld or %s", 3528dbbf854SShuah Khan ts.tv_sec, ts.tv_nsec, ctime(&ts.tv_sec)); 3538dbbf854SShuah Khan } 3548dbbf854SShuah Khan } 3558dbbf854SShuah Khan 3568dbbf854SShuah Khan if (settime == 1) { 3578dbbf854SShuah Khan clock_gettime(CLOCK_REALTIME, &ts); 3588dbbf854SShuah Khan if (clock_settime(clkid, &ts)) { 3598dbbf854SShuah Khan perror("clock_settime"); 3608dbbf854SShuah Khan } else { 3618dbbf854SShuah Khan puts("set time okay"); 3628dbbf854SShuah Khan } 3638dbbf854SShuah Khan } 3648dbbf854SShuah Khan 3658dbbf854SShuah Khan if (settime == 2) { 3668dbbf854SShuah Khan clock_gettime(clkid, &ts); 3678dbbf854SShuah Khan if (clock_settime(CLOCK_REALTIME, &ts)) { 3688dbbf854SShuah Khan perror("clock_settime"); 3698dbbf854SShuah Khan } else { 3708dbbf854SShuah Khan puts("set time okay"); 3718dbbf854SShuah Khan } 3728dbbf854SShuah Khan } 3738dbbf854SShuah Khan 3748dbbf854SShuah Khan if (settime == 3) { 3758dbbf854SShuah Khan ts.tv_sec = seconds; 3768dbbf854SShuah Khan ts.tv_nsec = 0; 3778dbbf854SShuah Khan if (clock_settime(clkid, &ts)) { 3788dbbf854SShuah Khan perror("clock_settime"); 3798dbbf854SShuah Khan } else { 3808dbbf854SShuah Khan puts("set time okay"); 3818dbbf854SShuah Khan } 3828dbbf854SShuah Khan } 3838dbbf854SShuah Khan 38487eee9c5SMiroslav Lichvar if (pin_index >= 0) { 38587eee9c5SMiroslav Lichvar memset(&desc, 0, sizeof(desc)); 38687eee9c5SMiroslav Lichvar desc.index = pin_index; 38787eee9c5SMiroslav Lichvar desc.func = pin_func; 38887eee9c5SMiroslav Lichvar desc.chan = index; 38987eee9c5SMiroslav Lichvar if (ioctl(fd, PTP_PIN_SETFUNC, &desc)) { 39087eee9c5SMiroslav Lichvar perror("PTP_PIN_SETFUNC"); 39187eee9c5SMiroslav Lichvar } else { 39287eee9c5SMiroslav Lichvar puts("set pin function okay"); 39387eee9c5SMiroslav Lichvar } 39487eee9c5SMiroslav Lichvar } 39587eee9c5SMiroslav Lichvar 3968dbbf854SShuah Khan if (extts) { 3978dbbf854SShuah Khan memset(&extts_request, 0, sizeof(extts_request)); 3988dbbf854SShuah Khan extts_request.index = index; 3998dbbf854SShuah Khan extts_request.flags = PTP_ENABLE_FEATURE; 4008dbbf854SShuah Khan if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) { 4018dbbf854SShuah Khan perror("PTP_EXTTS_REQUEST"); 4028dbbf854SShuah Khan extts = 0; 4038dbbf854SShuah Khan } else { 4048dbbf854SShuah Khan puts("external time stamp request okay"); 4058dbbf854SShuah Khan } 4068dbbf854SShuah Khan for (; extts; extts--) { 4078dbbf854SShuah Khan cnt = read(fd, &event, sizeof(event)); 4088dbbf854SShuah Khan if (cnt != sizeof(event)) { 4098dbbf854SShuah Khan perror("read"); 4108dbbf854SShuah Khan break; 4118dbbf854SShuah Khan } 4128dbbf854SShuah Khan printf("event index %u at %lld.%09u\n", event.index, 4138dbbf854SShuah Khan event.t.sec, event.t.nsec); 4148dbbf854SShuah Khan fflush(stdout); 4158dbbf854SShuah Khan } 4168dbbf854SShuah Khan /* Disable the feature again. */ 4178dbbf854SShuah Khan extts_request.flags = 0; 4188dbbf854SShuah Khan if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) { 4198dbbf854SShuah Khan perror("PTP_EXTTS_REQUEST"); 4208dbbf854SShuah Khan } 4218dbbf854SShuah Khan } 4228dbbf854SShuah Khan 4236eb54cbbSRichard Cochran if (flagtest) { 4246eb54cbbSRichard Cochran do_flag_test(fd, index); 4256eb54cbbSRichard Cochran } 4266eb54cbbSRichard Cochran 4278dbbf854SShuah Khan if (list_pins) { 4288dbbf854SShuah Khan int n_pins = 0; 4298dbbf854SShuah Khan if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) { 4308dbbf854SShuah Khan perror("PTP_CLOCK_GETCAPS"); 4318dbbf854SShuah Khan } else { 4328dbbf854SShuah Khan n_pins = caps.n_pins; 4338dbbf854SShuah Khan } 4348dbbf854SShuah Khan for (i = 0; i < n_pins; i++) { 4358dbbf854SShuah Khan desc.index = i; 4368dbbf854SShuah Khan if (ioctl(fd, PTP_PIN_GETFUNC, &desc)) { 4378dbbf854SShuah Khan perror("PTP_PIN_GETFUNC"); 4388dbbf854SShuah Khan break; 4398dbbf854SShuah Khan } 4408dbbf854SShuah Khan printf("name %s index %u func %u chan %u\n", 4418dbbf854SShuah Khan desc.name, desc.index, desc.func, desc.chan); 4428dbbf854SShuah Khan } 4438dbbf854SShuah Khan } 4448dbbf854SShuah Khan 4457570ebe0SVladimir Oltean if (pulsewidth >= 0 && perout < 0) { 4467570ebe0SVladimir Oltean puts("-w can only be specified together with -p"); 4477570ebe0SVladimir Oltean return -1; 4487570ebe0SVladimir Oltean } 4497570ebe0SVladimir Oltean 4507570ebe0SVladimir Oltean if (perout_phase >= 0 && perout < 0) { 4517570ebe0SVladimir Oltean puts("-H can only be specified together with -p"); 4527570ebe0SVladimir Oltean return -1; 4537570ebe0SVladimir Oltean } 4547570ebe0SVladimir Oltean 4558dbbf854SShuah Khan if (perout >= 0) { 4568dbbf854SShuah Khan if (clock_gettime(clkid, &ts)) { 4578dbbf854SShuah Khan perror("clock_gettime"); 4588dbbf854SShuah Khan return -1; 4598dbbf854SShuah Khan } 4608dbbf854SShuah Khan memset(&perout_request, 0, sizeof(perout_request)); 4618dbbf854SShuah Khan perout_request.index = index; 4624a09a981SVladimir Oltean perout_request.period.sec = perout / NSEC_PER_SEC; 4634a09a981SVladimir Oltean perout_request.period.nsec = perout % NSEC_PER_SEC; 4647570ebe0SVladimir Oltean perout_request.flags = 0; 4657570ebe0SVladimir Oltean if (pulsewidth >= 0) { 4667570ebe0SVladimir Oltean perout_request.flags |= PTP_PEROUT_DUTY_CYCLE; 4677570ebe0SVladimir Oltean perout_request.on.sec = pulsewidth / NSEC_PER_SEC; 4687570ebe0SVladimir Oltean perout_request.on.nsec = pulsewidth % NSEC_PER_SEC; 4697570ebe0SVladimir Oltean } 4707570ebe0SVladimir Oltean if (perout_phase >= 0) { 4717570ebe0SVladimir Oltean perout_request.flags |= PTP_PEROUT_PHASE; 4727570ebe0SVladimir Oltean perout_request.phase.sec = perout_phase / NSEC_PER_SEC; 4737570ebe0SVladimir Oltean perout_request.phase.nsec = perout_phase % NSEC_PER_SEC; 4747570ebe0SVladimir Oltean } else { 4757570ebe0SVladimir Oltean perout_request.start.sec = ts.tv_sec + 2; 4767570ebe0SVladimir Oltean perout_request.start.nsec = 0; 4777570ebe0SVladimir Oltean } 4787570ebe0SVladimir Oltean 4797570ebe0SVladimir Oltean if (ioctl(fd, PTP_PEROUT_REQUEST2, &perout_request)) { 4808dbbf854SShuah Khan perror("PTP_PEROUT_REQUEST"); 4818dbbf854SShuah Khan } else { 4828dbbf854SShuah Khan puts("periodic output request okay"); 4838dbbf854SShuah Khan } 4848dbbf854SShuah Khan } 4858dbbf854SShuah Khan 4868dbbf854SShuah Khan if (pps != -1) { 4878dbbf854SShuah Khan int enable = pps ? 1 : 0; 4888dbbf854SShuah Khan if (ioctl(fd, PTP_ENABLE_PPS, enable)) { 4898dbbf854SShuah Khan perror("PTP_ENABLE_PPS"); 4908dbbf854SShuah Khan } else { 4918dbbf854SShuah Khan puts("pps for system time request okay"); 4928dbbf854SShuah Khan } 4938dbbf854SShuah Khan } 4948dbbf854SShuah Khan 4958dbbf854SShuah Khan if (pct_offset) { 4968dbbf854SShuah Khan if (n_samples <= 0 || n_samples > 25) { 4978dbbf854SShuah Khan puts("n_samples should be between 1 and 25"); 4988dbbf854SShuah Khan usage(progname); 4998dbbf854SShuah Khan return -1; 5008dbbf854SShuah Khan } 5018dbbf854SShuah Khan 5028dbbf854SShuah Khan sysoff = calloc(1, sizeof(*sysoff)); 5038dbbf854SShuah Khan if (!sysoff) { 5048dbbf854SShuah Khan perror("calloc"); 5058dbbf854SShuah Khan return -1; 5068dbbf854SShuah Khan } 5078dbbf854SShuah Khan sysoff->n_samples = n_samples; 5088dbbf854SShuah Khan 5098dbbf854SShuah Khan if (ioctl(fd, PTP_SYS_OFFSET, sysoff)) 5108dbbf854SShuah Khan perror("PTP_SYS_OFFSET"); 5118dbbf854SShuah Khan else 5128dbbf854SShuah Khan puts("system and phc clock time offset request okay"); 5138dbbf854SShuah Khan 5148dbbf854SShuah Khan pct = &sysoff->ts[0]; 5158dbbf854SShuah Khan for (i = 0; i < sysoff->n_samples; i++) { 5168dbbf854SShuah Khan t1 = pctns(pct+2*i); 5178dbbf854SShuah Khan tp = pctns(pct+2*i+1); 5188dbbf854SShuah Khan t2 = pctns(pct+2*i+2); 5198dbbf854SShuah Khan interval = t2 - t1; 5208dbbf854SShuah Khan offset = (t2 + t1) / 2 - tp; 5218dbbf854SShuah Khan 52276a4c8b8SAlex Maftei printf("system time: %lld.%09u\n", 5238dbbf854SShuah Khan (pct+2*i)->sec, (pct+2*i)->nsec); 52476a4c8b8SAlex Maftei printf("phc time: %lld.%09u\n", 5258dbbf854SShuah Khan (pct+2*i+1)->sec, (pct+2*i+1)->nsec); 52676a4c8b8SAlex Maftei printf("system time: %lld.%09u\n", 5278dbbf854SShuah Khan (pct+2*i+2)->sec, (pct+2*i+2)->nsec); 5288dbbf854SShuah Khan printf("system/phc clock time offset is %" PRId64 " ns\n" 5298dbbf854SShuah Khan "system clock time delay is %" PRId64 " ns\n", 5308dbbf854SShuah Khan offset, interval); 5318dbbf854SShuah Khan } 5328dbbf854SShuah Khan 5338dbbf854SShuah Khan free(sysoff); 5348dbbf854SShuah Khan } 5358dbbf854SShuah Khan 5368dbbf854SShuah Khan close(fd); 5378dbbf854SShuah Khan return 0; 5388dbbf854SShuah Khan } 539