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