]> Pileus Git - ~andy/linux/blob - drivers/net/wireless/rt2x00/rt2x00config.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[~andy/linux] / drivers / net / wireless / rt2x00 / rt2x00config.c
1 /*
2         Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
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 nl80211_iftype type,
35                            const u8 *mac, const u8 *bssid)
36 {
37         struct rt2x00intf_conf conf;
38         unsigned int flags = 0;
39
40         conf.type = type;
41
42         switch (type) {
43         case NL80211_IFTYPE_ADHOC:
44                 conf.sync = TSF_SYNC_ADHOC;
45                 break;
46         case NL80211_IFTYPE_AP:
47         case NL80211_IFTYPE_MESH_POINT:
48         case NL80211_IFTYPE_WDS:
49                 conf.sync = TSF_SYNC_AP_NONE;
50                 break;
51         case NL80211_IFTYPE_STATION:
52                 conf.sync = TSF_SYNC_INFRA;
53                 break;
54         default:
55                 conf.sync = TSF_SYNC_NONE;
56                 break;
57         }
58
59         /*
60          * Note that when NULL is passed as address we will send
61          * 00:00:00:00:00 to the device to clear the address.
62          * This will prevent the device being confused when it wants
63          * to ACK frames or considers itself associated.
64          */
65         memset(conf.mac, 0, sizeof(conf.mac));
66         if (mac)
67                 memcpy(conf.mac, mac, ETH_ALEN);
68
69         memset(conf.bssid, 0, sizeof(conf.bssid));
70         if (bssid)
71                 memcpy(conf.bssid, bssid, ETH_ALEN);
72
73         flags |= CONFIG_UPDATE_TYPE;
74         if (mac || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
75                 flags |= CONFIG_UPDATE_MAC;
76         if (bssid || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
77                 flags |= CONFIG_UPDATE_BSSID;
78
79         rt2x00dev->ops->lib->config_intf(rt2x00dev, intf, &conf, flags);
80 }
81
82 void rt2x00lib_config_erp(struct rt2x00_dev *rt2x00dev,
83                           struct rt2x00_intf *intf,
84                           struct ieee80211_bss_conf *bss_conf,
85                           u32 changed)
86 {
87         struct rt2x00lib_erp erp;
88
89         memset(&erp, 0, sizeof(erp));
90
91         erp.short_preamble = bss_conf->use_short_preamble;
92         erp.cts_protection = bss_conf->use_cts_prot;
93
94         erp.slot_time = bss_conf->use_short_slot ? SHORT_SLOT_TIME : SLOT_TIME;
95         erp.sifs = SIFS;
96         erp.pifs = bss_conf->use_short_slot ? SHORT_PIFS : PIFS;
97         erp.difs = bss_conf->use_short_slot ? SHORT_DIFS : DIFS;
98         erp.eifs = bss_conf->use_short_slot ? SHORT_EIFS : EIFS;
99
100         erp.basic_rates = bss_conf->basic_rates;
101         erp.beacon_int = bss_conf->beacon_int;
102
103         /* Update global beacon interval time, this is needed for PS support */
104         rt2x00dev->beacon_int = bss_conf->beacon_int;
105
106         if (changed & BSS_CHANGED_HT)
107                 erp.ht_opmode = bss_conf->ht_operation_mode;
108
109         rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp, changed);
110 }
111
112 void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
113                               struct antenna_setup config)
114 {
115         struct link_ant *ant = &rt2x00dev->link.ant;
116         struct antenna_setup *def = &rt2x00dev->default_ant;
117         struct antenna_setup *active = &rt2x00dev->link.ant.active;
118
119         /*
120          * When the caller tries to send the SW diversity,
121          * we must update the ANTENNA_RX_DIVERSITY flag to
122          * enable the antenna diversity in the link tuner.
123          *
124          * Secondly, we must guarentee we never send the
125          * software antenna diversity command to the driver.
126          */
127         if (!(ant->flags & ANTENNA_RX_DIVERSITY)) {
128                 if (config.rx == ANTENNA_SW_DIVERSITY) {
129                         ant->flags |= ANTENNA_RX_DIVERSITY;
130
131                         if (def->rx == ANTENNA_SW_DIVERSITY)
132                                 config.rx = ANTENNA_B;
133                         else
134                                 config.rx = def->rx;
135                 }
136         } else if (config.rx == ANTENNA_SW_DIVERSITY)
137                 config.rx = active->rx;
138
139         if (!(ant->flags & ANTENNA_TX_DIVERSITY)) {
140                 if (config.tx == ANTENNA_SW_DIVERSITY) {
141                         ant->flags |= ANTENNA_TX_DIVERSITY;
142
143                         if (def->tx == ANTENNA_SW_DIVERSITY)
144                                 config.tx = ANTENNA_B;
145                         else
146                                 config.tx = def->tx;
147                 }
148         } else if (config.tx == ANTENNA_SW_DIVERSITY)
149                 config.tx = active->tx;
150
151         /*
152          * Antenna setup changes require the RX to be disabled,
153          * else the changes will be ignored by the device.
154          */
155         if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
156                 rt2x00queue_stop_queue(rt2x00dev->rx);
157
158         /*
159          * Write new antenna setup to device and reset the link tuner.
160          * The latter is required since we need to recalibrate the
161          * noise-sensitivity ratio for the new setup.
162          */
163         rt2x00dev->ops->lib->config_ant(rt2x00dev, &config);
164
165         rt2x00link_reset_tuner(rt2x00dev, true);
166
167         memcpy(active, &config, sizeof(config));
168
169         if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
170                 rt2x00queue_start_queue(rt2x00dev->rx);
171 }
172
173 static u16 rt2x00ht_center_channel(struct rt2x00_dev *rt2x00dev,
174                                    struct ieee80211_conf *conf)
175 {
176         struct hw_mode_spec *spec = &rt2x00dev->spec;
177         int center_channel;
178         u16 i;
179
180         /*
181          * Initialize center channel to current channel.
182          */
183         center_channel = spec->channels[conf->channel->hw_value].channel;
184
185         /*
186          * Adjust center channel to HT40+ and HT40- operation.
187          */
188         if (conf_is_ht40_plus(conf))
189                 center_channel += 2;
190         else if (conf_is_ht40_minus(conf))
191                 center_channel -= (center_channel == 14) ? 1 : 2;
192
193         for (i = 0; i < spec->num_channels; i++)
194                 if (spec->channels[i].channel == center_channel)
195                         return i;
196
197         WARN_ON(1);
198         return conf->channel->hw_value;
199 }
200
201 void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
202                       struct ieee80211_conf *conf,
203                       unsigned int ieee80211_flags)
204 {
205         struct rt2x00lib_conf libconf;
206         u16 hw_value;
207
208         memset(&libconf, 0, sizeof(libconf));
209
210         libconf.conf = conf;
211
212         if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL) {
213                 if (conf_is_ht40(conf)) {
214                         set_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
215                         hw_value = rt2x00ht_center_channel(rt2x00dev, conf);
216                 } else {
217                         clear_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
218                         hw_value = conf->channel->hw_value;
219                 }
220
221                 memcpy(&libconf.rf,
222                        &rt2x00dev->spec.channels[hw_value],
223                        sizeof(libconf.rf));
224
225                 memcpy(&libconf.channel,
226                        &rt2x00dev->spec.channels_info[hw_value],
227                        sizeof(libconf.channel));
228         }
229
230         /*
231          * Start configuration.
232          */
233         rt2x00dev->ops->lib->config(rt2x00dev, &libconf, ieee80211_flags);
234
235         /*
236          * Some configuration changes affect the link quality
237          * which means we need to reset the link tuner.
238          */
239         if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL)
240                 rt2x00link_reset_tuner(rt2x00dev, false);
241
242         rt2x00dev->curr_band = conf->channel->band;
243         rt2x00dev->curr_freq = conf->channel->center_freq;
244         rt2x00dev->tx_power = conf->power_level;
245         rt2x00dev->short_retry = conf->short_frame_max_tx_count;
246         rt2x00dev->long_retry = conf->long_frame_max_tx_count;
247 }