Path: blob/master/tools/testing/selftests/gpio/gpio-sim.sh
26285 views
#!/bin/sh1# SPDX-License-Identifier: GPL-2.02# Copyright (C) 2021 Bartosz Golaszewski <[email protected]>34BASE_DIR=`dirname $0`5CONFIGFS_DIR="/sys/kernel/config/gpio-sim"6MODULE="gpio-sim"78fail() {9echo "$*" >&210echo "GPIO $MODULE test FAIL"11exit 112}1314skip() {15echo "$*" >&216echo "GPIO $MODULE test SKIP"17exit 418}1920remove_chip() {21local CHIP=$12223for FILE in $CONFIGFS_DIR/$CHIP/*; do24BANK=`basename $FILE`25if [ "$BANK" = "live" -o "$BANK" = "dev_name" ]; then26continue27fi2829LINES=`ls $CONFIGFS_DIR/$CHIP/$BANK/ | grep -E ^line`30if [ "$?" = 0 ]; then31for LINE in $LINES; do32if [ -e $CONFIGFS_DIR/$CHIP/$BANK/$LINE/hog ]; then33rmdir $CONFIGFS_DIR/$CHIP/$BANK/$LINE/hog || \34fail "Unable to remove the hog"35fi3637rmdir $CONFIGFS_DIR/$CHIP/$BANK/$LINE || \38fail "Unable to remove the line"39done40fi4142rmdir $CONFIGFS_DIR/$CHIP/$BANK43done4445rmdir $CONFIGFS_DIR/$CHIP || fail "Unable to remove the chip"46}4748create_chip() {49local CHIP=$15051mkdir $CONFIGFS_DIR/$CHIP52}5354create_bank() {55local CHIP=$156local BANK=$25758mkdir $CONFIGFS_DIR/$CHIP/$BANK59}6061set_label() {62local CHIP=$163local BANK=$264local LABEL=$36566echo $LABEL > $CONFIGFS_DIR/$CHIP/$BANK/label || fail "Unable to set the chip label"67}6869set_num_lines() {70local CHIP=$171local BANK=$272local NUM_LINES=$37374echo $NUM_LINES > $CONFIGFS_DIR/$CHIP/$BANK/num_lines || \75fail "Unable to set the number of lines"76}7778set_line_name() {79local CHIP=$180local BANK=$281local OFFSET=$382local NAME=$483local LINE_DIR=$CONFIGFS_DIR/$CHIP/$BANK/line$OFFSET8485test -d $LINE_DIR || mkdir $LINE_DIR86echo $NAME > $LINE_DIR/name || fail "Unable to set the line name"87}8889enable_chip() {90local CHIP=$19192echo 1 > $CONFIGFS_DIR/$CHIP/live || fail "Unable to enable the chip"93}9495disable_chip() {96local CHIP=$19798echo 0 > $CONFIGFS_DIR/$CHIP/live || fail "Unable to disable the chip"99}100101configfs_cleanup() {102for CHIP in `ls $CONFIGFS_DIR/`; do103disable_chip $CHIP104remove_chip $CHIP105done106}107108configfs_chip_name() {109local CHIP=$1110local BANK=$2111112cat $CONFIGFS_DIR/$CHIP/$BANK/chip_name 2> /dev/null || \113fail "unable to read the chip name from configfs"114}115116configfs_dev_name() {117local CHIP=$1118119cat $CONFIGFS_DIR/$CHIP/dev_name 2> /dev/null || \120fail "unable to read the device name from configfs"121}122123get_chip_num_lines() {124local CHIP=$1125local BANK=$2126127$BASE_DIR/gpio-chip-info /dev/`configfs_chip_name $CHIP $BANK` num-lines || \128fail "unable to read the number of lines from the character device"129}130131get_chip_label() {132local CHIP=$1133local BANK=$2134135$BASE_DIR/gpio-chip-info /dev/`configfs_chip_name $CHIP $BANK` label || \136fail "unable to read the chip label from the character device"137}138139get_line_name() {140local CHIP=$1141local BANK=$2142local OFFSET=$3143144$BASE_DIR/gpio-line-name /dev/`configfs_chip_name $CHIP $BANK` $OFFSET || \145fail "unable to read the line name from the character device"146}147148sysfs_set_pull() {149local DEV=$1150local BANK=$2151local OFFSET=$3152local PULL=$4153local DEVNAME=`configfs_dev_name $DEV`154local CHIPNAME=`configfs_chip_name $DEV $BANK`155local SYSFS_PATH="/sys/devices/platform/$DEVNAME/$CHIPNAME/sim_gpio$OFFSET/pull"156157echo $PULL > $SYSFS_PATH || fail "Unable to set line pull in sysfs"158}159160# Load the gpio-sim module. This will pull in configfs if needed too.161modprobe gpio-sim || skip "unable to load the gpio-sim module"162# Make sure configfs is mounted at /sys/kernel/config. Wait a bit if needed.163for IDX in `seq 5`; do164if [ "$IDX" -eq "5" ]; then165skip "configfs not mounted at /sys/kernel/config"166fi167168mountpoint -q /sys/kernel/config && break169sleep 0.1170done171# If the module was already loaded: remove all previous chips172configfs_cleanup173174trap "exit 1" SIGTERM SIGINT175trap configfs_cleanup EXIT176177echo "1. chip_name and dev_name attributes"178179echo "1.1. Chip name is communicated to user"180create_chip chip181create_bank chip bank182enable_chip chip183test -n `cat $CONFIGFS_DIR/chip/bank/chip_name` || fail "chip_name doesn't work"184disable_chip chip185remove_chip chip186187echo "1.2. chip_name returns 'none' if the chip is still pending"188create_chip chip189create_bank chip bank190test "`cat $CONFIGFS_DIR/chip/bank/chip_name`" = "none" || \191fail "chip_name doesn't return 'none' for a pending chip"192remove_chip chip193194echo "1.3. Device name is communicated to user"195create_chip chip196create_bank chip bank197enable_chip chip198test -n `cat $CONFIGFS_DIR/chip/dev_name` || fail "dev_name doesn't work"199disable_chip chip200remove_chip chip201202echo "2. Creating and configuring simulated chips"203204echo "2.1. Default number of lines is 1"205create_chip chip206create_bank chip bank207enable_chip chip208test "`get_chip_num_lines chip bank`" = "1" || fail "default number of lines is not 1"209disable_chip chip210remove_chip chip211212echo "2.2. Number of lines can be specified"213create_chip chip214create_bank chip bank215set_num_lines chip bank 16216enable_chip chip217test "`get_chip_num_lines chip bank`" = "16" || fail "number of lines is not 16"218disable_chip chip219remove_chip chip220221echo "2.3. Label can be set"222create_chip chip223create_bank chip bank224set_label chip bank foobar225enable_chip chip226test "`get_chip_label chip bank`" = "foobar" || fail "label is incorrect"227disable_chip chip228remove_chip chip229230echo "2.4. Label can be left empty"231create_chip chip232create_bank chip bank233enable_chip chip234test -z "`cat $CONFIGFS_DIR/chip/bank/label`" || fail "label is not empty"235disable_chip chip236remove_chip chip237238echo "2.5. Line names can be configured"239create_chip chip240create_bank chip bank241set_num_lines chip bank 16242set_line_name chip bank 0 foo243set_line_name chip bank 2 bar244enable_chip chip245test "`get_line_name chip bank 0`" = "foo" || fail "line name is incorrect"246test "`get_line_name chip bank 2`" = "bar" || fail "line name is incorrect"247disable_chip chip248remove_chip chip249250echo "2.6. Line config can remain unused if offset is greater than number of lines"251create_chip chip252create_bank chip bank253set_num_lines chip bank 2254set_line_name chip bank 5 foobar255enable_chip chip256test "`get_line_name chip bank 0`" = "" || fail "line name is incorrect"257test "`get_line_name chip bank 1`" = "" || fail "line name is incorrect"258disable_chip chip259remove_chip chip260261echo "2.7. Line configfs directory names are sanitized"262create_chip chip263create_bank chip bank264mkdir $CONFIGFS_DIR/chip/bank/line12foobar 2> /dev/null && \265fail "invalid configfs line name accepted"266mkdir $CONFIGFS_DIR/chip/bank/line_no_offset 2> /dev/null && \267fail "invalid configfs line name accepted"268remove_chip chip269270echo "2.8. Multiple chips can be created"271CHIPS="chip0 chip1 chip2"272for CHIP in $CHIPS; do273create_chip $CHIP274create_bank $CHIP bank275enable_chip $CHIP276done277for CHIP in $CHIPS; do278disable_chip $CHIP279remove_chip $CHIP280done281282echo "2.9. Can't modify settings when chip is live"283create_chip chip284create_bank chip bank285enable_chip chip286echo foobar > $CONFIGFS_DIR/chip/bank/label 2> /dev/null && \287fail "Setting label of a live chip should fail"288echo 8 > $CONFIGFS_DIR/chip/bank/num_lines 2> /dev/null && \289fail "Setting number of lines of a live chip should fail"290disable_chip chip291remove_chip chip292293echo "2.10. Can't create line items when chip is live"294create_chip chip295create_bank chip bank296enable_chip chip297mkdir $CONFIGFS_DIR/chip/bank/line0 2> /dev/null && fail "Creating line item should fail"298disable_chip chip299remove_chip chip300301echo "2.11. Probe errors are propagated to user-space"302create_chip chip303create_bank chip bank304set_num_lines chip bank 99999305echo 1 > $CONFIGFS_DIR/chip/live 2> /dev/null && fail "Probe error was not propagated"306remove_chip chip307308echo "2.12. Cannot enable a chip without any GPIO banks"309create_chip chip310echo 1 > $CONFIGFS_DIR/chip/live 2> /dev/null && fail "Chip enabled without any GPIO banks"311remove_chip chip312313echo "2.13. Duplicate chip labels are not allowed"314create_chip chip315create_bank chip bank0316set_label chip bank0 foobar317create_bank chip bank1318set_label chip bank1 foobar319echo 1 > $CONFIGFS_DIR/chip/live 2> /dev/null && fail "Duplicate chip labels were not rejected"320remove_chip chip321322echo "2.14. Lines can be hogged"323create_chip chip324create_bank chip bank325set_num_lines chip bank 8326mkdir -p $CONFIGFS_DIR/chip/bank/line4/hog327enable_chip chip328$BASE_DIR/gpio-mockup-cdev -s 1 /dev/`configfs_chip_name chip bank` 4 2> /dev/null && \329fail "Setting the value of a hogged line shouldn't succeed"330disable_chip chip331remove_chip chip332333echo "3. Controlling simulated chips"334335echo "3.1. Pull can be set over sysfs"336create_chip chip337create_bank chip bank338set_num_lines chip bank 8339enable_chip chip340sysfs_set_pull chip bank 0 pull-up341$BASE_DIR/gpio-mockup-cdev /dev/`configfs_chip_name chip bank` 0342test "$?" = "1" || fail "pull set incorrectly"343sysfs_set_pull chip bank 0 pull-down344$BASE_DIR/gpio-mockup-cdev /dev/`configfs_chip_name chip bank` 1345test "$?" = "0" || fail "pull set incorrectly"346disable_chip chip347remove_chip chip348349echo "3.2. Pull can be read from sysfs"350create_chip chip351create_bank chip bank352set_num_lines chip bank 8353enable_chip chip354DEVNAME=`configfs_dev_name chip`355CHIPNAME=`configfs_chip_name chip bank`356SYSFS_PATH=/sys/devices/platform/$DEVNAME/$CHIPNAME/sim_gpio0/pull357test `cat $SYSFS_PATH` = "pull-down" || fail "reading the pull failed"358sysfs_set_pull chip bank 0 pull-up359test `cat $SYSFS_PATH` = "pull-up" || fail "reading the pull failed"360disable_chip chip361remove_chip chip362363echo "3.3. Incorrect input in sysfs is rejected"364create_chip chip365create_bank chip bank366set_num_lines chip bank 8367enable_chip chip368DEVNAME=`configfs_dev_name chip`369CHIPNAME=`configfs_chip_name chip bank`370SYSFS_PATH="/sys/devices/platform/$DEVNAME/$CHIPNAME/sim_gpio0/pull"371echo foobar > $SYSFS_PATH 2> /dev/null && fail "invalid input not detected"372disable_chip chip373remove_chip chip374375echo "3.4. Can't write to value"376create_chip chip377create_bank chip bank378enable_chip chip379DEVNAME=`configfs_dev_name chip`380CHIPNAME=`configfs_chip_name chip bank`381SYSFS_PATH="/sys/devices/platform/$DEVNAME/$CHIPNAME/sim_gpio0/value"382echo 1 > $SYSFS_PATH 2> /dev/null && fail "writing to 'value' succeeded unexpectedly"383disable_chip chip384remove_chip chip385386echo "4. Simulated GPIO chips are functional"387388echo "4.1. Values can be read from sysfs"389create_chip chip390create_bank chip bank391set_num_lines chip bank 8392enable_chip chip393DEVNAME=`configfs_dev_name chip`394CHIPNAME=`configfs_chip_name chip bank`395SYSFS_PATH="/sys/devices/platform/$DEVNAME/$CHIPNAME/sim_gpio0/value"396test `cat $SYSFS_PATH` = "0" || fail "incorrect value read from sysfs"397$BASE_DIR/gpio-mockup-cdev -s 1 /dev/`configfs_chip_name chip bank` 0 &398sleep 0.1 # FIXME Any better way?399test `cat $SYSFS_PATH` = "1" || fail "incorrect value read from sysfs"400kill $!401disable_chip chip402remove_chip chip403404echo "4.2. Bias settings work correctly"405create_chip chip406create_bank chip bank407set_num_lines chip bank 8408enable_chip chip409DEVNAME=`configfs_dev_name chip`410CHIPNAME=`configfs_chip_name chip bank`411SYSFS_PATH="/sys/devices/platform/$DEVNAME/$CHIPNAME/sim_gpio0/value"412$BASE_DIR/gpio-mockup-cdev -b pull-up /dev/`configfs_chip_name chip bank` 0413test `cat $SYSFS_PATH` = "1" || fail "bias setting does not work"414disable_chip chip415remove_chip chip416417echo "GPIO $MODULE test PASS"418419420