From patchwork Wed Jul 8 00:26:39 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lars Kruse X-Patchwork-Id: 492657 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (arrakis.dune.hu [78.24.191.176]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 9B17A1402A3 for ; Wed, 8 Jul 2015 10:26:56 +1000 (AEST) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 57DDA2848DA; Wed, 8 Jul 2015 02:26:28 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on arrakis.dune.hu X-Spam-Level: X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00 autolearn=unavailable version=3.3.2 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id E7856280264 for ; Wed, 8 Jul 2015 02:26:23 +0200 (CEST) X-policyd-weight: using cached result; rate: -5.5 Received: from mail.systemausfall.org (c.mail.systemausfall.org [80.153.252.42]) by arrakis.dune.hu (Postfix) with ESMTP for ; Wed, 8 Jul 2015 02:26:23 +0200 (CEST) Received: (qmail 3293 invoked from network); 8 Jul 2015 00:26:41 -0000 Received: from localhost.sao (HELO erker.lan) ([127.0.0.1]) (envelope-sender ) by mail.systemausfall.org (qmail-ldap-1.03) with SMTP for ; 8 Jul 2015 00:26:41 -0000 From: Lars Kruse To: openwrt-devel@lists.openwrt.org Date: Wed, 8 Jul 2015 02:26:39 +0200 Message-Id: <1436315199-13925-1-git-send-email-lists@sumpfralle.de> X-Mailer: git-send-email 2.1.4 Subject: [OpenWrt-Devel] [PATCH] base-files: implemented basic GPIO control X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openwrt-devel-bounces@lists.openwrt.org Sender: "openwrt-devel" Internal GPIO pins are used for PoE passthrough setups in multi-port routers. This patch implemnets control over this hardware feature for Ubiquiti Nanostations and TP-Link CPE510. Signed-off-by: Lars Kruse --- package/base-files/files/etc/init.d/gpio_switch | 42 ++++++++++++++++++++++ .../base-files/files/lib/functions/uci-defaults.sh | 24 +++++++++++++ .../base-files/etc/uci-defaults/01_gpio-switches | 25 +++++++++++++ 3 files changed, 91 insertions(+) create mode 100755 package/base-files/files/etc/init.d/gpio_switch create mode 100644 target/linux/ar71xx/base-files/etc/uci-defaults/01_gpio-switches diff --git a/package/base-files/files/etc/init.d/gpio_switch b/package/base-files/files/etc/init.d/gpio_switch new file mode 100755 index 0000000..1f1b44b --- /dev/null +++ b/package/base-files/files/etc/init.d/gpio_switch @@ -0,0 +1,42 @@ +#!/bin/sh /etc/rc.common +# Copyright (C) 2015 OpenWrt.org + +START=98 +STOP=10 +USE_PROCD=1 + + +load_gpio_switch() +{ + local name + local gpio_pin + local value + + config_get gpio_pin "$1" gpio_pin + config_get name "$1" name + config_get value "$1" value 0 + + local gpio_path="/sys/class/gpio/gpio${gpio_pin}" + # export GPIO pin for access + [ -d "$gpio_path" ] || { + echo "$gpio_pin" >/sys/class/gpio/export + # we need to wait a bit until the GPIO appears + [ -d "$gpio_path" ] || sleep 1 + echo out >"$gpio_path/direction" + } + # write 0 or 1 to the "value" field + { [ "$value" = "0" ] && echo "0" || echo "1"; } >"$gpio_path/value" +} + +service_triggers() +{ + procd_add_reload_trigger "system" +} + +start_service() +{ + [ -e /sys/class/gpio/ ] && { + config_load system + config_foreach load_gpio_switch gpio_switch + } +} diff --git a/package/base-files/files/lib/functions/uci-defaults.sh b/package/base-files/files/lib/functions/uci-defaults.sh index 5a8809d..6577ecd 100644 --- a/package/base-files/files/lib/functions/uci-defaults.sh +++ b/package/base-files/files/lib/functions/uci-defaults.sh @@ -2,6 +2,7 @@ # Copyright (C) 2011 OpenWrt.org UCIDEF_LEDS_CHANGED=0 +UCIDEF_GPIO_SWITCHES_CHANGED=0 ucidef_set_led_netdev() { local cfg="led_$1" @@ -180,6 +181,29 @@ ucidef_commit_leds() [ "$UCIDEF_LEDS_CHANGED" = "1" ] && uci commit system } +ucidef_set_gpio_switch() { + local cfg="gpio_switch_$1" + local name="$2" + local gpio_pin="$3" + # use "0" as default value + local default="${4:-0}" + + uci -q get "system.$cfg" && return 0 + + uci batch <