2 # SPDX-License-Identifier: GPL-2.0
7 #4: skip test - including run as non-root user
22 # Kselftest return codes
29 echo "$0 [-frv] [-t type]"
30 echo "-f: full test (minimal set run by default)"
31 echo "-r: test random lines as well as fence posts"
32 echo "-t: interface type:"
33 echo " cdev (character device ABI) - default"
34 echo " cdev_v1 (deprecated character device ABI)"
35 echo " sysfs (deprecated SYSFS ABI)"
36 echo "-v: verbose progress reporting"
43 echo "GPIO $module test SKIP"
49 [ $
(id
-u) -eq 0 ] || skip
"must be run as root"
51 DEBUGFS
=$
(grep -w debugfs
/proc
/mounts | cut
-f2 -d' ')
52 [ -d "$DEBUGFS" ] || skip
"debugfs is not mounted"
54 GPIO_DEBUGFS
=$DEBUGFS/$module
59 modprobe
-r -q $module
67 jobs -p |
xargs -r kill > /dev
/null
2>&1
72 echo "test failed: $*" >&2
73 echo "GPIO $module test FAIL"
79 modprobe
-q $module "$1" || fail
"insert $module failed with error $?"
84 [ -z "$verbose" ] ||
echo "$*"
87 # The following line helpers, release_Line, get_line and set_line, all
88 # make use of the global $chip and $offset variables.
90 # This implementation drives the GPIO character device (cdev) uAPI.
91 # Other implementations may override these to test different uAPIs.
93 # Release any resources related to the line
96 [ "$line_set_pid" ] && kill $line_set_pid && wait $line_set_pid || true
100 # Read the current value of the line
105 local cdev_opts
=${uapi_opt}${active_opt}
106 $BASE/gpio-mockup-cdev
$cdev_opts /dev
/$chip $offset
110 # Set the state of the line
112 # Changes to line configuration are provided as parameters.
113 # The line is assumed to be an output if the line value 0 or 1 is
114 # specified, else an input.
121 # parse config options...
134 bias_opt
="-bpull-down "
137 bias_opt
="-bpull-up "
148 local cdev_opts
=${uapi_opt}${active_opt}
150 $BASE/gpio-mockup-cdev
$cdev_opts -s$val /dev
/$chip $offset &
151 # failure to set is detected by reading mockup and toggling values
153 # allow for gpio-mockup-cdev to launch and request line
154 # (there is limited value in checking if line has been requested)
156 elif [ "$bias_opt" ]; then
157 cdev_opts
=${cdev_opts}${bias_opt}
158 $BASE/gpio-mockup-cdev
$cdev_opts /dev
/$chip $offset || true
165 # don't need any retry here as set_mock allows for propagation
167 [ "$val" = "$1" ] || fail
"line value is ${val:-empty} when $1 was expected"
170 # The following mockup helpers all make use of the $mock_line
173 local backoff_wait
=10
176 # retry allows for set propagation from uAPI to mockup
179 [ "$val" = "$1" ] && break
181 [ $retry -lt 5 ] || fail
"mockup $mock_line value ${val:-empty} when $1 expected"
182 sleep $
(printf "%0.2f" $
((backoff_wait
))e-3
)
183 backoff_wait
=$
((backoff_wait
* 2))
189 echo "$1" > $mock_line
190 # allow for set propagation - so we won't be in a race with set_line
194 # test the functionality of a line
196 # The line is set from the mockup side and is read from the userspace side
197 # (input), and is set from the userspace side and is read from the mockup side
200 # Setting the mockup pull using the userspace interface bias settings is
201 # tested where supported by the userspace interface (cdev).
206 log
"test_line $chip $offset"
207 mock_line
=$GPIO_DEBUGFS/$chip/$offset
208 [ -e "$mock_line" ] || fail
"missing line $chip:$offset"
210 # test input active-high
212 set_line input active-high
219 if [ "$full_test" ]; then
220 if [ "$dev_type" != "sysfs" ]; then
223 set_line input pull-up
229 set_line input pull-down
237 # test input active-low
246 # test output active-high
248 set_line active-high
0
256 # test output active-low
258 set_line active-low
0
270 log test_no_line
"$*"
271 [ ! -e "$GPIO_DEBUGFS/$1/$2" ] || fail
"unexpected line $1:$2"
274 # Load the module and check that the expected number of gpiochips, with the
275 # expected number of lines, are created and are functional.
277 # $1 is the gpio_mockup_ranges parameter for the module
278 # The remaining parameters are the number of lines, n, expected for each of
279 # the gpiochips expected to be created.
281 # For each gpiochip the fence post lines, 0 and n-1, are tested, and the
282 # line on the far side of the fence post, n, is tested to not exist.
284 # If the $random flag is set then a random line in the middle of the
285 # gpiochip is tested as well.
292 [ "${1:-}" ] || fail
"missing ranges"
294 try_insert_module
"gpio_mockup_ranges=$ranges"
295 log
"GPIO $module test with ranges: <$ranges>:"
296 # e.g. /sys/kernel/debug/gpio-mockup/gpiochip1
297 gpiochip
=$
(find "$DEBUGFS/$module/" -name gpiochip
* -type d |
sort)
298 for chip
in $gpiochip; do
300 [ "${1:-}" ] || fail
"unexpected chip - $gc"
303 if [ "$random" -a $width -gt 2 ]; then
304 test_line
$gc $
((RANDOM
% ($width - 2) + 1))
306 test_line
$gc $
(($width - 1))
307 test_no_line
$gc $width
309 [ "${1:-}" ] && fail
"missing expected chip of width $1"
310 remove_module || fail
"failed to remove module with error $?"
313 while getopts ":frvt:" opt
; do
332 shift $
((OPTIND
- 1))
334 [ "${1:-}" ] && fail
"unknown argument '$1'"
338 trap 'exit $ksft_fail' SIGTERM SIGINT
343 source $BASE/gpio-mockup-sysfs.sh
344 echo "WARNING: gpio sysfs ABI is deprecated."
347 echo "WARNING: gpio cdev ABI v1 is deprecated."
353 fail
"unknown interface type: $dev_type"
357 remove_module || fail
"can't remove existing $module module"
359 # manual gpio allocation tests fail if a physical chip already exists
360 [ "$full_test" -a -e "/dev/gpiochip0" ] && skip
"full tests conflict with gpiochip0"
362 echo "1. Module load tests"
363 echo "1.1. dynamic allocation of gpio"
364 insmod_test
"-1,32" 32
365 insmod_test
"-1,23,-1,32" 23 32
366 insmod_test
"-1,23,-1,26,-1,32" 23 26 32
367 if [ "$full_test" ]; then
368 echo "1.2. manual allocation of gpio"
369 insmod_test
"0,32" 32
370 insmod_test
"0,32,32,60" 32 28
371 insmod_test
"0,32,40,64,64,96" 32 24 32
372 echo "1.3. dynamic and manual allocation of gpio"
373 insmod_test
"-1,32,32,62" 32 30
374 insmod_test
"-1,22,-1,23,0,24,32,64" 22 23 24 32
375 insmod_test
"-1,32,32,60,-1,29" 32 28 29
376 insmod_test
"-1,32,40,64,-1,5" 32 24 5
377 insmod_test
"0,32,32,44,-1,22,-1,31" 32 12 22 31
379 echo "2. Module load error tests"
380 echo "2.1 no lines defined"
382 if [ "$full_test" ]; then
383 echo "2.2 ignore range overlap"
384 insmod_test
"0,32,0,1" 32
385 insmod_test
"0,32,1,5" 32
386 insmod_test
"0,32,30,35" 32
387 insmod_test
"0,32,31,32" 32
388 insmod_test
"10,32,30,35" 22
389 insmod_test
"10,32,9,14" 22
390 insmod_test
"0,32,20,21,40,56" 32 16
391 insmod_test
"0,32,32,64,32,40" 32 32
392 insmod_test
"0,32,32,64,36,37" 32 32
393 insmod_test
"0,32,35,64,34,36" 32 29
394 insmod_test
"0,30,35,64,35,45" 30 29
395 insmod_test
"0,32,40,56,30,33" 32 16
396 insmod_test
"0,32,40,56,30,41" 32 16
397 insmod_test
"0,32,40,56,39,45" 32 16
400 echo "GPIO $module test PASS"