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