Opennet Firmware
 Alle Dateien Funktionen Variablen Gruppen Seiten
gpio_switch_control.patch
gehe zur Dokumentation dieser Datei
1 Allow GPIO control via uci during bootup
2 
3 GPIO control can be used for features like PoE passthrough.
4 This is implemented for Ubiquiti Nanostation (XM/XW) and TP-Link CPE510.
5 
6 diff --git a/openwrt/package/base-files/files/etc/init.d/gpio_switch b/openwrt/package/base-files/files/etc/init.d/gpio_switch
7 new file mode 100755
8 index 0000000..1f1b44b
9 --- /dev/null
10 +++ b/openwrt/package/base-files/files/etc/init.d/gpio_switch
11 @@ -0,0 +1,42 @@
12 +#!/bin/sh /etc/rc.common
13 +# Copyright (C) 2015 OpenWrt.org
14 +
15 +START=98
16 +STOP=10
17 +USE_PROCD=1
18 +
19 +
20 +load_gpio_switch()
21 +{
22 + local name
23 + local gpio_pin
24 + local value
25 +
26 + config_get gpio_pin "$1" gpio_pin
27 + config_get name "$1" name
28 + config_get value "$1" value 0
29 +
30 + local gpio_path="/sys/class/gpio/gpio${gpio_pin}"
31 + # export GPIO pin for access
32 + [ -d "$gpio_path" ] || {
33 + echo "$gpio_pin" >/sys/class/gpio/export
34 + # we need to wait a bit until the GPIO appears
35 + [ -d "$gpio_path" ] || sleep 1
36 + echo out >"$gpio_path/direction"
37 + }
38 + # write 0 or 1 to the "value" field
39 + { [ "$value" = "0" ] && echo "0" || echo "1"; } >"$gpio_path/value"
40 +}
41 +
42 +service_triggers()
43 +{
44 + procd_add_reload_trigger "system"
45 +}
46 +
47 +start_service()
48 +{
49 + [ -e /sys/class/gpio/ ] && {
50 + config_load system
51 + config_foreach load_gpio_switch gpio_switch
52 + }
53 +}
54 diff --git a/iopenwrt/package/base-files/files/lib/functions/uci-defaults.sh b/openwrt/package/base-files/files/lib/functions/uci-defaults.sh
55 index 5a8809d..6577ecd 100644
56 --- a/openwrt/package/base-files/files/lib/functions/uci-defaults.sh
57 +++ b/openwrt/package/base-files/files/lib/functions/uci-defaults.sh
58 @@ -2,6 +2,7 @@
59  # Copyright (C) 2011 OpenWrt.org
60 
61  UCIDEF_LEDS_CHANGED=0
62 +UCIDEF_GPIO_SWITCHES_CHANGED=0
63 
64  ucidef_set_led_netdev() {
65  local cfg="led_$1"
66 @@ -180,6 +181,29 @@ ucidef_commit_leds()
67  [ "$UCIDEF_LEDS_CHANGED" = "1" ] && uci commit system
68  }
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/openwrt/target/linux/ar71xx/base-files/etc/uci-defaults/01_gpio-switches b/openwrt/target/linux/ar71xx/base-files/etc/uci-defaults/01_gpio-switches
97 new file mode 100644
98 index 0000000..81d3982
99 --- /dev/null
100 +++ b/openwrt/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" "8"
115 + ;;
116 +nanostation-m-xw)
117 + ucidef_set_gpio_switch "poe_passthrough" "PoE Passthrough" "2"
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