xref: /freebsd/sys/dev/usb/wlan/if_rsu.c (revision b8303685fab0406e705bd6954ae42598b68767c8)
131d98677SRui Paulo /*	$OpenBSD: if_rsu.c,v 1.17 2013/04/15 09:23:01 mglocker Exp $	*/
231d98677SRui Paulo 
331d98677SRui Paulo /*-
431d98677SRui Paulo  * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
531d98677SRui Paulo  *
631d98677SRui Paulo  * Permission to use, copy, modify, and distribute this software for any
731d98677SRui Paulo  * purpose with or without fee is hereby granted, provided that the above
831d98677SRui Paulo  * copyright notice and this permission notice appear in all copies.
931d98677SRui Paulo  *
1031d98677SRui Paulo  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1131d98677SRui Paulo  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1231d98677SRui Paulo  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1331d98677SRui Paulo  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1431d98677SRui Paulo  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1531d98677SRui Paulo  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1631d98677SRui Paulo  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1731d98677SRui Paulo  */
1831d98677SRui Paulo #include <sys/cdefs.h>
1931d98677SRui Paulo __FBSDID("$FreeBSD$");
2031d98677SRui Paulo 
2131d98677SRui Paulo /*
2231d98677SRui Paulo  * Driver for Realtek RTL8188SU/RTL8191SU/RTL8192SU.
2331d98677SRui Paulo  *
2431d98677SRui Paulo  * TODO:
2531d98677SRui Paulo  *   o 11n support
2631d98677SRui Paulo  *   o h/w crypto
2731d98677SRui Paulo  *   o hostap / ibss / mesh
2831d98677SRui Paulo  */
294914fa0fSAdrian Chadd 
3031d98677SRui Paulo #include <sys/param.h>
3131d98677SRui Paulo #include <sys/endian.h>
3231d98677SRui Paulo #include <sys/sockio.h>
3331d98677SRui Paulo #include <sys/mbuf.h>
3431d98677SRui Paulo #include <sys/kernel.h>
3531d98677SRui Paulo #include <sys/socket.h>
3631d98677SRui Paulo #include <sys/systm.h>
3731d98677SRui Paulo #include <sys/conf.h>
3831d98677SRui Paulo #include <sys/bus.h>
3931d98677SRui Paulo #include <sys/rman.h>
4031d98677SRui Paulo #include <sys/firmware.h>
4131d98677SRui Paulo #include <sys/module.h>
4231d98677SRui Paulo 
4331d98677SRui Paulo #include <machine/bus.h>
4431d98677SRui Paulo #include <machine/resource.h>
4531d98677SRui Paulo 
4631d98677SRui Paulo #include <net/bpf.h>
4731d98677SRui Paulo #include <net/if.h>
4876039bc8SGleb Smirnoff #include <net/if_var.h>
4931d98677SRui Paulo #include <net/if_arp.h>
5031d98677SRui Paulo #include <net/if_dl.h>
5131d98677SRui Paulo #include <net/if_media.h>
5231d98677SRui Paulo #include <net/if_types.h>
5331d98677SRui Paulo 
5431d98677SRui Paulo #include <netinet/in.h>
5531d98677SRui Paulo #include <netinet/in_systm.h>
5631d98677SRui Paulo #include <netinet/in_var.h>
5731d98677SRui Paulo #include <netinet/if_ether.h>
5831d98677SRui Paulo #include <netinet/ip.h>
5931d98677SRui Paulo 
6031d98677SRui Paulo #include <net80211/ieee80211_var.h>
6131d98677SRui Paulo #include <net80211/ieee80211_regdomain.h>
6231d98677SRui Paulo #include <net80211/ieee80211_radiotap.h>
6331d98677SRui Paulo 
6431d98677SRui Paulo #include <dev/usb/usb.h>
6531d98677SRui Paulo #include <dev/usb/usbdi.h>
6631d98677SRui Paulo #include "usbdevs.h"
6731d98677SRui Paulo 
6831d98677SRui Paulo #define USB_DEBUG_VAR rsu_debug
6931d98677SRui Paulo #include <dev/usb/usb_debug.h>
7031d98677SRui Paulo 
7131d98677SRui Paulo #include <dev/usb/wlan/if_rsureg.h>
7231d98677SRui Paulo 
7331d98677SRui Paulo #ifdef USB_DEBUG
7431d98677SRui Paulo static int rsu_debug = 0;
7531d98677SRui Paulo SYSCTL_NODE(_hw_usb, OID_AUTO, rsu, CTLFLAG_RW, 0, "USB rsu");
76ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_rsu, OID_AUTO, debug, CTLFLAG_RWTUN, &rsu_debug, 0,
7731d98677SRui Paulo     "Debug level");
784914fa0fSAdrian Chadd #define	RSU_DPRINTF(_sc, _flg, ...)					\
794914fa0fSAdrian Chadd 	do								\
804914fa0fSAdrian Chadd 		if (((_flg) == (RSU_DEBUG_ANY)) || (rsu_debug & (_flg))) \
814914fa0fSAdrian Chadd 			device_printf((_sc)->sc_dev, __VA_ARGS__);	\
824914fa0fSAdrian Chadd 	while (0)
834914fa0fSAdrian Chadd #else
844914fa0fSAdrian Chadd #define	RSU_DPRINTF(_sc, _flg, ...)
8531d98677SRui Paulo #endif
8631d98677SRui Paulo 
874914fa0fSAdrian Chadd #define	RSU_DEBUG_ANY		0xffffffff
884914fa0fSAdrian Chadd #define	RSU_DEBUG_TX		0x00000001
894914fa0fSAdrian Chadd #define	RSU_DEBUG_RX		0x00000002
904914fa0fSAdrian Chadd #define	RSU_DEBUG_RESET		0x00000004
914914fa0fSAdrian Chadd #define	RSU_DEBUG_CALIB		0x00000008
924914fa0fSAdrian Chadd #define	RSU_DEBUG_STATE		0x00000010
934914fa0fSAdrian Chadd #define	RSU_DEBUG_SCAN		0x00000020
944914fa0fSAdrian Chadd #define	RSU_DEBUG_FWCMD		0x00000040
954914fa0fSAdrian Chadd #define	RSU_DEBUG_TXDONE	0x00000080
964914fa0fSAdrian Chadd #define	RSU_DEBUG_FW		0x00000100
974914fa0fSAdrian Chadd #define	RSU_DEBUG_FWDBG		0x00000200
984914fa0fSAdrian Chadd 
9931d98677SRui Paulo static const STRUCT_USB_HOST_ID rsu_devs[] = {
10031d98677SRui Paulo #define	RSU_HT_NOT_SUPPORTED 0
10131d98677SRui Paulo #define	RSU_HT_SUPPORTED 1
10231d98677SRui Paulo #define RSU_DEV_HT(v,p)  { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, \
10331d98677SRui Paulo 				   RSU_HT_SUPPORTED) }
10431d98677SRui Paulo #define RSU_DEV(v,p)     { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, \
10531d98677SRui Paulo 				   RSU_HT_NOT_SUPPORTED) }
10631d98677SRui Paulo 	RSU_DEV(ASUS,			RTL8192SU),
10731d98677SRui Paulo 	RSU_DEV(AZUREWAVE,		RTL8192SU_4),
10831d98677SRui Paulo 	RSU_DEV_HT(ACCTON,		RTL8192SU),
10931d98677SRui Paulo 	RSU_DEV_HT(ASUS,		USBN10),
11031d98677SRui Paulo 	RSU_DEV_HT(AZUREWAVE,		RTL8192SU_1),
11131d98677SRui Paulo 	RSU_DEV_HT(AZUREWAVE,		RTL8192SU_2),
11231d98677SRui Paulo 	RSU_DEV_HT(AZUREWAVE,		RTL8192SU_3),
11331d98677SRui Paulo 	RSU_DEV_HT(AZUREWAVE,		RTL8192SU_5),
11431d98677SRui Paulo 	RSU_DEV_HT(BELKIN,		RTL8192SU_1),
11531d98677SRui Paulo 	RSU_DEV_HT(BELKIN,		RTL8192SU_2),
11631d98677SRui Paulo 	RSU_DEV_HT(BELKIN,		RTL8192SU_3),
11731d98677SRui Paulo 	RSU_DEV_HT(CONCEPTRONIC2,	RTL8192SU_1),
11831d98677SRui Paulo 	RSU_DEV_HT(CONCEPTRONIC2,	RTL8192SU_2),
11931d98677SRui Paulo 	RSU_DEV_HT(CONCEPTRONIC2,	RTL8192SU_3),
12031d98677SRui Paulo 	RSU_DEV_HT(COREGA,		RTL8192SU),
12131d98677SRui Paulo 	RSU_DEV_HT(DLINK2,		DWA131A1),
12231d98677SRui Paulo 	RSU_DEV_HT(DLINK2,		RTL8192SU_1),
12331d98677SRui Paulo 	RSU_DEV_HT(DLINK2,		RTL8192SU_2),
12431d98677SRui Paulo 	RSU_DEV_HT(EDIMAX,		RTL8192SU_1),
12531d98677SRui Paulo 	RSU_DEV_HT(EDIMAX,		RTL8192SU_2),
126bf124fcfSKevin Lo 	RSU_DEV_HT(EDIMAX,		EW7622UMN),
12731d98677SRui Paulo 	RSU_DEV_HT(GUILLEMOT,		HWGUN54),
12831d98677SRui Paulo 	RSU_DEV_HT(GUILLEMOT,		HWNUM300),
12931d98677SRui Paulo 	RSU_DEV_HT(HAWKING,		RTL8192SU_1),
13031d98677SRui Paulo 	RSU_DEV_HT(HAWKING,		RTL8192SU_2),
13131d98677SRui Paulo 	RSU_DEV_HT(PLANEX2,		GWUSNANO),
13231d98677SRui Paulo 	RSU_DEV_HT(REALTEK,		RTL8171),
13331d98677SRui Paulo 	RSU_DEV_HT(REALTEK,		RTL8172),
13431d98677SRui Paulo 	RSU_DEV_HT(REALTEK,		RTL8173),
13531d98677SRui Paulo 	RSU_DEV_HT(REALTEK,		RTL8174),
13631d98677SRui Paulo 	RSU_DEV_HT(REALTEK,		RTL8192SU),
13731d98677SRui Paulo 	RSU_DEV_HT(REALTEK,		RTL8712),
13831d98677SRui Paulo 	RSU_DEV_HT(REALTEK,		RTL8713),
13931d98677SRui Paulo 	RSU_DEV_HT(SENAO,		RTL8192SU_1),
14031d98677SRui Paulo 	RSU_DEV_HT(SENAO,		RTL8192SU_2),
14131d98677SRui Paulo 	RSU_DEV_HT(SITECOMEU,		WL349V1),
14231d98677SRui Paulo 	RSU_DEV_HT(SITECOMEU,		WL353),
14331d98677SRui Paulo 	RSU_DEV_HT(SWEEX2,		LW154),
144fa20eb98SKevin Lo 	RSU_DEV_HT(TRENDNET,		TEW646UBH),
14531d98677SRui Paulo #undef RSU_DEV_HT
14631d98677SRui Paulo #undef RSU_DEV
14731d98677SRui Paulo };
14831d98677SRui Paulo 
14931d98677SRui Paulo static device_probe_t   rsu_match;
15031d98677SRui Paulo static device_attach_t  rsu_attach;
15131d98677SRui Paulo static device_detach_t  rsu_detach;
152910593b5SHans Petter Selasky static usb_callback_t   rsu_bulk_tx_callback_be_bk;
153910593b5SHans Petter Selasky static usb_callback_t   rsu_bulk_tx_callback_vi_vo;
154bc6a9865SAdrian Chadd static usb_callback_t   rsu_bulk_tx_callback_h2c;
15531d98677SRui Paulo static usb_callback_t   rsu_bulk_rx_callback;
15631d98677SRui Paulo static usb_error_t	rsu_do_request(struct rsu_softc *,
15731d98677SRui Paulo 			    struct usb_device_request *, void *);
15831d98677SRui Paulo static struct ieee80211vap *
15931d98677SRui Paulo 		rsu_vap_create(struct ieee80211com *, const char name[],
16031d98677SRui Paulo 		    int, enum ieee80211_opmode, int, const uint8_t bssid[],
16131d98677SRui Paulo 		    const uint8_t mac[]);
16231d98677SRui Paulo static void	rsu_vap_delete(struct ieee80211vap *);
16331d98677SRui Paulo static void	rsu_scan_start(struct ieee80211com *);
16431d98677SRui Paulo static void	rsu_scan_end(struct ieee80211com *);
16531d98677SRui Paulo static void	rsu_set_channel(struct ieee80211com *);
166272f6adeSGleb Smirnoff static void	rsu_update_mcast(struct ieee80211com *);
16731d98677SRui Paulo static int	rsu_alloc_rx_list(struct rsu_softc *);
16831d98677SRui Paulo static void	rsu_free_rx_list(struct rsu_softc *);
16931d98677SRui Paulo static int	rsu_alloc_tx_list(struct rsu_softc *);
17031d98677SRui Paulo static void	rsu_free_tx_list(struct rsu_softc *);
17131d98677SRui Paulo static void	rsu_free_list(struct rsu_softc *, struct rsu_data [], int);
17231d98677SRui Paulo static struct rsu_data *_rsu_getbuf(struct rsu_softc *);
17331d98677SRui Paulo static struct rsu_data *rsu_getbuf(struct rsu_softc *);
17431d98677SRui Paulo static int	rsu_write_region_1(struct rsu_softc *, uint16_t, uint8_t *,
17531d98677SRui Paulo 		    int);
17631d98677SRui Paulo static void	rsu_write_1(struct rsu_softc *, uint16_t, uint8_t);
17731d98677SRui Paulo static void	rsu_write_2(struct rsu_softc *, uint16_t, uint16_t);
17831d98677SRui Paulo static void	rsu_write_4(struct rsu_softc *, uint16_t, uint32_t);
17931d98677SRui Paulo static int	rsu_read_region_1(struct rsu_softc *, uint16_t, uint8_t *,
18031d98677SRui Paulo 		    int);
18131d98677SRui Paulo static uint8_t	rsu_read_1(struct rsu_softc *, uint16_t);
18231d98677SRui Paulo static uint16_t	rsu_read_2(struct rsu_softc *, uint16_t);
18331d98677SRui Paulo static uint32_t	rsu_read_4(struct rsu_softc *, uint16_t);
18431d98677SRui Paulo static int	rsu_fw_iocmd(struct rsu_softc *, uint32_t);
18531d98677SRui Paulo static uint8_t	rsu_efuse_read_1(struct rsu_softc *, uint16_t);
18631d98677SRui Paulo static int	rsu_read_rom(struct rsu_softc *);
18731d98677SRui Paulo static int	rsu_fw_cmd(struct rsu_softc *, uint8_t, void *, int);
18831d98677SRui Paulo static void	rsu_calib_task(void *, int);
18931d98677SRui Paulo static int	rsu_newstate(struct ieee80211vap *, enum ieee80211_state, int);
19031d98677SRui Paulo #ifdef notyet
19131d98677SRui Paulo static void	rsu_set_key(struct rsu_softc *, const struct ieee80211_key *);
19231d98677SRui Paulo static void	rsu_delete_key(struct rsu_softc *, const struct ieee80211_key *);
19331d98677SRui Paulo #endif
19431d98677SRui Paulo static int	rsu_site_survey(struct rsu_softc *, struct ieee80211vap *);
19531d98677SRui Paulo static int	rsu_join_bss(struct rsu_softc *, struct ieee80211_node *);
19631d98677SRui Paulo static int	rsu_disconnect(struct rsu_softc *);
19731d98677SRui Paulo static void	rsu_event_survey(struct rsu_softc *, uint8_t *, int);
19831d98677SRui Paulo static void	rsu_event_join_bss(struct rsu_softc *, uint8_t *, int);
19931d98677SRui Paulo static void	rsu_rx_event(struct rsu_softc *, uint8_t, uint8_t *, int);
20031d98677SRui Paulo static void	rsu_rx_multi_event(struct rsu_softc *, uint8_t *, int);
20131d98677SRui Paulo static int8_t	rsu_get_rssi(struct rsu_softc *, int, void *);
20231d98677SRui Paulo static struct mbuf *
20331d98677SRui Paulo 		rsu_rx_frame(struct rsu_softc *, uint8_t *, int, int *);
20431d98677SRui Paulo static struct mbuf *
20531d98677SRui Paulo 		rsu_rx_multi_frame(struct rsu_softc *, uint8_t *, int, int *);
20631d98677SRui Paulo static struct mbuf *
20731d98677SRui Paulo 		rsu_rxeof(struct usb_xfer *, struct rsu_data *, int *);
20831d98677SRui Paulo static void	rsu_txeof(struct usb_xfer *, struct rsu_data *);
20931d98677SRui Paulo static int	rsu_raw_xmit(struct ieee80211_node *, struct mbuf *,
21031d98677SRui Paulo 		    const struct ieee80211_bpf_params *);
2117a79cebfSGleb Smirnoff static void	rsu_init(struct rsu_softc *);
21231d98677SRui Paulo static int	rsu_tx_start(struct rsu_softc *, struct ieee80211_node *,
213400b4e53SHans Petter Selasky 		    struct mbuf *, struct rsu_data *);
2147a79cebfSGleb Smirnoff static int	rsu_transmit(struct ieee80211com *, struct mbuf *);
2157a79cebfSGleb Smirnoff static void	rsu_start(struct rsu_softc *);
2167a79cebfSGleb Smirnoff static void	rsu_parent(struct ieee80211com *);
2177a79cebfSGleb Smirnoff static void	rsu_stop(struct rsu_softc *);
21817ebf553SAdrian Chadd static void	rsu_ms_delay(struct rsu_softc *, int);
21931d98677SRui Paulo 
22031d98677SRui Paulo static device_method_t rsu_methods[] = {
22131d98677SRui Paulo 	DEVMETHOD(device_probe,		rsu_match),
22231d98677SRui Paulo 	DEVMETHOD(device_attach,	rsu_attach),
22331d98677SRui Paulo 	DEVMETHOD(device_detach,	rsu_detach),
22431d98677SRui Paulo 
22531d98677SRui Paulo 	DEVMETHOD_END
22631d98677SRui Paulo };
22731d98677SRui Paulo 
22831d98677SRui Paulo static driver_t rsu_driver = {
22931d98677SRui Paulo 	.name = "rsu",
23031d98677SRui Paulo 	.methods = rsu_methods,
23131d98677SRui Paulo 	.size = sizeof(struct rsu_softc)
23231d98677SRui Paulo };
23331d98677SRui Paulo 
23431d98677SRui Paulo static devclass_t rsu_devclass;
23531d98677SRui Paulo 
23631d98677SRui Paulo DRIVER_MODULE(rsu, uhub, rsu_driver, rsu_devclass, NULL, 0);
23731d98677SRui Paulo MODULE_DEPEND(rsu, wlan, 1, 1, 1);
23831d98677SRui Paulo MODULE_DEPEND(rsu, usb, 1, 1, 1);
23931d98677SRui Paulo MODULE_DEPEND(rsu, firmware, 1, 1, 1);
24031d98677SRui Paulo MODULE_VERSION(rsu, 1);
24131d98677SRui Paulo 
242910593b5SHans Petter Selasky static uint8_t rsu_wme_ac_xfer_map[4] = {
243910593b5SHans Petter Selasky 	[WME_AC_BE] = RSU_BULK_TX_BE_BK,
244910593b5SHans Petter Selasky 	[WME_AC_BK] = RSU_BULK_TX_BE_BK,
245910593b5SHans Petter Selasky 	[WME_AC_VI] = RSU_BULK_TX_VI_VO,
246910593b5SHans Petter Selasky 	[WME_AC_VO] = RSU_BULK_TX_VI_VO,
247910593b5SHans Petter Selasky };
248910593b5SHans Petter Selasky 
249bc6a9865SAdrian Chadd /* XXX hard-coded */
250bc6a9865SAdrian Chadd #define	RSU_H2C_ENDPOINT	3
251bc6a9865SAdrian Chadd 
25231d98677SRui Paulo static const struct usb_config rsu_config[RSU_N_TRANSFER] = {
25331d98677SRui Paulo 	[RSU_BULK_RX] = {
25431d98677SRui Paulo 		.type = UE_BULK,
25531d98677SRui Paulo 		.endpoint = UE_ADDR_ANY,
25631d98677SRui Paulo 		.direction = UE_DIR_IN,
25731d98677SRui Paulo 		.bufsize = RSU_RXBUFSZ,
25831d98677SRui Paulo 		.flags = {
25931d98677SRui Paulo 			.pipe_bof = 1,
26031d98677SRui Paulo 			.short_xfer_ok = 1
26131d98677SRui Paulo 		},
26231d98677SRui Paulo 		.callback = rsu_bulk_rx_callback
26331d98677SRui Paulo 	},
264910593b5SHans Petter Selasky 	[RSU_BULK_TX_BE_BK] = {
26531d98677SRui Paulo 		.type = UE_BULK,
26631d98677SRui Paulo 		.endpoint = 0x06,
26731d98677SRui Paulo 		.direction = UE_DIR_OUT,
26831d98677SRui Paulo 		.bufsize = RSU_TXBUFSZ,
26931d98677SRui Paulo 		.flags = {
27031d98677SRui Paulo 			.ext_buffer = 1,
27131d98677SRui Paulo 			.pipe_bof = 1,
27231d98677SRui Paulo 			.force_short_xfer = 1
27331d98677SRui Paulo 		},
274910593b5SHans Petter Selasky 		.callback = rsu_bulk_tx_callback_be_bk,
27531d98677SRui Paulo 		.timeout = RSU_TX_TIMEOUT
27631d98677SRui Paulo 	},
277910593b5SHans Petter Selasky 	[RSU_BULK_TX_VI_VO] = {
27831d98677SRui Paulo 		.type = UE_BULK,
27931d98677SRui Paulo 		.endpoint = 0x04,
28031d98677SRui Paulo 		.direction = UE_DIR_OUT,
28131d98677SRui Paulo 		.bufsize = RSU_TXBUFSZ,
28231d98677SRui Paulo 		.flags = {
28331d98677SRui Paulo 			.ext_buffer = 1,
28431d98677SRui Paulo 			.pipe_bof = 1,
28531d98677SRui Paulo 			.force_short_xfer = 1
28631d98677SRui Paulo 		},
287910593b5SHans Petter Selasky 		.callback = rsu_bulk_tx_callback_vi_vo,
28831d98677SRui Paulo 		.timeout = RSU_TX_TIMEOUT
28931d98677SRui Paulo 	},
290bc6a9865SAdrian Chadd 	[RSU_BULK_TX_H2C] = {
291bc6a9865SAdrian Chadd 		.type = UE_BULK,
292bc6a9865SAdrian Chadd 		.endpoint = 0x0d,
293bc6a9865SAdrian Chadd 		.direction = UE_DIR_OUT,
294bc6a9865SAdrian Chadd 		.bufsize = RSU_TXBUFSZ,
295bc6a9865SAdrian Chadd 		.flags = {
296bc6a9865SAdrian Chadd 			.ext_buffer = 1,
297bc6a9865SAdrian Chadd 			.pipe_bof = 1,
298bc6a9865SAdrian Chadd 			.short_xfer_ok = 1
299bc6a9865SAdrian Chadd 		},
300bc6a9865SAdrian Chadd 		.callback = rsu_bulk_tx_callback_h2c,
301bc6a9865SAdrian Chadd 		.timeout = RSU_TX_TIMEOUT
302bc6a9865SAdrian Chadd 	},
30331d98677SRui Paulo };
30431d98677SRui Paulo 
30531d98677SRui Paulo static int
30631d98677SRui Paulo rsu_match(device_t self)
30731d98677SRui Paulo {
30831d98677SRui Paulo 	struct usb_attach_arg *uaa = device_get_ivars(self);
30931d98677SRui Paulo 
31031d98677SRui Paulo 	if (uaa->usb_mode != USB_MODE_HOST ||
31131d98677SRui Paulo 	    uaa->info.bIfaceIndex != 0 ||
31231d98677SRui Paulo 	    uaa->info.bConfigIndex != 0)
31331d98677SRui Paulo 		return (ENXIO);
31431d98677SRui Paulo 
31531d98677SRui Paulo 	return (usbd_lookup_id_by_uaa(rsu_devs, sizeof(rsu_devs), uaa));
31631d98677SRui Paulo }
31731d98677SRui Paulo 
31831d98677SRui Paulo static int
319cf52bbe0SAdrian Chadd rsu_send_mgmt(struct ieee80211_node *ni, int type, int arg)
320cf52bbe0SAdrian Chadd {
321cf52bbe0SAdrian Chadd 
322cf52bbe0SAdrian Chadd 	return (ENOTSUP);
323cf52bbe0SAdrian Chadd }
324cf52bbe0SAdrian Chadd 
325882704d0SAdrian Chadd static void
326882704d0SAdrian Chadd rsu_update_chw(struct ieee80211com *ic)
327882704d0SAdrian Chadd {
328882704d0SAdrian Chadd 
329882704d0SAdrian Chadd }
330882704d0SAdrian Chadd 
331882704d0SAdrian Chadd static int
332882704d0SAdrian Chadd rsu_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
333882704d0SAdrian Chadd {
334882704d0SAdrian Chadd 
335882704d0SAdrian Chadd 	/* Firmware handles this; not our problem */
336882704d0SAdrian Chadd 	return (0);
337882704d0SAdrian Chadd }
338882704d0SAdrian Chadd 
339882704d0SAdrian Chadd static int
340882704d0SAdrian Chadd rsu_wme_update(struct ieee80211com *ic)
341882704d0SAdrian Chadd {
342882704d0SAdrian Chadd 
343882704d0SAdrian Chadd 	/* Firmware handles this; not our problem */
344882704d0SAdrian Chadd 	return (0);
345882704d0SAdrian Chadd }
346882704d0SAdrian Chadd 
347cf52bbe0SAdrian Chadd static int
34831d98677SRui Paulo rsu_attach(device_t self)
34931d98677SRui Paulo {
35031d98677SRui Paulo 	struct usb_attach_arg *uaa = device_get_ivars(self);
35131d98677SRui Paulo 	struct rsu_softc *sc = device_get_softc(self);
3527a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
35331d98677SRui Paulo 	int error;
35431d98677SRui Paulo 	uint8_t iface_index, bands;
35547b0d9ddSAdrian Chadd 	struct usb_interface *iface;
35631d98677SRui Paulo 
35731d98677SRui Paulo 	device_set_usb_desc(self);
35831d98677SRui Paulo 	sc->sc_udev = uaa->device;
35931d98677SRui Paulo 	sc->sc_dev = self;
36047b0d9ddSAdrian Chadd 	sc->sc_ht = !! (USB_GET_DRIVER_INFO(uaa) & RSU_HT_SUPPORTED);
36147b0d9ddSAdrian Chadd 
36247b0d9ddSAdrian Chadd 	/* Get number of endpoints */
36347b0d9ddSAdrian Chadd 	iface = usbd_get_iface(sc->sc_udev, 0);
36447b0d9ddSAdrian Chadd 	sc->sc_nendpoints = iface->idesc->bNumEndpoints;
36531d98677SRui Paulo 
366*b8303685SAdrian Chadd 	/* Endpoints are hard-coded for now, so enforce 4-endpoint only */
367*b8303685SAdrian Chadd 	if (sc->sc_nendpoints != 4) {
368*b8303685SAdrian Chadd 		device_printf(sc->sc_dev,
369*b8303685SAdrian Chadd 		    "the driver currently only supports 4-endpoint devices\n");
370*b8303685SAdrian Chadd 		return (ENXIO);
371*b8303685SAdrian Chadd 	}
372*b8303685SAdrian Chadd 
37331d98677SRui Paulo 	mtx_init(&sc->sc_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
37431d98677SRui Paulo 	    MTX_DEF);
37531d98677SRui Paulo 	TIMEOUT_TASK_INIT(taskqueue_thread, &sc->calib_task, 0,
37631d98677SRui Paulo 	    rsu_calib_task, sc);
3777a79cebfSGleb Smirnoff 	mbufq_init(&sc->sc_snd, ifqmaxlen);
37831d98677SRui Paulo 
379885476cbSHans Petter Selasky 	/* Allocate Tx/Rx buffers. */
380885476cbSHans Petter Selasky 	error = rsu_alloc_rx_list(sc);
381885476cbSHans Petter Selasky 	if (error != 0) {
382885476cbSHans Petter Selasky 		device_printf(sc->sc_dev, "could not allocate Rx buffers\n");
383885476cbSHans Petter Selasky 		goto fail_usb;
384885476cbSHans Petter Selasky 	}
385885476cbSHans Petter Selasky 
386885476cbSHans Petter Selasky 	error = rsu_alloc_tx_list(sc);
387885476cbSHans Petter Selasky 	if (error != 0) {
388885476cbSHans Petter Selasky 		device_printf(sc->sc_dev, "could not allocate Tx buffers\n");
389885476cbSHans Petter Selasky 		rsu_free_rx_list(sc);
390885476cbSHans Petter Selasky 		goto fail_usb;
391885476cbSHans Petter Selasky 	}
392885476cbSHans Petter Selasky 
39331d98677SRui Paulo 	iface_index = 0;
39431d98677SRui Paulo 	error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
39531d98677SRui Paulo 	    rsu_config, RSU_N_TRANSFER, sc, &sc->sc_mtx);
39631d98677SRui Paulo 	if (error) {
39731d98677SRui Paulo 		device_printf(sc->sc_dev,
39831d98677SRui Paulo 		    "could not allocate USB transfers, err=%s\n",
39931d98677SRui Paulo 		    usbd_errstr(error));
40053dfd5c1SRui Paulo 		goto fail_usb;
40131d98677SRui Paulo 	}
40231d98677SRui Paulo 	RSU_LOCK(sc);
40331d98677SRui Paulo 	/* Read chip revision. */
40431d98677SRui Paulo 	sc->cut = MS(rsu_read_4(sc, R92S_PMC_FSM), R92S_PMC_FSM_CUT);
40531d98677SRui Paulo 	if (sc->cut != 3)
40631d98677SRui Paulo 		sc->cut = (sc->cut >> 1) + 1;
40731d98677SRui Paulo 	error = rsu_read_rom(sc);
408abfa11d6SHans Petter Selasky 	RSU_UNLOCK(sc);
40931d98677SRui Paulo 	if (error != 0) {
41031d98677SRui Paulo 		device_printf(self, "could not read ROM\n");
41153dfd5c1SRui Paulo 		goto fail_rom;
41231d98677SRui Paulo 	}
4137a79cebfSGleb Smirnoff 	IEEE80211_ADDR_COPY(ic->ic_macaddr, &sc->rom[0x12]);
41431d98677SRui Paulo 	device_printf(self, "MAC/BB RTL8712 cut %d\n", sc->cut);
41531d98677SRui Paulo 
41659686fe9SGleb Smirnoff 	ic->ic_softc = sc;
417c8550c02SGleb Smirnoff 	ic->ic_name = device_get_nameunit(self);
41831d98677SRui Paulo 	ic->ic_phytype = IEEE80211_T_OFDM;	/* Not only, but not used. */
41931d98677SRui Paulo 	ic->ic_opmode = IEEE80211_M_STA;	/* Default to BSS mode. */
42031d98677SRui Paulo 
42131d98677SRui Paulo 	/* Set device capabilities. */
42231d98677SRui Paulo 	ic->ic_caps =
42331d98677SRui Paulo 	    IEEE80211_C_STA |		/* station mode */
4244914fa0fSAdrian Chadd #if 0
42531d98677SRui Paulo 	    IEEE80211_C_BGSCAN |	/* Background scan. */
4264914fa0fSAdrian Chadd #endif
42731d98677SRui Paulo 	    IEEE80211_C_SHPREAMBLE |	/* Short preamble supported. */
42831d98677SRui Paulo 	    IEEE80211_C_SHSLOT |	/* Short slot time supported. */
42931d98677SRui Paulo 	    IEEE80211_C_WPA;		/* WPA/RSN. */
43031d98677SRui Paulo 
43131d98677SRui Paulo #if 0
43231d98677SRui Paulo 	/* Check if HT support is present. */
43331d98677SRui Paulo 	if (usb_lookup(rsu_devs_noht, uaa->vendor, uaa->product) == NULL) {
43431d98677SRui Paulo 		/* Set HT capabilities. */
43531d98677SRui Paulo 		ic->ic_htcaps =
43631d98677SRui Paulo 		    IEEE80211_HTCAP_CBW20_40 |
43731d98677SRui Paulo 		    IEEE80211_HTCAP_DSSSCCK40;
43831d98677SRui Paulo 		/* Set supported HT rates. */
43931d98677SRui Paulo 		for (i = 0; i < 2; i++)
44031d98677SRui Paulo 			ic->ic_sup_mcs[i] = 0xff;
44131d98677SRui Paulo 	}
44231d98677SRui Paulo #endif
44331d98677SRui Paulo 
44431d98677SRui Paulo 	/* Set supported .11b and .11g rates. */
44531d98677SRui Paulo 	bands = 0;
44631d98677SRui Paulo 	setbit(&bands, IEEE80211_MODE_11B);
44731d98677SRui Paulo 	setbit(&bands, IEEE80211_MODE_11G);
44831d98677SRui Paulo 	ieee80211_init_channels(ic, NULL, &bands);
44931d98677SRui Paulo 
4507a79cebfSGleb Smirnoff 	ieee80211_ifattach(ic);
45131d98677SRui Paulo 	ic->ic_raw_xmit = rsu_raw_xmit;
45231d98677SRui Paulo 	ic->ic_scan_start = rsu_scan_start;
45331d98677SRui Paulo 	ic->ic_scan_end = rsu_scan_end;
45431d98677SRui Paulo 	ic->ic_set_channel = rsu_set_channel;
45531d98677SRui Paulo 	ic->ic_vap_create = rsu_vap_create;
45631d98677SRui Paulo 	ic->ic_vap_delete = rsu_vap_delete;
45731d98677SRui Paulo 	ic->ic_update_mcast = rsu_update_mcast;
4587a79cebfSGleb Smirnoff 	ic->ic_parent = rsu_parent;
4597a79cebfSGleb Smirnoff 	ic->ic_transmit = rsu_transmit;
460cf52bbe0SAdrian Chadd 	ic->ic_send_mgmt = rsu_send_mgmt;
461882704d0SAdrian Chadd 	ic->ic_update_chw = rsu_update_chw;
462882704d0SAdrian Chadd 	ic->ic_ampdu_enable = rsu_ampdu_enable;
463882704d0SAdrian Chadd 	ic->ic_wme.wme_update = rsu_wme_update;
46431d98677SRui Paulo 
46531d98677SRui Paulo 	ieee80211_radiotap_attach(ic, &sc->sc_txtap.wt_ihdr,
46631d98677SRui Paulo 	    sizeof(sc->sc_txtap), RSU_TX_RADIOTAP_PRESENT,
46731d98677SRui Paulo 	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
46831d98677SRui Paulo 	    RSU_RX_RADIOTAP_PRESENT);
46931d98677SRui Paulo 
47031d98677SRui Paulo 	if (bootverbose)
47131d98677SRui Paulo 		ieee80211_announce(ic);
47231d98677SRui Paulo 
47331d98677SRui Paulo 	return (0);
47431d98677SRui Paulo 
47553dfd5c1SRui Paulo fail_rom:
47653dfd5c1SRui Paulo 	usbd_transfer_unsetup(sc->sc_xfer, RSU_N_TRANSFER);
47753dfd5c1SRui Paulo fail_usb:
47853dfd5c1SRui Paulo 	mtx_destroy(&sc->sc_mtx);
47931d98677SRui Paulo 	return (ENXIO);
48031d98677SRui Paulo }
48131d98677SRui Paulo 
48231d98677SRui Paulo static int
48331d98677SRui Paulo rsu_detach(device_t self)
48431d98677SRui Paulo {
48531d98677SRui Paulo 	struct rsu_softc *sc = device_get_softc(self);
4867a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
48731d98677SRui Paulo 
4887a79cebfSGleb Smirnoff 	RSU_LOCK(sc);
4897a79cebfSGleb Smirnoff 	rsu_stop(sc);
4907a79cebfSGleb Smirnoff 	RSU_UNLOCK(sc);
49131d98677SRui Paulo 	usbd_transfer_unsetup(sc->sc_xfer, RSU_N_TRANSFER);
49231d98677SRui Paulo 	ieee80211_ifdetach(ic);
49331d98677SRui Paulo 
49431d98677SRui Paulo 	taskqueue_drain_timeout(taskqueue_thread, &sc->calib_task);
49531d98677SRui Paulo 
49631d98677SRui Paulo 	/* Free Tx/Rx buffers. */
49731d98677SRui Paulo 	rsu_free_tx_list(sc);
49831d98677SRui Paulo 	rsu_free_rx_list(sc);
49931d98677SRui Paulo 
5007a79cebfSGleb Smirnoff 	mbufq_drain(&sc->sc_snd);
50131d98677SRui Paulo 	mtx_destroy(&sc->sc_mtx);
50231d98677SRui Paulo 
50331d98677SRui Paulo 	return (0);
50431d98677SRui Paulo }
50531d98677SRui Paulo 
50631d98677SRui Paulo static usb_error_t
50731d98677SRui Paulo rsu_do_request(struct rsu_softc *sc, struct usb_device_request *req,
50831d98677SRui Paulo     void *data)
50931d98677SRui Paulo {
51031d98677SRui Paulo 	usb_error_t err;
51131d98677SRui Paulo 	int ntries = 10;
51231d98677SRui Paulo 
51331d98677SRui Paulo 	RSU_ASSERT_LOCKED(sc);
51431d98677SRui Paulo 
51531d98677SRui Paulo 	while (ntries--) {
51631d98677SRui Paulo 		err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
51731d98677SRui Paulo 		    req, data, 0, NULL, 250 /* ms */);
5187243077cSHans Petter Selasky 		if (err == 0 || err == USB_ERR_NOT_CONFIGURED)
51931d98677SRui Paulo 			break;
52031d98677SRui Paulo 		DPRINTFN(1, "Control request failed, %s (retrying)\n",
52131d98677SRui Paulo 		    usbd_errstr(err));
52217ebf553SAdrian Chadd 		rsu_ms_delay(sc, 10);
52331d98677SRui Paulo         }
52431d98677SRui Paulo 
52531d98677SRui Paulo         return (err);
52631d98677SRui Paulo }
52731d98677SRui Paulo 
52831d98677SRui Paulo static struct ieee80211vap *
52931d98677SRui Paulo rsu_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
53031d98677SRui Paulo     enum ieee80211_opmode opmode, int flags,
53131d98677SRui Paulo     const uint8_t bssid[IEEE80211_ADDR_LEN],
53231d98677SRui Paulo     const uint8_t mac[IEEE80211_ADDR_LEN])
53331d98677SRui Paulo {
53431d98677SRui Paulo 	struct rsu_vap *uvp;
53531d98677SRui Paulo 	struct ieee80211vap *vap;
53631d98677SRui Paulo 
53731d98677SRui Paulo 	if (!TAILQ_EMPTY(&ic->ic_vaps))         /* only one at a time */
53831d98677SRui Paulo 		return (NULL);
53931d98677SRui Paulo 
5407a79cebfSGleb Smirnoff 	uvp =  malloc(sizeof(struct rsu_vap), M_80211_VAP, M_WAITOK | M_ZERO);
54131d98677SRui Paulo 	vap = &uvp->vap;
542bb2f69e8SHans Petter Selasky 
543bb2f69e8SHans Petter Selasky 	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
5447a79cebfSGleb Smirnoff 	    flags, bssid) != 0) {
545bb2f69e8SHans Petter Selasky 		/* out of memory */
546bb2f69e8SHans Petter Selasky 		free(uvp, M_80211_VAP);
547bb2f69e8SHans Petter Selasky 		return (NULL);
548bb2f69e8SHans Petter Selasky 	}
54931d98677SRui Paulo 
55031d98677SRui Paulo 	/* override state transition machine */
55131d98677SRui Paulo 	uvp->newstate = vap->iv_newstate;
55231d98677SRui Paulo 	vap->iv_newstate = rsu_newstate;
55331d98677SRui Paulo 
55431d98677SRui Paulo 	/* complete setup */
55531d98677SRui Paulo 	ieee80211_vap_attach(vap, ieee80211_media_change,
5567a79cebfSGleb Smirnoff 	    ieee80211_media_status, mac);
55731d98677SRui Paulo 	ic->ic_opmode = opmode;
55831d98677SRui Paulo 
55931d98677SRui Paulo 	return (vap);
56031d98677SRui Paulo }
56131d98677SRui Paulo 
56231d98677SRui Paulo static void
56331d98677SRui Paulo rsu_vap_delete(struct ieee80211vap *vap)
56431d98677SRui Paulo {
56531d98677SRui Paulo 	struct rsu_vap *uvp = RSU_VAP(vap);
56631d98677SRui Paulo 
56731d98677SRui Paulo 	ieee80211_vap_detach(vap);
56831d98677SRui Paulo 	free(uvp, M_80211_VAP);
56931d98677SRui Paulo }
57031d98677SRui Paulo 
57131d98677SRui Paulo static void
57231d98677SRui Paulo rsu_scan_start(struct ieee80211com *ic)
57331d98677SRui Paulo {
574d3fdd08cSAdrian Chadd 	struct rsu_softc *sc = ic->ic_softc;
5757a79cebfSGleb Smirnoff 	int error;
57631d98677SRui Paulo 
57731d98677SRui Paulo 	/* Scanning is done by the firmware. */
57831d98677SRui Paulo 	RSU_LOCK(sc);
57931d98677SRui Paulo 	error = rsu_site_survey(sc, TAILQ_FIRST(&ic->ic_vaps));
58031d98677SRui Paulo 	RSU_UNLOCK(sc);
58131d98677SRui Paulo 	if (error != 0)
58231d98677SRui Paulo 		device_printf(sc->sc_dev,
58331d98677SRui Paulo 		    "could not send site survey command\n");
58431d98677SRui Paulo }
58531d98677SRui Paulo 
58631d98677SRui Paulo static void
58731d98677SRui Paulo rsu_scan_end(struct ieee80211com *ic)
58831d98677SRui Paulo {
58931d98677SRui Paulo 	/* Nothing to do here. */
59031d98677SRui Paulo }
59131d98677SRui Paulo 
59231d98677SRui Paulo static void
59331d98677SRui Paulo rsu_set_channel(struct ieee80211com *ic __unused)
59431d98677SRui Paulo {
59531d98677SRui Paulo 	/* We are unable to switch channels, yet. */
59631d98677SRui Paulo }
59731d98677SRui Paulo 
59831d98677SRui Paulo static void
599272f6adeSGleb Smirnoff rsu_update_mcast(struct ieee80211com *ic)
60031d98677SRui Paulo {
60131d98677SRui Paulo         /* XXX do nothing?  */
60231d98677SRui Paulo }
60331d98677SRui Paulo 
60431d98677SRui Paulo static int
60531d98677SRui Paulo rsu_alloc_list(struct rsu_softc *sc, struct rsu_data data[],
60631d98677SRui Paulo     int ndata, int maxsz)
60731d98677SRui Paulo {
60831d98677SRui Paulo 	int i, error;
60931d98677SRui Paulo 
61031d98677SRui Paulo 	for (i = 0; i < ndata; i++) {
61131d98677SRui Paulo 		struct rsu_data *dp = &data[i];
61231d98677SRui Paulo 		dp->sc = sc;
61331d98677SRui Paulo 		dp->m = NULL;
61431d98677SRui Paulo 		dp->buf = malloc(maxsz, M_USBDEV, M_NOWAIT);
61531d98677SRui Paulo 		if (dp->buf == NULL) {
61631d98677SRui Paulo 			device_printf(sc->sc_dev,
61731d98677SRui Paulo 			    "could not allocate buffer\n");
61831d98677SRui Paulo 			error = ENOMEM;
61931d98677SRui Paulo 			goto fail;
62031d98677SRui Paulo 		}
62131d98677SRui Paulo 		dp->ni = NULL;
62231d98677SRui Paulo 	}
62331d98677SRui Paulo 
62431d98677SRui Paulo 	return (0);
62531d98677SRui Paulo fail:
62631d98677SRui Paulo 	rsu_free_list(sc, data, ndata);
62731d98677SRui Paulo 	return (error);
62831d98677SRui Paulo }
62931d98677SRui Paulo 
63031d98677SRui Paulo static int
63131d98677SRui Paulo rsu_alloc_rx_list(struct rsu_softc *sc)
63231d98677SRui Paulo {
63331d98677SRui Paulo         int error, i;
63431d98677SRui Paulo 
63531d98677SRui Paulo 	error = rsu_alloc_list(sc, sc->sc_rx, RSU_RX_LIST_COUNT,
63631d98677SRui Paulo 	    RSU_RXBUFSZ);
63731d98677SRui Paulo 	if (error != 0)
63831d98677SRui Paulo 		return (error);
63931d98677SRui Paulo 
64031d98677SRui Paulo 	STAILQ_INIT(&sc->sc_rx_active);
64131d98677SRui Paulo 	STAILQ_INIT(&sc->sc_rx_inactive);
64231d98677SRui Paulo 
64331d98677SRui Paulo 	for (i = 0; i < RSU_RX_LIST_COUNT; i++)
64431d98677SRui Paulo 		STAILQ_INSERT_HEAD(&sc->sc_rx_inactive, &sc->sc_rx[i], next);
64531d98677SRui Paulo 
64631d98677SRui Paulo 	return (0);
64731d98677SRui Paulo }
64831d98677SRui Paulo 
64931d98677SRui Paulo static int
65031d98677SRui Paulo rsu_alloc_tx_list(struct rsu_softc *sc)
65131d98677SRui Paulo {
65231d98677SRui Paulo 	int error, i;
65331d98677SRui Paulo 
65431d98677SRui Paulo 	error = rsu_alloc_list(sc, sc->sc_tx, RSU_TX_LIST_COUNT,
65531d98677SRui Paulo 	    RSU_TXBUFSZ);
65631d98677SRui Paulo 	if (error != 0)
65731d98677SRui Paulo 		return (error);
65831d98677SRui Paulo 
65931d98677SRui Paulo 	STAILQ_INIT(&sc->sc_tx_inactive);
660400b4e53SHans Petter Selasky 
661910593b5SHans Petter Selasky 	for (i = 0; i != RSU_N_TRANSFER; i++) {
662400b4e53SHans Petter Selasky 		STAILQ_INIT(&sc->sc_tx_active[i]);
663400b4e53SHans Petter Selasky 		STAILQ_INIT(&sc->sc_tx_pending[i]);
664400b4e53SHans Petter Selasky 	}
66531d98677SRui Paulo 
66631d98677SRui Paulo 	for (i = 0; i < RSU_TX_LIST_COUNT; i++) {
66731d98677SRui Paulo 		STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, &sc->sc_tx[i], next);
66831d98677SRui Paulo 	}
66931d98677SRui Paulo 
67031d98677SRui Paulo 	return (0);
67131d98677SRui Paulo }
67231d98677SRui Paulo 
67331d98677SRui Paulo static void
67431d98677SRui Paulo rsu_free_tx_list(struct rsu_softc *sc)
67531d98677SRui Paulo {
676885476cbSHans Petter Selasky 	int i;
677885476cbSHans Petter Selasky 
678885476cbSHans Petter Selasky 	/* prevent further allocations from TX list(s) */
679885476cbSHans Petter Selasky 	STAILQ_INIT(&sc->sc_tx_inactive);
680885476cbSHans Petter Selasky 
681910593b5SHans Petter Selasky 	for (i = 0; i != RSU_N_TRANSFER; i++) {
682885476cbSHans Petter Selasky 		STAILQ_INIT(&sc->sc_tx_active[i]);
683885476cbSHans Petter Selasky 		STAILQ_INIT(&sc->sc_tx_pending[i]);
684885476cbSHans Petter Selasky 	}
685885476cbSHans Petter Selasky 
68631d98677SRui Paulo 	rsu_free_list(sc, sc->sc_tx, RSU_TX_LIST_COUNT);
68731d98677SRui Paulo }
68831d98677SRui Paulo 
68931d98677SRui Paulo static void
69031d98677SRui Paulo rsu_free_rx_list(struct rsu_softc *sc)
69131d98677SRui Paulo {
692885476cbSHans Petter Selasky 	/* prevent further allocations from RX list(s) */
693885476cbSHans Petter Selasky 	STAILQ_INIT(&sc->sc_rx_inactive);
694885476cbSHans Petter Selasky 	STAILQ_INIT(&sc->sc_rx_active);
695885476cbSHans Petter Selasky 
69631d98677SRui Paulo 	rsu_free_list(sc, sc->sc_rx, RSU_RX_LIST_COUNT);
69731d98677SRui Paulo }
69831d98677SRui Paulo 
69931d98677SRui Paulo static void
70031d98677SRui Paulo rsu_free_list(struct rsu_softc *sc, struct rsu_data data[], int ndata)
70131d98677SRui Paulo {
70231d98677SRui Paulo 	int i;
70331d98677SRui Paulo 
70431d98677SRui Paulo 	for (i = 0; i < ndata; i++) {
70531d98677SRui Paulo 		struct rsu_data *dp = &data[i];
70631d98677SRui Paulo 
70731d98677SRui Paulo 		if (dp->buf != NULL) {
70831d98677SRui Paulo 			free(dp->buf, M_USBDEV);
70931d98677SRui Paulo 			dp->buf = NULL;
71031d98677SRui Paulo 		}
71131d98677SRui Paulo 		if (dp->ni != NULL) {
71231d98677SRui Paulo 			ieee80211_free_node(dp->ni);
71331d98677SRui Paulo 			dp->ni = NULL;
71431d98677SRui Paulo 		}
71531d98677SRui Paulo 	}
71631d98677SRui Paulo }
71731d98677SRui Paulo 
71831d98677SRui Paulo static struct rsu_data *
71931d98677SRui Paulo _rsu_getbuf(struct rsu_softc *sc)
72031d98677SRui Paulo {
72131d98677SRui Paulo 	struct rsu_data *bf;
72231d98677SRui Paulo 
72331d98677SRui Paulo 	bf = STAILQ_FIRST(&sc->sc_tx_inactive);
72431d98677SRui Paulo 	if (bf != NULL)
72531d98677SRui Paulo 		STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next);
72631d98677SRui Paulo 	else
72731d98677SRui Paulo 		bf = NULL;
72831d98677SRui Paulo 	if (bf == NULL)
72931d98677SRui Paulo 		DPRINTF("out of xmit buffers\n");
73031d98677SRui Paulo         return (bf);
73131d98677SRui Paulo }
73231d98677SRui Paulo 
73331d98677SRui Paulo static struct rsu_data *
73431d98677SRui Paulo rsu_getbuf(struct rsu_softc *sc)
73531d98677SRui Paulo {
73631d98677SRui Paulo 	struct rsu_data *bf;
73731d98677SRui Paulo 
73831d98677SRui Paulo 	RSU_ASSERT_LOCKED(sc);
73931d98677SRui Paulo 
74031d98677SRui Paulo 	bf = _rsu_getbuf(sc);
7417a79cebfSGleb Smirnoff 	if (bf == NULL)
74231d98677SRui Paulo 		DPRINTF("stop queue\n");
74331d98677SRui Paulo 	return (bf);
74431d98677SRui Paulo }
74531d98677SRui Paulo 
74631d98677SRui Paulo static int
74731d98677SRui Paulo rsu_write_region_1(struct rsu_softc *sc, uint16_t addr, uint8_t *buf,
74831d98677SRui Paulo     int len)
74931d98677SRui Paulo {
75031d98677SRui Paulo 	usb_device_request_t req;
75131d98677SRui Paulo 
75231d98677SRui Paulo 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
75331d98677SRui Paulo 	req.bRequest = R92S_REQ_REGS;
75431d98677SRui Paulo 	USETW(req.wValue, addr);
75531d98677SRui Paulo 	USETW(req.wIndex, 0);
75631d98677SRui Paulo 	USETW(req.wLength, len);
75731d98677SRui Paulo 
75831d98677SRui Paulo 	return (rsu_do_request(sc, &req, buf));
75931d98677SRui Paulo }
76031d98677SRui Paulo 
76131d98677SRui Paulo static void
76231d98677SRui Paulo rsu_write_1(struct rsu_softc *sc, uint16_t addr, uint8_t val)
76331d98677SRui Paulo {
76431d98677SRui Paulo 	rsu_write_region_1(sc, addr, &val, 1);
76531d98677SRui Paulo }
76631d98677SRui Paulo 
76731d98677SRui Paulo static void
76831d98677SRui Paulo rsu_write_2(struct rsu_softc *sc, uint16_t addr, uint16_t val)
76931d98677SRui Paulo {
77031d98677SRui Paulo 	val = htole16(val);
77131d98677SRui Paulo 	rsu_write_region_1(sc, addr, (uint8_t *)&val, 2);
77231d98677SRui Paulo }
77331d98677SRui Paulo 
77431d98677SRui Paulo static void
77531d98677SRui Paulo rsu_write_4(struct rsu_softc *sc, uint16_t addr, uint32_t val)
77631d98677SRui Paulo {
77731d98677SRui Paulo 	val = htole32(val);
77831d98677SRui Paulo 	rsu_write_region_1(sc, addr, (uint8_t *)&val, 4);
77931d98677SRui Paulo }
78031d98677SRui Paulo 
78131d98677SRui Paulo static int
78231d98677SRui Paulo rsu_read_region_1(struct rsu_softc *sc, uint16_t addr, uint8_t *buf,
78331d98677SRui Paulo     int len)
78431d98677SRui Paulo {
78531d98677SRui Paulo 	usb_device_request_t req;
78631d98677SRui Paulo 
78731d98677SRui Paulo 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
78831d98677SRui Paulo 	req.bRequest = R92S_REQ_REGS;
78931d98677SRui Paulo 	USETW(req.wValue, addr);
79031d98677SRui Paulo 	USETW(req.wIndex, 0);
79131d98677SRui Paulo 	USETW(req.wLength, len);
79231d98677SRui Paulo 
79331d98677SRui Paulo 	return (rsu_do_request(sc, &req, buf));
79431d98677SRui Paulo }
79531d98677SRui Paulo 
79631d98677SRui Paulo static uint8_t
79731d98677SRui Paulo rsu_read_1(struct rsu_softc *sc, uint16_t addr)
79831d98677SRui Paulo {
79931d98677SRui Paulo 	uint8_t val;
80031d98677SRui Paulo 
80131d98677SRui Paulo 	if (rsu_read_region_1(sc, addr, &val, 1) != 0)
80231d98677SRui Paulo 		return (0xff);
80331d98677SRui Paulo 	return (val);
80431d98677SRui Paulo }
80531d98677SRui Paulo 
80631d98677SRui Paulo static uint16_t
80731d98677SRui Paulo rsu_read_2(struct rsu_softc *sc, uint16_t addr)
80831d98677SRui Paulo {
80931d98677SRui Paulo 	uint16_t val;
81031d98677SRui Paulo 
81131d98677SRui Paulo 	if (rsu_read_region_1(sc, addr, (uint8_t *)&val, 2) != 0)
81231d98677SRui Paulo 		return (0xffff);
81331d98677SRui Paulo 	return (le16toh(val));
81431d98677SRui Paulo }
81531d98677SRui Paulo 
81631d98677SRui Paulo static uint32_t
81731d98677SRui Paulo rsu_read_4(struct rsu_softc *sc, uint16_t addr)
81831d98677SRui Paulo {
81931d98677SRui Paulo 	uint32_t val;
82031d98677SRui Paulo 
82131d98677SRui Paulo 	if (rsu_read_region_1(sc, addr, (uint8_t *)&val, 4) != 0)
82231d98677SRui Paulo 		return (0xffffffff);
82331d98677SRui Paulo 	return (le32toh(val));
82431d98677SRui Paulo }
82531d98677SRui Paulo 
82631d98677SRui Paulo static int
82731d98677SRui Paulo rsu_fw_iocmd(struct rsu_softc *sc, uint32_t iocmd)
82831d98677SRui Paulo {
82931d98677SRui Paulo 	int ntries;
83031d98677SRui Paulo 
83131d98677SRui Paulo 	rsu_write_4(sc, R92S_IOCMD_CTRL, iocmd);
83217ebf553SAdrian Chadd 	rsu_ms_delay(sc, 1);
83331d98677SRui Paulo 	for (ntries = 0; ntries < 50; ntries++) {
83431d98677SRui Paulo 		if (rsu_read_4(sc, R92S_IOCMD_CTRL) == 0)
83531d98677SRui Paulo 			return (0);
83617ebf553SAdrian Chadd 		rsu_ms_delay(sc, 1);
83731d98677SRui Paulo 	}
83831d98677SRui Paulo 	return (ETIMEDOUT);
83931d98677SRui Paulo }
84031d98677SRui Paulo 
84131d98677SRui Paulo static uint8_t
84231d98677SRui Paulo rsu_efuse_read_1(struct rsu_softc *sc, uint16_t addr)
84331d98677SRui Paulo {
84431d98677SRui Paulo 	uint32_t reg;
84531d98677SRui Paulo 	int ntries;
84631d98677SRui Paulo 
84731d98677SRui Paulo 	reg = rsu_read_4(sc, R92S_EFUSE_CTRL);
84831d98677SRui Paulo 	reg = RW(reg, R92S_EFUSE_CTRL_ADDR, addr);
84931d98677SRui Paulo 	reg &= ~R92S_EFUSE_CTRL_VALID;
85031d98677SRui Paulo 	rsu_write_4(sc, R92S_EFUSE_CTRL, reg);
85131d98677SRui Paulo 	/* Wait for read operation to complete. */
85231d98677SRui Paulo 	for (ntries = 0; ntries < 100; ntries++) {
85331d98677SRui Paulo 		reg = rsu_read_4(sc, R92S_EFUSE_CTRL);
85431d98677SRui Paulo 		if (reg & R92S_EFUSE_CTRL_VALID)
85531d98677SRui Paulo 			return (MS(reg, R92S_EFUSE_CTRL_DATA));
85617ebf553SAdrian Chadd 		rsu_ms_delay(sc, 1);
85731d98677SRui Paulo 	}
85831d98677SRui Paulo 	device_printf(sc->sc_dev,
85931d98677SRui Paulo 	    "could not read efuse byte at address 0x%x\n", addr);
86031d98677SRui Paulo 	return (0xff);
86131d98677SRui Paulo }
86231d98677SRui Paulo 
86331d98677SRui Paulo static int
86431d98677SRui Paulo rsu_read_rom(struct rsu_softc *sc)
86531d98677SRui Paulo {
86631d98677SRui Paulo 	uint8_t *rom = sc->rom;
86731d98677SRui Paulo 	uint16_t addr = 0;
86831d98677SRui Paulo 	uint32_t reg;
86931d98677SRui Paulo 	uint8_t off, msk;
87031d98677SRui Paulo 	int i;
87131d98677SRui Paulo 
87231d98677SRui Paulo 	/* Make sure that ROM type is eFuse and that autoload succeeded. */
87331d98677SRui Paulo 	reg = rsu_read_1(sc, R92S_EE_9346CR);
87431d98677SRui Paulo 	if ((reg & (R92S_9356SEL | R92S_EEPROM_EN)) != R92S_EEPROM_EN)
87531d98677SRui Paulo 		return (EIO);
87631d98677SRui Paulo 
87731d98677SRui Paulo 	/* Turn on 2.5V to prevent eFuse leakage. */
87831d98677SRui Paulo 	reg = rsu_read_1(sc, R92S_EFUSE_TEST + 3);
87931d98677SRui Paulo 	rsu_write_1(sc, R92S_EFUSE_TEST + 3, reg | 0x80);
88017ebf553SAdrian Chadd 	rsu_ms_delay(sc, 1);
88131d98677SRui Paulo 	rsu_write_1(sc, R92S_EFUSE_TEST + 3, reg & ~0x80);
88231d98677SRui Paulo 
88331d98677SRui Paulo 	/* Read full ROM image. */
88431d98677SRui Paulo 	memset(&sc->rom, 0xff, sizeof(sc->rom));
88531d98677SRui Paulo 	while (addr < 512) {
88631d98677SRui Paulo 		reg = rsu_efuse_read_1(sc, addr);
88731d98677SRui Paulo 		if (reg == 0xff)
88831d98677SRui Paulo 			break;
88931d98677SRui Paulo 		addr++;
89031d98677SRui Paulo 		off = reg >> 4;
89131d98677SRui Paulo 		msk = reg & 0xf;
89231d98677SRui Paulo 		for (i = 0; i < 4; i++) {
89331d98677SRui Paulo 			if (msk & (1 << i))
89431d98677SRui Paulo 				continue;
89531d98677SRui Paulo 			rom[off * 8 + i * 2 + 0] =
89631d98677SRui Paulo 			    rsu_efuse_read_1(sc, addr);
89731d98677SRui Paulo 			addr++;
89831d98677SRui Paulo 			rom[off * 8 + i * 2 + 1] =
89931d98677SRui Paulo 			    rsu_efuse_read_1(sc, addr);
90031d98677SRui Paulo 			addr++;
90131d98677SRui Paulo 		}
90231d98677SRui Paulo 	}
90331d98677SRui Paulo #ifdef USB_DEBUG
90431d98677SRui Paulo 	if (rsu_debug >= 5) {
90531d98677SRui Paulo 		/* Dump ROM content. */
90631d98677SRui Paulo 		printf("\n");
90731d98677SRui Paulo 		for (i = 0; i < sizeof(sc->rom); i++)
90831d98677SRui Paulo 			printf("%02x:", rom[i]);
90931d98677SRui Paulo 		printf("\n");
91031d98677SRui Paulo 	}
91131d98677SRui Paulo #endif
91231d98677SRui Paulo 	return (0);
91331d98677SRui Paulo }
91431d98677SRui Paulo 
91531d98677SRui Paulo static int
91631d98677SRui Paulo rsu_fw_cmd(struct rsu_softc *sc, uint8_t code, void *buf, int len)
91731d98677SRui Paulo {
918bc6a9865SAdrian Chadd 	const uint8_t which = RSU_H2C_ENDPOINT;
91931d98677SRui Paulo 	struct rsu_data *data;
92031d98677SRui Paulo 	struct r92s_tx_desc *txd;
92131d98677SRui Paulo 	struct r92s_fw_cmd_hdr *cmd;
922400b4e53SHans Petter Selasky 	int cmdsz;
923400b4e53SHans Petter Selasky 	int xferlen;
92431d98677SRui Paulo 
92531d98677SRui Paulo 	data = rsu_getbuf(sc);
92631d98677SRui Paulo 	if (data == NULL)
92731d98677SRui Paulo 		return (ENOMEM);
92831d98677SRui Paulo 
92931d98677SRui Paulo 	/* Round-up command length to a multiple of 8 bytes. */
93031d98677SRui Paulo 	cmdsz = (len + 7) & ~7;
93131d98677SRui Paulo 
93231d98677SRui Paulo 	xferlen = sizeof(*txd) + sizeof(*cmd) + cmdsz;
93331d98677SRui Paulo 	KASSERT(xferlen <= RSU_TXBUFSZ, ("%s: invalid length", __func__));
93431d98677SRui Paulo 	memset(data->buf, 0, xferlen);
93531d98677SRui Paulo 
93631d98677SRui Paulo 	/* Setup Tx descriptor. */
93731d98677SRui Paulo 	txd = (struct r92s_tx_desc *)data->buf;
93831d98677SRui Paulo 	txd->txdw0 = htole32(
93931d98677SRui Paulo 	    SM(R92S_TXDW0_OFFSET, sizeof(*txd)) |
94031d98677SRui Paulo 	    SM(R92S_TXDW0_PKTLEN, sizeof(*cmd) + cmdsz) |
94131d98677SRui Paulo 	    R92S_TXDW0_OWN | R92S_TXDW0_FSG | R92S_TXDW0_LSG);
94231d98677SRui Paulo 	txd->txdw1 = htole32(SM(R92S_TXDW1_QSEL, R92S_TXDW1_QSEL_H2C));
94331d98677SRui Paulo 
94431d98677SRui Paulo 	/* Setup command header. */
94531d98677SRui Paulo 	cmd = (struct r92s_fw_cmd_hdr *)&txd[1];
94631d98677SRui Paulo 	cmd->len = htole16(cmdsz);
94731d98677SRui Paulo 	cmd->code = code;
94831d98677SRui Paulo 	cmd->seq = sc->cmd_seq;
94931d98677SRui Paulo 	sc->cmd_seq = (sc->cmd_seq + 1) & 0x7f;
95031d98677SRui Paulo 
95131d98677SRui Paulo 	/* Copy command payload. */
95231d98677SRui Paulo 	memcpy(&cmd[1], buf, len);
95331d98677SRui Paulo 
954e6d258f6SAdrian Chadd 	RSU_DPRINTF(sc, RSU_DEBUG_TX | RSU_DEBUG_FWCMD,
9554914fa0fSAdrian Chadd 	    "%s: Tx cmd code=0x%x len=0x%x\n",
9564914fa0fSAdrian Chadd 	    __func__, code, cmdsz);
95731d98677SRui Paulo 	data->buflen = xferlen;
958400b4e53SHans Petter Selasky 	STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next);
959910593b5SHans Petter Selasky 	usbd_transfer_start(sc->sc_xfer[which]);
96031d98677SRui Paulo 
96131d98677SRui Paulo 	return (0);
96231d98677SRui Paulo }
96331d98677SRui Paulo 
96431d98677SRui Paulo /* ARGSUSED */
96531d98677SRui Paulo static void
96631d98677SRui Paulo rsu_calib_task(void *arg, int pending __unused)
96731d98677SRui Paulo {
96831d98677SRui Paulo 	struct rsu_softc *sc = arg;
96931d98677SRui Paulo 	uint32_t reg;
97031d98677SRui Paulo 
9714914fa0fSAdrian Chadd 	RSU_DPRINTF(sc, RSU_DEBUG_CALIB, "%s: running calibration task\n",
9724914fa0fSAdrian Chadd 	    __func__);
973910593b5SHans Petter Selasky 
97431d98677SRui Paulo 	RSU_LOCK(sc);
97531d98677SRui Paulo #ifdef notyet
97631d98677SRui Paulo 	/* Read WPS PBC status. */
97731d98677SRui Paulo 	rsu_write_1(sc, R92S_MAC_PINMUX_CTRL,
97831d98677SRui Paulo 	    R92S_GPIOMUX_EN | SM(R92S_GPIOSEL_GPIO, R92S_GPIOSEL_GPIO_JTAG));
97931d98677SRui Paulo 	rsu_write_1(sc, R92S_GPIO_IO_SEL,
98031d98677SRui Paulo 	    rsu_read_1(sc, R92S_GPIO_IO_SEL) & ~R92S_GPIO_WPS);
98131d98677SRui Paulo 	reg = rsu_read_1(sc, R92S_GPIO_CTRL);
98231d98677SRui Paulo 	if (reg != 0xff && (reg & R92S_GPIO_WPS))
98331d98677SRui Paulo 		DPRINTF(("WPS PBC is pushed\n"));
98431d98677SRui Paulo #endif
98531d98677SRui Paulo 	/* Read current signal level. */
98631d98677SRui Paulo 	if (rsu_fw_iocmd(sc, 0xf4000001) == 0) {
98731d98677SRui Paulo 		reg = rsu_read_4(sc, R92S_IOCMD_DATA);
9884914fa0fSAdrian Chadd 		RSU_DPRINTF(sc, RSU_DEBUG_CALIB, "%s: RSSI=%d%%\n",
9894914fa0fSAdrian Chadd 		    __func__, reg >> 4);
99031d98677SRui Paulo 	}
991910593b5SHans Petter Selasky 	if (sc->sc_calibrating)
992910593b5SHans Petter Selasky 		taskqueue_enqueue_timeout(taskqueue_thread, &sc->calib_task, hz);
99331d98677SRui Paulo 	RSU_UNLOCK(sc);
99431d98677SRui Paulo }
99531d98677SRui Paulo 
99631d98677SRui Paulo static int
99731d98677SRui Paulo rsu_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
99831d98677SRui Paulo {
99931d98677SRui Paulo 	struct rsu_vap *uvp = RSU_VAP(vap);
100031d98677SRui Paulo 	struct ieee80211com *ic = vap->iv_ic;
1001d3fdd08cSAdrian Chadd 	struct rsu_softc *sc = ic->ic_softc;
100231d98677SRui Paulo 	struct ieee80211_node *ni;
100331d98677SRui Paulo 	struct ieee80211_rateset *rs;
100431d98677SRui Paulo 	enum ieee80211_state ostate;
100531d98677SRui Paulo 	int error, startcal = 0;
100631d98677SRui Paulo 
100731d98677SRui Paulo 	ostate = vap->iv_state;
10084914fa0fSAdrian Chadd 	RSU_DPRINTF(sc, RSU_DEBUG_STATE, "%s: %s -> %s\n",
10094914fa0fSAdrian Chadd 	    __func__,
10104914fa0fSAdrian Chadd 	    ieee80211_state_name[ostate],
101131d98677SRui Paulo 	    ieee80211_state_name[nstate]);
101231d98677SRui Paulo 
101331d98677SRui Paulo 	IEEE80211_UNLOCK(ic);
101431d98677SRui Paulo 	if (ostate == IEEE80211_S_RUN) {
101531d98677SRui Paulo 		RSU_LOCK(sc);
101631d98677SRui Paulo 		/* Stop calibration. */
101731d98677SRui Paulo 		sc->sc_calibrating = 0;
101831d98677SRui Paulo 		RSU_UNLOCK(sc);
101931d98677SRui Paulo 		taskqueue_drain_timeout(taskqueue_thread, &sc->calib_task);
102031d98677SRui Paulo 		/* Disassociate from our current BSS. */
102131d98677SRui Paulo 		RSU_LOCK(sc);
102231d98677SRui Paulo 		rsu_disconnect(sc);
102331d98677SRui Paulo 	} else
102431d98677SRui Paulo 		RSU_LOCK(sc);
102531d98677SRui Paulo 	switch (nstate) {
102631d98677SRui Paulo 	case IEEE80211_S_INIT:
102731d98677SRui Paulo 		break;
102831d98677SRui Paulo 	case IEEE80211_S_AUTH:
102931d98677SRui Paulo 		ni = ieee80211_ref_node(vap->iv_bss);
103031d98677SRui Paulo 		error = rsu_join_bss(sc, ni);
103131d98677SRui Paulo 		ieee80211_free_node(ni);
103231d98677SRui Paulo 		if (error != 0) {
103331d98677SRui Paulo 			device_printf(sc->sc_dev,
103431d98677SRui Paulo 			    "could not send join command\n");
103531d98677SRui Paulo 		}
103631d98677SRui Paulo 		break;
103731d98677SRui Paulo 	case IEEE80211_S_RUN:
103831d98677SRui Paulo 		ni = ieee80211_ref_node(vap->iv_bss);
103931d98677SRui Paulo 		rs = &ni->ni_rates;
104031d98677SRui Paulo 		/* Indicate highest supported rate. */
104131d98677SRui Paulo 		ni->ni_txrate = rs->rs_rates[rs->rs_nrates - 1];
104231d98677SRui Paulo 		ieee80211_free_node(ni);
104331d98677SRui Paulo 		startcal = 1;
104431d98677SRui Paulo 		break;
104531d98677SRui Paulo 	default:
104631d98677SRui Paulo 		break;
104731d98677SRui Paulo 	}
104831d98677SRui Paulo 	sc->sc_calibrating = 1;
1049910593b5SHans Petter Selasky 	/* Start periodic calibration. */
1050910593b5SHans Petter Selasky 	taskqueue_enqueue_timeout(taskqueue_thread, &sc->calib_task, hz);
105131d98677SRui Paulo 	RSU_UNLOCK(sc);
105231d98677SRui Paulo 	IEEE80211_LOCK(ic);
105331d98677SRui Paulo 	return (uvp->newstate(vap, nstate, arg));
105431d98677SRui Paulo }
105531d98677SRui Paulo 
105631d98677SRui Paulo #ifdef notyet
105731d98677SRui Paulo static void
105831d98677SRui Paulo rsu_set_key(struct rsu_softc *sc, const struct ieee80211_key *k)
105931d98677SRui Paulo {
106031d98677SRui Paulo 	struct r92s_fw_cmd_set_key key;
106131d98677SRui Paulo 
106231d98677SRui Paulo 	memset(&key, 0, sizeof(key));
106331d98677SRui Paulo 	/* Map net80211 cipher to HW crypto algorithm. */
106431d98677SRui Paulo 	switch (k->wk_cipher->ic_cipher) {
106531d98677SRui Paulo 	case IEEE80211_CIPHER_WEP:
106631d98677SRui Paulo 		if (k->wk_keylen < 8)
106731d98677SRui Paulo 			key.algo = R92S_KEY_ALGO_WEP40;
106831d98677SRui Paulo 		else
106931d98677SRui Paulo 			key.algo = R92S_KEY_ALGO_WEP104;
107031d98677SRui Paulo 		break;
107131d98677SRui Paulo 	case IEEE80211_CIPHER_TKIP:
107231d98677SRui Paulo 		key.algo = R92S_KEY_ALGO_TKIP;
107331d98677SRui Paulo 		break;
107431d98677SRui Paulo 	case IEEE80211_CIPHER_AES_CCM:
107531d98677SRui Paulo 		key.algo = R92S_KEY_ALGO_AES;
107631d98677SRui Paulo 		break;
107731d98677SRui Paulo 	default:
107831d98677SRui Paulo 		return;
107931d98677SRui Paulo 	}
108031d98677SRui Paulo 	key.id = k->wk_keyix;
108131d98677SRui Paulo 	key.grpkey = (k->wk_flags & IEEE80211_KEY_GROUP) != 0;
108231d98677SRui Paulo 	memcpy(key.key, k->wk_key, MIN(k->wk_keylen, sizeof(key.key)));
108331d98677SRui Paulo 	(void)rsu_fw_cmd(sc, R92S_CMD_SET_KEY, &key, sizeof(key));
108431d98677SRui Paulo }
108531d98677SRui Paulo 
108631d98677SRui Paulo static void
108731d98677SRui Paulo rsu_delete_key(struct rsu_softc *sc, const struct ieee80211_key *k)
108831d98677SRui Paulo {
108931d98677SRui Paulo 	struct r92s_fw_cmd_set_key key;
109031d98677SRui Paulo 
109131d98677SRui Paulo 	memset(&key, 0, sizeof(key));
109231d98677SRui Paulo 	key.id = k->wk_keyix;
109331d98677SRui Paulo 	(void)rsu_fw_cmd(sc, R92S_CMD_SET_KEY, &key, sizeof(key));
109431d98677SRui Paulo }
109531d98677SRui Paulo #endif
109631d98677SRui Paulo 
109731d98677SRui Paulo static int
109831d98677SRui Paulo rsu_site_survey(struct rsu_softc *sc, struct ieee80211vap *vap)
109931d98677SRui Paulo {
110031d98677SRui Paulo 	struct r92s_fw_cmd_sitesurvey cmd;
11017a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
110231d98677SRui Paulo 
110331d98677SRui Paulo 	memset(&cmd, 0, sizeof(cmd));
11047a79cebfSGleb Smirnoff 	if ((ic->ic_flags & IEEE80211_F_ASCAN) || sc->sc_scan_pass == 1)
110531d98677SRui Paulo 		cmd.active = htole32(1);
110631d98677SRui Paulo 	cmd.limit = htole32(48);
11077a79cebfSGleb Smirnoff 	if (sc->sc_scan_pass == 1 && vap->iv_des_nssid > 0) {
110831d98677SRui Paulo 		/* Do a directed scan for second pass. */
110931d98677SRui Paulo 		cmd.ssidlen = htole32(vap->iv_des_ssid[0].len);
111031d98677SRui Paulo 		memcpy(cmd.ssid, vap->iv_des_ssid[0].ssid,
111131d98677SRui Paulo 		    vap->iv_des_ssid[0].len);
111231d98677SRui Paulo 
111331d98677SRui Paulo 	}
11147a79cebfSGleb Smirnoff 	DPRINTF("sending site survey command, pass=%d\n", sc->sc_scan_pass);
111531d98677SRui Paulo 	return (rsu_fw_cmd(sc, R92S_CMD_SITE_SURVEY, &cmd, sizeof(cmd)));
111631d98677SRui Paulo }
111731d98677SRui Paulo 
111831d98677SRui Paulo static int
111931d98677SRui Paulo rsu_join_bss(struct rsu_softc *sc, struct ieee80211_node *ni)
112031d98677SRui Paulo {
11217a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
112231d98677SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
112331d98677SRui Paulo 	struct ndis_wlan_bssid_ex *bss;
112431d98677SRui Paulo 	struct ndis_802_11_fixed_ies *fixed;
112531d98677SRui Paulo 	struct r92s_fw_cmd_auth auth;
1126bb03cd6fSHans Petter Selasky 	uint8_t buf[sizeof(*bss) + 128] __aligned(4);
1127bb03cd6fSHans Petter Selasky 	uint8_t *frm;
112831d98677SRui Paulo 	uint8_t opmode;
112931d98677SRui Paulo 	int error;
113031d98677SRui Paulo 
113131d98677SRui Paulo 	/* Let the FW decide the opmode based on the capinfo field. */
113231d98677SRui Paulo 	opmode = NDIS802_11AUTOUNKNOWN;
11334914fa0fSAdrian Chadd 	RSU_DPRINTF(sc, RSU_DEBUG_RESET,
11344914fa0fSAdrian Chadd 	    "%s: setting operating mode to %d\n",
11354914fa0fSAdrian Chadd 	    __func__, opmode);
113631d98677SRui Paulo 	error = rsu_fw_cmd(sc, R92S_CMD_SET_OPMODE, &opmode, sizeof(opmode));
113731d98677SRui Paulo 	if (error != 0)
113831d98677SRui Paulo 		return (error);
113931d98677SRui Paulo 
114031d98677SRui Paulo 	memset(&auth, 0, sizeof(auth));
114131d98677SRui Paulo 	if (vap->iv_flags & IEEE80211_F_WPA) {
114231d98677SRui Paulo 		auth.mode = R92S_AUTHMODE_WPA;
1143bb03cd6fSHans Petter Selasky 		auth.dot1x = (ni->ni_authmode == IEEE80211_AUTH_8021X);
114431d98677SRui Paulo 	} else
114531d98677SRui Paulo 		auth.mode = R92S_AUTHMODE_OPEN;
11464914fa0fSAdrian Chadd 	RSU_DPRINTF(sc, RSU_DEBUG_RESET,
11474914fa0fSAdrian Chadd 	    "%s: setting auth mode to %d\n",
11484914fa0fSAdrian Chadd 	    __func__, auth.mode);
114931d98677SRui Paulo 	error = rsu_fw_cmd(sc, R92S_CMD_SET_AUTH, &auth, sizeof(auth));
115031d98677SRui Paulo 	if (error != 0)
115131d98677SRui Paulo 		return (error);
115231d98677SRui Paulo 
115331d98677SRui Paulo 	memset(buf, 0, sizeof(buf));
115431d98677SRui Paulo 	bss = (struct ndis_wlan_bssid_ex *)buf;
115531d98677SRui Paulo 	IEEE80211_ADDR_COPY(bss->macaddr, ni->ni_bssid);
115631d98677SRui Paulo 	bss->ssid.ssidlen = htole32(ni->ni_esslen);
115731d98677SRui Paulo 	memcpy(bss->ssid.ssid, ni->ni_essid, ni->ni_esslen);
115831d98677SRui Paulo 	if (vap->iv_flags & (IEEE80211_F_PRIVACY | IEEE80211_F_WPA))
115931d98677SRui Paulo 		bss->privacy = htole32(1);
116031d98677SRui Paulo 	bss->rssi = htole32(ni->ni_avgrssi);
116131d98677SRui Paulo 	if (ic->ic_curmode == IEEE80211_MODE_11B)
116231d98677SRui Paulo 		bss->networktype = htole32(NDIS802_11DS);
116331d98677SRui Paulo 	else
116431d98677SRui Paulo 		bss->networktype = htole32(NDIS802_11OFDM24);
116531d98677SRui Paulo 	bss->config.len = htole32(sizeof(bss->config));
116631d98677SRui Paulo 	bss->config.bintval = htole32(ni->ni_intval);
116731d98677SRui Paulo 	bss->config.dsconfig = htole32(ieee80211_chan2ieee(ic, ni->ni_chan));
116831d98677SRui Paulo 	bss->inframode = htole32(NDIS802_11INFRASTRUCTURE);
11694914fa0fSAdrian Chadd 	/* XXX verify how this is supposed to look! */
117031d98677SRui Paulo 	memcpy(bss->supprates, ni->ni_rates.rs_rates,
117131d98677SRui Paulo 	    ni->ni_rates.rs_nrates);
117231d98677SRui Paulo 	/* Write the fixed fields of the beacon frame. */
117331d98677SRui Paulo 	fixed = (struct ndis_802_11_fixed_ies *)&bss[1];
117431d98677SRui Paulo 	memcpy(&fixed->tstamp, ni->ni_tstamp.data, 8);
117531d98677SRui Paulo 	fixed->bintval = htole16(ni->ni_intval);
117631d98677SRui Paulo 	fixed->capabilities = htole16(ni->ni_capinfo);
117731d98677SRui Paulo 	/* Write IEs to be included in the association request. */
117831d98677SRui Paulo 	frm = (uint8_t *)&fixed[1];
117931d98677SRui Paulo 	frm = ieee80211_add_rsn(frm, vap);
118031d98677SRui Paulo 	frm = ieee80211_add_wpa(frm, vap);
118131d98677SRui Paulo 	frm = ieee80211_add_qos(frm, ni);
118231d98677SRui Paulo 	if (ni->ni_flags & IEEE80211_NODE_HT)
118331d98677SRui Paulo 		frm = ieee80211_add_htcap(frm, ni);
118431d98677SRui Paulo 	bss->ieslen = htole32(frm - (uint8_t *)fixed);
118531d98677SRui Paulo 	bss->len = htole32(((frm - buf) + 3) & ~3);
11864914fa0fSAdrian Chadd 	RSU_DPRINTF(sc, RSU_DEBUG_RESET | RSU_DEBUG_FWCMD,
11874914fa0fSAdrian Chadd 	    "%s: sending join bss command to %s chan %d\n",
11884914fa0fSAdrian Chadd 	    __func__,
118931d98677SRui Paulo 	    ether_sprintf(bss->macaddr), le32toh(bss->config.dsconfig));
119031d98677SRui Paulo 	return (rsu_fw_cmd(sc, R92S_CMD_JOIN_BSS, buf, sizeof(buf)));
119131d98677SRui Paulo }
119231d98677SRui Paulo 
119331d98677SRui Paulo static int
119431d98677SRui Paulo rsu_disconnect(struct rsu_softc *sc)
119531d98677SRui Paulo {
119631d98677SRui Paulo 	uint32_t zero = 0;	/* :-) */
119731d98677SRui Paulo 
119831d98677SRui Paulo 	/* Disassociate from our current BSS. */
11994914fa0fSAdrian Chadd 	RSU_DPRINTF(sc, RSU_DEBUG_STATE | RSU_DEBUG_FWCMD,
12004914fa0fSAdrian Chadd 	    "%s: sending disconnect command\n", __func__);
120131d98677SRui Paulo 	return (rsu_fw_cmd(sc, R92S_CMD_DISCONNECT, &zero, sizeof(zero)));
120231d98677SRui Paulo }
120331d98677SRui Paulo 
120431d98677SRui Paulo static void
120531d98677SRui Paulo rsu_event_survey(struct rsu_softc *sc, uint8_t *buf, int len)
120631d98677SRui Paulo {
12077a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
120831d98677SRui Paulo 	struct ieee80211_frame *wh;
120931d98677SRui Paulo 	struct ndis_wlan_bssid_ex *bss;
121089fd8237SAdrian Chadd 	struct ieee80211_rx_stats rxs;
121131d98677SRui Paulo 	struct mbuf *m;
121231d98677SRui Paulo 	int pktlen;
121331d98677SRui Paulo 
121431d98677SRui Paulo 	if (__predict_false(len < sizeof(*bss)))
121531d98677SRui Paulo 		return;
121631d98677SRui Paulo 	bss = (struct ndis_wlan_bssid_ex *)buf;
121731d98677SRui Paulo 	if (__predict_false(len < sizeof(*bss) + le32toh(bss->ieslen)))
121831d98677SRui Paulo 		return;
121931d98677SRui Paulo 
12204914fa0fSAdrian Chadd 	RSU_DPRINTF(sc, RSU_DEBUG_SCAN,
12214914fa0fSAdrian Chadd 	    "%s: found BSS %s: len=%d chan=%d inframode=%d "
1222a934198bSAdrian Chadd 	    "networktype=%d privacy=%d, RSSI=%d\n",
12234914fa0fSAdrian Chadd 	    __func__,
122431d98677SRui Paulo 	    ether_sprintf(bss->macaddr), le32toh(bss->len),
122531d98677SRui Paulo 	    le32toh(bss->config.dsconfig), le32toh(bss->inframode),
1226a934198bSAdrian Chadd 	    le32toh(bss->networktype), le32toh(bss->privacy),
1227a934198bSAdrian Chadd 	    le32toh(bss->rssi));
122831d98677SRui Paulo 
122931d98677SRui Paulo 	/* Build a fake beacon frame to let net80211 do all the parsing. */
12304914fa0fSAdrian Chadd 	/* XXX TODO: just call the new scan API methods! */
123131d98677SRui Paulo 	pktlen = sizeof(*wh) + le32toh(bss->ieslen);
123231d98677SRui Paulo 	if (__predict_false(pktlen > MCLBYTES))
123331d98677SRui Paulo 		return;
1234107f00c0SKevin Lo 	m = m_get2(pktlen, M_NOWAIT, MT_DATA, M_PKTHDR);
123531d98677SRui Paulo 	if (__predict_false(m == NULL))
123631d98677SRui Paulo 		return;
123731d98677SRui Paulo 	wh = mtod(m, struct ieee80211_frame *);
123831d98677SRui Paulo 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
123931d98677SRui Paulo 	    IEEE80211_FC0_SUBTYPE_BEACON;
124031d98677SRui Paulo 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
12418cfe5440SHans Petter Selasky 	USETW(wh->i_dur, 0);
12427a79cebfSGleb Smirnoff 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
124331d98677SRui Paulo 	IEEE80211_ADDR_COPY(wh->i_addr2, bss->macaddr);
124431d98677SRui Paulo 	IEEE80211_ADDR_COPY(wh->i_addr3, bss->macaddr);
124531d98677SRui Paulo 	*(uint16_t *)wh->i_seq = 0;
124631d98677SRui Paulo 	memcpy(&wh[1], (uint8_t *)&bss[1], le32toh(bss->ieslen));
124731d98677SRui Paulo 
124831d98677SRui Paulo 	/* Finalize mbuf. */
124931d98677SRui Paulo 	m->m_pkthdr.len = m->m_len = pktlen;
125089fd8237SAdrian Chadd 
125189fd8237SAdrian Chadd 	/* Set channel flags for input path */
125289fd8237SAdrian Chadd 	bzero(&rxs, sizeof(rxs));
125389fd8237SAdrian Chadd 	rxs.r_flags |= IEEE80211_R_IEEE | IEEE80211_R_FREQ;
125489fd8237SAdrian Chadd 	rxs.r_flags |= IEEE80211_R_NF | IEEE80211_R_RSSI;
125589fd8237SAdrian Chadd 	rxs.c_ieee = le32toh(bss->config.dsconfig);
125689fd8237SAdrian Chadd 	rxs.c_freq = ieee80211_ieee2mhz(rxs.c_ieee, IEEE80211_CHAN_2GHZ);
125789fd8237SAdrian Chadd 	rxs.rssi = le32toh(bss->rssi);
125889fd8237SAdrian Chadd 	rxs.nf = 0; /* XXX */
125989fd8237SAdrian Chadd 
126031d98677SRui Paulo 	/* XXX avoid a LOR */
126131d98677SRui Paulo 	RSU_UNLOCK(sc);
126289fd8237SAdrian Chadd 	ieee80211_input_mimo_all(ic, m, &rxs);
126331d98677SRui Paulo 	RSU_LOCK(sc);
126431d98677SRui Paulo }
126531d98677SRui Paulo 
126631d98677SRui Paulo static void
126731d98677SRui Paulo rsu_event_join_bss(struct rsu_softc *sc, uint8_t *buf, int len)
126831d98677SRui Paulo {
12697a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
127031d98677SRui Paulo 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
127131d98677SRui Paulo 	struct ieee80211_node *ni = vap->iv_bss;
127231d98677SRui Paulo 	struct r92s_event_join_bss *rsp;
1273bb03cd6fSHans Petter Selasky 	uint32_t tmp;
127431d98677SRui Paulo 	int res;
127531d98677SRui Paulo 
127631d98677SRui Paulo 	if (__predict_false(len < sizeof(*rsp)))
127731d98677SRui Paulo 		return;
127831d98677SRui Paulo 	rsp = (struct r92s_event_join_bss *)buf;
127931d98677SRui Paulo 	res = (int)le32toh(rsp->join_res);
128031d98677SRui Paulo 
1281e6d258f6SAdrian Chadd 	RSU_DPRINTF(sc, RSU_DEBUG_STATE | RSU_DEBUG_FWCMD,
1282e6d258f6SAdrian Chadd 	    "%s: Rx join BSS event len=%d res=%d\n",
1283e6d258f6SAdrian Chadd 	    __func__, len, res);
128431d98677SRui Paulo 	if (res <= 0) {
128531d98677SRui Paulo 		RSU_UNLOCK(sc);
128631d98677SRui Paulo 		ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
128731d98677SRui Paulo 		RSU_LOCK(sc);
128831d98677SRui Paulo 		return;
128931d98677SRui Paulo 	}
1290bb03cd6fSHans Petter Selasky 	tmp = le32toh(rsp->associd);
1291bb03cd6fSHans Petter Selasky 	if (tmp >= vap->iv_max_aid) {
1292bb03cd6fSHans Petter Selasky 		DPRINTF("Assoc ID overflow\n");
1293bb03cd6fSHans Petter Selasky 		tmp = 1;
1294bb03cd6fSHans Petter Selasky 	}
1295e6d258f6SAdrian Chadd 	RSU_DPRINTF(sc, RSU_DEBUG_STATE | RSU_DEBUG_FWCMD,
1296e6d258f6SAdrian Chadd 	    "%s: associated with %s associd=%d\n",
1297e6d258f6SAdrian Chadd 	    __func__, ether_sprintf(rsp->bss.macaddr), tmp);
1298e6d258f6SAdrian Chadd 	/* XXX is this required? What's the top two bits for again? */
1299bb03cd6fSHans Petter Selasky 	ni->ni_associd = tmp | 0xc000;
130031d98677SRui Paulo 	RSU_UNLOCK(sc);
130131d98677SRui Paulo 	ieee80211_new_state(vap, IEEE80211_S_RUN,
130231d98677SRui Paulo 	    IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
130331d98677SRui Paulo 	RSU_LOCK(sc);
130431d98677SRui Paulo }
130531d98677SRui Paulo 
130631d98677SRui Paulo static void
130731d98677SRui Paulo rsu_rx_event(struct rsu_softc *sc, uint8_t code, uint8_t *buf, int len)
130831d98677SRui Paulo {
13097a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
131031d98677SRui Paulo 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
131131d98677SRui Paulo 
1312e6d258f6SAdrian Chadd 	RSU_DPRINTF(sc, RSU_DEBUG_RX | RSU_DEBUG_FWCMD,
13134914fa0fSAdrian Chadd 	    "%s: Rx event code=%d len=%d\n", __func__, code, len);
131431d98677SRui Paulo 	switch (code) {
131531d98677SRui Paulo 	case R92S_EVT_SURVEY:
131631d98677SRui Paulo 		if (vap->iv_state == IEEE80211_S_SCAN)
131731d98677SRui Paulo 			rsu_event_survey(sc, buf, len);
131831d98677SRui Paulo 		break;
131931d98677SRui Paulo 	case R92S_EVT_SURVEY_DONE:
13204914fa0fSAdrian Chadd 		RSU_DPRINTF(sc, RSU_DEBUG_SCAN,
13214914fa0fSAdrian Chadd 		    "%s: site survey pass %d done, found %d BSS\n",
13224914fa0fSAdrian Chadd 		    __func__, sc->sc_scan_pass, le32toh(*(uint32_t *)buf));
132331d98677SRui Paulo 		if (vap->iv_state != IEEE80211_S_SCAN)
132431d98677SRui Paulo 			break;	/* Ignore if not scanning. */
13257a79cebfSGleb Smirnoff 		if (sc->sc_scan_pass == 0 && vap->iv_des_nssid != 0) {
132631d98677SRui Paulo 			/* Schedule a directed scan for hidden APs. */
13277a79cebfSGleb Smirnoff 			sc->sc_scan_pass = 1;
132831d98677SRui Paulo 			RSU_UNLOCK(sc);
132931d98677SRui Paulo 			ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
133031d98677SRui Paulo 			RSU_LOCK(sc);
133131d98677SRui Paulo 			break;
133231d98677SRui Paulo 		}
13337a79cebfSGleb Smirnoff 		sc->sc_scan_pass = 0;
133431d98677SRui Paulo 		break;
133531d98677SRui Paulo 	case R92S_EVT_JOIN_BSS:
133631d98677SRui Paulo 		if (vap->iv_state == IEEE80211_S_AUTH)
133731d98677SRui Paulo 			rsu_event_join_bss(sc, buf, len);
133831d98677SRui Paulo 		break;
133931d98677SRui Paulo 	case R92S_EVT_DEL_STA:
1340e6d258f6SAdrian Chadd 		RSU_DPRINTF(sc, RSU_DEBUG_FWCMD | RSU_DEBUG_STATE,
1341e6d258f6SAdrian Chadd 		    "%s: disassociated from %s\n", __func__,
1342e6d258f6SAdrian Chadd 		    ether_sprintf(buf));
134331d98677SRui Paulo 		if (vap->iv_state == IEEE80211_S_RUN &&
134431d98677SRui Paulo 		    IEEE80211_ADDR_EQ(vap->iv_bss->ni_bssid, buf)) {
134531d98677SRui Paulo 			RSU_UNLOCK(sc);
134631d98677SRui Paulo 			ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
134731d98677SRui Paulo 			RSU_LOCK(sc);
134831d98677SRui Paulo 		}
134931d98677SRui Paulo 		break;
135031d98677SRui Paulo 	case R92S_EVT_WPS_PBC:
13514914fa0fSAdrian Chadd 		RSU_DPRINTF(sc, RSU_DEBUG_RX | RSU_DEBUG_FWCMD,
13524914fa0fSAdrian Chadd 		    "%s: WPS PBC pushed.\n", __func__);
135331d98677SRui Paulo 		break;
135431d98677SRui Paulo 	case R92S_EVT_FWDBG:
135531d98677SRui Paulo 		buf[60] = '\0';
13564914fa0fSAdrian Chadd 		RSU_DPRINTF(sc, RSU_DEBUG_FWDBG, "FWDBG: %s\n", (char *)buf);
135731d98677SRui Paulo 		break;
1358910593b5SHans Petter Selasky 	default:
13594914fa0fSAdrian Chadd 		RSU_DPRINTF(sc, RSU_DEBUG_ANY, "%s: unhandled code (%d)\n",
13604914fa0fSAdrian Chadd 		    __func__, code);
1361910593b5SHans Petter Selasky 		break;
136231d98677SRui Paulo 	}
136331d98677SRui Paulo }
136431d98677SRui Paulo 
136531d98677SRui Paulo static void
136631d98677SRui Paulo rsu_rx_multi_event(struct rsu_softc *sc, uint8_t *buf, int len)
136731d98677SRui Paulo {
136831d98677SRui Paulo 	struct r92s_fw_cmd_hdr *cmd;
136931d98677SRui Paulo 	int cmdsz;
137031d98677SRui Paulo 
13714914fa0fSAdrian Chadd 	RSU_DPRINTF(sc, RSU_DEBUG_RX, "%s: Rx events len=%d\n", __func__, len);
137231d98677SRui Paulo 
137331d98677SRui Paulo 	/* Skip Rx status. */
137431d98677SRui Paulo 	buf += sizeof(struct r92s_rx_stat);
137531d98677SRui Paulo 	len -= sizeof(struct r92s_rx_stat);
137631d98677SRui Paulo 
137731d98677SRui Paulo 	/* Process all events. */
137831d98677SRui Paulo 	for (;;) {
137931d98677SRui Paulo 		/* Check that command header fits. */
138031d98677SRui Paulo 		if (__predict_false(len < sizeof(*cmd)))
138131d98677SRui Paulo 			break;
138231d98677SRui Paulo 		cmd = (struct r92s_fw_cmd_hdr *)buf;
138331d98677SRui Paulo 		/* Check that command payload fits. */
138431d98677SRui Paulo 		cmdsz = le16toh(cmd->len);
138531d98677SRui Paulo 		if (__predict_false(len < sizeof(*cmd) + cmdsz))
138631d98677SRui Paulo 			break;
138731d98677SRui Paulo 
138831d98677SRui Paulo 		/* Process firmware event. */
138931d98677SRui Paulo 		rsu_rx_event(sc, cmd->code, (uint8_t *)&cmd[1], cmdsz);
139031d98677SRui Paulo 
139131d98677SRui Paulo 		if (!(cmd->seq & R92S_FW_CMD_MORE))
139231d98677SRui Paulo 			break;
139331d98677SRui Paulo 		buf += sizeof(*cmd) + cmdsz;
139431d98677SRui Paulo 		len -= sizeof(*cmd) + cmdsz;
139531d98677SRui Paulo 	}
139631d98677SRui Paulo }
139731d98677SRui Paulo 
139831d98677SRui Paulo static int8_t
139931d98677SRui Paulo rsu_get_rssi(struct rsu_softc *sc, int rate, void *physt)
140031d98677SRui Paulo {
140131d98677SRui Paulo 	static const int8_t cckoff[] = { 14, -2, -20, -40 };
140231d98677SRui Paulo 	struct r92s_rx_phystat *phy;
140331d98677SRui Paulo 	struct r92s_rx_cck *cck;
140431d98677SRui Paulo 	uint8_t rpt;
140531d98677SRui Paulo 	int8_t rssi;
140631d98677SRui Paulo 
140731d98677SRui Paulo 	if (rate <= 3) {
140831d98677SRui Paulo 		cck = (struct r92s_rx_cck *)physt;
140931d98677SRui Paulo 		rpt = (cck->agc_rpt >> 6) & 0x3;
141031d98677SRui Paulo 		rssi = cck->agc_rpt & 0x3e;
141131d98677SRui Paulo 		rssi = cckoff[rpt] - rssi;
141231d98677SRui Paulo 	} else {	/* OFDM/HT. */
141331d98677SRui Paulo 		phy = (struct r92s_rx_phystat *)physt;
141431d98677SRui Paulo 		rssi = ((le32toh(phy->phydw1) >> 1) & 0x7f) - 106;
141531d98677SRui Paulo 	}
141631d98677SRui Paulo 	return (rssi);
141731d98677SRui Paulo }
141831d98677SRui Paulo 
141931d98677SRui Paulo static struct mbuf *
142031d98677SRui Paulo rsu_rx_frame(struct rsu_softc *sc, uint8_t *buf, int pktlen, int *rssi)
142131d98677SRui Paulo {
14227a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
142331d98677SRui Paulo 	struct ieee80211_frame *wh;
142431d98677SRui Paulo 	struct r92s_rx_stat *stat;
142531d98677SRui Paulo 	uint32_t rxdw0, rxdw3;
142631d98677SRui Paulo 	struct mbuf *m;
142731d98677SRui Paulo 	uint8_t rate;
142831d98677SRui Paulo 	int infosz;
142931d98677SRui Paulo 
143031d98677SRui Paulo 	stat = (struct r92s_rx_stat *)buf;
143131d98677SRui Paulo 	rxdw0 = le32toh(stat->rxdw0);
143231d98677SRui Paulo 	rxdw3 = le32toh(stat->rxdw3);
143331d98677SRui Paulo 
143431d98677SRui Paulo 	if (__predict_false(rxdw0 & R92S_RXDW0_CRCERR)) {
14357a79cebfSGleb Smirnoff 		counter_u64_add(ic->ic_ierrors, 1);
143631d98677SRui Paulo 		return NULL;
143731d98677SRui Paulo 	}
143831d98677SRui Paulo 	if (__predict_false(pktlen < sizeof(*wh) || pktlen > MCLBYTES)) {
14397a79cebfSGleb Smirnoff 		counter_u64_add(ic->ic_ierrors, 1);
144031d98677SRui Paulo 		return NULL;
144131d98677SRui Paulo 	}
144231d98677SRui Paulo 
144331d98677SRui Paulo 	rate = MS(rxdw3, R92S_RXDW3_RATE);
144431d98677SRui Paulo 	infosz = MS(rxdw0, R92S_RXDW0_INFOSZ) * 8;
144531d98677SRui Paulo 
144631d98677SRui Paulo 	/* Get RSSI from PHY status descriptor if present. */
144731d98677SRui Paulo 	if (infosz != 0)
144831d98677SRui Paulo 		*rssi = rsu_get_rssi(sc, rate, &stat[1]);
144931d98677SRui Paulo 	else
145031d98677SRui Paulo 		*rssi = 0;
145131d98677SRui Paulo 
14524914fa0fSAdrian Chadd 	RSU_DPRINTF(sc, RSU_DEBUG_RX,
14534914fa0fSAdrian Chadd 	    "%s: Rx frame len=%d rate=%d infosz=%d rssi=%d\n",
14544914fa0fSAdrian Chadd 	    __func__,
145531d98677SRui Paulo 	    pktlen, rate, infosz, *rssi);
145631d98677SRui Paulo 
1457107f00c0SKevin Lo 	m = m_get2(pktlen, M_NOWAIT, MT_DATA, M_PKTHDR);
145831d98677SRui Paulo 	if (__predict_false(m == NULL)) {
14597a79cebfSGleb Smirnoff 		counter_u64_add(ic->ic_ierrors, 1);
146031d98677SRui Paulo 		return NULL;
146131d98677SRui Paulo 	}
146231d98677SRui Paulo 	/* Hardware does Rx TCP checksum offload. */
146331d98677SRui Paulo 	if (rxdw3 & R92S_RXDW3_TCPCHKVALID) {
146431d98677SRui Paulo 		if (__predict_true(rxdw3 & R92S_RXDW3_TCPCHKRPT))
146531d98677SRui Paulo 			m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
146631d98677SRui Paulo 	}
146731d98677SRui Paulo 	wh = (struct ieee80211_frame *)((uint8_t *)&stat[1] + infosz);
146831d98677SRui Paulo 	memcpy(mtod(m, uint8_t *), wh, pktlen);
146931d98677SRui Paulo 	m->m_pkthdr.len = m->m_len = pktlen;
147031d98677SRui Paulo 
147131d98677SRui Paulo 	if (ieee80211_radiotap_active(ic)) {
147231d98677SRui Paulo 		struct rsu_rx_radiotap_header *tap = &sc->sc_rxtap;
147331d98677SRui Paulo 
147431d98677SRui Paulo 		/* Map HW rate index to 802.11 rate. */
147531d98677SRui Paulo 		tap->wr_flags = 2;
147631d98677SRui Paulo 		if (!(rxdw3 & R92S_RXDW3_HTC)) {
147731d98677SRui Paulo 			switch (rate) {
147831d98677SRui Paulo 			/* CCK. */
147931d98677SRui Paulo 			case  0: tap->wr_rate =   2; break;
148031d98677SRui Paulo 			case  1: tap->wr_rate =   4; break;
148131d98677SRui Paulo 			case  2: tap->wr_rate =  11; break;
148231d98677SRui Paulo 			case  3: tap->wr_rate =  22; break;
148331d98677SRui Paulo 			/* OFDM. */
148431d98677SRui Paulo 			case  4: tap->wr_rate =  12; break;
148531d98677SRui Paulo 			case  5: tap->wr_rate =  18; break;
148631d98677SRui Paulo 			case  6: tap->wr_rate =  24; break;
148731d98677SRui Paulo 			case  7: tap->wr_rate =  36; break;
148831d98677SRui Paulo 			case  8: tap->wr_rate =  48; break;
148931d98677SRui Paulo 			case  9: tap->wr_rate =  72; break;
149031d98677SRui Paulo 			case 10: tap->wr_rate =  96; break;
149131d98677SRui Paulo 			case 11: tap->wr_rate = 108; break;
149231d98677SRui Paulo 			}
149331d98677SRui Paulo 		} else if (rate >= 12) {	/* MCS0~15. */
149431d98677SRui Paulo 			/* Bit 7 set means HT MCS instead of rate. */
149531d98677SRui Paulo 			tap->wr_rate = 0x80 | (rate - 12);
149631d98677SRui Paulo 		}
149731d98677SRui Paulo 		tap->wr_dbm_antsignal = *rssi;
149831d98677SRui Paulo 		tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
149931d98677SRui Paulo 		tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
150031d98677SRui Paulo 	}
150131d98677SRui Paulo 
150231d98677SRui Paulo 	return (m);
150331d98677SRui Paulo }
150431d98677SRui Paulo 
150531d98677SRui Paulo static struct mbuf *
150631d98677SRui Paulo rsu_rx_multi_frame(struct rsu_softc *sc, uint8_t *buf, int len, int *rssi)
150731d98677SRui Paulo {
150831d98677SRui Paulo 	struct r92s_rx_stat *stat;
150931d98677SRui Paulo 	uint32_t rxdw0;
151031d98677SRui Paulo 	int totlen, pktlen, infosz, npkts;
151131d98677SRui Paulo 	struct mbuf *m, *m0 = NULL, *prevm = NULL;
151231d98677SRui Paulo 
151331d98677SRui Paulo 	/* Get the number of encapsulated frames. */
151431d98677SRui Paulo 	stat = (struct r92s_rx_stat *)buf;
151531d98677SRui Paulo 	npkts = MS(le32toh(stat->rxdw2), R92S_RXDW2_PKTCNT);
15164914fa0fSAdrian Chadd 	RSU_DPRINTF(sc, RSU_DEBUG_RX,
15174914fa0fSAdrian Chadd 	    "%s: Rx %d frames in one chunk\n", __func__, npkts);
151831d98677SRui Paulo 
151931d98677SRui Paulo 	/* Process all of them. */
152031d98677SRui Paulo 	while (npkts-- > 0) {
152131d98677SRui Paulo 		if (__predict_false(len < sizeof(*stat)))
152231d98677SRui Paulo 			break;
152331d98677SRui Paulo 		stat = (struct r92s_rx_stat *)buf;
152431d98677SRui Paulo 		rxdw0 = le32toh(stat->rxdw0);
152531d98677SRui Paulo 
152631d98677SRui Paulo 		pktlen = MS(rxdw0, R92S_RXDW0_PKTLEN);
152731d98677SRui Paulo 		if (__predict_false(pktlen == 0))
152831d98677SRui Paulo 			break;
152931d98677SRui Paulo 
153031d98677SRui Paulo 		infosz = MS(rxdw0, R92S_RXDW0_INFOSZ) * 8;
153131d98677SRui Paulo 
153231d98677SRui Paulo 		/* Make sure everything fits in xfer. */
153331d98677SRui Paulo 		totlen = sizeof(*stat) + infosz + pktlen;
153431d98677SRui Paulo 		if (__predict_false(totlen > len))
153531d98677SRui Paulo 			break;
153631d98677SRui Paulo 
153731d98677SRui Paulo 		/* Process 802.11 frame. */
153831d98677SRui Paulo 		m = rsu_rx_frame(sc, buf, pktlen, rssi);
153931d98677SRui Paulo 		if (m0 == NULL)
154031d98677SRui Paulo 			m0 = m;
154131d98677SRui Paulo 		if (prevm == NULL)
154231d98677SRui Paulo 			prevm = m;
154331d98677SRui Paulo 		else {
154431d98677SRui Paulo 			prevm->m_next = m;
154531d98677SRui Paulo 			prevm = m;
154631d98677SRui Paulo 		}
154731d98677SRui Paulo 		/* Next chunk is 128-byte aligned. */
154831d98677SRui Paulo 		totlen = (totlen + 127) & ~127;
154931d98677SRui Paulo 		buf += totlen;
155031d98677SRui Paulo 		len -= totlen;
155131d98677SRui Paulo 	}
155231d98677SRui Paulo 
155331d98677SRui Paulo 	return (m0);
155431d98677SRui Paulo }
155531d98677SRui Paulo 
155631d98677SRui Paulo static struct mbuf *
155731d98677SRui Paulo rsu_rxeof(struct usb_xfer *xfer, struct rsu_data *data, int *rssi)
155831d98677SRui Paulo {
155931d98677SRui Paulo 	struct rsu_softc *sc = data->sc;
15607a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
156131d98677SRui Paulo 	struct r92s_rx_stat *stat;
156231d98677SRui Paulo 	int len;
156331d98677SRui Paulo 
156431d98677SRui Paulo 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
156531d98677SRui Paulo 
156631d98677SRui Paulo 	if (__predict_false(len < sizeof(*stat))) {
156731d98677SRui Paulo 		DPRINTF("xfer too short %d\n", len);
15687a79cebfSGleb Smirnoff 		counter_u64_add(ic->ic_ierrors, 1);
156931d98677SRui Paulo 		return (NULL);
157031d98677SRui Paulo 	}
157131d98677SRui Paulo 	/* Determine if it is a firmware C2H event or an 802.11 frame. */
157231d98677SRui Paulo 	stat = (struct r92s_rx_stat *)data->buf;
157331d98677SRui Paulo 	if ((le32toh(stat->rxdw1) & 0x1ff) == 0x1ff) {
157431d98677SRui Paulo 		rsu_rx_multi_event(sc, data->buf, len);
157531d98677SRui Paulo 		/* No packets to process. */
157631d98677SRui Paulo 		return (NULL);
157731d98677SRui Paulo 	} else
157831d98677SRui Paulo 		return (rsu_rx_multi_frame(sc, data->buf, len, rssi));
157931d98677SRui Paulo }
158031d98677SRui Paulo 
158131d98677SRui Paulo static void
158231d98677SRui Paulo rsu_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
158331d98677SRui Paulo {
158431d98677SRui Paulo 	struct rsu_softc *sc = usbd_xfer_softc(xfer);
15857a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
158631d98677SRui Paulo 	struct ieee80211_frame *wh;
158731d98677SRui Paulo 	struct ieee80211_node *ni;
158831d98677SRui Paulo 	struct mbuf *m = NULL, *next;
158931d98677SRui Paulo 	struct rsu_data *data;
159031d98677SRui Paulo 	int rssi = 1;
159131d98677SRui Paulo 
159231d98677SRui Paulo 	RSU_ASSERT_LOCKED(sc);
159331d98677SRui Paulo 
159431d98677SRui Paulo 	switch (USB_GET_STATE(xfer)) {
159531d98677SRui Paulo 	case USB_ST_TRANSFERRED:
159631d98677SRui Paulo 		data = STAILQ_FIRST(&sc->sc_rx_active);
159731d98677SRui Paulo 		if (data == NULL)
159831d98677SRui Paulo 			goto tr_setup;
159931d98677SRui Paulo 		STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
160031d98677SRui Paulo 		m = rsu_rxeof(xfer, data, &rssi);
160131d98677SRui Paulo 		STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
160231d98677SRui Paulo 		/* FALLTHROUGH */
160331d98677SRui Paulo 	case USB_ST_SETUP:
160431d98677SRui Paulo tr_setup:
160531d98677SRui Paulo 		data = STAILQ_FIRST(&sc->sc_rx_inactive);
160631d98677SRui Paulo 		if (data == NULL) {
160731d98677SRui Paulo 			KASSERT(m == NULL, ("mbuf isn't NULL"));
160831d98677SRui Paulo 			return;
160931d98677SRui Paulo 		}
161031d98677SRui Paulo 		STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next);
161131d98677SRui Paulo 		STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next);
161231d98677SRui Paulo 		usbd_xfer_set_frame_data(xfer, 0, data->buf,
161331d98677SRui Paulo 		    usbd_xfer_max_len(xfer));
161431d98677SRui Paulo 		usbd_transfer_submit(xfer);
161531d98677SRui Paulo 		/*
161631d98677SRui Paulo 		 * To avoid LOR we should unlock our private mutex here to call
161731d98677SRui Paulo 		 * ieee80211_input() because here is at the end of a USB
161831d98677SRui Paulo 		 * callback and safe to unlock.
161931d98677SRui Paulo 		 */
162031d98677SRui Paulo 		RSU_UNLOCK(sc);
162131d98677SRui Paulo 		while (m != NULL) {
162231d98677SRui Paulo 			next = m->m_next;
162331d98677SRui Paulo 			m->m_next = NULL;
162431d98677SRui Paulo 			wh = mtod(m, struct ieee80211_frame *);
162531d98677SRui Paulo 			ni = ieee80211_find_rxnode(ic,
162631d98677SRui Paulo 			    (struct ieee80211_frame_min *)wh);
162731d98677SRui Paulo 			if (ni != NULL) {
162831d98677SRui Paulo 				(void)ieee80211_input(ni, m, rssi, 0);
162931d98677SRui Paulo 				ieee80211_free_node(ni);
163031d98677SRui Paulo 			} else
163131d98677SRui Paulo 				(void)ieee80211_input_all(ic, m, rssi, 0);
163231d98677SRui Paulo 			m = next;
163331d98677SRui Paulo 		}
163431d98677SRui Paulo 		RSU_LOCK(sc);
163531d98677SRui Paulo 		break;
163631d98677SRui Paulo 	default:
163731d98677SRui Paulo 		/* needs it to the inactive queue due to a error. */
163831d98677SRui Paulo 		data = STAILQ_FIRST(&sc->sc_rx_active);
163931d98677SRui Paulo 		if (data != NULL) {
164031d98677SRui Paulo 			STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
164131d98677SRui Paulo 			STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
164231d98677SRui Paulo 		}
164331d98677SRui Paulo 		if (error != USB_ERR_CANCELLED) {
164431d98677SRui Paulo 			usbd_xfer_set_stall(xfer);
16457a79cebfSGleb Smirnoff 			counter_u64_add(ic->ic_ierrors, 1);
164631d98677SRui Paulo 			goto tr_setup;
164731d98677SRui Paulo 		}
164831d98677SRui Paulo 		break;
164931d98677SRui Paulo 	}
165031d98677SRui Paulo 
165131d98677SRui Paulo }
165231d98677SRui Paulo 
165331d98677SRui Paulo static void
165431d98677SRui Paulo rsu_txeof(struct usb_xfer *xfer, struct rsu_data *data)
165531d98677SRui Paulo {
16564914fa0fSAdrian Chadd #ifdef	USB_DEBUG
16574914fa0fSAdrian Chadd 	struct rsu_softc *sc = usbd_xfer_softc(xfer);
16584914fa0fSAdrian Chadd #endif
16594914fa0fSAdrian Chadd 
16604914fa0fSAdrian Chadd 	RSU_DPRINTF(sc, RSU_DEBUG_TXDONE, "%s: called; data=%p\n",
16614914fa0fSAdrian Chadd 	    __func__,
16624914fa0fSAdrian Chadd 	    data);
166331d98677SRui Paulo 
166431d98677SRui Paulo 	if (data->m) {
166531d98677SRui Paulo 		/* XXX status? */
16667a79cebfSGleb Smirnoff 		ieee80211_tx_complete(data->ni, data->m, 0);
166731d98677SRui Paulo 		data->m = NULL;
166831d98677SRui Paulo 		data->ni = NULL;
166931d98677SRui Paulo 	}
167031d98677SRui Paulo }
167131d98677SRui Paulo 
167231d98677SRui Paulo static void
1673400b4e53SHans Petter Selasky rsu_bulk_tx_callback_sub(struct usb_xfer *xfer, usb_error_t error,
1674400b4e53SHans Petter Selasky     uint8_t which)
167531d98677SRui Paulo {
167631d98677SRui Paulo 	struct rsu_softc *sc = usbd_xfer_softc(xfer);
16777a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
167831d98677SRui Paulo 	struct rsu_data *data;
167931d98677SRui Paulo 
168031d98677SRui Paulo 	RSU_ASSERT_LOCKED(sc);
168131d98677SRui Paulo 
168231d98677SRui Paulo 	switch (USB_GET_STATE(xfer)) {
168331d98677SRui Paulo 	case USB_ST_TRANSFERRED:
1684400b4e53SHans Petter Selasky 		data = STAILQ_FIRST(&sc->sc_tx_active[which]);
168531d98677SRui Paulo 		if (data == NULL)
168631d98677SRui Paulo 			goto tr_setup;
16874914fa0fSAdrian Chadd 		RSU_DPRINTF(sc, RSU_DEBUG_TXDONE, "%s: transfer done %p\n",
16884914fa0fSAdrian Chadd 		    __func__, data);
1689400b4e53SHans Petter Selasky 		STAILQ_REMOVE_HEAD(&sc->sc_tx_active[which], next);
169031d98677SRui Paulo 		rsu_txeof(xfer, data);
169131d98677SRui Paulo 		STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
169231d98677SRui Paulo 		/* FALLTHROUGH */
169331d98677SRui Paulo 	case USB_ST_SETUP:
169431d98677SRui Paulo tr_setup:
1695400b4e53SHans Petter Selasky 		data = STAILQ_FIRST(&sc->sc_tx_pending[which]);
169631d98677SRui Paulo 		if (data == NULL) {
16974914fa0fSAdrian Chadd 			RSU_DPRINTF(sc, RSU_DEBUG_TXDONE,
16984914fa0fSAdrian Chadd 			    "%s: empty pending queue sc %p\n", __func__, sc);
169931d98677SRui Paulo 			return;
170031d98677SRui Paulo 		}
1701400b4e53SHans Petter Selasky 		STAILQ_REMOVE_HEAD(&sc->sc_tx_pending[which], next);
1702400b4e53SHans Petter Selasky 		STAILQ_INSERT_TAIL(&sc->sc_tx_active[which], data, next);
170331d98677SRui Paulo 		usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen);
17044914fa0fSAdrian Chadd 		RSU_DPRINTF(sc, RSU_DEBUG_TXDONE,
17054914fa0fSAdrian Chadd 		    "%s: submitting transfer %p\n",
17064914fa0fSAdrian Chadd 		    __func__,
17074914fa0fSAdrian Chadd 		    data);
170831d98677SRui Paulo 		usbd_transfer_submit(xfer);
170931d98677SRui Paulo 		break;
171031d98677SRui Paulo 	default:
1711400b4e53SHans Petter Selasky 		data = STAILQ_FIRST(&sc->sc_tx_active[which]);
1712400b4e53SHans Petter Selasky 		if (data != NULL) {
1713400b4e53SHans Petter Selasky 			STAILQ_REMOVE_HEAD(&sc->sc_tx_active[which], next);
1714400b4e53SHans Petter Selasky 			rsu_txeof(xfer, data);
1715400b4e53SHans Petter Selasky 			STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
171631d98677SRui Paulo 		}
17177a79cebfSGleb Smirnoff 		counter_u64_add(ic->ic_oerrors, 1);
1718400b4e53SHans Petter Selasky 
171931d98677SRui Paulo 		if (error != USB_ERR_CANCELLED) {
172031d98677SRui Paulo 			usbd_xfer_set_stall(xfer);
172131d98677SRui Paulo 			goto tr_setup;
172231d98677SRui Paulo 		}
172331d98677SRui Paulo 		break;
172431d98677SRui Paulo 	}
172531d98677SRui Paulo }
172631d98677SRui Paulo 
1727400b4e53SHans Petter Selasky static void
1728910593b5SHans Petter Selasky rsu_bulk_tx_callback_be_bk(struct usb_xfer *xfer, usb_error_t error)
1729400b4e53SHans Petter Selasky {
1730910593b5SHans Petter Selasky 	rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_BE_BK);
1731400b4e53SHans Petter Selasky }
1732400b4e53SHans Petter Selasky 
1733400b4e53SHans Petter Selasky static void
1734910593b5SHans Petter Selasky rsu_bulk_tx_callback_vi_vo(struct usb_xfer *xfer, usb_error_t error)
1735400b4e53SHans Petter Selasky {
1736910593b5SHans Petter Selasky 	rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_VI_VO);
1737400b4e53SHans Petter Selasky }
1738400b4e53SHans Petter Selasky 
1739bc6a9865SAdrian Chadd static void
1740bc6a9865SAdrian Chadd rsu_bulk_tx_callback_h2c(struct usb_xfer *xfer, usb_error_t error)
1741bc6a9865SAdrian Chadd {
1742bc6a9865SAdrian Chadd 	rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_H2C);
1743bc6a9865SAdrian Chadd }
1744bc6a9865SAdrian Chadd 
174531d98677SRui Paulo static int
174631d98677SRui Paulo rsu_tx_start(struct rsu_softc *sc, struct ieee80211_node *ni,
1747400b4e53SHans Petter Selasky     struct mbuf *m0, struct rsu_data *data)
174831d98677SRui Paulo {
17497a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
175031d98677SRui Paulo         struct ieee80211vap *vap = ni->ni_vap;
175131d98677SRui Paulo 	struct ieee80211_frame *wh;
175231d98677SRui Paulo 	struct ieee80211_key *k = NULL;
175331d98677SRui Paulo 	struct r92s_tx_desc *txd;
1754400b4e53SHans Petter Selasky 	uint8_t type;
1755400b4e53SHans Petter Selasky 	uint8_t tid = 0;
1756400b4e53SHans Petter Selasky 	uint8_t which;
1757400b4e53SHans Petter Selasky 	int hasqos;
1758400b4e53SHans Petter Selasky 	int xferlen;
175931d98677SRui Paulo 
176031d98677SRui Paulo 	RSU_ASSERT_LOCKED(sc);
176131d98677SRui Paulo 
176231d98677SRui Paulo 	wh = mtod(m0, struct ieee80211_frame *);
176331d98677SRui Paulo 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
176431d98677SRui Paulo 
17654914fa0fSAdrian Chadd 	RSU_DPRINTF(sc, RSU_DEBUG_TX, "%s: data=%p, m=%p\n",
17664914fa0fSAdrian Chadd 	    __func__, data, m0);
17674914fa0fSAdrian Chadd 
17685945b5f5SKevin Lo 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
176931d98677SRui Paulo 		k = ieee80211_crypto_encap(ni, m0);
177031d98677SRui Paulo 		if (k == NULL) {
177131d98677SRui Paulo 			device_printf(sc->sc_dev,
177231d98677SRui Paulo 			    "ieee80211_crypto_encap returns NULL.\n");
177331d98677SRui Paulo 			/* XXX we don't expect the fragmented frames */
177431d98677SRui Paulo 			m_freem(m0);
177531d98677SRui Paulo 			return (ENOBUFS);
177631d98677SRui Paulo 		}
177731d98677SRui Paulo 		wh = mtod(m0, struct ieee80211_frame *);
177831d98677SRui Paulo 	}
177931d98677SRui Paulo 	switch (type) {
178031d98677SRui Paulo 	case IEEE80211_FC0_TYPE_CTL:
178131d98677SRui Paulo 	case IEEE80211_FC0_TYPE_MGT:
1782910593b5SHans Petter Selasky 		which = rsu_wme_ac_xfer_map[WME_AC_VO];
178331d98677SRui Paulo 		break;
178431d98677SRui Paulo 	default:
1785910593b5SHans Petter Selasky 		which = rsu_wme_ac_xfer_map[M_WME_GETAC(m0)];
178631d98677SRui Paulo 		break;
178731d98677SRui Paulo 	}
178831d98677SRui Paulo 	hasqos = 0;
178931d98677SRui Paulo 
179031d98677SRui Paulo 	/* Fill Tx descriptor. */
179131d98677SRui Paulo 	txd = (struct r92s_tx_desc *)data->buf;
179231d98677SRui Paulo 	memset(txd, 0, sizeof(*txd));
179331d98677SRui Paulo 
179431d98677SRui Paulo 	txd->txdw0 |= htole32(
179531d98677SRui Paulo 	    SM(R92S_TXDW0_PKTLEN, m0->m_pkthdr.len) |
179631d98677SRui Paulo 	    SM(R92S_TXDW0_OFFSET, sizeof(*txd)) |
179731d98677SRui Paulo 	    R92S_TXDW0_OWN | R92S_TXDW0_FSG | R92S_TXDW0_LSG);
179831d98677SRui Paulo 
179931d98677SRui Paulo 	txd->txdw1 |= htole32(
180031d98677SRui Paulo 	    SM(R92S_TXDW1_MACID, R92S_MACID_BSS) |
180131d98677SRui Paulo 	    SM(R92S_TXDW1_QSEL, R92S_TXDW1_QSEL_BE));
180231d98677SRui Paulo 	if (!hasqos)
180331d98677SRui Paulo 		txd->txdw1 |= htole32(R92S_TXDW1_NONQOS);
180431d98677SRui Paulo #ifdef notyet
180531d98677SRui Paulo 	if (k != NULL) {
180631d98677SRui Paulo 		switch (k->wk_cipher->ic_cipher) {
180731d98677SRui Paulo 		case IEEE80211_CIPHER_WEP:
180831d98677SRui Paulo 			cipher = R92S_TXDW1_CIPHER_WEP;
180931d98677SRui Paulo 			break;
181031d98677SRui Paulo 		case IEEE80211_CIPHER_TKIP:
181131d98677SRui Paulo 			cipher = R92S_TXDW1_CIPHER_TKIP;
181231d98677SRui Paulo 			break;
181331d98677SRui Paulo 		case IEEE80211_CIPHER_AES_CCM:
181431d98677SRui Paulo 			cipher = R92S_TXDW1_CIPHER_AES;
181531d98677SRui Paulo 			break;
181631d98677SRui Paulo 		default:
181731d98677SRui Paulo 			cipher = R92S_TXDW1_CIPHER_NONE;
181831d98677SRui Paulo 		}
181931d98677SRui Paulo 		txd->txdw1 |= htole32(
182031d98677SRui Paulo 		    SM(R92S_TXDW1_CIPHER, cipher) |
182131d98677SRui Paulo 		    SM(R92S_TXDW1_KEYIDX, k->k_id));
182231d98677SRui Paulo 	}
182331d98677SRui Paulo #endif
182431d98677SRui Paulo 	txd->txdw2 |= htole32(R92S_TXDW2_BK);
182531d98677SRui Paulo 	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
182631d98677SRui Paulo 		txd->txdw2 |= htole32(R92S_TXDW2_BMCAST);
182731d98677SRui Paulo 	/*
182831d98677SRui Paulo 	 * Firmware will use and increment the sequence number for the
182931d98677SRui Paulo 	 * specified TID.
183031d98677SRui Paulo 	 */
183131d98677SRui Paulo 	txd->txdw3 |= htole32(SM(R92S_TXDW3_SEQ, tid));
183231d98677SRui Paulo 
183331d98677SRui Paulo 	if (ieee80211_radiotap_active_vap(vap)) {
183431d98677SRui Paulo 		struct rsu_tx_radiotap_header *tap = &sc->sc_txtap;
183531d98677SRui Paulo 
183631d98677SRui Paulo 		tap->wt_flags = 0;
183731d98677SRui Paulo 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
183831d98677SRui Paulo 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
183931d98677SRui Paulo 		ieee80211_radiotap_tx(vap, m0);
184031d98677SRui Paulo 	}
18414914fa0fSAdrian Chadd 
184231d98677SRui Paulo 	xferlen = sizeof(*txd) + m0->m_pkthdr.len;
184331d98677SRui Paulo 	m_copydata(m0, 0, m0->m_pkthdr.len, (caddr_t)&txd[1]);
184431d98677SRui Paulo 
184531d98677SRui Paulo 	data->buflen = xferlen;
184631d98677SRui Paulo 	data->ni = ni;
184731d98677SRui Paulo 	data->m = m0;
1848400b4e53SHans Petter Selasky 	STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next);
184931d98677SRui Paulo 
1850400b4e53SHans Petter Selasky 	/* start transfer, if any */
1851910593b5SHans Petter Selasky 	usbd_transfer_start(sc->sc_xfer[which]);
185231d98677SRui Paulo 	return (0);
185331d98677SRui Paulo }
185431d98677SRui Paulo 
18557a79cebfSGleb Smirnoff static int
18567a79cebfSGleb Smirnoff rsu_transmit(struct ieee80211com *ic, struct mbuf *m)
185731d98677SRui Paulo {
18587a79cebfSGleb Smirnoff 	struct rsu_softc *sc = ic->ic_softc;
18597a79cebfSGleb Smirnoff 	int error;
186031d98677SRui Paulo 
186131d98677SRui Paulo 	RSU_LOCK(sc);
18627a79cebfSGleb Smirnoff 	if (!sc->sc_running) {
186331d98677SRui Paulo 		RSU_UNLOCK(sc);
18647a79cebfSGleb Smirnoff 		return (ENXIO);
18657a79cebfSGleb Smirnoff 	}
18667a79cebfSGleb Smirnoff 	error = mbufq_enqueue(&sc->sc_snd, m);
18677a79cebfSGleb Smirnoff 	if (error) {
18687a79cebfSGleb Smirnoff 		RSU_UNLOCK(sc);
18697a79cebfSGleb Smirnoff 		return (error);
18707a79cebfSGleb Smirnoff 	}
18717a79cebfSGleb Smirnoff 	rsu_start(sc);
18727a79cebfSGleb Smirnoff 	RSU_UNLOCK(sc);
18737a79cebfSGleb Smirnoff 
18747a79cebfSGleb Smirnoff 	return (0);
187531d98677SRui Paulo }
187631d98677SRui Paulo 
187731d98677SRui Paulo static void
18787a79cebfSGleb Smirnoff rsu_start(struct rsu_softc *sc)
187931d98677SRui Paulo {
188031d98677SRui Paulo 	struct ieee80211_node *ni;
188131d98677SRui Paulo 	struct rsu_data *bf;
1882400b4e53SHans Petter Selasky 	struct mbuf *m;
188331d98677SRui Paulo 
188431d98677SRui Paulo 	RSU_ASSERT_LOCKED(sc);
188531d98677SRui Paulo 
18867a79cebfSGleb Smirnoff 	while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
18877a79cebfSGleb Smirnoff 		bf = rsu_getbuf(sc);
18887a79cebfSGleb Smirnoff 		if (bf == NULL) {
18897a79cebfSGleb Smirnoff 			mbufq_prepend(&sc->sc_snd, m);
189031d98677SRui Paulo 			break;
18917a79cebfSGleb Smirnoff 		}
18927a79cebfSGleb Smirnoff 
189331d98677SRui Paulo 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
189431d98677SRui Paulo 		m->m_pkthdr.rcvif = NULL;
189531d98677SRui Paulo 
18967a79cebfSGleb Smirnoff 		if (rsu_tx_start(sc, ni, m, bf) != 0) {
18977a79cebfSGleb Smirnoff 			if_inc_counter(ni->ni_vap->iv_ifp,
18987a79cebfSGleb Smirnoff 			    IFCOUNTER_OERRORS, 1);
189931d98677SRui Paulo 			STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
190031d98677SRui Paulo 			ieee80211_free_node(ni);
19017a79cebfSGleb Smirnoff 			break;
190231d98677SRui Paulo 		}
190331d98677SRui Paulo 	}
190431d98677SRui Paulo }
190531d98677SRui Paulo 
19067a79cebfSGleb Smirnoff static void
19077a79cebfSGleb Smirnoff rsu_parent(struct ieee80211com *ic)
190831d98677SRui Paulo {
1909d3fdd08cSAdrian Chadd 	struct rsu_softc *sc = ic->ic_softc;
19107a79cebfSGleb Smirnoff 	int startall = 0;
191131d98677SRui Paulo 
19127a79cebfSGleb Smirnoff 	RSU_LOCK(sc);
19137a79cebfSGleb Smirnoff 	if (ic->ic_nrunning > 0) {
19147a79cebfSGleb Smirnoff 		if (!sc->sc_running) {
1915d3fdd08cSAdrian Chadd 			rsu_init(sc);
191631d98677SRui Paulo 			startall = 1;
191731d98677SRui Paulo 		}
19187a79cebfSGleb Smirnoff 	} else if (sc->sc_running)
19197a79cebfSGleb Smirnoff 		rsu_stop(sc);
19207a79cebfSGleb Smirnoff 	RSU_UNLOCK(sc);
19217a79cebfSGleb Smirnoff 
192231d98677SRui Paulo 	if (startall)
192331d98677SRui Paulo 		ieee80211_start_all(ic);
192431d98677SRui Paulo }
192531d98677SRui Paulo 
192631d98677SRui Paulo /*
192731d98677SRui Paulo  * Power on sequence for A-cut adapters.
192831d98677SRui Paulo  */
192931d98677SRui Paulo static void
193031d98677SRui Paulo rsu_power_on_acut(struct rsu_softc *sc)
193131d98677SRui Paulo {
193231d98677SRui Paulo 	uint32_t reg;
193331d98677SRui Paulo 
193431d98677SRui Paulo 	rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x53);
193531d98677SRui Paulo 	rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x57);
193631d98677SRui Paulo 
193731d98677SRui Paulo 	/* Enable AFE macro block's bandgap and Mbias. */
193831d98677SRui Paulo 	rsu_write_1(sc, R92S_AFE_MISC,
193931d98677SRui Paulo 	    rsu_read_1(sc, R92S_AFE_MISC) |
194031d98677SRui Paulo 	    R92S_AFE_MISC_BGEN | R92S_AFE_MISC_MBEN);
194131d98677SRui Paulo 	/* Enable LDOA15 block. */
194231d98677SRui Paulo 	rsu_write_1(sc, R92S_LDOA15_CTRL,
194331d98677SRui Paulo 	    rsu_read_1(sc, R92S_LDOA15_CTRL) | R92S_LDA15_EN);
194431d98677SRui Paulo 
194531d98677SRui Paulo 	rsu_write_1(sc, R92S_SPS1_CTRL,
194631d98677SRui Paulo 	    rsu_read_1(sc, R92S_SPS1_CTRL) | R92S_SPS1_LDEN);
194717ebf553SAdrian Chadd 	rsu_ms_delay(sc, 2000);
194831d98677SRui Paulo 	/* Enable switch regulator block. */
194931d98677SRui Paulo 	rsu_write_1(sc, R92S_SPS1_CTRL,
195031d98677SRui Paulo 	    rsu_read_1(sc, R92S_SPS1_CTRL) | R92S_SPS1_SWEN);
195131d98677SRui Paulo 
195231d98677SRui Paulo 	rsu_write_4(sc, R92S_SPS1_CTRL, 0x00a7b267);
195331d98677SRui Paulo 
195431d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
195531d98677SRui Paulo 	    rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) | 0x08);
195631d98677SRui Paulo 
195731d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
195831d98677SRui Paulo 	    rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x20);
195931d98677SRui Paulo 
196031d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
196131d98677SRui Paulo 	    rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) & ~0x90);
196231d98677SRui Paulo 
196331d98677SRui Paulo 	/* Enable AFE clock. */
196431d98677SRui Paulo 	rsu_write_1(sc, R92S_AFE_XTAL_CTRL + 1,
196531d98677SRui Paulo 	    rsu_read_1(sc, R92S_AFE_XTAL_CTRL + 1) & ~0x04);
196631d98677SRui Paulo 	/* Enable AFE PLL macro block. */
196731d98677SRui Paulo 	rsu_write_1(sc, R92S_AFE_PLL_CTRL,
196831d98677SRui Paulo 	    rsu_read_1(sc, R92S_AFE_PLL_CTRL) | 0x11);
196931d98677SRui Paulo 	/* Attach AFE PLL to MACTOP/BB. */
197031d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_ISO_CTRL,
197131d98677SRui Paulo 	    rsu_read_1(sc, R92S_SYS_ISO_CTRL) & ~0x11);
197231d98677SRui Paulo 
197331d98677SRui Paulo 	/* Switch to 40MHz clock instead of 80MHz. */
197431d98677SRui Paulo 	rsu_write_2(sc, R92S_SYS_CLKR,
197531d98677SRui Paulo 	    rsu_read_2(sc, R92S_SYS_CLKR) & ~R92S_SYS_CLKSEL);
197631d98677SRui Paulo 
197731d98677SRui Paulo 	/* Enable MAC clock. */
197831d98677SRui Paulo 	rsu_write_2(sc, R92S_SYS_CLKR,
197931d98677SRui Paulo 	    rsu_read_2(sc, R92S_SYS_CLKR) |
198031d98677SRui Paulo 	    R92S_MAC_CLK_EN | R92S_SYS_CLK_EN);
198131d98677SRui Paulo 
198231d98677SRui Paulo 	rsu_write_1(sc, R92S_PMC_FSM, 0x02);
198331d98677SRui Paulo 
198431d98677SRui Paulo 	/* Enable digital core and IOREG R/W. */
198531d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
198631d98677SRui Paulo 	    rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x08);
198731d98677SRui Paulo 
198831d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
198931d98677SRui Paulo 	    rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x80);
199031d98677SRui Paulo 
199131d98677SRui Paulo 	/* Switch the control path to firmware. */
199231d98677SRui Paulo 	reg = rsu_read_2(sc, R92S_SYS_CLKR);
199331d98677SRui Paulo 	reg = (reg & ~R92S_SWHW_SEL) | R92S_FWHW_SEL;
199431d98677SRui Paulo 	rsu_write_2(sc, R92S_SYS_CLKR, reg);
199531d98677SRui Paulo 
199631d98677SRui Paulo 	rsu_write_2(sc, R92S_CR, 0x37fc);
199731d98677SRui Paulo 
199831d98677SRui Paulo 	/* Fix USB RX FIFO issue. */
199931d98677SRui Paulo 	rsu_write_1(sc, 0xfe5c,
200031d98677SRui Paulo 	    rsu_read_1(sc, 0xfe5c) | 0x80);
200131d98677SRui Paulo 	rsu_write_1(sc, 0x00ab,
200231d98677SRui Paulo 	    rsu_read_1(sc, 0x00ab) | 0xc0);
200331d98677SRui Paulo 
200431d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_CLKR,
200531d98677SRui Paulo 	    rsu_read_1(sc, R92S_SYS_CLKR) & ~R92S_SYS_CPU_CLKSEL);
200631d98677SRui Paulo }
200731d98677SRui Paulo 
200831d98677SRui Paulo /*
200931d98677SRui Paulo  * Power on sequence for B-cut and C-cut adapters.
201031d98677SRui Paulo  */
201131d98677SRui Paulo static void
201231d98677SRui Paulo rsu_power_on_bcut(struct rsu_softc *sc)
201331d98677SRui Paulo {
201431d98677SRui Paulo 	uint32_t reg;
201531d98677SRui Paulo 	int ntries;
201631d98677SRui Paulo 
201731d98677SRui Paulo 	/* Prevent eFuse leakage. */
201831d98677SRui Paulo 	rsu_write_1(sc, 0x37, 0xb0);
201917ebf553SAdrian Chadd 	rsu_ms_delay(sc, 10);
202031d98677SRui Paulo 	rsu_write_1(sc, 0x37, 0x30);
202131d98677SRui Paulo 
202231d98677SRui Paulo 	/* Switch the control path to hardware. */
202331d98677SRui Paulo 	reg = rsu_read_2(sc, R92S_SYS_CLKR);
202431d98677SRui Paulo 	if (reg & R92S_FWHW_SEL) {
202531d98677SRui Paulo 		rsu_write_2(sc, R92S_SYS_CLKR,
202631d98677SRui Paulo 		    reg & ~(R92S_SWHW_SEL | R92S_FWHW_SEL));
202731d98677SRui Paulo 	}
202831d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
202931d98677SRui Paulo 	    rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) & ~0x8c);
203017ebf553SAdrian Chadd 	rsu_ms_delay(sc, 1);
203131d98677SRui Paulo 
203231d98677SRui Paulo 	rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x53);
203331d98677SRui Paulo 	rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x57);
203431d98677SRui Paulo 
203531d98677SRui Paulo 	reg = rsu_read_1(sc, R92S_AFE_MISC);
203631d98677SRui Paulo 	rsu_write_1(sc, R92S_AFE_MISC, reg | R92S_AFE_MISC_BGEN);
203731d98677SRui Paulo 	rsu_write_1(sc, R92S_AFE_MISC, reg | R92S_AFE_MISC_BGEN |
203831d98677SRui Paulo 	    R92S_AFE_MISC_MBEN | R92S_AFE_MISC_I32_EN);
203931d98677SRui Paulo 
204031d98677SRui Paulo 	/* Enable PLL. */
204131d98677SRui Paulo 	rsu_write_1(sc, R92S_LDOA15_CTRL,
204231d98677SRui Paulo 	    rsu_read_1(sc, R92S_LDOA15_CTRL) | R92S_LDA15_EN);
204331d98677SRui Paulo 
204431d98677SRui Paulo 	rsu_write_1(sc, R92S_LDOV12D_CTRL,
204531d98677SRui Paulo 	    rsu_read_1(sc, R92S_LDOV12D_CTRL) | R92S_LDV12_EN);
204631d98677SRui Paulo 
204731d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
204831d98677SRui Paulo 	    rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) | 0x08);
204931d98677SRui Paulo 
205031d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
205131d98677SRui Paulo 	    rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x20);
205231d98677SRui Paulo 
205331d98677SRui Paulo 	/* Support 64KB IMEM. */
205431d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
205531d98677SRui Paulo 	    rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) & ~0x97);
205631d98677SRui Paulo 
205731d98677SRui Paulo 	/* Enable AFE clock. */
205831d98677SRui Paulo 	rsu_write_1(sc, R92S_AFE_XTAL_CTRL + 1,
205931d98677SRui Paulo 	    rsu_read_1(sc, R92S_AFE_XTAL_CTRL + 1) & ~0x04);
206031d98677SRui Paulo 	/* Enable AFE PLL macro block. */
206131d98677SRui Paulo 	reg = rsu_read_1(sc, R92S_AFE_PLL_CTRL);
206231d98677SRui Paulo 	rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x11);
206317ebf553SAdrian Chadd 	rsu_ms_delay(sc, 1);
206431d98677SRui Paulo 	rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x51);
206517ebf553SAdrian Chadd 	rsu_ms_delay(sc, 1);
206631d98677SRui Paulo 	rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x11);
206717ebf553SAdrian Chadd 	rsu_ms_delay(sc, 1);
206831d98677SRui Paulo 
206931d98677SRui Paulo 	/* Attach AFE PLL to MACTOP/BB. */
207031d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_ISO_CTRL,
207131d98677SRui Paulo 	    rsu_read_1(sc, R92S_SYS_ISO_CTRL) & ~0x11);
207231d98677SRui Paulo 
207331d98677SRui Paulo 	/* Switch to 40MHz clock. */
207431d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_CLKR, 0x00);
207531d98677SRui Paulo 	/* Disable CPU clock and 80MHz SSC. */
207631d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_CLKR,
207731d98677SRui Paulo 	    rsu_read_1(sc, R92S_SYS_CLKR) | 0xa0);
207831d98677SRui Paulo 	/* Enable MAC clock. */
207931d98677SRui Paulo 	rsu_write_2(sc, R92S_SYS_CLKR,
208031d98677SRui Paulo 	    rsu_read_2(sc, R92S_SYS_CLKR) |
208131d98677SRui Paulo 	    R92S_MAC_CLK_EN | R92S_SYS_CLK_EN);
208231d98677SRui Paulo 
208331d98677SRui Paulo 	rsu_write_1(sc, R92S_PMC_FSM, 0x02);
208431d98677SRui Paulo 
208531d98677SRui Paulo 	/* Enable digital core and IOREG R/W. */
208631d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
208731d98677SRui Paulo 	    rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x08);
208831d98677SRui Paulo 
208931d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
209031d98677SRui Paulo 	    rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x80);
209131d98677SRui Paulo 
209231d98677SRui Paulo 	/* Switch the control path to firmware. */
209331d98677SRui Paulo 	reg = rsu_read_2(sc, R92S_SYS_CLKR);
209431d98677SRui Paulo 	reg = (reg & ~R92S_SWHW_SEL) | R92S_FWHW_SEL;
209531d98677SRui Paulo 	rsu_write_2(sc, R92S_SYS_CLKR, reg);
209631d98677SRui Paulo 
209731d98677SRui Paulo 	rsu_write_2(sc, R92S_CR, 0x37fc);
209831d98677SRui Paulo 
209931d98677SRui Paulo 	/* Fix USB RX FIFO issue. */
210031d98677SRui Paulo 	rsu_write_1(sc, 0xfe5c,
210131d98677SRui Paulo 	    rsu_read_1(sc, 0xfe5c) | 0x80);
210231d98677SRui Paulo 
210331d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_CLKR,
210431d98677SRui Paulo 	    rsu_read_1(sc, R92S_SYS_CLKR) & ~R92S_SYS_CPU_CLKSEL);
210531d98677SRui Paulo 
210631d98677SRui Paulo 	rsu_write_1(sc, 0xfe1c, 0x80);
210731d98677SRui Paulo 
210831d98677SRui Paulo 	/* Make sure TxDMA is ready to download firmware. */
210931d98677SRui Paulo 	for (ntries = 0; ntries < 20; ntries++) {
211031d98677SRui Paulo 		reg = rsu_read_1(sc, R92S_TCR);
211131d98677SRui Paulo 		if ((reg & (R92S_TCR_IMEM_CHK_RPT | R92S_TCR_EMEM_CHK_RPT)) ==
211231d98677SRui Paulo 		    (R92S_TCR_IMEM_CHK_RPT | R92S_TCR_EMEM_CHK_RPT))
211331d98677SRui Paulo 			break;
211417ebf553SAdrian Chadd 		rsu_ms_delay(sc, 1);
211531d98677SRui Paulo 	}
211631d98677SRui Paulo 	if (ntries == 20) {
21174914fa0fSAdrian Chadd 		RSU_DPRINTF(sc, RSU_DEBUG_RESET | RSU_DEBUG_TX,
21184914fa0fSAdrian Chadd 		    "%s: TxDMA is not ready\n",
21194914fa0fSAdrian Chadd 		    __func__);
212031d98677SRui Paulo 		/* Reset TxDMA. */
212131d98677SRui Paulo 		reg = rsu_read_1(sc, R92S_CR);
212231d98677SRui Paulo 		rsu_write_1(sc, R92S_CR, reg & ~R92S_CR_TXDMA_EN);
212317ebf553SAdrian Chadd 		rsu_ms_delay(sc, 1);
212431d98677SRui Paulo 		rsu_write_1(sc, R92S_CR, reg | R92S_CR_TXDMA_EN);
212531d98677SRui Paulo 	}
212631d98677SRui Paulo }
212731d98677SRui Paulo 
212831d98677SRui Paulo static void
212931d98677SRui Paulo rsu_power_off(struct rsu_softc *sc)
213031d98677SRui Paulo {
213131d98677SRui Paulo 	/* Turn RF off. */
213231d98677SRui Paulo 	rsu_write_1(sc, R92S_RF_CTRL, 0x00);
213317ebf553SAdrian Chadd 	rsu_ms_delay(sc, 5);
213431d98677SRui Paulo 
213531d98677SRui Paulo 	/* Turn MAC off. */
213631d98677SRui Paulo 	/* Switch control path. */
213731d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_CLKR + 1, 0x38);
213831d98677SRui Paulo 	/* Reset MACTOP. */
213931d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 0x70);
214031d98677SRui Paulo 	rsu_write_1(sc, R92S_PMC_FSM, 0x06);
214131d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_ISO_CTRL + 0, 0xf9);
214231d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1, 0xe8);
214331d98677SRui Paulo 
214431d98677SRui Paulo 	/* Disable AFE PLL. */
214531d98677SRui Paulo 	rsu_write_1(sc, R92S_AFE_PLL_CTRL, 0x00);
214631d98677SRui Paulo 	/* Disable A15V. */
214731d98677SRui Paulo 	rsu_write_1(sc, R92S_LDOA15_CTRL, 0x54);
214831d98677SRui Paulo 	/* Disable eFuse 1.2V. */
214931d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 0x50);
215031d98677SRui Paulo 	rsu_write_1(sc, R92S_LDOV12D_CTRL, 0x24);
215131d98677SRui Paulo 	/* Enable AFE macro block's bandgap and Mbias. */
215231d98677SRui Paulo 	rsu_write_1(sc, R92S_AFE_MISC, 0x30);
215331d98677SRui Paulo 	/* Disable 1.6V LDO. */
215431d98677SRui Paulo 	rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x56);
215531d98677SRui Paulo 	rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x43);
215631d98677SRui Paulo }
215731d98677SRui Paulo 
215831d98677SRui Paulo static int
21596d9b2f85SRui Paulo rsu_fw_loadsection(struct rsu_softc *sc, const uint8_t *buf, int len)
216031d98677SRui Paulo {
2161910593b5SHans Petter Selasky 	const uint8_t which = rsu_wme_ac_xfer_map[WME_AC_VO];
216231d98677SRui Paulo 	struct rsu_data *data;
216331d98677SRui Paulo 	struct r92s_tx_desc *txd;
216431d98677SRui Paulo 	int mlen;
216531d98677SRui Paulo 
216631d98677SRui Paulo 	while (len > 0) {
216731d98677SRui Paulo 		data = rsu_getbuf(sc);
216831d98677SRui Paulo 		if (data == NULL)
216931d98677SRui Paulo 			return (ENOMEM);
217031d98677SRui Paulo 		txd = (struct r92s_tx_desc *)data->buf;
217131d98677SRui Paulo 		memset(txd, 0, sizeof(*txd));
217231d98677SRui Paulo 		if (len <= RSU_TXBUFSZ - sizeof(*txd)) {
217331d98677SRui Paulo 			/* Last chunk. */
217431d98677SRui Paulo 			txd->txdw0 |= htole32(R92S_TXDW0_LINIP);
217531d98677SRui Paulo 			mlen = len;
217631d98677SRui Paulo 		} else
217731d98677SRui Paulo 			mlen = RSU_TXBUFSZ - sizeof(*txd);
217831d98677SRui Paulo 		txd->txdw0 |= htole32(SM(R92S_TXDW0_PKTLEN, mlen));
217931d98677SRui Paulo 		memcpy(&txd[1], buf, mlen);
218031d98677SRui Paulo 		data->buflen = sizeof(*txd) + mlen;
21814914fa0fSAdrian Chadd 		RSU_DPRINTF(sc, RSU_DEBUG_TX | RSU_DEBUG_FW | RSU_DEBUG_RESET,
21824914fa0fSAdrian Chadd 		    "%s: starting transfer %p\n",
21834914fa0fSAdrian Chadd 		    __func__, data);
2184400b4e53SHans Petter Selasky 		STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next);
218531d98677SRui Paulo 		buf += mlen;
218631d98677SRui Paulo 		len -= mlen;
218731d98677SRui Paulo 	}
2188910593b5SHans Petter Selasky 	usbd_transfer_start(sc->sc_xfer[which]);
218931d98677SRui Paulo 	return (0);
219031d98677SRui Paulo }
219131d98677SRui Paulo 
219231d98677SRui Paulo static int
219331d98677SRui Paulo rsu_load_firmware(struct rsu_softc *sc)
219431d98677SRui Paulo {
21956d9b2f85SRui Paulo 	const struct r92s_fw_hdr *hdr;
219631d98677SRui Paulo 	struct r92s_fw_priv *dmem;
21976d9b2f85SRui Paulo 	const uint8_t *imem, *emem;
219831d98677SRui Paulo 	int imemsz, ememsz;
219931d98677SRui Paulo 	const struct firmware *fw;
220031d98677SRui Paulo 	size_t size;
220131d98677SRui Paulo 	uint32_t reg;
220231d98677SRui Paulo 	int ntries, error;
220331d98677SRui Paulo 
2204910593b5SHans Petter Selasky 	if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_FWRDY) {
22054914fa0fSAdrian Chadd 		RSU_DPRINTF(sc, RSU_DEBUG_FW | RSU_DEBUG_RESET,
22064914fa0fSAdrian Chadd 		    "%s: Firmware already loaded\n",
22074914fa0fSAdrian Chadd 		    __func__);
2208910593b5SHans Petter Selasky 		return (0);
2209910593b5SHans Petter Selasky 	}
2210910593b5SHans Petter Selasky 
221131d98677SRui Paulo 	RSU_UNLOCK(sc);
221231d98677SRui Paulo 	/* Read firmware image from the filesystem. */
221331d98677SRui Paulo 	if ((fw = firmware_get("rsu-rtl8712fw")) == NULL) {
221431d98677SRui Paulo 		device_printf(sc->sc_dev,
221531d98677SRui Paulo 		    "%s: failed load firmware of file rsu-rtl8712fw\n",
221631d98677SRui Paulo 		    __func__);
221731d98677SRui Paulo 		RSU_LOCK(sc);
221831d98677SRui Paulo 		return (ENXIO);
221931d98677SRui Paulo 	}
222031d98677SRui Paulo 	RSU_LOCK(sc);
222131d98677SRui Paulo 	size = fw->datasize;
222231d98677SRui Paulo 	if (size < sizeof(*hdr)) {
222331d98677SRui Paulo 		device_printf(sc->sc_dev, "firmware too short\n");
222431d98677SRui Paulo 		error = EINVAL;
222531d98677SRui Paulo 		goto fail;
222631d98677SRui Paulo 	}
22276d9b2f85SRui Paulo 	hdr = (const struct r92s_fw_hdr *)fw->data;
222831d98677SRui Paulo 	if (hdr->signature != htole16(0x8712) &&
222931d98677SRui Paulo 	    hdr->signature != htole16(0x8192)) {
223031d98677SRui Paulo 		device_printf(sc->sc_dev,
223131d98677SRui Paulo 		    "invalid firmware signature 0x%x\n",
223231d98677SRui Paulo 		    le16toh(hdr->signature));
223331d98677SRui Paulo 		error = EINVAL;
223431d98677SRui Paulo 		goto fail;
223531d98677SRui Paulo 	}
223631d98677SRui Paulo 	DPRINTF("FW V%d %02x-%02x %02x:%02x\n", le16toh(hdr->version),
223731d98677SRui Paulo 	    hdr->month, hdr->day, hdr->hour, hdr->minute);
223831d98677SRui Paulo 
223931d98677SRui Paulo 	/* Make sure that driver and firmware are in sync. */
224031d98677SRui Paulo 	if (hdr->privsz != htole32(sizeof(*dmem))) {
224131d98677SRui Paulo 		device_printf(sc->sc_dev, "unsupported firmware image\n");
224231d98677SRui Paulo 		error = EINVAL;
224331d98677SRui Paulo 		goto fail;
224431d98677SRui Paulo 	}
224531d98677SRui Paulo 	/* Get FW sections sizes. */
224631d98677SRui Paulo 	imemsz = le32toh(hdr->imemsz);
224731d98677SRui Paulo 	ememsz = le32toh(hdr->sramsz);
224831d98677SRui Paulo 	/* Check that all FW sections fit in image. */
224931d98677SRui Paulo 	if (size < sizeof(*hdr) + imemsz + ememsz) {
225031d98677SRui Paulo 		device_printf(sc->sc_dev, "firmware too short\n");
225131d98677SRui Paulo 		error = EINVAL;
225231d98677SRui Paulo 		goto fail;
225331d98677SRui Paulo 	}
22546d9b2f85SRui Paulo 	imem = (const uint8_t *)&hdr[1];
225531d98677SRui Paulo 	emem = imem + imemsz;
225631d98677SRui Paulo 
225731d98677SRui Paulo 	/* Load IMEM section. */
225831d98677SRui Paulo 	error = rsu_fw_loadsection(sc, imem, imemsz);
225931d98677SRui Paulo 	if (error != 0) {
226031d98677SRui Paulo 		device_printf(sc->sc_dev,
226131d98677SRui Paulo 		    "could not load firmware section %s\n", "IMEM");
226231d98677SRui Paulo 		goto fail;
226331d98677SRui Paulo 	}
226431d98677SRui Paulo 	/* Wait for load to complete. */
2265885476cbSHans Petter Selasky 	for (ntries = 0; ntries != 50; ntries++) {
226617ebf553SAdrian Chadd 		rsu_ms_delay(sc, 10);
2267910593b5SHans Petter Selasky 		reg = rsu_read_1(sc, R92S_TCR);
226831d98677SRui Paulo 		if (reg & R92S_TCR_IMEM_CODE_DONE)
226931d98677SRui Paulo 			break;
227031d98677SRui Paulo 	}
2271885476cbSHans Petter Selasky 	if (ntries == 50) {
2272885476cbSHans Petter Selasky 		device_printf(sc->sc_dev, "timeout waiting for IMEM transfer\n");
227331d98677SRui Paulo 		error = ETIMEDOUT;
227431d98677SRui Paulo 		goto fail;
227531d98677SRui Paulo 	}
227631d98677SRui Paulo 	/* Load EMEM section. */
227731d98677SRui Paulo 	error = rsu_fw_loadsection(sc, emem, ememsz);
227831d98677SRui Paulo 	if (error != 0) {
227931d98677SRui Paulo 		device_printf(sc->sc_dev,
228031d98677SRui Paulo 		    "could not load firmware section %s\n", "EMEM");
228131d98677SRui Paulo 		goto fail;
228231d98677SRui Paulo 	}
228331d98677SRui Paulo 	/* Wait for load to complete. */
22841593e875SHans Petter Selasky 	for (ntries = 0; ntries != 50; ntries++) {
228517ebf553SAdrian Chadd 		rsu_ms_delay(sc, 10);
228631d98677SRui Paulo 		reg = rsu_read_2(sc, R92S_TCR);
228731d98677SRui Paulo 		if (reg & R92S_TCR_EMEM_CODE_DONE)
228831d98677SRui Paulo 			break;
228931d98677SRui Paulo 	}
22901593e875SHans Petter Selasky 	if (ntries == 50) {
2291885476cbSHans Petter Selasky 		device_printf(sc->sc_dev, "timeout waiting for EMEM transfer\n");
229231d98677SRui Paulo 		error = ETIMEDOUT;
229331d98677SRui Paulo 		goto fail;
229431d98677SRui Paulo 	}
229531d98677SRui Paulo 	/* Enable CPU. */
229631d98677SRui Paulo 	rsu_write_1(sc, R92S_SYS_CLKR,
229731d98677SRui Paulo 	    rsu_read_1(sc, R92S_SYS_CLKR) | R92S_SYS_CPU_CLKSEL);
229831d98677SRui Paulo 	if (!(rsu_read_1(sc, R92S_SYS_CLKR) & R92S_SYS_CPU_CLKSEL)) {
229931d98677SRui Paulo 		device_printf(sc->sc_dev, "could not enable system clock\n");
230031d98677SRui Paulo 		error = EIO;
230131d98677SRui Paulo 		goto fail;
230231d98677SRui Paulo 	}
230331d98677SRui Paulo 	rsu_write_2(sc, R92S_SYS_FUNC_EN,
230431d98677SRui Paulo 	    rsu_read_2(sc, R92S_SYS_FUNC_EN) | R92S_FEN_CPUEN);
230531d98677SRui Paulo 	if (!(rsu_read_2(sc, R92S_SYS_FUNC_EN) & R92S_FEN_CPUEN)) {
230631d98677SRui Paulo 		device_printf(sc->sc_dev,
230731d98677SRui Paulo 		    "could not enable microcontroller\n");
230831d98677SRui Paulo 		error = EIO;
230931d98677SRui Paulo 		goto fail;
231031d98677SRui Paulo 	}
231131d98677SRui Paulo 	/* Wait for CPU to initialize. */
231231d98677SRui Paulo 	for (ntries = 0; ntries < 100; ntries++) {
2313910593b5SHans Petter Selasky 		if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_IMEM_RDY)
231431d98677SRui Paulo 			break;
231517ebf553SAdrian Chadd 		rsu_ms_delay(sc, 1);
231631d98677SRui Paulo 	}
231731d98677SRui Paulo 	if (ntries == 100) {
231831d98677SRui Paulo 		device_printf(sc->sc_dev,
231931d98677SRui Paulo 		    "timeout waiting for microcontroller\n");
232031d98677SRui Paulo 		error = ETIMEDOUT;
232131d98677SRui Paulo 		goto fail;
232231d98677SRui Paulo 	}
232331d98677SRui Paulo 
232431d98677SRui Paulo 	/* Update DMEM section before loading. */
23256d9b2f85SRui Paulo 	dmem = __DECONST(struct r92s_fw_priv *, &hdr->priv);
232631d98677SRui Paulo 	memset(dmem, 0, sizeof(*dmem));
232731d98677SRui Paulo 	dmem->hci_sel = R92S_HCI_SEL_USB | R92S_HCI_SEL_8172;
2328*b8303685SAdrian Chadd 	dmem->nendpoints = sc->sc_nendpoints;
2329*b8303685SAdrian Chadd 	/* XXX TODO: rf_config should come from ROM */
2330*b8303685SAdrian Chadd 	dmem->rf_config = 0x11;	/* 1T1R */
233131d98677SRui Paulo 	dmem->vcs_type = R92S_VCS_TYPE_AUTO;
233231d98677SRui Paulo 	dmem->vcs_mode = R92S_VCS_MODE_RTS_CTS;
233331d98677SRui Paulo #ifdef notyet
233431d98677SRui Paulo 	dmem->bw40_en = (ic->ic_htcaps & IEEE80211_HTCAP_CBW20_40) != 0;
233531d98677SRui Paulo #endif
2336*b8303685SAdrian Chadd 	dmem->turbo_mode = 0;
233731d98677SRui Paulo 	/* Load DMEM section. */
233831d98677SRui Paulo 	error = rsu_fw_loadsection(sc, (uint8_t *)dmem, sizeof(*dmem));
233931d98677SRui Paulo 	if (error != 0) {
234031d98677SRui Paulo 		device_printf(sc->sc_dev,
234131d98677SRui Paulo 		    "could not load firmware section %s\n", "DMEM");
234231d98677SRui Paulo 		goto fail;
234331d98677SRui Paulo 	}
234431d98677SRui Paulo 	/* Wait for load to complete. */
234531d98677SRui Paulo 	for (ntries = 0; ntries < 100; ntries++) {
2346910593b5SHans Petter Selasky 		if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_DMEM_CODE_DONE)
234731d98677SRui Paulo 			break;
234817ebf553SAdrian Chadd 		rsu_ms_delay(sc, 1);
234931d98677SRui Paulo 	}
235031d98677SRui Paulo 	if (ntries == 100) {
235131d98677SRui Paulo 		device_printf(sc->sc_dev, "timeout waiting for %s transfer\n",
235231d98677SRui Paulo 		    "DMEM");
235331d98677SRui Paulo 		error = ETIMEDOUT;
235431d98677SRui Paulo 		goto fail;
235531d98677SRui Paulo 	}
235631d98677SRui Paulo 	/* Wait for firmware readiness. */
235731d98677SRui Paulo 	for (ntries = 0; ntries < 60; ntries++) {
2358910593b5SHans Petter Selasky 		if (!(rsu_read_1(sc, R92S_TCR) & R92S_TCR_FWRDY))
235931d98677SRui Paulo 			break;
236017ebf553SAdrian Chadd 		rsu_ms_delay(sc, 1);
236131d98677SRui Paulo 	}
236231d98677SRui Paulo 	if (ntries == 60) {
236331d98677SRui Paulo 		device_printf(sc->sc_dev,
236431d98677SRui Paulo 		    "timeout waiting for firmware readiness\n");
236531d98677SRui Paulo 		error = ETIMEDOUT;
236631d98677SRui Paulo 		goto fail;
236731d98677SRui Paulo 	}
236831d98677SRui Paulo  fail:
236931d98677SRui Paulo 	firmware_put(fw, FIRMWARE_UNLOAD);
237031d98677SRui Paulo 	return (error);
237131d98677SRui Paulo }
237231d98677SRui Paulo 
237331d98677SRui Paulo 
237431d98677SRui Paulo static int
237531d98677SRui Paulo rsu_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
237631d98677SRui Paulo     const struct ieee80211_bpf_params *params)
237731d98677SRui Paulo {
237831d98677SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
2379d3fdd08cSAdrian Chadd 	struct rsu_softc *sc = ic->ic_softc;
238031d98677SRui Paulo 	struct rsu_data *bf;
238131d98677SRui Paulo 
238231d98677SRui Paulo 	/* prevent management frames from being sent if we're not ready */
23837a79cebfSGleb Smirnoff 	if (!sc->sc_running) {
238431d98677SRui Paulo 		m_freem(m);
238531d98677SRui Paulo 		ieee80211_free_node(ni);
238631d98677SRui Paulo 		return (ENETDOWN);
238731d98677SRui Paulo 	}
238831d98677SRui Paulo 	RSU_LOCK(sc);
238931d98677SRui Paulo 	bf = rsu_getbuf(sc);
239031d98677SRui Paulo 	if (bf == NULL) {
239131d98677SRui Paulo 		ieee80211_free_node(ni);
239231d98677SRui Paulo 		m_freem(m);
239331d98677SRui Paulo 		RSU_UNLOCK(sc);
239431d98677SRui Paulo 		return (ENOBUFS);
239531d98677SRui Paulo 	}
2396400b4e53SHans Petter Selasky 	if (rsu_tx_start(sc, ni, m, bf) != 0) {
239731d98677SRui Paulo 		ieee80211_free_node(ni);
239831d98677SRui Paulo 		STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
239931d98677SRui Paulo 		RSU_UNLOCK(sc);
240031d98677SRui Paulo 		return (EIO);
240131d98677SRui Paulo 	}
240231d98677SRui Paulo 	RSU_UNLOCK(sc);
240331d98677SRui Paulo 
240431d98677SRui Paulo 	return (0);
240531d98677SRui Paulo }
240631d98677SRui Paulo 
240731d98677SRui Paulo static void
24087a79cebfSGleb Smirnoff rsu_init(struct rsu_softc *sc)
240931d98677SRui Paulo {
24107a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
24117a79cebfSGleb Smirnoff 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
24127a79cebfSGleb Smirnoff 	uint8_t macaddr[IEEE80211_ADDR_LEN];
241331d98677SRui Paulo 	struct r92s_set_pwr_mode cmd;
241431d98677SRui Paulo 	int error;
2415885476cbSHans Petter Selasky 	int i;
241631d98677SRui Paulo 
24177a79cebfSGleb Smirnoff 	RSU_ASSERT_LOCKED(sc);
24187a79cebfSGleb Smirnoff 
241931d98677SRui Paulo 	/* Init host async commands ring. */
242031d98677SRui Paulo 	sc->cmdq.cur = sc->cmdq.next = sc->cmdq.queued = 0;
242131d98677SRui Paulo 
242231d98677SRui Paulo 	/* Power on adapter. */
242331d98677SRui Paulo 	if (sc->cut == 1)
242431d98677SRui Paulo 		rsu_power_on_acut(sc);
242531d98677SRui Paulo 	else
242631d98677SRui Paulo 		rsu_power_on_bcut(sc);
2427910593b5SHans Petter Selasky 
242831d98677SRui Paulo 	/* Load firmware. */
242931d98677SRui Paulo 	error = rsu_load_firmware(sc);
243031d98677SRui Paulo 	if (error != 0)
243131d98677SRui Paulo 		goto fail;
243231d98677SRui Paulo 
243331d98677SRui Paulo 	/* Enable Rx TCP checksum offload. */
243431d98677SRui Paulo 	rsu_write_4(sc, R92S_RCR,
243531d98677SRui Paulo 	    rsu_read_4(sc, R92S_RCR) | 0x04000000);
243631d98677SRui Paulo 	/* Append PHY status. */
243731d98677SRui Paulo 	rsu_write_4(sc, R92S_RCR,
243831d98677SRui Paulo 	    rsu_read_4(sc, R92S_RCR) | 0x02000000);
243931d98677SRui Paulo 
244031d98677SRui Paulo 	rsu_write_4(sc, R92S_CR,
244131d98677SRui Paulo 	    rsu_read_4(sc, R92S_CR) & ~0xff000000);
244231d98677SRui Paulo 
244331d98677SRui Paulo 	/* Use 128 bytes pages. */
244431d98677SRui Paulo 	rsu_write_1(sc, 0x00b5,
244531d98677SRui Paulo 	    rsu_read_1(sc, 0x00b5) | 0x01);
244631d98677SRui Paulo 	/* Enable USB Rx aggregation. */
244731d98677SRui Paulo 	rsu_write_1(sc, 0x00bd,
244831d98677SRui Paulo 	    rsu_read_1(sc, 0x00bd) | 0x80);
244931d98677SRui Paulo 	/* Set USB Rx aggregation threshold. */
245031d98677SRui Paulo 	rsu_write_1(sc, 0x00d9, 0x01);
245131d98677SRui Paulo 	/* Set USB Rx aggregation timeout (1.7ms/4). */
245231d98677SRui Paulo 	rsu_write_1(sc, 0xfe5b, 0x04);
245331d98677SRui Paulo 	/* Fix USB Rx FIFO issue. */
245431d98677SRui Paulo 	rsu_write_1(sc, 0xfe5c,
245531d98677SRui Paulo 	    rsu_read_1(sc, 0xfe5c) | 0x80);
245631d98677SRui Paulo 
245731d98677SRui Paulo 	/* Set MAC address. */
24587a79cebfSGleb Smirnoff 	IEEE80211_ADDR_COPY(macaddr, vap ? vap->iv_myaddr : ic->ic_macaddr);
24597a79cebfSGleb Smirnoff 	rsu_write_region_1(sc, R92S_MACID, macaddr, IEEE80211_ADDR_LEN);
246031d98677SRui Paulo 
2461b41381aeSHans Petter Selasky 	/* It really takes 1.5 seconds for the firmware to boot: */
246217ebf553SAdrian Chadd 	rsu_ms_delay(sc, 2000);
246331d98677SRui Paulo 
24644914fa0fSAdrian Chadd 	RSU_DPRINTF(sc, RSU_DEBUG_RESET, "%s: setting MAC address to %s\n",
24654914fa0fSAdrian Chadd 	    __func__,
24664914fa0fSAdrian Chadd 	    ether_sprintf(macaddr));
24677a79cebfSGleb Smirnoff 	error = rsu_fw_cmd(sc, R92S_CMD_SET_MAC_ADDRESS, macaddr,
246831d98677SRui Paulo 	    IEEE80211_ADDR_LEN);
246931d98677SRui Paulo 	if (error != 0) {
247031d98677SRui Paulo 		device_printf(sc->sc_dev, "could not set MAC address\n");
247131d98677SRui Paulo 		goto fail;
247231d98677SRui Paulo 	}
247331d98677SRui Paulo 
247431d98677SRui Paulo 	rsu_write_1(sc, R92S_USB_HRPWM,
247531d98677SRui Paulo 	    R92S_USB_HRPWM_PS_ST_ACTIVE | R92S_USB_HRPWM_PS_ALL_ON);
247631d98677SRui Paulo 
24779b6916dbSAdrian Chadd 	/* Set PS mode fully active */
247831d98677SRui Paulo 	memset(&cmd, 0, sizeof(cmd));
247931d98677SRui Paulo 	cmd.mode = R92S_PS_MODE_ACTIVE;
24804914fa0fSAdrian Chadd 	RSU_DPRINTF(sc, RSU_DEBUG_RESET, "%s: setting ps mode to %d\n",
24814914fa0fSAdrian Chadd 	    __func__, cmd.mode);
248231d98677SRui Paulo 	error = rsu_fw_cmd(sc, R92S_CMD_SET_PWR_MODE, &cmd, sizeof(cmd));
248331d98677SRui Paulo 	if (error != 0) {
248431d98677SRui Paulo 		device_printf(sc->sc_dev, "could not set PS mode\n");
248531d98677SRui Paulo 		goto fail;
248631d98677SRui Paulo 	}
248731d98677SRui Paulo 
248831d98677SRui Paulo #if 0
248931d98677SRui Paulo 	if (ic->ic_htcaps & IEEE80211_HTCAP_CBW20_40) {
249031d98677SRui Paulo 		/* Enable 40MHz mode. */
249131d98677SRui Paulo 		error = rsu_fw_iocmd(sc,
249231d98677SRui Paulo 		    SM(R92S_IOCMD_CLASS, 0xf4) |
249331d98677SRui Paulo 		    SM(R92S_IOCMD_INDEX, 0x00) |
249431d98677SRui Paulo 		    SM(R92S_IOCMD_VALUE, 0x0007));
249531d98677SRui Paulo 		if (error != 0) {
249631d98677SRui Paulo 			device_printf(sc->sc_dev,
249731d98677SRui Paulo 			    "could not enable 40MHz mode\n");
249831d98677SRui Paulo 			goto fail;
249931d98677SRui Paulo 		}
250031d98677SRui Paulo 	}
250131d98677SRui Paulo 
250231d98677SRui Paulo 	/* Set default channel. */
250331d98677SRui Paulo 	ic->ic_bss->ni_chan = ic->ic_ibss_chan;
250431d98677SRui Paulo #endif
25057a79cebfSGleb Smirnoff 	sc->sc_scan_pass = 0;
250631d98677SRui Paulo 	usbd_transfer_start(sc->sc_xfer[RSU_BULK_RX]);
250731d98677SRui Paulo 
250831d98677SRui Paulo 	/* We're ready to go. */
25097a79cebfSGleb Smirnoff 	sc->sc_running = 1;
251031d98677SRui Paulo 	return;
251131d98677SRui Paulo fail:
2512885476cbSHans Petter Selasky 	/* Need to stop all failed transfers, if any */
2513885476cbSHans Petter Selasky 	for (i = 0; i != RSU_N_TRANSFER; i++)
2514885476cbSHans Petter Selasky 		usbd_transfer_stop(sc->sc_xfer[i]);
251531d98677SRui Paulo }
251631d98677SRui Paulo 
251731d98677SRui Paulo static void
25187a79cebfSGleb Smirnoff rsu_stop(struct rsu_softc *sc)
251931d98677SRui Paulo {
252031d98677SRui Paulo 	int i;
252131d98677SRui Paulo 
25227a79cebfSGleb Smirnoff 	sc->sc_running = 0;
252331d98677SRui Paulo 	sc->sc_calibrating = 0;
252431d98677SRui Paulo 	taskqueue_cancel_timeout(taskqueue_thread, &sc->calib_task, NULL);
252531d98677SRui Paulo 
252631d98677SRui Paulo 	/* Power off adapter. */
252731d98677SRui Paulo 	rsu_power_off(sc);
252831d98677SRui Paulo 
252931d98677SRui Paulo 	for (i = 0; i < RSU_N_TRANSFER; i++)
253031d98677SRui Paulo 		usbd_transfer_stop(sc->sc_xfer[i]);
253131d98677SRui Paulo }
253231d98677SRui Paulo 
253317ebf553SAdrian Chadd /*
253417ebf553SAdrian Chadd  * Note: usb_pause_mtx() actually releases the mutex before calling pause(),
253517ebf553SAdrian Chadd  * which breaks any kind of driver serialisation.
253617ebf553SAdrian Chadd  */
2537b41381aeSHans Petter Selasky static void
253817ebf553SAdrian Chadd rsu_ms_delay(struct rsu_softc *sc, int ms)
2539b41381aeSHans Petter Selasky {
254017ebf553SAdrian Chadd 
254117ebf553SAdrian Chadd 	//usb_pause_mtx(&sc->sc_mtx, hz / 1000);
254217ebf553SAdrian Chadd 	DELAY(ms * 1000);
2543b41381aeSHans Petter Selasky }
2544