Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/tools/testing/selftests/gpio/gpio-sim.sh
26285 views
1
#!/bin/sh
2
# SPDX-License-Identifier: GPL-2.0
3
# Copyright (C) 2021 Bartosz Golaszewski <[email protected]>
4
5
BASE_DIR=`dirname $0`
6
CONFIGFS_DIR="/sys/kernel/config/gpio-sim"
7
MODULE="gpio-sim"
8
9
fail() {
10
echo "$*" >&2
11
echo "GPIO $MODULE test FAIL"
12
exit 1
13
}
14
15
skip() {
16
echo "$*" >&2
17
echo "GPIO $MODULE test SKIP"
18
exit 4
19
}
20
21
remove_chip() {
22
local CHIP=$1
23
24
for FILE in $CONFIGFS_DIR/$CHIP/*; do
25
BANK=`basename $FILE`
26
if [ "$BANK" = "live" -o "$BANK" = "dev_name" ]; then
27
continue
28
fi
29
30
LINES=`ls $CONFIGFS_DIR/$CHIP/$BANK/ | grep -E ^line`
31
if [ "$?" = 0 ]; then
32
for LINE in $LINES; do
33
if [ -e $CONFIGFS_DIR/$CHIP/$BANK/$LINE/hog ]; then
34
rmdir $CONFIGFS_DIR/$CHIP/$BANK/$LINE/hog || \
35
fail "Unable to remove the hog"
36
fi
37
38
rmdir $CONFIGFS_DIR/$CHIP/$BANK/$LINE || \
39
fail "Unable to remove the line"
40
done
41
fi
42
43
rmdir $CONFIGFS_DIR/$CHIP/$BANK
44
done
45
46
rmdir $CONFIGFS_DIR/$CHIP || fail "Unable to remove the chip"
47
}
48
49
create_chip() {
50
local CHIP=$1
51
52
mkdir $CONFIGFS_DIR/$CHIP
53
}
54
55
create_bank() {
56
local CHIP=$1
57
local BANK=$2
58
59
mkdir $CONFIGFS_DIR/$CHIP/$BANK
60
}
61
62
set_label() {
63
local CHIP=$1
64
local BANK=$2
65
local LABEL=$3
66
67
echo $LABEL > $CONFIGFS_DIR/$CHIP/$BANK/label || fail "Unable to set the chip label"
68
}
69
70
set_num_lines() {
71
local CHIP=$1
72
local BANK=$2
73
local NUM_LINES=$3
74
75
echo $NUM_LINES > $CONFIGFS_DIR/$CHIP/$BANK/num_lines || \
76
fail "Unable to set the number of lines"
77
}
78
79
set_line_name() {
80
local CHIP=$1
81
local BANK=$2
82
local OFFSET=$3
83
local NAME=$4
84
local LINE_DIR=$CONFIGFS_DIR/$CHIP/$BANK/line$OFFSET
85
86
test -d $LINE_DIR || mkdir $LINE_DIR
87
echo $NAME > $LINE_DIR/name || fail "Unable to set the line name"
88
}
89
90
enable_chip() {
91
local CHIP=$1
92
93
echo 1 > $CONFIGFS_DIR/$CHIP/live || fail "Unable to enable the chip"
94
}
95
96
disable_chip() {
97
local CHIP=$1
98
99
echo 0 > $CONFIGFS_DIR/$CHIP/live || fail "Unable to disable the chip"
100
}
101
102
configfs_cleanup() {
103
for CHIP in `ls $CONFIGFS_DIR/`; do
104
disable_chip $CHIP
105
remove_chip $CHIP
106
done
107
}
108
109
configfs_chip_name() {
110
local CHIP=$1
111
local BANK=$2
112
113
cat $CONFIGFS_DIR/$CHIP/$BANK/chip_name 2> /dev/null || \
114
fail "unable to read the chip name from configfs"
115
}
116
117
configfs_dev_name() {
118
local CHIP=$1
119
120
cat $CONFIGFS_DIR/$CHIP/dev_name 2> /dev/null || \
121
fail "unable to read the device name from configfs"
122
}
123
124
get_chip_num_lines() {
125
local CHIP=$1
126
local BANK=$2
127
128
$BASE_DIR/gpio-chip-info /dev/`configfs_chip_name $CHIP $BANK` num-lines || \
129
fail "unable to read the number of lines from the character device"
130
}
131
132
get_chip_label() {
133
local CHIP=$1
134
local BANK=$2
135
136
$BASE_DIR/gpio-chip-info /dev/`configfs_chip_name $CHIP $BANK` label || \
137
fail "unable to read the chip label from the character device"
138
}
139
140
get_line_name() {
141
local CHIP=$1
142
local BANK=$2
143
local OFFSET=$3
144
145
$BASE_DIR/gpio-line-name /dev/`configfs_chip_name $CHIP $BANK` $OFFSET || \
146
fail "unable to read the line name from the character device"
147
}
148
149
sysfs_set_pull() {
150
local DEV=$1
151
local BANK=$2
152
local OFFSET=$3
153
local PULL=$4
154
local DEVNAME=`configfs_dev_name $DEV`
155
local CHIPNAME=`configfs_chip_name $DEV $BANK`
156
local SYSFS_PATH="/sys/devices/platform/$DEVNAME/$CHIPNAME/sim_gpio$OFFSET/pull"
157
158
echo $PULL > $SYSFS_PATH || fail "Unable to set line pull in sysfs"
159
}
160
161
# Load the gpio-sim module. This will pull in configfs if needed too.
162
modprobe gpio-sim || skip "unable to load the gpio-sim module"
163
# Make sure configfs is mounted at /sys/kernel/config. Wait a bit if needed.
164
for IDX in `seq 5`; do
165
if [ "$IDX" -eq "5" ]; then
166
skip "configfs not mounted at /sys/kernel/config"
167
fi
168
169
mountpoint -q /sys/kernel/config && break
170
sleep 0.1
171
done
172
# If the module was already loaded: remove all previous chips
173
configfs_cleanup
174
175
trap "exit 1" SIGTERM SIGINT
176
trap configfs_cleanup EXIT
177
178
echo "1. chip_name and dev_name attributes"
179
180
echo "1.1. Chip name is communicated to user"
181
create_chip chip
182
create_bank chip bank
183
enable_chip chip
184
test -n `cat $CONFIGFS_DIR/chip/bank/chip_name` || fail "chip_name doesn't work"
185
disable_chip chip
186
remove_chip chip
187
188
echo "1.2. chip_name returns 'none' if the chip is still pending"
189
create_chip chip
190
create_bank chip bank
191
test "`cat $CONFIGFS_DIR/chip/bank/chip_name`" = "none" || \
192
fail "chip_name doesn't return 'none' for a pending chip"
193
remove_chip chip
194
195
echo "1.3. Device name is communicated to user"
196
create_chip chip
197
create_bank chip bank
198
enable_chip chip
199
test -n `cat $CONFIGFS_DIR/chip/dev_name` || fail "dev_name doesn't work"
200
disable_chip chip
201
remove_chip chip
202
203
echo "2. Creating and configuring simulated chips"
204
205
echo "2.1. Default number of lines is 1"
206
create_chip chip
207
create_bank chip bank
208
enable_chip chip
209
test "`get_chip_num_lines chip bank`" = "1" || fail "default number of lines is not 1"
210
disable_chip chip
211
remove_chip chip
212
213
echo "2.2. Number of lines can be specified"
214
create_chip chip
215
create_bank chip bank
216
set_num_lines chip bank 16
217
enable_chip chip
218
test "`get_chip_num_lines chip bank`" = "16" || fail "number of lines is not 16"
219
disable_chip chip
220
remove_chip chip
221
222
echo "2.3. Label can be set"
223
create_chip chip
224
create_bank chip bank
225
set_label chip bank foobar
226
enable_chip chip
227
test "`get_chip_label chip bank`" = "foobar" || fail "label is incorrect"
228
disable_chip chip
229
remove_chip chip
230
231
echo "2.4. Label can be left empty"
232
create_chip chip
233
create_bank chip bank
234
enable_chip chip
235
test -z "`cat $CONFIGFS_DIR/chip/bank/label`" || fail "label is not empty"
236
disable_chip chip
237
remove_chip chip
238
239
echo "2.5. Line names can be configured"
240
create_chip chip
241
create_bank chip bank
242
set_num_lines chip bank 16
243
set_line_name chip bank 0 foo
244
set_line_name chip bank 2 bar
245
enable_chip chip
246
test "`get_line_name chip bank 0`" = "foo" || fail "line name is incorrect"
247
test "`get_line_name chip bank 2`" = "bar" || fail "line name is incorrect"
248
disable_chip chip
249
remove_chip chip
250
251
echo "2.6. Line config can remain unused if offset is greater than number of lines"
252
create_chip chip
253
create_bank chip bank
254
set_num_lines chip bank 2
255
set_line_name chip bank 5 foobar
256
enable_chip chip
257
test "`get_line_name chip bank 0`" = "" || fail "line name is incorrect"
258
test "`get_line_name chip bank 1`" = "" || fail "line name is incorrect"
259
disable_chip chip
260
remove_chip chip
261
262
echo "2.7. Line configfs directory names are sanitized"
263
create_chip chip
264
create_bank chip bank
265
mkdir $CONFIGFS_DIR/chip/bank/line12foobar 2> /dev/null && \
266
fail "invalid configfs line name accepted"
267
mkdir $CONFIGFS_DIR/chip/bank/line_no_offset 2> /dev/null && \
268
fail "invalid configfs line name accepted"
269
remove_chip chip
270
271
echo "2.8. Multiple chips can be created"
272
CHIPS="chip0 chip1 chip2"
273
for CHIP in $CHIPS; do
274
create_chip $CHIP
275
create_bank $CHIP bank
276
enable_chip $CHIP
277
done
278
for CHIP in $CHIPS; do
279
disable_chip $CHIP
280
remove_chip $CHIP
281
done
282
283
echo "2.9. Can't modify settings when chip is live"
284
create_chip chip
285
create_bank chip bank
286
enable_chip chip
287
echo foobar > $CONFIGFS_DIR/chip/bank/label 2> /dev/null && \
288
fail "Setting label of a live chip should fail"
289
echo 8 > $CONFIGFS_DIR/chip/bank/num_lines 2> /dev/null && \
290
fail "Setting number of lines of a live chip should fail"
291
disable_chip chip
292
remove_chip chip
293
294
echo "2.10. Can't create line items when chip is live"
295
create_chip chip
296
create_bank chip bank
297
enable_chip chip
298
mkdir $CONFIGFS_DIR/chip/bank/line0 2> /dev/null && fail "Creating line item should fail"
299
disable_chip chip
300
remove_chip chip
301
302
echo "2.11. Probe errors are propagated to user-space"
303
create_chip chip
304
create_bank chip bank
305
set_num_lines chip bank 99999
306
echo 1 > $CONFIGFS_DIR/chip/live 2> /dev/null && fail "Probe error was not propagated"
307
remove_chip chip
308
309
echo "2.12. Cannot enable a chip without any GPIO banks"
310
create_chip chip
311
echo 1 > $CONFIGFS_DIR/chip/live 2> /dev/null && fail "Chip enabled without any GPIO banks"
312
remove_chip chip
313
314
echo "2.13. Duplicate chip labels are not allowed"
315
create_chip chip
316
create_bank chip bank0
317
set_label chip bank0 foobar
318
create_bank chip bank1
319
set_label chip bank1 foobar
320
echo 1 > $CONFIGFS_DIR/chip/live 2> /dev/null && fail "Duplicate chip labels were not rejected"
321
remove_chip chip
322
323
echo "2.14. Lines can be hogged"
324
create_chip chip
325
create_bank chip bank
326
set_num_lines chip bank 8
327
mkdir -p $CONFIGFS_DIR/chip/bank/line4/hog
328
enable_chip chip
329
$BASE_DIR/gpio-mockup-cdev -s 1 /dev/`configfs_chip_name chip bank` 4 2> /dev/null && \
330
fail "Setting the value of a hogged line shouldn't succeed"
331
disable_chip chip
332
remove_chip chip
333
334
echo "3. Controlling simulated chips"
335
336
echo "3.1. Pull can be set over sysfs"
337
create_chip chip
338
create_bank chip bank
339
set_num_lines chip bank 8
340
enable_chip chip
341
sysfs_set_pull chip bank 0 pull-up
342
$BASE_DIR/gpio-mockup-cdev /dev/`configfs_chip_name chip bank` 0
343
test "$?" = "1" || fail "pull set incorrectly"
344
sysfs_set_pull chip bank 0 pull-down
345
$BASE_DIR/gpio-mockup-cdev /dev/`configfs_chip_name chip bank` 1
346
test "$?" = "0" || fail "pull set incorrectly"
347
disable_chip chip
348
remove_chip chip
349
350
echo "3.2. Pull can be read from sysfs"
351
create_chip chip
352
create_bank chip bank
353
set_num_lines chip bank 8
354
enable_chip chip
355
DEVNAME=`configfs_dev_name chip`
356
CHIPNAME=`configfs_chip_name chip bank`
357
SYSFS_PATH=/sys/devices/platform/$DEVNAME/$CHIPNAME/sim_gpio0/pull
358
test `cat $SYSFS_PATH` = "pull-down" || fail "reading the pull failed"
359
sysfs_set_pull chip bank 0 pull-up
360
test `cat $SYSFS_PATH` = "pull-up" || fail "reading the pull failed"
361
disable_chip chip
362
remove_chip chip
363
364
echo "3.3. Incorrect input in sysfs is rejected"
365
create_chip chip
366
create_bank chip bank
367
set_num_lines chip bank 8
368
enable_chip chip
369
DEVNAME=`configfs_dev_name chip`
370
CHIPNAME=`configfs_chip_name chip bank`
371
SYSFS_PATH="/sys/devices/platform/$DEVNAME/$CHIPNAME/sim_gpio0/pull"
372
echo foobar > $SYSFS_PATH 2> /dev/null && fail "invalid input not detected"
373
disable_chip chip
374
remove_chip chip
375
376
echo "3.4. Can't write to value"
377
create_chip chip
378
create_bank chip bank
379
enable_chip chip
380
DEVNAME=`configfs_dev_name chip`
381
CHIPNAME=`configfs_chip_name chip bank`
382
SYSFS_PATH="/sys/devices/platform/$DEVNAME/$CHIPNAME/sim_gpio0/value"
383
echo 1 > $SYSFS_PATH 2> /dev/null && fail "writing to 'value' succeeded unexpectedly"
384
disable_chip chip
385
remove_chip chip
386
387
echo "4. Simulated GPIO chips are functional"
388
389
echo "4.1. Values can be read from sysfs"
390
create_chip chip
391
create_bank chip bank
392
set_num_lines chip bank 8
393
enable_chip chip
394
DEVNAME=`configfs_dev_name chip`
395
CHIPNAME=`configfs_chip_name chip bank`
396
SYSFS_PATH="/sys/devices/platform/$DEVNAME/$CHIPNAME/sim_gpio0/value"
397
test `cat $SYSFS_PATH` = "0" || fail "incorrect value read from sysfs"
398
$BASE_DIR/gpio-mockup-cdev -s 1 /dev/`configfs_chip_name chip bank` 0 &
399
sleep 0.1 # FIXME Any better way?
400
test `cat $SYSFS_PATH` = "1" || fail "incorrect value read from sysfs"
401
kill $!
402
disable_chip chip
403
remove_chip chip
404
405
echo "4.2. Bias settings work correctly"
406
create_chip chip
407
create_bank chip bank
408
set_num_lines chip bank 8
409
enable_chip chip
410
DEVNAME=`configfs_dev_name chip`
411
CHIPNAME=`configfs_chip_name chip bank`
412
SYSFS_PATH="/sys/devices/platform/$DEVNAME/$CHIPNAME/sim_gpio0/value"
413
$BASE_DIR/gpio-mockup-cdev -b pull-up /dev/`configfs_chip_name chip bank` 0
414
test `cat $SYSFS_PATH` = "1" || fail "bias setting does not work"
415
disable_chip chip
416
remove_chip chip
417
418
echo "GPIO $MODULE test PASS"
419
420