db09a641a948fb471d7d19c509b4ec8744c2f905
[pandora-kernel.git] / 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 consideres 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 {
86         struct rt2x00lib_erp erp;
87
88         memset(&erp, 0, sizeof(erp));
89
90         erp.short_preamble = bss_conf->use_short_preamble;
91         erp.cts_protection = bss_conf->use_cts_prot;
92
93         erp.slot_time = bss_conf->use_short_slot ? SHORT_SLOT_TIME : SLOT_TIME;
94         erp.sifs = SIFS;
95         erp.pifs = bss_conf->use_short_slot ? SHORT_PIFS : PIFS;
96         erp.difs = bss_conf->use_short_slot ? SHORT_DIFS : DIFS;
97         erp.eifs = bss_conf->use_short_slot ? SHORT_EIFS : EIFS;
98
99         erp.basic_rates = bss_conf->basic_rates;
100         erp.beacon_int = bss_conf->beacon_int;
101
102         /* Update global beacon interval time, this is needed for PS support */
103         rt2x00dev->beacon_int = bss_conf->beacon_int;
104
105         rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp);
106 }
107
108 static inline
109 enum antenna rt2x00lib_config_antenna_check(enum antenna current_ant,
110                                             enum antenna default_ant)
111 {
112         if (current_ant != ANTENNA_SW_DIVERSITY)
113                 return current_ant;
114         return (default_ant != ANTENNA_SW_DIVERSITY) ? default_ant : ANTENNA_B;
115 }
116
117 void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
118                               struct antenna_setup config)
119 {
120         struct link_ant *ant = &rt2x00dev->link.ant;
121         struct antenna_setup *def = &rt2x00dev->default_ant;
122         struct antenna_setup *active = &rt2x00dev->link.ant.active;
123
124         /*
125          * Failsafe: Make sure we are not sending the
126          * ANTENNA_SW_DIVERSITY state to the driver.
127          * If that happens, fallback to hardware defaults,
128          * or our own default.
129          */
130         if (!(ant->flags & ANTENNA_RX_DIVERSITY))
131                 config.rx = rt2x00lib_config_antenna_check(config.rx, def->rx);
132         else if(config.rx == ANTENNA_SW_DIVERSITY)
133                 config.rx = active->rx;
134
135         if (!(ant->flags & ANTENNA_TX_DIVERSITY))
136                 config.tx = rt2x00lib_config_antenna_check(config.tx, def->tx);
137         else if (config.tx == ANTENNA_SW_DIVERSITY)
138                 config.tx = active->tx;
139
140         /*
141          * Antenna setup changes require the RX to be disabled,
142          * else the changes will be ignored by the device.
143          */
144         if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
145                 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF_LINK);
146
147         /*
148          * Write new antenna setup to device and reset the link tuner.
149          * The latter is required since we need to recalibrate the
150          * noise-sensitivity ratio for the new setup.
151          */
152         rt2x00dev->ops->lib->config_ant(rt2x00dev, &config);
153
154         rt2x00link_reset_tuner(rt2x00dev, true);
155
156         memcpy(active, &config, sizeof(config));
157
158         if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
159                 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON_LINK);
160 }
161
162 void rt2x00lib_config(struct rt2x00_dev *rt2x00dev,
163                       struct ieee80211_conf *conf,
164                       unsigned int ieee80211_flags)
165 {
166         struct rt2x00lib_conf libconf;
167         u16 hw_value;
168
169         memset(&libconf, 0, sizeof(libconf));
170
171         libconf.conf = conf;
172
173         if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL) {
174                 if (conf_is_ht40(conf)) {
175                         __set_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
176                         hw_value = rt2x00ht_center_channel(rt2x00dev, conf);
177                 } else {
178                         __clear_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags);
179                         hw_value = conf->channel->hw_value;
180                 }
181
182                 memcpy(&libconf.rf,
183                        &rt2x00dev->spec.channels[hw_value],
184                        sizeof(libconf.rf));
185
186                 memcpy(&libconf.channel,
187                        &rt2x00dev->spec.channels_info[hw_value],
188                        sizeof(libconf.channel));
189         }
190
191         /*
192          * Start configuration.
193          */
194         rt2x00dev->ops->lib->config(rt2x00dev, &libconf, ieee80211_flags);
195
196         /*
197          * Some configuration changes affect the link quality
198          * which means we need to reset the link tuner.
199          */
200         if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL)
201                 rt2x00link_reset_tuner(rt2x00dev, false);
202
203         rt2x00dev->curr_band = conf->channel->band;
204         rt2x00dev->curr_freq = conf->channel->center_freq;
205         rt2x00dev->tx_power = conf->power_level;
206         rt2x00dev->short_retry = conf->short_frame_max_tx_count;
207         rt2x00dev->long_retry = conf->long_frame_max_tx_count;
208 }