rt2x00: Implement HW encryption (rt73usb)
[deliverable/linux.git] / drivers / net / wireless / rt2x00 / rt2x00config.c
CommitLineData
95ea3627 1/*
811aa9ca 2 Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
95ea3627
ID
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
95ea3627
ID
26#include <linux/kernel.h>
27#include <linux/module.h>
28
29#include "rt2x00.h"
30#include "rt2x00lib.h"
31
6bb40dd1
ID
32void rt2x00lib_config_intf(struct rt2x00_dev *rt2x00dev,
33 struct rt2x00_intf *intf,
34 enum ieee80211_if_types type,
35 u8 *mac, u8 *bssid)
95ea3627 36{
6bb40dd1
ID
37 struct rt2x00intf_conf conf;
38 unsigned int flags = 0;
95ea3627 39
6bb40dd1 40 conf.type = type;
feb24691
ID
41
42 switch (type) {
43 case IEEE80211_IF_TYPE_IBSS:
44 case IEEE80211_IF_TYPE_AP:
6bb40dd1 45 conf.sync = TSF_SYNC_BEACON;
feb24691
ID
46 break;
47 case IEEE80211_IF_TYPE_STA:
6bb40dd1 48 conf.sync = TSF_SYNC_INFRA;
feb24691
ID
49 break;
50 default:
6bb40dd1 51 conf.sync = TSF_SYNC_NONE;
feb24691
ID
52 break;
53 }
54
6bb40dd1
ID
55 /*
56 * Note that when NULL is passed as address we will send
57 * 00:00:00:00:00 to the device to clear the address.
58 * This will prevent the device being confused when it wants
59 * to ACK frames or consideres itself associated.
60 */
61 memset(&conf.mac, 0, sizeof(conf.mac));
62 if (mac)
63 memcpy(&conf.mac, mac, ETH_ALEN);
64
65 memset(&conf.bssid, 0, sizeof(conf.bssid));
66 if (bssid)
67 memcpy(&conf.bssid, bssid, ETH_ALEN);
68
69 flags |= CONFIG_UPDATE_TYPE;
70 if (mac || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
71 flags |= CONFIG_UPDATE_MAC;
72 if (bssid || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
73 flags |= CONFIG_UPDATE_BSSID;
74
75 rt2x00dev->ops->lib->config_intf(rt2x00dev, intf, &conf, flags);
76}
77
72810379
ID
78void rt2x00lib_config_erp(struct rt2x00_dev *rt2x00dev,
79 struct rt2x00_intf *intf,
80 struct ieee80211_bss_conf *bss_conf)
6bb40dd1 81{
72810379 82 struct rt2x00lib_erp erp;
6bb40dd1 83
72810379
ID
84 memset(&erp, 0, sizeof(erp));
85
86 erp.short_preamble = bss_conf->use_short_preamble;
e360c4cb
ID
87 erp.cts_protection = bss_conf->use_cts_prot;
88
72810379
ID
89 erp.ack_timeout = PLCP + get_duration(ACK_SIZE, 10);
90 erp.ack_consume_time = SIFS + PLCP + get_duration(ACK_SIZE, 10);
6bb40dd1
ID
91
92 if (rt2x00dev->hw->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME)
72810379 93 erp.ack_timeout += SHORT_DIFS;
6bb40dd1 94 else
72810379 95 erp.ack_timeout += DIFS;
6bb40dd1 96
72810379
ID
97 if (bss_conf->use_short_preamble) {
98 erp.ack_timeout += SHORT_PREAMBLE;
99 erp.ack_consume_time += SHORT_PREAMBLE;
6bb40dd1 100 } else {
72810379
ID
101 erp.ack_timeout += PREAMBLE;
102 erp.ack_consume_time += PREAMBLE;
6bb40dd1
ID
103 }
104
3a643d24 105 rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp);
95ea3627
ID
106}
107
69f81a2c
ID
108void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
109 enum antenna rx, enum antenna tx)
110{
111 struct rt2x00lib_conf libconf;
112
113 libconf.ant.rx = rx;
114 libconf.ant.tx = tx;
115
05253c93
ID
116 if (rx == rt2x00dev->link.ant.active.rx &&
117 tx == rt2x00dev->link.ant.active.tx)
118 return;
119
e25c4bb9
ID
120 /*
121 * Antenna setup changes require the RX to be disabled,
122 * else the changes will be ignored by the device.
123 */
124 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
61667d8d 125 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF_LINK);
e25c4bb9 126
69f81a2c
ID
127 /*
128 * Write new antenna setup to device and reset the link tuner.
129 * The latter is required since we need to recalibrate the
130 * noise-sensitivity ratio for the new setup.
131 */
6bb40dd1 132 rt2x00dev->ops->lib->config(rt2x00dev, &libconf, CONFIG_UPDATE_ANTENNA);
69f81a2c 133 rt2x00lib_reset_link_tuner(rt2x00dev);
f06a0f48 134 rt2x00_reset_link_ant_rssi(&rt2x00dev->link);
69f81a2c
ID
135
136 rt2x00dev->link.ant.active.rx = libconf.ant.rx;
137 rt2x00dev->link.ant.active.tx = libconf.ant.tx;
e25c4bb9
ID
138
139 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
61667d8d 140 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON_LINK);
69f81a2c
ID
141}
142
aa776721
ID
143static u32 rt2x00lib_get_basic_rates(struct ieee80211_supported_band *band)
144{
145 const struct rt2x00_rate *rate;
146 unsigned int i;
147 u32 mask = 0;
148
149 for (i = 0; i < band->n_bitrates; i++) {
150 rate = rt2x00_get_rate(band->bitrates[i].hw_value);
151 if (rate->flags & DEV_RATE_BASIC)
152 mask |= rate->ratemask;
153 }
154
155 return mask;
156}
157
066cb637
ID
158void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
159 struct ieee80211_conf *conf, const int force_config)
95ea3627 160{
5c58ee51 161 struct rt2x00lib_conf libconf;
8318d78a 162 struct ieee80211_supported_band *band;
addc81bd 163 struct antenna_setup *default_ant = &rt2x00dev->default_ant;
69f81a2c 164 struct antenna_setup *active_ant = &rt2x00dev->link.ant.active;
95ea3627 165 int flags = 0;
5c58ee51 166 int short_slot_time;
95ea3627
ID
167
168 /*
066cb637
ID
169 * In some situations we want to force all configurations
170 * to be reloaded (When resuming for instance).
95ea3627 171 */
066cb637 172 if (force_config) {
95ea3627
ID
173 flags = CONFIG_UPDATE_ALL;
174 goto config;
175 }
176
177 /*
178 * Check which configuration options have been
179 * updated and should be send to the device.
180 */
8318d78a 181 if (rt2x00dev->rx_status.band != conf->channel->band)
95ea3627 182 flags |= CONFIG_UPDATE_PHYMODE;
8318d78a 183 if (rt2x00dev->rx_status.freq != conf->channel->center_freq)
95ea3627
ID
184 flags |= CONFIG_UPDATE_CHANNEL;
185 if (rt2x00dev->tx_power != conf->power_level)
186 flags |= CONFIG_UPDATE_TXPOWER;
addc81bd
ID
187
188 /*
189 * Determining changes in the antenna setups request several checks:
190 * antenna_sel_{r,t}x = 0
191 * -> Does active_{r,t}x match default_{r,t}x
192 * -> Is default_{r,t}x SW_DIVERSITY
193 * antenna_sel_{r,t}x = 1/2
194 * -> Does active_{r,t}x match antenna_sel_{r,t}x
195 * The reason for not updating the antenna while SW diversity
196 * should be used is simple: Software diversity means that
197 * we should switch between the antenna's based on the
198 * quality. This means that the current antenna is good enough
199 * to work with untill the link tuner decides that an antenna
200 * switch should be performed.
201 */
202 if (!conf->antenna_sel_rx &&
203 default_ant->rx != ANTENNA_SW_DIVERSITY &&
204 default_ant->rx != active_ant->rx)
205 flags |= CONFIG_UPDATE_ANTENNA;
206 else if (conf->antenna_sel_rx &&
207 conf->antenna_sel_rx != active_ant->rx)
208 flags |= CONFIG_UPDATE_ANTENNA;
16b1951f
MN
209 else if (active_ant->rx == ANTENNA_SW_DIVERSITY)
210 flags |= CONFIG_UPDATE_ANTENNA;
addc81bd
ID
211
212 if (!conf->antenna_sel_tx &&
213 default_ant->tx != ANTENNA_SW_DIVERSITY &&
214 default_ant->tx != active_ant->tx)
215 flags |= CONFIG_UPDATE_ANTENNA;
216 else if (conf->antenna_sel_tx &&
217 conf->antenna_sel_tx != active_ant->tx)
95ea3627 218 flags |= CONFIG_UPDATE_ANTENNA;
16b1951f
MN
219 else if (active_ant->tx == ANTENNA_SW_DIVERSITY)
220 flags |= CONFIG_UPDATE_ANTENNA;
95ea3627
ID
221
222 /*
223 * The following configuration options are never
224 * stored anywhere and will always be updated.
225 */
226 flags |= CONFIG_UPDATE_SLOT_TIME;
227 flags |= CONFIG_UPDATE_BEACON_INT;
228
5c58ee51
ID
229 /*
230 * We have determined what options should be updated,
231 * now precalculate device configuration values depending
232 * on what configuration options need to be updated.
233 */
95ea3627 234config:
5c58ee51
ID
235 memset(&libconf, 0, sizeof(libconf));
236
237 if (flags & CONFIG_UPDATE_PHYMODE) {
8318d78a 238 band = &rt2x00dev->bands[conf->channel->band];
f5507ce9
ID
239
240 libconf.band = conf->channel->band;
aa776721 241 libconf.basic_rates = rt2x00lib_get_basic_rates(band);
5c58ee51
ID
242 }
243
244 if (flags & CONFIG_UPDATE_CHANNEL) {
245 memcpy(&libconf.rf,
8318d78a 246 &rt2x00dev->spec.channels[conf->channel->hw_value],
5c58ee51
ID
247 sizeof(libconf.rf));
248 }
249
addc81bd
ID
250 if (flags & CONFIG_UPDATE_ANTENNA) {
251 if (conf->antenna_sel_rx)
252 libconf.ant.rx = conf->antenna_sel_rx;
253 else if (default_ant->rx != ANTENNA_SW_DIVERSITY)
254 libconf.ant.rx = default_ant->rx;
255 else if (active_ant->rx == ANTENNA_SW_DIVERSITY)
256 libconf.ant.rx = ANTENNA_B;
ada662f3
ID
257 else
258 libconf.ant.rx = active_ant->rx;
addc81bd
ID
259
260 if (conf->antenna_sel_tx)
261 libconf.ant.tx = conf->antenna_sel_tx;
262 else if (default_ant->tx != ANTENNA_SW_DIVERSITY)
263 libconf.ant.tx = default_ant->tx;
264 else if (active_ant->tx == ANTENNA_SW_DIVERSITY)
265 libconf.ant.tx = ANTENNA_B;
ada662f3
ID
266 else
267 libconf.ant.tx = active_ant->tx;
addc81bd
ID
268 }
269
5c58ee51
ID
270 if (flags & CONFIG_UPDATE_SLOT_TIME) {
271 short_slot_time = conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME;
272
273 libconf.slot_time =
274 short_slot_time ? SHORT_SLOT_TIME : SLOT_TIME;
275 libconf.sifs = SIFS;
276 libconf.pifs = short_slot_time ? SHORT_PIFS : PIFS;
277 libconf.difs = short_slot_time ? SHORT_DIFS : DIFS;
f2fdbc48 278 libconf.eifs = short_slot_time ? SHORT_EIFS : EIFS;
5c58ee51
ID
279 }
280
281 libconf.conf = conf;
282
283 /*
284 * Start configuration.
285 */
6bb40dd1 286 rt2x00dev->ops->lib->config(rt2x00dev, &libconf, flags);
95ea3627
ID
287
288 /*
289 * Some configuration changes affect the link quality
290 * which means we need to reset the link tuner.
291 */
292 if (flags & (CONFIG_UPDATE_CHANNEL | CONFIG_UPDATE_ANTENNA))
293 rt2x00lib_reset_link_tuner(rt2x00dev);
294
16b1951f 295 if (flags & CONFIG_UPDATE_PHYMODE) {
8318d78a
JB
296 rt2x00dev->curr_band = conf->channel->band;
297 rt2x00dev->rx_status.band = conf->channel->band;
16b1951f
MN
298 }
299
8318d78a 300 rt2x00dev->rx_status.freq = conf->channel->center_freq;
95ea3627 301 rt2x00dev->tx_power = conf->power_level;
16b1951f
MN
302
303 if (flags & CONFIG_UPDATE_ANTENNA) {
304 rt2x00dev->link.ant.active.rx = libconf.ant.rx;
305 rt2x00dev->link.ant.active.tx = libconf.ant.tx;
306 }
95ea3627 307}
This page took 0.262023 seconds and 5 git commands to generate.