blob: 04518b089bdbb1421e111b732203ce6400fe11c2 [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
26/*
27 * Set enviroment defines for rt2x00.h
28 */
29#define DRV_NAME "rt2x00lib"
30
31#include <linux/kernel.h>
32#include <linux/module.h>
33
34#include "rt2x00.h"
35#include "rt2x00lib.h"
36
Ivo van Doorn4abee4b2007-10-06 14:11:46 +020037
38/*
39 * The MAC and BSSID addressess are simple array of bytes,
40 * these arrays are little endian, so when sending the addressess
41 * to the drivers, copy the it into a endian-signed variable.
42 *
43 * Note that all devices (except rt2500usb) have 32 bits
44 * register word sizes. This means that whatever variable we
45 * pass _must_ be a multiple of 32 bits. Otherwise the device
46 * might not accept what we are sending to it.
47 * This will also make it easier for the driver to write
48 * the data to the device.
49 *
50 * Also note that when NULL is passed as address the
51 * we will send 00:00:00:00:00 to the device to clear the address.
52 * This will prevent the device being confused when it wants
53 * to ACK frames or consideres itself associated.
54 */
Ivo van Doorn95ea3622007-09-25 17:57:13 -070055void rt2x00lib_config_mac_addr(struct rt2x00_dev *rt2x00dev, u8 *mac)
56{
Ivo van Doorn4abee4b2007-10-06 14:11:46 +020057 __le32 reg[2];
58
59 memset(&reg, 0, sizeof(reg));
Ivo van Doorn95ea3622007-09-25 17:57:13 -070060 if (mac)
Ivo van Doorn4abee4b2007-10-06 14:11:46 +020061 memcpy(&reg, mac, ETH_ALEN);
62
63 rt2x00dev->ops->lib->config_mac_addr(rt2x00dev, &reg[0]);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070064}
65
66void rt2x00lib_config_bssid(struct rt2x00_dev *rt2x00dev, u8 *bssid)
67{
Ivo van Doorn4abee4b2007-10-06 14:11:46 +020068 __le32 reg[2];
69
70 memset(&reg, 0, sizeof(reg));
Ivo van Doorn95ea3622007-09-25 17:57:13 -070071 if (bssid)
Ivo van Doorn4abee4b2007-10-06 14:11:46 +020072 memcpy(&reg, bssid, ETH_ALEN);
73
74 rt2x00dev->ops->lib->config_bssid(rt2x00dev, &reg[0]);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070075}
76
Ivo van Doornfeb24692007-10-06 14:14:29 +020077void rt2x00lib_config_type(struct rt2x00_dev *rt2x00dev, const int type)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070078{
Ivo van Doornfeb24692007-10-06 14:14:29 +020079 int tsf_sync;
80
81 switch (type) {
82 case IEEE80211_IF_TYPE_IBSS:
83 case IEEE80211_IF_TYPE_AP:
84 tsf_sync = TSF_SYNC_BEACON;
85 break;
86 case IEEE80211_IF_TYPE_STA:
87 tsf_sync = TSF_SYNC_INFRA;
88 break;
89 default:
90 tsf_sync = TSF_SYNC_NONE;
91 break;
92 }
93
94 rt2x00dev->ops->lib->config_type(rt2x00dev, type, tsf_sync);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070095}
96
Ivo van Doorn066cb632007-09-25 20:55:39 +020097void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
98 struct ieee80211_conf *conf, const int force_config)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070099{
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200100 struct rt2x00lib_conf libconf;
101 struct ieee80211_hw_mode *mode;
102 struct ieee80211_rate *rate;
Ivo van Doornaddc81b2007-10-13 16:26:23 +0200103 struct antenna_setup *default_ant = &rt2x00dev->default_ant;
104 struct antenna_setup *active_ant = &rt2x00dev->link.active_ant;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700105 int flags = 0;
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200106 int short_slot_time;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700107
108 /*
Ivo van Doorn066cb632007-09-25 20:55:39 +0200109 * In some situations we want to force all configurations
110 * to be reloaded (When resuming for instance).
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700111 */
Ivo van Doorn066cb632007-09-25 20:55:39 +0200112 if (force_config) {
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700113 flags = CONFIG_UPDATE_ALL;
114 goto config;
115 }
116
117 /*
118 * Check which configuration options have been
119 * updated and should be send to the device.
120 */
121 if (rt2x00dev->rx_status.phymode != conf->phymode)
122 flags |= CONFIG_UPDATE_PHYMODE;
123 if (rt2x00dev->rx_status.channel != conf->channel)
124 flags |= CONFIG_UPDATE_CHANNEL;
125 if (rt2x00dev->tx_power != conf->power_level)
126 flags |= CONFIG_UPDATE_TXPOWER;
Ivo van Doornaddc81b2007-10-13 16:26:23 +0200127
128 /*
129 * Determining changes in the antenna setups request several checks:
130 * antenna_sel_{r,t}x = 0
131 * -> Does active_{r,t}x match default_{r,t}x
132 * -> Is default_{r,t}x SW_DIVERSITY
133 * antenna_sel_{r,t}x = 1/2
134 * -> Does active_{r,t}x match antenna_sel_{r,t}x
135 * The reason for not updating the antenna while SW diversity
136 * should be used is simple: Software diversity means that
137 * we should switch between the antenna's based on the
138 * quality. This means that the current antenna is good enough
139 * to work with untill the link tuner decides that an antenna
140 * switch should be performed.
141 */
142 if (!conf->antenna_sel_rx &&
143 default_ant->rx != ANTENNA_SW_DIVERSITY &&
144 default_ant->rx != active_ant->rx)
145 flags |= CONFIG_UPDATE_ANTENNA;
146 else if (conf->antenna_sel_rx &&
147 conf->antenna_sel_rx != active_ant->rx)
148 flags |= CONFIG_UPDATE_ANTENNA;
149
150 if (!conf->antenna_sel_tx &&
151 default_ant->tx != ANTENNA_SW_DIVERSITY &&
152 default_ant->tx != active_ant->tx)
153 flags |= CONFIG_UPDATE_ANTENNA;
154 else if (conf->antenna_sel_tx &&
155 conf->antenna_sel_tx != active_ant->tx)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700156 flags |= CONFIG_UPDATE_ANTENNA;
157
158 /*
159 * The following configuration options are never
160 * stored anywhere and will always be updated.
161 */
162 flags |= CONFIG_UPDATE_SLOT_TIME;
163 flags |= CONFIG_UPDATE_BEACON_INT;
164
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200165 /*
166 * We have determined what options should be updated,
167 * now precalculate device configuration values depending
168 * on what configuration options need to be updated.
169 */
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700170config:
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200171 memset(&libconf, 0, sizeof(libconf));
172
173 if (flags & CONFIG_UPDATE_PHYMODE) {
174 switch (conf->phymode) {
175 case MODE_IEEE80211A:
176 libconf.phymode = HWMODE_A;
177 break;
178 case MODE_IEEE80211B:
179 libconf.phymode = HWMODE_B;
180 break;
181 case MODE_IEEE80211G:
182 libconf.phymode = HWMODE_G;
183 break;
184 default:
185 ERROR(rt2x00dev,
186 "Attempt to configure unsupported mode (%d)"
187 "Defaulting to 802.11b", conf->phymode);
188 libconf.phymode = HWMODE_B;
189 }
190
191 mode = &rt2x00dev->hwmodes[libconf.phymode];
192 rate = &mode->rates[mode->num_rates - 1];
193
194 libconf.basic_rates =
195 DEVICE_GET_RATE_FIELD(rate->val, RATEMASK) & DEV_BASIC_RATEMASK;
196 }
197
198 if (flags & CONFIG_UPDATE_CHANNEL) {
199 memcpy(&libconf.rf,
200 &rt2x00dev->spec.channels[conf->channel_val],
201 sizeof(libconf.rf));
202 }
203
Ivo van Doornaddc81b2007-10-13 16:26:23 +0200204 if (flags & CONFIG_UPDATE_ANTENNA) {
205 if (conf->antenna_sel_rx)
206 libconf.ant.rx = conf->antenna_sel_rx;
207 else if (default_ant->rx != ANTENNA_SW_DIVERSITY)
208 libconf.ant.rx = default_ant->rx;
209 else if (active_ant->rx == ANTENNA_SW_DIVERSITY)
210 libconf.ant.rx = ANTENNA_B;
211
212 if (conf->antenna_sel_tx)
213 libconf.ant.tx = conf->antenna_sel_tx;
214 else if (default_ant->tx != ANTENNA_SW_DIVERSITY)
215 libconf.ant.tx = default_ant->tx;
216 else if (active_ant->tx == ANTENNA_SW_DIVERSITY)
217 libconf.ant.tx = ANTENNA_B;
218 }
219
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200220 if (flags & CONFIG_UPDATE_SLOT_TIME) {
221 short_slot_time = conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME;
222
223 libconf.slot_time =
224 short_slot_time ? SHORT_SLOT_TIME : SLOT_TIME;
225 libconf.sifs = SIFS;
226 libconf.pifs = short_slot_time ? SHORT_PIFS : PIFS;
227 libconf.difs = short_slot_time ? SHORT_DIFS : DIFS;
228 libconf.eifs = EIFS;
229 }
230
231 libconf.conf = conf;
232
233 /*
234 * Start configuration.
235 */
236 rt2x00dev->ops->lib->config(rt2x00dev, flags, &libconf);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700237
238 /*
239 * Some configuration changes affect the link quality
240 * which means we need to reset the link tuner.
241 */
242 if (flags & (CONFIG_UPDATE_CHANNEL | CONFIG_UPDATE_ANTENNA))
243 rt2x00lib_reset_link_tuner(rt2x00dev);
244
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200245 rt2x00dev->curr_hwmode = libconf.phymode;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700246 rt2x00dev->rx_status.phymode = conf->phymode;
247 rt2x00dev->rx_status.freq = conf->freq;
248 rt2x00dev->rx_status.channel = conf->channel;
249 rt2x00dev->tx_power = conf->power_level;
Ivo van Doornaddc81b2007-10-13 16:26:23 +0200250 rt2x00dev->link.active_ant.rx = libconf.ant.rx;
251 rt2x00dev->link.active_ant.tx = libconf.ant.tx;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700252}