]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - Documentation/ptp/testptp.c
Merge tag 'v3.13-rc1' into asoc-arizona
[mirror_ubuntu-bionic-kernel.git] / Documentation / ptp / testptp.c
1 /*
2 * PTP 1588 clock support - User space test program
3 *
4 * Copyright (C) 2010 OMICRON electronics GmbH
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <math.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/ioctl.h>
28 #include <sys/mman.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31 #include <sys/timex.h>
32 #include <sys/types.h>
33 #include <time.h>
34 #include <unistd.h>
35
36 #include <linux/ptp_clock.h>
37
38 #define DEVICE "/dev/ptp0"
39
40 #ifndef ADJ_SETOFFSET
41 #define ADJ_SETOFFSET 0x0100
42 #endif
43
44 #ifndef CLOCK_INVALID
45 #define CLOCK_INVALID -1
46 #endif
47
48 /* When glibc offers the syscall, this will go away. */
49 #include <sys/syscall.h>
50 static int clock_adjtime(clockid_t id, struct timex *tx)
51 {
52 return syscall(__NR_clock_adjtime, id, tx);
53 }
54
55 static clockid_t get_clockid(int fd)
56 {
57 #define CLOCKFD 3
58 #define FD_TO_CLOCKID(fd) ((~(clockid_t) (fd) << 3) | CLOCKFD)
59
60 return FD_TO_CLOCKID(fd);
61 }
62
63 static void handle_alarm(int s)
64 {
65 printf("received signal %d\n", s);
66 }
67
68 static int install_handler(int signum, void (*handler)(int))
69 {
70 struct sigaction action;
71 sigset_t mask;
72
73 /* Unblock the signal. */
74 sigemptyset(&mask);
75 sigaddset(&mask, signum);
76 sigprocmask(SIG_UNBLOCK, &mask, NULL);
77
78 /* Install the signal handler. */
79 action.sa_handler = handler;
80 action.sa_flags = 0;
81 sigemptyset(&action.sa_mask);
82 sigaction(signum, &action, NULL);
83
84 return 0;
85 }
86
87 static long ppb_to_scaled_ppm(int ppb)
88 {
89 /*
90 * The 'freq' field in the 'struct timex' is in parts per
91 * million, but with a 16 bit binary fractional field.
92 * Instead of calculating either one of
93 *
94 * scaled_ppm = (ppb / 1000) << 16 [1]
95 * scaled_ppm = (ppb << 16) / 1000 [2]
96 *
97 * we simply use double precision math, in order to avoid the
98 * truncation in [1] and the possible overflow in [2].
99 */
100 return (long) (ppb * 65.536);
101 }
102
103 static int64_t pctns(struct ptp_clock_time *t)
104 {
105 return t->sec * 1000000000LL + t->nsec;
106 }
107
108 static void usage(char *progname)
109 {
110 fprintf(stderr,
111 "usage: %s [options]\n"
112 " -a val request a one-shot alarm after 'val' seconds\n"
113 " -A val request a periodic alarm every 'val' seconds\n"
114 " -c query the ptp clock's capabilities\n"
115 " -d name device to open\n"
116 " -e val read 'val' external time stamp events\n"
117 " -f val adjust the ptp clock frequency by 'val' ppb\n"
118 " -g get the ptp clock time\n"
119 " -h prints this message\n"
120 " -k val measure the time offset between system and phc clock\n"
121 " for 'val' times (Maximum 25)\n"
122 " -p val enable output with a period of 'val' nanoseconds\n"
123 " -P val enable or disable (val=1|0) the system clock PPS\n"
124 " -s set the ptp clock time from the system time\n"
125 " -S set the system time from the ptp clock time\n"
126 " -t val shift the ptp clock time by 'val' seconds\n",
127 progname);
128 }
129
130 int main(int argc, char *argv[])
131 {
132 struct ptp_clock_caps caps;
133 struct ptp_extts_event event;
134 struct ptp_extts_request extts_request;
135 struct ptp_perout_request perout_request;
136 struct timespec ts;
137 struct timex tx;
138
139 static timer_t timerid;
140 struct itimerspec timeout;
141 struct sigevent sigevent;
142
143 struct ptp_clock_time *pct;
144 struct ptp_sys_offset *sysoff;
145
146
147 char *progname;
148 int i, c, cnt, fd;
149
150 char *device = DEVICE;
151 clockid_t clkid;
152 int adjfreq = 0x7fffffff;
153 int adjtime = 0;
154 int capabilities = 0;
155 int extts = 0;
156 int gettime = 0;
157 int oneshot = 0;
158 int pct_offset = 0;
159 int n_samples = 0;
160 int periodic = 0;
161 int perout = -1;
162 int pps = -1;
163 int settime = 0;
164
165 int64_t t1, t2, tp;
166 int64_t interval, offset;
167
168 progname = strrchr(argv[0], '/');
169 progname = progname ? 1+progname : argv[0];
170 while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghk:p:P:sSt:v"))) {
171 switch (c) {
172 case 'a':
173 oneshot = atoi(optarg);
174 break;
175 case 'A':
176 periodic = atoi(optarg);
177 break;
178 case 'c':
179 capabilities = 1;
180 break;
181 case 'd':
182 device = optarg;
183 break;
184 case 'e':
185 extts = atoi(optarg);
186 break;
187 case 'f':
188 adjfreq = atoi(optarg);
189 break;
190 case 'g':
191 gettime = 1;
192 break;
193 case 'k':
194 pct_offset = 1;
195 n_samples = atoi(optarg);
196 break;
197 case 'p':
198 perout = atoi(optarg);
199 break;
200 case 'P':
201 pps = atoi(optarg);
202 break;
203 case 's':
204 settime = 1;
205 break;
206 case 'S':
207 settime = 2;
208 break;
209 case 't':
210 adjtime = atoi(optarg);
211 break;
212 case 'h':
213 usage(progname);
214 return 0;
215 case '?':
216 default:
217 usage(progname);
218 return -1;
219 }
220 }
221
222 fd = open(device, O_RDWR);
223 if (fd < 0) {
224 fprintf(stderr, "opening %s: %s\n", device, strerror(errno));
225 return -1;
226 }
227
228 clkid = get_clockid(fd);
229 if (CLOCK_INVALID == clkid) {
230 fprintf(stderr, "failed to read clock id\n");
231 return -1;
232 }
233
234 if (capabilities) {
235 if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
236 perror("PTP_CLOCK_GETCAPS");
237 } else {
238 printf("capabilities:\n"
239 " %d maximum frequency adjustment (ppb)\n"
240 " %d programmable alarms\n"
241 " %d external time stamp channels\n"
242 " %d programmable periodic signals\n"
243 " %d pulse per second\n",
244 caps.max_adj,
245 caps.n_alarm,
246 caps.n_ext_ts,
247 caps.n_per_out,
248 caps.pps);
249 }
250 }
251
252 if (0x7fffffff != adjfreq) {
253 memset(&tx, 0, sizeof(tx));
254 tx.modes = ADJ_FREQUENCY;
255 tx.freq = ppb_to_scaled_ppm(adjfreq);
256 if (clock_adjtime(clkid, &tx)) {
257 perror("clock_adjtime");
258 } else {
259 puts("frequency adjustment okay");
260 }
261 }
262
263 if (adjtime) {
264 memset(&tx, 0, sizeof(tx));
265 tx.modes = ADJ_SETOFFSET;
266 tx.time.tv_sec = adjtime;
267 tx.time.tv_usec = 0;
268 if (clock_adjtime(clkid, &tx) < 0) {
269 perror("clock_adjtime");
270 } else {
271 puts("time shift okay");
272 }
273 }
274
275 if (gettime) {
276 if (clock_gettime(clkid, &ts)) {
277 perror("clock_gettime");
278 } else {
279 printf("clock time: %ld.%09ld or %s",
280 ts.tv_sec, ts.tv_nsec, ctime(&ts.tv_sec));
281 }
282 }
283
284 if (settime == 1) {
285 clock_gettime(CLOCK_REALTIME, &ts);
286 if (clock_settime(clkid, &ts)) {
287 perror("clock_settime");
288 } else {
289 puts("set time okay");
290 }
291 }
292
293 if (settime == 2) {
294 clock_gettime(clkid, &ts);
295 if (clock_settime(CLOCK_REALTIME, &ts)) {
296 perror("clock_settime");
297 } else {
298 puts("set time okay");
299 }
300 }
301
302 if (extts) {
303 memset(&extts_request, 0, sizeof(extts_request));
304 extts_request.index = 0;
305 extts_request.flags = PTP_ENABLE_FEATURE;
306 if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
307 perror("PTP_EXTTS_REQUEST");
308 extts = 0;
309 } else {
310 puts("external time stamp request okay");
311 }
312 for (; extts; extts--) {
313 cnt = read(fd, &event, sizeof(event));
314 if (cnt != sizeof(event)) {
315 perror("read");
316 break;
317 }
318 printf("event index %u at %lld.%09u\n", event.index,
319 event.t.sec, event.t.nsec);
320 fflush(stdout);
321 }
322 /* Disable the feature again. */
323 extts_request.flags = 0;
324 if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
325 perror("PTP_EXTTS_REQUEST");
326 }
327 }
328
329 if (oneshot) {
330 install_handler(SIGALRM, handle_alarm);
331 /* Create a timer. */
332 sigevent.sigev_notify = SIGEV_SIGNAL;
333 sigevent.sigev_signo = SIGALRM;
334 if (timer_create(clkid, &sigevent, &timerid)) {
335 perror("timer_create");
336 return -1;
337 }
338 /* Start the timer. */
339 memset(&timeout, 0, sizeof(timeout));
340 timeout.it_value.tv_sec = oneshot;
341 if (timer_settime(timerid, 0, &timeout, NULL)) {
342 perror("timer_settime");
343 return -1;
344 }
345 pause();
346 timer_delete(timerid);
347 }
348
349 if (periodic) {
350 install_handler(SIGALRM, handle_alarm);
351 /* Create a timer. */
352 sigevent.sigev_notify = SIGEV_SIGNAL;
353 sigevent.sigev_signo = SIGALRM;
354 if (timer_create(clkid, &sigevent, &timerid)) {
355 perror("timer_create");
356 return -1;
357 }
358 /* Start the timer. */
359 memset(&timeout, 0, sizeof(timeout));
360 timeout.it_interval.tv_sec = periodic;
361 timeout.it_value.tv_sec = periodic;
362 if (timer_settime(timerid, 0, &timeout, NULL)) {
363 perror("timer_settime");
364 return -1;
365 }
366 while (1) {
367 pause();
368 }
369 timer_delete(timerid);
370 }
371
372 if (perout >= 0) {
373 if (clock_gettime(clkid, &ts)) {
374 perror("clock_gettime");
375 return -1;
376 }
377 memset(&perout_request, 0, sizeof(perout_request));
378 perout_request.index = 0;
379 perout_request.start.sec = ts.tv_sec + 2;
380 perout_request.start.nsec = 0;
381 perout_request.period.sec = 0;
382 perout_request.period.nsec = perout;
383 if (ioctl(fd, PTP_PEROUT_REQUEST, &perout_request)) {
384 perror("PTP_PEROUT_REQUEST");
385 } else {
386 puts("periodic output request okay");
387 }
388 }
389
390 if (pps != -1) {
391 int enable = pps ? 1 : 0;
392 if (ioctl(fd, PTP_ENABLE_PPS, enable)) {
393 perror("PTP_ENABLE_PPS");
394 } else {
395 puts("pps for system time request okay");
396 }
397 }
398
399 if (pct_offset) {
400 if (n_samples <= 0 || n_samples > 25) {
401 puts("n_samples should be between 1 and 25");
402 usage(progname);
403 return -1;
404 }
405
406 sysoff = calloc(1, sizeof(*sysoff));
407 if (!sysoff) {
408 perror("calloc");
409 return -1;
410 }
411 sysoff->n_samples = n_samples;
412
413 if (ioctl(fd, PTP_SYS_OFFSET, sysoff))
414 perror("PTP_SYS_OFFSET");
415 else
416 puts("system and phc clock time offset request okay");
417
418 pct = &sysoff->ts[0];
419 for (i = 0; i < sysoff->n_samples; i++) {
420 t1 = pctns(pct+2*i);
421 tp = pctns(pct+2*i+1);
422 t2 = pctns(pct+2*i+2);
423 interval = t2 - t1;
424 offset = (t2 + t1) / 2 - tp;
425
426 printf("system time: %ld.%ld\n",
427 (pct+2*i)->sec, (pct+2*i)->nsec);
428 printf("phc time: %ld.%ld\n",
429 (pct+2*i+1)->sec, (pct+2*i+1)->nsec);
430 printf("system time: %ld.%ld\n",
431 (pct+2*i+2)->sec, (pct+2*i+2)->nsec);
432 printf("system/phc clock time offset is %ld ns\n"
433 "system clock time delay is %ld ns\n",
434 offset, interval);
435 }
436
437 free(sysoff);
438 }
439
440 close(fd);
441 return 0;
442 }