]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - Documentation/ptp/testptp.c
Merge tag 'drm/tegra/for-3.15-rc1' of git://anongit.freedesktop.org/tegra/linux into...
[mirror_ubuntu-artful-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 " -i val index for event/trigger\n"
121 " -k val measure the time offset between system and phc clock\n"
122 " for 'val' times (Maximum 25)\n"
123 " -p val enable output with a period of 'val' nanoseconds\n"
124 " -P val enable or disable (val=1|0) the system clock PPS\n"
125 " -s set the ptp clock time from the system time\n"
126 " -S set the system time from the ptp clock time\n"
127 " -t val shift the ptp clock time by 'val' seconds\n",
128 progname);
129 }
130
131 int main(int argc, char *argv[])
132 {
133 struct ptp_clock_caps caps;
134 struct ptp_extts_event event;
135 struct ptp_extts_request extts_request;
136 struct ptp_perout_request perout_request;
137 struct timespec ts;
138 struct timex tx;
139
140 static timer_t timerid;
141 struct itimerspec timeout;
142 struct sigevent sigevent;
143
144 struct ptp_clock_time *pct;
145 struct ptp_sys_offset *sysoff;
146
147
148 char *progname;
149 int i, c, cnt, fd;
150
151 char *device = DEVICE;
152 clockid_t clkid;
153 int adjfreq = 0x7fffffff;
154 int adjtime = 0;
155 int capabilities = 0;
156 int extts = 0;
157 int gettime = 0;
158 int index = 0;
159 int oneshot = 0;
160 int pct_offset = 0;
161 int n_samples = 0;
162 int periodic = 0;
163 int perout = -1;
164 int pps = -1;
165 int settime = 0;
166
167 int64_t t1, t2, tp;
168 int64_t interval, offset;
169
170 progname = strrchr(argv[0], '/');
171 progname = progname ? 1+progname : argv[0];
172 while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghi:k:p:P:sSt:v"))) {
173 switch (c) {
174 case 'a':
175 oneshot = atoi(optarg);
176 break;
177 case 'A':
178 periodic = atoi(optarg);
179 break;
180 case 'c':
181 capabilities = 1;
182 break;
183 case 'd':
184 device = optarg;
185 break;
186 case 'e':
187 extts = atoi(optarg);
188 break;
189 case 'f':
190 adjfreq = atoi(optarg);
191 break;
192 case 'g':
193 gettime = 1;
194 break;
195 case 'i':
196 index = atoi(optarg);
197 break;
198 case 'k':
199 pct_offset = 1;
200 n_samples = atoi(optarg);
201 break;
202 case 'p':
203 perout = atoi(optarg);
204 break;
205 case 'P':
206 pps = atoi(optarg);
207 break;
208 case 's':
209 settime = 1;
210 break;
211 case 'S':
212 settime = 2;
213 break;
214 case 't':
215 adjtime = atoi(optarg);
216 break;
217 case 'h':
218 usage(progname);
219 return 0;
220 case '?':
221 default:
222 usage(progname);
223 return -1;
224 }
225 }
226
227 fd = open(device, O_RDWR);
228 if (fd < 0) {
229 fprintf(stderr, "opening %s: %s\n", device, strerror(errno));
230 return -1;
231 }
232
233 clkid = get_clockid(fd);
234 if (CLOCK_INVALID == clkid) {
235 fprintf(stderr, "failed to read clock id\n");
236 return -1;
237 }
238
239 if (capabilities) {
240 if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
241 perror("PTP_CLOCK_GETCAPS");
242 } else {
243 printf("capabilities:\n"
244 " %d maximum frequency adjustment (ppb)\n"
245 " %d programmable alarms\n"
246 " %d external time stamp channels\n"
247 " %d programmable periodic signals\n"
248 " %d pulse per second\n",
249 caps.max_adj,
250 caps.n_alarm,
251 caps.n_ext_ts,
252 caps.n_per_out,
253 caps.pps);
254 }
255 }
256
257 if (0x7fffffff != adjfreq) {
258 memset(&tx, 0, sizeof(tx));
259 tx.modes = ADJ_FREQUENCY;
260 tx.freq = ppb_to_scaled_ppm(adjfreq);
261 if (clock_adjtime(clkid, &tx)) {
262 perror("clock_adjtime");
263 } else {
264 puts("frequency adjustment okay");
265 }
266 }
267
268 if (adjtime) {
269 memset(&tx, 0, sizeof(tx));
270 tx.modes = ADJ_SETOFFSET;
271 tx.time.tv_sec = adjtime;
272 tx.time.tv_usec = 0;
273 if (clock_adjtime(clkid, &tx) < 0) {
274 perror("clock_adjtime");
275 } else {
276 puts("time shift okay");
277 }
278 }
279
280 if (gettime) {
281 if (clock_gettime(clkid, &ts)) {
282 perror("clock_gettime");
283 } else {
284 printf("clock time: %ld.%09ld or %s",
285 ts.tv_sec, ts.tv_nsec, ctime(&ts.tv_sec));
286 }
287 }
288
289 if (settime == 1) {
290 clock_gettime(CLOCK_REALTIME, &ts);
291 if (clock_settime(clkid, &ts)) {
292 perror("clock_settime");
293 } else {
294 puts("set time okay");
295 }
296 }
297
298 if (settime == 2) {
299 clock_gettime(clkid, &ts);
300 if (clock_settime(CLOCK_REALTIME, &ts)) {
301 perror("clock_settime");
302 } else {
303 puts("set time okay");
304 }
305 }
306
307 if (extts) {
308 memset(&extts_request, 0, sizeof(extts_request));
309 extts_request.index = index;
310 extts_request.flags = PTP_ENABLE_FEATURE;
311 if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
312 perror("PTP_EXTTS_REQUEST");
313 extts = 0;
314 } else {
315 puts("external time stamp request okay");
316 }
317 for (; extts; extts--) {
318 cnt = read(fd, &event, sizeof(event));
319 if (cnt != sizeof(event)) {
320 perror("read");
321 break;
322 }
323 printf("event index %u at %lld.%09u\n", event.index,
324 event.t.sec, event.t.nsec);
325 fflush(stdout);
326 }
327 /* Disable the feature again. */
328 extts_request.flags = 0;
329 if (ioctl(fd, PTP_EXTTS_REQUEST, &extts_request)) {
330 perror("PTP_EXTTS_REQUEST");
331 }
332 }
333
334 if (oneshot) {
335 install_handler(SIGALRM, handle_alarm);
336 /* Create a timer. */
337 sigevent.sigev_notify = SIGEV_SIGNAL;
338 sigevent.sigev_signo = SIGALRM;
339 if (timer_create(clkid, &sigevent, &timerid)) {
340 perror("timer_create");
341 return -1;
342 }
343 /* Start the timer. */
344 memset(&timeout, 0, sizeof(timeout));
345 timeout.it_value.tv_sec = oneshot;
346 if (timer_settime(timerid, 0, &timeout, NULL)) {
347 perror("timer_settime");
348 return -1;
349 }
350 pause();
351 timer_delete(timerid);
352 }
353
354 if (periodic) {
355 install_handler(SIGALRM, handle_alarm);
356 /* Create a timer. */
357 sigevent.sigev_notify = SIGEV_SIGNAL;
358 sigevent.sigev_signo = SIGALRM;
359 if (timer_create(clkid, &sigevent, &timerid)) {
360 perror("timer_create");
361 return -1;
362 }
363 /* Start the timer. */
364 memset(&timeout, 0, sizeof(timeout));
365 timeout.it_interval.tv_sec = periodic;
366 timeout.it_value.tv_sec = periodic;
367 if (timer_settime(timerid, 0, &timeout, NULL)) {
368 perror("timer_settime");
369 return -1;
370 }
371 while (1) {
372 pause();
373 }
374 timer_delete(timerid);
375 }
376
377 if (perout >= 0) {
378 if (clock_gettime(clkid, &ts)) {
379 perror("clock_gettime");
380 return -1;
381 }
382 memset(&perout_request, 0, sizeof(perout_request));
383 perout_request.index = index;
384 perout_request.start.sec = ts.tv_sec + 2;
385 perout_request.start.nsec = 0;
386 perout_request.period.sec = 0;
387 perout_request.period.nsec = perout;
388 if (ioctl(fd, PTP_PEROUT_REQUEST, &perout_request)) {
389 perror("PTP_PEROUT_REQUEST");
390 } else {
391 puts("periodic output request okay");
392 }
393 }
394
395 if (pps != -1) {
396 int enable = pps ? 1 : 0;
397 if (ioctl(fd, PTP_ENABLE_PPS, enable)) {
398 perror("PTP_ENABLE_PPS");
399 } else {
400 puts("pps for system time request okay");
401 }
402 }
403
404 if (pct_offset) {
405 if (n_samples <= 0 || n_samples > 25) {
406 puts("n_samples should be between 1 and 25");
407 usage(progname);
408 return -1;
409 }
410
411 sysoff = calloc(1, sizeof(*sysoff));
412 if (!sysoff) {
413 perror("calloc");
414 return -1;
415 }
416 sysoff->n_samples = n_samples;
417
418 if (ioctl(fd, PTP_SYS_OFFSET, sysoff))
419 perror("PTP_SYS_OFFSET");
420 else
421 puts("system and phc clock time offset request okay");
422
423 pct = &sysoff->ts[0];
424 for (i = 0; i < sysoff->n_samples; i++) {
425 t1 = pctns(pct+2*i);
426 tp = pctns(pct+2*i+1);
427 t2 = pctns(pct+2*i+2);
428 interval = t2 - t1;
429 offset = (t2 + t1) / 2 - tp;
430
431 printf("system time: %ld.%ld\n",
432 (pct+2*i)->sec, (pct+2*i)->nsec);
433 printf("phc time: %ld.%ld\n",
434 (pct+2*i+1)->sec, (pct+2*i+1)->nsec);
435 printf("system time: %ld.%ld\n",
436 (pct+2*i+2)->sec, (pct+2*i+2)->nsec);
437 printf("system/phc clock time offset is %ld ns\n"
438 "system clock time delay is %ld ns\n",
439 offset, interval);
440 }
441
442 free(sysoff);
443 }
444
445 close(fd);
446 return 0;
447 }