1 /*- 2 * Copyright (c) 2008,2010 Damien Bergamini <damien.bergamini@free.fr> 3 * ported to FreeBSD by Akinori Furukoshi <moonlightakkiy@yahoo.ca> 4 * USB Consulting, Hans Petter Selasky <hselasky@freebsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/cdefs.h> 20 __FBSDID("$FreeBSD$"); 21 22 /*- 23 * Ralink Technology RT2700U/RT2800U/RT3000U chipset driver. 24 * http://www.ralinktech.com/ 25 */ 26 27 #include <sys/param.h> 28 #include <sys/sockio.h> 29 #include <sys/sysctl.h> 30 #include <sys/lock.h> 31 #include <sys/mutex.h> 32 #include <sys/mbuf.h> 33 #include <sys/kernel.h> 34 #include <sys/socket.h> 35 #include <sys/systm.h> 36 #include <sys/malloc.h> 37 #include <sys/module.h> 38 #include <sys/bus.h> 39 #include <sys/endian.h> 40 #include <sys/linker.h> 41 #include <sys/firmware.h> 42 #include <sys/kdb.h> 43 44 #include <machine/bus.h> 45 #include <machine/resource.h> 46 #include <sys/rman.h> 47 48 #include <net/bpf.h> 49 #include <net/if.h> 50 #include <net/if_var.h> 51 #include <net/if_arp.h> 52 #include <net/ethernet.h> 53 #include <net/if_dl.h> 54 #include <net/if_media.h> 55 #include <net/if_types.h> 56 57 #include <netinet/in.h> 58 #include <netinet/in_systm.h> 59 #include <netinet/in_var.h> 60 #include <netinet/if_ether.h> 61 #include <netinet/ip.h> 62 63 #include <net80211/ieee80211_var.h> 64 #include <net80211/ieee80211_regdomain.h> 65 #include <net80211/ieee80211_radiotap.h> 66 #include <net80211/ieee80211_ratectl.h> 67 68 #include <dev/usb/usb.h> 69 #include <dev/usb/usbdi.h> 70 #include "usbdevs.h" 71 72 #define USB_DEBUG_VAR run_debug 73 #include <dev/usb/usb_debug.h> 74 75 #include <dev/usb/wlan/if_runreg.h> 76 #include <dev/usb/wlan/if_runvar.h> 77 78 #define N(_a) ((int)(sizeof((_a)) / sizeof((_a)[0]))) 79 80 #ifdef USB_DEBUG 81 #define RUN_DEBUG 82 #endif 83 84 #ifdef RUN_DEBUG 85 int run_debug = 0; 86 static SYSCTL_NODE(_hw_usb, OID_AUTO, run, CTLFLAG_RW, 0, "USB run"); 87 SYSCTL_INT(_hw_usb_run, OID_AUTO, debug, CTLFLAG_RW, &run_debug, 0, 88 "run debug level"); 89 #endif 90 91 #define IEEE80211_HAS_ADDR4(wh) \ 92 (((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS) 93 94 /* 95 * Because of LOR in run_key_delete(), use atomic instead. 96 * '& RUN_CMDQ_MASQ' is to loop cmdq[]. 97 */ 98 #define RUN_CMDQ_GET(c) (atomic_fetchadd_32((c), 1) & RUN_CMDQ_MASQ) 99 100 static const STRUCT_USB_HOST_ID run_devs[] = { 101 #define RUN_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) } 102 RUN_DEV(ABOCOM, RT2770), 103 RUN_DEV(ABOCOM, RT2870), 104 RUN_DEV(ABOCOM, RT3070), 105 RUN_DEV(ABOCOM, RT3071), 106 RUN_DEV(ABOCOM, RT3072), 107 RUN_DEV(ABOCOM2, RT2870_1), 108 RUN_DEV(ACCTON, RT2770), 109 RUN_DEV(ACCTON, RT2870_1), 110 RUN_DEV(ACCTON, RT2870_2), 111 RUN_DEV(ACCTON, RT2870_3), 112 RUN_DEV(ACCTON, RT2870_4), 113 RUN_DEV(ACCTON, RT2870_5), 114 RUN_DEV(ACCTON, RT3070), 115 RUN_DEV(ACCTON, RT3070_1), 116 RUN_DEV(ACCTON, RT3070_2), 117 RUN_DEV(ACCTON, RT3070_3), 118 RUN_DEV(ACCTON, RT3070_4), 119 RUN_DEV(ACCTON, RT3070_5), 120 RUN_DEV(AIRTIES, RT3070), 121 RUN_DEV(ALLWIN, RT2070), 122 RUN_DEV(ALLWIN, RT2770), 123 RUN_DEV(ALLWIN, RT2870), 124 RUN_DEV(ALLWIN, RT3070), 125 RUN_DEV(ALLWIN, RT3071), 126 RUN_DEV(ALLWIN, RT3072), 127 RUN_DEV(ALLWIN, RT3572), 128 RUN_DEV(AMIGO, RT2870_1), 129 RUN_DEV(AMIGO, RT2870_2), 130 RUN_DEV(AMIT, CGWLUSB2GNR), 131 RUN_DEV(AMIT, RT2870_1), 132 RUN_DEV(AMIT2, RT2870), 133 RUN_DEV(ASUS, RT2870_1), 134 RUN_DEV(ASUS, RT2870_2), 135 RUN_DEV(ASUS, RT2870_3), 136 RUN_DEV(ASUS, RT2870_4), 137 RUN_DEV(ASUS, RT2870_5), 138 RUN_DEV(ASUS, USBN13), 139 RUN_DEV(ASUS, RT3070_1), 140 RUN_DEV(ASUS, USB_N53), 141 RUN_DEV(ASUS2, USBN11), 142 RUN_DEV(AZUREWAVE, RT2870_1), 143 RUN_DEV(AZUREWAVE, RT2870_2), 144 RUN_DEV(AZUREWAVE, RT3070_1), 145 RUN_DEV(AZUREWAVE, RT3070_2), 146 RUN_DEV(AZUREWAVE, RT3070_3), 147 RUN_DEV(BELKIN, F5D8053V3), 148 RUN_DEV(BELKIN, F5D8055), 149 RUN_DEV(BELKIN, F5D8055V2), 150 RUN_DEV(BELKIN, F6D4050V1), 151 RUN_DEV(BELKIN, F6D4050V2), 152 RUN_DEV(BELKIN, RT2870_1), 153 RUN_DEV(BELKIN, RT2870_2), 154 RUN_DEV(CISCOLINKSYS, AE1000), 155 RUN_DEV(CISCOLINKSYS2, RT3070), 156 RUN_DEV(CISCOLINKSYS3, RT3070), 157 RUN_DEV(CONCEPTRONIC2, RT2870_1), 158 RUN_DEV(CONCEPTRONIC2, RT2870_2), 159 RUN_DEV(CONCEPTRONIC2, RT2870_3), 160 RUN_DEV(CONCEPTRONIC2, RT2870_4), 161 RUN_DEV(CONCEPTRONIC2, RT2870_5), 162 RUN_DEV(CONCEPTRONIC2, RT2870_6), 163 RUN_DEV(CONCEPTRONIC2, RT2870_7), 164 RUN_DEV(CONCEPTRONIC2, RT2870_8), 165 RUN_DEV(CONCEPTRONIC2, RT3070_1), 166 RUN_DEV(CONCEPTRONIC2, RT3070_2), 167 RUN_DEV(CONCEPTRONIC2, VIGORN61), 168 RUN_DEV(COREGA, CGWLUSB300GNM), 169 RUN_DEV(COREGA, RT2870_1), 170 RUN_DEV(COREGA, RT2870_2), 171 RUN_DEV(COREGA, RT2870_3), 172 RUN_DEV(COREGA, RT3070), 173 RUN_DEV(CYBERTAN, RT2870), 174 RUN_DEV(DLINK, RT2870), 175 RUN_DEV(DLINK, RT3072), 176 RUN_DEV(DLINK, DWA127), 177 RUN_DEV(DLINK2, DWA130), 178 RUN_DEV(DLINK2, RT2870_1), 179 RUN_DEV(DLINK2, RT2870_2), 180 RUN_DEV(DLINK2, RT3070_1), 181 RUN_DEV(DLINK2, RT3070_2), 182 RUN_DEV(DLINK2, RT3070_3), 183 RUN_DEV(DLINK2, RT3070_4), 184 RUN_DEV(DLINK2, RT3070_5), 185 RUN_DEV(DLINK2, RT3072), 186 RUN_DEV(DLINK2, RT3072_1), 187 RUN_DEV(EDIMAX, EW7717), 188 RUN_DEV(EDIMAX, EW7718), 189 RUN_DEV(EDIMAX, RT2870_1), 190 RUN_DEV(ENCORE, RT3070_1), 191 RUN_DEV(ENCORE, RT3070_2), 192 RUN_DEV(ENCORE, RT3070_3), 193 RUN_DEV(GIGABYTE, GNWB31N), 194 RUN_DEV(GIGABYTE, GNWB32L), 195 RUN_DEV(GIGABYTE, RT2870_1), 196 RUN_DEV(GIGASET, RT3070_1), 197 RUN_DEV(GIGASET, RT3070_2), 198 RUN_DEV(GUILLEMOT, HWNU300), 199 RUN_DEV(HAWKING, HWUN2), 200 RUN_DEV(HAWKING, RT2870_1), 201 RUN_DEV(HAWKING, RT2870_2), 202 RUN_DEV(HAWKING, RT3070), 203 RUN_DEV(IODATA, RT3072_1), 204 RUN_DEV(IODATA, RT3072_2), 205 RUN_DEV(IODATA, RT3072_3), 206 RUN_DEV(IODATA, RT3072_4), 207 RUN_DEV(LINKSYS4, RT3070), 208 RUN_DEV(LINKSYS4, WUSB100), 209 RUN_DEV(LINKSYS4, WUSB54GCV3), 210 RUN_DEV(LINKSYS4, WUSB600N), 211 RUN_DEV(LINKSYS4, WUSB600NV2), 212 RUN_DEV(LOGITEC, RT2870_1), 213 RUN_DEV(LOGITEC, RT2870_2), 214 RUN_DEV(LOGITEC, RT2870_3), 215 RUN_DEV(LOGITEC, LANW300NU2), 216 RUN_DEV(LOGITEC, LANW150NU2), 217 RUN_DEV(LOGITEC, LANW300NU2S), 218 RUN_DEV(MELCO, RT2870_1), 219 RUN_DEV(MELCO, RT2870_2), 220 RUN_DEV(MELCO, WLIUCAG300N), 221 RUN_DEV(MELCO, WLIUCG300N), 222 RUN_DEV(MELCO, WLIUCG301N), 223 RUN_DEV(MELCO, WLIUCGN), 224 RUN_DEV(MELCO, WLIUCGNM), 225 RUN_DEV(MELCO, WLIUCGNM2), 226 RUN_DEV(MOTOROLA4, RT2770), 227 RUN_DEV(MOTOROLA4, RT3070), 228 RUN_DEV(MSI, RT3070_1), 229 RUN_DEV(MSI, RT3070_2), 230 RUN_DEV(MSI, RT3070_3), 231 RUN_DEV(MSI, RT3070_4), 232 RUN_DEV(MSI, RT3070_5), 233 RUN_DEV(MSI, RT3070_6), 234 RUN_DEV(MSI, RT3070_7), 235 RUN_DEV(MSI, RT3070_8), 236 RUN_DEV(MSI, RT3070_9), 237 RUN_DEV(MSI, RT3070_10), 238 RUN_DEV(MSI, RT3070_11), 239 RUN_DEV(OVISLINK, RT3072), 240 RUN_DEV(PARA, RT3070), 241 RUN_DEV(PEGATRON, RT2870), 242 RUN_DEV(PEGATRON, RT3070), 243 RUN_DEV(PEGATRON, RT3070_2), 244 RUN_DEV(PEGATRON, RT3070_3), 245 RUN_DEV(PHILIPS, RT2870), 246 RUN_DEV(PLANEX2, GWUS300MINIS), 247 RUN_DEV(PLANEX2, GWUSMICRON), 248 RUN_DEV(PLANEX2, RT2870), 249 RUN_DEV(PLANEX2, RT3070), 250 RUN_DEV(QCOM, RT2870), 251 RUN_DEV(QUANTA, RT3070), 252 RUN_DEV(RALINK, RT2070), 253 RUN_DEV(RALINK, RT2770), 254 RUN_DEV(RALINK, RT2870), 255 RUN_DEV(RALINK, RT3070), 256 RUN_DEV(RALINK, RT3071), 257 RUN_DEV(RALINK, RT3072), 258 RUN_DEV(RALINK, RT3370), 259 RUN_DEV(RALINK, RT3572), 260 RUN_DEV(RALINK, RT8070), 261 RUN_DEV(SAMSUNG, WIS09ABGN), 262 RUN_DEV(SAMSUNG2, RT2870_1), 263 RUN_DEV(SENAO, RT2870_1), 264 RUN_DEV(SENAO, RT2870_2), 265 RUN_DEV(SENAO, RT2870_3), 266 RUN_DEV(SENAO, RT2870_4), 267 RUN_DEV(SENAO, RT3070), 268 RUN_DEV(SENAO, RT3071), 269 RUN_DEV(SENAO, RT3072_1), 270 RUN_DEV(SENAO, RT3072_2), 271 RUN_DEV(SENAO, RT3072_3), 272 RUN_DEV(SENAO, RT3072_4), 273 RUN_DEV(SENAO, RT3072_5), 274 RUN_DEV(SITECOMEU, RT2770), 275 RUN_DEV(SITECOMEU, RT2870_1), 276 RUN_DEV(SITECOMEU, RT2870_2), 277 RUN_DEV(SITECOMEU, RT2870_3), 278 RUN_DEV(SITECOMEU, RT2870_4), 279 RUN_DEV(SITECOMEU, RT3070), 280 RUN_DEV(SITECOMEU, RT3070_2), 281 RUN_DEV(SITECOMEU, RT3070_3), 282 RUN_DEV(SITECOMEU, RT3070_4), 283 RUN_DEV(SITECOMEU, RT3071), 284 RUN_DEV(SITECOMEU, RT3072_1), 285 RUN_DEV(SITECOMEU, RT3072_2), 286 RUN_DEV(SITECOMEU, RT3072_3), 287 RUN_DEV(SITECOMEU, RT3072_4), 288 RUN_DEV(SITECOMEU, RT3072_5), 289 RUN_DEV(SITECOMEU, RT3072_6), 290 RUN_DEV(SITECOMEU, WL608), 291 RUN_DEV(SPARKLAN, RT2870_1), 292 RUN_DEV(SPARKLAN, RT3070), 293 RUN_DEV(SWEEX2, LW153), 294 RUN_DEV(SWEEX2, LW303), 295 RUN_DEV(SWEEX2, LW313), 296 RUN_DEV(TOSHIBA, RT3070), 297 RUN_DEV(UMEDIA, RT2870_1), 298 RUN_DEV(ZCOM, RT2870_1), 299 RUN_DEV(ZCOM, RT2870_2), 300 RUN_DEV(ZINWELL, RT2870_1), 301 RUN_DEV(ZINWELL, RT2870_2), 302 RUN_DEV(ZINWELL, RT3070), 303 RUN_DEV(ZINWELL, RT3072_1), 304 RUN_DEV(ZINWELL, RT3072_2), 305 RUN_DEV(ZYXEL, RT2870_1), 306 RUN_DEV(ZYXEL, RT2870_2), 307 #undef RUN_DEV 308 }; 309 310 static device_probe_t run_match; 311 static device_attach_t run_attach; 312 static device_detach_t run_detach; 313 314 static usb_callback_t run_bulk_rx_callback; 315 static usb_callback_t run_bulk_tx_callback0; 316 static usb_callback_t run_bulk_tx_callback1; 317 static usb_callback_t run_bulk_tx_callback2; 318 static usb_callback_t run_bulk_tx_callback3; 319 static usb_callback_t run_bulk_tx_callback4; 320 static usb_callback_t run_bulk_tx_callback5; 321 322 static void run_bulk_tx_callbackN(struct usb_xfer *xfer, 323 usb_error_t error, unsigned int index); 324 static struct ieee80211vap *run_vap_create(struct ieee80211com *, 325 const char [IFNAMSIZ], int, enum ieee80211_opmode, int, 326 const uint8_t [IEEE80211_ADDR_LEN], 327 const uint8_t [IEEE80211_ADDR_LEN]); 328 static void run_vap_delete(struct ieee80211vap *); 329 static void run_cmdq_cb(void *, int); 330 static void run_setup_tx_list(struct run_softc *, 331 struct run_endpoint_queue *); 332 static void run_unsetup_tx_list(struct run_softc *, 333 struct run_endpoint_queue *); 334 static int run_load_microcode(struct run_softc *); 335 static int run_reset(struct run_softc *); 336 static usb_error_t run_do_request(struct run_softc *, 337 struct usb_device_request *, void *); 338 static int run_read(struct run_softc *, uint16_t, uint32_t *); 339 static int run_read_region_1(struct run_softc *, uint16_t, uint8_t *, int); 340 static int run_write_2(struct run_softc *, uint16_t, uint16_t); 341 static int run_write(struct run_softc *, uint16_t, uint32_t); 342 static int run_write_region_1(struct run_softc *, uint16_t, 343 const uint8_t *, int); 344 static int run_set_region_4(struct run_softc *, uint16_t, uint32_t, int); 345 static int run_efuse_read_2(struct run_softc *, uint16_t, uint16_t *); 346 static int run_eeprom_read_2(struct run_softc *, uint16_t, uint16_t *); 347 static int run_rt2870_rf_write(struct run_softc *, uint8_t, uint32_t); 348 static int run_rt3070_rf_read(struct run_softc *, uint8_t, uint8_t *); 349 static int run_rt3070_rf_write(struct run_softc *, uint8_t, uint8_t); 350 static int run_bbp_read(struct run_softc *, uint8_t, uint8_t *); 351 static int run_bbp_write(struct run_softc *, uint8_t, uint8_t); 352 static int run_mcu_cmd(struct run_softc *, uint8_t, uint16_t); 353 static const char *run_get_rf(int); 354 static int run_read_eeprom(struct run_softc *); 355 static struct ieee80211_node *run_node_alloc(struct ieee80211vap *, 356 const uint8_t mac[IEEE80211_ADDR_LEN]); 357 static int run_media_change(struct ifnet *); 358 static int run_newstate(struct ieee80211vap *, enum ieee80211_state, int); 359 static int run_wme_update(struct ieee80211com *); 360 static void run_wme_update_cb(void *); 361 static void run_key_update_begin(struct ieee80211vap *); 362 static void run_key_update_end(struct ieee80211vap *); 363 static void run_key_set_cb(void *); 364 static int run_key_set(struct ieee80211vap *, struct ieee80211_key *, 365 const uint8_t mac[IEEE80211_ADDR_LEN]); 366 static void run_key_delete_cb(void *); 367 static int run_key_delete(struct ieee80211vap *, struct ieee80211_key *); 368 static void run_ratectl_to(void *); 369 static void run_ratectl_cb(void *, int); 370 static void run_drain_fifo(void *); 371 static void run_iter_func(void *, struct ieee80211_node *); 372 static void run_newassoc_cb(void *); 373 static void run_newassoc(struct ieee80211_node *, int); 374 static void run_rx_frame(struct run_softc *, struct mbuf *, uint32_t); 375 static void run_tx_free(struct run_endpoint_queue *pq, 376 struct run_tx_data *, int); 377 static void run_set_tx_desc(struct run_softc *, struct run_tx_data *); 378 static int run_tx(struct run_softc *, struct mbuf *, 379 struct ieee80211_node *); 380 static int run_tx_mgt(struct run_softc *, struct mbuf *, 381 struct ieee80211_node *); 382 static int run_sendprot(struct run_softc *, const struct mbuf *, 383 struct ieee80211_node *, int, int); 384 static int run_tx_param(struct run_softc *, struct mbuf *, 385 struct ieee80211_node *, 386 const struct ieee80211_bpf_params *); 387 static int run_raw_xmit(struct ieee80211_node *, struct mbuf *, 388 const struct ieee80211_bpf_params *); 389 static void run_start(struct ifnet *); 390 static int run_ioctl(struct ifnet *, u_long, caddr_t); 391 static void run_set_agc(struct run_softc *, uint8_t); 392 static void run_select_chan_group(struct run_softc *, int); 393 static void run_set_rx_antenna(struct run_softc *, int); 394 static void run_rt2870_set_chan(struct run_softc *, u_int); 395 static void run_rt3070_set_chan(struct run_softc *, u_int); 396 static void run_rt3572_set_chan(struct run_softc *, u_int); 397 static int run_set_chan(struct run_softc *, struct ieee80211_channel *); 398 static void run_set_channel(struct ieee80211com *); 399 static void run_scan_start(struct ieee80211com *); 400 static void run_scan_end(struct ieee80211com *); 401 static void run_update_beacon(struct ieee80211vap *, int); 402 static void run_update_beacon_cb(void *); 403 static void run_updateprot(struct ieee80211com *); 404 static void run_updateprot_cb(void *); 405 static void run_usb_timeout_cb(void *); 406 static void run_reset_livelock(struct run_softc *); 407 static void run_enable_tsf_sync(struct run_softc *); 408 static void run_enable_mrr(struct run_softc *); 409 static void run_set_txpreamble(struct run_softc *); 410 static void run_set_basicrates(struct run_softc *); 411 static void run_set_leds(struct run_softc *, uint16_t); 412 static void run_set_bssid(struct run_softc *, const uint8_t *); 413 static void run_set_macaddr(struct run_softc *, const uint8_t *); 414 static void run_updateslot(struct ifnet *); 415 static void run_updateslot_cb(void *); 416 static void run_update_mcast(struct ifnet *); 417 static int8_t run_rssi2dbm(struct run_softc *, uint8_t, uint8_t); 418 static void run_update_promisc_locked(struct ifnet *); 419 static void run_update_promisc(struct ifnet *); 420 static int run_bbp_init(struct run_softc *); 421 static int run_rt3070_rf_init(struct run_softc *); 422 static int run_rt3070_filter_calib(struct run_softc *, uint8_t, uint8_t, 423 uint8_t *); 424 static void run_rt3070_rf_setup(struct run_softc *); 425 static int run_txrx_enable(struct run_softc *); 426 static void run_init(void *); 427 static void run_init_locked(struct run_softc *); 428 static void run_stop(void *); 429 static void run_delay(struct run_softc *, unsigned int); 430 431 static const struct { 432 uint16_t reg; 433 uint32_t val; 434 } rt2870_def_mac[] = { 435 RT2870_DEF_MAC 436 }; 437 438 static const struct { 439 uint8_t reg; 440 uint8_t val; 441 } rt2860_def_bbp[] = { 442 RT2860_DEF_BBP 443 }; 444 445 static const struct rfprog { 446 uint8_t chan; 447 uint32_t r1, r2, r3, r4; 448 } rt2860_rf2850[] = { 449 RT2860_RF2850 450 }; 451 452 struct { 453 uint8_t n, r, k; 454 } rt3070_freqs[] = { 455 RT3070_RF3052 456 }; 457 458 static const struct { 459 uint8_t reg; 460 uint8_t val; 461 } rt3070_def_rf[] = { 462 RT3070_DEF_RF 463 },rt3572_def_rf[] = { 464 RT3572_DEF_RF 465 }; 466 467 static const struct usb_config run_config[RUN_N_XFER] = { 468 [RUN_BULK_TX_BE] = { 469 .type = UE_BULK, 470 .endpoint = UE_ADDR_ANY, 471 .ep_index = 0, 472 .direction = UE_DIR_OUT, 473 .bufsize = RUN_MAX_TXSZ, 474 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 475 .callback = run_bulk_tx_callback0, 476 .timeout = 5000, /* ms */ 477 }, 478 [RUN_BULK_TX_BK] = { 479 .type = UE_BULK, 480 .endpoint = UE_ADDR_ANY, 481 .direction = UE_DIR_OUT, 482 .ep_index = 1, 483 .bufsize = RUN_MAX_TXSZ, 484 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 485 .callback = run_bulk_tx_callback1, 486 .timeout = 5000, /* ms */ 487 }, 488 [RUN_BULK_TX_VI] = { 489 .type = UE_BULK, 490 .endpoint = UE_ADDR_ANY, 491 .direction = UE_DIR_OUT, 492 .ep_index = 2, 493 .bufsize = RUN_MAX_TXSZ, 494 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 495 .callback = run_bulk_tx_callback2, 496 .timeout = 5000, /* ms */ 497 }, 498 [RUN_BULK_TX_VO] = { 499 .type = UE_BULK, 500 .endpoint = UE_ADDR_ANY, 501 .direction = UE_DIR_OUT, 502 .ep_index = 3, 503 .bufsize = RUN_MAX_TXSZ, 504 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 505 .callback = run_bulk_tx_callback3, 506 .timeout = 5000, /* ms */ 507 }, 508 [RUN_BULK_TX_HCCA] = { 509 .type = UE_BULK, 510 .endpoint = UE_ADDR_ANY, 511 .direction = UE_DIR_OUT, 512 .ep_index = 4, 513 .bufsize = RUN_MAX_TXSZ, 514 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,}, 515 .callback = run_bulk_tx_callback4, 516 .timeout = 5000, /* ms */ 517 }, 518 [RUN_BULK_TX_PRIO] = { 519 .type = UE_BULK, 520 .endpoint = UE_ADDR_ANY, 521 .direction = UE_DIR_OUT, 522 .ep_index = 5, 523 .bufsize = RUN_MAX_TXSZ, 524 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,}, 525 .callback = run_bulk_tx_callback5, 526 .timeout = 5000, /* ms */ 527 }, 528 [RUN_BULK_RX] = { 529 .type = UE_BULK, 530 .endpoint = UE_ADDR_ANY, 531 .direction = UE_DIR_IN, 532 .bufsize = RUN_MAX_RXSZ, 533 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 534 .callback = run_bulk_rx_callback, 535 } 536 }; 537 538 static int 539 run_match(device_t self) 540 { 541 struct usb_attach_arg *uaa = device_get_ivars(self); 542 543 if (uaa->usb_mode != USB_MODE_HOST) 544 return (ENXIO); 545 if (uaa->info.bConfigIndex != 0) 546 return (ENXIO); 547 if (uaa->info.bIfaceIndex != RT2860_IFACE_INDEX) 548 return (ENXIO); 549 550 return (usbd_lookup_id_by_uaa(run_devs, sizeof(run_devs), uaa)); 551 } 552 553 static int 554 run_attach(device_t self) 555 { 556 struct run_softc *sc = device_get_softc(self); 557 struct usb_attach_arg *uaa = device_get_ivars(self); 558 struct ieee80211com *ic; 559 struct ifnet *ifp; 560 uint32_t ver; 561 int i, ntries, error; 562 uint8_t iface_index, bands; 563 564 device_set_usb_desc(self); 565 sc->sc_udev = uaa->device; 566 sc->sc_dev = self; 567 568 mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), 569 MTX_NETWORK_LOCK, MTX_DEF); 570 571 iface_index = RT2860_IFACE_INDEX; 572 573 error = usbd_transfer_setup(uaa->device, &iface_index, 574 sc->sc_xfer, run_config, RUN_N_XFER, sc, &sc->sc_mtx); 575 if (error) { 576 device_printf(self, "could not allocate USB transfers, " 577 "err=%s\n", usbd_errstr(error)); 578 goto detach; 579 } 580 581 RUN_LOCK(sc); 582 583 /* wait for the chip to settle */ 584 for (ntries = 0; ntries < 100; ntries++) { 585 if (run_read(sc, RT2860_ASIC_VER_ID, &ver) != 0) { 586 RUN_UNLOCK(sc); 587 goto detach; 588 } 589 if (ver != 0 && ver != 0xffffffff) 590 break; 591 run_delay(sc, 10); 592 } 593 if (ntries == 100) { 594 device_printf(sc->sc_dev, 595 "timeout waiting for NIC to initialize\n"); 596 RUN_UNLOCK(sc); 597 goto detach; 598 } 599 sc->mac_ver = ver >> 16; 600 sc->mac_rev = ver & 0xffff; 601 602 /* retrieve RF rev. no and various other things from EEPROM */ 603 run_read_eeprom(sc); 604 605 device_printf(sc->sc_dev, 606 "MAC/BBP RT%04X (rev 0x%04X), RF %s (MIMO %dT%dR), address %s\n", 607 sc->mac_ver, sc->mac_rev, run_get_rf(sc->rf_rev), 608 sc->ntxchains, sc->nrxchains, ether_sprintf(sc->sc_bssid)); 609 610 RUN_UNLOCK(sc); 611 612 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 613 if (ifp == NULL) { 614 device_printf(sc->sc_dev, "can not if_alloc()\n"); 615 goto detach; 616 } 617 ic = ifp->if_l2com; 618 619 ifp->if_softc = sc; 620 if_initname(ifp, "run", device_get_unit(sc->sc_dev)); 621 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 622 ifp->if_init = run_init; 623 ifp->if_ioctl = run_ioctl; 624 ifp->if_start = run_start; 625 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 626 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 627 IFQ_SET_READY(&ifp->if_snd); 628 629 ic->ic_ifp = ifp; 630 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 631 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 632 633 /* set device capabilities */ 634 ic->ic_caps = 635 IEEE80211_C_STA | /* station mode supported */ 636 IEEE80211_C_MONITOR | /* monitor mode supported */ 637 IEEE80211_C_IBSS | 638 IEEE80211_C_HOSTAP | 639 IEEE80211_C_WDS | /* 4-address traffic works */ 640 IEEE80211_C_MBSS | 641 IEEE80211_C_SHPREAMBLE | /* short preamble supported */ 642 IEEE80211_C_SHSLOT | /* short slot time supported */ 643 IEEE80211_C_WME | /* WME */ 644 IEEE80211_C_WPA; /* WPA1|WPA2(RSN) */ 645 646 ic->ic_cryptocaps = 647 IEEE80211_CRYPTO_WEP | 648 IEEE80211_CRYPTO_AES_CCM | 649 IEEE80211_CRYPTO_TKIPMIC | 650 IEEE80211_CRYPTO_TKIP; 651 652 ic->ic_flags |= IEEE80211_F_DATAPAD; 653 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS; 654 655 bands = 0; 656 setbit(&bands, IEEE80211_MODE_11B); 657 setbit(&bands, IEEE80211_MODE_11G); 658 ieee80211_init_channels(ic, NULL, &bands); 659 660 /* 661 * Do this by own because h/w supports 662 * more channels than ieee80211_init_channels() 663 */ 664 if (sc->rf_rev == RT2860_RF_2750 || 665 sc->rf_rev == RT2860_RF_2850 || 666 sc->rf_rev == RT3070_RF_3052) { 667 /* set supported .11a rates */ 668 for (i = 14; i < N(rt2860_rf2850); i++) { 669 uint8_t chan = rt2860_rf2850[i].chan; 670 ic->ic_channels[ic->ic_nchans].ic_freq = 671 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_A); 672 ic->ic_channels[ic->ic_nchans].ic_ieee = chan; 673 ic->ic_channels[ic->ic_nchans].ic_flags = IEEE80211_CHAN_A; 674 ic->ic_channels[ic->ic_nchans].ic_extieee = 0; 675 ic->ic_nchans++; 676 } 677 } 678 679 ieee80211_ifattach(ic, sc->sc_bssid); 680 681 ic->ic_scan_start = run_scan_start; 682 ic->ic_scan_end = run_scan_end; 683 ic->ic_set_channel = run_set_channel; 684 ic->ic_node_alloc = run_node_alloc; 685 ic->ic_newassoc = run_newassoc; 686 ic->ic_updateslot = run_updateslot; 687 ic->ic_update_mcast = run_update_mcast; 688 ic->ic_wme.wme_update = run_wme_update; 689 ic->ic_raw_xmit = run_raw_xmit; 690 ic->ic_update_promisc = run_update_promisc; 691 692 ic->ic_vap_create = run_vap_create; 693 ic->ic_vap_delete = run_vap_delete; 694 695 ieee80211_radiotap_attach(ic, 696 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap), 697 RUN_TX_RADIOTAP_PRESENT, 698 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 699 RUN_RX_RADIOTAP_PRESENT); 700 701 TASK_INIT(&sc->cmdq_task, 0, run_cmdq_cb, sc); 702 TASK_INIT(&sc->ratectl_task, 0, run_ratectl_cb, sc); 703 callout_init((struct callout *)&sc->ratectl_ch, 1); 704 705 if (bootverbose) 706 ieee80211_announce(ic); 707 708 return (0); 709 710 detach: 711 run_detach(self); 712 return (ENXIO); 713 } 714 715 static int 716 run_detach(device_t self) 717 { 718 struct run_softc *sc = device_get_softc(self); 719 struct ifnet *ifp = sc->sc_ifp; 720 struct ieee80211com *ic; 721 int i; 722 723 RUN_LOCK(sc); 724 sc->sc_detached = 1; 725 RUN_UNLOCK(sc); 726 727 /* stop all USB transfers */ 728 usbd_transfer_unsetup(sc->sc_xfer, RUN_N_XFER); 729 730 RUN_LOCK(sc); 731 sc->ratectl_run = RUN_RATECTL_OFF; 732 sc->cmdq_run = sc->cmdq_key_set = RUN_CMDQ_ABORT; 733 734 /* free TX list, if any */ 735 for (i = 0; i != RUN_EP_QUEUES; i++) 736 run_unsetup_tx_list(sc, &sc->sc_epq[i]); 737 RUN_UNLOCK(sc); 738 739 if (ifp) { 740 ic = ifp->if_l2com; 741 /* drain tasks */ 742 usb_callout_drain(&sc->ratectl_ch); 743 ieee80211_draintask(ic, &sc->cmdq_task); 744 ieee80211_draintask(ic, &sc->ratectl_task); 745 ieee80211_ifdetach(ic); 746 if_free(ifp); 747 } 748 749 mtx_destroy(&sc->sc_mtx); 750 751 return (0); 752 } 753 754 static struct ieee80211vap * 755 run_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, 756 enum ieee80211_opmode opmode, int flags, 757 const uint8_t bssid[IEEE80211_ADDR_LEN], 758 const uint8_t mac[IEEE80211_ADDR_LEN]) 759 { 760 struct ifnet *ifp = ic->ic_ifp; 761 struct run_softc *sc = ifp->if_softc; 762 struct run_vap *rvp; 763 struct ieee80211vap *vap; 764 int i; 765 766 if (sc->rvp_cnt >= RUN_VAP_MAX) { 767 if_printf(ifp, "number of VAPs maxed out\n"); 768 return (NULL); 769 } 770 771 switch (opmode) { 772 case IEEE80211_M_STA: 773 /* enable s/w bmiss handling for sta mode */ 774 flags |= IEEE80211_CLONE_NOBEACONS; 775 /* fall though */ 776 case IEEE80211_M_IBSS: 777 case IEEE80211_M_MONITOR: 778 case IEEE80211_M_HOSTAP: 779 case IEEE80211_M_MBSS: 780 /* other than WDS vaps, only one at a time */ 781 if (!TAILQ_EMPTY(&ic->ic_vaps)) 782 return (NULL); 783 break; 784 case IEEE80211_M_WDS: 785 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next){ 786 if(vap->iv_opmode != IEEE80211_M_HOSTAP) 787 continue; 788 /* WDS vap's always share the local mac address. */ 789 flags &= ~IEEE80211_CLONE_BSSID; 790 break; 791 } 792 if (vap == NULL) { 793 if_printf(ifp, "wds only supported in ap mode\n"); 794 return (NULL); 795 } 796 break; 797 default: 798 if_printf(ifp, "unknown opmode %d\n", opmode); 799 return (NULL); 800 } 801 802 rvp = (struct run_vap *) malloc(sizeof(struct run_vap), 803 M_80211_VAP, M_NOWAIT | M_ZERO); 804 if (rvp == NULL) 805 return (NULL); 806 vap = &rvp->vap; 807 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac); 808 809 vap->iv_key_update_begin = run_key_update_begin; 810 vap->iv_key_update_end = run_key_update_end; 811 vap->iv_update_beacon = run_update_beacon; 812 vap->iv_max_aid = RT2870_WCID_MAX; 813 /* 814 * To delete the right key from h/w, we need wcid. 815 * Luckily, there is unused space in ieee80211_key{}, wk_pad, 816 * and matching wcid will be written into there. So, cast 817 * some spells to remove 'const' from ieee80211_key{} 818 */ 819 vap->iv_key_delete = (void *)run_key_delete; 820 vap->iv_key_set = (void *)run_key_set; 821 822 /* override state transition machine */ 823 rvp->newstate = vap->iv_newstate; 824 vap->iv_newstate = run_newstate; 825 826 ieee80211_ratectl_init(vap); 827 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */); 828 829 /* complete setup */ 830 ieee80211_vap_attach(vap, run_media_change, ieee80211_media_status); 831 832 /* make sure id is always unique */ 833 for (i = 0; i < RUN_VAP_MAX; i++) { 834 if((sc->rvp_bmap & 1 << i) == 0){ 835 sc->rvp_bmap |= 1 << i; 836 rvp->rvp_id = i; 837 break; 838 } 839 } 840 if (sc->rvp_cnt++ == 0) 841 ic->ic_opmode = opmode; 842 843 if (opmode == IEEE80211_M_HOSTAP) 844 sc->cmdq_run = RUN_CMDQ_GO; 845 846 DPRINTF("rvp_id=%d bmap=%x rvp_cnt=%d\n", 847 rvp->rvp_id, sc->rvp_bmap, sc->rvp_cnt); 848 849 return (vap); 850 } 851 852 static void 853 run_vap_delete(struct ieee80211vap *vap) 854 { 855 struct run_vap *rvp = RUN_VAP(vap); 856 struct ifnet *ifp; 857 struct ieee80211com *ic; 858 struct run_softc *sc; 859 uint8_t rvp_id; 860 861 if (vap == NULL) 862 return; 863 864 ic = vap->iv_ic; 865 ifp = ic->ic_ifp; 866 867 sc = ifp->if_softc; 868 869 RUN_LOCK(sc); 870 871 m_freem(rvp->beacon_mbuf); 872 rvp->beacon_mbuf = NULL; 873 874 rvp_id = rvp->rvp_id; 875 sc->ratectl_run &= ~(1 << rvp_id); 876 sc->rvp_bmap &= ~(1 << rvp_id); 877 run_set_region_4(sc, RT2860_SKEY(rvp_id, 0), 0, 128); 878 run_set_region_4(sc, RT2860_BCN_BASE(rvp_id), 0, 512); 879 --sc->rvp_cnt; 880 881 DPRINTF("vap=%p rvp_id=%d bmap=%x rvp_cnt=%d\n", 882 vap, rvp_id, sc->rvp_bmap, sc->rvp_cnt); 883 884 RUN_UNLOCK(sc); 885 886 ieee80211_ratectl_deinit(vap); 887 ieee80211_vap_detach(vap); 888 free(rvp, M_80211_VAP); 889 } 890 891 /* 892 * There are numbers of functions need to be called in context thread. 893 * Rather than creating taskqueue event for each of those functions, 894 * here is all-for-one taskqueue callback function. This function 895 * gurantees deferred functions are executed in the same order they 896 * were enqueued. 897 * '& RUN_CMDQ_MASQ' is to loop cmdq[]. 898 */ 899 static void 900 run_cmdq_cb(void *arg, int pending) 901 { 902 struct run_softc *sc = arg; 903 uint8_t i; 904 905 /* call cmdq[].func locked */ 906 RUN_LOCK(sc); 907 for (i = sc->cmdq_exec; sc->cmdq[i].func && pending; 908 i = sc->cmdq_exec, pending--) { 909 DPRINTFN(6, "cmdq_exec=%d pending=%d\n", i, pending); 910 if (sc->cmdq_run == RUN_CMDQ_GO) { 911 /* 912 * If arg0 is NULL, callback func needs more 913 * than one arg. So, pass ptr to cmdq struct. 914 */ 915 if (sc->cmdq[i].arg0) 916 sc->cmdq[i].func(sc->cmdq[i].arg0); 917 else 918 sc->cmdq[i].func(&sc->cmdq[i]); 919 } 920 sc->cmdq[i].arg0 = NULL; 921 sc->cmdq[i].func = NULL; 922 sc->cmdq_exec++; 923 sc->cmdq_exec &= RUN_CMDQ_MASQ; 924 } 925 RUN_UNLOCK(sc); 926 } 927 928 static void 929 run_setup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq) 930 { 931 struct run_tx_data *data; 932 933 memset(pq, 0, sizeof(*pq)); 934 935 STAILQ_INIT(&pq->tx_qh); 936 STAILQ_INIT(&pq->tx_fh); 937 938 for (data = &pq->tx_data[0]; 939 data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) { 940 data->sc = sc; 941 STAILQ_INSERT_TAIL(&pq->tx_fh, data, next); 942 } 943 pq->tx_nfree = RUN_TX_RING_COUNT; 944 } 945 946 static void 947 run_unsetup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq) 948 { 949 struct run_tx_data *data; 950 951 /* make sure any subsequent use of the queues will fail */ 952 pq->tx_nfree = 0; 953 STAILQ_INIT(&pq->tx_fh); 954 STAILQ_INIT(&pq->tx_qh); 955 956 /* free up all node references and mbufs */ 957 for (data = &pq->tx_data[0]; 958 data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) { 959 if (data->m != NULL) { 960 m_freem(data->m); 961 data->m = NULL; 962 } 963 if (data->ni != NULL) { 964 ieee80211_free_node(data->ni); 965 data->ni = NULL; 966 } 967 } 968 } 969 970 static int 971 run_load_microcode(struct run_softc *sc) 972 { 973 usb_device_request_t req; 974 const struct firmware *fw; 975 const u_char *base; 976 uint32_t tmp; 977 int ntries, error; 978 const uint64_t *temp; 979 uint64_t bytes; 980 981 RUN_UNLOCK(sc); 982 fw = firmware_get("runfw"); 983 RUN_LOCK(sc); 984 if (fw == NULL) { 985 device_printf(sc->sc_dev, 986 "failed loadfirmware of file %s\n", "runfw"); 987 return ENOENT; 988 } 989 990 if (fw->datasize != 8192) { 991 device_printf(sc->sc_dev, 992 "invalid firmware size (should be 8KB)\n"); 993 error = EINVAL; 994 goto fail; 995 } 996 997 /* 998 * RT3071/RT3072 use a different firmware 999 * run-rt2870 (8KB) contains both, 1000 * first half (4KB) is for rt2870, 1001 * last half is for rt3071. 1002 */ 1003 base = fw->data; 1004 if ((sc->mac_ver) != 0x2860 && 1005 (sc->mac_ver) != 0x2872 && 1006 (sc->mac_ver) != 0x3070) { 1007 base += 4096; 1008 } 1009 1010 /* cheap sanity check */ 1011 temp = fw->data; 1012 bytes = *temp; 1013 if (bytes != be64toh(0xffffff0210280210)) { 1014 device_printf(sc->sc_dev, "firmware checksum failed\n"); 1015 error = EINVAL; 1016 goto fail; 1017 } 1018 1019 run_read(sc, RT2860_ASIC_VER_ID, &tmp); 1020 /* write microcode image */ 1021 run_write_region_1(sc, RT2870_FW_BASE, base, 4096); 1022 run_write(sc, RT2860_H2M_MAILBOX_CID, 0xffffffff); 1023 run_write(sc, RT2860_H2M_MAILBOX_STATUS, 0xffffffff); 1024 1025 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1026 req.bRequest = RT2870_RESET; 1027 USETW(req.wValue, 8); 1028 USETW(req.wIndex, 0); 1029 USETW(req.wLength, 0); 1030 if ((error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL)) 1031 != 0) { 1032 device_printf(sc->sc_dev, "firmware reset failed\n"); 1033 goto fail; 1034 } 1035 1036 run_delay(sc, 10); 1037 1038 run_write(sc, RT2860_H2M_MAILBOX, 0); 1039 if ((error = run_mcu_cmd(sc, RT2860_MCU_CMD_RFRESET, 0)) != 0) 1040 goto fail; 1041 1042 /* wait until microcontroller is ready */ 1043 for (ntries = 0; ntries < 1000; ntries++) { 1044 if ((error = run_read(sc, RT2860_SYS_CTRL, &tmp)) != 0) { 1045 goto fail; 1046 } 1047 if (tmp & RT2860_MCU_READY) 1048 break; 1049 run_delay(sc, 10); 1050 } 1051 if (ntries == 1000) { 1052 device_printf(sc->sc_dev, 1053 "timeout waiting for MCU to initialize\n"); 1054 error = ETIMEDOUT; 1055 goto fail; 1056 } 1057 device_printf(sc->sc_dev, "firmware %s ver. %u.%u loaded\n", 1058 (base == fw->data) ? "RT2870" : "RT3071", 1059 *(base + 4092), *(base + 4093)); 1060 1061 fail: 1062 firmware_put(fw, FIRMWARE_UNLOAD); 1063 return (error); 1064 } 1065 1066 int 1067 run_reset(struct run_softc *sc) 1068 { 1069 usb_device_request_t req; 1070 1071 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1072 req.bRequest = RT2870_RESET; 1073 USETW(req.wValue, 1); 1074 USETW(req.wIndex, 0); 1075 USETW(req.wLength, 0); 1076 return (usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL)); 1077 } 1078 1079 static usb_error_t 1080 run_do_request(struct run_softc *sc, 1081 struct usb_device_request *req, void *data) 1082 { 1083 usb_error_t err; 1084 int ntries = 10; 1085 1086 RUN_LOCK_ASSERT(sc, MA_OWNED); 1087 1088 while (ntries--) { 1089 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx, 1090 req, data, 0, NULL, 250 /* ms */); 1091 if (err == 0) 1092 break; 1093 DPRINTFN(1, "Control request failed, %s (retrying)\n", 1094 usbd_errstr(err)); 1095 run_delay(sc, 10); 1096 } 1097 return (err); 1098 } 1099 1100 static int 1101 run_read(struct run_softc *sc, uint16_t reg, uint32_t *val) 1102 { 1103 uint32_t tmp; 1104 int error; 1105 1106 error = run_read_region_1(sc, reg, (uint8_t *)&tmp, sizeof tmp); 1107 if (error == 0) 1108 *val = le32toh(tmp); 1109 else 1110 *val = 0xffffffff; 1111 return (error); 1112 } 1113 1114 static int 1115 run_read_region_1(struct run_softc *sc, uint16_t reg, uint8_t *buf, int len) 1116 { 1117 usb_device_request_t req; 1118 1119 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1120 req.bRequest = RT2870_READ_REGION_1; 1121 USETW(req.wValue, 0); 1122 USETW(req.wIndex, reg); 1123 USETW(req.wLength, len); 1124 1125 return (run_do_request(sc, &req, buf)); 1126 } 1127 1128 static int 1129 run_write_2(struct run_softc *sc, uint16_t reg, uint16_t val) 1130 { 1131 usb_device_request_t req; 1132 1133 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1134 req.bRequest = RT2870_WRITE_2; 1135 USETW(req.wValue, val); 1136 USETW(req.wIndex, reg); 1137 USETW(req.wLength, 0); 1138 1139 return (run_do_request(sc, &req, NULL)); 1140 } 1141 1142 static int 1143 run_write(struct run_softc *sc, uint16_t reg, uint32_t val) 1144 { 1145 int error; 1146 1147 if ((error = run_write_2(sc, reg, val & 0xffff)) == 0) 1148 error = run_write_2(sc, reg + 2, val >> 16); 1149 return (error); 1150 } 1151 1152 static int 1153 run_write_region_1(struct run_softc *sc, uint16_t reg, const uint8_t *buf, 1154 int len) 1155 { 1156 #if 1 1157 int i, error = 0; 1158 /* 1159 * NB: the WRITE_REGION_1 command is not stable on RT2860. 1160 * We thus issue multiple WRITE_2 commands instead. 1161 */ 1162 KASSERT((len & 1) == 0, ("run_write_region_1: Data too long.\n")); 1163 for (i = 0; i < len && error == 0; i += 2) 1164 error = run_write_2(sc, reg + i, buf[i] | buf[i + 1] << 8); 1165 return (error); 1166 #else 1167 usb_device_request_t req; 1168 1169 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1170 req.bRequest = RT2870_WRITE_REGION_1; 1171 USETW(req.wValue, 0); 1172 USETW(req.wIndex, reg); 1173 USETW(req.wLength, len); 1174 return (run_do_request(sc, &req, buf)); 1175 #endif 1176 } 1177 1178 static int 1179 run_set_region_4(struct run_softc *sc, uint16_t reg, uint32_t val, int len) 1180 { 1181 int i, error = 0; 1182 1183 KASSERT((len & 3) == 0, ("run_set_region_4: Invalid data length.\n")); 1184 for (i = 0; i < len && error == 0; i += 4) 1185 error = run_write(sc, reg + i, val); 1186 return (error); 1187 } 1188 1189 /* Read 16-bit from eFUSE ROM (RT3070 only.) */ 1190 static int 1191 run_efuse_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val) 1192 { 1193 uint32_t tmp; 1194 uint16_t reg; 1195 int error, ntries; 1196 1197 if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0) 1198 return (error); 1199 1200 addr *= 2; 1201 /*- 1202 * Read one 16-byte block into registers EFUSE_DATA[0-3]: 1203 * DATA0: F E D C 1204 * DATA1: B A 9 8 1205 * DATA2: 7 6 5 4 1206 * DATA3: 3 2 1 0 1207 */ 1208 tmp &= ~(RT3070_EFSROM_MODE_MASK | RT3070_EFSROM_AIN_MASK); 1209 tmp |= (addr & ~0xf) << RT3070_EFSROM_AIN_SHIFT | RT3070_EFSROM_KICK; 1210 run_write(sc, RT3070_EFUSE_CTRL, tmp); 1211 for (ntries = 0; ntries < 100; ntries++) { 1212 if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0) 1213 return (error); 1214 if (!(tmp & RT3070_EFSROM_KICK)) 1215 break; 1216 run_delay(sc, 2); 1217 } 1218 if (ntries == 100) 1219 return (ETIMEDOUT); 1220 1221 if ((tmp & RT3070_EFUSE_AOUT_MASK) == RT3070_EFUSE_AOUT_MASK) { 1222 *val = 0xffff; /* address not found */ 1223 return (0); 1224 } 1225 /* determine to which 32-bit register our 16-bit word belongs */ 1226 reg = RT3070_EFUSE_DATA3 - (addr & 0xc); 1227 if ((error = run_read(sc, reg, &tmp)) != 0) 1228 return (error); 1229 1230 *val = (addr & 2) ? tmp >> 16 : tmp & 0xffff; 1231 return (0); 1232 } 1233 1234 static int 1235 run_eeprom_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val) 1236 { 1237 usb_device_request_t req; 1238 uint16_t tmp; 1239 int error; 1240 1241 addr *= 2; 1242 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1243 req.bRequest = RT2870_EEPROM_READ; 1244 USETW(req.wValue, 0); 1245 USETW(req.wIndex, addr); 1246 USETW(req.wLength, sizeof tmp); 1247 1248 error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, &tmp); 1249 if (error == 0) 1250 *val = le16toh(tmp); 1251 else 1252 *val = 0xffff; 1253 return (error); 1254 } 1255 1256 static __inline int 1257 run_srom_read(struct run_softc *sc, uint16_t addr, uint16_t *val) 1258 { 1259 /* either eFUSE ROM or EEPROM */ 1260 return sc->sc_srom_read(sc, addr, val); 1261 } 1262 1263 static int 1264 run_rt2870_rf_write(struct run_softc *sc, uint8_t reg, uint32_t val) 1265 { 1266 uint32_t tmp; 1267 int error, ntries; 1268 1269 for (ntries = 0; ntries < 10; ntries++) { 1270 if ((error = run_read(sc, RT2860_RF_CSR_CFG0, &tmp)) != 0) 1271 return (error); 1272 if (!(tmp & RT2860_RF_REG_CTRL)) 1273 break; 1274 } 1275 if (ntries == 10) 1276 return (ETIMEDOUT); 1277 1278 /* RF registers are 24-bit on the RT2860 */ 1279 tmp = RT2860_RF_REG_CTRL | 24 << RT2860_RF_REG_WIDTH_SHIFT | 1280 (val & 0x3fffff) << 2 | (reg & 3); 1281 return (run_write(sc, RT2860_RF_CSR_CFG0, tmp)); 1282 } 1283 1284 static int 1285 run_rt3070_rf_read(struct run_softc *sc, uint8_t reg, uint8_t *val) 1286 { 1287 uint32_t tmp; 1288 int error, ntries; 1289 1290 for (ntries = 0; ntries < 100; ntries++) { 1291 if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0) 1292 return (error); 1293 if (!(tmp & RT3070_RF_KICK)) 1294 break; 1295 } 1296 if (ntries == 100) 1297 return (ETIMEDOUT); 1298 1299 tmp = RT3070_RF_KICK | reg << 8; 1300 if ((error = run_write(sc, RT3070_RF_CSR_CFG, tmp)) != 0) 1301 return (error); 1302 1303 for (ntries = 0; ntries < 100; ntries++) { 1304 if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0) 1305 return (error); 1306 if (!(tmp & RT3070_RF_KICK)) 1307 break; 1308 } 1309 if (ntries == 100) 1310 return (ETIMEDOUT); 1311 1312 *val = tmp & 0xff; 1313 return (0); 1314 } 1315 1316 static int 1317 run_rt3070_rf_write(struct run_softc *sc, uint8_t reg, uint8_t val) 1318 { 1319 uint32_t tmp; 1320 int error, ntries; 1321 1322 for (ntries = 0; ntries < 10; ntries++) { 1323 if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0) 1324 return (error); 1325 if (!(tmp & RT3070_RF_KICK)) 1326 break; 1327 } 1328 if (ntries == 10) 1329 return (ETIMEDOUT); 1330 1331 tmp = RT3070_RF_WRITE | RT3070_RF_KICK | reg << 8 | val; 1332 return (run_write(sc, RT3070_RF_CSR_CFG, tmp)); 1333 } 1334 1335 static int 1336 run_bbp_read(struct run_softc *sc, uint8_t reg, uint8_t *val) 1337 { 1338 uint32_t tmp; 1339 int ntries, error; 1340 1341 for (ntries = 0; ntries < 10; ntries++) { 1342 if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0) 1343 return (error); 1344 if (!(tmp & RT2860_BBP_CSR_KICK)) 1345 break; 1346 } 1347 if (ntries == 10) 1348 return (ETIMEDOUT); 1349 1350 tmp = RT2860_BBP_CSR_READ | RT2860_BBP_CSR_KICK | reg << 8; 1351 if ((error = run_write(sc, RT2860_BBP_CSR_CFG, tmp)) != 0) 1352 return (error); 1353 1354 for (ntries = 0; ntries < 10; ntries++) { 1355 if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0) 1356 return (error); 1357 if (!(tmp & RT2860_BBP_CSR_KICK)) 1358 break; 1359 } 1360 if (ntries == 10) 1361 return (ETIMEDOUT); 1362 1363 *val = tmp & 0xff; 1364 return (0); 1365 } 1366 1367 static int 1368 run_bbp_write(struct run_softc *sc, uint8_t reg, uint8_t val) 1369 { 1370 uint32_t tmp; 1371 int ntries, error; 1372 1373 for (ntries = 0; ntries < 10; ntries++) { 1374 if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0) 1375 return (error); 1376 if (!(tmp & RT2860_BBP_CSR_KICK)) 1377 break; 1378 } 1379 if (ntries == 10) 1380 return (ETIMEDOUT); 1381 1382 tmp = RT2860_BBP_CSR_KICK | reg << 8 | val; 1383 return (run_write(sc, RT2860_BBP_CSR_CFG, tmp)); 1384 } 1385 1386 /* 1387 * Send a command to the 8051 microcontroller unit. 1388 */ 1389 static int 1390 run_mcu_cmd(struct run_softc *sc, uint8_t cmd, uint16_t arg) 1391 { 1392 uint32_t tmp; 1393 int error, ntries; 1394 1395 for (ntries = 0; ntries < 100; ntries++) { 1396 if ((error = run_read(sc, RT2860_H2M_MAILBOX, &tmp)) != 0) 1397 return error; 1398 if (!(tmp & RT2860_H2M_BUSY)) 1399 break; 1400 } 1401 if (ntries == 100) 1402 return ETIMEDOUT; 1403 1404 tmp = RT2860_H2M_BUSY | RT2860_TOKEN_NO_INTR << 16 | arg; 1405 if ((error = run_write(sc, RT2860_H2M_MAILBOX, tmp)) == 0) 1406 error = run_write(sc, RT2860_HOST_CMD, cmd); 1407 return (error); 1408 } 1409 1410 /* 1411 * Add `delta' (signed) to each 4-bit sub-word of a 32-bit word. 1412 * Used to adjust per-rate Tx power registers. 1413 */ 1414 static __inline uint32_t 1415 b4inc(uint32_t b32, int8_t delta) 1416 { 1417 int8_t i, b4; 1418 1419 for (i = 0; i < 8; i++) { 1420 b4 = b32 & 0xf; 1421 b4 += delta; 1422 if (b4 < 0) 1423 b4 = 0; 1424 else if (b4 > 0xf) 1425 b4 = 0xf; 1426 b32 = b32 >> 4 | b4 << 28; 1427 } 1428 return (b32); 1429 } 1430 1431 static const char * 1432 run_get_rf(int rev) 1433 { 1434 switch (rev) { 1435 case RT2860_RF_2820: return "RT2820"; 1436 case RT2860_RF_2850: return "RT2850"; 1437 case RT2860_RF_2720: return "RT2720"; 1438 case RT2860_RF_2750: return "RT2750"; 1439 case RT3070_RF_3020: return "RT3020"; 1440 case RT3070_RF_2020: return "RT2020"; 1441 case RT3070_RF_3021: return "RT3021"; 1442 case RT3070_RF_3022: return "RT3022"; 1443 case RT3070_RF_3052: return "RT3052"; 1444 } 1445 return ("unknown"); 1446 } 1447 1448 int 1449 run_read_eeprom(struct run_softc *sc) 1450 { 1451 int8_t delta_2ghz, delta_5ghz; 1452 uint32_t tmp; 1453 uint16_t val; 1454 int ridx, ant, i; 1455 1456 /* check whether the ROM is eFUSE ROM or EEPROM */ 1457 sc->sc_srom_read = run_eeprom_read_2; 1458 if (sc->mac_ver >= 0x3070) { 1459 run_read(sc, RT3070_EFUSE_CTRL, &tmp); 1460 DPRINTF("EFUSE_CTRL=0x%08x\n", tmp); 1461 if (tmp & RT3070_SEL_EFUSE) 1462 sc->sc_srom_read = run_efuse_read_2; 1463 } 1464 1465 /* read ROM version */ 1466 run_srom_read(sc, RT2860_EEPROM_VERSION, &val); 1467 DPRINTF("EEPROM rev=%d, FAE=%d\n", val & 0xff, val >> 8); 1468 1469 /* read MAC address */ 1470 run_srom_read(sc, RT2860_EEPROM_MAC01, &val); 1471 sc->sc_bssid[0] = val & 0xff; 1472 sc->sc_bssid[1] = val >> 8; 1473 run_srom_read(sc, RT2860_EEPROM_MAC23, &val); 1474 sc->sc_bssid[2] = val & 0xff; 1475 sc->sc_bssid[3] = val >> 8; 1476 run_srom_read(sc, RT2860_EEPROM_MAC45, &val); 1477 sc->sc_bssid[4] = val & 0xff; 1478 sc->sc_bssid[5] = val >> 8; 1479 1480 /* read vender BBP settings */ 1481 for (i = 0; i < 10; i++) { 1482 run_srom_read(sc, RT2860_EEPROM_BBP_BASE + i, &val); 1483 sc->bbp[i].val = val & 0xff; 1484 sc->bbp[i].reg = val >> 8; 1485 DPRINTF("BBP%d=0x%02x\n", sc->bbp[i].reg, sc->bbp[i].val); 1486 } 1487 if (sc->mac_ver >= 0x3071) { 1488 /* read vendor RF settings */ 1489 for (i = 0; i < 10; i++) { 1490 run_srom_read(sc, RT3071_EEPROM_RF_BASE + i, &val); 1491 sc->rf[i].val = val & 0xff; 1492 sc->rf[i].reg = val >> 8; 1493 DPRINTF("RF%d=0x%02x\n", sc->rf[i].reg, 1494 sc->rf[i].val); 1495 } 1496 } 1497 1498 /* read RF frequency offset from EEPROM */ 1499 run_srom_read(sc, RT2860_EEPROM_FREQ_LEDS, &val); 1500 sc->freq = ((val & 0xff) != 0xff) ? val & 0xff : 0; 1501 DPRINTF("EEPROM freq offset %d\n", sc->freq & 0xff); 1502 1503 if (val >> 8 != 0xff) { 1504 /* read LEDs operating mode */ 1505 sc->leds = val >> 8; 1506 run_srom_read(sc, RT2860_EEPROM_LED1, &sc->led[0]); 1507 run_srom_read(sc, RT2860_EEPROM_LED2, &sc->led[1]); 1508 run_srom_read(sc, RT2860_EEPROM_LED3, &sc->led[2]); 1509 } else { 1510 /* broken EEPROM, use default settings */ 1511 sc->leds = 0x01; 1512 sc->led[0] = 0x5555; 1513 sc->led[1] = 0x2221; 1514 sc->led[2] = 0x5627; /* differs from RT2860 */ 1515 } 1516 DPRINTF("EEPROM LED mode=0x%02x, LEDs=0x%04x/0x%04x/0x%04x\n", 1517 sc->leds, sc->led[0], sc->led[1], sc->led[2]); 1518 1519 /* read RF information */ 1520 run_srom_read(sc, RT2860_EEPROM_ANTENNA, &val); 1521 if (val == 0xffff) { 1522 DPRINTF("invalid EEPROM antenna info, using default\n"); 1523 if (sc->mac_ver == 0x3572) { 1524 /* default to RF3052 2T2R */ 1525 sc->rf_rev = RT3070_RF_3052; 1526 sc->ntxchains = 2; 1527 sc->nrxchains = 2; 1528 } else if (sc->mac_ver >= 0x3070) { 1529 /* default to RF3020 1T1R */ 1530 sc->rf_rev = RT3070_RF_3020; 1531 sc->ntxchains = 1; 1532 sc->nrxchains = 1; 1533 } else { 1534 /* default to RF2820 1T2R */ 1535 sc->rf_rev = RT2860_RF_2820; 1536 sc->ntxchains = 1; 1537 sc->nrxchains = 2; 1538 } 1539 } else { 1540 sc->rf_rev = (val >> 8) & 0xf; 1541 sc->ntxchains = (val >> 4) & 0xf; 1542 sc->nrxchains = val & 0xf; 1543 } 1544 DPRINTF("EEPROM RF rev=0x%02x chains=%dT%dR\n", 1545 sc->rf_rev, sc->ntxchains, sc->nrxchains); 1546 1547 /* check if RF supports automatic Tx access gain control */ 1548 run_srom_read(sc, RT2860_EEPROM_CONFIG, &val); 1549 DPRINTF("EEPROM CFG 0x%04x\n", val); 1550 /* check if driver should patch the DAC issue */ 1551 if ((val >> 8) != 0xff) 1552 sc->patch_dac = (val >> 15) & 1; 1553 if ((val & 0xff) != 0xff) { 1554 sc->ext_5ghz_lna = (val >> 3) & 1; 1555 sc->ext_2ghz_lna = (val >> 2) & 1; 1556 /* check if RF supports automatic Tx access gain control */ 1557 sc->calib_2ghz = sc->calib_5ghz = (val >> 1) & 1; 1558 /* check if we have a hardware radio switch */ 1559 sc->rfswitch = val & 1; 1560 } 1561 1562 /* read power settings for 2GHz channels */ 1563 for (i = 0; i < 14; i += 2) { 1564 run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE1 + i / 2, &val); 1565 sc->txpow1[i + 0] = (int8_t)(val & 0xff); 1566 sc->txpow1[i + 1] = (int8_t)(val >> 8); 1567 1568 run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE2 + i / 2, &val); 1569 sc->txpow2[i + 0] = (int8_t)(val & 0xff); 1570 sc->txpow2[i + 1] = (int8_t)(val >> 8); 1571 } 1572 /* fix broken Tx power entries */ 1573 for (i = 0; i < 14; i++) { 1574 if (sc->txpow1[i] < 0 || sc->txpow1[i] > 31) 1575 sc->txpow1[i] = 5; 1576 if (sc->txpow2[i] < 0 || sc->txpow2[i] > 31) 1577 sc->txpow2[i] = 5; 1578 DPRINTF("chan %d: power1=%d, power2=%d\n", 1579 rt2860_rf2850[i].chan, sc->txpow1[i], sc->txpow2[i]); 1580 } 1581 /* read power settings for 5GHz channels */ 1582 for (i = 0; i < 40; i += 2) { 1583 run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE1 + i / 2, &val); 1584 sc->txpow1[i + 14] = (int8_t)(val & 0xff); 1585 sc->txpow1[i + 15] = (int8_t)(val >> 8); 1586 1587 run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE2 + i / 2, &val); 1588 sc->txpow2[i + 14] = (int8_t)(val & 0xff); 1589 sc->txpow2[i + 15] = (int8_t)(val >> 8); 1590 } 1591 /* fix broken Tx power entries */ 1592 for (i = 0; i < 40; i++) { 1593 if (sc->txpow1[14 + i] < -7 || sc->txpow1[14 + i] > 15) 1594 sc->txpow1[14 + i] = 5; 1595 if (sc->txpow2[14 + i] < -7 || sc->txpow2[14 + i] > 15) 1596 sc->txpow2[14 + i] = 5; 1597 DPRINTF("chan %d: power1=%d, power2=%d\n", 1598 rt2860_rf2850[14 + i].chan, sc->txpow1[14 + i], 1599 sc->txpow2[14 + i]); 1600 } 1601 1602 /* read Tx power compensation for each Tx rate */ 1603 run_srom_read(sc, RT2860_EEPROM_DELTAPWR, &val); 1604 delta_2ghz = delta_5ghz = 0; 1605 if ((val & 0xff) != 0xff && (val & 0x80)) { 1606 delta_2ghz = val & 0xf; 1607 if (!(val & 0x40)) /* negative number */ 1608 delta_2ghz = -delta_2ghz; 1609 } 1610 val >>= 8; 1611 if ((val & 0xff) != 0xff && (val & 0x80)) { 1612 delta_5ghz = val & 0xf; 1613 if (!(val & 0x40)) /* negative number */ 1614 delta_5ghz = -delta_5ghz; 1615 } 1616 DPRINTF("power compensation=%d (2GHz), %d (5GHz)\n", 1617 delta_2ghz, delta_5ghz); 1618 1619 for (ridx = 0; ridx < 5; ridx++) { 1620 uint32_t reg; 1621 1622 run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2, &val); 1623 reg = val; 1624 run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2 + 1, &val); 1625 reg |= (uint32_t)val << 16; 1626 1627 sc->txpow20mhz[ridx] = reg; 1628 sc->txpow40mhz_2ghz[ridx] = b4inc(reg, delta_2ghz); 1629 sc->txpow40mhz_5ghz[ridx] = b4inc(reg, delta_5ghz); 1630 1631 DPRINTF("ridx %d: power 20MHz=0x%08x, 40MHz/2GHz=0x%08x, " 1632 "40MHz/5GHz=0x%08x\n", ridx, sc->txpow20mhz[ridx], 1633 sc->txpow40mhz_2ghz[ridx], sc->txpow40mhz_5ghz[ridx]); 1634 } 1635 1636 /* read RSSI offsets and LNA gains from EEPROM */ 1637 run_srom_read(sc, RT2860_EEPROM_RSSI1_2GHZ, &val); 1638 sc->rssi_2ghz[0] = val & 0xff; /* Ant A */ 1639 sc->rssi_2ghz[1] = val >> 8; /* Ant B */ 1640 run_srom_read(sc, RT2860_EEPROM_RSSI2_2GHZ, &val); 1641 if (sc->mac_ver >= 0x3070) { 1642 /* 1643 * On RT3070 chips (limited to 2 Rx chains), this ROM 1644 * field contains the Tx mixer gain for the 2GHz band. 1645 */ 1646 if ((val & 0xff) != 0xff) 1647 sc->txmixgain_2ghz = val & 0x7; 1648 DPRINTF("tx mixer gain=%u (2GHz)\n", sc->txmixgain_2ghz); 1649 } else 1650 sc->rssi_2ghz[2] = val & 0xff; /* Ant C */ 1651 sc->lna[2] = val >> 8; /* channel group 2 */ 1652 1653 run_srom_read(sc, RT2860_EEPROM_RSSI1_5GHZ, &val); 1654 sc->rssi_5ghz[0] = val & 0xff; /* Ant A */ 1655 sc->rssi_5ghz[1] = val >> 8; /* Ant B */ 1656 run_srom_read(sc, RT2860_EEPROM_RSSI2_5GHZ, &val); 1657 if (sc->mac_ver == 0x3572) { 1658 /* 1659 * On RT3572 chips (limited to 2 Rx chains), this ROM 1660 * field contains the Tx mixer gain for the 5GHz band. 1661 */ 1662 if ((val & 0xff) != 0xff) 1663 sc->txmixgain_5ghz = val & 0x7; 1664 DPRINTF("tx mixer gain=%u (5GHz)\n", sc->txmixgain_5ghz); 1665 } else 1666 sc->rssi_5ghz[2] = val & 0xff; /* Ant C */ 1667 sc->lna[3] = val >> 8; /* channel group 3 */ 1668 1669 run_srom_read(sc, RT2860_EEPROM_LNA, &val); 1670 sc->lna[0] = val & 0xff; /* channel group 0 */ 1671 sc->lna[1] = val >> 8; /* channel group 1 */ 1672 1673 /* fix broken 5GHz LNA entries */ 1674 if (sc->lna[2] == 0 || sc->lna[2] == 0xff) { 1675 DPRINTF("invalid LNA for channel group %d\n", 2); 1676 sc->lna[2] = sc->lna[1]; 1677 } 1678 if (sc->lna[3] == 0 || sc->lna[3] == 0xff) { 1679 DPRINTF("invalid LNA for channel group %d\n", 3); 1680 sc->lna[3] = sc->lna[1]; 1681 } 1682 1683 /* fix broken RSSI offset entries */ 1684 for (ant = 0; ant < 3; ant++) { 1685 if (sc->rssi_2ghz[ant] < -10 || sc->rssi_2ghz[ant] > 10) { 1686 DPRINTF("invalid RSSI%d offset: %d (2GHz)\n", 1687 ant + 1, sc->rssi_2ghz[ant]); 1688 sc->rssi_2ghz[ant] = 0; 1689 } 1690 if (sc->rssi_5ghz[ant] < -10 || sc->rssi_5ghz[ant] > 10) { 1691 DPRINTF("invalid RSSI%d offset: %d (5GHz)\n", 1692 ant + 1, sc->rssi_5ghz[ant]); 1693 sc->rssi_5ghz[ant] = 0; 1694 } 1695 } 1696 return (0); 1697 } 1698 1699 static struct ieee80211_node * 1700 run_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN]) 1701 { 1702 return malloc(sizeof (struct run_node), M_DEVBUF, M_NOWAIT | M_ZERO); 1703 } 1704 1705 static int 1706 run_media_change(struct ifnet *ifp) 1707 { 1708 struct ieee80211vap *vap = ifp->if_softc; 1709 struct ieee80211com *ic = vap->iv_ic; 1710 const struct ieee80211_txparam *tp; 1711 struct run_softc *sc = ic->ic_ifp->if_softc; 1712 uint8_t rate, ridx; 1713 int error; 1714 1715 RUN_LOCK(sc); 1716 1717 error = ieee80211_media_change(ifp); 1718 if (error != ENETRESET) { 1719 RUN_UNLOCK(sc); 1720 return (error); 1721 } 1722 1723 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1724 if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) { 1725 struct ieee80211_node *ni; 1726 struct run_node *rn; 1727 1728 rate = ic->ic_sup_rates[ic->ic_curmode]. 1729 rs_rates[tp->ucastrate] & IEEE80211_RATE_VAL; 1730 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++) 1731 if (rt2860_rates[ridx].rate == rate) 1732 break; 1733 ni = ieee80211_ref_node(vap->iv_bss); 1734 rn = (struct run_node *)ni; 1735 rn->fix_ridx = ridx; 1736 DPRINTF("rate=%d, fix_ridx=%d\n", rate, rn->fix_ridx); 1737 ieee80211_free_node(ni); 1738 } 1739 1740 #if 0 1741 if ((ifp->if_flags & IFF_UP) && 1742 (ifp->if_drv_flags & IFF_DRV_RUNNING)){ 1743 run_init_locked(sc); 1744 } 1745 #endif 1746 1747 RUN_UNLOCK(sc); 1748 1749 return (0); 1750 } 1751 1752 static int 1753 run_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1754 { 1755 const struct ieee80211_txparam *tp; 1756 struct ieee80211com *ic = vap->iv_ic; 1757 struct run_softc *sc = ic->ic_ifp->if_softc; 1758 struct run_vap *rvp = RUN_VAP(vap); 1759 enum ieee80211_state ostate; 1760 uint32_t sta[3]; 1761 uint32_t tmp; 1762 uint8_t ratectl; 1763 uint8_t restart_ratectl = 0; 1764 uint8_t bid = 1 << rvp->rvp_id; 1765 1766 ostate = vap->iv_state; 1767 DPRINTF("%s -> %s\n", 1768 ieee80211_state_name[ostate], 1769 ieee80211_state_name[nstate]); 1770 1771 IEEE80211_UNLOCK(ic); 1772 RUN_LOCK(sc); 1773 1774 ratectl = sc->ratectl_run; /* remember current state */ 1775 sc->ratectl_run = RUN_RATECTL_OFF; 1776 usb_callout_stop(&sc->ratectl_ch); 1777 1778 if (ostate == IEEE80211_S_RUN) { 1779 /* turn link LED off */ 1780 run_set_leds(sc, RT2860_LED_RADIO); 1781 } 1782 1783 switch (nstate) { 1784 case IEEE80211_S_INIT: 1785 restart_ratectl = 1; 1786 1787 if (ostate != IEEE80211_S_RUN) 1788 break; 1789 1790 ratectl &= ~bid; 1791 sc->runbmap &= ~bid; 1792 1793 /* abort TSF synchronization if there is no vap running */ 1794 if (--sc->running == 0) { 1795 run_read(sc, RT2860_BCN_TIME_CFG, &tmp); 1796 run_write(sc, RT2860_BCN_TIME_CFG, 1797 tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN | 1798 RT2860_TBTT_TIMER_EN)); 1799 } 1800 break; 1801 1802 case IEEE80211_S_RUN: 1803 if (!(sc->runbmap & bid)) { 1804 if(sc->running++) 1805 restart_ratectl = 1; 1806 sc->runbmap |= bid; 1807 } 1808 1809 m_freem(rvp->beacon_mbuf); 1810 rvp->beacon_mbuf = NULL; 1811 1812 switch (vap->iv_opmode) { 1813 case IEEE80211_M_HOSTAP: 1814 case IEEE80211_M_MBSS: 1815 sc->ap_running |= bid; 1816 ic->ic_opmode = vap->iv_opmode; 1817 run_update_beacon_cb(vap); 1818 break; 1819 case IEEE80211_M_IBSS: 1820 sc->adhoc_running |= bid; 1821 if (!sc->ap_running) 1822 ic->ic_opmode = vap->iv_opmode; 1823 run_update_beacon_cb(vap); 1824 break; 1825 case IEEE80211_M_STA: 1826 sc->sta_running |= bid; 1827 if (!sc->ap_running && !sc->adhoc_running) 1828 ic->ic_opmode = vap->iv_opmode; 1829 1830 /* read statistic counters (clear on read) */ 1831 run_read_region_1(sc, RT2860_TX_STA_CNT0, 1832 (uint8_t *)sta, sizeof sta); 1833 1834 break; 1835 default: 1836 ic->ic_opmode = vap->iv_opmode; 1837 break; 1838 } 1839 1840 if (vap->iv_opmode != IEEE80211_M_MONITOR) { 1841 struct ieee80211_node *ni; 1842 1843 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) { 1844 RUN_UNLOCK(sc); 1845 IEEE80211_LOCK(ic); 1846 return (-1); 1847 } 1848 run_updateslot(ic->ic_ifp); 1849 run_enable_mrr(sc); 1850 run_set_txpreamble(sc); 1851 run_set_basicrates(sc); 1852 ni = ieee80211_ref_node(vap->iv_bss); 1853 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid); 1854 run_set_bssid(sc, ni->ni_bssid); 1855 ieee80211_free_node(ni); 1856 run_enable_tsf_sync(sc); 1857 1858 /* enable automatic rate adaptation */ 1859 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1860 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) 1861 ratectl |= bid; 1862 } 1863 1864 /* turn link LED on */ 1865 run_set_leds(sc, RT2860_LED_RADIO | 1866 (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan) ? 1867 RT2860_LED_LINK_2GHZ : RT2860_LED_LINK_5GHZ)); 1868 1869 break; 1870 default: 1871 DPRINTFN(6, "undefined case\n"); 1872 break; 1873 } 1874 1875 /* restart amrr for running VAPs */ 1876 if ((sc->ratectl_run = ratectl) && restart_ratectl) 1877 usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc); 1878 1879 RUN_UNLOCK(sc); 1880 IEEE80211_LOCK(ic); 1881 1882 return(rvp->newstate(vap, nstate, arg)); 1883 } 1884 1885 /* ARGSUSED */ 1886 static void 1887 run_wme_update_cb(void *arg) 1888 { 1889 struct ieee80211com *ic = arg; 1890 struct run_softc *sc = ic->ic_ifp->if_softc; 1891 struct ieee80211_wme_state *wmesp = &ic->ic_wme; 1892 int aci, error = 0; 1893 1894 RUN_LOCK_ASSERT(sc, MA_OWNED); 1895 1896 /* update MAC TX configuration registers */ 1897 for (aci = 0; aci < WME_NUM_AC; aci++) { 1898 error = run_write(sc, RT2860_EDCA_AC_CFG(aci), 1899 wmesp->wme_params[aci].wmep_logcwmax << 16 | 1900 wmesp->wme_params[aci].wmep_logcwmin << 12 | 1901 wmesp->wme_params[aci].wmep_aifsn << 8 | 1902 wmesp->wme_params[aci].wmep_txopLimit); 1903 if (error) goto err; 1904 } 1905 1906 /* update SCH/DMA registers too */ 1907 error = run_write(sc, RT2860_WMM_AIFSN_CFG, 1908 wmesp->wme_params[WME_AC_VO].wmep_aifsn << 12 | 1909 wmesp->wme_params[WME_AC_VI].wmep_aifsn << 8 | 1910 wmesp->wme_params[WME_AC_BK].wmep_aifsn << 4 | 1911 wmesp->wme_params[WME_AC_BE].wmep_aifsn); 1912 if (error) goto err; 1913 error = run_write(sc, RT2860_WMM_CWMIN_CFG, 1914 wmesp->wme_params[WME_AC_VO].wmep_logcwmin << 12 | 1915 wmesp->wme_params[WME_AC_VI].wmep_logcwmin << 8 | 1916 wmesp->wme_params[WME_AC_BK].wmep_logcwmin << 4 | 1917 wmesp->wme_params[WME_AC_BE].wmep_logcwmin); 1918 if (error) goto err; 1919 error = run_write(sc, RT2860_WMM_CWMAX_CFG, 1920 wmesp->wme_params[WME_AC_VO].wmep_logcwmax << 12 | 1921 wmesp->wme_params[WME_AC_VI].wmep_logcwmax << 8 | 1922 wmesp->wme_params[WME_AC_BK].wmep_logcwmax << 4 | 1923 wmesp->wme_params[WME_AC_BE].wmep_logcwmax); 1924 if (error) goto err; 1925 error = run_write(sc, RT2860_WMM_TXOP0_CFG, 1926 wmesp->wme_params[WME_AC_BK].wmep_txopLimit << 16 | 1927 wmesp->wme_params[WME_AC_BE].wmep_txopLimit); 1928 if (error) goto err; 1929 error = run_write(sc, RT2860_WMM_TXOP1_CFG, 1930 wmesp->wme_params[WME_AC_VO].wmep_txopLimit << 16 | 1931 wmesp->wme_params[WME_AC_VI].wmep_txopLimit); 1932 1933 err: 1934 if (error) 1935 DPRINTF("WME update failed\n"); 1936 1937 return; 1938 } 1939 1940 static int 1941 run_wme_update(struct ieee80211com *ic) 1942 { 1943 struct run_softc *sc = ic->ic_ifp->if_softc; 1944 1945 /* sometime called wothout lock */ 1946 if (mtx_owned(&ic->ic_comlock.mtx)) { 1947 uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store); 1948 DPRINTF("cmdq_store=%d\n", i); 1949 sc->cmdq[i].func = run_wme_update_cb; 1950 sc->cmdq[i].arg0 = ic; 1951 ieee80211_runtask(ic, &sc->cmdq_task); 1952 return (0); 1953 } 1954 1955 RUN_LOCK(sc); 1956 run_wme_update_cb(ic); 1957 RUN_UNLOCK(sc); 1958 1959 /* return whatever, upper layer desn't care anyway */ 1960 return (0); 1961 } 1962 1963 static void 1964 run_key_update_begin(struct ieee80211vap *vap) 1965 { 1966 /* 1967 * To avoid out-of-order events, both run_key_set() and 1968 * _delete() are deferred and handled by run_cmdq_cb(). 1969 * So, there is nothing we need to do here. 1970 */ 1971 } 1972 1973 static void 1974 run_key_update_end(struct ieee80211vap *vap) 1975 { 1976 /* null */ 1977 } 1978 1979 static void 1980 run_key_set_cb(void *arg) 1981 { 1982 struct run_cmdq *cmdq = arg; 1983 struct ieee80211vap *vap = cmdq->arg1; 1984 struct ieee80211_key *k = cmdq->k; 1985 struct ieee80211com *ic = vap->iv_ic; 1986 struct run_softc *sc = ic->ic_ifp->if_softc; 1987 struct ieee80211_node *ni; 1988 uint32_t attr; 1989 uint16_t base, associd; 1990 uint8_t mode, wcid, iv[8]; 1991 1992 RUN_LOCK_ASSERT(sc, MA_OWNED); 1993 1994 if (vap->iv_opmode == IEEE80211_M_HOSTAP) 1995 ni = ieee80211_find_vap_node(&ic->ic_sta, vap, cmdq->mac); 1996 else 1997 ni = vap->iv_bss; 1998 associd = (ni != NULL) ? ni->ni_associd : 0; 1999 2000 /* map net80211 cipher to RT2860 security mode */ 2001 switch (k->wk_cipher->ic_cipher) { 2002 case IEEE80211_CIPHER_WEP: 2003 if(k->wk_keylen < 8) 2004 mode = RT2860_MODE_WEP40; 2005 else 2006 mode = RT2860_MODE_WEP104; 2007 break; 2008 case IEEE80211_CIPHER_TKIP: 2009 mode = RT2860_MODE_TKIP; 2010 break; 2011 case IEEE80211_CIPHER_AES_CCM: 2012 mode = RT2860_MODE_AES_CCMP; 2013 break; 2014 default: 2015 DPRINTF("undefined case\n"); 2016 return; 2017 } 2018 2019 DPRINTFN(1, "associd=%x, keyix=%d, mode=%x, type=%s, tx=%s, rx=%s\n", 2020 associd, k->wk_keyix, mode, 2021 (k->wk_flags & IEEE80211_KEY_GROUP) ? "group" : "pairwise", 2022 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off", 2023 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off"); 2024 2025 if (k->wk_flags & IEEE80211_KEY_GROUP) { 2026 wcid = 0; /* NB: update WCID0 for group keys */ 2027 base = RT2860_SKEY(RUN_VAP(vap)->rvp_id, k->wk_keyix); 2028 } else { 2029 wcid = (vap->iv_opmode == IEEE80211_M_STA) ? 2030 1 : RUN_AID2WCID(associd); 2031 base = RT2860_PKEY(wcid); 2032 } 2033 2034 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) { 2035 if(run_write_region_1(sc, base, k->wk_key, 16)) 2036 return; 2037 if(run_write_region_1(sc, base + 16, &k->wk_key[16], 8)) /* wk_txmic */ 2038 return; 2039 if(run_write_region_1(sc, base + 24, &k->wk_key[24], 8)) /* wk_rxmic */ 2040 return; 2041 } else { 2042 /* roundup len to 16-bit: XXX fix write_region_1() instead */ 2043 if(run_write_region_1(sc, base, k->wk_key, (k->wk_keylen + 1) & ~1)) 2044 return; 2045 } 2046 2047 if (!(k->wk_flags & IEEE80211_KEY_GROUP) || 2048 (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))) { 2049 /* set initial packet number in IV+EIV */ 2050 if (k->wk_cipher == IEEE80211_CIPHER_WEP) { 2051 memset(iv, 0, sizeof iv); 2052 iv[3] = vap->iv_def_txkey << 6; 2053 } else { 2054 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) { 2055 iv[0] = k->wk_keytsc >> 8; 2056 iv[1] = (iv[0] | 0x20) & 0x7f; 2057 iv[2] = k->wk_keytsc; 2058 } else /* CCMP */ { 2059 iv[0] = k->wk_keytsc; 2060 iv[1] = k->wk_keytsc >> 8; 2061 iv[2] = 0; 2062 } 2063 iv[3] = k->wk_keyix << 6 | IEEE80211_WEP_EXTIV; 2064 iv[4] = k->wk_keytsc >> 16; 2065 iv[5] = k->wk_keytsc >> 24; 2066 iv[6] = k->wk_keytsc >> 32; 2067 iv[7] = k->wk_keytsc >> 40; 2068 } 2069 if (run_write_region_1(sc, RT2860_IVEIV(wcid), iv, 8)) 2070 return; 2071 } 2072 2073 if (k->wk_flags & IEEE80211_KEY_GROUP) { 2074 /* install group key */ 2075 if (run_read(sc, RT2860_SKEY_MODE_0_7, &attr)) 2076 return; 2077 attr &= ~(0xf << (k->wk_keyix * 4)); 2078 attr |= mode << (k->wk_keyix * 4); 2079 if (run_write(sc, RT2860_SKEY_MODE_0_7, attr)) 2080 return; 2081 } else { 2082 /* install pairwise key */ 2083 if (run_read(sc, RT2860_WCID_ATTR(wcid), &attr)) 2084 return; 2085 attr = (attr & ~0xf) | (mode << 1) | RT2860_RX_PKEY_EN; 2086 if (run_write(sc, RT2860_WCID_ATTR(wcid), attr)) 2087 return; 2088 } 2089 2090 /* TODO create a pass-thru key entry? */ 2091 2092 /* need wcid to delete the right key later */ 2093 k->wk_pad = wcid; 2094 } 2095 2096 /* 2097 * Don't have to be deferred, but in order to keep order of 2098 * execution, i.e. with run_key_delete(), defer this and let 2099 * run_cmdq_cb() maintain the order. 2100 * 2101 * return 0 on error 2102 */ 2103 static int 2104 run_key_set(struct ieee80211vap *vap, struct ieee80211_key *k, 2105 const uint8_t mac[IEEE80211_ADDR_LEN]) 2106 { 2107 struct ieee80211com *ic = vap->iv_ic; 2108 struct run_softc *sc = ic->ic_ifp->if_softc; 2109 uint32_t i; 2110 2111 i = RUN_CMDQ_GET(&sc->cmdq_store); 2112 DPRINTF("cmdq_store=%d\n", i); 2113 sc->cmdq[i].func = run_key_set_cb; 2114 sc->cmdq[i].arg0 = NULL; 2115 sc->cmdq[i].arg1 = vap; 2116 sc->cmdq[i].k = k; 2117 IEEE80211_ADDR_COPY(sc->cmdq[i].mac, mac); 2118 ieee80211_runtask(ic, &sc->cmdq_task); 2119 2120 /* 2121 * To make sure key will be set when hostapd 2122 * calls iv_key_set() before if_init(). 2123 */ 2124 if (vap->iv_opmode == IEEE80211_M_HOSTAP) { 2125 RUN_LOCK(sc); 2126 sc->cmdq_key_set = RUN_CMDQ_GO; 2127 RUN_UNLOCK(sc); 2128 } 2129 2130 return (1); 2131 } 2132 2133 /* 2134 * If wlan is destroyed without being brought down i.e. without 2135 * wlan down or wpa_cli terminate, this function is called after 2136 * vap is gone. Don't refer it. 2137 */ 2138 static void 2139 run_key_delete_cb(void *arg) 2140 { 2141 struct run_cmdq *cmdq = arg; 2142 struct run_softc *sc = cmdq->arg1; 2143 struct ieee80211_key *k = &cmdq->key; 2144 uint32_t attr; 2145 uint8_t wcid; 2146 2147 RUN_LOCK_ASSERT(sc, MA_OWNED); 2148 2149 if (k->wk_flags & IEEE80211_KEY_GROUP) { 2150 /* remove group key */ 2151 DPRINTF("removing group key\n"); 2152 run_read(sc, RT2860_SKEY_MODE_0_7, &attr); 2153 attr &= ~(0xf << (k->wk_keyix * 4)); 2154 run_write(sc, RT2860_SKEY_MODE_0_7, attr); 2155 } else { 2156 /* remove pairwise key */ 2157 DPRINTF("removing key for wcid %x\n", k->wk_pad); 2158 /* matching wcid was written to wk_pad in run_key_set() */ 2159 wcid = k->wk_pad; 2160 run_read(sc, RT2860_WCID_ATTR(wcid), &attr); 2161 attr &= ~0xf; 2162 run_write(sc, RT2860_WCID_ATTR(wcid), attr); 2163 run_set_region_4(sc, RT2860_WCID_ENTRY(wcid), 0, 8); 2164 } 2165 2166 k->wk_pad = 0; 2167 } 2168 2169 /* 2170 * return 0 on error 2171 */ 2172 static int 2173 run_key_delete(struct ieee80211vap *vap, struct ieee80211_key *k) 2174 { 2175 struct ieee80211com *ic = vap->iv_ic; 2176 struct run_softc *sc = ic->ic_ifp->if_softc; 2177 struct ieee80211_key *k0; 2178 uint32_t i; 2179 2180 /* 2181 * When called back, key might be gone. So, make a copy 2182 * of some values need to delete keys before deferring. 2183 * But, because of LOR with node lock, cannot use lock here. 2184 * So, use atomic instead. 2185 */ 2186 i = RUN_CMDQ_GET(&sc->cmdq_store); 2187 DPRINTF("cmdq_store=%d\n", i); 2188 sc->cmdq[i].func = run_key_delete_cb; 2189 sc->cmdq[i].arg0 = NULL; 2190 sc->cmdq[i].arg1 = sc; 2191 k0 = &sc->cmdq[i].key; 2192 k0->wk_flags = k->wk_flags; 2193 k0->wk_keyix = k->wk_keyix; 2194 /* matching wcid was written to wk_pad in run_key_set() */ 2195 k0->wk_pad = k->wk_pad; 2196 ieee80211_runtask(ic, &sc->cmdq_task); 2197 return (1); /* return fake success */ 2198 2199 } 2200 2201 static void 2202 run_ratectl_to(void *arg) 2203 { 2204 struct run_softc *sc = arg; 2205 2206 /* do it in a process context, so it can go sleep */ 2207 ieee80211_runtask(sc->sc_ifp->if_l2com, &sc->ratectl_task); 2208 /* next timeout will be rescheduled in the callback task */ 2209 } 2210 2211 /* ARGSUSED */ 2212 static void 2213 run_ratectl_cb(void *arg, int pending) 2214 { 2215 struct run_softc *sc = arg; 2216 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 2217 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2218 2219 if (vap == NULL) 2220 return; 2221 2222 if (sc->rvp_cnt <= 1 && vap->iv_opmode == IEEE80211_M_STA) 2223 run_iter_func(sc, vap->iv_bss); 2224 else { 2225 /* 2226 * run_reset_livelock() doesn't do anything with AMRR, 2227 * but Ralink wants us to call it every 1 sec. So, we 2228 * piggyback here rather than creating another callout. 2229 * Livelock may occur only in HOSTAP or IBSS mode 2230 * (when h/w is sending beacons). 2231 */ 2232 RUN_LOCK(sc); 2233 run_reset_livelock(sc); 2234 /* just in case, there are some stats to drain */ 2235 run_drain_fifo(sc); 2236 RUN_UNLOCK(sc); 2237 ieee80211_iterate_nodes(&ic->ic_sta, run_iter_func, sc); 2238 } 2239 2240 if(sc->ratectl_run != RUN_RATECTL_OFF) 2241 usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc); 2242 } 2243 2244 static void 2245 run_drain_fifo(void *arg) 2246 { 2247 struct run_softc *sc = arg; 2248 struct ifnet *ifp = sc->sc_ifp; 2249 uint32_t stat; 2250 uint16_t (*wstat)[3]; 2251 uint8_t wcid, mcs, pid; 2252 int8_t retry; 2253 2254 RUN_LOCK_ASSERT(sc, MA_OWNED); 2255 2256 for (;;) { 2257 /* drain Tx status FIFO (maxsize = 16) */ 2258 run_read(sc, RT2860_TX_STAT_FIFO, &stat); 2259 DPRINTFN(4, "tx stat 0x%08x\n", stat); 2260 if (!(stat & RT2860_TXQ_VLD)) 2261 break; 2262 2263 wcid = (stat >> RT2860_TXQ_WCID_SHIFT) & 0xff; 2264 2265 /* if no ACK was requested, no feedback is available */ 2266 if (!(stat & RT2860_TXQ_ACKREQ) || wcid > RT2870_WCID_MAX || 2267 wcid == 0) 2268 continue; 2269 2270 /* 2271 * Even though each stat is Tx-complete-status like format, 2272 * the device can poll stats. Because there is no guarantee 2273 * that the referring node is still around when read the stats. 2274 * So that, if we use ieee80211_ratectl_tx_update(), we will 2275 * have hard time not to refer already freed node. 2276 * 2277 * To eliminate such page faults, we poll stats in softc. 2278 * Then, update the rates later with ieee80211_ratectl_tx_update(). 2279 */ 2280 wstat = &(sc->wcid_stats[wcid]); 2281 (*wstat)[RUN_TXCNT]++; 2282 if (stat & RT2860_TXQ_OK) 2283 (*wstat)[RUN_SUCCESS]++; 2284 else 2285 ifp->if_oerrors++; 2286 /* 2287 * Check if there were retries, ie if the Tx success rate is 2288 * different from the requested rate. Note that it works only 2289 * because we do not allow rate fallback from OFDM to CCK. 2290 */ 2291 mcs = (stat >> RT2860_TXQ_MCS_SHIFT) & 0x7f; 2292 pid = (stat >> RT2860_TXQ_PID_SHIFT) & 0xf; 2293 if ((retry = pid -1 - mcs) > 0) { 2294 (*wstat)[RUN_TXCNT] += retry; 2295 (*wstat)[RUN_RETRY] += retry; 2296 } 2297 } 2298 DPRINTFN(3, "count=%d\n", sc->fifo_cnt); 2299 2300 sc->fifo_cnt = 0; 2301 } 2302 2303 static void 2304 run_iter_func(void *arg, struct ieee80211_node *ni) 2305 { 2306 struct run_softc *sc = arg; 2307 struct ieee80211vap *vap = ni->ni_vap; 2308 struct ieee80211com *ic = ni->ni_ic; 2309 struct ifnet *ifp = ic->ic_ifp; 2310 struct run_node *rn = (void *)ni; 2311 union run_stats sta[2]; 2312 uint16_t (*wstat)[3]; 2313 int txcnt, success, retrycnt, error; 2314 2315 RUN_LOCK(sc); 2316 2317 if (sc->rvp_cnt <= 1 && (vap->iv_opmode == IEEE80211_M_IBSS || 2318 vap->iv_opmode == IEEE80211_M_STA)) { 2319 /* read statistic counters (clear on read) and update AMRR state */ 2320 error = run_read_region_1(sc, RT2860_TX_STA_CNT0, (uint8_t *)sta, 2321 sizeof sta); 2322 if (error != 0) 2323 goto fail; 2324 2325 /* count failed TX as errors */ 2326 ifp->if_oerrors += le16toh(sta[0].error.fail); 2327 2328 retrycnt = le16toh(sta[1].tx.retry); 2329 success = le16toh(sta[1].tx.success); 2330 txcnt = retrycnt + success + le16toh(sta[0].error.fail); 2331 2332 DPRINTFN(3, "retrycnt=%d success=%d failcnt=%d\n", 2333 retrycnt, success, le16toh(sta[0].error.fail)); 2334 } else { 2335 wstat = &(sc->wcid_stats[RUN_AID2WCID(ni->ni_associd)]); 2336 2337 if (wstat == &(sc->wcid_stats[0]) || 2338 wstat > &(sc->wcid_stats[RT2870_WCID_MAX])) 2339 goto fail; 2340 2341 txcnt = (*wstat)[RUN_TXCNT]; 2342 success = (*wstat)[RUN_SUCCESS]; 2343 retrycnt = (*wstat)[RUN_RETRY]; 2344 DPRINTFN(3, "retrycnt=%d txcnt=%d success=%d\n", 2345 retrycnt, txcnt, success); 2346 2347 memset(wstat, 0, sizeof(*wstat)); 2348 } 2349 2350 ieee80211_ratectl_tx_update(vap, ni, &txcnt, &success, &retrycnt); 2351 rn->amrr_ridx = ieee80211_ratectl_rate(ni, NULL, 0); 2352 2353 fail: 2354 RUN_UNLOCK(sc); 2355 2356 DPRINTFN(3, "ridx=%d\n", rn->amrr_ridx); 2357 } 2358 2359 static void 2360 run_newassoc_cb(void *arg) 2361 { 2362 struct run_cmdq *cmdq = arg; 2363 struct ieee80211_node *ni = cmdq->arg1; 2364 struct run_softc *sc = ni->ni_vap->iv_ic->ic_ifp->if_softc; 2365 uint8_t wcid = cmdq->wcid; 2366 2367 RUN_LOCK_ASSERT(sc, MA_OWNED); 2368 2369 run_write_region_1(sc, RT2860_WCID_ENTRY(wcid), 2370 ni->ni_macaddr, IEEE80211_ADDR_LEN); 2371 2372 memset(&(sc->wcid_stats[wcid]), 0, sizeof(sc->wcid_stats[wcid])); 2373 } 2374 2375 static void 2376 run_newassoc(struct ieee80211_node *ni, int isnew) 2377 { 2378 struct run_node *rn = (void *)ni; 2379 struct ieee80211_rateset *rs = &ni->ni_rates; 2380 struct ieee80211vap *vap = ni->ni_vap; 2381 struct ieee80211com *ic = vap->iv_ic; 2382 struct run_softc *sc = ic->ic_ifp->if_softc; 2383 uint8_t rate; 2384 uint8_t ridx; 2385 uint8_t wcid; 2386 int i, j; 2387 2388 wcid = (vap->iv_opmode == IEEE80211_M_STA) ? 2389 1 : RUN_AID2WCID(ni->ni_associd); 2390 2391 if (wcid > RT2870_WCID_MAX) { 2392 device_printf(sc->sc_dev, "wcid=%d out of range\n", wcid); 2393 return; 2394 } 2395 2396 /* only interested in true associations */ 2397 if (isnew && ni->ni_associd != 0) { 2398 2399 /* 2400 * This function could is called though timeout function. 2401 * Need to defer. 2402 */ 2403 uint32_t cnt = RUN_CMDQ_GET(&sc->cmdq_store); 2404 DPRINTF("cmdq_store=%d\n", cnt); 2405 sc->cmdq[cnt].func = run_newassoc_cb; 2406 sc->cmdq[cnt].arg0 = NULL; 2407 sc->cmdq[cnt].arg1 = ni; 2408 sc->cmdq[cnt].wcid = wcid; 2409 ieee80211_runtask(ic, &sc->cmdq_task); 2410 } 2411 2412 DPRINTF("new assoc isnew=%d associd=%x addr=%s\n", 2413 isnew, ni->ni_associd, ether_sprintf(ni->ni_macaddr)); 2414 2415 for (i = 0; i < rs->rs_nrates; i++) { 2416 rate = rs->rs_rates[i] & IEEE80211_RATE_VAL; 2417 /* convert 802.11 rate to hardware rate index */ 2418 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++) 2419 if (rt2860_rates[ridx].rate == rate) 2420 break; 2421 rn->ridx[i] = ridx; 2422 /* determine rate of control response frames */ 2423 for (j = i; j >= 0; j--) { 2424 if ((rs->rs_rates[j] & IEEE80211_RATE_BASIC) && 2425 rt2860_rates[rn->ridx[i]].phy == 2426 rt2860_rates[rn->ridx[j]].phy) 2427 break; 2428 } 2429 if (j >= 0) { 2430 rn->ctl_ridx[i] = rn->ridx[j]; 2431 } else { 2432 /* no basic rate found, use mandatory one */ 2433 rn->ctl_ridx[i] = rt2860_rates[ridx].ctl_ridx; 2434 } 2435 DPRINTF("rate=0x%02x ridx=%d ctl_ridx=%d\n", 2436 rs->rs_rates[i], rn->ridx[i], rn->ctl_ridx[i]); 2437 } 2438 rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate; 2439 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++) 2440 if (rt2860_rates[ridx].rate == rate) 2441 break; 2442 rn->mgt_ridx = ridx; 2443 DPRINTF("rate=%d, mgmt_ridx=%d\n", rate, rn->mgt_ridx); 2444 2445 usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc); 2446 } 2447 2448 /* 2449 * Return the Rx chain with the highest RSSI for a given frame. 2450 */ 2451 static __inline uint8_t 2452 run_maxrssi_chain(struct run_softc *sc, const struct rt2860_rxwi *rxwi) 2453 { 2454 uint8_t rxchain = 0; 2455 2456 if (sc->nrxchains > 1) { 2457 if (rxwi->rssi[1] > rxwi->rssi[rxchain]) 2458 rxchain = 1; 2459 if (sc->nrxchains > 2) 2460 if (rxwi->rssi[2] > rxwi->rssi[rxchain]) 2461 rxchain = 2; 2462 } 2463 return (rxchain); 2464 } 2465 2466 static void 2467 run_rx_frame(struct run_softc *sc, struct mbuf *m, uint32_t dmalen) 2468 { 2469 struct ifnet *ifp = sc->sc_ifp; 2470 struct ieee80211com *ic = ifp->if_l2com; 2471 struct ieee80211_frame *wh; 2472 struct ieee80211_node *ni; 2473 struct rt2870_rxd *rxd; 2474 struct rt2860_rxwi *rxwi; 2475 uint32_t flags; 2476 uint16_t len, phy; 2477 uint8_t ant, rssi; 2478 int8_t nf; 2479 2480 rxwi = mtod(m, struct rt2860_rxwi *); 2481 len = le16toh(rxwi->len) & 0xfff; 2482 if (__predict_false(len > dmalen)) { 2483 m_freem(m); 2484 ifp->if_ierrors++; 2485 DPRINTF("bad RXWI length %u > %u\n", len, dmalen); 2486 return; 2487 } 2488 /* Rx descriptor is located at the end */ 2489 rxd = (struct rt2870_rxd *)(mtod(m, caddr_t) + dmalen); 2490 flags = le32toh(rxd->flags); 2491 2492 if (__predict_false(flags & (RT2860_RX_CRCERR | RT2860_RX_ICVERR))) { 2493 m_freem(m); 2494 ifp->if_ierrors++; 2495 DPRINTF("%s error.\n", (flags & RT2860_RX_CRCERR)?"CRC":"ICV"); 2496 return; 2497 } 2498 2499 m->m_data += sizeof(struct rt2860_rxwi); 2500 m->m_pkthdr.len = m->m_len -= sizeof(struct rt2860_rxwi); 2501 2502 wh = mtod(m, struct ieee80211_frame *); 2503 2504 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 2505 wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 2506 m->m_flags |= M_WEP; 2507 } 2508 2509 if (flags & RT2860_RX_L2PAD) { 2510 DPRINTFN(8, "received RT2860_RX_L2PAD frame\n"); 2511 len += 2; 2512 } 2513 2514 ni = ieee80211_find_rxnode(ic, 2515 mtod(m, struct ieee80211_frame_min *)); 2516 2517 if (__predict_false(flags & RT2860_RX_MICERR)) { 2518 /* report MIC failures to net80211 for TKIP */ 2519 if (ni != NULL) 2520 ieee80211_notify_michael_failure(ni->ni_vap, wh, rxwi->keyidx); 2521 m_freem(m); 2522 ifp->if_ierrors++; 2523 DPRINTF("MIC error. Someone is lying.\n"); 2524 return; 2525 } 2526 2527 ant = run_maxrssi_chain(sc, rxwi); 2528 rssi = rxwi->rssi[ant]; 2529 nf = run_rssi2dbm(sc, rssi, ant); 2530 2531 m->m_pkthdr.rcvif = ifp; 2532 m->m_pkthdr.len = m->m_len = len; 2533 2534 if (ni != NULL) { 2535 (void)ieee80211_input(ni, m, rssi, nf); 2536 ieee80211_free_node(ni); 2537 } else { 2538 (void)ieee80211_input_all(ic, m, rssi, nf); 2539 } 2540 2541 if (__predict_false(ieee80211_radiotap_active(ic))) { 2542 struct run_rx_radiotap_header *tap = &sc->sc_rxtap; 2543 2544 tap->wr_flags = 0; 2545 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq); 2546 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags); 2547 tap->wr_antsignal = rssi; 2548 tap->wr_antenna = ant; 2549 tap->wr_dbm_antsignal = run_rssi2dbm(sc, rssi, ant); 2550 tap->wr_rate = 2; /* in case it can't be found below */ 2551 phy = le16toh(rxwi->phy); 2552 switch (phy & RT2860_PHY_MODE) { 2553 case RT2860_PHY_CCK: 2554 switch ((phy & RT2860_PHY_MCS) & ~RT2860_PHY_SHPRE) { 2555 case 0: tap->wr_rate = 2; break; 2556 case 1: tap->wr_rate = 4; break; 2557 case 2: tap->wr_rate = 11; break; 2558 case 3: tap->wr_rate = 22; break; 2559 } 2560 if (phy & RT2860_PHY_SHPRE) 2561 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 2562 break; 2563 case RT2860_PHY_OFDM: 2564 switch (phy & RT2860_PHY_MCS) { 2565 case 0: tap->wr_rate = 12; break; 2566 case 1: tap->wr_rate = 18; break; 2567 case 2: tap->wr_rate = 24; break; 2568 case 3: tap->wr_rate = 36; break; 2569 case 4: tap->wr_rate = 48; break; 2570 case 5: tap->wr_rate = 72; break; 2571 case 6: tap->wr_rate = 96; break; 2572 case 7: tap->wr_rate = 108; break; 2573 } 2574 break; 2575 } 2576 } 2577 } 2578 2579 static void 2580 run_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error) 2581 { 2582 struct run_softc *sc = usbd_xfer_softc(xfer); 2583 struct ifnet *ifp = sc->sc_ifp; 2584 struct mbuf *m = NULL; 2585 struct mbuf *m0; 2586 uint32_t dmalen; 2587 int xferlen; 2588 2589 usbd_xfer_status(xfer, &xferlen, NULL, NULL, NULL); 2590 2591 switch (USB_GET_STATE(xfer)) { 2592 case USB_ST_TRANSFERRED: 2593 2594 DPRINTFN(15, "rx done, actlen=%d\n", xferlen); 2595 2596 if (xferlen < (int)(sizeof(uint32_t) + 2597 sizeof(struct rt2860_rxwi) + sizeof(struct rt2870_rxd))) { 2598 DPRINTF("xfer too short %d\n", xferlen); 2599 goto tr_setup; 2600 } 2601 2602 m = sc->rx_m; 2603 sc->rx_m = NULL; 2604 2605 /* FALLTHROUGH */ 2606 case USB_ST_SETUP: 2607 tr_setup: 2608 if (sc->rx_m == NULL) { 2609 sc->rx_m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, 2610 MJUMPAGESIZE /* xfer can be bigger than MCLBYTES */); 2611 } 2612 if (sc->rx_m == NULL) { 2613 DPRINTF("could not allocate mbuf - idle with stall\n"); 2614 ifp->if_ierrors++; 2615 usbd_xfer_set_stall(xfer); 2616 usbd_xfer_set_frames(xfer, 0); 2617 } else { 2618 /* 2619 * Directly loading a mbuf cluster into DMA to 2620 * save some data copying. This works because 2621 * there is only one cluster. 2622 */ 2623 usbd_xfer_set_frame_data(xfer, 0, 2624 mtod(sc->rx_m, caddr_t), RUN_MAX_RXSZ); 2625 usbd_xfer_set_frames(xfer, 1); 2626 } 2627 usbd_transfer_submit(xfer); 2628 break; 2629 2630 default: /* Error */ 2631 if (error != USB_ERR_CANCELLED) { 2632 /* try to clear stall first */ 2633 usbd_xfer_set_stall(xfer); 2634 2635 if (error == USB_ERR_TIMEOUT) 2636 device_printf(sc->sc_dev, "device timeout\n"); 2637 2638 ifp->if_ierrors++; 2639 2640 goto tr_setup; 2641 } 2642 if (sc->rx_m != NULL) { 2643 m_freem(sc->rx_m); 2644 sc->rx_m = NULL; 2645 } 2646 break; 2647 } 2648 2649 if (m == NULL) 2650 return; 2651 2652 /* inputting all the frames must be last */ 2653 2654 RUN_UNLOCK(sc); 2655 2656 m->m_pkthdr.len = m->m_len = xferlen; 2657 2658 /* HW can aggregate multiple 802.11 frames in a single USB xfer */ 2659 for(;;) { 2660 dmalen = le32toh(*mtod(m, uint32_t *)) & 0xffff; 2661 2662 if ((dmalen >= (uint32_t)-8) || (dmalen == 0) || 2663 ((dmalen & 3) != 0)) { 2664 DPRINTF("bad DMA length %u\n", dmalen); 2665 break; 2666 } 2667 if ((dmalen + 8) > (uint32_t)xferlen) { 2668 DPRINTF("bad DMA length %u > %d\n", 2669 dmalen + 8, xferlen); 2670 break; 2671 } 2672 2673 /* If it is the last one or a single frame, we won't copy. */ 2674 if ((xferlen -= dmalen + 8) <= 8) { 2675 /* trim 32-bit DMA-len header */ 2676 m->m_data += 4; 2677 m->m_pkthdr.len = m->m_len -= 4; 2678 run_rx_frame(sc, m, dmalen); 2679 break; 2680 } 2681 2682 /* copy aggregated frames to another mbuf */ 2683 m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 2684 if (__predict_false(m0 == NULL)) { 2685 DPRINTF("could not allocate mbuf\n"); 2686 ifp->if_ierrors++; 2687 break; 2688 } 2689 m_copydata(m, 4 /* skip 32-bit DMA-len header */, 2690 dmalen + sizeof(struct rt2870_rxd), mtod(m0, caddr_t)); 2691 m0->m_pkthdr.len = m0->m_len = 2692 dmalen + sizeof(struct rt2870_rxd); 2693 run_rx_frame(sc, m0, dmalen); 2694 2695 /* update data ptr */ 2696 m->m_data += dmalen + 8; 2697 m->m_pkthdr.len = m->m_len -= dmalen + 8; 2698 } 2699 2700 RUN_LOCK(sc); 2701 } 2702 2703 static void 2704 run_tx_free(struct run_endpoint_queue *pq, 2705 struct run_tx_data *data, int txerr) 2706 { 2707 if (data->m != NULL) { 2708 if (data->m->m_flags & M_TXCB) 2709 ieee80211_process_callback(data->ni, data->m, 2710 txerr ? ETIMEDOUT : 0); 2711 m_freem(data->m); 2712 data->m = NULL; 2713 2714 if (data->ni == NULL) { 2715 DPRINTF("no node\n"); 2716 } else { 2717 ieee80211_free_node(data->ni); 2718 data->ni = NULL; 2719 } 2720 } 2721 2722 STAILQ_INSERT_TAIL(&pq->tx_fh, data, next); 2723 pq->tx_nfree++; 2724 } 2725 2726 static void 2727 run_bulk_tx_callbackN(struct usb_xfer *xfer, usb_error_t error, unsigned int index) 2728 { 2729 struct run_softc *sc = usbd_xfer_softc(xfer); 2730 struct ifnet *ifp = sc->sc_ifp; 2731 struct ieee80211com *ic = ifp->if_l2com; 2732 struct run_tx_data *data; 2733 struct ieee80211vap *vap = NULL; 2734 struct usb_page_cache *pc; 2735 struct run_endpoint_queue *pq = &sc->sc_epq[index]; 2736 struct mbuf *m; 2737 usb_frlength_t size; 2738 int actlen; 2739 int sumlen; 2740 2741 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 2742 2743 switch (USB_GET_STATE(xfer)) { 2744 case USB_ST_TRANSFERRED: 2745 DPRINTFN(11, "transfer complete: %d " 2746 "bytes @ index %d\n", actlen, index); 2747 2748 data = usbd_xfer_get_priv(xfer); 2749 2750 run_tx_free(pq, data, 0); 2751 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2752 2753 usbd_xfer_set_priv(xfer, NULL); 2754 2755 ifp->if_opackets++; 2756 2757 /* FALLTHROUGH */ 2758 case USB_ST_SETUP: 2759 tr_setup: 2760 data = STAILQ_FIRST(&pq->tx_qh); 2761 if (data == NULL) 2762 break; 2763 2764 STAILQ_REMOVE_HEAD(&pq->tx_qh, next); 2765 2766 m = data->m; 2767 if ((m->m_pkthdr.len + 2768 sizeof(data->desc) + 3 + 8) > RUN_MAX_TXSZ) { 2769 DPRINTF("data overflow, %u bytes\n", 2770 m->m_pkthdr.len); 2771 2772 ifp->if_oerrors++; 2773 2774 run_tx_free(pq, data, 1); 2775 2776 goto tr_setup; 2777 } 2778 2779 pc = usbd_xfer_get_frame(xfer, 0); 2780 size = sizeof(data->desc); 2781 usbd_copy_in(pc, 0, &data->desc, size); 2782 usbd_m_copy_in(pc, size, m, 0, m->m_pkthdr.len); 2783 size += m->m_pkthdr.len; 2784 /* 2785 * Align end on a 4-byte boundary, pad 8 bytes (CRC + 2786 * 4-byte padding), and be sure to zero those trailing 2787 * bytes: 2788 */ 2789 usbd_frame_zero(pc, size, ((-size) & 3) + 8); 2790 size += ((-size) & 3) + 8; 2791 2792 vap = data->ni->ni_vap; 2793 if (ieee80211_radiotap_active_vap(vap)) { 2794 struct run_tx_radiotap_header *tap = &sc->sc_txtap; 2795 struct rt2860_txwi *txwi = 2796 (struct rt2860_txwi *)(&data->desc + sizeof(struct rt2870_txd)); 2797 2798 tap->wt_flags = 0; 2799 tap->wt_rate = rt2860_rates[data->ridx].rate; 2800 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 2801 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 2802 tap->wt_hwqueue = index; 2803 if (le16toh(txwi->phy) & RT2860_PHY_SHPRE) 2804 tap->wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 2805 2806 ieee80211_radiotap_tx(vap, m); 2807 } 2808 2809 DPRINTFN(11, "sending frame len=%u/%u @ index %d\n", 2810 m->m_pkthdr.len, size, index); 2811 2812 usbd_xfer_set_frame_len(xfer, 0, size); 2813 usbd_xfer_set_priv(xfer, data); 2814 2815 usbd_transfer_submit(xfer); 2816 2817 RUN_UNLOCK(sc); 2818 run_start(ifp); 2819 RUN_LOCK(sc); 2820 2821 break; 2822 2823 default: 2824 DPRINTF("USB transfer error, %s\n", 2825 usbd_errstr(error)); 2826 2827 data = usbd_xfer_get_priv(xfer); 2828 2829 ifp->if_oerrors++; 2830 2831 if (data != NULL) { 2832 if(data->ni != NULL) 2833 vap = data->ni->ni_vap; 2834 run_tx_free(pq, data, error); 2835 usbd_xfer_set_priv(xfer, NULL); 2836 } 2837 if (vap == NULL) 2838 vap = TAILQ_FIRST(&ic->ic_vaps); 2839 2840 if (error != USB_ERR_CANCELLED) { 2841 if (error == USB_ERR_TIMEOUT) { 2842 device_printf(sc->sc_dev, "device timeout\n"); 2843 uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store); 2844 DPRINTF("cmdq_store=%d\n", i); 2845 sc->cmdq[i].func = run_usb_timeout_cb; 2846 sc->cmdq[i].arg0 = vap; 2847 ieee80211_runtask(ic, &sc->cmdq_task); 2848 } 2849 2850 /* 2851 * Try to clear stall first, also if other 2852 * errors occur, hence clearing stall 2853 * introduces a 50 ms delay: 2854 */ 2855 usbd_xfer_set_stall(xfer); 2856 goto tr_setup; 2857 } 2858 break; 2859 } 2860 } 2861 2862 static void 2863 run_bulk_tx_callback0(struct usb_xfer *xfer, usb_error_t error) 2864 { 2865 run_bulk_tx_callbackN(xfer, error, 0); 2866 } 2867 2868 static void 2869 run_bulk_tx_callback1(struct usb_xfer *xfer, usb_error_t error) 2870 { 2871 run_bulk_tx_callbackN(xfer, error, 1); 2872 } 2873 2874 static void 2875 run_bulk_tx_callback2(struct usb_xfer *xfer, usb_error_t error) 2876 { 2877 run_bulk_tx_callbackN(xfer, error, 2); 2878 } 2879 2880 static void 2881 run_bulk_tx_callback3(struct usb_xfer *xfer, usb_error_t error) 2882 { 2883 run_bulk_tx_callbackN(xfer, error, 3); 2884 } 2885 2886 static void 2887 run_bulk_tx_callback4(struct usb_xfer *xfer, usb_error_t error) 2888 { 2889 run_bulk_tx_callbackN(xfer, error, 4); 2890 } 2891 2892 static void 2893 run_bulk_tx_callback5(struct usb_xfer *xfer, usb_error_t error) 2894 { 2895 run_bulk_tx_callbackN(xfer, error, 5); 2896 } 2897 2898 static void 2899 run_set_tx_desc(struct run_softc *sc, struct run_tx_data *data) 2900 { 2901 struct mbuf *m = data->m; 2902 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 2903 struct ieee80211vap *vap = data->ni->ni_vap; 2904 struct ieee80211_frame *wh; 2905 struct rt2870_txd *txd; 2906 struct rt2860_txwi *txwi; 2907 uint16_t xferlen; 2908 uint16_t mcs; 2909 uint8_t ridx = data->ridx; 2910 uint8_t pad; 2911 2912 /* get MCS code from rate index */ 2913 mcs = rt2860_rates[ridx].mcs; 2914 2915 xferlen = sizeof(*txwi) + m->m_pkthdr.len; 2916 2917 /* roundup to 32-bit alignment */ 2918 xferlen = (xferlen + 3) & ~3; 2919 2920 txd = (struct rt2870_txd *)&data->desc; 2921 txd->len = htole16(xferlen); 2922 2923 wh = mtod(m, struct ieee80211_frame *); 2924 2925 /* 2926 * Ether both are true or both are false, the header 2927 * are nicely aligned to 32-bit. So, no L2 padding. 2928 */ 2929 if(IEEE80211_HAS_ADDR4(wh) == IEEE80211_QOS_HAS_SEQ(wh)) 2930 pad = 0; 2931 else 2932 pad = 2; 2933 2934 /* setup TX Wireless Information */ 2935 txwi = (struct rt2860_txwi *)(txd + 1); 2936 txwi->len = htole16(m->m_pkthdr.len - pad); 2937 if (rt2860_rates[ridx].phy == IEEE80211_T_DS) { 2938 txwi->phy = htole16(RT2860_PHY_CCK); 2939 if (ridx != RT2860_RIDX_CCK1 && 2940 (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 2941 mcs |= RT2860_PHY_SHPRE; 2942 } else 2943 txwi->phy = htole16(RT2860_PHY_OFDM); 2944 txwi->phy |= htole16(mcs); 2945 2946 /* check if RTS/CTS or CTS-to-self protection is required */ 2947 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) && 2948 (m->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold || 2949 ((ic->ic_flags & IEEE80211_F_USEPROT) && 2950 rt2860_rates[ridx].phy == IEEE80211_T_OFDM))) 2951 txwi->txop |= RT2860_TX_TXOP_HT; 2952 else 2953 txwi->txop |= RT2860_TX_TXOP_BACKOFF; 2954 2955 if (vap->iv_opmode != IEEE80211_M_STA && !IEEE80211_QOS_HAS_SEQ(wh)) 2956 txwi->xflags |= RT2860_TX_NSEQ; 2957 } 2958 2959 /* This function must be called locked */ 2960 static int 2961 run_tx(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni) 2962 { 2963 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 2964 struct ieee80211vap *vap = ni->ni_vap; 2965 struct ieee80211_frame *wh; 2966 struct ieee80211_channel *chan; 2967 const struct ieee80211_txparam *tp; 2968 struct run_node *rn = (void *)ni; 2969 struct run_tx_data *data; 2970 struct rt2870_txd *txd; 2971 struct rt2860_txwi *txwi; 2972 uint16_t qos; 2973 uint16_t dur; 2974 uint16_t qid; 2975 uint8_t type; 2976 uint8_t tid; 2977 uint8_t ridx; 2978 uint8_t ctl_ridx; 2979 uint8_t qflags; 2980 uint8_t xflags = 0; 2981 int hasqos; 2982 2983 RUN_LOCK_ASSERT(sc, MA_OWNED); 2984 2985 wh = mtod(m, struct ieee80211_frame *); 2986 2987 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 2988 2989 /* 2990 * There are 7 bulk endpoints: 1 for RX 2991 * and 6 for TX (4 EDCAs + HCCA + Prio). 2992 * Update 03-14-2009: some devices like the Planex GW-US300MiniS 2993 * seem to have only 4 TX bulk endpoints (Fukaumi Naoki). 2994 */ 2995 if ((hasqos = IEEE80211_QOS_HAS_SEQ(wh))) { 2996 uint8_t *frm; 2997 2998 if(IEEE80211_HAS_ADDR4(wh)) 2999 frm = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos; 3000 else 3001 frm =((struct ieee80211_qosframe *)wh)->i_qos; 3002 3003 qos = le16toh(*(const uint16_t *)frm); 3004 tid = qos & IEEE80211_QOS_TID; 3005 qid = TID_TO_WME_AC(tid); 3006 } else { 3007 qos = 0; 3008 tid = 0; 3009 qid = WME_AC_BE; 3010 } 3011 qflags = (qid < 4) ? RT2860_TX_QSEL_EDCA : RT2860_TX_QSEL_HCCA; 3012 3013 DPRINTFN(8, "qos %d\tqid %d\ttid %d\tqflags %x\n", 3014 qos, qid, tid, qflags); 3015 3016 chan = (ni->ni_chan != IEEE80211_CHAN_ANYC)?ni->ni_chan:ic->ic_curchan; 3017 tp = &vap->iv_txparms[ieee80211_chan2mode(chan)]; 3018 3019 /* pickup a rate index */ 3020 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 3021 type != IEEE80211_FC0_TYPE_DATA) { 3022 ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ? 3023 RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1; 3024 ctl_ridx = rt2860_rates[ridx].ctl_ridx; 3025 } else { 3026 if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 3027 ridx = rn->fix_ridx; 3028 else 3029 ridx = rn->amrr_ridx; 3030 ctl_ridx = rt2860_rates[ridx].ctl_ridx; 3031 } 3032 3033 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) && 3034 (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) != 3035 IEEE80211_QOS_ACKPOLICY_NOACK)) { 3036 xflags |= RT2860_TX_ACK; 3037 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 3038 dur = rt2860_rates[ctl_ridx].sp_ack_dur; 3039 else 3040 dur = rt2860_rates[ctl_ridx].lp_ack_dur; 3041 *(uint16_t *)wh->i_dur = htole16(dur); 3042 } 3043 3044 /* reserve slots for mgmt packets, just in case */ 3045 if (sc->sc_epq[qid].tx_nfree < 3) { 3046 DPRINTFN(10, "tx ring %d is full\n", qid); 3047 return (-1); 3048 } 3049 3050 data = STAILQ_FIRST(&sc->sc_epq[qid].tx_fh); 3051 STAILQ_REMOVE_HEAD(&sc->sc_epq[qid].tx_fh, next); 3052 sc->sc_epq[qid].tx_nfree--; 3053 3054 txd = (struct rt2870_txd *)&data->desc; 3055 txd->flags = qflags; 3056 txwi = (struct rt2860_txwi *)(txd + 1); 3057 txwi->xflags = xflags; 3058 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 3059 txwi->wcid = 0; 3060 } else { 3061 txwi->wcid = (vap->iv_opmode == IEEE80211_M_STA) ? 3062 1 : RUN_AID2WCID(ni->ni_associd); 3063 } 3064 /* clear leftover garbage bits */ 3065 txwi->flags = 0; 3066 txwi->txop = 0; 3067 3068 data->m = m; 3069 data->ni = ni; 3070 data->ridx = ridx; 3071 3072 run_set_tx_desc(sc, data); 3073 3074 /* 3075 * The chip keeps track of 2 kind of Tx stats, 3076 * * TX_STAT_FIFO, for per WCID stats, and 3077 * * TX_STA_CNT0 for all-TX-in-one stats. 3078 * 3079 * To use FIFO stats, we need to store MCS into the driver-private 3080 * PacketID field. So that, we can tell whose stats when we read them. 3081 * We add 1 to the MCS because setting the PacketID field to 0 means 3082 * that we don't want feedback in TX_STAT_FIFO. 3083 * And, that's what we want for STA mode, since TX_STA_CNT0 does the job. 3084 * 3085 * FIFO stats doesn't count Tx with WCID 0xff, so we do this in run_tx(). 3086 */ 3087 if (sc->rvp_cnt > 1 || vap->iv_opmode == IEEE80211_M_HOSTAP || 3088 vap->iv_opmode == IEEE80211_M_MBSS) { 3089 uint16_t pid = (rt2860_rates[ridx].mcs + 1) & 0xf; 3090 txwi->len |= htole16(pid << RT2860_TX_PID_SHIFT); 3091 3092 /* 3093 * Unlike PCI based devices, we don't get any interrupt from 3094 * USB devices, so we simulate FIFO-is-full interrupt here. 3095 * Ralink recomends to drain FIFO stats every 100 ms, but 16 slots 3096 * quickly get fulled. To prevent overflow, increment a counter on 3097 * every FIFO stat request, so we know how many slots are left. 3098 * We do this only in HOSTAP or multiple vap mode since FIFO stats 3099 * are used only in those modes. 3100 * We just drain stats. AMRR gets updated every 1 sec by 3101 * run_ratectl_cb() via callout. 3102 * Call it early. Otherwise overflow. 3103 */ 3104 if (sc->fifo_cnt++ == 10) { 3105 /* 3106 * With multiple vaps or if_bridge, if_start() is called 3107 * with a non-sleepable lock, tcpinp. So, need to defer. 3108 */ 3109 uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store); 3110 DPRINTFN(6, "cmdq_store=%d\n", i); 3111 sc->cmdq[i].func = run_drain_fifo; 3112 sc->cmdq[i].arg0 = sc; 3113 ieee80211_runtask(ic, &sc->cmdq_task); 3114 } 3115 } 3116 3117 STAILQ_INSERT_TAIL(&sc->sc_epq[qid].tx_qh, data, next); 3118 3119 usbd_transfer_start(sc->sc_xfer[qid]); 3120 3121 DPRINTFN(8, "sending data frame len=%d rate=%d qid=%d\n", m->m_pkthdr.len + 3122 (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)), 3123 rt2860_rates[ridx].rate, qid); 3124 3125 return (0); 3126 } 3127 3128 static int 3129 run_tx_mgt(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni) 3130 { 3131 struct ifnet *ifp = sc->sc_ifp; 3132 struct ieee80211com *ic = ifp->if_l2com; 3133 struct run_node *rn = (void *)ni; 3134 struct run_tx_data *data; 3135 struct ieee80211_frame *wh; 3136 struct rt2870_txd *txd; 3137 struct rt2860_txwi *txwi; 3138 uint16_t dur; 3139 uint8_t ridx = rn->mgt_ridx; 3140 uint8_t type; 3141 uint8_t xflags = 0; 3142 uint8_t wflags = 0; 3143 3144 RUN_LOCK_ASSERT(sc, MA_OWNED); 3145 3146 wh = mtod(m, struct ieee80211_frame *); 3147 3148 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 3149 3150 /* tell hardware to add timestamp for probe responses */ 3151 if ((wh->i_fc[0] & 3152 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 3153 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP)) 3154 wflags |= RT2860_TX_TS; 3155 else if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 3156 xflags |= RT2860_TX_ACK; 3157 3158 dur = ieee80211_ack_duration(ic->ic_rt, rt2860_rates[ridx].rate, 3159 ic->ic_flags & IEEE80211_F_SHPREAMBLE); 3160 *(uint16_t *)wh->i_dur = htole16(dur); 3161 } 3162 3163 if (sc->sc_epq[0].tx_nfree == 0) { 3164 /* let caller free mbuf */ 3165 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 3166 return (EIO); 3167 } 3168 data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh); 3169 STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next); 3170 sc->sc_epq[0].tx_nfree--; 3171 3172 txd = (struct rt2870_txd *)&data->desc; 3173 txd->flags = RT2860_TX_QSEL_EDCA; 3174 txwi = (struct rt2860_txwi *)(txd + 1); 3175 txwi->wcid = 0xff; 3176 txwi->flags = wflags; 3177 txwi->xflags = xflags; 3178 txwi->txop = 0; /* clear leftover garbage bits */ 3179 3180 data->m = m; 3181 data->ni = ni; 3182 data->ridx = ridx; 3183 3184 run_set_tx_desc(sc, data); 3185 3186 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", m->m_pkthdr.len + 3187 (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)), 3188 rt2860_rates[ridx].rate); 3189 3190 STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next); 3191 3192 usbd_transfer_start(sc->sc_xfer[0]); 3193 3194 return (0); 3195 } 3196 3197 static int 3198 run_sendprot(struct run_softc *sc, 3199 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate) 3200 { 3201 struct ieee80211com *ic = ni->ni_ic; 3202 struct ieee80211_frame *wh; 3203 struct run_tx_data *data; 3204 struct rt2870_txd *txd; 3205 struct rt2860_txwi *txwi; 3206 struct mbuf *mprot; 3207 int ridx; 3208 int protrate; 3209 int ackrate; 3210 int pktlen; 3211 int isshort; 3212 uint16_t dur; 3213 uint8_t type; 3214 uint8_t wflags = 0; 3215 uint8_t xflags = 0; 3216 3217 RUN_LOCK_ASSERT(sc, MA_OWNED); 3218 3219 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY, 3220 ("protection %d", prot)); 3221 3222 wh = mtod(m, struct ieee80211_frame *); 3223 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 3224 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 3225 3226 protrate = ieee80211_ctl_rate(ic->ic_rt, rate); 3227 ackrate = ieee80211_ack_rate(ic->ic_rt, rate); 3228 3229 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0; 3230 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort) 3231 + ieee80211_ack_duration(ic->ic_rt, rate, isshort); 3232 wflags = RT2860_TX_FRAG; 3233 3234 /* check that there are free slots before allocating the mbuf */ 3235 if (sc->sc_epq[0].tx_nfree == 0) { 3236 /* let caller free mbuf */ 3237 sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE; 3238 return (ENOBUFS); 3239 } 3240 3241 if (prot == IEEE80211_PROT_RTSCTS) { 3242 /* NB: CTS is the same size as an ACK */ 3243 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort); 3244 xflags |= RT2860_TX_ACK; 3245 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur); 3246 } else { 3247 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur); 3248 } 3249 if (mprot == NULL) { 3250 sc->sc_ifp->if_oerrors++; 3251 DPRINTF("could not allocate mbuf\n"); 3252 return (ENOBUFS); 3253 } 3254 3255 data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh); 3256 STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next); 3257 sc->sc_epq[0].tx_nfree--; 3258 3259 txd = (struct rt2870_txd *)&data->desc; 3260 txd->flags = RT2860_TX_QSEL_EDCA; 3261 txwi = (struct rt2860_txwi *)(txd + 1); 3262 txwi->wcid = 0xff; 3263 txwi->flags = wflags; 3264 txwi->xflags = xflags; 3265 txwi->txop = 0; /* clear leftover garbage bits */ 3266 3267 data->m = mprot; 3268 data->ni = ieee80211_ref_node(ni); 3269 3270 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++) 3271 if (rt2860_rates[ridx].rate == protrate) 3272 break; 3273 data->ridx = ridx; 3274 3275 run_set_tx_desc(sc, data); 3276 3277 DPRINTFN(1, "sending prot len=%u rate=%u\n", 3278 m->m_pkthdr.len, rate); 3279 3280 STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next); 3281 3282 usbd_transfer_start(sc->sc_xfer[0]); 3283 3284 return (0); 3285 } 3286 3287 static int 3288 run_tx_param(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni, 3289 const struct ieee80211_bpf_params *params) 3290 { 3291 struct ieee80211com *ic = ni->ni_ic; 3292 struct ieee80211_frame *wh; 3293 struct run_tx_data *data; 3294 struct rt2870_txd *txd; 3295 struct rt2860_txwi *txwi; 3296 uint8_t type; 3297 uint8_t ridx; 3298 uint8_t rate; 3299 uint8_t opflags = 0; 3300 uint8_t xflags = 0; 3301 int error; 3302 3303 RUN_LOCK_ASSERT(sc, MA_OWNED); 3304 3305 KASSERT(params != NULL, ("no raw xmit params")); 3306 3307 wh = mtod(m, struct ieee80211_frame *); 3308 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 3309 3310 rate = params->ibp_rate0; 3311 if (!ieee80211_isratevalid(ic->ic_rt, rate)) { 3312 /* let caller free mbuf */ 3313 return (EINVAL); 3314 } 3315 3316 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 3317 xflags |= RT2860_TX_ACK; 3318 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) { 3319 error = run_sendprot(sc, m, ni, 3320 params->ibp_flags & IEEE80211_BPF_RTS ? 3321 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY, 3322 rate); 3323 if (error) { 3324 /* let caller free mbuf */ 3325 return error; 3326 } 3327 opflags |= /*XXX RT2573_TX_LONG_RETRY |*/ RT2860_TX_TXOP_SIFS; 3328 } 3329 3330 if (sc->sc_epq[0].tx_nfree == 0) { 3331 /* let caller free mbuf */ 3332 sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE; 3333 DPRINTF("sending raw frame, but tx ring is full\n"); 3334 return (EIO); 3335 } 3336 data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh); 3337 STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next); 3338 sc->sc_epq[0].tx_nfree--; 3339 3340 txd = (struct rt2870_txd *)&data->desc; 3341 txd->flags = RT2860_TX_QSEL_EDCA; 3342 txwi = (struct rt2860_txwi *)(txd + 1); 3343 txwi->wcid = 0xff; 3344 txwi->xflags = xflags; 3345 txwi->txop = opflags; 3346 txwi->flags = 0; /* clear leftover garbage bits */ 3347 3348 data->m = m; 3349 data->ni = ni; 3350 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++) 3351 if (rt2860_rates[ridx].rate == rate) 3352 break; 3353 data->ridx = ridx; 3354 3355 run_set_tx_desc(sc, data); 3356 3357 DPRINTFN(10, "sending raw frame len=%u rate=%u\n", 3358 m->m_pkthdr.len, rate); 3359 3360 STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next); 3361 3362 usbd_transfer_start(sc->sc_xfer[0]); 3363 3364 return (0); 3365 } 3366 3367 static int 3368 run_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 3369 const struct ieee80211_bpf_params *params) 3370 { 3371 struct ifnet *ifp = ni->ni_ic->ic_ifp; 3372 struct run_softc *sc = ifp->if_softc; 3373 int error = 0; 3374 3375 RUN_LOCK(sc); 3376 3377 /* prevent management frames from being sent if we're not ready */ 3378 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 3379 error = ENETDOWN; 3380 goto done; 3381 } 3382 3383 if (params == NULL) { 3384 /* tx mgt packet */ 3385 if ((error = run_tx_mgt(sc, m, ni)) != 0) { 3386 ifp->if_oerrors++; 3387 DPRINTF("mgt tx failed\n"); 3388 goto done; 3389 } 3390 } else { 3391 /* tx raw packet with param */ 3392 if ((error = run_tx_param(sc, m, ni, params)) != 0) { 3393 ifp->if_oerrors++; 3394 DPRINTF("tx with param failed\n"); 3395 goto done; 3396 } 3397 } 3398 3399 ifp->if_opackets++; 3400 3401 done: 3402 RUN_UNLOCK(sc); 3403 3404 if (error != 0) { 3405 if(m != NULL) 3406 m_freem(m); 3407 ieee80211_free_node(ni); 3408 } 3409 3410 return (error); 3411 } 3412 3413 static void 3414 run_start(struct ifnet *ifp) 3415 { 3416 struct run_softc *sc = ifp->if_softc; 3417 struct ieee80211_node *ni; 3418 struct mbuf *m; 3419 3420 RUN_LOCK(sc); 3421 3422 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 3423 RUN_UNLOCK(sc); 3424 return; 3425 } 3426 3427 for (;;) { 3428 /* send data frames */ 3429 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 3430 if (m == NULL) 3431 break; 3432 3433 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 3434 if (run_tx(sc, m, ni) != 0) { 3435 IFQ_DRV_PREPEND(&ifp->if_snd, m); 3436 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 3437 break; 3438 } 3439 } 3440 3441 RUN_UNLOCK(sc); 3442 } 3443 3444 static int 3445 run_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 3446 { 3447 struct run_softc *sc = ifp->if_softc; 3448 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 3449 struct ifreq *ifr = (struct ifreq *) data; 3450 int startall = 0; 3451 int error; 3452 3453 RUN_LOCK(sc); 3454 error = sc->sc_detached ? ENXIO : 0; 3455 RUN_UNLOCK(sc); 3456 if (error) 3457 return (error); 3458 3459 switch (cmd) { 3460 case SIOCSIFFLAGS: 3461 RUN_LOCK(sc); 3462 if (ifp->if_flags & IFF_UP) { 3463 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)){ 3464 startall = 1; 3465 run_init_locked(sc); 3466 } else 3467 run_update_promisc_locked(ifp); 3468 } else { 3469 if (ifp->if_drv_flags & IFF_DRV_RUNNING && 3470 (ic->ic_nrunning == 0 || sc->rvp_cnt <= 1)) { 3471 run_stop(sc); 3472 } 3473 } 3474 RUN_UNLOCK(sc); 3475 if (startall) 3476 ieee80211_start_all(ic); 3477 break; 3478 case SIOCGIFMEDIA: 3479 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 3480 break; 3481 case SIOCGIFADDR: 3482 error = ether_ioctl(ifp, cmd, data); 3483 break; 3484 default: 3485 error = EINVAL; 3486 break; 3487 } 3488 3489 return (error); 3490 } 3491 3492 static void 3493 run_set_agc(struct run_softc *sc, uint8_t agc) 3494 { 3495 uint8_t bbp; 3496 3497 if (sc->mac_ver == 0x3572) { 3498 run_bbp_read(sc, 27, &bbp); 3499 bbp &= ~(0x3 << 5); 3500 run_bbp_write(sc, 27, bbp | 0 << 5); /* select Rx0 */ 3501 run_bbp_write(sc, 66, agc); 3502 run_bbp_write(sc, 27, bbp | 1 << 5); /* select Rx1 */ 3503 run_bbp_write(sc, 66, agc); 3504 } else 3505 run_bbp_write(sc, 66, agc); 3506 } 3507 3508 static void 3509 run_select_chan_group(struct run_softc *sc, int group) 3510 { 3511 uint32_t tmp; 3512 uint8_t agc; 3513 3514 run_bbp_write(sc, 62, 0x37 - sc->lna[group]); 3515 run_bbp_write(sc, 63, 0x37 - sc->lna[group]); 3516 run_bbp_write(sc, 64, 0x37 - sc->lna[group]); 3517 run_bbp_write(sc, 86, 0x00); 3518 3519 if (group == 0) { 3520 if (sc->ext_2ghz_lna) { 3521 run_bbp_write(sc, 82, 0x62); 3522 run_bbp_write(sc, 75, 0x46); 3523 } else { 3524 run_bbp_write(sc, 82, 0x84); 3525 run_bbp_write(sc, 75, 0x50); 3526 } 3527 } else { 3528 if (sc->mac_ver == 0x3572) 3529 run_bbp_write(sc, 82, 0x94); 3530 else 3531 run_bbp_write(sc, 82, 0xf2); 3532 if (sc->ext_5ghz_lna) 3533 run_bbp_write(sc, 75, 0x46); 3534 else 3535 run_bbp_write(sc, 75, 0x50); 3536 } 3537 3538 run_read(sc, RT2860_TX_BAND_CFG, &tmp); 3539 tmp &= ~(RT2860_5G_BAND_SEL_N | RT2860_5G_BAND_SEL_P); 3540 tmp |= (group == 0) ? RT2860_5G_BAND_SEL_N : RT2860_5G_BAND_SEL_P; 3541 run_write(sc, RT2860_TX_BAND_CFG, tmp); 3542 3543 /* enable appropriate Power Amplifiers and Low Noise Amplifiers */ 3544 tmp = RT2860_RFTR_EN | RT2860_TRSW_EN | RT2860_LNA_PE0_EN; 3545 if (sc->nrxchains > 1) 3546 tmp |= RT2860_LNA_PE1_EN; 3547 if (group == 0) { /* 2GHz */ 3548 tmp |= RT2860_PA_PE_G0_EN; 3549 if (sc->ntxchains > 1) 3550 tmp |= RT2860_PA_PE_G1_EN; 3551 } else { /* 5GHz */ 3552 tmp |= RT2860_PA_PE_A0_EN; 3553 if (sc->ntxchains > 1) 3554 tmp |= RT2860_PA_PE_A1_EN; 3555 } 3556 if (sc->mac_ver == 0x3572) { 3557 run_rt3070_rf_write(sc, 8, 0x00); 3558 run_write(sc, RT2860_TX_PIN_CFG, tmp); 3559 run_rt3070_rf_write(sc, 8, 0x80); 3560 } else 3561 run_write(sc, RT2860_TX_PIN_CFG, tmp); 3562 3563 /* set initial AGC value */ 3564 if (group == 0) { /* 2GHz band */ 3565 if (sc->mac_ver >= 0x3070) 3566 agc = 0x1c + sc->lna[0] * 2; 3567 else 3568 agc = 0x2e + sc->lna[0]; 3569 } else { /* 5GHz band */ 3570 if (sc->mac_ver == 0x3572) 3571 agc = 0x22 + (sc->lna[group] * 5) / 3; 3572 else 3573 agc = 0x32 + (sc->lna[group] * 5) / 3; 3574 } 3575 run_set_agc(sc, agc); 3576 } 3577 3578 static void 3579 run_rt2870_set_chan(struct run_softc *sc, uint32_t chan) 3580 { 3581 const struct rfprog *rfprog = rt2860_rf2850; 3582 uint32_t r2, r3, r4; 3583 int8_t txpow1, txpow2; 3584 int i; 3585 3586 /* find the settings for this channel (we know it exists) */ 3587 for (i = 0; rfprog[i].chan != chan; i++); 3588 3589 r2 = rfprog[i].r2; 3590 if (sc->ntxchains == 1) 3591 r2 |= 1 << 12; /* 1T: disable Tx chain 2 */ 3592 if (sc->nrxchains == 1) 3593 r2 |= 1 << 15 | 1 << 4; /* 1R: disable Rx chains 2 & 3 */ 3594 else if (sc->nrxchains == 2) 3595 r2 |= 1 << 4; /* 2R: disable Rx chain 3 */ 3596 3597 /* use Tx power values from EEPROM */ 3598 txpow1 = sc->txpow1[i]; 3599 txpow2 = sc->txpow2[i]; 3600 if (chan > 14) { 3601 if (txpow1 >= 0) 3602 txpow1 = txpow1 << 1 | 1; 3603 else 3604 txpow1 = (7 + txpow1) << 1; 3605 if (txpow2 >= 0) 3606 txpow2 = txpow2 << 1 | 1; 3607 else 3608 txpow2 = (7 + txpow2) << 1; 3609 } 3610 r3 = rfprog[i].r3 | txpow1 << 7; 3611 r4 = rfprog[i].r4 | sc->freq << 13 | txpow2 << 4; 3612 3613 run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1); 3614 run_rt2870_rf_write(sc, RT2860_RF2, r2); 3615 run_rt2870_rf_write(sc, RT2860_RF3, r3); 3616 run_rt2870_rf_write(sc, RT2860_RF4, r4); 3617 3618 run_delay(sc, 10); 3619 3620 run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1); 3621 run_rt2870_rf_write(sc, RT2860_RF2, r2); 3622 run_rt2870_rf_write(sc, RT2860_RF3, r3 | 1); 3623 run_rt2870_rf_write(sc, RT2860_RF4, r4); 3624 3625 run_delay(sc, 10); 3626 3627 run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1); 3628 run_rt2870_rf_write(sc, RT2860_RF2, r2); 3629 run_rt2870_rf_write(sc, RT2860_RF3, r3); 3630 run_rt2870_rf_write(sc, RT2860_RF4, r4); 3631 } 3632 3633 static void 3634 run_rt3070_set_chan(struct run_softc *sc, uint32_t chan) 3635 { 3636 int8_t txpow1, txpow2; 3637 uint8_t rf; 3638 int i; 3639 3640 /* RT3070 is 2GHz only */ 3641 KASSERT(chan >= 1 && chan <= 14, ("wrong channel selected\n")); 3642 3643 /* find the settings for this channel (we know it exists) */ 3644 for (i = 0; rt2860_rf2850[i].chan != chan; i++); 3645 3646 /* use Tx power values from EEPROM */ 3647 txpow1 = sc->txpow1[i]; 3648 txpow2 = sc->txpow2[i]; 3649 3650 run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n); 3651 3652 /* RT3370/RT3390: RF R3 [7:4] is not reserved bits. */ 3653 run_rt3070_rf_read(sc, 3, &rf); 3654 rf = (rf & ~0x0f) | rt3070_freqs[i].k; 3655 run_rt3070_rf_write(sc, 3, rf); 3656 3657 run_rt3070_rf_read(sc, 6, &rf); 3658 rf = (rf & ~0x03) | rt3070_freqs[i].r; 3659 run_rt3070_rf_write(sc, 6, rf); 3660 3661 /* set Tx0 power */ 3662 run_rt3070_rf_read(sc, 12, &rf); 3663 rf = (rf & ~0x1f) | txpow1; 3664 run_rt3070_rf_write(sc, 12, rf); 3665 3666 /* set Tx1 power */ 3667 run_rt3070_rf_read(sc, 13, &rf); 3668 rf = (rf & ~0x1f) | txpow2; 3669 run_rt3070_rf_write(sc, 13, rf); 3670 3671 run_rt3070_rf_read(sc, 1, &rf); 3672 rf &= ~0xfc; 3673 if (sc->ntxchains == 1) 3674 rf |= 1 << 7 | 1 << 5; /* 1T: disable Tx chains 2 & 3 */ 3675 else if (sc->ntxchains == 2) 3676 rf |= 1 << 7; /* 2T: disable Tx chain 3 */ 3677 if (sc->nrxchains == 1) 3678 rf |= 1 << 6 | 1 << 4; /* 1R: disable Rx chains 2 & 3 */ 3679 else if (sc->nrxchains == 2) 3680 rf |= 1 << 6; /* 2R: disable Rx chain 3 */ 3681 run_rt3070_rf_write(sc, 1, rf); 3682 3683 /* set RF offset */ 3684 run_rt3070_rf_read(sc, 23, &rf); 3685 rf = (rf & ~0x7f) | sc->freq; 3686 run_rt3070_rf_write(sc, 23, rf); 3687 3688 /* program RF filter */ 3689 run_rt3070_rf_read(sc, 24, &rf); /* Tx */ 3690 rf = (rf & ~0x3f) | sc->rf24_20mhz; 3691 run_rt3070_rf_write(sc, 24, rf); 3692 run_rt3070_rf_read(sc, 31, &rf); /* Rx */ 3693 rf = (rf & ~0x3f) | sc->rf24_20mhz; 3694 run_rt3070_rf_write(sc, 31, rf); 3695 3696 /* enable RF tuning */ 3697 run_rt3070_rf_read(sc, 7, &rf); 3698 run_rt3070_rf_write(sc, 7, rf | 0x01); 3699 } 3700 3701 static void 3702 run_rt3572_set_chan(struct run_softc *sc, u_int chan) 3703 { 3704 int8_t txpow1, txpow2; 3705 uint32_t tmp; 3706 uint8_t rf; 3707 int i; 3708 3709 /* find the settings for this channel (we know it exists) */ 3710 for (i = 0; rt2860_rf2850[i].chan != chan; i++); 3711 3712 /* use Tx power values from EEPROM */ 3713 txpow1 = sc->txpow1[i]; 3714 txpow2 = sc->txpow2[i]; 3715 3716 if (chan <= 14) { 3717 run_bbp_write(sc, 25, sc->bbp25); 3718 run_bbp_write(sc, 26, sc->bbp26); 3719 } else { 3720 /* enable IQ phase correction */ 3721 run_bbp_write(sc, 25, 0x09); 3722 run_bbp_write(sc, 26, 0xff); 3723 } 3724 3725 run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n); 3726 run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k); 3727 run_rt3070_rf_read(sc, 6, &rf); 3728 rf = (rf & ~0x0f) | rt3070_freqs[i].r; 3729 rf |= (chan <= 14) ? 0x08 : 0x04; 3730 run_rt3070_rf_write(sc, 6, rf); 3731 3732 /* set PLL mode */ 3733 run_rt3070_rf_read(sc, 5, &rf); 3734 rf &= ~(0x08 | 0x04); 3735 rf |= (chan <= 14) ? 0x04 : 0x08; 3736 run_rt3070_rf_write(sc, 5, rf); 3737 3738 /* set Tx power for chain 0 */ 3739 if (chan <= 14) 3740 rf = 0x60 | txpow1; 3741 else 3742 rf = 0xe0 | (txpow1 & 0xc) << 1 | (txpow1 & 0x3); 3743 run_rt3070_rf_write(sc, 12, rf); 3744 3745 /* set Tx power for chain 1 */ 3746 if (chan <= 14) 3747 rf = 0x60 | txpow2; 3748 else 3749 rf = 0xe0 | (txpow2 & 0xc) << 1 | (txpow2 & 0x3); 3750 run_rt3070_rf_write(sc, 13, rf); 3751 3752 /* set Tx/Rx streams */ 3753 run_rt3070_rf_read(sc, 1, &rf); 3754 rf &= ~0xfc; 3755 if (sc->ntxchains == 1) 3756 rf |= 1 << 7 | 1 << 5; /* 1T: disable Tx chains 2 & 3 */ 3757 else if (sc->ntxchains == 2) 3758 rf |= 1 << 7; /* 2T: disable Tx chain 3 */ 3759 if (sc->nrxchains == 1) 3760 rf |= 1 << 6 | 1 << 4; /* 1R: disable Rx chains 2 & 3 */ 3761 else if (sc->nrxchains == 2) 3762 rf |= 1 << 6; /* 2R: disable Rx chain 3 */ 3763 run_rt3070_rf_write(sc, 1, rf); 3764 3765 /* set RF offset */ 3766 run_rt3070_rf_read(sc, 23, &rf); 3767 rf = (rf & ~0x7f) | sc->freq; 3768 run_rt3070_rf_write(sc, 23, rf); 3769 3770 /* program RF filter */ 3771 rf = sc->rf24_20mhz; 3772 run_rt3070_rf_write(sc, 24, rf); /* Tx */ 3773 run_rt3070_rf_write(sc, 31, rf); /* Rx */ 3774 3775 /* enable RF tuning */ 3776 run_rt3070_rf_read(sc, 7, &rf); 3777 rf = (chan <= 14) ? 0xd8 : ((rf & ~0xc8) | 0x14); 3778 run_rt3070_rf_write(sc, 7, rf); 3779 3780 /* TSSI */ 3781 rf = (chan <= 14) ? 0xc3 : 0xc0; 3782 run_rt3070_rf_write(sc, 9, rf); 3783 3784 /* set loop filter 1 */ 3785 run_rt3070_rf_write(sc, 10, 0xf1); 3786 /* set loop filter 2 */ 3787 run_rt3070_rf_write(sc, 11, (chan <= 14) ? 0xb9 : 0x00); 3788 3789 /* set tx_mx2_ic */ 3790 run_rt3070_rf_write(sc, 15, (chan <= 14) ? 0x53 : 0x43); 3791 /* set tx_mx1_ic */ 3792 if (chan <= 14) 3793 rf = 0x48 | sc->txmixgain_2ghz; 3794 else 3795 rf = 0x78 | sc->txmixgain_5ghz; 3796 run_rt3070_rf_write(sc, 16, rf); 3797 3798 /* set tx_lo1 */ 3799 run_rt3070_rf_write(sc, 17, 0x23); 3800 /* set tx_lo2 */ 3801 if (chan <= 14) 3802 rf = 0x93; 3803 else if (chan <= 64) 3804 rf = 0xb7; 3805 else if (chan <= 128) 3806 rf = 0x74; 3807 else 3808 rf = 0x72; 3809 run_rt3070_rf_write(sc, 19, rf); 3810 3811 /* set rx_lo1 */ 3812 if (chan <= 14) 3813 rf = 0xb3; 3814 else if (chan <= 64) 3815 rf = 0xf6; 3816 else if (chan <= 128) 3817 rf = 0xf4; 3818 else 3819 rf = 0xf3; 3820 run_rt3070_rf_write(sc, 20, rf); 3821 3822 /* set pfd_delay */ 3823 if (chan <= 14) 3824 rf = 0x15; 3825 else if (chan <= 64) 3826 rf = 0x3d; 3827 else 3828 rf = 0x01; 3829 run_rt3070_rf_write(sc, 25, rf); 3830 3831 /* set rx_lo2 */ 3832 run_rt3070_rf_write(sc, 26, (chan <= 14) ? 0x85 : 0x87); 3833 /* set ldo_rf_vc */ 3834 run_rt3070_rf_write(sc, 27, (chan <= 14) ? 0x00 : 0x01); 3835 /* set drv_cc */ 3836 run_rt3070_rf_write(sc, 29, (chan <= 14) ? 0x9b : 0x9f); 3837 3838 run_read(sc, RT2860_GPIO_CTRL, &tmp); 3839 tmp &= ~0x8080; 3840 if (chan <= 14) 3841 tmp |= 0x80; 3842 run_write(sc, RT2860_GPIO_CTRL, tmp); 3843 3844 /* enable RF tuning */ 3845 run_rt3070_rf_read(sc, 7, &rf); 3846 run_rt3070_rf_write(sc, 7, rf | 0x01); 3847 3848 run_delay(sc, 2); 3849 } 3850 3851 static void 3852 run_set_rx_antenna(struct run_softc *sc, int aux) 3853 { 3854 uint32_t tmp; 3855 3856 if (aux) { 3857 run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 0); 3858 run_read(sc, RT2860_GPIO_CTRL, &tmp); 3859 run_write(sc, RT2860_GPIO_CTRL, (tmp & ~0x0808) | 0x08); 3860 } else { 3861 run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 1); 3862 run_read(sc, RT2860_GPIO_CTRL, &tmp); 3863 run_write(sc, RT2860_GPIO_CTRL, tmp & ~0x0808); 3864 } 3865 } 3866 3867 static int 3868 run_set_chan(struct run_softc *sc, struct ieee80211_channel *c) 3869 { 3870 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 3871 uint32_t chan, group; 3872 3873 chan = ieee80211_chan2ieee(ic, c); 3874 if (chan == 0 || chan == IEEE80211_CHAN_ANY) 3875 return (EINVAL); 3876 3877 if (sc->mac_ver == 0x3572) 3878 run_rt3572_set_chan(sc, chan); 3879 else if (sc->mac_ver >= 0x3070) 3880 run_rt3070_set_chan(sc, chan); 3881 else 3882 run_rt2870_set_chan(sc, chan); 3883 3884 /* determine channel group */ 3885 if (chan <= 14) 3886 group = 0; 3887 else if (chan <= 64) 3888 group = 1; 3889 else if (chan <= 128) 3890 group = 2; 3891 else 3892 group = 3; 3893 3894 /* XXX necessary only when group has changed! */ 3895 run_select_chan_group(sc, group); 3896 3897 run_delay(sc, 10); 3898 3899 return (0); 3900 } 3901 3902 static void 3903 run_set_channel(struct ieee80211com *ic) 3904 { 3905 struct run_softc *sc = ic->ic_ifp->if_softc; 3906 3907 RUN_LOCK(sc); 3908 run_set_chan(sc, ic->ic_curchan); 3909 RUN_UNLOCK(sc); 3910 3911 return; 3912 } 3913 3914 static void 3915 run_scan_start(struct ieee80211com *ic) 3916 { 3917 struct run_softc *sc = ic->ic_ifp->if_softc; 3918 uint32_t tmp; 3919 3920 RUN_LOCK(sc); 3921 3922 /* abort TSF synchronization */ 3923 run_read(sc, RT2860_BCN_TIME_CFG, &tmp); 3924 run_write(sc, RT2860_BCN_TIME_CFG, 3925 tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN | 3926 RT2860_TBTT_TIMER_EN)); 3927 run_set_bssid(sc, sc->sc_ifp->if_broadcastaddr); 3928 3929 RUN_UNLOCK(sc); 3930 3931 return; 3932 } 3933 3934 static void 3935 run_scan_end(struct ieee80211com *ic) 3936 { 3937 struct run_softc *sc = ic->ic_ifp->if_softc; 3938 3939 RUN_LOCK(sc); 3940 3941 run_enable_tsf_sync(sc); 3942 /* XXX keep local copy */ 3943 run_set_bssid(sc, sc->sc_bssid); 3944 3945 RUN_UNLOCK(sc); 3946 3947 return; 3948 } 3949 3950 /* 3951 * Could be called from ieee80211_node_timeout() 3952 * (non-sleepable thread) 3953 */ 3954 static void 3955 run_update_beacon(struct ieee80211vap *vap, int item) 3956 { 3957 struct ieee80211com *ic = vap->iv_ic; 3958 struct run_softc *sc = ic->ic_ifp->if_softc; 3959 struct run_vap *rvp = RUN_VAP(vap); 3960 int mcast = 0; 3961 uint32_t i; 3962 3963 KASSERT(vap != NULL, ("no beacon")); 3964 3965 switch (item) { 3966 case IEEE80211_BEACON_ERP: 3967 run_updateslot(ic->ic_ifp); 3968 break; 3969 case IEEE80211_BEACON_HTINFO: 3970 run_updateprot(ic); 3971 break; 3972 case IEEE80211_BEACON_TIM: 3973 mcast = 1; /*TODO*/ 3974 break; 3975 default: 3976 break; 3977 } 3978 3979 setbit(rvp->bo.bo_flags, item); 3980 ieee80211_beacon_update(vap->iv_bss, &rvp->bo, rvp->beacon_mbuf, mcast); 3981 3982 i = RUN_CMDQ_GET(&sc->cmdq_store); 3983 DPRINTF("cmdq_store=%d\n", i); 3984 sc->cmdq[i].func = run_update_beacon_cb; 3985 sc->cmdq[i].arg0 = vap; 3986 ieee80211_runtask(ic, &sc->cmdq_task); 3987 3988 return; 3989 } 3990 3991 static void 3992 run_update_beacon_cb(void *arg) 3993 { 3994 struct ieee80211vap *vap = arg; 3995 struct run_vap *rvp = RUN_VAP(vap); 3996 struct ieee80211com *ic = vap->iv_ic; 3997 struct run_softc *sc = ic->ic_ifp->if_softc; 3998 struct rt2860_txwi txwi; 3999 struct mbuf *m; 4000 uint8_t ridx; 4001 4002 if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC) 4003 return; 4004 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) 4005 return; 4006 4007 /* 4008 * No need to call ieee80211_beacon_update(), run_update_beacon() 4009 * is taking care of apropriate calls. 4010 */ 4011 if (rvp->beacon_mbuf == NULL) { 4012 rvp->beacon_mbuf = ieee80211_beacon_alloc(vap->iv_bss, 4013 &rvp->bo); 4014 if (rvp->beacon_mbuf == NULL) 4015 return; 4016 } 4017 m = rvp->beacon_mbuf; 4018 4019 memset(&txwi, 0, sizeof txwi); 4020 txwi.wcid = 0xff; 4021 txwi.len = htole16(m->m_pkthdr.len); 4022 /* send beacons at the lowest available rate */ 4023 ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ? 4024 RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1; 4025 txwi.phy = htole16(rt2860_rates[ridx].mcs); 4026 if (rt2860_rates[ridx].phy == IEEE80211_T_OFDM) 4027 txwi.phy |= htole16(RT2860_PHY_OFDM); 4028 txwi.txop = RT2860_TX_TXOP_HT; 4029 txwi.flags = RT2860_TX_TS; 4030 txwi.xflags = RT2860_TX_NSEQ; 4031 4032 run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id), 4033 (uint8_t *)&txwi, sizeof txwi); 4034 run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id) + sizeof txwi, 4035 mtod(m, uint8_t *), (m->m_pkthdr.len + 1) & ~1); /* roundup len */ 4036 4037 return; 4038 } 4039 4040 static void 4041 run_updateprot(struct ieee80211com *ic) 4042 { 4043 struct run_softc *sc = ic->ic_ifp->if_softc; 4044 uint32_t i; 4045 4046 i = RUN_CMDQ_GET(&sc->cmdq_store); 4047 DPRINTF("cmdq_store=%d\n", i); 4048 sc->cmdq[i].func = run_updateprot_cb; 4049 sc->cmdq[i].arg0 = ic; 4050 ieee80211_runtask(ic, &sc->cmdq_task); 4051 } 4052 4053 static void 4054 run_updateprot_cb(void *arg) 4055 { 4056 struct ieee80211com *ic = arg; 4057 struct run_softc *sc = ic->ic_ifp->if_softc; 4058 uint32_t tmp; 4059 4060 tmp = RT2860_RTSTH_EN | RT2860_PROT_NAV_SHORT | RT2860_TXOP_ALLOW_ALL; 4061 /* setup protection frame rate (MCS code) */ 4062 tmp |= (ic->ic_curmode == IEEE80211_MODE_11A) ? 4063 rt2860_rates[RT2860_RIDX_OFDM6].mcs : 4064 rt2860_rates[RT2860_RIDX_CCK11].mcs; 4065 4066 /* CCK frames don't require protection */ 4067 run_write(sc, RT2860_CCK_PROT_CFG, tmp); 4068 if (ic->ic_flags & IEEE80211_F_USEPROT) { 4069 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 4070 tmp |= RT2860_PROT_CTRL_RTS_CTS; 4071 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 4072 tmp |= RT2860_PROT_CTRL_CTS; 4073 } 4074 run_write(sc, RT2860_OFDM_PROT_CFG, tmp); 4075 } 4076 4077 static void 4078 run_usb_timeout_cb(void *arg) 4079 { 4080 struct ieee80211vap *vap = arg; 4081 struct run_softc *sc = vap->iv_ic->ic_ifp->if_softc; 4082 4083 RUN_LOCK_ASSERT(sc, MA_OWNED); 4084 4085 if(vap->iv_state == IEEE80211_S_RUN && 4086 vap->iv_opmode != IEEE80211_M_STA) 4087 run_reset_livelock(sc); 4088 else if (vap->iv_state == IEEE80211_S_SCAN) { 4089 DPRINTF("timeout caused by scan\n"); 4090 /* cancel bgscan */ 4091 ieee80211_cancel_scan(vap); 4092 } else 4093 DPRINTF("timeout by unknown cause\n"); 4094 } 4095 4096 static void 4097 run_reset_livelock(struct run_softc *sc) 4098 { 4099 uint32_t tmp; 4100 4101 RUN_LOCK_ASSERT(sc, MA_OWNED); 4102 4103 /* 4104 * In IBSS or HostAP modes (when the hardware sends beacons), the MAC 4105 * can run into a livelock and start sending CTS-to-self frames like 4106 * crazy if protection is enabled. Reset MAC/BBP for a while 4107 */ 4108 run_read(sc, RT2860_DEBUG, &tmp); 4109 DPRINTFN(3, "debug reg %08x\n", tmp); 4110 if ((tmp & (1 << 29)) && (tmp & (1 << 7 | 1 << 5))) { 4111 DPRINTF("CTS-to-self livelock detected\n"); 4112 run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_SRST); 4113 run_delay(sc, 1); 4114 run_write(sc, RT2860_MAC_SYS_CTRL, 4115 RT2860_MAC_RX_EN | RT2860_MAC_TX_EN); 4116 } 4117 } 4118 4119 static void 4120 run_update_promisc_locked(struct ifnet *ifp) 4121 { 4122 struct run_softc *sc = ifp->if_softc; 4123 uint32_t tmp; 4124 4125 run_read(sc, RT2860_RX_FILTR_CFG, &tmp); 4126 4127 tmp |= RT2860_DROP_UC_NOME; 4128 if (ifp->if_flags & IFF_PROMISC) 4129 tmp &= ~RT2860_DROP_UC_NOME; 4130 4131 run_write(sc, RT2860_RX_FILTR_CFG, tmp); 4132 4133 DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ? 4134 "entering" : "leaving"); 4135 } 4136 4137 static void 4138 run_update_promisc(struct ifnet *ifp) 4139 { 4140 struct run_softc *sc = ifp->if_softc; 4141 4142 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 4143 return; 4144 4145 RUN_LOCK(sc); 4146 run_update_promisc_locked(ifp); 4147 RUN_UNLOCK(sc); 4148 } 4149 4150 static void 4151 run_enable_tsf_sync(struct run_softc *sc) 4152 { 4153 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 4154 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 4155 uint32_t tmp; 4156 4157 DPRINTF("rvp_id=%d ic_opmode=%d\n", RUN_VAP(vap)->rvp_id, ic->ic_opmode); 4158 4159 run_read(sc, RT2860_BCN_TIME_CFG, &tmp); 4160 tmp &= ~0x1fffff; 4161 tmp |= vap->iv_bss->ni_intval * 16; 4162 tmp |= RT2860_TSF_TIMER_EN | RT2860_TBTT_TIMER_EN; 4163 4164 if (ic->ic_opmode == IEEE80211_M_STA) { 4165 /* 4166 * Local TSF is always updated with remote TSF on beacon 4167 * reception. 4168 */ 4169 tmp |= 1 << RT2860_TSF_SYNC_MODE_SHIFT; 4170 } else if (ic->ic_opmode == IEEE80211_M_IBSS) { 4171 tmp |= RT2860_BCN_TX_EN; 4172 /* 4173 * Local TSF is updated with remote TSF on beacon reception 4174 * only if the remote TSF is greater than local TSF. 4175 */ 4176 tmp |= 2 << RT2860_TSF_SYNC_MODE_SHIFT; 4177 } else if (ic->ic_opmode == IEEE80211_M_HOSTAP || 4178 ic->ic_opmode == IEEE80211_M_MBSS) { 4179 tmp |= RT2860_BCN_TX_EN; 4180 /* SYNC with nobody */ 4181 tmp |= 3 << RT2860_TSF_SYNC_MODE_SHIFT; 4182 } else { 4183 DPRINTF("Enabling TSF failed. undefined opmode\n"); 4184 return; 4185 } 4186 4187 run_write(sc, RT2860_BCN_TIME_CFG, tmp); 4188 } 4189 4190 static void 4191 run_enable_mrr(struct run_softc *sc) 4192 { 4193 #define CCK(mcs) (mcs) 4194 #define OFDM(mcs) (1 << 3 | (mcs)) 4195 run_write(sc, RT2860_LG_FBK_CFG0, 4196 OFDM(6) << 28 | /* 54->48 */ 4197 OFDM(5) << 24 | /* 48->36 */ 4198 OFDM(4) << 20 | /* 36->24 */ 4199 OFDM(3) << 16 | /* 24->18 */ 4200 OFDM(2) << 12 | /* 18->12 */ 4201 OFDM(1) << 8 | /* 12-> 9 */ 4202 OFDM(0) << 4 | /* 9-> 6 */ 4203 OFDM(0)); /* 6-> 6 */ 4204 4205 run_write(sc, RT2860_LG_FBK_CFG1, 4206 CCK(2) << 12 | /* 11->5.5 */ 4207 CCK(1) << 8 | /* 5.5-> 2 */ 4208 CCK(0) << 4 | /* 2-> 1 */ 4209 CCK(0)); /* 1-> 1 */ 4210 #undef OFDM 4211 #undef CCK 4212 } 4213 4214 static void 4215 run_set_txpreamble(struct run_softc *sc) 4216 { 4217 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 4218 uint32_t tmp; 4219 4220 run_read(sc, RT2860_AUTO_RSP_CFG, &tmp); 4221 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 4222 tmp |= RT2860_CCK_SHORT_EN; 4223 else 4224 tmp &= ~RT2860_CCK_SHORT_EN; 4225 run_write(sc, RT2860_AUTO_RSP_CFG, tmp); 4226 } 4227 4228 static void 4229 run_set_basicrates(struct run_softc *sc) 4230 { 4231 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 4232 4233 /* set basic rates mask */ 4234 if (ic->ic_curmode == IEEE80211_MODE_11B) 4235 run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x003); 4236 else if (ic->ic_curmode == IEEE80211_MODE_11A) 4237 run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x150); 4238 else /* 11g */ 4239 run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x15f); 4240 } 4241 4242 static void 4243 run_set_leds(struct run_softc *sc, uint16_t which) 4244 { 4245 (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LEDS, 4246 which | (sc->leds & 0x7f)); 4247 } 4248 4249 static void 4250 run_set_bssid(struct run_softc *sc, const uint8_t *bssid) 4251 { 4252 run_write(sc, RT2860_MAC_BSSID_DW0, 4253 bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24); 4254 run_write(sc, RT2860_MAC_BSSID_DW1, 4255 bssid[4] | bssid[5] << 8); 4256 } 4257 4258 static void 4259 run_set_macaddr(struct run_softc *sc, const uint8_t *addr) 4260 { 4261 run_write(sc, RT2860_MAC_ADDR_DW0, 4262 addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24); 4263 run_write(sc, RT2860_MAC_ADDR_DW1, 4264 addr[4] | addr[5] << 8 | 0xff << 16); 4265 } 4266 4267 static void 4268 run_updateslot(struct ifnet *ifp) 4269 { 4270 struct run_softc *sc = ifp->if_softc; 4271 struct ieee80211com *ic = ifp->if_l2com; 4272 uint32_t i; 4273 4274 i = RUN_CMDQ_GET(&sc->cmdq_store); 4275 DPRINTF("cmdq_store=%d\n", i); 4276 sc->cmdq[i].func = run_updateslot_cb; 4277 sc->cmdq[i].arg0 = ifp; 4278 ieee80211_runtask(ic, &sc->cmdq_task); 4279 4280 return; 4281 } 4282 4283 /* ARGSUSED */ 4284 static void 4285 run_updateslot_cb(void *arg) 4286 { 4287 struct ifnet *ifp = arg; 4288 struct run_softc *sc = ifp->if_softc; 4289 struct ieee80211com *ic = ifp->if_l2com; 4290 uint32_t tmp; 4291 4292 run_read(sc, RT2860_BKOFF_SLOT_CFG, &tmp); 4293 tmp &= ~0xff; 4294 tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20; 4295 run_write(sc, RT2860_BKOFF_SLOT_CFG, tmp); 4296 } 4297 4298 static void 4299 run_update_mcast(struct ifnet *ifp) 4300 { 4301 /* h/w filter supports getting everything or nothing */ 4302 ifp->if_flags |= IFF_ALLMULTI; 4303 } 4304 4305 static int8_t 4306 run_rssi2dbm(struct run_softc *sc, uint8_t rssi, uint8_t rxchain) 4307 { 4308 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 4309 struct ieee80211_channel *c = ic->ic_curchan; 4310 int delta; 4311 4312 if (IEEE80211_IS_CHAN_5GHZ(c)) { 4313 uint32_t chan = ieee80211_chan2ieee(ic, c); 4314 delta = sc->rssi_5ghz[rxchain]; 4315 4316 /* determine channel group */ 4317 if (chan <= 64) 4318 delta -= sc->lna[1]; 4319 else if (chan <= 128) 4320 delta -= sc->lna[2]; 4321 else 4322 delta -= sc->lna[3]; 4323 } else 4324 delta = sc->rssi_2ghz[rxchain] - sc->lna[0]; 4325 4326 return (-12 - delta - rssi); 4327 } 4328 4329 static int 4330 run_bbp_init(struct run_softc *sc) 4331 { 4332 int i, error, ntries; 4333 uint8_t bbp0; 4334 4335 /* wait for BBP to wake up */ 4336 for (ntries = 0; ntries < 20; ntries++) { 4337 if ((error = run_bbp_read(sc, 0, &bbp0)) != 0) 4338 return error; 4339 if (bbp0 != 0 && bbp0 != 0xff) 4340 break; 4341 } 4342 if (ntries == 20) 4343 return (ETIMEDOUT); 4344 4345 /* initialize BBP registers to default values */ 4346 for (i = 0; i < N(rt2860_def_bbp); i++) { 4347 run_bbp_write(sc, rt2860_def_bbp[i].reg, 4348 rt2860_def_bbp[i].val); 4349 } 4350 4351 /* fix BBP84 for RT2860E */ 4352 if (sc->mac_ver == 0x2860 && sc->mac_rev != 0x0101) 4353 run_bbp_write(sc, 84, 0x19); 4354 4355 if (sc->mac_ver >= 0x3070) { 4356 run_bbp_write(sc, 79, 0x13); 4357 run_bbp_write(sc, 80, 0x05); 4358 run_bbp_write(sc, 81, 0x33); 4359 } else if (sc->mac_ver == 0x2860 && sc->mac_rev == 0x0100) { 4360 run_bbp_write(sc, 69, 0x16); 4361 run_bbp_write(sc, 73, 0x12); 4362 } 4363 return (0); 4364 } 4365 4366 static int 4367 run_rt3070_rf_init(struct run_softc *sc) 4368 { 4369 uint32_t tmp; 4370 uint8_t bbp4, mingain, rf, target; 4371 int i; 4372 4373 run_rt3070_rf_read(sc, 30, &rf); 4374 /* toggle RF R30 bit 7 */ 4375 run_rt3070_rf_write(sc, 30, rf | 0x80); 4376 run_delay(sc, 10); 4377 run_rt3070_rf_write(sc, 30, rf & ~0x80); 4378 4379 /* initialize RF registers to default value */ 4380 if (sc->mac_ver == 0x3572) { 4381 for (i = 0; i < N(rt3572_def_rf); i++) { 4382 run_rt3070_rf_write(sc, rt3572_def_rf[i].reg, 4383 rt3572_def_rf[i].val); 4384 } 4385 } else { 4386 for (i = 0; i < N(rt3070_def_rf); i++) { 4387 run_rt3070_rf_write(sc, rt3070_def_rf[i].reg, 4388 rt3070_def_rf[i].val); 4389 } 4390 } 4391 4392 if (sc->mac_ver == 0x3070 && sc->mac_rev < 0x0201) { 4393 /* 4394 * Change voltage from 1.2V to 1.35V for RT3070. 4395 * The DAC issue (RT3070_LDO_CFG0) has been fixed 4396 * in RT3070(F). 4397 */ 4398 run_read(sc, RT3070_LDO_CFG0, &tmp); 4399 tmp = (tmp & ~0x0f000000) | 0x0d000000; 4400 run_write(sc, RT3070_LDO_CFG0, tmp); 4401 4402 } else if (sc->mac_ver == 0x3071) { 4403 run_rt3070_rf_read(sc, 6, &rf); 4404 run_rt3070_rf_write(sc, 6, rf | 0x40); 4405 run_rt3070_rf_write(sc, 31, 0x14); 4406 4407 run_read(sc, RT3070_LDO_CFG0, &tmp); 4408 tmp &= ~0x1f000000; 4409 if (sc->mac_rev < 0x0211) 4410 tmp |= 0x0d000000; /* 1.3V */ 4411 else 4412 tmp |= 0x01000000; /* 1.2V */ 4413 run_write(sc, RT3070_LDO_CFG0, tmp); 4414 4415 /* patch LNA_PE_G1 */ 4416 run_read(sc, RT3070_GPIO_SWITCH, &tmp); 4417 run_write(sc, RT3070_GPIO_SWITCH, tmp & ~0x20); 4418 4419 } else if (sc->mac_ver == 0x3572) { 4420 run_rt3070_rf_read(sc, 6, &rf); 4421 run_rt3070_rf_write(sc, 6, rf | 0x40); 4422 4423 /* increase voltage from 1.2V to 1.35V */ 4424 run_read(sc, RT3070_LDO_CFG0, &tmp); 4425 tmp = (tmp & ~0x1f000000) | 0x0d000000; 4426 run_write(sc, RT3070_LDO_CFG0, tmp); 4427 4428 if (sc->mac_rev < 0x0211 || !sc->patch_dac) { 4429 run_delay(sc, 1); /* wait for 1msec */ 4430 /* decrease voltage back to 1.2V */ 4431 tmp = (tmp & ~0x1f000000) | 0x01000000; 4432 run_write(sc, RT3070_LDO_CFG0, tmp); 4433 } 4434 } 4435 4436 /* select 20MHz bandwidth */ 4437 run_rt3070_rf_read(sc, 31, &rf); 4438 run_rt3070_rf_write(sc, 31, rf & ~0x20); 4439 4440 /* calibrate filter for 20MHz bandwidth */ 4441 sc->rf24_20mhz = 0x1f; /* default value */ 4442 target = (sc->mac_ver < 0x3071) ? 0x16 : 0x13; 4443 run_rt3070_filter_calib(sc, 0x07, target, &sc->rf24_20mhz); 4444 4445 /* select 40MHz bandwidth */ 4446 run_bbp_read(sc, 4, &bbp4); 4447 run_bbp_write(sc, 4, (bbp4 & ~0x18) | 0x10); 4448 run_rt3070_rf_read(sc, 31, &rf); 4449 run_rt3070_rf_write(sc, 31, rf | 0x20); 4450 4451 /* calibrate filter for 40MHz bandwidth */ 4452 sc->rf24_40mhz = 0x2f; /* default value */ 4453 target = (sc->mac_ver < 0x3071) ? 0x19 : 0x15; 4454 run_rt3070_filter_calib(sc, 0x27, target, &sc->rf24_40mhz); 4455 4456 /* go back to 20MHz bandwidth */ 4457 run_bbp_read(sc, 4, &bbp4); 4458 run_bbp_write(sc, 4, bbp4 & ~0x18); 4459 4460 if (sc->mac_ver == 0x3572) { 4461 /* save default BBP registers 25 and 26 values */ 4462 run_bbp_read(sc, 25, &sc->bbp25); 4463 run_bbp_read(sc, 26, &sc->bbp26); 4464 } else if (sc->mac_rev < 0x0201 || sc->mac_rev < 0x0211) 4465 run_rt3070_rf_write(sc, 27, 0x03); 4466 4467 run_read(sc, RT3070_OPT_14, &tmp); 4468 run_write(sc, RT3070_OPT_14, tmp | 1); 4469 4470 if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) { 4471 run_rt3070_rf_read(sc, 17, &rf); 4472 rf &= ~RT3070_TX_LO1; 4473 if ((sc->mac_ver == 0x3070 || 4474 (sc->mac_ver == 0x3071 && sc->mac_rev >= 0x0211)) && 4475 !sc->ext_2ghz_lna) 4476 rf |= 0x20; /* fix for long range Rx issue */ 4477 mingain = (sc->mac_ver == 0x3070) ? 1 : 2; 4478 if (sc->txmixgain_2ghz >= mingain) 4479 rf = (rf & ~0x7) | sc->txmixgain_2ghz; 4480 run_rt3070_rf_write(sc, 17, rf); 4481 } 4482 4483 if (sc->mac_rev == 0x3071) { 4484 run_rt3070_rf_read(sc, 1, &rf); 4485 rf &= ~(RT3070_RX0_PD | RT3070_TX0_PD); 4486 rf |= RT3070_RF_BLOCK | RT3070_RX1_PD | RT3070_TX1_PD; 4487 run_rt3070_rf_write(sc, 1, rf); 4488 4489 run_rt3070_rf_read(sc, 15, &rf); 4490 run_rt3070_rf_write(sc, 15, rf & ~RT3070_TX_LO2); 4491 4492 run_rt3070_rf_read(sc, 20, &rf); 4493 run_rt3070_rf_write(sc, 20, rf & ~RT3070_RX_LO1); 4494 4495 run_rt3070_rf_read(sc, 21, &rf); 4496 run_rt3070_rf_write(sc, 21, rf & ~RT3070_RX_LO2); 4497 } 4498 4499 if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) { 4500 /* fix Tx to Rx IQ glitch by raising RF voltage */ 4501 run_rt3070_rf_read(sc, 27, &rf); 4502 rf &= ~0x77; 4503 if (sc->mac_rev < 0x0211) 4504 rf |= 0x03; 4505 run_rt3070_rf_write(sc, 27, rf); 4506 } 4507 return (0); 4508 } 4509 4510 static int 4511 run_rt3070_filter_calib(struct run_softc *sc, uint8_t init, uint8_t target, 4512 uint8_t *val) 4513 { 4514 uint8_t rf22, rf24; 4515 uint8_t bbp55_pb, bbp55_sb, delta; 4516 int ntries; 4517 4518 /* program filter */ 4519 run_rt3070_rf_read(sc, 24, &rf24); 4520 rf24 = (rf24 & 0xc0) | init; /* initial filter value */ 4521 run_rt3070_rf_write(sc, 24, rf24); 4522 4523 /* enable baseband loopback mode */ 4524 run_rt3070_rf_read(sc, 22, &rf22); 4525 run_rt3070_rf_write(sc, 22, rf22 | 0x01); 4526 4527 /* set power and frequency of passband test tone */ 4528 run_bbp_write(sc, 24, 0x00); 4529 for (ntries = 0; ntries < 100; ntries++) { 4530 /* transmit test tone */ 4531 run_bbp_write(sc, 25, 0x90); 4532 run_delay(sc, 10); 4533 /* read received power */ 4534 run_bbp_read(sc, 55, &bbp55_pb); 4535 if (bbp55_pb != 0) 4536 break; 4537 } 4538 if (ntries == 100) 4539 return ETIMEDOUT; 4540 4541 /* set power and frequency of stopband test tone */ 4542 run_bbp_write(sc, 24, 0x06); 4543 for (ntries = 0; ntries < 100; ntries++) { 4544 /* transmit test tone */ 4545 run_bbp_write(sc, 25, 0x90); 4546 run_delay(sc, 10); 4547 /* read received power */ 4548 run_bbp_read(sc, 55, &bbp55_sb); 4549 4550 delta = bbp55_pb - bbp55_sb; 4551 if (delta > target) 4552 break; 4553 4554 /* reprogram filter */ 4555 rf24++; 4556 run_rt3070_rf_write(sc, 24, rf24); 4557 } 4558 if (ntries < 100) { 4559 if (rf24 != init) 4560 rf24--; /* backtrack */ 4561 *val = rf24; 4562 run_rt3070_rf_write(sc, 24, rf24); 4563 } 4564 4565 /* restore initial state */ 4566 run_bbp_write(sc, 24, 0x00); 4567 4568 /* disable baseband loopback mode */ 4569 run_rt3070_rf_read(sc, 22, &rf22); 4570 run_rt3070_rf_write(sc, 22, rf22 & ~0x01); 4571 4572 return (0); 4573 } 4574 4575 static void 4576 run_rt3070_rf_setup(struct run_softc *sc) 4577 { 4578 uint8_t bbp, rf; 4579 int i; 4580 4581 if (sc->mac_ver == 0x3572) { 4582 /* enable DC filter */ 4583 if (sc->mac_rev >= 0x0201) 4584 run_bbp_write(sc, 103, 0xc0); 4585 4586 run_bbp_read(sc, 138, &bbp); 4587 if (sc->ntxchains == 1) 4588 bbp |= 0x20; /* turn off DAC1 */ 4589 if (sc->nrxchains == 1) 4590 bbp &= ~0x02; /* turn off ADC1 */ 4591 run_bbp_write(sc, 138, bbp); 4592 4593 if (sc->mac_rev >= 0x0211) { 4594 /* improve power consumption */ 4595 run_bbp_read(sc, 31, &bbp); 4596 run_bbp_write(sc, 31, bbp & ~0x03); 4597 } 4598 4599 run_rt3070_rf_read(sc, 16, &rf); 4600 rf = (rf & ~0x07) | sc->txmixgain_2ghz; 4601 run_rt3070_rf_write(sc, 16, rf); 4602 4603 } else if (sc->mac_ver == 0x3071) { 4604 /* enable DC filter */ 4605 if (sc->mac_rev >= 0x0201) 4606 run_bbp_write(sc, 103, 0xc0); 4607 4608 run_bbp_read(sc, 138, &bbp); 4609 if (sc->ntxchains == 1) 4610 bbp |= 0x20; /* turn off DAC1 */ 4611 if (sc->nrxchains == 1) 4612 bbp &= ~0x02; /* turn off ADC1 */ 4613 run_bbp_write(sc, 138, bbp); 4614 4615 if (sc->mac_rev >= 0x0211) { 4616 /* improve power consumption */ 4617 run_bbp_read(sc, 31, &bbp); 4618 run_bbp_write(sc, 31, bbp & ~0x03); 4619 } 4620 4621 run_write(sc, RT2860_TX_SW_CFG1, 0); 4622 if (sc->mac_rev < 0x0211) { 4623 run_write(sc, RT2860_TX_SW_CFG2, 4624 sc->patch_dac ? 0x2c : 0x0f); 4625 } else 4626 run_write(sc, RT2860_TX_SW_CFG2, 0); 4627 4628 } else if (sc->mac_ver == 0x3070) { 4629 if (sc->mac_rev >= 0x0201) { 4630 /* enable DC filter */ 4631 run_bbp_write(sc, 103, 0xc0); 4632 4633 /* improve power consumption */ 4634 run_bbp_read(sc, 31, &bbp); 4635 run_bbp_write(sc, 31, bbp & ~0x03); 4636 } 4637 4638 if (sc->mac_rev < 0x0201) { 4639 run_write(sc, RT2860_TX_SW_CFG1, 0); 4640 run_write(sc, RT2860_TX_SW_CFG2, 0x2c); 4641 } else 4642 run_write(sc, RT2860_TX_SW_CFG2, 0); 4643 } 4644 4645 /* initialize RF registers from ROM for >=RT3071*/ 4646 if (sc->mac_ver >= 0x3071) { 4647 for (i = 0; i < 10; i++) { 4648 if (sc->rf[i].reg == 0 || sc->rf[i].reg == 0xff) 4649 continue; 4650 run_rt3070_rf_write(sc, sc->rf[i].reg, sc->rf[i].val); 4651 } 4652 } 4653 } 4654 4655 static int 4656 run_txrx_enable(struct run_softc *sc) 4657 { 4658 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 4659 uint32_t tmp; 4660 int error, ntries; 4661 4662 run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_TX_EN); 4663 for (ntries = 0; ntries < 200; ntries++) { 4664 if ((error = run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp)) != 0) 4665 return error; 4666 if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0) 4667 break; 4668 run_delay(sc, 50); 4669 } 4670 if (ntries == 200) 4671 return ETIMEDOUT; 4672 4673 run_delay(sc, 50); 4674 4675 tmp |= RT2860_RX_DMA_EN | RT2860_TX_DMA_EN | RT2860_TX_WB_DDONE; 4676 run_write(sc, RT2860_WPDMA_GLO_CFG, tmp); 4677 4678 /* enable Rx bulk aggregation (set timeout and limit) */ 4679 tmp = RT2860_USB_TX_EN | RT2860_USB_RX_EN | RT2860_USB_RX_AGG_EN | 4680 RT2860_USB_RX_AGG_TO(128) | RT2860_USB_RX_AGG_LMT(2); 4681 run_write(sc, RT2860_USB_DMA_CFG, tmp); 4682 4683 /* set Rx filter */ 4684 tmp = RT2860_DROP_CRC_ERR | RT2860_DROP_PHY_ERR; 4685 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 4686 tmp |= RT2860_DROP_UC_NOME | RT2860_DROP_DUPL | 4687 RT2860_DROP_CTS | RT2860_DROP_BA | RT2860_DROP_ACK | 4688 RT2860_DROP_VER_ERR | RT2860_DROP_CTRL_RSV | 4689 RT2860_DROP_CFACK | RT2860_DROP_CFEND; 4690 if (ic->ic_opmode == IEEE80211_M_STA) 4691 tmp |= RT2860_DROP_RTS | RT2860_DROP_PSPOLL; 4692 } 4693 run_write(sc, RT2860_RX_FILTR_CFG, tmp); 4694 4695 run_write(sc, RT2860_MAC_SYS_CTRL, 4696 RT2860_MAC_RX_EN | RT2860_MAC_TX_EN); 4697 4698 return (0); 4699 } 4700 4701 static void 4702 run_init_locked(struct run_softc *sc) 4703 { 4704 struct ifnet *ifp = sc->sc_ifp; 4705 struct ieee80211com *ic = ifp->if_l2com; 4706 uint32_t tmp; 4707 uint8_t bbp1, bbp3; 4708 int i; 4709 int ridx; 4710 int ntries; 4711 4712 if (ic->ic_nrunning > 1) 4713 return; 4714 4715 run_stop(sc); 4716 4717 if (run_load_microcode(sc) != 0) { 4718 device_printf(sc->sc_dev, "could not load 8051 microcode\n"); 4719 goto fail; 4720 } 4721 4722 for (ntries = 0; ntries < 100; ntries++) { 4723 if (run_read(sc, RT2860_ASIC_VER_ID, &tmp) != 0) 4724 goto fail; 4725 if (tmp != 0 && tmp != 0xffffffff) 4726 break; 4727 run_delay(sc, 10); 4728 } 4729 if (ntries == 100) 4730 goto fail; 4731 4732 for (i = 0; i != RUN_EP_QUEUES; i++) 4733 run_setup_tx_list(sc, &sc->sc_epq[i]); 4734 4735 run_set_macaddr(sc, IF_LLADDR(ifp)); 4736 4737 for (ntries = 0; ntries < 100; ntries++) { 4738 if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0) 4739 goto fail; 4740 if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0) 4741 break; 4742 run_delay(sc, 10); 4743 } 4744 if (ntries == 100) { 4745 device_printf(sc->sc_dev, "timeout waiting for DMA engine\n"); 4746 goto fail; 4747 } 4748 tmp &= 0xff0; 4749 tmp |= RT2860_TX_WB_DDONE; 4750 run_write(sc, RT2860_WPDMA_GLO_CFG, tmp); 4751 4752 /* turn off PME_OEN to solve high-current issue */ 4753 run_read(sc, RT2860_SYS_CTRL, &tmp); 4754 run_write(sc, RT2860_SYS_CTRL, tmp & ~RT2860_PME_OEN); 4755 4756 run_write(sc, RT2860_MAC_SYS_CTRL, 4757 RT2860_BBP_HRST | RT2860_MAC_SRST); 4758 run_write(sc, RT2860_USB_DMA_CFG, 0); 4759 4760 if (run_reset(sc) != 0) { 4761 device_printf(sc->sc_dev, "could not reset chipset\n"); 4762 goto fail; 4763 } 4764 4765 run_write(sc, RT2860_MAC_SYS_CTRL, 0); 4766 4767 /* init Tx power for all Tx rates (from EEPROM) */ 4768 for (ridx = 0; ridx < 5; ridx++) { 4769 if (sc->txpow20mhz[ridx] == 0xffffffff) 4770 continue; 4771 run_write(sc, RT2860_TX_PWR_CFG(ridx), sc->txpow20mhz[ridx]); 4772 } 4773 4774 for (i = 0; i < N(rt2870_def_mac); i++) 4775 run_write(sc, rt2870_def_mac[i].reg, rt2870_def_mac[i].val); 4776 run_write(sc, RT2860_WMM_AIFSN_CFG, 0x00002273); 4777 run_write(sc, RT2860_WMM_CWMIN_CFG, 0x00002344); 4778 run_write(sc, RT2860_WMM_CWMAX_CFG, 0x000034aa); 4779 4780 if (sc->mac_ver >= 0x3070) { 4781 /* set delay of PA_PE assertion to 1us (unit of 0.25us) */ 4782 run_write(sc, RT2860_TX_SW_CFG0, 4783 4 << RT2860_DLY_PAPE_EN_SHIFT); 4784 } 4785 4786 /* wait while MAC is busy */ 4787 for (ntries = 0; ntries < 100; ntries++) { 4788 if (run_read(sc, RT2860_MAC_STATUS_REG, &tmp) != 0) 4789 goto fail; 4790 if (!(tmp & (RT2860_RX_STATUS_BUSY | RT2860_TX_STATUS_BUSY))) 4791 break; 4792 run_delay(sc, 10); 4793 } 4794 if (ntries == 100) 4795 goto fail; 4796 4797 /* clear Host to MCU mailbox */ 4798 run_write(sc, RT2860_H2M_BBPAGENT, 0); 4799 run_write(sc, RT2860_H2M_MAILBOX, 0); 4800 run_delay(sc, 10); 4801 4802 if (run_bbp_init(sc) != 0) { 4803 device_printf(sc->sc_dev, "could not initialize BBP\n"); 4804 goto fail; 4805 } 4806 4807 /* abort TSF synchronization */ 4808 run_read(sc, RT2860_BCN_TIME_CFG, &tmp); 4809 tmp &= ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN | 4810 RT2860_TBTT_TIMER_EN); 4811 run_write(sc, RT2860_BCN_TIME_CFG, tmp); 4812 4813 /* clear RX WCID search table */ 4814 run_set_region_4(sc, RT2860_WCID_ENTRY(0), 0, 512); 4815 /* clear WCID attribute table */ 4816 run_set_region_4(sc, RT2860_WCID_ATTR(0), 0, 8 * 32); 4817 4818 /* hostapd sets a key before init. So, don't clear it. */ 4819 if (sc->cmdq_key_set != RUN_CMDQ_GO) { 4820 /* clear shared key table */ 4821 run_set_region_4(sc, RT2860_SKEY(0, 0), 0, 8 * 32); 4822 /* clear shared key mode */ 4823 run_set_region_4(sc, RT2860_SKEY_MODE_0_7, 0, 4); 4824 } 4825 4826 run_read(sc, RT2860_US_CYC_CNT, &tmp); 4827 tmp = (tmp & ~0xff) | 0x1e; 4828 run_write(sc, RT2860_US_CYC_CNT, tmp); 4829 4830 if (sc->mac_rev != 0x0101) 4831 run_write(sc, RT2860_TXOP_CTRL_CFG, 0x0000583f); 4832 4833 run_write(sc, RT2860_WMM_TXOP0_CFG, 0); 4834 run_write(sc, RT2860_WMM_TXOP1_CFG, 48 << 16 | 96); 4835 4836 /* write vendor-specific BBP values (from EEPROM) */ 4837 for (i = 0; i < 10; i++) { 4838 if (sc->bbp[i].reg == 0 || sc->bbp[i].reg == 0xff) 4839 continue; 4840 run_bbp_write(sc, sc->bbp[i].reg, sc->bbp[i].val); 4841 } 4842 4843 /* select Main antenna for 1T1R devices */ 4844 if (sc->rf_rev == RT3070_RF_3020) 4845 run_set_rx_antenna(sc, 0); 4846 4847 /* send LEDs operating mode to microcontroller */ 4848 (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED1, sc->led[0]); 4849 (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED2, sc->led[1]); 4850 (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED3, sc->led[2]); 4851 4852 if (sc->mac_ver >= 0x3070) 4853 run_rt3070_rf_init(sc); 4854 4855 /* disable non-existing Rx chains */ 4856 run_bbp_read(sc, 3, &bbp3); 4857 bbp3 &= ~(1 << 3 | 1 << 4); 4858 if (sc->nrxchains == 2) 4859 bbp3 |= 1 << 3; 4860 else if (sc->nrxchains == 3) 4861 bbp3 |= 1 << 4; 4862 run_bbp_write(sc, 3, bbp3); 4863 4864 /* disable non-existing Tx chains */ 4865 run_bbp_read(sc, 1, &bbp1); 4866 if (sc->ntxchains == 1) 4867 bbp1 &= ~(1 << 3 | 1 << 4); 4868 run_bbp_write(sc, 1, bbp1); 4869 4870 if (sc->mac_ver >= 0x3070) 4871 run_rt3070_rf_setup(sc); 4872 4873 /* select default channel */ 4874 run_set_chan(sc, ic->ic_curchan); 4875 4876 /* setup initial protection mode */ 4877 run_updateprot_cb(ic); 4878 4879 /* turn radio LED on */ 4880 run_set_leds(sc, RT2860_LED_RADIO); 4881 4882 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 4883 ifp->if_drv_flags |= IFF_DRV_RUNNING; 4884 sc->cmdq_run = RUN_CMDQ_GO; 4885 4886 for (i = 0; i != RUN_N_XFER; i++) 4887 usbd_xfer_set_stall(sc->sc_xfer[i]); 4888 4889 usbd_transfer_start(sc->sc_xfer[RUN_BULK_RX]); 4890 4891 if (run_txrx_enable(sc) != 0) 4892 goto fail; 4893 4894 return; 4895 4896 fail: 4897 run_stop(sc); 4898 } 4899 4900 static void 4901 run_init(void *arg) 4902 { 4903 struct run_softc *sc = arg; 4904 struct ifnet *ifp = sc->sc_ifp; 4905 struct ieee80211com *ic = ifp->if_l2com; 4906 4907 RUN_LOCK(sc); 4908 run_init_locked(sc); 4909 RUN_UNLOCK(sc); 4910 4911 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 4912 ieee80211_start_all(ic); 4913 } 4914 4915 static void 4916 run_stop(void *arg) 4917 { 4918 struct run_softc *sc = (struct run_softc *)arg; 4919 struct ifnet *ifp = sc->sc_ifp; 4920 uint32_t tmp; 4921 int i; 4922 int ntries; 4923 4924 RUN_LOCK_ASSERT(sc, MA_OWNED); 4925 4926 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 4927 run_set_leds(sc, 0); /* turn all LEDs off */ 4928 4929 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 4930 4931 sc->ratectl_run = RUN_RATECTL_OFF; 4932 sc->cmdq_run = sc->cmdq_key_set; 4933 4934 RUN_UNLOCK(sc); 4935 4936 for(i = 0; i < RUN_N_XFER; i++) 4937 usbd_transfer_drain(sc->sc_xfer[i]); 4938 4939 RUN_LOCK(sc); 4940 4941 if (sc->rx_m != NULL) { 4942 m_free(sc->rx_m); 4943 sc->rx_m = NULL; 4944 } 4945 4946 /* disable Tx/Rx */ 4947 run_read(sc, RT2860_MAC_SYS_CTRL, &tmp); 4948 tmp &= ~(RT2860_MAC_RX_EN | RT2860_MAC_TX_EN); 4949 run_write(sc, RT2860_MAC_SYS_CTRL, tmp); 4950 4951 /* wait for pending Tx to complete */ 4952 for (ntries = 0; ntries < 100; ntries++) { 4953 if (run_read(sc, RT2860_TXRXQ_PCNT, &tmp) != 0) { 4954 DPRINTF("Cannot read Tx queue count\n"); 4955 break; 4956 } 4957 if ((tmp & RT2860_TX2Q_PCNT_MASK) == 0) { 4958 DPRINTF("All Tx cleared\n"); 4959 break; 4960 } 4961 run_delay(sc, 10); 4962 } 4963 if (ntries >= 100) 4964 DPRINTF("There are still pending Tx\n"); 4965 run_delay(sc, 10); 4966 run_write(sc, RT2860_USB_DMA_CFG, 0); 4967 4968 run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_BBP_HRST | RT2860_MAC_SRST); 4969 run_write(sc, RT2860_MAC_SYS_CTRL, 0); 4970 4971 for (i = 0; i != RUN_EP_QUEUES; i++) 4972 run_unsetup_tx_list(sc, &sc->sc_epq[i]); 4973 4974 return; 4975 } 4976 4977 static void 4978 run_delay(struct run_softc *sc, unsigned int ms) 4979 { 4980 usb_pause_mtx(mtx_owned(&sc->sc_mtx) ? 4981 &sc->sc_mtx : NULL, USB_MS_TO_TICKS(ms)); 4982 } 4983 4984 static device_method_t run_methods[] = { 4985 /* Device interface */ 4986 DEVMETHOD(device_probe, run_match), 4987 DEVMETHOD(device_attach, run_attach), 4988 DEVMETHOD(device_detach, run_detach), 4989 DEVMETHOD_END 4990 }; 4991 4992 static driver_t run_driver = { 4993 .name = "run", 4994 .methods = run_methods, 4995 .size = sizeof(struct run_softc) 4996 }; 4997 4998 static devclass_t run_devclass; 4999 5000 DRIVER_MODULE(run, uhub, run_driver, run_devclass, NULL, 0); 5001 MODULE_DEPEND(run, wlan, 1, 1, 1); 5002 MODULE_DEPEND(run, usb, 1, 1, 1); 5003 MODULE_DEPEND(run, firmware, 1, 1, 1); 5004 MODULE_VERSION(run, 1); 5005