rt2x00config.c 8.5 KB
Newer Older
1
/*
2
	Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
	<http://rt2x00.serialmonkey.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the
	Free Software Foundation, Inc.,
	59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/*
	Module: rt2x00lib
	Abstract: rt2x00 generic configuration routines.
 */

#include <linux/kernel.h>
#include <linux/module.h>

#include "rt2x00.h"
#include "rt2x00lib.h"

32
33
void rt2x00lib_config_intf(struct rt2x00_dev *rt2x00dev,
			   struct rt2x00_intf *intf,
34
			   enum nl80211_iftype type,
35
			   u8 *mac, u8 *bssid)
36
{
37
38
	struct rt2x00intf_conf conf;
	unsigned int flags = 0;
39

40
	conf.type = type;
41
42

	switch (type) {
43
44
	case NL80211_IFTYPE_ADHOC:
	case NL80211_IFTYPE_AP:
45
		conf.sync = TSF_SYNC_BEACON;
46
		break;
47
	case NL80211_IFTYPE_STATION:
48
		conf.sync = TSF_SYNC_INFRA;
49
50
		break;
	default:
51
		conf.sync = TSF_SYNC_NONE;
52
53
54
		break;
	}

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
	/*
	 * Note that when NULL is passed as address we will send
	 * 00:00:00:00:00 to the device to clear the address.
	 * This will prevent the device being confused when it wants
	 * to ACK frames or consideres itself associated.
	 */
	memset(&conf.mac, 0, sizeof(conf.mac));
	if (mac)
		memcpy(&conf.mac, mac, ETH_ALEN);

	memset(&conf.bssid, 0, sizeof(conf.bssid));
	if (bssid)
		memcpy(&conf.bssid, bssid, ETH_ALEN);

	flags |= CONFIG_UPDATE_TYPE;
	if (mac || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
		flags |= CONFIG_UPDATE_MAC;
	if (bssid || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
		flags |= CONFIG_UPDATE_BSSID;

	rt2x00dev->ops->lib->config_intf(rt2x00dev, intf, &conf, flags);
}

78
79
80
void rt2x00lib_config_erp(struct rt2x00_dev *rt2x00dev,
			  struct rt2x00_intf *intf,
			  struct ieee80211_bss_conf *bss_conf)
81
{
82
	struct rt2x00lib_erp erp;
83

84
85
86
	memset(&erp, 0, sizeof(erp));

	erp.short_preamble = bss_conf->use_short_preamble;
87
88
	erp.cts_protection = bss_conf->use_cts_prot;

89
90
	erp.ack_timeout = PLCP + get_duration(ACK_SIZE, 10);
	erp.ack_consume_time = SIFS + PLCP + get_duration(ACK_SIZE, 10);
91
92

	if (rt2x00dev->hw->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME)
93
		erp.ack_timeout += SHORT_DIFS;
94
	else
95
		erp.ack_timeout += DIFS;
96

97
98
99
	if (bss_conf->use_short_preamble) {
		erp.ack_timeout += SHORT_PREAMBLE;
		erp.ack_consume_time += SHORT_PREAMBLE;
100
	} else {
101
102
		erp.ack_timeout += PREAMBLE;
		erp.ack_consume_time += PREAMBLE;
103
104
	}

Ivo van Doorn's avatar
Ivo van Doorn committed
105
	rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp);
106
107
}

108
109
110
111
112
113
114
115
void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
			      enum antenna rx, enum antenna tx)
{
	struct rt2x00lib_conf libconf;

	libconf.ant.rx = rx;
	libconf.ant.tx = tx;

116
117
118
119
	if (rx == rt2x00dev->link.ant.active.rx &&
	    tx == rt2x00dev->link.ant.active.tx)
		return;

120
121
122
123
	/*
	 * Antenna setup changes require the RX to be disabled,
	 * else the changes will be ignored by the device.
	 */
124
	if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
125
		rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF_LINK);
126

127
128
129
130
131
	/*
	 * Write new antenna setup to device and reset the link tuner.
	 * The latter is required since we need to recalibrate the
	 * noise-sensitivity ratio for the new setup.
	 */
132
	rt2x00dev->ops->lib->config(rt2x00dev, &libconf, CONFIG_UPDATE_ANTENNA);
133
	rt2x00lib_reset_link_tuner(rt2x00dev);
134
	rt2x00_reset_link_ant_rssi(&rt2x00dev->link);
135
136
137

	rt2x00dev->link.ant.active.rx = libconf.ant.rx;
	rt2x00dev->link.ant.active.tx = libconf.ant.tx;
138

139
	if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
140
		rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON_LINK);
141
142
}

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
static u32 rt2x00lib_get_basic_rates(struct ieee80211_supported_band *band)
{
	const struct rt2x00_rate *rate;
	unsigned int i;
	u32 mask = 0;

	for (i = 0; i < band->n_bitrates; i++) {
		rate = rt2x00_get_rate(band->bitrates[i].hw_value);
		if (rate->flags & DEV_RATE_BASIC)
			mask |= rate->ratemask;
	}

	return mask;
}

158
159
void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
		      struct ieee80211_conf *conf, const int force_config)
160
{
161
	struct rt2x00lib_conf libconf;
162
	struct ieee80211_supported_band *band;
163
	struct antenna_setup *default_ant = &rt2x00dev->default_ant;
164
	struct antenna_setup *active_ant = &rt2x00dev->link.ant.active;
165
	int flags = 0;
166
	int short_slot_time;
167
168

	/*
169
170
	 * In some situations we want to force all configurations
	 * to be reloaded (When resuming for instance).
171
	 */
172
	if (force_config) {
173
174
175
176
177
178
179
180
		flags = CONFIG_UPDATE_ALL;
		goto config;
	}

	/*
	 * Check which configuration options have been
	 * updated and should be send to the device.
	 */
181
	if (rt2x00dev->rx_status.band != conf->channel->band)
182
		flags |= CONFIG_UPDATE_PHYMODE;
183
	if (rt2x00dev->rx_status.freq != conf->channel->center_freq)
184
185
186
		flags |= CONFIG_UPDATE_CHANNEL;
	if (rt2x00dev->tx_power != conf->power_level)
		flags |= CONFIG_UPDATE_TXPOWER;
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

	/*
	 * Determining changes in the antenna setups request several checks:
	 * antenna_sel_{r,t}x = 0
	 *    -> Does active_{r,t}x match default_{r,t}x
	 *    -> Is default_{r,t}x SW_DIVERSITY
	 * antenna_sel_{r,t}x = 1/2
	 *    -> Does active_{r,t}x match antenna_sel_{r,t}x
	 * The reason for not updating the antenna while SW diversity
	 * should be used is simple: Software diversity means that
	 * we should switch between the antenna's based on the
	 * quality. This means that the current antenna is good enough
	 * to work with untill the link tuner decides that an antenna
	 * switch should be performed.
	 */
202
	if (default_ant->rx != ANTENNA_SW_DIVERSITY &&
203
204
	    default_ant->rx != active_ant->rx)
		flags |= CONFIG_UPDATE_ANTENNA;
205
206
	else if (active_ant->rx == ANTENNA_SW_DIVERSITY)
		flags |= CONFIG_UPDATE_ANTENNA;
207

208
	if (default_ant->tx != ANTENNA_SW_DIVERSITY &&
209
210
	    default_ant->tx != active_ant->tx)
		flags |= CONFIG_UPDATE_ANTENNA;
211
212
	else if (active_ant->tx == ANTENNA_SW_DIVERSITY)
		flags |= CONFIG_UPDATE_ANTENNA;
213
214
215
216
217
218
219
220

	/*
	 * The following configuration options are never
	 * stored anywhere and will always be updated.
	 */
	flags |= CONFIG_UPDATE_SLOT_TIME;
	flags |= CONFIG_UPDATE_BEACON_INT;

221
222
223
224
225
	/*
	 * We have determined what options should be updated,
	 * now precalculate device configuration values depending
	 * on what configuration options need to be updated.
	 */
226
config:
227
228
229
	memset(&libconf, 0, sizeof(libconf));

	if (flags & CONFIG_UPDATE_PHYMODE) {
230
		band = &rt2x00dev->bands[conf->channel->band];
Ivo van Doorn's avatar
Ivo van Doorn committed
231
232

		libconf.band = conf->channel->band;
233
		libconf.basic_rates = rt2x00lib_get_basic_rates(band);
234
235
236
237
	}

	if (flags & CONFIG_UPDATE_CHANNEL) {
		memcpy(&libconf.rf,
238
		       &rt2x00dev->spec.channels[conf->channel->hw_value],
239
		       sizeof(libconf.rf));
240
241
242
243

		memcpy(&libconf.channel,
		       &rt2x00dev->spec.channels_info[conf->channel->hw_value],
		       sizeof(libconf.channel));
244
245
	}

246
	if (flags & CONFIG_UPDATE_ANTENNA) {
247
		if (default_ant->rx != ANTENNA_SW_DIVERSITY)
248
249
250
			libconf.ant.rx = default_ant->rx;
		else if (active_ant->rx == ANTENNA_SW_DIVERSITY)
			libconf.ant.rx = ANTENNA_B;
251
252
		else
			libconf.ant.rx = active_ant->rx;
253

254
		if (default_ant->tx != ANTENNA_SW_DIVERSITY)
255
256
257
			libconf.ant.tx = default_ant->tx;
		else if (active_ant->tx == ANTENNA_SW_DIVERSITY)
			libconf.ant.tx = ANTENNA_B;
258
259
		else
			libconf.ant.tx = active_ant->tx;
260
261
	}

262
263
264
265
266
267
268
269
	if (flags & CONFIG_UPDATE_SLOT_TIME) {
		short_slot_time = conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME;

		libconf.slot_time =
		    short_slot_time ? SHORT_SLOT_TIME : SLOT_TIME;
		libconf.sifs = SIFS;
		libconf.pifs = short_slot_time ? SHORT_PIFS : PIFS;
		libconf.difs = short_slot_time ? SHORT_DIFS : DIFS;
Ivo van Doorn's avatar
Ivo van Doorn committed
270
		libconf.eifs = short_slot_time ? SHORT_EIFS : EIFS;
271
272
273
274
275
276
277
	}

	libconf.conf = conf;

	/*
	 * Start configuration.
	 */
278
	rt2x00dev->ops->lib->config(rt2x00dev, &libconf, flags);
279
280
281
282
283
284
285
286

	/*
	 * Some configuration changes affect the link quality
	 * which means we need to reset the link tuner.
	 */
	if (flags & (CONFIG_UPDATE_CHANNEL | CONFIG_UPDATE_ANTENNA))
		rt2x00lib_reset_link_tuner(rt2x00dev);

287
	if (flags & CONFIG_UPDATE_PHYMODE) {
288
289
		rt2x00dev->curr_band = conf->channel->band;
		rt2x00dev->rx_status.band = conf->channel->band;
290
291
	}

292
	rt2x00dev->rx_status.freq = conf->channel->center_freq;
293
	rt2x00dev->tx_power = conf->power_level;
294
295
296
297
298

	if (flags & CONFIG_UPDATE_ANTENNA) {
		rt2x00dev->link.ant.active.rx = libconf.ant.rx;
		rt2x00dev->link.ant.active.tx = libconf.ant.tx;
	}
299
}