xref: /freebsd/sys/dev/rtwn/rtl8188e/r88e_chan.c (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
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 static int
60 r88e_get_power_group(struct rtwn_softc *sc, struct ieee80211_channel *c)
61 {
62 	uint8_t chan;
63 	int group;
64 
65 	chan = rtwn_chan2centieee(c);
66 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
67 		if (chan <= 2)			group = 0;
68 		else if (chan <= 5)		group = 1;
69 		else if (chan <= 8)		group = 2;
70 		else if (chan <= 11)		group = 3;
71 		else if (chan <= 13)		group = 4;
72 		else if (chan <= 14)		group = 5;
73 		else {
74 			KASSERT(0, ("wrong 2GHz channel %d!\n", chan));
75 			return (-1);
76 		}
77 	} else {
78 		KASSERT(0, ("wrong channel band (flags %08X)\n", c->ic_flags));
79 		return (-1);
80 	}
81 
82 	return (group);
83 }
84 
85 void
86 r88e_get_txpower(struct rtwn_softc *sc, int chain,
87     struct ieee80211_channel *c, uint8_t power[RTWN_RIDX_COUNT])
88 {
89 	struct r92c_softc *rs = sc->sc_priv;
90 	const struct rtwn_r88e_txpwr *rt = rs->rs_txpwr;
91 	uint8_t cckpow, ofdmpow, bw20pow, htpow = 0;
92 	int max_mcs, ridx, group;
93 
94 	/* Determine channel group. */
95 	group = r88e_get_power_group(sc, c);
96 	if (group == -1) {	/* shouldn't happen */
97 		device_printf(sc->sc_dev, "%s: incorrect channel\n", __func__);
98 		return;
99 	}
100 
101 	/* XXX net80211 regulatory */
102 
103 	max_mcs = RTWN_RIDX_HT_MCS(sc->ntxchains * 8 - 1);
104 	KASSERT(max_mcs <= RTWN_RIDX_COUNT, ("increase ridx limit\n"));
105 
106 	/* Compute per-CCK rate Tx power. */
107 	cckpow = rt->cck_tx_pwr[group];
108 	for (ridx = RTWN_RIDX_CCK1; ridx <= RTWN_RIDX_CCK11; ridx++) {
109 		power[ridx] = (ridx == RTWN_RIDX_CCK2) ? cckpow - 9 : cckpow;
110 	}
111 
112 	if (group < 5)
113 		htpow = rt->ht40_tx_pwr[group];
114 
115 	/* Compute per-OFDM rate Tx power. */
116 	ofdmpow = htpow + rt->ofdm_tx_pwr_diff;
117 	for (ridx = RTWN_RIDX_OFDM6; ridx <= RTWN_RIDX_OFDM54; ridx++)
118 		power[ridx] = ofdmpow;
119 
120 	bw20pow = htpow + rt->bw20_tx_pwr_diff;
121 	for (ridx = RTWN_RIDX_HT_MCS(0); ridx <= max_mcs; ridx++)
122 		power[ridx] = bw20pow;
123 
124 	/* Apply max limit. */
125 	for (ridx = RTWN_RIDX_CCK1; ridx <= max_mcs; ridx++) {
126 		if (power[ridx] > R92C_MAX_TX_PWR)
127 			power[ridx] = R92C_MAX_TX_PWR;
128 	}
129 }
130 
131 void
132 r88e_set_bw20(struct rtwn_softc *sc, uint8_t chan)
133 {
134 	struct r92c_softc *rs = sc->sc_priv;
135 
136 	rtwn_setbits_1(sc, R92C_BWOPMODE, 0, R92C_BWOPMODE_20MHZ);
137 
138 	rtwn_bb_setbits(sc, R92C_FPGA0_RFMOD, R92C_RFMOD_40MHZ, 0);
139 	rtwn_bb_setbits(sc, R92C_FPGA1_RFMOD, R92C_RFMOD_40MHZ, 0);
140 
141 	/* Select 20MHz bandwidth. */
142 	rtwn_rf_write(sc, 0, R92C_RF_CHNLBW,
143 	    (rs->rf_chnlbw[0] & ~0xfff) | chan | R88E_RF_CHNLBW_BW20);
144 }
145 
146 void
147 r88e_set_gain(struct rtwn_softc *sc, uint8_t gain)
148 {
149 
150 	rtwn_bb_setbits(sc, R92C_OFDM0_AGCCORE1(0),
151 	    R92C_OFDM0_AGCCORE1_GAIN_M, gain);
152 }
153