xref: /freebsd/sys/dev/usb/wlan/if_rum.c (revision aca2cf3032c97f0b64139bab179e0ffd53f3324d)
102ac6454SAndrew Thompson /*	$FreeBSD$	*/
202ac6454SAndrew Thompson 
302ac6454SAndrew Thompson /*-
402ac6454SAndrew Thompson  * Copyright (c) 2005-2007 Damien Bergamini <damien.bergamini@free.fr>
502ac6454SAndrew Thompson  * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
65d38a4d4SEd Schouten  * Copyright (c) 2007-2008 Hans Petter Selasky <hselasky@FreeBSD.org>
702ac6454SAndrew Thompson  *
802ac6454SAndrew Thompson  * Permission to use, copy, modify, and distribute this software for any
902ac6454SAndrew Thompson  * purpose with or without fee is hereby granted, provided that the above
1002ac6454SAndrew Thompson  * copyright notice and this permission notice appear in all copies.
1102ac6454SAndrew Thompson  *
1202ac6454SAndrew Thompson  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1302ac6454SAndrew Thompson  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1402ac6454SAndrew Thompson  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1502ac6454SAndrew Thompson  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1602ac6454SAndrew Thompson  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1702ac6454SAndrew Thompson  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1802ac6454SAndrew Thompson  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1902ac6454SAndrew Thompson  */
2002ac6454SAndrew Thompson 
2102ac6454SAndrew Thompson #include <sys/cdefs.h>
2202ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
2302ac6454SAndrew Thompson 
2402ac6454SAndrew Thompson /*-
2502ac6454SAndrew Thompson  * Ralink Technology RT2501USB/RT2601USB chipset driver
2602ac6454SAndrew Thompson  * http://www.ralinktech.com.tw/
2702ac6454SAndrew Thompson  */
2802ac6454SAndrew Thompson 
295efea30fSAndrew Thompson #include <sys/param.h>
305efea30fSAndrew Thompson #include <sys/sockio.h>
315efea30fSAndrew Thompson #include <sys/sysctl.h>
325efea30fSAndrew Thompson #include <sys/lock.h>
335efea30fSAndrew Thompson #include <sys/mutex.h>
345efea30fSAndrew Thompson #include <sys/mbuf.h>
355efea30fSAndrew Thompson #include <sys/kernel.h>
365efea30fSAndrew Thompson #include <sys/socket.h>
375efea30fSAndrew Thompson #include <sys/systm.h>
385efea30fSAndrew Thompson #include <sys/malloc.h>
395efea30fSAndrew Thompson #include <sys/module.h>
405efea30fSAndrew Thompson #include <sys/bus.h>
415efea30fSAndrew Thompson #include <sys/endian.h>
425efea30fSAndrew Thompson #include <sys/kdb.h>
435efea30fSAndrew Thompson 
445efea30fSAndrew Thompson #include <machine/bus.h>
4567784314SPoul-Henning Kamp #include <machine/resource.h>
4667784314SPoul-Henning Kamp #include <sys/rman.h>
475efea30fSAndrew Thompson 
485efea30fSAndrew Thompson #include <net/bpf.h>
495efea30fSAndrew Thompson #include <net/if.h>
5076039bc8SGleb Smirnoff #include <net/if_var.h>
5167784314SPoul-Henning Kamp #include <net/if_arp.h>
525efea30fSAndrew Thompson #include <net/ethernet.h>
535efea30fSAndrew Thompson #include <net/if_dl.h>
545efea30fSAndrew Thompson #include <net/if_media.h>
555efea30fSAndrew Thompson #include <net/if_types.h>
565efea30fSAndrew Thompson 
575efea30fSAndrew Thompson #ifdef INET
585efea30fSAndrew Thompson #include <netinet/in.h>
595efea30fSAndrew Thompson #include <netinet/in_systm.h>
605efea30fSAndrew Thompson #include <netinet/in_var.h>
615efea30fSAndrew Thompson #include <netinet/if_ether.h>
625efea30fSAndrew Thompson #include <netinet/ip.h>
635efea30fSAndrew Thompson #endif
645efea30fSAndrew Thompson 
655efea30fSAndrew Thompson #include <net80211/ieee80211_var.h>
665efea30fSAndrew Thompson #include <net80211/ieee80211_regdomain.h>
675efea30fSAndrew Thompson #include <net80211/ieee80211_radiotap.h>
68b6108616SRui Paulo #include <net80211/ieee80211_ratectl.h>
6902ac6454SAndrew Thompson 
705efea30fSAndrew Thompson #include <dev/usb/usb.h>
71ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
725efea30fSAndrew Thompson #include "usbdevs.h"
7302ac6454SAndrew Thompson 
74ed6d949aSAndrew Thompson #define	USB_DEBUG_VAR rum_debug
75ed6d949aSAndrew Thompson #include <dev/usb/usb_debug.h>
76ed6d949aSAndrew Thompson 
7702ac6454SAndrew Thompson #include <dev/usb/wlan/if_rumreg.h>
7802ac6454SAndrew Thompson #include <dev/usb/wlan/if_rumvar.h>
7902ac6454SAndrew Thompson #include <dev/usb/wlan/if_rumfw.h>
8002ac6454SAndrew Thompson 
81b850ecc1SAndrew Thompson #ifdef USB_DEBUG
8202ac6454SAndrew Thompson static int rum_debug = 0;
8302ac6454SAndrew Thompson 
846472ac3dSEd Schouten static SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
85ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RWTUN, &rum_debug, 0,
8602ac6454SAndrew Thompson     "Debug level");
8702ac6454SAndrew Thompson #endif
8802ac6454SAndrew Thompson 
89f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID rum_devs[] = {
909e6b5313SAndrew Thompson #define	RUM_DEV(v,p)  { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
919e6b5313SAndrew Thompson     RUM_DEV(ABOCOM, HWU54DM),
929e6b5313SAndrew Thompson     RUM_DEV(ABOCOM, RT2573_2),
939e6b5313SAndrew Thompson     RUM_DEV(ABOCOM, RT2573_3),
949e6b5313SAndrew Thompson     RUM_DEV(ABOCOM, RT2573_4),
959e6b5313SAndrew Thompson     RUM_DEV(ABOCOM, WUG2700),
969e6b5313SAndrew Thompson     RUM_DEV(AMIT, CGWLUSB2GO),
979e6b5313SAndrew Thompson     RUM_DEV(ASUS, RT2573_1),
989e6b5313SAndrew Thompson     RUM_DEV(ASUS, RT2573_2),
999e6b5313SAndrew Thompson     RUM_DEV(BELKIN, F5D7050A),
1009e6b5313SAndrew Thompson     RUM_DEV(BELKIN, F5D9050V3),
1019e6b5313SAndrew Thompson     RUM_DEV(CISCOLINKSYS, WUSB54GC),
1029e6b5313SAndrew Thompson     RUM_DEV(CISCOLINKSYS, WUSB54GR),
1039e6b5313SAndrew Thompson     RUM_DEV(CONCEPTRONIC2, C54RU2),
1049e6b5313SAndrew Thompson     RUM_DEV(COREGA, CGWLUSB2GL),
1059e6b5313SAndrew Thompson     RUM_DEV(COREGA, CGWLUSB2GPX),
1069e6b5313SAndrew Thompson     RUM_DEV(DICKSMITH, CWD854F),
1079e6b5313SAndrew Thompson     RUM_DEV(DICKSMITH, RT2573),
108031c7388SAndrew Thompson     RUM_DEV(EDIMAX, EW7318USG),
1099e6b5313SAndrew Thompson     RUM_DEV(DLINK2, DWLG122C1),
1109e6b5313SAndrew Thompson     RUM_DEV(DLINK2, WUA1340),
1119e6b5313SAndrew Thompson     RUM_DEV(DLINK2, DWA111),
1129e6b5313SAndrew Thompson     RUM_DEV(DLINK2, DWA110),
1139e6b5313SAndrew Thompson     RUM_DEV(GIGABYTE, GNWB01GS),
1149e6b5313SAndrew Thompson     RUM_DEV(GIGABYTE, GNWI05GS),
1159e6b5313SAndrew Thompson     RUM_DEV(GIGASET, RT2573),
1169e6b5313SAndrew Thompson     RUM_DEV(GOODWAY, RT2573),
1179e6b5313SAndrew Thompson     RUM_DEV(GUILLEMOT, HWGUSB254LB),
1189e6b5313SAndrew Thompson     RUM_DEV(GUILLEMOT, HWGUSB254V2AP),
1199e6b5313SAndrew Thompson     RUM_DEV(HUAWEI3COM, WUB320G),
1209e6b5313SAndrew Thompson     RUM_DEV(MELCO, G54HP),
1219e6b5313SAndrew Thompson     RUM_DEV(MELCO, SG54HP),
1223232aae3SHans Petter Selasky     RUM_DEV(MELCO, SG54HG),
12354de08a9SMIHIRA Sanpei Yoshiro     RUM_DEV(MELCO, WLIUCG),
124e11ad60dSHans Petter Selasky     RUM_DEV(MELCO, WLRUCG),
125e11ad60dSHans Petter Selasky     RUM_DEV(MELCO, WLRUCGAOSS),
1269e6b5313SAndrew Thompson     RUM_DEV(MSI, RT2573_1),
1279e6b5313SAndrew Thompson     RUM_DEV(MSI, RT2573_2),
1289e6b5313SAndrew Thompson     RUM_DEV(MSI, RT2573_3),
1299e6b5313SAndrew Thompson     RUM_DEV(MSI, RT2573_4),
1309e6b5313SAndrew Thompson     RUM_DEV(NOVATECH, RT2573),
1319e6b5313SAndrew Thompson     RUM_DEV(PLANEX2, GWUS54HP),
1329e6b5313SAndrew Thompson     RUM_DEV(PLANEX2, GWUS54MINI2),
1339e6b5313SAndrew Thompson     RUM_DEV(PLANEX2, GWUSMM),
1349e6b5313SAndrew Thompson     RUM_DEV(QCOM, RT2573),
1359e6b5313SAndrew Thompson     RUM_DEV(QCOM, RT2573_2),
1369e6b5313SAndrew Thompson     RUM_DEV(QCOM, RT2573_3),
1379e6b5313SAndrew Thompson     RUM_DEV(RALINK, RT2573),
1389e6b5313SAndrew Thompson     RUM_DEV(RALINK, RT2573_2),
1399e6b5313SAndrew Thompson     RUM_DEV(RALINK, RT2671),
1409e6b5313SAndrew Thompson     RUM_DEV(SITECOMEU, WL113R2),
1419e6b5313SAndrew Thompson     RUM_DEV(SITECOMEU, WL172),
1429e6b5313SAndrew Thompson     RUM_DEV(SPARKLAN, RT2573),
1439e6b5313SAndrew Thompson     RUM_DEV(SURECOM, RT2573),
1449e6b5313SAndrew Thompson #undef RUM_DEV
14502ac6454SAndrew Thompson };
14602ac6454SAndrew Thompson 
14702ac6454SAndrew Thompson static device_probe_t rum_match;
14802ac6454SAndrew Thompson static device_attach_t rum_attach;
14902ac6454SAndrew Thompson static device_detach_t rum_detach;
15002ac6454SAndrew Thompson 
151e0a69b51SAndrew Thompson static usb_callback_t rum_bulk_read_callback;
152e0a69b51SAndrew Thompson static usb_callback_t rum_bulk_write_callback;
15302ac6454SAndrew Thompson 
154e0a69b51SAndrew Thompson static usb_error_t	rum_do_request(struct rum_softc *sc,
155760bc48eSAndrew Thompson 			    struct usb_device_request *req, void *data);
15602ac6454SAndrew Thompson static struct ieee80211vap *rum_vap_create(struct ieee80211com *,
157fcd9500fSBernhard Schmidt 			    const char [IFNAMSIZ], int, enum ieee80211_opmode,
158fcd9500fSBernhard Schmidt 			    int, const uint8_t [IEEE80211_ADDR_LEN],
159fcd9500fSBernhard Schmidt 			    const uint8_t [IEEE80211_ADDR_LEN]);
16002ac6454SAndrew Thompson static void		rum_vap_delete(struct ieee80211vap *);
161f28c2f5eSAdrian Chadd static void		rum_cmdq_cb(void *, int);
162f28c2f5eSAdrian Chadd static int		rum_cmd_sleepable(struct rum_softc *, const void *,
163f28c2f5eSAdrian Chadd 			    size_t, uint8_t, uint8_t, CMD_FUNC_PROTO);
16402ac6454SAndrew Thompson static void		rum_tx_free(struct rum_tx_data *, int);
16502ac6454SAndrew Thompson static void		rum_setup_tx_list(struct rum_softc *);
16602ac6454SAndrew Thompson static void		rum_unsetup_tx_list(struct rum_softc *);
16702ac6454SAndrew Thompson static int		rum_newstate(struct ieee80211vap *,
16802ac6454SAndrew Thompson 			    enum ieee80211_state, int);
16902ac6454SAndrew Thompson static void		rum_setup_tx_desc(struct rum_softc *,
17002ac6454SAndrew Thompson 			    struct rum_tx_desc *, uint32_t, uint16_t, int,
17102ac6454SAndrew Thompson 			    int);
17202ac6454SAndrew Thompson static int		rum_tx_mgt(struct rum_softc *, struct mbuf *,
17302ac6454SAndrew Thompson 			    struct ieee80211_node *);
17402ac6454SAndrew Thompson static int		rum_tx_raw(struct rum_softc *, struct mbuf *,
17502ac6454SAndrew Thompson 			    struct ieee80211_node *,
17602ac6454SAndrew Thompson 			    const struct ieee80211_bpf_params *);
17702ac6454SAndrew Thompson static int		rum_tx_data(struct rum_softc *, struct mbuf *,
17802ac6454SAndrew Thompson 			    struct ieee80211_node *);
1797a79cebfSGleb Smirnoff static int		rum_transmit(struct ieee80211com *, struct mbuf *);
1807a79cebfSGleb Smirnoff static void		rum_start(struct rum_softc *);
1817a79cebfSGleb Smirnoff static void		rum_parent(struct ieee80211com *);
18202ac6454SAndrew Thompson static void		rum_eeprom_read(struct rum_softc *, uint16_t, void *,
18302ac6454SAndrew Thompson 			    int);
18402ac6454SAndrew Thompson static uint32_t		rum_read(struct rum_softc *, uint16_t);
18502ac6454SAndrew Thompson static void		rum_read_multi(struct rum_softc *, uint16_t, void *,
18602ac6454SAndrew Thompson 			    int);
187e0a69b51SAndrew Thompson static usb_error_t	rum_write(struct rum_softc *, uint16_t, uint32_t);
188e0a69b51SAndrew Thompson static usb_error_t	rum_write_multi(struct rum_softc *, uint16_t, void *,
18902ac6454SAndrew Thompson 			    size_t);
190a05022b2SAdrian Chadd static usb_error_t	rum_setbits(struct rum_softc *, uint16_t, uint32_t);
191a05022b2SAdrian Chadd static usb_error_t	rum_clrbits(struct rum_softc *, uint16_t, uint32_t);
192a05022b2SAdrian Chadd static usb_error_t	rum_modbits(struct rum_softc *, uint16_t, uint32_t,
193a05022b2SAdrian Chadd 			    uint32_t);
194cc9ae761SAdrian Chadd static int		rum_bbp_busy(struct rum_softc *);
19502ac6454SAndrew Thompson static void		rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
19602ac6454SAndrew Thompson static uint8_t		rum_bbp_read(struct rum_softc *, uint8_t);
19702ac6454SAndrew Thompson static void		rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
19802ac6454SAndrew Thompson static void		rum_select_antenna(struct rum_softc *);
19902ac6454SAndrew Thompson static void		rum_enable_mrr(struct rum_softc *);
20002ac6454SAndrew Thompson static void		rum_set_txpreamble(struct rum_softc *);
20102ac6454SAndrew Thompson static void		rum_set_basicrates(struct rum_softc *);
20202ac6454SAndrew Thompson static void		rum_select_band(struct rum_softc *,
20302ac6454SAndrew Thompson 			    struct ieee80211_channel *);
20402ac6454SAndrew Thompson static void		rum_set_chan(struct rum_softc *,
20502ac6454SAndrew Thompson 			    struct ieee80211_channel *);
20602ac6454SAndrew Thompson static void		rum_enable_tsf_sync(struct rum_softc *);
2075463c4a4SSam Leffler static void		rum_enable_tsf(struct rum_softc *);
2086d804321SAdrian Chadd static void		rum_abort_tsf_sync(struct rum_softc *);
209a6ccd477SAdrian Chadd static void		rum_get_tsf(struct rum_softc *, uint64_t *);
2107a79cebfSGleb Smirnoff static void		rum_update_slot(struct rum_softc *);
21102ac6454SAndrew Thompson static void		rum_set_bssid(struct rum_softc *, const uint8_t *);
21202ac6454SAndrew Thompson static void		rum_set_macaddr(struct rum_softc *, const uint8_t *);
213272f6adeSGleb Smirnoff static void		rum_update_mcast(struct ieee80211com *);
214272f6adeSGleb Smirnoff static void		rum_update_promisc(struct ieee80211com *);
2155efea30fSAndrew Thompson static void		rum_setpromisc(struct rum_softc *);
21602ac6454SAndrew Thompson static const char	*rum_get_rf(int);
21702ac6454SAndrew Thompson static void		rum_read_eeprom(struct rum_softc *);
21802ac6454SAndrew Thompson static int		rum_bbp_init(struct rum_softc *);
2197a79cebfSGleb Smirnoff static void		rum_init(struct rum_softc *);
2205efea30fSAndrew Thompson static void		rum_stop(struct rum_softc *);
22177ddf3d3SAndrew Thompson static void		rum_load_microcode(struct rum_softc *, const uint8_t *,
22202ac6454SAndrew Thompson 			    size_t);
223f620e64dSKevin Lo static void		rum_prepare_beacon(struct rum_softc *,
22402ac6454SAndrew Thompson 			    struct ieee80211vap *);
22502ac6454SAndrew Thompson static int		rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
22602ac6454SAndrew Thompson 			    const struct ieee80211_bpf_params *);
22702ac6454SAndrew Thompson static void		rum_scan_start(struct ieee80211com *);
22802ac6454SAndrew Thompson static void		rum_scan_end(struct ieee80211com *);
22902ac6454SAndrew Thompson static void		rum_set_channel(struct ieee80211com *);
23002ac6454SAndrew Thompson static int		rum_get_rssi(struct rum_softc *, uint8_t);
231b6108616SRui Paulo static void		rum_ratectl_start(struct rum_softc *,
23202ac6454SAndrew Thompson 			    struct ieee80211_node *);
233b6108616SRui Paulo static void		rum_ratectl_timeout(void *);
234b6108616SRui Paulo static void		rum_ratectl_task(void *, int);
23502ac6454SAndrew Thompson static int		rum_pause(struct rum_softc *, int);
23602ac6454SAndrew Thompson 
23702ac6454SAndrew Thompson static const struct {
23802ac6454SAndrew Thompson 	uint32_t	reg;
23902ac6454SAndrew Thompson 	uint32_t	val;
24002ac6454SAndrew Thompson } rum_def_mac[] = {
24102ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR0,  0x025fb032 },
24202ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR1,  0x9eaa9eaf },
24302ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR2,  0x8a8b8c8d },
24402ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR3,  0x00858687 },
24502ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR7,  0x2e31353b },
24602ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR8,  0x2a2a2a2c },
24702ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR15, 0x0000000f },
24802ac6454SAndrew Thompson 	{ RT2573_MAC_CSR6,   0x00000fff },
24902ac6454SAndrew Thompson 	{ RT2573_MAC_CSR8,   0x016c030a },
25002ac6454SAndrew Thompson 	{ RT2573_MAC_CSR10,  0x00000718 },
25102ac6454SAndrew Thompson 	{ RT2573_MAC_CSR12,  0x00000004 },
25202ac6454SAndrew Thompson 	{ RT2573_MAC_CSR13,  0x00007f00 },
25302ac6454SAndrew Thompson 	{ RT2573_SEC_CSR0,   0x00000000 },
25402ac6454SAndrew Thompson 	{ RT2573_SEC_CSR1,   0x00000000 },
25502ac6454SAndrew Thompson 	{ RT2573_SEC_CSR5,   0x00000000 },
25602ac6454SAndrew Thompson 	{ RT2573_PHY_CSR1,   0x000023b0 },
25702ac6454SAndrew Thompson 	{ RT2573_PHY_CSR5,   0x00040a06 },
25802ac6454SAndrew Thompson 	{ RT2573_PHY_CSR6,   0x00080606 },
25902ac6454SAndrew Thompson 	{ RT2573_PHY_CSR7,   0x00000408 },
26002ac6454SAndrew Thompson 	{ RT2573_AIFSN_CSR,  0x00002273 },
26102ac6454SAndrew Thompson 	{ RT2573_CWMIN_CSR,  0x00002344 },
26202ac6454SAndrew Thompson 	{ RT2573_CWMAX_CSR,  0x000034aa }
26302ac6454SAndrew Thompson };
26402ac6454SAndrew Thompson 
26502ac6454SAndrew Thompson static const struct {
26602ac6454SAndrew Thompson 	uint8_t	reg;
26702ac6454SAndrew Thompson 	uint8_t	val;
26802ac6454SAndrew Thompson } rum_def_bbp[] = {
26902ac6454SAndrew Thompson 	{   3, 0x80 },
27002ac6454SAndrew Thompson 	{  15, 0x30 },
27102ac6454SAndrew Thompson 	{  17, 0x20 },
27202ac6454SAndrew Thompson 	{  21, 0xc8 },
27302ac6454SAndrew Thompson 	{  22, 0x38 },
27402ac6454SAndrew Thompson 	{  23, 0x06 },
27502ac6454SAndrew Thompson 	{  24, 0xfe },
27602ac6454SAndrew Thompson 	{  25, 0x0a },
27702ac6454SAndrew Thompson 	{  26, 0x0d },
27802ac6454SAndrew Thompson 	{  32, 0x0b },
27902ac6454SAndrew Thompson 	{  34, 0x12 },
28002ac6454SAndrew Thompson 	{  37, 0x07 },
28102ac6454SAndrew Thompson 	{  39, 0xf8 },
28202ac6454SAndrew Thompson 	{  41, 0x60 },
28302ac6454SAndrew Thompson 	{  53, 0x10 },
28402ac6454SAndrew Thompson 	{  54, 0x18 },
28502ac6454SAndrew Thompson 	{  60, 0x10 },
28602ac6454SAndrew Thompson 	{  61, 0x04 },
28702ac6454SAndrew Thompson 	{  62, 0x04 },
28802ac6454SAndrew Thompson 	{  75, 0xfe },
28902ac6454SAndrew Thompson 	{  86, 0xfe },
29002ac6454SAndrew Thompson 	{  88, 0xfe },
29102ac6454SAndrew Thompson 	{  90, 0x0f },
29202ac6454SAndrew Thompson 	{  99, 0x00 },
29302ac6454SAndrew Thompson 	{ 102, 0x16 },
29402ac6454SAndrew Thompson 	{ 107, 0x04 }
29502ac6454SAndrew Thompson };
29602ac6454SAndrew Thompson 
29702ac6454SAndrew Thompson static const struct rfprog {
29802ac6454SAndrew Thompson 	uint8_t		chan;
29902ac6454SAndrew Thompson 	uint32_t	r1, r2, r3, r4;
30002ac6454SAndrew Thompson }  rum_rf5226[] = {
30102ac6454SAndrew Thompson 	{   1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
30202ac6454SAndrew Thompson 	{   2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
30302ac6454SAndrew Thompson 	{   3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
30402ac6454SAndrew Thompson 	{   4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
30502ac6454SAndrew Thompson 	{   5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
30602ac6454SAndrew Thompson 	{   6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
30702ac6454SAndrew Thompson 	{   7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
30802ac6454SAndrew Thompson 	{   8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
30902ac6454SAndrew Thompson 	{   9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
31002ac6454SAndrew Thompson 	{  10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
31102ac6454SAndrew Thompson 	{  11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
31202ac6454SAndrew Thompson 	{  12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
31302ac6454SAndrew Thompson 	{  13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
31402ac6454SAndrew Thompson 	{  14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
31502ac6454SAndrew Thompson 
31602ac6454SAndrew Thompson 	{  34, 0x00b03, 0x20266, 0x36014, 0x30282 },
31702ac6454SAndrew Thompson 	{  38, 0x00b03, 0x20267, 0x36014, 0x30284 },
31802ac6454SAndrew Thompson 	{  42, 0x00b03, 0x20268, 0x36014, 0x30286 },
31902ac6454SAndrew Thompson 	{  46, 0x00b03, 0x20269, 0x36014, 0x30288 },
32002ac6454SAndrew Thompson 
32102ac6454SAndrew Thompson 	{  36, 0x00b03, 0x00266, 0x26014, 0x30288 },
32202ac6454SAndrew Thompson 	{  40, 0x00b03, 0x00268, 0x26014, 0x30280 },
32302ac6454SAndrew Thompson 	{  44, 0x00b03, 0x00269, 0x26014, 0x30282 },
32402ac6454SAndrew Thompson 	{  48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
32502ac6454SAndrew Thompson 	{  52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
32602ac6454SAndrew Thompson 	{  56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
32702ac6454SAndrew Thompson 	{  60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
32802ac6454SAndrew Thompson 	{  64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
32902ac6454SAndrew Thompson 
33002ac6454SAndrew Thompson 	{ 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
33102ac6454SAndrew Thompson 	{ 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
33202ac6454SAndrew Thompson 	{ 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
33302ac6454SAndrew Thompson 	{ 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
33402ac6454SAndrew Thompson 	{ 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
33502ac6454SAndrew Thompson 	{ 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
33602ac6454SAndrew Thompson 	{ 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
33702ac6454SAndrew Thompson 	{ 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
33802ac6454SAndrew Thompson 	{ 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
33902ac6454SAndrew Thompson 	{ 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
34002ac6454SAndrew Thompson 	{ 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
34102ac6454SAndrew Thompson 
34202ac6454SAndrew Thompson 	{ 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
34302ac6454SAndrew Thompson 	{ 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
34402ac6454SAndrew Thompson 	{ 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
34502ac6454SAndrew Thompson 	{ 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
34602ac6454SAndrew Thompson 	{ 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
34702ac6454SAndrew Thompson }, rum_rf5225[] = {
34802ac6454SAndrew Thompson 	{   1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
34902ac6454SAndrew Thompson 	{   2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
35002ac6454SAndrew Thompson 	{   3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
35102ac6454SAndrew Thompson 	{   4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
35202ac6454SAndrew Thompson 	{   5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
35302ac6454SAndrew Thompson 	{   6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
35402ac6454SAndrew Thompson 	{   7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
35502ac6454SAndrew Thompson 	{   8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
35602ac6454SAndrew Thompson 	{   9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
35702ac6454SAndrew Thompson 	{  10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
35802ac6454SAndrew Thompson 	{  11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
35902ac6454SAndrew Thompson 	{  12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
36002ac6454SAndrew Thompson 	{  13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
36102ac6454SAndrew Thompson 	{  14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
36202ac6454SAndrew Thompson 
36302ac6454SAndrew Thompson 	{  34, 0x00b33, 0x01266, 0x26014, 0x30282 },
36402ac6454SAndrew Thompson 	{  38, 0x00b33, 0x01267, 0x26014, 0x30284 },
36502ac6454SAndrew Thompson 	{  42, 0x00b33, 0x01268, 0x26014, 0x30286 },
36602ac6454SAndrew Thompson 	{  46, 0x00b33, 0x01269, 0x26014, 0x30288 },
36702ac6454SAndrew Thompson 
36802ac6454SAndrew Thompson 	{  36, 0x00b33, 0x01266, 0x26014, 0x30288 },
36902ac6454SAndrew Thompson 	{  40, 0x00b33, 0x01268, 0x26014, 0x30280 },
37002ac6454SAndrew Thompson 	{  44, 0x00b33, 0x01269, 0x26014, 0x30282 },
37102ac6454SAndrew Thompson 	{  48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
37202ac6454SAndrew Thompson 	{  52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
37302ac6454SAndrew Thompson 	{  56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
37402ac6454SAndrew Thompson 	{  60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
37502ac6454SAndrew Thompson 	{  64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
37602ac6454SAndrew Thompson 
37702ac6454SAndrew Thompson 	{ 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
37802ac6454SAndrew Thompson 	{ 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
37902ac6454SAndrew Thompson 	{ 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
38002ac6454SAndrew Thompson 	{ 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
38102ac6454SAndrew Thompson 	{ 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
38202ac6454SAndrew Thompson 	{ 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
38302ac6454SAndrew Thompson 	{ 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
38402ac6454SAndrew Thompson 	{ 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
38502ac6454SAndrew Thompson 	{ 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
38602ac6454SAndrew Thompson 	{ 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
38702ac6454SAndrew Thompson 	{ 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
38802ac6454SAndrew Thompson 
38902ac6454SAndrew Thompson 	{ 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
39002ac6454SAndrew Thompson 	{ 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
39102ac6454SAndrew Thompson 	{ 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
39202ac6454SAndrew Thompson 	{ 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
39302ac6454SAndrew Thompson 	{ 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
39402ac6454SAndrew Thompson };
39502ac6454SAndrew Thompson 
396760bc48eSAndrew Thompson static const struct usb_config rum_config[RUM_N_TRANSFER] = {
39702ac6454SAndrew Thompson 	[RUM_BULK_WR] = {
39802ac6454SAndrew Thompson 		.type = UE_BULK,
39902ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
40002ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
4014eae601eSAndrew Thompson 		.bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
4024eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
4034eae601eSAndrew Thompson 		.callback = rum_bulk_write_callback,
4044eae601eSAndrew Thompson 		.timeout = 5000,	/* ms */
40502ac6454SAndrew Thompson 	},
40602ac6454SAndrew Thompson 	[RUM_BULK_RD] = {
40702ac6454SAndrew Thompson 		.type = UE_BULK,
40802ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
40902ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
4104eae601eSAndrew Thompson 		.bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
4114eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
4124eae601eSAndrew Thompson 		.callback = rum_bulk_read_callback,
41302ac6454SAndrew Thompson 	},
41402ac6454SAndrew Thompson };
41502ac6454SAndrew Thompson 
41602ac6454SAndrew Thompson static int
41702ac6454SAndrew Thompson rum_match(device_t self)
41802ac6454SAndrew Thompson {
419760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(self);
42002ac6454SAndrew Thompson 
421f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST)
42202ac6454SAndrew Thompson 		return (ENXIO);
42302ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != 0)
42402ac6454SAndrew Thompson 		return (ENXIO);
42502ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
42602ac6454SAndrew Thompson 		return (ENXIO);
42702ac6454SAndrew Thompson 
428a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
42902ac6454SAndrew Thompson }
43002ac6454SAndrew Thompson 
43102ac6454SAndrew Thompson static int
43202ac6454SAndrew Thompson rum_attach(device_t self)
43302ac6454SAndrew Thompson {
434760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(self);
43502ac6454SAndrew Thompson 	struct rum_softc *sc = device_get_softc(self);
4367a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
4375efea30fSAndrew Thompson 	uint8_t iface_index, bands;
4385efea30fSAndrew Thompson 	uint32_t tmp;
4395efea30fSAndrew Thompson 	int error, ntries;
44002ac6454SAndrew Thompson 
441a593f6b8SAndrew Thompson 	device_set_usb_desc(self);
44202ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
44302ac6454SAndrew Thompson 	sc->sc_dev = self;
44402ac6454SAndrew Thompson 
445f28c2f5eSAdrian Chadd 	RUM_LOCK_INIT(sc);
446f28c2f5eSAdrian Chadd 	RUM_CMDQ_LOCK_INIT(sc);
4477a79cebfSGleb Smirnoff 	mbufq_init(&sc->sc_snd, ifqmaxlen);
44802ac6454SAndrew Thompson 
44902ac6454SAndrew Thompson 	iface_index = RT2573_IFACE_INDEX;
450a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device, &iface_index,
45102ac6454SAndrew Thompson 	    sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
45202ac6454SAndrew Thompson 	if (error) {
45302ac6454SAndrew Thompson 		device_printf(self, "could not allocate USB transfers, "
454a593f6b8SAndrew Thompson 		    "err=%s\n", usbd_errstr(error));
45502ac6454SAndrew Thompson 		goto detach;
45602ac6454SAndrew Thompson 	}
45702ac6454SAndrew Thompson 
45802ac6454SAndrew Thompson 	RUM_LOCK(sc);
45902ac6454SAndrew Thompson 	/* retrieve RT2573 rev. no */
46002ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
46102ac6454SAndrew Thompson 		if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
46202ac6454SAndrew Thompson 			break;
46302ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
46402ac6454SAndrew Thompson 			break;
46502ac6454SAndrew Thompson 	}
46602ac6454SAndrew Thompson 	if (ntries == 100) {
46702ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
4685efea30fSAndrew Thompson 		RUM_UNLOCK(sc);
4695efea30fSAndrew Thompson 		goto detach;
47002ac6454SAndrew Thompson 	}
47102ac6454SAndrew Thompson 
47202ac6454SAndrew Thompson 	/* retrieve MAC address and various other things from EEPROM */
47302ac6454SAndrew Thompson 	rum_read_eeprom(sc);
47402ac6454SAndrew Thompson 
47502ac6454SAndrew Thompson 	device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
47602ac6454SAndrew Thompson 	    tmp, rum_get_rf(sc->rf_rev));
47702ac6454SAndrew Thompson 
47877ddf3d3SAndrew Thompson 	rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
47902ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
48002ac6454SAndrew Thompson 
48159686fe9SGleb Smirnoff 	ic->ic_softc = sc;
482c8550c02SGleb Smirnoff 	ic->ic_name = device_get_nameunit(self);
48302ac6454SAndrew Thompson 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
48402ac6454SAndrew Thompson 
48502ac6454SAndrew Thompson 	/* set device capabilities */
48602ac6454SAndrew Thompson 	ic->ic_caps =
48702ac6454SAndrew Thompson 	      IEEE80211_C_STA		/* station mode supported */
48802ac6454SAndrew Thompson 	    | IEEE80211_C_IBSS		/* IBSS mode supported */
48902ac6454SAndrew Thompson 	    | IEEE80211_C_MONITOR	/* monitor mode supported */
49002ac6454SAndrew Thompson 	    | IEEE80211_C_HOSTAP	/* HostAp mode supported */
49102ac6454SAndrew Thompson 	    | IEEE80211_C_TXPMGT	/* tx power management */
49202ac6454SAndrew Thompson 	    | IEEE80211_C_SHPREAMBLE	/* short preamble supported */
49302ac6454SAndrew Thompson 	    | IEEE80211_C_SHSLOT	/* short slot time supported */
49402ac6454SAndrew Thompson 	    | IEEE80211_C_BGSCAN	/* bg scanning supported */
49502ac6454SAndrew Thompson 	    | IEEE80211_C_WPA		/* 802.11i */
49602ac6454SAndrew Thompson 	    ;
49702ac6454SAndrew Thompson 
49802ac6454SAndrew Thompson 	bands = 0;
49902ac6454SAndrew Thompson 	setbit(&bands, IEEE80211_MODE_11B);
50002ac6454SAndrew Thompson 	setbit(&bands, IEEE80211_MODE_11G);
50102ac6454SAndrew Thompson 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226)
50202ac6454SAndrew Thompson 		setbit(&bands, IEEE80211_MODE_11A);
50302ac6454SAndrew Thompson 	ieee80211_init_channels(ic, NULL, &bands);
50402ac6454SAndrew Thompson 
5057a79cebfSGleb Smirnoff 	ieee80211_ifattach(ic);
50677ddf3d3SAndrew Thompson 	ic->ic_update_promisc = rum_update_promisc;
50702ac6454SAndrew Thompson 	ic->ic_raw_xmit = rum_raw_xmit;
50802ac6454SAndrew Thompson 	ic->ic_scan_start = rum_scan_start;
50902ac6454SAndrew Thompson 	ic->ic_scan_end = rum_scan_end;
51002ac6454SAndrew Thompson 	ic->ic_set_channel = rum_set_channel;
5117a79cebfSGleb Smirnoff 	ic->ic_transmit = rum_transmit;
5127a79cebfSGleb Smirnoff 	ic->ic_parent = rum_parent;
51302ac6454SAndrew Thompson 	ic->ic_vap_create = rum_vap_create;
51402ac6454SAndrew Thompson 	ic->ic_vap_delete = rum_vap_delete;
5156dd8e071SEd Maste 	ic->ic_update_mcast = rum_update_mcast;
51602ac6454SAndrew Thompson 
5175463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
5185463c4a4SSam Leffler 	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
5195463c4a4SSam Leffler 		RT2573_TX_RADIOTAP_PRESENT,
5205463c4a4SSam Leffler 	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
5215463c4a4SSam Leffler 		RT2573_RX_RADIOTAP_PRESENT);
52202ac6454SAndrew Thompson 
523f28c2f5eSAdrian Chadd 	TASK_INIT(&sc->cmdq_task, 0, rum_cmdq_cb, sc);
524f28c2f5eSAdrian Chadd 
52502ac6454SAndrew Thompson 	if (bootverbose)
52602ac6454SAndrew Thompson 		ieee80211_announce(ic);
52702ac6454SAndrew Thompson 
5285efea30fSAndrew Thompson 	return (0);
5295efea30fSAndrew Thompson 
5305efea30fSAndrew Thompson detach:
5315efea30fSAndrew Thompson 	rum_detach(self);
5325efea30fSAndrew Thompson 	return (ENXIO);			/* failure */
53302ac6454SAndrew Thompson }
53402ac6454SAndrew Thompson 
53502ac6454SAndrew Thompson static int
53602ac6454SAndrew Thompson rum_detach(device_t self)
53702ac6454SAndrew Thompson {
53802ac6454SAndrew Thompson 	struct rum_softc *sc = device_get_softc(self);
539e7b7db3dSAdrian Chadd 	struct ieee80211com *ic = &sc->sc_ic;
54002ac6454SAndrew Thompson 
541645e4d17SHans Petter Selasky 	/* Prevent further ioctls */
542645e4d17SHans Petter Selasky 	RUM_LOCK(sc);
543645e4d17SHans Petter Selasky 	sc->sc_detached = 1;
544645e4d17SHans Petter Selasky 	RUM_UNLOCK(sc);
545645e4d17SHans Petter Selasky 
54602ac6454SAndrew Thompson 	/* stop all USB transfers */
547a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
54802ac6454SAndrew Thompson 
54902ac6454SAndrew Thompson 	/* free TX list, if any */
55002ac6454SAndrew Thompson 	RUM_LOCK(sc);
55102ac6454SAndrew Thompson 	rum_unsetup_tx_list(sc);
55202ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
55302ac6454SAndrew Thompson 
554f28c2f5eSAdrian Chadd 	if (ic->ic_softc == sc) {
555f28c2f5eSAdrian Chadd 		ieee80211_draintask(ic, &sc->cmdq_task);
556e7b7db3dSAdrian Chadd 		ieee80211_ifdetach(ic);
557f28c2f5eSAdrian Chadd 	}
558f28c2f5eSAdrian Chadd 
5597a79cebfSGleb Smirnoff 	mbufq_drain(&sc->sc_snd);
560f28c2f5eSAdrian Chadd 	RUM_CMDQ_LOCK_DESTROY(sc);
561f28c2f5eSAdrian Chadd 	RUM_LOCK_DESTROY(sc);
562f28c2f5eSAdrian Chadd 
56302ac6454SAndrew Thompson 	return (0);
56402ac6454SAndrew Thompson }
56502ac6454SAndrew Thompson 
566e0a69b51SAndrew Thompson static usb_error_t
56777ddf3d3SAndrew Thompson rum_do_request(struct rum_softc *sc,
568760bc48eSAndrew Thompson     struct usb_device_request *req, void *data)
56977ddf3d3SAndrew Thompson {
570e0a69b51SAndrew Thompson 	usb_error_t err;
57177ddf3d3SAndrew Thompson 	int ntries = 10;
57277ddf3d3SAndrew Thompson 
57377ddf3d3SAndrew Thompson 	while (ntries--) {
574a593f6b8SAndrew Thompson 		err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
57577ddf3d3SAndrew Thompson 		    req, data, 0, NULL, 250 /* ms */);
57677ddf3d3SAndrew Thompson 		if (err == 0)
57777ddf3d3SAndrew Thompson 			break;
57877ddf3d3SAndrew Thompson 
57977ddf3d3SAndrew Thompson 		DPRINTFN(1, "Control request failed, %s (retrying)\n",
580a593f6b8SAndrew Thompson 		    usbd_errstr(err));
58177ddf3d3SAndrew Thompson 		if (rum_pause(sc, hz / 100))
58277ddf3d3SAndrew Thompson 			break;
58377ddf3d3SAndrew Thompson 	}
58477ddf3d3SAndrew Thompson 	return (err);
58577ddf3d3SAndrew Thompson }
58677ddf3d3SAndrew Thompson 
58702ac6454SAndrew Thompson static struct ieee80211vap *
588fcd9500fSBernhard Schmidt rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
589fcd9500fSBernhard Schmidt     enum ieee80211_opmode opmode, int flags,
59002ac6454SAndrew Thompson     const uint8_t bssid[IEEE80211_ADDR_LEN],
59102ac6454SAndrew Thompson     const uint8_t mac[IEEE80211_ADDR_LEN])
59202ac6454SAndrew Thompson {
593d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
59402ac6454SAndrew Thompson 	struct rum_vap *rvp;
59502ac6454SAndrew Thompson 	struct ieee80211vap *vap;
59602ac6454SAndrew Thompson 
59702ac6454SAndrew Thompson 	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
59802ac6454SAndrew Thompson 		return NULL;
5997a79cebfSGleb Smirnoff 	rvp = malloc(sizeof(struct rum_vap), M_80211_VAP, M_WAITOK | M_ZERO);
60002ac6454SAndrew Thompson 	vap = &rvp->vap;
60102ac6454SAndrew Thompson 	/* enable s/w bmiss handling for sta mode */
602bb2f69e8SHans Petter Selasky 
603bb2f69e8SHans Petter Selasky 	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
6047a79cebfSGleb Smirnoff 	    flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
605bb2f69e8SHans Petter Selasky 		/* out of memory */
606bb2f69e8SHans Petter Selasky 		free(rvp, M_80211_VAP);
607bb2f69e8SHans Petter Selasky 		return (NULL);
608bb2f69e8SHans Petter Selasky 	}
60902ac6454SAndrew Thompson 
61002ac6454SAndrew Thompson 	/* override state transition machine */
61102ac6454SAndrew Thompson 	rvp->newstate = vap->iv_newstate;
61202ac6454SAndrew Thompson 	vap->iv_newstate = rum_newstate;
61302ac6454SAndrew Thompson 
614b6108616SRui Paulo 	usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0);
615b6108616SRui Paulo 	TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
616b6108616SRui Paulo 	ieee80211_ratectl_init(vap);
617b6108616SRui Paulo 	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
61802ac6454SAndrew Thompson 	/* complete setup */
6197a79cebfSGleb Smirnoff 	ieee80211_vap_attach(vap, ieee80211_media_change,
6207a79cebfSGleb Smirnoff 	    ieee80211_media_status, mac);
62102ac6454SAndrew Thompson 	ic->ic_opmode = opmode;
62202ac6454SAndrew Thompson 	return vap;
62302ac6454SAndrew Thompson }
62402ac6454SAndrew Thompson 
62502ac6454SAndrew Thompson static void
62602ac6454SAndrew Thompson rum_vap_delete(struct ieee80211vap *vap)
62702ac6454SAndrew Thompson {
62802ac6454SAndrew Thompson 	struct rum_vap *rvp = RUM_VAP(vap);
6295efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
63002ac6454SAndrew Thompson 
631b6108616SRui Paulo 	usb_callout_drain(&rvp->ratectl_ch);
632b6108616SRui Paulo 	ieee80211_draintask(ic, &rvp->ratectl_task);
633b6108616SRui Paulo 	ieee80211_ratectl_deinit(vap);
63402ac6454SAndrew Thompson 	ieee80211_vap_detach(vap);
63502ac6454SAndrew Thompson 	free(rvp, M_80211_VAP);
63602ac6454SAndrew Thompson }
63702ac6454SAndrew Thompson 
63802ac6454SAndrew Thompson static void
639f28c2f5eSAdrian Chadd rum_cmdq_cb(void *arg, int pending)
640f28c2f5eSAdrian Chadd {
641f28c2f5eSAdrian Chadd 	struct rum_softc *sc = arg;
642f28c2f5eSAdrian Chadd 	struct rum_cmdq *rc;
643f28c2f5eSAdrian Chadd 
644f28c2f5eSAdrian Chadd 	RUM_CMDQ_LOCK(sc);
645f28c2f5eSAdrian Chadd 	while (sc->cmdq[sc->cmdq_first].func != NULL) {
646f28c2f5eSAdrian Chadd 		rc = &sc->cmdq[sc->cmdq_first];
647f28c2f5eSAdrian Chadd 		RUM_CMDQ_UNLOCK(sc);
648f28c2f5eSAdrian Chadd 
649f28c2f5eSAdrian Chadd 		RUM_LOCK(sc);
650f28c2f5eSAdrian Chadd 		rc->func(sc, &rc->data, rc->rn_id, rc->rvp_id);
651f28c2f5eSAdrian Chadd 		RUM_UNLOCK(sc);
652f28c2f5eSAdrian Chadd 
653f28c2f5eSAdrian Chadd 		RUM_CMDQ_LOCK(sc);
654f28c2f5eSAdrian Chadd 		memset(rc, 0, sizeof (*rc));
655f28c2f5eSAdrian Chadd 		sc->cmdq_first = (sc->cmdq_first + 1) % RUM_CMDQ_SIZE;
656f28c2f5eSAdrian Chadd 	}
657f28c2f5eSAdrian Chadd 	RUM_CMDQ_UNLOCK(sc);
658f28c2f5eSAdrian Chadd }
659f28c2f5eSAdrian Chadd 
660f28c2f5eSAdrian Chadd static int
661f28c2f5eSAdrian Chadd rum_cmd_sleepable(struct rum_softc *sc, const void *ptr, size_t len,
662f28c2f5eSAdrian Chadd     uint8_t rn_id, uint8_t rvp_id, CMD_FUNC_PROTO)
663f28c2f5eSAdrian Chadd {
664f28c2f5eSAdrian Chadd 	struct ieee80211com *ic = &sc->sc_ic;
665f28c2f5eSAdrian Chadd 
666f28c2f5eSAdrian Chadd 	KASSERT(len <= sizeof(union sec_param), ("buffer overflow"));
667f28c2f5eSAdrian Chadd 
668f28c2f5eSAdrian Chadd 	RUM_CMDQ_LOCK(sc);
669f28c2f5eSAdrian Chadd 	if (sc->cmdq[sc->cmdq_last].func != NULL) {
670f28c2f5eSAdrian Chadd 		device_printf(sc->sc_dev, "%s: cmdq overflow\n", __func__);
671f28c2f5eSAdrian Chadd 		RUM_CMDQ_UNLOCK(sc);
672f28c2f5eSAdrian Chadd 
673f28c2f5eSAdrian Chadd 		return EAGAIN;
674f28c2f5eSAdrian Chadd 	}
675f28c2f5eSAdrian Chadd 
676f28c2f5eSAdrian Chadd 	if (ptr != NULL)
677f28c2f5eSAdrian Chadd 		memcpy(&sc->cmdq[sc->cmdq_last].data, ptr, len);
678f28c2f5eSAdrian Chadd 	sc->cmdq[sc->cmdq_last].rn_id = rn_id;
679f28c2f5eSAdrian Chadd 	sc->cmdq[sc->cmdq_last].rvp_id = rvp_id;
680f28c2f5eSAdrian Chadd 	sc->cmdq[sc->cmdq_last].func = func;
681f28c2f5eSAdrian Chadd 	sc->cmdq_last = (sc->cmdq_last + 1) % RUM_CMDQ_SIZE;
682f28c2f5eSAdrian Chadd 	RUM_CMDQ_UNLOCK(sc);
683f28c2f5eSAdrian Chadd 
684f28c2f5eSAdrian Chadd 	ieee80211_runtask(ic, &sc->cmdq_task);
685f28c2f5eSAdrian Chadd 
686f28c2f5eSAdrian Chadd 	return 0;
687f28c2f5eSAdrian Chadd }
688f28c2f5eSAdrian Chadd 
689f28c2f5eSAdrian Chadd static void
69002ac6454SAndrew Thompson rum_tx_free(struct rum_tx_data *data, int txerr)
69102ac6454SAndrew Thompson {
69202ac6454SAndrew Thompson 	struct rum_softc *sc = data->sc;
69302ac6454SAndrew Thompson 
69402ac6454SAndrew Thompson 	if (data->m != NULL) {
6957a79cebfSGleb Smirnoff 		ieee80211_tx_complete(data->ni, data->m, txerr);
69602ac6454SAndrew Thompson 		data->m = NULL;
69702ac6454SAndrew Thompson 		data->ni = NULL;
69802ac6454SAndrew Thompson 	}
69902ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
70002ac6454SAndrew Thompson 	sc->tx_nfree++;
70102ac6454SAndrew Thompson }
70202ac6454SAndrew Thompson 
70302ac6454SAndrew Thompson static void
70402ac6454SAndrew Thompson rum_setup_tx_list(struct rum_softc *sc)
70502ac6454SAndrew Thompson {
70602ac6454SAndrew Thompson 	struct rum_tx_data *data;
70702ac6454SAndrew Thompson 	int i;
70802ac6454SAndrew Thompson 
70902ac6454SAndrew Thompson 	sc->tx_nfree = 0;
71002ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_q);
71102ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_free);
71202ac6454SAndrew Thompson 
71302ac6454SAndrew Thompson 	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
71402ac6454SAndrew Thompson 		data = &sc->tx_data[i];
71502ac6454SAndrew Thompson 
71602ac6454SAndrew Thompson 		data->sc = sc;
71702ac6454SAndrew Thompson 		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
71802ac6454SAndrew Thompson 		sc->tx_nfree++;
71902ac6454SAndrew Thompson 	}
72002ac6454SAndrew Thompson }
72102ac6454SAndrew Thompson 
72202ac6454SAndrew Thompson static void
72302ac6454SAndrew Thompson rum_unsetup_tx_list(struct rum_softc *sc)
72402ac6454SAndrew Thompson {
72502ac6454SAndrew Thompson 	struct rum_tx_data *data;
72602ac6454SAndrew Thompson 	int i;
72702ac6454SAndrew Thompson 
72802ac6454SAndrew Thompson 	/* make sure any subsequent use of the queues will fail */
72902ac6454SAndrew Thompson 	sc->tx_nfree = 0;
73002ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_q);
73102ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_free);
73202ac6454SAndrew Thompson 
73302ac6454SAndrew Thompson 	/* free up all node references and mbufs */
73402ac6454SAndrew Thompson 	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
73502ac6454SAndrew Thompson 		data = &sc->tx_data[i];
73602ac6454SAndrew Thompson 
73702ac6454SAndrew Thompson 		if (data->m != NULL) {
73802ac6454SAndrew Thompson 			m_freem(data->m);
73902ac6454SAndrew Thompson 			data->m = NULL;
74002ac6454SAndrew Thompson 		}
74102ac6454SAndrew Thompson 		if (data->ni != NULL) {
74202ac6454SAndrew Thompson 			ieee80211_free_node(data->ni);
74302ac6454SAndrew Thompson 			data->ni = NULL;
74402ac6454SAndrew Thompson 		}
74502ac6454SAndrew Thompson 	}
74602ac6454SAndrew Thompson }
74702ac6454SAndrew Thompson 
7485efea30fSAndrew Thompson static int
7495efea30fSAndrew Thompson rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
75002ac6454SAndrew Thompson {
75102ac6454SAndrew Thompson 	struct rum_vap *rvp = RUM_VAP(vap);
7525efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
753d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
75402ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
75502ac6454SAndrew Thompson 	enum ieee80211_state ostate;
75602ac6454SAndrew Thompson 	struct ieee80211_node *ni;
75702ac6454SAndrew Thompson 
75802ac6454SAndrew Thompson 	ostate = vap->iv_state;
7595efea30fSAndrew Thompson 	DPRINTF("%s -> %s\n",
7605efea30fSAndrew Thompson 		ieee80211_state_name[ostate],
7615efea30fSAndrew Thompson 		ieee80211_state_name[nstate]);
76202ac6454SAndrew Thompson 
7635efea30fSAndrew Thompson 	IEEE80211_UNLOCK(ic);
7645efea30fSAndrew Thompson 	RUM_LOCK(sc);
765b6108616SRui Paulo 	usb_callout_stop(&rvp->ratectl_ch);
7665efea30fSAndrew Thompson 
7675efea30fSAndrew Thompson 	switch (nstate) {
76802ac6454SAndrew Thompson 	case IEEE80211_S_INIT:
7696d804321SAdrian Chadd 		if (ostate == IEEE80211_S_RUN)
7706d804321SAdrian Chadd 			rum_abort_tsf_sync(sc);
7716d804321SAdrian Chadd 
77202ac6454SAndrew Thompson 		break;
77302ac6454SAndrew Thompson 
77402ac6454SAndrew Thompson 	case IEEE80211_S_RUN:
7758d8bdb01SAndrew Thompson 		ni = ieee80211_ref_node(vap->iv_bss);
77602ac6454SAndrew Thompson 
77702ac6454SAndrew Thompson 		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
778bb571462SHans Petter Selasky 			if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) {
779bb571462SHans Petter Selasky 				RUM_UNLOCK(sc);
780bb571462SHans Petter Selasky 				IEEE80211_LOCK(ic);
781bb571462SHans Petter Selasky 				ieee80211_free_node(ni);
782bb571462SHans Petter Selasky 				return (-1);
783bb571462SHans Petter Selasky 			}
7847a79cebfSGleb Smirnoff 			rum_update_slot(sc);
78502ac6454SAndrew Thompson 			rum_enable_mrr(sc);
78602ac6454SAndrew Thompson 			rum_set_txpreamble(sc);
78702ac6454SAndrew Thompson 			rum_set_basicrates(sc);
7887a79cebfSGleb Smirnoff 			IEEE80211_ADDR_COPY(ic->ic_macaddr, ni->ni_bssid);
7897a79cebfSGleb Smirnoff 			rum_set_bssid(sc, ic->ic_macaddr);
79002ac6454SAndrew Thompson 		}
79102ac6454SAndrew Thompson 
79202ac6454SAndrew Thompson 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
79302ac6454SAndrew Thompson 		    vap->iv_opmode == IEEE80211_M_IBSS)
79402ac6454SAndrew Thompson 			rum_prepare_beacon(sc, vap);
79502ac6454SAndrew Thompson 
79602ac6454SAndrew Thompson 		if (vap->iv_opmode != IEEE80211_M_MONITOR)
79702ac6454SAndrew Thompson 			rum_enable_tsf_sync(sc);
7985463c4a4SSam Leffler 		else
7995463c4a4SSam Leffler 			rum_enable_tsf(sc);
80002ac6454SAndrew Thompson 
80102ac6454SAndrew Thompson 		/* enable automatic rate adaptation */
8025463c4a4SSam Leffler 		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
80302ac6454SAndrew Thompson 		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
804b6108616SRui Paulo 			rum_ratectl_start(sc, ni);
8058d8bdb01SAndrew Thompson 		ieee80211_free_node(ni);
80602ac6454SAndrew Thompson 		break;
80702ac6454SAndrew Thompson 	default:
80802ac6454SAndrew Thompson 		break;
80902ac6454SAndrew Thompson 	}
81002ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
81102ac6454SAndrew Thompson 	IEEE80211_LOCK(ic);
8125efea30fSAndrew Thompson 	return (rvp->newstate(vap, nstate, arg));
81302ac6454SAndrew Thompson }
81402ac6454SAndrew Thompson 
81502ac6454SAndrew Thompson static void
816ed6d949aSAndrew Thompson rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
81702ac6454SAndrew Thompson {
818ed6d949aSAndrew Thompson 	struct rum_softc *sc = usbd_xfer_softc(xfer);
8195463c4a4SSam Leffler 	struct ieee80211vap *vap;
82002ac6454SAndrew Thompson 	struct rum_tx_data *data;
82102ac6454SAndrew Thompson 	struct mbuf *m;
822ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
82302ac6454SAndrew Thompson 	unsigned int len;
824ed6d949aSAndrew Thompson 	int actlen, sumlen;
825ed6d949aSAndrew Thompson 
826ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
82702ac6454SAndrew Thompson 
82802ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
82902ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
830ed6d949aSAndrew Thompson 		DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
83102ac6454SAndrew Thompson 
83202ac6454SAndrew Thompson 		/* free resources */
833ed6d949aSAndrew Thompson 		data = usbd_xfer_get_priv(xfer);
83402ac6454SAndrew Thompson 		rum_tx_free(data, 0);
835ed6d949aSAndrew Thompson 		usbd_xfer_set_priv(xfer, NULL);
83602ac6454SAndrew Thompson 
83702ac6454SAndrew Thompson 		/* FALLTHROUGH */
83802ac6454SAndrew Thompson 	case USB_ST_SETUP:
83902ac6454SAndrew Thompson tr_setup:
84002ac6454SAndrew Thompson 		data = STAILQ_FIRST(&sc->tx_q);
84102ac6454SAndrew Thompson 		if (data) {
84202ac6454SAndrew Thompson 			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
84302ac6454SAndrew Thompson 			m = data->m;
84402ac6454SAndrew Thompson 
8456d917491SHans Petter Selasky 			if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
84602ac6454SAndrew Thompson 				DPRINTFN(0, "data overflow, %u bytes\n",
84702ac6454SAndrew Thompson 				    m->m_pkthdr.len);
84802ac6454SAndrew Thompson 				m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
84902ac6454SAndrew Thompson 			}
850ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
851ed6d949aSAndrew Thompson 			usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
852ed6d949aSAndrew Thompson 			usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
853ed6d949aSAndrew Thompson 			    m->m_pkthdr.len);
85402ac6454SAndrew Thompson 
8555463c4a4SSam Leffler 			vap = data->ni->ni_vap;
8565463c4a4SSam Leffler 			if (ieee80211_radiotap_active_vap(vap)) {
85702ac6454SAndrew Thompson 				struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
85802ac6454SAndrew Thompson 
85902ac6454SAndrew Thompson 				tap->wt_flags = 0;
86002ac6454SAndrew Thompson 				tap->wt_rate = data->rate;
861a6ccd477SAdrian Chadd 				rum_get_tsf(sc, &tap->wt_tsf);
86202ac6454SAndrew Thompson 				tap->wt_antenna = sc->tx_ant;
86302ac6454SAndrew Thompson 
8645463c4a4SSam Leffler 				ieee80211_radiotap_tx(vap, m);
86502ac6454SAndrew Thompson 			}
86602ac6454SAndrew Thompson 
86702ac6454SAndrew Thompson 			/* align end on a 4-bytes boundary */
86802ac6454SAndrew Thompson 			len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
86902ac6454SAndrew Thompson 			if ((len % 64) == 0)
87002ac6454SAndrew Thompson 				len += 4;
87102ac6454SAndrew Thompson 
87202ac6454SAndrew Thompson 			DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
87302ac6454SAndrew Thompson 			    m->m_pkthdr.len, len);
87402ac6454SAndrew Thompson 
875ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, len);
876ed6d949aSAndrew Thompson 			usbd_xfer_set_priv(xfer, data);
87702ac6454SAndrew Thompson 
878a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
87902ac6454SAndrew Thompson 		}
8807a79cebfSGleb Smirnoff 		rum_start(sc);
88102ac6454SAndrew Thompson 		break;
88202ac6454SAndrew Thompson 
88302ac6454SAndrew Thompson 	default:			/* Error */
88402ac6454SAndrew Thompson 		DPRINTFN(11, "transfer error, %s\n",
885ed6d949aSAndrew Thompson 		    usbd_errstr(error));
88602ac6454SAndrew Thompson 
8877a79cebfSGleb Smirnoff 		counter_u64_add(sc->sc_ic.ic_oerrors, 1);
888ed6d949aSAndrew Thompson 		data = usbd_xfer_get_priv(xfer);
88902ac6454SAndrew Thompson 		if (data != NULL) {
890ed6d949aSAndrew Thompson 			rum_tx_free(data, error);
891ed6d949aSAndrew Thompson 			usbd_xfer_set_priv(xfer, NULL);
89202ac6454SAndrew Thompson 		}
89302ac6454SAndrew Thompson 
8940283fab7SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
8950283fab7SAndrew Thompson 			if (error == USB_ERR_TIMEOUT)
8960283fab7SAndrew Thompson 				device_printf(sc->sc_dev, "device timeout\n");
8970283fab7SAndrew Thompson 
8980283fab7SAndrew Thompson 			/*
8990283fab7SAndrew Thompson 			 * Try to clear stall first, also if other
9000283fab7SAndrew Thompson 			 * errors occur, hence clearing stall
9010283fab7SAndrew Thompson 			 * introduces a 50 ms delay:
9020283fab7SAndrew Thompson 			 */
903ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
90402ac6454SAndrew Thompson 			goto tr_setup;
90502ac6454SAndrew Thompson 		}
90602ac6454SAndrew Thompson 		break;
90702ac6454SAndrew Thompson 	}
90802ac6454SAndrew Thompson }
90902ac6454SAndrew Thompson 
91002ac6454SAndrew Thompson static void
911ed6d949aSAndrew Thompson rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
91202ac6454SAndrew Thompson {
913ed6d949aSAndrew Thompson 	struct rum_softc *sc = usbd_xfer_softc(xfer);
9147a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
915*aca2cf30SAdrian Chadd 	struct ieee80211_frame_min *wh;
91602ac6454SAndrew Thompson 	struct ieee80211_node *ni;
91702ac6454SAndrew Thompson 	struct mbuf *m = NULL;
918ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
91902ac6454SAndrew Thompson 	uint32_t flags;
92002ac6454SAndrew Thompson 	uint8_t rssi = 0;
921ed6d949aSAndrew Thompson 	int len;
922ed6d949aSAndrew Thompson 
923ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
92402ac6454SAndrew Thompson 
92502ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
92602ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
92702ac6454SAndrew Thompson 
928ed6d949aSAndrew Thompson 		DPRINTFN(15, "rx done, actlen=%d\n", len);
92902ac6454SAndrew Thompson 
9306d917491SHans Petter Selasky 		if (len < (int)(RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN)) {
93102ac6454SAndrew Thompson 			DPRINTF("%s: xfer too short %d\n",
93202ac6454SAndrew Thompson 			    device_get_nameunit(sc->sc_dev), len);
9337a79cebfSGleb Smirnoff 			counter_u64_add(ic->ic_ierrors, 1);
93402ac6454SAndrew Thompson 			goto tr_setup;
93502ac6454SAndrew Thompson 		}
93602ac6454SAndrew Thompson 
93702ac6454SAndrew Thompson 		len -= RT2573_RX_DESC_SIZE;
938ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
939ed6d949aSAndrew Thompson 		usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
94002ac6454SAndrew Thompson 
94102ac6454SAndrew Thompson 		rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
94202ac6454SAndrew Thompson 		flags = le32toh(sc->sc_rx_desc.flags);
94302ac6454SAndrew Thompson 		if (flags & RT2573_RX_CRC_ERROR) {
94402ac6454SAndrew Thompson 			/*
94502ac6454SAndrew Thompson 		         * This should not happen since we did not
94602ac6454SAndrew Thompson 		         * request to receive those frames when we
94702ac6454SAndrew Thompson 		         * filled RUM_TXRX_CSR2:
94802ac6454SAndrew Thompson 		         */
94902ac6454SAndrew Thompson 			DPRINTFN(5, "PHY or CRC error\n");
9507a79cebfSGleb Smirnoff 			counter_u64_add(ic->ic_ierrors, 1);
95102ac6454SAndrew Thompson 			goto tr_setup;
95202ac6454SAndrew Thompson 		}
95302ac6454SAndrew Thompson 
954c6499eccSGleb Smirnoff 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
95502ac6454SAndrew Thompson 		if (m == NULL) {
95602ac6454SAndrew Thompson 			DPRINTF("could not allocate mbuf\n");
9577a79cebfSGleb Smirnoff 			counter_u64_add(ic->ic_ierrors, 1);
95802ac6454SAndrew Thompson 			goto tr_setup;
95902ac6454SAndrew Thompson 		}
960ed6d949aSAndrew Thompson 		usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
96102ac6454SAndrew Thompson 		    mtod(m, uint8_t *), len);
96202ac6454SAndrew Thompson 
963*aca2cf30SAdrian Chadd 		wh = mtod(m, struct ieee80211_frame_min *);
964*aca2cf30SAdrian Chadd 
96502ac6454SAndrew Thompson 		/* finalize mbuf */
96602ac6454SAndrew Thompson 		m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
96702ac6454SAndrew Thompson 
9685463c4a4SSam Leffler 		if (ieee80211_radiotap_active(ic)) {
96902ac6454SAndrew Thompson 			struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
97002ac6454SAndrew Thompson 
9715463c4a4SSam Leffler 			tap->wr_flags = 0;
97202ac6454SAndrew Thompson 			tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
97302ac6454SAndrew Thompson 			    (flags & RT2573_RX_OFDM) ?
97402ac6454SAndrew Thompson 			    IEEE80211_T_OFDM : IEEE80211_T_CCK);
975a6ccd477SAdrian Chadd 			rum_get_tsf(sc, &tap->wr_tsf);
9765463c4a4SSam Leffler 			tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
9775463c4a4SSam Leffler 			tap->wr_antnoise = RT2573_NOISE_FLOOR;
97802ac6454SAndrew Thompson 			tap->wr_antenna = sc->rx_ant;
97902ac6454SAndrew Thompson 		}
98002ac6454SAndrew Thompson 		/* FALLTHROUGH */
98102ac6454SAndrew Thompson 	case USB_ST_SETUP:
98202ac6454SAndrew Thompson tr_setup:
983ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
984a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
98502ac6454SAndrew Thompson 
98602ac6454SAndrew Thompson 		/*
98702ac6454SAndrew Thompson 		 * At the end of a USB callback it is always safe to unlock
98802ac6454SAndrew Thompson 		 * the private mutex of a device! That is why we do the
98902ac6454SAndrew Thompson 		 * "ieee80211_input" here, and not some lines up!
99002ac6454SAndrew Thompson 		 */
99102ac6454SAndrew Thompson 		RUM_UNLOCK(sc);
992db63fc93SWeongyo Jeong 		if (m) {
993*aca2cf30SAdrian Chadd 			if (m->m_len >= sizeof(struct ieee80211_frame_min))
994*aca2cf30SAdrian Chadd 				ni = ieee80211_find_rxnode(ic, wh);
995*aca2cf30SAdrian Chadd 			else
996*aca2cf30SAdrian Chadd 				ni = NULL;
997*aca2cf30SAdrian Chadd 
99802ac6454SAndrew Thompson 			if (ni != NULL) {
99902ac6454SAndrew Thompson 				(void) ieee80211_input(ni, m, rssi,
10005463c4a4SSam Leffler 				    RT2573_NOISE_FLOOR);
100102ac6454SAndrew Thompson 				ieee80211_free_node(ni);
100202ac6454SAndrew Thompson 			} else
100302ac6454SAndrew Thompson 				(void) ieee80211_input_all(ic, m, rssi,
10045463c4a4SSam Leffler 				    RT2573_NOISE_FLOOR);
100502ac6454SAndrew Thompson 		}
1006db63fc93SWeongyo Jeong 		RUM_LOCK(sc);
10077a79cebfSGleb Smirnoff 		rum_start(sc);
100802ac6454SAndrew Thompson 		return;
100902ac6454SAndrew Thompson 
101002ac6454SAndrew Thompson 	default:			/* Error */
1011ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
101202ac6454SAndrew Thompson 			/* try to clear stall first */
1013ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
101402ac6454SAndrew Thompson 			goto tr_setup;
101502ac6454SAndrew Thompson 		}
101602ac6454SAndrew Thompson 		return;
101702ac6454SAndrew Thompson 	}
101802ac6454SAndrew Thompson }
101902ac6454SAndrew Thompson 
102002ac6454SAndrew Thompson static uint8_t
102102ac6454SAndrew Thompson rum_plcp_signal(int rate)
102202ac6454SAndrew Thompson {
102302ac6454SAndrew Thompson 	switch (rate) {
102402ac6454SAndrew Thompson 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
102502ac6454SAndrew Thompson 	case 12:	return 0xb;
102602ac6454SAndrew Thompson 	case 18:	return 0xf;
102702ac6454SAndrew Thompson 	case 24:	return 0xa;
102802ac6454SAndrew Thompson 	case 36:	return 0xe;
102902ac6454SAndrew Thompson 	case 48:	return 0x9;
103002ac6454SAndrew Thompson 	case 72:	return 0xd;
103102ac6454SAndrew Thompson 	case 96:	return 0x8;
103202ac6454SAndrew Thompson 	case 108:	return 0xc;
103302ac6454SAndrew Thompson 
103402ac6454SAndrew Thompson 	/* CCK rates (NB: not IEEE std, device-specific) */
103502ac6454SAndrew Thompson 	case 2:		return 0x0;
103602ac6454SAndrew Thompson 	case 4:		return 0x1;
103702ac6454SAndrew Thompson 	case 11:	return 0x2;
103802ac6454SAndrew Thompson 	case 22:	return 0x3;
103902ac6454SAndrew Thompson 	}
104002ac6454SAndrew Thompson 	return 0xff;		/* XXX unsupported/unknown rate */
104102ac6454SAndrew Thompson }
104202ac6454SAndrew Thompson 
104302ac6454SAndrew Thompson static void
104402ac6454SAndrew Thompson rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
104502ac6454SAndrew Thompson     uint32_t flags, uint16_t xflags, int len, int rate)
104602ac6454SAndrew Thompson {
10477a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
104802ac6454SAndrew Thompson 	uint16_t plcp_length;
104902ac6454SAndrew Thompson 	int remainder;
105002ac6454SAndrew Thompson 
105102ac6454SAndrew Thompson 	desc->flags = htole32(flags);
105202ac6454SAndrew Thompson 	desc->flags |= htole32(RT2573_TX_VALID);
105302ac6454SAndrew Thompson 	desc->flags |= htole32(len << 16);
105402ac6454SAndrew Thompson 
105502ac6454SAndrew Thompson 	desc->xflags = htole16(xflags);
105602ac6454SAndrew Thompson 
105702ac6454SAndrew Thompson 	desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) |
105802ac6454SAndrew Thompson 	    RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10));
105902ac6454SAndrew Thompson 
106002ac6454SAndrew Thompson 	/* setup PLCP fields */
106102ac6454SAndrew Thompson 	desc->plcp_signal  = rum_plcp_signal(rate);
106202ac6454SAndrew Thompson 	desc->plcp_service = 4;
106302ac6454SAndrew Thompson 
106402ac6454SAndrew Thompson 	len += IEEE80211_CRC_LEN;
106526d39e2cSSam Leffler 	if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
106602ac6454SAndrew Thompson 		desc->flags |= htole32(RT2573_TX_OFDM);
106702ac6454SAndrew Thompson 
106802ac6454SAndrew Thompson 		plcp_length = len & 0xfff;
106902ac6454SAndrew Thompson 		desc->plcp_length_hi = plcp_length >> 6;
107002ac6454SAndrew Thompson 		desc->plcp_length_lo = plcp_length & 0x3f;
107102ac6454SAndrew Thompson 	} else {
1072b8e3094cSHans Petter Selasky 		if (rate == 0)
1073b8e3094cSHans Petter Selasky 			rate = 2;	/* avoid division by zero */
107402ac6454SAndrew Thompson 		plcp_length = (16 * len + rate - 1) / rate;
107502ac6454SAndrew Thompson 		if (rate == 22) {
107602ac6454SAndrew Thompson 			remainder = (16 * len) % 22;
107702ac6454SAndrew Thompson 			if (remainder != 0 && remainder < 7)
107802ac6454SAndrew Thompson 				desc->plcp_service |= RT2573_PLCP_LENGEXT;
107902ac6454SAndrew Thompson 		}
108002ac6454SAndrew Thompson 		desc->plcp_length_hi = plcp_length >> 8;
108102ac6454SAndrew Thompson 		desc->plcp_length_lo = plcp_length & 0xff;
108202ac6454SAndrew Thompson 
108302ac6454SAndrew Thompson 		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
108402ac6454SAndrew Thompson 			desc->plcp_signal |= 0x08;
108502ac6454SAndrew Thompson 	}
108602ac6454SAndrew Thompson }
108702ac6454SAndrew Thompson 
108802ac6454SAndrew Thompson static int
108902ac6454SAndrew Thompson rum_sendprot(struct rum_softc *sc,
109002ac6454SAndrew Thompson     const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
109102ac6454SAndrew Thompson {
109202ac6454SAndrew Thompson 	struct ieee80211com *ic = ni->ni_ic;
109302ac6454SAndrew Thompson 	const struct ieee80211_frame *wh;
109402ac6454SAndrew Thompson 	struct rum_tx_data *data;
109502ac6454SAndrew Thompson 	struct mbuf *mprot;
1096e7b7db3dSAdrian Chadd 	int protrate, pktlen, flags, isshort;
109702ac6454SAndrew Thompson 	uint16_t dur;
109802ac6454SAndrew Thompson 
1099e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
110002ac6454SAndrew Thompson 	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
110102ac6454SAndrew Thompson 	    ("protection %d", prot));
110202ac6454SAndrew Thompson 
110302ac6454SAndrew Thompson 	wh = mtod(m, const struct ieee80211_frame *);
110402ac6454SAndrew Thompson 	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
110502ac6454SAndrew Thompson 
110626d39e2cSSam Leffler 	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
110702ac6454SAndrew Thompson 
110802ac6454SAndrew Thompson 	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
11090f817de7SJung-uk Kim 	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
111026d39e2cSSam Leffler 	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
111102ac6454SAndrew Thompson 	flags = RT2573_TX_MORE_FRAG;
111202ac6454SAndrew Thompson 	if (prot == IEEE80211_PROT_RTSCTS) {
111302ac6454SAndrew Thompson 		/* NB: CTS is the same size as an ACK */
111426d39e2cSSam Leffler 		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
111502ac6454SAndrew Thompson 		flags |= RT2573_TX_NEED_ACK;
111602ac6454SAndrew Thompson 		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
111702ac6454SAndrew Thompson 	} else {
111802ac6454SAndrew Thompson 		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
111902ac6454SAndrew Thompson 	}
112002ac6454SAndrew Thompson 	if (mprot == NULL) {
112102ac6454SAndrew Thompson 		/* XXX stat + msg */
112277ddf3d3SAndrew Thompson 		return (ENOBUFS);
112302ac6454SAndrew Thompson 	}
112402ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
112502ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
112602ac6454SAndrew Thompson 	sc->tx_nfree--;
112702ac6454SAndrew Thompson 
112802ac6454SAndrew Thompson 	data->m = mprot;
112902ac6454SAndrew Thompson 	data->ni = ieee80211_ref_node(ni);
113002ac6454SAndrew Thompson 	data->rate = protrate;
113102ac6454SAndrew Thompson 	rum_setup_tx_desc(sc, &data->desc, flags, 0, mprot->m_pkthdr.len, protrate);
113202ac6454SAndrew Thompson 
113302ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1134a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
113502ac6454SAndrew Thompson 
113602ac6454SAndrew Thompson 	return 0;
113702ac6454SAndrew Thompson }
113802ac6454SAndrew Thompson 
113902ac6454SAndrew Thompson static int
114002ac6454SAndrew Thompson rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
114102ac6454SAndrew Thompson {
114202ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
11437a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
114402ac6454SAndrew Thompson 	struct rum_tx_data *data;
114502ac6454SAndrew Thompson 	struct ieee80211_frame *wh;
114602ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
114702ac6454SAndrew Thompson 	struct ieee80211_key *k;
114802ac6454SAndrew Thompson 	uint32_t flags = 0;
114902ac6454SAndrew Thompson 	uint16_t dur;
115002ac6454SAndrew Thompson 
1151e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
115202ac6454SAndrew Thompson 
115302ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
115402ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
115502ac6454SAndrew Thompson 	sc->tx_nfree--;
115602ac6454SAndrew Thompson 
115702ac6454SAndrew Thompson 	wh = mtod(m0, struct ieee80211_frame *);
11585945b5f5SKevin Lo 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
115902ac6454SAndrew Thompson 		k = ieee80211_crypto_encap(ni, m0);
116002ac6454SAndrew Thompson 		if (k == NULL) {
116102ac6454SAndrew Thompson 			m_freem(m0);
116202ac6454SAndrew Thompson 			return ENOBUFS;
116302ac6454SAndrew Thompson 		}
116402ac6454SAndrew Thompson 		wh = mtod(m0, struct ieee80211_frame *);
116502ac6454SAndrew Thompson 	}
116602ac6454SAndrew Thompson 
116702ac6454SAndrew Thompson 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
116802ac6454SAndrew Thompson 
116902ac6454SAndrew Thompson 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
117002ac6454SAndrew Thompson 		flags |= RT2573_TX_NEED_ACK;
117102ac6454SAndrew Thompson 
117226d39e2cSSam Leffler 		dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
117302ac6454SAndrew Thompson 		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
11748cfe5440SHans Petter Selasky 		USETW(wh->i_dur, dur);
117502ac6454SAndrew Thompson 
117602ac6454SAndrew Thompson 		/* tell hardware to add timestamp for probe responses */
117702ac6454SAndrew Thompson 		if ((wh->i_fc[0] &
117802ac6454SAndrew Thompson 		    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
117902ac6454SAndrew Thompson 		    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
118002ac6454SAndrew Thompson 			flags |= RT2573_TX_TIMESTAMP;
118102ac6454SAndrew Thompson 	}
118202ac6454SAndrew Thompson 
118302ac6454SAndrew Thompson 	data->m = m0;
118402ac6454SAndrew Thompson 	data->ni = ni;
118502ac6454SAndrew Thompson 	data->rate = tp->mgmtrate;
118602ac6454SAndrew Thompson 
118702ac6454SAndrew Thompson 	rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, tp->mgmtrate);
118802ac6454SAndrew Thompson 
118902ac6454SAndrew Thompson 	DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
119002ac6454SAndrew Thompson 	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
119102ac6454SAndrew Thompson 
119202ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1193a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
119402ac6454SAndrew Thompson 
119577ddf3d3SAndrew Thompson 	return (0);
119602ac6454SAndrew Thompson }
119702ac6454SAndrew Thompson 
119802ac6454SAndrew Thompson static int
119902ac6454SAndrew Thompson rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
120002ac6454SAndrew Thompson     const struct ieee80211_bpf_params *params)
120102ac6454SAndrew Thompson {
1202515db61dSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
120302ac6454SAndrew Thompson 	struct rum_tx_data *data;
120402ac6454SAndrew Thompson 	uint32_t flags;
120502ac6454SAndrew Thompson 	int rate, error;
120602ac6454SAndrew Thompson 
1207e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
120802ac6454SAndrew Thompson 	KASSERT(params != NULL, ("no raw xmit params"));
120902ac6454SAndrew Thompson 
1210515db61dSSam Leffler 	rate = params->ibp_rate0;
1211515db61dSSam Leffler 	if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
121202ac6454SAndrew Thompson 		m_freem(m0);
121302ac6454SAndrew Thompson 		return EINVAL;
121402ac6454SAndrew Thompson 	}
121502ac6454SAndrew Thompson 	flags = 0;
121602ac6454SAndrew Thompson 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
121702ac6454SAndrew Thompson 		flags |= RT2573_TX_NEED_ACK;
121802ac6454SAndrew Thompson 	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
121902ac6454SAndrew Thompson 		error = rum_sendprot(sc, m0, ni,
122002ac6454SAndrew Thompson 		    params->ibp_flags & IEEE80211_BPF_RTS ?
122102ac6454SAndrew Thompson 			 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
122202ac6454SAndrew Thompson 		    rate);
122396ca458fSAndrew Thompson 		if (error || sc->tx_nfree == 0) {
122402ac6454SAndrew Thompson 			m_freem(m0);
122596ca458fSAndrew Thompson 			return ENOBUFS;
122602ac6454SAndrew Thompson 		}
122702ac6454SAndrew Thompson 		flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
122802ac6454SAndrew Thompson 	}
122902ac6454SAndrew Thompson 
123096ca458fSAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
123196ca458fSAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
123296ca458fSAndrew Thompson 	sc->tx_nfree--;
123396ca458fSAndrew Thompson 
123402ac6454SAndrew Thompson 	data->m = m0;
123502ac6454SAndrew Thompson 	data->ni = ni;
123602ac6454SAndrew Thompson 	data->rate = rate;
123702ac6454SAndrew Thompson 
123802ac6454SAndrew Thompson 	/* XXX need to setup descriptor ourself */
123902ac6454SAndrew Thompson 	rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
124002ac6454SAndrew Thompson 
124102ac6454SAndrew Thompson 	DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
124202ac6454SAndrew Thompson 	    m0->m_pkthdr.len, rate);
124302ac6454SAndrew Thompson 
124402ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1245a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
124602ac6454SAndrew Thompson 
124702ac6454SAndrew Thompson 	return 0;
124802ac6454SAndrew Thompson }
124902ac6454SAndrew Thompson 
125002ac6454SAndrew Thompson static int
125102ac6454SAndrew Thompson rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
125202ac6454SAndrew Thompson {
125302ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
12547a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
125502ac6454SAndrew Thompson 	struct rum_tx_data *data;
125602ac6454SAndrew Thompson 	struct ieee80211_frame *wh;
125702ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
125802ac6454SAndrew Thompson 	struct ieee80211_key *k;
125902ac6454SAndrew Thompson 	uint32_t flags = 0;
126002ac6454SAndrew Thompson 	uint16_t dur;
126102ac6454SAndrew Thompson 	int error, rate;
126202ac6454SAndrew Thompson 
1263e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
126402ac6454SAndrew Thompson 
126502ac6454SAndrew Thompson 	wh = mtod(m0, struct ieee80211_frame *);
126602ac6454SAndrew Thompson 
126702ac6454SAndrew Thompson 	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
126802ac6454SAndrew Thompson 	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
126902ac6454SAndrew Thompson 		rate = tp->mcastrate;
127002ac6454SAndrew Thompson 	else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
127102ac6454SAndrew Thompson 		rate = tp->ucastrate;
127202ac6454SAndrew Thompson 	else
127302ac6454SAndrew Thompson 		rate = ni->ni_txrate;
127402ac6454SAndrew Thompson 
12755945b5f5SKevin Lo 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
127602ac6454SAndrew Thompson 		k = ieee80211_crypto_encap(ni, m0);
127702ac6454SAndrew Thompson 		if (k == NULL) {
127802ac6454SAndrew Thompson 			m_freem(m0);
127902ac6454SAndrew Thompson 			return ENOBUFS;
128002ac6454SAndrew Thompson 		}
128102ac6454SAndrew Thompson 
128202ac6454SAndrew Thompson 		/* packet header may have moved, reset our local pointer */
128302ac6454SAndrew Thompson 		wh = mtod(m0, struct ieee80211_frame *);
128402ac6454SAndrew Thompson 	}
128502ac6454SAndrew Thompson 
128602ac6454SAndrew Thompson 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
128702ac6454SAndrew Thompson 		int prot = IEEE80211_PROT_NONE;
128802ac6454SAndrew Thompson 		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
128902ac6454SAndrew Thompson 			prot = IEEE80211_PROT_RTSCTS;
129002ac6454SAndrew Thompson 		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
129126d39e2cSSam Leffler 		    ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
129202ac6454SAndrew Thompson 			prot = ic->ic_protmode;
129302ac6454SAndrew Thompson 		if (prot != IEEE80211_PROT_NONE) {
129402ac6454SAndrew Thompson 			error = rum_sendprot(sc, m0, ni, prot, rate);
129596ca458fSAndrew Thompson 			if (error || sc->tx_nfree == 0) {
129602ac6454SAndrew Thompson 				m_freem(m0);
129796ca458fSAndrew Thompson 				return ENOBUFS;
129802ac6454SAndrew Thompson 			}
129902ac6454SAndrew Thompson 			flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
130002ac6454SAndrew Thompson 		}
130102ac6454SAndrew Thompson 	}
130202ac6454SAndrew Thompson 
130302ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
130402ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
130502ac6454SAndrew Thompson 	sc->tx_nfree--;
130602ac6454SAndrew Thompson 
130702ac6454SAndrew Thompson 	data->m = m0;
130802ac6454SAndrew Thompson 	data->ni = ni;
130902ac6454SAndrew Thompson 	data->rate = rate;
131002ac6454SAndrew Thompson 
131102ac6454SAndrew Thompson 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
131202ac6454SAndrew Thompson 		flags |= RT2573_TX_NEED_ACK;
131302ac6454SAndrew Thompson 		flags |= RT2573_TX_MORE_FRAG;
131402ac6454SAndrew Thompson 
131526d39e2cSSam Leffler 		dur = ieee80211_ack_duration(ic->ic_rt, rate,
131602ac6454SAndrew Thompson 		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
13178cfe5440SHans Petter Selasky 		USETW(wh->i_dur, dur);
131802ac6454SAndrew Thompson 	}
131902ac6454SAndrew Thompson 
132002ac6454SAndrew Thompson 	rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
132102ac6454SAndrew Thompson 
132202ac6454SAndrew Thompson 	DPRINTFN(10, "sending frame len=%d rate=%d\n",
132302ac6454SAndrew Thompson 	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
132402ac6454SAndrew Thompson 
132502ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1326a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
132702ac6454SAndrew Thompson 
132802ac6454SAndrew Thompson 	return 0;
132902ac6454SAndrew Thompson }
133002ac6454SAndrew Thompson 
13317a79cebfSGleb Smirnoff static int
13327a79cebfSGleb Smirnoff rum_transmit(struct ieee80211com *ic, struct mbuf *m)
133379d2c5e8SGleb Smirnoff {
13347a79cebfSGleb Smirnoff 	struct rum_softc *sc = ic->ic_softc;
13357a79cebfSGleb Smirnoff 	int error;
13367a79cebfSGleb Smirnoff 
13377a79cebfSGleb Smirnoff 	RUM_LOCK(sc);
13387a79cebfSGleb Smirnoff 	if (!sc->sc_running) {
13397a79cebfSGleb Smirnoff 		RUM_UNLOCK(sc);
13407a79cebfSGleb Smirnoff 		return (ENXIO);
13417a79cebfSGleb Smirnoff 	}
13427a79cebfSGleb Smirnoff 	error = mbufq_enqueue(&sc->sc_snd, m);
13437a79cebfSGleb Smirnoff 	if (error) {
13447a79cebfSGleb Smirnoff 		RUM_UNLOCK(sc);
13457a79cebfSGleb Smirnoff 		return (error);
13467a79cebfSGleb Smirnoff 	}
13477a79cebfSGleb Smirnoff 	rum_start(sc);
13487a79cebfSGleb Smirnoff 	RUM_UNLOCK(sc);
13497a79cebfSGleb Smirnoff 
13507a79cebfSGleb Smirnoff 	return (0);
13517a79cebfSGleb Smirnoff }
13527a79cebfSGleb Smirnoff 
13537a79cebfSGleb Smirnoff static void
13547a79cebfSGleb Smirnoff rum_start(struct rum_softc *sc)
13557a79cebfSGleb Smirnoff {
135602ac6454SAndrew Thompson 	struct ieee80211_node *ni;
135702ac6454SAndrew Thompson 	struct mbuf *m;
135802ac6454SAndrew Thompson 
1359e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
13607a79cebfSGleb Smirnoff 
13617a79cebfSGleb Smirnoff 	if (!sc->sc_running)
136202ac6454SAndrew Thompson 		return;
13637a79cebfSGleb Smirnoff 
13647a79cebfSGleb Smirnoff 	while (sc->tx_nfree >= RUM_TX_MINFREE &&
13657a79cebfSGleb Smirnoff 	    (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
136602ac6454SAndrew Thompson 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
136702ac6454SAndrew Thompson 		if (rum_tx_data(sc, m, ni) != 0) {
13687a79cebfSGleb Smirnoff 			if_inc_counter(ni->ni_vap->iv_ifp,
13697a79cebfSGleb Smirnoff 			    IFCOUNTER_OERRORS, 1);
137002ac6454SAndrew Thompson 			ieee80211_free_node(ni);
137102ac6454SAndrew Thompson 			break;
137202ac6454SAndrew Thompson 		}
137302ac6454SAndrew Thompson 	}
137402ac6454SAndrew Thompson }
137502ac6454SAndrew Thompson 
13767a79cebfSGleb Smirnoff static void
13777a79cebfSGleb Smirnoff rum_parent(struct ieee80211com *ic)
137802ac6454SAndrew Thompson {
1379d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
1380645e4d17SHans Petter Selasky 	int startall = 0;
1381645e4d17SHans Petter Selasky 
1382645e4d17SHans Petter Selasky 	RUM_LOCK(sc);
13837a79cebfSGleb Smirnoff 	if (sc->sc_detached) {
1384645e4d17SHans Petter Selasky 		RUM_UNLOCK(sc);
13857a79cebfSGleb Smirnoff 		return;
13867a79cebfSGleb Smirnoff 	}
13877a79cebfSGleb Smirnoff 	if (ic->ic_nrunning > 0) {
13887a79cebfSGleb Smirnoff 		if (!sc->sc_running) {
13897a79cebfSGleb Smirnoff 			rum_init(sc);
139002ac6454SAndrew Thompson 			startall = 1;
139102ac6454SAndrew Thompson 		} else
13925efea30fSAndrew Thompson 			rum_setpromisc(sc);
13937a79cebfSGleb Smirnoff 	} else if (sc->sc_running)
13945efea30fSAndrew Thompson 		rum_stop(sc);
139502ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
139602ac6454SAndrew Thompson 	if (startall)
139702ac6454SAndrew Thompson 		ieee80211_start_all(ic);
139802ac6454SAndrew Thompson }
139902ac6454SAndrew Thompson 
140002ac6454SAndrew Thompson static void
140102ac6454SAndrew Thompson rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
140202ac6454SAndrew Thompson {
1403760bc48eSAndrew Thompson 	struct usb_device_request req;
1404e0a69b51SAndrew Thompson 	usb_error_t error;
140502ac6454SAndrew Thompson 
140602ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
140702ac6454SAndrew Thompson 	req.bRequest = RT2573_READ_EEPROM;
140802ac6454SAndrew Thompson 	USETW(req.wValue, 0);
140902ac6454SAndrew Thompson 	USETW(req.wIndex, addr);
141002ac6454SAndrew Thompson 	USETW(req.wLength, len);
141102ac6454SAndrew Thompson 
141202ac6454SAndrew Thompson 	error = rum_do_request(sc, &req, buf);
141302ac6454SAndrew Thompson 	if (error != 0) {
141402ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1415a593f6b8SAndrew Thompson 		    usbd_errstr(error));
141602ac6454SAndrew Thompson 	}
141702ac6454SAndrew Thompson }
141802ac6454SAndrew Thompson 
141902ac6454SAndrew Thompson static uint32_t
142002ac6454SAndrew Thompson rum_read(struct rum_softc *sc, uint16_t reg)
142102ac6454SAndrew Thompson {
142202ac6454SAndrew Thompson 	uint32_t val;
142302ac6454SAndrew Thompson 
142402ac6454SAndrew Thompson 	rum_read_multi(sc, reg, &val, sizeof val);
142502ac6454SAndrew Thompson 
142602ac6454SAndrew Thompson 	return le32toh(val);
142702ac6454SAndrew Thompson }
142802ac6454SAndrew Thompson 
142902ac6454SAndrew Thompson static void
143002ac6454SAndrew Thompson rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
143102ac6454SAndrew Thompson {
1432760bc48eSAndrew Thompson 	struct usb_device_request req;
1433e0a69b51SAndrew Thompson 	usb_error_t error;
143402ac6454SAndrew Thompson 
143502ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
143602ac6454SAndrew Thompson 	req.bRequest = RT2573_READ_MULTI_MAC;
143702ac6454SAndrew Thompson 	USETW(req.wValue, 0);
143802ac6454SAndrew Thompson 	USETW(req.wIndex, reg);
143902ac6454SAndrew Thompson 	USETW(req.wLength, len);
144002ac6454SAndrew Thompson 
144102ac6454SAndrew Thompson 	error = rum_do_request(sc, &req, buf);
144202ac6454SAndrew Thompson 	if (error != 0) {
144302ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
144402ac6454SAndrew Thompson 		    "could not multi read MAC register: %s\n",
1445a593f6b8SAndrew Thompson 		    usbd_errstr(error));
144602ac6454SAndrew Thompson 	}
144702ac6454SAndrew Thompson }
144802ac6454SAndrew Thompson 
1449e0a69b51SAndrew Thompson static usb_error_t
145002ac6454SAndrew Thompson rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
145102ac6454SAndrew Thompson {
145202ac6454SAndrew Thompson 	uint32_t tmp = htole32(val);
145302ac6454SAndrew Thompson 
145477ddf3d3SAndrew Thompson 	return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
145502ac6454SAndrew Thompson }
145602ac6454SAndrew Thompson 
1457e0a69b51SAndrew Thompson static usb_error_t
145802ac6454SAndrew Thompson rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
145902ac6454SAndrew Thompson {
1460760bc48eSAndrew Thompson 	struct usb_device_request req;
1461e0a69b51SAndrew Thompson 	usb_error_t error;
14626d917491SHans Petter Selasky 	size_t offset;
146302ac6454SAndrew Thompson 
146402ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
146502ac6454SAndrew Thompson 	req.bRequest = RT2573_WRITE_MULTI_MAC;
146602ac6454SAndrew Thompson 	USETW(req.wValue, 0);
146702ac6454SAndrew Thompson 
146865d7e4d9SAdrian Chadd 	/* write at most 64 bytes at a time */
146965d7e4d9SAdrian Chadd 	for (offset = 0; offset < len; offset += 64) {
147065d7e4d9SAdrian Chadd 		USETW(req.wIndex, reg + offset);
147165d7e4d9SAdrian Chadd 		USETW(req.wLength, MIN(len - offset, 64));
147265d7e4d9SAdrian Chadd 
14738797cafeSBjoern A. Zeeb 		error = rum_do_request(sc, &req, (char *)buf + offset);
147402ac6454SAndrew Thompson 		if (error != 0) {
147502ac6454SAndrew Thompson 			device_printf(sc->sc_dev,
147602ac6454SAndrew Thompson 			    "could not multi write MAC register: %s\n",
1477a593f6b8SAndrew Thompson 			    usbd_errstr(error));
147877ddf3d3SAndrew Thompson 			return (error);
147902ac6454SAndrew Thompson 		}
148065d7e4d9SAdrian Chadd 	}
14818797cafeSBjoern A. Zeeb 
14828797cafeSBjoern A. Zeeb 	return (USB_ERR_NORMAL_COMPLETION);
148365d7e4d9SAdrian Chadd }
148402ac6454SAndrew Thompson 
1485a05022b2SAdrian Chadd static usb_error_t
1486a05022b2SAdrian Chadd rum_setbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1487a05022b2SAdrian Chadd {
1488a05022b2SAdrian Chadd 	return (rum_write(sc, reg, rum_read(sc, reg) | mask));
1489a05022b2SAdrian Chadd }
1490a05022b2SAdrian Chadd 
1491a05022b2SAdrian Chadd static usb_error_t
1492a05022b2SAdrian Chadd rum_clrbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1493a05022b2SAdrian Chadd {
1494a05022b2SAdrian Chadd 	return (rum_write(sc, reg, rum_read(sc, reg) & ~mask));
1495a05022b2SAdrian Chadd }
1496a05022b2SAdrian Chadd 
1497a05022b2SAdrian Chadd static usb_error_t
1498a05022b2SAdrian Chadd rum_modbits(struct rum_softc *sc, uint16_t reg, uint32_t set, uint32_t unset)
1499a05022b2SAdrian Chadd {
1500a05022b2SAdrian Chadd 	return (rum_write(sc, reg, (rum_read(sc, reg) & ~unset) | set));
1501a05022b2SAdrian Chadd }
1502a05022b2SAdrian Chadd 
1503cc9ae761SAdrian Chadd static int
1504cc9ae761SAdrian Chadd rum_bbp_busy(struct rum_softc *sc)
150502ac6454SAndrew Thompson {
150602ac6454SAndrew Thompson 	int ntries;
150702ac6454SAndrew Thompson 
150802ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
150902ac6454SAndrew Thompson 		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
151002ac6454SAndrew Thompson 			break;
151102ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
151202ac6454SAndrew Thompson 			break;
151302ac6454SAndrew Thompson 	}
1514cc9ae761SAdrian Chadd 	if (ntries == 100)
1515cc9ae761SAdrian Chadd 		return (ETIMEDOUT);
1516cc9ae761SAdrian Chadd 
1517cc9ae761SAdrian Chadd 	return (0);
1518cc9ae761SAdrian Chadd }
1519cc9ae761SAdrian Chadd 
1520cc9ae761SAdrian Chadd static void
1521cc9ae761SAdrian Chadd rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1522cc9ae761SAdrian Chadd {
1523cc9ae761SAdrian Chadd 	uint32_t tmp;
1524cc9ae761SAdrian Chadd 
1525cc9ae761SAdrian Chadd 	DPRINTFN(2, "reg=0x%08x\n", reg);
1526cc9ae761SAdrian Chadd 
1527cc9ae761SAdrian Chadd 	if (rum_bbp_busy(sc) != 0) {
152802ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not write to BBP\n");
152902ac6454SAndrew Thompson 		return;
153002ac6454SAndrew Thompson 	}
153102ac6454SAndrew Thompson 
153202ac6454SAndrew Thompson 	tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
153302ac6454SAndrew Thompson 	rum_write(sc, RT2573_PHY_CSR3, tmp);
153402ac6454SAndrew Thompson }
153502ac6454SAndrew Thompson 
153602ac6454SAndrew Thompson static uint8_t
153702ac6454SAndrew Thompson rum_bbp_read(struct rum_softc *sc, uint8_t reg)
153802ac6454SAndrew Thompson {
153902ac6454SAndrew Thompson 	uint32_t val;
154002ac6454SAndrew Thompson 	int ntries;
154102ac6454SAndrew Thompson 
154277ddf3d3SAndrew Thompson 	DPRINTFN(2, "reg=0x%08x\n", reg);
154377ddf3d3SAndrew Thompson 
1544cc9ae761SAdrian Chadd 	if (rum_bbp_busy(sc) != 0) {
154502ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read BBP\n");
154602ac6454SAndrew Thompson 		return 0;
154702ac6454SAndrew Thompson 	}
154802ac6454SAndrew Thompson 
154902ac6454SAndrew Thompson 	val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
155002ac6454SAndrew Thompson 	rum_write(sc, RT2573_PHY_CSR3, val);
155102ac6454SAndrew Thompson 
155202ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
155302ac6454SAndrew Thompson 		val = rum_read(sc, RT2573_PHY_CSR3);
155402ac6454SAndrew Thompson 		if (!(val & RT2573_BBP_BUSY))
155502ac6454SAndrew Thompson 			return val & 0xff;
155602ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
155702ac6454SAndrew Thompson 			break;
155802ac6454SAndrew Thompson 	}
155902ac6454SAndrew Thompson 
156002ac6454SAndrew Thompson 	device_printf(sc->sc_dev, "could not read BBP\n");
156102ac6454SAndrew Thompson 	return 0;
156202ac6454SAndrew Thompson }
156302ac6454SAndrew Thompson 
156402ac6454SAndrew Thompson static void
156502ac6454SAndrew Thompson rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
156602ac6454SAndrew Thompson {
156702ac6454SAndrew Thompson 	uint32_t tmp;
156802ac6454SAndrew Thompson 	int ntries;
156902ac6454SAndrew Thompson 
157002ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
157102ac6454SAndrew Thompson 		if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
157202ac6454SAndrew Thompson 			break;
157302ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
157402ac6454SAndrew Thompson 			break;
157502ac6454SAndrew Thompson 	}
157602ac6454SAndrew Thompson 	if (ntries == 100) {
157702ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not write to RF\n");
157802ac6454SAndrew Thompson 		return;
157902ac6454SAndrew Thompson 	}
158002ac6454SAndrew Thompson 
158167784314SPoul-Henning Kamp 	tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
158202ac6454SAndrew Thompson 	    (reg & 3);
158302ac6454SAndrew Thompson 	rum_write(sc, RT2573_PHY_CSR4, tmp);
158402ac6454SAndrew Thompson 
158502ac6454SAndrew Thompson 	/* remember last written value in sc */
158602ac6454SAndrew Thompson 	sc->rf_regs[reg] = val;
158702ac6454SAndrew Thompson 
158802ac6454SAndrew Thompson 	DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
158902ac6454SAndrew Thompson }
159002ac6454SAndrew Thompson 
159102ac6454SAndrew Thompson static void
159202ac6454SAndrew Thompson rum_select_antenna(struct rum_softc *sc)
159302ac6454SAndrew Thompson {
159402ac6454SAndrew Thompson 	uint8_t bbp4, bbp77;
159502ac6454SAndrew Thompson 	uint32_t tmp;
159602ac6454SAndrew Thompson 
159702ac6454SAndrew Thompson 	bbp4  = rum_bbp_read(sc, 4);
159802ac6454SAndrew Thompson 	bbp77 = rum_bbp_read(sc, 77);
159902ac6454SAndrew Thompson 
160002ac6454SAndrew Thompson 	/* TBD */
160102ac6454SAndrew Thompson 
160202ac6454SAndrew Thompson 	/* make sure Rx is disabled before switching antenna */
160302ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
160402ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
160502ac6454SAndrew Thompson 
160602ac6454SAndrew Thompson 	rum_bbp_write(sc,  4, bbp4);
160702ac6454SAndrew Thompson 	rum_bbp_write(sc, 77, bbp77);
160802ac6454SAndrew Thompson 
160902ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
161002ac6454SAndrew Thompson }
161102ac6454SAndrew Thompson 
161202ac6454SAndrew Thompson /*
161302ac6454SAndrew Thompson  * Enable multi-rate retries for frames sent at OFDM rates.
161402ac6454SAndrew Thompson  * In 802.11b/g mode, allow fallback to CCK rates.
161502ac6454SAndrew Thompson  */
161602ac6454SAndrew Thompson static void
161702ac6454SAndrew Thompson rum_enable_mrr(struct rum_softc *sc)
161802ac6454SAndrew Thompson {
16197a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
162002ac6454SAndrew Thompson 
1621a05022b2SAdrian Chadd 	if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1622a05022b2SAdrian Chadd 		rum_setbits(sc, RT2573_TXRX_CSR4,
1623a05022b2SAdrian Chadd 		    RT2573_MRR_ENABLED | RT2573_MRR_CCK_FALLBACK);
1624a05022b2SAdrian Chadd 	} else {
1625a05022b2SAdrian Chadd 		rum_modbits(sc, RT2573_TXRX_CSR4,
1626a05022b2SAdrian Chadd 		    RT2573_MRR_ENABLED, RT2573_MRR_CCK_FALLBACK);
1627a05022b2SAdrian Chadd 	}
162802ac6454SAndrew Thompson }
162902ac6454SAndrew Thompson 
163002ac6454SAndrew Thompson static void
163102ac6454SAndrew Thompson rum_set_txpreamble(struct rum_softc *sc)
163202ac6454SAndrew Thompson {
16337a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
163402ac6454SAndrew Thompson 
163502ac6454SAndrew Thompson 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1636a05022b2SAdrian Chadd 		rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
1637a05022b2SAdrian Chadd 	else
1638a05022b2SAdrian Chadd 		rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
163902ac6454SAndrew Thompson }
164002ac6454SAndrew Thompson 
164102ac6454SAndrew Thompson static void
164202ac6454SAndrew Thompson rum_set_basicrates(struct rum_softc *sc)
164302ac6454SAndrew Thompson {
16447a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
164502ac6454SAndrew Thompson 
164602ac6454SAndrew Thompson 	/* update basic rate set */
164702ac6454SAndrew Thompson 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
164802ac6454SAndrew Thompson 		/* 11b basic rates: 1, 2Mbps */
164902ac6454SAndrew Thompson 		rum_write(sc, RT2573_TXRX_CSR5, 0x3);
165002ac6454SAndrew Thompson 	} else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
165102ac6454SAndrew Thompson 		/* 11a basic rates: 6, 12, 24Mbps */
165202ac6454SAndrew Thompson 		rum_write(sc, RT2573_TXRX_CSR5, 0x150);
165302ac6454SAndrew Thompson 	} else {
165402ac6454SAndrew Thompson 		/* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
165502ac6454SAndrew Thompson 		rum_write(sc, RT2573_TXRX_CSR5, 0xf);
165602ac6454SAndrew Thompson 	}
165702ac6454SAndrew Thompson }
165802ac6454SAndrew Thompson 
165902ac6454SAndrew Thompson /*
166002ac6454SAndrew Thompson  * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
166102ac6454SAndrew Thompson  * driver.
166202ac6454SAndrew Thompson  */
166302ac6454SAndrew Thompson static void
166402ac6454SAndrew Thompson rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
166502ac6454SAndrew Thompson {
166602ac6454SAndrew Thompson 	uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
166702ac6454SAndrew Thompson 
166802ac6454SAndrew Thompson 	/* update all BBP registers that depend on the band */
166902ac6454SAndrew Thompson 	bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
167002ac6454SAndrew Thompson 	bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
167102ac6454SAndrew Thompson 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
167202ac6454SAndrew Thompson 		bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
167302ac6454SAndrew Thompson 		bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
167402ac6454SAndrew Thompson 	}
167502ac6454SAndrew Thompson 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
167602ac6454SAndrew Thompson 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
167702ac6454SAndrew Thompson 		bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
167802ac6454SAndrew Thompson 	}
167902ac6454SAndrew Thompson 
168002ac6454SAndrew Thompson 	sc->bbp17 = bbp17;
168102ac6454SAndrew Thompson 	rum_bbp_write(sc,  17, bbp17);
168202ac6454SAndrew Thompson 	rum_bbp_write(sc,  96, bbp96);
168302ac6454SAndrew Thompson 	rum_bbp_write(sc, 104, bbp104);
168402ac6454SAndrew Thompson 
168502ac6454SAndrew Thompson 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
168602ac6454SAndrew Thompson 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
168702ac6454SAndrew Thompson 		rum_bbp_write(sc, 75, 0x80);
168802ac6454SAndrew Thompson 		rum_bbp_write(sc, 86, 0x80);
168902ac6454SAndrew Thompson 		rum_bbp_write(sc, 88, 0x80);
169002ac6454SAndrew Thompson 	}
169102ac6454SAndrew Thompson 
169202ac6454SAndrew Thompson 	rum_bbp_write(sc, 35, bbp35);
169302ac6454SAndrew Thompson 	rum_bbp_write(sc, 97, bbp97);
169402ac6454SAndrew Thompson 	rum_bbp_write(sc, 98, bbp98);
169502ac6454SAndrew Thompson 
1696a05022b2SAdrian Chadd 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
1697a05022b2SAdrian Chadd 		rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_2GHZ,
1698a05022b2SAdrian Chadd 		    RT2573_PA_PE_5GHZ);
1699a05022b2SAdrian Chadd 	} else {
1700a05022b2SAdrian Chadd 		rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_5GHZ,
1701a05022b2SAdrian Chadd 		    RT2573_PA_PE_2GHZ);
1702a05022b2SAdrian Chadd 	}
170302ac6454SAndrew Thompson }
170402ac6454SAndrew Thompson 
170502ac6454SAndrew Thompson static void
170602ac6454SAndrew Thompson rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
170702ac6454SAndrew Thompson {
17087a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
170902ac6454SAndrew Thompson 	const struct rfprog *rfprog;
171002ac6454SAndrew Thompson 	uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
171102ac6454SAndrew Thompson 	int8_t power;
171202ac6454SAndrew Thompson 	int i, chan;
171302ac6454SAndrew Thompson 
171402ac6454SAndrew Thompson 	chan = ieee80211_chan2ieee(ic, c);
171502ac6454SAndrew Thompson 	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
171602ac6454SAndrew Thompson 		return;
171702ac6454SAndrew Thompson 
171802ac6454SAndrew Thompson 	/* select the appropriate RF settings based on what EEPROM says */
171902ac6454SAndrew Thompson 	rfprog = (sc->rf_rev == RT2573_RF_5225 ||
172002ac6454SAndrew Thompson 		  sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
172102ac6454SAndrew Thompson 
172202ac6454SAndrew Thompson 	/* find the settings for this channel (we know it exists) */
172302ac6454SAndrew Thompson 	for (i = 0; rfprog[i].chan != chan; i++);
172402ac6454SAndrew Thompson 
172502ac6454SAndrew Thompson 	power = sc->txpow[i];
172602ac6454SAndrew Thompson 	if (power < 0) {
172702ac6454SAndrew Thompson 		bbp94 += power;
172802ac6454SAndrew Thompson 		power = 0;
172902ac6454SAndrew Thompson 	} else if (power > 31) {
173002ac6454SAndrew Thompson 		bbp94 += power - 31;
173102ac6454SAndrew Thompson 		power = 31;
173202ac6454SAndrew Thompson 	}
173302ac6454SAndrew Thompson 
173402ac6454SAndrew Thompson 	/*
173502ac6454SAndrew Thompson 	 * If we are switching from the 2GHz band to the 5GHz band or
173602ac6454SAndrew Thompson 	 * vice-versa, BBP registers need to be reprogrammed.
173702ac6454SAndrew Thompson 	 */
173802ac6454SAndrew Thompson 	if (c->ic_flags != ic->ic_curchan->ic_flags) {
173902ac6454SAndrew Thompson 		rum_select_band(sc, c);
174002ac6454SAndrew Thompson 		rum_select_antenna(sc);
174102ac6454SAndrew Thompson 	}
174202ac6454SAndrew Thompson 	ic->ic_curchan = c;
174302ac6454SAndrew Thompson 
174402ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
174502ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
174602ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
174702ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
174802ac6454SAndrew Thompson 
174902ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
175002ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
175102ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
175202ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
175302ac6454SAndrew Thompson 
175402ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
175502ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
175602ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
175702ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
175802ac6454SAndrew Thompson 
175902ac6454SAndrew Thompson 	rum_pause(sc, hz / 100);
176002ac6454SAndrew Thompson 
176102ac6454SAndrew Thompson 	/* enable smart mode for MIMO-capable RFs */
176202ac6454SAndrew Thompson 	bbp3 = rum_bbp_read(sc, 3);
176302ac6454SAndrew Thompson 
176402ac6454SAndrew Thompson 	bbp3 &= ~RT2573_SMART_MODE;
176502ac6454SAndrew Thompson 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
176602ac6454SAndrew Thompson 		bbp3 |= RT2573_SMART_MODE;
176702ac6454SAndrew Thompson 
176802ac6454SAndrew Thompson 	rum_bbp_write(sc, 3, bbp3);
176902ac6454SAndrew Thompson 
177002ac6454SAndrew Thompson 	if (bbp94 != RT2573_BBPR94_DEFAULT)
177102ac6454SAndrew Thompson 		rum_bbp_write(sc, 94, bbp94);
177277ddf3d3SAndrew Thompson 
177377ddf3d3SAndrew Thompson 	/* give the chip some extra time to do the switchover */
177477ddf3d3SAndrew Thompson 	rum_pause(sc, hz / 100);
177502ac6454SAndrew Thompson }
177602ac6454SAndrew Thompson 
177702ac6454SAndrew Thompson /*
177802ac6454SAndrew Thompson  * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
177902ac6454SAndrew Thompson  * and HostAP operating modes.
178002ac6454SAndrew Thompson  */
178102ac6454SAndrew Thompson static void
178202ac6454SAndrew Thompson rum_enable_tsf_sync(struct rum_softc *sc)
178302ac6454SAndrew Thompson {
17847a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
178502ac6454SAndrew Thompson 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
178602ac6454SAndrew Thompson 	uint32_t tmp;
178702ac6454SAndrew Thompson 
178802ac6454SAndrew Thompson 	if (vap->iv_opmode != IEEE80211_M_STA) {
178902ac6454SAndrew Thompson 		/*
179002ac6454SAndrew Thompson 		 * Change default 16ms TBTT adjustment to 8ms.
179102ac6454SAndrew Thompson 		 * Must be done before enabling beacon generation.
179202ac6454SAndrew Thompson 		 */
179302ac6454SAndrew Thompson 		rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
179402ac6454SAndrew Thompson 	}
179502ac6454SAndrew Thompson 
179602ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
179702ac6454SAndrew Thompson 
179802ac6454SAndrew Thompson 	/* set beacon interval (in 1/16ms unit) */
179902ac6454SAndrew Thompson 	tmp |= vap->iv_bss->ni_intval * 16;
1800d847071cSAdrian Chadd 	tmp |= RT2573_TSF_TIMER_EN | RT2573_TBTT_TIMER_EN;
180102ac6454SAndrew Thompson 
1802d847071cSAdrian Chadd 	switch (vap->iv_opmode) {
1803d847071cSAdrian Chadd 	case IEEE80211_M_STA:
1804d847071cSAdrian Chadd 		/*
1805d847071cSAdrian Chadd 		 * Local TSF is always updated with remote TSF on beacon
1806d847071cSAdrian Chadd 		 * reception.
1807d847071cSAdrian Chadd 		 */
1808d847071cSAdrian Chadd 		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_STA);
1809d847071cSAdrian Chadd 		break;
1810d847071cSAdrian Chadd 	case IEEE80211_M_IBSS:
1811d847071cSAdrian Chadd 		/*
1812d847071cSAdrian Chadd 		 * Local TSF is updated with remote TSF on beacon reception
1813d847071cSAdrian Chadd 		 * only if the remote TSF is greater than local TSF.
1814d847071cSAdrian Chadd 		 */
1815d847071cSAdrian Chadd 		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_IBSS);
1816d847071cSAdrian Chadd 		tmp |= RT2573_BCN_TX_EN;
1817d847071cSAdrian Chadd 		break;
1818d847071cSAdrian Chadd 	case IEEE80211_M_HOSTAP:
1819d847071cSAdrian Chadd 		/* SYNC with nobody */
1820d847071cSAdrian Chadd 		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_HOSTAP);
1821d847071cSAdrian Chadd 		tmp |= RT2573_BCN_TX_EN;
1822d847071cSAdrian Chadd 		break;
1823d847071cSAdrian Chadd 	default:
1824d847071cSAdrian Chadd 		device_printf(sc->sc_dev,
1825d847071cSAdrian Chadd 		    "Enabling TSF failed. undefined opmode %d\n",
1826d847071cSAdrian Chadd 		    vap->iv_opmode);
1827d847071cSAdrian Chadd 		return;
1828d847071cSAdrian Chadd 	}
182902ac6454SAndrew Thompson 
183002ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR9, tmp);
183102ac6454SAndrew Thompson }
183202ac6454SAndrew Thompson 
183302ac6454SAndrew Thompson static void
18345463c4a4SSam Leffler rum_enable_tsf(struct rum_softc *sc)
18355463c4a4SSam Leffler {
1836d847071cSAdrian Chadd 	rum_modbits(sc, RT2573_TXRX_CSR9, RT2573_TSF_TIMER_EN |
1837d847071cSAdrian Chadd 	    RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_DIS), 0x00ffffff);
18385463c4a4SSam Leffler }
18395463c4a4SSam Leffler 
18405463c4a4SSam Leffler static void
18416d804321SAdrian Chadd rum_abort_tsf_sync(struct rum_softc *sc)
18426d804321SAdrian Chadd {
1843a05022b2SAdrian Chadd 	rum_clrbits(sc, RT2573_TXRX_CSR9, 0x00ffffff);
18446d804321SAdrian Chadd }
18456d804321SAdrian Chadd 
18466d804321SAdrian Chadd static void
1847a6ccd477SAdrian Chadd rum_get_tsf(struct rum_softc *sc, uint64_t *buf)
1848a6ccd477SAdrian Chadd {
1849a6ccd477SAdrian Chadd 	rum_read_multi(sc, RT2573_TXRX_CSR12, buf, sizeof (*buf));
1850a6ccd477SAdrian Chadd }
1851a6ccd477SAdrian Chadd 
1852a6ccd477SAdrian Chadd static void
18537a79cebfSGleb Smirnoff rum_update_slot(struct rum_softc *sc)
185402ac6454SAndrew Thompson {
18557a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
185602ac6454SAndrew Thompson 	uint8_t slottime;
185702ac6454SAndrew Thompson 
185802ac6454SAndrew Thompson 	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
185902ac6454SAndrew Thompson 
1860a05022b2SAdrian Chadd 	rum_modbits(sc, RT2573_MAC_CSR9, slottime, 0xff);
186102ac6454SAndrew Thompson 
186202ac6454SAndrew Thompson 	DPRINTF("setting slot time to %uus\n", slottime);
186302ac6454SAndrew Thompson }
186402ac6454SAndrew Thompson 
186502ac6454SAndrew Thompson static void
186602ac6454SAndrew Thompson rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
186702ac6454SAndrew Thompson {
186802ac6454SAndrew Thompson 	uint32_t tmp;
186902ac6454SAndrew Thompson 
187002ac6454SAndrew Thompson 	tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
187102ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR4, tmp);
187202ac6454SAndrew Thompson 
187302ac6454SAndrew Thompson 	tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
187402ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR5, tmp);
187502ac6454SAndrew Thompson }
187602ac6454SAndrew Thompson 
187702ac6454SAndrew Thompson static void
187802ac6454SAndrew Thompson rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
187902ac6454SAndrew Thompson {
188002ac6454SAndrew Thompson 	uint32_t tmp;
188102ac6454SAndrew Thompson 
188202ac6454SAndrew Thompson 	tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
188302ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR2, tmp);
188402ac6454SAndrew Thompson 
188502ac6454SAndrew Thompson 	tmp = addr[4] | addr[5] << 8 | 0xff << 16;
188602ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR3, tmp);
188702ac6454SAndrew Thompson }
188802ac6454SAndrew Thompson 
188902ac6454SAndrew Thompson static void
18905efea30fSAndrew Thompson rum_setpromisc(struct rum_softc *sc)
189102ac6454SAndrew Thompson {
1892a05022b2SAdrian Chadd 	struct ieee80211com *ic = &sc->sc_ic;
189302ac6454SAndrew Thompson 
1894a05022b2SAdrian Chadd 	if (ic->ic_promisc == 0)
1895a05022b2SAdrian Chadd 		rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
1896a05022b2SAdrian Chadd 	else
1897a05022b2SAdrian Chadd 		rum_clrbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
189802ac6454SAndrew Thompson 
1899a05022b2SAdrian Chadd 	DPRINTF("%s promiscuous mode\n", ic->ic_promisc > 0 ?
190002ac6454SAndrew Thompson 	    "entering" : "leaving");
190102ac6454SAndrew Thompson }
190202ac6454SAndrew Thompson 
190377ddf3d3SAndrew Thompson static void
1904272f6adeSGleb Smirnoff rum_update_promisc(struct ieee80211com *ic)
190577ddf3d3SAndrew Thompson {
1906272f6adeSGleb Smirnoff 	struct rum_softc *sc = ic->ic_softc;
190777ddf3d3SAndrew Thompson 
1908ba2c1fbcSAdrian Chadd 	RUM_LOCK(sc);
19097a79cebfSGleb Smirnoff 	if (!sc->sc_running) {
19107a79cebfSGleb Smirnoff 		RUM_UNLOCK(sc);
19117a79cebfSGleb Smirnoff 		return;
19127a79cebfSGleb Smirnoff 	}
19135efea30fSAndrew Thompson 	rum_setpromisc(sc);
191477ddf3d3SAndrew Thompson 	RUM_UNLOCK(sc);
191577ddf3d3SAndrew Thompson }
191677ddf3d3SAndrew Thompson 
19176dd8e071SEd Maste static void
1918272f6adeSGleb Smirnoff rum_update_mcast(struct ieee80211com *ic)
19196dd8e071SEd Maste {
1920e7b7db3dSAdrian Chadd 	/* Ignore. */
19216dd8e071SEd Maste }
19226dd8e071SEd Maste 
192302ac6454SAndrew Thompson static const char *
192402ac6454SAndrew Thompson rum_get_rf(int rev)
192502ac6454SAndrew Thompson {
192602ac6454SAndrew Thompson 	switch (rev) {
192702ac6454SAndrew Thompson 	case RT2573_RF_2527:	return "RT2527 (MIMO XR)";
192802ac6454SAndrew Thompson 	case RT2573_RF_2528:	return "RT2528";
192902ac6454SAndrew Thompson 	case RT2573_RF_5225:	return "RT5225 (MIMO XR)";
193002ac6454SAndrew Thompson 	case RT2573_RF_5226:	return "RT5226";
193102ac6454SAndrew Thompson 	default:		return "unknown";
193202ac6454SAndrew Thompson 	}
193302ac6454SAndrew Thompson }
193402ac6454SAndrew Thompson 
193502ac6454SAndrew Thompson static void
193602ac6454SAndrew Thompson rum_read_eeprom(struct rum_softc *sc)
193702ac6454SAndrew Thompson {
193802ac6454SAndrew Thompson 	uint16_t val;
193902ac6454SAndrew Thompson #ifdef RUM_DEBUG
194002ac6454SAndrew Thompson 	int i;
194102ac6454SAndrew Thompson #endif
194202ac6454SAndrew Thompson 
194302ac6454SAndrew Thompson 	/* read MAC address */
19447a79cebfSGleb Smirnoff 	rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_ic.ic_macaddr, 6);
194502ac6454SAndrew Thompson 
194602ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
194702ac6454SAndrew Thompson 	val = le16toh(val);
194802ac6454SAndrew Thompson 	sc->rf_rev =   (val >> 11) & 0x1f;
194902ac6454SAndrew Thompson 	sc->hw_radio = (val >> 10) & 0x1;
195002ac6454SAndrew Thompson 	sc->rx_ant =   (val >> 4)  & 0x3;
195102ac6454SAndrew Thompson 	sc->tx_ant =   (val >> 2)  & 0x3;
195202ac6454SAndrew Thompson 	sc->nb_ant =   val & 0x3;
195302ac6454SAndrew Thompson 
195402ac6454SAndrew Thompson 	DPRINTF("RF revision=%d\n", sc->rf_rev);
195502ac6454SAndrew Thompson 
195602ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
195702ac6454SAndrew Thompson 	val = le16toh(val);
195802ac6454SAndrew Thompson 	sc->ext_5ghz_lna = (val >> 6) & 0x1;
195902ac6454SAndrew Thompson 	sc->ext_2ghz_lna = (val >> 4) & 0x1;
196002ac6454SAndrew Thompson 
196102ac6454SAndrew Thompson 	DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
196202ac6454SAndrew Thompson 	    sc->ext_2ghz_lna, sc->ext_5ghz_lna);
196302ac6454SAndrew Thompson 
196402ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
196502ac6454SAndrew Thompson 	val = le16toh(val);
196602ac6454SAndrew Thompson 	if ((val & 0xff) != 0xff)
196702ac6454SAndrew Thompson 		sc->rssi_2ghz_corr = (int8_t)(val & 0xff);	/* signed */
196802ac6454SAndrew Thompson 
196902ac6454SAndrew Thompson 	/* Only [-10, 10] is valid */
197002ac6454SAndrew Thompson 	if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
197102ac6454SAndrew Thompson 		sc->rssi_2ghz_corr = 0;
197202ac6454SAndrew Thompson 
197302ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
197402ac6454SAndrew Thompson 	val = le16toh(val);
197502ac6454SAndrew Thompson 	if ((val & 0xff) != 0xff)
197602ac6454SAndrew Thompson 		sc->rssi_5ghz_corr = (int8_t)(val & 0xff);	/* signed */
197702ac6454SAndrew Thompson 
197802ac6454SAndrew Thompson 	/* Only [-10, 10] is valid */
197902ac6454SAndrew Thompson 	if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
198002ac6454SAndrew Thompson 		sc->rssi_5ghz_corr = 0;
198102ac6454SAndrew Thompson 
198202ac6454SAndrew Thompson 	if (sc->ext_2ghz_lna)
198302ac6454SAndrew Thompson 		sc->rssi_2ghz_corr -= 14;
198402ac6454SAndrew Thompson 	if (sc->ext_5ghz_lna)
198502ac6454SAndrew Thompson 		sc->rssi_5ghz_corr -= 14;
198602ac6454SAndrew Thompson 
198702ac6454SAndrew Thompson 	DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
198802ac6454SAndrew Thompson 	    sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
198902ac6454SAndrew Thompson 
199002ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
199102ac6454SAndrew Thompson 	val = le16toh(val);
199202ac6454SAndrew Thompson 	if ((val & 0xff) != 0xff)
199302ac6454SAndrew Thompson 		sc->rffreq = val & 0xff;
199402ac6454SAndrew Thompson 
199502ac6454SAndrew Thompson 	DPRINTF("RF freq=%d\n", sc->rffreq);
199602ac6454SAndrew Thompson 
199702ac6454SAndrew Thompson 	/* read Tx power for all a/b/g channels */
199802ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
199902ac6454SAndrew Thompson 	/* XXX default Tx power for 802.11a channels */
200002ac6454SAndrew Thompson 	memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
200102ac6454SAndrew Thompson #ifdef RUM_DEBUG
200202ac6454SAndrew Thompson 	for (i = 0; i < 14; i++)
200302ac6454SAndrew Thompson 		DPRINTF("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]);
200402ac6454SAndrew Thompson #endif
200502ac6454SAndrew Thompson 
200602ac6454SAndrew Thompson 	/* read default values for BBP registers */
200702ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
200802ac6454SAndrew Thompson #ifdef RUM_DEBUG
200902ac6454SAndrew Thompson 	for (i = 0; i < 14; i++) {
201002ac6454SAndrew Thompson 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
201102ac6454SAndrew Thompson 			continue;
201202ac6454SAndrew Thompson 		DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
201302ac6454SAndrew Thompson 		    sc->bbp_prom[i].val);
201402ac6454SAndrew Thompson 	}
201502ac6454SAndrew Thompson #endif
201602ac6454SAndrew Thompson }
201702ac6454SAndrew Thompson 
201802ac6454SAndrew Thompson static int
201902ac6454SAndrew Thompson rum_bbp_init(struct rum_softc *sc)
202002ac6454SAndrew Thompson {
202102ac6454SAndrew Thompson 	int i, ntries;
202202ac6454SAndrew Thompson 
202302ac6454SAndrew Thompson 	/* wait for BBP to be ready */
202402ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
202502ac6454SAndrew Thompson 		const uint8_t val = rum_bbp_read(sc, 0);
202602ac6454SAndrew Thompson 		if (val != 0 && val != 0xff)
202702ac6454SAndrew Thompson 			break;
202802ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
202902ac6454SAndrew Thompson 			break;
203002ac6454SAndrew Thompson 	}
203102ac6454SAndrew Thompson 	if (ntries == 100) {
203202ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "timeout waiting for BBP\n");
203302ac6454SAndrew Thompson 		return EIO;
203402ac6454SAndrew Thompson 	}
203502ac6454SAndrew Thompson 
203602ac6454SAndrew Thompson 	/* initialize BBP registers to default values */
2037e7b7db3dSAdrian Chadd 	for (i = 0; i < nitems(rum_def_bbp); i++)
203802ac6454SAndrew Thompson 		rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
203902ac6454SAndrew Thompson 
204002ac6454SAndrew Thompson 	/* write vendor-specific BBP values (from EEPROM) */
204102ac6454SAndrew Thompson 	for (i = 0; i < 16; i++) {
204202ac6454SAndrew Thompson 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
204302ac6454SAndrew Thompson 			continue;
204402ac6454SAndrew Thompson 		rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
204502ac6454SAndrew Thompson 	}
204602ac6454SAndrew Thompson 
204702ac6454SAndrew Thompson 	return 0;
204802ac6454SAndrew Thompson }
204902ac6454SAndrew Thompson 
205002ac6454SAndrew Thompson static void
20517a79cebfSGleb Smirnoff rum_init(struct rum_softc *sc)
205202ac6454SAndrew Thompson {
20537a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
20547a79cebfSGleb Smirnoff 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
205502ac6454SAndrew Thompson 	uint32_t tmp;
2056e0a69b51SAndrew Thompson 	usb_error_t error;
205702ac6454SAndrew Thompson 	int i, ntries;
205802ac6454SAndrew Thompson 
2059e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
206002ac6454SAndrew Thompson 
20615efea30fSAndrew Thompson 	rum_stop(sc);
206202ac6454SAndrew Thompson 
206302ac6454SAndrew Thompson 	/* initialize MAC registers to default values */
2064e7b7db3dSAdrian Chadd 	for (i = 0; i < nitems(rum_def_mac); i++)
206502ac6454SAndrew Thompson 		rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
206602ac6454SAndrew Thompson 
206702ac6454SAndrew Thompson 	/* set host ready */
2068e7b7db3dSAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
206902ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR1, 0);
207002ac6454SAndrew Thompson 
207102ac6454SAndrew Thompson 	/* wait for BBP/RF to wakeup */
207202ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
207302ac6454SAndrew Thompson 		if (rum_read(sc, RT2573_MAC_CSR12) & 8)
207402ac6454SAndrew Thompson 			break;
207502ac6454SAndrew Thompson 		rum_write(sc, RT2573_MAC_CSR12, 4);	/* force wakeup */
207602ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
207702ac6454SAndrew Thompson 			break;
207802ac6454SAndrew Thompson 	}
207902ac6454SAndrew Thompson 	if (ntries == 100) {
208002ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
208102ac6454SAndrew Thompson 		    "timeout waiting for BBP/RF to wakeup\n");
208202ac6454SAndrew Thompson 		goto fail;
208302ac6454SAndrew Thompson 	}
208402ac6454SAndrew Thompson 
208502ac6454SAndrew Thompson 	if ((error = rum_bbp_init(sc)) != 0)
208602ac6454SAndrew Thompson 		goto fail;
208702ac6454SAndrew Thompson 
208802ac6454SAndrew Thompson 	/* select default channel */
208902ac6454SAndrew Thompson 	rum_select_band(sc, ic->ic_curchan);
209002ac6454SAndrew Thompson 	rum_select_antenna(sc);
209102ac6454SAndrew Thompson 	rum_set_chan(sc, ic->ic_curchan);
209202ac6454SAndrew Thompson 
209302ac6454SAndrew Thompson 	/* clear STA registers */
209402ac6454SAndrew Thompson 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
209502ac6454SAndrew Thompson 
20967a79cebfSGleb Smirnoff 	rum_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
209702ac6454SAndrew Thompson 
209802ac6454SAndrew Thompson 	/* initialize ASIC */
2099e7b7db3dSAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR1, RT2573_HOST_READY);
210002ac6454SAndrew Thompson 
210102ac6454SAndrew Thompson 	/*
210202ac6454SAndrew Thompson 	 * Allocate Tx and Rx xfer queues.
210302ac6454SAndrew Thompson 	 */
210402ac6454SAndrew Thompson 	rum_setup_tx_list(sc);
210502ac6454SAndrew Thompson 
210602ac6454SAndrew Thompson 	/* update Rx filter */
210702ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
210802ac6454SAndrew Thompson 
210902ac6454SAndrew Thompson 	tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
211002ac6454SAndrew Thompson 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
211102ac6454SAndrew Thompson 		tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
211202ac6454SAndrew Thompson 		       RT2573_DROP_ACKCTS;
211302ac6454SAndrew Thompson 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
211402ac6454SAndrew Thompson 			tmp |= RT2573_DROP_TODS;
21157a79cebfSGleb Smirnoff 		if (ic->ic_promisc == 0)
211602ac6454SAndrew Thompson 			tmp |= RT2573_DROP_NOT_TO_ME;
211702ac6454SAndrew Thompson 	}
211802ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
211902ac6454SAndrew Thompson 
21207a79cebfSGleb Smirnoff 	sc->sc_running = 1;
2121ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2122a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
212302ac6454SAndrew Thompson 	return;
212402ac6454SAndrew Thompson 
21255efea30fSAndrew Thompson fail:	rum_stop(sc);
212602ac6454SAndrew Thompson #undef N
212702ac6454SAndrew Thompson }
212802ac6454SAndrew Thompson 
212902ac6454SAndrew Thompson static void
21305efea30fSAndrew Thompson rum_stop(struct rum_softc *sc)
213102ac6454SAndrew Thompson {
213202ac6454SAndrew Thompson 
2133e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
213402ac6454SAndrew Thompson 
21357a79cebfSGleb Smirnoff 	sc->sc_running = 0;
213602ac6454SAndrew Thompson 
213702ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
213802ac6454SAndrew Thompson 
213902ac6454SAndrew Thompson 	/*
214002ac6454SAndrew Thompson 	 * Drain the USB transfers, if not already drained:
214102ac6454SAndrew Thompson 	 */
2142a593f6b8SAndrew Thompson 	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2143a593f6b8SAndrew Thompson 	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
214402ac6454SAndrew Thompson 
214502ac6454SAndrew Thompson 	RUM_LOCK(sc);
214602ac6454SAndrew Thompson 
214702ac6454SAndrew Thompson 	rum_unsetup_tx_list(sc);
214802ac6454SAndrew Thompson 
214902ac6454SAndrew Thompson 	/* disable Rx */
2150a05022b2SAdrian Chadd 	rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DISABLE_RX);
215102ac6454SAndrew Thompson 
215202ac6454SAndrew Thompson 	/* reset ASIC */
2153e7b7db3dSAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
215402ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR1, 0);
215502ac6454SAndrew Thompson }
215602ac6454SAndrew Thompson 
215777ddf3d3SAndrew Thompson static void
215877ddf3d3SAndrew Thompson rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
215902ac6454SAndrew Thompson {
2160760bc48eSAndrew Thompson 	struct usb_device_request req;
216102ac6454SAndrew Thompson 	uint16_t reg = RT2573_MCU_CODE_BASE;
2162e0a69b51SAndrew Thompson 	usb_error_t err;
216302ac6454SAndrew Thompson 
216402ac6454SAndrew Thompson 	/* copy firmware image into NIC */
216577ddf3d3SAndrew Thompson 	for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
216677ddf3d3SAndrew Thompson 		err = rum_write(sc, reg, UGETDW(ucode));
216777ddf3d3SAndrew Thompson 		if (err) {
216877ddf3d3SAndrew Thompson 			/* firmware already loaded ? */
216977ddf3d3SAndrew Thompson 			device_printf(sc->sc_dev, "Firmware load "
217077ddf3d3SAndrew Thompson 			    "failure! (ignored)\n");
217177ddf3d3SAndrew Thompson 			break;
217277ddf3d3SAndrew Thompson 		}
217377ddf3d3SAndrew Thompson 	}
217402ac6454SAndrew Thompson 
217502ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
217602ac6454SAndrew Thompson 	req.bRequest = RT2573_MCU_CNTL;
217702ac6454SAndrew Thompson 	USETW(req.wValue, RT2573_MCU_RUN);
217802ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
217902ac6454SAndrew Thompson 	USETW(req.wLength, 0);
218002ac6454SAndrew Thompson 
218177ddf3d3SAndrew Thompson 	err = rum_do_request(sc, &req, NULL);
218277ddf3d3SAndrew Thompson 	if (err != 0) {
218302ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not run firmware: %s\n",
2184a593f6b8SAndrew Thompson 		    usbd_errstr(err));
218502ac6454SAndrew Thompson 	}
218677ddf3d3SAndrew Thompson 
218777ddf3d3SAndrew Thompson 	/* give the chip some time to boot */
218877ddf3d3SAndrew Thompson 	rum_pause(sc, hz / 8);
218902ac6454SAndrew Thompson }
219002ac6454SAndrew Thompson 
2191f620e64dSKevin Lo static void
219202ac6454SAndrew Thompson rum_prepare_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
219302ac6454SAndrew Thompson {
219402ac6454SAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
219502ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
219602ac6454SAndrew Thompson 	struct rum_tx_desc desc;
219702ac6454SAndrew Thompson 	struct mbuf *m0;
219802ac6454SAndrew Thompson 
2199f620e64dSKevin Lo 	if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC)
2200f620e64dSKevin Lo 		return;
2201bb571462SHans Petter Selasky 	if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2202bb571462SHans Petter Selasky 		return;
2203f620e64dSKevin Lo 
220430c00a2dSAdrian Chadd 	m0 = ieee80211_beacon_alloc(vap->iv_bss, &vap->iv_bcn_off);
2205bb571462SHans Petter Selasky 	if (m0 == NULL)
2206f620e64dSKevin Lo 		return;
220702ac6454SAndrew Thompson 
220802ac6454SAndrew Thompson 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
220902ac6454SAndrew Thompson 	rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
221002ac6454SAndrew Thompson 	    m0->m_pkthdr.len, tp->mgmtrate);
221102ac6454SAndrew Thompson 
221202ac6454SAndrew Thompson 	/* copy the first 24 bytes of Tx descriptor into NIC memory */
221302ac6454SAndrew Thompson 	rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
221402ac6454SAndrew Thompson 
221502ac6454SAndrew Thompson 	/* copy beacon header and payload into NIC memory */
221602ac6454SAndrew Thompson 	rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
221702ac6454SAndrew Thompson 	    m0->m_pkthdr.len);
221802ac6454SAndrew Thompson 
221902ac6454SAndrew Thompson 	m_freem(m0);
222002ac6454SAndrew Thompson }
222102ac6454SAndrew Thompson 
222202ac6454SAndrew Thompson static int
222302ac6454SAndrew Thompson rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
222402ac6454SAndrew Thompson     const struct ieee80211_bpf_params *params)
222502ac6454SAndrew Thompson {
2226d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ni->ni_ic->ic_softc;
222702ac6454SAndrew Thompson 
222802ac6454SAndrew Thompson 	RUM_LOCK(sc);
222902ac6454SAndrew Thompson 	/* prevent management frames from being sent if we're not ready */
22307a79cebfSGleb Smirnoff 	if (!sc->sc_running) {
223102ac6454SAndrew Thompson 		RUM_UNLOCK(sc);
223202ac6454SAndrew Thompson 		m_freem(m);
223302ac6454SAndrew Thompson 		ieee80211_free_node(ni);
223402ac6454SAndrew Thompson 		return ENETDOWN;
223502ac6454SAndrew Thompson 	}
223696ca458fSAndrew Thompson 	if (sc->tx_nfree < RUM_TX_MINFREE) {
223702ac6454SAndrew Thompson 		RUM_UNLOCK(sc);
223802ac6454SAndrew Thompson 		m_freem(m);
223902ac6454SAndrew Thompson 		ieee80211_free_node(ni);
224002ac6454SAndrew Thompson 		return EIO;
224102ac6454SAndrew Thompson 	}
224202ac6454SAndrew Thompson 
224302ac6454SAndrew Thompson 	if (params == NULL) {
224402ac6454SAndrew Thompson 		/*
224502ac6454SAndrew Thompson 		 * Legacy path; interpret frame contents to decide
224602ac6454SAndrew Thompson 		 * precisely how to send the frame.
224702ac6454SAndrew Thompson 		 */
224802ac6454SAndrew Thompson 		if (rum_tx_mgt(sc, m, ni) != 0)
224902ac6454SAndrew Thompson 			goto bad;
225002ac6454SAndrew Thompson 	} else {
225102ac6454SAndrew Thompson 		/*
225202ac6454SAndrew Thompson 		 * Caller supplied explicit parameters to use in
225302ac6454SAndrew Thompson 		 * sending the frame.
225402ac6454SAndrew Thompson 		 */
225502ac6454SAndrew Thompson 		if (rum_tx_raw(sc, m, ni, params) != 0)
225602ac6454SAndrew Thompson 			goto bad;
225702ac6454SAndrew Thompson 	}
225802ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
225902ac6454SAndrew Thompson 
226002ac6454SAndrew Thompson 	return 0;
226102ac6454SAndrew Thompson bad:
226202ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
226302ac6454SAndrew Thompson 	ieee80211_free_node(ni);
226402ac6454SAndrew Thompson 	return EIO;
226502ac6454SAndrew Thompson }
226602ac6454SAndrew Thompson 
226702ac6454SAndrew Thompson static void
2268b6108616SRui Paulo rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
226902ac6454SAndrew Thompson {
227002ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
227102ac6454SAndrew Thompson 	struct rum_vap *rvp = RUM_VAP(vap);
227202ac6454SAndrew Thompson 
227302ac6454SAndrew Thompson 	/* clear statistic registers (STA_CSR0 to STA_CSR5) */
227402ac6454SAndrew Thompson 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
227502ac6454SAndrew Thompson 
2276b6108616SRui Paulo 	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
227702ac6454SAndrew Thompson }
227802ac6454SAndrew Thompson 
227902ac6454SAndrew Thompson static void
2280b6108616SRui Paulo rum_ratectl_timeout(void *arg)
228102ac6454SAndrew Thompson {
228202ac6454SAndrew Thompson 	struct rum_vap *rvp = arg;
22835efea30fSAndrew Thompson 	struct ieee80211vap *vap = &rvp->vap;
22845efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
228502ac6454SAndrew Thompson 
2286b6108616SRui Paulo 	ieee80211_runtask(ic, &rvp->ratectl_task);
228702ac6454SAndrew Thompson }
228802ac6454SAndrew Thompson 
228902ac6454SAndrew Thompson static void
2290b6108616SRui Paulo rum_ratectl_task(void *arg, int pending)
229102ac6454SAndrew Thompson {
22925efea30fSAndrew Thompson 	struct rum_vap *rvp = arg;
22935efea30fSAndrew Thompson 	struct ieee80211vap *vap = &rvp->vap;
22945efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
2295d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
22968d8bdb01SAndrew Thompson 	struct ieee80211_node *ni;
229702ac6454SAndrew Thompson 	int ok, fail;
2298b6108616SRui Paulo 	int sum, retrycnt;
229902ac6454SAndrew Thompson 
23005efea30fSAndrew Thompson 	RUM_LOCK(sc);
230102ac6454SAndrew Thompson 	/* read and clear statistic registers (STA_CSR0 to STA_CSR10) */
230202ac6454SAndrew Thompson 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
230302ac6454SAndrew Thompson 
230402ac6454SAndrew Thompson 	ok = (le32toh(sc->sta[4]) >> 16) +	/* TX ok w/o retry */
230502ac6454SAndrew Thompson 	    (le32toh(sc->sta[5]) & 0xffff);	/* TX ok w/ retry */
230602ac6454SAndrew Thompson 	fail = (le32toh(sc->sta[5]) >> 16);	/* TX retry-fail count */
2307b6108616SRui Paulo 	sum = ok+fail;
2308b6108616SRui Paulo 	retrycnt = (le32toh(sc->sta[5]) & 0xffff) + fail;
230902ac6454SAndrew Thompson 
23108d8bdb01SAndrew Thompson 	ni = ieee80211_ref_node(vap->iv_bss);
2311b6108616SRui Paulo 	ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt);
2312b6108616SRui Paulo 	(void) ieee80211_ratectl_rate(ni, NULL, 0);
23138d8bdb01SAndrew Thompson 	ieee80211_free_node(ni);
231402ac6454SAndrew Thompson 
23157a79cebfSGleb Smirnoff 	/* count TX retry-fail as Tx errors */
23167a79cebfSGleb Smirnoff 	if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, fail);
231702ac6454SAndrew Thompson 
2318b6108616SRui Paulo 	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
23195efea30fSAndrew Thompson 	RUM_UNLOCK(sc);
232002ac6454SAndrew Thompson }
232102ac6454SAndrew Thompson 
232202ac6454SAndrew Thompson static void
232302ac6454SAndrew Thompson rum_scan_start(struct ieee80211com *ic)
232402ac6454SAndrew Thompson {
2325d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
232602ac6454SAndrew Thompson 
232702ac6454SAndrew Thompson 	RUM_LOCK(sc);
23286d804321SAdrian Chadd 	rum_abort_tsf_sync(sc);
23297a79cebfSGleb Smirnoff 	rum_set_bssid(sc, ieee80211broadcastaddr);
233002ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
233102ac6454SAndrew Thompson 
233202ac6454SAndrew Thompson }
233302ac6454SAndrew Thompson 
233402ac6454SAndrew Thompson static void
233502ac6454SAndrew Thompson rum_scan_end(struct ieee80211com *ic)
233602ac6454SAndrew Thompson {
2337d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
233802ac6454SAndrew Thompson 
233902ac6454SAndrew Thompson 	RUM_LOCK(sc);
23405efea30fSAndrew Thompson 	rum_enable_tsf_sync(sc);
23417a79cebfSGleb Smirnoff 	rum_set_bssid(sc, ic->ic_macaddr);
234202ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
234302ac6454SAndrew Thompson 
234402ac6454SAndrew Thompson }
234502ac6454SAndrew Thompson 
234602ac6454SAndrew Thompson static void
234702ac6454SAndrew Thompson rum_set_channel(struct ieee80211com *ic)
234802ac6454SAndrew Thompson {
2349d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
235002ac6454SAndrew Thompson 
235102ac6454SAndrew Thompson 	RUM_LOCK(sc);
235202ac6454SAndrew Thompson 	rum_set_chan(sc, ic->ic_curchan);
23535efea30fSAndrew Thompson 	RUM_UNLOCK(sc);
235402ac6454SAndrew Thompson }
235502ac6454SAndrew Thompson 
235602ac6454SAndrew Thompson static int
235702ac6454SAndrew Thompson rum_get_rssi(struct rum_softc *sc, uint8_t raw)
235802ac6454SAndrew Thompson {
23597a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
236002ac6454SAndrew Thompson 	int lna, agc, rssi;
236102ac6454SAndrew Thompson 
236202ac6454SAndrew Thompson 	lna = (raw >> 5) & 0x3;
236302ac6454SAndrew Thompson 	agc = raw & 0x1f;
236402ac6454SAndrew Thompson 
236502ac6454SAndrew Thompson 	if (lna == 0) {
236602ac6454SAndrew Thompson 		/*
236702ac6454SAndrew Thompson 		 * No RSSI mapping
236802ac6454SAndrew Thompson 		 *
236902ac6454SAndrew Thompson 		 * NB: Since RSSI is relative to noise floor, -1 is
237002ac6454SAndrew Thompson 		 *     adequate for caller to know error happened.
237102ac6454SAndrew Thompson 		 */
237202ac6454SAndrew Thompson 		return -1;
237302ac6454SAndrew Thompson 	}
237402ac6454SAndrew Thompson 
237502ac6454SAndrew Thompson 	rssi = (2 * agc) - RT2573_NOISE_FLOOR;
237602ac6454SAndrew Thompson 
237702ac6454SAndrew Thompson 	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
237802ac6454SAndrew Thompson 		rssi += sc->rssi_2ghz_corr;
237902ac6454SAndrew Thompson 
238002ac6454SAndrew Thompson 		if (lna == 1)
238102ac6454SAndrew Thompson 			rssi -= 64;
238202ac6454SAndrew Thompson 		else if (lna == 2)
238302ac6454SAndrew Thompson 			rssi -= 74;
238402ac6454SAndrew Thompson 		else if (lna == 3)
238502ac6454SAndrew Thompson 			rssi -= 90;
238602ac6454SAndrew Thompson 	} else {
238702ac6454SAndrew Thompson 		rssi += sc->rssi_5ghz_corr;
238802ac6454SAndrew Thompson 
238902ac6454SAndrew Thompson 		if (!sc->ext_5ghz_lna && lna != 1)
239002ac6454SAndrew Thompson 			rssi += 4;
239102ac6454SAndrew Thompson 
239202ac6454SAndrew Thompson 		if (lna == 1)
239302ac6454SAndrew Thompson 			rssi -= 64;
239402ac6454SAndrew Thompson 		else if (lna == 2)
239502ac6454SAndrew Thompson 			rssi -= 86;
239602ac6454SAndrew Thompson 		else if (lna == 3)
239702ac6454SAndrew Thompson 			rssi -= 100;
239802ac6454SAndrew Thompson 	}
239902ac6454SAndrew Thompson 	return rssi;
240002ac6454SAndrew Thompson }
240102ac6454SAndrew Thompson 
240202ac6454SAndrew Thompson static int
240302ac6454SAndrew Thompson rum_pause(struct rum_softc *sc, int timeout)
240402ac6454SAndrew Thompson {
240502ac6454SAndrew Thompson 
2406a593f6b8SAndrew Thompson 	usb_pause_mtx(&sc->sc_mtx, timeout);
240702ac6454SAndrew Thompson 	return (0);
240802ac6454SAndrew Thompson }
240902ac6454SAndrew Thompson 
241002ac6454SAndrew Thompson static device_method_t rum_methods[] = {
241102ac6454SAndrew Thompson 	/* Device interface */
241202ac6454SAndrew Thompson 	DEVMETHOD(device_probe,		rum_match),
241302ac6454SAndrew Thompson 	DEVMETHOD(device_attach,	rum_attach),
241402ac6454SAndrew Thompson 	DEVMETHOD(device_detach,	rum_detach),
2415645e4d17SHans Petter Selasky 	DEVMETHOD_END
241602ac6454SAndrew Thompson };
241702ac6454SAndrew Thompson 
241802ac6454SAndrew Thompson static driver_t rum_driver = {
241902ac6454SAndrew Thompson 	.name = "rum",
242002ac6454SAndrew Thompson 	.methods = rum_methods,
242102ac6454SAndrew Thompson 	.size = sizeof(struct rum_softc),
242202ac6454SAndrew Thompson };
242302ac6454SAndrew Thompson 
242402ac6454SAndrew Thompson static devclass_t rum_devclass;
242502ac6454SAndrew Thompson 
24269aef556dSAndrew Thompson DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
2427910cb8feSAndrew Thompson MODULE_DEPEND(rum, wlan, 1, 1, 1);
2428910cb8feSAndrew Thompson MODULE_DEPEND(rum, usb, 1, 1, 1);
2429910cb8feSAndrew Thompson MODULE_VERSION(rum, 1);
2430