if_rum.c revision 288605
1/* $FreeBSD: head/sys/dev/usb/wlan/if_rum.c 288605 2015-10-03 15:52:58Z 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 288605 2015-10-03 15:52:58Z 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_frame_min *wh; 916 struct ieee80211_node *ni; 917 struct mbuf *m = NULL; 918 struct usb_page_cache *pc; 919 uint32_t flags; 920 uint8_t rssi = 0; 921 int len; 922 923 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 924 925 switch (USB_GET_STATE(xfer)) { 926 case USB_ST_TRANSFERRED: 927 928 DPRINTFN(15, "rx done, actlen=%d\n", len); 929 930 if (len < (int)(RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN)) { 931 DPRINTF("%s: xfer too short %d\n", 932 device_get_nameunit(sc->sc_dev), len); 933 counter_u64_add(ic->ic_ierrors, 1); 934 goto tr_setup; 935 } 936 937 len -= RT2573_RX_DESC_SIZE; 938 pc = usbd_xfer_get_frame(xfer, 0); 939 usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE); 940 941 rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi); 942 flags = le32toh(sc->sc_rx_desc.flags); 943 if (flags & RT2573_RX_CRC_ERROR) { 944 /* 945 * This should not happen since we did not 946 * request to receive those frames when we 947 * filled RUM_TXRX_CSR2: 948 */ 949 DPRINTFN(5, "PHY or CRC error\n"); 950 counter_u64_add(ic->ic_ierrors, 1); 951 goto tr_setup; 952 } 953 954 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 955 if (m == NULL) { 956 DPRINTF("could not allocate mbuf\n"); 957 counter_u64_add(ic->ic_ierrors, 1); 958 goto tr_setup; 959 } 960 usbd_copy_out(pc, RT2573_RX_DESC_SIZE, 961 mtod(m, uint8_t *), len); 962 963 wh = mtod(m, struct ieee80211_frame_min *); 964 965 /* finalize mbuf */ 966 m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff; 967 968 if (ieee80211_radiotap_active(ic)) { 969 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap; 970 971 tap->wr_flags = 0; 972 tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate, 973 (flags & RT2573_RX_OFDM) ? 974 IEEE80211_T_OFDM : IEEE80211_T_CCK); 975 rum_get_tsf(sc, &tap->wr_tsf); 976 tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi; 977 tap->wr_antnoise = RT2573_NOISE_FLOOR; 978 tap->wr_antenna = sc->rx_ant; 979 } 980 /* FALLTHROUGH */ 981 case USB_ST_SETUP: 982tr_setup: 983 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 984 usbd_transfer_submit(xfer); 985 986 /* 987 * At the end of a USB callback it is always safe to unlock 988 * the private mutex of a device! That is why we do the 989 * "ieee80211_input" here, and not some lines up! 990 */ 991 RUM_UNLOCK(sc); 992 if (m) { 993 if (m->m_len >= sizeof(struct ieee80211_frame_min)) 994 ni = ieee80211_find_rxnode(ic, wh); 995 else 996 ni = NULL; 997 998 if (ni != NULL) { 999 (void) ieee80211_input(ni, m, rssi, 1000 RT2573_NOISE_FLOOR); 1001 ieee80211_free_node(ni); 1002 } else 1003 (void) ieee80211_input_all(ic, m, rssi, 1004 RT2573_NOISE_FLOOR); 1005 } 1006 RUM_LOCK(sc); 1007 rum_start(sc); 1008 return; 1009 1010 default: /* Error */ 1011 if (error != USB_ERR_CANCELLED) { 1012 /* try to clear stall first */ 1013 usbd_xfer_set_stall(xfer); 1014 goto tr_setup; 1015 } 1016 return; 1017 } 1018} 1019 1020static uint8_t 1021rum_plcp_signal(int rate) 1022{ 1023 switch (rate) { 1024 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 1025 case 12: return 0xb; 1026 case 18: return 0xf; 1027 case 24: return 0xa; 1028 case 36: return 0xe; 1029 case 48: return 0x9; 1030 case 72: return 0xd; 1031 case 96: return 0x8; 1032 case 108: return 0xc; 1033 1034 /* CCK rates (NB: not IEEE std, device-specific) */ 1035 case 2: return 0x0; 1036 case 4: return 0x1; 1037 case 11: return 0x2; 1038 case 22: return 0x3; 1039 } 1040 return 0xff; /* XXX unsupported/unknown rate */ 1041} 1042 1043static void 1044rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc, 1045 uint32_t flags, uint16_t xflags, int len, int rate) 1046{ 1047 struct ieee80211com *ic = &sc->sc_ic; 1048 uint16_t plcp_length; 1049 int remainder; 1050 1051 desc->flags = htole32(flags); 1052 desc->flags |= htole32(RT2573_TX_VALID); 1053 desc->flags |= htole32(len << 16); 1054 1055 desc->xflags = htole16(xflags); 1056 1057 desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) | 1058 RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10)); 1059 1060 /* setup PLCP fields */ 1061 desc->plcp_signal = rum_plcp_signal(rate); 1062 desc->plcp_service = 4; 1063 1064 len += IEEE80211_CRC_LEN; 1065 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) { 1066 desc->flags |= htole32(RT2573_TX_OFDM); 1067 1068 plcp_length = len & 0xfff; 1069 desc->plcp_length_hi = plcp_length >> 6; 1070 desc->plcp_length_lo = plcp_length & 0x3f; 1071 } else { 1072 if (rate == 0) 1073 rate = 2; /* avoid division by zero */ 1074 plcp_length = (16 * len + rate - 1) / rate; 1075 if (rate == 22) { 1076 remainder = (16 * len) % 22; 1077 if (remainder != 0 && remainder < 7) 1078 desc->plcp_service |= RT2573_PLCP_LENGEXT; 1079 } 1080 desc->plcp_length_hi = plcp_length >> 8; 1081 desc->plcp_length_lo = plcp_length & 0xff; 1082 1083 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 1084 desc->plcp_signal |= 0x08; 1085 } 1086} 1087 1088static int 1089rum_sendprot(struct rum_softc *sc, 1090 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate) 1091{ 1092 struct ieee80211com *ic = ni->ni_ic; 1093 const struct ieee80211_frame *wh; 1094 struct rum_tx_data *data; 1095 struct mbuf *mprot; 1096 int protrate, pktlen, flags, isshort; 1097 uint16_t dur; 1098 1099 RUM_LOCK_ASSERT(sc); 1100 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY, 1101 ("protection %d", prot)); 1102 1103 wh = mtod(m, const struct ieee80211_frame *); 1104 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 1105 1106 protrate = ieee80211_ctl_rate(ic->ic_rt, rate); 1107 1108 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0; 1109 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort) 1110 + ieee80211_ack_duration(ic->ic_rt, rate, isshort); 1111 flags = RT2573_TX_MORE_FRAG; 1112 if (prot == IEEE80211_PROT_RTSCTS) { 1113 /* NB: CTS is the same size as an ACK */ 1114 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort); 1115 flags |= RT2573_TX_NEED_ACK; 1116 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur); 1117 } else { 1118 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur); 1119 } 1120 if (mprot == NULL) { 1121 /* XXX stat + msg */ 1122 return (ENOBUFS); 1123 } 1124 data = STAILQ_FIRST(&sc->tx_free); 1125 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1126 sc->tx_nfree--; 1127 1128 data->m = mprot; 1129 data->ni = ieee80211_ref_node(ni); 1130 data->rate = protrate; 1131 rum_setup_tx_desc(sc, &data->desc, flags, 0, mprot->m_pkthdr.len, protrate); 1132 1133 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1134 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1135 1136 return 0; 1137} 1138 1139static int 1140rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1141{ 1142 struct ieee80211vap *vap = ni->ni_vap; 1143 struct ieee80211com *ic = &sc->sc_ic; 1144 struct rum_tx_data *data; 1145 struct ieee80211_frame *wh; 1146 const struct ieee80211_txparam *tp; 1147 struct ieee80211_key *k; 1148 uint32_t flags = 0; 1149 uint16_t dur; 1150 1151 RUM_LOCK_ASSERT(sc); 1152 1153 data = STAILQ_FIRST(&sc->tx_free); 1154 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1155 sc->tx_nfree--; 1156 1157 wh = mtod(m0, struct ieee80211_frame *); 1158 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 1159 k = ieee80211_crypto_encap(ni, m0); 1160 if (k == NULL) { 1161 m_freem(m0); 1162 return ENOBUFS; 1163 } 1164 wh = mtod(m0, struct ieee80211_frame *); 1165 } 1166 1167 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1168 1169 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1170 flags |= RT2573_TX_NEED_ACK; 1171 1172 dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate, 1173 ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1174 USETW(wh->i_dur, dur); 1175 1176 /* tell hardware to add timestamp for probe responses */ 1177 if ((wh->i_fc[0] & 1178 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 1179 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP)) 1180 flags |= RT2573_TX_TIMESTAMP; 1181 } 1182 1183 data->m = m0; 1184 data->ni = ni; 1185 data->rate = tp->mgmtrate; 1186 1187 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, tp->mgmtrate); 1188 1189 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", 1190 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate); 1191 1192 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1193 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1194 1195 return (0); 1196} 1197 1198static int 1199rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni, 1200 const struct ieee80211_bpf_params *params) 1201{ 1202 struct ieee80211com *ic = ni->ni_ic; 1203 struct rum_tx_data *data; 1204 uint32_t flags; 1205 int rate, error; 1206 1207 RUM_LOCK_ASSERT(sc); 1208 KASSERT(params != NULL, ("no raw xmit params")); 1209 1210 rate = params->ibp_rate0; 1211 if (!ieee80211_isratevalid(ic->ic_rt, rate)) { 1212 m_freem(m0); 1213 return EINVAL; 1214 } 1215 flags = 0; 1216 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 1217 flags |= RT2573_TX_NEED_ACK; 1218 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) { 1219 error = rum_sendprot(sc, m0, ni, 1220 params->ibp_flags & IEEE80211_BPF_RTS ? 1221 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY, 1222 rate); 1223 if (error || sc->tx_nfree == 0) { 1224 m_freem(m0); 1225 return ENOBUFS; 1226 } 1227 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS; 1228 } 1229 1230 data = STAILQ_FIRST(&sc->tx_free); 1231 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1232 sc->tx_nfree--; 1233 1234 data->m = m0; 1235 data->ni = ni; 1236 data->rate = rate; 1237 1238 /* XXX need to setup descriptor ourself */ 1239 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate); 1240 1241 DPRINTFN(10, "sending raw frame len=%u rate=%u\n", 1242 m0->m_pkthdr.len, rate); 1243 1244 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1245 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1246 1247 return 0; 1248} 1249 1250static int 1251rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1252{ 1253 struct ieee80211vap *vap = ni->ni_vap; 1254 struct ieee80211com *ic = &sc->sc_ic; 1255 struct rum_tx_data *data; 1256 struct ieee80211_frame *wh; 1257 const struct ieee80211_txparam *tp; 1258 struct ieee80211_key *k; 1259 uint32_t flags = 0; 1260 uint16_t dur; 1261 int error, rate; 1262 1263 RUM_LOCK_ASSERT(sc); 1264 1265 wh = mtod(m0, struct ieee80211_frame *); 1266 1267 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)]; 1268 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 1269 rate = tp->mcastrate; 1270 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 1271 rate = tp->ucastrate; 1272 else 1273 rate = ni->ni_txrate; 1274 1275 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 1276 k = ieee80211_crypto_encap(ni, m0); 1277 if (k == NULL) { 1278 m_freem(m0); 1279 return ENOBUFS; 1280 } 1281 1282 /* packet header may have moved, reset our local pointer */ 1283 wh = mtod(m0, struct ieee80211_frame *); 1284 } 1285 1286 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1287 int prot = IEEE80211_PROT_NONE; 1288 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) 1289 prot = IEEE80211_PROT_RTSCTS; 1290 else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 1291 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) 1292 prot = ic->ic_protmode; 1293 if (prot != IEEE80211_PROT_NONE) { 1294 error = rum_sendprot(sc, m0, ni, prot, rate); 1295 if (error || sc->tx_nfree == 0) { 1296 m_freem(m0); 1297 return ENOBUFS; 1298 } 1299 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS; 1300 } 1301 } 1302 1303 data = STAILQ_FIRST(&sc->tx_free); 1304 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1305 sc->tx_nfree--; 1306 1307 data->m = m0; 1308 data->ni = ni; 1309 data->rate = rate; 1310 1311 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1312 flags |= RT2573_TX_NEED_ACK; 1313 flags |= RT2573_TX_MORE_FRAG; 1314 1315 dur = ieee80211_ack_duration(ic->ic_rt, rate, 1316 ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1317 USETW(wh->i_dur, dur); 1318 } 1319 1320 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate); 1321 1322 DPRINTFN(10, "sending frame len=%d rate=%d\n", 1323 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate); 1324 1325 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1326 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1327 1328 return 0; 1329} 1330 1331static int 1332rum_transmit(struct ieee80211com *ic, struct mbuf *m) 1333{ 1334 struct rum_softc *sc = ic->ic_softc; 1335 int error; 1336 1337 RUM_LOCK(sc); 1338 if (!sc->sc_running) { 1339 RUM_UNLOCK(sc); 1340 return (ENXIO); 1341 } 1342 error = mbufq_enqueue(&sc->sc_snd, m); 1343 if (error) { 1344 RUM_UNLOCK(sc); 1345 return (error); 1346 } 1347 rum_start(sc); 1348 RUM_UNLOCK(sc); 1349 1350 return (0); 1351} 1352 1353static void 1354rum_start(struct rum_softc *sc) 1355{ 1356 struct ieee80211_node *ni; 1357 struct mbuf *m; 1358 1359 RUM_LOCK_ASSERT(sc); 1360 1361 if (!sc->sc_running) 1362 return; 1363 1364 while (sc->tx_nfree >= RUM_TX_MINFREE && 1365 (m = mbufq_dequeue(&sc->sc_snd)) != NULL) { 1366 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 1367 if (rum_tx_data(sc, m, ni) != 0) { 1368 if_inc_counter(ni->ni_vap->iv_ifp, 1369 IFCOUNTER_OERRORS, 1); 1370 ieee80211_free_node(ni); 1371 break; 1372 } 1373 } 1374} 1375 1376static void 1377rum_parent(struct ieee80211com *ic) 1378{ 1379 struct rum_softc *sc = ic->ic_softc; 1380 int startall = 0; 1381 1382 RUM_LOCK(sc); 1383 if (sc->sc_detached) { 1384 RUM_UNLOCK(sc); 1385 return; 1386 } 1387 if (ic->ic_nrunning > 0) { 1388 if (!sc->sc_running) { 1389 rum_init(sc); 1390 startall = 1; 1391 } else 1392 rum_setpromisc(sc); 1393 } else if (sc->sc_running) 1394 rum_stop(sc); 1395 RUM_UNLOCK(sc); 1396 if (startall) 1397 ieee80211_start_all(ic); 1398} 1399 1400static void 1401rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len) 1402{ 1403 struct usb_device_request req; 1404 usb_error_t error; 1405 1406 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1407 req.bRequest = RT2573_READ_EEPROM; 1408 USETW(req.wValue, 0); 1409 USETW(req.wIndex, addr); 1410 USETW(req.wLength, len); 1411 1412 error = rum_do_request(sc, &req, buf); 1413 if (error != 0) { 1414 device_printf(sc->sc_dev, "could not read EEPROM: %s\n", 1415 usbd_errstr(error)); 1416 } 1417} 1418 1419static uint32_t 1420rum_read(struct rum_softc *sc, uint16_t reg) 1421{ 1422 uint32_t val; 1423 1424 rum_read_multi(sc, reg, &val, sizeof val); 1425 1426 return le32toh(val); 1427} 1428 1429static void 1430rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len) 1431{ 1432 struct usb_device_request req; 1433 usb_error_t error; 1434 1435 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1436 req.bRequest = RT2573_READ_MULTI_MAC; 1437 USETW(req.wValue, 0); 1438 USETW(req.wIndex, reg); 1439 USETW(req.wLength, len); 1440 1441 error = rum_do_request(sc, &req, buf); 1442 if (error != 0) { 1443 device_printf(sc->sc_dev, 1444 "could not multi read MAC register: %s\n", 1445 usbd_errstr(error)); 1446 } 1447} 1448 1449static usb_error_t 1450rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val) 1451{ 1452 uint32_t tmp = htole32(val); 1453 1454 return (rum_write_multi(sc, reg, &tmp, sizeof tmp)); 1455} 1456 1457static usb_error_t 1458rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len) 1459{ 1460 struct usb_device_request req; 1461 usb_error_t error; 1462 size_t offset; 1463 1464 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1465 req.bRequest = RT2573_WRITE_MULTI_MAC; 1466 USETW(req.wValue, 0); 1467 1468 /* write at most 64 bytes at a time */ 1469 for (offset = 0; offset < len; offset += 64) { 1470 USETW(req.wIndex, reg + offset); 1471 USETW(req.wLength, MIN(len - offset, 64)); 1472 1473 error = rum_do_request(sc, &req, (char *)buf + offset); 1474 if (error != 0) { 1475 device_printf(sc->sc_dev, 1476 "could not multi write MAC register: %s\n", 1477 usbd_errstr(error)); 1478 return (error); 1479 } 1480 } 1481 1482 return (USB_ERR_NORMAL_COMPLETION); 1483} 1484 1485static usb_error_t 1486rum_setbits(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_clrbits(struct rum_softc *sc, uint16_t reg, uint32_t mask) 1493{ 1494 return (rum_write(sc, reg, rum_read(sc, reg) & ~mask)); 1495} 1496 1497static usb_error_t 1498rum_modbits(struct rum_softc *sc, uint16_t reg, uint32_t set, uint32_t unset) 1499{ 1500 return (rum_write(sc, reg, (rum_read(sc, reg) & ~unset) | set)); 1501} 1502 1503static int 1504rum_bbp_busy(struct rum_softc *sc) 1505{ 1506 int ntries; 1507 1508 for (ntries = 0; ntries < 100; ntries++) { 1509 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY)) 1510 break; 1511 if (rum_pause(sc, hz / 100)) 1512 break; 1513 } 1514 if (ntries == 100) 1515 return (ETIMEDOUT); 1516 1517 return (0); 1518} 1519 1520static void 1521rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val) 1522{ 1523 uint32_t tmp; 1524 1525 DPRINTFN(2, "reg=0x%08x\n", reg); 1526 1527 if (rum_bbp_busy(sc) != 0) { 1528 device_printf(sc->sc_dev, "could not write to BBP\n"); 1529 return; 1530 } 1531 1532 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val; 1533 rum_write(sc, RT2573_PHY_CSR3, tmp); 1534} 1535 1536static uint8_t 1537rum_bbp_read(struct rum_softc *sc, uint8_t reg) 1538{ 1539 uint32_t val; 1540 int ntries; 1541 1542 DPRINTFN(2, "reg=0x%08x\n", reg); 1543 1544 if (rum_bbp_busy(sc) != 0) { 1545 device_printf(sc->sc_dev, "could not read BBP\n"); 1546 return 0; 1547 } 1548 1549 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8; 1550 rum_write(sc, RT2573_PHY_CSR3, val); 1551 1552 for (ntries = 0; ntries < 100; ntries++) { 1553 val = rum_read(sc, RT2573_PHY_CSR3); 1554 if (!(val & RT2573_BBP_BUSY)) 1555 return val & 0xff; 1556 if (rum_pause(sc, hz / 100)) 1557 break; 1558 } 1559 1560 device_printf(sc->sc_dev, "could not read BBP\n"); 1561 return 0; 1562} 1563 1564static void 1565rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val) 1566{ 1567 uint32_t tmp; 1568 int ntries; 1569 1570 for (ntries = 0; ntries < 100; ntries++) { 1571 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY)) 1572 break; 1573 if (rum_pause(sc, hz / 100)) 1574 break; 1575 } 1576 if (ntries == 100) { 1577 device_printf(sc->sc_dev, "could not write to RF\n"); 1578 return; 1579 } 1580 1581 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 | 1582 (reg & 3); 1583 rum_write(sc, RT2573_PHY_CSR4, tmp); 1584 1585 /* remember last written value in sc */ 1586 sc->rf_regs[reg] = val; 1587 1588 DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff); 1589} 1590 1591static void 1592rum_select_antenna(struct rum_softc *sc) 1593{ 1594 uint8_t bbp4, bbp77; 1595 uint32_t tmp; 1596 1597 bbp4 = rum_bbp_read(sc, 4); 1598 bbp77 = rum_bbp_read(sc, 77); 1599 1600 /* TBD */ 1601 1602 /* make sure Rx is disabled before switching antenna */ 1603 tmp = rum_read(sc, RT2573_TXRX_CSR0); 1604 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX); 1605 1606 rum_bbp_write(sc, 4, bbp4); 1607 rum_bbp_write(sc, 77, bbp77); 1608 1609 rum_write(sc, RT2573_TXRX_CSR0, tmp); 1610} 1611 1612/* 1613 * Enable multi-rate retries for frames sent at OFDM rates. 1614 * In 802.11b/g mode, allow fallback to CCK rates. 1615 */ 1616static void 1617rum_enable_mrr(struct rum_softc *sc) 1618{ 1619 struct ieee80211com *ic = &sc->sc_ic; 1620 1621 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) { 1622 rum_setbits(sc, RT2573_TXRX_CSR4, 1623 RT2573_MRR_ENABLED | RT2573_MRR_CCK_FALLBACK); 1624 } else { 1625 rum_modbits(sc, RT2573_TXRX_CSR4, 1626 RT2573_MRR_ENABLED, RT2573_MRR_CCK_FALLBACK); 1627 } 1628} 1629 1630static void 1631rum_set_txpreamble(struct rum_softc *sc) 1632{ 1633 struct ieee80211com *ic = &sc->sc_ic; 1634 1635 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 1636 rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE); 1637 else 1638 rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE); 1639} 1640 1641static void 1642rum_set_basicrates(struct rum_softc *sc) 1643{ 1644 struct ieee80211com *ic = &sc->sc_ic; 1645 1646 /* update basic rate set */ 1647 if (ic->ic_curmode == IEEE80211_MODE_11B) { 1648 /* 11b basic rates: 1, 2Mbps */ 1649 rum_write(sc, RT2573_TXRX_CSR5, 0x3); 1650 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) { 1651 /* 11a basic rates: 6, 12, 24Mbps */ 1652 rum_write(sc, RT2573_TXRX_CSR5, 0x150); 1653 } else { 1654 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */ 1655 rum_write(sc, RT2573_TXRX_CSR5, 0xf); 1656 } 1657} 1658 1659/* 1660 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference 1661 * driver. 1662 */ 1663static void 1664rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c) 1665{ 1666 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104; 1667 1668 /* update all BBP registers that depend on the band */ 1669 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c; 1670 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48; 1671 if (IEEE80211_IS_CHAN_5GHZ(c)) { 1672 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c; 1673 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10; 1674 } 1675 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) || 1676 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) { 1677 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10; 1678 } 1679 1680 sc->bbp17 = bbp17; 1681 rum_bbp_write(sc, 17, bbp17); 1682 rum_bbp_write(sc, 96, bbp96); 1683 rum_bbp_write(sc, 104, bbp104); 1684 1685 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) || 1686 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) { 1687 rum_bbp_write(sc, 75, 0x80); 1688 rum_bbp_write(sc, 86, 0x80); 1689 rum_bbp_write(sc, 88, 0x80); 1690 } 1691 1692 rum_bbp_write(sc, 35, bbp35); 1693 rum_bbp_write(sc, 97, bbp97); 1694 rum_bbp_write(sc, 98, bbp98); 1695 1696 if (IEEE80211_IS_CHAN_2GHZ(c)) { 1697 rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_2GHZ, 1698 RT2573_PA_PE_5GHZ); 1699 } else { 1700 rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_5GHZ, 1701 RT2573_PA_PE_2GHZ); 1702 } 1703} 1704 1705static void 1706rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c) 1707{ 1708 struct ieee80211com *ic = &sc->sc_ic; 1709 const struct rfprog *rfprog; 1710 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT; 1711 int8_t power; 1712 int i, chan; 1713 1714 chan = ieee80211_chan2ieee(ic, c); 1715 if (chan == 0 || chan == IEEE80211_CHAN_ANY) 1716 return; 1717 1718 /* select the appropriate RF settings based on what EEPROM says */ 1719 rfprog = (sc->rf_rev == RT2573_RF_5225 || 1720 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226; 1721 1722 /* find the settings for this channel (we know it exists) */ 1723 for (i = 0; rfprog[i].chan != chan; i++); 1724 1725 power = sc->txpow[i]; 1726 if (power < 0) { 1727 bbp94 += power; 1728 power = 0; 1729 } else if (power > 31) { 1730 bbp94 += power - 31; 1731 power = 31; 1732 } 1733 1734 /* 1735 * If we are switching from the 2GHz band to the 5GHz band or 1736 * vice-versa, BBP registers need to be reprogrammed. 1737 */ 1738 if (c->ic_flags != ic->ic_curchan->ic_flags) { 1739 rum_select_band(sc, c); 1740 rum_select_antenna(sc); 1741 } 1742 ic->ic_curchan = c; 1743 1744 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1745 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1746 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7); 1747 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1748 1749 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1750 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1751 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1); 1752 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1753 1754 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1755 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1756 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7); 1757 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1758 1759 rum_pause(sc, hz / 100); 1760 1761 /* enable smart mode for MIMO-capable RFs */ 1762 bbp3 = rum_bbp_read(sc, 3); 1763 1764 bbp3 &= ~RT2573_SMART_MODE; 1765 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527) 1766 bbp3 |= RT2573_SMART_MODE; 1767 1768 rum_bbp_write(sc, 3, bbp3); 1769 1770 if (bbp94 != RT2573_BBPR94_DEFAULT) 1771 rum_bbp_write(sc, 94, bbp94); 1772 1773 /* give the chip some extra time to do the switchover */ 1774 rum_pause(sc, hz / 100); 1775} 1776 1777/* 1778 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS 1779 * and HostAP operating modes. 1780 */ 1781static void 1782rum_enable_tsf_sync(struct rum_softc *sc) 1783{ 1784 struct ieee80211com *ic = &sc->sc_ic; 1785 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1786 uint32_t tmp; 1787 1788 if (vap->iv_opmode != IEEE80211_M_STA) { 1789 /* 1790 * Change default 16ms TBTT adjustment to 8ms. 1791 * Must be done before enabling beacon generation. 1792 */ 1793 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8); 1794 } 1795 1796 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000; 1797 1798 /* set beacon interval (in 1/16ms unit) */ 1799 tmp |= vap->iv_bss->ni_intval * 16; 1800 tmp |= RT2573_TSF_TIMER_EN | RT2573_TBTT_TIMER_EN; 1801 1802 switch (vap->iv_opmode) { 1803 case IEEE80211_M_STA: 1804 /* 1805 * Local TSF is always updated with remote TSF on beacon 1806 * reception. 1807 */ 1808 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_STA); 1809 break; 1810 case IEEE80211_M_IBSS: 1811 /* 1812 * Local TSF is updated with remote TSF on beacon reception 1813 * only if the remote TSF is greater than local TSF. 1814 */ 1815 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_IBSS); 1816 tmp |= RT2573_BCN_TX_EN; 1817 break; 1818 case IEEE80211_M_HOSTAP: 1819 /* SYNC with nobody */ 1820 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_HOSTAP); 1821 tmp |= RT2573_BCN_TX_EN; 1822 break; 1823 default: 1824 device_printf(sc->sc_dev, 1825 "Enabling TSF failed. undefined opmode %d\n", 1826 vap->iv_opmode); 1827 return; 1828 } 1829 1830 rum_write(sc, RT2573_TXRX_CSR9, tmp); 1831} 1832 1833static void 1834rum_enable_tsf(struct rum_softc *sc) 1835{ 1836 rum_modbits(sc, RT2573_TXRX_CSR9, RT2573_TSF_TIMER_EN | 1837 RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_DIS), 0x00ffffff); 1838} 1839 1840static void 1841rum_abort_tsf_sync(struct rum_softc *sc) 1842{ 1843 rum_clrbits(sc, RT2573_TXRX_CSR9, 0x00ffffff); 1844} 1845 1846static void 1847rum_get_tsf(struct rum_softc *sc, uint64_t *buf) 1848{ 1849 rum_read_multi(sc, RT2573_TXRX_CSR12, buf, sizeof (*buf)); 1850} 1851 1852static void 1853rum_update_slot(struct rum_softc *sc) 1854{ 1855 struct ieee80211com *ic = &sc->sc_ic; 1856 uint8_t slottime; 1857 1858 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20; 1859 1860 rum_modbits(sc, RT2573_MAC_CSR9, slottime, 0xff); 1861 1862 DPRINTF("setting slot time to %uus\n", slottime); 1863} 1864 1865static void 1866rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid) 1867{ 1868 uint32_t tmp; 1869 1870 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24; 1871 rum_write(sc, RT2573_MAC_CSR4, tmp); 1872 1873 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16; 1874 rum_write(sc, RT2573_MAC_CSR5, tmp); 1875} 1876 1877static void 1878rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr) 1879{ 1880 uint32_t tmp; 1881 1882 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24; 1883 rum_write(sc, RT2573_MAC_CSR2, tmp); 1884 1885 tmp = addr[4] | addr[5] << 8 | 0xff << 16; 1886 rum_write(sc, RT2573_MAC_CSR3, tmp); 1887} 1888 1889static void 1890rum_setpromisc(struct rum_softc *sc) 1891{ 1892 struct ieee80211com *ic = &sc->sc_ic; 1893 1894 if (ic->ic_promisc == 0) 1895 rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME); 1896 else 1897 rum_clrbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME); 1898 1899 DPRINTF("%s promiscuous mode\n", ic->ic_promisc > 0 ? 1900 "entering" : "leaving"); 1901} 1902 1903static void 1904rum_update_promisc(struct ieee80211com *ic) 1905{ 1906 struct rum_softc *sc = ic->ic_softc; 1907 1908 RUM_LOCK(sc); 1909 if (!sc->sc_running) { 1910 RUM_UNLOCK(sc); 1911 return; 1912 } 1913 rum_setpromisc(sc); 1914 RUM_UNLOCK(sc); 1915} 1916 1917static void 1918rum_update_mcast(struct ieee80211com *ic) 1919{ 1920 /* Ignore. */ 1921} 1922 1923static const char * 1924rum_get_rf(int rev) 1925{ 1926 switch (rev) { 1927 case RT2573_RF_2527: return "RT2527 (MIMO XR)"; 1928 case RT2573_RF_2528: return "RT2528"; 1929 case RT2573_RF_5225: return "RT5225 (MIMO XR)"; 1930 case RT2573_RF_5226: return "RT5226"; 1931 default: return "unknown"; 1932 } 1933} 1934 1935static void 1936rum_read_eeprom(struct rum_softc *sc) 1937{ 1938 uint16_t val; 1939#ifdef RUM_DEBUG 1940 int i; 1941#endif 1942 1943 /* read MAC address */ 1944 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_ic.ic_macaddr, 6); 1945 1946 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2); 1947 val = le16toh(val); 1948 sc->rf_rev = (val >> 11) & 0x1f; 1949 sc->hw_radio = (val >> 10) & 0x1; 1950 sc->rx_ant = (val >> 4) & 0x3; 1951 sc->tx_ant = (val >> 2) & 0x3; 1952 sc->nb_ant = val & 0x3; 1953 1954 DPRINTF("RF revision=%d\n", sc->rf_rev); 1955 1956 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2); 1957 val = le16toh(val); 1958 sc->ext_5ghz_lna = (val >> 6) & 0x1; 1959 sc->ext_2ghz_lna = (val >> 4) & 0x1; 1960 1961 DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n", 1962 sc->ext_2ghz_lna, sc->ext_5ghz_lna); 1963 1964 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2); 1965 val = le16toh(val); 1966 if ((val & 0xff) != 0xff) 1967 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */ 1968 1969 /* Only [-10, 10] is valid */ 1970 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10) 1971 sc->rssi_2ghz_corr = 0; 1972 1973 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2); 1974 val = le16toh(val); 1975 if ((val & 0xff) != 0xff) 1976 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */ 1977 1978 /* Only [-10, 10] is valid */ 1979 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10) 1980 sc->rssi_5ghz_corr = 0; 1981 1982 if (sc->ext_2ghz_lna) 1983 sc->rssi_2ghz_corr -= 14; 1984 if (sc->ext_5ghz_lna) 1985 sc->rssi_5ghz_corr -= 14; 1986 1987 DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n", 1988 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr); 1989 1990 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2); 1991 val = le16toh(val); 1992 if ((val & 0xff) != 0xff) 1993 sc->rffreq = val & 0xff; 1994 1995 DPRINTF("RF freq=%d\n", sc->rffreq); 1996 1997 /* read Tx power for all a/b/g channels */ 1998 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14); 1999 /* XXX default Tx power for 802.11a channels */ 2000 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14); 2001#ifdef RUM_DEBUG 2002 for (i = 0; i < 14; i++) 2003 DPRINTF("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]); 2004#endif 2005 2006 /* read default values for BBP registers */ 2007 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16); 2008#ifdef RUM_DEBUG 2009 for (i = 0; i < 14; i++) { 2010 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff) 2011 continue; 2012 DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg, 2013 sc->bbp_prom[i].val); 2014 } 2015#endif 2016} 2017 2018static int 2019rum_bbp_init(struct rum_softc *sc) 2020{ 2021 int i, ntries; 2022 2023 /* wait for BBP to be ready */ 2024 for (ntries = 0; ntries < 100; ntries++) { 2025 const uint8_t val = rum_bbp_read(sc, 0); 2026 if (val != 0 && val != 0xff) 2027 break; 2028 if (rum_pause(sc, hz / 100)) 2029 break; 2030 } 2031 if (ntries == 100) { 2032 device_printf(sc->sc_dev, "timeout waiting for BBP\n"); 2033 return EIO; 2034 } 2035 2036 /* initialize BBP registers to default values */ 2037 for (i = 0; i < nitems(rum_def_bbp); i++) 2038 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val); 2039 2040 /* write vendor-specific BBP values (from EEPROM) */ 2041 for (i = 0; i < 16; i++) { 2042 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff) 2043 continue; 2044 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 2045 } 2046 2047 return 0; 2048} 2049 2050static void 2051rum_init(struct rum_softc *sc) 2052{ 2053 struct ieee80211com *ic = &sc->sc_ic; 2054 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2055 uint32_t tmp; 2056 usb_error_t error; 2057 int i, ntries; 2058 2059 RUM_LOCK_ASSERT(sc); 2060 2061 rum_stop(sc); 2062 2063 /* initialize MAC registers to default values */ 2064 for (i = 0; i < nitems(rum_def_mac); i++) 2065 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val); 2066 2067 /* set host ready */ 2068 rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP); 2069 rum_write(sc, RT2573_MAC_CSR1, 0); 2070 2071 /* wait for BBP/RF to wakeup */ 2072 for (ntries = 0; ntries < 100; ntries++) { 2073 if (rum_read(sc, RT2573_MAC_CSR12) & 8) 2074 break; 2075 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */ 2076 if (rum_pause(sc, hz / 100)) 2077 break; 2078 } 2079 if (ntries == 100) { 2080 device_printf(sc->sc_dev, 2081 "timeout waiting for BBP/RF to wakeup\n"); 2082 goto fail; 2083 } 2084 2085 if ((error = rum_bbp_init(sc)) != 0) 2086 goto fail; 2087 2088 /* select default channel */ 2089 rum_select_band(sc, ic->ic_curchan); 2090 rum_select_antenna(sc); 2091 rum_set_chan(sc, ic->ic_curchan); 2092 2093 /* clear STA registers */ 2094 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta); 2095 2096 rum_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr); 2097 2098 /* initialize ASIC */ 2099 rum_write(sc, RT2573_MAC_CSR1, RT2573_HOST_READY); 2100 2101 /* 2102 * Allocate Tx and Rx xfer queues. 2103 */ 2104 rum_setup_tx_list(sc); 2105 2106 /* update Rx filter */ 2107 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff; 2108 2109 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR; 2110 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2111 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR | 2112 RT2573_DROP_ACKCTS; 2113 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 2114 tmp |= RT2573_DROP_TODS; 2115 if (ic->ic_promisc == 0) 2116 tmp |= RT2573_DROP_NOT_TO_ME; 2117 } 2118 rum_write(sc, RT2573_TXRX_CSR0, tmp); 2119 2120 sc->sc_running = 1; 2121 usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]); 2122 usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]); 2123 return; 2124 2125fail: rum_stop(sc); 2126#undef N 2127} 2128 2129static void 2130rum_stop(struct rum_softc *sc) 2131{ 2132 2133 RUM_LOCK_ASSERT(sc); 2134 2135 sc->sc_running = 0; 2136 2137 RUM_UNLOCK(sc); 2138 2139 /* 2140 * Drain the USB transfers, if not already drained: 2141 */ 2142 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]); 2143 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]); 2144 2145 RUM_LOCK(sc); 2146 2147 rum_unsetup_tx_list(sc); 2148 2149 /* disable Rx */ 2150 rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DISABLE_RX); 2151 2152 /* reset ASIC */ 2153 rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP); 2154 rum_write(sc, RT2573_MAC_CSR1, 0); 2155} 2156 2157static void 2158rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size) 2159{ 2160 struct usb_device_request req; 2161 uint16_t reg = RT2573_MCU_CODE_BASE; 2162 usb_error_t err; 2163 2164 /* copy firmware image into NIC */ 2165 for (; size >= 4; reg += 4, ucode += 4, size -= 4) { 2166 err = rum_write(sc, reg, UGETDW(ucode)); 2167 if (err) { 2168 /* firmware already loaded ? */ 2169 device_printf(sc->sc_dev, "Firmware load " 2170 "failure! (ignored)\n"); 2171 break; 2172 } 2173 } 2174 2175 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 2176 req.bRequest = RT2573_MCU_CNTL; 2177 USETW(req.wValue, RT2573_MCU_RUN); 2178 USETW(req.wIndex, 0); 2179 USETW(req.wLength, 0); 2180 2181 err = rum_do_request(sc, &req, NULL); 2182 if (err != 0) { 2183 device_printf(sc->sc_dev, "could not run firmware: %s\n", 2184 usbd_errstr(err)); 2185 } 2186 2187 /* give the chip some time to boot */ 2188 rum_pause(sc, hz / 8); 2189} 2190 2191static void 2192rum_prepare_beacon(struct rum_softc *sc, struct ieee80211vap *vap) 2193{ 2194 struct ieee80211com *ic = vap->iv_ic; 2195 const struct ieee80211_txparam *tp; 2196 struct rum_tx_desc desc; 2197 struct mbuf *m0; 2198 2199 if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC) 2200 return; 2201 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) 2202 return; 2203 2204 m0 = ieee80211_beacon_alloc(vap->iv_bss, &vap->iv_bcn_off); 2205 if (m0 == NULL) 2206 return; 2207 2208 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)]; 2209 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ, 2210 m0->m_pkthdr.len, tp->mgmtrate); 2211 2212 /* copy the first 24 bytes of Tx descriptor into NIC memory */ 2213 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24); 2214 2215 /* copy beacon header and payload into NIC memory */ 2216 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *), 2217 m0->m_pkthdr.len); 2218 2219 m_freem(m0); 2220} 2221 2222static int 2223rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2224 const struct ieee80211_bpf_params *params) 2225{ 2226 struct rum_softc *sc = ni->ni_ic->ic_softc; 2227 2228 RUM_LOCK(sc); 2229 /* prevent management frames from being sent if we're not ready */ 2230 if (!sc->sc_running) { 2231 RUM_UNLOCK(sc); 2232 m_freem(m); 2233 ieee80211_free_node(ni); 2234 return ENETDOWN; 2235 } 2236 if (sc->tx_nfree < RUM_TX_MINFREE) { 2237 RUM_UNLOCK(sc); 2238 m_freem(m); 2239 ieee80211_free_node(ni); 2240 return EIO; 2241 } 2242 2243 if (params == NULL) { 2244 /* 2245 * Legacy path; interpret frame contents to decide 2246 * precisely how to send the frame. 2247 */ 2248 if (rum_tx_mgt(sc, m, ni) != 0) 2249 goto bad; 2250 } else { 2251 /* 2252 * Caller supplied explicit parameters to use in 2253 * sending the frame. 2254 */ 2255 if (rum_tx_raw(sc, m, ni, params) != 0) 2256 goto bad; 2257 } 2258 RUM_UNLOCK(sc); 2259 2260 return 0; 2261bad: 2262 RUM_UNLOCK(sc); 2263 ieee80211_free_node(ni); 2264 return EIO; 2265} 2266 2267static void 2268rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni) 2269{ 2270 struct ieee80211vap *vap = ni->ni_vap; 2271 struct rum_vap *rvp = RUM_VAP(vap); 2272 2273 /* clear statistic registers (STA_CSR0 to STA_CSR5) */ 2274 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta); 2275 2276 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp); 2277} 2278 2279static void 2280rum_ratectl_timeout(void *arg) 2281{ 2282 struct rum_vap *rvp = arg; 2283 struct ieee80211vap *vap = &rvp->vap; 2284 struct ieee80211com *ic = vap->iv_ic; 2285 2286 ieee80211_runtask(ic, &rvp->ratectl_task); 2287} 2288 2289static void 2290rum_ratectl_task(void *arg, int pending) 2291{ 2292 struct rum_vap *rvp = arg; 2293 struct ieee80211vap *vap = &rvp->vap; 2294 struct ieee80211com *ic = vap->iv_ic; 2295 struct rum_softc *sc = ic->ic_softc; 2296 struct ieee80211_node *ni; 2297 int ok, fail; 2298 int sum, retrycnt; 2299 2300 RUM_LOCK(sc); 2301 /* read and clear statistic registers (STA_CSR0 to STA_CSR10) */ 2302 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta)); 2303 2304 ok = (le32toh(sc->sta[4]) >> 16) + /* TX ok w/o retry */ 2305 (le32toh(sc->sta[5]) & 0xffff); /* TX ok w/ retry */ 2306 fail = (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */ 2307 sum = ok+fail; 2308 retrycnt = (le32toh(sc->sta[5]) & 0xffff) + fail; 2309 2310 ni = ieee80211_ref_node(vap->iv_bss); 2311 ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt); 2312 (void) ieee80211_ratectl_rate(ni, NULL, 0); 2313 ieee80211_free_node(ni); 2314 2315 /* count TX retry-fail as Tx errors */ 2316 if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, fail); 2317 2318 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp); 2319 RUM_UNLOCK(sc); 2320} 2321 2322static void 2323rum_scan_start(struct ieee80211com *ic) 2324{ 2325 struct rum_softc *sc = ic->ic_softc; 2326 2327 RUM_LOCK(sc); 2328 rum_abort_tsf_sync(sc); 2329 rum_set_bssid(sc, ieee80211broadcastaddr); 2330 RUM_UNLOCK(sc); 2331 2332} 2333 2334static void 2335rum_scan_end(struct ieee80211com *ic) 2336{ 2337 struct rum_softc *sc = ic->ic_softc; 2338 2339 RUM_LOCK(sc); 2340 rum_enable_tsf_sync(sc); 2341 rum_set_bssid(sc, ic->ic_macaddr); 2342 RUM_UNLOCK(sc); 2343 2344} 2345 2346static void 2347rum_set_channel(struct ieee80211com *ic) 2348{ 2349 struct rum_softc *sc = ic->ic_softc; 2350 2351 RUM_LOCK(sc); 2352 rum_set_chan(sc, ic->ic_curchan); 2353 RUM_UNLOCK(sc); 2354} 2355 2356static int 2357rum_get_rssi(struct rum_softc *sc, uint8_t raw) 2358{ 2359 struct ieee80211com *ic = &sc->sc_ic; 2360 int lna, agc, rssi; 2361 2362 lna = (raw >> 5) & 0x3; 2363 agc = raw & 0x1f; 2364 2365 if (lna == 0) { 2366 /* 2367 * No RSSI mapping 2368 * 2369 * NB: Since RSSI is relative to noise floor, -1 is 2370 * adequate for caller to know error happened. 2371 */ 2372 return -1; 2373 } 2374 2375 rssi = (2 * agc) - RT2573_NOISE_FLOOR; 2376 2377 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) { 2378 rssi += sc->rssi_2ghz_corr; 2379 2380 if (lna == 1) 2381 rssi -= 64; 2382 else if (lna == 2) 2383 rssi -= 74; 2384 else if (lna == 3) 2385 rssi -= 90; 2386 } else { 2387 rssi += sc->rssi_5ghz_corr; 2388 2389 if (!sc->ext_5ghz_lna && lna != 1) 2390 rssi += 4; 2391 2392 if (lna == 1) 2393 rssi -= 64; 2394 else if (lna == 2) 2395 rssi -= 86; 2396 else if (lna == 3) 2397 rssi -= 100; 2398 } 2399 return rssi; 2400} 2401 2402static int 2403rum_pause(struct rum_softc *sc, int timeout) 2404{ 2405 2406 usb_pause_mtx(&sc->sc_mtx, timeout); 2407 return (0); 2408} 2409 2410static device_method_t rum_methods[] = { 2411 /* Device interface */ 2412 DEVMETHOD(device_probe, rum_match), 2413 DEVMETHOD(device_attach, rum_attach), 2414 DEVMETHOD(device_detach, rum_detach), 2415 DEVMETHOD_END 2416}; 2417 2418static driver_t rum_driver = { 2419 .name = "rum", 2420 .methods = rum_methods, 2421 .size = sizeof(struct rum_softc), 2422}; 2423 2424static devclass_t rum_devclass; 2425 2426DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0); 2427MODULE_DEPEND(rum, wlan, 1, 1, 1); 2428MODULE_DEPEND(rum, usb, 1, 1, 1); 2429MODULE_VERSION(rum, 1); 2430