^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #!/bin/bash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) # SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #exit status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #1: Internal error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #2: sysfs/debugfs not mount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #3: insert module fail when gpio-mockup is a module.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #4: Skip test including run as non-root user.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #5: other reason.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) SYSFS=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) GPIO_SYSFS=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) GPIO_DRV_SYSFS=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) DEBUGFS=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) GPIO_DEBUGFS=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) dev_type=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) module=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) # Kselftest framework requirement - SKIP code is 4.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) ksft_skip=4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) usage()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) echo "Usage:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) echo "$0 [-f] [-m name] [-t type]"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) echo "-f: full test. It maybe conflict with existence gpio device."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) echo "-m: module name, default name is gpio-mockup. It could also test"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) echo " other gpio device."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) echo "-t: interface type: chardev(char device) and sysfs(being"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) echo " deprecated). The first one is default"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) echo ""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) echo "$0 -h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) echo "This usage"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) prerequisite()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) msg="skip all tests:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if [ $UID != 0 ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) echo $msg must be run as root >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) exit $ksft_skip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if [ ! -d "$SYSFS" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) echo $msg sysfs is not mounted >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) exit 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) GPIO_SYSFS=`echo $SYSFS/class/gpio`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) GPIO_DRV_SYSFS=`echo $SYSFS/devices/platform/$module/gpio`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) DEBUGFS=`mount -t debugfs | head -1 | awk '{ print $3 }'`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if [ ! -d "$DEBUGFS" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) echo $msg debugfs is not mounted >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) exit 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) GPIO_DEBUGFS=`echo $DEBUGFS/gpio`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) source gpio-mockup-sysfs.sh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) try_insert_module()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if [ -d "$GPIO_DRV_SYSFS" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) echo "$GPIO_DRV_SYSFS exist. Skip insert module"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) modprobe -q $module $1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if [ X$? != X0 ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) echo $msg insmod $module failed >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) exit 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) remove_module()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) modprobe -r -q $module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) die()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) remove_module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) exit 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) test_chips()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if [ X$dev_type = Xsysfs ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) echo "WARNING: sysfs ABI of gpio is going to deprecated."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) test_chips_sysfs $*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) $BASE/gpio-mockup-chardev $*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) gpio_test()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) param=$1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) valid=$2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if [ X"$param" = X ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) die
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) try_insert_module "gpio_mockup_ranges=$param"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) echo -n "GPIO $module test with ranges: <"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) echo "$param>: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) printf "%-10s %s\n" $param
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) test_chips $module $valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) remove_module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) BASE=`dirname $0`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) dev_type=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) TEMP=`getopt -o fhm:t: -n '$0' -- "$@"`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if [ "$?" != "0" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) echo "Parameter process failed, Terminating..." >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) # Note the quotes around `$TEMP': they are essential!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) eval set -- "$TEMP"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) while true; do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) case $1 in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) -f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) full_test=true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) -h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) -m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) module=$2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) shift 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) -t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) dev_type=$2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) shift 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) --)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) break
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) echo "Internal error!"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) ;;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) esac
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if [ X"$module" = X ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) module="gpio-mockup"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if [ X$dev_type != Xsysfs ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) dev_type="chardev"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) prerequisite
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) echo "1. Test dynamic allocation of gpio successful means insert gpiochip and"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) echo " manipulate gpio pin successful"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) gpio_test "-1,32" true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) gpio_test "-1,32,-1,32" true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) gpio_test "-1,32,-1,32,-1,32" true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if [ X$full_test = Xtrue ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) gpio_test "-1,32,32,64" true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) gpio_test "-1,32,40,64,-1,5" true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) gpio_test "-1,32,32,64,-1,32" true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) gpio_test "0,32,32,64,-1,32,-1,32" true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) gpio_test "-1,32,-1,32,0,32,32,64" true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) echo "2. Do basic test: successful means insert gpiochip and"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) echo " manipulate gpio pin successful"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) gpio_test "0,32" true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) gpio_test "0,32,32,64" true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) gpio_test "0,32,40,64,64,96" true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) echo "3. Error test: successful means insert gpiochip failed"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) echo "3.1 Test number of gpio overflow"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) #Currently: The max number of gpio(1024) is defined in arm architecture.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) gpio_test "-1,32,-1,1024" false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if [ X$full_test = Xtrue ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) echo "3.2 Test zero line of gpio"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) gpio_test "0,0" false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) echo "3.3 Test range overlap"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) echo "3.3.1 Test corner case"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) gpio_test "0,32,0,1" false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) gpio_test "0,32,32,64,32,40" false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) gpio_test "0,32,35,64,35,45" false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) gpio_test "0,32,31,32" false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) gpio_test "0,32,32,64,36,37" false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) gpio_test "0,32,35,64,34,36" false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) echo "3.3.2 Test inserting invalid second gpiochip"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) gpio_test "0,32,30,35" false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) gpio_test "0,32,1,5" false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) gpio_test "10,32,9,14" false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) gpio_test "10,32,30,35" false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) echo "3.3.3 Test others"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) gpio_test "0,32,40,56,39,45" false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) gpio_test "0,32,40,56,30,33" false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) gpio_test "0,32,40,56,30,41" false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) gpio_test "0,32,40,56,20,21" false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) echo GPIO test PASS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)