gpio-mockup.sh 4.0 KB

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