brintos

brintos / linux-shallow public Read only

0
0
Text · 10.6 KiB · 6fb66a6 Raw
400 lines · bash
1#!/bin/sh2# SPDX-License-Identifier: GPL-2.03# Copyright (C) 2021 Bartosz Golaszewski <brgl@bgdev.pl>4 5BASE_DIR=`dirname $0`6CONFIGFS_DIR="/sys/kernel/config/gpio-sim"7MODULE="gpio-sim"8 9fail() {10	echo "$*" >&211	echo "GPIO $MODULE test FAIL"12	exit 113}14 15skip() {16	echo "$*" >&217	echo "GPIO $MODULE test SKIP"18	exit 419}20 21remove_chip() {22	local CHIP=$123 24	for FILE in $CONFIGFS_DIR/$CHIP/*; do25		BANK=`basename $FILE`26		if [ "$BANK" = "live" -o "$BANK" = "dev_name" ]; then27			continue28		fi29 30		LINES=`ls $CONFIGFS_DIR/$CHIP/$BANK/ | grep -E ^line`31		if [ "$?" = 0 ]; then32			for LINE in $LINES; do33				if [ -e $CONFIGFS_DIR/$CHIP/$BANK/$LINE/hog ]; then34					rmdir $CONFIGFS_DIR/$CHIP/$BANK/$LINE/hog || \35						fail "Unable to remove the hog"36				fi37 38				rmdir $CONFIGFS_DIR/$CHIP/$BANK/$LINE || \39					fail "Unable to remove the line"40			done41		fi42 43		rmdir $CONFIGFS_DIR/$CHIP/$BANK44	done45 46	rmdir $CONFIGFS_DIR/$CHIP || fail "Unable to remove the chip"47}48 49configfs_cleanup() {50	for CHIP in `ls $CONFIGFS_DIR/`; do51		remove_chip $CHIP52	done53}54 55create_chip() {56	local CHIP=$157 58	mkdir $CONFIGFS_DIR/$CHIP59}60 61create_bank() {62	local CHIP=$163	local BANK=$264 65	mkdir $CONFIGFS_DIR/$CHIP/$BANK66}67 68set_label() {69	local CHIP=$170	local BANK=$271	local LABEL=$372 73	echo $LABEL > $CONFIGFS_DIR/$CHIP/$BANK/label || fail "Unable to set the chip label"74}75 76set_num_lines() {77	local CHIP=$178	local BANK=$279	local NUM_LINES=$380 81	echo $NUM_LINES > $CONFIGFS_DIR/$CHIP/$BANK/num_lines || \82		fail "Unable to set the number of lines"83}84 85set_line_name() {86	local CHIP=$187	local BANK=$288	local OFFSET=$389	local NAME=$490	local LINE_DIR=$CONFIGFS_DIR/$CHIP/$BANK/line$OFFSET91 92	test -d $LINE_DIR || mkdir $LINE_DIR93	echo $NAME > $LINE_DIR/name || fail "Unable to set the line name"94}95 96enable_chip() {97	local CHIP=$198 99	echo 1 > $CONFIGFS_DIR/$CHIP/live || fail "Unable to enable the chip"100}101 102disable_chip() {103	local CHIP=$1104 105	echo 0 > $CONFIGFS_DIR/$CHIP/live || fail "Unable to disable the chip"106}107 108configfs_chip_name() {109	local CHIP=$1110	local BANK=$2111 112	cat $CONFIGFS_DIR/$CHIP/$BANK/chip_name 2> /dev/null || \113		fail "unable to read the chip name from configfs"114}115 116configfs_dev_name() {117	local CHIP=$1118 119	cat $CONFIGFS_DIR/$CHIP/dev_name 2> /dev/null || \120		fail "unable to read the device name from configfs"121}122 123get_chip_num_lines() {124	local CHIP=$1125	local BANK=$2126 127	$BASE_DIR/gpio-chip-info /dev/`configfs_chip_name $CHIP $BANK` num-lines || \128		fail "unable to read the number of lines from the character device"129}130 131get_chip_label() {132	local CHIP=$1133	local BANK=$2134 135	$BASE_DIR/gpio-chip-info /dev/`configfs_chip_name $CHIP $BANK` label || \136		fail "unable to read the chip label from the character device"137}138 139get_line_name() {140	local CHIP=$1141	local BANK=$2142	local OFFSET=$3143 144	$BASE_DIR/gpio-line-name /dev/`configfs_chip_name $CHIP $BANK` $OFFSET || \145		fail "unable to read the line name from the character device"146}147 148sysfs_set_pull() {149	local DEV=$1150	local BANK=$2151	local OFFSET=$3152	local PULL=$4153	local DEVNAME=`configfs_dev_name $DEV`154	local CHIPNAME=`configfs_chip_name $DEV $BANK`155	local SYSFS_PATH="/sys/devices/platform/$DEVNAME/$CHIPNAME/sim_gpio$OFFSET/pull"156 157	echo $PULL > $SYSFS_PATH || fail "Unable to set line pull in sysfs"158}159 160# 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`; do164	if [ "$IDX" -eq "5" ]; then165		skip "configfs not mounted at /sys/kernel/config"166	fi167 168	mountpoint -q /sys/kernel/config && break169	sleep 0.1170done171# If the module was already loaded: remove all previous chips172configfs_cleanup173 174trap "exit 1" SIGTERM SIGINT175trap configfs_cleanup EXIT176 177echo "1. chip_name and dev_name attributes"178 179echo "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"184remove_chip chip185 186echo "1.2. chip_name returns 'none' if the chip is still pending"187create_chip chip188create_bank chip bank189test "`cat $CONFIGFS_DIR/chip/bank/chip_name`" = "none" || \190	fail "chip_name doesn't return 'none' for a pending chip"191remove_chip chip192 193echo "1.3. Device name is communicated to user"194create_chip chip195create_bank chip bank196enable_chip chip197test -n `cat $CONFIGFS_DIR/chip/dev_name` || fail "dev_name doesn't work"198remove_chip chip199 200echo "2. Creating and configuring simulated chips"201 202echo "2.1. Default number of lines is 1"203create_chip chip204create_bank chip bank205enable_chip chip206test "`get_chip_num_lines chip bank`" = "1" || fail "default number of lines is not 1"207remove_chip chip208 209echo "2.2. Number of lines can be specified"210create_chip chip211create_bank chip bank212set_num_lines chip bank 16213enable_chip chip214test "`get_chip_num_lines chip bank`" = "16" || fail "number of lines is not 16"215remove_chip chip216 217echo "2.3. Label can be set"218create_chip chip219create_bank chip bank220set_label chip bank foobar221enable_chip chip222test "`get_chip_label chip bank`" = "foobar" || fail "label is incorrect"223remove_chip chip224 225echo "2.4. Label can be left empty"226create_chip chip227create_bank chip bank228enable_chip chip229test -z "`cat $CONFIGFS_DIR/chip/bank/label`" || fail "label is not empty"230remove_chip chip231 232echo "2.5. Line names can be configured"233create_chip chip234create_bank chip bank235set_num_lines chip bank 16236set_line_name chip bank 0 foo237set_line_name chip bank 2 bar238enable_chip chip239test "`get_line_name chip bank 0`" = "foo" || fail "line name is incorrect"240test "`get_line_name chip bank 2`" = "bar" || fail "line name is incorrect"241remove_chip chip242 243echo "2.6. Line config can remain unused if offset is greater than number of lines"244create_chip chip245create_bank chip bank246set_num_lines chip bank 2247set_line_name chip bank 5 foobar248enable_chip chip249test "`get_line_name chip bank 0`" = "" || fail "line name is incorrect"250test "`get_line_name chip bank 1`" = "" || fail "line name is incorrect"251remove_chip chip252 253echo "2.7. Line configfs directory names are sanitized"254create_chip chip255create_bank chip bank256mkdir $CONFIGFS_DIR/chip/bank/line12foobar 2> /dev/null && \257	fail "invalid configfs line name accepted"258mkdir $CONFIGFS_DIR/chip/bank/line_no_offset 2> /dev/null && \259	fail "invalid configfs line name accepted"260remove_chip chip261 262echo "2.8. Multiple chips can be created"263CHIPS="chip0 chip1 chip2"264for CHIP in $CHIPS; do265	create_chip $CHIP266	create_bank $CHIP bank267	enable_chip $CHIP268done269for CHIP in $CHIPS; do270	remove_chip $CHIP271done272 273echo "2.9. Can't modify settings when chip is live"274create_chip chip275create_bank chip bank276enable_chip chip277echo foobar > $CONFIGFS_DIR/chip/bank/label 2> /dev/null && \278	fail "Setting label of a live chip should fail"279echo 8 > $CONFIGFS_DIR/chip/bank/num_lines 2> /dev/null && \280	fail "Setting number of lines of a live chip should fail"281remove_chip chip282 283echo "2.10. Can't create line items when chip is live"284create_chip chip285create_bank chip bank286enable_chip chip287mkdir $CONFIGFS_DIR/chip/bank/line0 2> /dev/null && fail "Creating line item should fail"288remove_chip chip289 290echo "2.11. Probe errors are propagated to user-space"291create_chip chip292create_bank chip bank293set_num_lines chip bank 99999294echo 1 > $CONFIGFS_DIR/chip/live 2> /dev/null && fail "Probe error was not propagated"295remove_chip chip296 297echo "2.12. Cannot enable a chip without any GPIO banks"298create_chip chip299echo 1 > $CONFIGFS_DIR/chip/live 2> /dev/null && fail "Chip enabled without any GPIO banks"300remove_chip chip301 302echo "2.13. Duplicate chip labels are not allowed"303create_chip chip304create_bank chip bank0305set_label chip bank0 foobar306create_bank chip bank1307set_label chip bank1 foobar308echo 1 > $CONFIGFS_DIR/chip/live 2> /dev/null && fail "Duplicate chip labels were not rejected"309remove_chip chip310 311echo "2.14. Lines can be hogged"312create_chip chip313create_bank chip bank314set_num_lines chip bank 8315mkdir -p $CONFIGFS_DIR/chip/bank/line4/hog316enable_chip chip317$BASE_DIR/gpio-mockup-cdev -s 1 /dev/`configfs_chip_name chip bank` 4 2> /dev/null && \318	fail "Setting the value of a hogged line shouldn't succeed"319remove_chip chip320 321echo "3. Controlling simulated chips"322 323echo "3.1. Pull can be set over sysfs"324create_chip chip325create_bank chip bank326set_num_lines chip bank 8327enable_chip chip328sysfs_set_pull chip bank 0 pull-up329$BASE_DIR/gpio-mockup-cdev /dev/`configfs_chip_name chip bank` 0330test "$?" = "1" || fail "pull set incorrectly"331sysfs_set_pull chip bank 0 pull-down332$BASE_DIR/gpio-mockup-cdev /dev/`configfs_chip_name chip bank` 1333test "$?" = "0" || fail "pull set incorrectly"334remove_chip chip335 336echo "3.2. Pull can be read from sysfs"337create_chip chip338create_bank chip bank339set_num_lines chip bank 8340enable_chip chip341DEVNAME=`configfs_dev_name chip`342CHIPNAME=`configfs_chip_name chip bank`343SYSFS_PATH=/sys/devices/platform/$DEVNAME/$CHIPNAME/sim_gpio0/pull344test `cat $SYSFS_PATH` = "pull-down" || fail "reading the pull failed"345sysfs_set_pull chip bank 0 pull-up346test `cat $SYSFS_PATH` = "pull-up" || fail "reading the pull failed"347remove_chip chip348 349echo "3.3. Incorrect input in sysfs is rejected"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/pull"357echo foobar > $SYSFS_PATH 2> /dev/null && fail "invalid input not detected"358remove_chip chip359 360echo "3.4. Can't write to value"361create_chip chip362create_bank chip bank363enable_chip chip364DEVNAME=`configfs_dev_name chip`365CHIPNAME=`configfs_chip_name chip bank`366SYSFS_PATH="/sys/devices/platform/$DEVNAME/$CHIPNAME/sim_gpio0/value"367echo 1 > $SYSFS_PATH 2> /dev/null && fail "writing to 'value' succeeded unexpectedly"368remove_chip chip369 370echo "4. Simulated GPIO chips are functional"371 372echo "4.1. Values can be read from sysfs"373create_chip chip374create_bank chip bank375set_num_lines chip bank 8376enable_chip chip377DEVNAME=`configfs_dev_name chip`378CHIPNAME=`configfs_chip_name chip bank`379SYSFS_PATH="/sys/devices/platform/$DEVNAME/$CHIPNAME/sim_gpio0/value"380test `cat $SYSFS_PATH` = "0" || fail "incorrect value read from sysfs"381$BASE_DIR/gpio-mockup-cdev -s 1 /dev/`configfs_chip_name chip bank` 0 &382sleep 0.1 # FIXME Any better way?383test `cat $SYSFS_PATH` = "1" || fail "incorrect value read from sysfs"384kill $!385remove_chip chip386 387echo "4.2. Bias settings work correctly"388create_chip chip389create_bank chip bank390set_num_lines chip bank 8391enable_chip chip392DEVNAME=`configfs_dev_name chip`393CHIPNAME=`configfs_chip_name chip bank`394SYSFS_PATH="/sys/devices/platform/$DEVNAME/$CHIPNAME/sim_gpio0/value"395$BASE_DIR/gpio-mockup-cdev -b pull-up /dev/`configfs_chip_name chip bank` 0396test `cat $SYSFS_PATH` = "1" || fail "bias setting does not work"397remove_chip chip398 399echo "GPIO $MODULE test PASS"400