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