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