From patchwork Sat Nov 14 08:51:36 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bamvor Zhang Jian X-Patchwork-Id: 56561 Delivered-To: patch@linaro.org Received: by 10.112.155.196 with SMTP id vy4csp246575lbb; Sat, 14 Nov 2015 00:52:14 -0800 (PST) X-Received: by 10.66.217.234 with SMTP id pb10mr38089736pac.148.1447491133398; Sat, 14 Nov 2015 00:52:13 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id gr2si33697396pbc.174.2015.11.14.00.52.13; Sat, 14 Nov 2015 00:52:13 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-gpio-owner@vger.kernel.org; dkim=neutral (body hash did not verify) header.i=@linaro_org.20150623.gappssmtp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751051AbbKNIwK (ORCPT + 4 others); Sat, 14 Nov 2015 03:52:10 -0500 Received: from mail-pa0-f41.google.com ([209.85.220.41]:35710 "EHLO mail-pa0-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751095AbbKNIwE (ORCPT ); Sat, 14 Nov 2015 03:52:04 -0500 Received: by pacej9 with SMTP id ej9so16901529pac.2 for ; Sat, 14 Nov 2015 00:52:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro_org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=3IFBefRX8GH9ivsI7K/AJZ4zLXjzWPaoGDJOahmnUP4=; b=fhiN68Pyjc5tBnRbnUUU9klYGpZTukt+PFB1hSSUx11AWiH7tleX9shq58qORcLhVj YLgG7rqvQMG/fGChDaalbEDtshWJq8jLKZ3B5hVF6g5PYwehxk1cs9NXVCu7q3rwG/tv cJX5CzxmleVjta75UHO4tJRSDenwG+b7urnl9OsfJXZSe0Bl8z7gbzDRxVGnjO5MiiQe V7BGxpLL+Umkv8UdPZC9Tw14+yypBLOySfqdRdJQz4ScsvDpYuTTV7+DOuYi1dLGgQKq V1xcSBXule5BoqnLli0Z/l8/pwW22YuaBDZMKP76WH+mcEL6zFplHBIvZfEbbn0Vppia utBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=3IFBefRX8GH9ivsI7K/AJZ4zLXjzWPaoGDJOahmnUP4=; b=E2CzSMQNB0g5l1plvTG1TvqzDkilk52IqPaLyaFu7yZoDIc8KGl042nU+BxjDoI05X 5VNMsXZAkNQDJMGN+X3CyE1upfGe2yFqmSbOGFgfmu1L+Od/1e8aDHGypHDocJpW3FYm YbFwm22KrL5z8DylkodAjmEKIhVxR6BdGn0dLngHMg6qgJHvD3k4f+L9GBkd2DK7g4IX Rrk6e/7BlwGZCEwhU0tKtUZNDlrR505yJXpz6mGlJKq63HcY4UUfLTnKDAN7GAxfOLz0 O9Tq+bX3XQP05rJM8uaUyuLMU09y+EXpMGNUwY3gSiwAJYzm3rUX6dDNKTLjFjqaOjsb sjYA== X-Gm-Message-State: ALoCoQk6IAL0CZ6FefLxeliy1CtjegDs8vj7dc7ieoTpzGO/U7vb4XuV7c92MfArakzwZXdxjX9O X-Received: by 10.66.179.6 with SMTP id dc6mr39200789pac.67.1447491124053; Sat, 14 Nov 2015 00:52:04 -0800 (PST) Received: from linux-j170.site ([116.251.213.227]) by smtp.gmail.com with ESMTPSA id qw1sm24649722pbb.22.2015.11.14.00.52.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 14 Nov 2015 00:52:03 -0800 (PST) From: Bamvor Jian Zhang To: linux-gpio@vger.kernel.org Cc: linus.walleij@linaro.org, broonie@kernel.org, Bamvor Jian Zhang Subject: [PATCH RFD 2/3] selftest/gpio: add gpio test case Date: Sat, 14 Nov 2015 16:51:36 +0800 Message-Id: <1447491097-14799-3-git-send-email-bamvor.zhangjian@linaro.org> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1447491097-14799-1-git-send-email-bamvor.zhangjian@linaro.org> References: <1447491097-14799-1-git-send-email-bamvor.zhangjian@linaro.org> Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org This test script try to do whitebox testing for gpio subsystem( based on gpiolib). It manipulate gpio-mockup device through sysfs provided and check the result from debugfs. Test the following things: 1. Add single, multi gpiochip with the checking of overlap. 2. Test direction and output value for valid pin. 3. Test dynamic allocation of gpio base. Signed-off-by: Bamvor Jian Zhang --- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/gpio/gpio.sh | 238 +++++++++++++++++++++++++++++++++++ 2 files changed, 239 insertions(+) create mode 100755 tools/testing/selftests/gpio/gpio.sh -- 2.1.4 -- To unsubscribe from this list: send the line "unsubscribe linux-gpio" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index cfe1213..df0b81d 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -5,6 +5,7 @@ TARGETS += exec TARGETS += firmware TARGETS += ftrace TARGETS += futex +TARGETS += gpio TARGETS += kcmp TARGETS += membarrier TARGETS += memfd diff --git a/tools/testing/selftests/gpio/gpio.sh b/tools/testing/selftests/gpio/gpio.sh new file mode 100755 index 0000000..05e109e --- /dev/null +++ b/tools/testing/selftests/gpio/gpio.sh @@ -0,0 +1,238 @@ +#!/bin/bash + +module="gpio-mockup" + +SYSFS= +GPIO_SYSFS= +GPIO_DRV_SYSFS= +DEBUGFS= +GPIO_DEBUGFS= + +prerequisite() +{ + msg="skip all tests:" + + if [ $UID != 0 ]; then + echo $msg must be run as root >&2 + exit 0 + fi + + SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'` + if [ ! -d "$SYSFS" ]; then + echo $msg sysfs is not mounted >&2 + exit 0 + fi + GPIO_SYSFS=`echo $SYSFS/class/gpio` + GPIO_DRV_SYSFS=`echo $SYSFS/devices/platform/$module/gpio` + + DEBUGFS=`mount -t debugfs | head -1 | awk '{ print $3 }'` + if [ ! -d "$DEBUGFS" ]; then + echo $msg debugfs is not mounted >&2 + exit 0 + fi + GPIO_DEBUGFS=`echo $DEBUGFS/gpio` + +} + +insert_module() +{ + if modprobe -q $module $1; then + echo -n "" + else + echo $msg insmod $module failed >&2 + exit 0 + fi +} + +remove_module() +{ + modprobe -r -q $module +} + +die() +{ + remove_module + exit 1 +} + +gpio_test() +{ + param=$1 + if [ X$2 != X ]; then + invalid_pin=$2 + fi + if [ X$param = X ]; then + insert_module + else + insert_module "conf=$param" + fi + + echo -n "GPIO $module test with base,ngpio configure<" + if [ X$param = X ]; then + echo "(default)>: "; + else + echo "$param>: " + fi + printf "%-10s %-5s %-5s\n" name base ngpio + gpiochip=`ls -d $GPIO_DRV_SYSFS/gpiochip* 2>/dev/null` + if [ X"$gpiochip" = X ]; then + echo "no gpiochip" + else + for chip in $gpiochip; do + name=`basename $chip` + base=`cat $chip/base` + ngpio=`cat $chip/ngpio` + printf "%-10s %-5s %-5s\n" $name $base $ngpio + if [ $ngpio = "0" ]; then + echo "number of gpio is zero is not allowed". + fi + test_one_pin $base + test_one_pin $(($base + $ngpio - 1)) + test_one_pin $((( RANDOM % $ngpio ) + $base )) + if [ X$invalid_pin != X ]; then + test_one_pin_fail $invalid_pin + fi + done + fi + remove_module +} + +gpio_test_fail() +{ + param=$1 + + insert_module "conf=$param" + echo -n "GPIO $module fail test with base,ngpio configure<$param>: " + gpiochip=`ls -d $GPIO_DRV_SYSFS/gpiochip* 2>/dev/null` + if [ X"$gpiochip" = X ]; then + echo "successful" + else + echo "fail" + die + fi + remove_module +} + +is_consistent() +{ + val= + + active_low_sysfs=`cat $GPIO_SYSFS/gpio$nr/active_low` + val_sysfs=`cat $GPIO_SYSFS/gpio$nr/value` + dir_sysfs=`cat $GPIO_SYSFS/gpio$nr/direction` + + gpio_this_debugfs=`cat $GPIO_DEBUGFS |grep "gpio-$nr" | sed "s/(.*)//g"` + dir_debugfs=`echo $gpio_this_debugfs | awk '{print $2}'` + val_debugfs=`echo $gpio_this_debugfs | awk '{print $3}'` + if [ $val_debugfs = "lo" ]; then + val=0 + elif [ $val_debugfs = "hi" ]; then + val=1 + fi + + if [ $active_low_sysfs = "1" ]; then + if [ $val = "0" ]; then + val="1" + else + val="0" + fi + fi + + if [ $val_sysfs = $val ] && [ $dir_sysfs = $dir_debugfs ]; then + echo -n "." + else + echo "test fail, exit" + die + fi +} + +test_pin_logic() +{ + nr=$1 + direction=$2 + active_low=$3 + value=$4 + + echo $direction > $GPIO_SYSFS/gpio$nr/direction + echo $active_low > $GPIO_SYSFS/gpio$nr/active_low + if [ $direction = "out" ]; then + echo $value > $GPIO_SYSFS/gpio$nr/value + fi + is_consistent $nr +} + +test_one_pin() +{ + nr=$1 + + echo -n "test pin<$nr>" + + echo $nr > $GPIO_SYSFS/export 2>/dev/null + + if [ X$? != X0 ]; then + echo "test GPIO pin $nr failed" + die + fi + + #"Checking if the sysfs is consistent with debugfs: " + is_consistent $nr + + #"Checking the logic of active_low: " + test_pin_logic $nr out 1 1 + test_pin_logic $nr out 1 0 + test_pin_logic $nr out 0 1 + test_pin_logic $nr out 0 0 + + #"Checking the logic of direction: " + test_pin_logic $nr in 1 1 + test_pin_logic $nr out 1 0 + test_pin_logic $nr low 0 1 + test_pin_logic $nr high 0 0 + + echo $nr > $GPIO_SYSFS/unexport + + echo "successful" +} + +test_one_pin_fail() +{ + nr=$1 + + echo $nr > $GPIO_SYSFS/export 2>/dev/null + + if [ X$? != X0 ]; then + echo "test invalid pin $nr successful" + else + echo "test invalid pin $nr failed" + echo $nr > $GPIO_SYSFS/unexport 2>/dev/null + die + fi +} + +prerequisite + +#basically +gpio_test +gpio_test "0,32" 33 +gpio_test "0,32,32,16" 50 +gpio_test "0,32,40,16,32,5" 38 +#test dynamic allocation of gpio +gpio_test "-1,32" +gpio_test "-1,32,32,16" 31 +gpio_test "-1,32,40,16,-1,5" 38 +#error test +#zero number of gpio +gpio_test_fail "0,0" +#base conflict +gpio_test_fail "0,32,0,1" +#range overlap +gpio_test_fail "0,32,30,5" +gpio_test_fail "0,32,1,5" +gpio_test_fail "10,32,9,5" +gpio_test_fail "10,32,30,5" +gpio_test_fail "0,32,40,16,39,5" +gpio_test_fail "0,32,40,16,30,5" +gpio_test_fail "0,32,40,16,30,11" +gpio_test_fail "0,32,40,16,20,1" + +echo GPIO test PASS