xref: /freebsd/sys/dev/usb/wlan/if_rum.c (revision 5efea30f039c48ddcdddc0d12150b656f4a2e7b8)
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>
455efea30fSAndrew Thompson #include <machine/resource.h>
465efea30fSAndrew Thompson #include <sys/rman.h>
475efea30fSAndrew Thompson 
485efea30fSAndrew Thompson #include <net/bpf.h>
495efea30fSAndrew Thompson #include <net/if.h>
505efea30fSAndrew Thompson #include <net/if_arp.h>
515efea30fSAndrew Thompson #include <net/ethernet.h>
525efea30fSAndrew Thompson #include <net/if_dl.h>
535efea30fSAndrew Thompson #include <net/if_media.h>
545efea30fSAndrew Thompson #include <net/if_types.h>
555efea30fSAndrew Thompson 
565efea30fSAndrew Thompson #ifdef INET
575efea30fSAndrew Thompson #include <netinet/in.h>
585efea30fSAndrew Thompson #include <netinet/in_systm.h>
595efea30fSAndrew Thompson #include <netinet/in_var.h>
605efea30fSAndrew Thompson #include <netinet/if_ether.h>
615efea30fSAndrew Thompson #include <netinet/ip.h>
625efea30fSAndrew Thompson #endif
635efea30fSAndrew Thompson 
645efea30fSAndrew Thompson #include <net80211/ieee80211_var.h>
655efea30fSAndrew Thompson #include <net80211/ieee80211_regdomain.h>
665efea30fSAndrew Thompson #include <net80211/ieee80211_radiotap.h>
675efea30fSAndrew Thompson #include <net80211/ieee80211_amrr.h>
6802ac6454SAndrew Thompson 
6902ac6454SAndrew Thompson #define	USB_DEBUG_VAR rum_debug
7002ac6454SAndrew Thompson 
715efea30fSAndrew Thompson #include <dev/usb/usb.h>
725efea30fSAndrew Thompson #include <dev/usb/usb_error.h>
7302ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
7402ac6454SAndrew Thompson #include <dev/usb/usb_lookup.h>
7502ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
7602ac6454SAndrew Thompson #include <dev/usb/usb_request.h>
7702ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
7802ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
795efea30fSAndrew Thompson #include "usbdevs.h"
8002ac6454SAndrew Thompson 
8102ac6454SAndrew Thompson #include <dev/usb/wlan/if_rumreg.h>
8202ac6454SAndrew Thompson #include <dev/usb/wlan/if_rumvar.h>
8302ac6454SAndrew Thompson #include <dev/usb/wlan/if_rumfw.h>
8402ac6454SAndrew Thompson 
8502ac6454SAndrew Thompson #if USB_DEBUG
8602ac6454SAndrew Thompson static int rum_debug = 0;
8702ac6454SAndrew Thompson 
8802ac6454SAndrew Thompson SYSCTL_NODE(_hw_usb2, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
8902ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2_rum, OID_AUTO, debug, CTLFLAG_RW, &rum_debug, 0,
9002ac6454SAndrew Thompson     "Debug level");
9102ac6454SAndrew Thompson #endif
9202ac6454SAndrew Thompson 
9302ac6454SAndrew Thompson static const struct usb2_device_id rum_devs[] = {
9402ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_HWU54DM) },
9502ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_RT2573_2) },
9602ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_RT2573_3) },
9702ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_RT2573_4) },
9802ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_WUG2700) },
9902ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_AMIT,		USB_PRODUCT_AMIT_CGWLUSB2GO) },
10002ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_ASUS,		USB_PRODUCT_ASUS_RT2573_1) },
10102ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_ASUS,		USB_PRODUCT_ASUS_RT2573_2) },
10202ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_BELKIN,		USB_PRODUCT_BELKIN_F5D7050A) },
10302ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_BELKIN,		USB_PRODUCT_BELKIN_F5D9050V3) },
10402ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_CISCOLINKSYS,	USB_PRODUCT_CISCOLINKSYS_WUSB54GC) },
10502ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_CISCOLINKSYS,	USB_PRODUCT_CISCOLINKSYS_WUSB54GR) },
10602ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_CONCEPTRONIC2,	USB_PRODUCT_CONCEPTRONIC2_C54RU2) },
10702ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_CGWLUSB2GL) },
10802ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_CGWLUSB2GPX) },
10902ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_DICKSMITH,	USB_PRODUCT_DICKSMITH_CWD854F) },
11002ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_DICKSMITH,	USB_PRODUCT_DICKSMITH_RT2573) },
11102ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_DLINK2,		USB_PRODUCT_DLINK2_DWLG122C1) },
11202ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_DLINK2,		USB_PRODUCT_DLINK2_WUA1340) },
11302ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_DLINK2,		USB_PRODUCT_DLINK2_DWA111) },
11402ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_DLINK2,		USB_PRODUCT_DLINK2_DWA110) },
11502ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_GIGABYTE,	USB_PRODUCT_GIGABYTE_GNWB01GS) },
11602ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_GIGABYTE,	USB_PRODUCT_GIGABYTE_GNWI05GS) },
11702ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_GIGASET,	USB_PRODUCT_GIGASET_RT2573) },
11802ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_GOODWAY,	USB_PRODUCT_GOODWAY_RT2573) },
11902ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_GUILLEMOT,	USB_PRODUCT_GUILLEMOT_HWGUSB254LB) },
12002ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_GUILLEMOT,	USB_PRODUCT_GUILLEMOT_HWGUSB254V2AP) },
12102ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_HUAWEI3COM,	USB_PRODUCT_HUAWEI3COM_WUB320G) },
12202ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_G54HP) },
12302ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_SG54HP) },
12402ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_MSI,		USB_PRODUCT_MSI_RT2573_1) },
12502ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_MSI,		USB_PRODUCT_MSI_RT2573_2) },
12602ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_MSI,		USB_PRODUCT_MSI_RT2573_3) },
12702ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_MSI,		USB_PRODUCT_MSI_RT2573_4) },
12802ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_NOVATECH,	USB_PRODUCT_NOVATECH_RT2573) },
12902ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_PLANEX2,	USB_PRODUCT_PLANEX2_GWUS54HP) },
13002ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_PLANEX2,	USB_PRODUCT_PLANEX2_GWUS54MINI2) },
13102ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_PLANEX2,	USB_PRODUCT_PLANEX2_GWUSMM) },
13202ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_QCOM,		USB_PRODUCT_QCOM_RT2573) },
13302ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_QCOM,		USB_PRODUCT_QCOM_RT2573_2) },
1342da462e3SKevin Lo     { USB_VP(USB_VENDOR_QCOM,		USB_PRODUCT_QCOM_RT2573_3) },
13502ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_RALINK,		USB_PRODUCT_RALINK_RT2573) },
13602ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_RALINK,		USB_PRODUCT_RALINK_RT2573_2) },
13702ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_RALINK,		USB_PRODUCT_RALINK_RT2671) },
13802ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_SITECOMEU,	USB_PRODUCT_SITECOMEU_WL113R2) },
13902ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_SITECOMEU,	USB_PRODUCT_SITECOMEU_WL172) },
14002ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_SPARKLAN,	USB_PRODUCT_SPARKLAN_RT2573) },
14102ac6454SAndrew Thompson     { USB_VP(USB_VENDOR_SURECOM,	USB_PRODUCT_SURECOM_RT2573) },
14202ac6454SAndrew Thompson };
14302ac6454SAndrew Thompson 
14402ac6454SAndrew Thompson MODULE_DEPEND(rum, wlan, 1, 1, 1);
14502ac6454SAndrew Thompson MODULE_DEPEND(rum, wlan_amrr, 1, 1, 1);
14602ac6454SAndrew Thompson MODULE_DEPEND(rum, usb, 1, 1, 1);
14702ac6454SAndrew Thompson 
14802ac6454SAndrew Thompson static device_probe_t rum_match;
14902ac6454SAndrew Thompson static device_attach_t rum_attach;
15002ac6454SAndrew Thompson static device_detach_t rum_detach;
15102ac6454SAndrew Thompson 
15202ac6454SAndrew Thompson static usb2_callback_t rum_bulk_read_callback;
15302ac6454SAndrew Thompson static usb2_callback_t rum_bulk_write_callback;
15402ac6454SAndrew Thompson 
15577ddf3d3SAndrew Thompson static usb2_error_t	rum_do_request(struct rum_softc *sc,
15677ddf3d3SAndrew Thompson 			    struct usb2_device_request *req, void *data);
15702ac6454SAndrew Thompson static struct ieee80211vap *rum_vap_create(struct ieee80211com *,
15802ac6454SAndrew Thompson 			    const char name[IFNAMSIZ], int unit, int opmode,
15902ac6454SAndrew Thompson 			    int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
16002ac6454SAndrew Thompson 			    const uint8_t mac[IEEE80211_ADDR_LEN]);
16102ac6454SAndrew Thompson static void		rum_vap_delete(struct ieee80211vap *);
16202ac6454SAndrew Thompson static void		rum_tx_free(struct rum_tx_data *, int);
16302ac6454SAndrew Thompson static void		rum_setup_tx_list(struct rum_softc *);
16402ac6454SAndrew Thompson static void		rum_unsetup_tx_list(struct rum_softc *);
16502ac6454SAndrew Thompson static int		rum_newstate(struct ieee80211vap *,
16602ac6454SAndrew Thompson 			    enum ieee80211_state, int);
16702ac6454SAndrew Thompson static void		rum_setup_tx_desc(struct rum_softc *,
16802ac6454SAndrew Thompson 			    struct rum_tx_desc *, uint32_t, uint16_t, int,
16902ac6454SAndrew Thompson 			    int);
17002ac6454SAndrew Thompson static int		rum_tx_mgt(struct rum_softc *, struct mbuf *,
17102ac6454SAndrew Thompson 			    struct ieee80211_node *);
17202ac6454SAndrew Thompson static int		rum_tx_raw(struct rum_softc *, struct mbuf *,
17302ac6454SAndrew Thompson 			    struct ieee80211_node *,
17402ac6454SAndrew Thompson 			    const struct ieee80211_bpf_params *);
17502ac6454SAndrew Thompson static int		rum_tx_data(struct rum_softc *, struct mbuf *,
17602ac6454SAndrew Thompson 			    struct ieee80211_node *);
17702ac6454SAndrew Thompson static void		rum_start(struct ifnet *);
17802ac6454SAndrew Thompson static int		rum_ioctl(struct ifnet *, u_long, caddr_t);
17902ac6454SAndrew Thompson static void		rum_eeprom_read(struct rum_softc *, uint16_t, void *,
18002ac6454SAndrew Thompson 			    int);
18102ac6454SAndrew Thompson static uint32_t		rum_read(struct rum_softc *, uint16_t);
18202ac6454SAndrew Thompson static void		rum_read_multi(struct rum_softc *, uint16_t, void *,
18302ac6454SAndrew Thompson 			    int);
18477ddf3d3SAndrew Thompson static usb2_error_t	rum_write(struct rum_softc *, uint16_t, uint32_t);
18577ddf3d3SAndrew Thompson static usb2_error_t	rum_write_multi(struct rum_softc *, uint16_t, void *,
18602ac6454SAndrew Thompson 			    size_t);
18702ac6454SAndrew Thompson static void		rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
18802ac6454SAndrew Thompson static uint8_t		rum_bbp_read(struct rum_softc *, uint8_t);
18902ac6454SAndrew Thompson static void		rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
19002ac6454SAndrew Thompson static void		rum_select_antenna(struct rum_softc *);
19102ac6454SAndrew Thompson static void		rum_enable_mrr(struct rum_softc *);
19202ac6454SAndrew Thompson static void		rum_set_txpreamble(struct rum_softc *);
19302ac6454SAndrew Thompson static void		rum_set_basicrates(struct rum_softc *);
19402ac6454SAndrew Thompson static void		rum_select_band(struct rum_softc *,
19502ac6454SAndrew Thompson 			    struct ieee80211_channel *);
19602ac6454SAndrew Thompson static void		rum_set_chan(struct rum_softc *,
19702ac6454SAndrew Thompson 			    struct ieee80211_channel *);
19802ac6454SAndrew Thompson static void		rum_enable_tsf_sync(struct rum_softc *);
19902ac6454SAndrew Thompson static void		rum_update_slot(struct ifnet *);
20002ac6454SAndrew Thompson static void		rum_set_bssid(struct rum_softc *, const uint8_t *);
20102ac6454SAndrew Thompson static void		rum_set_macaddr(struct rum_softc *, const uint8_t *);
20277ddf3d3SAndrew Thompson static void		rum_update_promisc(struct ifnet *);
2035efea30fSAndrew Thompson static void		rum_setpromisc(struct rum_softc *);
20402ac6454SAndrew Thompson static const char	*rum_get_rf(int);
20502ac6454SAndrew Thompson static void		rum_read_eeprom(struct rum_softc *);
20602ac6454SAndrew Thompson static int		rum_bbp_init(struct rum_softc *);
2075efea30fSAndrew Thompson static void		rum_init_locked(struct rum_softc *);
20802ac6454SAndrew Thompson static void		rum_init(void *);
2095efea30fSAndrew Thompson static void		rum_stop(struct rum_softc *);
21077ddf3d3SAndrew Thompson static void		rum_load_microcode(struct rum_softc *, const uint8_t *,
21102ac6454SAndrew Thompson 			    size_t);
21202ac6454SAndrew Thompson static int		rum_prepare_beacon(struct rum_softc *,
21302ac6454SAndrew Thompson 			    struct ieee80211vap *);
21402ac6454SAndrew Thompson static int		rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
21502ac6454SAndrew Thompson 			    const struct ieee80211_bpf_params *);
21602ac6454SAndrew Thompson static struct ieee80211_node *rum_node_alloc(struct ieee80211vap *,
21702ac6454SAndrew Thompson 			    const uint8_t mac[IEEE80211_ADDR_LEN]);
21802ac6454SAndrew Thompson static void		rum_newassoc(struct ieee80211_node *, int);
21902ac6454SAndrew Thompson static void		rum_scan_start(struct ieee80211com *);
22002ac6454SAndrew Thompson static void		rum_scan_end(struct ieee80211com *);
22102ac6454SAndrew Thompson static void		rum_set_channel(struct ieee80211com *);
22202ac6454SAndrew Thompson static int		rum_get_rssi(struct rum_softc *, uint8_t);
22302ac6454SAndrew Thompson static void		rum_amrr_start(struct rum_softc *,
22402ac6454SAndrew Thompson 			    struct ieee80211_node *);
22502ac6454SAndrew Thompson static void		rum_amrr_timeout(void *);
2265efea30fSAndrew Thompson static void		rum_amrr_task(void *, int);
22702ac6454SAndrew Thompson static int		rum_pause(struct rum_softc *, int);
22802ac6454SAndrew Thompson 
22902ac6454SAndrew Thompson static const struct {
23002ac6454SAndrew Thompson 	uint32_t	reg;
23102ac6454SAndrew Thompson 	uint32_t	val;
23202ac6454SAndrew Thompson } rum_def_mac[] = {
23302ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR0,  0x025fb032 },
23402ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR1,  0x9eaa9eaf },
23502ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR2,  0x8a8b8c8d },
23602ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR3,  0x00858687 },
23702ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR7,  0x2e31353b },
23802ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR8,  0x2a2a2a2c },
23902ac6454SAndrew Thompson 	{ RT2573_TXRX_CSR15, 0x0000000f },
24002ac6454SAndrew Thompson 	{ RT2573_MAC_CSR6,   0x00000fff },
24102ac6454SAndrew Thompson 	{ RT2573_MAC_CSR8,   0x016c030a },
24202ac6454SAndrew Thompson 	{ RT2573_MAC_CSR10,  0x00000718 },
24302ac6454SAndrew Thompson 	{ RT2573_MAC_CSR12,  0x00000004 },
24402ac6454SAndrew Thompson 	{ RT2573_MAC_CSR13,  0x00007f00 },
24502ac6454SAndrew Thompson 	{ RT2573_SEC_CSR0,   0x00000000 },
24602ac6454SAndrew Thompson 	{ RT2573_SEC_CSR1,   0x00000000 },
24702ac6454SAndrew Thompson 	{ RT2573_SEC_CSR5,   0x00000000 },
24802ac6454SAndrew Thompson 	{ RT2573_PHY_CSR1,   0x000023b0 },
24902ac6454SAndrew Thompson 	{ RT2573_PHY_CSR5,   0x00040a06 },
25002ac6454SAndrew Thompson 	{ RT2573_PHY_CSR6,   0x00080606 },
25102ac6454SAndrew Thompson 	{ RT2573_PHY_CSR7,   0x00000408 },
25202ac6454SAndrew Thompson 	{ RT2573_AIFSN_CSR,  0x00002273 },
25302ac6454SAndrew Thompson 	{ RT2573_CWMIN_CSR,  0x00002344 },
25402ac6454SAndrew Thompson 	{ RT2573_CWMAX_CSR,  0x000034aa }
25502ac6454SAndrew Thompson };
25602ac6454SAndrew Thompson 
25702ac6454SAndrew Thompson static const struct {
25802ac6454SAndrew Thompson 	uint8_t	reg;
25902ac6454SAndrew Thompson 	uint8_t	val;
26002ac6454SAndrew Thompson } rum_def_bbp[] = {
26102ac6454SAndrew Thompson 	{   3, 0x80 },
26202ac6454SAndrew Thompson 	{  15, 0x30 },
26302ac6454SAndrew Thompson 	{  17, 0x20 },
26402ac6454SAndrew Thompson 	{  21, 0xc8 },
26502ac6454SAndrew Thompson 	{  22, 0x38 },
26602ac6454SAndrew Thompson 	{  23, 0x06 },
26702ac6454SAndrew Thompson 	{  24, 0xfe },
26802ac6454SAndrew Thompson 	{  25, 0x0a },
26902ac6454SAndrew Thompson 	{  26, 0x0d },
27002ac6454SAndrew Thompson 	{  32, 0x0b },
27102ac6454SAndrew Thompson 	{  34, 0x12 },
27202ac6454SAndrew Thompson 	{  37, 0x07 },
27302ac6454SAndrew Thompson 	{  39, 0xf8 },
27402ac6454SAndrew Thompson 	{  41, 0x60 },
27502ac6454SAndrew Thompson 	{  53, 0x10 },
27602ac6454SAndrew Thompson 	{  54, 0x18 },
27702ac6454SAndrew Thompson 	{  60, 0x10 },
27802ac6454SAndrew Thompson 	{  61, 0x04 },
27902ac6454SAndrew Thompson 	{  62, 0x04 },
28002ac6454SAndrew Thompson 	{  75, 0xfe },
28102ac6454SAndrew Thompson 	{  86, 0xfe },
28202ac6454SAndrew Thompson 	{  88, 0xfe },
28302ac6454SAndrew Thompson 	{  90, 0x0f },
28402ac6454SAndrew Thompson 	{  99, 0x00 },
28502ac6454SAndrew Thompson 	{ 102, 0x16 },
28602ac6454SAndrew Thompson 	{ 107, 0x04 }
28702ac6454SAndrew Thompson };
28802ac6454SAndrew Thompson 
28902ac6454SAndrew Thompson static const struct rfprog {
29002ac6454SAndrew Thompson 	uint8_t		chan;
29102ac6454SAndrew Thompson 	uint32_t	r1, r2, r3, r4;
29202ac6454SAndrew Thompson }  rum_rf5226[] = {
29302ac6454SAndrew Thompson 	{   1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
29402ac6454SAndrew Thompson 	{   2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
29502ac6454SAndrew Thompson 	{   3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
29602ac6454SAndrew Thompson 	{   4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
29702ac6454SAndrew Thompson 	{   5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
29802ac6454SAndrew Thompson 	{   6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
29902ac6454SAndrew Thompson 	{   7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
30002ac6454SAndrew Thompson 	{   8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
30102ac6454SAndrew Thompson 	{   9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
30202ac6454SAndrew Thompson 	{  10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
30302ac6454SAndrew Thompson 	{  11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
30402ac6454SAndrew Thompson 	{  12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
30502ac6454SAndrew Thompson 	{  13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
30602ac6454SAndrew Thompson 	{  14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
30702ac6454SAndrew Thompson 
30802ac6454SAndrew Thompson 	{  34, 0x00b03, 0x20266, 0x36014, 0x30282 },
30902ac6454SAndrew Thompson 	{  38, 0x00b03, 0x20267, 0x36014, 0x30284 },
31002ac6454SAndrew Thompson 	{  42, 0x00b03, 0x20268, 0x36014, 0x30286 },
31102ac6454SAndrew Thompson 	{  46, 0x00b03, 0x20269, 0x36014, 0x30288 },
31202ac6454SAndrew Thompson 
31302ac6454SAndrew Thompson 	{  36, 0x00b03, 0x00266, 0x26014, 0x30288 },
31402ac6454SAndrew Thompson 	{  40, 0x00b03, 0x00268, 0x26014, 0x30280 },
31502ac6454SAndrew Thompson 	{  44, 0x00b03, 0x00269, 0x26014, 0x30282 },
31602ac6454SAndrew Thompson 	{  48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
31702ac6454SAndrew Thompson 	{  52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
31802ac6454SAndrew Thompson 	{  56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
31902ac6454SAndrew Thompson 	{  60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
32002ac6454SAndrew Thompson 	{  64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
32102ac6454SAndrew Thompson 
32202ac6454SAndrew Thompson 	{ 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
32302ac6454SAndrew Thompson 	{ 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
32402ac6454SAndrew Thompson 	{ 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
32502ac6454SAndrew Thompson 	{ 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
32602ac6454SAndrew Thompson 	{ 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
32702ac6454SAndrew Thompson 	{ 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
32802ac6454SAndrew Thompson 	{ 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
32902ac6454SAndrew Thompson 	{ 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
33002ac6454SAndrew Thompson 	{ 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
33102ac6454SAndrew Thompson 	{ 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
33202ac6454SAndrew Thompson 	{ 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
33302ac6454SAndrew Thompson 
33402ac6454SAndrew Thompson 	{ 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
33502ac6454SAndrew Thompson 	{ 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
33602ac6454SAndrew Thompson 	{ 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
33702ac6454SAndrew Thompson 	{ 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
33802ac6454SAndrew Thompson 	{ 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
33902ac6454SAndrew Thompson }, rum_rf5225[] = {
34002ac6454SAndrew Thompson 	{   1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
34102ac6454SAndrew Thompson 	{   2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
34202ac6454SAndrew Thompson 	{   3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
34302ac6454SAndrew Thompson 	{   4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
34402ac6454SAndrew Thompson 	{   5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
34502ac6454SAndrew Thompson 	{   6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
34602ac6454SAndrew Thompson 	{   7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
34702ac6454SAndrew Thompson 	{   8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
34802ac6454SAndrew Thompson 	{   9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
34902ac6454SAndrew Thompson 	{  10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
35002ac6454SAndrew Thompson 	{  11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
35102ac6454SAndrew Thompson 	{  12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
35202ac6454SAndrew Thompson 	{  13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
35302ac6454SAndrew Thompson 	{  14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
35402ac6454SAndrew Thompson 
35502ac6454SAndrew Thompson 	{  34, 0x00b33, 0x01266, 0x26014, 0x30282 },
35602ac6454SAndrew Thompson 	{  38, 0x00b33, 0x01267, 0x26014, 0x30284 },
35702ac6454SAndrew Thompson 	{  42, 0x00b33, 0x01268, 0x26014, 0x30286 },
35802ac6454SAndrew Thompson 	{  46, 0x00b33, 0x01269, 0x26014, 0x30288 },
35902ac6454SAndrew Thompson 
36002ac6454SAndrew Thompson 	{  36, 0x00b33, 0x01266, 0x26014, 0x30288 },
36102ac6454SAndrew Thompson 	{  40, 0x00b33, 0x01268, 0x26014, 0x30280 },
36202ac6454SAndrew Thompson 	{  44, 0x00b33, 0x01269, 0x26014, 0x30282 },
36302ac6454SAndrew Thompson 	{  48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
36402ac6454SAndrew Thompson 	{  52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
36502ac6454SAndrew Thompson 	{  56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
36602ac6454SAndrew Thompson 	{  60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
36702ac6454SAndrew Thompson 	{  64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
36802ac6454SAndrew Thompson 
36902ac6454SAndrew Thompson 	{ 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
37002ac6454SAndrew Thompson 	{ 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
37102ac6454SAndrew Thompson 	{ 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
37202ac6454SAndrew Thompson 	{ 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
37302ac6454SAndrew Thompson 	{ 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
37402ac6454SAndrew Thompson 	{ 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
37502ac6454SAndrew Thompson 	{ 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
37602ac6454SAndrew Thompson 	{ 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
37702ac6454SAndrew Thompson 	{ 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
37802ac6454SAndrew Thompson 	{ 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
37902ac6454SAndrew Thompson 	{ 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
38002ac6454SAndrew Thompson 
38102ac6454SAndrew Thompson 	{ 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
38202ac6454SAndrew Thompson 	{ 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
38302ac6454SAndrew Thompson 	{ 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
38402ac6454SAndrew Thompson 	{ 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
38502ac6454SAndrew Thompson 	{ 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
38602ac6454SAndrew Thompson };
38702ac6454SAndrew Thompson 
38802ac6454SAndrew Thompson static const struct usb2_config rum_config[RUM_N_TRANSFER] = {
38902ac6454SAndrew Thompson 	[RUM_BULK_WR] = {
39002ac6454SAndrew Thompson 		.type = UE_BULK,
39102ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
39202ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
3934eae601eSAndrew Thompson 		.bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
3944eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
3954eae601eSAndrew Thompson 		.callback = rum_bulk_write_callback,
3964eae601eSAndrew Thompson 		.timeout = 5000,	/* ms */
39702ac6454SAndrew Thompson 	},
39802ac6454SAndrew Thompson 	[RUM_BULK_RD] = {
39902ac6454SAndrew Thompson 		.type = UE_BULK,
40002ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
40102ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
4024eae601eSAndrew Thompson 		.bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
4034eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
4044eae601eSAndrew Thompson 		.callback = rum_bulk_read_callback,
40502ac6454SAndrew Thompson 	},
40602ac6454SAndrew Thompson };
40702ac6454SAndrew Thompson 
40802ac6454SAndrew Thompson static int
40902ac6454SAndrew Thompson rum_match(device_t self)
41002ac6454SAndrew Thompson {
41102ac6454SAndrew Thompson 	struct usb2_attach_arg *uaa = device_get_ivars(self);
41202ac6454SAndrew Thompson 
41302ac6454SAndrew Thompson 	if (uaa->usb2_mode != USB_MODE_HOST)
41402ac6454SAndrew Thompson 		return (ENXIO);
41502ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != 0)
41602ac6454SAndrew Thompson 		return (ENXIO);
41702ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
41802ac6454SAndrew Thompson 		return (ENXIO);
41902ac6454SAndrew Thompson 
42002ac6454SAndrew Thompson 	return (usb2_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
42102ac6454SAndrew Thompson }
42202ac6454SAndrew Thompson 
42302ac6454SAndrew Thompson static int
42402ac6454SAndrew Thompson rum_attach(device_t self)
42502ac6454SAndrew Thompson {
42602ac6454SAndrew Thompson 	struct usb2_attach_arg *uaa = device_get_ivars(self);
42702ac6454SAndrew Thompson 	struct rum_softc *sc = device_get_softc(self);
4285efea30fSAndrew Thompson 	struct ieee80211com *ic;
4295efea30fSAndrew Thompson 	struct ifnet *ifp;
4305efea30fSAndrew Thompson 	uint8_t iface_index, bands;
4315efea30fSAndrew Thompson 	uint32_t tmp;
4325efea30fSAndrew Thompson 	int error, ntries;
43302ac6454SAndrew Thompson 
43402ac6454SAndrew Thompson 	device_set_usb2_desc(self);
43502ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
43602ac6454SAndrew Thompson 	sc->sc_dev = self;
43702ac6454SAndrew Thompson 
43802ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, device_get_nameunit(self),
43902ac6454SAndrew Thompson 	    MTX_NETWORK_LOCK, MTX_DEF);
44002ac6454SAndrew Thompson 
44102ac6454SAndrew Thompson 	iface_index = RT2573_IFACE_INDEX;
44202ac6454SAndrew Thompson 	error = usb2_transfer_setup(uaa->device, &iface_index,
44302ac6454SAndrew Thompson 	    sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
44402ac6454SAndrew Thompson 	if (error) {
44502ac6454SAndrew Thompson 		device_printf(self, "could not allocate USB transfers, "
44602ac6454SAndrew Thompson 		    "err=%s\n", usb2_errstr(error));
44702ac6454SAndrew Thompson 		goto detach;
44802ac6454SAndrew Thompson 	}
44902ac6454SAndrew Thompson 
45002ac6454SAndrew Thompson 	RUM_LOCK(sc);
45102ac6454SAndrew Thompson 	/* retrieve RT2573 rev. no */
45202ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
45302ac6454SAndrew Thompson 		if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
45402ac6454SAndrew Thompson 			break;
45502ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
45602ac6454SAndrew Thompson 			break;
45702ac6454SAndrew Thompson 	}
45802ac6454SAndrew Thompson 	if (ntries == 100) {
45902ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
4605efea30fSAndrew Thompson 		RUM_UNLOCK(sc);
4615efea30fSAndrew Thompson 		goto detach;
46202ac6454SAndrew Thompson 	}
46302ac6454SAndrew Thompson 
46402ac6454SAndrew Thompson 	/* retrieve MAC address and various other things from EEPROM */
46502ac6454SAndrew Thompson 	rum_read_eeprom(sc);
46602ac6454SAndrew Thompson 
46702ac6454SAndrew Thompson 	device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
46802ac6454SAndrew Thompson 	    tmp, rum_get_rf(sc->rf_rev));
46902ac6454SAndrew Thompson 
47077ddf3d3SAndrew Thompson 	rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
47102ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
47202ac6454SAndrew Thompson 
47302ac6454SAndrew Thompson 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
47402ac6454SAndrew Thompson 	if (ifp == NULL) {
47502ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "can not if_alloc()\n");
4765efea30fSAndrew Thompson 		goto detach;
47702ac6454SAndrew Thompson 	}
47802ac6454SAndrew Thompson 	ic = ifp->if_l2com;
47902ac6454SAndrew Thompson 
48002ac6454SAndrew Thompson 	ifp->if_softc = sc;
48102ac6454SAndrew Thompson 	if_initname(ifp, "rum", device_get_unit(sc->sc_dev));
48202ac6454SAndrew Thompson 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
48302ac6454SAndrew Thompson 	ifp->if_init = rum_init;
48402ac6454SAndrew Thompson 	ifp->if_ioctl = rum_ioctl;
48502ac6454SAndrew Thompson 	ifp->if_start = rum_start;
48602ac6454SAndrew Thompson 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
48702ac6454SAndrew Thompson 	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
48802ac6454SAndrew Thompson 	IFQ_SET_READY(&ifp->if_snd);
48902ac6454SAndrew Thompson 
49002ac6454SAndrew Thompson 	ic->ic_ifp = ifp;
49102ac6454SAndrew Thompson 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
49202ac6454SAndrew Thompson 
49302ac6454SAndrew Thompson 	/* set device capabilities */
49402ac6454SAndrew Thompson 	ic->ic_caps =
49502ac6454SAndrew Thompson 	      IEEE80211_C_STA		/* station mode supported */
49602ac6454SAndrew Thompson 	    | IEEE80211_C_IBSS		/* IBSS mode supported */
49702ac6454SAndrew Thompson 	    | IEEE80211_C_MONITOR	/* monitor mode supported */
49802ac6454SAndrew Thompson 	    | IEEE80211_C_HOSTAP	/* HostAp mode supported */
49902ac6454SAndrew Thompson 	    | IEEE80211_C_TXPMGT	/* tx power management */
50002ac6454SAndrew Thompson 	    | IEEE80211_C_SHPREAMBLE	/* short preamble supported */
50102ac6454SAndrew Thompson 	    | IEEE80211_C_SHSLOT	/* short slot time supported */
50202ac6454SAndrew Thompson 	    | IEEE80211_C_BGSCAN	/* bg scanning supported */
50302ac6454SAndrew Thompson 	    | IEEE80211_C_WPA		/* 802.11i */
50402ac6454SAndrew Thompson 	    ;
50502ac6454SAndrew Thompson 
50602ac6454SAndrew Thompson 	bands = 0;
50702ac6454SAndrew Thompson 	setbit(&bands, IEEE80211_MODE_11B);
50802ac6454SAndrew Thompson 	setbit(&bands, IEEE80211_MODE_11G);
50902ac6454SAndrew Thompson 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226)
51002ac6454SAndrew Thompson 		setbit(&bands, IEEE80211_MODE_11A);
51102ac6454SAndrew Thompson 	ieee80211_init_channels(ic, NULL, &bands);
51202ac6454SAndrew Thompson 
51329aca940SSam Leffler 	ieee80211_ifattach(ic, sc->sc_bssid);
51477ddf3d3SAndrew Thompson 	ic->ic_update_promisc = rum_update_promisc;
51502ac6454SAndrew Thompson 	ic->ic_newassoc = rum_newassoc;
51602ac6454SAndrew Thompson 	ic->ic_raw_xmit = rum_raw_xmit;
51702ac6454SAndrew Thompson 	ic->ic_node_alloc = rum_node_alloc;
51802ac6454SAndrew Thompson 	ic->ic_scan_start = rum_scan_start;
51902ac6454SAndrew Thompson 	ic->ic_scan_end = rum_scan_end;
52002ac6454SAndrew Thompson 	ic->ic_set_channel = rum_set_channel;
52102ac6454SAndrew Thompson 
52202ac6454SAndrew Thompson 	ic->ic_vap_create = rum_vap_create;
52302ac6454SAndrew Thompson 	ic->ic_vap_delete = rum_vap_delete;
52402ac6454SAndrew Thompson 
52502ac6454SAndrew Thompson 	bpfattach(ifp, DLT_IEEE802_11_RADIO,
52602ac6454SAndrew Thompson 	    sizeof (struct ieee80211_frame) + sizeof(sc->sc_txtap));
52702ac6454SAndrew Thompson 
52802ac6454SAndrew Thompson 	sc->sc_rxtap_len = sizeof sc->sc_rxtap;
52902ac6454SAndrew Thompson 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
53002ac6454SAndrew Thompson 	sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2573_RX_RADIOTAP_PRESENT);
53102ac6454SAndrew Thompson 
53202ac6454SAndrew Thompson 	sc->sc_txtap_len = sizeof sc->sc_txtap;
53302ac6454SAndrew Thompson 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
53402ac6454SAndrew Thompson 	sc->sc_txtap.wt_ihdr.it_present = htole32(RT2573_TX_RADIOTAP_PRESENT);
53502ac6454SAndrew Thompson 
53602ac6454SAndrew Thompson 	if (bootverbose)
53702ac6454SAndrew Thompson 		ieee80211_announce(ic);
53802ac6454SAndrew Thompson 
5395efea30fSAndrew Thompson 	return (0);
5405efea30fSAndrew Thompson 
5415efea30fSAndrew Thompson detach:
5425efea30fSAndrew Thompson 	rum_detach(self);
5435efea30fSAndrew Thompson 	return (ENXIO);			/* failure */
54402ac6454SAndrew Thompson }
54502ac6454SAndrew Thompson 
54602ac6454SAndrew Thompson static int
54702ac6454SAndrew Thompson rum_detach(device_t self)
54802ac6454SAndrew Thompson {
54902ac6454SAndrew Thompson 	struct rum_softc *sc = device_get_softc(self);
55002ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
55102ac6454SAndrew Thompson 	struct ieee80211com *ic;
55202ac6454SAndrew Thompson 
55302ac6454SAndrew Thompson 	/* stop all USB transfers */
55402ac6454SAndrew Thompson 	usb2_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
55502ac6454SAndrew Thompson 
55602ac6454SAndrew Thompson 	/* free TX list, if any */
55702ac6454SAndrew Thompson 	RUM_LOCK(sc);
55802ac6454SAndrew Thompson 	rum_unsetup_tx_list(sc);
55902ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
56002ac6454SAndrew Thompson 
56102ac6454SAndrew Thompson 	if (ifp) {
56202ac6454SAndrew Thompson 		ic = ifp->if_l2com;
56302ac6454SAndrew Thompson 		bpfdetach(ifp);
56402ac6454SAndrew Thompson 		ieee80211_ifdetach(ic);
56502ac6454SAndrew Thompson 		if_free(ifp);
56602ac6454SAndrew Thompson 	}
56702ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
56802ac6454SAndrew Thompson 
56902ac6454SAndrew Thompson 	return (0);
57002ac6454SAndrew Thompson }
57102ac6454SAndrew Thompson 
57277ddf3d3SAndrew Thompson static usb2_error_t
57377ddf3d3SAndrew Thompson rum_do_request(struct rum_softc *sc,
57477ddf3d3SAndrew Thompson     struct usb2_device_request *req, void *data)
57577ddf3d3SAndrew Thompson {
57677ddf3d3SAndrew Thompson 	usb2_error_t err;
57777ddf3d3SAndrew Thompson 	int ntries = 10;
57877ddf3d3SAndrew Thompson 
57977ddf3d3SAndrew Thompson 	while (ntries--) {
5805efea30fSAndrew Thompson 		err = usb2_do_request_flags(sc->sc_udev, &sc->sc_mtx,
58177ddf3d3SAndrew Thompson 		    req, data, 0, NULL, 250 /* ms */);
58277ddf3d3SAndrew Thompson 		if (err == 0)
58377ddf3d3SAndrew Thompson 			break;
58477ddf3d3SAndrew Thompson 
58577ddf3d3SAndrew Thompson 		DPRINTFN(1, "Control request failed, %s (retrying)\n",
58677ddf3d3SAndrew Thompson 		    usb2_errstr(err));
58777ddf3d3SAndrew Thompson 		if (rum_pause(sc, hz / 100))
58877ddf3d3SAndrew Thompson 			break;
58977ddf3d3SAndrew Thompson 	}
59077ddf3d3SAndrew Thompson 	return (err);
59177ddf3d3SAndrew Thompson }
59277ddf3d3SAndrew Thompson 
59302ac6454SAndrew Thompson static struct ieee80211vap *
59402ac6454SAndrew Thompson rum_vap_create(struct ieee80211com *ic,
59502ac6454SAndrew Thompson 	const char name[IFNAMSIZ], int unit, int opmode, int flags,
59602ac6454SAndrew Thompson 	const uint8_t bssid[IEEE80211_ADDR_LEN],
59702ac6454SAndrew Thompson 	const uint8_t mac[IEEE80211_ADDR_LEN])
59802ac6454SAndrew Thompson {
59902ac6454SAndrew Thompson 	struct rum_softc *sc = ic->ic_ifp->if_softc;
60002ac6454SAndrew Thompson 	struct rum_vap *rvp;
60102ac6454SAndrew Thompson 	struct ieee80211vap *vap;
60202ac6454SAndrew Thompson 
60302ac6454SAndrew Thompson 	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
60402ac6454SAndrew Thompson 		return NULL;
60502ac6454SAndrew Thompson 	rvp = (struct rum_vap *) malloc(sizeof(struct rum_vap),
60602ac6454SAndrew Thompson 	    M_80211_VAP, M_NOWAIT | M_ZERO);
60702ac6454SAndrew Thompson 	if (rvp == NULL)
60802ac6454SAndrew Thompson 		return NULL;
60902ac6454SAndrew Thompson 	vap = &rvp->vap;
61002ac6454SAndrew Thompson 	/* enable s/w bmiss handling for sta mode */
61102ac6454SAndrew Thompson 	ieee80211_vap_setup(ic, vap, name, unit, opmode,
61202ac6454SAndrew Thompson 	    flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
61302ac6454SAndrew Thompson 
61402ac6454SAndrew Thompson 	/* override state transition machine */
61502ac6454SAndrew Thompson 	rvp->newstate = vap->iv_newstate;
61602ac6454SAndrew Thompson 	vap->iv_newstate = rum_newstate;
61702ac6454SAndrew Thompson 
61802ac6454SAndrew Thompson 	usb2_callout_init_mtx(&rvp->amrr_ch, &sc->sc_mtx, 0);
6195efea30fSAndrew Thompson 	TASK_INIT(&rvp->amrr_task, 0, rum_amrr_task, rvp);
62002ac6454SAndrew Thompson 	ieee80211_amrr_init(&rvp->amrr, vap,
62102ac6454SAndrew Thompson 	    IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD,
62202ac6454SAndrew Thompson 	    IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD,
62302ac6454SAndrew Thompson 	    1000 /* 1 sec */);
62402ac6454SAndrew Thompson 
62502ac6454SAndrew Thompson 	/* complete setup */
62602ac6454SAndrew Thompson 	ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
62702ac6454SAndrew Thompson 	ic->ic_opmode = opmode;
62802ac6454SAndrew Thompson 	return vap;
62902ac6454SAndrew Thompson }
63002ac6454SAndrew Thompson 
63102ac6454SAndrew Thompson static void
63202ac6454SAndrew Thompson rum_vap_delete(struct ieee80211vap *vap)
63302ac6454SAndrew Thompson {
63402ac6454SAndrew Thompson 	struct rum_vap *rvp = RUM_VAP(vap);
6355efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
63602ac6454SAndrew Thompson 
63702ac6454SAndrew Thompson 	usb2_callout_drain(&rvp->amrr_ch);
6385efea30fSAndrew Thompson 	ieee80211_draintask(ic, &rvp->amrr_task);
63902ac6454SAndrew Thompson 	ieee80211_amrr_cleanup(&rvp->amrr);
64002ac6454SAndrew Thompson 	ieee80211_vap_detach(vap);
64102ac6454SAndrew Thompson 	free(rvp, M_80211_VAP);
64202ac6454SAndrew Thompson }
64302ac6454SAndrew Thompson 
64402ac6454SAndrew Thompson static void
64502ac6454SAndrew Thompson rum_tx_free(struct rum_tx_data *data, int txerr)
64602ac6454SAndrew Thompson {
64702ac6454SAndrew Thompson 	struct rum_softc *sc = data->sc;
64802ac6454SAndrew Thompson 
64902ac6454SAndrew Thompson 	if (data->m != NULL) {
65002ac6454SAndrew Thompson 		if (data->m->m_flags & M_TXCB)
65102ac6454SAndrew Thompson 			ieee80211_process_callback(data->ni, data->m,
65202ac6454SAndrew Thompson 			    txerr ? ETIMEDOUT : 0);
65302ac6454SAndrew Thompson 		m_freem(data->m);
65402ac6454SAndrew Thompson 		data->m = NULL;
65502ac6454SAndrew Thompson 
65602ac6454SAndrew Thompson 		ieee80211_free_node(data->ni);
65702ac6454SAndrew Thompson 		data->ni = NULL;
65802ac6454SAndrew Thompson 	}
65902ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
66002ac6454SAndrew Thompson 	sc->tx_nfree++;
66102ac6454SAndrew Thompson }
66202ac6454SAndrew Thompson 
66302ac6454SAndrew Thompson static void
66402ac6454SAndrew Thompson rum_setup_tx_list(struct rum_softc *sc)
66502ac6454SAndrew Thompson {
66602ac6454SAndrew Thompson 	struct rum_tx_data *data;
66702ac6454SAndrew Thompson 	int i;
66802ac6454SAndrew Thompson 
66902ac6454SAndrew Thompson 	sc->tx_nfree = 0;
67002ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_q);
67102ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_free);
67202ac6454SAndrew Thompson 
67302ac6454SAndrew Thompson 	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
67402ac6454SAndrew Thompson 		data = &sc->tx_data[i];
67502ac6454SAndrew Thompson 
67602ac6454SAndrew Thompson 		data->sc = sc;
67702ac6454SAndrew Thompson 		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
67802ac6454SAndrew Thompson 		sc->tx_nfree++;
67902ac6454SAndrew Thompson 	}
68002ac6454SAndrew Thompson }
68102ac6454SAndrew Thompson 
68202ac6454SAndrew Thompson static void
68302ac6454SAndrew Thompson rum_unsetup_tx_list(struct rum_softc *sc)
68402ac6454SAndrew Thompson {
68502ac6454SAndrew Thompson 	struct rum_tx_data *data;
68602ac6454SAndrew Thompson 	int i;
68702ac6454SAndrew Thompson 
68802ac6454SAndrew Thompson 	/* make sure any subsequent use of the queues will fail */
68902ac6454SAndrew Thompson 	sc->tx_nfree = 0;
69002ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_q);
69102ac6454SAndrew Thompson 	STAILQ_INIT(&sc->tx_free);
69202ac6454SAndrew Thompson 
69302ac6454SAndrew Thompson 	/* free up all node references and mbufs */
69402ac6454SAndrew Thompson 	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
69502ac6454SAndrew Thompson 		data = &sc->tx_data[i];
69602ac6454SAndrew Thompson 
69702ac6454SAndrew Thompson 		if (data->m != NULL) {
69802ac6454SAndrew Thompson 			m_freem(data->m);
69902ac6454SAndrew Thompson 			data->m = NULL;
70002ac6454SAndrew Thompson 		}
70102ac6454SAndrew Thompson 		if (data->ni != NULL) {
70202ac6454SAndrew Thompson 			ieee80211_free_node(data->ni);
70302ac6454SAndrew Thompson 			data->ni = NULL;
70402ac6454SAndrew Thompson 		}
70502ac6454SAndrew Thompson 	}
70602ac6454SAndrew Thompson }
70702ac6454SAndrew Thompson 
7085efea30fSAndrew Thompson static int
7095efea30fSAndrew Thompson rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
71002ac6454SAndrew Thompson {
71102ac6454SAndrew Thompson 	struct rum_vap *rvp = RUM_VAP(vap);
7125efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
7135efea30fSAndrew Thompson 	struct rum_softc *sc = ic->ic_ifp->if_softc;
71402ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
71502ac6454SAndrew Thompson 	enum ieee80211_state ostate;
71602ac6454SAndrew Thompson 	struct ieee80211_node *ni;
71702ac6454SAndrew Thompson 	uint32_t tmp;
71802ac6454SAndrew Thompson 
71902ac6454SAndrew Thompson 	ostate = vap->iv_state;
7205efea30fSAndrew Thompson 	DPRINTF("%s -> %s\n",
7215efea30fSAndrew Thompson 		ieee80211_state_name[ostate],
7225efea30fSAndrew Thompson 		ieee80211_state_name[nstate]);
72302ac6454SAndrew Thompson 
7245efea30fSAndrew Thompson 	IEEE80211_UNLOCK(ic);
7255efea30fSAndrew Thompson 	RUM_LOCK(sc);
7265efea30fSAndrew Thompson 	usb2_callout_stop(&rvp->amrr_ch);
7275efea30fSAndrew Thompson 
7285efea30fSAndrew Thompson 	switch (nstate) {
72902ac6454SAndrew Thompson 	case IEEE80211_S_INIT:
73002ac6454SAndrew Thompson 		if (ostate == IEEE80211_S_RUN) {
73102ac6454SAndrew Thompson 			/* abort TSF synchronization */
73202ac6454SAndrew Thompson 			tmp = rum_read(sc, RT2573_TXRX_CSR9);
73302ac6454SAndrew Thompson 			rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
73402ac6454SAndrew Thompson 		}
73502ac6454SAndrew Thompson 		break;
73602ac6454SAndrew Thompson 
73702ac6454SAndrew Thompson 	case IEEE80211_S_RUN:
73802ac6454SAndrew Thompson 		ni = vap->iv_bss;
73902ac6454SAndrew Thompson 
74002ac6454SAndrew Thompson 		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
74102ac6454SAndrew Thompson 			rum_update_slot(ic->ic_ifp);
74202ac6454SAndrew Thompson 			rum_enable_mrr(sc);
74302ac6454SAndrew Thompson 			rum_set_txpreamble(sc);
74402ac6454SAndrew Thompson 			rum_set_basicrates(sc);
74502ac6454SAndrew Thompson 			IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
74602ac6454SAndrew Thompson 			rum_set_bssid(sc, sc->sc_bssid);
74702ac6454SAndrew Thompson 		}
74802ac6454SAndrew Thompson 
74902ac6454SAndrew Thompson 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
75002ac6454SAndrew Thompson 		    vap->iv_opmode == IEEE80211_M_IBSS)
75102ac6454SAndrew Thompson 			rum_prepare_beacon(sc, vap);
75202ac6454SAndrew Thompson 
75302ac6454SAndrew Thompson 		if (vap->iv_opmode != IEEE80211_M_MONITOR)
75402ac6454SAndrew Thompson 			rum_enable_tsf_sync(sc);
75502ac6454SAndrew Thompson 
75602ac6454SAndrew Thompson 		/* enable automatic rate adaptation */
75702ac6454SAndrew Thompson 		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
75802ac6454SAndrew Thompson 		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
75902ac6454SAndrew Thompson 			rum_amrr_start(sc, ni);
76002ac6454SAndrew Thompson 		break;
76102ac6454SAndrew Thompson 	default:
76202ac6454SAndrew Thompson 		break;
76302ac6454SAndrew Thompson 	}
76402ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
76502ac6454SAndrew Thompson 	IEEE80211_LOCK(ic);
7665efea30fSAndrew Thompson 	return (rvp->newstate(vap, nstate, arg));
76702ac6454SAndrew Thompson }
76802ac6454SAndrew Thompson 
76902ac6454SAndrew Thompson static void
77002ac6454SAndrew Thompson rum_bulk_write_callback(struct usb2_xfer *xfer)
77102ac6454SAndrew Thompson {
77202ac6454SAndrew Thompson 	struct rum_softc *sc = xfer->priv_sc;
77302ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
77402ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
77502ac6454SAndrew Thompson 	struct ieee80211_channel *c = ic->ic_curchan;
77602ac6454SAndrew Thompson 	struct rum_tx_data *data;
77702ac6454SAndrew Thompson 	struct mbuf *m;
77802ac6454SAndrew Thompson 	unsigned int len;
77902ac6454SAndrew Thompson 
78002ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
78102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
78202ac6454SAndrew Thompson 		DPRINTFN(11, "transfer complete, %d bytes\n", xfer->actlen);
78302ac6454SAndrew Thompson 
78402ac6454SAndrew Thompson 		/* free resources */
78502ac6454SAndrew Thompson 		data = xfer->priv_fifo;
78602ac6454SAndrew Thompson 		rum_tx_free(data, 0);
78702ac6454SAndrew Thompson 		xfer->priv_fifo = NULL;
78802ac6454SAndrew Thompson 
78902ac6454SAndrew Thompson 		ifp->if_opackets++;
79002ac6454SAndrew Thompson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
79102ac6454SAndrew Thompson 
79202ac6454SAndrew Thompson 		/* FALLTHROUGH */
79302ac6454SAndrew Thompson 	case USB_ST_SETUP:
79402ac6454SAndrew Thompson tr_setup:
79502ac6454SAndrew Thompson 		data = STAILQ_FIRST(&sc->tx_q);
79602ac6454SAndrew Thompson 		if (data) {
79702ac6454SAndrew Thompson 			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
79802ac6454SAndrew Thompson 			m = data->m;
79902ac6454SAndrew Thompson 
80002ac6454SAndrew Thompson 			if (m->m_pkthdr.len > (MCLBYTES + RT2573_TX_DESC_SIZE)) {
80102ac6454SAndrew Thompson 				DPRINTFN(0, "data overflow, %u bytes\n",
80202ac6454SAndrew Thompson 				    m->m_pkthdr.len);
80302ac6454SAndrew Thompson 				m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
80402ac6454SAndrew Thompson 			}
80502ac6454SAndrew Thompson 			usb2_copy_in(xfer->frbuffers, 0, &data->desc,
80602ac6454SAndrew Thompson 			    RT2573_TX_DESC_SIZE);
80702ac6454SAndrew Thompson 			usb2_m_copy_in(xfer->frbuffers, RT2573_TX_DESC_SIZE, m,
80802ac6454SAndrew Thompson 			    0, m->m_pkthdr.len);
80902ac6454SAndrew Thompson 
81002ac6454SAndrew Thompson 			if (bpf_peers_present(ifp->if_bpf)) {
81102ac6454SAndrew Thompson 				struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
81202ac6454SAndrew Thompson 
81302ac6454SAndrew Thompson 				tap->wt_flags = 0;
81402ac6454SAndrew Thompson 				tap->wt_rate = data->rate;
81502ac6454SAndrew Thompson 				tap->wt_chan_freq = htole16(c->ic_freq);
81602ac6454SAndrew Thompson 				tap->wt_chan_flags = htole16(c->ic_flags);
81702ac6454SAndrew Thompson 				tap->wt_antenna = sc->tx_ant;
81802ac6454SAndrew Thompson 
81902ac6454SAndrew Thompson 				bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m);
82002ac6454SAndrew Thompson 			}
82102ac6454SAndrew Thompson 
82202ac6454SAndrew Thompson 			/* align end on a 4-bytes boundary */
82302ac6454SAndrew Thompson 			len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
82402ac6454SAndrew Thompson 			if ((len % 64) == 0)
82502ac6454SAndrew Thompson 				len += 4;
82602ac6454SAndrew Thompson 
82702ac6454SAndrew Thompson 			DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
82802ac6454SAndrew Thompson 			    m->m_pkthdr.len, len);
82902ac6454SAndrew Thompson 
83002ac6454SAndrew Thompson 			xfer->frlengths[0] = len;
83102ac6454SAndrew Thompson 			xfer->priv_fifo = data;
83202ac6454SAndrew Thompson 
83302ac6454SAndrew Thompson 			usb2_start_hardware(xfer);
83402ac6454SAndrew Thompson 		}
83502ac6454SAndrew Thompson 		break;
83602ac6454SAndrew Thompson 
83702ac6454SAndrew Thompson 	default:			/* Error */
83802ac6454SAndrew Thompson 		DPRINTFN(11, "transfer error, %s\n",
83902ac6454SAndrew Thompson 		    usb2_errstr(xfer->error));
84002ac6454SAndrew Thompson 
84102ac6454SAndrew Thompson 		ifp->if_oerrors++;
84202ac6454SAndrew Thompson 		data = xfer->priv_fifo;
84302ac6454SAndrew Thompson 		if (data != NULL) {
84402ac6454SAndrew Thompson 			rum_tx_free(data, xfer->error);
84502ac6454SAndrew Thompson 			xfer->priv_fifo = NULL;
84602ac6454SAndrew Thompson 		}
84702ac6454SAndrew Thompson 
84802ac6454SAndrew Thompson 		if (xfer->error == USB_ERR_STALLED) {
84902ac6454SAndrew Thompson 			/* try to clear stall first */
85002ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
85102ac6454SAndrew Thompson 			goto tr_setup;
85202ac6454SAndrew Thompson 		}
85302ac6454SAndrew Thompson 		if (xfer->error == USB_ERR_TIMEOUT)
85402ac6454SAndrew Thompson 			device_printf(sc->sc_dev, "device timeout\n");
85502ac6454SAndrew Thompson 		break;
85602ac6454SAndrew Thompson 	}
85702ac6454SAndrew Thompson }
85802ac6454SAndrew Thompson 
85902ac6454SAndrew Thompson static void
86002ac6454SAndrew Thompson rum_bulk_read_callback(struct usb2_xfer *xfer)
86102ac6454SAndrew Thompson {
86202ac6454SAndrew Thompson 	struct rum_softc *sc = xfer->priv_sc;
86302ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
86402ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
86502ac6454SAndrew Thompson 	struct ieee80211_node *ni;
86602ac6454SAndrew Thompson 	struct mbuf *m = NULL;
86702ac6454SAndrew Thompson 	uint32_t flags;
86802ac6454SAndrew Thompson 	uint8_t rssi = 0;
86902ac6454SAndrew Thompson 	unsigned int len;
87002ac6454SAndrew Thompson 
87102ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
87202ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
87302ac6454SAndrew Thompson 
87402ac6454SAndrew Thompson 		DPRINTFN(15, "rx done, actlen=%d\n", xfer->actlen);
87502ac6454SAndrew Thompson 
87602ac6454SAndrew Thompson 		len = xfer->actlen;
87702ac6454SAndrew Thompson 		if (len < RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN) {
87802ac6454SAndrew Thompson 			DPRINTF("%s: xfer too short %d\n",
87902ac6454SAndrew Thompson 			    device_get_nameunit(sc->sc_dev), len);
88002ac6454SAndrew Thompson 			ifp->if_ierrors++;
88102ac6454SAndrew Thompson 			goto tr_setup;
88202ac6454SAndrew Thompson 		}
88302ac6454SAndrew Thompson 
88402ac6454SAndrew Thompson 		len -= RT2573_RX_DESC_SIZE;
88502ac6454SAndrew Thompson 		usb2_copy_out(xfer->frbuffers, 0, &sc->sc_rx_desc,
88602ac6454SAndrew Thompson 		    RT2573_RX_DESC_SIZE);
88702ac6454SAndrew Thompson 
88802ac6454SAndrew Thompson 		rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
88902ac6454SAndrew Thompson 		flags = le32toh(sc->sc_rx_desc.flags);
89002ac6454SAndrew Thompson 		if (flags & RT2573_RX_CRC_ERROR) {
89102ac6454SAndrew Thompson 			/*
89202ac6454SAndrew Thompson 		         * This should not happen since we did not
89302ac6454SAndrew Thompson 		         * request to receive those frames when we
89402ac6454SAndrew Thompson 		         * filled RUM_TXRX_CSR2:
89502ac6454SAndrew Thompson 		         */
89602ac6454SAndrew Thompson 			DPRINTFN(5, "PHY or CRC error\n");
89702ac6454SAndrew Thompson 			ifp->if_ierrors++;
89802ac6454SAndrew Thompson 			goto tr_setup;
89902ac6454SAndrew Thompson 		}
90002ac6454SAndrew Thompson 
90102ac6454SAndrew Thompson 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
90202ac6454SAndrew Thompson 		if (m == NULL) {
90302ac6454SAndrew Thompson 			DPRINTF("could not allocate mbuf\n");
90402ac6454SAndrew Thompson 			ifp->if_ierrors++;
90502ac6454SAndrew Thompson 			goto tr_setup;
90602ac6454SAndrew Thompson 		}
90702ac6454SAndrew Thompson 		usb2_copy_out(xfer->frbuffers, RT2573_RX_DESC_SIZE,
90802ac6454SAndrew Thompson 		    mtod(m, uint8_t *), len);
90902ac6454SAndrew Thompson 
91002ac6454SAndrew Thompson 		/* finalize mbuf */
91102ac6454SAndrew Thompson 		m->m_pkthdr.rcvif = ifp;
91202ac6454SAndrew Thompson 		m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
91302ac6454SAndrew Thompson 
91402ac6454SAndrew Thompson 		if (bpf_peers_present(ifp->if_bpf)) {
91502ac6454SAndrew Thompson 			struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
91602ac6454SAndrew Thompson 
91702ac6454SAndrew Thompson 			tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
91802ac6454SAndrew Thompson 			tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
91902ac6454SAndrew Thompson 			    (flags & RT2573_RX_OFDM) ?
92002ac6454SAndrew Thompson 			    IEEE80211_T_OFDM : IEEE80211_T_CCK);
92102ac6454SAndrew Thompson 			tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
92202ac6454SAndrew Thompson 			tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
92302ac6454SAndrew Thompson 			tap->wr_antenna = sc->rx_ant;
92402ac6454SAndrew Thompson 			tap->wr_antsignal = rssi;
92502ac6454SAndrew Thompson 
92602ac6454SAndrew Thompson 			bpf_mtap2(ifp->if_bpf, tap, sc->sc_rxtap_len, m);
92702ac6454SAndrew Thompson 		}
92802ac6454SAndrew Thompson 		/* FALLTHROUGH */
92902ac6454SAndrew Thompson 	case USB_ST_SETUP:
93002ac6454SAndrew Thompson tr_setup:
93102ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
93202ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
93302ac6454SAndrew Thompson 
93402ac6454SAndrew Thompson 		/*
93502ac6454SAndrew Thompson 		 * At the end of a USB callback it is always safe to unlock
93602ac6454SAndrew Thompson 		 * the private mutex of a device! That is why we do the
93702ac6454SAndrew Thompson 		 * "ieee80211_input" here, and not some lines up!
93802ac6454SAndrew Thompson 		 */
93902ac6454SAndrew Thompson 		if (m) {
94002ac6454SAndrew Thompson 			RUM_UNLOCK(sc);
94102ac6454SAndrew Thompson 			ni = ieee80211_find_rxnode(ic,
94202ac6454SAndrew Thompson 			    mtod(m, struct ieee80211_frame_min *));
94302ac6454SAndrew Thompson 			if (ni != NULL) {
94402ac6454SAndrew Thompson 				(void) ieee80211_input(ni, m, rssi,
94502ac6454SAndrew Thompson 				    RT2573_NOISE_FLOOR, 0);
94602ac6454SAndrew Thompson 				ieee80211_free_node(ni);
94702ac6454SAndrew Thompson 			} else
94802ac6454SAndrew Thompson 				(void) ieee80211_input_all(ic, m, rssi,
94902ac6454SAndrew Thompson 				    RT2573_NOISE_FLOOR, 0);
95002ac6454SAndrew Thompson 			RUM_LOCK(sc);
95102ac6454SAndrew Thompson 		}
95202ac6454SAndrew Thompson 		return;
95302ac6454SAndrew Thompson 
95402ac6454SAndrew Thompson 	default:			/* Error */
95502ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
95602ac6454SAndrew Thompson 			/* try to clear stall first */
95702ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
95802ac6454SAndrew Thompson 			goto tr_setup;
95902ac6454SAndrew Thompson 		}
96002ac6454SAndrew Thompson 		return;
96102ac6454SAndrew Thompson 	}
96202ac6454SAndrew Thompson }
96302ac6454SAndrew Thompson 
96402ac6454SAndrew Thompson static uint8_t
96502ac6454SAndrew Thompson rum_plcp_signal(int rate)
96602ac6454SAndrew Thompson {
96702ac6454SAndrew Thompson 	switch (rate) {
96802ac6454SAndrew Thompson 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
96902ac6454SAndrew Thompson 	case 12:	return 0xb;
97002ac6454SAndrew Thompson 	case 18:	return 0xf;
97102ac6454SAndrew Thompson 	case 24:	return 0xa;
97202ac6454SAndrew Thompson 	case 36:	return 0xe;
97302ac6454SAndrew Thompson 	case 48:	return 0x9;
97402ac6454SAndrew Thompson 	case 72:	return 0xd;
97502ac6454SAndrew Thompson 	case 96:	return 0x8;
97602ac6454SAndrew Thompson 	case 108:	return 0xc;
97702ac6454SAndrew Thompson 
97802ac6454SAndrew Thompson 	/* CCK rates (NB: not IEEE std, device-specific) */
97902ac6454SAndrew Thompson 	case 2:		return 0x0;
98002ac6454SAndrew Thompson 	case 4:		return 0x1;
98102ac6454SAndrew Thompson 	case 11:	return 0x2;
98202ac6454SAndrew Thompson 	case 22:	return 0x3;
98302ac6454SAndrew Thompson 	}
98402ac6454SAndrew Thompson 	return 0xff;		/* XXX unsupported/unknown rate */
98502ac6454SAndrew Thompson }
98602ac6454SAndrew Thompson 
98702ac6454SAndrew Thompson static void
98802ac6454SAndrew Thompson rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
98902ac6454SAndrew Thompson     uint32_t flags, uint16_t xflags, int len, int rate)
99002ac6454SAndrew Thompson {
99102ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
99202ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
99302ac6454SAndrew Thompson 	uint16_t plcp_length;
99402ac6454SAndrew Thompson 	int remainder;
99502ac6454SAndrew Thompson 
99602ac6454SAndrew Thompson 	desc->flags = htole32(flags);
99702ac6454SAndrew Thompson 	desc->flags |= htole32(RT2573_TX_VALID);
99802ac6454SAndrew Thompson 	desc->flags |= htole32(len << 16);
99902ac6454SAndrew Thompson 
100002ac6454SAndrew Thompson 	desc->xflags = htole16(xflags);
100102ac6454SAndrew Thompson 
100202ac6454SAndrew Thompson 	desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) |
100302ac6454SAndrew Thompson 	    RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10));
100402ac6454SAndrew Thompson 
100502ac6454SAndrew Thompson 	/* setup PLCP fields */
100602ac6454SAndrew Thompson 	desc->plcp_signal  = rum_plcp_signal(rate);
100702ac6454SAndrew Thompson 	desc->plcp_service = 4;
100802ac6454SAndrew Thompson 
100902ac6454SAndrew Thompson 	len += IEEE80211_CRC_LEN;
101026d39e2cSSam Leffler 	if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
101102ac6454SAndrew Thompson 		desc->flags |= htole32(RT2573_TX_OFDM);
101202ac6454SAndrew Thompson 
101302ac6454SAndrew Thompson 		plcp_length = len & 0xfff;
101402ac6454SAndrew Thompson 		desc->plcp_length_hi = plcp_length >> 6;
101502ac6454SAndrew Thompson 		desc->plcp_length_lo = plcp_length & 0x3f;
101602ac6454SAndrew Thompson 	} else {
101702ac6454SAndrew Thompson 		plcp_length = (16 * len + rate - 1) / rate;
101802ac6454SAndrew Thompson 		if (rate == 22) {
101902ac6454SAndrew Thompson 			remainder = (16 * len) % 22;
102002ac6454SAndrew Thompson 			if (remainder != 0 && remainder < 7)
102102ac6454SAndrew Thompson 				desc->plcp_service |= RT2573_PLCP_LENGEXT;
102202ac6454SAndrew Thompson 		}
102302ac6454SAndrew Thompson 		desc->plcp_length_hi = plcp_length >> 8;
102402ac6454SAndrew Thompson 		desc->plcp_length_lo = plcp_length & 0xff;
102502ac6454SAndrew Thompson 
102602ac6454SAndrew Thompson 		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
102702ac6454SAndrew Thompson 			desc->plcp_signal |= 0x08;
102802ac6454SAndrew Thompson 	}
102902ac6454SAndrew Thompson }
103002ac6454SAndrew Thompson 
103102ac6454SAndrew Thompson static int
103202ac6454SAndrew Thompson rum_sendprot(struct rum_softc *sc,
103302ac6454SAndrew Thompson     const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
103402ac6454SAndrew Thompson {
103502ac6454SAndrew Thompson 	struct ieee80211com *ic = ni->ni_ic;
103602ac6454SAndrew Thompson 	const struct ieee80211_frame *wh;
103702ac6454SAndrew Thompson 	struct rum_tx_data *data;
103802ac6454SAndrew Thompson 	struct mbuf *mprot;
103902ac6454SAndrew Thompson 	int protrate, ackrate, pktlen, flags, isshort;
104002ac6454SAndrew Thompson 	uint16_t dur;
104102ac6454SAndrew Thompson 
104202ac6454SAndrew Thompson 	RUM_LOCK_ASSERT(sc, MA_OWNED);
104302ac6454SAndrew Thompson 	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
104402ac6454SAndrew Thompson 	    ("protection %d", prot));
104502ac6454SAndrew Thompson 
104602ac6454SAndrew Thompson 	wh = mtod(m, const struct ieee80211_frame *);
104702ac6454SAndrew Thompson 	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
104802ac6454SAndrew Thompson 
104926d39e2cSSam Leffler 	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
105026d39e2cSSam Leffler 	ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
105102ac6454SAndrew Thompson 
105202ac6454SAndrew Thompson 	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
105326d39e2cSSam Leffler 	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort);
105426d39e2cSSam Leffler 	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
105502ac6454SAndrew Thompson 	flags = RT2573_TX_MORE_FRAG;
105602ac6454SAndrew Thompson 	if (prot == IEEE80211_PROT_RTSCTS) {
105702ac6454SAndrew Thompson 		/* NB: CTS is the same size as an ACK */
105826d39e2cSSam Leffler 		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
105902ac6454SAndrew Thompson 		flags |= RT2573_TX_NEED_ACK;
106002ac6454SAndrew Thompson 		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
106102ac6454SAndrew Thompson 	} else {
106202ac6454SAndrew Thompson 		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
106302ac6454SAndrew Thompson 	}
106402ac6454SAndrew Thompson 	if (mprot == NULL) {
106502ac6454SAndrew Thompson 		/* XXX stat + msg */
106677ddf3d3SAndrew Thompson 		return (ENOBUFS);
106702ac6454SAndrew Thompson 	}
106802ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
106902ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
107002ac6454SAndrew Thompson 	sc->tx_nfree--;
107102ac6454SAndrew Thompson 
107202ac6454SAndrew Thompson 	data->m = mprot;
107302ac6454SAndrew Thompson 	data->ni = ieee80211_ref_node(ni);
107402ac6454SAndrew Thompson 	data->rate = protrate;
107502ac6454SAndrew Thompson 	rum_setup_tx_desc(sc, &data->desc, flags, 0, mprot->m_pkthdr.len, protrate);
107602ac6454SAndrew Thompson 
107702ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
107802ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
107902ac6454SAndrew Thompson 
108002ac6454SAndrew Thompson 	return 0;
108102ac6454SAndrew Thompson }
108202ac6454SAndrew Thompson 
108302ac6454SAndrew Thompson static int
108402ac6454SAndrew Thompson rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
108502ac6454SAndrew Thompson {
108602ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
108702ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
108802ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
108902ac6454SAndrew Thompson 	struct rum_tx_data *data;
109002ac6454SAndrew Thompson 	struct ieee80211_frame *wh;
109102ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
109202ac6454SAndrew Thompson 	struct ieee80211_key *k;
109302ac6454SAndrew Thompson 	uint32_t flags = 0;
109402ac6454SAndrew Thompson 	uint16_t dur;
109502ac6454SAndrew Thompson 
109602ac6454SAndrew Thompson 	RUM_LOCK_ASSERT(sc, MA_OWNED);
109702ac6454SAndrew Thompson 
109802ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
109902ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
110002ac6454SAndrew Thompson 	sc->tx_nfree--;
110102ac6454SAndrew Thompson 
110202ac6454SAndrew Thompson 	wh = mtod(m0, struct ieee80211_frame *);
110302ac6454SAndrew Thompson 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
110402ac6454SAndrew Thompson 		k = ieee80211_crypto_encap(ni, m0);
110502ac6454SAndrew Thompson 		if (k == NULL) {
110602ac6454SAndrew Thompson 			m_freem(m0);
110702ac6454SAndrew Thompson 			return ENOBUFS;
110802ac6454SAndrew Thompson 		}
110902ac6454SAndrew Thompson 		wh = mtod(m0, struct ieee80211_frame *);
111002ac6454SAndrew Thompson 	}
111102ac6454SAndrew Thompson 
111202ac6454SAndrew Thompson 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
111302ac6454SAndrew Thompson 
111402ac6454SAndrew Thompson 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
111502ac6454SAndrew Thompson 		flags |= RT2573_TX_NEED_ACK;
111602ac6454SAndrew Thompson 
111726d39e2cSSam Leffler 		dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
111802ac6454SAndrew Thompson 		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
111902ac6454SAndrew Thompson 		*(uint16_t *)wh->i_dur = htole16(dur);
112002ac6454SAndrew Thompson 
112102ac6454SAndrew Thompson 		/* tell hardware to add timestamp for probe responses */
112202ac6454SAndrew Thompson 		if ((wh->i_fc[0] &
112302ac6454SAndrew Thompson 		    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
112402ac6454SAndrew Thompson 		    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
112502ac6454SAndrew Thompson 			flags |= RT2573_TX_TIMESTAMP;
112602ac6454SAndrew Thompson 	}
112702ac6454SAndrew Thompson 
112802ac6454SAndrew Thompson 	data->m = m0;
112902ac6454SAndrew Thompson 	data->ni = ni;
113002ac6454SAndrew Thompson 	data->rate = tp->mgmtrate;
113102ac6454SAndrew Thompson 
113202ac6454SAndrew Thompson 	rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, tp->mgmtrate);
113302ac6454SAndrew Thompson 
113402ac6454SAndrew Thompson 	DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
113502ac6454SAndrew Thompson 	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
113602ac6454SAndrew Thompson 
113702ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
113802ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
113902ac6454SAndrew Thompson 
114077ddf3d3SAndrew Thompson 	return (0);
114102ac6454SAndrew Thompson }
114202ac6454SAndrew Thompson 
114302ac6454SAndrew Thompson static int
114402ac6454SAndrew Thompson rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
114502ac6454SAndrew Thompson     const struct ieee80211_bpf_params *params)
114602ac6454SAndrew Thompson {
114702ac6454SAndrew Thompson 	struct rum_tx_data *data;
114802ac6454SAndrew Thompson 	uint32_t flags;
114902ac6454SAndrew Thompson 	int rate, error;
115002ac6454SAndrew Thompson 
115102ac6454SAndrew Thompson 	RUM_LOCK_ASSERT(sc, MA_OWNED);
115202ac6454SAndrew Thompson 	KASSERT(params != NULL, ("no raw xmit params"));
115302ac6454SAndrew Thompson 
115402ac6454SAndrew Thompson 	rate = params->ibp_rate0 & IEEE80211_RATE_VAL;
115502ac6454SAndrew Thompson 	/* XXX validate */
115602ac6454SAndrew Thompson 	if (rate == 0) {
115702ac6454SAndrew Thompson 		m_freem(m0);
115802ac6454SAndrew Thompson 		return EINVAL;
115902ac6454SAndrew Thompson 	}
116002ac6454SAndrew Thompson 	flags = 0;
116102ac6454SAndrew Thompson 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
116202ac6454SAndrew Thompson 		flags |= RT2573_TX_NEED_ACK;
116302ac6454SAndrew Thompson 	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
116402ac6454SAndrew Thompson 		error = rum_sendprot(sc, m0, ni,
116502ac6454SAndrew Thompson 		    params->ibp_flags & IEEE80211_BPF_RTS ?
116602ac6454SAndrew Thompson 			 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
116702ac6454SAndrew Thompson 		    rate);
116896ca458fSAndrew Thompson 		if (error || sc->tx_nfree == 0) {
116902ac6454SAndrew Thompson 			m_freem(m0);
117096ca458fSAndrew Thompson 			return ENOBUFS;
117102ac6454SAndrew Thompson 		}
117202ac6454SAndrew Thompson 		flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
117302ac6454SAndrew Thompson 	}
117402ac6454SAndrew Thompson 
117596ca458fSAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
117696ca458fSAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
117796ca458fSAndrew Thompson 	sc->tx_nfree--;
117896ca458fSAndrew Thompson 
117902ac6454SAndrew Thompson 	data->m = m0;
118002ac6454SAndrew Thompson 	data->ni = ni;
118102ac6454SAndrew Thompson 	data->rate = rate;
118202ac6454SAndrew Thompson 
118302ac6454SAndrew Thompson 	/* XXX need to setup descriptor ourself */
118402ac6454SAndrew Thompson 	rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
118502ac6454SAndrew Thompson 
118602ac6454SAndrew Thompson 	DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
118702ac6454SAndrew Thompson 	    m0->m_pkthdr.len, rate);
118802ac6454SAndrew Thompson 
118902ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
119002ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
119102ac6454SAndrew Thompson 
119202ac6454SAndrew Thompson 	return 0;
119302ac6454SAndrew Thompson }
119402ac6454SAndrew Thompson 
119502ac6454SAndrew Thompson static int
119602ac6454SAndrew Thompson rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
119702ac6454SAndrew Thompson {
119802ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
119902ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
120002ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
120102ac6454SAndrew Thompson 	struct rum_tx_data *data;
120202ac6454SAndrew Thompson 	struct ieee80211_frame *wh;
120302ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
120402ac6454SAndrew Thompson 	struct ieee80211_key *k;
120502ac6454SAndrew Thompson 	uint32_t flags = 0;
120602ac6454SAndrew Thompson 	uint16_t dur;
120702ac6454SAndrew Thompson 	int error, rate;
120802ac6454SAndrew Thompson 
120902ac6454SAndrew Thompson 	RUM_LOCK_ASSERT(sc, MA_OWNED);
121002ac6454SAndrew Thompson 
121102ac6454SAndrew Thompson 	wh = mtod(m0, struct ieee80211_frame *);
121202ac6454SAndrew Thompson 
121302ac6454SAndrew Thompson 	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
121402ac6454SAndrew Thompson 	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
121502ac6454SAndrew Thompson 		rate = tp->mcastrate;
121602ac6454SAndrew Thompson 	else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
121702ac6454SAndrew Thompson 		rate = tp->ucastrate;
121802ac6454SAndrew Thompson 	else
121902ac6454SAndrew Thompson 		rate = ni->ni_txrate;
122002ac6454SAndrew Thompson 
122102ac6454SAndrew Thompson 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
122202ac6454SAndrew Thompson 		k = ieee80211_crypto_encap(ni, m0);
122302ac6454SAndrew Thompson 		if (k == NULL) {
122402ac6454SAndrew Thompson 			m_freem(m0);
122502ac6454SAndrew Thompson 			return ENOBUFS;
122602ac6454SAndrew Thompson 		}
122702ac6454SAndrew Thompson 
122802ac6454SAndrew Thompson 		/* packet header may have moved, reset our local pointer */
122902ac6454SAndrew Thompson 		wh = mtod(m0, struct ieee80211_frame *);
123002ac6454SAndrew Thompson 	}
123102ac6454SAndrew Thompson 
123202ac6454SAndrew Thompson 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
123302ac6454SAndrew Thompson 		int prot = IEEE80211_PROT_NONE;
123402ac6454SAndrew Thompson 		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
123502ac6454SAndrew Thompson 			prot = IEEE80211_PROT_RTSCTS;
123602ac6454SAndrew Thompson 		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
123726d39e2cSSam Leffler 		    ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
123802ac6454SAndrew Thompson 			prot = ic->ic_protmode;
123902ac6454SAndrew Thompson 		if (prot != IEEE80211_PROT_NONE) {
124002ac6454SAndrew Thompson 			error = rum_sendprot(sc, m0, ni, prot, rate);
124196ca458fSAndrew Thompson 			if (error || sc->tx_nfree == 0) {
124202ac6454SAndrew Thompson 				m_freem(m0);
124396ca458fSAndrew Thompson 				return ENOBUFS;
124402ac6454SAndrew Thompson 			}
124502ac6454SAndrew Thompson 			flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
124602ac6454SAndrew Thompson 		}
124702ac6454SAndrew Thompson 	}
124802ac6454SAndrew Thompson 
124902ac6454SAndrew Thompson 	data = STAILQ_FIRST(&sc->tx_free);
125002ac6454SAndrew Thompson 	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
125102ac6454SAndrew Thompson 	sc->tx_nfree--;
125202ac6454SAndrew Thompson 
125302ac6454SAndrew Thompson 	data->m = m0;
125402ac6454SAndrew Thompson 	data->ni = ni;
125502ac6454SAndrew Thompson 	data->rate = rate;
125602ac6454SAndrew Thompson 
125702ac6454SAndrew Thompson 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
125802ac6454SAndrew Thompson 		flags |= RT2573_TX_NEED_ACK;
125902ac6454SAndrew Thompson 		flags |= RT2573_TX_MORE_FRAG;
126002ac6454SAndrew Thompson 
126126d39e2cSSam Leffler 		dur = ieee80211_ack_duration(ic->ic_rt, rate,
126202ac6454SAndrew Thompson 		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
126302ac6454SAndrew Thompson 		*(uint16_t *)wh->i_dur = htole16(dur);
126402ac6454SAndrew Thompson 	}
126502ac6454SAndrew Thompson 
126602ac6454SAndrew Thompson 	rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
126702ac6454SAndrew Thompson 
126802ac6454SAndrew Thompson 	DPRINTFN(10, "sending frame len=%d rate=%d\n",
126902ac6454SAndrew Thompson 	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
127002ac6454SAndrew Thompson 
127102ac6454SAndrew Thompson 	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
127202ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
127302ac6454SAndrew Thompson 
127402ac6454SAndrew Thompson 	return 0;
127502ac6454SAndrew Thompson }
127602ac6454SAndrew Thompson 
127702ac6454SAndrew Thompson static void
127802ac6454SAndrew Thompson rum_start(struct ifnet *ifp)
127902ac6454SAndrew Thompson {
128002ac6454SAndrew Thompson 	struct rum_softc *sc = ifp->if_softc;
128102ac6454SAndrew Thompson 	struct ieee80211_node *ni;
128202ac6454SAndrew Thompson 	struct mbuf *m;
128302ac6454SAndrew Thompson 
128402ac6454SAndrew Thompson 	RUM_LOCK(sc);
128502ac6454SAndrew Thompson 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
128602ac6454SAndrew Thompson 		RUM_UNLOCK(sc);
128702ac6454SAndrew Thompson 		return;
128802ac6454SAndrew Thompson 	}
128902ac6454SAndrew Thompson 	for (;;) {
129002ac6454SAndrew Thompson 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
129102ac6454SAndrew Thompson 		if (m == NULL)
129202ac6454SAndrew Thompson 			break;
129396ca458fSAndrew Thompson 		if (sc->tx_nfree < RUM_TX_MINFREE) {
129402ac6454SAndrew Thompson 			IFQ_DRV_PREPEND(&ifp->if_snd, m);
129502ac6454SAndrew Thompson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
129602ac6454SAndrew Thompson 			break;
129702ac6454SAndrew Thompson 		}
129802ac6454SAndrew Thompson 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
129902ac6454SAndrew Thompson 		if (rum_tx_data(sc, m, ni) != 0) {
130002ac6454SAndrew Thompson 			ieee80211_free_node(ni);
130102ac6454SAndrew Thompson 			ifp->if_oerrors++;
130202ac6454SAndrew Thompson 			break;
130302ac6454SAndrew Thompson 		}
130402ac6454SAndrew Thompson 	}
130502ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
130602ac6454SAndrew Thompson }
130702ac6454SAndrew Thompson 
130802ac6454SAndrew Thompson static int
130902ac6454SAndrew Thompson rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
131002ac6454SAndrew Thompson {
131102ac6454SAndrew Thompson 	struct rum_softc *sc = ifp->if_softc;
131202ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
131302ac6454SAndrew Thompson 	struct ifreq *ifr = (struct ifreq *) data;
131402ac6454SAndrew Thompson 	int error = 0, startall = 0;
131502ac6454SAndrew Thompson 
131602ac6454SAndrew Thompson 	switch (cmd) {
131702ac6454SAndrew Thompson 	case SIOCSIFFLAGS:
131802ac6454SAndrew Thompson 		RUM_LOCK(sc);
131902ac6454SAndrew Thompson 		if (ifp->if_flags & IFF_UP) {
132002ac6454SAndrew Thompson 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
13215efea30fSAndrew Thompson 				rum_init_locked(sc);
132202ac6454SAndrew Thompson 				startall = 1;
132302ac6454SAndrew Thompson 			} else
13245efea30fSAndrew Thompson 				rum_setpromisc(sc);
132502ac6454SAndrew Thompson 		} else {
13265efea30fSAndrew Thompson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
13275efea30fSAndrew Thompson 				rum_stop(sc);
132802ac6454SAndrew Thompson 		}
132902ac6454SAndrew Thompson 		RUM_UNLOCK(sc);
133002ac6454SAndrew Thompson 		if (startall)
133102ac6454SAndrew Thompson 			ieee80211_start_all(ic);
133202ac6454SAndrew Thompson 		break;
133302ac6454SAndrew Thompson 	case SIOCGIFMEDIA:
133402ac6454SAndrew Thompson 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
133502ac6454SAndrew Thompson 		break;
133602ac6454SAndrew Thompson 	case SIOCGIFADDR:
133702ac6454SAndrew Thompson 		error = ether_ioctl(ifp, cmd, data);
133802ac6454SAndrew Thompson 		break;
133902ac6454SAndrew Thompson 	default:
134002ac6454SAndrew Thompson 		error = EINVAL;
134102ac6454SAndrew Thompson 		break;
134202ac6454SAndrew Thompson 	}
134302ac6454SAndrew Thompson 	return error;
134402ac6454SAndrew Thompson }
134502ac6454SAndrew Thompson 
134602ac6454SAndrew Thompson static void
134702ac6454SAndrew Thompson rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
134802ac6454SAndrew Thompson {
134902ac6454SAndrew Thompson 	struct usb2_device_request req;
135002ac6454SAndrew Thompson 	usb2_error_t error;
135102ac6454SAndrew Thompson 
135202ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
135302ac6454SAndrew Thompson 	req.bRequest = RT2573_READ_EEPROM;
135402ac6454SAndrew Thompson 	USETW(req.wValue, 0);
135502ac6454SAndrew Thompson 	USETW(req.wIndex, addr);
135602ac6454SAndrew Thompson 	USETW(req.wLength, len);
135702ac6454SAndrew Thompson 
135802ac6454SAndrew Thompson 	error = rum_do_request(sc, &req, buf);
135902ac6454SAndrew Thompson 	if (error != 0) {
136002ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
136102ac6454SAndrew Thompson 		    usb2_errstr(error));
136202ac6454SAndrew Thompson 	}
136302ac6454SAndrew Thompson }
136402ac6454SAndrew Thompson 
136502ac6454SAndrew Thompson static uint32_t
136602ac6454SAndrew Thompson rum_read(struct rum_softc *sc, uint16_t reg)
136702ac6454SAndrew Thompson {
136802ac6454SAndrew Thompson 	uint32_t val;
136902ac6454SAndrew Thompson 
137002ac6454SAndrew Thompson 	rum_read_multi(sc, reg, &val, sizeof val);
137102ac6454SAndrew Thompson 
137202ac6454SAndrew Thompson 	return le32toh(val);
137302ac6454SAndrew Thompson }
137402ac6454SAndrew Thompson 
137502ac6454SAndrew Thompson static void
137602ac6454SAndrew Thompson rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
137702ac6454SAndrew Thompson {
137802ac6454SAndrew Thompson 	struct usb2_device_request req;
137902ac6454SAndrew Thompson 	usb2_error_t error;
138002ac6454SAndrew Thompson 
138102ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
138202ac6454SAndrew Thompson 	req.bRequest = RT2573_READ_MULTI_MAC;
138302ac6454SAndrew Thompson 	USETW(req.wValue, 0);
138402ac6454SAndrew Thompson 	USETW(req.wIndex, reg);
138502ac6454SAndrew Thompson 	USETW(req.wLength, len);
138602ac6454SAndrew Thompson 
138702ac6454SAndrew Thompson 	error = rum_do_request(sc, &req, buf);
138802ac6454SAndrew Thompson 	if (error != 0) {
138902ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
139002ac6454SAndrew Thompson 		    "could not multi read MAC register: %s\n",
139102ac6454SAndrew Thompson 		    usb2_errstr(error));
139202ac6454SAndrew Thompson 	}
139302ac6454SAndrew Thompson }
139402ac6454SAndrew Thompson 
139577ddf3d3SAndrew Thompson static usb2_error_t
139602ac6454SAndrew Thompson rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
139702ac6454SAndrew Thompson {
139802ac6454SAndrew Thompson 	uint32_t tmp = htole32(val);
139902ac6454SAndrew Thompson 
140077ddf3d3SAndrew Thompson 	return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
140102ac6454SAndrew Thompson }
140202ac6454SAndrew Thompson 
140377ddf3d3SAndrew Thompson static usb2_error_t
140402ac6454SAndrew Thompson rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
140502ac6454SAndrew Thompson {
140602ac6454SAndrew Thompson 	struct usb2_device_request req;
140702ac6454SAndrew Thompson 	usb2_error_t error;
140802ac6454SAndrew Thompson 
140902ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
141002ac6454SAndrew Thompson 	req.bRequest = RT2573_WRITE_MULTI_MAC;
141102ac6454SAndrew Thompson 	USETW(req.wValue, 0);
141202ac6454SAndrew Thompson 	USETW(req.wIndex, reg);
141302ac6454SAndrew Thompson 	USETW(req.wLength, len);
141402ac6454SAndrew Thompson 
141502ac6454SAndrew Thompson 	error = rum_do_request(sc, &req, buf);
141602ac6454SAndrew Thompson 	if (error != 0) {
141702ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
141802ac6454SAndrew Thompson 		    "could not multi write MAC register: %s\n",
141902ac6454SAndrew Thompson 		    usb2_errstr(error));
142002ac6454SAndrew Thompson 	}
142177ddf3d3SAndrew Thompson 	return (error);
142202ac6454SAndrew Thompson }
142302ac6454SAndrew Thompson 
142402ac6454SAndrew Thompson static void
142502ac6454SAndrew Thompson rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
142602ac6454SAndrew Thompson {
142702ac6454SAndrew Thompson 	uint32_t tmp;
142802ac6454SAndrew Thompson 	int ntries;
142902ac6454SAndrew Thompson 
143077ddf3d3SAndrew Thompson 	DPRINTFN(2, "reg=0x%08x\n", reg);
143177ddf3d3SAndrew Thompson 
143202ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
143302ac6454SAndrew Thompson 		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
143402ac6454SAndrew Thompson 			break;
143502ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
143602ac6454SAndrew Thompson 			break;
143702ac6454SAndrew Thompson 	}
143802ac6454SAndrew Thompson 	if (ntries == 100) {
143902ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not write to BBP\n");
144002ac6454SAndrew Thompson 		return;
144102ac6454SAndrew Thompson 	}
144202ac6454SAndrew Thompson 
144302ac6454SAndrew Thompson 	tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
144402ac6454SAndrew Thompson 	rum_write(sc, RT2573_PHY_CSR3, tmp);
144502ac6454SAndrew Thompson }
144602ac6454SAndrew Thompson 
144702ac6454SAndrew Thompson static uint8_t
144802ac6454SAndrew Thompson rum_bbp_read(struct rum_softc *sc, uint8_t reg)
144902ac6454SAndrew Thompson {
145002ac6454SAndrew Thompson 	uint32_t val;
145102ac6454SAndrew Thompson 	int ntries;
145202ac6454SAndrew Thompson 
145377ddf3d3SAndrew Thompson 	DPRINTFN(2, "reg=0x%08x\n", reg);
145477ddf3d3SAndrew Thompson 
145502ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
145602ac6454SAndrew Thompson 		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
145702ac6454SAndrew Thompson 			break;
145802ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
145902ac6454SAndrew Thompson 			break;
146002ac6454SAndrew Thompson 	}
146102ac6454SAndrew Thompson 	if (ntries == 100) {
146202ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not read BBP\n");
146302ac6454SAndrew Thompson 		return 0;
146402ac6454SAndrew Thompson 	}
146502ac6454SAndrew Thompson 
146602ac6454SAndrew Thompson 	val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
146702ac6454SAndrew Thompson 	rum_write(sc, RT2573_PHY_CSR3, val);
146802ac6454SAndrew Thompson 
146902ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
147002ac6454SAndrew Thompson 		val = rum_read(sc, RT2573_PHY_CSR3);
147102ac6454SAndrew Thompson 		if (!(val & RT2573_BBP_BUSY))
147202ac6454SAndrew Thompson 			return val & 0xff;
147302ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
147402ac6454SAndrew Thompson 			break;
147502ac6454SAndrew Thompson 	}
147602ac6454SAndrew Thompson 
147702ac6454SAndrew Thompson 	device_printf(sc->sc_dev, "could not read BBP\n");
147802ac6454SAndrew Thompson 	return 0;
147902ac6454SAndrew Thompson }
148002ac6454SAndrew Thompson 
148102ac6454SAndrew Thompson static void
148202ac6454SAndrew Thompson rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
148302ac6454SAndrew Thompson {
148402ac6454SAndrew Thompson 	uint32_t tmp;
148502ac6454SAndrew Thompson 	int ntries;
148602ac6454SAndrew Thompson 
148702ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
148802ac6454SAndrew Thompson 		if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
148902ac6454SAndrew Thompson 			break;
149002ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
149102ac6454SAndrew Thompson 			break;
149202ac6454SAndrew Thompson 	}
149302ac6454SAndrew Thompson 	if (ntries == 100) {
149402ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not write to RF\n");
149502ac6454SAndrew Thompson 		return;
149602ac6454SAndrew Thompson 	}
149702ac6454SAndrew Thompson 
149802ac6454SAndrew Thompson 	tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
149902ac6454SAndrew Thompson 	    (reg & 3);
150002ac6454SAndrew Thompson 	rum_write(sc, RT2573_PHY_CSR4, tmp);
150102ac6454SAndrew Thompson 
150202ac6454SAndrew Thompson 	/* remember last written value in sc */
150302ac6454SAndrew Thompson 	sc->rf_regs[reg] = val;
150402ac6454SAndrew Thompson 
150502ac6454SAndrew Thompson 	DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
150602ac6454SAndrew Thompson }
150702ac6454SAndrew Thompson 
150802ac6454SAndrew Thompson static void
150902ac6454SAndrew Thompson rum_select_antenna(struct rum_softc *sc)
151002ac6454SAndrew Thompson {
151102ac6454SAndrew Thompson 	uint8_t bbp4, bbp77;
151202ac6454SAndrew Thompson 	uint32_t tmp;
151302ac6454SAndrew Thompson 
151402ac6454SAndrew Thompson 	bbp4  = rum_bbp_read(sc, 4);
151502ac6454SAndrew Thompson 	bbp77 = rum_bbp_read(sc, 77);
151602ac6454SAndrew Thompson 
151702ac6454SAndrew Thompson 	/* TBD */
151802ac6454SAndrew Thompson 
151902ac6454SAndrew Thompson 	/* make sure Rx is disabled before switching antenna */
152002ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
152102ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
152202ac6454SAndrew Thompson 
152302ac6454SAndrew Thompson 	rum_bbp_write(sc,  4, bbp4);
152402ac6454SAndrew Thompson 	rum_bbp_write(sc, 77, bbp77);
152502ac6454SAndrew Thompson 
152602ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
152702ac6454SAndrew Thompson }
152802ac6454SAndrew Thompson 
152902ac6454SAndrew Thompson /*
153002ac6454SAndrew Thompson  * Enable multi-rate retries for frames sent at OFDM rates.
153102ac6454SAndrew Thompson  * In 802.11b/g mode, allow fallback to CCK rates.
153202ac6454SAndrew Thompson  */
153302ac6454SAndrew Thompson static void
153402ac6454SAndrew Thompson rum_enable_mrr(struct rum_softc *sc)
153502ac6454SAndrew Thompson {
153602ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
153702ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
153802ac6454SAndrew Thompson 	uint32_t tmp;
153902ac6454SAndrew Thompson 
154002ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR4);
154102ac6454SAndrew Thompson 
154202ac6454SAndrew Thompson 	tmp &= ~RT2573_MRR_CCK_FALLBACK;
154302ac6454SAndrew Thompson 	if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan))
154402ac6454SAndrew Thompson 		tmp |= RT2573_MRR_CCK_FALLBACK;
154502ac6454SAndrew Thompson 	tmp |= RT2573_MRR_ENABLED;
154602ac6454SAndrew Thompson 
154702ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR4, tmp);
154802ac6454SAndrew Thompson }
154902ac6454SAndrew Thompson 
155002ac6454SAndrew Thompson static void
155102ac6454SAndrew Thompson rum_set_txpreamble(struct rum_softc *sc)
155202ac6454SAndrew Thompson {
155302ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
155402ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
155502ac6454SAndrew Thompson 	uint32_t tmp;
155602ac6454SAndrew Thompson 
155702ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR4);
155802ac6454SAndrew Thompson 
155902ac6454SAndrew Thompson 	tmp &= ~RT2573_SHORT_PREAMBLE;
156002ac6454SAndrew Thompson 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
156102ac6454SAndrew Thompson 		tmp |= RT2573_SHORT_PREAMBLE;
156202ac6454SAndrew Thompson 
156302ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR4, tmp);
156402ac6454SAndrew Thompson }
156502ac6454SAndrew Thompson 
156602ac6454SAndrew Thompson static void
156702ac6454SAndrew Thompson rum_set_basicrates(struct rum_softc *sc)
156802ac6454SAndrew Thompson {
156902ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
157002ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
157102ac6454SAndrew Thompson 
157202ac6454SAndrew Thompson 	/* update basic rate set */
157302ac6454SAndrew Thompson 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
157402ac6454SAndrew Thompson 		/* 11b basic rates: 1, 2Mbps */
157502ac6454SAndrew Thompson 		rum_write(sc, RT2573_TXRX_CSR5, 0x3);
157602ac6454SAndrew Thompson 	} else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
157702ac6454SAndrew Thompson 		/* 11a basic rates: 6, 12, 24Mbps */
157802ac6454SAndrew Thompson 		rum_write(sc, RT2573_TXRX_CSR5, 0x150);
157902ac6454SAndrew Thompson 	} else {
158002ac6454SAndrew Thompson 		/* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
158102ac6454SAndrew Thompson 		rum_write(sc, RT2573_TXRX_CSR5, 0xf);
158202ac6454SAndrew Thompson 	}
158302ac6454SAndrew Thompson }
158402ac6454SAndrew Thompson 
158502ac6454SAndrew Thompson /*
158602ac6454SAndrew Thompson  * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
158702ac6454SAndrew Thompson  * driver.
158802ac6454SAndrew Thompson  */
158902ac6454SAndrew Thompson static void
159002ac6454SAndrew Thompson rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
159102ac6454SAndrew Thompson {
159202ac6454SAndrew Thompson 	uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
159302ac6454SAndrew Thompson 	uint32_t tmp;
159402ac6454SAndrew Thompson 
159502ac6454SAndrew Thompson 	/* update all BBP registers that depend on the band */
159602ac6454SAndrew Thompson 	bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
159702ac6454SAndrew Thompson 	bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
159802ac6454SAndrew Thompson 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
159902ac6454SAndrew Thompson 		bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
160002ac6454SAndrew Thompson 		bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
160102ac6454SAndrew Thompson 	}
160202ac6454SAndrew Thompson 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
160302ac6454SAndrew Thompson 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
160402ac6454SAndrew Thompson 		bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
160502ac6454SAndrew Thompson 	}
160602ac6454SAndrew Thompson 
160702ac6454SAndrew Thompson 	sc->bbp17 = bbp17;
160802ac6454SAndrew Thompson 	rum_bbp_write(sc,  17, bbp17);
160902ac6454SAndrew Thompson 	rum_bbp_write(sc,  96, bbp96);
161002ac6454SAndrew Thompson 	rum_bbp_write(sc, 104, bbp104);
161102ac6454SAndrew Thompson 
161202ac6454SAndrew Thompson 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
161302ac6454SAndrew Thompson 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
161402ac6454SAndrew Thompson 		rum_bbp_write(sc, 75, 0x80);
161502ac6454SAndrew Thompson 		rum_bbp_write(sc, 86, 0x80);
161602ac6454SAndrew Thompson 		rum_bbp_write(sc, 88, 0x80);
161702ac6454SAndrew Thompson 	}
161802ac6454SAndrew Thompson 
161902ac6454SAndrew Thompson 	rum_bbp_write(sc, 35, bbp35);
162002ac6454SAndrew Thompson 	rum_bbp_write(sc, 97, bbp97);
162102ac6454SAndrew Thompson 	rum_bbp_write(sc, 98, bbp98);
162202ac6454SAndrew Thompson 
162302ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_PHY_CSR0);
162402ac6454SAndrew Thompson 	tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
162502ac6454SAndrew Thompson 	if (IEEE80211_IS_CHAN_2GHZ(c))
162602ac6454SAndrew Thompson 		tmp |= RT2573_PA_PE_2GHZ;
162702ac6454SAndrew Thompson 	else
162802ac6454SAndrew Thompson 		tmp |= RT2573_PA_PE_5GHZ;
162902ac6454SAndrew Thompson 	rum_write(sc, RT2573_PHY_CSR0, tmp);
163002ac6454SAndrew Thompson }
163102ac6454SAndrew Thompson 
163202ac6454SAndrew Thompson static void
163302ac6454SAndrew Thompson rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
163402ac6454SAndrew Thompson {
163502ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
163602ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
163702ac6454SAndrew Thompson 	const struct rfprog *rfprog;
163802ac6454SAndrew Thompson 	uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
163902ac6454SAndrew Thompson 	int8_t power;
164002ac6454SAndrew Thompson 	int i, chan;
164102ac6454SAndrew Thompson 
164202ac6454SAndrew Thompson 	chan = ieee80211_chan2ieee(ic, c);
164302ac6454SAndrew Thompson 	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
164402ac6454SAndrew Thompson 		return;
164502ac6454SAndrew Thompson 
164602ac6454SAndrew Thompson 	/* select the appropriate RF settings based on what EEPROM says */
164702ac6454SAndrew Thompson 	rfprog = (sc->rf_rev == RT2573_RF_5225 ||
164802ac6454SAndrew Thompson 		  sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
164902ac6454SAndrew Thompson 
165002ac6454SAndrew Thompson 	/* find the settings for this channel (we know it exists) */
165102ac6454SAndrew Thompson 	for (i = 0; rfprog[i].chan != chan; i++);
165202ac6454SAndrew Thompson 
165302ac6454SAndrew Thompson 	power = sc->txpow[i];
165402ac6454SAndrew Thompson 	if (power < 0) {
165502ac6454SAndrew Thompson 		bbp94 += power;
165602ac6454SAndrew Thompson 		power = 0;
165702ac6454SAndrew Thompson 	} else if (power > 31) {
165802ac6454SAndrew Thompson 		bbp94 += power - 31;
165902ac6454SAndrew Thompson 		power = 31;
166002ac6454SAndrew Thompson 	}
166102ac6454SAndrew Thompson 
166202ac6454SAndrew Thompson 	/*
166302ac6454SAndrew Thompson 	 * If we are switching from the 2GHz band to the 5GHz band or
166402ac6454SAndrew Thompson 	 * vice-versa, BBP registers need to be reprogrammed.
166502ac6454SAndrew Thompson 	 */
166602ac6454SAndrew Thompson 	if (c->ic_flags != ic->ic_curchan->ic_flags) {
166702ac6454SAndrew Thompson 		rum_select_band(sc, c);
166802ac6454SAndrew Thompson 		rum_select_antenna(sc);
166902ac6454SAndrew Thompson 	}
167002ac6454SAndrew Thompson 	ic->ic_curchan = c;
167102ac6454SAndrew Thompson 
167202ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
167302ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
167402ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
167502ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
167602ac6454SAndrew Thompson 
167702ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
167802ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
167902ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
168002ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
168102ac6454SAndrew Thompson 
168202ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
168302ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
168402ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
168502ac6454SAndrew Thompson 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
168602ac6454SAndrew Thompson 
168702ac6454SAndrew Thompson 	rum_pause(sc, hz / 100);
168802ac6454SAndrew Thompson 
168902ac6454SAndrew Thompson 	/* enable smart mode for MIMO-capable RFs */
169002ac6454SAndrew Thompson 	bbp3 = rum_bbp_read(sc, 3);
169102ac6454SAndrew Thompson 
169202ac6454SAndrew Thompson 	bbp3 &= ~RT2573_SMART_MODE;
169302ac6454SAndrew Thompson 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
169402ac6454SAndrew Thompson 		bbp3 |= RT2573_SMART_MODE;
169502ac6454SAndrew Thompson 
169602ac6454SAndrew Thompson 	rum_bbp_write(sc, 3, bbp3);
169702ac6454SAndrew Thompson 
169802ac6454SAndrew Thompson 	if (bbp94 != RT2573_BBPR94_DEFAULT)
169902ac6454SAndrew Thompson 		rum_bbp_write(sc, 94, bbp94);
170077ddf3d3SAndrew Thompson 
170177ddf3d3SAndrew Thompson 	/* give the chip some extra time to do the switchover */
170277ddf3d3SAndrew Thompson 	rum_pause(sc, hz / 100);
170302ac6454SAndrew Thompson }
170402ac6454SAndrew Thompson 
170502ac6454SAndrew Thompson /*
170602ac6454SAndrew Thompson  * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
170702ac6454SAndrew Thompson  * and HostAP operating modes.
170802ac6454SAndrew Thompson  */
170902ac6454SAndrew Thompson static void
171002ac6454SAndrew Thompson rum_enable_tsf_sync(struct rum_softc *sc)
171102ac6454SAndrew Thompson {
171202ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
171302ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
171402ac6454SAndrew Thompson 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
171502ac6454SAndrew Thompson 	uint32_t tmp;
171602ac6454SAndrew Thompson 
171702ac6454SAndrew Thompson 	if (vap->iv_opmode != IEEE80211_M_STA) {
171802ac6454SAndrew Thompson 		/*
171902ac6454SAndrew Thompson 		 * Change default 16ms TBTT adjustment to 8ms.
172002ac6454SAndrew Thompson 		 * Must be done before enabling beacon generation.
172102ac6454SAndrew Thompson 		 */
172202ac6454SAndrew Thompson 		rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
172302ac6454SAndrew Thompson 	}
172402ac6454SAndrew Thompson 
172502ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
172602ac6454SAndrew Thompson 
172702ac6454SAndrew Thompson 	/* set beacon interval (in 1/16ms unit) */
172802ac6454SAndrew Thompson 	tmp |= vap->iv_bss->ni_intval * 16;
172902ac6454SAndrew Thompson 
173002ac6454SAndrew Thompson 	tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
173102ac6454SAndrew Thompson 	if (vap->iv_opmode == IEEE80211_M_STA)
173202ac6454SAndrew Thompson 		tmp |= RT2573_TSF_MODE(1);
173302ac6454SAndrew Thompson 	else
173402ac6454SAndrew Thompson 		tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
173502ac6454SAndrew Thompson 
173602ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR9, tmp);
173702ac6454SAndrew Thompson }
173802ac6454SAndrew Thompson 
173902ac6454SAndrew Thompson static void
174002ac6454SAndrew Thompson rum_update_slot(struct ifnet *ifp)
174102ac6454SAndrew Thompson {
174202ac6454SAndrew Thompson 	struct rum_softc *sc = ifp->if_softc;
174302ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
174402ac6454SAndrew Thompson 	uint8_t slottime;
174502ac6454SAndrew Thompson 	uint32_t tmp;
174602ac6454SAndrew Thompson 
174702ac6454SAndrew Thompson 	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
174802ac6454SAndrew Thompson 
174902ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_MAC_CSR9);
175002ac6454SAndrew Thompson 	tmp = (tmp & ~0xff) | slottime;
175102ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR9, tmp);
175202ac6454SAndrew Thompson 
175302ac6454SAndrew Thompson 	DPRINTF("setting slot time to %uus\n", slottime);
175402ac6454SAndrew Thompson }
175502ac6454SAndrew Thompson 
175602ac6454SAndrew Thompson static void
175702ac6454SAndrew Thompson rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
175802ac6454SAndrew Thompson {
175902ac6454SAndrew Thompson 	uint32_t tmp;
176002ac6454SAndrew Thompson 
176102ac6454SAndrew Thompson 	tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
176202ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR4, tmp);
176302ac6454SAndrew Thompson 
176402ac6454SAndrew Thompson 	tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
176502ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR5, tmp);
176602ac6454SAndrew Thompson }
176702ac6454SAndrew Thompson 
176802ac6454SAndrew Thompson static void
176902ac6454SAndrew Thompson rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
177002ac6454SAndrew Thompson {
177102ac6454SAndrew Thompson 	uint32_t tmp;
177202ac6454SAndrew Thompson 
177302ac6454SAndrew Thompson 	tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
177402ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR2, tmp);
177502ac6454SAndrew Thompson 
177602ac6454SAndrew Thompson 	tmp = addr[4] | addr[5] << 8 | 0xff << 16;
177702ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR3, tmp);
177802ac6454SAndrew Thompson }
177902ac6454SAndrew Thompson 
178002ac6454SAndrew Thompson static void
17815efea30fSAndrew Thompson rum_setpromisc(struct rum_softc *sc)
178202ac6454SAndrew Thompson {
178302ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
178402ac6454SAndrew Thompson 	uint32_t tmp;
178502ac6454SAndrew Thompson 
178602ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
178702ac6454SAndrew Thompson 
178802ac6454SAndrew Thompson 	tmp &= ~RT2573_DROP_NOT_TO_ME;
178902ac6454SAndrew Thompson 	if (!(ifp->if_flags & IFF_PROMISC))
179002ac6454SAndrew Thompson 		tmp |= RT2573_DROP_NOT_TO_ME;
179102ac6454SAndrew Thompson 
179202ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
179302ac6454SAndrew Thompson 
179402ac6454SAndrew Thompson 	DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
179502ac6454SAndrew Thompson 	    "entering" : "leaving");
179602ac6454SAndrew Thompson }
179702ac6454SAndrew Thompson 
179877ddf3d3SAndrew Thompson static void
179977ddf3d3SAndrew Thompson rum_update_promisc(struct ifnet *ifp)
180077ddf3d3SAndrew Thompson {
180177ddf3d3SAndrew Thompson 	struct rum_softc *sc = ifp->if_softc;
180277ddf3d3SAndrew Thompson 
180377ddf3d3SAndrew Thompson 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
180477ddf3d3SAndrew Thompson 		return;
180577ddf3d3SAndrew Thompson 
180677ddf3d3SAndrew Thompson 	RUM_LOCK(sc);
18075efea30fSAndrew Thompson 	rum_setpromisc(sc);
180877ddf3d3SAndrew Thompson 	RUM_UNLOCK(sc);
180977ddf3d3SAndrew Thompson }
181077ddf3d3SAndrew Thompson 
181102ac6454SAndrew Thompson static const char *
181202ac6454SAndrew Thompson rum_get_rf(int rev)
181302ac6454SAndrew Thompson {
181402ac6454SAndrew Thompson 	switch (rev) {
181502ac6454SAndrew Thompson 	case RT2573_RF_2527:	return "RT2527 (MIMO XR)";
181602ac6454SAndrew Thompson 	case RT2573_RF_2528:	return "RT2528";
181702ac6454SAndrew Thompson 	case RT2573_RF_5225:	return "RT5225 (MIMO XR)";
181802ac6454SAndrew Thompson 	case RT2573_RF_5226:	return "RT5226";
181902ac6454SAndrew Thompson 	default:		return "unknown";
182002ac6454SAndrew Thompson 	}
182102ac6454SAndrew Thompson }
182202ac6454SAndrew Thompson 
182302ac6454SAndrew Thompson static void
182402ac6454SAndrew Thompson rum_read_eeprom(struct rum_softc *sc)
182502ac6454SAndrew Thompson {
182602ac6454SAndrew Thompson 	uint16_t val;
182702ac6454SAndrew Thompson #ifdef RUM_DEBUG
182802ac6454SAndrew Thompson 	int i;
182902ac6454SAndrew Thompson #endif
183002ac6454SAndrew Thompson 
183102ac6454SAndrew Thompson 	/* read MAC address */
183202ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_bssid, 6);
183302ac6454SAndrew Thompson 
183402ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
183502ac6454SAndrew Thompson 	val = le16toh(val);
183602ac6454SAndrew Thompson 	sc->rf_rev =   (val >> 11) & 0x1f;
183702ac6454SAndrew Thompson 	sc->hw_radio = (val >> 10) & 0x1;
183802ac6454SAndrew Thompson 	sc->rx_ant =   (val >> 4)  & 0x3;
183902ac6454SAndrew Thompson 	sc->tx_ant =   (val >> 2)  & 0x3;
184002ac6454SAndrew Thompson 	sc->nb_ant =   val & 0x3;
184102ac6454SAndrew Thompson 
184202ac6454SAndrew Thompson 	DPRINTF("RF revision=%d\n", sc->rf_rev);
184302ac6454SAndrew Thompson 
184402ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
184502ac6454SAndrew Thompson 	val = le16toh(val);
184602ac6454SAndrew Thompson 	sc->ext_5ghz_lna = (val >> 6) & 0x1;
184702ac6454SAndrew Thompson 	sc->ext_2ghz_lna = (val >> 4) & 0x1;
184802ac6454SAndrew Thompson 
184902ac6454SAndrew Thompson 	DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
185002ac6454SAndrew Thompson 	    sc->ext_2ghz_lna, sc->ext_5ghz_lna);
185102ac6454SAndrew Thompson 
185202ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
185302ac6454SAndrew Thompson 	val = le16toh(val);
185402ac6454SAndrew Thompson 	if ((val & 0xff) != 0xff)
185502ac6454SAndrew Thompson 		sc->rssi_2ghz_corr = (int8_t)(val & 0xff);	/* signed */
185602ac6454SAndrew Thompson 
185702ac6454SAndrew Thompson 	/* Only [-10, 10] is valid */
185802ac6454SAndrew Thompson 	if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
185902ac6454SAndrew Thompson 		sc->rssi_2ghz_corr = 0;
186002ac6454SAndrew Thompson 
186102ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
186202ac6454SAndrew Thompson 	val = le16toh(val);
186302ac6454SAndrew Thompson 	if ((val & 0xff) != 0xff)
186402ac6454SAndrew Thompson 		sc->rssi_5ghz_corr = (int8_t)(val & 0xff);	/* signed */
186502ac6454SAndrew Thompson 
186602ac6454SAndrew Thompson 	/* Only [-10, 10] is valid */
186702ac6454SAndrew Thompson 	if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
186802ac6454SAndrew Thompson 		sc->rssi_5ghz_corr = 0;
186902ac6454SAndrew Thompson 
187002ac6454SAndrew Thompson 	if (sc->ext_2ghz_lna)
187102ac6454SAndrew Thompson 		sc->rssi_2ghz_corr -= 14;
187202ac6454SAndrew Thompson 	if (sc->ext_5ghz_lna)
187302ac6454SAndrew Thompson 		sc->rssi_5ghz_corr -= 14;
187402ac6454SAndrew Thompson 
187502ac6454SAndrew Thompson 	DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
187602ac6454SAndrew Thompson 	    sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
187702ac6454SAndrew Thompson 
187802ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
187902ac6454SAndrew Thompson 	val = le16toh(val);
188002ac6454SAndrew Thompson 	if ((val & 0xff) != 0xff)
188102ac6454SAndrew Thompson 		sc->rffreq = val & 0xff;
188202ac6454SAndrew Thompson 
188302ac6454SAndrew Thompson 	DPRINTF("RF freq=%d\n", sc->rffreq);
188402ac6454SAndrew Thompson 
188502ac6454SAndrew Thompson 	/* read Tx power for all a/b/g channels */
188602ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
188702ac6454SAndrew Thompson 	/* XXX default Tx power for 802.11a channels */
188802ac6454SAndrew Thompson 	memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
188902ac6454SAndrew Thompson #ifdef RUM_DEBUG
189002ac6454SAndrew Thompson 	for (i = 0; i < 14; i++)
189102ac6454SAndrew Thompson 		DPRINTF("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]);
189202ac6454SAndrew Thompson #endif
189302ac6454SAndrew Thompson 
189402ac6454SAndrew Thompson 	/* read default values for BBP registers */
189502ac6454SAndrew Thompson 	rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
189602ac6454SAndrew Thompson #ifdef RUM_DEBUG
189702ac6454SAndrew Thompson 	for (i = 0; i < 14; i++) {
189802ac6454SAndrew Thompson 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
189902ac6454SAndrew Thompson 			continue;
190002ac6454SAndrew Thompson 		DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
190102ac6454SAndrew Thompson 		    sc->bbp_prom[i].val);
190202ac6454SAndrew Thompson 	}
190302ac6454SAndrew Thompson #endif
190402ac6454SAndrew Thompson }
190502ac6454SAndrew Thompson 
190602ac6454SAndrew Thompson static int
190702ac6454SAndrew Thompson rum_bbp_init(struct rum_softc *sc)
190802ac6454SAndrew Thompson {
190902ac6454SAndrew Thompson #define N(a)	(sizeof (a) / sizeof ((a)[0]))
191002ac6454SAndrew Thompson 	int i, ntries;
191102ac6454SAndrew Thompson 
191202ac6454SAndrew Thompson 	/* wait for BBP to be ready */
191302ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
191402ac6454SAndrew Thompson 		const uint8_t val = rum_bbp_read(sc, 0);
191502ac6454SAndrew Thompson 		if (val != 0 && val != 0xff)
191602ac6454SAndrew Thompson 			break;
191702ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
191802ac6454SAndrew Thompson 			break;
191902ac6454SAndrew Thompson 	}
192002ac6454SAndrew Thompson 	if (ntries == 100) {
192102ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "timeout waiting for BBP\n");
192202ac6454SAndrew Thompson 		return EIO;
192302ac6454SAndrew Thompson 	}
192402ac6454SAndrew Thompson 
192502ac6454SAndrew Thompson 	/* initialize BBP registers to default values */
192602ac6454SAndrew Thompson 	for (i = 0; i < N(rum_def_bbp); i++)
192702ac6454SAndrew Thompson 		rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
192802ac6454SAndrew Thompson 
192902ac6454SAndrew Thompson 	/* write vendor-specific BBP values (from EEPROM) */
193002ac6454SAndrew Thompson 	for (i = 0; i < 16; i++) {
193102ac6454SAndrew Thompson 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
193202ac6454SAndrew Thompson 			continue;
193302ac6454SAndrew Thompson 		rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
193402ac6454SAndrew Thompson 	}
193502ac6454SAndrew Thompson 
193602ac6454SAndrew Thompson 	return 0;
193702ac6454SAndrew Thompson #undef N
193802ac6454SAndrew Thompson }
193902ac6454SAndrew Thompson 
194002ac6454SAndrew Thompson static void
19415efea30fSAndrew Thompson rum_init_locked(struct rum_softc *sc)
194202ac6454SAndrew Thompson {
194302ac6454SAndrew Thompson #define N(a)	(sizeof (a) / sizeof ((a)[0]))
194402ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
194502ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
194602ac6454SAndrew Thompson 	uint32_t tmp;
194702ac6454SAndrew Thompson 	usb2_error_t error;
194802ac6454SAndrew Thompson 	int i, ntries;
194902ac6454SAndrew Thompson 
195002ac6454SAndrew Thompson 	RUM_LOCK_ASSERT(sc, MA_OWNED);
195102ac6454SAndrew Thompson 
19525efea30fSAndrew Thompson 	rum_stop(sc);
195302ac6454SAndrew Thompson 
195402ac6454SAndrew Thompson 	/* initialize MAC registers to default values */
195502ac6454SAndrew Thompson 	for (i = 0; i < N(rum_def_mac); i++)
195602ac6454SAndrew Thompson 		rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
195702ac6454SAndrew Thompson 
195802ac6454SAndrew Thompson 	/* set host ready */
195902ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR1, 3);
196002ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR1, 0);
196102ac6454SAndrew Thompson 
196202ac6454SAndrew Thompson 	/* wait for BBP/RF to wakeup */
196302ac6454SAndrew Thompson 	for (ntries = 0; ntries < 100; ntries++) {
196402ac6454SAndrew Thompson 		if (rum_read(sc, RT2573_MAC_CSR12) & 8)
196502ac6454SAndrew Thompson 			break;
196602ac6454SAndrew Thompson 		rum_write(sc, RT2573_MAC_CSR12, 4);	/* force wakeup */
196702ac6454SAndrew Thompson 		if (rum_pause(sc, hz / 100))
196802ac6454SAndrew Thompson 			break;
196902ac6454SAndrew Thompson 	}
197002ac6454SAndrew Thompson 	if (ntries == 100) {
197102ac6454SAndrew Thompson 		device_printf(sc->sc_dev,
197202ac6454SAndrew Thompson 		    "timeout waiting for BBP/RF to wakeup\n");
197302ac6454SAndrew Thompson 		goto fail;
197402ac6454SAndrew Thompson 	}
197502ac6454SAndrew Thompson 
197602ac6454SAndrew Thompson 	if ((error = rum_bbp_init(sc)) != 0)
197702ac6454SAndrew Thompson 		goto fail;
197802ac6454SAndrew Thompson 
197902ac6454SAndrew Thompson 	/* select default channel */
198002ac6454SAndrew Thompson 	rum_select_band(sc, ic->ic_curchan);
198102ac6454SAndrew Thompson 	rum_select_antenna(sc);
198202ac6454SAndrew Thompson 	rum_set_chan(sc, ic->ic_curchan);
198302ac6454SAndrew Thompson 
198402ac6454SAndrew Thompson 	/* clear STA registers */
198502ac6454SAndrew Thompson 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
198602ac6454SAndrew Thompson 
198729aca940SSam Leffler 	rum_set_macaddr(sc, IF_LLADDR(ifp));
198802ac6454SAndrew Thompson 
198902ac6454SAndrew Thompson 	/* initialize ASIC */
199002ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR1, 4);
199102ac6454SAndrew Thompson 
199202ac6454SAndrew Thompson 	/*
199302ac6454SAndrew Thompson 	 * Allocate Tx and Rx xfer queues.
199402ac6454SAndrew Thompson 	 */
199502ac6454SAndrew Thompson 	rum_setup_tx_list(sc);
199602ac6454SAndrew Thompson 
199702ac6454SAndrew Thompson 	/* update Rx filter */
199802ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
199902ac6454SAndrew Thompson 
200002ac6454SAndrew Thompson 	tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
200102ac6454SAndrew Thompson 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
200202ac6454SAndrew Thompson 		tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
200302ac6454SAndrew Thompson 		       RT2573_DROP_ACKCTS;
200402ac6454SAndrew Thompson 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
200502ac6454SAndrew Thompson 			tmp |= RT2573_DROP_TODS;
200602ac6454SAndrew Thompson 		if (!(ifp->if_flags & IFF_PROMISC))
200702ac6454SAndrew Thompson 			tmp |= RT2573_DROP_NOT_TO_ME;
200802ac6454SAndrew Thompson 	}
200902ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
201002ac6454SAndrew Thompson 
201102ac6454SAndrew Thompson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
201202ac6454SAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
201302ac6454SAndrew Thompson 	usb2_transfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
201402ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
201502ac6454SAndrew Thompson 	return;
201602ac6454SAndrew Thompson 
20175efea30fSAndrew Thompson fail:	rum_stop(sc);
201802ac6454SAndrew Thompson #undef N
201902ac6454SAndrew Thompson }
202002ac6454SAndrew Thompson 
202102ac6454SAndrew Thompson static void
202202ac6454SAndrew Thompson rum_init(void *priv)
202302ac6454SAndrew Thompson {
202402ac6454SAndrew Thompson 	struct rum_softc *sc = priv;
202502ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
202602ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
202702ac6454SAndrew Thompson 
202802ac6454SAndrew Thompson 	RUM_LOCK(sc);
20295efea30fSAndrew Thompson 	rum_init_locked(sc);
203002ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
203102ac6454SAndrew Thompson 
203202ac6454SAndrew Thompson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
203302ac6454SAndrew Thompson 		ieee80211_start_all(ic);		/* start all vap's */
203402ac6454SAndrew Thompson }
203502ac6454SAndrew Thompson 
203602ac6454SAndrew Thompson static void
20375efea30fSAndrew Thompson rum_stop(struct rum_softc *sc)
203802ac6454SAndrew Thompson {
203902ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
204002ac6454SAndrew Thompson 	uint32_t tmp;
204102ac6454SAndrew Thompson 
204202ac6454SAndrew Thompson 	RUM_LOCK_ASSERT(sc, MA_OWNED);
204302ac6454SAndrew Thompson 
204402ac6454SAndrew Thompson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
204502ac6454SAndrew Thompson 
204602ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
204702ac6454SAndrew Thompson 
204802ac6454SAndrew Thompson 	/*
204902ac6454SAndrew Thompson 	 * Drain the USB transfers, if not already drained:
205002ac6454SAndrew Thompson 	 */
205102ac6454SAndrew Thompson 	usb2_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
205202ac6454SAndrew Thompson 	usb2_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
205302ac6454SAndrew Thompson 
205402ac6454SAndrew Thompson 	RUM_LOCK(sc);
205502ac6454SAndrew Thompson 
205602ac6454SAndrew Thompson 	rum_unsetup_tx_list(sc);
205702ac6454SAndrew Thompson 
205802ac6454SAndrew Thompson 	/* disable Rx */
205902ac6454SAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
206002ac6454SAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
206102ac6454SAndrew Thompson 
206202ac6454SAndrew Thompson 	/* reset ASIC */
206302ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR1, 3);
206402ac6454SAndrew Thompson 	rum_write(sc, RT2573_MAC_CSR1, 0);
206502ac6454SAndrew Thompson }
206602ac6454SAndrew Thompson 
206777ddf3d3SAndrew Thompson static void
206877ddf3d3SAndrew Thompson rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
206902ac6454SAndrew Thompson {
207002ac6454SAndrew Thompson 	struct usb2_device_request req;
207102ac6454SAndrew Thompson 	uint16_t reg = RT2573_MCU_CODE_BASE;
207277ddf3d3SAndrew Thompson 	usb2_error_t err;
207302ac6454SAndrew Thompson 
207402ac6454SAndrew Thompson 	/* copy firmware image into NIC */
207577ddf3d3SAndrew Thompson 	for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
207677ddf3d3SAndrew Thompson 		err = rum_write(sc, reg, UGETDW(ucode));
207777ddf3d3SAndrew Thompson 		if (err) {
207877ddf3d3SAndrew Thompson 			/* firmware already loaded ? */
207977ddf3d3SAndrew Thompson 			device_printf(sc->sc_dev, "Firmware load "
208077ddf3d3SAndrew Thompson 			    "failure! (ignored)\n");
208177ddf3d3SAndrew Thompson 			break;
208277ddf3d3SAndrew Thompson 		}
208377ddf3d3SAndrew Thompson 	}
208402ac6454SAndrew Thompson 
208502ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
208602ac6454SAndrew Thompson 	req.bRequest = RT2573_MCU_CNTL;
208702ac6454SAndrew Thompson 	USETW(req.wValue, RT2573_MCU_RUN);
208802ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
208902ac6454SAndrew Thompson 	USETW(req.wLength, 0);
209002ac6454SAndrew Thompson 
209177ddf3d3SAndrew Thompson 	err = rum_do_request(sc, &req, NULL);
209277ddf3d3SAndrew Thompson 	if (err != 0) {
209302ac6454SAndrew Thompson 		device_printf(sc->sc_dev, "could not run firmware: %s\n",
209477ddf3d3SAndrew Thompson 		    usb2_errstr(err));
209502ac6454SAndrew Thompson 	}
209677ddf3d3SAndrew Thompson 
209777ddf3d3SAndrew Thompson 	/* give the chip some time to boot */
209877ddf3d3SAndrew Thompson 	rum_pause(sc, hz / 8);
209902ac6454SAndrew Thompson }
210002ac6454SAndrew Thompson 
210102ac6454SAndrew Thompson static int
210202ac6454SAndrew Thompson rum_prepare_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
210302ac6454SAndrew Thompson {
210402ac6454SAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
210502ac6454SAndrew Thompson 	const struct ieee80211_txparam *tp;
210602ac6454SAndrew Thompson 	struct rum_tx_desc desc;
210702ac6454SAndrew Thompson 	struct mbuf *m0;
210802ac6454SAndrew Thompson 
210902ac6454SAndrew Thompson 	m0 = ieee80211_beacon_alloc(vap->iv_bss, &RUM_VAP(vap)->bo);
211002ac6454SAndrew Thompson 	if (m0 == NULL) {
211102ac6454SAndrew Thompson 		return ENOBUFS;
211202ac6454SAndrew Thompson 	}
211302ac6454SAndrew Thompson 
211402ac6454SAndrew Thompson 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
211502ac6454SAndrew Thompson 	rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
211602ac6454SAndrew Thompson 	    m0->m_pkthdr.len, tp->mgmtrate);
211702ac6454SAndrew Thompson 
211802ac6454SAndrew Thompson 	/* copy the first 24 bytes of Tx descriptor into NIC memory */
211902ac6454SAndrew Thompson 	rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
212002ac6454SAndrew Thompson 
212102ac6454SAndrew Thompson 	/* copy beacon header and payload into NIC memory */
212202ac6454SAndrew Thompson 	rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
212302ac6454SAndrew Thompson 	    m0->m_pkthdr.len);
212402ac6454SAndrew Thompson 
212502ac6454SAndrew Thompson 	m_freem(m0);
212602ac6454SAndrew Thompson 
212702ac6454SAndrew Thompson 	return 0;
212802ac6454SAndrew Thompson }
212902ac6454SAndrew Thompson 
213002ac6454SAndrew Thompson static int
213102ac6454SAndrew Thompson rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
213202ac6454SAndrew Thompson     const struct ieee80211_bpf_params *params)
213302ac6454SAndrew Thompson {
213402ac6454SAndrew Thompson 	struct ifnet *ifp = ni->ni_ic->ic_ifp;
213502ac6454SAndrew Thompson 	struct rum_softc *sc = ifp->if_softc;
213602ac6454SAndrew Thompson 
213702ac6454SAndrew Thompson 	RUM_LOCK(sc);
213802ac6454SAndrew Thompson 	/* prevent management frames from being sent if we're not ready */
213902ac6454SAndrew Thompson 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
214002ac6454SAndrew Thompson 		RUM_UNLOCK(sc);
214102ac6454SAndrew Thompson 		m_freem(m);
214202ac6454SAndrew Thompson 		ieee80211_free_node(ni);
214302ac6454SAndrew Thompson 		return ENETDOWN;
214402ac6454SAndrew Thompson 	}
214596ca458fSAndrew Thompson 	if (sc->tx_nfree < RUM_TX_MINFREE) {
214602ac6454SAndrew Thompson 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
214702ac6454SAndrew Thompson 		RUM_UNLOCK(sc);
214802ac6454SAndrew Thompson 		m_freem(m);
214902ac6454SAndrew Thompson 		ieee80211_free_node(ni);
215002ac6454SAndrew Thompson 		return EIO;
215102ac6454SAndrew Thompson 	}
215202ac6454SAndrew Thompson 
215302ac6454SAndrew Thompson 	ifp->if_opackets++;
215402ac6454SAndrew Thompson 
215502ac6454SAndrew Thompson 	if (params == NULL) {
215602ac6454SAndrew Thompson 		/*
215702ac6454SAndrew Thompson 		 * Legacy path; interpret frame contents to decide
215802ac6454SAndrew Thompson 		 * precisely how to send the frame.
215902ac6454SAndrew Thompson 		 */
216002ac6454SAndrew Thompson 		if (rum_tx_mgt(sc, m, ni) != 0)
216102ac6454SAndrew Thompson 			goto bad;
216202ac6454SAndrew Thompson 	} else {
216302ac6454SAndrew Thompson 		/*
216402ac6454SAndrew Thompson 		 * Caller supplied explicit parameters to use in
216502ac6454SAndrew Thompson 		 * sending the frame.
216602ac6454SAndrew Thompson 		 */
216702ac6454SAndrew Thompson 		if (rum_tx_raw(sc, m, ni, params) != 0)
216802ac6454SAndrew Thompson 			goto bad;
216902ac6454SAndrew Thompson 	}
217002ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
217102ac6454SAndrew Thompson 
217202ac6454SAndrew Thompson 	return 0;
217302ac6454SAndrew Thompson bad:
217402ac6454SAndrew Thompson 	ifp->if_oerrors++;
217502ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
217602ac6454SAndrew Thompson 	ieee80211_free_node(ni);
217702ac6454SAndrew Thompson 	return EIO;
217802ac6454SAndrew Thompson }
217902ac6454SAndrew Thompson 
218002ac6454SAndrew Thompson static void
218102ac6454SAndrew Thompson rum_amrr_start(struct rum_softc *sc, struct ieee80211_node *ni)
218202ac6454SAndrew Thompson {
218302ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
218402ac6454SAndrew Thompson 	struct rum_vap *rvp = RUM_VAP(vap);
218502ac6454SAndrew Thompson 
218602ac6454SAndrew Thompson 	/* clear statistic registers (STA_CSR0 to STA_CSR5) */
218702ac6454SAndrew Thompson 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
218802ac6454SAndrew Thompson 
218902ac6454SAndrew Thompson 	ieee80211_amrr_node_init(&rvp->amrr, &RUM_NODE(ni)->amn, ni);
219002ac6454SAndrew Thompson 
219102ac6454SAndrew Thompson 	usb2_callout_reset(&rvp->amrr_ch, hz, rum_amrr_timeout, rvp);
219202ac6454SAndrew Thompson }
219302ac6454SAndrew Thompson 
219402ac6454SAndrew Thompson static void
219502ac6454SAndrew Thompson rum_amrr_timeout(void *arg)
219602ac6454SAndrew Thompson {
219702ac6454SAndrew Thompson 	struct rum_vap *rvp = arg;
21985efea30fSAndrew Thompson 	struct ieee80211vap *vap = &rvp->vap;
21995efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
220002ac6454SAndrew Thompson 
22015efea30fSAndrew Thompson 	ieee80211_runtask(ic, &rvp->amrr_task);
220202ac6454SAndrew Thompson }
220302ac6454SAndrew Thompson 
220402ac6454SAndrew Thompson static void
22055efea30fSAndrew Thompson rum_amrr_task(void *arg, int pending)
220602ac6454SAndrew Thompson {
22075efea30fSAndrew Thompson 	struct rum_vap *rvp = arg;
22085efea30fSAndrew Thompson 	struct ieee80211vap *vap = &rvp->vap;
22095efea30fSAndrew Thompson 	struct ieee80211com *ic = vap->iv_ic;
22105efea30fSAndrew Thompson 	struct ifnet *ifp = ic->ic_ifp;
22115efea30fSAndrew Thompson 	struct rum_softc *sc = ifp->if_softc;
221202ac6454SAndrew Thompson 	struct ieee80211_node *ni = vap->iv_bss;
221302ac6454SAndrew Thompson 	int ok, fail;
221402ac6454SAndrew Thompson 
22155efea30fSAndrew Thompson 	RUM_LOCK(sc);
221602ac6454SAndrew Thompson 	/* read and clear statistic registers (STA_CSR0 to STA_CSR10) */
221702ac6454SAndrew Thompson 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
221802ac6454SAndrew Thompson 
221902ac6454SAndrew Thompson 	ok = (le32toh(sc->sta[4]) >> 16) +	/* TX ok w/o retry */
222002ac6454SAndrew Thompson 	    (le32toh(sc->sta[5]) & 0xffff);	/* TX ok w/ retry */
222102ac6454SAndrew Thompson 	fail = (le32toh(sc->sta[5]) >> 16);	/* TX retry-fail count */
222202ac6454SAndrew Thompson 
222302ac6454SAndrew Thompson 	ieee80211_amrr_tx_update(&RUM_NODE(ni)->amn,
222402ac6454SAndrew Thompson 	    ok+fail, ok, (le32toh(sc->sta[5]) & 0xffff) + fail);
222502ac6454SAndrew Thompson 	(void) ieee80211_amrr_choose(ni, &RUM_NODE(ni)->amn);
222602ac6454SAndrew Thompson 
222702ac6454SAndrew Thompson 	ifp->if_oerrors += fail;	/* count TX retry-fail as Tx errors */
222802ac6454SAndrew Thompson 
222902ac6454SAndrew Thompson 	usb2_callout_reset(&rvp->amrr_ch, hz, rum_amrr_timeout, rvp);
22305efea30fSAndrew Thompson 	RUM_UNLOCK(sc);
223102ac6454SAndrew Thompson }
223202ac6454SAndrew Thompson 
223302ac6454SAndrew Thompson /* ARGUSED */
223402ac6454SAndrew Thompson static struct ieee80211_node *
223502ac6454SAndrew Thompson rum_node_alloc(struct ieee80211vap *vap __unused,
223602ac6454SAndrew Thompson 	const uint8_t mac[IEEE80211_ADDR_LEN] __unused)
223702ac6454SAndrew Thompson {
223802ac6454SAndrew Thompson 	struct rum_node *rn;
223902ac6454SAndrew Thompson 
224002ac6454SAndrew Thompson 	rn = malloc(sizeof(struct rum_node), M_80211_NODE, M_NOWAIT | M_ZERO);
224102ac6454SAndrew Thompson 	return rn != NULL ? &rn->ni : NULL;
224202ac6454SAndrew Thompson }
224302ac6454SAndrew Thompson 
224402ac6454SAndrew Thompson static void
224502ac6454SAndrew Thompson rum_newassoc(struct ieee80211_node *ni, int isnew)
224602ac6454SAndrew Thompson {
224702ac6454SAndrew Thompson 	struct ieee80211vap *vap = ni->ni_vap;
224802ac6454SAndrew Thompson 
224902ac6454SAndrew Thompson 	ieee80211_amrr_node_init(&RUM_VAP(vap)->amrr, &RUM_NODE(ni)->amn, ni);
225002ac6454SAndrew Thompson }
225102ac6454SAndrew Thompson 
225202ac6454SAndrew Thompson static void
225302ac6454SAndrew Thompson rum_scan_start(struct ieee80211com *ic)
225402ac6454SAndrew Thompson {
22555efea30fSAndrew Thompson 	struct ifnet *ifp = ic->ic_ifp;
22565efea30fSAndrew Thompson 	struct rum_softc *sc = ifp->if_softc;
22575efea30fSAndrew Thompson 	uint32_t tmp;
225802ac6454SAndrew Thompson 
225902ac6454SAndrew Thompson 	RUM_LOCK(sc);
22605efea30fSAndrew Thompson 	/* abort TSF synchronization */
22615efea30fSAndrew Thompson 	tmp = rum_read(sc, RT2573_TXRX_CSR9);
22625efea30fSAndrew Thompson 	rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
22635efea30fSAndrew Thompson 	rum_set_bssid(sc, ifp->if_broadcastaddr);
226402ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
226502ac6454SAndrew Thompson 
226602ac6454SAndrew Thompson }
226702ac6454SAndrew Thompson 
226802ac6454SAndrew Thompson static void
226902ac6454SAndrew Thompson rum_scan_end(struct ieee80211com *ic)
227002ac6454SAndrew Thompson {
227102ac6454SAndrew Thompson 	struct rum_softc *sc = ic->ic_ifp->if_softc;
227202ac6454SAndrew Thompson 
227302ac6454SAndrew Thompson 	RUM_LOCK(sc);
22745efea30fSAndrew Thompson 	rum_enable_tsf_sync(sc);
22755efea30fSAndrew Thompson 	rum_set_bssid(sc, sc->sc_bssid);
227602ac6454SAndrew Thompson 	RUM_UNLOCK(sc);
227702ac6454SAndrew Thompson 
227802ac6454SAndrew Thompson }
227902ac6454SAndrew Thompson 
228002ac6454SAndrew Thompson static void
228102ac6454SAndrew Thompson rum_set_channel(struct ieee80211com *ic)
228202ac6454SAndrew Thompson {
228302ac6454SAndrew Thompson 	struct rum_softc *sc = ic->ic_ifp->if_softc;
228402ac6454SAndrew Thompson 
228502ac6454SAndrew Thompson 	RUM_LOCK(sc);
228602ac6454SAndrew Thompson 	rum_set_chan(sc, ic->ic_curchan);
22875efea30fSAndrew Thompson 	RUM_UNLOCK(sc);
228802ac6454SAndrew Thompson }
228902ac6454SAndrew Thompson 
229002ac6454SAndrew Thompson static int
229102ac6454SAndrew Thompson rum_get_rssi(struct rum_softc *sc, uint8_t raw)
229202ac6454SAndrew Thompson {
229302ac6454SAndrew Thompson 	struct ifnet *ifp = sc->sc_ifp;
229402ac6454SAndrew Thompson 	struct ieee80211com *ic = ifp->if_l2com;
229502ac6454SAndrew Thompson 	int lna, agc, rssi;
229602ac6454SAndrew Thompson 
229702ac6454SAndrew Thompson 	lna = (raw >> 5) & 0x3;
229802ac6454SAndrew Thompson 	agc = raw & 0x1f;
229902ac6454SAndrew Thompson 
230002ac6454SAndrew Thompson 	if (lna == 0) {
230102ac6454SAndrew Thompson 		/*
230202ac6454SAndrew Thompson 		 * No RSSI mapping
230302ac6454SAndrew Thompson 		 *
230402ac6454SAndrew Thompson 		 * NB: Since RSSI is relative to noise floor, -1 is
230502ac6454SAndrew Thompson 		 *     adequate for caller to know error happened.
230602ac6454SAndrew Thompson 		 */
230702ac6454SAndrew Thompson 		return -1;
230802ac6454SAndrew Thompson 	}
230902ac6454SAndrew Thompson 
231002ac6454SAndrew Thompson 	rssi = (2 * agc) - RT2573_NOISE_FLOOR;
231102ac6454SAndrew Thompson 
231202ac6454SAndrew Thompson 	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
231302ac6454SAndrew Thompson 		rssi += sc->rssi_2ghz_corr;
231402ac6454SAndrew Thompson 
231502ac6454SAndrew Thompson 		if (lna == 1)
231602ac6454SAndrew Thompson 			rssi -= 64;
231702ac6454SAndrew Thompson 		else if (lna == 2)
231802ac6454SAndrew Thompson 			rssi -= 74;
231902ac6454SAndrew Thompson 		else if (lna == 3)
232002ac6454SAndrew Thompson 			rssi -= 90;
232102ac6454SAndrew Thompson 	} else {
232202ac6454SAndrew Thompson 		rssi += sc->rssi_5ghz_corr;
232302ac6454SAndrew Thompson 
232402ac6454SAndrew Thompson 		if (!sc->ext_5ghz_lna && lna != 1)
232502ac6454SAndrew Thompson 			rssi += 4;
232602ac6454SAndrew Thompson 
232702ac6454SAndrew Thompson 		if (lna == 1)
232802ac6454SAndrew Thompson 			rssi -= 64;
232902ac6454SAndrew Thompson 		else if (lna == 2)
233002ac6454SAndrew Thompson 			rssi -= 86;
233102ac6454SAndrew Thompson 		else if (lna == 3)
233202ac6454SAndrew Thompson 			rssi -= 100;
233302ac6454SAndrew Thompson 	}
233402ac6454SAndrew Thompson 	return rssi;
233502ac6454SAndrew Thompson }
233602ac6454SAndrew Thompson 
233702ac6454SAndrew Thompson static int
233802ac6454SAndrew Thompson rum_pause(struct rum_softc *sc, int timeout)
233902ac6454SAndrew Thompson {
234002ac6454SAndrew Thompson 
234102ac6454SAndrew Thompson 	usb2_pause_mtx(&sc->sc_mtx, timeout);
234202ac6454SAndrew Thompson 	return (0);
234302ac6454SAndrew Thompson }
234402ac6454SAndrew Thompson 
234502ac6454SAndrew Thompson static device_method_t rum_methods[] = {
234602ac6454SAndrew Thompson 	/* Device interface */
234702ac6454SAndrew Thompson 	DEVMETHOD(device_probe,		rum_match),
234802ac6454SAndrew Thompson 	DEVMETHOD(device_attach,	rum_attach),
234902ac6454SAndrew Thompson 	DEVMETHOD(device_detach,	rum_detach),
235002ac6454SAndrew Thompson 
235102ac6454SAndrew Thompson 	{ 0, 0 }
235202ac6454SAndrew Thompson };
235302ac6454SAndrew Thompson 
235402ac6454SAndrew Thompson static driver_t rum_driver = {
235502ac6454SAndrew Thompson 	.name = "rum",
235602ac6454SAndrew Thompson 	.methods = rum_methods,
235702ac6454SAndrew Thompson 	.size = sizeof(struct rum_softc),
235802ac6454SAndrew Thompson };
235902ac6454SAndrew Thompson 
236002ac6454SAndrew Thompson static devclass_t rum_devclass;
236102ac6454SAndrew Thompson 
23629aef556dSAndrew Thompson DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
2363