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