gpio-mockup.sh 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #!/bin/bash -efu
  2. # SPDX-License-Identifier: GPL-2.0
  3. #exit status
  4. #0: success
  5. #1: fail
  6. #4: skip test - including run as non-root user
  7. BASE=${0%/*}
  8. DEBUGFS=
  9. GPIO_DEBUGFS=
  10. dev_type="cdev"
  11. module="gpio-mockup"
  12. verbose=
  13. full_test=
  14. random=
  15. uapi_opt=
  16. active_opt=
  17. bias_opt=
  18. line_set_pid=
  19. # Kselftest return codes
  20. ksft_fail=1
  21. ksft_skip=4
  22. usage()
  23. {
  24. echo "Usage:"
  25. echo "$0 [-frv] [-t type]"
  26. echo "-f: full test (minimal set run by default)"
  27. echo "-r: test random lines as well as fence posts"
  28. echo "-t: interface type:"
  29. echo " cdev (character device ABI) - default"
  30. echo " cdev_v1 (deprecated character device ABI)"
  31. echo " sysfs (deprecated SYSFS ABI)"
  32. echo "-v: verbose progress reporting"
  33. exit $ksft_fail
  34. }
  35. skip()
  36. {
  37. echo "$*" >&2
  38. echo "GPIO $module test SKIP"
  39. exit $ksft_skip
  40. }
  41. prerequisite()
  42. {
  43. [ $(id -u) -eq 0 ] || skip "must be run as root"
  44. DEBUGFS=$(grep -w debugfs /proc/mounts | cut -f2 -d' ')
  45. [ -d "$DEBUGFS" ] || skip "debugfs is not mounted"
  46. GPIO_DEBUGFS=$DEBUGFS/$module
  47. }
  48. remove_module()
  49. {
  50. modprobe -r -q $module
  51. }
  52. cleanup()
  53. {
  54. set +e
  55. release_line
  56. remove_module
  57. jobs -p | xargs -r kill > /dev/null 2>&1
  58. }
  59. fail()
  60. {
  61. echo "test failed: $*" >&2
  62. echo "GPIO $module test FAIL"
  63. exit $ksft_fail
  64. }
  65. try_insert_module()
  66. {
  67. modprobe -q $module "$1" || fail "insert $module failed with error $?"
  68. }
  69. log()
  70. {
  71. [ -z "$verbose" ] || echo "$*"
  72. }
  73. # The following line helpers, release_Line, get_line and set_line, all
  74. # make use of the global $chip and $offset variables.
  75. #
  76. # This implementation drives the GPIO character device (cdev) uAPI.
  77. # Other implementations may override these to test different uAPIs.
  78. # Release any resources related to the line
  79. release_line()
  80. {
  81. [ "$line_set_pid" ] && kill $line_set_pid && wait $line_set_pid || true
  82. line_set_pid=
  83. }
  84. # Read the current value of the line
  85. get_line()
  86. {
  87. release_line
  88. local cdev_opts=${uapi_opt}${active_opt}
  89. $BASE/gpio-mockup-cdev $cdev_opts /dev/$chip $offset
  90. echo $?
  91. }
  92. # Set the state of the line
  93. #
  94. # Changes to line configuration are provided as parameters.
  95. # The line is assumed to be an output if the line value 0 or 1 is
  96. # specified, else an input.
  97. set_line()
  98. {
  99. local val=
  100. release_line
  101. # parse config options...
  102. for option in $*; do
  103. case $option in
  104. active-low)
  105. active_opt="-l "
  106. ;;
  107. active-high)
  108. active_opt=
  109. ;;
  110. bias-none)
  111. bias_opt=
  112. ;;
  113. pull-down)
  114. bias_opt="-bpull-down "
  115. ;;
  116. pull-up)
  117. bias_opt="-bpull-up "
  118. ;;
  119. 0)
  120. val=0
  121. ;;
  122. 1)
  123. val=1
  124. ;;
  125. esac
  126. done
  127. local cdev_opts=${uapi_opt}${active_opt}
  128. if [ "$val" ]; then
  129. $BASE/gpio-mockup-cdev $cdev_opts -s$val /dev/$chip $offset &
  130. # failure to set is detected by reading mockup and toggling values
  131. line_set_pid=$!
  132. # allow for gpio-mockup-cdev to launch and request line
  133. # (there is limited value in checking if line has been requested)
  134. sleep 0.01
  135. elif [ "$bias_opt" ]; then
  136. cdev_opts=${cdev_opts}${bias_opt}
  137. $BASE/gpio-mockup-cdev $cdev_opts /dev/$chip $offset || true
  138. fi
  139. }
  140. assert_line()
  141. {
  142. local val
  143. # don't need any retry here as set_mock allows for propagation
  144. val=$(get_line)
  145. [ "$val" = "$1" ] || fail "line value is ${val:-empty} when $1 was expected"
  146. }
  147. # The following mockup helpers all make use of the $mock_line
  148. assert_mock()
  149. {
  150. local backoff_wait=10
  151. local retry=0
  152. local val
  153. # retry allows for set propagation from uAPI to mockup
  154. while true; do
  155. val=$(< $mock_line)
  156. [ "$val" = "$1" ] && break
  157. retry=$((retry + 1))
  158. [ $retry -lt 5 ] || fail "mockup $mock_line value ${val:-empty} when $1 expected"
  159. sleep $(printf "%0.2f" $((backoff_wait))e-3)
  160. backoff_wait=$((backoff_wait * 2))
  161. done
  162. }
  163. set_mock()
  164. {
  165. echo "$1" > $mock_line
  166. # allow for set propagation - so we won't be in a race with set_line
  167. assert_mock "$1"
  168. }
  169. # test the functionality of a line
  170. #
  171. # The line is set from the mockup side and is read from the userspace side
  172. # (input), and is set from the userspace side and is read from the mockup side
  173. # (output).
  174. #
  175. # Setting the mockup pull using the userspace interface bias settings is
  176. # tested where supported by the userspace interface (cdev).
  177. test_line()
  178. {
  179. chip=$1
  180. offset=$2
  181. log "test_line $chip $offset"
  182. mock_line=$GPIO_DEBUGFS/$chip/$offset
  183. [ -e "$mock_line" ] || fail "missing line $chip:$offset"
  184. # test input active-high
  185. set_mock 1
  186. set_line input active-high
  187. assert_line 1
  188. set_mock 0
  189. assert_line 0
  190. set_mock 1
  191. assert_line 1
  192. if [ "$full_test" ]; then
  193. if [ "$dev_type" != "sysfs" ]; then
  194. # test pulls
  195. set_mock 0
  196. set_line input pull-up
  197. assert_line 1
  198. set_mock 0
  199. assert_line 0
  200. set_mock 1
  201. set_line input pull-down
  202. assert_line 0
  203. set_mock 1
  204. assert_line 1
  205. set_line bias-none
  206. fi
  207. # test input active-low
  208. set_mock 0
  209. set_line active-low
  210. assert_line 1
  211. set_mock 1
  212. assert_line 0
  213. set_mock 0
  214. assert_line 1
  215. # test output active-high
  216. set_mock 1
  217. set_line active-high 0
  218. assert_mock 0
  219. set_line 1
  220. assert_mock 1
  221. set_line 0
  222. assert_mock 0
  223. fi
  224. # test output active-low
  225. set_mock 0
  226. set_line active-low 0
  227. assert_mock 1
  228. set_line 1
  229. assert_mock 0
  230. set_line 0
  231. assert_mock 1
  232. release_line
  233. }
  234. test_no_line()
  235. {
  236. log test_no_line "$*"
  237. [ ! -e "$GPIO_DEBUGFS/$1/$2" ] || fail "unexpected line $1:$2"
  238. }
  239. # Load the module and check that the expected number of gpiochips, with the
  240. # expected number of lines, are created and are functional.
  241. #
  242. # $1 is the gpio_mockup_ranges parameter for the module
  243. # The remaining parameters are the number of lines, n, expected for each of
  244. # the gpiochips expected to be created.
  245. #
  246. # For each gpiochip the fence post lines, 0 and n-1, are tested, and the
  247. # line on the far side of the fence post, n, is tested to not exist.
  248. #
  249. # If the $random flag is set then a random line in the middle of the
  250. # gpiochip is tested as well.
  251. insmod_test()
  252. {
  253. local ranges=
  254. local gc=
  255. local width=
  256. [ "${1:-}" ] || fail "missing ranges"
  257. ranges=$1 ; shift
  258. try_insert_module "gpio_mockup_ranges=$ranges"
  259. log "GPIO $module test with ranges: <$ranges>:"
  260. # e.g. /sys/kernel/debug/gpio-mockup/gpiochip1
  261. gpiochip=$(find "$DEBUGFS/$module/" -name gpiochip* -type d | sort)
  262. for chip in $gpiochip; do
  263. gc=${chip##*/}
  264. [ "${1:-}" ] || fail "unexpected chip - $gc"
  265. width=$1 ; shift
  266. test_line $gc 0
  267. if [ "$random" -a $width -gt 2 ]; then
  268. test_line $gc $((RANDOM % ($width - 2) + 1))
  269. fi
  270. test_line $gc $(($width - 1))
  271. test_no_line $gc $width
  272. done
  273. [ "${1:-}" ] && fail "missing expected chip of width $1"
  274. remove_module || fail "failed to remove module with error $?"
  275. }
  276. while getopts ":frvt:" opt; do
  277. case $opt in
  278. f)
  279. full_test=true
  280. ;;
  281. r)
  282. random=true
  283. ;;
  284. t)
  285. dev_type=$OPTARG
  286. ;;
  287. v)
  288. verbose=true
  289. ;;
  290. *)
  291. usage
  292. ;;
  293. esac
  294. done
  295. shift $((OPTIND - 1))
  296. [ "${1:-}" ] && fail "unknown argument '$1'"
  297. prerequisite
  298. trap 'exit $ksft_fail' SIGTERM SIGINT
  299. trap cleanup EXIT
  300. case "$dev_type" in
  301. sysfs)
  302. source $BASE/gpio-mockup-sysfs.sh
  303. echo "WARNING: gpio sysfs ABI is deprecated."
  304. ;;
  305. cdev_v1)
  306. echo "WARNING: gpio cdev ABI v1 is deprecated."
  307. uapi_opt="-u1 "
  308. ;;
  309. cdev)
  310. ;;
  311. *)
  312. fail "unknown interface type: $dev_type"
  313. ;;
  314. esac
  315. remove_module || fail "can't remove existing $module module"
  316. # manual gpio allocation tests fail if a physical chip already exists
  317. [ "$full_test" -a -e "/dev/gpiochip0" ] && skip "full tests conflict with gpiochip0"
  318. echo "1. Module load tests"
  319. echo "1.1. dynamic allocation of gpio"
  320. insmod_test "-1,32" 32
  321. insmod_test "-1,23,-1,32" 23 32
  322. insmod_test "-1,23,-1,26,-1,32" 23 26 32
  323. if [ "$full_test" ]; then
  324. echo "1.2. manual allocation of gpio"
  325. insmod_test "0,32" 32
  326. insmod_test "0,32,32,60" 32 28
  327. insmod_test "0,32,40,64,64,96" 32 24 32
  328. echo "1.3. dynamic and manual allocation of gpio"
  329. insmod_test "-1,32,32,62" 32 30
  330. insmod_test "-1,22,-1,23,0,24,32,64" 22 23 24 32
  331. insmod_test "-1,32,32,60,-1,29" 32 28 29
  332. insmod_test "-1,32,40,64,-1,5" 32 24 5
  333. insmod_test "0,32,32,44,-1,22,-1,31" 32 12 22 31
  334. fi
  335. echo "2. Module load error tests"
  336. echo "2.1 no lines defined"
  337. insmod_test "0,0"
  338. if [ "$full_test" ]; then
  339. echo "2.2 ignore range overlap"
  340. insmod_test "0,32,0,1" 32
  341. insmod_test "0,32,1,5" 32
  342. insmod_test "0,32,30,35" 32
  343. insmod_test "0,32,31,32" 32
  344. insmod_test "10,32,30,35" 22
  345. insmod_test "10,32,9,14" 22
  346. insmod_test "0,32,20,21,40,56" 32 16
  347. insmod_test "0,32,32,64,32,40" 32 32
  348. insmod_test "0,32,32,64,36,37" 32 32
  349. insmod_test "0,32,35,64,34,36" 32 29
  350. insmod_test "0,30,35,64,35,45" 30 29
  351. insmod_test "0,32,40,56,30,33" 32 16
  352. insmod_test "0,32,40,56,30,41" 32 16
  353. insmod_test "0,32,40,56,39,45" 32 16
  354. fi
  355. echo "GPIO $module test PASS"