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