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 struct r92c_softc *rs = sc->sc_priv; 88 const struct rtwn_r88e_txpwr *rt = rs->rs_txpwr; 89 uint8_t cckpow, ofdmpow, bw20pow, htpow = 0; 90 int max_mcs, ridx, group; 91 92 /* Determine channel group. */ 93 group = r88e_get_power_group(sc, c); 94 if (group == -1) { /* shouldn't happen */ 95 device_printf(sc->sc_dev, "%s: incorrect channel\n", __func__); 96 return; 97 } 98 99 /* XXX net80211 regulatory */ 100 101 max_mcs = RTWN_RIDX_HT_MCS(sc->ntxchains * 8 - 1); 102 KASSERT(max_mcs <= RTWN_RIDX_COUNT, ("increase ridx limit\n")); 103 104 /* Compute per-CCK rate Tx power. */ 105 cckpow = rt->cck_tx_pwr[group]; 106 for (ridx = RTWN_RIDX_CCK1; ridx <= RTWN_RIDX_CCK11; ridx++) { 107 power[ridx] = (ridx == RTWN_RIDX_CCK2) ? cckpow - 9 : cckpow; 108 } 109 110 if (group < 5) 111 htpow = rt->ht40_tx_pwr[group]; 112 113 /* Compute per-OFDM rate Tx power. */ 114 ofdmpow = htpow + rt->ofdm_tx_pwr_diff; 115 for (ridx = RTWN_RIDX_OFDM6; ridx <= RTWN_RIDX_OFDM54; ridx++) 116 power[ridx] = ofdmpow; 117 118 bw20pow = htpow + rt->bw20_tx_pwr_diff; 119 for (ridx = RTWN_RIDX_HT_MCS(0); ridx <= max_mcs; ridx++) 120 power[ridx] = bw20pow; 121 122 /* Apply max limit. */ 123 for (ridx = RTWN_RIDX_CCK1; ridx <= max_mcs; ridx++) { 124 if (power[ridx] > R92C_MAX_TX_PWR) 125 power[ridx] = R92C_MAX_TX_PWR; 126 } 127 } 128 129 void 130 r88e_set_bw20(struct rtwn_softc *sc, uint8_t chan) 131 { 132 struct r92c_softc *rs = sc->sc_priv; 133 134 rtwn_setbits_1(sc, R92C_BWOPMODE, 0, R92C_BWOPMODE_20MHZ); 135 136 rtwn_bb_setbits(sc, R92C_FPGA0_RFMOD, R92C_RFMOD_40MHZ, 0); 137 rtwn_bb_setbits(sc, R92C_FPGA1_RFMOD, R92C_RFMOD_40MHZ, 0); 138 139 /* Select 20MHz bandwidth. */ 140 rtwn_rf_write(sc, 0, R92C_RF_CHNLBW, 141 (rs->rf_chnlbw[0] & ~0xfff) | chan | R88E_RF_CHNLBW_BW20); 142 } 143 144 void 145 r88e_set_gain(struct rtwn_softc *sc, uint8_t gain) 146 { 147 148 rtwn_bb_setbits(sc, R92C_OFDM0_AGCCORE1(0), 149 R92C_OFDM0_AGCCORE1_GAIN_M, gain); 150 } 151