gpio-mockup-sysfs.sh 2.5 KB

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