if_rum.c revision 297165
1/* $FreeBSD: head/sys/dev/usb/wlan/if_rum.c 297165 2016-03-21 21:02:57Z avos $ */ 2 3/*- 4 * Copyright (c) 2005-2007 Damien Bergamini <damien.bergamini@free.fr> 5 * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org> 6 * Copyright (c) 2007-2008 Hans Petter Selasky <hselasky@FreeBSD.org> 7 * Copyright (c) 2015 Andriy Voskoboinyk <avos@FreeBSD.org> 8 * 9 * Permission to use, copy, modify, and distribute this software for any 10 * purpose with or without fee is hereby granted, provided that the above 11 * copyright notice and this permission notice appear in all copies. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 */ 21 22#include <sys/cdefs.h> 23__FBSDID("$FreeBSD: head/sys/dev/usb/wlan/if_rum.c 297165 2016-03-21 21:02:57Z avos $"); 24 25/*- 26 * Ralink Technology RT2501USB/RT2601USB chipset driver 27 * http://www.ralinktech.com.tw/ 28 */ 29 30#include <sys/param.h> 31#include <sys/sockio.h> 32#include <sys/sysctl.h> 33#include <sys/lock.h> 34#include <sys/mutex.h> 35#include <sys/mbuf.h> 36#include <sys/kernel.h> 37#include <sys/socket.h> 38#include <sys/systm.h> 39#include <sys/malloc.h> 40#include <sys/module.h> 41#include <sys/bus.h> 42#include <sys/endian.h> 43#include <sys/kdb.h> 44 45#include <machine/bus.h> 46#include <machine/resource.h> 47#include <sys/rman.h> 48 49#include <net/bpf.h> 50#include <net/if.h> 51#include <net/if_var.h> 52#include <net/if_arp.h> 53#include <net/ethernet.h> 54#include <net/if_dl.h> 55#include <net/if_media.h> 56#include <net/if_types.h> 57 58#ifdef INET 59#include <netinet/in.h> 60#include <netinet/in_systm.h> 61#include <netinet/in_var.h> 62#include <netinet/if_ether.h> 63#include <netinet/ip.h> 64#endif 65 66#include <net80211/ieee80211_var.h> 67#include <net80211/ieee80211_regdomain.h> 68#include <net80211/ieee80211_radiotap.h> 69#include <net80211/ieee80211_ratectl.h> 70 71#include <dev/usb/usb.h> 72#include <dev/usb/usbdi.h> 73#include "usbdevs.h" 74 75#define USB_DEBUG_VAR rum_debug 76#include <dev/usb/usb_debug.h> 77 78#include <dev/usb/wlan/if_rumreg.h> 79#include <dev/usb/wlan/if_rumvar.h> 80#include <dev/usb/wlan/if_rumfw.h> 81 82#ifdef USB_DEBUG 83static int rum_debug = 0; 84 85static SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum"); 86SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RWTUN, &rum_debug, 0, 87 "Debug level"); 88#endif 89 90static const STRUCT_USB_HOST_ID rum_devs[] = { 91#define RUM_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) } 92 RUM_DEV(ABOCOM, HWU54DM), 93 RUM_DEV(ABOCOM, RT2573_2), 94 RUM_DEV(ABOCOM, RT2573_3), 95 RUM_DEV(ABOCOM, RT2573_4), 96 RUM_DEV(ABOCOM, WUG2700), 97 RUM_DEV(AMIT, CGWLUSB2GO), 98 RUM_DEV(ASUS, RT2573_1), 99 RUM_DEV(ASUS, RT2573_2), 100 RUM_DEV(BELKIN, F5D7050A), 101 RUM_DEV(BELKIN, F5D9050V3), 102 RUM_DEV(CISCOLINKSYS, WUSB54GC), 103 RUM_DEV(CISCOLINKSYS, WUSB54GR), 104 RUM_DEV(CONCEPTRONIC2, C54RU2), 105 RUM_DEV(COREGA, CGWLUSB2GL), 106 RUM_DEV(COREGA, CGWLUSB2GPX), 107 RUM_DEV(DICKSMITH, CWD854F), 108 RUM_DEV(DICKSMITH, RT2573), 109 RUM_DEV(EDIMAX, EW7318USG), 110 RUM_DEV(DLINK2, DWLG122C1), 111 RUM_DEV(DLINK2, WUA1340), 112 RUM_DEV(DLINK2, DWA111), 113 RUM_DEV(DLINK2, DWA110), 114 RUM_DEV(GIGABYTE, GNWB01GS), 115 RUM_DEV(GIGABYTE, GNWI05GS), 116 RUM_DEV(GIGASET, RT2573), 117 RUM_DEV(GOODWAY, RT2573), 118 RUM_DEV(GUILLEMOT, HWGUSB254LB), 119 RUM_DEV(GUILLEMOT, HWGUSB254V2AP), 120 RUM_DEV(HUAWEI3COM, WUB320G), 121 RUM_DEV(MELCO, G54HP), 122 RUM_DEV(MELCO, SG54HP), 123 RUM_DEV(MELCO, SG54HG), 124 RUM_DEV(MELCO, WLIUCG), 125 RUM_DEV(MELCO, WLRUCG), 126 RUM_DEV(MELCO, WLRUCGAOSS), 127 RUM_DEV(MSI, RT2573_1), 128 RUM_DEV(MSI, RT2573_2), 129 RUM_DEV(MSI, RT2573_3), 130 RUM_DEV(MSI, RT2573_4), 131 RUM_DEV(NOVATECH, RT2573), 132 RUM_DEV(PLANEX2, GWUS54HP), 133 RUM_DEV(PLANEX2, GWUS54MINI2), 134 RUM_DEV(PLANEX2, GWUSMM), 135 RUM_DEV(QCOM, RT2573), 136 RUM_DEV(QCOM, RT2573_2), 137 RUM_DEV(QCOM, RT2573_3), 138 RUM_DEV(RALINK, RT2573), 139 RUM_DEV(RALINK, RT2573_2), 140 RUM_DEV(RALINK, RT2671), 141 RUM_DEV(SITECOMEU, WL113R2), 142 RUM_DEV(SITECOMEU, WL172), 143 RUM_DEV(SPARKLAN, RT2573), 144 RUM_DEV(SURECOM, RT2573), 145#undef RUM_DEV 146}; 147 148static device_probe_t rum_match; 149static device_attach_t rum_attach; 150static device_detach_t rum_detach; 151 152static usb_callback_t rum_bulk_read_callback; 153static usb_callback_t rum_bulk_write_callback; 154 155static usb_error_t rum_do_request(struct rum_softc *sc, 156 struct usb_device_request *req, void *data); 157static usb_error_t rum_do_mcu_request(struct rum_softc *sc, int); 158static struct ieee80211vap *rum_vap_create(struct ieee80211com *, 159 const char [IFNAMSIZ], int, enum ieee80211_opmode, 160 int, const uint8_t [IEEE80211_ADDR_LEN], 161 const uint8_t [IEEE80211_ADDR_LEN]); 162static void rum_vap_delete(struct ieee80211vap *); 163static void rum_cmdq_cb(void *, int); 164static int rum_cmd_sleepable(struct rum_softc *, const void *, 165 size_t, uint8_t, CMD_FUNC_PROTO); 166static void rum_tx_free(struct rum_tx_data *, int); 167static void rum_setup_tx_list(struct rum_softc *); 168static void rum_unsetup_tx_list(struct rum_softc *); 169static int rum_newstate(struct ieee80211vap *, 170 enum ieee80211_state, int); 171static uint8_t rum_crypto_mode(struct rum_softc *, u_int, int); 172static void rum_setup_tx_desc(struct rum_softc *, 173 struct rum_tx_desc *, struct ieee80211_key *, 174 uint32_t, uint8_t, uint8_t, int, int, int); 175static uint32_t rum_tx_crypto_flags(struct rum_softc *, 176 struct ieee80211_node *, 177 const struct ieee80211_key *); 178static int rum_tx_mgt(struct rum_softc *, struct mbuf *, 179 struct ieee80211_node *); 180static int rum_tx_raw(struct rum_softc *, struct mbuf *, 181 struct ieee80211_node *, 182 const struct ieee80211_bpf_params *); 183static int rum_tx_data(struct rum_softc *, struct mbuf *, 184 struct ieee80211_node *); 185static int rum_transmit(struct ieee80211com *, struct mbuf *); 186static void rum_start(struct rum_softc *); 187static void rum_parent(struct ieee80211com *); 188static void rum_eeprom_read(struct rum_softc *, uint16_t, void *, 189 int); 190static uint32_t rum_read(struct rum_softc *, uint16_t); 191static void rum_read_multi(struct rum_softc *, uint16_t, void *, 192 int); 193static usb_error_t rum_write(struct rum_softc *, uint16_t, uint32_t); 194static usb_error_t rum_write_multi(struct rum_softc *, uint16_t, void *, 195 size_t); 196static usb_error_t rum_setbits(struct rum_softc *, uint16_t, uint32_t); 197static usb_error_t rum_clrbits(struct rum_softc *, uint16_t, uint32_t); 198static usb_error_t rum_modbits(struct rum_softc *, uint16_t, uint32_t, 199 uint32_t); 200static int rum_bbp_busy(struct rum_softc *); 201static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t); 202static uint8_t rum_bbp_read(struct rum_softc *, uint8_t); 203static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t); 204static void rum_select_antenna(struct rum_softc *); 205static void rum_enable_mrr(struct rum_softc *); 206static void rum_set_txpreamble(struct rum_softc *); 207static void rum_set_basicrates(struct rum_softc *); 208static void rum_select_band(struct rum_softc *, 209 struct ieee80211_channel *); 210static void rum_set_chan(struct rum_softc *, 211 struct ieee80211_channel *); 212static void rum_set_maxretry(struct rum_softc *, 213 struct ieee80211vap *); 214static int rum_enable_tsf_sync(struct rum_softc *); 215static void rum_enable_tsf(struct rum_softc *); 216static void rum_abort_tsf_sync(struct rum_softc *); 217static void rum_get_tsf(struct rum_softc *, uint64_t *); 218static void rum_update_slot_cb(struct rum_softc *, 219 union sec_param *, uint8_t); 220static void rum_update_slot(struct ieee80211com *); 221static int rum_wme_update(struct ieee80211com *); 222static void rum_set_bssid(struct rum_softc *, const uint8_t *); 223static void rum_set_macaddr(struct rum_softc *, const uint8_t *); 224static void rum_update_mcast(struct ieee80211com *); 225static void rum_update_promisc(struct ieee80211com *); 226static void rum_setpromisc(struct rum_softc *); 227static const char *rum_get_rf(int); 228static void rum_read_eeprom(struct rum_softc *); 229static int rum_bbp_wakeup(struct rum_softc *); 230static int rum_bbp_init(struct rum_softc *); 231static void rum_clr_shkey_regs(struct rum_softc *); 232static int rum_init(struct rum_softc *); 233static void rum_stop(struct rum_softc *); 234static void rum_load_microcode(struct rum_softc *, const uint8_t *, 235 size_t); 236static int rum_set_beacon(struct rum_softc *, 237 struct ieee80211vap *); 238static int rum_alloc_beacon(struct rum_softc *, 239 struct ieee80211vap *); 240static void rum_update_beacon_cb(struct rum_softc *, 241 union sec_param *, uint8_t); 242static void rum_update_beacon(struct ieee80211vap *, int); 243static int rum_common_key_set(struct rum_softc *, 244 struct ieee80211_key *, uint16_t); 245static void rum_group_key_set_cb(struct rum_softc *, 246 union sec_param *, uint8_t); 247static void rum_group_key_del_cb(struct rum_softc *, 248 union sec_param *, uint8_t); 249static void rum_pair_key_set_cb(struct rum_softc *, 250 union sec_param *, uint8_t); 251static void rum_pair_key_del_cb(struct rum_softc *, 252 union sec_param *, uint8_t); 253static int rum_key_alloc(struct ieee80211vap *, 254 struct ieee80211_key *, ieee80211_keyix *, 255 ieee80211_keyix *); 256static int rum_key_set(struct ieee80211vap *, 257 const struct ieee80211_key *); 258static int rum_key_delete(struct ieee80211vap *, 259 const struct ieee80211_key *); 260static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *, 261 const struct ieee80211_bpf_params *); 262static void rum_scan_start(struct ieee80211com *); 263static void rum_scan_end(struct ieee80211com *); 264static void rum_set_channel(struct ieee80211com *); 265static int rum_get_rssi(struct rum_softc *, uint8_t); 266static void rum_ratectl_start(struct rum_softc *, 267 struct ieee80211_node *); 268static void rum_ratectl_timeout(void *); 269static void rum_ratectl_task(void *, int); 270static int rum_pause(struct rum_softc *, int); 271 272static const struct { 273 uint32_t reg; 274 uint32_t val; 275} rum_def_mac[] = { 276 { RT2573_TXRX_CSR0, 0x025fb032 }, 277 { RT2573_TXRX_CSR1, 0x9eaa9eaf }, 278 { RT2573_TXRX_CSR2, 0x8a8b8c8d }, 279 { RT2573_TXRX_CSR3, 0x00858687 }, 280 { RT2573_TXRX_CSR7, 0x2e31353b }, 281 { RT2573_TXRX_CSR8, 0x2a2a2a2c }, 282 { RT2573_TXRX_CSR15, 0x0000000f }, 283 { RT2573_MAC_CSR6, 0x00000fff }, 284 { RT2573_MAC_CSR8, 0x016c030a }, 285 { RT2573_MAC_CSR10, 0x00000718 }, 286 { RT2573_MAC_CSR12, 0x00000004 }, 287 { RT2573_MAC_CSR13, 0x00007f00 }, 288 { RT2573_SEC_CSR2, 0x00000000 }, 289 { RT2573_SEC_CSR3, 0x00000000 }, 290 { RT2573_SEC_CSR4, 0x00000000 }, 291 { RT2573_PHY_CSR1, 0x000023b0 }, 292 { RT2573_PHY_CSR5, 0x00040a06 }, 293 { RT2573_PHY_CSR6, 0x00080606 }, 294 { RT2573_PHY_CSR7, 0x00000408 }, 295 { RT2573_AIFSN_CSR, 0x00002273 }, 296 { RT2573_CWMIN_CSR, 0x00002344 }, 297 { RT2573_CWMAX_CSR, 0x000034aa } 298}; 299 300static const struct { 301 uint8_t reg; 302 uint8_t val; 303} rum_def_bbp[] = { 304 { 3, 0x80 }, 305 { 15, 0x30 }, 306 { 17, 0x20 }, 307 { 21, 0xc8 }, 308 { 22, 0x38 }, 309 { 23, 0x06 }, 310 { 24, 0xfe }, 311 { 25, 0x0a }, 312 { 26, 0x0d }, 313 { 32, 0x0b }, 314 { 34, 0x12 }, 315 { 37, 0x07 }, 316 { 39, 0xf8 }, 317 { 41, 0x60 }, 318 { 53, 0x10 }, 319 { 54, 0x18 }, 320 { 60, 0x10 }, 321 { 61, 0x04 }, 322 { 62, 0x04 }, 323 { 75, 0xfe }, 324 { 86, 0xfe }, 325 { 88, 0xfe }, 326 { 90, 0x0f }, 327 { 99, 0x00 }, 328 { 102, 0x16 }, 329 { 107, 0x04 } 330}; 331 332static const struct rfprog { 333 uint8_t chan; 334 uint32_t r1, r2, r3, r4; 335} rum_rf5226[] = { 336 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 }, 337 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 }, 338 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 }, 339 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 }, 340 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 }, 341 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 }, 342 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 }, 343 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 }, 344 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 }, 345 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 }, 346 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 }, 347 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 }, 348 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 }, 349 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 }, 350 351 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 }, 352 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 }, 353 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 }, 354 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 }, 355 356 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 }, 357 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 }, 358 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 }, 359 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 }, 360 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 }, 361 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 }, 362 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 }, 363 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 }, 364 365 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 }, 366 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 }, 367 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 }, 368 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 }, 369 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 }, 370 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 }, 371 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 }, 372 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 }, 373 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 }, 374 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 }, 375 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 }, 376 377 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 }, 378 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 }, 379 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 }, 380 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 }, 381 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 } 382}, rum_rf5225[] = { 383 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 }, 384 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 }, 385 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 }, 386 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 }, 387 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 }, 388 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 }, 389 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 }, 390 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 }, 391 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 }, 392 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 }, 393 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 }, 394 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 }, 395 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 }, 396 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 }, 397 398 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 }, 399 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 }, 400 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 }, 401 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 }, 402 403 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 }, 404 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 }, 405 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 }, 406 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 }, 407 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 }, 408 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 }, 409 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 }, 410 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 }, 411 412 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 }, 413 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 }, 414 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 }, 415 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 }, 416 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 }, 417 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 }, 418 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 }, 419 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 }, 420 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 }, 421 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 }, 422 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 }, 423 424 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 }, 425 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 }, 426 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 }, 427 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 }, 428 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 } 429}; 430 431static const struct usb_config rum_config[RUM_N_TRANSFER] = { 432 [RUM_BULK_WR] = { 433 .type = UE_BULK, 434 .endpoint = UE_ADDR_ANY, 435 .direction = UE_DIR_OUT, 436 .bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8), 437 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 438 .callback = rum_bulk_write_callback, 439 .timeout = 5000, /* ms */ 440 }, 441 [RUM_BULK_RD] = { 442 .type = UE_BULK, 443 .endpoint = UE_ADDR_ANY, 444 .direction = UE_DIR_IN, 445 .bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE), 446 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 447 .callback = rum_bulk_read_callback, 448 }, 449}; 450 451static int 452rum_match(device_t self) 453{ 454 struct usb_attach_arg *uaa = device_get_ivars(self); 455 456 if (uaa->usb_mode != USB_MODE_HOST) 457 return (ENXIO); 458 if (uaa->info.bConfigIndex != 0) 459 return (ENXIO); 460 if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX) 461 return (ENXIO); 462 463 return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa)); 464} 465 466static int 467rum_attach(device_t self) 468{ 469 struct usb_attach_arg *uaa = device_get_ivars(self); 470 struct rum_softc *sc = device_get_softc(self); 471 struct ieee80211com *ic = &sc->sc_ic; 472 uint32_t tmp; 473 uint8_t bands[howmany(IEEE80211_MODE_MAX, 8)]; 474 uint8_t iface_index; 475 int error, ntries; 476 477 device_set_usb_desc(self); 478 sc->sc_udev = uaa->device; 479 sc->sc_dev = self; 480 481 RUM_LOCK_INIT(sc); 482 RUM_CMDQ_LOCK_INIT(sc); 483 mbufq_init(&sc->sc_snd, ifqmaxlen); 484 485 iface_index = RT2573_IFACE_INDEX; 486 error = usbd_transfer_setup(uaa->device, &iface_index, 487 sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx); 488 if (error) { 489 device_printf(self, "could not allocate USB transfers, " 490 "err=%s\n", usbd_errstr(error)); 491 goto detach; 492 } 493 494 RUM_LOCK(sc); 495 /* retrieve RT2573 rev. no */ 496 for (ntries = 0; ntries < 100; ntries++) { 497 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0) 498 break; 499 if (rum_pause(sc, hz / 100)) 500 break; 501 } 502 if (ntries == 100) { 503 device_printf(sc->sc_dev, "timeout waiting for chip to settle\n"); 504 RUM_UNLOCK(sc); 505 goto detach; 506 } 507 508 /* retrieve MAC address and various other things from EEPROM */ 509 rum_read_eeprom(sc); 510 511 device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n", 512 tmp, rum_get_rf(sc->rf_rev)); 513 514 rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode)); 515 RUM_UNLOCK(sc); 516 517 ic->ic_softc = sc; 518 ic->ic_name = device_get_nameunit(self); 519 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 520 521 /* set device capabilities */ 522 ic->ic_caps = 523 IEEE80211_C_STA /* station mode supported */ 524 | IEEE80211_C_IBSS /* IBSS mode supported */ 525 | IEEE80211_C_MONITOR /* monitor mode supported */ 526 | IEEE80211_C_HOSTAP /* HostAp mode supported */ 527 | IEEE80211_C_AHDEMO /* adhoc demo mode */ 528 | IEEE80211_C_TXPMGT /* tx power management */ 529 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 530 | IEEE80211_C_SHSLOT /* short slot time supported */ 531 | IEEE80211_C_BGSCAN /* bg scanning supported */ 532 | IEEE80211_C_WPA /* 802.11i */ 533 | IEEE80211_C_WME /* 802.11e */ 534 ; 535 536 ic->ic_cryptocaps = 537 IEEE80211_CRYPTO_WEP | 538 IEEE80211_CRYPTO_AES_CCM | 539 IEEE80211_CRYPTO_TKIPMIC | 540 IEEE80211_CRYPTO_TKIP; 541 542 memset(bands, 0, sizeof(bands)); 543 setbit(bands, IEEE80211_MODE_11B); 544 setbit(bands, IEEE80211_MODE_11G); 545 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) 546 setbit(bands, IEEE80211_MODE_11A); 547 ieee80211_init_channels(ic, NULL, bands); 548 549 ieee80211_ifattach(ic); 550 ic->ic_update_promisc = rum_update_promisc; 551 ic->ic_raw_xmit = rum_raw_xmit; 552 ic->ic_scan_start = rum_scan_start; 553 ic->ic_scan_end = rum_scan_end; 554 ic->ic_set_channel = rum_set_channel; 555 ic->ic_transmit = rum_transmit; 556 ic->ic_parent = rum_parent; 557 ic->ic_vap_create = rum_vap_create; 558 ic->ic_vap_delete = rum_vap_delete; 559 ic->ic_updateslot = rum_update_slot; 560 ic->ic_wme.wme_update = rum_wme_update; 561 ic->ic_update_mcast = rum_update_mcast; 562 563 ieee80211_radiotap_attach(ic, 564 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap), 565 RT2573_TX_RADIOTAP_PRESENT, 566 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 567 RT2573_RX_RADIOTAP_PRESENT); 568 569 TASK_INIT(&sc->cmdq_task, 0, rum_cmdq_cb, sc); 570 571 if (bootverbose) 572 ieee80211_announce(ic); 573 574 return (0); 575 576detach: 577 rum_detach(self); 578 return (ENXIO); /* failure */ 579} 580 581static int 582rum_detach(device_t self) 583{ 584 struct rum_softc *sc = device_get_softc(self); 585 struct ieee80211com *ic = &sc->sc_ic; 586 587 /* Prevent further ioctls */ 588 RUM_LOCK(sc); 589 sc->sc_detached = 1; 590 RUM_UNLOCK(sc); 591 592 /* stop all USB transfers */ 593 usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER); 594 595 /* free TX list, if any */ 596 RUM_LOCK(sc); 597 rum_unsetup_tx_list(sc); 598 RUM_UNLOCK(sc); 599 600 if (ic->ic_softc == sc) { 601 ieee80211_draintask(ic, &sc->cmdq_task); 602 ieee80211_ifdetach(ic); 603 } 604 605 mbufq_drain(&sc->sc_snd); 606 RUM_CMDQ_LOCK_DESTROY(sc); 607 RUM_LOCK_DESTROY(sc); 608 609 return (0); 610} 611 612static usb_error_t 613rum_do_request(struct rum_softc *sc, 614 struct usb_device_request *req, void *data) 615{ 616 usb_error_t err; 617 int ntries = 10; 618 619 while (ntries--) { 620 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx, 621 req, data, 0, NULL, 250 /* ms */); 622 if (err == 0) 623 break; 624 625 DPRINTFN(1, "Control request failed, %s (retrying)\n", 626 usbd_errstr(err)); 627 if (rum_pause(sc, hz / 100)) 628 break; 629 } 630 return (err); 631} 632 633static usb_error_t 634rum_do_mcu_request(struct rum_softc *sc, int request) 635{ 636 struct usb_device_request req; 637 638 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 639 req.bRequest = RT2573_MCU_CNTL; 640 USETW(req.wValue, request); 641 USETW(req.wIndex, 0); 642 USETW(req.wLength, 0); 643 644 return (rum_do_request(sc, &req, NULL)); 645} 646 647static struct ieee80211vap * 648rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, 649 enum ieee80211_opmode opmode, int flags, 650 const uint8_t bssid[IEEE80211_ADDR_LEN], 651 const uint8_t mac[IEEE80211_ADDR_LEN]) 652{ 653 struct rum_softc *sc = ic->ic_softc; 654 struct rum_vap *rvp; 655 struct ieee80211vap *vap; 656 657 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 658 return NULL; 659 rvp = malloc(sizeof(struct rum_vap), M_80211_VAP, M_WAITOK | M_ZERO); 660 vap = &rvp->vap; 661 /* enable s/w bmiss handling for sta mode */ 662 663 if (ieee80211_vap_setup(ic, vap, name, unit, opmode, 664 flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) { 665 /* out of memory */ 666 free(rvp, M_80211_VAP); 667 return (NULL); 668 } 669 670 /* override state transition machine */ 671 rvp->newstate = vap->iv_newstate; 672 vap->iv_newstate = rum_newstate; 673 vap->iv_key_alloc = rum_key_alloc; 674 vap->iv_key_set = rum_key_set; 675 vap->iv_key_delete = rum_key_delete; 676 vap->iv_update_beacon = rum_update_beacon; 677 vap->iv_max_aid = RT2573_ADDR_MAX; 678 679 usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0); 680 TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp); 681 ieee80211_ratectl_init(vap); 682 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */); 683 /* complete setup */ 684 ieee80211_vap_attach(vap, ieee80211_media_change, 685 ieee80211_media_status, mac); 686 ic->ic_opmode = opmode; 687 return vap; 688} 689 690static void 691rum_vap_delete(struct ieee80211vap *vap) 692{ 693 struct rum_vap *rvp = RUM_VAP(vap); 694 struct ieee80211com *ic = vap->iv_ic; 695 696 m_freem(rvp->bcn_mbuf); 697 usb_callout_drain(&rvp->ratectl_ch); 698 ieee80211_draintask(ic, &rvp->ratectl_task); 699 ieee80211_ratectl_deinit(vap); 700 ieee80211_vap_detach(vap); 701 free(rvp, M_80211_VAP); 702} 703 704static void 705rum_cmdq_cb(void *arg, int pending) 706{ 707 struct rum_softc *sc = arg; 708 struct rum_cmdq *rc; 709 710 RUM_CMDQ_LOCK(sc); 711 while (sc->cmdq[sc->cmdq_first].func != NULL) { 712 rc = &sc->cmdq[sc->cmdq_first]; 713 RUM_CMDQ_UNLOCK(sc); 714 715 RUM_LOCK(sc); 716 rc->func(sc, &rc->data, rc->rvp_id); 717 RUM_UNLOCK(sc); 718 719 RUM_CMDQ_LOCK(sc); 720 memset(rc, 0, sizeof (*rc)); 721 sc->cmdq_first = (sc->cmdq_first + 1) % RUM_CMDQ_SIZE; 722 } 723 RUM_CMDQ_UNLOCK(sc); 724} 725 726static int 727rum_cmd_sleepable(struct rum_softc *sc, const void *ptr, size_t len, 728 uint8_t rvp_id, CMD_FUNC_PROTO) 729{ 730 struct ieee80211com *ic = &sc->sc_ic; 731 732 KASSERT(len <= sizeof(union sec_param), ("buffer overflow")); 733 734 RUM_CMDQ_LOCK(sc); 735 if (sc->cmdq[sc->cmdq_last].func != NULL) { 736 device_printf(sc->sc_dev, "%s: cmdq overflow\n", __func__); 737 RUM_CMDQ_UNLOCK(sc); 738 739 return EAGAIN; 740 } 741 742 if (ptr != NULL) 743 memcpy(&sc->cmdq[sc->cmdq_last].data, ptr, len); 744 sc->cmdq[sc->cmdq_last].rvp_id = rvp_id; 745 sc->cmdq[sc->cmdq_last].func = func; 746 sc->cmdq_last = (sc->cmdq_last + 1) % RUM_CMDQ_SIZE; 747 RUM_CMDQ_UNLOCK(sc); 748 749 ieee80211_runtask(ic, &sc->cmdq_task); 750 751 return 0; 752} 753 754static void 755rum_tx_free(struct rum_tx_data *data, int txerr) 756{ 757 struct rum_softc *sc = data->sc; 758 759 if (data->m != NULL) { 760 ieee80211_tx_complete(data->ni, data->m, txerr); 761 data->m = NULL; 762 data->ni = NULL; 763 } 764 STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 765 sc->tx_nfree++; 766} 767 768static void 769rum_setup_tx_list(struct rum_softc *sc) 770{ 771 struct rum_tx_data *data; 772 int i; 773 774 sc->tx_nfree = 0; 775 STAILQ_INIT(&sc->tx_q); 776 STAILQ_INIT(&sc->tx_free); 777 778 for (i = 0; i < RUM_TX_LIST_COUNT; i++) { 779 data = &sc->tx_data[i]; 780 781 data->sc = sc; 782 STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 783 sc->tx_nfree++; 784 } 785} 786 787static void 788rum_unsetup_tx_list(struct rum_softc *sc) 789{ 790 struct rum_tx_data *data; 791 int i; 792 793 /* make sure any subsequent use of the queues will fail */ 794 sc->tx_nfree = 0; 795 STAILQ_INIT(&sc->tx_q); 796 STAILQ_INIT(&sc->tx_free); 797 798 /* free up all node references and mbufs */ 799 for (i = 0; i < RUM_TX_LIST_COUNT; i++) { 800 data = &sc->tx_data[i]; 801 802 if (data->m != NULL) { 803 m_freem(data->m); 804 data->m = NULL; 805 } 806 if (data->ni != NULL) { 807 ieee80211_free_node(data->ni); 808 data->ni = NULL; 809 } 810 } 811} 812 813static int 814rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 815{ 816 struct rum_vap *rvp = RUM_VAP(vap); 817 struct ieee80211com *ic = vap->iv_ic; 818 struct rum_softc *sc = ic->ic_softc; 819 const struct ieee80211_txparam *tp; 820 enum ieee80211_state ostate; 821 struct ieee80211_node *ni; 822 int ret; 823 824 ostate = vap->iv_state; 825 DPRINTF("%s -> %s\n", 826 ieee80211_state_name[ostate], 827 ieee80211_state_name[nstate]); 828 829 IEEE80211_UNLOCK(ic); 830 RUM_LOCK(sc); 831 usb_callout_stop(&rvp->ratectl_ch); 832 833 switch (nstate) { 834 case IEEE80211_S_INIT: 835 if (ostate == IEEE80211_S_RUN) 836 rum_abort_tsf_sync(sc); 837 838 break; 839 840 case IEEE80211_S_RUN: 841 ni = ieee80211_ref_node(vap->iv_bss); 842 843 if (vap->iv_opmode != IEEE80211_M_MONITOR) { 844 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC || 845 ni->ni_chan == IEEE80211_CHAN_ANYC) { 846 ret = EINVAL; 847 goto run_fail; 848 } 849 rum_update_slot_cb(sc, NULL, 0); 850 rum_enable_mrr(sc); 851 rum_set_txpreamble(sc); 852 rum_set_basicrates(sc); 853 rum_set_maxretry(sc, vap); 854 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid); 855 rum_set_bssid(sc, sc->sc_bssid); 856 } 857 858 if (vap->iv_opmode == IEEE80211_M_HOSTAP || 859 vap->iv_opmode == IEEE80211_M_IBSS) { 860 if ((ret = rum_alloc_beacon(sc, vap)) != 0) 861 goto run_fail; 862 } 863 864 if (vap->iv_opmode != IEEE80211_M_MONITOR && 865 vap->iv_opmode != IEEE80211_M_AHDEMO) { 866 if ((ret = rum_enable_tsf_sync(sc)) != 0) 867 goto run_fail; 868 } else 869 rum_enable_tsf(sc); 870 871 /* enable automatic rate adaptation */ 872 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 873 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) 874 rum_ratectl_start(sc, ni); 875 ieee80211_free_node(ni); 876 break; 877 default: 878 break; 879 } 880 RUM_UNLOCK(sc); 881 IEEE80211_LOCK(ic); 882 return (rvp->newstate(vap, nstate, arg)); 883 884run_fail: 885 RUM_UNLOCK(sc); 886 IEEE80211_LOCK(ic); 887 ieee80211_free_node(ni); 888 return ret; 889} 890 891static void 892rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 893{ 894 struct rum_softc *sc = usbd_xfer_softc(xfer); 895 struct ieee80211vap *vap; 896 struct rum_tx_data *data; 897 struct mbuf *m; 898 struct usb_page_cache *pc; 899 unsigned int len; 900 int actlen, sumlen; 901 902 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 903 904 switch (USB_GET_STATE(xfer)) { 905 case USB_ST_TRANSFERRED: 906 DPRINTFN(11, "transfer complete, %d bytes\n", actlen); 907 908 /* free resources */ 909 data = usbd_xfer_get_priv(xfer); 910 rum_tx_free(data, 0); 911 usbd_xfer_set_priv(xfer, NULL); 912 913 /* FALLTHROUGH */ 914 case USB_ST_SETUP: 915tr_setup: 916 data = STAILQ_FIRST(&sc->tx_q); 917 if (data) { 918 STAILQ_REMOVE_HEAD(&sc->tx_q, next); 919 m = data->m; 920 921 if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) { 922 DPRINTFN(0, "data overflow, %u bytes\n", 923 m->m_pkthdr.len); 924 m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE); 925 } 926 pc = usbd_xfer_get_frame(xfer, 0); 927 usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE); 928 usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0, 929 m->m_pkthdr.len); 930 931 vap = data->ni->ni_vap; 932 if (ieee80211_radiotap_active_vap(vap)) { 933 struct rum_tx_radiotap_header *tap = &sc->sc_txtap; 934 935 tap->wt_flags = 0; 936 tap->wt_rate = data->rate; 937 rum_get_tsf(sc, &tap->wt_tsf); 938 tap->wt_antenna = sc->tx_ant; 939 940 ieee80211_radiotap_tx(vap, m); 941 } 942 943 /* align end on a 4-bytes boundary */ 944 len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3; 945 if ((len % 64) == 0) 946 len += 4; 947 948 DPRINTFN(11, "sending frame len=%u xferlen=%u\n", 949 m->m_pkthdr.len, len); 950 951 usbd_xfer_set_frame_len(xfer, 0, len); 952 usbd_xfer_set_priv(xfer, data); 953 954 usbd_transfer_submit(xfer); 955 } 956 rum_start(sc); 957 break; 958 959 default: /* Error */ 960 DPRINTFN(11, "transfer error, %s\n", 961 usbd_errstr(error)); 962 963 counter_u64_add(sc->sc_ic.ic_oerrors, 1); 964 data = usbd_xfer_get_priv(xfer); 965 if (data != NULL) { 966 rum_tx_free(data, error); 967 usbd_xfer_set_priv(xfer, NULL); 968 } 969 970 if (error != USB_ERR_CANCELLED) { 971 if (error == USB_ERR_TIMEOUT) 972 device_printf(sc->sc_dev, "device timeout\n"); 973 974 /* 975 * Try to clear stall first, also if other 976 * errors occur, hence clearing stall 977 * introduces a 50 ms delay: 978 */ 979 usbd_xfer_set_stall(xfer); 980 goto tr_setup; 981 } 982 break; 983 } 984} 985 986static void 987rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 988{ 989 struct rum_softc *sc = usbd_xfer_softc(xfer); 990 struct ieee80211com *ic = &sc->sc_ic; 991 struct ieee80211_frame_min *wh; 992 struct ieee80211_node *ni; 993 struct mbuf *m = NULL; 994 struct usb_page_cache *pc; 995 uint32_t flags; 996 uint8_t rssi = 0; 997 int len; 998 999 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 1000 1001 switch (USB_GET_STATE(xfer)) { 1002 case USB_ST_TRANSFERRED: 1003 1004 DPRINTFN(15, "rx done, actlen=%d\n", len); 1005 1006 if (len < (int)(RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN)) { 1007 DPRINTF("%s: xfer too short %d\n", 1008 device_get_nameunit(sc->sc_dev), len); 1009 counter_u64_add(ic->ic_ierrors, 1); 1010 goto tr_setup; 1011 } 1012 1013 len -= RT2573_RX_DESC_SIZE; 1014 pc = usbd_xfer_get_frame(xfer, 0); 1015 usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE); 1016 1017 rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi); 1018 flags = le32toh(sc->sc_rx_desc.flags); 1019 if (flags & RT2573_RX_CRC_ERROR) { 1020 /* 1021 * This should not happen since we did not 1022 * request to receive those frames when we 1023 * filled RUM_TXRX_CSR2: 1024 */ 1025 DPRINTFN(5, "PHY or CRC error\n"); 1026 counter_u64_add(ic->ic_ierrors, 1); 1027 goto tr_setup; 1028 } 1029 if ((flags & RT2573_RX_DEC_MASK) != RT2573_RX_DEC_OK) { 1030 switch (flags & RT2573_RX_DEC_MASK) { 1031 case RT2573_RX_IV_ERROR: 1032 DPRINTFN(5, "IV/EIV error\n"); 1033 break; 1034 case RT2573_RX_MIC_ERROR: 1035 DPRINTFN(5, "MIC error\n"); 1036 break; 1037 case RT2573_RX_KEY_ERROR: 1038 DPRINTFN(5, "Key error\n"); 1039 break; 1040 } 1041 counter_u64_add(ic->ic_ierrors, 1); 1042 goto tr_setup; 1043 } 1044 1045 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1046 if (m == NULL) { 1047 DPRINTF("could not allocate mbuf\n"); 1048 counter_u64_add(ic->ic_ierrors, 1); 1049 goto tr_setup; 1050 } 1051 usbd_copy_out(pc, RT2573_RX_DESC_SIZE, 1052 mtod(m, uint8_t *), len); 1053 1054 wh = mtod(m, struct ieee80211_frame_min *); 1055 1056 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) && 1057 (flags & RT2573_RX_CIP_MASK) != 1058 RT2573_RX_CIP_MODE(RT2573_MODE_NOSEC)) { 1059 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED; 1060 m->m_flags |= M_WEP; 1061 } 1062 1063 /* finalize mbuf */ 1064 m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff; 1065 1066 if (ieee80211_radiotap_active(ic)) { 1067 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap; 1068 1069 tap->wr_flags = 0; 1070 tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate, 1071 (flags & RT2573_RX_OFDM) ? 1072 IEEE80211_T_OFDM : IEEE80211_T_CCK); 1073 rum_get_tsf(sc, &tap->wr_tsf); 1074 tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi; 1075 tap->wr_antnoise = RT2573_NOISE_FLOOR; 1076 tap->wr_antenna = sc->rx_ant; 1077 } 1078 /* FALLTHROUGH */ 1079 case USB_ST_SETUP: 1080tr_setup: 1081 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1082 usbd_transfer_submit(xfer); 1083 1084 /* 1085 * At the end of a USB callback it is always safe to unlock 1086 * the private mutex of a device! That is why we do the 1087 * "ieee80211_input" here, and not some lines up! 1088 */ 1089 RUM_UNLOCK(sc); 1090 if (m) { 1091 if (m->m_len >= sizeof(struct ieee80211_frame_min)) 1092 ni = ieee80211_find_rxnode(ic, wh); 1093 else 1094 ni = NULL; 1095 1096 if (ni != NULL) { 1097 (void) ieee80211_input(ni, m, rssi, 1098 RT2573_NOISE_FLOOR); 1099 ieee80211_free_node(ni); 1100 } else 1101 (void) ieee80211_input_all(ic, m, rssi, 1102 RT2573_NOISE_FLOOR); 1103 } 1104 RUM_LOCK(sc); 1105 rum_start(sc); 1106 return; 1107 1108 default: /* Error */ 1109 if (error != USB_ERR_CANCELLED) { 1110 /* try to clear stall first */ 1111 usbd_xfer_set_stall(xfer); 1112 goto tr_setup; 1113 } 1114 return; 1115 } 1116} 1117 1118static uint8_t 1119rum_plcp_signal(int rate) 1120{ 1121 switch (rate) { 1122 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 1123 case 12: return 0xb; 1124 case 18: return 0xf; 1125 case 24: return 0xa; 1126 case 36: return 0xe; 1127 case 48: return 0x9; 1128 case 72: return 0xd; 1129 case 96: return 0x8; 1130 case 108: return 0xc; 1131 1132 /* CCK rates (NB: not IEEE std, device-specific) */ 1133 case 2: return 0x0; 1134 case 4: return 0x1; 1135 case 11: return 0x2; 1136 case 22: return 0x3; 1137 } 1138 return 0xff; /* XXX unsupported/unknown rate */ 1139} 1140 1141/* 1142 * Map net80211 cipher to RT2573 security mode. 1143 */ 1144static uint8_t 1145rum_crypto_mode(struct rum_softc *sc, u_int cipher, int keylen) 1146{ 1147 switch (cipher) { 1148 case IEEE80211_CIPHER_WEP: 1149 return (keylen < 8 ? RT2573_MODE_WEP40 : RT2573_MODE_WEP104); 1150 case IEEE80211_CIPHER_TKIP: 1151 return RT2573_MODE_TKIP; 1152 case IEEE80211_CIPHER_AES_CCM: 1153 return RT2573_MODE_AES_CCMP; 1154 default: 1155 device_printf(sc->sc_dev, "unknown cipher %d\n", cipher); 1156 return 0; 1157 } 1158} 1159 1160static void 1161rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc, 1162 struct ieee80211_key *k, uint32_t flags, uint8_t xflags, uint8_t qid, 1163 int hdrlen, int len, int rate) 1164{ 1165 struct ieee80211com *ic = &sc->sc_ic; 1166 struct wmeParams *wmep = &sc->wme_params[qid]; 1167 uint16_t plcp_length; 1168 int remainder; 1169 1170 flags |= RT2573_TX_VALID; 1171 flags |= len << 16; 1172 1173 if (k != NULL && !(k->wk_flags & IEEE80211_KEY_SWCRYPT)) { 1174 const struct ieee80211_cipher *cip = k->wk_cipher; 1175 1176 len += cip->ic_header + cip->ic_trailer + cip->ic_miclen; 1177 1178 desc->eiv = 0; /* for WEP */ 1179 cip->ic_setiv(k, (uint8_t *)&desc->iv); 1180 } 1181 1182 /* setup PLCP fields */ 1183 desc->plcp_signal = rum_plcp_signal(rate); 1184 desc->plcp_service = 4; 1185 1186 len += IEEE80211_CRC_LEN; 1187 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) { 1188 flags |= RT2573_TX_OFDM; 1189 1190 plcp_length = len & 0xfff; 1191 desc->plcp_length_hi = plcp_length >> 6; 1192 desc->plcp_length_lo = plcp_length & 0x3f; 1193 } else { 1194 if (rate == 0) 1195 rate = 2; /* avoid division by zero */ 1196 plcp_length = (16 * len + rate - 1) / rate; 1197 if (rate == 22) { 1198 remainder = (16 * len) % 22; 1199 if (remainder != 0 && remainder < 7) 1200 desc->plcp_service |= RT2573_PLCP_LENGEXT; 1201 } 1202 desc->plcp_length_hi = plcp_length >> 8; 1203 desc->plcp_length_lo = plcp_length & 0xff; 1204 1205 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 1206 desc->plcp_signal |= 0x08; 1207 } 1208 1209 desc->flags = htole32(flags); 1210 desc->hdrlen = hdrlen; 1211 desc->xflags = xflags; 1212 1213 desc->wme = htole16(RT2573_QID(qid) | 1214 RT2573_AIFSN(wmep->wmep_aifsn) | 1215 RT2573_LOGCWMIN(wmep->wmep_logcwmin) | 1216 RT2573_LOGCWMAX(wmep->wmep_logcwmax)); 1217} 1218 1219static int 1220rum_sendprot(struct rum_softc *sc, 1221 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate) 1222{ 1223 struct ieee80211com *ic = ni->ni_ic; 1224 const struct ieee80211_frame *wh; 1225 struct rum_tx_data *data; 1226 struct mbuf *mprot; 1227 int protrate, pktlen, flags, isshort; 1228 uint16_t dur; 1229 1230 RUM_LOCK_ASSERT(sc); 1231 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY, 1232 ("protection %d", prot)); 1233 1234 wh = mtod(m, const struct ieee80211_frame *); 1235 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 1236 1237 protrate = ieee80211_ctl_rate(ic->ic_rt, rate); 1238 1239 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0; 1240 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort) 1241 + ieee80211_ack_duration(ic->ic_rt, rate, isshort); 1242 flags = 0; 1243 if (prot == IEEE80211_PROT_RTSCTS) { 1244 /* NB: CTS is the same size as an ACK */ 1245 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort); 1246 flags |= RT2573_TX_NEED_ACK; 1247 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur); 1248 } else { 1249 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur); 1250 } 1251 if (mprot == NULL) { 1252 /* XXX stat + msg */ 1253 return (ENOBUFS); 1254 } 1255 data = STAILQ_FIRST(&sc->tx_free); 1256 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1257 sc->tx_nfree--; 1258 1259 data->m = mprot; 1260 data->ni = ieee80211_ref_node(ni); 1261 data->rate = protrate; 1262 rum_setup_tx_desc(sc, &data->desc, NULL, flags, 0, 0, 0, 1263 mprot->m_pkthdr.len, protrate); 1264 1265 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1266 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1267 1268 return 0; 1269} 1270 1271static uint32_t 1272rum_tx_crypto_flags(struct rum_softc *sc, struct ieee80211_node *ni, 1273 const struct ieee80211_key *k) 1274{ 1275 struct ieee80211vap *vap = ni->ni_vap; 1276 u_int cipher; 1277 uint32_t flags = 0; 1278 uint8_t mode, pos; 1279 1280 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) { 1281 cipher = k->wk_cipher->ic_cipher; 1282 pos = k->wk_keyix; 1283 mode = rum_crypto_mode(sc, cipher, k->wk_keylen); 1284 if (mode == 0) 1285 return 0; 1286 1287 flags |= RT2573_TX_CIP_MODE(mode); 1288 1289 /* Do not trust GROUP flag */ 1290 if (!(k >= &vap->iv_nw_keys[0] && 1291 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) 1292 flags |= RT2573_TX_KEY_PAIR; 1293 else 1294 pos += 0 * RT2573_SKEY_MAX; /* vap id */ 1295 1296 flags |= RT2573_TX_KEY_ID(pos); 1297 1298 if (cipher == IEEE80211_CIPHER_TKIP) 1299 flags |= RT2573_TX_TKIPMIC; 1300 } 1301 1302 return flags; 1303} 1304 1305static int 1306rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1307{ 1308 struct ieee80211vap *vap = ni->ni_vap; 1309 struct ieee80211com *ic = &sc->sc_ic; 1310 struct rum_tx_data *data; 1311 struct ieee80211_frame *wh; 1312 const struct ieee80211_txparam *tp; 1313 struct ieee80211_key *k = NULL; 1314 uint32_t flags = 0; 1315 uint16_t dur; 1316 uint8_t ac, type, xflags = 0; 1317 int hdrlen; 1318 1319 RUM_LOCK_ASSERT(sc); 1320 1321 data = STAILQ_FIRST(&sc->tx_free); 1322 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1323 sc->tx_nfree--; 1324 1325 wh = mtod(m0, struct ieee80211_frame *); 1326 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 1327 hdrlen = ieee80211_anyhdrsize(wh); 1328 ac = M_WME_GETAC(m0); 1329 1330 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 1331 k = ieee80211_crypto_get_txkey(ni, m0); 1332 if (k == NULL) 1333 return (ENOENT); 1334 1335 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) && 1336 !k->wk_cipher->ic_encap(k, m0)) 1337 return (ENOBUFS); 1338 1339 wh = mtod(m0, struct ieee80211_frame *); 1340 } 1341 1342 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1343 1344 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1345 flags |= RT2573_TX_NEED_ACK; 1346 1347 dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate, 1348 ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1349 USETW(wh->i_dur, dur); 1350 1351 /* tell hardware to add timestamp for probe responses */ 1352 if (type == IEEE80211_FC0_TYPE_MGT && 1353 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 1354 IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1355 flags |= RT2573_TX_TIMESTAMP; 1356 } 1357 1358 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh)) 1359 xflags |= RT2573_TX_HWSEQ; 1360 1361 if (k != NULL) 1362 flags |= rum_tx_crypto_flags(sc, ni, k); 1363 1364 data->m = m0; 1365 data->ni = ni; 1366 data->rate = tp->mgmtrate; 1367 1368 rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen, 1369 m0->m_pkthdr.len, tp->mgmtrate); 1370 1371 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", 1372 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate); 1373 1374 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1375 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1376 1377 return (0); 1378} 1379 1380static int 1381rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni, 1382 const struct ieee80211_bpf_params *params) 1383{ 1384 struct ieee80211com *ic = ni->ni_ic; 1385 struct ieee80211_frame *wh; 1386 struct rum_tx_data *data; 1387 uint32_t flags; 1388 uint8_t ac, type, xflags = 0; 1389 int rate, error; 1390 1391 RUM_LOCK_ASSERT(sc); 1392 1393 wh = mtod(m0, struct ieee80211_frame *); 1394 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 1395 1396 ac = params->ibp_pri & 3; 1397 1398 rate = params->ibp_rate0; 1399 if (!ieee80211_isratevalid(ic->ic_rt, rate)) 1400 return (EINVAL); 1401 1402 flags = 0; 1403 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 1404 flags |= RT2573_TX_NEED_ACK; 1405 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) { 1406 error = rum_sendprot(sc, m0, ni, 1407 params->ibp_flags & IEEE80211_BPF_RTS ? 1408 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY, 1409 rate); 1410 if (error || sc->tx_nfree == 0) 1411 return (ENOBUFS); 1412 1413 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS; 1414 } 1415 1416 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh)) 1417 xflags |= RT2573_TX_HWSEQ; 1418 1419 data = STAILQ_FIRST(&sc->tx_free); 1420 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1421 sc->tx_nfree--; 1422 1423 data->m = m0; 1424 data->ni = ni; 1425 data->rate = rate; 1426 1427 /* XXX need to setup descriptor ourself */ 1428 rum_setup_tx_desc(sc, &data->desc, NULL, flags, xflags, ac, 0, 1429 m0->m_pkthdr.len, rate); 1430 1431 DPRINTFN(10, "sending raw frame len=%u rate=%u\n", 1432 m0->m_pkthdr.len, rate); 1433 1434 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1435 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1436 1437 return 0; 1438} 1439 1440static int 1441rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1442{ 1443 struct ieee80211vap *vap = ni->ni_vap; 1444 struct ieee80211com *ic = &sc->sc_ic; 1445 struct rum_tx_data *data; 1446 struct ieee80211_frame *wh; 1447 const struct ieee80211_txparam *tp; 1448 struct ieee80211_key *k = NULL; 1449 uint32_t flags = 0; 1450 uint16_t dur; 1451 uint8_t ac, type, qos, xflags = 0; 1452 int error, hdrlen, rate; 1453 1454 RUM_LOCK_ASSERT(sc); 1455 1456 wh = mtod(m0, struct ieee80211_frame *); 1457 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 1458 hdrlen = ieee80211_anyhdrsize(wh); 1459 1460 if (IEEE80211_QOS_HAS_SEQ(wh)) 1461 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0]; 1462 else 1463 qos = 0; 1464 ac = M_WME_GETAC(m0); 1465 1466 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)]; 1467 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 1468 rate = tp->mcastrate; 1469 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 1470 rate = tp->ucastrate; 1471 else 1472 rate = ni->ni_txrate; 1473 1474 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 1475 k = ieee80211_crypto_get_txkey(ni, m0); 1476 if (k == NULL) { 1477 m_freem(m0); 1478 return (ENOENT); 1479 } 1480 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) && 1481 !k->wk_cipher->ic_encap(k, m0)) { 1482 m_freem(m0); 1483 return (ENOBUFS); 1484 } 1485 1486 /* packet header may have moved, reset our local pointer */ 1487 wh = mtod(m0, struct ieee80211_frame *); 1488 } 1489 1490 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh)) 1491 xflags |= RT2573_TX_HWSEQ; 1492 1493 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1494 int prot = IEEE80211_PROT_NONE; 1495 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) 1496 prot = IEEE80211_PROT_RTSCTS; 1497 else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 1498 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) 1499 prot = ic->ic_protmode; 1500 if (prot != IEEE80211_PROT_NONE) { 1501 error = rum_sendprot(sc, m0, ni, prot, rate); 1502 if (error || sc->tx_nfree == 0) { 1503 m_freem(m0); 1504 return ENOBUFS; 1505 } 1506 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS; 1507 } 1508 } 1509 1510 if (k != NULL) 1511 flags |= rum_tx_crypto_flags(sc, ni, k); 1512 1513 data = STAILQ_FIRST(&sc->tx_free); 1514 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1515 sc->tx_nfree--; 1516 1517 data->m = m0; 1518 data->ni = ni; 1519 data->rate = rate; 1520 1521 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1522 /* Unicast frame, check if an ACK is expected. */ 1523 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) != 1524 IEEE80211_QOS_ACKPOLICY_NOACK) 1525 flags |= RT2573_TX_NEED_ACK; 1526 1527 dur = ieee80211_ack_duration(ic->ic_rt, rate, 1528 ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1529 USETW(wh->i_dur, dur); 1530 } 1531 1532 rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen, 1533 m0->m_pkthdr.len, rate); 1534 1535 DPRINTFN(10, "sending frame len=%d rate=%d\n", 1536 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate); 1537 1538 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1539 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1540 1541 return 0; 1542} 1543 1544static int 1545rum_transmit(struct ieee80211com *ic, struct mbuf *m) 1546{ 1547 struct rum_softc *sc = ic->ic_softc; 1548 int error; 1549 1550 RUM_LOCK(sc); 1551 if (!sc->sc_running) { 1552 RUM_UNLOCK(sc); 1553 return (ENXIO); 1554 } 1555 error = mbufq_enqueue(&sc->sc_snd, m); 1556 if (error) { 1557 RUM_UNLOCK(sc); 1558 return (error); 1559 } 1560 rum_start(sc); 1561 RUM_UNLOCK(sc); 1562 1563 return (0); 1564} 1565 1566static void 1567rum_start(struct rum_softc *sc) 1568{ 1569 struct ieee80211_node *ni; 1570 struct mbuf *m; 1571 1572 RUM_LOCK_ASSERT(sc); 1573 1574 if (!sc->sc_running) 1575 return; 1576 1577 while (sc->tx_nfree >= RUM_TX_MINFREE && 1578 (m = mbufq_dequeue(&sc->sc_snd)) != NULL) { 1579 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 1580 if (rum_tx_data(sc, m, ni) != 0) { 1581 if_inc_counter(ni->ni_vap->iv_ifp, 1582 IFCOUNTER_OERRORS, 1); 1583 ieee80211_free_node(ni); 1584 break; 1585 } 1586 } 1587} 1588 1589static void 1590rum_parent(struct ieee80211com *ic) 1591{ 1592 struct rum_softc *sc = ic->ic_softc; 1593 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1594 1595 RUM_LOCK(sc); 1596 if (sc->sc_detached) { 1597 RUM_UNLOCK(sc); 1598 return; 1599 } 1600 RUM_UNLOCK(sc); 1601 1602 if (ic->ic_nrunning > 0) { 1603 if (rum_init(sc) == 0) 1604 ieee80211_start_all(ic); 1605 else 1606 ieee80211_stop(vap); 1607 } else 1608 rum_stop(sc); 1609} 1610 1611static void 1612rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len) 1613{ 1614 struct usb_device_request req; 1615 usb_error_t error; 1616 1617 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1618 req.bRequest = RT2573_READ_EEPROM; 1619 USETW(req.wValue, 0); 1620 USETW(req.wIndex, addr); 1621 USETW(req.wLength, len); 1622 1623 error = rum_do_request(sc, &req, buf); 1624 if (error != 0) { 1625 device_printf(sc->sc_dev, "could not read EEPROM: %s\n", 1626 usbd_errstr(error)); 1627 } 1628} 1629 1630static uint32_t 1631rum_read(struct rum_softc *sc, uint16_t reg) 1632{ 1633 uint32_t val; 1634 1635 rum_read_multi(sc, reg, &val, sizeof val); 1636 1637 return le32toh(val); 1638} 1639 1640static void 1641rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len) 1642{ 1643 struct usb_device_request req; 1644 usb_error_t error; 1645 1646 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1647 req.bRequest = RT2573_READ_MULTI_MAC; 1648 USETW(req.wValue, 0); 1649 USETW(req.wIndex, reg); 1650 USETW(req.wLength, len); 1651 1652 error = rum_do_request(sc, &req, buf); 1653 if (error != 0) { 1654 device_printf(sc->sc_dev, 1655 "could not multi read MAC register: %s\n", 1656 usbd_errstr(error)); 1657 } 1658} 1659 1660static usb_error_t 1661rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val) 1662{ 1663 uint32_t tmp = htole32(val); 1664 1665 return (rum_write_multi(sc, reg, &tmp, sizeof tmp)); 1666} 1667 1668static usb_error_t 1669rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len) 1670{ 1671 struct usb_device_request req; 1672 usb_error_t error; 1673 size_t offset; 1674 1675 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1676 req.bRequest = RT2573_WRITE_MULTI_MAC; 1677 USETW(req.wValue, 0); 1678 1679 /* write at most 64 bytes at a time */ 1680 for (offset = 0; offset < len; offset += 64) { 1681 USETW(req.wIndex, reg + offset); 1682 USETW(req.wLength, MIN(len - offset, 64)); 1683 1684 error = rum_do_request(sc, &req, (char *)buf + offset); 1685 if (error != 0) { 1686 device_printf(sc->sc_dev, 1687 "could not multi write MAC register: %s\n", 1688 usbd_errstr(error)); 1689 return (error); 1690 } 1691 } 1692 1693 return (USB_ERR_NORMAL_COMPLETION); 1694} 1695 1696static usb_error_t 1697rum_setbits(struct rum_softc *sc, uint16_t reg, uint32_t mask) 1698{ 1699 return (rum_write(sc, reg, rum_read(sc, reg) | mask)); 1700} 1701 1702static usb_error_t 1703rum_clrbits(struct rum_softc *sc, uint16_t reg, uint32_t mask) 1704{ 1705 return (rum_write(sc, reg, rum_read(sc, reg) & ~mask)); 1706} 1707 1708static usb_error_t 1709rum_modbits(struct rum_softc *sc, uint16_t reg, uint32_t set, uint32_t unset) 1710{ 1711 return (rum_write(sc, reg, (rum_read(sc, reg) & ~unset) | set)); 1712} 1713 1714static int 1715rum_bbp_busy(struct rum_softc *sc) 1716{ 1717 int ntries; 1718 1719 for (ntries = 0; ntries < 100; ntries++) { 1720 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY)) 1721 break; 1722 if (rum_pause(sc, hz / 100)) 1723 break; 1724 } 1725 if (ntries == 100) 1726 return (ETIMEDOUT); 1727 1728 return (0); 1729} 1730 1731static void 1732rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val) 1733{ 1734 uint32_t tmp; 1735 1736 DPRINTFN(2, "reg=0x%08x\n", reg); 1737 1738 if (rum_bbp_busy(sc) != 0) { 1739 device_printf(sc->sc_dev, "could not write to BBP\n"); 1740 return; 1741 } 1742 1743 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val; 1744 rum_write(sc, RT2573_PHY_CSR3, tmp); 1745} 1746 1747static uint8_t 1748rum_bbp_read(struct rum_softc *sc, uint8_t reg) 1749{ 1750 uint32_t val; 1751 int ntries; 1752 1753 DPRINTFN(2, "reg=0x%08x\n", reg); 1754 1755 if (rum_bbp_busy(sc) != 0) { 1756 device_printf(sc->sc_dev, "could not read BBP\n"); 1757 return 0; 1758 } 1759 1760 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8; 1761 rum_write(sc, RT2573_PHY_CSR3, val); 1762 1763 for (ntries = 0; ntries < 100; ntries++) { 1764 val = rum_read(sc, RT2573_PHY_CSR3); 1765 if (!(val & RT2573_BBP_BUSY)) 1766 return val & 0xff; 1767 if (rum_pause(sc, hz / 100)) 1768 break; 1769 } 1770 1771 device_printf(sc->sc_dev, "could not read BBP\n"); 1772 return 0; 1773} 1774 1775static void 1776rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val) 1777{ 1778 uint32_t tmp; 1779 int ntries; 1780 1781 for (ntries = 0; ntries < 100; ntries++) { 1782 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY)) 1783 break; 1784 if (rum_pause(sc, hz / 100)) 1785 break; 1786 } 1787 if (ntries == 100) { 1788 device_printf(sc->sc_dev, "could not write to RF\n"); 1789 return; 1790 } 1791 1792 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 | 1793 (reg & 3); 1794 rum_write(sc, RT2573_PHY_CSR4, tmp); 1795 1796 /* remember last written value in sc */ 1797 sc->rf_regs[reg] = val; 1798 1799 DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff); 1800} 1801 1802static void 1803rum_select_antenna(struct rum_softc *sc) 1804{ 1805 uint8_t bbp4, bbp77; 1806 uint32_t tmp; 1807 1808 bbp4 = rum_bbp_read(sc, 4); 1809 bbp77 = rum_bbp_read(sc, 77); 1810 1811 /* TBD */ 1812 1813 /* make sure Rx is disabled before switching antenna */ 1814 tmp = rum_read(sc, RT2573_TXRX_CSR0); 1815 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX); 1816 1817 rum_bbp_write(sc, 4, bbp4); 1818 rum_bbp_write(sc, 77, bbp77); 1819 1820 rum_write(sc, RT2573_TXRX_CSR0, tmp); 1821} 1822 1823/* 1824 * Enable multi-rate retries for frames sent at OFDM rates. 1825 * In 802.11b/g mode, allow fallback to CCK rates. 1826 */ 1827static void 1828rum_enable_mrr(struct rum_softc *sc) 1829{ 1830 struct ieee80211com *ic = &sc->sc_ic; 1831 1832 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) { 1833 rum_setbits(sc, RT2573_TXRX_CSR4, 1834 RT2573_MRR_ENABLED | RT2573_MRR_CCK_FALLBACK); 1835 } else { 1836 rum_modbits(sc, RT2573_TXRX_CSR4, 1837 RT2573_MRR_ENABLED, RT2573_MRR_CCK_FALLBACK); 1838 } 1839} 1840 1841static void 1842rum_set_txpreamble(struct rum_softc *sc) 1843{ 1844 struct ieee80211com *ic = &sc->sc_ic; 1845 1846 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 1847 rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE); 1848 else 1849 rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE); 1850} 1851 1852static void 1853rum_set_basicrates(struct rum_softc *sc) 1854{ 1855 struct ieee80211com *ic = &sc->sc_ic; 1856 1857 /* update basic rate set */ 1858 if (ic->ic_curmode == IEEE80211_MODE_11B) { 1859 /* 11b basic rates: 1, 2Mbps */ 1860 rum_write(sc, RT2573_TXRX_CSR5, 0x3); 1861 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) { 1862 /* 11a basic rates: 6, 12, 24Mbps */ 1863 rum_write(sc, RT2573_TXRX_CSR5, 0x150); 1864 } else { 1865 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */ 1866 rum_write(sc, RT2573_TXRX_CSR5, 0xf); 1867 } 1868} 1869 1870/* 1871 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference 1872 * driver. 1873 */ 1874static void 1875rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c) 1876{ 1877 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104; 1878 1879 /* update all BBP registers that depend on the band */ 1880 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c; 1881 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48; 1882 if (IEEE80211_IS_CHAN_5GHZ(c)) { 1883 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c; 1884 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10; 1885 } 1886 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) || 1887 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) { 1888 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10; 1889 } 1890 1891 sc->bbp17 = bbp17; 1892 rum_bbp_write(sc, 17, bbp17); 1893 rum_bbp_write(sc, 96, bbp96); 1894 rum_bbp_write(sc, 104, bbp104); 1895 1896 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) || 1897 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) { 1898 rum_bbp_write(sc, 75, 0x80); 1899 rum_bbp_write(sc, 86, 0x80); 1900 rum_bbp_write(sc, 88, 0x80); 1901 } 1902 1903 rum_bbp_write(sc, 35, bbp35); 1904 rum_bbp_write(sc, 97, bbp97); 1905 rum_bbp_write(sc, 98, bbp98); 1906 1907 if (IEEE80211_IS_CHAN_2GHZ(c)) { 1908 rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_2GHZ, 1909 RT2573_PA_PE_5GHZ); 1910 } else { 1911 rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_5GHZ, 1912 RT2573_PA_PE_2GHZ); 1913 } 1914} 1915 1916static void 1917rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c) 1918{ 1919 struct ieee80211com *ic = &sc->sc_ic; 1920 const struct rfprog *rfprog; 1921 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT; 1922 int8_t power; 1923 int i, chan; 1924 1925 chan = ieee80211_chan2ieee(ic, c); 1926 if (chan == 0 || chan == IEEE80211_CHAN_ANY) 1927 return; 1928 1929 /* select the appropriate RF settings based on what EEPROM says */ 1930 rfprog = (sc->rf_rev == RT2573_RF_5225 || 1931 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226; 1932 1933 /* find the settings for this channel (we know it exists) */ 1934 for (i = 0; rfprog[i].chan != chan; i++); 1935 1936 power = sc->txpow[i]; 1937 if (power < 0) { 1938 bbp94 += power; 1939 power = 0; 1940 } else if (power > 31) { 1941 bbp94 += power - 31; 1942 power = 31; 1943 } 1944 1945 /* 1946 * If we are switching from the 2GHz band to the 5GHz band or 1947 * vice-versa, BBP registers need to be reprogrammed. 1948 */ 1949 if (c->ic_flags != ic->ic_curchan->ic_flags) { 1950 rum_select_band(sc, c); 1951 rum_select_antenna(sc); 1952 } 1953 ic->ic_curchan = c; 1954 1955 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1956 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1957 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7); 1958 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1959 1960 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1961 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1962 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1); 1963 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1964 1965 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1966 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1967 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7); 1968 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1969 1970 rum_pause(sc, hz / 100); 1971 1972 /* enable smart mode for MIMO-capable RFs */ 1973 bbp3 = rum_bbp_read(sc, 3); 1974 1975 bbp3 &= ~RT2573_SMART_MODE; 1976 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527) 1977 bbp3 |= RT2573_SMART_MODE; 1978 1979 rum_bbp_write(sc, 3, bbp3); 1980 1981 if (bbp94 != RT2573_BBPR94_DEFAULT) 1982 rum_bbp_write(sc, 94, bbp94); 1983 1984 /* give the chip some extra time to do the switchover */ 1985 rum_pause(sc, hz / 100); 1986} 1987 1988static void 1989rum_set_maxretry(struct rum_softc *sc, struct ieee80211vap *vap) 1990{ 1991 const struct ieee80211_txparam *tp; 1992 struct ieee80211_node *ni = vap->iv_bss; 1993 struct rum_vap *rvp = RUM_VAP(vap); 1994 1995 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)]; 1996 rvp->maxretry = tp->maxretry < 0xf ? tp->maxretry : 0xf; 1997 1998 rum_modbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_RETRY(rvp->maxretry) | 1999 RT2573_LONG_RETRY(rvp->maxretry), 2000 RT2573_SHORT_RETRY_MASK | RT2573_LONG_RETRY_MASK); 2001} 2002 2003/* 2004 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS 2005 * and HostAP operating modes. 2006 */ 2007static int 2008rum_enable_tsf_sync(struct rum_softc *sc) 2009{ 2010 struct ieee80211com *ic = &sc->sc_ic; 2011 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2012 uint32_t tmp; 2013 2014 if (vap->iv_opmode != IEEE80211_M_STA) { 2015 /* 2016 * Change default 16ms TBTT adjustment to 8ms. 2017 * Must be done before enabling beacon generation. 2018 */ 2019 if (rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8) != 0) 2020 return EIO; 2021 } 2022 2023 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000; 2024 2025 /* set beacon interval (in 1/16ms unit) */ 2026 tmp |= vap->iv_bss->ni_intval * 16; 2027 tmp |= RT2573_TSF_TIMER_EN | RT2573_TBTT_TIMER_EN; 2028 2029 switch (vap->iv_opmode) { 2030 case IEEE80211_M_STA: 2031 /* 2032 * Local TSF is always updated with remote TSF on beacon 2033 * reception. 2034 */ 2035 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_STA); 2036 break; 2037 case IEEE80211_M_IBSS: 2038 /* 2039 * Local TSF is updated with remote TSF on beacon reception 2040 * only if the remote TSF is greater than local TSF. 2041 */ 2042 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_IBSS); 2043 tmp |= RT2573_BCN_TX_EN; 2044 break; 2045 case IEEE80211_M_HOSTAP: 2046 /* SYNC with nobody */ 2047 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_HOSTAP); 2048 tmp |= RT2573_BCN_TX_EN; 2049 break; 2050 default: 2051 device_printf(sc->sc_dev, 2052 "Enabling TSF failed. undefined opmode %d\n", 2053 vap->iv_opmode); 2054 return EINVAL; 2055 } 2056 2057 if (rum_write(sc, RT2573_TXRX_CSR9, tmp) != 0) 2058 return EIO; 2059 2060 return 0; 2061} 2062 2063static void 2064rum_enable_tsf(struct rum_softc *sc) 2065{ 2066 rum_modbits(sc, RT2573_TXRX_CSR9, RT2573_TSF_TIMER_EN | 2067 RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_DIS), 0x00ffffff); 2068} 2069 2070static void 2071rum_abort_tsf_sync(struct rum_softc *sc) 2072{ 2073 rum_clrbits(sc, RT2573_TXRX_CSR9, 0x00ffffff); 2074} 2075 2076static void 2077rum_get_tsf(struct rum_softc *sc, uint64_t *buf) 2078{ 2079 rum_read_multi(sc, RT2573_TXRX_CSR12, buf, sizeof (*buf)); 2080} 2081 2082static void 2083rum_update_slot_cb(struct rum_softc *sc, union sec_param *data, uint8_t rvp_id) 2084{ 2085 struct ieee80211com *ic = &sc->sc_ic; 2086 uint8_t slottime; 2087 2088 slottime = IEEE80211_GET_SLOTTIME(ic); 2089 2090 rum_modbits(sc, RT2573_MAC_CSR9, slottime, 0xff); 2091 2092 DPRINTF("setting slot time to %uus\n", slottime); 2093} 2094 2095static void 2096rum_update_slot(struct ieee80211com *ic) 2097{ 2098 rum_cmd_sleepable(ic->ic_softc, NULL, 0, 0, rum_update_slot_cb); 2099} 2100 2101static int 2102rum_wme_update(struct ieee80211com *ic) 2103{ 2104 const struct wmeParams *chanp = 2105 ic->ic_wme.wme_chanParams.cap_wmeParams; 2106 struct rum_softc *sc = ic->ic_softc; 2107 int error = 0; 2108 2109 RUM_LOCK(sc); 2110 error = rum_write(sc, RT2573_AIFSN_CSR, 2111 chanp[WME_AC_VO].wmep_aifsn << 12 | 2112 chanp[WME_AC_VI].wmep_aifsn << 8 | 2113 chanp[WME_AC_BK].wmep_aifsn << 4 | 2114 chanp[WME_AC_BE].wmep_aifsn); 2115 if (error) 2116 goto print_err; 2117 error = rum_write(sc, RT2573_CWMIN_CSR, 2118 chanp[WME_AC_VO].wmep_logcwmin << 12 | 2119 chanp[WME_AC_VI].wmep_logcwmin << 8 | 2120 chanp[WME_AC_BK].wmep_logcwmin << 4 | 2121 chanp[WME_AC_BE].wmep_logcwmin); 2122 if (error) 2123 goto print_err; 2124 error = rum_write(sc, RT2573_CWMAX_CSR, 2125 chanp[WME_AC_VO].wmep_logcwmax << 12 | 2126 chanp[WME_AC_VI].wmep_logcwmax << 8 | 2127 chanp[WME_AC_BK].wmep_logcwmax << 4 | 2128 chanp[WME_AC_BE].wmep_logcwmax); 2129 if (error) 2130 goto print_err; 2131 error = rum_write(sc, RT2573_TXOP01_CSR, 2132 chanp[WME_AC_BK].wmep_txopLimit << 16 | 2133 chanp[WME_AC_BE].wmep_txopLimit); 2134 if (error) 2135 goto print_err; 2136 error = rum_write(sc, RT2573_TXOP23_CSR, 2137 chanp[WME_AC_VO].wmep_txopLimit << 16 | 2138 chanp[WME_AC_VI].wmep_txopLimit); 2139 if (error) 2140 goto print_err; 2141 2142 memcpy(sc->wme_params, chanp, sizeof(*chanp) * WME_NUM_AC); 2143 2144print_err: 2145 RUM_UNLOCK(sc); 2146 if (error != 0) { 2147 device_printf(sc->sc_dev, "%s: WME update failed, error %d\n", 2148 __func__, error); 2149 } 2150 2151 return (error); 2152} 2153 2154static void 2155rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid) 2156{ 2157 2158 rum_write(sc, RT2573_MAC_CSR4, 2159 bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24); 2160 rum_write(sc, RT2573_MAC_CSR5, 2161 bssid[4] | bssid[5] << 8 | RT2573_NUM_BSSID_MSK(1)); 2162} 2163 2164static void 2165rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr) 2166{ 2167 2168 rum_write(sc, RT2573_MAC_CSR2, 2169 addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24); 2170 rum_write(sc, RT2573_MAC_CSR3, 2171 addr[4] | addr[5] << 8 | 0xff << 16); 2172} 2173 2174static void 2175rum_setpromisc(struct rum_softc *sc) 2176{ 2177 struct ieee80211com *ic = &sc->sc_ic; 2178 2179 if (ic->ic_promisc == 0) 2180 rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME); 2181 else 2182 rum_clrbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME); 2183 2184 DPRINTF("%s promiscuous mode\n", ic->ic_promisc > 0 ? 2185 "entering" : "leaving"); 2186} 2187 2188static void 2189rum_update_promisc(struct ieee80211com *ic) 2190{ 2191 struct rum_softc *sc = ic->ic_softc; 2192 2193 RUM_LOCK(sc); 2194 if (sc->sc_running) 2195 rum_setpromisc(sc); 2196 RUM_UNLOCK(sc); 2197} 2198 2199static void 2200rum_update_mcast(struct ieee80211com *ic) 2201{ 2202 /* Ignore. */ 2203} 2204 2205static const char * 2206rum_get_rf(int rev) 2207{ 2208 switch (rev) { 2209 case RT2573_RF_2527: return "RT2527 (MIMO XR)"; 2210 case RT2573_RF_2528: return "RT2528"; 2211 case RT2573_RF_5225: return "RT5225 (MIMO XR)"; 2212 case RT2573_RF_5226: return "RT5226"; 2213 default: return "unknown"; 2214 } 2215} 2216 2217static void 2218rum_read_eeprom(struct rum_softc *sc) 2219{ 2220 uint16_t val; 2221#ifdef RUM_DEBUG 2222 int i; 2223#endif 2224 2225 /* read MAC address */ 2226 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_ic.ic_macaddr, 6); 2227 2228 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2); 2229 val = le16toh(val); 2230 sc->rf_rev = (val >> 11) & 0x1f; 2231 sc->hw_radio = (val >> 10) & 0x1; 2232 sc->rx_ant = (val >> 4) & 0x3; 2233 sc->tx_ant = (val >> 2) & 0x3; 2234 sc->nb_ant = val & 0x3; 2235 2236 DPRINTF("RF revision=%d\n", sc->rf_rev); 2237 2238 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2); 2239 val = le16toh(val); 2240 sc->ext_5ghz_lna = (val >> 6) & 0x1; 2241 sc->ext_2ghz_lna = (val >> 4) & 0x1; 2242 2243 DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n", 2244 sc->ext_2ghz_lna, sc->ext_5ghz_lna); 2245 2246 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2); 2247 val = le16toh(val); 2248 if ((val & 0xff) != 0xff) 2249 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */ 2250 2251 /* Only [-10, 10] is valid */ 2252 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10) 2253 sc->rssi_2ghz_corr = 0; 2254 2255 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2); 2256 val = le16toh(val); 2257 if ((val & 0xff) != 0xff) 2258 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */ 2259 2260 /* Only [-10, 10] is valid */ 2261 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10) 2262 sc->rssi_5ghz_corr = 0; 2263 2264 if (sc->ext_2ghz_lna) 2265 sc->rssi_2ghz_corr -= 14; 2266 if (sc->ext_5ghz_lna) 2267 sc->rssi_5ghz_corr -= 14; 2268 2269 DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n", 2270 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr); 2271 2272 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2); 2273 val = le16toh(val); 2274 if ((val & 0xff) != 0xff) 2275 sc->rffreq = val & 0xff; 2276 2277 DPRINTF("RF freq=%d\n", sc->rffreq); 2278 2279 /* read Tx power for all a/b/g channels */ 2280 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14); 2281 /* XXX default Tx power for 802.11a channels */ 2282 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14); 2283#ifdef RUM_DEBUG 2284 for (i = 0; i < 14; i++) 2285 DPRINTF("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]); 2286#endif 2287 2288 /* read default values for BBP registers */ 2289 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16); 2290#ifdef RUM_DEBUG 2291 for (i = 0; i < 14; i++) { 2292 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff) 2293 continue; 2294 DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg, 2295 sc->bbp_prom[i].val); 2296 } 2297#endif 2298} 2299 2300static int 2301rum_bbp_wakeup(struct rum_softc *sc) 2302{ 2303 unsigned int ntries; 2304 2305 for (ntries = 0; ntries < 100; ntries++) { 2306 if (rum_read(sc, RT2573_MAC_CSR12) & 8) 2307 break; 2308 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */ 2309 if (rum_pause(sc, hz / 100)) 2310 break; 2311 } 2312 if (ntries == 100) { 2313 device_printf(sc->sc_dev, 2314 "timeout waiting for BBP/RF to wakeup\n"); 2315 return (ETIMEDOUT); 2316 } 2317 2318 return (0); 2319} 2320 2321static int 2322rum_bbp_init(struct rum_softc *sc) 2323{ 2324 int i, ntries; 2325 2326 /* wait for BBP to be ready */ 2327 for (ntries = 0; ntries < 100; ntries++) { 2328 const uint8_t val = rum_bbp_read(sc, 0); 2329 if (val != 0 && val != 0xff) 2330 break; 2331 if (rum_pause(sc, hz / 100)) 2332 break; 2333 } 2334 if (ntries == 100) { 2335 device_printf(sc->sc_dev, "timeout waiting for BBP\n"); 2336 return EIO; 2337 } 2338 2339 /* initialize BBP registers to default values */ 2340 for (i = 0; i < nitems(rum_def_bbp); i++) 2341 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val); 2342 2343 /* write vendor-specific BBP values (from EEPROM) */ 2344 for (i = 0; i < 16; i++) { 2345 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff) 2346 continue; 2347 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 2348 } 2349 2350 return 0; 2351} 2352 2353static void 2354rum_clr_shkey_regs(struct rum_softc *sc) 2355{ 2356 rum_write(sc, RT2573_SEC_CSR0, 0); 2357 rum_write(sc, RT2573_SEC_CSR1, 0); 2358 rum_write(sc, RT2573_SEC_CSR5, 0); 2359} 2360 2361static int 2362rum_init(struct rum_softc *sc) 2363{ 2364 struct ieee80211com *ic = &sc->sc_ic; 2365 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2366 uint32_t tmp; 2367 int i, ret; 2368 2369 RUM_LOCK(sc); 2370 if (sc->sc_running) { 2371 ret = 0; 2372 goto end; 2373 } 2374 2375 /* initialize MAC registers to default values */ 2376 for (i = 0; i < nitems(rum_def_mac); i++) 2377 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val); 2378 2379 /* reset some WME parameters to default values */ 2380 sc->wme_params[0].wmep_aifsn = 2; 2381 sc->wme_params[0].wmep_logcwmin = 4; 2382 sc->wme_params[0].wmep_logcwmax = 10; 2383 2384 /* set host ready */ 2385 rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP); 2386 rum_write(sc, RT2573_MAC_CSR1, 0); 2387 2388 /* wait for BBP/RF to wakeup */ 2389 if ((ret = rum_bbp_wakeup(sc)) != 0) 2390 goto end; 2391 2392 if ((ret = rum_bbp_init(sc)) != 0) 2393 goto end; 2394 2395 /* select default channel */ 2396 rum_select_band(sc, ic->ic_curchan); 2397 rum_select_antenna(sc); 2398 rum_set_chan(sc, ic->ic_curchan); 2399 2400 /* clear STA registers */ 2401 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta); 2402 2403 /* clear security registers (if required) */ 2404 if (sc->sc_clr_shkeys == 0) { 2405 rum_clr_shkey_regs(sc); 2406 sc->sc_clr_shkeys = 1; 2407 } 2408 2409 rum_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr); 2410 2411 /* initialize ASIC */ 2412 rum_write(sc, RT2573_MAC_CSR1, RT2573_HOST_READY); 2413 2414 /* 2415 * Allocate Tx and Rx xfer queues. 2416 */ 2417 rum_setup_tx_list(sc); 2418 2419 /* update Rx filter */ 2420 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff; 2421 2422 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR; 2423 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2424 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR | 2425 RT2573_DROP_ACKCTS; 2426 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 2427 tmp |= RT2573_DROP_TODS; 2428 if (ic->ic_promisc == 0) 2429 tmp |= RT2573_DROP_NOT_TO_ME; 2430 } 2431 rum_write(sc, RT2573_TXRX_CSR0, tmp); 2432 2433 sc->sc_running = 1; 2434 usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]); 2435 usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]); 2436 2437end: RUM_UNLOCK(sc); 2438 2439 if (ret != 0) 2440 rum_stop(sc); 2441 2442 return ret; 2443} 2444 2445static void 2446rum_stop(struct rum_softc *sc) 2447{ 2448 2449 RUM_LOCK(sc); 2450 if (!sc->sc_running) { 2451 RUM_UNLOCK(sc); 2452 return; 2453 } 2454 sc->sc_running = 0; 2455 RUM_UNLOCK(sc); 2456 2457 /* 2458 * Drain the USB transfers, if not already drained: 2459 */ 2460 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]); 2461 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]); 2462 2463 RUM_LOCK(sc); 2464 rum_unsetup_tx_list(sc); 2465 2466 /* disable Rx */ 2467 rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DISABLE_RX); 2468 2469 /* reset ASIC */ 2470 rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP); 2471 rum_write(sc, RT2573_MAC_CSR1, 0); 2472 RUM_UNLOCK(sc); 2473} 2474 2475static void 2476rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size) 2477{ 2478 uint16_t reg = RT2573_MCU_CODE_BASE; 2479 usb_error_t err; 2480 2481 /* copy firmware image into NIC */ 2482 for (; size >= 4; reg += 4, ucode += 4, size -= 4) { 2483 err = rum_write(sc, reg, UGETDW(ucode)); 2484 if (err) { 2485 /* firmware already loaded ? */ 2486 device_printf(sc->sc_dev, "Firmware load " 2487 "failure! (ignored)\n"); 2488 break; 2489 } 2490 } 2491 2492 err = rum_do_mcu_request(sc, RT2573_MCU_RUN); 2493 if (err != USB_ERR_NORMAL_COMPLETION) { 2494 device_printf(sc->sc_dev, "could not run firmware: %s\n", 2495 usbd_errstr(err)); 2496 } 2497 2498 /* give the chip some time to boot */ 2499 rum_pause(sc, hz / 8); 2500} 2501 2502static int 2503rum_set_beacon(struct rum_softc *sc, struct ieee80211vap *vap) 2504{ 2505 struct ieee80211com *ic = vap->iv_ic; 2506 struct rum_vap *rvp = RUM_VAP(vap); 2507 struct mbuf *m = rvp->bcn_mbuf; 2508 const struct ieee80211_txparam *tp; 2509 struct rum_tx_desc desc; 2510 2511 RUM_LOCK_ASSERT(sc); 2512 2513 if (m == NULL) 2514 return EINVAL; 2515 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) 2516 return EINVAL; 2517 2518 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)]; 2519 rum_setup_tx_desc(sc, &desc, NULL, RT2573_TX_TIMESTAMP, 2520 RT2573_TX_HWSEQ, 0, 0, m->m_pkthdr.len, tp->mgmtrate); 2521 2522 /* copy the Tx descriptor into NIC memory */ 2523 if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0), (uint8_t *)&desc, 2524 RT2573_TX_DESC_SIZE) != 0) 2525 return EIO; 2526 2527 /* copy beacon header and payload into NIC memory */ 2528 if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0) + RT2573_TX_DESC_SIZE, 2529 mtod(m, uint8_t *), m->m_pkthdr.len) != 0) 2530 return EIO; 2531 2532 return 0; 2533} 2534 2535static int 2536rum_alloc_beacon(struct rum_softc *sc, struct ieee80211vap *vap) 2537{ 2538 struct rum_vap *rvp = RUM_VAP(vap); 2539 struct ieee80211_node *ni = vap->iv_bss; 2540 struct mbuf *m; 2541 2542 if (ni->ni_chan == IEEE80211_CHAN_ANYC) 2543 return EINVAL; 2544 2545 m = ieee80211_beacon_alloc(ni); 2546 if (m == NULL) 2547 return ENOMEM; 2548 2549 if (rvp->bcn_mbuf != NULL) 2550 m_freem(rvp->bcn_mbuf); 2551 2552 rvp->bcn_mbuf = m; 2553 2554 return (rum_set_beacon(sc, vap)); 2555} 2556 2557static void 2558rum_update_beacon_cb(struct rum_softc *sc, union sec_param *data, 2559 uint8_t rvp_id) 2560{ 2561 struct ieee80211vap *vap = data->vap; 2562 2563 rum_set_beacon(sc, vap); 2564} 2565 2566static void 2567rum_update_beacon(struct ieee80211vap *vap, int item) 2568{ 2569 struct ieee80211com *ic = vap->iv_ic; 2570 struct rum_softc *sc = ic->ic_softc; 2571 struct rum_vap *rvp = RUM_VAP(vap); 2572 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off; 2573 struct ieee80211_node *ni = vap->iv_bss; 2574 struct mbuf *m = rvp->bcn_mbuf; 2575 int mcast = 0; 2576 2577 RUM_LOCK(sc); 2578 if (m == NULL) { 2579 m = ieee80211_beacon_alloc(ni); 2580 if (m == NULL) { 2581 device_printf(sc->sc_dev, 2582 "%s: could not allocate beacon frame\n", __func__); 2583 RUM_UNLOCK(sc); 2584 return; 2585 } 2586 rvp->bcn_mbuf = m; 2587 } 2588 2589 switch (item) { 2590 case IEEE80211_BEACON_ERP: 2591 rum_update_slot(ic); 2592 break; 2593 case IEEE80211_BEACON_TIM: 2594 mcast = 1; /*TODO*/ 2595 break; 2596 default: 2597 break; 2598 } 2599 RUM_UNLOCK(sc); 2600 2601 setbit(bo->bo_flags, item); 2602 ieee80211_beacon_update(ni, m, mcast); 2603 2604 rum_cmd_sleepable(sc, &vap, sizeof(vap), 0, rum_update_beacon_cb); 2605} 2606 2607static int 2608rum_common_key_set(struct rum_softc *sc, struct ieee80211_key *k, 2609 uint16_t base) 2610{ 2611 2612 if (rum_write_multi(sc, base, k->wk_key, k->wk_keylen)) 2613 return EIO; 2614 2615 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) { 2616 if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE, 2617 k->wk_txmic, 8)) 2618 return EIO; 2619 if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE + 8, 2620 k->wk_rxmic, 8)) 2621 return EIO; 2622 } 2623 2624 return 0; 2625} 2626 2627static void 2628rum_group_key_set_cb(struct rum_softc *sc, union sec_param *data, 2629 uint8_t rvp_id) 2630{ 2631 struct ieee80211_key *k = &data->key; 2632 uint8_t mode; 2633 2634 if (sc->sc_clr_shkeys == 0) { 2635 rum_clr_shkey_regs(sc); 2636 sc->sc_clr_shkeys = 1; 2637 } 2638 2639 mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen); 2640 if (mode == 0) 2641 goto print_err; 2642 2643 DPRINTFN(1, "setting group key %d for vap %d, mode %d " 2644 "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode, 2645 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off", 2646 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off"); 2647 2648 /* Install the key. */ 2649 if (rum_common_key_set(sc, k, RT2573_SKEY(rvp_id, k->wk_keyix)) != 0) 2650 goto print_err; 2651 2652 /* Set cipher mode. */ 2653 if (rum_modbits(sc, rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5, 2654 mode << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX, 2655 RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX) 2656 != 0) 2657 goto print_err; 2658 2659 /* Mark this key as valid. */ 2660 if (rum_setbits(sc, RT2573_SEC_CSR0, 2661 1 << (rvp_id * RT2573_SKEY_MAX + k->wk_keyix)) != 0) 2662 goto print_err; 2663 2664 return; 2665 2666print_err: 2667 device_printf(sc->sc_dev, "%s: cannot set group key %d for vap %d\n", 2668 __func__, k->wk_keyix, rvp_id); 2669} 2670 2671static void 2672rum_group_key_del_cb(struct rum_softc *sc, union sec_param *data, 2673 uint8_t rvp_id) 2674{ 2675 struct ieee80211_key *k = &data->key; 2676 2677 DPRINTF("%s: removing group key %d for vap %d\n", __func__, 2678 k->wk_keyix, rvp_id); 2679 rum_clrbits(sc, 2680 rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5, 2681 RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX); 2682 rum_clrbits(sc, RT2573_SEC_CSR0, 2683 rvp_id * RT2573_SKEY_MAX + k->wk_keyix); 2684} 2685 2686static void 2687rum_pair_key_set_cb(struct rum_softc *sc, union sec_param *data, 2688 uint8_t rvp_id) 2689{ 2690 struct ieee80211_key *k = &data->key; 2691 uint8_t buf[IEEE80211_ADDR_LEN + 1]; 2692 uint8_t mode; 2693 2694 mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen); 2695 if (mode == 0) 2696 goto print_err; 2697 2698 DPRINTFN(1, "setting pairwise key %d for vap %d, mode %d " 2699 "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode, 2700 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off", 2701 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off"); 2702 2703 /* Install the key. */ 2704 if (rum_common_key_set(sc, k, RT2573_PKEY(k->wk_keyix)) != 0) 2705 goto print_err; 2706 2707 IEEE80211_ADDR_COPY(buf, k->wk_macaddr); 2708 buf[IEEE80211_ADDR_LEN] = mode; 2709 2710 /* Set transmitter address and cipher mode. */ 2711 if (rum_write_multi(sc, RT2573_ADDR_ENTRY(k->wk_keyix), 2712 buf, sizeof buf) != 0) 2713 goto print_err; 2714 2715 /* Enable key table lookup for this vap. */ 2716 if (sc->vap_key_count[rvp_id]++ == 0) 2717 if (rum_setbits(sc, RT2573_SEC_CSR4, 1 << rvp_id) != 0) 2718 goto print_err; 2719 2720 /* Mark this key as valid. */ 2721 if (rum_setbits(sc, 2722 k->wk_keyix < 32 ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3, 2723 1 << (k->wk_keyix % 32)) != 0) 2724 goto print_err; 2725 2726 return; 2727 2728print_err: 2729 device_printf(sc->sc_dev, 2730 "%s: cannot set pairwise key %d, vap %d\n", __func__, k->wk_keyix, 2731 rvp_id); 2732} 2733 2734static void 2735rum_pair_key_del_cb(struct rum_softc *sc, union sec_param *data, 2736 uint8_t rvp_id) 2737{ 2738 struct ieee80211_key *k = &data->key; 2739 2740 DPRINTF("%s: removing key %d\n", __func__, k->wk_keyix); 2741 rum_clrbits(sc, (k->wk_keyix < 32) ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3, 2742 1 << (k->wk_keyix % 32)); 2743 sc->keys_bmap &= ~(1ULL << k->wk_keyix); 2744 if (--sc->vap_key_count[rvp_id] == 0) 2745 rum_clrbits(sc, RT2573_SEC_CSR4, 1 << rvp_id); 2746} 2747 2748static int 2749rum_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k, 2750 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 2751{ 2752 struct rum_softc *sc = vap->iv_ic->ic_softc; 2753 uint8_t i; 2754 2755 if (!(&vap->iv_nw_keys[0] <= k && 2756 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) { 2757 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) { 2758 RUM_LOCK(sc); 2759 for (i = 0; i < RT2573_ADDR_MAX; i++) { 2760 if ((sc->keys_bmap & (1ULL << i)) == 0) { 2761 sc->keys_bmap |= (1ULL << i); 2762 *keyix = i; 2763 break; 2764 } 2765 } 2766 RUM_UNLOCK(sc); 2767 if (i == RT2573_ADDR_MAX) { 2768 device_printf(sc->sc_dev, 2769 "%s: no free space in the key table\n", 2770 __func__); 2771 return 0; 2772 } 2773 } else 2774 *keyix = 0; 2775 } else { 2776 *keyix = k - vap->iv_nw_keys; 2777 } 2778 *rxkeyix = *keyix; 2779 return 1; 2780} 2781 2782static int 2783rum_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k) 2784{ 2785 struct rum_softc *sc = vap->iv_ic->ic_softc; 2786 int group; 2787 2788 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) { 2789 /* Not for us. */ 2790 return 1; 2791 } 2792 2793 group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]; 2794 2795 return !rum_cmd_sleepable(sc, k, sizeof(*k), 0, 2796 group ? rum_group_key_set_cb : rum_pair_key_set_cb); 2797} 2798 2799static int 2800rum_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k) 2801{ 2802 struct rum_softc *sc = vap->iv_ic->ic_softc; 2803 int group; 2804 2805 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) { 2806 /* Not for us. */ 2807 return 1; 2808 } 2809 2810 group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]; 2811 2812 return !rum_cmd_sleepable(sc, k, sizeof(*k), 0, 2813 group ? rum_group_key_del_cb : rum_pair_key_del_cb); 2814} 2815 2816static int 2817rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2818 const struct ieee80211_bpf_params *params) 2819{ 2820 struct rum_softc *sc = ni->ni_ic->ic_softc; 2821 int ret; 2822 2823 RUM_LOCK(sc); 2824 /* prevent management frames from being sent if we're not ready */ 2825 if (!sc->sc_running) { 2826 ret = ENETDOWN; 2827 goto bad; 2828 } 2829 if (sc->tx_nfree < RUM_TX_MINFREE) { 2830 ret = EIO; 2831 goto bad; 2832 } 2833 2834 if (params == NULL) { 2835 /* 2836 * Legacy path; interpret frame contents to decide 2837 * precisely how to send the frame. 2838 */ 2839 if ((ret = rum_tx_mgt(sc, m, ni)) != 0) 2840 goto bad; 2841 } else { 2842 /* 2843 * Caller supplied explicit parameters to use in 2844 * sending the frame. 2845 */ 2846 if ((ret = rum_tx_raw(sc, m, ni, params)) != 0) 2847 goto bad; 2848 } 2849 RUM_UNLOCK(sc); 2850 2851 return 0; 2852bad: 2853 RUM_UNLOCK(sc); 2854 m_freem(m); 2855 return ret; 2856} 2857 2858static void 2859rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni) 2860{ 2861 struct ieee80211vap *vap = ni->ni_vap; 2862 struct rum_vap *rvp = RUM_VAP(vap); 2863 2864 /* clear statistic registers (STA_CSR0 to STA_CSR5) */ 2865 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta); 2866 2867 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp); 2868} 2869 2870static void 2871rum_ratectl_timeout(void *arg) 2872{ 2873 struct rum_vap *rvp = arg; 2874 struct ieee80211vap *vap = &rvp->vap; 2875 struct ieee80211com *ic = vap->iv_ic; 2876 2877 ieee80211_runtask(ic, &rvp->ratectl_task); 2878} 2879 2880static void 2881rum_ratectl_task(void *arg, int pending) 2882{ 2883 struct rum_vap *rvp = arg; 2884 struct ieee80211vap *vap = &rvp->vap; 2885 struct rum_softc *sc = vap->iv_ic->ic_softc; 2886 struct ieee80211_node *ni; 2887 int ok[3], fail; 2888 int sum, success, retrycnt; 2889 2890 RUM_LOCK(sc); 2891 /* read and clear statistic registers (STA_CSR0 to STA_CSR5) */ 2892 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta)); 2893 2894 ok[0] = (le32toh(sc->sta[4]) & 0xffff); /* TX ok w/o retry */ 2895 ok[1] = (le32toh(sc->sta[4]) >> 16); /* TX ok w/ one retry */ 2896 ok[2] = (le32toh(sc->sta[5]) & 0xffff); /* TX ok w/ multiple retries */ 2897 fail = (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */ 2898 2899 success = ok[0] + ok[1] + ok[2]; 2900 sum = success + fail; 2901 /* XXX at least */ 2902 retrycnt = ok[1] + ok[2] * 2 + fail * (rvp->maxretry + 1); 2903 2904 if (sum != 0) { 2905 ni = ieee80211_ref_node(vap->iv_bss); 2906 ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt); 2907 (void) ieee80211_ratectl_rate(ni, NULL, 0); 2908 ieee80211_free_node(ni); 2909 } 2910 2911 /* count TX retry-fail as Tx errors */ 2912 if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, fail); 2913 2914 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp); 2915 RUM_UNLOCK(sc); 2916} 2917 2918static void 2919rum_scan_start(struct ieee80211com *ic) 2920{ 2921 struct rum_softc *sc = ic->ic_softc; 2922 2923 RUM_LOCK(sc); 2924 rum_abort_tsf_sync(sc); 2925 rum_set_bssid(sc, ieee80211broadcastaddr); 2926 RUM_UNLOCK(sc); 2927 2928} 2929 2930static void 2931rum_scan_end(struct ieee80211com *ic) 2932{ 2933 struct rum_softc *sc = ic->ic_softc; 2934 2935 RUM_LOCK(sc); 2936 if (ic->ic_opmode != IEEE80211_M_AHDEMO) 2937 rum_enable_tsf_sync(sc); 2938 else 2939 rum_enable_tsf(sc); 2940 rum_set_bssid(sc, sc->sc_bssid); 2941 RUM_UNLOCK(sc); 2942 2943} 2944 2945static void 2946rum_set_channel(struct ieee80211com *ic) 2947{ 2948 struct rum_softc *sc = ic->ic_softc; 2949 2950 RUM_LOCK(sc); 2951 rum_set_chan(sc, ic->ic_curchan); 2952 RUM_UNLOCK(sc); 2953} 2954 2955static int 2956rum_get_rssi(struct rum_softc *sc, uint8_t raw) 2957{ 2958 struct ieee80211com *ic = &sc->sc_ic; 2959 int lna, agc, rssi; 2960 2961 lna = (raw >> 5) & 0x3; 2962 agc = raw & 0x1f; 2963 2964 if (lna == 0) { 2965 /* 2966 * No RSSI mapping 2967 * 2968 * NB: Since RSSI is relative to noise floor, -1 is 2969 * adequate for caller to know error happened. 2970 */ 2971 return -1; 2972 } 2973 2974 rssi = (2 * agc) - RT2573_NOISE_FLOOR; 2975 2976 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) { 2977 rssi += sc->rssi_2ghz_corr; 2978 2979 if (lna == 1) 2980 rssi -= 64; 2981 else if (lna == 2) 2982 rssi -= 74; 2983 else if (lna == 3) 2984 rssi -= 90; 2985 } else { 2986 rssi += sc->rssi_5ghz_corr; 2987 2988 if (!sc->ext_5ghz_lna && lna != 1) 2989 rssi += 4; 2990 2991 if (lna == 1) 2992 rssi -= 64; 2993 else if (lna == 2) 2994 rssi -= 86; 2995 else if (lna == 3) 2996 rssi -= 100; 2997 } 2998 return rssi; 2999} 3000 3001static int 3002rum_pause(struct rum_softc *sc, int timeout) 3003{ 3004 3005 usb_pause_mtx(&sc->sc_mtx, timeout); 3006 return (0); 3007} 3008 3009static device_method_t rum_methods[] = { 3010 /* Device interface */ 3011 DEVMETHOD(device_probe, rum_match), 3012 DEVMETHOD(device_attach, rum_attach), 3013 DEVMETHOD(device_detach, rum_detach), 3014 DEVMETHOD_END 3015}; 3016 3017static driver_t rum_driver = { 3018 .name = "rum", 3019 .methods = rum_methods, 3020 .size = sizeof(struct rum_softc), 3021}; 3022 3023static devclass_t rum_devclass; 3024 3025DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0); 3026MODULE_DEPEND(rum, wlan, 1, 1, 1); 3027MODULE_DEPEND(rum, usb, 1, 1, 1); 3028MODULE_VERSION(rum, 1); 3029USB_PNP_HOST_INFO(rum_devs); 3030