carl9170 firmware: report device tallies
[carl9170fw.git] / carlfw / src / rf.c
index 8bf34ea5e0aac3be7c4c7355b42c3abd9eb8057e..8deafb50cb167fbbe54845bbc7a583b27f0a92e5 100644 (file)
 #include "rf.h"
 #include "shared/phy.h"
 
-#ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
-static void set_channel_start(void)
+void tally_update(void)
 {
-       /* Manipulate CCA threshold to stop transmission */
-       set(AR9170_PHY_REG_CCA_THRESHOLD, 0x300);
-       /* Enable Virtual CCA */
-       orl(AR9170_MAC_REG_QOS_PRIORITY_VIRTUAL_CCA,
-           AR9170_MAC_VIRTUAL_CCA_ALL);
+       unsigned int time;
+
+#ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
+       unsigned int main_not_free, ext_not_free;
+
+       main_not_free = get(AR9170_MAC_REG_CHANNEL_BUSY);
+       ext_not_free = get(AR9170_MAC_REG_EXT_BUSY);
+       time = get_clock_counter();
+
+       if (fw.phy.state == CARL9170_PHY_ON) {
+               unsigned int us_delta = (time - fw.tally_clock) / max(fw.ticks_per_usec, 40u);
+
+               fw.tally.active += us_delta;
+               fw.tally.main_free += main_not_free;
+               fw.tally.ext_free += ext_not_free;
+       }
+#else
+       time = get_clock_counter();
+
+#endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
+
+       fw.tally_clock = time;
 }
 
+#ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
 static void set_channel_end(void)
 {
        /* Manipulate CCA threshold to resume transmission */
@@ -46,11 +63,28 @@ static void set_channel_end(void)
        /* Disable Virtual CCA */
        andl(AR9170_MAC_REG_QOS_PRIORITY_VIRTUAL_CCA,
             ~AR9170_MAC_VIRTUAL_CCA_ALL);
+
+       /* clear statistics */
+       tally_update();
+
+       fw.phy.state = CARL9170_PHY_ON;
 }
 
 void rf_notify_set_channel(void)
 {
-       set_channel_start();
+       tally_update();
+
+       /* Manipulate CCA threshold to stop transmission */
+       set(AR9170_PHY_REG_CCA_THRESHOLD, 0x300);
+       /* Enable Virtual CCA */
+       orl(AR9170_MAC_REG_QOS_PRIORITY_VIRTUAL_CCA,
+           AR9170_MAC_VIRTUAL_CCA_ALL);
+
+       /* reset CCA stats */
+       fw.tally.active = 0;
+       fw.tally.main_free = 0;
+       fw.tally.ext_free = 0;
+       fw.phy.state = CARL9170_PHY_OFF;
 }
 
 /*
@@ -242,7 +276,8 @@ void rf_psm(void)
                /* Synthesizer off + RX off */
                bank3 = 0x00400018;
 
-               clock_set(AHB_20_22MHZ, false);
+               tally_update();
+               fw.phy.state = CARL9170_PHY_OFF;
        } else {
                /* advance to the next PSM step */
                fw.phy.psm.state--;
@@ -259,10 +294,8 @@ void rf_psm(void)
                        /* Synthesizer on + RX on */
                        bank3 = 0x01420098;
 
-                       if ((fw.phy.ht_settings & EIGHTY_FLAG) == EIGHTY_FLAG)
-                               clock_set(AHB_80_88MHZ, true);
-                       else
-                               clock_set(AHB_40_44MHZ, true);
+                       tally_update();
+                       fw.phy.state = CARL9170_PHY_ON;
                } else {
                        return ;
                }