summaryrefslogtreecommitdiff
path: root/src/ptp.c
blob: 65fbc52452a5ee2088bba0d4cc9595f92871c90a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Copyright (c) 2014 Stefan Sørensen <stefan.sorensen@spectralink.com>
 * Copyright (c) 2014-2015 Dmitry V. Levin <ldv@strace.io>
 * Copyright (c) 2014-2021 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "defs.h"

# include <linux/ioctl.h>
# include <linux/ptp_clock.h>

# include "xlat/ptp_extts_flags.h"
# include "xlat/ptp_perout_flags.h"
# include "xlat/ptp_pin_funcs.h"

static void
print_ptp_clock_time(const struct ptp_clock_time *const p, bool rtc)
{
	tprint_struct_begin();
	PRINT_FIELD_D(*p, sec);
	tprint_struct_next();
	PRINT_FIELD_U(*p, nsec);
	if (p->reserved) {
		tprint_struct_next();
		PRINT_FIELD_X(*p, reserved);
	}
	tprint_struct_end();

	if (rtc && xlat_verbose(xlat_verbosity) != XLAT_STYLE_RAW)
		tprints_comment(sprinttime_nsec(p->sec, p->nsec));
}

static bool
print_ptp_clock_time_am(struct tcb *tcp, void *elem_buf, size_t elem_size,
			void *data)
{
	print_ptp_clock_time(elem_buf, true);
	return true;
}

static bool
print_ptp_clock_time3_am(struct tcb *tcp, void *elem_buf, size_t elem_size,
			 void *data)
{
	const struct ptp_clock_time *const p = elem_buf;

	tprint_array_begin();
	print_ptp_clock_time(p, true);
	tprint_array_next();
	print_ptp_clock_time(p + 1, true);
	tprint_array_next();
	print_ptp_clock_time(p + 2, true);
	tprint_array_end();

	return true;
}

#define PRINT_RSV(where_, field_)					\
	do {								\
		if (!IS_ARRAY_ZERO(where_.field_)) {			\
			tprint_struct_next();				\
			PRINT_FIELD_ARRAY(where_, field_, tcp,		\
					  print_xint_array_member);	\
		}							\
	} while (0)

int
ptp_ioctl(struct tcb *const tcp, const unsigned int code,
	  const kernel_ulong_t arg)
{
	if (!verbose(tcp))
		return RVAL_DECODED;

	switch (code) {
	case PTP_CLOCK_GETCAPS:
	case PTP_CLOCK_GETCAPS2: {
		struct ptp_clock_caps caps;
		CHECK_TYPE_SIZE(caps.rsv, sizeof(unsigned int) * 12);
		CHECK_IOCTL_SIZE(PTP_CLOCK_GETCAPS, 80);
		CHECK_IOCTL_SIZE(PTP_CLOCK_GETCAPS2, 80);

		if (entering(tcp)) {
			tprint_arg_next();
			return 0;
		}

		if (umove_or_printaddr(tcp, arg, &caps))
			break;

		tprint_struct_begin();
		PRINT_FIELD_D(caps, max_adj);
		tprint_struct_next();
		PRINT_FIELD_D(caps, n_alarm);
		tprint_struct_next();
		PRINT_FIELD_D(caps, n_ext_ts);
		tprint_struct_next();
		PRINT_FIELD_D(caps, n_per_out);
		tprint_struct_next();
		PRINT_FIELD_D(caps, pps);
		tprint_struct_next();
		PRINT_FIELD_D(caps, n_pins);
		tprint_struct_next();
		PRINT_FIELD_D(caps, cross_timestamping);
		tprint_struct_next();
		PRINT_FIELD_D(caps, adjust_phase);
		PRINT_RSV(caps, rsv);
		tprint_struct_end();
		break;
	}

	case PTP_EXTTS_REQUEST:
	case PTP_EXTTS_REQUEST2: {
		struct ptp_extts_request extts;
		CHECK_TYPE_SIZE(extts.rsv, sizeof(unsigned int) * 2);
		CHECK_IOCTL_SIZE(PTP_EXTTS_REQUEST, 16);
		CHECK_IOCTL_SIZE(PTP_EXTTS_REQUEST2, 16);

		tprint_arg_next();
		if (umove_or_printaddr(tcp, arg, &extts))
			break;

		tprint_struct_begin();
		PRINT_FIELD_U(extts, index);
		tprint_struct_next();
		PRINT_FIELD_FLAGS(extts, flags, ptp_extts_flags, "PTP_???");
		if (code == PTP_EXTTS_REQUEST2)
			PRINT_RSV(extts, rsv);
		tprint_struct_end();
		break;
	}

	case PTP_PEROUT_REQUEST:
	case PTP_PEROUT_REQUEST2: {
		struct ptp_perout_request perout;
		CHECK_TYPE_SIZE(perout.rsv, sizeof(unsigned int) * 4);
		CHECK_IOCTL_SIZE(PTP_PEROUT_REQUEST, 56);
		CHECK_IOCTL_SIZE(PTP_PEROUT_REQUEST2, 56);

		tprint_arg_next();
		if (umove_or_printaddr(tcp, arg, &perout))
			break;

		tprint_struct_begin();
		if (perout.flags & PTP_PEROUT_PHASE) {
			PRINT_FIELD_OBJ_PTR(perout, phase, print_ptp_clock_time,
					    false);
		} else {
			PRINT_FIELD_OBJ_PTR(perout, start, print_ptp_clock_time,
					    true);
		}
		tprint_struct_next();
		PRINT_FIELD_OBJ_PTR(perout, period, print_ptp_clock_time,
				    false);
		tprint_struct_next();
		PRINT_FIELD_U(perout, index);
		tprint_struct_next();
		PRINT_FIELD_FLAGS(perout, flags, ptp_perout_flags,
				  "PTP_PEROUT_???");
		if (perout.flags & PTP_PEROUT_DUTY_CYCLE) {
			tprint_struct_next();
			PRINT_FIELD_OBJ_PTR(perout, on, print_ptp_clock_time,
					    false);
		} else if (code == PTP_PEROUT_REQUEST2) {
			PRINT_RSV(perout, rsv);
		}
		tprint_struct_end();
		break;
	}

	case PTP_ENABLE_PPS:
	case PTP_ENABLE_PPS2:
		tprint_arg_next();
		PRINT_VAL_X(arg);
		break;

	case PTP_SYS_OFFSET:
	case PTP_SYS_OFFSET2: {
		struct ptp_sys_offset sysoff;
		CHECK_TYPE_SIZE(sysoff.rsv, sizeof(unsigned int) * 3);
		CHECK_IOCTL_SIZE(PTP_SYS_OFFSET, 832);
		CHECK_IOCTL_SIZE(PTP_SYS_OFFSET2, 832);

		if (entering(tcp)) {
			tprint_arg_next();
			if (umove_or_printaddr(tcp, arg, &sysoff))
				break;

			tprint_struct_begin();
			PRINT_FIELD_U(sysoff, n_samples);
			PRINT_RSV(sysoff, rsv);
			return 0;
		} else {
			if (syserror(tcp)) {
				/* ... */
			} else if (!umove(tcp, arg, &sysoff)) {
				unsigned int n_samples =
					MIN(sysoff.n_samples, PTP_MAX_SAMPLES);
				tprint_struct_next();
				PRINT_FIELD_ARRAY_UPTO(sysoff, ts,
						       2 * n_samples + 1, tcp,
						       print_ptp_clock_time_am);
			} else {
				tprint_struct_next();
				tprint_unavailable();
			}
			tprint_struct_end();
			break;
		}
	}

	case PTP_PIN_GETFUNC:
	case PTP_PIN_GETFUNC2:
	case PTP_PIN_SETFUNC:
	case PTP_PIN_SETFUNC2: {
		struct ptp_pin_desc pinfunc;
		CHECK_TYPE_SIZE(pinfunc.rsv, sizeof(unsigned int) * 5);
		CHECK_IOCTL_SIZE(PTP_PIN_GETFUNC, 96);
		CHECK_IOCTL_SIZE(PTP_PIN_GETFUNC2, 96);
		CHECK_IOCTL_SIZE(PTP_PIN_SETFUNC, 96);
		CHECK_IOCTL_SIZE(PTP_PIN_SETFUNC2, 96);

		if (entering(tcp)) {
			tprint_arg_next();

			if (umove_or_printaddr(tcp, arg, &pinfunc))
				break;

			tprint_struct_begin();
			PRINT_FIELD_U(pinfunc, index);

			switch (code) {
			case PTP_PIN_GETFUNC2:
				PRINT_RSV(pinfunc, rsv);
				ATTRIBUTE_FALLTHROUGH;
			case PTP_PIN_GETFUNC:
				return 0;
			}
		} else /* getter syscall exit */ {
			if (syserror(tcp)) {
				tprint_struct_end();
				break;
			}

			if (umove(tcp, arg, &pinfunc) < 0) {
				tprint_arg_next();
				tprint_unavailable();
				tprint_struct_end();
				break;
			}
		}

		/* setter syscall enter or getter syscall exit */
		switch (code) {
		case PTP_PIN_GETFUNC:
		case PTP_PIN_GETFUNC2:
			tprint_struct_next();
			PRINT_FIELD_CSTRING(pinfunc, name);
		}
		tprint_struct_next();
		PRINT_FIELD_XVAL(pinfunc, func, ptp_pin_funcs, "PTP_PF_???");
		tprint_struct_next();
		PRINT_FIELD_U(pinfunc, chan);
		if (code == PTP_PIN_SETFUNC2) {
			PRINT_RSV(pinfunc, rsv);
		}
		tprint_struct_end();
		break;
	}

	case PTP_SYS_OFFSET_PRECISE:
	case PTP_SYS_OFFSET_PRECISE2: {
		struct ptp_sys_offset_precise sysoff;
		CHECK_TYPE_SIZE(sysoff.rsv, sizeof(unsigned int) * 4);
		CHECK_IOCTL_SIZE(PTP_SYS_OFFSET_PRECISE, 64);
		CHECK_IOCTL_SIZE(PTP_SYS_OFFSET_PRECISE2, 64);

		if (entering(tcp)) {
			tprint_arg_next();
			return 0;
		}

		if (umove_or_printaddr(tcp, arg, &sysoff))
			break;

		tprint_struct_begin();
		PRINT_FIELD_OBJ_PTR(sysoff, device, print_ptp_clock_time, true);
		tprint_struct_next();
		PRINT_FIELD_OBJ_PTR(sysoff, sys_realtime, print_ptp_clock_time,
				    true);
		tprint_struct_next();
		PRINT_FIELD_OBJ_PTR(sysoff, sys_monoraw, print_ptp_clock_time,
				    false);
		PRINT_RSV(sysoff, rsv);
		tprint_struct_end();
		break;
	}

	case PTP_SYS_OFFSET_EXTENDED:
	case PTP_SYS_OFFSET_EXTENDED2: {
		struct ptp_sys_offset_extended sysoff;
		CHECK_TYPE_SIZE(sysoff.rsv, sizeof(unsigned int) * 3);
		CHECK_IOCTL_SIZE(PTP_SYS_OFFSET_EXTENDED, 1216);
		CHECK_IOCTL_SIZE(PTP_SYS_OFFSET_EXTENDED2, 1216);

		if (entering(tcp)) {
			tprint_arg_next();
			if (umove_or_printaddr(tcp, arg, &sysoff))
				break;

			tprint_struct_begin();
			PRINT_FIELD_U(sysoff, n_samples);
			PRINT_RSV(sysoff, rsv);
			return 0;
		}

		if (syserror(tcp)) {
			/* ... */
		} else if (!umove(tcp, arg, &sysoff)) {
			unsigned int n_samples =
				MIN(sysoff.n_samples, PTP_MAX_SAMPLES);
			tprint_struct_next();
			PRINT_FIELD_ARRAY_UPTO(sysoff, ts,
					       n_samples, tcp,
					       print_ptp_clock_time3_am);
		} else {
			tprint_struct_next();
			tprint_unavailable();
		}
		tprint_struct_end();
		break;
	}

	default:
		return RVAL_DECODED;
	}

	return RVAL_IOCTL_DECODED;
}