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