xref: /freebsd/sys/dev/rtwn/rtl8192c/r92c_rom.c (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
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_efuse.h>
52 
53 #include <dev/rtwn/rtl8192c/r92c.h>
54 #include <dev/rtwn/rtl8192c/r92c_priv.h>
55 #include <dev/rtwn/rtl8192c/r92c_var.h>
56 #include <dev/rtwn/rtl8192c/r92c_rom_image.h>
57 
58 
59 static void
60 r92c_set_chains(struct rtwn_softc *sc)
61 {
62 	struct r92c_softc *rs = sc->sc_priv;
63 
64 	if (rs->chip & R92C_CHIP_92C) {
65 		sc->ntxchains = (rs->chip & R92C_CHIP_92C_1T2R) ? 1 : 2;
66 		sc->nrxchains = 2;
67 	} else {
68 		sc->ntxchains = 1;
69 		sc->nrxchains = 1;
70 	}
71 }
72 
73 void
74 r92c_efuse_postread(struct rtwn_softc *sc)
75 {
76 	struct r92c_softc *rs = sc->sc_priv;
77 
78 	/* XXX Weird but this is what the vendor driver does. */
79 	sc->next_rom_addr = 0x1fa;
80 	(void) rtwn_efuse_read_next(sc, &rs->pa_setting);
81 	RTWN_DPRINTF(sc, RTWN_DEBUG_ROM, "%s: PA setting=0x%x\n", __func__,
82 	    rs->pa_setting);
83 }
84 
85 void
86 r92c_parse_rom(struct rtwn_softc *sc, uint8_t *buf)
87 {
88 	struct r92c_softc *rs = sc->sc_priv;
89 	struct rtwn_r92c_txpwr *rt = rs->rs_txpwr;
90 	struct r92c_rom *rom = (struct r92c_rom *)buf;
91 	int i, j;
92 
93 	rs->board_type = MS(rom->rf_opt1, R92C_ROM_RF1_BOARD_TYPE);
94 	rs->regulatory = MS(rom->rf_opt1, R92C_ROM_RF1_REGULATORY);
95 	RTWN_DPRINTF(sc, RTWN_DEBUG_ROM, "%s: regulatory type=%d\n",
96 	    __func__, rs->regulatory);
97 
98 	/* Need to be set before postinit() (but after preinit()). */
99 	rtwn_r92c_set_name(sc);
100 	r92c_set_chains(sc);
101 
102 	for (j = 0; j < R92C_GROUP_2G; j++) {
103 		for (i = 0; i < sc->ntxchains; i++) {
104 			rt->cck_tx_pwr[i][j] = rom->cck_tx_pwr[i][j];
105 			rt->ht40_1s_tx_pwr[i][j] = rom->ht40_1s_tx_pwr[i][j];
106 		}
107 
108 		rt->ht40_2s_tx_pwr_diff[0][j] =
109 		    MS(rom->ht40_2s_tx_pwr_diff[j], LOW_PART);
110 		rt->ht20_tx_pwr_diff[0][j] =
111 		    RTWN_SIGN4TO8(MS(rom->ht20_tx_pwr_diff[j],
112 			LOW_PART));
113 		rt->ofdm_tx_pwr_diff[0][j] =
114 		    MS(rom->ofdm_tx_pwr_diff[j], LOW_PART);
115 		rt->ht40_max_pwr[0][j] =
116 		    MS(rom->ht40_max_pwr[j], LOW_PART);
117 		rt->ht20_max_pwr[0][j] =
118 		    MS(rom->ht20_max_pwr[j], LOW_PART);
119 
120 		if (sc->ntxchains > 1) {
121 			rt->ht40_2s_tx_pwr_diff[1][j] =
122 			    MS(rom->ht40_2s_tx_pwr_diff[j], HIGH_PART);
123 			rt->ht20_tx_pwr_diff[1][j] =
124 			    RTWN_SIGN4TO8(MS(rom->ht20_tx_pwr_diff[j],
125 				HIGH_PART));
126 			rt->ofdm_tx_pwr_diff[1][j] =
127 			    MS(rom->ofdm_tx_pwr_diff[j], HIGH_PART);
128 			rt->ht40_max_pwr[1][j] =
129 			    MS(rom->ht40_max_pwr[j], HIGH_PART);
130 			rt->ht20_max_pwr[1][j] =
131 			    MS(rom->ht20_max_pwr[j], HIGH_PART);
132 		}
133 	}
134 
135 	sc->thermal_meter = MS(rom->thermal_meter, R92C_ROM_THERMAL_METER);
136 	if (sc->thermal_meter == R92C_ROM_THERMAL_METER_M)
137 		sc->thermal_meter = 0xff;
138 	IEEE80211_ADDR_COPY(sc->sc_ic.ic_macaddr, rom->macaddr);
139 }
140