Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftest/gpio: add gpio test case

This test script try to do whitebox testing for gpio subsystem(based on
gpiolib). It manipulate gpio device through chardev or sysfs and check
the result from debugfs. This script test gpio-mockup through chardev by
default. User could test other gpio chip by passing the module name.
Some of the testcases are turned off by default to avoid the conflicting
with gpiochip in system.

In details, it test the following things:
1. Test direction and output value for valid pin.
2. Test dynamic allocation of gpio base.
3. Add single, multi gpiochip to do overlap check.

Run "tools/testing/selftests/gpio/gpio-mockup.sh -h" for usage.

Acked-by: Shuah Khan <shuahkh@osg.samsung.com>
Signed-off-by: Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>
Signed-off-by: Shuah Khan <shuahkh@osg.samsung.com>

authored by

Bamvor Jian Zhang and committed by
Shuah Khan
22f6592b 981c3db6

+683
+1
tools/testing/selftests/Makefile
··· 6 6 TARGETS += firmware 7 7 TARGETS += ftrace 8 8 TARGETS += futex 9 + TARGETS += gpio 9 10 TARGETS += ipc 10 11 TARGETS += kcmp 11 12 TARGETS += lib
+23
tools/testing/selftests/gpio/Makefile
··· 1 + 2 + TEST_PROGS := gpio-mockup.sh 3 + TEST_FILES := gpio-mockup-sysfs.sh $(BINARIES) 4 + BINARIES := gpio-mockup-chardev 5 + 6 + include ../lib.mk 7 + 8 + all: $(BINARIES) 9 + 10 + clean: 11 + $(RM) $(BINARIES) 12 + 13 + CFLAGS += -O2 -g -std=gnu99 -Wall -I../../../../usr/include/ 14 + LDLIBS += -lmount -I/usr/include/libmount 15 + 16 + $(BINARIES): ../../../gpio/gpio-utils.o ../../../../usr/include/linux/gpio.h 17 + 18 + ../../../gpio/gpio-utils.o: 19 + make ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C ../../../gpio 20 + 21 + ../../../../usr/include/linux/gpio.h: 22 + make -C ../../../.. headers_install INSTALL_HDR_PATH=$(shell pwd)/../../../../usr/ 23 +
+324
tools/testing/selftests/gpio/gpio-mockup-chardev.c
··· 1 + /* 2 + * GPIO chardev test helper 3 + * 4 + * Copyright (C) 2016 Bamvor Jian Zhang 5 + * 6 + * This program is free software; you can redistribute it and/or modify it 7 + * under the terms of the GNU General Public License version 2 as published by 8 + * the Free Software Foundation. 9 + */ 10 + 11 + #define _GNU_SOURCE 12 + #include <unistd.h> 13 + #include <stdio.h> 14 + #include <stdlib.h> 15 + #include <stdio.h> 16 + #include <errno.h> 17 + #include <string.h> 18 + #include <fcntl.h> 19 + #include <getopt.h> 20 + #include <sys/ioctl.h> 21 + #include <libmount.h> 22 + #include <err.h> 23 + #include <dirent.h> 24 + #include <linux/gpio.h> 25 + #include "../../../gpio/gpio-utils.h" 26 + 27 + #define CONSUMER "gpio-selftest" 28 + #define GC_NUM 10 29 + enum direction { 30 + OUT, 31 + IN 32 + }; 33 + 34 + static int get_debugfs(char **path) 35 + { 36 + struct libmnt_context *cxt; 37 + struct libmnt_table *tb; 38 + struct libmnt_iter *itr = NULL; 39 + struct libmnt_fs *fs; 40 + int found = 0; 41 + 42 + cxt = mnt_new_context(); 43 + if (!cxt) 44 + err(EXIT_FAILURE, "libmount context allocation failed"); 45 + 46 + itr = mnt_new_iter(MNT_ITER_FORWARD); 47 + if (!itr) 48 + err(EXIT_FAILURE, "failed to initialize libmount iterator"); 49 + 50 + if (mnt_context_get_mtab(cxt, &tb)) 51 + err(EXIT_FAILURE, "failed to read mtab"); 52 + 53 + while (mnt_table_next_fs(tb, itr, &fs) == 0) { 54 + const char *type = mnt_fs_get_fstype(fs); 55 + 56 + if (!strcmp(type, "debugfs")) { 57 + found = 1; 58 + break; 59 + } 60 + } 61 + if (found) 62 + asprintf(path, "%s/gpio", mnt_fs_get_target(fs)); 63 + 64 + mnt_free_iter(itr); 65 + mnt_free_context(cxt); 66 + 67 + if (!found) 68 + return -1; 69 + 70 + return 0; 71 + } 72 + 73 + static int gpio_debugfs_get(const char *consumer, int *dir, int *value) 74 + { 75 + char *debugfs; 76 + FILE *f; 77 + char *line = NULL; 78 + size_t len = 0; 79 + char *cur; 80 + int found = 0; 81 + 82 + if (get_debugfs(&debugfs) != 0) 83 + err(EXIT_FAILURE, "debugfs is not mounted"); 84 + 85 + f = fopen(debugfs, "r"); 86 + if (!f) 87 + err(EXIT_FAILURE, "read from gpio debugfs failed"); 88 + 89 + /* 90 + * gpio-2 ( |gpio-selftest ) in lo 91 + */ 92 + while (getline(&line, &len, f) != -1) { 93 + cur = strstr(line, consumer); 94 + if (cur == NULL) 95 + continue; 96 + 97 + cur = strchr(line, ')'); 98 + if (!cur) 99 + continue; 100 + 101 + cur += 2; 102 + if (!strncmp(cur, "out", 3)) { 103 + *dir = OUT; 104 + cur += 4; 105 + } else if (!strncmp(cur, "in", 2)) { 106 + *dir = IN; 107 + cur += 4; 108 + } 109 + 110 + if (!strncmp(cur, "hi", 2)) 111 + *value = 1; 112 + else if (!strncmp(cur, "lo", 2)) 113 + *value = 0; 114 + 115 + found = 1; 116 + break; 117 + } 118 + free(debugfs); 119 + fclose(f); 120 + free(line); 121 + 122 + if (!found) 123 + return -1; 124 + 125 + return 0; 126 + } 127 + 128 + static struct gpiochip_info *list_gpiochip(const char *gpiochip_name, int *ret) 129 + { 130 + struct gpiochip_info *cinfo; 131 + struct gpiochip_info *current; 132 + const struct dirent *ent; 133 + DIR *dp; 134 + char *chrdev_name; 135 + int fd; 136 + int i = 0; 137 + 138 + cinfo = calloc(sizeof(struct gpiochip_info) * 4, GC_NUM + 1); 139 + if (!cinfo) 140 + err(EXIT_FAILURE, "gpiochip_info allocation failed"); 141 + 142 + current = cinfo; 143 + dp = opendir("/dev"); 144 + if (!dp) { 145 + *ret = -errno; 146 + goto error_out; 147 + } else { 148 + *ret = 0; 149 + } 150 + 151 + while (ent = readdir(dp), ent) { 152 + if (check_prefix(ent->d_name, "gpiochip")) { 153 + *ret = asprintf(&chrdev_name, "/dev/%s", ent->d_name); 154 + if (*ret < 0) 155 + goto error_out; 156 + 157 + fd = open(chrdev_name, 0); 158 + if (fd == -1) { 159 + *ret = -errno; 160 + fprintf(stderr, "Failed to open %s\n", 161 + chrdev_name); 162 + goto error_close_dir; 163 + } 164 + *ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, current); 165 + if (*ret == -1) { 166 + perror("Failed to issue CHIPINFO IOCTL\n"); 167 + goto error_close_dir; 168 + } 169 + close(fd); 170 + if (strcmp(current->label, gpiochip_name) == 0 171 + || check_prefix(current->label, gpiochip_name)) { 172 + *ret = 0; 173 + current++; 174 + i++; 175 + } 176 + } 177 + } 178 + 179 + if ((!*ret && i == 0) || *ret < 0) { 180 + free(cinfo); 181 + cinfo = NULL; 182 + } 183 + if (!*ret && i > 0) { 184 + cinfo = realloc(cinfo, sizeof(struct gpiochip_info) * 4 * i); 185 + *ret = i; 186 + } 187 + 188 + error_close_dir: 189 + closedir(dp); 190 + error_out: 191 + if (*ret < 0) 192 + err(EXIT_FAILURE, "list gpiochip failed: %s", strerror(*ret)); 193 + 194 + return cinfo; 195 + } 196 + 197 + int gpio_pin_test(struct gpiochip_info *cinfo, int line, int flag, int value) 198 + { 199 + struct gpiohandle_data data; 200 + unsigned int lines[] = {line}; 201 + int fd; 202 + int debugfs_dir = IN; 203 + int debugfs_value = 0; 204 + int ret; 205 + 206 + data.values[0] = value; 207 + ret = gpiotools_request_linehandle(cinfo->name, lines, 1, flag, &data, 208 + CONSUMER); 209 + if (ret < 0) 210 + goto fail_out; 211 + else 212 + fd = ret; 213 + 214 + ret = gpio_debugfs_get(CONSUMER, &debugfs_dir, &debugfs_value); 215 + if (ret) { 216 + ret = -EINVAL; 217 + goto fail_out; 218 + } 219 + if (flag & GPIOHANDLE_REQUEST_INPUT) { 220 + if (debugfs_dir != IN) { 221 + errno = -EINVAL; 222 + ret = -errno; 223 + } 224 + } else if (flag & GPIOHANDLE_REQUEST_OUTPUT) { 225 + if (flag & GPIOHANDLE_REQUEST_ACTIVE_LOW) 226 + debugfs_value = !debugfs_value; 227 + 228 + if (!(debugfs_dir == OUT && value == debugfs_value)) 229 + errno = -EINVAL; 230 + ret = -errno; 231 + 232 + } 233 + gpiotools_release_linehandle(fd); 234 + 235 + fail_out: 236 + if (ret) 237 + err(EXIT_FAILURE, "gpio<%s> line<%d> test flag<0x%x> value<%d>", 238 + cinfo->name, line, flag, value); 239 + 240 + return ret; 241 + } 242 + 243 + void gpio_pin_tests(struct gpiochip_info *cinfo, unsigned int line) 244 + { 245 + printf("line<%d>", line); 246 + gpio_pin_test(cinfo, line, GPIOHANDLE_REQUEST_OUTPUT, 0); 247 + printf("."); 248 + gpio_pin_test(cinfo, line, GPIOHANDLE_REQUEST_OUTPUT, 1); 249 + printf("."); 250 + gpio_pin_test(cinfo, line, 251 + GPIOHANDLE_REQUEST_OUTPUT | GPIOHANDLE_REQUEST_ACTIVE_LOW, 252 + 0); 253 + printf("."); 254 + gpio_pin_test(cinfo, line, 255 + GPIOHANDLE_REQUEST_OUTPUT | GPIOHANDLE_REQUEST_ACTIVE_LOW, 256 + 1); 257 + printf("."); 258 + gpio_pin_test(cinfo, line, GPIOHANDLE_REQUEST_INPUT, 0); 259 + printf("."); 260 + } 261 + 262 + /* 263 + * ./gpio-mockup-chardev gpio_chip_name_prefix is_valid_gpio_chip 264 + * Return 0 if successful or exit with EXIT_FAILURE if test failed. 265 + * gpio_chip_name_prefix: The prefix of gpiochip you want to test. E.g. 266 + * gpio-mockup 267 + * is_valid_gpio_chip: Whether the gpio_chip is valid. 1 means valid, 268 + * 0 means invalid which could not be found by 269 + * list_gpiochip. 270 + */ 271 + int main(int argc, char *argv[]) 272 + { 273 + char *prefix; 274 + int valid; 275 + struct gpiochip_info *cinfo; 276 + struct gpiochip_info *current; 277 + int i; 278 + int ret; 279 + 280 + if (argc < 3) { 281 + printf("Usage: %s prefix is_valid", argv[0]); 282 + exit(EXIT_FAILURE); 283 + } 284 + 285 + prefix = argv[1]; 286 + valid = strcmp(argv[2], "true") == 0 ? 1 : 0; 287 + 288 + printf("Test gpiochip %s: ", prefix); 289 + cinfo = list_gpiochip(prefix, &ret); 290 + if (!cinfo) { 291 + if (!valid && ret == 0) { 292 + printf("Invalid test successful\n"); 293 + ret = 0; 294 + goto out; 295 + } else { 296 + ret = -EINVAL; 297 + goto out; 298 + } 299 + } else if (cinfo && !valid) { 300 + ret = -EINVAL; 301 + goto out; 302 + } 303 + current = cinfo; 304 + for (i = 0; i < ret; i++) { 305 + gpio_pin_tests(current, 0); 306 + gpio_pin_tests(current, current->lines - 1); 307 + gpio_pin_tests(current, random() % current->lines); 308 + current++; 309 + } 310 + ret = 0; 311 + printf("successful\n"); 312 + 313 + out: 314 + if (ret) 315 + fprintf(stderr, "gpio<%s> test failed\n", prefix); 316 + 317 + if (cinfo) 318 + free(cinfo); 319 + 320 + if (ret) 321 + exit(EXIT_FAILURE); 322 + 323 + return ret; 324 + }
+134
tools/testing/selftests/gpio/gpio-mockup-sysfs.sh
··· 1 + 2 + is_consistent() 3 + { 4 + val= 5 + 6 + active_low_sysfs=`cat $GPIO_SYSFS/gpio$nr/active_low` 7 + val_sysfs=`cat $GPIO_SYSFS/gpio$nr/value` 8 + dir_sysfs=`cat $GPIO_SYSFS/gpio$nr/direction` 9 + 10 + gpio_this_debugfs=`cat $GPIO_DEBUGFS |grep "gpio-$nr" | sed "s/(.*)//g"` 11 + dir_debugfs=`echo $gpio_this_debugfs | awk '{print $2}'` 12 + val_debugfs=`echo $gpio_this_debugfs | awk '{print $3}'` 13 + if [ $val_debugfs = "lo" ]; then 14 + val=0 15 + elif [ $val_debugfs = "hi" ]; then 16 + val=1 17 + fi 18 + 19 + if [ $active_low_sysfs = "1" ]; then 20 + if [ $val = "0" ]; then 21 + val="1" 22 + else 23 + val="0" 24 + fi 25 + fi 26 + 27 + if [ $val_sysfs = $val ] && [ $dir_sysfs = $dir_debugfs ]; then 28 + echo -n "." 29 + else 30 + echo "test fail, exit" 31 + die 32 + fi 33 + } 34 + 35 + test_pin_logic() 36 + { 37 + nr=$1 38 + direction=$2 39 + active_low=$3 40 + value=$4 41 + 42 + echo $direction > $GPIO_SYSFS/gpio$nr/direction 43 + echo $active_low > $GPIO_SYSFS/gpio$nr/active_low 44 + if [ $direction = "out" ]; then 45 + echo $value > $GPIO_SYSFS/gpio$nr/value 46 + fi 47 + is_consistent $nr 48 + } 49 + 50 + test_one_pin() 51 + { 52 + nr=$1 53 + 54 + echo -n "test pin<$nr>" 55 + 56 + echo $nr > $GPIO_SYSFS/export 2>/dev/null 57 + 58 + if [ X$? != X0 ]; then 59 + echo "test GPIO pin $nr failed" 60 + die 61 + fi 62 + 63 + #"Checking if the sysfs is consistent with debugfs: " 64 + is_consistent $nr 65 + 66 + #"Checking the logic of active_low: " 67 + test_pin_logic $nr out 1 1 68 + test_pin_logic $nr out 1 0 69 + test_pin_logic $nr out 0 1 70 + test_pin_logic $nr out 0 0 71 + 72 + #"Checking the logic of direction: " 73 + test_pin_logic $nr in 1 1 74 + test_pin_logic $nr out 1 0 75 + test_pin_logic $nr low 0 1 76 + test_pin_logic $nr high 0 0 77 + 78 + echo $nr > $GPIO_SYSFS/unexport 79 + 80 + echo "successful" 81 + } 82 + 83 + test_one_pin_fail() 84 + { 85 + nr=$1 86 + 87 + echo $nr > $GPIO_SYSFS/export 2>/dev/null 88 + 89 + if [ X$? != X0 ]; then 90 + echo "test invalid pin $nr successful" 91 + else 92 + echo "test invalid pin $nr failed" 93 + echo $nr > $GPIO_SYSFS/unexport 2>/dev/null 94 + die 95 + fi 96 + } 97 + 98 + list_chip() 99 + { 100 + echo `ls -d $GPIO_DRV_SYSFS/gpiochip* 2>/dev/null` 101 + } 102 + 103 + test_chip() 104 + { 105 + chip=$1 106 + name=`basename $chip` 107 + base=`cat $chip/base` 108 + ngpio=`cat $chip/ngpio` 109 + printf "%-10s %-5s %-5s\n" $name $base $ngpio 110 + if [ $ngpio = "0" ]; then 111 + echo "number of gpio is zero is not allowed". 112 + fi 113 + test_one_pin $base 114 + test_one_pin $(($base + $ngpio - 1)) 115 + test_one_pin $((( RANDOM % $ngpio ) + $base )) 116 + } 117 + 118 + test_chips_sysfs() 119 + { 120 + gpiochip=`list_chip $module` 121 + if [ X"$gpiochip" = X ]; then 122 + if [ X"$valid" = Xfalse ]; then 123 + echo "successful" 124 + else 125 + echo "fail" 126 + die 127 + fi 128 + else 129 + for chip in $gpiochip; do 130 + test_chip $chip 131 + done 132 + fi 133 + } 134 +
+201
tools/testing/selftests/gpio/gpio-mockup.sh
··· 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 + 9 + SYSFS= 10 + GPIO_SYSFS= 11 + GPIO_DRV_SYSFS= 12 + DEBUGFS= 13 + GPIO_DEBUGFS= 14 + dev_type= 15 + module= 16 + 17 + usage() 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 + 31 + prerequisite() 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 + 54 + try_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 + 67 + remove_module() 68 + { 69 + modprobe -r -q $module 70 + } 71 + 72 + die() 73 + { 74 + remove_module 75 + exit 4 76 + } 77 + 78 + test_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 + 88 + gpio_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 + 104 + BASE=`dirname $0` 105 + 106 + dev_type= 107 + TEMP=`getopt -o fhm:t: -n '$0' -- "$@"` 108 + 109 + if [ "$?" != "0" ]; then 110 + echo "Parameter process failed, Terminating..." >&2 111 + exit 1 112 + fi 113 + 114 + # Note the quotes around `$TEMP': they are essential! 115 + eval set -- "$TEMP" 116 + 117 + while 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 144 + done 145 + 146 + if [ X"$module" = X ]; then 147 + module="gpio-mockup" 148 + fi 149 + 150 + if [ X$dev_type != Xsysfs ]; then 151 + dev_type="chardev" 152 + fi 153 + 154 + prerequisite 155 + 156 + echo "1. Test dynamic allocation of gpio successful means insert gpiochip and" 157 + echo " manipulate gpio pin successful" 158 + gpio_test "-1,32" true 159 + gpio_test "-1,32,-1,32" true 160 + gpio_test "-1,32,-1,32,-1,32" true 161 + if [ 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 172 + fi 173 + echo "3. Error test: successful means insert gpiochip failed" 174 + echo "3.1 Test number of gpio overflow" 175 + #Currently: The max number of gpio(1024) is defined in arm architecture. 176 + gpio_test "-1,32,-1,1024" false 177 + if [ 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 198 + fi 199 + 200 + echo GPIO test PASS 201 +