rt2x00: use generic EWMA functions for average RSSI calculations
[deliverable/linux.git] / drivers / net / wireless / rt2x00 / rt2x00link.c
index 8368aab86f286ee6ff5be65b85a73940f3c24cd0..a0e3c021c12821ebce5483462f185947e9948c9a 100644 (file)
  */
 #define DEFAULT_RSSI           -128
 
-/*
- * Helper struct and macro to work with moving/walking averages.
- * When adding a value to the average value the following calculation
- * is needed:
- *
- *        avg_rssi = ((avg_rssi * 7) + rssi) / 8;
- *
- * The advantage of this approach is that we only need 1 variable
- * to store the average in (No need for a count and a total).
- * But more importantly, normal average values will over time
- * move less and less towards newly added values this results
- * that with link tuning, the device can have a very good RSSI
- * for a few minutes but when the device is moved away from the AP
- * the average will not decrease fast enough to compensate.
- * The walking average compensates this and will move towards
- * the new values correctly allowing a effective link tuning,
- * the speed of the average moving towards other values depends
- * on the value for the number of samples. The higher the number
- * of samples, the slower the average will move.
- * We use two variables to keep track of the average value to
- * compensate for the rounding errors. This can be a significant
- * error (>5dBm) if the factor is too low.
- */
-#define AVG_SAMPLES    8
-#define AVG_FACTOR     1000
-#define MOVING_AVERAGE(__avg, __val) \
-({ \
-       struct avg_val __new; \
-       __new.avg_weight = \
-           (__avg).avg_weight  ? \
-               ((((__avg).avg_weight * ((AVG_SAMPLES) - 1)) + \
-                 ((__val) * (AVG_FACTOR))) / \
-                (AVG_SAMPLES)) : \
-               ((__val) * (AVG_FACTOR)); \
-       __new.avg = __new.avg_weight / (AVG_FACTOR); \
-       __new; \
-})
+/* Constants for EWMA calculations. */
+#define RT2X00_EWMA_FACTOR     1024
+#define RT2X00_EWMA_WEIGHT     8
+
+static inline int rt2x00link_get_avg_rssi(struct ewma *ewma)
+{
+       unsigned long avg;
+
+       avg = ewma_read(ewma);
+       if (avg)
+               return -avg;
+
+       return DEFAULT_RSSI;
+}
 
 static int rt2x00link_antenna_get_link_rssi(struct rt2x00_dev *rt2x00dev)
 {
        struct link_ant *ant = &rt2x00dev->link.ant;
 
-       if (ant->rssi_ant.avg && rt2x00dev->link.qual.rx_success)
-               return ant->rssi_ant.avg;
+       if (rt2x00dev->link.qual.rx_success)
+               return rt2x00link_get_avg_rssi(&ant->rssi_ant);
+
        return DEFAULT_RSSI;
 }
 
@@ -100,8 +78,8 @@ static void rt2x00link_antenna_update_rssi_history(struct rt2x00_dev *rt2x00dev,
 
 static void rt2x00link_antenna_reset(struct rt2x00_dev *rt2x00dev)
 {
-       rt2x00dev->link.ant.rssi_ant.avg = 0;
-       rt2x00dev->link.ant.rssi_ant.avg_weight = 0;
+       ewma_init(&rt2x00dev->link.ant.rssi_ant, RT2X00_EWMA_FACTOR,
+                 RT2X00_EWMA_WEIGHT);
 }
 
 static void rt2x00lib_antenna_diversity_sample(struct rt2x00_dev *rt2x00dev)
@@ -249,12 +227,12 @@ void rt2x00link_update_stats(struct rt2x00_dev *rt2x00dev,
        /*
         * Update global RSSI
         */
-       link->avg_rssi = MOVING_AVERAGE(link->avg_rssi, rxdesc->rssi);
+       ewma_add(&link->avg_rssi, -rxdesc->rssi);
 
        /*
         * Update antenna RSSI
         */
-       ant->rssi_ant = MOVING_AVERAGE(ant->rssi_ant, rxdesc->rssi);
+       ewma_add(&ant->rssi_ant, -rxdesc->rssi);
 }
 
 void rt2x00link_start_tuner(struct rt2x00_dev *rt2x00dev)
@@ -309,6 +287,8 @@ void rt2x00link_reset_tuner(struct rt2x00_dev *rt2x00dev, bool antenna)
         */
        rt2x00dev->link.count = 0;
        memset(qual, 0, sizeof(*qual));
+       ewma_init(&rt2x00dev->link.avg_rssi, RT2X00_EWMA_FACTOR,
+                 RT2X00_EWMA_WEIGHT);
 
        /*
         * Restore the VGC level as stored in the registers,
@@ -363,10 +343,10 @@ static void rt2x00link_tuner(struct work_struct *work)
         * collect the RSSI data we could use this. Otherwise we
         * must fallback to the default RSSI value.
         */
-       if (!link->avg_rssi.avg || !qual->rx_success)
+       if (!qual->rx_success)
                qual->rssi = DEFAULT_RSSI;
        else
-               qual->rssi = link->avg_rssi.avg;
+               qual->rssi = rt2x00link_get_avg_rssi(&link->avg_rssi);
 
        /*
         * Check if link tuning is supported by the hardware, some hardware
This page took 0.030282 seconds and 5 git commands to generate.