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