GNU Linux-libre 4.14.313-gnu1
[releases.git] / tools / testing / selftests / gpio / gpio-mockup.sh
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
10 SYSFS=
11 GPIO_SYSFS=
12 GPIO_DRV_SYSFS=
13 DEBUGFS=
14 GPIO_DEBUGFS=
15 dev_type=
16 module=
17
18 usage()
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
32 prerequisite()
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
55 try_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
68 remove_module()
69 {
70         modprobe -r -q $module
71 }
72
73 die()
74 {
75         remove_module
76         exit 4
77 }
78
79 test_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
89 gpio_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
105 BASE=`dirname $0`
106
107 dev_type=
108 TEMP=`getopt -o fhm:t: -n '$0' -- "$@"`
109
110 if [ "$?" != "0" ]; then
111         echo "Parameter process failed, Terminating..." >&2
112         exit 1
113 fi
114
115 # Note the quotes around `$TEMP': they are essential!
116 eval set -- "$TEMP"
117
118 while 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
145 done
146
147 if [ X"$module" = X ]; then
148         module="gpio-mockup"
149 fi
150
151 if [ X$dev_type != Xsysfs ]; then
152         dev_type="chardev"
153 fi
154
155 prerequisite
156
157 echo "1.  Test dynamic allocation of gpio successful means insert gpiochip and"
158 echo "    manipulate gpio pin successful"
159 gpio_test "-1,32" true
160 gpio_test "-1,32,-1,32" true
161 gpio_test "-1,32,-1,32,-1,32" true
162 if [ 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
173 fi
174 echo "3.  Error test: successful means insert gpiochip failed"
175 echo "3.1 Test number of gpio overflow"
176 #Currently: The max number of gpio(1024) is defined in arm architecture.
177 gpio_test "-1,32,-1,1024" false
178 if [ 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
199 fi
200
201 echo GPIO test PASS
202