mt76: mt7921: add .set_sar_specs support
authorYN Chen <YN.Chen@mediatek.com>
Mon, 28 Jun 2021 23:05:53 +0000 (07:05 +0800)
committerFelix Fietkau <nbd@nbd.name>
Wed, 20 Oct 2021 08:36:26 +0000 (10:36 +0200)
add .set_sar_specs to allow configuring SAR power limitations on the
frequency ranges from the userland.

Co-developed-by: Sean Wang <sean.wang@mediatek.com>
Signed-off-by: Sean Wang <sean.wang@mediatek.com>
Signed-off-by: YN Chen <YN.Chen@mediatek.com>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
drivers/net/wireless/mediatek/mt76/mac80211.c
drivers/net/wireless/mediatek/mt76/mt76.h
drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.c
drivers/net/wireless/mediatek/mt76/mt7921/init.c
drivers/net/wireless/mediatek/mt76/mt7921/main.c

index d03aedc3286bb601c7dad414abd2cd804933ea6c..ee3a5b8d62d2f5829b0e5a352c09b4cc81f1433d 100644 (file)
@@ -99,6 +99,21 @@ struct ieee80211_rate mt76_rates[] = {
 };
 EXPORT_SYMBOL_GPL(mt76_rates);
 
+static const struct cfg80211_sar_freq_ranges mt76_sar_freq_ranges[] = {
+       { .start_freq = 2402, .end_freq = 2494, },
+       { .start_freq = 5150, .end_freq = 5350, },
+       { .start_freq = 5350, .end_freq = 5470, },
+       { .start_freq = 5470, .end_freq = 5725, },
+       { .start_freq = 5725, .end_freq = 5950, },
+};
+
+const struct cfg80211_sar_capa mt76_sar_capa = {
+       .type = NL80211_SAR_TYPE_POWER,
+       .num_freq_ranges = ARRAY_SIZE(mt76_sar_freq_ranges),
+       .freq_ranges = &mt76_sar_freq_ranges[0],
+};
+EXPORT_SYMBOL_GPL(mt76_sar_capa);
+
 static int mt76_led_init(struct mt76_dev *dev)
 {
        struct device_node *np = dev->dev->of_node;
index 25c5ceef52577301c7b7776b0450d62d3f7903eb..ec42b87c9779e0918587b64702d4f16a97c84a8f 100644 (file)
@@ -545,6 +545,11 @@ struct mt76_rx_status {
        s8 chain_signal[IEEE80211_MAX_CHAINS];
 };
 
+struct mt76_freq_range_power {
+       const struct cfg80211_sar_freq_ranges *range;
+       s8 power;
+};
+
 struct mt76_testmode_ops {
        int (*set_state)(struct mt76_phy *phy, enum mt76_testmode_state state);
        int (*set_params)(struct mt76_phy *phy, struct nlattr **tb,
@@ -636,6 +641,8 @@ struct mt76_phy {
                struct sk_buff **tail;
                u16 seqno;
        } rx_amsdu[__MT_RXQ_MAX];
+
+       struct mt76_freq_range_power *frp;
 };
 
 struct mt76_dev {
@@ -769,6 +776,7 @@ enum mt76_phy_type {
 }
 
 extern struct ieee80211_rate mt76_rates[12];
+extern const struct cfg80211_sar_capa mt76_sar_capa;
 
 #define __mt76_rr(dev, ...)    (dev)->bus->rr((dev), __VA_ARGS__)
 #define __mt76_wr(dev, ...)    (dev)->bus->wr((dev), __VA_ARGS__)
index 5c3a81e5f559dda906096857e112116c623717f5..a2555dc0f0036ec1ec73d6347ef1e8fcf15da56a 100644 (file)
@@ -1749,6 +1749,30 @@ mt76_connac_mcu_build_sku(struct mt76_dev *dev, s8 *sku,
        }
 }
 
+static s8 mt76_connac_get_sar_power(struct mt76_phy *phy,
+                                   struct ieee80211_channel *chan,
+                                   s8 target_power)
+{
+       const struct cfg80211_sar_capa *capa = phy->hw->wiphy->sar_capa;
+       struct mt76_freq_range_power *frp = phy->frp;
+       int freq, i;
+
+       if (!capa || !frp)
+               return target_power;
+
+       freq = ieee80211_channel_to_frequency(chan->hw_value, chan->band);
+       for (i = 0 ; i < capa->num_freq_ranges; i++) {
+               if (frp[i].range &&
+                   freq >= frp[i].range->start_freq &&
+                   freq < frp[i].range->end_freq) {
+                       target_power = min_t(s8, frp[i].power, target_power);
+                       break;
+               }
+       }
+
+       return target_power;
+}
+
 static int
 mt76_connac_mcu_rate_txpower_band(struct mt76_phy *phy,
                                  enum nl80211_band band)
@@ -1816,9 +1840,13 @@ mt76_connac_mcu_rate_txpower_band(struct mt76_phy *phy,
                                .hw_value = ch_list[idx],
                                .band = band,
                        };
+                       s8 sar_power;
+
+                       sar_power = mt76_connac_get_sar_power(phy, &chan,
+                                                             tx_power);
 
                        mt76_get_rate_power_limits(phy, &chan, &limits,
-                                                  tx_power);
+                                                  sar_power);
 
                        tx_power_tlv.last_msg = ch_list[idx] == last_ch;
                        sku_tlbv.channel = ch_list[idx];
index 52d40385fab6cdac854528b2dbd531cd56632cb4..fd119b2608348804c4ef561af84d2a2c33e2ab94 100644 (file)
@@ -41,7 +41,7 @@ mt7921_regd_notifier(struct wiphy *wiphy,
        mt7921_mutex_release(dev);
 }
 
-static void
+static int
 mt7921_init_wiphy(struct ieee80211_hw *hw)
 {
        struct mt7921_phy *phy = mt7921_hw_phy(hw);
@@ -75,6 +75,14 @@ mt7921_init_wiphy(struct ieee80211_hw *hw)
        wiphy->max_sched_scan_reqs = 1;
        wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
        wiphy->reg_notifier = mt7921_regd_notifier;
+       wiphy->sar_capa = &mt76_sar_capa;
+
+       phy->mt76->frp = devm_kcalloc(dev->mt76.dev,
+                                     wiphy->sar_capa->num_freq_ranges,
+                                     sizeof(struct mt76_freq_range_power),
+                                     GFP_KERNEL);
+       if (!phy->mt76->frp)
+               return -ENOMEM;
 
        wiphy->features |= NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
                           NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
@@ -92,6 +100,8 @@ mt7921_init_wiphy(struct ieee80211_hw *hw)
                ieee80211_hw_set(hw, CONNECTION_MONITOR);
 
        hw->max_tx_fragments = 4;
+
+       return 0;
 }
 
 static void
@@ -211,7 +221,10 @@ int mt7921_register_device(struct mt7921_dev *dev)
        if (ret)
                return ret;
 
-       mt7921_init_wiphy(hw);
+       ret = mt7921_init_wiphy(hw);
+       if (ret)
+               return ret;
+
        dev->mphy.sband_2g.sband.ht_cap.cap |=
                        IEEE80211_HT_CAP_LDPC_CODING |
                        IEEE80211_HT_CAP_MAX_AMSDU;
index 63ec140c9c372f74388125a6585a13a0032f4a85..0ad9ffb52b6ea684d2e25c5fe82dde3773b6fc6d 100644 (file)
@@ -1172,6 +1172,43 @@ static void mt7921_sta_set_decap_offload(struct ieee80211_hw *hw,
                                             MCU_UNI_CMD_STA_REC_UPDATE);
 }
 
+static int mt7921_set_sar_specs(struct ieee80211_hw *hw,
+                               const struct cfg80211_sar_specs *sar)
+{
+       const struct cfg80211_sar_capa *capa = hw->wiphy->sar_capa;
+       struct mt7921_dev *dev = mt7921_hw_dev(hw);
+       struct mt76_freq_range_power *data, *frp;
+       struct mt76_phy *mphy = hw->priv;
+       int err;
+       u32 i;
+
+       if (sar->type != NL80211_SAR_TYPE_POWER || !sar->num_sub_specs)
+               return -EINVAL;
+
+       mt7921_mutex_acquire(dev);
+
+       data = mphy->frp;
+
+       for (i = 0; i < sar->num_sub_specs; i++) {
+               u32 index = sar->sub_specs[i].freq_range_index;
+               /* SAR specifies power limitaton in 0.25dbm */
+               s32 power = sar->sub_specs[i].power >> 1;
+
+               if (power > 127 || power < -127)
+                       power = 127;
+
+               frp = &data[index];
+               frp->range = &capa->freq_ranges[index];
+               frp->power = power;
+       }
+
+       err = mt76_connac_mcu_set_rate_txpower(mphy);
+
+       mt7921_mutex_release(dev);
+
+       return err;
+}
+
 const struct ieee80211_ops mt7921_ops = {
        .tx = mt7921_tx,
        .start = mt7921_start,
@@ -1210,4 +1247,5 @@ const struct ieee80211_ops mt7921_ops = {
        .set_rekey_data = mt7921_set_rekey_data,
 #endif /* CONFIG_PM */
        .flush = mt7921_flush,
+       .set_sar_specs = mt7921_set_sar_specs,
 };