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