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