xref: /freebsd/sys/dev/usb/wlan/if_rum.c (revision 96e29c261d2d8deefec3c3521e1246ab714bbeec)
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 *);
16802ac6454SAndrew Thompson static void		rum_unsetup_tx_list(struct rum_softc *);
16902ac6454SAndrew Thompson static int		rum_newstate(struct ieee80211vap *,
17002ac6454SAndrew Thompson 			    enum ieee80211_state, int);
1712cb9ef8dSAdrian Chadd static uint8_t		rum_crypto_mode(struct rum_softc *, u_int, int);
17202ac6454SAndrew Thompson static void		rum_setup_tx_desc(struct rum_softc *,
1732cb9ef8dSAdrian Chadd 			    struct rum_tx_desc *, struct ieee80211_key *,
1741354b52cSAdrian Chadd 			    uint32_t, uint8_t, uint8_t, int, int, int);
1752cb9ef8dSAdrian Chadd static uint32_t		rum_tx_crypto_flags(struct rum_softc *,
1762cb9ef8dSAdrian Chadd 			    struct ieee80211_node *,
1772cb9ef8dSAdrian Chadd 			    const struct ieee80211_key *);
17802ac6454SAndrew Thompson static int		rum_tx_mgt(struct rum_softc *, struct mbuf *,
17902ac6454SAndrew Thompson 			    struct ieee80211_node *);
18002ac6454SAndrew Thompson static int		rum_tx_raw(struct rum_softc *, struct mbuf *,
18102ac6454SAndrew Thompson 			    struct ieee80211_node *,
18202ac6454SAndrew Thompson 			    const struct ieee80211_bpf_params *);
18302ac6454SAndrew Thompson static int		rum_tx_data(struct rum_softc *, struct mbuf *,
18402ac6454SAndrew Thompson 			    struct ieee80211_node *);
1857a79cebfSGleb Smirnoff static int		rum_transmit(struct ieee80211com *, struct mbuf *);
1867a79cebfSGleb Smirnoff static void		rum_start(struct rum_softc *);
1877a79cebfSGleb Smirnoff static void		rum_parent(struct ieee80211com *);
18802ac6454SAndrew Thompson static void		rum_eeprom_read(struct rum_softc *, uint16_t, void *,
18902ac6454SAndrew Thompson 			    int);
19002ac6454SAndrew Thompson static uint32_t		rum_read(struct rum_softc *, uint16_t);
19102ac6454SAndrew Thompson static void		rum_read_multi(struct rum_softc *, uint16_t, void *,
19202ac6454SAndrew Thompson 			    int);
193e0a69b51SAndrew Thompson static usb_error_t	rum_write(struct rum_softc *, uint16_t, uint32_t);
194e0a69b51SAndrew Thompson static usb_error_t	rum_write_multi(struct rum_softc *, uint16_t, void *,
19502ac6454SAndrew Thompson 			    size_t);
196a05022b2SAdrian Chadd static usb_error_t	rum_setbits(struct rum_softc *, uint16_t, uint32_t);
197a05022b2SAdrian Chadd static usb_error_t	rum_clrbits(struct rum_softc *, uint16_t, uint32_t);
198a05022b2SAdrian Chadd static usb_error_t	rum_modbits(struct rum_softc *, uint16_t, uint32_t,
199a05022b2SAdrian Chadd 			    uint32_t);
200cc9ae761SAdrian Chadd static int		rum_bbp_busy(struct rum_softc *);
20102ac6454SAndrew Thompson static void		rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
20202ac6454SAndrew Thompson static uint8_t		rum_bbp_read(struct rum_softc *, uint8_t);
20302ac6454SAndrew Thompson static void		rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
20402ac6454SAndrew Thompson static void		rum_select_antenna(struct rum_softc *);
20502ac6454SAndrew Thompson static void		rum_enable_mrr(struct rum_softc *);
20602ac6454SAndrew Thompson static void		rum_set_txpreamble(struct rum_softc *);
20702ac6454SAndrew Thompson static void		rum_set_basicrates(struct rum_softc *);
20802ac6454SAndrew Thompson static void		rum_select_band(struct rum_softc *,
20902ac6454SAndrew Thompson 			    struct ieee80211_channel *);
21002ac6454SAndrew Thompson static void		rum_set_chan(struct rum_softc *,
21102ac6454SAndrew Thompson 			    struct ieee80211_channel *);
212342ced03SAdrian Chadd static void		rum_set_maxretry(struct rum_softc *,
213342ced03SAdrian Chadd 			    struct ieee80211vap *);
214aa81f853SAdrian Chadd static int		rum_enable_tsf_sync(struct rum_softc *);
2155463c4a4SSam Leffler static void		rum_enable_tsf(struct rum_softc *);
2166d804321SAdrian Chadd static void		rum_abort_tsf_sync(struct rum_softc *);
217a6ccd477SAdrian Chadd static void		rum_get_tsf(struct rum_softc *, uint64_t *);
218a19dbd8dSAdrian Chadd static void		rum_update_slot_cb(struct rum_softc *,
219ed5711a1SAdrian Chadd 			    union sec_param *, uint8_t);
220a19dbd8dSAdrian Chadd static void		rum_update_slot(struct ieee80211com *);
2211354b52cSAdrian Chadd static int		rum_wme_update(struct ieee80211com *);
22202ac6454SAndrew Thompson static void		rum_set_bssid(struct rum_softc *, const uint8_t *);
22302ac6454SAndrew Thompson static void		rum_set_macaddr(struct rum_softc *, const uint8_t *);
224272f6adeSGleb Smirnoff static void		rum_update_mcast(struct ieee80211com *);
225272f6adeSGleb Smirnoff static void		rum_update_promisc(struct ieee80211com *);
2265efea30fSAndrew Thompson static void		rum_setpromisc(struct rum_softc *);
22702ac6454SAndrew Thompson static const char	*rum_get_rf(int);
22802ac6454SAndrew Thompson static void		rum_read_eeprom(struct rum_softc *);
2296095f7caSAdrian Chadd static int		rum_bbp_wakeup(struct rum_softc *);
23002ac6454SAndrew Thompson static int		rum_bbp_init(struct rum_softc *);
2312cb9ef8dSAdrian Chadd static void		rum_clr_shkey_regs(struct rum_softc *);
2320698c0b3SAdrian Chadd static int		rum_init(struct rum_softc *);
2335efea30fSAndrew Thompson static void		rum_stop(struct rum_softc *);
23477ddf3d3SAndrew Thompson static void		rum_load_microcode(struct rum_softc *, const uint8_t *,
23502ac6454SAndrew Thompson 			    size_t);
236b780f864SAdrian Chadd static int		rum_set_beacon(struct rum_softc *,
237b780f864SAdrian Chadd 			    struct ieee80211vap *);
238b780f864SAdrian Chadd static int		rum_alloc_beacon(struct rum_softc *,
23902ac6454SAndrew Thompson 			    struct ieee80211vap *);
2400e3b4c60SAdrian Chadd static void		rum_update_beacon_cb(struct rum_softc *,
241ed5711a1SAdrian Chadd 			    union sec_param *, uint8_t);
2420e3b4c60SAdrian Chadd static void		rum_update_beacon(struct ieee80211vap *, int);
2432cb9ef8dSAdrian Chadd static int		rum_common_key_set(struct rum_softc *,
2442cb9ef8dSAdrian Chadd 			    struct ieee80211_key *, uint16_t);
2452cb9ef8dSAdrian Chadd static void		rum_group_key_set_cb(struct rum_softc *,
246ed5711a1SAdrian Chadd 			    union sec_param *, uint8_t);
2472cb9ef8dSAdrian Chadd static void		rum_group_key_del_cb(struct rum_softc *,
248ed5711a1SAdrian Chadd 			    union sec_param *, uint8_t);
2492cb9ef8dSAdrian Chadd static void		rum_pair_key_set_cb(struct rum_softc *,
250ed5711a1SAdrian Chadd 			    union sec_param *, uint8_t);
2512cb9ef8dSAdrian Chadd static void		rum_pair_key_del_cb(struct rum_softc *,
252ed5711a1SAdrian Chadd 			    union sec_param *, uint8_t);
2532cb9ef8dSAdrian Chadd static int		rum_key_alloc(struct ieee80211vap *,
2542cb9ef8dSAdrian Chadd 			    struct ieee80211_key *, ieee80211_keyix *,
2552cb9ef8dSAdrian Chadd 			    ieee80211_keyix *);
2562cb9ef8dSAdrian Chadd static int		rum_key_set(struct ieee80211vap *,
257bc813c40SAdrian Chadd 			    const struct ieee80211_key *);
2582cb9ef8dSAdrian Chadd static int		rum_key_delete(struct ieee80211vap *,
2592cb9ef8dSAdrian Chadd 			    const struct ieee80211_key *);
26002ac6454SAndrew Thompson static int		rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
26102ac6454SAndrew Thompson 			    const struct ieee80211_bpf_params *);
26202ac6454SAndrew Thompson static void		rum_scan_start(struct ieee80211com *);
26302ac6454SAndrew Thompson static void		rum_scan_end(struct ieee80211com *);
26402ac6454SAndrew Thompson static void		rum_set_channel(struct ieee80211com *);
26502ac6454SAndrew Thompson static int		rum_get_rssi(struct rum_softc *, uint8_t);
266b6108616SRui Paulo static void		rum_ratectl_start(struct rum_softc *,
26702ac6454SAndrew Thompson 			    struct ieee80211_node *);
268b6108616SRui Paulo static void		rum_ratectl_timeout(void *);
269b6108616SRui Paulo static void		rum_ratectl_task(void *, int);
27002ac6454SAndrew Thompson static int		rum_pause(struct rum_softc *, int);
27102ac6454SAndrew Thompson 
27202ac6454SAndrew Thompson static const struct {
27302ac6454SAndrew Thompson 	uint32_t	reg;
27402ac6454SAndrew Thompson 	uint32_t	val;
27502ac6454SAndrew Thompson } rum_def_mac[] = {
27602ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR0,  0x025fb032 },
27702ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR1,  0x9eaa9eaf },
27802ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR2,  0x8a8b8c8d },
27902ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR3,  0x00858687 },
28002ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR7,  0x2e31353b },
28102ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR8,  0x2a2a2a2c },
28202ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR15, 0x0000000f },
28302ac6454SAndrew Thompson 	{ RT2573_MAC_CSR6,   0x00000fff },
28402ac6454SAndrew Thompson 	{ RT2573_MAC_CSR8,   0x016c030a },
28502ac6454SAndrew Thompson 	{ RT2573_MAC_CSR10,  0x00000718 },
28602ac6454SAndrew Thompson 	{ RT2573_MAC_CSR12,  0x00000004 },
28702ac6454SAndrew Thompson 	{ RT2573_MAC_CSR13,  0x00007f00 },
2882cb9ef8dSAdrian Chadd 	{ RT2573_SEC_CSR2,   0x00000000 },
2892cb9ef8dSAdrian Chadd 	{ RT2573_SEC_CSR3,   0x00000000 },
2902cb9ef8dSAdrian Chadd 	{ RT2573_SEC_CSR4,   0x00000000 },
29102ac6454SAndrew Thompson 	{ RT2573_PHY_CSR1,   0x000023b0 },
29202ac6454SAndrew Thompson 	{ RT2573_PHY_CSR5,   0x00040a06 },
29302ac6454SAndrew Thompson 	{ RT2573_PHY_CSR6,   0x00080606 },
29402ac6454SAndrew Thompson 	{ RT2573_PHY_CSR7,   0x00000408 },
29502ac6454SAndrew Thompson 	{ RT2573_AIFSN_CSR,  0x00002273 },
29602ac6454SAndrew Thompson 	{ RT2573_CWMIN_CSR,  0x00002344 },
29702ac6454SAndrew Thompson 	{ RT2573_CWMAX_CSR,  0x000034aa }
29802ac6454SAndrew Thompson };
29902ac6454SAndrew Thompson 
30002ac6454SAndrew Thompson static const struct {
30102ac6454SAndrew Thompson 	uint8_t	reg;
30202ac6454SAndrew Thompson 	uint8_t	val;
30302ac6454SAndrew Thompson } rum_def_bbp[] = {
30402ac6454SAndrew Thompson 	{   3, 0x80 },
30502ac6454SAndrew Thompson 	{  15, 0x30 },
30602ac6454SAndrew Thompson 	{  17, 0x20 },
30702ac6454SAndrew Thompson 	{  21, 0xc8 },
30802ac6454SAndrew Thompson 	{  22, 0x38 },
30902ac6454SAndrew Thompson 	{  23, 0x06 },
31002ac6454SAndrew Thompson 	{  24, 0xfe },
31102ac6454SAndrew Thompson 	{  25, 0x0a },
31202ac6454SAndrew Thompson 	{  26, 0x0d },
31302ac6454SAndrew Thompson 	{  32, 0x0b },
31402ac6454SAndrew Thompson 	{  34, 0x12 },
31502ac6454SAndrew Thompson 	{  37, 0x07 },
31602ac6454SAndrew Thompson 	{  39, 0xf8 },
31702ac6454SAndrew Thompson 	{  41, 0x60 },
31802ac6454SAndrew Thompson 	{  53, 0x10 },
31902ac6454SAndrew Thompson 	{  54, 0x18 },
32002ac6454SAndrew Thompson 	{  60, 0x10 },
32102ac6454SAndrew Thompson 	{  61, 0x04 },
32202ac6454SAndrew Thompson 	{  62, 0x04 },
32302ac6454SAndrew Thompson 	{  75, 0xfe },
32402ac6454SAndrew Thompson 	{  86, 0xfe },
32502ac6454SAndrew Thompson 	{  88, 0xfe },
32602ac6454SAndrew Thompson 	{  90, 0x0f },
32702ac6454SAndrew Thompson 	{  99, 0x00 },
32802ac6454SAndrew Thompson 	{ 102, 0x16 },
32902ac6454SAndrew Thompson 	{ 107, 0x04 }
33002ac6454SAndrew Thompson };
33102ac6454SAndrew Thompson 
33202ac6454SAndrew Thompson static const struct rfprog {
33302ac6454SAndrew Thompson 	uint8_t		chan;
33402ac6454SAndrew Thompson 	uint32_t	r1, r2, r3, r4;
33502ac6454SAndrew Thompson }  rum_rf5226[] = {
33602ac6454SAndrew Thompson 	{   1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
33702ac6454SAndrew Thompson 	{   2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
33802ac6454SAndrew Thompson 	{   3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
33902ac6454SAndrew Thompson 	{   4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
34002ac6454SAndrew Thompson 	{   5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
34102ac6454SAndrew Thompson 	{   6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
34202ac6454SAndrew Thompson 	{   7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
34302ac6454SAndrew Thompson 	{   8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
34402ac6454SAndrew Thompson 	{   9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
34502ac6454SAndrew Thompson 	{  10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
34602ac6454SAndrew Thompson 	{  11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
34702ac6454SAndrew Thompson 	{  12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
34802ac6454SAndrew Thompson 	{  13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
34902ac6454SAndrew Thompson 	{  14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
35002ac6454SAndrew Thompson 
35102ac6454SAndrew Thompson 	{  34, 0x00b03, 0x20266, 0x36014, 0x30282 },
35202ac6454SAndrew Thompson 	{  38, 0x00b03, 0x20267, 0x36014, 0x30284 },
35302ac6454SAndrew Thompson 	{  42, 0x00b03, 0x20268, 0x36014, 0x30286 },
35402ac6454SAndrew Thompson 	{  46, 0x00b03, 0x20269, 0x36014, 0x30288 },
35502ac6454SAndrew Thompson 
35602ac6454SAndrew Thompson 	{  36, 0x00b03, 0x00266, 0x26014, 0x30288 },
35702ac6454SAndrew Thompson 	{  40, 0x00b03, 0x00268, 0x26014, 0x30280 },
35802ac6454SAndrew Thompson 	{  44, 0x00b03, 0x00269, 0x26014, 0x30282 },
35902ac6454SAndrew Thompson 	{  48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
36002ac6454SAndrew Thompson 	{  52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
36102ac6454SAndrew Thompson 	{  56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
36202ac6454SAndrew Thompson 	{  60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
36302ac6454SAndrew Thompson 	{  64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
36402ac6454SAndrew Thompson 
36502ac6454SAndrew Thompson 	{ 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
36602ac6454SAndrew Thompson 	{ 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
36702ac6454SAndrew Thompson 	{ 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
36802ac6454SAndrew Thompson 	{ 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
36902ac6454SAndrew Thompson 	{ 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
37002ac6454SAndrew Thompson 	{ 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
37102ac6454SAndrew Thompson 	{ 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
37202ac6454SAndrew Thompson 	{ 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
37302ac6454SAndrew Thompson 	{ 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
37402ac6454SAndrew Thompson 	{ 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
37502ac6454SAndrew Thompson 	{ 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
37602ac6454SAndrew Thompson 
37702ac6454SAndrew Thompson 	{ 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
37802ac6454SAndrew Thompson 	{ 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
37902ac6454SAndrew Thompson 	{ 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
38002ac6454SAndrew Thompson 	{ 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
38102ac6454SAndrew Thompson 	{ 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
38202ac6454SAndrew Thompson }, rum_rf5225[] = {
38302ac6454SAndrew Thompson 	{   1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
38402ac6454SAndrew Thompson 	{   2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
38502ac6454SAndrew Thompson 	{   3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
38602ac6454SAndrew Thompson 	{   4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
38702ac6454SAndrew Thompson 	{   5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
38802ac6454SAndrew Thompson 	{   6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
38902ac6454SAndrew Thompson 	{   7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
39002ac6454SAndrew Thompson 	{   8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
39102ac6454SAndrew Thompson 	{   9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
39202ac6454SAndrew Thompson 	{  10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
39302ac6454SAndrew Thompson 	{  11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
39402ac6454SAndrew Thompson 	{  12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
39502ac6454SAndrew Thompson 	{  13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
39602ac6454SAndrew Thompson 	{  14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
39702ac6454SAndrew Thompson 
39802ac6454SAndrew Thompson 	{  34, 0x00b33, 0x01266, 0x26014, 0x30282 },
39902ac6454SAndrew Thompson 	{  38, 0x00b33, 0x01267, 0x26014, 0x30284 },
40002ac6454SAndrew Thompson 	{  42, 0x00b33, 0x01268, 0x26014, 0x30286 },
40102ac6454SAndrew Thompson 	{  46, 0x00b33, 0x01269, 0x26014, 0x30288 },
40202ac6454SAndrew Thompson 
40302ac6454SAndrew Thompson 	{  36, 0x00b33, 0x01266, 0x26014, 0x30288 },
40402ac6454SAndrew Thompson 	{  40, 0x00b33, 0x01268, 0x26014, 0x30280 },
40502ac6454SAndrew Thompson 	{  44, 0x00b33, 0x01269, 0x26014, 0x30282 },
40602ac6454SAndrew Thompson 	{  48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
40702ac6454SAndrew Thompson 	{  52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
40802ac6454SAndrew Thompson 	{  56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
40902ac6454SAndrew Thompson 	{  60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
41002ac6454SAndrew Thompson 	{  64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
41102ac6454SAndrew Thompson 
41202ac6454SAndrew Thompson 	{ 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
41302ac6454SAndrew Thompson 	{ 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
41402ac6454SAndrew Thompson 	{ 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
41502ac6454SAndrew Thompson 	{ 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
41602ac6454SAndrew Thompson 	{ 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
41702ac6454SAndrew Thompson 	{ 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
41802ac6454SAndrew Thompson 	{ 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
41902ac6454SAndrew Thompson 	{ 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
42002ac6454SAndrew Thompson 	{ 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
42102ac6454SAndrew Thompson 	{ 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
42202ac6454SAndrew Thompson 	{ 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
42302ac6454SAndrew Thompson 
42402ac6454SAndrew Thompson 	{ 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
42502ac6454SAndrew Thompson 	{ 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
42602ac6454SAndrew Thompson 	{ 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
42702ac6454SAndrew Thompson 	{ 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
42802ac6454SAndrew Thompson 	{ 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
42902ac6454SAndrew Thompson };
43002ac6454SAndrew Thompson 
431760bc48eSAndrew Thompson static const struct usb_config rum_config[RUM_N_TRANSFER] = {
43202ac6454SAndrew Thompson 	[RUM_BULK_WR] = {
43302ac6454SAndrew Thompson 		.type = UE_BULK,
43402ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
43502ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
4364eae601eSAndrew Thompson 		.bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
4374eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
4384eae601eSAndrew Thompson 		.callback = rum_bulk_write_callback,
4394eae601eSAndrew Thompson 		.timeout = 5000,	/* ms */
44002ac6454SAndrew Thompson 	},
44102ac6454SAndrew Thompson 	[RUM_BULK_RD] = {
44202ac6454SAndrew Thompson 		.type = UE_BULK,
44302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
44402ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
4454eae601eSAndrew Thompson 		.bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
4464eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
4474eae601eSAndrew Thompson 		.callback = rum_bulk_read_callback,
44802ac6454SAndrew Thompson 	},
44902ac6454SAndrew Thompson };
45002ac6454SAndrew Thompson 
45102ac6454SAndrew Thompson static int
45202ac6454SAndrew Thompson rum_match(device_t self)
45302ac6454SAndrew Thompson {
454760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(self);
45502ac6454SAndrew Thompson 
456f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST)
45702ac6454SAndrew Thompson 		return (ENXIO);
45802ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != 0)
45902ac6454SAndrew Thompson 		return (ENXIO);
46002ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
46102ac6454SAndrew Thompson 		return (ENXIO);
46202ac6454SAndrew Thompson 
463a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
46402ac6454SAndrew Thompson }
46502ac6454SAndrew Thompson 
46602ac6454SAndrew Thompson static int
46702ac6454SAndrew Thompson rum_attach(device_t self)
46802ac6454SAndrew Thompson {
469760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(self);
47002ac6454SAndrew Thompson 	struct rum_softc *sc = device_get_softc(self);
4717a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
4725efea30fSAndrew Thompson 	uint32_t tmp;
4730046e186SAndriy Voskoboinyk 	uint8_t bands[howmany(IEEE80211_MODE_MAX, 8)];
4740046e186SAndriy Voskoboinyk 	uint8_t iface_index;
4755efea30fSAndrew Thompson 	int error, ntries;
47602ac6454SAndrew Thompson 
477a593f6b8SAndrew Thompson 	device_set_usb_desc(self);
47802ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
47902ac6454SAndrew Thompson 	sc->sc_dev = self;
48002ac6454SAndrew Thompson 
481f28c2f5eSAdrian Chadd 	RUM_LOCK_INIT(sc);
482f28c2f5eSAdrian Chadd 	RUM_CMDQ_LOCK_INIT(sc);
4837a79cebfSGleb Smirnoff 	mbufq_init(&sc->sc_snd, ifqmaxlen);
48402ac6454SAndrew Thompson 
48502ac6454SAndrew Thompson 	iface_index = RT2573_IFACE_INDEX;
486a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device, &iface_index,
48702ac6454SAndrew Thompson 	    sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
48802ac6454SAndrew Thompson 	if (error) {
48902ac6454SAndrew Thompson 		device_printf(self, "could not allocate USB transfers, "
490a593f6b8SAndrew Thompson 		    "err=%s\n", usbd_errstr(error));
49102ac6454SAndrew Thompson 		goto detach;
49202ac6454SAndrew Thompson 	}
49302ac6454SAndrew Thompson 
49402ac6454SAndrew Thompson 	RUM_LOCK(sc);
49502ac6454SAndrew Thompson 	/* retrieve RT2573 rev. no */
49602ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
49702ac6454SAndrew Thompson 		if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
49802ac6454SAndrew Thompson 			break;
49902ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
50002ac6454SAndrew Thompson 			break;
50102ac6454SAndrew Thompson 	}
50202ac6454SAndrew Thompson 	if (ntries == 100) {
50302ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
5045efea30fSAndrew Thompson 		RUM_UNLOCK(sc);
5055efea30fSAndrew Thompson 		goto detach;
50602ac6454SAndrew Thompson 	}
50702ac6454SAndrew Thompson 
50802ac6454SAndrew Thompson 	/* retrieve MAC address and various other things from EEPROM */
50902ac6454SAndrew Thompson 	rum_read_eeprom(sc);
51002ac6454SAndrew Thompson 
51102ac6454SAndrew Thompson 	device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
51202ac6454SAndrew Thompson 	    tmp, rum_get_rf(sc->rf_rev));
51302ac6454SAndrew Thompson 
51477ddf3d3SAndrew Thompson 	rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
51502ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
51602ac6454SAndrew Thompson 
51759686fe9SGleb Smirnoff 	ic->ic_softc = sc;
518c8550c02SGleb Smirnoff 	ic->ic_name = device_get_nameunit(self);
51902ac6454SAndrew Thompson 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
52002ac6454SAndrew Thompson 
52102ac6454SAndrew Thompson 	/* set device capabilities */
52202ac6454SAndrew Thompson 	ic->ic_caps =
52302ac6454SAndrew Thompson 	      IEEE80211_C_STA		/* station mode supported */
52402ac6454SAndrew Thompson 	    | IEEE80211_C_IBSS		/* IBSS mode supported */
52502ac6454SAndrew Thompson 	    | IEEE80211_C_MONITOR	/* monitor mode supported */
52602ac6454SAndrew Thompson 	    | IEEE80211_C_HOSTAP	/* HostAp mode supported */
52750a31b48SAdrian Chadd 	    | IEEE80211_C_AHDEMO	/* adhoc demo mode */
52802ac6454SAndrew Thompson 	    | IEEE80211_C_TXPMGT	/* tx power management */
52902ac6454SAndrew Thompson 	    | IEEE80211_C_SHPREAMBLE	/* short preamble supported */
53002ac6454SAndrew Thompson 	    | IEEE80211_C_SHSLOT	/* short slot time supported */
53102ac6454SAndrew Thompson 	    | IEEE80211_C_BGSCAN	/* bg scanning supported */
53202ac6454SAndrew Thompson 	    | IEEE80211_C_WPA		/* 802.11i */
5331354b52cSAdrian Chadd 	    | IEEE80211_C_WME		/* 802.11e */
53402ac6454SAndrew Thompson 	    ;
53502ac6454SAndrew Thompson 
5362cb9ef8dSAdrian Chadd 	ic->ic_cryptocaps =
5372cb9ef8dSAdrian Chadd 	    IEEE80211_CRYPTO_WEP |
5382cb9ef8dSAdrian Chadd 	    IEEE80211_CRYPTO_AES_CCM |
5392cb9ef8dSAdrian Chadd 	    IEEE80211_CRYPTO_TKIPMIC |
5402cb9ef8dSAdrian Chadd 	    IEEE80211_CRYPTO_TKIP;
5412cb9ef8dSAdrian Chadd 
5420046e186SAndriy Voskoboinyk 	memset(bands, 0, sizeof(bands));
5430046e186SAndriy Voskoboinyk 	setbit(bands, IEEE80211_MODE_11B);
5440046e186SAndriy Voskoboinyk 	setbit(bands, IEEE80211_MODE_11G);
54502ac6454SAndrew Thompson 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226)
5460046e186SAndriy Voskoboinyk 		setbit(bands, IEEE80211_MODE_11A);
5470046e186SAndriy Voskoboinyk 	ieee80211_init_channels(ic, NULL, bands);
54802ac6454SAndrew Thompson 
5497a79cebfSGleb Smirnoff 	ieee80211_ifattach(ic);
55077ddf3d3SAndrew Thompson 	ic->ic_update_promisc = rum_update_promisc;
55102ac6454SAndrew Thompson 	ic->ic_raw_xmit = rum_raw_xmit;
55202ac6454SAndrew Thompson 	ic->ic_scan_start = rum_scan_start;
55302ac6454SAndrew Thompson 	ic->ic_scan_end = rum_scan_end;
55402ac6454SAndrew Thompson 	ic->ic_set_channel = rum_set_channel;
5557a79cebfSGleb Smirnoff 	ic->ic_transmit = rum_transmit;
5567a79cebfSGleb Smirnoff 	ic->ic_parent = rum_parent;
55702ac6454SAndrew Thompson 	ic->ic_vap_create = rum_vap_create;
55802ac6454SAndrew Thompson 	ic->ic_vap_delete = rum_vap_delete;
559a19dbd8dSAdrian Chadd 	ic->ic_updateslot = rum_update_slot;
5601354b52cSAdrian Chadd 	ic->ic_wme.wme_update = rum_wme_update;
5616dd8e071SEd Maste 	ic->ic_update_mcast = rum_update_mcast;
56202ac6454SAndrew Thompson 
5635463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
5645463c4a4SSam Leffler 	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
5655463c4a4SSam Leffler 		RT2573_TX_RADIOTAP_PRESENT,
5665463c4a4SSam Leffler 	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
5675463c4a4SSam Leffler 		RT2573_RX_RADIOTAP_PRESENT);
56802ac6454SAndrew Thompson 
569f28c2f5eSAdrian Chadd 	TASK_INIT(&sc->cmdq_task, 0, rum_cmdq_cb, sc);
570f28c2f5eSAdrian Chadd 
57102ac6454SAndrew Thompson 	if (bootverbose)
57202ac6454SAndrew Thompson 		ieee80211_announce(ic);
57302ac6454SAndrew Thompson 
5745efea30fSAndrew Thompson 	return (0);
5755efea30fSAndrew Thompson 
5765efea30fSAndrew Thompson detach:
5775efea30fSAndrew Thompson 	rum_detach(self);
5785efea30fSAndrew Thompson 	return (ENXIO);			/* failure */
57902ac6454SAndrew Thompson }
58002ac6454SAndrew Thompson 
58102ac6454SAndrew Thompson static int
58202ac6454SAndrew Thompson rum_detach(device_t self)
58302ac6454SAndrew Thompson {
58402ac6454SAndrew Thompson 	struct rum_softc *sc = device_get_softc(self);
585e7b7db3dSAdrian Chadd 	struct ieee80211com *ic = &sc->sc_ic;
58602ac6454SAndrew Thompson 
587645e4d17SHans Petter Selasky 	/* Prevent further ioctls */
588645e4d17SHans Petter Selasky 	RUM_LOCK(sc);
589645e4d17SHans Petter Selasky 	sc->sc_detached = 1;
590645e4d17SHans Petter Selasky 	RUM_UNLOCK(sc);
591645e4d17SHans Petter Selasky 
59202ac6454SAndrew Thompson 	/* stop all USB transfers */
593a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
59402ac6454SAndrew Thompson 
59502ac6454SAndrew Thompson 	/* free TX list, if any */
59602ac6454SAndrew Thompson 	RUM_LOCK(sc);
59702ac6454SAndrew Thompson 	rum_unsetup_tx_list(sc);
59802ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
59902ac6454SAndrew Thompson 
600f28c2f5eSAdrian Chadd 	if (ic->ic_softc == sc) {
601f28c2f5eSAdrian Chadd 		ieee80211_draintask(ic, &sc->cmdq_task);
602e7b7db3dSAdrian Chadd 		ieee80211_ifdetach(ic);
603f28c2f5eSAdrian Chadd 	}
604f28c2f5eSAdrian Chadd 
6057a79cebfSGleb Smirnoff 	mbufq_drain(&sc->sc_snd);
606f28c2f5eSAdrian Chadd 	RUM_CMDQ_LOCK_DESTROY(sc);
607f28c2f5eSAdrian Chadd 	RUM_LOCK_DESTROY(sc);
608f28c2f5eSAdrian Chadd 
60902ac6454SAndrew Thompson 	return (0);
61002ac6454SAndrew Thompson }
61102ac6454SAndrew Thompson 
612e0a69b51SAndrew Thompson static usb_error_t
61377ddf3d3SAndrew Thompson rum_do_request(struct rum_softc *sc,
614760bc48eSAndrew Thompson     struct usb_device_request *req, void *data)
61577ddf3d3SAndrew Thompson {
616e0a69b51SAndrew Thompson 	usb_error_t err;
61777ddf3d3SAndrew Thompson 	int ntries = 10;
61877ddf3d3SAndrew Thompson 
61977ddf3d3SAndrew Thompson 	while (ntries--) {
620a593f6b8SAndrew Thompson 		err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
62177ddf3d3SAndrew Thompson 		    req, data, 0, NULL, 250 /* ms */);
62277ddf3d3SAndrew Thompson 		if (err == 0)
62377ddf3d3SAndrew Thompson 			break;
62477ddf3d3SAndrew Thompson 
62577ddf3d3SAndrew Thompson 		DPRINTFN(1, "Control request failed, %s (retrying)\n",
626a593f6b8SAndrew Thompson 		    usbd_errstr(err));
62777ddf3d3SAndrew Thompson 		if (rum_pause(sc, hz / 100))
62877ddf3d3SAndrew Thompson 			break;
62977ddf3d3SAndrew Thompson 	}
63077ddf3d3SAndrew Thompson 	return (err);
63177ddf3d3SAndrew Thompson }
63277ddf3d3SAndrew Thompson 
633fd7dae9cSAndriy Voskoboinyk static usb_error_t
634fd7dae9cSAndriy Voskoboinyk rum_do_mcu_request(struct rum_softc *sc, int request)
635fd7dae9cSAndriy Voskoboinyk {
636fd7dae9cSAndriy Voskoboinyk 	struct usb_device_request req;
637fd7dae9cSAndriy Voskoboinyk 
638fd7dae9cSAndriy Voskoboinyk 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
639fd7dae9cSAndriy Voskoboinyk 	req.bRequest = RT2573_MCU_CNTL;
640fd7dae9cSAndriy Voskoboinyk 	USETW(req.wValue, request);
641fd7dae9cSAndriy Voskoboinyk 	USETW(req.wIndex, 0);
642fd7dae9cSAndriy Voskoboinyk 	USETW(req.wLength, 0);
643fd7dae9cSAndriy Voskoboinyk 
644fd7dae9cSAndriy Voskoboinyk 	return (rum_do_request(sc, &req, NULL));
645fd7dae9cSAndriy Voskoboinyk }
646fd7dae9cSAndriy Voskoboinyk 
64702ac6454SAndrew Thompson static struct ieee80211vap *
648fcd9500fSBernhard Schmidt rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
649fcd9500fSBernhard Schmidt     enum ieee80211_opmode opmode, int flags,
65002ac6454SAndrew Thompson     const uint8_t bssid[IEEE80211_ADDR_LEN],
65102ac6454SAndrew Thompson     const uint8_t mac[IEEE80211_ADDR_LEN])
65202ac6454SAndrew Thompson {
653d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
65402ac6454SAndrew Thompson 	struct rum_vap *rvp;
65502ac6454SAndrew Thompson 	struct ieee80211vap *vap;
65602ac6454SAndrew Thompson 
65702ac6454SAndrew Thompson 	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
65802ac6454SAndrew Thompson 		return NULL;
6597a79cebfSGleb Smirnoff 	rvp = malloc(sizeof(struct rum_vap), M_80211_VAP, M_WAITOK | M_ZERO);
66002ac6454SAndrew Thompson 	vap = &rvp->vap;
66102ac6454SAndrew Thompson 	/* enable s/w bmiss handling for sta mode */
662bb2f69e8SHans Petter Selasky 
663bb2f69e8SHans Petter Selasky 	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
6647a79cebfSGleb Smirnoff 	    flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
665bb2f69e8SHans Petter Selasky 		/* out of memory */
666bb2f69e8SHans Petter Selasky 		free(rvp, M_80211_VAP);
667bb2f69e8SHans Petter Selasky 		return (NULL);
668bb2f69e8SHans Petter Selasky 	}
66902ac6454SAndrew Thompson 
67002ac6454SAndrew Thompson 	/* override state transition machine */
67102ac6454SAndrew Thompson 	rvp->newstate = vap->iv_newstate;
67202ac6454SAndrew Thompson 	vap->iv_newstate = rum_newstate;
6732cb9ef8dSAdrian Chadd 	vap->iv_key_alloc = rum_key_alloc;
6742cb9ef8dSAdrian Chadd 	vap->iv_key_set = rum_key_set;
6752cb9ef8dSAdrian Chadd 	vap->iv_key_delete = rum_key_delete;
6760e3b4c60SAdrian Chadd 	vap->iv_update_beacon = rum_update_beacon;
6772cb9ef8dSAdrian Chadd 	vap->iv_max_aid = RT2573_ADDR_MAX;
67802ac6454SAndrew Thompson 
679b6108616SRui Paulo 	usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0);
680b6108616SRui Paulo 	TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
681b6108616SRui Paulo 	ieee80211_ratectl_init(vap);
682b6108616SRui Paulo 	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
68302ac6454SAndrew Thompson 	/* complete setup */
6847a79cebfSGleb Smirnoff 	ieee80211_vap_attach(vap, ieee80211_media_change,
6857a79cebfSGleb Smirnoff 	    ieee80211_media_status, mac);
68602ac6454SAndrew Thompson 	ic->ic_opmode = opmode;
68702ac6454SAndrew Thompson 	return vap;
68802ac6454SAndrew Thompson }
68902ac6454SAndrew Thompson 
69002ac6454SAndrew Thompson static void
69102ac6454SAndrew Thompson rum_vap_delete(struct ieee80211vap *vap)
69202ac6454SAndrew Thompson {
69302ac6454SAndrew Thompson 	struct rum_vap *rvp = RUM_VAP(vap);
6945efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
69502ac6454SAndrew Thompson 
696b780f864SAdrian Chadd 	m_freem(rvp->bcn_mbuf);
697b6108616SRui Paulo 	usb_callout_drain(&rvp->ratectl_ch);
698b6108616SRui Paulo 	ieee80211_draintask(ic, &rvp->ratectl_task);
699b6108616SRui Paulo 	ieee80211_ratectl_deinit(vap);
70002ac6454SAndrew Thompson 	ieee80211_vap_detach(vap);
70102ac6454SAndrew Thompson 	free(rvp, M_80211_VAP);
70202ac6454SAndrew Thompson }
70302ac6454SAndrew Thompson 
70402ac6454SAndrew Thompson static void
705f28c2f5eSAdrian Chadd rum_cmdq_cb(void *arg, int pending)
706f28c2f5eSAdrian Chadd {
707f28c2f5eSAdrian Chadd 	struct rum_softc *sc = arg;
708f28c2f5eSAdrian Chadd 	struct rum_cmdq *rc;
709f28c2f5eSAdrian Chadd 
710f28c2f5eSAdrian Chadd 	RUM_CMDQ_LOCK(sc);
711f28c2f5eSAdrian Chadd 	while (sc->cmdq[sc->cmdq_first].func != NULL) {
712f28c2f5eSAdrian Chadd 		rc = &sc->cmdq[sc->cmdq_first];
713f28c2f5eSAdrian Chadd 		RUM_CMDQ_UNLOCK(sc);
714f28c2f5eSAdrian Chadd 
715f28c2f5eSAdrian Chadd 		RUM_LOCK(sc);
716ed5711a1SAdrian Chadd 		rc->func(sc, &rc->data, rc->rvp_id);
717f28c2f5eSAdrian Chadd 		RUM_UNLOCK(sc);
718f28c2f5eSAdrian Chadd 
719f28c2f5eSAdrian Chadd 		RUM_CMDQ_LOCK(sc);
720f28c2f5eSAdrian Chadd 		memset(rc, 0, sizeof (*rc));
721f28c2f5eSAdrian Chadd 		sc->cmdq_first = (sc->cmdq_first + 1) % RUM_CMDQ_SIZE;
722f28c2f5eSAdrian Chadd 	}
723f28c2f5eSAdrian Chadd 	RUM_CMDQ_UNLOCK(sc);
724f28c2f5eSAdrian Chadd }
725f28c2f5eSAdrian Chadd 
726f28c2f5eSAdrian Chadd static int
727f28c2f5eSAdrian Chadd rum_cmd_sleepable(struct rum_softc *sc, const void *ptr, size_t len,
728ed5711a1SAdrian Chadd     uint8_t rvp_id, CMD_FUNC_PROTO)
729f28c2f5eSAdrian Chadd {
730f28c2f5eSAdrian Chadd 	struct ieee80211com *ic = &sc->sc_ic;
731f28c2f5eSAdrian Chadd 
732f28c2f5eSAdrian Chadd 	KASSERT(len <= sizeof(union sec_param), ("buffer overflow"));
733f28c2f5eSAdrian Chadd 
734f28c2f5eSAdrian Chadd 	RUM_CMDQ_LOCK(sc);
735f28c2f5eSAdrian Chadd 	if (sc->cmdq[sc->cmdq_last].func != NULL) {
736f28c2f5eSAdrian Chadd 		device_printf(sc->sc_dev, "%s: cmdq overflow\n", __func__);
737f28c2f5eSAdrian Chadd 		RUM_CMDQ_UNLOCK(sc);
738f28c2f5eSAdrian Chadd 
739f28c2f5eSAdrian Chadd 		return EAGAIN;
740f28c2f5eSAdrian Chadd 	}
741f28c2f5eSAdrian Chadd 
742f28c2f5eSAdrian Chadd 	if (ptr != NULL)
743f28c2f5eSAdrian Chadd 		memcpy(&sc->cmdq[sc->cmdq_last].data, ptr, len);
744f28c2f5eSAdrian Chadd 	sc->cmdq[sc->cmdq_last].rvp_id = rvp_id;
745f28c2f5eSAdrian Chadd 	sc->cmdq[sc->cmdq_last].func = func;
746f28c2f5eSAdrian Chadd 	sc->cmdq_last = (sc->cmdq_last + 1) % RUM_CMDQ_SIZE;
747f28c2f5eSAdrian Chadd 	RUM_CMDQ_UNLOCK(sc);
748f28c2f5eSAdrian Chadd 
749f28c2f5eSAdrian Chadd 	ieee80211_runtask(ic, &sc->cmdq_task);
750f28c2f5eSAdrian Chadd 
751f28c2f5eSAdrian Chadd 	return 0;
752f28c2f5eSAdrian Chadd }
753f28c2f5eSAdrian Chadd 
754f28c2f5eSAdrian Chadd static void
75502ac6454SAndrew Thompson rum_tx_free(struct rum_tx_data *data, int txerr)
75602ac6454SAndrew Thompson {
75702ac6454SAndrew Thompson 	struct rum_softc *sc = data->sc;
75802ac6454SAndrew Thompson 
75902ac6454SAndrew Thompson 	if (data->m != NULL) {
7607a79cebfSGleb Smirnoff 		ieee80211_tx_complete(data->ni, data->m, txerr);
76102ac6454SAndrew Thompson 		data->m = NULL;
76202ac6454SAndrew Thompson 		data->ni = NULL;
76302ac6454SAndrew Thompson 	}
76402ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
76502ac6454SAndrew Thompson 	sc->tx_nfree++;
76602ac6454SAndrew Thompson }
76702ac6454SAndrew Thompson 
76802ac6454SAndrew Thompson static void
76902ac6454SAndrew Thompson rum_setup_tx_list(struct rum_softc *sc)
77002ac6454SAndrew Thompson {
77102ac6454SAndrew Thompson 	struct rum_tx_data *data;
77202ac6454SAndrew Thompson 	int i;
77302ac6454SAndrew Thompson 
77402ac6454SAndrew Thompson 	sc->tx_nfree = 0;
77502ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_q);
77602ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_free);
77702ac6454SAndrew Thompson 
77802ac6454SAndrew Thompson 	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
77902ac6454SAndrew Thompson 		data = &sc->tx_data[i];
78002ac6454SAndrew Thompson 
78102ac6454SAndrew Thompson 		data->sc = sc;
78202ac6454SAndrew Thompson 		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
78302ac6454SAndrew Thompson 		sc->tx_nfree++;
78402ac6454SAndrew Thompson 	}
78502ac6454SAndrew Thompson }
78602ac6454SAndrew Thompson 
78702ac6454SAndrew Thompson static void
78802ac6454SAndrew Thompson rum_unsetup_tx_list(struct rum_softc *sc)
78902ac6454SAndrew Thompson {
79002ac6454SAndrew Thompson 	struct rum_tx_data *data;
79102ac6454SAndrew Thompson 	int i;
79202ac6454SAndrew Thompson 
79302ac6454SAndrew Thompson 	/* make sure any subsequent use of the queues will fail */
79402ac6454SAndrew Thompson 	sc->tx_nfree = 0;
79502ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_q);
79602ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_free);
79702ac6454SAndrew Thompson 
79802ac6454SAndrew Thompson 	/* free up all node references and mbufs */
79902ac6454SAndrew Thompson 	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
80002ac6454SAndrew Thompson 		data = &sc->tx_data[i];
80102ac6454SAndrew Thompson 
80202ac6454SAndrew Thompson 		if (data->m != NULL) {
80302ac6454SAndrew Thompson 			m_freem(data->m);
80402ac6454SAndrew Thompson 			data->m = NULL;
80502ac6454SAndrew Thompson 		}
80602ac6454SAndrew Thompson 		if (data->ni != NULL) {
80702ac6454SAndrew Thompson 			ieee80211_free_node(data->ni);
80802ac6454SAndrew Thompson 			data->ni = NULL;
80902ac6454SAndrew Thompson 		}
81002ac6454SAndrew Thompson 	}
81102ac6454SAndrew Thompson }
81202ac6454SAndrew Thompson 
8135efea30fSAndrew Thompson static int
8145efea30fSAndrew Thompson rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
81502ac6454SAndrew Thompson {
81602ac6454SAndrew Thompson 	struct rum_vap *rvp = RUM_VAP(vap);
8175efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
818d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
81902ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
82002ac6454SAndrew Thompson 	enum ieee80211_state ostate;
82102ac6454SAndrew Thompson 	struct ieee80211_node *ni;
822*96e29c26SAndriy Voskoboinyk 	int ret = 0;
82302ac6454SAndrew Thompson 
82402ac6454SAndrew Thompson 	ostate = vap->iv_state;
8255efea30fSAndrew Thompson 	DPRINTF("%s -> %s\n",
8265efea30fSAndrew Thompson 		ieee80211_state_name[ostate],
8275efea30fSAndrew Thompson 		ieee80211_state_name[nstate]);
82802ac6454SAndrew Thompson 
8295efea30fSAndrew Thompson 	IEEE80211_UNLOCK(ic);
8305efea30fSAndrew Thompson 	RUM_LOCK(sc);
831b6108616SRui Paulo 	usb_callout_stop(&rvp->ratectl_ch);
8325efea30fSAndrew Thompson 
8335efea30fSAndrew Thompson 	switch (nstate) {
83402ac6454SAndrew Thompson 	case IEEE80211_S_INIT:
8356d804321SAdrian Chadd 		if (ostate == IEEE80211_S_RUN)
8366d804321SAdrian Chadd 			rum_abort_tsf_sync(sc);
8376d804321SAdrian Chadd 
83802ac6454SAndrew Thompson 		break;
83902ac6454SAndrew Thompson 
84002ac6454SAndrew Thompson 	case IEEE80211_S_RUN:
8418d8bdb01SAndrew Thompson 		ni = ieee80211_ref_node(vap->iv_bss);
84202ac6454SAndrew Thompson 
84302ac6454SAndrew Thompson 		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
844342ced03SAdrian Chadd 			if (ic->ic_bsschan == IEEE80211_CHAN_ANYC ||
845342ced03SAdrian Chadd 			    ni->ni_chan == IEEE80211_CHAN_ANYC) {
846aa81f853SAdrian Chadd 				ret = EINVAL;
847aa81f853SAdrian Chadd 				goto run_fail;
848bb571462SHans Petter Selasky 			}
849ed5711a1SAdrian Chadd 			rum_update_slot_cb(sc, NULL, 0);
85002ac6454SAndrew Thompson 			rum_enable_mrr(sc);
85102ac6454SAndrew Thompson 			rum_set_txpreamble(sc);
85202ac6454SAndrew Thompson 			rum_set_basicrates(sc);
853342ced03SAdrian Chadd 			rum_set_maxretry(sc, vap);
8541ba67b11SAdrian Chadd 			IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
8551ba67b11SAdrian Chadd 			rum_set_bssid(sc, sc->sc_bssid);
85602ac6454SAndrew Thompson 		}
85702ac6454SAndrew Thompson 
85802ac6454SAndrew Thompson 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
859aa81f853SAdrian Chadd 		    vap->iv_opmode == IEEE80211_M_IBSS) {
860b780f864SAdrian Chadd 			if ((ret = rum_alloc_beacon(sc, vap)) != 0)
861aa81f853SAdrian Chadd 				goto run_fail;
862aa81f853SAdrian Chadd 		}
86302ac6454SAndrew Thompson 
86450a31b48SAdrian Chadd 		if (vap->iv_opmode != IEEE80211_M_MONITOR &&
86550a31b48SAdrian Chadd 		    vap->iv_opmode != IEEE80211_M_AHDEMO) {
866aa81f853SAdrian Chadd 			if ((ret = rum_enable_tsf_sync(sc)) != 0)
867aa81f853SAdrian Chadd 				goto run_fail;
868aa81f853SAdrian Chadd 		} else
8695463c4a4SSam Leffler 			rum_enable_tsf(sc);
87002ac6454SAndrew Thompson 
87102ac6454SAndrew Thompson 		/* enable automatic rate adaptation */
8725463c4a4SSam Leffler 		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
87302ac6454SAndrew Thompson 		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
874b6108616SRui Paulo 			rum_ratectl_start(sc, ni);
875*96e29c26SAndriy Voskoboinyk run_fail:
8768d8bdb01SAndrew Thompson 		ieee80211_free_node(ni);
87702ac6454SAndrew Thompson 		break;
87802ac6454SAndrew Thompson 	default:
87902ac6454SAndrew Thompson 		break;
88002ac6454SAndrew Thompson 	}
88102ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
88202ac6454SAndrew Thompson 	IEEE80211_LOCK(ic);
883*96e29c26SAndriy Voskoboinyk 	return (ret == 0 ? rvp->newstate(vap, nstate, arg) : ret);
88402ac6454SAndrew Thompson }
88502ac6454SAndrew Thompson 
88602ac6454SAndrew Thompson static void
887ed6d949aSAndrew Thompson rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
88802ac6454SAndrew Thompson {
889ed6d949aSAndrew Thompson 	struct rum_softc *sc = usbd_xfer_softc(xfer);
8905463c4a4SSam Leffler 	struct ieee80211vap *vap;
89102ac6454SAndrew Thompson 	struct rum_tx_data *data;
89202ac6454SAndrew Thompson 	struct mbuf *m;
893ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
89402ac6454SAndrew Thompson 	unsigned int len;
895ed6d949aSAndrew Thompson 	int actlen, sumlen;
896ed6d949aSAndrew Thompson 
897ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
89802ac6454SAndrew Thompson 
89902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
90002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
901ed6d949aSAndrew Thompson 		DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
90202ac6454SAndrew Thompson 
90302ac6454SAndrew Thompson 		/* free resources */
904ed6d949aSAndrew Thompson 		data = usbd_xfer_get_priv(xfer);
90502ac6454SAndrew Thompson 		rum_tx_free(data, 0);
906ed6d949aSAndrew Thompson 		usbd_xfer_set_priv(xfer, NULL);
90702ac6454SAndrew Thompson 
90802ac6454SAndrew Thompson 		/* FALLTHROUGH */
90902ac6454SAndrew Thompson 	case USB_ST_SETUP:
91002ac6454SAndrew Thompson tr_setup:
91102ac6454SAndrew Thompson 		data = STAILQ_FIRST(&sc->tx_q);
91202ac6454SAndrew Thompson 		if (data) {
91302ac6454SAndrew Thompson 			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
91402ac6454SAndrew Thompson 			m = data->m;
91502ac6454SAndrew Thompson 
9166d917491SHans Petter Selasky 			if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
91702ac6454SAndrew Thompson 				DPRINTFN(0, "data overflow, %u bytes\n",
91802ac6454SAndrew Thompson 				    m->m_pkthdr.len);
91902ac6454SAndrew Thompson 				m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
92002ac6454SAndrew Thompson 			}
921ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
922ed6d949aSAndrew Thompson 			usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
923ed6d949aSAndrew Thompson 			usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
924ed6d949aSAndrew Thompson 			    m->m_pkthdr.len);
92502ac6454SAndrew Thompson 
9265463c4a4SSam Leffler 			vap = data->ni->ni_vap;
9275463c4a4SSam Leffler 			if (ieee80211_radiotap_active_vap(vap)) {
92802ac6454SAndrew Thompson 				struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
92902ac6454SAndrew Thompson 
93002ac6454SAndrew Thompson 				tap->wt_flags = 0;
93102ac6454SAndrew Thompson 				tap->wt_rate = data->rate;
932a6ccd477SAdrian Chadd 				rum_get_tsf(sc, &tap->wt_tsf);
93302ac6454SAndrew Thompson 				tap->wt_antenna = sc->tx_ant;
93402ac6454SAndrew Thompson 
9355463c4a4SSam Leffler 				ieee80211_radiotap_tx(vap, m);
93602ac6454SAndrew Thompson 			}
93702ac6454SAndrew Thompson 
93802ac6454SAndrew Thompson 			/* align end on a 4-bytes boundary */
93902ac6454SAndrew Thompson 			len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
94002ac6454SAndrew Thompson 			if ((len % 64) == 0)
94102ac6454SAndrew Thompson 				len += 4;
94202ac6454SAndrew Thompson 
94302ac6454SAndrew Thompson 			DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
94402ac6454SAndrew Thompson 			    m->m_pkthdr.len, len);
94502ac6454SAndrew Thompson 
946ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, len);
947ed6d949aSAndrew Thompson 			usbd_xfer_set_priv(xfer, data);
94802ac6454SAndrew Thompson 
949a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
95002ac6454SAndrew Thompson 		}
9517a79cebfSGleb Smirnoff 		rum_start(sc);
95202ac6454SAndrew Thompson 		break;
95302ac6454SAndrew Thompson 
95402ac6454SAndrew Thompson 	default:			/* Error */
95502ac6454SAndrew Thompson 		DPRINTFN(11, "transfer error, %s\n",
956ed6d949aSAndrew Thompson 		    usbd_errstr(error));
95702ac6454SAndrew Thompson 
9587a79cebfSGleb Smirnoff 		counter_u64_add(sc->sc_ic.ic_oerrors, 1);
959ed6d949aSAndrew Thompson 		data = usbd_xfer_get_priv(xfer);
96002ac6454SAndrew Thompson 		if (data != NULL) {
961ed6d949aSAndrew Thompson 			rum_tx_free(data, error);
962ed6d949aSAndrew Thompson 			usbd_xfer_set_priv(xfer, NULL);
96302ac6454SAndrew Thompson 		}
96402ac6454SAndrew Thompson 
9650283fab7SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
9660283fab7SAndrew Thompson 			if (error == USB_ERR_TIMEOUT)
9670283fab7SAndrew Thompson 				device_printf(sc->sc_dev, "device timeout\n");
9680283fab7SAndrew Thompson 
9690283fab7SAndrew Thompson 			/*
9700283fab7SAndrew Thompson 			 * Try to clear stall first, also if other
9710283fab7SAndrew Thompson 			 * errors occur, hence clearing stall
9720283fab7SAndrew Thompson 			 * introduces a 50 ms delay:
9730283fab7SAndrew Thompson 			 */
974ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
97502ac6454SAndrew Thompson 			goto tr_setup;
97602ac6454SAndrew Thompson 		}
97702ac6454SAndrew Thompson 		break;
97802ac6454SAndrew Thompson 	}
97902ac6454SAndrew Thompson }
98002ac6454SAndrew Thompson 
98102ac6454SAndrew Thompson static void
982ed6d949aSAndrew Thompson rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
98302ac6454SAndrew Thompson {
984ed6d949aSAndrew Thompson 	struct rum_softc *sc = usbd_xfer_softc(xfer);
9857a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
986aca2cf30SAdrian Chadd 	struct ieee80211_frame_min *wh;
98702ac6454SAndrew Thompson 	struct ieee80211_node *ni;
98802ac6454SAndrew Thompson 	struct mbuf *m = NULL;
989ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
99002ac6454SAndrew Thompson 	uint32_t flags;
99102ac6454SAndrew Thompson 	uint8_t rssi = 0;
992ed6d949aSAndrew Thompson 	int len;
993ed6d949aSAndrew Thompson 
994ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
99502ac6454SAndrew Thompson 
99602ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
99702ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
99802ac6454SAndrew Thompson 
999ed6d949aSAndrew Thompson 		DPRINTFN(15, "rx done, actlen=%d\n", len);
100002ac6454SAndrew Thompson 
10016d917491SHans Petter Selasky 		if (len < (int)(RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN)) {
100202ac6454SAndrew Thompson 			DPRINTF("%s: xfer too short %d\n",
100302ac6454SAndrew Thompson 			    device_get_nameunit(sc->sc_dev), len);
10047a79cebfSGleb Smirnoff 			counter_u64_add(ic->ic_ierrors, 1);
100502ac6454SAndrew Thompson 			goto tr_setup;
100602ac6454SAndrew Thompson 		}
100702ac6454SAndrew Thompson 
100802ac6454SAndrew Thompson 		len -= RT2573_RX_DESC_SIZE;
1009ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
1010ed6d949aSAndrew Thompson 		usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
101102ac6454SAndrew Thompson 
101202ac6454SAndrew Thompson 		rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
101302ac6454SAndrew Thompson 		flags = le32toh(sc->sc_rx_desc.flags);
101402ac6454SAndrew Thompson 		if (flags & RT2573_RX_CRC_ERROR) {
101502ac6454SAndrew Thompson 			/*
101602ac6454SAndrew Thompson 		         * This should not happen since we did not
101702ac6454SAndrew Thompson 		         * request to receive those frames when we
101802ac6454SAndrew Thompson 		         * filled RUM_TXRX_CSR2:
101902ac6454SAndrew Thompson 		         */
102002ac6454SAndrew Thompson 			DPRINTFN(5, "PHY or CRC error\n");
10217a79cebfSGleb Smirnoff 			counter_u64_add(ic->ic_ierrors, 1);
102202ac6454SAndrew Thompson 			goto tr_setup;
102302ac6454SAndrew Thompson 		}
10242cb9ef8dSAdrian Chadd 		if ((flags & RT2573_RX_DEC_MASK) != RT2573_RX_DEC_OK) {
10252cb9ef8dSAdrian Chadd 			switch (flags & RT2573_RX_DEC_MASK) {
10262cb9ef8dSAdrian Chadd 			case RT2573_RX_IV_ERROR:
10272cb9ef8dSAdrian Chadd 				DPRINTFN(5, "IV/EIV error\n");
10282cb9ef8dSAdrian Chadd 				break;
10292cb9ef8dSAdrian Chadd 			case RT2573_RX_MIC_ERROR:
10302cb9ef8dSAdrian Chadd 				DPRINTFN(5, "MIC error\n");
10312cb9ef8dSAdrian Chadd 				break;
10322cb9ef8dSAdrian Chadd 			case RT2573_RX_KEY_ERROR:
10332cb9ef8dSAdrian Chadd 				DPRINTFN(5, "Key error\n");
10342cb9ef8dSAdrian Chadd 				break;
10352cb9ef8dSAdrian Chadd 			}
10362cb9ef8dSAdrian Chadd 			counter_u64_add(ic->ic_ierrors, 1);
10372cb9ef8dSAdrian Chadd 			goto tr_setup;
10382cb9ef8dSAdrian Chadd 		}
103902ac6454SAndrew Thompson 
1040c6499eccSGleb Smirnoff 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
104102ac6454SAndrew Thompson 		if (m == NULL) {
104202ac6454SAndrew Thompson 			DPRINTF("could not allocate mbuf\n");
10437a79cebfSGleb Smirnoff 			counter_u64_add(ic->ic_ierrors, 1);
104402ac6454SAndrew Thompson 			goto tr_setup;
104502ac6454SAndrew Thompson 		}
1046ed6d949aSAndrew Thompson 		usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
104702ac6454SAndrew Thompson 		    mtod(m, uint8_t *), len);
104802ac6454SAndrew Thompson 
1049aca2cf30SAdrian Chadd 		wh = mtod(m, struct ieee80211_frame_min *);
1050aca2cf30SAdrian Chadd 
10512cb9ef8dSAdrian Chadd 		if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
10522cb9ef8dSAdrian Chadd 		    (flags & RT2573_RX_CIP_MASK) !=
10532cb9ef8dSAdrian Chadd 		     RT2573_RX_CIP_MODE(RT2573_MODE_NOSEC)) {
10542cb9ef8dSAdrian Chadd 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
10552cb9ef8dSAdrian Chadd 			m->m_flags |= M_WEP;
10562cb9ef8dSAdrian Chadd 		}
10572cb9ef8dSAdrian Chadd 
105802ac6454SAndrew Thompson 		/* finalize mbuf */
105902ac6454SAndrew Thompson 		m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
106002ac6454SAndrew Thompson 
10615463c4a4SSam Leffler 		if (ieee80211_radiotap_active(ic)) {
106202ac6454SAndrew Thompson 			struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
106302ac6454SAndrew Thompson 
10645463c4a4SSam Leffler 			tap->wr_flags = 0;
106502ac6454SAndrew Thompson 			tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
106602ac6454SAndrew Thompson 			    (flags & RT2573_RX_OFDM) ?
106702ac6454SAndrew Thompson 			    IEEE80211_T_OFDM : IEEE80211_T_CCK);
1068a6ccd477SAdrian Chadd 			rum_get_tsf(sc, &tap->wr_tsf);
10695463c4a4SSam Leffler 			tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
10705463c4a4SSam Leffler 			tap->wr_antnoise = RT2573_NOISE_FLOOR;
107102ac6454SAndrew Thompson 			tap->wr_antenna = sc->rx_ant;
107202ac6454SAndrew Thompson 		}
107302ac6454SAndrew Thompson 		/* FALLTHROUGH */
107402ac6454SAndrew Thompson 	case USB_ST_SETUP:
107502ac6454SAndrew Thompson tr_setup:
1076ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1077a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
107802ac6454SAndrew Thompson 
107902ac6454SAndrew Thompson 		/*
108002ac6454SAndrew Thompson 		 * At the end of a USB callback it is always safe to unlock
108102ac6454SAndrew Thompson 		 * the private mutex of a device! That is why we do the
108202ac6454SAndrew Thompson 		 * "ieee80211_input" here, and not some lines up!
108302ac6454SAndrew Thompson 		 */
108402ac6454SAndrew Thompson 		RUM_UNLOCK(sc);
1085db63fc93SWeongyo Jeong 		if (m) {
1086aca2cf30SAdrian Chadd 			if (m->m_len >= sizeof(struct ieee80211_frame_min))
1087aca2cf30SAdrian Chadd 				ni = ieee80211_find_rxnode(ic, wh);
1088aca2cf30SAdrian Chadd 			else
1089aca2cf30SAdrian Chadd 				ni = NULL;
1090aca2cf30SAdrian Chadd 
109102ac6454SAndrew Thompson 			if (ni != NULL) {
109202ac6454SAndrew Thompson 				(void) ieee80211_input(ni, m, rssi,
10935463c4a4SSam Leffler 				    RT2573_NOISE_FLOOR);
109402ac6454SAndrew Thompson 				ieee80211_free_node(ni);
109502ac6454SAndrew Thompson 			} else
109602ac6454SAndrew Thompson 				(void) ieee80211_input_all(ic, m, rssi,
10975463c4a4SSam Leffler 				    RT2573_NOISE_FLOOR);
109802ac6454SAndrew Thompson 		}
1099db63fc93SWeongyo Jeong 		RUM_LOCK(sc);
11007a79cebfSGleb Smirnoff 		rum_start(sc);
110102ac6454SAndrew Thompson 		return;
110202ac6454SAndrew Thompson 
110302ac6454SAndrew Thompson 	default:			/* Error */
1104ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
110502ac6454SAndrew Thompson 			/* try to clear stall first */
1106ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
110702ac6454SAndrew Thompson 			goto tr_setup;
110802ac6454SAndrew Thompson 		}
110902ac6454SAndrew Thompson 		return;
111002ac6454SAndrew Thompson 	}
111102ac6454SAndrew Thompson }
111202ac6454SAndrew Thompson 
111302ac6454SAndrew Thompson static uint8_t
111402ac6454SAndrew Thompson rum_plcp_signal(int rate)
111502ac6454SAndrew Thompson {
111602ac6454SAndrew Thompson 	switch (rate) {
111702ac6454SAndrew Thompson 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
111802ac6454SAndrew Thompson 	case 12:	return 0xb;
111902ac6454SAndrew Thompson 	case 18:	return 0xf;
112002ac6454SAndrew Thompson 	case 24:	return 0xa;
112102ac6454SAndrew Thompson 	case 36:	return 0xe;
112202ac6454SAndrew Thompson 	case 48:	return 0x9;
112302ac6454SAndrew Thompson 	case 72:	return 0xd;
112402ac6454SAndrew Thompson 	case 96:	return 0x8;
112502ac6454SAndrew Thompson 	case 108:	return 0xc;
112602ac6454SAndrew Thompson 
112702ac6454SAndrew Thompson 	/* CCK rates (NB: not IEEE std, device-specific) */
112802ac6454SAndrew Thompson 	case 2:		return 0x0;
112902ac6454SAndrew Thompson 	case 4:		return 0x1;
113002ac6454SAndrew Thompson 	case 11:	return 0x2;
113102ac6454SAndrew Thompson 	case 22:	return 0x3;
113202ac6454SAndrew Thompson 	}
113302ac6454SAndrew Thompson 	return 0xff;		/* XXX unsupported/unknown rate */
113402ac6454SAndrew Thompson }
113502ac6454SAndrew Thompson 
11362cb9ef8dSAdrian Chadd /*
11372cb9ef8dSAdrian Chadd  * Map net80211 cipher to RT2573 security mode.
11382cb9ef8dSAdrian Chadd  */
11392cb9ef8dSAdrian Chadd static uint8_t
11402cb9ef8dSAdrian Chadd rum_crypto_mode(struct rum_softc *sc, u_int cipher, int keylen)
11412cb9ef8dSAdrian Chadd {
11422cb9ef8dSAdrian Chadd 	switch (cipher) {
11432cb9ef8dSAdrian Chadd 	case IEEE80211_CIPHER_WEP:
11442cb9ef8dSAdrian Chadd 		return (keylen < 8 ? RT2573_MODE_WEP40 : RT2573_MODE_WEP104);
11452cb9ef8dSAdrian Chadd 	case IEEE80211_CIPHER_TKIP:
11462cb9ef8dSAdrian Chadd 		return RT2573_MODE_TKIP;
11472cb9ef8dSAdrian Chadd 	case IEEE80211_CIPHER_AES_CCM:
11482cb9ef8dSAdrian Chadd 		return RT2573_MODE_AES_CCMP;
11492cb9ef8dSAdrian Chadd 	default:
11502cb9ef8dSAdrian Chadd 		device_printf(sc->sc_dev, "unknown cipher %d\n", cipher);
11512cb9ef8dSAdrian Chadd 		return 0;
11522cb9ef8dSAdrian Chadd 	}
11532cb9ef8dSAdrian Chadd }
11542cb9ef8dSAdrian Chadd 
115502ac6454SAndrew Thompson static void
115602ac6454SAndrew Thompson rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
11571354b52cSAdrian Chadd     struct ieee80211_key *k, uint32_t flags, uint8_t xflags, uint8_t qid,
11581354b52cSAdrian Chadd     int hdrlen, int len, int rate)
115902ac6454SAndrew Thompson {
11607a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
11611354b52cSAdrian Chadd 	struct wmeParams *wmep = &sc->wme_params[qid];
116202ac6454SAndrew Thompson 	uint16_t plcp_length;
116302ac6454SAndrew Thompson 	int remainder;
116402ac6454SAndrew Thompson 
11652cb9ef8dSAdrian Chadd 	flags |= RT2573_TX_VALID;
11662cb9ef8dSAdrian Chadd 	flags |= len << 16;
116702ac6454SAndrew Thompson 
11682cb9ef8dSAdrian Chadd 	if (k != NULL && !(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
11692cb9ef8dSAdrian Chadd 		const struct ieee80211_cipher *cip = k->wk_cipher;
117002ac6454SAndrew Thompson 
11712cb9ef8dSAdrian Chadd 		len += cip->ic_header + cip->ic_trailer + cip->ic_miclen;
11722cb9ef8dSAdrian Chadd 
11732cb9ef8dSAdrian Chadd 		desc->eiv = 0;		/* for WEP */
11742cb9ef8dSAdrian Chadd 		cip->ic_setiv(k, (uint8_t *)&desc->iv);
11752cb9ef8dSAdrian Chadd 	}
117602ac6454SAndrew Thompson 
117702ac6454SAndrew Thompson 	/* setup PLCP fields */
117802ac6454SAndrew Thompson 	desc->plcp_signal  = rum_plcp_signal(rate);
117902ac6454SAndrew Thompson 	desc->plcp_service = 4;
118002ac6454SAndrew Thompson 
118102ac6454SAndrew Thompson 	len += IEEE80211_CRC_LEN;
118226d39e2cSSam Leffler 	if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
11832cb9ef8dSAdrian Chadd 		flags |= RT2573_TX_OFDM;
118402ac6454SAndrew Thompson 
118502ac6454SAndrew Thompson 		plcp_length = len & 0xfff;
118602ac6454SAndrew Thompson 		desc->plcp_length_hi = plcp_length >> 6;
118702ac6454SAndrew Thompson 		desc->plcp_length_lo = plcp_length & 0x3f;
118802ac6454SAndrew Thompson 	} else {
1189b8e3094cSHans Petter Selasky 		if (rate == 0)
1190b8e3094cSHans Petter Selasky 			rate = 2;	/* avoid division by zero */
119102ac6454SAndrew Thompson 		plcp_length = (16 * len + rate - 1) / rate;
119202ac6454SAndrew Thompson 		if (rate == 22) {
119302ac6454SAndrew Thompson 			remainder = (16 * len) % 22;
119402ac6454SAndrew Thompson 			if (remainder != 0 && remainder < 7)
119502ac6454SAndrew Thompson 				desc->plcp_service |= RT2573_PLCP_LENGEXT;
119602ac6454SAndrew Thompson 		}
119702ac6454SAndrew Thompson 		desc->plcp_length_hi = plcp_length >> 8;
119802ac6454SAndrew Thompson 		desc->plcp_length_lo = plcp_length & 0xff;
119902ac6454SAndrew Thompson 
120002ac6454SAndrew Thompson 		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
120102ac6454SAndrew Thompson 			desc->plcp_signal |= 0x08;
120202ac6454SAndrew Thompson 	}
12032cb9ef8dSAdrian Chadd 
12042cb9ef8dSAdrian Chadd 	desc->flags = htole32(flags);
12052cb9ef8dSAdrian Chadd 	desc->hdrlen = hdrlen;
12062cb9ef8dSAdrian Chadd 	desc->xflags = xflags;
12072cb9ef8dSAdrian Chadd 
12081354b52cSAdrian Chadd 	desc->wme = htole16(RT2573_QID(qid) |
12091354b52cSAdrian Chadd 	    RT2573_AIFSN(wmep->wmep_aifsn) |
12101354b52cSAdrian Chadd 	    RT2573_LOGCWMIN(wmep->wmep_logcwmin) |
12111354b52cSAdrian Chadd 	    RT2573_LOGCWMAX(wmep->wmep_logcwmax));
121202ac6454SAndrew Thompson }
121302ac6454SAndrew Thompson 
121402ac6454SAndrew Thompson static int
121502ac6454SAndrew Thompson rum_sendprot(struct rum_softc *sc,
121602ac6454SAndrew Thompson     const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
121702ac6454SAndrew Thompson {
121802ac6454SAndrew Thompson 	struct ieee80211com *ic = ni->ni_ic;
121902ac6454SAndrew Thompson 	const struct ieee80211_frame *wh;
122002ac6454SAndrew Thompson 	struct rum_tx_data *data;
122102ac6454SAndrew Thompson 	struct mbuf *mprot;
1222e7b7db3dSAdrian Chadd 	int protrate, pktlen, flags, isshort;
122302ac6454SAndrew Thompson 	uint16_t dur;
122402ac6454SAndrew Thompson 
1225e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
122602ac6454SAndrew Thompson 	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
122702ac6454SAndrew Thompson 	    ("protection %d", prot));
122802ac6454SAndrew Thompson 
122902ac6454SAndrew Thompson 	wh = mtod(m, const struct ieee80211_frame *);
123002ac6454SAndrew Thompson 	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
123102ac6454SAndrew Thompson 
123226d39e2cSSam Leffler 	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
123302ac6454SAndrew Thompson 
123402ac6454SAndrew Thompson 	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
12350f817de7SJung-uk Kim 	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
123626d39e2cSSam Leffler 	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1237c42e124eSAdrian Chadd 	flags = 0;
123802ac6454SAndrew Thompson 	if (prot == IEEE80211_PROT_RTSCTS) {
123902ac6454SAndrew Thompson 		/* NB: CTS is the same size as an ACK */
124026d39e2cSSam Leffler 		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
124102ac6454SAndrew Thompson 		flags |= RT2573_TX_NEED_ACK;
124202ac6454SAndrew Thompson 		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
124302ac6454SAndrew Thompson 	} else {
124402ac6454SAndrew Thompson 		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
124502ac6454SAndrew Thompson 	}
124602ac6454SAndrew Thompson 	if (mprot == NULL) {
124702ac6454SAndrew Thompson 		/* XXX stat + msg */
124877ddf3d3SAndrew Thompson 		return (ENOBUFS);
124902ac6454SAndrew Thompson 	}
125002ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
125102ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
125202ac6454SAndrew Thompson 	sc->tx_nfree--;
125302ac6454SAndrew Thompson 
125402ac6454SAndrew Thompson 	data->m = mprot;
125502ac6454SAndrew Thompson 	data->ni = ieee80211_ref_node(ni);
125602ac6454SAndrew Thompson 	data->rate = protrate;
12571354b52cSAdrian Chadd 	rum_setup_tx_desc(sc, &data->desc, NULL, flags, 0, 0, 0,
12582cb9ef8dSAdrian Chadd 	    mprot->m_pkthdr.len, protrate);
125902ac6454SAndrew Thompson 
126002ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1261a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
126202ac6454SAndrew Thompson 
126302ac6454SAndrew Thompson 	return 0;
126402ac6454SAndrew Thompson }
126502ac6454SAndrew Thompson 
12662cb9ef8dSAdrian Chadd static uint32_t
12672cb9ef8dSAdrian Chadd rum_tx_crypto_flags(struct rum_softc *sc, struct ieee80211_node *ni,
12682cb9ef8dSAdrian Chadd     const struct ieee80211_key *k)
12692cb9ef8dSAdrian Chadd {
12702cb9ef8dSAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
12712cb9ef8dSAdrian Chadd 	u_int cipher;
12722cb9ef8dSAdrian Chadd 	uint32_t flags = 0;
12732cb9ef8dSAdrian Chadd 	uint8_t mode, pos;
12742cb9ef8dSAdrian Chadd 
12752cb9ef8dSAdrian Chadd 	if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
12762cb9ef8dSAdrian Chadd 		cipher = k->wk_cipher->ic_cipher;
12772cb9ef8dSAdrian Chadd 		pos = k->wk_keyix;
12782cb9ef8dSAdrian Chadd 		mode = rum_crypto_mode(sc, cipher, k->wk_keylen);
12792cb9ef8dSAdrian Chadd 		if (mode == 0)
12802cb9ef8dSAdrian Chadd 			return 0;
12812cb9ef8dSAdrian Chadd 
12822cb9ef8dSAdrian Chadd 		flags |= RT2573_TX_CIP_MODE(mode);
12832cb9ef8dSAdrian Chadd 
12842cb9ef8dSAdrian Chadd 		/* Do not trust GROUP flag */
12852cb9ef8dSAdrian Chadd 		if (!(k >= &vap->iv_nw_keys[0] &&
12862cb9ef8dSAdrian Chadd 		      k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]))
12872cb9ef8dSAdrian Chadd 			flags |= RT2573_TX_KEY_PAIR;
12882cb9ef8dSAdrian Chadd 		else
12892cb9ef8dSAdrian Chadd 			pos += 0 * RT2573_SKEY_MAX;	/* vap id */
12902cb9ef8dSAdrian Chadd 
12912cb9ef8dSAdrian Chadd 		flags |= RT2573_TX_KEY_ID(pos);
12922cb9ef8dSAdrian Chadd 
12932cb9ef8dSAdrian Chadd 		if (cipher == IEEE80211_CIPHER_TKIP)
12942cb9ef8dSAdrian Chadd 			flags |= RT2573_TX_TKIPMIC;
12952cb9ef8dSAdrian Chadd 	}
12962cb9ef8dSAdrian Chadd 
12972cb9ef8dSAdrian Chadd 	return flags;
12982cb9ef8dSAdrian Chadd }
12992cb9ef8dSAdrian Chadd 
130002ac6454SAndrew Thompson static int
130102ac6454SAndrew Thompson rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
130202ac6454SAndrew Thompson {
130302ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
13047a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
130502ac6454SAndrew Thompson 	struct rum_tx_data *data;
130602ac6454SAndrew Thompson 	struct ieee80211_frame *wh;
130702ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
13082cb9ef8dSAdrian Chadd 	struct ieee80211_key *k = NULL;
130902ac6454SAndrew Thompson 	uint32_t flags = 0;
131002ac6454SAndrew Thompson 	uint16_t dur;
13111354b52cSAdrian Chadd 	uint8_t ac, type, xflags = 0;
13122cb9ef8dSAdrian Chadd 	int hdrlen;
131302ac6454SAndrew Thompson 
1314e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
131502ac6454SAndrew Thompson 
131602ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
131702ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
131802ac6454SAndrew Thompson 	sc->tx_nfree--;
131902ac6454SAndrew Thompson 
132002ac6454SAndrew Thompson 	wh = mtod(m0, struct ieee80211_frame *);
1321c42e124eSAdrian Chadd 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
13222cb9ef8dSAdrian Chadd 	hdrlen = ieee80211_anyhdrsize(wh);
13231354b52cSAdrian Chadd 	ac = M_WME_GETAC(m0);
13242cb9ef8dSAdrian Chadd 
13255945b5f5SKevin Lo 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
13262cb9ef8dSAdrian Chadd 		k = ieee80211_crypto_get_txkey(ni, m0);
1327b2a76342SAdrian Chadd 		if (k == NULL)
13282cb9ef8dSAdrian Chadd 			return (ENOENT);
13292cb9ef8dSAdrian Chadd 
13302cb9ef8dSAdrian Chadd 		if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
13312cb9ef8dSAdrian Chadd 		    !k->wk_cipher->ic_encap(k, m0))
1332b2a76342SAdrian Chadd 			return (ENOBUFS);
1333b2a76342SAdrian Chadd 
133402ac6454SAndrew Thompson 		wh = mtod(m0, struct ieee80211_frame *);
133502ac6454SAndrew Thompson 	}
133602ac6454SAndrew Thompson 
133702ac6454SAndrew Thompson 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
133802ac6454SAndrew Thompson 
133902ac6454SAndrew Thompson 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
134002ac6454SAndrew Thompson 		flags |= RT2573_TX_NEED_ACK;
134102ac6454SAndrew Thompson 
134226d39e2cSSam Leffler 		dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
134302ac6454SAndrew Thompson 		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
13448cfe5440SHans Petter Selasky 		USETW(wh->i_dur, dur);
134502ac6454SAndrew Thompson 
134602ac6454SAndrew Thompson 		/* tell hardware to add timestamp for probe responses */
1347c42e124eSAdrian Chadd 		if (type == IEEE80211_FC0_TYPE_MGT &&
1348c42e124eSAdrian Chadd 		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1349c42e124eSAdrian Chadd 		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
135002ac6454SAndrew Thompson 			flags |= RT2573_TX_TIMESTAMP;
135102ac6454SAndrew Thompson 	}
135202ac6454SAndrew Thompson 
1353c42e124eSAdrian Chadd 	if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1354c42e124eSAdrian Chadd 		xflags |= RT2573_TX_HWSEQ;
1355c42e124eSAdrian Chadd 
13562cb9ef8dSAdrian Chadd 	if (k != NULL)
13572cb9ef8dSAdrian Chadd 		flags |= rum_tx_crypto_flags(sc, ni, k);
13582cb9ef8dSAdrian Chadd 
135902ac6454SAndrew Thompson 	data->m = m0;
136002ac6454SAndrew Thompson 	data->ni = ni;
136102ac6454SAndrew Thompson 	data->rate = tp->mgmtrate;
136202ac6454SAndrew Thompson 
13631354b52cSAdrian Chadd 	rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
13642cb9ef8dSAdrian Chadd 	    m0->m_pkthdr.len, tp->mgmtrate);
136502ac6454SAndrew Thompson 
136602ac6454SAndrew Thompson 	DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
136702ac6454SAndrew Thompson 	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
136802ac6454SAndrew Thompson 
136902ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1370a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
137102ac6454SAndrew Thompson 
137277ddf3d3SAndrew Thompson 	return (0);
137302ac6454SAndrew Thompson }
137402ac6454SAndrew Thompson 
137502ac6454SAndrew Thompson static int
137602ac6454SAndrew Thompson rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
137702ac6454SAndrew Thompson     const struct ieee80211_bpf_params *params)
137802ac6454SAndrew Thompson {
1379515db61dSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1380c42e124eSAdrian Chadd 	struct ieee80211_frame *wh;
138102ac6454SAndrew Thompson 	struct rum_tx_data *data;
138202ac6454SAndrew Thompson 	uint32_t flags;
13831354b52cSAdrian Chadd 	uint8_t ac, type, xflags = 0;
138402ac6454SAndrew Thompson 	int rate, error;
138502ac6454SAndrew Thompson 
1386e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
138702ac6454SAndrew Thompson 
1388c42e124eSAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1389c42e124eSAdrian Chadd 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1390c42e124eSAdrian Chadd 
13911354b52cSAdrian Chadd 	ac = params->ibp_pri & 3;
13921354b52cSAdrian Chadd 
1393515db61dSSam Leffler 	rate = params->ibp_rate0;
1394b2a76342SAdrian Chadd 	if (!ieee80211_isratevalid(ic->ic_rt, rate))
1395b2a76342SAdrian Chadd 		return (EINVAL);
1396b2a76342SAdrian Chadd 
139702ac6454SAndrew Thompson 	flags = 0;
139802ac6454SAndrew Thompson 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
139902ac6454SAndrew Thompson 		flags |= RT2573_TX_NEED_ACK;
140002ac6454SAndrew Thompson 	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
140102ac6454SAndrew Thompson 		error = rum_sendprot(sc, m0, ni,
140202ac6454SAndrew Thompson 		    params->ibp_flags & IEEE80211_BPF_RTS ?
140302ac6454SAndrew Thompson 			 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
140402ac6454SAndrew Thompson 		    rate);
1405b2a76342SAdrian Chadd 		if (error || sc->tx_nfree == 0)
1406b2a76342SAdrian Chadd 			return (ENOBUFS);
1407b2a76342SAdrian Chadd 
140802ac6454SAndrew Thompson 		flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
140902ac6454SAndrew Thompson 	}
141002ac6454SAndrew Thompson 
1411c42e124eSAdrian Chadd 	if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1412c42e124eSAdrian Chadd 		xflags |= RT2573_TX_HWSEQ;
1413c42e124eSAdrian Chadd 
141496ca458fSAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
141596ca458fSAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
141696ca458fSAndrew Thompson 	sc->tx_nfree--;
141796ca458fSAndrew Thompson 
141802ac6454SAndrew Thompson 	data->m = m0;
141902ac6454SAndrew Thompson 	data->ni = ni;
142002ac6454SAndrew Thompson 	data->rate = rate;
142102ac6454SAndrew Thompson 
142202ac6454SAndrew Thompson 	/* XXX need to setup descriptor ourself */
14231354b52cSAdrian Chadd 	rum_setup_tx_desc(sc, &data->desc, NULL, flags, xflags, ac, 0,
1424c42e124eSAdrian Chadd 	    m0->m_pkthdr.len, rate);
142502ac6454SAndrew Thompson 
142602ac6454SAndrew Thompson 	DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
142702ac6454SAndrew Thompson 	    m0->m_pkthdr.len, rate);
142802ac6454SAndrew Thompson 
142902ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1430a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
143102ac6454SAndrew Thompson 
143202ac6454SAndrew Thompson 	return 0;
143302ac6454SAndrew Thompson }
143402ac6454SAndrew Thompson 
143502ac6454SAndrew Thompson static int
143602ac6454SAndrew Thompson rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
143702ac6454SAndrew Thompson {
143802ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
14397a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
144002ac6454SAndrew Thompson 	struct rum_tx_data *data;
144102ac6454SAndrew Thompson 	struct ieee80211_frame *wh;
144202ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
14432cb9ef8dSAdrian Chadd 	struct ieee80211_key *k = NULL;
144402ac6454SAndrew Thompson 	uint32_t flags = 0;
144502ac6454SAndrew Thompson 	uint16_t dur;
14461354b52cSAdrian Chadd 	uint8_t ac, type, qos, xflags = 0;
14472cb9ef8dSAdrian Chadd 	int error, hdrlen, rate;
144802ac6454SAndrew Thompson 
1449e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
145002ac6454SAndrew Thompson 
145102ac6454SAndrew Thompson 	wh = mtod(m0, struct ieee80211_frame *);
1452c42e124eSAdrian Chadd 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
14532cb9ef8dSAdrian Chadd 	hdrlen = ieee80211_anyhdrsize(wh);
145402ac6454SAndrew Thompson 
14551354b52cSAdrian Chadd 	if (IEEE80211_QOS_HAS_SEQ(wh))
14561354b52cSAdrian Chadd 		qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
14571354b52cSAdrian Chadd 	else
14581354b52cSAdrian Chadd 		qos = 0;
14591354b52cSAdrian Chadd 	ac = M_WME_GETAC(m0);
14601354b52cSAdrian Chadd 
146102ac6454SAndrew Thompson 	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
146202ac6454SAndrew Thompson 	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
146302ac6454SAndrew Thompson 		rate = tp->mcastrate;
146402ac6454SAndrew Thompson 	else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
146502ac6454SAndrew Thompson 		rate = tp->ucastrate;
146602ac6454SAndrew Thompson 	else
146702ac6454SAndrew Thompson 		rate = ni->ni_txrate;
146802ac6454SAndrew Thompson 
14695945b5f5SKevin Lo 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
14702cb9ef8dSAdrian Chadd 		k = ieee80211_crypto_get_txkey(ni, m0);
147102ac6454SAndrew Thompson 		if (k == NULL) {
147202ac6454SAndrew Thompson 			m_freem(m0);
14732cb9ef8dSAdrian Chadd 			return (ENOENT);
14742cb9ef8dSAdrian Chadd 		}
14752cb9ef8dSAdrian Chadd 		if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
14762cb9ef8dSAdrian Chadd 		    !k->wk_cipher->ic_encap(k, m0)) {
14772cb9ef8dSAdrian Chadd 			m_freem(m0);
14782cb9ef8dSAdrian Chadd 			return (ENOBUFS);
147902ac6454SAndrew Thompson 		}
148002ac6454SAndrew Thompson 
148102ac6454SAndrew Thompson 		/* packet header may have moved, reset our local pointer */
148202ac6454SAndrew Thompson 		wh = mtod(m0, struct ieee80211_frame *);
148302ac6454SAndrew Thompson 	}
148402ac6454SAndrew Thompson 
1485c42e124eSAdrian Chadd 	if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1486c42e124eSAdrian Chadd 		xflags |= RT2573_TX_HWSEQ;
1487c42e124eSAdrian Chadd 
148802ac6454SAndrew Thompson 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
148902ac6454SAndrew Thompson 		int prot = IEEE80211_PROT_NONE;
149002ac6454SAndrew Thompson 		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
149102ac6454SAndrew Thompson 			prot = IEEE80211_PROT_RTSCTS;
149202ac6454SAndrew Thompson 		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
149326d39e2cSSam Leffler 		    ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
149402ac6454SAndrew Thompson 			prot = ic->ic_protmode;
149502ac6454SAndrew Thompson 		if (prot != IEEE80211_PROT_NONE) {
149602ac6454SAndrew Thompson 			error = rum_sendprot(sc, m0, ni, prot, rate);
149796ca458fSAndrew Thompson 			if (error || sc->tx_nfree == 0) {
149802ac6454SAndrew Thompson 				m_freem(m0);
149996ca458fSAndrew Thompson 				return ENOBUFS;
150002ac6454SAndrew Thompson 			}
150102ac6454SAndrew Thompson 			flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
150202ac6454SAndrew Thompson 		}
150302ac6454SAndrew Thompson 	}
150402ac6454SAndrew Thompson 
15052cb9ef8dSAdrian Chadd 	if (k != NULL)
15062cb9ef8dSAdrian Chadd 		flags |= rum_tx_crypto_flags(sc, ni, k);
15072cb9ef8dSAdrian Chadd 
150802ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
150902ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
151002ac6454SAndrew Thompson 	sc->tx_nfree--;
151102ac6454SAndrew Thompson 
151202ac6454SAndrew Thompson 	data->m = m0;
151302ac6454SAndrew Thompson 	data->ni = ni;
151402ac6454SAndrew Thompson 	data->rate = rate;
151502ac6454SAndrew Thompson 
151602ac6454SAndrew Thompson 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
15171354b52cSAdrian Chadd 		/* Unicast frame, check if an ACK is expected. */
15181354b52cSAdrian Chadd 		if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
15191354b52cSAdrian Chadd 		    IEEE80211_QOS_ACKPOLICY_NOACK)
152002ac6454SAndrew Thompson 			flags |= RT2573_TX_NEED_ACK;
152102ac6454SAndrew Thompson 
152226d39e2cSSam Leffler 		dur = ieee80211_ack_duration(ic->ic_rt, rate,
152302ac6454SAndrew Thompson 		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
15248cfe5440SHans Petter Selasky 		USETW(wh->i_dur, dur);
152502ac6454SAndrew Thompson 	}
152602ac6454SAndrew Thompson 
15271354b52cSAdrian Chadd 	rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
15282cb9ef8dSAdrian Chadd 	    m0->m_pkthdr.len, rate);
152902ac6454SAndrew Thompson 
153002ac6454SAndrew Thompson 	DPRINTFN(10, "sending frame len=%d rate=%d\n",
153102ac6454SAndrew Thompson 	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
153202ac6454SAndrew Thompson 
153302ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1534a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
153502ac6454SAndrew Thompson 
153602ac6454SAndrew Thompson 	return 0;
153702ac6454SAndrew Thompson }
153802ac6454SAndrew Thompson 
15397a79cebfSGleb Smirnoff static int
15407a79cebfSGleb Smirnoff rum_transmit(struct ieee80211com *ic, struct mbuf *m)
154179d2c5e8SGleb Smirnoff {
15427a79cebfSGleb Smirnoff 	struct rum_softc *sc = ic->ic_softc;
15437a79cebfSGleb Smirnoff 	int error;
15447a79cebfSGleb Smirnoff 
15457a79cebfSGleb Smirnoff 	RUM_LOCK(sc);
15467a79cebfSGleb Smirnoff 	if (!sc->sc_running) {
15477a79cebfSGleb Smirnoff 		RUM_UNLOCK(sc);
15487a79cebfSGleb Smirnoff 		return (ENXIO);
15497a79cebfSGleb Smirnoff 	}
15507a79cebfSGleb Smirnoff 	error = mbufq_enqueue(&sc->sc_snd, m);
15517a79cebfSGleb Smirnoff 	if (error) {
15527a79cebfSGleb Smirnoff 		RUM_UNLOCK(sc);
15537a79cebfSGleb Smirnoff 		return (error);
15547a79cebfSGleb Smirnoff 	}
15557a79cebfSGleb Smirnoff 	rum_start(sc);
15567a79cebfSGleb Smirnoff 	RUM_UNLOCK(sc);
15577a79cebfSGleb Smirnoff 
15587a79cebfSGleb Smirnoff 	return (0);
15597a79cebfSGleb Smirnoff }
15607a79cebfSGleb Smirnoff 
15617a79cebfSGleb Smirnoff static void
15627a79cebfSGleb Smirnoff rum_start(struct rum_softc *sc)
15637a79cebfSGleb Smirnoff {
156402ac6454SAndrew Thompson 	struct ieee80211_node *ni;
156502ac6454SAndrew Thompson 	struct mbuf *m;
156602ac6454SAndrew Thompson 
1567e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
15687a79cebfSGleb Smirnoff 
15697a79cebfSGleb Smirnoff 	if (!sc->sc_running)
157002ac6454SAndrew Thompson 		return;
15717a79cebfSGleb Smirnoff 
15727a79cebfSGleb Smirnoff 	while (sc->tx_nfree >= RUM_TX_MINFREE &&
15737a79cebfSGleb Smirnoff 	    (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
157402ac6454SAndrew Thompson 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
157502ac6454SAndrew Thompson 		if (rum_tx_data(sc, m, ni) != 0) {
15767a79cebfSGleb Smirnoff 			if_inc_counter(ni->ni_vap->iv_ifp,
15777a79cebfSGleb Smirnoff 			    IFCOUNTER_OERRORS, 1);
157802ac6454SAndrew Thompson 			ieee80211_free_node(ni);
157902ac6454SAndrew Thompson 			break;
158002ac6454SAndrew Thompson 		}
158102ac6454SAndrew Thompson 	}
158202ac6454SAndrew Thompson }
158302ac6454SAndrew Thompson 
15847a79cebfSGleb Smirnoff static void
15857a79cebfSGleb Smirnoff rum_parent(struct ieee80211com *ic)
158602ac6454SAndrew Thompson {
1587d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
15880698c0b3SAdrian Chadd 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1589645e4d17SHans Petter Selasky 
1590645e4d17SHans Petter Selasky 	RUM_LOCK(sc);
15917a79cebfSGleb Smirnoff 	if (sc->sc_detached) {
1592645e4d17SHans Petter Selasky 		RUM_UNLOCK(sc);
15937a79cebfSGleb Smirnoff 		return;
15947a79cebfSGleb Smirnoff 	}
159502ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
15960698c0b3SAdrian Chadd 
15970698c0b3SAdrian Chadd 	if (ic->ic_nrunning > 0) {
15980698c0b3SAdrian Chadd 		if (rum_init(sc) == 0)
159902ac6454SAndrew Thompson 			ieee80211_start_all(ic);
16000698c0b3SAdrian Chadd 		else
16010698c0b3SAdrian Chadd 			ieee80211_stop(vap);
16020698c0b3SAdrian Chadd 	} else
16030698c0b3SAdrian Chadd 		rum_stop(sc);
160402ac6454SAndrew Thompson }
160502ac6454SAndrew Thompson 
160602ac6454SAndrew Thompson static void
160702ac6454SAndrew Thompson rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
160802ac6454SAndrew Thompson {
1609760bc48eSAndrew Thompson 	struct usb_device_request req;
1610e0a69b51SAndrew Thompson 	usb_error_t error;
161102ac6454SAndrew Thompson 
161202ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
161302ac6454SAndrew Thompson 	req.bRequest = RT2573_READ_EEPROM;
161402ac6454SAndrew Thompson 	USETW(req.wValue, 0);
161502ac6454SAndrew Thompson 	USETW(req.wIndex, addr);
161602ac6454SAndrew Thompson 	USETW(req.wLength, len);
161702ac6454SAndrew Thompson 
161802ac6454SAndrew Thompson 	error = rum_do_request(sc, &req, buf);
161902ac6454SAndrew Thompson 	if (error != 0) {
162002ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1621a593f6b8SAndrew Thompson 		    usbd_errstr(error));
162202ac6454SAndrew Thompson 	}
162302ac6454SAndrew Thompson }
162402ac6454SAndrew Thompson 
162502ac6454SAndrew Thompson static uint32_t
162602ac6454SAndrew Thompson rum_read(struct rum_softc *sc, uint16_t reg)
162702ac6454SAndrew Thompson {
162802ac6454SAndrew Thompson 	uint32_t val;
162902ac6454SAndrew Thompson 
163002ac6454SAndrew Thompson 	rum_read_multi(sc, reg, &val, sizeof val);
163102ac6454SAndrew Thompson 
163202ac6454SAndrew Thompson 	return le32toh(val);
163302ac6454SAndrew Thompson }
163402ac6454SAndrew Thompson 
163502ac6454SAndrew Thompson static void
163602ac6454SAndrew Thompson rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
163702ac6454SAndrew Thompson {
1638760bc48eSAndrew Thompson 	struct usb_device_request req;
1639e0a69b51SAndrew Thompson 	usb_error_t error;
164002ac6454SAndrew Thompson 
164102ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
164202ac6454SAndrew Thompson 	req.bRequest = RT2573_READ_MULTI_MAC;
164302ac6454SAndrew Thompson 	USETW(req.wValue, 0);
164402ac6454SAndrew Thompson 	USETW(req.wIndex, reg);
164502ac6454SAndrew Thompson 	USETW(req.wLength, len);
164602ac6454SAndrew Thompson 
164702ac6454SAndrew Thompson 	error = rum_do_request(sc, &req, buf);
164802ac6454SAndrew Thompson 	if (error != 0) {
164902ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
165002ac6454SAndrew Thompson 		    "could not multi read MAC register: %s\n",
1651a593f6b8SAndrew Thompson 		    usbd_errstr(error));
165202ac6454SAndrew Thompson 	}
165302ac6454SAndrew Thompson }
165402ac6454SAndrew Thompson 
1655e0a69b51SAndrew Thompson static usb_error_t
165602ac6454SAndrew Thompson rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
165702ac6454SAndrew Thompson {
165802ac6454SAndrew Thompson 	uint32_t tmp = htole32(val);
165902ac6454SAndrew Thompson 
166077ddf3d3SAndrew Thompson 	return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
166102ac6454SAndrew Thompson }
166202ac6454SAndrew Thompson 
1663e0a69b51SAndrew Thompson static usb_error_t
166402ac6454SAndrew Thompson rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
166502ac6454SAndrew Thompson {
1666760bc48eSAndrew Thompson 	struct usb_device_request req;
1667e0a69b51SAndrew Thompson 	usb_error_t error;
16686d917491SHans Petter Selasky 	size_t offset;
166902ac6454SAndrew Thompson 
167002ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
167102ac6454SAndrew Thompson 	req.bRequest = RT2573_WRITE_MULTI_MAC;
167202ac6454SAndrew Thompson 	USETW(req.wValue, 0);
167302ac6454SAndrew Thompson 
167465d7e4d9SAdrian Chadd 	/* write at most 64 bytes at a time */
167565d7e4d9SAdrian Chadd 	for (offset = 0; offset < len; offset += 64) {
167665d7e4d9SAdrian Chadd 		USETW(req.wIndex, reg + offset);
167765d7e4d9SAdrian Chadd 		USETW(req.wLength, MIN(len - offset, 64));
167865d7e4d9SAdrian Chadd 
16798797cafeSBjoern A. Zeeb 		error = rum_do_request(sc, &req, (char *)buf + offset);
168002ac6454SAndrew Thompson 		if (error != 0) {
168102ac6454SAndrew Thompson 			device_printf(sc->sc_dev,
168202ac6454SAndrew Thompson 			    "could not multi write MAC register: %s\n",
1683a593f6b8SAndrew Thompson 			    usbd_errstr(error));
168477ddf3d3SAndrew Thompson 			return (error);
168502ac6454SAndrew Thompson 		}
168665d7e4d9SAdrian Chadd 	}
16878797cafeSBjoern A. Zeeb 
16888797cafeSBjoern A. Zeeb 	return (USB_ERR_NORMAL_COMPLETION);
168965d7e4d9SAdrian Chadd }
169002ac6454SAndrew Thompson 
1691a05022b2SAdrian Chadd static usb_error_t
1692a05022b2SAdrian Chadd rum_setbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1693a05022b2SAdrian Chadd {
1694a05022b2SAdrian Chadd 	return (rum_write(sc, reg, rum_read(sc, reg) | mask));
1695a05022b2SAdrian Chadd }
1696a05022b2SAdrian Chadd 
1697a05022b2SAdrian Chadd static usb_error_t
1698a05022b2SAdrian Chadd rum_clrbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1699a05022b2SAdrian Chadd {
1700a05022b2SAdrian Chadd 	return (rum_write(sc, reg, rum_read(sc, reg) & ~mask));
1701a05022b2SAdrian Chadd }
1702a05022b2SAdrian Chadd 
1703a05022b2SAdrian Chadd static usb_error_t
1704a05022b2SAdrian Chadd rum_modbits(struct rum_softc *sc, uint16_t reg, uint32_t set, uint32_t unset)
1705a05022b2SAdrian Chadd {
1706a05022b2SAdrian Chadd 	return (rum_write(sc, reg, (rum_read(sc, reg) & ~unset) | set));
1707a05022b2SAdrian Chadd }
1708a05022b2SAdrian Chadd 
1709cc9ae761SAdrian Chadd static int
1710cc9ae761SAdrian Chadd rum_bbp_busy(struct rum_softc *sc)
171102ac6454SAndrew Thompson {
171202ac6454SAndrew Thompson 	int ntries;
171302ac6454SAndrew Thompson 
171402ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
171502ac6454SAndrew Thompson 		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
171602ac6454SAndrew Thompson 			break;
171702ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
171802ac6454SAndrew Thompson 			break;
171902ac6454SAndrew Thompson 	}
1720cc9ae761SAdrian Chadd 	if (ntries == 100)
1721cc9ae761SAdrian Chadd 		return (ETIMEDOUT);
1722cc9ae761SAdrian Chadd 
1723cc9ae761SAdrian Chadd 	return (0);
1724cc9ae761SAdrian Chadd }
1725cc9ae761SAdrian Chadd 
1726cc9ae761SAdrian Chadd static void
1727cc9ae761SAdrian Chadd rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1728cc9ae761SAdrian Chadd {
1729cc9ae761SAdrian Chadd 	uint32_t tmp;
1730cc9ae761SAdrian Chadd 
1731cc9ae761SAdrian Chadd 	DPRINTFN(2, "reg=0x%08x\n", reg);
1732cc9ae761SAdrian Chadd 
1733cc9ae761SAdrian Chadd 	if (rum_bbp_busy(sc) != 0) {
173402ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not write to BBP\n");
173502ac6454SAndrew Thompson 		return;
173602ac6454SAndrew Thompson 	}
173702ac6454SAndrew Thompson 
173802ac6454SAndrew Thompson 	tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
173902ac6454SAndrew Thompson 	rum_write(sc, RT2573_PHY_CSR3, tmp);
174002ac6454SAndrew Thompson }
174102ac6454SAndrew Thompson 
174202ac6454SAndrew Thompson static uint8_t
174302ac6454SAndrew Thompson rum_bbp_read(struct rum_softc *sc, uint8_t reg)
174402ac6454SAndrew Thompson {
174502ac6454SAndrew Thompson 	uint32_t val;
174602ac6454SAndrew Thompson 	int ntries;
174702ac6454SAndrew Thompson 
174877ddf3d3SAndrew Thompson 	DPRINTFN(2, "reg=0x%08x\n", reg);
174977ddf3d3SAndrew Thompson 
1750cc9ae761SAdrian Chadd 	if (rum_bbp_busy(sc) != 0) {
175102ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read BBP\n");
175202ac6454SAndrew Thompson 		return 0;
175302ac6454SAndrew Thompson 	}
175402ac6454SAndrew Thompson 
175502ac6454SAndrew Thompson 	val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
175602ac6454SAndrew Thompson 	rum_write(sc, RT2573_PHY_CSR3, val);
175702ac6454SAndrew Thompson 
175802ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
175902ac6454SAndrew Thompson 		val = rum_read(sc, RT2573_PHY_CSR3);
176002ac6454SAndrew Thompson 		if (!(val & RT2573_BBP_BUSY))
176102ac6454SAndrew Thompson 			return val & 0xff;
176202ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
176302ac6454SAndrew Thompson 			break;
176402ac6454SAndrew Thompson 	}
176502ac6454SAndrew Thompson 
176602ac6454SAndrew Thompson 	device_printf(sc->sc_dev, "could not read BBP\n");
176702ac6454SAndrew Thompson 	return 0;
176802ac6454SAndrew Thompson }
176902ac6454SAndrew Thompson 
177002ac6454SAndrew Thompson static void
177102ac6454SAndrew Thompson rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
177202ac6454SAndrew Thompson {
177302ac6454SAndrew Thompson 	uint32_t tmp;
177402ac6454SAndrew Thompson 	int ntries;
177502ac6454SAndrew Thompson 
177602ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
177702ac6454SAndrew Thompson 		if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
177802ac6454SAndrew Thompson 			break;
177902ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
178002ac6454SAndrew Thompson 			break;
178102ac6454SAndrew Thompson 	}
178202ac6454SAndrew Thompson 	if (ntries == 100) {
178302ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not write to RF\n");
178402ac6454SAndrew Thompson 		return;
178502ac6454SAndrew Thompson 	}
178602ac6454SAndrew Thompson 
178767784314SPoul-Henning Kamp 	tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
178802ac6454SAndrew Thompson 	    (reg & 3);
178902ac6454SAndrew Thompson 	rum_write(sc, RT2573_PHY_CSR4, tmp);
179002ac6454SAndrew Thompson 
179102ac6454SAndrew Thompson 	/* remember last written value in sc */
179202ac6454SAndrew Thompson 	sc->rf_regs[reg] = val;
179302ac6454SAndrew Thompson 
179402ac6454SAndrew Thompson 	DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
179502ac6454SAndrew Thompson }
179602ac6454SAndrew Thompson 
179702ac6454SAndrew Thompson static void
179802ac6454SAndrew Thompson rum_select_antenna(struct rum_softc *sc)
179902ac6454SAndrew Thompson {
180002ac6454SAndrew Thompson 	uint8_t bbp4, bbp77;
180102ac6454SAndrew Thompson 	uint32_t tmp;
180202ac6454SAndrew Thompson 
180302ac6454SAndrew Thompson 	bbp4  = rum_bbp_read(sc, 4);
180402ac6454SAndrew Thompson 	bbp77 = rum_bbp_read(sc, 77);
180502ac6454SAndrew Thompson 
180602ac6454SAndrew Thompson 	/* TBD */
180702ac6454SAndrew Thompson 
180802ac6454SAndrew Thompson 	/* make sure Rx is disabled before switching antenna */
180902ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
181002ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
181102ac6454SAndrew Thompson 
181202ac6454SAndrew Thompson 	rum_bbp_write(sc,  4, bbp4);
181302ac6454SAndrew Thompson 	rum_bbp_write(sc, 77, bbp77);
181402ac6454SAndrew Thompson 
181502ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
181602ac6454SAndrew Thompson }
181702ac6454SAndrew Thompson 
181802ac6454SAndrew Thompson /*
181902ac6454SAndrew Thompson  * Enable multi-rate retries for frames sent at OFDM rates.
182002ac6454SAndrew Thompson  * In 802.11b/g mode, allow fallback to CCK rates.
182102ac6454SAndrew Thompson  */
182202ac6454SAndrew Thompson static void
182302ac6454SAndrew Thompson rum_enable_mrr(struct rum_softc *sc)
182402ac6454SAndrew Thompson {
18257a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
182602ac6454SAndrew Thompson 
1827a05022b2SAdrian Chadd 	if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1828a05022b2SAdrian Chadd 		rum_setbits(sc, RT2573_TXRX_CSR4,
1829a05022b2SAdrian Chadd 		    RT2573_MRR_ENABLED | RT2573_MRR_CCK_FALLBACK);
1830a05022b2SAdrian Chadd 	} else {
1831a05022b2SAdrian Chadd 		rum_modbits(sc, RT2573_TXRX_CSR4,
1832a05022b2SAdrian Chadd 		    RT2573_MRR_ENABLED, RT2573_MRR_CCK_FALLBACK);
1833a05022b2SAdrian Chadd 	}
183402ac6454SAndrew Thompson }
183502ac6454SAndrew Thompson 
183602ac6454SAndrew Thompson static void
183702ac6454SAndrew Thompson rum_set_txpreamble(struct rum_softc *sc)
183802ac6454SAndrew Thompson {
18397a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
184002ac6454SAndrew Thompson 
184102ac6454SAndrew Thompson 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1842a05022b2SAdrian Chadd 		rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
1843a05022b2SAdrian Chadd 	else
1844a05022b2SAdrian Chadd 		rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
184502ac6454SAndrew Thompson }
184602ac6454SAndrew Thompson 
184702ac6454SAndrew Thompson static void
184802ac6454SAndrew Thompson rum_set_basicrates(struct rum_softc *sc)
184902ac6454SAndrew Thompson {
18507a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
185102ac6454SAndrew Thompson 
185202ac6454SAndrew Thompson 	/* update basic rate set */
185302ac6454SAndrew Thompson 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
185402ac6454SAndrew Thompson 		/* 11b basic rates: 1, 2Mbps */
185502ac6454SAndrew Thompson 		rum_write(sc, RT2573_TXRX_CSR5, 0x3);
185602ac6454SAndrew Thompson 	} else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
185702ac6454SAndrew Thompson 		/* 11a basic rates: 6, 12, 24Mbps */
185802ac6454SAndrew Thompson 		rum_write(sc, RT2573_TXRX_CSR5, 0x150);
185902ac6454SAndrew Thompson 	} else {
186002ac6454SAndrew Thompson 		/* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
186102ac6454SAndrew Thompson 		rum_write(sc, RT2573_TXRX_CSR5, 0xf);
186202ac6454SAndrew Thompson 	}
186302ac6454SAndrew Thompson }
186402ac6454SAndrew Thompson 
186502ac6454SAndrew Thompson /*
186602ac6454SAndrew Thompson  * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
186702ac6454SAndrew Thompson  * driver.
186802ac6454SAndrew Thompson  */
186902ac6454SAndrew Thompson static void
187002ac6454SAndrew Thompson rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
187102ac6454SAndrew Thompson {
187202ac6454SAndrew Thompson 	uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
187302ac6454SAndrew Thompson 
187402ac6454SAndrew Thompson 	/* update all BBP registers that depend on the band */
187502ac6454SAndrew Thompson 	bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
187602ac6454SAndrew Thompson 	bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
187702ac6454SAndrew Thompson 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
187802ac6454SAndrew Thompson 		bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
187902ac6454SAndrew Thompson 		bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
188002ac6454SAndrew Thompson 	}
188102ac6454SAndrew Thompson 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
188202ac6454SAndrew Thompson 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
188302ac6454SAndrew Thompson 		bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
188402ac6454SAndrew Thompson 	}
188502ac6454SAndrew Thompson 
188602ac6454SAndrew Thompson 	sc->bbp17 = bbp17;
188702ac6454SAndrew Thompson 	rum_bbp_write(sc,  17, bbp17);
188802ac6454SAndrew Thompson 	rum_bbp_write(sc,  96, bbp96);
188902ac6454SAndrew Thompson 	rum_bbp_write(sc, 104, bbp104);
189002ac6454SAndrew Thompson 
189102ac6454SAndrew Thompson 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
189202ac6454SAndrew Thompson 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
189302ac6454SAndrew Thompson 		rum_bbp_write(sc, 75, 0x80);
189402ac6454SAndrew Thompson 		rum_bbp_write(sc, 86, 0x80);
189502ac6454SAndrew Thompson 		rum_bbp_write(sc, 88, 0x80);
189602ac6454SAndrew Thompson 	}
189702ac6454SAndrew Thompson 
189802ac6454SAndrew Thompson 	rum_bbp_write(sc, 35, bbp35);
189902ac6454SAndrew Thompson 	rum_bbp_write(sc, 97, bbp97);
190002ac6454SAndrew Thompson 	rum_bbp_write(sc, 98, bbp98);
190102ac6454SAndrew Thompson 
1902a05022b2SAdrian Chadd 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
1903a05022b2SAdrian Chadd 		rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_2GHZ,
1904a05022b2SAdrian Chadd 		    RT2573_PA_PE_5GHZ);
1905a05022b2SAdrian Chadd 	} else {
1906a05022b2SAdrian Chadd 		rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_5GHZ,
1907a05022b2SAdrian Chadd 		    RT2573_PA_PE_2GHZ);
1908a05022b2SAdrian Chadd 	}
190902ac6454SAndrew Thompson }
191002ac6454SAndrew Thompson 
191102ac6454SAndrew Thompson static void
191202ac6454SAndrew Thompson rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
191302ac6454SAndrew Thompson {
19147a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
191502ac6454SAndrew Thompson 	const struct rfprog *rfprog;
191602ac6454SAndrew Thompson 	uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
191702ac6454SAndrew Thompson 	int8_t power;
191802ac6454SAndrew Thompson 	int i, chan;
191902ac6454SAndrew Thompson 
192002ac6454SAndrew Thompson 	chan = ieee80211_chan2ieee(ic, c);
192102ac6454SAndrew Thompson 	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
192202ac6454SAndrew Thompson 		return;
192302ac6454SAndrew Thompson 
192402ac6454SAndrew Thompson 	/* select the appropriate RF settings based on what EEPROM says */
192502ac6454SAndrew Thompson 	rfprog = (sc->rf_rev == RT2573_RF_5225 ||
192602ac6454SAndrew Thompson 		  sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
192702ac6454SAndrew Thompson 
192802ac6454SAndrew Thompson 	/* find the settings for this channel (we know it exists) */
192902ac6454SAndrew Thompson 	for (i = 0; rfprog[i].chan != chan; i++);
193002ac6454SAndrew Thompson 
193102ac6454SAndrew Thompson 	power = sc->txpow[i];
193202ac6454SAndrew Thompson 	if (power < 0) {
193302ac6454SAndrew Thompson 		bbp94 += power;
193402ac6454SAndrew Thompson 		power = 0;
193502ac6454SAndrew Thompson 	} else if (power > 31) {
193602ac6454SAndrew Thompson 		bbp94 += power - 31;
193702ac6454SAndrew Thompson 		power = 31;
193802ac6454SAndrew Thompson 	}
193902ac6454SAndrew Thompson 
194002ac6454SAndrew Thompson 	/*
194102ac6454SAndrew Thompson 	 * If we are switching from the 2GHz band to the 5GHz band or
194202ac6454SAndrew Thompson 	 * vice-versa, BBP registers need to be reprogrammed.
194302ac6454SAndrew Thompson 	 */
194402ac6454SAndrew Thompson 	if (c->ic_flags != ic->ic_curchan->ic_flags) {
194502ac6454SAndrew Thompson 		rum_select_band(sc, c);
194602ac6454SAndrew Thompson 		rum_select_antenna(sc);
194702ac6454SAndrew Thompson 	}
194802ac6454SAndrew Thompson 	ic->ic_curchan = c;
194902ac6454SAndrew Thompson 
195002ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
195102ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
195202ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
195302ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
195402ac6454SAndrew Thompson 
195502ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
195602ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
195702ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
195802ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
195902ac6454SAndrew Thompson 
196002ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
196102ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
196202ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
196302ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
196402ac6454SAndrew Thompson 
196502ac6454SAndrew Thompson 	rum_pause(sc, hz / 100);
196602ac6454SAndrew Thompson 
196702ac6454SAndrew Thompson 	/* enable smart mode for MIMO-capable RFs */
196802ac6454SAndrew Thompson 	bbp3 = rum_bbp_read(sc, 3);
196902ac6454SAndrew Thompson 
197002ac6454SAndrew Thompson 	bbp3 &= ~RT2573_SMART_MODE;
197102ac6454SAndrew Thompson 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
197202ac6454SAndrew Thompson 		bbp3 |= RT2573_SMART_MODE;
197302ac6454SAndrew Thompson 
197402ac6454SAndrew Thompson 	rum_bbp_write(sc, 3, bbp3);
197502ac6454SAndrew Thompson 
197602ac6454SAndrew Thompson 	if (bbp94 != RT2573_BBPR94_DEFAULT)
197702ac6454SAndrew Thompson 		rum_bbp_write(sc, 94, bbp94);
197877ddf3d3SAndrew Thompson 
197977ddf3d3SAndrew Thompson 	/* give the chip some extra time to do the switchover */
198077ddf3d3SAndrew Thompson 	rum_pause(sc, hz / 100);
198102ac6454SAndrew Thompson }
198202ac6454SAndrew Thompson 
1983342ced03SAdrian Chadd static void
1984342ced03SAdrian Chadd rum_set_maxretry(struct rum_softc *sc, struct ieee80211vap *vap)
1985342ced03SAdrian Chadd {
1986342ced03SAdrian Chadd 	const struct ieee80211_txparam *tp;
1987342ced03SAdrian Chadd 	struct ieee80211_node *ni = vap->iv_bss;
1988342ced03SAdrian Chadd 	struct rum_vap *rvp = RUM_VAP(vap);
1989342ced03SAdrian Chadd 
1990342ced03SAdrian Chadd 	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1991342ced03SAdrian Chadd 	rvp->maxretry = tp->maxretry < 0xf ? tp->maxretry : 0xf;
1992342ced03SAdrian Chadd 
1993342ced03SAdrian Chadd 	rum_modbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_RETRY(rvp->maxretry) |
1994342ced03SAdrian Chadd 	    RT2573_LONG_RETRY(rvp->maxretry),
1995342ced03SAdrian Chadd 	    RT2573_SHORT_RETRY_MASK | RT2573_LONG_RETRY_MASK);
1996342ced03SAdrian Chadd }
1997342ced03SAdrian Chadd 
199802ac6454SAndrew Thompson /*
199902ac6454SAndrew Thompson  * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
200002ac6454SAndrew Thompson  * and HostAP operating modes.
200102ac6454SAndrew Thompson  */
2002aa81f853SAdrian Chadd static int
200302ac6454SAndrew Thompson rum_enable_tsf_sync(struct rum_softc *sc)
200402ac6454SAndrew Thompson {
20057a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
200602ac6454SAndrew Thompson 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
200702ac6454SAndrew Thompson 	uint32_t tmp;
200802ac6454SAndrew Thompson 
200902ac6454SAndrew Thompson 	if (vap->iv_opmode != IEEE80211_M_STA) {
201002ac6454SAndrew Thompson 		/*
201102ac6454SAndrew Thompson 		 * Change default 16ms TBTT adjustment to 8ms.
201202ac6454SAndrew Thompson 		 * Must be done before enabling beacon generation.
201302ac6454SAndrew Thompson 		 */
2014aa81f853SAdrian Chadd 		if (rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8) != 0)
2015aa81f853SAdrian Chadd 			return EIO;
201602ac6454SAndrew Thompson 	}
201702ac6454SAndrew Thompson 
201802ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
201902ac6454SAndrew Thompson 
202002ac6454SAndrew Thompson 	/* set beacon interval (in 1/16ms unit) */
202102ac6454SAndrew Thompson 	tmp |= vap->iv_bss->ni_intval * 16;
2022d847071cSAdrian Chadd 	tmp |= RT2573_TSF_TIMER_EN | RT2573_TBTT_TIMER_EN;
202302ac6454SAndrew Thompson 
2024d847071cSAdrian Chadd 	switch (vap->iv_opmode) {
2025d847071cSAdrian Chadd 	case IEEE80211_M_STA:
2026d847071cSAdrian Chadd 		/*
2027d847071cSAdrian Chadd 		 * Local TSF is always updated with remote TSF on beacon
2028d847071cSAdrian Chadd 		 * reception.
2029d847071cSAdrian Chadd 		 */
2030d847071cSAdrian Chadd 		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_STA);
2031d847071cSAdrian Chadd 		break;
2032d847071cSAdrian Chadd 	case IEEE80211_M_IBSS:
2033d847071cSAdrian Chadd 		/*
2034d847071cSAdrian Chadd 		 * Local TSF is updated with remote TSF on beacon reception
2035d847071cSAdrian Chadd 		 * only if the remote TSF is greater than local TSF.
2036d847071cSAdrian Chadd 		 */
2037d847071cSAdrian Chadd 		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_IBSS);
2038d847071cSAdrian Chadd 		tmp |= RT2573_BCN_TX_EN;
2039d847071cSAdrian Chadd 		break;
2040d847071cSAdrian Chadd 	case IEEE80211_M_HOSTAP:
2041d847071cSAdrian Chadd 		/* SYNC with nobody */
2042d847071cSAdrian Chadd 		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_HOSTAP);
2043d847071cSAdrian Chadd 		tmp |= RT2573_BCN_TX_EN;
2044d847071cSAdrian Chadd 		break;
2045d847071cSAdrian Chadd 	default:
2046d847071cSAdrian Chadd 		device_printf(sc->sc_dev,
2047d847071cSAdrian Chadd 		    "Enabling TSF failed. undefined opmode %d\n",
2048d847071cSAdrian Chadd 		    vap->iv_opmode);
2049aa81f853SAdrian Chadd 		return EINVAL;
2050d847071cSAdrian Chadd 	}
205102ac6454SAndrew Thompson 
2052aa81f853SAdrian Chadd 	if (rum_write(sc, RT2573_TXRX_CSR9, tmp) != 0)
2053aa81f853SAdrian Chadd 		return EIO;
2054aa81f853SAdrian Chadd 
2055aa81f853SAdrian Chadd 	return 0;
205602ac6454SAndrew Thompson }
205702ac6454SAndrew Thompson 
205802ac6454SAndrew Thompson static void
20595463c4a4SSam Leffler rum_enable_tsf(struct rum_softc *sc)
20605463c4a4SSam Leffler {
2061d847071cSAdrian Chadd 	rum_modbits(sc, RT2573_TXRX_CSR9, RT2573_TSF_TIMER_EN |
2062d847071cSAdrian Chadd 	    RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_DIS), 0x00ffffff);
20635463c4a4SSam Leffler }
20645463c4a4SSam Leffler 
20655463c4a4SSam Leffler static void
20666d804321SAdrian Chadd rum_abort_tsf_sync(struct rum_softc *sc)
20676d804321SAdrian Chadd {
2068a05022b2SAdrian Chadd 	rum_clrbits(sc, RT2573_TXRX_CSR9, 0x00ffffff);
20696d804321SAdrian Chadd }
20706d804321SAdrian Chadd 
20716d804321SAdrian Chadd static void
2072a6ccd477SAdrian Chadd rum_get_tsf(struct rum_softc *sc, uint64_t *buf)
2073a6ccd477SAdrian Chadd {
2074a6ccd477SAdrian Chadd 	rum_read_multi(sc, RT2573_TXRX_CSR12, buf, sizeof (*buf));
2075a6ccd477SAdrian Chadd }
2076a6ccd477SAdrian Chadd 
2077a6ccd477SAdrian Chadd static void
2078ed5711a1SAdrian Chadd rum_update_slot_cb(struct rum_softc *sc, union sec_param *data, uint8_t rvp_id)
207902ac6454SAndrew Thompson {
20807a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
208102ac6454SAndrew Thompson 	uint8_t slottime;
208202ac6454SAndrew Thompson 
2083bdfff33fSAndriy Voskoboinyk 	slottime = IEEE80211_GET_SLOTTIME(ic);
208402ac6454SAndrew Thompson 
2085a05022b2SAdrian Chadd 	rum_modbits(sc, RT2573_MAC_CSR9, slottime, 0xff);
208602ac6454SAndrew Thompson 
208702ac6454SAndrew Thompson 	DPRINTF("setting slot time to %uus\n", slottime);
208802ac6454SAndrew Thompson }
208902ac6454SAndrew Thompson 
209002ac6454SAndrew Thompson static void
2091a19dbd8dSAdrian Chadd rum_update_slot(struct ieee80211com *ic)
2092a19dbd8dSAdrian Chadd {
2093ed5711a1SAdrian Chadd 	rum_cmd_sleepable(ic->ic_softc, NULL, 0, 0, rum_update_slot_cb);
2094a19dbd8dSAdrian Chadd }
2095a19dbd8dSAdrian Chadd 
2096a14954c5SAndriy Voskoboinyk static int
2097a14954c5SAndriy Voskoboinyk rum_wme_update(struct ieee80211com *ic)
20981354b52cSAdrian Chadd {
20997682e597SAdrian Chadd 	const struct wmeParams *chanp =
21007682e597SAdrian Chadd 	    ic->ic_wme.wme_chanParams.cap_wmeParams;
2101a14954c5SAndriy Voskoboinyk 	struct rum_softc *sc = ic->ic_softc;
21021354b52cSAdrian Chadd 	int error = 0;
21031354b52cSAdrian Chadd 
2104a14954c5SAndriy Voskoboinyk 	RUM_LOCK(sc);
21051354b52cSAdrian Chadd 	error = rum_write(sc, RT2573_AIFSN_CSR,
21067682e597SAdrian Chadd 	    chanp[WME_AC_VO].wmep_aifsn  << 12 |
21077682e597SAdrian Chadd 	    chanp[WME_AC_VI].wmep_aifsn  <<  8 |
21087682e597SAdrian Chadd 	    chanp[WME_AC_BK].wmep_aifsn  <<  4 |
21097682e597SAdrian Chadd 	    chanp[WME_AC_BE].wmep_aifsn);
21101354b52cSAdrian Chadd 	if (error)
21111354b52cSAdrian Chadd 		goto print_err;
21121354b52cSAdrian Chadd 	error = rum_write(sc, RT2573_CWMIN_CSR,
21137682e597SAdrian Chadd 	    chanp[WME_AC_VO].wmep_logcwmin << 12 |
21147682e597SAdrian Chadd 	    chanp[WME_AC_VI].wmep_logcwmin <<  8 |
21157682e597SAdrian Chadd 	    chanp[WME_AC_BK].wmep_logcwmin <<  4 |
21167682e597SAdrian Chadd 	    chanp[WME_AC_BE].wmep_logcwmin);
21171354b52cSAdrian Chadd 	if (error)
21181354b52cSAdrian Chadd 		goto print_err;
21191354b52cSAdrian Chadd 	error = rum_write(sc, RT2573_CWMAX_CSR,
21207682e597SAdrian Chadd 	    chanp[WME_AC_VO].wmep_logcwmax << 12 |
21217682e597SAdrian Chadd 	    chanp[WME_AC_VI].wmep_logcwmax <<  8 |
21227682e597SAdrian Chadd 	    chanp[WME_AC_BK].wmep_logcwmax <<  4 |
21237682e597SAdrian Chadd 	    chanp[WME_AC_BE].wmep_logcwmax);
21241354b52cSAdrian Chadd 	if (error)
21251354b52cSAdrian Chadd 		goto print_err;
21261354b52cSAdrian Chadd 	error = rum_write(sc, RT2573_TXOP01_CSR,
21277682e597SAdrian Chadd 	    chanp[WME_AC_BK].wmep_txopLimit << 16 |
21287682e597SAdrian Chadd 	    chanp[WME_AC_BE].wmep_txopLimit);
21291354b52cSAdrian Chadd 	if (error)
21301354b52cSAdrian Chadd 		goto print_err;
21311354b52cSAdrian Chadd 	error = rum_write(sc, RT2573_TXOP23_CSR,
21327682e597SAdrian Chadd 	    chanp[WME_AC_VO].wmep_txopLimit << 16 |
21337682e597SAdrian Chadd 	    chanp[WME_AC_VI].wmep_txopLimit);
21341354b52cSAdrian Chadd 	if (error)
21351354b52cSAdrian Chadd 		goto print_err;
21361354b52cSAdrian Chadd 
21371354b52cSAdrian Chadd 	memcpy(sc->wme_params, chanp, sizeof(*chanp) * WME_NUM_AC);
21381354b52cSAdrian Chadd 
21391354b52cSAdrian Chadd print_err:
2140a14954c5SAndriy Voskoboinyk 	RUM_UNLOCK(sc);
2141a14954c5SAndriy Voskoboinyk 	if (error != 0) {
21421354b52cSAdrian Chadd 		device_printf(sc->sc_dev, "%s: WME update failed, error %d\n",
21431354b52cSAdrian Chadd 		    __func__, error);
21441354b52cSAdrian Chadd 	}
21451354b52cSAdrian Chadd 
2146a14954c5SAndriy Voskoboinyk 	return (error);
21471354b52cSAdrian Chadd }
21481354b52cSAdrian Chadd 
21491354b52cSAdrian Chadd static void
215002ac6454SAndrew Thompson rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
215102ac6454SAndrew Thompson {
215202ac6454SAndrew Thompson 
215342769826SAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR4,
215442769826SAdrian Chadd 	    bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
215542769826SAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR5,
215642769826SAdrian Chadd 	    bssid[4] | bssid[5] << 8 | RT2573_NUM_BSSID_MSK(1));
215702ac6454SAndrew Thompson }
215802ac6454SAndrew Thompson 
215902ac6454SAndrew Thompson static void
216002ac6454SAndrew Thompson rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
216102ac6454SAndrew Thompson {
216202ac6454SAndrew Thompson 
216342769826SAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR2,
216442769826SAdrian Chadd 	    addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
216542769826SAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR3,
216642769826SAdrian Chadd 	    addr[4] | addr[5] << 8 | 0xff << 16);
216702ac6454SAndrew Thompson }
216802ac6454SAndrew Thompson 
216902ac6454SAndrew Thompson static void
21705efea30fSAndrew Thompson rum_setpromisc(struct rum_softc *sc)
217102ac6454SAndrew Thompson {
2172a05022b2SAdrian Chadd 	struct ieee80211com *ic = &sc->sc_ic;
217302ac6454SAndrew Thompson 
2174a05022b2SAdrian Chadd 	if (ic->ic_promisc == 0)
2175a05022b2SAdrian Chadd 		rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2176a05022b2SAdrian Chadd 	else
2177a05022b2SAdrian Chadd 		rum_clrbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
217802ac6454SAndrew Thompson 
2179a05022b2SAdrian Chadd 	DPRINTF("%s promiscuous mode\n", ic->ic_promisc > 0 ?
218002ac6454SAndrew Thompson 	    "entering" : "leaving");
218102ac6454SAndrew Thompson }
218202ac6454SAndrew Thompson 
218377ddf3d3SAndrew Thompson static void
2184272f6adeSGleb Smirnoff rum_update_promisc(struct ieee80211com *ic)
218577ddf3d3SAndrew Thompson {
2186272f6adeSGleb Smirnoff 	struct rum_softc *sc = ic->ic_softc;
218777ddf3d3SAndrew Thompson 
2188ba2c1fbcSAdrian Chadd 	RUM_LOCK(sc);
218942769826SAdrian Chadd 	if (sc->sc_running)
21905efea30fSAndrew Thompson 		rum_setpromisc(sc);
219177ddf3d3SAndrew Thompson 	RUM_UNLOCK(sc);
219277ddf3d3SAndrew Thompson }
219377ddf3d3SAndrew Thompson 
21946dd8e071SEd Maste static void
2195272f6adeSGleb Smirnoff rum_update_mcast(struct ieee80211com *ic)
21966dd8e071SEd Maste {
2197e7b7db3dSAdrian Chadd 	/* Ignore. */
21986dd8e071SEd Maste }
21996dd8e071SEd Maste 
220002ac6454SAndrew Thompson static const char *
220102ac6454SAndrew Thompson rum_get_rf(int rev)
220202ac6454SAndrew Thompson {
220302ac6454SAndrew Thompson 	switch (rev) {
220402ac6454SAndrew Thompson 	case RT2573_RF_2527:	return "RT2527 (MIMO XR)";
220502ac6454SAndrew Thompson 	case RT2573_RF_2528:	return "RT2528";
220602ac6454SAndrew Thompson 	case RT2573_RF_5225:	return "RT5225 (MIMO XR)";
220702ac6454SAndrew Thompson 	case RT2573_RF_5226:	return "RT5226";
220802ac6454SAndrew Thompson 	default:		return "unknown";
220902ac6454SAndrew Thompson 	}
221002ac6454SAndrew Thompson }
221102ac6454SAndrew Thompson 
221202ac6454SAndrew Thompson static void
221302ac6454SAndrew Thompson rum_read_eeprom(struct rum_softc *sc)
221402ac6454SAndrew Thompson {
221502ac6454SAndrew Thompson 	uint16_t val;
221602ac6454SAndrew Thompson #ifdef RUM_DEBUG
221702ac6454SAndrew Thompson 	int i;
221802ac6454SAndrew Thompson #endif
221902ac6454SAndrew Thompson 
222002ac6454SAndrew Thompson 	/* read MAC address */
22217a79cebfSGleb Smirnoff 	rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_ic.ic_macaddr, 6);
222202ac6454SAndrew Thompson 
222302ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
222402ac6454SAndrew Thompson 	val = le16toh(val);
222502ac6454SAndrew Thompson 	sc->rf_rev =   (val >> 11) & 0x1f;
222602ac6454SAndrew Thompson 	sc->hw_radio = (val >> 10) & 0x1;
222702ac6454SAndrew Thompson 	sc->rx_ant =   (val >> 4)  & 0x3;
222802ac6454SAndrew Thompson 	sc->tx_ant =   (val >> 2)  & 0x3;
222902ac6454SAndrew Thompson 	sc->nb_ant =   val & 0x3;
223002ac6454SAndrew Thompson 
223102ac6454SAndrew Thompson 	DPRINTF("RF revision=%d\n", sc->rf_rev);
223202ac6454SAndrew Thompson 
223302ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
223402ac6454SAndrew Thompson 	val = le16toh(val);
223502ac6454SAndrew Thompson 	sc->ext_5ghz_lna = (val >> 6) & 0x1;
223602ac6454SAndrew Thompson 	sc->ext_2ghz_lna = (val >> 4) & 0x1;
223702ac6454SAndrew Thompson 
223802ac6454SAndrew Thompson 	DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
223902ac6454SAndrew Thompson 	    sc->ext_2ghz_lna, sc->ext_5ghz_lna);
224002ac6454SAndrew Thompson 
224102ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
224202ac6454SAndrew Thompson 	val = le16toh(val);
224302ac6454SAndrew Thompson 	if ((val & 0xff) != 0xff)
224402ac6454SAndrew Thompson 		sc->rssi_2ghz_corr = (int8_t)(val & 0xff);	/* signed */
224502ac6454SAndrew Thompson 
224602ac6454SAndrew Thompson 	/* Only [-10, 10] is valid */
224702ac6454SAndrew Thompson 	if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
224802ac6454SAndrew Thompson 		sc->rssi_2ghz_corr = 0;
224902ac6454SAndrew Thompson 
225002ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
225102ac6454SAndrew Thompson 	val = le16toh(val);
225202ac6454SAndrew Thompson 	if ((val & 0xff) != 0xff)
225302ac6454SAndrew Thompson 		sc->rssi_5ghz_corr = (int8_t)(val & 0xff);	/* signed */
225402ac6454SAndrew Thompson 
225502ac6454SAndrew Thompson 	/* Only [-10, 10] is valid */
225602ac6454SAndrew Thompson 	if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
225702ac6454SAndrew Thompson 		sc->rssi_5ghz_corr = 0;
225802ac6454SAndrew Thompson 
225902ac6454SAndrew Thompson 	if (sc->ext_2ghz_lna)
226002ac6454SAndrew Thompson 		sc->rssi_2ghz_corr -= 14;
226102ac6454SAndrew Thompson 	if (sc->ext_5ghz_lna)
226202ac6454SAndrew Thompson 		sc->rssi_5ghz_corr -= 14;
226302ac6454SAndrew Thompson 
226402ac6454SAndrew Thompson 	DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
226502ac6454SAndrew Thompson 	    sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
226602ac6454SAndrew Thompson 
226702ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
226802ac6454SAndrew Thompson 	val = le16toh(val);
226902ac6454SAndrew Thompson 	if ((val & 0xff) != 0xff)
227002ac6454SAndrew Thompson 		sc->rffreq = val & 0xff;
227102ac6454SAndrew Thompson 
227202ac6454SAndrew Thompson 	DPRINTF("RF freq=%d\n", sc->rffreq);
227302ac6454SAndrew Thompson 
227402ac6454SAndrew Thompson 	/* read Tx power for all a/b/g channels */
227502ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
227602ac6454SAndrew Thompson 	/* XXX default Tx power for 802.11a channels */
227702ac6454SAndrew Thompson 	memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
227802ac6454SAndrew Thompson #ifdef RUM_DEBUG
227902ac6454SAndrew Thompson 	for (i = 0; i < 14; i++)
228002ac6454SAndrew Thompson 		DPRINTF("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]);
228102ac6454SAndrew Thompson #endif
228202ac6454SAndrew Thompson 
228302ac6454SAndrew Thompson 	/* read default values for BBP registers */
228402ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
228502ac6454SAndrew Thompson #ifdef RUM_DEBUG
228602ac6454SAndrew Thompson 	for (i = 0; i < 14; i++) {
228702ac6454SAndrew Thompson 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
228802ac6454SAndrew Thompson 			continue;
228902ac6454SAndrew Thompson 		DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
229002ac6454SAndrew Thompson 		    sc->bbp_prom[i].val);
229102ac6454SAndrew Thompson 	}
229202ac6454SAndrew Thompson #endif
229302ac6454SAndrew Thompson }
229402ac6454SAndrew Thompson 
229502ac6454SAndrew Thompson static int
22966095f7caSAdrian Chadd rum_bbp_wakeup(struct rum_softc *sc)
22976095f7caSAdrian Chadd {
22986095f7caSAdrian Chadd 	unsigned int ntries;
22996095f7caSAdrian Chadd 
23006095f7caSAdrian Chadd 	for (ntries = 0; ntries < 100; ntries++) {
23016095f7caSAdrian Chadd 		if (rum_read(sc, RT2573_MAC_CSR12) & 8)
23026095f7caSAdrian Chadd 			break;
23036095f7caSAdrian Chadd 		rum_write(sc, RT2573_MAC_CSR12, 4);	/* force wakeup */
23046095f7caSAdrian Chadd 		if (rum_pause(sc, hz / 100))
23056095f7caSAdrian Chadd 			break;
23066095f7caSAdrian Chadd 	}
23076095f7caSAdrian Chadd 	if (ntries == 100) {
23086095f7caSAdrian Chadd 		device_printf(sc->sc_dev,
23096095f7caSAdrian Chadd 		    "timeout waiting for BBP/RF to wakeup\n");
23106095f7caSAdrian Chadd 		return (ETIMEDOUT);
23116095f7caSAdrian Chadd 	}
23126095f7caSAdrian Chadd 
23136095f7caSAdrian Chadd 	return (0);
23146095f7caSAdrian Chadd }
23156095f7caSAdrian Chadd 
23166095f7caSAdrian Chadd static int
231702ac6454SAndrew Thompson rum_bbp_init(struct rum_softc *sc)
231802ac6454SAndrew Thompson {
231902ac6454SAndrew Thompson 	int i, ntries;
232002ac6454SAndrew Thompson 
232102ac6454SAndrew Thompson 	/* wait for BBP to be ready */
232202ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
232302ac6454SAndrew Thompson 		const uint8_t val = rum_bbp_read(sc, 0);
232402ac6454SAndrew Thompson 		if (val != 0 && val != 0xff)
232502ac6454SAndrew Thompson 			break;
232602ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
232702ac6454SAndrew Thompson 			break;
232802ac6454SAndrew Thompson 	}
232902ac6454SAndrew Thompson 	if (ntries == 100) {
233002ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "timeout waiting for BBP\n");
233102ac6454SAndrew Thompson 		return EIO;
233202ac6454SAndrew Thompson 	}
233302ac6454SAndrew Thompson 
233402ac6454SAndrew Thompson 	/* initialize BBP registers to default values */
2335e7b7db3dSAdrian Chadd 	for (i = 0; i < nitems(rum_def_bbp); i++)
233602ac6454SAndrew Thompson 		rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
233702ac6454SAndrew Thompson 
233802ac6454SAndrew Thompson 	/* write vendor-specific BBP values (from EEPROM) */
233902ac6454SAndrew Thompson 	for (i = 0; i < 16; i++) {
234002ac6454SAndrew Thompson 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
234102ac6454SAndrew Thompson 			continue;
234202ac6454SAndrew Thompson 		rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
234302ac6454SAndrew Thompson 	}
234402ac6454SAndrew Thompson 
234502ac6454SAndrew Thompson 	return 0;
234602ac6454SAndrew Thompson }
234702ac6454SAndrew Thompson 
23482cb9ef8dSAdrian Chadd static void
23492cb9ef8dSAdrian Chadd rum_clr_shkey_regs(struct rum_softc *sc)
23502cb9ef8dSAdrian Chadd {
23512cb9ef8dSAdrian Chadd 	rum_write(sc, RT2573_SEC_CSR0, 0);
23522cb9ef8dSAdrian Chadd 	rum_write(sc, RT2573_SEC_CSR1, 0);
23532cb9ef8dSAdrian Chadd 	rum_write(sc, RT2573_SEC_CSR5, 0);
23542cb9ef8dSAdrian Chadd }
23552cb9ef8dSAdrian Chadd 
23560698c0b3SAdrian Chadd static int
23577a79cebfSGleb Smirnoff rum_init(struct rum_softc *sc)
235802ac6454SAndrew Thompson {
23597a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
23607a79cebfSGleb Smirnoff 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
236102ac6454SAndrew Thompson 	uint32_t tmp;
23626095f7caSAdrian Chadd 	int i, ret;
236302ac6454SAndrew Thompson 
23640698c0b3SAdrian Chadd 	RUM_LOCK(sc);
23650698c0b3SAdrian Chadd 	if (sc->sc_running) {
23660698c0b3SAdrian Chadd 		ret = 0;
23670698c0b3SAdrian Chadd 		goto end;
23680698c0b3SAdrian Chadd 	}
236902ac6454SAndrew Thompson 
237002ac6454SAndrew Thompson 	/* initialize MAC registers to default values */
2371e7b7db3dSAdrian Chadd 	for (i = 0; i < nitems(rum_def_mac); i++)
237202ac6454SAndrew Thompson 		rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
237302ac6454SAndrew Thompson 
23741354b52cSAdrian Chadd 	/* reset some WME parameters to default values */
23751354b52cSAdrian Chadd 	sc->wme_params[0].wmep_aifsn = 2;
23761354b52cSAdrian Chadd 	sc->wme_params[0].wmep_logcwmin = 4;
23771354b52cSAdrian Chadd 	sc->wme_params[0].wmep_logcwmax = 10;
23781354b52cSAdrian Chadd 
237902ac6454SAndrew Thompson 	/* set host ready */
2380e7b7db3dSAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
238102ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR1, 0);
238202ac6454SAndrew Thompson 
238302ac6454SAndrew Thompson 	/* wait for BBP/RF to wakeup */
23846095f7caSAdrian Chadd 	if ((ret = rum_bbp_wakeup(sc)) != 0)
23850698c0b3SAdrian Chadd 		goto end;
238602ac6454SAndrew Thompson 
23870698c0b3SAdrian Chadd 	if ((ret = rum_bbp_init(sc)) != 0)
23880698c0b3SAdrian Chadd 		goto end;
238902ac6454SAndrew Thompson 
239002ac6454SAndrew Thompson 	/* select default channel */
239102ac6454SAndrew Thompson 	rum_select_band(sc, ic->ic_curchan);
239202ac6454SAndrew Thompson 	rum_select_antenna(sc);
239302ac6454SAndrew Thompson 	rum_set_chan(sc, ic->ic_curchan);
239402ac6454SAndrew Thompson 
239502ac6454SAndrew Thompson 	/* clear STA registers */
239602ac6454SAndrew Thompson 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
239702ac6454SAndrew Thompson 
23982cb9ef8dSAdrian Chadd 	/* clear security registers (if required) */
23992cb9ef8dSAdrian Chadd 	if (sc->sc_clr_shkeys == 0) {
24002cb9ef8dSAdrian Chadd 		rum_clr_shkey_regs(sc);
24012cb9ef8dSAdrian Chadd 		sc->sc_clr_shkeys = 1;
24022cb9ef8dSAdrian Chadd 	}
24032cb9ef8dSAdrian Chadd 
24047a79cebfSGleb Smirnoff 	rum_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
240502ac6454SAndrew Thompson 
240602ac6454SAndrew Thompson 	/* initialize ASIC */
2407e7b7db3dSAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR1, RT2573_HOST_READY);
240802ac6454SAndrew Thompson 
240902ac6454SAndrew Thompson 	/*
241002ac6454SAndrew Thompson 	 * Allocate Tx and Rx xfer queues.
241102ac6454SAndrew Thompson 	 */
241202ac6454SAndrew Thompson 	rum_setup_tx_list(sc);
241302ac6454SAndrew Thompson 
241402ac6454SAndrew Thompson 	/* update Rx filter */
241502ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
241602ac6454SAndrew Thompson 
241702ac6454SAndrew Thompson 	tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
241802ac6454SAndrew Thompson 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
241902ac6454SAndrew Thompson 		tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
242002ac6454SAndrew Thompson 		       RT2573_DROP_ACKCTS;
242102ac6454SAndrew Thompson 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
242202ac6454SAndrew Thompson 			tmp |= RT2573_DROP_TODS;
24237a79cebfSGleb Smirnoff 		if (ic->ic_promisc == 0)
242402ac6454SAndrew Thompson 			tmp |= RT2573_DROP_NOT_TO_ME;
242502ac6454SAndrew Thompson 	}
242602ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
242702ac6454SAndrew Thompson 
24287a79cebfSGleb Smirnoff 	sc->sc_running = 1;
2429ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2430a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
243102ac6454SAndrew Thompson 
24320698c0b3SAdrian Chadd end:	RUM_UNLOCK(sc);
24330698c0b3SAdrian Chadd 
24340698c0b3SAdrian Chadd 	if (ret != 0)
24350698c0b3SAdrian Chadd 		rum_stop(sc);
24360698c0b3SAdrian Chadd 
24370698c0b3SAdrian Chadd 	return ret;
243802ac6454SAndrew Thompson }
243902ac6454SAndrew Thompson 
244002ac6454SAndrew Thompson static void
24415efea30fSAndrew Thompson rum_stop(struct rum_softc *sc)
244202ac6454SAndrew Thompson {
244302ac6454SAndrew Thompson 
24440698c0b3SAdrian Chadd 	RUM_LOCK(sc);
24450698c0b3SAdrian Chadd 	if (!sc->sc_running) {
24460698c0b3SAdrian Chadd 		RUM_UNLOCK(sc);
24470698c0b3SAdrian Chadd 		return;
24480698c0b3SAdrian Chadd 	}
24497a79cebfSGleb Smirnoff 	sc->sc_running = 0;
245002ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
245102ac6454SAndrew Thompson 
245202ac6454SAndrew Thompson 	/*
245302ac6454SAndrew Thompson 	 * Drain the USB transfers, if not already drained:
245402ac6454SAndrew Thompson 	 */
2455a593f6b8SAndrew Thompson 	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2456a593f6b8SAndrew Thompson 	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
245702ac6454SAndrew Thompson 
245802ac6454SAndrew Thompson 	RUM_LOCK(sc);
245902ac6454SAndrew Thompson 	rum_unsetup_tx_list(sc);
246002ac6454SAndrew Thompson 
246102ac6454SAndrew Thompson 	/* disable Rx */
2462a05022b2SAdrian Chadd 	rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DISABLE_RX);
246302ac6454SAndrew Thompson 
246402ac6454SAndrew Thompson 	/* reset ASIC */
2465e7b7db3dSAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
246602ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR1, 0);
24670698c0b3SAdrian Chadd 	RUM_UNLOCK(sc);
246802ac6454SAndrew Thompson }
246902ac6454SAndrew Thompson 
247077ddf3d3SAndrew Thompson static void
247177ddf3d3SAndrew Thompson rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
247202ac6454SAndrew Thompson {
247302ac6454SAndrew Thompson 	uint16_t reg = RT2573_MCU_CODE_BASE;
2474e0a69b51SAndrew Thompson 	usb_error_t err;
247502ac6454SAndrew Thompson 
247602ac6454SAndrew Thompson 	/* copy firmware image into NIC */
247777ddf3d3SAndrew Thompson 	for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
247877ddf3d3SAndrew Thompson 		err = rum_write(sc, reg, UGETDW(ucode));
247977ddf3d3SAndrew Thompson 		if (err) {
248077ddf3d3SAndrew Thompson 			/* firmware already loaded ? */
248177ddf3d3SAndrew Thompson 			device_printf(sc->sc_dev, "Firmware load "
248277ddf3d3SAndrew Thompson 			    "failure! (ignored)\n");
248377ddf3d3SAndrew Thompson 			break;
248477ddf3d3SAndrew Thompson 		}
248577ddf3d3SAndrew Thompson 	}
248602ac6454SAndrew Thompson 
2487fd7dae9cSAndriy Voskoboinyk 	err = rum_do_mcu_request(sc, RT2573_MCU_RUN);
2488fd7dae9cSAndriy Voskoboinyk 	if (err != USB_ERR_NORMAL_COMPLETION) {
248902ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not run firmware: %s\n",
2490a593f6b8SAndrew Thompson 		    usbd_errstr(err));
249102ac6454SAndrew Thompson 	}
249277ddf3d3SAndrew Thompson 
249377ddf3d3SAndrew Thompson 	/* give the chip some time to boot */
249477ddf3d3SAndrew Thompson 	rum_pause(sc, hz / 8);
249502ac6454SAndrew Thompson }
249602ac6454SAndrew Thompson 
2497aa81f853SAdrian Chadd static int
2498b780f864SAdrian Chadd rum_set_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
249902ac6454SAndrew Thompson {
250002ac6454SAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
2501b780f864SAdrian Chadd 	struct rum_vap *rvp = RUM_VAP(vap);
2502b780f864SAdrian Chadd 	struct mbuf *m = rvp->bcn_mbuf;
250302ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
250402ac6454SAndrew Thompson 	struct rum_tx_desc desc;
250502ac6454SAndrew Thompson 
2506b780f864SAdrian Chadd 	RUM_LOCK_ASSERT(sc);
2507b780f864SAdrian Chadd 
2508b780f864SAdrian Chadd 	if (m == NULL)
2509aa81f853SAdrian Chadd 		return EINVAL;
2510bb571462SHans Petter Selasky 	if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2511aa81f853SAdrian Chadd 		return EINVAL;
2512f620e64dSKevin Lo 
251302ac6454SAndrew Thompson 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
25142cb9ef8dSAdrian Chadd 	rum_setup_tx_desc(sc, &desc, NULL, RT2573_TX_TIMESTAMP,
25151354b52cSAdrian Chadd 	    RT2573_TX_HWSEQ, 0, 0, m->m_pkthdr.len, tp->mgmtrate);
251602ac6454SAndrew Thompson 
2517b780f864SAdrian Chadd 	/* copy the Tx descriptor into NIC memory */
2518b780f864SAdrian Chadd 	if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0), (uint8_t *)&desc,
2519b780f864SAdrian Chadd 	    RT2573_TX_DESC_SIZE) != 0)
2520b780f864SAdrian Chadd 		return EIO;
252102ac6454SAndrew Thompson 
252202ac6454SAndrew Thompson 	/* copy beacon header and payload into NIC memory */
2523b780f864SAdrian Chadd 	if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0) + RT2573_TX_DESC_SIZE,
2524b780f864SAdrian Chadd 	    mtod(m, uint8_t *), m->m_pkthdr.len) != 0)
2525b780f864SAdrian Chadd 		return EIO;
252602ac6454SAndrew Thompson 
2527b780f864SAdrian Chadd 	return 0;
2528b780f864SAdrian Chadd }
2529b780f864SAdrian Chadd 
2530b780f864SAdrian Chadd static int
2531b780f864SAdrian Chadd rum_alloc_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2532b780f864SAdrian Chadd {
2533b780f864SAdrian Chadd 	struct rum_vap *rvp = RUM_VAP(vap);
2534b780f864SAdrian Chadd 	struct ieee80211_node *ni = vap->iv_bss;
2535b780f864SAdrian Chadd 	struct mbuf *m;
2536b780f864SAdrian Chadd 
2537b780f864SAdrian Chadd 	if (ni->ni_chan == IEEE80211_CHAN_ANYC)
2538b780f864SAdrian Chadd 		return EINVAL;
2539b780f864SAdrian Chadd 
2540210ab3c2SAdrian Chadd 	m = ieee80211_beacon_alloc(ni);
2541b780f864SAdrian Chadd 	if (m == NULL)
2542b780f864SAdrian Chadd 		return ENOMEM;
2543b780f864SAdrian Chadd 
2544b780f864SAdrian Chadd 	if (rvp->bcn_mbuf != NULL)
2545b780f864SAdrian Chadd 		m_freem(rvp->bcn_mbuf);
2546b780f864SAdrian Chadd 
2547b780f864SAdrian Chadd 	rvp->bcn_mbuf = m;
2548b780f864SAdrian Chadd 
2549b780f864SAdrian Chadd 	return (rum_set_beacon(sc, vap));
255002ac6454SAndrew Thompson }
255102ac6454SAndrew Thompson 
25520e3b4c60SAdrian Chadd static void
25530e3b4c60SAdrian Chadd rum_update_beacon_cb(struct rum_softc *sc, union sec_param *data,
2554ed5711a1SAdrian Chadd     uint8_t rvp_id)
25550e3b4c60SAdrian Chadd {
25560e3b4c60SAdrian Chadd 	struct ieee80211vap *vap = data->vap;
25570e3b4c60SAdrian Chadd 
25580e3b4c60SAdrian Chadd 	rum_set_beacon(sc, vap);
25590e3b4c60SAdrian Chadd }
25600e3b4c60SAdrian Chadd 
25610e3b4c60SAdrian Chadd static void
25620e3b4c60SAdrian Chadd rum_update_beacon(struct ieee80211vap *vap, int item)
25630e3b4c60SAdrian Chadd {
25640e3b4c60SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
25650e3b4c60SAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
25660e3b4c60SAdrian Chadd 	struct rum_vap *rvp = RUM_VAP(vap);
25670e3b4c60SAdrian Chadd 	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
25680e3b4c60SAdrian Chadd 	struct ieee80211_node *ni = vap->iv_bss;
25690e3b4c60SAdrian Chadd 	struct mbuf *m = rvp->bcn_mbuf;
25700e3b4c60SAdrian Chadd 	int mcast = 0;
25710e3b4c60SAdrian Chadd 
25720e3b4c60SAdrian Chadd 	RUM_LOCK(sc);
25730e3b4c60SAdrian Chadd 	if (m == NULL) {
2574210ab3c2SAdrian Chadd 		m = ieee80211_beacon_alloc(ni);
25750e3b4c60SAdrian Chadd 		if (m == NULL) {
25760e3b4c60SAdrian Chadd 			device_printf(sc->sc_dev,
25770e3b4c60SAdrian Chadd 			    "%s: could not allocate beacon frame\n", __func__);
25780e3b4c60SAdrian Chadd 			RUM_UNLOCK(sc);
25790e3b4c60SAdrian Chadd 			return;
25800e3b4c60SAdrian Chadd 		}
25810e3b4c60SAdrian Chadd 		rvp->bcn_mbuf = m;
25820e3b4c60SAdrian Chadd 	}
25830e3b4c60SAdrian Chadd 
25840e3b4c60SAdrian Chadd 	switch (item) {
25850e3b4c60SAdrian Chadd 	case IEEE80211_BEACON_ERP:
25860e3b4c60SAdrian Chadd 		rum_update_slot(ic);
25870e3b4c60SAdrian Chadd 		break;
25880e3b4c60SAdrian Chadd 	case IEEE80211_BEACON_TIM:
25890e3b4c60SAdrian Chadd 		mcast = 1;	/*TODO*/
25900e3b4c60SAdrian Chadd 		break;
25910e3b4c60SAdrian Chadd 	default:
25920e3b4c60SAdrian Chadd 		break;
25930e3b4c60SAdrian Chadd 	}
25940e3b4c60SAdrian Chadd 	RUM_UNLOCK(sc);
25950e3b4c60SAdrian Chadd 
25960e3b4c60SAdrian Chadd 	setbit(bo->bo_flags, item);
2597210ab3c2SAdrian Chadd 	ieee80211_beacon_update(ni, m, mcast);
25980e3b4c60SAdrian Chadd 
2599ed5711a1SAdrian Chadd 	rum_cmd_sleepable(sc, &vap, sizeof(vap), 0, rum_update_beacon_cb);
26000e3b4c60SAdrian Chadd }
26010e3b4c60SAdrian Chadd 
260202ac6454SAndrew Thompson static int
26032cb9ef8dSAdrian Chadd rum_common_key_set(struct rum_softc *sc, struct ieee80211_key *k,
26042cb9ef8dSAdrian Chadd     uint16_t base)
26052cb9ef8dSAdrian Chadd {
26062cb9ef8dSAdrian Chadd 
26072cb9ef8dSAdrian Chadd 	if (rum_write_multi(sc, base, k->wk_key, k->wk_keylen))
26082cb9ef8dSAdrian Chadd 		return EIO;
26092cb9ef8dSAdrian Chadd 
26102cb9ef8dSAdrian Chadd 	if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
26112cb9ef8dSAdrian Chadd 		if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE,
26122cb9ef8dSAdrian Chadd 		    k->wk_txmic, 8))
26132cb9ef8dSAdrian Chadd 			return EIO;
26142cb9ef8dSAdrian Chadd 		if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE + 8,
26152cb9ef8dSAdrian Chadd 		    k->wk_rxmic, 8))
26162cb9ef8dSAdrian Chadd 			return EIO;
26172cb9ef8dSAdrian Chadd 	}
26182cb9ef8dSAdrian Chadd 
26192cb9ef8dSAdrian Chadd 	return 0;
26202cb9ef8dSAdrian Chadd }
26212cb9ef8dSAdrian Chadd 
26222cb9ef8dSAdrian Chadd static void
26232cb9ef8dSAdrian Chadd rum_group_key_set_cb(struct rum_softc *sc, union sec_param *data,
2624ed5711a1SAdrian Chadd     uint8_t rvp_id)
26252cb9ef8dSAdrian Chadd {
26262cb9ef8dSAdrian Chadd 	struct ieee80211_key *k = &data->key;
26272cb9ef8dSAdrian Chadd 	uint8_t mode;
26282cb9ef8dSAdrian Chadd 
26292cb9ef8dSAdrian Chadd 	if (sc->sc_clr_shkeys == 0) {
26302cb9ef8dSAdrian Chadd 		rum_clr_shkey_regs(sc);
26312cb9ef8dSAdrian Chadd 		sc->sc_clr_shkeys = 1;
26322cb9ef8dSAdrian Chadd 	}
26332cb9ef8dSAdrian Chadd 
26342cb9ef8dSAdrian Chadd 	mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
26352cb9ef8dSAdrian Chadd 	if (mode == 0)
26362cb9ef8dSAdrian Chadd 		goto print_err;
26372cb9ef8dSAdrian Chadd 
26382cb9ef8dSAdrian Chadd 	DPRINTFN(1, "setting group key %d for vap %d, mode %d "
26392cb9ef8dSAdrian Chadd 	    "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
26402cb9ef8dSAdrian Chadd 	    (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
26412cb9ef8dSAdrian Chadd 	    (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
26422cb9ef8dSAdrian Chadd 
26432cb9ef8dSAdrian Chadd 	/* Install the key. */
26442cb9ef8dSAdrian Chadd 	if (rum_common_key_set(sc, k, RT2573_SKEY(rvp_id, k->wk_keyix)) != 0)
26452cb9ef8dSAdrian Chadd 		goto print_err;
26462cb9ef8dSAdrian Chadd 
26472cb9ef8dSAdrian Chadd 	/* Set cipher mode. */
26482cb9ef8dSAdrian Chadd 	if (rum_modbits(sc, rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
26492cb9ef8dSAdrian Chadd 	      mode << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX,
26502cb9ef8dSAdrian Chadd 	      RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX)
26512cb9ef8dSAdrian Chadd 	    != 0)
26522cb9ef8dSAdrian Chadd 		goto print_err;
26532cb9ef8dSAdrian Chadd 
26542cb9ef8dSAdrian Chadd 	/* Mark this key as valid. */
26552cb9ef8dSAdrian Chadd 	if (rum_setbits(sc, RT2573_SEC_CSR0,
26562cb9ef8dSAdrian Chadd 	      1 << (rvp_id * RT2573_SKEY_MAX + k->wk_keyix)) != 0)
26572cb9ef8dSAdrian Chadd 		goto print_err;
26582cb9ef8dSAdrian Chadd 
26592cb9ef8dSAdrian Chadd 	return;
26602cb9ef8dSAdrian Chadd 
26612cb9ef8dSAdrian Chadd print_err:
26622cb9ef8dSAdrian Chadd 	device_printf(sc->sc_dev, "%s: cannot set group key %d for vap %d\n",
26632cb9ef8dSAdrian Chadd 	    __func__, k->wk_keyix, rvp_id);
26642cb9ef8dSAdrian Chadd }
26652cb9ef8dSAdrian Chadd 
26662cb9ef8dSAdrian Chadd static void
26672cb9ef8dSAdrian Chadd rum_group_key_del_cb(struct rum_softc *sc, union sec_param *data,
2668ed5711a1SAdrian Chadd     uint8_t rvp_id)
26692cb9ef8dSAdrian Chadd {
26702cb9ef8dSAdrian Chadd 	struct ieee80211_key *k = &data->key;
26712cb9ef8dSAdrian Chadd 
26722cb9ef8dSAdrian Chadd 	DPRINTF("%s: removing group key %d for vap %d\n", __func__,
26732cb9ef8dSAdrian Chadd 	    k->wk_keyix, rvp_id);
26742cb9ef8dSAdrian Chadd 	rum_clrbits(sc,
26752cb9ef8dSAdrian Chadd 	    rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
26762cb9ef8dSAdrian Chadd 	    RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX);
26772cb9ef8dSAdrian Chadd 	rum_clrbits(sc, RT2573_SEC_CSR0,
26782cb9ef8dSAdrian Chadd 	    rvp_id * RT2573_SKEY_MAX + k->wk_keyix);
26792cb9ef8dSAdrian Chadd }
26802cb9ef8dSAdrian Chadd 
26812cb9ef8dSAdrian Chadd static void
2682ed5711a1SAdrian Chadd rum_pair_key_set_cb(struct rum_softc *sc, union sec_param *data,
26832cb9ef8dSAdrian Chadd     uint8_t rvp_id)
26842cb9ef8dSAdrian Chadd {
26852cb9ef8dSAdrian Chadd 	struct ieee80211_key *k = &data->key;
26862cb9ef8dSAdrian Chadd 	uint8_t buf[IEEE80211_ADDR_LEN + 1];
26872cb9ef8dSAdrian Chadd 	uint8_t mode;
26882cb9ef8dSAdrian Chadd 
26892cb9ef8dSAdrian Chadd 	mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
26902cb9ef8dSAdrian Chadd 	if (mode == 0)
26912cb9ef8dSAdrian Chadd 		goto print_err;
26922cb9ef8dSAdrian Chadd 
26932cb9ef8dSAdrian Chadd 	DPRINTFN(1, "setting pairwise key %d for vap %d, mode %d "
26942cb9ef8dSAdrian Chadd 	    "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
26952cb9ef8dSAdrian Chadd 	    (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
26962cb9ef8dSAdrian Chadd 	    (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
26972cb9ef8dSAdrian Chadd 
26982cb9ef8dSAdrian Chadd 	/* Install the key. */
26992cb9ef8dSAdrian Chadd 	if (rum_common_key_set(sc, k, RT2573_PKEY(k->wk_keyix)) != 0)
27002cb9ef8dSAdrian Chadd 		goto print_err;
27012cb9ef8dSAdrian Chadd 
27022cb9ef8dSAdrian Chadd 	IEEE80211_ADDR_COPY(buf, k->wk_macaddr);
27032cb9ef8dSAdrian Chadd 	buf[IEEE80211_ADDR_LEN] = mode;
27042cb9ef8dSAdrian Chadd 
27052cb9ef8dSAdrian Chadd 	/* Set transmitter address and cipher mode. */
27062cb9ef8dSAdrian Chadd 	if (rum_write_multi(sc, RT2573_ADDR_ENTRY(k->wk_keyix),
27072cb9ef8dSAdrian Chadd 	      buf, sizeof buf) != 0)
27082cb9ef8dSAdrian Chadd 		goto print_err;
27092cb9ef8dSAdrian Chadd 
27102cb9ef8dSAdrian Chadd 	/* Enable key table lookup for this vap. */
27112cb9ef8dSAdrian Chadd 	if (sc->vap_key_count[rvp_id]++ == 0)
27122cb9ef8dSAdrian Chadd 		if (rum_setbits(sc, RT2573_SEC_CSR4, 1 << rvp_id) != 0)
27132cb9ef8dSAdrian Chadd 			goto print_err;
27142cb9ef8dSAdrian Chadd 
27152cb9ef8dSAdrian Chadd 	/* Mark this key as valid. */
27162cb9ef8dSAdrian Chadd 	if (rum_setbits(sc,
27172cb9ef8dSAdrian Chadd 	      k->wk_keyix < 32 ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
27182cb9ef8dSAdrian Chadd 	      1 << (k->wk_keyix % 32)) != 0)
27192cb9ef8dSAdrian Chadd 		goto print_err;
27202cb9ef8dSAdrian Chadd 
27212cb9ef8dSAdrian Chadd 	return;
27222cb9ef8dSAdrian Chadd 
27232cb9ef8dSAdrian Chadd print_err:
27242cb9ef8dSAdrian Chadd 	device_printf(sc->sc_dev,
27252cb9ef8dSAdrian Chadd 	    "%s: cannot set pairwise key %d, vap %d\n", __func__, k->wk_keyix,
27262cb9ef8dSAdrian Chadd 	    rvp_id);
27272cb9ef8dSAdrian Chadd }
27282cb9ef8dSAdrian Chadd 
27292cb9ef8dSAdrian Chadd static void
2730ed5711a1SAdrian Chadd rum_pair_key_del_cb(struct rum_softc *sc, union sec_param *data,
27312cb9ef8dSAdrian Chadd     uint8_t rvp_id)
27322cb9ef8dSAdrian Chadd {
27332cb9ef8dSAdrian Chadd 	struct ieee80211_key *k = &data->key;
27342cb9ef8dSAdrian Chadd 
27352cb9ef8dSAdrian Chadd 	DPRINTF("%s: removing key %d\n", __func__, k->wk_keyix);
27362cb9ef8dSAdrian Chadd 	rum_clrbits(sc, (k->wk_keyix < 32) ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
27372cb9ef8dSAdrian Chadd 	    1 << (k->wk_keyix % 32));
2738fe3e4523SHans Petter Selasky 	sc->keys_bmap &= ~(1ULL << k->wk_keyix);
27392cb9ef8dSAdrian Chadd 	if (--sc->vap_key_count[rvp_id] == 0)
27402cb9ef8dSAdrian Chadd 		rum_clrbits(sc, RT2573_SEC_CSR4, 1 << rvp_id);
27412cb9ef8dSAdrian Chadd }
27422cb9ef8dSAdrian Chadd 
27432cb9ef8dSAdrian Chadd static int
27442cb9ef8dSAdrian Chadd rum_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
27452cb9ef8dSAdrian Chadd     ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
27462cb9ef8dSAdrian Chadd {
27472cb9ef8dSAdrian Chadd 	struct rum_softc *sc = vap->iv_ic->ic_softc;
27482cb9ef8dSAdrian Chadd 	uint8_t i;
27492cb9ef8dSAdrian Chadd 
27502cb9ef8dSAdrian Chadd 	if (!(&vap->iv_nw_keys[0] <= k &&
27512cb9ef8dSAdrian Chadd 	     k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
27522cb9ef8dSAdrian Chadd 		if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
27532cb9ef8dSAdrian Chadd 			RUM_LOCK(sc);
27542cb9ef8dSAdrian Chadd 			for (i = 0; i < RT2573_ADDR_MAX; i++) {
2755fe3e4523SHans Petter Selasky 				if ((sc->keys_bmap & (1ULL << i)) == 0) {
2756fe3e4523SHans Petter Selasky 					sc->keys_bmap |= (1ULL << i);
27572cb9ef8dSAdrian Chadd 					*keyix = i;
27582cb9ef8dSAdrian Chadd 					break;
27592cb9ef8dSAdrian Chadd 				}
27602cb9ef8dSAdrian Chadd 			}
27612cb9ef8dSAdrian Chadd 			RUM_UNLOCK(sc);
27622cb9ef8dSAdrian Chadd 			if (i == RT2573_ADDR_MAX) {
27632cb9ef8dSAdrian Chadd 				device_printf(sc->sc_dev,
27642cb9ef8dSAdrian Chadd 				    "%s: no free space in the key table\n",
27652cb9ef8dSAdrian Chadd 				    __func__);
27662cb9ef8dSAdrian Chadd 				return 0;
27672cb9ef8dSAdrian Chadd 			}
27682cb9ef8dSAdrian Chadd 		} else
27692cb9ef8dSAdrian Chadd 			*keyix = 0;
27702cb9ef8dSAdrian Chadd 	} else {
27712cb9ef8dSAdrian Chadd 		*keyix = k - vap->iv_nw_keys;
27722cb9ef8dSAdrian Chadd 	}
27732cb9ef8dSAdrian Chadd 	*rxkeyix = *keyix;
27742cb9ef8dSAdrian Chadd 	return 1;
27752cb9ef8dSAdrian Chadd }
27762cb9ef8dSAdrian Chadd 
27772cb9ef8dSAdrian Chadd static int
2778bc813c40SAdrian Chadd rum_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
27792cb9ef8dSAdrian Chadd {
27802cb9ef8dSAdrian Chadd 	struct rum_softc *sc = vap->iv_ic->ic_softc;
27812cb9ef8dSAdrian Chadd 	int group;
27822cb9ef8dSAdrian Chadd 
27832cb9ef8dSAdrian Chadd 	if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
27842cb9ef8dSAdrian Chadd 		/* Not for us. */
27852cb9ef8dSAdrian Chadd 		return 1;
27862cb9ef8dSAdrian Chadd 	}
27872cb9ef8dSAdrian Chadd 
27882cb9ef8dSAdrian Chadd 	group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
27892cb9ef8dSAdrian Chadd 
2790ed5711a1SAdrian Chadd 	return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
27912cb9ef8dSAdrian Chadd 		   group ? rum_group_key_set_cb : rum_pair_key_set_cb);
27922cb9ef8dSAdrian Chadd }
27932cb9ef8dSAdrian Chadd 
27942cb9ef8dSAdrian Chadd static int
27952cb9ef8dSAdrian Chadd rum_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
27962cb9ef8dSAdrian Chadd {
27972cb9ef8dSAdrian Chadd 	struct rum_softc *sc = vap->iv_ic->ic_softc;
27982cb9ef8dSAdrian Chadd 	int group;
27992cb9ef8dSAdrian Chadd 
28002cb9ef8dSAdrian Chadd 	if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
28012cb9ef8dSAdrian Chadd 		/* Not for us. */
28022cb9ef8dSAdrian Chadd 		return 1;
28032cb9ef8dSAdrian Chadd 	}
28042cb9ef8dSAdrian Chadd 
28052cb9ef8dSAdrian Chadd 	group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
28062cb9ef8dSAdrian Chadd 
2807ed5711a1SAdrian Chadd 	return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
28082cb9ef8dSAdrian Chadd 		   group ? rum_group_key_del_cb : rum_pair_key_del_cb);
28092cb9ef8dSAdrian Chadd }
28102cb9ef8dSAdrian Chadd 
28112cb9ef8dSAdrian Chadd static int
281202ac6454SAndrew Thompson rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
281302ac6454SAndrew Thompson     const struct ieee80211_bpf_params *params)
281402ac6454SAndrew Thompson {
2815d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ni->ni_ic->ic_softc;
2816b2a76342SAdrian Chadd 	int ret;
281702ac6454SAndrew Thompson 
281802ac6454SAndrew Thompson 	RUM_LOCK(sc);
281902ac6454SAndrew Thompson 	/* prevent management frames from being sent if we're not ready */
28207a79cebfSGleb Smirnoff 	if (!sc->sc_running) {
2821b2a76342SAdrian Chadd 		ret = ENETDOWN;
2822b2a76342SAdrian Chadd 		goto bad;
282302ac6454SAndrew Thompson 	}
282496ca458fSAndrew Thompson 	if (sc->tx_nfree < RUM_TX_MINFREE) {
2825b2a76342SAdrian Chadd 		ret = EIO;
2826b2a76342SAdrian Chadd 		goto bad;
282702ac6454SAndrew Thompson 	}
282802ac6454SAndrew Thompson 
282902ac6454SAndrew Thompson 	if (params == NULL) {
283002ac6454SAndrew Thompson 		/*
283102ac6454SAndrew Thompson 		 * Legacy path; interpret frame contents to decide
283202ac6454SAndrew Thompson 		 * precisely how to send the frame.
283302ac6454SAndrew Thompson 		 */
2834b2a76342SAdrian Chadd 		if ((ret = rum_tx_mgt(sc, m, ni)) != 0)
283502ac6454SAndrew Thompson 			goto bad;
283602ac6454SAndrew Thompson 	} else {
283702ac6454SAndrew Thompson 		/*
283802ac6454SAndrew Thompson 		 * Caller supplied explicit parameters to use in
283902ac6454SAndrew Thompson 		 * sending the frame.
284002ac6454SAndrew Thompson 		 */
2841b2a76342SAdrian Chadd 		if ((ret = rum_tx_raw(sc, m, ni, params)) != 0)
284202ac6454SAndrew Thompson 			goto bad;
284302ac6454SAndrew Thompson 	}
284402ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
284502ac6454SAndrew Thompson 
284602ac6454SAndrew Thompson 	return 0;
284702ac6454SAndrew Thompson bad:
284802ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
2849b2a76342SAdrian Chadd 	m_freem(m);
2850b2a76342SAdrian Chadd 	return ret;
285102ac6454SAndrew Thompson }
285202ac6454SAndrew Thompson 
285302ac6454SAndrew Thompson static void
2854b6108616SRui Paulo rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
285502ac6454SAndrew Thompson {
285602ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
285702ac6454SAndrew Thompson 	struct rum_vap *rvp = RUM_VAP(vap);
285802ac6454SAndrew Thompson 
285902ac6454SAndrew Thompson 	/* clear statistic registers (STA_CSR0 to STA_CSR5) */
286002ac6454SAndrew Thompson 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
286102ac6454SAndrew Thompson 
2862b6108616SRui Paulo 	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
286302ac6454SAndrew Thompson }
286402ac6454SAndrew Thompson 
286502ac6454SAndrew Thompson static void
2866b6108616SRui Paulo rum_ratectl_timeout(void *arg)
286702ac6454SAndrew Thompson {
286802ac6454SAndrew Thompson 	struct rum_vap *rvp = arg;
28695efea30fSAndrew Thompson 	struct ieee80211vap *vap = &rvp->vap;
28705efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
287102ac6454SAndrew Thompson 
2872b6108616SRui Paulo 	ieee80211_runtask(ic, &rvp->ratectl_task);
287302ac6454SAndrew Thompson }
287402ac6454SAndrew Thompson 
287502ac6454SAndrew Thompson static void
2876b6108616SRui Paulo rum_ratectl_task(void *arg, int pending)
287702ac6454SAndrew Thompson {
28785efea30fSAndrew Thompson 	struct rum_vap *rvp = arg;
28795efea30fSAndrew Thompson 	struct ieee80211vap *vap = &rvp->vap;
2880f4ac78a2SAdrian Chadd 	struct rum_softc *sc = vap->iv_ic->ic_softc;
28818d8bdb01SAndrew Thompson 	struct ieee80211_node *ni;
2882f4ac78a2SAdrian Chadd 	int ok[3], fail;
2883f4ac78a2SAdrian Chadd 	int sum, success, retrycnt;
288402ac6454SAndrew Thompson 
28855efea30fSAndrew Thompson 	RUM_LOCK(sc);
2886f4ac78a2SAdrian Chadd 	/* read and clear statistic registers (STA_CSR0 to STA_CSR5) */
288702ac6454SAndrew Thompson 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
288802ac6454SAndrew Thompson 
2889f4ac78a2SAdrian Chadd 	ok[0] = (le32toh(sc->sta[4]) & 0xffff);	/* TX ok w/o retry */
2890f4ac78a2SAdrian Chadd 	ok[1] = (le32toh(sc->sta[4]) >> 16);	/* TX ok w/ one retry */
2891f4ac78a2SAdrian Chadd 	ok[2] = (le32toh(sc->sta[5]) & 0xffff);	/* TX ok w/ multiple retries */
289202ac6454SAndrew Thompson 	fail =  (le32toh(sc->sta[5]) >> 16);	/* TX retry-fail count */
289302ac6454SAndrew Thompson 
2894f4ac78a2SAdrian Chadd 	success = ok[0] + ok[1] + ok[2];
2895f4ac78a2SAdrian Chadd 	sum = success + fail;
2896f4ac78a2SAdrian Chadd 	/* XXX at least */
2897f4ac78a2SAdrian Chadd 	retrycnt = ok[1] + ok[2] * 2 + fail * (rvp->maxretry + 1);
2898f4ac78a2SAdrian Chadd 
2899f4ac78a2SAdrian Chadd 	if (sum != 0) {
29008d8bdb01SAndrew Thompson 		ni = ieee80211_ref_node(vap->iv_bss);
2901b6108616SRui Paulo 		ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt);
2902b6108616SRui Paulo 		(void) ieee80211_ratectl_rate(ni, NULL, 0);
29038d8bdb01SAndrew Thompson 		ieee80211_free_node(ni);
2904f4ac78a2SAdrian Chadd 	}
290502ac6454SAndrew Thompson 
29067a79cebfSGleb Smirnoff 	/* count TX retry-fail as Tx errors */
2907f4ac78a2SAdrian Chadd 	if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, fail);
290802ac6454SAndrew Thompson 
2909b6108616SRui Paulo 	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
29105efea30fSAndrew Thompson 	RUM_UNLOCK(sc);
291102ac6454SAndrew Thompson }
291202ac6454SAndrew Thompson 
291302ac6454SAndrew Thompson static void
291402ac6454SAndrew Thompson rum_scan_start(struct ieee80211com *ic)
291502ac6454SAndrew Thompson {
2916d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
291702ac6454SAndrew Thompson 
291802ac6454SAndrew Thompson 	RUM_LOCK(sc);
29196d804321SAdrian Chadd 	rum_abort_tsf_sync(sc);
29207a79cebfSGleb Smirnoff 	rum_set_bssid(sc, ieee80211broadcastaddr);
292102ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
292202ac6454SAndrew Thompson 
292302ac6454SAndrew Thompson }
292402ac6454SAndrew Thompson 
292502ac6454SAndrew Thompson static void
292602ac6454SAndrew Thompson rum_scan_end(struct ieee80211com *ic)
292702ac6454SAndrew Thompson {
2928d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
292902ac6454SAndrew Thompson 
293037704427SAndriy Voskoboinyk 	if (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) {
293102ac6454SAndrew Thompson 		RUM_LOCK(sc);
293250a31b48SAdrian Chadd 		if (ic->ic_opmode != IEEE80211_M_AHDEMO)
29335efea30fSAndrew Thompson 			rum_enable_tsf_sync(sc);
293450a31b48SAdrian Chadd 		else
293550a31b48SAdrian Chadd 			rum_enable_tsf(sc);
29361ba67b11SAdrian Chadd 		rum_set_bssid(sc, sc->sc_bssid);
293702ac6454SAndrew Thompson 		RUM_UNLOCK(sc);
293837704427SAndriy Voskoboinyk 	}
293902ac6454SAndrew Thompson }
294002ac6454SAndrew Thompson 
294102ac6454SAndrew Thompson static void
294202ac6454SAndrew Thompson rum_set_channel(struct ieee80211com *ic)
294302ac6454SAndrew Thompson {
2944d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
294502ac6454SAndrew Thompson 
294602ac6454SAndrew Thompson 	RUM_LOCK(sc);
294702ac6454SAndrew Thompson 	rum_set_chan(sc, ic->ic_curchan);
29485efea30fSAndrew Thompson 	RUM_UNLOCK(sc);
294902ac6454SAndrew Thompson }
295002ac6454SAndrew Thompson 
295102ac6454SAndrew Thompson static int
295202ac6454SAndrew Thompson rum_get_rssi(struct rum_softc *sc, uint8_t raw)
295302ac6454SAndrew Thompson {
29547a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
295502ac6454SAndrew Thompson 	int lna, agc, rssi;
295602ac6454SAndrew Thompson 
295702ac6454SAndrew Thompson 	lna = (raw >> 5) & 0x3;
295802ac6454SAndrew Thompson 	agc = raw & 0x1f;
295902ac6454SAndrew Thompson 
296002ac6454SAndrew Thompson 	if (lna == 0) {
296102ac6454SAndrew Thompson 		/*
296202ac6454SAndrew Thompson 		 * No RSSI mapping
296302ac6454SAndrew Thompson 		 *
296402ac6454SAndrew Thompson 		 * NB: Since RSSI is relative to noise floor, -1 is
296502ac6454SAndrew Thompson 		 *     adequate for caller to know error happened.
296602ac6454SAndrew Thompson 		 */
296702ac6454SAndrew Thompson 		return -1;
296802ac6454SAndrew Thompson 	}
296902ac6454SAndrew Thompson 
297002ac6454SAndrew Thompson 	rssi = (2 * agc) - RT2573_NOISE_FLOOR;
297102ac6454SAndrew Thompson 
297202ac6454SAndrew Thompson 	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
297302ac6454SAndrew Thompson 		rssi += sc->rssi_2ghz_corr;
297402ac6454SAndrew Thompson 
297502ac6454SAndrew Thompson 		if (lna == 1)
297602ac6454SAndrew Thompson 			rssi -= 64;
297702ac6454SAndrew Thompson 		else if (lna == 2)
297802ac6454SAndrew Thompson 			rssi -= 74;
297902ac6454SAndrew Thompson 		else if (lna == 3)
298002ac6454SAndrew Thompson 			rssi -= 90;
298102ac6454SAndrew Thompson 	} else {
298202ac6454SAndrew Thompson 		rssi += sc->rssi_5ghz_corr;
298302ac6454SAndrew Thompson 
298402ac6454SAndrew Thompson 		if (!sc->ext_5ghz_lna && lna != 1)
298502ac6454SAndrew Thompson 			rssi += 4;
298602ac6454SAndrew Thompson 
298702ac6454SAndrew Thompson 		if (lna == 1)
298802ac6454SAndrew Thompson 			rssi -= 64;
298902ac6454SAndrew Thompson 		else if (lna == 2)
299002ac6454SAndrew Thompson 			rssi -= 86;
299102ac6454SAndrew Thompson 		else if (lna == 3)
299202ac6454SAndrew Thompson 			rssi -= 100;
299302ac6454SAndrew Thompson 	}
299402ac6454SAndrew Thompson 	return rssi;
299502ac6454SAndrew Thompson }
299602ac6454SAndrew Thompson 
299702ac6454SAndrew Thompson static int
299802ac6454SAndrew Thompson rum_pause(struct rum_softc *sc, int timeout)
299902ac6454SAndrew Thompson {
300002ac6454SAndrew Thompson 
3001a593f6b8SAndrew Thompson 	usb_pause_mtx(&sc->sc_mtx, timeout);
300202ac6454SAndrew Thompson 	return (0);
300302ac6454SAndrew Thompson }
300402ac6454SAndrew Thompson 
300502ac6454SAndrew Thompson static device_method_t rum_methods[] = {
300602ac6454SAndrew Thompson 	/* Device interface */
300702ac6454SAndrew Thompson 	DEVMETHOD(device_probe,		rum_match),
300802ac6454SAndrew Thompson 	DEVMETHOD(device_attach,	rum_attach),
300902ac6454SAndrew Thompson 	DEVMETHOD(device_detach,	rum_detach),
3010645e4d17SHans Petter Selasky 	DEVMETHOD_END
301102ac6454SAndrew Thompson };
301202ac6454SAndrew Thompson 
301302ac6454SAndrew Thompson static driver_t rum_driver = {
301402ac6454SAndrew Thompson 	.name = "rum",
301502ac6454SAndrew Thompson 	.methods = rum_methods,
301602ac6454SAndrew Thompson 	.size = sizeof(struct rum_softc),
301702ac6454SAndrew Thompson };
301802ac6454SAndrew Thompson 
301902ac6454SAndrew Thompson static devclass_t rum_devclass;
302002ac6454SAndrew Thompson 
30219aef556dSAndrew Thompson DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
3022910cb8feSAndrew Thompson MODULE_DEPEND(rum, wlan, 1, 1, 1);
3023910cb8feSAndrew Thompson MODULE_DEPEND(rum, usb, 1, 1, 1);
3024910cb8feSAndrew Thompson MODULE_VERSION(rum, 1);
3025f809f280SWarner Losh USB_PNP_HOST_INFO(rum_devs);
3026