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