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