blob: 2f4cb8de998156fc37b1acb038638120b7d0c16c [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
Ivo van Doorn811aa9c2008-02-03 15:42:53 +01002 Copyright (C) 2004 - 2008 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,
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010035 u8 *mac, 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:
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010045 conf.sync = TSF_SYNC_BEACON;
Ivo van Doornfeb24692007-10-06 14:14:29 +020046 break;
Johannes Berg05c914f2008-09-11 00:01:58 +020047 case NL80211_IFTYPE_STATION:
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010048 conf.sync = TSF_SYNC_INFRA;
Ivo van Doornfeb24692007-10-06 14:14:29 +020049 break;
50 default:
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010051 conf.sync = TSF_SYNC_NONE;
Ivo van Doornfeb24692007-10-06 14:14:29 +020052 break;
53 }
54
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010055 /*
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
Ivo van Doorn72810372008-03-09 22:46:18 +010078void rt2x00lib_config_erp(struct rt2x00_dev *rt2x00dev,
79 struct rt2x00_intf *intf,
80 struct ieee80211_bss_conf *bss_conf)
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010081{
Ivo van Doorn72810372008-03-09 22:46:18 +010082 struct rt2x00lib_erp erp;
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010083
Ivo van Doorn72810372008-03-09 22:46:18 +010084 memset(&erp, 0, sizeof(erp));
85
86 erp.short_preamble = bss_conf->use_short_preamble;
Ivo van Doorne360c4c2008-07-09 15:12:06 +020087 erp.cts_protection = bss_conf->use_cts_prot;
88
Ivo van Doorne4ea1c42008-10-29 17:17:57 +010089 erp.slot_time = bss_conf->use_short_slot ? SHORT_SLOT_TIME : SLOT_TIME;
90 erp.sifs = SIFS;
91 erp.pifs = bss_conf->use_short_slot ? SHORT_PIFS : PIFS;
92 erp.difs = bss_conf->use_short_slot ? SHORT_DIFS : DIFS;
93 erp.eifs = bss_conf->use_short_slot ? SHORT_EIFS : EIFS;
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010094
Ivo van Doornbad13632008-11-09 20:47:00 +010095 erp.ack_timeout = PLCP + erp.difs + GET_DURATION(ACK_SIZE, 10);
96 erp.ack_consume_time = SIFS + PLCP + GET_DURATION(ACK_SIZE, 10);
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010097
Ivo van Doorn72810372008-03-09 22:46:18 +010098 if (bss_conf->use_short_preamble) {
99 erp.ack_timeout += SHORT_PREAMBLE;
100 erp.ack_consume_time += SHORT_PREAMBLE;
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100101 } else {
Ivo van Doorn72810372008-03-09 22:46:18 +0100102 erp.ack_timeout += PREAMBLE;
103 erp.ack_consume_time += PREAMBLE;
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100104 }
105
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100106 erp.basic_rates = bss_conf->basic_rates;
107
Ivo van Doorn3a643d22008-03-25 14:13:18 +0100108 rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700109}
110
Ivo van Doorn0b927a02008-12-02 18:20:22 +0100111static inline
112enum antenna rt2x00lib_config_antenna_check(enum antenna current_ant,
113 enum antenna default_ant)
114{
115 if (current_ant != ANTENNA_SW_DIVERSITY)
116 return current_ant;
117 return (default_ant != ANTENNA_SW_DIVERSITY) ? default_ant : ANTENNA_B;
118}
119
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200120void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn6d643602008-11-02 00:38:10 +0100121 struct antenna_setup *ant)
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200122{
Ivo van Doorn0b927a02008-12-02 18:20:22 +0100123 struct antenna_setup *def = &rt2x00dev->default_ant;
124 struct antenna_setup *active = &rt2x00dev->link.ant.active;
125
Ivo van Doorn6d643602008-11-02 00:38:10 +0100126 /*
127 * Failsafe: Make sure we are not sending the
128 * ANTENNA_SW_DIVERSITY state to the driver.
129 * If that happes fallback to hardware default,
130 * or our own default.
Ivo van Doorn0b927a02008-12-02 18:20:22 +0100131 * The calls to rt2x00lib_config_antenna_check()
132 * might have caused that we restore back to the already
133 * active setting. If that has happened we can quit.
Ivo van Doorn6d643602008-11-02 00:38:10 +0100134 */
Ivo van Doorn0b927a02008-12-02 18:20:22 +0100135 ant->rx = rt2x00lib_config_antenna_check(ant->rx, def->rx);
136 ant->tx = rt2x00lib_config_antenna_check(ant->tx, def->tx);
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200137
Ivo van Doorn0b927a02008-12-02 18:20:22 +0100138 if (ant->rx == active->rx && ant->tx == active->tx)
Ivo van Doorn05253c92008-02-25 23:15:08 +0100139 return;
140
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200141 /*
Ivo van Doorne25c4bb2007-10-27 13:39:28 +0200142 * Antenna setup changes require the RX to be disabled,
143 * else the changes will be ignored by the device.
144 */
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200145 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn61667d82008-02-25 23:15:05 +0100146 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF_LINK);
Ivo van Doorne25c4bb2007-10-27 13:39:28 +0200147
148 /*
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200149 * Write new antenna setup to device and reset the link tuner.
150 * The latter is required since we need to recalibrate the
151 * noise-sensitivity ratio for the new setup.
152 */
Ivo van Doorn6d643602008-11-02 00:38:10 +0100153 rt2x00dev->ops->lib->config_ant(rt2x00dev, ant);
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100154
Ivo van Doorn84e31962008-12-20 10:53:29 +0100155 rt2x00link_reset_tuner(rt2x00dev, true);
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200156
Ivo van Doorn0b927a02008-12-02 18:20:22 +0100157 memcpy(active, ant, sizeof(*ant));
Ivo van Doorne25c4bb2007-10-27 13:39:28 +0200158
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200159 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn61667d82008-02-25 23:15:05 +0100160 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON_LINK);
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200161}
162
Ivo van Doorn066cb632007-09-25 20:55:39 +0200163void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100164 struct ieee80211_conf *conf,
165 unsigned int ieee80211_flags)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700166{
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200167 struct rt2x00lib_conf libconf;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700168
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200169 memset(&libconf, 0, sizeof(libconf));
170
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100171 libconf.conf = conf;
Ivo van Doornf5507ce2008-02-03 15:51:13 +0100172
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100173 if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL) {
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200174 memcpy(&libconf.rf,
Johannes Berg8318d782008-01-24 19:38:38 +0100175 &rt2x00dev->spec.channels[conf->channel->hw_value],
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200176 sizeof(libconf.rf));
Ivo van Doorn8c5e7a52008-08-04 16:38:47 +0200177
178 memcpy(&libconf.channel,
179 &rt2x00dev->spec.channels_info[conf->channel->hw_value],
180 sizeof(libconf.channel));
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200181 }
182
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200183 /*
184 * Start configuration.
185 */
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100186 rt2x00dev->ops->lib->config(rt2x00dev, &libconf, ieee80211_flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700187
188 /*
189 * Some configuration changes affect the link quality
190 * which means we need to reset the link tuner.
191 */
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100192 if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL)
Ivo van Doorn84e31962008-12-20 10:53:29 +0100193 rt2x00link_reset_tuner(rt2x00dev, false);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700194
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100195 rt2x00dev->curr_band = conf->channel->band;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700196 rt2x00dev->tx_power = conf->power_level;
Ivo van Doorn42c82852008-12-02 18:20:04 +0100197 rt2x00dev->short_retry = conf->short_frame_max_tx_count;
198 rt2x00dev->long_retry = conf->long_frame_max_tx_count;
Mattias Nissler16b19512007-10-13 16:26:57 +0200199
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100200 rt2x00dev->rx_status.band = conf->channel->band;
201 rt2x00dev->rx_status.freq = conf->channel->center_freq;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700202}