if_run.c revision 243857
1203134Sthompsa/*- 2205042Sthompsa * Copyright (c) 2008,2010 Damien Bergamini <damien.bergamini@free.fr> 3205042Sthompsa * ported to FreeBSD by Akinori Furukoshi <moonlightakkiy@yahoo.ca> 4205042Sthompsa * USB Consulting, Hans Petter Selasky <hselasky@freebsd.org> 5203134Sthompsa * 6203134Sthompsa * Permission to use, copy, modify, and distribute this software for any 7203134Sthompsa * purpose with or without fee is hereby granted, provided that the above 8203134Sthompsa * copyright notice and this permission notice appear in all copies. 9203134Sthompsa * 10203134Sthompsa * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11203134Sthompsa * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12203134Sthompsa * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13203134Sthompsa * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14203134Sthompsa * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15203134Sthompsa * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16203134Sthompsa * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17203134Sthompsa */ 18203134Sthompsa 19203134Sthompsa#include <sys/cdefs.h> 20203134Sthompsa__FBSDID("$FreeBSD: head/sys/dev/usb/wlan/if_run.c 243857 2012-12-04 09:32:43Z glebius $"); 21203134Sthompsa 22203134Sthompsa/*- 23203134Sthompsa * Ralink Technology RT2700U/RT2800U/RT3000U chipset driver. 24203134Sthompsa * http://www.ralinktech.com/ 25203134Sthompsa */ 26203134Sthompsa 27203134Sthompsa#include <sys/param.h> 28203134Sthompsa#include <sys/sockio.h> 29203134Sthompsa#include <sys/sysctl.h> 30203134Sthompsa#include <sys/lock.h> 31203134Sthompsa#include <sys/mutex.h> 32203134Sthompsa#include <sys/mbuf.h> 33203134Sthompsa#include <sys/kernel.h> 34203134Sthompsa#include <sys/socket.h> 35203134Sthompsa#include <sys/systm.h> 36203134Sthompsa#include <sys/malloc.h> 37203134Sthompsa#include <sys/module.h> 38203134Sthompsa#include <sys/bus.h> 39203134Sthompsa#include <sys/endian.h> 40203134Sthompsa#include <sys/linker.h> 41203134Sthompsa#include <sys/firmware.h> 42203134Sthompsa#include <sys/kdb.h> 43203134Sthompsa 44203134Sthompsa#include <machine/bus.h> 45203134Sthompsa#include <machine/resource.h> 46203134Sthompsa#include <sys/rman.h> 47203134Sthompsa 48203134Sthompsa#include <net/bpf.h> 49203134Sthompsa#include <net/if.h> 50203134Sthompsa#include <net/if_arp.h> 51203134Sthompsa#include <net/ethernet.h> 52203134Sthompsa#include <net/if_dl.h> 53203134Sthompsa#include <net/if_media.h> 54203134Sthompsa#include <net/if_types.h> 55203134Sthompsa 56203134Sthompsa#include <netinet/in.h> 57203134Sthompsa#include <netinet/in_systm.h> 58203134Sthompsa#include <netinet/in_var.h> 59203134Sthompsa#include <netinet/if_ether.h> 60203134Sthompsa#include <netinet/ip.h> 61203134Sthompsa 62203134Sthompsa#include <net80211/ieee80211_var.h> 63203134Sthompsa#include <net80211/ieee80211_regdomain.h> 64203134Sthompsa#include <net80211/ieee80211_radiotap.h> 65206358Srpaulo#include <net80211/ieee80211_ratectl.h> 66203134Sthompsa 67203134Sthompsa#include <dev/usb/usb.h> 68203134Sthompsa#include <dev/usb/usbdi.h> 69203134Sthompsa#include "usbdevs.h" 70203134Sthompsa 71203134Sthompsa#define USB_DEBUG_VAR run_debug 72203134Sthompsa#include <dev/usb/usb_debug.h> 73203134Sthompsa 74220235Skevlo#include <dev/usb/wlan/if_runreg.h> 75220235Skevlo#include <dev/usb/wlan/if_runvar.h> 76203134Sthompsa 77233774Shselasky#define N(_a) ((int)(sizeof((_a)) / sizeof((_a)[0]))) 78203134Sthompsa 79207077Sthompsa#ifdef USB_DEBUG 80203134Sthompsa#define RUN_DEBUG 81203134Sthompsa#endif 82203134Sthompsa 83203134Sthompsa#ifdef RUN_DEBUG 84203134Sthompsaint run_debug = 0; 85227309Sedstatic SYSCTL_NODE(_hw_usb, OID_AUTO, run, CTLFLAG_RW, 0, "USB run"); 86203134SthompsaSYSCTL_INT(_hw_usb_run, OID_AUTO, debug, CTLFLAG_RW, &run_debug, 0, 87203134Sthompsa "run debug level"); 88203134Sthompsa#endif 89203134Sthompsa 90203134Sthompsa#define IEEE80211_HAS_ADDR4(wh) \ 91203134Sthompsa (((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS) 92203134Sthompsa 93208019Sthompsa/* 94208019Sthompsa * Because of LOR in run_key_delete(), use atomic instead. 95208019Sthompsa * '& RUN_CMDQ_MASQ' is to loop cmdq[]. 96208019Sthompsa */ 97208019Sthompsa#define RUN_CMDQ_GET(c) (atomic_fetchadd_32((c), 1) & RUN_CMDQ_MASQ) 98208019Sthompsa 99223486Shselaskystatic const STRUCT_USB_HOST_ID run_devs[] = { 100209918Sthompsa#define RUN_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) } 101209918Sthompsa RUN_DEV(ABOCOM, RT2770), 102209918Sthompsa RUN_DEV(ABOCOM, RT2870), 103209918Sthompsa RUN_DEV(ABOCOM, RT3070), 104209918Sthompsa RUN_DEV(ABOCOM, RT3071), 105209918Sthompsa RUN_DEV(ABOCOM, RT3072), 106209918Sthompsa RUN_DEV(ABOCOM2, RT2870_1), 107209918Sthompsa RUN_DEV(ACCTON, RT2770), 108209918Sthompsa RUN_DEV(ACCTON, RT2870_1), 109209918Sthompsa RUN_DEV(ACCTON, RT2870_2), 110209918Sthompsa RUN_DEV(ACCTON, RT2870_3), 111209918Sthompsa RUN_DEV(ACCTON, RT2870_4), 112209918Sthompsa RUN_DEV(ACCTON, RT2870_5), 113209918Sthompsa RUN_DEV(ACCTON, RT3070), 114209918Sthompsa RUN_DEV(ACCTON, RT3070_1), 115209918Sthompsa RUN_DEV(ACCTON, RT3070_2), 116209918Sthompsa RUN_DEV(ACCTON, RT3070_3), 117209918Sthompsa RUN_DEV(ACCTON, RT3070_4), 118209918Sthompsa RUN_DEV(ACCTON, RT3070_5), 119209918Sthompsa RUN_DEV(AIRTIES, RT3070), 120209918Sthompsa RUN_DEV(ALLWIN, RT2070), 121209918Sthompsa RUN_DEV(ALLWIN, RT2770), 122209918Sthompsa RUN_DEV(ALLWIN, RT2870), 123209918Sthompsa RUN_DEV(ALLWIN, RT3070), 124209918Sthompsa RUN_DEV(ALLWIN, RT3071), 125209918Sthompsa RUN_DEV(ALLWIN, RT3072), 126209918Sthompsa RUN_DEV(ALLWIN, RT3572), 127209918Sthompsa RUN_DEV(AMIGO, RT2870_1), 128209918Sthompsa RUN_DEV(AMIGO, RT2870_2), 129209918Sthompsa RUN_DEV(AMIT, CGWLUSB2GNR), 130209918Sthompsa RUN_DEV(AMIT, RT2870_1), 131209918Sthompsa RUN_DEV(AMIT2, RT2870), 132209918Sthompsa RUN_DEV(ASUS, RT2870_1), 133209918Sthompsa RUN_DEV(ASUS, RT2870_2), 134209918Sthompsa RUN_DEV(ASUS, RT2870_3), 135209918Sthompsa RUN_DEV(ASUS, RT2870_4), 136209918Sthompsa RUN_DEV(ASUS, RT2870_5), 137209918Sthompsa RUN_DEV(ASUS, USBN13), 138209918Sthompsa RUN_DEV(ASUS, RT3070_1), 139239358Shselasky RUN_DEV(ASUS, USB_N53), 140209918Sthompsa RUN_DEV(ASUS2, USBN11), 141209918Sthompsa RUN_DEV(AZUREWAVE, RT2870_1), 142209918Sthompsa RUN_DEV(AZUREWAVE, RT2870_2), 143209918Sthompsa RUN_DEV(AZUREWAVE, RT3070_1), 144209918Sthompsa RUN_DEV(AZUREWAVE, RT3070_2), 145209918Sthompsa RUN_DEV(AZUREWAVE, RT3070_3), 146209918Sthompsa RUN_DEV(BELKIN, F5D8053V3), 147209918Sthompsa RUN_DEV(BELKIN, F5D8055), 148226534Shselasky RUN_DEV(BELKIN, F5D8055V2), 149209918Sthompsa RUN_DEV(BELKIN, F6D4050V1), 150209918Sthompsa RUN_DEV(BELKIN, RT2870_1), 151209918Sthompsa RUN_DEV(BELKIN, RT2870_2), 152226534Shselasky RUN_DEV(CISCOLINKSYS, AE1000), 153209918Sthompsa RUN_DEV(CISCOLINKSYS2, RT3070), 154209918Sthompsa RUN_DEV(CISCOLINKSYS3, RT3070), 155209918Sthompsa RUN_DEV(CONCEPTRONIC2, RT2870_1), 156209918Sthompsa RUN_DEV(CONCEPTRONIC2, RT2870_2), 157209918Sthompsa RUN_DEV(CONCEPTRONIC2, RT2870_3), 158209918Sthompsa RUN_DEV(CONCEPTRONIC2, RT2870_4), 159209918Sthompsa RUN_DEV(CONCEPTRONIC2, RT2870_5), 160209918Sthompsa RUN_DEV(CONCEPTRONIC2, RT2870_6), 161209918Sthompsa RUN_DEV(CONCEPTRONIC2, RT2870_7), 162209918Sthompsa RUN_DEV(CONCEPTRONIC2, RT2870_8), 163209918Sthompsa RUN_DEV(CONCEPTRONIC2, RT3070_1), 164209918Sthompsa RUN_DEV(CONCEPTRONIC2, RT3070_2), 165209918Sthompsa RUN_DEV(CONCEPTRONIC2, VIGORN61), 166209918Sthompsa RUN_DEV(COREGA, CGWLUSB300GNM), 167209918Sthompsa RUN_DEV(COREGA, RT2870_1), 168209918Sthompsa RUN_DEV(COREGA, RT2870_2), 169209918Sthompsa RUN_DEV(COREGA, RT2870_3), 170209918Sthompsa RUN_DEV(COREGA, RT3070), 171209918Sthompsa RUN_DEV(CYBERTAN, RT2870), 172209918Sthompsa RUN_DEV(DLINK, RT2870), 173209918Sthompsa RUN_DEV(DLINK, RT3072), 174209918Sthompsa RUN_DEV(DLINK2, DWA130), 175209918Sthompsa RUN_DEV(DLINK2, RT2870_1), 176209918Sthompsa RUN_DEV(DLINK2, RT2870_2), 177209918Sthompsa RUN_DEV(DLINK2, RT3070_1), 178209918Sthompsa RUN_DEV(DLINK2, RT3070_2), 179209918Sthompsa RUN_DEV(DLINK2, RT3070_3), 180209918Sthompsa RUN_DEV(DLINK2, RT3070_4), 181209918Sthompsa RUN_DEV(DLINK2, RT3070_5), 182209918Sthompsa RUN_DEV(DLINK2, RT3072), 183209918Sthompsa RUN_DEV(DLINK2, RT3072_1), 184209918Sthompsa RUN_DEV(EDIMAX, EW7717), 185209918Sthompsa RUN_DEV(EDIMAX, EW7718), 186209918Sthompsa RUN_DEV(EDIMAX, RT2870_1), 187209918Sthompsa RUN_DEV(ENCORE, RT3070_1), 188209918Sthompsa RUN_DEV(ENCORE, RT3070_2), 189209918Sthompsa RUN_DEV(ENCORE, RT3070_3), 190209918Sthompsa RUN_DEV(GIGABYTE, GNWB31N), 191209918Sthompsa RUN_DEV(GIGABYTE, GNWB32L), 192209918Sthompsa RUN_DEV(GIGABYTE, RT2870_1), 193209918Sthompsa RUN_DEV(GIGASET, RT3070_1), 194209918Sthompsa RUN_DEV(GIGASET, RT3070_2), 195209918Sthompsa RUN_DEV(GUILLEMOT, HWNU300), 196209918Sthompsa RUN_DEV(HAWKING, HWUN2), 197209918Sthompsa RUN_DEV(HAWKING, RT2870_1), 198209918Sthompsa RUN_DEV(HAWKING, RT2870_2), 199209918Sthompsa RUN_DEV(HAWKING, RT3070), 200209918Sthompsa RUN_DEV(IODATA, RT3072_1), 201209918Sthompsa RUN_DEV(IODATA, RT3072_2), 202209918Sthompsa RUN_DEV(IODATA, RT3072_3), 203209918Sthompsa RUN_DEV(IODATA, RT3072_4), 204209918Sthompsa RUN_DEV(LINKSYS4, RT3070), 205209918Sthompsa RUN_DEV(LINKSYS4, WUSB100), 206209918Sthompsa RUN_DEV(LINKSYS4, WUSB54GCV3), 207209918Sthompsa RUN_DEV(LINKSYS4, WUSB600N), 208209918Sthompsa RUN_DEV(LINKSYS4, WUSB600NV2), 209209918Sthompsa RUN_DEV(LOGITEC, RT2870_1), 210209918Sthompsa RUN_DEV(LOGITEC, RT2870_2), 211209918Sthompsa RUN_DEV(LOGITEC, RT2870_3), 212230333Shselasky RUN_DEV(LOGITEC, LANW300NU2), 213238274Shrs RUN_DEV(LOGITEC, LANW150NU2), 214209918Sthompsa RUN_DEV(MELCO, RT2870_1), 215209918Sthompsa RUN_DEV(MELCO, RT2870_2), 216209918Sthompsa RUN_DEV(MELCO, WLIUCAG300N), 217209918Sthompsa RUN_DEV(MELCO, WLIUCG300N), 218219257Sdaichi RUN_DEV(MELCO, WLIUCG301N), 219209918Sthompsa RUN_DEV(MELCO, WLIUCGN), 220227781Shselasky RUN_DEV(MELCO, WLIUCGNM), 221238274Shrs RUN_DEV(MELCO, WLIUCGNM2), 222209918Sthompsa RUN_DEV(MOTOROLA4, RT2770), 223209918Sthompsa RUN_DEV(MOTOROLA4, RT3070), 224209918Sthompsa RUN_DEV(MSI, RT3070_1), 225209918Sthompsa RUN_DEV(MSI, RT3070_2), 226209918Sthompsa RUN_DEV(MSI, RT3070_3), 227209918Sthompsa RUN_DEV(MSI, RT3070_4), 228209918Sthompsa RUN_DEV(MSI, RT3070_5), 229209918Sthompsa RUN_DEV(MSI, RT3070_6), 230209918Sthompsa RUN_DEV(MSI, RT3070_7), 231209918Sthompsa RUN_DEV(MSI, RT3070_8), 232209918Sthompsa RUN_DEV(MSI, RT3070_9), 233209918Sthompsa RUN_DEV(MSI, RT3070_10), 234209918Sthompsa RUN_DEV(MSI, RT3070_11), 235209918Sthompsa RUN_DEV(OVISLINK, RT3072), 236209918Sthompsa RUN_DEV(PARA, RT3070), 237209918Sthompsa RUN_DEV(PEGATRON, RT2870), 238209918Sthompsa RUN_DEV(PEGATRON, RT3070), 239209918Sthompsa RUN_DEV(PEGATRON, RT3070_2), 240209918Sthompsa RUN_DEV(PEGATRON, RT3070_3), 241209918Sthompsa RUN_DEV(PHILIPS, RT2870), 242209918Sthompsa RUN_DEV(PLANEX2, GWUS300MINIS), 243209918Sthompsa RUN_DEV(PLANEX2, GWUSMICRON), 244209918Sthompsa RUN_DEV(PLANEX2, RT2870), 245209918Sthompsa RUN_DEV(PLANEX2, RT3070), 246209918Sthompsa RUN_DEV(QCOM, RT2870), 247209918Sthompsa RUN_DEV(QUANTA, RT3070), 248209918Sthompsa RUN_DEV(RALINK, RT2070), 249209918Sthompsa RUN_DEV(RALINK, RT2770), 250209918Sthompsa RUN_DEV(RALINK, RT2870), 251209918Sthompsa RUN_DEV(RALINK, RT3070), 252209918Sthompsa RUN_DEV(RALINK, RT3071), 253209918Sthompsa RUN_DEV(RALINK, RT3072), 254209918Sthompsa RUN_DEV(RALINK, RT3370), 255209918Sthompsa RUN_DEV(RALINK, RT3572), 256209918Sthompsa RUN_DEV(RALINK, RT8070), 257226534Shselasky RUN_DEV(SAMSUNG, WIS09ABGN), 258209918Sthompsa RUN_DEV(SAMSUNG2, RT2870_1), 259209918Sthompsa RUN_DEV(SENAO, RT2870_1), 260209918Sthompsa RUN_DEV(SENAO, RT2870_2), 261209918Sthompsa RUN_DEV(SENAO, RT2870_3), 262209918Sthompsa RUN_DEV(SENAO, RT2870_4), 263209918Sthompsa RUN_DEV(SENAO, RT3070), 264209918Sthompsa RUN_DEV(SENAO, RT3071), 265209918Sthompsa RUN_DEV(SENAO, RT3072_1), 266209918Sthompsa RUN_DEV(SENAO, RT3072_2), 267209918Sthompsa RUN_DEV(SENAO, RT3072_3), 268209918Sthompsa RUN_DEV(SENAO, RT3072_4), 269209918Sthompsa RUN_DEV(SENAO, RT3072_5), 270209918Sthompsa RUN_DEV(SITECOMEU, RT2770), 271209918Sthompsa RUN_DEV(SITECOMEU, RT2870_1), 272209918Sthompsa RUN_DEV(SITECOMEU, RT2870_2), 273209918Sthompsa RUN_DEV(SITECOMEU, RT2870_3), 274209918Sthompsa RUN_DEV(SITECOMEU, RT2870_4), 275209918Sthompsa RUN_DEV(SITECOMEU, RT3070), 276209918Sthompsa RUN_DEV(SITECOMEU, RT3070_2), 277209918Sthompsa RUN_DEV(SITECOMEU, RT3070_3), 278209918Sthompsa RUN_DEV(SITECOMEU, RT3070_4), 279209918Sthompsa RUN_DEV(SITECOMEU, RT3071), 280209918Sthompsa RUN_DEV(SITECOMEU, RT3072_1), 281209918Sthompsa RUN_DEV(SITECOMEU, RT3072_2), 282209918Sthompsa RUN_DEV(SITECOMEU, RT3072_3), 283209918Sthompsa RUN_DEV(SITECOMEU, RT3072_4), 284209918Sthompsa RUN_DEV(SITECOMEU, RT3072_5), 285209918Sthompsa RUN_DEV(SITECOMEU, RT3072_6), 286209918Sthompsa RUN_DEV(SITECOMEU, WL608), 287209918Sthompsa RUN_DEV(SPARKLAN, RT2870_1), 288209918Sthompsa RUN_DEV(SPARKLAN, RT3070), 289209918Sthompsa RUN_DEV(SWEEX2, LW153), 290209918Sthompsa RUN_DEV(SWEEX2, LW303), 291209918Sthompsa RUN_DEV(SWEEX2, LW313), 292209918Sthompsa RUN_DEV(TOSHIBA, RT3070), 293209918Sthompsa RUN_DEV(UMEDIA, RT2870_1), 294209918Sthompsa RUN_DEV(ZCOM, RT2870_1), 295209918Sthompsa RUN_DEV(ZCOM, RT2870_2), 296209918Sthompsa RUN_DEV(ZINWELL, RT2870_1), 297209918Sthompsa RUN_DEV(ZINWELL, RT2870_2), 298209918Sthompsa RUN_DEV(ZINWELL, RT3070), 299209918Sthompsa RUN_DEV(ZINWELL, RT3072_1), 300209918Sthompsa RUN_DEV(ZINWELL, RT3072_2), 301209918Sthompsa RUN_DEV(ZYXEL, RT2870_1), 302209918Sthompsa RUN_DEV(ZYXEL, RT2870_2), 303209918Sthompsa#undef RUN_DEV 304203134Sthompsa}; 305203134Sthompsa 306203134Sthompsastatic device_probe_t run_match; 307203134Sthompsastatic device_attach_t run_attach; 308203134Sthompsastatic device_detach_t run_detach; 309203134Sthompsa 310203134Sthompsastatic usb_callback_t run_bulk_rx_callback; 311203134Sthompsastatic usb_callback_t run_bulk_tx_callback0; 312203134Sthompsastatic usb_callback_t run_bulk_tx_callback1; 313203134Sthompsastatic usb_callback_t run_bulk_tx_callback2; 314203134Sthompsastatic usb_callback_t run_bulk_tx_callback3; 315203134Sthompsastatic usb_callback_t run_bulk_tx_callback4; 316203134Sthompsastatic usb_callback_t run_bulk_tx_callback5; 317203134Sthompsa 318203134Sthompsastatic void run_bulk_tx_callbackN(struct usb_xfer *xfer, 319203134Sthompsa usb_error_t error, unsigned int index); 320203134Sthompsastatic struct ieee80211vap *run_vap_create(struct ieee80211com *, 321228621Sbschmidt const char [IFNAMSIZ], int, enum ieee80211_opmode, int, 322228621Sbschmidt const uint8_t [IEEE80211_ADDR_LEN], 323228621Sbschmidt const uint8_t [IEEE80211_ADDR_LEN]); 324203134Sthompsastatic void run_vap_delete(struct ieee80211vap *); 325208019Sthompsastatic void run_cmdq_cb(void *, int); 326203134Sthompsastatic void run_setup_tx_list(struct run_softc *, 327203134Sthompsa struct run_endpoint_queue *); 328203134Sthompsastatic void run_unsetup_tx_list(struct run_softc *, 329203134Sthompsa struct run_endpoint_queue *); 330203134Sthompsastatic int run_load_microcode(struct run_softc *); 331203134Sthompsastatic int run_reset(struct run_softc *); 332203134Sthompsastatic usb_error_t run_do_request(struct run_softc *, 333203134Sthompsa struct usb_device_request *, void *); 334203134Sthompsastatic int run_read(struct run_softc *, uint16_t, uint32_t *); 335203134Sthompsastatic int run_read_region_1(struct run_softc *, uint16_t, uint8_t *, int); 336203134Sthompsastatic int run_write_2(struct run_softc *, uint16_t, uint16_t); 337203134Sthompsastatic int run_write(struct run_softc *, uint16_t, uint32_t); 338203134Sthompsastatic int run_write_region_1(struct run_softc *, uint16_t, 339203134Sthompsa const uint8_t *, int); 340203134Sthompsastatic int run_set_region_4(struct run_softc *, uint16_t, uint32_t, int); 341203134Sthompsastatic int run_efuse_read_2(struct run_softc *, uint16_t, uint16_t *); 342203134Sthompsastatic int run_eeprom_read_2(struct run_softc *, uint16_t, uint16_t *); 343203134Sthompsastatic int run_rt2870_rf_write(struct run_softc *, uint8_t, uint32_t); 344203134Sthompsastatic int run_rt3070_rf_read(struct run_softc *, uint8_t, uint8_t *); 345203134Sthompsastatic int run_rt3070_rf_write(struct run_softc *, uint8_t, uint8_t); 346203134Sthompsastatic int run_bbp_read(struct run_softc *, uint8_t, uint8_t *); 347203134Sthompsastatic int run_bbp_write(struct run_softc *, uint8_t, uint8_t); 348203134Sthompsastatic int run_mcu_cmd(struct run_softc *, uint8_t, uint16_t); 349203134Sthompsastatic const char *run_get_rf(int); 350203134Sthompsastatic int run_read_eeprom(struct run_softc *); 351203134Sthompsastatic struct ieee80211_node *run_node_alloc(struct ieee80211vap *, 352203134Sthompsa const uint8_t mac[IEEE80211_ADDR_LEN]); 353203134Sthompsastatic int run_media_change(struct ifnet *); 354203134Sthompsastatic int run_newstate(struct ieee80211vap *, enum ieee80211_state, int); 355203134Sthompsastatic int run_wme_update(struct ieee80211com *); 356208019Sthompsastatic void run_wme_update_cb(void *); 357203134Sthompsastatic void run_key_update_begin(struct ieee80211vap *); 358203134Sthompsastatic void run_key_update_end(struct ieee80211vap *); 359208019Sthompsastatic void run_key_set_cb(void *); 360208019Sthompsastatic int run_key_set(struct ieee80211vap *, struct ieee80211_key *, 361203134Sthompsa const uint8_t mac[IEEE80211_ADDR_LEN]); 362208019Sthompsastatic void run_key_delete_cb(void *); 363208019Sthompsastatic int run_key_delete(struct ieee80211vap *, struct ieee80211_key *); 364206358Srpaulostatic void run_ratectl_to(void *); 365206358Srpaulostatic void run_ratectl_cb(void *, int); 366208019Sthompsastatic void run_drain_fifo(void *); 367203134Sthompsastatic void run_iter_func(void *, struct ieee80211_node *); 368208019Sthompsastatic void run_newassoc_cb(void *); 369203134Sthompsastatic void run_newassoc(struct ieee80211_node *, int); 370203134Sthompsastatic void run_rx_frame(struct run_softc *, struct mbuf *, uint32_t); 371203134Sthompsastatic void run_tx_free(struct run_endpoint_queue *pq, 372203134Sthompsa struct run_tx_data *, int); 373208019Sthompsastatic void run_set_tx_desc(struct run_softc *, struct run_tx_data *); 374203134Sthompsastatic int run_tx(struct run_softc *, struct mbuf *, 375203134Sthompsa struct ieee80211_node *); 376203134Sthompsastatic int run_tx_mgt(struct run_softc *, struct mbuf *, 377203134Sthompsa struct ieee80211_node *); 378203134Sthompsastatic int run_sendprot(struct run_softc *, const struct mbuf *, 379203134Sthompsa struct ieee80211_node *, int, int); 380203134Sthompsastatic int run_tx_param(struct run_softc *, struct mbuf *, 381203134Sthompsa struct ieee80211_node *, 382203134Sthompsa const struct ieee80211_bpf_params *); 383203134Sthompsastatic int run_raw_xmit(struct ieee80211_node *, struct mbuf *, 384203134Sthompsa const struct ieee80211_bpf_params *); 385203134Sthompsastatic void run_start(struct ifnet *); 386203134Sthompsastatic int run_ioctl(struct ifnet *, u_long, caddr_t); 387205042Sthompsastatic void run_set_agc(struct run_softc *, uint8_t); 388203134Sthompsastatic void run_select_chan_group(struct run_softc *, int); 389203134Sthompsastatic void run_set_rx_antenna(struct run_softc *, int); 390203134Sthompsastatic void run_rt2870_set_chan(struct run_softc *, u_int); 391203134Sthompsastatic void run_rt3070_set_chan(struct run_softc *, u_int); 392205042Sthompsastatic void run_rt3572_set_chan(struct run_softc *, u_int); 393203134Sthompsastatic int run_set_chan(struct run_softc *, struct ieee80211_channel *); 394203134Sthompsastatic void run_set_channel(struct ieee80211com *); 395203134Sthompsastatic void run_scan_start(struct ieee80211com *); 396203134Sthompsastatic void run_scan_end(struct ieee80211com *); 397203134Sthompsastatic void run_update_beacon(struct ieee80211vap *, int); 398208019Sthompsastatic void run_update_beacon_cb(void *); 399203134Sthompsastatic void run_updateprot(struct ieee80211com *); 400218492Sbschmidtstatic void run_updateprot_cb(void *); 401208019Sthompsastatic void run_usb_timeout_cb(void *); 402203134Sthompsastatic void run_reset_livelock(struct run_softc *); 403203134Sthompsastatic void run_enable_tsf_sync(struct run_softc *); 404203134Sthompsastatic void run_enable_mrr(struct run_softc *); 405203134Sthompsastatic void run_set_txpreamble(struct run_softc *); 406203134Sthompsastatic void run_set_basicrates(struct run_softc *); 407203134Sthompsastatic void run_set_leds(struct run_softc *, uint16_t); 408203134Sthompsastatic void run_set_bssid(struct run_softc *, const uint8_t *); 409203134Sthompsastatic void run_set_macaddr(struct run_softc *, const uint8_t *); 410203134Sthompsastatic void run_updateslot(struct ifnet *); 411218492Sbschmidtstatic void run_updateslot_cb(void *); 412208019Sthompsastatic void run_update_mcast(struct ifnet *); 413203134Sthompsastatic int8_t run_rssi2dbm(struct run_softc *, uint8_t, uint8_t); 414203134Sthompsastatic void run_update_promisc_locked(struct ifnet *); 415203134Sthompsastatic void run_update_promisc(struct ifnet *); 416203134Sthompsastatic int run_bbp_init(struct run_softc *); 417203134Sthompsastatic int run_rt3070_rf_init(struct run_softc *); 418203134Sthompsastatic int run_rt3070_filter_calib(struct run_softc *, uint8_t, uint8_t, 419203134Sthompsa uint8_t *); 420205042Sthompsastatic void run_rt3070_rf_setup(struct run_softc *); 421203134Sthompsastatic int run_txrx_enable(struct run_softc *); 422203134Sthompsastatic void run_init(void *); 423203134Sthompsastatic void run_init_locked(struct run_softc *); 424203134Sthompsastatic void run_stop(void *); 425203134Sthompsastatic void run_delay(struct run_softc *, unsigned int); 426203134Sthompsa 427203134Sthompsastatic const struct { 428208019Sthompsa uint16_t reg; 429203134Sthompsa uint32_t val; 430203134Sthompsa} rt2870_def_mac[] = { 431203134Sthompsa RT2870_DEF_MAC 432203134Sthompsa}; 433203134Sthompsa 434203134Sthompsastatic const struct { 435203134Sthompsa uint8_t reg; 436203134Sthompsa uint8_t val; 437203134Sthompsa} rt2860_def_bbp[] = { 438203134Sthompsa RT2860_DEF_BBP 439203134Sthompsa}; 440203134Sthompsa 441203134Sthompsastatic const struct rfprog { 442203134Sthompsa uint8_t chan; 443203134Sthompsa uint32_t r1, r2, r3, r4; 444203134Sthompsa} rt2860_rf2850[] = { 445203134Sthompsa RT2860_RF2850 446203134Sthompsa}; 447203134Sthompsa 448203134Sthompsastruct { 449203134Sthompsa uint8_t n, r, k; 450205042Sthompsa} rt3070_freqs[] = { 451205042Sthompsa RT3070_RF3052 452203134Sthompsa}; 453203134Sthompsa 454203134Sthompsastatic const struct { 455203134Sthompsa uint8_t reg; 456203134Sthompsa uint8_t val; 457203134Sthompsa} rt3070_def_rf[] = { 458203134Sthompsa RT3070_DEF_RF 459205042Sthompsa},rt3572_def_rf[] = { 460205042Sthompsa RT3572_DEF_RF 461203134Sthompsa}; 462203134Sthompsa 463203134Sthompsastatic const struct usb_config run_config[RUN_N_XFER] = { 464203134Sthompsa [RUN_BULK_TX_BE] = { 465203134Sthompsa .type = UE_BULK, 466203134Sthompsa .endpoint = UE_ADDR_ANY, 467203134Sthompsa .ep_index = 0, 468203134Sthompsa .direction = UE_DIR_OUT, 469203134Sthompsa .bufsize = RUN_MAX_TXSZ, 470203134Sthompsa .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 471203134Sthompsa .callback = run_bulk_tx_callback0, 472203134Sthompsa .timeout = 5000, /* ms */ 473203134Sthompsa }, 474203134Sthompsa [RUN_BULK_TX_BK] = { 475203134Sthompsa .type = UE_BULK, 476203134Sthompsa .endpoint = UE_ADDR_ANY, 477203134Sthompsa .direction = UE_DIR_OUT, 478203134Sthompsa .ep_index = 1, 479203134Sthompsa .bufsize = RUN_MAX_TXSZ, 480203134Sthompsa .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 481203134Sthompsa .callback = run_bulk_tx_callback1, 482203134Sthompsa .timeout = 5000, /* ms */ 483203134Sthompsa }, 484203134Sthompsa [RUN_BULK_TX_VI] = { 485203134Sthompsa .type = UE_BULK, 486203134Sthompsa .endpoint = UE_ADDR_ANY, 487203134Sthompsa .direction = UE_DIR_OUT, 488203134Sthompsa .ep_index = 2, 489203134Sthompsa .bufsize = RUN_MAX_TXSZ, 490203134Sthompsa .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 491203134Sthompsa .callback = run_bulk_tx_callback2, 492203134Sthompsa .timeout = 5000, /* ms */ 493203134Sthompsa }, 494203134Sthompsa [RUN_BULK_TX_VO] = { 495203134Sthompsa .type = UE_BULK, 496203134Sthompsa .endpoint = UE_ADDR_ANY, 497203134Sthompsa .direction = UE_DIR_OUT, 498203134Sthompsa .ep_index = 3, 499203134Sthompsa .bufsize = RUN_MAX_TXSZ, 500203134Sthompsa .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 501203134Sthompsa .callback = run_bulk_tx_callback3, 502203134Sthompsa .timeout = 5000, /* ms */ 503203134Sthompsa }, 504203134Sthompsa [RUN_BULK_TX_HCCA] = { 505203134Sthompsa .type = UE_BULK, 506203134Sthompsa .endpoint = UE_ADDR_ANY, 507203134Sthompsa .direction = UE_DIR_OUT, 508203134Sthompsa .ep_index = 4, 509203134Sthompsa .bufsize = RUN_MAX_TXSZ, 510203134Sthompsa .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,}, 511203134Sthompsa .callback = run_bulk_tx_callback4, 512203134Sthompsa .timeout = 5000, /* ms */ 513203134Sthompsa }, 514203134Sthompsa [RUN_BULK_TX_PRIO] = { 515203134Sthompsa .type = UE_BULK, 516203134Sthompsa .endpoint = UE_ADDR_ANY, 517203134Sthompsa .direction = UE_DIR_OUT, 518203134Sthompsa .ep_index = 5, 519203134Sthompsa .bufsize = RUN_MAX_TXSZ, 520203134Sthompsa .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,}, 521203134Sthompsa .callback = run_bulk_tx_callback5, 522203134Sthompsa .timeout = 5000, /* ms */ 523203134Sthompsa }, 524203134Sthompsa [RUN_BULK_RX] = { 525203134Sthompsa .type = UE_BULK, 526203134Sthompsa .endpoint = UE_ADDR_ANY, 527203134Sthompsa .direction = UE_DIR_IN, 528203134Sthompsa .bufsize = RUN_MAX_RXSZ, 529203134Sthompsa .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 530203134Sthompsa .callback = run_bulk_rx_callback, 531203134Sthompsa } 532203134Sthompsa}; 533203134Sthompsa 534220235Skevlostatic int 535203134Sthompsarun_match(device_t self) 536203134Sthompsa{ 537203134Sthompsa struct usb_attach_arg *uaa = device_get_ivars(self); 538203134Sthompsa 539203134Sthompsa if (uaa->usb_mode != USB_MODE_HOST) 540203134Sthompsa return (ENXIO); 541203134Sthompsa if (uaa->info.bConfigIndex != 0) 542203134Sthompsa return (ENXIO); 543203134Sthompsa if (uaa->info.bIfaceIndex != RT2860_IFACE_INDEX) 544203134Sthompsa return (ENXIO); 545203134Sthompsa 546203134Sthompsa return (usbd_lookup_id_by_uaa(run_devs, sizeof(run_devs), uaa)); 547203134Sthompsa} 548203134Sthompsa 549203134Sthompsastatic int 550203134Sthompsarun_attach(device_t self) 551203134Sthompsa{ 552203134Sthompsa struct run_softc *sc = device_get_softc(self); 553203134Sthompsa struct usb_attach_arg *uaa = device_get_ivars(self); 554203134Sthompsa struct ieee80211com *ic; 555203134Sthompsa struct ifnet *ifp; 556205042Sthompsa uint32_t ver; 557203134Sthompsa int i, ntries, error; 558203134Sthompsa uint8_t iface_index, bands; 559203134Sthompsa 560203134Sthompsa device_set_usb_desc(self); 561203134Sthompsa sc->sc_udev = uaa->device; 562203134Sthompsa sc->sc_dev = self; 563203134Sthompsa 564203134Sthompsa mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), 565203134Sthompsa MTX_NETWORK_LOCK, MTX_DEF); 566203134Sthompsa 567203134Sthompsa iface_index = RT2860_IFACE_INDEX; 568208019Sthompsa 569203134Sthompsa error = usbd_transfer_setup(uaa->device, &iface_index, 570203134Sthompsa sc->sc_xfer, run_config, RUN_N_XFER, sc, &sc->sc_mtx); 571203134Sthompsa if (error) { 572205042Sthompsa device_printf(self, "could not allocate USB transfers, " 573203134Sthompsa "err=%s\n", usbd_errstr(error)); 574203134Sthompsa goto detach; 575203134Sthompsa } 576203134Sthompsa 577203134Sthompsa RUN_LOCK(sc); 578203134Sthompsa 579203134Sthompsa /* wait for the chip to settle */ 580203134Sthompsa for (ntries = 0; ntries < 100; ntries++) { 581209917Sthompsa if (run_read(sc, RT2860_ASIC_VER_ID, &ver) != 0) { 582203134Sthompsa RUN_UNLOCK(sc); 583203134Sthompsa goto detach; 584203134Sthompsa } 585205042Sthompsa if (ver != 0 && ver != 0xffffffff) 586203134Sthompsa break; 587203134Sthompsa run_delay(sc, 10); 588203134Sthompsa } 589203134Sthompsa if (ntries == 100) { 590203138Sthompsa device_printf(sc->sc_dev, 591203138Sthompsa "timeout waiting for NIC to initialize\n"); 592203134Sthompsa RUN_UNLOCK(sc); 593203134Sthompsa goto detach; 594203134Sthompsa } 595205042Sthompsa sc->mac_ver = ver >> 16; 596205042Sthompsa sc->mac_rev = ver & 0xffff; 597203134Sthompsa 598203134Sthompsa /* retrieve RF rev. no and various other things from EEPROM */ 599203134Sthompsa run_read_eeprom(sc); 600203134Sthompsa 601203138Sthompsa device_printf(sc->sc_dev, 602203138Sthompsa "MAC/BBP RT%04X (rev 0x%04X), RF %s (MIMO %dT%dR), address %s\n", 603205042Sthompsa sc->mac_ver, sc->mac_rev, run_get_rf(sc->rf_rev), 604203138Sthompsa sc->ntxchains, sc->nrxchains, ether_sprintf(sc->sc_bssid)); 605203134Sthompsa 606203134Sthompsa RUN_UNLOCK(sc); 607203134Sthompsa 608203134Sthompsa ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 609220235Skevlo if (ifp == NULL) { 610203138Sthompsa device_printf(sc->sc_dev, "can not if_alloc()\n"); 611203134Sthompsa goto detach; 612203134Sthompsa } 613203134Sthompsa ic = ifp->if_l2com; 614203134Sthompsa 615203134Sthompsa ifp->if_softc = sc; 616203134Sthompsa if_initname(ifp, "run", device_get_unit(sc->sc_dev)); 617203134Sthompsa ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 618203134Sthompsa ifp->if_init = run_init; 619203134Sthompsa ifp->if_ioctl = run_ioctl; 620203134Sthompsa ifp->if_start = run_start; 621207554Ssobomax IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 622207554Ssobomax ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 623203134Sthompsa IFQ_SET_READY(&ifp->if_snd); 624203134Sthompsa 625203134Sthompsa ic->ic_ifp = ifp; 626203134Sthompsa ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 627203134Sthompsa ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 628208019Sthompsa 629203134Sthompsa /* set device capabilities */ 630203134Sthompsa ic->ic_caps = 631203134Sthompsa IEEE80211_C_STA | /* station mode supported */ 632203134Sthompsa IEEE80211_C_MONITOR | /* monitor mode supported */ 633203134Sthompsa IEEE80211_C_IBSS | 634203134Sthompsa IEEE80211_C_HOSTAP | 635208019Sthompsa IEEE80211_C_WDS | /* 4-address traffic works */ 636208019Sthompsa IEEE80211_C_MBSS | 637203134Sthompsa IEEE80211_C_SHPREAMBLE | /* short preamble supported */ 638203134Sthompsa IEEE80211_C_SHSLOT | /* short slot time supported */ 639203134Sthompsa IEEE80211_C_WME | /* WME */ 640214894Sbschmidt IEEE80211_C_WPA; /* WPA1|WPA2(RSN) */ 641203134Sthompsa 642203134Sthompsa ic->ic_cryptocaps = 643203134Sthompsa IEEE80211_CRYPTO_WEP | 644203134Sthompsa IEEE80211_CRYPTO_AES_CCM | 645203134Sthompsa IEEE80211_CRYPTO_TKIPMIC | 646203134Sthompsa IEEE80211_CRYPTO_TKIP; 647203134Sthompsa 648203134Sthompsa ic->ic_flags |= IEEE80211_F_DATAPAD; 649203134Sthompsa ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS; 650203134Sthompsa 651203134Sthompsa bands = 0; 652203134Sthompsa setbit(&bands, IEEE80211_MODE_11B); 653203134Sthompsa setbit(&bands, IEEE80211_MODE_11G); 654203134Sthompsa ieee80211_init_channels(ic, NULL, &bands); 655203134Sthompsa 656203134Sthompsa /* 657203134Sthompsa * Do this by own because h/w supports 658203134Sthompsa * more channels than ieee80211_init_channels() 659203134Sthompsa */ 660205042Sthompsa if (sc->rf_rev == RT2860_RF_2750 || 661205042Sthompsa sc->rf_rev == RT2860_RF_2850 || 662205042Sthompsa sc->rf_rev == RT3070_RF_3052) { 663203134Sthompsa /* set supported .11a rates */ 664233774Shselasky for (i = 14; i < N(rt2860_rf2850); i++) { 665203134Sthompsa uint8_t chan = rt2860_rf2850[i].chan; 666203134Sthompsa ic->ic_channels[ic->ic_nchans].ic_freq = 667203134Sthompsa ieee80211_ieee2mhz(chan, IEEE80211_CHAN_A); 668203134Sthompsa ic->ic_channels[ic->ic_nchans].ic_ieee = chan; 669203134Sthompsa ic->ic_channels[ic->ic_nchans].ic_flags = IEEE80211_CHAN_A; 670203134Sthompsa ic->ic_channels[ic->ic_nchans].ic_extieee = 0; 671203134Sthompsa ic->ic_nchans++; 672203134Sthompsa } 673203134Sthompsa } 674203134Sthompsa 675203134Sthompsa ieee80211_ifattach(ic, sc->sc_bssid); 676203134Sthompsa 677203134Sthompsa ic->ic_scan_start = run_scan_start; 678203134Sthompsa ic->ic_scan_end = run_scan_end; 679203134Sthompsa ic->ic_set_channel = run_set_channel; 680203134Sthompsa ic->ic_node_alloc = run_node_alloc; 681203134Sthompsa ic->ic_newassoc = run_newassoc; 682218492Sbschmidt ic->ic_updateslot = run_updateslot; 683208019Sthompsa ic->ic_update_mcast = run_update_mcast; 684203134Sthompsa ic->ic_wme.wme_update = run_wme_update; 685203134Sthompsa ic->ic_raw_xmit = run_raw_xmit; 686203134Sthompsa ic->ic_update_promisc = run_update_promisc; 687203134Sthompsa 688203134Sthompsa ic->ic_vap_create = run_vap_create; 689203134Sthompsa ic->ic_vap_delete = run_vap_delete; 690203134Sthompsa 691203134Sthompsa ieee80211_radiotap_attach(ic, 692203134Sthompsa &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap), 693203134Sthompsa RUN_TX_RADIOTAP_PRESENT, 694203134Sthompsa &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 695203134Sthompsa RUN_RX_RADIOTAP_PRESENT); 696203134Sthompsa 697208019Sthompsa TASK_INIT(&sc->cmdq_task, 0, run_cmdq_cb, sc); 698208019Sthompsa TASK_INIT(&sc->ratectl_task, 0, run_ratectl_cb, sc); 699208019Sthompsa callout_init((struct callout *)&sc->ratectl_ch, 1); 700208019Sthompsa 701203134Sthompsa if (bootverbose) 702203134Sthompsa ieee80211_announce(ic); 703203134Sthompsa 704209917Sthompsa return (0); 705203134Sthompsa 706203134Sthompsadetach: 707203134Sthompsa run_detach(self); 708209917Sthompsa return (ENXIO); 709203134Sthompsa} 710203134Sthompsa 711203134Sthompsastatic int 712203134Sthompsarun_detach(device_t self) 713203134Sthompsa{ 714203134Sthompsa struct run_softc *sc = device_get_softc(self); 715203134Sthompsa struct ifnet *ifp = sc->sc_ifp; 716203134Sthompsa struct ieee80211com *ic; 717203134Sthompsa int i; 718203134Sthompsa 719203134Sthompsa /* stop all USB transfers */ 720203134Sthompsa usbd_transfer_unsetup(sc->sc_xfer, RUN_N_XFER); 721203134Sthompsa 722203134Sthompsa RUN_LOCK(sc); 723209144Sthompsa 724209144Sthompsa sc->ratectl_run = RUN_RATECTL_OFF; 725209144Sthompsa sc->cmdq_run = sc->cmdq_key_set = RUN_CMDQ_ABORT; 726209144Sthompsa 727203134Sthompsa /* free TX list, if any */ 728203134Sthompsa for (i = 0; i != RUN_EP_QUEUES; i++) 729203134Sthompsa run_unsetup_tx_list(sc, &sc->sc_epq[i]); 730203134Sthompsa RUN_UNLOCK(sc); 731203134Sthompsa 732203134Sthompsa if (ifp) { 733203134Sthompsa ic = ifp->if_l2com; 734208019Sthompsa /* drain tasks */ 735208019Sthompsa usb_callout_drain(&sc->ratectl_ch); 736208019Sthompsa ieee80211_draintask(ic, &sc->cmdq_task); 737208019Sthompsa ieee80211_draintask(ic, &sc->ratectl_task); 738203134Sthompsa ieee80211_ifdetach(ic); 739203134Sthompsa if_free(ifp); 740203134Sthompsa } 741203134Sthompsa 742203134Sthompsa mtx_destroy(&sc->sc_mtx); 743203134Sthompsa 744203134Sthompsa return (0); 745203134Sthompsa} 746203134Sthompsa 747203134Sthompsastatic struct ieee80211vap * 748228621Sbschmidtrun_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, 749228621Sbschmidt enum ieee80211_opmode opmode, int flags, 750203134Sthompsa const uint8_t bssid[IEEE80211_ADDR_LEN], 751203134Sthompsa const uint8_t mac[IEEE80211_ADDR_LEN]) 752203134Sthompsa{ 753208019Sthompsa struct ifnet *ifp = ic->ic_ifp; 754208019Sthompsa struct run_softc *sc = ifp->if_softc; 755203134Sthompsa struct run_vap *rvp; 756203134Sthompsa struct ieee80211vap *vap; 757208019Sthompsa int i; 758203134Sthompsa 759209917Sthompsa if (sc->rvp_cnt >= RUN_VAP_MAX) { 760208019Sthompsa if_printf(ifp, "number of VAPs maxed out\n"); 761209917Sthompsa return (NULL); 762208019Sthompsa } 763208019Sthompsa 764208019Sthompsa switch (opmode) { 765208019Sthompsa case IEEE80211_M_STA: 766208019Sthompsa /* enable s/w bmiss handling for sta mode */ 767208019Sthompsa flags |= IEEE80211_CLONE_NOBEACONS; 768208019Sthompsa /* fall though */ 769208019Sthompsa case IEEE80211_M_IBSS: 770208019Sthompsa case IEEE80211_M_MONITOR: 771208019Sthompsa case IEEE80211_M_HOSTAP: 772208019Sthompsa case IEEE80211_M_MBSS: 773208019Sthompsa /* other than WDS vaps, only one at a time */ 774208019Sthompsa if (!TAILQ_EMPTY(&ic->ic_vaps)) 775209917Sthompsa return (NULL); 776208019Sthompsa break; 777208019Sthompsa case IEEE80211_M_WDS: 778208019Sthompsa TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next){ 779208019Sthompsa if(vap->iv_opmode != IEEE80211_M_HOSTAP) 780208019Sthompsa continue; 781208019Sthompsa /* WDS vap's always share the local mac address. */ 782208019Sthompsa flags &= ~IEEE80211_CLONE_BSSID; 783208019Sthompsa break; 784208019Sthompsa } 785209917Sthompsa if (vap == NULL) { 786208019Sthompsa if_printf(ifp, "wds only supported in ap mode\n"); 787209917Sthompsa return (NULL); 788208019Sthompsa } 789208019Sthompsa break; 790208019Sthompsa default: 791208019Sthompsa if_printf(ifp, "unknown opmode %d\n", opmode); 792209917Sthompsa return (NULL); 793208019Sthompsa } 794208019Sthompsa 795208019Sthompsa rvp = (struct run_vap *) malloc(sizeof(struct run_vap), 796203134Sthompsa M_80211_VAP, M_NOWAIT | M_ZERO); 797203134Sthompsa if (rvp == NULL) 798209917Sthompsa return (NULL); 799203134Sthompsa vap = &rvp->vap; 800208019Sthompsa ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac); 801203134Sthompsa 802203134Sthompsa vap->iv_key_update_begin = run_key_update_begin; 803203134Sthompsa vap->iv_key_update_end = run_key_update_end; 804203134Sthompsa vap->iv_update_beacon = run_update_beacon; 805208019Sthompsa vap->iv_max_aid = RT2870_WCID_MAX; 806208019Sthompsa /* 807208019Sthompsa * To delete the right key from h/w, we need wcid. 808208019Sthompsa * Luckily, there is unused space in ieee80211_key{}, wk_pad, 809208019Sthompsa * and matching wcid will be written into there. So, cast 810208019Sthompsa * some spells to remove 'const' from ieee80211_key{} 811208019Sthompsa */ 812208019Sthompsa vap->iv_key_delete = (void *)run_key_delete; 813208019Sthompsa vap->iv_key_set = (void *)run_key_set; 814203134Sthompsa 815203134Sthompsa /* override state transition machine */ 816203134Sthompsa rvp->newstate = vap->iv_newstate; 817203134Sthompsa vap->iv_newstate = run_newstate; 818203134Sthompsa 819206358Srpaulo ieee80211_ratectl_init(vap); 820206358Srpaulo ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */); 821203134Sthompsa 822203134Sthompsa /* complete setup */ 823203134Sthompsa ieee80211_vap_attach(vap, run_media_change, ieee80211_media_status); 824208019Sthompsa 825208019Sthompsa /* make sure id is always unique */ 826209917Sthompsa for (i = 0; i < RUN_VAP_MAX; i++) { 827208019Sthompsa if((sc->rvp_bmap & 1 << i) == 0){ 828208019Sthompsa sc->rvp_bmap |= 1 << i; 829208019Sthompsa rvp->rvp_id = i; 830208019Sthompsa break; 831208019Sthompsa } 832208019Sthompsa } 833209917Sthompsa if (sc->rvp_cnt++ == 0) 834208019Sthompsa ic->ic_opmode = opmode; 835208019Sthompsa 836209917Sthompsa if (opmode == IEEE80211_M_HOSTAP) 837209144Sthompsa sc->cmdq_run = RUN_CMDQ_GO; 838209144Sthompsa 839208019Sthompsa DPRINTF("rvp_id=%d bmap=%x rvp_cnt=%d\n", 840208019Sthompsa rvp->rvp_id, sc->rvp_bmap, sc->rvp_cnt); 841208019Sthompsa 842209917Sthompsa return (vap); 843203134Sthompsa} 844203134Sthompsa 845203134Sthompsastatic void 846203134Sthompsarun_vap_delete(struct ieee80211vap *vap) 847203134Sthompsa{ 848203134Sthompsa struct run_vap *rvp = RUN_VAP(vap); 849203134Sthompsa struct ifnet *ifp; 850203134Sthompsa struct ieee80211com *ic; 851203134Sthompsa struct run_softc *sc; 852208019Sthompsa uint8_t rvp_id; 853203134Sthompsa 854209917Sthompsa if (vap == NULL) 855203134Sthompsa return; 856203134Sthompsa 857203134Sthompsa ic = vap->iv_ic; 858203134Sthompsa ifp = ic->ic_ifp; 859203134Sthompsa 860203134Sthompsa sc = ifp->if_softc; 861203134Sthompsa 862205042Sthompsa RUN_LOCK(sc); 863208019Sthompsa 864218492Sbschmidt m_freem(rvp->beacon_mbuf); 865218492Sbschmidt rvp->beacon_mbuf = NULL; 866218492Sbschmidt 867208019Sthompsa rvp_id = rvp->rvp_id; 868208019Sthompsa sc->ratectl_run &= ~(1 << rvp_id); 869208019Sthompsa sc->rvp_bmap &= ~(1 << rvp_id); 870208019Sthompsa run_set_region_4(sc, RT2860_SKEY(rvp_id, 0), 0, 128); 871208019Sthompsa run_set_region_4(sc, RT2860_BCN_BASE(rvp_id), 0, 512); 872208019Sthompsa --sc->rvp_cnt; 873208019Sthompsa 874208019Sthompsa DPRINTF("vap=%p rvp_id=%d bmap=%x rvp_cnt=%d\n", 875208019Sthompsa vap, rvp_id, sc->rvp_bmap, sc->rvp_cnt); 876208019Sthompsa 877205042Sthompsa RUN_UNLOCK(sc); 878203134Sthompsa 879206358Srpaulo ieee80211_ratectl_deinit(vap); 880203134Sthompsa ieee80211_vap_detach(vap); 881203134Sthompsa free(rvp, M_80211_VAP); 882203134Sthompsa} 883203134Sthompsa 884208019Sthompsa/* 885208019Sthompsa * There are numbers of functions need to be called in context thread. 886208019Sthompsa * Rather than creating taskqueue event for each of those functions, 887208019Sthompsa * here is all-for-one taskqueue callback function. This function 888208019Sthompsa * gurantees deferred functions are executed in the same order they 889208019Sthompsa * were enqueued. 890208019Sthompsa * '& RUN_CMDQ_MASQ' is to loop cmdq[]. 891208019Sthompsa */ 892203134Sthompsastatic void 893208019Sthompsarun_cmdq_cb(void *arg, int pending) 894208019Sthompsa{ 895208019Sthompsa struct run_softc *sc = arg; 896208019Sthompsa uint8_t i; 897208019Sthompsa 898208019Sthompsa /* call cmdq[].func locked */ 899208019Sthompsa RUN_LOCK(sc); 900209917Sthompsa for (i = sc->cmdq_exec; sc->cmdq[i].func && pending; 901209917Sthompsa i = sc->cmdq_exec, pending--) { 902208019Sthompsa DPRINTFN(6, "cmdq_exec=%d pending=%d\n", i, pending); 903209917Sthompsa if (sc->cmdq_run == RUN_CMDQ_GO) { 904208019Sthompsa /* 905208019Sthompsa * If arg0 is NULL, callback func needs more 906208019Sthompsa * than one arg. So, pass ptr to cmdq struct. 907208019Sthompsa */ 908209917Sthompsa if (sc->cmdq[i].arg0) 909208019Sthompsa sc->cmdq[i].func(sc->cmdq[i].arg0); 910208019Sthompsa else 911208019Sthompsa sc->cmdq[i].func(&sc->cmdq[i]); 912208019Sthompsa } 913208019Sthompsa sc->cmdq[i].arg0 = NULL; 914208019Sthompsa sc->cmdq[i].func = NULL; 915208019Sthompsa sc->cmdq_exec++; 916208019Sthompsa sc->cmdq_exec &= RUN_CMDQ_MASQ; 917208019Sthompsa } 918208019Sthompsa RUN_UNLOCK(sc); 919208019Sthompsa} 920208019Sthompsa 921208019Sthompsastatic void 922203134Sthompsarun_setup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq) 923203134Sthompsa{ 924203134Sthompsa struct run_tx_data *data; 925203134Sthompsa 926203134Sthompsa memset(pq, 0, sizeof(*pq)); 927203134Sthompsa 928203134Sthompsa STAILQ_INIT(&pq->tx_qh); 929203134Sthompsa STAILQ_INIT(&pq->tx_fh); 930203134Sthompsa 931203134Sthompsa for (data = &pq->tx_data[0]; 932203134Sthompsa data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) { 933203134Sthompsa data->sc = sc; 934203134Sthompsa STAILQ_INSERT_TAIL(&pq->tx_fh, data, next); 935203134Sthompsa } 936203134Sthompsa pq->tx_nfree = RUN_TX_RING_COUNT; 937203134Sthompsa} 938203134Sthompsa 939203134Sthompsastatic void 940203134Sthompsarun_unsetup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq) 941203134Sthompsa{ 942203134Sthompsa struct run_tx_data *data; 943203134Sthompsa 944203134Sthompsa /* make sure any subsequent use of the queues will fail */ 945203134Sthompsa pq->tx_nfree = 0; 946203134Sthompsa STAILQ_INIT(&pq->tx_fh); 947203134Sthompsa STAILQ_INIT(&pq->tx_qh); 948203134Sthompsa 949203134Sthompsa /* free up all node references and mbufs */ 950203134Sthompsa for (data = &pq->tx_data[0]; 951209917Sthompsa data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) { 952203134Sthompsa if (data->m != NULL) { 953203134Sthompsa m_freem(data->m); 954203134Sthompsa data->m = NULL; 955203134Sthompsa } 956203134Sthompsa if (data->ni != NULL) { 957203134Sthompsa ieee80211_free_node(data->ni); 958203134Sthompsa data->ni = NULL; 959203134Sthompsa } 960203134Sthompsa } 961203134Sthompsa} 962203134Sthompsa 963220235Skevlostatic int 964203134Sthompsarun_load_microcode(struct run_softc *sc) 965203134Sthompsa{ 966203134Sthompsa usb_device_request_t req; 967203137Sthompsa const struct firmware *fw; 968203134Sthompsa const u_char *base; 969203134Sthompsa uint32_t tmp; 970203134Sthompsa int ntries, error; 971203134Sthompsa const uint64_t *temp; 972203134Sthompsa uint64_t bytes; 973203134Sthompsa 974205042Sthompsa RUN_UNLOCK(sc); 975203137Sthompsa fw = firmware_get("runfw"); 976205042Sthompsa RUN_LOCK(sc); 977209917Sthompsa if (fw == NULL) { 978203138Sthompsa device_printf(sc->sc_dev, 979203138Sthompsa "failed loadfirmware of file %s\n", "runfw"); 980203134Sthompsa return ENOENT; 981203134Sthompsa } 982203134Sthompsa 983203137Sthompsa if (fw->datasize != 8192) { 984203138Sthompsa device_printf(sc->sc_dev, 985203138Sthompsa "invalid firmware size (should be 8KB)\n"); 986203137Sthompsa error = EINVAL; 987203137Sthompsa goto fail; 988203134Sthompsa } 989203134Sthompsa 990203134Sthompsa /* 991203134Sthompsa * RT3071/RT3072 use a different firmware 992203134Sthompsa * run-rt2870 (8KB) contains both, 993203134Sthompsa * first half (4KB) is for rt2870, 994203134Sthompsa * last half is for rt3071. 995203134Sthompsa */ 996203137Sthompsa base = fw->data; 997205042Sthompsa if ((sc->mac_ver) != 0x2860 && 998205042Sthompsa (sc->mac_ver) != 0x2872 && 999209917Sthompsa (sc->mac_ver) != 0x3070) { 1000203134Sthompsa base += 4096; 1001205042Sthompsa } 1002203134Sthompsa 1003203134Sthompsa /* cheap sanity check */ 1004203137Sthompsa temp = fw->data; 1005203134Sthompsa bytes = *temp; 1006209917Sthompsa if (bytes != be64toh(0xffffff0210280210)) { 1007203138Sthompsa device_printf(sc->sc_dev, "firmware checksum failed\n"); 1008203137Sthompsa error = EINVAL; 1009203137Sthompsa goto fail; 1010203137Sthompsa } 1011203134Sthompsa 1012203134Sthompsa run_read(sc, RT2860_ASIC_VER_ID, &tmp); 1013203134Sthompsa /* write microcode image */ 1014203134Sthompsa run_write_region_1(sc, RT2870_FW_BASE, base, 4096); 1015203134Sthompsa run_write(sc, RT2860_H2M_MAILBOX_CID, 0xffffffff); 1016203134Sthompsa run_write(sc, RT2860_H2M_MAILBOX_STATUS, 0xffffffff); 1017203134Sthompsa 1018203134Sthompsa req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1019203134Sthompsa req.bRequest = RT2870_RESET; 1020203134Sthompsa USETW(req.wValue, 8); 1021203134Sthompsa USETW(req.wIndex, 0); 1022203134Sthompsa USETW(req.wLength, 0); 1023220235Skevlo if ((error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL)) 1024220235Skevlo != 0) { 1025203138Sthompsa device_printf(sc->sc_dev, "firmware reset failed\n"); 1026203137Sthompsa goto fail; 1027203137Sthompsa } 1028203134Sthompsa 1029203134Sthompsa run_delay(sc, 10); 1030203134Sthompsa 1031203134Sthompsa run_write(sc, RT2860_H2M_MAILBOX, 0); 1032205042Sthompsa if ((error = run_mcu_cmd(sc, RT2860_MCU_CMD_RFRESET, 0)) != 0) 1033203137Sthompsa goto fail; 1034203134Sthompsa 1035203134Sthompsa /* wait until microcontroller is ready */ 1036203134Sthompsa for (ntries = 0; ntries < 1000; ntries++) { 1037203137Sthompsa if ((error = run_read(sc, RT2860_SYS_CTRL, &tmp)) != 0) { 1038203137Sthompsa goto fail; 1039203137Sthompsa } 1040203134Sthompsa if (tmp & RT2860_MCU_READY) 1041203134Sthompsa break; 1042203134Sthompsa run_delay(sc, 10); 1043203134Sthompsa } 1044203134Sthompsa if (ntries == 1000) { 1045203138Sthompsa device_printf(sc->sc_dev, 1046203138Sthompsa "timeout waiting for MCU to initialize\n"); 1047203137Sthompsa error = ETIMEDOUT; 1048203137Sthompsa goto fail; 1049203134Sthompsa } 1050233283Sbschmidt device_printf(sc->sc_dev, "firmware %s ver. %u.%u loaded\n", 1051233283Sbschmidt (base == fw->data) ? "RT2870" : "RT3071", 1052233283Sbschmidt *(base + 4092), *(base + 4093)); 1053203134Sthompsa 1054203137Sthompsafail: 1055203137Sthompsa firmware_put(fw, FIRMWARE_UNLOAD); 1056203137Sthompsa return (error); 1057203134Sthompsa} 1058203134Sthompsa 1059203134Sthompsaint 1060203134Sthompsarun_reset(struct run_softc *sc) 1061203134Sthompsa{ 1062203134Sthompsa usb_device_request_t req; 1063203134Sthompsa 1064203134Sthompsa req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1065203134Sthompsa req.bRequest = RT2870_RESET; 1066203134Sthompsa USETW(req.wValue, 1); 1067203134Sthompsa USETW(req.wIndex, 0); 1068203134Sthompsa USETW(req.wLength, 0); 1069209917Sthompsa return (usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL)); 1070203134Sthompsa} 1071203134Sthompsa 1072203134Sthompsastatic usb_error_t 1073203134Sthompsarun_do_request(struct run_softc *sc, 1074203134Sthompsa struct usb_device_request *req, void *data) 1075203134Sthompsa{ 1076203134Sthompsa usb_error_t err; 1077203134Sthompsa int ntries = 10; 1078203134Sthompsa 1079203134Sthompsa RUN_LOCK_ASSERT(sc, MA_OWNED); 1080203134Sthompsa 1081203134Sthompsa while (ntries--) { 1082203134Sthompsa err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx, 1083203134Sthompsa req, data, 0, NULL, 250 /* ms */); 1084203134Sthompsa if (err == 0) 1085203134Sthompsa break; 1086203134Sthompsa DPRINTFN(1, "Control request failed, %s (retrying)\n", 1087203134Sthompsa usbd_errstr(err)); 1088203134Sthompsa run_delay(sc, 10); 1089203134Sthompsa } 1090203134Sthompsa return (err); 1091203134Sthompsa} 1092203134Sthompsa 1093203134Sthompsastatic int 1094203134Sthompsarun_read(struct run_softc *sc, uint16_t reg, uint32_t *val) 1095203134Sthompsa{ 1096203134Sthompsa uint32_t tmp; 1097203134Sthompsa int error; 1098203134Sthompsa 1099203134Sthompsa error = run_read_region_1(sc, reg, (uint8_t *)&tmp, sizeof tmp); 1100203134Sthompsa if (error == 0) 1101203134Sthompsa *val = le32toh(tmp); 1102203134Sthompsa else 1103203134Sthompsa *val = 0xffffffff; 1104209917Sthompsa return (error); 1105203134Sthompsa} 1106203134Sthompsa 1107203134Sthompsastatic int 1108203134Sthompsarun_read_region_1(struct run_softc *sc, uint16_t reg, uint8_t *buf, int len) 1109203134Sthompsa{ 1110203134Sthompsa usb_device_request_t req; 1111203134Sthompsa 1112203134Sthompsa req.bmRequestType = UT_READ_VENDOR_DEVICE; 1113203134Sthompsa req.bRequest = RT2870_READ_REGION_1; 1114203134Sthompsa USETW(req.wValue, 0); 1115203134Sthompsa USETW(req.wIndex, reg); 1116203134Sthompsa USETW(req.wLength, len); 1117203134Sthompsa 1118209917Sthompsa return (run_do_request(sc, &req, buf)); 1119203134Sthompsa} 1120203134Sthompsa 1121203134Sthompsastatic int 1122203134Sthompsarun_write_2(struct run_softc *sc, uint16_t reg, uint16_t val) 1123203134Sthompsa{ 1124203134Sthompsa usb_device_request_t req; 1125203134Sthompsa 1126203134Sthompsa req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1127203134Sthompsa req.bRequest = RT2870_WRITE_2; 1128203134Sthompsa USETW(req.wValue, val); 1129203134Sthompsa USETW(req.wIndex, reg); 1130203134Sthompsa USETW(req.wLength, 0); 1131203134Sthompsa 1132209917Sthompsa return (run_do_request(sc, &req, NULL)); 1133203134Sthompsa} 1134203134Sthompsa 1135203134Sthompsastatic int 1136203134Sthompsarun_write(struct run_softc *sc, uint16_t reg, uint32_t val) 1137203134Sthompsa{ 1138203134Sthompsa int error; 1139203134Sthompsa 1140203134Sthompsa if ((error = run_write_2(sc, reg, val & 0xffff)) == 0) 1141203134Sthompsa error = run_write_2(sc, reg + 2, val >> 16); 1142209917Sthompsa return (error); 1143203134Sthompsa} 1144203134Sthompsa 1145203134Sthompsastatic int 1146203134Sthompsarun_write_region_1(struct run_softc *sc, uint16_t reg, const uint8_t *buf, 1147203134Sthompsa int len) 1148203134Sthompsa{ 1149203134Sthompsa#if 1 1150203134Sthompsa int i, error = 0; 1151203134Sthompsa /* 1152203134Sthompsa * NB: the WRITE_REGION_1 command is not stable on RT2860. 1153203134Sthompsa * We thus issue multiple WRITE_2 commands instead. 1154203134Sthompsa */ 1155203134Sthompsa KASSERT((len & 1) == 0, ("run_write_region_1: Data too long.\n")); 1156203134Sthompsa for (i = 0; i < len && error == 0; i += 2) 1157203134Sthompsa error = run_write_2(sc, reg + i, buf[i] | buf[i + 1] << 8); 1158209917Sthompsa return (error); 1159203134Sthompsa#else 1160203134Sthompsa usb_device_request_t req; 1161203134Sthompsa 1162203134Sthompsa req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1163203134Sthompsa req.bRequest = RT2870_WRITE_REGION_1; 1164203134Sthompsa USETW(req.wValue, 0); 1165203134Sthompsa USETW(req.wIndex, reg); 1166203134Sthompsa USETW(req.wLength, len); 1167209917Sthompsa return (run_do_request(sc, &req, buf)); 1168203134Sthompsa#endif 1169203134Sthompsa} 1170203134Sthompsa 1171203134Sthompsastatic int 1172203134Sthompsarun_set_region_4(struct run_softc *sc, uint16_t reg, uint32_t val, int len) 1173203134Sthompsa{ 1174203134Sthompsa int i, error = 0; 1175203134Sthompsa 1176203134Sthompsa KASSERT((len & 3) == 0, ("run_set_region_4: Invalid data length.\n")); 1177203134Sthompsa for (i = 0; i < len && error == 0; i += 4) 1178203134Sthompsa error = run_write(sc, reg + i, val); 1179209917Sthompsa return (error); 1180203134Sthompsa} 1181203134Sthompsa 1182203134Sthompsa/* Read 16-bit from eFUSE ROM (RT3070 only.) */ 1183203134Sthompsastatic int 1184203134Sthompsarun_efuse_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val) 1185203134Sthompsa{ 1186203134Sthompsa uint32_t tmp; 1187203134Sthompsa uint16_t reg; 1188203134Sthompsa int error, ntries; 1189203134Sthompsa 1190203134Sthompsa if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0) 1191209917Sthompsa return (error); 1192203134Sthompsa 1193203134Sthompsa addr *= 2; 1194203134Sthompsa /*- 1195203134Sthompsa * Read one 16-byte block into registers EFUSE_DATA[0-3]: 1196203134Sthompsa * DATA0: F E D C 1197203134Sthompsa * DATA1: B A 9 8 1198203134Sthompsa * DATA2: 7 6 5 4 1199203134Sthompsa * DATA3: 3 2 1 0 1200203134Sthompsa */ 1201203134Sthompsa tmp &= ~(RT3070_EFSROM_MODE_MASK | RT3070_EFSROM_AIN_MASK); 1202203134Sthompsa tmp |= (addr & ~0xf) << RT3070_EFSROM_AIN_SHIFT | RT3070_EFSROM_KICK; 1203203134Sthompsa run_write(sc, RT3070_EFUSE_CTRL, tmp); 1204203134Sthompsa for (ntries = 0; ntries < 100; ntries++) { 1205203134Sthompsa if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0) 1206209917Sthompsa return (error); 1207203134Sthompsa if (!(tmp & RT3070_EFSROM_KICK)) 1208203134Sthompsa break; 1209203134Sthompsa run_delay(sc, 2); 1210203134Sthompsa } 1211203134Sthompsa if (ntries == 100) 1212209917Sthompsa return (ETIMEDOUT); 1213203134Sthompsa 1214203134Sthompsa if ((tmp & RT3070_EFUSE_AOUT_MASK) == RT3070_EFUSE_AOUT_MASK) { 1215203134Sthompsa *val = 0xffff; /* address not found */ 1216209917Sthompsa return (0); 1217203134Sthompsa } 1218203134Sthompsa /* determine to which 32-bit register our 16-bit word belongs */ 1219203134Sthompsa reg = RT3070_EFUSE_DATA3 - (addr & 0xc); 1220203134Sthompsa if ((error = run_read(sc, reg, &tmp)) != 0) 1221209917Sthompsa return (error); 1222203134Sthompsa 1223203134Sthompsa *val = (addr & 2) ? tmp >> 16 : tmp & 0xffff; 1224209917Sthompsa return (0); 1225203134Sthompsa} 1226203134Sthompsa 1227203134Sthompsastatic int 1228203134Sthompsarun_eeprom_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val) 1229203134Sthompsa{ 1230203134Sthompsa usb_device_request_t req; 1231203134Sthompsa uint16_t tmp; 1232203134Sthompsa int error; 1233203134Sthompsa 1234203134Sthompsa addr *= 2; 1235203134Sthompsa req.bmRequestType = UT_READ_VENDOR_DEVICE; 1236203134Sthompsa req.bRequest = RT2870_EEPROM_READ; 1237203134Sthompsa USETW(req.wValue, 0); 1238203134Sthompsa USETW(req.wIndex, addr); 1239203134Sthompsa USETW(req.wLength, sizeof tmp); 1240203134Sthompsa 1241203134Sthompsa error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, &tmp); 1242203134Sthompsa if (error == 0) 1243203134Sthompsa *val = le16toh(tmp); 1244203134Sthompsa else 1245203134Sthompsa *val = 0xffff; 1246209917Sthompsa return (error); 1247203134Sthompsa} 1248203134Sthompsa 1249203134Sthompsastatic __inline int 1250203134Sthompsarun_srom_read(struct run_softc *sc, uint16_t addr, uint16_t *val) 1251203134Sthompsa{ 1252203134Sthompsa /* either eFUSE ROM or EEPROM */ 1253203134Sthompsa return sc->sc_srom_read(sc, addr, val); 1254203134Sthompsa} 1255203134Sthompsa 1256203134Sthompsastatic int 1257203134Sthompsarun_rt2870_rf_write(struct run_softc *sc, uint8_t reg, uint32_t val) 1258203134Sthompsa{ 1259203134Sthompsa uint32_t tmp; 1260203134Sthompsa int error, ntries; 1261203134Sthompsa 1262203134Sthompsa for (ntries = 0; ntries < 10; ntries++) { 1263203134Sthompsa if ((error = run_read(sc, RT2860_RF_CSR_CFG0, &tmp)) != 0) 1264209917Sthompsa return (error); 1265203134Sthompsa if (!(tmp & RT2860_RF_REG_CTRL)) 1266203134Sthompsa break; 1267203134Sthompsa } 1268203134Sthompsa if (ntries == 10) 1269209917Sthompsa return (ETIMEDOUT); 1270203134Sthompsa 1271203134Sthompsa /* RF registers are 24-bit on the RT2860 */ 1272203134Sthompsa tmp = RT2860_RF_REG_CTRL | 24 << RT2860_RF_REG_WIDTH_SHIFT | 1273203134Sthompsa (val & 0x3fffff) << 2 | (reg & 3); 1274209917Sthompsa return (run_write(sc, RT2860_RF_CSR_CFG0, tmp)); 1275203134Sthompsa} 1276203134Sthompsa 1277203134Sthompsastatic int 1278203134Sthompsarun_rt3070_rf_read(struct run_softc *sc, uint8_t reg, uint8_t *val) 1279203134Sthompsa{ 1280203134Sthompsa uint32_t tmp; 1281203134Sthompsa int error, ntries; 1282203134Sthompsa 1283203134Sthompsa for (ntries = 0; ntries < 100; ntries++) { 1284203134Sthompsa if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0) 1285209917Sthompsa return (error); 1286203134Sthompsa if (!(tmp & RT3070_RF_KICK)) 1287203134Sthompsa break; 1288203134Sthompsa } 1289203134Sthompsa if (ntries == 100) 1290209917Sthompsa return (ETIMEDOUT); 1291203134Sthompsa 1292203134Sthompsa tmp = RT3070_RF_KICK | reg << 8; 1293203134Sthompsa if ((error = run_write(sc, RT3070_RF_CSR_CFG, tmp)) != 0) 1294209917Sthompsa return (error); 1295203134Sthompsa 1296203134Sthompsa for (ntries = 0; ntries < 100; ntries++) { 1297203134Sthompsa if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0) 1298209917Sthompsa return (error); 1299203134Sthompsa if (!(tmp & RT3070_RF_KICK)) 1300203134Sthompsa break; 1301203134Sthompsa } 1302203134Sthompsa if (ntries == 100) 1303209917Sthompsa return (ETIMEDOUT); 1304203134Sthompsa 1305203134Sthompsa *val = tmp & 0xff; 1306209917Sthompsa return (0); 1307203134Sthompsa} 1308203134Sthompsa 1309203134Sthompsastatic int 1310203134Sthompsarun_rt3070_rf_write(struct run_softc *sc, uint8_t reg, uint8_t val) 1311203134Sthompsa{ 1312203134Sthompsa uint32_t tmp; 1313203134Sthompsa int error, ntries; 1314203134Sthompsa 1315203134Sthompsa for (ntries = 0; ntries < 10; ntries++) { 1316203134Sthompsa if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0) 1317209917Sthompsa return (error); 1318203134Sthompsa if (!(tmp & RT3070_RF_KICK)) 1319203134Sthompsa break; 1320203134Sthompsa } 1321203134Sthompsa if (ntries == 10) 1322209917Sthompsa return (ETIMEDOUT); 1323203134Sthompsa 1324203134Sthompsa tmp = RT3070_RF_WRITE | RT3070_RF_KICK | reg << 8 | val; 1325209917Sthompsa return (run_write(sc, RT3070_RF_CSR_CFG, tmp)); 1326203134Sthompsa} 1327203134Sthompsa 1328203134Sthompsastatic int 1329203134Sthompsarun_bbp_read(struct run_softc *sc, uint8_t reg, uint8_t *val) 1330203134Sthompsa{ 1331203134Sthompsa uint32_t tmp; 1332203134Sthompsa int ntries, error; 1333203134Sthompsa 1334203134Sthompsa for (ntries = 0; ntries < 10; ntries++) { 1335203134Sthompsa if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0) 1336209917Sthompsa return (error); 1337203134Sthompsa if (!(tmp & RT2860_BBP_CSR_KICK)) 1338203134Sthompsa break; 1339203134Sthompsa } 1340203134Sthompsa if (ntries == 10) 1341209917Sthompsa return (ETIMEDOUT); 1342203134Sthompsa 1343203134Sthompsa tmp = RT2860_BBP_CSR_READ | RT2860_BBP_CSR_KICK | reg << 8; 1344203134Sthompsa if ((error = run_write(sc, RT2860_BBP_CSR_CFG, tmp)) != 0) 1345209917Sthompsa return (error); 1346203134Sthompsa 1347203134Sthompsa for (ntries = 0; ntries < 10; ntries++) { 1348203134Sthompsa if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0) 1349209917Sthompsa return (error); 1350203134Sthompsa if (!(tmp & RT2860_BBP_CSR_KICK)) 1351203134Sthompsa break; 1352203134Sthompsa } 1353203134Sthompsa if (ntries == 10) 1354209917Sthompsa return (ETIMEDOUT); 1355203134Sthompsa 1356203134Sthompsa *val = tmp & 0xff; 1357209917Sthompsa return (0); 1358203134Sthompsa} 1359203134Sthompsa 1360203134Sthompsastatic int 1361203134Sthompsarun_bbp_write(struct run_softc *sc, uint8_t reg, uint8_t val) 1362203134Sthompsa{ 1363203134Sthompsa uint32_t tmp; 1364203134Sthompsa int ntries, error; 1365203134Sthompsa 1366203134Sthompsa for (ntries = 0; ntries < 10; ntries++) { 1367203134Sthompsa if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0) 1368209917Sthompsa return (error); 1369203134Sthompsa if (!(tmp & RT2860_BBP_CSR_KICK)) 1370203134Sthompsa break; 1371203134Sthompsa } 1372203134Sthompsa if (ntries == 10) 1373209917Sthompsa return (ETIMEDOUT); 1374203134Sthompsa 1375203134Sthompsa tmp = RT2860_BBP_CSR_KICK | reg << 8 | val; 1376209917Sthompsa return (run_write(sc, RT2860_BBP_CSR_CFG, tmp)); 1377203134Sthompsa} 1378203134Sthompsa 1379203134Sthompsa/* 1380203134Sthompsa * Send a command to the 8051 microcontroller unit. 1381203134Sthompsa */ 1382203134Sthompsastatic int 1383203134Sthompsarun_mcu_cmd(struct run_softc *sc, uint8_t cmd, uint16_t arg) 1384203134Sthompsa{ 1385203134Sthompsa uint32_t tmp; 1386203134Sthompsa int error, ntries; 1387203134Sthompsa 1388203134Sthompsa for (ntries = 0; ntries < 100; ntries++) { 1389203134Sthompsa if ((error = run_read(sc, RT2860_H2M_MAILBOX, &tmp)) != 0) 1390203134Sthompsa return error; 1391203134Sthompsa if (!(tmp & RT2860_H2M_BUSY)) 1392203134Sthompsa break; 1393203134Sthompsa } 1394203134Sthompsa if (ntries == 100) 1395203134Sthompsa return ETIMEDOUT; 1396203134Sthompsa 1397203134Sthompsa tmp = RT2860_H2M_BUSY | RT2860_TOKEN_NO_INTR << 16 | arg; 1398203134Sthompsa if ((error = run_write(sc, RT2860_H2M_MAILBOX, tmp)) == 0) 1399203134Sthompsa error = run_write(sc, RT2860_HOST_CMD, cmd); 1400209917Sthompsa return (error); 1401203134Sthompsa} 1402203134Sthompsa 1403203134Sthompsa/* 1404203134Sthompsa * Add `delta' (signed) to each 4-bit sub-word of a 32-bit word. 1405203134Sthompsa * Used to adjust per-rate Tx power registers. 1406203134Sthompsa */ 1407203134Sthompsastatic __inline uint32_t 1408203134Sthompsab4inc(uint32_t b32, int8_t delta) 1409203134Sthompsa{ 1410203134Sthompsa int8_t i, b4; 1411203134Sthompsa 1412203134Sthompsa for (i = 0; i < 8; i++) { 1413203134Sthompsa b4 = b32 & 0xf; 1414203134Sthompsa b4 += delta; 1415203134Sthompsa if (b4 < 0) 1416203134Sthompsa b4 = 0; 1417203134Sthompsa else if (b4 > 0xf) 1418203134Sthompsa b4 = 0xf; 1419203134Sthompsa b32 = b32 >> 4 | b4 << 28; 1420203134Sthompsa } 1421209917Sthompsa return (b32); 1422203134Sthompsa} 1423203134Sthompsa 1424203134Sthompsastatic const char * 1425203134Sthompsarun_get_rf(int rev) 1426203134Sthompsa{ 1427203134Sthompsa switch (rev) { 1428203134Sthompsa case RT2860_RF_2820: return "RT2820"; 1429203134Sthompsa case RT2860_RF_2850: return "RT2850"; 1430203134Sthompsa case RT2860_RF_2720: return "RT2720"; 1431203134Sthompsa case RT2860_RF_2750: return "RT2750"; 1432203134Sthompsa case RT3070_RF_3020: return "RT3020"; 1433203134Sthompsa case RT3070_RF_2020: return "RT2020"; 1434203134Sthompsa case RT3070_RF_3021: return "RT3021"; 1435203134Sthompsa case RT3070_RF_3022: return "RT3022"; 1436203134Sthompsa case RT3070_RF_3052: return "RT3052"; 1437203134Sthompsa } 1438209917Sthompsa return ("unknown"); 1439203134Sthompsa} 1440203134Sthompsa 1441203134Sthompsaint 1442203134Sthompsarun_read_eeprom(struct run_softc *sc) 1443203134Sthompsa{ 1444203134Sthompsa int8_t delta_2ghz, delta_5ghz; 1445203134Sthompsa uint32_t tmp; 1446203134Sthompsa uint16_t val; 1447203134Sthompsa int ridx, ant, i; 1448203134Sthompsa 1449203134Sthompsa /* check whether the ROM is eFUSE ROM or EEPROM */ 1450203134Sthompsa sc->sc_srom_read = run_eeprom_read_2; 1451205042Sthompsa if (sc->mac_ver >= 0x3070) { 1452203134Sthompsa run_read(sc, RT3070_EFUSE_CTRL, &tmp); 1453203134Sthompsa DPRINTF("EFUSE_CTRL=0x%08x\n", tmp); 1454203134Sthompsa if (tmp & RT3070_SEL_EFUSE) 1455203134Sthompsa sc->sc_srom_read = run_efuse_read_2; 1456203134Sthompsa } 1457203134Sthompsa 1458203134Sthompsa /* read ROM version */ 1459203134Sthompsa run_srom_read(sc, RT2860_EEPROM_VERSION, &val); 1460203134Sthompsa DPRINTF("EEPROM rev=%d, FAE=%d\n", val & 0xff, val >> 8); 1461203134Sthompsa 1462203134Sthompsa /* read MAC address */ 1463203134Sthompsa run_srom_read(sc, RT2860_EEPROM_MAC01, &val); 1464203134Sthompsa sc->sc_bssid[0] = val & 0xff; 1465203134Sthompsa sc->sc_bssid[1] = val >> 8; 1466203134Sthompsa run_srom_read(sc, RT2860_EEPROM_MAC23, &val); 1467203134Sthompsa sc->sc_bssid[2] = val & 0xff; 1468203134Sthompsa sc->sc_bssid[3] = val >> 8; 1469203134Sthompsa run_srom_read(sc, RT2860_EEPROM_MAC45, &val); 1470203134Sthompsa sc->sc_bssid[4] = val & 0xff; 1471203134Sthompsa sc->sc_bssid[5] = val >> 8; 1472203134Sthompsa 1473205042Sthompsa /* read vender BBP settings */ 1474205042Sthompsa for (i = 0; i < 10; i++) { 1475203134Sthompsa run_srom_read(sc, RT2860_EEPROM_BBP_BASE + i, &val); 1476203134Sthompsa sc->bbp[i].val = val & 0xff; 1477203134Sthompsa sc->bbp[i].reg = val >> 8; 1478203134Sthompsa DPRINTF("BBP%d=0x%02x\n", sc->bbp[i].reg, sc->bbp[i].val); 1479203134Sthompsa } 1480205042Sthompsa if (sc->mac_ver >= 0x3071) { 1481205042Sthompsa /* read vendor RF settings */ 1482205042Sthompsa for (i = 0; i < 10; i++) { 1483205042Sthompsa run_srom_read(sc, RT3071_EEPROM_RF_BASE + i, &val); 1484205042Sthompsa sc->rf[i].val = val & 0xff; 1485205042Sthompsa sc->rf[i].reg = val >> 8; 1486205042Sthompsa DPRINTF("RF%d=0x%02x\n", sc->rf[i].reg, 1487205042Sthompsa sc->rf[i].val); 1488205042Sthompsa } 1489205042Sthompsa } 1490203134Sthompsa 1491203134Sthompsa /* read RF frequency offset from EEPROM */ 1492203134Sthompsa run_srom_read(sc, RT2860_EEPROM_FREQ_LEDS, &val); 1493203134Sthompsa sc->freq = ((val & 0xff) != 0xff) ? val & 0xff : 0; 1494203134Sthompsa DPRINTF("EEPROM freq offset %d\n", sc->freq & 0xff); 1495203134Sthompsa 1496205042Sthompsa if (val >> 8 != 0xff) { 1497203134Sthompsa /* read LEDs operating mode */ 1498205042Sthompsa sc->leds = val >> 8; 1499203134Sthompsa run_srom_read(sc, RT2860_EEPROM_LED1, &sc->led[0]); 1500203134Sthompsa run_srom_read(sc, RT2860_EEPROM_LED2, &sc->led[1]); 1501203134Sthompsa run_srom_read(sc, RT2860_EEPROM_LED3, &sc->led[2]); 1502203134Sthompsa } else { 1503203134Sthompsa /* broken EEPROM, use default settings */ 1504203134Sthompsa sc->leds = 0x01; 1505203134Sthompsa sc->led[0] = 0x5555; 1506203134Sthompsa sc->led[1] = 0x2221; 1507203134Sthompsa sc->led[2] = 0x5627; /* differs from RT2860 */ 1508203134Sthompsa } 1509203134Sthompsa DPRINTF("EEPROM LED mode=0x%02x, LEDs=0x%04x/0x%04x/0x%04x\n", 1510203134Sthompsa sc->leds, sc->led[0], sc->led[1], sc->led[2]); 1511203134Sthompsa 1512203134Sthompsa /* read RF information */ 1513203134Sthompsa run_srom_read(sc, RT2860_EEPROM_ANTENNA, &val); 1514203134Sthompsa if (val == 0xffff) { 1515203134Sthompsa DPRINTF("invalid EEPROM antenna info, using default\n"); 1516205042Sthompsa if (sc->mac_ver == 0x3572) { 1517205042Sthompsa /* default to RF3052 2T2R */ 1518205042Sthompsa sc->rf_rev = RT3070_RF_3052; 1519205042Sthompsa sc->ntxchains = 2; 1520205042Sthompsa sc->nrxchains = 2; 1521205042Sthompsa } else if (sc->mac_ver >= 0x3070) { 1522203134Sthompsa /* default to RF3020 1T1R */ 1523203134Sthompsa sc->rf_rev = RT3070_RF_3020; 1524203134Sthompsa sc->ntxchains = 1; 1525203134Sthompsa sc->nrxchains = 1; 1526203134Sthompsa } else { 1527203134Sthompsa /* default to RF2820 1T2R */ 1528203134Sthompsa sc->rf_rev = RT2860_RF_2820; 1529203134Sthompsa sc->ntxchains = 1; 1530203134Sthompsa sc->nrxchains = 2; 1531203134Sthompsa } 1532203134Sthompsa } else { 1533203134Sthompsa sc->rf_rev = (val >> 8) & 0xf; 1534203134Sthompsa sc->ntxchains = (val >> 4) & 0xf; 1535203134Sthompsa sc->nrxchains = val & 0xf; 1536203134Sthompsa } 1537203134Sthompsa DPRINTF("EEPROM RF rev=0x%02x chains=%dT%dR\n", 1538203134Sthompsa sc->rf_rev, sc->ntxchains, sc->nrxchains); 1539203134Sthompsa 1540208019Sthompsa /* check if RF supports automatic Tx access gain control */ 1541203134Sthompsa run_srom_read(sc, RT2860_EEPROM_CONFIG, &val); 1542203134Sthompsa DPRINTF("EEPROM CFG 0x%04x\n", val); 1543205042Sthompsa /* check if driver should patch the DAC issue */ 1544205042Sthompsa if ((val >> 8) != 0xff) 1545205042Sthompsa sc->patch_dac = (val >> 15) & 1; 1546203134Sthompsa if ((val & 0xff) != 0xff) { 1547203134Sthompsa sc->ext_5ghz_lna = (val >> 3) & 1; 1548203134Sthompsa sc->ext_2ghz_lna = (val >> 2) & 1; 1549205042Sthompsa /* check if RF supports automatic Tx access gain control */ 1550203134Sthompsa sc->calib_2ghz = sc->calib_5ghz = (val >> 1) & 1; 1551205042Sthompsa /* check if we have a hardware radio switch */ 1552205042Sthompsa sc->rfswitch = val & 1; 1553203134Sthompsa } 1554203134Sthompsa 1555203134Sthompsa /* read power settings for 2GHz channels */ 1556203134Sthompsa for (i = 0; i < 14; i += 2) { 1557203134Sthompsa run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE1 + i / 2, &val); 1558203134Sthompsa sc->txpow1[i + 0] = (int8_t)(val & 0xff); 1559203134Sthompsa sc->txpow1[i + 1] = (int8_t)(val >> 8); 1560203134Sthompsa 1561203134Sthompsa run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE2 + i / 2, &val); 1562203134Sthompsa sc->txpow2[i + 0] = (int8_t)(val & 0xff); 1563203134Sthompsa sc->txpow2[i + 1] = (int8_t)(val >> 8); 1564203134Sthompsa } 1565203134Sthompsa /* fix broken Tx power entries */ 1566203134Sthompsa for (i = 0; i < 14; i++) { 1567203134Sthompsa if (sc->txpow1[i] < 0 || sc->txpow1[i] > 31) 1568203134Sthompsa sc->txpow1[i] = 5; 1569203134Sthompsa if (sc->txpow2[i] < 0 || sc->txpow2[i] > 31) 1570203134Sthompsa sc->txpow2[i] = 5; 1571203134Sthompsa DPRINTF("chan %d: power1=%d, power2=%d\n", 1572203134Sthompsa rt2860_rf2850[i].chan, sc->txpow1[i], sc->txpow2[i]); 1573203134Sthompsa } 1574203134Sthompsa /* read power settings for 5GHz channels */ 1575205042Sthompsa for (i = 0; i < 40; i += 2) { 1576203134Sthompsa run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE1 + i / 2, &val); 1577203134Sthompsa sc->txpow1[i + 14] = (int8_t)(val & 0xff); 1578203134Sthompsa sc->txpow1[i + 15] = (int8_t)(val >> 8); 1579203134Sthompsa 1580203134Sthompsa run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE2 + i / 2, &val); 1581203134Sthompsa sc->txpow2[i + 14] = (int8_t)(val & 0xff); 1582203134Sthompsa sc->txpow2[i + 15] = (int8_t)(val >> 8); 1583203134Sthompsa } 1584203134Sthompsa /* fix broken Tx power entries */ 1585205042Sthompsa for (i = 0; i < 40; i++) { 1586203134Sthompsa if (sc->txpow1[14 + i] < -7 || sc->txpow1[14 + i] > 15) 1587203134Sthompsa sc->txpow1[14 + i] = 5; 1588203134Sthompsa if (sc->txpow2[14 + i] < -7 || sc->txpow2[14 + i] > 15) 1589203134Sthompsa sc->txpow2[14 + i] = 5; 1590203134Sthompsa DPRINTF("chan %d: power1=%d, power2=%d\n", 1591203134Sthompsa rt2860_rf2850[14 + i].chan, sc->txpow1[14 + i], 1592203134Sthompsa sc->txpow2[14 + i]); 1593203134Sthompsa } 1594203134Sthompsa 1595203134Sthompsa /* read Tx power compensation for each Tx rate */ 1596203134Sthompsa run_srom_read(sc, RT2860_EEPROM_DELTAPWR, &val); 1597203134Sthompsa delta_2ghz = delta_5ghz = 0; 1598203134Sthompsa if ((val & 0xff) != 0xff && (val & 0x80)) { 1599203134Sthompsa delta_2ghz = val & 0xf; 1600203134Sthompsa if (!(val & 0x40)) /* negative number */ 1601203134Sthompsa delta_2ghz = -delta_2ghz; 1602203134Sthompsa } 1603203134Sthompsa val >>= 8; 1604203134Sthompsa if ((val & 0xff) != 0xff && (val & 0x80)) { 1605203134Sthompsa delta_5ghz = val & 0xf; 1606203134Sthompsa if (!(val & 0x40)) /* negative number */ 1607203134Sthompsa delta_5ghz = -delta_5ghz; 1608203134Sthompsa } 1609203134Sthompsa DPRINTF("power compensation=%d (2GHz), %d (5GHz)\n", 1610203134Sthompsa delta_2ghz, delta_5ghz); 1611203134Sthompsa 1612203134Sthompsa for (ridx = 0; ridx < 5; ridx++) { 1613203134Sthompsa uint32_t reg; 1614203134Sthompsa 1615208019Sthompsa run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2, &val); 1616208019Sthompsa reg = val; 1617208019Sthompsa run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2 + 1, &val); 1618208019Sthompsa reg |= (uint32_t)val << 16; 1619203134Sthompsa 1620203134Sthompsa sc->txpow20mhz[ridx] = reg; 1621203134Sthompsa sc->txpow40mhz_2ghz[ridx] = b4inc(reg, delta_2ghz); 1622203134Sthompsa sc->txpow40mhz_5ghz[ridx] = b4inc(reg, delta_5ghz); 1623203134Sthompsa 1624203134Sthompsa DPRINTF("ridx %d: power 20MHz=0x%08x, 40MHz/2GHz=0x%08x, " 1625203134Sthompsa "40MHz/5GHz=0x%08x\n", ridx, sc->txpow20mhz[ridx], 1626203134Sthompsa sc->txpow40mhz_2ghz[ridx], sc->txpow40mhz_5ghz[ridx]); 1627203134Sthompsa } 1628203134Sthompsa 1629203134Sthompsa /* read RSSI offsets and LNA gains from EEPROM */ 1630203134Sthompsa run_srom_read(sc, RT2860_EEPROM_RSSI1_2GHZ, &val); 1631203134Sthompsa sc->rssi_2ghz[0] = val & 0xff; /* Ant A */ 1632203134Sthompsa sc->rssi_2ghz[1] = val >> 8; /* Ant B */ 1633203134Sthompsa run_srom_read(sc, RT2860_EEPROM_RSSI2_2GHZ, &val); 1634205042Sthompsa if (sc->mac_ver >= 0x3070) { 1635205042Sthompsa /* 1636205042Sthompsa * On RT3070 chips (limited to 2 Rx chains), this ROM 1637205042Sthompsa * field contains the Tx mixer gain for the 2GHz band. 1638205042Sthompsa */ 1639205042Sthompsa if ((val & 0xff) != 0xff) 1640205042Sthompsa sc->txmixgain_2ghz = val & 0x7; 1641205042Sthompsa DPRINTF("tx mixer gain=%u (2GHz)\n", sc->txmixgain_2ghz); 1642205042Sthompsa } else 1643205042Sthompsa sc->rssi_2ghz[2] = val & 0xff; /* Ant C */ 1644203134Sthompsa sc->lna[2] = val >> 8; /* channel group 2 */ 1645203134Sthompsa 1646203134Sthompsa run_srom_read(sc, RT2860_EEPROM_RSSI1_5GHZ, &val); 1647203134Sthompsa sc->rssi_5ghz[0] = val & 0xff; /* Ant A */ 1648203134Sthompsa sc->rssi_5ghz[1] = val >> 8; /* Ant B */ 1649203134Sthompsa run_srom_read(sc, RT2860_EEPROM_RSSI2_5GHZ, &val); 1650205042Sthompsa if (sc->mac_ver == 0x3572) { 1651205042Sthompsa /* 1652205042Sthompsa * On RT3572 chips (limited to 2 Rx chains), this ROM 1653205042Sthompsa * field contains the Tx mixer gain for the 5GHz band. 1654205042Sthompsa */ 1655205042Sthompsa if ((val & 0xff) != 0xff) 1656205042Sthompsa sc->txmixgain_5ghz = val & 0x7; 1657205042Sthompsa DPRINTF("tx mixer gain=%u (5GHz)\n", sc->txmixgain_5ghz); 1658205042Sthompsa } else 1659205042Sthompsa sc->rssi_5ghz[2] = val & 0xff; /* Ant C */ 1660203134Sthompsa sc->lna[3] = val >> 8; /* channel group 3 */ 1661203134Sthompsa 1662203134Sthompsa run_srom_read(sc, RT2860_EEPROM_LNA, &val); 1663203134Sthompsa sc->lna[0] = val & 0xff; /* channel group 0 */ 1664203134Sthompsa sc->lna[1] = val >> 8; /* channel group 1 */ 1665203134Sthompsa 1666203134Sthompsa /* fix broken 5GHz LNA entries */ 1667203134Sthompsa if (sc->lna[2] == 0 || sc->lna[2] == 0xff) { 1668203134Sthompsa DPRINTF("invalid LNA for channel group %d\n", 2); 1669203134Sthompsa sc->lna[2] = sc->lna[1]; 1670203134Sthompsa } 1671203134Sthompsa if (sc->lna[3] == 0 || sc->lna[3] == 0xff) { 1672203134Sthompsa DPRINTF("invalid LNA for channel group %d\n", 3); 1673203134Sthompsa sc->lna[3] = sc->lna[1]; 1674203134Sthompsa } 1675203134Sthompsa 1676203134Sthompsa /* fix broken RSSI offset entries */ 1677203134Sthompsa for (ant = 0; ant < 3; ant++) { 1678203134Sthompsa if (sc->rssi_2ghz[ant] < -10 || sc->rssi_2ghz[ant] > 10) { 1679203134Sthompsa DPRINTF("invalid RSSI%d offset: %d (2GHz)\n", 1680203134Sthompsa ant + 1, sc->rssi_2ghz[ant]); 1681203134Sthompsa sc->rssi_2ghz[ant] = 0; 1682203134Sthompsa } 1683203134Sthompsa if (sc->rssi_5ghz[ant] < -10 || sc->rssi_5ghz[ant] > 10) { 1684203134Sthompsa DPRINTF("invalid RSSI%d offset: %d (5GHz)\n", 1685203134Sthompsa ant + 1, sc->rssi_5ghz[ant]); 1686203134Sthompsa sc->rssi_5ghz[ant] = 0; 1687203134Sthompsa } 1688203134Sthompsa } 1689209917Sthompsa return (0); 1690203134Sthompsa} 1691203134Sthompsa 1692218676Shselaskystatic struct ieee80211_node * 1693203134Sthompsarun_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN]) 1694203134Sthompsa{ 1695203134Sthompsa return malloc(sizeof (struct run_node), M_DEVBUF, M_NOWAIT | M_ZERO); 1696203134Sthompsa} 1697203134Sthompsa 1698203134Sthompsastatic int 1699203134Sthompsarun_media_change(struct ifnet *ifp) 1700203134Sthompsa{ 1701208019Sthompsa struct ieee80211vap *vap = ifp->if_softc; 1702208019Sthompsa struct ieee80211com *ic = vap->iv_ic; 1703203134Sthompsa const struct ieee80211_txparam *tp; 1704208019Sthompsa struct run_softc *sc = ic->ic_ifp->if_softc; 1705203134Sthompsa uint8_t rate, ridx; 1706203134Sthompsa int error; 1707203134Sthompsa 1708203134Sthompsa RUN_LOCK(sc); 1709203134Sthompsa 1710203134Sthompsa error = ieee80211_media_change(ifp); 1711209917Sthompsa if (error != ENETRESET) { 1712203134Sthompsa RUN_UNLOCK(sc); 1713209917Sthompsa return (error); 1714208019Sthompsa } 1715203134Sthompsa 1716203134Sthompsa tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1717203134Sthompsa if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) { 1718212127Sthompsa struct ieee80211_node *ni; 1719212127Sthompsa struct run_node *rn; 1720212127Sthompsa 1721203134Sthompsa rate = ic->ic_sup_rates[ic->ic_curmode]. 1722203134Sthompsa rs_rates[tp->ucastrate] & IEEE80211_RATE_VAL; 1723203134Sthompsa for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++) 1724203134Sthompsa if (rt2860_rates[ridx].rate == rate) 1725203134Sthompsa break; 1726212127Sthompsa ni = ieee80211_ref_node(vap->iv_bss); 1727212127Sthompsa rn = (struct run_node *)ni; 1728208019Sthompsa rn->fix_ridx = ridx; 1729208019Sthompsa DPRINTF("rate=%d, fix_ridx=%d\n", rate, rn->fix_ridx); 1730212127Sthompsa ieee80211_free_node(ni); 1731203134Sthompsa } 1732203134Sthompsa 1733208019Sthompsa#if 0 1734203134Sthompsa if ((ifp->if_flags & IFF_UP) && 1735203134Sthompsa (ifp->if_drv_flags & IFF_DRV_RUNNING)){ 1736203134Sthompsa run_init_locked(sc); 1737203134Sthompsa } 1738208019Sthompsa#endif 1739203134Sthompsa 1740203134Sthompsa RUN_UNLOCK(sc); 1741203134Sthompsa 1742209917Sthompsa return (0); 1743203134Sthompsa} 1744203134Sthompsa 1745203134Sthompsastatic int 1746203134Sthompsarun_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1747203134Sthompsa{ 1748203134Sthompsa const struct ieee80211_txparam *tp; 1749203134Sthompsa struct ieee80211com *ic = vap->iv_ic; 1750203134Sthompsa struct run_softc *sc = ic->ic_ifp->if_softc; 1751203134Sthompsa struct run_vap *rvp = RUN_VAP(vap); 1752203134Sthompsa enum ieee80211_state ostate; 1753208019Sthompsa uint32_t sta[3]; 1754203134Sthompsa uint32_t tmp; 1755208019Sthompsa uint8_t ratectl; 1756208019Sthompsa uint8_t restart_ratectl = 0; 1757208019Sthompsa uint8_t bid = 1 << rvp->rvp_id; 1758203134Sthompsa 1759203134Sthompsa ostate = vap->iv_state; 1760203134Sthompsa DPRINTF("%s -> %s\n", 1761203134Sthompsa ieee80211_state_name[ostate], 1762203134Sthompsa ieee80211_state_name[nstate]); 1763203134Sthompsa 1764203134Sthompsa IEEE80211_UNLOCK(ic); 1765203134Sthompsa RUN_LOCK(sc); 1766203134Sthompsa 1767208019Sthompsa ratectl = sc->ratectl_run; /* remember current state */ 1768208019Sthompsa sc->ratectl_run = RUN_RATECTL_OFF; 1769208019Sthompsa usb_callout_stop(&sc->ratectl_ch); 1770203134Sthompsa 1771203134Sthompsa if (ostate == IEEE80211_S_RUN) { 1772203134Sthompsa /* turn link LED off */ 1773203134Sthompsa run_set_leds(sc, RT2860_LED_RADIO); 1774203134Sthompsa } 1775203134Sthompsa 1776203134Sthompsa switch (nstate) { 1777203134Sthompsa case IEEE80211_S_INIT: 1778208019Sthompsa restart_ratectl = 1; 1779208019Sthompsa 1780208019Sthompsa if (ostate != IEEE80211_S_RUN) 1781208019Sthompsa break; 1782208019Sthompsa 1783208019Sthompsa ratectl &= ~bid; 1784208019Sthompsa sc->runbmap &= ~bid; 1785208019Sthompsa 1786208019Sthompsa /* abort TSF synchronization if there is no vap running */ 1787209917Sthompsa if (--sc->running == 0) { 1788203134Sthompsa run_read(sc, RT2860_BCN_TIME_CFG, &tmp); 1789203134Sthompsa run_write(sc, RT2860_BCN_TIME_CFG, 1790203134Sthompsa tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN | 1791203134Sthompsa RT2860_TBTT_TIMER_EN)); 1792203134Sthompsa } 1793203134Sthompsa break; 1794203134Sthompsa 1795203134Sthompsa case IEEE80211_S_RUN: 1796209917Sthompsa if (!(sc->runbmap & bid)) { 1797208019Sthompsa if(sc->running++) 1798208019Sthompsa restart_ratectl = 1; 1799208019Sthompsa sc->runbmap |= bid; 1800208019Sthompsa } 1801203134Sthompsa 1802218492Sbschmidt m_freem(rvp->beacon_mbuf); 1803218492Sbschmidt rvp->beacon_mbuf = NULL; 1804218492Sbschmidt 1805209917Sthompsa switch (vap->iv_opmode) { 1806208019Sthompsa case IEEE80211_M_HOSTAP: 1807208019Sthompsa case IEEE80211_M_MBSS: 1808208019Sthompsa sc->ap_running |= bid; 1809208019Sthompsa ic->ic_opmode = vap->iv_opmode; 1810208019Sthompsa run_update_beacon_cb(vap); 1811208019Sthompsa break; 1812208019Sthompsa case IEEE80211_M_IBSS: 1813208019Sthompsa sc->adhoc_running |= bid; 1814209917Sthompsa if (!sc->ap_running) 1815208019Sthompsa ic->ic_opmode = vap->iv_opmode; 1816208019Sthompsa run_update_beacon_cb(vap); 1817208019Sthompsa break; 1818208019Sthompsa case IEEE80211_M_STA: 1819208019Sthompsa sc->sta_running |= bid; 1820209917Sthompsa if (!sc->ap_running && !sc->adhoc_running) 1821208019Sthompsa ic->ic_opmode = vap->iv_opmode; 1822208019Sthompsa 1823208019Sthompsa /* read statistic counters (clear on read) */ 1824208019Sthompsa run_read_region_1(sc, RT2860_TX_STA_CNT0, 1825208019Sthompsa (uint8_t *)sta, sizeof sta); 1826208019Sthompsa 1827208019Sthompsa break; 1828208019Sthompsa default: 1829208019Sthompsa ic->ic_opmode = vap->iv_opmode; 1830208019Sthompsa break; 1831208019Sthompsa } 1832208019Sthompsa 1833203134Sthompsa if (vap->iv_opmode != IEEE80211_M_MONITOR) { 1834212127Sthompsa struct ieee80211_node *ni; 1835212127Sthompsa 1836236439Shselasky if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) { 1837236439Shselasky RUN_UNLOCK(sc); 1838236439Shselasky IEEE80211_LOCK(ic); 1839236439Shselasky return (-1); 1840236439Shselasky } 1841203134Sthompsa run_updateslot(ic->ic_ifp); 1842203134Sthompsa run_enable_mrr(sc); 1843203134Sthompsa run_set_txpreamble(sc); 1844203134Sthompsa run_set_basicrates(sc); 1845212127Sthompsa ni = ieee80211_ref_node(vap->iv_bss); 1846203134Sthompsa IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid); 1847203134Sthompsa run_set_bssid(sc, ni->ni_bssid); 1848212127Sthompsa ieee80211_free_node(ni); 1849208019Sthompsa run_enable_tsf_sync(sc); 1850203134Sthompsa 1851208019Sthompsa /* enable automatic rate adaptation */ 1852208019Sthompsa tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1853208019Sthompsa if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) 1854208019Sthompsa ratectl |= bid; 1855203134Sthompsa } 1856203134Sthompsa 1857203134Sthompsa /* turn link LED on */ 1858203134Sthompsa run_set_leds(sc, RT2860_LED_RADIO | 1859208019Sthompsa (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan) ? 1860203134Sthompsa RT2860_LED_LINK_2GHZ : RT2860_LED_LINK_5GHZ)); 1861203134Sthompsa 1862203134Sthompsa break; 1863203134Sthompsa default: 1864203134Sthompsa DPRINTFN(6, "undefined case\n"); 1865203134Sthompsa break; 1866203134Sthompsa } 1867203134Sthompsa 1868208019Sthompsa /* restart amrr for running VAPs */ 1869209917Sthompsa if ((sc->ratectl_run = ratectl) && restart_ratectl) 1870208019Sthompsa usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc); 1871208019Sthompsa 1872203134Sthompsa RUN_UNLOCK(sc); 1873203134Sthompsa IEEE80211_LOCK(ic); 1874203134Sthompsa 1875203134Sthompsa return(rvp->newstate(vap, nstate, arg)); 1876203134Sthompsa} 1877203134Sthompsa 1878203134Sthompsa/* ARGSUSED */ 1879203134Sthompsastatic void 1880208019Sthompsarun_wme_update_cb(void *arg) 1881203134Sthompsa{ 1882203134Sthompsa struct ieee80211com *ic = arg; 1883203134Sthompsa struct run_softc *sc = ic->ic_ifp->if_softc; 1884203134Sthompsa struct ieee80211_wme_state *wmesp = &ic->ic_wme; 1885203134Sthompsa int aci, error = 0; 1886203134Sthompsa 1887208019Sthompsa RUN_LOCK_ASSERT(sc, MA_OWNED); 1888203134Sthompsa 1889203134Sthompsa /* update MAC TX configuration registers */ 1890203134Sthompsa for (aci = 0; aci < WME_NUM_AC; aci++) { 1891203134Sthompsa error = run_write(sc, RT2860_EDCA_AC_CFG(aci), 1892203134Sthompsa wmesp->wme_params[aci].wmep_logcwmax << 16 | 1893203134Sthompsa wmesp->wme_params[aci].wmep_logcwmin << 12 | 1894203134Sthompsa wmesp->wme_params[aci].wmep_aifsn << 8 | 1895203134Sthompsa wmesp->wme_params[aci].wmep_txopLimit); 1896209917Sthompsa if (error) goto err; 1897203134Sthompsa } 1898203134Sthompsa 1899203134Sthompsa /* update SCH/DMA registers too */ 1900203134Sthompsa error = run_write(sc, RT2860_WMM_AIFSN_CFG, 1901203134Sthompsa wmesp->wme_params[WME_AC_VO].wmep_aifsn << 12 | 1902203134Sthompsa wmesp->wme_params[WME_AC_VI].wmep_aifsn << 8 | 1903203134Sthompsa wmesp->wme_params[WME_AC_BK].wmep_aifsn << 4 | 1904203134Sthompsa wmesp->wme_params[WME_AC_BE].wmep_aifsn); 1905209917Sthompsa if (error) goto err; 1906203134Sthompsa error = run_write(sc, RT2860_WMM_CWMIN_CFG, 1907203134Sthompsa wmesp->wme_params[WME_AC_VO].wmep_logcwmin << 12 | 1908203134Sthompsa wmesp->wme_params[WME_AC_VI].wmep_logcwmin << 8 | 1909203134Sthompsa wmesp->wme_params[WME_AC_BK].wmep_logcwmin << 4 | 1910203134Sthompsa wmesp->wme_params[WME_AC_BE].wmep_logcwmin); 1911209917Sthompsa if (error) goto err; 1912203134Sthompsa error = run_write(sc, RT2860_WMM_CWMAX_CFG, 1913203134Sthompsa wmesp->wme_params[WME_AC_VO].wmep_logcwmax << 12 | 1914203134Sthompsa wmesp->wme_params[WME_AC_VI].wmep_logcwmax << 8 | 1915203134Sthompsa wmesp->wme_params[WME_AC_BK].wmep_logcwmax << 4 | 1916203134Sthompsa wmesp->wme_params[WME_AC_BE].wmep_logcwmax); 1917209917Sthompsa if (error) goto err; 1918203134Sthompsa error = run_write(sc, RT2860_WMM_TXOP0_CFG, 1919203134Sthompsa wmesp->wme_params[WME_AC_BK].wmep_txopLimit << 16 | 1920203134Sthompsa wmesp->wme_params[WME_AC_BE].wmep_txopLimit); 1921209917Sthompsa if (error) goto err; 1922203134Sthompsa error = run_write(sc, RT2860_WMM_TXOP1_CFG, 1923203134Sthompsa wmesp->wme_params[WME_AC_VO].wmep_txopLimit << 16 | 1924203134Sthompsa wmesp->wme_params[WME_AC_VI].wmep_txopLimit); 1925203134Sthompsa 1926203134Sthompsaerr: 1927209917Sthompsa if (error) 1928203134Sthompsa DPRINTF("WME update failed\n"); 1929203134Sthompsa 1930203134Sthompsa return; 1931203134Sthompsa} 1932203134Sthompsa 1933208019Sthompsastatic int 1934208019Sthompsarun_wme_update(struct ieee80211com *ic) 1935208019Sthompsa{ 1936208019Sthompsa struct run_softc *sc = ic->ic_ifp->if_softc; 1937208019Sthompsa 1938208019Sthompsa /* sometime called wothout lock */ 1939209917Sthompsa if (mtx_owned(&ic->ic_comlock.mtx)) { 1940208019Sthompsa uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store); 1941208019Sthompsa DPRINTF("cmdq_store=%d\n", i); 1942208019Sthompsa sc->cmdq[i].func = run_wme_update_cb; 1943208019Sthompsa sc->cmdq[i].arg0 = ic; 1944208019Sthompsa ieee80211_runtask(ic, &sc->cmdq_task); 1945209918Sthompsa return (0); 1946208019Sthompsa } 1947208019Sthompsa 1948208019Sthompsa RUN_LOCK(sc); 1949208019Sthompsa run_wme_update_cb(ic); 1950208019Sthompsa RUN_UNLOCK(sc); 1951208019Sthompsa 1952208019Sthompsa /* return whatever, upper layer desn't care anyway */ 1953208019Sthompsa return (0); 1954208019Sthompsa} 1955208019Sthompsa 1956203134Sthompsastatic void 1957203134Sthompsarun_key_update_begin(struct ieee80211vap *vap) 1958203134Sthompsa{ 1959203134Sthompsa /* 1960208019Sthompsa * To avoid out-of-order events, both run_key_set() and 1961208019Sthompsa * _delete() are deferred and handled by run_cmdq_cb(). 1962208019Sthompsa * So, there is nothing we need to do here. 1963203134Sthompsa */ 1964203134Sthompsa} 1965203134Sthompsa 1966203134Sthompsastatic void 1967203134Sthompsarun_key_update_end(struct ieee80211vap *vap) 1968203134Sthompsa{ 1969203134Sthompsa /* null */ 1970203134Sthompsa} 1971203134Sthompsa 1972208019Sthompsastatic void 1973208019Sthompsarun_key_set_cb(void *arg) 1974203134Sthompsa{ 1975208019Sthompsa struct run_cmdq *cmdq = arg; 1976208019Sthompsa struct ieee80211vap *vap = cmdq->arg1; 1977208019Sthompsa struct ieee80211_key *k = cmdq->k; 1978203134Sthompsa struct ieee80211com *ic = vap->iv_ic; 1979208019Sthompsa struct run_softc *sc = ic->ic_ifp->if_softc; 1980203134Sthompsa struct ieee80211_node *ni; 1981203134Sthompsa uint32_t attr; 1982203134Sthompsa uint16_t base, associd; 1983209144Sthompsa uint8_t mode, wcid, iv[8]; 1984203134Sthompsa 1985208019Sthompsa RUN_LOCK_ASSERT(sc, MA_OWNED); 1986203134Sthompsa 1987209917Sthompsa if (vap->iv_opmode == IEEE80211_M_HOSTAP) 1988208019Sthompsa ni = ieee80211_find_vap_node(&ic->ic_sta, vap, cmdq->mac); 1989209144Sthompsa else 1990203134Sthompsa ni = vap->iv_bss; 1991208019Sthompsa associd = (ni != NULL) ? ni->ni_associd : 0; 1992203134Sthompsa 1993203134Sthompsa /* map net80211 cipher to RT2860 security mode */ 1994203134Sthompsa switch (k->wk_cipher->ic_cipher) { 1995203134Sthompsa case IEEE80211_CIPHER_WEP: 1996203134Sthompsa if(k->wk_keylen < 8) 1997203134Sthompsa mode = RT2860_MODE_WEP40; 1998203134Sthompsa else 1999203134Sthompsa mode = RT2860_MODE_WEP104; 2000203134Sthompsa break; 2001203134Sthompsa case IEEE80211_CIPHER_TKIP: 2002203134Sthompsa mode = RT2860_MODE_TKIP; 2003203134Sthompsa break; 2004203134Sthompsa case IEEE80211_CIPHER_AES_CCM: 2005203134Sthompsa mode = RT2860_MODE_AES_CCMP; 2006203134Sthompsa break; 2007203134Sthompsa default: 2008203134Sthompsa DPRINTF("undefined case\n"); 2009208019Sthompsa return; 2010203134Sthompsa } 2011203134Sthompsa 2012208019Sthompsa DPRINTFN(1, "associd=%x, keyix=%d, mode=%x, type=%s, tx=%s, rx=%s\n", 2013203134Sthompsa associd, k->wk_keyix, mode, 2014208019Sthompsa (k->wk_flags & IEEE80211_KEY_GROUP) ? "group" : "pairwise", 2015208019Sthompsa (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off", 2016208019Sthompsa (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off"); 2017203134Sthompsa 2018203134Sthompsa if (k->wk_flags & IEEE80211_KEY_GROUP) { 2019203134Sthompsa wcid = 0; /* NB: update WCID0 for group keys */ 2020208019Sthompsa base = RT2860_SKEY(RUN_VAP(vap)->rvp_id, k->wk_keyix); 2021203134Sthompsa } else { 2022203134Sthompsa wcid = RUN_AID2WCID(associd); 2023203134Sthompsa base = RT2860_PKEY(wcid); 2024203134Sthompsa } 2025203134Sthompsa 2026203134Sthompsa if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) { 2027203134Sthompsa if(run_write_region_1(sc, base, k->wk_key, 16)) 2028208019Sthompsa return; 2029209144Sthompsa if(run_write_region_1(sc, base + 16, &k->wk_key[16], 8)) /* wk_txmic */ 2030208019Sthompsa return; 2031209144Sthompsa if(run_write_region_1(sc, base + 24, &k->wk_key[24], 8)) /* wk_rxmic */ 2032208019Sthompsa return; 2033203134Sthompsa } else { 2034203134Sthompsa /* roundup len to 16-bit: XXX fix write_region_1() instead */ 2035203134Sthompsa if(run_write_region_1(sc, base, k->wk_key, (k->wk_keylen + 1) & ~1)) 2036208019Sthompsa return; 2037203134Sthompsa } 2038203134Sthompsa 2039203134Sthompsa if (!(k->wk_flags & IEEE80211_KEY_GROUP) || 2040203134Sthompsa (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))) { 2041203134Sthompsa /* set initial packet number in IV+EIV */ 2042209917Sthompsa if (k->wk_cipher == IEEE80211_CIPHER_WEP) { 2043203134Sthompsa memset(iv, 0, sizeof iv); 2044208019Sthompsa iv[3] = vap->iv_def_txkey << 6; 2045203134Sthompsa } else { 2046203134Sthompsa if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) { 2047203134Sthompsa iv[0] = k->wk_keytsc >> 8; 2048203134Sthompsa iv[1] = (iv[0] | 0x20) & 0x7f; 2049203134Sthompsa iv[2] = k->wk_keytsc; 2050203134Sthompsa } else /* CCMP */ { 2051203134Sthompsa iv[0] = k->wk_keytsc; 2052203134Sthompsa iv[1] = k->wk_keytsc >> 8; 2053203134Sthompsa iv[2] = 0; 2054203134Sthompsa } 2055203134Sthompsa iv[3] = k->wk_keyix << 6 | IEEE80211_WEP_EXTIV; 2056203134Sthompsa iv[4] = k->wk_keytsc >> 16; 2057203134Sthompsa iv[5] = k->wk_keytsc >> 24; 2058203134Sthompsa iv[6] = k->wk_keytsc >> 32; 2059203134Sthompsa iv[7] = k->wk_keytsc >> 40; 2060203134Sthompsa } 2061209917Sthompsa if (run_write_region_1(sc, RT2860_IVEIV(wcid), iv, 8)) 2062208019Sthompsa return; 2063203134Sthompsa } 2064203134Sthompsa 2065203134Sthompsa if (k->wk_flags & IEEE80211_KEY_GROUP) { 2066203134Sthompsa /* install group key */ 2067209917Sthompsa if (run_read(sc, RT2860_SKEY_MODE_0_7, &attr)) 2068208019Sthompsa return; 2069203134Sthompsa attr &= ~(0xf << (k->wk_keyix * 4)); 2070203134Sthompsa attr |= mode << (k->wk_keyix * 4); 2071209917Sthompsa if (run_write(sc, RT2860_SKEY_MODE_0_7, attr)) 2072208019Sthompsa return; 2073203134Sthompsa } else { 2074203134Sthompsa /* install pairwise key */ 2075209917Sthompsa if (run_read(sc, RT2860_WCID_ATTR(wcid), &attr)) 2076208019Sthompsa return; 2077203134Sthompsa attr = (attr & ~0xf) | (mode << 1) | RT2860_RX_PKEY_EN; 2078209917Sthompsa if (run_write(sc, RT2860_WCID_ATTR(wcid), attr)) 2079208019Sthompsa return; 2080203134Sthompsa } 2081203134Sthompsa 2082203134Sthompsa /* TODO create a pass-thru key entry? */ 2083203134Sthompsa 2084208019Sthompsa /* need wcid to delete the right key later */ 2085208019Sthompsa k->wk_pad = wcid; 2086203134Sthompsa} 2087203134Sthompsa 2088203134Sthompsa/* 2089208019Sthompsa * Don't have to be deferred, but in order to keep order of 2090208019Sthompsa * execution, i.e. with run_key_delete(), defer this and let 2091208019Sthompsa * run_cmdq_cb() maintain the order. 2092208019Sthompsa * 2093203134Sthompsa * return 0 on error 2094203134Sthompsa */ 2095203134Sthompsastatic int 2096208019Sthompsarun_key_set(struct ieee80211vap *vap, struct ieee80211_key *k, 2097208019Sthompsa const uint8_t mac[IEEE80211_ADDR_LEN]) 2098203134Sthompsa{ 2099203134Sthompsa struct ieee80211com *ic = vap->iv_ic; 2100203134Sthompsa struct run_softc *sc = ic->ic_ifp->if_softc; 2101208019Sthompsa uint32_t i; 2102208019Sthompsa 2103208019Sthompsa i = RUN_CMDQ_GET(&sc->cmdq_store); 2104208019Sthompsa DPRINTF("cmdq_store=%d\n", i); 2105208019Sthompsa sc->cmdq[i].func = run_key_set_cb; 2106208019Sthompsa sc->cmdq[i].arg0 = NULL; 2107208019Sthompsa sc->cmdq[i].arg1 = vap; 2108208019Sthompsa sc->cmdq[i].k = k; 2109208019Sthompsa IEEE80211_ADDR_COPY(sc->cmdq[i].mac, mac); 2110208019Sthompsa ieee80211_runtask(ic, &sc->cmdq_task); 2111208019Sthompsa 2112209144Sthompsa /* 2113209144Sthompsa * To make sure key will be set when hostapd 2114209144Sthompsa * calls iv_key_set() before if_init(). 2115209144Sthompsa */ 2116209917Sthompsa if (vap->iv_opmode == IEEE80211_M_HOSTAP) { 2117209144Sthompsa RUN_LOCK(sc); 2118209144Sthompsa sc->cmdq_key_set = RUN_CMDQ_GO; 2119209144Sthompsa RUN_UNLOCK(sc); 2120209144Sthompsa } 2121209144Sthompsa 2122209917Sthompsa return (1); 2123208019Sthompsa} 2124208019Sthompsa 2125208019Sthompsa/* 2126208019Sthompsa * If wlan is destroyed without being brought down i.e. without 2127208019Sthompsa * wlan down or wpa_cli terminate, this function is called after 2128208019Sthompsa * vap is gone. Don't refer it. 2129208019Sthompsa */ 2130208019Sthompsastatic void 2131208019Sthompsarun_key_delete_cb(void *arg) 2132208019Sthompsa{ 2133208019Sthompsa struct run_cmdq *cmdq = arg; 2134208019Sthompsa struct run_softc *sc = cmdq->arg1; 2135208019Sthompsa struct ieee80211_key *k = &cmdq->key; 2136203134Sthompsa uint32_t attr; 2137203134Sthompsa uint8_t wcid; 2138203134Sthompsa 2139208019Sthompsa RUN_LOCK_ASSERT(sc, MA_OWNED); 2140203134Sthompsa 2141203134Sthompsa if (k->wk_flags & IEEE80211_KEY_GROUP) { 2142203134Sthompsa /* remove group key */ 2143208019Sthompsa DPRINTF("removing group key\n"); 2144208019Sthompsa run_read(sc, RT2860_SKEY_MODE_0_7, &attr); 2145203134Sthompsa attr &= ~(0xf << (k->wk_keyix * 4)); 2146208019Sthompsa run_write(sc, RT2860_SKEY_MODE_0_7, attr); 2147203134Sthompsa } else { 2148203134Sthompsa /* remove pairwise key */ 2149208019Sthompsa DPRINTF("removing key for wcid %x\n", k->wk_pad); 2150208019Sthompsa /* matching wcid was written to wk_pad in run_key_set() */ 2151208019Sthompsa wcid = k->wk_pad; 2152208019Sthompsa run_read(sc, RT2860_WCID_ATTR(wcid), &attr); 2153203134Sthompsa attr &= ~0xf; 2154208019Sthompsa run_write(sc, RT2860_WCID_ATTR(wcid), attr); 2155208019Sthompsa run_set_region_4(sc, RT2860_WCID_ENTRY(wcid), 0, 8); 2156203134Sthompsa } 2157203134Sthompsa 2158208019Sthompsa k->wk_pad = 0; 2159203134Sthompsa} 2160203134Sthompsa 2161208019Sthompsa/* 2162208019Sthompsa * return 0 on error 2163208019Sthompsa */ 2164208019Sthompsastatic int 2165208019Sthompsarun_key_delete(struct ieee80211vap *vap, struct ieee80211_key *k) 2166203134Sthompsa{ 2167208019Sthompsa struct ieee80211com *ic = vap->iv_ic; 2168208019Sthompsa struct run_softc *sc = ic->ic_ifp->if_softc; 2169208019Sthompsa struct ieee80211_key *k0; 2170208019Sthompsa uint32_t i; 2171203134Sthompsa 2172208019Sthompsa /* 2173208019Sthompsa * When called back, key might be gone. So, make a copy 2174208019Sthompsa * of some values need to delete keys before deferring. 2175208019Sthompsa * But, because of LOR with node lock, cannot use lock here. 2176208019Sthompsa * So, use atomic instead. 2177208019Sthompsa */ 2178208019Sthompsa i = RUN_CMDQ_GET(&sc->cmdq_store); 2179208019Sthompsa DPRINTF("cmdq_store=%d\n", i); 2180208019Sthompsa sc->cmdq[i].func = run_key_delete_cb; 2181208019Sthompsa sc->cmdq[i].arg0 = NULL; 2182208019Sthompsa sc->cmdq[i].arg1 = sc; 2183208019Sthompsa k0 = &sc->cmdq[i].key; 2184208019Sthompsa k0->wk_flags = k->wk_flags; 2185208019Sthompsa k0->wk_keyix = k->wk_keyix; 2186208019Sthompsa /* matching wcid was written to wk_pad in run_key_set() */ 2187208019Sthompsa k0->wk_pad = k->wk_pad; 2188208019Sthompsa ieee80211_runtask(ic, &sc->cmdq_task); 2189208019Sthompsa return (1); /* return fake success */ 2190203134Sthompsa 2191203134Sthompsa} 2192203134Sthompsa 2193203134Sthompsastatic void 2194206358Srpaulorun_ratectl_to(void *arg) 2195203134Sthompsa{ 2196208019Sthompsa struct run_softc *sc = arg; 2197203134Sthompsa 2198203134Sthompsa /* do it in a process context, so it can go sleep */ 2199208019Sthompsa ieee80211_runtask(sc->sc_ifp->if_l2com, &sc->ratectl_task); 2200203134Sthompsa /* next timeout will be rescheduled in the callback task */ 2201203134Sthompsa} 2202203134Sthompsa 2203203134Sthompsa/* ARGSUSED */ 2204203134Sthompsastatic void 2205206358Srpaulorun_ratectl_cb(void *arg, int pending) 2206203134Sthompsa{ 2207208019Sthompsa struct run_softc *sc = arg; 2208208019Sthompsa struct ieee80211com *ic = sc->sc_ifp->if_l2com; 2209208019Sthompsa struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2210203134Sthompsa 2211209917Sthompsa if (vap == NULL) 2212208019Sthompsa return; 2213208019Sthompsa 2214209917Sthompsa if (sc->rvp_cnt <= 1 && vap->iv_opmode == IEEE80211_M_STA) 2215208019Sthompsa run_iter_func(sc, vap->iv_bss); 2216203134Sthompsa else { 2217203134Sthompsa /* 2218203134Sthompsa * run_reset_livelock() doesn't do anything with AMRR, 2219203134Sthompsa * but Ralink wants us to call it every 1 sec. So, we 2220203134Sthompsa * piggyback here rather than creating another callout. 2221203134Sthompsa * Livelock may occur only in HOSTAP or IBSS mode 2222203134Sthompsa * (when h/w is sending beacons). 2223203134Sthompsa */ 2224203134Sthompsa RUN_LOCK(sc); 2225203134Sthompsa run_reset_livelock(sc); 2226208019Sthompsa /* just in case, there are some stats to drain */ 2227208019Sthompsa run_drain_fifo(sc); 2228203134Sthompsa RUN_UNLOCK(sc); 2229208019Sthompsa ieee80211_iterate_nodes(&ic->ic_sta, run_iter_func, sc); 2230203134Sthompsa } 2231203134Sthompsa 2232208019Sthompsa if(sc->ratectl_run != RUN_RATECTL_OFF) 2233208019Sthompsa usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc); 2234203134Sthompsa} 2235203134Sthompsa 2236203134Sthompsastatic void 2237208019Sthompsarun_drain_fifo(void *arg) 2238203134Sthompsa{ 2239208019Sthompsa struct run_softc *sc = arg; 2240208019Sthompsa struct ifnet *ifp = sc->sc_ifp; 2241208019Sthompsa uint32_t stat; 2242218676Shselasky uint16_t (*wstat)[3]; 2243203134Sthompsa uint8_t wcid, mcs, pid; 2244218676Shselasky int8_t retry; 2245203134Sthompsa 2246208019Sthompsa RUN_LOCK_ASSERT(sc, MA_OWNED); 2247203134Sthompsa 2248208019Sthompsa for (;;) { 2249203134Sthompsa /* drain Tx status FIFO (maxsize = 16) */ 2250203134Sthompsa run_read(sc, RT2860_TX_STAT_FIFO, &stat); 2251208019Sthompsa DPRINTFN(4, "tx stat 0x%08x\n", stat); 2252209917Sthompsa if (!(stat & RT2860_TXQ_VLD)) 2253208019Sthompsa break; 2254203134Sthompsa 2255208019Sthompsa wcid = (stat >> RT2860_TXQ_WCID_SHIFT) & 0xff; 2256203134Sthompsa 2257208019Sthompsa /* if no ACK was requested, no feedback is available */ 2258208019Sthompsa if (!(stat & RT2860_TXQ_ACKREQ) || wcid > RT2870_WCID_MAX || 2259208019Sthompsa wcid == 0) 2260208019Sthompsa continue; 2261203134Sthompsa 2262218676Shselasky /* 2263218676Shselasky * Even though each stat is Tx-complete-status like format, 2264218676Shselasky * the device can poll stats. Because there is no guarantee 2265218676Shselasky * that the referring node is still around when read the stats. 2266218676Shselasky * So that, if we use ieee80211_ratectl_tx_update(), we will 2267218676Shselasky * have hard time not to refer already freed node. 2268218676Shselasky * 2269218676Shselasky * To eliminate such page faults, we poll stats in softc. 2270218676Shselasky * Then, update the rates later with ieee80211_ratectl_tx_update(). 2271218676Shselasky */ 2272218676Shselasky wstat = &(sc->wcid_stats[wcid]); 2273218676Shselasky (*wstat)[RUN_TXCNT]++; 2274218676Shselasky if (stat & RT2860_TXQ_OK) 2275218676Shselasky (*wstat)[RUN_SUCCESS]++; 2276218676Shselasky else 2277208019Sthompsa ifp->if_oerrors++; 2278218676Shselasky /* 2279218676Shselasky * Check if there were retries, ie if the Tx success rate is 2280218676Shselasky * different from the requested rate. Note that it works only 2281218676Shselasky * because we do not allow rate fallback from OFDM to CCK. 2282218676Shselasky */ 2283218676Shselasky mcs = (stat >> RT2860_TXQ_MCS_SHIFT) & 0x7f; 2284218676Shselasky pid = (stat >> RT2860_TXQ_PID_SHIFT) & 0xf; 2285218676Shselasky if ((retry = pid -1 - mcs) > 0) { 2286218676Shselasky (*wstat)[RUN_TXCNT] += retry; 2287218676Shselasky (*wstat)[RUN_RETRY] += retry; 2288203134Sthompsa } 2289208019Sthompsa } 2290208019Sthompsa DPRINTFN(3, "count=%d\n", sc->fifo_cnt); 2291208019Sthompsa 2292208019Sthompsa sc->fifo_cnt = 0; 2293208019Sthompsa} 2294208019Sthompsa 2295208019Sthompsastatic void 2296208019Sthompsarun_iter_func(void *arg, struct ieee80211_node *ni) 2297208019Sthompsa{ 2298208019Sthompsa struct run_softc *sc = arg; 2299208019Sthompsa struct ieee80211vap *vap = ni->ni_vap; 2300208019Sthompsa struct ieee80211com *ic = ni->ni_ic; 2301208019Sthompsa struct ifnet *ifp = ic->ic_ifp; 2302208019Sthompsa struct run_node *rn = (void *)ni; 2303218676Shselasky union run_stats sta[2]; 2304218676Shselasky uint16_t (*wstat)[3]; 2305218676Shselasky int txcnt, success, retrycnt, error; 2306208019Sthompsa 2307218676Shselasky RUN_LOCK(sc); 2308218676Shselasky 2309209917Sthompsa if (sc->rvp_cnt <= 1 && (vap->iv_opmode == IEEE80211_M_IBSS || 2310209917Sthompsa vap->iv_opmode == IEEE80211_M_STA)) { 2311203134Sthompsa /* read statistic counters (clear on read) and update AMRR state */ 2312203134Sthompsa error = run_read_region_1(sc, RT2860_TX_STA_CNT0, (uint8_t *)sta, 2313203134Sthompsa sizeof sta); 2314203134Sthompsa if (error != 0) 2315218676Shselasky goto fail; 2316203134Sthompsa 2317203134Sthompsa /* count failed TX as errors */ 2318218676Shselasky ifp->if_oerrors += le16toh(sta[0].error.fail); 2319203134Sthompsa 2320218676Shselasky retrycnt = le16toh(sta[1].tx.retry); 2321218676Shselasky success = le16toh(sta[1].tx.success); 2322218676Shselasky txcnt = retrycnt + success + le16toh(sta[0].error.fail); 2323203134Sthompsa 2324218676Shselasky DPRINTFN(3, "retrycnt=%d success=%d failcnt=%d\n", 2325218676Shselasky retrycnt, success, le16toh(sta[0].error.fail)); 2326218676Shselasky } else { 2327218676Shselasky wstat = &(sc->wcid_stats[RUN_AID2WCID(ni->ni_associd)]); 2328203134Sthompsa 2329218676Shselasky if (wstat == &(sc->wcid_stats[0]) || 2330218676Shselasky wstat > &(sc->wcid_stats[RT2870_WCID_MAX])) 2331218676Shselasky goto fail; 2332208019Sthompsa 2333218676Shselasky txcnt = (*wstat)[RUN_TXCNT]; 2334218676Shselasky success = (*wstat)[RUN_SUCCESS]; 2335218676Shselasky retrycnt = (*wstat)[RUN_RETRY]; 2336218676Shselasky DPRINTFN(3, "retrycnt=%d txcnt=%d success=%d\n", 2337218676Shselasky retrycnt, txcnt, success); 2338208019Sthompsa 2339218676Shselasky memset(wstat, 0, sizeof(*wstat)); 2340203134Sthompsa } 2341203134Sthompsa 2342218676Shselasky ieee80211_ratectl_tx_update(vap, ni, &txcnt, &success, &retrycnt); 2343208019Sthompsa rn->amrr_ridx = ieee80211_ratectl_rate(ni, NULL, 0); 2344218676Shselasky 2345218676Shselaskyfail: 2346218676Shselasky RUN_UNLOCK(sc); 2347218676Shselasky 2348208019Sthompsa DPRINTFN(3, "ridx=%d\n", rn->amrr_ridx); 2349208019Sthompsa} 2350203134Sthompsa 2351208019Sthompsastatic void 2352208019Sthompsarun_newassoc_cb(void *arg) 2353208019Sthompsa{ 2354208019Sthompsa struct run_cmdq *cmdq = arg; 2355208019Sthompsa struct ieee80211_node *ni = cmdq->arg1; 2356208019Sthompsa struct run_softc *sc = ni->ni_vap->iv_ic->ic_ifp->if_softc; 2357208019Sthompsa uint8_t wcid = cmdq->wcid; 2358203134Sthompsa 2359208019Sthompsa RUN_LOCK_ASSERT(sc, MA_OWNED); 2360208019Sthompsa 2361208019Sthompsa run_write_region_1(sc, RT2860_WCID_ENTRY(wcid), 2362208019Sthompsa ni->ni_macaddr, IEEE80211_ADDR_LEN); 2363218676Shselasky 2364218676Shselasky memset(&(sc->wcid_stats[wcid]), 0, sizeof(sc->wcid_stats[wcid])); 2365203134Sthompsa} 2366203134Sthompsa 2367203134Sthompsastatic void 2368203134Sthompsarun_newassoc(struct ieee80211_node *ni, int isnew) 2369203134Sthompsa{ 2370203134Sthompsa struct run_node *rn = (void *)ni; 2371203134Sthompsa struct ieee80211_rateset *rs = &ni->ni_rates; 2372208019Sthompsa struct ieee80211vap *vap = ni->ni_vap; 2373208019Sthompsa struct ieee80211com *ic = vap->iv_ic; 2374208019Sthompsa struct run_softc *sc = ic->ic_ifp->if_softc; 2375203134Sthompsa uint8_t rate; 2376208019Sthompsa uint8_t ridx; 2377208019Sthompsa uint8_t wcid = RUN_AID2WCID(ni->ni_associd); 2378208019Sthompsa int i, j; 2379203134Sthompsa 2380209917Sthompsa if (wcid > RT2870_WCID_MAX) { 2381208019Sthompsa device_printf(sc->sc_dev, "wcid=%d out of range\n", wcid); 2382208019Sthompsa return; 2383208019Sthompsa } 2384203134Sthompsa 2385208019Sthompsa /* only interested in true associations */ 2386209917Sthompsa if (isnew && ni->ni_associd != 0) { 2387208019Sthompsa 2388208019Sthompsa /* 2389208019Sthompsa * This function could is called though timeout function. 2390208019Sthompsa * Need to defer. 2391208019Sthompsa */ 2392208019Sthompsa uint32_t cnt = RUN_CMDQ_GET(&sc->cmdq_store); 2393208019Sthompsa DPRINTF("cmdq_store=%d\n", cnt); 2394208019Sthompsa sc->cmdq[cnt].func = run_newassoc_cb; 2395208019Sthompsa sc->cmdq[cnt].arg0 = NULL; 2396208019Sthompsa sc->cmdq[cnt].arg1 = ni; 2397208019Sthompsa sc->cmdq[cnt].wcid = wcid; 2398208019Sthompsa ieee80211_runtask(ic, &sc->cmdq_task); 2399208019Sthompsa } 2400208019Sthompsa 2401208019Sthompsa DPRINTF("new assoc isnew=%d associd=%x addr=%s\n", 2402208019Sthompsa isnew, ni->ni_associd, ether_sprintf(ni->ni_macaddr)); 2403208019Sthompsa 2404203134Sthompsa for (i = 0; i < rs->rs_nrates; i++) { 2405203134Sthompsa rate = rs->rs_rates[i] & IEEE80211_RATE_VAL; 2406203134Sthompsa /* convert 802.11 rate to hardware rate index */ 2407203134Sthompsa for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++) 2408203134Sthompsa if (rt2860_rates[ridx].rate == rate) 2409203134Sthompsa break; 2410203134Sthompsa rn->ridx[i] = ridx; 2411203134Sthompsa /* determine rate of control response frames */ 2412203134Sthompsa for (j = i; j >= 0; j--) { 2413203134Sthompsa if ((rs->rs_rates[j] & IEEE80211_RATE_BASIC) && 2414203134Sthompsa rt2860_rates[rn->ridx[i]].phy == 2415203134Sthompsa rt2860_rates[rn->ridx[j]].phy) 2416203134Sthompsa break; 2417203134Sthompsa } 2418203134Sthompsa if (j >= 0) { 2419203134Sthompsa rn->ctl_ridx[i] = rn->ridx[j]; 2420203134Sthompsa } else { 2421203134Sthompsa /* no basic rate found, use mandatory one */ 2422203134Sthompsa rn->ctl_ridx[i] = rt2860_rates[ridx].ctl_ridx; 2423203134Sthompsa } 2424203134Sthompsa DPRINTF("rate=0x%02x ridx=%d ctl_ridx=%d\n", 2425203134Sthompsa rs->rs_rates[i], rn->ridx[i], rn->ctl_ridx[i]); 2426203134Sthompsa } 2427208019Sthompsa rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate; 2428208019Sthompsa for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++) 2429208019Sthompsa if (rt2860_rates[ridx].rate == rate) 2430208019Sthompsa break; 2431208019Sthompsa rn->mgt_ridx = ridx; 2432208019Sthompsa DPRINTF("rate=%d, mgmt_ridx=%d\n", rate, rn->mgt_ridx); 2433208019Sthompsa 2434208019Sthompsa usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc); 2435203134Sthompsa} 2436203134Sthompsa 2437203134Sthompsa/* 2438203134Sthompsa * Return the Rx chain with the highest RSSI for a given frame. 2439203134Sthompsa */ 2440203134Sthompsastatic __inline uint8_t 2441203134Sthompsarun_maxrssi_chain(struct run_softc *sc, const struct rt2860_rxwi *rxwi) 2442203134Sthompsa{ 2443203134Sthompsa uint8_t rxchain = 0; 2444203134Sthompsa 2445203134Sthompsa if (sc->nrxchains > 1) { 2446203134Sthompsa if (rxwi->rssi[1] > rxwi->rssi[rxchain]) 2447203134Sthompsa rxchain = 1; 2448203134Sthompsa if (sc->nrxchains > 2) 2449203134Sthompsa if (rxwi->rssi[2] > rxwi->rssi[rxchain]) 2450203134Sthompsa rxchain = 2; 2451203134Sthompsa } 2452209917Sthompsa return (rxchain); 2453203134Sthompsa} 2454203134Sthompsa 2455203134Sthompsastatic void 2456203134Sthompsarun_rx_frame(struct run_softc *sc, struct mbuf *m, uint32_t dmalen) 2457203134Sthompsa{ 2458203134Sthompsa struct ifnet *ifp = sc->sc_ifp; 2459203134Sthompsa struct ieee80211com *ic = ifp->if_l2com; 2460203134Sthompsa struct ieee80211_frame *wh; 2461203134Sthompsa struct ieee80211_node *ni; 2462203134Sthompsa struct rt2870_rxd *rxd; 2463203134Sthompsa struct rt2860_rxwi *rxwi; 2464203134Sthompsa uint32_t flags; 2465203134Sthompsa uint16_t len, phy; 2466203134Sthompsa uint8_t ant, rssi; 2467203134Sthompsa int8_t nf; 2468203134Sthompsa 2469203134Sthompsa rxwi = mtod(m, struct rt2860_rxwi *); 2470203134Sthompsa len = le16toh(rxwi->len) & 0xfff; 2471203134Sthompsa if (__predict_false(len > dmalen)) { 2472203134Sthompsa m_freem(m); 2473203134Sthompsa ifp->if_ierrors++; 2474203134Sthompsa DPRINTF("bad RXWI length %u > %u\n", len, dmalen); 2475203134Sthompsa return; 2476203134Sthompsa } 2477203134Sthompsa /* Rx descriptor is located at the end */ 2478203134Sthompsa rxd = (struct rt2870_rxd *)(mtod(m, caddr_t) + dmalen); 2479203134Sthompsa flags = le32toh(rxd->flags); 2480203134Sthompsa 2481203134Sthompsa if (__predict_false(flags & (RT2860_RX_CRCERR | RT2860_RX_ICVERR))) { 2482203134Sthompsa m_freem(m); 2483203134Sthompsa ifp->if_ierrors++; 2484203134Sthompsa DPRINTF("%s error.\n", (flags & RT2860_RX_CRCERR)?"CRC":"ICV"); 2485203134Sthompsa return; 2486203134Sthompsa } 2487203134Sthompsa 2488203134Sthompsa m->m_data += sizeof(struct rt2860_rxwi); 2489203134Sthompsa m->m_pkthdr.len = m->m_len -= sizeof(struct rt2860_rxwi); 2490203134Sthompsa 2491203134Sthompsa wh = mtod(m, struct ieee80211_frame *); 2492203134Sthompsa 2493209917Sthompsa if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 2494203134Sthompsa wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 2495203134Sthompsa m->m_flags |= M_WEP; 2496203134Sthompsa } 2497203134Sthompsa 2498209917Sthompsa if (flags & RT2860_RX_L2PAD) { 2499203134Sthompsa DPRINTFN(8, "received RT2860_RX_L2PAD frame\n"); 2500203134Sthompsa len += 2; 2501203134Sthompsa } 2502203134Sthompsa 2503208019Sthompsa ni = ieee80211_find_rxnode(ic, 2504208019Sthompsa mtod(m, struct ieee80211_frame_min *)); 2505208019Sthompsa 2506203134Sthompsa if (__predict_false(flags & RT2860_RX_MICERR)) { 2507203134Sthompsa /* report MIC failures to net80211 for TKIP */ 2508209917Sthompsa if (ni != NULL) 2509208019Sthompsa ieee80211_notify_michael_failure(ni->ni_vap, wh, rxwi->keyidx); 2510203134Sthompsa m_freem(m); 2511203134Sthompsa ifp->if_ierrors++; 2512203134Sthompsa DPRINTF("MIC error. Someone is lying.\n"); 2513203134Sthompsa return; 2514203134Sthompsa } 2515203134Sthompsa 2516203134Sthompsa ant = run_maxrssi_chain(sc, rxwi); 2517203134Sthompsa rssi = rxwi->rssi[ant]; 2518203134Sthompsa nf = run_rssi2dbm(sc, rssi, ant); 2519203134Sthompsa 2520203134Sthompsa m->m_pkthdr.rcvif = ifp; 2521203134Sthompsa m->m_pkthdr.len = m->m_len = len; 2522203134Sthompsa 2523203134Sthompsa if (ni != NULL) { 2524203134Sthompsa (void)ieee80211_input(ni, m, rssi, nf); 2525203134Sthompsa ieee80211_free_node(ni); 2526203134Sthompsa } else { 2527203134Sthompsa (void)ieee80211_input_all(ic, m, rssi, nf); 2528203134Sthompsa } 2529203134Sthompsa 2530209917Sthompsa if (__predict_false(ieee80211_radiotap_active(ic))) { 2531203134Sthompsa struct run_rx_radiotap_header *tap = &sc->sc_rxtap; 2532203134Sthompsa 2533203134Sthompsa tap->wr_flags = 0; 2534236439Shselasky tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq); 2535236439Shselasky tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags); 2536203134Sthompsa tap->wr_antsignal = rssi; 2537203134Sthompsa tap->wr_antenna = ant; 2538203134Sthompsa tap->wr_dbm_antsignal = run_rssi2dbm(sc, rssi, ant); 2539203134Sthompsa tap->wr_rate = 2; /* in case it can't be found below */ 2540203134Sthompsa phy = le16toh(rxwi->phy); 2541203134Sthompsa switch (phy & RT2860_PHY_MODE) { 2542203134Sthompsa case RT2860_PHY_CCK: 2543203134Sthompsa switch ((phy & RT2860_PHY_MCS) & ~RT2860_PHY_SHPRE) { 2544203134Sthompsa case 0: tap->wr_rate = 2; break; 2545203134Sthompsa case 1: tap->wr_rate = 4; break; 2546203134Sthompsa case 2: tap->wr_rate = 11; break; 2547203134Sthompsa case 3: tap->wr_rate = 22; break; 2548203134Sthompsa } 2549203134Sthompsa if (phy & RT2860_PHY_SHPRE) 2550203134Sthompsa tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 2551203134Sthompsa break; 2552203134Sthompsa case RT2860_PHY_OFDM: 2553203134Sthompsa switch (phy & RT2860_PHY_MCS) { 2554203134Sthompsa case 0: tap->wr_rate = 12; break; 2555203134Sthompsa case 1: tap->wr_rate = 18; break; 2556203134Sthompsa case 2: tap->wr_rate = 24; break; 2557203134Sthompsa case 3: tap->wr_rate = 36; break; 2558203134Sthompsa case 4: tap->wr_rate = 48; break; 2559203134Sthompsa case 5: tap->wr_rate = 72; break; 2560203134Sthompsa case 6: tap->wr_rate = 96; break; 2561203134Sthompsa case 7: tap->wr_rate = 108; break; 2562203134Sthompsa } 2563203134Sthompsa break; 2564203134Sthompsa } 2565203134Sthompsa } 2566203134Sthompsa} 2567203134Sthompsa 2568203134Sthompsastatic void 2569203134Sthompsarun_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error) 2570203134Sthompsa{ 2571203134Sthompsa struct run_softc *sc = usbd_xfer_softc(xfer); 2572203134Sthompsa struct ifnet *ifp = sc->sc_ifp; 2573203134Sthompsa struct mbuf *m = NULL; 2574203134Sthompsa struct mbuf *m0; 2575203134Sthompsa uint32_t dmalen; 2576203134Sthompsa int xferlen; 2577203134Sthompsa 2578203134Sthompsa usbd_xfer_status(xfer, &xferlen, NULL, NULL, NULL); 2579203134Sthompsa 2580203134Sthompsa switch (USB_GET_STATE(xfer)) { 2581203134Sthompsa case USB_ST_TRANSFERRED: 2582203134Sthompsa 2583203134Sthompsa DPRINTFN(15, "rx done, actlen=%d\n", xferlen); 2584203134Sthompsa 2585233774Shselasky if (xferlen < (int)(sizeof(uint32_t) + 2586233774Shselasky sizeof(struct rt2860_rxwi) + sizeof(struct rt2870_rxd))) { 2587203134Sthompsa DPRINTF("xfer too short %d\n", xferlen); 2588203134Sthompsa goto tr_setup; 2589203134Sthompsa } 2590203134Sthompsa 2591203134Sthompsa m = sc->rx_m; 2592203134Sthompsa sc->rx_m = NULL; 2593203134Sthompsa 2594203134Sthompsa /* FALLTHROUGH */ 2595203134Sthompsa case USB_ST_SETUP: 2596203134Sthompsatr_setup: 2597203134Sthompsa if (sc->rx_m == NULL) { 2598243857Sglebius sc->rx_m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, 2599203134Sthompsa MJUMPAGESIZE /* xfer can be bigger than MCLBYTES */); 2600203134Sthompsa } 2601203134Sthompsa if (sc->rx_m == NULL) { 2602203134Sthompsa DPRINTF("could not allocate mbuf - idle with stall\n"); 2603203134Sthompsa ifp->if_ierrors++; 2604203134Sthompsa usbd_xfer_set_stall(xfer); 2605203134Sthompsa usbd_xfer_set_frames(xfer, 0); 2606203134Sthompsa } else { 2607203134Sthompsa /* 2608203134Sthompsa * Directly loading a mbuf cluster into DMA to 2609203134Sthompsa * save some data copying. This works because 2610203134Sthompsa * there is only one cluster. 2611203134Sthompsa */ 2612203134Sthompsa usbd_xfer_set_frame_data(xfer, 0, 2613203134Sthompsa mtod(sc->rx_m, caddr_t), RUN_MAX_RXSZ); 2614203134Sthompsa usbd_xfer_set_frames(xfer, 1); 2615203134Sthompsa } 2616203134Sthompsa usbd_transfer_submit(xfer); 2617203134Sthompsa break; 2618203134Sthompsa 2619203134Sthompsa default: /* Error */ 2620203134Sthompsa if (error != USB_ERR_CANCELLED) { 2621203134Sthompsa /* try to clear stall first */ 2622203134Sthompsa usbd_xfer_set_stall(xfer); 2623203134Sthompsa 2624203134Sthompsa if (error == USB_ERR_TIMEOUT) 2625203134Sthompsa device_printf(sc->sc_dev, "device timeout\n"); 2626203134Sthompsa 2627203134Sthompsa ifp->if_ierrors++; 2628203134Sthompsa 2629203134Sthompsa goto tr_setup; 2630203134Sthompsa } 2631209917Sthompsa if (sc->rx_m != NULL) { 2632203134Sthompsa m_freem(sc->rx_m); 2633203134Sthompsa sc->rx_m = NULL; 2634203134Sthompsa } 2635203134Sthompsa break; 2636203134Sthompsa } 2637203134Sthompsa 2638203134Sthompsa if (m == NULL) 2639203134Sthompsa return; 2640203134Sthompsa 2641203134Sthompsa /* inputting all the frames must be last */ 2642203134Sthompsa 2643203134Sthompsa RUN_UNLOCK(sc); 2644203134Sthompsa 2645203134Sthompsa m->m_pkthdr.len = m->m_len = xferlen; 2646203134Sthompsa 2647203134Sthompsa /* HW can aggregate multiple 802.11 frames in a single USB xfer */ 2648203134Sthompsa for(;;) { 2649203134Sthompsa dmalen = le32toh(*mtod(m, uint32_t *)) & 0xffff; 2650203134Sthompsa 2651233774Shselasky if ((dmalen >= (uint32_t)-8) || (dmalen == 0) || 2652233774Shselasky ((dmalen & 3) != 0)) { 2653203134Sthompsa DPRINTF("bad DMA length %u\n", dmalen); 2654203134Sthompsa break; 2655203134Sthompsa } 2656233774Shselasky if ((dmalen + 8) > (uint32_t)xferlen) { 2657203134Sthompsa DPRINTF("bad DMA length %u > %d\n", 2658203134Sthompsa dmalen + 8, xferlen); 2659203134Sthompsa break; 2660203134Sthompsa } 2661203134Sthompsa 2662203134Sthompsa /* If it is the last one or a single frame, we won't copy. */ 2663209917Sthompsa if ((xferlen -= dmalen + 8) <= 8) { 2664203134Sthompsa /* trim 32-bit DMA-len header */ 2665203134Sthompsa m->m_data += 4; 2666203134Sthompsa m->m_pkthdr.len = m->m_len -= 4; 2667203134Sthompsa run_rx_frame(sc, m, dmalen); 2668203134Sthompsa break; 2669203134Sthompsa } 2670203134Sthompsa 2671203134Sthompsa /* copy aggregated frames to another mbuf */ 2672243857Sglebius m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 2673203134Sthompsa if (__predict_false(m0 == NULL)) { 2674203134Sthompsa DPRINTF("could not allocate mbuf\n"); 2675203134Sthompsa ifp->if_ierrors++; 2676203134Sthompsa break; 2677203134Sthompsa } 2678203134Sthompsa m_copydata(m, 4 /* skip 32-bit DMA-len header */, 2679203134Sthompsa dmalen + sizeof(struct rt2870_rxd), mtod(m0, caddr_t)); 2680203134Sthompsa m0->m_pkthdr.len = m0->m_len = 2681203134Sthompsa dmalen + sizeof(struct rt2870_rxd); 2682203134Sthompsa run_rx_frame(sc, m0, dmalen); 2683203134Sthompsa 2684203134Sthompsa /* update data ptr */ 2685203134Sthompsa m->m_data += dmalen + 8; 2686203134Sthompsa m->m_pkthdr.len = m->m_len -= dmalen + 8; 2687203134Sthompsa } 2688203134Sthompsa 2689203134Sthompsa RUN_LOCK(sc); 2690203134Sthompsa} 2691203134Sthompsa 2692203134Sthompsastatic void 2693203134Sthompsarun_tx_free(struct run_endpoint_queue *pq, 2694203134Sthompsa struct run_tx_data *data, int txerr) 2695203134Sthompsa{ 2696203134Sthompsa if (data->m != NULL) { 2697203134Sthompsa if (data->m->m_flags & M_TXCB) 2698203134Sthompsa ieee80211_process_callback(data->ni, data->m, 2699203134Sthompsa txerr ? ETIMEDOUT : 0); 2700203134Sthompsa m_freem(data->m); 2701203134Sthompsa data->m = NULL; 2702203134Sthompsa 2703209917Sthompsa if (data->ni == NULL) { 2704203134Sthompsa DPRINTF("no node\n"); 2705203134Sthompsa } else { 2706203134Sthompsa ieee80211_free_node(data->ni); 2707203134Sthompsa data->ni = NULL; 2708203134Sthompsa } 2709203134Sthompsa } 2710203134Sthompsa 2711203134Sthompsa STAILQ_INSERT_TAIL(&pq->tx_fh, data, next); 2712203134Sthompsa pq->tx_nfree++; 2713203134Sthompsa} 2714203134Sthompsa 2715203134Sthompsastatic void 2716203134Sthompsarun_bulk_tx_callbackN(struct usb_xfer *xfer, usb_error_t error, unsigned int index) 2717203134Sthompsa{ 2718203134Sthompsa struct run_softc *sc = usbd_xfer_softc(xfer); 2719203134Sthompsa struct ifnet *ifp = sc->sc_ifp; 2720208019Sthompsa struct ieee80211com *ic = ifp->if_l2com; 2721203134Sthompsa struct run_tx_data *data; 2722203134Sthompsa struct ieee80211vap *vap = NULL; 2723203134Sthompsa struct usb_page_cache *pc; 2724203134Sthompsa struct run_endpoint_queue *pq = &sc->sc_epq[index]; 2725203134Sthompsa struct mbuf *m; 2726203134Sthompsa usb_frlength_t size; 2727203134Sthompsa int actlen; 2728203134Sthompsa int sumlen; 2729203134Sthompsa 2730203134Sthompsa usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 2731203134Sthompsa 2732209917Sthompsa switch (USB_GET_STATE(xfer)) { 2733203134Sthompsa case USB_ST_TRANSFERRED: 2734203134Sthompsa DPRINTFN(11, "transfer complete: %d " 2735203134Sthompsa "bytes @ index %d\n", actlen, index); 2736203134Sthompsa 2737203134Sthompsa data = usbd_xfer_get_priv(xfer); 2738203134Sthompsa 2739203134Sthompsa run_tx_free(pq, data, 0); 2740203134Sthompsa ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2741203134Sthompsa 2742203134Sthompsa usbd_xfer_set_priv(xfer, NULL); 2743203134Sthompsa 2744203134Sthompsa ifp->if_opackets++; 2745203134Sthompsa 2746203134Sthompsa /* FALLTHROUGH */ 2747203134Sthompsa case USB_ST_SETUP: 2748203134Sthompsatr_setup: 2749203134Sthompsa data = STAILQ_FIRST(&pq->tx_qh); 2750209917Sthompsa if (data == NULL) 2751203134Sthompsa break; 2752203134Sthompsa 2753203134Sthompsa STAILQ_REMOVE_HEAD(&pq->tx_qh, next); 2754203134Sthompsa 2755203134Sthompsa m = data->m; 2756228508Shselasky if ((m->m_pkthdr.len + 2757228508Shselasky sizeof(data->desc) + 3 + 8) > RUN_MAX_TXSZ) { 2758203134Sthompsa DPRINTF("data overflow, %u bytes\n", 2759203134Sthompsa m->m_pkthdr.len); 2760203134Sthompsa 2761203134Sthompsa ifp->if_oerrors++; 2762203134Sthompsa 2763203134Sthompsa run_tx_free(pq, data, 1); 2764203134Sthompsa 2765203134Sthompsa goto tr_setup; 2766203134Sthompsa } 2767203134Sthompsa 2768203134Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 2769203134Sthompsa size = sizeof(data->desc); 2770203134Sthompsa usbd_copy_in(pc, 0, &data->desc, size); 2771203134Sthompsa usbd_m_copy_in(pc, size, m, 0, m->m_pkthdr.len); 2772228508Shselasky size += m->m_pkthdr.len; 2773228508Shselasky /* 2774228508Shselasky * Align end on a 4-byte boundary, pad 8 bytes (CRC + 2775228508Shselasky * 4-byte padding), and be sure to zero those trailing 2776228508Shselasky * bytes: 2777228508Shselasky */ 2778228508Shselasky usbd_frame_zero(pc, size, ((-size) & 3) + 8); 2779228508Shselasky size += ((-size) & 3) + 8; 2780203134Sthompsa 2781203134Sthompsa vap = data->ni->ni_vap; 2782203134Sthompsa if (ieee80211_radiotap_active_vap(vap)) { 2783203134Sthompsa struct run_tx_radiotap_header *tap = &sc->sc_txtap; 2784208019Sthompsa struct rt2860_txwi *txwi = 2785208019Sthompsa (struct rt2860_txwi *)(&data->desc + sizeof(struct rt2870_txd)); 2786203134Sthompsa 2787203134Sthompsa tap->wt_flags = 0; 2788203134Sthompsa tap->wt_rate = rt2860_rates[data->ridx].rate; 2789236439Shselasky tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 2790236439Shselasky tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 2791203134Sthompsa tap->wt_hwqueue = index; 2792208019Sthompsa if (le16toh(txwi->phy) & RT2860_PHY_SHPRE) 2793203134Sthompsa tap->wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 2794203134Sthompsa 2795203134Sthompsa ieee80211_radiotap_tx(vap, m); 2796203134Sthompsa } 2797203134Sthompsa 2798228508Shselasky DPRINTFN(11, "sending frame len=%u/%u @ index %d\n", 2799228508Shselasky m->m_pkthdr.len, size, index); 2800203134Sthompsa 2801228508Shselasky usbd_xfer_set_frame_len(xfer, 0, size); 2802203134Sthompsa usbd_xfer_set_priv(xfer, data); 2803203134Sthompsa 2804203134Sthompsa usbd_transfer_submit(xfer); 2805203134Sthompsa 2806203134Sthompsa RUN_UNLOCK(sc); 2807203134Sthompsa run_start(ifp); 2808203134Sthompsa RUN_LOCK(sc); 2809203134Sthompsa 2810203134Sthompsa break; 2811203134Sthompsa 2812203134Sthompsa default: 2813203134Sthompsa DPRINTF("USB transfer error, %s\n", 2814203134Sthompsa usbd_errstr(error)); 2815203134Sthompsa 2816203134Sthompsa data = usbd_xfer_get_priv(xfer); 2817203134Sthompsa 2818203134Sthompsa ifp->if_oerrors++; 2819203134Sthompsa 2820203134Sthompsa if (data != NULL) { 2821208019Sthompsa if(data->ni != NULL) 2822208019Sthompsa vap = data->ni->ni_vap; 2823203134Sthompsa run_tx_free(pq, data, error); 2824203134Sthompsa usbd_xfer_set_priv(xfer, NULL); 2825203134Sthompsa } 2826209917Sthompsa if (vap == NULL) 2827208019Sthompsa vap = TAILQ_FIRST(&ic->ic_vaps); 2828203134Sthompsa 2829203134Sthompsa if (error != USB_ERR_CANCELLED) { 2830203134Sthompsa if (error == USB_ERR_TIMEOUT) { 2831203134Sthompsa device_printf(sc->sc_dev, "device timeout\n"); 2832208019Sthompsa uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store); 2833208019Sthompsa DPRINTF("cmdq_store=%d\n", i); 2834208019Sthompsa sc->cmdq[i].func = run_usb_timeout_cb; 2835208019Sthompsa sc->cmdq[i].arg0 = vap; 2836208019Sthompsa ieee80211_runtask(ic, &sc->cmdq_task); 2837203134Sthompsa } 2838203134Sthompsa 2839203134Sthompsa /* 2840203134Sthompsa * Try to clear stall first, also if other 2841203134Sthompsa * errors occur, hence clearing stall 2842203134Sthompsa * introduces a 50 ms delay: 2843203134Sthompsa */ 2844203134Sthompsa usbd_xfer_set_stall(xfer); 2845203134Sthompsa goto tr_setup; 2846203134Sthompsa } 2847203134Sthompsa break; 2848203134Sthompsa } 2849203134Sthompsa} 2850203134Sthompsa 2851203134Sthompsastatic void 2852203134Sthompsarun_bulk_tx_callback0(struct usb_xfer *xfer, usb_error_t error) 2853203134Sthompsa{ 2854203134Sthompsa run_bulk_tx_callbackN(xfer, error, 0); 2855203134Sthompsa} 2856203134Sthompsa 2857203134Sthompsastatic void 2858203134Sthompsarun_bulk_tx_callback1(struct usb_xfer *xfer, usb_error_t error) 2859203134Sthompsa{ 2860203134Sthompsa run_bulk_tx_callbackN(xfer, error, 1); 2861203134Sthompsa} 2862203134Sthompsa 2863203134Sthompsastatic void 2864203134Sthompsarun_bulk_tx_callback2(struct usb_xfer *xfer, usb_error_t error) 2865203134Sthompsa{ 2866203134Sthompsa run_bulk_tx_callbackN(xfer, error, 2); 2867203134Sthompsa} 2868203134Sthompsa 2869203134Sthompsastatic void 2870203134Sthompsarun_bulk_tx_callback3(struct usb_xfer *xfer, usb_error_t error) 2871203134Sthompsa{ 2872203134Sthompsa run_bulk_tx_callbackN(xfer, error, 3); 2873203134Sthompsa} 2874203134Sthompsa 2875203134Sthompsastatic void 2876203134Sthompsarun_bulk_tx_callback4(struct usb_xfer *xfer, usb_error_t error) 2877203134Sthompsa{ 2878203134Sthompsa run_bulk_tx_callbackN(xfer, error, 4); 2879203134Sthompsa} 2880203134Sthompsa 2881203134Sthompsastatic void 2882203134Sthompsarun_bulk_tx_callback5(struct usb_xfer *xfer, usb_error_t error) 2883203134Sthompsa{ 2884203134Sthompsa run_bulk_tx_callbackN(xfer, error, 5); 2885203134Sthompsa} 2886203134Sthompsa 2887203134Sthompsastatic void 2888208019Sthompsarun_set_tx_desc(struct run_softc *sc, struct run_tx_data *data) 2889203134Sthompsa{ 2890203134Sthompsa struct mbuf *m = data->m; 2891203134Sthompsa struct ieee80211com *ic = sc->sc_ifp->if_l2com; 2892208019Sthompsa struct ieee80211vap *vap = data->ni->ni_vap; 2893203134Sthompsa struct ieee80211_frame *wh; 2894203134Sthompsa struct rt2870_txd *txd; 2895203134Sthompsa struct rt2860_txwi *txwi; 2896208019Sthompsa uint16_t xferlen; 2897208019Sthompsa uint16_t mcs; 2898203134Sthompsa uint8_t ridx = data->ridx; 2899208019Sthompsa uint8_t pad; 2900203134Sthompsa 2901203134Sthompsa /* get MCS code from rate index */ 2902208019Sthompsa mcs = rt2860_rates[ridx].mcs; 2903203134Sthompsa 2904203134Sthompsa xferlen = sizeof(*txwi) + m->m_pkthdr.len; 2905203134Sthompsa 2906203134Sthompsa /* roundup to 32-bit alignment */ 2907203134Sthompsa xferlen = (xferlen + 3) & ~3; 2908203134Sthompsa 2909203134Sthompsa txd = (struct rt2870_txd *)&data->desc; 2910203134Sthompsa txd->len = htole16(xferlen); 2911203134Sthompsa 2912208019Sthompsa wh = mtod(m, struct ieee80211_frame *); 2913208019Sthompsa 2914208019Sthompsa /* 2915208019Sthompsa * Ether both are true or both are false, the header 2916208019Sthompsa * are nicely aligned to 32-bit. So, no L2 padding. 2917208019Sthompsa */ 2918208019Sthompsa if(IEEE80211_HAS_ADDR4(wh) == IEEE80211_QOS_HAS_SEQ(wh)) 2919208019Sthompsa pad = 0; 2920208019Sthompsa else 2921208019Sthompsa pad = 2; 2922208019Sthompsa 2923203134Sthompsa /* setup TX Wireless Information */ 2924203134Sthompsa txwi = (struct rt2860_txwi *)(txd + 1); 2925203134Sthompsa txwi->len = htole16(m->m_pkthdr.len - pad); 2926203134Sthompsa if (rt2860_rates[ridx].phy == IEEE80211_T_DS) { 2927203134Sthompsa txwi->phy = htole16(RT2860_PHY_CCK); 2928203134Sthompsa if (ridx != RT2860_RIDX_CCK1 && 2929203134Sthompsa (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 2930203134Sthompsa mcs |= RT2860_PHY_SHPRE; 2931203134Sthompsa } else 2932203134Sthompsa txwi->phy = htole16(RT2860_PHY_OFDM); 2933203134Sthompsa txwi->phy |= htole16(mcs); 2934203134Sthompsa 2935203134Sthompsa /* check if RTS/CTS or CTS-to-self protection is required */ 2936203134Sthompsa if (!IEEE80211_IS_MULTICAST(wh->i_addr1) && 2937203134Sthompsa (m->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold || 2938203134Sthompsa ((ic->ic_flags & IEEE80211_F_USEPROT) && 2939203134Sthompsa rt2860_rates[ridx].phy == IEEE80211_T_OFDM))) 2940208019Sthompsa txwi->txop |= RT2860_TX_TXOP_HT; 2941203134Sthompsa else 2942208019Sthompsa txwi->txop |= RT2860_TX_TXOP_BACKOFF; 2943209144Sthompsa 2944209917Sthompsa if (vap->iv_opmode != IEEE80211_M_STA && !IEEE80211_QOS_HAS_SEQ(wh)) 2945209144Sthompsa txwi->xflags |= RT2860_TX_NSEQ; 2946203134Sthompsa} 2947203134Sthompsa 2948203134Sthompsa/* This function must be called locked */ 2949203134Sthompsastatic int 2950203134Sthompsarun_tx(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni) 2951203134Sthompsa{ 2952203134Sthompsa struct ieee80211com *ic = sc->sc_ifp->if_l2com; 2953208019Sthompsa struct ieee80211vap *vap = ni->ni_vap; 2954203134Sthompsa struct ieee80211_frame *wh; 2955208019Sthompsa struct ieee80211_channel *chan; 2956203134Sthompsa const struct ieee80211_txparam *tp; 2957208019Sthompsa struct run_node *rn = (void *)ni; 2958203134Sthompsa struct run_tx_data *data; 2959208019Sthompsa struct rt2870_txd *txd; 2960208019Sthompsa struct rt2860_txwi *txwi; 2961203134Sthompsa uint16_t qos; 2962203134Sthompsa uint16_t dur; 2963208019Sthompsa uint16_t qid; 2964203134Sthompsa uint8_t type; 2965203134Sthompsa uint8_t tid; 2966208019Sthompsa uint8_t ridx; 2967208019Sthompsa uint8_t ctl_ridx; 2968203134Sthompsa uint8_t qflags; 2969203134Sthompsa uint8_t xflags = 0; 2970203134Sthompsa int hasqos; 2971203134Sthompsa 2972203134Sthompsa RUN_LOCK_ASSERT(sc, MA_OWNED); 2973203134Sthompsa 2974203134Sthompsa wh = mtod(m, struct ieee80211_frame *); 2975203134Sthompsa 2976203134Sthompsa type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 2977203134Sthompsa 2978203134Sthompsa /* 2979203134Sthompsa * There are 7 bulk endpoints: 1 for RX 2980203134Sthompsa * and 6 for TX (4 EDCAs + HCCA + Prio). 2981203134Sthompsa * Update 03-14-2009: some devices like the Planex GW-US300MiniS 2982203134Sthompsa * seem to have only 4 TX bulk endpoints (Fukaumi Naoki). 2983203134Sthompsa */ 2984203134Sthompsa if ((hasqos = IEEE80211_QOS_HAS_SEQ(wh))) { 2985203134Sthompsa uint8_t *frm; 2986203134Sthompsa 2987203134Sthompsa if(IEEE80211_HAS_ADDR4(wh)) 2988203134Sthompsa frm = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos; 2989203134Sthompsa else 2990203134Sthompsa frm =((struct ieee80211_qosframe *)wh)->i_qos; 2991203134Sthompsa 2992203134Sthompsa qos = le16toh(*(const uint16_t *)frm); 2993203134Sthompsa tid = qos & IEEE80211_QOS_TID; 2994203134Sthompsa qid = TID_TO_WME_AC(tid); 2995203134Sthompsa } else { 2996203134Sthompsa qos = 0; 2997203134Sthompsa tid = 0; 2998203134Sthompsa qid = WME_AC_BE; 2999203134Sthompsa } 3000203134Sthompsa qflags = (qid < 4) ? RT2860_TX_QSEL_EDCA : RT2860_TX_QSEL_HCCA; 3001203134Sthompsa 3002203134Sthompsa DPRINTFN(8, "qos %d\tqid %d\ttid %d\tqflags %x\n", 3003203134Sthompsa qos, qid, tid, qflags); 3004203134Sthompsa 3005208019Sthompsa chan = (ni->ni_chan != IEEE80211_CHAN_ANYC)?ni->ni_chan:ic->ic_curchan; 3006208019Sthompsa tp = &vap->iv_txparms[ieee80211_chan2mode(chan)]; 3007203134Sthompsa 3008203134Sthompsa /* pickup a rate index */ 3009203134Sthompsa if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 3010203134Sthompsa type != IEEE80211_FC0_TYPE_DATA) { 3011203134Sthompsa ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ? 3012203134Sthompsa RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1; 3013203134Sthompsa ctl_ridx = rt2860_rates[ridx].ctl_ridx; 3014203134Sthompsa } else { 3015208019Sthompsa if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 3016208019Sthompsa ridx = rn->fix_ridx; 3017208019Sthompsa else 3018208019Sthompsa ridx = rn->amrr_ridx; 3019203134Sthompsa ctl_ridx = rt2860_rates[ridx].ctl_ridx; 3020203134Sthompsa } 3021203134Sthompsa 3022203134Sthompsa if (!IEEE80211_IS_MULTICAST(wh->i_addr1) && 3023203134Sthompsa (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) != 3024203134Sthompsa IEEE80211_QOS_ACKPOLICY_NOACK)) { 3025209144Sthompsa xflags |= RT2860_TX_ACK; 3026203134Sthompsa if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 3027208019Sthompsa dur = rt2860_rates[ctl_ridx].sp_ack_dur; 3028203134Sthompsa else 3029208019Sthompsa dur = rt2860_rates[ctl_ridx].lp_ack_dur; 3030205042Sthompsa *(uint16_t *)wh->i_dur = htole16(dur); 3031203134Sthompsa } 3032203134Sthompsa 3033203134Sthompsa /* reserve slots for mgmt packets, just in case */ 3034203134Sthompsa if (sc->sc_epq[qid].tx_nfree < 3) { 3035203134Sthompsa DPRINTFN(10, "tx ring %d is full\n", qid); 3036203134Sthompsa return (-1); 3037203134Sthompsa } 3038203134Sthompsa 3039203134Sthompsa data = STAILQ_FIRST(&sc->sc_epq[qid].tx_fh); 3040203134Sthompsa STAILQ_REMOVE_HEAD(&sc->sc_epq[qid].tx_fh, next); 3041203134Sthompsa sc->sc_epq[qid].tx_nfree--; 3042203134Sthompsa 3043208019Sthompsa txd = (struct rt2870_txd *)&data->desc; 3044208019Sthompsa txd->flags = qflags; 3045208019Sthompsa txwi = (struct rt2860_txwi *)(txd + 1); 3046208019Sthompsa txwi->xflags = xflags; 3047209144Sthompsa txwi->wcid = IEEE80211_IS_MULTICAST(wh->i_addr1) ? 3048209144Sthompsa 0 : RUN_AID2WCID(ni->ni_associd); 3049208019Sthompsa /* clear leftover garbage bits */ 3050208019Sthompsa txwi->flags = 0; 3051208019Sthompsa txwi->txop = 0; 3052208019Sthompsa 3053203134Sthompsa data->m = m; 3054203134Sthompsa data->ni = ni; 3055203134Sthompsa data->ridx = ridx; 3056203134Sthompsa 3057208019Sthompsa run_set_tx_desc(sc, data); 3058203134Sthompsa 3059208019Sthompsa /* 3060208019Sthompsa * The chip keeps track of 2 kind of Tx stats, 3061208019Sthompsa * * TX_STAT_FIFO, for per WCID stats, and 3062208019Sthompsa * * TX_STA_CNT0 for all-TX-in-one stats. 3063208019Sthompsa * 3064208019Sthompsa * To use FIFO stats, we need to store MCS into the driver-private 3065208019Sthompsa * PacketID field. So that, we can tell whose stats when we read them. 3066208019Sthompsa * We add 1 to the MCS because setting the PacketID field to 0 means 3067208019Sthompsa * that we don't want feedback in TX_STAT_FIFO. 3068208019Sthompsa * And, that's what we want for STA mode, since TX_STA_CNT0 does the job. 3069208019Sthompsa * 3070208019Sthompsa * FIFO stats doesn't count Tx with WCID 0xff, so we do this in run_tx(). 3071208019Sthompsa */ 3072209917Sthompsa if (sc->rvp_cnt > 1 || vap->iv_opmode == IEEE80211_M_HOSTAP || 3073209917Sthompsa vap->iv_opmode == IEEE80211_M_MBSS) { 3074208019Sthompsa uint16_t pid = (rt2860_rates[ridx].mcs + 1) & 0xf; 3075208019Sthompsa txwi->len |= htole16(pid << RT2860_TX_PID_SHIFT); 3076208019Sthompsa 3077208019Sthompsa /* 3078208019Sthompsa * Unlike PCI based devices, we don't get any interrupt from 3079208019Sthompsa * USB devices, so we simulate FIFO-is-full interrupt here. 3080208019Sthompsa * Ralink recomends to drain FIFO stats every 100 ms, but 16 slots 3081208019Sthompsa * quickly get fulled. To prevent overflow, increment a counter on 3082208019Sthompsa * every FIFO stat request, so we know how many slots are left. 3083208019Sthompsa * We do this only in HOSTAP or multiple vap mode since FIFO stats 3084208019Sthompsa * are used only in those modes. 3085208019Sthompsa * We just drain stats. AMRR gets updated every 1 sec by 3086208019Sthompsa * run_ratectl_cb() via callout. 3087208019Sthompsa * Call it early. Otherwise overflow. 3088208019Sthompsa */ 3089209917Sthompsa if (sc->fifo_cnt++ == 10) { 3090208019Sthompsa /* 3091208019Sthompsa * With multiple vaps or if_bridge, if_start() is called 3092208019Sthompsa * with a non-sleepable lock, tcpinp. So, need to defer. 3093208019Sthompsa */ 3094208019Sthompsa uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store); 3095208019Sthompsa DPRINTFN(6, "cmdq_store=%d\n", i); 3096208019Sthompsa sc->cmdq[i].func = run_drain_fifo; 3097208019Sthompsa sc->cmdq[i].arg0 = sc; 3098208019Sthompsa ieee80211_runtask(ic, &sc->cmdq_task); 3099208019Sthompsa } 3100208019Sthompsa } 3101208019Sthompsa 3102203134Sthompsa STAILQ_INSERT_TAIL(&sc->sc_epq[qid].tx_qh, data, next); 3103203134Sthompsa 3104203134Sthompsa usbd_transfer_start(sc->sc_xfer[qid]); 3105203134Sthompsa 3106203134Sthompsa DPRINTFN(8, "sending data frame len=%d rate=%d qid=%d\n", m->m_pkthdr.len + 3107203134Sthompsa (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)), 3108203134Sthompsa rt2860_rates[ridx].rate, qid); 3109203134Sthompsa 3110203134Sthompsa return (0); 3111203134Sthompsa} 3112203134Sthompsa 3113203134Sthompsastatic int 3114203134Sthompsarun_tx_mgt(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni) 3115203134Sthompsa{ 3116203134Sthompsa struct ifnet *ifp = sc->sc_ifp; 3117203134Sthompsa struct ieee80211com *ic = ifp->if_l2com; 3118208019Sthompsa struct run_node *rn = (void *)ni; 3119203134Sthompsa struct run_tx_data *data; 3120203134Sthompsa struct ieee80211_frame *wh; 3121208019Sthompsa struct rt2870_txd *txd; 3122208019Sthompsa struct rt2860_txwi *txwi; 3123203134Sthompsa uint16_t dur; 3124208019Sthompsa uint8_t ridx = rn->mgt_ridx; 3125203134Sthompsa uint8_t type; 3126203134Sthompsa uint8_t xflags = 0; 3127208019Sthompsa uint8_t wflags = 0; 3128203134Sthompsa 3129203134Sthompsa RUN_LOCK_ASSERT(sc, MA_OWNED); 3130203134Sthompsa 3131203134Sthompsa wh = mtod(m, struct ieee80211_frame *); 3132203134Sthompsa 3133203134Sthompsa type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 3134203134Sthompsa 3135208019Sthompsa /* tell hardware to add timestamp for probe responses */ 3136208019Sthompsa if ((wh->i_fc[0] & 3137208019Sthompsa (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 3138208019Sthompsa (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP)) 3139208019Sthompsa wflags |= RT2860_TX_TS; 3140208019Sthompsa else if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 3141203134Sthompsa xflags |= RT2860_TX_ACK; 3142203134Sthompsa 3143208019Sthompsa dur = ieee80211_ack_duration(ic->ic_rt, rt2860_rates[ridx].rate, 3144203134Sthompsa ic->ic_flags & IEEE80211_F_SHPREAMBLE); 3145203134Sthompsa *(uint16_t *)wh->i_dur = htole16(dur); 3146203134Sthompsa } 3147203134Sthompsa 3148203134Sthompsa if (sc->sc_epq[0].tx_nfree == 0) { 3149203134Sthompsa /* let caller free mbuf */ 3150203134Sthompsa ifp->if_drv_flags |= IFF_DRV_OACTIVE; 3151203134Sthompsa return (EIO); 3152203134Sthompsa } 3153203134Sthompsa data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh); 3154203134Sthompsa STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next); 3155203134Sthompsa sc->sc_epq[0].tx_nfree--; 3156203134Sthompsa 3157208019Sthompsa txd = (struct rt2870_txd *)&data->desc; 3158208019Sthompsa txd->flags = RT2860_TX_QSEL_EDCA; 3159208019Sthompsa txwi = (struct rt2860_txwi *)(txd + 1); 3160208019Sthompsa txwi->wcid = 0xff; 3161208019Sthompsa txwi->flags = wflags; 3162208019Sthompsa txwi->xflags = xflags; 3163208019Sthompsa txwi->txop = 0; /* clear leftover garbage bits */ 3164208019Sthompsa 3165203134Sthompsa data->m = m; 3166203134Sthompsa data->ni = ni; 3167203134Sthompsa data->ridx = ridx; 3168203134Sthompsa 3169208019Sthompsa run_set_tx_desc(sc, data); 3170203134Sthompsa 3171203134Sthompsa DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", m->m_pkthdr.len + 3172203134Sthompsa (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)), 3173208019Sthompsa rt2860_rates[ridx].rate); 3174203134Sthompsa 3175203134Sthompsa STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next); 3176203134Sthompsa 3177203134Sthompsa usbd_transfer_start(sc->sc_xfer[0]); 3178203134Sthompsa 3179203134Sthompsa return (0); 3180203134Sthompsa} 3181203134Sthompsa 3182203134Sthompsastatic int 3183203134Sthompsarun_sendprot(struct run_softc *sc, 3184203134Sthompsa const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate) 3185203134Sthompsa{ 3186203134Sthompsa struct ieee80211com *ic = ni->ni_ic; 3187203134Sthompsa struct ieee80211_frame *wh; 3188203134Sthompsa struct run_tx_data *data; 3189208019Sthompsa struct rt2870_txd *txd; 3190208019Sthompsa struct rt2860_txwi *txwi; 3191203134Sthompsa struct mbuf *mprot; 3192203134Sthompsa int ridx; 3193203134Sthompsa int protrate; 3194203134Sthompsa int ackrate; 3195203134Sthompsa int pktlen; 3196203134Sthompsa int isshort; 3197203134Sthompsa uint16_t dur; 3198203134Sthompsa uint8_t type; 3199208019Sthompsa uint8_t wflags = 0; 3200208019Sthompsa uint8_t xflags = 0; 3201203134Sthompsa 3202203134Sthompsa RUN_LOCK_ASSERT(sc, MA_OWNED); 3203203134Sthompsa 3204203134Sthompsa KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY, 3205203134Sthompsa ("protection %d", prot)); 3206203134Sthompsa 3207203134Sthompsa wh = mtod(m, struct ieee80211_frame *); 3208203134Sthompsa pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 3209203134Sthompsa type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 3210203134Sthompsa 3211203134Sthompsa protrate = ieee80211_ctl_rate(ic->ic_rt, rate); 3212203134Sthompsa ackrate = ieee80211_ack_rate(ic->ic_rt, rate); 3213203134Sthompsa 3214203134Sthompsa isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0; 3215209189Sjkim dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort) 3216203134Sthompsa + ieee80211_ack_duration(ic->ic_rt, rate, isshort); 3217203134Sthompsa wflags = RT2860_TX_FRAG; 3218203134Sthompsa 3219203134Sthompsa /* check that there are free slots before allocating the mbuf */ 3220203134Sthompsa if (sc->sc_epq[0].tx_nfree == 0) { 3221203134Sthompsa /* let caller free mbuf */ 3222203134Sthompsa sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE; 3223203134Sthompsa return (ENOBUFS); 3224203134Sthompsa } 3225203134Sthompsa 3226203134Sthompsa if (prot == IEEE80211_PROT_RTSCTS) { 3227203134Sthompsa /* NB: CTS is the same size as an ACK */ 3228203134Sthompsa dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort); 3229208019Sthompsa xflags |= RT2860_TX_ACK; 3230203134Sthompsa mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur); 3231203134Sthompsa } else { 3232203134Sthompsa mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur); 3233203134Sthompsa } 3234203134Sthompsa if (mprot == NULL) { 3235203134Sthompsa sc->sc_ifp->if_oerrors++; 3236203134Sthompsa DPRINTF("could not allocate mbuf\n"); 3237203134Sthompsa return (ENOBUFS); 3238203134Sthompsa } 3239203134Sthompsa 3240203134Sthompsa data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh); 3241203134Sthompsa STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next); 3242203134Sthompsa sc->sc_epq[0].tx_nfree--; 3243203134Sthompsa 3244208019Sthompsa txd = (struct rt2870_txd *)&data->desc; 3245208019Sthompsa txd->flags = RT2860_TX_QSEL_EDCA; 3246208019Sthompsa txwi = (struct rt2860_txwi *)(txd + 1); 3247208019Sthompsa txwi->wcid = 0xff; 3248208019Sthompsa txwi->flags = wflags; 3249208019Sthompsa txwi->xflags = xflags; 3250208019Sthompsa txwi->txop = 0; /* clear leftover garbage bits */ 3251208019Sthompsa 3252203134Sthompsa data->m = mprot; 3253203134Sthompsa data->ni = ieee80211_ref_node(ni); 3254203134Sthompsa 3255203134Sthompsa for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++) 3256203134Sthompsa if (rt2860_rates[ridx].rate == protrate) 3257203134Sthompsa break; 3258203134Sthompsa data->ridx = ridx; 3259203134Sthompsa 3260208019Sthompsa run_set_tx_desc(sc, data); 3261203134Sthompsa 3262203134Sthompsa DPRINTFN(1, "sending prot len=%u rate=%u\n", 3263203134Sthompsa m->m_pkthdr.len, rate); 3264203134Sthompsa 3265203134Sthompsa STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next); 3266203134Sthompsa 3267203134Sthompsa usbd_transfer_start(sc->sc_xfer[0]); 3268203134Sthompsa 3269203134Sthompsa return (0); 3270203134Sthompsa} 3271203134Sthompsa 3272203134Sthompsastatic int 3273203134Sthompsarun_tx_param(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni, 3274203134Sthompsa const struct ieee80211_bpf_params *params) 3275203134Sthompsa{ 3276203134Sthompsa struct ieee80211com *ic = ni->ni_ic; 3277203134Sthompsa struct ieee80211_frame *wh; 3278203134Sthompsa struct run_tx_data *data; 3279208019Sthompsa struct rt2870_txd *txd; 3280208019Sthompsa struct rt2860_txwi *txwi; 3281203134Sthompsa uint8_t type; 3282208019Sthompsa uint8_t ridx; 3283208019Sthompsa uint8_t rate; 3284208019Sthompsa uint8_t opflags = 0; 3285208019Sthompsa uint8_t xflags = 0; 3286203134Sthompsa int error; 3287203134Sthompsa 3288203134Sthompsa RUN_LOCK_ASSERT(sc, MA_OWNED); 3289203134Sthompsa 3290203134Sthompsa KASSERT(params != NULL, ("no raw xmit params")); 3291203134Sthompsa 3292203134Sthompsa wh = mtod(m, struct ieee80211_frame *); 3293203134Sthompsa type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 3294203134Sthompsa 3295203134Sthompsa rate = params->ibp_rate0; 3296203134Sthompsa if (!ieee80211_isratevalid(ic->ic_rt, rate)) { 3297203134Sthompsa /* let caller free mbuf */ 3298203134Sthompsa return (EINVAL); 3299203134Sthompsa } 3300203134Sthompsa 3301203134Sthompsa if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 3302208019Sthompsa xflags |= RT2860_TX_ACK; 3303203134Sthompsa if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) { 3304203134Sthompsa error = run_sendprot(sc, m, ni, 3305203134Sthompsa params->ibp_flags & IEEE80211_BPF_RTS ? 3306203134Sthompsa IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY, 3307203134Sthompsa rate); 3308203134Sthompsa if (error) { 3309203134Sthompsa /* let caller free mbuf */ 3310209917Sthompsa return error; 3311203134Sthompsa } 3312203134Sthompsa opflags |= /*XXX RT2573_TX_LONG_RETRY |*/ RT2860_TX_TXOP_SIFS; 3313203134Sthompsa } 3314203134Sthompsa 3315203134Sthompsa if (sc->sc_epq[0].tx_nfree == 0) { 3316203134Sthompsa /* let caller free mbuf */ 3317203134Sthompsa sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE; 3318203134Sthompsa DPRINTF("sending raw frame, but tx ring is full\n"); 3319203134Sthompsa return (EIO); 3320203134Sthompsa } 3321203134Sthompsa data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh); 3322203134Sthompsa STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next); 3323203134Sthompsa sc->sc_epq[0].tx_nfree--; 3324203134Sthompsa 3325208019Sthompsa txd = (struct rt2870_txd *)&data->desc; 3326208019Sthompsa txd->flags = RT2860_TX_QSEL_EDCA; 3327208019Sthompsa txwi = (struct rt2860_txwi *)(txd + 1); 3328208019Sthompsa txwi->wcid = 0xff; 3329208019Sthompsa txwi->xflags = xflags; 3330208019Sthompsa txwi->txop = opflags; 3331208019Sthompsa txwi->flags = 0; /* clear leftover garbage bits */ 3332208019Sthompsa 3333203134Sthompsa data->m = m; 3334203134Sthompsa data->ni = ni; 3335203134Sthompsa for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++) 3336203134Sthompsa if (rt2860_rates[ridx].rate == rate) 3337203134Sthompsa break; 3338203134Sthompsa data->ridx = ridx; 3339203134Sthompsa 3340208019Sthompsa run_set_tx_desc(sc, data); 3341203134Sthompsa 3342203134Sthompsa DPRINTFN(10, "sending raw frame len=%u rate=%u\n", 3343203134Sthompsa m->m_pkthdr.len, rate); 3344203134Sthompsa 3345203134Sthompsa STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next); 3346203134Sthompsa 3347203134Sthompsa usbd_transfer_start(sc->sc_xfer[0]); 3348203134Sthompsa 3349209917Sthompsa return (0); 3350203134Sthompsa} 3351203134Sthompsa 3352203134Sthompsastatic int 3353203134Sthompsarun_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 3354203134Sthompsa const struct ieee80211_bpf_params *params) 3355203134Sthompsa{ 3356203134Sthompsa struct ifnet *ifp = ni->ni_ic->ic_ifp; 3357203134Sthompsa struct run_softc *sc = ifp->if_softc; 3358208019Sthompsa int error = 0; 3359208019Sthompsa 3360203134Sthompsa RUN_LOCK(sc); 3361203134Sthompsa 3362203134Sthompsa /* prevent management frames from being sent if we're not ready */ 3363203134Sthompsa if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 3364203134Sthompsa error = ENETDOWN; 3365208019Sthompsa goto done; 3366203134Sthompsa } 3367203134Sthompsa 3368203134Sthompsa if (params == NULL) { 3369203134Sthompsa /* tx mgt packet */ 3370209917Sthompsa if ((error = run_tx_mgt(sc, m, ni)) != 0) { 3371203134Sthompsa ifp->if_oerrors++; 3372203134Sthompsa DPRINTF("mgt tx failed\n"); 3373208019Sthompsa goto done; 3374203134Sthompsa } 3375203134Sthompsa } else { 3376203134Sthompsa /* tx raw packet with param */ 3377209917Sthompsa if ((error = run_tx_param(sc, m, ni, params)) != 0) { 3378203134Sthompsa ifp->if_oerrors++; 3379203134Sthompsa DPRINTF("tx with param failed\n"); 3380208019Sthompsa goto done; 3381203134Sthompsa } 3382203134Sthompsa } 3383203134Sthompsa 3384203134Sthompsa ifp->if_opackets++; 3385203134Sthompsa 3386208019Sthompsadone: 3387203134Sthompsa RUN_UNLOCK(sc); 3388203134Sthompsa 3389209917Sthompsa if (error != 0) { 3390208019Sthompsa if(m != NULL) 3391208019Sthompsa m_freem(m); 3392208019Sthompsa ieee80211_free_node(ni); 3393208019Sthompsa } 3394203134Sthompsa 3395203134Sthompsa return (error); 3396203134Sthompsa} 3397203134Sthompsa 3398203134Sthompsastatic void 3399203134Sthompsarun_start(struct ifnet *ifp) 3400203134Sthompsa{ 3401203134Sthompsa struct run_softc *sc = ifp->if_softc; 3402203134Sthompsa struct ieee80211_node *ni; 3403203134Sthompsa struct mbuf *m; 3404203134Sthompsa 3405203134Sthompsa RUN_LOCK(sc); 3406203134Sthompsa 3407203134Sthompsa if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 3408203134Sthompsa RUN_UNLOCK(sc); 3409203134Sthompsa return; 3410203134Sthompsa } 3411203134Sthompsa 3412203134Sthompsa for (;;) { 3413203134Sthompsa /* send data frames */ 3414203134Sthompsa IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 3415203134Sthompsa if (m == NULL) 3416203134Sthompsa break; 3417203134Sthompsa 3418203134Sthompsa ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 3419203134Sthompsa if (run_tx(sc, m, ni) != 0) { 3420203134Sthompsa IFQ_DRV_PREPEND(&ifp->if_snd, m); 3421203134Sthompsa ifp->if_drv_flags |= IFF_DRV_OACTIVE; 3422203134Sthompsa break; 3423203134Sthompsa } 3424203134Sthompsa } 3425203134Sthompsa 3426203134Sthompsa RUN_UNLOCK(sc); 3427203134Sthompsa} 3428203134Sthompsa 3429203134Sthompsastatic int 3430203134Sthompsarun_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 3431203134Sthompsa{ 3432203134Sthompsa struct run_softc *sc = ifp->if_softc; 3433203134Sthompsa struct ieee80211com *ic = sc->sc_ifp->if_l2com; 3434203134Sthompsa struct ifreq *ifr = (struct ifreq *) data; 3435208019Sthompsa int startall = 0; 3436208019Sthompsa int error = 0; 3437203134Sthompsa 3438203134Sthompsa switch (cmd) { 3439203134Sthompsa case SIOCSIFFLAGS: 3440203134Sthompsa RUN_LOCK(sc); 3441203134Sthompsa if (ifp->if_flags & IFF_UP) { 3442203134Sthompsa if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)){ 3443208019Sthompsa startall = 1; 3444203134Sthompsa run_init_locked(sc); 3445203134Sthompsa } else 3446203134Sthompsa run_update_promisc_locked(ifp); 3447203134Sthompsa } else { 3448209917Sthompsa if (ifp->if_drv_flags & IFF_DRV_RUNNING && 3449209917Sthompsa (ic->ic_nrunning == 0 || sc->rvp_cnt <= 1)) { 3450208019Sthompsa run_stop(sc); 3451208019Sthompsa } 3452203134Sthompsa } 3453203134Sthompsa RUN_UNLOCK(sc); 3454209917Sthompsa if (startall) 3455208019Sthompsa ieee80211_start_all(ic); 3456203134Sthompsa break; 3457203134Sthompsa case SIOCGIFMEDIA: 3458203134Sthompsa error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 3459203134Sthompsa break; 3460203134Sthompsa case SIOCGIFADDR: 3461203134Sthompsa error = ether_ioctl(ifp, cmd, data); 3462203134Sthompsa break; 3463203134Sthompsa default: 3464203134Sthompsa error = EINVAL; 3465203134Sthompsa break; 3466203134Sthompsa } 3467203134Sthompsa 3468203134Sthompsa return (error); 3469203134Sthompsa} 3470203134Sthompsa 3471203134Sthompsastatic void 3472205042Sthompsarun_set_agc(struct run_softc *sc, uint8_t agc) 3473205042Sthompsa{ 3474205042Sthompsa uint8_t bbp; 3475205042Sthompsa 3476205042Sthompsa if (sc->mac_ver == 0x3572) { 3477205042Sthompsa run_bbp_read(sc, 27, &bbp); 3478205042Sthompsa bbp &= ~(0x3 << 5); 3479205042Sthompsa run_bbp_write(sc, 27, bbp | 0 << 5); /* select Rx0 */ 3480205042Sthompsa run_bbp_write(sc, 66, agc); 3481205042Sthompsa run_bbp_write(sc, 27, bbp | 1 << 5); /* select Rx1 */ 3482205042Sthompsa run_bbp_write(sc, 66, agc); 3483205042Sthompsa } else 3484205042Sthompsa run_bbp_write(sc, 66, agc); 3485205042Sthompsa} 3486205042Sthompsa 3487205042Sthompsastatic void 3488203134Sthompsarun_select_chan_group(struct run_softc *sc, int group) 3489203134Sthompsa{ 3490203134Sthompsa uint32_t tmp; 3491205042Sthompsa uint8_t agc; 3492203134Sthompsa 3493203134Sthompsa run_bbp_write(sc, 62, 0x37 - sc->lna[group]); 3494203134Sthompsa run_bbp_write(sc, 63, 0x37 - sc->lna[group]); 3495203134Sthompsa run_bbp_write(sc, 64, 0x37 - sc->lna[group]); 3496203134Sthompsa run_bbp_write(sc, 86, 0x00); 3497203134Sthompsa 3498203134Sthompsa if (group == 0) { 3499203134Sthompsa if (sc->ext_2ghz_lna) { 3500203134Sthompsa run_bbp_write(sc, 82, 0x62); 3501203134Sthompsa run_bbp_write(sc, 75, 0x46); 3502203134Sthompsa } else { 3503203134Sthompsa run_bbp_write(sc, 82, 0x84); 3504203134Sthompsa run_bbp_write(sc, 75, 0x50); 3505203134Sthompsa } 3506203134Sthompsa } else { 3507205042Sthompsa if (sc->mac_ver == 0x3572) 3508205042Sthompsa run_bbp_write(sc, 82, 0x94); 3509205042Sthompsa else 3510203134Sthompsa run_bbp_write(sc, 82, 0xf2); 3511205042Sthompsa if (sc->ext_5ghz_lna) 3512203134Sthompsa run_bbp_write(sc, 75, 0x46); 3513205042Sthompsa else 3514203134Sthompsa run_bbp_write(sc, 75, 0x50); 3515203134Sthompsa } 3516203134Sthompsa 3517203134Sthompsa run_read(sc, RT2860_TX_BAND_CFG, &tmp); 3518203134Sthompsa tmp &= ~(RT2860_5G_BAND_SEL_N | RT2860_5G_BAND_SEL_P); 3519203134Sthompsa tmp |= (group == 0) ? RT2860_5G_BAND_SEL_N : RT2860_5G_BAND_SEL_P; 3520203134Sthompsa run_write(sc, RT2860_TX_BAND_CFG, tmp); 3521203134Sthompsa 3522203134Sthompsa /* enable appropriate Power Amplifiers and Low Noise Amplifiers */ 3523208019Sthompsa tmp = RT2860_RFTR_EN | RT2860_TRSW_EN | RT2860_LNA_PE0_EN; 3524208019Sthompsa if (sc->nrxchains > 1) 3525208019Sthompsa tmp |= RT2860_LNA_PE1_EN; 3526203134Sthompsa if (group == 0) { /* 2GHz */ 3527208019Sthompsa tmp |= RT2860_PA_PE_G0_EN; 3528203134Sthompsa if (sc->ntxchains > 1) 3529203134Sthompsa tmp |= RT2860_PA_PE_G1_EN; 3530203134Sthompsa } else { /* 5GHz */ 3531208019Sthompsa tmp |= RT2860_PA_PE_A0_EN; 3532203134Sthompsa if (sc->ntxchains > 1) 3533203134Sthompsa tmp |= RT2860_PA_PE_A1_EN; 3534203134Sthompsa } 3535205042Sthompsa if (sc->mac_ver == 0x3572) { 3536205042Sthompsa run_rt3070_rf_write(sc, 8, 0x00); 3537205042Sthompsa run_write(sc, RT2860_TX_PIN_CFG, tmp); 3538205042Sthompsa run_rt3070_rf_write(sc, 8, 0x80); 3539205042Sthompsa } else 3540205042Sthompsa run_write(sc, RT2860_TX_PIN_CFG, tmp); 3541203134Sthompsa 3542203134Sthompsa /* set initial AGC value */ 3543205042Sthompsa if (group == 0) { /* 2GHz band */ 3544205042Sthompsa if (sc->mac_ver >= 0x3070) 3545205042Sthompsa agc = 0x1c + sc->lna[0] * 2; 3546205042Sthompsa else 3547205042Sthompsa agc = 0x2e + sc->lna[0]; 3548205042Sthompsa } else { /* 5GHz band */ 3549205042Sthompsa if (sc->mac_ver == 0x3572) 3550205042Sthompsa agc = 0x22 + (sc->lna[group] * 5) / 3; 3551205042Sthompsa else 3552205042Sthompsa agc = 0x32 + (sc->lna[group] * 5) / 3; 3553205042Sthompsa } 3554205042Sthompsa run_set_agc(sc, agc); 3555203134Sthompsa} 3556203134Sthompsa 3557203134Sthompsastatic void 3558203134Sthompsarun_rt2870_set_chan(struct run_softc *sc, uint32_t chan) 3559203134Sthompsa{ 3560203134Sthompsa const struct rfprog *rfprog = rt2860_rf2850; 3561203134Sthompsa uint32_t r2, r3, r4; 3562203134Sthompsa int8_t txpow1, txpow2; 3563203134Sthompsa int i; 3564203134Sthompsa 3565203134Sthompsa /* find the settings for this channel (we know it exists) */ 3566203134Sthompsa for (i = 0; rfprog[i].chan != chan; i++); 3567203134Sthompsa 3568203134Sthompsa r2 = rfprog[i].r2; 3569203134Sthompsa if (sc->ntxchains == 1) 3570203134Sthompsa r2 |= 1 << 12; /* 1T: disable Tx chain 2 */ 3571203134Sthompsa if (sc->nrxchains == 1) 3572203134Sthompsa r2 |= 1 << 15 | 1 << 4; /* 1R: disable Rx chains 2 & 3 */ 3573203134Sthompsa else if (sc->nrxchains == 2) 3574203134Sthompsa r2 |= 1 << 4; /* 2R: disable Rx chain 3 */ 3575203134Sthompsa 3576203134Sthompsa /* use Tx power values from EEPROM */ 3577203134Sthompsa txpow1 = sc->txpow1[i]; 3578203134Sthompsa txpow2 = sc->txpow2[i]; 3579203134Sthompsa if (chan > 14) { 3580203134Sthompsa if (txpow1 >= 0) 3581208019Sthompsa txpow1 = txpow1 << 1 | 1; 3582203134Sthompsa else 3583208019Sthompsa txpow1 = (7 + txpow1) << 1; 3584203134Sthompsa if (txpow2 >= 0) 3585208019Sthompsa txpow2 = txpow2 << 1 | 1; 3586203134Sthompsa else 3587208019Sthompsa txpow2 = (7 + txpow2) << 1; 3588203134Sthompsa } 3589203134Sthompsa r3 = rfprog[i].r3 | txpow1 << 7; 3590203134Sthompsa r4 = rfprog[i].r4 | sc->freq << 13 | txpow2 << 4; 3591203134Sthompsa 3592203134Sthompsa run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1); 3593203134Sthompsa run_rt2870_rf_write(sc, RT2860_RF2, r2); 3594203134Sthompsa run_rt2870_rf_write(sc, RT2860_RF3, r3); 3595203134Sthompsa run_rt2870_rf_write(sc, RT2860_RF4, r4); 3596203134Sthompsa 3597203134Sthompsa run_delay(sc, 10); 3598203134Sthompsa 3599203134Sthompsa run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1); 3600203134Sthompsa run_rt2870_rf_write(sc, RT2860_RF2, r2); 3601203134Sthompsa run_rt2870_rf_write(sc, RT2860_RF3, r3 | 1); 3602203134Sthompsa run_rt2870_rf_write(sc, RT2860_RF4, r4); 3603203134Sthompsa 3604203134Sthompsa run_delay(sc, 10); 3605203134Sthompsa 3606203134Sthompsa run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1); 3607203134Sthompsa run_rt2870_rf_write(sc, RT2860_RF2, r2); 3608203134Sthompsa run_rt2870_rf_write(sc, RT2860_RF3, r3); 3609203134Sthompsa run_rt2870_rf_write(sc, RT2860_RF4, r4); 3610203134Sthompsa} 3611203134Sthompsa 3612203134Sthompsastatic void 3613203134Sthompsarun_rt3070_set_chan(struct run_softc *sc, uint32_t chan) 3614203134Sthompsa{ 3615203134Sthompsa int8_t txpow1, txpow2; 3616203134Sthompsa uint8_t rf; 3617205042Sthompsa int i; 3618203134Sthompsa 3619203134Sthompsa /* RT3070 is 2GHz only */ 3620203134Sthompsa KASSERT(chan >= 1 && chan <= 14, ("wrong channel selected\n")); 3621203134Sthompsa 3622205042Sthompsa /* find the settings for this channel (we know it exists) */ 3623205042Sthompsa for (i = 0; rt2860_rf2850[i].chan != chan; i++); 3624205042Sthompsa 3625203134Sthompsa /* use Tx power values from EEPROM */ 3626205042Sthompsa txpow1 = sc->txpow1[i]; 3627205042Sthompsa txpow2 = sc->txpow2[i]; 3628203134Sthompsa 3629205042Sthompsa run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n); 3630205042Sthompsa run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k); 3631203134Sthompsa run_rt3070_rf_read(sc, 6, &rf); 3632205042Sthompsa rf = (rf & ~0x03) | rt3070_freqs[i].r; 3633203134Sthompsa run_rt3070_rf_write(sc, 6, rf); 3634203134Sthompsa 3635203134Sthompsa /* set Tx0 power */ 3636203134Sthompsa run_rt3070_rf_read(sc, 12, &rf); 3637203134Sthompsa rf = (rf & ~0x1f) | txpow1; 3638203134Sthompsa run_rt3070_rf_write(sc, 12, rf); 3639203134Sthompsa 3640203134Sthompsa /* set Tx1 power */ 3641203134Sthompsa run_rt3070_rf_read(sc, 13, &rf); 3642203134Sthompsa rf = (rf & ~0x1f) | txpow2; 3643203134Sthompsa run_rt3070_rf_write(sc, 13, rf); 3644203134Sthompsa 3645203134Sthompsa run_rt3070_rf_read(sc, 1, &rf); 3646203134Sthompsa rf &= ~0xfc; 3647203134Sthompsa if (sc->ntxchains == 1) 3648203134Sthompsa rf |= 1 << 7 | 1 << 5; /* 1T: disable Tx chains 2 & 3 */ 3649203134Sthompsa else if (sc->ntxchains == 2) 3650203134Sthompsa rf |= 1 << 7; /* 2T: disable Tx chain 3 */ 3651203134Sthompsa if (sc->nrxchains == 1) 3652203134Sthompsa rf |= 1 << 6 | 1 << 4; /* 1R: disable Rx chains 2 & 3 */ 3653203134Sthompsa else if (sc->nrxchains == 2) 3654203134Sthompsa rf |= 1 << 6; /* 2R: disable Rx chain 3 */ 3655203134Sthompsa run_rt3070_rf_write(sc, 1, rf); 3656203134Sthompsa 3657203134Sthompsa /* set RF offset */ 3658203134Sthompsa run_rt3070_rf_read(sc, 23, &rf); 3659203134Sthompsa rf = (rf & ~0x7f) | sc->freq; 3660203134Sthompsa run_rt3070_rf_write(sc, 23, rf); 3661203134Sthompsa 3662203134Sthompsa /* program RF filter */ 3663205042Sthompsa run_rt3070_rf_read(sc, 24, &rf); /* Tx */ 3664205042Sthompsa rf = (rf & ~0x3f) | sc->rf24_20mhz; 3665205042Sthompsa run_rt3070_rf_write(sc, 24, rf); 3666205042Sthompsa run_rt3070_rf_read(sc, 31, &rf); /* Rx */ 3667205042Sthompsa rf = (rf & ~0x3f) | sc->rf24_20mhz; 3668205042Sthompsa run_rt3070_rf_write(sc, 31, rf); 3669203134Sthompsa 3670203134Sthompsa /* enable RF tuning */ 3671203134Sthompsa run_rt3070_rf_read(sc, 7, &rf); 3672203134Sthompsa run_rt3070_rf_write(sc, 7, rf | 0x01); 3673203134Sthompsa} 3674203134Sthompsa 3675203134Sthompsastatic void 3676205042Sthompsarun_rt3572_set_chan(struct run_softc *sc, u_int chan) 3677205042Sthompsa{ 3678205042Sthompsa int8_t txpow1, txpow2; 3679205042Sthompsa uint32_t tmp; 3680205042Sthompsa uint8_t rf; 3681205042Sthompsa int i; 3682205042Sthompsa 3683205042Sthompsa /* find the settings for this channel (we know it exists) */ 3684205042Sthompsa for (i = 0; rt2860_rf2850[i].chan != chan; i++); 3685205042Sthompsa 3686205042Sthompsa /* use Tx power values from EEPROM */ 3687205042Sthompsa txpow1 = sc->txpow1[i]; 3688205042Sthompsa txpow2 = sc->txpow2[i]; 3689205042Sthompsa 3690205042Sthompsa if (chan <= 14) { 3691205042Sthompsa run_bbp_write(sc, 25, sc->bbp25); 3692205042Sthompsa run_bbp_write(sc, 26, sc->bbp26); 3693205042Sthompsa } else { 3694205042Sthompsa /* enable IQ phase correction */ 3695205042Sthompsa run_bbp_write(sc, 25, 0x09); 3696205042Sthompsa run_bbp_write(sc, 26, 0xff); 3697205042Sthompsa } 3698205042Sthompsa 3699205042Sthompsa run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n); 3700205042Sthompsa run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k); 3701205042Sthompsa run_rt3070_rf_read(sc, 6, &rf); 3702205042Sthompsa rf = (rf & ~0x0f) | rt3070_freqs[i].r; 3703205042Sthompsa rf |= (chan <= 14) ? 0x08 : 0x04; 3704205042Sthompsa run_rt3070_rf_write(sc, 6, rf); 3705205042Sthompsa 3706205042Sthompsa /* set PLL mode */ 3707205042Sthompsa run_rt3070_rf_read(sc, 5, &rf); 3708205042Sthompsa rf &= ~(0x08 | 0x04); 3709205042Sthompsa rf |= (chan <= 14) ? 0x04 : 0x08; 3710205042Sthompsa run_rt3070_rf_write(sc, 5, rf); 3711205042Sthompsa 3712205042Sthompsa /* set Tx power for chain 0 */ 3713205042Sthompsa if (chan <= 14) 3714205042Sthompsa rf = 0x60 | txpow1; 3715205042Sthompsa else 3716205042Sthompsa rf = 0xe0 | (txpow1 & 0xc) << 1 | (txpow1 & 0x3); 3717205042Sthompsa run_rt3070_rf_write(sc, 12, rf); 3718205042Sthompsa 3719205042Sthompsa /* set Tx power for chain 1 */ 3720205042Sthompsa if (chan <= 14) 3721205042Sthompsa rf = 0x60 | txpow2; 3722205042Sthompsa else 3723205042Sthompsa rf = 0xe0 | (txpow2 & 0xc) << 1 | (txpow2 & 0x3); 3724205042Sthompsa run_rt3070_rf_write(sc, 13, rf); 3725205042Sthompsa 3726205042Sthompsa /* set Tx/Rx streams */ 3727205042Sthompsa run_rt3070_rf_read(sc, 1, &rf); 3728205042Sthompsa rf &= ~0xfc; 3729205042Sthompsa if (sc->ntxchains == 1) 3730205042Sthompsa rf |= 1 << 7 | 1 << 5; /* 1T: disable Tx chains 2 & 3 */ 3731205042Sthompsa else if (sc->ntxchains == 2) 3732205042Sthompsa rf |= 1 << 7; /* 2T: disable Tx chain 3 */ 3733205042Sthompsa if (sc->nrxchains == 1) 3734205042Sthompsa rf |= 1 << 6 | 1 << 4; /* 1R: disable Rx chains 2 & 3 */ 3735205042Sthompsa else if (sc->nrxchains == 2) 3736205042Sthompsa rf |= 1 << 6; /* 2R: disable Rx chain 3 */ 3737205042Sthompsa run_rt3070_rf_write(sc, 1, rf); 3738205042Sthompsa 3739205042Sthompsa /* set RF offset */ 3740205042Sthompsa run_rt3070_rf_read(sc, 23, &rf); 3741205042Sthompsa rf = (rf & ~0x7f) | sc->freq; 3742205042Sthompsa run_rt3070_rf_write(sc, 23, rf); 3743205042Sthompsa 3744205042Sthompsa /* program RF filter */ 3745205042Sthompsa rf = sc->rf24_20mhz; 3746205042Sthompsa run_rt3070_rf_write(sc, 24, rf); /* Tx */ 3747205042Sthompsa run_rt3070_rf_write(sc, 31, rf); /* Rx */ 3748205042Sthompsa 3749205042Sthompsa /* enable RF tuning */ 3750205042Sthompsa run_rt3070_rf_read(sc, 7, &rf); 3751205042Sthompsa rf = (chan <= 14) ? 0xd8 : ((rf & ~0xc8) | 0x14); 3752205042Sthompsa run_rt3070_rf_write(sc, 7, rf); 3753205042Sthompsa 3754205042Sthompsa /* TSSI */ 3755205042Sthompsa rf = (chan <= 14) ? 0xc3 : 0xc0; 3756205042Sthompsa run_rt3070_rf_write(sc, 9, rf); 3757205042Sthompsa 3758205042Sthompsa /* set loop filter 1 */ 3759205042Sthompsa run_rt3070_rf_write(sc, 10, 0xf1); 3760205042Sthompsa /* set loop filter 2 */ 3761205042Sthompsa run_rt3070_rf_write(sc, 11, (chan <= 14) ? 0xb9 : 0x00); 3762205042Sthompsa 3763205042Sthompsa /* set tx_mx2_ic */ 3764205042Sthompsa run_rt3070_rf_write(sc, 15, (chan <= 14) ? 0x53 : 0x43); 3765205042Sthompsa /* set tx_mx1_ic */ 3766205042Sthompsa if (chan <= 14) 3767205042Sthompsa rf = 0x48 | sc->txmixgain_2ghz; 3768205042Sthompsa else 3769205042Sthompsa rf = 0x78 | sc->txmixgain_5ghz; 3770205042Sthompsa run_rt3070_rf_write(sc, 16, rf); 3771205042Sthompsa 3772205042Sthompsa /* set tx_lo1 */ 3773205042Sthompsa run_rt3070_rf_write(sc, 17, 0x23); 3774205042Sthompsa /* set tx_lo2 */ 3775205042Sthompsa if (chan <= 14) 3776205042Sthompsa rf = 0x93; 3777205042Sthompsa else if (chan <= 64) 3778205042Sthompsa rf = 0xb7; 3779205042Sthompsa else if (chan <= 128) 3780205042Sthompsa rf = 0x74; 3781205042Sthompsa else 3782205042Sthompsa rf = 0x72; 3783205042Sthompsa run_rt3070_rf_write(sc, 19, rf); 3784205042Sthompsa 3785205042Sthompsa /* set rx_lo1 */ 3786205042Sthompsa if (chan <= 14) 3787205042Sthompsa rf = 0xb3; 3788205042Sthompsa else if (chan <= 64) 3789205042Sthompsa rf = 0xf6; 3790205042Sthompsa else if (chan <= 128) 3791205042Sthompsa rf = 0xf4; 3792205042Sthompsa else 3793205042Sthompsa rf = 0xf3; 3794205042Sthompsa run_rt3070_rf_write(sc, 20, rf); 3795205042Sthompsa 3796205042Sthompsa /* set pfd_delay */ 3797205042Sthompsa if (chan <= 14) 3798205042Sthompsa rf = 0x15; 3799205042Sthompsa else if (chan <= 64) 3800205042Sthompsa rf = 0x3d; 3801205042Sthompsa else 3802205042Sthompsa rf = 0x01; 3803205042Sthompsa run_rt3070_rf_write(sc, 25, rf); 3804205042Sthompsa 3805205042Sthompsa /* set rx_lo2 */ 3806205042Sthompsa run_rt3070_rf_write(sc, 26, (chan <= 14) ? 0x85 : 0x87); 3807205042Sthompsa /* set ldo_rf_vc */ 3808205042Sthompsa run_rt3070_rf_write(sc, 27, (chan <= 14) ? 0x00 : 0x01); 3809205042Sthompsa /* set drv_cc */ 3810205042Sthompsa run_rt3070_rf_write(sc, 29, (chan <= 14) ? 0x9b : 0x9f); 3811205042Sthompsa 3812205042Sthompsa run_read(sc, RT2860_GPIO_CTRL, &tmp); 3813205042Sthompsa tmp &= ~0x8080; 3814205042Sthompsa if (chan <= 14) 3815205042Sthompsa tmp |= 0x80; 3816205042Sthompsa run_write(sc, RT2860_GPIO_CTRL, tmp); 3817205042Sthompsa 3818205042Sthompsa /* enable RF tuning */ 3819205042Sthompsa run_rt3070_rf_read(sc, 7, &rf); 3820205042Sthompsa run_rt3070_rf_write(sc, 7, rf | 0x01); 3821205042Sthompsa 3822205042Sthompsa run_delay(sc, 2); 3823205042Sthompsa} 3824205042Sthompsa 3825205042Sthompsastatic void 3826203134Sthompsarun_set_rx_antenna(struct run_softc *sc, int aux) 3827203134Sthompsa{ 3828203134Sthompsa uint32_t tmp; 3829203134Sthompsa 3830203134Sthompsa if (aux) { 3831205042Sthompsa run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 0); 3832203134Sthompsa run_read(sc, RT2860_GPIO_CTRL, &tmp); 3833203134Sthompsa run_write(sc, RT2860_GPIO_CTRL, (tmp & ~0x0808) | 0x08); 3834203134Sthompsa } else { 3835205042Sthompsa run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 1); 3836203134Sthompsa run_read(sc, RT2860_GPIO_CTRL, &tmp); 3837203134Sthompsa run_write(sc, RT2860_GPIO_CTRL, tmp & ~0x0808); 3838203134Sthompsa } 3839203134Sthompsa} 3840203134Sthompsa 3841203134Sthompsastatic int 3842203134Sthompsarun_set_chan(struct run_softc *sc, struct ieee80211_channel *c) 3843203134Sthompsa{ 3844203134Sthompsa struct ieee80211com *ic = sc->sc_ifp->if_l2com; 3845203134Sthompsa uint32_t chan, group; 3846203134Sthompsa 3847203134Sthompsa chan = ieee80211_chan2ieee(ic, c); 3848203134Sthompsa if (chan == 0 || chan == IEEE80211_CHAN_ANY) 3849209917Sthompsa return (EINVAL); 3850203134Sthompsa 3851205042Sthompsa if (sc->mac_ver == 0x3572) 3852205042Sthompsa run_rt3572_set_chan(sc, chan); 3853205042Sthompsa else if (sc->mac_ver >= 0x3070) 3854203134Sthompsa run_rt3070_set_chan(sc, chan); 3855203134Sthompsa else 3856203134Sthompsa run_rt2870_set_chan(sc, chan); 3857203134Sthompsa 3858203134Sthompsa /* determine channel group */ 3859203134Sthompsa if (chan <= 14) 3860203134Sthompsa group = 0; 3861203134Sthompsa else if (chan <= 64) 3862203134Sthompsa group = 1; 3863203134Sthompsa else if (chan <= 128) 3864203134Sthompsa group = 2; 3865203134Sthompsa else 3866203134Sthompsa group = 3; 3867203134Sthompsa 3868203134Sthompsa /* XXX necessary only when group has changed! */ 3869203134Sthompsa run_select_chan_group(sc, group); 3870203134Sthompsa 3871203134Sthompsa run_delay(sc, 10); 3872203134Sthompsa 3873209917Sthompsa return (0); 3874203134Sthompsa} 3875203134Sthompsa 3876203134Sthompsastatic void 3877203134Sthompsarun_set_channel(struct ieee80211com *ic) 3878203134Sthompsa{ 3879203134Sthompsa struct run_softc *sc = ic->ic_ifp->if_softc; 3880203134Sthompsa 3881203134Sthompsa RUN_LOCK(sc); 3882203134Sthompsa run_set_chan(sc, ic->ic_curchan); 3883203134Sthompsa RUN_UNLOCK(sc); 3884203134Sthompsa 3885203134Sthompsa return; 3886203134Sthompsa} 3887203134Sthompsa 3888203134Sthompsastatic void 3889203134Sthompsarun_scan_start(struct ieee80211com *ic) 3890203134Sthompsa{ 3891203134Sthompsa struct run_softc *sc = ic->ic_ifp->if_softc; 3892203134Sthompsa uint32_t tmp; 3893203134Sthompsa 3894203134Sthompsa RUN_LOCK(sc); 3895203134Sthompsa 3896203134Sthompsa /* abort TSF synchronization */ 3897203134Sthompsa run_read(sc, RT2860_BCN_TIME_CFG, &tmp); 3898203134Sthompsa run_write(sc, RT2860_BCN_TIME_CFG, 3899203134Sthompsa tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN | 3900203134Sthompsa RT2860_TBTT_TIMER_EN)); 3901203134Sthompsa run_set_bssid(sc, sc->sc_ifp->if_broadcastaddr); 3902203134Sthompsa 3903203134Sthompsa RUN_UNLOCK(sc); 3904203134Sthompsa 3905203134Sthompsa return; 3906203134Sthompsa} 3907203134Sthompsa 3908203134Sthompsastatic void 3909203134Sthompsarun_scan_end(struct ieee80211com *ic) 3910203134Sthompsa{ 3911203134Sthompsa struct run_softc *sc = ic->ic_ifp->if_softc; 3912203134Sthompsa 3913203134Sthompsa RUN_LOCK(sc); 3914203134Sthompsa 3915203134Sthompsa run_enable_tsf_sync(sc); 3916203134Sthompsa /* XXX keep local copy */ 3917203134Sthompsa run_set_bssid(sc, sc->sc_bssid); 3918203134Sthompsa 3919203134Sthompsa RUN_UNLOCK(sc); 3920203134Sthompsa 3921203134Sthompsa return; 3922203134Sthompsa} 3923203134Sthompsa 3924208019Sthompsa/* 3925208019Sthompsa * Could be called from ieee80211_node_timeout() 3926208019Sthompsa * (non-sleepable thread) 3927208019Sthompsa */ 3928208019Sthompsastatic void 3929208019Sthompsarun_update_beacon(struct ieee80211vap *vap, int item) 3930203134Sthompsa{ 3931208019Sthompsa struct ieee80211com *ic = vap->iv_ic; 3932208019Sthompsa struct run_softc *sc = ic->ic_ifp->if_softc; 3933218492Sbschmidt struct run_vap *rvp = RUN_VAP(vap); 3934218492Sbschmidt int mcast = 0; 3935208019Sthompsa uint32_t i; 3936208019Sthompsa 3937218492Sbschmidt KASSERT(vap != NULL, ("no beacon")); 3938218492Sbschmidt 3939218492Sbschmidt switch (item) { 3940218492Sbschmidt case IEEE80211_BEACON_ERP: 3941218492Sbschmidt run_updateslot(ic->ic_ifp); 3942218492Sbschmidt break; 3943218492Sbschmidt case IEEE80211_BEACON_HTINFO: 3944218492Sbschmidt run_updateprot(ic); 3945218492Sbschmidt break; 3946218492Sbschmidt case IEEE80211_BEACON_TIM: 3947218492Sbschmidt mcast = 1; /*TODO*/ 3948218492Sbschmidt break; 3949218492Sbschmidt default: 3950218492Sbschmidt break; 3951218492Sbschmidt } 3952218492Sbschmidt 3953218492Sbschmidt setbit(rvp->bo.bo_flags, item); 3954218492Sbschmidt ieee80211_beacon_update(vap->iv_bss, &rvp->bo, rvp->beacon_mbuf, mcast); 3955218492Sbschmidt 3956208019Sthompsa i = RUN_CMDQ_GET(&sc->cmdq_store); 3957208019Sthompsa DPRINTF("cmdq_store=%d\n", i); 3958208019Sthompsa sc->cmdq[i].func = run_update_beacon_cb; 3959208019Sthompsa sc->cmdq[i].arg0 = vap; 3960208019Sthompsa ieee80211_runtask(ic, &sc->cmdq_task); 3961208019Sthompsa 3962208019Sthompsa return; 3963203134Sthompsa} 3964203134Sthompsa 3965203134Sthompsastatic void 3966208019Sthompsarun_update_beacon_cb(void *arg) 3967203134Sthompsa{ 3968208019Sthompsa struct ieee80211vap *vap = arg; 3969218492Sbschmidt struct run_vap *rvp = RUN_VAP(vap); 3970203134Sthompsa struct ieee80211com *ic = vap->iv_ic; 3971203134Sthompsa struct run_softc *sc = ic->ic_ifp->if_softc; 3972203134Sthompsa struct rt2860_txwi txwi; 3973203134Sthompsa struct mbuf *m; 3974208019Sthompsa uint8_t ridx; 3975203134Sthompsa 3976209917Sthompsa if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC) 3977208019Sthompsa return; 3978236439Shselasky if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) 3979236439Shselasky return; 3980208019Sthompsa 3981218492Sbschmidt /* 3982218492Sbschmidt * No need to call ieee80211_beacon_update(), run_update_beacon() 3983218492Sbschmidt * is taking care of apropriate calls. 3984218492Sbschmidt */ 3985218492Sbschmidt if (rvp->beacon_mbuf == NULL) { 3986218492Sbschmidt rvp->beacon_mbuf = ieee80211_beacon_alloc(vap->iv_bss, 3987218492Sbschmidt &rvp->bo); 3988218492Sbschmidt if (rvp->beacon_mbuf == NULL) 3989218492Sbschmidt return; 3990218492Sbschmidt } 3991218492Sbschmidt m = rvp->beacon_mbuf; 3992203134Sthompsa 3993203134Sthompsa memset(&txwi, 0, sizeof txwi); 3994203134Sthompsa txwi.wcid = 0xff; 3995203134Sthompsa txwi.len = htole16(m->m_pkthdr.len); 3996203134Sthompsa /* send beacons at the lowest available rate */ 3997208019Sthompsa ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ? 3998208019Sthompsa RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1; 3999208019Sthompsa txwi.phy = htole16(rt2860_rates[ridx].mcs); 4000208019Sthompsa if (rt2860_rates[ridx].phy == IEEE80211_T_OFDM) 4001203134Sthompsa txwi.phy |= htole16(RT2860_PHY_OFDM); 4002203134Sthompsa txwi.txop = RT2860_TX_TXOP_HT; 4003203134Sthompsa txwi.flags = RT2860_TX_TS; 4004209144Sthompsa txwi.xflags = RT2860_TX_NSEQ; 4005203134Sthompsa 4006218492Sbschmidt run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id), 4007208019Sthompsa (uint8_t *)&txwi, sizeof txwi); 4008218492Sbschmidt run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id) + sizeof txwi, 4009203134Sthompsa mtod(m, uint8_t *), (m->m_pkthdr.len + 1) & ~1); /* roundup len */ 4010203134Sthompsa 4011203134Sthompsa return; 4012203134Sthompsa} 4013203134Sthompsa 4014203134Sthompsastatic void 4015203134Sthompsarun_updateprot(struct ieee80211com *ic) 4016203134Sthompsa{ 4017203134Sthompsa struct run_softc *sc = ic->ic_ifp->if_softc; 4018218492Sbschmidt uint32_t i; 4019218492Sbschmidt 4020218492Sbschmidt i = RUN_CMDQ_GET(&sc->cmdq_store); 4021218492Sbschmidt DPRINTF("cmdq_store=%d\n", i); 4022218492Sbschmidt sc->cmdq[i].func = run_updateprot_cb; 4023218492Sbschmidt sc->cmdq[i].arg0 = ic; 4024218492Sbschmidt ieee80211_runtask(ic, &sc->cmdq_task); 4025218492Sbschmidt} 4026218492Sbschmidt 4027218492Sbschmidtstatic void 4028218492Sbschmidtrun_updateprot_cb(void *arg) 4029218492Sbschmidt{ 4030218492Sbschmidt struct ieee80211com *ic = arg; 4031218492Sbschmidt struct run_softc *sc = ic->ic_ifp->if_softc; 4032203134Sthompsa uint32_t tmp; 4033203134Sthompsa 4034203134Sthompsa tmp = RT2860_RTSTH_EN | RT2860_PROT_NAV_SHORT | RT2860_TXOP_ALLOW_ALL; 4035203134Sthompsa /* setup protection frame rate (MCS code) */ 4036203134Sthompsa tmp |= (ic->ic_curmode == IEEE80211_MODE_11A) ? 4037203134Sthompsa rt2860_rates[RT2860_RIDX_OFDM6].mcs : 4038203134Sthompsa rt2860_rates[RT2860_RIDX_CCK11].mcs; 4039203134Sthompsa 4040203134Sthompsa /* CCK frames don't require protection */ 4041203134Sthompsa run_write(sc, RT2860_CCK_PROT_CFG, tmp); 4042203134Sthompsa if (ic->ic_flags & IEEE80211_F_USEPROT) { 4043203134Sthompsa if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 4044203134Sthompsa tmp |= RT2860_PROT_CTRL_RTS_CTS; 4045203134Sthompsa else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 4046203134Sthompsa tmp |= RT2860_PROT_CTRL_CTS; 4047203134Sthompsa } 4048203134Sthompsa run_write(sc, RT2860_OFDM_PROT_CFG, tmp); 4049203134Sthompsa} 4050203134Sthompsa 4051203134Sthompsastatic void 4052208019Sthompsarun_usb_timeout_cb(void *arg) 4053203134Sthompsa{ 4054208019Sthompsa struct ieee80211vap *vap = arg; 4055208019Sthompsa struct run_softc *sc = vap->iv_ic->ic_ifp->if_softc; 4056203134Sthompsa 4057208019Sthompsa RUN_LOCK_ASSERT(sc, MA_OWNED); 4058203134Sthompsa 4059203134Sthompsa if(vap->iv_state == IEEE80211_S_RUN && 4060203134Sthompsa vap->iv_opmode != IEEE80211_M_STA) 4061203134Sthompsa run_reset_livelock(sc); 4062209917Sthompsa else if (vap->iv_state == IEEE80211_S_SCAN) { 4063203134Sthompsa DPRINTF("timeout caused by scan\n"); 4064203134Sthompsa /* cancel bgscan */ 4065203134Sthompsa ieee80211_cancel_scan(vap); 4066203134Sthompsa } else 4067203134Sthompsa DPRINTF("timeout by unknown cause\n"); 4068203134Sthompsa} 4069203134Sthompsa 4070203134Sthompsastatic void 4071203134Sthompsarun_reset_livelock(struct run_softc *sc) 4072203134Sthompsa{ 4073203134Sthompsa uint32_t tmp; 4074203134Sthompsa 4075208019Sthompsa RUN_LOCK_ASSERT(sc, MA_OWNED); 4076208019Sthompsa 4077203134Sthompsa /* 4078203134Sthompsa * In IBSS or HostAP modes (when the hardware sends beacons), the MAC 4079203134Sthompsa * can run into a livelock and start sending CTS-to-self frames like 4080203134Sthompsa * crazy if protection is enabled. Reset MAC/BBP for a while 4081203134Sthompsa */ 4082203134Sthompsa run_read(sc, RT2860_DEBUG, &tmp); 4083208019Sthompsa DPRINTFN(3, "debug reg %08x\n", tmp); 4084209917Sthompsa if ((tmp & (1 << 29)) && (tmp & (1 << 7 | 1 << 5))) { 4085203134Sthompsa DPRINTF("CTS-to-self livelock detected\n"); 4086203134Sthompsa run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_SRST); 4087203134Sthompsa run_delay(sc, 1); 4088203134Sthompsa run_write(sc, RT2860_MAC_SYS_CTRL, 4089203134Sthompsa RT2860_MAC_RX_EN | RT2860_MAC_TX_EN); 4090203134Sthompsa } 4091203134Sthompsa} 4092203134Sthompsa 4093203134Sthompsastatic void 4094203134Sthompsarun_update_promisc_locked(struct ifnet *ifp) 4095203134Sthompsa{ 4096203134Sthompsa struct run_softc *sc = ifp->if_softc; 4097203134Sthompsa uint32_t tmp; 4098203134Sthompsa 4099203134Sthompsa run_read(sc, RT2860_RX_FILTR_CFG, &tmp); 4100203134Sthompsa 4101203134Sthompsa tmp |= RT2860_DROP_UC_NOME; 4102203134Sthompsa if (ifp->if_flags & IFF_PROMISC) 4103203134Sthompsa tmp &= ~RT2860_DROP_UC_NOME; 4104203134Sthompsa 4105203134Sthompsa run_write(sc, RT2860_RX_FILTR_CFG, tmp); 4106203134Sthompsa 4107203134Sthompsa DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ? 4108203134Sthompsa "entering" : "leaving"); 4109203134Sthompsa} 4110203134Sthompsa 4111203134Sthompsastatic void 4112203134Sthompsarun_update_promisc(struct ifnet *ifp) 4113203134Sthompsa{ 4114203134Sthompsa struct run_softc *sc = ifp->if_softc; 4115203134Sthompsa 4116203134Sthompsa if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 4117203134Sthompsa return; 4118203134Sthompsa 4119203134Sthompsa RUN_LOCK(sc); 4120203134Sthompsa run_update_promisc_locked(ifp); 4121203134Sthompsa RUN_UNLOCK(sc); 4122203134Sthompsa} 4123203134Sthompsa 4124203134Sthompsastatic void 4125203134Sthompsarun_enable_tsf_sync(struct run_softc *sc) 4126203134Sthompsa{ 4127208019Sthompsa struct ieee80211com *ic = sc->sc_ifp->if_l2com; 4128203134Sthompsa struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 4129203134Sthompsa uint32_t tmp; 4130203134Sthompsa 4131208019Sthompsa DPRINTF("rvp_id=%d ic_opmode=%d\n", RUN_VAP(vap)->rvp_id, ic->ic_opmode); 4132208019Sthompsa 4133203134Sthompsa run_read(sc, RT2860_BCN_TIME_CFG, &tmp); 4134203134Sthompsa tmp &= ~0x1fffff; 4135203134Sthompsa tmp |= vap->iv_bss->ni_intval * 16; 4136203134Sthompsa tmp |= RT2860_TSF_TIMER_EN | RT2860_TBTT_TIMER_EN; 4137203134Sthompsa 4138208019Sthompsa if (ic->ic_opmode == IEEE80211_M_STA) { 4139203134Sthompsa /* 4140203134Sthompsa * Local TSF is always updated with remote TSF on beacon 4141203134Sthompsa * reception. 4142203134Sthompsa */ 4143203134Sthompsa tmp |= 1 << RT2860_TSF_SYNC_MODE_SHIFT; 4144208019Sthompsa } else if (ic->ic_opmode == IEEE80211_M_IBSS) { 4145203134Sthompsa tmp |= RT2860_BCN_TX_EN; 4146203134Sthompsa /* 4147203134Sthompsa * Local TSF is updated with remote TSF on beacon reception 4148203134Sthompsa * only if the remote TSF is greater than local TSF. 4149203134Sthompsa */ 4150203134Sthompsa tmp |= 2 << RT2860_TSF_SYNC_MODE_SHIFT; 4151208019Sthompsa } else if (ic->ic_opmode == IEEE80211_M_HOSTAP || 4152208019Sthompsa ic->ic_opmode == IEEE80211_M_MBSS) { 4153203134Sthompsa tmp |= RT2860_BCN_TX_EN; 4154203134Sthompsa /* SYNC with nobody */ 4155203134Sthompsa tmp |= 3 << RT2860_TSF_SYNC_MODE_SHIFT; 4156208019Sthompsa } else { 4157203134Sthompsa DPRINTF("Enabling TSF failed. undefined opmode\n"); 4158208019Sthompsa return; 4159208019Sthompsa } 4160203134Sthompsa 4161203134Sthompsa run_write(sc, RT2860_BCN_TIME_CFG, tmp); 4162203134Sthompsa} 4163203134Sthompsa 4164203134Sthompsastatic void 4165203134Sthompsarun_enable_mrr(struct run_softc *sc) 4166203134Sthompsa{ 4167203134Sthompsa#define CCK(mcs) (mcs) 4168203134Sthompsa#define OFDM(mcs) (1 << 3 | (mcs)) 4169203134Sthompsa run_write(sc, RT2860_LG_FBK_CFG0, 4170203134Sthompsa OFDM(6) << 28 | /* 54->48 */ 4171203134Sthompsa OFDM(5) << 24 | /* 48->36 */ 4172203134Sthompsa OFDM(4) << 20 | /* 36->24 */ 4173203134Sthompsa OFDM(3) << 16 | /* 24->18 */ 4174203134Sthompsa OFDM(2) << 12 | /* 18->12 */ 4175203134Sthompsa OFDM(1) << 8 | /* 12-> 9 */ 4176203134Sthompsa OFDM(0) << 4 | /* 9-> 6 */ 4177203134Sthompsa OFDM(0)); /* 6-> 6 */ 4178203134Sthompsa 4179203134Sthompsa run_write(sc, RT2860_LG_FBK_CFG1, 4180203134Sthompsa CCK(2) << 12 | /* 11->5.5 */ 4181203134Sthompsa CCK(1) << 8 | /* 5.5-> 2 */ 4182203134Sthompsa CCK(0) << 4 | /* 2-> 1 */ 4183203134Sthompsa CCK(0)); /* 1-> 1 */ 4184203134Sthompsa#undef OFDM 4185203134Sthompsa#undef CCK 4186203134Sthompsa} 4187203134Sthompsa 4188203134Sthompsastatic void 4189203134Sthompsarun_set_txpreamble(struct run_softc *sc) 4190203134Sthompsa{ 4191203134Sthompsa struct ieee80211com *ic = sc->sc_ifp->if_l2com; 4192203134Sthompsa uint32_t tmp; 4193203134Sthompsa 4194203134Sthompsa run_read(sc, RT2860_AUTO_RSP_CFG, &tmp); 4195203134Sthompsa if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 4196203134Sthompsa tmp |= RT2860_CCK_SHORT_EN; 4197203134Sthompsa else 4198203134Sthompsa tmp &= ~RT2860_CCK_SHORT_EN; 4199203134Sthompsa run_write(sc, RT2860_AUTO_RSP_CFG, tmp); 4200203134Sthompsa} 4201203134Sthompsa 4202203134Sthompsastatic void 4203203134Sthompsarun_set_basicrates(struct run_softc *sc) 4204203134Sthompsa{ 4205203134Sthompsa struct ieee80211com *ic = sc->sc_ifp->if_l2com; 4206203134Sthompsa 4207203134Sthompsa /* set basic rates mask */ 4208203134Sthompsa if (ic->ic_curmode == IEEE80211_MODE_11B) 4209203134Sthompsa run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x003); 4210203134Sthompsa else if (ic->ic_curmode == IEEE80211_MODE_11A) 4211203134Sthompsa run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x150); 4212203134Sthompsa else /* 11g */ 4213203134Sthompsa run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x15f); 4214203134Sthompsa} 4215203134Sthompsa 4216203134Sthompsastatic void 4217203134Sthompsarun_set_leds(struct run_softc *sc, uint16_t which) 4218203134Sthompsa{ 4219203134Sthompsa (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LEDS, 4220203134Sthompsa which | (sc->leds & 0x7f)); 4221203134Sthompsa} 4222203134Sthompsa 4223203134Sthompsastatic void 4224203134Sthompsarun_set_bssid(struct run_softc *sc, const uint8_t *bssid) 4225203134Sthompsa{ 4226203134Sthompsa run_write(sc, RT2860_MAC_BSSID_DW0, 4227203134Sthompsa bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24); 4228203134Sthompsa run_write(sc, RT2860_MAC_BSSID_DW1, 4229203134Sthompsa bssid[4] | bssid[5] << 8); 4230203134Sthompsa} 4231203134Sthompsa 4232203134Sthompsastatic void 4233203134Sthompsarun_set_macaddr(struct run_softc *sc, const uint8_t *addr) 4234203134Sthompsa{ 4235203134Sthompsa run_write(sc, RT2860_MAC_ADDR_DW0, 4236203134Sthompsa addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24); 4237203134Sthompsa run_write(sc, RT2860_MAC_ADDR_DW1, 4238203134Sthompsa addr[4] | addr[5] << 8 | 0xff << 16); 4239203134Sthompsa} 4240203134Sthompsa 4241203134Sthompsastatic void 4242203134Sthompsarun_updateslot(struct ifnet *ifp) 4243203134Sthompsa{ 4244203134Sthompsa struct run_softc *sc = ifp->if_softc; 4245203134Sthompsa struct ieee80211com *ic = ifp->if_l2com; 4246218492Sbschmidt uint32_t i; 4247218492Sbschmidt 4248218492Sbschmidt i = RUN_CMDQ_GET(&sc->cmdq_store); 4249218492Sbschmidt DPRINTF("cmdq_store=%d\n", i); 4250218492Sbschmidt sc->cmdq[i].func = run_updateslot_cb; 4251218492Sbschmidt sc->cmdq[i].arg0 = ifp; 4252218492Sbschmidt ieee80211_runtask(ic, &sc->cmdq_task); 4253218492Sbschmidt 4254218492Sbschmidt return; 4255218492Sbschmidt} 4256218492Sbschmidt 4257218492Sbschmidt/* ARGSUSED */ 4258218492Sbschmidtstatic void 4259218492Sbschmidtrun_updateslot_cb(void *arg) 4260218492Sbschmidt{ 4261218492Sbschmidt struct ifnet *ifp = arg; 4262218492Sbschmidt struct run_softc *sc = ifp->if_softc; 4263218492Sbschmidt struct ieee80211com *ic = ifp->if_l2com; 4264203134Sthompsa uint32_t tmp; 4265203134Sthompsa 4266203134Sthompsa run_read(sc, RT2860_BKOFF_SLOT_CFG, &tmp); 4267203134Sthompsa tmp &= ~0xff; 4268203134Sthompsa tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20; 4269203134Sthompsa run_write(sc, RT2860_BKOFF_SLOT_CFG, tmp); 4270203134Sthompsa} 4271203134Sthompsa 4272208019Sthompsastatic void 4273208019Sthompsarun_update_mcast(struct ifnet *ifp) 4274208019Sthompsa{ 4275208019Sthompsa /* h/w filter supports getting everything or nothing */ 4276208019Sthompsa ifp->if_flags |= IFF_ALLMULTI; 4277208019Sthompsa} 4278208019Sthompsa 4279203134Sthompsastatic int8_t 4280203134Sthompsarun_rssi2dbm(struct run_softc *sc, uint8_t rssi, uint8_t rxchain) 4281203134Sthompsa{ 4282203134Sthompsa struct ieee80211com *ic = sc->sc_ifp->if_l2com; 4283203134Sthompsa struct ieee80211_channel *c = ic->ic_curchan; 4284203134Sthompsa int delta; 4285203134Sthompsa 4286203134Sthompsa if (IEEE80211_IS_CHAN_5GHZ(c)) { 4287203134Sthompsa uint32_t chan = ieee80211_chan2ieee(ic, c); 4288203134Sthompsa delta = sc->rssi_5ghz[rxchain]; 4289203134Sthompsa 4290203134Sthompsa /* determine channel group */ 4291203134Sthompsa if (chan <= 64) 4292203134Sthompsa delta -= sc->lna[1]; 4293203134Sthompsa else if (chan <= 128) 4294203134Sthompsa delta -= sc->lna[2]; 4295203134Sthompsa else 4296203134Sthompsa delta -= sc->lna[3]; 4297203134Sthompsa } else 4298203134Sthompsa delta = sc->rssi_2ghz[rxchain] - sc->lna[0]; 4299203134Sthompsa 4300209917Sthompsa return (-12 - delta - rssi); 4301203134Sthompsa} 4302203134Sthompsa 4303203134Sthompsastatic int 4304203134Sthompsarun_bbp_init(struct run_softc *sc) 4305203134Sthompsa{ 4306203134Sthompsa int i, error, ntries; 4307203134Sthompsa uint8_t bbp0; 4308203134Sthompsa 4309203134Sthompsa /* wait for BBP to wake up */ 4310203134Sthompsa for (ntries = 0; ntries < 20; ntries++) { 4311203134Sthompsa if ((error = run_bbp_read(sc, 0, &bbp0)) != 0) 4312203134Sthompsa return error; 4313203134Sthompsa if (bbp0 != 0 && bbp0 != 0xff) 4314203134Sthompsa break; 4315203134Sthompsa } 4316203134Sthompsa if (ntries == 20) 4317209917Sthompsa return (ETIMEDOUT); 4318203134Sthompsa 4319203134Sthompsa /* initialize BBP registers to default values */ 4320233774Shselasky for (i = 0; i < N(rt2860_def_bbp); i++) { 4321203134Sthompsa run_bbp_write(sc, rt2860_def_bbp[i].reg, 4322203134Sthompsa rt2860_def_bbp[i].val); 4323203134Sthompsa } 4324203134Sthompsa 4325203134Sthompsa /* fix BBP84 for RT2860E */ 4326205042Sthompsa if (sc->mac_ver == 0x2860 && sc->mac_rev != 0x0101) 4327205042Sthompsa run_bbp_write(sc, 84, 0x19); 4328203134Sthompsa 4329205042Sthompsa if (sc->mac_ver >= 0x3070) { 4330203134Sthompsa run_bbp_write(sc, 79, 0x13); 4331203134Sthompsa run_bbp_write(sc, 80, 0x05); 4332203134Sthompsa run_bbp_write(sc, 81, 0x33); 4333205042Sthompsa } else if (sc->mac_ver == 0x2860 && sc->mac_rev == 0x0100) { 4334203134Sthompsa run_bbp_write(sc, 69, 0x16); 4335203134Sthompsa run_bbp_write(sc, 73, 0x12); 4336203134Sthompsa } 4337209917Sthompsa return (0); 4338203134Sthompsa} 4339203134Sthompsa 4340203134Sthompsastatic int 4341203134Sthompsarun_rt3070_rf_init(struct run_softc *sc) 4342203134Sthompsa{ 4343203134Sthompsa uint32_t tmp; 4344205042Sthompsa uint8_t rf, target, bbp4; 4345203134Sthompsa int i; 4346203134Sthompsa 4347203134Sthompsa run_rt3070_rf_read(sc, 30, &rf); 4348203134Sthompsa /* toggle RF R30 bit 7 */ 4349203134Sthompsa run_rt3070_rf_write(sc, 30, rf | 0x80); 4350203134Sthompsa run_delay(sc, 10); 4351203134Sthompsa run_rt3070_rf_write(sc, 30, rf & ~0x80); 4352203134Sthompsa 4353203134Sthompsa /* initialize RF registers to default value */ 4354205042Sthompsa if (sc->mac_ver == 0x3572) { 4355233774Shselasky for (i = 0; i < N(rt3572_def_rf); i++) { 4356205042Sthompsa run_rt3070_rf_write(sc, rt3572_def_rf[i].reg, 4357205042Sthompsa rt3572_def_rf[i].val); 4358205042Sthompsa } 4359205042Sthompsa } else { 4360233774Shselasky for (i = 0; i < N(rt3070_def_rf); i++) { 4361205042Sthompsa run_rt3070_rf_write(sc, rt3070_def_rf[i].reg, 4362205042Sthompsa rt3070_def_rf[i].val); 4363205042Sthompsa } 4364203134Sthompsa } 4365205042Sthompsa 4366205042Sthompsa if (sc->mac_ver == 0x3070) { 4367203134Sthompsa /* change voltage from 1.2V to 1.35V for RT3070 */ 4368203134Sthompsa run_read(sc, RT3070_LDO_CFG0, &tmp); 4369203134Sthompsa tmp = (tmp & ~0x0f000000) | 0x0d000000; 4370203134Sthompsa run_write(sc, RT3070_LDO_CFG0, tmp); 4371203134Sthompsa 4372205042Sthompsa } else if (sc->mac_ver == 0x3071) { 4373203134Sthompsa run_rt3070_rf_read(sc, 6, &rf); 4374203134Sthompsa run_rt3070_rf_write(sc, 6, rf | 0x40); 4375203134Sthompsa run_rt3070_rf_write(sc, 31, 0x14); 4376203134Sthompsa 4377203134Sthompsa run_read(sc, RT3070_LDO_CFG0, &tmp); 4378203134Sthompsa tmp &= ~0x1f000000; 4379205042Sthompsa if (sc->mac_rev < 0x0211) 4380205042Sthompsa tmp |= 0x0d000000; /* 1.3V */ 4381203134Sthompsa else 4382205042Sthompsa tmp |= 0x01000000; /* 1.2V */ 4383203134Sthompsa run_write(sc, RT3070_LDO_CFG0, tmp); 4384203134Sthompsa 4385203134Sthompsa /* patch LNA_PE_G1 */ 4386203134Sthompsa run_read(sc, RT3070_GPIO_SWITCH, &tmp); 4387203134Sthompsa run_write(sc, RT3070_GPIO_SWITCH, tmp & ~0x20); 4388208019Sthompsa 4389209917Sthompsa } else if (sc->mac_ver == 0x3572) { 4390205042Sthompsa run_rt3070_rf_read(sc, 6, &rf); 4391205042Sthompsa run_rt3070_rf_write(sc, 6, rf | 0x40); 4392205042Sthompsa 4393208019Sthompsa /* increase voltage from 1.2V to 1.35V */ 4394208019Sthompsa run_read(sc, RT3070_LDO_CFG0, &tmp); 4395208019Sthompsa tmp = (tmp & ~0x1f000000) | 0x0d000000; 4396208019Sthompsa run_write(sc, RT3070_LDO_CFG0, tmp); 4397203134Sthompsa 4398209917Sthompsa if (sc->mac_rev < 0x0211 || !sc->patch_dac) { 4399203134Sthompsa run_delay(sc, 1); /* wait for 1msec */ 4400205042Sthompsa /* decrease voltage back to 1.2V */ 4401203134Sthompsa tmp = (tmp & ~0x1f000000) | 0x01000000; 4402203134Sthompsa run_write(sc, RT3070_LDO_CFG0, tmp); 4403203134Sthompsa } 4404203134Sthompsa } 4405203134Sthompsa 4406203134Sthompsa /* select 20MHz bandwidth */ 4407203134Sthompsa run_rt3070_rf_read(sc, 31, &rf); 4408203134Sthompsa run_rt3070_rf_write(sc, 31, rf & ~0x20); 4409203134Sthompsa 4410203134Sthompsa /* calibrate filter for 20MHz bandwidth */ 4411203134Sthompsa sc->rf24_20mhz = 0x1f; /* default value */ 4412205042Sthompsa target = (sc->mac_ver < 0x3071) ? 0x16 : 0x13; 4413205042Sthompsa run_rt3070_filter_calib(sc, 0x07, target, &sc->rf24_20mhz); 4414203134Sthompsa 4415203134Sthompsa /* select 40MHz bandwidth */ 4416203134Sthompsa run_bbp_read(sc, 4, &bbp4); 4417203134Sthompsa run_bbp_write(sc, 4, (bbp4 & ~0x08) | 0x10); 4418205042Sthompsa run_rt3070_rf_read(sc, 31, &rf); 4419205042Sthompsa run_rt3070_rf_write(sc, 31, rf | 0x20); 4420203134Sthompsa 4421203134Sthompsa /* calibrate filter for 40MHz bandwidth */ 4422203134Sthompsa sc->rf24_40mhz = 0x2f; /* default value */ 4423205042Sthompsa target = (sc->mac_ver < 0x3071) ? 0x19 : 0x15; 4424205042Sthompsa run_rt3070_filter_calib(sc, 0x27, target, &sc->rf24_40mhz); 4425203134Sthompsa 4426203134Sthompsa /* go back to 20MHz bandwidth */ 4427203134Sthompsa run_bbp_read(sc, 4, &bbp4); 4428203134Sthompsa run_bbp_write(sc, 4, bbp4 & ~0x18); 4429203134Sthompsa 4430205042Sthompsa if (sc->mac_ver == 0x3572) { 4431205042Sthompsa /* save default BBP registers 25 and 26 values */ 4432205042Sthompsa run_bbp_read(sc, 25, &sc->bbp25); 4433205042Sthompsa run_bbp_read(sc, 26, &sc->bbp26); 4434205042Sthompsa } else if (sc->mac_rev < 0x0211) 4435203134Sthompsa run_rt3070_rf_write(sc, 27, 0x03); 4436203134Sthompsa 4437203134Sthompsa run_read(sc, RT3070_OPT_14, &tmp); 4438203134Sthompsa run_write(sc, RT3070_OPT_14, tmp | 1); 4439203134Sthompsa 4440205042Sthompsa if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) { 4441205042Sthompsa run_rt3070_rf_read(sc, 17, &rf); 4442205042Sthompsa rf &= ~RT3070_TX_LO1; 4443205042Sthompsa if ((sc->mac_ver == 0x3070 || 4444205042Sthompsa (sc->mac_ver == 0x3071 && sc->mac_rev >= 0x0211)) && 4445205042Sthompsa !sc->ext_2ghz_lna) 4446205042Sthompsa rf |= 0x20; /* fix for long range Rx issue */ 4447205042Sthompsa if (sc->txmixgain_2ghz >= 1) 4448205042Sthompsa rf = (rf & ~0x7) | sc->txmixgain_2ghz; 4449205042Sthompsa run_rt3070_rf_write(sc, 17, rf); 4450205042Sthompsa } 4451205042Sthompsa 4452205042Sthompsa if (sc->mac_rev == 0x3071) { 4453203134Sthompsa run_rt3070_rf_read(sc, 1, &rf); 4454203134Sthompsa rf &= ~(RT3070_RX0_PD | RT3070_TX0_PD); 4455203134Sthompsa rf |= RT3070_RF_BLOCK | RT3070_RX1_PD | RT3070_TX1_PD; 4456203134Sthompsa run_rt3070_rf_write(sc, 1, rf); 4457203134Sthompsa 4458203134Sthompsa run_rt3070_rf_read(sc, 15, &rf); 4459203134Sthompsa run_rt3070_rf_write(sc, 15, rf & ~RT3070_TX_LO2); 4460203134Sthompsa 4461203134Sthompsa run_rt3070_rf_read(sc, 20, &rf); 4462203134Sthompsa run_rt3070_rf_write(sc, 20, rf & ~RT3070_RX_LO1); 4463203134Sthompsa 4464203134Sthompsa run_rt3070_rf_read(sc, 21, &rf); 4465203134Sthompsa run_rt3070_rf_write(sc, 21, rf & ~RT3070_RX_LO2); 4466205042Sthompsa } 4467203134Sthompsa 4468205042Sthompsa if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) { 4469205042Sthompsa /* fix Tx to Rx IQ glitch by raising RF voltage */ 4470203134Sthompsa run_rt3070_rf_read(sc, 27, &rf); 4471203134Sthompsa rf &= ~0x77; 4472205042Sthompsa if (sc->mac_rev < 0x0211) 4473203134Sthompsa rf |= 0x03; 4474203134Sthompsa run_rt3070_rf_write(sc, 27, rf); 4475203134Sthompsa } 4476209917Sthompsa return (0); 4477203134Sthompsa} 4478203134Sthompsa 4479203134Sthompsastatic int 4480203134Sthompsarun_rt3070_filter_calib(struct run_softc *sc, uint8_t init, uint8_t target, 4481203134Sthompsa uint8_t *val) 4482203134Sthompsa{ 4483203134Sthompsa uint8_t rf22, rf24; 4484203134Sthompsa uint8_t bbp55_pb, bbp55_sb, delta; 4485203134Sthompsa int ntries; 4486203134Sthompsa 4487203134Sthompsa /* program filter */ 4488205042Sthompsa run_rt3070_rf_read(sc, 24, &rf24); 4489205042Sthompsa rf24 = (rf24 & 0xc0) | init; /* initial filter value */ 4490203134Sthompsa run_rt3070_rf_write(sc, 24, rf24); 4491203134Sthompsa 4492203134Sthompsa /* enable baseband loopback mode */ 4493203134Sthompsa run_rt3070_rf_read(sc, 22, &rf22); 4494203134Sthompsa run_rt3070_rf_write(sc, 22, rf22 | 0x01); 4495203134Sthompsa 4496203134Sthompsa /* set power and frequency of passband test tone */ 4497203134Sthompsa run_bbp_write(sc, 24, 0x00); 4498203134Sthompsa for (ntries = 0; ntries < 100; ntries++) { 4499203134Sthompsa /* transmit test tone */ 4500203134Sthompsa run_bbp_write(sc, 25, 0x90); 4501203134Sthompsa run_delay(sc, 10); 4502203134Sthompsa /* read received power */ 4503203134Sthompsa run_bbp_read(sc, 55, &bbp55_pb); 4504203134Sthompsa if (bbp55_pb != 0) 4505203134Sthompsa break; 4506203134Sthompsa } 4507203134Sthompsa if (ntries == 100) 4508203134Sthompsa return ETIMEDOUT; 4509203134Sthompsa 4510203134Sthompsa /* set power and frequency of stopband test tone */ 4511203134Sthompsa run_bbp_write(sc, 24, 0x06); 4512203134Sthompsa for (ntries = 0; ntries < 100; ntries++) { 4513203134Sthompsa /* transmit test tone */ 4514203134Sthompsa run_bbp_write(sc, 25, 0x90); 4515203134Sthompsa run_delay(sc, 10); 4516203134Sthompsa /* read received power */ 4517203134Sthompsa run_bbp_read(sc, 55, &bbp55_sb); 4518203134Sthompsa 4519203134Sthompsa delta = bbp55_pb - bbp55_sb; 4520203134Sthompsa if (delta > target) 4521203134Sthompsa break; 4522203134Sthompsa 4523203134Sthompsa /* reprogram filter */ 4524203134Sthompsa rf24++; 4525203134Sthompsa run_rt3070_rf_write(sc, 24, rf24); 4526203134Sthompsa } 4527203134Sthompsa if (ntries < 100) { 4528203134Sthompsa if (rf24 != init) 4529203134Sthompsa rf24--; /* backtrack */ 4530203134Sthompsa *val = rf24; 4531203134Sthompsa run_rt3070_rf_write(sc, 24, rf24); 4532203134Sthompsa } 4533203134Sthompsa 4534203134Sthompsa /* restore initial state */ 4535203134Sthompsa run_bbp_write(sc, 24, 0x00); 4536203134Sthompsa 4537203134Sthompsa /* disable baseband loopback mode */ 4538203134Sthompsa run_rt3070_rf_read(sc, 22, &rf22); 4539203134Sthompsa run_rt3070_rf_write(sc, 22, rf22 & ~0x01); 4540203134Sthompsa 4541209917Sthompsa return (0); 4542203134Sthompsa} 4543203134Sthompsa 4544205042Sthompsastatic void 4545205042Sthompsarun_rt3070_rf_setup(struct run_softc *sc) 4546205042Sthompsa{ 4547205042Sthompsa uint8_t bbp, rf; 4548205042Sthompsa int i; 4549205042Sthompsa 4550205042Sthompsa if (sc->mac_ver == 0x3572) { 4551205042Sthompsa /* enable DC filter */ 4552205042Sthompsa if (sc->mac_rev >= 0x0201) 4553205042Sthompsa run_bbp_write(sc, 103, 0xc0); 4554205042Sthompsa 4555205042Sthompsa run_bbp_read(sc, 138, &bbp); 4556205042Sthompsa if (sc->ntxchains == 1) 4557205042Sthompsa bbp |= 0x20; /* turn off DAC1 */ 4558205042Sthompsa if (sc->nrxchains == 1) 4559205042Sthompsa bbp &= ~0x02; /* turn off ADC1 */ 4560205042Sthompsa run_bbp_write(sc, 138, bbp); 4561205042Sthompsa 4562205042Sthompsa if (sc->mac_rev >= 0x0211) { 4563205042Sthompsa /* improve power consumption */ 4564205042Sthompsa run_bbp_read(sc, 31, &bbp); 4565205042Sthompsa run_bbp_write(sc, 31, bbp & ~0x03); 4566205042Sthompsa } 4567205042Sthompsa 4568205042Sthompsa run_rt3070_rf_read(sc, 16, &rf); 4569205042Sthompsa rf = (rf & ~0x07) | sc->txmixgain_2ghz; 4570205042Sthompsa run_rt3070_rf_write(sc, 16, rf); 4571205042Sthompsa 4572205042Sthompsa } else if (sc->mac_ver == 0x3071) { 4573205042Sthompsa /* enable DC filter */ 4574205042Sthompsa if (sc->mac_rev >= 0x0201) 4575205042Sthompsa run_bbp_write(sc, 103, 0xc0); 4576205042Sthompsa 4577205042Sthompsa run_bbp_read(sc, 138, &bbp); 4578205042Sthompsa if (sc->ntxchains == 1) 4579205042Sthompsa bbp |= 0x20; /* turn off DAC1 */ 4580205042Sthompsa if (sc->nrxchains == 1) 4581205042Sthompsa bbp &= ~0x02; /* turn off ADC1 */ 4582205042Sthompsa run_bbp_write(sc, 138, bbp); 4583205042Sthompsa 4584205042Sthompsa if (sc->mac_rev >= 0x0211) { 4585205042Sthompsa /* improve power consumption */ 4586205042Sthompsa run_bbp_read(sc, 31, &bbp); 4587205042Sthompsa run_bbp_write(sc, 31, bbp & ~0x03); 4588205042Sthompsa } 4589205042Sthompsa 4590205042Sthompsa run_write(sc, RT2860_TX_SW_CFG1, 0); 4591205042Sthompsa if (sc->mac_rev < 0x0211) { 4592205042Sthompsa run_write(sc, RT2860_TX_SW_CFG2, 4593205042Sthompsa sc->patch_dac ? 0x2c : 0x0f); 4594205042Sthompsa } else 4595205042Sthompsa run_write(sc, RT2860_TX_SW_CFG2, 0); 4596205042Sthompsa 4597205042Sthompsa } else if (sc->mac_ver == 0x3070) { 4598205042Sthompsa if (sc->mac_rev >= 0x0201) { 4599205042Sthompsa /* enable DC filter */ 4600205042Sthompsa run_bbp_write(sc, 103, 0xc0); 4601205042Sthompsa 4602205042Sthompsa /* improve power consumption */ 4603205042Sthompsa run_bbp_read(sc, 31, &bbp); 4604205042Sthompsa run_bbp_write(sc, 31, bbp & ~0x03); 4605205042Sthompsa } 4606205042Sthompsa 4607205042Sthompsa if (sc->mac_rev < 0x0211) { 4608205042Sthompsa run_write(sc, RT2860_TX_SW_CFG1, 0); 4609205042Sthompsa run_write(sc, RT2860_TX_SW_CFG2, 0x2c); 4610205042Sthompsa } else 4611205042Sthompsa run_write(sc, RT2860_TX_SW_CFG2, 0); 4612205042Sthompsa } 4613205042Sthompsa 4614205042Sthompsa /* initialize RF registers from ROM for >=RT3071*/ 4615205042Sthompsa if (sc->mac_ver >= 0x3071) { 4616205042Sthompsa for (i = 0; i < 10; i++) { 4617205042Sthompsa if (sc->rf[i].reg == 0 || sc->rf[i].reg == 0xff) 4618205042Sthompsa continue; 4619205042Sthompsa run_rt3070_rf_write(sc, sc->rf[i].reg, sc->rf[i].val); 4620205042Sthompsa } 4621205042Sthompsa } 4622205042Sthompsa} 4623205042Sthompsa 4624203134Sthompsastatic int 4625203134Sthompsarun_txrx_enable(struct run_softc *sc) 4626203134Sthompsa{ 4627203134Sthompsa struct ieee80211com *ic = sc->sc_ifp->if_l2com; 4628203134Sthompsa uint32_t tmp; 4629203134Sthompsa int error, ntries; 4630203134Sthompsa 4631203134Sthompsa run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_TX_EN); 4632203134Sthompsa for (ntries = 0; ntries < 200; ntries++) { 4633203134Sthompsa if ((error = run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp)) != 0) 4634203134Sthompsa return error; 4635203134Sthompsa if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0) 4636203134Sthompsa break; 4637203134Sthompsa run_delay(sc, 50); 4638203134Sthompsa } 4639203134Sthompsa if (ntries == 200) 4640203134Sthompsa return ETIMEDOUT; 4641203134Sthompsa 4642203134Sthompsa run_delay(sc, 50); 4643203134Sthompsa 4644203134Sthompsa tmp |= RT2860_RX_DMA_EN | RT2860_TX_DMA_EN | RT2860_TX_WB_DDONE; 4645203134Sthompsa run_write(sc, RT2860_WPDMA_GLO_CFG, tmp); 4646203134Sthompsa 4647203134Sthompsa /* enable Rx bulk aggregation (set timeout and limit) */ 4648203134Sthompsa tmp = RT2860_USB_TX_EN | RT2860_USB_RX_EN | RT2860_USB_RX_AGG_EN | 4649203134Sthompsa RT2860_USB_RX_AGG_TO(128) | RT2860_USB_RX_AGG_LMT(2); 4650203134Sthompsa run_write(sc, RT2860_USB_DMA_CFG, tmp); 4651203134Sthompsa 4652203134Sthompsa /* set Rx filter */ 4653203134Sthompsa tmp = RT2860_DROP_CRC_ERR | RT2860_DROP_PHY_ERR; 4654203134Sthompsa if (ic->ic_opmode != IEEE80211_M_MONITOR) { 4655203134Sthompsa tmp |= RT2860_DROP_UC_NOME | RT2860_DROP_DUPL | 4656203134Sthompsa RT2860_DROP_CTS | RT2860_DROP_BA | RT2860_DROP_ACK | 4657203134Sthompsa RT2860_DROP_VER_ERR | RT2860_DROP_CTRL_RSV | 4658203134Sthompsa RT2860_DROP_CFACK | RT2860_DROP_CFEND; 4659203134Sthompsa if (ic->ic_opmode == IEEE80211_M_STA) 4660203134Sthompsa tmp |= RT2860_DROP_RTS | RT2860_DROP_PSPOLL; 4661203134Sthompsa } 4662203134Sthompsa run_write(sc, RT2860_RX_FILTR_CFG, tmp); 4663203134Sthompsa 4664203134Sthompsa run_write(sc, RT2860_MAC_SYS_CTRL, 4665203134Sthompsa RT2860_MAC_RX_EN | RT2860_MAC_TX_EN); 4666203134Sthompsa 4667209917Sthompsa return (0); 4668203134Sthompsa} 4669203134Sthompsa 4670203134Sthompsastatic void 4671203134Sthompsarun_init_locked(struct run_softc *sc) 4672203134Sthompsa{ 4673203134Sthompsa struct ifnet *ifp = sc->sc_ifp; 4674203134Sthompsa struct ieee80211com *ic = ifp->if_l2com; 4675203134Sthompsa uint32_t tmp; 4676203134Sthompsa uint8_t bbp1, bbp3; 4677203134Sthompsa int i; 4678203134Sthompsa int ridx; 4679203134Sthompsa int ntries; 4680203134Sthompsa 4681209917Sthompsa if (ic->ic_nrunning > 1) 4682208019Sthompsa return; 4683208019Sthompsa 4684203134Sthompsa run_stop(sc); 4685203134Sthompsa 4686233283Sbschmidt if (run_load_microcode(sc) != 0) { 4687233283Sbschmidt device_printf(sc->sc_dev, "could not load 8051 microcode\n"); 4688233283Sbschmidt goto fail; 4689233283Sbschmidt } 4690233283Sbschmidt 4691203134Sthompsa for (ntries = 0; ntries < 100; ntries++) { 4692203134Sthompsa if (run_read(sc, RT2860_ASIC_VER_ID, &tmp) != 0) 4693203134Sthompsa goto fail; 4694203134Sthompsa if (tmp != 0 && tmp != 0xffffffff) 4695203134Sthompsa break; 4696203134Sthompsa run_delay(sc, 10); 4697203134Sthompsa } 4698203134Sthompsa if (ntries == 100) 4699203134Sthompsa goto fail; 4700203134Sthompsa 4701203134Sthompsa for (i = 0; i != RUN_EP_QUEUES; i++) 4702203134Sthompsa run_setup_tx_list(sc, &sc->sc_epq[i]); 4703203134Sthompsa 4704203134Sthompsa run_set_macaddr(sc, IF_LLADDR(ifp)); 4705203134Sthompsa 4706203134Sthompsa for (ntries = 0; ntries < 100; ntries++) { 4707203134Sthompsa if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0) 4708203134Sthompsa goto fail; 4709203134Sthompsa if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0) 4710203134Sthompsa break; 4711203134Sthompsa run_delay(sc, 10); 4712203134Sthompsa } 4713203134Sthompsa if (ntries == 100) { 4714203138Sthompsa device_printf(sc->sc_dev, "timeout waiting for DMA engine\n"); 4715203134Sthompsa goto fail; 4716203134Sthompsa } 4717203134Sthompsa tmp &= 0xff0; 4718203134Sthompsa tmp |= RT2860_TX_WB_DDONE; 4719203134Sthompsa run_write(sc, RT2860_WPDMA_GLO_CFG, tmp); 4720203134Sthompsa 4721203134Sthompsa /* turn off PME_OEN to solve high-current issue */ 4722203134Sthompsa run_read(sc, RT2860_SYS_CTRL, &tmp); 4723203134Sthompsa run_write(sc, RT2860_SYS_CTRL, tmp & ~RT2860_PME_OEN); 4724203134Sthompsa 4725203134Sthompsa run_write(sc, RT2860_MAC_SYS_CTRL, 4726203134Sthompsa RT2860_BBP_HRST | RT2860_MAC_SRST); 4727203134Sthompsa run_write(sc, RT2860_USB_DMA_CFG, 0); 4728203134Sthompsa 4729203134Sthompsa if (run_reset(sc) != 0) { 4730203138Sthompsa device_printf(sc->sc_dev, "could not reset chipset\n"); 4731203134Sthompsa goto fail; 4732203134Sthompsa } 4733203134Sthompsa 4734203134Sthompsa run_write(sc, RT2860_MAC_SYS_CTRL, 0); 4735203134Sthompsa 4736203134Sthompsa /* init Tx power for all Tx rates (from EEPROM) */ 4737203134Sthompsa for (ridx = 0; ridx < 5; ridx++) { 4738203134Sthompsa if (sc->txpow20mhz[ridx] == 0xffffffff) 4739203134Sthompsa continue; 4740203134Sthompsa run_write(sc, RT2860_TX_PWR_CFG(ridx), sc->txpow20mhz[ridx]); 4741203134Sthompsa } 4742203134Sthompsa 4743233774Shselasky for (i = 0; i < N(rt2870_def_mac); i++) 4744203134Sthompsa run_write(sc, rt2870_def_mac[i].reg, rt2870_def_mac[i].val); 4745203134Sthompsa run_write(sc, RT2860_WMM_AIFSN_CFG, 0x00002273); 4746203134Sthompsa run_write(sc, RT2860_WMM_CWMIN_CFG, 0x00002344); 4747203134Sthompsa run_write(sc, RT2860_WMM_CWMAX_CFG, 0x000034aa); 4748203134Sthompsa 4749205042Sthompsa if (sc->mac_ver >= 0x3070) { 4750203134Sthompsa /* set delay of PA_PE assertion to 1us (unit of 0.25us) */ 4751203134Sthompsa run_write(sc, RT2860_TX_SW_CFG0, 4752203134Sthompsa 4 << RT2860_DLY_PAPE_EN_SHIFT); 4753203134Sthompsa } 4754203134Sthompsa 4755203134Sthompsa /* wait while MAC is busy */ 4756203134Sthompsa for (ntries = 0; ntries < 100; ntries++) { 4757203134Sthompsa if (run_read(sc, RT2860_MAC_STATUS_REG, &tmp) != 0) 4758203134Sthompsa goto fail; 4759203134Sthompsa if (!(tmp & (RT2860_RX_STATUS_BUSY | RT2860_TX_STATUS_BUSY))) 4760203134Sthompsa break; 4761203134Sthompsa run_delay(sc, 10); 4762203134Sthompsa } 4763203134Sthompsa if (ntries == 100) 4764203134Sthompsa goto fail; 4765203134Sthompsa 4766203134Sthompsa /* clear Host to MCU mailbox */ 4767203134Sthompsa run_write(sc, RT2860_H2M_BBPAGENT, 0); 4768203134Sthompsa run_write(sc, RT2860_H2M_MAILBOX, 0); 4769203134Sthompsa run_delay(sc, 10); 4770203134Sthompsa 4771203134Sthompsa if (run_bbp_init(sc) != 0) { 4772203138Sthompsa device_printf(sc->sc_dev, "could not initialize BBP\n"); 4773203134Sthompsa goto fail; 4774203134Sthompsa } 4775203134Sthompsa 4776203134Sthompsa /* abort TSF synchronization */ 4777203134Sthompsa run_read(sc, RT2860_BCN_TIME_CFG, &tmp); 4778203134Sthompsa tmp &= ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN | 4779203134Sthompsa RT2860_TBTT_TIMER_EN); 4780203134Sthompsa run_write(sc, RT2860_BCN_TIME_CFG, tmp); 4781203134Sthompsa 4782203134Sthompsa /* clear RX WCID search table */ 4783203134Sthompsa run_set_region_4(sc, RT2860_WCID_ENTRY(0), 0, 512); 4784203134Sthompsa /* clear WCID attribute table */ 4785203134Sthompsa run_set_region_4(sc, RT2860_WCID_ATTR(0), 0, 8 * 32); 4786203134Sthompsa 4787209144Sthompsa /* hostapd sets a key before init. So, don't clear it. */ 4788209917Sthompsa if (sc->cmdq_key_set != RUN_CMDQ_GO) { 4789209144Sthompsa /* clear shared key table */ 4790209144Sthompsa run_set_region_4(sc, RT2860_SKEY(0, 0), 0, 8 * 32); 4791209144Sthompsa /* clear shared key mode */ 4792209144Sthompsa run_set_region_4(sc, RT2860_SKEY_MODE_0_7, 0, 4); 4793209144Sthompsa } 4794209144Sthompsa 4795203134Sthompsa run_read(sc, RT2860_US_CYC_CNT, &tmp); 4796203134Sthompsa tmp = (tmp & ~0xff) | 0x1e; 4797203134Sthompsa run_write(sc, RT2860_US_CYC_CNT, tmp); 4798203134Sthompsa 4799205042Sthompsa if (sc->mac_rev != 0x0101) 4800203134Sthompsa run_write(sc, RT2860_TXOP_CTRL_CFG, 0x0000583f); 4801203134Sthompsa 4802203134Sthompsa run_write(sc, RT2860_WMM_TXOP0_CFG, 0); 4803203134Sthompsa run_write(sc, RT2860_WMM_TXOP1_CFG, 48 << 16 | 96); 4804203134Sthompsa 4805203134Sthompsa /* write vendor-specific BBP values (from EEPROM) */ 4806208019Sthompsa for (i = 0; i < 10; i++) { 4807203134Sthompsa if (sc->bbp[i].reg == 0 || sc->bbp[i].reg == 0xff) 4808203134Sthompsa continue; 4809203134Sthompsa run_bbp_write(sc, sc->bbp[i].reg, sc->bbp[i].val); 4810203134Sthompsa } 4811203134Sthompsa 4812203134Sthompsa /* select Main antenna for 1T1R devices */ 4813203134Sthompsa if (sc->rf_rev == RT3070_RF_3020) 4814203134Sthompsa run_set_rx_antenna(sc, 0); 4815203134Sthompsa 4816203134Sthompsa /* send LEDs operating mode to microcontroller */ 4817203134Sthompsa (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED1, sc->led[0]); 4818203134Sthompsa (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED2, sc->led[1]); 4819203134Sthompsa (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED3, sc->led[2]); 4820203134Sthompsa 4821205042Sthompsa if (sc->mac_ver >= 0x3070) 4822205042Sthompsa run_rt3070_rf_init(sc); 4823205042Sthompsa 4824203134Sthompsa /* disable non-existing Rx chains */ 4825203134Sthompsa run_bbp_read(sc, 3, &bbp3); 4826203134Sthompsa bbp3 &= ~(1 << 3 | 1 << 4); 4827203134Sthompsa if (sc->nrxchains == 2) 4828203134Sthompsa bbp3 |= 1 << 3; 4829203134Sthompsa else if (sc->nrxchains == 3) 4830203134Sthompsa bbp3 |= 1 << 4; 4831203134Sthompsa run_bbp_write(sc, 3, bbp3); 4832203134Sthompsa 4833203134Sthompsa /* disable non-existing Tx chains */ 4834203134Sthompsa run_bbp_read(sc, 1, &bbp1); 4835203134Sthompsa if (sc->ntxchains == 1) 4836203134Sthompsa bbp1 &= ~(1 << 3 | 1 << 4); 4837203134Sthompsa run_bbp_write(sc, 1, bbp1); 4838203134Sthompsa 4839205042Sthompsa if (sc->mac_ver >= 0x3070) 4840205042Sthompsa run_rt3070_rf_setup(sc); 4841203134Sthompsa 4842203134Sthompsa /* select default channel */ 4843203134Sthompsa run_set_chan(sc, ic->ic_curchan); 4844203134Sthompsa 4845203134Sthompsa /* setup initial protection mode */ 4846218492Sbschmidt run_updateprot_cb(ic); 4847203134Sthompsa 4848203134Sthompsa /* turn radio LED on */ 4849203134Sthompsa run_set_leds(sc, RT2860_LED_RADIO); 4850203134Sthompsa 4851203134Sthompsa ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 4852203134Sthompsa ifp->if_drv_flags |= IFF_DRV_RUNNING; 4853208019Sthompsa sc->cmdq_run = RUN_CMDQ_GO; 4854203134Sthompsa 4855209917Sthompsa for (i = 0; i != RUN_N_XFER; i++) 4856203134Sthompsa usbd_xfer_set_stall(sc->sc_xfer[i]); 4857203134Sthompsa 4858203134Sthompsa usbd_transfer_start(sc->sc_xfer[RUN_BULK_RX]); 4859203134Sthompsa 4860203134Sthompsa if (run_txrx_enable(sc) != 0) 4861203134Sthompsa goto fail; 4862203134Sthompsa 4863203134Sthompsa return; 4864203134Sthompsa 4865203134Sthompsafail: 4866203134Sthompsa run_stop(sc); 4867203134Sthompsa} 4868203134Sthompsa 4869203134Sthompsastatic void 4870203134Sthompsarun_init(void *arg) 4871203134Sthompsa{ 4872203134Sthompsa struct run_softc *sc = arg; 4873203134Sthompsa struct ifnet *ifp = sc->sc_ifp; 4874203134Sthompsa struct ieee80211com *ic = ifp->if_l2com; 4875203134Sthompsa 4876203134Sthompsa RUN_LOCK(sc); 4877203134Sthompsa run_init_locked(sc); 4878203134Sthompsa RUN_UNLOCK(sc); 4879203134Sthompsa 4880203134Sthompsa if (ifp->if_drv_flags & IFF_DRV_RUNNING) 4881208019Sthompsa ieee80211_start_all(ic); 4882203134Sthompsa} 4883203134Sthompsa 4884203134Sthompsastatic void 4885203134Sthompsarun_stop(void *arg) 4886203134Sthompsa{ 4887203134Sthompsa struct run_softc *sc = (struct run_softc *)arg; 4888203134Sthompsa struct ifnet *ifp = sc->sc_ifp; 4889203134Sthompsa uint32_t tmp; 4890203134Sthompsa int i; 4891203134Sthompsa int ntries; 4892203134Sthompsa 4893203134Sthompsa RUN_LOCK_ASSERT(sc, MA_OWNED); 4894203134Sthompsa 4895203134Sthompsa if (ifp->if_drv_flags & IFF_DRV_RUNNING) 4896203134Sthompsa run_set_leds(sc, 0); /* turn all LEDs off */ 4897203134Sthompsa 4898203134Sthompsa ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 4899203134Sthompsa 4900208019Sthompsa sc->ratectl_run = RUN_RATECTL_OFF; 4901209144Sthompsa sc->cmdq_run = sc->cmdq_key_set; 4902208019Sthompsa 4903203134Sthompsa RUN_UNLOCK(sc); 4904203134Sthompsa 4905203134Sthompsa for(i = 0; i < RUN_N_XFER; i++) 4906203134Sthompsa usbd_transfer_drain(sc->sc_xfer[i]); 4907203134Sthompsa 4908203134Sthompsa RUN_LOCK(sc); 4909203134Sthompsa 4910209917Sthompsa if (sc->rx_m != NULL) { 4911203134Sthompsa m_free(sc->rx_m); 4912203134Sthompsa sc->rx_m = NULL; 4913203134Sthompsa } 4914203134Sthompsa 4915203134Sthompsa /* disable Tx/Rx */ 4916203134Sthompsa run_read(sc, RT2860_MAC_SYS_CTRL, &tmp); 4917203134Sthompsa tmp &= ~(RT2860_MAC_RX_EN | RT2860_MAC_TX_EN); 4918203134Sthompsa run_write(sc, RT2860_MAC_SYS_CTRL, tmp); 4919203134Sthompsa 4920203134Sthompsa /* wait for pending Tx to complete */ 4921203134Sthompsa for (ntries = 0; ntries < 100; ntries++) { 4922209917Sthompsa if (run_read(sc, RT2860_TXRXQ_PCNT, &tmp) != 0) { 4923203134Sthompsa DPRINTF("Cannot read Tx queue count\n"); 4924203134Sthompsa break; 4925203134Sthompsa } 4926209917Sthompsa if ((tmp & RT2860_TX2Q_PCNT_MASK) == 0) { 4927203134Sthompsa DPRINTF("All Tx cleared\n"); 4928203134Sthompsa break; 4929203134Sthompsa } 4930203134Sthompsa run_delay(sc, 10); 4931203134Sthompsa } 4932209917Sthompsa if (ntries >= 100) 4933203134Sthompsa DPRINTF("There are still pending Tx\n"); 4934203134Sthompsa run_delay(sc, 10); 4935203134Sthompsa run_write(sc, RT2860_USB_DMA_CFG, 0); 4936203134Sthompsa 4937203134Sthompsa run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_BBP_HRST | RT2860_MAC_SRST); 4938203134Sthompsa run_write(sc, RT2860_MAC_SYS_CTRL, 0); 4939203134Sthompsa 4940203134Sthompsa for (i = 0; i != RUN_EP_QUEUES; i++) 4941203134Sthompsa run_unsetup_tx_list(sc, &sc->sc_epq[i]); 4942203134Sthompsa 4943203134Sthompsa return; 4944203134Sthompsa} 4945203134Sthompsa 4946203134Sthompsastatic void 4947203134Sthompsarun_delay(struct run_softc *sc, unsigned int ms) 4948203134Sthompsa{ 4949203134Sthompsa usb_pause_mtx(mtx_owned(&sc->sc_mtx) ? 4950203134Sthompsa &sc->sc_mtx : NULL, USB_MS_TO_TICKS(ms)); 4951203134Sthompsa} 4952203134Sthompsa 4953203134Sthompsastatic device_method_t run_methods[] = { 4954203134Sthompsa /* Device interface */ 4955203134Sthompsa DEVMETHOD(device_probe, run_match), 4956203134Sthompsa DEVMETHOD(device_attach, run_attach), 4957203134Sthompsa DEVMETHOD(device_detach, run_detach), 4958203134Sthompsa 4959203134Sthompsa { 0, 0 } 4960203134Sthompsa}; 4961203134Sthompsa 4962203134Sthompsastatic driver_t run_driver = { 4963233774Shselasky .name = "run", 4964233774Shselasky .methods = run_methods, 4965233774Shselasky .size = sizeof(struct run_softc) 4966203134Sthompsa}; 4967203134Sthompsa 4968203134Sthompsastatic devclass_t run_devclass; 4969203134Sthompsa 4970203134SthompsaDRIVER_MODULE(run, uhub, run_driver, run_devclass, NULL, 0); 4971212122SthompsaMODULE_DEPEND(run, wlan, 1, 1, 1); 4972212122SthompsaMODULE_DEPEND(run, usb, 1, 1, 1); 4973212122SthompsaMODULE_DEPEND(run, firmware, 1, 1, 1); 4974212122SthompsaMODULE_VERSION(run, 1); 4975