#!/bin/bash ## $Id$ ## vim:ts=4:sw=4:nu:ai:nowrap: ## ## Created by Wolfram Schlich ## Licensed under the GNU GPLv2 or later ## ## Script for being called by udev to activate power and activity LEDs on Atheros cards ## ## Intended to be placed at /lib/udev/ath_led.sh ## ## Example udev rules for a Philips SNN6500 PCMCIA card (802.11abg) ## (e.g. update your /etc/udev/rules.d/70-persistent-net.rules): ## --8<--[ snip ]--8<-- ## SUBSYSTEM=="net", DRIVERS=="?*", ATTR{address}=="00:12:bf:12:ab:cd", \ ## ATTR{type}=="1", NAME="ath0" ## SUBSYSTEM=="net", DRIVERS=="?*", ATTR{address}=="00:12:bf:12:ab:cd", \ ## ATTR{type}=="801", RUN+="ath_led.sh -p 1 -a 0" ## --8<--[ snap ]--8<-- ## Please note you might change the -p and -a options depending on your card! ## ## ## functions ## ## logs via syslog and optionally stderr function log() { local log_syslog_facility="user" local log_syslog_level="${1}"; shift local log_syslog_pri="${log_syslog_facility}.${log_syslog_level}" local log_syslog_tag="${0##*/}[${$}]" # scriptname[PID] local logger_args= case "${log_syslog_level}" in err|crit|alert|emerg) ## log err|crit|alert|emerg messages to stderr as well logger_args='-s' ;; *) ;; esac logger ${logger_args} -p "${log_syslog_pri}" -t "${log_syslog_tag}" <<-EOF ${@} EOF } ## exits and optionally logs a message function die() { local retval=${1:-1}; shift if [[ ${#} -gt 0 ]]; then if [[ ${retval} -ne 0 ]]; then log crit "${@}" else log notice "${@}" fi fi exit ${retval} } ## ## parse command line options ## # # -a: activity LED pin (e.g. 0) # -g: power LED pin (e.g. 1) # declare -i act_led_pin=-1 pwr_led_pin=-1 while getopts ':a:p:' opt; do case "${opt}" in ## device data a) declare -i act_led_pin=${OPTARG};; p) declare -i pwr_led_pin=${OPTARG};; ## option without a required argument :) die 1 "option -${OPTARG} requires an argument";; ## unknown option \?) die 1 "unknown option -${OPTARG}";; ## this should never happen *) die 1 "there's an error in the matrix!";; esac done; opt_idx=${OPTIND}; unset OPTIND ## ## main() ## ## check if command line options were given at all if [[ ${opt_idx} -eq 1 ]]; then die 1 "no command line options specified" fi ## shift options + arguments off the positional parameters ## and process remaining positional parameters let opt_idx--; shift ${opt_idx}; unset opt_idx set -- "${@}" ## check for udev environment variable if [[ -z ${INTERFACE} ]]; then die 1 "udev environment variable INTERFACE missing (wifi device)" fi ## check for required arguments if [[ -z ${act_led_pin} ]]; then die 1 "${INTERFACE}: no activity LED pin specified" fi if [[ -z ${pwr_led_pin} ]]; then die 1 "${INTERFACE}: no power LED pin specified" fi ## first, configure power LED if [[ ${pwr_led_pin} -gt -1 ]]; then log info "${INTERFACE}: enabling power LED on pin #${pwr_led_pin}" sysctl -q -e -w dev.${INTERFACE}.ledpin=${pwr_led_pin} sysctl -q -e -w dev.${INTERFACE}.softled=1 sysctl -q -e -w dev.${INTERFACE}.softled=0 fi ## second, configure activity LED if [[ ${act_led_pin} -gt -1 ]]; then log info "${INTERFACE}: enabling activity LED on pin #${act_led_pin}" sysctl -q -e -w dev.${INTERFACE}.ledpin=${act_led_pin} sysctl -q -e -w dev.${INTERFACE}.softled=1 fi exit 0