1 Allow GPIO control via uci during bootup
3 GPIO control can be used for features like PoE passthrough.
4 This is implemented for Ubiquiti Nanostation (XM/XW) and TP-Link CPE510.
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
10 +++ b/openwrt/package/base-files/files/etc/init.d/gpio_switch
12 +#!/bin/sh /etc/rc.common
13 +# Copyright (C) 2015 OpenWrt.org
26 + config_get gpio_pin "$1" gpio_pin
27 + config_get name "$1" name
28 + config_get value "$1" value 0
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"
38 + # write 0 or 1 to the "value" field
39 + { [ "$value" = "0" ] && echo "0" || echo "1"; } >"$gpio_path/value"
44 + procd_add_reload_trigger "system"
49 + [ -e /sys/class/gpio/ ] && {
51 + config_foreach load_gpio_switch gpio_switch
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
59 # Copyright (C) 2011 OpenWrt.org
62 +UCIDEF_GPIO_SWITCHES_CHANGED=0
64 ucidef_set_led_netdev() {
66 @@ -180,6 +181,29 @@ ucidef_commit_leds()
67 [ "$UCIDEF_LEDS_CHANGED" = "1" ] && uci commit system
70 +ucidef_set_gpio_switch() {
71 + local cfg="gpio_switch_$1"
74 + # use "0" as default value
75 + local default="${4:-0}"
77 + uci -q get "system.$cfg" && return 0
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'
85 + UCIDEF_GPIO_SWITCHES_CHANGED=1
88 +ucidef_commit_gpio_switches()
90 + [ "$UCIDEF_GPIO_SWITCHES_CHANGED" = "1" ] && uci commit system
93 ucidef_set_interface_loopback() {
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
98 index 0000000..81d3982
100 +++ b/openwrt/target/linux/ar71xx/base-files/etc/uci-defaults/01_gpio-switches
104 +# Copyright (C) 2015 OpenWrt.org
107 +. /lib/functions/uci-defaults.sh
110 +board=$(ar71xx_board_name)
114 + ucidef_set_gpio_switch "poe_passthrough" "PoE Passthrough" "8"
117 + ucidef_set_gpio_switch "poe_passthrough" "PoE Passthrough" "2"
120 + ucidef_set_gpio_switch "poe_passthrough" "PoE Passthrough" "20"
124 +ucidef_commit_gpio_switches