Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003     Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
0004     <http://rt2x00.serialmonkey.com>
0005 
0006  */
0007 
0008 /*
0009     Module: rt2x00lib
0010     Abstract: rt2x00 generic configuration routines.
0011  */
0012 
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 
0016 #include "rt2x00.h"
0017 #include "rt2x00lib.h"
0018 
0019 void rt2x00lib_config_intf(struct rt2x00_dev *rt2x00dev,
0020                struct rt2x00_intf *intf,
0021                enum nl80211_iftype type,
0022                const u8 *mac, const u8 *bssid)
0023 {
0024     struct rt2x00intf_conf conf;
0025     unsigned int flags = 0;
0026 
0027     conf.type = type;
0028 
0029     switch (type) {
0030     case NL80211_IFTYPE_ADHOC:
0031         conf.sync = TSF_SYNC_ADHOC;
0032         break;
0033     case NL80211_IFTYPE_AP:
0034     case NL80211_IFTYPE_MESH_POINT:
0035         conf.sync = TSF_SYNC_AP_NONE;
0036         break;
0037     case NL80211_IFTYPE_STATION:
0038         conf.sync = TSF_SYNC_INFRA;
0039         break;
0040     default:
0041         conf.sync = TSF_SYNC_NONE;
0042         break;
0043     }
0044 
0045     /*
0046      * Note that when NULL is passed as address we will send
0047      * 00:00:00:00:00 to the device to clear the address.
0048      * This will prevent the device being confused when it wants
0049      * to ACK frames or considers itself associated.
0050      */
0051     memset(conf.mac, 0, sizeof(conf.mac));
0052     if (mac)
0053         memcpy(conf.mac, mac, ETH_ALEN);
0054 
0055     memset(conf.bssid, 0, sizeof(conf.bssid));
0056     if (bssid)
0057         memcpy(conf.bssid, bssid, ETH_ALEN);
0058 
0059     flags |= CONFIG_UPDATE_TYPE;
0060     if (mac || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
0061         flags |= CONFIG_UPDATE_MAC;
0062     if (bssid || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
0063         flags |= CONFIG_UPDATE_BSSID;
0064 
0065     rt2x00dev->ops->lib->config_intf(rt2x00dev, intf, &conf, flags);
0066 }
0067 
0068 void rt2x00lib_config_erp(struct rt2x00_dev *rt2x00dev,
0069               struct rt2x00_intf *intf,
0070               struct ieee80211_bss_conf *bss_conf,
0071               u32 changed)
0072 {
0073     struct ieee80211_vif *vif = container_of(bss_conf, struct ieee80211_vif,
0074                          bss_conf);
0075     struct rt2x00lib_erp erp;
0076 
0077     memset(&erp, 0, sizeof(erp));
0078 
0079     erp.short_preamble = bss_conf->use_short_preamble;
0080     erp.cts_protection = bss_conf->use_cts_prot;
0081 
0082     erp.slot_time = bss_conf->use_short_slot ? SHORT_SLOT_TIME : SLOT_TIME;
0083     erp.sifs = SIFS;
0084     erp.pifs = bss_conf->use_short_slot ? SHORT_PIFS : PIFS;
0085     erp.difs = bss_conf->use_short_slot ? SHORT_DIFS : DIFS;
0086     erp.eifs = bss_conf->use_short_slot ? SHORT_EIFS : EIFS;
0087 
0088     erp.basic_rates = bss_conf->basic_rates;
0089     erp.beacon_int = bss_conf->beacon_int;
0090 
0091     /* Update the AID, this is needed for dynamic PS support */
0092     rt2x00dev->aid = vif->cfg.assoc ? vif->cfg.aid : 0;
0093     rt2x00dev->last_beacon = bss_conf->sync_tsf;
0094 
0095     /* Update global beacon interval time, this is needed for PS support */
0096     rt2x00dev->beacon_int = bss_conf->beacon_int;
0097 
0098     if (changed & BSS_CHANGED_HT)
0099         erp.ht_opmode = bss_conf->ht_operation_mode;
0100 
0101     rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp, changed);
0102 }
0103 
0104 void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
0105                   struct antenna_setup config)
0106 {
0107     struct link_ant *ant = &rt2x00dev->link.ant;
0108     struct antenna_setup *def = &rt2x00dev->default_ant;
0109     struct antenna_setup *active = &rt2x00dev->link.ant.active;
0110 
0111     /*
0112      * When the caller tries to send the SW diversity,
0113      * we must update the ANTENNA_RX_DIVERSITY flag to
0114      * enable the antenna diversity in the link tuner.
0115      *
0116      * Secondly, we must guarentee we never send the
0117      * software antenna diversity command to the driver.
0118      */
0119     if (!(ant->flags & ANTENNA_RX_DIVERSITY)) {
0120         if (config.rx == ANTENNA_SW_DIVERSITY) {
0121             ant->flags |= ANTENNA_RX_DIVERSITY;
0122 
0123             if (def->rx == ANTENNA_SW_DIVERSITY)
0124                 config.rx = ANTENNA_B;
0125             else
0126                 config.rx = def->rx;
0127         }
0128     } else if (config.rx == ANTENNA_SW_DIVERSITY)
0129         config.rx = active->rx;
0130 
0131     if (!(ant->flags & ANTENNA_TX_DIVERSITY)) {
0132         if (config.tx == ANTENNA_SW_DIVERSITY) {
0133             ant->flags |= ANTENNA_TX_DIVERSITY;
0134 
0135             if (def->tx == ANTENNA_SW_DIVERSITY)
0136                 config.tx = ANTENNA_B;
0137             else
0138                 config.tx = def->tx;
0139         }
0140     } else if (config.tx == ANTENNA_SW_DIVERSITY)
0141         config.tx = active->tx;
0142 
0143     /*
0144      * Antenna setup changes require the RX to be disabled,
0145      * else the changes will be ignored by the device.
0146      */
0147     if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
0148         rt2x00queue_stop_queue(rt2x00dev->rx);
0149 
0150     /*
0151      * Write new antenna setup to device and reset the link tuner.
0152      * The latter is required since we need to recalibrate the
0153      * noise-sensitivity ratio for the new setup.
0154      */
0155     rt2x00dev->ops->lib->config_ant(rt2x00dev, &config);
0156 
0157     rt2x00link_reset_tuner(rt2x00dev, true);
0158 
0159     memcpy(active, &config, sizeof(config));
0160 
0161     if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
0162         rt2x00queue_start_queue(rt2x00dev->rx);
0163 }
0164 
0165 static u16 rt2x00ht_center_channel(struct rt2x00_dev *rt2x00dev,
0166                    struct ieee80211_conf *conf)
0167 {
0168     struct hw_mode_spec *spec = &rt2x00dev->spec;
0169     int center_channel;
0170     u16 i;
0171 
0172     /*
0173      * Initialize center channel to current channel.
0174      */
0175     center_channel = spec->channels[conf->chandef.chan->hw_value].channel;
0176 
0177     /*
0178      * Adjust center channel to HT40+ and HT40- operation.
0179      */
0180     if (conf_is_ht40_plus(conf))
0181         center_channel += 2;
0182     else if (conf_is_ht40_minus(conf))
0183         center_channel -= (center_channel == 14) ? 1 : 2;
0184 
0185     for (i = 0; i < spec->num_channels; i++)
0186         if (spec->channels[i].channel == center_channel)
0187             return i;
0188 
0189     WARN_ON(1);
0190     return conf->chandef.chan->hw_value;
0191 }
0192 
0193 void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
0194               struct ieee80211_conf *conf,
0195               unsigned int ieee80211_flags)
0196 {
0197     struct rt2x00lib_conf libconf;
0198     u16 hw_value;
0199     u16 autowake_timeout;
0200     u16 beacon_int;
0201     u16 beacon_diff;
0202 
0203     memset(&libconf, 0, sizeof(libconf));
0204 
0205     libconf.conf = conf;
0206 
0207     if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL) {
0208         if (!conf_is_ht(conf))
0209             set_bit(CONFIG_HT_DISABLED, &rt2x00dev->flags);
0210         else
0211             clear_bit(CONFIG_HT_DISABLED, &rt2x00dev->flags);
0212 
0213         if (conf_is_ht40(conf)) {
0214             set_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
0215             hw_value = rt2x00ht_center_channel(rt2x00dev, conf);
0216         } else {
0217             clear_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
0218             hw_value = conf->chandef.chan->hw_value;
0219         }
0220 
0221         memcpy(&libconf.rf,
0222                &rt2x00dev->spec.channels[hw_value],
0223                sizeof(libconf.rf));
0224 
0225         memcpy(&libconf.channel,
0226                &rt2x00dev->spec.channels_info[hw_value],
0227                sizeof(libconf.channel));
0228 
0229         /* Used for VCO periodic calibration */
0230         rt2x00dev->rf_channel = libconf.rf.channel;
0231     }
0232 
0233     if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_PS_AUTOWAKE) &&
0234         (ieee80211_flags & IEEE80211_CONF_CHANGE_PS))
0235         cancel_delayed_work_sync(&rt2x00dev->autowakeup_work);
0236 
0237     /*
0238      * Start configuration.
0239      */
0240     rt2x00dev->ops->lib->config(rt2x00dev, &libconf, ieee80211_flags);
0241 
0242     if (conf->flags & IEEE80211_CONF_PS)
0243         set_bit(CONFIG_POWERSAVING, &rt2x00dev->flags);
0244     else
0245         clear_bit(CONFIG_POWERSAVING, &rt2x00dev->flags);
0246 
0247     if (conf->flags & IEEE80211_CONF_MONITOR)
0248         set_bit(CONFIG_MONITORING, &rt2x00dev->flags);
0249     else
0250         clear_bit(CONFIG_MONITORING, &rt2x00dev->flags);
0251 
0252     rt2x00dev->curr_band = conf->chandef.chan->band;
0253     rt2x00dev->curr_freq = conf->chandef.chan->center_freq;
0254     rt2x00dev->tx_power = conf->power_level;
0255     rt2x00dev->short_retry = conf->short_frame_max_tx_count;
0256     rt2x00dev->long_retry = conf->long_frame_max_tx_count;
0257 
0258     /*
0259      * Some configuration changes affect the link quality
0260      * which means we need to reset the link tuner.
0261      */
0262     if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL)
0263         rt2x00link_reset_tuner(rt2x00dev, false);
0264 
0265     if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
0266         rt2x00_has_cap_flag(rt2x00dev, REQUIRE_PS_AUTOWAKE) &&
0267         (ieee80211_flags & IEEE80211_CONF_CHANGE_PS) &&
0268         (conf->flags & IEEE80211_CONF_PS)) {
0269         beacon_diff = (long)jiffies - (long)rt2x00dev->last_beacon;
0270         beacon_int = msecs_to_jiffies(rt2x00dev->beacon_int);
0271 
0272         if (beacon_diff > beacon_int)
0273             beacon_diff = 0;
0274 
0275         autowake_timeout = (conf->ps_dtim_period * beacon_int) - beacon_diff;
0276         queue_delayed_work(rt2x00dev->workqueue,
0277                    &rt2x00dev->autowakeup_work,
0278                    autowake_timeout - 15);
0279     }
0280 }