gpio-mockup.sh 4.0 KB

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