3501788ab4982915998e9cac8326d6e84d8c02f7
[pandora-kernel.git] / drivers / net / wireless / rt2x00 / rt2x00config.c
1 /*
2         Copyright (C) 2004 - 2009 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 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         case NL80211_IFTYPE_AP:
45         case NL80211_IFTYPE_MESH_POINT:
46         case NL80211_IFTYPE_WDS:
47                 conf.sync = TSF_SYNC_BEACON;
48                 break;
49         case NL80211_IFTYPE_STATION:
50                 conf.sync = TSF_SYNC_INFRA;
51                 break;
52         default:
53                 conf.sync = TSF_SYNC_NONE;
54                 break;
55         }
56
57         /*
58          * Note that when NULL is passed as address we will send
59          * 00:00:00:00:00 to the device to clear the address.
60          * This will prevent the device being confused when it wants
61          * to ACK frames or consideres itself associated.
62          */
63         memset(&conf.mac, 0, sizeof(conf.mac));
64         if (mac)
65                 memcpy(&conf.mac, mac, ETH_ALEN);
66
67         memset(&conf.bssid, 0, sizeof(conf.bssid));
68         if (bssid)
69                 memcpy(&conf.bssid, bssid, ETH_ALEN);
70
71         flags |= CONFIG_UPDATE_TYPE;
72         if (mac || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
73                 flags |= CONFIG_UPDATE_MAC;
74         if (bssid || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count))
75                 flags |= CONFIG_UPDATE_BSSID;
76
77         rt2x00dev->ops->lib->config_intf(rt2x00dev, intf, &conf, flags);
78 }
79
80 void rt2x00lib_config_erp(struct rt2x00_dev *rt2x00dev,
81                           struct rt2x00_intf *intf,
82                           struct ieee80211_bss_conf *bss_conf)
83 {
84         struct rt2x00lib_erp erp;
85
86         memset(&erp, 0, sizeof(erp));
87
88         erp.short_preamble = bss_conf->use_short_preamble;
89         erp.cts_protection = bss_conf->use_cts_prot;
90
91         erp.slot_time = bss_conf->use_short_slot ? SHORT_SLOT_TIME : SLOT_TIME;
92         erp.sifs = SIFS;
93         erp.pifs = bss_conf->use_short_slot ? SHORT_PIFS : PIFS;
94         erp.difs = bss_conf->use_short_slot ? SHORT_DIFS : DIFS;
95         erp.eifs = bss_conf->use_short_slot ? SHORT_EIFS : EIFS;
96
97         erp.ack_timeout = PLCP + erp.difs + GET_DURATION(ACK_SIZE, 10);
98         erp.ack_consume_time = SIFS + PLCP + GET_DURATION(ACK_SIZE, 10);
99
100         if (bss_conf->use_short_preamble) {
101                 erp.ack_timeout += SHORT_PREAMBLE;
102                 erp.ack_consume_time += SHORT_PREAMBLE;
103         } else {
104                 erp.ack_timeout += PREAMBLE;
105                 erp.ack_consume_time += PREAMBLE;
106         }
107
108         erp.basic_rates = bss_conf->basic_rates;
109         erp.beacon_int = bss_conf->beacon_int;
110
111         /* Update global beacon interval time, this is needed for PS support */
112         rt2x00dev->beacon_int = bss_conf->beacon_int;
113
114         rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp);
115 }
116
117 static inline
118 enum antenna rt2x00lib_config_antenna_check(enum antenna current_ant,
119                                             enum antenna default_ant)
120 {
121         if (current_ant != ANTENNA_SW_DIVERSITY)
122                 return current_ant;
123         return (default_ant != ANTENNA_SW_DIVERSITY) ? default_ant : ANTENNA_B;
124 }
125
126 void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
127                               struct antenna_setup config)
128 {
129         struct link_ant *ant = &rt2x00dev->link.ant;
130         struct antenna_setup *def = &rt2x00dev->default_ant;
131         struct antenna_setup *active = &rt2x00dev->link.ant.active;
132
133         /*
134          * Failsafe: Make sure we are not sending the
135          * ANTENNA_SW_DIVERSITY state to the driver.
136          * If that happens, fallback to hardware defaults,
137          * or our own default.
138          * If diversity handling is active for a particular antenna,
139          * we shouldn't overwrite that antenna.
140          * The calls to rt2x00lib_config_antenna_check()
141          * might have caused that we restore back to the already
142          * active setting. If that has happened we can quit.
143          */
144         if (!(ant->flags & ANTENNA_RX_DIVERSITY))
145                 config.rx = rt2x00lib_config_antenna_check(config.rx, def->rx);
146         else
147                 config.rx = active->rx;
148
149         if (!(ant->flags & ANTENNA_TX_DIVERSITY))
150                 config.tx = rt2x00lib_config_antenna_check(config.tx, def->tx);
151         else
152                 config.tx = active->tx;
153
154         if (config.rx == active->rx && config.tx == active->tx)
155                 return;
156
157         /*
158          * Antenna setup changes require the RX to be disabled,
159          * else the changes will be ignored by the device.
160          */
161         if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
162                 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF_LINK);
163
164         /*
165          * Write new antenna setup to device and reset the link tuner.
166          * The latter is required since we need to recalibrate the
167          * noise-sensitivity ratio for the new setup.
168          */
169         rt2x00dev->ops->lib->config_ant(rt2x00dev, &config);
170
171         rt2x00link_reset_tuner(rt2x00dev, true);
172
173         memcpy(active, &config, sizeof(config));
174
175         if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
176                 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON_LINK);
177 }
178
179 void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
180                       struct ieee80211_conf *conf,
181                       unsigned int ieee80211_flags)
182 {
183         struct rt2x00lib_conf libconf;
184
185         memset(&libconf, 0, sizeof(libconf));
186
187         libconf.conf = conf;
188
189         if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL) {
190                 if (conf_is_ht40(conf))
191                         __set_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
192                 else
193                         __clear_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
194
195                 memcpy(&libconf.rf,
196                        &rt2x00dev->spec.channels[conf->channel->hw_value],
197                        sizeof(libconf.rf));
198
199                 memcpy(&libconf.channel,
200                        &rt2x00dev->spec.channels_info[conf->channel->hw_value],
201                        sizeof(libconf.channel));
202         }
203
204         /*
205          * Start configuration.
206          */
207         rt2x00dev->ops->lib->config(rt2x00dev, &libconf, ieee80211_flags);
208
209         /*
210          * Some configuration changes affect the link quality
211          * which means we need to reset the link tuner.
212          */
213         if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL)
214                 rt2x00link_reset_tuner(rt2x00dev, false);
215
216         rt2x00dev->curr_band = conf->channel->band;
217         rt2x00dev->tx_power = conf->power_level;
218         rt2x00dev->short_retry = conf->short_frame_max_tx_count;
219         rt2x00dev->long_retry = conf->long_frame_max_tx_count;
220
221         rt2x00dev->rx_status.band = conf->channel->band;
222         rt2x00dev->rx_status.freq = conf->channel->center_freq;
223 }