rt2x00config.c 5.9 KB
Newer Older
1
/*
I
Ivo van Doorn 已提交
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:
A
Andrey Yurovsky 已提交
45
	case NL80211_IFTYPE_MESH_POINT:
I
Ivo van Doorn 已提交
46
	case NL80211_IFTYPE_WDS:
47
		conf.sync = TSF_SYNC_BEACON;
48
		break;
49
	case NL80211_IFTYPE_STATION:
50
		conf.sync = TSF_SYNC_INFRA;
51 52
		break;
	default:
53
		conf.sync = TSF_SYNC_NONE;
54 55 56
		break;
	}

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	/*
	 * 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);
}

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

86 87 88
	memset(&erp, 0, sizeof(erp));

	erp.short_preamble = bss_conf->use_short_preamble;
89 90
	erp.cts_protection = bss_conf->use_cts_prot;

91 92 93 94 95
	erp.slot_time = bss_conf->use_short_slot ? SHORT_SLOT_TIME : SLOT_TIME;
	erp.sifs = SIFS;
	erp.pifs = bss_conf->use_short_slot ? SHORT_PIFS : PIFS;
	erp.difs = bss_conf->use_short_slot ? SHORT_DIFS : DIFS;
	erp.eifs = bss_conf->use_short_slot ? SHORT_EIFS : EIFS;
96

97 98
	erp.ack_timeout = PLCP + erp.difs + GET_DURATION(ACK_SIZE, 10);
	erp.ack_consume_time = SIFS + PLCP + GET_DURATION(ACK_SIZE, 10);
99

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

108 109
	erp.basic_rates = bss_conf->basic_rates;

I
Ivo van Doorn 已提交
110
	rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp);
111 112
}

I
Ivo van Doorn 已提交
113 114 115 116 117 118 119 120 121
static inline
enum antenna rt2x00lib_config_antenna_check(enum antenna current_ant,
					    enum antenna default_ant)
{
	if (current_ant != ANTENNA_SW_DIVERSITY)
		return current_ant;
	return (default_ant != ANTENNA_SW_DIVERSITY) ? default_ant : ANTENNA_B;
}

122
void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
123
			      struct antenna_setup *ant)
124
{
I
Ivo van Doorn 已提交
125 126 127
	struct antenna_setup *def = &rt2x00dev->default_ant;
	struct antenna_setup *active = &rt2x00dev->link.ant.active;

128 129 130 131 132
	/*
	 * Failsafe: Make sure we are not sending the
	 * ANTENNA_SW_DIVERSITY state to the driver.
	 * If that happes fallback to hardware default,
	 * or our own default.
I
Ivo van Doorn 已提交
133 134 135
	 * The calls to rt2x00lib_config_antenna_check()
	 * might have caused that we restore back to the already
	 * active setting. If that has happened we can quit.
136
	 */
I
Ivo van Doorn 已提交
137 138
	ant->rx = rt2x00lib_config_antenna_check(ant->rx, def->rx);
	ant->tx = rt2x00lib_config_antenna_check(ant->tx, def->tx);
139

I
Ivo van Doorn 已提交
140
	if (ant->rx == active->rx && ant->tx == active->tx)
141 142
		return;

143 144 145 146
	/*
	 * Antenna setup changes require the RX to be disabled,
	 * else the changes will be ignored by the device.
	 */
147
	if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
148
		rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF_LINK);
149

150 151 152 153 154
	/*
	 * 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.
	 */
155
	rt2x00dev->ops->lib->config_ant(rt2x00dev, ant);
156

157
	rt2x00link_reset_tuner(rt2x00dev, true);
158

I
Ivo van Doorn 已提交
159
	memcpy(active, ant, sizeof(*ant));
160

161
	if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
162
		rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON_LINK);
163 164
}

165
void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
166 167
		      struct ieee80211_conf *conf,
		      unsigned int ieee80211_flags)
168
{
169
	struct rt2x00lib_conf libconf;
170

171 172
	memset(&libconf, 0, sizeof(libconf));

173
	libconf.conf = conf;
174

175
	if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL) {
176
		memcpy(&libconf.rf,
177
		       &rt2x00dev->spec.channels[conf->channel->hw_value],
178
		       sizeof(libconf.rf));
179 180 181 182

		memcpy(&libconf.channel,
		       &rt2x00dev->spec.channels_info[conf->channel->hw_value],
		       sizeof(libconf.channel));
183 184 185 186 187
	}

	/*
	 * Start configuration.
	 */
188
	rt2x00dev->ops->lib->config(rt2x00dev, &libconf, ieee80211_flags);
189 190 191 192 193

	/*
	 * Some configuration changes affect the link quality
	 * which means we need to reset the link tuner.
	 */
194
	if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL)
195
		rt2x00link_reset_tuner(rt2x00dev, false);
196

197
	rt2x00dev->curr_band = conf->channel->band;
198
	rt2x00dev->tx_power = conf->power_level;
I
Ivo van Doorn 已提交
199 200
	rt2x00dev->short_retry = conf->short_frame_max_tx_count;
	rt2x00dev->long_retry = conf->long_frame_max_tx_count;
201

202 203
	rt2x00dev->rx_status.band = conf->channel->band;
	rt2x00dev->rx_status.freq = conf->channel->center_freq;
204
}