gpio-mockup-sysfs.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # SPDX-License-Identifier: GPL-2.0
  2. is_consistent()
  3. {
  4. val=
  5. active_low_sysfs=`cat $GPIO_SYSFS/gpio$nr/active_low`
  6. val_sysfs=`cat $GPIO_SYSFS/gpio$nr/value`
  7. dir_sysfs=`cat $GPIO_SYSFS/gpio$nr/direction`
  8. gpio_this_debugfs=`cat $GPIO_DEBUGFS |grep "gpio-$nr" | sed "s/(.*)//g"`
  9. dir_debugfs=`echo $gpio_this_debugfs | awk '{print $2}'`
  10. val_debugfs=`echo $gpio_this_debugfs | awk '{print $3}'`
  11. if [ $val_debugfs = "lo" ]; then
  12. val=0
  13. elif [ $val_debugfs = "hi" ]; then
  14. val=1
  15. fi
  16. if [ $active_low_sysfs = "1" ]; then
  17. if [ $val = "0" ]; then
  18. val="1"
  19. else
  20. val="0"
  21. fi
  22. fi
  23. if [ $val_sysfs = $val ] && [ $dir_sysfs = $dir_debugfs ]; then
  24. echo -n "."
  25. else
  26. echo "test fail, exit"
  27. die
  28. fi
  29. }
  30. test_pin_logic()
  31. {
  32. nr=$1
  33. direction=$2
  34. active_low=$3
  35. value=$4
  36. echo $direction > $GPIO_SYSFS/gpio$nr/direction
  37. echo $active_low > $GPIO_SYSFS/gpio$nr/active_low
  38. if [ $direction = "out" ]; then
  39. echo $value > $GPIO_SYSFS/gpio$nr/value
  40. fi
  41. is_consistent $nr
  42. }
  43. test_one_pin()
  44. {
  45. nr=$1
  46. echo -n "test pin<$nr>"
  47. echo $nr > $GPIO_SYSFS/export 2>/dev/null
  48. if [ X$? != X0 ]; then
  49. echo "test GPIO pin $nr failed"
  50. die
  51. fi
  52. #"Checking if the sysfs is consistent with debugfs: "
  53. is_consistent $nr
  54. #"Checking the logic of active_low: "
  55. test_pin_logic $nr out 1 1
  56. test_pin_logic $nr out 1 0
  57. test_pin_logic $nr out 0 1
  58. test_pin_logic $nr out 0 0
  59. #"Checking the logic of direction: "
  60. test_pin_logic $nr in 1 1
  61. test_pin_logic $nr out 1 0
  62. test_pin_logic $nr low 0 1
  63. test_pin_logic $nr high 0 0
  64. echo $nr > $GPIO_SYSFS/unexport
  65. echo "successful"
  66. }
  67. test_one_pin_fail()
  68. {
  69. nr=$1
  70. echo $nr > $GPIO_SYSFS/export 2>/dev/null
  71. if [ X$? != X0 ]; then
  72. echo "test invalid pin $nr successful"
  73. else
  74. echo "test invalid pin $nr failed"
  75. echo $nr > $GPIO_SYSFS/unexport 2>/dev/null
  76. die
  77. fi
  78. }
  79. list_chip()
  80. {
  81. echo `ls -d $GPIO_DRV_SYSFS/gpiochip* 2>/dev/null`
  82. }
  83. test_chip()
  84. {
  85. chip=$1
  86. name=`basename $chip`
  87. base=`cat $chip/base`
  88. ngpio=`cat $chip/ngpio`
  89. printf "%-10s %-5s %-5s\n" $name $base $ngpio
  90. if [ $ngpio = "0" ]; then
  91. echo "number of gpio is zero is not allowed".
  92. fi
  93. test_one_pin $base
  94. test_one_pin $(($base + $ngpio - 1))
  95. test_one_pin $((( RANDOM % $ngpio ) + $base ))
  96. }
  97. test_chips_sysfs()
  98. {
  99. gpiochip=`list_chip $module`
  100. if [ X"$gpiochip" = X ]; then
  101. if [ X"$valid" = Xfalse ]; then
  102. echo "successful"
  103. else
  104. echo "fail"
  105. die
  106. fi
  107. else
  108. for chip in $gpiochip; do
  109. test_chip $chip
  110. done
  111. fi
  112. }