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