License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-block.git] / tools / testing / selftests / gpio / gpio-mockup-sysfs.sh
1
2 # SPDX-License-Identifier: GPL-2.0
3 is_consistent()
4 {
5         val=
6
7         active_low_sysfs=`cat $GPIO_SYSFS/gpio$nr/active_low`
8         val_sysfs=`cat $GPIO_SYSFS/gpio$nr/value`
9         dir_sysfs=`cat $GPIO_SYSFS/gpio$nr/direction`
10
11         gpio_this_debugfs=`cat $GPIO_DEBUGFS |grep "gpio-$nr" | sed "s/(.*)//g"`
12         dir_debugfs=`echo $gpio_this_debugfs | awk '{print $2}'`
13         val_debugfs=`echo $gpio_this_debugfs | awk '{print $3}'`
14         if [ $val_debugfs = "lo" ]; then
15                 val=0
16         elif [ $val_debugfs = "hi" ]; then
17                 val=1
18         fi
19
20         if [ $active_low_sysfs = "1" ]; then
21                 if [ $val = "0" ]; then
22                         val="1"
23                 else
24                         val="0"
25                 fi
26         fi
27
28         if [ $val_sysfs = $val ] && [ $dir_sysfs = $dir_debugfs ]; then
29                 echo -n "."
30         else
31                 echo "test fail, exit"
32                 die
33         fi
34 }
35
36 test_pin_logic()
37 {
38         nr=$1
39         direction=$2
40         active_low=$3
41         value=$4
42
43         echo $direction > $GPIO_SYSFS/gpio$nr/direction
44         echo $active_low > $GPIO_SYSFS/gpio$nr/active_low
45         if [ $direction = "out" ]; then
46                 echo $value > $GPIO_SYSFS/gpio$nr/value
47         fi
48         is_consistent $nr
49 }
50
51 test_one_pin()
52 {
53         nr=$1
54
55         echo -n "test pin<$nr>"
56
57         echo $nr > $GPIO_SYSFS/export 2>/dev/null
58
59         if [ X$? != X0 ]; then
60                 echo "test GPIO pin $nr failed"
61                 die
62         fi
63
64         #"Checking if the sysfs is consistent with debugfs: "
65         is_consistent $nr
66
67         #"Checking the logic of active_low: "
68         test_pin_logic $nr out 1 1
69         test_pin_logic $nr out 1 0
70         test_pin_logic $nr out 0 1
71         test_pin_logic $nr out 0 0
72
73         #"Checking the logic of direction: "
74         test_pin_logic $nr in 1 1
75         test_pin_logic $nr out 1 0
76         test_pin_logic $nr low 0 1
77         test_pin_logic $nr high 0 0
78
79         echo $nr > $GPIO_SYSFS/unexport
80
81         echo "successful"
82 }
83
84 test_one_pin_fail()
85 {
86         nr=$1
87
88         echo $nr > $GPIO_SYSFS/export 2>/dev/null
89
90         if [ X$? != X0 ]; then
91                 echo "test invalid pin $nr successful"
92         else
93                 echo "test invalid pin $nr failed"
94                 echo $nr > $GPIO_SYSFS/unexport 2>/dev/null
95                 die
96         fi
97 }
98
99 list_chip()
100 {
101         echo `ls -d $GPIO_DRV_SYSFS/gpiochip* 2>/dev/null`
102 }
103
104 test_chip()
105 {
106         chip=$1
107         name=`basename $chip`
108         base=`cat $chip/base`
109         ngpio=`cat $chip/ngpio`
110         printf "%-10s %-5s %-5s\n" $name $base $ngpio
111         if [ $ngpio = "0" ]; then
112                 echo "number of gpio is zero is not allowed".
113         fi
114         test_one_pin $base
115         test_one_pin $(($base + $ngpio - 1))
116         test_one_pin $((( RANDOM % $ngpio )  + $base ))
117 }
118
119 test_chips_sysfs()
120 {
121        gpiochip=`list_chip $module`
122        if [ X"$gpiochip" = X ]; then
123                if [ X"$valid" = Xfalse ]; then
124                        echo "successful"
125                else
126                        echo "fail"
127                        die
128                fi
129        else
130                for chip in $gpiochip; do
131                        test_chip $chip
132                done
133        fi
134 }
135