1 /******************************************************************************
5 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28 *****************************************************************************/
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/sched.h>
40 int iwlagn_send_tx_ant_config(struct iwl_priv *priv, u8 valid_tx_ant)
42 struct iwl_tx_ant_config_cmd tx_ant_cmd = {
43 .valid = cpu_to_le32(valid_tx_ant),
46 if (IWL_UCODE_API(priv->ucode_ver) > 1) {
47 IWL_DEBUG_HC(priv, "select valid tx ant: %u\n", valid_tx_ant);
48 return iwl_send_cmd_pdu(priv, TX_ANT_CONFIGURATION_CMD,
49 sizeof(struct iwl_tx_ant_config_cmd),
52 IWL_DEBUG_HC(priv, "TX_ANT_CONFIGURATION_CMD not supported\n");
57 static u16 iwlagn_build_addsta_hcmd(const struct iwl_addsta_cmd *cmd, u8 *data)
59 u16 size = (u16)sizeof(struct iwl_addsta_cmd);
60 struct iwl_addsta_cmd *addsta = (struct iwl_addsta_cmd *)data;
61 memcpy(addsta, cmd, size);
63 addsta->rate_n_flags = cpu_to_le16(0);
67 static void iwlagn_gain_computation(struct iwl_priv *priv,
68 u32 average_noise[NUM_RX_CHAINS],
69 u16 min_average_noise_antenna_i,
70 u32 min_average_noise,
75 struct iwl_chain_noise_data *data = &priv->chain_noise_data;
78 * Find Gain Code for the chains based on "default chain"
80 for (i = default_chain + 1; i < NUM_RX_CHAINS; i++) {
81 if ((data->disconn_array[i])) {
82 data->delta_gain_code[i] = 0;
86 delta_g = (priv->cfg->base_params->chain_noise_scale *
87 ((s32)average_noise[default_chain] -
88 (s32)average_noise[i])) / 1500;
90 /* bound gain by 2 bits value max, 3rd bit is sign */
91 data->delta_gain_code[i] =
92 min(abs(delta_g), (long) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
96 * set negative sign ...
97 * note to Intel developers: This is uCode API format,
98 * not the format of any internal device registers.
99 * Do not change this format for e.g. 6050 or similar
100 * devices. Change format only if more resolution
101 * (i.e. more than 2 bits magnitude) is needed.
103 data->delta_gain_code[i] |= (1 << 2);
106 IWL_DEBUG_CALIB(priv, "Delta gains: ANT_B = %d ANT_C = %d\n",
107 data->delta_gain_code[1], data->delta_gain_code[2]);
109 if (!data->radio_write) {
110 struct iwl_calib_chain_noise_gain_cmd cmd;
112 memset(&cmd, 0, sizeof(cmd));
114 cmd.hdr.op_code = priv->_agn.phy_calib_chain_noise_gain_cmd;
115 cmd.hdr.first_group = 0;
116 cmd.hdr.groups_num = 1;
117 cmd.hdr.data_valid = 1;
118 cmd.delta_gain_1 = data->delta_gain_code[1];
119 cmd.delta_gain_2 = data->delta_gain_code[2];
120 iwl_send_cmd_pdu_async(priv, REPLY_PHY_CALIBRATION_CMD,
121 sizeof(cmd), &cmd, NULL);
123 data->radio_write = 1;
124 data->state = IWL_CHAIN_NOISE_CALIBRATED;
128 static void iwlagn_chain_noise_reset(struct iwl_priv *priv)
130 struct iwl_chain_noise_data *data = &priv->chain_noise_data;
133 if ((data->state == IWL_CHAIN_NOISE_ALIVE) &&
134 iwl_is_any_associated(priv)) {
135 struct iwl_calib_chain_noise_reset_cmd cmd;
137 /* clear data for chain noise calibration algorithm */
138 data->chain_noise_a = 0;
139 data->chain_noise_b = 0;
140 data->chain_noise_c = 0;
141 data->chain_signal_a = 0;
142 data->chain_signal_b = 0;
143 data->chain_signal_c = 0;
144 data->beacon_count = 0;
146 memset(&cmd, 0, sizeof(cmd));
147 cmd.hdr.op_code = priv->_agn.phy_calib_chain_noise_reset_cmd;
148 cmd.hdr.first_group = 0;
149 cmd.hdr.groups_num = 1;
150 cmd.hdr.data_valid = 1;
151 ret = iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
155 "Could not send REPLY_PHY_CALIBRATION_CMD\n");
156 data->state = IWL_CHAIN_NOISE_ACCUMULATE;
157 IWL_DEBUG_CALIB(priv, "Run chain_noise_calibrate\n");
161 static void iwlagn_tx_cmd_protection(struct iwl_priv *priv,
162 struct ieee80211_tx_info *info,
163 __le16 fc, __le32 *tx_flags)
165 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS ||
166 info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
167 *tx_flags |= TX_CMD_FLG_PROT_REQUIRE_MSK;
171 if (priv->cfg->ht_params &&
172 priv->cfg->ht_params->use_rts_for_aggregation &&
173 info->flags & IEEE80211_TX_CTL_AMPDU) {
174 *tx_flags |= TX_CMD_FLG_PROT_REQUIRE_MSK;
179 /* Calc max signal level (dBm) among 3 possible receivers */
180 static int iwlagn_calc_rssi(struct iwl_priv *priv,
181 struct iwl_rx_phy_res *rx_resp)
183 /* data from PHY/DSP regarding signal strength, etc.,
184 * contents are always there, not configurable by host
186 struct iwlagn_non_cfg_phy *ncphy =
187 (struct iwlagn_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
188 u32 val, rssi_a, rssi_b, rssi_c, max_rssi;
191 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_AGC_IDX]);
192 agc = (val & IWLAGN_OFDM_AGC_MSK) >> IWLAGN_OFDM_AGC_BIT_POS;
194 /* Find max rssi among 3 possible receivers.
195 * These values are measured by the digital signal processor (DSP).
196 * They should stay fairly constant even as the signal strength varies,
197 * if the radio's automatic gain control (AGC) is working right.
198 * AGC value (see below) will provide the "interesting" info.
200 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_AB_IDX]);
201 rssi_a = (val & IWLAGN_OFDM_RSSI_INBAND_A_BITMSK) >>
202 IWLAGN_OFDM_RSSI_A_BIT_POS;
203 rssi_b = (val & IWLAGN_OFDM_RSSI_INBAND_B_BITMSK) >>
204 IWLAGN_OFDM_RSSI_B_BIT_POS;
205 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_C_IDX]);
206 rssi_c = (val & IWLAGN_OFDM_RSSI_INBAND_C_BITMSK) >>
207 IWLAGN_OFDM_RSSI_C_BIT_POS;
209 max_rssi = max_t(u32, rssi_a, rssi_b);
210 max_rssi = max_t(u32, max_rssi, rssi_c);
212 IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
213 rssi_a, rssi_b, rssi_c, max_rssi, agc);
215 /* dBm = max_rssi dB - agc dB - constant.
216 * Higher AGC (higher radio gain) means lower signal. */
217 return max_rssi - agc - IWLAGN_RSSI_OFFSET;
220 static int iwlagn_set_pan_params(struct iwl_priv *priv)
222 struct iwl_wipan_params_cmd cmd;
223 struct iwl_rxon_context *ctx_bss, *ctx_pan;
224 int slot0 = 300, slot1 = 0;
227 if (priv->valid_contexts == BIT(IWL_RXON_CTX_BSS))
230 BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
232 lockdep_assert_held(&priv->mutex);
234 ctx_bss = &priv->contexts[IWL_RXON_CTX_BSS];
235 ctx_pan = &priv->contexts[IWL_RXON_CTX_PAN];
238 * If the PAN context is inactive, then we don't need
239 * to update the PAN parameters, the last thing we'll
240 * have done before it goes inactive is making the PAN
241 * parameters be WLAN-only.
243 if (!ctx_pan->is_active)
246 memset(&cmd, 0, sizeof(cmd));
248 /* only 2 slots are currently allowed */
251 cmd.slots[0].type = 0; /* BSS */
252 cmd.slots[1].type = 1; /* PAN */
254 if (priv->_agn.hw_roc_channel) {
255 /* both contexts must be used for this to happen */
256 slot1 = priv->_agn.hw_roc_duration;
257 slot0 = IWL_MIN_SLOT_TIME;
258 } else if (ctx_bss->vif && ctx_pan->vif) {
259 int bcnint = ctx_pan->vif->bss_conf.beacon_int;
260 int dtim = ctx_pan->vif->bss_conf.dtim_period ?: 1;
262 /* should be set, but seems unused?? */
263 cmd.flags |= cpu_to_le16(IWL_WIPAN_PARAMS_FLG_SLOTTED_MODE);
265 if (ctx_pan->vif->type == NL80211_IFTYPE_AP &&
267 bcnint != ctx_bss->vif->bss_conf.beacon_int) {
269 "beacon intervals don't match (%d, %d)\n",
270 ctx_bss->vif->bss_conf.beacon_int,
271 ctx_pan->vif->bss_conf.beacon_int);
273 bcnint = max_t(int, bcnint,
274 ctx_bss->vif->bss_conf.beacon_int);
276 bcnint = DEFAULT_BEACON_INTERVAL;
278 slot1 = bcnint - slot0;
280 if (test_bit(STATUS_SCAN_HW, &priv->status) ||
281 (!ctx_bss->vif->bss_conf.idle &&
282 !ctx_bss->vif->bss_conf.assoc)) {
283 slot0 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
284 slot1 = IWL_MIN_SLOT_TIME;
285 } else if (!ctx_pan->vif->bss_conf.idle &&
286 !ctx_pan->vif->bss_conf.assoc) {
287 slot1 = bcnint * 3 - IWL_MIN_SLOT_TIME;
288 slot0 = IWL_MIN_SLOT_TIME;
290 } else if (ctx_pan->vif) {
292 slot1 = max_t(int, 1, ctx_pan->vif->bss_conf.dtim_period) *
293 ctx_pan->vif->bss_conf.beacon_int;
294 slot1 = max_t(int, DEFAULT_BEACON_INTERVAL, slot1);
296 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
297 slot0 = slot1 * 3 - IWL_MIN_SLOT_TIME;
298 slot1 = IWL_MIN_SLOT_TIME;
302 cmd.slots[0].width = cpu_to_le16(slot0);
303 cmd.slots[1].width = cpu_to_le16(slot1);
305 ret = iwl_send_cmd_pdu(priv, REPLY_WIPAN_PARAMS, sizeof(cmd), &cmd);
307 IWL_ERR(priv, "Error setting PAN parameters (%d)\n", ret);
312 struct iwl_hcmd_ops iwlagn_hcmd = {
313 .commit_rxon = iwlagn_commit_rxon,
314 .set_rxon_chain = iwlagn_set_rxon_chain,
315 .set_tx_ant = iwlagn_send_tx_ant_config,
316 .send_bt_config = iwl_send_bt_config,
317 .set_pan_params = iwlagn_set_pan_params,
320 struct iwl_hcmd_ops iwlagn_bt_hcmd = {
321 .commit_rxon = iwlagn_commit_rxon,
322 .set_rxon_chain = iwlagn_set_rxon_chain,
323 .set_tx_ant = iwlagn_send_tx_ant_config,
324 .send_bt_config = iwlagn_send_advance_bt_config,
325 .set_pan_params = iwlagn_set_pan_params,
328 struct iwl_hcmd_utils_ops iwlagn_hcmd_utils = {
329 .build_addsta_hcmd = iwlagn_build_addsta_hcmd,
330 .gain_computation = iwlagn_gain_computation,
331 .chain_noise_reset = iwlagn_chain_noise_reset,
332 .tx_cmd_protection = iwlagn_tx_cmd_protection,
333 .calc_rssi = iwlagn_calc_rssi,
334 .request_scan = iwlagn_request_scan,
335 .post_scan = iwlagn_post_scan,