blob: 07adc576db49dc28f92819c6ec46f04e926b4b48 [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
2 Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3 <http://rt2x00.serialmonkey.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the
17 Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21/*
22 Module: rt2x00lib
23 Abstract: rt2x00 generic configuration routines.
24 */
25
Ivo van Doorn95ea3622007-09-25 17:57:13 -070026#include <linux/kernel.h>
27#include <linux/module.h>
28
29#include "rt2x00.h"
30#include "rt2x00lib.h"
31
Ivo van Doorn4abee4b2007-10-06 14:11:46 +020032
33/*
34 * The MAC and BSSID addressess are simple array of bytes,
35 * these arrays are little endian, so when sending the addressess
36 * to the drivers, copy the it into a endian-signed variable.
37 *
38 * Note that all devices (except rt2500usb) have 32 bits
39 * register word sizes. This means that whatever variable we
40 * pass _must_ be a multiple of 32 bits. Otherwise the device
41 * might not accept what we are sending to it.
42 * This will also make it easier for the driver to write
43 * the data to the device.
44 *
45 * Also note that when NULL is passed as address the
46 * we will send 00:00:00:00:00 to the device to clear the address.
47 * This will prevent the device being confused when it wants
48 * to ACK frames or consideres itself associated.
49 */
Ivo van Doorn95ea3622007-09-25 17:57:13 -070050void rt2x00lib_config_mac_addr(struct rt2x00_dev *rt2x00dev, u8 *mac)
51{
Ivo van Doorn4abee4b2007-10-06 14:11:46 +020052 __le32 reg[2];
53
54 memset(&reg, 0, sizeof(reg));
Ivo van Doorn95ea3622007-09-25 17:57:13 -070055 if (mac)
Ivo van Doorn4abee4b2007-10-06 14:11:46 +020056 memcpy(&reg, mac, ETH_ALEN);
57
58 rt2x00dev->ops->lib->config_mac_addr(rt2x00dev, &reg[0]);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070059}
60
61void rt2x00lib_config_bssid(struct rt2x00_dev *rt2x00dev, u8 *bssid)
62{
Ivo van Doorn4abee4b2007-10-06 14:11:46 +020063 __le32 reg[2];
64
65 memset(&reg, 0, sizeof(reg));
Ivo van Doorn95ea3622007-09-25 17:57:13 -070066 if (bssid)
Ivo van Doorn4abee4b2007-10-06 14:11:46 +020067 memcpy(&reg, bssid, ETH_ALEN);
68
69 rt2x00dev->ops->lib->config_bssid(rt2x00dev, &reg[0]);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070070}
71
Ivo van Doornfeb24692007-10-06 14:14:29 +020072void rt2x00lib_config_type(struct rt2x00_dev *rt2x00dev, const int type)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070073{
Ivo van Doornfeb24692007-10-06 14:14:29 +020074 int tsf_sync;
75
76 switch (type) {
77 case IEEE80211_IF_TYPE_IBSS:
78 case IEEE80211_IF_TYPE_AP:
79 tsf_sync = TSF_SYNC_BEACON;
80 break;
81 case IEEE80211_IF_TYPE_STA:
82 tsf_sync = TSF_SYNC_INFRA;
83 break;
84 default:
85 tsf_sync = TSF_SYNC_NONE;
86 break;
87 }
88
89 rt2x00dev->ops->lib->config_type(rt2x00dev, type, tsf_sync);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070090}
91
Ivo van Doorn69f81a22007-10-13 16:26:36 +020092void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
93 enum antenna rx, enum antenna tx)
94{
95 struct rt2x00lib_conf libconf;
96
97 libconf.ant.rx = rx;
98 libconf.ant.tx = tx;
99
Ivo van Doorn05253c92008-02-25 23:15:08 +0100100 if (rx == rt2x00dev->link.ant.active.rx &&
101 tx == rt2x00dev->link.ant.active.tx)
102 return;
103
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200104 /*
Ivo van Doorne25c4bb2007-10-27 13:39:28 +0200105 * Antenna setup changes require the RX to be disabled,
106 * else the changes will be ignored by the device.
107 */
108 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn61667d82008-02-25 23:15:05 +0100109 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF_LINK);
Ivo van Doorne25c4bb2007-10-27 13:39:28 +0200110
111 /*
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200112 * Write new antenna setup to device and reset the link tuner.
113 * The latter is required since we need to recalibrate the
114 * noise-sensitivity ratio for the new setup.
115 */
116 rt2x00dev->ops->lib->config(rt2x00dev, CONFIG_UPDATE_ANTENNA, &libconf);
117 rt2x00lib_reset_link_tuner(rt2x00dev);
118
119 rt2x00dev->link.ant.active.rx = libconf.ant.rx;
120 rt2x00dev->link.ant.active.tx = libconf.ant.tx;
Ivo van Doorne25c4bb2007-10-27 13:39:28 +0200121
122 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn61667d82008-02-25 23:15:05 +0100123 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON_LINK);
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200124}
125
Ivo van Doorn066cb632007-09-25 20:55:39 +0200126void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
127 struct ieee80211_conf *conf, const int force_config)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700128{
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200129 struct rt2x00lib_conf libconf;
130 struct ieee80211_hw_mode *mode;
131 struct ieee80211_rate *rate;
Ivo van Doornaddc81bd2007-10-13 16:26:23 +0200132 struct antenna_setup *default_ant = &rt2x00dev->default_ant;
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200133 struct antenna_setup *active_ant = &rt2x00dev->link.ant.active;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700134 int flags = 0;
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200135 int short_slot_time;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700136
137 /*
Ivo van Doorn066cb632007-09-25 20:55:39 +0200138 * In some situations we want to force all configurations
139 * to be reloaded (When resuming for instance).
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700140 */
Ivo van Doorn066cb632007-09-25 20:55:39 +0200141 if (force_config) {
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700142 flags = CONFIG_UPDATE_ALL;
143 goto config;
144 }
145
146 /*
147 * Check which configuration options have been
148 * updated and should be send to the device.
149 */
150 if (rt2x00dev->rx_status.phymode != conf->phymode)
151 flags |= CONFIG_UPDATE_PHYMODE;
152 if (rt2x00dev->rx_status.channel != conf->channel)
153 flags |= CONFIG_UPDATE_CHANNEL;
154 if (rt2x00dev->tx_power != conf->power_level)
155 flags |= CONFIG_UPDATE_TXPOWER;
Ivo van Doornaddc81bd2007-10-13 16:26:23 +0200156
157 /*
158 * Determining changes in the antenna setups request several checks:
159 * antenna_sel_{r,t}x = 0
160 * -> Does active_{r,t}x match default_{r,t}x
161 * -> Is default_{r,t}x SW_DIVERSITY
162 * antenna_sel_{r,t}x = 1/2
163 * -> Does active_{r,t}x match antenna_sel_{r,t}x
164 * The reason for not updating the antenna while SW diversity
165 * should be used is simple: Software diversity means that
166 * we should switch between the antenna's based on the
167 * quality. This means that the current antenna is good enough
168 * to work with untill the link tuner decides that an antenna
169 * switch should be performed.
170 */
171 if (!conf->antenna_sel_rx &&
172 default_ant->rx != ANTENNA_SW_DIVERSITY &&
173 default_ant->rx != active_ant->rx)
174 flags |= CONFIG_UPDATE_ANTENNA;
175 else if (conf->antenna_sel_rx &&
176 conf->antenna_sel_rx != active_ant->rx)
177 flags |= CONFIG_UPDATE_ANTENNA;
Mattias Nissler16b19512007-10-13 16:26:57 +0200178 else if (active_ant->rx == ANTENNA_SW_DIVERSITY)
179 flags |= CONFIG_UPDATE_ANTENNA;
Ivo van Doornaddc81bd2007-10-13 16:26:23 +0200180
181 if (!conf->antenna_sel_tx &&
182 default_ant->tx != ANTENNA_SW_DIVERSITY &&
183 default_ant->tx != active_ant->tx)
184 flags |= CONFIG_UPDATE_ANTENNA;
185 else if (conf->antenna_sel_tx &&
186 conf->antenna_sel_tx != active_ant->tx)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700187 flags |= CONFIG_UPDATE_ANTENNA;
Mattias Nissler16b19512007-10-13 16:26:57 +0200188 else if (active_ant->tx == ANTENNA_SW_DIVERSITY)
189 flags |= CONFIG_UPDATE_ANTENNA;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700190
191 /*
192 * The following configuration options are never
193 * stored anywhere and will always be updated.
194 */
195 flags |= CONFIG_UPDATE_SLOT_TIME;
196 flags |= CONFIG_UPDATE_BEACON_INT;
197
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200198 /*
199 * We have determined what options should be updated,
200 * now precalculate device configuration values depending
201 * on what configuration options need to be updated.
202 */
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700203config:
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200204 memset(&libconf, 0, sizeof(libconf));
205
206 if (flags & CONFIG_UPDATE_PHYMODE) {
207 switch (conf->phymode) {
208 case MODE_IEEE80211A:
209 libconf.phymode = HWMODE_A;
210 break;
211 case MODE_IEEE80211B:
212 libconf.phymode = HWMODE_B;
213 break;
214 case MODE_IEEE80211G:
215 libconf.phymode = HWMODE_G;
216 break;
217 default:
218 ERROR(rt2x00dev,
219 "Attempt to configure unsupported mode (%d)"
220 "Defaulting to 802.11b", conf->phymode);
221 libconf.phymode = HWMODE_B;
222 }
223
224 mode = &rt2x00dev->hwmodes[libconf.phymode];
225 rate = &mode->rates[mode->num_rates - 1];
226
227 libconf.basic_rates =
228 DEVICE_GET_RATE_FIELD(rate->val, RATEMASK) & DEV_BASIC_RATEMASK;
229 }
230
231 if (flags & CONFIG_UPDATE_CHANNEL) {
232 memcpy(&libconf.rf,
233 &rt2x00dev->spec.channels[conf->channel_val],
234 sizeof(libconf.rf));
235 }
236
Ivo van Doornaddc81bd2007-10-13 16:26:23 +0200237 if (flags & CONFIG_UPDATE_ANTENNA) {
238 if (conf->antenna_sel_rx)
239 libconf.ant.rx = conf->antenna_sel_rx;
240 else if (default_ant->rx != ANTENNA_SW_DIVERSITY)
241 libconf.ant.rx = default_ant->rx;
242 else if (active_ant->rx == ANTENNA_SW_DIVERSITY)
243 libconf.ant.rx = ANTENNA_B;
244
245 if (conf->antenna_sel_tx)
246 libconf.ant.tx = conf->antenna_sel_tx;
247 else if (default_ant->tx != ANTENNA_SW_DIVERSITY)
248 libconf.ant.tx = default_ant->tx;
249 else if (active_ant->tx == ANTENNA_SW_DIVERSITY)
250 libconf.ant.tx = ANTENNA_B;
251 }
252
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200253 if (flags & CONFIG_UPDATE_SLOT_TIME) {
254 short_slot_time = conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME;
255
256 libconf.slot_time =
257 short_slot_time ? SHORT_SLOT_TIME : SLOT_TIME;
258 libconf.sifs = SIFS;
259 libconf.pifs = short_slot_time ? SHORT_PIFS : PIFS;
260 libconf.difs = short_slot_time ? SHORT_DIFS : DIFS;
261 libconf.eifs = EIFS;
262 }
263
264 libconf.conf = conf;
265
266 /*
267 * Start configuration.
268 */
269 rt2x00dev->ops->lib->config(rt2x00dev, flags, &libconf);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700270
271 /*
272 * Some configuration changes affect the link quality
273 * which means we need to reset the link tuner.
274 */
275 if (flags & (CONFIG_UPDATE_CHANNEL | CONFIG_UPDATE_ANTENNA))
276 rt2x00lib_reset_link_tuner(rt2x00dev);
277
Mattias Nissler16b19512007-10-13 16:26:57 +0200278 if (flags & CONFIG_UPDATE_PHYMODE) {
279 rt2x00dev->curr_hwmode = libconf.phymode;
280 rt2x00dev->rx_status.phymode = conf->phymode;
281 }
282
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700283 rt2x00dev->rx_status.freq = conf->freq;
284 rt2x00dev->rx_status.channel = conf->channel;
285 rt2x00dev->tx_power = conf->power_level;
Mattias Nissler16b19512007-10-13 16:26:57 +0200286
287 if (flags & CONFIG_UPDATE_ANTENNA) {
288 rt2x00dev->link.ant.active.rx = libconf.ant.rx;
289 rt2x00dev->link.ant.active.tx = libconf.ant.tx;
290 }
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700291}