gpio-mockup.sh 4.0 KB
Newer Older
1
#!/bin/bash
2
# SPDX-License-Identifier: GPL-2.0
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

#exit status
#1: run as non-root user
#2: sysfs/debugfs not mount
#3: insert module fail when gpio-mockup is a module.
#4: other reason.

SYSFS=
GPIO_SYSFS=
GPIO_DRV_SYSFS=
DEBUGFS=
GPIO_DEBUGFS=
dev_type=
module=

usage()
{
	echo "Usage:"
	echo "$0 [-f] [-m name] [-t type]"
	echo "-f:  full test. It maybe conflict with existence gpio device."
	echo "-m:  module name, default name is gpio-mockup. It could also test"
	echo "     other gpio device."
	echo "-t:  interface type: chardev(char device) and sysfs(being"
	echo "     deprecated). The first one is default"
	echo ""
	echo "$0 -h"
	echo "This usage"
}

prerequisite()
{
	msg="skip all tests:"
	if [ $UID != 0 ]; then
		echo $msg must be run as root >&2
		exit 1
	fi
	SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'`
	if [ ! -d "$SYSFS" ]; then
		echo $msg sysfs is not mounted >&2
		exit 2
	fi
	GPIO_SYSFS=`echo $SYSFS/class/gpio`
	GPIO_DRV_SYSFS=`echo $SYSFS/devices/platform/$module/gpio`
	DEBUGFS=`mount -t debugfs | head -1 | awk '{ print $3 }'`
	if [ ! -d "$DEBUGFS" ]; then
		echo $msg debugfs is not mounted >&2
		exit 2
	fi
	GPIO_DEBUGFS=`echo $DEBUGFS/gpio`
	source gpio-mockup-sysfs.sh
}

try_insert_module()
{
	if [ -d "$GPIO_DRV_SYSFS" ]; then
		echo "$GPIO_DRV_SYSFS exist. Skip insert module"
	else
		modprobe -q $module $1
		if [ X$? != X0 ]; then
			echo $msg insmod $module failed >&2
			exit 3
		fi
	fi
}

remove_module()
{
	modprobe -r -q $module
}

die()
{
	remove_module
	exit 4
}

test_chips()
{
	if [ X$dev_type = Xsysfs ]; then
		echo "WARNING: sysfs ABI of gpio is going to deprecated."
		test_chips_sysfs $*
	else
		$BASE/gpio-mockup-chardev $*
	fi
}

gpio_test()
{
	param=$1
	valid=$2

	if [ X"$param" = X ]; then
		die
	fi
	try_insert_module "gpio_mockup_ranges=$param"
	echo -n "GPIO $module test with ranges: <"
	echo "$param>: "
	printf "%-10s %s\n" $param
	test_chips $module $valid
	remove_module
}

BASE=`dirname $0`

dev_type=
TEMP=`getopt -o fhm:t: -n '$0' -- "$@"`

if [ "$?" != "0" ]; then
        echo "Parameter process failed, Terminating..." >&2
        exit 1
fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

while true; do
	case $1 in
	-f)
		full_test=true
		shift
		;;
	-h)
		usage
		exit
		;;
	-m)
		module=$2
		shift 2
		;;
	-t)
		dev_type=$2
		shift 2
		;;
	--)
		shift
		break
		;;
	*)
		echo "Internal error!"
		exit 1
		;;
	esac
done

if [ X"$module" = X ]; then
	module="gpio-mockup"
fi

if [ X$dev_type != Xsysfs ]; then
	dev_type="chardev"
fi

prerequisite

echo "1.  Test dynamic allocation of gpio successful means insert gpiochip and"
echo "    manipulate gpio pin successful"
gpio_test "-1,32" true
gpio_test "-1,32,-1,32" true
gpio_test "-1,32,-1,32,-1,32" true
if [ X$full_test = Xtrue ]; then
	gpio_test "-1,32,32,64" true
	gpio_test "-1,32,40,64,-1,5" true
	gpio_test "-1,32,32,64,-1,32" true
	gpio_test "0,32,32,64,-1,32,-1,32" true
	gpio_test "-1,32,-1,32,0,32,32,64" true
	echo "2.  Do basic test: successful means insert gpiochip and"
	echo "    manipulate gpio pin successful"
	gpio_test "0,32" true
	gpio_test "0,32,32,64" true
	gpio_test "0,32,40,64,64,96" true
fi
echo "3.  Error test: successful means insert gpiochip failed"
echo "3.1 Test number of gpio overflow"
#Currently: The max number of gpio(1024) is defined in arm architecture.
gpio_test "-1,32,-1,1024" false
if [ X$full_test = Xtrue ]; then
	echo "3.2 Test zero line of gpio"
	gpio_test "0,0" false
	echo "3.3 Test range overlap"
	echo "3.3.1 Test corner case"
	gpio_test "0,32,0,1" false
	gpio_test "0,32,32,64,32,40" false
	gpio_test "0,32,35,64,35,45" false
	gpio_test "0,32,31,32" false
	gpio_test "0,32,32,64,36,37" false
	gpio_test "0,32,35,64,34,36" false
	echo "3.3.2 Test inserting invalid second gpiochip"
	gpio_test "0,32,30,35" false
	gpio_test "0,32,1,5" false
	gpio_test "10,32,9,14" false
	gpio_test "10,32,30,35" false
	echo "3.3.3 Test others"
	gpio_test "0,32,40,56,39,45" false
	gpio_test "0,32,40,56,30,33" false
	gpio_test "0,32,40,56,30,41" false
	gpio_test "0,32,40,56,20,21" false
fi

echo GPIO test PASS