rt2x00: Fix basic rate initialization
[safe/jmp/linux-2.6] / drivers / net / wireless / rt2x00 / rt2x00config.c
1 /*
2         Copyright (C) 2004 - 2008 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 #include <linux/kernel.h>
27 #include <linux/module.h>
28
29 #include "rt2x00.h"
30 #include "rt2x00lib.h"
31
32 void rt2x00lib_config_intf(struct rt2x00_dev *rt2x00dev,
33                            struct rt2x00_intf *intf,
34                            enum ieee80211_if_types type,
35                            u8 *mac, u8 *bssid)
36 {
37         struct rt2x00intf_conf conf;
38         unsigned int flags = 0;
39
40         conf.type = type;
41
42         switch (type) {
43         case IEEE80211_IF_TYPE_IBSS:
44         case IEEE80211_IF_TYPE_AP:
45                 conf.sync = TSF_SYNC_BEACON;
46                 break;
47         case IEEE80211_IF_TYPE_STA:
48                 conf.sync = TSF_SYNC_INFRA;
49                 break;
50         default:
51                 conf.sync = TSF_SYNC_NONE;
52                 break;
53         }
54
55         /*
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
78 void rt2x00lib_config_erp(struct rt2x00_dev *rt2x00dev,
79                           struct rt2x00_intf *intf,
80                           struct ieee80211_bss_conf *bss_conf)
81 {
82         struct rt2x00lib_erp erp;
83         int retval;
84
85         memset(&erp, 0, sizeof(erp));
86
87         erp.short_preamble = bss_conf->use_short_preamble;
88         erp.ack_timeout = PLCP + get_duration(ACK_SIZE, 10);
89         erp.ack_consume_time = SIFS + PLCP + get_duration(ACK_SIZE, 10);
90
91         if (rt2x00dev->hw->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME)
92                 erp.ack_timeout += SHORT_DIFS;
93         else
94                 erp.ack_timeout += DIFS;
95
96         if (bss_conf->use_short_preamble) {
97                 erp.ack_timeout += SHORT_PREAMBLE;
98                 erp.ack_consume_time += SHORT_PREAMBLE;
99         } else {
100                 erp.ack_timeout += PREAMBLE;
101                 erp.ack_consume_time += PREAMBLE;
102         }
103
104         retval = rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp);
105
106         if (retval) {
107                 spin_lock(&intf->lock);
108                 intf->delayed_flags |= DELAYED_CONFIG_ERP;
109                 queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->intf_work);
110                 spin_unlock(&intf->lock);
111         }
112 }
113
114 void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
115                               enum antenna rx, enum antenna tx)
116 {
117         struct rt2x00lib_conf libconf;
118
119         libconf.ant.rx = rx;
120         libconf.ant.tx = tx;
121
122         if (rx == rt2x00dev->link.ant.active.rx &&
123             tx == rt2x00dev->link.ant.active.tx)
124                 return;
125
126         /*
127          * Antenna setup changes require the RX to be disabled,
128          * else the changes will be ignored by the device.
129          */
130         if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
131                 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF_LINK);
132
133         /*
134          * Write new antenna setup to device and reset the link tuner.
135          * The latter is required since we need to recalibrate the
136          * noise-sensitivity ratio for the new setup.
137          */
138         rt2x00dev->ops->lib->config(rt2x00dev, &libconf, CONFIG_UPDATE_ANTENNA);
139         rt2x00lib_reset_link_tuner(rt2x00dev);
140
141         rt2x00dev->link.ant.active.rx = libconf.ant.rx;
142         rt2x00dev->link.ant.active.tx = libconf.ant.tx;
143
144         if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
145                 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON_LINK);
146 }
147
148 static u32 rt2x00lib_get_basic_rates(struct ieee80211_supported_band *band)
149 {
150         const struct rt2x00_rate *rate;
151         unsigned int i;
152         u32 mask = 0;
153
154         for (i = 0; i < band->n_bitrates; i++) {
155                 rate = rt2x00_get_rate(band->bitrates[i].hw_value);
156                 if (rate->flags & DEV_RATE_BASIC)
157                         mask |= rate->ratemask;
158         }
159
160         return mask;
161 }
162
163 void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
164                       struct ieee80211_conf *conf, const int force_config)
165 {
166         struct rt2x00lib_conf libconf;
167         struct ieee80211_supported_band *band;
168         struct antenna_setup *default_ant = &rt2x00dev->default_ant;
169         struct antenna_setup *active_ant = &rt2x00dev->link.ant.active;
170         int flags = 0;
171         int short_slot_time;
172
173         /*
174          * In some situations we want to force all configurations
175          * to be reloaded (When resuming for instance).
176          */
177         if (force_config) {
178                 flags = CONFIG_UPDATE_ALL;
179                 goto config;
180         }
181
182         /*
183          * Check which configuration options have been
184          * updated and should be send to the device.
185          */
186         if (rt2x00dev->rx_status.band != conf->channel->band)
187                 flags |= CONFIG_UPDATE_PHYMODE;
188         if (rt2x00dev->rx_status.freq != conf->channel->center_freq)
189                 flags |= CONFIG_UPDATE_CHANNEL;
190         if (rt2x00dev->tx_power != conf->power_level)
191                 flags |= CONFIG_UPDATE_TXPOWER;
192
193         /*
194          * Determining changes in the antenna setups request several checks:
195          * antenna_sel_{r,t}x = 0
196          *    -> Does active_{r,t}x match default_{r,t}x
197          *    -> Is default_{r,t}x SW_DIVERSITY
198          * antenna_sel_{r,t}x = 1/2
199          *    -> Does active_{r,t}x match antenna_sel_{r,t}x
200          * The reason for not updating the antenna while SW diversity
201          * should be used is simple: Software diversity means that
202          * we should switch between the antenna's based on the
203          * quality. This means that the current antenna is good enough
204          * to work with untill the link tuner decides that an antenna
205          * switch should be performed.
206          */
207         if (!conf->antenna_sel_rx &&
208             default_ant->rx != ANTENNA_SW_DIVERSITY &&
209             default_ant->rx != active_ant->rx)
210                 flags |= CONFIG_UPDATE_ANTENNA;
211         else if (conf->antenna_sel_rx &&
212                  conf->antenna_sel_rx != active_ant->rx)
213                 flags |= CONFIG_UPDATE_ANTENNA;
214         else if (active_ant->rx == ANTENNA_SW_DIVERSITY)
215                 flags |= CONFIG_UPDATE_ANTENNA;
216
217         if (!conf->antenna_sel_tx &&
218             default_ant->tx != ANTENNA_SW_DIVERSITY &&
219             default_ant->tx != active_ant->tx)
220                 flags |= CONFIG_UPDATE_ANTENNA;
221         else if (conf->antenna_sel_tx &&
222                  conf->antenna_sel_tx != active_ant->tx)
223                 flags |= CONFIG_UPDATE_ANTENNA;
224         else if (active_ant->tx == ANTENNA_SW_DIVERSITY)
225                 flags |= CONFIG_UPDATE_ANTENNA;
226
227         /*
228          * The following configuration options are never
229          * stored anywhere and will always be updated.
230          */
231         flags |= CONFIG_UPDATE_SLOT_TIME;
232         flags |= CONFIG_UPDATE_BEACON_INT;
233
234         /*
235          * We have determined what options should be updated,
236          * now precalculate device configuration values depending
237          * on what configuration options need to be updated.
238          */
239 config:
240         memset(&libconf, 0, sizeof(libconf));
241
242         if (flags & CONFIG_UPDATE_PHYMODE) {
243                 band = &rt2x00dev->bands[conf->channel->band];
244
245                 libconf.band = conf->channel->band;
246                 libconf.basic_rates = rt2x00lib_get_basic_rates(band);
247         }
248
249         if (flags & CONFIG_UPDATE_CHANNEL) {
250                 memcpy(&libconf.rf,
251                        &rt2x00dev->spec.channels[conf->channel->hw_value],
252                        sizeof(libconf.rf));
253         }
254
255         if (flags & CONFIG_UPDATE_ANTENNA) {
256                 if (conf->antenna_sel_rx)
257                         libconf.ant.rx = conf->antenna_sel_rx;
258                 else if (default_ant->rx != ANTENNA_SW_DIVERSITY)
259                         libconf.ant.rx = default_ant->rx;
260                 else if (active_ant->rx == ANTENNA_SW_DIVERSITY)
261                         libconf.ant.rx = ANTENNA_B;
262
263                 if (conf->antenna_sel_tx)
264                         libconf.ant.tx = conf->antenna_sel_tx;
265                 else if (default_ant->tx != ANTENNA_SW_DIVERSITY)
266                         libconf.ant.tx = default_ant->tx;
267                 else if (active_ant->tx == ANTENNA_SW_DIVERSITY)
268                         libconf.ant.tx = ANTENNA_B;
269         }
270
271         if (flags & CONFIG_UPDATE_SLOT_TIME) {
272                 short_slot_time = conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME;
273
274                 libconf.slot_time =
275                     short_slot_time ? SHORT_SLOT_TIME : SLOT_TIME;
276                 libconf.sifs = SIFS;
277                 libconf.pifs = short_slot_time ? SHORT_PIFS : PIFS;
278                 libconf.difs = short_slot_time ? SHORT_DIFS : DIFS;
279                 libconf.eifs = EIFS;
280         }
281
282         libconf.conf = conf;
283
284         /*
285          * Start configuration.
286          */
287         rt2x00dev->ops->lib->config(rt2x00dev, &libconf, flags);
288
289         /*
290          * Some configuration changes affect the link quality
291          * which means we need to reset the link tuner.
292          */
293         if (flags & (CONFIG_UPDATE_CHANNEL | CONFIG_UPDATE_ANTENNA))
294                 rt2x00lib_reset_link_tuner(rt2x00dev);
295
296         if (flags & CONFIG_UPDATE_PHYMODE) {
297                 rt2x00dev->curr_band = conf->channel->band;
298                 rt2x00dev->rx_status.band = conf->channel->band;
299         }
300
301         rt2x00dev->rx_status.freq = conf->channel->center_freq;
302         rt2x00dev->tx_power = conf->power_level;
303
304         if (flags & CONFIG_UPDATE_ANTENNA) {
305                 rt2x00dev->link.ant.active.rx = libconf.ant.rx;
306                 rt2x00dev->link.ant.active.tx = libconf.ant.tx;
307         }
308 }