1 /*- 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> 5 * 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. 9 * 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. 17 */ 18 19 #include <sys/cdefs.h> 20 __FBSDID("$FreeBSD$"); 21 22 #include <sys/param.h> 23 #include <sys/lock.h> 24 #include <sys/mutex.h> 25 #include <sys/mbuf.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> 32 #include <sys/bus.h> 33 #include <sys/endian.h> 34 35 #include <net/if.h> 36 #include <net/ethernet.h> 37 #include <net/if_media.h> 38 39 #include <net80211/ieee80211_var.h> 40 #include <net80211/ieee80211_radiotap.h> 41 42 #include <dev/rtwn/if_rtwnvar.h> 43 44 #include <dev/rtwn/if_rtwn_beacon.h> 45 #include <dev/rtwn/if_rtwn_debug.h> 46 #include <dev/rtwn/if_rtwn_tx.h> 47 48 #include <dev/rtwn/rtl8192c/r92c_reg.h> 49 50 51 static void 52 rtwn_reset_beacon_valid(struct rtwn_softc *sc, int id) 53 { 54 55 KASSERT (id == 0 || id == 1, ("wrong port id %d\n", id)); 56 57 rtwn_setbits_1_shift(sc, sc->bcn_status_reg[id], 58 R92C_TDECTRL_BCN_VALID, 0, 2); 59 60 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON, 61 "%s: 'beacon valid' bit for vap %d was unset\n", 62 __func__, id); 63 } 64 65 static int 66 rtwn_check_beacon_valid(struct rtwn_softc *sc, int id) 67 { 68 uint16_t reg; 69 int ntries; 70 71 if (id == RTWN_VAP_ID_INVALID) 72 return (0); 73 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", 79 __func__, id); 80 break; 81 } 82 rtwn_delay(sc, 100); 83 } 84 if (ntries == 10) 85 return (ETIMEDOUT); 86 87 return (0); 88 } 89 90 void 91 rtwn_switch_bcnq(struct rtwn_softc *sc, int id) 92 { 93 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); 97 98 /* Change current port. */ 99 rtwn_beacon_select(sc, id); 100 sc->cur_bcnq_id = id; 101 } 102 103 /* Reset 'beacon valid' bit. */ 104 rtwn_reset_beacon_valid(sc, id); 105 } 106 107 int 108 rtwn_setup_beacon(struct rtwn_softc *sc, struct ieee80211_node *ni) 109 { 110 struct ieee80211vap *vap = ni->ni_vap; 111 struct rtwn_vap *uvp = RTWN_VAP(vap); 112 struct mbuf *m; 113 114 RTWN_ASSERT_LOCKED(sc); 115 116 if (ni->ni_chan == IEEE80211_CHAN_ANYC) 117 return (EINVAL); 118 119 m = ieee80211_beacon_alloc(ni); 120 if (m == NULL) { 121 device_printf(sc->sc_dev, 122 "%s: could not allocate beacon frame\n", __func__); 123 return (ENOMEM); 124 } 125 126 if (uvp->bcn_mbuf != NULL) 127 m_freem(uvp->bcn_mbuf); 128 129 uvp->bcn_mbuf = m; 130 131 rtwn_beacon_set_rate(sc, &uvp->bcn_desc.txd[0], 132 IEEE80211_IS_CHAN_5GHZ(ni->ni_chan)); 133 134 return (rtwn_tx_beacon_check(sc, uvp)); 135 } 136 137 /* 138 * Push a beacon frame into the chip. Beacon will 139 * be repeated by the chip every R92C_BCN_INTERVAL. 140 */ 141 static int 142 rtwn_tx_beacon(struct rtwn_softc *sc, struct rtwn_vap *uvp) 143 { 144 int error; 145 146 RTWN_ASSERT_LOCKED(sc); 147 148 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON, 149 "%s: sending beacon for vap %d\n", __func__, uvp->id); 150 151 error = rtwn_tx_start(sc, NULL, uvp->bcn_mbuf, &uvp->bcn_desc.txd[0], 152 IEEE80211_FC0_TYPE_MGT, uvp->id); 153 154 return (error); 155 } 156 157 void 158 rtwn_update_beacon(struct ieee80211vap *vap, int item) 159 { 160 struct rtwn_softc *sc = vap->iv_ic->ic_softc; 161 struct rtwn_vap *uvp = RTWN_VAP(vap); 162 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off; 163 struct ieee80211_node *ni = vap->iv_bss; 164 int mcast = 0; 165 166 RTWN_LOCK(sc); 167 if (uvp->bcn_mbuf == NULL) { 168 uvp->bcn_mbuf = ieee80211_beacon_alloc(ni); 169 if (uvp->bcn_mbuf == NULL) { 170 device_printf(sc->sc_dev, 171 "%s: could not allocate beacon frame\n", __func__); 172 RTWN_UNLOCK(sc); 173 return; 174 } 175 } 176 RTWN_UNLOCK(sc); 177 178 if (item == IEEE80211_BEACON_TIM) 179 mcast = 1; /* XXX */ 180 181 setbit(bo->bo_flags, item); 182 ieee80211_beacon_update(ni, uvp->bcn_mbuf, mcast); 183 184 RTWN_LOCK(sc); 185 rtwn_tx_beacon(sc, uvp); 186 RTWN_UNLOCK(sc); 187 } 188 189 int 190 rtwn_tx_beacon_check(struct rtwn_softc *sc, struct rtwn_vap *uvp) 191 { 192 int ntries, error; 193 194 for (ntries = 0; ntries < 5; ntries++) { 195 rtwn_reset_beacon_valid(sc, uvp->id); 196 197 error = rtwn_tx_beacon(sc, uvp); 198 if (error != 0) 199 continue; 200 201 error = rtwn_check_beacon_valid(sc, uvp->id); 202 if (error == 0) 203 break; 204 } 205 if (ntries == 5) { 206 device_printf(sc->sc_dev, 207 "%s: cannot push beacon into chip, error %d!\n", 208 __func__, error); 209 return (error); 210 } 211 212 return (0); 213 } 214