1 /* $OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $ */
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>
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.
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.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
26 #include <sys/param.h>
28 #include <sys/mutex.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>
37 #include <sys/endian.h>
40 #include <net/ethernet.h>
41 #include <net/if_media.h>
43 #include <net80211/ieee80211_var.h>
44 #include <net80211/ieee80211_radiotap.h>
46 #include <dev/rtwn/if_rtwnvar.h>
48 #include <dev/rtwn/if_rtwn_calib.h>
49 #include <dev/rtwn/if_rtwn_debug.h>
50 #include <dev/rtwn/if_rtwn_task.h>
54 rtwn_temp_calib(struct rtwn_softc *sc)
58 RTWN_ASSERT_LOCKED(sc);
60 if (!(sc->sc_flags & RTWN_TEMP_MEASURED)) {
61 /* Start measuring temperature. */
62 RTWN_DPRINTF(sc, RTWN_DEBUG_TEMP,
63 "%s: start measuring temperature\n", __func__);
64 rtwn_temp_measure(sc);
65 sc->sc_flags |= RTWN_TEMP_MEASURED;
68 sc->sc_flags &= ~RTWN_TEMP_MEASURED;
70 /* Read measured temperature. */
71 temp = rtwn_temp_read(sc);
72 if (temp == 0) { /* Read failed, skip. */
73 RTWN_DPRINTF(sc, RTWN_DEBUG_TEMP,
74 "%s: temperature read failed, skipping\n", __func__);
78 RTWN_DPRINTF(sc, RTWN_DEBUG_TEMP,
79 "temperature: previous %u, current %u\n",
80 sc->thcal_temp, temp);
83 * Redo LC/IQ calibration if temperature changed significantly since
86 if (sc->thcal_temp == 0xff) {
87 /* efuse value is absent; do LCK at initial status. */
90 sc->thcal_temp = temp;
91 } else if (abs(temp - sc->thcal_temp) > sc->temp_delta) {
92 RTWN_DPRINTF(sc, RTWN_DEBUG_TEMP,
93 "%s: LC/IQ calib triggered by temp: %u -> %u\n",
94 __func__, sc->thcal_temp, temp);
99 /* Record temperature of last calibration. */
100 sc->thcal_temp = temp;
105 rtwn_calib_cb(struct rtwn_softc *sc, union sec_param *data)
107 /* Do temperature compensation. */
110 #ifndef RTWN_WITHOUT_UCODE
111 if (sc->sc_ratectl == RTWN_RATECTL_FW) {
112 /* Refresh per-node RSSI. */
117 if (sc->vaps_running > sc->monvaps_running)
118 callout_reset(&sc->sc_calib_to, 2*hz, rtwn_calib_to, sc);
122 rtwn_calib_to(void *arg)
124 struct rtwn_softc *sc = arg;
126 /* Do it in a process context. */
127 rtwn_cmd_sleepable(sc, NULL, 0, rtwn_calib_cb);