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 3102ac6454SAndrew Thompson #include "usbdevs.h" 3202ac6454SAndrew Thompson #include <dev/usb/usb.h> 3302ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h> 3402ac6454SAndrew Thompson #include <dev/usb/usb_error.h> 3502ac6454SAndrew Thompson 3602ac6454SAndrew Thompson #define USB_DEBUG_VAR ural_debug 3702ac6454SAndrew Thompson 3802ac6454SAndrew Thompson #include <dev/usb/usb_core.h> 3902ac6454SAndrew Thompson #include <dev/usb/usb_lookup.h> 4002ac6454SAndrew Thompson #include <dev/usb/usb_process.h> 4102ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 4202ac6454SAndrew Thompson #include <dev/usb/usb_request.h> 4302ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h> 4402ac6454SAndrew Thompson #include <dev/usb/usb_util.h> 4502ac6454SAndrew Thompson 4602ac6454SAndrew Thompson #include <dev/usb/wlan/usb_wlan.h> 4702ac6454SAndrew Thompson #include <dev/usb/wlan/if_uralreg.h> 4802ac6454SAndrew Thompson #include <dev/usb/wlan/if_uralvar.h> 4902ac6454SAndrew Thompson 5002ac6454SAndrew Thompson #if USB_DEBUG 5102ac6454SAndrew Thompson static int ural_debug = 0; 5202ac6454SAndrew Thompson 5302ac6454SAndrew Thompson SYSCTL_NODE(_hw_usb2, OID_AUTO, ural, CTLFLAG_RW, 0, "USB ural"); 5402ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2_ural, OID_AUTO, debug, CTLFLAG_RW, &ural_debug, 0, 5502ac6454SAndrew Thompson "Debug level"); 5602ac6454SAndrew Thompson #endif 5702ac6454SAndrew Thompson 5802ac6454SAndrew Thompson #define URAL_RSSI(rssi) \ 5902ac6454SAndrew Thompson ((rssi) > (RAL_NOISE_FLOOR + RAL_RSSI_CORR) ? \ 6002ac6454SAndrew Thompson ((rssi) - (RAL_NOISE_FLOOR + RAL_RSSI_CORR)) : 0) 6102ac6454SAndrew Thompson 6202ac6454SAndrew Thompson /* various supported device vendors/products */ 6302ac6454SAndrew Thompson static const struct usb2_device_id ural_devs[] = { 6402ac6454SAndrew Thompson { USB_VP(USB_VENDOR_ASUS, USB_PRODUCT_ASUS_WL167G) }, 6502ac6454SAndrew Thompson { USB_VP(USB_VENDOR_ASUS, USB_PRODUCT_RALINK_RT2570) }, 6602ac6454SAndrew Thompson { USB_VP(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050) }, 6702ac6454SAndrew Thompson { USB_VP(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7051) }, 6802ac6454SAndrew Thompson { USB_VP(USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_HU200TS) }, 6902ac6454SAndrew Thompson { USB_VP(USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54G) }, 7002ac6454SAndrew Thompson { USB_VP(USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GP) }, 7102ac6454SAndrew Thompson { USB_VP(USB_VENDOR_CONCEPTRONIC2, USB_PRODUCT_CONCEPTRONIC2_C54RU) }, 7202ac6454SAndrew Thompson { USB_VP(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DWLG122) }, 7302ac6454SAndrew Thompson { USB_VP(USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GN54G) }, 7402ac6454SAndrew Thompson { USB_VP(USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWBKG) }, 7502ac6454SAndrew Thompson { USB_VP(USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254) }, 7602ac6454SAndrew Thompson { USB_VP(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54) }, 7702ac6454SAndrew Thompson { USB_VP(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54AI) }, 7802ac6454SAndrew Thompson { USB_VP(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54YB) }, 7902ac6454SAndrew Thompson { USB_VP(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_NINWIFI) }, 8002ac6454SAndrew Thompson { USB_VP(USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2570) }, 8102ac6454SAndrew Thompson { USB_VP(USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2570_2) }, 8202ac6454SAndrew Thompson { USB_VP(USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2570_3) }, 8302ac6454SAndrew Thompson { USB_VP(USB_VENDOR_NOVATECH, USB_PRODUCT_NOVATECH_NV902) }, 8402ac6454SAndrew Thompson { USB_VP(USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570) }, 8502ac6454SAndrew Thompson { USB_VP(USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570_2) }, 8602ac6454SAndrew Thompson { USB_VP(USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570_3) }, 8702ac6454SAndrew Thompson { USB_VP(USB_VENDOR_SIEMENS2, USB_PRODUCT_SIEMENS2_WL54G) }, 8802ac6454SAndrew Thompson { USB_VP(USB_VENDOR_SMC, USB_PRODUCT_SMC_2862WG) }, 8902ac6454SAndrew Thompson { USB_VP(USB_VENDOR_SPHAIRON, USB_PRODUCT_SPHAIRON_UB801R) }, 9002ac6454SAndrew Thompson { USB_VP(USB_VENDOR_SURECOM, USB_PRODUCT_SURECOM_RT2570) }, 9102ac6454SAndrew Thompson { USB_VP(USB_VENDOR_VTECH, USB_PRODUCT_VTECH_RT2570) }, 9202ac6454SAndrew Thompson { USB_VP(USB_VENDOR_ZINWELL, USB_PRODUCT_ZINWELL_RT2570) }, 9302ac6454SAndrew Thompson }; 9402ac6454SAndrew Thompson 9502ac6454SAndrew Thompson static usb2_callback_t ural_bulk_read_callback; 9602ac6454SAndrew Thompson static usb2_callback_t ural_bulk_write_callback; 9702ac6454SAndrew Thompson 9877ddf3d3SAndrew Thompson static usb2_proc_callback_t ural_command_wrapper; 9902ac6454SAndrew Thompson static usb2_proc_callback_t ural_attach_post; 10002ac6454SAndrew Thompson static usb2_proc_callback_t ural_task; 10102ac6454SAndrew Thompson static usb2_proc_callback_t ural_scantask; 10202ac6454SAndrew Thompson static usb2_proc_callback_t ural_promisctask; 10302ac6454SAndrew Thompson static usb2_proc_callback_t ural_amrr_task; 10402ac6454SAndrew Thompson static usb2_proc_callback_t ural_init_task; 10502ac6454SAndrew Thompson static usb2_proc_callback_t ural_stop_task; 10677ddf3d3SAndrew Thompson static usb2_proc_callback_t ural_flush_task; 10702ac6454SAndrew Thompson 10877ddf3d3SAndrew Thompson static usb2_error_t ural_do_request(struct ural_softc *sc, 10977ddf3d3SAndrew Thompson struct usb2_device_request *req, void *data); 11002ac6454SAndrew Thompson static struct ieee80211vap *ural_vap_create(struct ieee80211com *, 11102ac6454SAndrew Thompson const char name[IFNAMSIZ], int unit, int opmode, 11202ac6454SAndrew Thompson int flags, const uint8_t bssid[IEEE80211_ADDR_LEN], 11302ac6454SAndrew Thompson const uint8_t mac[IEEE80211_ADDR_LEN]); 11402ac6454SAndrew Thompson static void ural_vap_delete(struct ieee80211vap *); 11502ac6454SAndrew Thompson static void ural_tx_free(struct ural_tx_data *, int); 11602ac6454SAndrew Thompson static void ural_setup_tx_list(struct ural_softc *); 11702ac6454SAndrew Thompson static void ural_unsetup_tx_list(struct ural_softc *); 11802ac6454SAndrew Thompson static int ural_newstate(struct ieee80211vap *, 11902ac6454SAndrew Thompson enum ieee80211_state, int); 12002ac6454SAndrew Thompson static void ural_setup_tx_desc(struct ural_softc *, 12102ac6454SAndrew Thompson struct ural_tx_desc *, uint32_t, int, int); 12202ac6454SAndrew Thompson static int ural_tx_bcn(struct ural_softc *, struct mbuf *, 12302ac6454SAndrew Thompson struct ieee80211_node *); 12402ac6454SAndrew Thompson static int ural_tx_mgt(struct ural_softc *, struct mbuf *, 12502ac6454SAndrew Thompson struct ieee80211_node *); 12602ac6454SAndrew Thompson static int ural_tx_data(struct ural_softc *, struct mbuf *, 12702ac6454SAndrew Thompson struct ieee80211_node *); 12802ac6454SAndrew Thompson static void ural_start(struct ifnet *); 12902ac6454SAndrew Thompson static int ural_ioctl(struct ifnet *, u_long, caddr_t); 13002ac6454SAndrew Thompson static void ural_set_testmode(struct ural_softc *); 13102ac6454SAndrew Thompson static void ural_eeprom_read(struct ural_softc *, uint16_t, void *, 13202ac6454SAndrew Thompson int); 13302ac6454SAndrew Thompson static uint16_t ural_read(struct ural_softc *, uint16_t); 13402ac6454SAndrew Thompson static void ural_read_multi(struct ural_softc *, uint16_t, void *, 13502ac6454SAndrew Thompson int); 13602ac6454SAndrew Thompson static void ural_write(struct ural_softc *, uint16_t, uint16_t); 13702ac6454SAndrew Thompson static void ural_write_multi(struct ural_softc *, uint16_t, void *, 13802ac6454SAndrew Thompson int) __unused; 13902ac6454SAndrew Thompson static void ural_bbp_write(struct ural_softc *, uint8_t, uint8_t); 14002ac6454SAndrew Thompson static uint8_t ural_bbp_read(struct ural_softc *, uint8_t); 14102ac6454SAndrew Thompson static void ural_rf_write(struct ural_softc *, uint8_t, uint32_t); 14202ac6454SAndrew Thompson static struct ieee80211_node *ural_node_alloc(struct ieee80211vap *, 14302ac6454SAndrew Thompson const uint8_t mac[IEEE80211_ADDR_LEN]); 14402ac6454SAndrew Thompson static void ural_newassoc(struct ieee80211_node *, int); 14502ac6454SAndrew Thompson static void ural_scan_start(struct ieee80211com *); 14602ac6454SAndrew Thompson static void ural_scan_end(struct ieee80211com *); 14702ac6454SAndrew Thompson static void ural_set_channel(struct ieee80211com *); 14802ac6454SAndrew Thompson static void ural_set_chan(struct ural_softc *, 14902ac6454SAndrew Thompson struct ieee80211_channel *); 15002ac6454SAndrew Thompson static void ural_disable_rf_tune(struct ural_softc *); 15102ac6454SAndrew Thompson static void ural_enable_tsf_sync(struct ural_softc *); 15202ac6454SAndrew Thompson static void ural_update_slot(struct ifnet *); 15302ac6454SAndrew Thompson static void ural_set_txpreamble(struct ural_softc *); 15402ac6454SAndrew Thompson static void ural_set_basicrates(struct ural_softc *, 15502ac6454SAndrew Thompson const struct ieee80211_channel *); 15602ac6454SAndrew Thompson static void ural_set_bssid(struct ural_softc *, const uint8_t *); 15702ac6454SAndrew Thompson static void ural_set_macaddr(struct ural_softc *, uint8_t *); 15877ddf3d3SAndrew Thompson static void ural_update_promisc(struct ifnet *); 15902ac6454SAndrew Thompson static const char *ural_get_rf(int); 16002ac6454SAndrew Thompson static void ural_read_eeprom(struct ural_softc *); 16102ac6454SAndrew Thompson static int ural_bbp_init(struct ural_softc *); 16202ac6454SAndrew Thompson static void ural_set_txantenna(struct ural_softc *, int); 16302ac6454SAndrew Thompson static void ural_set_rxantenna(struct ural_softc *, int); 16402ac6454SAndrew Thompson static void ural_init(void *); 16502ac6454SAndrew Thompson static int ural_raw_xmit(struct ieee80211_node *, struct mbuf *, 16602ac6454SAndrew Thompson const struct ieee80211_bpf_params *); 16702ac6454SAndrew Thompson static void ural_amrr_start(struct ural_softc *, 16802ac6454SAndrew Thompson struct ieee80211_node *); 16902ac6454SAndrew Thompson static void ural_amrr_timeout(void *); 17002ac6454SAndrew Thompson static int ural_pause(struct ural_softc *sc, int timeout); 17102ac6454SAndrew Thompson static void ural_queue_command(struct ural_softc *, 17202ac6454SAndrew Thompson usb2_proc_callback_t *, struct usb2_proc_msg *, 17302ac6454SAndrew Thompson struct usb2_proc_msg *); 17402ac6454SAndrew Thompson 17502ac6454SAndrew Thompson /* 17602ac6454SAndrew Thompson * Default values for MAC registers; values taken from the reference driver. 17702ac6454SAndrew Thompson */ 17802ac6454SAndrew Thompson static const struct { 17902ac6454SAndrew Thompson uint16_t reg; 18002ac6454SAndrew Thompson uint16_t val; 18102ac6454SAndrew Thompson } ural_def_mac[] = { 18202ac6454SAndrew Thompson { RAL_TXRX_CSR5, 0x8c8d }, 18302ac6454SAndrew Thompson { RAL_TXRX_CSR6, 0x8b8a }, 18402ac6454SAndrew Thompson { RAL_TXRX_CSR7, 0x8687 }, 18502ac6454SAndrew Thompson { RAL_TXRX_CSR8, 0x0085 }, 18602ac6454SAndrew Thompson { RAL_MAC_CSR13, 0x1111 }, 18702ac6454SAndrew Thompson { RAL_MAC_CSR14, 0x1e11 }, 18802ac6454SAndrew Thompson { RAL_TXRX_CSR21, 0xe78f }, 18902ac6454SAndrew Thompson { RAL_MAC_CSR9, 0xff1d }, 19002ac6454SAndrew Thompson { RAL_MAC_CSR11, 0x0002 }, 19102ac6454SAndrew Thompson { RAL_MAC_CSR22, 0x0053 }, 19202ac6454SAndrew Thompson { RAL_MAC_CSR15, 0x0000 }, 19302ac6454SAndrew Thompson { RAL_MAC_CSR8, RAL_FRAME_SIZE }, 19402ac6454SAndrew Thompson { RAL_TXRX_CSR19, 0x0000 }, 19502ac6454SAndrew Thompson { RAL_TXRX_CSR18, 0x005a }, 19602ac6454SAndrew Thompson { RAL_PHY_CSR2, 0x0000 }, 19702ac6454SAndrew Thompson { RAL_TXRX_CSR0, 0x1ec0 }, 19802ac6454SAndrew Thompson { RAL_PHY_CSR4, 0x000f } 19902ac6454SAndrew Thompson }; 20002ac6454SAndrew Thompson 20102ac6454SAndrew Thompson /* 20202ac6454SAndrew Thompson * Default values for BBP registers; values taken from the reference driver. 20302ac6454SAndrew Thompson */ 20402ac6454SAndrew Thompson static const struct { 20502ac6454SAndrew Thompson uint8_t reg; 20602ac6454SAndrew Thompson uint8_t val; 20702ac6454SAndrew Thompson } ural_def_bbp[] = { 20802ac6454SAndrew Thompson { 3, 0x02 }, 20902ac6454SAndrew Thompson { 4, 0x19 }, 21002ac6454SAndrew Thompson { 14, 0x1c }, 21102ac6454SAndrew Thompson { 15, 0x30 }, 21202ac6454SAndrew Thompson { 16, 0xac }, 21302ac6454SAndrew Thompson { 17, 0x48 }, 21402ac6454SAndrew Thompson { 18, 0x18 }, 21502ac6454SAndrew Thompson { 19, 0xff }, 21602ac6454SAndrew Thompson { 20, 0x1e }, 21702ac6454SAndrew Thompson { 21, 0x08 }, 21802ac6454SAndrew Thompson { 22, 0x08 }, 21902ac6454SAndrew Thompson { 23, 0x08 }, 22002ac6454SAndrew Thompson { 24, 0x80 }, 22102ac6454SAndrew Thompson { 25, 0x50 }, 22202ac6454SAndrew Thompson { 26, 0x08 }, 22302ac6454SAndrew Thompson { 27, 0x23 }, 22402ac6454SAndrew Thompson { 30, 0x10 }, 22502ac6454SAndrew Thompson { 31, 0x2b }, 22602ac6454SAndrew Thompson { 32, 0xb9 }, 22702ac6454SAndrew Thompson { 34, 0x12 }, 22802ac6454SAndrew Thompson { 35, 0x50 }, 22902ac6454SAndrew Thompson { 39, 0xc4 }, 23002ac6454SAndrew Thompson { 40, 0x02 }, 23102ac6454SAndrew Thompson { 41, 0x60 }, 23202ac6454SAndrew Thompson { 53, 0x10 }, 23302ac6454SAndrew Thompson { 54, 0x18 }, 23402ac6454SAndrew Thompson { 56, 0x08 }, 23502ac6454SAndrew Thompson { 57, 0x10 }, 23602ac6454SAndrew Thompson { 58, 0x08 }, 23702ac6454SAndrew Thompson { 61, 0x60 }, 23802ac6454SAndrew Thompson { 62, 0x10 }, 23902ac6454SAndrew Thompson { 75, 0xff } 24002ac6454SAndrew Thompson }; 24102ac6454SAndrew Thompson 24202ac6454SAndrew Thompson /* 24302ac6454SAndrew Thompson * Default values for RF register R2 indexed by channel numbers. 24402ac6454SAndrew Thompson */ 24502ac6454SAndrew Thompson static const uint32_t ural_rf2522_r2[] = { 24602ac6454SAndrew Thompson 0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814, 24702ac6454SAndrew Thompson 0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e 24802ac6454SAndrew Thompson }; 24902ac6454SAndrew Thompson 25002ac6454SAndrew Thompson static const uint32_t ural_rf2523_r2[] = { 25102ac6454SAndrew Thompson 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d, 25202ac6454SAndrew Thompson 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346 25302ac6454SAndrew Thompson }; 25402ac6454SAndrew Thompson 25502ac6454SAndrew Thompson static const uint32_t ural_rf2524_r2[] = { 25602ac6454SAndrew Thompson 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d, 25702ac6454SAndrew Thompson 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346 25802ac6454SAndrew Thompson }; 25902ac6454SAndrew Thompson 26002ac6454SAndrew Thompson static const uint32_t ural_rf2525_r2[] = { 26102ac6454SAndrew Thompson 0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d, 26202ac6454SAndrew Thompson 0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346 26302ac6454SAndrew Thompson }; 26402ac6454SAndrew Thompson 26502ac6454SAndrew Thompson static const uint32_t ural_rf2525_hi_r2[] = { 26602ac6454SAndrew Thompson 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345, 26702ac6454SAndrew Thompson 0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e 26802ac6454SAndrew Thompson }; 26902ac6454SAndrew Thompson 27002ac6454SAndrew Thompson static const uint32_t ural_rf2525e_r2[] = { 27102ac6454SAndrew Thompson 0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463, 27202ac6454SAndrew Thompson 0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b 27302ac6454SAndrew Thompson }; 27402ac6454SAndrew Thompson 27502ac6454SAndrew Thompson static const uint32_t ural_rf2526_hi_r2[] = { 27602ac6454SAndrew Thompson 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d, 27702ac6454SAndrew Thompson 0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241 27802ac6454SAndrew Thompson }; 27902ac6454SAndrew Thompson 28002ac6454SAndrew Thompson static const uint32_t ural_rf2526_r2[] = { 28102ac6454SAndrew Thompson 0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229, 28202ac6454SAndrew Thompson 0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d 28302ac6454SAndrew Thompson }; 28402ac6454SAndrew Thompson 28502ac6454SAndrew Thompson /* 28602ac6454SAndrew Thompson * For dual-band RF, RF registers R1 and R4 also depend on channel number; 28702ac6454SAndrew Thompson * values taken from the reference driver. 28802ac6454SAndrew Thompson */ 28902ac6454SAndrew Thompson static const struct { 29002ac6454SAndrew Thompson uint8_t chan; 29102ac6454SAndrew Thompson uint32_t r1; 29202ac6454SAndrew Thompson uint32_t r2; 29302ac6454SAndrew Thompson uint32_t r4; 29402ac6454SAndrew Thompson } ural_rf5222[] = { 29502ac6454SAndrew Thompson { 1, 0x08808, 0x0044d, 0x00282 }, 29602ac6454SAndrew Thompson { 2, 0x08808, 0x0044e, 0x00282 }, 29702ac6454SAndrew Thompson { 3, 0x08808, 0x0044f, 0x00282 }, 29802ac6454SAndrew Thompson { 4, 0x08808, 0x00460, 0x00282 }, 29902ac6454SAndrew Thompson { 5, 0x08808, 0x00461, 0x00282 }, 30002ac6454SAndrew Thompson { 6, 0x08808, 0x00462, 0x00282 }, 30102ac6454SAndrew Thompson { 7, 0x08808, 0x00463, 0x00282 }, 30202ac6454SAndrew Thompson { 8, 0x08808, 0x00464, 0x00282 }, 30302ac6454SAndrew Thompson { 9, 0x08808, 0x00465, 0x00282 }, 30402ac6454SAndrew Thompson { 10, 0x08808, 0x00466, 0x00282 }, 30502ac6454SAndrew Thompson { 11, 0x08808, 0x00467, 0x00282 }, 30602ac6454SAndrew Thompson { 12, 0x08808, 0x00468, 0x00282 }, 30702ac6454SAndrew Thompson { 13, 0x08808, 0x00469, 0x00282 }, 30802ac6454SAndrew Thompson { 14, 0x08808, 0x0046b, 0x00286 }, 30902ac6454SAndrew Thompson 31002ac6454SAndrew Thompson { 36, 0x08804, 0x06225, 0x00287 }, 31102ac6454SAndrew Thompson { 40, 0x08804, 0x06226, 0x00287 }, 31202ac6454SAndrew Thompson { 44, 0x08804, 0x06227, 0x00287 }, 31302ac6454SAndrew Thompson { 48, 0x08804, 0x06228, 0x00287 }, 31402ac6454SAndrew Thompson { 52, 0x08804, 0x06229, 0x00287 }, 31502ac6454SAndrew Thompson { 56, 0x08804, 0x0622a, 0x00287 }, 31602ac6454SAndrew Thompson { 60, 0x08804, 0x0622b, 0x00287 }, 31702ac6454SAndrew Thompson { 64, 0x08804, 0x0622c, 0x00287 }, 31802ac6454SAndrew Thompson 31902ac6454SAndrew Thompson { 100, 0x08804, 0x02200, 0x00283 }, 32002ac6454SAndrew Thompson { 104, 0x08804, 0x02201, 0x00283 }, 32102ac6454SAndrew Thompson { 108, 0x08804, 0x02202, 0x00283 }, 32202ac6454SAndrew Thompson { 112, 0x08804, 0x02203, 0x00283 }, 32302ac6454SAndrew Thompson { 116, 0x08804, 0x02204, 0x00283 }, 32402ac6454SAndrew Thompson { 120, 0x08804, 0x02205, 0x00283 }, 32502ac6454SAndrew Thompson { 124, 0x08804, 0x02206, 0x00283 }, 32602ac6454SAndrew Thompson { 128, 0x08804, 0x02207, 0x00283 }, 32702ac6454SAndrew Thompson { 132, 0x08804, 0x02208, 0x00283 }, 32802ac6454SAndrew Thompson { 136, 0x08804, 0x02209, 0x00283 }, 32902ac6454SAndrew Thompson { 140, 0x08804, 0x0220a, 0x00283 }, 33002ac6454SAndrew Thompson 33102ac6454SAndrew Thompson { 149, 0x08808, 0x02429, 0x00281 }, 33202ac6454SAndrew Thompson { 153, 0x08808, 0x0242b, 0x00281 }, 33302ac6454SAndrew Thompson { 157, 0x08808, 0x0242d, 0x00281 }, 33402ac6454SAndrew Thompson { 161, 0x08808, 0x0242f, 0x00281 } 33502ac6454SAndrew Thompson }; 33602ac6454SAndrew Thompson 33702ac6454SAndrew Thompson static const struct usb2_config ural_config[URAL_N_TRANSFER] = { 33802ac6454SAndrew Thompson [URAL_BULK_WR] = { 33902ac6454SAndrew Thompson .type = UE_BULK, 34002ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 34102ac6454SAndrew Thompson .direction = UE_DIR_OUT, 34202ac6454SAndrew Thompson .mh.bufsize = (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE + 4), 34302ac6454SAndrew Thompson .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 34402ac6454SAndrew Thompson .mh.callback = ural_bulk_write_callback, 34502ac6454SAndrew Thompson .mh.timeout = 5000, /* ms */ 34602ac6454SAndrew Thompson }, 34702ac6454SAndrew Thompson [URAL_BULK_RD] = { 34802ac6454SAndrew Thompson .type = UE_BULK, 34902ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 35002ac6454SAndrew Thompson .direction = UE_DIR_IN, 35102ac6454SAndrew Thompson .mh.bufsize = (RAL_FRAME_SIZE + RAL_RX_DESC_SIZE), 35202ac6454SAndrew Thompson .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 35302ac6454SAndrew Thompson .mh.callback = ural_bulk_read_callback, 35402ac6454SAndrew Thompson }, 35502ac6454SAndrew Thompson }; 35602ac6454SAndrew Thompson 35702ac6454SAndrew Thompson static device_probe_t ural_match; 35802ac6454SAndrew Thompson static device_attach_t ural_attach; 35902ac6454SAndrew Thompson static device_detach_t ural_detach; 36002ac6454SAndrew Thompson 36102ac6454SAndrew Thompson static device_method_t ural_methods[] = { 36202ac6454SAndrew Thompson /* Device interface */ 36302ac6454SAndrew Thompson DEVMETHOD(device_probe, ural_match), 36402ac6454SAndrew Thompson DEVMETHOD(device_attach, ural_attach), 36502ac6454SAndrew Thompson DEVMETHOD(device_detach, ural_detach), 36602ac6454SAndrew Thompson 36702ac6454SAndrew Thompson { 0, 0 } 36802ac6454SAndrew Thompson }; 36902ac6454SAndrew Thompson 37002ac6454SAndrew Thompson static driver_t ural_driver = { 37102ac6454SAndrew Thompson .name = "ural", 37202ac6454SAndrew Thompson .methods = ural_methods, 37302ac6454SAndrew Thompson .size = sizeof(struct ural_softc), 37402ac6454SAndrew Thompson }; 37502ac6454SAndrew Thompson 37602ac6454SAndrew Thompson static devclass_t ural_devclass; 37702ac6454SAndrew Thompson 3789aef556dSAndrew Thompson DRIVER_MODULE(ural, uhub, ural_driver, ural_devclass, NULL, 0); 37902ac6454SAndrew Thompson MODULE_DEPEND(ural, usb, 1, 1, 1); 38002ac6454SAndrew Thompson MODULE_DEPEND(ural, wlan, 1, 1, 1); 38102ac6454SAndrew Thompson MODULE_DEPEND(ural, wlan_amrr, 1, 1, 1); 38202ac6454SAndrew Thompson 38302ac6454SAndrew Thompson static int 38402ac6454SAndrew Thompson ural_match(device_t self) 38502ac6454SAndrew Thompson { 38602ac6454SAndrew Thompson struct usb2_attach_arg *uaa = device_get_ivars(self); 38702ac6454SAndrew Thompson 38802ac6454SAndrew Thompson if (uaa->usb2_mode != USB_MODE_HOST) 38902ac6454SAndrew Thompson return (ENXIO); 39002ac6454SAndrew Thompson if (uaa->info.bConfigIndex != 0) 39102ac6454SAndrew Thompson return (ENXIO); 39202ac6454SAndrew Thompson if (uaa->info.bIfaceIndex != RAL_IFACE_INDEX) 39302ac6454SAndrew Thompson return (ENXIO); 39402ac6454SAndrew Thompson 39502ac6454SAndrew Thompson return (usb2_lookup_id_by_uaa(ural_devs, sizeof(ural_devs), uaa)); 39602ac6454SAndrew Thompson } 39702ac6454SAndrew Thompson 39802ac6454SAndrew Thompson static int 39902ac6454SAndrew Thompson ural_attach(device_t self) 40002ac6454SAndrew Thompson { 40102ac6454SAndrew Thompson struct usb2_attach_arg *uaa = device_get_ivars(self); 40202ac6454SAndrew Thompson struct ural_softc *sc = device_get_softc(self); 40302ac6454SAndrew Thompson int error; 40402ac6454SAndrew Thompson uint8_t iface_index; 40502ac6454SAndrew Thompson 40602ac6454SAndrew Thompson device_set_usb2_desc(self); 40702ac6454SAndrew Thompson sc->sc_udev = uaa->device; 40802ac6454SAndrew Thompson sc->sc_dev = self; 40902ac6454SAndrew Thompson 41002ac6454SAndrew Thompson mtx_init(&sc->sc_mtx, device_get_nameunit(self), 41102ac6454SAndrew Thompson MTX_NETWORK_LOCK, MTX_DEF); 41202ac6454SAndrew Thompson 41377ddf3d3SAndrew Thompson cv_init(&sc->sc_cmd_cv, "wtxdone"); 41477ddf3d3SAndrew Thompson 41502ac6454SAndrew Thompson iface_index = RAL_IFACE_INDEX; 41602ac6454SAndrew Thompson error = usb2_transfer_setup(uaa->device, 41702ac6454SAndrew Thompson &iface_index, sc->sc_xfer, ural_config, 41802ac6454SAndrew Thompson URAL_N_TRANSFER, sc, &sc->sc_mtx); 41902ac6454SAndrew Thompson if (error) { 42002ac6454SAndrew Thompson device_printf(self, "could not allocate USB transfers, " 42102ac6454SAndrew Thompson "err=%s\n", usb2_errstr(error)); 42202ac6454SAndrew Thompson goto detach; 42302ac6454SAndrew Thompson } 42402ac6454SAndrew Thompson error = usb2_proc_create(&sc->sc_tq, &sc->sc_mtx, 42502ac6454SAndrew Thompson device_get_nameunit(self), USB_PRI_MED); 42602ac6454SAndrew Thompson if (error) { 42702ac6454SAndrew Thompson device_printf(self, "could not setup config thread!\n"); 42802ac6454SAndrew Thompson goto detach; 42902ac6454SAndrew Thompson } 43002ac6454SAndrew Thompson 43102ac6454SAndrew Thompson /* fork rest of the attach code */ 43202ac6454SAndrew Thompson RAL_LOCK(sc); 43302ac6454SAndrew Thompson ural_queue_command(sc, ural_attach_post, 43402ac6454SAndrew Thompson &sc->sc_synctask[0].hdr, 43502ac6454SAndrew Thompson &sc->sc_synctask[1].hdr); 43602ac6454SAndrew Thompson RAL_UNLOCK(sc); 43702ac6454SAndrew Thompson return (0); 43802ac6454SAndrew Thompson 43902ac6454SAndrew Thompson detach: 44002ac6454SAndrew Thompson ural_detach(self); 44102ac6454SAndrew Thompson return (ENXIO); /* failure */ 44202ac6454SAndrew Thompson } 44302ac6454SAndrew Thompson 44402ac6454SAndrew Thompson static void 44502ac6454SAndrew Thompson ural_attach_post(struct usb2_proc_msg *pm) 44602ac6454SAndrew Thompson { 44702ac6454SAndrew Thompson struct ural_task *task = (struct ural_task *)pm; 44802ac6454SAndrew Thompson struct ural_softc *sc = task->sc; 44902ac6454SAndrew Thompson struct ifnet *ifp; 45002ac6454SAndrew Thompson struct ieee80211com *ic; 45102ac6454SAndrew Thompson uint8_t bands; 45202ac6454SAndrew Thompson 45302ac6454SAndrew Thompson /* retrieve RT2570 rev. no */ 45402ac6454SAndrew Thompson sc->asic_rev = ural_read(sc, RAL_MAC_CSR0); 45502ac6454SAndrew Thompson 45602ac6454SAndrew Thompson /* retrieve MAC address and various other things from EEPROM */ 45702ac6454SAndrew Thompson ural_read_eeprom(sc); 45877ddf3d3SAndrew Thompson 45977ddf3d3SAndrew Thompson /* XXX Async attach race */ 46077ddf3d3SAndrew Thompson if (usb2_proc_is_gone(&sc->sc_tq)) 46177ddf3d3SAndrew Thompson return; 46277ddf3d3SAndrew Thompson 46302ac6454SAndrew Thompson RAL_UNLOCK(sc); 46402ac6454SAndrew Thompson 46502ac6454SAndrew Thompson device_printf(sc->sc_dev, "MAC/BBP RT2570 (rev 0x%02x), RF %s\n", 46602ac6454SAndrew Thompson sc->asic_rev, ural_get_rf(sc->rf_rev)); 46702ac6454SAndrew Thompson 46802ac6454SAndrew Thompson ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 46902ac6454SAndrew Thompson if (ifp == NULL) { 47002ac6454SAndrew Thompson device_printf(sc->sc_dev, "can not if_alloc()\n"); 47102ac6454SAndrew Thompson RAL_LOCK(sc); 47202ac6454SAndrew Thompson return; 47302ac6454SAndrew Thompson } 47402ac6454SAndrew Thompson ic = ifp->if_l2com; 47502ac6454SAndrew Thompson 47602ac6454SAndrew Thompson ifp->if_softc = sc; 47702ac6454SAndrew Thompson if_initname(ifp, "ural", device_get_unit(sc->sc_dev)); 47802ac6454SAndrew Thompson ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 47902ac6454SAndrew Thompson ifp->if_init = ural_init; 48002ac6454SAndrew Thompson ifp->if_ioctl = ural_ioctl; 48102ac6454SAndrew Thompson ifp->if_start = ural_start; 48202ac6454SAndrew Thompson IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 48302ac6454SAndrew Thompson ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 48402ac6454SAndrew Thompson IFQ_SET_READY(&ifp->if_snd); 48502ac6454SAndrew Thompson 48602ac6454SAndrew Thompson ic->ic_ifp = ifp; 48702ac6454SAndrew Thompson ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 48802ac6454SAndrew Thompson 48902ac6454SAndrew Thompson /* set device capabilities */ 49002ac6454SAndrew Thompson ic->ic_caps = 49102ac6454SAndrew Thompson IEEE80211_C_STA /* station mode supported */ 49202ac6454SAndrew Thompson | IEEE80211_C_IBSS /* IBSS mode supported */ 49302ac6454SAndrew Thompson | IEEE80211_C_MONITOR /* monitor mode supported */ 49402ac6454SAndrew Thompson | IEEE80211_C_HOSTAP /* HostAp mode supported */ 49502ac6454SAndrew Thompson | IEEE80211_C_TXPMGT /* tx power management */ 49602ac6454SAndrew Thompson | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 49702ac6454SAndrew Thompson | IEEE80211_C_SHSLOT /* short slot time supported */ 49802ac6454SAndrew Thompson | IEEE80211_C_BGSCAN /* bg scanning supported */ 49902ac6454SAndrew Thompson | IEEE80211_C_WPA /* 802.11i */ 50002ac6454SAndrew Thompson ; 50102ac6454SAndrew Thompson 50202ac6454SAndrew Thompson bands = 0; 50302ac6454SAndrew Thompson setbit(&bands, IEEE80211_MODE_11B); 50402ac6454SAndrew Thompson setbit(&bands, IEEE80211_MODE_11G); 50502ac6454SAndrew Thompson if (sc->rf_rev == RAL_RF_5222) 50602ac6454SAndrew Thompson setbit(&bands, IEEE80211_MODE_11A); 50702ac6454SAndrew Thompson ieee80211_init_channels(ic, NULL, &bands); 50802ac6454SAndrew Thompson 50929aca940SSam Leffler ieee80211_ifattach(ic, sc->sc_bssid); 51077ddf3d3SAndrew Thompson ic->ic_update_promisc = ural_update_promisc; 51102ac6454SAndrew Thompson ic->ic_newassoc = ural_newassoc; 51202ac6454SAndrew Thompson ic->ic_raw_xmit = ural_raw_xmit; 51302ac6454SAndrew Thompson ic->ic_node_alloc = ural_node_alloc; 51402ac6454SAndrew Thompson ic->ic_scan_start = ural_scan_start; 51502ac6454SAndrew Thompson ic->ic_scan_end = ural_scan_end; 51602ac6454SAndrew Thompson ic->ic_set_channel = ural_set_channel; 51702ac6454SAndrew Thompson 51802ac6454SAndrew Thompson ic->ic_vap_create = ural_vap_create; 51902ac6454SAndrew Thompson ic->ic_vap_delete = ural_vap_delete; 52002ac6454SAndrew Thompson 52102ac6454SAndrew Thompson bpfattach(ifp, DLT_IEEE802_11_RADIO, 52202ac6454SAndrew Thompson sizeof (struct ieee80211_frame) + sizeof(sc->sc_txtap)); 52302ac6454SAndrew Thompson 52402ac6454SAndrew Thompson sc->sc_rxtap_len = sizeof sc->sc_rxtap; 52502ac6454SAndrew Thompson sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 52602ac6454SAndrew Thompson sc->sc_rxtap.wr_ihdr.it_present = htole32(RAL_RX_RADIOTAP_PRESENT); 52702ac6454SAndrew Thompson 52802ac6454SAndrew Thompson sc->sc_txtap_len = sizeof sc->sc_txtap; 52902ac6454SAndrew Thompson sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 53002ac6454SAndrew Thompson sc->sc_txtap.wt_ihdr.it_present = htole32(RAL_TX_RADIOTAP_PRESENT); 53102ac6454SAndrew Thompson 53202ac6454SAndrew Thompson if (bootverbose) 53302ac6454SAndrew Thompson ieee80211_announce(ic); 53402ac6454SAndrew Thompson 53502ac6454SAndrew Thompson RAL_LOCK(sc); 53602ac6454SAndrew Thompson } 53702ac6454SAndrew Thompson 53802ac6454SAndrew Thompson static int 53902ac6454SAndrew Thompson ural_detach(device_t self) 54002ac6454SAndrew Thompson { 54102ac6454SAndrew Thompson struct ural_softc *sc = device_get_softc(self); 54202ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 54302ac6454SAndrew Thompson struct ieee80211com *ic; 54402ac6454SAndrew Thompson 54502ac6454SAndrew Thompson /* wait for any post attach or other command to complete */ 54602ac6454SAndrew Thompson usb2_proc_drain(&sc->sc_tq); 54702ac6454SAndrew Thompson 54802ac6454SAndrew Thompson /* stop all USB transfers */ 54902ac6454SAndrew Thompson usb2_transfer_unsetup(sc->sc_xfer, URAL_N_TRANSFER); 55002ac6454SAndrew Thompson usb2_proc_free(&sc->sc_tq); 55102ac6454SAndrew Thompson 55202ac6454SAndrew Thompson /* free TX list, if any */ 55302ac6454SAndrew Thompson RAL_LOCK(sc); 55402ac6454SAndrew Thompson ural_unsetup_tx_list(sc); 55502ac6454SAndrew Thompson RAL_UNLOCK(sc); 55602ac6454SAndrew Thompson 55702ac6454SAndrew Thompson if (ifp) { 55802ac6454SAndrew Thompson ic = ifp->if_l2com; 55902ac6454SAndrew Thompson bpfdetach(ifp); 56002ac6454SAndrew Thompson ieee80211_ifdetach(ic); 56102ac6454SAndrew Thompson if_free(ifp); 56202ac6454SAndrew Thompson } 56377ddf3d3SAndrew Thompson cv_destroy(&sc->sc_cmd_cv); 56402ac6454SAndrew Thompson mtx_destroy(&sc->sc_mtx); 56502ac6454SAndrew Thompson 56602ac6454SAndrew Thompson return (0); 56702ac6454SAndrew Thompson } 56802ac6454SAndrew Thompson 56977ddf3d3SAndrew Thompson static usb2_error_t 57077ddf3d3SAndrew Thompson ural_do_request(struct ural_softc *sc, 57177ddf3d3SAndrew Thompson struct usb2_device_request *req, void *data) 57277ddf3d3SAndrew Thompson { 57377ddf3d3SAndrew Thompson usb2_error_t err; 57477ddf3d3SAndrew Thompson int ntries = 10; 57577ddf3d3SAndrew Thompson 57677ddf3d3SAndrew Thompson while (ntries--) { 57777ddf3d3SAndrew Thompson err = usb2_do_request_proc(sc->sc_udev, &sc->sc_tq, 57877ddf3d3SAndrew Thompson req, data, 0, NULL, 250 /* ms */); 57977ddf3d3SAndrew Thompson if (err == 0) 58077ddf3d3SAndrew Thompson break; 58177ddf3d3SAndrew Thompson 58277ddf3d3SAndrew Thompson DPRINTFN(1, "Control request failed, %s (retrying)\n", 58377ddf3d3SAndrew Thompson usb2_errstr(err)); 58477ddf3d3SAndrew Thompson if (ural_pause(sc, hz / 100)) 58577ddf3d3SAndrew Thompson break; 58677ddf3d3SAndrew Thompson } 58777ddf3d3SAndrew Thompson return (err); 58877ddf3d3SAndrew Thompson } 58977ddf3d3SAndrew Thompson 59002ac6454SAndrew Thompson static struct ieee80211vap * 59102ac6454SAndrew Thompson ural_vap_create(struct ieee80211com *ic, 59202ac6454SAndrew Thompson const char name[IFNAMSIZ], int unit, int opmode, int flags, 59302ac6454SAndrew Thompson const uint8_t bssid[IEEE80211_ADDR_LEN], 59402ac6454SAndrew Thompson const uint8_t mac[IEEE80211_ADDR_LEN]) 59502ac6454SAndrew Thompson { 59602ac6454SAndrew Thompson struct ural_softc *sc = ic->ic_ifp->if_softc; 59702ac6454SAndrew Thompson struct ural_vap *uvp; 59802ac6454SAndrew Thompson struct ieee80211vap *vap; 59902ac6454SAndrew Thompson 60002ac6454SAndrew Thompson if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 60102ac6454SAndrew Thompson return NULL; 60202ac6454SAndrew Thompson uvp = (struct ural_vap *) malloc(sizeof(struct ural_vap), 60302ac6454SAndrew Thompson M_80211_VAP, M_NOWAIT | M_ZERO); 60402ac6454SAndrew Thompson if (uvp == NULL) 60502ac6454SAndrew Thompson return NULL; 60602ac6454SAndrew Thompson vap = &uvp->vap; 60702ac6454SAndrew Thompson /* enable s/w bmiss handling for sta mode */ 60802ac6454SAndrew Thompson ieee80211_vap_setup(ic, vap, name, unit, opmode, 60902ac6454SAndrew Thompson flags | IEEE80211_CLONE_NOBEACONS, bssid, mac); 61002ac6454SAndrew Thompson 61102ac6454SAndrew Thompson /* override state transition machine */ 61202ac6454SAndrew Thompson uvp->newstate = vap->iv_newstate; 61302ac6454SAndrew Thompson vap->iv_newstate = ural_newstate; 61402ac6454SAndrew Thompson 61502ac6454SAndrew Thompson uvp->sc = sc; 61602ac6454SAndrew Thompson usb2_callout_init_mtx(&uvp->amrr_ch, &sc->sc_mtx, 0); 61702ac6454SAndrew Thompson ieee80211_amrr_init(&uvp->amrr, vap, 61802ac6454SAndrew Thompson IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD, 61902ac6454SAndrew Thompson IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD, 62002ac6454SAndrew Thompson 1000 /* 1 sec */); 62102ac6454SAndrew Thompson 62202ac6454SAndrew Thompson /* complete setup */ 62302ac6454SAndrew Thompson ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status); 62402ac6454SAndrew Thompson ic->ic_opmode = opmode; 62502ac6454SAndrew Thompson return vap; 62602ac6454SAndrew Thompson } 62702ac6454SAndrew Thompson 62802ac6454SAndrew Thompson static void 62977ddf3d3SAndrew Thompson ural_flush_task(struct usb2_proc_msg *pm) 63077ddf3d3SAndrew Thompson { 63177ddf3d3SAndrew Thompson /* nothing to do */ 63277ddf3d3SAndrew Thompson } 63377ddf3d3SAndrew Thompson 63477ddf3d3SAndrew Thompson static void 63502ac6454SAndrew Thompson ural_vap_delete(struct ieee80211vap *vap) 63602ac6454SAndrew Thompson { 63702ac6454SAndrew Thompson struct ural_vap *uvp = URAL_VAP(vap); 63877ddf3d3SAndrew Thompson struct ural_softc *sc = uvp->sc; 63977ddf3d3SAndrew Thompson 64077ddf3d3SAndrew Thompson RAL_LOCK(sc); 64177ddf3d3SAndrew Thompson /* wait for any pending tasks to complete */ 64277ddf3d3SAndrew Thompson ural_queue_command(sc, ural_flush_task, 64377ddf3d3SAndrew Thompson &sc->sc_synctask[0].hdr, 64477ddf3d3SAndrew Thompson &sc->sc_synctask[1].hdr); 64577ddf3d3SAndrew Thompson RAL_UNLOCK(sc); 64602ac6454SAndrew Thompson 64702ac6454SAndrew Thompson usb2_callout_drain(&uvp->amrr_ch); 64802ac6454SAndrew Thompson ieee80211_amrr_cleanup(&uvp->amrr); 64902ac6454SAndrew Thompson ieee80211_vap_detach(vap); 65002ac6454SAndrew Thompson free(uvp, M_80211_VAP); 65102ac6454SAndrew Thompson } 65202ac6454SAndrew Thompson 65302ac6454SAndrew Thompson static void 65402ac6454SAndrew Thompson ural_tx_free(struct ural_tx_data *data, int txerr) 65502ac6454SAndrew Thompson { 65602ac6454SAndrew Thompson struct ural_softc *sc = data->sc; 65702ac6454SAndrew Thompson 65802ac6454SAndrew Thompson if (data->m != NULL) { 65902ac6454SAndrew Thompson if (data->m->m_flags & M_TXCB) 66002ac6454SAndrew Thompson ieee80211_process_callback(data->ni, data->m, 66102ac6454SAndrew Thompson txerr ? ETIMEDOUT : 0); 66202ac6454SAndrew Thompson m_freem(data->m); 66302ac6454SAndrew Thompson data->m = NULL; 66402ac6454SAndrew Thompson 66502ac6454SAndrew Thompson ieee80211_free_node(data->ni); 66602ac6454SAndrew Thompson data->ni = NULL; 66702ac6454SAndrew Thompson } 66802ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 66902ac6454SAndrew Thompson sc->tx_nfree++; 67002ac6454SAndrew Thompson } 67102ac6454SAndrew Thompson 67202ac6454SAndrew Thompson static void 67302ac6454SAndrew Thompson ural_setup_tx_list(struct ural_softc *sc) 67402ac6454SAndrew Thompson { 67502ac6454SAndrew Thompson struct ural_tx_data *data; 67602ac6454SAndrew Thompson int i; 67702ac6454SAndrew Thompson 67802ac6454SAndrew Thompson sc->tx_nfree = 0; 67902ac6454SAndrew Thompson STAILQ_INIT(&sc->tx_q); 68002ac6454SAndrew Thompson STAILQ_INIT(&sc->tx_free); 68102ac6454SAndrew Thompson 68202ac6454SAndrew Thompson for (i = 0; i < RAL_TX_LIST_COUNT; i++) { 68302ac6454SAndrew Thompson data = &sc->tx_data[i]; 68402ac6454SAndrew Thompson 68502ac6454SAndrew Thompson data->sc = sc; 68602ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 68702ac6454SAndrew Thompson sc->tx_nfree++; 68802ac6454SAndrew Thompson } 68902ac6454SAndrew Thompson } 69002ac6454SAndrew Thompson 69102ac6454SAndrew Thompson static void 69202ac6454SAndrew Thompson ural_unsetup_tx_list(struct ural_softc *sc) 69302ac6454SAndrew Thompson { 69402ac6454SAndrew Thompson struct ural_tx_data *data; 69502ac6454SAndrew Thompson int i; 69602ac6454SAndrew Thompson 69702ac6454SAndrew Thompson /* make sure any subsequent use of the queues will fail */ 69802ac6454SAndrew Thompson sc->tx_nfree = 0; 69902ac6454SAndrew Thompson STAILQ_INIT(&sc->tx_q); 70002ac6454SAndrew Thompson STAILQ_INIT(&sc->tx_free); 70102ac6454SAndrew Thompson 70202ac6454SAndrew Thompson /* free up all node references and mbufs */ 70302ac6454SAndrew Thompson for (i = 0; i < RAL_TX_LIST_COUNT; i++) { 70402ac6454SAndrew Thompson data = &sc->tx_data[i]; 70502ac6454SAndrew Thompson 70602ac6454SAndrew Thompson if (data->m != NULL) { 70702ac6454SAndrew Thompson m_freem(data->m); 70802ac6454SAndrew Thompson data->m = NULL; 70902ac6454SAndrew Thompson } 71002ac6454SAndrew Thompson if (data->ni != NULL) { 71102ac6454SAndrew Thompson ieee80211_free_node(data->ni); 71202ac6454SAndrew Thompson data->ni = NULL; 71302ac6454SAndrew Thompson } 71402ac6454SAndrew Thompson } 71502ac6454SAndrew Thompson } 71602ac6454SAndrew Thompson 71702ac6454SAndrew Thompson static void 71802ac6454SAndrew Thompson ural_task(struct usb2_proc_msg *pm) 71902ac6454SAndrew Thompson { 72002ac6454SAndrew Thompson struct ural_task *task = (struct ural_task *)pm; 72102ac6454SAndrew Thompson struct ural_softc *sc = task->sc; 72202ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 72302ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 72402ac6454SAndrew Thompson struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 72502ac6454SAndrew Thompson struct ural_vap *uvp = URAL_VAP(vap); 72602ac6454SAndrew Thompson const struct ieee80211_txparam *tp; 72702ac6454SAndrew Thompson enum ieee80211_state ostate; 72802ac6454SAndrew Thompson struct ieee80211_node *ni; 72902ac6454SAndrew Thompson struct mbuf *m; 73002ac6454SAndrew Thompson 73102ac6454SAndrew Thompson ostate = vap->iv_state; 73202ac6454SAndrew Thompson 73302ac6454SAndrew Thompson switch (sc->sc_state) { 73402ac6454SAndrew Thompson case IEEE80211_S_INIT: 73502ac6454SAndrew Thompson if (ostate == IEEE80211_S_RUN) { 73602ac6454SAndrew Thompson /* abort TSF synchronization */ 73702ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR19, 0); 73802ac6454SAndrew Thompson 73902ac6454SAndrew Thompson /* force tx led to stop blinking */ 74002ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR20, 0); 74102ac6454SAndrew Thompson } 74202ac6454SAndrew Thompson break; 74302ac6454SAndrew Thompson 74402ac6454SAndrew Thompson case IEEE80211_S_RUN: 74502ac6454SAndrew Thompson ni = vap->iv_bss; 74602ac6454SAndrew Thompson 74702ac6454SAndrew Thompson if (vap->iv_opmode != IEEE80211_M_MONITOR) { 74802ac6454SAndrew Thompson ural_update_slot(ic->ic_ifp); 74902ac6454SAndrew Thompson ural_set_txpreamble(sc); 75002ac6454SAndrew Thompson ural_set_basicrates(sc, ic->ic_bsschan); 75102ac6454SAndrew Thompson IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid); 75202ac6454SAndrew Thompson ural_set_bssid(sc, sc->sc_bssid); 75302ac6454SAndrew Thompson } 75402ac6454SAndrew Thompson 75502ac6454SAndrew Thompson if (vap->iv_opmode == IEEE80211_M_HOSTAP || 75602ac6454SAndrew Thompson vap->iv_opmode == IEEE80211_M_IBSS) { 75702ac6454SAndrew Thompson m = ieee80211_beacon_alloc(ni, &uvp->bo); 75802ac6454SAndrew Thompson if (m == NULL) { 75902ac6454SAndrew Thompson device_printf(sc->sc_dev, 76002ac6454SAndrew Thompson "could not allocate beacon\n"); 76102ac6454SAndrew Thompson return; 76202ac6454SAndrew Thompson } 76302ac6454SAndrew Thompson 76402ac6454SAndrew Thompson if (ural_tx_bcn(sc, m, ni) != 0) { 76502ac6454SAndrew Thompson device_printf(sc->sc_dev, 76602ac6454SAndrew Thompson "could not send beacon\n"); 76702ac6454SAndrew Thompson return; 76802ac6454SAndrew Thompson } 76902ac6454SAndrew Thompson } 77002ac6454SAndrew Thompson 77102ac6454SAndrew Thompson /* make tx led blink on tx (controlled by ASIC) */ 77202ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR20, 1); 77302ac6454SAndrew Thompson 77402ac6454SAndrew Thompson if (vap->iv_opmode != IEEE80211_M_MONITOR) 77502ac6454SAndrew Thompson ural_enable_tsf_sync(sc); 77602ac6454SAndrew Thompson 77702ac6454SAndrew Thompson /* enable automatic rate adaptation */ 77802ac6454SAndrew Thompson tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)]; 77902ac6454SAndrew Thompson if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) 78002ac6454SAndrew Thompson ural_amrr_start(sc, ni); 78102ac6454SAndrew Thompson 78202ac6454SAndrew Thompson break; 78302ac6454SAndrew Thompson 78402ac6454SAndrew Thompson default: 78502ac6454SAndrew Thompson break; 78602ac6454SAndrew Thompson } 78702ac6454SAndrew Thompson 78802ac6454SAndrew Thompson RAL_UNLOCK(sc); 78902ac6454SAndrew Thompson IEEE80211_LOCK(ic); 79002ac6454SAndrew Thompson uvp->newstate(vap, sc->sc_state, sc->sc_arg); 79102ac6454SAndrew Thompson if (vap->iv_newstate_cb != NULL) 79202ac6454SAndrew Thompson vap->iv_newstate_cb(vap, sc->sc_state, sc->sc_arg); 79302ac6454SAndrew Thompson IEEE80211_UNLOCK(ic); 79402ac6454SAndrew Thompson RAL_LOCK(sc); 79502ac6454SAndrew Thompson } 79602ac6454SAndrew Thompson 79702ac6454SAndrew Thompson static void 79802ac6454SAndrew Thompson ural_scantask(struct usb2_proc_msg *pm) 79902ac6454SAndrew Thompson { 80002ac6454SAndrew Thompson struct ural_task *task = (struct ural_task *)pm; 80102ac6454SAndrew Thompson struct ural_softc *sc = task->sc; 80202ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 80302ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 80402ac6454SAndrew Thompson 80502ac6454SAndrew Thompson RAL_LOCK_ASSERT(sc, MA_OWNED); 80602ac6454SAndrew Thompson 80702ac6454SAndrew Thompson switch (sc->sc_scan_action) { 80802ac6454SAndrew Thompson case URAL_SCAN_START: 80902ac6454SAndrew Thompson /* abort TSF synchronization */ 81002ac6454SAndrew Thompson DPRINTF("starting scan\n"); 81102ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR19, 0); 81202ac6454SAndrew Thompson ural_set_bssid(sc, ifp->if_broadcastaddr); 81302ac6454SAndrew Thompson break; 81402ac6454SAndrew Thompson 81502ac6454SAndrew Thompson case URAL_SET_CHANNEL: 81602ac6454SAndrew Thompson ural_set_chan(sc, ic->ic_curchan); 81702ac6454SAndrew Thompson break; 81802ac6454SAndrew Thompson 81902ac6454SAndrew Thompson default: /* URAL_SCAN_END */ 82002ac6454SAndrew Thompson DPRINTF("stopping scan\n"); 82102ac6454SAndrew Thompson ural_enable_tsf_sync(sc); 82202ac6454SAndrew Thompson ural_set_bssid(sc, sc->sc_bssid); 82302ac6454SAndrew Thompson break; 82402ac6454SAndrew Thompson } 82502ac6454SAndrew Thompson } 82602ac6454SAndrew Thompson 82702ac6454SAndrew Thompson static int 82802ac6454SAndrew Thompson ural_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 82902ac6454SAndrew Thompson { 83002ac6454SAndrew Thompson struct ural_vap *uvp = URAL_VAP(vap); 83102ac6454SAndrew Thompson struct ieee80211com *ic = vap->iv_ic; 83202ac6454SAndrew Thompson struct ural_softc *sc = ic->ic_ifp->if_softc; 83302ac6454SAndrew Thompson 83402ac6454SAndrew Thompson DPRINTF("%s -> %s\n", 83502ac6454SAndrew Thompson ieee80211_state_name[vap->iv_state], 83602ac6454SAndrew Thompson ieee80211_state_name[nstate]); 83702ac6454SAndrew Thompson 83802ac6454SAndrew Thompson RAL_LOCK(sc); 83902ac6454SAndrew Thompson usb2_callout_stop(&uvp->amrr_ch); 84002ac6454SAndrew Thompson 84102ac6454SAndrew Thompson /* do it in a process context */ 84202ac6454SAndrew Thompson sc->sc_state = nstate; 84302ac6454SAndrew Thompson sc->sc_arg = arg; 84402ac6454SAndrew Thompson RAL_UNLOCK(sc); 84502ac6454SAndrew Thompson 84602ac6454SAndrew Thompson if (nstate == IEEE80211_S_INIT) { 84702ac6454SAndrew Thompson uvp->newstate(vap, nstate, arg); 84802ac6454SAndrew Thompson return 0; 84902ac6454SAndrew Thompson } else { 85002ac6454SAndrew Thompson RAL_LOCK(sc); 85102ac6454SAndrew Thompson ural_queue_command(sc, ural_task, &sc->sc_task[0].hdr, 85202ac6454SAndrew Thompson &sc->sc_task[1].hdr); 85302ac6454SAndrew Thompson RAL_UNLOCK(sc); 85402ac6454SAndrew Thompson return EINPROGRESS; 85502ac6454SAndrew Thompson } 85602ac6454SAndrew Thompson } 85702ac6454SAndrew Thompson 85802ac6454SAndrew Thompson 85902ac6454SAndrew Thompson static void 86002ac6454SAndrew Thompson ural_bulk_write_callback(struct usb2_xfer *xfer) 86102ac6454SAndrew Thompson { 86202ac6454SAndrew Thompson struct ural_softc *sc = xfer->priv_sc; 86302ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 86402ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 86502ac6454SAndrew Thompson struct ieee80211_channel *c = ic->ic_curchan; 86602ac6454SAndrew Thompson struct ural_tx_data *data; 86702ac6454SAndrew Thompson struct mbuf *m; 86802ac6454SAndrew Thompson unsigned int len; 86902ac6454SAndrew Thompson 87077ddf3d3SAndrew Thompson /* wakeup waiting command, if any */ 87177ddf3d3SAndrew Thompson if (sc->sc_last_task != NULL) 87277ddf3d3SAndrew Thompson cv_signal(&sc->sc_cmd_cv); 87377ddf3d3SAndrew Thompson 87402ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 87502ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 87602ac6454SAndrew Thompson DPRINTFN(11, "transfer complete, %d bytes\n", xfer->actlen); 87702ac6454SAndrew Thompson 87802ac6454SAndrew Thompson /* free resources */ 87902ac6454SAndrew Thompson data = xfer->priv_fifo; 88002ac6454SAndrew Thompson ural_tx_free(data, 0); 88102ac6454SAndrew Thompson xfer->priv_fifo = NULL; 88202ac6454SAndrew Thompson 88302ac6454SAndrew Thompson ifp->if_opackets++; 88402ac6454SAndrew Thompson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 88502ac6454SAndrew Thompson 88602ac6454SAndrew Thompson /* FALLTHROUGH */ 88702ac6454SAndrew Thompson case USB_ST_SETUP: 88802ac6454SAndrew Thompson tr_setup: 88977ddf3d3SAndrew Thompson /* wait for command to complete, if any */ 89077ddf3d3SAndrew Thompson if (sc->sc_last_task != NULL) 89177ddf3d3SAndrew Thompson break; 89277ddf3d3SAndrew Thompson 89302ac6454SAndrew Thompson data = STAILQ_FIRST(&sc->tx_q); 89402ac6454SAndrew Thompson if (data) { 89502ac6454SAndrew Thompson STAILQ_REMOVE_HEAD(&sc->tx_q, next); 89602ac6454SAndrew Thompson m = data->m; 89702ac6454SAndrew Thompson 89802ac6454SAndrew Thompson if (m->m_pkthdr.len > (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE)) { 89902ac6454SAndrew Thompson DPRINTFN(0, "data overflow, %u bytes\n", 90002ac6454SAndrew Thompson m->m_pkthdr.len); 90102ac6454SAndrew Thompson m->m_pkthdr.len = (RAL_FRAME_SIZE + RAL_TX_DESC_SIZE); 90202ac6454SAndrew Thompson } 90302ac6454SAndrew Thompson usb2_copy_in(xfer->frbuffers, 0, &data->desc, 90402ac6454SAndrew Thompson RAL_TX_DESC_SIZE); 90502ac6454SAndrew Thompson usb2_m_copy_in(xfer->frbuffers, RAL_TX_DESC_SIZE, m, 0, 90602ac6454SAndrew Thompson m->m_pkthdr.len); 90702ac6454SAndrew Thompson 90802ac6454SAndrew Thompson if (bpf_peers_present(ifp->if_bpf)) { 90902ac6454SAndrew Thompson struct ural_tx_radiotap_header *tap = &sc->sc_txtap; 91002ac6454SAndrew Thompson 91102ac6454SAndrew Thompson tap->wt_flags = 0; 91202ac6454SAndrew Thompson tap->wt_rate = data->rate; 91302ac6454SAndrew Thompson tap->wt_chan_freq = htole16(c->ic_freq); 91402ac6454SAndrew Thompson tap->wt_chan_flags = htole16(c->ic_flags); 91502ac6454SAndrew Thompson tap->wt_antenna = sc->tx_ant; 91602ac6454SAndrew Thompson 91702ac6454SAndrew Thompson bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m); 91802ac6454SAndrew Thompson } 91902ac6454SAndrew Thompson 92002ac6454SAndrew Thompson /* xfer length needs to be a multiple of two! */ 92102ac6454SAndrew Thompson len = (RAL_TX_DESC_SIZE + m->m_pkthdr.len + 1) & ~1; 92202ac6454SAndrew Thompson if ((len % 64) == 0) 92302ac6454SAndrew Thompson len += 2; 92402ac6454SAndrew Thompson 92502ac6454SAndrew Thompson DPRINTFN(11, "sending frame len=%u xferlen=%u\n", 92602ac6454SAndrew Thompson m->m_pkthdr.len, len); 92702ac6454SAndrew Thompson 92802ac6454SAndrew Thompson xfer->frlengths[0] = len; 92902ac6454SAndrew Thompson xfer->priv_fifo = data; 93002ac6454SAndrew Thompson 93102ac6454SAndrew Thompson usb2_start_hardware(xfer); 93202ac6454SAndrew Thompson } 93302ac6454SAndrew Thompson break; 93402ac6454SAndrew Thompson 93502ac6454SAndrew Thompson default: /* Error */ 93602ac6454SAndrew Thompson DPRINTFN(11, "transfer error, %s\n", 93702ac6454SAndrew Thompson usb2_errstr(xfer->error)); 93802ac6454SAndrew Thompson 93902ac6454SAndrew Thompson ifp->if_oerrors++; 94002ac6454SAndrew Thompson data = xfer->priv_fifo; 94102ac6454SAndrew Thompson if (data != NULL) { 94202ac6454SAndrew Thompson ural_tx_free(data, xfer->error); 94302ac6454SAndrew Thompson xfer->priv_fifo = NULL; 94402ac6454SAndrew Thompson } 94502ac6454SAndrew Thompson 94602ac6454SAndrew Thompson if (xfer->error == USB_ERR_STALLED) { 94702ac6454SAndrew Thompson /* try to clear stall first */ 94802ac6454SAndrew Thompson xfer->flags.stall_pipe = 1; 94902ac6454SAndrew Thompson goto tr_setup; 95002ac6454SAndrew Thompson } 95102ac6454SAndrew Thompson if (xfer->error == USB_ERR_TIMEOUT) 95202ac6454SAndrew Thompson device_printf(sc->sc_dev, "device timeout\n"); 95302ac6454SAndrew Thompson break; 95402ac6454SAndrew Thompson } 95502ac6454SAndrew Thompson } 95602ac6454SAndrew Thompson 95702ac6454SAndrew Thompson static void 95802ac6454SAndrew Thompson ural_bulk_read_callback(struct usb2_xfer *xfer) 95902ac6454SAndrew Thompson { 96002ac6454SAndrew Thompson struct ural_softc *sc = xfer->priv_sc; 96102ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 96202ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 96302ac6454SAndrew Thompson struct ieee80211_node *ni; 96402ac6454SAndrew Thompson struct mbuf *m = NULL; 96502ac6454SAndrew Thompson uint32_t flags; 96602ac6454SAndrew Thompson uint8_t rssi = 0; 96702ac6454SAndrew Thompson unsigned int len; 96802ac6454SAndrew Thompson 96902ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 97002ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 97102ac6454SAndrew Thompson 97202ac6454SAndrew Thompson DPRINTFN(15, "rx done, actlen=%d\n", xfer->actlen); 97302ac6454SAndrew Thompson 97402ac6454SAndrew Thompson len = xfer->actlen; 97502ac6454SAndrew Thompson if (len < RAL_RX_DESC_SIZE + IEEE80211_MIN_LEN) { 97602ac6454SAndrew Thompson DPRINTF("%s: xfer too short %d\n", 97702ac6454SAndrew Thompson device_get_nameunit(sc->sc_dev), len); 97802ac6454SAndrew Thompson ifp->if_ierrors++; 97902ac6454SAndrew Thompson goto tr_setup; 98002ac6454SAndrew Thompson } 98102ac6454SAndrew Thompson 98202ac6454SAndrew Thompson len -= RAL_RX_DESC_SIZE; 98302ac6454SAndrew Thompson /* rx descriptor is located at the end */ 98402ac6454SAndrew Thompson usb2_copy_out(xfer->frbuffers, len, &sc->sc_rx_desc, 98502ac6454SAndrew Thompson RAL_RX_DESC_SIZE); 98602ac6454SAndrew Thompson 98702ac6454SAndrew Thompson rssi = URAL_RSSI(sc->sc_rx_desc.rssi); 98802ac6454SAndrew Thompson flags = le32toh(sc->sc_rx_desc.flags); 98902ac6454SAndrew Thompson if (flags & (RAL_RX_PHY_ERROR | RAL_RX_CRC_ERROR)) { 99002ac6454SAndrew Thompson /* 99102ac6454SAndrew Thompson * This should not happen since we did not 99202ac6454SAndrew Thompson * request to receive those frames when we 99302ac6454SAndrew Thompson * filled RAL_TXRX_CSR2: 99402ac6454SAndrew Thompson */ 99502ac6454SAndrew Thompson DPRINTFN(5, "PHY or CRC error\n"); 99602ac6454SAndrew Thompson ifp->if_ierrors++; 99702ac6454SAndrew Thompson goto tr_setup; 99802ac6454SAndrew Thompson } 99902ac6454SAndrew Thompson 100002ac6454SAndrew Thompson m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 100102ac6454SAndrew Thompson if (m == NULL) { 100202ac6454SAndrew Thompson DPRINTF("could not allocate mbuf\n"); 100302ac6454SAndrew Thompson ifp->if_ierrors++; 100402ac6454SAndrew Thompson goto tr_setup; 100502ac6454SAndrew Thompson } 100602ac6454SAndrew Thompson usb2_copy_out(xfer->frbuffers, 0, mtod(m, uint8_t *), len); 100702ac6454SAndrew Thompson 100802ac6454SAndrew Thompson /* finalize mbuf */ 100902ac6454SAndrew Thompson m->m_pkthdr.rcvif = ifp; 101002ac6454SAndrew Thompson m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff; 101102ac6454SAndrew Thompson 101202ac6454SAndrew Thompson if (bpf_peers_present(ifp->if_bpf)) { 101302ac6454SAndrew Thompson struct ural_rx_radiotap_header *tap = &sc->sc_rxtap; 101402ac6454SAndrew Thompson 101502ac6454SAndrew Thompson tap->wr_flags = IEEE80211_RADIOTAP_F_FCS; 101602ac6454SAndrew Thompson tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate, 101702ac6454SAndrew Thompson (flags & RAL_RX_OFDM) ? 101802ac6454SAndrew Thompson IEEE80211_T_OFDM : IEEE80211_T_CCK); 101902ac6454SAndrew Thompson tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq); 102002ac6454SAndrew Thompson tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags); 102102ac6454SAndrew Thompson tap->wr_antenna = sc->rx_ant; 102202ac6454SAndrew Thompson tap->wr_antsignal = rssi; 102302ac6454SAndrew Thompson 102402ac6454SAndrew Thompson bpf_mtap2(ifp->if_bpf, tap, sc->sc_rxtap_len, m); 102502ac6454SAndrew Thompson } 102602ac6454SAndrew Thompson /* Strip trailing 802.11 MAC FCS. */ 102702ac6454SAndrew Thompson m_adj(m, -IEEE80211_CRC_LEN); 102802ac6454SAndrew Thompson 102902ac6454SAndrew Thompson /* FALLTHROUGH */ 103002ac6454SAndrew Thompson case USB_ST_SETUP: 103102ac6454SAndrew Thompson tr_setup: 103202ac6454SAndrew Thompson xfer->frlengths[0] = xfer->max_data_length; 103302ac6454SAndrew Thompson usb2_start_hardware(xfer); 103402ac6454SAndrew Thompson 103502ac6454SAndrew Thompson /* 103602ac6454SAndrew Thompson * At the end of a USB callback it is always safe to unlock 103702ac6454SAndrew Thompson * the private mutex of a device! That is why we do the 103802ac6454SAndrew Thompson * "ieee80211_input" here, and not some lines up! 103902ac6454SAndrew Thompson */ 104002ac6454SAndrew Thompson if (m) { 104102ac6454SAndrew Thompson RAL_UNLOCK(sc); 104202ac6454SAndrew Thompson ni = ieee80211_find_rxnode(ic, 104302ac6454SAndrew Thompson mtod(m, struct ieee80211_frame_min *)); 104402ac6454SAndrew Thompson if (ni != NULL) { 104502ac6454SAndrew Thompson (void) ieee80211_input(ni, m, rssi, 104602ac6454SAndrew Thompson RAL_NOISE_FLOOR, 0); 104702ac6454SAndrew Thompson ieee80211_free_node(ni); 104802ac6454SAndrew Thompson } else 104902ac6454SAndrew Thompson (void) ieee80211_input_all(ic, m, rssi, 105002ac6454SAndrew Thompson RAL_NOISE_FLOOR, 0); 105102ac6454SAndrew Thompson RAL_LOCK(sc); 105202ac6454SAndrew Thompson } 105302ac6454SAndrew Thompson return; 105402ac6454SAndrew Thompson 105502ac6454SAndrew Thompson default: /* Error */ 105602ac6454SAndrew Thompson if (xfer->error != USB_ERR_CANCELLED) { 105702ac6454SAndrew Thompson /* try to clear stall first */ 105802ac6454SAndrew Thompson xfer->flags.stall_pipe = 1; 105902ac6454SAndrew Thompson goto tr_setup; 106002ac6454SAndrew Thompson } 106102ac6454SAndrew Thompson return; 106202ac6454SAndrew Thompson } 106302ac6454SAndrew Thompson } 106402ac6454SAndrew Thompson 106502ac6454SAndrew Thompson static uint8_t 106602ac6454SAndrew Thompson ural_plcp_signal(int rate) 106702ac6454SAndrew Thompson { 106802ac6454SAndrew Thompson switch (rate) { 106902ac6454SAndrew Thompson /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 107002ac6454SAndrew Thompson case 12: return 0xb; 107102ac6454SAndrew Thompson case 18: return 0xf; 107202ac6454SAndrew Thompson case 24: return 0xa; 107302ac6454SAndrew Thompson case 36: return 0xe; 107402ac6454SAndrew Thompson case 48: return 0x9; 107502ac6454SAndrew Thompson case 72: return 0xd; 107602ac6454SAndrew Thompson case 96: return 0x8; 107702ac6454SAndrew Thompson case 108: return 0xc; 107802ac6454SAndrew Thompson 107902ac6454SAndrew Thompson /* CCK rates (NB: not IEEE std, device-specific) */ 108002ac6454SAndrew Thompson case 2: return 0x0; 108102ac6454SAndrew Thompson case 4: return 0x1; 108202ac6454SAndrew Thompson case 11: return 0x2; 108302ac6454SAndrew Thompson case 22: return 0x3; 108402ac6454SAndrew Thompson } 108502ac6454SAndrew Thompson return 0xff; /* XXX unsupported/unknown rate */ 108602ac6454SAndrew Thompson } 108702ac6454SAndrew Thompson 108802ac6454SAndrew Thompson static void 108902ac6454SAndrew Thompson ural_setup_tx_desc(struct ural_softc *sc, struct ural_tx_desc *desc, 109002ac6454SAndrew Thompson uint32_t flags, int len, int rate) 109102ac6454SAndrew Thompson { 109202ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 109302ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 109402ac6454SAndrew Thompson uint16_t plcp_length; 109502ac6454SAndrew Thompson int remainder; 109602ac6454SAndrew Thompson 109702ac6454SAndrew Thompson desc->flags = htole32(flags); 109802ac6454SAndrew Thompson desc->flags |= htole32(RAL_TX_NEWSEQ); 109902ac6454SAndrew Thompson desc->flags |= htole32(len << 16); 110002ac6454SAndrew Thompson 110102ac6454SAndrew Thompson desc->wme = htole16(RAL_AIFSN(2) | RAL_LOGCWMIN(3) | RAL_LOGCWMAX(5)); 110202ac6454SAndrew Thompson desc->wme |= htole16(RAL_IVOFFSET(sizeof (struct ieee80211_frame))); 110302ac6454SAndrew Thompson 110402ac6454SAndrew Thompson /* setup PLCP fields */ 110502ac6454SAndrew Thompson desc->plcp_signal = ural_plcp_signal(rate); 110602ac6454SAndrew Thompson desc->plcp_service = 4; 110702ac6454SAndrew Thompson 110802ac6454SAndrew Thompson len += IEEE80211_CRC_LEN; 110926d39e2cSSam Leffler if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) { 111002ac6454SAndrew Thompson desc->flags |= htole32(RAL_TX_OFDM); 111102ac6454SAndrew Thompson 111202ac6454SAndrew Thompson plcp_length = len & 0xfff; 111302ac6454SAndrew Thompson desc->plcp_length_hi = plcp_length >> 6; 111402ac6454SAndrew Thompson desc->plcp_length_lo = plcp_length & 0x3f; 111502ac6454SAndrew Thompson } else { 111602ac6454SAndrew Thompson plcp_length = (16 * len + rate - 1) / rate; 111702ac6454SAndrew Thompson if (rate == 22) { 111802ac6454SAndrew Thompson remainder = (16 * len) % 22; 111902ac6454SAndrew Thompson if (remainder != 0 && remainder < 7) 112002ac6454SAndrew Thompson desc->plcp_service |= RAL_PLCP_LENGEXT; 112102ac6454SAndrew Thompson } 112202ac6454SAndrew Thompson desc->plcp_length_hi = plcp_length >> 8; 112302ac6454SAndrew Thompson desc->plcp_length_lo = plcp_length & 0xff; 112402ac6454SAndrew Thompson 112502ac6454SAndrew Thompson if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 112602ac6454SAndrew Thompson desc->plcp_signal |= 0x08; 112702ac6454SAndrew Thompson } 112802ac6454SAndrew Thompson 112902ac6454SAndrew Thompson desc->iv = 0; 113002ac6454SAndrew Thompson desc->eiv = 0; 113102ac6454SAndrew Thompson } 113202ac6454SAndrew Thompson 113302ac6454SAndrew Thompson #define RAL_TX_TIMEOUT 5000 113402ac6454SAndrew Thompson 113502ac6454SAndrew Thompson static int 113602ac6454SAndrew Thompson ural_tx_bcn(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 113702ac6454SAndrew Thompson { 113802ac6454SAndrew Thompson struct ieee80211vap *vap = ni->ni_vap; 113902ac6454SAndrew Thompson struct ieee80211com *ic = ni->ni_ic; 114002ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 114102ac6454SAndrew Thompson const struct ieee80211_txparam *tp; 114202ac6454SAndrew Thompson struct ural_tx_data *data; 114302ac6454SAndrew Thompson 114402ac6454SAndrew Thompson if (sc->tx_nfree == 0) { 114502ac6454SAndrew Thompson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 114602ac6454SAndrew Thompson m_freem(m0); 114702ac6454SAndrew Thompson ieee80211_free_node(ni); 114802ac6454SAndrew Thompson return EIO; 114902ac6454SAndrew Thompson } 115002ac6454SAndrew Thompson data = STAILQ_FIRST(&sc->tx_free); 115102ac6454SAndrew Thompson STAILQ_REMOVE_HEAD(&sc->tx_free, next); 115202ac6454SAndrew Thompson sc->tx_nfree--; 115302ac6454SAndrew Thompson tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)]; 115402ac6454SAndrew Thompson 115502ac6454SAndrew Thompson data->m = m0; 115602ac6454SAndrew Thompson data->ni = ni; 115702ac6454SAndrew Thompson data->rate = tp->mgmtrate; 115802ac6454SAndrew Thompson 115902ac6454SAndrew Thompson ural_setup_tx_desc(sc, &data->desc, 116002ac6454SAndrew Thompson RAL_TX_IFS_NEWBACKOFF | RAL_TX_TIMESTAMP, m0->m_pkthdr.len, 116102ac6454SAndrew Thompson tp->mgmtrate); 116202ac6454SAndrew Thompson 116302ac6454SAndrew Thompson DPRINTFN(10, "sending beacon frame len=%u rate=%u\n", 116402ac6454SAndrew Thompson m0->m_pkthdr.len, tp->mgmtrate); 116502ac6454SAndrew Thompson 116602ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 116702ac6454SAndrew Thompson usb2_transfer_start(sc->sc_xfer[URAL_BULK_WR]); 116802ac6454SAndrew Thompson 116902ac6454SAndrew Thompson return (0); 117002ac6454SAndrew Thompson } 117102ac6454SAndrew Thompson 117202ac6454SAndrew Thompson static int 117302ac6454SAndrew Thompson ural_tx_mgt(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 117402ac6454SAndrew Thompson { 117502ac6454SAndrew Thompson struct ieee80211vap *vap = ni->ni_vap; 117602ac6454SAndrew Thompson struct ieee80211com *ic = ni->ni_ic; 117702ac6454SAndrew Thompson const struct ieee80211_txparam *tp; 117802ac6454SAndrew Thompson struct ural_tx_data *data; 117902ac6454SAndrew Thompson struct ieee80211_frame *wh; 118002ac6454SAndrew Thompson struct ieee80211_key *k; 118102ac6454SAndrew Thompson uint32_t flags; 118202ac6454SAndrew Thompson uint16_t dur; 118302ac6454SAndrew Thompson 118402ac6454SAndrew Thompson RAL_LOCK_ASSERT(sc, MA_OWNED); 118502ac6454SAndrew Thompson 118602ac6454SAndrew Thompson data = STAILQ_FIRST(&sc->tx_free); 118702ac6454SAndrew Thompson STAILQ_REMOVE_HEAD(&sc->tx_free, next); 118802ac6454SAndrew Thompson sc->tx_nfree--; 118902ac6454SAndrew Thompson 119002ac6454SAndrew Thompson tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 119102ac6454SAndrew Thompson 119202ac6454SAndrew Thompson wh = mtod(m0, struct ieee80211_frame *); 119302ac6454SAndrew Thompson if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 119402ac6454SAndrew Thompson k = ieee80211_crypto_encap(ni, m0); 119502ac6454SAndrew Thompson if (k == NULL) { 119602ac6454SAndrew Thompson m_freem(m0); 119702ac6454SAndrew Thompson return ENOBUFS; 119802ac6454SAndrew Thompson } 119902ac6454SAndrew Thompson wh = mtod(m0, struct ieee80211_frame *); 120002ac6454SAndrew Thompson } 120102ac6454SAndrew Thompson 120202ac6454SAndrew Thompson data->m = m0; 120302ac6454SAndrew Thompson data->ni = ni; 120402ac6454SAndrew Thompson data->rate = tp->mgmtrate; 120502ac6454SAndrew Thompson 120602ac6454SAndrew Thompson flags = 0; 120702ac6454SAndrew Thompson if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 120802ac6454SAndrew Thompson flags |= RAL_TX_ACK; 120902ac6454SAndrew Thompson 121026d39e2cSSam Leffler dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate, 121102ac6454SAndrew Thompson ic->ic_flags & IEEE80211_F_SHPREAMBLE); 121202ac6454SAndrew Thompson *(uint16_t *)wh->i_dur = htole16(dur); 121302ac6454SAndrew Thompson 121402ac6454SAndrew Thompson /* tell hardware to add timestamp for probe responses */ 121502ac6454SAndrew Thompson if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 121602ac6454SAndrew Thompson IEEE80211_FC0_TYPE_MGT && 121702ac6454SAndrew Thompson (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 121802ac6454SAndrew Thompson IEEE80211_FC0_SUBTYPE_PROBE_RESP) 121902ac6454SAndrew Thompson flags |= RAL_TX_TIMESTAMP; 122002ac6454SAndrew Thompson } 122102ac6454SAndrew Thompson 122202ac6454SAndrew Thompson ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, tp->mgmtrate); 122302ac6454SAndrew Thompson 122402ac6454SAndrew Thompson DPRINTFN(10, "sending mgt frame len=%u rate=%u\n", 122502ac6454SAndrew Thompson m0->m_pkthdr.len, tp->mgmtrate); 122602ac6454SAndrew Thompson 122702ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 122802ac6454SAndrew Thompson usb2_transfer_start(sc->sc_xfer[URAL_BULK_WR]); 122902ac6454SAndrew Thompson 123002ac6454SAndrew Thompson return 0; 123102ac6454SAndrew Thompson } 123202ac6454SAndrew Thompson 123302ac6454SAndrew Thompson static int 123402ac6454SAndrew Thompson ural_sendprot(struct ural_softc *sc, 123502ac6454SAndrew Thompson const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate) 123602ac6454SAndrew Thompson { 123702ac6454SAndrew Thompson struct ieee80211com *ic = ni->ni_ic; 123802ac6454SAndrew Thompson const struct ieee80211_frame *wh; 123902ac6454SAndrew Thompson struct ural_tx_data *data; 124002ac6454SAndrew Thompson struct mbuf *mprot; 124102ac6454SAndrew Thompson int protrate, ackrate, pktlen, flags, isshort; 124202ac6454SAndrew Thompson uint16_t dur; 124302ac6454SAndrew Thompson 124402ac6454SAndrew Thompson KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY, 124502ac6454SAndrew Thompson ("protection %d", prot)); 124602ac6454SAndrew Thompson 124702ac6454SAndrew Thompson wh = mtod(m, const struct ieee80211_frame *); 124802ac6454SAndrew Thompson pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 124902ac6454SAndrew Thompson 125026d39e2cSSam Leffler protrate = ieee80211_ctl_rate(ic->ic_rt, rate); 125126d39e2cSSam Leffler ackrate = ieee80211_ack_rate(ic->ic_rt, rate); 125202ac6454SAndrew Thompson 125302ac6454SAndrew Thompson isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0; 125426d39e2cSSam Leffler dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort); 125526d39e2cSSam Leffler + ieee80211_ack_duration(ic->ic_rt, rate, isshort); 125602ac6454SAndrew Thompson flags = RAL_TX_RETRY(7); 125702ac6454SAndrew Thompson if (prot == IEEE80211_PROT_RTSCTS) { 125802ac6454SAndrew Thompson /* NB: CTS is the same size as an ACK */ 125926d39e2cSSam Leffler dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort); 126002ac6454SAndrew Thompson flags |= RAL_TX_ACK; 126102ac6454SAndrew Thompson mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur); 126202ac6454SAndrew Thompson } else { 126302ac6454SAndrew Thompson mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur); 126402ac6454SAndrew Thompson } 126502ac6454SAndrew Thompson if (mprot == NULL) { 126602ac6454SAndrew Thompson /* XXX stat + msg */ 126702ac6454SAndrew Thompson return ENOBUFS; 126802ac6454SAndrew Thompson } 126902ac6454SAndrew Thompson data = STAILQ_FIRST(&sc->tx_free); 127002ac6454SAndrew Thompson STAILQ_REMOVE_HEAD(&sc->tx_free, next); 127102ac6454SAndrew Thompson sc->tx_nfree--; 127202ac6454SAndrew Thompson 127302ac6454SAndrew Thompson data->m = mprot; 127402ac6454SAndrew Thompson data->ni = ieee80211_ref_node(ni); 127502ac6454SAndrew Thompson data->rate = protrate; 127602ac6454SAndrew Thompson ural_setup_tx_desc(sc, &data->desc, flags, mprot->m_pkthdr.len, protrate); 127702ac6454SAndrew Thompson 127802ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 127902ac6454SAndrew Thompson usb2_transfer_start(sc->sc_xfer[URAL_BULK_WR]); 128002ac6454SAndrew Thompson 128102ac6454SAndrew Thompson return 0; 128202ac6454SAndrew Thompson } 128302ac6454SAndrew Thompson 128402ac6454SAndrew Thompson static int 128502ac6454SAndrew Thompson ural_tx_raw(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni, 128602ac6454SAndrew Thompson const struct ieee80211_bpf_params *params) 128702ac6454SAndrew Thompson { 128802ac6454SAndrew Thompson struct ural_tx_data *data; 128902ac6454SAndrew Thompson uint32_t flags; 129002ac6454SAndrew Thompson int error; 129102ac6454SAndrew Thompson int rate; 129202ac6454SAndrew Thompson 129302ac6454SAndrew Thompson RAL_LOCK_ASSERT(sc, MA_OWNED); 129402ac6454SAndrew Thompson KASSERT(params != NULL, ("no raw xmit params")); 129502ac6454SAndrew Thompson 129602ac6454SAndrew Thompson rate = params->ibp_rate0 & IEEE80211_RATE_VAL; 129702ac6454SAndrew Thompson /* XXX validate */ 129802ac6454SAndrew Thompson if (rate == 0) { 129902ac6454SAndrew Thompson m_freem(m0); 130002ac6454SAndrew Thompson return EINVAL; 130102ac6454SAndrew Thompson } 130202ac6454SAndrew Thompson flags = 0; 130302ac6454SAndrew Thompson if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 130402ac6454SAndrew Thompson flags |= RAL_TX_ACK; 130502ac6454SAndrew Thompson if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) { 130602ac6454SAndrew Thompson error = ural_sendprot(sc, m0, ni, 130702ac6454SAndrew Thompson params->ibp_flags & IEEE80211_BPF_RTS ? 130802ac6454SAndrew Thompson IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY, 130902ac6454SAndrew Thompson rate); 131096ca458fSAndrew Thompson if (error || sc->tx_nfree == 0) { 131102ac6454SAndrew Thompson m_freem(m0); 131296ca458fSAndrew Thompson return ENOBUFS; 131302ac6454SAndrew Thompson } 131402ac6454SAndrew Thompson flags |= RAL_TX_IFS_SIFS; 131502ac6454SAndrew Thompson } 131602ac6454SAndrew Thompson 131796ca458fSAndrew Thompson data = STAILQ_FIRST(&sc->tx_free); 131896ca458fSAndrew Thompson STAILQ_REMOVE_HEAD(&sc->tx_free, next); 131996ca458fSAndrew Thompson sc->tx_nfree--; 132096ca458fSAndrew Thompson 132102ac6454SAndrew Thompson data->m = m0; 132202ac6454SAndrew Thompson data->ni = ni; 132302ac6454SAndrew Thompson data->rate = rate; 132402ac6454SAndrew Thompson 132502ac6454SAndrew Thompson /* XXX need to setup descriptor ourself */ 132602ac6454SAndrew Thompson ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, rate); 132702ac6454SAndrew Thompson 132802ac6454SAndrew Thompson DPRINTFN(10, "sending raw frame len=%u rate=%u\n", 132902ac6454SAndrew Thompson m0->m_pkthdr.len, rate); 133002ac6454SAndrew Thompson 133102ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 133202ac6454SAndrew Thompson usb2_transfer_start(sc->sc_xfer[URAL_BULK_WR]); 133302ac6454SAndrew Thompson 133402ac6454SAndrew Thompson return 0; 133502ac6454SAndrew Thompson } 133602ac6454SAndrew Thompson 133702ac6454SAndrew Thompson static int 133802ac6454SAndrew Thompson ural_tx_data(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 133902ac6454SAndrew Thompson { 134002ac6454SAndrew Thompson struct ieee80211vap *vap = ni->ni_vap; 134102ac6454SAndrew Thompson struct ieee80211com *ic = ni->ni_ic; 134202ac6454SAndrew Thompson struct ural_tx_data *data; 134302ac6454SAndrew Thompson struct ieee80211_frame *wh; 134402ac6454SAndrew Thompson const struct ieee80211_txparam *tp; 134502ac6454SAndrew Thompson struct ieee80211_key *k; 134602ac6454SAndrew Thompson uint32_t flags = 0; 134702ac6454SAndrew Thompson uint16_t dur; 134802ac6454SAndrew Thompson int error, rate; 134902ac6454SAndrew Thompson 135002ac6454SAndrew Thompson RAL_LOCK_ASSERT(sc, MA_OWNED); 135102ac6454SAndrew Thompson 135202ac6454SAndrew Thompson wh = mtod(m0, struct ieee80211_frame *); 135302ac6454SAndrew Thompson 135402ac6454SAndrew Thompson tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)]; 135502ac6454SAndrew Thompson if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 135602ac6454SAndrew Thompson rate = tp->mcastrate; 135702ac6454SAndrew Thompson else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 135802ac6454SAndrew Thompson rate = tp->ucastrate; 135902ac6454SAndrew Thompson else 136002ac6454SAndrew Thompson rate = ni->ni_txrate; 136102ac6454SAndrew Thompson 136202ac6454SAndrew Thompson if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 136302ac6454SAndrew Thompson k = ieee80211_crypto_encap(ni, m0); 136402ac6454SAndrew Thompson if (k == NULL) { 136502ac6454SAndrew Thompson m_freem(m0); 136602ac6454SAndrew Thompson return ENOBUFS; 136702ac6454SAndrew Thompson } 136802ac6454SAndrew Thompson /* packet header may have moved, reset our local pointer */ 136902ac6454SAndrew Thompson wh = mtod(m0, struct ieee80211_frame *); 137002ac6454SAndrew Thompson } 137102ac6454SAndrew Thompson 137202ac6454SAndrew Thompson if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 137302ac6454SAndrew Thompson int prot = IEEE80211_PROT_NONE; 137402ac6454SAndrew Thompson if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) 137502ac6454SAndrew Thompson prot = IEEE80211_PROT_RTSCTS; 137602ac6454SAndrew Thompson else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 137726d39e2cSSam Leffler ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) 137802ac6454SAndrew Thompson prot = ic->ic_protmode; 137902ac6454SAndrew Thompson if (prot != IEEE80211_PROT_NONE) { 138002ac6454SAndrew Thompson error = ural_sendprot(sc, m0, ni, prot, rate); 138196ca458fSAndrew Thompson if (error || sc->tx_nfree == 0) { 138202ac6454SAndrew Thompson m_freem(m0); 138396ca458fSAndrew Thompson return ENOBUFS; 138402ac6454SAndrew Thompson } 138502ac6454SAndrew Thompson flags |= RAL_TX_IFS_SIFS; 138602ac6454SAndrew Thompson } 138702ac6454SAndrew Thompson } 138802ac6454SAndrew Thompson 138902ac6454SAndrew Thompson data = STAILQ_FIRST(&sc->tx_free); 139002ac6454SAndrew Thompson STAILQ_REMOVE_HEAD(&sc->tx_free, next); 139102ac6454SAndrew Thompson sc->tx_nfree--; 139202ac6454SAndrew Thompson 139302ac6454SAndrew Thompson data->m = m0; 139402ac6454SAndrew Thompson data->ni = ni; 139502ac6454SAndrew Thompson data->rate = rate; 139602ac6454SAndrew Thompson 139702ac6454SAndrew Thompson if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 139802ac6454SAndrew Thompson flags |= RAL_TX_ACK; 139902ac6454SAndrew Thompson flags |= RAL_TX_RETRY(7); 140002ac6454SAndrew Thompson 140126d39e2cSSam Leffler dur = ieee80211_ack_duration(ic->ic_rt, rate, 140202ac6454SAndrew Thompson ic->ic_flags & IEEE80211_F_SHPREAMBLE); 140302ac6454SAndrew Thompson *(uint16_t *)wh->i_dur = htole16(dur); 140402ac6454SAndrew Thompson } 140502ac6454SAndrew Thompson 140602ac6454SAndrew Thompson ural_setup_tx_desc(sc, &data->desc, flags, m0->m_pkthdr.len, rate); 140702ac6454SAndrew Thompson 140802ac6454SAndrew Thompson DPRINTFN(10, "sending data frame len=%u rate=%u\n", 140902ac6454SAndrew Thompson m0->m_pkthdr.len, rate); 141002ac6454SAndrew Thompson 141102ac6454SAndrew Thompson STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 141202ac6454SAndrew Thompson usb2_transfer_start(sc->sc_xfer[URAL_BULK_WR]); 141302ac6454SAndrew Thompson 141402ac6454SAndrew Thompson return 0; 141502ac6454SAndrew Thompson } 141602ac6454SAndrew Thompson 141702ac6454SAndrew Thompson static void 141802ac6454SAndrew Thompson ural_start(struct ifnet *ifp) 141902ac6454SAndrew Thompson { 142002ac6454SAndrew Thompson struct ural_softc *sc = ifp->if_softc; 142102ac6454SAndrew Thompson struct ieee80211_node *ni; 142202ac6454SAndrew Thompson struct mbuf *m; 142302ac6454SAndrew Thompson 142402ac6454SAndrew Thompson RAL_LOCK(sc); 142502ac6454SAndrew Thompson if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 142602ac6454SAndrew Thompson RAL_UNLOCK(sc); 142702ac6454SAndrew Thompson return; 142802ac6454SAndrew Thompson } 142902ac6454SAndrew Thompson for (;;) { 143002ac6454SAndrew Thompson IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 143102ac6454SAndrew Thompson if (m == NULL) 143202ac6454SAndrew Thompson break; 143396ca458fSAndrew Thompson if (sc->tx_nfree < RAL_TX_MINFREE) { 143402ac6454SAndrew Thompson IFQ_DRV_PREPEND(&ifp->if_snd, m); 143502ac6454SAndrew Thompson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 143602ac6454SAndrew Thompson break; 143702ac6454SAndrew Thompson } 143802ac6454SAndrew Thompson ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 143902ac6454SAndrew Thompson m = ieee80211_encap(ni, m); 144002ac6454SAndrew Thompson if (m == NULL) { 144102ac6454SAndrew Thompson ieee80211_free_node(ni); 144277ddf3d3SAndrew Thompson ifp->if_oerrors++; 144302ac6454SAndrew Thompson continue; 144402ac6454SAndrew Thompson } 144502ac6454SAndrew Thompson if (ural_tx_data(sc, m, ni) != 0) { 144602ac6454SAndrew Thompson ieee80211_free_node(ni); 144702ac6454SAndrew Thompson ifp->if_oerrors++; 144802ac6454SAndrew Thompson break; 144902ac6454SAndrew Thompson } 145002ac6454SAndrew Thompson } 145102ac6454SAndrew Thompson RAL_UNLOCK(sc); 145202ac6454SAndrew Thompson } 145302ac6454SAndrew Thompson 145402ac6454SAndrew Thompson static int 145502ac6454SAndrew Thompson ural_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 145602ac6454SAndrew Thompson { 145702ac6454SAndrew Thompson struct ural_softc *sc = ifp->if_softc; 145802ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 145902ac6454SAndrew Thompson struct ifreq *ifr = (struct ifreq *) data; 146002ac6454SAndrew Thompson int error = 0, startall = 0; 146102ac6454SAndrew Thompson 146202ac6454SAndrew Thompson switch (cmd) { 146302ac6454SAndrew Thompson case SIOCSIFFLAGS: 146402ac6454SAndrew Thompson RAL_LOCK(sc); 146502ac6454SAndrew Thompson if (ifp->if_flags & IFF_UP) { 146602ac6454SAndrew Thompson if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 146702ac6454SAndrew Thompson ural_queue_command(sc, ural_init_task, 146802ac6454SAndrew Thompson &sc->sc_synctask[0].hdr, 146902ac6454SAndrew Thompson &sc->sc_synctask[1].hdr); 147002ac6454SAndrew Thompson startall = 1; 147102ac6454SAndrew Thompson } else 147202ac6454SAndrew Thompson ural_queue_command(sc, ural_promisctask, 147302ac6454SAndrew Thompson &sc->sc_promisctask[0].hdr, 147402ac6454SAndrew Thompson &sc->sc_promisctask[1].hdr); 147502ac6454SAndrew Thompson } else { 147602ac6454SAndrew Thompson if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 147702ac6454SAndrew Thompson ural_queue_command(sc, ural_stop_task, 147802ac6454SAndrew Thompson &sc->sc_synctask[0].hdr, 147902ac6454SAndrew Thompson &sc->sc_synctask[1].hdr); 148002ac6454SAndrew Thompson } 148102ac6454SAndrew Thompson } 148202ac6454SAndrew Thompson RAL_UNLOCK(sc); 148302ac6454SAndrew Thompson if (startall) 148402ac6454SAndrew Thompson ieee80211_start_all(ic); 148502ac6454SAndrew Thompson break; 148602ac6454SAndrew Thompson case SIOCGIFMEDIA: 148702ac6454SAndrew Thompson case SIOCSIFMEDIA: 148802ac6454SAndrew Thompson error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 148902ac6454SAndrew Thompson break; 149002ac6454SAndrew Thompson default: 149102ac6454SAndrew Thompson error = ether_ioctl(ifp, cmd, data); 149202ac6454SAndrew Thompson break; 149302ac6454SAndrew Thompson } 149402ac6454SAndrew Thompson return error; 149502ac6454SAndrew Thompson } 149602ac6454SAndrew Thompson 149702ac6454SAndrew Thompson static void 149802ac6454SAndrew Thompson ural_set_testmode(struct ural_softc *sc) 149902ac6454SAndrew Thompson { 150002ac6454SAndrew Thompson struct usb2_device_request req; 150102ac6454SAndrew Thompson usb2_error_t error; 150202ac6454SAndrew Thompson 150302ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 150402ac6454SAndrew Thompson req.bRequest = RAL_VENDOR_REQUEST; 150502ac6454SAndrew Thompson USETW(req.wValue, 4); 150602ac6454SAndrew Thompson USETW(req.wIndex, 1); 150702ac6454SAndrew Thompson USETW(req.wLength, 0); 150802ac6454SAndrew Thompson 150902ac6454SAndrew Thompson error = ural_do_request(sc, &req, NULL); 151002ac6454SAndrew Thompson if (error != 0) { 151102ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not set test mode: %s\n", 151202ac6454SAndrew Thompson usb2_errstr(error)); 151302ac6454SAndrew Thompson } 151402ac6454SAndrew Thompson } 151502ac6454SAndrew Thompson 151602ac6454SAndrew Thompson static void 151702ac6454SAndrew Thompson ural_eeprom_read(struct ural_softc *sc, uint16_t addr, void *buf, int len) 151802ac6454SAndrew Thompson { 151902ac6454SAndrew Thompson struct usb2_device_request req; 152002ac6454SAndrew Thompson usb2_error_t error; 152102ac6454SAndrew Thompson 152202ac6454SAndrew Thompson req.bmRequestType = UT_READ_VENDOR_DEVICE; 152302ac6454SAndrew Thompson req.bRequest = RAL_READ_EEPROM; 152402ac6454SAndrew Thompson USETW(req.wValue, 0); 152502ac6454SAndrew Thompson USETW(req.wIndex, addr); 152602ac6454SAndrew Thompson USETW(req.wLength, len); 152702ac6454SAndrew Thompson 152802ac6454SAndrew Thompson error = ural_do_request(sc, &req, buf); 152902ac6454SAndrew Thompson if (error != 0) { 153002ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not read EEPROM: %s\n", 153102ac6454SAndrew Thompson usb2_errstr(error)); 153202ac6454SAndrew Thompson } 153302ac6454SAndrew Thompson } 153402ac6454SAndrew Thompson 153502ac6454SAndrew Thompson static uint16_t 153602ac6454SAndrew Thompson ural_read(struct ural_softc *sc, uint16_t reg) 153702ac6454SAndrew Thompson { 153802ac6454SAndrew Thompson struct usb2_device_request req; 153902ac6454SAndrew Thompson usb2_error_t error; 154002ac6454SAndrew Thompson uint16_t val; 154102ac6454SAndrew Thompson 154202ac6454SAndrew Thompson req.bmRequestType = UT_READ_VENDOR_DEVICE; 154302ac6454SAndrew Thompson req.bRequest = RAL_READ_MAC; 154402ac6454SAndrew Thompson USETW(req.wValue, 0); 154502ac6454SAndrew Thompson USETW(req.wIndex, reg); 154602ac6454SAndrew Thompson USETW(req.wLength, sizeof (uint16_t)); 154702ac6454SAndrew Thompson 154802ac6454SAndrew Thompson error = ural_do_request(sc, &req, &val); 154902ac6454SAndrew Thompson if (error != 0) { 155002ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not read MAC register: %s\n", 155102ac6454SAndrew Thompson usb2_errstr(error)); 155202ac6454SAndrew Thompson return 0; 155302ac6454SAndrew Thompson } 155402ac6454SAndrew Thompson 155502ac6454SAndrew Thompson return le16toh(val); 155602ac6454SAndrew Thompson } 155702ac6454SAndrew Thompson 155802ac6454SAndrew Thompson static void 155902ac6454SAndrew Thompson ural_read_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len) 156002ac6454SAndrew Thompson { 156102ac6454SAndrew Thompson struct usb2_device_request req; 156202ac6454SAndrew Thompson usb2_error_t error; 156302ac6454SAndrew Thompson 156402ac6454SAndrew Thompson req.bmRequestType = UT_READ_VENDOR_DEVICE; 156502ac6454SAndrew Thompson req.bRequest = RAL_READ_MULTI_MAC; 156602ac6454SAndrew Thompson USETW(req.wValue, 0); 156702ac6454SAndrew Thompson USETW(req.wIndex, reg); 156802ac6454SAndrew Thompson USETW(req.wLength, len); 156902ac6454SAndrew Thompson 157002ac6454SAndrew Thompson error = ural_do_request(sc, &req, buf); 157102ac6454SAndrew Thompson if (error != 0) { 157202ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not read MAC register: %s\n", 157302ac6454SAndrew Thompson usb2_errstr(error)); 157402ac6454SAndrew Thompson } 157502ac6454SAndrew Thompson } 157602ac6454SAndrew Thompson 157702ac6454SAndrew Thompson static void 157802ac6454SAndrew Thompson ural_write(struct ural_softc *sc, uint16_t reg, uint16_t val) 157902ac6454SAndrew Thompson { 158002ac6454SAndrew Thompson struct usb2_device_request req; 158102ac6454SAndrew Thompson usb2_error_t error; 158202ac6454SAndrew Thompson 158302ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 158402ac6454SAndrew Thompson req.bRequest = RAL_WRITE_MAC; 158502ac6454SAndrew Thompson USETW(req.wValue, val); 158602ac6454SAndrew Thompson USETW(req.wIndex, reg); 158702ac6454SAndrew Thompson USETW(req.wLength, 0); 158802ac6454SAndrew Thompson 158902ac6454SAndrew Thompson error = ural_do_request(sc, &req, NULL); 159002ac6454SAndrew Thompson if (error != 0) { 159102ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not write MAC register: %s\n", 159202ac6454SAndrew Thompson usb2_errstr(error)); 159302ac6454SAndrew Thompson } 159402ac6454SAndrew Thompson } 159502ac6454SAndrew Thompson 159602ac6454SAndrew Thompson static void 159702ac6454SAndrew Thompson ural_write_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len) 159802ac6454SAndrew Thompson { 159902ac6454SAndrew Thompson struct usb2_device_request req; 160002ac6454SAndrew Thompson usb2_error_t error; 160102ac6454SAndrew Thompson 160202ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 160302ac6454SAndrew Thompson req.bRequest = RAL_WRITE_MULTI_MAC; 160402ac6454SAndrew Thompson USETW(req.wValue, 0); 160502ac6454SAndrew Thompson USETW(req.wIndex, reg); 160602ac6454SAndrew Thompson USETW(req.wLength, len); 160702ac6454SAndrew Thompson 160802ac6454SAndrew Thompson error = ural_do_request(sc, &req, buf); 160902ac6454SAndrew Thompson if (error != 0) { 161002ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not write MAC register: %s\n", 161102ac6454SAndrew Thompson usb2_errstr(error)); 161202ac6454SAndrew Thompson } 161302ac6454SAndrew Thompson } 161402ac6454SAndrew Thompson 161502ac6454SAndrew Thompson static void 161602ac6454SAndrew Thompson ural_bbp_write(struct ural_softc *sc, uint8_t reg, uint8_t val) 161702ac6454SAndrew Thompson { 161802ac6454SAndrew Thompson uint16_t tmp; 161902ac6454SAndrew Thompson int ntries; 162002ac6454SAndrew Thompson 162102ac6454SAndrew Thompson for (ntries = 0; ntries < 100; ntries++) { 162202ac6454SAndrew Thompson if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY)) 162302ac6454SAndrew Thompson break; 162402ac6454SAndrew Thompson if (ural_pause(sc, hz / 100)) 162502ac6454SAndrew Thompson break; 162602ac6454SAndrew Thompson } 162702ac6454SAndrew Thompson if (ntries == 100) { 162802ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not write to BBP\n"); 162902ac6454SAndrew Thompson return; 163002ac6454SAndrew Thompson } 163102ac6454SAndrew Thompson 163202ac6454SAndrew Thompson tmp = reg << 8 | val; 163302ac6454SAndrew Thompson ural_write(sc, RAL_PHY_CSR7, tmp); 163402ac6454SAndrew Thompson } 163502ac6454SAndrew Thompson 163602ac6454SAndrew Thompson static uint8_t 163702ac6454SAndrew Thompson ural_bbp_read(struct ural_softc *sc, uint8_t reg) 163802ac6454SAndrew Thompson { 163902ac6454SAndrew Thompson uint16_t val; 164002ac6454SAndrew Thompson int ntries; 164102ac6454SAndrew Thompson 164202ac6454SAndrew Thompson val = RAL_BBP_WRITE | reg << 8; 164302ac6454SAndrew Thompson ural_write(sc, RAL_PHY_CSR7, val); 164402ac6454SAndrew Thompson 164502ac6454SAndrew Thompson for (ntries = 0; ntries < 100; ntries++) { 164602ac6454SAndrew Thompson if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY)) 164702ac6454SAndrew Thompson break; 164802ac6454SAndrew Thompson if (ural_pause(sc, hz / 100)) 164902ac6454SAndrew Thompson break; 165002ac6454SAndrew Thompson } 165102ac6454SAndrew Thompson if (ntries == 100) { 165202ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not read BBP\n"); 165302ac6454SAndrew Thompson return 0; 165402ac6454SAndrew Thompson } 165502ac6454SAndrew Thompson 165602ac6454SAndrew Thompson return ural_read(sc, RAL_PHY_CSR7) & 0xff; 165702ac6454SAndrew Thompson } 165802ac6454SAndrew Thompson 165902ac6454SAndrew Thompson static void 166002ac6454SAndrew Thompson ural_rf_write(struct ural_softc *sc, uint8_t reg, uint32_t val) 166102ac6454SAndrew Thompson { 166202ac6454SAndrew Thompson uint32_t tmp; 166302ac6454SAndrew Thompson int ntries; 166402ac6454SAndrew Thompson 166502ac6454SAndrew Thompson for (ntries = 0; ntries < 100; ntries++) { 166602ac6454SAndrew Thompson if (!(ural_read(sc, RAL_PHY_CSR10) & RAL_RF_LOBUSY)) 166702ac6454SAndrew Thompson break; 166802ac6454SAndrew Thompson if (ural_pause(sc, hz / 100)) 166902ac6454SAndrew Thompson break; 167002ac6454SAndrew Thompson } 167102ac6454SAndrew Thompson if (ntries == 100) { 167202ac6454SAndrew Thompson device_printf(sc->sc_dev, "could not write to RF\n"); 167302ac6454SAndrew Thompson return; 167402ac6454SAndrew Thompson } 167502ac6454SAndrew Thompson 167602ac6454SAndrew Thompson tmp = RAL_RF_BUSY | RAL_RF_20BIT | (val & 0xfffff) << 2 | (reg & 0x3); 167702ac6454SAndrew Thompson ural_write(sc, RAL_PHY_CSR9, tmp & 0xffff); 167802ac6454SAndrew Thompson ural_write(sc, RAL_PHY_CSR10, tmp >> 16); 167902ac6454SAndrew Thompson 168002ac6454SAndrew Thompson /* remember last written value in sc */ 168102ac6454SAndrew Thompson sc->rf_regs[reg] = val; 168202ac6454SAndrew Thompson 168302ac6454SAndrew Thompson DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff); 168402ac6454SAndrew Thompson } 168502ac6454SAndrew Thompson 168602ac6454SAndrew Thompson /* ARGUSED */ 168702ac6454SAndrew Thompson static struct ieee80211_node * 168802ac6454SAndrew Thompson ural_node_alloc(struct ieee80211vap *vap __unused, 168902ac6454SAndrew Thompson const uint8_t mac[IEEE80211_ADDR_LEN] __unused) 169002ac6454SAndrew Thompson { 169102ac6454SAndrew Thompson struct ural_node *un; 169202ac6454SAndrew Thompson 169302ac6454SAndrew Thompson un = malloc(sizeof(struct ural_node), M_80211_NODE, M_NOWAIT | M_ZERO); 169402ac6454SAndrew Thompson return un != NULL ? &un->ni : NULL; 169502ac6454SAndrew Thompson } 169602ac6454SAndrew Thompson 169702ac6454SAndrew Thompson static void 169802ac6454SAndrew Thompson ural_newassoc(struct ieee80211_node *ni, int isnew) 169902ac6454SAndrew Thompson { 170002ac6454SAndrew Thompson struct ieee80211vap *vap = ni->ni_vap; 170102ac6454SAndrew Thompson 170202ac6454SAndrew Thompson ieee80211_amrr_node_init(&URAL_VAP(vap)->amrr, &URAL_NODE(ni)->amn, ni); 170302ac6454SAndrew Thompson } 170402ac6454SAndrew Thompson 170502ac6454SAndrew Thompson static void 170602ac6454SAndrew Thompson ural_scan_start(struct ieee80211com *ic) 170702ac6454SAndrew Thompson { 170802ac6454SAndrew Thompson struct ural_softc *sc = ic->ic_ifp->if_softc; 170902ac6454SAndrew Thompson 171002ac6454SAndrew Thompson RAL_LOCK(sc); 171102ac6454SAndrew Thompson /* do it in a process context */ 171202ac6454SAndrew Thompson sc->sc_scan_action = URAL_SCAN_START; 171302ac6454SAndrew Thompson ural_queue_command(sc, ural_scantask, 171402ac6454SAndrew Thompson &sc->sc_scantask[0].hdr, &sc->sc_scantask[1].hdr); 171502ac6454SAndrew Thompson RAL_UNLOCK(sc); 171602ac6454SAndrew Thompson 171702ac6454SAndrew Thompson } 171802ac6454SAndrew Thompson 171902ac6454SAndrew Thompson static void 172002ac6454SAndrew Thompson ural_scan_end(struct ieee80211com *ic) 172102ac6454SAndrew Thompson { 172202ac6454SAndrew Thompson struct ural_softc *sc = ic->ic_ifp->if_softc; 172302ac6454SAndrew Thompson 172402ac6454SAndrew Thompson RAL_LOCK(sc); 172502ac6454SAndrew Thompson /* do it in a process context */ 172602ac6454SAndrew Thompson sc->sc_scan_action = URAL_SCAN_END; 172702ac6454SAndrew Thompson ural_queue_command(sc, ural_scantask, 172802ac6454SAndrew Thompson &sc->sc_scantask[0].hdr, &sc->sc_scantask[1].hdr); 172902ac6454SAndrew Thompson RAL_UNLOCK(sc); 173002ac6454SAndrew Thompson 173102ac6454SAndrew Thompson } 173202ac6454SAndrew Thompson 173302ac6454SAndrew Thompson static void 173402ac6454SAndrew Thompson ural_set_channel(struct ieee80211com *ic) 173502ac6454SAndrew Thompson { 173602ac6454SAndrew Thompson struct ural_softc *sc = ic->ic_ifp->if_softc; 173702ac6454SAndrew Thompson 173802ac6454SAndrew Thompson RAL_LOCK(sc); 173902ac6454SAndrew Thompson /* do it in a process context */ 174002ac6454SAndrew Thompson sc->sc_scan_action = URAL_SET_CHANNEL; 174102ac6454SAndrew Thompson ural_queue_command(sc, ural_scantask, 174202ac6454SAndrew Thompson &sc->sc_scantask[0].hdr, &sc->sc_scantask[1].hdr); 174302ac6454SAndrew Thompson RAL_UNLOCK(sc); 174402ac6454SAndrew Thompson } 174502ac6454SAndrew Thompson 174602ac6454SAndrew Thompson static void 174702ac6454SAndrew Thompson ural_set_chan(struct ural_softc *sc, struct ieee80211_channel *c) 174802ac6454SAndrew Thompson { 174902ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 175002ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 175102ac6454SAndrew Thompson uint8_t power, tmp; 175202ac6454SAndrew Thompson int i, chan; 175302ac6454SAndrew Thompson 175402ac6454SAndrew Thompson chan = ieee80211_chan2ieee(ic, c); 175502ac6454SAndrew Thompson if (chan == 0 || chan == IEEE80211_CHAN_ANY) 175602ac6454SAndrew Thompson return; 175702ac6454SAndrew Thompson 175802ac6454SAndrew Thompson if (IEEE80211_IS_CHAN_2GHZ(c)) 175902ac6454SAndrew Thompson power = min(sc->txpow[chan - 1], 31); 176002ac6454SAndrew Thompson else 176102ac6454SAndrew Thompson power = 31; 176202ac6454SAndrew Thompson 176302ac6454SAndrew Thompson /* adjust txpower using ifconfig settings */ 176402ac6454SAndrew Thompson power -= (100 - ic->ic_txpowlimit) / 8; 176502ac6454SAndrew Thompson 176602ac6454SAndrew Thompson DPRINTFN(2, "setting channel to %u, txpower to %u\n", chan, power); 176702ac6454SAndrew Thompson 176802ac6454SAndrew Thompson switch (sc->rf_rev) { 176902ac6454SAndrew Thompson case RAL_RF_2522: 177002ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x00814); 177102ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2522_r2[chan - 1]); 177202ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 177302ac6454SAndrew Thompson break; 177402ac6454SAndrew Thompson 177502ac6454SAndrew Thompson case RAL_RF_2523: 177602ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x08804); 177702ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2523_r2[chan - 1]); 177802ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x38044); 177902ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 178002ac6454SAndrew Thompson break; 178102ac6454SAndrew Thompson 178202ac6454SAndrew Thompson case RAL_RF_2524: 178302ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x0c808); 178402ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2524_r2[chan - 1]); 178502ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 178602ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 178702ac6454SAndrew Thompson break; 178802ac6454SAndrew Thompson 178902ac6454SAndrew Thompson case RAL_RF_2525: 179002ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x08808); 179102ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2525_hi_r2[chan - 1]); 179202ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 179302ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 179402ac6454SAndrew Thompson 179502ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x08808); 179602ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2525_r2[chan - 1]); 179702ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 179802ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 179902ac6454SAndrew Thompson break; 180002ac6454SAndrew Thompson 180102ac6454SAndrew Thompson case RAL_RF_2525E: 180202ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x08808); 180302ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2525e_r2[chan - 1]); 180402ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 180502ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282); 180602ac6454SAndrew Thompson break; 180702ac6454SAndrew Thompson 180802ac6454SAndrew Thompson case RAL_RF_2526: 180902ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2526_hi_r2[chan - 1]); 181002ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 181102ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, 0x08804); 181202ac6454SAndrew Thompson 181302ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf2526_r2[chan - 1]); 181402ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 181502ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 181602ac6454SAndrew Thompson break; 181702ac6454SAndrew Thompson 181802ac6454SAndrew Thompson /* dual-band RF */ 181902ac6454SAndrew Thompson case RAL_RF_5222: 182002ac6454SAndrew Thompson for (i = 0; ural_rf5222[i].chan != chan; i++); 182102ac6454SAndrew Thompson 182202ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, ural_rf5222[i].r1); 182302ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF2, ural_rf5222[i].r2); 182402ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 182502ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF4, ural_rf5222[i].r4); 182602ac6454SAndrew Thompson break; 182702ac6454SAndrew Thompson } 182802ac6454SAndrew Thompson 182902ac6454SAndrew Thompson if (ic->ic_opmode != IEEE80211_M_MONITOR && 183002ac6454SAndrew Thompson (ic->ic_flags & IEEE80211_F_SCAN) == 0) { 183102ac6454SAndrew Thompson /* set Japan filter bit for channel 14 */ 183202ac6454SAndrew Thompson tmp = ural_bbp_read(sc, 70); 183302ac6454SAndrew Thompson 183402ac6454SAndrew Thompson tmp &= ~RAL_JAPAN_FILTER; 183502ac6454SAndrew Thompson if (chan == 14) 183602ac6454SAndrew Thompson tmp |= RAL_JAPAN_FILTER; 183702ac6454SAndrew Thompson 183802ac6454SAndrew Thompson ural_bbp_write(sc, 70, tmp); 183902ac6454SAndrew Thompson 184002ac6454SAndrew Thompson /* clear CRC errors */ 184102ac6454SAndrew Thompson ural_read(sc, RAL_STA_CSR0); 184202ac6454SAndrew Thompson 184302ac6454SAndrew Thompson ural_pause(sc, hz / 100); 184402ac6454SAndrew Thompson ural_disable_rf_tune(sc); 184502ac6454SAndrew Thompson } 184602ac6454SAndrew Thompson 184702ac6454SAndrew Thompson /* XXX doesn't belong here */ 184802ac6454SAndrew Thompson /* update basic rate set */ 184902ac6454SAndrew Thompson ural_set_basicrates(sc, c); 185077ddf3d3SAndrew Thompson 185177ddf3d3SAndrew Thompson /* give the hardware some time to do the switchover */ 185277ddf3d3SAndrew Thompson ural_pause(sc, hz / 100); 185302ac6454SAndrew Thompson } 185402ac6454SAndrew Thompson 185502ac6454SAndrew Thompson /* 185602ac6454SAndrew Thompson * Disable RF auto-tuning. 185702ac6454SAndrew Thompson */ 185802ac6454SAndrew Thompson static void 185902ac6454SAndrew Thompson ural_disable_rf_tune(struct ural_softc *sc) 186002ac6454SAndrew Thompson { 186102ac6454SAndrew Thompson uint32_t tmp; 186202ac6454SAndrew Thompson 186302ac6454SAndrew Thompson if (sc->rf_rev != RAL_RF_2523) { 186402ac6454SAndrew Thompson tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE; 186502ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF1, tmp); 186602ac6454SAndrew Thompson } 186702ac6454SAndrew Thompson 186802ac6454SAndrew Thompson tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE; 186902ac6454SAndrew Thompson ural_rf_write(sc, RAL_RF3, tmp); 187002ac6454SAndrew Thompson 187102ac6454SAndrew Thompson DPRINTFN(2, "disabling RF autotune\n"); 187202ac6454SAndrew Thompson } 187302ac6454SAndrew Thompson 187402ac6454SAndrew Thompson /* 187502ac6454SAndrew Thompson * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF 187602ac6454SAndrew Thompson * synchronization. 187702ac6454SAndrew Thompson */ 187802ac6454SAndrew Thompson static void 187902ac6454SAndrew Thompson ural_enable_tsf_sync(struct ural_softc *sc) 188002ac6454SAndrew Thompson { 188102ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 188202ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 188302ac6454SAndrew Thompson struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 188402ac6454SAndrew Thompson uint16_t logcwmin, preload, tmp; 188502ac6454SAndrew Thompson 188602ac6454SAndrew Thompson /* first, disable TSF synchronization */ 188702ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR19, 0); 188802ac6454SAndrew Thompson 188902ac6454SAndrew Thompson tmp = (16 * vap->iv_bss->ni_intval) << 4; 189002ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR18, tmp); 189102ac6454SAndrew Thompson 189202ac6454SAndrew Thompson logcwmin = (ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 0; 189302ac6454SAndrew Thompson preload = (ic->ic_opmode == IEEE80211_M_IBSS) ? 320 : 6; 189402ac6454SAndrew Thompson tmp = logcwmin << 12 | preload; 189502ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR20, tmp); 189602ac6454SAndrew Thompson 189702ac6454SAndrew Thompson /* finally, enable TSF synchronization */ 189802ac6454SAndrew Thompson tmp = RAL_ENABLE_TSF | RAL_ENABLE_TBCN; 189902ac6454SAndrew Thompson if (ic->ic_opmode == IEEE80211_M_STA) 190002ac6454SAndrew Thompson tmp |= RAL_ENABLE_TSF_SYNC(1); 190102ac6454SAndrew Thompson else 190202ac6454SAndrew Thompson tmp |= RAL_ENABLE_TSF_SYNC(2) | RAL_ENABLE_BEACON_GENERATOR; 190302ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR19, tmp); 190402ac6454SAndrew Thompson 190502ac6454SAndrew Thompson DPRINTF("enabling TSF synchronization\n"); 190602ac6454SAndrew Thompson } 190702ac6454SAndrew Thompson 190802ac6454SAndrew Thompson #define RAL_RXTX_TURNAROUND 5 /* us */ 190902ac6454SAndrew Thompson static void 191002ac6454SAndrew Thompson ural_update_slot(struct ifnet *ifp) 191102ac6454SAndrew Thompson { 191202ac6454SAndrew Thompson struct ural_softc *sc = ifp->if_softc; 191302ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 191402ac6454SAndrew Thompson uint16_t slottime, sifs, eifs; 191502ac6454SAndrew Thompson 191602ac6454SAndrew Thompson slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20; 191702ac6454SAndrew Thompson 191802ac6454SAndrew Thompson /* 191902ac6454SAndrew Thompson * These settings may sound a bit inconsistent but this is what the 192002ac6454SAndrew Thompson * reference driver does. 192102ac6454SAndrew Thompson */ 192202ac6454SAndrew Thompson if (ic->ic_curmode == IEEE80211_MODE_11B) { 192302ac6454SAndrew Thompson sifs = 16 - RAL_RXTX_TURNAROUND; 192402ac6454SAndrew Thompson eifs = 364; 192502ac6454SAndrew Thompson } else { 192602ac6454SAndrew Thompson sifs = 10 - RAL_RXTX_TURNAROUND; 192702ac6454SAndrew Thompson eifs = 64; 192802ac6454SAndrew Thompson } 192902ac6454SAndrew Thompson 193002ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR10, slottime); 193102ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR11, sifs); 193202ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR12, eifs); 193302ac6454SAndrew Thompson } 193402ac6454SAndrew Thompson 193502ac6454SAndrew Thompson static void 193602ac6454SAndrew Thompson ural_set_txpreamble(struct ural_softc *sc) 193702ac6454SAndrew Thompson { 193802ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 193902ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 194002ac6454SAndrew Thompson uint16_t tmp; 194102ac6454SAndrew Thompson 194202ac6454SAndrew Thompson tmp = ural_read(sc, RAL_TXRX_CSR10); 194302ac6454SAndrew Thompson 194402ac6454SAndrew Thompson tmp &= ~RAL_SHORT_PREAMBLE; 194502ac6454SAndrew Thompson if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 194602ac6454SAndrew Thompson tmp |= RAL_SHORT_PREAMBLE; 194702ac6454SAndrew Thompson 194802ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR10, tmp); 194902ac6454SAndrew Thompson } 195002ac6454SAndrew Thompson 195102ac6454SAndrew Thompson static void 195202ac6454SAndrew Thompson ural_set_basicrates(struct ural_softc *sc, const struct ieee80211_channel *c) 195302ac6454SAndrew Thompson { 195402ac6454SAndrew Thompson /* XXX wrong, take from rate set */ 195502ac6454SAndrew Thompson /* update basic rate set */ 195602ac6454SAndrew Thompson if (IEEE80211_IS_CHAN_5GHZ(c)) { 195702ac6454SAndrew Thompson /* 11a basic rates: 6, 12, 24Mbps */ 195802ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR11, 0x150); 195902ac6454SAndrew Thompson } else if (IEEE80211_IS_CHAN_ANYG(c)) { 196002ac6454SAndrew Thompson /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */ 196102ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR11, 0x15f); 196202ac6454SAndrew Thompson } else { 196302ac6454SAndrew Thompson /* 11b basic rates: 1, 2Mbps */ 196402ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR11, 0x3); 196502ac6454SAndrew Thompson } 196602ac6454SAndrew Thompson } 196702ac6454SAndrew Thompson 196802ac6454SAndrew Thompson static void 196902ac6454SAndrew Thompson ural_set_bssid(struct ural_softc *sc, const uint8_t *bssid) 197002ac6454SAndrew Thompson { 197102ac6454SAndrew Thompson uint16_t tmp; 197202ac6454SAndrew Thompson 197302ac6454SAndrew Thompson tmp = bssid[0] | bssid[1] << 8; 197402ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR5, tmp); 197502ac6454SAndrew Thompson 197602ac6454SAndrew Thompson tmp = bssid[2] | bssid[3] << 8; 197702ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR6, tmp); 197802ac6454SAndrew Thompson 197902ac6454SAndrew Thompson tmp = bssid[4] | bssid[5] << 8; 198002ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR7, tmp); 198102ac6454SAndrew Thompson 198202ac6454SAndrew Thompson DPRINTF("setting BSSID to %6D\n", bssid, ":"); 198302ac6454SAndrew Thompson } 198402ac6454SAndrew Thompson 198502ac6454SAndrew Thompson static void 198602ac6454SAndrew Thompson ural_set_macaddr(struct ural_softc *sc, uint8_t *addr) 198702ac6454SAndrew Thompson { 198802ac6454SAndrew Thompson uint16_t tmp; 198902ac6454SAndrew Thompson 199002ac6454SAndrew Thompson tmp = addr[0] | addr[1] << 8; 199102ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR2, tmp); 199202ac6454SAndrew Thompson 199302ac6454SAndrew Thompson tmp = addr[2] | addr[3] << 8; 199402ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR3, tmp); 199502ac6454SAndrew Thompson 199602ac6454SAndrew Thompson tmp = addr[4] | addr[5] << 8; 199702ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR4, tmp); 199802ac6454SAndrew Thompson 199902ac6454SAndrew Thompson DPRINTF("setting MAC address to %6D\n", addr, ":"); 200002ac6454SAndrew Thompson } 200102ac6454SAndrew Thompson 200202ac6454SAndrew Thompson static void 200302ac6454SAndrew Thompson ural_promisctask(struct usb2_proc_msg *pm) 200402ac6454SAndrew Thompson { 200502ac6454SAndrew Thompson struct ural_task *task = (struct ural_task *)pm; 200602ac6454SAndrew Thompson struct ural_softc *sc = task->sc; 200702ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 200802ac6454SAndrew Thompson uint32_t tmp; 200902ac6454SAndrew Thompson 201002ac6454SAndrew Thompson tmp = ural_read(sc, RAL_TXRX_CSR2); 201102ac6454SAndrew Thompson 201202ac6454SAndrew Thompson tmp &= ~RAL_DROP_NOT_TO_ME; 201302ac6454SAndrew Thompson if (!(ifp->if_flags & IFF_PROMISC)) 201402ac6454SAndrew Thompson tmp |= RAL_DROP_NOT_TO_ME; 201502ac6454SAndrew Thompson 201602ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR2, tmp); 201702ac6454SAndrew Thompson 201802ac6454SAndrew Thompson DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ? 201902ac6454SAndrew Thompson "entering" : "leaving"); 202002ac6454SAndrew Thompson } 202102ac6454SAndrew Thompson 202277ddf3d3SAndrew Thompson static void 202377ddf3d3SAndrew Thompson ural_update_promisc(struct ifnet *ifp) 202477ddf3d3SAndrew Thompson { 202577ddf3d3SAndrew Thompson struct ural_softc *sc = ifp->if_softc; 202677ddf3d3SAndrew Thompson 202777ddf3d3SAndrew Thompson if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 202877ddf3d3SAndrew Thompson return; 202977ddf3d3SAndrew Thompson 203077ddf3d3SAndrew Thompson RAL_LOCK(sc); 203177ddf3d3SAndrew Thompson ural_queue_command(sc, ural_promisctask, 203277ddf3d3SAndrew Thompson &sc->sc_promisctask[0].hdr, 203377ddf3d3SAndrew Thompson &sc->sc_promisctask[1].hdr); 203477ddf3d3SAndrew Thompson RAL_UNLOCK(sc); 203577ddf3d3SAndrew Thompson } 203677ddf3d3SAndrew Thompson 203702ac6454SAndrew Thompson static const char * 203802ac6454SAndrew Thompson ural_get_rf(int rev) 203902ac6454SAndrew Thompson { 204002ac6454SAndrew Thompson switch (rev) { 204102ac6454SAndrew Thompson case RAL_RF_2522: return "RT2522"; 204202ac6454SAndrew Thompson case RAL_RF_2523: return "RT2523"; 204302ac6454SAndrew Thompson case RAL_RF_2524: return "RT2524"; 204402ac6454SAndrew Thompson case RAL_RF_2525: return "RT2525"; 204502ac6454SAndrew Thompson case RAL_RF_2525E: return "RT2525e"; 204602ac6454SAndrew Thompson case RAL_RF_2526: return "RT2526"; 204702ac6454SAndrew Thompson case RAL_RF_5222: return "RT5222"; 204802ac6454SAndrew Thompson default: return "unknown"; 204902ac6454SAndrew Thompson } 205002ac6454SAndrew Thompson } 205102ac6454SAndrew Thompson 205202ac6454SAndrew Thompson static void 205302ac6454SAndrew Thompson ural_read_eeprom(struct ural_softc *sc) 205402ac6454SAndrew Thompson { 205502ac6454SAndrew Thompson uint16_t val; 205602ac6454SAndrew Thompson 205702ac6454SAndrew Thompson ural_eeprom_read(sc, RAL_EEPROM_CONFIG0, &val, 2); 205802ac6454SAndrew Thompson val = le16toh(val); 205902ac6454SAndrew Thompson sc->rf_rev = (val >> 11) & 0x7; 206002ac6454SAndrew Thompson sc->hw_radio = (val >> 10) & 0x1; 206102ac6454SAndrew Thompson sc->led_mode = (val >> 6) & 0x7; 206202ac6454SAndrew Thompson sc->rx_ant = (val >> 4) & 0x3; 206302ac6454SAndrew Thompson sc->tx_ant = (val >> 2) & 0x3; 206402ac6454SAndrew Thompson sc->nb_ant = val & 0x3; 206502ac6454SAndrew Thompson 206602ac6454SAndrew Thompson /* read MAC address */ 206702ac6454SAndrew Thompson ural_eeprom_read(sc, RAL_EEPROM_ADDRESS, sc->sc_bssid, 6); 206802ac6454SAndrew Thompson 206902ac6454SAndrew Thompson /* read default values for BBP registers */ 207002ac6454SAndrew Thompson ural_eeprom_read(sc, RAL_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16); 207102ac6454SAndrew Thompson 207202ac6454SAndrew Thompson /* read Tx power for all b/g channels */ 207302ac6454SAndrew Thompson ural_eeprom_read(sc, RAL_EEPROM_TXPOWER, sc->txpow, 14); 207402ac6454SAndrew Thompson } 207502ac6454SAndrew Thompson 207602ac6454SAndrew Thompson static int 207702ac6454SAndrew Thompson ural_bbp_init(struct ural_softc *sc) 207802ac6454SAndrew Thompson { 207902ac6454SAndrew Thompson #define N(a) (sizeof (a) / sizeof ((a)[0])) 208002ac6454SAndrew Thompson int i, ntries; 208102ac6454SAndrew Thompson 208202ac6454SAndrew Thompson /* wait for BBP to be ready */ 208302ac6454SAndrew Thompson for (ntries = 0; ntries < 100; ntries++) { 208402ac6454SAndrew Thompson if (ural_bbp_read(sc, RAL_BBP_VERSION) != 0) 208502ac6454SAndrew Thompson break; 208602ac6454SAndrew Thompson if (ural_pause(sc, hz / 100)) 208702ac6454SAndrew Thompson break; 208802ac6454SAndrew Thompson } 208902ac6454SAndrew Thompson if (ntries == 100) { 209002ac6454SAndrew Thompson device_printf(sc->sc_dev, "timeout waiting for BBP\n"); 209102ac6454SAndrew Thompson return EIO; 209202ac6454SAndrew Thompson } 209302ac6454SAndrew Thompson 209402ac6454SAndrew Thompson /* initialize BBP registers to default values */ 209502ac6454SAndrew Thompson for (i = 0; i < N(ural_def_bbp); i++) 209602ac6454SAndrew Thompson ural_bbp_write(sc, ural_def_bbp[i].reg, ural_def_bbp[i].val); 209702ac6454SAndrew Thompson 209802ac6454SAndrew Thompson #if 0 209902ac6454SAndrew Thompson /* initialize BBP registers to values stored in EEPROM */ 210002ac6454SAndrew Thompson for (i = 0; i < 16; i++) { 210102ac6454SAndrew Thompson if (sc->bbp_prom[i].reg == 0xff) 210202ac6454SAndrew Thompson continue; 210302ac6454SAndrew Thompson ural_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 210402ac6454SAndrew Thompson } 210502ac6454SAndrew Thompson #endif 210602ac6454SAndrew Thompson 210702ac6454SAndrew Thompson return 0; 210802ac6454SAndrew Thompson #undef N 210902ac6454SAndrew Thompson } 211002ac6454SAndrew Thompson 211102ac6454SAndrew Thompson static void 211202ac6454SAndrew Thompson ural_set_txantenna(struct ural_softc *sc, int antenna) 211302ac6454SAndrew Thompson { 211402ac6454SAndrew Thompson uint16_t tmp; 211502ac6454SAndrew Thompson uint8_t tx; 211602ac6454SAndrew Thompson 211702ac6454SAndrew Thompson tx = ural_bbp_read(sc, RAL_BBP_TX) & ~RAL_BBP_ANTMASK; 211802ac6454SAndrew Thompson if (antenna == 1) 211902ac6454SAndrew Thompson tx |= RAL_BBP_ANTA; 212002ac6454SAndrew Thompson else if (antenna == 2) 212102ac6454SAndrew Thompson tx |= RAL_BBP_ANTB; 212202ac6454SAndrew Thompson else 212302ac6454SAndrew Thompson tx |= RAL_BBP_DIVERSITY; 212402ac6454SAndrew Thompson 212502ac6454SAndrew Thompson /* need to force I/Q flip for RF 2525e, 2526 and 5222 */ 212602ac6454SAndrew Thompson if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526 || 212702ac6454SAndrew Thompson sc->rf_rev == RAL_RF_5222) 212802ac6454SAndrew Thompson tx |= RAL_BBP_FLIPIQ; 212902ac6454SAndrew Thompson 213002ac6454SAndrew Thompson ural_bbp_write(sc, RAL_BBP_TX, tx); 213102ac6454SAndrew Thompson 213202ac6454SAndrew Thompson /* update values in PHY_CSR5 and PHY_CSR6 */ 213302ac6454SAndrew Thompson tmp = ural_read(sc, RAL_PHY_CSR5) & ~0x7; 213402ac6454SAndrew Thompson ural_write(sc, RAL_PHY_CSR5, tmp | (tx & 0x7)); 213502ac6454SAndrew Thompson 213602ac6454SAndrew Thompson tmp = ural_read(sc, RAL_PHY_CSR6) & ~0x7; 213702ac6454SAndrew Thompson ural_write(sc, RAL_PHY_CSR6, tmp | (tx & 0x7)); 213802ac6454SAndrew Thompson } 213902ac6454SAndrew Thompson 214002ac6454SAndrew Thompson static void 214102ac6454SAndrew Thompson ural_set_rxantenna(struct ural_softc *sc, int antenna) 214202ac6454SAndrew Thompson { 214302ac6454SAndrew Thompson uint8_t rx; 214402ac6454SAndrew Thompson 214502ac6454SAndrew Thompson rx = ural_bbp_read(sc, RAL_BBP_RX) & ~RAL_BBP_ANTMASK; 214602ac6454SAndrew Thompson if (antenna == 1) 214702ac6454SAndrew Thompson rx |= RAL_BBP_ANTA; 214802ac6454SAndrew Thompson else if (antenna == 2) 214902ac6454SAndrew Thompson rx |= RAL_BBP_ANTB; 215002ac6454SAndrew Thompson else 215102ac6454SAndrew Thompson rx |= RAL_BBP_DIVERSITY; 215202ac6454SAndrew Thompson 215302ac6454SAndrew Thompson /* need to force no I/Q flip for RF 2525e and 2526 */ 215402ac6454SAndrew Thompson if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526) 215502ac6454SAndrew Thompson rx &= ~RAL_BBP_FLIPIQ; 215602ac6454SAndrew Thompson 215702ac6454SAndrew Thompson ural_bbp_write(sc, RAL_BBP_RX, rx); 215802ac6454SAndrew Thompson } 215902ac6454SAndrew Thompson 216002ac6454SAndrew Thompson static void 216102ac6454SAndrew Thompson ural_init_task(struct usb2_proc_msg *pm) 216202ac6454SAndrew Thompson { 216302ac6454SAndrew Thompson #define N(a) (sizeof (a) / sizeof ((a)[0])) 216402ac6454SAndrew Thompson struct ural_task *task = (struct ural_task *)pm; 216502ac6454SAndrew Thompson struct ural_softc *sc = task->sc; 216602ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 216702ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 216802ac6454SAndrew Thompson uint16_t tmp; 216902ac6454SAndrew Thompson int i, ntries; 217002ac6454SAndrew Thompson 217102ac6454SAndrew Thompson RAL_LOCK_ASSERT(sc, MA_OWNED); 217202ac6454SAndrew Thompson 217302ac6454SAndrew Thompson ural_set_testmode(sc); 217402ac6454SAndrew Thompson ural_write(sc, 0x308, 0x00f0); /* XXX magic */ 217502ac6454SAndrew Thompson 217602ac6454SAndrew Thompson ural_stop_task(pm); 217702ac6454SAndrew Thompson 217802ac6454SAndrew Thompson /* initialize MAC registers to default values */ 217902ac6454SAndrew Thompson for (i = 0; i < N(ural_def_mac); i++) 218002ac6454SAndrew Thompson ural_write(sc, ural_def_mac[i].reg, ural_def_mac[i].val); 218102ac6454SAndrew Thompson 218202ac6454SAndrew Thompson /* wait for BBP and RF to wake up (this can take a long time!) */ 218302ac6454SAndrew Thompson for (ntries = 0; ntries < 100; ntries++) { 218402ac6454SAndrew Thompson tmp = ural_read(sc, RAL_MAC_CSR17); 218502ac6454SAndrew Thompson if ((tmp & (RAL_BBP_AWAKE | RAL_RF_AWAKE)) == 218602ac6454SAndrew Thompson (RAL_BBP_AWAKE | RAL_RF_AWAKE)) 218702ac6454SAndrew Thompson break; 218802ac6454SAndrew Thompson if (ural_pause(sc, hz / 100)) 218902ac6454SAndrew Thompson break; 219002ac6454SAndrew Thompson } 219102ac6454SAndrew Thompson if (ntries == 100) { 219202ac6454SAndrew Thompson device_printf(sc->sc_dev, 219302ac6454SAndrew Thompson "timeout waiting for BBP/RF to wakeup\n"); 219402ac6454SAndrew Thompson goto fail; 219502ac6454SAndrew Thompson } 219602ac6454SAndrew Thompson 219702ac6454SAndrew Thompson /* we're ready! */ 219802ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR1, RAL_HOST_READY); 219902ac6454SAndrew Thompson 220002ac6454SAndrew Thompson /* set basic rate set (will be updated later) */ 220102ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR11, 0x15f); 220202ac6454SAndrew Thompson 220302ac6454SAndrew Thompson if (ural_bbp_init(sc) != 0) 220402ac6454SAndrew Thompson goto fail; 220502ac6454SAndrew Thompson 220602ac6454SAndrew Thompson ural_set_chan(sc, ic->ic_curchan); 220702ac6454SAndrew Thompson 220802ac6454SAndrew Thompson /* clear statistic registers (STA_CSR0 to STA_CSR10) */ 220902ac6454SAndrew Thompson ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta); 221002ac6454SAndrew Thompson 221102ac6454SAndrew Thompson ural_set_txantenna(sc, sc->tx_ant); 221202ac6454SAndrew Thompson ural_set_rxantenna(sc, sc->rx_ant); 221302ac6454SAndrew Thompson 221429aca940SSam Leffler ural_set_macaddr(sc, IF_LLADDR(ifp)); 221502ac6454SAndrew Thompson 221602ac6454SAndrew Thompson /* 221702ac6454SAndrew Thompson * Allocate Tx and Rx xfer queues. 221802ac6454SAndrew Thompson */ 221902ac6454SAndrew Thompson ural_setup_tx_list(sc); 222002ac6454SAndrew Thompson 222102ac6454SAndrew Thompson /* kick Rx */ 222202ac6454SAndrew Thompson tmp = RAL_DROP_PHY | RAL_DROP_CRC; 222302ac6454SAndrew Thompson if (ic->ic_opmode != IEEE80211_M_MONITOR) { 222402ac6454SAndrew Thompson tmp |= RAL_DROP_CTL | RAL_DROP_BAD_VERSION; 222502ac6454SAndrew Thompson if (ic->ic_opmode != IEEE80211_M_HOSTAP) 222602ac6454SAndrew Thompson tmp |= RAL_DROP_TODS; 222702ac6454SAndrew Thompson if (!(ifp->if_flags & IFF_PROMISC)) 222802ac6454SAndrew Thompson tmp |= RAL_DROP_NOT_TO_ME; 222902ac6454SAndrew Thompson } 223002ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR2, tmp); 223102ac6454SAndrew Thompson 223202ac6454SAndrew Thompson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 223302ac6454SAndrew Thompson ifp->if_drv_flags |= IFF_DRV_RUNNING; 223477ddf3d3SAndrew Thompson usb2_transfer_set_stall(sc->sc_xfer[URAL_BULK_WR]); 223502ac6454SAndrew Thompson usb2_transfer_start(sc->sc_xfer[URAL_BULK_RD]); 223602ac6454SAndrew Thompson return; 223702ac6454SAndrew Thompson 223802ac6454SAndrew Thompson fail: ural_stop_task(pm); 223902ac6454SAndrew Thompson #undef N 224002ac6454SAndrew Thompson } 224102ac6454SAndrew Thompson 224202ac6454SAndrew Thompson static void 224302ac6454SAndrew Thompson ural_init(void *priv) 224402ac6454SAndrew Thompson { 224502ac6454SAndrew Thompson struct ural_softc *sc = priv; 224602ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 224702ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 224802ac6454SAndrew Thompson 224902ac6454SAndrew Thompson RAL_LOCK(sc); 225002ac6454SAndrew Thompson ural_queue_command(sc, ural_init_task, 225102ac6454SAndrew Thompson &sc->sc_synctask[0].hdr, 225202ac6454SAndrew Thompson &sc->sc_synctask[1].hdr); 225302ac6454SAndrew Thompson RAL_UNLOCK(sc); 225402ac6454SAndrew Thompson 225502ac6454SAndrew Thompson if (ifp->if_drv_flags & IFF_DRV_RUNNING) 225602ac6454SAndrew Thompson ieee80211_start_all(ic); /* start all vap's */ 225702ac6454SAndrew Thompson } 225802ac6454SAndrew Thompson 225902ac6454SAndrew Thompson static void 226002ac6454SAndrew Thompson ural_stop_task(struct usb2_proc_msg *pm) 226102ac6454SAndrew Thompson { 226202ac6454SAndrew Thompson struct ural_task *task = (struct ural_task *)pm; 226302ac6454SAndrew Thompson struct ural_softc *sc = task->sc; 226402ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 226502ac6454SAndrew Thompson 226602ac6454SAndrew Thompson RAL_LOCK_ASSERT(sc, MA_OWNED); 226702ac6454SAndrew Thompson 226802ac6454SAndrew Thompson ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 226902ac6454SAndrew Thompson 227002ac6454SAndrew Thompson /* 227102ac6454SAndrew Thompson * Drain all the transfers, if not already drained: 227202ac6454SAndrew Thompson */ 227302ac6454SAndrew Thompson RAL_UNLOCK(sc); 227402ac6454SAndrew Thompson usb2_transfer_drain(sc->sc_xfer[URAL_BULK_WR]); 227502ac6454SAndrew Thompson usb2_transfer_drain(sc->sc_xfer[URAL_BULK_RD]); 227602ac6454SAndrew Thompson RAL_LOCK(sc); 227702ac6454SAndrew Thompson 227802ac6454SAndrew Thompson ural_unsetup_tx_list(sc); 227902ac6454SAndrew Thompson 228002ac6454SAndrew Thompson /* disable Rx */ 228102ac6454SAndrew Thompson ural_write(sc, RAL_TXRX_CSR2, RAL_DISABLE_RX); 228202ac6454SAndrew Thompson /* reset ASIC and BBP (but won't reset MAC registers!) */ 228302ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR1, RAL_RESET_ASIC | RAL_RESET_BBP); 228402ac6454SAndrew Thompson /* wait a little */ 228502ac6454SAndrew Thompson ural_pause(sc, hz / 10); 228602ac6454SAndrew Thompson ural_write(sc, RAL_MAC_CSR1, 0); 228777ddf3d3SAndrew Thompson /* wait a little */ 228877ddf3d3SAndrew Thompson ural_pause(sc, hz / 10); 228902ac6454SAndrew Thompson } 229002ac6454SAndrew Thompson 229102ac6454SAndrew Thompson static int 229202ac6454SAndrew Thompson ural_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 229302ac6454SAndrew Thompson const struct ieee80211_bpf_params *params) 229402ac6454SAndrew Thompson { 229502ac6454SAndrew Thompson struct ieee80211com *ic = ni->ni_ic; 229602ac6454SAndrew Thompson struct ifnet *ifp = ic->ic_ifp; 229702ac6454SAndrew Thompson struct ural_softc *sc = ifp->if_softc; 229802ac6454SAndrew Thompson 229902ac6454SAndrew Thompson RAL_LOCK(sc); 230002ac6454SAndrew Thompson /* prevent management frames from being sent if we're not ready */ 230102ac6454SAndrew Thompson if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 230202ac6454SAndrew Thompson RAL_UNLOCK(sc); 230302ac6454SAndrew Thompson m_freem(m); 230402ac6454SAndrew Thompson ieee80211_free_node(ni); 230502ac6454SAndrew Thompson return ENETDOWN; 230602ac6454SAndrew Thompson } 230796ca458fSAndrew Thompson if (sc->tx_nfree < RAL_TX_MINFREE) { 230802ac6454SAndrew Thompson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 230902ac6454SAndrew Thompson RAL_UNLOCK(sc); 231002ac6454SAndrew Thompson m_freem(m); 231102ac6454SAndrew Thompson ieee80211_free_node(ni); 231202ac6454SAndrew Thompson return EIO; 231302ac6454SAndrew Thompson } 231402ac6454SAndrew Thompson 231502ac6454SAndrew Thompson ifp->if_opackets++; 231602ac6454SAndrew Thompson 231702ac6454SAndrew Thompson if (params == NULL) { 231802ac6454SAndrew Thompson /* 231902ac6454SAndrew Thompson * Legacy path; interpret frame contents to decide 232002ac6454SAndrew Thompson * precisely how to send the frame. 232102ac6454SAndrew Thompson */ 232202ac6454SAndrew Thompson if (ural_tx_mgt(sc, m, ni) != 0) 232302ac6454SAndrew Thompson goto bad; 232402ac6454SAndrew Thompson } else { 232502ac6454SAndrew Thompson /* 232602ac6454SAndrew Thompson * Caller supplied explicit parameters to use in 232702ac6454SAndrew Thompson * sending the frame. 232802ac6454SAndrew Thompson */ 232902ac6454SAndrew Thompson if (ural_tx_raw(sc, m, ni, params) != 0) 233002ac6454SAndrew Thompson goto bad; 233102ac6454SAndrew Thompson } 233202ac6454SAndrew Thompson RAL_UNLOCK(sc); 233302ac6454SAndrew Thompson return 0; 233402ac6454SAndrew Thompson bad: 233502ac6454SAndrew Thompson ifp->if_oerrors++; 233602ac6454SAndrew Thompson RAL_UNLOCK(sc); 233702ac6454SAndrew Thompson ieee80211_free_node(ni); 233802ac6454SAndrew Thompson return EIO; /* XXX */ 233902ac6454SAndrew Thompson } 234002ac6454SAndrew Thompson 234102ac6454SAndrew Thompson static void 234202ac6454SAndrew Thompson ural_amrr_start(struct ural_softc *sc, struct ieee80211_node *ni) 234302ac6454SAndrew Thompson { 234402ac6454SAndrew Thompson struct ieee80211vap *vap = ni->ni_vap; 234502ac6454SAndrew Thompson struct ural_vap *uvp = URAL_VAP(vap); 234602ac6454SAndrew Thompson 234702ac6454SAndrew Thompson /* clear statistic registers (STA_CSR0 to STA_CSR10) */ 234802ac6454SAndrew Thompson ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof sc->sta); 234902ac6454SAndrew Thompson 235002ac6454SAndrew Thompson ieee80211_amrr_node_init(&uvp->amrr, &URAL_NODE(ni)->amn, ni); 235102ac6454SAndrew Thompson 235202ac6454SAndrew Thompson usb2_callout_reset(&uvp->amrr_ch, hz, ural_amrr_timeout, uvp); 235302ac6454SAndrew Thompson } 235402ac6454SAndrew Thompson 235502ac6454SAndrew Thompson static void 235602ac6454SAndrew Thompson ural_amrr_timeout(void *arg) 235702ac6454SAndrew Thompson { 235802ac6454SAndrew Thompson struct ural_vap *uvp = arg; 235902ac6454SAndrew Thompson struct ural_softc *sc = uvp->sc; 236002ac6454SAndrew Thompson 236102ac6454SAndrew Thompson ural_queue_command(sc, ural_amrr_task, 236202ac6454SAndrew Thompson &uvp->amrr_task[0].hdr, &uvp->amrr_task[1].hdr); 236302ac6454SAndrew Thompson } 236402ac6454SAndrew Thompson 236502ac6454SAndrew Thompson static void 236602ac6454SAndrew Thompson ural_amrr_task(struct usb2_proc_msg *pm) 236702ac6454SAndrew Thompson { 236802ac6454SAndrew Thompson struct ural_task *task = (struct ural_task *)pm; 236902ac6454SAndrew Thompson struct ural_softc *sc = task->sc; 237002ac6454SAndrew Thompson struct ifnet *ifp = sc->sc_ifp; 237102ac6454SAndrew Thompson struct ieee80211com *ic = ifp->if_l2com; 237202ac6454SAndrew Thompson struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 237302ac6454SAndrew Thompson struct ural_vap *uvp = URAL_VAP(vap); 237402ac6454SAndrew Thompson struct ieee80211_node *ni = vap->iv_bss; 237502ac6454SAndrew Thompson int ok, fail; 237602ac6454SAndrew Thompson 237702ac6454SAndrew Thompson /* read and clear statistic registers (STA_CSR0 to STA_CSR10) */ 237802ac6454SAndrew Thompson ural_read_multi(sc, RAL_STA_CSR0, sc->sta, sizeof(sc->sta)); 237902ac6454SAndrew Thompson 238002ac6454SAndrew Thompson ok = sc->sta[7] + /* TX ok w/o retry */ 238102ac6454SAndrew Thompson sc->sta[8]; /* TX ok w/ retry */ 238202ac6454SAndrew Thompson fail = sc->sta[9]; /* TX retry-fail count */ 238302ac6454SAndrew Thompson 238402ac6454SAndrew Thompson ieee80211_amrr_tx_update(&URAL_NODE(ni)->amn, 238502ac6454SAndrew Thompson ok+fail, ok, sc->sta[8] + fail); 238602ac6454SAndrew Thompson (void) ieee80211_amrr_choose(ni, &URAL_NODE(ni)->amn); 238702ac6454SAndrew Thompson 238802ac6454SAndrew Thompson ifp->if_oerrors += fail; /* count TX retry-fail as Tx errors */ 238902ac6454SAndrew Thompson 239002ac6454SAndrew Thompson usb2_callout_reset(&uvp->amrr_ch, hz, ural_amrr_timeout, uvp); 239102ac6454SAndrew Thompson } 239202ac6454SAndrew Thompson 239302ac6454SAndrew Thompson static int 239402ac6454SAndrew Thompson ural_pause(struct ural_softc *sc, int timeout) 239502ac6454SAndrew Thompson { 239602ac6454SAndrew Thompson if (usb2_proc_is_gone(&sc->sc_tq)) 239702ac6454SAndrew Thompson return (1); 239802ac6454SAndrew Thompson 239902ac6454SAndrew Thompson usb2_pause_mtx(&sc->sc_mtx, timeout); 240002ac6454SAndrew Thompson return (0); 240102ac6454SAndrew Thompson } 240202ac6454SAndrew Thompson 240302ac6454SAndrew Thompson static void 240477ddf3d3SAndrew Thompson ural_command_wrapper(struct usb2_proc_msg *pm) 240577ddf3d3SAndrew Thompson { 240677ddf3d3SAndrew Thompson struct ural_task *task = (struct ural_task *)pm; 240777ddf3d3SAndrew Thompson struct ural_softc *sc = task->sc; 240877ddf3d3SAndrew Thompson struct ifnet *ifp; 240977ddf3d3SAndrew Thompson 241077ddf3d3SAndrew Thompson /* wait for pending transfer, if any */ 241177ddf3d3SAndrew Thompson while (usb2_transfer_pending(sc->sc_xfer[URAL_BULK_WR])) 241277ddf3d3SAndrew Thompson cv_wait(&sc->sc_cmd_cv, &sc->sc_mtx); 241377ddf3d3SAndrew Thompson 241477ddf3d3SAndrew Thompson /* make sure any hardware FIFOs are emptied */ 241577ddf3d3SAndrew Thompson ural_pause(sc, hz / 1000); 241677ddf3d3SAndrew Thompson 241777ddf3d3SAndrew Thompson /* execute task */ 241877ddf3d3SAndrew Thompson task->func(pm); 241977ddf3d3SAndrew Thompson 242077ddf3d3SAndrew Thompson /* check if this is the last task executed */ 242177ddf3d3SAndrew Thompson if (sc->sc_last_task == task) { 242277ddf3d3SAndrew Thompson sc->sc_last_task = NULL; 242377ddf3d3SAndrew Thompson ifp = sc->sc_ifp; 242477ddf3d3SAndrew Thompson /* re-start TX, if any */ 242577ddf3d3SAndrew Thompson if ((ifp != NULL) && (ifp->if_drv_flags & IFF_DRV_RUNNING)) 242677ddf3d3SAndrew Thompson usb2_transfer_start(sc->sc_xfer[URAL_BULK_WR]); 242777ddf3d3SAndrew Thompson } 242877ddf3d3SAndrew Thompson } 242977ddf3d3SAndrew Thompson 243077ddf3d3SAndrew Thompson static void 243102ac6454SAndrew Thompson ural_queue_command(struct ural_softc *sc, usb2_proc_callback_t *fn, 243202ac6454SAndrew Thompson struct usb2_proc_msg *t0, struct usb2_proc_msg *t1) 243302ac6454SAndrew Thompson { 243402ac6454SAndrew Thompson struct ural_task *task; 243502ac6454SAndrew Thompson 243602ac6454SAndrew Thompson RAL_LOCK_ASSERT(sc, MA_OWNED); 243702ac6454SAndrew Thompson 243802ac6454SAndrew Thompson /* 243902ac6454SAndrew Thompson * NOTE: The task cannot get executed before we drop the 244002ac6454SAndrew Thompson * "sc_mtx" mutex. It is safe to update fields in the message 244102ac6454SAndrew Thompson * structure after that the message got queued. 244202ac6454SAndrew Thompson */ 244302ac6454SAndrew Thompson task = (struct ural_task *) 244402ac6454SAndrew Thompson usb2_proc_msignal(&sc->sc_tq, t0, t1); 244502ac6454SAndrew Thompson 244602ac6454SAndrew Thompson /* Setup callback and softc pointers */ 244777ddf3d3SAndrew Thompson task->hdr.pm_callback = ural_command_wrapper; 244877ddf3d3SAndrew Thompson task->func = fn; 244902ac6454SAndrew Thompson task->sc = sc; 245002ac6454SAndrew Thompson 245177ddf3d3SAndrew Thompson /* Make sure that any TX operation will stop */ 245277ddf3d3SAndrew Thompson sc->sc_last_task = task; 245377ddf3d3SAndrew Thompson 245402ac6454SAndrew Thompson /* 245577ddf3d3SAndrew Thompson * Init, stop and flush must be synchronous! 245602ac6454SAndrew Thompson */ 245777ddf3d3SAndrew Thompson if ((fn == ural_init_task) || (fn == ural_stop_task) || 245877ddf3d3SAndrew Thompson (fn == ural_stop_task)) 245902ac6454SAndrew Thompson usb2_proc_mwait(&sc->sc_tq, t0, t1); 246002ac6454SAndrew Thompson } 2461