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 52 #include <dev/rtwn/rtl8188e/r88e.h> 53 #include <dev/rtwn/rtl8188e/r88e_reg.h> 54 #include <dev/rtwn/rtl8188e/r88e_fw_cmd.h> 55 56 57 #ifndef RTWN_WITHOUT_UCODE 58 int 59 r88e_fw_cmd(struct rtwn_softc *sc, uint8_t id, const void *buf, int len) 60 { 61 struct r88e_fw_cmd cmd; 62 int ntries, error; 63 64 if (!(sc->sc_flags & RTWN_FW_LOADED)) { 65 RTWN_DPRINTF(sc, RTWN_DEBUG_FIRMWARE, "%s: firmware " 66 "was not loaded; command (id %d) will be discarded\n", 67 __func__, id); 68 return (0); 69 } 70 71 /* Wait for current FW box to be empty. */ 72 for (ntries = 0; ntries < 100; ntries++) { 73 if (!(rtwn_read_1(sc, R92C_HMETFR) & (1 << sc->fwcur))) 74 break; 75 rtwn_delay(sc, 2000); 76 } 77 if (ntries == 100) { 78 device_printf(sc->sc_dev, 79 "could not send firmware command\n"); 80 return (ETIMEDOUT); 81 } 82 memset(&cmd, 0, sizeof(cmd)); 83 cmd.id = id; 84 KASSERT(len <= sizeof(cmd.msg), 85 ("%s: firmware command too long (%d > %zu)\n", 86 __func__, len, sizeof(cmd.msg))); 87 memcpy(cmd.msg, buf, len); 88 89 /* Write the first word last since that will trigger the FW. */ 90 if (len > 3) { 91 error = rtwn_write_4(sc, R88E_HMEBOX_EXT(sc->fwcur), 92 *(uint32_t *)((uint8_t *)&cmd + 4)); 93 if (error != 0) 94 return (error); 95 } 96 error = rtwn_write_4(sc, R92C_HMEBOX(sc->fwcur), *(uint32_t *)&cmd); 97 if (error != 0) 98 return (error); 99 100 sc->fwcur = (sc->fwcur + 1) % R92C_H2C_NBOX; 101 102 return (0); 103 } 104 105 void 106 r88e_fw_reset(struct rtwn_softc *sc, int reason) 107 { 108 uint16_t reg; 109 110 reg = rtwn_read_2(sc, R92C_SYS_FUNC_EN); 111 rtwn_write_2(sc, R92C_SYS_FUNC_EN, reg & ~R92C_SYS_FUNC_EN_CPUEN); 112 rtwn_write_2(sc, R92C_SYS_FUNC_EN, reg | R92C_SYS_FUNC_EN_CPUEN); 113 } 114 115 void 116 r88e_fw_download_enable(struct rtwn_softc *sc, int enable) 117 { 118 if (enable) { 119 /* MCU firmware download enable. */ 120 rtwn_setbits_1(sc, R92C_MCUFWDL, 0, R92C_MCUFWDL_EN); 121 /* 8051 reset. */ 122 rtwn_setbits_1_shift(sc, R92C_MCUFWDL, R92C_MCUFWDL_ROM_DLEN, 123 0, 2); 124 } else { 125 /* MCU download disable. */ 126 rtwn_setbits_1(sc, R92C_MCUFWDL, R92C_MCUFWDL_EN, 0); 127 /* Reserved for f/w extension. */ 128 rtwn_write_1(sc, R92C_MCUFWDL + 1, 0); 129 } 130 } 131 #endif 132 133 void 134 r88e_macid_enable_link(struct rtwn_softc *sc, int id, int enable) 135 { 136 uint32_t reg; 137 138 reg = R88E_MACID_NO_LINK; 139 if (id > 32) 140 reg += 4; 141 142 if (enable) 143 rtwn_setbits_4(sc, reg, 1 << (id % 32), 0); 144 else 145 rtwn_setbits_4(sc, reg, 0, 1 << (id % 32)); 146 147 /* XXX max macid for tx reports */ 148 } 149 150 void 151 r88e_set_media_status(struct rtwn_softc *sc, int macid) 152 { 153 struct r88e_fw_cmd_msrrpt status; 154 155 if (macid & RTWN_MACID_VALID) 156 status.msrb0 = R88E_MSRRPT_B0_ASSOC; 157 else 158 status.msrb0 = R88E_MSRRPT_B0_DISASSOC; 159 status.macid = (macid & ~RTWN_MACID_VALID); 160 161 r88e_macid_enable_link(sc, status.macid, 162 (macid & RTWN_MACID_VALID) != 0); 163 164 #ifndef RTWN_WITHOUT_UCODE 165 if (r88e_fw_cmd(sc, R88E_CMD_MSR_RPT, &status, sizeof(status)) != 0) { 166 device_printf(sc->sc_dev, "%s: cannot change media status!\n", 167 __func__); 168 } 169 #endif 170 } 171 172 #ifndef RTWN_WITHOUT_UCODE 173 int 174 r88e_set_rsvd_page(struct rtwn_softc *sc, int probe_resp, int null, 175 int qos_null) 176 { 177 struct r88e_fw_cmd_rsvdpage rsvd; 178 179 rsvd.probe_resp = probe_resp; 180 rsvd.ps_poll = 0; 181 rsvd.null_data = null; 182 rsvd.null_data_qos = qos_null; 183 rsvd.null_data_qos_bt = 0; 184 return (r88e_fw_cmd(sc, R88E_CMD_RSVD_PAGE, &rsvd, sizeof(rsvd))); 185 } 186 187 int 188 r88e_set_pwrmode(struct rtwn_softc *sc, struct ieee80211vap *vap, 189 int off) 190 { 191 struct r88e_fw_cmd_pwrmode mode; 192 int error; 193 194 if (off && vap->iv_state == IEEE80211_S_RUN && 195 (vap->iv_flags & IEEE80211_F_PMGTON)) { 196 mode.mode = R88E_PWRMODE_LEG; 197 /* 198 * TODO: switch to RFOFF state 199 * (something is missing here - Rx stops with it). 200 */ 201 #ifdef RTWN_TODO 202 mode.pwr_state = R88E_PWRMODE_STATE_RFOFF; 203 #else 204 mode.pwr_state = R88E_PWRMODE_STATE_RFON; 205 #endif 206 } else { 207 mode.mode = R88E_PWRMODE_CAM; 208 mode.pwr_state = R88E_PWRMODE_STATE_ALLON; 209 } 210 mode.pwrb1 = 211 SM(R88E_PWRMODE_B1_SMART_PS, R88E_PWRMODE_B1_LEG_NULLDATA) | 212 SM(R88E_PWRMODE_B1_RLBM, R88E_PWRMODE_B1_MODE_MIN); 213 /* XXX ignored */ 214 mode.bcn_pass = 0; 215 mode.queue_uapsd = 0; 216 error = r88e_fw_cmd(sc, R88E_CMD_SET_PWRMODE, &mode, sizeof(mode)); 217 if (error != 0) { 218 device_printf(sc->sc_dev, 219 "%s: CMD_SET_PWRMODE was not sent, error %d\n", 220 __func__, error); 221 } 222 223 return (error); 224 } 225 #endif 226