xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/ralink/rt2x00/rt2x00config.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun 	Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
4*4882a593Smuzhiyun 	<http://rt2x00.serialmonkey.com>
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun 	Module: rt2x00lib
10*4882a593Smuzhiyun 	Abstract: rt2x00 generic configuration routines.
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/kernel.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include "rt2x00.h"
17*4882a593Smuzhiyun #include "rt2x00lib.h"
18*4882a593Smuzhiyun 
rt2x00lib_config_intf(struct rt2x00_dev * rt2x00dev,struct rt2x00_intf * intf,enum nl80211_iftype type,const u8 * mac,const u8 * bssid)19*4882a593Smuzhiyun void rt2x00lib_config_intf(struct rt2x00_dev *rt2x00dev,
20*4882a593Smuzhiyun 			   struct rt2x00_intf *intf,
21*4882a593Smuzhiyun 			   enum nl80211_iftype type,
22*4882a593Smuzhiyun 			   const u8 *mac, const u8 *bssid)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun 	struct rt2x00intf_conf conf;
25*4882a593Smuzhiyun 	unsigned int flags = 0;
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun 	conf.type = type;
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun 	switch (type) {
30*4882a593Smuzhiyun 	case NL80211_IFTYPE_ADHOC:
31*4882a593Smuzhiyun 		conf.sync = TSF_SYNC_ADHOC;
32*4882a593Smuzhiyun 		break;
33*4882a593Smuzhiyun 	case NL80211_IFTYPE_AP:
34*4882a593Smuzhiyun 	case NL80211_IFTYPE_MESH_POINT:
35*4882a593Smuzhiyun 	case NL80211_IFTYPE_WDS:
36*4882a593Smuzhiyun 		conf.sync = TSF_SYNC_AP_NONE;
37*4882a593Smuzhiyun 		break;
38*4882a593Smuzhiyun 	case NL80211_IFTYPE_STATION:
39*4882a593Smuzhiyun 		conf.sync = TSF_SYNC_INFRA;
40*4882a593Smuzhiyun 		break;
41*4882a593Smuzhiyun 	default:
42*4882a593Smuzhiyun 		conf.sync = TSF_SYNC_NONE;
43*4882a593Smuzhiyun 		break;
44*4882a593Smuzhiyun 	}
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	/*
47*4882a593Smuzhiyun 	 * Note that when NULL is passed as address we will send
48*4882a593Smuzhiyun 	 * 00:00:00:00:00 to the device to clear the address.
49*4882a593Smuzhiyun 	 * This will prevent the device being confused when it wants
50*4882a593Smuzhiyun 	 * to ACK frames or considers itself associated.
51*4882a593Smuzhiyun 	 */
52*4882a593Smuzhiyun 	memset(conf.mac, 0, sizeof(conf.mac));
53*4882a593Smuzhiyun 	if (mac)
54*4882a593Smuzhiyun 		memcpy(conf.mac, mac, ETH_ALEN);
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	memset(conf.bssid, 0, sizeof(conf.bssid));
57*4882a593Smuzhiyun 	if (bssid)
58*4882a593Smuzhiyun 		memcpy(conf.bssid, bssid, ETH_ALEN);
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	flags |= CONFIG_UPDATE_TYPE;
61*4882a593Smuzhiyun 	if (mac || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
62*4882a593Smuzhiyun 		flags |= CONFIG_UPDATE_MAC;
63*4882a593Smuzhiyun 	if (bssid || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
64*4882a593Smuzhiyun 		flags |= CONFIG_UPDATE_BSSID;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	rt2x00dev->ops->lib->config_intf(rt2x00dev, intf, &conf, flags);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
rt2x00lib_config_erp(struct rt2x00_dev * rt2x00dev,struct rt2x00_intf * intf,struct ieee80211_bss_conf * bss_conf,u32 changed)69*4882a593Smuzhiyun void rt2x00lib_config_erp(struct rt2x00_dev *rt2x00dev,
70*4882a593Smuzhiyun 			  struct rt2x00_intf *intf,
71*4882a593Smuzhiyun 			  struct ieee80211_bss_conf *bss_conf,
72*4882a593Smuzhiyun 			  u32 changed)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	struct rt2x00lib_erp erp;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	memset(&erp, 0, sizeof(erp));
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	erp.short_preamble = bss_conf->use_short_preamble;
79*4882a593Smuzhiyun 	erp.cts_protection = bss_conf->use_cts_prot;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	erp.slot_time = bss_conf->use_short_slot ? SHORT_SLOT_TIME : SLOT_TIME;
82*4882a593Smuzhiyun 	erp.sifs = SIFS;
83*4882a593Smuzhiyun 	erp.pifs = bss_conf->use_short_slot ? SHORT_PIFS : PIFS;
84*4882a593Smuzhiyun 	erp.difs = bss_conf->use_short_slot ? SHORT_DIFS : DIFS;
85*4882a593Smuzhiyun 	erp.eifs = bss_conf->use_short_slot ? SHORT_EIFS : EIFS;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	erp.basic_rates = bss_conf->basic_rates;
88*4882a593Smuzhiyun 	erp.beacon_int = bss_conf->beacon_int;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	/* Update the AID, this is needed for dynamic PS support */
91*4882a593Smuzhiyun 	rt2x00dev->aid = bss_conf->assoc ? bss_conf->aid : 0;
92*4882a593Smuzhiyun 	rt2x00dev->last_beacon = bss_conf->sync_tsf;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	/* Update global beacon interval time, this is needed for PS support */
95*4882a593Smuzhiyun 	rt2x00dev->beacon_int = bss_conf->beacon_int;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	if (changed & BSS_CHANGED_HT)
98*4882a593Smuzhiyun 		erp.ht_opmode = bss_conf->ht_operation_mode;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp, changed);
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun 
rt2x00lib_config_antenna(struct rt2x00_dev * rt2x00dev,struct antenna_setup config)103*4882a593Smuzhiyun void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
104*4882a593Smuzhiyun 			      struct antenna_setup config)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	struct link_ant *ant = &rt2x00dev->link.ant;
107*4882a593Smuzhiyun 	struct antenna_setup *def = &rt2x00dev->default_ant;
108*4882a593Smuzhiyun 	struct antenna_setup *active = &rt2x00dev->link.ant.active;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	/*
111*4882a593Smuzhiyun 	 * When the caller tries to send the SW diversity,
112*4882a593Smuzhiyun 	 * we must update the ANTENNA_RX_DIVERSITY flag to
113*4882a593Smuzhiyun 	 * enable the antenna diversity in the link tuner.
114*4882a593Smuzhiyun 	 *
115*4882a593Smuzhiyun 	 * Secondly, we must guarentee we never send the
116*4882a593Smuzhiyun 	 * software antenna diversity command to the driver.
117*4882a593Smuzhiyun 	 */
118*4882a593Smuzhiyun 	if (!(ant->flags & ANTENNA_RX_DIVERSITY)) {
119*4882a593Smuzhiyun 		if (config.rx == ANTENNA_SW_DIVERSITY) {
120*4882a593Smuzhiyun 			ant->flags |= ANTENNA_RX_DIVERSITY;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 			if (def->rx == ANTENNA_SW_DIVERSITY)
123*4882a593Smuzhiyun 				config.rx = ANTENNA_B;
124*4882a593Smuzhiyun 			else
125*4882a593Smuzhiyun 				config.rx = def->rx;
126*4882a593Smuzhiyun 		}
127*4882a593Smuzhiyun 	} else if (config.rx == ANTENNA_SW_DIVERSITY)
128*4882a593Smuzhiyun 		config.rx = active->rx;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	if (!(ant->flags & ANTENNA_TX_DIVERSITY)) {
131*4882a593Smuzhiyun 		if (config.tx == ANTENNA_SW_DIVERSITY) {
132*4882a593Smuzhiyun 			ant->flags |= ANTENNA_TX_DIVERSITY;
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 			if (def->tx == ANTENNA_SW_DIVERSITY)
135*4882a593Smuzhiyun 				config.tx = ANTENNA_B;
136*4882a593Smuzhiyun 			else
137*4882a593Smuzhiyun 				config.tx = def->tx;
138*4882a593Smuzhiyun 		}
139*4882a593Smuzhiyun 	} else if (config.tx == ANTENNA_SW_DIVERSITY)
140*4882a593Smuzhiyun 		config.tx = active->tx;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	/*
143*4882a593Smuzhiyun 	 * Antenna setup changes require the RX to be disabled,
144*4882a593Smuzhiyun 	 * else the changes will be ignored by the device.
145*4882a593Smuzhiyun 	 */
146*4882a593Smuzhiyun 	if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
147*4882a593Smuzhiyun 		rt2x00queue_stop_queue(rt2x00dev->rx);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	/*
150*4882a593Smuzhiyun 	 * Write new antenna setup to device and reset the link tuner.
151*4882a593Smuzhiyun 	 * The latter is required since we need to recalibrate the
152*4882a593Smuzhiyun 	 * noise-sensitivity ratio for the new setup.
153*4882a593Smuzhiyun 	 */
154*4882a593Smuzhiyun 	rt2x00dev->ops->lib->config_ant(rt2x00dev, &config);
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	rt2x00link_reset_tuner(rt2x00dev, true);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	memcpy(active, &config, sizeof(config));
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
161*4882a593Smuzhiyun 		rt2x00queue_start_queue(rt2x00dev->rx);
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun 
rt2x00ht_center_channel(struct rt2x00_dev * rt2x00dev,struct ieee80211_conf * conf)164*4882a593Smuzhiyun static u16 rt2x00ht_center_channel(struct rt2x00_dev *rt2x00dev,
165*4882a593Smuzhiyun 				   struct ieee80211_conf *conf)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	struct hw_mode_spec *spec = &rt2x00dev->spec;
168*4882a593Smuzhiyun 	int center_channel;
169*4882a593Smuzhiyun 	u16 i;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	/*
172*4882a593Smuzhiyun 	 * Initialize center channel to current channel.
173*4882a593Smuzhiyun 	 */
174*4882a593Smuzhiyun 	center_channel = spec->channels[conf->chandef.chan->hw_value].channel;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	/*
177*4882a593Smuzhiyun 	 * Adjust center channel to HT40+ and HT40- operation.
178*4882a593Smuzhiyun 	 */
179*4882a593Smuzhiyun 	if (conf_is_ht40_plus(conf))
180*4882a593Smuzhiyun 		center_channel += 2;
181*4882a593Smuzhiyun 	else if (conf_is_ht40_minus(conf))
182*4882a593Smuzhiyun 		center_channel -= (center_channel == 14) ? 1 : 2;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	for (i = 0; i < spec->num_channels; i++)
185*4882a593Smuzhiyun 		if (spec->channels[i].channel == center_channel)
186*4882a593Smuzhiyun 			return i;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	WARN_ON(1);
189*4882a593Smuzhiyun 	return conf->chandef.chan->hw_value;
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
rt2x00lib_config(struct rt2x00_dev * rt2x00dev,struct ieee80211_conf * conf,unsigned int ieee80211_flags)192*4882a593Smuzhiyun void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
193*4882a593Smuzhiyun 		      struct ieee80211_conf *conf,
194*4882a593Smuzhiyun 		      unsigned int ieee80211_flags)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun 	struct rt2x00lib_conf libconf;
197*4882a593Smuzhiyun 	u16 hw_value;
198*4882a593Smuzhiyun 	u16 autowake_timeout;
199*4882a593Smuzhiyun 	u16 beacon_int;
200*4882a593Smuzhiyun 	u16 beacon_diff;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	memset(&libconf, 0, sizeof(libconf));
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	libconf.conf = conf;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL) {
207*4882a593Smuzhiyun 		if (!conf_is_ht(conf))
208*4882a593Smuzhiyun 			set_bit(CONFIG_HT_DISABLED, &rt2x00dev->flags);
209*4882a593Smuzhiyun 		else
210*4882a593Smuzhiyun 			clear_bit(CONFIG_HT_DISABLED, &rt2x00dev->flags);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 		if (conf_is_ht40(conf)) {
213*4882a593Smuzhiyun 			set_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
214*4882a593Smuzhiyun 			hw_value = rt2x00ht_center_channel(rt2x00dev, conf);
215*4882a593Smuzhiyun 		} else {
216*4882a593Smuzhiyun 			clear_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
217*4882a593Smuzhiyun 			hw_value = conf->chandef.chan->hw_value;
218*4882a593Smuzhiyun 		}
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 		memcpy(&libconf.rf,
221*4882a593Smuzhiyun 		       &rt2x00dev->spec.channels[hw_value],
222*4882a593Smuzhiyun 		       sizeof(libconf.rf));
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 		memcpy(&libconf.channel,
225*4882a593Smuzhiyun 		       &rt2x00dev->spec.channels_info[hw_value],
226*4882a593Smuzhiyun 		       sizeof(libconf.channel));
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 		/* Used for VCO periodic calibration */
229*4882a593Smuzhiyun 		rt2x00dev->rf_channel = libconf.rf.channel;
230*4882a593Smuzhiyun 	}
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_PS_AUTOWAKE) &&
233*4882a593Smuzhiyun 	    (ieee80211_flags & IEEE80211_CONF_CHANGE_PS))
234*4882a593Smuzhiyun 		cancel_delayed_work_sync(&rt2x00dev->autowakeup_work);
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	/*
237*4882a593Smuzhiyun 	 * Start configuration.
238*4882a593Smuzhiyun 	 */
239*4882a593Smuzhiyun 	rt2x00dev->ops->lib->config(rt2x00dev, &libconf, ieee80211_flags);
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	if (conf->flags & IEEE80211_CONF_PS)
242*4882a593Smuzhiyun 		set_bit(CONFIG_POWERSAVING, &rt2x00dev->flags);
243*4882a593Smuzhiyun 	else
244*4882a593Smuzhiyun 		clear_bit(CONFIG_POWERSAVING, &rt2x00dev->flags);
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	if (conf->flags & IEEE80211_CONF_MONITOR)
247*4882a593Smuzhiyun 		set_bit(CONFIG_MONITORING, &rt2x00dev->flags);
248*4882a593Smuzhiyun 	else
249*4882a593Smuzhiyun 		clear_bit(CONFIG_MONITORING, &rt2x00dev->flags);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	rt2x00dev->curr_band = conf->chandef.chan->band;
252*4882a593Smuzhiyun 	rt2x00dev->curr_freq = conf->chandef.chan->center_freq;
253*4882a593Smuzhiyun 	rt2x00dev->tx_power = conf->power_level;
254*4882a593Smuzhiyun 	rt2x00dev->short_retry = conf->short_frame_max_tx_count;
255*4882a593Smuzhiyun 	rt2x00dev->long_retry = conf->long_frame_max_tx_count;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	/*
258*4882a593Smuzhiyun 	 * Some configuration changes affect the link quality
259*4882a593Smuzhiyun 	 * which means we need to reset the link tuner.
260*4882a593Smuzhiyun 	 */
261*4882a593Smuzhiyun 	if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL)
262*4882a593Smuzhiyun 		rt2x00link_reset_tuner(rt2x00dev, false);
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
265*4882a593Smuzhiyun 	    rt2x00_has_cap_flag(rt2x00dev, REQUIRE_PS_AUTOWAKE) &&
266*4882a593Smuzhiyun 	    (ieee80211_flags & IEEE80211_CONF_CHANGE_PS) &&
267*4882a593Smuzhiyun 	    (conf->flags & IEEE80211_CONF_PS)) {
268*4882a593Smuzhiyun 		beacon_diff = (long)jiffies - (long)rt2x00dev->last_beacon;
269*4882a593Smuzhiyun 		beacon_int = msecs_to_jiffies(rt2x00dev->beacon_int);
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 		if (beacon_diff > beacon_int)
272*4882a593Smuzhiyun 			beacon_diff = 0;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 		autowake_timeout = (conf->ps_dtim_period * beacon_int) - beacon_diff;
275*4882a593Smuzhiyun 		queue_delayed_work(rt2x00dev->workqueue,
276*4882a593Smuzhiyun 				   &rt2x00dev->autowakeup_work,
277*4882a593Smuzhiyun 				   autowake_timeout - 15);
278*4882a593Smuzhiyun 	}
279*4882a593Smuzhiyun }
280