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