if_rum.c revision 203139
1/* $FreeBSD: head/sys/dev/usb/wlan/if_rum.c 203139 2010-01-29 01:27:54Z thompsa $ */ 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 203139 2010-01-29 01:27:54Z thompsa $"); 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_arp.h> 51#include <net/ethernet.h> 52#include <net/if_dl.h> 53#include <net/if_media.h> 54#include <net/if_types.h> 55 56#ifdef INET 57#include <netinet/in.h> 58#include <netinet/in_systm.h> 59#include <netinet/in_var.h> 60#include <netinet/if_ether.h> 61#include <netinet/ip.h> 62#endif 63 64#include <net80211/ieee80211_var.h> 65#include <net80211/ieee80211_regdomain.h> 66#include <net80211/ieee80211_radiotap.h> 67#include <net80211/ieee80211_amrr.h> 68 69#include <dev/usb/usb.h> 70#include <dev/usb/usbdi.h> 71#include "usbdevs.h" 72 73#define USB_DEBUG_VAR rum_debug 74#include <dev/usb/usb_debug.h> 75 76#include <dev/usb/wlan/if_rumreg.h> 77#include <dev/usb/wlan/if_rumvar.h> 78#include <dev/usb/wlan/if_rumfw.h> 79 80#if USB_DEBUG 81static int rum_debug = 0; 82 83SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum"); 84SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RW, &rum_debug, 0, 85 "Debug level"); 86#endif 87 88static const struct usb_device_id rum_devs[] = { 89#define RUM_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) } 90 RUM_DEV(ABOCOM, HWU54DM), 91 RUM_DEV(ABOCOM, RT2573_2), 92 RUM_DEV(ABOCOM, RT2573_3), 93 RUM_DEV(ABOCOM, RT2573_4), 94 RUM_DEV(ABOCOM, WUG2700), 95 RUM_DEV(AMIT, CGWLUSB2GO), 96 RUM_DEV(ASUS, RT2573_1), 97 RUM_DEV(ASUS, RT2573_2), 98 RUM_DEV(BELKIN, F5D7050A), 99 RUM_DEV(BELKIN, F5D9050V3), 100 RUM_DEV(CISCOLINKSYS, WUSB54GC), 101 RUM_DEV(CISCOLINKSYS, WUSB54GR), 102 RUM_DEV(CONCEPTRONIC2, C54RU2), 103 RUM_DEV(COREGA, CGWLUSB2GL), 104 RUM_DEV(COREGA, CGWLUSB2GPX), 105 RUM_DEV(DICKSMITH, CWD854F), 106 RUM_DEV(DICKSMITH, RT2573), 107 RUM_DEV(EDIMAX, EW7318USG), 108 RUM_DEV(DLINK2, DWLG122C1), 109 RUM_DEV(DLINK2, WUA1340), 110 RUM_DEV(DLINK2, DWA111), 111 RUM_DEV(DLINK2, DWA110), 112 RUM_DEV(GIGABYTE, GNWB01GS), 113 RUM_DEV(GIGABYTE, GNWI05GS), 114 RUM_DEV(GIGASET, RT2573), 115 RUM_DEV(GOODWAY, RT2573), 116 RUM_DEV(GUILLEMOT, HWGUSB254LB), 117 RUM_DEV(GUILLEMOT, HWGUSB254V2AP), 118 RUM_DEV(HUAWEI3COM, WUB320G), 119 RUM_DEV(MELCO, G54HP), 120 RUM_DEV(MELCO, SG54HP), 121 RUM_DEV(MSI, RT2573_1), 122 RUM_DEV(MSI, RT2573_2), 123 RUM_DEV(MSI, RT2573_3), 124 RUM_DEV(MSI, RT2573_4), 125 RUM_DEV(NOVATECH, RT2573), 126 RUM_DEV(PLANEX2, GWUS54HP), 127 RUM_DEV(PLANEX2, GWUS54MINI2), 128 RUM_DEV(PLANEX2, GWUSMM), 129 RUM_DEV(QCOM, RT2573), 130 RUM_DEV(QCOM, RT2573_2), 131 RUM_DEV(QCOM, RT2573_3), 132 RUM_DEV(RALINK, RT2573), 133 RUM_DEV(RALINK, RT2573_2), 134 RUM_DEV(RALINK, RT2671), 135 RUM_DEV(SITECOMEU, WL113R2), 136 RUM_DEV(SITECOMEU, WL172), 137 RUM_DEV(SPARKLAN, RT2573), 138 RUM_DEV(SURECOM, RT2573), 139#undef RUM_DEV 140}; 141 142MODULE_DEPEND(rum, wlan, 1, 1, 1); 143MODULE_DEPEND(rum, wlan_amrr, 1, 1, 1); 144MODULE_DEPEND(rum, usb, 1, 1, 1); 145 146static device_probe_t rum_match; 147static device_attach_t rum_attach; 148static device_detach_t rum_detach; 149 150static usb_callback_t rum_bulk_read_callback; 151static usb_callback_t rum_bulk_write_callback; 152 153static usb_error_t rum_do_request(struct rum_softc *sc, 154 struct usb_device_request *req, void *data); 155static struct ieee80211vap *rum_vap_create(struct ieee80211com *, 156 const char name[IFNAMSIZ], int unit, int opmode, 157 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN], 158 const uint8_t mac[IEEE80211_ADDR_LEN]); 159static void rum_vap_delete(struct ieee80211vap *); 160static void rum_tx_free(struct rum_tx_data *, int); 161static void rum_setup_tx_list(struct rum_softc *); 162static void rum_unsetup_tx_list(struct rum_softc *); 163static int rum_newstate(struct ieee80211vap *, 164 enum ieee80211_state, int); 165static void rum_setup_tx_desc(struct rum_softc *, 166 struct rum_tx_desc *, uint32_t, uint16_t, int, 167 int); 168static int rum_tx_mgt(struct rum_softc *, struct mbuf *, 169 struct ieee80211_node *); 170static int rum_tx_raw(struct rum_softc *, struct mbuf *, 171 struct ieee80211_node *, 172 const struct ieee80211_bpf_params *); 173static int rum_tx_data(struct rum_softc *, struct mbuf *, 174 struct ieee80211_node *); 175static void rum_start(struct ifnet *); 176static int rum_ioctl(struct ifnet *, u_long, caddr_t); 177static void rum_eeprom_read(struct rum_softc *, uint16_t, void *, 178 int); 179static uint32_t rum_read(struct rum_softc *, uint16_t); 180static void rum_read_multi(struct rum_softc *, uint16_t, void *, 181 int); 182static usb_error_t rum_write(struct rum_softc *, uint16_t, uint32_t); 183static usb_error_t rum_write_multi(struct rum_softc *, uint16_t, void *, 184 size_t); 185static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t); 186static uint8_t rum_bbp_read(struct rum_softc *, uint8_t); 187static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t); 188static void rum_select_antenna(struct rum_softc *); 189static void rum_enable_mrr(struct rum_softc *); 190static void rum_set_txpreamble(struct rum_softc *); 191static void rum_set_basicrates(struct rum_softc *); 192static void rum_select_band(struct rum_softc *, 193 struct ieee80211_channel *); 194static void rum_set_chan(struct rum_softc *, 195 struct ieee80211_channel *); 196static void rum_enable_tsf_sync(struct rum_softc *); 197static void rum_enable_tsf(struct rum_softc *); 198static void rum_update_slot(struct ifnet *); 199static void rum_set_bssid(struct rum_softc *, const uint8_t *); 200static void rum_set_macaddr(struct rum_softc *, const uint8_t *); 201static void rum_update_promisc(struct ifnet *); 202static void rum_setpromisc(struct rum_softc *); 203static const char *rum_get_rf(int); 204static void rum_read_eeprom(struct rum_softc *); 205static int rum_bbp_init(struct rum_softc *); 206static void rum_init_locked(struct rum_softc *); 207static void rum_init(void *); 208static void rum_stop(struct rum_softc *); 209static void rum_load_microcode(struct rum_softc *, const uint8_t *, 210 size_t); 211static int rum_prepare_beacon(struct rum_softc *, 212 struct ieee80211vap *); 213static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *, 214 const struct ieee80211_bpf_params *); 215static struct ieee80211_node *rum_node_alloc(struct ieee80211vap *, 216 const uint8_t mac[IEEE80211_ADDR_LEN]); 217static void rum_newassoc(struct ieee80211_node *, int); 218static void rum_scan_start(struct ieee80211com *); 219static void rum_scan_end(struct ieee80211com *); 220static void rum_set_channel(struct ieee80211com *); 221static int rum_get_rssi(struct rum_softc *, uint8_t); 222static void rum_amrr_start(struct rum_softc *, 223 struct ieee80211_node *); 224static void rum_amrr_timeout(void *); 225static void rum_amrr_task(void *, int); 226static int rum_pause(struct rum_softc *, int); 227 228static const struct { 229 uint32_t reg; 230 uint32_t val; 231} rum_def_mac[] = { 232 { RT2573_TXRX_CSR0, 0x025fb032 }, 233 { RT2573_TXRX_CSR1, 0x9eaa9eaf }, 234 { RT2573_TXRX_CSR2, 0x8a8b8c8d }, 235 { RT2573_TXRX_CSR3, 0x00858687 }, 236 { RT2573_TXRX_CSR7, 0x2e31353b }, 237 { RT2573_TXRX_CSR8, 0x2a2a2a2c }, 238 { RT2573_TXRX_CSR15, 0x0000000f }, 239 { RT2573_MAC_CSR6, 0x00000fff }, 240 { RT2573_MAC_CSR8, 0x016c030a }, 241 { RT2573_MAC_CSR10, 0x00000718 }, 242 { RT2573_MAC_CSR12, 0x00000004 }, 243 { RT2573_MAC_CSR13, 0x00007f00 }, 244 { RT2573_SEC_CSR0, 0x00000000 }, 245 { RT2573_SEC_CSR1, 0x00000000 }, 246 { RT2573_SEC_CSR5, 0x00000000 }, 247 { RT2573_PHY_CSR1, 0x000023b0 }, 248 { RT2573_PHY_CSR5, 0x00040a06 }, 249 { RT2573_PHY_CSR6, 0x00080606 }, 250 { RT2573_PHY_CSR7, 0x00000408 }, 251 { RT2573_AIFSN_CSR, 0x00002273 }, 252 { RT2573_CWMIN_CSR, 0x00002344 }, 253 { RT2573_CWMAX_CSR, 0x000034aa } 254}; 255 256static const struct { 257 uint8_t reg; 258 uint8_t val; 259} rum_def_bbp[] = { 260 { 3, 0x80 }, 261 { 15, 0x30 }, 262 { 17, 0x20 }, 263 { 21, 0xc8 }, 264 { 22, 0x38 }, 265 { 23, 0x06 }, 266 { 24, 0xfe }, 267 { 25, 0x0a }, 268 { 26, 0x0d }, 269 { 32, 0x0b }, 270 { 34, 0x12 }, 271 { 37, 0x07 }, 272 { 39, 0xf8 }, 273 { 41, 0x60 }, 274 { 53, 0x10 }, 275 { 54, 0x18 }, 276 { 60, 0x10 }, 277 { 61, 0x04 }, 278 { 62, 0x04 }, 279 { 75, 0xfe }, 280 { 86, 0xfe }, 281 { 88, 0xfe }, 282 { 90, 0x0f }, 283 { 99, 0x00 }, 284 { 102, 0x16 }, 285 { 107, 0x04 } 286}; 287 288static const struct rfprog { 289 uint8_t chan; 290 uint32_t r1, r2, r3, r4; 291} rum_rf5226[] = { 292 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 }, 293 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 }, 294 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 }, 295 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 }, 296 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 }, 297 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 }, 298 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 }, 299 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 }, 300 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 }, 301 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 }, 302 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 }, 303 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 }, 304 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 }, 305 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 }, 306 307 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 }, 308 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 }, 309 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 }, 310 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 }, 311 312 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 }, 313 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 }, 314 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 }, 315 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 }, 316 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 }, 317 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 }, 318 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 }, 319 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 }, 320 321 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 }, 322 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 }, 323 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 }, 324 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 }, 325 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 }, 326 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 }, 327 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 }, 328 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 }, 329 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 }, 330 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 }, 331 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 }, 332 333 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 }, 334 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 }, 335 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 }, 336 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 }, 337 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 } 338}, rum_rf5225[] = { 339 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 }, 340 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 }, 341 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 }, 342 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 }, 343 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 }, 344 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 }, 345 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 }, 346 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 }, 347 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 }, 348 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 }, 349 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 }, 350 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 }, 351 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 }, 352 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 }, 353 354 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 }, 355 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 }, 356 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 }, 357 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 }, 358 359 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 }, 360 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 }, 361 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 }, 362 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 }, 363 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 }, 364 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 }, 365 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 }, 366 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 }, 367 368 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 }, 369 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 }, 370 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 }, 371 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 }, 372 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 }, 373 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 }, 374 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 }, 375 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 }, 376 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 }, 377 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 }, 378 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 }, 379 380 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 }, 381 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 }, 382 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 }, 383 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 }, 384 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 } 385}; 386 387static const struct usb_config rum_config[RUM_N_TRANSFER] = { 388 [RUM_BULK_WR] = { 389 .type = UE_BULK, 390 .endpoint = UE_ADDR_ANY, 391 .direction = UE_DIR_OUT, 392 .bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8), 393 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 394 .callback = rum_bulk_write_callback, 395 .timeout = 5000, /* ms */ 396 }, 397 [RUM_BULK_RD] = { 398 .type = UE_BULK, 399 .endpoint = UE_ADDR_ANY, 400 .direction = UE_DIR_IN, 401 .bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE), 402 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 403 .callback = rum_bulk_read_callback, 404 }, 405}; 406 407static int 408rum_match(device_t self) 409{ 410 struct usb_attach_arg *uaa = device_get_ivars(self); 411 412 if (uaa->usb_mode != USB_MODE_HOST) 413 return (ENXIO); 414 if (uaa->info.bConfigIndex != 0) 415 return (ENXIO); 416 if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX) 417 return (ENXIO); 418 419 return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa)); 420} 421 422static int 423rum_attach(device_t self) 424{ 425 struct usb_attach_arg *uaa = device_get_ivars(self); 426 struct rum_softc *sc = device_get_softc(self); 427 struct ieee80211com *ic; 428 struct ifnet *ifp; 429 uint8_t iface_index, bands; 430 uint32_t tmp; 431 int error, ntries; 432 433 device_set_usb_desc(self); 434 sc->sc_udev = uaa->device; 435 sc->sc_dev = self; 436 437 mtx_init(&sc->sc_mtx, device_get_nameunit(self), 438 MTX_NETWORK_LOCK, MTX_DEF); 439 440 iface_index = RT2573_IFACE_INDEX; 441 error = usbd_transfer_setup(uaa->device, &iface_index, 442 sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx); 443 if (error) { 444 device_printf(self, "could not allocate USB transfers, " 445 "err=%s\n", usbd_errstr(error)); 446 goto detach; 447 } 448 449 RUM_LOCK(sc); 450 /* retrieve RT2573 rev. no */ 451 for (ntries = 0; ntries < 100; ntries++) { 452 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0) 453 break; 454 if (rum_pause(sc, hz / 100)) 455 break; 456 } 457 if (ntries == 100) { 458 device_printf(sc->sc_dev, "timeout waiting for chip to settle\n"); 459 RUM_UNLOCK(sc); 460 goto detach; 461 } 462 463 /* retrieve MAC address and various other things from EEPROM */ 464 rum_read_eeprom(sc); 465 466 device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n", 467 tmp, rum_get_rf(sc->rf_rev)); 468 469 rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode)); 470 RUM_UNLOCK(sc); 471 472 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 473 if (ifp == NULL) { 474 device_printf(sc->sc_dev, "can not if_alloc()\n"); 475 goto detach; 476 } 477 ic = ifp->if_l2com; 478 479 ifp->if_softc = sc; 480 if_initname(ifp, "rum", device_get_unit(sc->sc_dev)); 481 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 482 ifp->if_init = rum_init; 483 ifp->if_ioctl = rum_ioctl; 484 ifp->if_start = rum_start; 485 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 486 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 487 IFQ_SET_READY(&ifp->if_snd); 488 489 ic->ic_ifp = ifp; 490 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 491 492 /* set device capabilities */ 493 ic->ic_caps = 494 IEEE80211_C_STA /* station mode supported */ 495 | IEEE80211_C_IBSS /* IBSS mode supported */ 496 | IEEE80211_C_MONITOR /* monitor mode supported */ 497 | IEEE80211_C_HOSTAP /* HostAp mode supported */ 498 | IEEE80211_C_TXPMGT /* tx power management */ 499 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 500 | IEEE80211_C_SHSLOT /* short slot time supported */ 501 | IEEE80211_C_BGSCAN /* bg scanning supported */ 502 | IEEE80211_C_WPA /* 802.11i */ 503 ; 504 505 bands = 0; 506 setbit(&bands, IEEE80211_MODE_11B); 507 setbit(&bands, IEEE80211_MODE_11G); 508 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) 509 setbit(&bands, IEEE80211_MODE_11A); 510 ieee80211_init_channels(ic, NULL, &bands); 511 512 ieee80211_ifattach(ic, sc->sc_bssid); 513 ic->ic_update_promisc = rum_update_promisc; 514 ic->ic_newassoc = rum_newassoc; 515 ic->ic_raw_xmit = rum_raw_xmit; 516 ic->ic_node_alloc = rum_node_alloc; 517 ic->ic_scan_start = rum_scan_start; 518 ic->ic_scan_end = rum_scan_end; 519 ic->ic_set_channel = rum_set_channel; 520 521 ic->ic_vap_create = rum_vap_create; 522 ic->ic_vap_delete = rum_vap_delete; 523 524 ieee80211_radiotap_attach(ic, 525 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap), 526 RT2573_TX_RADIOTAP_PRESENT, 527 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 528 RT2573_RX_RADIOTAP_PRESENT); 529 530 if (bootverbose) 531 ieee80211_announce(ic); 532 533 return (0); 534 535detach: 536 rum_detach(self); 537 return (ENXIO); /* failure */ 538} 539 540static int 541rum_detach(device_t self) 542{ 543 struct rum_softc *sc = device_get_softc(self); 544 struct ifnet *ifp = sc->sc_ifp; 545 struct ieee80211com *ic; 546 547 /* stop all USB transfers */ 548 usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER); 549 550 /* free TX list, if any */ 551 RUM_LOCK(sc); 552 rum_unsetup_tx_list(sc); 553 RUM_UNLOCK(sc); 554 555 if (ifp) { 556 ic = ifp->if_l2com; 557 ieee80211_ifdetach(ic); 558 if_free(ifp); 559 } 560 mtx_destroy(&sc->sc_mtx); 561 562 return (0); 563} 564 565static usb_error_t 566rum_do_request(struct rum_softc *sc, 567 struct usb_device_request *req, void *data) 568{ 569 usb_error_t err; 570 int ntries = 10; 571 572 while (ntries--) { 573 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx, 574 req, data, 0, NULL, 250 /* ms */); 575 if (err == 0) 576 break; 577 578 DPRINTFN(1, "Control request failed, %s (retrying)\n", 579 usbd_errstr(err)); 580 if (rum_pause(sc, hz / 100)) 581 break; 582 } 583 return (err); 584} 585 586static struct ieee80211vap * 587rum_vap_create(struct ieee80211com *ic, 588 const char name[IFNAMSIZ], int unit, int opmode, int flags, 589 const uint8_t bssid[IEEE80211_ADDR_LEN], 590 const uint8_t mac[IEEE80211_ADDR_LEN]) 591{ 592 struct rum_softc *sc = ic->ic_ifp->if_softc; 593 struct rum_vap *rvp; 594 struct ieee80211vap *vap; 595 596 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 597 return NULL; 598 rvp = (struct rum_vap *) malloc(sizeof(struct rum_vap), 599 M_80211_VAP, M_NOWAIT | M_ZERO); 600 if (rvp == NULL) 601 return NULL; 602 vap = &rvp->vap; 603 /* enable s/w bmiss handling for sta mode */ 604 ieee80211_vap_setup(ic, vap, name, unit, opmode, 605 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac); 606 607 /* override state transition machine */ 608 rvp->newstate = vap->iv_newstate; 609 vap->iv_newstate = rum_newstate; 610 611 usb_callout_init_mtx(&rvp->amrr_ch, &sc->sc_mtx, 0); 612 TASK_INIT(&rvp->amrr_task, 0, rum_amrr_task, rvp); 613 ieee80211_amrr_init(&rvp->amrr, vap, 614 IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD, 615 IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD, 616 1000 /* 1 sec */); 617 618 /* complete setup */ 619 ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status); 620 ic->ic_opmode = opmode; 621 return vap; 622} 623 624static void 625rum_vap_delete(struct ieee80211vap *vap) 626{ 627 struct rum_vap *rvp = RUM_VAP(vap); 628 struct ieee80211com *ic = vap->iv_ic; 629 630 usb_callout_drain(&rvp->amrr_ch); 631 ieee80211_draintask(ic, &rvp->amrr_task); 632 ieee80211_amrr_cleanup(&rvp->amrr); 633 ieee80211_vap_detach(vap); 634 free(rvp, M_80211_VAP); 635} 636 637static void 638rum_tx_free(struct rum_tx_data *data, int txerr) 639{ 640 struct rum_softc *sc = data->sc; 641 642 if (data->m != NULL) { 643 if (data->m->m_flags & M_TXCB) 644 ieee80211_process_callback(data->ni, data->m, 645 txerr ? ETIMEDOUT : 0); 646 m_freem(data->m); 647 data->m = NULL; 648 649 ieee80211_free_node(data->ni); 650 data->ni = NULL; 651 } 652 STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 653 sc->tx_nfree++; 654} 655 656static void 657rum_setup_tx_list(struct rum_softc *sc) 658{ 659 struct rum_tx_data *data; 660 int i; 661 662 sc->tx_nfree = 0; 663 STAILQ_INIT(&sc->tx_q); 664 STAILQ_INIT(&sc->tx_free); 665 666 for (i = 0; i < RUM_TX_LIST_COUNT; i++) { 667 data = &sc->tx_data[i]; 668 669 data->sc = sc; 670 STAILQ_INSERT_TAIL(&sc->tx_free, data, next); 671 sc->tx_nfree++; 672 } 673} 674 675static void 676rum_unsetup_tx_list(struct rum_softc *sc) 677{ 678 struct rum_tx_data *data; 679 int i; 680 681 /* make sure any subsequent use of the queues will fail */ 682 sc->tx_nfree = 0; 683 STAILQ_INIT(&sc->tx_q); 684 STAILQ_INIT(&sc->tx_free); 685 686 /* free up all node references and mbufs */ 687 for (i = 0; i < RUM_TX_LIST_COUNT; i++) { 688 data = &sc->tx_data[i]; 689 690 if (data->m != NULL) { 691 m_freem(data->m); 692 data->m = NULL; 693 } 694 if (data->ni != NULL) { 695 ieee80211_free_node(data->ni); 696 data->ni = NULL; 697 } 698 } 699} 700 701static int 702rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 703{ 704 struct rum_vap *rvp = RUM_VAP(vap); 705 struct ieee80211com *ic = vap->iv_ic; 706 struct rum_softc *sc = ic->ic_ifp->if_softc; 707 const struct ieee80211_txparam *tp; 708 enum ieee80211_state ostate; 709 struct ieee80211_node *ni; 710 uint32_t tmp; 711 712 ostate = vap->iv_state; 713 DPRINTF("%s -> %s\n", 714 ieee80211_state_name[ostate], 715 ieee80211_state_name[nstate]); 716 717 IEEE80211_UNLOCK(ic); 718 RUM_LOCK(sc); 719 usb_callout_stop(&rvp->amrr_ch); 720 721 switch (nstate) { 722 case IEEE80211_S_INIT: 723 if (ostate == IEEE80211_S_RUN) { 724 /* abort TSF synchronization */ 725 tmp = rum_read(sc, RT2573_TXRX_CSR9); 726 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff); 727 } 728 break; 729 730 case IEEE80211_S_RUN: 731 ni = vap->iv_bss; 732 733 if (vap->iv_opmode != IEEE80211_M_MONITOR) { 734 rum_update_slot(ic->ic_ifp); 735 rum_enable_mrr(sc); 736 rum_set_txpreamble(sc); 737 rum_set_basicrates(sc); 738 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid); 739 rum_set_bssid(sc, sc->sc_bssid); 740 } 741 742 if (vap->iv_opmode == IEEE80211_M_HOSTAP || 743 vap->iv_opmode == IEEE80211_M_IBSS) 744 rum_prepare_beacon(sc, vap); 745 746 if (vap->iv_opmode != IEEE80211_M_MONITOR) 747 rum_enable_tsf_sync(sc); 748 else 749 rum_enable_tsf(sc); 750 751 /* enable automatic rate adaptation */ 752 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 753 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) 754 rum_amrr_start(sc, ni); 755 break; 756 default: 757 break; 758 } 759 RUM_UNLOCK(sc); 760 IEEE80211_LOCK(ic); 761 return (rvp->newstate(vap, nstate, arg)); 762} 763 764static void 765rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 766{ 767 struct rum_softc *sc = usbd_xfer_softc(xfer); 768 struct ifnet *ifp = sc->sc_ifp; 769 struct ieee80211vap *vap; 770 struct rum_tx_data *data; 771 struct mbuf *m; 772 struct usb_page_cache *pc; 773 unsigned int len; 774 int actlen, sumlen; 775 776 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 777 778 switch (USB_GET_STATE(xfer)) { 779 case USB_ST_TRANSFERRED: 780 DPRINTFN(11, "transfer complete, %d bytes\n", actlen); 781 782 /* free resources */ 783 data = usbd_xfer_get_priv(xfer); 784 rum_tx_free(data, 0); 785 usbd_xfer_set_priv(xfer, NULL); 786 787 ifp->if_opackets++; 788 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 789 790 /* FALLTHROUGH */ 791 case USB_ST_SETUP: 792tr_setup: 793 data = STAILQ_FIRST(&sc->tx_q); 794 if (data) { 795 STAILQ_REMOVE_HEAD(&sc->tx_q, next); 796 m = data->m; 797 798 if (m->m_pkthdr.len > (MCLBYTES + RT2573_TX_DESC_SIZE)) { 799 DPRINTFN(0, "data overflow, %u bytes\n", 800 m->m_pkthdr.len); 801 m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE); 802 } 803 pc = usbd_xfer_get_frame(xfer, 0); 804 usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE); 805 usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0, 806 m->m_pkthdr.len); 807 808 vap = data->ni->ni_vap; 809 if (ieee80211_radiotap_active_vap(vap)) { 810 struct rum_tx_radiotap_header *tap = &sc->sc_txtap; 811 812 tap->wt_flags = 0; 813 tap->wt_rate = data->rate; 814 tap->wt_antenna = sc->tx_ant; 815 816 ieee80211_radiotap_tx(vap, m); 817 } 818 819 /* align end on a 4-bytes boundary */ 820 len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3; 821 if ((len % 64) == 0) 822 len += 4; 823 824 DPRINTFN(11, "sending frame len=%u xferlen=%u\n", 825 m->m_pkthdr.len, len); 826 827 usbd_xfer_set_frame_len(xfer, 0, len); 828 usbd_xfer_set_priv(xfer, data); 829 830 usbd_transfer_submit(xfer); 831 } 832 RUM_UNLOCK(sc); 833 rum_start(ifp); 834 RUM_LOCK(sc); 835 break; 836 837 default: /* Error */ 838 DPRINTFN(11, "transfer error, %s\n", 839 usbd_errstr(error)); 840 841 ifp->if_oerrors++; 842 data = usbd_xfer_get_priv(xfer); 843 if (data != NULL) { 844 rum_tx_free(data, error); 845 usbd_xfer_set_priv(xfer, NULL); 846 } 847 848 if (error == USB_ERR_STALLED) { 849 /* try to clear stall first */ 850 usbd_xfer_set_stall(xfer); 851 goto tr_setup; 852 } 853 if (error == USB_ERR_TIMEOUT) 854 device_printf(sc->sc_dev, "device timeout\n"); 855 break; 856 } 857} 858 859static void 860rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 861{ 862 struct rum_softc *sc = usbd_xfer_softc(xfer); 863 struct ifnet *ifp = sc->sc_ifp; 864 struct ieee80211com *ic = ifp->if_l2com; 865 struct ieee80211_node *ni; 866 struct mbuf *m = NULL; 867 struct usb_page_cache *pc; 868 uint32_t flags; 869 uint8_t rssi = 0; 870 int len; 871 872 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 873 874 switch (USB_GET_STATE(xfer)) { 875 case USB_ST_TRANSFERRED: 876 877 DPRINTFN(15, "rx done, actlen=%d\n", len); 878 879 if (len < RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN) { 880 DPRINTF("%s: xfer too short %d\n", 881 device_get_nameunit(sc->sc_dev), len); 882 ifp->if_ierrors++; 883 goto tr_setup; 884 } 885 886 len -= RT2573_RX_DESC_SIZE; 887 pc = usbd_xfer_get_frame(xfer, 0); 888 usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE); 889 890 rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi); 891 flags = le32toh(sc->sc_rx_desc.flags); 892 if (flags & RT2573_RX_CRC_ERROR) { 893 /* 894 * This should not happen since we did not 895 * request to receive those frames when we 896 * filled RUM_TXRX_CSR2: 897 */ 898 DPRINTFN(5, "PHY or CRC error\n"); 899 ifp->if_ierrors++; 900 goto tr_setup; 901 } 902 903 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 904 if (m == NULL) { 905 DPRINTF("could not allocate mbuf\n"); 906 ifp->if_ierrors++; 907 goto tr_setup; 908 } 909 usbd_copy_out(pc, RT2573_RX_DESC_SIZE, 910 mtod(m, uint8_t *), len); 911 912 /* finalize mbuf */ 913 m->m_pkthdr.rcvif = ifp; 914 m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff; 915 916 if (ieee80211_radiotap_active(ic)) { 917 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap; 918 919 /* XXX read tsf */ 920 tap->wr_flags = 0; 921 tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate, 922 (flags & RT2573_RX_OFDM) ? 923 IEEE80211_T_OFDM : IEEE80211_T_CCK); 924 tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi; 925 tap->wr_antnoise = RT2573_NOISE_FLOOR; 926 tap->wr_antenna = sc->rx_ant; 927 } 928 /* FALLTHROUGH */ 929 case USB_ST_SETUP: 930tr_setup: 931 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 932 usbd_transfer_submit(xfer); 933 934 /* 935 * At the end of a USB callback it is always safe to unlock 936 * the private mutex of a device! That is why we do the 937 * "ieee80211_input" here, and not some lines up! 938 */ 939 RUM_UNLOCK(sc); 940 if (m) { 941 ni = ieee80211_find_rxnode(ic, 942 mtod(m, struct ieee80211_frame_min *)); 943 if (ni != NULL) { 944 (void) ieee80211_input(ni, m, rssi, 945 RT2573_NOISE_FLOOR); 946 ieee80211_free_node(ni); 947 } else 948 (void) ieee80211_input_all(ic, m, rssi, 949 RT2573_NOISE_FLOOR); 950 } 951 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 && 952 !IFQ_IS_EMPTY(&ifp->if_snd)) 953 rum_start(ifp); 954 RUM_LOCK(sc); 955 return; 956 957 default: /* Error */ 958 if (error != USB_ERR_CANCELLED) { 959 /* try to clear stall first */ 960 usbd_xfer_set_stall(xfer); 961 goto tr_setup; 962 } 963 return; 964 } 965} 966 967static uint8_t 968rum_plcp_signal(int rate) 969{ 970 switch (rate) { 971 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 972 case 12: return 0xb; 973 case 18: return 0xf; 974 case 24: return 0xa; 975 case 36: return 0xe; 976 case 48: return 0x9; 977 case 72: return 0xd; 978 case 96: return 0x8; 979 case 108: return 0xc; 980 981 /* CCK rates (NB: not IEEE std, device-specific) */ 982 case 2: return 0x0; 983 case 4: return 0x1; 984 case 11: return 0x2; 985 case 22: return 0x3; 986 } 987 return 0xff; /* XXX unsupported/unknown rate */ 988} 989 990static void 991rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc, 992 uint32_t flags, uint16_t xflags, int len, int rate) 993{ 994 struct ifnet *ifp = sc->sc_ifp; 995 struct ieee80211com *ic = ifp->if_l2com; 996 uint16_t plcp_length; 997 int remainder; 998 999 desc->flags = htole32(flags); 1000 desc->flags |= htole32(RT2573_TX_VALID); 1001 desc->flags |= htole32(len << 16); 1002 1003 desc->xflags = htole16(xflags); 1004 1005 desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) | 1006 RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10)); 1007 1008 /* setup PLCP fields */ 1009 desc->plcp_signal = rum_plcp_signal(rate); 1010 desc->plcp_service = 4; 1011 1012 len += IEEE80211_CRC_LEN; 1013 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) { 1014 desc->flags |= htole32(RT2573_TX_OFDM); 1015 1016 plcp_length = len & 0xfff; 1017 desc->plcp_length_hi = plcp_length >> 6; 1018 desc->plcp_length_lo = plcp_length & 0x3f; 1019 } else { 1020 plcp_length = (16 * len + rate - 1) / rate; 1021 if (rate == 22) { 1022 remainder = (16 * len) % 22; 1023 if (remainder != 0 && remainder < 7) 1024 desc->plcp_service |= RT2573_PLCP_LENGEXT; 1025 } 1026 desc->plcp_length_hi = plcp_length >> 8; 1027 desc->plcp_length_lo = plcp_length & 0xff; 1028 1029 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 1030 desc->plcp_signal |= 0x08; 1031 } 1032} 1033 1034static int 1035rum_sendprot(struct rum_softc *sc, 1036 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate) 1037{ 1038 struct ieee80211com *ic = ni->ni_ic; 1039 const struct ieee80211_frame *wh; 1040 struct rum_tx_data *data; 1041 struct mbuf *mprot; 1042 int protrate, ackrate, pktlen, flags, isshort; 1043 uint16_t dur; 1044 1045 RUM_LOCK_ASSERT(sc, MA_OWNED); 1046 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY, 1047 ("protection %d", prot)); 1048 1049 wh = mtod(m, const struct ieee80211_frame *); 1050 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 1051 1052 protrate = ieee80211_ctl_rate(ic->ic_rt, rate); 1053 ackrate = ieee80211_ack_rate(ic->ic_rt, rate); 1054 1055 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0; 1056 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort); 1057 + ieee80211_ack_duration(ic->ic_rt, rate, isshort); 1058 flags = RT2573_TX_MORE_FRAG; 1059 if (prot == IEEE80211_PROT_RTSCTS) { 1060 /* NB: CTS is the same size as an ACK */ 1061 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort); 1062 flags |= RT2573_TX_NEED_ACK; 1063 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur); 1064 } else { 1065 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur); 1066 } 1067 if (mprot == NULL) { 1068 /* XXX stat + msg */ 1069 return (ENOBUFS); 1070 } 1071 data = STAILQ_FIRST(&sc->tx_free); 1072 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1073 sc->tx_nfree--; 1074 1075 data->m = mprot; 1076 data->ni = ieee80211_ref_node(ni); 1077 data->rate = protrate; 1078 rum_setup_tx_desc(sc, &data->desc, flags, 0, mprot->m_pkthdr.len, protrate); 1079 1080 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1081 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1082 1083 return 0; 1084} 1085 1086static int 1087rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1088{ 1089 struct ieee80211vap *vap = ni->ni_vap; 1090 struct ifnet *ifp = sc->sc_ifp; 1091 struct ieee80211com *ic = ifp->if_l2com; 1092 struct rum_tx_data *data; 1093 struct ieee80211_frame *wh; 1094 const struct ieee80211_txparam *tp; 1095 struct ieee80211_key *k; 1096 uint32_t flags = 0; 1097 uint16_t dur; 1098 1099 RUM_LOCK_ASSERT(sc, MA_OWNED); 1100 1101 data = STAILQ_FIRST(&sc->tx_free); 1102 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1103 sc->tx_nfree--; 1104 1105 wh = mtod(m0, struct ieee80211_frame *); 1106 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1107 k = ieee80211_crypto_encap(ni, m0); 1108 if (k == NULL) { 1109 m_freem(m0); 1110 return ENOBUFS; 1111 } 1112 wh = mtod(m0, struct ieee80211_frame *); 1113 } 1114 1115 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1116 1117 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1118 flags |= RT2573_TX_NEED_ACK; 1119 1120 dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate, 1121 ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1122 *(uint16_t *)wh->i_dur = htole16(dur); 1123 1124 /* tell hardware to add timestamp for probe responses */ 1125 if ((wh->i_fc[0] & 1126 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 1127 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP)) 1128 flags |= RT2573_TX_TIMESTAMP; 1129 } 1130 1131 data->m = m0; 1132 data->ni = ni; 1133 data->rate = tp->mgmtrate; 1134 1135 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, tp->mgmtrate); 1136 1137 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", 1138 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate); 1139 1140 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1141 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1142 1143 return (0); 1144} 1145 1146static int 1147rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni, 1148 const struct ieee80211_bpf_params *params) 1149{ 1150 struct ieee80211com *ic = ni->ni_ic; 1151 struct rum_tx_data *data; 1152 uint32_t flags; 1153 int rate, error; 1154 1155 RUM_LOCK_ASSERT(sc, MA_OWNED); 1156 KASSERT(params != NULL, ("no raw xmit params")); 1157 1158 rate = params->ibp_rate0; 1159 if (!ieee80211_isratevalid(ic->ic_rt, rate)) { 1160 m_freem(m0); 1161 return EINVAL; 1162 } 1163 flags = 0; 1164 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 1165 flags |= RT2573_TX_NEED_ACK; 1166 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) { 1167 error = rum_sendprot(sc, m0, ni, 1168 params->ibp_flags & IEEE80211_BPF_RTS ? 1169 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY, 1170 rate); 1171 if (error || sc->tx_nfree == 0) { 1172 m_freem(m0); 1173 return ENOBUFS; 1174 } 1175 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS; 1176 } 1177 1178 data = STAILQ_FIRST(&sc->tx_free); 1179 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1180 sc->tx_nfree--; 1181 1182 data->m = m0; 1183 data->ni = ni; 1184 data->rate = rate; 1185 1186 /* XXX need to setup descriptor ourself */ 1187 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate); 1188 1189 DPRINTFN(10, "sending raw frame len=%u rate=%u\n", 1190 m0->m_pkthdr.len, rate); 1191 1192 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1193 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1194 1195 return 0; 1196} 1197 1198static int 1199rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1200{ 1201 struct ieee80211vap *vap = ni->ni_vap; 1202 struct ifnet *ifp = sc->sc_ifp; 1203 struct ieee80211com *ic = ifp->if_l2com; 1204 struct rum_tx_data *data; 1205 struct ieee80211_frame *wh; 1206 const struct ieee80211_txparam *tp; 1207 struct ieee80211_key *k; 1208 uint32_t flags = 0; 1209 uint16_t dur; 1210 int error, rate; 1211 1212 RUM_LOCK_ASSERT(sc, MA_OWNED); 1213 1214 wh = mtod(m0, struct ieee80211_frame *); 1215 1216 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)]; 1217 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 1218 rate = tp->mcastrate; 1219 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 1220 rate = tp->ucastrate; 1221 else 1222 rate = ni->ni_txrate; 1223 1224 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1225 k = ieee80211_crypto_encap(ni, m0); 1226 if (k == NULL) { 1227 m_freem(m0); 1228 return ENOBUFS; 1229 } 1230 1231 /* packet header may have moved, reset our local pointer */ 1232 wh = mtod(m0, struct ieee80211_frame *); 1233 } 1234 1235 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1236 int prot = IEEE80211_PROT_NONE; 1237 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) 1238 prot = IEEE80211_PROT_RTSCTS; 1239 else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 1240 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) 1241 prot = ic->ic_protmode; 1242 if (prot != IEEE80211_PROT_NONE) { 1243 error = rum_sendprot(sc, m0, ni, prot, rate); 1244 if (error || sc->tx_nfree == 0) { 1245 m_freem(m0); 1246 return ENOBUFS; 1247 } 1248 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS; 1249 } 1250 } 1251 1252 data = STAILQ_FIRST(&sc->tx_free); 1253 STAILQ_REMOVE_HEAD(&sc->tx_free, next); 1254 sc->tx_nfree--; 1255 1256 data->m = m0; 1257 data->ni = ni; 1258 data->rate = rate; 1259 1260 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1261 flags |= RT2573_TX_NEED_ACK; 1262 flags |= RT2573_TX_MORE_FRAG; 1263 1264 dur = ieee80211_ack_duration(ic->ic_rt, rate, 1265 ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1266 *(uint16_t *)wh->i_dur = htole16(dur); 1267 } 1268 1269 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate); 1270 1271 DPRINTFN(10, "sending frame len=%d rate=%d\n", 1272 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate); 1273 1274 STAILQ_INSERT_TAIL(&sc->tx_q, data, next); 1275 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]); 1276 1277 return 0; 1278} 1279 1280static void 1281rum_start(struct ifnet *ifp) 1282{ 1283 struct rum_softc *sc = ifp->if_softc; 1284 struct ieee80211_node *ni; 1285 struct mbuf *m; 1286 1287 RUM_LOCK(sc); 1288 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1289 RUM_UNLOCK(sc); 1290 return; 1291 } 1292 for (;;) { 1293 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 1294 if (m == NULL) 1295 break; 1296 if (sc->tx_nfree < RUM_TX_MINFREE) { 1297 IFQ_DRV_PREPEND(&ifp->if_snd, m); 1298 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1299 break; 1300 } 1301 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 1302 if (rum_tx_data(sc, m, ni) != 0) { 1303 ieee80211_free_node(ni); 1304 ifp->if_oerrors++; 1305 break; 1306 } 1307 } 1308 RUM_UNLOCK(sc); 1309} 1310 1311static int 1312rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1313{ 1314 struct rum_softc *sc = ifp->if_softc; 1315 struct ieee80211com *ic = ifp->if_l2com; 1316 struct ifreq *ifr = (struct ifreq *) data; 1317 int error = 0, startall = 0; 1318 1319 switch (cmd) { 1320 case SIOCSIFFLAGS: 1321 RUM_LOCK(sc); 1322 if (ifp->if_flags & IFF_UP) { 1323 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1324 rum_init_locked(sc); 1325 startall = 1; 1326 } else 1327 rum_setpromisc(sc); 1328 } else { 1329 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1330 rum_stop(sc); 1331 } 1332 RUM_UNLOCK(sc); 1333 if (startall) 1334 ieee80211_start_all(ic); 1335 break; 1336 case SIOCGIFMEDIA: 1337 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 1338 break; 1339 case SIOCGIFADDR: 1340 error = ether_ioctl(ifp, cmd, data); 1341 break; 1342 default: 1343 error = EINVAL; 1344 break; 1345 } 1346 return error; 1347} 1348 1349static void 1350rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len) 1351{ 1352 struct usb_device_request req; 1353 usb_error_t error; 1354 1355 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1356 req.bRequest = RT2573_READ_EEPROM; 1357 USETW(req.wValue, 0); 1358 USETW(req.wIndex, addr); 1359 USETW(req.wLength, len); 1360 1361 error = rum_do_request(sc, &req, buf); 1362 if (error != 0) { 1363 device_printf(sc->sc_dev, "could not read EEPROM: %s\n", 1364 usbd_errstr(error)); 1365 } 1366} 1367 1368static uint32_t 1369rum_read(struct rum_softc *sc, uint16_t reg) 1370{ 1371 uint32_t val; 1372 1373 rum_read_multi(sc, reg, &val, sizeof val); 1374 1375 return le32toh(val); 1376} 1377 1378static void 1379rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len) 1380{ 1381 struct usb_device_request req; 1382 usb_error_t error; 1383 1384 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1385 req.bRequest = RT2573_READ_MULTI_MAC; 1386 USETW(req.wValue, 0); 1387 USETW(req.wIndex, reg); 1388 USETW(req.wLength, len); 1389 1390 error = rum_do_request(sc, &req, buf); 1391 if (error != 0) { 1392 device_printf(sc->sc_dev, 1393 "could not multi read MAC register: %s\n", 1394 usbd_errstr(error)); 1395 } 1396} 1397 1398static usb_error_t 1399rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val) 1400{ 1401 uint32_t tmp = htole32(val); 1402 1403 return (rum_write_multi(sc, reg, &tmp, sizeof tmp)); 1404} 1405 1406static usb_error_t 1407rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len) 1408{ 1409 struct usb_device_request req; 1410 usb_error_t error; 1411 1412 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 1413 req.bRequest = RT2573_WRITE_MULTI_MAC; 1414 USETW(req.wValue, 0); 1415 USETW(req.wIndex, reg); 1416 USETW(req.wLength, len); 1417 1418 error = rum_do_request(sc, &req, buf); 1419 if (error != 0) { 1420 device_printf(sc->sc_dev, 1421 "could not multi write MAC register: %s\n", 1422 usbd_errstr(error)); 1423 } 1424 return (error); 1425} 1426 1427static void 1428rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val) 1429{ 1430 uint32_t tmp; 1431 int ntries; 1432 1433 DPRINTFN(2, "reg=0x%08x\n", reg); 1434 1435 for (ntries = 0; ntries < 100; ntries++) { 1436 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY)) 1437 break; 1438 if (rum_pause(sc, hz / 100)) 1439 break; 1440 } 1441 if (ntries == 100) { 1442 device_printf(sc->sc_dev, "could not write to BBP\n"); 1443 return; 1444 } 1445 1446 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val; 1447 rum_write(sc, RT2573_PHY_CSR3, tmp); 1448} 1449 1450static uint8_t 1451rum_bbp_read(struct rum_softc *sc, uint8_t reg) 1452{ 1453 uint32_t val; 1454 int ntries; 1455 1456 DPRINTFN(2, "reg=0x%08x\n", reg); 1457 1458 for (ntries = 0; ntries < 100; ntries++) { 1459 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY)) 1460 break; 1461 if (rum_pause(sc, hz / 100)) 1462 break; 1463 } 1464 if (ntries == 100) { 1465 device_printf(sc->sc_dev, "could not read BBP\n"); 1466 return 0; 1467 } 1468 1469 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8; 1470 rum_write(sc, RT2573_PHY_CSR3, val); 1471 1472 for (ntries = 0; ntries < 100; ntries++) { 1473 val = rum_read(sc, RT2573_PHY_CSR3); 1474 if (!(val & RT2573_BBP_BUSY)) 1475 return val & 0xff; 1476 if (rum_pause(sc, hz / 100)) 1477 break; 1478 } 1479 1480 device_printf(sc->sc_dev, "could not read BBP\n"); 1481 return 0; 1482} 1483 1484static void 1485rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val) 1486{ 1487 uint32_t tmp; 1488 int ntries; 1489 1490 for (ntries = 0; ntries < 100; ntries++) { 1491 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY)) 1492 break; 1493 if (rum_pause(sc, hz / 100)) 1494 break; 1495 } 1496 if (ntries == 100) { 1497 device_printf(sc->sc_dev, "could not write to RF\n"); 1498 return; 1499 } 1500 1501 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 | 1502 (reg & 3); 1503 rum_write(sc, RT2573_PHY_CSR4, tmp); 1504 1505 /* remember last written value in sc */ 1506 sc->rf_regs[reg] = val; 1507 1508 DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff); 1509} 1510 1511static void 1512rum_select_antenna(struct rum_softc *sc) 1513{ 1514 uint8_t bbp4, bbp77; 1515 uint32_t tmp; 1516 1517 bbp4 = rum_bbp_read(sc, 4); 1518 bbp77 = rum_bbp_read(sc, 77); 1519 1520 /* TBD */ 1521 1522 /* make sure Rx is disabled before switching antenna */ 1523 tmp = rum_read(sc, RT2573_TXRX_CSR0); 1524 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX); 1525 1526 rum_bbp_write(sc, 4, bbp4); 1527 rum_bbp_write(sc, 77, bbp77); 1528 1529 rum_write(sc, RT2573_TXRX_CSR0, tmp); 1530} 1531 1532/* 1533 * Enable multi-rate retries for frames sent at OFDM rates. 1534 * In 802.11b/g mode, allow fallback to CCK rates. 1535 */ 1536static void 1537rum_enable_mrr(struct rum_softc *sc) 1538{ 1539 struct ifnet *ifp = sc->sc_ifp; 1540 struct ieee80211com *ic = ifp->if_l2com; 1541 uint32_t tmp; 1542 1543 tmp = rum_read(sc, RT2573_TXRX_CSR4); 1544 1545 tmp &= ~RT2573_MRR_CCK_FALLBACK; 1546 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) 1547 tmp |= RT2573_MRR_CCK_FALLBACK; 1548 tmp |= RT2573_MRR_ENABLED; 1549 1550 rum_write(sc, RT2573_TXRX_CSR4, tmp); 1551} 1552 1553static void 1554rum_set_txpreamble(struct rum_softc *sc) 1555{ 1556 struct ifnet *ifp = sc->sc_ifp; 1557 struct ieee80211com *ic = ifp->if_l2com; 1558 uint32_t tmp; 1559 1560 tmp = rum_read(sc, RT2573_TXRX_CSR4); 1561 1562 tmp &= ~RT2573_SHORT_PREAMBLE; 1563 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 1564 tmp |= RT2573_SHORT_PREAMBLE; 1565 1566 rum_write(sc, RT2573_TXRX_CSR4, tmp); 1567} 1568 1569static void 1570rum_set_basicrates(struct rum_softc *sc) 1571{ 1572 struct ifnet *ifp = sc->sc_ifp; 1573 struct ieee80211com *ic = ifp->if_l2com; 1574 1575 /* update basic rate set */ 1576 if (ic->ic_curmode == IEEE80211_MODE_11B) { 1577 /* 11b basic rates: 1, 2Mbps */ 1578 rum_write(sc, RT2573_TXRX_CSR5, 0x3); 1579 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) { 1580 /* 11a basic rates: 6, 12, 24Mbps */ 1581 rum_write(sc, RT2573_TXRX_CSR5, 0x150); 1582 } else { 1583 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */ 1584 rum_write(sc, RT2573_TXRX_CSR5, 0xf); 1585 } 1586} 1587 1588/* 1589 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference 1590 * driver. 1591 */ 1592static void 1593rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c) 1594{ 1595 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104; 1596 uint32_t tmp; 1597 1598 /* update all BBP registers that depend on the band */ 1599 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c; 1600 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48; 1601 if (IEEE80211_IS_CHAN_5GHZ(c)) { 1602 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c; 1603 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10; 1604 } 1605 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) || 1606 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) { 1607 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10; 1608 } 1609 1610 sc->bbp17 = bbp17; 1611 rum_bbp_write(sc, 17, bbp17); 1612 rum_bbp_write(sc, 96, bbp96); 1613 rum_bbp_write(sc, 104, bbp104); 1614 1615 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) || 1616 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) { 1617 rum_bbp_write(sc, 75, 0x80); 1618 rum_bbp_write(sc, 86, 0x80); 1619 rum_bbp_write(sc, 88, 0x80); 1620 } 1621 1622 rum_bbp_write(sc, 35, bbp35); 1623 rum_bbp_write(sc, 97, bbp97); 1624 rum_bbp_write(sc, 98, bbp98); 1625 1626 tmp = rum_read(sc, RT2573_PHY_CSR0); 1627 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ); 1628 if (IEEE80211_IS_CHAN_2GHZ(c)) 1629 tmp |= RT2573_PA_PE_2GHZ; 1630 else 1631 tmp |= RT2573_PA_PE_5GHZ; 1632 rum_write(sc, RT2573_PHY_CSR0, tmp); 1633} 1634 1635static void 1636rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c) 1637{ 1638 struct ifnet *ifp = sc->sc_ifp; 1639 struct ieee80211com *ic = ifp->if_l2com; 1640 const struct rfprog *rfprog; 1641 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT; 1642 int8_t power; 1643 int i, chan; 1644 1645 chan = ieee80211_chan2ieee(ic, c); 1646 if (chan == 0 || chan == IEEE80211_CHAN_ANY) 1647 return; 1648 1649 /* select the appropriate RF settings based on what EEPROM says */ 1650 rfprog = (sc->rf_rev == RT2573_RF_5225 || 1651 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226; 1652 1653 /* find the settings for this channel (we know it exists) */ 1654 for (i = 0; rfprog[i].chan != chan; i++); 1655 1656 power = sc->txpow[i]; 1657 if (power < 0) { 1658 bbp94 += power; 1659 power = 0; 1660 } else if (power > 31) { 1661 bbp94 += power - 31; 1662 power = 31; 1663 } 1664 1665 /* 1666 * If we are switching from the 2GHz band to the 5GHz band or 1667 * vice-versa, BBP registers need to be reprogrammed. 1668 */ 1669 if (c->ic_flags != ic->ic_curchan->ic_flags) { 1670 rum_select_band(sc, c); 1671 rum_select_antenna(sc); 1672 } 1673 ic->ic_curchan = c; 1674 1675 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1676 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1677 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7); 1678 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1679 1680 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1681 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1682 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1); 1683 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1684 1685 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1); 1686 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2); 1687 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7); 1688 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10); 1689 1690 rum_pause(sc, hz / 100); 1691 1692 /* enable smart mode for MIMO-capable RFs */ 1693 bbp3 = rum_bbp_read(sc, 3); 1694 1695 bbp3 &= ~RT2573_SMART_MODE; 1696 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527) 1697 bbp3 |= RT2573_SMART_MODE; 1698 1699 rum_bbp_write(sc, 3, bbp3); 1700 1701 if (bbp94 != RT2573_BBPR94_DEFAULT) 1702 rum_bbp_write(sc, 94, bbp94); 1703 1704 /* give the chip some extra time to do the switchover */ 1705 rum_pause(sc, hz / 100); 1706} 1707 1708/* 1709 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS 1710 * and HostAP operating modes. 1711 */ 1712static void 1713rum_enable_tsf_sync(struct rum_softc *sc) 1714{ 1715 struct ifnet *ifp = sc->sc_ifp; 1716 struct ieee80211com *ic = ifp->if_l2com; 1717 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1718 uint32_t tmp; 1719 1720 if (vap->iv_opmode != IEEE80211_M_STA) { 1721 /* 1722 * Change default 16ms TBTT adjustment to 8ms. 1723 * Must be done before enabling beacon generation. 1724 */ 1725 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8); 1726 } 1727 1728 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000; 1729 1730 /* set beacon interval (in 1/16ms unit) */ 1731 tmp |= vap->iv_bss->ni_intval * 16; 1732 1733 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT; 1734 if (vap->iv_opmode == IEEE80211_M_STA) 1735 tmp |= RT2573_TSF_MODE(1); 1736 else 1737 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON; 1738 1739 rum_write(sc, RT2573_TXRX_CSR9, tmp); 1740} 1741 1742static void 1743rum_enable_tsf(struct rum_softc *sc) 1744{ 1745 rum_write(sc, RT2573_TXRX_CSR9, 1746 (rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000) | 1747 RT2573_TSF_TICKING | RT2573_TSF_MODE(2)); 1748} 1749 1750static void 1751rum_update_slot(struct ifnet *ifp) 1752{ 1753 struct rum_softc *sc = ifp->if_softc; 1754 struct ieee80211com *ic = ifp->if_l2com; 1755 uint8_t slottime; 1756 uint32_t tmp; 1757 1758 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20; 1759 1760 tmp = rum_read(sc, RT2573_MAC_CSR9); 1761 tmp = (tmp & ~0xff) | slottime; 1762 rum_write(sc, RT2573_MAC_CSR9, tmp); 1763 1764 DPRINTF("setting slot time to %uus\n", slottime); 1765} 1766 1767static void 1768rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid) 1769{ 1770 uint32_t tmp; 1771 1772 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24; 1773 rum_write(sc, RT2573_MAC_CSR4, tmp); 1774 1775 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16; 1776 rum_write(sc, RT2573_MAC_CSR5, tmp); 1777} 1778 1779static void 1780rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr) 1781{ 1782 uint32_t tmp; 1783 1784 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24; 1785 rum_write(sc, RT2573_MAC_CSR2, tmp); 1786 1787 tmp = addr[4] | addr[5] << 8 | 0xff << 16; 1788 rum_write(sc, RT2573_MAC_CSR3, tmp); 1789} 1790 1791static void 1792rum_setpromisc(struct rum_softc *sc) 1793{ 1794 struct ifnet *ifp = sc->sc_ifp; 1795 uint32_t tmp; 1796 1797 tmp = rum_read(sc, RT2573_TXRX_CSR0); 1798 1799 tmp &= ~RT2573_DROP_NOT_TO_ME; 1800 if (!(ifp->if_flags & IFF_PROMISC)) 1801 tmp |= RT2573_DROP_NOT_TO_ME; 1802 1803 rum_write(sc, RT2573_TXRX_CSR0, tmp); 1804 1805 DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ? 1806 "entering" : "leaving"); 1807} 1808 1809static void 1810rum_update_promisc(struct ifnet *ifp) 1811{ 1812 struct rum_softc *sc = ifp->if_softc; 1813 1814 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 1815 return; 1816 1817 RUM_LOCK(sc); 1818 rum_setpromisc(sc); 1819 RUM_UNLOCK(sc); 1820} 1821 1822static const char * 1823rum_get_rf(int rev) 1824{ 1825 switch (rev) { 1826 case RT2573_RF_2527: return "RT2527 (MIMO XR)"; 1827 case RT2573_RF_2528: return "RT2528"; 1828 case RT2573_RF_5225: return "RT5225 (MIMO XR)"; 1829 case RT2573_RF_5226: return "RT5226"; 1830 default: return "unknown"; 1831 } 1832} 1833 1834static void 1835rum_read_eeprom(struct rum_softc *sc) 1836{ 1837 uint16_t val; 1838#ifdef RUM_DEBUG 1839 int i; 1840#endif 1841 1842 /* read MAC address */ 1843 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_bssid, 6); 1844 1845 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2); 1846 val = le16toh(val); 1847 sc->rf_rev = (val >> 11) & 0x1f; 1848 sc->hw_radio = (val >> 10) & 0x1; 1849 sc->rx_ant = (val >> 4) & 0x3; 1850 sc->tx_ant = (val >> 2) & 0x3; 1851 sc->nb_ant = val & 0x3; 1852 1853 DPRINTF("RF revision=%d\n", sc->rf_rev); 1854 1855 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2); 1856 val = le16toh(val); 1857 sc->ext_5ghz_lna = (val >> 6) & 0x1; 1858 sc->ext_2ghz_lna = (val >> 4) & 0x1; 1859 1860 DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n", 1861 sc->ext_2ghz_lna, sc->ext_5ghz_lna); 1862 1863 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2); 1864 val = le16toh(val); 1865 if ((val & 0xff) != 0xff) 1866 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */ 1867 1868 /* Only [-10, 10] is valid */ 1869 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10) 1870 sc->rssi_2ghz_corr = 0; 1871 1872 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2); 1873 val = le16toh(val); 1874 if ((val & 0xff) != 0xff) 1875 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */ 1876 1877 /* Only [-10, 10] is valid */ 1878 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10) 1879 sc->rssi_5ghz_corr = 0; 1880 1881 if (sc->ext_2ghz_lna) 1882 sc->rssi_2ghz_corr -= 14; 1883 if (sc->ext_5ghz_lna) 1884 sc->rssi_5ghz_corr -= 14; 1885 1886 DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n", 1887 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr); 1888 1889 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2); 1890 val = le16toh(val); 1891 if ((val & 0xff) != 0xff) 1892 sc->rffreq = val & 0xff; 1893 1894 DPRINTF("RF freq=%d\n", sc->rffreq); 1895 1896 /* read Tx power for all a/b/g channels */ 1897 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14); 1898 /* XXX default Tx power for 802.11a channels */ 1899 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14); 1900#ifdef RUM_DEBUG 1901 for (i = 0; i < 14; i++) 1902 DPRINTF("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]); 1903#endif 1904 1905 /* read default values for BBP registers */ 1906 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16); 1907#ifdef RUM_DEBUG 1908 for (i = 0; i < 14; i++) { 1909 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff) 1910 continue; 1911 DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg, 1912 sc->bbp_prom[i].val); 1913 } 1914#endif 1915} 1916 1917static int 1918rum_bbp_init(struct rum_softc *sc) 1919{ 1920#define N(a) (sizeof (a) / sizeof ((a)[0])) 1921 int i, ntries; 1922 1923 /* wait for BBP to be ready */ 1924 for (ntries = 0; ntries < 100; ntries++) { 1925 const uint8_t val = rum_bbp_read(sc, 0); 1926 if (val != 0 && val != 0xff) 1927 break; 1928 if (rum_pause(sc, hz / 100)) 1929 break; 1930 } 1931 if (ntries == 100) { 1932 device_printf(sc->sc_dev, "timeout waiting for BBP\n"); 1933 return EIO; 1934 } 1935 1936 /* initialize BBP registers to default values */ 1937 for (i = 0; i < N(rum_def_bbp); i++) 1938 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val); 1939 1940 /* write vendor-specific BBP values (from EEPROM) */ 1941 for (i = 0; i < 16; i++) { 1942 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff) 1943 continue; 1944 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 1945 } 1946 1947 return 0; 1948#undef N 1949} 1950 1951static void 1952rum_init_locked(struct rum_softc *sc) 1953{ 1954#define N(a) (sizeof (a) / sizeof ((a)[0])) 1955 struct ifnet *ifp = sc->sc_ifp; 1956 struct ieee80211com *ic = ifp->if_l2com; 1957 uint32_t tmp; 1958 usb_error_t error; 1959 int i, ntries; 1960 1961 RUM_LOCK_ASSERT(sc, MA_OWNED); 1962 1963 rum_stop(sc); 1964 1965 /* initialize MAC registers to default values */ 1966 for (i = 0; i < N(rum_def_mac); i++) 1967 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val); 1968 1969 /* set host ready */ 1970 rum_write(sc, RT2573_MAC_CSR1, 3); 1971 rum_write(sc, RT2573_MAC_CSR1, 0); 1972 1973 /* wait for BBP/RF to wakeup */ 1974 for (ntries = 0; ntries < 100; ntries++) { 1975 if (rum_read(sc, RT2573_MAC_CSR12) & 8) 1976 break; 1977 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */ 1978 if (rum_pause(sc, hz / 100)) 1979 break; 1980 } 1981 if (ntries == 100) { 1982 device_printf(sc->sc_dev, 1983 "timeout waiting for BBP/RF to wakeup\n"); 1984 goto fail; 1985 } 1986 1987 if ((error = rum_bbp_init(sc)) != 0) 1988 goto fail; 1989 1990 /* select default channel */ 1991 rum_select_band(sc, ic->ic_curchan); 1992 rum_select_antenna(sc); 1993 rum_set_chan(sc, ic->ic_curchan); 1994 1995 /* clear STA registers */ 1996 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta); 1997 1998 rum_set_macaddr(sc, IF_LLADDR(ifp)); 1999 2000 /* initialize ASIC */ 2001 rum_write(sc, RT2573_MAC_CSR1, 4); 2002 2003 /* 2004 * Allocate Tx and Rx xfer queues. 2005 */ 2006 rum_setup_tx_list(sc); 2007 2008 /* update Rx filter */ 2009 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff; 2010 2011 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR; 2012 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2013 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR | 2014 RT2573_DROP_ACKCTS; 2015 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 2016 tmp |= RT2573_DROP_TODS; 2017 if (!(ifp->if_flags & IFF_PROMISC)) 2018 tmp |= RT2573_DROP_NOT_TO_ME; 2019 } 2020 rum_write(sc, RT2573_TXRX_CSR0, tmp); 2021 2022 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2023 ifp->if_drv_flags |= IFF_DRV_RUNNING; 2024 usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]); 2025 usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]); 2026 return; 2027 2028fail: rum_stop(sc); 2029#undef N 2030} 2031 2032static void 2033rum_init(void *priv) 2034{ 2035 struct rum_softc *sc = priv; 2036 struct ifnet *ifp = sc->sc_ifp; 2037 struct ieee80211com *ic = ifp->if_l2com; 2038 2039 RUM_LOCK(sc); 2040 rum_init_locked(sc); 2041 RUM_UNLOCK(sc); 2042 2043 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2044 ieee80211_start_all(ic); /* start all vap's */ 2045} 2046 2047static void 2048rum_stop(struct rum_softc *sc) 2049{ 2050 struct ifnet *ifp = sc->sc_ifp; 2051 uint32_t tmp; 2052 2053 RUM_LOCK_ASSERT(sc, MA_OWNED); 2054 2055 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2056 2057 RUM_UNLOCK(sc); 2058 2059 /* 2060 * Drain the USB transfers, if not already drained: 2061 */ 2062 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]); 2063 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]); 2064 2065 RUM_LOCK(sc); 2066 2067 rum_unsetup_tx_list(sc); 2068 2069 /* disable Rx */ 2070 tmp = rum_read(sc, RT2573_TXRX_CSR0); 2071 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX); 2072 2073 /* reset ASIC */ 2074 rum_write(sc, RT2573_MAC_CSR1, 3); 2075 rum_write(sc, RT2573_MAC_CSR1, 0); 2076} 2077 2078static void 2079rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size) 2080{ 2081 struct usb_device_request req; 2082 uint16_t reg = RT2573_MCU_CODE_BASE; 2083 usb_error_t err; 2084 2085 /* copy firmware image into NIC */ 2086 for (; size >= 4; reg += 4, ucode += 4, size -= 4) { 2087 err = rum_write(sc, reg, UGETDW(ucode)); 2088 if (err) { 2089 /* firmware already loaded ? */ 2090 device_printf(sc->sc_dev, "Firmware load " 2091 "failure! (ignored)\n"); 2092 break; 2093 } 2094 } 2095 2096 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 2097 req.bRequest = RT2573_MCU_CNTL; 2098 USETW(req.wValue, RT2573_MCU_RUN); 2099 USETW(req.wIndex, 0); 2100 USETW(req.wLength, 0); 2101 2102 err = rum_do_request(sc, &req, NULL); 2103 if (err != 0) { 2104 device_printf(sc->sc_dev, "could not run firmware: %s\n", 2105 usbd_errstr(err)); 2106 } 2107 2108 /* give the chip some time to boot */ 2109 rum_pause(sc, hz / 8); 2110} 2111 2112static int 2113rum_prepare_beacon(struct rum_softc *sc, struct ieee80211vap *vap) 2114{ 2115 struct ieee80211com *ic = vap->iv_ic; 2116 const struct ieee80211_txparam *tp; 2117 struct rum_tx_desc desc; 2118 struct mbuf *m0; 2119 2120 m0 = ieee80211_beacon_alloc(vap->iv_bss, &RUM_VAP(vap)->bo); 2121 if (m0 == NULL) { 2122 return ENOBUFS; 2123 } 2124 2125 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)]; 2126 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ, 2127 m0->m_pkthdr.len, tp->mgmtrate); 2128 2129 /* copy the first 24 bytes of Tx descriptor into NIC memory */ 2130 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24); 2131 2132 /* copy beacon header and payload into NIC memory */ 2133 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *), 2134 m0->m_pkthdr.len); 2135 2136 m_freem(m0); 2137 2138 return 0; 2139} 2140 2141static int 2142rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2143 const struct ieee80211_bpf_params *params) 2144{ 2145 struct ifnet *ifp = ni->ni_ic->ic_ifp; 2146 struct rum_softc *sc = ifp->if_softc; 2147 2148 RUM_LOCK(sc); 2149 /* prevent management frames from being sent if we're not ready */ 2150 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 2151 RUM_UNLOCK(sc); 2152 m_freem(m); 2153 ieee80211_free_node(ni); 2154 return ENETDOWN; 2155 } 2156 if (sc->tx_nfree < RUM_TX_MINFREE) { 2157 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2158 RUM_UNLOCK(sc); 2159 m_freem(m); 2160 ieee80211_free_node(ni); 2161 return EIO; 2162 } 2163 2164 ifp->if_opackets++; 2165 2166 if (params == NULL) { 2167 /* 2168 * Legacy path; interpret frame contents to decide 2169 * precisely how to send the frame. 2170 */ 2171 if (rum_tx_mgt(sc, m, ni) != 0) 2172 goto bad; 2173 } else { 2174 /* 2175 * Caller supplied explicit parameters to use in 2176 * sending the frame. 2177 */ 2178 if (rum_tx_raw(sc, m, ni, params) != 0) 2179 goto bad; 2180 } 2181 RUM_UNLOCK(sc); 2182 2183 return 0; 2184bad: 2185 ifp->if_oerrors++; 2186 RUM_UNLOCK(sc); 2187 ieee80211_free_node(ni); 2188 return EIO; 2189} 2190 2191static void 2192rum_amrr_start(struct rum_softc *sc, struct ieee80211_node *ni) 2193{ 2194 struct ieee80211vap *vap = ni->ni_vap; 2195 struct rum_vap *rvp = RUM_VAP(vap); 2196 2197 /* clear statistic registers (STA_CSR0 to STA_CSR5) */ 2198 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta); 2199 2200 ieee80211_amrr_node_init(&rvp->amrr, &RUM_NODE(ni)->amn, ni); 2201 2202 usb_callout_reset(&rvp->amrr_ch, hz, rum_amrr_timeout, rvp); 2203} 2204 2205static void 2206rum_amrr_timeout(void *arg) 2207{ 2208 struct rum_vap *rvp = arg; 2209 struct ieee80211vap *vap = &rvp->vap; 2210 struct ieee80211com *ic = vap->iv_ic; 2211 2212 ieee80211_runtask(ic, &rvp->amrr_task); 2213} 2214 2215static void 2216rum_amrr_task(void *arg, int pending) 2217{ 2218 struct rum_vap *rvp = arg; 2219 struct ieee80211vap *vap = &rvp->vap; 2220 struct ieee80211com *ic = vap->iv_ic; 2221 struct ifnet *ifp = ic->ic_ifp; 2222 struct rum_softc *sc = ifp->if_softc; 2223 struct ieee80211_node *ni = vap->iv_bss; 2224 int ok, fail; 2225 2226 RUM_LOCK(sc); 2227 /* read and clear statistic registers (STA_CSR0 to STA_CSR10) */ 2228 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta)); 2229 2230 ok = (le32toh(sc->sta[4]) >> 16) + /* TX ok w/o retry */ 2231 (le32toh(sc->sta[5]) & 0xffff); /* TX ok w/ retry */ 2232 fail = (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */ 2233 2234 ieee80211_amrr_tx_update(&RUM_NODE(ni)->amn, 2235 ok+fail, ok, (le32toh(sc->sta[5]) & 0xffff) + fail); 2236 (void) ieee80211_amrr_choose(ni, &RUM_NODE(ni)->amn); 2237 2238 ifp->if_oerrors += fail; /* count TX retry-fail as Tx errors */ 2239 2240 usb_callout_reset(&rvp->amrr_ch, hz, rum_amrr_timeout, rvp); 2241 RUM_UNLOCK(sc); 2242} 2243 2244/* ARGUSED */ 2245static struct ieee80211_node * 2246rum_node_alloc(struct ieee80211vap *vap __unused, 2247 const uint8_t mac[IEEE80211_ADDR_LEN] __unused) 2248{ 2249 struct rum_node *rn; 2250 2251 rn = malloc(sizeof(struct rum_node), M_80211_NODE, M_NOWAIT | M_ZERO); 2252 return rn != NULL ? &rn->ni : NULL; 2253} 2254 2255static void 2256rum_newassoc(struct ieee80211_node *ni, int isnew) 2257{ 2258 struct ieee80211vap *vap = ni->ni_vap; 2259 2260 ieee80211_amrr_node_init(&RUM_VAP(vap)->amrr, &RUM_NODE(ni)->amn, ni); 2261} 2262 2263static void 2264rum_scan_start(struct ieee80211com *ic) 2265{ 2266 struct ifnet *ifp = ic->ic_ifp; 2267 struct rum_softc *sc = ifp->if_softc; 2268 uint32_t tmp; 2269 2270 RUM_LOCK(sc); 2271 /* abort TSF synchronization */ 2272 tmp = rum_read(sc, RT2573_TXRX_CSR9); 2273 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff); 2274 rum_set_bssid(sc, ifp->if_broadcastaddr); 2275 RUM_UNLOCK(sc); 2276 2277} 2278 2279static void 2280rum_scan_end(struct ieee80211com *ic) 2281{ 2282 struct rum_softc *sc = ic->ic_ifp->if_softc; 2283 2284 RUM_LOCK(sc); 2285 rum_enable_tsf_sync(sc); 2286 rum_set_bssid(sc, sc->sc_bssid); 2287 RUM_UNLOCK(sc); 2288 2289} 2290 2291static void 2292rum_set_channel(struct ieee80211com *ic) 2293{ 2294 struct rum_softc *sc = ic->ic_ifp->if_softc; 2295 2296 RUM_LOCK(sc); 2297 rum_set_chan(sc, ic->ic_curchan); 2298 RUM_UNLOCK(sc); 2299} 2300 2301static int 2302rum_get_rssi(struct rum_softc *sc, uint8_t raw) 2303{ 2304 struct ifnet *ifp = sc->sc_ifp; 2305 struct ieee80211com *ic = ifp->if_l2com; 2306 int lna, agc, rssi; 2307 2308 lna = (raw >> 5) & 0x3; 2309 agc = raw & 0x1f; 2310 2311 if (lna == 0) { 2312 /* 2313 * No RSSI mapping 2314 * 2315 * NB: Since RSSI is relative to noise floor, -1 is 2316 * adequate for caller to know error happened. 2317 */ 2318 return -1; 2319 } 2320 2321 rssi = (2 * agc) - RT2573_NOISE_FLOOR; 2322 2323 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) { 2324 rssi += sc->rssi_2ghz_corr; 2325 2326 if (lna == 1) 2327 rssi -= 64; 2328 else if (lna == 2) 2329 rssi -= 74; 2330 else if (lna == 3) 2331 rssi -= 90; 2332 } else { 2333 rssi += sc->rssi_5ghz_corr; 2334 2335 if (!sc->ext_5ghz_lna && lna != 1) 2336 rssi += 4; 2337 2338 if (lna == 1) 2339 rssi -= 64; 2340 else if (lna == 2) 2341 rssi -= 86; 2342 else if (lna == 3) 2343 rssi -= 100; 2344 } 2345 return rssi; 2346} 2347 2348static int 2349rum_pause(struct rum_softc *sc, int timeout) 2350{ 2351 2352 usb_pause_mtx(&sc->sc_mtx, timeout); 2353 return (0); 2354} 2355 2356static device_method_t rum_methods[] = { 2357 /* Device interface */ 2358 DEVMETHOD(device_probe, rum_match), 2359 DEVMETHOD(device_attach, rum_attach), 2360 DEVMETHOD(device_detach, rum_detach), 2361 2362 { 0, 0 } 2363}; 2364 2365static driver_t rum_driver = { 2366 .name = "rum", 2367 .methods = rum_methods, 2368 .size = sizeof(struct rum_softc), 2369}; 2370 2371static devclass_t rum_devclass; 2372 2373DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0); 2374