1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0 3 4ALL_TESTS=" 5 settime 6 adjtime 7 adjfreq 8" 9DEV=$1 10 11############################################################################## 12# Sanity checks 13 14if [[ "$(id -u)" -ne 0 ]]; then 15 echo "SKIP: need root privileges" 16 exit 0 17fi 18 19if [[ "$DEV" == "" ]]; then 20 echo "SKIP: PTP device not provided" 21 exit 0 22fi 23 24require_command() 25{ 26 local cmd=$1; shift 27 28 if [[ ! -x "$(command -v "$cmd")" ]]; then 29 echo "SKIP: $cmd not installed" 30 exit 1 31 fi 32} 33 34phc_sanity() 35{ 36 phc_ctl $DEV get &> /dev/null 37 38 if [ $? != 0 ]; then 39 echo "SKIP: unknown clock $DEV: No such device" 40 exit 1 41 fi 42} 43 44require_command phc_ctl 45phc_sanity 46 47############################################################################## 48# Helpers 49 50# Exit status to return at the end. Set in case one of the tests fails. 51EXIT_STATUS=0 52# Per-test return value. Clear at the beginning of each test. 53RET=0 54 55check_err() 56{ 57 local err=$1 58 59 if [[ $RET -eq 0 && $err -ne 0 ]]; then 60 RET=$err 61 fi 62} 63 64log_test() 65{ 66 local test_name=$1 67 68 if [[ $RET -ne 0 ]]; then 69 EXIT_STATUS=1 70 printf "TEST: %-60s [FAIL]\n" "$test_name" 71 return 1 72 fi 73 74 printf "TEST: %-60s [ OK ]\n" "$test_name" 75 return 0 76} 77 78tests_run() 79{ 80 local current_test 81 82 for current_test in ${TESTS:-$ALL_TESTS}; do 83 $current_test 84 done 85} 86 87############################################################################## 88# Tests 89 90settime_do() 91{ 92 local res 93 94 res=$(phc_ctl $DEV set 0 wait 120.5 get 2> /dev/null \ 95 | awk '/clock time is/{print $5}' \ 96 | awk -F. '{print $1}') 97 98 (( res == 120 )) 99} 100 101adjtime_do() 102{ 103 local res 104 105 res=$(phc_ctl $DEV set 0 adj 10 get 2> /dev/null \ 106 | awk '/clock time is/{print $5}' \ 107 | awk -F. '{print $1}') 108 109 (( res == 10 )) 110} 111 112adjfreq_do() 113{ 114 local res 115 116 # Set the clock to be 1% faster 117 res=$(phc_ctl $DEV freq 10000000 set 0 wait 100.5 get 2> /dev/null \ 118 | awk '/clock time is/{print $5}' \ 119 | awk -F. '{print $1}') 120 121 (( res == 101 )) 122} 123 124############################################################################## 125 126cleanup() 127{ 128 phc_ctl $DEV freq 0.0 &> /dev/null 129 phc_ctl $DEV set &> /dev/null 130} 131 132settime() 133{ 134 RET=0 135 136 settime_do 137 check_err $? 138 log_test "settime" 139 cleanup 140} 141 142adjtime() 143{ 144 RET=0 145 146 adjtime_do 147 check_err $? 148 log_test "adjtime" 149 cleanup 150} 151 152adjfreq() 153{ 154 RET=0 155 156 adjfreq_do 157 check_err $? 158 log_test "adjfreq" 159 cleanup 160} 161 162trap cleanup EXIT 163 164tests_run 165 166exit $EXIT_STATUS 167