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 __FBSDID("$FreeBSD$"); 23 24 #include "opt_wlan.h" 25 26 #include <sys/param.h> 27 #include <sys/lock.h> 28 #include <sys/mutex.h> 29 #include <sys/mbuf.h> 30 #include <sys/kernel.h> 31 #include <sys/socket.h> 32 #include <sys/systm.h> 33 #include <sys/malloc.h> 34 #include <sys/queue.h> 35 #include <sys/taskqueue.h> 36 #include <sys/bus.h> 37 #include <sys/endian.h> 38 #include <sys/linker.h> 39 40 #include <net/if.h> 41 #include <net/ethernet.h> 42 #include <net/if_media.h> 43 44 #include <net80211/ieee80211_var.h> 45 #include <net80211/ieee80211_radiotap.h> 46 47 #include <dev/rtwn/if_rtwnreg.h> 48 #include <dev/rtwn/if_rtwnvar.h> 49 50 #include <dev/rtwn/if_rtwn_debug.h> 51 #include <dev/rtwn/if_rtwn_ridx.h> 52 53 #include <dev/rtwn/rtl8192c/r92c_var.h> 54 55 #include <dev/rtwn/rtl8188e/r88e.h> 56 #include <dev/rtwn/rtl8188e/r88e_priv.h> 57 #include <dev/rtwn/rtl8188e/r88e_reg.h> 58 59 60 static int 61 r88e_get_power_group(struct rtwn_softc *sc, struct ieee80211_channel *c) 62 { 63 uint8_t chan; 64 int group; 65 66 chan = rtwn_chan2centieee(c); 67 if (IEEE80211_IS_CHAN_2GHZ(c)) { 68 if (chan <= 2) group = 0; 69 else if (chan <= 5) group = 1; 70 else if (chan <= 8) group = 2; 71 else if (chan <= 11) group = 3; 72 else if (chan <= 13) group = 4; 73 else if (chan <= 14) group = 5; 74 else { 75 KASSERT(0, ("wrong 2GHz channel %d!\n", chan)); 76 return (-1); 77 } 78 } else { 79 KASSERT(0, ("wrong channel band (flags %08X)\n", c->ic_flags)); 80 return (-1); 81 } 82 83 return (group); 84 } 85 86 void 87 r88e_get_txpower(struct rtwn_softc *sc, int chain, 88 struct ieee80211_channel *c, uint16_t power[RTWN_RIDX_COUNT]) 89 { 90 struct r92c_softc *rs = sc->sc_priv; 91 const struct rtwn_r88e_txpwr *rt = rs->rs_txpwr; 92 const struct rtwn_r88e_txagc *base = rs->rs_txagc; 93 uint16_t cckpow, ofdmpow, bw20pow, htpow; 94 int max_mcs, ridx, group; 95 96 /* Determine channel group. */ 97 group = r88e_get_power_group(sc, c); 98 if (group == -1) { /* shouldn't happen */ 99 device_printf(sc->sc_dev, "%s: incorrect channel\n", __func__); 100 return; 101 } 102 103 /* XXX net80211 regulatory */ 104 105 max_mcs = RTWN_RIDX_MCS(sc->ntxchains * 8 - 1); 106 KASSERT(max_mcs <= RTWN_RIDX_COUNT, ("increase ridx limit\n")); 107 108 memset(power, 0, max_mcs * sizeof(power[0])); 109 if (rs->regulatory == 0) { 110 for (ridx = RTWN_RIDX_CCK1; ridx <= RTWN_RIDX_CCK11; ridx++) 111 power[ridx] = base->pwr[0][ridx]; 112 } 113 for (ridx = RTWN_RIDX_OFDM6; ridx < RTWN_RIDX_COUNT; ridx++) { 114 if (rs->regulatory == 3) 115 power[ridx] = base->pwr[0][ridx]; 116 else if (rs->regulatory == 1) { 117 if (!IEEE80211_IS_CHAN_HT40(c)) 118 power[ridx] = base->pwr[group][ridx]; 119 } else if (rs->regulatory != 2) 120 power[ridx] = base->pwr[0][ridx]; 121 } 122 123 /* Compute per-CCK rate Tx power. */ 124 cckpow = rt->cck_tx_pwr[group]; 125 for (ridx = RTWN_RIDX_CCK1; ridx <= RTWN_RIDX_CCK11; ridx++) 126 power[ridx] += cckpow; 127 128 htpow = rt->ht40_tx_pwr[group]; 129 130 /* Compute per-OFDM rate Tx power. */ 131 ofdmpow = htpow + rt->ofdm_tx_pwr_diff; 132 for (ridx = RTWN_RIDX_OFDM6; ridx <= RTWN_RIDX_OFDM54; ridx++) 133 power[ridx] += ofdmpow; 134 135 bw20pow = htpow + rt->bw20_tx_pwr_diff; 136 for (ridx = RTWN_RIDX_MCS(0); ridx <= max_mcs; ridx++) 137 power[ridx] += bw20pow; 138 139 /* Apply max limit. */ 140 for (ridx = RTWN_RIDX_CCK1; ridx <= max_mcs; ridx++) { 141 if (power[ridx] > R92C_MAX_TX_PWR) 142 power[ridx] = R92C_MAX_TX_PWR; 143 } 144 } 145 146 void 147 r88e_set_bw20(struct rtwn_softc *sc, uint8_t chan) 148 { 149 struct r92c_softc *rs = sc->sc_priv; 150 151 rtwn_setbits_1(sc, R92C_BWOPMODE, 0, R92C_BWOPMODE_20MHZ); 152 153 rtwn_bb_setbits(sc, R92C_FPGA0_RFMOD, R92C_RFMOD_40MHZ, 0); 154 rtwn_bb_setbits(sc, R92C_FPGA1_RFMOD, R92C_RFMOD_40MHZ, 0); 155 156 /* Select 20MHz bandwidth. */ 157 rtwn_rf_write(sc, 0, R92C_RF_CHNLBW, 158 (rs->rf_chnlbw[0] & ~0xfff) | chan | R88E_RF_CHNLBW_BW20); 159 } 160 161 void 162 r88e_set_gain(struct rtwn_softc *sc, uint8_t gain) 163 { 164 165 rtwn_bb_setbits(sc, R92C_OFDM0_AGCCORE1(0), 166 R92C_OFDM0_AGCCORE1_GAIN_M, gain); 167 } 168