if_rum.c revision 297169
1/* $FreeBSD: head/sys/dev/usb/wlan/if_rum.c 297169 2016-03-21 22:14:48Z 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 297169 2016-03-21 22:14:48Z 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 = 0; 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); 875run_fail: 876 ieee80211_free_node(ni); 877 break; 878 default: 879 break; 880 } 881 RUM_UNLOCK(sc); 882 IEEE80211_LOCK(ic); 883 return (ret == 0 ? rvp->newstate(vap, nstate, arg) : ret); 884} 885 886static void 887rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 888{ 889 struct rum_softc *sc = usbd_xfer_softc(xfer); 890 struct ieee80211vap *vap; 891 struct rum_tx_data *data; 892 struct mbuf *m; 893 struct usb_page_cache *pc; 894 unsigned int len; 895 int actlen, sumlen; 896 897 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 898 899 switch (USB_GET_STATE(xfer)) { 900 case USB_ST_TRANSFERRED: 901 DPRINTFN(11, "transfer complete, %d bytes\n", actlen); 902 903 /* free resources */ 904 data = usbd_xfer_get_priv(xfer); 905 rum_tx_free(data, 0); 906 usbd_xfer_set_priv(xfer, NULL); 907 908 /* FALLTHROUGH */ 909 case USB_ST_SETUP: 910tr_setup: 911 data = STAILQ_FIRST(&sc->tx_q); 912 if (data) { 913 STAILQ_REMOVE_HEAD(&sc->tx_q, next); 914 m = data->m; 915 916 if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) { 917 DPRINTFN(0, "data overflow, %u bytes\n", 918 m->m_pkthdr.len); 919 m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE); 920 } 921 pc = usbd_xfer_get_frame(xfer, 0); 922 usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE); 923 usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0, 924 m->m_pkthdr.len); 925 926 vap = data->ni->ni_vap; 927 if (ieee80211_radiotap_active_vap(vap)) { 928 struct rum_tx_radiotap_header *tap = &sc->sc_txtap; 929 930 tap->wt_flags = 0; 931 tap->wt_rate = data->rate; 932 rum_get_tsf(sc, &tap->wt_tsf); 933 tap->wt_antenna = sc->tx_ant; 934 935 ieee80211_radiotap_tx(vap, m); 936 } 937 938 /* align end on a 4-bytes boundary */ 939 len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3; 940 if ((len % 64) == 0) 941 len += 4; 942 943 DPRINTFN(11, "sending frame len=%u xferlen=%u\n", 944 m->m_pkthdr.len, len); 945 946 usbd_xfer_set_frame_len(xfer, 0, len); 947 usbd_xfer_set_priv(xfer, data); 948 949 usbd_transfer_submit(xfer); 950 } 951 rum_start(sc); 952 break; 953 954 default: /* Error */ 955 DPRINTFN(11, "transfer error, %s\n", 956 usbd_errstr(error)); 957 958 counter_u64_add(sc->sc_ic.ic_oerrors, 1); 959 data = usbd_xfer_get_priv(xfer); 960 if (data != NULL) { 961 rum_tx_free(data, error); 962 usbd_xfer_set_priv(xfer, NULL); 963 } 964 965 if (error != USB_ERR_CANCELLED) { 966 if (error == USB_ERR_TIMEOUT) 967 device_printf(sc->sc_dev, "device timeout\n"); 968 969 /* 970 * Try to clear stall first, also if other 971 * errors occur, hence clearing stall 972 * introduces a 50 ms delay: 973 */ 974 usbd_xfer_set_stall(xfer); 975 goto tr_setup; 976 } 977 break; 978 } 979} 980 981static void 982rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 983{ 984 struct rum_softc *sc = usbd_xfer_softc(xfer); 985 struct ieee80211com *ic = &sc->sc_ic; 986 struct ieee80211_frame_min *wh; 987 struct ieee80211_node *ni; 988 struct mbuf *m = NULL; 989 struct usb_page_cache *pc; 990 uint32_t flags; 991 uint8_t rssi = 0; 992 int len; 993 994 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 995 996 switch (USB_GET_STATE(xfer)) { 997 case USB_ST_TRANSFERRED: 998 999 DPRINTFN(15, "rx done, actlen=%d\n", len); 1000 1001 if (len < (int)(RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN)) { 1002 DPRINTF("%s: xfer too short %d\n", 1003 device_get_nameunit(sc->sc_dev), len); 1004 counter_u64_add(ic->ic_ierrors, 1); 1005 goto tr_setup; 1006 } 1007 1008 len -= RT2573_RX_DESC_SIZE; 1009 pc = usbd_xfer_get_frame(xfer, 0); 1010 usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE); 1011 1012 rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi); 1013 flags = le32toh(sc->sc_rx_desc.flags); 1014 if (flags & RT2573_RX_CRC_ERROR) { 1015 /* 1016 * This should not happen since we did not 1017 * request to receive those frames when we 1018 * filled RUM_TXRX_CSR2: 1019 */ 1020 DPRINTFN(5, "PHY or CRC error\n"); 1021 counter_u64_add(ic->ic_ierrors, 1); 1022 goto tr_setup; 1023 } 1024 if ((flags & RT2573_RX_DEC_MASK) != RT2573_RX_DEC_OK) { 1025 switch (flags & RT2573_RX_DEC_MASK) { 1026 case RT2573_RX_IV_ERROR: 1027 DPRINTFN(5, "IV/EIV error\n"); 1028 break; 1029 case RT2573_RX_MIC_ERROR: 1030 DPRINTFN(5, "MIC error\n"); 1031 break; 1032 case RT2573_RX_KEY_ERROR: 1033 DPRINTFN(5, "Key error\n"); 1034 break; 1035 } 1036 counter_u64_add(ic->ic_ierrors, 1); 1037 goto tr_setup; 1038 } 1039 1040 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1041 if (m == NULL) { 1042 DPRINTF("could not allocate mbuf\n"); 1043 counter_u64_add(ic->ic_ierrors, 1); 1044 goto tr_setup; 1045 } 1046 usbd_copy_out(pc, RT2573_RX_DESC_SIZE, 1047 mtod(m, uint8_t *), len); 1048 1049 wh = mtod(m, struct ieee80211_frame_min *); 1050 1051 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) && 1052 (flags & RT2573_RX_CIP_MASK) != 1053 RT2573_RX_CIP_MODE(RT2573_MODE_NOSEC)) { 1054 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED; 1055 m->m_flags |= M_WEP; 1056 } 1057 1058 /* finalize mbuf */ 1059 m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff; 1060 1061 if (ieee80211_radiotap_active(ic)) { 1062 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap; 1063 1064 tap->wr_flags = 0; 1065 tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate, 1066 (flags & RT2573_RX_OFDM) ? 1067 IEEE80211_T_OFDM : IEEE80211_T_CCK); 1068 rum_get_tsf(sc, &tap->wr_tsf); 1069 tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi; 1070 tap->wr_antnoise = RT2573_NOISE_FLOOR; 1071 tap->wr_antenna = sc->rx_ant; 1072 } 1073 /* FALLTHROUGH */ 1074 case USB_ST_SETUP: 1075tr_setup: 1076 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1077 usbd_transfer_submit(xfer); 1078 1079 /* 1080 * At the end of a USB callback it is always safe to unlock 1081 * the private mutex of a device! That is why we do the 1082 * "ieee80211_input" here, and not some lines up! 1083 */ 1084 RUM_UNLOCK(sc); 1085 if (m) { 1086 if (m->m_len >= sizeof(struct ieee80211_frame_min)) 1087 ni = ieee80211_find_rxnode(ic, wh); 1088 else 1089 ni = NULL; 1090 1091 if (ni != NULL) { 1092 (void) ieee80211_input(ni, m, rssi, 1093 RT2573_NOISE_FLOOR); 1094 ieee80211_free_node(ni); 1095 } else 1096 (void) ieee80211_input_all(ic, m, rssi, 1097 RT2573_NOISE_FLOOR); 1098 } 1099 RUM_LOCK(sc); 1100 rum_start(sc); 1101 return; 1102 1103 default: /* Error */ 1104 if (error != USB_ERR_CANCELLED) { 1105 /* try to clear stall first */ 1106 usbd_xfer_set_stall(xfer); 1107 goto tr_setup; 1108 } 1109 return; 1110 } 1111} 1112 1113static uint8_t 1114rum_plcp_signal(int rate) 1115{ 1116 switch (rate) { 1117 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 1118 case 12: return 0xb; 1119 case 18: return 0xf; 1120 case 24: return 0xa; 1121 case 36: return 0xe; 1122 case 48: return 0x9; 1123 case 72: return 0xd; 1124 case 96: return 0x8; 1125 case 108: return 0xc; 1126 1127 /* CCK rates (NB: not IEEE std, device-specific) */ 1128 case 2: return 0x0; 1129 case 4: return 0x1; 1130 case 11: return 0x2; 1131 case 22: return 0x3; 1132 } 1133 return 0xff; /* XXX unsupported/unknown rate */ 1134} 1135 1136/* 1137 * Map net80211 cipher to RT2573 security mode. 1138 */ 1139static uint8_t 1140rum_crypto_mode(struct rum_softc *sc, u_int cipher, int keylen) 1141{ 1142 switch (cipher) { 1143 case IEEE80211_CIPHER_WEP: 1144 return (keylen < 8 ? RT2573_MODE_WEP40 : RT2573_MODE_WEP104); 1145 case IEEE80211_CIPHER_TKIP: 1146 return RT2573_MODE_TKIP; 1147 case IEEE80211_CIPHER_AES_CCM: 1148 return RT2573_MODE_AES_CCMP; 1149 default: 1150 device_printf(sc->sc_dev, "unknown cipher %d\n", cipher); 1151 return 0; 1152 } 1153} 1154 1155static void 1156rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc, 1157 struct ieee80211_key *k, uint32_t flags, uint8_t xflags, uint8_t qid, 1158 int hdrlen, int len, int rate) 1159{ 1160 struct ieee80211com *ic = &sc->sc_ic; 1161 struct wmeParams *wmep = &sc->wme_params[qid]; 1162 uint16_t plcp_length; 1163 int remainder; 1164 1165 flags |= RT2573_TX_VALID; 1166 flags |= len << 16; 1167 1168 if (k != NULL && !(k->wk_flags & IEEE80211_KEY_SWCRYPT)) { 1169 const struct ieee80211_cipher *cip = k->wk_cipher; 1170 1171 len += cip->ic_header + cip->ic_trailer + cip->ic_miclen; 1172 1173 desc->eiv = 0; /* for WEP */ 1174 cip->ic_setiv(k, (uint8_t *)&desc->iv); 1175 } 1176 1177 /* setup PLCP fields */ 1178 desc->plcp_signal = rum_plcp_signal(rate); 1179 desc->plcp_service = 4; 1180 1181 len += IEEE80211_CRC_LEN; 1182 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) { 1183 flags |= RT2573_TX_OFDM; 1184 1185 plcp_length = len & 0xfff; 1186 desc->plcp_length_hi = plcp_length >> 6; 1187 desc->plcp_length_lo = plcp_length & 0x3f; 1188 } else { 1189 if (rate == 0) 1190 rate = 2; /* avoid division by zero */ 1191 plcp_length = (16 * len + rate - 1) / rate; 1192 if (rate == 22) { 1193 remainder = (16 * len) % 22; 1194 if (remainder != 0 && remainder < 7) 1195 desc->plcp_service |= RT2573_PLCP_LENGEXT; 1196 } 1197 desc->plcp_length_hi = plcp_length >> 8; 1198 desc->plcp_length_lo = plcp_length & 0xff; 1199 1200 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 1201 desc->plcp_signal |= 0x08; 1202 } 1203 1204 desc->flags = htole32(flags); 1205 desc->hdrlen = hdrlen; 1206 desc->xflags = xflags; 1207 1208 desc->wme = htole16(RT2573_QID(qid) | 1209 RT2573_AIFSN(wmep->wmep_aifsn) | 1210 RT2573_LOGCWMIN(wmep->wmep_logcwmin) | 1211 RT2573_LOGCWMAX(wmep->wmep_logcwmax)); 1212} 1213 1214static int 1215rum_sendprot(struct rum_softc *sc, 1216 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate) 1217{ 1218 struct ieee80211com *ic = ni->ni_ic; 1219 const struct ieee80211_frame *wh; 1220 struct rum_tx_data *data; 1221 struct mbuf *mprot; 1222 int protrate, pktlen, flags, isshort; 1223 uint16_t dur; 1224 1225 RUM_LOCK_ASSERT(sc); 1226 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY, 1227 ("protection %d", prot)); 1228 1229 wh = mtod(m, const struct ieee80211_frame *); 1230 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 1231 1232 protrate = ieee80211_ctl_rate(ic->ic_rt, rate); 1233 1234 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0; 1235 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort) 1236 + ieee80211_ack_duration(ic->ic_rt, rate, isshort); 1237 flags = 0; 1238 if (prot == IEEE80211_PROT_RTSCTS) { 1239 /* NB: CTS is the same size as an ACK */ 1240 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort); 1241 flags |= RT2573_TX_NEED_ACK; 1242 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur); 1243 } else { 1244 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur); 1245 } 1246 if (mprot == NULL) { 1247 /* XXX stat + msg */ 1248 return (ENOBUFS); 1249 } 1250 data = STAILQ_FIRST(&sc->tx_free); 1251 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1252 sc->tx_nfree--; 1253 1254 data->m = mprot; 1255 data->ni = ieee80211_ref_node(ni); 1256 data->rate = protrate; 1257 rum_setup_tx_desc(sc, &data->desc, NULL, flags, 0, 0, 0, 1258 mprot->m_pkthdr.len, protrate); 1259 1260 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1261 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1262 1263 return 0; 1264} 1265 1266static uint32_t 1267rum_tx_crypto_flags(struct rum_softc *sc, struct ieee80211_node *ni, 1268 const struct ieee80211_key *k) 1269{ 1270 struct ieee80211vap *vap = ni->ni_vap; 1271 u_int cipher; 1272 uint32_t flags = 0; 1273 uint8_t mode, pos; 1274 1275 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) { 1276 cipher = k->wk_cipher->ic_cipher; 1277 pos = k->wk_keyix; 1278 mode = rum_crypto_mode(sc, cipher, k->wk_keylen); 1279 if (mode == 0) 1280 return 0; 1281 1282 flags |= RT2573_TX_CIP_MODE(mode); 1283 1284 /* Do not trust GROUP flag */ 1285 if (!(k >= &vap->iv_nw_keys[0] && 1286 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) 1287 flags |= RT2573_TX_KEY_PAIR; 1288 else 1289 pos += 0 * RT2573_SKEY_MAX; /* vap id */ 1290 1291 flags |= RT2573_TX_KEY_ID(pos); 1292 1293 if (cipher == IEEE80211_CIPHER_TKIP) 1294 flags |= RT2573_TX_TKIPMIC; 1295 } 1296 1297 return flags; 1298} 1299 1300static int 1301rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1302{ 1303 struct ieee80211vap *vap = ni->ni_vap; 1304 struct ieee80211com *ic = &sc->sc_ic; 1305 struct rum_tx_data *data; 1306 struct ieee80211_frame *wh; 1307 const struct ieee80211_txparam *tp; 1308 struct ieee80211_key *k = NULL; 1309 uint32_t flags = 0; 1310 uint16_t dur; 1311 uint8_t ac, type, xflags = 0; 1312 int hdrlen; 1313 1314 RUM_LOCK_ASSERT(sc); 1315 1316 data = STAILQ_FIRST(&sc->tx_free); 1317 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1318 sc->tx_nfree--; 1319 1320 wh = mtod(m0, struct ieee80211_frame *); 1321 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 1322 hdrlen = ieee80211_anyhdrsize(wh); 1323 ac = M_WME_GETAC(m0); 1324 1325 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 1326 k = ieee80211_crypto_get_txkey(ni, m0); 1327 if (k == NULL) 1328 return (ENOENT); 1329 1330 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) && 1331 !k->wk_cipher->ic_encap(k, m0)) 1332 return (ENOBUFS); 1333 1334 wh = mtod(m0, struct ieee80211_frame *); 1335 } 1336 1337 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1338 1339 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1340 flags |= RT2573_TX_NEED_ACK; 1341 1342 dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate, 1343 ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1344 USETW(wh->i_dur, dur); 1345 1346 /* tell hardware to add timestamp for probe responses */ 1347 if (type == IEEE80211_FC0_TYPE_MGT && 1348 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 1349 IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1350 flags |= RT2573_TX_TIMESTAMP; 1351 } 1352 1353 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh)) 1354 xflags |= RT2573_TX_HWSEQ; 1355 1356 if (k != NULL) 1357 flags |= rum_tx_crypto_flags(sc, ni, k); 1358 1359 data->m = m0; 1360 data->ni = ni; 1361 data->rate = tp->mgmtrate; 1362 1363 rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen, 1364 m0->m_pkthdr.len, tp->mgmtrate); 1365 1366 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", 1367 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate); 1368 1369 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1370 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1371 1372 return (0); 1373} 1374 1375static int 1376rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni, 1377 const struct ieee80211_bpf_params *params) 1378{ 1379 struct ieee80211com *ic = ni->ni_ic; 1380 struct ieee80211_frame *wh; 1381 struct rum_tx_data *data; 1382 uint32_t flags; 1383 uint8_t ac, type, xflags = 0; 1384 int rate, error; 1385 1386 RUM_LOCK_ASSERT(sc); 1387 1388 wh = mtod(m0, struct ieee80211_frame *); 1389 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 1390 1391 ac = params->ibp_pri & 3; 1392 1393 rate = params->ibp_rate0; 1394 if (!ieee80211_isratevalid(ic->ic_rt, rate)) 1395 return (EINVAL); 1396 1397 flags = 0; 1398 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 1399 flags |= RT2573_TX_NEED_ACK; 1400 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) { 1401 error = rum_sendprot(sc, m0, ni, 1402 params->ibp_flags & IEEE80211_BPF_RTS ? 1403 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY, 1404 rate); 1405 if (error || sc->tx_nfree == 0) 1406 return (ENOBUFS); 1407 1408 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS; 1409 } 1410 1411 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh)) 1412 xflags |= RT2573_TX_HWSEQ; 1413 1414 data = STAILQ_FIRST(&sc->tx_free); 1415 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1416 sc->tx_nfree--; 1417 1418 data->m = m0; 1419 data->ni = ni; 1420 data->rate = rate; 1421 1422 /* XXX need to setup descriptor ourself */ 1423 rum_setup_tx_desc(sc, &data->desc, NULL, flags, xflags, ac, 0, 1424 m0->m_pkthdr.len, rate); 1425 1426 DPRINTFN(10, "sending raw frame len=%u rate=%u\n", 1427 m0->m_pkthdr.len, rate); 1428 1429 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1430 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1431 1432 return 0; 1433} 1434 1435static int 1436rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1437{ 1438 struct ieee80211vap *vap = ni->ni_vap; 1439 struct ieee80211com *ic = &sc->sc_ic; 1440 struct rum_tx_data *data; 1441 struct ieee80211_frame *wh; 1442 const struct ieee80211_txparam *tp; 1443 struct ieee80211_key *k = NULL; 1444 uint32_t flags = 0; 1445 uint16_t dur; 1446 uint8_t ac, type, qos, xflags = 0; 1447 int error, hdrlen, rate; 1448 1449 RUM_LOCK_ASSERT(sc); 1450 1451 wh = mtod(m0, struct ieee80211_frame *); 1452 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 1453 hdrlen = ieee80211_anyhdrsize(wh); 1454 1455 if (IEEE80211_QOS_HAS_SEQ(wh)) 1456 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0]; 1457 else 1458 qos = 0; 1459 ac = M_WME_GETAC(m0); 1460 1461 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)]; 1462 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 1463 rate = tp->mcastrate; 1464 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 1465 rate = tp->ucastrate; 1466 else 1467 rate = ni->ni_txrate; 1468 1469 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 1470 k = ieee80211_crypto_get_txkey(ni, m0); 1471 if (k == NULL) { 1472 m_freem(m0); 1473 return (ENOENT); 1474 } 1475 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) && 1476 !k->wk_cipher->ic_encap(k, m0)) { 1477 m_freem(m0); 1478 return (ENOBUFS); 1479 } 1480 1481 /* packet header may have moved, reset our local pointer */ 1482 wh = mtod(m0, struct ieee80211_frame *); 1483 } 1484 1485 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh)) 1486 xflags |= RT2573_TX_HWSEQ; 1487 1488 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1489 int prot = IEEE80211_PROT_NONE; 1490 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) 1491 prot = IEEE80211_PROT_RTSCTS; 1492 else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 1493 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) 1494 prot = ic->ic_protmode; 1495 if (prot != IEEE80211_PROT_NONE) { 1496 error = rum_sendprot(sc, m0, ni, prot, rate); 1497 if (error || sc->tx_nfree == 0) { 1498 m_freem(m0); 1499 return ENOBUFS; 1500 } 1501 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS; 1502 } 1503 } 1504 1505 if (k != NULL) 1506 flags |= rum_tx_crypto_flags(sc, ni, k); 1507 1508 data = STAILQ_FIRST(&sc->tx_free); 1509 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1510 sc->tx_nfree--; 1511 1512 data->m = m0; 1513 data->ni = ni; 1514 data->rate = rate; 1515 1516 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1517 /* Unicast frame, check if an ACK is expected. */ 1518 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) != 1519 IEEE80211_QOS_ACKPOLICY_NOACK) 1520 flags |= RT2573_TX_NEED_ACK; 1521 1522 dur = ieee80211_ack_duration(ic->ic_rt, rate, 1523 ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1524 USETW(wh->i_dur, dur); 1525 } 1526 1527 rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen, 1528 m0->m_pkthdr.len, rate); 1529 1530 DPRINTFN(10, "sending frame len=%d rate=%d\n", 1531 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate); 1532 1533 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1534 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1535 1536 return 0; 1537} 1538 1539static int 1540rum_transmit(struct ieee80211com *ic, struct mbuf *m) 1541{ 1542 struct rum_softc *sc = ic->ic_softc; 1543 int error; 1544 1545 RUM_LOCK(sc); 1546 if (!sc->sc_running) { 1547 RUM_UNLOCK(sc); 1548 return (ENXIO); 1549 } 1550 error = mbufq_enqueue(&sc->sc_snd, m); 1551 if (error) { 1552 RUM_UNLOCK(sc); 1553 return (error); 1554 } 1555 rum_start(sc); 1556 RUM_UNLOCK(sc); 1557 1558 return (0); 1559} 1560 1561static void 1562rum_start(struct rum_softc *sc) 1563{ 1564 struct ieee80211_node *ni; 1565 struct mbuf *m; 1566 1567 RUM_LOCK_ASSERT(sc); 1568 1569 if (!sc->sc_running) 1570 return; 1571 1572 while (sc->tx_nfree >= RUM_TX_MINFREE && 1573 (m = mbufq_dequeue(&sc->sc_snd)) != NULL) { 1574 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 1575 if (rum_tx_data(sc, m, ni) != 0) { 1576 if_inc_counter(ni->ni_vap->iv_ifp, 1577 IFCOUNTER_OERRORS, 1); 1578 ieee80211_free_node(ni); 1579 break; 1580 } 1581 } 1582} 1583 1584static void 1585rum_parent(struct ieee80211com *ic) 1586{ 1587 struct rum_softc *sc = ic->ic_softc; 1588 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1589 1590 RUM_LOCK(sc); 1591 if (sc->sc_detached) { 1592 RUM_UNLOCK(sc); 1593 return; 1594 } 1595 RUM_UNLOCK(sc); 1596 1597 if (ic->ic_nrunning > 0) { 1598 if (rum_init(sc) == 0) 1599 ieee80211_start_all(ic); 1600 else 1601 ieee80211_stop(vap); 1602 } else 1603 rum_stop(sc); 1604} 1605 1606static void 1607rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len) 1608{ 1609 struct usb_device_request req; 1610 usb_error_t error; 1611 1612 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1613 req.bRequest = RT2573_READ_EEPROM; 1614 USETW(req.wValue, 0); 1615 USETW(req.wIndex, addr); 1616 USETW(req.wLength, len); 1617 1618 error = rum_do_request(sc, &req, buf); 1619 if (error != 0) { 1620 device_printf(sc->sc_dev, "could not read EEPROM: %s\n", 1621 usbd_errstr(error)); 1622 } 1623} 1624 1625static uint32_t 1626rum_read(struct rum_softc *sc, uint16_t reg) 1627{ 1628 uint32_t val; 1629 1630 rum_read_multi(sc, reg, &val, sizeof val); 1631 1632 return le32toh(val); 1633} 1634 1635static void 1636rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len) 1637{ 1638 struct usb_device_request req; 1639 usb_error_t error; 1640 1641 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1642 req.bRequest = RT2573_READ_MULTI_MAC; 1643 USETW(req.wValue, 0); 1644 USETW(req.wIndex, reg); 1645 USETW(req.wLength, len); 1646 1647 error = rum_do_request(sc, &req, buf); 1648 if (error != 0) { 1649 device_printf(sc->sc_dev, 1650 "could not multi read MAC register: %s\n", 1651 usbd_errstr(error)); 1652 } 1653} 1654 1655static usb_error_t 1656rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val) 1657{ 1658 uint32_t tmp = htole32(val); 1659 1660 return (rum_write_multi(sc, reg, &tmp, sizeof tmp)); 1661} 1662 1663static usb_error_t 1664rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len) 1665{ 1666 struct usb_device_request req; 1667 usb_error_t error; 1668 size_t offset; 1669 1670 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1671 req.bRequest = RT2573_WRITE_MULTI_MAC; 1672 USETW(req.wValue, 0); 1673 1674 /* write at most 64 bytes at a time */ 1675 for (offset = 0; offset < len; offset += 64) { 1676 USETW(req.wIndex, reg + offset); 1677 USETW(req.wLength, MIN(len - offset, 64)); 1678 1679 error = rum_do_request(sc, &req, (char *)buf + offset); 1680 if (error != 0) { 1681 device_printf(sc->sc_dev, 1682 "could not multi write MAC register: %s\n", 1683 usbd_errstr(error)); 1684 return (error); 1685 } 1686 } 1687 1688 return (USB_ERR_NORMAL_COMPLETION); 1689} 1690 1691static usb_error_t 1692rum_setbits(struct rum_softc *sc, uint16_t reg, uint32_t mask) 1693{ 1694 return (rum_write(sc, reg, rum_read(sc, reg) | mask)); 1695} 1696 1697static usb_error_t 1698rum_clrbits(struct rum_softc *sc, uint16_t reg, uint32_t mask) 1699{ 1700 return (rum_write(sc, reg, rum_read(sc, reg) & ~mask)); 1701} 1702 1703static usb_error_t 1704rum_modbits(struct rum_softc *sc, uint16_t reg, uint32_t set, uint32_t unset) 1705{ 1706 return (rum_write(sc, reg, (rum_read(sc, reg) & ~unset) | set)); 1707} 1708 1709static int 1710rum_bbp_busy(struct rum_softc *sc) 1711{ 1712 int ntries; 1713 1714 for (ntries = 0; ntries < 100; ntries++) { 1715 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY)) 1716 break; 1717 if (rum_pause(sc, hz / 100)) 1718 break; 1719 } 1720 if (ntries == 100) 1721 return (ETIMEDOUT); 1722 1723 return (0); 1724} 1725 1726static void 1727rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val) 1728{ 1729 uint32_t tmp; 1730 1731 DPRINTFN(2, "reg=0x%08x\n", reg); 1732 1733 if (rum_bbp_busy(sc) != 0) { 1734 device_printf(sc->sc_dev, "could not write to BBP\n"); 1735 return; 1736 } 1737 1738 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val; 1739 rum_write(sc, RT2573_PHY_CSR3, tmp); 1740} 1741 1742static uint8_t 1743rum_bbp_read(struct rum_softc *sc, uint8_t reg) 1744{ 1745 uint32_t val; 1746 int ntries; 1747 1748 DPRINTFN(2, "reg=0x%08x\n", reg); 1749 1750 if (rum_bbp_busy(sc) != 0) { 1751 device_printf(sc->sc_dev, "could not read BBP\n"); 1752 return 0; 1753 } 1754 1755 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8; 1756 rum_write(sc, RT2573_PHY_CSR3, val); 1757 1758 for (ntries = 0; ntries < 100; ntries++) { 1759 val = rum_read(sc, RT2573_PHY_CSR3); 1760 if (!(val & RT2573_BBP_BUSY)) 1761 return val & 0xff; 1762 if (rum_pause(sc, hz / 100)) 1763 break; 1764 } 1765 1766 device_printf(sc->sc_dev, "could not read BBP\n"); 1767 return 0; 1768} 1769 1770static void 1771rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val) 1772{ 1773 uint32_t tmp; 1774 int ntries; 1775 1776 for (ntries = 0; ntries < 100; ntries++) { 1777 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY)) 1778 break; 1779 if (rum_pause(sc, hz / 100)) 1780 break; 1781 } 1782 if (ntries == 100) { 1783 device_printf(sc->sc_dev, "could not write to RF\n"); 1784 return; 1785 } 1786 1787 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 | 1788 (reg & 3); 1789 rum_write(sc, RT2573_PHY_CSR4, tmp); 1790 1791 /* remember last written value in sc */ 1792 sc->rf_regs[reg] = val; 1793 1794 DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff); 1795} 1796 1797static void 1798rum_select_antenna(struct rum_softc *sc) 1799{ 1800 uint8_t bbp4, bbp77; 1801 uint32_t tmp; 1802 1803 bbp4 = rum_bbp_read(sc, 4); 1804 bbp77 = rum_bbp_read(sc, 77); 1805 1806 /* TBD */ 1807 1808 /* make sure Rx is disabled before switching antenna */ 1809 tmp = rum_read(sc, RT2573_TXRX_CSR0); 1810 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX); 1811 1812 rum_bbp_write(sc, 4, bbp4); 1813 rum_bbp_write(sc, 77, bbp77); 1814 1815 rum_write(sc, RT2573_TXRX_CSR0, tmp); 1816} 1817 1818/* 1819 * Enable multi-rate retries for frames sent at OFDM rates. 1820 * In 802.11b/g mode, allow fallback to CCK rates. 1821 */ 1822static void 1823rum_enable_mrr(struct rum_softc *sc) 1824{ 1825 struct ieee80211com *ic = &sc->sc_ic; 1826 1827 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) { 1828 rum_setbits(sc, RT2573_TXRX_CSR4, 1829 RT2573_MRR_ENABLED | RT2573_MRR_CCK_FALLBACK); 1830 } else { 1831 rum_modbits(sc, RT2573_TXRX_CSR4, 1832 RT2573_MRR_ENABLED, RT2573_MRR_CCK_FALLBACK); 1833 } 1834} 1835 1836static void 1837rum_set_txpreamble(struct rum_softc *sc) 1838{ 1839 struct ieee80211com *ic = &sc->sc_ic; 1840 1841 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 1842 rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE); 1843 else 1844 rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE); 1845} 1846 1847static void 1848rum_set_basicrates(struct rum_softc *sc) 1849{ 1850 struct ieee80211com *ic = &sc->sc_ic; 1851 1852 /* update basic rate set */ 1853 if (ic->ic_curmode == IEEE80211_MODE_11B) { 1854 /* 11b basic rates: 1, 2Mbps */ 1855 rum_write(sc, RT2573_TXRX_CSR5, 0x3); 1856 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) { 1857 /* 11a basic rates: 6, 12, 24Mbps */ 1858 rum_write(sc, RT2573_TXRX_CSR5, 0x150); 1859 } else { 1860 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */ 1861 rum_write(sc, RT2573_TXRX_CSR5, 0xf); 1862 } 1863} 1864 1865/* 1866 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference 1867 * driver. 1868 */ 1869static void 1870rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c) 1871{ 1872 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104; 1873 1874 /* update all BBP registers that depend on the band */ 1875 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c; 1876 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48; 1877 if (IEEE80211_IS_CHAN_5GHZ(c)) { 1878 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c; 1879 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10; 1880 } 1881 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) || 1882 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) { 1883 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10; 1884 } 1885 1886 sc->bbp17 = bbp17; 1887 rum_bbp_write(sc, 17, bbp17); 1888 rum_bbp_write(sc, 96, bbp96); 1889 rum_bbp_write(sc, 104, bbp104); 1890 1891 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) || 1892 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) { 1893 rum_bbp_write(sc, 75, 0x80); 1894 rum_bbp_write(sc, 86, 0x80); 1895 rum_bbp_write(sc, 88, 0x80); 1896 } 1897 1898 rum_bbp_write(sc, 35, bbp35); 1899 rum_bbp_write(sc, 97, bbp97); 1900 rum_bbp_write(sc, 98, bbp98); 1901 1902 if (IEEE80211_IS_CHAN_2GHZ(c)) { 1903 rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_2GHZ, 1904 RT2573_PA_PE_5GHZ); 1905 } else { 1906 rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_5GHZ, 1907 RT2573_PA_PE_2GHZ); 1908 } 1909} 1910 1911static void 1912rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c) 1913{ 1914 struct ieee80211com *ic = &sc->sc_ic; 1915 const struct rfprog *rfprog; 1916 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT; 1917 int8_t power; 1918 int i, chan; 1919 1920 chan = ieee80211_chan2ieee(ic, c); 1921 if (chan == 0 || chan == IEEE80211_CHAN_ANY) 1922 return; 1923 1924 /* select the appropriate RF settings based on what EEPROM says */ 1925 rfprog = (sc->rf_rev == RT2573_RF_5225 || 1926 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226; 1927 1928 /* find the settings for this channel (we know it exists) */ 1929 for (i = 0; rfprog[i].chan != chan; i++); 1930 1931 power = sc->txpow[i]; 1932 if (power < 0) { 1933 bbp94 += power; 1934 power = 0; 1935 } else if (power > 31) { 1936 bbp94 += power - 31; 1937 power = 31; 1938 } 1939 1940 /* 1941 * If we are switching from the 2GHz band to the 5GHz band or 1942 * vice-versa, BBP registers need to be reprogrammed. 1943 */ 1944 if (c->ic_flags != ic->ic_curchan->ic_flags) { 1945 rum_select_band(sc, c); 1946 rum_select_antenna(sc); 1947 } 1948 ic->ic_curchan = c; 1949 1950 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1951 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1952 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7); 1953 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 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 | 1); 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); 1963 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1964 1965 rum_pause(sc, hz / 100); 1966 1967 /* enable smart mode for MIMO-capable RFs */ 1968 bbp3 = rum_bbp_read(sc, 3); 1969 1970 bbp3 &= ~RT2573_SMART_MODE; 1971 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527) 1972 bbp3 |= RT2573_SMART_MODE; 1973 1974 rum_bbp_write(sc, 3, bbp3); 1975 1976 if (bbp94 != RT2573_BBPR94_DEFAULT) 1977 rum_bbp_write(sc, 94, bbp94); 1978 1979 /* give the chip some extra time to do the switchover */ 1980 rum_pause(sc, hz / 100); 1981} 1982 1983static void 1984rum_set_maxretry(struct rum_softc *sc, struct ieee80211vap *vap) 1985{ 1986 const struct ieee80211_txparam *tp; 1987 struct ieee80211_node *ni = vap->iv_bss; 1988 struct rum_vap *rvp = RUM_VAP(vap); 1989 1990 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)]; 1991 rvp->maxretry = tp->maxretry < 0xf ? tp->maxretry : 0xf; 1992 1993 rum_modbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_RETRY(rvp->maxretry) | 1994 RT2573_LONG_RETRY(rvp->maxretry), 1995 RT2573_SHORT_RETRY_MASK | RT2573_LONG_RETRY_MASK); 1996} 1997 1998/* 1999 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS 2000 * and HostAP operating modes. 2001 */ 2002static int 2003rum_enable_tsf_sync(struct rum_softc *sc) 2004{ 2005 struct ieee80211com *ic = &sc->sc_ic; 2006 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2007 uint32_t tmp; 2008 2009 if (vap->iv_opmode != IEEE80211_M_STA) { 2010 /* 2011 * Change default 16ms TBTT adjustment to 8ms. 2012 * Must be done before enabling beacon generation. 2013 */ 2014 if (rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8) != 0) 2015 return EIO; 2016 } 2017 2018 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000; 2019 2020 /* set beacon interval (in 1/16ms unit) */ 2021 tmp |= vap->iv_bss->ni_intval * 16; 2022 tmp |= RT2573_TSF_TIMER_EN | RT2573_TBTT_TIMER_EN; 2023 2024 switch (vap->iv_opmode) { 2025 case IEEE80211_M_STA: 2026 /* 2027 * Local TSF is always updated with remote TSF on beacon 2028 * reception. 2029 */ 2030 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_STA); 2031 break; 2032 case IEEE80211_M_IBSS: 2033 /* 2034 * Local TSF is updated with remote TSF on beacon reception 2035 * only if the remote TSF is greater than local TSF. 2036 */ 2037 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_IBSS); 2038 tmp |= RT2573_BCN_TX_EN; 2039 break; 2040 case IEEE80211_M_HOSTAP: 2041 /* SYNC with nobody */ 2042 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_HOSTAP); 2043 tmp |= RT2573_BCN_TX_EN; 2044 break; 2045 default: 2046 device_printf(sc->sc_dev, 2047 "Enabling TSF failed. undefined opmode %d\n", 2048 vap->iv_opmode); 2049 return EINVAL; 2050 } 2051 2052 if (rum_write(sc, RT2573_TXRX_CSR9, tmp) != 0) 2053 return EIO; 2054 2055 return 0; 2056} 2057 2058static void 2059rum_enable_tsf(struct rum_softc *sc) 2060{ 2061 rum_modbits(sc, RT2573_TXRX_CSR9, RT2573_TSF_TIMER_EN | 2062 RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_DIS), 0x00ffffff); 2063} 2064 2065static void 2066rum_abort_tsf_sync(struct rum_softc *sc) 2067{ 2068 rum_clrbits(sc, RT2573_TXRX_CSR9, 0x00ffffff); 2069} 2070 2071static void 2072rum_get_tsf(struct rum_softc *sc, uint64_t *buf) 2073{ 2074 rum_read_multi(sc, RT2573_TXRX_CSR12, buf, sizeof (*buf)); 2075} 2076 2077static void 2078rum_update_slot_cb(struct rum_softc *sc, union sec_param *data, uint8_t rvp_id) 2079{ 2080 struct ieee80211com *ic = &sc->sc_ic; 2081 uint8_t slottime; 2082 2083 slottime = IEEE80211_GET_SLOTTIME(ic); 2084 2085 rum_modbits(sc, RT2573_MAC_CSR9, slottime, 0xff); 2086 2087 DPRINTF("setting slot time to %uus\n", slottime); 2088} 2089 2090static void 2091rum_update_slot(struct ieee80211com *ic) 2092{ 2093 rum_cmd_sleepable(ic->ic_softc, NULL, 0, 0, rum_update_slot_cb); 2094} 2095 2096static int 2097rum_wme_update(struct ieee80211com *ic) 2098{ 2099 const struct wmeParams *chanp = 2100 ic->ic_wme.wme_chanParams.cap_wmeParams; 2101 struct rum_softc *sc = ic->ic_softc; 2102 int error = 0; 2103 2104 RUM_LOCK(sc); 2105 error = rum_write(sc, RT2573_AIFSN_CSR, 2106 chanp[WME_AC_VO].wmep_aifsn << 12 | 2107 chanp[WME_AC_VI].wmep_aifsn << 8 | 2108 chanp[WME_AC_BK].wmep_aifsn << 4 | 2109 chanp[WME_AC_BE].wmep_aifsn); 2110 if (error) 2111 goto print_err; 2112 error = rum_write(sc, RT2573_CWMIN_CSR, 2113 chanp[WME_AC_VO].wmep_logcwmin << 12 | 2114 chanp[WME_AC_VI].wmep_logcwmin << 8 | 2115 chanp[WME_AC_BK].wmep_logcwmin << 4 | 2116 chanp[WME_AC_BE].wmep_logcwmin); 2117 if (error) 2118 goto print_err; 2119 error = rum_write(sc, RT2573_CWMAX_CSR, 2120 chanp[WME_AC_VO].wmep_logcwmax << 12 | 2121 chanp[WME_AC_VI].wmep_logcwmax << 8 | 2122 chanp[WME_AC_BK].wmep_logcwmax << 4 | 2123 chanp[WME_AC_BE].wmep_logcwmax); 2124 if (error) 2125 goto print_err; 2126 error = rum_write(sc, RT2573_TXOP01_CSR, 2127 chanp[WME_AC_BK].wmep_txopLimit << 16 | 2128 chanp[WME_AC_BE].wmep_txopLimit); 2129 if (error) 2130 goto print_err; 2131 error = rum_write(sc, RT2573_TXOP23_CSR, 2132 chanp[WME_AC_VO].wmep_txopLimit << 16 | 2133 chanp[WME_AC_VI].wmep_txopLimit); 2134 if (error) 2135 goto print_err; 2136 2137 memcpy(sc->wme_params, chanp, sizeof(*chanp) * WME_NUM_AC); 2138 2139print_err: 2140 RUM_UNLOCK(sc); 2141 if (error != 0) { 2142 device_printf(sc->sc_dev, "%s: WME update failed, error %d\n", 2143 __func__, error); 2144 } 2145 2146 return (error); 2147} 2148 2149static void 2150rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid) 2151{ 2152 2153 rum_write(sc, RT2573_MAC_CSR4, 2154 bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24); 2155 rum_write(sc, RT2573_MAC_CSR5, 2156 bssid[4] | bssid[5] << 8 | RT2573_NUM_BSSID_MSK(1)); 2157} 2158 2159static void 2160rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr) 2161{ 2162 2163 rum_write(sc, RT2573_MAC_CSR2, 2164 addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24); 2165 rum_write(sc, RT2573_MAC_CSR3, 2166 addr[4] | addr[5] << 8 | 0xff << 16); 2167} 2168 2169static void 2170rum_setpromisc(struct rum_softc *sc) 2171{ 2172 struct ieee80211com *ic = &sc->sc_ic; 2173 2174 if (ic->ic_promisc == 0) 2175 rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME); 2176 else 2177 rum_clrbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME); 2178 2179 DPRINTF("%s promiscuous mode\n", ic->ic_promisc > 0 ? 2180 "entering" : "leaving"); 2181} 2182 2183static void 2184rum_update_promisc(struct ieee80211com *ic) 2185{ 2186 struct rum_softc *sc = ic->ic_softc; 2187 2188 RUM_LOCK(sc); 2189 if (sc->sc_running) 2190 rum_setpromisc(sc); 2191 RUM_UNLOCK(sc); 2192} 2193 2194static void 2195rum_update_mcast(struct ieee80211com *ic) 2196{ 2197 /* Ignore. */ 2198} 2199 2200static const char * 2201rum_get_rf(int rev) 2202{ 2203 switch (rev) { 2204 case RT2573_RF_2527: return "RT2527 (MIMO XR)"; 2205 case RT2573_RF_2528: return "RT2528"; 2206 case RT2573_RF_5225: return "RT5225 (MIMO XR)"; 2207 case RT2573_RF_5226: return "RT5226"; 2208 default: return "unknown"; 2209 } 2210} 2211 2212static void 2213rum_read_eeprom(struct rum_softc *sc) 2214{ 2215 uint16_t val; 2216#ifdef RUM_DEBUG 2217 int i; 2218#endif 2219 2220 /* read MAC address */ 2221 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_ic.ic_macaddr, 6); 2222 2223 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2); 2224 val = le16toh(val); 2225 sc->rf_rev = (val >> 11) & 0x1f; 2226 sc->hw_radio = (val >> 10) & 0x1; 2227 sc->rx_ant = (val >> 4) & 0x3; 2228 sc->tx_ant = (val >> 2) & 0x3; 2229 sc->nb_ant = val & 0x3; 2230 2231 DPRINTF("RF revision=%d\n", sc->rf_rev); 2232 2233 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2); 2234 val = le16toh(val); 2235 sc->ext_5ghz_lna = (val >> 6) & 0x1; 2236 sc->ext_2ghz_lna = (val >> 4) & 0x1; 2237 2238 DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n", 2239 sc->ext_2ghz_lna, sc->ext_5ghz_lna); 2240 2241 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2); 2242 val = le16toh(val); 2243 if ((val & 0xff) != 0xff) 2244 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */ 2245 2246 /* Only [-10, 10] is valid */ 2247 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10) 2248 sc->rssi_2ghz_corr = 0; 2249 2250 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2); 2251 val = le16toh(val); 2252 if ((val & 0xff) != 0xff) 2253 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */ 2254 2255 /* Only [-10, 10] is valid */ 2256 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10) 2257 sc->rssi_5ghz_corr = 0; 2258 2259 if (sc->ext_2ghz_lna) 2260 sc->rssi_2ghz_corr -= 14; 2261 if (sc->ext_5ghz_lna) 2262 sc->rssi_5ghz_corr -= 14; 2263 2264 DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n", 2265 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr); 2266 2267 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2); 2268 val = le16toh(val); 2269 if ((val & 0xff) != 0xff) 2270 sc->rffreq = val & 0xff; 2271 2272 DPRINTF("RF freq=%d\n", sc->rffreq); 2273 2274 /* read Tx power for all a/b/g channels */ 2275 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14); 2276 /* XXX default Tx power for 802.11a channels */ 2277 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14); 2278#ifdef RUM_DEBUG 2279 for (i = 0; i < 14; i++) 2280 DPRINTF("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]); 2281#endif 2282 2283 /* read default values for BBP registers */ 2284 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16); 2285#ifdef RUM_DEBUG 2286 for (i = 0; i < 14; i++) { 2287 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff) 2288 continue; 2289 DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg, 2290 sc->bbp_prom[i].val); 2291 } 2292#endif 2293} 2294 2295static int 2296rum_bbp_wakeup(struct rum_softc *sc) 2297{ 2298 unsigned int ntries; 2299 2300 for (ntries = 0; ntries < 100; ntries++) { 2301 if (rum_read(sc, RT2573_MAC_CSR12) & 8) 2302 break; 2303 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */ 2304 if (rum_pause(sc, hz / 100)) 2305 break; 2306 } 2307 if (ntries == 100) { 2308 device_printf(sc->sc_dev, 2309 "timeout waiting for BBP/RF to wakeup\n"); 2310 return (ETIMEDOUT); 2311 } 2312 2313 return (0); 2314} 2315 2316static int 2317rum_bbp_init(struct rum_softc *sc) 2318{ 2319 int i, ntries; 2320 2321 /* wait for BBP to be ready */ 2322 for (ntries = 0; ntries < 100; ntries++) { 2323 const uint8_t val = rum_bbp_read(sc, 0); 2324 if (val != 0 && val != 0xff) 2325 break; 2326 if (rum_pause(sc, hz / 100)) 2327 break; 2328 } 2329 if (ntries == 100) { 2330 device_printf(sc->sc_dev, "timeout waiting for BBP\n"); 2331 return EIO; 2332 } 2333 2334 /* initialize BBP registers to default values */ 2335 for (i = 0; i < nitems(rum_def_bbp); i++) 2336 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val); 2337 2338 /* write vendor-specific BBP values (from EEPROM) */ 2339 for (i = 0; i < 16; i++) { 2340 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff) 2341 continue; 2342 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 2343 } 2344 2345 return 0; 2346} 2347 2348static void 2349rum_clr_shkey_regs(struct rum_softc *sc) 2350{ 2351 rum_write(sc, RT2573_SEC_CSR0, 0); 2352 rum_write(sc, RT2573_SEC_CSR1, 0); 2353 rum_write(sc, RT2573_SEC_CSR5, 0); 2354} 2355 2356static int 2357rum_init(struct rum_softc *sc) 2358{ 2359 struct ieee80211com *ic = &sc->sc_ic; 2360 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2361 uint32_t tmp; 2362 int i, ret; 2363 2364 RUM_LOCK(sc); 2365 if (sc->sc_running) { 2366 ret = 0; 2367 goto end; 2368 } 2369 2370 /* initialize MAC registers to default values */ 2371 for (i = 0; i < nitems(rum_def_mac); i++) 2372 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val); 2373 2374 /* reset some WME parameters to default values */ 2375 sc->wme_params[0].wmep_aifsn = 2; 2376 sc->wme_params[0].wmep_logcwmin = 4; 2377 sc->wme_params[0].wmep_logcwmax = 10; 2378 2379 /* set host ready */ 2380 rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP); 2381 rum_write(sc, RT2573_MAC_CSR1, 0); 2382 2383 /* wait for BBP/RF to wakeup */ 2384 if ((ret = rum_bbp_wakeup(sc)) != 0) 2385 goto end; 2386 2387 if ((ret = rum_bbp_init(sc)) != 0) 2388 goto end; 2389 2390 /* select default channel */ 2391 rum_select_band(sc, ic->ic_curchan); 2392 rum_select_antenna(sc); 2393 rum_set_chan(sc, ic->ic_curchan); 2394 2395 /* clear STA registers */ 2396 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta); 2397 2398 /* clear security registers (if required) */ 2399 if (sc->sc_clr_shkeys == 0) { 2400 rum_clr_shkey_regs(sc); 2401 sc->sc_clr_shkeys = 1; 2402 } 2403 2404 rum_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr); 2405 2406 /* initialize ASIC */ 2407 rum_write(sc, RT2573_MAC_CSR1, RT2573_HOST_READY); 2408 2409 /* 2410 * Allocate Tx and Rx xfer queues. 2411 */ 2412 rum_setup_tx_list(sc); 2413 2414 /* update Rx filter */ 2415 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff; 2416 2417 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR; 2418 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2419 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR | 2420 RT2573_DROP_ACKCTS; 2421 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 2422 tmp |= RT2573_DROP_TODS; 2423 if (ic->ic_promisc == 0) 2424 tmp |= RT2573_DROP_NOT_TO_ME; 2425 } 2426 rum_write(sc, RT2573_TXRX_CSR0, tmp); 2427 2428 sc->sc_running = 1; 2429 usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]); 2430 usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]); 2431 2432end: RUM_UNLOCK(sc); 2433 2434 if (ret != 0) 2435 rum_stop(sc); 2436 2437 return ret; 2438} 2439 2440static void 2441rum_stop(struct rum_softc *sc) 2442{ 2443 2444 RUM_LOCK(sc); 2445 if (!sc->sc_running) { 2446 RUM_UNLOCK(sc); 2447 return; 2448 } 2449 sc->sc_running = 0; 2450 RUM_UNLOCK(sc); 2451 2452 /* 2453 * Drain the USB transfers, if not already drained: 2454 */ 2455 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]); 2456 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]); 2457 2458 RUM_LOCK(sc); 2459 rum_unsetup_tx_list(sc); 2460 2461 /* disable Rx */ 2462 rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DISABLE_RX); 2463 2464 /* reset ASIC */ 2465 rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP); 2466 rum_write(sc, RT2573_MAC_CSR1, 0); 2467 RUM_UNLOCK(sc); 2468} 2469 2470static void 2471rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size) 2472{ 2473 uint16_t reg = RT2573_MCU_CODE_BASE; 2474 usb_error_t err; 2475 2476 /* copy firmware image into NIC */ 2477 for (; size >= 4; reg += 4, ucode += 4, size -= 4) { 2478 err = rum_write(sc, reg, UGETDW(ucode)); 2479 if (err) { 2480 /* firmware already loaded ? */ 2481 device_printf(sc->sc_dev, "Firmware load " 2482 "failure! (ignored)\n"); 2483 break; 2484 } 2485 } 2486 2487 err = rum_do_mcu_request(sc, RT2573_MCU_RUN); 2488 if (err != USB_ERR_NORMAL_COMPLETION) { 2489 device_printf(sc->sc_dev, "could not run firmware: %s\n", 2490 usbd_errstr(err)); 2491 } 2492 2493 /* give the chip some time to boot */ 2494 rum_pause(sc, hz / 8); 2495} 2496 2497static int 2498rum_set_beacon(struct rum_softc *sc, struct ieee80211vap *vap) 2499{ 2500 struct ieee80211com *ic = vap->iv_ic; 2501 struct rum_vap *rvp = RUM_VAP(vap); 2502 struct mbuf *m = rvp->bcn_mbuf; 2503 const struct ieee80211_txparam *tp; 2504 struct rum_tx_desc desc; 2505 2506 RUM_LOCK_ASSERT(sc); 2507 2508 if (m == NULL) 2509 return EINVAL; 2510 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) 2511 return EINVAL; 2512 2513 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)]; 2514 rum_setup_tx_desc(sc, &desc, NULL, RT2573_TX_TIMESTAMP, 2515 RT2573_TX_HWSEQ, 0, 0, m->m_pkthdr.len, tp->mgmtrate); 2516 2517 /* copy the Tx descriptor into NIC memory */ 2518 if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0), (uint8_t *)&desc, 2519 RT2573_TX_DESC_SIZE) != 0) 2520 return EIO; 2521 2522 /* copy beacon header and payload into NIC memory */ 2523 if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0) + RT2573_TX_DESC_SIZE, 2524 mtod(m, uint8_t *), m->m_pkthdr.len) != 0) 2525 return EIO; 2526 2527 return 0; 2528} 2529 2530static int 2531rum_alloc_beacon(struct rum_softc *sc, struct ieee80211vap *vap) 2532{ 2533 struct rum_vap *rvp = RUM_VAP(vap); 2534 struct ieee80211_node *ni = vap->iv_bss; 2535 struct mbuf *m; 2536 2537 if (ni->ni_chan == IEEE80211_CHAN_ANYC) 2538 return EINVAL; 2539 2540 m = ieee80211_beacon_alloc(ni); 2541 if (m == NULL) 2542 return ENOMEM; 2543 2544 if (rvp->bcn_mbuf != NULL) 2545 m_freem(rvp->bcn_mbuf); 2546 2547 rvp->bcn_mbuf = m; 2548 2549 return (rum_set_beacon(sc, vap)); 2550} 2551 2552static void 2553rum_update_beacon_cb(struct rum_softc *sc, union sec_param *data, 2554 uint8_t rvp_id) 2555{ 2556 struct ieee80211vap *vap = data->vap; 2557 2558 rum_set_beacon(sc, vap); 2559} 2560 2561static void 2562rum_update_beacon(struct ieee80211vap *vap, int item) 2563{ 2564 struct ieee80211com *ic = vap->iv_ic; 2565 struct rum_softc *sc = ic->ic_softc; 2566 struct rum_vap *rvp = RUM_VAP(vap); 2567 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off; 2568 struct ieee80211_node *ni = vap->iv_bss; 2569 struct mbuf *m = rvp->bcn_mbuf; 2570 int mcast = 0; 2571 2572 RUM_LOCK(sc); 2573 if (m == NULL) { 2574 m = ieee80211_beacon_alloc(ni); 2575 if (m == NULL) { 2576 device_printf(sc->sc_dev, 2577 "%s: could not allocate beacon frame\n", __func__); 2578 RUM_UNLOCK(sc); 2579 return; 2580 } 2581 rvp->bcn_mbuf = m; 2582 } 2583 2584 switch (item) { 2585 case IEEE80211_BEACON_ERP: 2586 rum_update_slot(ic); 2587 break; 2588 case IEEE80211_BEACON_TIM: 2589 mcast = 1; /*TODO*/ 2590 break; 2591 default: 2592 break; 2593 } 2594 RUM_UNLOCK(sc); 2595 2596 setbit(bo->bo_flags, item); 2597 ieee80211_beacon_update(ni, m, mcast); 2598 2599 rum_cmd_sleepable(sc, &vap, sizeof(vap), 0, rum_update_beacon_cb); 2600} 2601 2602static int 2603rum_common_key_set(struct rum_softc *sc, struct ieee80211_key *k, 2604 uint16_t base) 2605{ 2606 2607 if (rum_write_multi(sc, base, k->wk_key, k->wk_keylen)) 2608 return EIO; 2609 2610 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) { 2611 if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE, 2612 k->wk_txmic, 8)) 2613 return EIO; 2614 if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE + 8, 2615 k->wk_rxmic, 8)) 2616 return EIO; 2617 } 2618 2619 return 0; 2620} 2621 2622static void 2623rum_group_key_set_cb(struct rum_softc *sc, union sec_param *data, 2624 uint8_t rvp_id) 2625{ 2626 struct ieee80211_key *k = &data->key; 2627 uint8_t mode; 2628 2629 if (sc->sc_clr_shkeys == 0) { 2630 rum_clr_shkey_regs(sc); 2631 sc->sc_clr_shkeys = 1; 2632 } 2633 2634 mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen); 2635 if (mode == 0) 2636 goto print_err; 2637 2638 DPRINTFN(1, "setting group key %d for vap %d, mode %d " 2639 "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode, 2640 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off", 2641 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off"); 2642 2643 /* Install the key. */ 2644 if (rum_common_key_set(sc, k, RT2573_SKEY(rvp_id, k->wk_keyix)) != 0) 2645 goto print_err; 2646 2647 /* Set cipher mode. */ 2648 if (rum_modbits(sc, rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5, 2649 mode << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX, 2650 RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX) 2651 != 0) 2652 goto print_err; 2653 2654 /* Mark this key as valid. */ 2655 if (rum_setbits(sc, RT2573_SEC_CSR0, 2656 1 << (rvp_id * RT2573_SKEY_MAX + k->wk_keyix)) != 0) 2657 goto print_err; 2658 2659 return; 2660 2661print_err: 2662 device_printf(sc->sc_dev, "%s: cannot set group key %d for vap %d\n", 2663 __func__, k->wk_keyix, rvp_id); 2664} 2665 2666static void 2667rum_group_key_del_cb(struct rum_softc *sc, union sec_param *data, 2668 uint8_t rvp_id) 2669{ 2670 struct ieee80211_key *k = &data->key; 2671 2672 DPRINTF("%s: removing group key %d for vap %d\n", __func__, 2673 k->wk_keyix, rvp_id); 2674 rum_clrbits(sc, 2675 rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5, 2676 RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX); 2677 rum_clrbits(sc, RT2573_SEC_CSR0, 2678 rvp_id * RT2573_SKEY_MAX + k->wk_keyix); 2679} 2680 2681static void 2682rum_pair_key_set_cb(struct rum_softc *sc, union sec_param *data, 2683 uint8_t rvp_id) 2684{ 2685 struct ieee80211_key *k = &data->key; 2686 uint8_t buf[IEEE80211_ADDR_LEN + 1]; 2687 uint8_t mode; 2688 2689 mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen); 2690 if (mode == 0) 2691 goto print_err; 2692 2693 DPRINTFN(1, "setting pairwise key %d for vap %d, mode %d " 2694 "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode, 2695 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off", 2696 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off"); 2697 2698 /* Install the key. */ 2699 if (rum_common_key_set(sc, k, RT2573_PKEY(k->wk_keyix)) != 0) 2700 goto print_err; 2701 2702 IEEE80211_ADDR_COPY(buf, k->wk_macaddr); 2703 buf[IEEE80211_ADDR_LEN] = mode; 2704 2705 /* Set transmitter address and cipher mode. */ 2706 if (rum_write_multi(sc, RT2573_ADDR_ENTRY(k->wk_keyix), 2707 buf, sizeof buf) != 0) 2708 goto print_err; 2709 2710 /* Enable key table lookup for this vap. */ 2711 if (sc->vap_key_count[rvp_id]++ == 0) 2712 if (rum_setbits(sc, RT2573_SEC_CSR4, 1 << rvp_id) != 0) 2713 goto print_err; 2714 2715 /* Mark this key as valid. */ 2716 if (rum_setbits(sc, 2717 k->wk_keyix < 32 ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3, 2718 1 << (k->wk_keyix % 32)) != 0) 2719 goto print_err; 2720 2721 return; 2722 2723print_err: 2724 device_printf(sc->sc_dev, 2725 "%s: cannot set pairwise key %d, vap %d\n", __func__, k->wk_keyix, 2726 rvp_id); 2727} 2728 2729static void 2730rum_pair_key_del_cb(struct rum_softc *sc, union sec_param *data, 2731 uint8_t rvp_id) 2732{ 2733 struct ieee80211_key *k = &data->key; 2734 2735 DPRINTF("%s: removing key %d\n", __func__, k->wk_keyix); 2736 rum_clrbits(sc, (k->wk_keyix < 32) ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3, 2737 1 << (k->wk_keyix % 32)); 2738 sc->keys_bmap &= ~(1ULL << k->wk_keyix); 2739 if (--sc->vap_key_count[rvp_id] == 0) 2740 rum_clrbits(sc, RT2573_SEC_CSR4, 1 << rvp_id); 2741} 2742 2743static int 2744rum_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k, 2745 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 2746{ 2747 struct rum_softc *sc = vap->iv_ic->ic_softc; 2748 uint8_t i; 2749 2750 if (!(&vap->iv_nw_keys[0] <= k && 2751 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) { 2752 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) { 2753 RUM_LOCK(sc); 2754 for (i = 0; i < RT2573_ADDR_MAX; i++) { 2755 if ((sc->keys_bmap & (1ULL << i)) == 0) { 2756 sc->keys_bmap |= (1ULL << i); 2757 *keyix = i; 2758 break; 2759 } 2760 } 2761 RUM_UNLOCK(sc); 2762 if (i == RT2573_ADDR_MAX) { 2763 device_printf(sc->sc_dev, 2764 "%s: no free space in the key table\n", 2765 __func__); 2766 return 0; 2767 } 2768 } else 2769 *keyix = 0; 2770 } else { 2771 *keyix = k - vap->iv_nw_keys; 2772 } 2773 *rxkeyix = *keyix; 2774 return 1; 2775} 2776 2777static int 2778rum_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k) 2779{ 2780 struct rum_softc *sc = vap->iv_ic->ic_softc; 2781 int group; 2782 2783 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) { 2784 /* Not for us. */ 2785 return 1; 2786 } 2787 2788 group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]; 2789 2790 return !rum_cmd_sleepable(sc, k, sizeof(*k), 0, 2791 group ? rum_group_key_set_cb : rum_pair_key_set_cb); 2792} 2793 2794static int 2795rum_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k) 2796{ 2797 struct rum_softc *sc = vap->iv_ic->ic_softc; 2798 int group; 2799 2800 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) { 2801 /* Not for us. */ 2802 return 1; 2803 } 2804 2805 group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]; 2806 2807 return !rum_cmd_sleepable(sc, k, sizeof(*k), 0, 2808 group ? rum_group_key_del_cb : rum_pair_key_del_cb); 2809} 2810 2811static int 2812rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2813 const struct ieee80211_bpf_params *params) 2814{ 2815 struct rum_softc *sc = ni->ni_ic->ic_softc; 2816 int ret; 2817 2818 RUM_LOCK(sc); 2819 /* prevent management frames from being sent if we're not ready */ 2820 if (!sc->sc_running) { 2821 ret = ENETDOWN; 2822 goto bad; 2823 } 2824 if (sc->tx_nfree < RUM_TX_MINFREE) { 2825 ret = EIO; 2826 goto bad; 2827 } 2828 2829 if (params == NULL) { 2830 /* 2831 * Legacy path; interpret frame contents to decide 2832 * precisely how to send the frame. 2833 */ 2834 if ((ret = rum_tx_mgt(sc, m, ni)) != 0) 2835 goto bad; 2836 } else { 2837 /* 2838 * Caller supplied explicit parameters to use in 2839 * sending the frame. 2840 */ 2841 if ((ret = rum_tx_raw(sc, m, ni, params)) != 0) 2842 goto bad; 2843 } 2844 RUM_UNLOCK(sc); 2845 2846 return 0; 2847bad: 2848 RUM_UNLOCK(sc); 2849 m_freem(m); 2850 return ret; 2851} 2852 2853static void 2854rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni) 2855{ 2856 struct ieee80211vap *vap = ni->ni_vap; 2857 struct rum_vap *rvp = RUM_VAP(vap); 2858 2859 /* clear statistic registers (STA_CSR0 to STA_CSR5) */ 2860 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta); 2861 2862 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp); 2863} 2864 2865static void 2866rum_ratectl_timeout(void *arg) 2867{ 2868 struct rum_vap *rvp = arg; 2869 struct ieee80211vap *vap = &rvp->vap; 2870 struct ieee80211com *ic = vap->iv_ic; 2871 2872 ieee80211_runtask(ic, &rvp->ratectl_task); 2873} 2874 2875static void 2876rum_ratectl_task(void *arg, int pending) 2877{ 2878 struct rum_vap *rvp = arg; 2879 struct ieee80211vap *vap = &rvp->vap; 2880 struct rum_softc *sc = vap->iv_ic->ic_softc; 2881 struct ieee80211_node *ni; 2882 int ok[3], fail; 2883 int sum, success, retrycnt; 2884 2885 RUM_LOCK(sc); 2886 /* read and clear statistic registers (STA_CSR0 to STA_CSR5) */ 2887 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta)); 2888 2889 ok[0] = (le32toh(sc->sta[4]) & 0xffff); /* TX ok w/o retry */ 2890 ok[1] = (le32toh(sc->sta[4]) >> 16); /* TX ok w/ one retry */ 2891 ok[2] = (le32toh(sc->sta[5]) & 0xffff); /* TX ok w/ multiple retries */ 2892 fail = (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */ 2893 2894 success = ok[0] + ok[1] + ok[2]; 2895 sum = success + fail; 2896 /* XXX at least */ 2897 retrycnt = ok[1] + ok[2] * 2 + fail * (rvp->maxretry + 1); 2898 2899 if (sum != 0) { 2900 ni = ieee80211_ref_node(vap->iv_bss); 2901 ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt); 2902 (void) ieee80211_ratectl_rate(ni, NULL, 0); 2903 ieee80211_free_node(ni); 2904 } 2905 2906 /* count TX retry-fail as Tx errors */ 2907 if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, fail); 2908 2909 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp); 2910 RUM_UNLOCK(sc); 2911} 2912 2913static void 2914rum_scan_start(struct ieee80211com *ic) 2915{ 2916 struct rum_softc *sc = ic->ic_softc; 2917 2918 RUM_LOCK(sc); 2919 rum_abort_tsf_sync(sc); 2920 rum_set_bssid(sc, ieee80211broadcastaddr); 2921 RUM_UNLOCK(sc); 2922 2923} 2924 2925static void 2926rum_scan_end(struct ieee80211com *ic) 2927{ 2928 struct rum_softc *sc = ic->ic_softc; 2929 2930 if (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) { 2931 RUM_LOCK(sc); 2932 if (ic->ic_opmode != IEEE80211_M_AHDEMO) 2933 rum_enable_tsf_sync(sc); 2934 else 2935 rum_enable_tsf(sc); 2936 rum_set_bssid(sc, sc->sc_bssid); 2937 RUM_UNLOCK(sc); 2938 } 2939} 2940 2941static void 2942rum_set_channel(struct ieee80211com *ic) 2943{ 2944 struct rum_softc *sc = ic->ic_softc; 2945 2946 RUM_LOCK(sc); 2947 rum_set_chan(sc, ic->ic_curchan); 2948 RUM_UNLOCK(sc); 2949} 2950 2951static int 2952rum_get_rssi(struct rum_softc *sc, uint8_t raw) 2953{ 2954 struct ieee80211com *ic = &sc->sc_ic; 2955 int lna, agc, rssi; 2956 2957 lna = (raw >> 5) & 0x3; 2958 agc = raw & 0x1f; 2959 2960 if (lna == 0) { 2961 /* 2962 * No RSSI mapping 2963 * 2964 * NB: Since RSSI is relative to noise floor, -1 is 2965 * adequate for caller to know error happened. 2966 */ 2967 return -1; 2968 } 2969 2970 rssi = (2 * agc) - RT2573_NOISE_FLOOR; 2971 2972 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) { 2973 rssi += sc->rssi_2ghz_corr; 2974 2975 if (lna == 1) 2976 rssi -= 64; 2977 else if (lna == 2) 2978 rssi -= 74; 2979 else if (lna == 3) 2980 rssi -= 90; 2981 } else { 2982 rssi += sc->rssi_5ghz_corr; 2983 2984 if (!sc->ext_5ghz_lna && lna != 1) 2985 rssi += 4; 2986 2987 if (lna == 1) 2988 rssi -= 64; 2989 else if (lna == 2) 2990 rssi -= 86; 2991 else if (lna == 3) 2992 rssi -= 100; 2993 } 2994 return rssi; 2995} 2996 2997static int 2998rum_pause(struct rum_softc *sc, int timeout) 2999{ 3000 3001 usb_pause_mtx(&sc->sc_mtx, timeout); 3002 return (0); 3003} 3004 3005static device_method_t rum_methods[] = { 3006 /* Device interface */ 3007 DEVMETHOD(device_probe, rum_match), 3008 DEVMETHOD(device_attach, rum_attach), 3009 DEVMETHOD(device_detach, rum_detach), 3010 DEVMETHOD_END 3011}; 3012 3013static driver_t rum_driver = { 3014 .name = "rum", 3015 .methods = rum_methods, 3016 .size = sizeof(struct rum_softc), 3017}; 3018 3019static devclass_t rum_devclass; 3020 3021DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0); 3022MODULE_DEPEND(rum, wlan, 1, 1, 1); 3023MODULE_DEPEND(rum, usb, 1, 1, 1); 3024MODULE_VERSION(rum, 1); 3025USB_PNP_HOST_INFO(rum_devs); 3026