xref: /freebsd/sys/dev/usb/wlan/if_rum.c (revision d204cea9f8a9eb39eb49cda3924aca7de1d16ca3)
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>
7ce036b51SAndriy Voskoboinyk  * Copyright (c) 2015 Andriy Voskoboinyk <avos@FreeBSD.org>
802ac6454SAndrew Thompson  *
902ac6454SAndrew Thompson  * Permission to use, copy, modify, and distribute this software for any
1002ac6454SAndrew Thompson  * purpose with or without fee is hereby granted, provided that the above
1102ac6454SAndrew Thompson  * copyright notice and this permission notice appear in all copies.
1202ac6454SAndrew Thompson  *
1302ac6454SAndrew Thompson  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1402ac6454SAndrew Thompson  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1502ac6454SAndrew Thompson  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1602ac6454SAndrew Thompson  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1702ac6454SAndrew Thompson  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1802ac6454SAndrew Thompson  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1902ac6454SAndrew Thompson  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2002ac6454SAndrew Thompson  */
2102ac6454SAndrew Thompson 
2202ac6454SAndrew Thompson #include <sys/cdefs.h>
2302ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
2402ac6454SAndrew Thompson 
2502ac6454SAndrew Thompson /*-
2602ac6454SAndrew Thompson  * Ralink Technology RT2501USB/RT2601USB chipset driver
2702ac6454SAndrew Thompson  * http://www.ralinktech.com.tw/
2802ac6454SAndrew Thompson  */
2902ac6454SAndrew Thompson 
305efea30fSAndrew Thompson #include <sys/param.h>
315efea30fSAndrew Thompson #include <sys/sockio.h>
325efea30fSAndrew Thompson #include <sys/sysctl.h>
335efea30fSAndrew Thompson #include <sys/lock.h>
345efea30fSAndrew Thompson #include <sys/mutex.h>
355efea30fSAndrew Thompson #include <sys/mbuf.h>
365efea30fSAndrew Thompson #include <sys/kernel.h>
375efea30fSAndrew Thompson #include <sys/socket.h>
385efea30fSAndrew Thompson #include <sys/systm.h>
395efea30fSAndrew Thompson #include <sys/malloc.h>
405efea30fSAndrew Thompson #include <sys/module.h>
415efea30fSAndrew Thompson #include <sys/bus.h>
425efea30fSAndrew Thompson #include <sys/endian.h>
435efea30fSAndrew Thompson #include <sys/kdb.h>
445efea30fSAndrew Thompson 
455efea30fSAndrew Thompson #include <machine/bus.h>
4667784314SPoul-Henning Kamp #include <machine/resource.h>
4767784314SPoul-Henning Kamp #include <sys/rman.h>
485efea30fSAndrew Thompson 
495efea30fSAndrew Thompson #include <net/bpf.h>
505efea30fSAndrew Thompson #include <net/if.h>
5176039bc8SGleb Smirnoff #include <net/if_var.h>
5267784314SPoul-Henning Kamp #include <net/if_arp.h>
535efea30fSAndrew Thompson #include <net/ethernet.h>
545efea30fSAndrew Thompson #include <net/if_dl.h>
555efea30fSAndrew Thompson #include <net/if_media.h>
565efea30fSAndrew Thompson #include <net/if_types.h>
575efea30fSAndrew Thompson 
585efea30fSAndrew Thompson #ifdef INET
595efea30fSAndrew Thompson #include <netinet/in.h>
605efea30fSAndrew Thompson #include <netinet/in_systm.h>
615efea30fSAndrew Thompson #include <netinet/in_var.h>
625efea30fSAndrew Thompson #include <netinet/if_ether.h>
635efea30fSAndrew Thompson #include <netinet/ip.h>
645efea30fSAndrew Thompson #endif
655efea30fSAndrew Thompson 
665efea30fSAndrew Thompson #include <net80211/ieee80211_var.h>
675efea30fSAndrew Thompson #include <net80211/ieee80211_regdomain.h>
685efea30fSAndrew Thompson #include <net80211/ieee80211_radiotap.h>
69b6108616SRui Paulo #include <net80211/ieee80211_ratectl.h>
7002ac6454SAndrew Thompson 
715efea30fSAndrew Thompson #include <dev/usb/usb.h>
72ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
735efea30fSAndrew Thompson #include "usbdevs.h"
7402ac6454SAndrew Thompson 
75ed6d949aSAndrew Thompson #define	USB_DEBUG_VAR rum_debug
76ed6d949aSAndrew Thompson #include <dev/usb/usb_debug.h>
77ed6d949aSAndrew Thompson 
7802ac6454SAndrew Thompson #include <dev/usb/wlan/if_rumreg.h>
7902ac6454SAndrew Thompson #include <dev/usb/wlan/if_rumvar.h>
8002ac6454SAndrew Thompson #include <dev/usb/wlan/if_rumfw.h>
8102ac6454SAndrew Thompson 
82b850ecc1SAndrew Thompson #ifdef USB_DEBUG
8302ac6454SAndrew Thompson static int rum_debug = 0;
8402ac6454SAndrew Thompson 
856472ac3dSEd Schouten static SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
86ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RWTUN, &rum_debug, 0,
8702ac6454SAndrew Thompson     "Debug level");
8802ac6454SAndrew Thompson #endif
8902ac6454SAndrew Thompson 
90f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID rum_devs[] = {
919e6b5313SAndrew Thompson #define	RUM_DEV(v,p)  { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
929e6b5313SAndrew Thompson     RUM_DEV(ABOCOM, HWU54DM),
939e6b5313SAndrew Thompson     RUM_DEV(ABOCOM, RT2573_2),
949e6b5313SAndrew Thompson     RUM_DEV(ABOCOM, RT2573_3),
959e6b5313SAndrew Thompson     RUM_DEV(ABOCOM, RT2573_4),
969e6b5313SAndrew Thompson     RUM_DEV(ABOCOM, WUG2700),
979e6b5313SAndrew Thompson     RUM_DEV(AMIT, CGWLUSB2GO),
989e6b5313SAndrew Thompson     RUM_DEV(ASUS, RT2573_1),
999e6b5313SAndrew Thompson     RUM_DEV(ASUS, RT2573_2),
1009e6b5313SAndrew Thompson     RUM_DEV(BELKIN, F5D7050A),
1019e6b5313SAndrew Thompson     RUM_DEV(BELKIN, F5D9050V3),
1029e6b5313SAndrew Thompson     RUM_DEV(CISCOLINKSYS, WUSB54GC),
1039e6b5313SAndrew Thompson     RUM_DEV(CISCOLINKSYS, WUSB54GR),
1049e6b5313SAndrew Thompson     RUM_DEV(CONCEPTRONIC2, C54RU2),
1059e6b5313SAndrew Thompson     RUM_DEV(COREGA, CGWLUSB2GL),
1069e6b5313SAndrew Thompson     RUM_DEV(COREGA, CGWLUSB2GPX),
1079e6b5313SAndrew Thompson     RUM_DEV(DICKSMITH, CWD854F),
1089e6b5313SAndrew Thompson     RUM_DEV(DICKSMITH, RT2573),
109031c7388SAndrew Thompson     RUM_DEV(EDIMAX, EW7318USG),
1109e6b5313SAndrew Thompson     RUM_DEV(DLINK2, DWLG122C1),
1119e6b5313SAndrew Thompson     RUM_DEV(DLINK2, WUA1340),
1129e6b5313SAndrew Thompson     RUM_DEV(DLINK2, DWA111),
1139e6b5313SAndrew Thompson     RUM_DEV(DLINK2, DWA110),
1149e6b5313SAndrew Thompson     RUM_DEV(GIGABYTE, GNWB01GS),
1159e6b5313SAndrew Thompson     RUM_DEV(GIGABYTE, GNWI05GS),
1169e6b5313SAndrew Thompson     RUM_DEV(GIGASET, RT2573),
1179e6b5313SAndrew Thompson     RUM_DEV(GOODWAY, RT2573),
1189e6b5313SAndrew Thompson     RUM_DEV(GUILLEMOT, HWGUSB254LB),
1199e6b5313SAndrew Thompson     RUM_DEV(GUILLEMOT, HWGUSB254V2AP),
1209e6b5313SAndrew Thompson     RUM_DEV(HUAWEI3COM, WUB320G),
1219e6b5313SAndrew Thompson     RUM_DEV(MELCO, G54HP),
1229e6b5313SAndrew Thompson     RUM_DEV(MELCO, SG54HP),
1233232aae3SHans Petter Selasky     RUM_DEV(MELCO, SG54HG),
12454de08a9SMIHIRA Sanpei Yoshiro     RUM_DEV(MELCO, WLIUCG),
125e11ad60dSHans Petter Selasky     RUM_DEV(MELCO, WLRUCG),
126e11ad60dSHans Petter Selasky     RUM_DEV(MELCO, WLRUCGAOSS),
1279e6b5313SAndrew Thompson     RUM_DEV(MSI, RT2573_1),
1289e6b5313SAndrew Thompson     RUM_DEV(MSI, RT2573_2),
1299e6b5313SAndrew Thompson     RUM_DEV(MSI, RT2573_3),
1309e6b5313SAndrew Thompson     RUM_DEV(MSI, RT2573_4),
1319e6b5313SAndrew Thompson     RUM_DEV(NOVATECH, RT2573),
1329e6b5313SAndrew Thompson     RUM_DEV(PLANEX2, GWUS54HP),
1339e6b5313SAndrew Thompson     RUM_DEV(PLANEX2, GWUS54MINI2),
1349e6b5313SAndrew Thompson     RUM_DEV(PLANEX2, GWUSMM),
1359e6b5313SAndrew Thompson     RUM_DEV(QCOM, RT2573),
1369e6b5313SAndrew Thompson     RUM_DEV(QCOM, RT2573_2),
1379e6b5313SAndrew Thompson     RUM_DEV(QCOM, RT2573_3),
1389e6b5313SAndrew Thompson     RUM_DEV(RALINK, RT2573),
1399e6b5313SAndrew Thompson     RUM_DEV(RALINK, RT2573_2),
1409e6b5313SAndrew Thompson     RUM_DEV(RALINK, RT2671),
1419e6b5313SAndrew Thompson     RUM_DEV(SITECOMEU, WL113R2),
1429e6b5313SAndrew Thompson     RUM_DEV(SITECOMEU, WL172),
1439e6b5313SAndrew Thompson     RUM_DEV(SPARKLAN, RT2573),
1449e6b5313SAndrew Thompson     RUM_DEV(SURECOM, RT2573),
1459e6b5313SAndrew Thompson #undef RUM_DEV
14602ac6454SAndrew Thompson };
14702ac6454SAndrew Thompson 
14802ac6454SAndrew Thompson static device_probe_t rum_match;
14902ac6454SAndrew Thompson static device_attach_t rum_attach;
15002ac6454SAndrew Thompson static device_detach_t rum_detach;
15102ac6454SAndrew Thompson 
152e0a69b51SAndrew Thompson static usb_callback_t rum_bulk_read_callback;
153e0a69b51SAndrew Thompson static usb_callback_t rum_bulk_write_callback;
15402ac6454SAndrew Thompson 
155e0a69b51SAndrew Thompson static usb_error_t	rum_do_request(struct rum_softc *sc,
156760bc48eSAndrew Thompson 			    struct usb_device_request *req, void *data);
157fd7dae9cSAndriy Voskoboinyk static usb_error_t	rum_do_mcu_request(struct rum_softc *sc, int);
15802ac6454SAndrew Thompson static struct ieee80211vap *rum_vap_create(struct ieee80211com *,
159fcd9500fSBernhard Schmidt 			    const char [IFNAMSIZ], int, enum ieee80211_opmode,
160fcd9500fSBernhard Schmidt 			    int, const uint8_t [IEEE80211_ADDR_LEN],
161fcd9500fSBernhard Schmidt 			    const uint8_t [IEEE80211_ADDR_LEN]);
16202ac6454SAndrew Thompson static void		rum_vap_delete(struct ieee80211vap *);
163f28c2f5eSAdrian Chadd static void		rum_cmdq_cb(void *, int);
164f28c2f5eSAdrian Chadd static int		rum_cmd_sleepable(struct rum_softc *, const void *,
165ed5711a1SAdrian Chadd 			    size_t, uint8_t, CMD_FUNC_PROTO);
16602ac6454SAndrew Thompson static void		rum_tx_free(struct rum_tx_data *, int);
16702ac6454SAndrew Thompson static void		rum_setup_tx_list(struct rum_softc *);
168*d204cea9SAndriy Voskoboinyk static void		rum_reset_tx_list(struct rum_softc *,
169*d204cea9SAndriy Voskoboinyk 			    struct ieee80211vap *);
17002ac6454SAndrew Thompson static void		rum_unsetup_tx_list(struct rum_softc *);
171349156a9SAndriy Voskoboinyk static void		rum_beacon_miss(struct ieee80211vap *);
172349156a9SAndriy Voskoboinyk static void		rum_sta_recv_mgmt(struct ieee80211_node *,
173349156a9SAndriy Voskoboinyk 			    struct mbuf *, int,
174349156a9SAndriy Voskoboinyk 			    const struct ieee80211_rx_stats *, int, int);
175349156a9SAndriy Voskoboinyk static int		rum_set_power_state(struct rum_softc *, int);
17602ac6454SAndrew Thompson static int		rum_newstate(struct ieee80211vap *,
17702ac6454SAndrew Thompson 			    enum ieee80211_state, int);
1782cb9ef8dSAdrian Chadd static uint8_t		rum_crypto_mode(struct rum_softc *, u_int, int);
17902ac6454SAndrew Thompson static void		rum_setup_tx_desc(struct rum_softc *,
1802cb9ef8dSAdrian Chadd 			    struct rum_tx_desc *, struct ieee80211_key *,
1811354b52cSAdrian Chadd 			    uint32_t, uint8_t, uint8_t, int, int, int);
1822cb9ef8dSAdrian Chadd static uint32_t		rum_tx_crypto_flags(struct rum_softc *,
1832cb9ef8dSAdrian Chadd 			    struct ieee80211_node *,
1842cb9ef8dSAdrian Chadd 			    const struct ieee80211_key *);
18502ac6454SAndrew Thompson static int		rum_tx_mgt(struct rum_softc *, struct mbuf *,
18602ac6454SAndrew Thompson 			    struct ieee80211_node *);
18702ac6454SAndrew Thompson static int		rum_tx_raw(struct rum_softc *, struct mbuf *,
18802ac6454SAndrew Thompson 			    struct ieee80211_node *,
18902ac6454SAndrew Thompson 			    const struct ieee80211_bpf_params *);
19002ac6454SAndrew Thompson static int		rum_tx_data(struct rum_softc *, struct mbuf *,
19102ac6454SAndrew Thompson 			    struct ieee80211_node *);
1927a79cebfSGleb Smirnoff static int		rum_transmit(struct ieee80211com *, struct mbuf *);
1937a79cebfSGleb Smirnoff static void		rum_start(struct rum_softc *);
1947a79cebfSGleb Smirnoff static void		rum_parent(struct ieee80211com *);
19502ac6454SAndrew Thompson static void		rum_eeprom_read(struct rum_softc *, uint16_t, void *,
19602ac6454SAndrew Thompson 			    int);
19702ac6454SAndrew Thompson static uint32_t		rum_read(struct rum_softc *, uint16_t);
19802ac6454SAndrew Thompson static void		rum_read_multi(struct rum_softc *, uint16_t, void *,
19902ac6454SAndrew Thompson 			    int);
200e0a69b51SAndrew Thompson static usb_error_t	rum_write(struct rum_softc *, uint16_t, uint32_t);
201e0a69b51SAndrew Thompson static usb_error_t	rum_write_multi(struct rum_softc *, uint16_t, void *,
20202ac6454SAndrew Thompson 			    size_t);
203a05022b2SAdrian Chadd static usb_error_t	rum_setbits(struct rum_softc *, uint16_t, uint32_t);
204a05022b2SAdrian Chadd static usb_error_t	rum_clrbits(struct rum_softc *, uint16_t, uint32_t);
205a05022b2SAdrian Chadd static usb_error_t	rum_modbits(struct rum_softc *, uint16_t, uint32_t,
206a05022b2SAdrian Chadd 			    uint32_t);
207cc9ae761SAdrian Chadd static int		rum_bbp_busy(struct rum_softc *);
20802ac6454SAndrew Thompson static void		rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
20902ac6454SAndrew Thompson static uint8_t		rum_bbp_read(struct rum_softc *, uint8_t);
21002ac6454SAndrew Thompson static void		rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
21102ac6454SAndrew Thompson static void		rum_select_antenna(struct rum_softc *);
21202ac6454SAndrew Thompson static void		rum_enable_mrr(struct rum_softc *);
21302ac6454SAndrew Thompson static void		rum_set_txpreamble(struct rum_softc *);
21402ac6454SAndrew Thompson static void		rum_set_basicrates(struct rum_softc *);
21502ac6454SAndrew Thompson static void		rum_select_band(struct rum_softc *,
21602ac6454SAndrew Thompson 			    struct ieee80211_channel *);
21702ac6454SAndrew Thompson static void		rum_set_chan(struct rum_softc *,
21802ac6454SAndrew Thompson 			    struct ieee80211_channel *);
219342ced03SAdrian Chadd static void		rum_set_maxretry(struct rum_softc *,
220342ced03SAdrian Chadd 			    struct ieee80211vap *);
221aa81f853SAdrian Chadd static int		rum_enable_tsf_sync(struct rum_softc *);
2225463c4a4SSam Leffler static void		rum_enable_tsf(struct rum_softc *);
2236d804321SAdrian Chadd static void		rum_abort_tsf_sync(struct rum_softc *);
224a6ccd477SAdrian Chadd static void		rum_get_tsf(struct rum_softc *, uint64_t *);
225a19dbd8dSAdrian Chadd static void		rum_update_slot_cb(struct rum_softc *,
226ed5711a1SAdrian Chadd 			    union sec_param *, uint8_t);
227a19dbd8dSAdrian Chadd static void		rum_update_slot(struct ieee80211com *);
2281354b52cSAdrian Chadd static int		rum_wme_update(struct ieee80211com *);
22902ac6454SAndrew Thompson static void		rum_set_bssid(struct rum_softc *, const uint8_t *);
23002ac6454SAndrew Thompson static void		rum_set_macaddr(struct rum_softc *, const uint8_t *);
231272f6adeSGleb Smirnoff static void		rum_update_mcast(struct ieee80211com *);
232272f6adeSGleb Smirnoff static void		rum_update_promisc(struct ieee80211com *);
2335efea30fSAndrew Thompson static void		rum_setpromisc(struct rum_softc *);
23402ac6454SAndrew Thompson static const char	*rum_get_rf(int);
23502ac6454SAndrew Thompson static void		rum_read_eeprom(struct rum_softc *);
2366095f7caSAdrian Chadd static int		rum_bbp_wakeup(struct rum_softc *);
23702ac6454SAndrew Thompson static int		rum_bbp_init(struct rum_softc *);
2382cb9ef8dSAdrian Chadd static void		rum_clr_shkey_regs(struct rum_softc *);
2390698c0b3SAdrian Chadd static int		rum_init(struct rum_softc *);
2405efea30fSAndrew Thompson static void		rum_stop(struct rum_softc *);
24177ddf3d3SAndrew Thompson static void		rum_load_microcode(struct rum_softc *, const uint8_t *,
24202ac6454SAndrew Thompson 			    size_t);
243349156a9SAndriy Voskoboinyk static int		rum_set_sleep_time(struct rum_softc *, uint16_t);
244349156a9SAndriy Voskoboinyk static int		rum_reset(struct ieee80211vap *, u_long);
245b780f864SAdrian Chadd static int		rum_set_beacon(struct rum_softc *,
246b780f864SAdrian Chadd 			    struct ieee80211vap *);
247b780f864SAdrian Chadd static int		rum_alloc_beacon(struct rum_softc *,
24802ac6454SAndrew Thompson 			    struct ieee80211vap *);
2490e3b4c60SAdrian Chadd static void		rum_update_beacon_cb(struct rum_softc *,
250ed5711a1SAdrian Chadd 			    union sec_param *, uint8_t);
2510e3b4c60SAdrian Chadd static void		rum_update_beacon(struct ieee80211vap *, int);
2522cb9ef8dSAdrian Chadd static int		rum_common_key_set(struct rum_softc *,
2532cb9ef8dSAdrian Chadd 			    struct ieee80211_key *, uint16_t);
2542cb9ef8dSAdrian Chadd static void		rum_group_key_set_cb(struct rum_softc *,
255ed5711a1SAdrian Chadd 			    union sec_param *, uint8_t);
2562cb9ef8dSAdrian Chadd static void		rum_group_key_del_cb(struct rum_softc *,
257ed5711a1SAdrian Chadd 			    union sec_param *, uint8_t);
2582cb9ef8dSAdrian Chadd static void		rum_pair_key_set_cb(struct rum_softc *,
259ed5711a1SAdrian Chadd 			    union sec_param *, uint8_t);
2602cb9ef8dSAdrian Chadd static void		rum_pair_key_del_cb(struct rum_softc *,
261ed5711a1SAdrian Chadd 			    union sec_param *, uint8_t);
2622cb9ef8dSAdrian Chadd static int		rum_key_alloc(struct ieee80211vap *,
2632cb9ef8dSAdrian Chadd 			    struct ieee80211_key *, ieee80211_keyix *,
2642cb9ef8dSAdrian Chadd 			    ieee80211_keyix *);
2652cb9ef8dSAdrian Chadd static int		rum_key_set(struct ieee80211vap *,
266bc813c40SAdrian Chadd 			    const struct ieee80211_key *);
2672cb9ef8dSAdrian Chadd static int		rum_key_delete(struct ieee80211vap *,
2682cb9ef8dSAdrian Chadd 			    const struct ieee80211_key *);
26902ac6454SAndrew Thompson static int		rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
27002ac6454SAndrew Thompson 			    const struct ieee80211_bpf_params *);
27102ac6454SAndrew Thompson static void		rum_scan_start(struct ieee80211com *);
27202ac6454SAndrew Thompson static void		rum_scan_end(struct ieee80211com *);
27302ac6454SAndrew Thompson static void		rum_set_channel(struct ieee80211com *);
2747b45c9dfSAndriy Voskoboinyk static void		rum_getradiocaps(struct ieee80211com *, int, int *,
2757b45c9dfSAndriy Voskoboinyk 			    struct ieee80211_channel[]);
27602ac6454SAndrew Thompson static int		rum_get_rssi(struct rum_softc *, uint8_t);
277b6108616SRui Paulo static void		rum_ratectl_start(struct rum_softc *,
27802ac6454SAndrew Thompson 			    struct ieee80211_node *);
279b6108616SRui Paulo static void		rum_ratectl_timeout(void *);
280b6108616SRui Paulo static void		rum_ratectl_task(void *, int);
28102ac6454SAndrew Thompson static int		rum_pause(struct rum_softc *, int);
28202ac6454SAndrew Thompson 
28302ac6454SAndrew Thompson static const struct {
28402ac6454SAndrew Thompson 	uint32_t	reg;
28502ac6454SAndrew Thompson 	uint32_t	val;
28602ac6454SAndrew Thompson } rum_def_mac[] = {
28702ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR0,  0x025fb032 },
28802ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR1,  0x9eaa9eaf },
28902ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR2,  0x8a8b8c8d },
29002ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR3,  0x00858687 },
29102ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR7,  0x2e31353b },
29202ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR8,  0x2a2a2a2c },
29302ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR15, 0x0000000f },
29402ac6454SAndrew Thompson 	{ RT2573_MAC_CSR6,   0x00000fff },
29502ac6454SAndrew Thompson 	{ RT2573_MAC_CSR8,   0x016c030a },
29602ac6454SAndrew Thompson 	{ RT2573_MAC_CSR10,  0x00000718 },
29702ac6454SAndrew Thompson 	{ RT2573_MAC_CSR12,  0x00000004 },
29802ac6454SAndrew Thompson 	{ RT2573_MAC_CSR13,  0x00007f00 },
2992cb9ef8dSAdrian Chadd 	{ RT2573_SEC_CSR2,   0x00000000 },
3002cb9ef8dSAdrian Chadd 	{ RT2573_SEC_CSR3,   0x00000000 },
3012cb9ef8dSAdrian Chadd 	{ RT2573_SEC_CSR4,   0x00000000 },
30202ac6454SAndrew Thompson 	{ RT2573_PHY_CSR1,   0x000023b0 },
30302ac6454SAndrew Thompson 	{ RT2573_PHY_CSR5,   0x00040a06 },
30402ac6454SAndrew Thompson 	{ RT2573_PHY_CSR6,   0x00080606 },
30502ac6454SAndrew Thompson 	{ RT2573_PHY_CSR7,   0x00000408 },
30602ac6454SAndrew Thompson 	{ RT2573_AIFSN_CSR,  0x00002273 },
30702ac6454SAndrew Thompson 	{ RT2573_CWMIN_CSR,  0x00002344 },
30802ac6454SAndrew Thompson 	{ RT2573_CWMAX_CSR,  0x000034aa }
30902ac6454SAndrew Thompson };
31002ac6454SAndrew Thompson 
31102ac6454SAndrew Thompson static const struct {
31202ac6454SAndrew Thompson 	uint8_t	reg;
31302ac6454SAndrew Thompson 	uint8_t	val;
31402ac6454SAndrew Thompson } rum_def_bbp[] = {
31502ac6454SAndrew Thompson 	{   3, 0x80 },
31602ac6454SAndrew Thompson 	{  15, 0x30 },
31702ac6454SAndrew Thompson 	{  17, 0x20 },
31802ac6454SAndrew Thompson 	{  21, 0xc8 },
31902ac6454SAndrew Thompson 	{  22, 0x38 },
32002ac6454SAndrew Thompson 	{  23, 0x06 },
32102ac6454SAndrew Thompson 	{  24, 0xfe },
32202ac6454SAndrew Thompson 	{  25, 0x0a },
32302ac6454SAndrew Thompson 	{  26, 0x0d },
32402ac6454SAndrew Thompson 	{  32, 0x0b },
32502ac6454SAndrew Thompson 	{  34, 0x12 },
32602ac6454SAndrew Thompson 	{  37, 0x07 },
32702ac6454SAndrew Thompson 	{  39, 0xf8 },
32802ac6454SAndrew Thompson 	{  41, 0x60 },
32902ac6454SAndrew Thompson 	{  53, 0x10 },
33002ac6454SAndrew Thompson 	{  54, 0x18 },
33102ac6454SAndrew Thompson 	{  60, 0x10 },
33202ac6454SAndrew Thompson 	{  61, 0x04 },
33302ac6454SAndrew Thompson 	{  62, 0x04 },
33402ac6454SAndrew Thompson 	{  75, 0xfe },
33502ac6454SAndrew Thompson 	{  86, 0xfe },
33602ac6454SAndrew Thompson 	{  88, 0xfe },
33702ac6454SAndrew Thompson 	{  90, 0x0f },
33802ac6454SAndrew Thompson 	{  99, 0x00 },
33902ac6454SAndrew Thompson 	{ 102, 0x16 },
34002ac6454SAndrew Thompson 	{ 107, 0x04 }
34102ac6454SAndrew Thompson };
34202ac6454SAndrew Thompson 
3437b45c9dfSAndriy Voskoboinyk static const uint8_t rum_chan_2ghz[] =
3447b45c9dfSAndriy Voskoboinyk 	{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
3457b45c9dfSAndriy Voskoboinyk 
3467b45c9dfSAndriy Voskoboinyk static const uint8_t rum_chan_5ghz[] =
3477b45c9dfSAndriy Voskoboinyk 	{ 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64,
3487b45c9dfSAndriy Voskoboinyk 	  100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140,
3497b45c9dfSAndriy Voskoboinyk 	  149, 153, 157, 161, 165 };
3507b45c9dfSAndriy Voskoboinyk 
35102ac6454SAndrew Thompson static const struct rfprog {
35202ac6454SAndrew Thompson 	uint8_t		chan;
35302ac6454SAndrew Thompson 	uint32_t	r1, r2, r3, r4;
35402ac6454SAndrew Thompson }  rum_rf5226[] = {
35502ac6454SAndrew Thompson 	{   1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
35602ac6454SAndrew Thompson 	{   2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
35702ac6454SAndrew Thompson 	{   3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
35802ac6454SAndrew Thompson 	{   4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
35902ac6454SAndrew Thompson 	{   5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
36002ac6454SAndrew Thompson 	{   6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
36102ac6454SAndrew Thompson 	{   7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
36202ac6454SAndrew Thompson 	{   8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
36302ac6454SAndrew Thompson 	{   9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
36402ac6454SAndrew Thompson 	{  10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
36502ac6454SAndrew Thompson 	{  11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
36602ac6454SAndrew Thompson 	{  12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
36702ac6454SAndrew Thompson 	{  13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
36802ac6454SAndrew Thompson 	{  14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
36902ac6454SAndrew Thompson 
37002ac6454SAndrew Thompson 	{  34, 0x00b03, 0x20266, 0x36014, 0x30282 },
37102ac6454SAndrew Thompson 	{  38, 0x00b03, 0x20267, 0x36014, 0x30284 },
37202ac6454SAndrew Thompson 	{  42, 0x00b03, 0x20268, 0x36014, 0x30286 },
37302ac6454SAndrew Thompson 	{  46, 0x00b03, 0x20269, 0x36014, 0x30288 },
37402ac6454SAndrew Thompson 
37502ac6454SAndrew Thompson 	{  36, 0x00b03, 0x00266, 0x26014, 0x30288 },
37602ac6454SAndrew Thompson 	{  40, 0x00b03, 0x00268, 0x26014, 0x30280 },
37702ac6454SAndrew Thompson 	{  44, 0x00b03, 0x00269, 0x26014, 0x30282 },
37802ac6454SAndrew Thompson 	{  48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
37902ac6454SAndrew Thompson 	{  52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
38002ac6454SAndrew Thompson 	{  56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
38102ac6454SAndrew Thompson 	{  60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
38202ac6454SAndrew Thompson 	{  64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
38302ac6454SAndrew Thompson 
38402ac6454SAndrew Thompson 	{ 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
38502ac6454SAndrew Thompson 	{ 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
38602ac6454SAndrew Thompson 	{ 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
38702ac6454SAndrew Thompson 	{ 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
38802ac6454SAndrew Thompson 	{ 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
38902ac6454SAndrew Thompson 	{ 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
39002ac6454SAndrew Thompson 	{ 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
39102ac6454SAndrew Thompson 	{ 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
39202ac6454SAndrew Thompson 	{ 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
39302ac6454SAndrew Thompson 	{ 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
39402ac6454SAndrew Thompson 	{ 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
39502ac6454SAndrew Thompson 
39602ac6454SAndrew Thompson 	{ 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
39702ac6454SAndrew Thompson 	{ 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
39802ac6454SAndrew Thompson 	{ 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
39902ac6454SAndrew Thompson 	{ 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
40002ac6454SAndrew Thompson 	{ 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
40102ac6454SAndrew Thompson }, rum_rf5225[] = {
40202ac6454SAndrew Thompson 	{   1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
40302ac6454SAndrew Thompson 	{   2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
40402ac6454SAndrew Thompson 	{   3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
40502ac6454SAndrew Thompson 	{   4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
40602ac6454SAndrew Thompson 	{   5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
40702ac6454SAndrew Thompson 	{   6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
40802ac6454SAndrew Thompson 	{   7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
40902ac6454SAndrew Thompson 	{   8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
41002ac6454SAndrew Thompson 	{   9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
41102ac6454SAndrew Thompson 	{  10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
41202ac6454SAndrew Thompson 	{  11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
41302ac6454SAndrew Thompson 	{  12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
41402ac6454SAndrew Thompson 	{  13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
41502ac6454SAndrew Thompson 	{  14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
41602ac6454SAndrew Thompson 
41702ac6454SAndrew Thompson 	{  34, 0x00b33, 0x01266, 0x26014, 0x30282 },
41802ac6454SAndrew Thompson 	{  38, 0x00b33, 0x01267, 0x26014, 0x30284 },
41902ac6454SAndrew Thompson 	{  42, 0x00b33, 0x01268, 0x26014, 0x30286 },
42002ac6454SAndrew Thompson 	{  46, 0x00b33, 0x01269, 0x26014, 0x30288 },
42102ac6454SAndrew Thompson 
42202ac6454SAndrew Thompson 	{  36, 0x00b33, 0x01266, 0x26014, 0x30288 },
42302ac6454SAndrew Thompson 	{  40, 0x00b33, 0x01268, 0x26014, 0x30280 },
42402ac6454SAndrew Thompson 	{  44, 0x00b33, 0x01269, 0x26014, 0x30282 },
42502ac6454SAndrew Thompson 	{  48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
42602ac6454SAndrew Thompson 	{  52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
42702ac6454SAndrew Thompson 	{  56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
42802ac6454SAndrew Thompson 	{  60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
42902ac6454SAndrew Thompson 	{  64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
43002ac6454SAndrew Thompson 
43102ac6454SAndrew Thompson 	{ 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
43202ac6454SAndrew Thompson 	{ 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
43302ac6454SAndrew Thompson 	{ 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
43402ac6454SAndrew Thompson 	{ 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
43502ac6454SAndrew Thompson 	{ 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
43602ac6454SAndrew Thompson 	{ 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
43702ac6454SAndrew Thompson 	{ 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
43802ac6454SAndrew Thompson 	{ 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
43902ac6454SAndrew Thompson 	{ 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
44002ac6454SAndrew Thompson 	{ 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
44102ac6454SAndrew Thompson 	{ 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
44202ac6454SAndrew Thompson 
44302ac6454SAndrew Thompson 	{ 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
44402ac6454SAndrew Thompson 	{ 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
44502ac6454SAndrew Thompson 	{ 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
44602ac6454SAndrew Thompson 	{ 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
44702ac6454SAndrew Thompson 	{ 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
44802ac6454SAndrew Thompson };
44902ac6454SAndrew Thompson 
450760bc48eSAndrew Thompson static const struct usb_config rum_config[RUM_N_TRANSFER] = {
45102ac6454SAndrew Thompson 	[RUM_BULK_WR] = {
45202ac6454SAndrew Thompson 		.type = UE_BULK,
45302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
45402ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
4554eae601eSAndrew Thompson 		.bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
4564eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
4574eae601eSAndrew Thompson 		.callback = rum_bulk_write_callback,
4584eae601eSAndrew Thompson 		.timeout = 5000,	/* ms */
45902ac6454SAndrew Thompson 	},
46002ac6454SAndrew Thompson 	[RUM_BULK_RD] = {
46102ac6454SAndrew Thompson 		.type = UE_BULK,
46202ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
46302ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
4644eae601eSAndrew Thompson 		.bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
4654eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
4664eae601eSAndrew Thompson 		.callback = rum_bulk_read_callback,
46702ac6454SAndrew Thompson 	},
46802ac6454SAndrew Thompson };
46902ac6454SAndrew Thompson 
47002ac6454SAndrew Thompson static int
47102ac6454SAndrew Thompson rum_match(device_t self)
47202ac6454SAndrew Thompson {
473760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(self);
47402ac6454SAndrew Thompson 
475f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST)
47602ac6454SAndrew Thompson 		return (ENXIO);
47702ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != 0)
47802ac6454SAndrew Thompson 		return (ENXIO);
47902ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
48002ac6454SAndrew Thompson 		return (ENXIO);
48102ac6454SAndrew Thompson 
482a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
48302ac6454SAndrew Thompson }
48402ac6454SAndrew Thompson 
48502ac6454SAndrew Thompson static int
48602ac6454SAndrew Thompson rum_attach(device_t self)
48702ac6454SAndrew Thompson {
488760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(self);
48902ac6454SAndrew Thompson 	struct rum_softc *sc = device_get_softc(self);
4907a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
4915efea30fSAndrew Thompson 	uint32_t tmp;
4920046e186SAndriy Voskoboinyk 	uint8_t iface_index;
4935efea30fSAndrew Thompson 	int error, ntries;
49402ac6454SAndrew Thompson 
495a593f6b8SAndrew Thompson 	device_set_usb_desc(self);
49602ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
49702ac6454SAndrew Thompson 	sc->sc_dev = self;
49802ac6454SAndrew Thompson 
499f28c2f5eSAdrian Chadd 	RUM_LOCK_INIT(sc);
500f28c2f5eSAdrian Chadd 	RUM_CMDQ_LOCK_INIT(sc);
5017a79cebfSGleb Smirnoff 	mbufq_init(&sc->sc_snd, ifqmaxlen);
50202ac6454SAndrew Thompson 
50302ac6454SAndrew Thompson 	iface_index = RT2573_IFACE_INDEX;
504a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device, &iface_index,
50502ac6454SAndrew Thompson 	    sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
50602ac6454SAndrew Thompson 	if (error) {
50702ac6454SAndrew Thompson 		device_printf(self, "could not allocate USB transfers, "
508a593f6b8SAndrew Thompson 		    "err=%s\n", usbd_errstr(error));
50902ac6454SAndrew Thompson 		goto detach;
51002ac6454SAndrew Thompson 	}
51102ac6454SAndrew Thompson 
51202ac6454SAndrew Thompson 	RUM_LOCK(sc);
51302ac6454SAndrew Thompson 	/* retrieve RT2573 rev. no */
51402ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
51502ac6454SAndrew Thompson 		if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
51602ac6454SAndrew Thompson 			break;
51702ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
51802ac6454SAndrew Thompson 			break;
51902ac6454SAndrew Thompson 	}
52002ac6454SAndrew Thompson 	if (ntries == 100) {
52102ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
5225efea30fSAndrew Thompson 		RUM_UNLOCK(sc);
5235efea30fSAndrew Thompson 		goto detach;
52402ac6454SAndrew Thompson 	}
52502ac6454SAndrew Thompson 
52602ac6454SAndrew Thompson 	/* retrieve MAC address and various other things from EEPROM */
52702ac6454SAndrew Thompson 	rum_read_eeprom(sc);
52802ac6454SAndrew Thompson 
52902ac6454SAndrew Thompson 	device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
53002ac6454SAndrew Thompson 	    tmp, rum_get_rf(sc->rf_rev));
53102ac6454SAndrew Thompson 
53277ddf3d3SAndrew Thompson 	rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
53302ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
53402ac6454SAndrew Thompson 
53559686fe9SGleb Smirnoff 	ic->ic_softc = sc;
536c8550c02SGleb Smirnoff 	ic->ic_name = device_get_nameunit(self);
53702ac6454SAndrew Thompson 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
53802ac6454SAndrew Thompson 
53902ac6454SAndrew Thompson 	/* set device capabilities */
54002ac6454SAndrew Thompson 	ic->ic_caps =
54102ac6454SAndrew Thompson 	      IEEE80211_C_STA		/* station mode supported */
54202ac6454SAndrew Thompson 	    | IEEE80211_C_IBSS		/* IBSS mode supported */
54302ac6454SAndrew Thompson 	    | IEEE80211_C_MONITOR	/* monitor mode supported */
54402ac6454SAndrew Thompson 	    | IEEE80211_C_HOSTAP	/* HostAp mode supported */
54550a31b48SAdrian Chadd 	    | IEEE80211_C_AHDEMO	/* adhoc demo mode */
54602ac6454SAndrew Thompson 	    | IEEE80211_C_TXPMGT	/* tx power management */
54702ac6454SAndrew Thompson 	    | IEEE80211_C_SHPREAMBLE	/* short preamble supported */
54802ac6454SAndrew Thompson 	    | IEEE80211_C_SHSLOT	/* short slot time supported */
54902ac6454SAndrew Thompson 	    | IEEE80211_C_BGSCAN	/* bg scanning supported */
55002ac6454SAndrew Thompson 	    | IEEE80211_C_WPA		/* 802.11i */
5511354b52cSAdrian Chadd 	    | IEEE80211_C_WME		/* 802.11e */
552349156a9SAndriy Voskoboinyk 	    | IEEE80211_C_PMGT		/* Station-side power mgmt */
553349156a9SAndriy Voskoboinyk 	    | IEEE80211_C_SWSLEEP	/* net80211 managed power mgmt */
55402ac6454SAndrew Thompson 	    ;
55502ac6454SAndrew Thompson 
5562cb9ef8dSAdrian Chadd 	ic->ic_cryptocaps =
5572cb9ef8dSAdrian Chadd 	    IEEE80211_CRYPTO_WEP |
5582cb9ef8dSAdrian Chadd 	    IEEE80211_CRYPTO_AES_CCM |
5592cb9ef8dSAdrian Chadd 	    IEEE80211_CRYPTO_TKIPMIC |
5602cb9ef8dSAdrian Chadd 	    IEEE80211_CRYPTO_TKIP;
5612cb9ef8dSAdrian Chadd 
5627b45c9dfSAndriy Voskoboinyk 	rum_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
5637b45c9dfSAndriy Voskoboinyk 	    ic->ic_channels);
56402ac6454SAndrew Thompson 
5657a79cebfSGleb Smirnoff 	ieee80211_ifattach(ic);
56677ddf3d3SAndrew Thompson 	ic->ic_update_promisc = rum_update_promisc;
56702ac6454SAndrew Thompson 	ic->ic_raw_xmit = rum_raw_xmit;
56802ac6454SAndrew Thompson 	ic->ic_scan_start = rum_scan_start;
56902ac6454SAndrew Thompson 	ic->ic_scan_end = rum_scan_end;
57002ac6454SAndrew Thompson 	ic->ic_set_channel = rum_set_channel;
5717b45c9dfSAndriy Voskoboinyk 	ic->ic_getradiocaps = rum_getradiocaps;
5727a79cebfSGleb Smirnoff 	ic->ic_transmit = rum_transmit;
5737a79cebfSGleb Smirnoff 	ic->ic_parent = rum_parent;
57402ac6454SAndrew Thompson 	ic->ic_vap_create = rum_vap_create;
57502ac6454SAndrew Thompson 	ic->ic_vap_delete = rum_vap_delete;
576a19dbd8dSAdrian Chadd 	ic->ic_updateslot = rum_update_slot;
5771354b52cSAdrian Chadd 	ic->ic_wme.wme_update = rum_wme_update;
5786dd8e071SEd Maste 	ic->ic_update_mcast = rum_update_mcast;
57902ac6454SAndrew Thompson 
5805463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
5815463c4a4SSam Leffler 	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
5825463c4a4SSam Leffler 		RT2573_TX_RADIOTAP_PRESENT,
5835463c4a4SSam Leffler 	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
5845463c4a4SSam Leffler 		RT2573_RX_RADIOTAP_PRESENT);
58502ac6454SAndrew Thompson 
586f28c2f5eSAdrian Chadd 	TASK_INIT(&sc->cmdq_task, 0, rum_cmdq_cb, sc);
587f28c2f5eSAdrian Chadd 
58802ac6454SAndrew Thompson 	if (bootverbose)
58902ac6454SAndrew Thompson 		ieee80211_announce(ic);
59002ac6454SAndrew Thompson 
5915efea30fSAndrew Thompson 	return (0);
5925efea30fSAndrew Thompson 
5935efea30fSAndrew Thompson detach:
5945efea30fSAndrew Thompson 	rum_detach(self);
5955efea30fSAndrew Thompson 	return (ENXIO);			/* failure */
59602ac6454SAndrew Thompson }
59702ac6454SAndrew Thompson 
59802ac6454SAndrew Thompson static int
59902ac6454SAndrew Thompson rum_detach(device_t self)
60002ac6454SAndrew Thompson {
60102ac6454SAndrew Thompson 	struct rum_softc *sc = device_get_softc(self);
602e7b7db3dSAdrian Chadd 	struct ieee80211com *ic = &sc->sc_ic;
60302ac6454SAndrew Thompson 
604645e4d17SHans Petter Selasky 	/* Prevent further ioctls */
605645e4d17SHans Petter Selasky 	RUM_LOCK(sc);
606645e4d17SHans Petter Selasky 	sc->sc_detached = 1;
607645e4d17SHans Petter Selasky 	RUM_UNLOCK(sc);
608645e4d17SHans Petter Selasky 
60902ac6454SAndrew Thompson 	/* stop all USB transfers */
610a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
61102ac6454SAndrew Thompson 
61202ac6454SAndrew Thompson 	/* free TX list, if any */
61302ac6454SAndrew Thompson 	RUM_LOCK(sc);
61402ac6454SAndrew Thompson 	rum_unsetup_tx_list(sc);
61502ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
61602ac6454SAndrew Thompson 
617f28c2f5eSAdrian Chadd 	if (ic->ic_softc == sc) {
618f28c2f5eSAdrian Chadd 		ieee80211_draintask(ic, &sc->cmdq_task);
619e7b7db3dSAdrian Chadd 		ieee80211_ifdetach(ic);
620f28c2f5eSAdrian Chadd 	}
621f28c2f5eSAdrian Chadd 
6227a79cebfSGleb Smirnoff 	mbufq_drain(&sc->sc_snd);
623f28c2f5eSAdrian Chadd 	RUM_CMDQ_LOCK_DESTROY(sc);
624f28c2f5eSAdrian Chadd 	RUM_LOCK_DESTROY(sc);
625f28c2f5eSAdrian Chadd 
62602ac6454SAndrew Thompson 	return (0);
62702ac6454SAndrew Thompson }
62802ac6454SAndrew Thompson 
629e0a69b51SAndrew Thompson static usb_error_t
63077ddf3d3SAndrew Thompson rum_do_request(struct rum_softc *sc,
631760bc48eSAndrew Thompson     struct usb_device_request *req, void *data)
63277ddf3d3SAndrew Thompson {
633e0a69b51SAndrew Thompson 	usb_error_t err;
63477ddf3d3SAndrew Thompson 	int ntries = 10;
63577ddf3d3SAndrew Thompson 
63677ddf3d3SAndrew Thompson 	while (ntries--) {
637a593f6b8SAndrew Thompson 		err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
63877ddf3d3SAndrew Thompson 		    req, data, 0, NULL, 250 /* ms */);
63977ddf3d3SAndrew Thompson 		if (err == 0)
64077ddf3d3SAndrew Thompson 			break;
64177ddf3d3SAndrew Thompson 
64277ddf3d3SAndrew Thompson 		DPRINTFN(1, "Control request failed, %s (retrying)\n",
643a593f6b8SAndrew Thompson 		    usbd_errstr(err));
64477ddf3d3SAndrew Thompson 		if (rum_pause(sc, hz / 100))
64577ddf3d3SAndrew Thompson 			break;
64677ddf3d3SAndrew Thompson 	}
64777ddf3d3SAndrew Thompson 	return (err);
64877ddf3d3SAndrew Thompson }
64977ddf3d3SAndrew Thompson 
650fd7dae9cSAndriy Voskoboinyk static usb_error_t
651fd7dae9cSAndriy Voskoboinyk rum_do_mcu_request(struct rum_softc *sc, int request)
652fd7dae9cSAndriy Voskoboinyk {
653fd7dae9cSAndriy Voskoboinyk 	struct usb_device_request req;
654fd7dae9cSAndriy Voskoboinyk 
655fd7dae9cSAndriy Voskoboinyk 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
656fd7dae9cSAndriy Voskoboinyk 	req.bRequest = RT2573_MCU_CNTL;
657fd7dae9cSAndriy Voskoboinyk 	USETW(req.wValue, request);
658fd7dae9cSAndriy Voskoboinyk 	USETW(req.wIndex, 0);
659fd7dae9cSAndriy Voskoboinyk 	USETW(req.wLength, 0);
660fd7dae9cSAndriy Voskoboinyk 
661fd7dae9cSAndriy Voskoboinyk 	return (rum_do_request(sc, &req, NULL));
662fd7dae9cSAndriy Voskoboinyk }
663fd7dae9cSAndriy Voskoboinyk 
66402ac6454SAndrew Thompson static struct ieee80211vap *
665fcd9500fSBernhard Schmidt rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
666fcd9500fSBernhard Schmidt     enum ieee80211_opmode opmode, int flags,
66702ac6454SAndrew Thompson     const uint8_t bssid[IEEE80211_ADDR_LEN],
66802ac6454SAndrew Thompson     const uint8_t mac[IEEE80211_ADDR_LEN])
66902ac6454SAndrew Thompson {
670d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
67102ac6454SAndrew Thompson 	struct rum_vap *rvp;
67202ac6454SAndrew Thompson 	struct ieee80211vap *vap;
67302ac6454SAndrew Thompson 
67402ac6454SAndrew Thompson 	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
67502ac6454SAndrew Thompson 		return NULL;
6767a79cebfSGleb Smirnoff 	rvp = malloc(sizeof(struct rum_vap), M_80211_VAP, M_WAITOK | M_ZERO);
67702ac6454SAndrew Thompson 	vap = &rvp->vap;
67802ac6454SAndrew Thompson 	/* enable s/w bmiss handling for sta mode */
679bb2f69e8SHans Petter Selasky 
680bb2f69e8SHans Petter Selasky 	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
6817a79cebfSGleb Smirnoff 	    flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
682bb2f69e8SHans Petter Selasky 		/* out of memory */
683bb2f69e8SHans Petter Selasky 		free(rvp, M_80211_VAP);
684bb2f69e8SHans Petter Selasky 		return (NULL);
685bb2f69e8SHans Petter Selasky 	}
68602ac6454SAndrew Thompson 
68702ac6454SAndrew Thompson 	/* override state transition machine */
68802ac6454SAndrew Thompson 	rvp->newstate = vap->iv_newstate;
68902ac6454SAndrew Thompson 	vap->iv_newstate = rum_newstate;
6902cb9ef8dSAdrian Chadd 	vap->iv_key_alloc = rum_key_alloc;
6912cb9ef8dSAdrian Chadd 	vap->iv_key_set = rum_key_set;
6922cb9ef8dSAdrian Chadd 	vap->iv_key_delete = rum_key_delete;
6930e3b4c60SAdrian Chadd 	vap->iv_update_beacon = rum_update_beacon;
694349156a9SAndriy Voskoboinyk 	vap->iv_reset = rum_reset;
6952cb9ef8dSAdrian Chadd 	vap->iv_max_aid = RT2573_ADDR_MAX;
69602ac6454SAndrew Thompson 
697349156a9SAndriy Voskoboinyk 	if (opmode == IEEE80211_M_STA) {
698349156a9SAndriy Voskoboinyk 		/*
699349156a9SAndriy Voskoboinyk 		 * Move device to the sleep state when
700349156a9SAndriy Voskoboinyk 		 * beacon is received and there is no data for us.
701349156a9SAndriy Voskoboinyk 		 *
702349156a9SAndriy Voskoboinyk 		 * Used only for IEEE80211_S_SLEEP state.
703349156a9SAndriy Voskoboinyk 		 */
704349156a9SAndriy Voskoboinyk 		rvp->recv_mgmt = vap->iv_recv_mgmt;
705349156a9SAndriy Voskoboinyk 		vap->iv_recv_mgmt = rum_sta_recv_mgmt;
706349156a9SAndriy Voskoboinyk 
707349156a9SAndriy Voskoboinyk 		/* Ignored while sleeping. */
708349156a9SAndriy Voskoboinyk 		rvp->bmiss = vap->iv_bmiss;
709349156a9SAndriy Voskoboinyk 		vap->iv_bmiss = rum_beacon_miss;
710349156a9SAndriy Voskoboinyk 	}
711349156a9SAndriy Voskoboinyk 
712b6108616SRui Paulo 	usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0);
713b6108616SRui Paulo 	TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
714b6108616SRui Paulo 	ieee80211_ratectl_init(vap);
715b6108616SRui Paulo 	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
71602ac6454SAndrew Thompson 	/* complete setup */
7177a79cebfSGleb Smirnoff 	ieee80211_vap_attach(vap, ieee80211_media_change,
7187a79cebfSGleb Smirnoff 	    ieee80211_media_status, mac);
71902ac6454SAndrew Thompson 	ic->ic_opmode = opmode;
72002ac6454SAndrew Thompson 	return vap;
72102ac6454SAndrew Thompson }
72202ac6454SAndrew Thompson 
72302ac6454SAndrew Thompson static void
72402ac6454SAndrew Thompson rum_vap_delete(struct ieee80211vap *vap)
72502ac6454SAndrew Thompson {
72602ac6454SAndrew Thompson 	struct rum_vap *rvp = RUM_VAP(vap);
7275efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
728*d204cea9SAndriy Voskoboinyk 	struct rum_softc *sc = ic->ic_softc;
72902ac6454SAndrew Thompson 
730*d204cea9SAndriy Voskoboinyk 	/* Put vap into INIT state. */
731*d204cea9SAndriy Voskoboinyk 	ieee80211_new_state(vap, IEEE80211_S_INIT, -1);
732*d204cea9SAndriy Voskoboinyk 	ieee80211_draintask(ic, &vap->iv_nstate_task);
733*d204cea9SAndriy Voskoboinyk 
734*d204cea9SAndriy Voskoboinyk 	RUM_LOCK(sc);
735*d204cea9SAndriy Voskoboinyk 	/* Cancel any unfinished Tx. */
736*d204cea9SAndriy Voskoboinyk 	rum_reset_tx_list(sc, vap);
737*d204cea9SAndriy Voskoboinyk 	RUM_UNLOCK(sc);
738*d204cea9SAndriy Voskoboinyk 
739b6108616SRui Paulo 	usb_callout_drain(&rvp->ratectl_ch);
740b6108616SRui Paulo 	ieee80211_draintask(ic, &rvp->ratectl_task);
741b6108616SRui Paulo 	ieee80211_ratectl_deinit(vap);
74202ac6454SAndrew Thompson 	ieee80211_vap_detach(vap);
743*d204cea9SAndriy Voskoboinyk 	m_freem(rvp->bcn_mbuf);
74402ac6454SAndrew Thompson 	free(rvp, M_80211_VAP);
74502ac6454SAndrew Thompson }
74602ac6454SAndrew Thompson 
74702ac6454SAndrew Thompson static void
748f28c2f5eSAdrian Chadd rum_cmdq_cb(void *arg, int pending)
749f28c2f5eSAdrian Chadd {
750f28c2f5eSAdrian Chadd 	struct rum_softc *sc = arg;
751f28c2f5eSAdrian Chadd 	struct rum_cmdq *rc;
752f28c2f5eSAdrian Chadd 
753f28c2f5eSAdrian Chadd 	RUM_CMDQ_LOCK(sc);
754f28c2f5eSAdrian Chadd 	while (sc->cmdq[sc->cmdq_first].func != NULL) {
755f28c2f5eSAdrian Chadd 		rc = &sc->cmdq[sc->cmdq_first];
756f28c2f5eSAdrian Chadd 		RUM_CMDQ_UNLOCK(sc);
757f28c2f5eSAdrian Chadd 
758f28c2f5eSAdrian Chadd 		RUM_LOCK(sc);
759ed5711a1SAdrian Chadd 		rc->func(sc, &rc->data, rc->rvp_id);
760f28c2f5eSAdrian Chadd 		RUM_UNLOCK(sc);
761f28c2f5eSAdrian Chadd 
762f28c2f5eSAdrian Chadd 		RUM_CMDQ_LOCK(sc);
763f28c2f5eSAdrian Chadd 		memset(rc, 0, sizeof (*rc));
764f28c2f5eSAdrian Chadd 		sc->cmdq_first = (sc->cmdq_first + 1) % RUM_CMDQ_SIZE;
765f28c2f5eSAdrian Chadd 	}
766f28c2f5eSAdrian Chadd 	RUM_CMDQ_UNLOCK(sc);
767f28c2f5eSAdrian Chadd }
768f28c2f5eSAdrian Chadd 
769f28c2f5eSAdrian Chadd static int
770f28c2f5eSAdrian Chadd rum_cmd_sleepable(struct rum_softc *sc, const void *ptr, size_t len,
771ed5711a1SAdrian Chadd     uint8_t rvp_id, CMD_FUNC_PROTO)
772f28c2f5eSAdrian Chadd {
773f28c2f5eSAdrian Chadd 	struct ieee80211com *ic = &sc->sc_ic;
774f28c2f5eSAdrian Chadd 
775f28c2f5eSAdrian Chadd 	KASSERT(len <= sizeof(union sec_param), ("buffer overflow"));
776f28c2f5eSAdrian Chadd 
777f28c2f5eSAdrian Chadd 	RUM_CMDQ_LOCK(sc);
778f28c2f5eSAdrian Chadd 	if (sc->cmdq[sc->cmdq_last].func != NULL) {
779f28c2f5eSAdrian Chadd 		device_printf(sc->sc_dev, "%s: cmdq overflow\n", __func__);
780f28c2f5eSAdrian Chadd 		RUM_CMDQ_UNLOCK(sc);
781f28c2f5eSAdrian Chadd 
782f28c2f5eSAdrian Chadd 		return EAGAIN;
783f28c2f5eSAdrian Chadd 	}
784f28c2f5eSAdrian Chadd 
785f28c2f5eSAdrian Chadd 	if (ptr != NULL)
786f28c2f5eSAdrian Chadd 		memcpy(&sc->cmdq[sc->cmdq_last].data, ptr, len);
787f28c2f5eSAdrian Chadd 	sc->cmdq[sc->cmdq_last].rvp_id = rvp_id;
788f28c2f5eSAdrian Chadd 	sc->cmdq[sc->cmdq_last].func = func;
789f28c2f5eSAdrian Chadd 	sc->cmdq_last = (sc->cmdq_last + 1) % RUM_CMDQ_SIZE;
790f28c2f5eSAdrian Chadd 	RUM_CMDQ_UNLOCK(sc);
791f28c2f5eSAdrian Chadd 
792f28c2f5eSAdrian Chadd 	ieee80211_runtask(ic, &sc->cmdq_task);
793f28c2f5eSAdrian Chadd 
794f28c2f5eSAdrian Chadd 	return 0;
795f28c2f5eSAdrian Chadd }
796f28c2f5eSAdrian Chadd 
797f28c2f5eSAdrian Chadd static void
79802ac6454SAndrew Thompson rum_tx_free(struct rum_tx_data *data, int txerr)
79902ac6454SAndrew Thompson {
80002ac6454SAndrew Thompson 	struct rum_softc *sc = data->sc;
80102ac6454SAndrew Thompson 
80202ac6454SAndrew Thompson 	if (data->m != NULL) {
8037a79cebfSGleb Smirnoff 		ieee80211_tx_complete(data->ni, data->m, txerr);
80402ac6454SAndrew Thompson 		data->m = NULL;
80502ac6454SAndrew Thompson 		data->ni = NULL;
80602ac6454SAndrew Thompson 	}
80702ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
80802ac6454SAndrew Thompson 	sc->tx_nfree++;
80902ac6454SAndrew Thompson }
81002ac6454SAndrew Thompson 
81102ac6454SAndrew Thompson static void
81202ac6454SAndrew Thompson rum_setup_tx_list(struct rum_softc *sc)
81302ac6454SAndrew Thompson {
81402ac6454SAndrew Thompson 	struct rum_tx_data *data;
81502ac6454SAndrew Thompson 	int i;
81602ac6454SAndrew Thompson 
81702ac6454SAndrew Thompson 	sc->tx_nfree = 0;
81802ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_q);
81902ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_free);
82002ac6454SAndrew Thompson 
82102ac6454SAndrew Thompson 	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
82202ac6454SAndrew Thompson 		data = &sc->tx_data[i];
82302ac6454SAndrew Thompson 
82402ac6454SAndrew Thompson 		data->sc = sc;
82502ac6454SAndrew Thompson 		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
82602ac6454SAndrew Thompson 		sc->tx_nfree++;
82702ac6454SAndrew Thompson 	}
82802ac6454SAndrew Thompson }
82902ac6454SAndrew Thompson 
83002ac6454SAndrew Thompson static void
831*d204cea9SAndriy Voskoboinyk rum_reset_tx_list(struct rum_softc *sc, struct ieee80211vap *vap)
832*d204cea9SAndriy Voskoboinyk {
833*d204cea9SAndriy Voskoboinyk 	struct rum_tx_data *data, *tmp;
834*d204cea9SAndriy Voskoboinyk 
835*d204cea9SAndriy Voskoboinyk 	KASSERT(vap != NULL, ("%s: vap is NULL\n", __func__));
836*d204cea9SAndriy Voskoboinyk 
837*d204cea9SAndriy Voskoboinyk 	STAILQ_FOREACH_SAFE(data, &sc->tx_q, next, tmp) {
838*d204cea9SAndriy Voskoboinyk 		if (data->ni != NULL && data->ni->ni_vap == vap) {
839*d204cea9SAndriy Voskoboinyk 			ieee80211_free_node(data->ni);
840*d204cea9SAndriy Voskoboinyk 			data->ni = NULL;
841*d204cea9SAndriy Voskoboinyk 
842*d204cea9SAndriy Voskoboinyk 			KASSERT(data->m != NULL, ("%s: m is NULL\n",
843*d204cea9SAndriy Voskoboinyk 			    __func__));
844*d204cea9SAndriy Voskoboinyk 			m_freem(data->m);
845*d204cea9SAndriy Voskoboinyk 			data->m = NULL;
846*d204cea9SAndriy Voskoboinyk 
847*d204cea9SAndriy Voskoboinyk 			STAILQ_REMOVE(&sc->tx_q, data, rum_tx_data, next);
848*d204cea9SAndriy Voskoboinyk 			STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
849*d204cea9SAndriy Voskoboinyk 			sc->tx_nfree++;
850*d204cea9SAndriy Voskoboinyk 		}
851*d204cea9SAndriy Voskoboinyk 	}
852*d204cea9SAndriy Voskoboinyk }
853*d204cea9SAndriy Voskoboinyk 
854*d204cea9SAndriy Voskoboinyk static void
85502ac6454SAndrew Thompson rum_unsetup_tx_list(struct rum_softc *sc)
85602ac6454SAndrew Thompson {
85702ac6454SAndrew Thompson 	struct rum_tx_data *data;
85802ac6454SAndrew Thompson 	int i;
85902ac6454SAndrew Thompson 
86002ac6454SAndrew Thompson 	/* make sure any subsequent use of the queues will fail */
86102ac6454SAndrew Thompson 	sc->tx_nfree = 0;
86202ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_q);
86302ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_free);
86402ac6454SAndrew Thompson 
86502ac6454SAndrew Thompson 	/* free up all node references and mbufs */
86602ac6454SAndrew Thompson 	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
86702ac6454SAndrew Thompson 		data = &sc->tx_data[i];
86802ac6454SAndrew Thompson 
86902ac6454SAndrew Thompson 		if (data->m != NULL) {
87002ac6454SAndrew Thompson 			m_freem(data->m);
87102ac6454SAndrew Thompson 			data->m = NULL;
87202ac6454SAndrew Thompson 		}
87302ac6454SAndrew Thompson 		if (data->ni != NULL) {
87402ac6454SAndrew Thompson 			ieee80211_free_node(data->ni);
87502ac6454SAndrew Thompson 			data->ni = NULL;
87602ac6454SAndrew Thompson 		}
87702ac6454SAndrew Thompson 	}
87802ac6454SAndrew Thompson }
87902ac6454SAndrew Thompson 
880349156a9SAndriy Voskoboinyk static void
881349156a9SAndriy Voskoboinyk rum_beacon_miss(struct ieee80211vap *vap)
882349156a9SAndriy Voskoboinyk {
883349156a9SAndriy Voskoboinyk 	struct ieee80211com *ic = vap->iv_ic;
884349156a9SAndriy Voskoboinyk 	struct rum_softc *sc = ic->ic_softc;
885349156a9SAndriy Voskoboinyk 	struct rum_vap *rvp = RUM_VAP(vap);
886349156a9SAndriy Voskoboinyk 	int sleep;
887349156a9SAndriy Voskoboinyk 
888349156a9SAndriy Voskoboinyk 	RUM_LOCK(sc);
889349156a9SAndriy Voskoboinyk 	if (sc->sc_sleeping && sc->sc_sleep_end < ticks) {
890349156a9SAndriy Voskoboinyk 		DPRINTFN(12, "dropping 'sleeping' bit, "
891349156a9SAndriy Voskoboinyk 		    "device must be awake now\n");
892349156a9SAndriy Voskoboinyk 
893349156a9SAndriy Voskoboinyk 		sc->sc_sleeping = 0;
894349156a9SAndriy Voskoboinyk 	}
895349156a9SAndriy Voskoboinyk 
896349156a9SAndriy Voskoboinyk 	sleep = sc->sc_sleeping;
897349156a9SAndriy Voskoboinyk 	RUM_UNLOCK(sc);
898349156a9SAndriy Voskoboinyk 
899349156a9SAndriy Voskoboinyk 	if (!sleep)
900349156a9SAndriy Voskoboinyk 		rvp->bmiss(vap);
901349156a9SAndriy Voskoboinyk #ifdef USB_DEBUG
902349156a9SAndriy Voskoboinyk 	else
903349156a9SAndriy Voskoboinyk 		DPRINTFN(13, "bmiss event is ignored whilst sleeping\n");
904349156a9SAndriy Voskoboinyk #endif
905349156a9SAndriy Voskoboinyk }
906349156a9SAndriy Voskoboinyk 
907349156a9SAndriy Voskoboinyk static void
908349156a9SAndriy Voskoboinyk rum_sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, int subtype,
909349156a9SAndriy Voskoboinyk     const struct ieee80211_rx_stats *rxs,
910349156a9SAndriy Voskoboinyk     int rssi, int nf)
911349156a9SAndriy Voskoboinyk {
912349156a9SAndriy Voskoboinyk 	struct ieee80211vap *vap = ni->ni_vap;
913349156a9SAndriy Voskoboinyk 	struct rum_softc *sc = vap->iv_ic->ic_softc;
914349156a9SAndriy Voskoboinyk 	struct rum_vap *rvp = RUM_VAP(vap);
915349156a9SAndriy Voskoboinyk 
916349156a9SAndriy Voskoboinyk 	if (vap->iv_state == IEEE80211_S_SLEEP &&
917349156a9SAndriy Voskoboinyk 	    subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
918349156a9SAndriy Voskoboinyk 		RUM_LOCK(sc);
919349156a9SAndriy Voskoboinyk 		DPRINTFN(12, "beacon, mybss %d (flags %02X)\n",
920349156a9SAndriy Voskoboinyk 		    !!(sc->last_rx_flags & RT2573_RX_MYBSS),
921349156a9SAndriy Voskoboinyk 		    sc->last_rx_flags);
922349156a9SAndriy Voskoboinyk 
923349156a9SAndriy Voskoboinyk 		if ((sc->last_rx_flags & (RT2573_RX_MYBSS | RT2573_RX_BC)) ==
924349156a9SAndriy Voskoboinyk 		    (RT2573_RX_MYBSS | RT2573_RX_BC)) {
925349156a9SAndriy Voskoboinyk 			/*
926349156a9SAndriy Voskoboinyk 			 * Put it to sleep here; in case if there is a data
927349156a9SAndriy Voskoboinyk 			 * for us, iv_recv_mgmt() will wakeup the device via
928349156a9SAndriy Voskoboinyk 			 * SLEEP -> RUN state transition.
929349156a9SAndriy Voskoboinyk 			 */
930349156a9SAndriy Voskoboinyk 			rum_set_power_state(sc, 1);
931349156a9SAndriy Voskoboinyk 		}
932349156a9SAndriy Voskoboinyk 		RUM_UNLOCK(sc);
933349156a9SAndriy Voskoboinyk 	}
934349156a9SAndriy Voskoboinyk 
935349156a9SAndriy Voskoboinyk 	rvp->recv_mgmt(ni, m, subtype, rxs, rssi, nf);
936349156a9SAndriy Voskoboinyk }
937349156a9SAndriy Voskoboinyk 
938349156a9SAndriy Voskoboinyk static int
939349156a9SAndriy Voskoboinyk rum_set_power_state(struct rum_softc *sc, int sleep)
940349156a9SAndriy Voskoboinyk {
941349156a9SAndriy Voskoboinyk 	usb_error_t uerror;
942349156a9SAndriy Voskoboinyk 
943349156a9SAndriy Voskoboinyk 	RUM_LOCK_ASSERT(sc);
944349156a9SAndriy Voskoboinyk 
945349156a9SAndriy Voskoboinyk 	DPRINTFN(12, "moving to %s state (sleep time %u)\n",
946349156a9SAndriy Voskoboinyk 	    sleep ? "sleep" : "awake", sc->sc_sleep_time);
947349156a9SAndriy Voskoboinyk 
948349156a9SAndriy Voskoboinyk 	uerror = rum_do_mcu_request(sc,
949349156a9SAndriy Voskoboinyk 	    sleep ? RT2573_MCU_SLEEP : RT2573_MCU_WAKEUP);
950349156a9SAndriy Voskoboinyk 	if (uerror != USB_ERR_NORMAL_COMPLETION) {
951349156a9SAndriy Voskoboinyk 		device_printf(sc->sc_dev,
952349156a9SAndriy Voskoboinyk 		    "%s: could not change power state: %s\n",
953349156a9SAndriy Voskoboinyk 		    __func__, usbd_errstr(uerror));
954349156a9SAndriy Voskoboinyk 		return (EIO);
955349156a9SAndriy Voskoboinyk 	}
956349156a9SAndriy Voskoboinyk 
957349156a9SAndriy Voskoboinyk 	sc->sc_sleeping = !!sleep;
958349156a9SAndriy Voskoboinyk 	sc->sc_sleep_end = sleep ? ticks + sc->sc_sleep_time : 0;
959349156a9SAndriy Voskoboinyk 
960349156a9SAndriy Voskoboinyk 	return (0);
961349156a9SAndriy Voskoboinyk }
962349156a9SAndriy Voskoboinyk 
9635efea30fSAndrew Thompson static int
9645efea30fSAndrew Thompson rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
96502ac6454SAndrew Thompson {
96602ac6454SAndrew Thompson 	struct rum_vap *rvp = RUM_VAP(vap);
9675efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
968d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
96902ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
97002ac6454SAndrew Thompson 	enum ieee80211_state ostate;
97102ac6454SAndrew Thompson 	struct ieee80211_node *ni;
972349156a9SAndriy Voskoboinyk 	usb_error_t uerror;
97396e29c26SAndriy Voskoboinyk 	int ret = 0;
97402ac6454SAndrew Thompson 
97502ac6454SAndrew Thompson 	ostate = vap->iv_state;
9765efea30fSAndrew Thompson 	DPRINTF("%s -> %s\n",
9775efea30fSAndrew Thompson 		ieee80211_state_name[ostate],
9785efea30fSAndrew Thompson 		ieee80211_state_name[nstate]);
97902ac6454SAndrew Thompson 
9805efea30fSAndrew Thompson 	IEEE80211_UNLOCK(ic);
9815efea30fSAndrew Thompson 	RUM_LOCK(sc);
982b6108616SRui Paulo 	usb_callout_stop(&rvp->ratectl_ch);
9835efea30fSAndrew Thompson 
984349156a9SAndriy Voskoboinyk 	if (ostate == IEEE80211_S_SLEEP && vap->iv_opmode == IEEE80211_M_STA) {
985349156a9SAndriy Voskoboinyk 		rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_ACKCTS_PWRMGT);
986349156a9SAndriy Voskoboinyk 		rum_clrbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
987349156a9SAndriy Voskoboinyk 
988349156a9SAndriy Voskoboinyk 		/*
989349156a9SAndriy Voskoboinyk 		 * Ignore any errors;
990349156a9SAndriy Voskoboinyk 		 * any subsequent TX will wakeup it anyway
991349156a9SAndriy Voskoboinyk 		 */
992349156a9SAndriy Voskoboinyk 		(void) rum_set_power_state(sc, 0);
993349156a9SAndriy Voskoboinyk 	}
994349156a9SAndriy Voskoboinyk 
9955efea30fSAndrew Thompson 	switch (nstate) {
99602ac6454SAndrew Thompson 	case IEEE80211_S_INIT:
9976d804321SAdrian Chadd 		if (ostate == IEEE80211_S_RUN)
9986d804321SAdrian Chadd 			rum_abort_tsf_sync(sc);
9996d804321SAdrian Chadd 
100002ac6454SAndrew Thompson 		break;
100102ac6454SAndrew Thompson 
100202ac6454SAndrew Thompson 	case IEEE80211_S_RUN:
1003349156a9SAndriy Voskoboinyk 		if (ostate == IEEE80211_S_SLEEP)
1004349156a9SAndriy Voskoboinyk 			break;		/* already handled */
1005349156a9SAndriy Voskoboinyk 
10068d8bdb01SAndrew Thompson 		ni = ieee80211_ref_node(vap->iv_bss);
100702ac6454SAndrew Thompson 
100802ac6454SAndrew Thompson 		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
1009342ced03SAdrian Chadd 			if (ic->ic_bsschan == IEEE80211_CHAN_ANYC ||
1010342ced03SAdrian Chadd 			    ni->ni_chan == IEEE80211_CHAN_ANYC) {
1011aa81f853SAdrian Chadd 				ret = EINVAL;
1012aa81f853SAdrian Chadd 				goto run_fail;
1013bb571462SHans Petter Selasky 			}
1014ed5711a1SAdrian Chadd 			rum_update_slot_cb(sc, NULL, 0);
101502ac6454SAndrew Thompson 			rum_enable_mrr(sc);
101602ac6454SAndrew Thompson 			rum_set_txpreamble(sc);
101702ac6454SAndrew Thompson 			rum_set_basicrates(sc);
1018342ced03SAdrian Chadd 			rum_set_maxretry(sc, vap);
10191ba67b11SAdrian Chadd 			IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
10201ba67b11SAdrian Chadd 			rum_set_bssid(sc, sc->sc_bssid);
102102ac6454SAndrew Thompson 		}
102202ac6454SAndrew Thompson 
102302ac6454SAndrew Thompson 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1024aa81f853SAdrian Chadd 		    vap->iv_opmode == IEEE80211_M_IBSS) {
1025b780f864SAdrian Chadd 			if ((ret = rum_alloc_beacon(sc, vap)) != 0)
1026aa81f853SAdrian Chadd 				goto run_fail;
1027aa81f853SAdrian Chadd 		}
102802ac6454SAndrew Thompson 
102950a31b48SAdrian Chadd 		if (vap->iv_opmode != IEEE80211_M_MONITOR &&
103050a31b48SAdrian Chadd 		    vap->iv_opmode != IEEE80211_M_AHDEMO) {
1031aa81f853SAdrian Chadd 			if ((ret = rum_enable_tsf_sync(sc)) != 0)
1032aa81f853SAdrian Chadd 				goto run_fail;
1033aa81f853SAdrian Chadd 		} else
10345463c4a4SSam Leffler 			rum_enable_tsf(sc);
103502ac6454SAndrew Thompson 
103602ac6454SAndrew Thompson 		/* enable automatic rate adaptation */
10375463c4a4SSam Leffler 		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
103802ac6454SAndrew Thompson 		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
1039b6108616SRui Paulo 			rum_ratectl_start(sc, ni);
104096e29c26SAndriy Voskoboinyk run_fail:
10418d8bdb01SAndrew Thompson 		ieee80211_free_node(ni);
104202ac6454SAndrew Thompson 		break;
1043349156a9SAndriy Voskoboinyk 	case IEEE80211_S_SLEEP:
1044349156a9SAndriy Voskoboinyk 		/* Implemented for STA mode only. */
1045349156a9SAndriy Voskoboinyk 		if (vap->iv_opmode != IEEE80211_M_STA)
1046349156a9SAndriy Voskoboinyk 			break;
1047349156a9SAndriy Voskoboinyk 
1048349156a9SAndriy Voskoboinyk 		uerror = rum_setbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
1049349156a9SAndriy Voskoboinyk 		if (uerror != USB_ERR_NORMAL_COMPLETION) {
1050349156a9SAndriy Voskoboinyk 			ret = EIO;
1051349156a9SAndriy Voskoboinyk 			break;
1052349156a9SAndriy Voskoboinyk 		}
1053349156a9SAndriy Voskoboinyk 
1054349156a9SAndriy Voskoboinyk 		uerror = rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_ACKCTS_PWRMGT);
1055349156a9SAndriy Voskoboinyk 		if (uerror != USB_ERR_NORMAL_COMPLETION) {
1056349156a9SAndriy Voskoboinyk 			ret = EIO;
1057349156a9SAndriy Voskoboinyk 			break;
1058349156a9SAndriy Voskoboinyk 		}
1059349156a9SAndriy Voskoboinyk 
1060349156a9SAndriy Voskoboinyk 		ret = rum_set_power_state(sc, 1);
1061349156a9SAndriy Voskoboinyk 		if (ret != 0) {
1062349156a9SAndriy Voskoboinyk 			device_printf(sc->sc_dev,
1063349156a9SAndriy Voskoboinyk 			    "%s: could not move to the SLEEP state: %s\n",
1064349156a9SAndriy Voskoboinyk 			    __func__, usbd_errstr(uerror));
1065349156a9SAndriy Voskoboinyk 		}
1066349156a9SAndriy Voskoboinyk 		break;
106702ac6454SAndrew Thompson 	default:
106802ac6454SAndrew Thompson 		break;
106902ac6454SAndrew Thompson 	}
107002ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
107102ac6454SAndrew Thompson 	IEEE80211_LOCK(ic);
107296e29c26SAndriy Voskoboinyk 	return (ret == 0 ? rvp->newstate(vap, nstate, arg) : ret);
107302ac6454SAndrew Thompson }
107402ac6454SAndrew Thompson 
107502ac6454SAndrew Thompson static void
1076ed6d949aSAndrew Thompson rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
107702ac6454SAndrew Thompson {
1078ed6d949aSAndrew Thompson 	struct rum_softc *sc = usbd_xfer_softc(xfer);
10795463c4a4SSam Leffler 	struct ieee80211vap *vap;
108002ac6454SAndrew Thompson 	struct rum_tx_data *data;
108102ac6454SAndrew Thompson 	struct mbuf *m;
1082ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
108302ac6454SAndrew Thompson 	unsigned int len;
1084ed6d949aSAndrew Thompson 	int actlen, sumlen;
1085ed6d949aSAndrew Thompson 
1086ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
108702ac6454SAndrew Thompson 
108802ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
108902ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
1090ed6d949aSAndrew Thompson 		DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
109102ac6454SAndrew Thompson 
109202ac6454SAndrew Thompson 		/* free resources */
1093ed6d949aSAndrew Thompson 		data = usbd_xfer_get_priv(xfer);
109402ac6454SAndrew Thompson 		rum_tx_free(data, 0);
1095ed6d949aSAndrew Thompson 		usbd_xfer_set_priv(xfer, NULL);
109602ac6454SAndrew Thompson 
109702ac6454SAndrew Thompson 		/* FALLTHROUGH */
109802ac6454SAndrew Thompson 	case USB_ST_SETUP:
109902ac6454SAndrew Thompson tr_setup:
110002ac6454SAndrew Thompson 		data = STAILQ_FIRST(&sc->tx_q);
110102ac6454SAndrew Thompson 		if (data) {
110202ac6454SAndrew Thompson 			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
110302ac6454SAndrew Thompson 			m = data->m;
110402ac6454SAndrew Thompson 
11056d917491SHans Petter Selasky 			if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
110602ac6454SAndrew Thompson 				DPRINTFN(0, "data overflow, %u bytes\n",
110702ac6454SAndrew Thompson 				    m->m_pkthdr.len);
110802ac6454SAndrew Thompson 				m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
110902ac6454SAndrew Thompson 			}
1110ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
1111ed6d949aSAndrew Thompson 			usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
1112ed6d949aSAndrew Thompson 			usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
1113ed6d949aSAndrew Thompson 			    m->m_pkthdr.len);
111402ac6454SAndrew Thompson 
11155463c4a4SSam Leffler 			vap = data->ni->ni_vap;
11165463c4a4SSam Leffler 			if (ieee80211_radiotap_active_vap(vap)) {
111702ac6454SAndrew Thompson 				struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
111802ac6454SAndrew Thompson 
111902ac6454SAndrew Thompson 				tap->wt_flags = 0;
112002ac6454SAndrew Thompson 				tap->wt_rate = data->rate;
1121a6ccd477SAdrian Chadd 				rum_get_tsf(sc, &tap->wt_tsf);
112202ac6454SAndrew Thompson 				tap->wt_antenna = sc->tx_ant;
112302ac6454SAndrew Thompson 
11245463c4a4SSam Leffler 				ieee80211_radiotap_tx(vap, m);
112502ac6454SAndrew Thompson 			}
112602ac6454SAndrew Thompson 
112702ac6454SAndrew Thompson 			/* align end on a 4-bytes boundary */
112802ac6454SAndrew Thompson 			len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
112902ac6454SAndrew Thompson 			if ((len % 64) == 0)
113002ac6454SAndrew Thompson 				len += 4;
113102ac6454SAndrew Thompson 
113202ac6454SAndrew Thompson 			DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
113302ac6454SAndrew Thompson 			    m->m_pkthdr.len, len);
113402ac6454SAndrew Thompson 
1135ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, len);
1136ed6d949aSAndrew Thompson 			usbd_xfer_set_priv(xfer, data);
113702ac6454SAndrew Thompson 
1138a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
113902ac6454SAndrew Thompson 		}
11407a79cebfSGleb Smirnoff 		rum_start(sc);
114102ac6454SAndrew Thompson 		break;
114202ac6454SAndrew Thompson 
114302ac6454SAndrew Thompson 	default:			/* Error */
114402ac6454SAndrew Thompson 		DPRINTFN(11, "transfer error, %s\n",
1145ed6d949aSAndrew Thompson 		    usbd_errstr(error));
114602ac6454SAndrew Thompson 
11477a79cebfSGleb Smirnoff 		counter_u64_add(sc->sc_ic.ic_oerrors, 1);
1148ed6d949aSAndrew Thompson 		data = usbd_xfer_get_priv(xfer);
114902ac6454SAndrew Thompson 		if (data != NULL) {
1150ed6d949aSAndrew Thompson 			rum_tx_free(data, error);
1151ed6d949aSAndrew Thompson 			usbd_xfer_set_priv(xfer, NULL);
115202ac6454SAndrew Thompson 		}
115302ac6454SAndrew Thompson 
11540283fab7SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
11550283fab7SAndrew Thompson 			if (error == USB_ERR_TIMEOUT)
11560283fab7SAndrew Thompson 				device_printf(sc->sc_dev, "device timeout\n");
11570283fab7SAndrew Thompson 
11580283fab7SAndrew Thompson 			/*
11590283fab7SAndrew Thompson 			 * Try to clear stall first, also if other
11600283fab7SAndrew Thompson 			 * errors occur, hence clearing stall
11610283fab7SAndrew Thompson 			 * introduces a 50 ms delay:
11620283fab7SAndrew Thompson 			 */
1163ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
116402ac6454SAndrew Thompson 			goto tr_setup;
116502ac6454SAndrew Thompson 		}
116602ac6454SAndrew Thompson 		break;
116702ac6454SAndrew Thompson 	}
116802ac6454SAndrew Thompson }
116902ac6454SAndrew Thompson 
117002ac6454SAndrew Thompson static void
1171ed6d949aSAndrew Thompson rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
117202ac6454SAndrew Thompson {
1173ed6d949aSAndrew Thompson 	struct rum_softc *sc = usbd_xfer_softc(xfer);
11747a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
1175aca2cf30SAdrian Chadd 	struct ieee80211_frame_min *wh;
117602ac6454SAndrew Thompson 	struct ieee80211_node *ni;
117702ac6454SAndrew Thompson 	struct mbuf *m = NULL;
1178ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
117902ac6454SAndrew Thompson 	uint32_t flags;
118002ac6454SAndrew Thompson 	uint8_t rssi = 0;
1181ed6d949aSAndrew Thompson 	int len;
1182ed6d949aSAndrew Thompson 
1183ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
118402ac6454SAndrew Thompson 
118502ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
118602ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
118702ac6454SAndrew Thompson 
1188ed6d949aSAndrew Thompson 		DPRINTFN(15, "rx done, actlen=%d\n", len);
118902ac6454SAndrew Thompson 
1190c84bb702SAndriy Voskoboinyk 		if (len < RT2573_RX_DESC_SIZE) {
119102ac6454SAndrew Thompson 			DPRINTF("%s: xfer too short %d\n",
119202ac6454SAndrew Thompson 			    device_get_nameunit(sc->sc_dev), len);
11937a79cebfSGleb Smirnoff 			counter_u64_add(ic->ic_ierrors, 1);
119402ac6454SAndrew Thompson 			goto tr_setup;
119502ac6454SAndrew Thompson 		}
119602ac6454SAndrew Thompson 
119702ac6454SAndrew Thompson 		len -= RT2573_RX_DESC_SIZE;
1198ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
1199ed6d949aSAndrew Thompson 		usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
120002ac6454SAndrew Thompson 
120102ac6454SAndrew Thompson 		rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
120202ac6454SAndrew Thompson 		flags = le32toh(sc->sc_rx_desc.flags);
1203349156a9SAndriy Voskoboinyk 		sc->last_rx_flags = flags;
1204c84bb702SAndriy Voskoboinyk 		if (len < ((flags >> 16) & 0xfff)) {
1205c84bb702SAndriy Voskoboinyk 			DPRINTFN(5, "%s: frame is truncated from %d to %d "
1206c84bb702SAndriy Voskoboinyk 			    "bytes\n", device_get_nameunit(sc->sc_dev),
1207c84bb702SAndriy Voskoboinyk 			    (flags >> 16) & 0xfff, len);
1208c84bb702SAndriy Voskoboinyk 			counter_u64_add(ic->ic_ierrors, 1);
1209c84bb702SAndriy Voskoboinyk 			goto tr_setup;
1210c84bb702SAndriy Voskoboinyk 		}
1211c84bb702SAndriy Voskoboinyk 		len = (flags >> 16) & 0xfff;
1212c84bb702SAndriy Voskoboinyk 		if (len < sizeof(struct ieee80211_frame_ack)) {
1213c84bb702SAndriy Voskoboinyk 			DPRINTFN(5, "%s: frame too short %d\n",
1214c84bb702SAndriy Voskoboinyk 			    device_get_nameunit(sc->sc_dev), len);
1215c84bb702SAndriy Voskoboinyk 			counter_u64_add(ic->ic_ierrors, 1);
1216c84bb702SAndriy Voskoboinyk 			goto tr_setup;
1217c84bb702SAndriy Voskoboinyk 		}
121802ac6454SAndrew Thompson 		if (flags & RT2573_RX_CRC_ERROR) {
121902ac6454SAndrew Thompson 			/*
122002ac6454SAndrew Thompson 		         * This should not happen since we did not
122102ac6454SAndrew Thompson 		         * request to receive those frames when we
122202ac6454SAndrew Thompson 		         * filled RUM_TXRX_CSR2:
122302ac6454SAndrew Thompson 		         */
122402ac6454SAndrew Thompson 			DPRINTFN(5, "PHY or CRC error\n");
12257a79cebfSGleb Smirnoff 			counter_u64_add(ic->ic_ierrors, 1);
122602ac6454SAndrew Thompson 			goto tr_setup;
122702ac6454SAndrew Thompson 		}
12282cb9ef8dSAdrian Chadd 		if ((flags & RT2573_RX_DEC_MASK) != RT2573_RX_DEC_OK) {
12292cb9ef8dSAdrian Chadd 			switch (flags & RT2573_RX_DEC_MASK) {
12302cb9ef8dSAdrian Chadd 			case RT2573_RX_IV_ERROR:
12312cb9ef8dSAdrian Chadd 				DPRINTFN(5, "IV/EIV error\n");
12322cb9ef8dSAdrian Chadd 				break;
12332cb9ef8dSAdrian Chadd 			case RT2573_RX_MIC_ERROR:
12342cb9ef8dSAdrian Chadd 				DPRINTFN(5, "MIC error\n");
12352cb9ef8dSAdrian Chadd 				break;
12362cb9ef8dSAdrian Chadd 			case RT2573_RX_KEY_ERROR:
12372cb9ef8dSAdrian Chadd 				DPRINTFN(5, "Key error\n");
12382cb9ef8dSAdrian Chadd 				break;
12392cb9ef8dSAdrian Chadd 			}
12402cb9ef8dSAdrian Chadd 			counter_u64_add(ic->ic_ierrors, 1);
12412cb9ef8dSAdrian Chadd 			goto tr_setup;
12422cb9ef8dSAdrian Chadd 		}
124302ac6454SAndrew Thompson 
124493ae4747SAndriy Voskoboinyk 		m = m_get2(len, M_NOWAIT, MT_DATA, M_PKTHDR);
124502ac6454SAndrew Thompson 		if (m == NULL) {
124602ac6454SAndrew Thompson 			DPRINTF("could not allocate mbuf\n");
12477a79cebfSGleb Smirnoff 			counter_u64_add(ic->ic_ierrors, 1);
124802ac6454SAndrew Thompson 			goto tr_setup;
124902ac6454SAndrew Thompson 		}
1250ed6d949aSAndrew Thompson 		usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
125102ac6454SAndrew Thompson 		    mtod(m, uint8_t *), len);
125202ac6454SAndrew Thompson 
1253aca2cf30SAdrian Chadd 		wh = mtod(m, struct ieee80211_frame_min *);
1254aca2cf30SAdrian Chadd 
12552cb9ef8dSAdrian Chadd 		if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
12562cb9ef8dSAdrian Chadd 		    (flags & RT2573_RX_CIP_MASK) !=
12572cb9ef8dSAdrian Chadd 		     RT2573_RX_CIP_MODE(RT2573_MODE_NOSEC)) {
12582cb9ef8dSAdrian Chadd 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
12592cb9ef8dSAdrian Chadd 			m->m_flags |= M_WEP;
12602cb9ef8dSAdrian Chadd 		}
12612cb9ef8dSAdrian Chadd 
126202ac6454SAndrew Thompson 		/* finalize mbuf */
1263c84bb702SAndriy Voskoboinyk 		m->m_pkthdr.len = m->m_len = len;
126402ac6454SAndrew Thompson 
12655463c4a4SSam Leffler 		if (ieee80211_radiotap_active(ic)) {
126602ac6454SAndrew Thompson 			struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
126702ac6454SAndrew Thompson 
12685463c4a4SSam Leffler 			tap->wr_flags = 0;
126902ac6454SAndrew Thompson 			tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
127002ac6454SAndrew Thompson 			    (flags & RT2573_RX_OFDM) ?
127102ac6454SAndrew Thompson 			    IEEE80211_T_OFDM : IEEE80211_T_CCK);
1272a6ccd477SAdrian Chadd 			rum_get_tsf(sc, &tap->wr_tsf);
12735463c4a4SSam Leffler 			tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
12745463c4a4SSam Leffler 			tap->wr_antnoise = RT2573_NOISE_FLOOR;
127502ac6454SAndrew Thompson 			tap->wr_antenna = sc->rx_ant;
127602ac6454SAndrew Thompson 		}
127702ac6454SAndrew Thompson 		/* FALLTHROUGH */
127802ac6454SAndrew Thompson 	case USB_ST_SETUP:
127902ac6454SAndrew Thompson tr_setup:
1280ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1281a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
128202ac6454SAndrew Thompson 
128302ac6454SAndrew Thompson 		/*
128402ac6454SAndrew Thompson 		 * At the end of a USB callback it is always safe to unlock
128502ac6454SAndrew Thompson 		 * the private mutex of a device! That is why we do the
128602ac6454SAndrew Thompson 		 * "ieee80211_input" here, and not some lines up!
128702ac6454SAndrew Thompson 		 */
128802ac6454SAndrew Thompson 		RUM_UNLOCK(sc);
1289db63fc93SWeongyo Jeong 		if (m) {
1290aca2cf30SAdrian Chadd 			if (m->m_len >= sizeof(struct ieee80211_frame_min))
1291aca2cf30SAdrian Chadd 				ni = ieee80211_find_rxnode(ic, wh);
1292aca2cf30SAdrian Chadd 			else
1293aca2cf30SAdrian Chadd 				ni = NULL;
1294aca2cf30SAdrian Chadd 
129502ac6454SAndrew Thompson 			if (ni != NULL) {
129602ac6454SAndrew Thompson 				(void) ieee80211_input(ni, m, rssi,
12975463c4a4SSam Leffler 				    RT2573_NOISE_FLOOR);
129802ac6454SAndrew Thompson 				ieee80211_free_node(ni);
129902ac6454SAndrew Thompson 			} else
130002ac6454SAndrew Thompson 				(void) ieee80211_input_all(ic, m, rssi,
13015463c4a4SSam Leffler 				    RT2573_NOISE_FLOOR);
130202ac6454SAndrew Thompson 		}
1303db63fc93SWeongyo Jeong 		RUM_LOCK(sc);
13047a79cebfSGleb Smirnoff 		rum_start(sc);
130502ac6454SAndrew Thompson 		return;
130602ac6454SAndrew Thompson 
130702ac6454SAndrew Thompson 	default:			/* Error */
1308ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
130902ac6454SAndrew Thompson 			/* try to clear stall first */
1310ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
131102ac6454SAndrew Thompson 			goto tr_setup;
131202ac6454SAndrew Thompson 		}
131302ac6454SAndrew Thompson 		return;
131402ac6454SAndrew Thompson 	}
131502ac6454SAndrew Thompson }
131602ac6454SAndrew Thompson 
131702ac6454SAndrew Thompson static uint8_t
131802ac6454SAndrew Thompson rum_plcp_signal(int rate)
131902ac6454SAndrew Thompson {
132002ac6454SAndrew Thompson 	switch (rate) {
132102ac6454SAndrew Thompson 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
132202ac6454SAndrew Thompson 	case 12:	return 0xb;
132302ac6454SAndrew Thompson 	case 18:	return 0xf;
132402ac6454SAndrew Thompson 	case 24:	return 0xa;
132502ac6454SAndrew Thompson 	case 36:	return 0xe;
132602ac6454SAndrew Thompson 	case 48:	return 0x9;
132702ac6454SAndrew Thompson 	case 72:	return 0xd;
132802ac6454SAndrew Thompson 	case 96:	return 0x8;
132902ac6454SAndrew Thompson 	case 108:	return 0xc;
133002ac6454SAndrew Thompson 
133102ac6454SAndrew Thompson 	/* CCK rates (NB: not IEEE std, device-specific) */
133202ac6454SAndrew Thompson 	case 2:		return 0x0;
133302ac6454SAndrew Thompson 	case 4:		return 0x1;
133402ac6454SAndrew Thompson 	case 11:	return 0x2;
133502ac6454SAndrew Thompson 	case 22:	return 0x3;
133602ac6454SAndrew Thompson 	}
133702ac6454SAndrew Thompson 	return 0xff;		/* XXX unsupported/unknown rate */
133802ac6454SAndrew Thompson }
133902ac6454SAndrew Thompson 
13402cb9ef8dSAdrian Chadd /*
13412cb9ef8dSAdrian Chadd  * Map net80211 cipher to RT2573 security mode.
13422cb9ef8dSAdrian Chadd  */
13432cb9ef8dSAdrian Chadd static uint8_t
13442cb9ef8dSAdrian Chadd rum_crypto_mode(struct rum_softc *sc, u_int cipher, int keylen)
13452cb9ef8dSAdrian Chadd {
13462cb9ef8dSAdrian Chadd 	switch (cipher) {
13472cb9ef8dSAdrian Chadd 	case IEEE80211_CIPHER_WEP:
13482cb9ef8dSAdrian Chadd 		return (keylen < 8 ? RT2573_MODE_WEP40 : RT2573_MODE_WEP104);
13492cb9ef8dSAdrian Chadd 	case IEEE80211_CIPHER_TKIP:
13502cb9ef8dSAdrian Chadd 		return RT2573_MODE_TKIP;
13512cb9ef8dSAdrian Chadd 	case IEEE80211_CIPHER_AES_CCM:
13522cb9ef8dSAdrian Chadd 		return RT2573_MODE_AES_CCMP;
13532cb9ef8dSAdrian Chadd 	default:
13542cb9ef8dSAdrian Chadd 		device_printf(sc->sc_dev, "unknown cipher %d\n", cipher);
13552cb9ef8dSAdrian Chadd 		return 0;
13562cb9ef8dSAdrian Chadd 	}
13572cb9ef8dSAdrian Chadd }
13582cb9ef8dSAdrian Chadd 
135902ac6454SAndrew Thompson static void
136002ac6454SAndrew Thompson rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
13611354b52cSAdrian Chadd     struct ieee80211_key *k, uint32_t flags, uint8_t xflags, uint8_t qid,
13621354b52cSAdrian Chadd     int hdrlen, int len, int rate)
136302ac6454SAndrew Thompson {
13647a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
13651354b52cSAdrian Chadd 	struct wmeParams *wmep = &sc->wme_params[qid];
136602ac6454SAndrew Thompson 	uint16_t plcp_length;
136702ac6454SAndrew Thompson 	int remainder;
136802ac6454SAndrew Thompson 
13692cb9ef8dSAdrian Chadd 	flags |= RT2573_TX_VALID;
13702cb9ef8dSAdrian Chadd 	flags |= len << 16;
137102ac6454SAndrew Thompson 
13722cb9ef8dSAdrian Chadd 	if (k != NULL && !(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
13732cb9ef8dSAdrian Chadd 		const struct ieee80211_cipher *cip = k->wk_cipher;
137402ac6454SAndrew Thompson 
13752cb9ef8dSAdrian Chadd 		len += cip->ic_header + cip->ic_trailer + cip->ic_miclen;
13762cb9ef8dSAdrian Chadd 
13772cb9ef8dSAdrian Chadd 		desc->eiv = 0;		/* for WEP */
13782cb9ef8dSAdrian Chadd 		cip->ic_setiv(k, (uint8_t *)&desc->iv);
13792cb9ef8dSAdrian Chadd 	}
138002ac6454SAndrew Thompson 
138102ac6454SAndrew Thompson 	/* setup PLCP fields */
138202ac6454SAndrew Thompson 	desc->plcp_signal  = rum_plcp_signal(rate);
138302ac6454SAndrew Thompson 	desc->plcp_service = 4;
138402ac6454SAndrew Thompson 
138502ac6454SAndrew Thompson 	len += IEEE80211_CRC_LEN;
138626d39e2cSSam Leffler 	if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
13872cb9ef8dSAdrian Chadd 		flags |= RT2573_TX_OFDM;
138802ac6454SAndrew Thompson 
138902ac6454SAndrew Thompson 		plcp_length = len & 0xfff;
139002ac6454SAndrew Thompson 		desc->plcp_length_hi = plcp_length >> 6;
139102ac6454SAndrew Thompson 		desc->plcp_length_lo = plcp_length & 0x3f;
139202ac6454SAndrew Thompson 	} else {
1393b8e3094cSHans Petter Selasky 		if (rate == 0)
1394b8e3094cSHans Petter Selasky 			rate = 2;	/* avoid division by zero */
1395057b4402SPedro F. Giffuni 		plcp_length = howmany(16 * len, rate);
139602ac6454SAndrew Thompson 		if (rate == 22) {
139702ac6454SAndrew Thompson 			remainder = (16 * len) % 22;
139802ac6454SAndrew Thompson 			if (remainder != 0 && remainder < 7)
139902ac6454SAndrew Thompson 				desc->plcp_service |= RT2573_PLCP_LENGEXT;
140002ac6454SAndrew Thompson 		}
140102ac6454SAndrew Thompson 		desc->plcp_length_hi = plcp_length >> 8;
140202ac6454SAndrew Thompson 		desc->plcp_length_lo = plcp_length & 0xff;
140302ac6454SAndrew Thompson 
140402ac6454SAndrew Thompson 		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
140502ac6454SAndrew Thompson 			desc->plcp_signal |= 0x08;
140602ac6454SAndrew Thompson 	}
14072cb9ef8dSAdrian Chadd 
14082cb9ef8dSAdrian Chadd 	desc->flags = htole32(flags);
14092cb9ef8dSAdrian Chadd 	desc->hdrlen = hdrlen;
14102cb9ef8dSAdrian Chadd 	desc->xflags = xflags;
14112cb9ef8dSAdrian Chadd 
14121354b52cSAdrian Chadd 	desc->wme = htole16(RT2573_QID(qid) |
14131354b52cSAdrian Chadd 	    RT2573_AIFSN(wmep->wmep_aifsn) |
14141354b52cSAdrian Chadd 	    RT2573_LOGCWMIN(wmep->wmep_logcwmin) |
14151354b52cSAdrian Chadd 	    RT2573_LOGCWMAX(wmep->wmep_logcwmax));
141602ac6454SAndrew Thompson }
141702ac6454SAndrew Thompson 
141802ac6454SAndrew Thompson static int
141902ac6454SAndrew Thompson rum_sendprot(struct rum_softc *sc,
142002ac6454SAndrew Thompson     const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
142102ac6454SAndrew Thompson {
142202ac6454SAndrew Thompson 	struct ieee80211com *ic = ni->ni_ic;
142302ac6454SAndrew Thompson 	const struct ieee80211_frame *wh;
142402ac6454SAndrew Thompson 	struct rum_tx_data *data;
142502ac6454SAndrew Thompson 	struct mbuf *mprot;
1426e7b7db3dSAdrian Chadd 	int protrate, pktlen, flags, isshort;
142702ac6454SAndrew Thompson 	uint16_t dur;
142802ac6454SAndrew Thompson 
1429e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
143002ac6454SAndrew Thompson 	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
143102ac6454SAndrew Thompson 	    ("protection %d", prot));
143202ac6454SAndrew Thompson 
143302ac6454SAndrew Thompson 	wh = mtod(m, const struct ieee80211_frame *);
143402ac6454SAndrew Thompson 	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
143502ac6454SAndrew Thompson 
143626d39e2cSSam Leffler 	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
143702ac6454SAndrew Thompson 
143802ac6454SAndrew Thompson 	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
14390f817de7SJung-uk Kim 	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
144026d39e2cSSam Leffler 	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1441c42e124eSAdrian Chadd 	flags = 0;
144202ac6454SAndrew Thompson 	if (prot == IEEE80211_PROT_RTSCTS) {
144302ac6454SAndrew Thompson 		/* NB: CTS is the same size as an ACK */
144426d39e2cSSam Leffler 		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
144502ac6454SAndrew Thompson 		flags |= RT2573_TX_NEED_ACK;
144602ac6454SAndrew Thompson 		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
144702ac6454SAndrew Thompson 	} else {
144802ac6454SAndrew Thompson 		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
144902ac6454SAndrew Thompson 	}
145002ac6454SAndrew Thompson 	if (mprot == NULL) {
145102ac6454SAndrew Thompson 		/* XXX stat + msg */
145277ddf3d3SAndrew Thompson 		return (ENOBUFS);
145302ac6454SAndrew Thompson 	}
145402ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
145502ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
145602ac6454SAndrew Thompson 	sc->tx_nfree--;
145702ac6454SAndrew Thompson 
145802ac6454SAndrew Thompson 	data->m = mprot;
145902ac6454SAndrew Thompson 	data->ni = ieee80211_ref_node(ni);
146002ac6454SAndrew Thompson 	data->rate = protrate;
14611354b52cSAdrian Chadd 	rum_setup_tx_desc(sc, &data->desc, NULL, flags, 0, 0, 0,
14622cb9ef8dSAdrian Chadd 	    mprot->m_pkthdr.len, protrate);
146302ac6454SAndrew Thompson 
146402ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1465a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
146602ac6454SAndrew Thompson 
146702ac6454SAndrew Thompson 	return 0;
146802ac6454SAndrew Thompson }
146902ac6454SAndrew Thompson 
14702cb9ef8dSAdrian Chadd static uint32_t
14712cb9ef8dSAdrian Chadd rum_tx_crypto_flags(struct rum_softc *sc, struct ieee80211_node *ni,
14722cb9ef8dSAdrian Chadd     const struct ieee80211_key *k)
14732cb9ef8dSAdrian Chadd {
14742cb9ef8dSAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
14752cb9ef8dSAdrian Chadd 	u_int cipher;
14762cb9ef8dSAdrian Chadd 	uint32_t flags = 0;
14772cb9ef8dSAdrian Chadd 	uint8_t mode, pos;
14782cb9ef8dSAdrian Chadd 
14792cb9ef8dSAdrian Chadd 	if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
14802cb9ef8dSAdrian Chadd 		cipher = k->wk_cipher->ic_cipher;
14812cb9ef8dSAdrian Chadd 		pos = k->wk_keyix;
14822cb9ef8dSAdrian Chadd 		mode = rum_crypto_mode(sc, cipher, k->wk_keylen);
14832cb9ef8dSAdrian Chadd 		if (mode == 0)
14842cb9ef8dSAdrian Chadd 			return 0;
14852cb9ef8dSAdrian Chadd 
14862cb9ef8dSAdrian Chadd 		flags |= RT2573_TX_CIP_MODE(mode);
14872cb9ef8dSAdrian Chadd 
14882cb9ef8dSAdrian Chadd 		/* Do not trust GROUP flag */
14892cb9ef8dSAdrian Chadd 		if (!(k >= &vap->iv_nw_keys[0] &&
14902cb9ef8dSAdrian Chadd 		      k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]))
14912cb9ef8dSAdrian Chadd 			flags |= RT2573_TX_KEY_PAIR;
14922cb9ef8dSAdrian Chadd 		else
14932cb9ef8dSAdrian Chadd 			pos += 0 * RT2573_SKEY_MAX;	/* vap id */
14942cb9ef8dSAdrian Chadd 
14952cb9ef8dSAdrian Chadd 		flags |= RT2573_TX_KEY_ID(pos);
14962cb9ef8dSAdrian Chadd 
14972cb9ef8dSAdrian Chadd 		if (cipher == IEEE80211_CIPHER_TKIP)
14982cb9ef8dSAdrian Chadd 			flags |= RT2573_TX_TKIPMIC;
14992cb9ef8dSAdrian Chadd 	}
15002cb9ef8dSAdrian Chadd 
15012cb9ef8dSAdrian Chadd 	return flags;
15022cb9ef8dSAdrian Chadd }
15032cb9ef8dSAdrian Chadd 
150402ac6454SAndrew Thompson static int
150502ac6454SAndrew Thompson rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
150602ac6454SAndrew Thompson {
150702ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
15087a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
150902ac6454SAndrew Thompson 	struct rum_tx_data *data;
151002ac6454SAndrew Thompson 	struct ieee80211_frame *wh;
151102ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
15122cb9ef8dSAdrian Chadd 	struct ieee80211_key *k = NULL;
151302ac6454SAndrew Thompson 	uint32_t flags = 0;
151402ac6454SAndrew Thompson 	uint16_t dur;
15151354b52cSAdrian Chadd 	uint8_t ac, type, xflags = 0;
15162cb9ef8dSAdrian Chadd 	int hdrlen;
151702ac6454SAndrew Thompson 
1518e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
151902ac6454SAndrew Thompson 
152002ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
152102ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
152202ac6454SAndrew Thompson 	sc->tx_nfree--;
152302ac6454SAndrew Thompson 
152402ac6454SAndrew Thompson 	wh = mtod(m0, struct ieee80211_frame *);
1525c42e124eSAdrian Chadd 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
15262cb9ef8dSAdrian Chadd 	hdrlen = ieee80211_anyhdrsize(wh);
15271354b52cSAdrian Chadd 	ac = M_WME_GETAC(m0);
15282cb9ef8dSAdrian Chadd 
15295945b5f5SKevin Lo 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
15302cb9ef8dSAdrian Chadd 		k = ieee80211_crypto_get_txkey(ni, m0);
1531b2a76342SAdrian Chadd 		if (k == NULL)
15322cb9ef8dSAdrian Chadd 			return (ENOENT);
15332cb9ef8dSAdrian Chadd 
15342cb9ef8dSAdrian Chadd 		if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
15352cb9ef8dSAdrian Chadd 		    !k->wk_cipher->ic_encap(k, m0))
1536b2a76342SAdrian Chadd 			return (ENOBUFS);
1537b2a76342SAdrian Chadd 
153802ac6454SAndrew Thompson 		wh = mtod(m0, struct ieee80211_frame *);
153902ac6454SAndrew Thompson 	}
154002ac6454SAndrew Thompson 
154102ac6454SAndrew Thompson 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
154202ac6454SAndrew Thompson 
154302ac6454SAndrew Thompson 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
154402ac6454SAndrew Thompson 		flags |= RT2573_TX_NEED_ACK;
154502ac6454SAndrew Thompson 
154626d39e2cSSam Leffler 		dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
154702ac6454SAndrew Thompson 		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
15488cfe5440SHans Petter Selasky 		USETW(wh->i_dur, dur);
154902ac6454SAndrew Thompson 
155002ac6454SAndrew Thompson 		/* tell hardware to add timestamp for probe responses */
1551c42e124eSAdrian Chadd 		if (type == IEEE80211_FC0_TYPE_MGT &&
1552c42e124eSAdrian Chadd 		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1553c42e124eSAdrian Chadd 		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
155402ac6454SAndrew Thompson 			flags |= RT2573_TX_TIMESTAMP;
155502ac6454SAndrew Thompson 	}
155602ac6454SAndrew Thompson 
1557c42e124eSAdrian Chadd 	if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1558c42e124eSAdrian Chadd 		xflags |= RT2573_TX_HWSEQ;
1559c42e124eSAdrian Chadd 
15602cb9ef8dSAdrian Chadd 	if (k != NULL)
15612cb9ef8dSAdrian Chadd 		flags |= rum_tx_crypto_flags(sc, ni, k);
15622cb9ef8dSAdrian Chadd 
156302ac6454SAndrew Thompson 	data->m = m0;
156402ac6454SAndrew Thompson 	data->ni = ni;
156502ac6454SAndrew Thompson 	data->rate = tp->mgmtrate;
156602ac6454SAndrew Thompson 
15671354b52cSAdrian Chadd 	rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
15682cb9ef8dSAdrian Chadd 	    m0->m_pkthdr.len, tp->mgmtrate);
156902ac6454SAndrew Thompson 
157002ac6454SAndrew Thompson 	DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
157102ac6454SAndrew Thompson 	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
157202ac6454SAndrew Thompson 
157302ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1574a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
157502ac6454SAndrew Thompson 
157677ddf3d3SAndrew Thompson 	return (0);
157702ac6454SAndrew Thompson }
157802ac6454SAndrew Thompson 
157902ac6454SAndrew Thompson static int
158002ac6454SAndrew Thompson rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
158102ac6454SAndrew Thompson     const struct ieee80211_bpf_params *params)
158202ac6454SAndrew Thompson {
1583515db61dSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1584c42e124eSAdrian Chadd 	struct ieee80211_frame *wh;
158502ac6454SAndrew Thompson 	struct rum_tx_data *data;
158602ac6454SAndrew Thompson 	uint32_t flags;
15871354b52cSAdrian Chadd 	uint8_t ac, type, xflags = 0;
158802ac6454SAndrew Thompson 	int rate, error;
158902ac6454SAndrew Thompson 
1590e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
159102ac6454SAndrew Thompson 
1592c42e124eSAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1593c42e124eSAdrian Chadd 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1594c42e124eSAdrian Chadd 
15951354b52cSAdrian Chadd 	ac = params->ibp_pri & 3;
15961354b52cSAdrian Chadd 
1597515db61dSSam Leffler 	rate = params->ibp_rate0;
1598b2a76342SAdrian Chadd 	if (!ieee80211_isratevalid(ic->ic_rt, rate))
1599b2a76342SAdrian Chadd 		return (EINVAL);
1600b2a76342SAdrian Chadd 
160102ac6454SAndrew Thompson 	flags = 0;
160202ac6454SAndrew Thompson 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
160302ac6454SAndrew Thompson 		flags |= RT2573_TX_NEED_ACK;
160402ac6454SAndrew Thompson 	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
160502ac6454SAndrew Thompson 		error = rum_sendprot(sc, m0, ni,
160602ac6454SAndrew Thompson 		    params->ibp_flags & IEEE80211_BPF_RTS ?
160702ac6454SAndrew Thompson 			 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
160802ac6454SAndrew Thompson 		    rate);
1609b2a76342SAdrian Chadd 		if (error || sc->tx_nfree == 0)
1610b2a76342SAdrian Chadd 			return (ENOBUFS);
1611b2a76342SAdrian Chadd 
161202ac6454SAndrew Thompson 		flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
161302ac6454SAndrew Thompson 	}
161402ac6454SAndrew Thompson 
1615c42e124eSAdrian Chadd 	if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1616c42e124eSAdrian Chadd 		xflags |= RT2573_TX_HWSEQ;
1617c42e124eSAdrian Chadd 
161896ca458fSAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
161996ca458fSAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
162096ca458fSAndrew Thompson 	sc->tx_nfree--;
162196ca458fSAndrew Thompson 
162202ac6454SAndrew Thompson 	data->m = m0;
162302ac6454SAndrew Thompson 	data->ni = ni;
162402ac6454SAndrew Thompson 	data->rate = rate;
162502ac6454SAndrew Thompson 
162602ac6454SAndrew Thompson 	/* XXX need to setup descriptor ourself */
16271354b52cSAdrian Chadd 	rum_setup_tx_desc(sc, &data->desc, NULL, flags, xflags, ac, 0,
1628c42e124eSAdrian Chadd 	    m0->m_pkthdr.len, rate);
162902ac6454SAndrew Thompson 
163002ac6454SAndrew Thompson 	DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
163102ac6454SAndrew Thompson 	    m0->m_pkthdr.len, rate);
163202ac6454SAndrew Thompson 
163302ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1634a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
163502ac6454SAndrew Thompson 
163602ac6454SAndrew Thompson 	return 0;
163702ac6454SAndrew Thompson }
163802ac6454SAndrew Thompson 
163902ac6454SAndrew Thompson static int
164002ac6454SAndrew Thompson rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
164102ac6454SAndrew Thompson {
164202ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
16437a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
164402ac6454SAndrew Thompson 	struct rum_tx_data *data;
164502ac6454SAndrew Thompson 	struct ieee80211_frame *wh;
164602ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
16472cb9ef8dSAdrian Chadd 	struct ieee80211_key *k = NULL;
164802ac6454SAndrew Thompson 	uint32_t flags = 0;
164902ac6454SAndrew Thompson 	uint16_t dur;
16501354b52cSAdrian Chadd 	uint8_t ac, type, qos, xflags = 0;
16512cb9ef8dSAdrian Chadd 	int error, hdrlen, rate;
165202ac6454SAndrew Thompson 
1653e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
165402ac6454SAndrew Thompson 
165502ac6454SAndrew Thompson 	wh = mtod(m0, struct ieee80211_frame *);
1656c42e124eSAdrian Chadd 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
16572cb9ef8dSAdrian Chadd 	hdrlen = ieee80211_anyhdrsize(wh);
165802ac6454SAndrew Thompson 
16591354b52cSAdrian Chadd 	if (IEEE80211_QOS_HAS_SEQ(wh))
16601354b52cSAdrian Chadd 		qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
16611354b52cSAdrian Chadd 	else
16621354b52cSAdrian Chadd 		qos = 0;
16631354b52cSAdrian Chadd 	ac = M_WME_GETAC(m0);
16641354b52cSAdrian Chadd 
166502ac6454SAndrew Thompson 	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
166602ac6454SAndrew Thompson 	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
166702ac6454SAndrew Thompson 		rate = tp->mcastrate;
166802ac6454SAndrew Thompson 	else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
166902ac6454SAndrew Thompson 		rate = tp->ucastrate;
16704c90f11bSAndriy Voskoboinyk 	else if (m0->m_flags & M_EAPOL)
16714c90f11bSAndriy Voskoboinyk 		rate = tp->mgmtrate;
167202ac6454SAndrew Thompson 	else
167302ac6454SAndrew Thompson 		rate = ni->ni_txrate;
167402ac6454SAndrew Thompson 
16755945b5f5SKevin Lo 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
16762cb9ef8dSAdrian Chadd 		k = ieee80211_crypto_get_txkey(ni, m0);
167702ac6454SAndrew Thompson 		if (k == NULL) {
167802ac6454SAndrew Thompson 			m_freem(m0);
16792cb9ef8dSAdrian Chadd 			return (ENOENT);
16802cb9ef8dSAdrian Chadd 		}
16812cb9ef8dSAdrian Chadd 		if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
16822cb9ef8dSAdrian Chadd 		    !k->wk_cipher->ic_encap(k, m0)) {
16832cb9ef8dSAdrian Chadd 			m_freem(m0);
16842cb9ef8dSAdrian Chadd 			return (ENOBUFS);
168502ac6454SAndrew Thompson 		}
168602ac6454SAndrew Thompson 
168702ac6454SAndrew Thompson 		/* packet header may have moved, reset our local pointer */
168802ac6454SAndrew Thompson 		wh = mtod(m0, struct ieee80211_frame *);
168902ac6454SAndrew Thompson 	}
169002ac6454SAndrew Thompson 
1691c42e124eSAdrian Chadd 	if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1692c42e124eSAdrian Chadd 		xflags |= RT2573_TX_HWSEQ;
1693c42e124eSAdrian Chadd 
169402ac6454SAndrew Thompson 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
169502ac6454SAndrew Thompson 		int prot = IEEE80211_PROT_NONE;
169602ac6454SAndrew Thompson 		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
169702ac6454SAndrew Thompson 			prot = IEEE80211_PROT_RTSCTS;
169802ac6454SAndrew Thompson 		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
169926d39e2cSSam Leffler 		    ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
170002ac6454SAndrew Thompson 			prot = ic->ic_protmode;
170102ac6454SAndrew Thompson 		if (prot != IEEE80211_PROT_NONE) {
170202ac6454SAndrew Thompson 			error = rum_sendprot(sc, m0, ni, prot, rate);
170396ca458fSAndrew Thompson 			if (error || sc->tx_nfree == 0) {
170402ac6454SAndrew Thompson 				m_freem(m0);
170596ca458fSAndrew Thompson 				return ENOBUFS;
170602ac6454SAndrew Thompson 			}
170702ac6454SAndrew Thompson 			flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
170802ac6454SAndrew Thompson 		}
170902ac6454SAndrew Thompson 	}
171002ac6454SAndrew Thompson 
17112cb9ef8dSAdrian Chadd 	if (k != NULL)
17122cb9ef8dSAdrian Chadd 		flags |= rum_tx_crypto_flags(sc, ni, k);
17132cb9ef8dSAdrian Chadd 
171402ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
171502ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
171602ac6454SAndrew Thompson 	sc->tx_nfree--;
171702ac6454SAndrew Thompson 
171802ac6454SAndrew Thompson 	data->m = m0;
171902ac6454SAndrew Thompson 	data->ni = ni;
172002ac6454SAndrew Thompson 	data->rate = rate;
172102ac6454SAndrew Thompson 
172202ac6454SAndrew Thompson 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
17231354b52cSAdrian Chadd 		/* Unicast frame, check if an ACK is expected. */
17241354b52cSAdrian Chadd 		if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
17251354b52cSAdrian Chadd 		    IEEE80211_QOS_ACKPOLICY_NOACK)
172602ac6454SAndrew Thompson 			flags |= RT2573_TX_NEED_ACK;
172702ac6454SAndrew Thompson 
172826d39e2cSSam Leffler 		dur = ieee80211_ack_duration(ic->ic_rt, rate,
172902ac6454SAndrew Thompson 		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
17308cfe5440SHans Petter Selasky 		USETW(wh->i_dur, dur);
173102ac6454SAndrew Thompson 	}
173202ac6454SAndrew Thompson 
17331354b52cSAdrian Chadd 	rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
17342cb9ef8dSAdrian Chadd 	    m0->m_pkthdr.len, rate);
173502ac6454SAndrew Thompson 
173602ac6454SAndrew Thompson 	DPRINTFN(10, "sending frame len=%d rate=%d\n",
173702ac6454SAndrew Thompson 	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
173802ac6454SAndrew Thompson 
173902ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1740a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
174102ac6454SAndrew Thompson 
174202ac6454SAndrew Thompson 	return 0;
174302ac6454SAndrew Thompson }
174402ac6454SAndrew Thompson 
17457a79cebfSGleb Smirnoff static int
17467a79cebfSGleb Smirnoff rum_transmit(struct ieee80211com *ic, struct mbuf *m)
174779d2c5e8SGleb Smirnoff {
17487a79cebfSGleb Smirnoff 	struct rum_softc *sc = ic->ic_softc;
17497a79cebfSGleb Smirnoff 	int error;
17507a79cebfSGleb Smirnoff 
17517a79cebfSGleb Smirnoff 	RUM_LOCK(sc);
17527a79cebfSGleb Smirnoff 	if (!sc->sc_running) {
17537a79cebfSGleb Smirnoff 		RUM_UNLOCK(sc);
17547a79cebfSGleb Smirnoff 		return (ENXIO);
17557a79cebfSGleb Smirnoff 	}
17567a79cebfSGleb Smirnoff 	error = mbufq_enqueue(&sc->sc_snd, m);
17577a79cebfSGleb Smirnoff 	if (error) {
17587a79cebfSGleb Smirnoff 		RUM_UNLOCK(sc);
17597a79cebfSGleb Smirnoff 		return (error);
17607a79cebfSGleb Smirnoff 	}
17617a79cebfSGleb Smirnoff 	rum_start(sc);
17627a79cebfSGleb Smirnoff 	RUM_UNLOCK(sc);
17637a79cebfSGleb Smirnoff 
17647a79cebfSGleb Smirnoff 	return (0);
17657a79cebfSGleb Smirnoff }
17667a79cebfSGleb Smirnoff 
17677a79cebfSGleb Smirnoff static void
17687a79cebfSGleb Smirnoff rum_start(struct rum_softc *sc)
17697a79cebfSGleb Smirnoff {
177002ac6454SAndrew Thompson 	struct ieee80211_node *ni;
177102ac6454SAndrew Thompson 	struct mbuf *m;
177202ac6454SAndrew Thompson 
1773e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
17747a79cebfSGleb Smirnoff 
17757a79cebfSGleb Smirnoff 	if (!sc->sc_running)
177602ac6454SAndrew Thompson 		return;
17777a79cebfSGleb Smirnoff 
17787a79cebfSGleb Smirnoff 	while (sc->tx_nfree >= RUM_TX_MINFREE &&
17797a79cebfSGleb Smirnoff 	    (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
178002ac6454SAndrew Thompson 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
178102ac6454SAndrew Thompson 		if (rum_tx_data(sc, m, ni) != 0) {
17827a79cebfSGleb Smirnoff 			if_inc_counter(ni->ni_vap->iv_ifp,
17837a79cebfSGleb Smirnoff 			    IFCOUNTER_OERRORS, 1);
178402ac6454SAndrew Thompson 			ieee80211_free_node(ni);
178502ac6454SAndrew Thompson 			break;
178602ac6454SAndrew Thompson 		}
178702ac6454SAndrew Thompson 	}
178802ac6454SAndrew Thompson }
178902ac6454SAndrew Thompson 
17907a79cebfSGleb Smirnoff static void
17917a79cebfSGleb Smirnoff rum_parent(struct ieee80211com *ic)
179202ac6454SAndrew Thompson {
1793d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
17940698c0b3SAdrian Chadd 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1795645e4d17SHans Petter Selasky 
1796645e4d17SHans Petter Selasky 	RUM_LOCK(sc);
17977a79cebfSGleb Smirnoff 	if (sc->sc_detached) {
1798645e4d17SHans Petter Selasky 		RUM_UNLOCK(sc);
17997a79cebfSGleb Smirnoff 		return;
18007a79cebfSGleb Smirnoff 	}
180102ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
18020698c0b3SAdrian Chadd 
18030698c0b3SAdrian Chadd 	if (ic->ic_nrunning > 0) {
18040698c0b3SAdrian Chadd 		if (rum_init(sc) == 0)
180502ac6454SAndrew Thompson 			ieee80211_start_all(ic);
18060698c0b3SAdrian Chadd 		else
18070698c0b3SAdrian Chadd 			ieee80211_stop(vap);
18080698c0b3SAdrian Chadd 	} else
18090698c0b3SAdrian Chadd 		rum_stop(sc);
181002ac6454SAndrew Thompson }
181102ac6454SAndrew Thompson 
181202ac6454SAndrew Thompson static void
181302ac6454SAndrew Thompson rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
181402ac6454SAndrew Thompson {
1815760bc48eSAndrew Thompson 	struct usb_device_request req;
1816e0a69b51SAndrew Thompson 	usb_error_t error;
181702ac6454SAndrew Thompson 
181802ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
181902ac6454SAndrew Thompson 	req.bRequest = RT2573_READ_EEPROM;
182002ac6454SAndrew Thompson 	USETW(req.wValue, 0);
182102ac6454SAndrew Thompson 	USETW(req.wIndex, addr);
182202ac6454SAndrew Thompson 	USETW(req.wLength, len);
182302ac6454SAndrew Thompson 
182402ac6454SAndrew Thompson 	error = rum_do_request(sc, &req, buf);
182502ac6454SAndrew Thompson 	if (error != 0) {
182602ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1827a593f6b8SAndrew Thompson 		    usbd_errstr(error));
182802ac6454SAndrew Thompson 	}
182902ac6454SAndrew Thompson }
183002ac6454SAndrew Thompson 
183102ac6454SAndrew Thompson static uint32_t
183202ac6454SAndrew Thompson rum_read(struct rum_softc *sc, uint16_t reg)
183302ac6454SAndrew Thompson {
183402ac6454SAndrew Thompson 	uint32_t val;
183502ac6454SAndrew Thompson 
183602ac6454SAndrew Thompson 	rum_read_multi(sc, reg, &val, sizeof val);
183702ac6454SAndrew Thompson 
183802ac6454SAndrew Thompson 	return le32toh(val);
183902ac6454SAndrew Thompson }
184002ac6454SAndrew Thompson 
184102ac6454SAndrew Thompson static void
184202ac6454SAndrew Thompson rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
184302ac6454SAndrew Thompson {
1844760bc48eSAndrew Thompson 	struct usb_device_request req;
1845e0a69b51SAndrew Thompson 	usb_error_t error;
184602ac6454SAndrew Thompson 
184702ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
184802ac6454SAndrew Thompson 	req.bRequest = RT2573_READ_MULTI_MAC;
184902ac6454SAndrew Thompson 	USETW(req.wValue, 0);
185002ac6454SAndrew Thompson 	USETW(req.wIndex, reg);
185102ac6454SAndrew Thompson 	USETW(req.wLength, len);
185202ac6454SAndrew Thompson 
185302ac6454SAndrew Thompson 	error = rum_do_request(sc, &req, buf);
185402ac6454SAndrew Thompson 	if (error != 0) {
185502ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
185602ac6454SAndrew Thompson 		    "could not multi read MAC register: %s\n",
1857a593f6b8SAndrew Thompson 		    usbd_errstr(error));
185802ac6454SAndrew Thompson 	}
185902ac6454SAndrew Thompson }
186002ac6454SAndrew Thompson 
1861e0a69b51SAndrew Thompson static usb_error_t
186202ac6454SAndrew Thompson rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
186302ac6454SAndrew Thompson {
186402ac6454SAndrew Thompson 	uint32_t tmp = htole32(val);
186502ac6454SAndrew Thompson 
186677ddf3d3SAndrew Thompson 	return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
186702ac6454SAndrew Thompson }
186802ac6454SAndrew Thompson 
1869e0a69b51SAndrew Thompson static usb_error_t
187002ac6454SAndrew Thompson rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
187102ac6454SAndrew Thompson {
1872760bc48eSAndrew Thompson 	struct usb_device_request req;
1873e0a69b51SAndrew Thompson 	usb_error_t error;
18746d917491SHans Petter Selasky 	size_t offset;
187502ac6454SAndrew Thompson 
187602ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
187702ac6454SAndrew Thompson 	req.bRequest = RT2573_WRITE_MULTI_MAC;
187802ac6454SAndrew Thompson 	USETW(req.wValue, 0);
187902ac6454SAndrew Thompson 
188065d7e4d9SAdrian Chadd 	/* write at most 64 bytes at a time */
188165d7e4d9SAdrian Chadd 	for (offset = 0; offset < len; offset += 64) {
188265d7e4d9SAdrian Chadd 		USETW(req.wIndex, reg + offset);
188365d7e4d9SAdrian Chadd 		USETW(req.wLength, MIN(len - offset, 64));
188465d7e4d9SAdrian Chadd 
18858797cafeSBjoern A. Zeeb 		error = rum_do_request(sc, &req, (char *)buf + offset);
188602ac6454SAndrew Thompson 		if (error != 0) {
188702ac6454SAndrew Thompson 			device_printf(sc->sc_dev,
188802ac6454SAndrew Thompson 			    "could not multi write MAC register: %s\n",
1889a593f6b8SAndrew Thompson 			    usbd_errstr(error));
189077ddf3d3SAndrew Thompson 			return (error);
189102ac6454SAndrew Thompson 		}
189265d7e4d9SAdrian Chadd 	}
18938797cafeSBjoern A. Zeeb 
18948797cafeSBjoern A. Zeeb 	return (USB_ERR_NORMAL_COMPLETION);
189565d7e4d9SAdrian Chadd }
189602ac6454SAndrew Thompson 
1897a05022b2SAdrian Chadd static usb_error_t
1898a05022b2SAdrian Chadd rum_setbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1899a05022b2SAdrian Chadd {
1900a05022b2SAdrian Chadd 	return (rum_write(sc, reg, rum_read(sc, reg) | mask));
1901a05022b2SAdrian Chadd }
1902a05022b2SAdrian Chadd 
1903a05022b2SAdrian Chadd static usb_error_t
1904a05022b2SAdrian Chadd rum_clrbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1905a05022b2SAdrian Chadd {
1906a05022b2SAdrian Chadd 	return (rum_write(sc, reg, rum_read(sc, reg) & ~mask));
1907a05022b2SAdrian Chadd }
1908a05022b2SAdrian Chadd 
1909a05022b2SAdrian Chadd static usb_error_t
1910a05022b2SAdrian Chadd rum_modbits(struct rum_softc *sc, uint16_t reg, uint32_t set, uint32_t unset)
1911a05022b2SAdrian Chadd {
1912a05022b2SAdrian Chadd 	return (rum_write(sc, reg, (rum_read(sc, reg) & ~unset) | set));
1913a05022b2SAdrian Chadd }
1914a05022b2SAdrian Chadd 
1915cc9ae761SAdrian Chadd static int
1916cc9ae761SAdrian Chadd rum_bbp_busy(struct rum_softc *sc)
191702ac6454SAndrew Thompson {
191802ac6454SAndrew Thompson 	int ntries;
191902ac6454SAndrew Thompson 
192002ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
192102ac6454SAndrew Thompson 		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
192202ac6454SAndrew Thompson 			break;
192302ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
192402ac6454SAndrew Thompson 			break;
192502ac6454SAndrew Thompson 	}
1926cc9ae761SAdrian Chadd 	if (ntries == 100)
1927cc9ae761SAdrian Chadd 		return (ETIMEDOUT);
1928cc9ae761SAdrian Chadd 
1929cc9ae761SAdrian Chadd 	return (0);
1930cc9ae761SAdrian Chadd }
1931cc9ae761SAdrian Chadd 
1932cc9ae761SAdrian Chadd static void
1933cc9ae761SAdrian Chadd rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1934cc9ae761SAdrian Chadd {
1935cc9ae761SAdrian Chadd 	uint32_t tmp;
1936cc9ae761SAdrian Chadd 
1937cc9ae761SAdrian Chadd 	DPRINTFN(2, "reg=0x%08x\n", reg);
1938cc9ae761SAdrian Chadd 
1939cc9ae761SAdrian Chadd 	if (rum_bbp_busy(sc) != 0) {
194002ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not write to BBP\n");
194102ac6454SAndrew Thompson 		return;
194202ac6454SAndrew Thompson 	}
194302ac6454SAndrew Thompson 
194402ac6454SAndrew Thompson 	tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
194502ac6454SAndrew Thompson 	rum_write(sc, RT2573_PHY_CSR3, tmp);
194602ac6454SAndrew Thompson }
194702ac6454SAndrew Thompson 
194802ac6454SAndrew Thompson static uint8_t
194902ac6454SAndrew Thompson rum_bbp_read(struct rum_softc *sc, uint8_t reg)
195002ac6454SAndrew Thompson {
195102ac6454SAndrew Thompson 	uint32_t val;
195202ac6454SAndrew Thompson 	int ntries;
195302ac6454SAndrew Thompson 
195477ddf3d3SAndrew Thompson 	DPRINTFN(2, "reg=0x%08x\n", reg);
195577ddf3d3SAndrew Thompson 
1956cc9ae761SAdrian Chadd 	if (rum_bbp_busy(sc) != 0) {
195702ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read BBP\n");
195802ac6454SAndrew Thompson 		return 0;
195902ac6454SAndrew Thompson 	}
196002ac6454SAndrew Thompson 
196102ac6454SAndrew Thompson 	val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
196202ac6454SAndrew Thompson 	rum_write(sc, RT2573_PHY_CSR3, val);
196302ac6454SAndrew Thompson 
196402ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
196502ac6454SAndrew Thompson 		val = rum_read(sc, RT2573_PHY_CSR3);
196602ac6454SAndrew Thompson 		if (!(val & RT2573_BBP_BUSY))
196702ac6454SAndrew Thompson 			return val & 0xff;
196802ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
196902ac6454SAndrew Thompson 			break;
197002ac6454SAndrew Thompson 	}
197102ac6454SAndrew Thompson 
197202ac6454SAndrew Thompson 	device_printf(sc->sc_dev, "could not read BBP\n");
197302ac6454SAndrew Thompson 	return 0;
197402ac6454SAndrew Thompson }
197502ac6454SAndrew Thompson 
197602ac6454SAndrew Thompson static void
197702ac6454SAndrew Thompson rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
197802ac6454SAndrew Thompson {
197902ac6454SAndrew Thompson 	uint32_t tmp;
198002ac6454SAndrew Thompson 	int ntries;
198102ac6454SAndrew Thompson 
198202ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
198302ac6454SAndrew Thompson 		if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
198402ac6454SAndrew Thompson 			break;
198502ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
198602ac6454SAndrew Thompson 			break;
198702ac6454SAndrew Thompson 	}
198802ac6454SAndrew Thompson 	if (ntries == 100) {
198902ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not write to RF\n");
199002ac6454SAndrew Thompson 		return;
199102ac6454SAndrew Thompson 	}
199202ac6454SAndrew Thompson 
199367784314SPoul-Henning Kamp 	tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
199402ac6454SAndrew Thompson 	    (reg & 3);
199502ac6454SAndrew Thompson 	rum_write(sc, RT2573_PHY_CSR4, tmp);
199602ac6454SAndrew Thompson 
199702ac6454SAndrew Thompson 	/* remember last written value in sc */
199802ac6454SAndrew Thompson 	sc->rf_regs[reg] = val;
199902ac6454SAndrew Thompson 
200002ac6454SAndrew Thompson 	DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
200102ac6454SAndrew Thompson }
200202ac6454SAndrew Thompson 
200302ac6454SAndrew Thompson static void
200402ac6454SAndrew Thompson rum_select_antenna(struct rum_softc *sc)
200502ac6454SAndrew Thompson {
200602ac6454SAndrew Thompson 	uint8_t bbp4, bbp77;
200702ac6454SAndrew Thompson 	uint32_t tmp;
200802ac6454SAndrew Thompson 
200902ac6454SAndrew Thompson 	bbp4  = rum_bbp_read(sc, 4);
201002ac6454SAndrew Thompson 	bbp77 = rum_bbp_read(sc, 77);
201102ac6454SAndrew Thompson 
201202ac6454SAndrew Thompson 	/* TBD */
201302ac6454SAndrew Thompson 
201402ac6454SAndrew Thompson 	/* make sure Rx is disabled before switching antenna */
201502ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
201602ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
201702ac6454SAndrew Thompson 
201802ac6454SAndrew Thompson 	rum_bbp_write(sc,  4, bbp4);
201902ac6454SAndrew Thompson 	rum_bbp_write(sc, 77, bbp77);
202002ac6454SAndrew Thompson 
202102ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
202202ac6454SAndrew Thompson }
202302ac6454SAndrew Thompson 
202402ac6454SAndrew Thompson /*
202502ac6454SAndrew Thompson  * Enable multi-rate retries for frames sent at OFDM rates.
202602ac6454SAndrew Thompson  * In 802.11b/g mode, allow fallback to CCK rates.
202702ac6454SAndrew Thompson  */
202802ac6454SAndrew Thompson static void
202902ac6454SAndrew Thompson rum_enable_mrr(struct rum_softc *sc)
203002ac6454SAndrew Thompson {
20317a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
203202ac6454SAndrew Thompson 
2033a05022b2SAdrian Chadd 	if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
2034a05022b2SAdrian Chadd 		rum_setbits(sc, RT2573_TXRX_CSR4,
2035a05022b2SAdrian Chadd 		    RT2573_MRR_ENABLED | RT2573_MRR_CCK_FALLBACK);
2036a05022b2SAdrian Chadd 	} else {
2037a05022b2SAdrian Chadd 		rum_modbits(sc, RT2573_TXRX_CSR4,
2038a05022b2SAdrian Chadd 		    RT2573_MRR_ENABLED, RT2573_MRR_CCK_FALLBACK);
2039a05022b2SAdrian Chadd 	}
204002ac6454SAndrew Thompson }
204102ac6454SAndrew Thompson 
204202ac6454SAndrew Thompson static void
204302ac6454SAndrew Thompson rum_set_txpreamble(struct rum_softc *sc)
204402ac6454SAndrew Thompson {
20457a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
204602ac6454SAndrew Thompson 
204702ac6454SAndrew Thompson 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
2048a05022b2SAdrian Chadd 		rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
2049a05022b2SAdrian Chadd 	else
2050a05022b2SAdrian Chadd 		rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
205102ac6454SAndrew Thompson }
205202ac6454SAndrew Thompson 
205302ac6454SAndrew Thompson static void
205402ac6454SAndrew Thompson rum_set_basicrates(struct rum_softc *sc)
205502ac6454SAndrew Thompson {
20567a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
205702ac6454SAndrew Thompson 
205802ac6454SAndrew Thompson 	/* update basic rate set */
205902ac6454SAndrew Thompson 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
206002ac6454SAndrew Thompson 		/* 11b basic rates: 1, 2Mbps */
206102ac6454SAndrew Thompson 		rum_write(sc, RT2573_TXRX_CSR5, 0x3);
206202ac6454SAndrew Thompson 	} else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
206302ac6454SAndrew Thompson 		/* 11a basic rates: 6, 12, 24Mbps */
206402ac6454SAndrew Thompson 		rum_write(sc, RT2573_TXRX_CSR5, 0x150);
206502ac6454SAndrew Thompson 	} else {
206602ac6454SAndrew Thompson 		/* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
206702ac6454SAndrew Thompson 		rum_write(sc, RT2573_TXRX_CSR5, 0xf);
206802ac6454SAndrew Thompson 	}
206902ac6454SAndrew Thompson }
207002ac6454SAndrew Thompson 
207102ac6454SAndrew Thompson /*
207202ac6454SAndrew Thompson  * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
207302ac6454SAndrew Thompson  * driver.
207402ac6454SAndrew Thompson  */
207502ac6454SAndrew Thompson static void
207602ac6454SAndrew Thompson rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
207702ac6454SAndrew Thompson {
207802ac6454SAndrew Thompson 	uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
207902ac6454SAndrew Thompson 
208002ac6454SAndrew Thompson 	/* update all BBP registers that depend on the band */
208102ac6454SAndrew Thompson 	bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
208202ac6454SAndrew Thompson 	bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
208302ac6454SAndrew Thompson 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
208402ac6454SAndrew Thompson 		bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
208502ac6454SAndrew Thompson 		bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
208602ac6454SAndrew Thompson 	}
208702ac6454SAndrew Thompson 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
208802ac6454SAndrew Thompson 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
208902ac6454SAndrew Thompson 		bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
209002ac6454SAndrew Thompson 	}
209102ac6454SAndrew Thompson 
209202ac6454SAndrew Thompson 	sc->bbp17 = bbp17;
209302ac6454SAndrew Thompson 	rum_bbp_write(sc,  17, bbp17);
209402ac6454SAndrew Thompson 	rum_bbp_write(sc,  96, bbp96);
209502ac6454SAndrew Thompson 	rum_bbp_write(sc, 104, bbp104);
209602ac6454SAndrew Thompson 
209702ac6454SAndrew Thompson 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
209802ac6454SAndrew Thompson 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
209902ac6454SAndrew Thompson 		rum_bbp_write(sc, 75, 0x80);
210002ac6454SAndrew Thompson 		rum_bbp_write(sc, 86, 0x80);
210102ac6454SAndrew Thompson 		rum_bbp_write(sc, 88, 0x80);
210202ac6454SAndrew Thompson 	}
210302ac6454SAndrew Thompson 
210402ac6454SAndrew Thompson 	rum_bbp_write(sc, 35, bbp35);
210502ac6454SAndrew Thompson 	rum_bbp_write(sc, 97, bbp97);
210602ac6454SAndrew Thompson 	rum_bbp_write(sc, 98, bbp98);
210702ac6454SAndrew Thompson 
2108a05022b2SAdrian Chadd 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
2109a05022b2SAdrian Chadd 		rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_2GHZ,
2110a05022b2SAdrian Chadd 		    RT2573_PA_PE_5GHZ);
2111a05022b2SAdrian Chadd 	} else {
2112a05022b2SAdrian Chadd 		rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_5GHZ,
2113a05022b2SAdrian Chadd 		    RT2573_PA_PE_2GHZ);
2114a05022b2SAdrian Chadd 	}
211502ac6454SAndrew Thompson }
211602ac6454SAndrew Thompson 
211702ac6454SAndrew Thompson static void
211802ac6454SAndrew Thompson rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
211902ac6454SAndrew Thompson {
21207a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
212102ac6454SAndrew Thompson 	const struct rfprog *rfprog;
212202ac6454SAndrew Thompson 	uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
212302ac6454SAndrew Thompson 	int8_t power;
212402ac6454SAndrew Thompson 	int i, chan;
212502ac6454SAndrew Thompson 
212602ac6454SAndrew Thompson 	chan = ieee80211_chan2ieee(ic, c);
212702ac6454SAndrew Thompson 	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
212802ac6454SAndrew Thompson 		return;
212902ac6454SAndrew Thompson 
213002ac6454SAndrew Thompson 	/* select the appropriate RF settings based on what EEPROM says */
213102ac6454SAndrew Thompson 	rfprog = (sc->rf_rev == RT2573_RF_5225 ||
213202ac6454SAndrew Thompson 		  sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
213302ac6454SAndrew Thompson 
213402ac6454SAndrew Thompson 	/* find the settings for this channel (we know it exists) */
213502ac6454SAndrew Thompson 	for (i = 0; rfprog[i].chan != chan; i++);
213602ac6454SAndrew Thompson 
213702ac6454SAndrew Thompson 	power = sc->txpow[i];
213802ac6454SAndrew Thompson 	if (power < 0) {
213902ac6454SAndrew Thompson 		bbp94 += power;
214002ac6454SAndrew Thompson 		power = 0;
214102ac6454SAndrew Thompson 	} else if (power > 31) {
214202ac6454SAndrew Thompson 		bbp94 += power - 31;
214302ac6454SAndrew Thompson 		power = 31;
214402ac6454SAndrew Thompson 	}
214502ac6454SAndrew Thompson 
214602ac6454SAndrew Thompson 	/*
214702ac6454SAndrew Thompson 	 * If we are switching from the 2GHz band to the 5GHz band or
214802ac6454SAndrew Thompson 	 * vice-versa, BBP registers need to be reprogrammed.
214902ac6454SAndrew Thompson 	 */
215002ac6454SAndrew Thompson 	if (c->ic_flags != ic->ic_curchan->ic_flags) {
215102ac6454SAndrew Thompson 		rum_select_band(sc, c);
215202ac6454SAndrew Thompson 		rum_select_antenna(sc);
215302ac6454SAndrew Thompson 	}
215402ac6454SAndrew Thompson 	ic->ic_curchan = c;
215502ac6454SAndrew Thompson 
215602ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
215702ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
215802ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
215902ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
216002ac6454SAndrew Thompson 
216102ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
216202ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
216302ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
216402ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
216502ac6454SAndrew Thompson 
216602ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
216702ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
216802ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
216902ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
217002ac6454SAndrew Thompson 
217102ac6454SAndrew Thompson 	rum_pause(sc, hz / 100);
217202ac6454SAndrew Thompson 
217302ac6454SAndrew Thompson 	/* enable smart mode for MIMO-capable RFs */
217402ac6454SAndrew Thompson 	bbp3 = rum_bbp_read(sc, 3);
217502ac6454SAndrew Thompson 
217602ac6454SAndrew Thompson 	bbp3 &= ~RT2573_SMART_MODE;
217702ac6454SAndrew Thompson 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
217802ac6454SAndrew Thompson 		bbp3 |= RT2573_SMART_MODE;
217902ac6454SAndrew Thompson 
218002ac6454SAndrew Thompson 	rum_bbp_write(sc, 3, bbp3);
218102ac6454SAndrew Thompson 
218202ac6454SAndrew Thompson 	if (bbp94 != RT2573_BBPR94_DEFAULT)
218302ac6454SAndrew Thompson 		rum_bbp_write(sc, 94, bbp94);
218477ddf3d3SAndrew Thompson 
218577ddf3d3SAndrew Thompson 	/* give the chip some extra time to do the switchover */
218677ddf3d3SAndrew Thompson 	rum_pause(sc, hz / 100);
218702ac6454SAndrew Thompson }
218802ac6454SAndrew Thompson 
2189342ced03SAdrian Chadd static void
2190342ced03SAdrian Chadd rum_set_maxretry(struct rum_softc *sc, struct ieee80211vap *vap)
2191342ced03SAdrian Chadd {
2192342ced03SAdrian Chadd 	const struct ieee80211_txparam *tp;
2193342ced03SAdrian Chadd 	struct ieee80211_node *ni = vap->iv_bss;
2194342ced03SAdrian Chadd 	struct rum_vap *rvp = RUM_VAP(vap);
2195342ced03SAdrian Chadd 
2196342ced03SAdrian Chadd 	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
2197342ced03SAdrian Chadd 	rvp->maxretry = tp->maxretry < 0xf ? tp->maxretry : 0xf;
2198342ced03SAdrian Chadd 
2199342ced03SAdrian Chadd 	rum_modbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_RETRY(rvp->maxretry) |
2200342ced03SAdrian Chadd 	    RT2573_LONG_RETRY(rvp->maxretry),
2201342ced03SAdrian Chadd 	    RT2573_SHORT_RETRY_MASK | RT2573_LONG_RETRY_MASK);
2202342ced03SAdrian Chadd }
2203342ced03SAdrian Chadd 
220402ac6454SAndrew Thompson /*
220502ac6454SAndrew Thompson  * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
220602ac6454SAndrew Thompson  * and HostAP operating modes.
220702ac6454SAndrew Thompson  */
2208aa81f853SAdrian Chadd static int
220902ac6454SAndrew Thompson rum_enable_tsf_sync(struct rum_softc *sc)
221002ac6454SAndrew Thompson {
22117a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
221202ac6454SAndrew Thompson 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
221302ac6454SAndrew Thompson 	uint32_t tmp;
2214349156a9SAndriy Voskoboinyk 	uint16_t bintval;
221502ac6454SAndrew Thompson 
221602ac6454SAndrew Thompson 	if (vap->iv_opmode != IEEE80211_M_STA) {
221702ac6454SAndrew Thompson 		/*
221802ac6454SAndrew Thompson 		 * Change default 16ms TBTT adjustment to 8ms.
221902ac6454SAndrew Thompson 		 * Must be done before enabling beacon generation.
222002ac6454SAndrew Thompson 		 */
2221aa81f853SAdrian Chadd 		if (rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8) != 0)
2222aa81f853SAdrian Chadd 			return EIO;
222302ac6454SAndrew Thompson 	}
222402ac6454SAndrew Thompson 
222502ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
222602ac6454SAndrew Thompson 
222702ac6454SAndrew Thompson 	/* set beacon interval (in 1/16ms unit) */
2228349156a9SAndriy Voskoboinyk 	bintval = vap->iv_bss->ni_intval;
2229349156a9SAndriy Voskoboinyk 	tmp |= bintval * 16;
2230d847071cSAdrian Chadd 	tmp |= RT2573_TSF_TIMER_EN | RT2573_TBTT_TIMER_EN;
223102ac6454SAndrew Thompson 
2232d847071cSAdrian Chadd 	switch (vap->iv_opmode) {
2233d847071cSAdrian Chadd 	case IEEE80211_M_STA:
2234d847071cSAdrian Chadd 		/*
2235d847071cSAdrian Chadd 		 * Local TSF is always updated with remote TSF on beacon
2236d847071cSAdrian Chadd 		 * reception.
2237d847071cSAdrian Chadd 		 */
2238d847071cSAdrian Chadd 		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_STA);
2239d847071cSAdrian Chadd 		break;
2240d847071cSAdrian Chadd 	case IEEE80211_M_IBSS:
2241d847071cSAdrian Chadd 		/*
2242d847071cSAdrian Chadd 		 * Local TSF is updated with remote TSF on beacon reception
2243d847071cSAdrian Chadd 		 * only if the remote TSF is greater than local TSF.
2244d847071cSAdrian Chadd 		 */
2245d847071cSAdrian Chadd 		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_IBSS);
2246d847071cSAdrian Chadd 		tmp |= RT2573_BCN_TX_EN;
2247d847071cSAdrian Chadd 		break;
2248d847071cSAdrian Chadd 	case IEEE80211_M_HOSTAP:
2249d847071cSAdrian Chadd 		/* SYNC with nobody */
2250d847071cSAdrian Chadd 		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_HOSTAP);
2251d847071cSAdrian Chadd 		tmp |= RT2573_BCN_TX_EN;
2252d847071cSAdrian Chadd 		break;
2253d847071cSAdrian Chadd 	default:
2254d847071cSAdrian Chadd 		device_printf(sc->sc_dev,
2255d847071cSAdrian Chadd 		    "Enabling TSF failed. undefined opmode %d\n",
2256d847071cSAdrian Chadd 		    vap->iv_opmode);
2257aa81f853SAdrian Chadd 		return EINVAL;
2258d847071cSAdrian Chadd 	}
225902ac6454SAndrew Thompson 
2260aa81f853SAdrian Chadd 	if (rum_write(sc, RT2573_TXRX_CSR9, tmp) != 0)
2261aa81f853SAdrian Chadd 		return EIO;
2262aa81f853SAdrian Chadd 
2263349156a9SAndriy Voskoboinyk 	/* refresh current sleep time */
2264349156a9SAndriy Voskoboinyk 	return (rum_set_sleep_time(sc, bintval));
226502ac6454SAndrew Thompson }
226602ac6454SAndrew Thompson 
226702ac6454SAndrew Thompson static void
22685463c4a4SSam Leffler rum_enable_tsf(struct rum_softc *sc)
22695463c4a4SSam Leffler {
2270d847071cSAdrian Chadd 	rum_modbits(sc, RT2573_TXRX_CSR9, RT2573_TSF_TIMER_EN |
2271d847071cSAdrian Chadd 	    RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_DIS), 0x00ffffff);
22725463c4a4SSam Leffler }
22735463c4a4SSam Leffler 
22745463c4a4SSam Leffler static void
22756d804321SAdrian Chadd rum_abort_tsf_sync(struct rum_softc *sc)
22766d804321SAdrian Chadd {
2277a05022b2SAdrian Chadd 	rum_clrbits(sc, RT2573_TXRX_CSR9, 0x00ffffff);
22786d804321SAdrian Chadd }
22796d804321SAdrian Chadd 
22806d804321SAdrian Chadd static void
2281a6ccd477SAdrian Chadd rum_get_tsf(struct rum_softc *sc, uint64_t *buf)
2282a6ccd477SAdrian Chadd {
2283a6ccd477SAdrian Chadd 	rum_read_multi(sc, RT2573_TXRX_CSR12, buf, sizeof (*buf));
2284a6ccd477SAdrian Chadd }
2285a6ccd477SAdrian Chadd 
2286a6ccd477SAdrian Chadd static void
2287ed5711a1SAdrian Chadd rum_update_slot_cb(struct rum_softc *sc, union sec_param *data, uint8_t rvp_id)
228802ac6454SAndrew Thompson {
22897a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
229002ac6454SAndrew Thompson 	uint8_t slottime;
229102ac6454SAndrew Thompson 
2292bdfff33fSAndriy Voskoboinyk 	slottime = IEEE80211_GET_SLOTTIME(ic);
229302ac6454SAndrew Thompson 
2294a05022b2SAdrian Chadd 	rum_modbits(sc, RT2573_MAC_CSR9, slottime, 0xff);
229502ac6454SAndrew Thompson 
229602ac6454SAndrew Thompson 	DPRINTF("setting slot time to %uus\n", slottime);
229702ac6454SAndrew Thompson }
229802ac6454SAndrew Thompson 
229902ac6454SAndrew Thompson static void
2300a19dbd8dSAdrian Chadd rum_update_slot(struct ieee80211com *ic)
2301a19dbd8dSAdrian Chadd {
2302ed5711a1SAdrian Chadd 	rum_cmd_sleepable(ic->ic_softc, NULL, 0, 0, rum_update_slot_cb);
2303a19dbd8dSAdrian Chadd }
2304a19dbd8dSAdrian Chadd 
2305a14954c5SAndriy Voskoboinyk static int
2306a14954c5SAndriy Voskoboinyk rum_wme_update(struct ieee80211com *ic)
23071354b52cSAdrian Chadd {
23087682e597SAdrian Chadd 	const struct wmeParams *chanp =
23097682e597SAdrian Chadd 	    ic->ic_wme.wme_chanParams.cap_wmeParams;
2310a14954c5SAndriy Voskoboinyk 	struct rum_softc *sc = ic->ic_softc;
23111354b52cSAdrian Chadd 	int error = 0;
23121354b52cSAdrian Chadd 
2313a14954c5SAndriy Voskoboinyk 	RUM_LOCK(sc);
23141354b52cSAdrian Chadd 	error = rum_write(sc, RT2573_AIFSN_CSR,
23157682e597SAdrian Chadd 	    chanp[WME_AC_VO].wmep_aifsn  << 12 |
23167682e597SAdrian Chadd 	    chanp[WME_AC_VI].wmep_aifsn  <<  8 |
23177682e597SAdrian Chadd 	    chanp[WME_AC_BK].wmep_aifsn  <<  4 |
23187682e597SAdrian Chadd 	    chanp[WME_AC_BE].wmep_aifsn);
23191354b52cSAdrian Chadd 	if (error)
23201354b52cSAdrian Chadd 		goto print_err;
23211354b52cSAdrian Chadd 	error = rum_write(sc, RT2573_CWMIN_CSR,
23227682e597SAdrian Chadd 	    chanp[WME_AC_VO].wmep_logcwmin << 12 |
23237682e597SAdrian Chadd 	    chanp[WME_AC_VI].wmep_logcwmin <<  8 |
23247682e597SAdrian Chadd 	    chanp[WME_AC_BK].wmep_logcwmin <<  4 |
23257682e597SAdrian Chadd 	    chanp[WME_AC_BE].wmep_logcwmin);
23261354b52cSAdrian Chadd 	if (error)
23271354b52cSAdrian Chadd 		goto print_err;
23281354b52cSAdrian Chadd 	error = rum_write(sc, RT2573_CWMAX_CSR,
23297682e597SAdrian Chadd 	    chanp[WME_AC_VO].wmep_logcwmax << 12 |
23307682e597SAdrian Chadd 	    chanp[WME_AC_VI].wmep_logcwmax <<  8 |
23317682e597SAdrian Chadd 	    chanp[WME_AC_BK].wmep_logcwmax <<  4 |
23327682e597SAdrian Chadd 	    chanp[WME_AC_BE].wmep_logcwmax);
23331354b52cSAdrian Chadd 	if (error)
23341354b52cSAdrian Chadd 		goto print_err;
23351354b52cSAdrian Chadd 	error = rum_write(sc, RT2573_TXOP01_CSR,
23367682e597SAdrian Chadd 	    chanp[WME_AC_BK].wmep_txopLimit << 16 |
23377682e597SAdrian Chadd 	    chanp[WME_AC_BE].wmep_txopLimit);
23381354b52cSAdrian Chadd 	if (error)
23391354b52cSAdrian Chadd 		goto print_err;
23401354b52cSAdrian Chadd 	error = rum_write(sc, RT2573_TXOP23_CSR,
23417682e597SAdrian Chadd 	    chanp[WME_AC_VO].wmep_txopLimit << 16 |
23427682e597SAdrian Chadd 	    chanp[WME_AC_VI].wmep_txopLimit);
23431354b52cSAdrian Chadd 	if (error)
23441354b52cSAdrian Chadd 		goto print_err;
23451354b52cSAdrian Chadd 
23461354b52cSAdrian Chadd 	memcpy(sc->wme_params, chanp, sizeof(*chanp) * WME_NUM_AC);
23471354b52cSAdrian Chadd 
23481354b52cSAdrian Chadd print_err:
2349a14954c5SAndriy Voskoboinyk 	RUM_UNLOCK(sc);
2350a14954c5SAndriy Voskoboinyk 	if (error != 0) {
23511354b52cSAdrian Chadd 		device_printf(sc->sc_dev, "%s: WME update failed, error %d\n",
23521354b52cSAdrian Chadd 		    __func__, error);
23531354b52cSAdrian Chadd 	}
23541354b52cSAdrian Chadd 
2355a14954c5SAndriy Voskoboinyk 	return (error);
23561354b52cSAdrian Chadd }
23571354b52cSAdrian Chadd 
23581354b52cSAdrian Chadd static void
235902ac6454SAndrew Thompson rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
236002ac6454SAndrew Thompson {
236102ac6454SAndrew Thompson 
236242769826SAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR4,
236342769826SAdrian Chadd 	    bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
236442769826SAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR5,
236542769826SAdrian Chadd 	    bssid[4] | bssid[5] << 8 | RT2573_NUM_BSSID_MSK(1));
236602ac6454SAndrew Thompson }
236702ac6454SAndrew Thompson 
236802ac6454SAndrew Thompson static void
236902ac6454SAndrew Thompson rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
237002ac6454SAndrew Thompson {
237102ac6454SAndrew Thompson 
237242769826SAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR2,
237342769826SAdrian Chadd 	    addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
237442769826SAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR3,
237542769826SAdrian Chadd 	    addr[4] | addr[5] << 8 | 0xff << 16);
237602ac6454SAndrew Thompson }
237702ac6454SAndrew Thompson 
237802ac6454SAndrew Thompson static void
23795efea30fSAndrew Thompson rum_setpromisc(struct rum_softc *sc)
238002ac6454SAndrew Thompson {
2381a05022b2SAdrian Chadd 	struct ieee80211com *ic = &sc->sc_ic;
238202ac6454SAndrew Thompson 
2383a05022b2SAdrian Chadd 	if (ic->ic_promisc == 0)
2384a05022b2SAdrian Chadd 		rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2385a05022b2SAdrian Chadd 	else
2386a05022b2SAdrian Chadd 		rum_clrbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
238702ac6454SAndrew Thompson 
2388a05022b2SAdrian Chadd 	DPRINTF("%s promiscuous mode\n", ic->ic_promisc > 0 ?
238902ac6454SAndrew Thompson 	    "entering" : "leaving");
239002ac6454SAndrew Thompson }
239102ac6454SAndrew Thompson 
239277ddf3d3SAndrew Thompson static void
2393272f6adeSGleb Smirnoff rum_update_promisc(struct ieee80211com *ic)
239477ddf3d3SAndrew Thompson {
2395272f6adeSGleb Smirnoff 	struct rum_softc *sc = ic->ic_softc;
239677ddf3d3SAndrew Thompson 
2397ba2c1fbcSAdrian Chadd 	RUM_LOCK(sc);
239842769826SAdrian Chadd 	if (sc->sc_running)
23995efea30fSAndrew Thompson 		rum_setpromisc(sc);
240077ddf3d3SAndrew Thompson 	RUM_UNLOCK(sc);
240177ddf3d3SAndrew Thompson }
240277ddf3d3SAndrew Thompson 
24036dd8e071SEd Maste static void
2404272f6adeSGleb Smirnoff rum_update_mcast(struct ieee80211com *ic)
24056dd8e071SEd Maste {
2406e7b7db3dSAdrian Chadd 	/* Ignore. */
24076dd8e071SEd Maste }
24086dd8e071SEd Maste 
240902ac6454SAndrew Thompson static const char *
241002ac6454SAndrew Thompson rum_get_rf(int rev)
241102ac6454SAndrew Thompson {
241202ac6454SAndrew Thompson 	switch (rev) {
241302ac6454SAndrew Thompson 	case RT2573_RF_2527:	return "RT2527 (MIMO XR)";
241402ac6454SAndrew Thompson 	case RT2573_RF_2528:	return "RT2528";
241502ac6454SAndrew Thompson 	case RT2573_RF_5225:	return "RT5225 (MIMO XR)";
241602ac6454SAndrew Thompson 	case RT2573_RF_5226:	return "RT5226";
241702ac6454SAndrew Thompson 	default:		return "unknown";
241802ac6454SAndrew Thompson 	}
241902ac6454SAndrew Thompson }
242002ac6454SAndrew Thompson 
242102ac6454SAndrew Thompson static void
242202ac6454SAndrew Thompson rum_read_eeprom(struct rum_softc *sc)
242302ac6454SAndrew Thompson {
242402ac6454SAndrew Thompson 	uint16_t val;
242502ac6454SAndrew Thompson #ifdef RUM_DEBUG
242602ac6454SAndrew Thompson 	int i;
242702ac6454SAndrew Thompson #endif
242802ac6454SAndrew Thompson 
242902ac6454SAndrew Thompson 	/* read MAC address */
24307a79cebfSGleb Smirnoff 	rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_ic.ic_macaddr, 6);
243102ac6454SAndrew Thompson 
243202ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
243302ac6454SAndrew Thompson 	val = le16toh(val);
243402ac6454SAndrew Thompson 	sc->rf_rev =   (val >> 11) & 0x1f;
243502ac6454SAndrew Thompson 	sc->hw_radio = (val >> 10) & 0x1;
243602ac6454SAndrew Thompson 	sc->rx_ant =   (val >> 4)  & 0x3;
243702ac6454SAndrew Thompson 	sc->tx_ant =   (val >> 2)  & 0x3;
243802ac6454SAndrew Thompson 	sc->nb_ant =   val & 0x3;
243902ac6454SAndrew Thompson 
244002ac6454SAndrew Thompson 	DPRINTF("RF revision=%d\n", sc->rf_rev);
244102ac6454SAndrew Thompson 
244202ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
244302ac6454SAndrew Thompson 	val = le16toh(val);
244402ac6454SAndrew Thompson 	sc->ext_5ghz_lna = (val >> 6) & 0x1;
244502ac6454SAndrew Thompson 	sc->ext_2ghz_lna = (val >> 4) & 0x1;
244602ac6454SAndrew Thompson 
244702ac6454SAndrew Thompson 	DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
244802ac6454SAndrew Thompson 	    sc->ext_2ghz_lna, sc->ext_5ghz_lna);
244902ac6454SAndrew Thompson 
245002ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
245102ac6454SAndrew Thompson 	val = le16toh(val);
245202ac6454SAndrew Thompson 	if ((val & 0xff) != 0xff)
245302ac6454SAndrew Thompson 		sc->rssi_2ghz_corr = (int8_t)(val & 0xff);	/* signed */
245402ac6454SAndrew Thompson 
245502ac6454SAndrew Thompson 	/* Only [-10, 10] is valid */
245602ac6454SAndrew Thompson 	if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
245702ac6454SAndrew Thompson 		sc->rssi_2ghz_corr = 0;
245802ac6454SAndrew Thompson 
245902ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
246002ac6454SAndrew Thompson 	val = le16toh(val);
246102ac6454SAndrew Thompson 	if ((val & 0xff) != 0xff)
246202ac6454SAndrew Thompson 		sc->rssi_5ghz_corr = (int8_t)(val & 0xff);	/* signed */
246302ac6454SAndrew Thompson 
246402ac6454SAndrew Thompson 	/* Only [-10, 10] is valid */
246502ac6454SAndrew Thompson 	if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
246602ac6454SAndrew Thompson 		sc->rssi_5ghz_corr = 0;
246702ac6454SAndrew Thompson 
246802ac6454SAndrew Thompson 	if (sc->ext_2ghz_lna)
246902ac6454SAndrew Thompson 		sc->rssi_2ghz_corr -= 14;
247002ac6454SAndrew Thompson 	if (sc->ext_5ghz_lna)
247102ac6454SAndrew Thompson 		sc->rssi_5ghz_corr -= 14;
247202ac6454SAndrew Thompson 
247302ac6454SAndrew Thompson 	DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
247402ac6454SAndrew Thompson 	    sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
247502ac6454SAndrew Thompson 
247602ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
247702ac6454SAndrew Thompson 	val = le16toh(val);
247802ac6454SAndrew Thompson 	if ((val & 0xff) != 0xff)
247902ac6454SAndrew Thompson 		sc->rffreq = val & 0xff;
248002ac6454SAndrew Thompson 
248102ac6454SAndrew Thompson 	DPRINTF("RF freq=%d\n", sc->rffreq);
248202ac6454SAndrew Thompson 
248302ac6454SAndrew Thompson 	/* read Tx power for all a/b/g channels */
248402ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
248502ac6454SAndrew Thompson 	/* XXX default Tx power for 802.11a channels */
248602ac6454SAndrew Thompson 	memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
248702ac6454SAndrew Thompson #ifdef RUM_DEBUG
248802ac6454SAndrew Thompson 	for (i = 0; i < 14; i++)
248902ac6454SAndrew Thompson 		DPRINTF("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]);
249002ac6454SAndrew Thompson #endif
249102ac6454SAndrew Thompson 
249202ac6454SAndrew Thompson 	/* read default values for BBP registers */
249302ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
249402ac6454SAndrew Thompson #ifdef RUM_DEBUG
249502ac6454SAndrew Thompson 	for (i = 0; i < 14; i++) {
249602ac6454SAndrew Thompson 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
249702ac6454SAndrew Thompson 			continue;
249802ac6454SAndrew Thompson 		DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
249902ac6454SAndrew Thompson 		    sc->bbp_prom[i].val);
250002ac6454SAndrew Thompson 	}
250102ac6454SAndrew Thompson #endif
250202ac6454SAndrew Thompson }
250302ac6454SAndrew Thompson 
250402ac6454SAndrew Thompson static int
25056095f7caSAdrian Chadd rum_bbp_wakeup(struct rum_softc *sc)
25066095f7caSAdrian Chadd {
25076095f7caSAdrian Chadd 	unsigned int ntries;
25086095f7caSAdrian Chadd 
25096095f7caSAdrian Chadd 	for (ntries = 0; ntries < 100; ntries++) {
25106095f7caSAdrian Chadd 		if (rum_read(sc, RT2573_MAC_CSR12) & 8)
25116095f7caSAdrian Chadd 			break;
25126095f7caSAdrian Chadd 		rum_write(sc, RT2573_MAC_CSR12, 4);	/* force wakeup */
25136095f7caSAdrian Chadd 		if (rum_pause(sc, hz / 100))
25146095f7caSAdrian Chadd 			break;
25156095f7caSAdrian Chadd 	}
25166095f7caSAdrian Chadd 	if (ntries == 100) {
25176095f7caSAdrian Chadd 		device_printf(sc->sc_dev,
25186095f7caSAdrian Chadd 		    "timeout waiting for BBP/RF to wakeup\n");
25196095f7caSAdrian Chadd 		return (ETIMEDOUT);
25206095f7caSAdrian Chadd 	}
25216095f7caSAdrian Chadd 
25226095f7caSAdrian Chadd 	return (0);
25236095f7caSAdrian Chadd }
25246095f7caSAdrian Chadd 
25256095f7caSAdrian Chadd static int
252602ac6454SAndrew Thompson rum_bbp_init(struct rum_softc *sc)
252702ac6454SAndrew Thompson {
252802ac6454SAndrew Thompson 	int i, ntries;
252902ac6454SAndrew Thompson 
253002ac6454SAndrew Thompson 	/* wait for BBP to be ready */
253102ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
253202ac6454SAndrew Thompson 		const uint8_t val = rum_bbp_read(sc, 0);
253302ac6454SAndrew Thompson 		if (val != 0 && val != 0xff)
253402ac6454SAndrew Thompson 			break;
253502ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
253602ac6454SAndrew Thompson 			break;
253702ac6454SAndrew Thompson 	}
253802ac6454SAndrew Thompson 	if (ntries == 100) {
253902ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "timeout waiting for BBP\n");
254002ac6454SAndrew Thompson 		return EIO;
254102ac6454SAndrew Thompson 	}
254202ac6454SAndrew Thompson 
254302ac6454SAndrew Thompson 	/* initialize BBP registers to default values */
2544e7b7db3dSAdrian Chadd 	for (i = 0; i < nitems(rum_def_bbp); i++)
254502ac6454SAndrew Thompson 		rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
254602ac6454SAndrew Thompson 
254702ac6454SAndrew Thompson 	/* write vendor-specific BBP values (from EEPROM) */
254802ac6454SAndrew Thompson 	for (i = 0; i < 16; i++) {
254902ac6454SAndrew Thompson 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
255002ac6454SAndrew Thompson 			continue;
255102ac6454SAndrew Thompson 		rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
255202ac6454SAndrew Thompson 	}
255302ac6454SAndrew Thompson 
255402ac6454SAndrew Thompson 	return 0;
255502ac6454SAndrew Thompson }
255602ac6454SAndrew Thompson 
25572cb9ef8dSAdrian Chadd static void
25582cb9ef8dSAdrian Chadd rum_clr_shkey_regs(struct rum_softc *sc)
25592cb9ef8dSAdrian Chadd {
25602cb9ef8dSAdrian Chadd 	rum_write(sc, RT2573_SEC_CSR0, 0);
25612cb9ef8dSAdrian Chadd 	rum_write(sc, RT2573_SEC_CSR1, 0);
25622cb9ef8dSAdrian Chadd 	rum_write(sc, RT2573_SEC_CSR5, 0);
25632cb9ef8dSAdrian Chadd }
25642cb9ef8dSAdrian Chadd 
25650698c0b3SAdrian Chadd static int
25667a79cebfSGleb Smirnoff rum_init(struct rum_softc *sc)
256702ac6454SAndrew Thompson {
25687a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
25697a79cebfSGleb Smirnoff 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
257002ac6454SAndrew Thompson 	uint32_t tmp;
25716095f7caSAdrian Chadd 	int i, ret;
257202ac6454SAndrew Thompson 
25730698c0b3SAdrian Chadd 	RUM_LOCK(sc);
25740698c0b3SAdrian Chadd 	if (sc->sc_running) {
25750698c0b3SAdrian Chadd 		ret = 0;
25760698c0b3SAdrian Chadd 		goto end;
25770698c0b3SAdrian Chadd 	}
257802ac6454SAndrew Thompson 
257902ac6454SAndrew Thompson 	/* initialize MAC registers to default values */
2580e7b7db3dSAdrian Chadd 	for (i = 0; i < nitems(rum_def_mac); i++)
258102ac6454SAndrew Thompson 		rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
258202ac6454SAndrew Thompson 
25831354b52cSAdrian Chadd 	/* reset some WME parameters to default values */
25841354b52cSAdrian Chadd 	sc->wme_params[0].wmep_aifsn = 2;
25851354b52cSAdrian Chadd 	sc->wme_params[0].wmep_logcwmin = 4;
25861354b52cSAdrian Chadd 	sc->wme_params[0].wmep_logcwmax = 10;
25871354b52cSAdrian Chadd 
258802ac6454SAndrew Thompson 	/* set host ready */
2589e7b7db3dSAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
259002ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR1, 0);
259102ac6454SAndrew Thompson 
259202ac6454SAndrew Thompson 	/* wait for BBP/RF to wakeup */
25936095f7caSAdrian Chadd 	if ((ret = rum_bbp_wakeup(sc)) != 0)
25940698c0b3SAdrian Chadd 		goto end;
259502ac6454SAndrew Thompson 
25960698c0b3SAdrian Chadd 	if ((ret = rum_bbp_init(sc)) != 0)
25970698c0b3SAdrian Chadd 		goto end;
259802ac6454SAndrew Thompson 
259902ac6454SAndrew Thompson 	/* select default channel */
260002ac6454SAndrew Thompson 	rum_select_band(sc, ic->ic_curchan);
260102ac6454SAndrew Thompson 	rum_select_antenna(sc);
260202ac6454SAndrew Thompson 	rum_set_chan(sc, ic->ic_curchan);
260302ac6454SAndrew Thompson 
260402ac6454SAndrew Thompson 	/* clear STA registers */
260502ac6454SAndrew Thompson 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
260602ac6454SAndrew Thompson 
26072cb9ef8dSAdrian Chadd 	/* clear security registers (if required) */
26082cb9ef8dSAdrian Chadd 	if (sc->sc_clr_shkeys == 0) {
26092cb9ef8dSAdrian Chadd 		rum_clr_shkey_regs(sc);
26102cb9ef8dSAdrian Chadd 		sc->sc_clr_shkeys = 1;
26112cb9ef8dSAdrian Chadd 	}
26122cb9ef8dSAdrian Chadd 
26137a79cebfSGleb Smirnoff 	rum_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
261402ac6454SAndrew Thompson 
261502ac6454SAndrew Thompson 	/* initialize ASIC */
2616e7b7db3dSAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR1, RT2573_HOST_READY);
261702ac6454SAndrew Thompson 
261802ac6454SAndrew Thompson 	/*
261902ac6454SAndrew Thompson 	 * Allocate Tx and Rx xfer queues.
262002ac6454SAndrew Thompson 	 */
262102ac6454SAndrew Thompson 	rum_setup_tx_list(sc);
262202ac6454SAndrew Thompson 
262302ac6454SAndrew Thompson 	/* update Rx filter */
262402ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
262502ac6454SAndrew Thompson 
262602ac6454SAndrew Thompson 	tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
262702ac6454SAndrew Thompson 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
262802ac6454SAndrew Thompson 		tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
262902ac6454SAndrew Thompson 		       RT2573_DROP_ACKCTS;
263002ac6454SAndrew Thompson 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
263102ac6454SAndrew Thompson 			tmp |= RT2573_DROP_TODS;
26327a79cebfSGleb Smirnoff 		if (ic->ic_promisc == 0)
263302ac6454SAndrew Thompson 			tmp |= RT2573_DROP_NOT_TO_ME;
263402ac6454SAndrew Thompson 	}
263502ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
263602ac6454SAndrew Thompson 
26377a79cebfSGleb Smirnoff 	sc->sc_running = 1;
2638ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2639a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
264002ac6454SAndrew Thompson 
26410698c0b3SAdrian Chadd end:	RUM_UNLOCK(sc);
26420698c0b3SAdrian Chadd 
26430698c0b3SAdrian Chadd 	if (ret != 0)
26440698c0b3SAdrian Chadd 		rum_stop(sc);
26450698c0b3SAdrian Chadd 
26460698c0b3SAdrian Chadd 	return ret;
264702ac6454SAndrew Thompson }
264802ac6454SAndrew Thompson 
264902ac6454SAndrew Thompson static void
26505efea30fSAndrew Thompson rum_stop(struct rum_softc *sc)
265102ac6454SAndrew Thompson {
265202ac6454SAndrew Thompson 
26530698c0b3SAdrian Chadd 	RUM_LOCK(sc);
26540698c0b3SAdrian Chadd 	if (!sc->sc_running) {
26550698c0b3SAdrian Chadd 		RUM_UNLOCK(sc);
26560698c0b3SAdrian Chadd 		return;
26570698c0b3SAdrian Chadd 	}
26587a79cebfSGleb Smirnoff 	sc->sc_running = 0;
265902ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
266002ac6454SAndrew Thompson 
266102ac6454SAndrew Thompson 	/*
266202ac6454SAndrew Thompson 	 * Drain the USB transfers, if not already drained:
266302ac6454SAndrew Thompson 	 */
2664a593f6b8SAndrew Thompson 	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2665a593f6b8SAndrew Thompson 	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
266602ac6454SAndrew Thompson 
266702ac6454SAndrew Thompson 	RUM_LOCK(sc);
266802ac6454SAndrew Thompson 	rum_unsetup_tx_list(sc);
266902ac6454SAndrew Thompson 
267002ac6454SAndrew Thompson 	/* disable Rx */
2671a05022b2SAdrian Chadd 	rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DISABLE_RX);
267202ac6454SAndrew Thompson 
267302ac6454SAndrew Thompson 	/* reset ASIC */
2674e7b7db3dSAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
267502ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR1, 0);
26760698c0b3SAdrian Chadd 	RUM_UNLOCK(sc);
267702ac6454SAndrew Thompson }
267802ac6454SAndrew Thompson 
267977ddf3d3SAndrew Thompson static void
268077ddf3d3SAndrew Thompson rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
268102ac6454SAndrew Thompson {
268202ac6454SAndrew Thompson 	uint16_t reg = RT2573_MCU_CODE_BASE;
2683e0a69b51SAndrew Thompson 	usb_error_t err;
268402ac6454SAndrew Thompson 
268502ac6454SAndrew Thompson 	/* copy firmware image into NIC */
268677ddf3d3SAndrew Thompson 	for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
268777ddf3d3SAndrew Thompson 		err = rum_write(sc, reg, UGETDW(ucode));
268877ddf3d3SAndrew Thompson 		if (err) {
268977ddf3d3SAndrew Thompson 			/* firmware already loaded ? */
269077ddf3d3SAndrew Thompson 			device_printf(sc->sc_dev, "Firmware load "
269177ddf3d3SAndrew Thompson 			    "failure! (ignored)\n");
269277ddf3d3SAndrew Thompson 			break;
269377ddf3d3SAndrew Thompson 		}
269477ddf3d3SAndrew Thompson 	}
269502ac6454SAndrew Thompson 
2696fd7dae9cSAndriy Voskoboinyk 	err = rum_do_mcu_request(sc, RT2573_MCU_RUN);
2697fd7dae9cSAndriy Voskoboinyk 	if (err != USB_ERR_NORMAL_COMPLETION) {
269802ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not run firmware: %s\n",
2699a593f6b8SAndrew Thompson 		    usbd_errstr(err));
270002ac6454SAndrew Thompson 	}
270177ddf3d3SAndrew Thompson 
270277ddf3d3SAndrew Thompson 	/* give the chip some time to boot */
270377ddf3d3SAndrew Thompson 	rum_pause(sc, hz / 8);
270402ac6454SAndrew Thompson }
270502ac6454SAndrew Thompson 
2706aa81f853SAdrian Chadd static int
2707349156a9SAndriy Voskoboinyk rum_set_sleep_time(struct rum_softc *sc, uint16_t bintval)
2708349156a9SAndriy Voskoboinyk {
2709349156a9SAndriy Voskoboinyk 	struct ieee80211com *ic = &sc->sc_ic;
2710349156a9SAndriy Voskoboinyk 	usb_error_t uerror;
2711349156a9SAndriy Voskoboinyk 	int exp, delay;
2712349156a9SAndriy Voskoboinyk 
2713349156a9SAndriy Voskoboinyk 	RUM_LOCK_ASSERT(sc);
2714349156a9SAndriy Voskoboinyk 
2715349156a9SAndriy Voskoboinyk 	exp = ic->ic_lintval / bintval;
2716349156a9SAndriy Voskoboinyk 	delay = ic->ic_lintval % bintval;
2717349156a9SAndriy Voskoboinyk 
2718349156a9SAndriy Voskoboinyk 	if (exp > RT2573_TBCN_EXP_MAX)
2719349156a9SAndriy Voskoboinyk 		exp = RT2573_TBCN_EXP_MAX;
2720349156a9SAndriy Voskoboinyk 	if (delay > RT2573_TBCN_DELAY_MAX)
2721349156a9SAndriy Voskoboinyk 		delay = RT2573_TBCN_DELAY_MAX;
2722349156a9SAndriy Voskoboinyk 
2723349156a9SAndriy Voskoboinyk 	uerror = rum_modbits(sc, RT2573_MAC_CSR11,
2724349156a9SAndriy Voskoboinyk 	    RT2573_TBCN_EXP(exp) |
2725349156a9SAndriy Voskoboinyk 	    RT2573_TBCN_DELAY(delay),
2726349156a9SAndriy Voskoboinyk 	    RT2573_TBCN_EXP(RT2573_TBCN_EXP_MAX) |
2727349156a9SAndriy Voskoboinyk 	    RT2573_TBCN_DELAY(RT2573_TBCN_DELAY_MAX));
2728349156a9SAndriy Voskoboinyk 
2729349156a9SAndriy Voskoboinyk 	if (uerror != USB_ERR_NORMAL_COMPLETION)
2730349156a9SAndriy Voskoboinyk 		return (EIO);
2731349156a9SAndriy Voskoboinyk 
2732349156a9SAndriy Voskoboinyk 	sc->sc_sleep_time = IEEE80211_TU_TO_TICKS(exp * bintval + delay);
2733349156a9SAndriy Voskoboinyk 
2734349156a9SAndriy Voskoboinyk 	return (0);
2735349156a9SAndriy Voskoboinyk }
2736349156a9SAndriy Voskoboinyk 
2737349156a9SAndriy Voskoboinyk static int
2738349156a9SAndriy Voskoboinyk rum_reset(struct ieee80211vap *vap, u_long cmd)
2739349156a9SAndriy Voskoboinyk {
2740349156a9SAndriy Voskoboinyk 	struct ieee80211com *ic = vap->iv_ic;
2741349156a9SAndriy Voskoboinyk 	struct ieee80211_node *ni;
2742349156a9SAndriy Voskoboinyk 	struct rum_softc *sc = ic->ic_softc;
2743349156a9SAndriy Voskoboinyk 	int error;
2744349156a9SAndriy Voskoboinyk 
2745349156a9SAndriy Voskoboinyk 	switch (cmd) {
2746349156a9SAndriy Voskoboinyk 	case IEEE80211_IOC_POWERSAVE:
2747c57ee45bSAndriy Voskoboinyk 	case IEEE80211_IOC_PROTMODE:
2748c57ee45bSAndriy Voskoboinyk 	case IEEE80211_IOC_RTSTHRESHOLD:
2749349156a9SAndriy Voskoboinyk 		error = 0;
2750349156a9SAndriy Voskoboinyk 		break;
2751349156a9SAndriy Voskoboinyk 	case IEEE80211_IOC_POWERSAVESLEEP:
2752349156a9SAndriy Voskoboinyk 		ni = ieee80211_ref_node(vap->iv_bss);
2753349156a9SAndriy Voskoboinyk 
2754349156a9SAndriy Voskoboinyk 		RUM_LOCK(sc);
2755349156a9SAndriy Voskoboinyk 		error = rum_set_sleep_time(sc, ni->ni_intval);
2756349156a9SAndriy Voskoboinyk 		if (vap->iv_state == IEEE80211_S_SLEEP) {
2757349156a9SAndriy Voskoboinyk 			/* Use new values for wakeup timer. */
2758349156a9SAndriy Voskoboinyk 			rum_clrbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
2759349156a9SAndriy Voskoboinyk 			rum_setbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
2760349156a9SAndriy Voskoboinyk 		}
2761349156a9SAndriy Voskoboinyk 		/* XXX send reassoc */
2762349156a9SAndriy Voskoboinyk 		RUM_UNLOCK(sc);
2763349156a9SAndriy Voskoboinyk 
2764349156a9SAndriy Voskoboinyk 		ieee80211_free_node(ni);
2765349156a9SAndriy Voskoboinyk 		break;
2766349156a9SAndriy Voskoboinyk 	default:
2767349156a9SAndriy Voskoboinyk 		error = ENETRESET;
2768349156a9SAndriy Voskoboinyk 		break;
2769349156a9SAndriy Voskoboinyk 	}
2770349156a9SAndriy Voskoboinyk 
2771349156a9SAndriy Voskoboinyk 	return (error);
2772349156a9SAndriy Voskoboinyk }
2773349156a9SAndriy Voskoboinyk 
2774349156a9SAndriy Voskoboinyk static int
2775b780f864SAdrian Chadd rum_set_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
277602ac6454SAndrew Thompson {
277702ac6454SAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
2778b780f864SAdrian Chadd 	struct rum_vap *rvp = RUM_VAP(vap);
2779b780f864SAdrian Chadd 	struct mbuf *m = rvp->bcn_mbuf;
278002ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
278102ac6454SAndrew Thompson 	struct rum_tx_desc desc;
278202ac6454SAndrew Thompson 
2783b780f864SAdrian Chadd 	RUM_LOCK_ASSERT(sc);
2784b780f864SAdrian Chadd 
2785b780f864SAdrian Chadd 	if (m == NULL)
2786aa81f853SAdrian Chadd 		return EINVAL;
2787bb571462SHans Petter Selasky 	if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2788aa81f853SAdrian Chadd 		return EINVAL;
2789f620e64dSKevin Lo 
279002ac6454SAndrew Thompson 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
27912cb9ef8dSAdrian Chadd 	rum_setup_tx_desc(sc, &desc, NULL, RT2573_TX_TIMESTAMP,
27921354b52cSAdrian Chadd 	    RT2573_TX_HWSEQ, 0, 0, m->m_pkthdr.len, tp->mgmtrate);
279302ac6454SAndrew Thompson 
2794b780f864SAdrian Chadd 	/* copy the Tx descriptor into NIC memory */
2795b780f864SAdrian Chadd 	if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0), (uint8_t *)&desc,
2796b780f864SAdrian Chadd 	    RT2573_TX_DESC_SIZE) != 0)
2797b780f864SAdrian Chadd 		return EIO;
279802ac6454SAndrew Thompson 
279902ac6454SAndrew Thompson 	/* copy beacon header and payload into NIC memory */
2800b780f864SAdrian Chadd 	if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0) + RT2573_TX_DESC_SIZE,
2801b780f864SAdrian Chadd 	    mtod(m, uint8_t *), m->m_pkthdr.len) != 0)
2802b780f864SAdrian Chadd 		return EIO;
280302ac6454SAndrew Thompson 
2804b780f864SAdrian Chadd 	return 0;
2805b780f864SAdrian Chadd }
2806b780f864SAdrian Chadd 
2807b780f864SAdrian Chadd static int
2808b780f864SAdrian Chadd rum_alloc_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2809b780f864SAdrian Chadd {
2810b780f864SAdrian Chadd 	struct rum_vap *rvp = RUM_VAP(vap);
2811b780f864SAdrian Chadd 	struct ieee80211_node *ni = vap->iv_bss;
2812b780f864SAdrian Chadd 	struct mbuf *m;
2813b780f864SAdrian Chadd 
2814b780f864SAdrian Chadd 	if (ni->ni_chan == IEEE80211_CHAN_ANYC)
2815b780f864SAdrian Chadd 		return EINVAL;
2816b780f864SAdrian Chadd 
2817210ab3c2SAdrian Chadd 	m = ieee80211_beacon_alloc(ni);
2818b780f864SAdrian Chadd 	if (m == NULL)
2819b780f864SAdrian Chadd 		return ENOMEM;
2820b780f864SAdrian Chadd 
2821b780f864SAdrian Chadd 	if (rvp->bcn_mbuf != NULL)
2822b780f864SAdrian Chadd 		m_freem(rvp->bcn_mbuf);
2823b780f864SAdrian Chadd 
2824b780f864SAdrian Chadd 	rvp->bcn_mbuf = m;
2825b780f864SAdrian Chadd 
2826b780f864SAdrian Chadd 	return (rum_set_beacon(sc, vap));
282702ac6454SAndrew Thompson }
282802ac6454SAndrew Thompson 
28290e3b4c60SAdrian Chadd static void
28300e3b4c60SAdrian Chadd rum_update_beacon_cb(struct rum_softc *sc, union sec_param *data,
2831ed5711a1SAdrian Chadd     uint8_t rvp_id)
28320e3b4c60SAdrian Chadd {
28330e3b4c60SAdrian Chadd 	struct ieee80211vap *vap = data->vap;
28340e3b4c60SAdrian Chadd 
28350e3b4c60SAdrian Chadd 	rum_set_beacon(sc, vap);
28360e3b4c60SAdrian Chadd }
28370e3b4c60SAdrian Chadd 
28380e3b4c60SAdrian Chadd static void
28390e3b4c60SAdrian Chadd rum_update_beacon(struct ieee80211vap *vap, int item)
28400e3b4c60SAdrian Chadd {
28410e3b4c60SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
28420e3b4c60SAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
28430e3b4c60SAdrian Chadd 	struct rum_vap *rvp = RUM_VAP(vap);
28440e3b4c60SAdrian Chadd 	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
28450e3b4c60SAdrian Chadd 	struct ieee80211_node *ni = vap->iv_bss;
28460e3b4c60SAdrian Chadd 	struct mbuf *m = rvp->bcn_mbuf;
28470e3b4c60SAdrian Chadd 	int mcast = 0;
28480e3b4c60SAdrian Chadd 
28490e3b4c60SAdrian Chadd 	RUM_LOCK(sc);
28500e3b4c60SAdrian Chadd 	if (m == NULL) {
2851210ab3c2SAdrian Chadd 		m = ieee80211_beacon_alloc(ni);
28520e3b4c60SAdrian Chadd 		if (m == NULL) {
28530e3b4c60SAdrian Chadd 			device_printf(sc->sc_dev,
28540e3b4c60SAdrian Chadd 			    "%s: could not allocate beacon frame\n", __func__);
28550e3b4c60SAdrian Chadd 			RUM_UNLOCK(sc);
28560e3b4c60SAdrian Chadd 			return;
28570e3b4c60SAdrian Chadd 		}
28580e3b4c60SAdrian Chadd 		rvp->bcn_mbuf = m;
28590e3b4c60SAdrian Chadd 	}
28600e3b4c60SAdrian Chadd 
28610e3b4c60SAdrian Chadd 	switch (item) {
28620e3b4c60SAdrian Chadd 	case IEEE80211_BEACON_ERP:
28630e3b4c60SAdrian Chadd 		rum_update_slot(ic);
28640e3b4c60SAdrian Chadd 		break;
28650e3b4c60SAdrian Chadd 	case IEEE80211_BEACON_TIM:
28660e3b4c60SAdrian Chadd 		mcast = 1;	/*TODO*/
28670e3b4c60SAdrian Chadd 		break;
28680e3b4c60SAdrian Chadd 	default:
28690e3b4c60SAdrian Chadd 		break;
28700e3b4c60SAdrian Chadd 	}
28710e3b4c60SAdrian Chadd 	RUM_UNLOCK(sc);
28720e3b4c60SAdrian Chadd 
28730e3b4c60SAdrian Chadd 	setbit(bo->bo_flags, item);
2874210ab3c2SAdrian Chadd 	ieee80211_beacon_update(ni, m, mcast);
28750e3b4c60SAdrian Chadd 
2876ed5711a1SAdrian Chadd 	rum_cmd_sleepable(sc, &vap, sizeof(vap), 0, rum_update_beacon_cb);
28770e3b4c60SAdrian Chadd }
28780e3b4c60SAdrian Chadd 
287902ac6454SAndrew Thompson static int
28802cb9ef8dSAdrian Chadd rum_common_key_set(struct rum_softc *sc, struct ieee80211_key *k,
28812cb9ef8dSAdrian Chadd     uint16_t base)
28822cb9ef8dSAdrian Chadd {
28832cb9ef8dSAdrian Chadd 
28842cb9ef8dSAdrian Chadd 	if (rum_write_multi(sc, base, k->wk_key, k->wk_keylen))
28852cb9ef8dSAdrian Chadd 		return EIO;
28862cb9ef8dSAdrian Chadd 
28872cb9ef8dSAdrian Chadd 	if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
28882cb9ef8dSAdrian Chadd 		if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE,
28892cb9ef8dSAdrian Chadd 		    k->wk_txmic, 8))
28902cb9ef8dSAdrian Chadd 			return EIO;
28912cb9ef8dSAdrian Chadd 		if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE + 8,
28922cb9ef8dSAdrian Chadd 		    k->wk_rxmic, 8))
28932cb9ef8dSAdrian Chadd 			return EIO;
28942cb9ef8dSAdrian Chadd 	}
28952cb9ef8dSAdrian Chadd 
28962cb9ef8dSAdrian Chadd 	return 0;
28972cb9ef8dSAdrian Chadd }
28982cb9ef8dSAdrian Chadd 
28992cb9ef8dSAdrian Chadd static void
29002cb9ef8dSAdrian Chadd rum_group_key_set_cb(struct rum_softc *sc, union sec_param *data,
2901ed5711a1SAdrian Chadd     uint8_t rvp_id)
29022cb9ef8dSAdrian Chadd {
29032cb9ef8dSAdrian Chadd 	struct ieee80211_key *k = &data->key;
29042cb9ef8dSAdrian Chadd 	uint8_t mode;
29052cb9ef8dSAdrian Chadd 
29062cb9ef8dSAdrian Chadd 	if (sc->sc_clr_shkeys == 0) {
29072cb9ef8dSAdrian Chadd 		rum_clr_shkey_regs(sc);
29082cb9ef8dSAdrian Chadd 		sc->sc_clr_shkeys = 1;
29092cb9ef8dSAdrian Chadd 	}
29102cb9ef8dSAdrian Chadd 
29112cb9ef8dSAdrian Chadd 	mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
29122cb9ef8dSAdrian Chadd 	if (mode == 0)
29132cb9ef8dSAdrian Chadd 		goto print_err;
29142cb9ef8dSAdrian Chadd 
29152cb9ef8dSAdrian Chadd 	DPRINTFN(1, "setting group key %d for vap %d, mode %d "
29162cb9ef8dSAdrian Chadd 	    "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
29172cb9ef8dSAdrian Chadd 	    (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
29182cb9ef8dSAdrian Chadd 	    (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
29192cb9ef8dSAdrian Chadd 
29202cb9ef8dSAdrian Chadd 	/* Install the key. */
29212cb9ef8dSAdrian Chadd 	if (rum_common_key_set(sc, k, RT2573_SKEY(rvp_id, k->wk_keyix)) != 0)
29222cb9ef8dSAdrian Chadd 		goto print_err;
29232cb9ef8dSAdrian Chadd 
29242cb9ef8dSAdrian Chadd 	/* Set cipher mode. */
29252cb9ef8dSAdrian Chadd 	if (rum_modbits(sc, rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
29262cb9ef8dSAdrian Chadd 	      mode << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX,
29272cb9ef8dSAdrian Chadd 	      RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX)
29282cb9ef8dSAdrian Chadd 	    != 0)
29292cb9ef8dSAdrian Chadd 		goto print_err;
29302cb9ef8dSAdrian Chadd 
29312cb9ef8dSAdrian Chadd 	/* Mark this key as valid. */
29322cb9ef8dSAdrian Chadd 	if (rum_setbits(sc, RT2573_SEC_CSR0,
29332cb9ef8dSAdrian Chadd 	      1 << (rvp_id * RT2573_SKEY_MAX + k->wk_keyix)) != 0)
29342cb9ef8dSAdrian Chadd 		goto print_err;
29352cb9ef8dSAdrian Chadd 
29362cb9ef8dSAdrian Chadd 	return;
29372cb9ef8dSAdrian Chadd 
29382cb9ef8dSAdrian Chadd print_err:
29392cb9ef8dSAdrian Chadd 	device_printf(sc->sc_dev, "%s: cannot set group key %d for vap %d\n",
29402cb9ef8dSAdrian Chadd 	    __func__, k->wk_keyix, rvp_id);
29412cb9ef8dSAdrian Chadd }
29422cb9ef8dSAdrian Chadd 
29432cb9ef8dSAdrian Chadd static void
29442cb9ef8dSAdrian Chadd rum_group_key_del_cb(struct rum_softc *sc, union sec_param *data,
2945ed5711a1SAdrian Chadd     uint8_t rvp_id)
29462cb9ef8dSAdrian Chadd {
29472cb9ef8dSAdrian Chadd 	struct ieee80211_key *k = &data->key;
29482cb9ef8dSAdrian Chadd 
29492cb9ef8dSAdrian Chadd 	DPRINTF("%s: removing group key %d for vap %d\n", __func__,
29502cb9ef8dSAdrian Chadd 	    k->wk_keyix, rvp_id);
29512cb9ef8dSAdrian Chadd 	rum_clrbits(sc,
29522cb9ef8dSAdrian Chadd 	    rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
29532cb9ef8dSAdrian Chadd 	    RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX);
29542cb9ef8dSAdrian Chadd 	rum_clrbits(sc, RT2573_SEC_CSR0,
29552cb9ef8dSAdrian Chadd 	    rvp_id * RT2573_SKEY_MAX + k->wk_keyix);
29562cb9ef8dSAdrian Chadd }
29572cb9ef8dSAdrian Chadd 
29582cb9ef8dSAdrian Chadd static void
2959ed5711a1SAdrian Chadd rum_pair_key_set_cb(struct rum_softc *sc, union sec_param *data,
29602cb9ef8dSAdrian Chadd     uint8_t rvp_id)
29612cb9ef8dSAdrian Chadd {
29622cb9ef8dSAdrian Chadd 	struct ieee80211_key *k = &data->key;
29632cb9ef8dSAdrian Chadd 	uint8_t buf[IEEE80211_ADDR_LEN + 1];
29642cb9ef8dSAdrian Chadd 	uint8_t mode;
29652cb9ef8dSAdrian Chadd 
29662cb9ef8dSAdrian Chadd 	mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
29672cb9ef8dSAdrian Chadd 	if (mode == 0)
29682cb9ef8dSAdrian Chadd 		goto print_err;
29692cb9ef8dSAdrian Chadd 
29702cb9ef8dSAdrian Chadd 	DPRINTFN(1, "setting pairwise key %d for vap %d, mode %d "
29712cb9ef8dSAdrian Chadd 	    "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
29722cb9ef8dSAdrian Chadd 	    (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
29732cb9ef8dSAdrian Chadd 	    (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
29742cb9ef8dSAdrian Chadd 
29752cb9ef8dSAdrian Chadd 	/* Install the key. */
29762cb9ef8dSAdrian Chadd 	if (rum_common_key_set(sc, k, RT2573_PKEY(k->wk_keyix)) != 0)
29772cb9ef8dSAdrian Chadd 		goto print_err;
29782cb9ef8dSAdrian Chadd 
29792cb9ef8dSAdrian Chadd 	IEEE80211_ADDR_COPY(buf, k->wk_macaddr);
29802cb9ef8dSAdrian Chadd 	buf[IEEE80211_ADDR_LEN] = mode;
29812cb9ef8dSAdrian Chadd 
29822cb9ef8dSAdrian Chadd 	/* Set transmitter address and cipher mode. */
29832cb9ef8dSAdrian Chadd 	if (rum_write_multi(sc, RT2573_ADDR_ENTRY(k->wk_keyix),
29842cb9ef8dSAdrian Chadd 	      buf, sizeof buf) != 0)
29852cb9ef8dSAdrian Chadd 		goto print_err;
29862cb9ef8dSAdrian Chadd 
29872cb9ef8dSAdrian Chadd 	/* Enable key table lookup for this vap. */
29882cb9ef8dSAdrian Chadd 	if (sc->vap_key_count[rvp_id]++ == 0)
29892cb9ef8dSAdrian Chadd 		if (rum_setbits(sc, RT2573_SEC_CSR4, 1 << rvp_id) != 0)
29902cb9ef8dSAdrian Chadd 			goto print_err;
29912cb9ef8dSAdrian Chadd 
29922cb9ef8dSAdrian Chadd 	/* Mark this key as valid. */
29932cb9ef8dSAdrian Chadd 	if (rum_setbits(sc,
29942cb9ef8dSAdrian Chadd 	      k->wk_keyix < 32 ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
29952cb9ef8dSAdrian Chadd 	      1 << (k->wk_keyix % 32)) != 0)
29962cb9ef8dSAdrian Chadd 		goto print_err;
29972cb9ef8dSAdrian Chadd 
29982cb9ef8dSAdrian Chadd 	return;
29992cb9ef8dSAdrian Chadd 
30002cb9ef8dSAdrian Chadd print_err:
30012cb9ef8dSAdrian Chadd 	device_printf(sc->sc_dev,
30022cb9ef8dSAdrian Chadd 	    "%s: cannot set pairwise key %d, vap %d\n", __func__, k->wk_keyix,
30032cb9ef8dSAdrian Chadd 	    rvp_id);
30042cb9ef8dSAdrian Chadd }
30052cb9ef8dSAdrian Chadd 
30062cb9ef8dSAdrian Chadd static void
3007ed5711a1SAdrian Chadd rum_pair_key_del_cb(struct rum_softc *sc, union sec_param *data,
30082cb9ef8dSAdrian Chadd     uint8_t rvp_id)
30092cb9ef8dSAdrian Chadd {
30102cb9ef8dSAdrian Chadd 	struct ieee80211_key *k = &data->key;
30112cb9ef8dSAdrian Chadd 
30122cb9ef8dSAdrian Chadd 	DPRINTF("%s: removing key %d\n", __func__, k->wk_keyix);
30132cb9ef8dSAdrian Chadd 	rum_clrbits(sc, (k->wk_keyix < 32) ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
30142cb9ef8dSAdrian Chadd 	    1 << (k->wk_keyix % 32));
3015fe3e4523SHans Petter Selasky 	sc->keys_bmap &= ~(1ULL << k->wk_keyix);
30162cb9ef8dSAdrian Chadd 	if (--sc->vap_key_count[rvp_id] == 0)
30172cb9ef8dSAdrian Chadd 		rum_clrbits(sc, RT2573_SEC_CSR4, 1 << rvp_id);
30182cb9ef8dSAdrian Chadd }
30192cb9ef8dSAdrian Chadd 
30202cb9ef8dSAdrian Chadd static int
30212cb9ef8dSAdrian Chadd rum_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
30222cb9ef8dSAdrian Chadd     ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
30232cb9ef8dSAdrian Chadd {
30242cb9ef8dSAdrian Chadd 	struct rum_softc *sc = vap->iv_ic->ic_softc;
30252cb9ef8dSAdrian Chadd 	uint8_t i;
30262cb9ef8dSAdrian Chadd 
30272cb9ef8dSAdrian Chadd 	if (!(&vap->iv_nw_keys[0] <= k &&
30282cb9ef8dSAdrian Chadd 	     k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
30292cb9ef8dSAdrian Chadd 		if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
30302cb9ef8dSAdrian Chadd 			RUM_LOCK(sc);
30312cb9ef8dSAdrian Chadd 			for (i = 0; i < RT2573_ADDR_MAX; i++) {
3032fe3e4523SHans Petter Selasky 				if ((sc->keys_bmap & (1ULL << i)) == 0) {
3033fe3e4523SHans Petter Selasky 					sc->keys_bmap |= (1ULL << i);
30342cb9ef8dSAdrian Chadd 					*keyix = i;
30352cb9ef8dSAdrian Chadd 					break;
30362cb9ef8dSAdrian Chadd 				}
30372cb9ef8dSAdrian Chadd 			}
30382cb9ef8dSAdrian Chadd 			RUM_UNLOCK(sc);
30392cb9ef8dSAdrian Chadd 			if (i == RT2573_ADDR_MAX) {
30402cb9ef8dSAdrian Chadd 				device_printf(sc->sc_dev,
30412cb9ef8dSAdrian Chadd 				    "%s: no free space in the key table\n",
30422cb9ef8dSAdrian Chadd 				    __func__);
30432cb9ef8dSAdrian Chadd 				return 0;
30442cb9ef8dSAdrian Chadd 			}
30452cb9ef8dSAdrian Chadd 		} else
30462cb9ef8dSAdrian Chadd 			*keyix = 0;
30472cb9ef8dSAdrian Chadd 	} else {
30482cb9ef8dSAdrian Chadd 		*keyix = k - vap->iv_nw_keys;
30492cb9ef8dSAdrian Chadd 	}
30502cb9ef8dSAdrian Chadd 	*rxkeyix = *keyix;
30512cb9ef8dSAdrian Chadd 	return 1;
30522cb9ef8dSAdrian Chadd }
30532cb9ef8dSAdrian Chadd 
30542cb9ef8dSAdrian Chadd static int
3055bc813c40SAdrian Chadd rum_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
30562cb9ef8dSAdrian Chadd {
30572cb9ef8dSAdrian Chadd 	struct rum_softc *sc = vap->iv_ic->ic_softc;
30582cb9ef8dSAdrian Chadd 	int group;
30592cb9ef8dSAdrian Chadd 
30602cb9ef8dSAdrian Chadd 	if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
30612cb9ef8dSAdrian Chadd 		/* Not for us. */
30622cb9ef8dSAdrian Chadd 		return 1;
30632cb9ef8dSAdrian Chadd 	}
30642cb9ef8dSAdrian Chadd 
30652cb9ef8dSAdrian Chadd 	group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
30662cb9ef8dSAdrian Chadd 
3067ed5711a1SAdrian Chadd 	return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
30682cb9ef8dSAdrian Chadd 		   group ? rum_group_key_set_cb : rum_pair_key_set_cb);
30692cb9ef8dSAdrian Chadd }
30702cb9ef8dSAdrian Chadd 
30712cb9ef8dSAdrian Chadd static int
30722cb9ef8dSAdrian Chadd rum_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
30732cb9ef8dSAdrian Chadd {
30742cb9ef8dSAdrian Chadd 	struct rum_softc *sc = vap->iv_ic->ic_softc;
30752cb9ef8dSAdrian Chadd 	int group;
30762cb9ef8dSAdrian Chadd 
30772cb9ef8dSAdrian Chadd 	if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
30782cb9ef8dSAdrian Chadd 		/* Not for us. */
30792cb9ef8dSAdrian Chadd 		return 1;
30802cb9ef8dSAdrian Chadd 	}
30812cb9ef8dSAdrian Chadd 
30822cb9ef8dSAdrian Chadd 	group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
30832cb9ef8dSAdrian Chadd 
3084ed5711a1SAdrian Chadd 	return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
30852cb9ef8dSAdrian Chadd 		   group ? rum_group_key_del_cb : rum_pair_key_del_cb);
30862cb9ef8dSAdrian Chadd }
30872cb9ef8dSAdrian Chadd 
30882cb9ef8dSAdrian Chadd static int
308902ac6454SAndrew Thompson rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
309002ac6454SAndrew Thompson     const struct ieee80211_bpf_params *params)
309102ac6454SAndrew Thompson {
3092d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ni->ni_ic->ic_softc;
3093b2a76342SAdrian Chadd 	int ret;
309402ac6454SAndrew Thompson 
309502ac6454SAndrew Thompson 	RUM_LOCK(sc);
309602ac6454SAndrew Thompson 	/* prevent management frames from being sent if we're not ready */
30977a79cebfSGleb Smirnoff 	if (!sc->sc_running) {
3098b2a76342SAdrian Chadd 		ret = ENETDOWN;
3099b2a76342SAdrian Chadd 		goto bad;
310002ac6454SAndrew Thompson 	}
310196ca458fSAndrew Thompson 	if (sc->tx_nfree < RUM_TX_MINFREE) {
3102b2a76342SAdrian Chadd 		ret = EIO;
3103b2a76342SAdrian Chadd 		goto bad;
310402ac6454SAndrew Thompson 	}
310502ac6454SAndrew Thompson 
310602ac6454SAndrew Thompson 	if (params == NULL) {
310702ac6454SAndrew Thompson 		/*
310802ac6454SAndrew Thompson 		 * Legacy path; interpret frame contents to decide
310902ac6454SAndrew Thompson 		 * precisely how to send the frame.
311002ac6454SAndrew Thompson 		 */
3111b2a76342SAdrian Chadd 		if ((ret = rum_tx_mgt(sc, m, ni)) != 0)
311202ac6454SAndrew Thompson 			goto bad;
311302ac6454SAndrew Thompson 	} else {
311402ac6454SAndrew Thompson 		/*
311502ac6454SAndrew Thompson 		 * Caller supplied explicit parameters to use in
311602ac6454SAndrew Thompson 		 * sending the frame.
311702ac6454SAndrew Thompson 		 */
3118b2a76342SAdrian Chadd 		if ((ret = rum_tx_raw(sc, m, ni, params)) != 0)
311902ac6454SAndrew Thompson 			goto bad;
312002ac6454SAndrew Thompson 	}
312102ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
312202ac6454SAndrew Thompson 
312302ac6454SAndrew Thompson 	return 0;
312402ac6454SAndrew Thompson bad:
312502ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
3126b2a76342SAdrian Chadd 	m_freem(m);
3127b2a76342SAdrian Chadd 	return ret;
312802ac6454SAndrew Thompson }
312902ac6454SAndrew Thompson 
313002ac6454SAndrew Thompson static void
3131b6108616SRui Paulo rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
313202ac6454SAndrew Thompson {
313302ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
313402ac6454SAndrew Thompson 	struct rum_vap *rvp = RUM_VAP(vap);
313502ac6454SAndrew Thompson 
313602ac6454SAndrew Thompson 	/* clear statistic registers (STA_CSR0 to STA_CSR5) */
313702ac6454SAndrew Thompson 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
313802ac6454SAndrew Thompson 
3139b6108616SRui Paulo 	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
314002ac6454SAndrew Thompson }
314102ac6454SAndrew Thompson 
314202ac6454SAndrew Thompson static void
3143b6108616SRui Paulo rum_ratectl_timeout(void *arg)
314402ac6454SAndrew Thompson {
314502ac6454SAndrew Thompson 	struct rum_vap *rvp = arg;
31465efea30fSAndrew Thompson 	struct ieee80211vap *vap = &rvp->vap;
31475efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
314802ac6454SAndrew Thompson 
3149b6108616SRui Paulo 	ieee80211_runtask(ic, &rvp->ratectl_task);
315002ac6454SAndrew Thompson }
315102ac6454SAndrew Thompson 
315202ac6454SAndrew Thompson static void
3153b6108616SRui Paulo rum_ratectl_task(void *arg, int pending)
315402ac6454SAndrew Thompson {
31555efea30fSAndrew Thompson 	struct rum_vap *rvp = arg;
31565efea30fSAndrew Thompson 	struct ieee80211vap *vap = &rvp->vap;
3157f4ac78a2SAdrian Chadd 	struct rum_softc *sc = vap->iv_ic->ic_softc;
31588d8bdb01SAndrew Thompson 	struct ieee80211_node *ni;
3159f4ac78a2SAdrian Chadd 	int ok[3], fail;
3160f4ac78a2SAdrian Chadd 	int sum, success, retrycnt;
316102ac6454SAndrew Thompson 
31625efea30fSAndrew Thompson 	RUM_LOCK(sc);
3163f4ac78a2SAdrian Chadd 	/* read and clear statistic registers (STA_CSR0 to STA_CSR5) */
316402ac6454SAndrew Thompson 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
316502ac6454SAndrew Thompson 
3166f4ac78a2SAdrian Chadd 	ok[0] = (le32toh(sc->sta[4]) & 0xffff);	/* TX ok w/o retry */
3167f4ac78a2SAdrian Chadd 	ok[1] = (le32toh(sc->sta[4]) >> 16);	/* TX ok w/ one retry */
3168f4ac78a2SAdrian Chadd 	ok[2] = (le32toh(sc->sta[5]) & 0xffff);	/* TX ok w/ multiple retries */
316902ac6454SAndrew Thompson 	fail =  (le32toh(sc->sta[5]) >> 16);	/* TX retry-fail count */
317002ac6454SAndrew Thompson 
3171f4ac78a2SAdrian Chadd 	success = ok[0] + ok[1] + ok[2];
3172f4ac78a2SAdrian Chadd 	sum = success + fail;
3173f4ac78a2SAdrian Chadd 	/* XXX at least */
3174f4ac78a2SAdrian Chadd 	retrycnt = ok[1] + ok[2] * 2 + fail * (rvp->maxretry + 1);
3175f4ac78a2SAdrian Chadd 
3176f4ac78a2SAdrian Chadd 	if (sum != 0) {
31778d8bdb01SAndrew Thompson 		ni = ieee80211_ref_node(vap->iv_bss);
3178b6108616SRui Paulo 		ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt);
3179b6108616SRui Paulo 		(void) ieee80211_ratectl_rate(ni, NULL, 0);
31808d8bdb01SAndrew Thompson 		ieee80211_free_node(ni);
3181f4ac78a2SAdrian Chadd 	}
318202ac6454SAndrew Thompson 
31837a79cebfSGleb Smirnoff 	/* count TX retry-fail as Tx errors */
3184f4ac78a2SAdrian Chadd 	if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, fail);
318502ac6454SAndrew Thompson 
3186b6108616SRui Paulo 	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
31875efea30fSAndrew Thompson 	RUM_UNLOCK(sc);
318802ac6454SAndrew Thompson }
318902ac6454SAndrew Thompson 
319002ac6454SAndrew Thompson static void
319102ac6454SAndrew Thompson rum_scan_start(struct ieee80211com *ic)
319202ac6454SAndrew Thompson {
3193d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
319402ac6454SAndrew Thompson 
319502ac6454SAndrew Thompson 	RUM_LOCK(sc);
31966d804321SAdrian Chadd 	rum_abort_tsf_sync(sc);
31977a79cebfSGleb Smirnoff 	rum_set_bssid(sc, ieee80211broadcastaddr);
319802ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
319902ac6454SAndrew Thompson 
320002ac6454SAndrew Thompson }
320102ac6454SAndrew Thompson 
320202ac6454SAndrew Thompson static void
320302ac6454SAndrew Thompson rum_scan_end(struct ieee80211com *ic)
320402ac6454SAndrew Thompson {
3205d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
320602ac6454SAndrew Thompson 
320737704427SAndriy Voskoboinyk 	if (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) {
320802ac6454SAndrew Thompson 		RUM_LOCK(sc);
320950a31b48SAdrian Chadd 		if (ic->ic_opmode != IEEE80211_M_AHDEMO)
32105efea30fSAndrew Thompson 			rum_enable_tsf_sync(sc);
321150a31b48SAdrian Chadd 		else
321250a31b48SAdrian Chadd 			rum_enable_tsf(sc);
32131ba67b11SAdrian Chadd 		rum_set_bssid(sc, sc->sc_bssid);
321402ac6454SAndrew Thompson 		RUM_UNLOCK(sc);
321537704427SAndriy Voskoboinyk 	}
321602ac6454SAndrew Thompson }
321702ac6454SAndrew Thompson 
321802ac6454SAndrew Thompson static void
321902ac6454SAndrew Thompson rum_set_channel(struct ieee80211com *ic)
322002ac6454SAndrew Thompson {
3221d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
322202ac6454SAndrew Thompson 
322302ac6454SAndrew Thompson 	RUM_LOCK(sc);
322402ac6454SAndrew Thompson 	rum_set_chan(sc, ic->ic_curchan);
32255efea30fSAndrew Thompson 	RUM_UNLOCK(sc);
322602ac6454SAndrew Thompson }
322702ac6454SAndrew Thompson 
32287b45c9dfSAndriy Voskoboinyk static void
32297b45c9dfSAndriy Voskoboinyk rum_getradiocaps(struct ieee80211com *ic,
32307b45c9dfSAndriy Voskoboinyk     int maxchans, int *nchans, struct ieee80211_channel chans[])
32317b45c9dfSAndriy Voskoboinyk {
32327b45c9dfSAndriy Voskoboinyk 	struct rum_softc *sc = ic->ic_softc;
32337b45c9dfSAndriy Voskoboinyk 	uint8_t bands[IEEE80211_MODE_BYTES];
32347b45c9dfSAndriy Voskoboinyk 
32357b45c9dfSAndriy Voskoboinyk 	memset(bands, 0, sizeof(bands));
32367b45c9dfSAndriy Voskoboinyk 	setbit(bands, IEEE80211_MODE_11B);
32377b45c9dfSAndriy Voskoboinyk 	setbit(bands, IEEE80211_MODE_11G);
32387b45c9dfSAndriy Voskoboinyk 	ieee80211_add_channel_list_2ghz(chans, maxchans, nchans,
32397b45c9dfSAndriy Voskoboinyk 	    rum_chan_2ghz, nitems(rum_chan_2ghz), bands, 0);
32407b45c9dfSAndriy Voskoboinyk 
32417b45c9dfSAndriy Voskoboinyk 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
32427b45c9dfSAndriy Voskoboinyk 		setbit(bands, IEEE80211_MODE_11A);
32437b45c9dfSAndriy Voskoboinyk 		ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
32447b45c9dfSAndriy Voskoboinyk 		    rum_chan_5ghz, nitems(rum_chan_5ghz), bands, 0);
32457b45c9dfSAndriy Voskoboinyk 	}
32467b45c9dfSAndriy Voskoboinyk }
32477b45c9dfSAndriy Voskoboinyk 
324802ac6454SAndrew Thompson static int
324902ac6454SAndrew Thompson rum_get_rssi(struct rum_softc *sc, uint8_t raw)
325002ac6454SAndrew Thompson {
32517a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
325202ac6454SAndrew Thompson 	int lna, agc, rssi;
325302ac6454SAndrew Thompson 
325402ac6454SAndrew Thompson 	lna = (raw >> 5) & 0x3;
325502ac6454SAndrew Thompson 	agc = raw & 0x1f;
325602ac6454SAndrew Thompson 
325702ac6454SAndrew Thompson 	if (lna == 0) {
325802ac6454SAndrew Thompson 		/*
325902ac6454SAndrew Thompson 		 * No RSSI mapping
326002ac6454SAndrew Thompson 		 *
326102ac6454SAndrew Thompson 		 * NB: Since RSSI is relative to noise floor, -1 is
326202ac6454SAndrew Thompson 		 *     adequate for caller to know error happened.
326302ac6454SAndrew Thompson 		 */
326402ac6454SAndrew Thompson 		return -1;
326502ac6454SAndrew Thompson 	}
326602ac6454SAndrew Thompson 
326702ac6454SAndrew Thompson 	rssi = (2 * agc) - RT2573_NOISE_FLOOR;
326802ac6454SAndrew Thompson 
326902ac6454SAndrew Thompson 	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
327002ac6454SAndrew Thompson 		rssi += sc->rssi_2ghz_corr;
327102ac6454SAndrew Thompson 
327202ac6454SAndrew Thompson 		if (lna == 1)
327302ac6454SAndrew Thompson 			rssi -= 64;
327402ac6454SAndrew Thompson 		else if (lna == 2)
327502ac6454SAndrew Thompson 			rssi -= 74;
327602ac6454SAndrew Thompson 		else if (lna == 3)
327702ac6454SAndrew Thompson 			rssi -= 90;
327802ac6454SAndrew Thompson 	} else {
327902ac6454SAndrew Thompson 		rssi += sc->rssi_5ghz_corr;
328002ac6454SAndrew Thompson 
328102ac6454SAndrew Thompson 		if (!sc->ext_5ghz_lna && lna != 1)
328202ac6454SAndrew Thompson 			rssi += 4;
328302ac6454SAndrew Thompson 
328402ac6454SAndrew Thompson 		if (lna == 1)
328502ac6454SAndrew Thompson 			rssi -= 64;
328602ac6454SAndrew Thompson 		else if (lna == 2)
328702ac6454SAndrew Thompson 			rssi -= 86;
328802ac6454SAndrew Thompson 		else if (lna == 3)
328902ac6454SAndrew Thompson 			rssi -= 100;
329002ac6454SAndrew Thompson 	}
329102ac6454SAndrew Thompson 	return rssi;
329202ac6454SAndrew Thompson }
329302ac6454SAndrew Thompson 
329402ac6454SAndrew Thompson static int
329502ac6454SAndrew Thompson rum_pause(struct rum_softc *sc, int timeout)
329602ac6454SAndrew Thompson {
329702ac6454SAndrew Thompson 
3298a593f6b8SAndrew Thompson 	usb_pause_mtx(&sc->sc_mtx, timeout);
329902ac6454SAndrew Thompson 	return (0);
330002ac6454SAndrew Thompson }
330102ac6454SAndrew Thompson 
330202ac6454SAndrew Thompson static device_method_t rum_methods[] = {
330302ac6454SAndrew Thompson 	/* Device interface */
330402ac6454SAndrew Thompson 	DEVMETHOD(device_probe,		rum_match),
330502ac6454SAndrew Thompson 	DEVMETHOD(device_attach,	rum_attach),
330602ac6454SAndrew Thompson 	DEVMETHOD(device_detach,	rum_detach),
3307645e4d17SHans Petter Selasky 	DEVMETHOD_END
330802ac6454SAndrew Thompson };
330902ac6454SAndrew Thompson 
331002ac6454SAndrew Thompson static driver_t rum_driver = {
331102ac6454SAndrew Thompson 	.name = "rum",
331202ac6454SAndrew Thompson 	.methods = rum_methods,
331302ac6454SAndrew Thompson 	.size = sizeof(struct rum_softc),
331402ac6454SAndrew Thompson };
331502ac6454SAndrew Thompson 
331602ac6454SAndrew Thompson static devclass_t rum_devclass;
331702ac6454SAndrew Thompson 
33189aef556dSAndrew Thompson DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
3319910cb8feSAndrew Thompson MODULE_DEPEND(rum, wlan, 1, 1, 1);
3320910cb8feSAndrew Thompson MODULE_DEPEND(rum, usb, 1, 1, 1);
3321910cb8feSAndrew Thompson MODULE_VERSION(rum, 1);
3322f809f280SWarner Losh USB_PNP_HOST_INFO(rum_devs);
3323