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