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>
51 rtwn_reset_beacon_valid(struct rtwn_softc *sc, int id)
54 KASSERT (id == 0 || id == 1, ("wrong port id %d\n", id));
56 /* XXX cannot be cleared on RTL8188CE */
57 rtwn_setbits_1_shift(sc, sc->bcn_status_reg[id],
58 R92C_TDECTRL_BCN_VALID, 0, 2);
60 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
61 "%s: 'beacon valid' bit for vap %d was unset\n",
66 rtwn_check_beacon_valid(struct rtwn_softc *sc, int id)
71 if (id == RTWN_VAP_ID_INVALID)
74 reg = sc->bcn_status_reg[id];
75 for (ntries = 0; ntries < 10; ntries++) {
76 if (rtwn_read_4(sc, reg) & R92C_TDECTRL_BCN_VALID) {
77 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
78 "%s: beacon for vap %d was recognized\n",
82 rtwn_delay(sc, sc->bcn_check_interval);
91 rtwn_switch_bcnq(struct rtwn_softc *sc, int id)
94 if (sc->cur_bcnq_id != id) {
95 /* Wait until any previous transmit completes. */
96 (void) rtwn_check_beacon_valid(sc, sc->cur_bcnq_id);
98 /* Change current port. */
99 rtwn_beacon_select(sc, id);
100 sc->cur_bcnq_id = id;
103 /* Reset 'beacon valid' bit. */
104 rtwn_reset_beacon_valid(sc, id);
108 rtwn_setup_beacon(struct rtwn_softc *sc, struct ieee80211_node *ni)
110 struct ieee80211vap *vap = ni->ni_vap;
111 struct rtwn_vap *uvp = RTWN_VAP(vap);
114 RTWN_ASSERT_LOCKED(sc);
116 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
119 m = ieee80211_beacon_alloc(ni);
121 device_printf(sc->sc_dev,
122 "%s: could not allocate beacon frame\n", __func__);
126 if (uvp->bcn_mbuf != NULL) {
127 rtwn_beacon_unload(sc, uvp->id);
128 m_freem(uvp->bcn_mbuf);
133 rtwn_beacon_set_rate(sc, &uvp->bcn_desc.txd[0],
134 IEEE80211_IS_CHAN_5GHZ(ni->ni_chan));
136 return (rtwn_tx_beacon_check(sc, uvp));
140 * Push a beacon frame into the chip. Beacon will
141 * be repeated by the chip every R92C_BCN_INTERVAL.
144 rtwn_tx_beacon(struct rtwn_softc *sc, struct rtwn_vap *uvp)
148 RTWN_ASSERT_LOCKED(sc);
150 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
151 "%s: sending beacon for vap %d\n", __func__, uvp->id);
153 error = rtwn_tx_start(sc, NULL, uvp->bcn_mbuf, &uvp->bcn_desc.txd[0],
154 IEEE80211_FC0_TYPE_MGT, uvp->id);
160 rtwn_update_beacon(struct ieee80211vap *vap, int item)
162 struct ieee80211com *ic = vap->iv_ic;
163 struct rtwn_softc *sc = ic->ic_softc;
164 struct rtwn_vap *uvp = RTWN_VAP(vap);
165 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
166 struct ieee80211_node *ni = vap->iv_bss;
170 if (uvp->bcn_mbuf == NULL) {
171 uvp->bcn_mbuf = ieee80211_beacon_alloc(ni);
172 if (uvp->bcn_mbuf == NULL) {
173 device_printf(sc->sc_dev,
174 "%s: could not allocate beacon frame\n", __func__);
180 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
181 "%s: vap id %d, iv_csa_count %d, ic_csa_count %d, item %d\n",
182 __func__, uvp->id, vap->iv_csa_count, ic->ic_csa_count, item);
185 case IEEE80211_BEACON_CSA:
186 if (vap->iv_csa_count != ic->ic_csa_count) {
188 * XXX two APs with different beacon intervals
189 * are not handled properly.
191 /* XXX check TBTT? */
192 taskqueue_enqueue_timeout(taskqueue_thread,
194 msecs_to_ticks(ni->ni_intval));
197 case IEEE80211_BEACON_TIM:
204 setbit(bo->bo_flags, item);
206 rtwn_beacon_update_begin(sc, vap);
209 ieee80211_beacon_update(ni, uvp->bcn_mbuf, mcast);
211 /* XXX clear manually */
212 clrbit(bo->bo_flags, IEEE80211_BEACON_CSA);
215 rtwn_tx_beacon(sc, uvp);
216 rtwn_beacon_update_end(sc, vap);
221 rtwn_tx_beacon_csa(void *arg, int npending __unused)
223 struct ieee80211vap *vap = arg;
224 struct ieee80211com *ic = vap->iv_ic;
225 struct rtwn_softc *sc = ic->ic_softc;
226 struct rtwn_vap *rvp = RTWN_VAP(vap);
228 KASSERT (rvp->id == 0 || rvp->id == 1,
229 ("wrong port id %d\n", rvp->id));
232 if (ic->ic_flags & IEEE80211_F_CSAPENDING) {
233 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
234 "%s: vap id %d, iv_csa_count %d, ic_csa_count %d\n",
235 __func__, rvp->id, vap->iv_csa_count, ic->ic_csa_count);
237 rtwn_update_beacon(vap, IEEE80211_BEACON_CSA);
239 IEEE80211_UNLOCK(ic);
245 rtwn_tx_beacon_check(struct rtwn_softc *sc, struct rtwn_vap *uvp)
249 for (ntries = 0; ntries < 5; ntries++) {
250 rtwn_reset_beacon_valid(sc, uvp->id);
252 error = rtwn_tx_beacon(sc, uvp);
256 error = rtwn_check_beacon_valid(sc, uvp->id);
261 device_printf(sc->sc_dev,
262 "%s: cannot push beacon into chip, error %d!\n",