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