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