blob: 1122dc44c9fd3f0c9fb0b0906936020204c263f1 [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01002 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
Ivo van Doorn95ea3622007-09-25 17:57:13 -07003 <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
Jeff Kirshera05b8c52013-12-06 03:32:11 -080016 along with this program; if not, see <http://www.gnu.org/licenses/>.
Ivo van Doorn95ea3622007-09-25 17:57:13 -070017 */
18
19/*
20 Module: rt2x00lib
21 Abstract: rt2x00 generic configuration routines.
22 */
23
Ivo van Doorn95ea3622007-09-25 17:57:13 -070024#include <linux/kernel.h>
25#include <linux/module.h>
26
27#include "rt2x00.h"
28#include "rt2x00lib.h"
29
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010030void rt2x00lib_config_intf(struct rt2x00_dev *rt2x00dev,
31 struct rt2x00_intf *intf,
Johannes Berg05c914f2008-09-11 00:01:58 +020032 enum nl80211_iftype type,
Johannes Berg5f936f12009-01-21 12:47:05 +010033 const u8 *mac, const u8 *bssid)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070034{
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010035 struct rt2x00intf_conf conf;
36 unsigned int flags = 0;
Ivo van Doorn4abee4b2007-10-06 14:11:46 +020037
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010038 conf.type = type;
Ivo van Doornfeb24692007-10-06 14:14:29 +020039
40 switch (type) {
Johannes Berg05c914f2008-09-11 00:01:58 +020041 case NL80211_IFTYPE_ADHOC:
Helmut Schaaab8966d2010-07-11 12:30:13 +020042 conf.sync = TSF_SYNC_ADHOC;
43 break;
Johannes Berg05c914f2008-09-11 00:01:58 +020044 case NL80211_IFTYPE_AP:
Andrey Yurovskya07dbea2008-12-20 10:55:34 +010045 case NL80211_IFTYPE_MESH_POINT:
Ivo van Doornce292a62008-12-20 10:57:02 +010046 case NL80211_IFTYPE_WDS:
Helmut Schaaab8966d2010-07-11 12:30:13 +020047 conf.sync = TSF_SYNC_AP_NONE;
Ivo van Doornfeb24692007-10-06 14:14:29 +020048 break;
Johannes Berg05c914f2008-09-11 00:01:58 +020049 case NL80211_IFTYPE_STATION:
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010050 conf.sync = TSF_SYNC_INFRA;
Ivo van Doornfeb24692007-10-06 14:14:29 +020051 break;
52 default:
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010053 conf.sync = TSF_SYNC_NONE;
Ivo van Doornfeb24692007-10-06 14:14:29 +020054 break;
55 }
56
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010057 /*
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
Lucas De Marchi25985ed2011-03-30 22:57:33 -030061 * to ACK frames or considers itself associated.
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010062 */
Gertjan van Wingerde736e3ac2010-12-27 15:05:55 +010063 memset(conf.mac, 0, sizeof(conf.mac));
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010064 if (mac)
Gertjan van Wingerde736e3ac2010-12-27 15:05:55 +010065 memcpy(conf.mac, mac, ETH_ALEN);
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010066
Gertjan van Wingerde736e3ac2010-12-27 15:05:55 +010067 memset(conf.bssid, 0, sizeof(conf.bssid));
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010068 if (bssid)
Gertjan van Wingerde736e3ac2010-12-27 15:05:55 +010069 memcpy(conf.bssid, bssid, ETH_ALEN);
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010070
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
Ivo van Doorn72810372008-03-09 22:46:18 +010080void rt2x00lib_config_erp(struct rt2x00_dev *rt2x00dev,
81 struct rt2x00_intf *intf,
Helmut Schaa02044642010-09-08 20:56:32 +020082 struct ieee80211_bss_conf *bss_conf,
83 u32 changed)
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010084{
Ivo van Doorn72810372008-03-09 22:46:18 +010085 struct rt2x00lib_erp erp;
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010086
Ivo van Doorn72810372008-03-09 22:46:18 +010087 memset(&erp, 0, sizeof(erp));
88
89 erp.short_preamble = bss_conf->use_short_preamble;
Ivo van Doorne360c4c2008-07-09 15:12:06 +020090 erp.cts_protection = bss_conf->use_cts_prot;
91
Ivo van Doorne4ea1c42008-10-29 17:17:57 +010092 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;
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010097
Ivo van Doorne4ea1c42008-10-29 17:17:57 +010098 erp.basic_rates = bss_conf->basic_rates;
Ivo van Doorn8a566af2009-05-21 19:16:46 +020099 erp.beacon_int = bss_conf->beacon_int;
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100100
Ivo van Doorn1c0bcf82011-04-30 17:18:18 +0200101 /* Update the AID, this is needed for dynamic PS support */
102 rt2x00dev->aid = bss_conf->assoc ? bss_conf->aid : 0;
Johannes Berg8c358bc2012-05-22 22:13:05 +0200103 rt2x00dev->last_beacon = bss_conf->sync_tsf;
Ivo van Doorn1c0bcf82011-04-30 17:18:18 +0200104
Ivo van Doorn6b347bf2009-05-23 21:09:28 +0200105 /* Update global beacon interval time, this is needed for PS support */
106 rt2x00dev->beacon_int = bss_conf->beacon_int;
107
Helmut Schaa87c19152010-10-02 11:28:34 +0200108 if (changed & BSS_CHANGED_HT)
109 erp.ht_opmode = bss_conf->ht_operation_mode;
110
Helmut Schaa02044642010-09-08 20:56:32 +0200111 rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp, changed);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700112}
113
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200114void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200115 struct antenna_setup config)
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200116{
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200117 struct link_ant *ant = &rt2x00dev->link.ant;
Ivo van Doorn0b927a02008-12-02 18:20:22 +0100118 struct antenna_setup *def = &rt2x00dev->default_ant;
119 struct antenna_setup *active = &rt2x00dev->link.ant.active;
120
Ivo van Doorn6d643602008-11-02 00:38:10 +0100121 /*
Ivo van Doorn0ed7b3c2011-04-18 15:35:12 +0200122 * 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.
Ivo van Doorn6d643602008-11-02 00:38:10 +0100128 */
Ivo van Doorn0ed7b3c2011-04-18 15:35:12 +0200129 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)
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200139 config.rx = active->rx;
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200140
Ivo van Doorn0ed7b3c2011-04-18 15:35:12 +0200141 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)
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200151 config.tx = active->tx;
152
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200153 /*
Ivo van Doorne25c4bb2007-10-27 13:39:28 +0200154 * Antenna setup changes require the RX to be disabled,
155 * else the changes will be ignored by the device.
156 */
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200157 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100158 rt2x00queue_stop_queue(rt2x00dev->rx);
Ivo van Doorne25c4bb2007-10-27 13:39:28 +0200159
160 /*
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200161 * 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 */
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200165 rt2x00dev->ops->lib->config_ant(rt2x00dev, &config);
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100166
Ivo van Doorn84e31962008-12-20 10:53:29 +0100167 rt2x00link_reset_tuner(rt2x00dev, true);
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200168
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200169 memcpy(active, &config, sizeof(config));
Ivo van Doorne25c4bb2007-10-27 13:39:28 +0200170
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200171 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100172 rt2x00queue_start_queue(rt2x00dev->rx);
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200173}
174
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200175static 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 */
Karl Beldan675a0b02013-03-25 16:26:57 +0100185 center_channel = spec->channels[conf->chandef.chan->hw_value].channel;
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200186
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);
Karl Beldan675a0b02013-03-25 16:26:57 +0100200 return conf->chandef.chan->hw_value;
Gertjan van Wingerde46a01ec2011-04-18 15:33:41 +0200201}
202
Ivo van Doorn066cb632007-09-25 20:55:39 +0200203void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100204 struct ieee80211_conf *conf,
205 unsigned int ieee80211_flags)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700206{
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200207 struct rt2x00lib_conf libconf;
Gertjan van Wingerde06443e42010-06-03 10:52:08 +0200208 u16 hw_value;
Ivo van Doorn1c0bcf82011-04-30 17:18:18 +0200209 u16 autowake_timeout;
210 u16 beacon_int;
211 u16 beacon_diff;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700212
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200213 memset(&libconf, 0, sizeof(libconf));
214
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100215 libconf.conf = conf;
Ivo van Doornf5507ce2008-02-03 15:51:13 +0100216
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100217 if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL) {
Stanislaw Gruszkada40f402012-04-04 16:15:33 +0200218 if (!conf_is_ht(conf))
219 set_bit(CONFIG_HT_DISABLED, &rt2x00dev->flags);
220 else
221 clear_bit(CONFIG_HT_DISABLED, &rt2x00dev->flags);
222
Gertjan van Wingerde06443e42010-06-03 10:52:08 +0200223 if (conf_is_ht40(conf)) {
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200224 set_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
Gertjan van Wingerde06443e42010-06-03 10:52:08 +0200225 hw_value = rt2x00ht_center_channel(rt2x00dev, conf);
226 } else {
Ivo van Doorn7dab73b2011-04-18 15:27:06 +0200227 clear_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
Karl Beldan675a0b02013-03-25 16:26:57 +0100228 hw_value = conf->chandef.chan->hw_value;
Gertjan van Wingerde06443e42010-06-03 10:52:08 +0200229 }
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200230
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200231 memcpy(&libconf.rf,
Gertjan van Wingerde06443e42010-06-03 10:52:08 +0200232 &rt2x00dev->spec.channels[hw_value],
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200233 sizeof(libconf.rf));
Ivo van Doorn8c5e7a52008-08-04 16:38:47 +0200234
235 memcpy(&libconf.channel,
Gertjan van Wingerde06443e42010-06-03 10:52:08 +0200236 &rt2x00dev->spec.channels_info[hw_value],
Ivo van Doorn8c5e7a52008-08-04 16:38:47 +0200237 sizeof(libconf.channel));
John Li2e9c43d2012-02-16 21:40:57 +0800238
239 /* Used for VCO periodic calibration */
240 rt2x00dev->rf_channel = libconf.rf.channel;
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200241 }
242
Ivo van Doorn1c0bcf82011-04-30 17:18:18 +0200243 if (test_bit(REQUIRE_PS_AUTOWAKE, &rt2x00dev->cap_flags) &&
244 (ieee80211_flags & IEEE80211_CONF_CHANGE_PS))
245 cancel_delayed_work_sync(&rt2x00dev->autowakeup_work);
246
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200247 /*
248 * Start configuration.
249 */
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100250 rt2x00dev->ops->lib->config(rt2x00dev, &libconf, ieee80211_flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700251
252 /*
253 * Some configuration changes affect the link quality
254 * which means we need to reset the link tuner.
255 */
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100256 if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL)
Ivo van Doorn84e31962008-12-20 10:53:29 +0100257 rt2x00link_reset_tuner(rt2x00dev, false);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700258
Stanislaw Gruszka3bb42a62011-06-04 16:48:54 +0200259 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
260 test_bit(REQUIRE_PS_AUTOWAKE, &rt2x00dev->cap_flags) &&
Ivo van Doorn1c0bcf82011-04-30 17:18:18 +0200261 (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
269 autowake_timeout = (conf->max_sleep_period * beacon_int) - beacon_diff;
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
Karl Beldan675a0b02013-03-25 16:26:57 +0100280 rt2x00dev->curr_band = conf->chandef.chan->band;
281 rt2x00dev->curr_freq = conf->chandef.chan->center_freq;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700282 rt2x00dev->tx_power = conf->power_level;
Ivo van Doorn42c82852008-12-02 18:20:04 +0100283 rt2x00dev->short_retry = conf->short_frame_max_tx_count;
284 rt2x00dev->long_retry = conf->long_frame_max_tx_count;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700285}