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, |
Johannes Berg | 05c914f | 2008-09-11 00:01:58 +0200 | [diff] [blame] | 34 | enum nl80211_iftype type, |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame] | 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) { |
Johannes Berg | 05c914f | 2008-09-11 00:01:58 +0200 | [diff] [blame] | 43 | case NL80211_IFTYPE_ADHOC: |
| 44 | case NL80211_IFTYPE_AP: |
Andrey Yurovsky | a07dbea | 2008-12-20 10:55:34 +0100 | [diff] [blame] | 45 | case NL80211_IFTYPE_MESH_POINT: |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame] | 46 | conf.sync = TSF_SYNC_BEACON; |
Ivo van Doorn | feb2469 | 2007-10-06 14:14:29 +0200 | [diff] [blame] | 47 | break; |
Johannes Berg | 05c914f | 2008-09-11 00:01:58 +0200 | [diff] [blame] | 48 | case NL80211_IFTYPE_STATION: |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame] | 49 | conf.sync = TSF_SYNC_INFRA; |
Ivo van Doorn | feb2469 | 2007-10-06 14:14:29 +0200 | [diff] [blame] | 50 | break; |
| 51 | default: |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame] | 52 | conf.sync = TSF_SYNC_NONE; |
Ivo van Doorn | feb2469 | 2007-10-06 14:14:29 +0200 | [diff] [blame] | 53 | break; |
| 54 | } |
| 55 | |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame] | 56 | /* |
| 57 | * Note that when NULL is passed as address we will send |
| 58 | * 00:00:00:00:00 to the device to clear the address. |
| 59 | * This will prevent the device being confused when it wants |
| 60 | * to ACK frames or consideres itself associated. |
| 61 | */ |
| 62 | memset(&conf.mac, 0, sizeof(conf.mac)); |
| 63 | if (mac) |
| 64 | memcpy(&conf.mac, mac, ETH_ALEN); |
| 65 | |
| 66 | memset(&conf.bssid, 0, sizeof(conf.bssid)); |
| 67 | if (bssid) |
| 68 | memcpy(&conf.bssid, bssid, ETH_ALEN); |
| 69 | |
| 70 | flags |= CONFIG_UPDATE_TYPE; |
| 71 | if (mac || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count)) |
| 72 | flags |= CONFIG_UPDATE_MAC; |
| 73 | if (bssid || (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count)) |
| 74 | flags |= CONFIG_UPDATE_BSSID; |
| 75 | |
| 76 | rt2x00dev->ops->lib->config_intf(rt2x00dev, intf, &conf, flags); |
| 77 | } |
| 78 | |
Ivo van Doorn | 7281037 | 2008-03-09 22:46:18 +0100 | [diff] [blame] | 79 | void rt2x00lib_config_erp(struct rt2x00_dev *rt2x00dev, |
| 80 | struct rt2x00_intf *intf, |
| 81 | struct ieee80211_bss_conf *bss_conf) |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame] | 82 | { |
Ivo van Doorn | 7281037 | 2008-03-09 22:46:18 +0100 | [diff] [blame] | 83 | struct rt2x00lib_erp erp; |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame] | 84 | |
Ivo van Doorn | 7281037 | 2008-03-09 22:46:18 +0100 | [diff] [blame] | 85 | memset(&erp, 0, sizeof(erp)); |
| 86 | |
| 87 | erp.short_preamble = bss_conf->use_short_preamble; |
Ivo van Doorn | e360c4c | 2008-07-09 15:12:06 +0200 | [diff] [blame] | 88 | erp.cts_protection = bss_conf->use_cts_prot; |
| 89 | |
Ivo van Doorn | e4ea1c4 | 2008-10-29 17:17:57 +0100 | [diff] [blame] | 90 | erp.slot_time = bss_conf->use_short_slot ? SHORT_SLOT_TIME : SLOT_TIME; |
| 91 | erp.sifs = SIFS; |
| 92 | erp.pifs = bss_conf->use_short_slot ? SHORT_PIFS : PIFS; |
| 93 | erp.difs = bss_conf->use_short_slot ? SHORT_DIFS : DIFS; |
| 94 | erp.eifs = bss_conf->use_short_slot ? SHORT_EIFS : EIFS; |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame] | 95 | |
Ivo van Doorn | bad1363 | 2008-11-09 20:47:00 +0100 | [diff] [blame] | 96 | erp.ack_timeout = PLCP + erp.difs + GET_DURATION(ACK_SIZE, 10); |
| 97 | erp.ack_consume_time = SIFS + PLCP + GET_DURATION(ACK_SIZE, 10); |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame] | 98 | |
Ivo van Doorn | 7281037 | 2008-03-09 22:46:18 +0100 | [diff] [blame] | 99 | if (bss_conf->use_short_preamble) { |
| 100 | erp.ack_timeout += SHORT_PREAMBLE; |
| 101 | erp.ack_consume_time += SHORT_PREAMBLE; |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame] | 102 | } else { |
Ivo van Doorn | 7281037 | 2008-03-09 22:46:18 +0100 | [diff] [blame] | 103 | erp.ack_timeout += PREAMBLE; |
| 104 | erp.ack_consume_time += PREAMBLE; |
Ivo van Doorn | 6bb40dd | 2008-02-03 15:49:59 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Ivo van Doorn | e4ea1c4 | 2008-10-29 17:17:57 +0100 | [diff] [blame] | 107 | erp.basic_rates = bss_conf->basic_rates; |
| 108 | |
Ivo van Doorn | 3a643d2 | 2008-03-25 14:13:18 +0100 | [diff] [blame] | 109 | rt2x00dev->ops->lib->config_erp(rt2x00dev, &erp); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Ivo van Doorn | 0b927a0 | 2008-12-02 18:20:22 +0100 | [diff] [blame] | 112 | static inline |
| 113 | enum antenna rt2x00lib_config_antenna_check(enum antenna current_ant, |
| 114 | enum antenna default_ant) |
| 115 | { |
| 116 | if (current_ant != ANTENNA_SW_DIVERSITY) |
| 117 | return current_ant; |
| 118 | return (default_ant != ANTENNA_SW_DIVERSITY) ? default_ant : ANTENNA_B; |
| 119 | } |
| 120 | |
Ivo van Doorn | 69f81a2 | 2007-10-13 16:26:36 +0200 | [diff] [blame] | 121 | void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev, |
Ivo van Doorn | 6d64360 | 2008-11-02 00:38:10 +0100 | [diff] [blame] | 122 | struct antenna_setup *ant) |
Ivo van Doorn | 69f81a2 | 2007-10-13 16:26:36 +0200 | [diff] [blame] | 123 | { |
Ivo van Doorn | 0b927a0 | 2008-12-02 18:20:22 +0100 | [diff] [blame] | 124 | struct antenna_setup *def = &rt2x00dev->default_ant; |
| 125 | struct antenna_setup *active = &rt2x00dev->link.ant.active; |
| 126 | |
Ivo van Doorn | 6d64360 | 2008-11-02 00:38:10 +0100 | [diff] [blame] | 127 | /* |
| 128 | * Failsafe: Make sure we are not sending the |
| 129 | * ANTENNA_SW_DIVERSITY state to the driver. |
| 130 | * If that happes fallback to hardware default, |
| 131 | * or our own default. |
Ivo van Doorn | 0b927a0 | 2008-12-02 18:20:22 +0100 | [diff] [blame] | 132 | * The calls to rt2x00lib_config_antenna_check() |
| 133 | * might have caused that we restore back to the already |
| 134 | * active setting. If that has happened we can quit. |
Ivo van Doorn | 6d64360 | 2008-11-02 00:38:10 +0100 | [diff] [blame] | 135 | */ |
Ivo van Doorn | 0b927a0 | 2008-12-02 18:20:22 +0100 | [diff] [blame] | 136 | ant->rx = rt2x00lib_config_antenna_check(ant->rx, def->rx); |
| 137 | ant->tx = rt2x00lib_config_antenna_check(ant->tx, def->tx); |
Ivo van Doorn | 69f81a2 | 2007-10-13 16:26:36 +0200 | [diff] [blame] | 138 | |
Ivo van Doorn | 0b927a0 | 2008-12-02 18:20:22 +0100 | [diff] [blame] | 139 | if (ant->rx == active->rx && ant->tx == active->tx) |
Ivo van Doorn | 05253c9 | 2008-02-25 23:15:08 +0100 | [diff] [blame] | 140 | return; |
| 141 | |
Ivo van Doorn | 69f81a2 | 2007-10-13 16:26:36 +0200 | [diff] [blame] | 142 | /* |
Ivo van Doorn | e25c4bb | 2007-10-27 13:39:28 +0200 | [diff] [blame] | 143 | * Antenna setup changes require the RX to be disabled, |
| 144 | * else the changes will be ignored by the device. |
| 145 | */ |
Ivo van Doorn | 0262ab0 | 2008-08-29 21:04:26 +0200 | [diff] [blame] | 146 | if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags)) |
Ivo van Doorn | 61667d8 | 2008-02-25 23:15:05 +0100 | [diff] [blame] | 147 | rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF_LINK); |
Ivo van Doorn | e25c4bb | 2007-10-27 13:39:28 +0200 | [diff] [blame] | 148 | |
| 149 | /* |
Ivo van Doorn | 69f81a2 | 2007-10-13 16:26:36 +0200 | [diff] [blame] | 150 | * Write new antenna setup to device and reset the link tuner. |
| 151 | * The latter is required since we need to recalibrate the |
| 152 | * noise-sensitivity ratio for the new setup. |
| 153 | */ |
Ivo van Doorn | 6d64360 | 2008-11-02 00:38:10 +0100 | [diff] [blame] | 154 | rt2x00dev->ops->lib->config_ant(rt2x00dev, ant); |
Ivo van Doorn | e4ea1c4 | 2008-10-29 17:17:57 +0100 | [diff] [blame] | 155 | |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 156 | rt2x00link_reset_tuner(rt2x00dev, true); |
Ivo van Doorn | 69f81a2 | 2007-10-13 16:26:36 +0200 | [diff] [blame] | 157 | |
Ivo van Doorn | 0b927a0 | 2008-12-02 18:20:22 +0100 | [diff] [blame] | 158 | memcpy(active, ant, sizeof(*ant)); |
Ivo van Doorn | e25c4bb | 2007-10-27 13:39:28 +0200 | [diff] [blame] | 159 | |
Ivo van Doorn | 0262ab0 | 2008-08-29 21:04:26 +0200 | [diff] [blame] | 160 | if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags)) |
Ivo van Doorn | 61667d8 | 2008-02-25 23:15:05 +0100 | [diff] [blame] | 161 | rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON_LINK); |
Ivo van Doorn | 69f81a2 | 2007-10-13 16:26:36 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 164 | void rt2x00lib_config(struct rt2x00_dev *rt2x00dev, |
Ivo van Doorn | e4ea1c4 | 2008-10-29 17:17:57 +0100 | [diff] [blame] | 165 | struct ieee80211_conf *conf, |
| 166 | unsigned int ieee80211_flags) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 167 | { |
Ivo van Doorn | 5c58ee5 | 2007-10-06 13:34:52 +0200 | [diff] [blame] | 168 | struct rt2x00lib_conf libconf; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 169 | |
Ivo van Doorn | 5c58ee5 | 2007-10-06 13:34:52 +0200 | [diff] [blame] | 170 | memset(&libconf, 0, sizeof(libconf)); |
| 171 | |
Ivo van Doorn | e4ea1c4 | 2008-10-29 17:17:57 +0100 | [diff] [blame] | 172 | libconf.conf = conf; |
Ivo van Doorn | f5507ce | 2008-02-03 15:51:13 +0100 | [diff] [blame] | 173 | |
Ivo van Doorn | e4ea1c4 | 2008-10-29 17:17:57 +0100 | [diff] [blame] | 174 | if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL) { |
Ivo van Doorn | 5c58ee5 | 2007-10-06 13:34:52 +0200 | [diff] [blame] | 175 | memcpy(&libconf.rf, |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 176 | &rt2x00dev->spec.channels[conf->channel->hw_value], |
Ivo van Doorn | 5c58ee5 | 2007-10-06 13:34:52 +0200 | [diff] [blame] | 177 | sizeof(libconf.rf)); |
Ivo van Doorn | 8c5e7a5 | 2008-08-04 16:38:47 +0200 | [diff] [blame] | 178 | |
| 179 | memcpy(&libconf.channel, |
| 180 | &rt2x00dev->spec.channels_info[conf->channel->hw_value], |
| 181 | sizeof(libconf.channel)); |
Ivo van Doorn | 5c58ee5 | 2007-10-06 13:34:52 +0200 | [diff] [blame] | 182 | } |
| 183 | |
Ivo van Doorn | 5c58ee5 | 2007-10-06 13:34:52 +0200 | [diff] [blame] | 184 | /* |
| 185 | * Start configuration. |
| 186 | */ |
Ivo van Doorn | e4ea1c4 | 2008-10-29 17:17:57 +0100 | [diff] [blame] | 187 | rt2x00dev->ops->lib->config(rt2x00dev, &libconf, ieee80211_flags); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 188 | |
| 189 | /* |
| 190 | * Some configuration changes affect the link quality |
| 191 | * which means we need to reset the link tuner. |
| 192 | */ |
Ivo van Doorn | e4ea1c4 | 2008-10-29 17:17:57 +0100 | [diff] [blame] | 193 | if (ieee80211_flags & IEEE80211_CONF_CHANGE_CHANNEL) |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 194 | rt2x00link_reset_tuner(rt2x00dev, false); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 195 | |
Ivo van Doorn | e4ea1c4 | 2008-10-29 17:17:57 +0100 | [diff] [blame] | 196 | rt2x00dev->curr_band = conf->channel->band; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 197 | rt2x00dev->tx_power = conf->power_level; |
Ivo van Doorn | 42c8285 | 2008-12-02 18:20:04 +0100 | [diff] [blame] | 198 | rt2x00dev->short_retry = conf->short_frame_max_tx_count; |
| 199 | rt2x00dev->long_retry = conf->long_frame_max_tx_count; |
Mattias Nissler | 16b1951 | 2007-10-13 16:26:57 +0200 | [diff] [blame] | 200 | |
Ivo van Doorn | e4ea1c4 | 2008-10-29 17:17:57 +0100 | [diff] [blame] | 201 | rt2x00dev->rx_status.band = conf->channel->band; |
| 202 | rt2x00dev->rx_status.freq = conf->channel->center_freq; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 203 | } |