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