testptp.c 17 KB

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