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