Home | History | Annotate | Line # | Download | only in usb
if_urtwn.c revision 1.111
      1 /*	$NetBSD: if_urtwn.c,v 1.111 2024/11/10 19:01:25 riastradh Exp $	*/
      2 /*	$OpenBSD: if_urtwn.c,v 1.42 2015/02/10 23:25:46 mpi Exp $	*/
      3 
      4 /*-
      5  * Copyright (c) 2010 Damien Bergamini <damien.bergamini (at) free.fr>
      6  * Copyright (c) 2014 Kevin Lo <kevlo (at) FreeBSD.org>
      7  * Copyright (c) 2016 Nathanial Sloss <nathanialsloss (at) yahoo.com.au>
      8  *
      9  * Permission to use, copy, modify, and distribute this software for any
     10  * purpose with or without fee is hereby granted, provided that the above
     11  * copyright notice and this permission notice appear in all copies.
     12  *
     13  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     19  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     20  */
     21 
     22 /*-
     23  * Driver for Realtek RTL8188CE-VAU/RTL8188CUS/RTL8188EU/RTL8188RU/RTL8192CU
     24  * RTL8192EU.
     25  */
     26 
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: if_urtwn.c,v 1.111 2024/11/10 19:01:25 riastradh Exp $");
     29 
     30 #ifdef _KERNEL_OPT
     31 #include "opt_inet.h"
     32 #include "opt_usb.h"
     33 #endif
     34 
     35 #include <sys/param.h>
     36 #include <sys/sockio.h>
     37 #include <sys/sysctl.h>
     38 #include <sys/mbuf.h>
     39 #include <sys/kernel.h>
     40 #include <sys/socket.h>
     41 #include <sys/systm.h>
     42 #include <sys/module.h>
     43 #include <sys/conf.h>
     44 #include <sys/device.h>
     45 #include <sys/rndsource.h>
     46 
     47 #include <sys/bus.h>
     48 #include <machine/endian.h>
     49 #include <sys/intr.h>
     50 
     51 #include <net/bpf.h>
     52 #include <net/if.h>
     53 #include <net/if_arp.h>
     54 #include <net/if_dl.h>
     55 #include <net/if_ether.h>
     56 #include <net/if_media.h>
     57 #include <net/if_types.h>
     58 
     59 #include <netinet/in.h>
     60 #include <netinet/in_systm.h>
     61 #include <netinet/in_var.h>
     62 #include <netinet/ip.h>
     63 #include <netinet/if_inarp.h>
     64 
     65 #include <net80211/ieee80211_netbsd.h>
     66 #include <net80211/ieee80211_var.h>
     67 #include <net80211/ieee80211_radiotap.h>
     68 
     69 #include <dev/firmload.h>
     70 
     71 #include <dev/usb/usb.h>
     72 #include <dev/usb/usbdi.h>
     73 #include <dev/usb/usbdivar.h>
     74 #include <dev/usb/usbdi_util.h>
     75 #include <dev/usb/usbdevs.h>
     76 #include <dev/usb/usbhist.h>
     77 
     78 #include <dev/ic/rtwnreg.h>
     79 #include <dev/ic/rtwn_data.h>
     80 #include <dev/usb/if_urtwnreg.h>
     81 #include <dev/usb/if_urtwnvar.h>
     82 
     83 /*
     84  * The sc_write_mtx locking is to prevent sequences of writes from
     85  * being intermingled with each other.  I don't know if this is really
     86  * needed.  I have added it just to be on the safe side.
     87  */
     88 
     89 #ifdef URTWN_DEBUG
     90 #define	DBG_INIT	__BIT(0)
     91 #define	DBG_FN		__BIT(1)
     92 #define	DBG_TX		__BIT(2)
     93 #define	DBG_RX		__BIT(3)
     94 #define	DBG_STM		__BIT(4)
     95 #define	DBG_RF		__BIT(5)
     96 #define	DBG_REG		__BIT(6)
     97 #define	DBG_ALL		0xffffffffU
     98 
     99 #ifndef URTWN_DEBUG_DEFAULT
    100 #define URTWN_DEBUG_DEFAULT 0
    101 #endif
    102 
    103 u_int urtwn_debug = URTWN_DEBUG_DEFAULT;
    104 
    105 #define DPRINTFN(n, fmt, a, b, c, d) do {			\
    106 	if (urtwn_debug & (n)) {				\
    107 		KERNHIST_LOG(usbhist, fmt, a, b, c, d);		\
    108 	}							\
    109 } while (/*CONSTCOND*/0)
    110 #define URTWNHIST_FUNC() USBHIST_FUNC()
    111 #define URTWNHIST_CALLED() do {					\
    112 	if (urtwn_debug & DBG_FN) {				\
    113 		KERNHIST_CALLED(usbhist);			\
    114 	}							\
    115 } while(/*CONSTCOND*/0)
    116 #define URTWNHIST_CALLARGS(fmt, a, b, c, d) do {		\
    117 	if (urtwn_debug & DBG_FN) {				\
    118 		KERNHIST_CALLARGS(usbhist, fmt, a, b, c, d);	\
    119 	}							\
    120 } while(/*CONSTCOND*/0)
    121 #else
    122 #define DPRINTFN(n, fmt, a, b, c, d)
    123 #define URTWNHIST_FUNC()
    124 #define URTWNHIST_CALLED()
    125 #define URTWNHIST_CALLARGS(fmt, a, b, c, d)
    126 #endif
    127 
    128 #define URTWN_DEV(v,p)	{ { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, 0 }
    129 #define URTWN_RTL8188E_DEV(v,p) \
    130 	{ { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, FLAG_RTL8188E }
    131 #define URTWN_RTL8192EU_DEV(v,p) \
    132 	{ { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, FLAG_RTL8192E }
    133 static const struct urtwn_dev {
    134 	struct usb_devno	dev;
    135 	uint32_t		flags;
    136 #define	FLAG_RTL8188E	__BIT(0)
    137 #define	FLAG_RTL8192E	__BIT(1)
    138 } urtwn_devs[] = {
    139 	URTWN_DEV(ABOCOM,	RTL8188CU_1),
    140 	URTWN_DEV(ABOCOM,	RTL8188CU_2),
    141 	URTWN_DEV(ABOCOM,	RTL8192CU),
    142 	URTWN_DEV(ASUSTEK,	RTL8192CU),
    143 	URTWN_DEV(ASUSTEK,	RTL8192CU_3),
    144 	URTWN_DEV(ASUSTEK,	USBN10NANO),
    145 	URTWN_DEV(ASUSTEK,	RTL8192CU_3),
    146 	URTWN_DEV(AZUREWAVE,	RTL8188CE_1),
    147 	URTWN_DEV(AZUREWAVE,	RTL8188CE_2),
    148 	URTWN_DEV(AZUREWAVE,	RTL8188CU),
    149 	URTWN_DEV(BELKIN,	F7D2102),
    150 	URTWN_DEV(BELKIN,	RTL8188CU),
    151 	URTWN_DEV(BELKIN,	RTL8188CUS),
    152 	URTWN_DEV(BELKIN,	RTL8192CU),
    153 	URTWN_DEV(BELKIN,	RTL8192CU_1),
    154 	URTWN_DEV(BELKIN,	RTL8192CU_2),
    155 	URTWN_DEV(CHICONY,	RTL8188CUS_1),
    156 	URTWN_DEV(CHICONY,	RTL8188CUS_2),
    157 	URTWN_DEV(CHICONY,	RTL8188CUS_3),
    158 	URTWN_DEV(CHICONY,	RTL8188CUS_4),
    159 	URTWN_DEV(CHICONY,	RTL8188CUS_5),
    160 	URTWN_DEV(CHICONY,	RTL8188CUS_6),
    161 	URTWN_DEV(COMPARE,	RTL8192CU),
    162 	URTWN_DEV(COREGA,	RTL8192CU),
    163 	URTWN_DEV(DLINK,	DWA131B),
    164 	URTWN_DEV(DLINK,	RTL8188CU),
    165 	URTWN_DEV(DLINK,	RTL8192CU_1),
    166 	URTWN_DEV(DLINK,	RTL8192CU_2),
    167 	URTWN_DEV(DLINK,	RTL8192CU_3),
    168 	URTWN_DEV(DLINK,	RTL8192CU_4),
    169 	URTWN_DEV(EDIMAX,	RTL8188CU),
    170 	URTWN_DEV(EDIMAX,	RTL8192CU),
    171 	URTWN_DEV(FEIXUN,	RTL8188CU),
    172 	URTWN_DEV(FEIXUN,	RTL8192CU),
    173 	URTWN_DEV(GUILLEMOT,	HWNUP150),
    174 	URTWN_DEV(GUILLEMOT,	RTL8192CU),
    175 	URTWN_DEV(HAWKING,	RTL8192CU),
    176 	URTWN_DEV(HAWKING,	RTL8192CU_2),
    177 	URTWN_DEV(HP3,		RTL8188CU),
    178 	URTWN_DEV(IODATA,	WNG150UM),
    179 	URTWN_DEV(IODATA,	RTL8192CU),
    180 	URTWN_DEV(NETGEAR,	WNA1000M),
    181 	URTWN_DEV(NETGEAR,	RTL8192CU),
    182 	URTWN_DEV(NETGEAR4,	RTL8188CU),
    183 	URTWN_DEV(NOVATECH,	RTL8188CU),
    184 	URTWN_DEV(PLANEX2,	RTL8188CU_1),
    185 	URTWN_DEV(PLANEX2,	RTL8188CU_2),
    186 	URTWN_DEV(PLANEX2,	RTL8192CU),
    187 	URTWN_DEV(PLANEX2,	RTL8188CU_3),
    188 	URTWN_DEV(PLANEX2,	RTL8188CU_4),
    189 	URTWN_DEV(PLANEX2,	RTL8188CUS),
    190 	URTWN_DEV(REALTEK,	RTL8188CE_0),
    191 	URTWN_DEV(REALTEK,	RTL8188CE_1),
    192 	URTWN_DEV(REALTEK,	RTL8188CTV),
    193 	URTWN_DEV(REALTEK,	RTL8188CU_0),
    194 	URTWN_DEV(REALTEK,	RTL8188CU_1),
    195 	URTWN_DEV(REALTEK,	RTL8188CU_2),
    196 	URTWN_DEV(REALTEK,	RTL8188CU_3),
    197 	URTWN_DEV(REALTEK,	RTL8188CU_COMBO),
    198 	URTWN_DEV(REALTEK,	RTL8188CUS),
    199 	URTWN_DEV(REALTEK,	RTL8188RU),
    200 	URTWN_DEV(REALTEK,	RTL8188RU_2),
    201 	URTWN_DEV(REALTEK,	RTL8188RU_3),
    202 	URTWN_DEV(REALTEK,	RTL8191CU),
    203 	URTWN_DEV(REALTEK,	RTL8192CE),
    204 	URTWN_DEV(REALTEK,	RTL8192CU),
    205 	URTWN_DEV(SITECOMEU,	RTL8188CU),
    206 	URTWN_DEV(SITECOMEU,	RTL8188CU_2),
    207 	URTWN_DEV(SITECOMEU,	RTL8192CU),
    208 	URTWN_DEV(SITECOMEU,	RTL8192CUR2),
    209 	URTWN_DEV(TPLINK,	RTL8192CU),
    210 	URTWN_DEV(TRENDNET,	RTL8188CU),
    211 	URTWN_DEV(TRENDNET,	RTL8192CU),
    212 	URTWN_DEV(TRENDNET,	TEW648UBM),
    213 	URTWN_DEV(ZYXEL,	RTL8192CU),
    214 
    215 	/* URTWN_RTL8188E */
    216 	URTWN_RTL8188E_DEV(DLINK, DWA125D1),
    217 	URTWN_RTL8188E_DEV(ELECOM, WDC150SU2M),
    218 	URTWN_RTL8188E_DEV(MERCUSYS, MW150USV2),
    219 	URTWN_RTL8188E_DEV(REALTEK, RTL8188ETV),
    220 	URTWN_RTL8188E_DEV(REALTEK, RTL8188EU),
    221 	URTWN_RTL8188E_DEV(ABOCOM, RTL8188EU),
    222 	URTWN_RTL8188E_DEV(TPLINK, RTL8188EU),
    223 	URTWN_RTL8188E_DEV(DLINK, DWA121B1),
    224 	URTWN_RTL8188E_DEV(EDIMAX, EW7811UNV2),
    225 
    226 	/* URTWN_RTL8192EU */
    227 	URTWN_RTL8192EU_DEV(DLINK,	DWA131E),
    228 	URTWN_RTL8192EU_DEV(REALTEK,	RTL8192EU),
    229 	URTWN_RTL8192EU_DEV(TPLINK,	WN821NV5),
    230 	URTWN_RTL8192EU_DEV(TPLINK,	WN822NV4),
    231 	URTWN_RTL8192EU_DEV(TPLINK,	WN823NV2),
    232 };
    233 #undef URTWN_DEV
    234 #undef URTWN_RTL8188E_DEV
    235 #undef URTWN_RTL8192EU_DEV
    236 
    237 static int	urtwn_match(device_t, cfdata_t, void *);
    238 static void	urtwn_attach(device_t, device_t, void *);
    239 static int	urtwn_detach(device_t, int);
    240 static int	urtwn_activate(device_t, enum devact);
    241 
    242 CFATTACH_DECL_NEW(urtwn, sizeof(struct urtwn_softc), urtwn_match,
    243     urtwn_attach, urtwn_detach, urtwn_activate);
    244 
    245 static int	urtwn_open_pipes(struct urtwn_softc *);
    246 static void	urtwn_close_pipes(struct urtwn_softc *);
    247 static int	urtwn_alloc_rx_list(struct urtwn_softc *);
    248 static void	urtwn_free_rx_list(struct urtwn_softc *);
    249 static int	urtwn_alloc_tx_list(struct urtwn_softc *);
    250 static void	urtwn_free_tx_list(struct urtwn_softc *);
    251 static void	urtwn_task(void *);
    252 static void	urtwn_do_async(struct urtwn_softc *,
    253 		    void (*)(struct urtwn_softc *, void *), void *, int);
    254 static void	urtwn_wait_async(struct urtwn_softc *);
    255 static int	urtwn_write_region_1(struct urtwn_softc *, uint16_t, uint8_t *,
    256 		    int);
    257 static void	urtwn_write_1(struct urtwn_softc *, uint16_t, uint8_t);
    258 static void	urtwn_write_2(struct urtwn_softc *, uint16_t, uint16_t);
    259 static void	urtwn_write_4(struct urtwn_softc *, uint16_t, uint32_t);
    260 static int	urtwn_write_region(struct urtwn_softc *, uint16_t, uint8_t *,
    261 		    int);
    262 static int	urtwn_read_region_1(struct urtwn_softc *, uint16_t, uint8_t *,
    263 		    int);
    264 static uint8_t	urtwn_read_1(struct urtwn_softc *, uint16_t);
    265 static uint16_t	urtwn_read_2(struct urtwn_softc *, uint16_t);
    266 static uint32_t	urtwn_read_4(struct urtwn_softc *, uint16_t);
    267 static int	urtwn_fw_cmd(struct urtwn_softc *, uint8_t, const void *, int);
    268 static void	urtwn_r92c_rf_write(struct urtwn_softc *, int, uint8_t,
    269 		    uint32_t);
    270 static void	urtwn_r88e_rf_write(struct urtwn_softc *, int, uint8_t,
    271 		    uint32_t);
    272 static void	urtwn_r92e_rf_write(struct urtwn_softc *, int, uint8_t,
    273 		    uint32_t);
    274 static uint32_t	urtwn_rf_read(struct urtwn_softc *, int, uint8_t);
    275 static int	urtwn_llt_write(struct urtwn_softc *, uint32_t, uint32_t);
    276 static uint8_t	urtwn_efuse_read_1(struct urtwn_softc *, uint16_t);
    277 static void	urtwn_efuse_read(struct urtwn_softc *);
    278 static void	urtwn_efuse_switch_power(struct urtwn_softc *);
    279 static int	urtwn_read_chipid(struct urtwn_softc *);
    280 #ifdef URTWN_DEBUG
    281 static void	urtwn_dump_rom(struct urtwn_softc *, struct r92c_rom *);
    282 #endif
    283 static void	urtwn_read_rom(struct urtwn_softc *);
    284 static void	urtwn_r88e_read_rom(struct urtwn_softc *);
    285 static int	urtwn_media_change(struct ifnet *);
    286 static int	urtwn_ra_init(struct urtwn_softc *);
    287 static int	urtwn_get_nettype(struct urtwn_softc *);
    288 static void	urtwn_set_nettype0_msr(struct urtwn_softc *, uint8_t);
    289 static void	urtwn_tsf_sync_enable(struct urtwn_softc *);
    290 static void	urtwn_set_led(struct urtwn_softc *, int, int);
    291 static void	urtwn_calib_to(void *);
    292 static void	urtwn_calib_to_cb(struct urtwn_softc *, void *);
    293 static void	urtwn_next_scan(void *);
    294 static int	urtwn_newstate(struct ieee80211com *, enum ieee80211_state,
    295 		    int);
    296 static void	urtwn_newstate_cb(struct urtwn_softc *, void *);
    297 static int	urtwn_wme_update(struct ieee80211com *);
    298 static void	urtwn_wme_update_cb(struct urtwn_softc *, void *);
    299 static void	urtwn_update_avgrssi(struct urtwn_softc *, int, int8_t);
    300 static int8_t	urtwn_get_rssi(struct urtwn_softc *, int, void *);
    301 static int8_t	urtwn_r88e_get_rssi(struct urtwn_softc *, int, void *);
    302 static void	urtwn_rx_frame(struct urtwn_softc *, uint8_t *, int);
    303 static void	urtwn_rxeof(struct usbd_xfer *, void *, usbd_status);
    304 static void	urtwn_txeof(struct usbd_xfer *, void *, usbd_status);
    305 static int	urtwn_tx(struct urtwn_softc *, struct mbuf *,
    306 		    struct ieee80211_node *, struct urtwn_tx_data *);
    307 static struct urtwn_tx_data *
    308 		urtwn_get_tx_data(struct urtwn_softc *, size_t);
    309 static void	urtwn_start(struct ifnet *);
    310 static void	urtwn_watchdog(struct ifnet *);
    311 static int	urtwn_ioctl(struct ifnet *, u_long, void *);
    312 static int	urtwn_r92c_power_on(struct urtwn_softc *);
    313 static int	urtwn_r92e_power_on(struct urtwn_softc *);
    314 static int	urtwn_r88e_power_on(struct urtwn_softc *);
    315 static int	urtwn_llt_init(struct urtwn_softc *);
    316 static void	urtwn_fw_reset(struct urtwn_softc *);
    317 static void	urtwn_r88e_fw_reset(struct urtwn_softc *);
    318 static int	urtwn_fw_loadpage(struct urtwn_softc *, int, uint8_t *, int);
    319 static int	urtwn_load_firmware(struct urtwn_softc *);
    320 static int	urtwn_r92c_dma_init(struct urtwn_softc *);
    321 static int	urtwn_r88e_dma_init(struct urtwn_softc *);
    322 static void	urtwn_mac_init(struct urtwn_softc *);
    323 static void	urtwn_bb_init(struct urtwn_softc *);
    324 static void	urtwn_rf_init(struct urtwn_softc *);
    325 static void	urtwn_cam_init(struct urtwn_softc *);
    326 static void	urtwn_pa_bias_init(struct urtwn_softc *);
    327 static void	urtwn_rxfilter_init(struct urtwn_softc *);
    328 static void	urtwn_edca_init(struct urtwn_softc *);
    329 static void	urtwn_write_txpower(struct urtwn_softc *, int,
    330 		    uint16_t[URTWN_RIDX_COUNT]);
    331 static void	urtwn_get_txpower(struct urtwn_softc *, size_t, u_int, u_int,
    332 		    uint16_t[URTWN_RIDX_COUNT]);
    333 static void	urtwn_r88e_get_txpower(struct urtwn_softc *, size_t, u_int,
    334 		    u_int, uint16_t[URTWN_RIDX_COUNT]);
    335 static void	urtwn_set_txpower(struct urtwn_softc *, u_int, u_int);
    336 static void	urtwn_set_chan(struct urtwn_softc *, struct ieee80211_channel *,
    337 		    u_int);
    338 static void	urtwn_iq_calib(struct urtwn_softc *, bool);
    339 static void	urtwn_lc_calib(struct urtwn_softc *);
    340 static void	urtwn_temp_calib(struct urtwn_softc *);
    341 static int	urtwn_init(struct ifnet *);
    342 static void	urtwn_stop(struct ifnet *, int);
    343 static int	urtwn_reset(struct ifnet *);
    344 static void	urtwn_chip_stop(struct urtwn_softc *);
    345 static void	urtwn_newassoc(struct ieee80211_node *, int);
    346 static void	urtwn_delay_ms(struct urtwn_softc *, int ms);
    347 
    348 /* Aliases. */
    349 #define	urtwn_bb_write	urtwn_write_4
    350 #define	urtwn_bb_read	urtwn_read_4
    351 
    352 #define	urtwn_lookup(d,v,p)	((const struct urtwn_dev *)usb_lookup(d,v,p))
    353 
    354 static const uint16_t addaReg[] = {
    355 	R92C_FPGA0_XCD_SWITCHCTL, R92C_BLUETOOTH, R92C_RX_WAIT_CCA,
    356 	R92C_TX_CCK_RFON, R92C_TX_CCK_BBON, R92C_TX_OFDM_RFON,
    357 	R92C_TX_OFDM_BBON, R92C_TX_TO_RX, R92C_TX_TO_TX, R92C_RX_CCK,
    358 	R92C_RX_OFDM, R92C_RX_WAIT_RIFS, R92C_RX_TO_RX,
    359 	R92C_STANDBY, R92C_SLEEP, R92C_PMPD_ANAEN
    360 };
    361 
    362 static int
    363 urtwn_match(device_t parent, cfdata_t match, void *aux)
    364 {
    365 	struct usb_attach_arg *uaa = aux;
    366 
    367 	return urtwn_lookup(urtwn_devs, uaa->uaa_vendor, uaa->uaa_product) !=
    368 	    NULL ?  UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    369 }
    370 
    371 static void
    372 urtwn_attach(device_t parent, device_t self, void *aux)
    373 {
    374 	struct urtwn_softc *sc = device_private(self);
    375 	struct ieee80211com *ic = &sc->sc_ic;
    376 	struct ifnet *ifp = &sc->sc_if;
    377 	struct usb_attach_arg *uaa = aux;
    378 	char *devinfop;
    379 	const struct urtwn_dev *dev;
    380 	usb_device_request_t req;
    381 	size_t i;
    382 	int error;
    383 
    384 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
    385 
    386 	sc->sc_dev = self;
    387 	sc->sc_udev = uaa->uaa_device;
    388 
    389 	sc->chip = 0;
    390 	dev = urtwn_lookup(urtwn_devs, uaa->uaa_vendor, uaa->uaa_product);
    391 	if (dev != NULL && ISSET(dev->flags, FLAG_RTL8188E))
    392 		SET(sc->chip, URTWN_CHIP_88E);
    393 	if (dev != NULL && ISSET(dev->flags, FLAG_RTL8192E))
    394 		SET(sc->chip, URTWN_CHIP_92EU);
    395 
    396 	aprint_naive("\n");
    397 	aprint_normal("\n");
    398 
    399 	devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
    400 	aprint_normal_dev(self, "%s\n", devinfop);
    401 	usbd_devinfo_free(devinfop);
    402 
    403 	req.bmRequestType = UT_WRITE_DEVICE;
    404 	req.bRequest = UR_SET_FEATURE;
    405 	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
    406 	USETW(req.wIndex, UHF_PORT_SUSPEND);
    407 	USETW(req.wLength, 0);
    408 
    409 	(void) usbd_do_request(sc->sc_udev, &req, 0);
    410 
    411 	cv_init(&sc->sc_task_cv, "urtwntsk");
    412 	mutex_init(&sc->sc_task_mtx, MUTEX_DEFAULT, IPL_NET);
    413 	mutex_init(&sc->sc_tx_mtx, MUTEX_DEFAULT, IPL_NONE);
    414 	mutex_init(&sc->sc_rx_mtx, MUTEX_DEFAULT, IPL_NONE);
    415 	mutex_init(&sc->sc_fwcmd_mtx, MUTEX_DEFAULT, IPL_NONE);
    416 	mutex_init(&sc->sc_write_mtx, MUTEX_DEFAULT, IPL_NONE);
    417 
    418 	usb_init_task(&sc->sc_task, urtwn_task, sc, 0);
    419 
    420 	callout_init(&sc->sc_scan_to, 0);
    421 	callout_setfunc(&sc->sc_scan_to, urtwn_next_scan, sc);
    422 	callout_init(&sc->sc_calib_to, 0);
    423 	callout_setfunc(&sc->sc_calib_to, urtwn_calib_to, sc);
    424 
    425 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    426 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    427 
    428 	error = usbd_set_config_no(sc->sc_udev, 1, 0);
    429 	if (error != 0) {
    430 		aprint_error_dev(self, "failed to set configuration"
    431 		    ", err=%s\n", usbd_errstr(error));
    432 		goto fail;
    433 	}
    434 
    435 	/* Get the first interface handle. */
    436 	error = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface);
    437 	if (error != 0) {
    438 		aprint_error_dev(self, "could not get interface handle\n");
    439 		goto fail;
    440 	}
    441 
    442 	error = urtwn_read_chipid(sc);
    443 	if (error != 0) {
    444 		aprint_error_dev(self, "unsupported test chip\n");
    445 		goto fail;
    446 	}
    447 
    448 	/* Determine number of Tx/Rx chains. */
    449 	if (sc->chip & URTWN_CHIP_92C) {
    450 		sc->ntxchains = (sc->chip & URTWN_CHIP_92C_1T2R) ? 1 : 2;
    451 		sc->nrxchains = 2;
    452 	} else if (sc->chip & URTWN_CHIP_92EU) {
    453 		sc->ntxchains = 2;
    454 		sc->nrxchains = 2;
    455 	} else {
    456 		sc->ntxchains = 1;
    457 		sc->nrxchains = 1;
    458 	}
    459 
    460 	if (ISSET(sc->chip, URTWN_CHIP_88E) ||
    461 	    ISSET(sc->chip, URTWN_CHIP_92EU))
    462 		urtwn_r88e_read_rom(sc);
    463 	else
    464 		urtwn_read_rom(sc);
    465 
    466 	aprint_normal_dev(self, "MAC/BB RTL%s, RF 6052 %zdT%zdR, address %s\n",
    467 	    (sc->chip & URTWN_CHIP_92EU) ? "8192EU" :
    468 	    (sc->chip & URTWN_CHIP_92C) ? "8192CU" :
    469 	    (sc->chip & URTWN_CHIP_88E) ? "8188EU" :
    470 	    (sc->board_type == R92C_BOARD_TYPE_HIGHPA) ? "8188RU" :
    471 	    (sc->board_type == R92C_BOARD_TYPE_MINICARD) ? "8188CE-VAU" :
    472 	    "8188CUS", sc->ntxchains, sc->nrxchains,
    473 	    ether_sprintf(ic->ic_myaddr));
    474 
    475 	error = urtwn_open_pipes(sc);
    476 	if (error != 0) {
    477 		aprint_error_dev(sc->sc_dev, "could not open pipes\n");
    478 		goto fail;
    479 	}
    480 	aprint_normal_dev(self, "%d rx pipe%s, %d tx pipe%s\n",
    481 	    sc->rx_npipe, sc->rx_npipe > 1 ? "s" : "",
    482 	    sc->tx_npipe, sc->tx_npipe > 1 ? "s" : "");
    483 
    484 	/*
    485 	 * Setup the 802.11 device.
    486 	 */
    487 	ic->ic_ifp = ifp;
    488 	ic->ic_phytype = IEEE80211_T_OFDM;	/* Not only, but not used. */
    489 	ic->ic_opmode = IEEE80211_M_STA;	/* Default to BSS mode. */
    490 	ic->ic_state = IEEE80211_S_INIT;
    491 
    492 	/* Set device capabilities. */
    493 	ic->ic_caps =
    494 	    IEEE80211_C_MONITOR |	/* Monitor mode supported. */
    495 	    IEEE80211_C_IBSS |		/* IBSS mode supported */
    496 	    IEEE80211_C_HOSTAP |	/* HostAp mode supported */
    497 	    IEEE80211_C_SHPREAMBLE |	/* Short preamble supported. */
    498 	    IEEE80211_C_SHSLOT |	/* Short slot time supported. */
    499 	    IEEE80211_C_WME |		/* 802.11e */
    500 	    IEEE80211_C_WPA;		/* 802.11i */
    501 
    502 	/* Set supported .11b and .11g rates. */
    503 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
    504 	ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
    505 
    506 	/* Set supported .11b and .11g channels (1 through 14). */
    507 	for (i = 1; i <= 14; i++) {
    508 		ic->ic_channels[i].ic_freq =
    509 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
    510 		ic->ic_channels[i].ic_flags =
    511 		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
    512 		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
    513 	}
    514 
    515 	ifp->if_softc = sc;
    516 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    517 	ifp->if_init = urtwn_init;
    518 	ifp->if_ioctl = urtwn_ioctl;
    519 	ifp->if_start = urtwn_start;
    520 	ifp->if_watchdog = urtwn_watchdog;
    521 	IFQ_SET_READY(&ifp->if_snd);
    522 	memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    523 
    524 	if_initialize(ifp);
    525 	ieee80211_ifattach(ic);
    526 
    527 	/* override default methods */
    528 	ic->ic_newassoc = urtwn_newassoc;
    529 	ic->ic_reset = urtwn_reset;
    530 	ic->ic_wme.wme_update = urtwn_wme_update;
    531 
    532 	/* Override state transition machine. */
    533 	sc->sc_newstate = ic->ic_newstate;
    534 	ic->ic_newstate = urtwn_newstate;
    535 
    536 	/* XXX media locking needs revisiting */
    537 	mutex_init(&sc->sc_media_mtx, MUTEX_DEFAULT, IPL_SOFTUSB);
    538 	ieee80211_media_init_with_lock(ic,
    539 	    urtwn_media_change, ieee80211_media_status, &sc->sc_media_mtx);
    540 
    541 	bpf_attach2(ifp, DLT_IEEE802_11_RADIO,
    542 	    sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
    543 	    &sc->sc_drvbpf);
    544 
    545 	sc->sc_rxtap_len = sizeof(sc->sc_rxtapu);
    546 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
    547 	sc->sc_rxtap.wr_ihdr.it_present = htole32(URTWN_RX_RADIOTAP_PRESENT);
    548 
    549 	sc->sc_txtap_len = sizeof(sc->sc_txtapu);
    550 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
    551 	sc->sc_txtap.wt_ihdr.it_present = htole32(URTWN_TX_RADIOTAP_PRESENT);
    552 
    553 	ifp->if_percpuq = if_percpuq_create(ifp);
    554 	if_register(ifp);
    555 
    556 	ieee80211_announce(ic);
    557 
    558 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    559 
    560 	if (!pmf_device_register(self, NULL, NULL))
    561 		aprint_error_dev(self, "couldn't establish power handler\n");
    562 
    563 	SET(sc->sc_flags, URTWN_FLAG_ATTACHED);
    564 	return;
    565 
    566  fail:
    567 	sc->sc_dying = 1;
    568 	aprint_error_dev(self, "attach failed\n");
    569 }
    570 
    571 static int
    572 urtwn_detach(device_t self, int flags)
    573 {
    574 	struct urtwn_softc *sc = device_private(self);
    575 	struct ifnet *ifp = &sc->sc_if;
    576 	int s;
    577 
    578 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
    579 
    580 	pmf_device_deregister(self);
    581 
    582 	s = splusb();
    583 
    584 	sc->sc_dying = 1;
    585 
    586 	callout_halt(&sc->sc_scan_to, NULL);
    587 	callout_halt(&sc->sc_calib_to, NULL);
    588 
    589 	if (ISSET(sc->sc_flags, URTWN_FLAG_ATTACHED)) {
    590 		urtwn_stop(ifp, 0);
    591 		usb_rem_task_wait(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER,
    592 		    NULL);
    593 
    594 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    595 		bpf_detach(ifp);
    596 		ieee80211_ifdetach(&sc->sc_ic);
    597 		if_detach(ifp);
    598 
    599 		mutex_destroy(&sc->sc_media_mtx);
    600 
    601 		/* Close Tx/Rx pipes.  Abort done by urtwn_stop. */
    602 		urtwn_close_pipes(sc);
    603 	}
    604 
    605 	splx(s);
    606 
    607 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    608 
    609 	rnd_detach_source(&sc->rnd_source);
    610 
    611 	callout_destroy(&sc->sc_scan_to);
    612 	callout_destroy(&sc->sc_calib_to);
    613 
    614 	cv_destroy(&sc->sc_task_cv);
    615 	mutex_destroy(&sc->sc_write_mtx);
    616 	mutex_destroy(&sc->sc_fwcmd_mtx);
    617 	mutex_destroy(&sc->sc_tx_mtx);
    618 	mutex_destroy(&sc->sc_rx_mtx);
    619 	mutex_destroy(&sc->sc_task_mtx);
    620 
    621 	return 0;
    622 }
    623 
    624 static int
    625 urtwn_activate(device_t self, enum devact act)
    626 {
    627 	struct urtwn_softc *sc = device_private(self);
    628 
    629 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
    630 
    631 	switch (act) {
    632 	case DVACT_DEACTIVATE:
    633 		if_deactivate(sc->sc_ic.ic_ifp);
    634 		return 0;
    635 	default:
    636 		return EOPNOTSUPP;
    637 	}
    638 }
    639 
    640 static int
    641 urtwn_open_pipes(struct urtwn_softc *sc)
    642 {
    643 	/* Bulk-out endpoints addresses (from highest to lowest prio). */
    644 	static uint8_t epaddr[R92C_MAX_EPOUT];
    645 	static uint8_t rxepaddr[R92C_MAX_EPIN];
    646 	usb_interface_descriptor_t *id;
    647 	usb_endpoint_descriptor_t *ed;
    648 	size_t i, ntx = 0, nrx = 0;
    649 	int error;
    650 
    651 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
    652 
    653 	/* Determine the number of bulk-out pipes. */
    654 	id = usbd_get_interface_descriptor(sc->sc_iface);
    655 	for (i = 0; i < id->bNumEndpoints; i++) {
    656 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    657 		if (ed == NULL || UE_GET_XFERTYPE(ed->bmAttributes) != UE_BULK) {
    658 			continue;
    659 		}
    660 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT) {
    661 			if (ntx < sizeof(epaddr))
    662 				epaddr[ntx] = ed->bEndpointAddress;
    663 			ntx++;
    664 		}
    665 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) {
    666 			if (nrx < sizeof(rxepaddr))
    667 				rxepaddr[nrx] = ed->bEndpointAddress;
    668 			nrx++;
    669 		}
    670 	}
    671 	if (nrx == 0 || nrx > R92C_MAX_EPIN) {
    672 		aprint_error_dev(sc->sc_dev,
    673 		    "%zd: invalid number of Rx bulk pipes\n", nrx);
    674 		return EIO;
    675 	}
    676 	if (ntx == 0 || ntx > R92C_MAX_EPOUT) {
    677 		aprint_error_dev(sc->sc_dev,
    678 		    "%zd: invalid number of Tx bulk pipes\n", ntx);
    679 		return EIO;
    680 	}
    681 	DPRINTFN(DBG_INIT, "found %jd/%jd bulk-in/out pipes",
    682 	    nrx, ntx, 0, 0);
    683 	sc->rx_npipe = nrx;
    684 	sc->tx_npipe = ntx;
    685 
    686 	/* Open bulk-in pipe at address 0x81. */
    687 	for (i = 0; i < nrx; i++) {
    688 		error = usbd_open_pipe(sc->sc_iface, rxepaddr[i],
    689 		    USBD_EXCLUSIVE_USE, &sc->rx_pipe[i]);
    690 		if (error != 0) {
    691 			aprint_error_dev(sc->sc_dev,
    692 			    "could not open Rx bulk pipe 0x%02x: %d\n",
    693 			    rxepaddr[i], error);
    694 			goto fail;
    695 		}
    696 	}
    697 
    698 	/* Open bulk-out pipes (up to 3). */
    699 	for (i = 0; i < ntx; i++) {
    700 		error = usbd_open_pipe(sc->sc_iface, epaddr[i],
    701 		    USBD_EXCLUSIVE_USE, &sc->tx_pipe[i]);
    702 		if (error != 0) {
    703 			aprint_error_dev(sc->sc_dev,
    704 			    "could not open Tx bulk pipe 0x%02x: %d\n",
    705 			    epaddr[i], error);
    706 			goto fail;
    707 		}
    708 	}
    709 
    710 	/* Map 802.11 access categories to USB pipes. */
    711 	sc->ac2idx[WME_AC_BK] =
    712 	sc->ac2idx[WME_AC_BE] = (ntx == 3) ? 2 : ((ntx == 2) ? 1 : 0);
    713 	sc->ac2idx[WME_AC_VI] = (ntx == 3) ? 1 : 0;
    714 	sc->ac2idx[WME_AC_VO] = 0;	/* Always use highest prio. */
    715 
    716  fail:
    717 	if (error != 0)
    718 		urtwn_close_pipes(sc);
    719 	return error;
    720 }
    721 
    722 static void
    723 urtwn_close_pipes(struct urtwn_softc *sc)
    724 {
    725 	struct usbd_pipe *pipe;
    726 	size_t i;
    727 
    728 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
    729 
    730 	/* Close Rx pipes. */
    731 	CTASSERT(sizeof(pipe) == sizeof(void *));
    732 	for (i = 0; i < sc->rx_npipe; i++) {
    733 		pipe = atomic_swap_ptr(&sc->rx_pipe[i], NULL);
    734 		if (pipe != NULL) {
    735 			usbd_close_pipe(pipe);
    736 		}
    737 	}
    738 
    739 	/* Close Tx pipes. */
    740 	for (i = 0; i < sc->tx_npipe; i++) {
    741 		pipe = atomic_swap_ptr(&sc->tx_pipe[i], NULL);
    742 		if (pipe != NULL) {
    743 			usbd_close_pipe(pipe);
    744 		}
    745 	}
    746 }
    747 
    748 static int __noinline
    749 urtwn_alloc_rx_list(struct urtwn_softc *sc)
    750 {
    751 	struct urtwn_rx_data *data;
    752 	size_t i;
    753 	int error = 0;
    754 
    755 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
    756 
    757 	for (size_t j = 0; j < sc->rx_npipe; j++) {
    758 		TAILQ_INIT(&sc->rx_free_list[j]);
    759 		for (i = 0; i < URTWN_RX_LIST_COUNT; i++) {
    760 			data = &sc->rx_data[j][i];
    761 
    762 			data->sc = sc;	/* Backpointer for callbacks. */
    763 
    764 			error = usbd_create_xfer(sc->rx_pipe[j], URTWN_RXBUFSZ,
    765 			    0, 0, &data->xfer);
    766 			if (error) {
    767 				aprint_error_dev(sc->sc_dev,
    768 				    "could not allocate xfer\n");
    769 				break;
    770 			}
    771 
    772 			data->buf = usbd_get_buffer(data->xfer);
    773 			TAILQ_INSERT_TAIL(&sc->rx_free_list[j], data, next);
    774 		}
    775 	}
    776 	if (error != 0)
    777 		urtwn_free_rx_list(sc);
    778 	return error;
    779 }
    780 
    781 static void
    782 urtwn_free_rx_list(struct urtwn_softc *sc)
    783 {
    784 	struct usbd_xfer *xfer;
    785 	size_t i;
    786 
    787 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
    788 
    789 	/* NB: Caller must abort pipe first. */
    790 	for (size_t j = 0; j < sc->rx_npipe; j++) {
    791 		for (i = 0; i < URTWN_RX_LIST_COUNT; i++) {
    792 			CTASSERT(sizeof(xfer) == sizeof(void *));
    793 			xfer = atomic_swap_ptr(&sc->rx_data[j][i].xfer, NULL);
    794 			if (xfer != NULL)
    795 				usbd_destroy_xfer(xfer);
    796 		}
    797 	}
    798 }
    799 
    800 static int __noinline
    801 urtwn_alloc_tx_list(struct urtwn_softc *sc)
    802 {
    803 	struct urtwn_tx_data *data;
    804 	size_t i;
    805 	int error = 0;
    806 
    807 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
    808 
    809 	mutex_enter(&sc->sc_tx_mtx);
    810 	for (size_t j = 0; j < sc->tx_npipe; j++) {
    811 		TAILQ_INIT(&sc->tx_free_list[j]);
    812 		for (i = 0; i < URTWN_TX_LIST_COUNT; i++) {
    813 			data = &sc->tx_data[j][i];
    814 
    815 			data->sc = sc;	/* Backpointer for callbacks. */
    816 			data->pidx = j;
    817 
    818 			error = usbd_create_xfer(sc->tx_pipe[j],
    819 			    URTWN_TXBUFSZ, USBD_FORCE_SHORT_XFER, 0,
    820 			    &data->xfer);
    821 			if (error) {
    822 				aprint_error_dev(sc->sc_dev,
    823 				    "could not allocate xfer\n");
    824 				goto fail;
    825 			}
    826 
    827 			data->buf = usbd_get_buffer(data->xfer);
    828 
    829 			/* Append this Tx buffer to our free list. */
    830 			TAILQ_INSERT_TAIL(&sc->tx_free_list[j], data, next);
    831 		}
    832 	}
    833 	mutex_exit(&sc->sc_tx_mtx);
    834 	return 0;
    835 
    836  fail:
    837 	urtwn_free_tx_list(sc);
    838 	mutex_exit(&sc->sc_tx_mtx);
    839 	return error;
    840 }
    841 
    842 static void
    843 urtwn_free_tx_list(struct urtwn_softc *sc)
    844 {
    845 	struct usbd_xfer *xfer;
    846 	size_t i;
    847 
    848 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
    849 
    850 	/* NB: Caller must abort pipe first. */
    851 	for (size_t j = 0; j < sc->tx_npipe; j++) {
    852 		for (i = 0; i < URTWN_TX_LIST_COUNT; i++) {
    853 			CTASSERT(sizeof(xfer) == sizeof(void *));
    854 			xfer = atomic_swap_ptr(&sc->tx_data[j][i].xfer, NULL);
    855 			if (xfer != NULL)
    856 				usbd_destroy_xfer(xfer);
    857 		}
    858 	}
    859 }
    860 
    861 static int
    862 urtwn_tx_beacon(struct urtwn_softc *sc, struct mbuf *m,
    863     struct ieee80211_node *ni)
    864 {
    865 	struct urtwn_tx_data *data =
    866 	    urtwn_get_tx_data(sc, sc->ac2idx[WME_AC_VO]);
    867 
    868 	if (data == NULL)
    869 		return ENOBUFS;
    870 
    871 	return urtwn_tx(sc, m, ni, data);
    872 }
    873 
    874 static void
    875 urtwn_cmdq_invariants(struct urtwn_softc *sc)
    876 {
    877 	struct urtwn_host_cmd_ring *const ring = &sc->cmdq;
    878 
    879 	KASSERT(mutex_owned(&sc->sc_task_mtx));
    880 	KASSERTMSG((ring->cur >= 0 && ring->cur < URTWN_HOST_CMD_RING_COUNT),
    881 	    "%s: cur=%d next=%d queued=%d",
    882 	    device_xname(sc->sc_dev), ring->cur, ring->next, ring->queued);
    883 	KASSERTMSG((ring->next >= 0 && ring->next < URTWN_HOST_CMD_RING_COUNT),
    884 	    "%s: cur=%d next=%d queued=%d",
    885 	    device_xname(sc->sc_dev), ring->cur, ring->next, ring->queued);
    886 	KASSERTMSG((ring->queued >= 0 &&
    887 		ring->queued <= URTWN_HOST_CMD_RING_COUNT),
    888 	    "%s: %d commands queued",
    889 	    device_xname(sc->sc_dev), ring->queued);
    890 }
    891 
    892 static void
    893 urtwn_task(void *arg)
    894 {
    895 	struct urtwn_softc *sc = arg;
    896 	struct ieee80211com *ic = &sc->sc_ic;
    897 	struct urtwn_host_cmd_ring *ring = &sc->cmdq;
    898 	struct urtwn_host_cmd *cmd;
    899 	int s;
    900 
    901 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
    902 	if (ic->ic_state == IEEE80211_S_RUN &&
    903 	    (ic->ic_opmode == IEEE80211_M_HOSTAP ||
    904 	    ic->ic_opmode == IEEE80211_M_IBSS)) {
    905 
    906 		struct mbuf *m = ieee80211_beacon_alloc(ic, ic->ic_bss,
    907 		    &sc->sc_bo);
    908 		if (m == NULL) {
    909 			aprint_error_dev(sc->sc_dev,
    910 			    "could not allocate beacon");
    911 		}
    912 
    913 		if (urtwn_tx_beacon(sc, m, ic->ic_bss) != 0) {
    914 			aprint_error_dev(sc->sc_dev, "could not send beacon\n");
    915 		}
    916 
    917 		/* beacon is no longer needed */
    918 		m_freem(m);
    919 	}
    920 
    921 	/* Process host commands. */
    922 	s = splusb();
    923 	mutex_spin_enter(&sc->sc_task_mtx);
    924 	urtwn_cmdq_invariants(sc);
    925 	while (ring->next != ring->cur) {
    926 		KASSERTMSG(ring->queued > 0, "%s: cur=%d next=%d queued=%d",
    927 		    device_xname(sc->sc_dev),
    928 		    ring->cur, ring->next, ring->queued);
    929 		cmd = &ring->cmd[ring->next];
    930 		mutex_spin_exit(&sc->sc_task_mtx);
    931 		splx(s);
    932 		/* Invoke callback with kernel lock held. */
    933 		cmd->cb(sc, cmd->data);
    934 		s = splusb();
    935 		mutex_spin_enter(&sc->sc_task_mtx);
    936 		urtwn_cmdq_invariants(sc);
    937 		KASSERTMSG(ring->queued > 0, "%s: cur=%d next=%d queued=%d",
    938 		    device_xname(sc->sc_dev),
    939 		    ring->cur, ring->next, ring->queued);
    940 		ring->queued--;
    941 		ring->next = (ring->next + 1) % URTWN_HOST_CMD_RING_COUNT;
    942 	}
    943 	cv_broadcast(&sc->sc_task_cv);
    944 	mutex_spin_exit(&sc->sc_task_mtx);
    945 	splx(s);
    946 }
    947 
    948 static void
    949 urtwn_do_async(struct urtwn_softc *sc, void (*cb)(struct urtwn_softc *, void *),
    950     void *arg, int len)
    951 {
    952 	struct urtwn_host_cmd_ring *ring = &sc->cmdq;
    953 	struct urtwn_host_cmd *cmd;
    954 	bool schedtask = false;
    955 	int s;
    956 
    957 	URTWNHIST_FUNC();
    958 	URTWNHIST_CALLARGS("cb=%#jx, arg=%#jx, len=%jd",
    959 	    (uintptr_t)cb, (uintptr_t)arg, len, 0);
    960 
    961 	s = splusb();
    962 	mutex_spin_enter(&sc->sc_task_mtx);
    963 	urtwn_cmdq_invariants(sc);
    964 	cmd = &ring->cmd[ring->cur];
    965 	cmd->cb = cb;
    966 	KASSERT(len <= sizeof(cmd->data));
    967 	memcpy(cmd->data, arg, len);
    968 	ring->cur = (ring->cur + 1) % URTWN_HOST_CMD_RING_COUNT;
    969 
    970 	/*
    971 	 * Schedule a task to process the command if need be.
    972 	 */
    973 	if (!sc->sc_dying) {
    974 		if (ring->queued == URTWN_HOST_CMD_RING_COUNT)
    975 			device_printf(sc->sc_dev, "command queue overflow\n");
    976 		else if (ring->queued++ == 0)
    977 			schedtask = true;
    978 	}
    979 	mutex_spin_exit(&sc->sc_task_mtx);
    980 	splx(s);
    981 
    982 	if (schedtask)
    983 		usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
    984 }
    985 
    986 static void
    987 urtwn_wait_async(struct urtwn_softc *sc)
    988 {
    989 
    990 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
    991 
    992 	/* Wait for all queued asynchronous commands to complete. */
    993 	mutex_spin_enter(&sc->sc_task_mtx);
    994 	while (sc->cmdq.queued > 0)
    995 		cv_wait(&sc->sc_task_cv, &sc->sc_task_mtx);
    996 	mutex_spin_exit(&sc->sc_task_mtx);
    997 }
    998 
    999 static int
   1000 urtwn_write_region_1(struct urtwn_softc *sc, uint16_t addr, uint8_t *buf,
   1001     int len)
   1002 {
   1003 	usb_device_request_t req;
   1004 	usbd_status error;
   1005 
   1006 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1007 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   1008 
   1009 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
   1010 	req.bRequest = R92C_REQ_REGS;
   1011 	USETW(req.wValue, addr);
   1012 	USETW(req.wIndex, 0);
   1013 	USETW(req.wLength, len);
   1014 	error = usbd_do_request(sc->sc_udev, &req, buf);
   1015 	if (error != USBD_NORMAL_COMPLETION) {
   1016 		DPRINTFN(DBG_REG, "error=%jd: addr=%#jx, len=%jd",
   1017 		    error, addr, len, 0);
   1018 	}
   1019 	return error;
   1020 }
   1021 
   1022 static void
   1023 urtwn_write_1(struct urtwn_softc *sc, uint16_t addr, uint8_t val)
   1024 {
   1025 
   1026 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1027 	DPRINTFN(DBG_REG, "addr=%#jx, val=%#jx", addr, val, 0, 0);
   1028 
   1029 	urtwn_write_region_1(sc, addr, &val, 1);
   1030 }
   1031 
   1032 static void
   1033 urtwn_write_2(struct urtwn_softc *sc, uint16_t addr, uint16_t val)
   1034 {
   1035 	uint8_t buf[2];
   1036 
   1037 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1038 	DPRINTFN(DBG_REG, "addr=%#jx, val=%#jx", addr, val, 0, 0);
   1039 
   1040 	buf[0] = (uint8_t)val;
   1041 	buf[1] = (uint8_t)(val >> 8);
   1042 	urtwn_write_region_1(sc, addr, buf, 2);
   1043 }
   1044 
   1045 static void
   1046 urtwn_write_4(struct urtwn_softc *sc, uint16_t addr, uint32_t val)
   1047 {
   1048 	uint8_t buf[4];
   1049 
   1050 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1051 	DPRINTFN(DBG_REG, "addr=%#jx, val=%#jx", addr, val, 0, 0);
   1052 
   1053 	buf[0] = (uint8_t)val;
   1054 	buf[1] = (uint8_t)(val >> 8);
   1055 	buf[2] = (uint8_t)(val >> 16);
   1056 	buf[3] = (uint8_t)(val >> 24);
   1057 	urtwn_write_region_1(sc, addr, buf, 4);
   1058 }
   1059 
   1060 static int
   1061 urtwn_write_region(struct urtwn_softc *sc, uint16_t addr, uint8_t *buf, int len)
   1062 {
   1063 
   1064 	URTWNHIST_FUNC();
   1065 	URTWNHIST_CALLARGS("addr=%#jx, len=%#jx", addr, len, 0, 0);
   1066 
   1067 	return urtwn_write_region_1(sc, addr, buf, len);
   1068 }
   1069 
   1070 static int
   1071 urtwn_read_region_1(struct urtwn_softc *sc, uint16_t addr, uint8_t *buf,
   1072     int len)
   1073 {
   1074 	usb_device_request_t req;
   1075 	usbd_status error;
   1076 
   1077 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1078 
   1079 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
   1080 	req.bRequest = R92C_REQ_REGS;
   1081 	USETW(req.wValue, addr);
   1082 	USETW(req.wIndex, 0);
   1083 	USETW(req.wLength, len);
   1084 	error = usbd_do_request(sc->sc_udev, &req, buf);
   1085 	if (error != USBD_NORMAL_COMPLETION) {
   1086 		DPRINTFN(DBG_REG, "error=%jd: addr=%#jx, len=%jd",
   1087 		    error, addr, len, 0);
   1088 	}
   1089 	return error;
   1090 }
   1091 
   1092 static uint8_t
   1093 urtwn_read_1(struct urtwn_softc *sc, uint16_t addr)
   1094 {
   1095 	uint8_t val;
   1096 
   1097 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1098 
   1099 	if (urtwn_read_region_1(sc, addr, &val, 1) != USBD_NORMAL_COMPLETION)
   1100 		return 0xff;
   1101 
   1102 	DPRINTFN(DBG_REG, "addr=%#jx, val=%#jx", addr, val, 0, 0);
   1103 	return val;
   1104 }
   1105 
   1106 static uint16_t
   1107 urtwn_read_2(struct urtwn_softc *sc, uint16_t addr)
   1108 {
   1109 	uint8_t buf[2];
   1110 	uint16_t val;
   1111 
   1112 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1113 
   1114 	if (urtwn_read_region_1(sc, addr, buf, 2) != USBD_NORMAL_COMPLETION)
   1115 		return 0xffff;
   1116 
   1117 	val = LE_READ_2(&buf[0]);
   1118 	DPRINTFN(DBG_REG, "addr=%#jx, val=%#jx", addr, val, 0, 0);
   1119 	return val;
   1120 }
   1121 
   1122 static uint32_t
   1123 urtwn_read_4(struct urtwn_softc *sc, uint16_t addr)
   1124 {
   1125 	uint8_t buf[4];
   1126 	uint32_t val;
   1127 
   1128 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1129 
   1130 	if (urtwn_read_region_1(sc, addr, buf, 4) != USBD_NORMAL_COMPLETION)
   1131 		return 0xffffffff;
   1132 
   1133 	val = LE_READ_4(&buf[0]);
   1134 	DPRINTFN(DBG_REG, "addr=%#jx, val=%#jx", addr, val, 0, 0);
   1135 	return val;
   1136 }
   1137 
   1138 static int
   1139 urtwn_fw_cmd(struct urtwn_softc *sc, uint8_t id, const void *buf, int len)
   1140 {
   1141 	struct r92c_fw_cmd cmd;
   1142 	uint8_t *cp;
   1143 	int fwcur;
   1144 	int ntries;
   1145 
   1146 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1147 	DPRINTFN(DBG_REG, "id=%jd, buf=%#jx, len=%jd", id, (uintptr_t)buf, len, 0);
   1148 
   1149 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   1150 
   1151 	mutex_enter(&sc->sc_fwcmd_mtx);
   1152 	fwcur = sc->fwcur;
   1153 	sc->fwcur = (sc->fwcur + 1) % R92C_H2C_NBOX;
   1154 
   1155 	/* Wait for current FW box to be empty. */
   1156 	for (ntries = 0; ntries < 100; ntries++) {
   1157 		if (!(urtwn_read_1(sc, R92C_HMETFR) & (1 << fwcur)))
   1158 			break;
   1159 		urtwn_delay_ms(sc, 2);
   1160 	}
   1161 	if (ntries == 100) {
   1162 		aprint_error_dev(sc->sc_dev,
   1163 		    "could not send firmware command %d\n", id);
   1164 		mutex_exit(&sc->sc_fwcmd_mtx);
   1165 		return ETIMEDOUT;
   1166 	}
   1167 
   1168 	memset(&cmd, 0, sizeof(cmd));
   1169 	KASSERT(len <= sizeof(cmd.msg));
   1170 	memcpy(cmd.msg, buf, len);
   1171 
   1172 	/* Write the first word last since that will trigger the FW. */
   1173 	cp = (uint8_t *)&cmd;
   1174 	cmd.id = id;
   1175 	if (len >= 4) {
   1176 		if (!ISSET(sc->chip, URTWN_CHIP_92EU)) {
   1177 			cmd.id |= R92C_CMD_FLAG_EXT;
   1178 			urtwn_write_region(sc, R92C_HMEBOX_EXT(fwcur),
   1179 			    &cp[1], 2);
   1180 			urtwn_write_4(sc, R92C_HMEBOX(fwcur),
   1181 			    cp[0] + (cp[3] << 8) + (cp[4] << 16) +
   1182 			    ((uint32_t)cp[5] << 24));
   1183 		} else {
   1184 			urtwn_write_region(sc, R92E_HMEBOX_EXT(fwcur),
   1185 			    &cp[4], 2);
   1186 			urtwn_write_4(sc, R92C_HMEBOX(fwcur),
   1187 			    cp[0] + (cp[1] << 8) + (cp[2] << 16) +
   1188 			    ((uint32_t)cp[3] << 24));
   1189 		}
   1190 	} else {
   1191 		urtwn_write_region(sc, R92C_HMEBOX(fwcur), cp, len);
   1192 	}
   1193 	mutex_exit(&sc->sc_fwcmd_mtx);
   1194 
   1195 	return 0;
   1196 }
   1197 
   1198 static __inline void
   1199 urtwn_rf_write(struct urtwn_softc *sc, int chain, uint8_t addr, uint32_t val)
   1200 {
   1201 
   1202 	sc->sc_rf_write(sc, chain, addr, val);
   1203 }
   1204 
   1205 static void
   1206 urtwn_r92c_rf_write(struct urtwn_softc *sc, int chain, uint8_t addr,
   1207     uint32_t val)
   1208 {
   1209 
   1210 	urtwn_bb_write(sc, R92C_LSSI_PARAM(chain),
   1211 	    SM(R92C_LSSI_PARAM_ADDR, addr) | SM(R92C_LSSI_PARAM_DATA, val));
   1212 }
   1213 
   1214 static void
   1215 urtwn_r88e_rf_write(struct urtwn_softc *sc, int chain, uint8_t addr,
   1216     uint32_t val)
   1217 {
   1218 
   1219 	urtwn_bb_write(sc, R92C_LSSI_PARAM(chain),
   1220 	    SM(R88E_LSSI_PARAM_ADDR, addr) | SM(R92C_LSSI_PARAM_DATA, val));
   1221 }
   1222 
   1223 static void
   1224 urtwn_r92e_rf_write(struct urtwn_softc *sc, int chain, uint8_t addr,
   1225     uint32_t val)
   1226 {
   1227 
   1228 	urtwn_bb_write(sc, R92C_LSSI_PARAM(chain),
   1229 	    SM(R88E_LSSI_PARAM_ADDR, addr) | SM(R92C_LSSI_PARAM_DATA, val));
   1230 }
   1231 
   1232 static uint32_t
   1233 urtwn_rf_read(struct urtwn_softc *sc, int chain, uint8_t addr)
   1234 {
   1235 	uint32_t reg[R92C_MAX_CHAINS], val;
   1236 
   1237 	reg[0] = urtwn_bb_read(sc, R92C_HSSI_PARAM2(0));
   1238 	if (chain != 0) {
   1239 		reg[chain] = urtwn_bb_read(sc, R92C_HSSI_PARAM2(chain));
   1240 	}
   1241 
   1242 	urtwn_bb_write(sc, R92C_HSSI_PARAM2(0),
   1243 	    reg[0] & ~R92C_HSSI_PARAM2_READ_EDGE);
   1244 	urtwn_delay_ms(sc, 1);
   1245 
   1246 	urtwn_bb_write(sc, R92C_HSSI_PARAM2(chain),
   1247 	    RW(reg[chain], R92C_HSSI_PARAM2_READ_ADDR, addr) |
   1248 	    R92C_HSSI_PARAM2_READ_EDGE);
   1249 	urtwn_delay_ms(sc, 1);
   1250 
   1251 	urtwn_bb_write(sc, R92C_HSSI_PARAM2(0),
   1252 	    reg[0] | R92C_HSSI_PARAM2_READ_EDGE);
   1253 	urtwn_delay_ms(sc, 1);
   1254 
   1255 	if (urtwn_bb_read(sc, R92C_HSSI_PARAM1(chain)) & R92C_HSSI_PARAM1_PI) {
   1256 		val = urtwn_bb_read(sc, R92C_HSPI_READBACK(chain));
   1257 	} else {
   1258 		val = urtwn_bb_read(sc, R92C_LSSI_READBACK(chain));
   1259 	}
   1260 	return MS(val, R92C_LSSI_READBACK_DATA);
   1261 }
   1262 
   1263 static int
   1264 urtwn_llt_write(struct urtwn_softc *sc, uint32_t addr, uint32_t data)
   1265 {
   1266 	int ntries;
   1267 
   1268 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   1269 
   1270 	urtwn_write_4(sc, R92C_LLT_INIT,
   1271 	    SM(R92C_LLT_INIT_OP, R92C_LLT_INIT_OP_WRITE) |
   1272 	    SM(R92C_LLT_INIT_ADDR, addr) |
   1273 	    SM(R92C_LLT_INIT_DATA, data));
   1274 	/* Wait for write operation to complete. */
   1275 	for (ntries = 0; ntries < 20; ntries++) {
   1276 		if (MS(urtwn_read_4(sc, R92C_LLT_INIT), R92C_LLT_INIT_OP) ==
   1277 		    R92C_LLT_INIT_OP_NO_ACTIVE) {
   1278 			/* Done */
   1279 			return 0;
   1280 		}
   1281 		DELAY(5);
   1282 	}
   1283 	return ETIMEDOUT;
   1284 }
   1285 
   1286 static uint8_t
   1287 urtwn_efuse_read_1(struct urtwn_softc *sc, uint16_t addr)
   1288 {
   1289 	uint32_t reg;
   1290 	int ntries;
   1291 
   1292 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   1293 
   1294 	reg = urtwn_read_4(sc, R92C_EFUSE_CTRL);
   1295 	reg = RW(reg, R92C_EFUSE_CTRL_ADDR, addr);
   1296 	reg &= ~R92C_EFUSE_CTRL_VALID;
   1297 	urtwn_write_4(sc, R92C_EFUSE_CTRL, reg);
   1298 
   1299 	/* Wait for read operation to complete. */
   1300 	for (ntries = 0; ntries < 100; ntries++) {
   1301 		reg = urtwn_read_4(sc, R92C_EFUSE_CTRL);
   1302 		if (reg & R92C_EFUSE_CTRL_VALID) {
   1303 			/* Done */
   1304 			return MS(reg, R92C_EFUSE_CTRL_DATA);
   1305 		}
   1306 		DELAY(5);
   1307 	}
   1308 	aprint_error_dev(sc->sc_dev,
   1309 	    "could not read efuse byte at address 0x%04x\n", addr);
   1310 	return 0xff;
   1311 }
   1312 
   1313 static void
   1314 urtwn_efuse_read(struct urtwn_softc *sc)
   1315 {
   1316 	uint8_t *rom = (uint8_t *)&sc->rom;
   1317 	uint32_t reg;
   1318 	uint16_t addr = 0;
   1319 	uint8_t off, msk;
   1320 	size_t i;
   1321 
   1322 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1323 
   1324 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   1325 
   1326 	urtwn_efuse_switch_power(sc);
   1327 
   1328 	memset(&sc->rom, 0xff, sizeof(sc->rom));
   1329 	while (addr < 512) {
   1330 		reg = urtwn_efuse_read_1(sc, addr);
   1331 		if (reg == 0xff)
   1332 			break;
   1333 		addr++;
   1334 		off = reg >> 4;
   1335 		msk = reg & 0xf;
   1336 		for (i = 0; i < 4; i++) {
   1337 			if (msk & (1U << i))
   1338 				continue;
   1339 
   1340 			rom[off * 8 + i * 2 + 0] = urtwn_efuse_read_1(sc, addr);
   1341 			addr++;
   1342 			rom[off * 8 + i * 2 + 1] = urtwn_efuse_read_1(sc, addr);
   1343 			addr++;
   1344 		}
   1345 	}
   1346 #ifdef URTWN_DEBUG
   1347 	/* Dump ROM content. */
   1348 	for (i = 0; i < (int)sizeof(sc->rom); i++)
   1349 		DPRINTFN(DBG_INIT, "%04jx: %02jx", i, rom[i], 0, 0);
   1350 #endif
   1351 }
   1352 
   1353 static void
   1354 urtwn_efuse_switch_power(struct urtwn_softc *sc)
   1355 {
   1356 	uint32_t reg;
   1357 
   1358 	reg = urtwn_read_2(sc, R92C_SYS_ISO_CTRL);
   1359 	if (!(reg & R92C_SYS_ISO_CTRL_PWC_EV12V)) {
   1360 		urtwn_write_2(sc, R92C_SYS_ISO_CTRL,
   1361 		    reg | R92C_SYS_ISO_CTRL_PWC_EV12V);
   1362 	}
   1363 	reg = urtwn_read_2(sc, R92C_SYS_FUNC_EN);
   1364 	if (!(reg & R92C_SYS_FUNC_EN_ELDR)) {
   1365 		urtwn_write_2(sc, R92C_SYS_FUNC_EN,
   1366 		    reg | R92C_SYS_FUNC_EN_ELDR);
   1367 	}
   1368 	reg = urtwn_read_2(sc, R92C_SYS_CLKR);
   1369 	if ((reg & (R92C_SYS_CLKR_LOADER_EN | R92C_SYS_CLKR_ANA8M)) !=
   1370 	    (R92C_SYS_CLKR_LOADER_EN | R92C_SYS_CLKR_ANA8M)) {
   1371 		urtwn_write_2(sc, R92C_SYS_CLKR,
   1372 		    reg | R92C_SYS_CLKR_LOADER_EN | R92C_SYS_CLKR_ANA8M);
   1373 	}
   1374 }
   1375 
   1376 static int
   1377 urtwn_read_chipid(struct urtwn_softc *sc)
   1378 {
   1379 	uint32_t reg;
   1380 
   1381 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1382 
   1383 	if (ISSET(sc->chip, URTWN_CHIP_88E) ||
   1384 	    ISSET(sc->chip, URTWN_CHIP_92EU))
   1385 		return 0;
   1386 
   1387 	reg = urtwn_read_4(sc, R92C_SYS_CFG);
   1388 	if (reg & R92C_SYS_CFG_TRP_VAUX_EN) {
   1389 		/* test chip, not supported */
   1390 		return EIO;
   1391 	}
   1392 	if (reg & R92C_SYS_CFG_TYPE_92C) {
   1393 		sc->chip |= URTWN_CHIP_92C;
   1394 		/* Check if it is a castrated 8192C. */
   1395 		if (MS(urtwn_read_4(sc, R92C_HPON_FSM),
   1396 		    R92C_HPON_FSM_CHIP_BONDING_ID) ==
   1397 		    R92C_HPON_FSM_CHIP_BONDING_ID_92C_1T2R) {
   1398 			sc->chip |= URTWN_CHIP_92C_1T2R;
   1399 		}
   1400 	}
   1401 	if (reg & R92C_SYS_CFG_VENDOR_UMC) {
   1402 		sc->chip |= URTWN_CHIP_UMC;
   1403 		if (MS(reg, R92C_SYS_CFG_CHIP_VER_RTL) == 0) {
   1404 			sc->chip |= URTWN_CHIP_UMC_A_CUT;
   1405 		}
   1406 	}
   1407 	return 0;
   1408 }
   1409 
   1410 #ifdef URTWN_DEBUG
   1411 static void
   1412 urtwn_dump_rom(struct urtwn_softc *sc, struct r92c_rom *rp)
   1413 {
   1414 
   1415 	aprint_normal_dev(sc->sc_dev,
   1416 	    "id 0x%04x, dbg_sel %#x, vid %#x, pid %#x\n",
   1417 	    rp->id, rp->dbg_sel, rp->vid, rp->pid);
   1418 
   1419 	aprint_normal_dev(sc->sc_dev,
   1420 	    "usb_opt %#x, ep_setting %#x, usb_phy %#x\n",
   1421 	    rp->usb_opt, rp->ep_setting, rp->usb_phy);
   1422 
   1423 	aprint_normal_dev(sc->sc_dev,
   1424 	    "macaddr %s\n",
   1425 	    ether_sprintf(rp->macaddr));
   1426 
   1427 	aprint_normal_dev(sc->sc_dev,
   1428 	    "string %s, subcustomer_id %#x\n",
   1429 	    rp->string, rp->subcustomer_id);
   1430 
   1431 	aprint_normal_dev(sc->sc_dev,
   1432 	    "cck_tx_pwr c0: %d %d %d, c1: %d %d %d\n",
   1433 	    rp->cck_tx_pwr[0][0], rp->cck_tx_pwr[0][1], rp->cck_tx_pwr[0][2],
   1434 	    rp->cck_tx_pwr[1][0], rp->cck_tx_pwr[1][1], rp->cck_tx_pwr[1][2]);
   1435 
   1436 	aprint_normal_dev(sc->sc_dev,
   1437 	    "ht40_1s_tx_pwr c0 %d %d %d, c1 %d %d %d\n",
   1438 	    rp->ht40_1s_tx_pwr[0][0], rp->ht40_1s_tx_pwr[0][1],
   1439 	    rp->ht40_1s_tx_pwr[0][2],
   1440 	    rp->ht40_1s_tx_pwr[1][0], rp->ht40_1s_tx_pwr[1][1],
   1441 	    rp->ht40_1s_tx_pwr[1][2]);
   1442 
   1443 	aprint_normal_dev(sc->sc_dev,
   1444 	    "ht40_2s_tx_pwr_diff c0: %d %d %d, c1: %d %d %d\n",
   1445 	    rp->ht40_2s_tx_pwr_diff[0] & 0xf, rp->ht40_2s_tx_pwr_diff[1] & 0xf,
   1446 	    rp->ht40_2s_tx_pwr_diff[2] & 0xf,
   1447 	    rp->ht40_2s_tx_pwr_diff[0] >> 4, rp->ht40_2s_tx_pwr_diff[1] & 0xf,
   1448 	    rp->ht40_2s_tx_pwr_diff[2] >> 4);
   1449 
   1450 	aprint_normal_dev(sc->sc_dev,
   1451 	    "ht20_tx_pwr_diff c0: %d %d %d, c1: %d %d %d\n",
   1452 	    rp->ht20_tx_pwr_diff[0] & 0xf, rp->ht20_tx_pwr_diff[1] & 0xf,
   1453 	    rp->ht20_tx_pwr_diff[2] & 0xf,
   1454 	    rp->ht20_tx_pwr_diff[0] >> 4, rp->ht20_tx_pwr_diff[1] >> 4,
   1455 	    rp->ht20_tx_pwr_diff[2] >> 4);
   1456 
   1457 	aprint_normal_dev(sc->sc_dev,
   1458 	    "ofdm_tx_pwr_diff c0: %d %d %d, c1: %d %d %d\n",
   1459 	    rp->ofdm_tx_pwr_diff[0] & 0xf, rp->ofdm_tx_pwr_diff[1] & 0xf,
   1460 	    rp->ofdm_tx_pwr_diff[2] & 0xf,
   1461 	    rp->ofdm_tx_pwr_diff[0] >> 4, rp->ofdm_tx_pwr_diff[1] >> 4,
   1462 	    rp->ofdm_tx_pwr_diff[2] >> 4);
   1463 
   1464 	aprint_normal_dev(sc->sc_dev,
   1465 	    "ht40_max_pwr_offset c0: %d %d %d, c1: %d %d %d\n",
   1466 	    rp->ht40_max_pwr[0] & 0xf, rp->ht40_max_pwr[1] & 0xf,
   1467 	    rp->ht40_max_pwr[2] & 0xf,
   1468 	    rp->ht40_max_pwr[0] >> 4, rp->ht40_max_pwr[1] >> 4,
   1469 	    rp->ht40_max_pwr[2] >> 4);
   1470 
   1471 	aprint_normal_dev(sc->sc_dev,
   1472 	    "ht20_max_pwr_offset c0: %d %d %d, c1: %d %d %d\n",
   1473 	    rp->ht20_max_pwr[0] & 0xf, rp->ht20_max_pwr[1] & 0xf,
   1474 	    rp->ht20_max_pwr[2] & 0xf,
   1475 	    rp->ht20_max_pwr[0] >> 4, rp->ht20_max_pwr[1] >> 4,
   1476 	    rp->ht20_max_pwr[2] >> 4);
   1477 
   1478 	aprint_normal_dev(sc->sc_dev,
   1479 	    "xtal_calib %d, tssi %d %d, thermal %d\n",
   1480 	    rp->xtal_calib, rp->tssi[0], rp->tssi[1], rp->thermal_meter);
   1481 
   1482 	aprint_normal_dev(sc->sc_dev,
   1483 	    "rf_opt1 %#x, rf_opt2 %#x, rf_opt3 %#x, rf_opt4 %#x\n",
   1484 	    rp->rf_opt1, rp->rf_opt2, rp->rf_opt3, rp->rf_opt4);
   1485 
   1486 	aprint_normal_dev(sc->sc_dev,
   1487 	    "channnel_plan %d, version %d customer_id %#x\n",
   1488 	    rp->channel_plan, rp->version, rp->curstomer_id);
   1489 }
   1490 #endif
   1491 
   1492 static void
   1493 urtwn_read_rom(struct urtwn_softc *sc)
   1494 {
   1495 	struct ieee80211com *ic = &sc->sc_ic;
   1496 	struct r92c_rom *rom = &sc->rom;
   1497 
   1498 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1499 
   1500 	mutex_enter(&sc->sc_write_mtx);
   1501 
   1502 	/* Read full ROM image. */
   1503 	urtwn_efuse_read(sc);
   1504 #ifdef URTWN_DEBUG
   1505 	if (urtwn_debug & DBG_REG)
   1506 		urtwn_dump_rom(sc, rom);
   1507 #endif
   1508 
   1509 	/* XXX Weird but this is what the vendor driver does. */
   1510 	sc->pa_setting = urtwn_efuse_read_1(sc, 0x1fa);
   1511 	sc->board_type = MS(rom->rf_opt1, R92C_ROM_RF1_BOARD_TYPE);
   1512 	sc->regulatory = MS(rom->rf_opt1, R92C_ROM_RF1_REGULATORY);
   1513 
   1514 	DPRINTFN(DBG_INIT,
   1515 	    "PA setting=%#jx, board=%#jx, regulatory=%jd",
   1516 	    sc->pa_setting, sc->board_type, sc->regulatory, 0);
   1517 
   1518 	IEEE80211_ADDR_COPY(ic->ic_myaddr, rom->macaddr);
   1519 
   1520 	sc->sc_rf_write = urtwn_r92c_rf_write;
   1521 	sc->sc_power_on = urtwn_r92c_power_on;
   1522 	sc->sc_dma_init = urtwn_r92c_dma_init;
   1523 
   1524 	mutex_exit(&sc->sc_write_mtx);
   1525 }
   1526 
   1527 static void
   1528 urtwn_r88e_read_rom(struct urtwn_softc *sc)
   1529 {
   1530 	struct ieee80211com *ic = &sc->sc_ic;
   1531 	uint8_t *rom = sc->r88e_rom;
   1532 	uint32_t reg;
   1533 	uint16_t addr = 0;
   1534 	uint8_t off, msk, tmp;
   1535 	int i;
   1536 
   1537 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1538 
   1539 	mutex_enter(&sc->sc_write_mtx);
   1540 
   1541 	off = 0;
   1542 	urtwn_efuse_switch_power(sc);
   1543 
   1544 	/* Read full ROM image. */
   1545 	memset(&sc->r88e_rom, 0xff, sizeof(sc->r88e_rom));
   1546 	while (addr < 4096) {
   1547 		reg = urtwn_efuse_read_1(sc, addr);
   1548 		if (reg == 0xff)
   1549 			break;
   1550 		addr++;
   1551 		if ((reg & 0x1f) == 0x0f) {
   1552 			tmp = (reg & 0xe0) >> 5;
   1553 			reg = urtwn_efuse_read_1(sc, addr);
   1554 			if ((reg & 0x0f) != 0x0f)
   1555 				off = ((reg & 0xf0) >> 1) | tmp;
   1556 			addr++;
   1557 		} else
   1558 			off = reg >> 4;
   1559 		msk = reg & 0xf;
   1560 		for (i = 0; i < 4; i++) {
   1561 			if (msk & (1 << i))
   1562 				continue;
   1563 			rom[off * 8 + i * 2 + 0] = urtwn_efuse_read_1(sc, addr);
   1564 			addr++;
   1565 			rom[off * 8 + i * 2 + 1] = urtwn_efuse_read_1(sc, addr);
   1566 			addr++;
   1567 		}
   1568 	}
   1569 #ifdef URTWN_DEBUG
   1570 	if (urtwn_debug & DBG_REG) {
   1571 	}
   1572 #endif
   1573 
   1574 	addr = 0x10;
   1575 	for (i = 0; i < 6; i++)
   1576 		sc->cck_tx_pwr[i] = sc->r88e_rom[addr++];
   1577 	for (i = 0; i < 5; i++)
   1578 		sc->ht40_tx_pwr[i] = sc->r88e_rom[addr++];
   1579 	sc->bw20_tx_pwr_diff = (sc->r88e_rom[addr] & 0xf0) >> 4;
   1580 	if (sc->bw20_tx_pwr_diff & 0x08)
   1581 		sc->bw20_tx_pwr_diff |= 0xf0;
   1582 	sc->ofdm_tx_pwr_diff = (sc->r88e_rom[addr] & 0xf);
   1583 	if (sc->ofdm_tx_pwr_diff & 0x08)
   1584 		sc->ofdm_tx_pwr_diff |= 0xf0;
   1585 	sc->regulatory = MS(sc->r88e_rom[0xc1], R92C_ROM_RF1_REGULATORY);
   1586 
   1587 	IEEE80211_ADDR_COPY(ic->ic_myaddr, &sc->r88e_rom[0xd7]);
   1588 
   1589 	if (ISSET(sc->chip, URTWN_CHIP_92EU)) {
   1590 		sc->sc_power_on = urtwn_r92e_power_on;
   1591 		sc->sc_rf_write = urtwn_r92e_rf_write;
   1592 	} else {
   1593 		sc->sc_power_on = urtwn_r88e_power_on;
   1594 		sc->sc_rf_write = urtwn_r88e_rf_write;
   1595 	}
   1596 	sc->sc_dma_init = urtwn_r88e_dma_init;
   1597 
   1598 	mutex_exit(&sc->sc_write_mtx);
   1599 }
   1600 
   1601 static int
   1602 urtwn_media_change(struct ifnet *ifp)
   1603 {
   1604 	int error;
   1605 
   1606 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1607 
   1608 	if ((error = ieee80211_media_change(ifp)) != ENETRESET)
   1609 		return error;
   1610 
   1611 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
   1612 	    (IFF_UP | IFF_RUNNING)) {
   1613 		urtwn_init(ifp);
   1614 	}
   1615 	return 0;
   1616 }
   1617 
   1618 /*
   1619  * Initialize rate adaptation in firmware.
   1620  */
   1621 static int __noinline
   1622 urtwn_ra_init(struct urtwn_softc *sc)
   1623 {
   1624 	static const uint8_t map[] = {
   1625 		2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108
   1626 	};
   1627 	struct ieee80211com *ic = &sc->sc_ic;
   1628 	struct ieee80211_node *ni = ic->ic_bss;
   1629 	struct ieee80211_rateset *rs = &ni->ni_rates;
   1630 	struct r92c_fw_cmd_macid_cfg cmd;
   1631 	uint32_t rates, basicrates;
   1632 	uint32_t rrsr_mask, rrsr_rate;
   1633 	uint8_t mode;
   1634 	size_t maxrate, maxbasicrate, i, j;
   1635 	int error;
   1636 
   1637 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1638 
   1639 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   1640 
   1641 	/* Get normal and basic rates mask. */
   1642 	rates = basicrates = 1;
   1643 	maxrate = maxbasicrate = 0;
   1644 	for (i = 0; i < rs->rs_nrates; i++) {
   1645 		/* Convert 802.11 rate to HW rate index. */
   1646 		for (j = 0; j < __arraycount(map); j++) {
   1647 			if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == map[j]) {
   1648 				break;
   1649 			}
   1650 		}
   1651 		if (j == __arraycount(map)) {
   1652 			/* Unknown rate, skip. */
   1653 			continue;
   1654 		}
   1655 
   1656 		rates |= 1U << j;
   1657 		if (j > maxrate) {
   1658 			maxrate = j;
   1659 		}
   1660 
   1661 		if (rs->rs_rates[i] & IEEE80211_RATE_BASIC) {
   1662 			basicrates |= 1U << j;
   1663 			if (j > maxbasicrate) {
   1664 				maxbasicrate = j;
   1665 			}
   1666 		}
   1667 	}
   1668 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
   1669 		mode = R92C_RAID_11B;
   1670 	} else {
   1671 		mode = R92C_RAID_11BG;
   1672 	}
   1673 	DPRINTFN(DBG_INIT, "mode=%#jx", mode, 0, 0, 0);
   1674 	DPRINTFN(DBG_INIT, "rates=%#jx, basicrates=%#jx, "
   1675 	    "maxrate=%jx, maxbasicrate=%jx",
   1676 	    rates, basicrates, maxrate, maxbasicrate);
   1677 
   1678 	if (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) {
   1679 		maxbasicrate |= R92C_RATE_SHORTGI;
   1680 		maxrate |= R92C_RATE_SHORTGI;
   1681 	}
   1682 
   1683 	/* Set rates mask for group addressed frames. */
   1684 	cmd.macid = RTWN_MACID_BC | RTWN_MACID_VALID;
   1685 	if (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
   1686 		cmd.macid |= RTWN_MACID_SHORTGI;
   1687 	cmd.mask = htole32((mode << 28) | basicrates);
   1688 	error = urtwn_fw_cmd(sc, R92C_CMD_MACID_CONFIG, &cmd, sizeof(cmd));
   1689 	if (error != 0) {
   1690 		aprint_error_dev(sc->sc_dev,
   1691 		    "could not add broadcast station\n");
   1692 		return error;
   1693 	}
   1694 	/* Set initial MRR rate. */
   1695 	DPRINTFN(DBG_INIT, "maxbasicrate=%jd", maxbasicrate, 0, 0, 0);
   1696 	urtwn_write_1(sc, R92C_INIDATA_RATE_SEL(RTWN_MACID_BC), maxbasicrate);
   1697 
   1698 	/* Set rates mask for unicast frames. */
   1699 	cmd.macid = RTWN_MACID_BSS | RTWN_MACID_VALID;
   1700 	if (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)
   1701 		cmd.macid |= RTWN_MACID_SHORTGI;
   1702 	cmd.mask = htole32((mode << 28) | rates);
   1703 	error = urtwn_fw_cmd(sc, R92C_CMD_MACID_CONFIG, &cmd, sizeof(cmd));
   1704 	if (error != 0) {
   1705 		aprint_error_dev(sc->sc_dev, "could not add BSS station\n");
   1706 		return error;
   1707 	}
   1708 	/* Set initial MRR rate. */
   1709 	DPRINTFN(DBG_INIT, "maxrate=%jd", maxrate, 0, 0, 0);
   1710 	urtwn_write_1(sc, R92C_INIDATA_RATE_SEL(RTWN_MACID_BSS), maxrate);
   1711 
   1712 	rrsr_rate = ic->ic_fixed_rate;
   1713 	if (rrsr_rate == -1)
   1714 		rrsr_rate = 11;
   1715 
   1716 	rrsr_mask = 0xffff >> (15 - rrsr_rate);
   1717 	urtwn_write_2(sc, R92C_RRSR, rrsr_mask);
   1718 
   1719 	/* Indicate highest supported rate. */
   1720 	ni->ni_txrate = rs->rs_nrates - 1;
   1721 
   1722 	return 0;
   1723 }
   1724 
   1725 static int
   1726 urtwn_get_nettype(struct urtwn_softc *sc)
   1727 {
   1728 	struct ieee80211com *ic = &sc->sc_ic;
   1729 	int type;
   1730 
   1731 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1732 
   1733 	switch (ic->ic_opmode) {
   1734 	case IEEE80211_M_STA:
   1735 		type = R92C_CR_NETTYPE_INFRA;
   1736 		break;
   1737 
   1738 	case IEEE80211_M_IBSS:
   1739 		type = R92C_CR_NETTYPE_ADHOC;
   1740 		break;
   1741 
   1742 	default:
   1743 		type = R92C_CR_NETTYPE_NOLINK;
   1744 		break;
   1745 	}
   1746 
   1747 	return type;
   1748 }
   1749 
   1750 static void
   1751 urtwn_set_nettype0_msr(struct urtwn_softc *sc, uint8_t type)
   1752 {
   1753 	uint8_t	reg;
   1754 
   1755 	URTWNHIST_FUNC();
   1756 	URTWNHIST_CALLARGS("type=%jd", type, 0, 0, 0);
   1757 
   1758 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   1759 
   1760 	reg = urtwn_read_1(sc, R92C_CR + 2) & 0x0c;
   1761 	urtwn_write_1(sc, R92C_CR + 2, reg | type);
   1762 }
   1763 
   1764 static void
   1765 urtwn_tsf_sync_enable(struct urtwn_softc *sc)
   1766 {
   1767 	struct ieee80211_node *ni = sc->sc_ic.ic_bss;
   1768 	uint64_t tsf;
   1769 
   1770 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1771 
   1772 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   1773 
   1774 	/* Enable TSF synchronization. */
   1775 	urtwn_write_1(sc, R92C_BCN_CTRL,
   1776 	    urtwn_read_1(sc, R92C_BCN_CTRL) & ~R92C_BCN_CTRL_DIS_TSF_UDT0);
   1777 
   1778 	/* Correct TSF */
   1779 	urtwn_write_1(sc, R92C_BCN_CTRL,
   1780 	    urtwn_read_1(sc, R92C_BCN_CTRL) & ~R92C_BCN_CTRL_EN_BCN);
   1781 
   1782 	/* Set initial TSF. */
   1783 	tsf = ni->ni_tstamp.tsf;
   1784 	tsf = le64toh(tsf);
   1785 	tsf = tsf - (tsf % (ni->ni_intval * IEEE80211_DUR_TU));
   1786 	tsf -= IEEE80211_DUR_TU;
   1787 	urtwn_write_4(sc, R92C_TSFTR + 0, (uint32_t)tsf);
   1788 	urtwn_write_4(sc, R92C_TSFTR + 4, (uint32_t)(tsf >> 32));
   1789 
   1790 	urtwn_write_1(sc, R92C_BCN_CTRL,
   1791 	    urtwn_read_1(sc, R92C_BCN_CTRL) | R92C_BCN_CTRL_EN_BCN);
   1792 }
   1793 
   1794 static void
   1795 urtwn_set_led(struct urtwn_softc *sc, int led, int on)
   1796 {
   1797 	uint8_t reg;
   1798 
   1799 	URTWNHIST_FUNC();
   1800 	URTWNHIST_CALLARGS("led=%jd, on=%jd", led, on, 0, 0);
   1801 
   1802 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   1803 
   1804 	if (led == URTWN_LED_LINK) {
   1805 		if (ISSET(sc->chip, URTWN_CHIP_92EU)) {
   1806 			urtwn_write_1(sc, 0x64, urtwn_read_1(sc, 0x64) & 0xfe);
   1807 			reg = urtwn_read_1(sc, R92C_LEDCFG1) & R92E_LEDSON;
   1808 			urtwn_write_1(sc, R92C_LEDCFG1, reg |
   1809 			    (R92C_LEDCFG0_DIS << 1));
   1810 			if (on) {
   1811 				reg = urtwn_read_1(sc, R92C_LEDCFG1) &
   1812 				    R92E_LEDSON;
   1813 				urtwn_write_1(sc, R92C_LEDCFG1, reg);
   1814 			}
   1815 		} else if (ISSET(sc->chip, URTWN_CHIP_88E)) {
   1816 			reg = urtwn_read_1(sc, R92C_LEDCFG2) & 0xf0;
   1817 			urtwn_write_1(sc, R92C_LEDCFG2, reg | 0x60);
   1818 			if (!on) {
   1819 				reg = urtwn_read_1(sc, R92C_LEDCFG2) & 0x90;
   1820 				urtwn_write_1(sc, R92C_LEDCFG2,
   1821 				    reg | R92C_LEDCFG0_DIS);
   1822 				reg = urtwn_read_1(sc, R92C_MAC_PINMUX_CFG);
   1823 				urtwn_write_1(sc, R92C_MAC_PINMUX_CFG,
   1824 				    reg & 0xfe);
   1825 			}
   1826 		} else {
   1827 			reg = urtwn_read_1(sc, R92C_LEDCFG0) & 0x70;
   1828 			if (!on) {
   1829 				reg |= R92C_LEDCFG0_DIS;
   1830 			}
   1831 			urtwn_write_1(sc, R92C_LEDCFG0, reg);
   1832 		}
   1833 		sc->ledlink = on;	/* Save LED state. */
   1834 	}
   1835 }
   1836 
   1837 static void
   1838 urtwn_calib_to(void *arg)
   1839 {
   1840 	struct urtwn_softc *sc = arg;
   1841 
   1842 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1843 
   1844 	if (sc->sc_dying)
   1845 		return;
   1846 
   1847 	/* Do it in a process context. */
   1848 	urtwn_do_async(sc, urtwn_calib_to_cb, NULL, 0);
   1849 }
   1850 
   1851 /* ARGSUSED */
   1852 static void
   1853 urtwn_calib_to_cb(struct urtwn_softc *sc, void *arg)
   1854 {
   1855 	struct r92c_fw_cmd_rssi cmd;
   1856 	struct r92e_fw_cmd_rssi cmde;
   1857 
   1858 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1859 
   1860 	if (sc->sc_ic.ic_state != IEEE80211_S_RUN)
   1861 		goto restart_timer;
   1862 
   1863 	mutex_enter(&sc->sc_write_mtx);
   1864 	if (sc->avg_pwdb != -1) {
   1865 		/* Indicate Rx signal strength to FW for rate adaptation. */
   1866 		memset(&cmd, 0, sizeof(cmd));
   1867 		memset(&cmde, 0, sizeof(cmde));
   1868 		cmd.macid = 0;	/* BSS. */
   1869 		cmde.macid = 0;	/* BSS. */
   1870 		cmd.pwdb = sc->avg_pwdb;
   1871 		cmde.pwdb = sc->avg_pwdb;
   1872 		DPRINTFN(DBG_RF, "sending RSSI command avg=%jd",
   1873 		    sc->avg_pwdb, 0, 0, 0);
   1874 		if (!ISSET(sc->chip, URTWN_CHIP_92EU)) {
   1875 			urtwn_fw_cmd(sc, R92C_CMD_RSSI_SETTING, &cmd,
   1876 			    sizeof(cmd));
   1877 		} else {
   1878 			urtwn_fw_cmd(sc, R92E_CMD_RSSI_REPORT, &cmde,
   1879 			    sizeof(cmde));
   1880 		}
   1881 	}
   1882 
   1883 	/* Do temperature compensation. */
   1884 	urtwn_temp_calib(sc);
   1885 	mutex_exit(&sc->sc_write_mtx);
   1886 
   1887  restart_timer:
   1888 	if (!sc->sc_dying) {
   1889 		/* Restart calibration timer. */
   1890 		callout_schedule(&sc->sc_calib_to, hz);
   1891 	}
   1892 }
   1893 
   1894 static void
   1895 urtwn_next_scan(void *arg)
   1896 {
   1897 	struct urtwn_softc *sc = arg;
   1898 	int s;
   1899 
   1900 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1901 
   1902 	if (sc->sc_dying)
   1903 		return;
   1904 
   1905 	s = splnet();
   1906 	if (sc->sc_ic.ic_state == IEEE80211_S_SCAN)
   1907 		ieee80211_next_scan(&sc->sc_ic);
   1908 	splx(s);
   1909 }
   1910 
   1911 static void
   1912 urtwn_newassoc(struct ieee80211_node *ni, int isnew)
   1913 {
   1914 	URTWNHIST_FUNC();
   1915 	URTWNHIST_CALLARGS("new node %06jx%06jx",
   1916 	    ni->ni_macaddr[0] << 2 |
   1917 	    ni->ni_macaddr[1] << 1 |
   1918 	    ni->ni_macaddr[2],
   1919 	    ni->ni_macaddr[3] << 2 |
   1920 	    ni->ni_macaddr[4] << 1 |
   1921 	    ni->ni_macaddr[5],
   1922 	    0, 0);
   1923 	/* start with lowest Tx rate */
   1924 	ni->ni_txrate = 0;
   1925 }
   1926 
   1927 static int
   1928 urtwn_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
   1929 {
   1930 	struct urtwn_softc *sc = ic->ic_ifp->if_softc;
   1931 	struct urtwn_cmd_newstate cmd;
   1932 
   1933 	URTWNHIST_FUNC();
   1934 	URTWNHIST_CALLARGS("nstate=%jd, arg=%jd", nstate, arg, 0, 0);
   1935 
   1936 	callout_stop(&sc->sc_scan_to);
   1937 	callout_stop(&sc->sc_calib_to);
   1938 
   1939 	/* Do it in a process context. */
   1940 	cmd.state = nstate;
   1941 	cmd.arg = arg;
   1942 	urtwn_do_async(sc, urtwn_newstate_cb, &cmd, sizeof(cmd));
   1943 	return 0;
   1944 }
   1945 
   1946 static void
   1947 urtwn_newstate_cb(struct urtwn_softc *sc, void *arg)
   1948 {
   1949 	struct urtwn_cmd_newstate *cmd = arg;
   1950 	struct ieee80211com *ic = &sc->sc_ic;
   1951 	struct ieee80211_node *ni;
   1952 	enum ieee80211_state ostate = ic->ic_state;
   1953 	enum ieee80211_state nstate = cmd->state;
   1954 	uint32_t reg;
   1955 	uint8_t sifs_time, msr;
   1956 	int s;
   1957 
   1958 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   1959 	DPRINTFN(DBG_STM, "%jd->%jd", ostate, nstate, 0, 0);
   1960 
   1961 	s = splnet();
   1962 	mutex_enter(&sc->sc_write_mtx);
   1963 
   1964 	callout_stop(&sc->sc_scan_to);
   1965 	callout_stop(&sc->sc_calib_to);
   1966 
   1967 	switch (ostate) {
   1968 	case IEEE80211_S_INIT:
   1969 		break;
   1970 
   1971 	case IEEE80211_S_SCAN:
   1972 		if (nstate != IEEE80211_S_SCAN) {
   1973 			/*
   1974 			 * End of scanning
   1975 			 */
   1976 			/* flush 4-AC Queue after site_survey */
   1977 			urtwn_write_1(sc, R92C_TXPAUSE, 0x0);
   1978 
   1979 			/* Allow Rx from our BSSID only. */
   1980 			urtwn_write_4(sc, R92C_RCR,
   1981 			    urtwn_read_4(sc, R92C_RCR) |
   1982 			      R92C_RCR_CBSSID_DATA | R92C_RCR_CBSSID_BCN);
   1983 		}
   1984 		break;
   1985 
   1986 	case IEEE80211_S_AUTH:
   1987 	case IEEE80211_S_ASSOC:
   1988 		break;
   1989 
   1990 	case IEEE80211_S_RUN:
   1991 		/* Turn link LED off. */
   1992 		urtwn_set_led(sc, URTWN_LED_LINK, 0);
   1993 
   1994 		/* Set media status to 'No Link'. */
   1995 		urtwn_set_nettype0_msr(sc, R92C_CR_NETTYPE_NOLINK);
   1996 
   1997 		/* Stop Rx of data frames. */
   1998 		urtwn_write_2(sc, R92C_RXFLTMAP2, 0);
   1999 
   2000 		/* Reset TSF. */
   2001 		urtwn_write_1(sc, R92C_DUAL_TSF_RST, 0x03);
   2002 
   2003 		/* Disable TSF synchronization. */
   2004 		urtwn_write_1(sc, R92C_BCN_CTRL,
   2005 		    urtwn_read_1(sc, R92C_BCN_CTRL) |
   2006 		      R92C_BCN_CTRL_DIS_TSF_UDT0);
   2007 
   2008 		/* Back to 20MHz mode */
   2009 		urtwn_set_chan(sc, ic->ic_curchan,
   2010 		    IEEE80211_HTINFO_2NDCHAN_NONE);
   2011 
   2012 		if (ic->ic_opmode == IEEE80211_M_IBSS ||
   2013 		    ic->ic_opmode == IEEE80211_M_HOSTAP) {
   2014 			/* Stop BCN */
   2015 			urtwn_write_1(sc, R92C_BCN_CTRL,
   2016 			    urtwn_read_1(sc, R92C_BCN_CTRL) &
   2017 			    ~(R92C_BCN_CTRL_EN_BCN | R92C_BCN_CTRL_TXBCN_RPT));
   2018 		}
   2019 
   2020 		/* Reset EDCA parameters. */
   2021 		urtwn_write_4(sc, R92C_EDCA_VO_PARAM, 0x002f3217);
   2022 		urtwn_write_4(sc, R92C_EDCA_VI_PARAM, 0x005e4317);
   2023 		urtwn_write_4(sc, R92C_EDCA_BE_PARAM, 0x00105320);
   2024 		urtwn_write_4(sc, R92C_EDCA_BK_PARAM, 0x0000a444);
   2025 
   2026 		/* flush all cam entries */
   2027 		urtwn_cam_init(sc);
   2028 		break;
   2029 	}
   2030 
   2031 	switch (nstate) {
   2032 	case IEEE80211_S_INIT:
   2033 		/* Turn link LED off. */
   2034 		urtwn_set_led(sc, URTWN_LED_LINK, 0);
   2035 		break;
   2036 
   2037 	case IEEE80211_S_SCAN:
   2038 		if (ostate != IEEE80211_S_SCAN) {
   2039 			/*
   2040 			 * Begin of scanning
   2041 			 */
   2042 
   2043 			/* Set gain for scanning. */
   2044 			reg = urtwn_bb_read(sc, R92C_OFDM0_AGCCORE1(0));
   2045 			reg = RW(reg, R92C_OFDM0_AGCCORE1_GAIN, 0x20);
   2046 			urtwn_bb_write(sc, R92C_OFDM0_AGCCORE1(0), reg);
   2047 
   2048 			if (!ISSET(sc->chip, URTWN_CHIP_88E)) {
   2049 				reg = urtwn_bb_read(sc, R92C_OFDM0_AGCCORE1(1));
   2050 				reg = RW(reg, R92C_OFDM0_AGCCORE1_GAIN, 0x20);
   2051 				urtwn_bb_write(sc, R92C_OFDM0_AGCCORE1(1), reg);
   2052 			}
   2053 
   2054 			/* Set media status to 'No Link'. */
   2055 			urtwn_set_nettype0_msr(sc, R92C_CR_NETTYPE_NOLINK);
   2056 
   2057 			/* Allow Rx from any BSSID. */
   2058 			urtwn_write_4(sc, R92C_RCR,
   2059 			    urtwn_read_4(sc, R92C_RCR) &
   2060 			    ~(R92C_RCR_CBSSID_DATA | R92C_RCR_CBSSID_BCN));
   2061 
   2062 			/* Stop Rx of data frames. */
   2063 			urtwn_write_2(sc, R92C_RXFLTMAP2, 0);
   2064 
   2065 			/* Disable update TSF */
   2066 			urtwn_write_1(sc, R92C_BCN_CTRL,
   2067 			    urtwn_read_1(sc, R92C_BCN_CTRL) |
   2068 			      R92C_BCN_CTRL_DIS_TSF_UDT0);
   2069 		}
   2070 
   2071 		/* Make link LED blink during scan. */
   2072 		urtwn_set_led(sc, URTWN_LED_LINK, !sc->ledlink);
   2073 
   2074 		/* Pause AC Tx queues. */
   2075 		urtwn_write_1(sc, R92C_TXPAUSE,
   2076 		    urtwn_read_1(sc, R92C_TXPAUSE) | 0x0f);
   2077 
   2078 		urtwn_set_chan(sc, ic->ic_curchan,
   2079 		    IEEE80211_HTINFO_2NDCHAN_NONE);
   2080 
   2081 		/* Start periodic scan. */
   2082 		if (!sc->sc_dying)
   2083 			callout_schedule(&sc->sc_scan_to, hz / 5);
   2084 		break;
   2085 
   2086 	case IEEE80211_S_AUTH:
   2087 		/* Set initial gain under link. */
   2088 		reg = urtwn_bb_read(sc, R92C_OFDM0_AGCCORE1(0));
   2089 		reg = RW(reg, R92C_OFDM0_AGCCORE1_GAIN, 0x32);
   2090 		urtwn_bb_write(sc, R92C_OFDM0_AGCCORE1(0), reg);
   2091 
   2092 		if (!ISSET(sc->chip, URTWN_CHIP_88E)) {
   2093 			reg = urtwn_bb_read(sc, R92C_OFDM0_AGCCORE1(1));
   2094 			reg = RW(reg, R92C_OFDM0_AGCCORE1_GAIN, 0x32);
   2095 			urtwn_bb_write(sc, R92C_OFDM0_AGCCORE1(1), reg);
   2096 		}
   2097 
   2098 		/* Set media status to 'No Link'. */
   2099 		urtwn_set_nettype0_msr(sc, R92C_CR_NETTYPE_NOLINK);
   2100 
   2101 		/* Allow Rx from any BSSID. */
   2102 		urtwn_write_4(sc, R92C_RCR,
   2103 		    urtwn_read_4(sc, R92C_RCR) &
   2104 		      ~(R92C_RCR_CBSSID_DATA | R92C_RCR_CBSSID_BCN));
   2105 
   2106 		urtwn_set_chan(sc, ic->ic_curchan,
   2107 		    IEEE80211_HTINFO_2NDCHAN_NONE);
   2108 		break;
   2109 
   2110 	case IEEE80211_S_ASSOC:
   2111 		break;
   2112 
   2113 	case IEEE80211_S_RUN:
   2114 		ni = ic->ic_bss;
   2115 
   2116 		/* XXX: Set 20MHz mode */
   2117 		urtwn_set_chan(sc, ic->ic_curchan,
   2118 		    IEEE80211_HTINFO_2NDCHAN_NONE);
   2119 
   2120 		if (ic->ic_opmode == IEEE80211_M_MONITOR) {
   2121 			/* Back to 20MHz mode */
   2122 			urtwn_set_chan(sc, ic->ic_curchan,
   2123 			    IEEE80211_HTINFO_2NDCHAN_NONE);
   2124 
   2125 			/* Set media status to 'No Link'. */
   2126 			urtwn_set_nettype0_msr(sc, R92C_CR_NETTYPE_NOLINK);
   2127 
   2128 			/* Enable Rx of data frames. */
   2129 			urtwn_write_2(sc, R92C_RXFLTMAP2, 0xffff);
   2130 
   2131 			/* Allow Rx from any BSSID. */
   2132 			urtwn_write_4(sc, R92C_RCR,
   2133 			    urtwn_read_4(sc, R92C_RCR) &
   2134 			    ~(R92C_RCR_CBSSID_DATA | R92C_RCR_CBSSID_BCN));
   2135 
   2136 			/* Accept Rx data/control/management frames */
   2137 			urtwn_write_4(sc, R92C_RCR,
   2138 			    urtwn_read_4(sc, R92C_RCR) |
   2139 			    R92C_RCR_ADF | R92C_RCR_ACF | R92C_RCR_AMF);
   2140 
   2141 			/* Turn link LED on. */
   2142 			urtwn_set_led(sc, URTWN_LED_LINK, 1);
   2143 			break;
   2144 		}
   2145 
   2146 		/* Set media status to 'Associated'. */
   2147 		urtwn_set_nettype0_msr(sc, urtwn_get_nettype(sc));
   2148 
   2149 		/* Set BSSID. */
   2150 		urtwn_write_4(sc, R92C_BSSID + 0, LE_READ_4(&ni->ni_bssid[0]));
   2151 		urtwn_write_4(sc, R92C_BSSID + 4, LE_READ_2(&ni->ni_bssid[4]));
   2152 
   2153 		if (ic->ic_curmode == IEEE80211_MODE_11B) {
   2154 			urtwn_write_1(sc, R92C_INIRTS_RATE_SEL, 0);
   2155 		} else {
   2156 			/* 802.11b/g */
   2157 			urtwn_write_1(sc, R92C_INIRTS_RATE_SEL, 3);
   2158 		}
   2159 
   2160 		/* Enable Rx of data frames. */
   2161 		urtwn_write_2(sc, R92C_RXFLTMAP2, 0xffff);
   2162 
   2163 		/* Set beacon interval. */
   2164 		urtwn_write_2(sc, R92C_BCN_INTERVAL, ni->ni_intval);
   2165 
   2166 		msr = urtwn_read_1(sc, R92C_MSR);
   2167 		msr &= R92C_MSR_MASK;
   2168 		switch (ic->ic_opmode) {
   2169 		case IEEE80211_M_STA:
   2170 			/* Allow Rx from our BSSID only. */
   2171 			urtwn_write_4(sc, R92C_RCR,
   2172 			    urtwn_read_4(sc, R92C_RCR) |
   2173 			      R92C_RCR_CBSSID_DATA | R92C_RCR_CBSSID_BCN);
   2174 
   2175 			/* Enable TSF synchronization. */
   2176 			urtwn_tsf_sync_enable(sc);
   2177 
   2178 			msr |= R92C_MSR_INFRA;
   2179 			break;
   2180 		case IEEE80211_M_HOSTAP:
   2181 			urtwn_write_2(sc, R92C_BCNTCFG, 0x000f);
   2182 
   2183 			/* Allow Rx from any BSSID. */
   2184 			urtwn_write_4(sc, R92C_RCR,
   2185 			    urtwn_read_4(sc, R92C_RCR) &
   2186 			    ~(R92C_RCR_CBSSID_DATA | R92C_RCR_CBSSID_BCN));
   2187 
   2188 			/* Reset TSF timer to zero. */
   2189 			reg = urtwn_read_4(sc, R92C_TCR);
   2190 			reg &= ~0x01;
   2191 			urtwn_write_4(sc, R92C_TCR, reg);
   2192 			reg |= 0x01;
   2193 			urtwn_write_4(sc, R92C_TCR, reg);
   2194 
   2195 			msr |= R92C_MSR_AP;
   2196 			break;
   2197 		default:
   2198 			msr |= R92C_MSR_ADHOC;
   2199 			break;
   2200 		}
   2201 		urtwn_write_1(sc, R92C_MSR, msr);
   2202 
   2203 		sifs_time = 10;
   2204 		urtwn_write_1(sc, R92C_SIFS_CCK + 1, sifs_time);
   2205 		urtwn_write_1(sc, R92C_SIFS_OFDM + 1, sifs_time);
   2206 		urtwn_write_1(sc, R92C_SPEC_SIFS + 1, sifs_time);
   2207 		urtwn_write_1(sc, R92C_MAC_SPEC_SIFS + 1, sifs_time);
   2208 		urtwn_write_1(sc, R92C_R2T_SIFS + 1, sifs_time);
   2209 		urtwn_write_1(sc, R92C_T2T_SIFS + 1, sifs_time);
   2210 
   2211 		/* Initialize rate adaptation. */
   2212 		if (ISSET(sc->chip, URTWN_CHIP_88E) ||
   2213 		    ISSET(sc->chip, URTWN_CHIP_92EU))
   2214 			ni->ni_txrate = ni->ni_rates.rs_nrates - 1;
   2215 		else
   2216 			urtwn_ra_init(sc);
   2217 
   2218 		/* Turn link LED on. */
   2219 		urtwn_set_led(sc, URTWN_LED_LINK, 1);
   2220 
   2221 		/* Reset average RSSI. */
   2222 		sc->avg_pwdb = -1;
   2223 
   2224 		/* Reset temperature calibration state machine. */
   2225 		sc->thcal_state = 0;
   2226 		sc->thcal_lctemp = 0;
   2227 
   2228 		/* Start periodic calibration. */
   2229 		if (!sc->sc_dying)
   2230 			callout_schedule(&sc->sc_calib_to, hz);
   2231 		break;
   2232 	}
   2233 
   2234 	(*sc->sc_newstate)(ic, nstate, cmd->arg);
   2235 
   2236 	mutex_exit(&sc->sc_write_mtx);
   2237 	splx(s);
   2238 }
   2239 
   2240 static int
   2241 urtwn_wme_update(struct ieee80211com *ic)
   2242 {
   2243 	struct urtwn_softc *sc = ic->ic_ifp->if_softc;
   2244 
   2245 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   2246 
   2247 	/* don't override default WME values if WME is not actually enabled */
   2248 	if (!(ic->ic_flags & IEEE80211_F_WME))
   2249 		return 0;
   2250 
   2251 	/* Do it in a process context. */
   2252 	urtwn_do_async(sc, urtwn_wme_update_cb, NULL, 0);
   2253 	return 0;
   2254 }
   2255 
   2256 static void
   2257 urtwn_wme_update_cb(struct urtwn_softc *sc, void *arg)
   2258 {
   2259 	static const uint16_t ac2reg[WME_NUM_AC] = {
   2260 		R92C_EDCA_BE_PARAM,
   2261 		R92C_EDCA_BK_PARAM,
   2262 		R92C_EDCA_VI_PARAM,
   2263 		R92C_EDCA_VO_PARAM
   2264 	};
   2265 	struct ieee80211com *ic = &sc->sc_ic;
   2266 	const struct wmeParams *wmep;
   2267 	int ac, aifs, slottime;
   2268 	int s;
   2269 
   2270 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   2271 	DPRINTFN(DBG_STM, "called", 0, 0, 0, 0);
   2272 
   2273 	s = splnet();
   2274 	mutex_enter(&sc->sc_write_mtx);
   2275 	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
   2276 	for (ac = 0; ac < WME_NUM_AC; ac++) {
   2277 		wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
   2278 		/* AIFS[AC] = AIFSN[AC] * aSlotTime + aSIFSTime. */
   2279 		aifs = wmep->wmep_aifsn * slottime + 10;
   2280 		urtwn_write_4(sc, ac2reg[ac],
   2281 		    SM(R92C_EDCA_PARAM_TXOP, wmep->wmep_txopLimit) |
   2282 		    SM(R92C_EDCA_PARAM_ECWMIN, wmep->wmep_logcwmin) |
   2283 		    SM(R92C_EDCA_PARAM_ECWMAX, wmep->wmep_logcwmax) |
   2284 		    SM(R92C_EDCA_PARAM_AIFS, aifs));
   2285 	}
   2286 	mutex_exit(&sc->sc_write_mtx);
   2287 	splx(s);
   2288 }
   2289 
   2290 static void
   2291 urtwn_update_avgrssi(struct urtwn_softc *sc, int rate, int8_t rssi)
   2292 {
   2293 	int pwdb;
   2294 
   2295 	URTWNHIST_FUNC();
   2296 	URTWNHIST_CALLARGS("rate=%jd, rsst=%jd", rate, rssi, 0, 0);
   2297 
   2298 	/* Convert antenna signal to percentage. */
   2299 	if (rssi <= -100 || rssi >= 20)
   2300 		pwdb = 0;
   2301 	else if (rssi >= 0)
   2302 		pwdb = 100;
   2303 	else
   2304 		pwdb = 100 + rssi;
   2305 	if (!ISSET(sc->chip, URTWN_CHIP_88E)) {
   2306 		if (rate <= 3) {
   2307 			/* CCK gain is smaller than OFDM/MCS gain. */
   2308 			pwdb += 6;
   2309 			if (pwdb > 100)
   2310 				pwdb = 100;
   2311 			if (pwdb <= 14)
   2312 				pwdb -= 4;
   2313 			else if (pwdb <= 26)
   2314 				pwdb -= 8;
   2315 			else if (pwdb <= 34)
   2316 				pwdb -= 6;
   2317 			else if (pwdb <= 42)
   2318 				pwdb -= 2;
   2319 		}
   2320 	}
   2321 	if (sc->avg_pwdb == -1)	/* Init. */
   2322 		sc->avg_pwdb = pwdb;
   2323 	else if (sc->avg_pwdb < pwdb)
   2324 		sc->avg_pwdb = ((sc->avg_pwdb * 19 + pwdb) / 20) + 1;
   2325 	else
   2326 		sc->avg_pwdb = ((sc->avg_pwdb * 19 + pwdb) / 20);
   2327 
   2328 	DPRINTFN(DBG_RF, "rate=%jd rssi=%jd PWDB=%jd EMA=%jd",
   2329 	    rate, rssi, pwdb, sc->avg_pwdb);
   2330 }
   2331 
   2332 static int8_t
   2333 urtwn_get_rssi(struct urtwn_softc *sc, int rate, void *physt)
   2334 {
   2335 	static const int8_t cckoff[] = { 16, -12, -26, -46 };
   2336 	struct r92c_rx_phystat *phy;
   2337 	struct r92c_rx_cck *cck;
   2338 	uint8_t rpt;
   2339 	int8_t rssi;
   2340 
   2341 	URTWNHIST_FUNC();
   2342 	URTWNHIST_CALLARGS("rate=%jd", rate, 0, 0, 0);
   2343 
   2344 	if (rate <= 3) {
   2345 		cck = (struct r92c_rx_cck *)physt;
   2346 		if (ISSET(sc->sc_flags, URTWN_FLAG_CCK_HIPWR)) {
   2347 			rpt = (cck->agc_rpt >> 5) & 0x3;
   2348 			rssi = (cck->agc_rpt & 0x1f) << 1;
   2349 		} else {
   2350 			rpt = (cck->agc_rpt >> 6) & 0x3;
   2351 			rssi = cck->agc_rpt & 0x3e;
   2352 		}
   2353 		rssi = cckoff[rpt] - rssi;
   2354 	} else {	/* OFDM/HT. */
   2355 		phy = (struct r92c_rx_phystat *)physt;
   2356 		rssi = ((le32toh(phy->phydw1) >> 1) & 0x7f) - 110;
   2357 	}
   2358 	return rssi;
   2359 }
   2360 
   2361 static int8_t
   2362 urtwn_r88e_get_rssi(struct urtwn_softc *sc, int rate, void *physt)
   2363 {
   2364 	struct r92c_rx_phystat *phy;
   2365 	struct r88e_rx_cck *cck;
   2366 	uint8_t cck_agc_rpt, lna_idx, vga_idx;
   2367 	int8_t rssi;
   2368 
   2369 	URTWNHIST_FUNC();
   2370 	URTWNHIST_CALLARGS("rate=%jd", rate, 0, 0, 0);
   2371 
   2372 	rssi = 0;
   2373 	if (rate <= 3) {
   2374 		cck = (struct r88e_rx_cck *)physt;
   2375 		cck_agc_rpt = cck->agc_rpt;
   2376 		lna_idx = (cck_agc_rpt & 0xe0) >> 5;
   2377 		vga_idx = cck_agc_rpt & 0x1f;
   2378 		switch (lna_idx) {
   2379 		case 7:
   2380 			if (vga_idx <= 27)
   2381 				rssi = -100 + 2* (27 - vga_idx);
   2382 			else
   2383 				rssi = -100;
   2384 			break;
   2385 		case 6:
   2386 			rssi = -48 + 2 * (2 - vga_idx);
   2387 			break;
   2388 		case 5:
   2389 			rssi = -42 + 2 * (7 - vga_idx);
   2390 			break;
   2391 		case 4:
   2392 			rssi = -36 + 2 * (7 - vga_idx);
   2393 			break;
   2394 		case 3:
   2395 			rssi = -24 + 2 * (7 - vga_idx);
   2396 			break;
   2397 		case 2:
   2398 			rssi = -12 + 2 * (5 - vga_idx);
   2399 			break;
   2400 		case 1:
   2401 			rssi = 8 - (2 * vga_idx);
   2402 			break;
   2403 		case 0:
   2404 			rssi = 14 - (2 * vga_idx);
   2405 			break;
   2406 		}
   2407 		rssi += 6;
   2408 	} else {	/* OFDM/HT. */
   2409 		phy = (struct r92c_rx_phystat *)physt;
   2410 		rssi = ((le32toh(phy->phydw1) >> 1) & 0x7f) - 110;
   2411 	}
   2412 	return rssi;
   2413 }
   2414 
   2415 static void
   2416 urtwn_rx_frame(struct urtwn_softc *sc, uint8_t *buf, int pktlen)
   2417 {
   2418 	struct ieee80211com *ic = &sc->sc_ic;
   2419 	struct ifnet *ifp = ic->ic_ifp;
   2420 	struct ieee80211_frame *wh;
   2421 	struct ieee80211_node *ni;
   2422 	struct r92c_rx_desc_usb *stat;
   2423 	uint32_t rxdw0, rxdw3;
   2424 	struct mbuf *m;
   2425 	uint8_t rate;
   2426 	int8_t rssi = 0;
   2427 	int s, infosz;
   2428 
   2429 	URTWNHIST_FUNC();
   2430 	URTWNHIST_CALLARGS("buf=%jp, pktlen=%#jd", (uintptr_t)buf, pktlen, 0, 0);
   2431 
   2432 	stat = (struct r92c_rx_desc_usb *)buf;
   2433 	rxdw0 = le32toh(stat->rxdw0);
   2434 	rxdw3 = le32toh(stat->rxdw3);
   2435 
   2436 	if (__predict_false(rxdw0 & (R92C_RXDW0_CRCERR | R92C_RXDW0_ICVERR))) {
   2437 		/*
   2438 		 * This should not happen since we setup our Rx filter
   2439 		 * to not receive these frames.
   2440 		 */
   2441 		DPRINTFN(DBG_RX, "CRC error", 0, 0, 0, 0);
   2442 		if_statinc(ifp, if_ierrors);
   2443 		return;
   2444 	}
   2445 	/*
   2446 	 * XXX: This will drop most control packets.  Do we really
   2447 	 * want this in IEEE80211_M_MONITOR mode?
   2448 	 */
   2449 //	if (__predict_false(pktlen < (int)sizeof(*wh))) {
   2450 	if (__predict_false(pktlen < (int)sizeof(struct ieee80211_frame_ack))) {
   2451 		DPRINTFN(DBG_RX, "packet too short %jd", pktlen, 0, 0, 0);
   2452 		ic->ic_stats.is_rx_tooshort++;
   2453 		if_statinc(ifp, if_ierrors);
   2454 		return;
   2455 	}
   2456 	if (__predict_false(pktlen > MCLBYTES)) {
   2457 		DPRINTFN(DBG_RX, "packet too big %jd", pktlen, 0, 0, 0);
   2458 		if_statinc(ifp, if_ierrors);
   2459 		return;
   2460 	}
   2461 
   2462 	rate = MS(rxdw3, R92C_RXDW3_RATE);
   2463 	infosz = MS(rxdw0, R92C_RXDW0_INFOSZ) * 8;
   2464 
   2465 	/* Get RSSI from PHY status descriptor if present. */
   2466 	if (infosz != 0 && (rxdw0 & R92C_RXDW0_PHYST)) {
   2467 		if (!ISSET(sc->chip, URTWN_CHIP_92C))
   2468 			rssi = urtwn_r88e_get_rssi(sc, rate, &stat[1]);
   2469 		else
   2470 			rssi = urtwn_get_rssi(sc, rate, &stat[1]);
   2471 		/* Update our average RSSI. */
   2472 		urtwn_update_avgrssi(sc, rate, rssi);
   2473 	}
   2474 
   2475 	DPRINTFN(DBG_RX, "Rx frame len=%jd rate=%jd infosz=%jd rssi=%jd",
   2476 	    pktlen, rate, infosz, rssi);
   2477 
   2478 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2479 	if (__predict_false(m == NULL)) {
   2480 		aprint_error_dev(sc->sc_dev, "couldn't allocate rx mbuf\n");
   2481 		ic->ic_stats.is_rx_nobuf++;
   2482 		if_statinc(ifp, if_ierrors);
   2483 		return;
   2484 	}
   2485 	MCLAIM(m, &sc->sc_ec.ec_rx_mowner);
   2486 	if (pktlen > (int)MHLEN) {
   2487 		MCLGET(m, M_DONTWAIT);
   2488 		if (__predict_false(!(m->m_flags & M_EXT))) {
   2489 			aprint_error_dev(sc->sc_dev,
   2490 			    "couldn't allocate rx mbuf cluster\n");
   2491 			m_freem(m);
   2492 			ic->ic_stats.is_rx_nobuf++;
   2493 			if_statinc(ifp, if_ierrors);
   2494 			return;
   2495 		}
   2496 	}
   2497 
   2498 	/* Finalize mbuf. */
   2499 	m_set_rcvif(m, ifp);
   2500 	wh = (struct ieee80211_frame *)((uint8_t *)&stat[1] + infosz);
   2501 	memcpy(mtod(m, uint8_t *), wh, pktlen);
   2502 	m->m_pkthdr.len = m->m_len = pktlen;
   2503 
   2504 	s = splnet();
   2505 	if (__predict_false(sc->sc_drvbpf != NULL)) {
   2506 		struct urtwn_rx_radiotap_header *tap = &sc->sc_rxtap;
   2507 
   2508 		tap->wr_flags = 0;
   2509 		if (!(rxdw3 & R92C_RXDW3_HT)) {
   2510 			switch (rate) {
   2511 			/* CCK. */
   2512 			case  0: tap->wr_rate =   2; break;
   2513 			case  1: tap->wr_rate =   4; break;
   2514 			case  2: tap->wr_rate =  11; break;
   2515 			case  3: tap->wr_rate =  22; break;
   2516 			/* OFDM. */
   2517 			case  4: tap->wr_rate =  12; break;
   2518 			case  5: tap->wr_rate =  18; break;
   2519 			case  6: tap->wr_rate =  24; break;
   2520 			case  7: tap->wr_rate =  36; break;
   2521 			case  8: tap->wr_rate =  48; break;
   2522 			case  9: tap->wr_rate =  72; break;
   2523 			case 10: tap->wr_rate =  96; break;
   2524 			case 11: tap->wr_rate = 108; break;
   2525 			}
   2526 		} else if (rate >= 12) {	/* MCS0~15. */
   2527 			/* Bit 7 set means HT MCS instead of rate. */
   2528 			tap->wr_rate = 0x80 | (rate - 12);
   2529 		}
   2530 		tap->wr_dbm_antsignal = rssi;
   2531 		tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
   2532 		tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
   2533 
   2534 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m, BPF_D_IN);
   2535 	}
   2536 
   2537 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
   2538 
   2539 	/* push the frame up to the 802.11 stack */
   2540 	ieee80211_input(ic, m, ni, rssi, 0);
   2541 
   2542 	/* Node is no longer needed. */
   2543 	ieee80211_free_node(ni);
   2544 
   2545 	splx(s);
   2546 }
   2547 
   2548 static void
   2549 urtwn_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
   2550 {
   2551 	struct urtwn_rx_data *data = priv;
   2552 	struct urtwn_softc *sc = data->sc;
   2553 	struct r92c_rx_desc_usb *stat;
   2554 	size_t pidx = data->pidx;
   2555 	uint32_t rxdw0;
   2556 	uint8_t *buf;
   2557 	int len, totlen, pktlen, infosz, npkts;
   2558 
   2559 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   2560 	DPRINTFN(DBG_RX, "status=%jd", status, 0, 0, 0);
   2561 
   2562 	mutex_enter(&sc->sc_rx_mtx);
   2563 	TAILQ_REMOVE(&sc->rx_free_list[pidx], data, next);
   2564 	TAILQ_INSERT_TAIL(&sc->rx_free_list[pidx], data, next);
   2565 	/* Put this Rx buffer back to our free list. */
   2566 	mutex_exit(&sc->sc_rx_mtx);
   2567 
   2568 	if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
   2569 		if (status == USBD_STALLED)
   2570 			usbd_clear_endpoint_stall_async(sc->rx_pipe[pidx]);
   2571 		else if (status != USBD_CANCELLED)
   2572 			goto resubmit;
   2573 		return;
   2574 	}
   2575 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
   2576 
   2577 	if (__predict_false(len < (int)sizeof(*stat))) {
   2578 		DPRINTFN(DBG_RX, "xfer too short %jd", len, 0, 0, 0);
   2579 		goto resubmit;
   2580 	}
   2581 	buf = data->buf;
   2582 
   2583 	/* Get the number of encapsulated frames. */
   2584 	stat = (struct r92c_rx_desc_usb *)buf;
   2585 	if (ISSET(sc->chip, URTWN_CHIP_92EU))
   2586 		npkts = MS(le32toh(stat->rxdw2), R92E_RXDW2_PKTCNT);
   2587 	else
   2588 		npkts = MS(le32toh(stat->rxdw2), R92C_RXDW2_PKTCNT);
   2589 	DPRINTFN(DBG_RX, "Rx %jd frames in one chunk", npkts, 0, 0, 0);
   2590 
   2591 	if (npkts != 0)
   2592 		rnd_add_uint32(&sc->rnd_source, npkts);
   2593 
   2594 	/* Process all of them. */
   2595 	while (npkts-- > 0) {
   2596 		if (__predict_false(len < (int)sizeof(*stat))) {
   2597 			DPRINTFN(DBG_RX, "len(%jd) is short than header",
   2598 			    len, 0, 0, 0);
   2599 			break;
   2600 		}
   2601 		stat = (struct r92c_rx_desc_usb *)buf;
   2602 		rxdw0 = le32toh(stat->rxdw0);
   2603 
   2604 		pktlen = MS(rxdw0, R92C_RXDW0_PKTLEN);
   2605 		if (__predict_false(pktlen == 0)) {
   2606 			DPRINTFN(DBG_RX, "pktlen is 0 byte", 0, 0, 0, 0);
   2607 			break;
   2608 		}
   2609 
   2610 		infosz = MS(rxdw0, R92C_RXDW0_INFOSZ) * 8;
   2611 
   2612 		/* Make sure everything fits in xfer. */
   2613 		totlen = sizeof(*stat) + infosz + pktlen;
   2614 		if (__predict_false(totlen > len)) {
   2615 			DPRINTFN(DBG_RX, "pktlen (%jd+%jd+%jd) > %jd",
   2616 			    (int)sizeof(*stat), infosz, pktlen, len);
   2617 			break;
   2618 		}
   2619 
   2620 		/* Process 802.11 frame. */
   2621 		urtwn_rx_frame(sc, buf, pktlen);
   2622 
   2623 		/* Next chunk is 128-byte aligned. */
   2624 		totlen = roundup2(totlen, 128);
   2625 		buf += totlen;
   2626 		len -= totlen;
   2627 	}
   2628 
   2629  resubmit:
   2630 	/* Setup a new transfer. */
   2631 	usbd_setup_xfer(xfer, data, data->buf, URTWN_RXBUFSZ,
   2632 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, urtwn_rxeof);
   2633 	(void)usbd_transfer(xfer);
   2634 }
   2635 
   2636 static void
   2637 urtwn_put_tx_data(struct urtwn_softc *sc, struct urtwn_tx_data *data)
   2638 {
   2639 	size_t pidx = data->pidx;
   2640 
   2641 	mutex_enter(&sc->sc_tx_mtx);
   2642 	/* Put this Tx buffer back to our free list. */
   2643 	TAILQ_INSERT_TAIL(&sc->tx_free_list[pidx], data, next);
   2644 	mutex_exit(&sc->sc_tx_mtx);
   2645 }
   2646 
   2647 static void
   2648 urtwn_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
   2649 {
   2650 	struct urtwn_tx_data *data = priv;
   2651 	struct urtwn_softc *sc = data->sc;
   2652 	struct ifnet *ifp = &sc->sc_if;
   2653 	size_t pidx = data->pidx;
   2654 	int s;
   2655 
   2656 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   2657 	DPRINTFN(DBG_TX, "status=%jd", status, 0, 0, 0);
   2658 
   2659 	urtwn_put_tx_data(sc, data);
   2660 
   2661 	s = splnet();
   2662 	sc->tx_timer = 0;
   2663 	ifp->if_flags &= ~IFF_OACTIVE;
   2664 
   2665 	if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
   2666 		if (status != USBD_NOT_STARTED && status != USBD_CANCELLED) {
   2667 			if (status == USBD_STALLED) {
   2668 				struct usbd_pipe *pipe = sc->tx_pipe[pidx];
   2669 				usbd_clear_endpoint_stall_async(pipe);
   2670 			}
   2671 			device_printf(sc->sc_dev, "transmit failed, %s\n",
   2672 			    usbd_errstr(status));
   2673 			if_statinc(ifp, if_oerrors);
   2674 		}
   2675 		splx(s);
   2676 		return;
   2677 	}
   2678 
   2679 	if_statinc(ifp, if_opackets);
   2680 	urtwn_start(ifp);
   2681 	splx(s);
   2682 
   2683 }
   2684 
   2685 static int
   2686 urtwn_tx(struct urtwn_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
   2687     struct urtwn_tx_data *data)
   2688 {
   2689 	struct ieee80211com *ic = &sc->sc_ic;
   2690 	struct ieee80211_frame *wh;
   2691 	struct ieee80211_key *k = NULL;
   2692 	struct r92c_tx_desc_usb *txd;
   2693 	size_t i, padsize, xferlen, txd_len;
   2694 	uint16_t seq, sum;
   2695 	uint8_t raid, type, tid;
   2696 	int s, hasqos, error;
   2697 
   2698 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   2699 
   2700 	wh = mtod(m, struct ieee80211_frame *);
   2701 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
   2702 	txd_len = sizeof(*txd);
   2703 
   2704 	if (!ISSET(sc->chip, URTWN_CHIP_92EU))
   2705 		txd_len = 32;
   2706 
   2707 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   2708 		k = ieee80211_crypto_encap(ic, ni, m);
   2709 		if (k == NULL) {
   2710 			urtwn_put_tx_data(sc, data);
   2711 			m_free(m);
   2712 			return ENOBUFS;
   2713 		}
   2714 
   2715 		/* packet header may have moved, reset our local pointer */
   2716 		wh = mtod(m, struct ieee80211_frame *);
   2717 	}
   2718 
   2719 	if (__predict_false(sc->sc_drvbpf != NULL)) {
   2720 		struct urtwn_tx_radiotap_header *tap = &sc->sc_txtap;
   2721 
   2722 		tap->wt_flags = 0;
   2723 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
   2724 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
   2725 		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
   2726 			tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
   2727 
   2728 		/* XXX: set tap->wt_rate? */
   2729 
   2730 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m, BPF_D_OUT);
   2731 	}
   2732 
   2733 	/* non-qos data frames */
   2734 	tid = R92C_TXDW1_QSEL_BE;
   2735 	if ((hasqos = ieee80211_has_qos(wh))) {
   2736 		/* data frames in 11n mode */
   2737 		struct ieee80211_qosframe *qwh = (void *)wh;
   2738 		tid = qwh->i_qos[0] & IEEE80211_QOS_TID;
   2739 	} else if (type != IEEE80211_FC0_TYPE_DATA) {
   2740 		tid = R92C_TXDW1_QSEL_MGNT;
   2741 	}
   2742 
   2743 	if (((txd_len + m->m_pkthdr.len) % 64) == 0) /* XXX: 64 */
   2744 		padsize = 8;
   2745 	else
   2746 		padsize = 0;
   2747 
   2748 	if (ISSET(sc->chip, URTWN_CHIP_92EU))
   2749 		padsize = 0;
   2750 
   2751 	/* Fill Tx descriptor. */
   2752 	txd = (struct r92c_tx_desc_usb *)data->buf;
   2753 	memset(txd, 0, txd_len + padsize);
   2754 
   2755 	txd->txdw0 |= htole32(
   2756 	    SM(R92C_TXDW0_PKTLEN, m->m_pkthdr.len) |
   2757 	    SM(R92C_TXDW0_OFFSET, txd_len));
   2758 	if (!ISSET(sc->chip, URTWN_CHIP_92EU)) {
   2759 		txd->txdw0 |= htole32(
   2760 		    R92C_TXDW0_OWN | R92C_TXDW0_FSG | R92C_TXDW0_LSG);
   2761 	}
   2762 
   2763 	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
   2764 		txd->txdw0 |= htole32(R92C_TXDW0_BMCAST);
   2765 
   2766 	/* fix pad field */
   2767 	if (padsize > 0) {
   2768 		DPRINTFN(DBG_TX, "padding: size=%jd", padsize, 0, 0, 0);
   2769 		txd->txdw1 |= htole32(SM(R92C_TXDW1_PKTOFF, (padsize / 8)));
   2770 	}
   2771 
   2772 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
   2773 	    type == IEEE80211_FC0_TYPE_DATA) {
   2774 		if (ic->ic_curmode == IEEE80211_MODE_11B)
   2775 			raid = R92C_RAID_11B;
   2776 		else
   2777 			raid = R92C_RAID_11BG;
   2778 		DPRINTFN(DBG_TX, "data packet: tid=%jd, raid=%jd",
   2779 		    tid, raid, 0, 0);
   2780 
   2781 		if (!ISSET(sc->chip, URTWN_CHIP_92C)) {
   2782 			txd->txdw1 |= htole32(
   2783 			    SM(R88E_TXDW1_MACID, RTWN_MACID_BSS) |
   2784 			    SM(R92C_TXDW1_QSEL, tid) |
   2785 			    SM(R92C_TXDW1_RAID, raid) |
   2786 			    R92C_TXDW1_AGGBK);
   2787 		} else
   2788 			txd->txdw1 |= htole32(
   2789 			    SM(R92C_TXDW1_MACID, RTWN_MACID_BSS) |
   2790 			    SM(R92C_TXDW1_QSEL, tid) |
   2791 			    SM(R92C_TXDW1_RAID, raid) |
   2792 			    R92C_TXDW1_AGGBK);
   2793 
   2794 		if (ISSET(sc->chip, URTWN_CHIP_88E))
   2795 			txd->txdw2 |= htole32(R88E_TXDW2_AGGBK);
   2796 		if (ISSET(sc->chip, URTWN_CHIP_92EU))
   2797 			txd->txdw3 |= htole32(R92E_TXDW3_AGGBK);
   2798 
   2799 		if (hasqos) {
   2800 			txd->txdw4 |= htole32(R92C_TXDW4_QOS);
   2801 		}
   2802 
   2803 		if (ic->ic_flags & IEEE80211_F_USEPROT) {
   2804 			/* for 11g */
   2805 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) {
   2806 				txd->txdw4 |= htole32(R92C_TXDW4_CTS2SELF |
   2807 				    R92C_TXDW4_HWRTSEN);
   2808 			} else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) {
   2809 				txd->txdw4 |= htole32(R92C_TXDW4_RTSEN |
   2810 				    R92C_TXDW4_HWRTSEN);
   2811 			}
   2812 		}
   2813 		/* Send RTS at OFDM24. */
   2814 		txd->txdw4 |= htole32(SM(R92C_TXDW4_RTSRATE, 8));
   2815 		txd->txdw5 |= htole32(0x0001ff00);
   2816 		/* Send data at OFDM54. */
   2817 		if (ISSET(sc->chip, URTWN_CHIP_88E))
   2818 			txd->txdw5 |= htole32(0x13 & 0x3f);
   2819 		else
   2820 			txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, 11));
   2821 	} else if (type == IEEE80211_FC0_TYPE_MGT) {
   2822 		DPRINTFN(DBG_TX, "mgmt packet", 0, 0, 0, 0);
   2823 		txd->txdw1 |= htole32(
   2824 		    SM(R92C_TXDW1_MACID, RTWN_MACID_BSS) |
   2825 		    SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_MGNT) |
   2826 		    SM(R92C_TXDW1_RAID, R92C_RAID_11B));
   2827 
   2828 		/* Force CCK1. */
   2829 		txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE);
   2830 		/* Use 1Mbps */
   2831 		txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, 0));
   2832 	} else {
   2833 		/* broadcast or multicast packets */
   2834 		DPRINTFN(DBG_TX, "bc or mc packet", 0, 0, 0, 0);
   2835 		txd->txdw1 |= htole32(
   2836 		    SM(R92C_TXDW1_MACID, RTWN_MACID_BC) |
   2837 		    SM(R92C_TXDW1_RAID, R92C_RAID_11B));
   2838 
   2839 		/* Force CCK1. */
   2840 		txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE);
   2841 		/* Use 1Mbps */
   2842 		txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, 0));
   2843 	}
   2844 	/* Set sequence number */
   2845 	seq = LE_READ_2(&wh->i_seq[0]) >> IEEE80211_SEQ_SEQ_SHIFT;
   2846 	if (!ISSET(sc->chip, URTWN_CHIP_92EU)) {
   2847 		txd->txdseq |= htole16(seq);
   2848 
   2849 		if (!hasqos) {
   2850 			/* Use HW sequence numbering for non-QoS frames. */
   2851 			txd->txdw4  |= htole32(R92C_TXDW4_HWSEQ);
   2852 			txd->txdseq |= htole16(R92C_HWSEQ_EN);
   2853 		}
   2854 	} else {
   2855 		txd->txdseq2 |= htole16((seq & R92E_HWSEQ_MASK) <<
   2856 		    R92E_HWSEQ_SHIFT);
   2857 		if (!hasqos) {
   2858 			/* Use HW sequence numbering for non-QoS frames. */
   2859 			txd->txdw4  |= htole32(R92C_TXDW4_HWSEQ);
   2860 			txd->txdw7 |= htole16(R92C_HWSEQ_EN);
   2861 		}
   2862 	}
   2863 
   2864 	/* Compute Tx descriptor checksum. */
   2865 	sum = 0;
   2866 	for (i = 0; i < R92C_TXDESC_SUMSIZE / 2; i++)
   2867 		sum ^= ((uint16_t *)txd)[i];
   2868 	txd->txdsum = sum;	/* NB: already little endian. */
   2869 
   2870 	xferlen = txd_len + m->m_pkthdr.len + padsize;
   2871 	m_copydata(m, 0, m->m_pkthdr.len, (char *)&txd[0] + txd_len + padsize);
   2872 
   2873 	s = splnet();
   2874 	usbd_setup_xfer(data->xfer, data, data->buf, xferlen,
   2875 	    USBD_FORCE_SHORT_XFER, URTWN_TX_TIMEOUT,
   2876 	    urtwn_txeof);
   2877 	error = usbd_transfer(data->xfer);
   2878 	if (__predict_false(error != USBD_NORMAL_COMPLETION &&
   2879 	    error != USBD_IN_PROGRESS)) {
   2880 		splx(s);
   2881 		DPRINTFN(DBG_TX, "transfer failed %jd", error, 0, 0, 0);
   2882 		return error;
   2883 	}
   2884 	splx(s);
   2885 	return 0;
   2886 }
   2887 
   2888 struct urtwn_tx_data *
   2889 urtwn_get_tx_data(struct urtwn_softc *sc, size_t pidx)
   2890 {
   2891 	struct urtwn_tx_data *data = NULL;
   2892 
   2893 	mutex_enter(&sc->sc_tx_mtx);
   2894 	if (!TAILQ_EMPTY(&sc->tx_free_list[pidx])) {
   2895 		data = TAILQ_FIRST(&sc->tx_free_list[pidx]);
   2896 		TAILQ_REMOVE(&sc->tx_free_list[pidx], data, next);
   2897 	}
   2898 	mutex_exit(&sc->sc_tx_mtx);
   2899 
   2900 	return data;
   2901 }
   2902 
   2903 static void
   2904 urtwn_start(struct ifnet *ifp)
   2905 {
   2906 	struct urtwn_softc *sc = ifp->if_softc;
   2907 	struct ieee80211com *ic = &sc->sc_ic;
   2908 	struct urtwn_tx_data *data;
   2909 	struct ether_header *eh;
   2910 	struct ieee80211_node *ni;
   2911 	struct mbuf *m;
   2912 
   2913 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   2914 
   2915 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   2916 		return;
   2917 
   2918 	data = NULL;
   2919 	for (;;) {
   2920 		/* Send pending management frames first. */
   2921 		IF_POLL(&ic->ic_mgtq, m);
   2922 		if (m != NULL) {
   2923 			/* Use AC_VO for management frames. */
   2924 
   2925 			data = urtwn_get_tx_data(sc, sc->ac2idx[WME_AC_VO]);
   2926 
   2927 			if (data == NULL) {
   2928 				ifp->if_flags |= IFF_OACTIVE;
   2929 				DPRINTFN(DBG_TX, "empty tx_free_list",
   2930 				    0, 0, 0, 0);
   2931 				return;
   2932 			}
   2933 			IF_DEQUEUE(&ic->ic_mgtq, m);
   2934 			ni = M_GETCTX(m, struct ieee80211_node *);
   2935 			M_CLEARCTX(m);
   2936 			goto sendit;
   2937 		}
   2938 		if (ic->ic_state != IEEE80211_S_RUN)
   2939 			break;
   2940 
   2941 		/* Encapsulate and send data frames. */
   2942 		IFQ_POLL(&ifp->if_snd, m);
   2943 		if (m == NULL)
   2944 			break;
   2945 
   2946 		struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
   2947 		uint8_t type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
   2948 		uint8_t qid = WME_AC_BE;
   2949 		if (ieee80211_has_qos(wh)) {
   2950 			/* data frames in 11n mode */
   2951 			struct ieee80211_qosframe *qwh = (void *)wh;
   2952 			uint8_t tid = qwh->i_qos[0] & IEEE80211_QOS_TID;
   2953 			qid = TID_TO_WME_AC(tid);
   2954 		} else if (type != IEEE80211_FC0_TYPE_DATA) {
   2955 			qid = WME_AC_VO;
   2956 		}
   2957 		data = urtwn_get_tx_data(sc, sc->ac2idx[qid]);
   2958 
   2959 		if (data == NULL) {
   2960 			ifp->if_flags |= IFF_OACTIVE;
   2961 			DPRINTFN(DBG_TX, "empty tx_free_list", 0, 0, 0, 0);
   2962 			return;
   2963 		}
   2964 		IFQ_DEQUEUE(&ifp->if_snd, m);
   2965 
   2966 		if (m->m_len < (int)sizeof(*eh) &&
   2967 		    (m = m_pullup(m, sizeof(*eh))) == NULL) {
   2968 			device_printf(sc->sc_dev, "m_pullup failed\n");
   2969 			if_statinc(ifp, if_oerrors);
   2970 			urtwn_put_tx_data(sc, data);
   2971 			m_freem(m);
   2972 			continue;
   2973 		}
   2974 		eh = mtod(m, struct ether_header *);
   2975 		ni = ieee80211_find_txnode(ic, eh->ether_dhost);
   2976 		if (ni == NULL) {
   2977 			device_printf(sc->sc_dev,
   2978 			    "unable to find transmit node\n");
   2979 			if_statinc(ifp, if_oerrors);
   2980 			urtwn_put_tx_data(sc, data);
   2981 			m_freem(m);
   2982 			continue;
   2983 		}
   2984 
   2985 		bpf_mtap(ifp, m, BPF_D_OUT);
   2986 
   2987 		if ((m = ieee80211_encap(ic, m, ni)) == NULL) {
   2988 			ieee80211_free_node(ni);
   2989 			device_printf(sc->sc_dev,
   2990 			    "unable to encapsulate packet\n");
   2991 			if_statinc(ifp, if_oerrors);
   2992 			urtwn_put_tx_data(sc, data);
   2993 			m_freem(m);
   2994 			continue;
   2995 		}
   2996  sendit:
   2997 		bpf_mtap3(ic->ic_rawbpf, m, BPF_D_OUT);
   2998 
   2999 		if (urtwn_tx(sc, m, ni, data) != 0) {
   3000 			m_freem(m);
   3001 			ieee80211_free_node(ni);
   3002 			device_printf(sc->sc_dev,
   3003 			    "unable to transmit packet\n");
   3004 			if_statinc(ifp, if_oerrors);
   3005 			continue;
   3006 		}
   3007 		m_freem(m);
   3008 		ieee80211_free_node(ni);
   3009 		sc->tx_timer = 5;
   3010 		ifp->if_timer = 1;
   3011 	}
   3012 }
   3013 
   3014 static void
   3015 urtwn_watchdog(struct ifnet *ifp)
   3016 {
   3017 	struct urtwn_softc *sc = ifp->if_softc;
   3018 
   3019 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   3020 
   3021 	ifp->if_timer = 0;
   3022 
   3023 	if (sc->tx_timer > 0) {
   3024 		if (--sc->tx_timer == 0) {
   3025 			device_printf(sc->sc_dev, "device timeout\n");
   3026 			/* urtwn_init(ifp); XXX needs a process context! */
   3027 			if_statinc(ifp, if_oerrors);
   3028 			return;
   3029 		}
   3030 		ifp->if_timer = 1;
   3031 	}
   3032 	ieee80211_watchdog(&sc->sc_ic);
   3033 }
   3034 
   3035 static int
   3036 urtwn_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   3037 {
   3038 	struct urtwn_softc *sc = ifp->if_softc;
   3039 	struct ieee80211com *ic = &sc->sc_ic;
   3040 	int s, error = 0;
   3041 
   3042 	URTWNHIST_FUNC();
   3043 	URTWNHIST_CALLARGS("cmd=0x%08jx, data=%#jx", cmd, (uintptr_t)data,
   3044 	    0, 0);
   3045 
   3046 	s = splnet();
   3047 
   3048 	switch (cmd) {
   3049 	case SIOCSIFFLAGS:
   3050 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   3051 			break;
   3052 		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
   3053 		case IFF_UP | IFF_RUNNING:
   3054 			break;
   3055 		case IFF_UP:
   3056 			urtwn_init(ifp);
   3057 			break;
   3058 		case IFF_RUNNING:
   3059 			urtwn_stop(ifp, 1);
   3060 			break;
   3061 		case 0:
   3062 			break;
   3063 		}
   3064 		break;
   3065 
   3066 	case SIOCADDMULTI:
   3067 	case SIOCDELMULTI:
   3068 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   3069 			/* setup multicast filter, etc */
   3070 			error = 0;
   3071 		}
   3072 		break;
   3073 
   3074 	case SIOCS80211CHANNEL:
   3075 		/*
   3076 		 * This allows for fast channel switching in monitor mode
   3077 		 * (used by kismet). In IBSS mode, we must explicitly reset
   3078 		 * the interface to generate a new beacon frame.
   3079 		 */
   3080 		error = ieee80211_ioctl(ic, cmd, data);
   3081 		if (error == ENETRESET &&
   3082 		    ic->ic_opmode == IEEE80211_M_MONITOR) {
   3083 			urtwn_set_chan(sc, ic->ic_curchan,
   3084 			    IEEE80211_HTINFO_2NDCHAN_NONE);
   3085 			error = 0;
   3086 		}
   3087 		break;
   3088 
   3089 	default:
   3090 		error = ieee80211_ioctl(ic, cmd, data);
   3091 		break;
   3092 	}
   3093 	if (error == ENETRESET) {
   3094 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
   3095 		    (IFF_UP | IFF_RUNNING) &&
   3096 		    ic->ic_roaming != IEEE80211_ROAMING_MANUAL) {
   3097 			urtwn_init(ifp);
   3098 		}
   3099 		error = 0;
   3100 	}
   3101 
   3102 	splx(s);
   3103 
   3104 	return error;
   3105 }
   3106 
   3107 static __inline int
   3108 urtwn_power_on(struct urtwn_softc *sc)
   3109 {
   3110 
   3111 	return sc->sc_power_on(sc);
   3112 }
   3113 
   3114 static int
   3115 urtwn_r92c_power_on(struct urtwn_softc *sc)
   3116 {
   3117 	uint32_t reg;
   3118 	int ntries;
   3119 
   3120 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   3121 
   3122 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   3123 
   3124 	/* Wait for autoload done bit. */
   3125 	for (ntries = 0; ntries < 1000; ntries++) {
   3126 		if (urtwn_read_1(sc, R92C_APS_FSMCO) & R92C_APS_FSMCO_PFM_ALDN)
   3127 			break;
   3128 		DELAY(5);
   3129 	}
   3130 	if (ntries == 1000) {
   3131 		aprint_error_dev(sc->sc_dev,
   3132 		    "timeout waiting for chip autoload\n");
   3133 		return ETIMEDOUT;
   3134 	}
   3135 
   3136 	/* Unlock ISO/CLK/Power control register. */
   3137 	urtwn_write_1(sc, R92C_RSV_CTRL, 0);
   3138 	DELAY(5);
   3139 	/* Move SPS into PWM mode. */
   3140 	urtwn_write_1(sc, R92C_SPS0_CTRL, 0x2b);
   3141 	DELAY(5);
   3142 
   3143 	reg = urtwn_read_1(sc, R92C_LDOV12D_CTRL);
   3144 	if (!(reg & R92C_LDOV12D_CTRL_LDV12_EN)) {
   3145 		urtwn_write_1(sc, R92C_LDOV12D_CTRL,
   3146 		    reg | R92C_LDOV12D_CTRL_LDV12_EN);
   3147 		DELAY(100);
   3148 		urtwn_write_1(sc, R92C_SYS_ISO_CTRL,
   3149 		    urtwn_read_1(sc, R92C_SYS_ISO_CTRL) &
   3150 		    ~R92C_SYS_ISO_CTRL_MD2PP);
   3151 	}
   3152 
   3153 	/* Auto enable WLAN. */
   3154 	urtwn_write_2(sc, R92C_APS_FSMCO,
   3155 	    urtwn_read_2(sc, R92C_APS_FSMCO) | R92C_APS_FSMCO_APFM_ONMAC);
   3156 	for (ntries = 0; ntries < 1000; ntries++) {
   3157 		if (!(urtwn_read_2(sc, R92C_APS_FSMCO) &
   3158 		    R92C_APS_FSMCO_APFM_ONMAC))
   3159 			break;
   3160 		DELAY(100);
   3161 	}
   3162 	if (ntries == 1000) {
   3163 		aprint_error_dev(sc->sc_dev,
   3164 		    "timeout waiting for MAC auto ON\n");
   3165 		return ETIMEDOUT;
   3166 	}
   3167 
   3168 	/* Enable radio, GPIO and LED functions. */
   3169 	KASSERT((R92C_APS_FSMCO_AFSM_HSUS | R92C_APS_FSMCO_PDN_EN |
   3170 	    R92C_APS_FSMCO_PFM_ALDN) == 0x0812);
   3171 	urtwn_write_2(sc, R92C_APS_FSMCO,
   3172 	    R92C_APS_FSMCO_AFSM_HSUS |
   3173 	    R92C_APS_FSMCO_PDN_EN |
   3174 	    R92C_APS_FSMCO_PFM_ALDN);
   3175 
   3176 	/* Release RF digital isolation. */
   3177 	urtwn_write_2(sc, R92C_SYS_ISO_CTRL,
   3178 	    urtwn_read_2(sc, R92C_SYS_ISO_CTRL) & ~R92C_SYS_ISO_CTRL_DIOR);
   3179 
   3180 	/* Initialize MAC. */
   3181 	urtwn_write_1(sc, R92C_APSD_CTRL,
   3182 	    urtwn_read_1(sc, R92C_APSD_CTRL) & ~R92C_APSD_CTRL_OFF);
   3183 	for (ntries = 0; ntries < 200; ntries++) {
   3184 		if (!(urtwn_read_1(sc, R92C_APSD_CTRL) &
   3185 		    R92C_APSD_CTRL_OFF_STATUS))
   3186 			break;
   3187 		DELAY(5);
   3188 	}
   3189 	if (ntries == 200) {
   3190 		aprint_error_dev(sc->sc_dev,
   3191 		    "timeout waiting for MAC initialization\n");
   3192 		return ETIMEDOUT;
   3193 	}
   3194 
   3195 	/* Enable MAC DMA/WMAC/SCHEDULE/SEC blocks. */
   3196 	reg = urtwn_read_2(sc, R92C_CR);
   3197 	reg |= R92C_CR_HCI_TXDMA_EN | R92C_CR_HCI_RXDMA_EN |
   3198 	    R92C_CR_TXDMA_EN | R92C_CR_RXDMA_EN | R92C_CR_PROTOCOL_EN |
   3199 	    R92C_CR_SCHEDULE_EN | R92C_CR_MACTXEN | R92C_CR_MACRXEN |
   3200 	    R92C_CR_ENSEC;
   3201 	urtwn_write_2(sc, R92C_CR, reg);
   3202 
   3203 	urtwn_write_1(sc, 0xfe10, 0x19);
   3204 
   3205 	urtwn_delay_ms(sc, 1);
   3206 
   3207 	return 0;
   3208 }
   3209 
   3210 static int
   3211 urtwn_r92e_power_on(struct urtwn_softc *sc)
   3212 {
   3213 	uint32_t reg;
   3214 	uint32_t val;
   3215 	int ntries;
   3216 
   3217 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   3218 
   3219 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   3220 
   3221 	/* Enable radio, GPIO and LED functions. */
   3222 	KASSERT((R92C_APS_FSMCO_AFSM_HSUS | R92C_APS_FSMCO_PDN_EN |
   3223 	    R92C_APS_FSMCO_PFM_ALDN) == 0x0812);
   3224 	urtwn_write_2(sc, R92C_APS_FSMCO,
   3225 	    R92C_APS_FSMCO_AFSM_HSUS |
   3226 	    R92C_APS_FSMCO_PDN_EN |
   3227 	    R92C_APS_FSMCO_PFM_ALDN);
   3228 
   3229 	if (urtwn_read_4(sc, R92E_SYS_CFG1_8192E) & R92E_SPSLDO_SEL){
   3230 		/* LDO. */
   3231 		urtwn_write_1(sc, R92E_LDO_SWR_CTRL, 0xc3);
   3232 	}
   3233 	else	{
   3234 		urtwn_write_2(sc, R92C_SYS_SWR_CTRL2, urtwn_read_2(sc,
   3235 		    R92C_SYS_SWR_CTRL2) & 0xffff);
   3236 		urtwn_write_1(sc, R92E_LDO_SWR_CTRL, 0x83);
   3237 	}
   3238 
   3239 	for (ntries = 0; ntries < 2; ntries++) {
   3240 		urtwn_write_1(sc, R92C_AFE_PLL_CTRL,
   3241 		    urtwn_read_1(sc, R92C_AFE_PLL_CTRL));
   3242 		urtwn_write_2(sc, R92C_AFE_CTRL4, urtwn_read_2(sc,
   3243 		    R92C_AFE_CTRL4));
   3244 	}
   3245 
   3246 	/* Reset BB. */
   3247 	urtwn_write_1(sc, R92C_SYS_FUNC_EN,
   3248 	urtwn_read_1(sc, R92C_SYS_FUNC_EN) & ~(R92C_SYS_FUNC_EN_BBRSTB |
   3249 	    R92C_SYS_FUNC_EN_BB_GLB_RST));
   3250 
   3251 	urtwn_write_1(sc, R92C_AFE_XTAL_CTRL + 2, urtwn_read_1(sc,
   3252 	    R92C_AFE_XTAL_CTRL + 2) | 0x80);
   3253 
   3254 	/* Disable HWPDN. */
   3255 	urtwn_write_2(sc, R92C_APS_FSMCO, urtwn_read_2(sc,
   3256 	    R92C_APS_FSMCO) & ~R92C_APS_FSMCO_APDM_HPDN);
   3257 
   3258 	/* Disable WL suspend. */
   3259 	urtwn_write_2(sc, R92C_APS_FSMCO, urtwn_read_2(sc,
   3260 	    R92C_APS_FSMCO) & ~(R92C_APS_FSMCO_AFSM_PCIE |
   3261 	    R92C_APS_FSMCO_AFSM_HSUS));
   3262 
   3263 	urtwn_write_4(sc, R92C_APS_FSMCO, urtwn_read_4(sc,
   3264 	    R92C_APS_FSMCO) | R92C_APS_FSMCO_RDY_MACON);
   3265 	urtwn_write_2(sc, R92C_APS_FSMCO, urtwn_read_2(sc,
   3266 	    R92C_APS_FSMCO) | R92C_APS_FSMCO_APFM_ONMAC);
   3267 	for (ntries = 0; ntries < 10000; ntries++) {
   3268 		val = urtwn_read_2(sc, R92C_APS_FSMCO) &
   3269 		 R92C_APS_FSMCO_APFM_ONMAC;
   3270 		if (val == 0x0)
   3271 			break;
   3272 		DELAY(10);
   3273 	}
   3274 	if (ntries == 10000) {
   3275 		aprint_error_dev(sc->sc_dev,
   3276 		    "timeout waiting for chip power up\n");
   3277 		return ETIMEDOUT;
   3278 	}
   3279 
   3280 	urtwn_write_2(sc, R92C_CR, 0x00);
   3281 	reg = urtwn_read_2(sc, R92C_CR);
   3282 	reg |= R92C_CR_HCI_TXDMA_EN | R92C_CR_HCI_RXDMA_EN |
   3283 	    R92C_CR_TXDMA_EN | R92C_CR_RXDMA_EN | R92C_CR_PROTOCOL_EN |
   3284 	    R92C_CR_SCHEDULE_EN | R92C_CR_ENSEC;
   3285 	urtwn_write_2(sc, R92C_CR, reg);
   3286 
   3287 	return 0;
   3288 }
   3289 
   3290 static int
   3291 urtwn_r88e_power_on(struct urtwn_softc *sc)
   3292 {
   3293 	uint32_t reg;
   3294 	uint8_t val;
   3295 	int ntries;
   3296 
   3297 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   3298 
   3299 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   3300 
   3301 	/* Wait for power ready bit. */
   3302 	for (ntries = 0; ntries < 5000; ntries++) {
   3303 		val = urtwn_read_1(sc, 0x6) & 0x2;
   3304 		if (val == 0x2)
   3305 			break;
   3306 		DELAY(10);
   3307 	}
   3308 	if (ntries == 5000) {
   3309 		aprint_error_dev(sc->sc_dev,
   3310 		    "timeout waiting for chip power up\n");
   3311 		return ETIMEDOUT;
   3312 	}
   3313 
   3314 	/* Reset BB. */
   3315 	urtwn_write_1(sc, R92C_SYS_FUNC_EN,
   3316 	urtwn_read_1(sc, R92C_SYS_FUNC_EN) & ~(R92C_SYS_FUNC_EN_BBRSTB |
   3317 	    R92C_SYS_FUNC_EN_BB_GLB_RST));
   3318 
   3319 	urtwn_write_1(sc, 0x26, urtwn_read_1(sc, 0x26) | 0x80);
   3320 
   3321 	/* Disable HWPDN. */
   3322 	urtwn_write_1(sc, 0x5, urtwn_read_1(sc, 0x5) & ~0x80);
   3323 
   3324 	/* Disable WL suspend. */
   3325 	urtwn_write_1(sc, 0x5, urtwn_read_1(sc, 0x5) & ~0x18);
   3326 
   3327 	urtwn_write_1(sc, 0x5, urtwn_read_1(sc, 0x5) | 0x1);
   3328 	for (ntries = 0; ntries < 5000; ntries++) {
   3329 		if (!(urtwn_read_1(sc, 0x5) & 0x1))
   3330 			break;
   3331 		DELAY(10);
   3332 	}
   3333 	if (ntries == 5000)
   3334 		return ETIMEDOUT;
   3335 
   3336 	/* Enable LDO normal mode. */
   3337 	urtwn_write_1(sc, 0x23, urtwn_read_1(sc, 0x23) & ~0x10);
   3338 
   3339 	/* Enable MAC DMA/WMAC/SCHEDULE/SEC blocks. */
   3340 	urtwn_write_2(sc, R92C_CR, 0);
   3341 	reg = urtwn_read_2(sc, R92C_CR);
   3342 	reg |= R92C_CR_HCI_TXDMA_EN | R92C_CR_HCI_RXDMA_EN |
   3343 	    R92C_CR_TXDMA_EN | R92C_CR_RXDMA_EN | R92C_CR_PROTOCOL_EN |
   3344 	    R92C_CR_SCHEDULE_EN | R92C_CR_ENSEC | R92C_CR_CALTMR_EN;
   3345 	urtwn_write_2(sc, R92C_CR, reg);
   3346 
   3347 	return 0;
   3348 }
   3349 
   3350 static int __noinline
   3351 urtwn_llt_init(struct urtwn_softc *sc)
   3352 {
   3353 	size_t i, page_count, pktbuf_count;
   3354 	uint32_t val;
   3355 	int error;
   3356 
   3357 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   3358 
   3359 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   3360 
   3361 	if (sc->chip & URTWN_CHIP_88E)
   3362 		page_count = R88E_TX_PAGE_COUNT;
   3363 	else if (sc->chip & URTWN_CHIP_92EU)
   3364 		page_count = R92E_TX_PAGE_COUNT;
   3365 	else
   3366 		page_count = R92C_TX_PAGE_COUNT;
   3367 	if (sc->chip & URTWN_CHIP_88E)
   3368 		pktbuf_count = R88E_TXPKTBUF_COUNT;
   3369 	else if (sc->chip & URTWN_CHIP_92EU)
   3370 		pktbuf_count = R88E_TXPKTBUF_COUNT;
   3371 	else
   3372 		pktbuf_count = R92C_TXPKTBUF_COUNT;
   3373 
   3374 	if (sc->chip & URTWN_CHIP_92EU) {
   3375 		val = urtwn_read_4(sc, R92E_AUTO_LLT) | R92E_AUTO_LLT_EN;
   3376 		urtwn_write_4(sc, R92E_AUTO_LLT, val);
   3377 		DELAY(100);
   3378 		val = urtwn_read_4(sc, R92E_AUTO_LLT);
   3379 		if (val & R92E_AUTO_LLT_EN)
   3380 			return EIO;
   3381 		return 0;
   3382 	}
   3383 
   3384 	/* Reserve pages [0; page_count]. */
   3385 	for (i = 0; i < page_count; i++) {
   3386 		if ((error = urtwn_llt_write(sc, i, i + 1)) != 0)
   3387 			return error;
   3388 	}
   3389 	/* NB: 0xff indicates end-of-list. */
   3390 	if ((error = urtwn_llt_write(sc, i, 0xff)) != 0)
   3391 		return error;
   3392 	/*
   3393 	 * Use pages [page_count + 1; pktbuf_count - 1]
   3394 	 * as ring buffer.
   3395 	 */
   3396 	for (++i; i < pktbuf_count - 1; i++) {
   3397 		if ((error = urtwn_llt_write(sc, i, i + 1)) != 0)
   3398 			return error;
   3399 	}
   3400 	/* Make the last page point to the beginning of the ring buffer. */
   3401 	error = urtwn_llt_write(sc, i, pktbuf_count + 1);
   3402 	return error;
   3403 }
   3404 
   3405 static void
   3406 urtwn_fw_reset(struct urtwn_softc *sc)
   3407 {
   3408 	uint16_t reg;
   3409 	int ntries;
   3410 
   3411 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   3412 
   3413 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   3414 
   3415 	/* Tell 8051 to reset itself. */
   3416 	mutex_enter(&sc->sc_fwcmd_mtx);
   3417 	urtwn_write_1(sc, R92C_HMETFR + 3, 0x20);
   3418 	sc->fwcur = 0;
   3419 	mutex_exit(&sc->sc_fwcmd_mtx);
   3420 
   3421 	/* Wait until 8051 resets by itself. */
   3422 	for (ntries = 0; ntries < 100; ntries++) {
   3423 		reg = urtwn_read_2(sc, R92C_SYS_FUNC_EN);
   3424 		if (!(reg & R92C_SYS_FUNC_EN_CPUEN))
   3425 			return;
   3426 		DELAY(50);
   3427 	}
   3428 	/* Force 8051 reset. */
   3429 	urtwn_write_2(sc, R92C_SYS_FUNC_EN,
   3430 	    urtwn_read_2(sc, R92C_SYS_FUNC_EN) & ~R92C_SYS_FUNC_EN_CPUEN);
   3431 }
   3432 
   3433 static void
   3434 urtwn_r88e_fw_reset(struct urtwn_softc *sc)
   3435 {
   3436 	uint16_t reg;
   3437 
   3438 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   3439 
   3440 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   3441 
   3442 	if (ISSET(sc->chip, URTWN_CHIP_92EU)) {
   3443 		reg = urtwn_read_2(sc, R92C_RSV_CTRL) & ~R92E_RSV_MIO_EN;
   3444 		urtwn_write_2(sc,R92C_RSV_CTRL, reg);
   3445 	}
   3446 	DELAY(50);
   3447 
   3448 	reg = urtwn_read_2(sc, R92C_SYS_FUNC_EN);
   3449 	urtwn_write_2(sc, R92C_SYS_FUNC_EN, reg & ~R92C_SYS_FUNC_EN_CPUEN);
   3450 	DELAY(50);
   3451 
   3452 	urtwn_write_2(sc, R92C_SYS_FUNC_EN, reg | R92C_SYS_FUNC_EN_CPUEN);
   3453 	DELAY(50);
   3454 
   3455 	if (ISSET(sc->chip, URTWN_CHIP_92EU)) {
   3456 		reg = urtwn_read_2(sc, R92C_RSV_CTRL) | R92E_RSV_MIO_EN;
   3457 		urtwn_write_2(sc,R92C_RSV_CTRL, reg);
   3458 	}
   3459 	DELAY(50);
   3460 
   3461 	mutex_enter(&sc->sc_fwcmd_mtx);
   3462 	/* Init firmware commands ring. */
   3463 	sc->fwcur = 0;
   3464 	mutex_exit(&sc->sc_fwcmd_mtx);
   3465 
   3466 }
   3467 
   3468 static int
   3469 urtwn_fw_loadpage(struct urtwn_softc *sc, int page, uint8_t *buf, int len)
   3470 {
   3471 	uint32_t reg;
   3472 	int off, mlen, error = 0;
   3473 
   3474 	URTWNHIST_FUNC();
   3475 	URTWNHIST_CALLARGS("page=%jd, buf=%#jx, len=%jd",
   3476 	    page, (uintptr_t)buf, len, 0);
   3477 
   3478 	reg = urtwn_read_4(sc, R92C_MCUFWDL);
   3479 	reg = RW(reg, R92C_MCUFWDL_PAGE, page);
   3480 	urtwn_write_4(sc, R92C_MCUFWDL, reg);
   3481 
   3482 	off = R92C_FW_START_ADDR;
   3483 	while (len > 0) {
   3484 		if (len > 196)
   3485 			mlen = 196;
   3486 		else if (len > 4)
   3487 			mlen = 4;
   3488 		else
   3489 			mlen = 1;
   3490 		error = urtwn_write_region(sc, off, buf, mlen);
   3491 		if (error != 0)
   3492 			break;
   3493 		off += mlen;
   3494 		buf += mlen;
   3495 		len -= mlen;
   3496 	}
   3497 	return error;
   3498 }
   3499 
   3500 static int __noinline
   3501 urtwn_load_firmware(struct urtwn_softc *sc)
   3502 {
   3503 	firmware_handle_t fwh;
   3504 	const struct r92c_fw_hdr *hdr;
   3505 	const char *name;
   3506 	u_char *fw, *ptr;
   3507 	size_t len;
   3508 	uint32_t reg;
   3509 	int mlen, ntries, page, error;
   3510 
   3511 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   3512 
   3513 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   3514 
   3515 	/* Read firmware image from the filesystem. */
   3516 	if (ISSET(sc->chip, URTWN_CHIP_88E))
   3517 		name = "rtl8188eufw.bin";
   3518 	else if (ISSET(sc->chip, URTWN_CHIP_92EU))
   3519 		name = "rtl8192eefw.bin";
   3520 	else if ((sc->chip & (URTWN_CHIP_UMC_A_CUT | URTWN_CHIP_92C)) ==
   3521 	    URTWN_CHIP_UMC_A_CUT)
   3522 		name = "rtl8192cfwU.bin";
   3523 	else
   3524 		name = "rtl8192cfw.bin";
   3525 	if ((error = firmware_open("if_urtwn", name, &fwh)) != 0) {
   3526 		aprint_error_dev(sc->sc_dev,
   3527 		    "failed load firmware of file %s (error %d)\n", name,
   3528 		    error);
   3529 		return error;
   3530 	}
   3531 	const size_t fwlen = len = firmware_get_size(fwh);
   3532 	fw = firmware_malloc(len);
   3533 	if (fw == NULL) {
   3534 		aprint_error_dev(sc->sc_dev,
   3535 		    "failed to allocate firmware memory\n");
   3536 		firmware_close(fwh);
   3537 		return ENOMEM;
   3538 	}
   3539 	error = firmware_read(fwh, 0, fw, len);
   3540 	firmware_close(fwh);
   3541 	if (error != 0) {
   3542 		aprint_error_dev(sc->sc_dev,
   3543 		    "failed to read firmware (error %d)\n", error);
   3544 		firmware_free(fw, fwlen);
   3545 		return error;
   3546 	}
   3547 
   3548 	len = fwlen;
   3549 	ptr = fw;
   3550 	hdr = (const struct r92c_fw_hdr *)ptr;
   3551 	/* Check if there is a valid FW header and skip it. */
   3552 	if ((le16toh(hdr->signature) >> 4) == 0x88c ||
   3553 	    (le16toh(hdr->signature) >> 4) == 0x88e ||
   3554 	    (le16toh(hdr->signature) >> 4) == 0x92e ||
   3555 	    (le16toh(hdr->signature) >> 4) == 0x92c) {
   3556 		DPRINTFN(DBG_INIT, "FW V%jd.%jd",
   3557 		    le16toh(hdr->version), le16toh(hdr->subversion), 0, 0);
   3558 		DPRINTFN(DBG_INIT, "%02jd-%02jd %02jd:%02jd",
   3559 		    hdr->month, hdr->date, hdr->hour, hdr->minute);
   3560 		ptr += sizeof(*hdr);
   3561 		len -= sizeof(*hdr);
   3562 	}
   3563 
   3564 	if (urtwn_read_1(sc, R92C_MCUFWDL) & R92C_MCUFWDL_RAM_DL_SEL) {
   3565 		/* Reset MCU ready status */
   3566 		urtwn_write_1(sc, R92C_MCUFWDL, 0);
   3567 		if (ISSET(sc->chip, URTWN_CHIP_88E) ||
   3568 		    ISSET(sc->chip, URTWN_CHIP_92EU))
   3569 			urtwn_r88e_fw_reset(sc);
   3570 		else
   3571 			urtwn_fw_reset(sc);
   3572 	}
   3573 	if (!ISSET(sc->chip, URTWN_CHIP_88E) &&
   3574 	    !ISSET(sc->chip, URTWN_CHIP_92EU)) {
   3575 		urtwn_write_2(sc, R92C_SYS_FUNC_EN,
   3576 		    urtwn_read_2(sc, R92C_SYS_FUNC_EN) |
   3577 		    R92C_SYS_FUNC_EN_CPUEN);
   3578 	}
   3579 
   3580 	/* download enabled */
   3581 	urtwn_write_1(sc, R92C_MCUFWDL,
   3582 	    urtwn_read_1(sc, R92C_MCUFWDL) | R92C_MCUFWDL_EN);
   3583 	urtwn_write_1(sc, R92C_MCUFWDL + 2,
   3584 	    urtwn_read_1(sc, R92C_MCUFWDL + 2) & ~0x08);
   3585 
   3586 	/* Reset the FWDL checksum. */
   3587 	urtwn_write_1(sc, R92C_MCUFWDL,
   3588 	urtwn_read_1(sc, R92C_MCUFWDL) | R92C_MCUFWDL_CHKSUM_RPT);
   3589 
   3590 	DELAY(50);
   3591 	/* download firmware */
   3592 	for (page = 0; len > 0; page++) {
   3593 		mlen = MIN(len, R92C_FW_PAGE_SIZE);
   3594 		error = urtwn_fw_loadpage(sc, page, ptr, mlen);
   3595 		if (error != 0) {
   3596 			aprint_error_dev(sc->sc_dev,
   3597 			    "could not load firmware page %d\n", page);
   3598 			goto fail;
   3599 		}
   3600 		ptr += mlen;
   3601 		len -= mlen;
   3602 	}
   3603 
   3604 	/* download disable */
   3605 	urtwn_write_1(sc, R92C_MCUFWDL,
   3606 	    urtwn_read_1(sc, R92C_MCUFWDL) & ~R92C_MCUFWDL_EN);
   3607 	urtwn_write_1(sc, R92C_MCUFWDL + 1, 0);
   3608 
   3609 	/* Wait for checksum report. */
   3610 	for (ntries = 0; ntries < 1000; ntries++) {
   3611 		if (urtwn_read_4(sc, R92C_MCUFWDL) & R92C_MCUFWDL_CHKSUM_RPT)
   3612 			break;
   3613 		DELAY(5);
   3614 	}
   3615 	if (ntries == 1000) {
   3616 		aprint_error_dev(sc->sc_dev,
   3617 		    "timeout waiting for checksum report\n");
   3618 		error = ETIMEDOUT;
   3619 		goto fail;
   3620 	}
   3621 
   3622 	/* Wait for firmware readiness. */
   3623 	reg = urtwn_read_4(sc, R92C_MCUFWDL);
   3624 	reg = (reg & ~R92C_MCUFWDL_WINTINI_RDY) | R92C_MCUFWDL_RDY;
   3625 	urtwn_write_4(sc, R92C_MCUFWDL, reg);
   3626 	if (ISSET(sc->chip, URTWN_CHIP_88E) ||
   3627 	    ISSET(sc->chip, URTWN_CHIP_92EU))
   3628 		urtwn_r88e_fw_reset(sc);
   3629 	for (ntries = 0; ntries < 6000; ntries++) {
   3630 		if (urtwn_read_4(sc, R92C_MCUFWDL) & R92C_MCUFWDL_WINTINI_RDY)
   3631 			break;
   3632 		DELAY(5);
   3633 	}
   3634 	if (ntries == 6000) {
   3635 		aprint_error_dev(sc->sc_dev,
   3636 		    "timeout waiting for firmware readiness\n");
   3637 		error = ETIMEDOUT;
   3638 		goto fail;
   3639 	}
   3640  fail:
   3641 	firmware_free(fw, fwlen);
   3642 	return error;
   3643 }
   3644 
   3645 static __inline int
   3646 urtwn_dma_init(struct urtwn_softc *sc)
   3647 {
   3648 
   3649 	return sc->sc_dma_init(sc);
   3650 }
   3651 
   3652 static int
   3653 urtwn_r92c_dma_init(struct urtwn_softc *sc)
   3654 {
   3655 	int hashq, hasnq, haslq, nqueues, nqpages, nrempages;
   3656 	uint32_t reg;
   3657 	int error;
   3658 
   3659 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   3660 
   3661 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   3662 
   3663 	/* Initialize LLT table. */
   3664 	error = urtwn_llt_init(sc);
   3665 	if (error != 0)
   3666 		return error;
   3667 
   3668 	/* Get Tx queues to USB endpoints mapping. */
   3669 	hashq = hasnq = haslq = 0;
   3670 	reg = urtwn_read_2(sc, R92C_USB_EP + 1);
   3671 	DPRINTFN(DBG_INIT, "USB endpoints mapping %#jx", reg, 0, 0, 0);
   3672 	if (MS(reg, R92C_USB_EP_HQ) != 0)
   3673 		hashq = 1;
   3674 	if (MS(reg, R92C_USB_EP_NQ) != 0)
   3675 		hasnq = 1;
   3676 	if (MS(reg, R92C_USB_EP_LQ) != 0)
   3677 		haslq = 1;
   3678 	nqueues = hashq + hasnq + haslq;
   3679 	if (nqueues == 0)
   3680 		return EIO;
   3681 	/* Get the number of pages for each queue. */
   3682 	nqpages = (R92C_TX_PAGE_COUNT - R92C_PUBQ_NPAGES) / nqueues;
   3683 	/* The remaining pages are assigned to the high priority queue. */
   3684 	nrempages = (R92C_TX_PAGE_COUNT - R92C_PUBQ_NPAGES) % nqueues;
   3685 
   3686 	/* Set number of pages for normal priority queue. */
   3687 	urtwn_write_1(sc, R92C_RQPN_NPQ, hasnq ? nqpages : 0);
   3688 	urtwn_write_4(sc, R92C_RQPN,
   3689 	    /* Set number of pages for public queue. */
   3690 	    SM(R92C_RQPN_PUBQ, R92C_PUBQ_NPAGES) |
   3691 	    /* Set number of pages for high priority queue. */
   3692 	    SM(R92C_RQPN_HPQ, hashq ? nqpages + nrempages : 0) |
   3693 	    /* Set number of pages for low priority queue. */
   3694 	    SM(R92C_RQPN_LPQ, haslq ? nqpages : 0) |
   3695 	    /* Load values. */
   3696 	    R92C_RQPN_LD);
   3697 
   3698 	urtwn_write_1(sc, R92C_TXPKTBUF_BCNQ_BDNY, R92C_TX_PAGE_BOUNDARY);
   3699 	urtwn_write_1(sc, R92C_TXPKTBUF_MGQ_BDNY, R92C_TX_PAGE_BOUNDARY);
   3700 	urtwn_write_1(sc, R92C_TXPKTBUF_WMAC_LBK_BF_HD, R92C_TX_PAGE_BOUNDARY);
   3701 	urtwn_write_1(sc, R92C_TRXFF_BNDY, R92C_TX_PAGE_BOUNDARY);
   3702 	urtwn_write_1(sc, R92C_TDECTRL + 1, R92C_TX_PAGE_BOUNDARY);
   3703 
   3704 	/* Set queue to USB pipe mapping. */
   3705 	reg = urtwn_read_2(sc, R92C_TRXDMA_CTRL);
   3706 	reg &= ~R92C_TRXDMA_CTRL_QMAP_M;
   3707 	if (nqueues == 1) {
   3708 		if (hashq) {
   3709 			reg |= R92C_TRXDMA_CTRL_QMAP_HQ;
   3710 		} else if (hasnq) {
   3711 			reg |= R92C_TRXDMA_CTRL_QMAP_NQ;
   3712 		} else {
   3713 			reg |= R92C_TRXDMA_CTRL_QMAP_LQ;
   3714 		}
   3715 	} else if (nqueues == 2) {
   3716 		/* All 2-endpoints configs have a high priority queue. */
   3717 		if (!hashq) {
   3718 			return EIO;
   3719 		}
   3720 		if (hasnq) {
   3721 			reg |= R92C_TRXDMA_CTRL_QMAP_HQ_NQ;
   3722 		} else {
   3723 			reg |= R92C_TRXDMA_CTRL_QMAP_HQ_LQ;
   3724 		}
   3725 	} else {
   3726 		reg |= R92C_TRXDMA_CTRL_QMAP_3EP;
   3727 	}
   3728 	urtwn_write_2(sc, R92C_TRXDMA_CTRL, reg);
   3729 
   3730 	/* Set Tx/Rx transfer page boundary. */
   3731 	urtwn_write_2(sc, R92C_TRXFF_BNDY + 2, 0x27ff);
   3732 
   3733 	/* Set Tx/Rx transfer page size. */
   3734 	urtwn_write_1(sc, R92C_PBP,
   3735 	    SM(R92C_PBP_PSRX, R92C_PBP_128) | SM(R92C_PBP_PSTX, R92C_PBP_128));
   3736 	return 0;
   3737 }
   3738 
   3739 static int
   3740 urtwn_r88e_dma_init(struct urtwn_softc *sc)
   3741 {
   3742 	usb_interface_descriptor_t *id;
   3743 	uint32_t reg;
   3744 	int nqueues;
   3745 	int error;
   3746 
   3747 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   3748 
   3749 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   3750 
   3751 	/* Initialize LLT table. */
   3752 	error = urtwn_llt_init(sc);
   3753 	if (error != 0)
   3754 		return error;
   3755 
   3756 	/* Get Tx queues to USB endpoints mapping. */
   3757 	id = usbd_get_interface_descriptor(sc->sc_iface);
   3758 	nqueues = id->bNumEndpoints - 1;
   3759 	if (nqueues == 0)
   3760 		return EIO;
   3761 
   3762 	/* Set number of pages for normal priority queue. */
   3763 	urtwn_write_2(sc, R92C_RQPN_NPQ, 0);
   3764 	urtwn_write_2(sc, R92C_RQPN_NPQ, 0x000d);
   3765 	urtwn_write_4(sc, R92C_RQPN, 0x808e000d);
   3766 
   3767 	urtwn_write_1(sc, R92C_TXPKTBUF_BCNQ_BDNY, R88E_TX_PAGE_BOUNDARY);
   3768 	urtwn_write_1(sc, R92C_TXPKTBUF_MGQ_BDNY, R88E_TX_PAGE_BOUNDARY);
   3769 	urtwn_write_1(sc, R92C_TXPKTBUF_WMAC_LBK_BF_HD, R88E_TX_PAGE_BOUNDARY);
   3770 	urtwn_write_1(sc, R92C_TRXFF_BNDY, R88E_TX_PAGE_BOUNDARY);
   3771 	urtwn_write_1(sc, R92C_TDECTRL + 1, R88E_TX_PAGE_BOUNDARY);
   3772 
   3773 	/* Set queue to USB pipe mapping. */
   3774 	reg = urtwn_read_2(sc, R92C_TRXDMA_CTRL);
   3775 	reg &= ~R92C_TRXDMA_CTRL_QMAP_M;
   3776 	if (nqueues == 1)
   3777 		reg |= R92C_TRXDMA_CTRL_QMAP_LQ;
   3778 	else if (nqueues == 2)
   3779 		reg |= R92C_TRXDMA_CTRL_QMAP_HQ_NQ;
   3780 	else
   3781 		reg |= R92C_TRXDMA_CTRL_QMAP_3EP;
   3782 	urtwn_write_2(sc, R92C_TRXDMA_CTRL, reg);
   3783 
   3784 	/* Set Tx/Rx transfer page boundary. */
   3785 	urtwn_write_2(sc, R92C_TRXFF_BNDY + 2, 0x23ff);
   3786 
   3787 	/* Set Tx/Rx transfer page size. */
   3788 	urtwn_write_1(sc, R92C_PBP,
   3789 	    SM(R92C_PBP_PSRX, R92C_PBP_128) | SM(R92C_PBP_PSTX, R92C_PBP_128));
   3790 
   3791 	return 0;
   3792 }
   3793 
   3794 static void __noinline
   3795 urtwn_mac_init(struct urtwn_softc *sc)
   3796 {
   3797 	size_t i;
   3798 
   3799 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   3800 
   3801 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   3802 
   3803 	/* Write MAC initialization values. */
   3804 	if (ISSET(sc->chip, URTWN_CHIP_88E)) {
   3805 		for (i = 0; i < __arraycount(rtl8188eu_mac); i++)
   3806 			urtwn_write_1(sc, rtl8188eu_mac[i].reg,
   3807 			    rtl8188eu_mac[i].val);
   3808 	} else if (ISSET(sc->chip, URTWN_CHIP_92EU)) {
   3809 		for (i = 0; i < __arraycount(rtl8192eu_mac); i++)
   3810 			urtwn_write_1(sc, rtl8192eu_mac[i].reg,
   3811 			    rtl8192eu_mac[i].val);
   3812 	} else {
   3813 		for (i = 0; i < __arraycount(rtl8192cu_mac); i++)
   3814 			urtwn_write_1(sc, rtl8192cu_mac[i].reg,
   3815 			    rtl8192cu_mac[i].val);
   3816 	}
   3817 }
   3818 
   3819 static void __noinline
   3820 urtwn_bb_init(struct urtwn_softc *sc)
   3821 {
   3822 	const struct rtwn_bb_prog *prog;
   3823 	uint32_t reg;
   3824 	uint8_t crystalcap;
   3825 	size_t i;
   3826 
   3827 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   3828 
   3829 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   3830 
   3831 	/* Enable BB and RF. */
   3832 	urtwn_write_2(sc, R92C_SYS_FUNC_EN,
   3833 	    urtwn_read_2(sc, R92C_SYS_FUNC_EN) |
   3834 	    R92C_SYS_FUNC_EN_BBRSTB | R92C_SYS_FUNC_EN_BB_GLB_RST |
   3835 	    R92C_SYS_FUNC_EN_DIO_RF);
   3836 
   3837 	if (!ISSET(sc->chip, URTWN_CHIP_88E) &&
   3838 	    !ISSET(sc->chip, URTWN_CHIP_92EU)) {
   3839 		urtwn_write_1(sc, R92C_AFE_PLL_CTRL, 0x83);
   3840 		urtwn_write_1(sc, R92C_AFE_PLL_CTRL + 1, 0xdb);
   3841 	}
   3842 
   3843 	urtwn_write_1(sc, R92C_RF_CTRL,
   3844 	    R92C_RF_CTRL_EN | R92C_RF_CTRL_RSTB | R92C_RF_CTRL_SDMRSTB);
   3845 	urtwn_write_1(sc, R92C_SYS_FUNC_EN,
   3846 	    R92C_SYS_FUNC_EN_USBA | R92C_SYS_FUNC_EN_USBD |
   3847 	    R92C_SYS_FUNC_EN_BB_GLB_RST | R92C_SYS_FUNC_EN_BBRSTB);
   3848 
   3849 	if (!ISSET(sc->chip, URTWN_CHIP_88E) &&
   3850 	    !ISSET(sc->chip, URTWN_CHIP_92EU)) {
   3851 		urtwn_write_1(sc, R92C_LDOHCI12_CTRL, 0x0f);
   3852 		urtwn_write_1(sc, 0x15, 0xe9);
   3853 		urtwn_write_1(sc, R92C_AFE_XTAL_CTRL + 1, 0x80);
   3854 	}
   3855 
   3856 	/* Select BB programming based on board type. */
   3857 	if (ISSET(sc->chip, URTWN_CHIP_88E))
   3858 		prog = &rtl8188eu_bb_prog;
   3859 	else if (ISSET(sc->chip, URTWN_CHIP_92EU))
   3860 		prog = &rtl8192eu_bb_prog;
   3861 	else if (!(sc->chip & URTWN_CHIP_92C)) {
   3862 		if (sc->board_type == R92C_BOARD_TYPE_MINICARD) {
   3863 			prog = &rtl8188ce_bb_prog;
   3864 		} else if (sc->board_type == R92C_BOARD_TYPE_HIGHPA) {
   3865 			prog = &rtl8188ru_bb_prog;
   3866 		} else {
   3867 			prog = &rtl8188cu_bb_prog;
   3868 		}
   3869 	} else {
   3870 		if (sc->board_type == R92C_BOARD_TYPE_MINICARD) {
   3871 			prog = &rtl8192ce_bb_prog;
   3872 		} else {
   3873 			prog = &rtl8192cu_bb_prog;
   3874 		}
   3875 	}
   3876 	/* Write BB initialization values. */
   3877 	for (i = 0; i < prog->count; i++) {
   3878 		/* additional delay depend on registers */
   3879 		switch (prog->regs[i]) {
   3880 		case 0xfe:
   3881 			urtwn_delay_ms(sc, 50);
   3882 			break;
   3883 		case 0xfd:
   3884 			urtwn_delay_ms(sc, 5);
   3885 			break;
   3886 		case 0xfc:
   3887 			urtwn_delay_ms(sc, 1);
   3888 			break;
   3889 		case 0xfb:
   3890 			DELAY(50);
   3891 			break;
   3892 		case 0xfa:
   3893 			DELAY(5);
   3894 			break;
   3895 		case 0xf9:
   3896 			DELAY(1);
   3897 			break;
   3898 		}
   3899 		urtwn_bb_write(sc, prog->regs[i], prog->vals[i]);
   3900 		DELAY(1);
   3901 	}
   3902 
   3903 	if (sc->chip & URTWN_CHIP_92C_1T2R) {
   3904 		/* 8192C 1T only configuration. */
   3905 		reg = urtwn_bb_read(sc, R92C_FPGA0_TXINFO);
   3906 		reg = (reg & ~0x00000003) | 0x2;
   3907 		urtwn_bb_write(sc, R92C_FPGA0_TXINFO, reg);
   3908 
   3909 		reg = urtwn_bb_read(sc, R92C_FPGA1_TXINFO);
   3910 		reg = (reg & ~0x00300033) | 0x00200022;
   3911 		urtwn_bb_write(sc, R92C_FPGA1_TXINFO, reg);
   3912 
   3913 		reg = urtwn_bb_read(sc, R92C_CCK0_AFESETTING);
   3914 		reg = (reg & ~0xff000000) | (0x45 << 24);
   3915 		urtwn_bb_write(sc, R92C_CCK0_AFESETTING, reg);
   3916 
   3917 		reg = urtwn_bb_read(sc, R92C_OFDM0_TRXPATHENA);
   3918 		reg = (reg & ~0x000000ff) | 0x23;
   3919 		urtwn_bb_write(sc, R92C_OFDM0_TRXPATHENA, reg);
   3920 
   3921 		reg = urtwn_bb_read(sc, R92C_OFDM0_AGCPARAM1);
   3922 		reg = (reg & ~0x00000030) | (1 << 4);
   3923 		urtwn_bb_write(sc, R92C_OFDM0_AGCPARAM1, reg);
   3924 
   3925 		reg = urtwn_bb_read(sc, 0xe74);
   3926 		reg = (reg & ~0x0c000000) | (2 << 26);
   3927 		urtwn_bb_write(sc, 0xe74, reg);
   3928 		reg = urtwn_bb_read(sc, 0xe78);
   3929 		reg = (reg & ~0x0c000000) | (2 << 26);
   3930 		urtwn_bb_write(sc, 0xe78, reg);
   3931 		reg = urtwn_bb_read(sc, 0xe7c);
   3932 		reg = (reg & ~0x0c000000) | (2 << 26);
   3933 		urtwn_bb_write(sc, 0xe7c, reg);
   3934 		reg = urtwn_bb_read(sc, 0xe80);
   3935 		reg = (reg & ~0x0c000000) | (2 << 26);
   3936 		urtwn_bb_write(sc, 0xe80, reg);
   3937 		reg = urtwn_bb_read(sc, 0xe88);
   3938 		reg = (reg & ~0x0c000000) | (2 << 26);
   3939 		urtwn_bb_write(sc, 0xe88, reg);
   3940 	}
   3941 
   3942 	/* Write AGC values. */
   3943 	for (i = 0; i < prog->agccount; i++) {
   3944 		urtwn_bb_write(sc, R92C_OFDM0_AGCRSSITABLE, prog->agcvals[i]);
   3945 		DELAY(1);
   3946 	}
   3947 
   3948 	if (ISSET(sc->chip, URTWN_CHIP_88E) ||
   3949 	    ISSET(sc->chip, URTWN_CHIP_92EU)) {
   3950 		urtwn_bb_write(sc, R92C_OFDM0_AGCCORE1(0), 0x69553422);
   3951 		DELAY(1);
   3952 		urtwn_bb_write(sc, R92C_OFDM0_AGCCORE1(0), 0x69553420);
   3953 		DELAY(1);
   3954 	}
   3955 
   3956 	if (ISSET(sc->chip, URTWN_CHIP_92EU)) {
   3957 		crystalcap = sc->r88e_rom[0xb9];
   3958 		if (crystalcap == 0x00)
   3959 			crystalcap = 0x20;
   3960 		crystalcap &= 0x3f;
   3961 		reg = urtwn_bb_read(sc, R92C_AFE_CTRL3);
   3962 		urtwn_bb_write(sc, R92C_AFE_CTRL3,
   3963 		    RW(reg, R92C_AFE_XTAL_CTRL_ADDR,
   3964 		    crystalcap | crystalcap << 6));
   3965 		urtwn_write_4(sc, R92C_AFE_XTAL_CTRL, 0xf81fb);
   3966 	} else if (ISSET(sc->chip, URTWN_CHIP_88E)) {
   3967 		crystalcap = sc->r88e_rom[0xb9];
   3968 		if (crystalcap == 0xff)
   3969 			crystalcap = 0x20;
   3970 		crystalcap &= 0x3f;
   3971 		reg = urtwn_bb_read(sc, R92C_AFE_XTAL_CTRL);
   3972 		urtwn_bb_write(sc, R92C_AFE_XTAL_CTRL,
   3973 		    RW(reg, R92C_AFE_XTAL_CTRL_ADDR,
   3974 		    crystalcap | crystalcap << 6));
   3975 	} else {
   3976 		if (urtwn_bb_read(sc, R92C_HSSI_PARAM2(0)) &
   3977 		    R92C_HSSI_PARAM2_CCK_HIPWR) {
   3978 			SET(sc->sc_flags, URTWN_FLAG_CCK_HIPWR);
   3979 		}
   3980 	}
   3981 }
   3982 
   3983 static void __noinline
   3984 urtwn_rf_init(struct urtwn_softc *sc)
   3985 {
   3986 	const struct rtwn_rf_prog *prog;
   3987 	uint32_t reg, mask, saved;
   3988 	size_t i, j, idx;
   3989 
   3990 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   3991 
   3992 	/* Select RF programming based on board type. */
   3993 	if (ISSET(sc->chip, URTWN_CHIP_88E))
   3994 		prog = rtl8188eu_rf_prog;
   3995 	else if (ISSET(sc->chip, URTWN_CHIP_92EU))
   3996 		prog = rtl8192eu_rf_prog;
   3997 	else if (!(sc->chip & URTWN_CHIP_92C)) {
   3998 		if (sc->board_type == R92C_BOARD_TYPE_MINICARD) {
   3999 			prog = rtl8188ce_rf_prog;
   4000 		} else if (sc->board_type == R92C_BOARD_TYPE_HIGHPA) {
   4001 			prog = rtl8188ru_rf_prog;
   4002 		} else {
   4003 			prog = rtl8188cu_rf_prog;
   4004 		}
   4005 	} else {
   4006 		prog = rtl8192ce_rf_prog;
   4007 	}
   4008 
   4009 	for (i = 0; i < sc->nrxchains; i++) {
   4010 		/* Save RF_ENV control type. */
   4011 		idx = i / 2;
   4012 		mask = 0xffffU << ((i % 2) * 16);
   4013 		saved = urtwn_bb_read(sc, R92C_FPGA0_RFIFACESW(idx)) & mask;
   4014 
   4015 		/* Set RF_ENV enable. */
   4016 		reg = urtwn_bb_read(sc, R92C_FPGA0_RFIFACEOE(i));
   4017 		reg |= 0x100000;
   4018 		urtwn_bb_write(sc, R92C_FPGA0_RFIFACEOE(i), reg);
   4019 		DELAY(50);
   4020 
   4021 		/* Set RF_ENV output high. */
   4022 		reg = urtwn_bb_read(sc, R92C_FPGA0_RFIFACEOE(i));
   4023 		reg |= 0x10;
   4024 		urtwn_bb_write(sc, R92C_FPGA0_RFIFACEOE(i), reg);
   4025 		DELAY(50);
   4026 
   4027 		/* Set address and data lengths of RF registers. */
   4028 		reg = urtwn_bb_read(sc, R92C_HSSI_PARAM2(i));
   4029 		reg &= ~R92C_HSSI_PARAM2_ADDR_LENGTH;
   4030 		urtwn_bb_write(sc, R92C_HSSI_PARAM2(i), reg);
   4031 		DELAY(50);
   4032 		reg = urtwn_bb_read(sc, R92C_HSSI_PARAM2(i));
   4033 		reg &= ~R92C_HSSI_PARAM2_DATA_LENGTH;
   4034 		urtwn_bb_write(sc, R92C_HSSI_PARAM2(i), reg);
   4035 		DELAY(50);
   4036 
   4037 		/* Write RF initialization values for this chain. */
   4038 		for (j = 0; j < prog[i].count; j++) {
   4039 			if (prog[i].regs[j] >= 0xf9 &&
   4040 			    prog[i].regs[j] <= 0xfe) {
   4041 				/*
   4042 				 * These are fake RF registers offsets that
   4043 				 * indicate a delay is required.
   4044 				 */
   4045 				urtwn_delay_ms(sc, 50);
   4046 				continue;
   4047 			}
   4048 			urtwn_rf_write(sc, i, prog[i].regs[j], prog[i].vals[j]);
   4049 			DELAY(5);
   4050 		}
   4051 
   4052 		/* Restore RF_ENV control type. */
   4053 		reg = urtwn_bb_read(sc, R92C_FPGA0_RFIFACESW(idx)) & ~mask;
   4054 		urtwn_bb_write(sc, R92C_FPGA0_RFIFACESW(idx), reg | saved);
   4055 	}
   4056 
   4057 	if ((sc->chip & (URTWN_CHIP_UMC_A_CUT | URTWN_CHIP_92C)) ==
   4058 	    URTWN_CHIP_UMC_A_CUT) {
   4059 		urtwn_rf_write(sc, 0, R92C_RF_RX_G1, 0x30255);
   4060 		urtwn_rf_write(sc, 0, R92C_RF_RX_G2, 0x50a00);
   4061 	}
   4062 
   4063 	/* Cache RF register CHNLBW. */
   4064 	for (i = 0; i < 2; i++) {
   4065 		sc->rf_chnlbw[i] = urtwn_rf_read(sc, i, R92C_RF_CHNLBW);
   4066 	}
   4067 }
   4068 
   4069 static void __noinline
   4070 urtwn_cam_init(struct urtwn_softc *sc)
   4071 {
   4072 	uint32_t content, command;
   4073 	uint8_t idx;
   4074 	size_t i;
   4075 
   4076 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   4077 
   4078 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   4079 	if (ISSET(sc->chip, URTWN_CHIP_92EU))
   4080 		return;
   4081 
   4082 	for (idx = 0; idx < R92C_CAM_ENTRY_COUNT; idx++) {
   4083 		content = (idx & 3)
   4084 		    | (R92C_CAM_ALGO_AES << R92C_CAM_ALGO_S)
   4085 		    | R92C_CAM_VALID;
   4086 
   4087 		command = R92C_CAMCMD_POLLING
   4088 		    | R92C_CAMCMD_WRITE
   4089 		    | R92C_CAM_CTL0(idx);
   4090 
   4091 		urtwn_write_4(sc, R92C_CAMWRITE, content);
   4092 		urtwn_write_4(sc, R92C_CAMCMD, command);
   4093 	}
   4094 
   4095 	for (idx = 0; idx < R92C_CAM_ENTRY_COUNT; idx++) {
   4096 		for (i = 0; i < /* CAM_CONTENT_COUNT */ 8; i++) {
   4097 			if (i == 0) {
   4098 				content = (idx & 3)
   4099 				    | (R92C_CAM_ALGO_AES << R92C_CAM_ALGO_S)
   4100 				    | R92C_CAM_VALID;
   4101 			} else {
   4102 				content = 0;
   4103 			}
   4104 
   4105 			command = R92C_CAMCMD_POLLING
   4106 			    | R92C_CAMCMD_WRITE
   4107 			    | R92C_CAM_CTL0(idx)
   4108 			    | i;
   4109 
   4110 			urtwn_write_4(sc, R92C_CAMWRITE, content);
   4111 			urtwn_write_4(sc, R92C_CAMCMD, command);
   4112 		}
   4113 	}
   4114 
   4115 	/* Invalidate all CAM entries. */
   4116 	urtwn_write_4(sc, R92C_CAMCMD, R92C_CAMCMD_POLLING | R92C_CAMCMD_CLR);
   4117 }
   4118 
   4119 static void __noinline
   4120 urtwn_pa_bias_init(struct urtwn_softc *sc)
   4121 {
   4122 	uint8_t reg;
   4123 	size_t i;
   4124 
   4125 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   4126 
   4127 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   4128 
   4129 	for (i = 0; i < sc->nrxchains; i++) {
   4130 		if (sc->pa_setting & (1U << i))
   4131 			continue;
   4132 
   4133 		urtwn_rf_write(sc, i, R92C_RF_IPA, 0x0f406);
   4134 		urtwn_rf_write(sc, i, R92C_RF_IPA, 0x4f406);
   4135 		urtwn_rf_write(sc, i, R92C_RF_IPA, 0x8f406);
   4136 		urtwn_rf_write(sc, i, R92C_RF_IPA, 0xcf406);
   4137 	}
   4138 	if (!(sc->pa_setting & 0x10)) {
   4139 		reg = urtwn_read_1(sc, 0x16);
   4140 		reg = (reg & ~0xf0) | 0x90;
   4141 		urtwn_write_1(sc, 0x16, reg);
   4142 	}
   4143 }
   4144 
   4145 static void __noinline
   4146 urtwn_rxfilter_init(struct urtwn_softc *sc)
   4147 {
   4148 
   4149 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   4150 
   4151 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   4152 
   4153 	/* Initialize Rx filter. */
   4154 	/* TODO: use better filter for monitor mode. */
   4155 	urtwn_write_4(sc, R92C_RCR,
   4156 	    R92C_RCR_AAP | R92C_RCR_APM | R92C_RCR_AM | R92C_RCR_AB |
   4157 	    R92C_RCR_APP_ICV | R92C_RCR_AMF | R92C_RCR_HTC_LOC_CTRL |
   4158 	    R92C_RCR_APP_MIC | R92C_RCR_APP_PHYSTS);
   4159 	/* Accept all multicast frames. */
   4160 	urtwn_write_4(sc, R92C_MAR + 0, 0xffffffff);
   4161 	urtwn_write_4(sc, R92C_MAR + 4, 0xffffffff);
   4162 	/* Accept all management frames. */
   4163 	urtwn_write_2(sc, R92C_RXFLTMAP0, 0xffff);
   4164 	/* Reject all control frames. */
   4165 	urtwn_write_2(sc, R92C_RXFLTMAP1, 0x0000);
   4166 	/* Accept all data frames. */
   4167 	urtwn_write_2(sc, R92C_RXFLTMAP2, 0xffff);
   4168 }
   4169 
   4170 static void __noinline
   4171 urtwn_edca_init(struct urtwn_softc *sc)
   4172 {
   4173 
   4174 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   4175 
   4176 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   4177 
   4178 	/* set spec SIFS (used in NAV) */
   4179 	urtwn_write_2(sc, R92C_SPEC_SIFS, 0x100a);
   4180 	urtwn_write_2(sc, R92C_MAC_SPEC_SIFS, 0x100a);
   4181 
   4182 	/* set SIFS CCK/OFDM */
   4183 	urtwn_write_2(sc, R92C_SIFS_CCK, 0x100a);
   4184 	urtwn_write_2(sc, R92C_SIFS_OFDM, 0x100a);
   4185 
   4186 	/* TXOP */
   4187 	urtwn_write_4(sc, R92C_EDCA_BE_PARAM, 0x005ea42b);
   4188 	urtwn_write_4(sc, R92C_EDCA_BK_PARAM, 0x0000a44f);
   4189 	urtwn_write_4(sc, R92C_EDCA_VI_PARAM, 0x005ea324);
   4190 	urtwn_write_4(sc, R92C_EDCA_VO_PARAM, 0x002fa226);
   4191 }
   4192 
   4193 static void
   4194 urtwn_write_txpower(struct urtwn_softc *sc, int chain,
   4195     uint16_t power[URTWN_RIDX_COUNT])
   4196 {
   4197 	uint32_t reg;
   4198 
   4199 	URTWNHIST_FUNC();
   4200 	URTWNHIST_CALLARGS("chain=%jd", chain, 0, 0, 0);
   4201 
   4202 	/* Write per-CCK rate Tx power. */
   4203 	if (chain == 0) {
   4204 		reg = urtwn_bb_read(sc, R92C_TXAGC_A_CCK1_MCS32);
   4205 		reg = RW(reg, R92C_TXAGC_A_CCK1,  power[0]);
   4206 		urtwn_bb_write(sc, R92C_TXAGC_A_CCK1_MCS32, reg);
   4207 
   4208 		reg = urtwn_bb_read(sc, R92C_TXAGC_B_CCK11_A_CCK2_11);
   4209 		reg = RW(reg, R92C_TXAGC_A_CCK2,  power[1]);
   4210 		reg = RW(reg, R92C_TXAGC_A_CCK55, power[2]);
   4211 		reg = RW(reg, R92C_TXAGC_A_CCK11, power[3]);
   4212 		urtwn_bb_write(sc, R92C_TXAGC_B_CCK11_A_CCK2_11, reg);
   4213 	} else {
   4214 		reg = urtwn_bb_read(sc, R92C_TXAGC_B_CCK1_55_MCS32);
   4215 		reg = RW(reg, R92C_TXAGC_B_CCK1,  power[0]);
   4216 		reg = RW(reg, R92C_TXAGC_B_CCK2,  power[1]);
   4217 		reg = RW(reg, R92C_TXAGC_B_CCK55, power[2]);
   4218 		urtwn_bb_write(sc, R92C_TXAGC_B_CCK1_55_MCS32, reg);
   4219 
   4220 		reg = urtwn_bb_read(sc, R92C_TXAGC_B_CCK11_A_CCK2_11);
   4221 		reg = RW(reg, R92C_TXAGC_B_CCK11, power[3]);
   4222 		urtwn_bb_write(sc, R92C_TXAGC_B_CCK11_A_CCK2_11, reg);
   4223 	}
   4224 	/* Write per-OFDM rate Tx power. */
   4225 	urtwn_bb_write(sc, R92C_TXAGC_RATE18_06(chain),
   4226 	    SM(R92C_TXAGC_RATE06, power[ 4]) |
   4227 	    SM(R92C_TXAGC_RATE09, power[ 5]) |
   4228 	    SM(R92C_TXAGC_RATE12, power[ 6]) |
   4229 	    SM(R92C_TXAGC_RATE18, power[ 7]));
   4230 	urtwn_bb_write(sc, R92C_TXAGC_RATE54_24(chain),
   4231 	    SM(R92C_TXAGC_RATE24, power[ 8]) |
   4232 	    SM(R92C_TXAGC_RATE36, power[ 9]) |
   4233 	    SM(R92C_TXAGC_RATE48, power[10]) |
   4234 	    SM(R92C_TXAGC_RATE54, power[11]));
   4235 	/* Write per-MCS Tx power. */
   4236 	urtwn_bb_write(sc, R92C_TXAGC_MCS03_MCS00(chain),
   4237 	    SM(R92C_TXAGC_MCS00,  power[12]) |
   4238 	    SM(R92C_TXAGC_MCS01,  power[13]) |
   4239 	    SM(R92C_TXAGC_MCS02,  power[14]) |
   4240 	    SM(R92C_TXAGC_MCS03,  power[15]));
   4241 	urtwn_bb_write(sc, R92C_TXAGC_MCS07_MCS04(chain),
   4242 	    SM(R92C_TXAGC_MCS04,  power[16]) |
   4243 	    SM(R92C_TXAGC_MCS05,  power[17]) |
   4244 	    SM(R92C_TXAGC_MCS06,  power[18]) |
   4245 	    SM(R92C_TXAGC_MCS07,  power[19]));
   4246 	urtwn_bb_write(sc, R92C_TXAGC_MCS11_MCS08(chain),
   4247 	    SM(R92C_TXAGC_MCS08,  power[20]) |
   4248 	    SM(R92C_TXAGC_MCS09,  power[21]) |
   4249 	    SM(R92C_TXAGC_MCS10,  power[22]) |
   4250 	    SM(R92C_TXAGC_MCS11,  power[23]));
   4251 	urtwn_bb_write(sc, R92C_TXAGC_MCS15_MCS12(chain),
   4252 	    SM(R92C_TXAGC_MCS12,  power[24]) |
   4253 	    SM(R92C_TXAGC_MCS13,  power[25]) |
   4254 	    SM(R92C_TXAGC_MCS14,  power[26]) |
   4255 	    SM(R92C_TXAGC_MCS15,  power[27]));
   4256 }
   4257 
   4258 static void
   4259 urtwn_get_txpower(struct urtwn_softc *sc, size_t chain, u_int chan, u_int ht40m,
   4260     uint16_t power[URTWN_RIDX_COUNT])
   4261 {
   4262 	struct r92c_rom *rom = &sc->rom;
   4263 	uint16_t cckpow, ofdmpow, htpow, diff, maxpow;
   4264 	const struct rtwn_txpwr *base;
   4265 	int ridx, group;
   4266 
   4267 	URTWNHIST_FUNC();
   4268 	URTWNHIST_CALLARGS("chain=%jd, chan=%jd", chain, chan, 0, 0);
   4269 
   4270 	/* Determine channel group. */
   4271 	if (chan <= 3) {
   4272 		group = 0;
   4273 	} else if (chan <= 9) {
   4274 		group = 1;
   4275 	} else {
   4276 		group = 2;
   4277 	}
   4278 
   4279 	/* Get original Tx power based on board type and RF chain. */
   4280 	if (!(sc->chip & URTWN_CHIP_92C)) {
   4281 		if (sc->board_type == R92C_BOARD_TYPE_HIGHPA) {
   4282 			base = &rtl8188ru_txagc[chain];
   4283 		} else {
   4284 			base = &rtl8192cu_txagc[chain];
   4285 		}
   4286 	} else {
   4287 		base = &rtl8192cu_txagc[chain];
   4288 	}
   4289 
   4290 	memset(power, 0, URTWN_RIDX_COUNT * sizeof(power[0]));
   4291 	if (sc->regulatory == 0) {
   4292 		for (ridx = 0; ridx <= 3; ridx++) {
   4293 			power[ridx] = base->pwr[0][ridx];
   4294 		}
   4295 	}
   4296 	for (ridx = 4; ridx < URTWN_RIDX_COUNT; ridx++) {
   4297 		if (sc->regulatory == 3) {
   4298 			power[ridx] = base->pwr[0][ridx];
   4299 			/* Apply vendor limits. */
   4300 			if (ht40m != IEEE80211_HTINFO_2NDCHAN_NONE) {
   4301 				maxpow = rom->ht40_max_pwr[group];
   4302 			} else {
   4303 				maxpow = rom->ht20_max_pwr[group];
   4304 			}
   4305 			maxpow = (maxpow >> (chain * 4)) & 0xf;
   4306 			if (power[ridx] > maxpow) {
   4307 				power[ridx] = maxpow;
   4308 			}
   4309 		} else if (sc->regulatory == 1) {
   4310 			if (ht40m == IEEE80211_HTINFO_2NDCHAN_NONE) {
   4311 				power[ridx] = base->pwr[group][ridx];
   4312 			}
   4313 		} else if (sc->regulatory != 2) {
   4314 			power[ridx] = base->pwr[0][ridx];
   4315 		}
   4316 	}
   4317 
   4318 	/* Compute per-CCK rate Tx power. */
   4319 	cckpow = rom->cck_tx_pwr[chain][group];
   4320 	for (ridx = 0; ridx <= 3; ridx++) {
   4321 		power[ridx] += cckpow;
   4322 		if (power[ridx] > R92C_MAX_TX_PWR) {
   4323 			power[ridx] = R92C_MAX_TX_PWR;
   4324 		}
   4325 	}
   4326 
   4327 	htpow = rom->ht40_1s_tx_pwr[chain][group];
   4328 	if (sc->ntxchains > 1) {
   4329 		/* Apply reduction for 2 spatial streams. */
   4330 		diff = rom->ht40_2s_tx_pwr_diff[group];
   4331 		diff = (diff >> (chain * 4)) & 0xf;
   4332 		htpow = (htpow > diff) ? htpow - diff : 0;
   4333 	}
   4334 
   4335 	/* Compute per-OFDM rate Tx power. */
   4336 	diff = rom->ofdm_tx_pwr_diff[group];
   4337 	diff = (diff >> (chain * 4)) & 0xf;
   4338 	ofdmpow = htpow + diff;	/* HT->OFDM correction. */
   4339 	for (ridx = 4; ridx <= 11; ridx++) {
   4340 		power[ridx] += ofdmpow;
   4341 		if (power[ridx] > R92C_MAX_TX_PWR) {
   4342 			power[ridx] = R92C_MAX_TX_PWR;
   4343 		}
   4344 	}
   4345 
   4346 	/* Compute per-MCS Tx power. */
   4347 	if (ht40m == IEEE80211_HTINFO_2NDCHAN_NONE) {
   4348 		diff = rom->ht20_tx_pwr_diff[group];
   4349 		diff = (diff >> (chain * 4)) & 0xf;
   4350 		htpow += diff;	/* HT40->HT20 correction. */
   4351 	}
   4352 	for (ridx = 12; ridx < URTWN_RIDX_COUNT; ridx++) {
   4353 		power[ridx] += htpow;
   4354 		if (power[ridx] > R92C_MAX_TX_PWR) {
   4355 			power[ridx] = R92C_MAX_TX_PWR;
   4356 		}
   4357 	}
   4358 #ifdef URTWN_DEBUG
   4359 	if (urtwn_debug & DBG_RF) {
   4360 		/* Dump per-rate Tx power values. */
   4361 		DPRINTFN(DBG_RF, "Tx power for chain %jd:", chain, 0, 0, 0);
   4362 		for (ridx = 0; ridx < URTWN_RIDX_COUNT; ridx++)
   4363 			DPRINTFN(DBG_RF, "Rate %jd = %ju", ridx, power[ridx], 0, 0);
   4364 	}
   4365 #endif
   4366 }
   4367 
   4368 void
   4369 urtwn_r88e_get_txpower(struct urtwn_softc *sc, size_t chain, u_int chan,
   4370     u_int ht40m, uint16_t power[URTWN_RIDX_COUNT])
   4371 {
   4372 	uint16_t cckpow, ofdmpow, bw20pow, htpow;
   4373 	const struct rtwn_r88e_txpwr *base;
   4374 	int ridx, group;
   4375 
   4376 	URTWNHIST_FUNC();
   4377 	URTWNHIST_CALLARGS("chain=%jd, chan=%jd", chain, chan, 0, 0);
   4378 
   4379 	/* Determine channel group. */
   4380 	if (chan <= 2)
   4381 		group = 0;
   4382 	else if (chan <= 5)
   4383 		group = 1;
   4384 	else if (chan <= 8)
   4385 		group = 2;
   4386 	else if (chan <= 11)
   4387 		group = 3;
   4388 	else if (chan <= 13)
   4389 		group = 4;
   4390 	else
   4391 		group = 5;
   4392 
   4393 	/* Get original Tx power based on board type and RF chain. */
   4394 	base = &rtl8188eu_txagc[chain];
   4395 
   4396 	memset(power, 0, URTWN_RIDX_COUNT * sizeof(power[0]));
   4397 	if (sc->regulatory == 0) {
   4398 		for (ridx = 0; ridx <= 3; ridx++)
   4399 			power[ridx] = base->pwr[0][ridx];
   4400 	}
   4401 	for (ridx = 4; ridx < URTWN_RIDX_COUNT; ridx++) {
   4402 		if (sc->regulatory == 3)
   4403 			power[ridx] = base->pwr[0][ridx];
   4404 		else if (sc->regulatory == 1) {
   4405 			if (ht40m == IEEE80211_HTINFO_2NDCHAN_NONE)
   4406 				power[ridx] = base->pwr[group][ridx];
   4407 		} else if (sc->regulatory != 2)
   4408 			power[ridx] = base->pwr[0][ridx];
   4409 	}
   4410 
   4411 	/* Compute per-CCK rate Tx power. */
   4412 	cckpow = sc->cck_tx_pwr[group];
   4413 	for (ridx = 0; ridx <= 3; ridx++) {
   4414 		power[ridx] += cckpow;
   4415 		if (power[ridx] > R92C_MAX_TX_PWR)
   4416 			power[ridx] = R92C_MAX_TX_PWR;
   4417 	}
   4418 
   4419 	htpow = sc->ht40_tx_pwr[group];
   4420 
   4421 	/* Compute per-OFDM rate Tx power. */
   4422 	ofdmpow = htpow + sc->ofdm_tx_pwr_diff;
   4423 	for (ridx = 4; ridx <= 11; ridx++) {
   4424 		power[ridx] += ofdmpow;
   4425 		if (power[ridx] > R92C_MAX_TX_PWR)
   4426 			power[ridx] = R92C_MAX_TX_PWR;
   4427 	}
   4428 
   4429 	bw20pow = htpow + sc->bw20_tx_pwr_diff;
   4430 	for (ridx = 12; ridx <= 27; ridx++) {
   4431 		power[ridx] += bw20pow;
   4432 		if (power[ridx] > R92C_MAX_TX_PWR)
   4433 			power[ridx] = R92C_MAX_TX_PWR;
   4434 	}
   4435 }
   4436 
   4437 static void
   4438 urtwn_set_txpower(struct urtwn_softc *sc, u_int chan, u_int ht40m)
   4439 {
   4440 	uint16_t power[URTWN_RIDX_COUNT];
   4441 	size_t i;
   4442 
   4443 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   4444 
   4445 	for (i = 0; i < sc->ntxchains; i++) {
   4446 		/* Compute per-rate Tx power values. */
   4447 		if (ISSET(sc->chip, URTWN_CHIP_88E) ||
   4448 		    ISSET(sc->chip, URTWN_CHIP_92EU))
   4449 			urtwn_r88e_get_txpower(sc, i, chan, ht40m, power);
   4450 		else
   4451 			urtwn_get_txpower(sc, i, chan, ht40m, power);
   4452 		/* Write per-rate Tx power values to hardware. */
   4453 		urtwn_write_txpower(sc, i, power);
   4454 	}
   4455 }
   4456 
   4457 static void __noinline
   4458 urtwn_set_chan(struct urtwn_softc *sc, struct ieee80211_channel *c, u_int ht40m)
   4459 {
   4460 	struct ieee80211com *ic = &sc->sc_ic;
   4461 	u_int chan;
   4462 	size_t i;
   4463 
   4464 	chan = ieee80211_chan2ieee(ic, c);	/* XXX center freq! */
   4465 
   4466 	URTWNHIST_FUNC();
   4467 	URTWNHIST_CALLARGS("chan=%jd", chan, 0, 0, 0);
   4468 
   4469 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   4470 
   4471 	if (ht40m == IEEE80211_HTINFO_2NDCHAN_ABOVE) {
   4472 		chan += 2;
   4473 	} else if (ht40m == IEEE80211_HTINFO_2NDCHAN_BELOW){
   4474 		chan -= 2;
   4475 	}
   4476 
   4477 	/* Set Tx power for this new channel. */
   4478 	urtwn_set_txpower(sc, chan, ht40m);
   4479 
   4480 	for (i = 0; i < sc->nrxchains; i++) {
   4481 		urtwn_rf_write(sc, i, R92C_RF_CHNLBW,
   4482 		    RW(sc->rf_chnlbw[i], R92C_RF_CHNLBW_CHNL, chan));
   4483 	}
   4484 
   4485 	if (ht40m) {
   4486 		/* Is secondary channel below or above primary? */
   4487 		int prichlo = (ht40m == IEEE80211_HTINFO_2NDCHAN_ABOVE);
   4488 		uint32_t reg;
   4489 
   4490 		urtwn_write_1(sc, R92C_BWOPMODE,
   4491 		    urtwn_read_1(sc, R92C_BWOPMODE) & ~R92C_BWOPMODE_20MHZ);
   4492 
   4493 		reg = urtwn_read_1(sc, R92C_RRSR + 2);
   4494 		reg = (reg & ~0x6f) | (prichlo ? 1 : 2) << 5;
   4495 		urtwn_write_1(sc, R92C_RRSR + 2, (uint8_t)reg);
   4496 
   4497 		urtwn_bb_write(sc, R92C_FPGA0_RFMOD,
   4498 		    urtwn_bb_read(sc, R92C_FPGA0_RFMOD) | R92C_RFMOD_40MHZ);
   4499 		urtwn_bb_write(sc, R92C_FPGA1_RFMOD,
   4500 		    urtwn_bb_read(sc, R92C_FPGA1_RFMOD) | R92C_RFMOD_40MHZ);
   4501 
   4502 		/* Set CCK side band. */
   4503 		reg = urtwn_bb_read(sc, R92C_CCK0_SYSTEM);
   4504 		reg = (reg & ~0x00000010) | (prichlo ? 0 : 1) << 4;
   4505 		urtwn_bb_write(sc, R92C_CCK0_SYSTEM, reg);
   4506 
   4507 		reg = urtwn_bb_read(sc, R92C_OFDM1_LSTF);
   4508 		reg = (reg & ~0x00000c00) | (prichlo ? 1 : 2) << 10;
   4509 		urtwn_bb_write(sc, R92C_OFDM1_LSTF, reg);
   4510 
   4511 		urtwn_bb_write(sc, R92C_FPGA0_ANAPARAM2,
   4512 		    urtwn_bb_read(sc, R92C_FPGA0_ANAPARAM2) &
   4513 		    ~R92C_FPGA0_ANAPARAM2_CBW20);
   4514 
   4515 		reg = urtwn_bb_read(sc, 0x818);
   4516 		reg = (reg & ~0x0c000000) | (prichlo ? 2 : 1) << 26;
   4517 		urtwn_bb_write(sc, 0x818, reg);
   4518 
   4519 		/* Select 40MHz bandwidth. */
   4520 		urtwn_rf_write(sc, 0, R92C_RF_CHNLBW,
   4521 		    (sc->rf_chnlbw[0] & ~0xfff) | chan);
   4522 	} else {
   4523 		urtwn_write_1(sc, R92C_BWOPMODE,
   4524 		    urtwn_read_1(sc, R92C_BWOPMODE) | R92C_BWOPMODE_20MHZ);
   4525 
   4526 		urtwn_bb_write(sc, R92C_FPGA0_RFMOD,
   4527 		    urtwn_bb_read(sc, R92C_FPGA0_RFMOD) & ~R92C_RFMOD_40MHZ);
   4528 		urtwn_bb_write(sc, R92C_FPGA1_RFMOD,
   4529 		    urtwn_bb_read(sc, R92C_FPGA1_RFMOD) & ~R92C_RFMOD_40MHZ);
   4530 
   4531 		if (!ISSET(sc->chip, URTWN_CHIP_88E) &&
   4532 		    !ISSET(sc->chip, URTWN_CHIP_92EU)) {
   4533 			urtwn_bb_write(sc, R92C_FPGA0_ANAPARAM2,
   4534 			    urtwn_bb_read(sc, R92C_FPGA0_ANAPARAM2) |
   4535 			    R92C_FPGA0_ANAPARAM2_CBW20);
   4536 		}
   4537 
   4538 		/* Select 20MHz bandwidth. */
   4539 		urtwn_rf_write(sc, 0, R92C_RF_CHNLBW,
   4540 		    (sc->rf_chnlbw[0] & ~0xfff) | chan |
   4541 		    (ISSET(sc->chip, URTWN_CHIP_88E) ||
   4542 		     ISSET(sc->chip, URTWN_CHIP_92EU) ?
   4543 		      R88E_RF_CHNLBW_BW20 : R92C_RF_CHNLBW_BW20));
   4544 	}
   4545 }
   4546 
   4547 static void __noinline
   4548 urtwn_iq_calib(struct urtwn_softc *sc, bool inited)
   4549 {
   4550 
   4551 	URTWNHIST_FUNC();
   4552 	URTWNHIST_CALLARGS("inited=%jd", inited, 0, 0, 0);
   4553 
   4554 	uint32_t addaBackup[16], iqkBackup[4], piMode;
   4555 
   4556 #ifdef notyet
   4557 	uint32_t odfm0_agccore_regs[3];
   4558 	uint32_t ant_regs[3];
   4559 	uint32_t rf_regs[8];
   4560 #endif
   4561 	uint32_t reg0, reg1, reg2;
   4562 	int i, attempt;
   4563 
   4564 #ifdef notyet
   4565 	urtwn_write_1(sc, R92E_STBC_SETTING + 2, urtwn_read_1(sc,
   4566 	    R92E_STBC_SETTING + 2));
   4567 	urtwn_write_1(sc, R92C_ACLK_MON, 0);
   4568 	/* Save AGCCORE regs. */
   4569 	for (i = 0; i < sc->nrxchains; i++) {
   4570 		odfm0_agccore_regs[i] = urtwn_read_4(sc,
   4571 		    R92C_OFDM0_AGCCORE1(i));
   4572 	}
   4573 #endif
   4574 	/* Save BB regs. */
   4575 	reg0 = urtwn_bb_read(sc, R92C_OFDM0_TRXPATHENA);
   4576 	reg1 = urtwn_bb_read(sc, R92C_OFDM0_TRMUXPAR);
   4577 	reg2 = urtwn_bb_read(sc, R92C_FPGA0_RFIFACESW(1));
   4578 
   4579 	/* Save adda regs to be restored when finished. */
   4580 	for (i = 0; i < __arraycount(addaReg); i++)
   4581 		addaBackup[i] = urtwn_bb_read(sc, addaReg[i]);
   4582 	/* Save mac regs. */
   4583 	iqkBackup[0] = urtwn_read_1(sc, R92C_TXPAUSE);
   4584 	iqkBackup[1] = urtwn_read_1(sc, R92C_BCN_CTRL);
   4585 	iqkBackup[2] = urtwn_read_1(sc, R92C_BCN_CTRL1);
   4586 	iqkBackup[3] = urtwn_read_4(sc, R92C_GPIO_MUXCFG);
   4587 
   4588 #ifdef notyet
   4589 	ant_regs[0] = urtwn_read_4(sc, R92C_CONFIG_ANT_A);
   4590 	ant_regs[1] = urtwn_read_4(sc, R92C_CONFIG_ANT_B);
   4591 
   4592 	rf_regs[0] = urtwn_read_4(sc, R92C_FPGA0_RFIFACESW(0));
   4593 	for (i = 0; i < sc->nrxchains; i++)
   4594 		rf_regs[i+1] = urtwn_read_4(sc, R92C_FPGA0_RFIFACEOE(i));
   4595 	reg4 = urtwn_read_4(sc, R92C_CCK0_AFESETTING);
   4596 #endif
   4597 
   4598 	piMode = (urtwn_bb_read(sc, R92C_HSSI_PARAM1(0)) &
   4599 	    R92C_HSSI_PARAM1_PI);
   4600 	if (piMode == 0) {
   4601 		urtwn_bb_write(sc, R92C_HSSI_PARAM1(0),
   4602 		    urtwn_bb_read(sc, R92C_HSSI_PARAM1(0))|
   4603 		    R92C_HSSI_PARAM1_PI);
   4604 		urtwn_bb_write(sc, R92C_HSSI_PARAM1(1),
   4605 		    urtwn_bb_read(sc, R92C_HSSI_PARAM1(1))|
   4606 		    R92C_HSSI_PARAM1_PI);
   4607 	}
   4608 
   4609 	attempt = 1;
   4610 
   4611 next_attempt:
   4612 
   4613 	/* Set mac regs for calibration. */
   4614 	for (i = 0; i < __arraycount(addaReg); i++) {
   4615 		urtwn_bb_write(sc, addaReg[i],
   4616 		    addaReg[__arraycount(addaReg) - 1]);
   4617 	}
   4618 	urtwn_write_2(sc, R92C_CCK0_AFESETTING, urtwn_read_2(sc,
   4619 	    R92C_CCK0_AFESETTING));
   4620 	urtwn_write_2(sc, R92C_OFDM0_TRXPATHENA, R92C_IQK_TRXPATHENA);
   4621 	urtwn_write_2(sc, R92C_OFDM0_TRMUXPAR, R92C_IQK_TRMUXPAR);
   4622 	urtwn_write_2(sc, R92C_FPGA0_RFIFACESW(1), R92C_IQK_RFIFACESW1);
   4623 	urtwn_write_4(sc, R92C_LSSI_PARAM(0), R92C_IQK_LSSI_PARAM);
   4624 
   4625 	if (sc->ntxchains > 1)
   4626 		urtwn_bb_write(sc, R92C_LSSI_PARAM(1), R92C_IQK_LSSI_PARAM);
   4627 
   4628 	urtwn_write_1(sc, R92C_TXPAUSE, (~R92C_TXPAUSE_BCN) & R92C_TXPAUSE_ALL);
   4629 	urtwn_write_1(sc, R92C_BCN_CTRL, (iqkBackup[1] &
   4630 	    ~R92C_BCN_CTRL_EN_BCN));
   4631 	urtwn_write_1(sc, R92C_BCN_CTRL1, (iqkBackup[2] &
   4632 	    ~R92C_BCN_CTRL_EN_BCN));
   4633 
   4634 	urtwn_write_1(sc, R92C_GPIO_MUXCFG, (iqkBackup[3] &
   4635 	    ~R92C_GPIO_MUXCFG_ENBT));
   4636 
   4637 	urtwn_bb_write(sc, R92C_CONFIG_ANT_A, R92C_IQK_CONFIG_ANT);
   4638 
   4639 	if (sc->ntxchains > 1)
   4640 		urtwn_bb_write(sc, R92C_CONFIG_ANT_B, R92C_IQK_CONFIG_ANT);
   4641 	urtwn_bb_write(sc, R92C_FPGA0_IQK, R92C_FPGA0_IQK_SETTING);
   4642 	urtwn_bb_write(sc, R92C_TX_IQK, R92C_TX_IQK_SETTING);
   4643 	urtwn_bb_write(sc, R92C_RX_IQK, R92C_RX_IQK_SETTING);
   4644 
   4645 	/* Restore BB regs. */
   4646 	urtwn_bb_write(sc, R92C_OFDM0_TRXPATHENA, reg0);
   4647 	urtwn_bb_write(sc, R92C_FPGA0_RFIFACESW(1), reg2);
   4648 	urtwn_bb_write(sc, R92C_OFDM0_TRMUXPAR, reg1);
   4649 
   4650 	urtwn_bb_write(sc, R92C_FPGA0_IQK, 0x0);
   4651 	urtwn_bb_write(sc, R92C_LSSI_PARAM(0), R92C_IQK_LSSI_RESTORE);
   4652 	if (sc->nrxchains > 1)
   4653 		urtwn_bb_write(sc, R92C_LSSI_PARAM(1), R92C_IQK_LSSI_RESTORE);
   4654 
   4655 	if (attempt-- > 0)
   4656 		goto next_attempt;
   4657 
   4658 	/* Restore mode. */
   4659 	if (piMode == 0) {
   4660 		urtwn_bb_write(sc, R92C_HSSI_PARAM1(0),
   4661 		    urtwn_bb_read(sc, R92C_HSSI_PARAM1(0)) &
   4662 		    ~R92C_HSSI_PARAM1_PI);
   4663 		urtwn_bb_write(sc, R92C_HSSI_PARAM1(1),
   4664 		    urtwn_bb_read(sc, R92C_HSSI_PARAM1(1)) &
   4665 		    ~R92C_HSSI_PARAM1_PI);
   4666 	}
   4667 
   4668 #ifdef notyet
   4669 	for (i = 0; i < sc->nrxchains; i++) {
   4670 		urtwn_write_4(sc, R92C_OFDM0_AGCCORE1(i),
   4671 		    odfm0_agccore_regs[i]);
   4672 	}
   4673 #endif
   4674 
   4675 	/* Restore adda regs. */
   4676 	for (i = 0; i < __arraycount(addaReg); i++)
   4677 		urtwn_bb_write(sc, addaReg[i], addaBackup[i]);
   4678 	/* Restore mac regs. */
   4679 	urtwn_write_1(sc, R92C_TXPAUSE, iqkBackup[0]);
   4680 	urtwn_write_1(sc, R92C_BCN_CTRL, iqkBackup[1]);
   4681 	urtwn_write_1(sc, R92C_USTIME_TSF, iqkBackup[2]);
   4682 	urtwn_write_4(sc, R92C_GPIO_MUXCFG, iqkBackup[3]);
   4683 
   4684 #ifdef notyet
   4685 	urtwn_write_4(sc, R92C_CONFIG_ANT_A, ant_regs[0]);
   4686 	urtwn_write_4(sc, R92C_CONFIG_ANT_B, ant_regs[1]);
   4687 
   4688 	urtwn_write_4(sc, R92C_FPGA0_RFIFACESW(0), rf_regs[0]);
   4689 	for (i = 0; i < sc->nrxchains; i++)
   4690 		urtwn_write_4(sc, R92C_FPGA0_RFIFACEOE(i), rf_regs[i+1]);
   4691 	urtwn_write_4(sc, R92C_CCK0_AFESETTING, reg4);
   4692 #endif
   4693 }
   4694 
   4695 static void
   4696 urtwn_lc_calib(struct urtwn_softc *sc)
   4697 {
   4698 	uint32_t rf_ac[2];
   4699 	uint8_t txmode;
   4700 	size_t i;
   4701 
   4702 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   4703 
   4704 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   4705 
   4706 	txmode = urtwn_read_1(sc, R92C_OFDM1_LSTF + 3);
   4707 	if ((txmode & 0x70) != 0) {
   4708 		/* Disable all continuous Tx. */
   4709 		urtwn_write_1(sc, R92C_OFDM1_LSTF + 3, txmode & ~0x70);
   4710 
   4711 		/* Set RF mode to standby mode. */
   4712 		for (i = 0; i < sc->nrxchains; i++) {
   4713 			rf_ac[i] = urtwn_rf_read(sc, i, R92C_RF_AC);
   4714 			urtwn_rf_write(sc, i, R92C_RF_AC,
   4715 			    RW(rf_ac[i], R92C_RF_AC_MODE,
   4716 				R92C_RF_AC_MODE_STANDBY));
   4717 		}
   4718 	} else {
   4719 		/* Block all Tx queues. */
   4720 		urtwn_write_1(sc, R92C_TXPAUSE, 0xff);
   4721 	}
   4722 	/* Start calibration. */
   4723 	urtwn_rf_write(sc, 0, R92C_RF_CHNLBW,
   4724 	    urtwn_rf_read(sc, 0, R92C_RF_CHNLBW) | R92C_RF_CHNLBW_LCSTART);
   4725 
   4726 	/* Give calibration the time to complete. */
   4727 	urtwn_delay_ms(sc, 100);
   4728 
   4729 	/* Restore configuration. */
   4730 	if ((txmode & 0x70) != 0) {
   4731 		/* Restore Tx mode. */
   4732 		urtwn_write_1(sc, R92C_OFDM1_LSTF + 3, txmode);
   4733 		/* Restore RF mode. */
   4734 		for (i = 0; i < sc->nrxchains; i++) {
   4735 			urtwn_rf_write(sc, i, R92C_RF_AC, rf_ac[i]);
   4736 		}
   4737 	} else {
   4738 		/* Unblock all Tx queues. */
   4739 		urtwn_write_1(sc, R92C_TXPAUSE, 0x00);
   4740 	}
   4741 }
   4742 
   4743 static void
   4744 urtwn_temp_calib(struct urtwn_softc *sc)
   4745 {
   4746 	int temp, t_meter_reg;
   4747 
   4748 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   4749 
   4750 	KASSERT(mutex_owned(&sc->sc_write_mtx));
   4751 
   4752 	if (!ISSET(sc->chip, URTWN_CHIP_92EU))
   4753 		t_meter_reg = R92C_RF_T_METER;
   4754 	else
   4755 		t_meter_reg = R92E_RF_T_METER;
   4756 
   4757 	if (sc->thcal_state == 0) {
   4758 		/* Start measuring temperature. */
   4759 		DPRINTFN(DBG_RF, "start measuring temperature", 0, 0, 0, 0);
   4760 		urtwn_rf_write(sc, 0, t_meter_reg, 0x60);
   4761 		sc->thcal_state = 1;
   4762 		return;
   4763 	}
   4764 	sc->thcal_state = 0;
   4765 
   4766 	/* Read measured temperature. */
   4767 	temp = urtwn_rf_read(sc, 0, R92C_RF_T_METER) & 0x1f;
   4768 	DPRINTFN(DBG_RF, "temperature=%jd", temp, 0, 0, 0);
   4769 	if (temp == 0)		/* Read failed, skip. */
   4770 		return;
   4771 
   4772 	/*
   4773 	 * Redo LC calibration if temperature changed significantly since
   4774 	 * last calibration.
   4775 	 */
   4776 	if (sc->thcal_lctemp == 0) {
   4777 		/* First LC calibration is performed in urtwn_init(). */
   4778 		sc->thcal_lctemp = temp;
   4779 	} else if (abs(temp - sc->thcal_lctemp) > 1) {
   4780 		DPRINTFN(DBG_RF, "LC calib triggered by temp: %jd -> %jd",
   4781 		    sc->thcal_lctemp, temp, 0, 0);
   4782 		urtwn_lc_calib(sc);
   4783 		/* Record temperature of last LC calibration. */
   4784 		sc->thcal_lctemp = temp;
   4785 	}
   4786 }
   4787 
   4788 static int
   4789 urtwn_init(struct ifnet *ifp)
   4790 {
   4791 	struct urtwn_softc *sc = ifp->if_softc;
   4792 	struct ieee80211com *ic = &sc->sc_ic;
   4793 	struct urtwn_rx_data *data;
   4794 	uint32_t reg;
   4795 	size_t i;
   4796 	int error;
   4797 
   4798 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   4799 
   4800 	urtwn_stop(ifp, 0);
   4801 
   4802 	mutex_enter(&sc->sc_write_mtx);
   4803 
   4804 	mutex_enter(&sc->sc_task_mtx);
   4805 	/* Init host async commands ring. */
   4806 	sc->cmdq.cur = sc->cmdq.next = sc->cmdq.queued = 0;
   4807 	mutex_exit(&sc->sc_task_mtx);
   4808 
   4809 	mutex_enter(&sc->sc_fwcmd_mtx);
   4810 	/* Init firmware commands ring. */
   4811 	sc->fwcur = 0;
   4812 	mutex_exit(&sc->sc_fwcmd_mtx);
   4813 
   4814 	/* Allocate Tx/Rx buffers. */
   4815 	error = urtwn_alloc_rx_list(sc);
   4816 	if (error != 0) {
   4817 		aprint_error_dev(sc->sc_dev,
   4818 		    "could not allocate Rx buffers\n");
   4819 		goto fail;
   4820 	}
   4821 	error = urtwn_alloc_tx_list(sc);
   4822 	if (error != 0) {
   4823 		aprint_error_dev(sc->sc_dev,
   4824 		    "could not allocate Tx buffers\n");
   4825 		goto fail;
   4826 	}
   4827 
   4828 	/* Power on adapter. */
   4829 	error = urtwn_power_on(sc);
   4830 	if (error != 0)
   4831 		goto fail;
   4832 
   4833 	/* Initialize DMA. */
   4834 	error = urtwn_dma_init(sc);
   4835 	if (error != 0)
   4836 		goto fail;
   4837 
   4838 	/* Set info size in Rx descriptors (in 64-bit words). */
   4839 	urtwn_write_1(sc, R92C_RX_DRVINFO_SZ, 4);
   4840 
   4841 	/* Init interrupts. */
   4842 	if (ISSET(sc->chip, URTWN_CHIP_88E) ||
   4843 	    ISSET(sc->chip, URTWN_CHIP_92EU)) {
   4844 		urtwn_write_4(sc, R88E_HISR, 0xffffffff);
   4845 		urtwn_write_4(sc, R88E_HIMR, R88E_HIMR_CPWM | R88E_HIMR_CPWM2 |
   4846 		    R88E_HIMR_TBDER | R88E_HIMR_PSTIMEOUT);
   4847 		urtwn_write_4(sc, R88E_HIMRE, R88E_HIMRE_RXFOVW |
   4848 		    R88E_HIMRE_TXFOVW | R88E_HIMRE_RXERR | R88E_HIMRE_TXERR);
   4849 		if (ISSET(sc->chip, URTWN_CHIP_88E)) {
   4850 			urtwn_write_1(sc, R92C_USB_SPECIAL_OPTION,
   4851 			    urtwn_read_1(sc, R92C_USB_SPECIAL_OPTION) |
   4852 			      R92C_USB_SPECIAL_OPTION_INT_BULK_SEL);
   4853 		}
   4854 		if (ISSET(sc->chip, URTWN_CHIP_92EU))
   4855 			urtwn_write_1(sc, R92C_USB_HRPWM, 0);
   4856 	} else {
   4857 		urtwn_write_4(sc, R92C_HISR, 0xffffffff);
   4858 		urtwn_write_4(sc, R92C_HIMR, 0xffffffff);
   4859 	}
   4860 
   4861 	/* Set MAC address. */
   4862 	IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
   4863 	urtwn_write_region(sc, R92C_MACID, ic->ic_myaddr, IEEE80211_ADDR_LEN);
   4864 
   4865 	/* Set initial network type. */
   4866 	reg = urtwn_read_4(sc, R92C_CR);
   4867 	switch (ic->ic_opmode) {
   4868 	case IEEE80211_M_STA:
   4869 	default:
   4870 		reg = RW(reg, R92C_CR_NETTYPE, R92C_CR_NETTYPE_INFRA);
   4871 		break;
   4872 
   4873 	case IEEE80211_M_IBSS:
   4874 		reg = RW(reg, R92C_CR_NETTYPE, R92C_CR_NETTYPE_ADHOC);
   4875 		break;
   4876 	}
   4877 	urtwn_write_4(sc, R92C_CR, reg);
   4878 
   4879 	/* Set response rate */
   4880 	reg = urtwn_read_4(sc, R92C_RRSR);
   4881 	reg = RW(reg, R92C_RRSR_RATE_BITMAP, R92C_RRSR_RATE_CCK_ONLY_1M);
   4882 	urtwn_write_4(sc, R92C_RRSR, reg);
   4883 
   4884 	/* SIFS (used in NAV) */
   4885 	urtwn_write_2(sc, R92C_SPEC_SIFS,
   4886 	    SM(R92C_SPEC_SIFS_CCK, 0x10) | SM(R92C_SPEC_SIFS_OFDM, 0x10));
   4887 
   4888 	/* Set short/long retry limits. */
   4889 	urtwn_write_2(sc, R92C_RL,
   4890 	    SM(R92C_RL_SRL, 0x30) | SM(R92C_RL_LRL, 0x30));
   4891 
   4892 	/* Initialize EDCA parameters. */
   4893 	urtwn_edca_init(sc);
   4894 
   4895 	/* Setup rate fallback. */
   4896 	if (!ISSET(sc->chip, URTWN_CHIP_88E) &&
   4897 	    !ISSET(sc->chip, URTWN_CHIP_92EU)) {
   4898 		urtwn_write_4(sc, R92C_DARFRC + 0, 0x00000000);
   4899 		urtwn_write_4(sc, R92C_DARFRC + 4, 0x10080404);
   4900 		urtwn_write_4(sc, R92C_RARFRC + 0, 0x04030201);
   4901 		urtwn_write_4(sc, R92C_RARFRC + 4, 0x08070605);
   4902 	}
   4903 
   4904 	urtwn_write_1(sc, R92C_FWHW_TXQ_CTRL,
   4905 	    urtwn_read_1(sc, R92C_FWHW_TXQ_CTRL) |
   4906 	    R92C_FWHW_TXQ_CTRL_AMPDU_RTY_NEW);
   4907 	/* Set ACK timeout. */
   4908 	urtwn_write_1(sc, R92C_ACKTO, 0x40);
   4909 
   4910 	/* Setup USB aggregation. */
   4911 	/* Tx */
   4912 	reg = urtwn_read_4(sc, R92C_TDECTRL);
   4913 	reg = RW(reg, R92C_TDECTRL_BLK_DESC_NUM, 6);
   4914 	urtwn_write_4(sc, R92C_TDECTRL, reg);
   4915 	/* Rx */
   4916 	urtwn_write_1(sc, R92C_TRXDMA_CTRL,
   4917 	    urtwn_read_1(sc, R92C_TRXDMA_CTRL) |
   4918 	      R92C_TRXDMA_CTRL_RXDMA_AGG_EN);
   4919 	urtwn_write_1(sc, R92C_USB_SPECIAL_OPTION,
   4920 	    urtwn_read_1(sc, R92C_USB_SPECIAL_OPTION) &
   4921 	      ~R92C_USB_SPECIAL_OPTION_AGG_EN);
   4922 	urtwn_write_1(sc, R92C_RXDMA_AGG_PG_TH, 48);
   4923 	if (ISSET(sc->chip, URTWN_CHIP_88E) ||
   4924 	    ISSET(sc->chip, URTWN_CHIP_92EU))
   4925 		urtwn_write_1(sc, R92C_RXDMA_AGG_PG_TH + 1, 4);
   4926 	else
   4927 		urtwn_write_1(sc, R92C_USB_DMA_AGG_TO, 4);
   4928 
   4929 	/* Initialize beacon parameters. */
   4930 	urtwn_write_2(sc, R92C_BCN_CTRL, 0x1010);
   4931 	urtwn_write_2(sc, R92C_TBTT_PROHIBIT, 0x6404);
   4932 	urtwn_write_1(sc, R92C_DRVERLYINT, R92C_DRVERLYINT_INIT_TIME);
   4933 	urtwn_write_1(sc, R92C_BCNDMATIM, R92C_BCNDMATIM_INIT_TIME);
   4934 	urtwn_write_2(sc, R92C_BCNTCFG, 0x660f);
   4935 
   4936 	if (!ISSET(sc->chip, URTWN_CHIP_88E) &&
   4937 	    !ISSET(sc->chip, URTWN_CHIP_92EU)) {
   4938 		/* Setup AMPDU aggregation. */
   4939 		urtwn_write_4(sc, R92C_AGGLEN_LMT, 0x99997631);	/* MCS7~0 */
   4940 		urtwn_write_1(sc, R92C_AGGR_BREAK_TIME, 0x16);
   4941 		urtwn_write_2(sc, 0x4ca, 0x0708);
   4942 
   4943 		urtwn_write_1(sc, R92C_BCN_MAX_ERR, 0xff);
   4944 		urtwn_write_1(sc, R92C_BCN_CTRL, R92C_BCN_CTRL_DIS_TSF_UDT0);
   4945 	}
   4946 
   4947 	/* Load 8051 microcode. */
   4948 	error = urtwn_load_firmware(sc);
   4949 	if (error != 0)
   4950 		goto fail;
   4951 	SET(sc->sc_flags, URTWN_FLAG_FWREADY);
   4952 
   4953 	/* Initialize MAC/BB/RF blocks. */
   4954 	/*
   4955 	 * XXX: urtwn_mac_init() sets R92C_RCR[0:15] = R92C_RCR_APM |
   4956 	 * R92C_RCR_AM | R92C_RCR_AB | R92C_RCR_AICV | R92C_RCR_AMF.
   4957 	 * XXX: This setting should be removed from rtl8192cu_mac[].
   4958 	 */
   4959 	urtwn_mac_init(sc);		// sets R92C_RCR[0:15]
   4960 	urtwn_rxfilter_init(sc);	// reset R92C_RCR
   4961 	urtwn_bb_init(sc);
   4962 	urtwn_rf_init(sc);
   4963 
   4964 	if (ISSET(sc->chip, URTWN_CHIP_88E) ||
   4965 	    ISSET(sc->chip, URTWN_CHIP_92EU)) {
   4966 		urtwn_write_2(sc, R92C_CR,
   4967 		    urtwn_read_2(sc, R92C_CR) | R92C_CR_MACTXEN |
   4968 		      R92C_CR_MACRXEN);
   4969 	}
   4970 
   4971 	/* Turn CCK and OFDM blocks on. */
   4972 	reg = urtwn_bb_read(sc, R92C_FPGA0_RFMOD);
   4973 	reg |= R92C_RFMOD_CCK_EN;
   4974 	urtwn_bb_write(sc, R92C_FPGA0_RFMOD, reg);
   4975 	reg = urtwn_bb_read(sc, R92C_FPGA0_RFMOD);
   4976 	reg |= R92C_RFMOD_OFDM_EN;
   4977 	urtwn_bb_write(sc, R92C_FPGA0_RFMOD, reg);
   4978 
   4979 	/* Clear per-station keys table. */
   4980 	urtwn_cam_init(sc);
   4981 
   4982 	/* Enable hardware sequence numbering. */
   4983 	urtwn_write_1(sc, R92C_HWSEQ_CTRL, 0xff);
   4984 
   4985 	/* Perform LO and IQ calibrations. */
   4986 	urtwn_iq_calib(sc, sc->iqk_inited);
   4987 	sc->iqk_inited = true;
   4988 
   4989 	/* Perform LC calibration. */
   4990 	urtwn_lc_calib(sc);
   4991 
   4992 	if (!ISSET(sc->chip, URTWN_CHIP_88E) &&
   4993 	    !ISSET(sc->chip, URTWN_CHIP_92EU)) {
   4994 		/* Fix USB interference issue. */
   4995 		urtwn_write_1(sc, 0xfe40, 0xe0);
   4996 		urtwn_write_1(sc, 0xfe41, 0x8d);
   4997 		urtwn_write_1(sc, 0xfe42, 0x80);
   4998 		urtwn_write_4(sc, 0x20c, 0xfd0320);
   4999 
   5000 		urtwn_pa_bias_init(sc);
   5001 	}
   5002 
   5003 	if (!(sc->chip & (URTWN_CHIP_92C | URTWN_CHIP_92C_1T2R)) ||
   5004 	    !(sc->chip & URTWN_CHIP_92EU)) {
   5005 		/* 1T1R */
   5006 		urtwn_bb_write(sc, R92C_FPGA0_RFPARAM(0),
   5007 		    urtwn_bb_read(sc, R92C_FPGA0_RFPARAM(0)) | __BIT(13));
   5008 	}
   5009 
   5010 	/* Initialize GPIO setting. */
   5011 	urtwn_write_1(sc, R92C_GPIO_MUXCFG,
   5012 	    urtwn_read_1(sc, R92C_GPIO_MUXCFG) & ~R92C_GPIO_MUXCFG_ENBT);
   5013 
   5014 	/* Fix for lower temperature. */
   5015 	if (!ISSET(sc->chip, URTWN_CHIP_88E) &&
   5016 	    !ISSET(sc->chip, URTWN_CHIP_92EU))
   5017 		urtwn_write_1(sc, 0x15, 0xe9);
   5018 
   5019 	/* Set default channel. */
   5020 	urtwn_set_chan(sc, ic->ic_curchan, IEEE80211_HTINFO_2NDCHAN_NONE);
   5021 
   5022 	/* Queue Rx xfers. */
   5023 	for (size_t j = 0; j < sc->rx_npipe; j++) {
   5024 		for (i = 0; i < URTWN_RX_LIST_COUNT; i++) {
   5025 			data = &sc->rx_data[j][i];
   5026 			usbd_setup_xfer(data->xfer, data, data->buf,
   5027 			    URTWN_RXBUFSZ, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
   5028 			    urtwn_rxeof);
   5029 			error = usbd_transfer(data->xfer);
   5030 			if (__predict_false(error != USBD_NORMAL_COMPLETION &&
   5031 			    error != USBD_IN_PROGRESS))
   5032 				goto fail;
   5033 		}
   5034 	}
   5035 
   5036 	/* We're ready to go. */
   5037 	ifp->if_flags &= ~IFF_OACTIVE;
   5038 	ifp->if_flags |= IFF_RUNNING;
   5039 	sc->sc_running = true;
   5040 
   5041 	mutex_exit(&sc->sc_write_mtx);
   5042 
   5043 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
   5044 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   5045 	else if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
   5046 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   5047 	urtwn_wait_async(sc);
   5048 
   5049 	return 0;
   5050 
   5051  fail:
   5052 	mutex_exit(&sc->sc_write_mtx);
   5053 
   5054 	urtwn_stop(ifp, 1);
   5055 	return error;
   5056 }
   5057 
   5058 static void __noinline
   5059 urtwn_stop(struct ifnet *ifp, int disable)
   5060 {
   5061 	struct urtwn_softc *sc = ifp->if_softc;
   5062 	struct ieee80211com *ic = &sc->sc_ic;
   5063 	size_t i;
   5064 	int s;
   5065 
   5066 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   5067 
   5068 	s = splusb();
   5069 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   5070 	urtwn_wait_async(sc);
   5071 	splx(s);
   5072 
   5073 	sc->tx_timer = 0;
   5074 	ifp->if_timer = 0;
   5075 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   5076 
   5077 	callout_stop(&sc->sc_scan_to);
   5078 	callout_stop(&sc->sc_calib_to);
   5079 
   5080 	/* Abort Tx. */
   5081 	for (i = 0; i < sc->tx_npipe; i++) {
   5082 		if (sc->tx_pipe[i] != NULL)
   5083 			usbd_abort_pipe(sc->tx_pipe[i]);
   5084 	}
   5085 
   5086 	/* Stop Rx pipe. */
   5087 	for (i = 0; i < sc->rx_npipe; i++) {
   5088 		if (sc->rx_pipe[i] != NULL)
   5089 			usbd_abort_pipe(sc->rx_pipe[i]);
   5090 	}
   5091 
   5092 	/* Free Tx/Rx buffers. */
   5093 	urtwn_free_tx_list(sc);
   5094 	urtwn_free_rx_list(sc);
   5095 
   5096 	sc->sc_running = false;
   5097 	if (disable)
   5098 		urtwn_chip_stop(sc);
   5099 }
   5100 
   5101 static int
   5102 urtwn_reset(struct ifnet *ifp)
   5103 {
   5104 	struct urtwn_softc *sc = ifp->if_softc;
   5105 	struct ieee80211com *ic = &sc->sc_ic;
   5106 
   5107 	if (ic->ic_opmode != IEEE80211_M_MONITOR)
   5108 		return ENETRESET;
   5109 
   5110 	urtwn_set_chan(sc, ic->ic_curchan, IEEE80211_HTINFO_2NDCHAN_NONE);
   5111 
   5112 	return 0;
   5113 }
   5114 
   5115 static void
   5116 urtwn_chip_stop(struct urtwn_softc *sc)
   5117 {
   5118 	uint32_t reg;
   5119 	bool disabled = true;
   5120 
   5121 	URTWNHIST_FUNC(); URTWNHIST_CALLED();
   5122 
   5123 	if (ISSET(sc->chip, URTWN_CHIP_88E) ||
   5124 	    ISSET(sc->chip, URTWN_CHIP_92EU))
   5125 		return;
   5126 
   5127 	mutex_enter(&sc->sc_write_mtx);
   5128 
   5129 	/*
   5130 	 * RF Off Sequence
   5131 	 */
   5132 	/* Pause MAC TX queue */
   5133 	urtwn_write_1(sc, R92C_TXPAUSE, 0xFF);
   5134 
   5135 	/* Disable RF */
   5136 	urtwn_rf_write(sc, 0, 0, 0);
   5137 
   5138 	urtwn_write_1(sc, R92C_APSD_CTRL, R92C_APSD_CTRL_OFF);
   5139 
   5140 	/* Reset BB state machine */
   5141 	urtwn_write_1(sc, R92C_SYS_FUNC_EN,
   5142 	    R92C_SYS_FUNC_EN_USBD |
   5143 	    R92C_SYS_FUNC_EN_USBA |
   5144 	    R92C_SYS_FUNC_EN_BB_GLB_RST);
   5145 	urtwn_write_1(sc, R92C_SYS_FUNC_EN,
   5146 	    R92C_SYS_FUNC_EN_USBD | R92C_SYS_FUNC_EN_USBA);
   5147 
   5148 	/*
   5149 	 * Reset digital sequence
   5150 	 */
   5151 	if (urtwn_read_1(sc, R92C_MCUFWDL) & R92C_MCUFWDL_RDY) {
   5152 		/* Reset MCU ready status */
   5153 		urtwn_write_1(sc, R92C_MCUFWDL, 0);
   5154 		/* If firmware in ram code, do reset */
   5155 		if (ISSET(sc->sc_flags, URTWN_FLAG_FWREADY)) {
   5156 			if (ISSET(sc->chip, URTWN_CHIP_88E) ||
   5157 			    ISSET(sc->chip, URTWN_CHIP_92EU))
   5158 				urtwn_r88e_fw_reset(sc);
   5159 			else
   5160 				urtwn_fw_reset(sc);
   5161 			CLR(sc->sc_flags, URTWN_FLAG_FWREADY);
   5162 		}
   5163 	}
   5164 
   5165 	/* Reset MAC and Enable 8051 */
   5166 	urtwn_write_1(sc, R92C_SYS_FUNC_EN + 1, 0x54);
   5167 
   5168 	/* Reset MCU ready status */
   5169 	urtwn_write_1(sc, R92C_MCUFWDL, 0);
   5170 
   5171 	if (disabled) {
   5172 		/* Disable MAC clock */
   5173 		urtwn_write_2(sc, R92C_SYS_CLKR, 0x70A3);
   5174 		/* Disable AFE PLL */
   5175 		urtwn_write_1(sc, R92C_AFE_PLL_CTRL, 0x80);
   5176 		/* Gated AFE DIG_CLOCK */
   5177 		urtwn_write_2(sc, R92C_AFE_XTAL_CTRL, 0x880F);
   5178 		/* Isolated digital to PON */
   5179 		urtwn_write_1(sc, R92C_SYS_ISO_CTRL, 0xF9);
   5180 	}
   5181 
   5182 	/*
   5183 	 * Pull GPIO PIN to balance level and LED control
   5184 	 */
   5185 	/* 1. Disable GPIO[7:0] */
   5186 	urtwn_write_2(sc, R92C_GPIO_PIN_CTRL + 2, 0x0000);
   5187 
   5188 	reg = urtwn_read_4(sc, R92C_GPIO_PIN_CTRL) & ~0x0000ff00;
   5189 	reg |= ((reg << 8) & 0x0000ff00) | 0x00ff0000;
   5190 	urtwn_write_4(sc, R92C_GPIO_PIN_CTRL, reg);
   5191 
   5192 	/* Disable GPIO[10:8] */
   5193 	urtwn_write_1(sc, R92C_GPIO_MUXCFG + 3, 0x00);
   5194 
   5195 	reg = urtwn_read_2(sc, R92C_GPIO_MUXCFG + 2) & ~0x00f0;
   5196 	reg |= (((reg & 0x000f) << 4) | 0x0780);
   5197 	urtwn_write_2(sc, R92C_GPIO_MUXCFG + 2, reg);
   5198 
   5199 	/* Disable LED0 & 1 */
   5200 	urtwn_write_2(sc, R92C_LEDCFG0, 0x8080);
   5201 
   5202 	/*
   5203 	 * Reset digital sequence
   5204 	 */
   5205 	if (disabled) {
   5206 		/* Disable ELDR clock */
   5207 		urtwn_write_2(sc, R92C_SYS_CLKR, 0x70A3);
   5208 		/* Isolated ELDR to PON */
   5209 		urtwn_write_1(sc, R92C_SYS_ISO_CTRL + 1, 0x82);
   5210 	}
   5211 
   5212 	/*
   5213 	 * Disable analog sequence
   5214 	 */
   5215 	if (disabled) {
   5216 		/* Disable A15 power */
   5217 		urtwn_write_1(sc, R92C_LDOA15_CTRL, 0x04);
   5218 		/* Disable digital core power */
   5219 		urtwn_write_1(sc, R92C_LDOV12D_CTRL,
   5220 		    urtwn_read_1(sc, R92C_LDOV12D_CTRL) &
   5221 		      ~R92C_LDOV12D_CTRL_LDV12_EN);
   5222 	}
   5223 
   5224 	/* Enter PFM mode */
   5225 	urtwn_write_1(sc, R92C_SPS0_CTRL, 0x23);
   5226 
   5227 	/* Set USB suspend */
   5228 	urtwn_write_2(sc, R92C_APS_FSMCO,
   5229 	    R92C_APS_FSMCO_APDM_HOST |
   5230 	    R92C_APS_FSMCO_AFSM_HSUS |
   5231 	    R92C_APS_FSMCO_PFM_ALDN);
   5232 
   5233 	urtwn_write_1(sc, R92C_RSV_CTRL, 0x0E);
   5234 
   5235 	mutex_exit(&sc->sc_write_mtx);
   5236 }
   5237 
   5238 static void
   5239 urtwn_delay_ms(struct urtwn_softc *sc, int ms)
   5240 {
   5241 	if (sc->sc_running == false)
   5242 		DELAY(ms * 1000);
   5243 	else
   5244 		usbd_delay_ms(sc->sc_udev, ms);
   5245 }
   5246 
   5247 MODULE(MODULE_CLASS_DRIVER, if_urtwn, NULL);
   5248 
   5249 #ifdef _MODULE
   5250 #include "ioconf.c"
   5251 #endif
   5252 
   5253 static int
   5254 if_urtwn_modcmd(modcmd_t cmd, void *aux)
   5255 {
   5256 	int error = 0;
   5257 
   5258 	switch (cmd) {
   5259 	case MODULE_CMD_INIT:
   5260 #ifdef _MODULE
   5261 		error = config_init_component(cfdriver_ioconf_urtwn,
   5262 		    cfattach_ioconf_urtwn, cfdata_ioconf_urtwn);
   5263 #endif
   5264 		return error;
   5265 	case MODULE_CMD_FINI:
   5266 #ifdef _MODULE
   5267 		error = config_fini_component(cfdriver_ioconf_urtwn,
   5268 		    cfattach_ioconf_urtwn, cfdata_ioconf_urtwn);
   5269 #endif
   5270 		return error;
   5271 	default:
   5272 		return ENOTTY;
   5273 	}
   5274 }
   5275