hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/tools/testing/selftests/ptp/testptp.c
....@@ -1,21 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * PTP 1588 clock support - User space test program
34 *
45 * 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.
196 */
207 #define _GNU_SOURCE
218 #define __SANE_USERSPACE_TYPES__ /* For PPC64, to get LL64 types */
....@@ -48,6 +35,8 @@
4835 #define CLOCK_INVALID -1
4936 #endif
5037
38
+#define NSEC_PER_SEC 1000000000LL
39
+
5140 /* clock_adjtime is not available in GLIBC < 2.14 */
5241 #if !__GLIBC_PREREQ(2, 14)
5342 #include <sys/syscall.h>
....@@ -57,34 +46,50 @@
5746 }
5847 #endif
5948
49
+static void show_flag_test(int rq_index, unsigned int flags, int err)
50
+{
51
+ printf("PTP_EXTTS_REQUEST%c flags 0x%08x : (%d) %s\n",
52
+ rq_index ? '1' + rq_index : ' ',
53
+ flags, err, strerror(errno));
54
+ /* sigh, uClibc ... */
55
+ errno = 0;
56
+}
57
+
58
+static void do_flag_test(int fd, unsigned int index)
59
+{
60
+ struct ptp_extts_request extts_request;
61
+ unsigned long request[2] = {
62
+ PTP_EXTTS_REQUEST,
63
+ PTP_EXTTS_REQUEST2,
64
+ };
65
+ unsigned int enable_flags[5] = {
66
+ PTP_ENABLE_FEATURE,
67
+ PTP_ENABLE_FEATURE | PTP_RISING_EDGE,
68
+ PTP_ENABLE_FEATURE | PTP_FALLING_EDGE,
69
+ PTP_ENABLE_FEATURE | PTP_RISING_EDGE | PTP_FALLING_EDGE,
70
+ PTP_ENABLE_FEATURE | (PTP_EXTTS_VALID_FLAGS + 1),
71
+ };
72
+ int err, i, j;
73
+
74
+ memset(&extts_request, 0, sizeof(extts_request));
75
+ extts_request.index = index;
76
+
77
+ for (i = 0; i < 2; i++) {
78
+ for (j = 0; j < 5; j++) {
79
+ extts_request.flags = enable_flags[j];
80
+ err = ioctl(fd, request[i], &extts_request);
81
+ show_flag_test(i, extts_request.flags, err);
82
+
83
+ extts_request.flags = 0;
84
+ err = ioctl(fd, request[i], &extts_request);
85
+ }
86
+ }
87
+}
88
+
6089 static clockid_t get_clockid(int fd)
6190 {
6291 #define CLOCKFD 3
6392 return (((unsigned int) ~fd) << 3) | CLOCKFD;
64
-}
65
-
66
-static void handle_alarm(int s)
67
-{
68
- printf("received signal %d\n", s);
69
-}
70
-
71
-static int install_handler(int signum, void (*handler)(int))
72
-{
73
- struct sigaction action;
74
- sigset_t mask;
75
-
76
- /* Unblock the signal. */
77
- sigemptyset(&mask);
78
- sigaddset(&mask, signum);
79
- sigprocmask(SIG_UNBLOCK, &mask, NULL);
80
-
81
- /* Install the signal handler. */
82
- action.sa_handler = handler;
83
- action.sa_flags = 0;
84
- sigemptyset(&action.sa_mask);
85
- sigaction(signum, &action, NULL);
86
-
87
- return 0;
8893 }
8994
9095 static long ppb_to_scaled_ppm(int ppb)
....@@ -112,8 +117,6 @@
112117 {
113118 fprintf(stderr,
114119 "usage: %s [options]\n"
115
- " -a val request a one-shot alarm after 'val' seconds\n"
116
- " -A val request a periodic alarm every 'val' seconds\n"
117120 " -c query the ptp clock's capabilities\n"
118121 " -d name device to open\n"
119122 " -e val read 'val' external time stamp events\n"
....@@ -131,11 +134,14 @@
131134 " 1 - external time stamp\n"
132135 " 2 - periodic output\n"
133136 " -p val enable output with a period of 'val' nanoseconds\n"
137
+ " -H val set output phase to 'val' nanoseconds (requires -p)\n"
138
+ " -w val set output pulse width to 'val' nanoseconds (requires -p)\n"
134139 " -P val enable or disable (val=1|0) the system clock PPS\n"
135140 " -s set the ptp clock time from the system time\n"
136141 " -S set the system time from the ptp clock time\n"
137142 " -t val shift the ptp clock time by 'val' seconds\n"
138
- " -T val set the ptp clock time to 'val' seconds\n",
143
+ " -T val set the ptp clock time to 'val' seconds\n"
144
+ " -z test combinations of rising/falling external time stamp flags\n",
139145 progname);
140146 }
141147
....@@ -148,14 +154,8 @@
148154 struct ptp_pin_desc desc;
149155 struct timespec ts;
150156 struct timex tx;
151
-
152
- static timer_t timerid;
153
- struct itimerspec timeout;
154
- struct sigevent sigevent;
155
-
156157 struct ptp_clock_time *pct;
157158 struct ptp_sys_offset *sysoff;
158
-
159159
160160 char *progname;
161161 unsigned int i;
....@@ -167,14 +167,12 @@
167167 int adjtime = 0;
168168 int capabilities = 0;
169169 int extts = 0;
170
+ int flagtest = 0;
170171 int gettime = 0;
171172 int index = 0;
172173 int list_pins = 0;
173
- int oneshot = 0;
174174 int pct_offset = 0;
175175 int n_samples = 0;
176
- int periodic = 0;
177
- int perout = -1;
178176 int pin_index = -1, pin_func;
179177 int pps = -1;
180178 int seconds = 0;
....@@ -182,17 +180,14 @@
182180
183181 int64_t t1, t2, tp;
184182 int64_t interval, offset;
183
+ int64_t perout_phase = -1;
184
+ int64_t pulsewidth = -1;
185
+ int64_t perout = -1;
185186
186187 progname = strrchr(argv[0], '/');
187188 progname = progname ? 1+progname : argv[0];
188
- while (EOF != (c = getopt(argc, argv, "a:A:cd:e:f:ghi:k:lL:p:P:sSt:T:v"))) {
189
+ while (EOF != (c = getopt(argc, argv, "cd:e:f:ghH:i:k:lL:p:P:sSt:T:w:z"))) {
189190 switch (c) {
190
- case 'a':
191
- oneshot = atoi(optarg);
192
- break;
193
- case 'A':
194
- periodic = atoi(optarg);
195
- break;
196191 case 'c':
197192 capabilities = 1;
198193 break;
....@@ -207,6 +202,9 @@
207202 break;
208203 case 'g':
209204 gettime = 1;
205
+ break;
206
+ case 'H':
207
+ perout_phase = atoll(optarg);
210208 break;
211209 case 'i':
212210 index = atoi(optarg);
....@@ -226,7 +224,7 @@
226224 }
227225 break;
228226 case 'p':
229
- perout = atoi(optarg);
227
+ perout = atoll(optarg);
230228 break;
231229 case 'P':
232230 pps = atoi(optarg);
....@@ -243,6 +241,12 @@
243241 case 'T':
244242 settime = 3;
245243 seconds = atoi(optarg);
244
+ break;
245
+ case 'w':
246
+ pulsewidth = atoi(optarg);
247
+ break;
248
+ case 'z':
249
+ flagtest = 1;
246250 break;
247251 case 'h':
248252 usage(progname);
....@@ -277,14 +281,16 @@
277281 " %d programmable periodic signals\n"
278282 " %d pulse per second\n"
279283 " %d programmable pins\n"
280
- " %d cross timestamping\n",
284
+ " %d cross timestamping\n"
285
+ " %d adjust_phase\n",
281286 caps.max_adj,
282287 caps.n_alarm,
283288 caps.n_ext_ts,
284289 caps.n_per_out,
285290 caps.pps,
286291 caps.n_pins,
287
- caps.cross_timestamping);
292
+ caps.cross_timestamping,
293
+ caps.adjust_phase);
288294 }
289295 }
290296
....@@ -375,6 +381,10 @@
375381 }
376382 }
377383
384
+ if (flagtest) {
385
+ do_flag_test(fd, index);
386
+ }
387
+
378388 if (list_pins) {
379389 int n_pins = 0;
380390 if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) {
....@@ -393,47 +403,14 @@
393403 }
394404 }
395405
396
- if (oneshot) {
397
- install_handler(SIGALRM, handle_alarm);
398
- /* Create a timer. */
399
- sigevent.sigev_notify = SIGEV_SIGNAL;
400
- sigevent.sigev_signo = SIGALRM;
401
- if (timer_create(clkid, &sigevent, &timerid)) {
402
- perror("timer_create");
403
- return -1;
404
- }
405
- /* Start the timer. */
406
- memset(&timeout, 0, sizeof(timeout));
407
- timeout.it_value.tv_sec = oneshot;
408
- if (timer_settime(timerid, 0, &timeout, NULL)) {
409
- perror("timer_settime");
410
- return -1;
411
- }
412
- pause();
413
- timer_delete(timerid);
406
+ if (pulsewidth >= 0 && perout < 0) {
407
+ puts("-w can only be specified together with -p");
408
+ return -1;
414409 }
415410
416
- if (periodic) {
417
- install_handler(SIGALRM, handle_alarm);
418
- /* Create a timer. */
419
- sigevent.sigev_notify = SIGEV_SIGNAL;
420
- sigevent.sigev_signo = SIGALRM;
421
- if (timer_create(clkid, &sigevent, &timerid)) {
422
- perror("timer_create");
423
- return -1;
424
- }
425
- /* Start the timer. */
426
- memset(&timeout, 0, sizeof(timeout));
427
- timeout.it_interval.tv_sec = periodic;
428
- timeout.it_value.tv_sec = periodic;
429
- if (timer_settime(timerid, 0, &timeout, NULL)) {
430
- perror("timer_settime");
431
- return -1;
432
- }
433
- while (1) {
434
- pause();
435
- }
436
- timer_delete(timerid);
411
+ if (perout_phase >= 0 && perout < 0) {
412
+ puts("-H can only be specified together with -p");
413
+ return -1;
437414 }
438415
439416 if (perout >= 0) {
....@@ -443,11 +420,24 @@
443420 }
444421 memset(&perout_request, 0, sizeof(perout_request));
445422 perout_request.index = index;
446
- perout_request.start.sec = ts.tv_sec + 2;
447
- perout_request.start.nsec = 0;
448
- perout_request.period.sec = 0;
449
- perout_request.period.nsec = perout;
450
- if (ioctl(fd, PTP_PEROUT_REQUEST, &perout_request)) {
423
+ perout_request.period.sec = perout / NSEC_PER_SEC;
424
+ perout_request.period.nsec = perout % NSEC_PER_SEC;
425
+ perout_request.flags = 0;
426
+ if (pulsewidth >= 0) {
427
+ perout_request.flags |= PTP_PEROUT_DUTY_CYCLE;
428
+ perout_request.on.sec = pulsewidth / NSEC_PER_SEC;
429
+ perout_request.on.nsec = pulsewidth % NSEC_PER_SEC;
430
+ }
431
+ if (perout_phase >= 0) {
432
+ perout_request.flags |= PTP_PEROUT_PHASE;
433
+ perout_request.phase.sec = perout_phase / NSEC_PER_SEC;
434
+ perout_request.phase.nsec = perout_phase % NSEC_PER_SEC;
435
+ } else {
436
+ perout_request.start.sec = ts.tv_sec + 2;
437
+ perout_request.start.nsec = 0;
438
+ }
439
+
440
+ if (ioctl(fd, PTP_PEROUT_REQUEST2, &perout_request)) {
451441 perror("PTP_PEROUT_REQUEST");
452442 } else {
453443 puts("periodic output request okay");