2 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
3 * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org>
4 * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
22 #include <sys/param.h>
24 #include <sys/mutex.h>
26 #include <sys/kernel.h>
27 #include <sys/socket.h>
28 #include <sys/systm.h>
29 #include <sys/malloc.h>
30 #include <sys/queue.h>
31 #include <sys/taskqueue.h>
33 #include <sys/endian.h>
36 #include <net/ethernet.h>
37 #include <net/if_media.h>
39 #include <net80211/ieee80211_var.h>
40 #include <net80211/ieee80211_radiotap.h>
42 #include <dev/rtwn/if_rtwnvar.h>
44 #include <dev/rtwn/if_rtwn_beacon.h>
45 #include <dev/rtwn/if_rtwn_debug.h>
46 #include <dev/rtwn/if_rtwn_tx.h>
48 #include <dev/rtwn/rtl8192c/r92c_reg.h>
52 rtwn_reset_beacon_valid(struct rtwn_softc *sc, int id)
55 KASSERT (id == 0 || id == 1, ("wrong port id %d\n", id));
57 /* XXX cannot be cleared on RTL8188CE */
58 rtwn_setbits_1_shift(sc, sc->bcn_status_reg[id],
59 R92C_TDECTRL_BCN_VALID, 0, 2);
61 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
62 "%s: 'beacon valid' bit for vap %d was unset\n",
67 rtwn_check_beacon_valid(struct rtwn_softc *sc, int id)
72 if (id == RTWN_VAP_ID_INVALID)
75 reg = sc->bcn_status_reg[id];
76 for (ntries = 0; ntries < 10; ntries++) {
77 if (rtwn_read_4(sc, reg) & R92C_TDECTRL_BCN_VALID) {
78 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
79 "%s: beacon for vap %d was recognized\n",
83 rtwn_delay(sc, sc->bcn_check_interval);
92 rtwn_switch_bcnq(struct rtwn_softc *sc, int id)
95 if (sc->cur_bcnq_id != id) {
96 /* Wait until any previous transmit completes. */
97 (void) rtwn_check_beacon_valid(sc, sc->cur_bcnq_id);
99 /* Change current port. */
100 rtwn_beacon_select(sc, id);
101 sc->cur_bcnq_id = id;
104 /* Reset 'beacon valid' bit. */
105 rtwn_reset_beacon_valid(sc, id);
109 rtwn_setup_beacon(struct rtwn_softc *sc, struct ieee80211_node *ni)
111 struct ieee80211vap *vap = ni->ni_vap;
112 struct rtwn_vap *uvp = RTWN_VAP(vap);
115 RTWN_ASSERT_LOCKED(sc);
117 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
120 m = ieee80211_beacon_alloc(ni);
122 device_printf(sc->sc_dev,
123 "%s: could not allocate beacon frame\n", __func__);
127 if (uvp->bcn_mbuf != NULL) {
128 rtwn_beacon_unload(sc, uvp->id);
129 m_freem(uvp->bcn_mbuf);
134 rtwn_beacon_set_rate(sc, &uvp->bcn_desc.txd[0],
135 IEEE80211_IS_CHAN_5GHZ(ni->ni_chan));
137 return (rtwn_tx_beacon_check(sc, uvp));
141 * Push a beacon frame into the chip. Beacon will
142 * be repeated by the chip every R92C_BCN_INTERVAL.
145 rtwn_tx_beacon(struct rtwn_softc *sc, struct rtwn_vap *uvp)
149 RTWN_ASSERT_LOCKED(sc);
151 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
152 "%s: sending beacon for vap %d\n", __func__, uvp->id);
154 error = rtwn_tx_start(sc, NULL, uvp->bcn_mbuf, &uvp->bcn_desc.txd[0],
155 IEEE80211_FC0_TYPE_MGT, uvp->id);
161 rtwn_update_beacon(struct ieee80211vap *vap, int item)
163 struct ieee80211com *ic = vap->iv_ic;
164 struct rtwn_softc *sc = ic->ic_softc;
165 struct rtwn_vap *uvp = RTWN_VAP(vap);
166 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
167 struct ieee80211_node *ni = vap->iv_bss;
171 if (uvp->bcn_mbuf == NULL) {
172 uvp->bcn_mbuf = ieee80211_beacon_alloc(ni);
173 if (uvp->bcn_mbuf == NULL) {
174 device_printf(sc->sc_dev,
175 "%s: could not allocate beacon frame\n", __func__);
181 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
182 "%s: vap id %d, iv_csa_count %d, ic_csa_count %d, item %d\n",
183 __func__, uvp->id, vap->iv_csa_count, ic->ic_csa_count, item);
186 case IEEE80211_BEACON_CSA:
187 if (vap->iv_csa_count != ic->ic_csa_count) {
189 * XXX two APs with different beacon intervals
190 * are not handled properly.
192 /* XXX check TBTT? */
193 taskqueue_enqueue_timeout(taskqueue_thread,
195 msecs_to_ticks(ni->ni_intval));
198 case IEEE80211_BEACON_TIM:
205 setbit(bo->bo_flags, item);
207 rtwn_beacon_update_begin(sc, vap);
210 ieee80211_beacon_update(ni, uvp->bcn_mbuf, mcast);
212 /* XXX clear manually */
213 clrbit(bo->bo_flags, IEEE80211_BEACON_CSA);
216 rtwn_tx_beacon(sc, uvp);
217 rtwn_beacon_update_end(sc, vap);
222 rtwn_tx_beacon_csa(void *arg, int npending __unused)
224 struct ieee80211vap *vap = arg;
225 struct ieee80211com *ic = vap->iv_ic;
226 struct rtwn_softc *sc = ic->ic_softc;
227 struct rtwn_vap *rvp = RTWN_VAP(vap);
229 KASSERT (rvp->id == 0 || rvp->id == 1,
230 ("wrong port id %d\n", rvp->id));
233 if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
234 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
235 "%s: vap id %d, iv_csa_count %d, ic_csa_count %d\n",
236 __func__, rvp->id, vap->iv_csa_count, ic->ic_csa_count);
238 rtwn_update_beacon(vap, IEEE80211_BEACON_CSA);
240 IEEE80211_UNLOCK(ic);
246 rtwn_tx_beacon_check(struct rtwn_softc *sc, struct rtwn_vap *uvp)
250 for (ntries = 0; ntries < 5; ntries++) {
251 rtwn_reset_beacon_valid(sc, uvp->id);
253 error = rtwn_tx_beacon(sc, uvp);
257 error = rtwn_check_beacon_valid(sc, uvp->id);
262 device_printf(sc->sc_dev,
263 "%s: cannot push beacon into chip, error %d!\n",