0014-base-files-implemented-basic-GPIO-control.patch 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. From: Matthias Schiffer <mschiffer@universe-factory.net>
  2. Date: Mon, 7 Mar 2016 06:07:21 +0100
  3. Subject: base-files: implemented basic GPIO control
  4. Internal GPIO pins are used for PoE passthrough setups in multi-port
  5. routers. This patch implemnets control over this hardware feature for
  6. Ubiquiti Nanostations and TP-Link CPE510.
  7. Signed-off-by: Lars Kruse <lists@sumpfralle.de>
  8. Backport of r46271
  9. diff --git a/package/base-files/files/etc/init.d/gpio_switch b/package/base-files/files/etc/init.d/gpio_switch
  10. new file mode 100755
  11. index 0000000000000000000000000000000000000000..1f1b44b2129ce2315943f6a10508eefb66412c48
  12. --- /dev/null
  13. +++ b/package/base-files/files/etc/init.d/gpio_switch
  14. @@ -0,0 +1,42 @@
  15. +#!/bin/sh /etc/rc.common
  16. +# Copyright (C) 2015 OpenWrt.org
  17. +
  18. +START=98
  19. +STOP=10
  20. +USE_PROCD=1
  21. +
  22. +
  23. +load_gpio_switch()
  24. +{
  25. + local name
  26. + local gpio_pin
  27. + local value
  28. +
  29. + config_get gpio_pin "$1" gpio_pin
  30. + config_get name "$1" name
  31. + config_get value "$1" value 0
  32. +
  33. + local gpio_path="/sys/class/gpio/gpio${gpio_pin}"
  34. + # export GPIO pin for access
  35. + [ -d "$gpio_path" ] || {
  36. + echo "$gpio_pin" >/sys/class/gpio/export
  37. + # we need to wait a bit until the GPIO appears
  38. + [ -d "$gpio_path" ] || sleep 1
  39. + echo out >"$gpio_path/direction"
  40. + }
  41. + # write 0 or 1 to the "value" field
  42. + { [ "$value" = "0" ] && echo "0" || echo "1"; } >"$gpio_path/value"
  43. +}
  44. +
  45. +service_triggers()
  46. +{
  47. + procd_add_reload_trigger "system"
  48. +}
  49. +
  50. +start_service()
  51. +{
  52. + [ -e /sys/class/gpio/ ] && {
  53. + config_load system
  54. + config_foreach load_gpio_switch gpio_switch
  55. + }
  56. +}
  57. diff --git a/package/base-files/files/lib/functions/uci-defaults.sh b/package/base-files/files/lib/functions/uci-defaults.sh
  58. index 5a8809d68c89ac0431919f15dad5f5c33351bbf2..6577ecdcde0c856b3e6dd1b9337f23cb782ef2be 100644
  59. --- a/package/base-files/files/lib/functions/uci-defaults.sh
  60. +++ b/package/base-files/files/lib/functions/uci-defaults.sh
  61. @@ -2,6 +2,7 @@
  62. # Copyright (C) 2011 OpenWrt.org
  63. UCIDEF_LEDS_CHANGED=0
  64. +UCIDEF_GPIO_SWITCHES_CHANGED=0
  65. ucidef_set_led_netdev() {
  66. local cfg="led_$1"
  67. @@ -180,6 +181,29 @@ ucidef_commit_leds()
  68. [ "$UCIDEF_LEDS_CHANGED" = "1" ] && uci commit system
  69. }
  70. +ucidef_set_gpio_switch() {
  71. + local cfg="gpio_switch_$1"
  72. + local name="$2"
  73. + local gpio_pin="$3"
  74. + # use "0" as default value
  75. + local default="${4:-0}"
  76. +
  77. + uci -q get "system.$cfg" && return 0
  78. +
  79. + uci batch <<EOF
  80. +set system.$cfg='gpio_switch'
  81. +set system.$cfg.name='$name'
  82. +set system.$cfg.gpio_pin='$gpio_pin'
  83. +set system.$cfg.value='$default'
  84. +EOF
  85. + UCIDEF_GPIO_SWITCHES_CHANGED=1
  86. +}
  87. +
  88. +ucidef_commit_gpio_switches()
  89. +{
  90. + [ "$UCIDEF_GPIO_SWITCHES_CHANGED" = "1" ] && uci commit system
  91. +}
  92. +
  93. ucidef_set_interface_loopback() {
  94. uci batch <<EOF
  95. set network.loopback='interface'
  96. diff --git a/target/linux/ar71xx/base-files/etc/uci-defaults/01_gpio-switches b/target/linux/ar71xx/base-files/etc/uci-defaults/01_gpio-switches
  97. new file mode 100644
  98. index 0000000000000000000000000000000000000000..81d3982ed8bf98e01ce2e161017f088a1d0f60dc
  99. --- /dev/null
  100. +++ b/target/linux/ar71xx/base-files/etc/uci-defaults/01_gpio-switches
  101. @@ -0,0 +1,25 @@
  102. +#!/bin/sh
  103. +#
  104. +# Copyright (C) 2015 OpenWrt.org
  105. +#
  106. +
  107. +. /lib/functions/uci-defaults.sh
  108. +. /lib/ar71xx.sh
  109. +
  110. +board=$(ar71xx_board_name)
  111. +
  112. +case "$board" in
  113. +nanostation-m)
  114. + ucidef_set_gpio_switch "poe_passthrough" "PoE Passthrough" "2"
  115. + ;;
  116. +nanostation-m-xw)
  117. + ucidef_set_gpio_switch "poe_passthrough" "PoE Passthrough" "8"
  118. + ;;
  119. +cpe510)
  120. + ucidef_set_gpio_switch "poe_passthrough" "PoE Passthrough" "20"
  121. + ;;
  122. +esac
  123. +
  124. +ucidef_commit_gpio_switches
  125. +
  126. +exit 0