1 /* $OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $ */ 2 3 /*- 4 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr> 5 * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org> 6 * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 #include <sys/cdefs.h> 22 #include "opt_wlan.h" 23 24 #include <sys/param.h> 25 #include <sys/lock.h> 26 #include <sys/mutex.h> 27 #include <sys/mbuf.h> 28 #include <sys/kernel.h> 29 #include <sys/socket.h> 30 #include <sys/systm.h> 31 #include <sys/malloc.h> 32 #include <sys/queue.h> 33 #include <sys/taskqueue.h> 34 #include <sys/bus.h> 35 #include <sys/endian.h> 36 #include <sys/linker.h> 37 38 #include <net/if.h> 39 #include <net/ethernet.h> 40 #include <net/if_media.h> 41 42 #include <net80211/ieee80211_var.h> 43 #include <net80211/ieee80211_radiotap.h> 44 45 #include <dev/rtwn/if_rtwnreg.h> 46 #include <dev/rtwn/if_rtwnvar.h> 47 48 #include <dev/rtwn/if_rtwn_debug.h> 49 #include <dev/rtwn/if_rtwn_ridx.h> 50 51 #include <dev/rtwn/rtl8192c/r92c_var.h> 52 53 #include <dev/rtwn/rtl8188e/r88e.h> 54 #include <dev/rtwn/rtl8188e/r88e_priv.h> 55 #include <dev/rtwn/rtl8188e/r88e_reg.h> 56 57 static int 58 r88e_get_power_group(struct rtwn_softc *sc, struct ieee80211_channel *c) 59 { 60 uint8_t chan; 61 int group; 62 63 chan = rtwn_chan2centieee(c); 64 if (IEEE80211_IS_CHAN_2GHZ(c)) { 65 if (chan <= 2) group = 0; 66 else if (chan <= 5) group = 1; 67 else if (chan <= 8) group = 2; 68 else if (chan <= 11) group = 3; 69 else if (chan <= 13) group = 4; 70 else if (chan <= 14) group = 5; 71 else { 72 KASSERT(0, ("wrong 2GHz channel %d!\n", chan)); 73 return (-1); 74 } 75 } else { 76 KASSERT(0, ("wrong channel band (flags %08X)\n", c->ic_flags)); 77 return (-1); 78 } 79 80 return (group); 81 } 82 83 void 84 r88e_get_txpower(struct rtwn_softc *sc, int chain, 85 struct ieee80211_channel *c, uint8_t power[RTWN_RIDX_COUNT]) 86 { 87 const struct ieee80211com *ic = &sc->sc_ic; 88 struct r92c_softc *rs = sc->sc_priv; 89 const struct rtwn_r88e_txpwr *rt = rs->rs_txpwr; 90 uint8_t cckpow, ofdmpow, bw20pow, htpow = 0; 91 int max_mcs, ridx, group; 92 93 /* Determine channel group. */ 94 group = r88e_get_power_group(sc, c); 95 if (group == -1) { /* shouldn't happen */ 96 device_printf(sc->sc_dev, "%s: incorrect channel\n", __func__); 97 return; 98 } 99 100 /* 101 * Treat the entries in 1/2 dBm resolution where 0 = 0dBm. 102 * Apply the adjustments afterwards; assume that the vendor 103 * driver is applying offsets to make up for the actual 104 * target power in dBm. 105 */ 106 107 max_mcs = RTWN_RIDX_HT_MCS(sc->ntxchains * 8 - 1); 108 KASSERT(max_mcs <= RTWN_RIDX_LEGACY_HT_COUNT, ("increase ridx limit\n")); 109 110 /* Compute per-CCK rate Tx power. */ 111 for (ridx = RTWN_RIDX_CCK1; ridx <= RTWN_RIDX_CCK11; ridx++) { 112 /* 113 * Note: the regulatory limit is applied to cckpow before 114 * it's subtracted for CCK2. 115 */ 116 cckpow = rt->cck_tx_pwr[group]; 117 if (cckpow > ic->ic_txpowlimit) 118 cckpow = ic->ic_txpowlimit; 119 120 /* 121 * If it's CCK2 then we subtract the 9 (4.5dB?) offset 122 * and make sure we aren't going to underflow. 123 */ 124 if (ridx == RTWN_RIDX_CCK2 && cckpow < 9) 125 cckpow = 0; 126 else if (ridx == RTWN_RIDX_CCK2) 127 cckpow = cckpow - 9; 128 129 power[ridx] = cckpow; 130 } 131 132 if (group < 5) 133 htpow = rt->ht40_tx_pwr[group]; 134 135 /* Compute per-OFDM rate Tx power. */ 136 ofdmpow = htpow + rt->ofdm_tx_pwr_diff; 137 if (ofdmpow > ic->ic_txpowlimit) 138 ofdmpow = ic->ic_txpowlimit; 139 for (ridx = RTWN_RIDX_OFDM6; ridx <= RTWN_RIDX_OFDM54; ridx++) 140 power[ridx] = ofdmpow; 141 142 bw20pow = htpow + rt->bw20_tx_pwr_diff; 143 if (bw20pow > ic->ic_txpowlimit) 144 bw20pow = ic->ic_txpowlimit; 145 for (ridx = RTWN_RIDX_HT_MCS(0); ridx <= max_mcs; ridx++) 146 power[ridx] = bw20pow; 147 148 /* Apply max limit */ 149 for (ridx = RTWN_RIDX_CCK1; ridx <= max_mcs; ridx++) { 150 if (power[ridx] > R92C_MAX_TX_PWR) 151 power[ridx] = R92C_MAX_TX_PWR; 152 } 153 } 154 155 void 156 r88e_set_bw20(struct rtwn_softc *sc, uint8_t chan) 157 { 158 struct r92c_softc *rs = sc->sc_priv; 159 160 rtwn_setbits_1(sc, R92C_BWOPMODE, 0, R92C_BWOPMODE_20MHZ); 161 162 rtwn_bb_setbits(sc, R92C_FPGA0_RFMOD, R92C_RFMOD_40MHZ, 0); 163 rtwn_bb_setbits(sc, R92C_FPGA1_RFMOD, R92C_RFMOD_40MHZ, 0); 164 165 /* Select 20MHz bandwidth. */ 166 rtwn_rf_write(sc, 0, R92C_RF_CHNLBW, 167 (rs->rf_chnlbw[0] & ~0xfff) | chan | R88E_RF_CHNLBW_BW20); 168 } 169 170 void 171 r88e_set_gain(struct rtwn_softc *sc, uint8_t gain) 172 { 173 174 rtwn_bb_setbits(sc, R92C_OFDM0_AGCCORE1(0), 175 R92C_OFDM0_AGCCORE1_GAIN_M, gain); 176 } 177