102ac6454SAndrew Thompson /* $FreeBSD$ */ 202ac6454SAndrew Thompson 302ac6454SAndrew Thompson /*- 402ac6454SAndrew Thompson * Copyright (c) 2005, 2006 502ac6454SAndrew Thompson * Damien Bergamini <damien.bergamini@free.fr> 602ac6454SAndrew Thompson * 702ac6454SAndrew Thompson * Copyright (c) 2006, 2008 85d38a4d4SEd Schouten * Hans Petter Selasky <hselasky@FreeBSD.org> 902ac6454SAndrew Thompson * 1002ac6454SAndrew Thompson * Permission to use, copy, modify, and distribute this software for any 1102ac6454SAndrew Thompson * purpose with or without fee is hereby granted, provided that the above 1202ac6454SAndrew Thompson * copyright notice and this permission notice appear in all copies. 1302ac6454SAndrew Thompson * 1402ac6454SAndrew Thompson * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 1502ac6454SAndrew Thompson * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1602ac6454SAndrew Thompson * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 1702ac6454SAndrew Thompson * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1802ac6454SAndrew Thompson * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1902ac6454SAndrew Thompson * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 2002ac6454SAndrew Thompson * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 2102ac6454SAndrew Thompson */ 2202ac6454SAndrew Thompson 2302ac6454SAndrew Thompson #include <sys/cdefs.h> 2402ac6454SAndrew Thompson __FBSDID("$FreeBSD$"); 2502ac6454SAndrew Thompson 2602ac6454SAndrew Thompson /*- 2702ac6454SAndrew Thompson * Ralink Technology RT2500USB chipset driver 2802ac6454SAndrew Thompson * http://www.ralinktech.com/ 2902ac6454SAndrew Thompson */ 3002ac6454SAndrew Thompson 315efea30fSAndrew Thompson #include <sys/param.h> 325efea30fSAndrew Thompson #include <sys/sockio.h> 335efea30fSAndrew Thompson #include <sys/sysctl.h> 345efea30fSAndrew Thompson #include <sys/lock.h> 355efea30fSAndrew Thompson #include <sys/mutex.h> 365efea30fSAndrew Thompson #include <sys/mbuf.h> 375efea30fSAndrew Thompson #include <sys/kernel.h> 385efea30fSAndrew Thompson #include <sys/socket.h> 395efea30fSAndrew Thompson #include <sys/systm.h> 405efea30fSAndrew Thompson #include <sys/malloc.h> 415efea30fSAndrew Thompson #include <sys/module.h> 425efea30fSAndrew Thompson #include <sys/bus.h> 435efea30fSAndrew Thompson #include <sys/endian.h> 445efea30fSAndrew Thompson #include <sys/kdb.h> 455efea30fSAndrew Thompson 465efea30fSAndrew Thompson #include <machine/bus.h> 475efea30fSAndrew Thompson #include <machine/resource.h> 485efea30fSAndrew Thompson #include <sys/rman.h> 495efea30fSAndrew Thompson 505efea30fSAndrew Thompson #include <net/bpf.h> 515efea30fSAndrew Thompson #include <net/if.h> 525efea30fSAndrew Thompson #include <net/if_arp.h> 535efea30fSAndrew Thompson #include <net/ethernet.h> 545efea30fSAndrew Thompson #include <net/if_dl.h> 555efea30fSAndrew Thompson #include <net/if_media.h> 565efea30fSAndrew Thompson #include <net/if_types.h> 575efea30fSAndrew Thompson 585efea30fSAndrew Thompson #ifdef INET 595efea30fSAndrew Thompson #include <netinet/in.h> 605efea30fSAndrew Thompson #include <netinet/in_systm.h> 615efea30fSAndrew Thompson #include <netinet/in_var.h> 625efea30fSAndrew Thompson #include <netinet/if_ether.h> 635efea30fSAndrew Thompson #include <netinet/ip.h> 645efea30fSAndrew Thompson #endif 655efea30fSAndrew Thompson 665efea30fSAndrew Thompson #include <net80211/ieee80211_var.h> 675efea30fSAndrew Thompson #include <net80211/ieee80211_regdomain.h> 685efea30fSAndrew Thompson #include <net80211/ieee80211_radiotap.h> 695efea30fSAndrew Thompson #include <net80211/ieee80211_amrr.h> 7002ac6454SAndrew Thompson 715efea30fSAndrew Thompson #include <dev/usb/usb.h> 72ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 735efea30fSAndrew Thompson #include "usbdevs.h" 7402ac6454SAndrew Thompson 75ed6d949aSAndrew Thompson #define USB_DEBUG_VAR ural_debug 76ed6d949aSAndrew Thompson #include <dev/usb/usb_debug.h> 775efea30fSAndrew Thompson 7802ac6454SAndrew Thompson #include <dev/usb/wlan/if_uralreg.h> 7902ac6454SAndrew Thompson #include <dev/usb/wlan/if_uralvar.h> 8002ac6454SAndrew Thompson 8102ac6454SAndrew Thompson #if USB_DEBUG 8202ac6454SAndrew Thompson static int ural_debug = 0; 8302ac6454SAndrew Thompson 849360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, ural, CTLFLAG_RW, 0, "USB ural"); 859360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_ural, OID_AUTO, debug, CTLFLAG_RW, &ural_debug, 0, 8602ac6454SAndrew Thompson "Debug level"); 8702ac6454SAndrew Thompson #endif 8802ac6454SAndrew Thompson 8902ac6454SAndrew Thompson #define URAL_RSSI(rssi) \ 9002ac6454SAndrew Thompson ((rssi) > (RAL_NOISE_FLOOR + RAL_RSSI_CORR) ? \ 9102ac6454SAndrew Thompson ((rssi) - (RAL_NOISE_FLOOR + RAL_RSSI_CORR)) : 0) 9202ac6454SAndrew Thompson 9302ac6454SAndrew Thompson /* various supported device vendors/products */ 94760bc48eSAndrew Thompson static const struct usb_device_id ural_devs[] = { 959e6b5313SAndrew Thompson #define URAL_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) } 969e6b5313SAndrew Thompson URAL_DEV(ASUS, WL167G), 979e6b5313SAndrew Thompson URAL_DEV(ASUS, RT2570), 989e6b5313SAndrew Thompson URAL_DEV(BELKIN, F5D7050), 999e6b5313SAndrew Thompson URAL_DEV(BELKIN, F5D7051), 1009e6b5313SAndrew Thompson URAL_DEV(CISCOLINKSYS, HU200TS), 1019e6b5313SAndrew Thompson URAL_DEV(CISCOLINKSYS, WUSB54G), 1029e6b5313SAndrew Thompson URAL_DEV(CISCOLINKSYS, WUSB54GP), 1039e6b5313SAndrew Thompson URAL_DEV(CONCEPTRONIC2, C54RU), 1049e6b5313SAndrew Thompson URAL_DEV(DLINK, DWLG122), 1059e6b5313SAndrew Thompson URAL_DEV(GIGABYTE, GN54G), 1069e6b5313SAndrew Thompson URAL_DEV(GIGABYTE, GNWBKG), 1079e6b5313SAndrew Thompson URAL_DEV(GUILLEMOT, HWGUSB254), 1089e6b5313SAndrew Thompson URAL_DEV(MELCO, KG54), 1099e6b5313SAndrew Thompson URAL_DEV(MELCO, KG54AI), 1109e6b5313SAndrew Thompson URAL_DEV(MELCO, KG54YB), 1119e6b5313SAndrew Thompson URAL_DEV(MELCO, NINWIFI), 1129e6b5313SAndrew Thompson URAL_DEV(MSI, RT2570), 1139e6b5313SAndrew Thompson URAL_DEV(MSI, RT2570_2), 1149e6b5313SAndrew Thompson URAL_DEV(MSI, RT2570_3), 1159e6b5313SAndrew Thompson URAL_DEV(NOVATECH, NV902), 1169e6b5313SAndrew Thompson URAL_DEV(RALINK, RT2570), 1179e6b5313SAndrew Thompson URAL_DEV(RALINK, RT2570_2), 1189e6b5313SAndrew Thompson URAL_DEV(RALINK, RT2570_3), 1199e6b5313SAndrew Thompson URAL_DEV(SIEMENS2, WL54G), 1209e6b5313SAndrew Thompson URAL_DEV(SMC, 2862WG), 1219e6b5313SAndrew Thompson URAL_DEV(SPHAIRON, UB801R), 1229e6b5313SAndrew Thompson URAL_DEV(SURECOM, RT2570), 1239e6b5313SAndrew Thompson URAL_DEV(VTECH, RT2570), 1249e6b5313SAndrew Thompson URAL_DEV(ZINWELL, RT2570), 1259e6b5313SAndrew Thompson #undef URAL_DEV 12602ac6454SAndrew Thompson }; 12702ac6454SAndrew Thompson 128e0a69b51SAndrew Thompson static usb_callback_t ural_bulk_read_callback; 129e0a69b51SAndrew Thompson static usb_callback_t ural_bulk_write_callback; 13002ac6454SAndrew Thompson 131e0a69b51SAndrew Thompson static usb_error_t ural_do_request(struct ural_softc *sc, 132760bc48eSAndrew Thompson struct usb_device_request *req, void *data); 13302ac6454SAndrew Thompson static struct ieee80211vap *ural_vap_create(struct ieee80211com *, 13402ac6454SAndrew Thompson const char name[IFNAMSIZ], int unit, int opmode, 13502ac6454SAndrew Thompson int flags, const uint8_t bssid[IEEE80211_ADDR_LEN], 13602ac6454SAndrew Thompson const uint8_t mac[IEEE80211_ADDR_LEN]); 13702ac6454SAndrew Thompson static void ural_vap_delete(struct ieee80211vap *); 13802ac6454SAndrew Thompson static void ural_tx_free(struct ural_tx_data *, int); 13902ac6454SAndrew Thompson static void ural_setup_tx_list(struct ural_softc *); 14002ac6454SAndrew Thompson static void ural_unsetup_tx_list(struct ural_softc *); 14102ac6454SAndrew Thompson static int ural_newstate(struct ieee80211vap *, 14202ac6454SAndrew Thompson enum ieee80211_state, int); 14302ac6454SAndrew Thompson static void ural_setup_tx_desc(struct ural_softc *, 14402ac6454SAndrew Thompson struct ural_tx_desc *, uint32_t, int, int); 14502ac6454SAndrew Thompson static int ural_tx_bcn(struct ural_softc *, struct mbuf *, 14602ac6454SAndrew Thompson struct ieee80211_node *); 14702ac6454SAndrew Thompson static int ural_tx_mgt(struct ural_softc *, struct mbuf *, 14802ac6454SAndrew Thompson struct ieee80211_node *); 14902ac6454SAndrew Thompson static int ural_tx_data(struct ural_softc *, struct mbuf *, 15002ac6454SAndrew Thompson struct ieee80211_node *); 15102ac6454SAndrew Thompson static void ural_start(struct ifnet *); 15202ac6454SAndrew Thompson static int ural_ioctl(struct ifnet *, u_long, caddr_t); 15302ac6454SAndrew Thompson static void ural_set_testmode(struct ural_softc *); 15402ac6454SAndrew Thompson static void ural_eeprom_read(struct ural_softc *, uint16_t, void *, 15502ac6454SAndrew Thompson int); 15602ac6454SAndrew Thompson static uint16_t ural_read(struct ural_softc *, uint16_t); 15702ac6454SAndrew Thompson static void ural_read_multi(struct ural_softc *, uint16_t, void *, 15802ac6454SAndrew Thompson int); 15902ac6454SAndrew Thompson static void ural_write(struct ural_softc *, uint16_t, uint16_t); 16002ac6454SAndrew Thompson static void ural_write_multi(struct ural_softc *, uint16_t, void *, 16102ac6454SAndrew Thompson int) __unused; 16202ac6454SAndrew Thompson static void ural_bbp_write(struct ural_softc *, uint8_t, uint8_t); 16302ac6454SAndrew Thompson static uint8_t ural_bbp_read(struct ural_softc *, uint8_t); 16402ac6454SAndrew Thompson static void ural_rf_write(struct ural_softc *, uint8_t, uint32_t); 16502ac6454SAndrew Thompson static struct ieee80211_node *ural_node_alloc(struct ieee80211vap *, 16602ac6454SAndrew Thompson const uint8_t mac[IEEE80211_ADDR_LEN]); 16702ac6454SAndrew Thompson static void ural_newassoc(struct ieee80211_node *, int); 16802ac6454SAndrew Thompson static void ural_scan_start(struct ieee80211com *); 16902ac6454SAndrew Thompson static void ural_scan_end(struct ieee80211com *); 17002ac6454SAndrew Thompson static void ural_set_channel(struct ieee80211com *); 17102ac6454SAndrew Thompson static void ural_set_chan(struct ural_softc *, 17202ac6454SAndrew Thompson struct ieee80211_channel *); 17302ac6454SAndrew Thompson static void ural_disable_rf_tune(struct ural_softc *); 17402ac6454SAndrew Thompson static void ural_enable_tsf_sync(struct ural_softc *); 1755463c4a4SSam Leffler static void ural_enable_tsf(struct ural_softc *); 17602ac6454SAndrew Thompson static void ural_update_slot(struct ifnet *); 17702ac6454SAndrew Thompson static void ural_set_txpreamble(struct ural_softc *); 17802ac6454SAndrew Thompson static void ural_set_basicrates(struct ural_softc *, 17902ac6454SAndrew Thompson const struct ieee80211_channel *); 18002ac6454SAndrew Thompson static void ural_set_bssid(struct ural_softc *, const uint8_t *); 18102ac6454SAndrew Thompson static void ural_set_macaddr(struct ural_softc *, uint8_t *); 18277ddf3d3SAndrew Thompson static void ural_update_promisc(struct ifnet *); 1835efea30fSAndrew Thompson static void ural_setpromisc(struct ural_softc *); 18402ac6454SAndrew Thompson static const char *ural_get_rf(int); 18502ac6454SAndrew Thompson static void ural_read_eeprom(struct ural_softc *); 18602ac6454SAndrew Thompson static int ural_bbp_init(struct ural_softc *); 18702ac6454SAndrew Thompson static void ural_set_txantenna(struct ural_softc *, int); 18802ac6454SAndrew Thompson static void ural_set_rxantenna(struct ural_softc *, int); 1895efea30fSAndrew Thompson static void ural_init_locked(struct ural_softc *); 19002ac6454SAndrew Thompson static void ural_init(void *); 1915efea30fSAndrew Thompson static void ural_stop(struct ural_softc *); 19202ac6454SAndrew Thompson static int ural_raw_xmit(struct ieee80211_node *, struct mbuf *, 19302ac6454SAndrew Thompson const struct ieee80211_bpf_params *); 19402ac6454SAndrew Thompson static void ural_amrr_start(struct ural_softc *, 19502ac6454SAndrew Thompson struct ieee80211_node *); 19602ac6454SAndrew Thompson static void ural_amrr_timeout(void *); 1975efea30fSAndrew Thompson static void ural_amrr_task(void *, int); 19802ac6454SAndrew Thompson static int ural_pause(struct ural_softc *sc, int timeout); 19902ac6454SAndrew Thompson 20002ac6454SAndrew Thompson /* 20102ac6454SAndrew Thompson * Default values for MAC registers; values taken from the reference driver. 20202ac6454SAndrew Thompson */ 20302ac6454SAndrew Thompson static const struct { 20402ac6454SAndrew Thompson uint16_t reg; 20502ac6454SAndrew Thompson uint16_t val; 20602ac6454SAndrew Thompson } ural_def_mac[] = { 20702ac6454SAndrew Thompson { RAL_TXRX_CSR5, 0x8c8d }, 20802ac6454SAndrew Thompson { RAL_TXRX_CSR6, 0x8b8a }, 20902ac6454SAndrew Thompson { RAL_TXRX_CSR7, 0x8687 }, 21002ac6454SAndrew Thompson { RAL_TXRX_CSR8, 0x0085 }, 21102ac6454SAndrew Thompson { RAL_MAC_CSR13, 0x1111 }, 21202ac6454SAndrew Thompson { RAL_MAC_CSR14, 0x1e11 }, 21302ac6454SAndrew Thompson { RAL_TXRX_CSR21, 0xe78f }, 21402ac6454SAndrew Thompson { RAL_MAC_CSR9, 0xff1d }, 21502ac6454SAndrew Thompson { RAL_MAC_CSR11, 0x0002 }, 21602ac6454SAndrew Thompson { RAL_MAC_CSR22, 0x0053 }, 21702ac6454SAndrew Thompson { RAL_MAC_CSR15, 0x0000 }, 21802ac6454SAndrew Thompson { RAL_MAC_CSR8, RAL_FRAME_SIZE }, 21902ac6454SAndrew Thompson { RAL_TXRX_CSR19, 0x0000 }, 22002ac6454SAndrew Thompson { RAL_TXRX_CSR18, 0x005a }, 22102ac6454SAndrew Thompson { RAL_PHY_CSR2, 0x0000 }, 22202ac6454SAndrew Thompson { RAL_TXRX_CSR0, 0x1ec0 }, 22302ac6454SAndrew Thompson { RAL_PHY_CSR4, 0x000f } 22402ac6454SAndrew Thompson }; 22502ac6454SAndrew Thompson 22602ac6454SAndrew Thompson /* 22702ac6454SAndrew Thompson * Default values for BBP registers; values taken from the reference driver. 22802ac6454SAndrew Thompson */ 22902ac6454SAndrew Thompson static const struct { 23002ac6454SAndrew Thompson uint8_t reg; 23102ac6454SAndrew Thompson uint8_t val; 23202ac6454SAndrew Thompson } ural_def_bbp[] = { 23302ac6454SAndrew Thompson { 3, 0x02 }, 23402ac6454SAndrew Thompson { 4, 0x19 }, 23502ac6454SAndrew Thompson { 14, 0x1c }, 23602ac6454SAndrew Thompson { 15, 0x30 }, 23702ac6454SAndrew Thompson { 16, 0xac }, 23802ac6454SAndrew Thompson { 17, 0x48 }, 23902ac6454SAndrew Thompson { 18, 0x18 }, 24002ac6454SAndrew Thompson { 19, 0xff }, 24102ac6454SAndrew Thompson { 20, 0x1e }, 24202ac6454SAndrew Thompson { 21, 0x08 }, 24302ac6454SAndrew Thompson { 22, 0x08 }, 24402ac6454SAndrew Thompson { 23, 0x08 }, 24502ac6454SAndrew Thompson { 24, 0x80 }, 24602ac6454SAndrew Thompson { 25, 0x50 }, 24702ac6454SAndrew Thompson { 26, 0x08 }, 24802ac6454SAndrew Thompson { 27, 0x23 }, 24902ac6454SAndrew Thompson { 30, 0x10 }, 25002ac6454SAndrew Thompson { 31, 0x2b }, 25102ac6454SAndrew Thompson { 32, 0xb9 }, 25202ac6454SAndrew Thompson { 34, 0x12 }, 25302ac6454SAndrew Thompson { 35, 0x50 }, 25402ac6454SAndrew Thompson { 39, 0xc4 }, 25502ac6454SAndrew Thompson { 40, 0x02 }, 25602ac6454SAndrew Thompson { 41, 0x60 }, 25702ac6454SAndrew Thompson { 53, 0x10 }, 25802ac6454SAndrew Thompson { 54, 0x18 }, 25902ac6454SAndrew Thompson { 56, 0x08 }, 26002ac6454SAndrew Thompson { 57, 0x10 }, 26102ac6454SAndrew Thompson { 58, 0x08 }, 26202ac6454SAndrew Thompson { 61, 0x60 }, 26302ac6454SAndrew Thompson { 62, 0x10 }, 26402ac6454SAndrew Thompson { 75, 0xff } 26502ac6454SAndrew Thompson }; 26602ac6454SAndrew Thompson 26702ac6454SAndrew Thompson /* 26802ac6454SAndrew Thompson * Default values for RF register R2 indexed by channel numbers. 26902ac6454SAndrew Thompson */ 27002ac6454SAndrew Thompson static const uint32_t ural_rf2522_r2[] = { 27102ac6454SAndrew Thompson 0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814, 27202ac6454SAndrew Thompson 0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e 27302ac6454SAndrew Thompson }; 27402ac6454SAndrew Thompson 27502ac6454SAndrew Thompson static const uint32_t ural_rf2523_r2[] = { 27602ac6454SAndrew Thompson 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d, 27702ac6454SAndrew Thompson 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346 27802ac6454SAndrew Thompson }; 27902ac6454SAndrew Thompson 28002ac6454SAndrew Thompson static const uint32_t ural_rf2524_r2[] = { 28102ac6454SAndrew Thompson 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d, 28202ac6454SAndrew Thompson 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346 28302ac6454SAndrew Thompson }; 28402ac6454SAndrew Thompson 28502ac6454SAndrew Thompson static const uint32_t ural_rf2525_r2[] = { 28602ac6454SAndrew Thompson 0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d, 28702ac6454SAndrew Thompson 0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346 28802ac6454SAndrew Thompson }; 28902ac6454SAndrew Thompson 29002ac6454SAndrew Thompson static const uint32_t ural_rf2525_hi_r2[] = { 29102ac6454SAndrew Thompson 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345, 29202ac6454SAndrew Thompson 0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e 29302ac6454SAndrew Thompson }; 29402ac6454SAndrew Thompson 29502ac6454SAndrew Thompson static const uint32_t ural_rf2525e_r2[] = { 29602ac6454SAndrew Thompson 0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463, 29702ac6454SAndrew Thompson 0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b 29802ac6454SAndrew Thompson }; 29902ac6454SAndrew Thompson 30002ac6454SAndrew Thompson static const uint32_t ural_rf2526_hi_r2[] = { 30102ac6454SAndrew Thompson 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d, 30202ac6454SAndrew Thompson 0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241 30302ac6454SAndrew Thompson }; 30402ac6454SAndrew Thompson 30502ac6454SAndrew Thompson static const uint32_t ural_rf2526_r2[] = { 30602ac6454SAndrew Thompson 0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229, 30702ac6454SAndrew Thompson 0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d 30802ac6454SAndrew Thompson }; 30902ac6454SAndrew Thompson 31002ac6454SAndrew Thompson /* 31102ac6454SAndrew Thompson * For dual-band RF, RF registers R1 and R4 also depend on channel number; 31202ac6454SAndrew Thompson * values taken from the reference driver. 31302ac6454SAndrew Thompson */ 31402ac6454SAndrew Thompson static const struct { 31502ac6454SAndrew Thompson uint8_t chan; 31602ac6454SAndrew Thompson uint32_t r1; 31702ac6454SAndrew Thompson uint32_t r2; 31802ac6454SAndrew Thompson uint32_t r4; 31902ac6454SAndrew Thompson } ural_rf5222[] = { 32002ac6454SAndrew Thompson { 1, 0x08808, 0x0044d, 0x00282 }, 32102ac6454SAndrew Thompson { 2, 0x08808, 0x0044e, 0x00282 }, 32202ac6454SAndrew Thompson { 3, 0x08808, 0x0044f, 0x00282 }, 32302ac6454SAndrew Thompson { 4, 0x08808, 0x00460, 0x00282 }, 32402ac6454SAndrew Thompson { 5, 0x08808, 0x00461, 0x00282 }, 32502ac6454SAndrew Thompson { 6, 0x08808, 0x00462, 0x00282 }, 32602ac6454SAndrew Thompson { 7, 0x08808, 0x00463, 0x00282 }, 32702ac6454SAndrew Thompson { 8, 0x08808, 0x00464, 0x00282 }, 32802ac6454SAndrew Thompson { 9, 0x08808, 0x00465, 0x00282 }, 32902ac6454SAndrew Thompson { 10, 0x08808, 0x00466, 0x00282 }, 33002ac6454SAndrew Thompson { 11, 0x08808, 0x00467, 0x00282 }, 33102ac6454SAndrew Thompson { 12, 0x08808, 0x00468, 0x00282 }, 33202ac6454SAndrew Thompson { 13, 0x08808, 0x00469, 0x00282 }, 33302ac6454SAndrew Thompson { 14, 0x08808, 0x0046b, 0x00286 }, 33402ac6454SAndrew Thompson 33502ac6454SAndrew Thompson { 36, 0x08804, 0x06225, 0x00287 }, 33602ac6454SAndrew Thompson { 40, 0x08804, 0x06226, 0x00287 }, 33702ac6454SAndrew Thompson { 44, 0x08804, 0x06227, 0x00287 }, 33802ac6454SAndrew Thompson { 48, 0x08804, 0x06228, 0x00287 }, 33902ac6454SAndrew Thompson { 52, 0x08804, 0x06229, 0x00287 }, 34002ac6454SAndrew Thompson { 56, 0x08804, 0x0622a, 0x00287 }, 34102ac6454SAndrew Thompson { 60, 0x08804, 0x0622b, 0x00287 }, 34202ac6454SAndrew Thompson { 64, 0x08804, 0x0622c, 0x00287 }, 34302ac6454SAndrew Thompson 34402ac6454SAndrew Thompson { 100, 0x08804, 0x02200, 0x00283 }, 34502ac6454SAndrew Thompson { 104, 0x08804, 0x02201, 0x00283 }, 34602ac6454SAndrew Thompson { 108, 0x08804, 0x02202, 0x00283 }, 34702ac6454SAndrew Thompson { 112, 0x08804, 0x02203, 0x00283 }, 34802ac6454SAndrew Thompson { 116, 0x08804, 0x02204, 0x00283 }, 34902ac6454SAndrew Thompson { 120, 0x08804, 0x02205, 0x00283 }, 35002ac6454SAndrew Thompson { 124, 0x08804, 0x02206, 0x00283 }, 35102ac6454SAndrew Thompson { 128, 0x08804, 0x02207, 0x00283 }, 35202ac6454SAndrew Thompson { 132, 0x08804, 0x02208, 0x00283 }, 35302ac6454SAndrew Thompson { 136, 0x08804, 0x02209, 0x00283 }, 35402ac6454SAndrew Thompson { 140, 0x08804, 0x0220a, 0x00283 }, 35502ac6454SAndrew Thompson 35602ac6454SAndrew Thompson { 149, 0x08808, 0x02429, 0x00281 }, 35702ac6454SAndrew Thompson { 153, 0x08808, 0x0242b, 0x00281 }, 35802ac6454SAndrew Thompson { 157, 0x08808, 0x0242d, 0x00281 }, 35902ac6454SAndrew Thompson { 161, 0x08808, 0x0242f, 0x00281 } 36002ac6454SAndrew Thompson }; 36102ac6454SAndrew Thompson 362760bc48eSAndrew Thompson static const struct usb_config ural_config[URAL_N_TRANSFER] = { 36302ac6454SAndrew Thompson [URAL_BULK_WR] = { 36402ac6454SAndrew Thompson .type = UE_BULK, 36502ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 36602ac6454SAndrew Thompson .direction = UE_DIR_OUT, 3674eae601eSAndrew Thompson .bufsize = (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE + 4), 3684eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 3694eae601eSAndrew Thompson .callback = ural_bulk_write_callback, 3704eae601eSAndrew Thompson .timeout = 5000, /* ms */ 37102ac6454SAndrew Thompson }, 37202ac6454SAndrew Thompson [URAL_BULK_RD] = { 37302ac6454SAndrew Thompson .type = UE_BULK, 37402ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 37502ac6454SAndrew Thompson .direction = UE_DIR_IN, 3764eae601eSAndrew Thompson .bufsize = (RAL_FRAME_SIZE + RAL_RX_DESC_SIZE), 3774eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 3784eae601eSAndrew Thompson .callback = ural_bulk_read_callback, 37902ac6454SAndrew Thompson }, 38002ac6454SAndrew Thompson }; 38102ac6454SAndrew Thompson 38202ac6454SAndrew Thompson static device_probe_t ural_match; 38302ac6454SAndrew Thompson static device_attach_t ural_attach; 38402ac6454SAndrew Thompson static device_detach_t ural_detach; 38502ac6454SAndrew Thompson 38602ac6454SAndrew Thompson static device_method_t ural_methods[] = { 38702ac6454SAndrew Thompson /* Device interface */ 38802ac6454SAndrew Thompson DEVMETHOD(device_probe, ural_match), 38902ac6454SAndrew Thompson DEVMETHOD(device_attach, ural_attach), 39002ac6454SAndrew Thompson DEVMETHOD(device_detach, ural_detach), 39102ac6454SAndrew Thompson 39202ac6454SAndrew Thompson { 0, 0 } 39302ac6454SAndrew Thompson }; 39402ac6454SAndrew Thompson 39502ac6454SAndrew Thompson static driver_t ural_driver = { 39602ac6454SAndrew Thompson .name = "ural", 39702ac6454SAndrew Thompson .methods = ural_methods, 39802ac6454SAndrew Thompson .size = sizeof(struct ural_softc), 39902ac6454SAndrew Thompson }; 40002ac6454SAndrew Thompson 40102ac6454SAndrew Thompson static devclass_t ural_devclass; 40202ac6454SAndrew Thompson 4039aef556dSAndrew Thompson DRIVER_MODULE(ural, uhub, ural_driver, ural_devclass, NULL, 0); 40402ac6454SAndrew Thompson MODULE_DEPEND(ural, usb, 1, 1, 1); 40502ac6454SAndrew Thompson MODULE_DEPEND(ural, wlan, 1, 1, 1); 40602ac6454SAndrew Thompson MODULE_DEPEND(ural, wlan_amrr, 1, 1, 1); 40702ac6454SAndrew Thompson 40802ac6454SAndrew Thompson static int 40902ac6454SAndrew Thompson ural_match(device_t self) 41002ac6454SAndrew Thompson { 411760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(self); 41202ac6454SAndrew Thompson 413f29a0724SAndrew Thompson if (uaa->usb_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 != RAL_IFACE_INDEX) 41802ac6454SAndrew Thompson return (ENXIO); 41902ac6454SAndrew Thompson 420a593f6b8SAndrew Thompson return (usbd_lookup_id_by_uaa(ural_devs, sizeof(ural_devs), uaa)); 42102ac6454SAndrew Thompson } 42202ac6454SAndrew Thompson 42302ac6454SAndrew Thompson static int 42402ac6454SAndrew Thompson ural_attach(device_t self) 42502ac6454SAndrew Thompson { 426760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(self); 42702ac6454SAndrew Thompson struct ural_softc *sc = device_get_softc(self); 4285efea30fSAndrew Thompson struct ifnet *ifp; 4295efea30fSAndrew Thompson struct ieee80211com *ic; 4305efea30fSAndrew Thompson uint8_t iface_index, bands; 43102ac6454SAndrew Thompson int error; 43202ac6454SAndrew Thompson 433a593f6b8SAndrew Thompson device_set_usb_desc(self); 43402ac6454SAndrew Thompson sc->sc_udev = uaa->device; 43502ac6454SAndrew Thompson sc->sc_dev = self; 43602ac6454SAndrew Thompson 43702ac6454SAndrew Thompson mtx_init(&sc->sc_mtx, device_get_nameunit(self), 43802ac6454SAndrew Thompson MTX_NETWORK_LOCK, MTX_DEF); 43902ac6454SAndrew Thompson 44002ac6454SAndrew Thompson iface_index = RAL_IFACE_INDEX; 441a593f6b8SAndrew Thompson error = usbd_transfer_setup(uaa->device, 44202ac6454SAndrew Thompson &iface_index, sc->sc_xfer, ural_config, 44302ac6454SAndrew Thompson URAL_N_TRANSFER, sc, &sc->sc_mtx); 44402ac6454SAndrew Thompson if (error) { 44502ac6454SAndrew Thompson device_printf(self, "could not allocate USB transfers, " 446a593f6b8SAndrew Thompson "err=%s\n", usbd_errstr(error)); 44702ac6454SAndrew Thompson goto detach; 44802ac6454SAndrew Thompson } 44902ac6454SAndrew Thompson 45002ac6454SAndrew Thompson RAL_LOCK(sc); 45102ac6454SAndrew Thompson /* retrieve RT2570 rev. no */ 45202ac6454SAndrew Thompson sc->asic_rev = ural_read(sc, RAL_MAC_CSR0); 45302ac6454SAndrew Thompson 45402ac6454SAndrew Thompson /* retrieve MAC address and various other things from EEPROM */ 45502ac6454SAndrew Thompson ural_read_eeprom(sc); 45602ac6454SAndrew Thompson RAL_UNLOCK(sc); 45702ac6454SAndrew Thompson 4585efea30fSAndrew Thompson device_printf(self, "MAC/BBP RT2570 (rev 0x%02x), RF %s\n", 45902ac6454SAndrew Thompson sc->asic_rev, ural_get_rf(sc->rf_rev)); 46002ac6454SAndrew Thompson 46102ac6454SAndrew Thompson ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 46202ac6454SAndrew Thompson if (ifp == NULL) { 46302ac6454SAndrew Thompson device_printf(sc->sc_dev, "can not if_alloc()\n"); 4645efea30fSAndrew Thompson goto detach; 46502ac6454SAndrew Thompson } 46602ac6454SAndrew Thompson ic = ifp->if_l2com; 46702ac6454SAndrew Thompson 46802ac6454SAndrew Thompson ifp->if_softc = sc; 46902ac6454SAndrew Thompson if_initname(ifp, "ural", device_get_unit(sc->sc_dev)); 47002ac6454SAndrew Thompson ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 47102ac6454SAndrew Thompson ifp->if_init = ural_init; 47202ac6454SAndrew Thompson ifp->if_ioctl = ural_ioctl; 47302ac6454SAndrew Thompson ifp->if_start = ural_start; 47402ac6454SAndrew Thompson IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 47502ac6454SAndrew Thompson ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 47602ac6454SAndrew Thompson IFQ_SET_READY(&ifp->if_snd); 47702ac6454SAndrew Thompson 47802ac6454SAndrew Thompson ic->ic_ifp = ifp; 47902ac6454SAndrew Thompson ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 48002ac6454SAndrew Thompson 48102ac6454SAndrew Thompson /* set device capabilities */ 48202ac6454SAndrew Thompson ic->ic_caps = 48302ac6454SAndrew Thompson IEEE80211_C_STA /* station mode supported */ 48402ac6454SAndrew Thompson | IEEE80211_C_IBSS /* IBSS mode supported */ 48502ac6454SAndrew Thompson | IEEE80211_C_MONITOR /* monitor mode supported */ 48602ac6454SAndrew Thompson | IEEE80211_C_HOSTAP /* HostAp mode supported */ 48702ac6454SAndrew Thompson | IEEE80211_C_TXPMGT /* tx power management */ 48802ac6454SAndrew Thompson | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 48902ac6454SAndrew Thompson | IEEE80211_C_SHSLOT /* short slot time supported */ 49002ac6454SAndrew Thompson | IEEE80211_C_BGSCAN /* bg scanning supported */ 49102ac6454SAndrew Thompson | IEEE80211_C_WPA /* 802.11i */ 49202ac6454SAndrew Thompson ; 49302ac6454SAndrew Thompson 49402ac6454SAndrew Thompson bands = 0; 49502ac6454SAndrew Thompson setbit(&bands, IEEE80211_MODE_11B); 49602ac6454SAndrew Thompson setbit(&bands, IEEE80211_MODE_11G); 49702ac6454SAndrew Thompson if (sc->rf_rev == RAL_RF_5222) 49802ac6454SAndrew Thompson setbit(&bands, IEEE80211_MODE_11A); 49902ac6454SAndrew Thompson ieee80211_init_channels(ic, NULL, &bands); 50002ac6454SAndrew Thompson 50129aca940SSam Leffler ieee80211_ifattach(ic, sc->sc_bssid); 50277ddf3d3SAndrew Thompson ic->ic_update_promisc = ural_update_promisc; 50302ac6454SAndrew Thompson ic->ic_newassoc = ural_newassoc; 50402ac6454SAndrew Thompson ic->ic_raw_xmit = ural_raw_xmit; 50502ac6454SAndrew Thompson ic->ic_node_alloc = ural_node_alloc; 50602ac6454SAndrew Thompson ic->ic_scan_start = ural_scan_start; 50702ac6454SAndrew Thompson ic->ic_scan_end = ural_scan_end; 50802ac6454SAndrew Thompson ic->ic_set_channel = ural_set_channel; 50902ac6454SAndrew Thompson 51002ac6454SAndrew Thompson ic->ic_vap_create = ural_vap_create; 51102ac6454SAndrew Thompson ic->ic_vap_delete = ural_vap_delete; 51202ac6454SAndrew Thompson 5135463c4a4SSam Leffler ieee80211_radiotap_attach(ic, 5145463c4a4SSam Leffler &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap), 5155463c4a4SSam Leffler RAL_TX_RADIOTAP_PRESENT, 5165463c4a4SSam Leffler &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 5175463c4a4SSam Leffler RAL_RX_RADIOTAP_PRESENT); 51802ac6454SAndrew Thompson 51902ac6454SAndrew Thompson if (bootverbose) 52002ac6454SAndrew Thompson ieee80211_announce(ic); 52102ac6454SAndrew Thompson 5225efea30fSAndrew Thompson return (0); 5235efea30fSAndrew Thompson 5245efea30fSAndrew Thompson detach: 5255efea30fSAndrew Thompson ural_detach(self); 5265efea30fSAndrew Thompson return (ENXIO); /* failure */ 52702ac6454SAndrew Thompson } 52802ac6454SAndrew Thompson 52902ac6454SAndrew Thompson static int 53002ac6454SAndrew Thompson ural_detach(device_t self) 53102ac6454SAndrew Thompson { 53202ac6454SAndrew Thompson struct ural_softc *sc = device_get_softc(self); 53302ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 53402ac6454SAndrew Thompson struct ieee80211com *ic; 53502ac6454SAndrew Thompson 53602ac6454SAndrew Thompson /* stop all USB transfers */ 537a593f6b8SAndrew Thompson usbd_transfer_unsetup(sc->sc_xfer, URAL_N_TRANSFER); 53802ac6454SAndrew Thompson 53902ac6454SAndrew Thompson /* free TX list, if any */ 54002ac6454SAndrew Thompson RAL_LOCK(sc); 54102ac6454SAndrew Thompson ural_unsetup_tx_list(sc); 54202ac6454SAndrew Thompson RAL_UNLOCK(sc); 54302ac6454SAndrew Thompson 54402ac6454SAndrew Thompson if (ifp) { 54502ac6454SAndrew Thompson ic = ifp->if_l2com; 54602ac6454SAndrew Thompson ieee80211_ifdetach(ic); 54702ac6454SAndrew Thompson if_free(ifp); 54802ac6454SAndrew Thompson } 54902ac6454SAndrew Thompson mtx_destroy(&sc->sc_mtx); 55002ac6454SAndrew Thompson 55102ac6454SAndrew Thompson return (0); 55202ac6454SAndrew Thompson } 55302ac6454SAndrew Thompson 554e0a69b51SAndrew Thompson static usb_error_t 55577ddf3d3SAndrew Thompson ural_do_request(struct ural_softc *sc, 556760bc48eSAndrew Thompson struct usb_device_request *req, void *data) 55777ddf3d3SAndrew Thompson { 558e0a69b51SAndrew Thompson usb_error_t err; 55977ddf3d3SAndrew Thompson int ntries = 10; 56077ddf3d3SAndrew Thompson 56177ddf3d3SAndrew Thompson while (ntries--) { 562a593f6b8SAndrew Thompson err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx, 56377ddf3d3SAndrew Thompson req, data, 0, NULL, 250 /* ms */); 56477ddf3d3SAndrew Thompson if (err == 0) 56577ddf3d3SAndrew Thompson break; 56677ddf3d3SAndrew Thompson 56777ddf3d3SAndrew Thompson DPRINTFN(1, "Control request failed, %s (retrying)\n", 568a593f6b8SAndrew Thompson usbd_errstr(err)); 56977ddf3d3SAndrew Thompson if (ural_pause(sc, hz / 100)) 57077ddf3d3SAndrew Thompson break; 57177ddf3d3SAndrew Thompson } 57277ddf3d3SAndrew Thompson return (err); 57377ddf3d3SAndrew Thompson } 57477ddf3d3SAndrew Thompson 57502ac6454SAndrew Thompson static struct ieee80211vap * 57602ac6454SAndrew Thompson ural_vap_create(struct ieee80211com *ic, 57702ac6454SAndrew Thompson const char name[IFNAMSIZ], int unit, int opmode, int flags, 57802ac6454SAndrew Thompson const uint8_t bssid[IEEE80211_ADDR_LEN], 57902ac6454SAndrew Thompson const uint8_t mac[IEEE80211_ADDR_LEN]) 58002ac6454SAndrew Thompson { 58102ac6454SAndrew Thompson struct ural_softc *sc = ic->ic_ifp->if_softc; 58202ac6454SAndrew Thompson struct ural_vap *uvp; 58302ac6454SAndrew Thompson struct ieee80211vap *vap; 58402ac6454SAndrew Thompson 58502ac6454SAndrew Thompson if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 58602ac6454SAndrew Thompson return NULL; 58702ac6454SAndrew Thompson uvp = (struct ural_vap *) malloc(sizeof(struct ural_vap), 58802ac6454SAndrew Thompson M_80211_VAP, M_NOWAIT | M_ZERO); 58902ac6454SAndrew Thompson if (uvp == NULL) 59002ac6454SAndrew Thompson return NULL; 59102ac6454SAndrew Thompson vap = &uvp->vap; 59202ac6454SAndrew Thompson /* enable s/w bmiss handling for sta mode */ 59302ac6454SAndrew Thompson ieee80211_vap_setup(ic, vap, name, unit, opmode, 59402ac6454SAndrew Thompson flags | IEEE80211_CLONE_NOBEACONS, bssid, mac); 59502ac6454SAndrew Thompson 59602ac6454SAndrew Thompson /* override state transition machine */ 59702ac6454SAndrew Thompson uvp->newstate = vap->iv_newstate; 59802ac6454SAndrew Thompson vap->iv_newstate = ural_newstate; 59902ac6454SAndrew Thompson 600a593f6b8SAndrew Thompson usb_callout_init_mtx(&uvp->amrr_ch, &sc->sc_mtx, 0); 6015efea30fSAndrew Thompson TASK_INIT(&uvp->amrr_task, 0, ural_amrr_task, uvp); 60202ac6454SAndrew Thompson ieee80211_amrr_init(&uvp->amrr, vap, 60302ac6454SAndrew Thompson IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD, 60402ac6454SAndrew Thompson IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD, 60502ac6454SAndrew Thompson 1000 /* 1 sec */); 60602ac6454SAndrew Thompson 60702ac6454SAndrew Thompson /* complete setup */ 60802ac6454SAndrew Thompson ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status); 60902ac6454SAndrew Thompson ic->ic_opmode = opmode; 61002ac6454SAndrew Thompson return vap; 61102ac6454SAndrew Thompson } 61202ac6454SAndrew Thompson 61302ac6454SAndrew Thompson static void 61402ac6454SAndrew Thompson ural_vap_delete(struct ieee80211vap *vap) 61502ac6454SAndrew Thompson { 61602ac6454SAndrew Thompson struct ural_vap *uvp = URAL_VAP(vap); 6175efea30fSAndrew Thompson struct ieee80211com *ic = vap->iv_ic; 61802ac6454SAndrew Thompson 619a593f6b8SAndrew Thompson usb_callout_drain(&uvp->amrr_ch); 6205efea30fSAndrew Thompson ieee80211_draintask(ic, &uvp->amrr_task); 62102ac6454SAndrew Thompson ieee80211_amrr_cleanup(&uvp->amrr); 62202ac6454SAndrew Thompson ieee80211_vap_detach(vap); 62302ac6454SAndrew Thompson free(uvp, M_80211_VAP); 62402ac6454SAndrew Thompson } 62502ac6454SAndrew Thompson 62602ac6454SAndrew Thompson static void 62702ac6454SAndrew Thompson ural_tx_free(struct ural_tx_data *data, int txerr) 62802ac6454SAndrew Thompson { 62902ac6454SAndrew Thompson struct ural_softc *sc = data->sc; 63002ac6454SAndrew Thompson 63102ac6454SAndrew Thompson if (data->m != NULL) { 63202ac6454SAndrew Thompson if (data->m->m_flags & M_TXCB) 63302ac6454SAndrew Thompson ieee80211_process_callback(data->ni, data->m, 63402ac6454SAndrew Thompson txerr ? ETIMEDOUT : 0); 63502ac6454SAndrew Thompson m_freem(data->m); 63602ac6454SAndrew Thompson data->m = NULL; 63702ac6454SAndrew Thompson 63802ac6454SAndrew Thompson ieee80211_free_node(data->ni); 63902ac6454SAndrew Thompson data->ni = NULL; 64002ac6454SAndrew Thompson } 64102ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 64202ac6454SAndrew Thompson sc->tx_nfree++; 64302ac6454SAndrew Thompson } 64402ac6454SAndrew Thompson 64502ac6454SAndrew Thompson static void 64602ac6454SAndrew Thompson ural_setup_tx_list(struct ural_softc *sc) 64702ac6454SAndrew Thompson { 64802ac6454SAndrew Thompson struct ural_tx_data *data; 64902ac6454SAndrew Thompson int i; 65002ac6454SAndrew Thompson 65102ac6454SAndrew Thompson sc->tx_nfree = 0; 65202ac6454SAndrew Thompson STAILQ_INIT(&sc->tx_q); 65302ac6454SAndrew Thompson STAILQ_INIT(&sc->tx_free); 65402ac6454SAndrew Thompson 65502ac6454SAndrew Thompson for (i = 0; i < RAL_TX_LIST_COUNT; i++) { 65602ac6454SAndrew Thompson data = &sc->tx_data[i]; 65702ac6454SAndrew Thompson 65802ac6454SAndrew Thompson data->sc = sc; 65902ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 66002ac6454SAndrew Thompson sc->tx_nfree++; 66102ac6454SAndrew Thompson } 66202ac6454SAndrew Thompson } 66302ac6454SAndrew Thompson 66402ac6454SAndrew Thompson static void 66502ac6454SAndrew Thompson ural_unsetup_tx_list(struct ural_softc *sc) 66602ac6454SAndrew Thompson { 66702ac6454SAndrew Thompson struct ural_tx_data *data; 66802ac6454SAndrew Thompson int i; 66902ac6454SAndrew Thompson 67002ac6454SAndrew Thompson /* make sure any subsequent use of the queues will fail */ 67102ac6454SAndrew Thompson sc->tx_nfree = 0; 67202ac6454SAndrew Thompson STAILQ_INIT(&sc->tx_q); 67302ac6454SAndrew Thompson STAILQ_INIT(&sc->tx_free); 67402ac6454SAndrew Thompson 67502ac6454SAndrew Thompson /* free up all node references and mbufs */ 67602ac6454SAndrew Thompson for (i = 0; i < RAL_TX_LIST_COUNT; i++) { 67702ac6454SAndrew Thompson data = &sc->tx_data[i]; 67802ac6454SAndrew Thompson 67902ac6454SAndrew Thompson if (data->m != NULL) { 68002ac6454SAndrew Thompson m_freem(data->m); 68102ac6454SAndrew Thompson data->m = NULL; 68202ac6454SAndrew Thompson } 68302ac6454SAndrew Thompson if (data->ni != NULL) { 68402ac6454SAndrew Thompson ieee80211_free_node(data->ni); 68502ac6454SAndrew Thompson data->ni = NULL; 68602ac6454SAndrew Thompson } 68702ac6454SAndrew Thompson } 68802ac6454SAndrew Thompson } 68902ac6454SAndrew Thompson 6905efea30fSAndrew Thompson static int 6915efea30fSAndrew Thompson ural_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 69202ac6454SAndrew Thompson { 69302ac6454SAndrew Thompson struct ural_vap *uvp = URAL_VAP(vap); 6945efea30fSAndrew Thompson struct ieee80211com *ic = vap->iv_ic; 6955efea30fSAndrew Thompson struct ural_softc *sc = ic->ic_ifp->if_softc; 69602ac6454SAndrew Thompson const struct ieee80211_txparam *tp; 69702ac6454SAndrew Thompson struct ieee80211_node *ni; 69802ac6454SAndrew Thompson struct mbuf *m; 69902ac6454SAndrew Thompson 7005efea30fSAndrew Thompson DPRINTF("%s -> %s\n", 7015efea30fSAndrew Thompson ieee80211_state_name[vap->iv_state], 7025efea30fSAndrew Thompson ieee80211_state_name[nstate]); 70302ac6454SAndrew Thompson 7045efea30fSAndrew Thompson IEEE80211_UNLOCK(ic); 7055efea30fSAndrew Thompson RAL_LOCK(sc); 706a593f6b8SAndrew Thompson usb_callout_stop(&uvp->amrr_ch); 7075efea30fSAndrew Thompson 7085efea30fSAndrew Thompson switch (nstate) { 70902ac6454SAndrew Thompson case IEEE80211_S_INIT: 7105efea30fSAndrew Thompson if (vap->iv_state == IEEE80211_S_RUN) { 71102ac6454SAndrew Thompson /* abort TSF synchronization */ 71202ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR19, 0); 71302ac6454SAndrew Thompson 71402ac6454SAndrew Thompson /* force tx led to stop blinking */ 71502ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR20, 0); 71602ac6454SAndrew Thompson } 71702ac6454SAndrew Thompson break; 71802ac6454SAndrew Thompson 71902ac6454SAndrew Thompson case IEEE80211_S_RUN: 72002ac6454SAndrew Thompson ni = vap->iv_bss; 72102ac6454SAndrew Thompson 72202ac6454SAndrew Thompson if (vap->iv_opmode != IEEE80211_M_MONITOR) { 72302ac6454SAndrew Thompson ural_update_slot(ic->ic_ifp); 72402ac6454SAndrew Thompson ural_set_txpreamble(sc); 72502ac6454SAndrew Thompson ural_set_basicrates(sc, ic->ic_bsschan); 72602ac6454SAndrew Thompson IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid); 72702ac6454SAndrew Thompson ural_set_bssid(sc, sc->sc_bssid); 72802ac6454SAndrew Thompson } 72902ac6454SAndrew Thompson 73002ac6454SAndrew Thompson if (vap->iv_opmode == IEEE80211_M_HOSTAP || 73102ac6454SAndrew Thompson vap->iv_opmode == IEEE80211_M_IBSS) { 73202ac6454SAndrew Thompson m = ieee80211_beacon_alloc(ni, &uvp->bo); 73302ac6454SAndrew Thompson if (m == NULL) { 73402ac6454SAndrew Thompson device_printf(sc->sc_dev, 73502ac6454SAndrew Thompson "could not allocate beacon\n"); 7365efea30fSAndrew Thompson RAL_UNLOCK(sc); 7375efea30fSAndrew Thompson IEEE80211_LOCK(ic); 7385efea30fSAndrew Thompson return (-1); 73902ac6454SAndrew Thompson } 7406fca0210SAndrew Thompson ieee80211_ref_node(ni); 74102ac6454SAndrew Thompson if (ural_tx_bcn(sc, m, ni) != 0) { 74202ac6454SAndrew Thompson device_printf(sc->sc_dev, 74302ac6454SAndrew Thompson "could not send beacon\n"); 7445efea30fSAndrew Thompson RAL_UNLOCK(sc); 7455efea30fSAndrew Thompson IEEE80211_LOCK(ic); 7465efea30fSAndrew Thompson return (-1); 74702ac6454SAndrew Thompson } 74802ac6454SAndrew Thompson } 74902ac6454SAndrew Thompson 75002ac6454SAndrew Thompson /* make tx led blink on tx (controlled by ASIC) */ 75102ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR20, 1); 75202ac6454SAndrew Thompson 75302ac6454SAndrew Thompson if (vap->iv_opmode != IEEE80211_M_MONITOR) 75402ac6454SAndrew Thompson ural_enable_tsf_sync(sc); 7555463c4a4SSam Leffler else 7565463c4a4SSam Leffler ural_enable_tsf(sc); 75702ac6454SAndrew Thompson 75802ac6454SAndrew Thompson /* enable automatic rate adaptation */ 7595463c4a4SSam Leffler /* XXX should use ic_bsschan but not valid until after newstate call below */ 7605463c4a4SSam Leffler tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 76102ac6454SAndrew Thompson if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) 76202ac6454SAndrew Thompson ural_amrr_start(sc, ni); 76302ac6454SAndrew Thompson 76402ac6454SAndrew Thompson break; 76502ac6454SAndrew Thompson 76602ac6454SAndrew Thompson default: 76702ac6454SAndrew Thompson break; 76802ac6454SAndrew Thompson } 76902ac6454SAndrew Thompson RAL_UNLOCK(sc); 77002ac6454SAndrew Thompson IEEE80211_LOCK(ic); 7715efea30fSAndrew Thompson return (uvp->newstate(vap, nstate, arg)); 77202ac6454SAndrew Thompson } 77302ac6454SAndrew Thompson 77402ac6454SAndrew Thompson 77502ac6454SAndrew Thompson static void 776ed6d949aSAndrew Thompson ural_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 77702ac6454SAndrew Thompson { 778ed6d949aSAndrew Thompson struct ural_softc *sc = usbd_xfer_softc(xfer); 77902ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 7805463c4a4SSam Leffler struct ieee80211vap *vap; 78102ac6454SAndrew Thompson struct ural_tx_data *data; 78202ac6454SAndrew Thompson struct mbuf *m; 783ed6d949aSAndrew Thompson struct usb_page_cache *pc; 784ed6d949aSAndrew Thompson int len; 785ed6d949aSAndrew Thompson 786ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 78702ac6454SAndrew Thompson 78802ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 78902ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 790ed6d949aSAndrew Thompson DPRINTFN(11, "transfer complete, %d bytes\n", len); 79102ac6454SAndrew Thompson 79202ac6454SAndrew Thompson /* free resources */ 793ed6d949aSAndrew Thompson data = usbd_xfer_get_priv(xfer); 79402ac6454SAndrew Thompson ural_tx_free(data, 0); 795ed6d949aSAndrew Thompson usbd_xfer_set_priv(xfer, NULL); 79602ac6454SAndrew Thompson 79702ac6454SAndrew Thompson ifp->if_opackets++; 79802ac6454SAndrew Thompson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 79902ac6454SAndrew Thompson 80002ac6454SAndrew Thompson /* FALLTHROUGH */ 80102ac6454SAndrew Thompson case USB_ST_SETUP: 80202ac6454SAndrew Thompson tr_setup: 80302ac6454SAndrew Thompson data = STAILQ_FIRST(&sc->tx_q); 80402ac6454SAndrew Thompson if (data) { 80502ac6454SAndrew Thompson STAILQ_REMOVE_HEAD(&sc->tx_q, next); 80602ac6454SAndrew Thompson m = data->m; 80702ac6454SAndrew Thompson 80802ac6454SAndrew Thompson if (m->m_pkthdr.len > (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE)) { 80902ac6454SAndrew Thompson DPRINTFN(0, "data overflow, %u bytes\n", 81002ac6454SAndrew Thompson m->m_pkthdr.len); 81102ac6454SAndrew Thompson m->m_pkthdr.len = (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE); 81202ac6454SAndrew Thompson } 813ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 814ed6d949aSAndrew Thompson usbd_copy_in(pc, 0, &data->desc, RAL_TX_DESC_SIZE); 815ed6d949aSAndrew Thompson usbd_m_copy_in(pc, RAL_TX_DESC_SIZE, m, 0, 81602ac6454SAndrew Thompson m->m_pkthdr.len); 81702ac6454SAndrew Thompson 8185463c4a4SSam Leffler vap = data->ni->ni_vap; 8195463c4a4SSam Leffler if (ieee80211_radiotap_active_vap(vap)) { 82002ac6454SAndrew Thompson struct ural_tx_radiotap_header *tap = &sc->sc_txtap; 82102ac6454SAndrew Thompson 82202ac6454SAndrew Thompson tap->wt_flags = 0; 82302ac6454SAndrew Thompson tap->wt_rate = data->rate; 82402ac6454SAndrew Thompson tap->wt_antenna = sc->tx_ant; 82502ac6454SAndrew Thompson 8265463c4a4SSam Leffler ieee80211_radiotap_tx(vap, m); 82702ac6454SAndrew Thompson } 82802ac6454SAndrew Thompson 82902ac6454SAndrew Thompson /* xfer length needs to be a multiple of two! */ 83002ac6454SAndrew Thompson len = (RAL_TX_DESC_SIZE + m->m_pkthdr.len + 1) & ~1; 83102ac6454SAndrew Thompson if ((len % 64) == 0) 83202ac6454SAndrew Thompson len += 2; 83302ac6454SAndrew Thompson 83402ac6454SAndrew Thompson DPRINTFN(11, "sending frame len=%u xferlen=%u\n", 83502ac6454SAndrew Thompson m->m_pkthdr.len, len); 83602ac6454SAndrew Thompson 837ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, len); 838ed6d949aSAndrew Thompson usbd_xfer_set_priv(xfer, data); 83902ac6454SAndrew Thompson 840a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 84102ac6454SAndrew Thompson } 842941da6d3SWeongyo Jeong RAL_UNLOCK(sc); 843941da6d3SWeongyo Jeong ural_start(ifp); 844941da6d3SWeongyo Jeong RAL_LOCK(sc); 84502ac6454SAndrew Thompson break; 84602ac6454SAndrew Thompson 84702ac6454SAndrew Thompson default: /* Error */ 84802ac6454SAndrew Thompson DPRINTFN(11, "transfer error, %s\n", 849ed6d949aSAndrew Thompson usbd_errstr(error)); 85002ac6454SAndrew Thompson 85102ac6454SAndrew Thompson ifp->if_oerrors++; 852ed6d949aSAndrew Thompson data = usbd_xfer_get_priv(xfer); 85302ac6454SAndrew Thompson if (data != NULL) { 854ed6d949aSAndrew Thompson ural_tx_free(data, error); 855ed6d949aSAndrew Thompson usbd_xfer_set_priv(xfer, NULL); 85602ac6454SAndrew Thompson } 85702ac6454SAndrew Thompson 858ed6d949aSAndrew Thompson if (error == USB_ERR_STALLED) { 85902ac6454SAndrew Thompson /* try to clear stall first */ 860ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 86102ac6454SAndrew Thompson goto tr_setup; 86202ac6454SAndrew Thompson } 863ed6d949aSAndrew Thompson if (error == USB_ERR_TIMEOUT) 86402ac6454SAndrew Thompson device_printf(sc->sc_dev, "device timeout\n"); 86502ac6454SAndrew Thompson break; 86602ac6454SAndrew Thompson } 86702ac6454SAndrew Thompson } 86802ac6454SAndrew Thompson 86902ac6454SAndrew Thompson static void 870ed6d949aSAndrew Thompson ural_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 87102ac6454SAndrew Thompson { 872ed6d949aSAndrew Thompson struct ural_softc *sc = usbd_xfer_softc(xfer); 87302ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 87402ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 87502ac6454SAndrew Thompson struct ieee80211_node *ni; 87602ac6454SAndrew Thompson struct mbuf *m = NULL; 877ed6d949aSAndrew Thompson struct usb_page_cache *pc; 87802ac6454SAndrew Thompson uint32_t flags; 8795463c4a4SSam Leffler int8_t rssi = 0, nf = 0; 880ed6d949aSAndrew Thompson int len; 881ed6d949aSAndrew Thompson 882ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 88302ac6454SAndrew Thompson 88402ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 88502ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 88602ac6454SAndrew Thompson 887ed6d949aSAndrew Thompson DPRINTFN(15, "rx done, actlen=%d\n", len); 88802ac6454SAndrew Thompson 88902ac6454SAndrew Thompson if (len < RAL_RX_DESC_SIZE + IEEE80211_MIN_LEN) { 89002ac6454SAndrew Thompson DPRINTF("%s: xfer too short %d\n", 89102ac6454SAndrew Thompson device_get_nameunit(sc->sc_dev), len); 89202ac6454SAndrew Thompson ifp->if_ierrors++; 89302ac6454SAndrew Thompson goto tr_setup; 89402ac6454SAndrew Thompson } 89502ac6454SAndrew Thompson 89602ac6454SAndrew Thompson len -= RAL_RX_DESC_SIZE; 89702ac6454SAndrew Thompson /* rx descriptor is located at the end */ 898ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 899ed6d949aSAndrew Thompson usbd_copy_out(pc, len, &sc->sc_rx_desc, RAL_RX_DESC_SIZE); 90002ac6454SAndrew Thompson 90102ac6454SAndrew Thompson rssi = URAL_RSSI(sc->sc_rx_desc.rssi); 9025463c4a4SSam Leffler nf = RAL_NOISE_FLOOR; 90302ac6454SAndrew Thompson flags = le32toh(sc->sc_rx_desc.flags); 90402ac6454SAndrew Thompson if (flags & (RAL_RX_PHY_ERROR | RAL_RX_CRC_ERROR)) { 90502ac6454SAndrew Thompson /* 90602ac6454SAndrew Thompson * This should not happen since we did not 90702ac6454SAndrew Thompson * request to receive those frames when we 90802ac6454SAndrew Thompson * filled RAL_TXRX_CSR2: 90902ac6454SAndrew Thompson */ 91002ac6454SAndrew Thompson DPRINTFN(5, "PHY or CRC error\n"); 91102ac6454SAndrew Thompson ifp->if_ierrors++; 91202ac6454SAndrew Thompson goto tr_setup; 91302ac6454SAndrew Thompson } 91402ac6454SAndrew Thompson 91502ac6454SAndrew Thompson m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 91602ac6454SAndrew Thompson if (m == NULL) { 91702ac6454SAndrew Thompson DPRINTF("could not allocate mbuf\n"); 91802ac6454SAndrew Thompson ifp->if_ierrors++; 91902ac6454SAndrew Thompson goto tr_setup; 92002ac6454SAndrew Thompson } 921ed6d949aSAndrew Thompson usbd_copy_out(pc, 0, mtod(m, uint8_t *), len); 92202ac6454SAndrew Thompson 92302ac6454SAndrew Thompson /* finalize mbuf */ 92402ac6454SAndrew Thompson m->m_pkthdr.rcvif = ifp; 92502ac6454SAndrew Thompson m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff; 92602ac6454SAndrew Thompson 9275463c4a4SSam Leffler if (ieee80211_radiotap_active(ic)) { 92802ac6454SAndrew Thompson struct ural_rx_radiotap_header *tap = &sc->sc_rxtap; 92902ac6454SAndrew Thompson 9305463c4a4SSam Leffler /* XXX set once */ 9315463c4a4SSam Leffler tap->wr_flags = 0; 93202ac6454SAndrew Thompson tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate, 93302ac6454SAndrew Thompson (flags & RAL_RX_OFDM) ? 93402ac6454SAndrew Thompson IEEE80211_T_OFDM : IEEE80211_T_CCK); 93502ac6454SAndrew Thompson tap->wr_antenna = sc->rx_ant; 9365463c4a4SSam Leffler tap->wr_antsignal = nf + rssi; 9375463c4a4SSam Leffler tap->wr_antnoise = nf; 93802ac6454SAndrew Thompson } 93902ac6454SAndrew Thompson /* Strip trailing 802.11 MAC FCS. */ 94002ac6454SAndrew Thompson m_adj(m, -IEEE80211_CRC_LEN); 94102ac6454SAndrew Thompson 94202ac6454SAndrew Thompson /* FALLTHROUGH */ 94302ac6454SAndrew Thompson case USB_ST_SETUP: 94402ac6454SAndrew Thompson tr_setup: 945ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 946a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 94702ac6454SAndrew Thompson 94802ac6454SAndrew Thompson /* 94902ac6454SAndrew Thompson * At the end of a USB callback it is always safe to unlock 95002ac6454SAndrew Thompson * the private mutex of a device! That is why we do the 95102ac6454SAndrew Thompson * "ieee80211_input" here, and not some lines up! 95202ac6454SAndrew Thompson */ 95302ac6454SAndrew Thompson RAL_UNLOCK(sc); 954941da6d3SWeongyo Jeong if (m) { 95502ac6454SAndrew Thompson ni = ieee80211_find_rxnode(ic, 95602ac6454SAndrew Thompson mtod(m, struct ieee80211_frame_min *)); 95702ac6454SAndrew Thompson if (ni != NULL) { 9585463c4a4SSam Leffler (void) ieee80211_input(ni, m, rssi, nf); 95902ac6454SAndrew Thompson ieee80211_free_node(ni); 96002ac6454SAndrew Thompson } else 9615463c4a4SSam Leffler (void) ieee80211_input_all(ic, m, rssi, nf); 96202ac6454SAndrew Thompson } 963941da6d3SWeongyo Jeong if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 && 964941da6d3SWeongyo Jeong !IFQ_IS_EMPTY(&ifp->if_snd)) 965941da6d3SWeongyo Jeong ural_start(ifp); 966941da6d3SWeongyo Jeong RAL_LOCK(sc); 96702ac6454SAndrew Thompson return; 96802ac6454SAndrew Thompson 96902ac6454SAndrew Thompson default: /* Error */ 970ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) { 97102ac6454SAndrew Thompson /* try to clear stall first */ 972ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 97302ac6454SAndrew Thompson goto tr_setup; 97402ac6454SAndrew Thompson } 97502ac6454SAndrew Thompson return; 97602ac6454SAndrew Thompson } 97702ac6454SAndrew Thompson } 97802ac6454SAndrew Thompson 97902ac6454SAndrew Thompson static uint8_t 98002ac6454SAndrew Thompson ural_plcp_signal(int rate) 98102ac6454SAndrew Thompson { 98202ac6454SAndrew Thompson switch (rate) { 98302ac6454SAndrew Thompson /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 98402ac6454SAndrew Thompson case 12: return 0xb; 98502ac6454SAndrew Thompson case 18: return 0xf; 98602ac6454SAndrew Thompson case 24: return 0xa; 98702ac6454SAndrew Thompson case 36: return 0xe; 98802ac6454SAndrew Thompson case 48: return 0x9; 98902ac6454SAndrew Thompson case 72: return 0xd; 99002ac6454SAndrew Thompson case 96: return 0x8; 99102ac6454SAndrew Thompson case 108: return 0xc; 99202ac6454SAndrew Thompson 99302ac6454SAndrew Thompson /* CCK rates (NB: not IEEE std, device-specific) */ 99402ac6454SAndrew Thompson case 2: return 0x0; 99502ac6454SAndrew Thompson case 4: return 0x1; 99602ac6454SAndrew Thompson case 11: return 0x2; 99702ac6454SAndrew Thompson case 22: return 0x3; 99802ac6454SAndrew Thompson } 99902ac6454SAndrew Thompson return 0xff; /* XXX unsupported/unknown rate */ 100002ac6454SAndrew Thompson } 100102ac6454SAndrew Thompson 100202ac6454SAndrew Thompson static void 100302ac6454SAndrew Thompson ural_setup_tx_desc(struct ural_softc *sc, struct ural_tx_desc *desc, 100402ac6454SAndrew Thompson uint32_t flags, int len, int rate) 100502ac6454SAndrew Thompson { 100602ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 100702ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 100802ac6454SAndrew Thompson uint16_t plcp_length; 100902ac6454SAndrew Thompson int remainder; 101002ac6454SAndrew Thompson 101102ac6454SAndrew Thompson desc->flags = htole32(flags); 101202ac6454SAndrew Thompson desc->flags |= htole32(RAL_TX_NEWSEQ); 101302ac6454SAndrew Thompson desc->flags |= htole32(len << 16); 101402ac6454SAndrew Thompson 101502ac6454SAndrew Thompson desc->wme = htole16(RAL_AIFSN(2) | RAL_LOGCWMIN(3) | RAL_LOGCWMAX(5)); 101602ac6454SAndrew Thompson desc->wme |= htole16(RAL_IVOFFSET(sizeof (struct ieee80211_frame))); 101702ac6454SAndrew Thompson 101802ac6454SAndrew Thompson /* setup PLCP fields */ 101902ac6454SAndrew Thompson desc->plcp_signal = ural_plcp_signal(rate); 102002ac6454SAndrew Thompson desc->plcp_service = 4; 102102ac6454SAndrew Thompson 102202ac6454SAndrew Thompson len += IEEE80211_CRC_LEN; 102326d39e2cSSam Leffler if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) { 102402ac6454SAndrew Thompson desc->flags |= htole32(RAL_TX_OFDM); 102502ac6454SAndrew Thompson 102602ac6454SAndrew Thompson plcp_length = len & 0xfff; 102702ac6454SAndrew Thompson desc->plcp_length_hi = plcp_length >> 6; 102802ac6454SAndrew Thompson desc->plcp_length_lo = plcp_length & 0x3f; 102902ac6454SAndrew Thompson } else { 103002ac6454SAndrew Thompson plcp_length = (16 * len + rate - 1) / rate; 103102ac6454SAndrew Thompson if (rate == 22) { 103202ac6454SAndrew Thompson remainder = (16 * len) % 22; 103302ac6454SAndrew Thompson if (remainder != 0 && remainder < 7) 103402ac6454SAndrew Thompson desc->plcp_service |= RAL_PLCP_LENGEXT; 103502ac6454SAndrew Thompson } 103602ac6454SAndrew Thompson desc->plcp_length_hi = plcp_length >> 8; 103702ac6454SAndrew Thompson desc->plcp_length_lo = plcp_length & 0xff; 103802ac6454SAndrew Thompson 103902ac6454SAndrew Thompson if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 104002ac6454SAndrew Thompson desc->plcp_signal |= 0x08; 104102ac6454SAndrew Thompson } 104202ac6454SAndrew Thompson 104302ac6454SAndrew Thompson desc->iv = 0; 104402ac6454SAndrew Thompson desc->eiv = 0; 104502ac6454SAndrew Thompson } 104602ac6454SAndrew Thompson 104702ac6454SAndrew Thompson #define RAL_TX_TIMEOUT 5000 104802ac6454SAndrew Thompson 104902ac6454SAndrew Thompson static int 105002ac6454SAndrew Thompson ural_tx_bcn(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 105102ac6454SAndrew Thompson { 105202ac6454SAndrew Thompson struct ieee80211vap *vap = ni->ni_vap; 105302ac6454SAndrew Thompson struct ieee80211com *ic = ni->ni_ic; 105402ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 105502ac6454SAndrew Thompson const struct ieee80211_txparam *tp; 105602ac6454SAndrew Thompson struct ural_tx_data *data; 105702ac6454SAndrew Thompson 105802ac6454SAndrew Thompson if (sc->tx_nfree == 0) { 105902ac6454SAndrew Thompson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 106002ac6454SAndrew Thompson m_freem(m0); 106102ac6454SAndrew Thompson ieee80211_free_node(ni); 106202ac6454SAndrew Thompson return EIO; 106302ac6454SAndrew Thompson } 106402ac6454SAndrew Thompson data = STAILQ_FIRST(&sc->tx_free); 106502ac6454SAndrew Thompson STAILQ_REMOVE_HEAD(&sc->tx_free, next); 106602ac6454SAndrew Thompson sc->tx_nfree--; 106702ac6454SAndrew Thompson tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)]; 106802ac6454SAndrew Thompson 106902ac6454SAndrew Thompson data->m = m0; 107002ac6454SAndrew Thompson data->ni = ni; 107102ac6454SAndrew Thompson data->rate = tp->mgmtrate; 107202ac6454SAndrew Thompson 107302ac6454SAndrew Thompson ural_setup_tx_desc(sc, &data->desc, 107402ac6454SAndrew Thompson RAL_TX_IFS_NEWBACKOFF | RAL_TX_TIMESTAMP, m0->m_pkthdr.len, 107502ac6454SAndrew Thompson tp->mgmtrate); 107602ac6454SAndrew Thompson 107702ac6454SAndrew Thompson DPRINTFN(10, "sending beacon frame len=%u rate=%u\n", 107802ac6454SAndrew Thompson m0->m_pkthdr.len, tp->mgmtrate); 107902ac6454SAndrew Thompson 108002ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1081a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]); 108202ac6454SAndrew Thompson 108302ac6454SAndrew Thompson return (0); 108402ac6454SAndrew Thompson } 108502ac6454SAndrew Thompson 108602ac6454SAndrew Thompson static int 108702ac6454SAndrew Thompson ural_tx_mgt(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 108802ac6454SAndrew Thompson { 108902ac6454SAndrew Thompson struct ieee80211vap *vap = ni->ni_vap; 109002ac6454SAndrew Thompson struct ieee80211com *ic = ni->ni_ic; 109102ac6454SAndrew Thompson const struct ieee80211_txparam *tp; 109202ac6454SAndrew Thompson struct ural_tx_data *data; 109302ac6454SAndrew Thompson struct ieee80211_frame *wh; 109402ac6454SAndrew Thompson struct ieee80211_key *k; 109502ac6454SAndrew Thompson uint32_t flags; 109602ac6454SAndrew Thompson uint16_t dur; 109702ac6454SAndrew Thompson 109802ac6454SAndrew Thompson RAL_LOCK_ASSERT(sc, MA_OWNED); 109902ac6454SAndrew Thompson 110002ac6454SAndrew Thompson data = STAILQ_FIRST(&sc->tx_free); 110102ac6454SAndrew Thompson STAILQ_REMOVE_HEAD(&sc->tx_free, next); 110202ac6454SAndrew Thompson sc->tx_nfree--; 110302ac6454SAndrew Thompson 110402ac6454SAndrew Thompson tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 110502ac6454SAndrew Thompson 110602ac6454SAndrew Thompson wh = mtod(m0, struct ieee80211_frame *); 110702ac6454SAndrew Thompson if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 110802ac6454SAndrew Thompson k = ieee80211_crypto_encap(ni, m0); 110902ac6454SAndrew Thompson if (k == NULL) { 111002ac6454SAndrew Thompson m_freem(m0); 111102ac6454SAndrew Thompson return ENOBUFS; 111202ac6454SAndrew Thompson } 111302ac6454SAndrew Thompson wh = mtod(m0, struct ieee80211_frame *); 111402ac6454SAndrew Thompson } 111502ac6454SAndrew Thompson 111602ac6454SAndrew Thompson data->m = m0; 111702ac6454SAndrew Thompson data->ni = ni; 111802ac6454SAndrew Thompson data->rate = tp->mgmtrate; 111902ac6454SAndrew Thompson 112002ac6454SAndrew Thompson flags = 0; 112102ac6454SAndrew Thompson if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 112202ac6454SAndrew Thompson flags |= RAL_TX_ACK; 112302ac6454SAndrew Thompson 112426d39e2cSSam Leffler dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate, 112502ac6454SAndrew Thompson ic->ic_flags & IEEE80211_F_SHPREAMBLE); 112602ac6454SAndrew Thompson *(uint16_t *)wh->i_dur = htole16(dur); 112702ac6454SAndrew Thompson 112802ac6454SAndrew Thompson /* tell hardware to add timestamp for probe responses */ 112902ac6454SAndrew Thompson if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 113002ac6454SAndrew Thompson IEEE80211_FC0_TYPE_MGT && 113102ac6454SAndrew Thompson (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 113202ac6454SAndrew Thompson IEEE80211_FC0_SUBTYPE_PROBE_RESP) 113302ac6454SAndrew Thompson flags |= RAL_TX_TIMESTAMP; 113402ac6454SAndrew Thompson } 113502ac6454SAndrew Thompson 113602ac6454SAndrew Thompson ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, tp->mgmtrate); 113702ac6454SAndrew Thompson 113802ac6454SAndrew Thompson DPRINTFN(10, "sending mgt frame len=%u rate=%u\n", 113902ac6454SAndrew Thompson m0->m_pkthdr.len, tp->mgmtrate); 114002ac6454SAndrew Thompson 114102ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1142a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]); 114302ac6454SAndrew Thompson 114402ac6454SAndrew Thompson return 0; 114502ac6454SAndrew Thompson } 114602ac6454SAndrew Thompson 114702ac6454SAndrew Thompson static int 114802ac6454SAndrew Thompson ural_sendprot(struct ural_softc *sc, 114902ac6454SAndrew Thompson const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate) 115002ac6454SAndrew Thompson { 115102ac6454SAndrew Thompson struct ieee80211com *ic = ni->ni_ic; 115202ac6454SAndrew Thompson const struct ieee80211_frame *wh; 115302ac6454SAndrew Thompson struct ural_tx_data *data; 115402ac6454SAndrew Thompson struct mbuf *mprot; 115502ac6454SAndrew Thompson int protrate, ackrate, pktlen, flags, isshort; 115602ac6454SAndrew Thompson uint16_t dur; 115702ac6454SAndrew Thompson 115802ac6454SAndrew Thompson KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY, 115902ac6454SAndrew Thompson ("protection %d", prot)); 116002ac6454SAndrew Thompson 116102ac6454SAndrew Thompson wh = mtod(m, const struct ieee80211_frame *); 116202ac6454SAndrew Thompson pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 116302ac6454SAndrew Thompson 116426d39e2cSSam Leffler protrate = ieee80211_ctl_rate(ic->ic_rt, rate); 116526d39e2cSSam Leffler ackrate = ieee80211_ack_rate(ic->ic_rt, rate); 116602ac6454SAndrew Thompson 116702ac6454SAndrew Thompson isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0; 116826d39e2cSSam Leffler dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort); 116926d39e2cSSam Leffler + ieee80211_ack_duration(ic->ic_rt, rate, isshort); 117002ac6454SAndrew Thompson flags = RAL_TX_RETRY(7); 117102ac6454SAndrew Thompson if (prot == IEEE80211_PROT_RTSCTS) { 117202ac6454SAndrew Thompson /* NB: CTS is the same size as an ACK */ 117326d39e2cSSam Leffler dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort); 117402ac6454SAndrew Thompson flags |= RAL_TX_ACK; 117502ac6454SAndrew Thompson mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur); 117602ac6454SAndrew Thompson } else { 117702ac6454SAndrew Thompson mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur); 117802ac6454SAndrew Thompson } 117902ac6454SAndrew Thompson if (mprot == NULL) { 118002ac6454SAndrew Thompson /* XXX stat + msg */ 118102ac6454SAndrew Thompson return ENOBUFS; 118202ac6454SAndrew Thompson } 118302ac6454SAndrew Thompson data = STAILQ_FIRST(&sc->tx_free); 118402ac6454SAndrew Thompson STAILQ_REMOVE_HEAD(&sc->tx_free, next); 118502ac6454SAndrew Thompson sc->tx_nfree--; 118602ac6454SAndrew Thompson 118702ac6454SAndrew Thompson data->m = mprot; 118802ac6454SAndrew Thompson data->ni = ieee80211_ref_node(ni); 118902ac6454SAndrew Thompson data->rate = protrate; 119002ac6454SAndrew Thompson ural_setup_tx_desc(sc, &data->desc, flags, mprot->m_pkthdr.len, protrate); 119102ac6454SAndrew Thompson 119202ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1193a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]); 119402ac6454SAndrew Thompson 119502ac6454SAndrew Thompson return 0; 119602ac6454SAndrew Thompson } 119702ac6454SAndrew Thompson 119802ac6454SAndrew Thompson static int 119902ac6454SAndrew Thompson ural_tx_raw(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni, 120002ac6454SAndrew Thompson const struct ieee80211_bpf_params *params) 120102ac6454SAndrew Thompson { 1202515db61dSSam Leffler struct ieee80211com *ic = ni->ni_ic; 120302ac6454SAndrew Thompson struct ural_tx_data *data; 120402ac6454SAndrew Thompson uint32_t flags; 120502ac6454SAndrew Thompson int error; 120602ac6454SAndrew Thompson int rate; 120702ac6454SAndrew Thompson 120802ac6454SAndrew Thompson RAL_LOCK_ASSERT(sc, MA_OWNED); 120902ac6454SAndrew Thompson KASSERT(params != NULL, ("no raw xmit params")); 121002ac6454SAndrew Thompson 1211515db61dSSam Leffler rate = params->ibp_rate0; 1212515db61dSSam Leffler if (!ieee80211_isratevalid(ic->ic_rt, rate)) { 121302ac6454SAndrew Thompson m_freem(m0); 121402ac6454SAndrew Thompson return EINVAL; 121502ac6454SAndrew Thompson } 121602ac6454SAndrew Thompson flags = 0; 121702ac6454SAndrew Thompson if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 121802ac6454SAndrew Thompson flags |= RAL_TX_ACK; 121902ac6454SAndrew Thompson if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) { 122002ac6454SAndrew Thompson error = ural_sendprot(sc, m0, ni, 122102ac6454SAndrew Thompson params->ibp_flags & IEEE80211_BPF_RTS ? 122202ac6454SAndrew Thompson IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY, 122302ac6454SAndrew Thompson rate); 122496ca458fSAndrew Thompson if (error || sc->tx_nfree == 0) { 122502ac6454SAndrew Thompson m_freem(m0); 122696ca458fSAndrew Thompson return ENOBUFS; 122702ac6454SAndrew Thompson } 122802ac6454SAndrew Thompson flags |= RAL_TX_IFS_SIFS; 122902ac6454SAndrew Thompson } 123002ac6454SAndrew Thompson 123196ca458fSAndrew Thompson data = STAILQ_FIRST(&sc->tx_free); 123296ca458fSAndrew Thompson STAILQ_REMOVE_HEAD(&sc->tx_free, next); 123396ca458fSAndrew Thompson sc->tx_nfree--; 123496ca458fSAndrew Thompson 123502ac6454SAndrew Thompson data->m = m0; 123602ac6454SAndrew Thompson data->ni = ni; 123702ac6454SAndrew Thompson data->rate = rate; 123802ac6454SAndrew Thompson 123902ac6454SAndrew Thompson /* XXX need to setup descriptor ourself */ 124002ac6454SAndrew Thompson ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, rate); 124102ac6454SAndrew Thompson 124202ac6454SAndrew Thompson DPRINTFN(10, "sending raw frame len=%u rate=%u\n", 124302ac6454SAndrew Thompson m0->m_pkthdr.len, rate); 124402ac6454SAndrew Thompson 124502ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1246a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]); 124702ac6454SAndrew Thompson 124802ac6454SAndrew Thompson return 0; 124902ac6454SAndrew Thompson } 125002ac6454SAndrew Thompson 125102ac6454SAndrew Thompson static int 125202ac6454SAndrew Thompson ural_tx_data(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 125302ac6454SAndrew Thompson { 125402ac6454SAndrew Thompson struct ieee80211vap *vap = ni->ni_vap; 125502ac6454SAndrew Thompson struct ieee80211com *ic = ni->ni_ic; 125602ac6454SAndrew Thompson struct ural_tx_data *data; 125702ac6454SAndrew Thompson struct ieee80211_frame *wh; 125802ac6454SAndrew Thompson const struct ieee80211_txparam *tp; 125902ac6454SAndrew Thompson struct ieee80211_key *k; 126002ac6454SAndrew Thompson uint32_t flags = 0; 126102ac6454SAndrew Thompson uint16_t dur; 126202ac6454SAndrew Thompson int error, rate; 126302ac6454SAndrew Thompson 126402ac6454SAndrew Thompson RAL_LOCK_ASSERT(sc, MA_OWNED); 126502ac6454SAndrew Thompson 126602ac6454SAndrew Thompson wh = mtod(m0, struct ieee80211_frame *); 126702ac6454SAndrew Thompson 126802ac6454SAndrew Thompson tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)]; 126902ac6454SAndrew Thompson if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 127002ac6454SAndrew Thompson rate = tp->mcastrate; 127102ac6454SAndrew Thompson else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 127202ac6454SAndrew Thompson rate = tp->ucastrate; 127302ac6454SAndrew Thompson else 127402ac6454SAndrew Thompson rate = ni->ni_txrate; 127502ac6454SAndrew Thompson 127602ac6454SAndrew Thompson if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 127702ac6454SAndrew Thompson k = ieee80211_crypto_encap(ni, m0); 127802ac6454SAndrew Thompson if (k == NULL) { 127902ac6454SAndrew Thompson m_freem(m0); 128002ac6454SAndrew Thompson return ENOBUFS; 128102ac6454SAndrew Thompson } 128202ac6454SAndrew Thompson /* packet header may have moved, reset our local pointer */ 128302ac6454SAndrew Thompson wh = mtod(m0, struct ieee80211_frame *); 128402ac6454SAndrew Thompson } 128502ac6454SAndrew Thompson 128602ac6454SAndrew Thompson if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 128702ac6454SAndrew Thompson int prot = IEEE80211_PROT_NONE; 128802ac6454SAndrew Thompson if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) 128902ac6454SAndrew Thompson prot = IEEE80211_PROT_RTSCTS; 129002ac6454SAndrew Thompson else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 129126d39e2cSSam Leffler ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) 129202ac6454SAndrew Thompson prot = ic->ic_protmode; 129302ac6454SAndrew Thompson if (prot != IEEE80211_PROT_NONE) { 129402ac6454SAndrew Thompson error = ural_sendprot(sc, m0, ni, prot, rate); 129596ca458fSAndrew Thompson if (error || sc->tx_nfree == 0) { 129602ac6454SAndrew Thompson m_freem(m0); 129796ca458fSAndrew Thompson return ENOBUFS; 129802ac6454SAndrew Thompson } 129902ac6454SAndrew Thompson flags |= RAL_TX_IFS_SIFS; 130002ac6454SAndrew Thompson } 130102ac6454SAndrew Thompson } 130202ac6454SAndrew Thompson 130302ac6454SAndrew Thompson data = STAILQ_FIRST(&sc->tx_free); 130402ac6454SAndrew Thompson STAILQ_REMOVE_HEAD(&sc->tx_free, next); 130502ac6454SAndrew Thompson sc->tx_nfree--; 130602ac6454SAndrew Thompson 130702ac6454SAndrew Thompson data->m = m0; 130802ac6454SAndrew Thompson data->ni = ni; 130902ac6454SAndrew Thompson data->rate = rate; 131002ac6454SAndrew Thompson 131102ac6454SAndrew Thompson if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 131202ac6454SAndrew Thompson flags |= RAL_TX_ACK; 131302ac6454SAndrew Thompson flags |= RAL_TX_RETRY(7); 131402ac6454SAndrew Thompson 131526d39e2cSSam Leffler dur = ieee80211_ack_duration(ic->ic_rt, rate, 131602ac6454SAndrew Thompson ic->ic_flags & IEEE80211_F_SHPREAMBLE); 131702ac6454SAndrew Thompson *(uint16_t *)wh->i_dur = htole16(dur); 131802ac6454SAndrew Thompson } 131902ac6454SAndrew Thompson 132002ac6454SAndrew Thompson ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, rate); 132102ac6454SAndrew Thompson 132202ac6454SAndrew Thompson DPRINTFN(10, "sending data frame len=%u rate=%u\n", 132302ac6454SAndrew Thompson m0->m_pkthdr.len, rate); 132402ac6454SAndrew Thompson 132502ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1326a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[URAL_BULK_WR]); 132702ac6454SAndrew Thompson 132802ac6454SAndrew Thompson return 0; 132902ac6454SAndrew Thompson } 133002ac6454SAndrew Thompson 133102ac6454SAndrew Thompson static void 133202ac6454SAndrew Thompson ural_start(struct ifnet *ifp) 133302ac6454SAndrew Thompson { 133402ac6454SAndrew Thompson struct ural_softc *sc = ifp->if_softc; 133502ac6454SAndrew Thompson struct ieee80211_node *ni; 133602ac6454SAndrew Thompson struct mbuf *m; 133702ac6454SAndrew Thompson 133802ac6454SAndrew Thompson RAL_LOCK(sc); 133902ac6454SAndrew Thompson if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 134002ac6454SAndrew Thompson RAL_UNLOCK(sc); 134102ac6454SAndrew Thompson return; 134202ac6454SAndrew Thompson } 134302ac6454SAndrew Thompson for (;;) { 134402ac6454SAndrew Thompson IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 134502ac6454SAndrew Thompson if (m == NULL) 134602ac6454SAndrew Thompson break; 134796ca458fSAndrew Thompson if (sc->tx_nfree < RAL_TX_MINFREE) { 134802ac6454SAndrew Thompson IFQ_DRV_PREPEND(&ifp->if_snd, m); 134902ac6454SAndrew Thompson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 135002ac6454SAndrew Thompson break; 135102ac6454SAndrew Thompson } 135202ac6454SAndrew Thompson ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 135302ac6454SAndrew Thompson if (ural_tx_data(sc, m, ni) != 0) { 135402ac6454SAndrew Thompson ieee80211_free_node(ni); 135502ac6454SAndrew Thompson ifp->if_oerrors++; 135602ac6454SAndrew Thompson break; 135702ac6454SAndrew Thompson } 135802ac6454SAndrew Thompson } 135902ac6454SAndrew Thompson RAL_UNLOCK(sc); 136002ac6454SAndrew Thompson } 136102ac6454SAndrew Thompson 136202ac6454SAndrew Thompson static int 136302ac6454SAndrew Thompson ural_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 136402ac6454SAndrew Thompson { 136502ac6454SAndrew Thompson struct ural_softc *sc = ifp->if_softc; 136602ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 136702ac6454SAndrew Thompson struct ifreq *ifr = (struct ifreq *) data; 136802ac6454SAndrew Thompson int error = 0, startall = 0; 136902ac6454SAndrew Thompson 137002ac6454SAndrew Thompson switch (cmd) { 137102ac6454SAndrew Thompson case SIOCSIFFLAGS: 137202ac6454SAndrew Thompson RAL_LOCK(sc); 137302ac6454SAndrew Thompson if (ifp->if_flags & IFF_UP) { 137402ac6454SAndrew Thompson if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 13755efea30fSAndrew Thompson ural_init_locked(sc); 137602ac6454SAndrew Thompson startall = 1; 137702ac6454SAndrew Thompson } else 13785efea30fSAndrew Thompson ural_setpromisc(sc); 137902ac6454SAndrew Thompson } else { 13805efea30fSAndrew Thompson if (ifp->if_drv_flags & IFF_DRV_RUNNING) 13815efea30fSAndrew Thompson ural_stop(sc); 138202ac6454SAndrew Thompson } 138302ac6454SAndrew Thompson RAL_UNLOCK(sc); 138402ac6454SAndrew Thompson if (startall) 138502ac6454SAndrew Thompson ieee80211_start_all(ic); 138602ac6454SAndrew Thompson break; 138702ac6454SAndrew Thompson case SIOCGIFMEDIA: 138802ac6454SAndrew Thompson case SIOCSIFMEDIA: 138902ac6454SAndrew Thompson error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 139002ac6454SAndrew Thompson break; 139102ac6454SAndrew Thompson default: 139202ac6454SAndrew Thompson error = ether_ioctl(ifp, cmd, data); 139302ac6454SAndrew Thompson break; 139402ac6454SAndrew Thompson } 139502ac6454SAndrew Thompson return error; 139602ac6454SAndrew Thompson } 139702ac6454SAndrew Thompson 139802ac6454SAndrew Thompson static void 139902ac6454SAndrew Thompson ural_set_testmode(struct ural_softc *sc) 140002ac6454SAndrew Thompson { 1401760bc48eSAndrew Thompson struct usb_device_request req; 1402e0a69b51SAndrew Thompson usb_error_t error; 140302ac6454SAndrew Thompson 140402ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 140502ac6454SAndrew Thompson req.bRequest = RAL_VENDOR_REQUEST; 140602ac6454SAndrew Thompson USETW(req.wValue, 4); 140702ac6454SAndrew Thompson USETW(req.wIndex, 1); 140802ac6454SAndrew Thompson USETW(req.wLength, 0); 140902ac6454SAndrew Thompson 141002ac6454SAndrew Thompson error = ural_do_request(sc, &req, NULL); 141102ac6454SAndrew Thompson if (error != 0) { 141202ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not set test mode: %s\n", 1413a593f6b8SAndrew Thompson usbd_errstr(error)); 141402ac6454SAndrew Thompson } 141502ac6454SAndrew Thompson } 141602ac6454SAndrew Thompson 141702ac6454SAndrew Thompson static void 141802ac6454SAndrew Thompson ural_eeprom_read(struct ural_softc *sc, uint16_t addr, void *buf, int len) 141902ac6454SAndrew Thompson { 1420760bc48eSAndrew Thompson struct usb_device_request req; 1421e0a69b51SAndrew Thompson usb_error_t error; 142202ac6454SAndrew Thompson 142302ac6454SAndrew Thompson req.bmRequestType = UT_READ_VENDOR_DEVICE; 142402ac6454SAndrew Thompson req.bRequest = RAL_READ_EEPROM; 142502ac6454SAndrew Thompson USETW(req.wValue, 0); 142602ac6454SAndrew Thompson USETW(req.wIndex, addr); 142702ac6454SAndrew Thompson USETW(req.wLength, len); 142802ac6454SAndrew Thompson 142902ac6454SAndrew Thompson error = ural_do_request(sc, &req, buf); 143002ac6454SAndrew Thompson if (error != 0) { 143102ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not read EEPROM: %s\n", 1432a593f6b8SAndrew Thompson usbd_errstr(error)); 143302ac6454SAndrew Thompson } 143402ac6454SAndrew Thompson } 143502ac6454SAndrew Thompson 143602ac6454SAndrew Thompson static uint16_t 143702ac6454SAndrew Thompson ural_read(struct ural_softc *sc, uint16_t reg) 143802ac6454SAndrew Thompson { 1439760bc48eSAndrew Thompson struct usb_device_request req; 1440e0a69b51SAndrew Thompson usb_error_t error; 144102ac6454SAndrew Thompson uint16_t val; 144202ac6454SAndrew Thompson 144302ac6454SAndrew Thompson req.bmRequestType = UT_READ_VENDOR_DEVICE; 144402ac6454SAndrew Thompson req.bRequest = RAL_READ_MAC; 144502ac6454SAndrew Thompson USETW(req.wValue, 0); 144602ac6454SAndrew Thompson USETW(req.wIndex, reg); 144702ac6454SAndrew Thompson USETW(req.wLength, sizeof (uint16_t)); 144802ac6454SAndrew Thompson 144902ac6454SAndrew Thompson error = ural_do_request(sc, &req, &val); 145002ac6454SAndrew Thompson if (error != 0) { 145102ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not read MAC register: %s\n", 1452a593f6b8SAndrew Thompson usbd_errstr(error)); 145302ac6454SAndrew Thompson return 0; 145402ac6454SAndrew Thompson } 145502ac6454SAndrew Thompson 145602ac6454SAndrew Thompson return le16toh(val); 145702ac6454SAndrew Thompson } 145802ac6454SAndrew Thompson 145902ac6454SAndrew Thompson static void 146002ac6454SAndrew Thompson ural_read_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len) 146102ac6454SAndrew Thompson { 1462760bc48eSAndrew Thompson struct usb_device_request req; 1463e0a69b51SAndrew Thompson usb_error_t error; 146402ac6454SAndrew Thompson 146502ac6454SAndrew Thompson req.bmRequestType = UT_READ_VENDOR_DEVICE; 146602ac6454SAndrew Thompson req.bRequest = RAL_READ_MULTI_MAC; 146702ac6454SAndrew Thompson USETW(req.wValue, 0); 146802ac6454SAndrew Thompson USETW(req.wIndex, reg); 146902ac6454SAndrew Thompson USETW(req.wLength, len); 147002ac6454SAndrew Thompson 147102ac6454SAndrew Thompson error = ural_do_request(sc, &req, buf); 147202ac6454SAndrew Thompson if (error != 0) { 147302ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not read MAC register: %s\n", 1474a593f6b8SAndrew Thompson usbd_errstr(error)); 147502ac6454SAndrew Thompson } 147602ac6454SAndrew Thompson } 147702ac6454SAndrew Thompson 147802ac6454SAndrew Thompson static void 147902ac6454SAndrew Thompson ural_write(struct ural_softc *sc, uint16_t reg, uint16_t val) 148002ac6454SAndrew Thompson { 1481760bc48eSAndrew Thompson struct usb_device_request req; 1482e0a69b51SAndrew Thompson usb_error_t error; 148302ac6454SAndrew Thompson 148402ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 148502ac6454SAndrew Thompson req.bRequest = RAL_WRITE_MAC; 148602ac6454SAndrew Thompson USETW(req.wValue, val); 148702ac6454SAndrew Thompson USETW(req.wIndex, reg); 148802ac6454SAndrew Thompson USETW(req.wLength, 0); 148902ac6454SAndrew Thompson 149002ac6454SAndrew Thompson error = ural_do_request(sc, &req, NULL); 149102ac6454SAndrew Thompson if (error != 0) { 149202ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not write MAC register: %s\n", 1493a593f6b8SAndrew Thompson usbd_errstr(error)); 149402ac6454SAndrew Thompson } 149502ac6454SAndrew Thompson } 149602ac6454SAndrew Thompson 149702ac6454SAndrew Thompson static void 149802ac6454SAndrew Thompson ural_write_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len) 149902ac6454SAndrew Thompson { 1500760bc48eSAndrew Thompson struct usb_device_request req; 1501e0a69b51SAndrew Thompson usb_error_t error; 150202ac6454SAndrew Thompson 150302ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 150402ac6454SAndrew Thompson req.bRequest = RAL_WRITE_MULTI_MAC; 150502ac6454SAndrew Thompson USETW(req.wValue, 0); 150602ac6454SAndrew Thompson USETW(req.wIndex, reg); 150702ac6454SAndrew Thompson USETW(req.wLength, len); 150802ac6454SAndrew Thompson 150902ac6454SAndrew Thompson error = ural_do_request(sc, &req, buf); 151002ac6454SAndrew Thompson if (error != 0) { 151102ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not write MAC register: %s\n", 1512a593f6b8SAndrew Thompson usbd_errstr(error)); 151302ac6454SAndrew Thompson } 151402ac6454SAndrew Thompson } 151502ac6454SAndrew Thompson 151602ac6454SAndrew Thompson static void 151702ac6454SAndrew Thompson ural_bbp_write(struct ural_softc *sc, uint8_t reg, uint8_t val) 151802ac6454SAndrew Thompson { 151902ac6454SAndrew Thompson uint16_t tmp; 152002ac6454SAndrew Thompson int ntries; 152102ac6454SAndrew Thompson 152202ac6454SAndrew Thompson for (ntries = 0; ntries < 100; ntries++) { 152302ac6454SAndrew Thompson if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY)) 152402ac6454SAndrew Thompson break; 152502ac6454SAndrew Thompson if (ural_pause(sc, hz / 100)) 152602ac6454SAndrew Thompson break; 152702ac6454SAndrew Thompson } 152802ac6454SAndrew Thompson if (ntries == 100) { 152902ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not write to BBP\n"); 153002ac6454SAndrew Thompson return; 153102ac6454SAndrew Thompson } 153202ac6454SAndrew Thompson 153302ac6454SAndrew Thompson tmp = reg << 8 | val; 153402ac6454SAndrew Thompson ural_write(sc, RAL_PHY_CSR7, tmp); 153502ac6454SAndrew Thompson } 153602ac6454SAndrew Thompson 153702ac6454SAndrew Thompson static uint8_t 153802ac6454SAndrew Thompson ural_bbp_read(struct ural_softc *sc, uint8_t reg) 153902ac6454SAndrew Thompson { 154002ac6454SAndrew Thompson uint16_t val; 154102ac6454SAndrew Thompson int ntries; 154202ac6454SAndrew Thompson 154302ac6454SAndrew Thompson val = RAL_BBP_WRITE | reg << 8; 154402ac6454SAndrew Thompson ural_write(sc, RAL_PHY_CSR7, val); 154502ac6454SAndrew Thompson 154602ac6454SAndrew Thompson for (ntries = 0; ntries < 100; ntries++) { 154702ac6454SAndrew Thompson if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY)) 154802ac6454SAndrew Thompson break; 154902ac6454SAndrew Thompson if (ural_pause(sc, hz / 100)) 155002ac6454SAndrew Thompson break; 155102ac6454SAndrew Thompson } 155202ac6454SAndrew Thompson if (ntries == 100) { 155302ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not read BBP\n"); 155402ac6454SAndrew Thompson return 0; 155502ac6454SAndrew Thompson } 155602ac6454SAndrew Thompson 155702ac6454SAndrew Thompson return ural_read(sc, RAL_PHY_CSR7) & 0xff; 155802ac6454SAndrew Thompson } 155902ac6454SAndrew Thompson 156002ac6454SAndrew Thompson static void 156102ac6454SAndrew Thompson ural_rf_write(struct ural_softc *sc, uint8_t reg, uint32_t val) 156202ac6454SAndrew Thompson { 156302ac6454SAndrew Thompson uint32_t tmp; 156402ac6454SAndrew Thompson int ntries; 156502ac6454SAndrew Thompson 156602ac6454SAndrew Thompson for (ntries = 0; ntries < 100; ntries++) { 156702ac6454SAndrew Thompson if (!(ural_read(sc, RAL_PHY_CSR10) & RAL_RF_LOBUSY)) 156802ac6454SAndrew Thompson break; 156902ac6454SAndrew Thompson if (ural_pause(sc, hz / 100)) 157002ac6454SAndrew Thompson break; 157102ac6454SAndrew Thompson } 157202ac6454SAndrew Thompson if (ntries == 100) { 157302ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not write to RF\n"); 157402ac6454SAndrew Thompson return; 157502ac6454SAndrew Thompson } 157602ac6454SAndrew Thompson 157702ac6454SAndrew Thompson tmp = RAL_RF_BUSY | RAL_RF_20BIT | (val & 0xfffff) << 2 | (reg & 0x3); 157802ac6454SAndrew Thompson ural_write(sc, RAL_PHY_CSR9, tmp & 0xffff); 157902ac6454SAndrew Thompson ural_write(sc, RAL_PHY_CSR10, tmp >> 16); 158002ac6454SAndrew Thompson 158102ac6454SAndrew Thompson /* remember last written value in sc */ 158202ac6454SAndrew Thompson sc->rf_regs[reg] = val; 158302ac6454SAndrew Thompson 158402ac6454SAndrew Thompson DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff); 158502ac6454SAndrew Thompson } 158602ac6454SAndrew Thompson 158702ac6454SAndrew Thompson /* ARGUSED */ 158802ac6454SAndrew Thompson static struct ieee80211_node * 158902ac6454SAndrew Thompson ural_node_alloc(struct ieee80211vap *vap __unused, 159002ac6454SAndrew Thompson const uint8_t mac[IEEE80211_ADDR_LEN] __unused) 159102ac6454SAndrew Thompson { 159202ac6454SAndrew Thompson struct ural_node *un; 159302ac6454SAndrew Thompson 159402ac6454SAndrew Thompson un = malloc(sizeof(struct ural_node), M_80211_NODE, M_NOWAIT | M_ZERO); 159502ac6454SAndrew Thompson return un != NULL ? &un->ni : NULL; 159602ac6454SAndrew Thompson } 159702ac6454SAndrew Thompson 159802ac6454SAndrew Thompson static void 159902ac6454SAndrew Thompson ural_newassoc(struct ieee80211_node *ni, int isnew) 160002ac6454SAndrew Thompson { 160102ac6454SAndrew Thompson struct ieee80211vap *vap = ni->ni_vap; 160202ac6454SAndrew Thompson 160302ac6454SAndrew Thompson ieee80211_amrr_node_init(&URAL_VAP(vap)->amrr, &URAL_NODE(ni)->amn, ni); 160402ac6454SAndrew Thompson } 160502ac6454SAndrew Thompson 160602ac6454SAndrew Thompson static void 160702ac6454SAndrew Thompson ural_scan_start(struct ieee80211com *ic) 160802ac6454SAndrew Thompson { 16095efea30fSAndrew Thompson struct ifnet *ifp = ic->ic_ifp; 16105efea30fSAndrew Thompson struct ural_softc *sc = ifp->if_softc; 161102ac6454SAndrew Thompson 161202ac6454SAndrew Thompson RAL_LOCK(sc); 16135efea30fSAndrew Thompson ural_write(sc, RAL_TXRX_CSR19, 0); 16145efea30fSAndrew Thompson ural_set_bssid(sc, ifp->if_broadcastaddr); 161502ac6454SAndrew Thompson RAL_UNLOCK(sc); 161602ac6454SAndrew Thompson } 161702ac6454SAndrew Thompson 161802ac6454SAndrew Thompson static void 161902ac6454SAndrew Thompson ural_scan_end(struct ieee80211com *ic) 162002ac6454SAndrew Thompson { 162102ac6454SAndrew Thompson struct ural_softc *sc = ic->ic_ifp->if_softc; 162202ac6454SAndrew Thompson 162302ac6454SAndrew Thompson RAL_LOCK(sc); 16245efea30fSAndrew Thompson ural_enable_tsf_sync(sc); 16255efea30fSAndrew Thompson ural_set_bssid(sc, sc->sc_bssid); 162602ac6454SAndrew Thompson RAL_UNLOCK(sc); 162702ac6454SAndrew Thompson 162802ac6454SAndrew Thompson } 162902ac6454SAndrew Thompson 163002ac6454SAndrew Thompson static void 163102ac6454SAndrew Thompson ural_set_channel(struct ieee80211com *ic) 163202ac6454SAndrew Thompson { 163302ac6454SAndrew Thompson struct ural_softc *sc = ic->ic_ifp->if_softc; 163402ac6454SAndrew Thompson 163502ac6454SAndrew Thompson RAL_LOCK(sc); 16365efea30fSAndrew Thompson ural_set_chan(sc, ic->ic_curchan); 163702ac6454SAndrew Thompson RAL_UNLOCK(sc); 163802ac6454SAndrew Thompson } 163902ac6454SAndrew Thompson 164002ac6454SAndrew Thompson static void 164102ac6454SAndrew Thompson ural_set_chan(struct ural_softc *sc, struct ieee80211_channel *c) 164202ac6454SAndrew Thompson { 164302ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 164402ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 164502ac6454SAndrew Thompson uint8_t power, tmp; 164602ac6454SAndrew Thompson int i, chan; 164702ac6454SAndrew Thompson 164802ac6454SAndrew Thompson chan = ieee80211_chan2ieee(ic, c); 164902ac6454SAndrew Thompson if (chan == 0 || chan == IEEE80211_CHAN_ANY) 165002ac6454SAndrew Thompson return; 165102ac6454SAndrew Thompson 165202ac6454SAndrew Thompson if (IEEE80211_IS_CHAN_2GHZ(c)) 165302ac6454SAndrew Thompson power = min(sc->txpow[chan - 1], 31); 165402ac6454SAndrew Thompson else 165502ac6454SAndrew Thompson power = 31; 165602ac6454SAndrew Thompson 165702ac6454SAndrew Thompson /* adjust txpower using ifconfig settings */ 165802ac6454SAndrew Thompson power -= (100 - ic->ic_txpowlimit) / 8; 165902ac6454SAndrew Thompson 166002ac6454SAndrew Thompson DPRINTFN(2, "setting channel to %u, txpower to %u\n", chan, power); 166102ac6454SAndrew Thompson 166202ac6454SAndrew Thompson switch (sc->rf_rev) { 166302ac6454SAndrew Thompson case RAL_RF_2522: 166402ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x00814); 166502ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2522_r2[chan - 1]); 166602ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 166702ac6454SAndrew Thompson break; 166802ac6454SAndrew Thompson 166902ac6454SAndrew Thompson case RAL_RF_2523: 167002ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x08804); 167102ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2523_r2[chan - 1]); 167202ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x38044); 167302ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 167402ac6454SAndrew Thompson break; 167502ac6454SAndrew Thompson 167602ac6454SAndrew Thompson case RAL_RF_2524: 167702ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x0c808); 167802ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2524_r2[chan - 1]); 167902ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 168002ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 168102ac6454SAndrew Thompson break; 168202ac6454SAndrew Thompson 168302ac6454SAndrew Thompson case RAL_RF_2525: 168402ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x08808); 168502ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2525_hi_r2[chan - 1]); 168602ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 168702ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 168802ac6454SAndrew Thompson 168902ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x08808); 169002ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2525_r2[chan - 1]); 169102ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 169202ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 169302ac6454SAndrew Thompson break; 169402ac6454SAndrew Thompson 169502ac6454SAndrew Thompson case RAL_RF_2525E: 169602ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x08808); 169702ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2525e_r2[chan - 1]); 169802ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 169902ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282); 170002ac6454SAndrew Thompson break; 170102ac6454SAndrew Thompson 170202ac6454SAndrew Thompson case RAL_RF_2526: 170302ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2526_hi_r2[chan - 1]); 170402ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 170502ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x08804); 170602ac6454SAndrew Thompson 170702ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2526_r2[chan - 1]); 170802ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 170902ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 171002ac6454SAndrew Thompson break; 171102ac6454SAndrew Thompson 171202ac6454SAndrew Thompson /* dual-band RF */ 171302ac6454SAndrew Thompson case RAL_RF_5222: 171402ac6454SAndrew Thompson for (i = 0; ural_rf5222[i].chan != chan; i++); 171502ac6454SAndrew Thompson 171602ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, ural_rf5222[i].r1); 171702ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf5222[i].r2); 171802ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 171902ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, ural_rf5222[i].r4); 172002ac6454SAndrew Thompson break; 172102ac6454SAndrew Thompson } 172202ac6454SAndrew Thompson 172302ac6454SAndrew Thompson if (ic->ic_opmode != IEEE80211_M_MONITOR && 172402ac6454SAndrew Thompson (ic->ic_flags & IEEE80211_F_SCAN) == 0) { 172502ac6454SAndrew Thompson /* set Japan filter bit for channel 14 */ 172602ac6454SAndrew Thompson tmp = ural_bbp_read(sc, 70); 172702ac6454SAndrew Thompson 172802ac6454SAndrew Thompson tmp &= ~RAL_JAPAN_FILTER; 172902ac6454SAndrew Thompson if (chan == 14) 173002ac6454SAndrew Thompson tmp |= RAL_JAPAN_FILTER; 173102ac6454SAndrew Thompson 173202ac6454SAndrew Thompson ural_bbp_write(sc, 70, tmp); 173302ac6454SAndrew Thompson 173402ac6454SAndrew Thompson /* clear CRC errors */ 173502ac6454SAndrew Thompson ural_read(sc, RAL_STA_CSR0); 173602ac6454SAndrew Thompson 173702ac6454SAndrew Thompson ural_pause(sc, hz / 100); 173802ac6454SAndrew Thompson ural_disable_rf_tune(sc); 173902ac6454SAndrew Thompson } 174002ac6454SAndrew Thompson 174102ac6454SAndrew Thompson /* XXX doesn't belong here */ 174202ac6454SAndrew Thompson /* update basic rate set */ 174302ac6454SAndrew Thompson ural_set_basicrates(sc, c); 174477ddf3d3SAndrew Thompson 174577ddf3d3SAndrew Thompson /* give the hardware some time to do the switchover */ 174677ddf3d3SAndrew Thompson ural_pause(sc, hz / 100); 174702ac6454SAndrew Thompson } 174802ac6454SAndrew Thompson 174902ac6454SAndrew Thompson /* 175002ac6454SAndrew Thompson * Disable RF auto-tuning. 175102ac6454SAndrew Thompson */ 175202ac6454SAndrew Thompson static void 175302ac6454SAndrew Thompson ural_disable_rf_tune(struct ural_softc *sc) 175402ac6454SAndrew Thompson { 175502ac6454SAndrew Thompson uint32_t tmp; 175602ac6454SAndrew Thompson 175702ac6454SAndrew Thompson if (sc->rf_rev != RAL_RF_2523) { 175802ac6454SAndrew Thompson tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE; 175902ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, tmp); 176002ac6454SAndrew Thompson } 176102ac6454SAndrew Thompson 176202ac6454SAndrew Thompson tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE; 176302ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, tmp); 176402ac6454SAndrew Thompson 176502ac6454SAndrew Thompson DPRINTFN(2, "disabling RF autotune\n"); 176602ac6454SAndrew Thompson } 176702ac6454SAndrew Thompson 176802ac6454SAndrew Thompson /* 176902ac6454SAndrew Thompson * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF 177002ac6454SAndrew Thompson * synchronization. 177102ac6454SAndrew Thompson */ 177202ac6454SAndrew Thompson static void 177302ac6454SAndrew Thompson ural_enable_tsf_sync(struct ural_softc *sc) 177402ac6454SAndrew Thompson { 177502ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 177602ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 177702ac6454SAndrew Thompson struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 177802ac6454SAndrew Thompson uint16_t logcwmin, preload, tmp; 177902ac6454SAndrew Thompson 178002ac6454SAndrew Thompson /* first, disable TSF synchronization */ 178102ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR19, 0); 178202ac6454SAndrew Thompson 178302ac6454SAndrew Thompson tmp = (16 * vap->iv_bss->ni_intval) << 4; 178402ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR18, tmp); 178502ac6454SAndrew Thompson 178602ac6454SAndrew Thompson logcwmin = (ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 0; 178702ac6454SAndrew Thompson preload = (ic->ic_opmode == IEEE80211_M_IBSS) ? 320 : 6; 178802ac6454SAndrew Thompson tmp = logcwmin << 12 | preload; 178902ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR20, tmp); 179002ac6454SAndrew Thompson 179102ac6454SAndrew Thompson /* finally, enable TSF synchronization */ 179202ac6454SAndrew Thompson tmp = RAL_ENABLE_TSF | RAL_ENABLE_TBCN; 179302ac6454SAndrew Thompson if (ic->ic_opmode == IEEE80211_M_STA) 179402ac6454SAndrew Thompson tmp |= RAL_ENABLE_TSF_SYNC(1); 179502ac6454SAndrew Thompson else 179602ac6454SAndrew Thompson tmp |= RAL_ENABLE_TSF_SYNC(2) | RAL_ENABLE_BEACON_GENERATOR; 179702ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR19, tmp); 179802ac6454SAndrew Thompson 179902ac6454SAndrew Thompson DPRINTF("enabling TSF synchronization\n"); 180002ac6454SAndrew Thompson } 180102ac6454SAndrew Thompson 18025463c4a4SSam Leffler static void 18035463c4a4SSam Leffler ural_enable_tsf(struct ural_softc *sc) 18045463c4a4SSam Leffler { 18055463c4a4SSam Leffler /* first, disable TSF synchronization */ 18065463c4a4SSam Leffler ural_write(sc, RAL_TXRX_CSR19, 0); 18075463c4a4SSam Leffler ural_write(sc, RAL_TXRX_CSR19, RAL_ENABLE_TSF | RAL_ENABLE_TSF_SYNC(2)); 18085463c4a4SSam Leffler } 18095463c4a4SSam Leffler 181002ac6454SAndrew Thompson #define RAL_RXTX_TURNAROUND 5 /* us */ 181102ac6454SAndrew Thompson static void 181202ac6454SAndrew Thompson ural_update_slot(struct ifnet *ifp) 181302ac6454SAndrew Thompson { 181402ac6454SAndrew Thompson struct ural_softc *sc = ifp->if_softc; 181502ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 181602ac6454SAndrew Thompson uint16_t slottime, sifs, eifs; 181702ac6454SAndrew Thompson 181802ac6454SAndrew Thompson slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20; 181902ac6454SAndrew Thompson 182002ac6454SAndrew Thompson /* 182102ac6454SAndrew Thompson * These settings may sound a bit inconsistent but this is what the 182202ac6454SAndrew Thompson * reference driver does. 182302ac6454SAndrew Thompson */ 182402ac6454SAndrew Thompson if (ic->ic_curmode == IEEE80211_MODE_11B) { 182502ac6454SAndrew Thompson sifs = 16 - RAL_RXTX_TURNAROUND; 182602ac6454SAndrew Thompson eifs = 364; 182702ac6454SAndrew Thompson } else { 182802ac6454SAndrew Thompson sifs = 10 - RAL_RXTX_TURNAROUND; 182902ac6454SAndrew Thompson eifs = 64; 183002ac6454SAndrew Thompson } 183102ac6454SAndrew Thompson 183202ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR10, slottime); 183302ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR11, sifs); 183402ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR12, eifs); 183502ac6454SAndrew Thompson } 183602ac6454SAndrew Thompson 183702ac6454SAndrew Thompson static void 183802ac6454SAndrew Thompson ural_set_txpreamble(struct ural_softc *sc) 183902ac6454SAndrew Thompson { 184002ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 184102ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 184202ac6454SAndrew Thompson uint16_t tmp; 184302ac6454SAndrew Thompson 184402ac6454SAndrew Thompson tmp = ural_read(sc, RAL_TXRX_CSR10); 184502ac6454SAndrew Thompson 184602ac6454SAndrew Thompson tmp &= ~RAL_SHORT_PREAMBLE; 184702ac6454SAndrew Thompson if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 184802ac6454SAndrew Thompson tmp |= RAL_SHORT_PREAMBLE; 184902ac6454SAndrew Thompson 185002ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR10, tmp); 185102ac6454SAndrew Thompson } 185202ac6454SAndrew Thompson 185302ac6454SAndrew Thompson static void 185402ac6454SAndrew Thompson ural_set_basicrates(struct ural_softc *sc, const struct ieee80211_channel *c) 185502ac6454SAndrew Thompson { 185602ac6454SAndrew Thompson /* XXX wrong, take from rate set */ 185702ac6454SAndrew Thompson /* update basic rate set */ 185802ac6454SAndrew Thompson if (IEEE80211_IS_CHAN_5GHZ(c)) { 185902ac6454SAndrew Thompson /* 11a basic rates: 6, 12, 24Mbps */ 186002ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR11, 0x150); 186102ac6454SAndrew Thompson } else if (IEEE80211_IS_CHAN_ANYG(c)) { 186202ac6454SAndrew Thompson /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */ 186302ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR11, 0x15f); 186402ac6454SAndrew Thompson } else { 186502ac6454SAndrew Thompson /* 11b basic rates: 1, 2Mbps */ 186602ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR11, 0x3); 186702ac6454SAndrew Thompson } 186802ac6454SAndrew Thompson } 186902ac6454SAndrew Thompson 187002ac6454SAndrew Thompson static void 187102ac6454SAndrew Thompson ural_set_bssid(struct ural_softc *sc, const uint8_t *bssid) 187202ac6454SAndrew Thompson { 187302ac6454SAndrew Thompson uint16_t tmp; 187402ac6454SAndrew Thompson 187502ac6454SAndrew Thompson tmp = bssid[0] | bssid[1] << 8; 187602ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR5, tmp); 187702ac6454SAndrew Thompson 187802ac6454SAndrew Thompson tmp = bssid[2] | bssid[3] << 8; 187902ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR6, tmp); 188002ac6454SAndrew Thompson 188102ac6454SAndrew Thompson tmp = bssid[4] | bssid[5] << 8; 188202ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR7, tmp); 188302ac6454SAndrew Thompson 188402ac6454SAndrew Thompson DPRINTF("setting BSSID to %6D\n", bssid, ":"); 188502ac6454SAndrew Thompson } 188602ac6454SAndrew Thompson 188702ac6454SAndrew Thompson static void 188802ac6454SAndrew Thompson ural_set_macaddr(struct ural_softc *sc, uint8_t *addr) 188902ac6454SAndrew Thompson { 189002ac6454SAndrew Thompson uint16_t tmp; 189102ac6454SAndrew Thompson 189202ac6454SAndrew Thompson tmp = addr[0] | addr[1] << 8; 189302ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR2, tmp); 189402ac6454SAndrew Thompson 189502ac6454SAndrew Thompson tmp = addr[2] | addr[3] << 8; 189602ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR3, tmp); 189702ac6454SAndrew Thompson 189802ac6454SAndrew Thompson tmp = addr[4] | addr[5] << 8; 189902ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR4, tmp); 190002ac6454SAndrew Thompson 190102ac6454SAndrew Thompson DPRINTF("setting MAC address to %6D\n", addr, ":"); 190202ac6454SAndrew Thompson } 190302ac6454SAndrew Thompson 190402ac6454SAndrew Thompson static void 19055efea30fSAndrew Thompson ural_setpromisc(struct ural_softc *sc) 190602ac6454SAndrew Thompson { 190702ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 190802ac6454SAndrew Thompson uint32_t tmp; 190902ac6454SAndrew Thompson 191002ac6454SAndrew Thompson tmp = ural_read(sc, RAL_TXRX_CSR2); 191102ac6454SAndrew Thompson 191202ac6454SAndrew Thompson tmp &= ~RAL_DROP_NOT_TO_ME; 191302ac6454SAndrew Thompson if (!(ifp->if_flags & IFF_PROMISC)) 191402ac6454SAndrew Thompson tmp |= RAL_DROP_NOT_TO_ME; 191502ac6454SAndrew Thompson 191602ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR2, tmp); 191702ac6454SAndrew Thompson 191802ac6454SAndrew Thompson DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ? 191902ac6454SAndrew Thompson "entering" : "leaving"); 192002ac6454SAndrew Thompson } 192102ac6454SAndrew Thompson 192277ddf3d3SAndrew Thompson static void 192377ddf3d3SAndrew Thompson ural_update_promisc(struct ifnet *ifp) 192477ddf3d3SAndrew Thompson { 192577ddf3d3SAndrew Thompson struct ural_softc *sc = ifp->if_softc; 192677ddf3d3SAndrew Thompson 192777ddf3d3SAndrew Thompson if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 192877ddf3d3SAndrew Thompson return; 192977ddf3d3SAndrew Thompson 193077ddf3d3SAndrew Thompson RAL_LOCK(sc); 19315efea30fSAndrew Thompson ural_setpromisc(sc); 193277ddf3d3SAndrew Thompson RAL_UNLOCK(sc); 193377ddf3d3SAndrew Thompson } 193477ddf3d3SAndrew Thompson 193502ac6454SAndrew Thompson static const char * 193602ac6454SAndrew Thompson ural_get_rf(int rev) 193702ac6454SAndrew Thompson { 193802ac6454SAndrew Thompson switch (rev) { 193902ac6454SAndrew Thompson case RAL_RF_2522: return "RT2522"; 194002ac6454SAndrew Thompson case RAL_RF_2523: return "RT2523"; 194102ac6454SAndrew Thompson case RAL_RF_2524: return "RT2524"; 194202ac6454SAndrew Thompson case RAL_RF_2525: return "RT2525"; 194302ac6454SAndrew Thompson case RAL_RF_2525E: return "RT2525e"; 194402ac6454SAndrew Thompson case RAL_RF_2526: return "RT2526"; 194502ac6454SAndrew Thompson case RAL_RF_5222: return "RT5222"; 194602ac6454SAndrew Thompson default: return "unknown"; 194702ac6454SAndrew Thompson } 194802ac6454SAndrew Thompson } 194902ac6454SAndrew Thompson 195002ac6454SAndrew Thompson static void 195102ac6454SAndrew Thompson ural_read_eeprom(struct ural_softc *sc) 195202ac6454SAndrew Thompson { 195302ac6454SAndrew Thompson uint16_t val; 195402ac6454SAndrew Thompson 195502ac6454SAndrew Thompson ural_eeprom_read(sc, RAL_EEPROM_CONFIG0, &val, 2); 195602ac6454SAndrew Thompson val = le16toh(val); 195702ac6454SAndrew Thompson sc->rf_rev = (val >> 11) & 0x7; 195802ac6454SAndrew Thompson sc->hw_radio = (val >> 10) & 0x1; 195902ac6454SAndrew Thompson sc->led_mode = (val >> 6) & 0x7; 196002ac6454SAndrew Thompson sc->rx_ant = (val >> 4) & 0x3; 196102ac6454SAndrew Thompson sc->tx_ant = (val >> 2) & 0x3; 196202ac6454SAndrew Thompson sc->nb_ant = val & 0x3; 196302ac6454SAndrew Thompson 196402ac6454SAndrew Thompson /* read MAC address */ 196502ac6454SAndrew Thompson ural_eeprom_read(sc, RAL_EEPROM_ADDRESS, sc->sc_bssid, 6); 196602ac6454SAndrew Thompson 196702ac6454SAndrew Thompson /* read default values for BBP registers */ 196802ac6454SAndrew Thompson ural_eeprom_read(sc, RAL_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16); 196902ac6454SAndrew Thompson 197002ac6454SAndrew Thompson /* read Tx power for all b/g channels */ 197102ac6454SAndrew Thompson ural_eeprom_read(sc, RAL_EEPROM_TXPOWER, sc->txpow, 14); 197202ac6454SAndrew Thompson } 197302ac6454SAndrew Thompson 197402ac6454SAndrew Thompson static int 197502ac6454SAndrew Thompson ural_bbp_init(struct ural_softc *sc) 197602ac6454SAndrew Thompson { 197702ac6454SAndrew Thompson #define N(a) (sizeof (a) / sizeof ((a)[0])) 197802ac6454SAndrew Thompson int i, ntries; 197902ac6454SAndrew Thompson 198002ac6454SAndrew Thompson /* wait for BBP to be ready */ 198102ac6454SAndrew Thompson for (ntries = 0; ntries < 100; ntries++) { 198202ac6454SAndrew Thompson if (ural_bbp_read(sc, RAL_BBP_VERSION) != 0) 198302ac6454SAndrew Thompson break; 198402ac6454SAndrew Thompson if (ural_pause(sc, hz / 100)) 198502ac6454SAndrew Thompson break; 198602ac6454SAndrew Thompson } 198702ac6454SAndrew Thompson if (ntries == 100) { 198802ac6454SAndrew Thompson device_printf(sc->sc_dev, "timeout waiting for BBP\n"); 198902ac6454SAndrew Thompson return EIO; 199002ac6454SAndrew Thompson } 199102ac6454SAndrew Thompson 199202ac6454SAndrew Thompson /* initialize BBP registers to default values */ 199302ac6454SAndrew Thompson for (i = 0; i < N(ural_def_bbp); i++) 199402ac6454SAndrew Thompson ural_bbp_write(sc, ural_def_bbp[i].reg, ural_def_bbp[i].val); 199502ac6454SAndrew Thompson 199602ac6454SAndrew Thompson #if 0 199702ac6454SAndrew Thompson /* initialize BBP registers to values stored in EEPROM */ 199802ac6454SAndrew Thompson for (i = 0; i < 16; i++) { 199902ac6454SAndrew Thompson if (sc->bbp_prom[i].reg == 0xff) 200002ac6454SAndrew Thompson continue; 200102ac6454SAndrew Thompson ural_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 200202ac6454SAndrew Thompson } 200302ac6454SAndrew Thompson #endif 200402ac6454SAndrew Thompson 200502ac6454SAndrew Thompson return 0; 200602ac6454SAndrew Thompson #undef N 200702ac6454SAndrew Thompson } 200802ac6454SAndrew Thompson 200902ac6454SAndrew Thompson static void 201002ac6454SAndrew Thompson ural_set_txantenna(struct ural_softc *sc, int antenna) 201102ac6454SAndrew Thompson { 201202ac6454SAndrew Thompson uint16_t tmp; 201302ac6454SAndrew Thompson uint8_t tx; 201402ac6454SAndrew Thompson 201502ac6454SAndrew Thompson tx = ural_bbp_read(sc, RAL_BBP_TX) & ~RAL_BBP_ANTMASK; 201602ac6454SAndrew Thompson if (antenna == 1) 201702ac6454SAndrew Thompson tx |= RAL_BBP_ANTA; 201802ac6454SAndrew Thompson else if (antenna == 2) 201902ac6454SAndrew Thompson tx |= RAL_BBP_ANTB; 202002ac6454SAndrew Thompson else 202102ac6454SAndrew Thompson tx |= RAL_BBP_DIVERSITY; 202202ac6454SAndrew Thompson 202302ac6454SAndrew Thompson /* need to force I/Q flip for RF 2525e, 2526 and 5222 */ 202402ac6454SAndrew Thompson if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526 || 202502ac6454SAndrew Thompson sc->rf_rev == RAL_RF_5222) 202602ac6454SAndrew Thompson tx |= RAL_BBP_FLIPIQ; 202702ac6454SAndrew Thompson 202802ac6454SAndrew Thompson ural_bbp_write(sc, RAL_BBP_TX, tx); 202902ac6454SAndrew Thompson 203002ac6454SAndrew Thompson /* update values in PHY_CSR5 and PHY_CSR6 */ 203102ac6454SAndrew Thompson tmp = ural_read(sc, RAL_PHY_CSR5) & ~0x7; 203202ac6454SAndrew Thompson ural_write(sc, RAL_PHY_CSR5, tmp | (tx & 0x7)); 203302ac6454SAndrew Thompson 203402ac6454SAndrew Thompson tmp = ural_read(sc, RAL_PHY_CSR6) & ~0x7; 203502ac6454SAndrew Thompson ural_write(sc, RAL_PHY_CSR6, tmp | (tx & 0x7)); 203602ac6454SAndrew Thompson } 203702ac6454SAndrew Thompson 203802ac6454SAndrew Thompson static void 203902ac6454SAndrew Thompson ural_set_rxantenna(struct ural_softc *sc, int antenna) 204002ac6454SAndrew Thompson { 204102ac6454SAndrew Thompson uint8_t rx; 204202ac6454SAndrew Thompson 204302ac6454SAndrew Thompson rx = ural_bbp_read(sc, RAL_BBP_RX) & ~RAL_BBP_ANTMASK; 204402ac6454SAndrew Thompson if (antenna == 1) 204502ac6454SAndrew Thompson rx |= RAL_BBP_ANTA; 204602ac6454SAndrew Thompson else if (antenna == 2) 204702ac6454SAndrew Thompson rx |= RAL_BBP_ANTB; 204802ac6454SAndrew Thompson else 204902ac6454SAndrew Thompson rx |= RAL_BBP_DIVERSITY; 205002ac6454SAndrew Thompson 205102ac6454SAndrew Thompson /* need to force no I/Q flip for RF 2525e and 2526 */ 205202ac6454SAndrew Thompson if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526) 205302ac6454SAndrew Thompson rx &= ~RAL_BBP_FLIPIQ; 205402ac6454SAndrew Thompson 205502ac6454SAndrew Thompson ural_bbp_write(sc, RAL_BBP_RX, rx); 205602ac6454SAndrew Thompson } 205702ac6454SAndrew Thompson 205802ac6454SAndrew Thompson static void 20595efea30fSAndrew Thompson ural_init_locked(struct ural_softc *sc) 206002ac6454SAndrew Thompson { 206102ac6454SAndrew Thompson #define N(a) (sizeof (a) / sizeof ((a)[0])) 206202ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 206302ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 206402ac6454SAndrew Thompson uint16_t tmp; 206502ac6454SAndrew Thompson int i, ntries; 206602ac6454SAndrew Thompson 206702ac6454SAndrew Thompson RAL_LOCK_ASSERT(sc, MA_OWNED); 206802ac6454SAndrew Thompson 206902ac6454SAndrew Thompson ural_set_testmode(sc); 207002ac6454SAndrew Thompson ural_write(sc, 0x308, 0x00f0); /* XXX magic */ 207102ac6454SAndrew Thompson 20725efea30fSAndrew Thompson ural_stop(sc); 207302ac6454SAndrew Thompson 207402ac6454SAndrew Thompson /* initialize MAC registers to default values */ 207502ac6454SAndrew Thompson for (i = 0; i < N(ural_def_mac); i++) 207602ac6454SAndrew Thompson ural_write(sc, ural_def_mac[i].reg, ural_def_mac[i].val); 207702ac6454SAndrew Thompson 207802ac6454SAndrew Thompson /* wait for BBP and RF to wake up (this can take a long time!) */ 207902ac6454SAndrew Thompson for (ntries = 0; ntries < 100; ntries++) { 208002ac6454SAndrew Thompson tmp = ural_read(sc, RAL_MAC_CSR17); 208102ac6454SAndrew Thompson if ((tmp & (RAL_BBP_AWAKE | RAL_RF_AWAKE)) == 208202ac6454SAndrew Thompson (RAL_BBP_AWAKE | RAL_RF_AWAKE)) 208302ac6454SAndrew Thompson break; 208402ac6454SAndrew Thompson if (ural_pause(sc, hz / 100)) 208502ac6454SAndrew Thompson break; 208602ac6454SAndrew Thompson } 208702ac6454SAndrew Thompson if (ntries == 100) { 208802ac6454SAndrew Thompson device_printf(sc->sc_dev, 208902ac6454SAndrew Thompson "timeout waiting for BBP/RF to wakeup\n"); 209002ac6454SAndrew Thompson goto fail; 209102ac6454SAndrew Thompson } 209202ac6454SAndrew Thompson 209302ac6454SAndrew Thompson /* we're ready! */ 209402ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR1, RAL_HOST_READY); 209502ac6454SAndrew Thompson 209602ac6454SAndrew Thompson /* set basic rate set (will be updated later) */ 209702ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR11, 0x15f); 209802ac6454SAndrew Thompson 209902ac6454SAndrew Thompson if (ural_bbp_init(sc) != 0) 210002ac6454SAndrew Thompson goto fail; 210102ac6454SAndrew Thompson 210202ac6454SAndrew Thompson ural_set_chan(sc, ic->ic_curchan); 210302ac6454SAndrew Thompson 210402ac6454SAndrew Thompson /* clear statistic registers (STA_CSR0 to STA_CSR10) */ 210502ac6454SAndrew Thompson ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta); 210602ac6454SAndrew Thompson 210702ac6454SAndrew Thompson ural_set_txantenna(sc, sc->tx_ant); 210802ac6454SAndrew Thompson ural_set_rxantenna(sc, sc->rx_ant); 210902ac6454SAndrew Thompson 211029aca940SSam Leffler ural_set_macaddr(sc, IF_LLADDR(ifp)); 211102ac6454SAndrew Thompson 211202ac6454SAndrew Thompson /* 211302ac6454SAndrew Thompson * Allocate Tx and Rx xfer queues. 211402ac6454SAndrew Thompson */ 211502ac6454SAndrew Thompson ural_setup_tx_list(sc); 211602ac6454SAndrew Thompson 211702ac6454SAndrew Thompson /* kick Rx */ 211802ac6454SAndrew Thompson tmp = RAL_DROP_PHY | RAL_DROP_CRC; 211902ac6454SAndrew Thompson if (ic->ic_opmode != IEEE80211_M_MONITOR) { 212002ac6454SAndrew Thompson tmp |= RAL_DROP_CTL | RAL_DROP_BAD_VERSION; 212102ac6454SAndrew Thompson if (ic->ic_opmode != IEEE80211_M_HOSTAP) 212202ac6454SAndrew Thompson tmp |= RAL_DROP_TODS; 212302ac6454SAndrew Thompson if (!(ifp->if_flags & IFF_PROMISC)) 212402ac6454SAndrew Thompson tmp |= RAL_DROP_NOT_TO_ME; 212502ac6454SAndrew Thompson } 212602ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR2, tmp); 212702ac6454SAndrew Thompson 212802ac6454SAndrew Thompson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 212902ac6454SAndrew Thompson ifp->if_drv_flags |= IFF_DRV_RUNNING; 2130ed6d949aSAndrew Thompson usbd_xfer_set_stall(sc->sc_xfer[URAL_BULK_WR]); 2131a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[URAL_BULK_RD]); 213202ac6454SAndrew Thompson return; 213302ac6454SAndrew Thompson 21345efea30fSAndrew Thompson fail: ural_stop(sc); 213502ac6454SAndrew Thompson #undef N 213602ac6454SAndrew Thompson } 213702ac6454SAndrew Thompson 213802ac6454SAndrew Thompson static void 213902ac6454SAndrew Thompson ural_init(void *priv) 214002ac6454SAndrew Thompson { 214102ac6454SAndrew Thompson struct ural_softc *sc = priv; 214202ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 214302ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 214402ac6454SAndrew Thompson 214502ac6454SAndrew Thompson RAL_LOCK(sc); 21465efea30fSAndrew Thompson ural_init_locked(sc); 214702ac6454SAndrew Thompson RAL_UNLOCK(sc); 214802ac6454SAndrew Thompson 214902ac6454SAndrew Thompson if (ifp->if_drv_flags & IFF_DRV_RUNNING) 215002ac6454SAndrew Thompson ieee80211_start_all(ic); /* start all vap's */ 215102ac6454SAndrew Thompson } 215202ac6454SAndrew Thompson 215302ac6454SAndrew Thompson static void 21545efea30fSAndrew Thompson ural_stop(struct ural_softc *sc) 215502ac6454SAndrew Thompson { 215602ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 215702ac6454SAndrew Thompson 215802ac6454SAndrew Thompson RAL_LOCK_ASSERT(sc, MA_OWNED); 215902ac6454SAndrew Thompson 216002ac6454SAndrew Thompson ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 216102ac6454SAndrew Thompson 216202ac6454SAndrew Thompson /* 216302ac6454SAndrew Thompson * Drain all the transfers, if not already drained: 216402ac6454SAndrew Thompson */ 216502ac6454SAndrew Thompson RAL_UNLOCK(sc); 2166a593f6b8SAndrew Thompson usbd_transfer_drain(sc->sc_xfer[URAL_BULK_WR]); 2167a593f6b8SAndrew Thompson usbd_transfer_drain(sc->sc_xfer[URAL_BULK_RD]); 216802ac6454SAndrew Thompson RAL_LOCK(sc); 216902ac6454SAndrew Thompson 217002ac6454SAndrew Thompson ural_unsetup_tx_list(sc); 217102ac6454SAndrew Thompson 217202ac6454SAndrew Thompson /* disable Rx */ 217302ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR2, RAL_DISABLE_RX); 217402ac6454SAndrew Thompson /* reset ASIC and BBP (but won't reset MAC registers!) */ 217502ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR1, RAL_RESET_ASIC | RAL_RESET_BBP); 217602ac6454SAndrew Thompson /* wait a little */ 217702ac6454SAndrew Thompson ural_pause(sc, hz / 10); 217802ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR1, 0); 217977ddf3d3SAndrew Thompson /* wait a little */ 218077ddf3d3SAndrew Thompson ural_pause(sc, hz / 10); 218102ac6454SAndrew Thompson } 218202ac6454SAndrew Thompson 218302ac6454SAndrew Thompson static int 218402ac6454SAndrew Thompson ural_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 218502ac6454SAndrew Thompson const struct ieee80211_bpf_params *params) 218602ac6454SAndrew Thompson { 218702ac6454SAndrew Thompson struct ieee80211com *ic = ni->ni_ic; 218802ac6454SAndrew Thompson struct ifnet *ifp = ic->ic_ifp; 218902ac6454SAndrew Thompson struct ural_softc *sc = ifp->if_softc; 219002ac6454SAndrew Thompson 219102ac6454SAndrew Thompson RAL_LOCK(sc); 219202ac6454SAndrew Thompson /* prevent management frames from being sent if we're not ready */ 219302ac6454SAndrew Thompson if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 219402ac6454SAndrew Thompson RAL_UNLOCK(sc); 219502ac6454SAndrew Thompson m_freem(m); 219602ac6454SAndrew Thompson ieee80211_free_node(ni); 219702ac6454SAndrew Thompson return ENETDOWN; 219802ac6454SAndrew Thompson } 219996ca458fSAndrew Thompson if (sc->tx_nfree < RAL_TX_MINFREE) { 220002ac6454SAndrew Thompson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 220102ac6454SAndrew Thompson RAL_UNLOCK(sc); 220202ac6454SAndrew Thompson m_freem(m); 220302ac6454SAndrew Thompson ieee80211_free_node(ni); 220402ac6454SAndrew Thompson return EIO; 220502ac6454SAndrew Thompson } 220602ac6454SAndrew Thompson 220702ac6454SAndrew Thompson ifp->if_opackets++; 220802ac6454SAndrew Thompson 220902ac6454SAndrew Thompson if (params == NULL) { 221002ac6454SAndrew Thompson /* 221102ac6454SAndrew Thompson * Legacy path; interpret frame contents to decide 221202ac6454SAndrew Thompson * precisely how to send the frame. 221302ac6454SAndrew Thompson */ 221402ac6454SAndrew Thompson if (ural_tx_mgt(sc, m, ni) != 0) 221502ac6454SAndrew Thompson goto bad; 221602ac6454SAndrew Thompson } else { 221702ac6454SAndrew Thompson /* 221802ac6454SAndrew Thompson * Caller supplied explicit parameters to use in 221902ac6454SAndrew Thompson * sending the frame. 222002ac6454SAndrew Thompson */ 222102ac6454SAndrew Thompson if (ural_tx_raw(sc, m, ni, params) != 0) 222202ac6454SAndrew Thompson goto bad; 222302ac6454SAndrew Thompson } 222402ac6454SAndrew Thompson RAL_UNLOCK(sc); 222502ac6454SAndrew Thompson return 0; 222602ac6454SAndrew Thompson bad: 222702ac6454SAndrew Thompson ifp->if_oerrors++; 222802ac6454SAndrew Thompson RAL_UNLOCK(sc); 222902ac6454SAndrew Thompson ieee80211_free_node(ni); 223002ac6454SAndrew Thompson return EIO; /* XXX */ 223102ac6454SAndrew Thompson } 223202ac6454SAndrew Thompson 223302ac6454SAndrew Thompson static void 223402ac6454SAndrew Thompson ural_amrr_start(struct ural_softc *sc, struct ieee80211_node *ni) 223502ac6454SAndrew Thompson { 223602ac6454SAndrew Thompson struct ieee80211vap *vap = ni->ni_vap; 223702ac6454SAndrew Thompson struct ural_vap *uvp = URAL_VAP(vap); 223802ac6454SAndrew Thompson 223902ac6454SAndrew Thompson /* clear statistic registers (STA_CSR0 to STA_CSR10) */ 224002ac6454SAndrew Thompson ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta); 224102ac6454SAndrew Thompson 224202ac6454SAndrew Thompson ieee80211_amrr_node_init(&uvp->amrr, &URAL_NODE(ni)->amn, ni); 224302ac6454SAndrew Thompson 2244a593f6b8SAndrew Thompson usb_callout_reset(&uvp->amrr_ch, hz, ural_amrr_timeout, uvp); 224502ac6454SAndrew Thompson } 224602ac6454SAndrew Thompson 224702ac6454SAndrew Thompson static void 224802ac6454SAndrew Thompson ural_amrr_timeout(void *arg) 224902ac6454SAndrew Thompson { 225002ac6454SAndrew Thompson struct ural_vap *uvp = arg; 22515efea30fSAndrew Thompson struct ieee80211vap *vap = &uvp->vap; 22525efea30fSAndrew Thompson struct ieee80211com *ic = vap->iv_ic; 225302ac6454SAndrew Thompson 22545efea30fSAndrew Thompson ieee80211_runtask(ic, &uvp->amrr_task); 225502ac6454SAndrew Thompson } 225602ac6454SAndrew Thompson 225702ac6454SAndrew Thompson static void 22585efea30fSAndrew Thompson ural_amrr_task(void *arg, int pending) 225902ac6454SAndrew Thompson { 22605efea30fSAndrew Thompson struct ural_vap *uvp = arg; 22615efea30fSAndrew Thompson struct ieee80211vap *vap = &uvp->vap; 22625efea30fSAndrew Thompson struct ieee80211com *ic = vap->iv_ic; 22635efea30fSAndrew Thompson struct ifnet *ifp = ic->ic_ifp; 22645efea30fSAndrew Thompson struct ural_softc *sc = ifp->if_softc; 226502ac6454SAndrew Thompson struct ieee80211_node *ni = vap->iv_bss; 226602ac6454SAndrew Thompson int ok, fail; 226702ac6454SAndrew Thompson 22685efea30fSAndrew Thompson RAL_LOCK(sc); 226902ac6454SAndrew Thompson /* read and clear statistic registers (STA_CSR0 to STA_CSR10) */ 227002ac6454SAndrew Thompson ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta)); 227102ac6454SAndrew Thompson 227202ac6454SAndrew Thompson ok = sc->sta[7] + /* TX ok w/o retry */ 227302ac6454SAndrew Thompson sc->sta[8]; /* TX ok w/ retry */ 227402ac6454SAndrew Thompson fail = sc->sta[9]; /* TX retry-fail count */ 227502ac6454SAndrew Thompson 227602ac6454SAndrew Thompson ieee80211_amrr_tx_update(&URAL_NODE(ni)->amn, 227702ac6454SAndrew Thompson ok+fail, ok, sc->sta[8] + fail); 227802ac6454SAndrew Thompson (void) ieee80211_amrr_choose(ni, &URAL_NODE(ni)->amn); 227902ac6454SAndrew Thompson 228002ac6454SAndrew Thompson ifp->if_oerrors += fail; /* count TX retry-fail as Tx errors */ 228102ac6454SAndrew Thompson 2282a593f6b8SAndrew Thompson usb_callout_reset(&uvp->amrr_ch, hz, ural_amrr_timeout, uvp); 22835efea30fSAndrew Thompson RAL_UNLOCK(sc); 228402ac6454SAndrew Thompson } 228502ac6454SAndrew Thompson 228602ac6454SAndrew Thompson static int 228702ac6454SAndrew Thompson ural_pause(struct ural_softc *sc, int timeout) 228802ac6454SAndrew Thompson { 228902ac6454SAndrew Thompson 2290a593f6b8SAndrew Thompson usb_pause_mtx(&sc->sc_mtx, timeout); 229102ac6454SAndrew Thompson return (0); 229202ac6454SAndrew Thompson } 2293