gpio-mockup.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. #exit status
  4. #1: Internal error
  5. #2: sysfs/debugfs not mount
  6. #3: insert module fail when gpio-mockup is a module.
  7. #4: Skip test including run as non-root user.
  8. #5: other reason.
  9. SYSFS=
  10. GPIO_SYSFS=
  11. GPIO_DRV_SYSFS=
  12. DEBUGFS=
  13. GPIO_DEBUGFS=
  14. dev_type=
  15. module=
  16. # Kselftest framework requirement - SKIP code is 4.
  17. ksft_skip=4
  18. usage()
  19. {
  20. echo "Usage:"
  21. echo "$0 [-f] [-m name] [-t type]"
  22. echo "-f: full test. It maybe conflict with existence gpio device."
  23. echo "-m: module name, default name is gpio-mockup. It could also test"
  24. echo " other gpio device."
  25. echo "-t: interface type: chardev(char device) and sysfs(being"
  26. echo " deprecated). The first one is default"
  27. echo ""
  28. echo "$0 -h"
  29. echo "This usage"
  30. }
  31. prerequisite()
  32. {
  33. msg="skip all tests:"
  34. if [ $UID != 0 ]; then
  35. echo $msg must be run as root >&2
  36. exit $ksft_skip
  37. fi
  38. SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'`
  39. if [ ! -d "$SYSFS" ]; then
  40. echo $msg sysfs is not mounted >&2
  41. exit 2
  42. fi
  43. GPIO_SYSFS=`echo $SYSFS/class/gpio`
  44. GPIO_DRV_SYSFS=`echo $SYSFS/devices/platform/$module/gpio`
  45. DEBUGFS=`mount -t debugfs | head -1 | awk '{ print $3 }'`
  46. if [ ! -d "$DEBUGFS" ]; then
  47. echo $msg debugfs is not mounted >&2
  48. exit 2
  49. fi
  50. GPIO_DEBUGFS=`echo $DEBUGFS/gpio`
  51. source gpio-mockup-sysfs.sh
  52. }
  53. try_insert_module()
  54. {
  55. if [ -d "$GPIO_DRV_SYSFS" ]; then
  56. echo "$GPIO_DRV_SYSFS exist. Skip insert module"
  57. else
  58. modprobe -q $module $1
  59. if [ X$? != X0 ]; then
  60. echo $msg insmod $module failed >&2
  61. exit 3
  62. fi
  63. fi
  64. }
  65. remove_module()
  66. {
  67. modprobe -r -q $module
  68. }
  69. die()
  70. {
  71. remove_module
  72. exit 5
  73. }
  74. test_chips()
  75. {
  76. if [ X$dev_type = Xsysfs ]; then
  77. echo "WARNING: sysfs ABI of gpio is going to deprecated."
  78. test_chips_sysfs $*
  79. else
  80. $BASE/gpio-mockup-chardev $*
  81. fi
  82. }
  83. gpio_test()
  84. {
  85. param=$1
  86. valid=$2
  87. if [ X"$param" = X ]; then
  88. die
  89. fi
  90. try_insert_module "gpio_mockup_ranges=$param"
  91. echo -n "GPIO $module test with ranges: <"
  92. echo "$param>: "
  93. printf "%-10s %s\n" $param
  94. test_chips $module $valid
  95. remove_module
  96. }
  97. BASE=`dirname $0`
  98. dev_type=
  99. TEMP=`getopt -o fhm:t: -n '$0' -- "$@"`
  100. if [ "$?" != "0" ]; then
  101. echo "Parameter process failed, Terminating..." >&2
  102. exit 1
  103. fi
  104. # Note the quotes around `$TEMP': they are essential!
  105. eval set -- "$TEMP"
  106. while true; do
  107. case $1 in
  108. -f)
  109. full_test=true
  110. shift
  111. ;;
  112. -h)
  113. usage
  114. exit
  115. ;;
  116. -m)
  117. module=$2
  118. shift 2
  119. ;;
  120. -t)
  121. dev_type=$2
  122. shift 2
  123. ;;
  124. --)
  125. shift
  126. break
  127. ;;
  128. *)
  129. echo "Internal error!"
  130. exit 1
  131. ;;
  132. esac
  133. done
  134. if [ X"$module" = X ]; then
  135. module="gpio-mockup"
  136. fi
  137. if [ X$dev_type != Xsysfs ]; then
  138. dev_type="chardev"
  139. fi
  140. prerequisite
  141. echo "1. Test dynamic allocation of gpio successful means insert gpiochip and"
  142. echo " manipulate gpio pin successful"
  143. gpio_test "-1,32" true
  144. gpio_test "-1,32,-1,32" true
  145. gpio_test "-1,32,-1,32,-1,32" true
  146. if [ X$full_test = Xtrue ]; then
  147. gpio_test "-1,32,32,64" true
  148. gpio_test "-1,32,40,64,-1,5" true
  149. gpio_test "-1,32,32,64,-1,32" true
  150. gpio_test "0,32,32,64,-1,32,-1,32" true
  151. gpio_test "-1,32,-1,32,0,32,32,64" true
  152. echo "2. Do basic test: successful means insert gpiochip and"
  153. echo " manipulate gpio pin successful"
  154. gpio_test "0,32" true
  155. gpio_test "0,32,32,64" true
  156. gpio_test "0,32,40,64,64,96" true
  157. fi
  158. echo "3. Error test: successful means insert gpiochip failed"
  159. echo "3.1 Test number of gpio overflow"
  160. #Currently: The max number of gpio(1024) is defined in arm architecture.
  161. gpio_test "-1,32,-1,1024" false
  162. if [ X$full_test = Xtrue ]; then
  163. echo "3.2 Test zero line of gpio"
  164. gpio_test "0,0" false
  165. echo "3.3 Test range overlap"
  166. echo "3.3.1 Test corner case"
  167. gpio_test "0,32,0,1" false
  168. gpio_test "0,32,32,64,32,40" false
  169. gpio_test "0,32,35,64,35,45" false
  170. gpio_test "0,32,31,32" false
  171. gpio_test "0,32,32,64,36,37" false
  172. gpio_test "0,32,35,64,34,36" false
  173. echo "3.3.2 Test inserting invalid second gpiochip"
  174. gpio_test "0,32,30,35" false
  175. gpio_test "0,32,1,5" false
  176. gpio_test "10,32,9,14" false
  177. gpio_test "10,32,30,35" false
  178. echo "3.3.3 Test others"
  179. gpio_test "0,32,40,56,39,45" false
  180. gpio_test "0,32,40,56,30,33" false
  181. gpio_test "0,32,40,56,30,41" false
  182. gpio_test "0,32,40,56,20,21" false
  183. fi
  184. echo GPIO test PASS