]> git.proxmox.com Git - mirror_ubuntu-disco-kernel.git/blame - drivers/net/wireless/ralink/rt2x00/rt2x00config.c
Merge remote-tracking branches 'asoc/fix/amd', 'asoc/fix/arizona', 'asoc/fix/dpcm...
[mirror_ubuntu-disco-kernel.git] / drivers / net / wireless / ralink / rt2x00 / rt2x00config.c
CommitLineData
95ea3627 1/*
9c9a0d14 2 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
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
a05b8c58 16 along with this program; if not, see <http://www.gnu.org/licenses/>.
95ea3627
ID
17 */
18
19/*
20 Module: rt2x00lib
21 Abstract: rt2x00 generic configuration routines.
22 */
23
95ea3627
ID
24#include <linux/kernel.h>
25#include <linux/module.h>
26
27#include "rt2x00.h"
28#include "rt2x00lib.h"
29
6bb40dd1
ID
30void rt2x00lib_config_intf(struct rt2x00_dev *rt2x00dev,
31 struct rt2x00_intf *intf,
05c914fe 32 enum nl80211_iftype type,
5f936f11 33 const u8 *mac, const u8 *bssid)
95ea3627 34{
6bb40dd1
ID
35 struct rt2x00intf_conf conf;
36 unsigned int flags = 0;
95ea3627 37
6bb40dd1 38 conf.type = type;
feb24691
ID
39
40 switch (type) {
05c914fe 41 case NL80211_IFTYPE_ADHOC:
ab8966dd
HS
42 conf.sync = TSF_SYNC_ADHOC;
43 break;
05c914fe 44 case NL80211_IFTYPE_AP:
a07dbea2 45 case NL80211_IFTYPE_MESH_POINT:
ce292a64 46 case NL80211_IFTYPE_WDS:
ab8966dd 47 conf.sync = TSF_SYNC_AP_NONE;
feb24691 48 break;
05c914fe 49 case NL80211_IFTYPE_STATION:
6bb40dd1 50 conf.sync = TSF_SYNC_INFRA;
feb24691
ID
51 break;
52 default:
6bb40dd1 53 conf.sync = TSF_SYNC_NONE;
feb24691
ID
54 break;
55 }
56
6bb40dd1
ID
57 /*
58 * Note that when NULL is passed as address we will send
59 * 00:00:00:00:00 to the device to clear the address.
60 * This will prevent the device being confused when it wants
25985edc 61 * to ACK frames or considers itself associated.
6bb40dd1 62 */
736e3aca 63 memset(conf.mac, 0, sizeof(conf.mac));
6bb40dd1 64 if (mac)
736e3aca 65 memcpy(conf.mac, mac, ETH_ALEN);
6bb40dd1 66
736e3aca 67 memset(conf.bssid, 0, sizeof(conf.bssid));
6bb40dd1 68 if (bssid)
736e3aca 69 memcpy(conf.bssid, bssid, ETH_ALEN);
6bb40dd1
ID
70
71 flags |= CONFIG_UPDATE_TYPE;
72 if (mac || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
73 flags |= CONFIG_UPDATE_MAC;
74 if (bssid || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
75 flags |= CONFIG_UPDATE_BSSID;
76
77 rt2x00dev->ops->lib->config_intf(rt2x00dev, intf, &conf, flags);
78}
79
72810379
ID
80void rt2x00lib_config_erp(struct rt2x00_dev *rt2x00dev,
81 struct rt2x00_intf *intf,
02044643
HS
82 struct ieee80211_bss_conf *bss_conf,
83 u32 changed)
6bb40dd1 84{
72810379 85 struct rt2x00lib_erp erp;
6bb40dd1 86
72810379
ID
87 memset(&erp, 0, sizeof(erp));
88
89 erp.short_preamble = bss_conf->use_short_preamble;
e360c4cb
ID
90 erp.cts_protection = bss_conf->use_cts_prot;
91
e4ea1c40
ID
92 erp.slot_time = bss_conf->use_short_slot ? SHORT_SLOT_TIME : SLOT_TIME;
93 erp.sifs = SIFS;
94 erp.pifs = bss_conf->use_short_slot ? SHORT_PIFS : PIFS;
95 erp.difs = bss_conf->use_short_slot ? SHORT_DIFS : DIFS;
96 erp.eifs = bss_conf->use_short_slot ? SHORT_EIFS : EIFS;
6bb40dd1 97
e4ea1c40 98 erp.basic_rates = bss_conf->basic_rates;
8a566afe 99 erp.beacon_int = bss_conf->beacon_int;
e4ea1c40 100
1c0bcf89
ID
101 /* Update the AID, this is needed for dynamic PS support */
102 rt2x00dev->aid = bss_conf->assoc ? bss_conf->aid : 0;
8c358bcd 103 rt2x00dev->last_beacon = bss_conf->sync_tsf;
1c0bcf89 104
6b347bff
ID
105 /* Update global beacon interval time, this is needed for PS support */
106 rt2x00dev->beacon_int = bss_conf->beacon_int;
107
87c1915d
HS
108 if (changed & BSS_CHANGED_HT)
109 erp.ht_opmode = bss_conf->ht_operation_mode;
110
02044643 111 rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp, changed);
95ea3627
ID
112}
113
69f81a2c 114void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
bdfa500b 115 struct antenna_setup config)
69f81a2c 116{
bdfa500b 117 struct link_ant *ant = &rt2x00dev->link.ant;
0b927a07
ID
118 struct antenna_setup *def = &rt2x00dev->default_ant;
119 struct antenna_setup *active = &rt2x00dev->link.ant.active;
120
6d64360a 121 /*
0ed7b3c0
ID
122 * When the caller tries to send the SW diversity,
123 * we must update the ANTENNA_RX_DIVERSITY flag to
124 * enable the antenna diversity in the link tuner.
125 *
126 * Secondly, we must guarentee we never send the
127 * software antenna diversity command to the driver.
6d64360a 128 */
0ed7b3c0
ID
129 if (!(ant->flags & ANTENNA_RX_DIVERSITY)) {
130 if (config.rx == ANTENNA_SW_DIVERSITY) {
131 ant->flags |= ANTENNA_RX_DIVERSITY;
132
133 if (def->rx == ANTENNA_SW_DIVERSITY)
134 config.rx = ANTENNA_B;
135 else
136 config.rx = def->rx;
137 }
138 } else if (config.rx == ANTENNA_SW_DIVERSITY)
bdfa500b 139 config.rx = active->rx;
69f81a2c 140
0ed7b3c0
ID
141 if (!(ant->flags & ANTENNA_TX_DIVERSITY)) {
142 if (config.tx == ANTENNA_SW_DIVERSITY) {
143 ant->flags |= ANTENNA_TX_DIVERSITY;
144
145 if (def->tx == ANTENNA_SW_DIVERSITY)
146 config.tx = ANTENNA_B;
147 else
148 config.tx = def->tx;
149 }
150 } else if (config.tx == ANTENNA_SW_DIVERSITY)
bdfa500b
ID
151 config.tx = active->tx;
152
e25c4bb9
ID
153 /*
154 * Antenna setup changes require the RX to be disabled,
155 * else the changes will be ignored by the device.
156 */
0262ab0d 157 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
0b7fde54 158 rt2x00queue_stop_queue(rt2x00dev->rx);
e25c4bb9 159
69f81a2c
ID
160 /*
161 * Write new antenna setup to device and reset the link tuner.
162 * The latter is required since we need to recalibrate the
163 * noise-sensitivity ratio for the new setup.
164 */
bdfa500b 165 rt2x00dev->ops->lib->config_ant(rt2x00dev, &config);
e4ea1c40 166
84e3196f 167 rt2x00link_reset_tuner(rt2x00dev, true);
69f81a2c 168
bdfa500b 169 memcpy(active, &config, sizeof(config));
e25c4bb9 170
0262ab0d 171 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
0b7fde54 172 rt2x00queue_start_queue(rt2x00dev->rx);
69f81a2c
ID
173}
174
46a01ec0
GW
175static u16 rt2x00ht_center_channel(struct rt2x00_dev *rt2x00dev,
176 struct ieee80211_conf *conf)
177{
178 struct hw_mode_spec *spec = &rt2x00dev->spec;
179 int center_channel;
180 u16 i;
181
182 /*
183 * Initialize center channel to current channel.
184 */
675a0b04 185 center_channel = spec->channels[conf->chandef.chan->hw_value].channel;
46a01ec0
GW
186
187 /*
188 * Adjust center channel to HT40+ and HT40- operation.
189 */
190 if (conf_is_ht40_plus(conf))
191 center_channel += 2;
192 else if (conf_is_ht40_minus(conf))
193 center_channel -= (center_channel == 14) ? 1 : 2;
194
195 for (i = 0; i < spec->num_channels; i++)
196 if (spec->channels[i].channel == center_channel)
197 return i;
198
199 WARN_ON(1);
675a0b04 200 return conf->chandef.chan->hw_value;
46a01ec0
GW
201}
202
066cb637 203void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
e4ea1c40
ID
204 struct ieee80211_conf *conf,
205 unsigned int ieee80211_flags)
95ea3627 206{
5c58ee51 207 struct rt2x00lib_conf libconf;
06443e46 208 u16 hw_value;
1c0bcf89
ID
209 u16 autowake_timeout;
210 u16 beacon_int;
211 u16 beacon_diff;
95ea3627 212
5c58ee51
ID
213 memset(&libconf, 0, sizeof(libconf));
214
e4ea1c40 215 libconf.conf = conf;
5c58ee51 216
e4ea1c40 217 if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL) {
da40f407
SG
218 if (!conf_is_ht(conf))
219 set_bit(CONFIG_HT_DISABLED, &rt2x00dev->flags);
220 else
221 clear_bit(CONFIG_HT_DISABLED, &rt2x00dev->flags);
222
06443e46 223 if (conf_is_ht40(conf)) {
7dab73b3 224 set_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
06443e46
GW
225 hw_value = rt2x00ht_center_channel(rt2x00dev, conf);
226 } else {
7dab73b3 227 clear_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
675a0b04 228 hw_value = conf->chandef.chan->hw_value;
06443e46 229 }
35f00cfc 230
5c58ee51 231 memcpy(&libconf.rf,
06443e46 232 &rt2x00dev->spec.channels[hw_value],
5c58ee51 233 sizeof(libconf.rf));
8c5e7a5f
ID
234
235 memcpy(&libconf.channel,
06443e46 236 &rt2x00dev->spec.channels_info[hw_value],
8c5e7a5f 237 sizeof(libconf.channel));
2e9c43dd
JL
238
239 /* Used for VCO periodic calibration */
240 rt2x00dev->rf_channel = libconf.rf.channel;
5c58ee51
ID
241 }
242
b9d305cc 243 if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_PS_AUTOWAKE) &&
1c0bcf89
ID
244 (ieee80211_flags & IEEE80211_CONF_CHANGE_PS))
245 cancel_delayed_work_sync(&rt2x00dev->autowakeup_work);
246
5c58ee51
ID
247 /*
248 * Start configuration.
249 */
e4ea1c40 250 rt2x00dev->ops->lib->config(rt2x00dev, &libconf, ieee80211_flags);
95ea3627
ID
251
252 /*
253 * Some configuration changes affect the link quality
254 * which means we need to reset the link tuner.
255 */
e4ea1c40 256 if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL)
84e3196f 257 rt2x00link_reset_tuner(rt2x00dev, false);
95ea3627 258
3bb42a64 259 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
b9d305cc 260 rt2x00_has_cap_flag(rt2x00dev, REQUIRE_PS_AUTOWAKE) &&
1c0bcf89
ID
261 (ieee80211_flags & IEEE80211_CONF_CHANGE_PS) &&
262 (conf->flags & IEEE80211_CONF_PS)) {
263 beacon_diff = (long)jiffies - (long)rt2x00dev->last_beacon;
264 beacon_int = msecs_to_jiffies(rt2x00dev->beacon_int);
265
266 if (beacon_diff > beacon_int)
267 beacon_diff = 0;
268
4a733ef1 269 autowake_timeout = (conf->ps_dtim_period * beacon_int) - beacon_diff;
1c0bcf89
ID
270 queue_delayed_work(rt2x00dev->workqueue,
271 &rt2x00dev->autowakeup_work,
272 autowake_timeout - 15);
273 }
274
275 if (conf->flags & IEEE80211_CONF_PS)
276 set_bit(CONFIG_POWERSAVING, &rt2x00dev->flags);
277 else
278 clear_bit(CONFIG_POWERSAVING, &rt2x00dev->flags);
279
262c741e
EC
280 if (conf->flags & IEEE80211_CONF_MONITOR)
281 set_bit(CONFIG_MONITORING, &rt2x00dev->flags);
282 else
283 clear_bit(CONFIG_MONITORING, &rt2x00dev->flags);
284
675a0b04
KB
285 rt2x00dev->curr_band = conf->chandef.chan->band;
286 rt2x00dev->curr_freq = conf->chandef.chan->center_freq;
95ea3627 287 rt2x00dev->tx_power = conf->power_level;
42c82857
ID
288 rt2x00dev->short_retry = conf->short_frame_max_tx_count;
289 rt2x00dev->long_retry = conf->long_frame_max_tx_count;
95ea3627 290}