Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1 | /* |
Ivo van Doorn | 811aa9c | 2008-02-03 15:42:53 +0100 | [diff] [blame] | 2 | Copyright (C) 2004 - 2008 rt2x00 SourceForge Project |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 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 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 26 | #include <linux/kernel.h> |
| 27 | #include <linux/module.h> |
| 28 | |
| 29 | #include "rt2x00.h" |
| 30 | #include "rt2x00lib.h" |
| 31 | |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame^] | 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) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 36 | { |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame^] | 37 | struct rt2x00intf_conf conf; |
| 38 | unsigned int flags = 0; |
Ivo van Doorn | 4abee4b | 2007-10-06 14:11:46 +0200 | [diff] [blame] | 39 | |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame^] | 40 | conf.type = type; |
Ivo van Doorn | feb2469 | 2007-10-06 14:14:29 +0200 | [diff] [blame] | 41 | |
| 42 | switch (type) { |
| 43 | case IEEE80211_IF_TYPE_IBSS: |
| 44 | case IEEE80211_IF_TYPE_AP: |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame^] | 45 | conf.sync = TSF_SYNC_BEACON; |
Ivo van Doorn | feb2469 | 2007-10-06 14:14:29 +0200 | [diff] [blame] | 46 | break; |
| 47 | case IEEE80211_IF_TYPE_STA: |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame^] | 48 | conf.sync = TSF_SYNC_INFRA; |
Ivo van Doorn | feb2469 | 2007-10-06 14:14:29 +0200 | [diff] [blame] | 49 | break; |
| 50 | default: |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame^] | 51 | conf.sync = TSF_SYNC_NONE; |
Ivo van Doorn | feb2469 | 2007-10-06 14:14:29 +0200 | [diff] [blame] | 52 | break; |
| 53 | } |
| 54 | |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame^] | 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_preamble(struct rt2x00_dev *rt2x00dev, |
| 79 | struct rt2x00_intf *intf, |
| 80 | const unsigned int short_preamble) |
| 81 | { |
| 82 | int retval; |
| 83 | int ack_timeout; |
| 84 | int ack_consume_time; |
| 85 | |
| 86 | ack_timeout = PLCP + get_duration(ACK_SIZE, 10); |
| 87 | ack_consume_time = SIFS + PLCP + get_duration(ACK_SIZE, 10); |
| 88 | |
| 89 | if (rt2x00dev->hw->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME) |
| 90 | ack_timeout += SHORT_DIFS; |
| 91 | else |
| 92 | ack_timeout += DIFS; |
| 93 | |
| 94 | if (short_preamble) { |
| 95 | ack_timeout += SHORT_PREAMBLE; |
| 96 | ack_consume_time += SHORT_PREAMBLE; |
| 97 | } else { |
| 98 | ack_timeout += PREAMBLE; |
| 99 | ack_consume_time += PREAMBLE; |
| 100 | } |
| 101 | |
| 102 | retval = rt2x00dev->ops->lib->config_preamble(rt2x00dev, |
| 103 | short_preamble, |
| 104 | ack_timeout, |
| 105 | ack_consume_time); |
| 106 | |
| 107 | spin_lock(&intf->lock); |
| 108 | |
| 109 | if (retval) { |
| 110 | intf->delayed_flags |= DELAYED_CONFIG_PREAMBLE; |
| 111 | queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->intf_work); |
| 112 | } |
| 113 | |
| 114 | spin_unlock(&intf->lock); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Ivo van Doorn | 69f81a2 | 2007-10-13 16:26:36 +0200 | [diff] [blame] | 117 | void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev, |
| 118 | enum antenna rx, enum antenna tx) |
| 119 | { |
| 120 | struct rt2x00lib_conf libconf; |
| 121 | |
| 122 | libconf.ant.rx = rx; |
| 123 | libconf.ant.tx = tx; |
| 124 | |
Ivo van Doorn | 05253c9 | 2008-02-25 23:15:08 +0100 | [diff] [blame] | 125 | if (rx == rt2x00dev->link.ant.active.rx && |
| 126 | tx == rt2x00dev->link.ant.active.tx) |
| 127 | return; |
| 128 | |
Ivo van Doorn | 69f81a2 | 2007-10-13 16:26:36 +0200 | [diff] [blame] | 129 | /* |
Ivo van Doorn | e25c4bb | 2007-10-27 13:39:28 +0200 | [diff] [blame] | 130 | * Antenna setup changes require the RX to be disabled, |
| 131 | * else the changes will be ignored by the device. |
| 132 | */ |
| 133 | if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) |
Ivo van Doorn | 61667d8 | 2008-02-25 23:15:05 +0100 | [diff] [blame] | 134 | rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF_LINK); |
Ivo van Doorn | e25c4bb | 2007-10-27 13:39:28 +0200 | [diff] [blame] | 135 | |
| 136 | /* |
Ivo van Doorn | 69f81a2 | 2007-10-13 16:26:36 +0200 | [diff] [blame] | 137 | * Write new antenna setup to device and reset the link tuner. |
| 138 | * The latter is required since we need to recalibrate the |
| 139 | * noise-sensitivity ratio for the new setup. |
| 140 | */ |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame^] | 141 | rt2x00dev->ops->lib->config(rt2x00dev, &libconf, CONFIG_UPDATE_ANTENNA); |
Ivo van Doorn | 69f81a2 | 2007-10-13 16:26:36 +0200 | [diff] [blame] | 142 | rt2x00lib_reset_link_tuner(rt2x00dev); |
| 143 | |
| 144 | rt2x00dev->link.ant.active.rx = libconf.ant.rx; |
| 145 | rt2x00dev->link.ant.active.tx = libconf.ant.tx; |
Ivo van Doorn | e25c4bb | 2007-10-27 13:39:28 +0200 | [diff] [blame] | 146 | |
| 147 | if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) |
Ivo van Doorn | 61667d8 | 2008-02-25 23:15:05 +0100 | [diff] [blame] | 148 | rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON_LINK); |
Ivo van Doorn | 69f81a2 | 2007-10-13 16:26:36 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 151 | void rt2x00lib_config(struct rt2x00_dev *rt2x00dev, |
| 152 | struct ieee80211_conf *conf, const int force_config) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 153 | { |
Ivo van Doorn | 5c58ee5 | 2007-10-06 13:34:52 +0200 | [diff] [blame] | 154 | struct rt2x00lib_conf libconf; |
| 155 | struct ieee80211_hw_mode *mode; |
| 156 | struct ieee80211_rate *rate; |
Ivo van Doorn | addc81b | 2007-10-13 16:26:23 +0200 | [diff] [blame] | 157 | struct antenna_setup *default_ant = &rt2x00dev->default_ant; |
Ivo van Doorn | 69f81a2 | 2007-10-13 16:26:36 +0200 | [diff] [blame] | 158 | struct antenna_setup *active_ant = &rt2x00dev->link.ant.active; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 159 | int flags = 0; |
Ivo van Doorn | 5c58ee5 | 2007-10-06 13:34:52 +0200 | [diff] [blame] | 160 | int short_slot_time; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 161 | |
| 162 | /* |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 163 | * In some situations we want to force all configurations |
| 164 | * to be reloaded (When resuming for instance). |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 165 | */ |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 166 | if (force_config) { |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 167 | flags = CONFIG_UPDATE_ALL; |
| 168 | goto config; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Check which configuration options have been |
| 173 | * updated and should be send to the device. |
| 174 | */ |
| 175 | if (rt2x00dev->rx_status.phymode != conf->phymode) |
| 176 | flags |= CONFIG_UPDATE_PHYMODE; |
| 177 | if (rt2x00dev->rx_status.channel != conf->channel) |
| 178 | flags |= CONFIG_UPDATE_CHANNEL; |
| 179 | if (rt2x00dev->tx_power != conf->power_level) |
| 180 | flags |= CONFIG_UPDATE_TXPOWER; |
Ivo van Doorn | addc81b | 2007-10-13 16:26:23 +0200 | [diff] [blame] | 181 | |
| 182 | /* |
| 183 | * Determining changes in the antenna setups request several checks: |
| 184 | * antenna_sel_{r,t}x = 0 |
| 185 | * -> Does active_{r,t}x match default_{r,t}x |
| 186 | * -> Is default_{r,t}x SW_DIVERSITY |
| 187 | * antenna_sel_{r,t}x = 1/2 |
| 188 | * -> Does active_{r,t}x match antenna_sel_{r,t}x |
| 189 | * The reason for not updating the antenna while SW diversity |
| 190 | * should be used is simple: Software diversity means that |
| 191 | * we should switch between the antenna's based on the |
| 192 | * quality. This means that the current antenna is good enough |
| 193 | * to work with untill the link tuner decides that an antenna |
| 194 | * switch should be performed. |
| 195 | */ |
| 196 | if (!conf->antenna_sel_rx && |
| 197 | default_ant->rx != ANTENNA_SW_DIVERSITY && |
| 198 | default_ant->rx != active_ant->rx) |
| 199 | flags |= CONFIG_UPDATE_ANTENNA; |
| 200 | else if (conf->antenna_sel_rx && |
| 201 | conf->antenna_sel_rx != active_ant->rx) |
| 202 | flags |= CONFIG_UPDATE_ANTENNA; |
Mattias Nissler | 16b1951 | 2007-10-13 16:26:57 +0200 | [diff] [blame] | 203 | else if (active_ant->rx == ANTENNA_SW_DIVERSITY) |
| 204 | flags |= CONFIG_UPDATE_ANTENNA; |
Ivo van Doorn | addc81b | 2007-10-13 16:26:23 +0200 | [diff] [blame] | 205 | |
| 206 | if (!conf->antenna_sel_tx && |
| 207 | default_ant->tx != ANTENNA_SW_DIVERSITY && |
| 208 | default_ant->tx != active_ant->tx) |
| 209 | flags |= CONFIG_UPDATE_ANTENNA; |
| 210 | else if (conf->antenna_sel_tx && |
| 211 | conf->antenna_sel_tx != active_ant->tx) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 212 | flags |= CONFIG_UPDATE_ANTENNA; |
Mattias Nissler | 16b1951 | 2007-10-13 16:26:57 +0200 | [diff] [blame] | 213 | else if (active_ant->tx == ANTENNA_SW_DIVERSITY) |
| 214 | flags |= CONFIG_UPDATE_ANTENNA; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 215 | |
| 216 | /* |
| 217 | * The following configuration options are never |
| 218 | * stored anywhere and will always be updated. |
| 219 | */ |
| 220 | flags |= CONFIG_UPDATE_SLOT_TIME; |
| 221 | flags |= CONFIG_UPDATE_BEACON_INT; |
| 222 | |
Ivo van Doorn | 5c58ee5 | 2007-10-06 13:34:52 +0200 | [diff] [blame] | 223 | /* |
| 224 | * We have determined what options should be updated, |
| 225 | * now precalculate device configuration values depending |
| 226 | * on what configuration options need to be updated. |
| 227 | */ |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 228 | config: |
Ivo van Doorn | 5c58ee5 | 2007-10-06 13:34:52 +0200 | [diff] [blame] | 229 | memset(&libconf, 0, sizeof(libconf)); |
| 230 | |
| 231 | if (flags & CONFIG_UPDATE_PHYMODE) { |
| 232 | switch (conf->phymode) { |
| 233 | case MODE_IEEE80211A: |
| 234 | libconf.phymode = HWMODE_A; |
| 235 | break; |
| 236 | case MODE_IEEE80211B: |
| 237 | libconf.phymode = HWMODE_B; |
| 238 | break; |
| 239 | case MODE_IEEE80211G: |
| 240 | libconf.phymode = HWMODE_G; |
| 241 | break; |
| 242 | default: |
| 243 | ERROR(rt2x00dev, |
| 244 | "Attempt to configure unsupported mode (%d)" |
| 245 | "Defaulting to 802.11b", conf->phymode); |
| 246 | libconf.phymode = HWMODE_B; |
| 247 | } |
| 248 | |
| 249 | mode = &rt2x00dev->hwmodes[libconf.phymode]; |
| 250 | rate = &mode->rates[mode->num_rates - 1]; |
| 251 | |
| 252 | libconf.basic_rates = |
| 253 | DEVICE_GET_RATE_FIELD(rate->val, RATEMASK) & DEV_BASIC_RATEMASK; |
| 254 | } |
| 255 | |
| 256 | if (flags & CONFIG_UPDATE_CHANNEL) { |
| 257 | memcpy(&libconf.rf, |
| 258 | &rt2x00dev->spec.channels[conf->channel_val], |
| 259 | sizeof(libconf.rf)); |
| 260 | } |
| 261 | |
Ivo van Doorn | addc81b | 2007-10-13 16:26:23 +0200 | [diff] [blame] | 262 | if (flags & CONFIG_UPDATE_ANTENNA) { |
| 263 | if (conf->antenna_sel_rx) |
| 264 | libconf.ant.rx = conf->antenna_sel_rx; |
| 265 | else if (default_ant->rx != ANTENNA_SW_DIVERSITY) |
| 266 | libconf.ant.rx = default_ant->rx; |
| 267 | else if (active_ant->rx == ANTENNA_SW_DIVERSITY) |
| 268 | libconf.ant.rx = ANTENNA_B; |
| 269 | |
| 270 | if (conf->antenna_sel_tx) |
| 271 | libconf.ant.tx = conf->antenna_sel_tx; |
| 272 | else if (default_ant->tx != ANTENNA_SW_DIVERSITY) |
| 273 | libconf.ant.tx = default_ant->tx; |
| 274 | else if (active_ant->tx == ANTENNA_SW_DIVERSITY) |
| 275 | libconf.ant.tx = ANTENNA_B; |
| 276 | } |
| 277 | |
Ivo van Doorn | 5c58ee5 | 2007-10-06 13:34:52 +0200 | [diff] [blame] | 278 | if (flags & CONFIG_UPDATE_SLOT_TIME) { |
| 279 | short_slot_time = conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME; |
| 280 | |
| 281 | libconf.slot_time = |
| 282 | short_slot_time ? SHORT_SLOT_TIME : SLOT_TIME; |
| 283 | libconf.sifs = SIFS; |
| 284 | libconf.pifs = short_slot_time ? SHORT_PIFS : PIFS; |
| 285 | libconf.difs = short_slot_time ? SHORT_DIFS : DIFS; |
| 286 | libconf.eifs = EIFS; |
| 287 | } |
| 288 | |
| 289 | libconf.conf = conf; |
| 290 | |
| 291 | /* |
| 292 | * Start configuration. |
| 293 | */ |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame^] | 294 | rt2x00dev->ops->lib->config(rt2x00dev, &libconf, flags); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 295 | |
| 296 | /* |
| 297 | * Some configuration changes affect the link quality |
| 298 | * which means we need to reset the link tuner. |
| 299 | */ |
| 300 | if (flags & (CONFIG_UPDATE_CHANNEL | CONFIG_UPDATE_ANTENNA)) |
| 301 | rt2x00lib_reset_link_tuner(rt2x00dev); |
| 302 | |
Mattias Nissler | 16b1951 | 2007-10-13 16:26:57 +0200 | [diff] [blame] | 303 | if (flags & CONFIG_UPDATE_PHYMODE) { |
| 304 | rt2x00dev->curr_hwmode = libconf.phymode; |
| 305 | rt2x00dev->rx_status.phymode = conf->phymode; |
| 306 | } |
| 307 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 308 | rt2x00dev->rx_status.freq = conf->freq; |
| 309 | rt2x00dev->rx_status.channel = conf->channel; |
| 310 | rt2x00dev->tx_power = conf->power_level; |
Mattias Nissler | 16b1951 | 2007-10-13 16:26:57 +0200 | [diff] [blame] | 311 | |
| 312 | if (flags & CONFIG_UPDATE_ANTENNA) { |
| 313 | rt2x00dev->link.ant.active.rx = libconf.ant.rx; |
| 314 | rt2x00dev->link.ant.active.tx = libconf.ant.tx; |
| 315 | } |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 316 | } |