[PATCH] rt2x00: Implement SW diversity
[safe/jmp/linux-2.6] / drivers / net / wireless / rt2x00 / rt2x00config.c
1 /*
2         Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3         <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
26 /*
27  * Set enviroment defines for rt2x00.h
28  */
29 #define DRV_NAME "rt2x00lib"
30
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33
34 #include "rt2x00.h"
35 #include "rt2x00lib.h"
36
37
38 /*
39  * The MAC and BSSID addressess are simple array of bytes,
40  * these arrays are little endian, so when sending the addressess
41  * to the drivers, copy the it into a endian-signed variable.
42  *
43  * Note that all devices (except rt2500usb) have 32 bits
44  * register word sizes. This means that whatever variable we
45  * pass _must_ be a multiple of 32 bits. Otherwise the device
46  * might not accept what we are sending to it.
47  * This will also make it easier for the driver to write
48  * the data to the device.
49  *
50  * Also note that when NULL is passed as address the
51  * we will send 00:00:00:00:00 to the device to clear the address.
52  * This will prevent the device being confused when it wants
53  * to ACK frames or consideres itself associated.
54  */
55 void rt2x00lib_config_mac_addr(struct rt2x00_dev *rt2x00dev, u8 *mac)
56 {
57         __le32 reg[2];
58
59         memset(&reg, 0, sizeof(reg));
60         if (mac)
61                 memcpy(&reg, mac, ETH_ALEN);
62
63         rt2x00dev->ops->lib->config_mac_addr(rt2x00dev, &reg[0]);
64 }
65
66 void rt2x00lib_config_bssid(struct rt2x00_dev *rt2x00dev, u8 *bssid)
67 {
68         __le32 reg[2];
69
70         memset(&reg, 0, sizeof(reg));
71         if (bssid)
72                 memcpy(&reg, bssid, ETH_ALEN);
73
74         rt2x00dev->ops->lib->config_bssid(rt2x00dev, &reg[0]);
75 }
76
77 void rt2x00lib_config_type(struct rt2x00_dev *rt2x00dev, const int type)
78 {
79         int tsf_sync;
80
81         switch (type) {
82         case IEEE80211_IF_TYPE_IBSS:
83         case IEEE80211_IF_TYPE_AP:
84                 tsf_sync = TSF_SYNC_BEACON;
85                 break;
86         case IEEE80211_IF_TYPE_STA:
87                 tsf_sync = TSF_SYNC_INFRA;
88                 break;
89         default:
90                 tsf_sync = TSF_SYNC_NONE;
91                 break;
92         }
93
94         rt2x00dev->ops->lib->config_type(rt2x00dev, type, tsf_sync);
95 }
96
97 void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
98                               enum antenna rx, enum antenna tx)
99 {
100         struct rt2x00lib_conf libconf;
101
102         libconf.ant.rx = rx;
103         libconf.ant.tx = tx;
104
105         /*
106          * Write new antenna setup to device and reset the link tuner.
107          * The latter is required since we need to recalibrate the
108          * noise-sensitivity ratio for the new setup.
109          */
110         rt2x00dev->ops->lib->config(rt2x00dev, CONFIG_UPDATE_ANTENNA, &libconf);
111         rt2x00lib_reset_link_tuner(rt2x00dev);
112
113         rt2x00dev->link.ant.active.rx = libconf.ant.rx;
114         rt2x00dev->link.ant.active.tx = libconf.ant.tx;
115 }
116
117 void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
118                       struct ieee80211_conf *conf, const int force_config)
119 {
120         struct rt2x00lib_conf libconf;
121         struct ieee80211_hw_mode *mode;
122         struct ieee80211_rate *rate;
123         struct antenna_setup *default_ant = &rt2x00dev->default_ant;
124         struct antenna_setup *active_ant = &rt2x00dev->link.ant.active;
125         int flags = 0;
126         int short_slot_time;
127
128         /*
129          * In some situations we want to force all configurations
130          * to be reloaded (When resuming for instance).
131          */
132         if (force_config) {
133                 flags = CONFIG_UPDATE_ALL;
134                 goto config;
135         }
136
137         /*
138          * Check which configuration options have been
139          * updated and should be send to the device.
140          */
141         if (rt2x00dev->rx_status.phymode != conf->phymode)
142                 flags |= CONFIG_UPDATE_PHYMODE;
143         if (rt2x00dev->rx_status.channel != conf->channel)
144                 flags |= CONFIG_UPDATE_CHANNEL;
145         if (rt2x00dev->tx_power != conf->power_level)
146                 flags |= CONFIG_UPDATE_TXPOWER;
147
148         /*
149          * Determining changes in the antenna setups request several checks:
150          * antenna_sel_{r,t}x = 0
151          *    -> Does active_{r,t}x match default_{r,t}x
152          *    -> Is default_{r,t}x SW_DIVERSITY
153          * antenna_sel_{r,t}x = 1/2
154          *    -> Does active_{r,t}x match antenna_sel_{r,t}x
155          * The reason for not updating the antenna while SW diversity
156          * should be used is simple: Software diversity means that
157          * we should switch between the antenna's based on the
158          * quality. This means that the current antenna is good enough
159          * to work with untill the link tuner decides that an antenna
160          * switch should be performed.
161          */
162         if (!conf->antenna_sel_rx &&
163             default_ant->rx != ANTENNA_SW_DIVERSITY &&
164             default_ant->rx != active_ant->rx)
165                 flags |= CONFIG_UPDATE_ANTENNA;
166         else if (conf->antenna_sel_rx &&
167                  conf->antenna_sel_rx != active_ant->rx)
168                 flags |= CONFIG_UPDATE_ANTENNA;
169
170         if (!conf->antenna_sel_tx &&
171             default_ant->tx != ANTENNA_SW_DIVERSITY &&
172             default_ant->tx != active_ant->tx)
173                 flags |= CONFIG_UPDATE_ANTENNA;
174         else if (conf->antenna_sel_tx &&
175                  conf->antenna_sel_tx != active_ant->tx)
176                 flags |= CONFIG_UPDATE_ANTENNA;
177
178         /*
179          * The following configuration options are never
180          * stored anywhere and will always be updated.
181          */
182         flags |= CONFIG_UPDATE_SLOT_TIME;
183         flags |= CONFIG_UPDATE_BEACON_INT;
184
185         /*
186          * We have determined what options should be updated,
187          * now precalculate device configuration values depending
188          * on what configuration options need to be updated.
189          */
190 config:
191         memset(&libconf, 0, sizeof(libconf));
192
193         if (flags & CONFIG_UPDATE_PHYMODE) {
194                 switch (conf->phymode) {
195                 case MODE_IEEE80211A:
196                         libconf.phymode = HWMODE_A;
197                         break;
198                 case MODE_IEEE80211B:
199                         libconf.phymode = HWMODE_B;
200                         break;
201                 case MODE_IEEE80211G:
202                         libconf.phymode = HWMODE_G;
203                         break;
204                 default:
205                         ERROR(rt2x00dev,
206                               "Attempt to configure unsupported mode (%d)"
207                               "Defaulting to 802.11b", conf->phymode);
208                         libconf.phymode = HWMODE_B;
209                 }
210
211                 mode = &rt2x00dev->hwmodes[libconf.phymode];
212                 rate = &mode->rates[mode->num_rates - 1];
213
214                 libconf.basic_rates =
215                     DEVICE_GET_RATE_FIELD(rate->val, RATEMASK) & DEV_BASIC_RATEMASK;
216         }
217
218         if (flags & CONFIG_UPDATE_CHANNEL) {
219                 memcpy(&libconf.rf,
220                        &rt2x00dev->spec.channels[conf->channel_val],
221                        sizeof(libconf.rf));
222         }
223
224         if (flags & CONFIG_UPDATE_ANTENNA) {
225                 if (conf->antenna_sel_rx)
226                         libconf.ant.rx = conf->antenna_sel_rx;
227                 else if (default_ant->rx != ANTENNA_SW_DIVERSITY)
228                         libconf.ant.rx = default_ant->rx;
229                 else if (active_ant->rx == ANTENNA_SW_DIVERSITY)
230                         libconf.ant.rx = ANTENNA_B;
231
232                 if (conf->antenna_sel_tx)
233                         libconf.ant.tx = conf->antenna_sel_tx;
234                 else if (default_ant->tx != ANTENNA_SW_DIVERSITY)
235                         libconf.ant.tx = default_ant->tx;
236                 else if (active_ant->tx == ANTENNA_SW_DIVERSITY)
237                         libconf.ant.tx = ANTENNA_B;
238         }
239
240         if (flags & CONFIG_UPDATE_SLOT_TIME) {
241                 short_slot_time = conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME;
242
243                 libconf.slot_time =
244                     short_slot_time ? SHORT_SLOT_TIME : SLOT_TIME;
245                 libconf.sifs = SIFS;
246                 libconf.pifs = short_slot_time ? SHORT_PIFS : PIFS;
247                 libconf.difs = short_slot_time ? SHORT_DIFS : DIFS;
248                 libconf.eifs = EIFS;
249         }
250
251         libconf.conf = conf;
252
253         /*
254          * Start configuration.
255          */
256         rt2x00dev->ops->lib->config(rt2x00dev, flags, &libconf);
257
258         /*
259          * Some configuration changes affect the link quality
260          * which means we need to reset the link tuner.
261          */
262         if (flags & (CONFIG_UPDATE_CHANNEL | CONFIG_UPDATE_ANTENNA))
263                 rt2x00lib_reset_link_tuner(rt2x00dev);
264
265         rt2x00dev->curr_hwmode = libconf.phymode;
266         rt2x00dev->rx_status.phymode = conf->phymode;
267         rt2x00dev->rx_status.freq = conf->freq;
268         rt2x00dev->rx_status.channel = conf->channel;
269         rt2x00dev->tx_power = conf->power_level;
270         rt2x00dev->link.ant.active.rx = libconf.ant.rx;
271         rt2x00dev->link.ant.active.tx = libconf.ant.tx;
272 }