SOFTLED hack for more intuitive LED blinking,

created by Georg Lukas <georg@boerde.de> for madwifi CVS from 2004-05-29.

WARNING: use at your own risk! You've been warned!

diff -u -r1.140 if_ath.c
--- driver/if_ath.c	22 May 2004 00:38:32 -0000	1.140
+++ driver/if_ath.c	29 May 2004 10:48:13 -0000
@@ -386,7 +386,7 @@
 	DPRINTF(("ath_detach flags %x\n", dev->flags));
 #ifdef SOFTLED 
 	sc->sc_ic.ic_beaconCnt = 0;
-	ath_hal_gpioSet(sc->sc_ah,sc->sc_ic.ic_ledPin,1);
+	ath_hal_gpioSet(sc->sc_ah,sc->sc_ic.ic_ledPin,0);
 #endif
 	ath_stop(dev);
 	sc->sc_invalid = 1;
@@ -440,6 +440,12 @@
 	}
 	if (!ath_hal_intrpend(ah))		/* shared irq, not for us */
 		return IRQ_NONE;
+#ifdef SOFTLED
+	/* XXX WARNING: this is a very ugly hack! XXX */
+	if (sc->sc_ic.ic_caps & IEEE80211_C_SOFTLED) {
+		ath_update_LED(dev, ah, &sc->sc_ic, POLL_EVENT);
+	}
+#endif
 	if ((dev->flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP)) {
 		DPRINTF(("ath_intr: flags 0x%x\n", dev->flags));
 		ath_hal_getisr(ah, &status);	/* clear ISR */
@@ -1678,37 +1684,49 @@
 ath_update_LED (struct net_device *dev, struct ath_hal *ah, 
                 struct ieee80211com *ic, u_int32_t event)
 {
+	static unsigned int nextjiffies=0;
 	static unsigned int state=1;
-	int rateOn, rateOff, diff;
+	static unsigned int rfkillstate=1;
+	int rfkill, ifup;
+	//int rateOn, rateOff, diff;
 	
-	if (ic->ic_state != IEEE80211_S_RUN) {
-		// Device is in scan mode, searching for AP
-		/* We flash the LED on for 5s and off for 200ms */
-		if (ic->ic_beaconCnt >= (2+state*48)) {
+	ath_hal_gpioCfgInput(ah,ic->ic_rfKillPin);
+	rfkill = ath_hal_gpioGet(ah,ic->ic_rfKillPin);
+	if (rfkill != rfkillstate) {
+		printk("%s: rfKill state changed to %i\n", dev->name, rfkill);
+		rfkillstate = rfkill;
+		if (rfkill == 0) {
+			/* turn off LED */
 			ath_hal_gpioCfgOutput(ah,ic->ic_ledPin);
-			ath_hal_gpioSet(ah,ic->ic_ledPin,state);
-			state ^= 1;
-			ic->ic_beaconCnt = 0;
+			ath_hal_gpioSet(ah,ic->ic_ledPin,state & rfkillstate & ifup);
+			return;
 		}
 	}
+	
+	if (jiffies < nextjiffies) return;
+
+	if (ic->ic_state != IEEE80211_S_RUN) {
+		/* Device is in scan mode, searching for AP */
+		nextjiffies = jiffies + (66 - state*20)*HZ/100;
+		state ^= 1;
+	}
 	else {
 		switch(event) {
 		case TRANSMIT_EVENT:
-		case RECEIVE_EVENT:
-			rateOn = 20;
-			rateOff = 2;
-			diff = rateOn-rateOff;
-			if (ic->ic_beaconCnt >= (rateOff + state*diff)) {
-				ath_hal_gpioCfgOutput(ah,ic->ic_ledPin);
-				ath_hal_gpioSet(ah,ic->ic_ledPin,state);
-				state ^= 1;
-				ic->ic_beaconCnt = 0;
-			}
+			state ^= 1;
+			nextjiffies = jiffies + 5*HZ/100;
 			break;
+		case RECEIVE_EVENT:
 		default:
+			nextjiffies = jiffies;
+			state = 1;
 			break;
 		}
 	}
+	ifup = ((dev->flags & (IFF_RUNNING|IFF_UP)) == (IFF_RUNNING|IFF_UP));
+
+	ath_hal_gpioCfgOutput(ah,ic->ic_ledPin);
+	ath_hal_gpioSet(ah,ic->ic_ledPin,state & ifup);
 }
 #endif
 
