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