blob: e7f67d5eda52341d488532a67f257008e6c26143 [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
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 Doorn6bb40dd2008-02-03 15:49:59 +010032void rt2x00lib_config_intf(struct rt2x00_dev *rt2x00dev,
33 struct rt2x00_intf *intf,
Johannes Berg05c914f2008-09-11 00:01:58 +020034 enum nl80211_iftype type,
Johannes Berg5f936f12009-01-21 12:47:05 +010035 const u8 *mac, const u8 *bssid)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070036{
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010037 struct rt2x00intf_conf conf;
38 unsigned int flags = 0;
Ivo van Doorn4abee4b2007-10-06 14:11:46 +020039
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010040 conf.type = type;
Ivo van Doornfeb24692007-10-06 14:14:29 +020041
42 switch (type) {
Johannes Berg05c914f2008-09-11 00:01:58 +020043 case NL80211_IFTYPE_ADHOC:
Helmut Schaaab8966d2010-07-11 12:30:13 +020044 conf.sync = TSF_SYNC_ADHOC;
45 break;
Johannes Berg05c914f2008-09-11 00:01:58 +020046 case NL80211_IFTYPE_AP:
Andrey Yurovskya07dbea2008-12-20 10:55:34 +010047 case NL80211_IFTYPE_MESH_POINT:
Ivo van Doornce292a62008-12-20 10:57:02 +010048 case NL80211_IFTYPE_WDS:
Helmut Schaaab8966d2010-07-11 12:30:13 +020049 conf.sync = TSF_SYNC_AP_NONE;
Ivo van Doornfeb24692007-10-06 14:14:29 +020050 break;
Johannes Berg05c914f2008-09-11 00:01:58 +020051 case NL80211_IFTYPE_STATION:
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010052 conf.sync = TSF_SYNC_INFRA;
Ivo van Doornfeb24692007-10-06 14:14:29 +020053 break;
54 default:
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010055 conf.sync = TSF_SYNC_NONE;
Ivo van Doornfeb24692007-10-06 14:14:29 +020056 break;
57 }
58
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010059 /*
60 * Note that when NULL is passed as address we will send
61 * 00:00:00:00:00 to the device to clear the address.
62 * This will prevent the device being confused when it wants
63 * to ACK frames or consideres itself associated.
64 */
Gertjan van Wingerde736e3ac2010-12-27 15:05:55 +010065 memset(conf.mac, 0, sizeof(conf.mac));
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010066 if (mac)
Gertjan van Wingerde736e3ac2010-12-27 15:05:55 +010067 memcpy(conf.mac, mac, ETH_ALEN);
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010068
Gertjan van Wingerde736e3ac2010-12-27 15:05:55 +010069 memset(conf.bssid, 0, sizeof(conf.bssid));
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010070 if (bssid)
Gertjan van Wingerde736e3ac2010-12-27 15:05:55 +010071 memcpy(conf.bssid, bssid, ETH_ALEN);
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010072
73 flags |= CONFIG_UPDATE_TYPE;
74 if (mac || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
75 flags |= CONFIG_UPDATE_MAC;
76 if (bssid || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
77 flags |= CONFIG_UPDATE_BSSID;
78
79 rt2x00dev->ops->lib->config_intf(rt2x00dev, intf, &conf, flags);
80}
81
Ivo van Doorn72810372008-03-09 22:46:18 +010082void rt2x00lib_config_erp(struct rt2x00_dev *rt2x00dev,
83 struct rt2x00_intf *intf,
Helmut Schaa02044642010-09-08 20:56:32 +020084 struct ieee80211_bss_conf *bss_conf,
85 u32 changed)
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010086{
Ivo van Doorn72810372008-03-09 22:46:18 +010087 struct rt2x00lib_erp erp;
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010088
Ivo van Doorn72810372008-03-09 22:46:18 +010089 memset(&erp, 0, sizeof(erp));
90
91 erp.short_preamble = bss_conf->use_short_preamble;
Ivo van Doorne360c4c2008-07-09 15:12:06 +020092 erp.cts_protection = bss_conf->use_cts_prot;
93
Ivo van Doorne4ea1c42008-10-29 17:17:57 +010094 erp.slot_time = bss_conf->use_short_slot ? SHORT_SLOT_TIME : SLOT_TIME;
95 erp.sifs = SIFS;
96 erp.pifs = bss_conf->use_short_slot ? SHORT_PIFS : PIFS;
97 erp.difs = bss_conf->use_short_slot ? SHORT_DIFS : DIFS;
98 erp.eifs = bss_conf->use_short_slot ? SHORT_EIFS : EIFS;
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010099
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100100 erp.basic_rates = bss_conf->basic_rates;
Ivo van Doorn8a566af2009-05-21 19:16:46 +0200101 erp.beacon_int = bss_conf->beacon_int;
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100102
Ivo van Doorn6b347bf2009-05-23 21:09:28 +0200103 /* Update global beacon interval time, this is needed for PS support */
104 rt2x00dev->beacon_int = bss_conf->beacon_int;
105
Helmut Schaa87c19152010-10-02 11:28:34 +0200106 if (changed & BSS_CHANGED_HT)
107 erp.ht_opmode = bss_conf->ht_operation_mode;
108
Helmut Schaa02044642010-09-08 20:56:32 +0200109 rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp, changed);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700110}
111
Ivo van Doorn0b927a02008-12-02 18:20:22 +0100112static inline
113enum antenna rt2x00lib_config_antenna_check(enum antenna current_ant,
114 enum antenna default_ant)
115{
116 if (current_ant != ANTENNA_SW_DIVERSITY)
117 return current_ant;
118 return (default_ant != ANTENNA_SW_DIVERSITY) ? default_ant : ANTENNA_B;
119}
120
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200121void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200122 struct antenna_setup config)
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200123{
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200124 struct link_ant *ant = &rt2x00dev->link.ant;
Ivo van Doorn0b927a02008-12-02 18:20:22 +0100125 struct antenna_setup *def = &rt2x00dev->default_ant;
126 struct antenna_setup *active = &rt2x00dev->link.ant.active;
127
Ivo van Doorn6d643602008-11-02 00:38:10 +0100128 /*
129 * Failsafe: Make sure we are not sending the
130 * ANTENNA_SW_DIVERSITY state to the driver.
Luis Correia49513482009-07-17 21:39:19 +0200131 * If that happens, fallback to hardware defaults,
Ivo van Doorn6d643602008-11-02 00:38:10 +0100132 * or our own default.
133 */
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200134 if (!(ant->flags & ANTENNA_RX_DIVERSITY))
135 config.rx = rt2x00lib_config_antenna_check(config.rx, def->rx);
Mark Einon8a239032010-11-06 15:46:17 +0100136 else if (config.rx == ANTENNA_SW_DIVERSITY)
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200137 config.rx = active->rx;
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200138
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200139 if (!(ant->flags & ANTENNA_TX_DIVERSITY))
140 config.tx = rt2x00lib_config_antenna_check(config.tx, def->tx);
Lars Ericsson85f4d642010-09-08 20:55:36 +0200141 else if (config.tx == ANTENNA_SW_DIVERSITY)
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200142 config.tx = active->tx;
143
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200144 /*
Ivo van Doorne25c4bb2007-10-27 13:39:28 +0200145 * Antenna setup changes require the RX to be disabled,
146 * else the changes will be ignored by the device.
147 */
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200148 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100149 rt2x00queue_stop_queue(rt2x00dev->rx);
Ivo van Doorne25c4bb2007-10-27 13:39:28 +0200150
151 /*
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200152 * Write new antenna setup to device and reset the link tuner.
153 * The latter is required since we need to recalibrate the
154 * noise-sensitivity ratio for the new setup.
155 */
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200156 rt2x00dev->ops->lib->config_ant(rt2x00dev, &config);
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100157
Ivo van Doorn84e31962008-12-20 10:53:29 +0100158 rt2x00link_reset_tuner(rt2x00dev, true);
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200159
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200160 memcpy(active, &config, sizeof(config));
Ivo van Doorne25c4bb2007-10-27 13:39:28 +0200161
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200162 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn0b7fde52010-12-13 12:35:17 +0100163 rt2x00queue_start_queue(rt2x00dev->rx);
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200164}
165
Ivo van Doorn066cb632007-09-25 20:55:39 +0200166void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100167 struct ieee80211_conf *conf,
168 unsigned int ieee80211_flags)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700169{
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200170 struct rt2x00lib_conf libconf;
Gertjan van Wingerde06443e42010-06-03 10:52:08 +0200171 u16 hw_value;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700172
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200173 memset(&libconf, 0, sizeof(libconf));
174
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100175 libconf.conf = conf;
Ivo van Doornf5507ce2008-02-03 15:51:13 +0100176
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100177 if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL) {
Gertjan van Wingerde06443e42010-06-03 10:52:08 +0200178 if (conf_is_ht40(conf)) {
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200179 __set_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
Gertjan van Wingerde06443e42010-06-03 10:52:08 +0200180 hw_value = rt2x00ht_center_channel(rt2x00dev, conf);
181 } else {
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200182 __clear_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
Gertjan van Wingerde06443e42010-06-03 10:52:08 +0200183 hw_value = conf->channel->hw_value;
184 }
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200185
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200186 memcpy(&libconf.rf,
Gertjan van Wingerde06443e42010-06-03 10:52:08 +0200187 &rt2x00dev->spec.channels[hw_value],
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200188 sizeof(libconf.rf));
Ivo van Doorn8c5e7a52008-08-04 16:38:47 +0200189
190 memcpy(&libconf.channel,
Gertjan van Wingerde06443e42010-06-03 10:52:08 +0200191 &rt2x00dev->spec.channels_info[hw_value],
Ivo van Doorn8c5e7a52008-08-04 16:38:47 +0200192 sizeof(libconf.channel));
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200193 }
194
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200195 /*
196 * Start configuration.
197 */
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100198 rt2x00dev->ops->lib->config(rt2x00dev, &libconf, ieee80211_flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700199
200 /*
201 * Some configuration changes affect the link quality
202 * which means we need to reset the link tuner.
203 */
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100204 if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL)
Ivo van Doorn84e31962008-12-20 10:53:29 +0100205 rt2x00link_reset_tuner(rt2x00dev, false);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700206
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100207 rt2x00dev->curr_band = conf->channel->band;
Ivo van Doorne5ef5ba2010-08-06 20:49:27 +0200208 rt2x00dev->curr_freq = conf->channel->center_freq;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700209 rt2x00dev->tx_power = conf->power_level;
Ivo van Doorn42c82852008-12-02 18:20:04 +0100210 rt2x00dev->short_retry = conf->short_frame_max_tx_count;
211 rt2x00dev->long_retry = conf->long_frame_max_tx_count;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700212}