xref: /freebsd/sys/dev/usb/wlan/if_rum.c (revision 2cb9ef8d38ca96deb887691204a0f23ecf6f79e5)
102ac6454SAndrew Thompson /*	$FreeBSD$	*/
202ac6454SAndrew Thompson 
302ac6454SAndrew Thompson /*-
402ac6454SAndrew Thompson  * Copyright (c) 2005-2007 Damien Bergamini <damien.bergamini@free.fr>
502ac6454SAndrew Thompson  * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
65d38a4d4SEd Schouten  * Copyright (c) 2007-2008 Hans Petter Selasky <hselasky@FreeBSD.org>
702ac6454SAndrew Thompson  *
802ac6454SAndrew Thompson  * Permission to use, copy, modify, and distribute this software for any
902ac6454SAndrew Thompson  * purpose with or without fee is hereby granted, provided that the above
1002ac6454SAndrew Thompson  * copyright notice and this permission notice appear in all copies.
1102ac6454SAndrew Thompson  *
1202ac6454SAndrew Thompson  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1302ac6454SAndrew Thompson  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1402ac6454SAndrew Thompson  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1502ac6454SAndrew Thompson  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1602ac6454SAndrew Thompson  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1702ac6454SAndrew Thompson  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1802ac6454SAndrew Thompson  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1902ac6454SAndrew Thompson  */
2002ac6454SAndrew Thompson 
2102ac6454SAndrew Thompson #include <sys/cdefs.h>
2202ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
2302ac6454SAndrew Thompson 
2402ac6454SAndrew Thompson /*-
2502ac6454SAndrew Thompson  * Ralink Technology RT2501USB/RT2601USB chipset driver
2602ac6454SAndrew Thompson  * http://www.ralinktech.com.tw/
2702ac6454SAndrew Thompson  */
2802ac6454SAndrew Thompson 
295efea30fSAndrew Thompson #include <sys/param.h>
305efea30fSAndrew Thompson #include <sys/sockio.h>
315efea30fSAndrew Thompson #include <sys/sysctl.h>
325efea30fSAndrew Thompson #include <sys/lock.h>
335efea30fSAndrew Thompson #include <sys/mutex.h>
345efea30fSAndrew Thompson #include <sys/mbuf.h>
355efea30fSAndrew Thompson #include <sys/kernel.h>
365efea30fSAndrew Thompson #include <sys/socket.h>
375efea30fSAndrew Thompson #include <sys/systm.h>
385efea30fSAndrew Thompson #include <sys/malloc.h>
395efea30fSAndrew Thompson #include <sys/module.h>
405efea30fSAndrew Thompson #include <sys/bus.h>
415efea30fSAndrew Thompson #include <sys/endian.h>
425efea30fSAndrew Thompson #include <sys/kdb.h>
435efea30fSAndrew Thompson 
445efea30fSAndrew Thompson #include <machine/bus.h>
4567784314SPoul-Henning Kamp #include <machine/resource.h>
4667784314SPoul-Henning Kamp #include <sys/rman.h>
475efea30fSAndrew Thompson 
485efea30fSAndrew Thompson #include <net/bpf.h>
495efea30fSAndrew Thompson #include <net/if.h>
5076039bc8SGleb Smirnoff #include <net/if_var.h>
5167784314SPoul-Henning Kamp #include <net/if_arp.h>
525efea30fSAndrew Thompson #include <net/ethernet.h>
535efea30fSAndrew Thompson #include <net/if_dl.h>
545efea30fSAndrew Thompson #include <net/if_media.h>
555efea30fSAndrew Thompson #include <net/if_types.h>
565efea30fSAndrew Thompson 
575efea30fSAndrew Thompson #ifdef INET
585efea30fSAndrew Thompson #include <netinet/in.h>
595efea30fSAndrew Thompson #include <netinet/in_systm.h>
605efea30fSAndrew Thompson #include <netinet/in_var.h>
615efea30fSAndrew Thompson #include <netinet/if_ether.h>
625efea30fSAndrew Thompson #include <netinet/ip.h>
635efea30fSAndrew Thompson #endif
645efea30fSAndrew Thompson 
655efea30fSAndrew Thompson #include <net80211/ieee80211_var.h>
665efea30fSAndrew Thompson #include <net80211/ieee80211_regdomain.h>
675efea30fSAndrew Thompson #include <net80211/ieee80211_radiotap.h>
68b6108616SRui Paulo #include <net80211/ieee80211_ratectl.h>
6902ac6454SAndrew Thompson 
705efea30fSAndrew Thompson #include <dev/usb/usb.h>
71ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
725efea30fSAndrew Thompson #include "usbdevs.h"
7302ac6454SAndrew Thompson 
74ed6d949aSAndrew Thompson #define	USB_DEBUG_VAR rum_debug
75ed6d949aSAndrew Thompson #include <dev/usb/usb_debug.h>
76ed6d949aSAndrew Thompson 
7702ac6454SAndrew Thompson #include <dev/usb/wlan/if_rumreg.h>
7802ac6454SAndrew Thompson #include <dev/usb/wlan/if_rumvar.h>
7902ac6454SAndrew Thompson #include <dev/usb/wlan/if_rumfw.h>
8002ac6454SAndrew Thompson 
81b850ecc1SAndrew Thompson #ifdef USB_DEBUG
8202ac6454SAndrew Thompson static int rum_debug = 0;
8302ac6454SAndrew Thompson 
846472ac3dSEd Schouten static SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
85ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RWTUN, &rum_debug, 0,
8602ac6454SAndrew Thompson     "Debug level");
8702ac6454SAndrew Thompson #endif
8802ac6454SAndrew Thompson 
89f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID rum_devs[] = {
909e6b5313SAndrew Thompson #define	RUM_DEV(v,p)  { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
919e6b5313SAndrew Thompson     RUM_DEV(ABOCOM, HWU54DM),
929e6b5313SAndrew Thompson     RUM_DEV(ABOCOM, RT2573_2),
939e6b5313SAndrew Thompson     RUM_DEV(ABOCOM, RT2573_3),
949e6b5313SAndrew Thompson     RUM_DEV(ABOCOM, RT2573_4),
959e6b5313SAndrew Thompson     RUM_DEV(ABOCOM, WUG2700),
969e6b5313SAndrew Thompson     RUM_DEV(AMIT, CGWLUSB2GO),
979e6b5313SAndrew Thompson     RUM_DEV(ASUS, RT2573_1),
989e6b5313SAndrew Thompson     RUM_DEV(ASUS, RT2573_2),
999e6b5313SAndrew Thompson     RUM_DEV(BELKIN, F5D7050A),
1009e6b5313SAndrew Thompson     RUM_DEV(BELKIN, F5D9050V3),
1019e6b5313SAndrew Thompson     RUM_DEV(CISCOLINKSYS, WUSB54GC),
1029e6b5313SAndrew Thompson     RUM_DEV(CISCOLINKSYS, WUSB54GR),
1039e6b5313SAndrew Thompson     RUM_DEV(CONCEPTRONIC2, C54RU2),
1049e6b5313SAndrew Thompson     RUM_DEV(COREGA, CGWLUSB2GL),
1059e6b5313SAndrew Thompson     RUM_DEV(COREGA, CGWLUSB2GPX),
1069e6b5313SAndrew Thompson     RUM_DEV(DICKSMITH, CWD854F),
1079e6b5313SAndrew Thompson     RUM_DEV(DICKSMITH, RT2573),
108031c7388SAndrew Thompson     RUM_DEV(EDIMAX, EW7318USG),
1099e6b5313SAndrew Thompson     RUM_DEV(DLINK2, DWLG122C1),
1109e6b5313SAndrew Thompson     RUM_DEV(DLINK2, WUA1340),
1119e6b5313SAndrew Thompson     RUM_DEV(DLINK2, DWA111),
1129e6b5313SAndrew Thompson     RUM_DEV(DLINK2, DWA110),
1139e6b5313SAndrew Thompson     RUM_DEV(GIGABYTE, GNWB01GS),
1149e6b5313SAndrew Thompson     RUM_DEV(GIGABYTE, GNWI05GS),
1159e6b5313SAndrew Thompson     RUM_DEV(GIGASET, RT2573),
1169e6b5313SAndrew Thompson     RUM_DEV(GOODWAY, RT2573),
1179e6b5313SAndrew Thompson     RUM_DEV(GUILLEMOT, HWGUSB254LB),
1189e6b5313SAndrew Thompson     RUM_DEV(GUILLEMOT, HWGUSB254V2AP),
1199e6b5313SAndrew Thompson     RUM_DEV(HUAWEI3COM, WUB320G),
1209e6b5313SAndrew Thompson     RUM_DEV(MELCO, G54HP),
1219e6b5313SAndrew Thompson     RUM_DEV(MELCO, SG54HP),
1223232aae3SHans Petter Selasky     RUM_DEV(MELCO, SG54HG),
12354de08a9SMIHIRA Sanpei Yoshiro     RUM_DEV(MELCO, WLIUCG),
124e11ad60dSHans Petter Selasky     RUM_DEV(MELCO, WLRUCG),
125e11ad60dSHans Petter Selasky     RUM_DEV(MELCO, WLRUCGAOSS),
1269e6b5313SAndrew Thompson     RUM_DEV(MSI, RT2573_1),
1279e6b5313SAndrew Thompson     RUM_DEV(MSI, RT2573_2),
1289e6b5313SAndrew Thompson     RUM_DEV(MSI, RT2573_3),
1299e6b5313SAndrew Thompson     RUM_DEV(MSI, RT2573_4),
1309e6b5313SAndrew Thompson     RUM_DEV(NOVATECH, RT2573),
1319e6b5313SAndrew Thompson     RUM_DEV(PLANEX2, GWUS54HP),
1329e6b5313SAndrew Thompson     RUM_DEV(PLANEX2, GWUS54MINI2),
1339e6b5313SAndrew Thompson     RUM_DEV(PLANEX2, GWUSMM),
1349e6b5313SAndrew Thompson     RUM_DEV(QCOM, RT2573),
1359e6b5313SAndrew Thompson     RUM_DEV(QCOM, RT2573_2),
1369e6b5313SAndrew Thompson     RUM_DEV(QCOM, RT2573_3),
1379e6b5313SAndrew Thompson     RUM_DEV(RALINK, RT2573),
1389e6b5313SAndrew Thompson     RUM_DEV(RALINK, RT2573_2),
1399e6b5313SAndrew Thompson     RUM_DEV(RALINK, RT2671),
1409e6b5313SAndrew Thompson     RUM_DEV(SITECOMEU, WL113R2),
1419e6b5313SAndrew Thompson     RUM_DEV(SITECOMEU, WL172),
1429e6b5313SAndrew Thompson     RUM_DEV(SPARKLAN, RT2573),
1439e6b5313SAndrew Thompson     RUM_DEV(SURECOM, RT2573),
1449e6b5313SAndrew Thompson #undef RUM_DEV
14502ac6454SAndrew Thompson };
14602ac6454SAndrew Thompson 
14702ac6454SAndrew Thompson static device_probe_t rum_match;
14802ac6454SAndrew Thompson static device_attach_t rum_attach;
14902ac6454SAndrew Thompson static device_detach_t rum_detach;
15002ac6454SAndrew Thompson 
151e0a69b51SAndrew Thompson static usb_callback_t rum_bulk_read_callback;
152e0a69b51SAndrew Thompson static usb_callback_t rum_bulk_write_callback;
15302ac6454SAndrew Thompson 
154e0a69b51SAndrew Thompson static usb_error_t	rum_do_request(struct rum_softc *sc,
155760bc48eSAndrew Thompson 			    struct usb_device_request *req, void *data);
15602ac6454SAndrew Thompson static struct ieee80211vap *rum_vap_create(struct ieee80211com *,
157fcd9500fSBernhard Schmidt 			    const char [IFNAMSIZ], int, enum ieee80211_opmode,
158fcd9500fSBernhard Schmidt 			    int, const uint8_t [IEEE80211_ADDR_LEN],
159fcd9500fSBernhard Schmidt 			    const uint8_t [IEEE80211_ADDR_LEN]);
16002ac6454SAndrew Thompson static void		rum_vap_delete(struct ieee80211vap *);
161f28c2f5eSAdrian Chadd static void		rum_cmdq_cb(void *, int);
162f28c2f5eSAdrian Chadd static int		rum_cmd_sleepable(struct rum_softc *, const void *,
163f28c2f5eSAdrian Chadd 			    size_t, uint8_t, uint8_t, CMD_FUNC_PROTO);
16402ac6454SAndrew Thompson static void		rum_tx_free(struct rum_tx_data *, int);
16502ac6454SAndrew Thompson static void		rum_setup_tx_list(struct rum_softc *);
16602ac6454SAndrew Thompson static void		rum_unsetup_tx_list(struct rum_softc *);
16702ac6454SAndrew Thompson static int		rum_newstate(struct ieee80211vap *,
16802ac6454SAndrew Thompson 			    enum ieee80211_state, int);
169*2cb9ef8dSAdrian Chadd static uint8_t		rum_crypto_mode(struct rum_softc *, u_int, int);
17002ac6454SAndrew Thompson static void		rum_setup_tx_desc(struct rum_softc *,
171*2cb9ef8dSAdrian Chadd 			    struct rum_tx_desc *, struct ieee80211_key *,
172*2cb9ef8dSAdrian Chadd 			    uint32_t, uint8_t, int, int, int);
173*2cb9ef8dSAdrian Chadd static uint32_t		rum_tx_crypto_flags(struct rum_softc *,
174*2cb9ef8dSAdrian Chadd 			    struct ieee80211_node *,
175*2cb9ef8dSAdrian Chadd 			    const struct ieee80211_key *);
17602ac6454SAndrew Thompson static int		rum_tx_mgt(struct rum_softc *, struct mbuf *,
17702ac6454SAndrew Thompson 			    struct ieee80211_node *);
17802ac6454SAndrew Thompson static int		rum_tx_raw(struct rum_softc *, struct mbuf *,
17902ac6454SAndrew Thompson 			    struct ieee80211_node *,
18002ac6454SAndrew Thompson 			    const struct ieee80211_bpf_params *);
18102ac6454SAndrew Thompson static int		rum_tx_data(struct rum_softc *, struct mbuf *,
18202ac6454SAndrew Thompson 			    struct ieee80211_node *);
1837a79cebfSGleb Smirnoff static int		rum_transmit(struct ieee80211com *, struct mbuf *);
1847a79cebfSGleb Smirnoff static void		rum_start(struct rum_softc *);
1857a79cebfSGleb Smirnoff static void		rum_parent(struct ieee80211com *);
18602ac6454SAndrew Thompson static void		rum_eeprom_read(struct rum_softc *, uint16_t, void *,
18702ac6454SAndrew Thompson 			    int);
18802ac6454SAndrew Thompson static uint32_t		rum_read(struct rum_softc *, uint16_t);
18902ac6454SAndrew Thompson static void		rum_read_multi(struct rum_softc *, uint16_t, void *,
19002ac6454SAndrew Thompson 			    int);
191e0a69b51SAndrew Thompson static usb_error_t	rum_write(struct rum_softc *, uint16_t, uint32_t);
192e0a69b51SAndrew Thompson static usb_error_t	rum_write_multi(struct rum_softc *, uint16_t, void *,
19302ac6454SAndrew Thompson 			    size_t);
194a05022b2SAdrian Chadd static usb_error_t	rum_setbits(struct rum_softc *, uint16_t, uint32_t);
195a05022b2SAdrian Chadd static usb_error_t	rum_clrbits(struct rum_softc *, uint16_t, uint32_t);
196a05022b2SAdrian Chadd static usb_error_t	rum_modbits(struct rum_softc *, uint16_t, uint32_t,
197a05022b2SAdrian Chadd 			    uint32_t);
198cc9ae761SAdrian Chadd static int		rum_bbp_busy(struct rum_softc *);
19902ac6454SAndrew Thompson static void		rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
20002ac6454SAndrew Thompson static uint8_t		rum_bbp_read(struct rum_softc *, uint8_t);
20102ac6454SAndrew Thompson static void		rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
20202ac6454SAndrew Thompson static void		rum_select_antenna(struct rum_softc *);
20302ac6454SAndrew Thompson static void		rum_enable_mrr(struct rum_softc *);
20402ac6454SAndrew Thompson static void		rum_set_txpreamble(struct rum_softc *);
20502ac6454SAndrew Thompson static void		rum_set_basicrates(struct rum_softc *);
20602ac6454SAndrew Thompson static void		rum_select_band(struct rum_softc *,
20702ac6454SAndrew Thompson 			    struct ieee80211_channel *);
20802ac6454SAndrew Thompson static void		rum_set_chan(struct rum_softc *,
20902ac6454SAndrew Thompson 			    struct ieee80211_channel *);
210aa81f853SAdrian Chadd static int		rum_enable_tsf_sync(struct rum_softc *);
2115463c4a4SSam Leffler static void		rum_enable_tsf(struct rum_softc *);
2126d804321SAdrian Chadd static void		rum_abort_tsf_sync(struct rum_softc *);
213a6ccd477SAdrian Chadd static void		rum_get_tsf(struct rum_softc *, uint64_t *);
214a19dbd8dSAdrian Chadd static void		rum_update_slot_cb(struct rum_softc *,
215a19dbd8dSAdrian Chadd 			    union sec_param *, uint8_t, uint8_t);
216a19dbd8dSAdrian Chadd static void		rum_update_slot(struct ieee80211com *);
21702ac6454SAndrew Thompson static void		rum_set_bssid(struct rum_softc *, const uint8_t *);
21802ac6454SAndrew Thompson static void		rum_set_macaddr(struct rum_softc *, const uint8_t *);
219272f6adeSGleb Smirnoff static void		rum_update_mcast(struct ieee80211com *);
220272f6adeSGleb Smirnoff static void		rum_update_promisc(struct ieee80211com *);
2215efea30fSAndrew Thompson static void		rum_setpromisc(struct rum_softc *);
22202ac6454SAndrew Thompson static const char	*rum_get_rf(int);
22302ac6454SAndrew Thompson static void		rum_read_eeprom(struct rum_softc *);
2246095f7caSAdrian Chadd static int		rum_bbp_wakeup(struct rum_softc *);
22502ac6454SAndrew Thompson static int		rum_bbp_init(struct rum_softc *);
226*2cb9ef8dSAdrian Chadd static void		rum_clr_shkey_regs(struct rum_softc *);
2270698c0b3SAdrian Chadd static int		rum_init(struct rum_softc *);
2285efea30fSAndrew Thompson static void		rum_stop(struct rum_softc *);
22977ddf3d3SAndrew Thompson static void		rum_load_microcode(struct rum_softc *, const uint8_t *,
23002ac6454SAndrew Thompson 			    size_t);
231b780f864SAdrian Chadd static int		rum_set_beacon(struct rum_softc *,
232b780f864SAdrian Chadd 			    struct ieee80211vap *);
233b780f864SAdrian Chadd static int		rum_alloc_beacon(struct rum_softc *,
23402ac6454SAndrew Thompson 			    struct ieee80211vap *);
2350e3b4c60SAdrian Chadd static void		rum_update_beacon_cb(struct rum_softc *,
2360e3b4c60SAdrian Chadd 			    union sec_param *, uint8_t, uint8_t);
2370e3b4c60SAdrian Chadd static void		rum_update_beacon(struct ieee80211vap *, int);
238*2cb9ef8dSAdrian Chadd static int		rum_common_key_set(struct rum_softc *,
239*2cb9ef8dSAdrian Chadd 			    struct ieee80211_key *, uint16_t);
240*2cb9ef8dSAdrian Chadd static void		rum_group_key_set_cb(struct rum_softc *,
241*2cb9ef8dSAdrian Chadd 			    union sec_param *, uint8_t, uint8_t);
242*2cb9ef8dSAdrian Chadd static void		rum_group_key_del_cb(struct rum_softc *,
243*2cb9ef8dSAdrian Chadd 			    union sec_param *, uint8_t, uint8_t);
244*2cb9ef8dSAdrian Chadd static void		rum_pair_key_set_cb(struct rum_softc *,
245*2cb9ef8dSAdrian Chadd 			    union sec_param *, uint8_t, uint8_t);
246*2cb9ef8dSAdrian Chadd static void		rum_pair_key_del_cb(struct rum_softc *,
247*2cb9ef8dSAdrian Chadd 			    union sec_param *, uint8_t, uint8_t);
248*2cb9ef8dSAdrian Chadd static int		rum_key_alloc(struct ieee80211vap *,
249*2cb9ef8dSAdrian Chadd 			    struct ieee80211_key *, ieee80211_keyix *,
250*2cb9ef8dSAdrian Chadd 			    ieee80211_keyix *);
251*2cb9ef8dSAdrian Chadd static int		rum_key_set(struct ieee80211vap *,
252*2cb9ef8dSAdrian Chadd 			    const struct ieee80211_key *,
253*2cb9ef8dSAdrian Chadd 			    const uint8_t mac[IEEE80211_ADDR_LEN]);
254*2cb9ef8dSAdrian Chadd static int		rum_key_delete(struct ieee80211vap *,
255*2cb9ef8dSAdrian Chadd 			    const struct ieee80211_key *);
25602ac6454SAndrew Thompson static int		rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
25702ac6454SAndrew Thompson 			    const struct ieee80211_bpf_params *);
25802ac6454SAndrew Thompson static void		rum_scan_start(struct ieee80211com *);
25902ac6454SAndrew Thompson static void		rum_scan_end(struct ieee80211com *);
26002ac6454SAndrew Thompson static void		rum_set_channel(struct ieee80211com *);
26102ac6454SAndrew Thompson static int		rum_get_rssi(struct rum_softc *, uint8_t);
262b6108616SRui Paulo static void		rum_ratectl_start(struct rum_softc *,
26302ac6454SAndrew Thompson 			    struct ieee80211_node *);
264b6108616SRui Paulo static void		rum_ratectl_timeout(void *);
265b6108616SRui Paulo static void		rum_ratectl_task(void *, int);
26602ac6454SAndrew Thompson static int		rum_pause(struct rum_softc *, int);
26702ac6454SAndrew Thompson 
26802ac6454SAndrew Thompson static const struct {
26902ac6454SAndrew Thompson 	uint32_t	reg;
27002ac6454SAndrew Thompson 	uint32_t	val;
27102ac6454SAndrew Thompson } rum_def_mac[] = {
27202ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR0,  0x025fb032 },
27302ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR1,  0x9eaa9eaf },
27402ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR2,  0x8a8b8c8d },
27502ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR3,  0x00858687 },
27602ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR7,  0x2e31353b },
27702ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR8,  0x2a2a2a2c },
27802ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR15, 0x0000000f },
27902ac6454SAndrew Thompson 	{ RT2573_MAC_CSR6,   0x00000fff },
28002ac6454SAndrew Thompson 	{ RT2573_MAC_CSR8,   0x016c030a },
28102ac6454SAndrew Thompson 	{ RT2573_MAC_CSR10,  0x00000718 },
28202ac6454SAndrew Thompson 	{ RT2573_MAC_CSR12,  0x00000004 },
28302ac6454SAndrew Thompson 	{ RT2573_MAC_CSR13,  0x00007f00 },
284*2cb9ef8dSAdrian Chadd 	{ RT2573_SEC_CSR2,   0x00000000 },
285*2cb9ef8dSAdrian Chadd 	{ RT2573_SEC_CSR3,   0x00000000 },
286*2cb9ef8dSAdrian Chadd 	{ RT2573_SEC_CSR4,   0x00000000 },
28702ac6454SAndrew Thompson 	{ RT2573_PHY_CSR1,   0x000023b0 },
28802ac6454SAndrew Thompson 	{ RT2573_PHY_CSR5,   0x00040a06 },
28902ac6454SAndrew Thompson 	{ RT2573_PHY_CSR6,   0x00080606 },
29002ac6454SAndrew Thompson 	{ RT2573_PHY_CSR7,   0x00000408 },
29102ac6454SAndrew Thompson 	{ RT2573_AIFSN_CSR,  0x00002273 },
29202ac6454SAndrew Thompson 	{ RT2573_CWMIN_CSR,  0x00002344 },
29302ac6454SAndrew Thompson 	{ RT2573_CWMAX_CSR,  0x000034aa }
29402ac6454SAndrew Thompson };
29502ac6454SAndrew Thompson 
29602ac6454SAndrew Thompson static const struct {
29702ac6454SAndrew Thompson 	uint8_t	reg;
29802ac6454SAndrew Thompson 	uint8_t	val;
29902ac6454SAndrew Thompson } rum_def_bbp[] = {
30002ac6454SAndrew Thompson 	{   3, 0x80 },
30102ac6454SAndrew Thompson 	{  15, 0x30 },
30202ac6454SAndrew Thompson 	{  17, 0x20 },
30302ac6454SAndrew Thompson 	{  21, 0xc8 },
30402ac6454SAndrew Thompson 	{  22, 0x38 },
30502ac6454SAndrew Thompson 	{  23, 0x06 },
30602ac6454SAndrew Thompson 	{  24, 0xfe },
30702ac6454SAndrew Thompson 	{  25, 0x0a },
30802ac6454SAndrew Thompson 	{  26, 0x0d },
30902ac6454SAndrew Thompson 	{  32, 0x0b },
31002ac6454SAndrew Thompson 	{  34, 0x12 },
31102ac6454SAndrew Thompson 	{  37, 0x07 },
31202ac6454SAndrew Thompson 	{  39, 0xf8 },
31302ac6454SAndrew Thompson 	{  41, 0x60 },
31402ac6454SAndrew Thompson 	{  53, 0x10 },
31502ac6454SAndrew Thompson 	{  54, 0x18 },
31602ac6454SAndrew Thompson 	{  60, 0x10 },
31702ac6454SAndrew Thompson 	{  61, 0x04 },
31802ac6454SAndrew Thompson 	{  62, 0x04 },
31902ac6454SAndrew Thompson 	{  75, 0xfe },
32002ac6454SAndrew Thompson 	{  86, 0xfe },
32102ac6454SAndrew Thompson 	{  88, 0xfe },
32202ac6454SAndrew Thompson 	{  90, 0x0f },
32302ac6454SAndrew Thompson 	{  99, 0x00 },
32402ac6454SAndrew Thompson 	{ 102, 0x16 },
32502ac6454SAndrew Thompson 	{ 107, 0x04 }
32602ac6454SAndrew Thompson };
32702ac6454SAndrew Thompson 
32802ac6454SAndrew Thompson static const struct rfprog {
32902ac6454SAndrew Thompson 	uint8_t		chan;
33002ac6454SAndrew Thompson 	uint32_t	r1, r2, r3, r4;
33102ac6454SAndrew Thompson }  rum_rf5226[] = {
33202ac6454SAndrew Thompson 	{   1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
33302ac6454SAndrew Thompson 	{   2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
33402ac6454SAndrew Thompson 	{   3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
33502ac6454SAndrew Thompson 	{   4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
33602ac6454SAndrew Thompson 	{   5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
33702ac6454SAndrew Thompson 	{   6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
33802ac6454SAndrew Thompson 	{   7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
33902ac6454SAndrew Thompson 	{   8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
34002ac6454SAndrew Thompson 	{   9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
34102ac6454SAndrew Thompson 	{  10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
34202ac6454SAndrew Thompson 	{  11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
34302ac6454SAndrew Thompson 	{  12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
34402ac6454SAndrew Thompson 	{  13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
34502ac6454SAndrew Thompson 	{  14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
34602ac6454SAndrew Thompson 
34702ac6454SAndrew Thompson 	{  34, 0x00b03, 0x20266, 0x36014, 0x30282 },
34802ac6454SAndrew Thompson 	{  38, 0x00b03, 0x20267, 0x36014, 0x30284 },
34902ac6454SAndrew Thompson 	{  42, 0x00b03, 0x20268, 0x36014, 0x30286 },
35002ac6454SAndrew Thompson 	{  46, 0x00b03, 0x20269, 0x36014, 0x30288 },
35102ac6454SAndrew Thompson 
35202ac6454SAndrew Thompson 	{  36, 0x00b03, 0x00266, 0x26014, 0x30288 },
35302ac6454SAndrew Thompson 	{  40, 0x00b03, 0x00268, 0x26014, 0x30280 },
35402ac6454SAndrew Thompson 	{  44, 0x00b03, 0x00269, 0x26014, 0x30282 },
35502ac6454SAndrew Thompson 	{  48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
35602ac6454SAndrew Thompson 	{  52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
35702ac6454SAndrew Thompson 	{  56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
35802ac6454SAndrew Thompson 	{  60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
35902ac6454SAndrew Thompson 	{  64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
36002ac6454SAndrew Thompson 
36102ac6454SAndrew Thompson 	{ 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
36202ac6454SAndrew Thompson 	{ 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
36302ac6454SAndrew Thompson 	{ 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
36402ac6454SAndrew Thompson 	{ 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
36502ac6454SAndrew Thompson 	{ 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
36602ac6454SAndrew Thompson 	{ 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
36702ac6454SAndrew Thompson 	{ 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
36802ac6454SAndrew Thompson 	{ 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
36902ac6454SAndrew Thompson 	{ 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
37002ac6454SAndrew Thompson 	{ 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
37102ac6454SAndrew Thompson 	{ 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
37202ac6454SAndrew Thompson 
37302ac6454SAndrew Thompson 	{ 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
37402ac6454SAndrew Thompson 	{ 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
37502ac6454SAndrew Thompson 	{ 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
37602ac6454SAndrew Thompson 	{ 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
37702ac6454SAndrew Thompson 	{ 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
37802ac6454SAndrew Thompson }, rum_rf5225[] = {
37902ac6454SAndrew Thompson 	{   1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
38002ac6454SAndrew Thompson 	{   2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
38102ac6454SAndrew Thompson 	{   3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
38202ac6454SAndrew Thompson 	{   4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
38302ac6454SAndrew Thompson 	{   5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
38402ac6454SAndrew Thompson 	{   6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
38502ac6454SAndrew Thompson 	{   7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
38602ac6454SAndrew Thompson 	{   8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
38702ac6454SAndrew Thompson 	{   9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
38802ac6454SAndrew Thompson 	{  10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
38902ac6454SAndrew Thompson 	{  11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
39002ac6454SAndrew Thompson 	{  12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
39102ac6454SAndrew Thompson 	{  13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
39202ac6454SAndrew Thompson 	{  14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
39302ac6454SAndrew Thompson 
39402ac6454SAndrew Thompson 	{  34, 0x00b33, 0x01266, 0x26014, 0x30282 },
39502ac6454SAndrew Thompson 	{  38, 0x00b33, 0x01267, 0x26014, 0x30284 },
39602ac6454SAndrew Thompson 	{  42, 0x00b33, 0x01268, 0x26014, 0x30286 },
39702ac6454SAndrew Thompson 	{  46, 0x00b33, 0x01269, 0x26014, 0x30288 },
39802ac6454SAndrew Thompson 
39902ac6454SAndrew Thompson 	{  36, 0x00b33, 0x01266, 0x26014, 0x30288 },
40002ac6454SAndrew Thompson 	{  40, 0x00b33, 0x01268, 0x26014, 0x30280 },
40102ac6454SAndrew Thompson 	{  44, 0x00b33, 0x01269, 0x26014, 0x30282 },
40202ac6454SAndrew Thompson 	{  48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
40302ac6454SAndrew Thompson 	{  52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
40402ac6454SAndrew Thompson 	{  56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
40502ac6454SAndrew Thompson 	{  60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
40602ac6454SAndrew Thompson 	{  64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
40702ac6454SAndrew Thompson 
40802ac6454SAndrew Thompson 	{ 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
40902ac6454SAndrew Thompson 	{ 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
41002ac6454SAndrew Thompson 	{ 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
41102ac6454SAndrew Thompson 	{ 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
41202ac6454SAndrew Thompson 	{ 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
41302ac6454SAndrew Thompson 	{ 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
41402ac6454SAndrew Thompson 	{ 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
41502ac6454SAndrew Thompson 	{ 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
41602ac6454SAndrew Thompson 	{ 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
41702ac6454SAndrew Thompson 	{ 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
41802ac6454SAndrew Thompson 	{ 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
41902ac6454SAndrew Thompson 
42002ac6454SAndrew Thompson 	{ 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
42102ac6454SAndrew Thompson 	{ 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
42202ac6454SAndrew Thompson 	{ 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
42302ac6454SAndrew Thompson 	{ 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
42402ac6454SAndrew Thompson 	{ 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
42502ac6454SAndrew Thompson };
42602ac6454SAndrew Thompson 
427760bc48eSAndrew Thompson static const struct usb_config rum_config[RUM_N_TRANSFER] = {
42802ac6454SAndrew Thompson 	[RUM_BULK_WR] = {
42902ac6454SAndrew Thompson 		.type = UE_BULK,
43002ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
43102ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
4324eae601eSAndrew Thompson 		.bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
4334eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
4344eae601eSAndrew Thompson 		.callback = rum_bulk_write_callback,
4354eae601eSAndrew Thompson 		.timeout = 5000,	/* ms */
43602ac6454SAndrew Thompson 	},
43702ac6454SAndrew Thompson 	[RUM_BULK_RD] = {
43802ac6454SAndrew Thompson 		.type = UE_BULK,
43902ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
44002ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
4414eae601eSAndrew Thompson 		.bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
4424eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
4434eae601eSAndrew Thompson 		.callback = rum_bulk_read_callback,
44402ac6454SAndrew Thompson 	},
44502ac6454SAndrew Thompson };
44602ac6454SAndrew Thompson 
44702ac6454SAndrew Thompson static int
44802ac6454SAndrew Thompson rum_match(device_t self)
44902ac6454SAndrew Thompson {
450760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(self);
45102ac6454SAndrew Thompson 
452f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST)
45302ac6454SAndrew Thompson 		return (ENXIO);
45402ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != 0)
45502ac6454SAndrew Thompson 		return (ENXIO);
45602ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
45702ac6454SAndrew Thompson 		return (ENXIO);
45802ac6454SAndrew Thompson 
459a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
46002ac6454SAndrew Thompson }
46102ac6454SAndrew Thompson 
46202ac6454SAndrew Thompson static int
46302ac6454SAndrew Thompson rum_attach(device_t self)
46402ac6454SAndrew Thompson {
465760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(self);
46602ac6454SAndrew Thompson 	struct rum_softc *sc = device_get_softc(self);
4677a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
4685efea30fSAndrew Thompson 	uint8_t iface_index, bands;
4695efea30fSAndrew Thompson 	uint32_t tmp;
4705efea30fSAndrew Thompson 	int error, ntries;
47102ac6454SAndrew Thompson 
472a593f6b8SAndrew Thompson 	device_set_usb_desc(self);
47302ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
47402ac6454SAndrew Thompson 	sc->sc_dev = self;
47502ac6454SAndrew Thompson 
476f28c2f5eSAdrian Chadd 	RUM_LOCK_INIT(sc);
477f28c2f5eSAdrian Chadd 	RUM_CMDQ_LOCK_INIT(sc);
4787a79cebfSGleb Smirnoff 	mbufq_init(&sc->sc_snd, ifqmaxlen);
47902ac6454SAndrew Thompson 
48002ac6454SAndrew Thompson 	iface_index = RT2573_IFACE_INDEX;
481a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device, &iface_index,
48202ac6454SAndrew Thompson 	    sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
48302ac6454SAndrew Thompson 	if (error) {
48402ac6454SAndrew Thompson 		device_printf(self, "could not allocate USB transfers, "
485a593f6b8SAndrew Thompson 		    "err=%s\n", usbd_errstr(error));
48602ac6454SAndrew Thompson 		goto detach;
48702ac6454SAndrew Thompson 	}
48802ac6454SAndrew Thompson 
48902ac6454SAndrew Thompson 	RUM_LOCK(sc);
49002ac6454SAndrew Thompson 	/* retrieve RT2573 rev. no */
49102ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
49202ac6454SAndrew Thompson 		if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
49302ac6454SAndrew Thompson 			break;
49402ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
49502ac6454SAndrew Thompson 			break;
49602ac6454SAndrew Thompson 	}
49702ac6454SAndrew Thompson 	if (ntries == 100) {
49802ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
4995efea30fSAndrew Thompson 		RUM_UNLOCK(sc);
5005efea30fSAndrew Thompson 		goto detach;
50102ac6454SAndrew Thompson 	}
50202ac6454SAndrew Thompson 
50302ac6454SAndrew Thompson 	/* retrieve MAC address and various other things from EEPROM */
50402ac6454SAndrew Thompson 	rum_read_eeprom(sc);
50502ac6454SAndrew Thompson 
50602ac6454SAndrew Thompson 	device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
50702ac6454SAndrew Thompson 	    tmp, rum_get_rf(sc->rf_rev));
50802ac6454SAndrew Thompson 
50977ddf3d3SAndrew Thompson 	rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
51002ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
51102ac6454SAndrew Thompson 
51259686fe9SGleb Smirnoff 	ic->ic_softc = sc;
513c8550c02SGleb Smirnoff 	ic->ic_name = device_get_nameunit(self);
51402ac6454SAndrew Thompson 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
51502ac6454SAndrew Thompson 
51602ac6454SAndrew Thompson 	/* set device capabilities */
51702ac6454SAndrew Thompson 	ic->ic_caps =
51802ac6454SAndrew Thompson 	      IEEE80211_C_STA		/* station mode supported */
51902ac6454SAndrew Thompson 	    | IEEE80211_C_IBSS		/* IBSS mode supported */
52002ac6454SAndrew Thompson 	    | IEEE80211_C_MONITOR	/* monitor mode supported */
52102ac6454SAndrew Thompson 	    | IEEE80211_C_HOSTAP	/* HostAp mode supported */
52250a31b48SAdrian Chadd 	    | IEEE80211_C_AHDEMO	/* adhoc demo mode */
52302ac6454SAndrew Thompson 	    | IEEE80211_C_TXPMGT	/* tx power management */
52402ac6454SAndrew Thompson 	    | IEEE80211_C_SHPREAMBLE	/* short preamble supported */
52502ac6454SAndrew Thompson 	    | IEEE80211_C_SHSLOT	/* short slot time supported */
52602ac6454SAndrew Thompson 	    | IEEE80211_C_BGSCAN	/* bg scanning supported */
52702ac6454SAndrew Thompson 	    | IEEE80211_C_WPA		/* 802.11i */
52802ac6454SAndrew Thompson 	    ;
52902ac6454SAndrew Thompson 
530*2cb9ef8dSAdrian Chadd 	ic->ic_cryptocaps =
531*2cb9ef8dSAdrian Chadd 	    IEEE80211_CRYPTO_WEP |
532*2cb9ef8dSAdrian Chadd 	    IEEE80211_CRYPTO_AES_CCM |
533*2cb9ef8dSAdrian Chadd 	    IEEE80211_CRYPTO_TKIPMIC |
534*2cb9ef8dSAdrian Chadd 	    IEEE80211_CRYPTO_TKIP;
535*2cb9ef8dSAdrian Chadd 
53602ac6454SAndrew Thompson 	bands = 0;
53702ac6454SAndrew Thompson 	setbit(&bands, IEEE80211_MODE_11B);
53802ac6454SAndrew Thompson 	setbit(&bands, IEEE80211_MODE_11G);
53902ac6454SAndrew Thompson 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226)
54002ac6454SAndrew Thompson 		setbit(&bands, IEEE80211_MODE_11A);
54102ac6454SAndrew Thompson 	ieee80211_init_channels(ic, NULL, &bands);
54202ac6454SAndrew Thompson 
5437a79cebfSGleb Smirnoff 	ieee80211_ifattach(ic);
54477ddf3d3SAndrew Thompson 	ic->ic_update_promisc = rum_update_promisc;
54502ac6454SAndrew Thompson 	ic->ic_raw_xmit = rum_raw_xmit;
54602ac6454SAndrew Thompson 	ic->ic_scan_start = rum_scan_start;
54702ac6454SAndrew Thompson 	ic->ic_scan_end = rum_scan_end;
54802ac6454SAndrew Thompson 	ic->ic_set_channel = rum_set_channel;
5497a79cebfSGleb Smirnoff 	ic->ic_transmit = rum_transmit;
5507a79cebfSGleb Smirnoff 	ic->ic_parent = rum_parent;
55102ac6454SAndrew Thompson 	ic->ic_vap_create = rum_vap_create;
55202ac6454SAndrew Thompson 	ic->ic_vap_delete = rum_vap_delete;
553a19dbd8dSAdrian Chadd 	ic->ic_updateslot = rum_update_slot;
5546dd8e071SEd Maste 	ic->ic_update_mcast = rum_update_mcast;
55502ac6454SAndrew Thompson 
5565463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
5575463c4a4SSam Leffler 	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
5585463c4a4SSam Leffler 		RT2573_TX_RADIOTAP_PRESENT,
5595463c4a4SSam Leffler 	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
5605463c4a4SSam Leffler 		RT2573_RX_RADIOTAP_PRESENT);
56102ac6454SAndrew Thompson 
562f28c2f5eSAdrian Chadd 	TASK_INIT(&sc->cmdq_task, 0, rum_cmdq_cb, sc);
563f28c2f5eSAdrian Chadd 
56402ac6454SAndrew Thompson 	if (bootverbose)
56502ac6454SAndrew Thompson 		ieee80211_announce(ic);
56602ac6454SAndrew Thompson 
5675efea30fSAndrew Thompson 	return (0);
5685efea30fSAndrew Thompson 
5695efea30fSAndrew Thompson detach:
5705efea30fSAndrew Thompson 	rum_detach(self);
5715efea30fSAndrew Thompson 	return (ENXIO);			/* failure */
57202ac6454SAndrew Thompson }
57302ac6454SAndrew Thompson 
57402ac6454SAndrew Thompson static int
57502ac6454SAndrew Thompson rum_detach(device_t self)
57602ac6454SAndrew Thompson {
57702ac6454SAndrew Thompson 	struct rum_softc *sc = device_get_softc(self);
578e7b7db3dSAdrian Chadd 	struct ieee80211com *ic = &sc->sc_ic;
57902ac6454SAndrew Thompson 
580645e4d17SHans Petter Selasky 	/* Prevent further ioctls */
581645e4d17SHans Petter Selasky 	RUM_LOCK(sc);
582645e4d17SHans Petter Selasky 	sc->sc_detached = 1;
583645e4d17SHans Petter Selasky 	RUM_UNLOCK(sc);
584645e4d17SHans Petter Selasky 
58502ac6454SAndrew Thompson 	/* stop all USB transfers */
586a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
58702ac6454SAndrew Thompson 
58802ac6454SAndrew Thompson 	/* free TX list, if any */
58902ac6454SAndrew Thompson 	RUM_LOCK(sc);
59002ac6454SAndrew Thompson 	rum_unsetup_tx_list(sc);
59102ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
59202ac6454SAndrew Thompson 
593f28c2f5eSAdrian Chadd 	if (ic->ic_softc == sc) {
594f28c2f5eSAdrian Chadd 		ieee80211_draintask(ic, &sc->cmdq_task);
595e7b7db3dSAdrian Chadd 		ieee80211_ifdetach(ic);
596f28c2f5eSAdrian Chadd 	}
597f28c2f5eSAdrian Chadd 
5987a79cebfSGleb Smirnoff 	mbufq_drain(&sc->sc_snd);
599f28c2f5eSAdrian Chadd 	RUM_CMDQ_LOCK_DESTROY(sc);
600f28c2f5eSAdrian Chadd 	RUM_LOCK_DESTROY(sc);
601f28c2f5eSAdrian Chadd 
60202ac6454SAndrew Thompson 	return (0);
60302ac6454SAndrew Thompson }
60402ac6454SAndrew Thompson 
605e0a69b51SAndrew Thompson static usb_error_t
60677ddf3d3SAndrew Thompson rum_do_request(struct rum_softc *sc,
607760bc48eSAndrew Thompson     struct usb_device_request *req, void *data)
60877ddf3d3SAndrew Thompson {
609e0a69b51SAndrew Thompson 	usb_error_t err;
61077ddf3d3SAndrew Thompson 	int ntries = 10;
61177ddf3d3SAndrew Thompson 
61277ddf3d3SAndrew Thompson 	while (ntries--) {
613a593f6b8SAndrew Thompson 		err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
61477ddf3d3SAndrew Thompson 		    req, data, 0, NULL, 250 /* ms */);
61577ddf3d3SAndrew Thompson 		if (err == 0)
61677ddf3d3SAndrew Thompson 			break;
61777ddf3d3SAndrew Thompson 
61877ddf3d3SAndrew Thompson 		DPRINTFN(1, "Control request failed, %s (retrying)\n",
619a593f6b8SAndrew Thompson 		    usbd_errstr(err));
62077ddf3d3SAndrew Thompson 		if (rum_pause(sc, hz / 100))
62177ddf3d3SAndrew Thompson 			break;
62277ddf3d3SAndrew Thompson 	}
62377ddf3d3SAndrew Thompson 	return (err);
62477ddf3d3SAndrew Thompson }
62577ddf3d3SAndrew Thompson 
62602ac6454SAndrew Thompson static struct ieee80211vap *
627fcd9500fSBernhard Schmidt rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
628fcd9500fSBernhard Schmidt     enum ieee80211_opmode opmode, int flags,
62902ac6454SAndrew Thompson     const uint8_t bssid[IEEE80211_ADDR_LEN],
63002ac6454SAndrew Thompson     const uint8_t mac[IEEE80211_ADDR_LEN])
63102ac6454SAndrew Thompson {
632d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
63302ac6454SAndrew Thompson 	struct rum_vap *rvp;
63402ac6454SAndrew Thompson 	struct ieee80211vap *vap;
63502ac6454SAndrew Thompson 
63602ac6454SAndrew Thompson 	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
63702ac6454SAndrew Thompson 		return NULL;
6387a79cebfSGleb Smirnoff 	rvp = malloc(sizeof(struct rum_vap), M_80211_VAP, M_WAITOK | M_ZERO);
63902ac6454SAndrew Thompson 	vap = &rvp->vap;
64002ac6454SAndrew Thompson 	/* enable s/w bmiss handling for sta mode */
641bb2f69e8SHans Petter Selasky 
642bb2f69e8SHans Petter Selasky 	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
6437a79cebfSGleb Smirnoff 	    flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
644bb2f69e8SHans Petter Selasky 		/* out of memory */
645bb2f69e8SHans Petter Selasky 		free(rvp, M_80211_VAP);
646bb2f69e8SHans Petter Selasky 		return (NULL);
647bb2f69e8SHans Petter Selasky 	}
64802ac6454SAndrew Thompson 
64902ac6454SAndrew Thompson 	/* override state transition machine */
65002ac6454SAndrew Thompson 	rvp->newstate = vap->iv_newstate;
65102ac6454SAndrew Thompson 	vap->iv_newstate = rum_newstate;
652*2cb9ef8dSAdrian Chadd 	vap->iv_key_alloc = rum_key_alloc;
653*2cb9ef8dSAdrian Chadd 	vap->iv_key_set = rum_key_set;
654*2cb9ef8dSAdrian Chadd 	vap->iv_key_delete = rum_key_delete;
6550e3b4c60SAdrian Chadd 	vap->iv_update_beacon = rum_update_beacon;
656*2cb9ef8dSAdrian Chadd 	vap->iv_max_aid = RT2573_ADDR_MAX;
65702ac6454SAndrew Thompson 
658b6108616SRui Paulo 	usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0);
659b6108616SRui Paulo 	TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
660b6108616SRui Paulo 	ieee80211_ratectl_init(vap);
661b6108616SRui Paulo 	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
66202ac6454SAndrew Thompson 	/* complete setup */
6637a79cebfSGleb Smirnoff 	ieee80211_vap_attach(vap, ieee80211_media_change,
6647a79cebfSGleb Smirnoff 	    ieee80211_media_status, mac);
66502ac6454SAndrew Thompson 	ic->ic_opmode = opmode;
66602ac6454SAndrew Thompson 	return vap;
66702ac6454SAndrew Thompson }
66802ac6454SAndrew Thompson 
66902ac6454SAndrew Thompson static void
67002ac6454SAndrew Thompson rum_vap_delete(struct ieee80211vap *vap)
67102ac6454SAndrew Thompson {
67202ac6454SAndrew Thompson 	struct rum_vap *rvp = RUM_VAP(vap);
6735efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
67402ac6454SAndrew Thompson 
675b780f864SAdrian Chadd 	m_freem(rvp->bcn_mbuf);
676b6108616SRui Paulo 	usb_callout_drain(&rvp->ratectl_ch);
677b6108616SRui Paulo 	ieee80211_draintask(ic, &rvp->ratectl_task);
678b6108616SRui Paulo 	ieee80211_ratectl_deinit(vap);
67902ac6454SAndrew Thompson 	ieee80211_vap_detach(vap);
68002ac6454SAndrew Thompson 	free(rvp, M_80211_VAP);
68102ac6454SAndrew Thompson }
68202ac6454SAndrew Thompson 
68302ac6454SAndrew Thompson static void
684f28c2f5eSAdrian Chadd rum_cmdq_cb(void *arg, int pending)
685f28c2f5eSAdrian Chadd {
686f28c2f5eSAdrian Chadd 	struct rum_softc *sc = arg;
687f28c2f5eSAdrian Chadd 	struct rum_cmdq *rc;
688f28c2f5eSAdrian Chadd 
689f28c2f5eSAdrian Chadd 	RUM_CMDQ_LOCK(sc);
690f28c2f5eSAdrian Chadd 	while (sc->cmdq[sc->cmdq_first].func != NULL) {
691f28c2f5eSAdrian Chadd 		rc = &sc->cmdq[sc->cmdq_first];
692f28c2f5eSAdrian Chadd 		RUM_CMDQ_UNLOCK(sc);
693f28c2f5eSAdrian Chadd 
694f28c2f5eSAdrian Chadd 		RUM_LOCK(sc);
695f28c2f5eSAdrian Chadd 		rc->func(sc, &rc->data, rc->rn_id, rc->rvp_id);
696f28c2f5eSAdrian Chadd 		RUM_UNLOCK(sc);
697f28c2f5eSAdrian Chadd 
698f28c2f5eSAdrian Chadd 		RUM_CMDQ_LOCK(sc);
699f28c2f5eSAdrian Chadd 		memset(rc, 0, sizeof (*rc));
700f28c2f5eSAdrian Chadd 		sc->cmdq_first = (sc->cmdq_first + 1) % RUM_CMDQ_SIZE;
701f28c2f5eSAdrian Chadd 	}
702f28c2f5eSAdrian Chadd 	RUM_CMDQ_UNLOCK(sc);
703f28c2f5eSAdrian Chadd }
704f28c2f5eSAdrian Chadd 
705f28c2f5eSAdrian Chadd static int
706f28c2f5eSAdrian Chadd rum_cmd_sleepable(struct rum_softc *sc, const void *ptr, size_t len,
707f28c2f5eSAdrian Chadd     uint8_t rn_id, uint8_t rvp_id, CMD_FUNC_PROTO)
708f28c2f5eSAdrian Chadd {
709f28c2f5eSAdrian Chadd 	struct ieee80211com *ic = &sc->sc_ic;
710f28c2f5eSAdrian Chadd 
711f28c2f5eSAdrian Chadd 	KASSERT(len <= sizeof(union sec_param), ("buffer overflow"));
712f28c2f5eSAdrian Chadd 
713f28c2f5eSAdrian Chadd 	RUM_CMDQ_LOCK(sc);
714f28c2f5eSAdrian Chadd 	if (sc->cmdq[sc->cmdq_last].func != NULL) {
715f28c2f5eSAdrian Chadd 		device_printf(sc->sc_dev, "%s: cmdq overflow\n", __func__);
716f28c2f5eSAdrian Chadd 		RUM_CMDQ_UNLOCK(sc);
717f28c2f5eSAdrian Chadd 
718f28c2f5eSAdrian Chadd 		return EAGAIN;
719f28c2f5eSAdrian Chadd 	}
720f28c2f5eSAdrian Chadd 
721f28c2f5eSAdrian Chadd 	if (ptr != NULL)
722f28c2f5eSAdrian Chadd 		memcpy(&sc->cmdq[sc->cmdq_last].data, ptr, len);
723f28c2f5eSAdrian Chadd 	sc->cmdq[sc->cmdq_last].rn_id = rn_id;
724f28c2f5eSAdrian Chadd 	sc->cmdq[sc->cmdq_last].rvp_id = rvp_id;
725f28c2f5eSAdrian Chadd 	sc->cmdq[sc->cmdq_last].func = func;
726f28c2f5eSAdrian Chadd 	sc->cmdq_last = (sc->cmdq_last + 1) % RUM_CMDQ_SIZE;
727f28c2f5eSAdrian Chadd 	RUM_CMDQ_UNLOCK(sc);
728f28c2f5eSAdrian Chadd 
729f28c2f5eSAdrian Chadd 	ieee80211_runtask(ic, &sc->cmdq_task);
730f28c2f5eSAdrian Chadd 
731f28c2f5eSAdrian Chadd 	return 0;
732f28c2f5eSAdrian Chadd }
733f28c2f5eSAdrian Chadd 
734f28c2f5eSAdrian Chadd static void
73502ac6454SAndrew Thompson rum_tx_free(struct rum_tx_data *data, int txerr)
73602ac6454SAndrew Thompson {
73702ac6454SAndrew Thompson 	struct rum_softc *sc = data->sc;
73802ac6454SAndrew Thompson 
73902ac6454SAndrew Thompson 	if (data->m != NULL) {
7407a79cebfSGleb Smirnoff 		ieee80211_tx_complete(data->ni, data->m, txerr);
74102ac6454SAndrew Thompson 		data->m = NULL;
74202ac6454SAndrew Thompson 		data->ni = NULL;
74302ac6454SAndrew Thompson 	}
74402ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
74502ac6454SAndrew Thompson 	sc->tx_nfree++;
74602ac6454SAndrew Thompson }
74702ac6454SAndrew Thompson 
74802ac6454SAndrew Thompson static void
74902ac6454SAndrew Thompson rum_setup_tx_list(struct rum_softc *sc)
75002ac6454SAndrew Thompson {
75102ac6454SAndrew Thompson 	struct rum_tx_data *data;
75202ac6454SAndrew Thompson 	int i;
75302ac6454SAndrew Thompson 
75402ac6454SAndrew Thompson 	sc->tx_nfree = 0;
75502ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_q);
75602ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_free);
75702ac6454SAndrew Thompson 
75802ac6454SAndrew Thompson 	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
75902ac6454SAndrew Thompson 		data = &sc->tx_data[i];
76002ac6454SAndrew Thompson 
76102ac6454SAndrew Thompson 		data->sc = sc;
76202ac6454SAndrew Thompson 		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
76302ac6454SAndrew Thompson 		sc->tx_nfree++;
76402ac6454SAndrew Thompson 	}
76502ac6454SAndrew Thompson }
76602ac6454SAndrew Thompson 
76702ac6454SAndrew Thompson static void
76802ac6454SAndrew Thompson rum_unsetup_tx_list(struct rum_softc *sc)
76902ac6454SAndrew Thompson {
77002ac6454SAndrew Thompson 	struct rum_tx_data *data;
77102ac6454SAndrew Thompson 	int i;
77202ac6454SAndrew Thompson 
77302ac6454SAndrew Thompson 	/* make sure any subsequent use of the queues will fail */
77402ac6454SAndrew Thompson 	sc->tx_nfree = 0;
77502ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_q);
77602ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_free);
77702ac6454SAndrew Thompson 
77802ac6454SAndrew Thompson 	/* free up all node references and mbufs */
77902ac6454SAndrew Thompson 	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
78002ac6454SAndrew Thompson 		data = &sc->tx_data[i];
78102ac6454SAndrew Thompson 
78202ac6454SAndrew Thompson 		if (data->m != NULL) {
78302ac6454SAndrew Thompson 			m_freem(data->m);
78402ac6454SAndrew Thompson 			data->m = NULL;
78502ac6454SAndrew Thompson 		}
78602ac6454SAndrew Thompson 		if (data->ni != NULL) {
78702ac6454SAndrew Thompson 			ieee80211_free_node(data->ni);
78802ac6454SAndrew Thompson 			data->ni = NULL;
78902ac6454SAndrew Thompson 		}
79002ac6454SAndrew Thompson 	}
79102ac6454SAndrew Thompson }
79202ac6454SAndrew Thompson 
7935efea30fSAndrew Thompson static int
7945efea30fSAndrew Thompson rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
79502ac6454SAndrew Thompson {
79602ac6454SAndrew Thompson 	struct rum_vap *rvp = RUM_VAP(vap);
7975efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
798d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
79902ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
80002ac6454SAndrew Thompson 	enum ieee80211_state ostate;
80102ac6454SAndrew Thompson 	struct ieee80211_node *ni;
802aa81f853SAdrian Chadd 	int ret;
80302ac6454SAndrew Thompson 
80402ac6454SAndrew Thompson 	ostate = vap->iv_state;
8055efea30fSAndrew Thompson 	DPRINTF("%s -> %s\n",
8065efea30fSAndrew Thompson 		ieee80211_state_name[ostate],
8075efea30fSAndrew Thompson 		ieee80211_state_name[nstate]);
80802ac6454SAndrew Thompson 
8095efea30fSAndrew Thompson 	IEEE80211_UNLOCK(ic);
8105efea30fSAndrew Thompson 	RUM_LOCK(sc);
811b6108616SRui Paulo 	usb_callout_stop(&rvp->ratectl_ch);
8125efea30fSAndrew Thompson 
8135efea30fSAndrew Thompson 	switch (nstate) {
81402ac6454SAndrew Thompson 	case IEEE80211_S_INIT:
8156d804321SAdrian Chadd 		if (ostate == IEEE80211_S_RUN)
8166d804321SAdrian Chadd 			rum_abort_tsf_sync(sc);
8176d804321SAdrian Chadd 
81802ac6454SAndrew Thompson 		break;
81902ac6454SAndrew Thompson 
82002ac6454SAndrew Thompson 	case IEEE80211_S_RUN:
8218d8bdb01SAndrew Thompson 		ni = ieee80211_ref_node(vap->iv_bss);
82202ac6454SAndrew Thompson 
82302ac6454SAndrew Thompson 		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
824bb571462SHans Petter Selasky 			if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) {
825aa81f853SAdrian Chadd 				ret = EINVAL;
826aa81f853SAdrian Chadd 				goto run_fail;
827bb571462SHans Petter Selasky 			}
828a19dbd8dSAdrian Chadd 			rum_update_slot_cb(sc, NULL, 0, 0);
82902ac6454SAndrew Thompson 			rum_enable_mrr(sc);
83002ac6454SAndrew Thompson 			rum_set_txpreamble(sc);
83102ac6454SAndrew Thompson 			rum_set_basicrates(sc);
8321ba67b11SAdrian Chadd 			IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
8331ba67b11SAdrian Chadd 			rum_set_bssid(sc, sc->sc_bssid);
83402ac6454SAndrew Thompson 		}
83502ac6454SAndrew Thompson 
83602ac6454SAndrew Thompson 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
837aa81f853SAdrian Chadd 		    vap->iv_opmode == IEEE80211_M_IBSS) {
838b780f864SAdrian Chadd 			if ((ret = rum_alloc_beacon(sc, vap)) != 0)
839aa81f853SAdrian Chadd 				goto run_fail;
840aa81f853SAdrian Chadd 		}
84102ac6454SAndrew Thompson 
84250a31b48SAdrian Chadd 		if (vap->iv_opmode != IEEE80211_M_MONITOR &&
84350a31b48SAdrian Chadd 		    vap->iv_opmode != IEEE80211_M_AHDEMO) {
844aa81f853SAdrian Chadd 			if ((ret = rum_enable_tsf_sync(sc)) != 0)
845aa81f853SAdrian Chadd 				goto run_fail;
846aa81f853SAdrian Chadd 		} else
8475463c4a4SSam Leffler 			rum_enable_tsf(sc);
84802ac6454SAndrew Thompson 
84902ac6454SAndrew Thompson 		/* enable automatic rate adaptation */
8505463c4a4SSam Leffler 		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
85102ac6454SAndrew Thompson 		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
852b6108616SRui Paulo 			rum_ratectl_start(sc, ni);
8538d8bdb01SAndrew Thompson 		ieee80211_free_node(ni);
85402ac6454SAndrew Thompson 		break;
85502ac6454SAndrew Thompson 	default:
85602ac6454SAndrew Thompson 		break;
85702ac6454SAndrew Thompson 	}
85802ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
85902ac6454SAndrew Thompson 	IEEE80211_LOCK(ic);
8605efea30fSAndrew Thompson 	return (rvp->newstate(vap, nstate, arg));
861aa81f853SAdrian Chadd 
862aa81f853SAdrian Chadd run_fail:
863aa81f853SAdrian Chadd 	RUM_UNLOCK(sc);
864aa81f853SAdrian Chadd 	IEEE80211_LOCK(ic);
865aa81f853SAdrian Chadd 	ieee80211_free_node(ni);
866aa81f853SAdrian Chadd 	return ret;
86702ac6454SAndrew Thompson }
86802ac6454SAndrew Thompson 
86902ac6454SAndrew Thompson static void
870ed6d949aSAndrew Thompson rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
87102ac6454SAndrew Thompson {
872ed6d949aSAndrew Thompson 	struct rum_softc *sc = usbd_xfer_softc(xfer);
8735463c4a4SSam Leffler 	struct ieee80211vap *vap;
87402ac6454SAndrew Thompson 	struct rum_tx_data *data;
87502ac6454SAndrew Thompson 	struct mbuf *m;
876ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
87702ac6454SAndrew Thompson 	unsigned int len;
878ed6d949aSAndrew Thompson 	int actlen, sumlen;
879ed6d949aSAndrew Thompson 
880ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
88102ac6454SAndrew Thompson 
88202ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
88302ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
884ed6d949aSAndrew Thompson 		DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
88502ac6454SAndrew Thompson 
88602ac6454SAndrew Thompson 		/* free resources */
887ed6d949aSAndrew Thompson 		data = usbd_xfer_get_priv(xfer);
88802ac6454SAndrew Thompson 		rum_tx_free(data, 0);
889ed6d949aSAndrew Thompson 		usbd_xfer_set_priv(xfer, NULL);
89002ac6454SAndrew Thompson 
89102ac6454SAndrew Thompson 		/* FALLTHROUGH */
89202ac6454SAndrew Thompson 	case USB_ST_SETUP:
89302ac6454SAndrew Thompson tr_setup:
89402ac6454SAndrew Thompson 		data = STAILQ_FIRST(&sc->tx_q);
89502ac6454SAndrew Thompson 		if (data) {
89602ac6454SAndrew Thompson 			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
89702ac6454SAndrew Thompson 			m = data->m;
89802ac6454SAndrew Thompson 
8996d917491SHans Petter Selasky 			if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
90002ac6454SAndrew Thompson 				DPRINTFN(0, "data overflow, %u bytes\n",
90102ac6454SAndrew Thompson 				    m->m_pkthdr.len);
90202ac6454SAndrew Thompson 				m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
90302ac6454SAndrew Thompson 			}
904ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
905ed6d949aSAndrew Thompson 			usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
906ed6d949aSAndrew Thompson 			usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
907ed6d949aSAndrew Thompson 			    m->m_pkthdr.len);
90802ac6454SAndrew Thompson 
9095463c4a4SSam Leffler 			vap = data->ni->ni_vap;
9105463c4a4SSam Leffler 			if (ieee80211_radiotap_active_vap(vap)) {
91102ac6454SAndrew Thompson 				struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
91202ac6454SAndrew Thompson 
91302ac6454SAndrew Thompson 				tap->wt_flags = 0;
91402ac6454SAndrew Thompson 				tap->wt_rate = data->rate;
915a6ccd477SAdrian Chadd 				rum_get_tsf(sc, &tap->wt_tsf);
91602ac6454SAndrew Thompson 				tap->wt_antenna = sc->tx_ant;
91702ac6454SAndrew Thompson 
9185463c4a4SSam Leffler 				ieee80211_radiotap_tx(vap, m);
91902ac6454SAndrew Thompson 			}
92002ac6454SAndrew Thompson 
92102ac6454SAndrew Thompson 			/* align end on a 4-bytes boundary */
92202ac6454SAndrew Thompson 			len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
92302ac6454SAndrew Thompson 			if ((len % 64) == 0)
92402ac6454SAndrew Thompson 				len += 4;
92502ac6454SAndrew Thompson 
92602ac6454SAndrew Thompson 			DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
92702ac6454SAndrew Thompson 			    m->m_pkthdr.len, len);
92802ac6454SAndrew Thompson 
929ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, len);
930ed6d949aSAndrew Thompson 			usbd_xfer_set_priv(xfer, data);
93102ac6454SAndrew Thompson 
932a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
93302ac6454SAndrew Thompson 		}
9347a79cebfSGleb Smirnoff 		rum_start(sc);
93502ac6454SAndrew Thompson 		break;
93602ac6454SAndrew Thompson 
93702ac6454SAndrew Thompson 	default:			/* Error */
93802ac6454SAndrew Thompson 		DPRINTFN(11, "transfer error, %s\n",
939ed6d949aSAndrew Thompson 		    usbd_errstr(error));
94002ac6454SAndrew Thompson 
9417a79cebfSGleb Smirnoff 		counter_u64_add(sc->sc_ic.ic_oerrors, 1);
942ed6d949aSAndrew Thompson 		data = usbd_xfer_get_priv(xfer);
94302ac6454SAndrew Thompson 		if (data != NULL) {
944ed6d949aSAndrew Thompson 			rum_tx_free(data, error);
945ed6d949aSAndrew Thompson 			usbd_xfer_set_priv(xfer, NULL);
94602ac6454SAndrew Thompson 		}
94702ac6454SAndrew Thompson 
9480283fab7SAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
9490283fab7SAndrew Thompson 			if (error == USB_ERR_TIMEOUT)
9500283fab7SAndrew Thompson 				device_printf(sc->sc_dev, "device timeout\n");
9510283fab7SAndrew Thompson 
9520283fab7SAndrew Thompson 			/*
9530283fab7SAndrew Thompson 			 * Try to clear stall first, also if other
9540283fab7SAndrew Thompson 			 * errors occur, hence clearing stall
9550283fab7SAndrew Thompson 			 * introduces a 50 ms delay:
9560283fab7SAndrew Thompson 			 */
957ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
95802ac6454SAndrew Thompson 			goto tr_setup;
95902ac6454SAndrew Thompson 		}
96002ac6454SAndrew Thompson 		break;
96102ac6454SAndrew Thompson 	}
96202ac6454SAndrew Thompson }
96302ac6454SAndrew Thompson 
96402ac6454SAndrew Thompson static void
965ed6d949aSAndrew Thompson rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
96602ac6454SAndrew Thompson {
967ed6d949aSAndrew Thompson 	struct rum_softc *sc = usbd_xfer_softc(xfer);
9687a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
969aca2cf30SAdrian Chadd 	struct ieee80211_frame_min *wh;
97002ac6454SAndrew Thompson 	struct ieee80211_node *ni;
97102ac6454SAndrew Thompson 	struct mbuf *m = NULL;
972ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
97302ac6454SAndrew Thompson 	uint32_t flags;
97402ac6454SAndrew Thompson 	uint8_t rssi = 0;
975ed6d949aSAndrew Thompson 	int len;
976ed6d949aSAndrew Thompson 
977ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
97802ac6454SAndrew Thompson 
97902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
98002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
98102ac6454SAndrew Thompson 
982ed6d949aSAndrew Thompson 		DPRINTFN(15, "rx done, actlen=%d\n", len);
98302ac6454SAndrew Thompson 
9846d917491SHans Petter Selasky 		if (len < (int)(RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN)) {
98502ac6454SAndrew Thompson 			DPRINTF("%s: xfer too short %d\n",
98602ac6454SAndrew Thompson 			    device_get_nameunit(sc->sc_dev), len);
9877a79cebfSGleb Smirnoff 			counter_u64_add(ic->ic_ierrors, 1);
98802ac6454SAndrew Thompson 			goto tr_setup;
98902ac6454SAndrew Thompson 		}
99002ac6454SAndrew Thompson 
99102ac6454SAndrew Thompson 		len -= RT2573_RX_DESC_SIZE;
992ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
993ed6d949aSAndrew Thompson 		usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
99402ac6454SAndrew Thompson 
99502ac6454SAndrew Thompson 		rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
99602ac6454SAndrew Thompson 		flags = le32toh(sc->sc_rx_desc.flags);
99702ac6454SAndrew Thompson 		if (flags & RT2573_RX_CRC_ERROR) {
99802ac6454SAndrew Thompson 			/*
99902ac6454SAndrew Thompson 		         * This should not happen since we did not
100002ac6454SAndrew Thompson 		         * request to receive those frames when we
100102ac6454SAndrew Thompson 		         * filled RUM_TXRX_CSR2:
100202ac6454SAndrew Thompson 		         */
100302ac6454SAndrew Thompson 			DPRINTFN(5, "PHY or CRC error\n");
10047a79cebfSGleb Smirnoff 			counter_u64_add(ic->ic_ierrors, 1);
100502ac6454SAndrew Thompson 			goto tr_setup;
100602ac6454SAndrew Thompson 		}
1007*2cb9ef8dSAdrian Chadd 		if ((flags & RT2573_RX_DEC_MASK) != RT2573_RX_DEC_OK) {
1008*2cb9ef8dSAdrian Chadd 			switch (flags & RT2573_RX_DEC_MASK) {
1009*2cb9ef8dSAdrian Chadd 			case RT2573_RX_IV_ERROR:
1010*2cb9ef8dSAdrian Chadd 				DPRINTFN(5, "IV/EIV error\n");
1011*2cb9ef8dSAdrian Chadd 				break;
1012*2cb9ef8dSAdrian Chadd 			case RT2573_RX_MIC_ERROR:
1013*2cb9ef8dSAdrian Chadd 				DPRINTFN(5, "MIC error\n");
1014*2cb9ef8dSAdrian Chadd 				break;
1015*2cb9ef8dSAdrian Chadd 			case RT2573_RX_KEY_ERROR:
1016*2cb9ef8dSAdrian Chadd 				DPRINTFN(5, "Key error\n");
1017*2cb9ef8dSAdrian Chadd 				break;
1018*2cb9ef8dSAdrian Chadd 			}
1019*2cb9ef8dSAdrian Chadd 			counter_u64_add(ic->ic_ierrors, 1);
1020*2cb9ef8dSAdrian Chadd 			goto tr_setup;
1021*2cb9ef8dSAdrian Chadd 		}
102202ac6454SAndrew Thompson 
1023c6499eccSGleb Smirnoff 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
102402ac6454SAndrew Thompson 		if (m == NULL) {
102502ac6454SAndrew Thompson 			DPRINTF("could not allocate mbuf\n");
10267a79cebfSGleb Smirnoff 			counter_u64_add(ic->ic_ierrors, 1);
102702ac6454SAndrew Thompson 			goto tr_setup;
102802ac6454SAndrew Thompson 		}
1029ed6d949aSAndrew Thompson 		usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
103002ac6454SAndrew Thompson 		    mtod(m, uint8_t *), len);
103102ac6454SAndrew Thompson 
1032aca2cf30SAdrian Chadd 		wh = mtod(m, struct ieee80211_frame_min *);
1033aca2cf30SAdrian Chadd 
1034*2cb9ef8dSAdrian Chadd 		if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
1035*2cb9ef8dSAdrian Chadd 		    (flags & RT2573_RX_CIP_MASK) !=
1036*2cb9ef8dSAdrian Chadd 		     RT2573_RX_CIP_MODE(RT2573_MODE_NOSEC)) {
1037*2cb9ef8dSAdrian Chadd 			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
1038*2cb9ef8dSAdrian Chadd 			m->m_flags |= M_WEP;
1039*2cb9ef8dSAdrian Chadd 		}
1040*2cb9ef8dSAdrian Chadd 
104102ac6454SAndrew Thompson 		/* finalize mbuf */
104202ac6454SAndrew Thompson 		m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
104302ac6454SAndrew Thompson 
10445463c4a4SSam Leffler 		if (ieee80211_radiotap_active(ic)) {
104502ac6454SAndrew Thompson 			struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
104602ac6454SAndrew Thompson 
10475463c4a4SSam Leffler 			tap->wr_flags = 0;
104802ac6454SAndrew Thompson 			tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
104902ac6454SAndrew Thompson 			    (flags & RT2573_RX_OFDM) ?
105002ac6454SAndrew Thompson 			    IEEE80211_T_OFDM : IEEE80211_T_CCK);
1051a6ccd477SAdrian Chadd 			rum_get_tsf(sc, &tap->wr_tsf);
10525463c4a4SSam Leffler 			tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
10535463c4a4SSam Leffler 			tap->wr_antnoise = RT2573_NOISE_FLOOR;
105402ac6454SAndrew Thompson 			tap->wr_antenna = sc->rx_ant;
105502ac6454SAndrew Thompson 		}
105602ac6454SAndrew Thompson 		/* FALLTHROUGH */
105702ac6454SAndrew Thompson 	case USB_ST_SETUP:
105802ac6454SAndrew Thompson tr_setup:
1059ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1060a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
106102ac6454SAndrew Thompson 
106202ac6454SAndrew Thompson 		/*
106302ac6454SAndrew Thompson 		 * At the end of a USB callback it is always safe to unlock
106402ac6454SAndrew Thompson 		 * the private mutex of a device! That is why we do the
106502ac6454SAndrew Thompson 		 * "ieee80211_input" here, and not some lines up!
106602ac6454SAndrew Thompson 		 */
106702ac6454SAndrew Thompson 		RUM_UNLOCK(sc);
1068db63fc93SWeongyo Jeong 		if (m) {
1069aca2cf30SAdrian Chadd 			if (m->m_len >= sizeof(struct ieee80211_frame_min))
1070aca2cf30SAdrian Chadd 				ni = ieee80211_find_rxnode(ic, wh);
1071aca2cf30SAdrian Chadd 			else
1072aca2cf30SAdrian Chadd 				ni = NULL;
1073aca2cf30SAdrian Chadd 
107402ac6454SAndrew Thompson 			if (ni != NULL) {
107502ac6454SAndrew Thompson 				(void) ieee80211_input(ni, m, rssi,
10765463c4a4SSam Leffler 				    RT2573_NOISE_FLOOR);
107702ac6454SAndrew Thompson 				ieee80211_free_node(ni);
107802ac6454SAndrew Thompson 			} else
107902ac6454SAndrew Thompson 				(void) ieee80211_input_all(ic, m, rssi,
10805463c4a4SSam Leffler 				    RT2573_NOISE_FLOOR);
108102ac6454SAndrew Thompson 		}
1082db63fc93SWeongyo Jeong 		RUM_LOCK(sc);
10837a79cebfSGleb Smirnoff 		rum_start(sc);
108402ac6454SAndrew Thompson 		return;
108502ac6454SAndrew Thompson 
108602ac6454SAndrew Thompson 	default:			/* Error */
1087ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
108802ac6454SAndrew Thompson 			/* try to clear stall first */
1089ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
109002ac6454SAndrew Thompson 			goto tr_setup;
109102ac6454SAndrew Thompson 		}
109202ac6454SAndrew Thompson 		return;
109302ac6454SAndrew Thompson 	}
109402ac6454SAndrew Thompson }
109502ac6454SAndrew Thompson 
109602ac6454SAndrew Thompson static uint8_t
109702ac6454SAndrew Thompson rum_plcp_signal(int rate)
109802ac6454SAndrew Thompson {
109902ac6454SAndrew Thompson 	switch (rate) {
110002ac6454SAndrew Thompson 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
110102ac6454SAndrew Thompson 	case 12:	return 0xb;
110202ac6454SAndrew Thompson 	case 18:	return 0xf;
110302ac6454SAndrew Thompson 	case 24:	return 0xa;
110402ac6454SAndrew Thompson 	case 36:	return 0xe;
110502ac6454SAndrew Thompson 	case 48:	return 0x9;
110602ac6454SAndrew Thompson 	case 72:	return 0xd;
110702ac6454SAndrew Thompson 	case 96:	return 0x8;
110802ac6454SAndrew Thompson 	case 108:	return 0xc;
110902ac6454SAndrew Thompson 
111002ac6454SAndrew Thompson 	/* CCK rates (NB: not IEEE std, device-specific) */
111102ac6454SAndrew Thompson 	case 2:		return 0x0;
111202ac6454SAndrew Thompson 	case 4:		return 0x1;
111302ac6454SAndrew Thompson 	case 11:	return 0x2;
111402ac6454SAndrew Thompson 	case 22:	return 0x3;
111502ac6454SAndrew Thompson 	}
111602ac6454SAndrew Thompson 	return 0xff;		/* XXX unsupported/unknown rate */
111702ac6454SAndrew Thompson }
111802ac6454SAndrew Thompson 
1119*2cb9ef8dSAdrian Chadd /*
1120*2cb9ef8dSAdrian Chadd  * Map net80211 cipher to RT2573 security mode.
1121*2cb9ef8dSAdrian Chadd  */
1122*2cb9ef8dSAdrian Chadd static uint8_t
1123*2cb9ef8dSAdrian Chadd rum_crypto_mode(struct rum_softc *sc, u_int cipher, int keylen)
1124*2cb9ef8dSAdrian Chadd {
1125*2cb9ef8dSAdrian Chadd 	switch (cipher) {
1126*2cb9ef8dSAdrian Chadd 	case IEEE80211_CIPHER_WEP:
1127*2cb9ef8dSAdrian Chadd 		return (keylen < 8 ? RT2573_MODE_WEP40 : RT2573_MODE_WEP104);
1128*2cb9ef8dSAdrian Chadd 	case IEEE80211_CIPHER_TKIP:
1129*2cb9ef8dSAdrian Chadd 		return RT2573_MODE_TKIP;
1130*2cb9ef8dSAdrian Chadd 	case IEEE80211_CIPHER_AES_CCM:
1131*2cb9ef8dSAdrian Chadd 		return RT2573_MODE_AES_CCMP;
1132*2cb9ef8dSAdrian Chadd 	default:
1133*2cb9ef8dSAdrian Chadd 		device_printf(sc->sc_dev, "unknown cipher %d\n", cipher);
1134*2cb9ef8dSAdrian Chadd 		return 0;
1135*2cb9ef8dSAdrian Chadd 	}
1136*2cb9ef8dSAdrian Chadd }
1137*2cb9ef8dSAdrian Chadd 
113802ac6454SAndrew Thompson static void
113902ac6454SAndrew Thompson rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1140*2cb9ef8dSAdrian Chadd     struct ieee80211_key *k, uint32_t flags, uint8_t xflags, int hdrlen,
1141*2cb9ef8dSAdrian Chadd     int len, int rate)
114202ac6454SAndrew Thompson {
11437a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
114402ac6454SAndrew Thompson 	uint16_t plcp_length;
114502ac6454SAndrew Thompson 	int remainder;
114602ac6454SAndrew Thompson 
1147*2cb9ef8dSAdrian Chadd 	flags |= RT2573_TX_VALID;
1148*2cb9ef8dSAdrian Chadd 	flags |= len << 16;
114902ac6454SAndrew Thompson 
1150*2cb9ef8dSAdrian Chadd 	if (k != NULL && !(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1151*2cb9ef8dSAdrian Chadd 		const struct ieee80211_cipher *cip = k->wk_cipher;
115202ac6454SAndrew Thompson 
1153*2cb9ef8dSAdrian Chadd 		len += cip->ic_header + cip->ic_trailer + cip->ic_miclen;
1154*2cb9ef8dSAdrian Chadd 
1155*2cb9ef8dSAdrian Chadd 		desc->eiv = 0;		/* for WEP */
1156*2cb9ef8dSAdrian Chadd 		cip->ic_setiv(k, (uint8_t *)&desc->iv);
1157*2cb9ef8dSAdrian Chadd 	}
115802ac6454SAndrew Thompson 
115902ac6454SAndrew Thompson 	/* setup PLCP fields */
116002ac6454SAndrew Thompson 	desc->plcp_signal  = rum_plcp_signal(rate);
116102ac6454SAndrew Thompson 	desc->plcp_service = 4;
116202ac6454SAndrew Thompson 
116302ac6454SAndrew Thompson 	len += IEEE80211_CRC_LEN;
116426d39e2cSSam Leffler 	if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1165*2cb9ef8dSAdrian Chadd 		flags |= RT2573_TX_OFDM;
116602ac6454SAndrew Thompson 
116702ac6454SAndrew Thompson 		plcp_length = len & 0xfff;
116802ac6454SAndrew Thompson 		desc->plcp_length_hi = plcp_length >> 6;
116902ac6454SAndrew Thompson 		desc->plcp_length_lo = plcp_length & 0x3f;
117002ac6454SAndrew Thompson 	} else {
1171b8e3094cSHans Petter Selasky 		if (rate == 0)
1172b8e3094cSHans Petter Selasky 			rate = 2;	/* avoid division by zero */
117302ac6454SAndrew Thompson 		plcp_length = (16 * len + rate - 1) / rate;
117402ac6454SAndrew Thompson 		if (rate == 22) {
117502ac6454SAndrew Thompson 			remainder = (16 * len) % 22;
117602ac6454SAndrew Thompson 			if (remainder != 0 && remainder < 7)
117702ac6454SAndrew Thompson 				desc->plcp_service |= RT2573_PLCP_LENGEXT;
117802ac6454SAndrew Thompson 		}
117902ac6454SAndrew Thompson 		desc->plcp_length_hi = plcp_length >> 8;
118002ac6454SAndrew Thompson 		desc->plcp_length_lo = plcp_length & 0xff;
118102ac6454SAndrew Thompson 
118202ac6454SAndrew Thompson 		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
118302ac6454SAndrew Thompson 			desc->plcp_signal |= 0x08;
118402ac6454SAndrew Thompson 	}
1185*2cb9ef8dSAdrian Chadd 
1186*2cb9ef8dSAdrian Chadd 	desc->flags = htole32(flags);
1187*2cb9ef8dSAdrian Chadd 	desc->hdrlen = hdrlen;
1188*2cb9ef8dSAdrian Chadd 	desc->xflags = xflags;
1189*2cb9ef8dSAdrian Chadd 
1190*2cb9ef8dSAdrian Chadd 	desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) |
1191*2cb9ef8dSAdrian Chadd 	    RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10));
119202ac6454SAndrew Thompson }
119302ac6454SAndrew Thompson 
119402ac6454SAndrew Thompson static int
119502ac6454SAndrew Thompson rum_sendprot(struct rum_softc *sc,
119602ac6454SAndrew Thompson     const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
119702ac6454SAndrew Thompson {
119802ac6454SAndrew Thompson 	struct ieee80211com *ic = ni->ni_ic;
119902ac6454SAndrew Thompson 	const struct ieee80211_frame *wh;
120002ac6454SAndrew Thompson 	struct rum_tx_data *data;
120102ac6454SAndrew Thompson 	struct mbuf *mprot;
1202e7b7db3dSAdrian Chadd 	int protrate, pktlen, flags, isshort;
120302ac6454SAndrew Thompson 	uint16_t dur;
120402ac6454SAndrew Thompson 
1205e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
120602ac6454SAndrew Thompson 	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
120702ac6454SAndrew Thompson 	    ("protection %d", prot));
120802ac6454SAndrew Thompson 
120902ac6454SAndrew Thompson 	wh = mtod(m, const struct ieee80211_frame *);
121002ac6454SAndrew Thompson 	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
121102ac6454SAndrew Thompson 
121226d39e2cSSam Leffler 	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
121302ac6454SAndrew Thompson 
121402ac6454SAndrew Thompson 	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
12150f817de7SJung-uk Kim 	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
121626d39e2cSSam Leffler 	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
121702ac6454SAndrew Thompson 	flags = RT2573_TX_MORE_FRAG;
121802ac6454SAndrew Thompson 	if (prot == IEEE80211_PROT_RTSCTS) {
121902ac6454SAndrew Thompson 		/* NB: CTS is the same size as an ACK */
122026d39e2cSSam Leffler 		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
122102ac6454SAndrew Thompson 		flags |= RT2573_TX_NEED_ACK;
122202ac6454SAndrew Thompson 		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
122302ac6454SAndrew Thompson 	} else {
122402ac6454SAndrew Thompson 		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
122502ac6454SAndrew Thompson 	}
122602ac6454SAndrew Thompson 	if (mprot == NULL) {
122702ac6454SAndrew Thompson 		/* XXX stat + msg */
122877ddf3d3SAndrew Thompson 		return (ENOBUFS);
122902ac6454SAndrew Thompson 	}
123002ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
123102ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
123202ac6454SAndrew Thompson 	sc->tx_nfree--;
123302ac6454SAndrew Thompson 
123402ac6454SAndrew Thompson 	data->m = mprot;
123502ac6454SAndrew Thompson 	data->ni = ieee80211_ref_node(ni);
123602ac6454SAndrew Thompson 	data->rate = protrate;
1237*2cb9ef8dSAdrian Chadd 	rum_setup_tx_desc(sc, &data->desc, NULL, flags, 0, 0,
1238*2cb9ef8dSAdrian Chadd 	    mprot->m_pkthdr.len, protrate);
123902ac6454SAndrew Thompson 
124002ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1241a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
124202ac6454SAndrew Thompson 
124302ac6454SAndrew Thompson 	return 0;
124402ac6454SAndrew Thompson }
124502ac6454SAndrew Thompson 
1246*2cb9ef8dSAdrian Chadd static uint32_t
1247*2cb9ef8dSAdrian Chadd rum_tx_crypto_flags(struct rum_softc *sc, struct ieee80211_node *ni,
1248*2cb9ef8dSAdrian Chadd     const struct ieee80211_key *k)
1249*2cb9ef8dSAdrian Chadd {
1250*2cb9ef8dSAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
1251*2cb9ef8dSAdrian Chadd 	u_int cipher;
1252*2cb9ef8dSAdrian Chadd 	uint32_t flags = 0;
1253*2cb9ef8dSAdrian Chadd 	uint8_t mode, pos;
1254*2cb9ef8dSAdrian Chadd 
1255*2cb9ef8dSAdrian Chadd 	if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1256*2cb9ef8dSAdrian Chadd 		cipher = k->wk_cipher->ic_cipher;
1257*2cb9ef8dSAdrian Chadd 		pos = k->wk_keyix;
1258*2cb9ef8dSAdrian Chadd 		mode = rum_crypto_mode(sc, cipher, k->wk_keylen);
1259*2cb9ef8dSAdrian Chadd 		if (mode == 0)
1260*2cb9ef8dSAdrian Chadd 			return 0;
1261*2cb9ef8dSAdrian Chadd 
1262*2cb9ef8dSAdrian Chadd 		flags |= RT2573_TX_CIP_MODE(mode);
1263*2cb9ef8dSAdrian Chadd 
1264*2cb9ef8dSAdrian Chadd 		/* Do not trust GROUP flag */
1265*2cb9ef8dSAdrian Chadd 		if (!(k >= &vap->iv_nw_keys[0] &&
1266*2cb9ef8dSAdrian Chadd 		      k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]))
1267*2cb9ef8dSAdrian Chadd 			flags |= RT2573_TX_KEY_PAIR;
1268*2cb9ef8dSAdrian Chadd 		else
1269*2cb9ef8dSAdrian Chadd 			pos += 0 * RT2573_SKEY_MAX;	/* vap id */
1270*2cb9ef8dSAdrian Chadd 
1271*2cb9ef8dSAdrian Chadd 		flags |= RT2573_TX_KEY_ID(pos);
1272*2cb9ef8dSAdrian Chadd 
1273*2cb9ef8dSAdrian Chadd 		if (cipher == IEEE80211_CIPHER_TKIP)
1274*2cb9ef8dSAdrian Chadd 			flags |= RT2573_TX_TKIPMIC;
1275*2cb9ef8dSAdrian Chadd 	}
1276*2cb9ef8dSAdrian Chadd 
1277*2cb9ef8dSAdrian Chadd 	return flags;
1278*2cb9ef8dSAdrian Chadd }
1279*2cb9ef8dSAdrian Chadd 
128002ac6454SAndrew Thompson static int
128102ac6454SAndrew Thompson rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
128202ac6454SAndrew Thompson {
128302ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
12847a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
128502ac6454SAndrew Thompson 	struct rum_tx_data *data;
128602ac6454SAndrew Thompson 	struct ieee80211_frame *wh;
128702ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
1288*2cb9ef8dSAdrian Chadd 	struct ieee80211_key *k = NULL;
128902ac6454SAndrew Thompson 	uint32_t flags = 0;
129002ac6454SAndrew Thompson 	uint16_t dur;
1291*2cb9ef8dSAdrian Chadd 	int hdrlen;
129202ac6454SAndrew Thompson 
1293e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
129402ac6454SAndrew Thompson 
129502ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
129602ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
129702ac6454SAndrew Thompson 	sc->tx_nfree--;
129802ac6454SAndrew Thompson 
129902ac6454SAndrew Thompson 	wh = mtod(m0, struct ieee80211_frame *);
1300*2cb9ef8dSAdrian Chadd 	hdrlen = ieee80211_anyhdrsize(wh);
1301*2cb9ef8dSAdrian Chadd 
13025945b5f5SKevin Lo 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1303*2cb9ef8dSAdrian Chadd 		k = ieee80211_crypto_get_txkey(ni, m0);
1304b2a76342SAdrian Chadd 		if (k == NULL)
1305*2cb9ef8dSAdrian Chadd 			return (ENOENT);
1306*2cb9ef8dSAdrian Chadd 
1307*2cb9ef8dSAdrian Chadd 		if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1308*2cb9ef8dSAdrian Chadd 		    !k->wk_cipher->ic_encap(k, m0))
1309b2a76342SAdrian Chadd 			return (ENOBUFS);
1310b2a76342SAdrian Chadd 
131102ac6454SAndrew Thompson 		wh = mtod(m0, struct ieee80211_frame *);
131202ac6454SAndrew Thompson 	}
131302ac6454SAndrew Thompson 
131402ac6454SAndrew Thompson 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
131502ac6454SAndrew Thompson 
131602ac6454SAndrew Thompson 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
131702ac6454SAndrew Thompson 		flags |= RT2573_TX_NEED_ACK;
131802ac6454SAndrew Thompson 
131926d39e2cSSam Leffler 		dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
132002ac6454SAndrew Thompson 		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
13218cfe5440SHans Petter Selasky 		USETW(wh->i_dur, dur);
132202ac6454SAndrew Thompson 
132302ac6454SAndrew Thompson 		/* tell hardware to add timestamp for probe responses */
132402ac6454SAndrew Thompson 		if ((wh->i_fc[0] &
132502ac6454SAndrew Thompson 		    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
132602ac6454SAndrew Thompson 		    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
132702ac6454SAndrew Thompson 			flags |= RT2573_TX_TIMESTAMP;
132802ac6454SAndrew Thompson 	}
132902ac6454SAndrew Thompson 
1330*2cb9ef8dSAdrian Chadd 	if (k != NULL)
1331*2cb9ef8dSAdrian Chadd 		flags |= rum_tx_crypto_flags(sc, ni, k);
1332*2cb9ef8dSAdrian Chadd 
133302ac6454SAndrew Thompson 	data->m = m0;
133402ac6454SAndrew Thompson 	data->ni = ni;
133502ac6454SAndrew Thompson 	data->rate = tp->mgmtrate;
133602ac6454SAndrew Thompson 
1337*2cb9ef8dSAdrian Chadd 	rum_setup_tx_desc(sc, &data->desc, k, flags, 0, hdrlen,
1338*2cb9ef8dSAdrian Chadd 	    m0->m_pkthdr.len, tp->mgmtrate);
133902ac6454SAndrew Thompson 
134002ac6454SAndrew Thompson 	DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
134102ac6454SAndrew Thompson 	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
134202ac6454SAndrew Thompson 
134302ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1344a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
134502ac6454SAndrew Thompson 
134677ddf3d3SAndrew Thompson 	return (0);
134702ac6454SAndrew Thompson }
134802ac6454SAndrew Thompson 
134902ac6454SAndrew Thompson static int
135002ac6454SAndrew Thompson rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
135102ac6454SAndrew Thompson     const struct ieee80211_bpf_params *params)
135202ac6454SAndrew Thompson {
1353515db61dSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
135402ac6454SAndrew Thompson 	struct rum_tx_data *data;
135502ac6454SAndrew Thompson 	uint32_t flags;
135602ac6454SAndrew Thompson 	int rate, error;
135702ac6454SAndrew Thompson 
1358e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
135902ac6454SAndrew Thompson 
1360515db61dSSam Leffler 	rate = params->ibp_rate0;
1361b2a76342SAdrian Chadd 	if (!ieee80211_isratevalid(ic->ic_rt, rate))
1362b2a76342SAdrian Chadd 		return (EINVAL);
1363b2a76342SAdrian Chadd 
136402ac6454SAndrew Thompson 	flags = 0;
136502ac6454SAndrew Thompson 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
136602ac6454SAndrew Thompson 		flags |= RT2573_TX_NEED_ACK;
136702ac6454SAndrew Thompson 	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
136802ac6454SAndrew Thompson 		error = rum_sendprot(sc, m0, ni,
136902ac6454SAndrew Thompson 		    params->ibp_flags & IEEE80211_BPF_RTS ?
137002ac6454SAndrew Thompson 			 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
137102ac6454SAndrew Thompson 		    rate);
1372b2a76342SAdrian Chadd 		if (error || sc->tx_nfree == 0)
1373b2a76342SAdrian Chadd 			return (ENOBUFS);
1374b2a76342SAdrian Chadd 
137502ac6454SAndrew Thompson 		flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
137602ac6454SAndrew Thompson 	}
137702ac6454SAndrew Thompson 
137896ca458fSAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
137996ca458fSAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
138096ca458fSAndrew Thompson 	sc->tx_nfree--;
138196ca458fSAndrew Thompson 
138202ac6454SAndrew Thompson 	data->m = m0;
138302ac6454SAndrew Thompson 	data->ni = ni;
138402ac6454SAndrew Thompson 	data->rate = rate;
138502ac6454SAndrew Thompson 
138602ac6454SAndrew Thompson 	/* XXX need to setup descriptor ourself */
1387*2cb9ef8dSAdrian Chadd 	rum_setup_tx_desc(sc, &data->desc, NULL, flags, 0, 0, m0->m_pkthdr.len,
1388*2cb9ef8dSAdrian Chadd 	    rate);
138902ac6454SAndrew Thompson 
139002ac6454SAndrew Thompson 	DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
139102ac6454SAndrew Thompson 	    m0->m_pkthdr.len, rate);
139202ac6454SAndrew Thompson 
139302ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1394a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
139502ac6454SAndrew Thompson 
139602ac6454SAndrew Thompson 	return 0;
139702ac6454SAndrew Thompson }
139802ac6454SAndrew Thompson 
139902ac6454SAndrew Thompson static int
140002ac6454SAndrew Thompson rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
140102ac6454SAndrew Thompson {
140202ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
14037a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
140402ac6454SAndrew Thompson 	struct rum_tx_data *data;
140502ac6454SAndrew Thompson 	struct ieee80211_frame *wh;
140602ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
1407*2cb9ef8dSAdrian Chadd 	struct ieee80211_key *k = NULL;
140802ac6454SAndrew Thompson 	uint32_t flags = 0;
140902ac6454SAndrew Thompson 	uint16_t dur;
1410*2cb9ef8dSAdrian Chadd 	int error, hdrlen, rate;
141102ac6454SAndrew Thompson 
1412e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
141302ac6454SAndrew Thompson 
141402ac6454SAndrew Thompson 	wh = mtod(m0, struct ieee80211_frame *);
1415*2cb9ef8dSAdrian Chadd 	hdrlen = ieee80211_anyhdrsize(wh);
141602ac6454SAndrew Thompson 
141702ac6454SAndrew Thompson 	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
141802ac6454SAndrew Thompson 	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
141902ac6454SAndrew Thompson 		rate = tp->mcastrate;
142002ac6454SAndrew Thompson 	else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
142102ac6454SAndrew Thompson 		rate = tp->ucastrate;
142202ac6454SAndrew Thompson 	else
142302ac6454SAndrew Thompson 		rate = ni->ni_txrate;
142402ac6454SAndrew Thompson 
14255945b5f5SKevin Lo 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1426*2cb9ef8dSAdrian Chadd 		k = ieee80211_crypto_get_txkey(ni, m0);
142702ac6454SAndrew Thompson 		if (k == NULL) {
142802ac6454SAndrew Thompson 			m_freem(m0);
1429*2cb9ef8dSAdrian Chadd 			return (ENOENT);
1430*2cb9ef8dSAdrian Chadd 		}
1431*2cb9ef8dSAdrian Chadd 		if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1432*2cb9ef8dSAdrian Chadd 		    !k->wk_cipher->ic_encap(k, m0)) {
1433*2cb9ef8dSAdrian Chadd 			m_freem(m0);
1434*2cb9ef8dSAdrian Chadd 			return (ENOBUFS);
143502ac6454SAndrew Thompson 		}
143602ac6454SAndrew Thompson 
143702ac6454SAndrew Thompson 		/* packet header may have moved, reset our local pointer */
143802ac6454SAndrew Thompson 		wh = mtod(m0, struct ieee80211_frame *);
143902ac6454SAndrew Thompson 	}
144002ac6454SAndrew Thompson 
144102ac6454SAndrew Thompson 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
144202ac6454SAndrew Thompson 		int prot = IEEE80211_PROT_NONE;
144302ac6454SAndrew Thompson 		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
144402ac6454SAndrew Thompson 			prot = IEEE80211_PROT_RTSCTS;
144502ac6454SAndrew Thompson 		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
144626d39e2cSSam Leffler 		    ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
144702ac6454SAndrew Thompson 			prot = ic->ic_protmode;
144802ac6454SAndrew Thompson 		if (prot != IEEE80211_PROT_NONE) {
144902ac6454SAndrew Thompson 			error = rum_sendprot(sc, m0, ni, prot, rate);
145096ca458fSAndrew Thompson 			if (error || sc->tx_nfree == 0) {
145102ac6454SAndrew Thompson 				m_freem(m0);
145296ca458fSAndrew Thompson 				return ENOBUFS;
145302ac6454SAndrew Thompson 			}
145402ac6454SAndrew Thompson 			flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
145502ac6454SAndrew Thompson 		}
145602ac6454SAndrew Thompson 	}
145702ac6454SAndrew Thompson 
1458*2cb9ef8dSAdrian Chadd 	if (k != NULL)
1459*2cb9ef8dSAdrian Chadd 		flags |= rum_tx_crypto_flags(sc, ni, k);
1460*2cb9ef8dSAdrian Chadd 
146102ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
146202ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
146302ac6454SAndrew Thompson 	sc->tx_nfree--;
146402ac6454SAndrew Thompson 
146502ac6454SAndrew Thompson 	data->m = m0;
146602ac6454SAndrew Thompson 	data->ni = ni;
146702ac6454SAndrew Thompson 	data->rate = rate;
146802ac6454SAndrew Thompson 
146902ac6454SAndrew Thompson 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
147002ac6454SAndrew Thompson 		flags |= RT2573_TX_NEED_ACK;
147102ac6454SAndrew Thompson 		flags |= RT2573_TX_MORE_FRAG;
147202ac6454SAndrew Thompson 
147326d39e2cSSam Leffler 		dur = ieee80211_ack_duration(ic->ic_rt, rate,
147402ac6454SAndrew Thompson 		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
14758cfe5440SHans Petter Selasky 		USETW(wh->i_dur, dur);
147602ac6454SAndrew Thompson 	}
147702ac6454SAndrew Thompson 
1478*2cb9ef8dSAdrian Chadd 	rum_setup_tx_desc(sc, &data->desc, k, flags, 0, hdrlen,
1479*2cb9ef8dSAdrian Chadd 	    m0->m_pkthdr.len, rate);
148002ac6454SAndrew Thompson 
148102ac6454SAndrew Thompson 	DPRINTFN(10, "sending frame len=%d rate=%d\n",
148202ac6454SAndrew Thompson 	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
148302ac6454SAndrew Thompson 
148402ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1485a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
148602ac6454SAndrew Thompson 
148702ac6454SAndrew Thompson 	return 0;
148802ac6454SAndrew Thompson }
148902ac6454SAndrew Thompson 
14907a79cebfSGleb Smirnoff static int
14917a79cebfSGleb Smirnoff rum_transmit(struct ieee80211com *ic, struct mbuf *m)
149279d2c5e8SGleb Smirnoff {
14937a79cebfSGleb Smirnoff 	struct rum_softc *sc = ic->ic_softc;
14947a79cebfSGleb Smirnoff 	int error;
14957a79cebfSGleb Smirnoff 
14967a79cebfSGleb Smirnoff 	RUM_LOCK(sc);
14977a79cebfSGleb Smirnoff 	if (!sc->sc_running) {
14987a79cebfSGleb Smirnoff 		RUM_UNLOCK(sc);
14997a79cebfSGleb Smirnoff 		return (ENXIO);
15007a79cebfSGleb Smirnoff 	}
15017a79cebfSGleb Smirnoff 	error = mbufq_enqueue(&sc->sc_snd, m);
15027a79cebfSGleb Smirnoff 	if (error) {
15037a79cebfSGleb Smirnoff 		RUM_UNLOCK(sc);
15047a79cebfSGleb Smirnoff 		return (error);
15057a79cebfSGleb Smirnoff 	}
15067a79cebfSGleb Smirnoff 	rum_start(sc);
15077a79cebfSGleb Smirnoff 	RUM_UNLOCK(sc);
15087a79cebfSGleb Smirnoff 
15097a79cebfSGleb Smirnoff 	return (0);
15107a79cebfSGleb Smirnoff }
15117a79cebfSGleb Smirnoff 
15127a79cebfSGleb Smirnoff static void
15137a79cebfSGleb Smirnoff rum_start(struct rum_softc *sc)
15147a79cebfSGleb Smirnoff {
151502ac6454SAndrew Thompson 	struct ieee80211_node *ni;
151602ac6454SAndrew Thompson 	struct mbuf *m;
151702ac6454SAndrew Thompson 
1518e7b7db3dSAdrian Chadd 	RUM_LOCK_ASSERT(sc);
15197a79cebfSGleb Smirnoff 
15207a79cebfSGleb Smirnoff 	if (!sc->sc_running)
152102ac6454SAndrew Thompson 		return;
15227a79cebfSGleb Smirnoff 
15237a79cebfSGleb Smirnoff 	while (sc->tx_nfree >= RUM_TX_MINFREE &&
15247a79cebfSGleb Smirnoff 	    (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
152502ac6454SAndrew Thompson 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
152602ac6454SAndrew Thompson 		if (rum_tx_data(sc, m, ni) != 0) {
15277a79cebfSGleb Smirnoff 			if_inc_counter(ni->ni_vap->iv_ifp,
15287a79cebfSGleb Smirnoff 			    IFCOUNTER_OERRORS, 1);
152902ac6454SAndrew Thompson 			ieee80211_free_node(ni);
153002ac6454SAndrew Thompson 			break;
153102ac6454SAndrew Thompson 		}
153202ac6454SAndrew Thompson 	}
153302ac6454SAndrew Thompson }
153402ac6454SAndrew Thompson 
15357a79cebfSGleb Smirnoff static void
15367a79cebfSGleb Smirnoff rum_parent(struct ieee80211com *ic)
153702ac6454SAndrew Thompson {
1538d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
15390698c0b3SAdrian Chadd 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1540645e4d17SHans Petter Selasky 
1541645e4d17SHans Petter Selasky 	RUM_LOCK(sc);
15427a79cebfSGleb Smirnoff 	if (sc->sc_detached) {
1543645e4d17SHans Petter Selasky 		RUM_UNLOCK(sc);
15447a79cebfSGleb Smirnoff 		return;
15457a79cebfSGleb Smirnoff 	}
154602ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
15470698c0b3SAdrian Chadd 
15480698c0b3SAdrian Chadd 	if (ic->ic_nrunning > 0) {
15490698c0b3SAdrian Chadd 		if (rum_init(sc) == 0)
155002ac6454SAndrew Thompson 			ieee80211_start_all(ic);
15510698c0b3SAdrian Chadd 		else
15520698c0b3SAdrian Chadd 			ieee80211_stop(vap);
15530698c0b3SAdrian Chadd 	} else
15540698c0b3SAdrian Chadd 		rum_stop(sc);
155502ac6454SAndrew Thompson }
155602ac6454SAndrew Thompson 
155702ac6454SAndrew Thompson static void
155802ac6454SAndrew Thompson rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
155902ac6454SAndrew Thompson {
1560760bc48eSAndrew Thompson 	struct usb_device_request req;
1561e0a69b51SAndrew Thompson 	usb_error_t error;
156202ac6454SAndrew Thompson 
156302ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
156402ac6454SAndrew Thompson 	req.bRequest = RT2573_READ_EEPROM;
156502ac6454SAndrew Thompson 	USETW(req.wValue, 0);
156602ac6454SAndrew Thompson 	USETW(req.wIndex, addr);
156702ac6454SAndrew Thompson 	USETW(req.wLength, len);
156802ac6454SAndrew Thompson 
156902ac6454SAndrew Thompson 	error = rum_do_request(sc, &req, buf);
157002ac6454SAndrew Thompson 	if (error != 0) {
157102ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1572a593f6b8SAndrew Thompson 		    usbd_errstr(error));
157302ac6454SAndrew Thompson 	}
157402ac6454SAndrew Thompson }
157502ac6454SAndrew Thompson 
157602ac6454SAndrew Thompson static uint32_t
157702ac6454SAndrew Thompson rum_read(struct rum_softc *sc, uint16_t reg)
157802ac6454SAndrew Thompson {
157902ac6454SAndrew Thompson 	uint32_t val;
158002ac6454SAndrew Thompson 
158102ac6454SAndrew Thompson 	rum_read_multi(sc, reg, &val, sizeof val);
158202ac6454SAndrew Thompson 
158302ac6454SAndrew Thompson 	return le32toh(val);
158402ac6454SAndrew Thompson }
158502ac6454SAndrew Thompson 
158602ac6454SAndrew Thompson static void
158702ac6454SAndrew Thompson rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
158802ac6454SAndrew Thompson {
1589760bc48eSAndrew Thompson 	struct usb_device_request req;
1590e0a69b51SAndrew Thompson 	usb_error_t error;
159102ac6454SAndrew Thompson 
159202ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
159302ac6454SAndrew Thompson 	req.bRequest = RT2573_READ_MULTI_MAC;
159402ac6454SAndrew Thompson 	USETW(req.wValue, 0);
159502ac6454SAndrew Thompson 	USETW(req.wIndex, reg);
159602ac6454SAndrew Thompson 	USETW(req.wLength, len);
159702ac6454SAndrew Thompson 
159802ac6454SAndrew Thompson 	error = rum_do_request(sc, &req, buf);
159902ac6454SAndrew Thompson 	if (error != 0) {
160002ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
160102ac6454SAndrew Thompson 		    "could not multi read MAC register: %s\n",
1602a593f6b8SAndrew Thompson 		    usbd_errstr(error));
160302ac6454SAndrew Thompson 	}
160402ac6454SAndrew Thompson }
160502ac6454SAndrew Thompson 
1606e0a69b51SAndrew Thompson static usb_error_t
160702ac6454SAndrew Thompson rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
160802ac6454SAndrew Thompson {
160902ac6454SAndrew Thompson 	uint32_t tmp = htole32(val);
161002ac6454SAndrew Thompson 
161177ddf3d3SAndrew Thompson 	return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
161202ac6454SAndrew Thompson }
161302ac6454SAndrew Thompson 
1614e0a69b51SAndrew Thompson static usb_error_t
161502ac6454SAndrew Thompson rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
161602ac6454SAndrew Thompson {
1617760bc48eSAndrew Thompson 	struct usb_device_request req;
1618e0a69b51SAndrew Thompson 	usb_error_t error;
16196d917491SHans Petter Selasky 	size_t offset;
162002ac6454SAndrew Thompson 
162102ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
162202ac6454SAndrew Thompson 	req.bRequest = RT2573_WRITE_MULTI_MAC;
162302ac6454SAndrew Thompson 	USETW(req.wValue, 0);
162402ac6454SAndrew Thompson 
162565d7e4d9SAdrian Chadd 	/* write at most 64 bytes at a time */
162665d7e4d9SAdrian Chadd 	for (offset = 0; offset < len; offset += 64) {
162765d7e4d9SAdrian Chadd 		USETW(req.wIndex, reg + offset);
162865d7e4d9SAdrian Chadd 		USETW(req.wLength, MIN(len - offset, 64));
162965d7e4d9SAdrian Chadd 
16308797cafeSBjoern A. Zeeb 		error = rum_do_request(sc, &req, (char *)buf + offset);
163102ac6454SAndrew Thompson 		if (error != 0) {
163202ac6454SAndrew Thompson 			device_printf(sc->sc_dev,
163302ac6454SAndrew Thompson 			    "could not multi write MAC register: %s\n",
1634a593f6b8SAndrew Thompson 			    usbd_errstr(error));
163577ddf3d3SAndrew Thompson 			return (error);
163602ac6454SAndrew Thompson 		}
163765d7e4d9SAdrian Chadd 	}
16388797cafeSBjoern A. Zeeb 
16398797cafeSBjoern A. Zeeb 	return (USB_ERR_NORMAL_COMPLETION);
164065d7e4d9SAdrian Chadd }
164102ac6454SAndrew Thompson 
1642a05022b2SAdrian Chadd static usb_error_t
1643a05022b2SAdrian Chadd rum_setbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1644a05022b2SAdrian Chadd {
1645a05022b2SAdrian Chadd 	return (rum_write(sc, reg, rum_read(sc, reg) | mask));
1646a05022b2SAdrian Chadd }
1647a05022b2SAdrian Chadd 
1648a05022b2SAdrian Chadd static usb_error_t
1649a05022b2SAdrian Chadd rum_clrbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1650a05022b2SAdrian Chadd {
1651a05022b2SAdrian Chadd 	return (rum_write(sc, reg, rum_read(sc, reg) & ~mask));
1652a05022b2SAdrian Chadd }
1653a05022b2SAdrian Chadd 
1654a05022b2SAdrian Chadd static usb_error_t
1655a05022b2SAdrian Chadd rum_modbits(struct rum_softc *sc, uint16_t reg, uint32_t set, uint32_t unset)
1656a05022b2SAdrian Chadd {
1657a05022b2SAdrian Chadd 	return (rum_write(sc, reg, (rum_read(sc, reg) & ~unset) | set));
1658a05022b2SAdrian Chadd }
1659a05022b2SAdrian Chadd 
1660cc9ae761SAdrian Chadd static int
1661cc9ae761SAdrian Chadd rum_bbp_busy(struct rum_softc *sc)
166202ac6454SAndrew Thompson {
166302ac6454SAndrew Thompson 	int ntries;
166402ac6454SAndrew Thompson 
166502ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
166602ac6454SAndrew Thompson 		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
166702ac6454SAndrew Thompson 			break;
166802ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
166902ac6454SAndrew Thompson 			break;
167002ac6454SAndrew Thompson 	}
1671cc9ae761SAdrian Chadd 	if (ntries == 100)
1672cc9ae761SAdrian Chadd 		return (ETIMEDOUT);
1673cc9ae761SAdrian Chadd 
1674cc9ae761SAdrian Chadd 	return (0);
1675cc9ae761SAdrian Chadd }
1676cc9ae761SAdrian Chadd 
1677cc9ae761SAdrian Chadd static void
1678cc9ae761SAdrian Chadd rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1679cc9ae761SAdrian Chadd {
1680cc9ae761SAdrian Chadd 	uint32_t tmp;
1681cc9ae761SAdrian Chadd 
1682cc9ae761SAdrian Chadd 	DPRINTFN(2, "reg=0x%08x\n", reg);
1683cc9ae761SAdrian Chadd 
1684cc9ae761SAdrian Chadd 	if (rum_bbp_busy(sc) != 0) {
168502ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not write to BBP\n");
168602ac6454SAndrew Thompson 		return;
168702ac6454SAndrew Thompson 	}
168802ac6454SAndrew Thompson 
168902ac6454SAndrew Thompson 	tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
169002ac6454SAndrew Thompson 	rum_write(sc, RT2573_PHY_CSR3, tmp);
169102ac6454SAndrew Thompson }
169202ac6454SAndrew Thompson 
169302ac6454SAndrew Thompson static uint8_t
169402ac6454SAndrew Thompson rum_bbp_read(struct rum_softc *sc, uint8_t reg)
169502ac6454SAndrew Thompson {
169602ac6454SAndrew Thompson 	uint32_t val;
169702ac6454SAndrew Thompson 	int ntries;
169802ac6454SAndrew Thompson 
169977ddf3d3SAndrew Thompson 	DPRINTFN(2, "reg=0x%08x\n", reg);
170077ddf3d3SAndrew Thompson 
1701cc9ae761SAdrian Chadd 	if (rum_bbp_busy(sc) != 0) {
170202ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read BBP\n");
170302ac6454SAndrew Thompson 		return 0;
170402ac6454SAndrew Thompson 	}
170502ac6454SAndrew Thompson 
170602ac6454SAndrew Thompson 	val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
170702ac6454SAndrew Thompson 	rum_write(sc, RT2573_PHY_CSR3, val);
170802ac6454SAndrew Thompson 
170902ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
171002ac6454SAndrew Thompson 		val = rum_read(sc, RT2573_PHY_CSR3);
171102ac6454SAndrew Thompson 		if (!(val & RT2573_BBP_BUSY))
171202ac6454SAndrew Thompson 			return val & 0xff;
171302ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
171402ac6454SAndrew Thompson 			break;
171502ac6454SAndrew Thompson 	}
171602ac6454SAndrew Thompson 
171702ac6454SAndrew Thompson 	device_printf(sc->sc_dev, "could not read BBP\n");
171802ac6454SAndrew Thompson 	return 0;
171902ac6454SAndrew Thompson }
172002ac6454SAndrew Thompson 
172102ac6454SAndrew Thompson static void
172202ac6454SAndrew Thompson rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
172302ac6454SAndrew Thompson {
172402ac6454SAndrew Thompson 	uint32_t tmp;
172502ac6454SAndrew Thompson 	int ntries;
172602ac6454SAndrew Thompson 
172702ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
172802ac6454SAndrew Thompson 		if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
172902ac6454SAndrew Thompson 			break;
173002ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
173102ac6454SAndrew Thompson 			break;
173202ac6454SAndrew Thompson 	}
173302ac6454SAndrew Thompson 	if (ntries == 100) {
173402ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not write to RF\n");
173502ac6454SAndrew Thompson 		return;
173602ac6454SAndrew Thompson 	}
173702ac6454SAndrew Thompson 
173867784314SPoul-Henning Kamp 	tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
173902ac6454SAndrew Thompson 	    (reg & 3);
174002ac6454SAndrew Thompson 	rum_write(sc, RT2573_PHY_CSR4, tmp);
174102ac6454SAndrew Thompson 
174202ac6454SAndrew Thompson 	/* remember last written value in sc */
174302ac6454SAndrew Thompson 	sc->rf_regs[reg] = val;
174402ac6454SAndrew Thompson 
174502ac6454SAndrew Thompson 	DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
174602ac6454SAndrew Thompson }
174702ac6454SAndrew Thompson 
174802ac6454SAndrew Thompson static void
174902ac6454SAndrew Thompson rum_select_antenna(struct rum_softc *sc)
175002ac6454SAndrew Thompson {
175102ac6454SAndrew Thompson 	uint8_t bbp4, bbp77;
175202ac6454SAndrew Thompson 	uint32_t tmp;
175302ac6454SAndrew Thompson 
175402ac6454SAndrew Thompson 	bbp4  = rum_bbp_read(sc, 4);
175502ac6454SAndrew Thompson 	bbp77 = rum_bbp_read(sc, 77);
175602ac6454SAndrew Thompson 
175702ac6454SAndrew Thompson 	/* TBD */
175802ac6454SAndrew Thompson 
175902ac6454SAndrew Thompson 	/* make sure Rx is disabled before switching antenna */
176002ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
176102ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
176202ac6454SAndrew Thompson 
176302ac6454SAndrew Thompson 	rum_bbp_write(sc,  4, bbp4);
176402ac6454SAndrew Thompson 	rum_bbp_write(sc, 77, bbp77);
176502ac6454SAndrew Thompson 
176602ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
176702ac6454SAndrew Thompson }
176802ac6454SAndrew Thompson 
176902ac6454SAndrew Thompson /*
177002ac6454SAndrew Thompson  * Enable multi-rate retries for frames sent at OFDM rates.
177102ac6454SAndrew Thompson  * In 802.11b/g mode, allow fallback to CCK rates.
177202ac6454SAndrew Thompson  */
177302ac6454SAndrew Thompson static void
177402ac6454SAndrew Thompson rum_enable_mrr(struct rum_softc *sc)
177502ac6454SAndrew Thompson {
17767a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
177702ac6454SAndrew Thompson 
1778a05022b2SAdrian Chadd 	if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1779a05022b2SAdrian Chadd 		rum_setbits(sc, RT2573_TXRX_CSR4,
1780a05022b2SAdrian Chadd 		    RT2573_MRR_ENABLED | RT2573_MRR_CCK_FALLBACK);
1781a05022b2SAdrian Chadd 	} else {
1782a05022b2SAdrian Chadd 		rum_modbits(sc, RT2573_TXRX_CSR4,
1783a05022b2SAdrian Chadd 		    RT2573_MRR_ENABLED, RT2573_MRR_CCK_FALLBACK);
1784a05022b2SAdrian Chadd 	}
178502ac6454SAndrew Thompson }
178602ac6454SAndrew Thompson 
178702ac6454SAndrew Thompson static void
178802ac6454SAndrew Thompson rum_set_txpreamble(struct rum_softc *sc)
178902ac6454SAndrew Thompson {
17907a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
179102ac6454SAndrew Thompson 
179202ac6454SAndrew Thompson 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1793a05022b2SAdrian Chadd 		rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
1794a05022b2SAdrian Chadd 	else
1795a05022b2SAdrian Chadd 		rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
179602ac6454SAndrew Thompson }
179702ac6454SAndrew Thompson 
179802ac6454SAndrew Thompson static void
179902ac6454SAndrew Thompson rum_set_basicrates(struct rum_softc *sc)
180002ac6454SAndrew Thompson {
18017a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
180202ac6454SAndrew Thompson 
180302ac6454SAndrew Thompson 	/* update basic rate set */
180402ac6454SAndrew Thompson 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
180502ac6454SAndrew Thompson 		/* 11b basic rates: 1, 2Mbps */
180602ac6454SAndrew Thompson 		rum_write(sc, RT2573_TXRX_CSR5, 0x3);
180702ac6454SAndrew Thompson 	} else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
180802ac6454SAndrew Thompson 		/* 11a basic rates: 6, 12, 24Mbps */
180902ac6454SAndrew Thompson 		rum_write(sc, RT2573_TXRX_CSR5, 0x150);
181002ac6454SAndrew Thompson 	} else {
181102ac6454SAndrew Thompson 		/* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
181202ac6454SAndrew Thompson 		rum_write(sc, RT2573_TXRX_CSR5, 0xf);
181302ac6454SAndrew Thompson 	}
181402ac6454SAndrew Thompson }
181502ac6454SAndrew Thompson 
181602ac6454SAndrew Thompson /*
181702ac6454SAndrew Thompson  * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
181802ac6454SAndrew Thompson  * driver.
181902ac6454SAndrew Thompson  */
182002ac6454SAndrew Thompson static void
182102ac6454SAndrew Thompson rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
182202ac6454SAndrew Thompson {
182302ac6454SAndrew Thompson 	uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
182402ac6454SAndrew Thompson 
182502ac6454SAndrew Thompson 	/* update all BBP registers that depend on the band */
182602ac6454SAndrew Thompson 	bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
182702ac6454SAndrew Thompson 	bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
182802ac6454SAndrew Thompson 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
182902ac6454SAndrew Thompson 		bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
183002ac6454SAndrew Thompson 		bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
183102ac6454SAndrew Thompson 	}
183202ac6454SAndrew Thompson 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
183302ac6454SAndrew Thompson 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
183402ac6454SAndrew Thompson 		bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
183502ac6454SAndrew Thompson 	}
183602ac6454SAndrew Thompson 
183702ac6454SAndrew Thompson 	sc->bbp17 = bbp17;
183802ac6454SAndrew Thompson 	rum_bbp_write(sc,  17, bbp17);
183902ac6454SAndrew Thompson 	rum_bbp_write(sc,  96, bbp96);
184002ac6454SAndrew Thompson 	rum_bbp_write(sc, 104, bbp104);
184102ac6454SAndrew Thompson 
184202ac6454SAndrew Thompson 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
184302ac6454SAndrew Thompson 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
184402ac6454SAndrew Thompson 		rum_bbp_write(sc, 75, 0x80);
184502ac6454SAndrew Thompson 		rum_bbp_write(sc, 86, 0x80);
184602ac6454SAndrew Thompson 		rum_bbp_write(sc, 88, 0x80);
184702ac6454SAndrew Thompson 	}
184802ac6454SAndrew Thompson 
184902ac6454SAndrew Thompson 	rum_bbp_write(sc, 35, bbp35);
185002ac6454SAndrew Thompson 	rum_bbp_write(sc, 97, bbp97);
185102ac6454SAndrew Thompson 	rum_bbp_write(sc, 98, bbp98);
185202ac6454SAndrew Thompson 
1853a05022b2SAdrian Chadd 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
1854a05022b2SAdrian Chadd 		rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_2GHZ,
1855a05022b2SAdrian Chadd 		    RT2573_PA_PE_5GHZ);
1856a05022b2SAdrian Chadd 	} else {
1857a05022b2SAdrian Chadd 		rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_5GHZ,
1858a05022b2SAdrian Chadd 		    RT2573_PA_PE_2GHZ);
1859a05022b2SAdrian Chadd 	}
186002ac6454SAndrew Thompson }
186102ac6454SAndrew Thompson 
186202ac6454SAndrew Thompson static void
186302ac6454SAndrew Thompson rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
186402ac6454SAndrew Thompson {
18657a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
186602ac6454SAndrew Thompson 	const struct rfprog *rfprog;
186702ac6454SAndrew Thompson 	uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
186802ac6454SAndrew Thompson 	int8_t power;
186902ac6454SAndrew Thompson 	int i, chan;
187002ac6454SAndrew Thompson 
187102ac6454SAndrew Thompson 	chan = ieee80211_chan2ieee(ic, c);
187202ac6454SAndrew Thompson 	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
187302ac6454SAndrew Thompson 		return;
187402ac6454SAndrew Thompson 
187502ac6454SAndrew Thompson 	/* select the appropriate RF settings based on what EEPROM says */
187602ac6454SAndrew Thompson 	rfprog = (sc->rf_rev == RT2573_RF_5225 ||
187702ac6454SAndrew Thompson 		  sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
187802ac6454SAndrew Thompson 
187902ac6454SAndrew Thompson 	/* find the settings for this channel (we know it exists) */
188002ac6454SAndrew Thompson 	for (i = 0; rfprog[i].chan != chan; i++);
188102ac6454SAndrew Thompson 
188202ac6454SAndrew Thompson 	power = sc->txpow[i];
188302ac6454SAndrew Thompson 	if (power < 0) {
188402ac6454SAndrew Thompson 		bbp94 += power;
188502ac6454SAndrew Thompson 		power = 0;
188602ac6454SAndrew Thompson 	} else if (power > 31) {
188702ac6454SAndrew Thompson 		bbp94 += power - 31;
188802ac6454SAndrew Thompson 		power = 31;
188902ac6454SAndrew Thompson 	}
189002ac6454SAndrew Thompson 
189102ac6454SAndrew Thompson 	/*
189202ac6454SAndrew Thompson 	 * If we are switching from the 2GHz band to the 5GHz band or
189302ac6454SAndrew Thompson 	 * vice-versa, BBP registers need to be reprogrammed.
189402ac6454SAndrew Thompson 	 */
189502ac6454SAndrew Thompson 	if (c->ic_flags != ic->ic_curchan->ic_flags) {
189602ac6454SAndrew Thompson 		rum_select_band(sc, c);
189702ac6454SAndrew Thompson 		rum_select_antenna(sc);
189802ac6454SAndrew Thompson 	}
189902ac6454SAndrew Thompson 	ic->ic_curchan = c;
190002ac6454SAndrew Thompson 
190102ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
190202ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
190302ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
190402ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
190502ac6454SAndrew Thompson 
190602ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
190702ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
190802ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
190902ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
191002ac6454SAndrew Thompson 
191102ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
191202ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
191302ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
191402ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
191502ac6454SAndrew Thompson 
191602ac6454SAndrew Thompson 	rum_pause(sc, hz / 100);
191702ac6454SAndrew Thompson 
191802ac6454SAndrew Thompson 	/* enable smart mode for MIMO-capable RFs */
191902ac6454SAndrew Thompson 	bbp3 = rum_bbp_read(sc, 3);
192002ac6454SAndrew Thompson 
192102ac6454SAndrew Thompson 	bbp3 &= ~RT2573_SMART_MODE;
192202ac6454SAndrew Thompson 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
192302ac6454SAndrew Thompson 		bbp3 |= RT2573_SMART_MODE;
192402ac6454SAndrew Thompson 
192502ac6454SAndrew Thompson 	rum_bbp_write(sc, 3, bbp3);
192602ac6454SAndrew Thompson 
192702ac6454SAndrew Thompson 	if (bbp94 != RT2573_BBPR94_DEFAULT)
192802ac6454SAndrew Thompson 		rum_bbp_write(sc, 94, bbp94);
192977ddf3d3SAndrew Thompson 
193077ddf3d3SAndrew Thompson 	/* give the chip some extra time to do the switchover */
193177ddf3d3SAndrew Thompson 	rum_pause(sc, hz / 100);
193202ac6454SAndrew Thompson }
193302ac6454SAndrew Thompson 
193402ac6454SAndrew Thompson /*
193502ac6454SAndrew Thompson  * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
193602ac6454SAndrew Thompson  * and HostAP operating modes.
193702ac6454SAndrew Thompson  */
1938aa81f853SAdrian Chadd static int
193902ac6454SAndrew Thompson rum_enable_tsf_sync(struct rum_softc *sc)
194002ac6454SAndrew Thompson {
19417a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
194202ac6454SAndrew Thompson 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
194302ac6454SAndrew Thompson 	uint32_t tmp;
194402ac6454SAndrew Thompson 
194502ac6454SAndrew Thompson 	if (vap->iv_opmode != IEEE80211_M_STA) {
194602ac6454SAndrew Thompson 		/*
194702ac6454SAndrew Thompson 		 * Change default 16ms TBTT adjustment to 8ms.
194802ac6454SAndrew Thompson 		 * Must be done before enabling beacon generation.
194902ac6454SAndrew Thompson 		 */
1950aa81f853SAdrian Chadd 		if (rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8) != 0)
1951aa81f853SAdrian Chadd 			return EIO;
195202ac6454SAndrew Thompson 	}
195302ac6454SAndrew Thompson 
195402ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
195502ac6454SAndrew Thompson 
195602ac6454SAndrew Thompson 	/* set beacon interval (in 1/16ms unit) */
195702ac6454SAndrew Thompson 	tmp |= vap->iv_bss->ni_intval * 16;
1958d847071cSAdrian Chadd 	tmp |= RT2573_TSF_TIMER_EN | RT2573_TBTT_TIMER_EN;
195902ac6454SAndrew Thompson 
1960d847071cSAdrian Chadd 	switch (vap->iv_opmode) {
1961d847071cSAdrian Chadd 	case IEEE80211_M_STA:
1962d847071cSAdrian Chadd 		/*
1963d847071cSAdrian Chadd 		 * Local TSF is always updated with remote TSF on beacon
1964d847071cSAdrian Chadd 		 * reception.
1965d847071cSAdrian Chadd 		 */
1966d847071cSAdrian Chadd 		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_STA);
1967d847071cSAdrian Chadd 		break;
1968d847071cSAdrian Chadd 	case IEEE80211_M_IBSS:
1969d847071cSAdrian Chadd 		/*
1970d847071cSAdrian Chadd 		 * Local TSF is updated with remote TSF on beacon reception
1971d847071cSAdrian Chadd 		 * only if the remote TSF is greater than local TSF.
1972d847071cSAdrian Chadd 		 */
1973d847071cSAdrian Chadd 		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_IBSS);
1974d847071cSAdrian Chadd 		tmp |= RT2573_BCN_TX_EN;
1975d847071cSAdrian Chadd 		break;
1976d847071cSAdrian Chadd 	case IEEE80211_M_HOSTAP:
1977d847071cSAdrian Chadd 		/* SYNC with nobody */
1978d847071cSAdrian Chadd 		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_HOSTAP);
1979d847071cSAdrian Chadd 		tmp |= RT2573_BCN_TX_EN;
1980d847071cSAdrian Chadd 		break;
1981d847071cSAdrian Chadd 	default:
1982d847071cSAdrian Chadd 		device_printf(sc->sc_dev,
1983d847071cSAdrian Chadd 		    "Enabling TSF failed. undefined opmode %d\n",
1984d847071cSAdrian Chadd 		    vap->iv_opmode);
1985aa81f853SAdrian Chadd 		return EINVAL;
1986d847071cSAdrian Chadd 	}
198702ac6454SAndrew Thompson 
1988aa81f853SAdrian Chadd 	if (rum_write(sc, RT2573_TXRX_CSR9, tmp) != 0)
1989aa81f853SAdrian Chadd 		return EIO;
1990aa81f853SAdrian Chadd 
1991aa81f853SAdrian Chadd 	return 0;
199202ac6454SAndrew Thompson }
199302ac6454SAndrew Thompson 
199402ac6454SAndrew Thompson static void
19955463c4a4SSam Leffler rum_enable_tsf(struct rum_softc *sc)
19965463c4a4SSam Leffler {
1997d847071cSAdrian Chadd 	rum_modbits(sc, RT2573_TXRX_CSR9, RT2573_TSF_TIMER_EN |
1998d847071cSAdrian Chadd 	    RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_DIS), 0x00ffffff);
19995463c4a4SSam Leffler }
20005463c4a4SSam Leffler 
20015463c4a4SSam Leffler static void
20026d804321SAdrian Chadd rum_abort_tsf_sync(struct rum_softc *sc)
20036d804321SAdrian Chadd {
2004a05022b2SAdrian Chadd 	rum_clrbits(sc, RT2573_TXRX_CSR9, 0x00ffffff);
20056d804321SAdrian Chadd }
20066d804321SAdrian Chadd 
20076d804321SAdrian Chadd static void
2008a6ccd477SAdrian Chadd rum_get_tsf(struct rum_softc *sc, uint64_t *buf)
2009a6ccd477SAdrian Chadd {
2010a6ccd477SAdrian Chadd 	rum_read_multi(sc, RT2573_TXRX_CSR12, buf, sizeof (*buf));
2011a6ccd477SAdrian Chadd }
2012a6ccd477SAdrian Chadd 
2013a6ccd477SAdrian Chadd static void
2014a19dbd8dSAdrian Chadd rum_update_slot_cb(struct rum_softc *sc, union sec_param *data, uint8_t rv_id,
2015a19dbd8dSAdrian Chadd     uint8_t rvp_id)
201602ac6454SAndrew Thompson {
20177a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
201802ac6454SAndrew Thompson 	uint8_t slottime;
201902ac6454SAndrew Thompson 
202002ac6454SAndrew Thompson 	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
202102ac6454SAndrew Thompson 
2022a05022b2SAdrian Chadd 	rum_modbits(sc, RT2573_MAC_CSR9, slottime, 0xff);
202302ac6454SAndrew Thompson 
202402ac6454SAndrew Thompson 	DPRINTF("setting slot time to %uus\n", slottime);
202502ac6454SAndrew Thompson }
202602ac6454SAndrew Thompson 
202702ac6454SAndrew Thompson static void
2028a19dbd8dSAdrian Chadd rum_update_slot(struct ieee80211com *ic)
2029a19dbd8dSAdrian Chadd {
2030a19dbd8dSAdrian Chadd 	rum_cmd_sleepable(ic->ic_softc, NULL, 0, 0, 0, rum_update_slot_cb);
2031a19dbd8dSAdrian Chadd }
2032a19dbd8dSAdrian Chadd 
2033a19dbd8dSAdrian Chadd static void
203402ac6454SAndrew Thompson rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
203502ac6454SAndrew Thompson {
203602ac6454SAndrew Thompson 
203742769826SAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR4,
203842769826SAdrian Chadd 	    bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
203942769826SAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR5,
204042769826SAdrian Chadd 	    bssid[4] | bssid[5] << 8 | RT2573_NUM_BSSID_MSK(1));
204102ac6454SAndrew Thompson }
204202ac6454SAndrew Thompson 
204302ac6454SAndrew Thompson static void
204402ac6454SAndrew Thompson rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
204502ac6454SAndrew Thompson {
204602ac6454SAndrew Thompson 
204742769826SAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR2,
204842769826SAdrian Chadd 	    addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
204942769826SAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR3,
205042769826SAdrian Chadd 	    addr[4] | addr[5] << 8 | 0xff << 16);
205102ac6454SAndrew Thompson }
205202ac6454SAndrew Thompson 
205302ac6454SAndrew Thompson static void
20545efea30fSAndrew Thompson rum_setpromisc(struct rum_softc *sc)
205502ac6454SAndrew Thompson {
2056a05022b2SAdrian Chadd 	struct ieee80211com *ic = &sc->sc_ic;
205702ac6454SAndrew Thompson 
2058a05022b2SAdrian Chadd 	if (ic->ic_promisc == 0)
2059a05022b2SAdrian Chadd 		rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2060a05022b2SAdrian Chadd 	else
2061a05022b2SAdrian Chadd 		rum_clrbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
206202ac6454SAndrew Thompson 
2063a05022b2SAdrian Chadd 	DPRINTF("%s promiscuous mode\n", ic->ic_promisc > 0 ?
206402ac6454SAndrew Thompson 	    "entering" : "leaving");
206502ac6454SAndrew Thompson }
206602ac6454SAndrew Thompson 
206777ddf3d3SAndrew Thompson static void
2068272f6adeSGleb Smirnoff rum_update_promisc(struct ieee80211com *ic)
206977ddf3d3SAndrew Thompson {
2070272f6adeSGleb Smirnoff 	struct rum_softc *sc = ic->ic_softc;
207177ddf3d3SAndrew Thompson 
2072ba2c1fbcSAdrian Chadd 	RUM_LOCK(sc);
207342769826SAdrian Chadd 	if (sc->sc_running)
20745efea30fSAndrew Thompson 		rum_setpromisc(sc);
207577ddf3d3SAndrew Thompson 	RUM_UNLOCK(sc);
207677ddf3d3SAndrew Thompson }
207777ddf3d3SAndrew Thompson 
20786dd8e071SEd Maste static void
2079272f6adeSGleb Smirnoff rum_update_mcast(struct ieee80211com *ic)
20806dd8e071SEd Maste {
2081e7b7db3dSAdrian Chadd 	/* Ignore. */
20826dd8e071SEd Maste }
20836dd8e071SEd Maste 
208402ac6454SAndrew Thompson static const char *
208502ac6454SAndrew Thompson rum_get_rf(int rev)
208602ac6454SAndrew Thompson {
208702ac6454SAndrew Thompson 	switch (rev) {
208802ac6454SAndrew Thompson 	case RT2573_RF_2527:	return "RT2527 (MIMO XR)";
208902ac6454SAndrew Thompson 	case RT2573_RF_2528:	return "RT2528";
209002ac6454SAndrew Thompson 	case RT2573_RF_5225:	return "RT5225 (MIMO XR)";
209102ac6454SAndrew Thompson 	case RT2573_RF_5226:	return "RT5226";
209202ac6454SAndrew Thompson 	default:		return "unknown";
209302ac6454SAndrew Thompson 	}
209402ac6454SAndrew Thompson }
209502ac6454SAndrew Thompson 
209602ac6454SAndrew Thompson static void
209702ac6454SAndrew Thompson rum_read_eeprom(struct rum_softc *sc)
209802ac6454SAndrew Thompson {
209902ac6454SAndrew Thompson 	uint16_t val;
210002ac6454SAndrew Thompson #ifdef RUM_DEBUG
210102ac6454SAndrew Thompson 	int i;
210202ac6454SAndrew Thompson #endif
210302ac6454SAndrew Thompson 
210402ac6454SAndrew Thompson 	/* read MAC address */
21057a79cebfSGleb Smirnoff 	rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_ic.ic_macaddr, 6);
210602ac6454SAndrew Thompson 
210702ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
210802ac6454SAndrew Thompson 	val = le16toh(val);
210902ac6454SAndrew Thompson 	sc->rf_rev =   (val >> 11) & 0x1f;
211002ac6454SAndrew Thompson 	sc->hw_radio = (val >> 10) & 0x1;
211102ac6454SAndrew Thompson 	sc->rx_ant =   (val >> 4)  & 0x3;
211202ac6454SAndrew Thompson 	sc->tx_ant =   (val >> 2)  & 0x3;
211302ac6454SAndrew Thompson 	sc->nb_ant =   val & 0x3;
211402ac6454SAndrew Thompson 
211502ac6454SAndrew Thompson 	DPRINTF("RF revision=%d\n", sc->rf_rev);
211602ac6454SAndrew Thompson 
211702ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
211802ac6454SAndrew Thompson 	val = le16toh(val);
211902ac6454SAndrew Thompson 	sc->ext_5ghz_lna = (val >> 6) & 0x1;
212002ac6454SAndrew Thompson 	sc->ext_2ghz_lna = (val >> 4) & 0x1;
212102ac6454SAndrew Thompson 
212202ac6454SAndrew Thompson 	DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
212302ac6454SAndrew Thompson 	    sc->ext_2ghz_lna, sc->ext_5ghz_lna);
212402ac6454SAndrew Thompson 
212502ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
212602ac6454SAndrew Thompson 	val = le16toh(val);
212702ac6454SAndrew Thompson 	if ((val & 0xff) != 0xff)
212802ac6454SAndrew Thompson 		sc->rssi_2ghz_corr = (int8_t)(val & 0xff);	/* signed */
212902ac6454SAndrew Thompson 
213002ac6454SAndrew Thompson 	/* Only [-10, 10] is valid */
213102ac6454SAndrew Thompson 	if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
213202ac6454SAndrew Thompson 		sc->rssi_2ghz_corr = 0;
213302ac6454SAndrew Thompson 
213402ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
213502ac6454SAndrew Thompson 	val = le16toh(val);
213602ac6454SAndrew Thompson 	if ((val & 0xff) != 0xff)
213702ac6454SAndrew Thompson 		sc->rssi_5ghz_corr = (int8_t)(val & 0xff);	/* signed */
213802ac6454SAndrew Thompson 
213902ac6454SAndrew Thompson 	/* Only [-10, 10] is valid */
214002ac6454SAndrew Thompson 	if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
214102ac6454SAndrew Thompson 		sc->rssi_5ghz_corr = 0;
214202ac6454SAndrew Thompson 
214302ac6454SAndrew Thompson 	if (sc->ext_2ghz_lna)
214402ac6454SAndrew Thompson 		sc->rssi_2ghz_corr -= 14;
214502ac6454SAndrew Thompson 	if (sc->ext_5ghz_lna)
214602ac6454SAndrew Thompson 		sc->rssi_5ghz_corr -= 14;
214702ac6454SAndrew Thompson 
214802ac6454SAndrew Thompson 	DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
214902ac6454SAndrew Thompson 	    sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
215002ac6454SAndrew Thompson 
215102ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
215202ac6454SAndrew Thompson 	val = le16toh(val);
215302ac6454SAndrew Thompson 	if ((val & 0xff) != 0xff)
215402ac6454SAndrew Thompson 		sc->rffreq = val & 0xff;
215502ac6454SAndrew Thompson 
215602ac6454SAndrew Thompson 	DPRINTF("RF freq=%d\n", sc->rffreq);
215702ac6454SAndrew Thompson 
215802ac6454SAndrew Thompson 	/* read Tx power for all a/b/g channels */
215902ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
216002ac6454SAndrew Thompson 	/* XXX default Tx power for 802.11a channels */
216102ac6454SAndrew Thompson 	memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
216202ac6454SAndrew Thompson #ifdef RUM_DEBUG
216302ac6454SAndrew Thompson 	for (i = 0; i < 14; i++)
216402ac6454SAndrew Thompson 		DPRINTF("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]);
216502ac6454SAndrew Thompson #endif
216602ac6454SAndrew Thompson 
216702ac6454SAndrew Thompson 	/* read default values for BBP registers */
216802ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
216902ac6454SAndrew Thompson #ifdef RUM_DEBUG
217002ac6454SAndrew Thompson 	for (i = 0; i < 14; i++) {
217102ac6454SAndrew Thompson 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
217202ac6454SAndrew Thompson 			continue;
217302ac6454SAndrew Thompson 		DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
217402ac6454SAndrew Thompson 		    sc->bbp_prom[i].val);
217502ac6454SAndrew Thompson 	}
217602ac6454SAndrew Thompson #endif
217702ac6454SAndrew Thompson }
217802ac6454SAndrew Thompson 
217902ac6454SAndrew Thompson static int
21806095f7caSAdrian Chadd rum_bbp_wakeup(struct rum_softc *sc)
21816095f7caSAdrian Chadd {
21826095f7caSAdrian Chadd 	unsigned int ntries;
21836095f7caSAdrian Chadd 
21846095f7caSAdrian Chadd 	for (ntries = 0; ntries < 100; ntries++) {
21856095f7caSAdrian Chadd 		if (rum_read(sc, RT2573_MAC_CSR12) & 8)
21866095f7caSAdrian Chadd 			break;
21876095f7caSAdrian Chadd 		rum_write(sc, RT2573_MAC_CSR12, 4);	/* force wakeup */
21886095f7caSAdrian Chadd 		if (rum_pause(sc, hz / 100))
21896095f7caSAdrian Chadd 			break;
21906095f7caSAdrian Chadd 	}
21916095f7caSAdrian Chadd 	if (ntries == 100) {
21926095f7caSAdrian Chadd 		device_printf(sc->sc_dev,
21936095f7caSAdrian Chadd 		    "timeout waiting for BBP/RF to wakeup\n");
21946095f7caSAdrian Chadd 		return (ETIMEDOUT);
21956095f7caSAdrian Chadd 	}
21966095f7caSAdrian Chadd 
21976095f7caSAdrian Chadd 	return (0);
21986095f7caSAdrian Chadd }
21996095f7caSAdrian Chadd 
22006095f7caSAdrian Chadd static int
220102ac6454SAndrew Thompson rum_bbp_init(struct rum_softc *sc)
220202ac6454SAndrew Thompson {
220302ac6454SAndrew Thompson 	int i, ntries;
220402ac6454SAndrew Thompson 
220502ac6454SAndrew Thompson 	/* wait for BBP to be ready */
220602ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
220702ac6454SAndrew Thompson 		const uint8_t val = rum_bbp_read(sc, 0);
220802ac6454SAndrew Thompson 		if (val != 0 && val != 0xff)
220902ac6454SAndrew Thompson 			break;
221002ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
221102ac6454SAndrew Thompson 			break;
221202ac6454SAndrew Thompson 	}
221302ac6454SAndrew Thompson 	if (ntries == 100) {
221402ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "timeout waiting for BBP\n");
221502ac6454SAndrew Thompson 		return EIO;
221602ac6454SAndrew Thompson 	}
221702ac6454SAndrew Thompson 
221802ac6454SAndrew Thompson 	/* initialize BBP registers to default values */
2219e7b7db3dSAdrian Chadd 	for (i = 0; i < nitems(rum_def_bbp); i++)
222002ac6454SAndrew Thompson 		rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
222102ac6454SAndrew Thompson 
222202ac6454SAndrew Thompson 	/* write vendor-specific BBP values (from EEPROM) */
222302ac6454SAndrew Thompson 	for (i = 0; i < 16; i++) {
222402ac6454SAndrew Thompson 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
222502ac6454SAndrew Thompson 			continue;
222602ac6454SAndrew Thompson 		rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
222702ac6454SAndrew Thompson 	}
222802ac6454SAndrew Thompson 
222902ac6454SAndrew Thompson 	return 0;
223002ac6454SAndrew Thompson }
223102ac6454SAndrew Thompson 
2232*2cb9ef8dSAdrian Chadd static void
2233*2cb9ef8dSAdrian Chadd rum_clr_shkey_regs(struct rum_softc *sc)
2234*2cb9ef8dSAdrian Chadd {
2235*2cb9ef8dSAdrian Chadd 	rum_write(sc, RT2573_SEC_CSR0, 0);
2236*2cb9ef8dSAdrian Chadd 	rum_write(sc, RT2573_SEC_CSR1, 0);
2237*2cb9ef8dSAdrian Chadd 	rum_write(sc, RT2573_SEC_CSR5, 0);
2238*2cb9ef8dSAdrian Chadd }
2239*2cb9ef8dSAdrian Chadd 
22400698c0b3SAdrian Chadd static int
22417a79cebfSGleb Smirnoff rum_init(struct rum_softc *sc)
224202ac6454SAndrew Thompson {
22437a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
22447a79cebfSGleb Smirnoff 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
224502ac6454SAndrew Thompson 	uint32_t tmp;
22466095f7caSAdrian Chadd 	int i, ret;
224702ac6454SAndrew Thompson 
22480698c0b3SAdrian Chadd 	RUM_LOCK(sc);
22490698c0b3SAdrian Chadd 	if (sc->sc_running) {
22500698c0b3SAdrian Chadd 		ret = 0;
22510698c0b3SAdrian Chadd 		goto end;
22520698c0b3SAdrian Chadd 	}
225302ac6454SAndrew Thompson 
225402ac6454SAndrew Thompson 	/* initialize MAC registers to default values */
2255e7b7db3dSAdrian Chadd 	for (i = 0; i < nitems(rum_def_mac); i++)
225602ac6454SAndrew Thompson 		rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
225702ac6454SAndrew Thompson 
225802ac6454SAndrew Thompson 	/* set host ready */
2259e7b7db3dSAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
226002ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR1, 0);
226102ac6454SAndrew Thompson 
226202ac6454SAndrew Thompson 	/* wait for BBP/RF to wakeup */
22636095f7caSAdrian Chadd 	if ((ret = rum_bbp_wakeup(sc)) != 0)
22640698c0b3SAdrian Chadd 		goto end;
226502ac6454SAndrew Thompson 
22660698c0b3SAdrian Chadd 	if ((ret = rum_bbp_init(sc)) != 0)
22670698c0b3SAdrian Chadd 		goto end;
226802ac6454SAndrew Thompson 
226902ac6454SAndrew Thompson 	/* select default channel */
227002ac6454SAndrew Thompson 	rum_select_band(sc, ic->ic_curchan);
227102ac6454SAndrew Thompson 	rum_select_antenna(sc);
227202ac6454SAndrew Thompson 	rum_set_chan(sc, ic->ic_curchan);
227302ac6454SAndrew Thompson 
227402ac6454SAndrew Thompson 	/* clear STA registers */
227502ac6454SAndrew Thompson 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
227602ac6454SAndrew Thompson 
2277*2cb9ef8dSAdrian Chadd 	/* clear security registers (if required) */
2278*2cb9ef8dSAdrian Chadd 	if (sc->sc_clr_shkeys == 0) {
2279*2cb9ef8dSAdrian Chadd 		rum_clr_shkey_regs(sc);
2280*2cb9ef8dSAdrian Chadd 		sc->sc_clr_shkeys = 1;
2281*2cb9ef8dSAdrian Chadd 	}
2282*2cb9ef8dSAdrian Chadd 
22837a79cebfSGleb Smirnoff 	rum_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
228402ac6454SAndrew Thompson 
228502ac6454SAndrew Thompson 	/* initialize ASIC */
2286e7b7db3dSAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR1, RT2573_HOST_READY);
228702ac6454SAndrew Thompson 
228802ac6454SAndrew Thompson 	/*
228902ac6454SAndrew Thompson 	 * Allocate Tx and Rx xfer queues.
229002ac6454SAndrew Thompson 	 */
229102ac6454SAndrew Thompson 	rum_setup_tx_list(sc);
229202ac6454SAndrew Thompson 
229302ac6454SAndrew Thompson 	/* update Rx filter */
229402ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
229502ac6454SAndrew Thompson 
229602ac6454SAndrew Thompson 	tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
229702ac6454SAndrew Thompson 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
229802ac6454SAndrew Thompson 		tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
229902ac6454SAndrew Thompson 		       RT2573_DROP_ACKCTS;
230002ac6454SAndrew Thompson 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
230102ac6454SAndrew Thompson 			tmp |= RT2573_DROP_TODS;
23027a79cebfSGleb Smirnoff 		if (ic->ic_promisc == 0)
230302ac6454SAndrew Thompson 			tmp |= RT2573_DROP_NOT_TO_ME;
230402ac6454SAndrew Thompson 	}
230502ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
230602ac6454SAndrew Thompson 
23077a79cebfSGleb Smirnoff 	sc->sc_running = 1;
2308ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2309a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
231002ac6454SAndrew Thompson 
23110698c0b3SAdrian Chadd end:	RUM_UNLOCK(sc);
23120698c0b3SAdrian Chadd 
23130698c0b3SAdrian Chadd 	if (ret != 0)
23140698c0b3SAdrian Chadd 		rum_stop(sc);
23150698c0b3SAdrian Chadd 
23160698c0b3SAdrian Chadd 	return ret;
231702ac6454SAndrew Thompson }
231802ac6454SAndrew Thompson 
231902ac6454SAndrew Thompson static void
23205efea30fSAndrew Thompson rum_stop(struct rum_softc *sc)
232102ac6454SAndrew Thompson {
232202ac6454SAndrew Thompson 
23230698c0b3SAdrian Chadd 	RUM_LOCK(sc);
23240698c0b3SAdrian Chadd 	if (!sc->sc_running) {
23250698c0b3SAdrian Chadd 		RUM_UNLOCK(sc);
23260698c0b3SAdrian Chadd 		return;
23270698c0b3SAdrian Chadd 	}
23287a79cebfSGleb Smirnoff 	sc->sc_running = 0;
232902ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
233002ac6454SAndrew Thompson 
233102ac6454SAndrew Thompson 	/*
233202ac6454SAndrew Thompson 	 * Drain the USB transfers, if not already drained:
233302ac6454SAndrew Thompson 	 */
2334a593f6b8SAndrew Thompson 	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2335a593f6b8SAndrew Thompson 	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
233602ac6454SAndrew Thompson 
233702ac6454SAndrew Thompson 	RUM_LOCK(sc);
233802ac6454SAndrew Thompson 	rum_unsetup_tx_list(sc);
233902ac6454SAndrew Thompson 
234002ac6454SAndrew Thompson 	/* disable Rx */
2341a05022b2SAdrian Chadd 	rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DISABLE_RX);
234202ac6454SAndrew Thompson 
234302ac6454SAndrew Thompson 	/* reset ASIC */
2344e7b7db3dSAdrian Chadd 	rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
234502ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR1, 0);
23460698c0b3SAdrian Chadd 	RUM_UNLOCK(sc);
234702ac6454SAndrew Thompson }
234802ac6454SAndrew Thompson 
234977ddf3d3SAndrew Thompson static void
235077ddf3d3SAndrew Thompson rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
235102ac6454SAndrew Thompson {
2352760bc48eSAndrew Thompson 	struct usb_device_request req;
235302ac6454SAndrew Thompson 	uint16_t reg = RT2573_MCU_CODE_BASE;
2354e0a69b51SAndrew Thompson 	usb_error_t err;
235502ac6454SAndrew Thompson 
235602ac6454SAndrew Thompson 	/* copy firmware image into NIC */
235777ddf3d3SAndrew Thompson 	for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
235877ddf3d3SAndrew Thompson 		err = rum_write(sc, reg, UGETDW(ucode));
235977ddf3d3SAndrew Thompson 		if (err) {
236077ddf3d3SAndrew Thompson 			/* firmware already loaded ? */
236177ddf3d3SAndrew Thompson 			device_printf(sc->sc_dev, "Firmware load "
236277ddf3d3SAndrew Thompson 			    "failure! (ignored)\n");
236377ddf3d3SAndrew Thompson 			break;
236477ddf3d3SAndrew Thompson 		}
236577ddf3d3SAndrew Thompson 	}
236602ac6454SAndrew Thompson 
236702ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
236802ac6454SAndrew Thompson 	req.bRequest = RT2573_MCU_CNTL;
236902ac6454SAndrew Thompson 	USETW(req.wValue, RT2573_MCU_RUN);
237002ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
237102ac6454SAndrew Thompson 	USETW(req.wLength, 0);
237202ac6454SAndrew Thompson 
237377ddf3d3SAndrew Thompson 	err = rum_do_request(sc, &req, NULL);
237477ddf3d3SAndrew Thompson 	if (err != 0) {
237502ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not run firmware: %s\n",
2376a593f6b8SAndrew Thompson 		    usbd_errstr(err));
237702ac6454SAndrew Thompson 	}
237877ddf3d3SAndrew Thompson 
237977ddf3d3SAndrew Thompson 	/* give the chip some time to boot */
238077ddf3d3SAndrew Thompson 	rum_pause(sc, hz / 8);
238102ac6454SAndrew Thompson }
238202ac6454SAndrew Thompson 
2383aa81f853SAdrian Chadd static int
2384b780f864SAdrian Chadd rum_set_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
238502ac6454SAndrew Thompson {
238602ac6454SAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
2387b780f864SAdrian Chadd 	struct rum_vap *rvp = RUM_VAP(vap);
2388b780f864SAdrian Chadd 	struct mbuf *m = rvp->bcn_mbuf;
238902ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
239002ac6454SAndrew Thompson 	struct rum_tx_desc desc;
239102ac6454SAndrew Thompson 
2392b780f864SAdrian Chadd 	RUM_LOCK_ASSERT(sc);
2393b780f864SAdrian Chadd 
2394b780f864SAdrian Chadd 	if (m == NULL)
2395aa81f853SAdrian Chadd 		return EINVAL;
2396bb571462SHans Petter Selasky 	if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2397aa81f853SAdrian Chadd 		return EINVAL;
2398f620e64dSKevin Lo 
239902ac6454SAndrew Thompson 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2400*2cb9ef8dSAdrian Chadd 	rum_setup_tx_desc(sc, &desc, NULL, RT2573_TX_TIMESTAMP,
2401*2cb9ef8dSAdrian Chadd 	    RT2573_TX_HWSEQ, 0, m->m_pkthdr.len, tp->mgmtrate);
240202ac6454SAndrew Thompson 
2403b780f864SAdrian Chadd 	/* copy the Tx descriptor into NIC memory */
2404b780f864SAdrian Chadd 	if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0), (uint8_t *)&desc,
2405b780f864SAdrian Chadd 	    RT2573_TX_DESC_SIZE) != 0)
2406b780f864SAdrian Chadd 		return EIO;
240702ac6454SAndrew Thompson 
240802ac6454SAndrew Thompson 	/* copy beacon header and payload into NIC memory */
2409b780f864SAdrian Chadd 	if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0) + RT2573_TX_DESC_SIZE,
2410b780f864SAdrian Chadd 	    mtod(m, uint8_t *), m->m_pkthdr.len) != 0)
2411b780f864SAdrian Chadd 		return EIO;
241202ac6454SAndrew Thompson 
2413b780f864SAdrian Chadd 	return 0;
2414b780f864SAdrian Chadd }
2415b780f864SAdrian Chadd 
2416b780f864SAdrian Chadd static int
2417b780f864SAdrian Chadd rum_alloc_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2418b780f864SAdrian Chadd {
2419b780f864SAdrian Chadd 	struct rum_vap *rvp = RUM_VAP(vap);
2420b780f864SAdrian Chadd 	struct ieee80211_node *ni = vap->iv_bss;
2421b780f864SAdrian Chadd 	struct mbuf *m;
2422b780f864SAdrian Chadd 
2423b780f864SAdrian Chadd 	if (ni->ni_chan == IEEE80211_CHAN_ANYC)
2424b780f864SAdrian Chadd 		return EINVAL;
2425b780f864SAdrian Chadd 
2426b780f864SAdrian Chadd 	m = ieee80211_beacon_alloc(ni, &vap->iv_bcn_off);
2427b780f864SAdrian Chadd 	if (m == NULL)
2428b780f864SAdrian Chadd 		return ENOMEM;
2429b780f864SAdrian Chadd 
2430b780f864SAdrian Chadd 	if (rvp->bcn_mbuf != NULL)
2431b780f864SAdrian Chadd 		m_freem(rvp->bcn_mbuf);
2432b780f864SAdrian Chadd 
2433b780f864SAdrian Chadd 	rvp->bcn_mbuf = m;
2434b780f864SAdrian Chadd 
2435b780f864SAdrian Chadd 	return (rum_set_beacon(sc, vap));
243602ac6454SAndrew Thompson }
243702ac6454SAndrew Thompson 
24380e3b4c60SAdrian Chadd static void
24390e3b4c60SAdrian Chadd rum_update_beacon_cb(struct rum_softc *sc, union sec_param *data,
24400e3b4c60SAdrian Chadd     uint8_t rn_id, uint8_t rvp_id)
24410e3b4c60SAdrian Chadd {
24420e3b4c60SAdrian Chadd 	struct ieee80211vap *vap = data->vap;
24430e3b4c60SAdrian Chadd 
24440e3b4c60SAdrian Chadd 	rum_set_beacon(sc, vap);
24450e3b4c60SAdrian Chadd }
24460e3b4c60SAdrian Chadd 
24470e3b4c60SAdrian Chadd static void
24480e3b4c60SAdrian Chadd rum_update_beacon(struct ieee80211vap *vap, int item)
24490e3b4c60SAdrian Chadd {
24500e3b4c60SAdrian Chadd 	struct ieee80211com *ic = vap->iv_ic;
24510e3b4c60SAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
24520e3b4c60SAdrian Chadd 	struct rum_vap *rvp = RUM_VAP(vap);
24530e3b4c60SAdrian Chadd 	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
24540e3b4c60SAdrian Chadd 	struct ieee80211_node *ni = vap->iv_bss;
24550e3b4c60SAdrian Chadd 	struct mbuf *m = rvp->bcn_mbuf;
24560e3b4c60SAdrian Chadd 	int mcast = 0;
24570e3b4c60SAdrian Chadd 
24580e3b4c60SAdrian Chadd 	RUM_LOCK(sc);
24590e3b4c60SAdrian Chadd 	if (m == NULL) {
24600e3b4c60SAdrian Chadd 		m = ieee80211_beacon_alloc(ni, bo);
24610e3b4c60SAdrian Chadd 		if (m == NULL) {
24620e3b4c60SAdrian Chadd 			device_printf(sc->sc_dev,
24630e3b4c60SAdrian Chadd 			    "%s: could not allocate beacon frame\n", __func__);
24640e3b4c60SAdrian Chadd 			RUM_UNLOCK(sc);
24650e3b4c60SAdrian Chadd 			return;
24660e3b4c60SAdrian Chadd 		}
24670e3b4c60SAdrian Chadd 		rvp->bcn_mbuf = m;
24680e3b4c60SAdrian Chadd 	}
24690e3b4c60SAdrian Chadd 
24700e3b4c60SAdrian Chadd 	switch (item) {
24710e3b4c60SAdrian Chadd 	case IEEE80211_BEACON_ERP:
24720e3b4c60SAdrian Chadd 		rum_update_slot(ic);
24730e3b4c60SAdrian Chadd 		break;
24740e3b4c60SAdrian Chadd 	case IEEE80211_BEACON_TIM:
24750e3b4c60SAdrian Chadd 		mcast = 1;	/*TODO*/
24760e3b4c60SAdrian Chadd 		break;
24770e3b4c60SAdrian Chadd 	default:
24780e3b4c60SAdrian Chadd 		break;
24790e3b4c60SAdrian Chadd 	}
24800e3b4c60SAdrian Chadd 	RUM_UNLOCK(sc);
24810e3b4c60SAdrian Chadd 
24820e3b4c60SAdrian Chadd 	setbit(bo->bo_flags, item);
24830e3b4c60SAdrian Chadd 	ieee80211_beacon_update(ni, bo, m, mcast);
24840e3b4c60SAdrian Chadd 
24850e3b4c60SAdrian Chadd 	rum_cmd_sleepable(sc, &vap, sizeof(vap), 0, 0, rum_update_beacon_cb);
24860e3b4c60SAdrian Chadd }
24870e3b4c60SAdrian Chadd 
248802ac6454SAndrew Thompson static int
2489*2cb9ef8dSAdrian Chadd rum_common_key_set(struct rum_softc *sc, struct ieee80211_key *k,
2490*2cb9ef8dSAdrian Chadd     uint16_t base)
2491*2cb9ef8dSAdrian Chadd {
2492*2cb9ef8dSAdrian Chadd 
2493*2cb9ef8dSAdrian Chadd 	if (rum_write_multi(sc, base, k->wk_key, k->wk_keylen))
2494*2cb9ef8dSAdrian Chadd 		return EIO;
2495*2cb9ef8dSAdrian Chadd 
2496*2cb9ef8dSAdrian Chadd 	if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2497*2cb9ef8dSAdrian Chadd 		if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE,
2498*2cb9ef8dSAdrian Chadd 		    k->wk_txmic, 8))
2499*2cb9ef8dSAdrian Chadd 			return EIO;
2500*2cb9ef8dSAdrian Chadd 		if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE + 8,
2501*2cb9ef8dSAdrian Chadd 		    k->wk_rxmic, 8))
2502*2cb9ef8dSAdrian Chadd 			return EIO;
2503*2cb9ef8dSAdrian Chadd 	}
2504*2cb9ef8dSAdrian Chadd 
2505*2cb9ef8dSAdrian Chadd 	return 0;
2506*2cb9ef8dSAdrian Chadd }
2507*2cb9ef8dSAdrian Chadd 
2508*2cb9ef8dSAdrian Chadd static void
2509*2cb9ef8dSAdrian Chadd rum_group_key_set_cb(struct rum_softc *sc, union sec_param *data,
2510*2cb9ef8dSAdrian Chadd     uint8_t rn_id, uint8_t rvp_id)
2511*2cb9ef8dSAdrian Chadd {
2512*2cb9ef8dSAdrian Chadd 	struct ieee80211_key *k = &data->key;
2513*2cb9ef8dSAdrian Chadd 	uint8_t mode;
2514*2cb9ef8dSAdrian Chadd 
2515*2cb9ef8dSAdrian Chadd 	if (sc->sc_clr_shkeys == 0) {
2516*2cb9ef8dSAdrian Chadd 		rum_clr_shkey_regs(sc);
2517*2cb9ef8dSAdrian Chadd 		sc->sc_clr_shkeys = 1;
2518*2cb9ef8dSAdrian Chadd 	}
2519*2cb9ef8dSAdrian Chadd 
2520*2cb9ef8dSAdrian Chadd 	mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2521*2cb9ef8dSAdrian Chadd 	if (mode == 0)
2522*2cb9ef8dSAdrian Chadd 		goto print_err;
2523*2cb9ef8dSAdrian Chadd 
2524*2cb9ef8dSAdrian Chadd 	DPRINTFN(1, "setting group key %d for vap %d, mode %d "
2525*2cb9ef8dSAdrian Chadd 	    "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2526*2cb9ef8dSAdrian Chadd 	    (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2527*2cb9ef8dSAdrian Chadd 	    (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2528*2cb9ef8dSAdrian Chadd 
2529*2cb9ef8dSAdrian Chadd 	/* Install the key. */
2530*2cb9ef8dSAdrian Chadd 	if (rum_common_key_set(sc, k, RT2573_SKEY(rvp_id, k->wk_keyix)) != 0)
2531*2cb9ef8dSAdrian Chadd 		goto print_err;
2532*2cb9ef8dSAdrian Chadd 
2533*2cb9ef8dSAdrian Chadd 	/* Set cipher mode. */
2534*2cb9ef8dSAdrian Chadd 	if (rum_modbits(sc, rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2535*2cb9ef8dSAdrian Chadd 	      mode << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX,
2536*2cb9ef8dSAdrian Chadd 	      RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX)
2537*2cb9ef8dSAdrian Chadd 	    != 0)
2538*2cb9ef8dSAdrian Chadd 		goto print_err;
2539*2cb9ef8dSAdrian Chadd 
2540*2cb9ef8dSAdrian Chadd 	/* Mark this key as valid. */
2541*2cb9ef8dSAdrian Chadd 	if (rum_setbits(sc, RT2573_SEC_CSR0,
2542*2cb9ef8dSAdrian Chadd 	      1 << (rvp_id * RT2573_SKEY_MAX + k->wk_keyix)) != 0)
2543*2cb9ef8dSAdrian Chadd 		goto print_err;
2544*2cb9ef8dSAdrian Chadd 
2545*2cb9ef8dSAdrian Chadd 	return;
2546*2cb9ef8dSAdrian Chadd 
2547*2cb9ef8dSAdrian Chadd print_err:
2548*2cb9ef8dSAdrian Chadd 	device_printf(sc->sc_dev, "%s: cannot set group key %d for vap %d\n",
2549*2cb9ef8dSAdrian Chadd 	    __func__, k->wk_keyix, rvp_id);
2550*2cb9ef8dSAdrian Chadd }
2551*2cb9ef8dSAdrian Chadd 
2552*2cb9ef8dSAdrian Chadd static void
2553*2cb9ef8dSAdrian Chadd rum_group_key_del_cb(struct rum_softc *sc, union sec_param *data,
2554*2cb9ef8dSAdrian Chadd     uint8_t rn_id, uint8_t rvp_id)
2555*2cb9ef8dSAdrian Chadd {
2556*2cb9ef8dSAdrian Chadd 	struct ieee80211_key *k = &data->key;
2557*2cb9ef8dSAdrian Chadd 
2558*2cb9ef8dSAdrian Chadd 	DPRINTF("%s: removing group key %d for vap %d\n", __func__,
2559*2cb9ef8dSAdrian Chadd 	    k->wk_keyix, rvp_id);
2560*2cb9ef8dSAdrian Chadd 	rum_clrbits(sc,
2561*2cb9ef8dSAdrian Chadd 	    rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2562*2cb9ef8dSAdrian Chadd 	    RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX);
2563*2cb9ef8dSAdrian Chadd 	rum_clrbits(sc, RT2573_SEC_CSR0,
2564*2cb9ef8dSAdrian Chadd 	    rvp_id * RT2573_SKEY_MAX + k->wk_keyix);
2565*2cb9ef8dSAdrian Chadd }
2566*2cb9ef8dSAdrian Chadd 
2567*2cb9ef8dSAdrian Chadd static void
2568*2cb9ef8dSAdrian Chadd rum_pair_key_set_cb(struct rum_softc *sc, union sec_param *data, uint8_t rn_id,
2569*2cb9ef8dSAdrian Chadd     uint8_t rvp_id)
2570*2cb9ef8dSAdrian Chadd {
2571*2cb9ef8dSAdrian Chadd 	struct ieee80211_key *k = &data->key;
2572*2cb9ef8dSAdrian Chadd 	uint8_t buf[IEEE80211_ADDR_LEN + 1];
2573*2cb9ef8dSAdrian Chadd 	uint8_t mode;
2574*2cb9ef8dSAdrian Chadd 
2575*2cb9ef8dSAdrian Chadd 	mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2576*2cb9ef8dSAdrian Chadd 	if (mode == 0)
2577*2cb9ef8dSAdrian Chadd 		goto print_err;
2578*2cb9ef8dSAdrian Chadd 
2579*2cb9ef8dSAdrian Chadd 	DPRINTFN(1, "setting pairwise key %d for vap %d, mode %d "
2580*2cb9ef8dSAdrian Chadd 	    "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2581*2cb9ef8dSAdrian Chadd 	    (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2582*2cb9ef8dSAdrian Chadd 	    (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2583*2cb9ef8dSAdrian Chadd 
2584*2cb9ef8dSAdrian Chadd 	/* Install the key. */
2585*2cb9ef8dSAdrian Chadd 	if (rum_common_key_set(sc, k, RT2573_PKEY(k->wk_keyix)) != 0)
2586*2cb9ef8dSAdrian Chadd 		goto print_err;
2587*2cb9ef8dSAdrian Chadd 
2588*2cb9ef8dSAdrian Chadd 	IEEE80211_ADDR_COPY(buf, k->wk_macaddr);
2589*2cb9ef8dSAdrian Chadd 	buf[IEEE80211_ADDR_LEN] = mode;
2590*2cb9ef8dSAdrian Chadd 
2591*2cb9ef8dSAdrian Chadd 	/* Set transmitter address and cipher mode. */
2592*2cb9ef8dSAdrian Chadd 	if (rum_write_multi(sc, RT2573_ADDR_ENTRY(k->wk_keyix),
2593*2cb9ef8dSAdrian Chadd 	      buf, sizeof buf) != 0)
2594*2cb9ef8dSAdrian Chadd 		goto print_err;
2595*2cb9ef8dSAdrian Chadd 
2596*2cb9ef8dSAdrian Chadd 	/* Enable key table lookup for this vap. */
2597*2cb9ef8dSAdrian Chadd 	if (sc->vap_key_count[rvp_id]++ == 0)
2598*2cb9ef8dSAdrian Chadd 		if (rum_setbits(sc, RT2573_SEC_CSR4, 1 << rvp_id) != 0)
2599*2cb9ef8dSAdrian Chadd 			goto print_err;
2600*2cb9ef8dSAdrian Chadd 
2601*2cb9ef8dSAdrian Chadd 	/* Mark this key as valid. */
2602*2cb9ef8dSAdrian Chadd 	if (rum_setbits(sc,
2603*2cb9ef8dSAdrian Chadd 	      k->wk_keyix < 32 ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
2604*2cb9ef8dSAdrian Chadd 	      1 << (k->wk_keyix % 32)) != 0)
2605*2cb9ef8dSAdrian Chadd 		goto print_err;
2606*2cb9ef8dSAdrian Chadd 
2607*2cb9ef8dSAdrian Chadd 	return;
2608*2cb9ef8dSAdrian Chadd 
2609*2cb9ef8dSAdrian Chadd print_err:
2610*2cb9ef8dSAdrian Chadd 	device_printf(sc->sc_dev,
2611*2cb9ef8dSAdrian Chadd 	    "%s: cannot set pairwise key %d, vap %d\n", __func__, k->wk_keyix,
2612*2cb9ef8dSAdrian Chadd 	    rvp_id);
2613*2cb9ef8dSAdrian Chadd }
2614*2cb9ef8dSAdrian Chadd 
2615*2cb9ef8dSAdrian Chadd static void
2616*2cb9ef8dSAdrian Chadd rum_pair_key_del_cb(struct rum_softc *sc, union sec_param *data, uint8_t rn_id,
2617*2cb9ef8dSAdrian Chadd     uint8_t rvp_id)
2618*2cb9ef8dSAdrian Chadd {
2619*2cb9ef8dSAdrian Chadd 	struct ieee80211_key *k = &data->key;
2620*2cb9ef8dSAdrian Chadd 
2621*2cb9ef8dSAdrian Chadd 	DPRINTF("%s: removing key %d\n", __func__, k->wk_keyix);
2622*2cb9ef8dSAdrian Chadd 	rum_clrbits(sc, (k->wk_keyix < 32) ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
2623*2cb9ef8dSAdrian Chadd 	    1 << (k->wk_keyix % 32));
2624*2cb9ef8dSAdrian Chadd 	sc->keys_bmap &= ~(1 << k->wk_keyix);
2625*2cb9ef8dSAdrian Chadd 	if (--sc->vap_key_count[rvp_id] == 0)
2626*2cb9ef8dSAdrian Chadd 		rum_clrbits(sc, RT2573_SEC_CSR4, 1 << rvp_id);
2627*2cb9ef8dSAdrian Chadd }
2628*2cb9ef8dSAdrian Chadd 
2629*2cb9ef8dSAdrian Chadd static int
2630*2cb9ef8dSAdrian Chadd rum_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
2631*2cb9ef8dSAdrian Chadd     ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
2632*2cb9ef8dSAdrian Chadd {
2633*2cb9ef8dSAdrian Chadd 	struct rum_softc *sc = vap->iv_ic->ic_softc;
2634*2cb9ef8dSAdrian Chadd 	uint8_t i;
2635*2cb9ef8dSAdrian Chadd 
2636*2cb9ef8dSAdrian Chadd 	if (!(&vap->iv_nw_keys[0] <= k &&
2637*2cb9ef8dSAdrian Chadd 	     k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
2638*2cb9ef8dSAdrian Chadd 		if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
2639*2cb9ef8dSAdrian Chadd 			RUM_LOCK(sc);
2640*2cb9ef8dSAdrian Chadd 			for (i = 0; i < RT2573_ADDR_MAX; i++) {
2641*2cb9ef8dSAdrian Chadd 				if ((sc->keys_bmap & (1 << i)) == 0) {
2642*2cb9ef8dSAdrian Chadd 					sc->keys_bmap |= 1 << i;
2643*2cb9ef8dSAdrian Chadd 					*keyix = i;
2644*2cb9ef8dSAdrian Chadd 					break;
2645*2cb9ef8dSAdrian Chadd 				}
2646*2cb9ef8dSAdrian Chadd 			}
2647*2cb9ef8dSAdrian Chadd 			RUM_UNLOCK(sc);
2648*2cb9ef8dSAdrian Chadd 			if (i == RT2573_ADDR_MAX) {
2649*2cb9ef8dSAdrian Chadd 				device_printf(sc->sc_dev,
2650*2cb9ef8dSAdrian Chadd 				    "%s: no free space in the key table\n",
2651*2cb9ef8dSAdrian Chadd 				    __func__);
2652*2cb9ef8dSAdrian Chadd 				return 0;
2653*2cb9ef8dSAdrian Chadd 			}
2654*2cb9ef8dSAdrian Chadd 		} else
2655*2cb9ef8dSAdrian Chadd 			*keyix = 0;
2656*2cb9ef8dSAdrian Chadd 	} else {
2657*2cb9ef8dSAdrian Chadd 		*keyix = k - vap->iv_nw_keys;
2658*2cb9ef8dSAdrian Chadd 	}
2659*2cb9ef8dSAdrian Chadd 	*rxkeyix = *keyix;
2660*2cb9ef8dSAdrian Chadd 	return 1;
2661*2cb9ef8dSAdrian Chadd }
2662*2cb9ef8dSAdrian Chadd 
2663*2cb9ef8dSAdrian Chadd static int
2664*2cb9ef8dSAdrian Chadd rum_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k,
2665*2cb9ef8dSAdrian Chadd     const uint8_t mac[IEEE80211_ADDR_LEN])
2666*2cb9ef8dSAdrian Chadd {
2667*2cb9ef8dSAdrian Chadd 	struct rum_softc *sc = vap->iv_ic->ic_softc;
2668*2cb9ef8dSAdrian Chadd 	int group;
2669*2cb9ef8dSAdrian Chadd 
2670*2cb9ef8dSAdrian Chadd 	if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
2671*2cb9ef8dSAdrian Chadd 		/* Not for us. */
2672*2cb9ef8dSAdrian Chadd 		return 1;
2673*2cb9ef8dSAdrian Chadd 	}
2674*2cb9ef8dSAdrian Chadd 
2675*2cb9ef8dSAdrian Chadd 	group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
2676*2cb9ef8dSAdrian Chadd 
2677*2cb9ef8dSAdrian Chadd 	return !rum_cmd_sleepable(sc, k, sizeof(*k), 0, 0,
2678*2cb9ef8dSAdrian Chadd 		   group ? rum_group_key_set_cb : rum_pair_key_set_cb);
2679*2cb9ef8dSAdrian Chadd }
2680*2cb9ef8dSAdrian Chadd 
2681*2cb9ef8dSAdrian Chadd static int
2682*2cb9ef8dSAdrian Chadd rum_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
2683*2cb9ef8dSAdrian Chadd {
2684*2cb9ef8dSAdrian Chadd 	struct rum_softc *sc = vap->iv_ic->ic_softc;
2685*2cb9ef8dSAdrian Chadd 	int group;
2686*2cb9ef8dSAdrian Chadd 
2687*2cb9ef8dSAdrian Chadd 	if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
2688*2cb9ef8dSAdrian Chadd 		/* Not for us. */
2689*2cb9ef8dSAdrian Chadd 		return 1;
2690*2cb9ef8dSAdrian Chadd 	}
2691*2cb9ef8dSAdrian Chadd 
2692*2cb9ef8dSAdrian Chadd 	group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
2693*2cb9ef8dSAdrian Chadd 
2694*2cb9ef8dSAdrian Chadd 	return !rum_cmd_sleepable(sc, k, sizeof(*k), 0, 0,
2695*2cb9ef8dSAdrian Chadd 		   group ? rum_group_key_del_cb : rum_pair_key_del_cb);
2696*2cb9ef8dSAdrian Chadd }
2697*2cb9ef8dSAdrian Chadd 
2698*2cb9ef8dSAdrian Chadd static int
269902ac6454SAndrew Thompson rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
270002ac6454SAndrew Thompson     const struct ieee80211_bpf_params *params)
270102ac6454SAndrew Thompson {
2702d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ni->ni_ic->ic_softc;
2703b2a76342SAdrian Chadd 	int ret;
270402ac6454SAndrew Thompson 
270502ac6454SAndrew Thompson 	RUM_LOCK(sc);
270602ac6454SAndrew Thompson 	/* prevent management frames from being sent if we're not ready */
27077a79cebfSGleb Smirnoff 	if (!sc->sc_running) {
2708b2a76342SAdrian Chadd 		ret = ENETDOWN;
2709b2a76342SAdrian Chadd 		goto bad;
271002ac6454SAndrew Thompson 	}
271196ca458fSAndrew Thompson 	if (sc->tx_nfree < RUM_TX_MINFREE) {
2712b2a76342SAdrian Chadd 		ret = EIO;
2713b2a76342SAdrian Chadd 		goto bad;
271402ac6454SAndrew Thompson 	}
271502ac6454SAndrew Thompson 
271602ac6454SAndrew Thompson 	if (params == NULL) {
271702ac6454SAndrew Thompson 		/*
271802ac6454SAndrew Thompson 		 * Legacy path; interpret frame contents to decide
271902ac6454SAndrew Thompson 		 * precisely how to send the frame.
272002ac6454SAndrew Thompson 		 */
2721b2a76342SAdrian Chadd 		if ((ret = rum_tx_mgt(sc, m, ni)) != 0)
272202ac6454SAndrew Thompson 			goto bad;
272302ac6454SAndrew Thompson 	} else {
272402ac6454SAndrew Thompson 		/*
272502ac6454SAndrew Thompson 		 * Caller supplied explicit parameters to use in
272602ac6454SAndrew Thompson 		 * sending the frame.
272702ac6454SAndrew Thompson 		 */
2728b2a76342SAdrian Chadd 		if ((ret = rum_tx_raw(sc, m, ni, params)) != 0)
272902ac6454SAndrew Thompson 			goto bad;
273002ac6454SAndrew Thompson 	}
273102ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
273202ac6454SAndrew Thompson 
273302ac6454SAndrew Thompson 	return 0;
273402ac6454SAndrew Thompson bad:
273502ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
2736b2a76342SAdrian Chadd 	m_freem(m);
273702ac6454SAndrew Thompson 	ieee80211_free_node(ni);
2738b2a76342SAdrian Chadd 	return ret;
273902ac6454SAndrew Thompson }
274002ac6454SAndrew Thompson 
274102ac6454SAndrew Thompson static void
2742b6108616SRui Paulo rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
274302ac6454SAndrew Thompson {
274402ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
274502ac6454SAndrew Thompson 	struct rum_vap *rvp = RUM_VAP(vap);
274602ac6454SAndrew Thompson 
274702ac6454SAndrew Thompson 	/* clear statistic registers (STA_CSR0 to STA_CSR5) */
274802ac6454SAndrew Thompson 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
274902ac6454SAndrew Thompson 
2750b6108616SRui Paulo 	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
275102ac6454SAndrew Thompson }
275202ac6454SAndrew Thompson 
275302ac6454SAndrew Thompson static void
2754b6108616SRui Paulo rum_ratectl_timeout(void *arg)
275502ac6454SAndrew Thompson {
275602ac6454SAndrew Thompson 	struct rum_vap *rvp = arg;
27575efea30fSAndrew Thompson 	struct ieee80211vap *vap = &rvp->vap;
27585efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
275902ac6454SAndrew Thompson 
2760b6108616SRui Paulo 	ieee80211_runtask(ic, &rvp->ratectl_task);
276102ac6454SAndrew Thompson }
276202ac6454SAndrew Thompson 
276302ac6454SAndrew Thompson static void
2764b6108616SRui Paulo rum_ratectl_task(void *arg, int pending)
276502ac6454SAndrew Thompson {
27665efea30fSAndrew Thompson 	struct rum_vap *rvp = arg;
27675efea30fSAndrew Thompson 	struct ieee80211vap *vap = &rvp->vap;
27685efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
2769d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
27708d8bdb01SAndrew Thompson 	struct ieee80211_node *ni;
277102ac6454SAndrew Thompson 	int ok, fail;
2772b6108616SRui Paulo 	int sum, retrycnt;
277302ac6454SAndrew Thompson 
27745efea30fSAndrew Thompson 	RUM_LOCK(sc);
277502ac6454SAndrew Thompson 	/* read and clear statistic registers (STA_CSR0 to STA_CSR10) */
277602ac6454SAndrew Thompson 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
277702ac6454SAndrew Thompson 
277802ac6454SAndrew Thompson 	ok = (le32toh(sc->sta[4]) >> 16) +	/* TX ok w/o retry */
277902ac6454SAndrew Thompson 	    (le32toh(sc->sta[5]) & 0xffff);	/* TX ok w/ retry */
278002ac6454SAndrew Thompson 	fail = (le32toh(sc->sta[5]) >> 16);	/* TX retry-fail count */
2781b6108616SRui Paulo 	sum = ok+fail;
2782b6108616SRui Paulo 	retrycnt = (le32toh(sc->sta[5]) & 0xffff) + fail;
278302ac6454SAndrew Thompson 
27848d8bdb01SAndrew Thompson 	ni = ieee80211_ref_node(vap->iv_bss);
2785b6108616SRui Paulo 	ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt);
2786b6108616SRui Paulo 	(void) ieee80211_ratectl_rate(ni, NULL, 0);
27878d8bdb01SAndrew Thompson 	ieee80211_free_node(ni);
278802ac6454SAndrew Thompson 
27897a79cebfSGleb Smirnoff 	/* count TX retry-fail as Tx errors */
27907a79cebfSGleb Smirnoff 	if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, fail);
279102ac6454SAndrew Thompson 
2792b6108616SRui Paulo 	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
27935efea30fSAndrew Thompson 	RUM_UNLOCK(sc);
279402ac6454SAndrew Thompson }
279502ac6454SAndrew Thompson 
279602ac6454SAndrew Thompson static void
279702ac6454SAndrew Thompson rum_scan_start(struct ieee80211com *ic)
279802ac6454SAndrew Thompson {
2799d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
280002ac6454SAndrew Thompson 
280102ac6454SAndrew Thompson 	RUM_LOCK(sc);
28026d804321SAdrian Chadd 	rum_abort_tsf_sync(sc);
28037a79cebfSGleb Smirnoff 	rum_set_bssid(sc, ieee80211broadcastaddr);
280402ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
280502ac6454SAndrew Thompson 
280602ac6454SAndrew Thompson }
280702ac6454SAndrew Thompson 
280802ac6454SAndrew Thompson static void
280902ac6454SAndrew Thompson rum_scan_end(struct ieee80211com *ic)
281002ac6454SAndrew Thompson {
2811d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
281202ac6454SAndrew Thompson 
281302ac6454SAndrew Thompson 	RUM_LOCK(sc);
281450a31b48SAdrian Chadd 	if (ic->ic_opmode != IEEE80211_M_AHDEMO)
28155efea30fSAndrew Thompson 		rum_enable_tsf_sync(sc);
281650a31b48SAdrian Chadd 	else
281750a31b48SAdrian Chadd 		rum_enable_tsf(sc);
28181ba67b11SAdrian Chadd 	rum_set_bssid(sc, sc->sc_bssid);
281902ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
282002ac6454SAndrew Thompson 
282102ac6454SAndrew Thompson }
282202ac6454SAndrew Thompson 
282302ac6454SAndrew Thompson static void
282402ac6454SAndrew Thompson rum_set_channel(struct ieee80211com *ic)
282502ac6454SAndrew Thompson {
2826d3fdd08cSAdrian Chadd 	struct rum_softc *sc = ic->ic_softc;
282702ac6454SAndrew Thompson 
282802ac6454SAndrew Thompson 	RUM_LOCK(sc);
282902ac6454SAndrew Thompson 	rum_set_chan(sc, ic->ic_curchan);
28305efea30fSAndrew Thompson 	RUM_UNLOCK(sc);
283102ac6454SAndrew Thompson }
283202ac6454SAndrew Thompson 
283302ac6454SAndrew Thompson static int
283402ac6454SAndrew Thompson rum_get_rssi(struct rum_softc *sc, uint8_t raw)
283502ac6454SAndrew Thompson {
28367a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
283702ac6454SAndrew Thompson 	int lna, agc, rssi;
283802ac6454SAndrew Thompson 
283902ac6454SAndrew Thompson 	lna = (raw >> 5) & 0x3;
284002ac6454SAndrew Thompson 	agc = raw & 0x1f;
284102ac6454SAndrew Thompson 
284202ac6454SAndrew Thompson 	if (lna == 0) {
284302ac6454SAndrew Thompson 		/*
284402ac6454SAndrew Thompson 		 * No RSSI mapping
284502ac6454SAndrew Thompson 		 *
284602ac6454SAndrew Thompson 		 * NB: Since RSSI is relative to noise floor, -1 is
284702ac6454SAndrew Thompson 		 *     adequate for caller to know error happened.
284802ac6454SAndrew Thompson 		 */
284902ac6454SAndrew Thompson 		return -1;
285002ac6454SAndrew Thompson 	}
285102ac6454SAndrew Thompson 
285202ac6454SAndrew Thompson 	rssi = (2 * agc) - RT2573_NOISE_FLOOR;
285302ac6454SAndrew Thompson 
285402ac6454SAndrew Thompson 	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
285502ac6454SAndrew Thompson 		rssi += sc->rssi_2ghz_corr;
285602ac6454SAndrew Thompson 
285702ac6454SAndrew Thompson 		if (lna == 1)
285802ac6454SAndrew Thompson 			rssi -= 64;
285902ac6454SAndrew Thompson 		else if (lna == 2)
286002ac6454SAndrew Thompson 			rssi -= 74;
286102ac6454SAndrew Thompson 		else if (lna == 3)
286202ac6454SAndrew Thompson 			rssi -= 90;
286302ac6454SAndrew Thompson 	} else {
286402ac6454SAndrew Thompson 		rssi += sc->rssi_5ghz_corr;
286502ac6454SAndrew Thompson 
286602ac6454SAndrew Thompson 		if (!sc->ext_5ghz_lna && lna != 1)
286702ac6454SAndrew Thompson 			rssi += 4;
286802ac6454SAndrew Thompson 
286902ac6454SAndrew Thompson 		if (lna == 1)
287002ac6454SAndrew Thompson 			rssi -= 64;
287102ac6454SAndrew Thompson 		else if (lna == 2)
287202ac6454SAndrew Thompson 			rssi -= 86;
287302ac6454SAndrew Thompson 		else if (lna == 3)
287402ac6454SAndrew Thompson 			rssi -= 100;
287502ac6454SAndrew Thompson 	}
287602ac6454SAndrew Thompson 	return rssi;
287702ac6454SAndrew Thompson }
287802ac6454SAndrew Thompson 
287902ac6454SAndrew Thompson static int
288002ac6454SAndrew Thompson rum_pause(struct rum_softc *sc, int timeout)
288102ac6454SAndrew Thompson {
288202ac6454SAndrew Thompson 
2883a593f6b8SAndrew Thompson 	usb_pause_mtx(&sc->sc_mtx, timeout);
288402ac6454SAndrew Thompson 	return (0);
288502ac6454SAndrew Thompson }
288602ac6454SAndrew Thompson 
288702ac6454SAndrew Thompson static device_method_t rum_methods[] = {
288802ac6454SAndrew Thompson 	/* Device interface */
288902ac6454SAndrew Thompson 	DEVMETHOD(device_probe,		rum_match),
289002ac6454SAndrew Thompson 	DEVMETHOD(device_attach,	rum_attach),
289102ac6454SAndrew Thompson 	DEVMETHOD(device_detach,	rum_detach),
2892645e4d17SHans Petter Selasky 	DEVMETHOD_END
289302ac6454SAndrew Thompson };
289402ac6454SAndrew Thompson 
289502ac6454SAndrew Thompson static driver_t rum_driver = {
289602ac6454SAndrew Thompson 	.name = "rum",
289702ac6454SAndrew Thompson 	.methods = rum_methods,
289802ac6454SAndrew Thompson 	.size = sizeof(struct rum_softc),
289902ac6454SAndrew Thompson };
290002ac6454SAndrew Thompson 
290102ac6454SAndrew Thompson static devclass_t rum_devclass;
290202ac6454SAndrew Thompson 
29039aef556dSAndrew Thompson DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
2904910cb8feSAndrew Thompson MODULE_DEPEND(rum, wlan, 1, 1, 1);
2905910cb8feSAndrew Thompson MODULE_DEPEND(rum, usb, 1, 1, 1);
2906910cb8feSAndrew Thompson MODULE_VERSION(rum, 1);
2907