blob: 3845316ccd3941624ae0e4e0674d1fc52f48f51f [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
Ivo van Doorn4e54c712009-01-17 20:42:32 +01002 Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
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:
44 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:
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010047 conf.sync = TSF_SYNC_BEACON;
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
61 * to ACK frames or consideres itself associated.
62 */
63 memset(&conf.mac, 0, sizeof(conf.mac));
64 if (mac)
65 memcpy(&conf.mac, mac, ETH_ALEN);
66
67 memset(&conf.bssid, 0, sizeof(conf.bssid));
68 if (bssid)
69 memcpy(&conf.bssid, bssid, ETH_ALEN);
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
Ivo van Doorn72810372008-03-09 22:46:18 +010080void rt2x00lib_config_erp(struct rt2x00_dev *rt2x00dev,
81 struct rt2x00_intf *intf,
82 struct ieee80211_bss_conf *bss_conf)
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010083{
Ivo van Doorn72810372008-03-09 22:46:18 +010084 struct rt2x00lib_erp erp;
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010085
Ivo van Doorn72810372008-03-09 22:46:18 +010086 memset(&erp, 0, sizeof(erp));
87
88 erp.short_preamble = bss_conf->use_short_preamble;
Ivo van Doorne360c4c2008-07-09 15:12:06 +020089 erp.cts_protection = bss_conf->use_cts_prot;
90
Ivo van Doorne4ea1c42008-10-29 17:17:57 +010091 erp.slot_time = bss_conf->use_short_slot ? SHORT_SLOT_TIME : SLOT_TIME;
92 erp.sifs = SIFS;
93 erp.pifs = bss_conf->use_short_slot ? SHORT_PIFS : PIFS;
94 erp.difs = bss_conf->use_short_slot ? SHORT_DIFS : DIFS;
95 erp.eifs = bss_conf->use_short_slot ? SHORT_EIFS : EIFS;
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010096
Ivo van Doornbad13632008-11-09 20:47:00 +010097 erp.ack_timeout = PLCP + erp.difs + GET_DURATION(ACK_SIZE, 10);
98 erp.ack_consume_time = SIFS + PLCP + GET_DURATION(ACK_SIZE, 10);
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010099
Ivo van Doorn72810372008-03-09 22:46:18 +0100100 if (bss_conf->use_short_preamble) {
101 erp.ack_timeout += SHORT_PREAMBLE;
102 erp.ack_consume_time += SHORT_PREAMBLE;
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100103 } else {
Ivo van Doorn72810372008-03-09 22:46:18 +0100104 erp.ack_timeout += PREAMBLE;
105 erp.ack_consume_time += PREAMBLE;
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100106 }
107
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100108 erp.basic_rates = bss_conf->basic_rates;
Ivo van Doorn8a566af2009-05-21 19:16:46 +0200109 erp.beacon_int = bss_conf->beacon_int;
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100110
Ivo van Doorn6b347bf2009-05-23 21:09:28 +0200111 /* Update global beacon interval time, this is needed for PS support */
112 rt2x00dev->beacon_int = bss_conf->beacon_int;
113
Ivo van Doorn3a643d22008-03-25 14:13:18 +0100114 rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700115}
116
Ivo van Doorn0b927a02008-12-02 18:20:22 +0100117static inline
118enum antenna rt2x00lib_config_antenna_check(enum antenna current_ant,
119 enum antenna default_ant)
120{
121 if (current_ant != ANTENNA_SW_DIVERSITY)
122 return current_ant;
123 return (default_ant != ANTENNA_SW_DIVERSITY) ? default_ant : ANTENNA_B;
124}
125
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200126void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
Lars Ericssoneb87eaa2009-07-18 20:21:52 +0200127 struct antenna_setup ant)
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200128{
Ivo van Doorn0b927a02008-12-02 18:20:22 +0100129 struct antenna_setup *def = &rt2x00dev->default_ant;
130 struct antenna_setup *active = &rt2x00dev->link.ant.active;
131
Ivo van Doorn6d643602008-11-02 00:38:10 +0100132 /*
133 * Failsafe: Make sure we are not sending the
134 * ANTENNA_SW_DIVERSITY state to the driver.
Luis Correia49513482009-07-17 21:39:19 +0200135 * If that happens, fallback to hardware defaults,
Ivo van Doorn6d643602008-11-02 00:38:10 +0100136 * or our own default.
Ivo van Doorn0b927a02008-12-02 18:20:22 +0100137 * The calls to rt2x00lib_config_antenna_check()
138 * might have caused that we restore back to the already
139 * active setting. If that has happened we can quit.
Ivo van Doorn6d643602008-11-02 00:38:10 +0100140 */
Lars Ericssoneb87eaa2009-07-18 20:21:52 +0200141 ant.rx = rt2x00lib_config_antenna_check(ant.rx, def->rx);
142 ant.tx = rt2x00lib_config_antenna_check(ant.tx, def->tx);
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200143
Lars Ericssoneb87eaa2009-07-18 20:21:52 +0200144 if (ant.rx == active->rx && ant.tx == active->tx)
Ivo van Doorn05253c92008-02-25 23:15:08 +0100145 return;
146
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200147 /*
Ivo van Doorne25c4bb2007-10-27 13:39:28 +0200148 * Antenna setup changes require the RX to be disabled,
149 * else the changes will be ignored by the device.
150 */
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200151 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn61667d82008-02-25 23:15:05 +0100152 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF_LINK);
Ivo van Doorne25c4bb2007-10-27 13:39:28 +0200153
154 /*
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200155 * Write new antenna setup to device and reset the link tuner.
156 * The latter is required since we need to recalibrate the
157 * noise-sensitivity ratio for the new setup.
158 */
Lars Ericssoneb87eaa2009-07-18 20:21:52 +0200159 rt2x00dev->ops->lib->config_ant(rt2x00dev, &ant);
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100160
Ivo van Doorn84e31962008-12-20 10:53:29 +0100161 rt2x00link_reset_tuner(rt2x00dev, true);
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200162
Lars Ericssoneb87eaa2009-07-18 20:21:52 +0200163 memcpy(active, &ant, sizeof(ant));
Ivo van Doorne25c4bb2007-10-27 13:39:28 +0200164
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200165 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn61667d82008-02-25 23:15:05 +0100166 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON_LINK);
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200167}
168
Ivo van Doorn066cb632007-09-25 20:55:39 +0200169void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100170 struct ieee80211_conf *conf,
171 unsigned int ieee80211_flags)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700172{
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200173 struct rt2x00lib_conf libconf;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700174
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200175 memset(&libconf, 0, sizeof(libconf));
176
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100177 libconf.conf = conf;
Ivo van Doornf5507ce2008-02-03 15:51:13 +0100178
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100179 if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL) {
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200180 if (conf_is_ht40(conf))
181 __set_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
182 else
183 __clear_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
184
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200185 memcpy(&libconf.rf,
Johannes Berg8318d782008-01-24 19:38:38 +0100186 &rt2x00dev->spec.channels[conf->channel->hw_value],
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200187 sizeof(libconf.rf));
Ivo van Doorn8c5e7a52008-08-04 16:38:47 +0200188
189 memcpy(&libconf.channel,
190 &rt2x00dev->spec.channels_info[conf->channel->hw_value],
191 sizeof(libconf.channel));
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200192 }
193
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200194 /*
195 * Start configuration.
196 */
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100197 rt2x00dev->ops->lib->config(rt2x00dev, &libconf, ieee80211_flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700198
199 /*
200 * Some configuration changes affect the link quality
201 * which means we need to reset the link tuner.
202 */
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100203 if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL)
Ivo van Doorn84e31962008-12-20 10:53:29 +0100204 rt2x00link_reset_tuner(rt2x00dev, false);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700205
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100206 rt2x00dev->curr_band = conf->channel->band;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700207 rt2x00dev->tx_power = conf->power_level;
Ivo van Doorn42c82852008-12-02 18:20:04 +0100208 rt2x00dev->short_retry = conf->short_frame_max_tx_count;
209 rt2x00dev->long_retry = conf->long_frame_max_tx_count;
Mattias Nissler16b19512007-10-13 16:26:57 +0200210
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100211 rt2x00dev->rx_status.band = conf->channel->band;
212 rt2x00dev->rx_status.freq = conf->channel->center_freq;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700213}