summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/ptp/phc.sh
blob: 9f61c1579edf098b23a12d9b17e270021b039b58 (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
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0

ALL_TESTS="
	settime
	adjtime
	adjfreq
"
DEV=$1

# Kselftest framework requirement - SKIP code is 4.
ksft_skip=4

##############################################################################
# Sanity checks

if [[ "$(id -u)" -ne 0 ]]; then
	echo "SKIP: need root privileges"
	exit $ksft_skip
fi

if [[ "$DEV" == "" ]]; then
	echo "SKIP: PTP device not provided"
	exit $ksft_skip
fi

require_command()
{
	local cmd=$1; shift

	if [[ ! -x "$(command -v "$cmd")" ]]; then
		echo "SKIP: $cmd not installed"
		exit $ksft_skip
	fi
}

phc_sanity()
{
	phc_ctl $DEV get &> /dev/null

	if [ $? != 0 ]; then
		echo "SKIP: unknown clock $DEV: No such device"
		exit $ksft_skip
	fi
}

require_command phc_ctl
phc_sanity

##############################################################################
# Helpers

# Exit status to return at the end. Set in case one of the tests fails.
EXIT_STATUS=0
PASS_COUNT=0
# Per-test return value. Clear at the beginning of each test.
RET=0

check_err()
{
	local err=$1

	if [[ $RET -eq 0 && $err -ne 0 ]]; then
		RET=$err
	fi
}

log_test()
{
	local test_name=$1

	if [[ $RET -eq $ksft_skip ]]; then
		printf "TEST: %-60s  [SKIP]\n" "$test_name"
		return 0
	fi

	if [[ $RET -ne 0 ]]; then
		EXIT_STATUS=1
		printf "TEST: %-60s  [FAIL]\n" "$test_name"
		return 1
	fi

	((PASS_COUNT++))
	printf "TEST: %-60s  [ OK ]\n" "$test_name"
	return 0
}

tests_run()
{
	local current_test

	for current_test in ${TESTS:-$ALL_TESTS}; do
		$current_test
	done
}

##############################################################################
# Tests

settime_do()
{
	local res out

	out=$(LC_ALL=C phc_ctl $DEV set 0 wait 120.5 get 2>&1)
	if [[ $? -ne 0 ]]; then
		if echo "$out" | grep -qi "Operation not supported"; then
			return $ksft_skip
		fi
		return 1
	fi
	res=$(echo "$out" | awk '/clock time is/{print $5}' | awk -F. '{print $1}')

	(( res == 120 ))
}

adjtime_do()
{
	local res out

	out=$(LC_ALL=C phc_ctl $DEV set 0 adj 10 get 2>&1)
	if [[ $? -ne 0 ]]; then
		if echo "$out" | grep -qi "Operation not supported"; then
			return $ksft_skip
		fi
		return 1
	fi
	res=$(echo "$out" | awk '/clock time is/{print $5}' | awk -F. '{print $1}')

	(( res == 10 ))
}

adjfreq_do()
{
	local res out

	# Set the clock to be 1% faster
	out=$(LC_ALL=C phc_ctl $DEV freq 10000000 set 0 wait 100.5 get 2>&1)
	if [[ $? -ne 0 ]]; then
		if echo "$out" | grep -qi "Operation not supported"; then
			return $ksft_skip
		fi
		return 1
	fi
	res=$(echo "$out" | awk '/clock time is/{print $5}' | awk -F. '{print $1}')

	(( res == 101 ))
}

##############################################################################

cleanup()
{
	phc_ctl $DEV freq 0.0 &> /dev/null
	phc_ctl $DEV set &> /dev/null
}

settime()
{
	RET=0

	settime_do
	check_err $?
	log_test "settime"
	cleanup
}

adjtime()
{
	RET=0

	adjtime_do
	check_err $?
	log_test "adjtime"
	cleanup
}

adjfreq()
{
	RET=0

	adjfreq_do
	check_err $?
	log_test "adjfreq"
	cleanup
}

trap cleanup EXIT

tests_run

if [[ $EXIT_STATUS -eq 0 && $PASS_COUNT -eq 0 ]]; then
	exit $ksft_skip
fi
exit $EXIT_STATUS