blob: a35265cc7540ddb78882f3488dcf77d6ab079bc7 [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:
Andrey Yurovskya07dbea2008-12-20 10:55:34 +010045 case NL80211_IFTYPE_MESH_POINT:
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010046 conf.sync = TSF_SYNC_BEACON;
Ivo van Doornfeb24692007-10-06 14:14:29 +020047 break;
Johannes Berg05c914f2008-09-11 00:01:58 +020048 case NL80211_IFTYPE_STATION:
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010049 conf.sync = TSF_SYNC_INFRA;
Ivo van Doornfeb24692007-10-06 14:14:29 +020050 break;
51 default:
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010052 conf.sync = TSF_SYNC_NONE;
Ivo van Doornfeb24692007-10-06 14:14:29 +020053 break;
54 }
55
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010056 /*
57 * Note that when NULL is passed as address we will send
58 * 00:00:00:00:00 to the device to clear the address.
59 * This will prevent the device being confused when it wants
60 * to ACK frames or consideres itself associated.
61 */
62 memset(&conf.mac, 0, sizeof(conf.mac));
63 if (mac)
64 memcpy(&conf.mac, mac, ETH_ALEN);
65
66 memset(&conf.bssid, 0, sizeof(conf.bssid));
67 if (bssid)
68 memcpy(&conf.bssid, bssid, ETH_ALEN);
69
70 flags |= CONFIG_UPDATE_TYPE;
71 if (mac || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
72 flags |= CONFIG_UPDATE_MAC;
73 if (bssid || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
74 flags |= CONFIG_UPDATE_BSSID;
75
76 rt2x00dev->ops->lib->config_intf(rt2x00dev, intf, &conf, flags);
77}
78
Ivo van Doorn72810372008-03-09 22:46:18 +010079void rt2x00lib_config_erp(struct rt2x00_dev *rt2x00dev,
80 struct rt2x00_intf *intf,
81 struct ieee80211_bss_conf *bss_conf)
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010082{
Ivo van Doorn72810372008-03-09 22:46:18 +010083 struct rt2x00lib_erp erp;
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010084
Ivo van Doorn72810372008-03-09 22:46:18 +010085 memset(&erp, 0, sizeof(erp));
86
87 erp.short_preamble = bss_conf->use_short_preamble;
Ivo van Doorne360c4c2008-07-09 15:12:06 +020088 erp.cts_protection = bss_conf->use_cts_prot;
89
Ivo van Doorne4ea1c42008-10-29 17:17:57 +010090 erp.slot_time = bss_conf->use_short_slot ? SHORT_SLOT_TIME : SLOT_TIME;
91 erp.sifs = SIFS;
92 erp.pifs = bss_conf->use_short_slot ? SHORT_PIFS : PIFS;
93 erp.difs = bss_conf->use_short_slot ? SHORT_DIFS : DIFS;
94 erp.eifs = bss_conf->use_short_slot ? SHORT_EIFS : EIFS;
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010095
Ivo van Doornbad13632008-11-09 20:47:00 +010096 erp.ack_timeout = PLCP + erp.difs + GET_DURATION(ACK_SIZE, 10);
97 erp.ack_consume_time = SIFS + PLCP + GET_DURATION(ACK_SIZE, 10);
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +010098
Ivo van Doorn72810372008-03-09 22:46:18 +010099 if (bss_conf->use_short_preamble) {
100 erp.ack_timeout += SHORT_PREAMBLE;
101 erp.ack_consume_time += SHORT_PREAMBLE;
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100102 } else {
Ivo van Doorn72810372008-03-09 22:46:18 +0100103 erp.ack_timeout += PREAMBLE;
104 erp.ack_consume_time += PREAMBLE;
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100105 }
106
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100107 erp.basic_rates = bss_conf->basic_rates;
108
Ivo van Doorn3a643d22008-03-25 14:13:18 +0100109 rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp);
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 Doorn6d643602008-11-02 00:38:10 +0100122 struct antenna_setup *ant)
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200123{
Ivo van Doorn0b927a02008-12-02 18:20:22 +0100124 struct antenna_setup *def = &rt2x00dev->default_ant;
125 struct antenna_setup *active = &rt2x00dev->link.ant.active;
126
Ivo van Doorn6d643602008-11-02 00:38:10 +0100127 /*
128 * Failsafe: Make sure we are not sending the
129 * ANTENNA_SW_DIVERSITY state to the driver.
130 * If that happes fallback to hardware default,
131 * or our own default.
Ivo van Doorn0b927a02008-12-02 18:20:22 +0100132 * The calls to rt2x00lib_config_antenna_check()
133 * might have caused that we restore back to the already
134 * active setting. If that has happened we can quit.
Ivo van Doorn6d643602008-11-02 00:38:10 +0100135 */
Ivo van Doorn0b927a02008-12-02 18:20:22 +0100136 ant->rx = rt2x00lib_config_antenna_check(ant->rx, def->rx);
137 ant->tx = rt2x00lib_config_antenna_check(ant->tx, def->tx);
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200138
Ivo van Doorn0b927a02008-12-02 18:20:22 +0100139 if (ant->rx == active->rx && ant->tx == active->tx)
Ivo van Doorn05253c92008-02-25 23:15:08 +0100140 return;
141
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200142 /*
Ivo van Doorne25c4bb2007-10-27 13:39:28 +0200143 * Antenna setup changes require the RX to be disabled,
144 * else the changes will be ignored by the device.
145 */
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200146 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn61667d82008-02-25 23:15:05 +0100147 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF_LINK);
Ivo van Doorne25c4bb2007-10-27 13:39:28 +0200148
149 /*
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200150 * Write new antenna setup to device and reset the link tuner.
151 * The latter is required since we need to recalibrate the
152 * noise-sensitivity ratio for the new setup.
153 */
Ivo van Doorn6d643602008-11-02 00:38:10 +0100154 rt2x00dev->ops->lib->config_ant(rt2x00dev, ant);
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100155
Ivo van Doorn84e31962008-12-20 10:53:29 +0100156 rt2x00link_reset_tuner(rt2x00dev, true);
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200157
Ivo van Doorn0b927a02008-12-02 18:20:22 +0100158 memcpy(active, ant, sizeof(*ant));
Ivo van Doorne25c4bb2007-10-27 13:39:28 +0200159
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200160 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn61667d82008-02-25 23:15:05 +0100161 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON_LINK);
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200162}
163
Ivo van Doorn066cb632007-09-25 20:55:39 +0200164void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100165 struct ieee80211_conf *conf,
166 unsigned int ieee80211_flags)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700167{
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200168 struct rt2x00lib_conf libconf;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700169
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200170 memset(&libconf, 0, sizeof(libconf));
171
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100172 libconf.conf = conf;
Ivo van Doornf5507ce2008-02-03 15:51:13 +0100173
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100174 if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL) {
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200175 memcpy(&libconf.rf,
Johannes Berg8318d782008-01-24 19:38:38 +0100176 &rt2x00dev->spec.channels[conf->channel->hw_value],
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200177 sizeof(libconf.rf));
Ivo van Doorn8c5e7a52008-08-04 16:38:47 +0200178
179 memcpy(&libconf.channel,
180 &rt2x00dev->spec.channels_info[conf->channel->hw_value],
181 sizeof(libconf.channel));
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200182 }
183
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200184 /*
185 * Start configuration.
186 */
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100187 rt2x00dev->ops->lib->config(rt2x00dev, &libconf, ieee80211_flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700188
189 /*
190 * Some configuration changes affect the link quality
191 * which means we need to reset the link tuner.
192 */
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100193 if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL)
Ivo van Doorn84e31962008-12-20 10:53:29 +0100194 rt2x00link_reset_tuner(rt2x00dev, false);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700195
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100196 rt2x00dev->curr_band = conf->channel->band;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700197 rt2x00dev->tx_power = conf->power_level;
Ivo van Doorn42c82852008-12-02 18:20:04 +0100198 rt2x00dev->short_retry = conf->short_frame_max_tx_count;
199 rt2x00dev->long_retry = conf->long_frame_max_tx_count;
Mattias Nissler16b19512007-10-13 16:26:57 +0200200
Ivo van Doorne4ea1c42008-10-29 17:17:57 +0100201 rt2x00dev->rx_status.band = conf->channel->band;
202 rt2x00dev->rx_status.freq = conf->channel->center_freq;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700203}